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