xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_helpers.c (revision ec0b62ccc986c06552c57f54116171cfd186ef92)
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 
edid_extract_panel_id(struct edid * edid)52 static u32 edid_extract_panel_id(struct edid *edid)
53 {
54 	return (u32)edid->mfg_id[0] << 24   |
55 	       (u32)edid->mfg_id[1] << 16   |
56 	       (u32)EDID_PRODUCT_ID(edid);
57 }
58 
apply_edid_quirks(struct drm_device * dev,struct edid * edid,struct dc_edid_caps * edid_caps)59 static void apply_edid_quirks(struct drm_device *dev, struct edid *edid, struct dc_edid_caps *edid_caps)
60 {
61 	uint32_t panel_id = edid_extract_panel_id(edid);
62 
63 	switch (panel_id) {
64 	/* Workaround for monitors that need a delay after detecting the link */
65 	case drm_edid_encode_panel_id('G', 'B', 'T', 0x3215):
66 		drm_dbg_driver(dev, "Add 10s delay for link detection for panel id %X\n", panel_id);
67 		edid_caps->panel_patch.wait_after_dpcd_poweroff_ms = 10000;
68 		break;
69 	/* Workaround for some monitors which does not work well with FAMS */
70 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x0E5E):
71 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x7053):
72 	case drm_edid_encode_panel_id('S', 'A', 'M', 0x71AC):
73 		drm_dbg_driver(dev, "Disabling FAMS on monitor with panel id %X\n", panel_id);
74 		edid_caps->panel_patch.disable_fams = true;
75 		break;
76 	/* Workaround for some monitors that do not clear DPCD 0x317 if FreeSync is unsupported */
77 	case drm_edid_encode_panel_id('A', 'U', 'O', 0xA7AB):
78 	case drm_edid_encode_panel_id('A', 'U', 'O', 0xE69B):
79 	case drm_edid_encode_panel_id('B', 'O', 'E', 0x092A):
80 	case drm_edid_encode_panel_id('L', 'G', 'D', 0x06D1):
81 	case drm_edid_encode_panel_id('M', 'S', 'F', 0x1003):
82 		drm_dbg_driver(dev, "Clearing DPCD 0x317 on monitor with panel id %X\n", panel_id);
83 		edid_caps->panel_patch.remove_sink_ext_caps = true;
84 		break;
85 	case drm_edid_encode_panel_id('S', 'D', 'C', 0x4154):
86 	case drm_edid_encode_panel_id('S', 'D', 'C', 0x4171):
87 		drm_dbg_driver(dev, "Disabling VSC on monitor with panel id %X\n", panel_id);
88 		edid_caps->panel_patch.disable_colorimetry = true;
89 		break;
90 	default:
91 		return;
92 	}
93 }
94 
95 /**
96  * dm_helpers_parse_edid_caps() - Parse edid caps
97  *
98  * @link: current detected link
99  * @edid:	[in] pointer to edid
100  * @edid_caps:	[in] pointer to edid caps
101  *
102  * Return: void
103  */
dm_helpers_parse_edid_caps(struct dc_link * link,const struct dc_edid * edid,struct dc_edid_caps * edid_caps)104 enum dc_edid_status dm_helpers_parse_edid_caps(
105 		struct dc_link *link,
106 		const struct dc_edid *edid,
107 		struct dc_edid_caps *edid_caps)
108 {
109 	struct amdgpu_dm_connector *aconnector = link->priv;
110 	struct drm_connector *connector = &aconnector->base;
111 	struct drm_device *dev = connector->dev;
112 	struct edid *edid_buf = edid ? (struct edid *) edid->raw_edid : NULL;
113 	struct cea_sad *sads;
114 	int sad_count = -1;
115 	int sadb_count = -1;
116 	int i = 0;
117 	uint8_t *sadb = NULL;
118 
119 	enum dc_edid_status result = EDID_OK;
120 
121 	if (!edid_caps || !edid)
122 		return EDID_BAD_INPUT;
123 
124 	if (!drm_edid_is_valid(edid_buf))
125 		result = EDID_BAD_CHECKSUM;
126 
127 	edid_caps->manufacturer_id = (uint16_t) edid_buf->mfg_id[0] |
128 					((uint16_t) edid_buf->mfg_id[1])<<8;
129 	edid_caps->product_id = (uint16_t) edid_buf->prod_code[0] |
130 					((uint16_t) edid_buf->prod_code[1])<<8;
131 	edid_caps->serial_number = edid_buf->serial;
132 	edid_caps->manufacture_week = edid_buf->mfg_week;
133 	edid_caps->manufacture_year = edid_buf->mfg_year;
134 
135 	drm_edid_get_monitor_name(edid_buf,
136 				  edid_caps->display_name,
137 				  AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
138 
139 	edid_caps->edid_hdmi = connector->display_info.is_hdmi;
140 
141 	apply_edid_quirks(dev, edid_buf, edid_caps);
142 
143 	sad_count = drm_edid_to_sad((struct edid *) edid->raw_edid, &sads);
144 	if (sad_count <= 0)
145 		return result;
146 
147 	edid_caps->audio_mode_count = min(sad_count, DC_MAX_AUDIO_DESC_COUNT);
148 	for (i = 0; i < edid_caps->audio_mode_count; ++i) {
149 		struct cea_sad *sad = &sads[i];
150 
151 		edid_caps->audio_modes[i].format_code = sad->format;
152 		edid_caps->audio_modes[i].channel_count = sad->channels + 1;
153 		edid_caps->audio_modes[i].sample_rate = sad->freq;
154 		edid_caps->audio_modes[i].sample_size = sad->byte2;
155 	}
156 
157 	sadb_count = drm_edid_to_speaker_allocation((struct edid *) edid->raw_edid, &sadb);
158 
159 	if (sadb_count < 0) {
160 		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sadb_count);
161 		sadb_count = 0;
162 	}
163 
164 	if (sadb_count)
165 		edid_caps->speaker_flags = sadb[0];
166 	else
167 		edid_caps->speaker_flags = DEFAULT_SPEAKER_LOCATION;
168 
169 	kfree(sads);
170 	kfree(sadb);
171 
172 	return result;
173 }
174 
175 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)176 fill_dc_mst_payload_table_from_drm(struct dc_link *link,
177 				   bool enable,
178 				   struct drm_dp_mst_atomic_payload *target_payload,
179 				   struct dc_dp_mst_stream_allocation_table *table)
180 {
181 	struct dc_dp_mst_stream_allocation_table new_table = { 0 };
182 	struct dc_dp_mst_stream_allocation *sa;
183 	struct link_mst_stream_allocation_table copy_of_link_table =
184 										link->mst_stream_alloc_table;
185 
186 	int i;
187 	int current_hw_table_stream_cnt = copy_of_link_table.stream_count;
188 	struct link_mst_stream_allocation *dc_alloc;
189 
190 	/* TODO: refactor to set link->mst_stream_alloc_table directly if possible.*/
191 	if (enable) {
192 		dc_alloc =
193 		&copy_of_link_table.stream_allocations[current_hw_table_stream_cnt];
194 		dc_alloc->vcp_id = target_payload->vcpi;
195 		dc_alloc->slot_count = target_payload->time_slots;
196 	} else {
197 		for (i = 0; i < copy_of_link_table.stream_count; i++) {
198 			dc_alloc =
199 			&copy_of_link_table.stream_allocations[i];
200 
201 			if (dc_alloc->vcp_id == target_payload->vcpi) {
202 				dc_alloc->vcp_id = 0;
203 				dc_alloc->slot_count = 0;
204 				break;
205 			}
206 		}
207 		ASSERT(i != copy_of_link_table.stream_count);
208 	}
209 
210 	/* Fill payload info*/
211 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
212 		dc_alloc =
213 			&copy_of_link_table.stream_allocations[i];
214 		if (dc_alloc->vcp_id > 0 && dc_alloc->slot_count > 0) {
215 			sa = &new_table.stream_allocations[new_table.stream_count];
216 			sa->slot_count = dc_alloc->slot_count;
217 			sa->vcp_id = dc_alloc->vcp_id;
218 			new_table.stream_count++;
219 		}
220 	}
221 
222 	/* Overwrite the old table */
223 	*table = new_table;
224 }
225 
dm_helpers_dp_update_branch_info(struct dc_context * ctx,const struct dc_link * link)226 void dm_helpers_dp_update_branch_info(
227 	struct dc_context *ctx,
228 	const struct dc_link *link)
229 {}
230 
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)231 static void dm_helpers_construct_old_payload(
232 			struct drm_dp_mst_topology_mgr *mgr,
233 			struct drm_dp_mst_topology_state *mst_state,
234 			struct drm_dp_mst_atomic_payload *new_payload,
235 			struct drm_dp_mst_atomic_payload *old_payload)
236 {
237 	struct drm_dp_mst_atomic_payload *pos;
238 	int pbn_per_slot = dfixed_trunc(mst_state->pbn_div);
239 	u8 next_payload_vc_start = mgr->next_start_slot;
240 	u8 payload_vc_start = new_payload->vc_start_slot;
241 	u8 allocated_time_slots;
242 
243 	*old_payload = *new_payload;
244 
245 	/* Set correct time_slots/PBN of old payload.
246 	 * other fields (delete & dsc_enabled) in
247 	 * struct drm_dp_mst_atomic_payload are don't care fields
248 	 * while calling drm_dp_remove_payload_part2()
249 	 */
250 	list_for_each_entry(pos, &mst_state->payloads, next) {
251 		if (pos != new_payload &&
252 		    pos->vc_start_slot > payload_vc_start &&
253 		    pos->vc_start_slot < next_payload_vc_start)
254 			next_payload_vc_start = pos->vc_start_slot;
255 	}
256 
257 	allocated_time_slots = next_payload_vc_start - payload_vc_start;
258 
259 	old_payload->time_slots = allocated_time_slots;
260 	old_payload->pbn = allocated_time_slots * pbn_per_slot;
261 }
262 
263 /*
264  * Writes payload allocation table in immediate downstream device.
265  */
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)266 bool dm_helpers_dp_mst_write_payload_allocation_table(
267 		struct dc_context *ctx,
268 		const struct dc_stream_state *stream,
269 		struct dc_dp_mst_stream_allocation_table *proposed_table,
270 		bool enable)
271 {
272 	struct amdgpu_dm_connector *aconnector;
273 	struct drm_dp_mst_topology_state *mst_state;
274 	struct drm_dp_mst_atomic_payload *target_payload, *new_payload, old_payload;
275 	struct drm_dp_mst_topology_mgr *mst_mgr;
276 
277 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
278 	/* Accessing the connector state is required for vcpi_slots allocation
279 	 * and directly relies on behaviour in commit check
280 	 * that blocks before commit guaranteeing that the state
281 	 * is not gonna be swapped while still in use in commit tail
282 	 */
283 
284 	if (!aconnector || !aconnector->mst_root)
285 		return false;
286 
287 	mst_mgr = &aconnector->mst_root->mst_mgr;
288 	mst_state = to_drm_dp_mst_topology_state(mst_mgr->base.state);
289 	new_payload = drm_atomic_get_mst_payload_state(mst_state, aconnector->mst_output_port);
290 
291 	if (enable) {
292 		target_payload = new_payload;
293 
294 		/* It's OK for this to fail */
295 		drm_dp_add_payload_part1(mst_mgr, mst_state, new_payload);
296 	} else {
297 		/* construct old payload by VCPI*/
298 		dm_helpers_construct_old_payload(mst_mgr, mst_state,
299 						 new_payload, &old_payload);
300 		target_payload = &old_payload;
301 
302 		drm_dp_remove_payload_part1(mst_mgr, mst_state, new_payload);
303 	}
304 
305 	/* mst_mgr->->payloads are VC payload notify MST branch using DPCD or
306 	 * AUX message. The sequence is slot 1-63 allocated sequence for each
307 	 * stream. AMD ASIC stream slot allocation should follow the same
308 	 * sequence. copy DRM MST allocation to dc
309 	 */
310 	fill_dc_mst_payload_table_from_drm(stream->link, enable, target_payload, proposed_table);
311 
312 	return true;
313 }
314 
315 /*
316  * poll pending down reply
317  */
dm_helpers_dp_mst_poll_pending_down_reply(struct dc_context * ctx,const struct dc_link * link)318 void dm_helpers_dp_mst_poll_pending_down_reply(
319 	struct dc_context *ctx,
320 	const struct dc_link *link)
321 {}
322 
323 /*
324  * Clear payload allocation table before enable MST DP link.
325  */
dm_helpers_dp_mst_clear_payload_allocation_table(struct dc_context * ctx,const struct dc_link * link)326 void dm_helpers_dp_mst_clear_payload_allocation_table(
327 	struct dc_context *ctx,
328 	const struct dc_link *link)
329 {}
330 
331 /*
332  * Polls for ACT (allocation change trigger) handled and sends
333  * ALLOCATE_PAYLOAD message.
334  */
dm_helpers_dp_mst_poll_for_allocation_change_trigger(struct dc_context * ctx,const struct dc_stream_state * stream)335 enum act_return_status dm_helpers_dp_mst_poll_for_allocation_change_trigger(
336 		struct dc_context *ctx,
337 		const struct dc_stream_state *stream)
338 {
339 	struct amdgpu_dm_connector *aconnector;
340 	struct drm_dp_mst_topology_mgr *mst_mgr;
341 	int ret;
342 
343 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
344 
345 	if (!aconnector || !aconnector->mst_root)
346 		return ACT_FAILED;
347 
348 	mst_mgr = &aconnector->mst_root->mst_mgr;
349 
350 	if (!mst_mgr->mst_state)
351 		return ACT_FAILED;
352 
353 	ret = drm_dp_check_act_status(mst_mgr);
354 
355 	if (ret)
356 		return ACT_FAILED;
357 
358 	return ACT_SUCCESS;
359 }
360 
dm_helpers_dp_mst_send_payload_allocation(struct dc_context * ctx,const struct dc_stream_state * stream)361 void dm_helpers_dp_mst_send_payload_allocation(
362 		struct dc_context *ctx,
363 		const struct dc_stream_state *stream)
364 {
365 	struct amdgpu_dm_connector *aconnector;
366 	struct drm_dp_mst_topology_state *mst_state;
367 	struct drm_dp_mst_topology_mgr *mst_mgr;
368 	struct drm_dp_mst_atomic_payload *new_payload;
369 	enum mst_progress_status set_flag = MST_ALLOCATE_NEW_PAYLOAD;
370 	enum mst_progress_status clr_flag = MST_CLEAR_ALLOCATED_PAYLOAD;
371 	int ret = 0;
372 
373 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
374 
375 	if (!aconnector || !aconnector->mst_root)
376 		return;
377 
378 	mst_mgr = &aconnector->mst_root->mst_mgr;
379 	mst_state = to_drm_dp_mst_topology_state(mst_mgr->base.state);
380 	new_payload = drm_atomic_get_mst_payload_state(mst_state, aconnector->mst_output_port);
381 
382 	ret = drm_dp_add_payload_part2(mst_mgr, new_payload);
383 
384 	if (ret) {
385 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
386 			set_flag, false);
387 	} else {
388 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
389 			set_flag, true);
390 		amdgpu_dm_set_mst_status(&aconnector->mst_status,
391 			clr_flag, false);
392 	}
393 }
394 
dm_helpers_dp_mst_update_mst_mgr_for_deallocation(struct dc_context * ctx,const struct dc_stream_state * stream)395 void dm_helpers_dp_mst_update_mst_mgr_for_deallocation(
396 		struct dc_context *ctx,
397 		const struct dc_stream_state *stream)
398 {
399 	struct amdgpu_dm_connector *aconnector;
400 	struct drm_dp_mst_topology_state *mst_state;
401 	struct drm_dp_mst_topology_mgr *mst_mgr;
402 	struct drm_dp_mst_atomic_payload *new_payload, old_payload;
403 	enum mst_progress_status set_flag = MST_CLEAR_ALLOCATED_PAYLOAD;
404 	enum mst_progress_status clr_flag = MST_ALLOCATE_NEW_PAYLOAD;
405 
406 	aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
407 
408 	if (!aconnector || !aconnector->mst_root)
409 		return;
410 
411 	mst_mgr = &aconnector->mst_root->mst_mgr;
412 	mst_state = to_drm_dp_mst_topology_state(mst_mgr->base.state);
413 	new_payload = drm_atomic_get_mst_payload_state(mst_state, aconnector->mst_output_port);
414 	dm_helpers_construct_old_payload(mst_mgr, mst_state,
415 					 new_payload, &old_payload);
416 
417 	drm_dp_remove_payload_part2(mst_mgr, mst_state, &old_payload, new_payload);
418 
419 	amdgpu_dm_set_mst_status(&aconnector->mst_status, set_flag, true);
420 	amdgpu_dm_set_mst_status(&aconnector->mst_status, clr_flag, false);
421  }
422 
dm_dtn_log_begin(struct dc_context * ctx,struct dc_log_buffer_ctx * log_ctx)423 void dm_dtn_log_begin(struct dc_context *ctx,
424 	struct dc_log_buffer_ctx *log_ctx)
425 {
426 	static const char msg[] = "[dtn begin]\n";
427 
428 	if (!log_ctx) {
429 		pr_info("%s", msg);
430 		return;
431 	}
432 
433 	dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
434 }
435 
436 __printf(3, 4)
dm_dtn_log_append_v(struct dc_context * ctx,struct dc_log_buffer_ctx * log_ctx,const char * msg,...)437 void dm_dtn_log_append_v(struct dc_context *ctx,
438 	struct dc_log_buffer_ctx *log_ctx,
439 	const char *msg, ...)
440 {
441 	va_list args;
442 	size_t total;
443 	int n;
444 
445 	if (!log_ctx) {
446 		/* No context, redirect to dmesg. */
447 		struct va_format vaf;
448 
449 		vaf.fmt = msg;
450 		vaf.va = &args;
451 
452 		va_start(args, msg);
453 		pr_info("%pV", &vaf);
454 		va_end(args);
455 
456 		return;
457 	}
458 
459 	/* Measure the output. */
460 	va_start(args, msg);
461 	n = vsnprintf(NULL, 0, msg, args);
462 	va_end(args);
463 
464 	if (n <= 0)
465 		return;
466 
467 	/* Reallocate the string buffer as needed. */
468 	total = log_ctx->pos + n + 1;
469 
470 	if (total > log_ctx->size) {
471 		char *buf = kvcalloc(total, sizeof(char), GFP_KERNEL);
472 
473 		if (buf) {
474 			memcpy(buf, log_ctx->buf, log_ctx->pos);
475 			kfree(log_ctx->buf);
476 
477 			log_ctx->buf = buf;
478 			log_ctx->size = total;
479 		}
480 	}
481 
482 	if (!log_ctx->buf)
483 		return;
484 
485 	/* Write the formatted string to the log buffer. */
486 	va_start(args, msg);
487 	n = vscnprintf(
488 		log_ctx->buf + log_ctx->pos,
489 		log_ctx->size - log_ctx->pos,
490 		msg,
491 		args);
492 	va_end(args);
493 
494 	if (n > 0)
495 		log_ctx->pos += n;
496 }
497 
dm_dtn_log_end(struct dc_context * ctx,struct dc_log_buffer_ctx * log_ctx)498 void dm_dtn_log_end(struct dc_context *ctx,
499 	struct dc_log_buffer_ctx *log_ctx)
500 {
501 	static const char msg[] = "[dtn end]\n";
502 
503 	if (!log_ctx) {
504 		pr_info("%s", msg);
505 		return;
506 	}
507 
508 	dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
509 }
510 
dm_helpers_dp_mst_start_top_mgr(struct dc_context * ctx,const struct dc_link * link,bool boot)511 bool dm_helpers_dp_mst_start_top_mgr(
512 		struct dc_context *ctx,
513 		const struct dc_link *link,
514 		bool boot)
515 {
516 	struct amdgpu_dm_connector *aconnector = link->priv;
517 	int ret;
518 
519 	if (!aconnector) {
520 		DRM_ERROR("Failed to find connector for link!");
521 		return false;
522 	}
523 
524 	if (boot) {
525 		DRM_INFO("DM_MST: Differing MST start on aconnector: %p [id: %d]\n",
526 					aconnector, aconnector->base.base.id);
527 		return true;
528 	}
529 
530 	DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
531 			aconnector, aconnector->base.base.id);
532 
533 	ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
534 	if (ret < 0) {
535 		DRM_ERROR("DM_MST: Failed to set the device into MST mode!");
536 		return false;
537 	}
538 
539 	DRM_INFO("DM_MST: DP%x, %d-lane link detected\n", aconnector->mst_mgr.dpcd[0],
540 		aconnector->mst_mgr.dpcd[2] & DP_MAX_LANE_COUNT_MASK);
541 
542 	return true;
543 }
544 
dm_helpers_dp_mst_stop_top_mgr(struct dc_context * ctx,struct dc_link * link)545 bool dm_helpers_dp_mst_stop_top_mgr(
546 		struct dc_context *ctx,
547 		struct dc_link *link)
548 {
549 	struct amdgpu_dm_connector *aconnector = link->priv;
550 
551 	if (!aconnector) {
552 		DRM_ERROR("Failed to find connector for link!");
553 		return false;
554 	}
555 
556 	DRM_INFO("DM_MST: stopping TM on aconnector: %p [id: %d]\n",
557 			aconnector, aconnector->base.base.id);
558 
559 	if (aconnector->mst_mgr.mst_state == true) {
560 		drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, false);
561 		link->cur_link_settings.lane_count = 0;
562 	}
563 
564 	return false;
565 }
566 
dm_helpers_dp_read_dpcd(struct dc_context * ctx,const struct dc_link * link,uint32_t address,uint8_t * data,uint32_t size)567 bool dm_helpers_dp_read_dpcd(
568 		struct dc_context *ctx,
569 		const struct dc_link *link,
570 		uint32_t address,
571 		uint8_t *data,
572 		uint32_t size)
573 {
574 
575 	struct amdgpu_dm_connector *aconnector = link->priv;
576 
577 	if (!aconnector)
578 		return false;
579 
580 	return drm_dp_dpcd_read(&aconnector->dm_dp_aux.aux, address, data,
581 				size) == size;
582 }
583 
dm_helpers_dp_write_dpcd(struct dc_context * ctx,const struct dc_link * link,uint32_t address,const uint8_t * data,uint32_t size)584 bool dm_helpers_dp_write_dpcd(
585 		struct dc_context *ctx,
586 		const struct dc_link *link,
587 		uint32_t address,
588 		const uint8_t *data,
589 		uint32_t size)
590 {
591 	struct amdgpu_dm_connector *aconnector = link->priv;
592 
593 	if (!aconnector)
594 		return false;
595 
596 	return drm_dp_dpcd_write(&aconnector->dm_dp_aux.aux,
597 			address, (uint8_t *)data, size) > 0;
598 }
599 
dm_helpers_submit_i2c(struct dc_context * ctx,const struct dc_link * link,struct i2c_command * cmd)600 bool dm_helpers_submit_i2c(
601 		struct dc_context *ctx,
602 		const struct dc_link *link,
603 		struct i2c_command *cmd)
604 {
605 	struct amdgpu_dm_connector *aconnector = link->priv;
606 	struct i2c_msg *msgs;
607 	int i = 0;
608 	int num = cmd->number_of_payloads;
609 	bool result;
610 
611 	if (!aconnector) {
612 		DRM_ERROR("Failed to find connector for link!");
613 		return false;
614 	}
615 
616 	msgs = kcalloc(num, sizeof(struct i2c_msg), GFP_KERNEL);
617 
618 	if (!msgs)
619 		return false;
620 
621 	for (i = 0; i < num; i++) {
622 		msgs[i].flags = cmd->payloads[i].write ? 0 : I2C_M_RD;
623 		msgs[i].addr = cmd->payloads[i].address;
624 		msgs[i].len = cmd->payloads[i].length;
625 		msgs[i].buf = cmd->payloads[i].data;
626 	}
627 
628 	result = i2c_transfer(&aconnector->i2c->base, msgs, num) == num;
629 
630 	kfree(msgs);
631 
632 	return result;
633 }
634 
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)635 bool dm_helpers_execute_fused_io(
636 		struct dc_context *ctx,
637 		struct dc_link *link,
638 		union dmub_rb_cmd *commands,
639 		uint8_t count,
640 		uint32_t timeout_us
641 )
642 {
643 	struct amdgpu_device *dev = ctx->driver_context;
644 
645 	return amdgpu_dm_execute_fused_io(dev, link, commands, count, timeout_us);
646 }
647 
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)648 static bool execute_synaptics_rc_command(struct drm_dp_aux *aux,
649 		bool is_write_cmd,
650 		unsigned char cmd,
651 		unsigned int length,
652 		unsigned int offset,
653 		unsigned char *data)
654 {
655 	bool success = false;
656 	unsigned char rc_data[16] = {0};
657 	unsigned char rc_offset[4] = {0};
658 	unsigned char rc_length[2] = {0};
659 	unsigned char rc_cmd = 0;
660 	unsigned char rc_result = 0xFF;
661 	unsigned char i = 0;
662 	int ret;
663 
664 	if (is_write_cmd) {
665 		// write rc data
666 		memmove(rc_data, data, length);
667 		ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_DATA, rc_data, sizeof(rc_data));
668 		if (ret < 0)
669 			goto err;
670 	}
671 
672 	// write rc offset
673 	rc_offset[0] = (unsigned char) offset & 0xFF;
674 	rc_offset[1] = (unsigned char) (offset >> 8) & 0xFF;
675 	rc_offset[2] = (unsigned char) (offset >> 16) & 0xFF;
676 	rc_offset[3] = (unsigned char) (offset >> 24) & 0xFF;
677 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_OFFSET, rc_offset, sizeof(rc_offset));
678 	if (ret < 0)
679 		goto err;
680 
681 	// write rc length
682 	rc_length[0] = (unsigned char) length & 0xFF;
683 	rc_length[1] = (unsigned char) (length >> 8) & 0xFF;
684 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_LENGTH, rc_length, sizeof(rc_length));
685 	if (ret < 0)
686 		goto err;
687 
688 	// write rc cmd
689 	rc_cmd = cmd | 0x80;
690 	ret = drm_dp_dpcd_write(aux, SYNAPTICS_RC_COMMAND, &rc_cmd, sizeof(rc_cmd));
691 	if (ret < 0)
692 		goto err;
693 
694 	// poll until active is 0
695 	for (i = 0; i < 10; i++) {
696 		drm_dp_dpcd_read(aux, SYNAPTICS_RC_COMMAND, &rc_cmd, sizeof(rc_cmd));
697 		if (rc_cmd == cmd)
698 			// active is 0
699 			break;
700 		msleep(10);
701 	}
702 
703 	// read rc result
704 	drm_dp_dpcd_read(aux, SYNAPTICS_RC_RESULT, &rc_result, sizeof(rc_result));
705 	success = (rc_result == 0);
706 
707 	if (success && !is_write_cmd) {
708 		// read rc data
709 		drm_dp_dpcd_read(aux, SYNAPTICS_RC_DATA, data, length);
710 	}
711 
712 	drm_dbg_dp(aux->drm_dev, "success = %d\n", success);
713 
714 	return success;
715 
716 err:
717 	DRM_ERROR("%s: write cmd ..., err = %d\n",  __func__, ret);
718 	return false;
719 }
720 
apply_synaptics_fifo_reset_wa(struct drm_dp_aux * aux)721 static void apply_synaptics_fifo_reset_wa(struct drm_dp_aux *aux)
722 {
723 	unsigned char data[16] = {0};
724 
725 	drm_dbg_dp(aux->drm_dev, "Start\n");
726 
727 	// Step 2
728 	data[0] = 'P';
729 	data[1] = 'R';
730 	data[2] = 'I';
731 	data[3] = 'U';
732 	data[4] = 'S';
733 
734 	if (!execute_synaptics_rc_command(aux, true, 0x01, 5, 0, data))
735 		return;
736 
737 	// Step 3 and 4
738 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220998, data))
739 		return;
740 
741 	data[0] &= (~(1 << 1)); // set bit 1 to 0
742 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220998, data))
743 		return;
744 
745 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220D98, data))
746 		return;
747 
748 	data[0] &= (~(1 << 1)); // set bit 1 to 0
749 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220D98, data))
750 		return;
751 
752 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x221198, data))
753 		return;
754 
755 	data[0] &= (~(1 << 1)); // set bit 1 to 0
756 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x221198, data))
757 		return;
758 
759 	// Step 3 and 5
760 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220998, data))
761 		return;
762 
763 	data[0] |= (1 << 1); // set bit 1 to 1
764 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x220998, data))
765 		return;
766 
767 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x220D98, data))
768 		return;
769 
770 	data[0] |= (1 << 1); // set bit 1 to 1
771 
772 	if (!execute_synaptics_rc_command(aux, false, 0x31, 4, 0x221198, data))
773 		return;
774 
775 	data[0] |= (1 << 1); // set bit 1 to 1
776 	if (!execute_synaptics_rc_command(aux, true, 0x21, 4, 0x221198, data))
777 		return;
778 
779 	// Step 6
780 	if (!execute_synaptics_rc_command(aux, true, 0x02, 0, 0, NULL))
781 		return;
782 
783 	drm_dbg_dp(aux->drm_dev, "Done\n");
784 }
785 
786 /* MST Dock */
787 static const uint8_t SYNAPTICS_DEVICE_ID[] = "SYNA";
788 
write_dsc_enable_synaptics_non_virtual_dpcd_mst(struct drm_dp_aux * aux,const struct dc_stream_state * stream,bool enable)789 static uint8_t write_dsc_enable_synaptics_non_virtual_dpcd_mst(
790 		struct drm_dp_aux *aux,
791 		const struct dc_stream_state *stream,
792 		bool enable)
793 {
794 	uint8_t ret = 0;
795 
796 	drm_dbg_dp(aux->drm_dev,
797 		   "MST_DSC Configure DSC to non-virtual dpcd synaptics\n");
798 
799 	if (enable) {
800 		/* When DSC is enabled on previous boot and reboot with the hub,
801 		 * there is a chance that Synaptics hub gets stuck during reboot sequence.
802 		 * Applying a workaround to reset Synaptics SDP fifo before enabling the first stream
803 		 */
804 		if (!stream->link->link_status.link_active &&
805 			memcmp(stream->link->dpcd_caps.branch_dev_name,
806 				(int8_t *)SYNAPTICS_DEVICE_ID, 4) == 0)
807 			apply_synaptics_fifo_reset_wa(aux);
808 
809 		ret = drm_dp_dpcd_write(aux, DP_DSC_ENABLE, &enable, 1);
810 		DRM_INFO("MST_DSC Send DSC enable to synaptics\n");
811 
812 	} else {
813 		/* Synaptics hub not support virtual dpcd,
814 		 * external monitor occur garbage while disable DSC,
815 		 * Disable DSC only when entire link status turn to false,
816 		 */
817 		if (!stream->link->link_status.link_active) {
818 			ret = drm_dp_dpcd_write(aux, DP_DSC_ENABLE, &enable, 1);
819 			DRM_INFO("MST_DSC Send DSC disable to synaptics\n");
820 		}
821 	}
822 
823 	return ret;
824 }
825 
dm_helpers_dp_write_dsc_enable(struct dc_context * ctx,const struct dc_stream_state * stream,bool enable)826 bool dm_helpers_dp_write_dsc_enable(
827 		struct dc_context *ctx,
828 		const struct dc_stream_state *stream,
829 		bool enable)
830 {
831 	static const uint8_t DSC_DISABLE;
832 	static const uint8_t DSC_DECODING = 0x01;
833 	static const uint8_t DSC_PASSTHROUGH = 0x02;
834 
835 	struct amdgpu_dm_connector *aconnector =
836 		(struct amdgpu_dm_connector *)stream->dm_stream_context;
837 	struct drm_device *dev = aconnector->base.dev;
838 	struct drm_dp_mst_port *port;
839 	uint8_t enable_dsc = enable ? DSC_DECODING : DSC_DISABLE;
840 	uint8_t enable_passthrough = enable ? DSC_PASSTHROUGH : DSC_DISABLE;
841 	uint8_t ret = 0;
842 
843 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
844 		if (!aconnector->dsc_aux)
845 			return false;
846 
847 		// apply w/a to synaptics
848 		if (needs_dsc_aux_workaround(aconnector->dc_link) &&
849 		    (aconnector->mst_downstream_port_present.byte & 0x7) != 0x3)
850 			return write_dsc_enable_synaptics_non_virtual_dpcd_mst(
851 				aconnector->dsc_aux, stream, enable_dsc);
852 
853 		port = aconnector->mst_output_port;
854 
855 		if (enable) {
856 			if (port->passthrough_aux) {
857 				ret = drm_dp_dpcd_write(port->passthrough_aux,
858 							DP_DSC_ENABLE,
859 							&enable_passthrough, 1);
860 				drm_dbg_dp(dev,
861 					   "MST_DSC Sent DSC pass-through enable to virtual dpcd port, ret = %u\n",
862 					   ret);
863 			}
864 
865 			ret = drm_dp_dpcd_write(aconnector->dsc_aux,
866 						DP_DSC_ENABLE, &enable_dsc, 1);
867 			drm_dbg_dp(dev,
868 				   "MST_DSC Sent DSC decoding enable to %s port, ret = %u\n",
869 				   (port->passthrough_aux) ? "remote RX" :
870 				   "virtual dpcd",
871 				   ret);
872 		} else {
873 			ret = drm_dp_dpcd_write(aconnector->dsc_aux,
874 						DP_DSC_ENABLE, &enable_dsc, 1);
875 			drm_dbg_dp(dev,
876 				   "MST_DSC Sent DSC decoding disable to %s port, ret = %u\n",
877 				   (port->passthrough_aux) ? "remote RX" :
878 				   "virtual dpcd",
879 				   ret);
880 
881 			if (port->passthrough_aux) {
882 				ret = drm_dp_dpcd_write(port->passthrough_aux,
883 							DP_DSC_ENABLE,
884 							&enable_passthrough, 1);
885 				drm_dbg_dp(dev,
886 					   "MST_DSC Sent DSC pass-through disable to virtual dpcd port, ret = %u\n",
887 					   ret);
888 			}
889 		}
890 	}
891 
892 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT || stream->signal == SIGNAL_TYPE_EDP) {
893 		if (stream->sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
894 			ret = dm_helpers_dp_write_dpcd(ctx, stream->link, DP_DSC_ENABLE, &enable_dsc, 1);
895 			drm_dbg_dp(dev,
896 				   "SST_DSC Send DSC %s to SST RX\n",
897 				   enable_dsc ? "enable" : "disable");
898 		} else if (stream->sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
899 			ret = dm_helpers_dp_write_dpcd(ctx, stream->link, DP_DSC_ENABLE, &enable_dsc, 1);
900 			drm_dbg_dp(dev,
901 				   "SST_DSC Send DSC %s to DP-HDMI PCON\n",
902 				   enable_dsc ? "enable" : "disable");
903 		}
904 	}
905 
906 	return ret;
907 }
908 
dm_helpers_dp_write_hblank_reduction(struct dc_context * ctx,const struct dc_stream_state * stream)909 bool dm_helpers_dp_write_hblank_reduction(struct dc_context *ctx, const struct dc_stream_state *stream)
910 {
911 	// TODO
912 	return false;
913 }
914 
dm_helpers_is_dp_sink_present(struct dc_link * link)915 bool dm_helpers_is_dp_sink_present(struct dc_link *link)
916 {
917 	bool dp_sink_present;
918 	struct amdgpu_dm_connector *aconnector = link->priv;
919 
920 	if (!aconnector) {
921 		BUG_ON("Failed to find connector for link!");
922 		return true;
923 	}
924 
925 	mutex_lock(&aconnector->dm_dp_aux.aux.hw_mutex);
926 	dp_sink_present = dc_link_is_dp_sink_present(link);
927 	mutex_unlock(&aconnector->dm_dp_aux.aux.hw_mutex);
928 	return dp_sink_present;
929 }
930 
931 static int
dm_helpers_probe_acpi_edid(void * data,u8 * buf,unsigned int block,size_t len)932 dm_helpers_probe_acpi_edid(void *data, u8 *buf, unsigned int block, size_t len)
933 {
934 	struct drm_connector *connector = data;
935 	struct acpi_device *acpidev = ACPI_COMPANION(connector->dev->dev);
936 	unsigned short start = block * EDID_LENGTH;
937 	struct edid *edid;
938 	int r;
939 
940 	if (!acpidev)
941 		return -ENODEV;
942 
943 	/* fetch the entire edid from BIOS */
944 	r = acpi_video_get_edid(acpidev, ACPI_VIDEO_DISPLAY_LCD, -1, (void *)&edid);
945 	if (r < 0) {
946 		drm_dbg(connector->dev, "Failed to get EDID from ACPI: %d\n", r);
947 		return r;
948 	}
949 	if (len > r || start > r || start + len > r) {
950 		r = -EINVAL;
951 		goto cleanup;
952 	}
953 
954 	/* sanity check */
955 	if (edid->revision < 4 || !(edid->input & DRM_EDID_INPUT_DIGITAL) ||
956 	    (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_UNDEF) {
957 		r = -EINVAL;
958 		goto cleanup;
959 	}
960 
961 	memcpy(buf, (void *)edid + start, len);
962 	r = 0;
963 
964 cleanup:
965 	kfree(edid);
966 
967 	return r;
968 }
969 
970 static const struct drm_edid *
dm_helpers_read_acpi_edid(struct amdgpu_dm_connector * aconnector)971 dm_helpers_read_acpi_edid(struct amdgpu_dm_connector *aconnector)
972 {
973 	struct drm_connector *connector = &aconnector->base;
974 
975 	if (amdgpu_dc_debug_mask & DC_DISABLE_ACPI_EDID)
976 		return NULL;
977 
978 	switch (connector->connector_type) {
979 	case DRM_MODE_CONNECTOR_LVDS:
980 	case DRM_MODE_CONNECTOR_eDP:
981 		break;
982 	default:
983 		return NULL;
984 	}
985 
986 	if (connector->force == DRM_FORCE_OFF)
987 		return NULL;
988 
989 	return drm_edid_read_custom(connector, dm_helpers_probe_acpi_edid, connector);
990 }
991 
dm_helpers_read_local_edid(struct dc_context * ctx,struct dc_link * link,struct dc_sink * sink)992 enum dc_edid_status dm_helpers_read_local_edid(
993 		struct dc_context *ctx,
994 		struct dc_link *link,
995 		struct dc_sink *sink)
996 {
997 	struct amdgpu_dm_connector *aconnector = link->priv;
998 	struct drm_connector *connector = &aconnector->base;
999 	struct i2c_adapter *ddc;
1000 	int retry = 3;
1001 	enum dc_edid_status edid_status;
1002 	const struct drm_edid *drm_edid;
1003 	const struct edid *edid;
1004 
1005 	if (link->aux_mode)
1006 		ddc = &aconnector->dm_dp_aux.aux.ddc;
1007 	else
1008 		ddc = &aconnector->i2c->base;
1009 
1010 	/* some dongles read edid incorrectly the first time,
1011 	 * do check sum and retry to make sure read correct edid.
1012 	 */
1013 	do {
1014 		drm_edid = dm_helpers_read_acpi_edid(aconnector);
1015 		if (drm_edid)
1016 			drm_info(connector->dev, "Using ACPI provided EDID for %s\n", connector->name);
1017 		else
1018 			drm_edid = drm_edid_read_ddc(connector, ddc);
1019 		drm_edid_connector_update(connector, drm_edid);
1020 
1021 		/* DP Compliance Test 4.2.2.6 */
1022 		if (link->aux_mode && connector->edid_corrupt)
1023 			drm_dp_send_real_edid_checksum(&aconnector->dm_dp_aux.aux, connector->real_edid_checksum);
1024 
1025 		if (!drm_edid && connector->edid_corrupt) {
1026 			connector->edid_corrupt = false;
1027 			return EDID_BAD_CHECKSUM;
1028 		}
1029 
1030 		if (!drm_edid)
1031 			return EDID_NO_RESPONSE;
1032 
1033 		edid = drm_edid_raw(drm_edid); // FIXME: Get rid of drm_edid_raw()
1034 		if (!edid ||
1035 		    edid->extensions >= sizeof(sink->dc_edid.raw_edid) / EDID_LENGTH)
1036 			return EDID_BAD_INPUT;
1037 
1038 		sink->dc_edid.length = EDID_LENGTH * (edid->extensions + 1);
1039 		memmove(sink->dc_edid.raw_edid, (uint8_t *)edid, sink->dc_edid.length);
1040 
1041 		/* We don't need the original edid anymore */
1042 		drm_edid_free(drm_edid);
1043 
1044 		edid_status = dm_helpers_parse_edid_caps(
1045 						link,
1046 						&sink->dc_edid,
1047 						&sink->edid_caps);
1048 
1049 	} while (edid_status == EDID_BAD_CHECKSUM && --retry > 0);
1050 
1051 	if (edid_status != EDID_OK)
1052 		DRM_ERROR("EDID err: %d, on connector: %s",
1053 				edid_status,
1054 				aconnector->base.name);
1055 	if (link->aux_mode) {
1056 		union test_request test_request = {0};
1057 		union test_response test_response = {0};
1058 
1059 		dm_helpers_dp_read_dpcd(ctx,
1060 					link,
1061 					DP_TEST_REQUEST,
1062 					&test_request.raw,
1063 					sizeof(union test_request));
1064 
1065 		if (!test_request.bits.EDID_READ)
1066 			return edid_status;
1067 
1068 		test_response.bits.EDID_CHECKSUM_WRITE = 1;
1069 
1070 		dm_helpers_dp_write_dpcd(ctx,
1071 					link,
1072 					DP_TEST_EDID_CHECKSUM,
1073 					&sink->dc_edid.raw_edid[sink->dc_edid.length-1],
1074 					1);
1075 
1076 		dm_helpers_dp_write_dpcd(ctx,
1077 					link,
1078 					DP_TEST_RESPONSE,
1079 					&test_response.raw,
1080 					sizeof(test_response));
1081 
1082 	}
1083 
1084 	return edid_status;
1085 }
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)1086 int dm_helper_dmub_aux_transfer_sync(
1087 		struct dc_context *ctx,
1088 		const struct dc_link *link,
1089 		struct aux_payload *payload,
1090 		enum aux_return_code_type *operation_result)
1091 {
1092 	if (!link->hpd_status) {
1093 		*operation_result = AUX_RET_ERROR_HPD_DISCON;
1094 		return -1;
1095 	}
1096 
1097 	return amdgpu_dm_process_dmub_aux_transfer_sync(ctx, link->link_index, payload,
1098 			operation_result);
1099 }
1100 
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)1101 int dm_helpers_dmub_set_config_sync(struct dc_context *ctx,
1102 		const struct dc_link *link,
1103 		struct set_config_cmd_payload *payload,
1104 		enum set_config_status *operation_result)
1105 {
1106 	return amdgpu_dm_process_dmub_set_config_sync(ctx, link->link_index, payload,
1107 			operation_result);
1108 }
1109 
dm_set_dcn_clocks(struct dc_context * ctx,struct dc_clocks * clks)1110 void dm_set_dcn_clocks(struct dc_context *ctx, struct dc_clocks *clks)
1111 {
1112 	/* TODO: something */
1113 }
1114 
dm_helpers_smu_timeout(struct dc_context * ctx,unsigned int msg_id,unsigned int param,unsigned int timeout_us)1115 void dm_helpers_smu_timeout(struct dc_context *ctx, unsigned int msg_id, unsigned int param, unsigned int timeout_us)
1116 {
1117 	// TODO:
1118 	//amdgpu_device_gpu_recover(dc_context->driver-context, NULL);
1119 }
1120 
dm_helpers_init_panel_settings(struct dc_context * ctx,struct dc_panel_config * panel_config,struct dc_sink * sink)1121 void dm_helpers_init_panel_settings(
1122 	struct dc_context *ctx,
1123 	struct dc_panel_config *panel_config,
1124 	struct dc_sink *sink)
1125 {
1126 	// Extra Panel Power Sequence
1127 	panel_config->pps.extra_t3_ms = sink->edid_caps.panel_patch.extra_t3_ms;
1128 	panel_config->pps.extra_t7_ms = sink->edid_caps.panel_patch.extra_t7_ms;
1129 	panel_config->pps.extra_delay_backlight_off = sink->edid_caps.panel_patch.extra_delay_backlight_off;
1130 	panel_config->pps.extra_post_t7_ms = 0;
1131 	panel_config->pps.extra_pre_t11_ms = 0;
1132 	panel_config->pps.extra_t12_ms = sink->edid_caps.panel_patch.extra_t12_ms;
1133 	panel_config->pps.extra_post_OUI_ms = 0;
1134 	// Feature DSC
1135 	panel_config->dsc.disable_dsc_edp = false;
1136 	panel_config->dsc.force_dsc_edp_policy = 0;
1137 }
1138 
dm_helpers_override_panel_settings(struct dc_context * ctx,struct dc_panel_config * panel_config)1139 void dm_helpers_override_panel_settings(
1140 	struct dc_context *ctx,
1141 	struct dc_panel_config *panel_config)
1142 {
1143 	// Feature DSC
1144 	if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
1145 		panel_config->dsc.disable_dsc_edp = true;
1146 }
1147 
dm_helpers_allocate_gpu_mem(struct dc_context * ctx,enum dc_gpu_mem_alloc_type type,size_t size,long long * addr)1148 void *dm_helpers_allocate_gpu_mem(
1149 		struct dc_context *ctx,
1150 		enum dc_gpu_mem_alloc_type type,
1151 		size_t size,
1152 		long long *addr)
1153 {
1154 	struct amdgpu_device *adev = ctx->driver_context;
1155 
1156 	return dm_allocate_gpu_mem(adev, type, size, addr);
1157 }
1158 
dm_helpers_free_gpu_mem(struct dc_context * ctx,enum dc_gpu_mem_alloc_type type,void * pvMem)1159 void dm_helpers_free_gpu_mem(
1160 		struct dc_context *ctx,
1161 		enum dc_gpu_mem_alloc_type type,
1162 		void *pvMem)
1163 {
1164 	struct amdgpu_device *adev = ctx->driver_context;
1165 
1166 	dm_free_gpu_mem(adev, type, pvMem);
1167 }
1168 
dm_helpers_dmub_outbox_interrupt_control(struct dc_context * ctx,bool enable)1169 bool dm_helpers_dmub_outbox_interrupt_control(struct dc_context *ctx, bool enable)
1170 {
1171 	enum dc_irq_source irq_source;
1172 	bool ret;
1173 
1174 	irq_source = DC_IRQ_SOURCE_DMCUB_OUTBOX;
1175 
1176 	ret = dc_interrupt_set(ctx->dc, irq_source, enable);
1177 
1178 	DRM_DEBUG_DRIVER("Dmub trace irq %sabling: r=%d\n",
1179 			 enable ? "en" : "dis", ret);
1180 	return ret;
1181 }
1182 
dm_helpers_mst_enable_stream_features(const struct dc_stream_state * stream)1183 void dm_helpers_mst_enable_stream_features(const struct dc_stream_state *stream)
1184 {
1185 	/* TODO: virtual DPCD */
1186 	struct dc_link *link = stream->link;
1187 	union down_spread_ctrl old_downspread;
1188 	union down_spread_ctrl new_downspread;
1189 
1190 	if (link->aux_access_disabled)
1191 		return;
1192 
1193 	if (!dm_helpers_dp_read_dpcd(link->ctx, link, DP_DOWNSPREAD_CTRL,
1194 				     &old_downspread.raw,
1195 				     sizeof(old_downspread)))
1196 		return;
1197 
1198 	new_downspread.raw = old_downspread.raw;
1199 	new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
1200 		(stream->ignore_msa_timing_param) ? 1 : 0;
1201 
1202 	if (new_downspread.raw != old_downspread.raw)
1203 		dm_helpers_dp_write_dpcd(link->ctx, link, DP_DOWNSPREAD_CTRL,
1204 					 &new_downspread.raw,
1205 					 sizeof(new_downspread));
1206 }
1207 
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)1208 bool dm_helpers_dp_handle_test_pattern_request(
1209 		struct dc_context *ctx,
1210 		const struct dc_link *link,
1211 		union link_test_pattern dpcd_test_pattern,
1212 		union test_misc dpcd_test_params)
1213 {
1214 	enum dp_test_pattern test_pattern;
1215 	enum dp_test_pattern_color_space test_pattern_color_space =
1216 			DP_TEST_PATTERN_COLOR_SPACE_UNDEFINED;
1217 	enum dc_color_depth requestColorDepth = COLOR_DEPTH_UNDEFINED;
1218 	enum dc_pixel_encoding requestPixelEncoding = PIXEL_ENCODING_UNDEFINED;
1219 	struct pipe_ctx *pipes = link->dc->current_state->res_ctx.pipe_ctx;
1220 	struct pipe_ctx *pipe_ctx = NULL;
1221 	struct amdgpu_dm_connector *aconnector = link->priv;
1222 	struct drm_device *dev = aconnector->base.dev;
1223 	struct dc_state *dc_state = ctx->dc->current_state;
1224 	struct clk_mgr *clk_mgr = ctx->dc->clk_mgr;
1225 	int i;
1226 
1227 	for (i = 0; i < MAX_PIPES; i++) {
1228 		if (pipes[i].stream == NULL)
1229 			continue;
1230 
1231 		if (pipes[i].stream->link == link && !pipes[i].top_pipe &&
1232 			!pipes[i].prev_odm_pipe) {
1233 			pipe_ctx = &pipes[i];
1234 			break;
1235 		}
1236 	}
1237 
1238 	if (pipe_ctx == NULL)
1239 		return false;
1240 
1241 	switch (dpcd_test_pattern.bits.PATTERN) {
1242 	case LINK_TEST_PATTERN_COLOR_RAMP:
1243 		test_pattern = DP_TEST_PATTERN_COLOR_RAMP;
1244 	break;
1245 	case LINK_TEST_PATTERN_VERTICAL_BARS:
1246 		test_pattern = DP_TEST_PATTERN_VERTICAL_BARS;
1247 	break; /* black and white */
1248 	case LINK_TEST_PATTERN_COLOR_SQUARES:
1249 		test_pattern = (dpcd_test_params.bits.DYN_RANGE ==
1250 				TEST_DYN_RANGE_VESA ?
1251 				DP_TEST_PATTERN_COLOR_SQUARES :
1252 				DP_TEST_PATTERN_COLOR_SQUARES_CEA);
1253 	break;
1254 	default:
1255 		test_pattern = DP_TEST_PATTERN_VIDEO_MODE;
1256 	break;
1257 	}
1258 
1259 	if (dpcd_test_params.bits.CLR_FORMAT == 0)
1260 		test_pattern_color_space = DP_TEST_PATTERN_COLOR_SPACE_RGB;
1261 	else
1262 		test_pattern_color_space = dpcd_test_params.bits.YCBCR_COEFS ?
1263 				DP_TEST_PATTERN_COLOR_SPACE_YCBCR709 :
1264 				DP_TEST_PATTERN_COLOR_SPACE_YCBCR601;
1265 
1266 	switch (dpcd_test_params.bits.BPC) {
1267 	case 0: // 6 bits
1268 		requestColorDepth = COLOR_DEPTH_666;
1269 		break;
1270 	case 1: // 8 bits
1271 		requestColorDepth = COLOR_DEPTH_888;
1272 		break;
1273 	case 2: // 10 bits
1274 		requestColorDepth = COLOR_DEPTH_101010;
1275 		break;
1276 	case 3: // 12 bits
1277 		requestColorDepth = COLOR_DEPTH_121212;
1278 		break;
1279 	default:
1280 		break;
1281 	}
1282 
1283 	switch (dpcd_test_params.bits.CLR_FORMAT) {
1284 	case 0:
1285 		requestPixelEncoding = PIXEL_ENCODING_RGB;
1286 		break;
1287 	case 1:
1288 		requestPixelEncoding = PIXEL_ENCODING_YCBCR422;
1289 		break;
1290 	case 2:
1291 		requestPixelEncoding = PIXEL_ENCODING_YCBCR444;
1292 		break;
1293 	default:
1294 		requestPixelEncoding = PIXEL_ENCODING_RGB;
1295 		break;
1296 	}
1297 
1298 	if ((requestColorDepth != COLOR_DEPTH_UNDEFINED
1299 		&& pipe_ctx->stream->timing.display_color_depth != requestColorDepth)
1300 		|| (requestPixelEncoding != PIXEL_ENCODING_UNDEFINED
1301 		&& pipe_ctx->stream->timing.pixel_encoding != requestPixelEncoding)) {
1302 		drm_dbg(dev,
1303 			"original bpc %d pix encoding %d, changing to %d  %d\n",
1304 			pipe_ctx->stream->timing.display_color_depth,
1305 			pipe_ctx->stream->timing.pixel_encoding,
1306 			requestColorDepth,
1307 			requestPixelEncoding);
1308 		pipe_ctx->stream->timing.display_color_depth = requestColorDepth;
1309 		pipe_ctx->stream->timing.pixel_encoding = requestPixelEncoding;
1310 
1311 		dc_link_update_dsc_config(pipe_ctx);
1312 
1313 		aconnector->timing_changed = true;
1314 		/* store current timing */
1315 		if (aconnector->timing_requested)
1316 			*aconnector->timing_requested = pipe_ctx->stream->timing;
1317 		else
1318 			drm_err(dev, "timing storage failed\n");
1319 
1320 	}
1321 
1322 	pipe_ctx->stream->test_pattern.type = test_pattern;
1323 	pipe_ctx->stream->test_pattern.color_space = test_pattern_color_space;
1324 
1325 	/* Temp W/A for compliance test failure */
1326 	dc_state->bw_ctx.bw.dcn.clk.p_state_change_support = false;
1327 	dc_state->bw_ctx.bw.dcn.clk.dramclk_khz = clk_mgr->dc_mode_softmax_enabled ?
1328 		clk_mgr->bw_params->dc_mode_softmax_memclk : clk_mgr->bw_params->max_memclk_mhz;
1329 	dc_state->bw_ctx.bw.dcn.clk.idle_dramclk_khz = dc_state->bw_ctx.bw.dcn.clk.dramclk_khz;
1330 	ctx->dc->clk_mgr->funcs->update_clocks(
1331 			ctx->dc->clk_mgr,
1332 			dc_state,
1333 			false);
1334 
1335 	dc_link_dp_set_test_pattern(
1336 		(struct dc_link *) link,
1337 		test_pattern,
1338 		test_pattern_color_space,
1339 		NULL,
1340 		NULL,
1341 		0);
1342 
1343 	return false;
1344 }
1345 
dm_set_phyd32clk(struct dc_context * ctx,int freq_khz)1346 void dm_set_phyd32clk(struct dc_context *ctx, int freq_khz)
1347 {
1348        // TODO
1349 }
1350 
dm_helpers_enable_periodic_detection(struct dc_context * ctx,bool enable)1351 void dm_helpers_enable_periodic_detection(struct dc_context *ctx, bool enable)
1352 {
1353 	struct amdgpu_device *adev = ctx->driver_context;
1354 
1355 	if (adev->dm.idle_workqueue) {
1356 		adev->dm.idle_workqueue->enable = enable;
1357 		if (enable && !adev->dm.idle_workqueue->running && amdgpu_dm_is_headless(adev))
1358 			schedule_work(&adev->dm.idle_workqueue->work);
1359 	}
1360 }
1361 
dm_helpers_dp_mst_update_branch_bandwidth(struct dc_context * ctx,struct dc_link * link)1362 void dm_helpers_dp_mst_update_branch_bandwidth(
1363 		struct dc_context *ctx,
1364 		struct dc_link *link)
1365 {
1366 	// TODO
1367 }
1368 
dm_is_freesync_pcon_whitelist(const uint32_t branch_dev_id)1369 static bool dm_is_freesync_pcon_whitelist(const uint32_t branch_dev_id)
1370 {
1371 	bool ret_val = false;
1372 
1373 	switch (branch_dev_id) {
1374 	case DP_BRANCH_DEVICE_ID_0060AD:
1375 	case DP_BRANCH_DEVICE_ID_00E04C:
1376 	case DP_BRANCH_DEVICE_ID_90CC24:
1377 		ret_val = true;
1378 		break;
1379 	default:
1380 		break;
1381 	}
1382 
1383 	return ret_val;
1384 }
1385 
dm_get_adaptive_sync_support_type(struct dc_link * link)1386 enum adaptive_sync_type dm_get_adaptive_sync_support_type(struct dc_link *link)
1387 {
1388 	struct dpcd_caps *dpcd_caps = &link->dpcd_caps;
1389 	enum adaptive_sync_type as_type = ADAPTIVE_SYNC_TYPE_NONE;
1390 
1391 	switch (dpcd_caps->dongle_type) {
1392 	case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
1393 		if (dpcd_caps->adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT == true &&
1394 			dpcd_caps->allow_invalid_MSA_timing_param == true &&
1395 			dm_is_freesync_pcon_whitelist(dpcd_caps->branch_dev_id))
1396 			as_type = FREESYNC_TYPE_PCON_IN_WHITELIST;
1397 		break;
1398 	default:
1399 		break;
1400 	}
1401 
1402 	return as_type;
1403 }
1404 
dm_helpers_is_fullscreen(struct dc_context * ctx,struct dc_stream_state * stream)1405 bool dm_helpers_is_fullscreen(struct dc_context *ctx, struct dc_stream_state *stream)
1406 {
1407 	// TODO
1408 	return false;
1409 }
1410 
dm_helpers_is_hdr_on(struct dc_context * ctx,struct dc_stream_state * stream)1411 bool dm_helpers_is_hdr_on(struct dc_context *ctx, struct dc_stream_state *stream)
1412 {
1413 	// TODO
1414 	return false;
1415 }
1416