xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_helpers.c (revision fe7945d092a1d3c340febc2ab176cee50d0f6c80)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2015 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: AMD
24  *
25  */
26 
27 #include <acpi/video.h>
28 
29 #include <linux/string.h>
30 #include <linux/acpi.h>
31 #include <linux/i2c.h>
32 
33 #include <drm/drm_atomic.h>
34 #include <drm/drm_probe_helper.h>
35 #include <drm/amdgpu_drm.h>
36 #include <drm/drm_edid.h>
37 #include <drm/drm_fixed.h>
38 
39 #include "dm_services.h"
40 #include "amdgpu.h"
41 #include "dc.h"
42 #include "amdgpu_dm.h"
43 #include "amdgpu_dm_irq.h"
44 #include "amdgpu_dm_mst_types.h"
45 #include "dpcd_defs.h"
46 #include "dc/inc/core_types.h"
47 
48 #include "dm_helpers.h"
49 #include "ddc_service_types.h"
50 #include "clk_mgr.h"
51 
52 #define MCCS_DEST_ADDR (0x6E >> 1)
53 #define MCCS_SRC_ADDR	0x51
54 #define MCCS_LENGTH_OFFSET 0x80
55 #define MCCS_MAX_DATA_SIZE 0x20
56 
57 enum mccs_op_code {
58 	MCCS_OP_CODE_VCP_REQUEST = 0x01,
59 	MCCS_OP_CODE_VCP_REPLY = 0x02,
60 	MCCS_OP_CODE_VCP_SET = 0x03,
61 	MCCS_OP_CODE_VCP_RESET = 0x09,
62 	MCCS_OP_CODE_CAP_REQUEST = 0xF3,
63 	MCCS_OP_CODE_CAP_REPLY = 0xE3
64 };
65 
66 enum mccs_op_buff_size {
67 	MCCS_OP_BUFF_SIZE__WR_VCP_REQUEST = 5,
68 	MCCS_OP_BUFF_SIZE_RD_VCP_REQUEST = 11,
69 	MCCS_OP_BUFF_SIZE_WR_VCP_SET = 7,
70 };
71 
72 enum vcp_reply_mask {
73 	FREESYNC_SUPPORTED = 0x1
74 };
75 
76 union vcp_reply {
77 	struct {
78 		unsigned char src_addr;
79 		unsigned char length;			/* Length is offset by MccsLengthOffs = 0x80 */
80 		unsigned char reply_op_code;	/* Should return MCCS_OP_CODE_VCP_REPLY = 0x02 */
81 		unsigned char result_code;		/* 00h No Error, 01h Unsupported VCP Code */
82 		unsigned char request_code;		/* Should return mccs vcp code sent in the vcp request */
83 		unsigned char type_code;		/* VCP type code: 00h Set parameter, 01h Momentary */
84 		unsigned char max_value[2];		/* 2 bytes returning max value current value */
85 		unsigned char present_value[2];	/* NOTE: Byte0 is MSB, Byte1 is LSB */
86 		unsigned char check_sum;
87 	} bytes;
88 	unsigned char raw[11];
89 };
90 
91 static u32 edid_extract_panel_id(struct edid *edid)
92 {
93 	return (u32)edid->mfg_id[0] << 24   |
94 	       (u32)edid->mfg_id[1] << 16   |
95 	       (u32)EDID_PRODUCT_ID(edid);
96 }
97 
98 static void apply_edid_quirks(struct dc_link *link, struct edid *edid,
99 			      struct dc_edid_caps *edid_caps)
100 {
101 	struct amdgpu_dm_connector *aconnector = link->priv;
102 	struct drm_device *dev = aconnector->base.dev;
103 	uint32_t panel_id = edid_extract_panel_id(edid);
104 
105 	switch (panel_id) {
106 	/* Workaround for monitors that need a delay after detecting the link */
107 	case drm_edid_encode_panel_id('G', 'B', 'T', 0x3215):
108 		drm_dbg_driver(dev, "Add 10s delay for link detection for panel id %X\n", panel_id);
109 		edid_caps->panel_patch.wait_after_dpcd_poweroff_ms = 10000;
110 		break;
111 	/* Workaround for some monitors which does not work well with FAMS */
112 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x0E5E):
113 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x7053):
114 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x71AC):
115 		drm_dbg_driver(dev, "Disabling FAMS on monitor with panel id %X\n", panel_id);
116 		edid_caps->panel_patch.disable_fams = true;
117 		break;
118 	/* Workaround for some monitors that do not clear DPCD 0x317 if FreeSync is unsupported */
119 	case drm_edid_encode_panel_id('A', 'U', 'O', 0xA7AB):
120 	case drm_edid_encode_panel_id('A', 'U', 'O', 0xE69B):
121 	case drm_edid_encode_panel_id('B', 'O', 'E', 0x092A):
122 	case drm_edid_encode_panel_id('L', 'G', 'D', 0x06D1):
123 	case drm_edid_encode_panel_id('M', 'S', 'F', 0x1003):
124 		drm_dbg_driver(dev, "Clearing DPCD 0x317 on monitor with panel id %X\n", panel_id);
125 		edid_caps->panel_patch.remove_sink_ext_caps = true;
126 		break;
127 	case drm_edid_encode_panel_id('S', 'D', 'C', 0x4154):
128 	case drm_edid_encode_panel_id('S', 'D', 'C', 0x4171):
129 		drm_dbg_driver(dev, "Disabling VSC on monitor with panel id %X\n", panel_id);
130 		edid_caps->panel_patch.disable_colorimetry = true;
131 		break;
132 	/* Workaround for monitors that get corrupted by the PHY SSC reduction */
133 	case drm_edid_encode_panel_id('D', 'E', 'L', 0x4147):
134 		drm_dbg_driver(dev, "Skip PHY SSC reduction on panel id %X\n", panel_id);
135 		link->wa_flags.skip_phy_ssc_reduction = true;
136 		break;
137 	default:
138 		return;
139 	}
140 }
141 
142 /**
143  * dm_helpers_parse_edid_caps() - Parse edid caps
144  *
145  * @link: current detected link
146  * @edid:	[in] pointer to edid
147  * @edid_caps:	[in] pointer to edid caps
148  *
149  * Return: void
150  */
151 enum dc_edid_status dm_helpers_parse_edid_caps(
152 		struct dc_link *link,
153 		const struct dc_edid *edid,
154 		struct dc_edid_caps *edid_caps)
155 {
156 	struct amdgpu_dm_connector *aconnector = link->priv;
157 	struct drm_connector *connector = &aconnector->base;
158 	struct edid *edid_buf = edid ? (struct edid *) edid->raw_edid : NULL;
159 	struct cea_sad *sads;
160 	int sad_count = -1;
161 	int sadb_count = -1;
162 	int i = 0;
163 	uint8_t *sadb = NULL;
164 
165 	enum dc_edid_status result = EDID_OK;
166 
167 	if (!edid_caps || !edid)
168 		return EDID_BAD_INPUT;
169 
170 	if (!drm_edid_is_valid(edid_buf))
171 		result = EDID_BAD_CHECKSUM;
172 
173 	edid_caps->manufacturer_id = (uint16_t) edid_buf->mfg_id[0] |
174 					((uint16_t) edid_buf->mfg_id[1])<<8;
175 	edid_caps->product_id = (uint16_t) edid_buf->prod_code[0] |
176 					((uint16_t) edid_buf->prod_code[1])<<8;
177 	edid_caps->serial_number = edid_buf->serial;
178 	edid_caps->manufacture_week = edid_buf->mfg_week;
179 	edid_caps->manufacture_year = edid_buf->mfg_year;
180 	edid_caps->analog = !(edid_buf->input & DRM_EDID_INPUT_DIGITAL);
181 
182 	drm_edid_get_monitor_name(edid_buf,
183 				  edid_caps->display_name,
184 				  AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
185 
186 	edid_caps->edid_hdmi = connector->display_info.is_hdmi;
187 
188 	if (edid_caps->edid_hdmi) {
189 		populate_hdmi_info_from_connector(link->dc->config.enable_frl, &connector->display_info.hdmi, edid_caps);
190 		drm_dbg_driver(connector->dev, "%s: HDMI_FRL [%s] max_frl_rate %d\n", __func__, connector->name, edid_caps->max_frl_rate);
191 		if (edid_caps->frl_dsc_support)
192 			drm_dbg_driver(connector->dev, "%s: HDMI_FRL_DSC [%s] frl_dsc_10bpc %d, frl_dsc_12bpc %d, frl_dsc_all_bpp %d, frl_dsc_native_420 %d, frl_dsc_max_slices %d, frl_dsc_max_frl_rate %d, frl_dsc_total_chunk_kbytes %d\n",
193 					__func__, connector->name, edid_caps->frl_dsc_10bpc, edid_caps->frl_dsc_12bpc, \
194 					edid_caps->frl_dsc_all_bpp, edid_caps->frl_dsc_native_420, edid_caps->frl_dsc_max_slices, \
195 					edid_caps->frl_dsc_max_frl_rate, edid_caps->frl_dsc_total_chunk_kbytes);
196 	}
197 
198 	apply_edid_quirks(link, edid_buf, edid_caps);
199 
200 	sad_count = drm_edid_to_sad((struct edid *) edid->raw_edid, &sads);
201 	if (sad_count <= 0)
202 		return result;
203 
204 	edid_caps->audio_mode_count = min(sad_count, DC_MAX_AUDIO_DESC_COUNT);
205 	for (i = 0; i < edid_caps->audio_mode_count; ++i) {
206 		struct cea_sad *sad = &sads[i];
207 
208 		edid_caps->audio_modes[i].format_code = sad->format;
209 		edid_caps->audio_modes[i].channel_count = sad->channels + 1;
210 		edid_caps->audio_modes[i].sample_rate = sad->freq;
211 		edid_caps->audio_modes[i].sample_size = sad->byte2;
212 	}
213 
214 	sadb_count = drm_edid_to_speaker_allocation((struct edid *) edid->raw_edid, &sadb);
215 
216 	if (sadb_count < 0) {
217 		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sadb_count);
218 		sadb_count = 0;
219 	}
220 
221 	if (sadb_count)
222 		edid_caps->speaker_flags = sadb[0];
223 	else
224 		edid_caps->speaker_flags = DEFAULT_SPEAKER_LOCATION;
225 
226 	kfree(sads);
227 	kfree(sadb);
228 
229 	return result;
230 }
231 
232 static void
233 fill_dc_mst_payload_table_from_drm(struct dc_link *link,
234 				   bool enable,
235 				   struct drm_dp_mst_atomic_payload *target_payload,
236 				   struct dc_dp_mst_stream_allocation_table *table)
237 {
238 	struct dc_dp_mst_stream_allocation_table new_table = { 0 };
239 	struct dc_dp_mst_stream_allocation *sa;
240 	struct link_mst_stream_allocation_table copy_of_link_table =
241 										link->mst_stream_alloc_table;
242 
243 	int i;
244 	int current_hw_table_stream_cnt = copy_of_link_table.stream_count;
245 	struct link_mst_stream_allocation *dc_alloc;
246 
247 	/* TODO: refactor to set link->mst_stream_alloc_table directly if possible.*/
248 	if (enable) {
249 		dc_alloc =
250 		&copy_of_link_table.stream_allocations[current_hw_table_stream_cnt];
251 		dc_alloc->vcp_id = target_payload->vcpi;
252 		dc_alloc->slot_count = target_payload->time_slots;
253 	} else {
254 		for (i = 0; i < copy_of_link_table.stream_count; i++) {
255 			dc_alloc =
256 			&copy_of_link_table.stream_allocations[i];
257 
258 			if (dc_alloc->vcp_id == target_payload->vcpi) {
259 				dc_alloc->vcp_id = 0;
260 				dc_alloc->slot_count = 0;
261 				break;
262 			}
263 		}
264 		ASSERT(i != copy_of_link_table.stream_count);
265 	}
266 
267 	/* Fill payload info*/
268 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
269 		dc_alloc =
270 			&copy_of_link_table.stream_allocations[i];
271 		if (dc_alloc->vcp_id > 0 && dc_alloc->slot_count > 0) {
272 			sa = &new_table.stream_allocations[new_table.stream_count];
273 			sa->slot_count = dc_alloc->slot_count;
274 			sa->vcp_id = dc_alloc->vcp_id;
275 			new_table.stream_count++;
276 		}
277 	}
278 
279 	/* Overwrite the old table */
280 	*table = new_table;
281 }
282 
283 void dm_helpers_dp_update_branch_info(
284 	struct dc_context *ctx,
285 	const struct dc_link *link)
286 {}
287 
288 static void dm_helpers_construct_old_payload(
289 			struct drm_dp_mst_topology_mgr *mgr,
290 			struct drm_dp_mst_topology_state *mst_state,
291 			struct drm_dp_mst_atomic_payload *new_payload,
292 			struct drm_dp_mst_atomic_payload *old_payload)
293 {
294 	struct drm_dp_mst_atomic_payload *pos;
295 	int pbn_per_slot = dfixed_trunc(mst_state->pbn_div);
296 	u8 next_payload_vc_start = mgr->next_start_slot;
297 	u8 payload_vc_start = new_payload->vc_start_slot;
298 	u8 allocated_time_slots;
299 
300 	*old_payload = *new_payload;
301 
302 	/* Set correct time_slots/PBN of old payload.
303 	 * other fields (delete & dsc_enabled) in
304 	 * struct drm_dp_mst_atomic_payload are don't care fields
305 	 * while calling drm_dp_remove_payload_part2()
306 	 */
307 	list_for_each_entry(pos, &mst_state->payloads, next) {
308 		if (pos != new_payload &&
309 		    pos->vc_start_slot > payload_vc_start &&
310 		    pos->vc_start_slot < next_payload_vc_start)
311 			next_payload_vc_start = pos->vc_start_slot;
312 	}
313 
314 	allocated_time_slots = next_payload_vc_start - payload_vc_start;
315 
316 	old_payload->time_slots = allocated_time_slots;
317 	old_payload->pbn = allocated_time_slots * pbn_per_slot;
318 }
319 
320 /*
321  * Writes payload allocation table in immediate downstream device.
322  */
323 bool dm_helpers_dp_mst_write_payload_allocation_table(
324 		struct dc_context *ctx,
325 		const struct dc_stream_state *stream,
326 		struct dc_dp_mst_stream_allocation_table *proposed_table,
327 		bool enable)
328 {
329 	struct amdgpu_dm_connector *aconnector;
330 	struct drm_dp_mst_topology_state *mst_state;
331 	struct drm_dp_mst_atomic_payload *target_payload, *new_payload, old_payload;
332 	struct drm_dp_mst_topology_mgr *mst_mgr;
333 
334 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
335 	/* Accessing the connector state is required for vcpi_slots allocation
336 	 * and directly relies on behaviour in commit check
337 	 * that blocks before commit guaranteeing that the state
338 	 * is not gonna be swapped while still in use in commit tail
339 	 */
340 
341 	if (!aconnector || !aconnector->mst_root)
342 		return false;
343 
344 	mst_mgr = &aconnector->mst_root->mst_mgr;
345 	mst_state = to_drm_dp_mst_topology_state(mst_mgr->base.state);
346 	new_payload = drm_atomic_get_mst_payload_state(mst_state, aconnector->mst_output_port);
347 
348 	if (enable) {
349 		target_payload = new_payload;
350 
351 		/* It's OK for this to fail */
352 		drm_dp_add_payload_part1(mst_mgr, mst_state, new_payload);
353 	} else {
354 		/* construct old payload by VCPI*/
355 		dm_helpers_construct_old_payload(mst_mgr, mst_state,
356 						 new_payload, &old_payload);
357 		target_payload = &old_payload;
358 
359 		drm_dp_remove_payload_part1(mst_mgr, mst_state, new_payload);
360 	}
361 
362 	/* mst_mgr->->payloads are VC payload notify MST branch using DPCD or
363 	 * AUX message. The sequence is slot 1-63 allocated sequence for each
364 	 * stream. AMD ASIC stream slot allocation should follow the same
365 	 * sequence. copy DRM MST allocation to dc
366 	 */
367 	fill_dc_mst_payload_table_from_drm(stream->link, enable, target_payload, proposed_table);
368 
369 	return true;
370 }
371 
372 /*
373  * poll pending down reply
374  */
375 void dm_helpers_dp_mst_poll_pending_down_reply(
376 	struct dc_context *ctx,
377 	const struct dc_link *link)
378 {}
379 
380 /*
381  * Clear payload allocation table before enable MST DP link.
382  */
383 void dm_helpers_dp_mst_clear_payload_allocation_table(
384 	struct dc_context *ctx,
385 	const struct dc_link *link)
386 {}
387 
388 /*
389  * Polls for ACT (allocation change trigger) handled and sends
390  * ALLOCATE_PAYLOAD message.
391  */
392 enum act_return_status dm_helpers_dp_mst_poll_for_allocation_change_trigger(
393 		struct dc_context *ctx,
394 		const struct dc_stream_state *stream)
395 {
396 	struct amdgpu_dm_connector *aconnector;
397 	struct drm_dp_mst_topology_mgr *mst_mgr;
398 	int ret;
399 
400 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
401 
402 	if (!aconnector || !aconnector->mst_root)
403 		return ACT_FAILED;
404 
405 	mst_mgr = &aconnector->mst_root->mst_mgr;
406 
407 	if (!mst_mgr->mst_state)
408 		return ACT_FAILED;
409 
410 	ret = drm_dp_check_act_status(mst_mgr);
411 
412 	if (ret)
413 		return ACT_FAILED;
414 
415 	return ACT_SUCCESS;
416 }
417 
418 void dm_helpers_dp_mst_send_payload_allocation(
419 		struct dc_context *ctx,
420 		const struct dc_stream_state *stream)
421 {
422 	struct amdgpu_dm_connector *aconnector;
423 	struct drm_dp_mst_topology_state *mst_state;
424 	struct drm_dp_mst_topology_mgr *mst_mgr;
425 	struct drm_dp_mst_atomic_payload *new_payload;
426 	enum mst_progress_status set_flag = MST_ALLOCATE_NEW_PAYLOAD;
427 	enum mst_progress_status clr_flag = MST_CLEAR_ALLOCATED_PAYLOAD;
428 	int ret = 0;
429 
430 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
431 
432 	if (!aconnector || !aconnector->mst_root)
433 		return;
434 
435 	mst_mgr = &aconnector->mst_root->mst_mgr;
436 	mst_state = to_drm_dp_mst_topology_state(mst_mgr->base.state);
437 	new_payload = drm_atomic_get_mst_payload_state(mst_state, aconnector->mst_output_port);
438 
439 	ret = drm_dp_add_payload_part2(mst_mgr, new_payload);
440 
441 	if (ret) {
442 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
443 			set_flag, false);
444 	} else {
445 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
446 			set_flag, true);
447 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
448 			clr_flag, false);
449 	}
450 }
451 
452 void dm_helpers_dp_mst_update_mst_mgr_for_deallocation(
453 		struct dc_context *ctx,
454 		const struct dc_stream_state *stream)
455 {
456 	struct amdgpu_dm_connector *aconnector;
457 	struct drm_dp_mst_topology_state *mst_state;
458 	struct drm_dp_mst_topology_mgr *mst_mgr;
459 	struct drm_dp_mst_atomic_payload *new_payload, old_payload;
460 	enum mst_progress_status set_flag = MST_CLEAR_ALLOCATED_PAYLOAD;
461 	enum mst_progress_status clr_flag = MST_ALLOCATE_NEW_PAYLOAD;
462 
463 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
464 
465 	if (!aconnector || !aconnector->mst_root)
466 		return;
467 
468 	mst_mgr = &aconnector->mst_root->mst_mgr;
469 	mst_state = to_drm_dp_mst_topology_state(mst_mgr->base.state);
470 	new_payload = drm_atomic_get_mst_payload_state(mst_state, aconnector->mst_output_port);
471 	dm_helpers_construct_old_payload(mst_mgr, mst_state,
472 					 new_payload, &old_payload);
473 
474 	drm_dp_remove_payload_part2(mst_mgr, mst_state, &old_payload, new_payload);
475 
476 	amdgpu_dm_set_mst_status(&aconnector->mst_status, set_flag, true);
477 	amdgpu_dm_set_mst_status(&aconnector->mst_status, clr_flag, false);
478  }
479 
480 void dm_dtn_log_begin(struct dc_context *ctx,
481 	struct dc_log_buffer_ctx *log_ctx)
482 {
483 	static const char msg[] = "[dtn begin]\n";
484 
485 	if (!log_ctx) {
486 		pr_info("%s", msg);
487 		return;
488 	}
489 
490 	dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
491 }
492 
493 __printf(3, 4)
494 void dm_dtn_log_append_v(struct dc_context *ctx,
495 	struct dc_log_buffer_ctx *log_ctx,
496 	const char *msg, ...)
497 {
498 	va_list args;
499 	size_t total;
500 	int n;
501 
502 	if (!log_ctx) {
503 		/* No context, redirect to dmesg. */
504 		struct va_format vaf;
505 
506 		vaf.fmt = msg;
507 		vaf.va = &args;
508 
509 		va_start(args, msg);
510 		pr_info("%pV", &vaf);
511 		va_end(args);
512 
513 		return;
514 	}
515 
516 	/* Measure the output. */
517 	va_start(args, msg);
518 	n = vsnprintf(NULL, 0, msg, args);
519 	va_end(args);
520 
521 	if (n <= 0)
522 		return;
523 
524 	/* Reallocate the string buffer as needed. */
525 	total = log_ctx->pos + n + 1;
526 
527 	if (total > log_ctx->size) {
528 		char *buf = kvcalloc(total, sizeof(char), GFP_KERNEL);
529 
530 		if (buf) {
531 			memcpy(buf, log_ctx->buf, log_ctx->pos);
532 			kfree(log_ctx->buf);
533 
534 			log_ctx->buf = buf;
535 			log_ctx->size = total;
536 		}
537 	}
538 
539 	if (!log_ctx->buf)
540 		return;
541 
542 	/* Write the formatted string to the log buffer. */
543 	va_start(args, msg);
544 	n = vscnprintf(
545 		log_ctx->buf + log_ctx->pos,
546 		log_ctx->size - log_ctx->pos,
547 		msg,
548 		args);
549 	va_end(args);
550 
551 	if (n > 0)
552 		log_ctx->pos += n;
553 }
554 
555 void dm_dtn_log_end(struct dc_context *ctx,
556 	struct dc_log_buffer_ctx *log_ctx)
557 {
558 	static const char msg[] = "[dtn end]\n";
559 
560 	if (!log_ctx) {
561 		pr_info("%s", msg);
562 		return;
563 	}
564 
565 	dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
566 }
567 
568 bool dm_helpers_dp_mst_start_top_mgr(
569 		struct dc_context *ctx,
570 		const struct dc_link *link,
571 		bool boot)
572 {
573 	struct amdgpu_dm_connector *aconnector = link->priv;
574 	int ret;
575 
576 	if (!aconnector) {
577 		DRM_ERROR("Failed to find connector for link!");
578 		return false;
579 	}
580 
581 	if (boot) {
582 		DRM_INFO("DM_MST: Differing MST start on aconnector: %p [id: %d]\n",
583 					aconnector, aconnector->base.base.id);
584 		return true;
585 	}
586 
587 	DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
588 			aconnector, aconnector->base.base.id);
589 
590 	ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
591 	if (ret < 0) {
592 		DRM_ERROR("DM_MST: Failed to set the device into MST mode!");
593 		return false;
594 	}
595 
596 	DRM_INFO("DM_MST: DP%x, %d-lane link detected\n", aconnector->mst_mgr.dpcd[0],
597 		aconnector->mst_mgr.dpcd[2] & DP_MAX_LANE_COUNT_MASK);
598 
599 	return true;
600 }
601 
602 bool dm_helpers_dp_mst_stop_top_mgr(
603 		struct dc_context *ctx,
604 		struct dc_link *link)
605 {
606 	struct amdgpu_dm_connector *aconnector = link->priv;
607 
608 	if (!aconnector) {
609 		DRM_ERROR("Failed to find connector for link!");
610 		return false;
611 	}
612 
613 	DRM_INFO("DM_MST: stopping TM on aconnector: %p [id: %d]\n",
614 			aconnector, aconnector->base.base.id);
615 
616 	if (aconnector->mst_mgr.mst_state == true) {
617 		drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, false);
618 		link->cur_link_settings.lane_count = 0;
619 	}
620 
621 	return false;
622 }
623 
624 bool dm_helpers_dp_read_dpcd(
625 		struct dc_context *ctx,
626 		const struct dc_link *link,
627 		uint32_t address,
628 		uint8_t *data,
629 		uint32_t size)
630 {
631 
632 	struct amdgpu_dm_connector *aconnector = link->priv;
633 
634 	if (!aconnector)
635 		return false;
636 
637 	return drm_dp_dpcd_read(&aconnector->dm_dp_aux.aux, address, data,
638 				size) == size;
639 }
640 
641 bool dm_helpers_dp_write_dpcd(
642 		struct dc_context *ctx,
643 		const struct dc_link *link,
644 		uint32_t address,
645 		const uint8_t *data,
646 		uint32_t size)
647 {
648 	struct amdgpu_dm_connector *aconnector = link->priv;
649 
650 	if (!aconnector)
651 		return false;
652 
653 	return drm_dp_dpcd_write(&aconnector->dm_dp_aux.aux,
654 			address, (uint8_t *)data, size) > 0;
655 }
656 
657 bool dm_helpers_submit_i2c(
658 		struct dc_context *ctx,
659 		const struct dc_link *link,
660 		struct i2c_command *cmd)
661 {
662 	struct amdgpu_dm_connector *aconnector = link->priv;
663 	struct i2c_msg *msgs;
664 	int i = 0;
665 	int num = cmd->number_of_payloads;
666 	bool result;
667 
668 	if (!aconnector) {
669 		DRM_ERROR("Failed to find connector for link!");
670 		return false;
671 	}
672 
673 	msgs = kzalloc_objs(struct i2c_msg, num);
674 
675 	if (!msgs)
676 		return false;
677 
678 	for (i = 0; i < num; i++) {
679 		msgs[i].flags = cmd->payloads[i].write ? 0 : I2C_M_RD;
680 		msgs[i].addr = cmd->payloads[i].address;
681 		msgs[i].len = cmd->payloads[i].length;
682 		msgs[i].buf = cmd->payloads[i].data;
683 	}
684 
685 	result = i2c_transfer(&aconnector->i2c->base, msgs, num) == num;
686 
687 	kfree(msgs);
688 
689 	return result;
690 }
691 
692 bool dm_helpers_execute_fused_io(
693 		struct dc_context *ctx,
694 		struct dc_link *link,
695 		union dmub_rb_cmd *commands,
696 		uint8_t count,
697 		uint32_t timeout_us
698 )
699 {
700 	struct amdgpu_device *dev = ctx->driver_context;
701 
702 	return amdgpu_dm_execute_fused_io(dev, link, commands, count, timeout_us);
703 }
704 
705 static bool execute_synaptics_rc_command(struct drm_dp_aux *aux,
706 		bool is_write_cmd,
707 		unsigned char cmd,
708 		unsigned int length,
709 		unsigned int offset,
710 		unsigned char *data)
711 {
712 	bool success = false;
713 	unsigned char rc_data[16] = {0};
714 	unsigned char rc_offset[4] = {0};
715 	unsigned char rc_length[2] = {0};
716 	unsigned char rc_cmd = 0;
717 	unsigned char rc_result = 0xFF;
718 	unsigned char i = 0;
719 	int ret;
720 
721 	if (is_write_cmd) {
722 		// write rc data
723 		memmove(rc_data, data, length);
724 		ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_DATA, rc_data, sizeof(rc_data));
725 		if (ret < 0)
726 			goto err;
727 	}
728 
729 	// write rc offset
730 	rc_offset[0] = (unsigned char) offset & 0xFF;
731 	rc_offset[1] = (unsigned char) (offset >> 8) & 0xFF;
732 	rc_offset[2] = (unsigned char) (offset >> 16) & 0xFF;
733 	rc_offset[3] = (unsigned char) (offset >> 24) & 0xFF;
734 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_OFFSET, rc_offset, sizeof(rc_offset));
735 	if (ret < 0)
736 		goto err;
737 
738 	// write rc length
739 	rc_length[0] = (unsigned char) length & 0xFF;
740 	rc_length[1] = (unsigned char) (length >> 8) & 0xFF;
741 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_LENGTH, rc_length, sizeof(rc_length));
742 	if (ret < 0)
743 		goto err;
744 
745 	// write rc cmd
746 	rc_cmd = cmd | 0x80;
747 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_COMMAND, &rc_cmd, sizeof(rc_cmd));
748 	if (ret < 0)
749 		goto err;
750 
751 	// poll until active is 0
752 	for (i = 0; i < 10; i++) {
753 		drm_dp_dpcd_read(aux, SYNAPTICS_RC_COMMAND, &rc_cmd, sizeof(rc_cmd));
754 		if (rc_cmd == cmd)
755 			// active is 0
756 			break;
757 		msleep(10);
758 	}
759 
760 	// read rc result
761 	drm_dp_dpcd_read(aux, SYNAPTICS_RC_RESULT, &rc_result, sizeof(rc_result));
762 	success = (rc_result == 0);
763 
764 	if (success && !is_write_cmd) {
765 		// read rc data
766 		drm_dp_dpcd_read(aux, SYNAPTICS_RC_DATA, data, length);
767 	}
768 
769 	drm_dbg_dp(aux->drm_dev, "success = %d\n", success);
770 
771 	return success;
772 
773 err:
774 	DRM_ERROR("%s: write cmd ..., err = %d\n",  __func__, ret);
775 	return false;
776 }
777 
778 static void apply_synaptics_fifo_reset_wa(struct drm_dp_aux *aux)
779 {
780 	unsigned char data[16] = {0};
781 
782 	drm_dbg_dp(aux->drm_dev, "Start\n");
783 
784 	// Step 2
785 	data[0] = 'P';
786 	data[1] = 'R';
787 	data[2] = 'I';
788 	data[3] = 'U';
789 	data[4] = 'S';
790 
791 	if (!execute_synaptics_rc_command(aux, true, 0x01, 5, 0, data))
792 		return;
793 
794 	// Step 3 and 4
795 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220998, data))
796 		return;
797 
798 	data[0] &= (~(1 << 1)); // set bit 1 to 0
799 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220998, data))
800 		return;
801 
802 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220D98, data))
803 		return;
804 
805 	data[0] &= (~(1 << 1)); // set bit 1 to 0
806 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220D98, data))
807 		return;
808 
809 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x221198, data))
810 		return;
811 
812 	data[0] &= (~(1 << 1)); // set bit 1 to 0
813 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x221198, data))
814 		return;
815 
816 	// Step 3 and 5
817 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220998, data))
818 		return;
819 
820 	data[0] |= (1 << 1); // set bit 1 to 1
821 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220998, data))
822 		return;
823 
824 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220D98, data))
825 		return;
826 
827 	data[0] |= (1 << 1); // set bit 1 to 1
828 
829 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x221198, data))
830 		return;
831 
832 	data[0] |= (1 << 1); // set bit 1 to 1
833 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x221198, data))
834 		return;
835 
836 	// Step 6
837 	if (!execute_synaptics_rc_command(aux, true, 0x02, 0, 0, NULL))
838 		return;
839 
840 	drm_dbg_dp(aux->drm_dev, "Done\n");
841 }
842 
843 /* MST Dock */
844 static const uint8_t SYNAPTICS_DEVICE_ID[] = "SYNA";
845 
846 static uint8_t write_dsc_enable_synaptics_non_virtual_dpcd_mst(
847 		struct drm_dp_aux *aux,
848 		const struct dc_stream_state *stream,
849 		bool enable)
850 {
851 	uint8_t ret = 0;
852 
853 	drm_dbg_dp(aux->drm_dev,
854 		   "MST_DSC Configure DSC to non-virtual dpcd synaptics\n");
855 
856 	if (enable) {
857 		/* When DSC is enabled on previous boot and reboot with the hub,
858 		 * there is a chance that Synaptics hub gets stuck during reboot sequence.
859 		 * Applying a workaround to reset Synaptics SDP fifo before enabling the first stream
860 		 */
861 		if (!stream->link->link_status.link_active &&
862 			memcmp(stream->link->dpcd_caps.branch_dev_name,
863 				(int8_t *)SYNAPTICS_DEVICE_ID, 4) == 0)
864 			apply_synaptics_fifo_reset_wa(aux);
865 
866 		ret = drm_dp_dpcd_write(aux, DP_DSC_ENABLE, &enable, 1);
867 		DRM_INFO("MST_DSC Send DSC enable to synaptics\n");
868 
869 	} else {
870 		/* Synaptics hub not support virtual dpcd,
871 		 * external monitor occur garbage while disable DSC,
872 		 * Disable DSC only when entire link status turn to false,
873 		 */
874 		if (!stream->link->link_status.link_active) {
875 			ret = drm_dp_dpcd_write(aux, DP_DSC_ENABLE, &enable, 1);
876 			DRM_INFO("MST_DSC Send DSC disable to synaptics\n");
877 		}
878 	}
879 
880 	return ret;
881 }
882 
883 bool dm_helpers_dp_write_dsc_enable(
884 		struct dc_context *ctx,
885 		const struct dc_stream_state *stream,
886 		bool enable)
887 {
888 	static const uint8_t DSC_DISABLE;
889 	static const uint8_t DSC_DECODING = 0x01;
890 	static const uint8_t DSC_PASSTHROUGH = 0x02;
891 
892 	struct amdgpu_dm_connector *aconnector =
893 		(struct amdgpu_dm_connector *)stream->dm_stream_context;
894 	struct drm_device *dev = aconnector->base.dev;
895 	struct drm_dp_mst_port *port;
896 	uint8_t enable_dsc = enable ? DSC_DECODING : DSC_DISABLE;
897 	uint8_t enable_passthrough = enable ? DSC_PASSTHROUGH : DSC_DISABLE;
898 	uint8_t ret = 0;
899 
900 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
901 		if (!aconnector->dsc_aux)
902 			return false;
903 
904 		// apply w/a to synaptics
905 		if (needs_dsc_aux_workaround(aconnector->dc_link) &&
906 		    (aconnector->mst_downstream_port_present.byte & 0x7) != 0x3)
907 			return write_dsc_enable_synaptics_non_virtual_dpcd_mst(
908 				aconnector->dsc_aux, stream, enable_dsc);
909 
910 		port = aconnector->mst_output_port;
911 
912 		if (enable) {
913 			if (port->passthrough_aux) {
914 				ret = drm_dp_dpcd_write(port->passthrough_aux,
915 							DP_DSC_ENABLE,
916 							&enable_passthrough, 1);
917 				drm_dbg_dp(dev,
918 					   "MST_DSC Sent DSC pass-through enable to virtual dpcd port, ret = %u\n",
919 					   ret);
920 			}
921 
922 			ret = drm_dp_dpcd_write(aconnector->dsc_aux,
923 						DP_DSC_ENABLE, &enable_dsc, 1);
924 			drm_dbg_dp(dev,
925 				   "MST_DSC Sent DSC decoding enable to %s port, ret = %u\n",
926 				   (port->passthrough_aux) ? "remote RX" :
927 				   "virtual dpcd",
928 				   ret);
929 		} else {
930 			ret = drm_dp_dpcd_write(aconnector->dsc_aux,
931 						DP_DSC_ENABLE, &enable_dsc, 1);
932 			drm_dbg_dp(dev,
933 				   "MST_DSC Sent DSC decoding disable to %s port, ret = %u\n",
934 				   (port->passthrough_aux) ? "remote RX" :
935 				   "virtual dpcd",
936 				   ret);
937 
938 			if (port->passthrough_aux) {
939 				ret = drm_dp_dpcd_write(port->passthrough_aux,
940 							DP_DSC_ENABLE,
941 							&enable_passthrough, 1);
942 				drm_dbg_dp(dev,
943 					   "MST_DSC Sent DSC pass-through disable to virtual dpcd port, ret = %u\n",
944 					   ret);
945 			}
946 		}
947 	}
948 
949 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT || stream->signal == SIGNAL_TYPE_EDP) {
950 		if (stream->sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
951 			ret = dm_helpers_dp_write_dpcd(ctx, stream->link, DP_DSC_ENABLE, &enable_dsc, 1);
952 			drm_dbg_dp(dev,
953 				   "SST_DSC Send DSC %s to SST RX\n",
954 				   enable_dsc ? "enable" : "disable");
955 		} else if (stream->sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
956 			ret = dm_helpers_dp_write_dpcd(ctx, stream->link, DP_DSC_ENABLE, &enable_dsc, 1);
957 			drm_dbg_dp(dev,
958 				   "SST_DSC Send DSC %s to DP-HDMI PCON\n",
959 				   enable_dsc ? "enable" : "disable");
960 		}
961 	}
962 
963 	return ret;
964 }
965 
966 bool dm_helpers_dp_write_hblank_reduction(struct dc_context *ctx, const struct dc_stream_state *stream)
967 {
968 	// TODO
969 	return false;
970 }
971 
972 bool dm_helpers_is_dp_sink_present(struct dc_link *link)
973 {
974 	bool dp_sink_present;
975 	struct amdgpu_dm_connector *aconnector = link->priv;
976 
977 	if (!aconnector) {
978 		DRM_ERROR("Failed to find connector for link!");
979 		return true;
980 	}
981 
982 	mutex_lock(&aconnector->dm_dp_aux.aux.hw_mutex);
983 	dp_sink_present = dc_link_is_dp_sink_present(link);
984 	mutex_unlock(&aconnector->dm_dp_aux.aux.hw_mutex);
985 	return dp_sink_present;
986 }
987 
988 static int
989 dm_helpers_probe_acpi_edid(void *data, u8 *buf, unsigned int block, size_t len)
990 {
991 	struct drm_connector *connector = data;
992 	struct acpi_device *acpidev = ACPI_COMPANION(connector->dev->dev);
993 	unsigned short start = block * EDID_LENGTH;
994 	struct edid *edid;
995 	int r;
996 
997 	if (!acpidev)
998 		return -ENODEV;
999 
1000 	/* fetch the entire edid from BIOS */
1001 	r = acpi_video_get_edid(acpidev, ACPI_VIDEO_DISPLAY_LCD, -1, (void *)&edid);
1002 	if (r < 0) {
1003 		drm_dbg(connector->dev, "Failed to get EDID from ACPI: %d\n", r);
1004 		return r;
1005 	}
1006 	if (len > r || start > r || start + len > r) {
1007 		r = -EINVAL;
1008 		goto cleanup;
1009 	}
1010 
1011 	/* sanity check */
1012 	if (edid->revision < 4 || !(edid->input & DRM_EDID_INPUT_DIGITAL) ||
1013 	    (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_UNDEF) {
1014 		r = -EINVAL;
1015 		goto cleanup;
1016 	}
1017 
1018 	memcpy(buf, (void *)edid + start, len);
1019 	r = 0;
1020 
1021 cleanup:
1022 	kfree(edid);
1023 
1024 	return r;
1025 }
1026 
1027 static const struct drm_edid *
1028 dm_helpers_read_acpi_edid(struct amdgpu_dm_connector *aconnector)
1029 {
1030 	struct drm_connector *connector = &aconnector->base;
1031 
1032 	if (amdgpu_dc_debug_mask & DC_DISABLE_ACPI_EDID)
1033 		return NULL;
1034 
1035 	switch (connector->connector_type) {
1036 	case DRM_MODE_CONNECTOR_LVDS:
1037 	case DRM_MODE_CONNECTOR_eDP:
1038 		break;
1039 	default:
1040 		return NULL;
1041 	}
1042 
1043 	if (connector->force == DRM_FORCE_OFF)
1044 		return NULL;
1045 
1046 	return drm_edid_read_custom(connector, dm_helpers_probe_acpi_edid, connector);
1047 }
1048 
1049 static const struct drm_edid *
1050 dm_helpers_read_vbios_hardcoded_edid(struct dc_link *link, struct amdgpu_dm_connector *aconnector)
1051 {
1052 	struct dc_bios *bios = link->ctx->dc_bios;
1053 	struct embedded_panel_info info;
1054 	const struct drm_edid *edid;
1055 	enum bp_result r;
1056 
1057 	if (!dc_is_embedded_signal(link->connector_signal) ||
1058 	    !bios->funcs->get_embedded_panel_info)
1059 		return NULL;
1060 
1061 	memset(&info, 0, sizeof(info));
1062 	r = bios->funcs->get_embedded_panel_info(bios, &info);
1063 
1064 	if (r != BP_RESULT_OK) {
1065 		dm_error("Error when reading embedded panel info: %u\n", r);
1066 		return NULL;
1067 	}
1068 
1069 	if (!info.fake_edid || !info.fake_edid_size) {
1070 		dm_error("Embedded panel info doesn't contain an EDID\n");
1071 		return NULL;
1072 	}
1073 
1074 	edid = drm_edid_alloc(info.fake_edid, info.fake_edid_size);
1075 
1076 	if (!drm_edid_valid(edid)) {
1077 		dm_error("EDID from embedded panel info is invalid\n");
1078 		drm_edid_free(edid);
1079 		return NULL;
1080 	}
1081 
1082 	aconnector->base.display_info.width_mm = info.panel_width_mm;
1083 	aconnector->base.display_info.height_mm = info.panel_height_mm;
1084 
1085 	return edid;
1086 }
1087 
1088 static uint8_t get_max_frl_rate(uint8_t max_lanes, uint8_t max_rate_per_lane)
1089 {
1090 	uint8_t max_frl_rate;
1091 
1092 	if ((max_lanes == 3) && (max_rate_per_lane == 3))
1093 		max_frl_rate = 1;
1094 	else if ((max_lanes == 3) && (max_rate_per_lane == 6))
1095 		max_frl_rate = 2;
1096 	else if ((max_lanes == 4) && (max_rate_per_lane == 6))
1097 		max_frl_rate = 3;
1098 	else if ((max_lanes == 4) && (max_rate_per_lane == 8))
1099 		max_frl_rate = 4;
1100 	else if ((max_lanes == 4) && (max_rate_per_lane == 10))
1101 		max_frl_rate = 5;
1102 	else if ((max_lanes == 4) && (max_rate_per_lane == 12))
1103 		max_frl_rate = 6;
1104 	else
1105 		max_frl_rate = 0;
1106 
1107 	return max_frl_rate;
1108 }
1109 
1110 static uint8_t get_dsc_max_slices(uint8_t max_slices, int clk_per_slice)
1111 {
1112 	uint8_t dsc_max_slices;
1113 
1114 	if ((max_slices == 1) && (clk_per_slice == 340))
1115 		dsc_max_slices = 1;
1116 	else if ((max_slices == 2) && (clk_per_slice == 340))
1117 		dsc_max_slices = 2;
1118 	else if ((max_slices == 4) && (clk_per_slice == 340))
1119 		dsc_max_slices = 3;
1120 	else if ((max_slices == 8) && (clk_per_slice == 340))
1121 		dsc_max_slices = 4;
1122 	else if ((max_slices == 8) && (clk_per_slice == 400))
1123 		dsc_max_slices = 5;
1124 	else if ((max_slices == 12) && (clk_per_slice == 400))
1125 		dsc_max_slices = 6;
1126 	else if ((max_slices == 16) && (clk_per_slice == 400))
1127 		dsc_max_slices = 7;
1128 	else
1129 		dsc_max_slices = 0;
1130 
1131 	return dsc_max_slices;
1132 }
1133 
1134 void populate_hdmi_info_from_connector(bool enable_frl, struct drm_hdmi_info *hdmi, struct dc_edid_caps *edid_caps)
1135 {
1136 	edid_caps->scdc_present = hdmi->scdc.supported;
1137 	if (enable_frl) {
1138 		edid_caps->max_frl_rate = get_max_frl_rate(hdmi->max_lanes, hdmi->max_frl_rate_per_lane);
1139 		edid_caps->frl_dsc_support = hdmi->dsc_cap.v_1p2;
1140 		if (edid_caps->frl_dsc_support) {
1141 			if (hdmi->dsc_cap.bpc_supported == 10)
1142 				edid_caps->frl_dsc_10bpc = true;
1143 			else if (hdmi->dsc_cap.bpc_supported == 12)
1144 				edid_caps->frl_dsc_12bpc = true;
1145 			edid_caps->frl_dsc_all_bpp = hdmi->dsc_cap.all_bpp;
1146 			edid_caps->frl_dsc_native_420 = hdmi->dsc_cap.native_420;
1147 			edid_caps->frl_dsc_max_slices = get_dsc_max_slices(hdmi->dsc_cap.max_slices, hdmi->dsc_cap.clk_per_slice);
1148 			edid_caps->frl_dsc_max_frl_rate = get_max_frl_rate(hdmi->dsc_cap.max_lanes, hdmi->dsc_cap.max_frl_rate_per_lane);
1149 			edid_caps->frl_dsc_total_chunk_kbytes = hdmi->dsc_cap.total_chunk_kbytes;
1150 		}
1151 	}
1152 }
1153 
1154 enum dc_edid_status dm_helpers_read_local_edid(
1155 		struct dc_context *ctx,
1156 		struct dc_link *link,
1157 		struct dc_sink *sink)
1158 {
1159 	struct amdgpu_dm_connector *aconnector = link->priv;
1160 	struct drm_connector *connector = &aconnector->base;
1161 	struct i2c_adapter *ddc;
1162 	int retry = 25;
1163 	enum dc_edid_status edid_status = EDID_NO_RESPONSE;
1164 	const struct drm_edid *drm_edid;
1165 	const struct edid *edid;
1166 
1167 	if (link->aux_mode)
1168 		ddc = &aconnector->dm_dp_aux.aux.ddc;
1169 	else if (link->ddc_hw_inst == GPIO_DDC_LINE_UNKNOWN &&
1170 		 dc_is_embedded_signal(link->connector_signal))
1171 		ddc = NULL;
1172 	else
1173 		ddc = &aconnector->i2c->base;
1174 
1175 	if (link->dc->hwss.prepare_ddc)
1176 		link->dc->hwss.prepare_ddc(link);
1177 
1178 	/* some dongles read edid incorrectly the first time,
1179 	 * do check sum and retry to make sure read correct edid.
1180 	 */
1181 	do {
1182 		drm_edid = dm_helpers_read_acpi_edid(aconnector);
1183 		if (drm_edid)
1184 			drm_info(connector->dev, "Using ACPI provided EDID for %s\n", connector->name);
1185 		else if (!ddc)
1186 			drm_edid = dm_helpers_read_vbios_hardcoded_edid(link, aconnector);
1187 		else
1188 			drm_edid = drm_edid_read_ddc(connector, ddc);
1189 		drm_edid_connector_update(connector, drm_edid);
1190 
1191 		/* DP Compliance Test 4.2.2.6 */
1192 		if (link->aux_mode && connector->edid_corrupt)
1193 			drm_dp_send_real_edid_checksum(&aconnector->dm_dp_aux.aux, connector->real_edid_checksum);
1194 
1195 		if (!drm_edid && connector->edid_corrupt) {
1196 			connector->edid_corrupt = false;
1197 			return EDID_BAD_CHECKSUM;
1198 		}
1199 
1200 		if (!drm_edid)
1201 			continue;
1202 
1203 		edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
1204 		if (!edid ||
1205 		    edid->extensions >= sizeof(sink->dc_edid.raw_edid) / EDID_LENGTH)
1206 			return EDID_BAD_INPUT;
1207 
1208 		sink->dc_edid.length = EDID_LENGTH * (edid->extensions + 1);
1209 		memmove(sink->dc_edid.raw_edid, (uint8_t *)edid, sink->dc_edid.length);
1210 
1211 		/* We don't need the original edid anymore */
1212 		drm_edid_free(drm_edid);
1213 
1214 		edid_status = dm_helpers_parse_edid_caps(
1215 						link,
1216 						&sink->dc_edid,
1217 						&sink->edid_caps);
1218 
1219 	} while ((edid_status == EDID_BAD_CHECKSUM || edid_status == EDID_NO_RESPONSE) && --retry > 0);
1220 
1221 	if (edid_status != EDID_OK)
1222 		DRM_ERROR("EDID err: %d, on connector: %s",
1223 				edid_status,
1224 				aconnector->base.name);
1225 	if (link->aux_mode) {
1226 		union test_request test_request = {0};
1227 		union test_response test_response = {0};
1228 
1229 		dm_helpers_dp_read_dpcd(ctx,
1230 					link,
1231 					DP_TEST_REQUEST,
1232 					&test_request.raw,
1233 					sizeof(union test_request));
1234 
1235 		if (!test_request.bits.EDID_READ)
1236 			return edid_status;
1237 
1238 		test_response.bits.EDID_CHECKSUM_WRITE = 1;
1239 
1240 		dm_helpers_dp_write_dpcd(ctx,
1241 					link,
1242 					DP_TEST_EDID_CHECKSUM,
1243 					&sink->dc_edid.raw_edid[sink->dc_edid.length-1],
1244 					1);
1245 
1246 		dm_helpers_dp_write_dpcd(ctx,
1247 					link,
1248 					DP_TEST_RESPONSE,
1249 					&test_response.raw,
1250 					sizeof(test_response));
1251 
1252 	}
1253 
1254 	return edid_status;
1255 }
1256 int dm_helper_dmub_aux_transfer_sync(
1257 		struct dc_context *ctx,
1258 		const struct dc_link *link,
1259 		struct aux_payload *payload,
1260 		enum aux_return_code_type *operation_result)
1261 {
1262 	if (!link->hpd_status) {
1263 		*operation_result = AUX_RET_ERROR_HPD_DISCON;
1264 		return -1;
1265 	}
1266 
1267 	return amdgpu_dm_process_dmub_aux_transfer_sync(ctx, link->link_index, payload,
1268 			operation_result);
1269 }
1270 
1271 int dm_helpers_dmub_set_config_sync(struct dc_context *ctx,
1272 		const struct dc_link *link,
1273 		struct set_config_cmd_payload *payload,
1274 		enum set_config_status *operation_result)
1275 {
1276 	return amdgpu_dm_process_dmub_set_config_sync(ctx, link->link_index, payload,
1277 			operation_result);
1278 }
1279 
1280 void dm_set_dcn_clocks(struct dc_context *ctx, struct dc_clocks *clks)
1281 {
1282 	/* TODO: something */
1283 }
1284 
1285 void dm_helpers_dmu_timeout(struct dc_context *ctx)
1286 {
1287 	// TODO:
1288 	//amdgpu_device_gpu_recover(dc_context->driver-context, NULL);
1289 }
1290 
1291 void dm_helpers_smu_timeout(struct dc_context *ctx, unsigned int msg_id, unsigned int param, unsigned int timeout_us)
1292 {
1293 	// TODO:
1294 	//amdgpu_device_gpu_recover(dc_context->driver-context, NULL);
1295 }
1296 
1297 void dm_helpers_init_panel_settings(
1298 	struct dc_context *ctx,
1299 	struct dc_panel_config *panel_config,
1300 	struct dc_sink *sink)
1301 {
1302 	// Extra Panel Power Sequence
1303 	panel_config->pps.extra_t3_ms = sink->edid_caps.panel_patch.extra_t3_ms;
1304 	panel_config->pps.extra_t7_ms = sink->edid_caps.panel_patch.extra_t7_ms;
1305 	panel_config->pps.extra_delay_backlight_off = sink->edid_caps.panel_patch.extra_delay_backlight_off;
1306 	panel_config->pps.extra_post_t7_ms = 0;
1307 	panel_config->pps.extra_pre_t11_ms = 0;
1308 	panel_config->pps.extra_t12_ms = sink->edid_caps.panel_patch.extra_t12_ms;
1309 	panel_config->pps.extra_post_OUI_ms = 0;
1310 	// Feature DSC
1311 	panel_config->dsc.disable_dsc_edp = false;
1312 	panel_config->dsc.force_dsc_edp_policy = 0;
1313 }
1314 
1315 void dm_helpers_override_panel_settings(
1316 	struct dc_context *ctx,
1317 	struct dc_link *link)
1318 {
1319 	unsigned int panel_inst = 0;
1320 
1321 	// Feature DSC
1322 	if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
1323 		link->panel_config.dsc.disable_dsc_edp = true;
1324 
1325 	if (dc_get_edp_link_panel_inst(ctx->dc, link, &panel_inst) && panel_inst == 1) {
1326 		link->panel_config.psr.disable_psr = true;
1327 		link->panel_config.psr.disallow_psrsu = true;
1328 		link->panel_config.psr.disallow_replay = true;
1329 	}
1330 }
1331 
1332 void *dm_helpers_allocate_gpu_mem(
1333 		struct dc_context *ctx,
1334 		enum dc_gpu_mem_alloc_type type,
1335 		size_t size,
1336 		long long *addr)
1337 {
1338 	struct amdgpu_device *adev = ctx->driver_context;
1339 
1340 	return dm_allocate_gpu_mem(adev, type, size, addr);
1341 }
1342 
1343 void dm_helpers_free_gpu_mem(
1344 		struct dc_context *ctx,
1345 		enum dc_gpu_mem_alloc_type type,
1346 		void *pvMem)
1347 {
1348 	struct amdgpu_device *adev = ctx->driver_context;
1349 
1350 	dm_free_gpu_mem(adev, type, pvMem);
1351 }
1352 
1353 bool dm_helpers_dmub_outbox_interrupt_control(struct dc_context *ctx, bool enable)
1354 {
1355 	enum dc_irq_source irq_source;
1356 	bool ret;
1357 
1358 	irq_source = DC_IRQ_SOURCE_DMCUB_OUTBOX;
1359 
1360 	ret = dc_interrupt_set(ctx->dc, irq_source, enable);
1361 
1362 	DRM_DEBUG_DRIVER("Dmub trace irq %sabling: r=%d\n",
1363 			 enable ? "en" : "dis", ret);
1364 	return ret;
1365 }
1366 
1367 void dm_helpers_mst_enable_stream_features(const struct dc_stream_state *stream)
1368 {
1369 	/* TODO: virtual DPCD */
1370 	struct dc_link *link = stream->link;
1371 	union down_spread_ctrl old_downspread;
1372 	union down_spread_ctrl new_downspread;
1373 
1374 	if (link->aux_access_disabled)
1375 		return;
1376 
1377 	if (!dm_helpers_dp_read_dpcd(link->ctx, link, DP_DOWNSPREAD_CTRL,
1378 				     &old_downspread.raw,
1379 				     sizeof(old_downspread)))
1380 		return;
1381 
1382 	new_downspread.raw = old_downspread.raw;
1383 	new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
1384 		(stream->ignore_msa_timing_param) ? 1 : 0;
1385 
1386 	if (new_downspread.raw != old_downspread.raw)
1387 		dm_helpers_dp_write_dpcd(link->ctx, link, DP_DOWNSPREAD_CTRL,
1388 					 &new_downspread.raw,
1389 					 sizeof(new_downspread));
1390 }
1391 
1392 bool dm_helpers_dp_handle_test_pattern_request(
1393 		struct dc_context *ctx,
1394 		const struct dc_link *link,
1395 		union link_test_pattern dpcd_test_pattern,
1396 		union test_misc dpcd_test_params)
1397 {
1398 	enum dp_test_pattern test_pattern;
1399 	enum dp_test_pattern_color_space test_pattern_color_space =
1400 			DP_TEST_PATTERN_COLOR_SPACE_UNDEFINED;
1401 	enum dc_color_depth requestColorDepth = COLOR_DEPTH_UNDEFINED;
1402 	enum dc_pixel_encoding requestPixelEncoding = PIXEL_ENCODING_UNDEFINED;
1403 	struct pipe_ctx *pipes = link->dc->current_state->res_ctx.pipe_ctx;
1404 	struct pipe_ctx *pipe_ctx = NULL;
1405 	struct amdgpu_dm_connector *aconnector = link->priv;
1406 	struct drm_device *dev = aconnector->base.dev;
1407 	struct dc_state *dc_state = ctx->dc->current_state;
1408 	struct clk_mgr *clk_mgr = ctx->dc->clk_mgr;
1409 	int i;
1410 
1411 	for (i = 0; i < MAX_PIPES; i++) {
1412 		if (pipes[i].stream == NULL)
1413 			continue;
1414 
1415 		if (pipes[i].stream->link == link && !pipes[i].top_pipe &&
1416 			!pipes[i].prev_odm_pipe) {
1417 			pipe_ctx = &pipes[i];
1418 			break;
1419 		}
1420 	}
1421 
1422 	if (pipe_ctx == NULL)
1423 		return false;
1424 
1425 	switch (dpcd_test_pattern.bits.PATTERN) {
1426 	case LINK_TEST_PATTERN_COLOR_RAMP:
1427 		test_pattern = DP_TEST_PATTERN_COLOR_RAMP;
1428 	break;
1429 	case LINK_TEST_PATTERN_VERTICAL_BARS:
1430 		test_pattern = DP_TEST_PATTERN_VERTICAL_BARS;
1431 	break; /* black and white */
1432 	case LINK_TEST_PATTERN_COLOR_SQUARES:
1433 		test_pattern = (dpcd_test_params.bits.DYN_RANGE ==
1434 				TEST_DYN_RANGE_VESA ?
1435 				DP_TEST_PATTERN_COLOR_SQUARES :
1436 				DP_TEST_PATTERN_COLOR_SQUARES_CEA);
1437 	break;
1438 	default:
1439 		test_pattern = DP_TEST_PATTERN_VIDEO_MODE;
1440 	break;
1441 	}
1442 
1443 	if (dpcd_test_params.bits.CLR_FORMAT == 0)
1444 		test_pattern_color_space = DP_TEST_PATTERN_COLOR_SPACE_RGB;
1445 	else
1446 		test_pattern_color_space = dpcd_test_params.bits.YCBCR_COEFS ?
1447 				DP_TEST_PATTERN_COLOR_SPACE_YCBCR709 :
1448 				DP_TEST_PATTERN_COLOR_SPACE_YCBCR601;
1449 
1450 	switch (dpcd_test_params.bits.BPC) {
1451 	case 0: // 6 bits
1452 		requestColorDepth = COLOR_DEPTH_666;
1453 		break;
1454 	case 1: // 8 bits
1455 		requestColorDepth = COLOR_DEPTH_888;
1456 		break;
1457 	case 2: // 10 bits
1458 		requestColorDepth = COLOR_DEPTH_101010;
1459 		break;
1460 	case 3: // 12 bits
1461 		requestColorDepth = COLOR_DEPTH_121212;
1462 		break;
1463 	default:
1464 		break;
1465 	}
1466 
1467 	switch (dpcd_test_params.bits.CLR_FORMAT) {
1468 	case 0:
1469 		requestPixelEncoding = PIXEL_ENCODING_RGB;
1470 		break;
1471 	case 1:
1472 		requestPixelEncoding = PIXEL_ENCODING_YCBCR422;
1473 		break;
1474 	case 2:
1475 		requestPixelEncoding = PIXEL_ENCODING_YCBCR444;
1476 		break;
1477 	default:
1478 		requestPixelEncoding = PIXEL_ENCODING_RGB;
1479 		break;
1480 	}
1481 
1482 	if ((requestColorDepth != COLOR_DEPTH_UNDEFINED
1483 		&& pipe_ctx->stream->timing.display_color_depth != requestColorDepth)
1484 		|| (requestPixelEncoding != PIXEL_ENCODING_UNDEFINED
1485 		&& pipe_ctx->stream->timing.pixel_encoding != requestPixelEncoding)) {
1486 		drm_dbg(dev,
1487 			"original bpc %d pix encoding %d, changing to %d  %d\n",
1488 			pipe_ctx->stream->timing.display_color_depth,
1489 			pipe_ctx->stream->timing.pixel_encoding,
1490 			requestColorDepth,
1491 			requestPixelEncoding);
1492 		pipe_ctx->stream->timing.display_color_depth = requestColorDepth;
1493 		pipe_ctx->stream->timing.pixel_encoding = requestPixelEncoding;
1494 
1495 		dc_link_update_dsc_config(pipe_ctx);
1496 
1497 		aconnector->timing_changed = true;
1498 		/* store current timing */
1499 		if (aconnector->timing_requested)
1500 			*aconnector->timing_requested = pipe_ctx->stream->timing;
1501 		else
1502 			drm_err(dev, "timing storage failed\n");
1503 
1504 	}
1505 
1506 	pipe_ctx->stream->test_pattern.type = test_pattern;
1507 	pipe_ctx->stream->test_pattern.color_space = test_pattern_color_space;
1508 
1509 	/* Temp W/A for compliance test failure */
1510 	dc_state->bw_ctx.bw.dcn.clk.p_state_change_support = false;
1511 	dc_state->bw_ctx.bw.dcn.clk.dramclk_khz = clk_mgr->dc_mode_softmax_enabled ?
1512 		clk_mgr->bw_params->dc_mode_softmax_memclk : clk_mgr->bw_params->max_memclk_mhz;
1513 	dc_state->bw_ctx.bw.dcn.clk.idle_dramclk_khz = dc_state->bw_ctx.bw.dcn.clk.dramclk_khz;
1514 	ctx->dc->clk_mgr->funcs->update_clocks(
1515 			ctx->dc->clk_mgr,
1516 			dc_state,
1517 			false);
1518 
1519 	dc_link_dp_set_test_pattern(
1520 		(struct dc_link *) link,
1521 		test_pattern,
1522 		test_pattern_color_space,
1523 		NULL,
1524 		NULL,
1525 		0);
1526 
1527 	return false;
1528 }
1529 
1530 void dm_set_phyd32clk(struct dc_context *ctx, int freq_khz)
1531 {
1532        // TODO
1533 }
1534 
1535 void dm_helpers_enable_periodic_detection(struct dc_context *ctx, bool enable)
1536 {
1537 	struct amdgpu_device *adev = ctx->driver_context;
1538 
1539 	if (adev->dm.idle_workqueue) {
1540 		adev->dm.idle_workqueue->enable = enable;
1541 		if (enable && !adev->dm.idle_workqueue->running && amdgpu_dm_is_headless(adev))
1542 			schedule_work(&adev->dm.idle_workqueue->work);
1543 	}
1544 }
1545 
1546 void dm_helpers_dp_mst_update_branch_bandwidth(
1547 		struct dc_context *ctx,
1548 		struct dc_link *link)
1549 {
1550 	// TODO
1551 }
1552 
1553 static bool dm_is_freesync_pcon_whitelist(const uint32_t branch_dev_id)
1554 {
1555 	bool ret_val = false;
1556 
1557 	switch (branch_dev_id) {
1558 	case DP_BRANCH_DEVICE_ID_0060AD:
1559 	case DP_BRANCH_DEVICE_ID_00E04C:
1560 	case DP_BRANCH_DEVICE_ID_90CC24:
1561 	case DP_BRANCH_DEVICE_ID_001CF8:
1562 	case DP_BRANCH_DEVICE_ID_001FF2:
1563 		ret_val = true;
1564 		break;
1565 	default:
1566 		break;
1567 	}
1568 
1569 	return ret_val;
1570 }
1571 
1572 enum adaptive_sync_type dm_get_adaptive_sync_support_type(struct dc_link *link)
1573 {
1574 	struct dpcd_caps *dpcd_caps = &link->dpcd_caps;
1575 	enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
1576 
1577 	switch (dpcd_caps->dongle_type) {
1578 	case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
1579 		if (dpcd_caps->adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT == true &&
1580 			dpcd_caps->allow_invalid_MSA_timing_param == true &&
1581 			dm_is_freesync_pcon_whitelist(dpcd_caps->branch_dev_id))
1582 			as_type = FREESYNC_TYPE_PCON_IN_WHITELIST;
1583 		break;
1584 	default:
1585 		break;
1586 	}
1587 
1588 	return as_type;
1589 }
1590 
1591 bool dm_helpers_is_fullscreen(struct dc_context *ctx, struct dc_stream_state *stream)
1592 {
1593 	// TODO
1594 	return false;
1595 }
1596 
1597 bool dm_helpers_is_hdr_on(struct dc_context *ctx, struct dc_stream_state *stream)
1598 {
1599 	// TODO
1600 	return false;
1601 }
1602 
1603 static int mccs_operation_vcp_request(unsigned int vcp_code, struct dc_link *link,
1604 				union vcp_reply *reply)
1605 {
1606 	const unsigned char retry_interval_ms = 40;
1607 	unsigned char retry = 5;
1608 	struct amdgpu_dm_connector *aconnector = link->priv;
1609 	struct i2c_adapter *ddc;
1610 	struct i2c_msg msg = {0};
1611 	int ret = 0;
1612 	int idx;
1613 
1614 	unsigned char wr_data[MCCS_OP_BUFF_SIZE__WR_VCP_REQUEST] = {
1615 		MCCS_SRC_ADDR,				/* Byte0 - Src Addr */
1616 		MCCS_LENGTH_OFFSET + 2,		/* Byte1 - Length */
1617 		MCCS_OP_CODE_VCP_REQUEST,	/* Byte2 - MCCS Command */
1618 		(unsigned char) vcp_code,	/* Byte3 - VCP Code */
1619 		MCCS_DEST_ADDR << 1			/* Byte4 - CheckSum */
1620 	};
1621 
1622 	/* calculate checksum */
1623 	for (idx = 0; idx < (MCCS_OP_BUFF_SIZE__WR_VCP_REQUEST - 1); idx++)
1624 		wr_data[(MCCS_OP_BUFF_SIZE__WR_VCP_REQUEST-1)] ^= wr_data[idx];
1625 
1626 	if (link->aux_mode)
1627 		ddc = &aconnector->dm_dp_aux.aux.ddc;
1628 	else
1629 		ddc = &aconnector->i2c->base;
1630 
1631 	do {
1632 		msg.addr = MCCS_DEST_ADDR;
1633 		msg.flags = 0;
1634 		msg.len = MCCS_OP_BUFF_SIZE__WR_VCP_REQUEST;
1635 		msg.buf = wr_data;
1636 
1637 		ret = i2c_transfer(ddc, &msg, 1);
1638 		if (ret != 1)
1639 			goto mccs_retry;
1640 
1641 		msleep(retry_interval_ms);
1642 
1643 		msg.addr = MCCS_DEST_ADDR;
1644 		msg.flags = I2C_M_RD;
1645 		msg.len = MCCS_OP_BUFF_SIZE_RD_VCP_REQUEST;
1646 		msg.buf = reply->raw;
1647 
1648 		ret = i2c_transfer(ddc, &msg, 1);
1649 
1650 		/* sink might reply with null msg if it can't reply in time */
1651 		if (ret == 1 && reply->bytes.length > MCCS_LENGTH_OFFSET)
1652 			break;
1653 mccs_retry:
1654 		retry--;
1655 		msleep(retry_interval_ms);
1656 	} while (retry);
1657 
1658 	if (!retry) {
1659 		drm_dbg_driver(aconnector->base.dev,
1660 			"%s: MCCS VCP request failed after retries", __func__);
1661 		return -EIO;
1662 	}
1663 
1664 	return 0;
1665 }
1666 
1667 void dm_helpers_read_mccs_caps(struct dc_context *ctx, struct dc_link *link,
1668 		struct dc_sink *sink)
1669 {
1670 	bool mccs_op = false;
1671 	struct dpcd_caps *dpcd_caps;
1672 	struct drm_device *dev;
1673 	uint16_t freesync_vcp_value = 0;
1674 	union vcp_reply vcp_reply_value = {0};
1675 
1676 	if (!ctx)
1677 		return;
1678 	dev = adev_to_drm(ctx->driver_context);
1679 
1680 	if (!link || !sink) {
1681 		drm_dbg_driver(dev, "%s: link or sink is NULL", __func__);
1682 		return;
1683 	}
1684 
1685 	sink->mccs_caps.freesync_supported = false;
1686 	dpcd_caps = &link->dpcd_caps;
1687 
1688 	if (sink->edid_caps.freesync_vcp_code != 0) {
1689 		if (dc_is_dp_signal(link->connector_signal)) {
1690 			if ((dpcd_caps->dpcd_rev.raw >= DPCD_REV_14) &&
1691 				(dpcd_caps->dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) &&
1692 				dm_is_freesync_pcon_whitelist(dpcd_caps->branch_dev_id) &&
1693 				(dpcd_caps->adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT == true))
1694 				mccs_op = true;
1695 
1696 			if ((dpcd_caps->dongle_type != DISPLAY_DONGLE_NONE &&
1697 				dpcd_caps->dongle_type != DISPLAY_DONGLE_DP_HDMI_CONVERTER)) {
1698 				if (mccs_op == false)
1699 					drm_dbg_driver(dev, "%s: Legacy Pcon support", __func__);
1700 				mccs_op = true;
1701 			}
1702 
1703 			if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
1704 				// Todo: Freesync over MST
1705 				mccs_op = false;
1706 			}
1707 		}
1708 
1709 		if (dc_is_hdmi_signal(link->connector_signal)) {
1710 			drm_dbg_driver(dev, "%s: Local HDMI sink", __func__);
1711 			mccs_op = true;
1712 		}
1713 
1714 		if (mccs_op == true) {
1715 			// MCCS VCP request to get VCP value
1716 			if (!mccs_operation_vcp_request(sink->edid_caps.freesync_vcp_code, link,
1717 					&vcp_reply_value)) {
1718 				freesync_vcp_value = vcp_reply_value.bytes.present_value[1];
1719 				freesync_vcp_value |= (uint16_t) vcp_reply_value.bytes.present_value[0] << 8;
1720 			}
1721 			// If VCP Value bit 0 is 1, freesyncSupport = true
1722 			sink->mccs_caps.freesync_supported =
1723 				(freesync_vcp_value & FREESYNC_SUPPORTED) ? true : false;
1724 		}
1725 	}
1726 }
1727 
1728 static int mccs_operation_vcp_set(unsigned int vcp_code, struct dc_link *link, uint16_t value)
1729 {
1730 	const unsigned char retry_interval_ms = 40;
1731 	unsigned char retry = 5;
1732 	struct amdgpu_dm_connector *aconnector = link->priv;
1733 	struct i2c_adapter *ddc;
1734 	struct i2c_msg msg = {0};
1735 	int ret = 0;
1736 	int idx;
1737 
1738 	unsigned char wr_data[MCCS_OP_BUFF_SIZE_WR_VCP_SET] = {
1739 		MCCS_SRC_ADDR,				/* Byte0 - Src Addr */
1740 		MCCS_LENGTH_OFFSET + 4,		/* Byte1 - Length */
1741 		MCCS_OP_CODE_VCP_SET,		/* Byte2 - MCCS Command */
1742 		(unsigned char)vcp_code,	/* Byte3 - VCP Code */
1743 		(unsigned char)(value >> 8),	/* Byte4 - Value High Byte */
1744 		(unsigned char)(value & 0xFF),	/* Byte5 - Value Low Byte */
1745 		MCCS_DEST_ADDR << 1		/* Byte6 - CheckSum */
1746 	};
1747 
1748 	/* calculate checksum */
1749 	for (idx = 0; idx < (MCCS_OP_BUFF_SIZE_WR_VCP_SET - 1); idx++)
1750 		wr_data[MCCS_OP_BUFF_SIZE_WR_VCP_SET - 1] ^= wr_data[idx];
1751 
1752 	if (link->aux_mode)
1753 		ddc = &aconnector->dm_dp_aux.aux.ddc;
1754 	else
1755 		ddc = &aconnector->i2c->base;
1756 
1757 	do {
1758 		msg.addr = MCCS_DEST_ADDR;
1759 		msg.flags = 0;
1760 		msg.len = MCCS_OP_BUFF_SIZE_WR_VCP_SET;
1761 		msg.buf = wr_data;
1762 
1763 		ret = i2c_transfer(ddc, &msg, 1);
1764 		if (ret == 1)
1765 			break;
1766 
1767 		retry--;
1768 		msleep(retry_interval_ms);
1769 	} while (retry);
1770 
1771 	if (!retry)
1772 		return -EIO;
1773 
1774 	return 0;
1775 }
1776 
1777 void dm_helpers_mccs_vcp_set(struct dc_context *ctx, struct dc_link *link,
1778 		struct dc_sink *sink)
1779 {
1780 	struct drm_device *dev;
1781 	const uint16_t enable = 0x0101;
1782 
1783 	if (!ctx)
1784 		return;
1785 	dev = adev_to_drm(ctx->driver_context);
1786 
1787 	if (!link || !sink) {
1788 		drm_dbg_driver(dev, "%s: link or sink is NULL", __func__);
1789 		return;
1790 	}
1791 
1792 	if (!sink->mccs_caps.freesync_supported) {
1793 		drm_dbg_driver(dev, "%s: MCCS freesync not supported on this sink", __func__);
1794 		return;
1795 	}
1796 
1797 	if (mccs_operation_vcp_set(sink->edid_caps.freesync_vcp_code, link, enable))
1798 		drm_dbg_driver(dev, "%s: Failed to set VCP code %d", __func__,
1799 				sink->edid_caps.freesync_vcp_code);
1800 }
1801 
1802