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