1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include <linux/string.h>
27 #include <linux/acpi.h>
28 #include <linux/i2c.h>
29 
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/amdgpu_drm.h>
33 #include <drm/drm_edid.h>
34 
35 #include "dm_services.h"
36 #include "amdgpu.h"
37 #include "dc.h"
38 #include "amdgpu_dm.h"
39 #include "amdgpu_dm_irq.h"
40 #include "amdgpu_dm_mst_types.h"
41 #include "dpcd_defs.h"
42 #include "dc/inc/core_types.h"
43 
44 #include "dm_helpers.h"
45 #include "ddc_service_types.h"
46 
edid_extract_panel_id(struct edid * edid)47 static u32 edid_extract_panel_id(struct edid *edid)
48 {
49 	return (u32)edid->mfg_id[0] << 24   |
50 	       (u32)edid->mfg_id[1] << 16   |
51 	       (u32)EDID_PRODUCT_ID(edid);
52 }
53 
apply_edid_quirks(struct edid * edid,struct dc_edid_caps * edid_caps)54 static void apply_edid_quirks(struct edid *edid, struct dc_edid_caps *edid_caps)
55 {
56 	uint32_t panel_id = edid_extract_panel_id(edid);
57 
58 	switch (panel_id) {
59 	/* Workaround for some monitors which does not work well with FAMS */
60 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x0E5E):
61 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x7053):
62 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x71AC):
63 		DRM_DEBUG_DRIVER("Disabling FAMS on monitor with panel id %X\n", panel_id);
64 		edid_caps->panel_patch.disable_fams = true;
65 		break;
66 	/* Workaround for some monitors that do not clear DPCD 0x317 if FreeSync is unsupported */
67 	case drm_edid_encode_panel_id('A', 'U', 'O', 0xA7AB):
68 	case drm_edid_encode_panel_id('A', 'U', 'O', 0xE69B):
69 	case drm_edid_encode_panel_id('B', 'O', 'E', 0x092A):
70 	case drm_edid_encode_panel_id('L', 'G', 'D', 0x06D1):
71 		DRM_DEBUG_DRIVER("Clearing DPCD 0x317 on monitor with panel id %X\n", panel_id);
72 		edid_caps->panel_patch.remove_sink_ext_caps = true;
73 		break;
74 	default:
75 		return;
76 	}
77 }
78 
79 /**
80  * dm_helpers_parse_edid_caps() - Parse edid caps
81  *
82  * @link: current detected link
83  * @edid:	[in] pointer to edid
84  * @edid_caps:	[in] pointer to edid caps
85  *
86  * Return: void
87  */
dm_helpers_parse_edid_caps(struct dc_link * link,const struct dc_edid * edid,struct dc_edid_caps * edid_caps)88 enum dc_edid_status dm_helpers_parse_edid_caps(
89 		struct dc_link *link,
90 		const struct dc_edid *edid,
91 		struct dc_edid_caps *edid_caps)
92 {
93 	struct amdgpu_dm_connector *aconnector = link->priv;
94 	struct drm_connector *connector = &aconnector->base;
95 	struct edid *edid_buf = edid ? (struct edid *) edid->raw_edid : NULL;
96 	struct cea_sad *sads;
97 	int sad_count = -1;
98 	int sadb_count = -1;
99 	int i = 0;
100 	uint8_t *sadb = NULL;
101 
102 	enum dc_edid_status result = EDID_OK;
103 
104 	if (!edid_caps || !edid)
105 		return EDID_BAD_INPUT;
106 
107 	if (!drm_edid_is_valid(edid_buf))
108 		result = EDID_BAD_CHECKSUM;
109 
110 	edid_caps->manufacturer_id = (uint16_t) edid_buf->mfg_id[0] |
111 					((uint16_t) edid_buf->mfg_id[1])<<8;
112 	edid_caps->product_id = (uint16_t) edid_buf->prod_code[0] |
113 					((uint16_t) edid_buf->prod_code[1])<<8;
114 	edid_caps->serial_number = edid_buf->serial;
115 	edid_caps->manufacture_week = edid_buf->mfg_week;
116 	edid_caps->manufacture_year = edid_buf->mfg_year;
117 
118 	drm_edid_get_monitor_name(edid_buf,
119 				  edid_caps->display_name,
120 				  AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
121 
122 	edid_caps->edid_hdmi = connector->display_info.is_hdmi;
123 
124 	apply_edid_quirks(edid_buf, edid_caps);
125 
126 	sad_count = drm_edid_to_sad((struct edid *) edid->raw_edid, &sads);
127 	if (sad_count <= 0)
128 		return result;
129 
130 	edid_caps->audio_mode_count = min(sad_count, DC_MAX_AUDIO_DESC_COUNT);
131 	for (i = 0; i < edid_caps->audio_mode_count; ++i) {
132 		struct cea_sad *sad = &sads[i];
133 
134 		edid_caps->audio_modes[i].format_code = sad->format;
135 		edid_caps->audio_modes[i].channel_count = sad->channels + 1;
136 		edid_caps->audio_modes[i].sample_rate = sad->freq;
137 		edid_caps->audio_modes[i].sample_size = sad->byte2;
138 	}
139 
140 	sadb_count = drm_edid_to_speaker_allocation((struct edid *) edid->raw_edid, &sadb);
141 
142 	if (sadb_count < 0) {
143 		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sadb_count);
144 		sadb_count = 0;
145 	}
146 
147 	if (sadb_count)
148 		edid_caps->speaker_flags = sadb[0];
149 	else
150 		edid_caps->speaker_flags = DEFAULT_SPEAKER_LOCATION;
151 
152 	kfree(sads);
153 	kfree(sadb);
154 
155 	return result;
156 }
157 
158 static void
fill_dc_mst_payload_table_from_drm(struct dc_link * link,bool enable,struct drm_dp_mst_atomic_payload * target_payload,struct dc_dp_mst_stream_allocation_table * table)159 fill_dc_mst_payload_table_from_drm(struct dc_link *link,
160 				   bool enable,
161 				   struct drm_dp_mst_atomic_payload *target_payload,
162 				   struct dc_dp_mst_stream_allocation_table *table)
163 {
164 	struct dc_dp_mst_stream_allocation_table new_table = { 0 };
165 	struct dc_dp_mst_stream_allocation *sa;
166 	struct link_mst_stream_allocation_table copy_of_link_table =
167 										link->mst_stream_alloc_table;
168 
169 	int i;
170 	int current_hw_table_stream_cnt = copy_of_link_table.stream_count;
171 	struct link_mst_stream_allocation *dc_alloc;
172 
173 	/* TODO: refactor to set link->mst_stream_alloc_table directly if possible.*/
174 	if (enable) {
175 		dc_alloc =
176 		&copy_of_link_table.stream_allocations[current_hw_table_stream_cnt];
177 		dc_alloc->vcp_id = target_payload->vcpi;
178 		dc_alloc->slot_count = target_payload->time_slots;
179 	} else {
180 		for (i = 0; i < copy_of_link_table.stream_count; i++) {
181 			dc_alloc =
182 			&copy_of_link_table.stream_allocations[i];
183 
184 			if (dc_alloc->vcp_id == target_payload->vcpi) {
185 				dc_alloc->vcp_id = 0;
186 				dc_alloc->slot_count = 0;
187 				break;
188 			}
189 		}
190 		ASSERT(i != copy_of_link_table.stream_count);
191 	}
192 
193 	/* Fill payload info*/
194 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
195 		dc_alloc =
196 			&copy_of_link_table.stream_allocations[i];
197 		if (dc_alloc->vcp_id > 0 && dc_alloc->slot_count > 0) {
198 			sa = &new_table.stream_allocations[new_table.stream_count];
199 			sa->slot_count = dc_alloc->slot_count;
200 			sa->vcp_id = dc_alloc->vcp_id;
201 			new_table.stream_count++;
202 		}
203 	}
204 
205 	/* Overwrite the old table */
206 	*table = new_table;
207 }
208 
dm_helpers_dp_update_branch_info(struct dc_context * ctx,const struct dc_link * link)209 void dm_helpers_dp_update_branch_info(
210 	struct dc_context *ctx,
211 	const struct dc_link *link)
212 {}
213 
dm_helpers_construct_old_payload(struct dc_link * link,int pbn_per_slot,struct drm_dp_mst_atomic_payload * new_payload,struct drm_dp_mst_atomic_payload * old_payload)214 static void dm_helpers_construct_old_payload(
215 			struct dc_link *link,
216 			int pbn_per_slot,
217 			struct drm_dp_mst_atomic_payload *new_payload,
218 			struct drm_dp_mst_atomic_payload *old_payload)
219 {
220 	struct link_mst_stream_allocation_table current_link_table =
221 									link->mst_stream_alloc_table;
222 	struct link_mst_stream_allocation *dc_alloc;
223 	int i;
224 
225 	*old_payload = *new_payload;
226 
227 	/* Set correct time_slots/PBN of old payload.
228 	 * other fields (delete & dsc_enabled) in
229 	 * struct drm_dp_mst_atomic_payload are don't care fields
230 	 * while calling drm_dp_remove_payload()
231 	 */
232 	for (i = 0; i < current_link_table.stream_count; i++) {
233 		dc_alloc =
234 			&current_link_table.stream_allocations[i];
235 
236 		if (dc_alloc->vcp_id == new_payload->vcpi) {
237 			old_payload->time_slots = dc_alloc->slot_count;
238 			old_payload->pbn = dc_alloc->slot_count * pbn_per_slot;
239 			break;
240 		}
241 	}
242 
243 	/* make sure there is an old payload*/
244 	ASSERT(i != current_link_table.stream_count);
245 
246 }
247 
248 /*
249  * Writes payload allocation table in immediate downstream device.
250  */
dm_helpers_dp_mst_write_payload_allocation_table(struct dc_context * ctx,const struct dc_stream_state * stream,struct dc_dp_mst_stream_allocation_table * proposed_table,bool enable)251 bool dm_helpers_dp_mst_write_payload_allocation_table(
252 		struct dc_context *ctx,
253 		const struct dc_stream_state *stream,
254 		struct dc_dp_mst_stream_allocation_table *proposed_table,
255 		bool enable)
256 {
257 	struct amdgpu_dm_connector *aconnector;
258 	struct drm_dp_mst_topology_state *mst_state;
259 	struct drm_dp_mst_atomic_payload *target_payload, *new_payload, old_payload;
260 	struct drm_dp_mst_topology_mgr *mst_mgr;
261 
262 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
263 	/* Accessing the connector state is required for vcpi_slots allocation
264 	 * and directly relies on behaviour in commit check
265 	 * that blocks before commit guaranteeing that the state
266 	 * is not gonna be swapped while still in use in commit tail
267 	 */
268 
269 	if (!aconnector || !aconnector->mst_root)
270 		return false;
271 
272 	mst_mgr = &aconnector->mst_root->mst_mgr;
273 	mst_state = to_drm_dp_mst_topology_state(mst_mgr->base.state);
274 
275 	/* It's OK for this to fail */
276 	new_payload = drm_atomic_get_mst_payload_state(mst_state, aconnector->mst_output_port);
277 
278 	if (enable) {
279 		target_payload = new_payload;
280 
281 		drm_dp_add_payload_part1(mst_mgr, mst_state, new_payload);
282 	} else {
283 		/* construct old payload by VCPI*/
284 		dm_helpers_construct_old_payload(stream->link, mst_state->pbn_div,
285 						new_payload, &old_payload);
286 		target_payload = &old_payload;
287 
288 		drm_dp_remove_payload(mst_mgr, mst_state, &old_payload, new_payload);
289 	}
290 
291 	/* mst_mgr->->payloads are VC payload notify MST branch using DPCD or
292 	 * AUX message. The sequence is slot 1-63 allocated sequence for each
293 	 * stream. AMD ASIC stream slot allocation should follow the same
294 	 * sequence. copy DRM MST allocation to dc
295 	 */
296 	fill_dc_mst_payload_table_from_drm(stream->link, enable, target_payload, proposed_table);
297 
298 	return true;
299 }
300 
301 /*
302  * poll pending down reply
303  */
dm_helpers_dp_mst_poll_pending_down_reply(struct dc_context * ctx,const struct dc_link * link)304 void dm_helpers_dp_mst_poll_pending_down_reply(
305 	struct dc_context *ctx,
306 	const struct dc_link *link)
307 {}
308 
309 /*
310  * Clear payload allocation table before enable MST DP link.
311  */
dm_helpers_dp_mst_clear_payload_allocation_table(struct dc_context * ctx,const struct dc_link * link)312 void dm_helpers_dp_mst_clear_payload_allocation_table(
313 	struct dc_context *ctx,
314 	const struct dc_link *link)
315 {}
316 
317 /*
318  * Polls for ACT (allocation change trigger) handled and sends
319  * ALLOCATE_PAYLOAD message.
320  */
dm_helpers_dp_mst_poll_for_allocation_change_trigger(struct dc_context * ctx,const struct dc_stream_state * stream)321 enum act_return_status dm_helpers_dp_mst_poll_for_allocation_change_trigger(
322 		struct dc_context *ctx,
323 		const struct dc_stream_state *stream)
324 {
325 	struct amdgpu_dm_connector *aconnector;
326 	struct drm_dp_mst_topology_mgr *mst_mgr;
327 	int ret;
328 
329 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
330 
331 	if (!aconnector || !aconnector->mst_root)
332 		return ACT_FAILED;
333 
334 	mst_mgr = &aconnector->mst_root->mst_mgr;
335 
336 	if (!mst_mgr->mst_state)
337 		return ACT_FAILED;
338 
339 	ret = drm_dp_check_act_status(mst_mgr);
340 
341 	if (ret)
342 		return ACT_FAILED;
343 
344 	return ACT_SUCCESS;
345 }
346 
dm_helpers_dp_mst_send_payload_allocation(struct dc_context * ctx,const struct dc_stream_state * stream,bool enable)347 bool dm_helpers_dp_mst_send_payload_allocation(
348 		struct dc_context *ctx,
349 		const struct dc_stream_state *stream,
350 		bool enable)
351 {
352 	struct amdgpu_dm_connector *aconnector;
353 	struct drm_dp_mst_topology_state *mst_state;
354 	struct drm_dp_mst_topology_mgr *mst_mgr;
355 	struct drm_dp_mst_atomic_payload *payload;
356 	enum mst_progress_status set_flag = MST_ALLOCATE_NEW_PAYLOAD;
357 	enum mst_progress_status clr_flag = MST_CLEAR_ALLOCATED_PAYLOAD;
358 	int ret = 0;
359 
360 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
361 
362 	if (!aconnector || !aconnector->mst_root)
363 		return false;
364 
365 	mst_mgr = &aconnector->mst_root->mst_mgr;
366 	mst_state = to_drm_dp_mst_topology_state(mst_mgr->base.state);
367 
368 	payload = drm_atomic_get_mst_payload_state(mst_state, aconnector->mst_output_port);
369 
370 	if (!enable) {
371 		set_flag = MST_CLEAR_ALLOCATED_PAYLOAD;
372 		clr_flag = MST_ALLOCATE_NEW_PAYLOAD;
373 	}
374 
375 	if (enable)
376 		ret = drm_dp_add_payload_part2(mst_mgr, mst_state->base.state, payload);
377 
378 	if (ret) {
379 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
380 			set_flag, false);
381 	} else {
382 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
383 			set_flag, true);
384 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
385 			clr_flag, false);
386 	}
387 
388 	return true;
389 }
390 
dm_dtn_log_begin(struct dc_context * ctx,struct dc_log_buffer_ctx * log_ctx)391 void dm_dtn_log_begin(struct dc_context *ctx,
392 	struct dc_log_buffer_ctx *log_ctx)
393 {
394 	static const char msg[] = "[dtn begin]\n";
395 
396 	if (!log_ctx) {
397 		pr_info("%s", msg);
398 		return;
399 	}
400 
401 	dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
402 }
403 
404 __printf(3, 4)
dm_dtn_log_append_v(struct dc_context * ctx,struct dc_log_buffer_ctx * log_ctx,const char * msg,...)405 void dm_dtn_log_append_v(struct dc_context *ctx,
406 	struct dc_log_buffer_ctx *log_ctx,
407 	const char *msg, ...)
408 {
409 	va_list args;
410 	size_t total;
411 	int n;
412 
413 	if (!log_ctx) {
414 		/* No context, redirect to dmesg. */
415 		struct va_format vaf;
416 
417 		vaf.fmt = msg;
418 		vaf.va = &args;
419 
420 		va_start(args, msg);
421 		pr_info("%pV", &vaf);
422 		va_end(args);
423 
424 		return;
425 	}
426 
427 	/* Measure the output. */
428 	va_start(args, msg);
429 	n = vsnprintf(NULL, 0, msg, args);
430 	va_end(args);
431 
432 	if (n <= 0)
433 		return;
434 
435 	/* Reallocate the string buffer as needed. */
436 	total = log_ctx->pos + n + 1;
437 
438 	if (total > log_ctx->size) {
439 		char *buf = kvcalloc(total, sizeof(char), GFP_KERNEL);
440 
441 		if (buf) {
442 			memcpy(buf, log_ctx->buf, log_ctx->pos);
443 			kfree(log_ctx->buf);
444 
445 			log_ctx->buf = buf;
446 			log_ctx->size = total;
447 		}
448 	}
449 
450 	if (!log_ctx->buf)
451 		return;
452 
453 	/* Write the formatted string to the log buffer. */
454 	va_start(args, msg);
455 	n = vscnprintf(
456 		log_ctx->buf + log_ctx->pos,
457 		log_ctx->size - log_ctx->pos,
458 		msg,
459 		args);
460 	va_end(args);
461 
462 	if (n > 0)
463 		log_ctx->pos += n;
464 }
465 
dm_dtn_log_end(struct dc_context * ctx,struct dc_log_buffer_ctx * log_ctx)466 void dm_dtn_log_end(struct dc_context *ctx,
467 	struct dc_log_buffer_ctx *log_ctx)
468 {
469 	static const char msg[] = "[dtn end]\n";
470 
471 	if (!log_ctx) {
472 		pr_info("%s", msg);
473 		return;
474 	}
475 
476 	dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
477 }
478 
dm_helpers_dp_mst_start_top_mgr(struct dc_context * ctx,const struct dc_link * link,bool boot)479 bool dm_helpers_dp_mst_start_top_mgr(
480 		struct dc_context *ctx,
481 		const struct dc_link *link,
482 		bool boot)
483 {
484 	struct amdgpu_dm_connector *aconnector = link->priv;
485 	int ret;
486 
487 	if (!aconnector) {
488 		DRM_ERROR("Failed to find connector for link!");
489 		return false;
490 	}
491 
492 	if (boot) {
493 		DRM_INFO("DM_MST: Differing MST start on aconnector: %p [id: %d]\n",
494 					aconnector, aconnector->base.base.id);
495 		return true;
496 	}
497 
498 	DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
499 			aconnector, aconnector->base.base.id);
500 
501 	ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
502 	if (ret < 0) {
503 		DRM_ERROR("DM_MST: Failed to set the device into MST mode!");
504 		return false;
505 	}
506 
507 	DRM_INFO("DM_MST: DP%x, %d-lane link detected\n", aconnector->mst_mgr.dpcd[0],
508 		aconnector->mst_mgr.dpcd[2] & DP_MAX_LANE_COUNT_MASK);
509 
510 	return true;
511 }
512 
dm_helpers_dp_mst_stop_top_mgr(struct dc_context * ctx,struct dc_link * link)513 bool dm_helpers_dp_mst_stop_top_mgr(
514 		struct dc_context *ctx,
515 		struct dc_link *link)
516 {
517 	struct amdgpu_dm_connector *aconnector = link->priv;
518 
519 	if (!aconnector) {
520 		DRM_ERROR("Failed to find connector for link!");
521 		return false;
522 	}
523 
524 	DRM_INFO("DM_MST: stopping TM on aconnector: %p [id: %d]\n",
525 			aconnector, aconnector->base.base.id);
526 
527 	if (aconnector->mst_mgr.mst_state == true) {
528 		drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, false);
529 		link->cur_link_settings.lane_count = 0;
530 	}
531 
532 	return false;
533 }
534 
dm_helpers_dp_read_dpcd(struct dc_context * ctx,const struct dc_link * link,uint32_t address,uint8_t * data,uint32_t size)535 bool dm_helpers_dp_read_dpcd(
536 		struct dc_context *ctx,
537 		const struct dc_link *link,
538 		uint32_t address,
539 		uint8_t *data,
540 		uint32_t size)
541 {
542 
543 	struct amdgpu_dm_connector *aconnector = link->priv;
544 
545 	if (!aconnector) {
546 		DC_LOG_DC("Failed to find connector for link!\n");
547 		return false;
548 	}
549 
550 	return drm_dp_dpcd_read(&aconnector->dm_dp_aux.aux, address, data,
551 				size) == size;
552 }
553 
dm_helpers_dp_write_dpcd(struct dc_context * ctx,const struct dc_link * link,uint32_t address,const uint8_t * data,uint32_t size)554 bool dm_helpers_dp_write_dpcd(
555 		struct dc_context *ctx,
556 		const struct dc_link *link,
557 		uint32_t address,
558 		const uint8_t *data,
559 		uint32_t size)
560 {
561 	struct amdgpu_dm_connector *aconnector = link->priv;
562 
563 	if (!aconnector) {
564 		DRM_ERROR("Failed to find connector for link!");
565 		return false;
566 	}
567 
568 	return drm_dp_dpcd_write(&aconnector->dm_dp_aux.aux,
569 			address, (uint8_t *)data, size) > 0;
570 }
571 
dm_helpers_submit_i2c(struct dc_context * ctx,const struct dc_link * link,struct i2c_command * cmd)572 bool dm_helpers_submit_i2c(
573 		struct dc_context *ctx,
574 		const struct dc_link *link,
575 		struct i2c_command *cmd)
576 {
577 	struct amdgpu_dm_connector *aconnector = link->priv;
578 	struct i2c_msg *msgs;
579 	int i = 0;
580 	int num = cmd->number_of_payloads;
581 	bool result;
582 
583 	if (!aconnector) {
584 		DRM_ERROR("Failed to find connector for link!");
585 		return false;
586 	}
587 
588 	msgs = kcalloc(num, sizeof(struct i2c_msg), GFP_KERNEL);
589 
590 	if (!msgs)
591 		return false;
592 
593 	for (i = 0; i < num; i++) {
594 		msgs[i].flags = cmd->payloads[i].write ? 0 : I2C_M_RD;
595 		msgs[i].addr = cmd->payloads[i].address;
596 		msgs[i].len = cmd->payloads[i].length;
597 		msgs[i].buf = cmd->payloads[i].data;
598 	}
599 
600 	result = i2c_transfer(&aconnector->i2c->base, msgs, num) == num;
601 
602 	kfree(msgs);
603 
604 	return result;
605 }
606 
execute_synaptics_rc_command(struct drm_dp_aux * aux,bool is_write_cmd,unsigned char cmd,unsigned int length,unsigned int offset,unsigned char * data)607 static bool execute_synaptics_rc_command(struct drm_dp_aux *aux,
608 		bool is_write_cmd,
609 		unsigned char cmd,
610 		unsigned int length,
611 		unsigned int offset,
612 		unsigned char *data)
613 {
614 	bool success = false;
615 	unsigned char rc_data[16] = {0};
616 	unsigned char rc_offset[4] = {0};
617 	unsigned char rc_length[2] = {0};
618 	unsigned char rc_cmd = 0;
619 	unsigned char rc_result = 0xFF;
620 	unsigned char i = 0;
621 	int ret;
622 
623 	if (is_write_cmd) {
624 		// write rc data
625 		memmove(rc_data, data, length);
626 		ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_DATA, rc_data, sizeof(rc_data));
627 	}
628 
629 	// write rc offset
630 	rc_offset[0] = (unsigned char) offset & 0xFF;
631 	rc_offset[1] = (unsigned char) (offset >> 8) & 0xFF;
632 	rc_offset[2] = (unsigned char) (offset >> 16) & 0xFF;
633 	rc_offset[3] = (unsigned char) (offset >> 24) & 0xFF;
634 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_OFFSET, rc_offset, sizeof(rc_offset));
635 
636 	// write rc length
637 	rc_length[0] = (unsigned char) length & 0xFF;
638 	rc_length[1] = (unsigned char) (length >> 8) & 0xFF;
639 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_LENGTH, rc_length, sizeof(rc_length));
640 
641 	// write rc cmd
642 	rc_cmd = cmd | 0x80;
643 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_COMMAND, &rc_cmd, sizeof(rc_cmd));
644 
645 	if (ret < 0) {
646 		DRM_ERROR("%s: write cmd ..., err = %d\n",  __func__, ret);
647 		return false;
648 	}
649 
650 	// poll until active is 0
651 	for (i = 0; i < 10; i++) {
652 		drm_dp_dpcd_read(aux, SYNAPTICS_RC_COMMAND, &rc_cmd, sizeof(rc_cmd));
653 		if (rc_cmd == cmd)
654 			// active is 0
655 			break;
656 		msleep(10);
657 	}
658 
659 	// read rc result
660 	drm_dp_dpcd_read(aux, SYNAPTICS_RC_RESULT, &rc_result, sizeof(rc_result));
661 	success = (rc_result == 0);
662 
663 	if (success && !is_write_cmd) {
664 		// read rc data
665 		drm_dp_dpcd_read(aux, SYNAPTICS_RC_DATA, data, length);
666 	}
667 
668 	DC_LOG_DC("%s: success = %d\n", __func__, success);
669 
670 	return success;
671 }
672 
apply_synaptics_fifo_reset_wa(struct drm_dp_aux * aux)673 static void apply_synaptics_fifo_reset_wa(struct drm_dp_aux *aux)
674 {
675 	unsigned char data[16] = {0};
676 
677 	DC_LOG_DC("Start %s\n", __func__);
678 
679 	// Step 2
680 	data[0] = 'P';
681 	data[1] = 'R';
682 	data[2] = 'I';
683 	data[3] = 'U';
684 	data[4] = 'S';
685 
686 	if (!execute_synaptics_rc_command(aux, true, 0x01, 5, 0, data))
687 		return;
688 
689 	// Step 3 and 4
690 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220998, data))
691 		return;
692 
693 	data[0] &= (~(1 << 1)); // set bit 1 to 0
694 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220998, data))
695 		return;
696 
697 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220D98, data))
698 		return;
699 
700 	data[0] &= (~(1 << 1)); // set bit 1 to 0
701 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220D98, data))
702 		return;
703 
704 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x221198, data))
705 		return;
706 
707 	data[0] &= (~(1 << 1)); // set bit 1 to 0
708 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x221198, data))
709 		return;
710 
711 	// Step 3 and 5
712 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220998, data))
713 		return;
714 
715 	data[0] |= (1 << 1); // set bit 1 to 1
716 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220998, data))
717 		return;
718 
719 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220D98, data))
720 		return;
721 
722 	data[0] |= (1 << 1); // set bit 1 to 1
723 
724 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x221198, data))
725 		return;
726 
727 	data[0] |= (1 << 1); // set bit 1 to 1
728 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x221198, data))
729 		return;
730 
731 	// Step 6
732 	if (!execute_synaptics_rc_command(aux, true, 0x02, 0, 0, NULL))
733 		return;
734 
735 	DC_LOG_DC("Done %s\n", __func__);
736 }
737 
738 /* MST Dock */
739 static const uint8_t SYNAPTICS_DEVICE_ID[] = "SYNA";
740 
write_dsc_enable_synaptics_non_virtual_dpcd_mst(struct drm_dp_aux * aux,const struct dc_stream_state * stream,bool enable)741 static uint8_t write_dsc_enable_synaptics_non_virtual_dpcd_mst(
742 		struct drm_dp_aux *aux,
743 		const struct dc_stream_state *stream,
744 		bool enable)
745 {
746 	uint8_t ret = 0;
747 
748 	DC_LOG_DC("Configure DSC to non-virtual dpcd synaptics\n");
749 
750 	if (enable) {
751 		/* When DSC is enabled on previous boot and reboot with the hub,
752 		 * there is a chance that Synaptics hub gets stuck during reboot sequence.
753 		 * Applying a workaround to reset Synaptics SDP fifo before enabling the first stream
754 		 */
755 		if (!stream->link->link_status.link_active &&
756 			memcmp(stream->link->dpcd_caps.branch_dev_name,
757 				(int8_t *)SYNAPTICS_DEVICE_ID, 4) == 0)
758 			apply_synaptics_fifo_reset_wa(aux);
759 
760 		ret = drm_dp_dpcd_write(aux, DP_DSC_ENABLE, &enable, 1);
761 		DRM_INFO("Send DSC enable to synaptics\n");
762 
763 	} else {
764 		/* Synaptics hub not support virtual dpcd,
765 		 * external monitor occur garbage while disable DSC,
766 		 * Disable DSC only when entire link status turn to false,
767 		 */
768 		if (!stream->link->link_status.link_active) {
769 			ret = drm_dp_dpcd_write(aux, DP_DSC_ENABLE, &enable, 1);
770 			DRM_INFO("Send DSC disable to synaptics\n");
771 		}
772 	}
773 
774 	return ret;
775 }
776 
dm_helpers_dp_write_dsc_enable(struct dc_context * ctx,const struct dc_stream_state * stream,bool enable)777 bool dm_helpers_dp_write_dsc_enable(
778 		struct dc_context *ctx,
779 		const struct dc_stream_state *stream,
780 		bool enable)
781 {
782 	static const uint8_t DSC_DISABLE;
783 	static const uint8_t DSC_DECODING = 0x01;
784 	static const uint8_t DSC_PASSTHROUGH = 0x02;
785 
786 	struct amdgpu_dm_connector *aconnector;
787 	struct drm_dp_mst_port *port;
788 	uint8_t enable_dsc = enable ? DSC_DECODING : DSC_DISABLE;
789 	uint8_t enable_passthrough = enable ? DSC_PASSTHROUGH : DSC_DISABLE;
790 	uint8_t ret = 0;
791 
792 	if (!stream)
793 		return false;
794 
795 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
796 		aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
797 
798 		if (!aconnector->dsc_aux)
799 			return false;
800 
801 		// apply w/a to synaptics
802 		if (needs_dsc_aux_workaround(aconnector->dc_link) &&
803 		    (aconnector->mst_downstream_port_present.byte & 0x7) != 0x3)
804 			return write_dsc_enable_synaptics_non_virtual_dpcd_mst(
805 				aconnector->dsc_aux, stream, enable_dsc);
806 
807 		port = aconnector->mst_output_port;
808 
809 		if (enable) {
810 			if (port->passthrough_aux) {
811 				ret = drm_dp_dpcd_write(port->passthrough_aux,
812 							DP_DSC_ENABLE,
813 							&enable_passthrough, 1);
814 				DC_LOG_DC("Sent DSC pass-through enable to virtual dpcd port, ret = %u\n",
815 					  ret);
816 			}
817 
818 			ret = drm_dp_dpcd_write(aconnector->dsc_aux,
819 						DP_DSC_ENABLE, &enable_dsc, 1);
820 			DC_LOG_DC("Sent DSC decoding enable to %s port, ret = %u\n",
821 				  (port->passthrough_aux) ? "remote RX" :
822 				  "virtual dpcd",
823 				  ret);
824 		} else {
825 			ret = drm_dp_dpcd_write(aconnector->dsc_aux,
826 						DP_DSC_ENABLE, &enable_dsc, 1);
827 			DC_LOG_DC("Sent DSC decoding disable to %s port, ret = %u\n",
828 				  (port->passthrough_aux) ? "remote RX" :
829 				  "virtual dpcd",
830 				  ret);
831 
832 			if (port->passthrough_aux) {
833 				ret = drm_dp_dpcd_write(port->passthrough_aux,
834 							DP_DSC_ENABLE,
835 							&enable_passthrough, 1);
836 				DC_LOG_DC("Sent DSC pass-through disable to virtual dpcd port, ret = %u\n",
837 					  ret);
838 			}
839 		}
840 	}
841 
842 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT || stream->signal == SIGNAL_TYPE_EDP) {
843 		if (stream->sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
844 			ret = dm_helpers_dp_write_dpcd(ctx, stream->link, DP_DSC_ENABLE, &enable_dsc, 1);
845 			DC_LOG_DC("Send DSC %s to SST RX\n", enable_dsc ? "enable" : "disable");
846 		} else if (stream->sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
847 			ret = dm_helpers_dp_write_dpcd(ctx, stream->link, DP_DSC_ENABLE, &enable_dsc, 1);
848 			DC_LOG_DC("Send DSC %s to DP-HDMI PCON\n", enable_dsc ? "enable" : "disable");
849 		}
850 	}
851 
852 	return ret;
853 }
854 
dm_helpers_is_dp_sink_present(struct dc_link * link)855 bool dm_helpers_is_dp_sink_present(struct dc_link *link)
856 {
857 	bool dp_sink_present;
858 	struct amdgpu_dm_connector *aconnector = link->priv;
859 
860 	if (!aconnector) {
861 		BUG_ON("Failed to find connector for link!");
862 		return true;
863 	}
864 
865 	mutex_lock(&aconnector->dm_dp_aux.aux.hw_mutex);
866 	dp_sink_present = dc_link_is_dp_sink_present(link);
867 	mutex_unlock(&aconnector->dm_dp_aux.aux.hw_mutex);
868 	return dp_sink_present;
869 }
870 
dm_helpers_read_local_edid(struct dc_context * ctx,struct dc_link * link,struct dc_sink * sink)871 enum dc_edid_status dm_helpers_read_local_edid(
872 		struct dc_context *ctx,
873 		struct dc_link *link,
874 		struct dc_sink *sink)
875 {
876 	struct amdgpu_dm_connector *aconnector = link->priv;
877 	struct drm_connector *connector = &aconnector->base;
878 	struct i2c_adapter *ddc;
879 	int retry = 3;
880 	enum dc_edid_status edid_status;
881 	struct edid *edid;
882 
883 	if (link->aux_mode)
884 		ddc = &aconnector->dm_dp_aux.aux.ddc;
885 	else
886 		ddc = &aconnector->i2c->base;
887 
888 	/* some dongles read edid incorrectly the first time,
889 	 * do check sum and retry to make sure read correct edid.
890 	 */
891 	do {
892 
893 		edid = drm_get_edid(&aconnector->base, ddc);
894 
895 		/* DP Compliance Test 4.2.2.6 */
896 		if (link->aux_mode && connector->edid_corrupt)
897 			drm_dp_send_real_edid_checksum(&aconnector->dm_dp_aux.aux, connector->real_edid_checksum);
898 
899 		if (!edid && connector->edid_corrupt) {
900 			connector->edid_corrupt = false;
901 			return EDID_BAD_CHECKSUM;
902 		}
903 
904 		if (!edid)
905 			return EDID_NO_RESPONSE;
906 
907 		sink->dc_edid.length = EDID_LENGTH * (edid->extensions + 1);
908 		memmove(sink->dc_edid.raw_edid, (uint8_t *)edid, sink->dc_edid.length);
909 
910 		/* We don't need the original edid anymore */
911 		kfree(edid);
912 
913 		edid_status = dm_helpers_parse_edid_caps(
914 						link,
915 						&sink->dc_edid,
916 						&sink->edid_caps);
917 
918 	} while (edid_status == EDID_BAD_CHECKSUM && --retry > 0);
919 
920 	if (edid_status != EDID_OK)
921 		DRM_ERROR("EDID err: %d, on connector: %s",
922 				edid_status,
923 				aconnector->base.name);
924 	if (link->aux_mode) {
925 		union test_request test_request = {0};
926 		union test_response test_response = {0};
927 
928 		dm_helpers_dp_read_dpcd(ctx,
929 					link,
930 					DP_TEST_REQUEST,
931 					&test_request.raw,
932 					sizeof(union test_request));
933 
934 		if (!test_request.bits.EDID_READ)
935 			return edid_status;
936 
937 		test_response.bits.EDID_CHECKSUM_WRITE = 1;
938 
939 		dm_helpers_dp_write_dpcd(ctx,
940 					link,
941 					DP_TEST_EDID_CHECKSUM,
942 					&sink->dc_edid.raw_edid[sink->dc_edid.length-1],
943 					1);
944 
945 		dm_helpers_dp_write_dpcd(ctx,
946 					link,
947 					DP_TEST_RESPONSE,
948 					&test_response.raw,
949 					sizeof(test_response));
950 
951 	}
952 
953 	return edid_status;
954 }
dm_helper_dmub_aux_transfer_sync(struct dc_context * ctx,const struct dc_link * link,struct aux_payload * payload,enum aux_return_code_type * operation_result)955 int dm_helper_dmub_aux_transfer_sync(
956 		struct dc_context *ctx,
957 		const struct dc_link *link,
958 		struct aux_payload *payload,
959 		enum aux_return_code_type *operation_result)
960 {
961 	if (!link->hpd_status) {
962 		*operation_result = AUX_RET_ERROR_HPD_DISCON;
963 		return -1;
964 	}
965 
966 	return amdgpu_dm_process_dmub_aux_transfer_sync(ctx, link->link_index, payload,
967 			operation_result);
968 }
969 
dm_helpers_dmub_set_config_sync(struct dc_context * ctx,const struct dc_link * link,struct set_config_cmd_payload * payload,enum set_config_status * operation_result)970 int dm_helpers_dmub_set_config_sync(struct dc_context *ctx,
971 		const struct dc_link *link,
972 		struct set_config_cmd_payload *payload,
973 		enum set_config_status *operation_result)
974 {
975 	return amdgpu_dm_process_dmub_set_config_sync(ctx, link->link_index, payload,
976 			operation_result);
977 }
978 
dm_set_dcn_clocks(struct dc_context * ctx,struct dc_clocks * clks)979 void dm_set_dcn_clocks(struct dc_context *ctx, struct dc_clocks *clks)
980 {
981 	/* TODO: something */
982 }
983 
dm_helpers_smu_timeout(struct dc_context * ctx,unsigned int msg_id,unsigned int param,unsigned int timeout_us)984 void dm_helpers_smu_timeout(struct dc_context *ctx, unsigned int msg_id, unsigned int param, unsigned int timeout_us)
985 {
986 	// TODO:
987 	//amdgpu_device_gpu_recover(dc_context->driver-context, NULL);
988 }
989 
dm_helpers_init_panel_settings(struct dc_context * ctx,struct dc_panel_config * panel_config,struct dc_sink * sink)990 void dm_helpers_init_panel_settings(
991 	struct dc_context *ctx,
992 	struct dc_panel_config *panel_config,
993 	struct dc_sink *sink)
994 {
995 	// Extra Panel Power Sequence
996 	panel_config->pps.extra_t3_ms = sink->edid_caps.panel_patch.extra_t3_ms;
997 	panel_config->pps.extra_t7_ms = sink->edid_caps.panel_patch.extra_t7_ms;
998 	panel_config->pps.extra_delay_backlight_off = sink->edid_caps.panel_patch.extra_delay_backlight_off;
999 	panel_config->pps.extra_post_t7_ms = 0;
1000 	panel_config->pps.extra_pre_t11_ms = 0;
1001 	panel_config->pps.extra_t12_ms = sink->edid_caps.panel_patch.extra_t12_ms;
1002 	panel_config->pps.extra_post_OUI_ms = 0;
1003 	// Feature DSC
1004 	panel_config->dsc.disable_dsc_edp = false;
1005 	panel_config->dsc.force_dsc_edp_policy = 0;
1006 }
1007 
dm_helpers_override_panel_settings(struct dc_context * ctx,struct dc_panel_config * panel_config)1008 void dm_helpers_override_panel_settings(
1009 	struct dc_context *ctx,
1010 	struct dc_panel_config *panel_config)
1011 {
1012 	// Feature DSC
1013 	if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
1014 		panel_config->dsc.disable_dsc_edp = true;
1015 }
1016 
dm_helpers_allocate_gpu_mem(struct dc_context * ctx,enum dc_gpu_mem_alloc_type type,size_t size,long long * addr)1017 void *dm_helpers_allocate_gpu_mem(
1018 		struct dc_context *ctx,
1019 		enum dc_gpu_mem_alloc_type type,
1020 		size_t size,
1021 		long long *addr)
1022 {
1023 	struct amdgpu_device *adev = ctx->driver_context;
1024 	struct dal_allocation *da;
1025 	u32 domain = (type == DC_MEM_ALLOC_TYPE_GART) ?
1026 		AMDGPU_GEM_DOMAIN_GTT : AMDGPU_GEM_DOMAIN_VRAM;
1027 	int ret;
1028 
1029 	da = kzalloc(sizeof(struct dal_allocation), GFP_KERNEL);
1030 	if (!da)
1031 		return NULL;
1032 
1033 	ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
1034 				      domain, &da->bo,
1035 				      &da->gpu_addr, &da->cpu_ptr);
1036 
1037 	*addr = da->gpu_addr;
1038 
1039 	if (ret) {
1040 		kfree(da);
1041 		return NULL;
1042 	}
1043 
1044 	/* add da to list in dm */
1045 	list_add(&da->list, &adev->dm.da_list);
1046 
1047 	return da->cpu_ptr;
1048 }
1049 
dm_helpers_free_gpu_mem(struct dc_context * ctx,enum dc_gpu_mem_alloc_type type,void * pvMem)1050 void dm_helpers_free_gpu_mem(
1051 		struct dc_context *ctx,
1052 		enum dc_gpu_mem_alloc_type type,
1053 		void *pvMem)
1054 {
1055 	struct amdgpu_device *adev = ctx->driver_context;
1056 	struct dal_allocation *da;
1057 
1058 	/* walk the da list in DM */
1059 	list_for_each_entry(da, &adev->dm.da_list, list) {
1060 		if (pvMem == da->cpu_ptr) {
1061 			amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
1062 			list_del(&da->list);
1063 			kfree(da);
1064 			break;
1065 		}
1066 	}
1067 }
1068 
dm_helpers_dmub_outbox_interrupt_control(struct dc_context * ctx,bool enable)1069 bool dm_helpers_dmub_outbox_interrupt_control(struct dc_context *ctx, bool enable)
1070 {
1071 	enum dc_irq_source irq_source;
1072 	bool ret;
1073 
1074 	irq_source = DC_IRQ_SOURCE_DMCUB_OUTBOX;
1075 
1076 	ret = dc_interrupt_set(ctx->dc, irq_source, enable);
1077 
1078 	DRM_DEBUG_DRIVER("Dmub trace irq %sabling: r=%d\n",
1079 			 enable ? "en" : "dis", ret);
1080 	return ret;
1081 }
1082 
dm_helpers_mst_enable_stream_features(const struct dc_stream_state * stream)1083 void dm_helpers_mst_enable_stream_features(const struct dc_stream_state *stream)
1084 {
1085 	/* TODO: virtual DPCD */
1086 	struct dc_link *link = stream->link;
1087 	union down_spread_ctrl old_downspread;
1088 	union down_spread_ctrl new_downspread;
1089 
1090 	if (link->aux_access_disabled)
1091 		return;
1092 
1093 	if (!dm_helpers_dp_read_dpcd(link->ctx, link, DP_DOWNSPREAD_CTRL,
1094 				     &old_downspread.raw,
1095 				     sizeof(old_downspread)))
1096 		return;
1097 
1098 	new_downspread.raw = old_downspread.raw;
1099 	new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
1100 		(stream->ignore_msa_timing_param) ? 1 : 0;
1101 
1102 	if (new_downspread.raw != old_downspread.raw)
1103 		dm_helpers_dp_write_dpcd(link->ctx, link, DP_DOWNSPREAD_CTRL,
1104 					 &new_downspread.raw,
1105 					 sizeof(new_downspread));
1106 }
1107 
dm_helpers_dp_handle_test_pattern_request(struct dc_context * ctx,const struct dc_link * link,union link_test_pattern dpcd_test_pattern,union test_misc dpcd_test_params)1108 bool dm_helpers_dp_handle_test_pattern_request(
1109 		struct dc_context *ctx,
1110 		const struct dc_link *link,
1111 		union link_test_pattern dpcd_test_pattern,
1112 		union test_misc dpcd_test_params)
1113 {
1114 	enum dp_test_pattern test_pattern;
1115 	enum dp_test_pattern_color_space test_pattern_color_space =
1116 			DP_TEST_PATTERN_COLOR_SPACE_UNDEFINED;
1117 	enum dc_color_depth requestColorDepth = COLOR_DEPTH_UNDEFINED;
1118 	enum dc_pixel_encoding requestPixelEncoding = PIXEL_ENCODING_UNDEFINED;
1119 	struct pipe_ctx *pipes = link->dc->current_state->res_ctx.pipe_ctx;
1120 	struct pipe_ctx *pipe_ctx = NULL;
1121 	struct amdgpu_dm_connector *aconnector = link->priv;
1122 	int i;
1123 
1124 	for (i = 0; i < MAX_PIPES; i++) {
1125 		if (pipes[i].stream == NULL)
1126 			continue;
1127 
1128 		if (pipes[i].stream->link == link && !pipes[i].top_pipe &&
1129 			!pipes[i].prev_odm_pipe) {
1130 			pipe_ctx = &pipes[i];
1131 			break;
1132 		}
1133 	}
1134 
1135 	if (pipe_ctx == NULL)
1136 		return false;
1137 
1138 	switch (dpcd_test_pattern.bits.PATTERN) {
1139 	case LINK_TEST_PATTERN_COLOR_RAMP:
1140 		test_pattern = DP_TEST_PATTERN_COLOR_RAMP;
1141 	break;
1142 	case LINK_TEST_PATTERN_VERTICAL_BARS:
1143 		test_pattern = DP_TEST_PATTERN_VERTICAL_BARS;
1144 	break; /* black and white */
1145 	case LINK_TEST_PATTERN_COLOR_SQUARES:
1146 		test_pattern = (dpcd_test_params.bits.DYN_RANGE ==
1147 				TEST_DYN_RANGE_VESA ?
1148 				DP_TEST_PATTERN_COLOR_SQUARES :
1149 				DP_TEST_PATTERN_COLOR_SQUARES_CEA);
1150 	break;
1151 	default:
1152 		test_pattern = DP_TEST_PATTERN_VIDEO_MODE;
1153 	break;
1154 	}
1155 
1156 	if (dpcd_test_params.bits.CLR_FORMAT == 0)
1157 		test_pattern_color_space = DP_TEST_PATTERN_COLOR_SPACE_RGB;
1158 	else
1159 		test_pattern_color_space = dpcd_test_params.bits.YCBCR_COEFS ?
1160 				DP_TEST_PATTERN_COLOR_SPACE_YCBCR709 :
1161 				DP_TEST_PATTERN_COLOR_SPACE_YCBCR601;
1162 
1163 	switch (dpcd_test_params.bits.BPC) {
1164 	case 0: // 6 bits
1165 		requestColorDepth = COLOR_DEPTH_666;
1166 		break;
1167 	case 1: // 8 bits
1168 		requestColorDepth = COLOR_DEPTH_888;
1169 		break;
1170 	case 2: // 10 bits
1171 		requestColorDepth = COLOR_DEPTH_101010;
1172 		break;
1173 	case 3: // 12 bits
1174 		requestColorDepth = COLOR_DEPTH_121212;
1175 		break;
1176 	default:
1177 		break;
1178 	}
1179 
1180 	switch (dpcd_test_params.bits.CLR_FORMAT) {
1181 	case 0:
1182 		requestPixelEncoding = PIXEL_ENCODING_RGB;
1183 		break;
1184 	case 1:
1185 		requestPixelEncoding = PIXEL_ENCODING_YCBCR422;
1186 		break;
1187 	case 2:
1188 		requestPixelEncoding = PIXEL_ENCODING_YCBCR444;
1189 		break;
1190 	default:
1191 		requestPixelEncoding = PIXEL_ENCODING_RGB;
1192 		break;
1193 	}
1194 
1195 	if ((requestColorDepth != COLOR_DEPTH_UNDEFINED
1196 		&& pipe_ctx->stream->timing.display_color_depth != requestColorDepth)
1197 		|| (requestPixelEncoding != PIXEL_ENCODING_UNDEFINED
1198 		&& pipe_ctx->stream->timing.pixel_encoding != requestPixelEncoding)) {
1199 		DC_LOG_DEBUG("%s: original bpc %d pix encoding %d, changing to %d  %d\n",
1200 				__func__,
1201 				pipe_ctx->stream->timing.display_color_depth,
1202 				pipe_ctx->stream->timing.pixel_encoding,
1203 				requestColorDepth,
1204 				requestPixelEncoding);
1205 		pipe_ctx->stream->timing.display_color_depth = requestColorDepth;
1206 		pipe_ctx->stream->timing.pixel_encoding = requestPixelEncoding;
1207 
1208 		dc_link_update_dsc_config(pipe_ctx);
1209 
1210 		aconnector->timing_changed = true;
1211 		/* store current timing */
1212 		if (aconnector->timing_requested)
1213 			*aconnector->timing_requested = pipe_ctx->stream->timing;
1214 		else
1215 			DC_LOG_ERROR("%s: timing storage failed\n", __func__);
1216 
1217 	}
1218 
1219 	dc_link_dp_set_test_pattern(
1220 		(struct dc_link *) link,
1221 		test_pattern,
1222 		test_pattern_color_space,
1223 		NULL,
1224 		NULL,
1225 		0);
1226 
1227 	return false;
1228 }
1229 
dm_set_phyd32clk(struct dc_context * ctx,int freq_khz)1230 void dm_set_phyd32clk(struct dc_context *ctx, int freq_khz)
1231 {
1232        // TODO
1233 }
1234 
dm_helpers_enable_periodic_detection(struct dc_context * ctx,bool enable)1235 void dm_helpers_enable_periodic_detection(struct dc_context *ctx, bool enable)
1236 {
1237 	/* TODO: add periodic detection implementation */
1238 }
1239 
dm_helpers_dp_mst_update_branch_bandwidth(struct dc_context * ctx,struct dc_link * link)1240 void dm_helpers_dp_mst_update_branch_bandwidth(
1241 		struct dc_context *ctx,
1242 		struct dc_link *link)
1243 {
1244 	// TODO
1245 }
1246 
dm_is_freesync_pcon_whitelist(const uint32_t branch_dev_id)1247 static bool dm_is_freesync_pcon_whitelist(const uint32_t branch_dev_id)
1248 {
1249 	bool ret_val = false;
1250 
1251 	switch (branch_dev_id) {
1252 	case DP_BRANCH_DEVICE_ID_0060AD:
1253 	case DP_BRANCH_DEVICE_ID_00E04C:
1254 	case DP_BRANCH_DEVICE_ID_90CC24:
1255 		ret_val = true;
1256 		break;
1257 	default:
1258 		break;
1259 	}
1260 
1261 	return ret_val;
1262 }
1263 
dm_get_adaptive_sync_support_type(struct dc_link * link)1264 enum adaptive_sync_type dm_get_adaptive_sync_support_type(struct dc_link *link)
1265 {
1266 	struct dpcd_caps *dpcd_caps = &link->dpcd_caps;
1267 	enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
1268 
1269 	switch (dpcd_caps->dongle_type) {
1270 	case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
1271 		if (dpcd_caps->adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT == true &&
1272 			dpcd_caps->allow_invalid_MSA_timing_param == true &&
1273 			dm_is_freesync_pcon_whitelist(dpcd_caps->branch_dev_id))
1274 			as_type = FREESYNC_TYPE_PCON_IN_WHITELIST;
1275 		break;
1276 	default:
1277 		break;
1278 	}
1279 
1280 	return as_type;
1281 }
1282