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