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