1f4594cd1SRoman Li /*
2f4594cd1SRoman Li  * Copyright 2021 Advanced Micro Devices, Inc.
3f4594cd1SRoman Li  *
4f4594cd1SRoman Li  * Permission is hereby granted, free of charge, to any person obtaining a
5f4594cd1SRoman Li  * copy of this software and associated documentation files (the "Software"),
6f4594cd1SRoman Li  * to deal in the Software without restriction, including without limitation
7f4594cd1SRoman Li  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f4594cd1SRoman Li  * and/or sell copies of the Software, and to permit persons to whom the
9f4594cd1SRoman Li  * Software is furnished to do so, subject to the following conditions:
10f4594cd1SRoman Li  *
11f4594cd1SRoman Li  * The above copyright notice and this permission notice shall be included in
12f4594cd1SRoman Li  * all copies or substantial portions of the Software.
13f4594cd1SRoman Li  *
14f4594cd1SRoman Li  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f4594cd1SRoman Li  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f4594cd1SRoman Li  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17f4594cd1SRoman Li  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18f4594cd1SRoman Li  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19f4594cd1SRoman Li  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20f4594cd1SRoman Li  * OTHER DEALINGS IN THE SOFTWARE.
21f4594cd1SRoman Li  *
22f4594cd1SRoman Li  * Authors: AMD
23f4594cd1SRoman Li  *
24f4594cd1SRoman Li  */
25f4594cd1SRoman Li 
26f4594cd1SRoman Li #include "amdgpu_dm_psr.h"
27c35b6ea8SMario Limonciello #include "dc_dmub_srv.h"
28f4594cd1SRoman Li #include "dc.h"
29f4594cd1SRoman Li #include "dm_helpers.h"
30cd9a0d02SMikita Lipski #include "amdgpu_dm.h"
319dd94101SDavid Zhang #include "modules/power/power_helpers.h"
32cd9a0d02SMikita Lipski 
link_supports_psrsu(struct dc_link * link)33cd9a0d02SMikita Lipski static bool link_supports_psrsu(struct dc_link *link)
34cd9a0d02SMikita Lipski {
35cd9a0d02SMikita Lipski 	struct dc *dc = link->ctx->dc;
36cd9a0d02SMikita Lipski 
37cd9a0d02SMikita Lipski 	if (!dc->caps.dmcub_support)
38cd9a0d02SMikita Lipski 		return false;
39cd9a0d02SMikita Lipski 
40cd9a0d02SMikita Lipski 	if (dc->ctx->dce_version < DCN_VERSION_3_1)
41cd9a0d02SMikita Lipski 		return false;
42cd9a0d02SMikita Lipski 
439dd94101SDavid Zhang 	if (!is_psr_su_specific_panel(link))
449dd94101SDavid Zhang 		return false;
459dd94101SDavid Zhang 
463e6084aeSPo Ting Chen 	if (!link->dpcd_caps.alpm_caps.bits.AUX_WAKE_ALPM_CAP ||
473e6084aeSPo Ting Chen 	    !link->dpcd_caps.psr_info.psr_dpcd_caps.bits.Y_COORDINATE_REQUIRED)
48cd9a0d02SMikita Lipski 		return false;
49cd9a0d02SMikita Lipski 
503e6084aeSPo Ting Chen 	if (link->dpcd_caps.psr_info.psr_dpcd_caps.bits.SU_GRANULARITY_REQUIRED &&
513e6084aeSPo Ting Chen 	    !link->dpcd_caps.psr_info.psr2_su_y_granularity_cap)
52cd9a0d02SMikita Lipski 		return false;
53cd9a0d02SMikita Lipski 
544e08378bSMario Limonciello 	if (amdgpu_dc_debug_mask & DC_DISABLE_PSR_SU)
554e08378bSMario Limonciello 		return false;
564e08378bSMario Limonciello 
57*e8863f8bSTom Chung 	/* Temporarily disable PSR-SU to avoid glitches */
58*e8863f8bSTom Chung 	return false;
59cd9a0d02SMikita Lipski }
60f4594cd1SRoman Li 
61f4594cd1SRoman Li /*
62f4594cd1SRoman Li  * amdgpu_dm_set_psr_caps() - set link psr capabilities
63f4594cd1SRoman Li  * @link: link
64f4594cd1SRoman Li  *
65f4594cd1SRoman Li  */
amdgpu_dm_set_psr_caps(struct dc_link * link)66f4594cd1SRoman Li void amdgpu_dm_set_psr_caps(struct dc_link *link)
67f4594cd1SRoman Li {
686094b913SShirish S 	if (!(link->connector_signal & SIGNAL_TYPE_EDP)) {
696094b913SShirish S 		link->psr_settings.psr_feature_enabled = false;
70f4594cd1SRoman Li 		return;
716094b913SShirish S 	}
72cd9a0d02SMikita Lipski 
736094b913SShirish S 	if (link->type == dc_connection_none) {
746094b913SShirish S 		link->psr_settings.psr_feature_enabled = false;
75f4594cd1SRoman Li 		return;
766094b913SShirish S 	}
77f4594cd1SRoman Li 
783e6084aeSPo Ting Chen 	if (link->dpcd_caps.psr_info.psr_version == 0) {
79f4594cd1SRoman Li 		link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
80f4594cd1SRoman Li 		link->psr_settings.psr_feature_enabled = false;
81cd9a0d02SMikita Lipski 
82f4594cd1SRoman Li 	} else {
83cd9a0d02SMikita Lipski 		if (link_supports_psrsu(link))
84cd9a0d02SMikita Lipski 			link->psr_settings.psr_version = DC_PSR_VERSION_SU_1;
85cd9a0d02SMikita Lipski 		else
86f4594cd1SRoman Li 			link->psr_settings.psr_version = DC_PSR_VERSION_1;
87cd9a0d02SMikita Lipski 
88f4594cd1SRoman Li 		link->psr_settings.psr_feature_enabled = true;
89f4594cd1SRoman Li 	}
90f4594cd1SRoman Li 
917cc191eeSLeo Li 	DRM_INFO("PSR support %d, DC PSR ver %d, sink PSR ver %d DPCD caps 0x%x su_y_granularity %d\n",
929dd94101SDavid Zhang 		link->psr_settings.psr_feature_enabled,
939dd94101SDavid Zhang 		link->psr_settings.psr_version,
947cc191eeSLeo Li 		link->dpcd_caps.psr_info.psr_version,
957cc191eeSLeo Li 		link->dpcd_caps.psr_info.psr_dpcd_caps.raw,
967cc191eeSLeo Li 		link->dpcd_caps.psr_info.psr2_su_y_granularity_cap);
97cd9a0d02SMikita Lipski 
98f4594cd1SRoman Li }
99f4594cd1SRoman Li 
100f4594cd1SRoman Li /*
101f4594cd1SRoman Li  * amdgpu_dm_link_setup_psr() - configure psr link
102f4594cd1SRoman Li  * @stream: stream state
103f4594cd1SRoman Li  *
104f4594cd1SRoman Li  * Return: true if success
105f4594cd1SRoman Li  */
amdgpu_dm_link_setup_psr(struct dc_stream_state * stream)106f4594cd1SRoman Li bool amdgpu_dm_link_setup_psr(struct dc_stream_state *stream)
107f4594cd1SRoman Li {
108f4594cd1SRoman Li 	struct dc_link *link = NULL;
109f4594cd1SRoman Li 	struct psr_config psr_config = {0};
110f4594cd1SRoman Li 	struct psr_context psr_context = {0};
111381b590cSDavid Zhang 	struct dc *dc = NULL;
112f4594cd1SRoman Li 	bool ret = false;
113f4594cd1SRoman Li 
114f4594cd1SRoman Li 	if (stream == NULL)
115f4594cd1SRoman Li 		return false;
116f4594cd1SRoman Li 
117f4594cd1SRoman Li 	link = stream->link;
118381b590cSDavid Zhang 	dc = link->ctx->dc;
119f4594cd1SRoman Li 
120b80ddeb2SNicholas Kazlauskas 	if (link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED) {
121381b590cSDavid Zhang 		mod_power_calc_psr_configs(&psr_config, link, stream);
122381b590cSDavid Zhang 
123381b590cSDavid Zhang 		/* linux DM specific updating for psr config fields */
124381b590cSDavid Zhang 		psr_config.allow_smu_optimizations =
125381b590cSDavid Zhang 			(amdgpu_dc_feature_mask & DC_PSR_ALLOW_SMU_OPT) &&
126381b590cSDavid Zhang 			mod_power_only_edp(dc->current_state, stream);
127381b590cSDavid Zhang 		psr_config.allow_multi_disp_optimizations =
128381b590cSDavid Zhang 			(amdgpu_dc_feature_mask & DC_PSR_ALLOW_MULTI_DISP_OPT);
129f4594cd1SRoman Li 
130c84ff24aSRobin Chen 		if (!psr_su_set_dsc_slice_height(dc, link, stream, &psr_config))
13113b90cf9SHamza Mahfooz 			return false;
13213b90cf9SHamza Mahfooz 
133f4594cd1SRoman Li 		ret = dc_link_setup_psr(link, stream, &psr_config, &psr_context);
134f4594cd1SRoman Li 
135f4594cd1SRoman Li 	}
136f4594cd1SRoman Li 	DRM_DEBUG_DRIVER("PSR link: %d\n",	link->psr_settings.psr_feature_enabled);
137f4594cd1SRoman Li 
138f4594cd1SRoman Li 	return ret;
139f4594cd1SRoman Li }
140f4594cd1SRoman Li 
141f4594cd1SRoman Li /*
142f4594cd1SRoman Li  * amdgpu_dm_psr_enable() - enable psr f/w
143f4594cd1SRoman Li  * @stream: stream state
144f4594cd1SRoman Li  *
145f4594cd1SRoman Li  */
amdgpu_dm_psr_enable(struct dc_stream_state * stream)146aca8a9b1SHamza Mahfooz void amdgpu_dm_psr_enable(struct dc_stream_state *stream)
147f4594cd1SRoman Li {
148f4594cd1SRoman Li 	struct dc_link *link = stream->link;
149f4594cd1SRoman Li 	unsigned int vsync_rate_hz = 0;
150f4594cd1SRoman Li 	struct dc_static_screen_params params = {0};
151f4594cd1SRoman Li 	/* Calculate number of static frames before generating interrupt to
152f4594cd1SRoman Li 	 * enter PSR.
153f4594cd1SRoman Li 	 */
154f4594cd1SRoman Li 	// Init fail safe of 2 frames static
155f4594cd1SRoman Li 	unsigned int num_frames_static = 2;
156e5dfcd27SRobin Chen 	unsigned int power_opt = 0;
157e5dfcd27SRobin Chen 	bool psr_enable = true;
158f4594cd1SRoman Li 
159f4594cd1SRoman Li 	DRM_DEBUG_DRIVER("Enabling psr...\n");
160f4594cd1SRoman Li 
161f4594cd1SRoman Li 	vsync_rate_hz = div64_u64(div64_u64((
162bbd0d1c9SAlex Hung 			stream->timing.pix_clk_100hz * (uint64_t)100),
163f4594cd1SRoman Li 			stream->timing.v_total),
164f4594cd1SRoman Li 			stream->timing.h_total);
165f4594cd1SRoman Li 
166f4594cd1SRoman Li 	/* Round up
167f4594cd1SRoman Li 	 * Calculate number of frames such that at least 30 ms of time has
168f4594cd1SRoman Li 	 * passed.
169f4594cd1SRoman Li 	 */
170f4594cd1SRoman Li 	if (vsync_rate_hz != 0) {
171f4594cd1SRoman Li 		unsigned int frame_time_microsec = 1000000 / vsync_rate_hz;
172788dbb6bSSrinivasan Shanmugam 
173f4594cd1SRoman Li 		num_frames_static = (30000 / frame_time_microsec) + 1;
174f4594cd1SRoman Li 	}
175f4594cd1SRoman Li 
176f4594cd1SRoman Li 	params.triggers.cursor_update = true;
177f4594cd1SRoman Li 	params.triggers.overlay_update = true;
178f4594cd1SRoman Li 	params.triggers.surface_update = true;
179f4594cd1SRoman Li 	params.num_frames = num_frames_static;
180f4594cd1SRoman Li 
181f4594cd1SRoman Li 	dc_stream_set_static_screen_params(link->ctx->dc,
182f4594cd1SRoman Li 					   &stream, 1,
183f4594cd1SRoman Li 					   &params);
184f4594cd1SRoman Li 
185ab5c4670SLeo Li 	/*
186ab5c4670SLeo Li 	 * Only enable static-screen optimizations for PSR1. For PSR SU, this
187ab5c4670SLeo Li 	 * causes vstartup interrupt issues, used by amdgpu_dm to send vblank
188ab5c4670SLeo Li 	 * events.
189ab5c4670SLeo Li 	 */
190ab5c4670SLeo Li 	if (link->psr_settings.psr_version < DC_PSR_VERSION_SU_1)
191e5dfcd27SRobin Chen 		power_opt |= psr_power_opt_z10_static_screen;
192e5dfcd27SRobin Chen 
193aca8a9b1SHamza Mahfooz 	dc_link_set_psr_allow_active(link, &psr_enable, false, false, &power_opt);
194aca8a9b1SHamza Mahfooz 
195aca8a9b1SHamza Mahfooz 	if (link->ctx->dc->caps.ips_support)
196aca8a9b1SHamza Mahfooz 		dc_allow_idle_optimizations(link->ctx->dc, true);
197f4594cd1SRoman Li }
198f4594cd1SRoman Li 
199f4594cd1SRoman Li /*
200f4594cd1SRoman Li  * amdgpu_dm_psr_disable() - disable psr f/w
201f4594cd1SRoman Li  * @stream:  stream state
202f4594cd1SRoman Li  *
203f4594cd1SRoman Li  * Return: true if success
204f4594cd1SRoman Li  */
amdgpu_dm_psr_disable(struct dc_stream_state * stream,bool wait)205ff2e4d87SLeo Li bool amdgpu_dm_psr_disable(struct dc_stream_state *stream, bool wait)
206f4594cd1SRoman Li {
207e5dfcd27SRobin Chen 	bool psr_enable = false;
208f4594cd1SRoman Li 
209f4594cd1SRoman Li 	DRM_DEBUG_DRIVER("Disabling psr...\n");
210f4594cd1SRoman Li 
211ff2e4d87SLeo Li 	return dc_link_set_psr_allow_active(stream->link, &psr_enable, wait, false, NULL);
212f4594cd1SRoman Li }
213f4594cd1SRoman Li 
214f4594cd1SRoman Li /*
2159bbe7878SRoman Li  * amdgpu_dm_psr_disable_all() - disable psr f/w for all streams
216f4594cd1SRoman Li  * if psr is enabled on any stream
217f4594cd1SRoman Li  *
218f4594cd1SRoman Li  * Return: true if success
219f4594cd1SRoman Li  */
amdgpu_dm_psr_disable_all(struct amdgpu_display_manager * dm)220f4594cd1SRoman Li bool amdgpu_dm_psr_disable_all(struct amdgpu_display_manager *dm)
221f4594cd1SRoman Li {
222f4594cd1SRoman Li 	DRM_DEBUG_DRIVER("Disabling psr if psr is enabled on any stream\n");
223f4594cd1SRoman Li 	return dc_set_psr_allow_active(dm->dc, false);
224f4594cd1SRoman Li }
225f4594cd1SRoman Li 
226afca033fSRoman Li /*
227afca033fSRoman Li  * amdgpu_dm_psr_is_active_allowed() - check if psr is allowed on any stream
228afca033fSRoman Li  * @dm:  pointer to amdgpu_display_manager
229afca033fSRoman Li  *
230afca033fSRoman Li  * Return: true if allowed
231afca033fSRoman Li  */
232afca033fSRoman Li 
amdgpu_dm_psr_is_active_allowed(struct amdgpu_display_manager * dm)233afca033fSRoman Li bool amdgpu_dm_psr_is_active_allowed(struct amdgpu_display_manager *dm)
234afca033fSRoman Li {
235afca033fSRoman Li 	unsigned int i;
236afca033fSRoman Li 	bool allow_active = false;
237afca033fSRoman Li 
238afca033fSRoman Li 	for (i = 0; i < dm->dc->current_state->stream_count ; i++) {
239afca033fSRoman Li 		struct dc_link *link;
240afca033fSRoman Li 		struct dc_stream_state *stream = dm->dc->current_state->streams[i];
241afca033fSRoman Li 
242afca033fSRoman Li 		link = stream->link;
243afca033fSRoman Li 		if (!link)
244afca033fSRoman Li 			continue;
245afca033fSRoman Li 		if (link->psr_settings.psr_feature_enabled &&
246afca033fSRoman Li 		    link->psr_settings.psr_allow_active) {
247afca033fSRoman Li 			allow_active = true;
248afca033fSRoman Li 			break;
249afca033fSRoman Li 		}
250afca033fSRoman Li 	}
251afca033fSRoman Li 
252afca033fSRoman Li 	return allow_active;
253afca033fSRoman Li }
254ff2e4d87SLeo Li 
255ff2e4d87SLeo Li /**
256ff2e4d87SLeo Li  * amdgpu_dm_psr_wait_disable() - Wait for eDP panel to exit PSR
257ff2e4d87SLeo Li  * @stream: stream state attached to the eDP link
258ff2e4d87SLeo Li  *
259ff2e4d87SLeo Li  * Waits for a max of 500ms for the eDP panel to exit PSR.
260ff2e4d87SLeo Li  *
261ff2e4d87SLeo Li  * Return: true if panel exited PSR, false otherwise.
262ff2e4d87SLeo Li  */
amdgpu_dm_psr_wait_disable(struct dc_stream_state * stream)263ff2e4d87SLeo Li bool amdgpu_dm_psr_wait_disable(struct dc_stream_state *stream)
264ff2e4d87SLeo Li {
265ff2e4d87SLeo Li 	enum dc_psr_state psr_state = PSR_STATE0;
266ff2e4d87SLeo Li 	struct dc_link *link = stream->link;
267ff2e4d87SLeo Li 	int retry_count;
268ff2e4d87SLeo Li 
269ff2e4d87SLeo Li 	if (link == NULL)
270ff2e4d87SLeo Li 		return false;
271ff2e4d87SLeo Li 
272ff2e4d87SLeo Li 	for (retry_count = 0; retry_count <= 1000; retry_count++) {
273ff2e4d87SLeo Li 		dc_link_get_psr_state(link, &psr_state);
274ff2e4d87SLeo Li 		if (psr_state == PSR_STATE0)
275ff2e4d87SLeo Li 			break;
276ff2e4d87SLeo Li 		udelay(500);
277ff2e4d87SLeo Li 	}
278ff2e4d87SLeo Li 
279ff2e4d87SLeo Li 	if (retry_count == 1000)
280ff2e4d87SLeo Li 		return false;
281ff2e4d87SLeo Li 
282ff2e4d87SLeo Li 	return true;
283ff2e4d87SLeo Li }
284