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