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