xref: /linux/drivers/gpu/drm/amd/display/dc/link/protocols/link_edp_panel_control.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 /*
2  * Copyright 2022 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 /* FILE POLICY AND INTENDED USAGE:
27  * This file implements retrieval and configuration of eDP panel features such
28  * as PSR and ABM and it also manages specs defined eDP panel power sequences.
29  */
30 
31 #include "link_edp_panel_control.h"
32 #include "link_dpcd.h"
33 #include "link_dp_capability.h"
34 #include "dm_helpers.h"
35 #include "dal_asic_id.h"
36 #include "link_dp_phy.h"
37 #include "dce/dmub_psr.h"
38 #include "dc/dc_dmub_srv.h"
39 #include "dce/dmub_replay.h"
40 #include "abm.h"
41 #include "resource.h"
42 #define DC_LOGGER \
43 	link->ctx->logger
44 #define DC_LOGGER_INIT(logger)
45 
46 #define DP_SINK_PR_ENABLE_AND_CONFIGURATION		0x37B
47 
48 /* Travis */
49 static const uint8_t DP_VGA_LVDS_CONVERTER_ID_2[] = "sivarT";
50 /* Nutmeg */
51 static const uint8_t DP_VGA_LVDS_CONVERTER_ID_3[] = "dnomlA";
52 
dp_set_panel_mode(struct dc_link * link,enum dp_panel_mode panel_mode)53 void dp_set_panel_mode(struct dc_link *link, enum dp_panel_mode panel_mode)
54 {
55 	union dpcd_edp_config edp_config_set;
56 	bool panel_mode_edp = false;
57 	enum dc_status result;
58 
59 	memset(&edp_config_set, '\0', sizeof(union dpcd_edp_config));
60 
61 	switch (panel_mode) {
62 	case DP_PANEL_MODE_EDP:
63 	case DP_PANEL_MODE_SPECIAL:
64 		panel_mode_edp = true;
65 		break;
66 
67 	default:
68 		break;
69 	}
70 
71 	/*set edp panel mode in receiver*/
72 	result = core_link_read_dpcd(
73 		link,
74 		DP_EDP_CONFIGURATION_SET,
75 		&edp_config_set.raw,
76 		sizeof(edp_config_set.raw));
77 
78 	if (result == DC_OK &&
79 		edp_config_set.bits.PANEL_MODE_EDP
80 		!= panel_mode_edp) {
81 
82 		edp_config_set.bits.PANEL_MODE_EDP =
83 		panel_mode_edp;
84 		result = core_link_write_dpcd(
85 			link,
86 			DP_EDP_CONFIGURATION_SET,
87 			&edp_config_set.raw,
88 			sizeof(edp_config_set.raw));
89 
90 		ASSERT(result == DC_OK);
91 	}
92 
93 	link->panel_mode = panel_mode;
94 	DC_LOG_DETECTION_DP_CAPS("Link: %d eDP panel mode supported: %d "
95 		 "eDP panel mode enabled: %d \n",
96 		 link->link_index,
97 		 link->dpcd_caps.panel_mode_edp,
98 		 panel_mode_edp);
99 }
100 
dp_get_panel_mode(struct dc_link * link)101 enum dp_panel_mode dp_get_panel_mode(struct dc_link *link)
102 {
103 	/* We need to explicitly check that connector
104 	 * is not DP. Some Travis_VGA get reported
105 	 * by video bios as DP.
106 	 */
107 	if (link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT) {
108 
109 		switch (link->dpcd_caps.branch_dev_id) {
110 		case DP_BRANCH_DEVICE_ID_0022B9:
111 			/* alternate scrambler reset is required for Travis
112 			 * for the case when external chip does not
113 			 * provide sink device id, alternate scrambler
114 			 * scheme will  be overriden later by querying
115 			 * Encoder features
116 			 */
117 			if (strncmp(
118 				link->dpcd_caps.branch_dev_name,
119 				DP_VGA_LVDS_CONVERTER_ID_2,
120 				sizeof(
121 				link->dpcd_caps.
122 				branch_dev_name)) == 0) {
123 					return DP_PANEL_MODE_SPECIAL;
124 			}
125 			break;
126 		case DP_BRANCH_DEVICE_ID_00001A:
127 			/* alternate scrambler reset is required for Travis
128 			 * for the case when external chip does not provide
129 			 * sink device id, alternate scrambler scheme will
130 			 * be overriden later by querying Encoder feature
131 			 */
132 			if (strncmp(link->dpcd_caps.branch_dev_name,
133 				DP_VGA_LVDS_CONVERTER_ID_3,
134 				sizeof(
135 				link->dpcd_caps.
136 				branch_dev_name)) == 0) {
137 					return DP_PANEL_MODE_SPECIAL;
138 			}
139 			break;
140 		default:
141 			break;
142 		}
143 	}
144 
145 	if (link->dpcd_caps.panel_mode_edp &&
146 		(link->connector_signal == SIGNAL_TYPE_EDP ||
147 		 (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
148 		  link->is_internal_display))) {
149 		return DP_PANEL_MODE_EDP;
150 	}
151 
152 	return DP_PANEL_MODE_DEFAULT;
153 }
154 
edp_set_backlight_level_nits(struct dc_link * link,bool isHDR,uint32_t backlight_millinits,uint32_t transition_time_in_ms)155 bool edp_set_backlight_level_nits(struct dc_link *link,
156 		bool isHDR,
157 		uint32_t backlight_millinits,
158 		uint32_t transition_time_in_ms)
159 {
160 	if (!link || (link->connector_signal != SIGNAL_TYPE_EDP &&
161 			link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT))
162 		return false;
163 
164 	if (link->is_dds && !link->dpcd_caps.panel_luminance_control)
165 		return true;
166 
167 	// use internal backlight control if dmub capabilities are not present
168 	if (link->backlight_control_type == BACKLIGHT_CONTROL_VESA_AUX &&
169 		!link->dc->caps.dmub_caps.aux_backlight_support) {
170 		uint8_t backlight_enable = 0;
171 		struct target_luminance_value *target_luminance = NULL;
172 
173 		//if target luminance value is greater than 24 bits, clip the value to 24 bits
174 		if (backlight_millinits > 0xFFFFFF)
175 			backlight_millinits = 0xFFFFFF;
176 
177 		target_luminance = (struct target_luminance_value *)&backlight_millinits;
178 
179 		//make sure we disable AMD ABC first.
180 		core_link_read_dpcd(link, DP_SOURCE_BACKLIGHT_CONTROL,
181 			&backlight_enable, sizeof(uint8_t));
182 		if (backlight_enable) {
183 			backlight_enable = 0;
184 			core_link_write_dpcd(link, DP_SOURCE_BACKLIGHT_CONTROL,
185 					&backlight_enable, 1);
186 		}
187 
188 		core_link_read_dpcd(link, DP_EDP_BACKLIGHT_MODE_SET_REGISTER,
189 			&backlight_enable, sizeof(uint8_t));
190 
191 		backlight_enable |= DP_EDP_PANEL_LUMINANCE_CONTROL_ENABLE;
192 
193 		if (core_link_write_dpcd(link, DP_EDP_BACKLIGHT_MODE_SET_REGISTER,
194 			&backlight_enable,
195 			sizeof(backlight_enable)) != DC_OK)
196 			return false;
197 
198 		if (core_link_write_dpcd(link, DP_EDP_PANEL_TARGET_LUMINANCE_VALUE,
199 			(uint8_t *)(target_luminance),
200 			sizeof(struct target_luminance_value)) != DC_OK)
201 			return false;
202 	} else if (link->backlight_control_type == BACKLIGHT_CONTROL_AMD_AUX) {
203 		struct dpcd_source_backlight_set dpcd_backlight_set;
204 		*(uint32_t *)&dpcd_backlight_set.backlight_level_millinits = backlight_millinits;
205 		*(uint16_t *)&dpcd_backlight_set.backlight_transition_time_ms = (uint16_t)transition_time_in_ms;
206 
207 		uint8_t backlight_control = isHDR ? 1 : 0;
208 		uint8_t backlight_enable = 0;
209 
210 		// OLEDs have no PWM, they can only use AUX
211 		if (link->dpcd_sink_ext_caps.bits.oled == 1)
212 			backlight_control = 1;
213 
214 		//make sure we disable VESA ABC first.
215 		core_link_read_dpcd(link, DP_EDP_BACKLIGHT_MODE_SET_REGISTER,
216 			&backlight_enable, sizeof(uint8_t));
217 
218 		if (backlight_enable & DP_EDP_PANEL_LUMINANCE_CONTROL_ENABLE) {
219 			backlight_enable &= ~DP_EDP_PANEL_LUMINANCE_CONTROL_ENABLE;
220 			core_link_write_dpcd(link, DP_EDP_BACKLIGHT_MODE_SET_REGISTER,
221 					&backlight_enable, sizeof(backlight_enable));
222 		}
223 
224 		if (core_link_write_dpcd(link, DP_SOURCE_BACKLIGHT_LEVEL,
225 			(uint8_t *)(&dpcd_backlight_set),
226 			sizeof(dpcd_backlight_set)) != DC_OK)
227 			return false;
228 
229 		if (core_link_write_dpcd(link, DP_SOURCE_BACKLIGHT_CONTROL,
230 			&backlight_control, 1) != DC_OK)
231 			return false;
232 	}
233 
234 	return true;
235 }
236 
edp_get_backlight_level_nits(struct dc_link * link,uint32_t * backlight_millinits_avg,uint32_t * backlight_millinits_peak)237 bool edp_get_backlight_level_nits(struct dc_link *link,
238 		uint32_t *backlight_millinits_avg,
239 		uint32_t *backlight_millinits_peak)
240 {
241 	union dpcd_source_backlight_get dpcd_backlight_get;
242 
243 	memset(&dpcd_backlight_get, 0, sizeof(union dpcd_source_backlight_get));
244 
245 	if (!link || (link->connector_signal != SIGNAL_TYPE_EDP &&
246 			link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT))
247 		return false;
248 
249 	if (link->is_dds)
250 		return false;
251 	if (!core_link_read_dpcd(link, DP_SOURCE_BACKLIGHT_CURRENT_PEAK,
252 			dpcd_backlight_get.raw,
253 			sizeof(union dpcd_source_backlight_get)))
254 		return false;
255 
256 	*backlight_millinits_avg =
257 		dpcd_backlight_get.bytes.backlight_millinits_avg;
258 	*backlight_millinits_peak =
259 		dpcd_backlight_get.bytes.backlight_millinits_peak;
260 
261 	/* On non-supported panels dpcd_read usually succeeds with 0 returned */
262 	if (*backlight_millinits_avg == 0 ||
263 			*backlight_millinits_avg > *backlight_millinits_peak)
264 		return false;
265 
266 	return true;
267 }
268 
edp_backlight_enable_aux(struct dc_link * link,bool enable)269 bool edp_backlight_enable_aux(struct dc_link *link, bool enable)
270 {
271 	uint8_t backlight_enable = enable ? 1 : 0;
272 
273 	if (!link || (link->connector_signal != SIGNAL_TYPE_EDP &&
274 		link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT))
275 		return false;
276 
277 	if (link->is_dds)
278 		return true;
279 	if (core_link_write_dpcd(link, DP_SOURCE_BACKLIGHT_ENABLE,
280 		&backlight_enable, 1) != DC_OK)
281 		return false;
282 
283 	return true;
284 }
285 
286 // we read default from 0x320 because we expect BIOS wrote it there
287 // regular get_backlight_nit reads from panel set at 0x326
read_default_bl_aux(struct dc_link * link,uint32_t * backlight_millinits)288 static bool read_default_bl_aux(struct dc_link *link, uint32_t *backlight_millinits)
289 {
290 	if (!link || (link->connector_signal != SIGNAL_TYPE_EDP &&
291 		link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT))
292 		return false;
293 
294 	if (!link->dpcd_caps.panel_luminance_control) {
295 		if (!core_link_read_dpcd(link, DP_SOURCE_BACKLIGHT_LEVEL,
296 			(uint8_t *)backlight_millinits,
297 			sizeof(uint32_t)))
298 			return false;
299 	} else {
300 		//setting to 0 as a precaution, since target_luminance_value is 3 bytes
301 		memset(backlight_millinits, 0, sizeof(uint32_t));
302 
303 		if (!core_link_read_dpcd(link, DP_EDP_PANEL_TARGET_LUMINANCE_VALUE,
304 			(uint8_t *)backlight_millinits,
305 			sizeof(struct target_luminance_value)))
306 			return false;
307 	}
308 
309 	return true;
310 }
311 
set_default_brightness_aux(struct dc_link * link)312 bool set_default_brightness_aux(struct dc_link *link)
313 {
314 	uint32_t default_backlight;
315 
316 	if (link && link->dpcd_sink_ext_caps.bits.oled == 1) {
317 		if (!read_default_bl_aux(link, &default_backlight))
318 			default_backlight = 150000;
319 		// if > 5000, it might be wrong readback. 0 nits is a valid default value for OLED panel.
320 		if (default_backlight < 1000 || default_backlight > 5000000)
321 			default_backlight = 150000;
322 
323 		return edp_set_backlight_level_nits(link, true,
324 				default_backlight, 0);
325 	}
326 	return false;
327 }
328 
edp_is_ilr_optimization_enabled(struct dc_link * link)329 bool edp_is_ilr_optimization_enabled(struct dc_link *link)
330 {
331 	if (link->dpcd_caps.edp_supported_link_rates_count == 0 || !link->panel_config.ilr.optimize_edp_link_rate)
332 		return false;
333 	return true;
334 }
335 
get_max_edp_link_rate(struct dc_link * link)336 enum dc_link_rate get_max_edp_link_rate(struct dc_link *link)
337 {
338 	enum dc_link_rate max_ilr_rate = LINK_RATE_UNKNOWN;
339 	enum dc_link_rate max_non_ilr_rate = dp_get_max_link_cap(link).link_rate;
340 
341 	for (int i = 0; i < link->dpcd_caps.edp_supported_link_rates_count; i++) {
342 		if (max_ilr_rate < link->dpcd_caps.edp_supported_link_rates[i])
343 			max_ilr_rate = link->dpcd_caps.edp_supported_link_rates[i];
344 	}
345 
346 	return (max_ilr_rate > max_non_ilr_rate ? max_ilr_rate : max_non_ilr_rate);
347 }
348 
edp_is_ilr_optimization_required(struct dc_link * link,struct dc_crtc_timing * crtc_timing)349 bool edp_is_ilr_optimization_required(struct dc_link *link,
350 		struct dc_crtc_timing *crtc_timing)
351 {
352 	struct dc_link_settings link_setting;
353 	uint8_t link_bw_set = 0;
354 	uint8_t link_rate_set = 0;
355 	uint32_t req_bw;
356 	union lane_count_set lane_count_set = {0};
357 
358 	ASSERT(link || crtc_timing); // invalid input
359 
360 	if (!edp_is_ilr_optimization_enabled(link))
361 		return false;
362 
363 
364 	// Read DPCD 00100h to find if standard link rates are set
365 	core_link_read_dpcd(link, DP_LINK_BW_SET,
366 				&link_bw_set, sizeof(link_bw_set));
367 
368 	if (link_bw_set) {
369 		DC_LOG_EVENT_LINK_TRAINING("eDP ILR: Optimization required, VBIOS used link_bw_set\n");
370 		return true;
371 	}
372 
373 	// Read DPCD 00115h to find the edp link rate set used
374 	core_link_read_dpcd(link, DP_LINK_RATE_SET,
375 			    &link_rate_set, sizeof(link_rate_set));
376 
377 	// Read DPCD 00101h to find out the number of lanes currently set
378 	core_link_read_dpcd(link, DP_LANE_COUNT_SET,
379 				&lane_count_set.raw, sizeof(lane_count_set));
380 
381 	req_bw = dc_bandwidth_in_kbps_from_timing(crtc_timing, dc_link_get_highest_encoding_format(link));
382 
383 	if (!crtc_timing->flags.DSC)
384 		edp_decide_link_settings(link, &link_setting, req_bw);
385 	else
386 		decide_edp_link_settings_with_dsc(link, &link_setting, req_bw, LINK_RATE_UNKNOWN);
387 
388 	if (link->dpcd_caps.edp_supported_link_rates[link_rate_set] != link_setting.link_rate ||
389 			lane_count_set.bits.LANE_COUNT_SET != link_setting.lane_count) {
390 		DC_LOG_EVENT_LINK_TRAINING("eDP ILR: Optimization required, VBIOS link_rate_set not optimal\n");
391 		return true;
392 	}
393 
394 	DC_LOG_EVENT_LINK_TRAINING("eDP ILR: No optimization required, VBIOS set optimal link_rate_set\n");
395 	return false;
396 }
397 
edp_panel_backlight_power_on(struct dc_link * link,bool wait_for_hpd)398 void edp_panel_backlight_power_on(struct dc_link *link, bool wait_for_hpd)
399 {
400 	if (link->connector_signal != SIGNAL_TYPE_EDP)
401 		return;
402 
403 	link->dc->hwss.edp_power_control(link, true);
404 	if (wait_for_hpd)
405 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
406 	if (link->dc->hwss.edp_backlight_control)
407 		link->dc->hwss.edp_backlight_control(link, true);
408 }
409 
edp_set_panel_power(struct dc_link * link,bool powerOn)410 void edp_set_panel_power(struct dc_link *link, bool powerOn)
411 {
412 	if (powerOn) {
413 		// 1. panel VDD on
414 		if (!link->dc->config.edp_no_power_sequencing)
415 			link->dc->hwss.edp_power_control(link, true);
416 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
417 
418 		// 2. panel BL on
419 		if (link->dc->hwss.edp_backlight_control)
420 			link->dc->hwss.edp_backlight_control(link, true);
421 
422 		// 3. Rx power on
423 		dpcd_write_rx_power_ctrl(link, true);
424 	} else {
425 		// 3. Rx power off
426 		dpcd_write_rx_power_ctrl(link, false);
427 
428 		// 2. panel BL off
429 		if (link->dc->hwss.edp_backlight_control)
430 			link->dc->hwss.edp_backlight_control(link, false);
431 
432 		// 1. panel VDD off
433 		if (!link->dc->config.edp_no_power_sequencing)
434 			link->dc->hwss.edp_power_control(link, false);
435 	}
436 }
437 
edp_wait_for_t12(struct dc_link * link)438 bool edp_wait_for_t12(struct dc_link *link)
439 {
440 	if (link->connector_signal == SIGNAL_TYPE_EDP && link->dc->hwss.edp_wait_for_T12) {
441 		link->dc->hwss.edp_wait_for_T12(link);
442 
443 		return true;
444 	}
445 
446 	return false;
447 }
448 
edp_add_delay_for_T9(struct dc_link * link)449 void edp_add_delay_for_T9(struct dc_link *link)
450 {
451 	if (link && link->panel_config.pps.extra_delay_backlight_off > 0)
452 		fsleep(link->panel_config.pps.extra_delay_backlight_off * 1000);
453 }
454 
edp_receiver_ready_T9(struct dc_link * link)455 bool edp_receiver_ready_T9(struct dc_link *link)
456 {
457 	unsigned int tries = 0;
458 	unsigned char sinkstatus = 0;
459 	unsigned char edpRev = 0;
460 	enum dc_status result = DC_OK;
461 
462 	result = core_link_read_dpcd(link, DP_EDP_DPCD_REV, &edpRev, sizeof(edpRev));
463 
464 	/* start from eDP version 1.2, SINK_STAUS indicate the sink is ready.*/
465 	if (result == DC_OK && edpRev >= DP_EDP_12) {
466 		do {
467 			sinkstatus = 1;
468 			result = core_link_read_dpcd(link, DP_SINK_STATUS, &sinkstatus, sizeof(sinkstatus));
469 			if (sinkstatus == 0)
470 				break;
471 			if (result != DC_OK)
472 				break;
473 			udelay(100); //MAx T9
474 		} while (++tries < 50);
475 	}
476 
477 	return result;
478 }
479 
edp_receiver_ready_T7(struct dc_link * link)480 bool edp_receiver_ready_T7(struct dc_link *link)
481 {
482 	unsigned char sinkstatus = 0;
483 	unsigned char edpRev = 0;
484 	enum dc_status result = DC_OK;
485 
486 	/* use absolute time stamp to constrain max T7*/
487 	unsigned long long enter_timestamp = 0;
488 	unsigned long long finish_timestamp = 0;
489 	unsigned long long time_taken_in_ns = 0;
490 
491 	result = core_link_read_dpcd(link, DP_EDP_DPCD_REV, &edpRev, sizeof(edpRev));
492 
493 	if (result == DC_OK && edpRev >= DP_EDP_12) {
494 		/* start from eDP version 1.2, SINK_STAUS indicate the sink is ready.*/
495 		enter_timestamp = dm_get_timestamp(link->ctx);
496 		do {
497 			sinkstatus = 0;
498 			result = core_link_read_dpcd(link, DP_SINK_STATUS, &sinkstatus, sizeof(sinkstatus));
499 			if (sinkstatus == 1)
500 				break;
501 			if (result != DC_OK)
502 				break;
503 			udelay(25);
504 			finish_timestamp = dm_get_timestamp(link->ctx);
505 			time_taken_in_ns = dm_get_elapse_time_in_ns(link->ctx, finish_timestamp, enter_timestamp);
506 		} while (time_taken_in_ns < 50 * 1000000); //MAx T7 is 50ms
507 	}
508 
509 	if (link && link->panel_config.pps.extra_t7_ms > 0)
510 		fsleep(link->panel_config.pps.extra_t7_ms * 1000);
511 
512 	return result;
513 }
514 
edp_power_alpm_dpcd_enable(struct dc_link * link,bool enable)515 bool edp_power_alpm_dpcd_enable(struct dc_link *link, bool enable)
516 {
517 	bool ret = false;
518 	union dpcd_alpm_configuration alpm_config;
519 
520 	if (link->psr_settings.psr_version == DC_PSR_VERSION_SU_1) {
521 		memset(&alpm_config, 0, sizeof(alpm_config));
522 
523 		alpm_config.bits.ENABLE = (enable ? true : false);
524 		ret = dm_helpers_dp_write_dpcd(link->ctx, link,
525 				DP_RECEIVER_ALPM_CONFIG, &alpm_config.raw,
526 				sizeof(alpm_config.raw));
527 	}
528 	return ret;
529 }
530 
get_pipe_from_link(const struct dc_link * link)531 static struct pipe_ctx *get_pipe_from_link(const struct dc_link *link)
532 {
533 	int i;
534 	struct dc *dc = link->ctx->dc;
535 	struct pipe_ctx *pipe_ctx = NULL;
536 
537 	for (i = 0; i < MAX_PIPES; i++) {
538 		if (dc->current_state->res_ctx.pipe_ctx[i].stream) {
539 			if (dc->current_state->res_ctx.pipe_ctx[i].stream->link == link) {
540 				pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
541 				break;
542 			}
543 		}
544 	}
545 
546 	return pipe_ctx;
547 }
548 
edp_set_backlight_level(const struct dc_link * link,struct set_backlight_level_params * backlight_level_params)549 bool edp_set_backlight_level(const struct dc_link *link,
550 		struct set_backlight_level_params *backlight_level_params)
551 {
552 	struct dc  *dc = link->ctx->dc;
553 	uint32_t backlight_pwm_u16_16 = backlight_level_params->backlight_pwm_u16_16;
554 	uint32_t frame_ramp = backlight_level_params->frame_ramp;
555 
556 	DC_LOG_BACKLIGHT("New Backlight level: %d (0x%X)\n",
557 			backlight_pwm_u16_16, backlight_pwm_u16_16);
558 
559 	if (dc_is_embedded_signal(link->connector_signal)) {
560 		struct pipe_ctx *pipe_ctx = get_pipe_from_link(link);
561 
562 		if (link->panel_cntl)
563 			link->panel_cntl->stored_backlight_registers.USER_LEVEL = backlight_pwm_u16_16;
564 
565 		if (pipe_ctx) {
566 			/* Disable brightness ramping when the display is blanked
567 			 * as it can hang the DMCU
568 			 */
569 			if (pipe_ctx->plane_state == NULL)
570 				frame_ramp = 0;
571 		} else {
572 			return false;
573 		}
574 
575 		backlight_level_params->frame_ramp = frame_ramp;
576 
577 		dc->hwss.set_backlight_level(
578 				pipe_ctx,
579 				backlight_level_params);
580 	}
581 	return true;
582 }
583 
edp_set_psr_allow_active(struct dc_link * link,const bool * allow_active,bool wait,bool force_static,const unsigned int * power_opts)584 bool edp_set_psr_allow_active(struct dc_link *link, const bool *allow_active,
585 		bool wait, bool force_static, const unsigned int *power_opts)
586 {
587 	struct dc  *dc = link->ctx->dc;
588 	struct dmcu *dmcu = dc->res_pool->dmcu;
589 	struct dmub_psr *psr = dc->res_pool->psr;
590 	unsigned int panel_inst;
591 
592 	if (psr == NULL && force_static)
593 		return false;
594 
595 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
596 		return false;
597 
598 	if ((allow_active != NULL) && (*allow_active == true) && (link->type == dc_connection_none)) {
599 		// Don't enter PSR if panel is not connected
600 		return false;
601 	}
602 
603 	/* Set power optimization flag */
604 	if (power_opts && link->psr_settings.psr_power_opt != *power_opts) {
605 		link->psr_settings.psr_power_opt = *power_opts;
606 
607 		if (psr != NULL && link->psr_settings.psr_feature_enabled && psr->funcs->psr_set_power_opt)
608 			psr->funcs->psr_set_power_opt(psr, link->psr_settings.psr_power_opt, panel_inst);
609 	}
610 
611 	if (psr != NULL && link->psr_settings.psr_feature_enabled &&
612 			force_static && psr->funcs->psr_force_static)
613 		psr->funcs->psr_force_static(psr, panel_inst);
614 
615 	/* Enable or Disable PSR */
616 	if (allow_active && link->psr_settings.psr_allow_active != *allow_active) {
617 		link->psr_settings.psr_allow_active = *allow_active;
618 
619 		if (!link->psr_settings.psr_allow_active)
620 			dc_z10_restore(dc);
621 
622 		if (psr != NULL && link->psr_settings.psr_feature_enabled) {
623 			psr->funcs->psr_enable(psr, link->psr_settings.psr_allow_active, wait, panel_inst);
624 		} else if ((dmcu != NULL && dmcu->funcs->is_dmcu_initialized(dmcu)) &&
625 			link->psr_settings.psr_feature_enabled)
626 			dmcu->funcs->set_psr_enable(dmcu, link->psr_settings.psr_allow_active, wait);
627 		else
628 			return false;
629 	}
630 	return true;
631 }
632 
edp_get_psr_state(const struct dc_link * link,enum dc_psr_state * state)633 bool edp_get_psr_state(const struct dc_link *link, enum dc_psr_state *state)
634 {
635 	struct dc  *dc = link->ctx->dc;
636 	struct dmcu *dmcu = dc->res_pool->dmcu;
637 	struct dmub_psr *psr = dc->res_pool->psr;
638 	unsigned int panel_inst;
639 
640 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
641 		return false;
642 
643 	if (psr != NULL && link->psr_settings.psr_feature_enabled)
644 		psr->funcs->psr_get_state(psr, state, panel_inst);
645 	else if (dmcu != NULL && link->psr_settings.psr_feature_enabled)
646 		dmcu->funcs->get_psr_state(dmcu, state);
647 
648 	return true;
649 }
650 
651 static inline enum physical_phy_id
transmitter_to_phy_id(struct dc_link * link)652 transmitter_to_phy_id(struct dc_link *link)
653 {
654 	struct dc_context *dc_ctx = link->ctx;
655 	enum transmitter transmitter_value = link->link_enc->transmitter;
656 
657 	switch (transmitter_value) {
658 	case TRANSMITTER_UNIPHY_A:
659 		return PHYLD_0;
660 	case TRANSMITTER_UNIPHY_B:
661 		return PHYLD_1;
662 	case TRANSMITTER_UNIPHY_C:
663 		return PHYLD_2;
664 	case TRANSMITTER_UNIPHY_D:
665 		return PHYLD_3;
666 	case TRANSMITTER_UNIPHY_E:
667 		return PHYLD_4;
668 	case TRANSMITTER_UNIPHY_F:
669 		return PHYLD_5;
670 	case TRANSMITTER_NUTMEG_CRT:
671 		return PHYLD_6;
672 	case TRANSMITTER_TRAVIS_CRT:
673 		return PHYLD_7;
674 	case TRANSMITTER_TRAVIS_LCD:
675 		return PHYLD_8;
676 	case TRANSMITTER_UNIPHY_G:
677 		return PHYLD_9;
678 	case TRANSMITTER_COUNT:
679 		return PHYLD_COUNT;
680 	case TRANSMITTER_UNKNOWN:
681 		return PHYLD_UNKNOWN;
682 	default:
683 		DC_ERROR("Unknown transmitter value %d\n", transmitter_value);
684 		return PHYLD_UNKNOWN;
685 	}
686 }
687 
edp_setup_psr(struct dc_link * link,const struct dc_stream_state * stream,struct psr_config * psr_config,struct psr_context * psr_context)688 bool edp_setup_psr(struct dc_link *link,
689 		const struct dc_stream_state *stream, struct psr_config *psr_config,
690 		struct psr_context *psr_context)
691 {
692 	struct dc *dc;
693 	struct dmcu *dmcu;
694 	struct dmub_psr *psr;
695 	int i;
696 	unsigned int panel_inst;
697 	/* updateSinkPsrDpcdConfig*/
698 	union dpcd_psr_configuration psr_configuration;
699 	union dpcd_sink_active_vtotal_control_mode vtotal_control = {0};
700 
701 	psr_context->controllerId = CONTROLLER_ID_UNDEFINED;
702 
703 	if (!link)
704 		return false;
705 
706 	//Clear PSR cfg
707 	memset(&psr_configuration, 0, sizeof(psr_configuration));
708 	dm_helpers_dp_write_dpcd(
709 		link->ctx,
710 		link,
711 		DP_PSR_EN_CFG,
712 		&psr_configuration.raw,
713 		sizeof(psr_configuration.raw));
714 
715 	if (link->psr_settings.psr_version == DC_PSR_VERSION_UNSUPPORTED)
716 		return false;
717 
718 	dc = link->ctx->dc;
719 	dmcu = dc->res_pool->dmcu;
720 	psr = dc->res_pool->psr;
721 
722 	if (!dmcu && !psr)
723 		return false;
724 
725 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
726 		return false;
727 
728 	psr_configuration.bits.ENABLE                    = 1;
729 	psr_configuration.bits.CRC_VERIFICATION          = 1;
730 	psr_configuration.bits.FRAME_CAPTURE_INDICATION  =
731 			psr_config->psr_frame_capture_indication_req;
732 
733 	/* Check for PSR v2*/
734 	if (link->psr_settings.psr_version == DC_PSR_VERSION_SU_1) {
735 		/* For PSR v2 selective update.
736 		 * Indicates whether sink should start capturing
737 		 * immediately following active scan line,
738 		 * or starting with the 2nd active scan line.
739 		 */
740 		psr_configuration.bits.LINE_CAPTURE_INDICATION = 0;
741 		/*For PSR v2, determines whether Sink should generate
742 		 * IRQ_HPD when CRC mismatch is detected.
743 		 */
744 		psr_configuration.bits.IRQ_HPD_WITH_CRC_ERROR    = 1;
745 		/* For PSR v2, set the bit when the Source device will
746 		 * be enabling PSR2 operation.
747 		 */
748 		psr_configuration.bits.ENABLE_PSR2    = 1;
749 		/* For PSR v2, the Sink device must be able to receive
750 		 * SU region updates early in the frame time.
751 		 */
752 		psr_configuration.bits.EARLY_TRANSPORT_ENABLE    = 1;
753 	}
754 
755 	dm_helpers_dp_write_dpcd(
756 		link->ctx,
757 		link,
758 		368,
759 		&psr_configuration.raw,
760 		sizeof(psr_configuration.raw));
761 
762 	if (link->psr_settings.psr_version == DC_PSR_VERSION_SU_1) {
763 		edp_power_alpm_dpcd_enable(link, true);
764 		psr_context->su_granularity_required =
765 			psr_config->su_granularity_required;
766 		psr_context->su_y_granularity =
767 			psr_config->su_y_granularity;
768 		psr_context->line_time_in_us = psr_config->line_time_in_us;
769 
770 		/* linux must be able to expose AMD Source DPCD definition
771 		 * in order to support FreeSync PSR
772 		 */
773 		if (link->psr_settings.psr_vtotal_control_support) {
774 			psr_context->rate_control_caps = psr_config->rate_control_caps;
775 			vtotal_control.bits.ENABLE = true;
776 			core_link_write_dpcd(link, DP_SINK_PSR_ACTIVE_VTOTAL_CONTROL_MODE,
777 							&vtotal_control.raw, sizeof(vtotal_control.raw));
778 		}
779 	}
780 
781 	psr_context->channel = link->ddc->ddc_pin->hw_info.ddc_channel;
782 	psr_context->transmitterId = link->link_enc->transmitter;
783 	psr_context->engineId = link->link_enc->preferred_engine;
784 
785 	for (i = 0; i < MAX_PIPES; i++) {
786 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
787 				== stream) {
788 			/* dmcu -1 for all controller id values,
789 			 * therefore +1 here
790 			 */
791 			psr_context->controllerId =
792 				dc->current_state->res_ctx.
793 				pipe_ctx[i].stream_res.tg->inst + 1;
794 			break;
795 		}
796 	}
797 
798 	/* Hardcoded for now.  Can be Pcie or Uniphy (or Unknown)*/
799 	psr_context->phyType = PHY_TYPE_UNIPHY;
800 	/*PhyId is associated with the transmitter id*/
801 	psr_context->smuPhyId = transmitter_to_phy_id(link);
802 
803 	psr_context->crtcTimingVerticalTotal = stream->timing.v_total;
804 	psr_context->vsync_rate_hz = div64_u64(div64_u64((stream->
805 					timing.pix_clk_100hz * (u64)100),
806 					stream->timing.v_total),
807 					stream->timing.h_total);
808 
809 	psr_context->psrSupportedDisplayConfig = true;
810 	psr_context->psrExitLinkTrainingRequired =
811 		psr_config->psr_exit_link_training_required;
812 	psr_context->sdpTransmitLineNumDeadline =
813 		psr_config->psr_sdp_transmit_line_num_deadline;
814 	psr_context->psrFrameCaptureIndicationReq =
815 		psr_config->psr_frame_capture_indication_req;
816 
817 	psr_context->skipPsrWaitForPllLock = 0; /* only = 1 in KV */
818 
819 	psr_context->numberOfControllers =
820 			link->dc->res_pool->timing_generator_count;
821 
822 	psr_context->rfb_update_auto_en = true;
823 
824 	/* 2 frames before enter PSR. */
825 	psr_context->timehyst_frames = 2;
826 	/* half a frame
827 	 * (units in 100 lines, i.e. a value of 1 represents 100 lines)
828 	 */
829 	psr_context->hyst_lines = stream->timing.v_total / 2 / 100;
830 	psr_context->aux_repeats = 10;
831 
832 	psr_context->psr_level.u32all = 0;
833 
834 	/*skip power down the single pipe since it blocks the cstate*/
835 	if (link->ctx->asic_id.chip_family >= FAMILY_RV) {
836 		switch (link->ctx->asic_id.chip_family) {
837 		case FAMILY_YELLOW_CARP:
838 		case AMDGPU_FAMILY_GC_10_3_6:
839 		case AMDGPU_FAMILY_GC_11_0_1:
840 			if (dc->debug.disable_z10 || dc->debug.psr_skip_crtc_disable)
841 				psr_context->psr_level.bits.SKIP_CRTC_DISABLE = true;
842 			break;
843 		default:
844 			psr_context->psr_level.bits.SKIP_CRTC_DISABLE = true;
845 			break;
846 		}
847 	}
848 
849 	/* SMU will perform additional powerdown sequence.
850 	 * For unsupported ASICs, set psr_level flag to skip PSR
851 	 *  static screen notification to SMU.
852 	 *  (Always set for DAL2, did not check ASIC)
853 	 */
854 	psr_context->allow_smu_optimizations = psr_config->allow_smu_optimizations;
855 	psr_context->allow_multi_disp_optimizations = psr_config->allow_multi_disp_optimizations;
856 
857 	/* Complete PSR entry before aborting to prevent intermittent
858 	 * freezes on certain eDPs
859 	 */
860 	psr_context->psr_level.bits.DISABLE_PSR_ENTRY_ABORT = 1;
861 
862 	/* Disable ALPM first for compatible non-ALPM panel now */
863 	psr_context->psr_level.bits.DISABLE_ALPM = 0;
864 	psr_context->psr_level.bits.ALPM_DEFAULT_PD_MODE = 1;
865 
866 	/* Controls additional delay after remote frame capture before
867 	 * continuing power down, default = 0
868 	 */
869 	psr_context->frame_delay = 0;
870 
871 	psr_context->dsc_slice_height = psr_config->dsc_slice_height;
872 
873 	if (psr) {
874 		link->psr_settings.psr_feature_enabled = psr->funcs->psr_copy_settings(psr,
875 			link, psr_context, panel_inst);
876 		link->psr_settings.psr_power_opt = 0;
877 		link->psr_settings.psr_allow_active = 0;
878 	} else {
879 		link->psr_settings.psr_feature_enabled = dmcu->funcs->setup_psr(dmcu, link, psr_context);
880 	}
881 
882 	/* psr_enabled == 0 indicates setup_psr did not succeed, but this
883 	 * should not happen since firmware should be running at this point
884 	 */
885 	if (link->psr_settings.psr_feature_enabled == 0)
886 		ASSERT(0);
887 
888 	return true;
889 
890 }
891 
edp_get_psr_residency(const struct dc_link * link,uint32_t * residency,enum psr_residency_mode mode)892 void edp_get_psr_residency(const struct dc_link *link, uint32_t *residency, enum psr_residency_mode mode)
893 {
894 	struct dc  *dc = link->ctx->dc;
895 	struct dmub_psr *psr = dc->res_pool->psr;
896 	unsigned int panel_inst;
897 
898 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
899 		return;
900 
901 	// PSR residency measurements only supported on DMCUB
902 	if (psr != NULL && link->psr_settings.psr_feature_enabled)
903 		psr->funcs->psr_get_residency(psr, residency, panel_inst, mode);
904 	else
905 		*residency = 0;
906 }
edp_set_sink_vtotal_in_psr_active(const struct dc_link * link,uint16_t psr_vtotal_idle,uint16_t psr_vtotal_su)907 bool edp_set_sink_vtotal_in_psr_active(const struct dc_link *link, uint16_t psr_vtotal_idle, uint16_t psr_vtotal_su)
908 {
909 	struct dc *dc = link->ctx->dc;
910 	struct dmub_psr *psr = dc->res_pool->psr;
911 
912 	if (psr == NULL || !link->psr_settings.psr_feature_enabled || !link->psr_settings.psr_vtotal_control_support)
913 		return false;
914 
915 	psr->funcs->psr_set_sink_vtotal_in_psr_active(psr, psr_vtotal_idle, psr_vtotal_su);
916 
917 	return true;
918 }
919 
edp_set_replay_allow_active(struct dc_link * link,const bool * allow_active,bool wait,bool force_static,const unsigned int * power_opts)920 bool edp_set_replay_allow_active(struct dc_link *link, const bool *allow_active,
921 	bool wait, bool force_static, const unsigned int *power_opts)
922 {
923 	struct dc  *dc = link->ctx->dc;
924 	struct dmub_replay *replay = dc->res_pool->replay;
925 	unsigned int panel_inst;
926 
927 	if (replay == NULL && force_static)
928 		return false;
929 
930 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
931 		return false;
932 
933 	/* Set power optimization flag */
934 	if (power_opts && link->replay_settings.replay_power_opt_active != *power_opts) {
935 		if (replay != NULL && link->replay_settings.replay_feature_enabled &&
936 		    replay->funcs->replay_set_power_opt) {
937 			replay->funcs->replay_set_power_opt(replay, *power_opts, panel_inst);
938 			link->replay_settings.replay_power_opt_active = *power_opts;
939 		}
940 	}
941 
942 	/* Activate or deactivate Replay */
943 	if (allow_active && link->replay_settings.replay_allow_active != *allow_active) {
944 		// TODO: Handle mux change case if force_static is set
945 		// If force_static is set, just change the replay_allow_active state directly
946 		if (replay != NULL && link->replay_settings.replay_feature_enabled)
947 			replay->funcs->replay_enable(replay, *allow_active, wait, panel_inst, link);
948 		link->replay_settings.replay_allow_active = *allow_active;
949 	}
950 
951 	return true;
952 }
953 
edp_get_replay_state(const struct dc_link * link,uint64_t * state)954 bool edp_get_replay_state(const struct dc_link *link, uint64_t *state)
955 {
956 	struct dc  *dc = link->ctx->dc;
957 	struct dmub_replay *replay = dc->res_pool->replay;
958 	unsigned int panel_inst;
959 	enum replay_state pr_state = REPLAY_STATE_0;
960 
961 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
962 		return false;
963 
964 	if (replay != NULL && link->replay_settings.replay_feature_enabled)
965 		replay->funcs->replay_get_state(replay, &pr_state, panel_inst);
966 	*state = pr_state;
967 
968 	return true;
969 }
970 
edp_setup_replay(struct dc_link * link,const struct dc_stream_state * stream)971 bool edp_setup_replay(struct dc_link *link, const struct dc_stream_state *stream)
972 {
973 	/* To-do: Setup Replay */
974 	struct dc *dc;
975 	struct dmub_replay *replay;
976 	int i;
977 	unsigned int panel_inst;
978 	struct replay_context replay_context = { 0 };
979 	unsigned int lineTimeInNs = 0;
980 
981 	union replay_enable_and_configuration replay_config = { 0 };
982 
983 	union dpcd_alpm_configuration alpm_config;
984 
985 	replay_context.controllerId = CONTROLLER_ID_UNDEFINED;
986 
987 	if (!link)
988 		return false;
989 
990 	//Clear Replay config
991 	dm_helpers_dp_write_dpcd(link->ctx, link,
992 		DP_SINK_PR_ENABLE_AND_CONFIGURATION,
993 		(uint8_t *)&(replay_config.raw), sizeof(uint8_t));
994 
995 	if (!(link->replay_settings.config.replay_supported))
996 		return false;
997 
998 	link->replay_settings.config.replay_error_status.raw = 0;
999 
1000 	dc = link->ctx->dc;
1001 
1002 	replay = dc->res_pool->replay;
1003 
1004 	if (!replay)
1005 		return false;
1006 
1007 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
1008 		return false;
1009 
1010 	replay_context.aux_inst = link->ddc->ddc_pin->hw_info.ddc_channel;
1011 	replay_context.digbe_inst = link->link_enc->transmitter;
1012 	replay_context.digfe_inst = link->link_enc->preferred_engine;
1013 
1014 	for (i = 0; i < MAX_PIPES; i++) {
1015 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
1016 				== stream) {
1017 			/* dmcu -1 for all controller id values,
1018 			 * therefore +1 here
1019 			 */
1020 			replay_context.controllerId =
1021 				dc->current_state->res_ctx.pipe_ctx[i].stream_res.tg->inst + 1;
1022 			break;
1023 		}
1024 	}
1025 
1026 	lineTimeInNs =
1027 		((stream->timing.h_total * 1000000) /
1028 			(stream->timing.pix_clk_100hz / 10)) + 1;
1029 
1030 	replay_context.line_time_in_ns = lineTimeInNs;
1031 
1032 	link->replay_settings.replay_feature_enabled =
1033 			replay->funcs->replay_copy_settings(replay, link, &replay_context, panel_inst);
1034 	if (link->replay_settings.replay_feature_enabled) {
1035 
1036 		replay_config.bits.FREESYNC_PANEL_REPLAY_MODE = 1;
1037 		replay_config.bits.TIMING_DESYNC_ERROR_VERIFICATION =
1038 			link->replay_settings.config.replay_timing_sync_supported;
1039 		replay_config.bits.STATE_TRANSITION_ERROR_DETECTION = 1;
1040 		dm_helpers_dp_write_dpcd(link->ctx, link,
1041 			DP_SINK_PR_ENABLE_AND_CONFIGURATION,
1042 			(uint8_t *)&(replay_config.raw), sizeof(uint8_t));
1043 
1044 		memset(&alpm_config, 0, sizeof(alpm_config));
1045 		alpm_config.bits.ENABLE = 1;
1046 		dm_helpers_dp_write_dpcd(
1047 			link->ctx,
1048 			link,
1049 			DP_RECEIVER_ALPM_CONFIG,
1050 			&alpm_config.raw,
1051 			sizeof(alpm_config.raw));
1052 	}
1053 
1054 	link->replay_settings.config.replay_video_conferencing_optimization_enabled = false;
1055 
1056 	return true;
1057 }
1058 
1059 /*
1060  * This is general Interface for Replay to set an 32 bit variable to dmub
1061  * replay_FW_Message_type: Indicates which instruction or variable pass to DMUB
1062  * cmd_data: Value of the config.
1063  */
edp_send_replay_cmd(struct dc_link * link,enum replay_FW_Message_type msg,union dmub_replay_cmd_set * cmd_data)1064 bool edp_send_replay_cmd(struct dc_link *link,
1065 			enum replay_FW_Message_type msg,
1066 			union dmub_replay_cmd_set *cmd_data)
1067 {
1068 	struct dc *dc = link->ctx->dc;
1069 	struct dmub_replay *replay = dc->res_pool->replay;
1070 	unsigned int panel_inst;
1071 
1072 	if (!replay)
1073 		return false;
1074 
1075 	DC_LOGGER_INIT(link->ctx->logger);
1076 
1077 	if (dc_get_edp_link_panel_inst(dc, link, &panel_inst))
1078 		cmd_data->panel_inst = panel_inst;
1079 	else {
1080 		DC_LOG_DC("%s(): get edp panel inst fail ", __func__);
1081 		return false;
1082 	}
1083 
1084 	replay->funcs->replay_send_cmd(replay, msg, cmd_data);
1085 
1086 	return true;
1087 }
1088 
edp_set_coasting_vtotal(struct dc_link * link,uint32_t coasting_vtotal)1089 bool edp_set_coasting_vtotal(struct dc_link *link, uint32_t coasting_vtotal)
1090 {
1091 	struct dc *dc = link->ctx->dc;
1092 	struct dmub_replay *replay = dc->res_pool->replay;
1093 	unsigned int panel_inst;
1094 
1095 	if (!replay)
1096 		return false;
1097 
1098 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
1099 		return false;
1100 
1101 	if (coasting_vtotal && link->replay_settings.coasting_vtotal != coasting_vtotal) {
1102 		replay->funcs->replay_set_coasting_vtotal(replay, coasting_vtotal, panel_inst);
1103 		link->replay_settings.coasting_vtotal = coasting_vtotal;
1104 	}
1105 
1106 	return true;
1107 }
1108 
edp_replay_residency(const struct dc_link * link,unsigned int * residency,const bool is_start,const enum pr_residency_mode mode)1109 bool edp_replay_residency(const struct dc_link *link,
1110 	unsigned int *residency, const bool is_start, const enum pr_residency_mode mode)
1111 {
1112 	struct dc  *dc = link->ctx->dc;
1113 	struct dmub_replay *replay = dc->res_pool->replay;
1114 	unsigned int panel_inst;
1115 
1116 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
1117 		return false;
1118 
1119 	if (!residency)
1120 		return false;
1121 
1122 	if (replay != NULL && link->replay_settings.replay_feature_enabled)
1123 		replay->funcs->replay_residency(replay, panel_inst, residency, is_start, mode);
1124 	else
1125 		*residency = 0;
1126 
1127 	return true;
1128 }
1129 
edp_set_replay_power_opt_and_coasting_vtotal(struct dc_link * link,const unsigned int * power_opts,uint32_t coasting_vtotal)1130 bool edp_set_replay_power_opt_and_coasting_vtotal(struct dc_link *link,
1131 	const unsigned int *power_opts, uint32_t coasting_vtotal)
1132 {
1133 	struct dc  *dc = link->ctx->dc;
1134 	struct dmub_replay *replay = dc->res_pool->replay;
1135 	unsigned int panel_inst;
1136 
1137 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
1138 		return false;
1139 
1140 	/* Only both power and coasting vtotal changed, this func could return true */
1141 	if (power_opts && link->replay_settings.replay_power_opt_active != *power_opts &&
1142 		coasting_vtotal && link->replay_settings.coasting_vtotal != coasting_vtotal) {
1143 		if (link->replay_settings.replay_feature_enabled &&
1144 			replay->funcs->replay_set_power_opt_and_coasting_vtotal) {
1145 			replay->funcs->replay_set_power_opt_and_coasting_vtotal(replay,
1146 				*power_opts, panel_inst, coasting_vtotal);
1147 			link->replay_settings.replay_power_opt_active = *power_opts;
1148 			link->replay_settings.coasting_vtotal = coasting_vtotal;
1149 		} else
1150 			return false;
1151 	} else
1152 		return false;
1153 
1154 	return true;
1155 }
1156 
get_abm_from_stream_res(const struct dc_link * link)1157 static struct abm *get_abm_from_stream_res(const struct dc_link *link)
1158 {
1159 	int i;
1160 	struct dc *dc = link->ctx->dc;
1161 	struct abm *abm = NULL;
1162 
1163 	for (i = 0; i < MAX_PIPES; i++) {
1164 		struct pipe_ctx *pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
1165 		struct dc_stream_state *stream = pipe_ctx->stream;
1166 
1167 		if (stream && stream->link == link) {
1168 			abm = pipe_ctx->stream_res.abm;
1169 			break;
1170 		}
1171 	}
1172 	return abm;
1173 }
1174 
edp_get_backlight_level(const struct dc_link * link)1175 int edp_get_backlight_level(const struct dc_link *link)
1176 {
1177 	struct abm *abm = get_abm_from_stream_res(link);
1178 	struct panel_cntl *panel_cntl = link->panel_cntl;
1179 	struct dc  *dc = link->ctx->dc;
1180 	struct dmcu *dmcu = dc->res_pool->dmcu;
1181 	bool fw_set_brightness = true;
1182 
1183 	if (dmcu)
1184 		fw_set_brightness = dmcu->funcs->is_dmcu_initialized(dmcu);
1185 
1186 	if (!fw_set_brightness && panel_cntl->funcs->get_current_backlight)
1187 		return panel_cntl->funcs->get_current_backlight(panel_cntl);
1188 	else if (abm != NULL && abm->funcs->get_current_backlight != NULL)
1189 		return (int) abm->funcs->get_current_backlight(abm);
1190 	else
1191 		return DC_ERROR_UNEXPECTED;
1192 }
1193 
edp_get_target_backlight_pwm(const struct dc_link * link)1194 int edp_get_target_backlight_pwm(const struct dc_link *link)
1195 {
1196 	struct abm *abm = get_abm_from_stream_res(link);
1197 
1198 	if (abm == NULL || abm->funcs->get_target_backlight == NULL)
1199 		return DC_ERROR_UNEXPECTED;
1200 
1201 	return (int) abm->funcs->get_target_backlight(abm);
1202 }
1203 
is_smartmux_suported(struct dc_link * link)1204 bool is_smartmux_suported(struct dc_link *link)
1205 {
1206 	if (link->dc->caps.is_apu)
1207 		return false;
1208 	if (!link->dc->config.smart_mux_version)
1209 		return false;
1210 
1211 	return true;
1212 }
1213 
edp_set_assr_enable(const struct dc * pDC,struct dc_link * link,struct link_resource * link_res,bool enable)1214 static void edp_set_assr_enable(const struct dc *pDC, struct dc_link *link,
1215 		struct link_resource *link_res, bool enable)
1216 {
1217 	union dmub_rb_cmd cmd;
1218 	bool use_hpo_dp_link_enc = false;
1219 	uint8_t link_enc_index = 0;
1220 	uint8_t phy_type = 0;
1221 	uint8_t phy_id = 0;
1222 
1223 	if (!pDC->config.use_assr_psp_message)
1224 		return;
1225 
1226 	memset(&cmd, 0, sizeof(cmd));
1227 
1228 	link_enc_index = link->link_enc->transmitter - TRANSMITTER_UNIPHY_A;
1229 
1230 	if (link_res->hpo_dp_link_enc) {
1231 		link_enc_index = link_res->hpo_dp_link_enc->inst;
1232 		use_hpo_dp_link_enc = true;
1233 	}
1234 
1235 	if (enable)
1236 		phy_type = ((dp_get_panel_mode(link) == DP_PANEL_MODE_EDP) ? 1 : 0);
1237 
1238 	phy_id = resource_transmitter_to_phy_idx(pDC, link->link_enc->transmitter);
1239 
1240 	cmd.assr_enable.header.type = DMUB_CMD__PSP;
1241 	cmd.assr_enable.header.sub_type = DMUB_CMD__PSP_ASSR_ENABLE;
1242 	cmd.assr_enable.assr_data.enable = enable;
1243 	cmd.assr_enable.assr_data.phy_port_type = phy_type;
1244 	cmd.assr_enable.assr_data.phy_port_id = phy_id;
1245 	cmd.assr_enable.assr_data.link_enc_index = link_enc_index;
1246 	cmd.assr_enable.assr_data.hpo_mode = use_hpo_dp_link_enc;
1247 
1248 	dc_wake_and_execute_dmub_cmd(pDC->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
1249 }
1250 
edp_set_panel_assr(struct dc_link * link,struct pipe_ctx * pipe_ctx,enum dp_panel_mode * panel_mode,bool enable)1251 void edp_set_panel_assr(struct dc_link *link, struct pipe_ctx *pipe_ctx,
1252 		enum dp_panel_mode *panel_mode, bool enable)
1253 {
1254 	struct link_resource *link_res = &pipe_ctx->link_res;
1255 	struct cp_psp *cp_psp = &pipe_ctx->stream->ctx->cp_psp;
1256 
1257 	if (*panel_mode != DP_PANEL_MODE_EDP)
1258 		return;
1259 
1260 	if (link->dc->config.use_assr_psp_message) {
1261 		edp_set_assr_enable(link->dc, link, link_res, enable);
1262 	} else if (cp_psp && cp_psp->funcs.enable_assr && enable) {
1263 		/* ASSR is bound to fail with unsigned PSP
1264 		 * verstage used during devlopment phase.
1265 		 * Report and continue with eDP panel mode to
1266 		 * perform eDP link training with right settings
1267 		 */
1268 		bool result;
1269 
1270 		result = cp_psp->funcs.enable_assr(cp_psp->handle, link);
1271 
1272 		if (!result && link->panel_mode != DP_PANEL_MODE_EDP)
1273 			*panel_mode = DP_PANEL_MODE_DEFAULT;
1274 	}
1275 }
1276