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 
54c35b6ea8SMario Limonciello 	return dc_dmub_check_min_version(dc->ctx->dmub_srv->dmub);
55cd9a0d02SMikita Lipski }
56f4594cd1SRoman Li 
57f4594cd1SRoman Li /*
58f4594cd1SRoman Li  * amdgpu_dm_set_psr_caps() - set link psr capabilities
59f4594cd1SRoman Li  * @link: link
60f4594cd1SRoman Li  *
61f4594cd1SRoman Li  */
amdgpu_dm_set_psr_caps(struct dc_link * link)62f4594cd1SRoman Li void amdgpu_dm_set_psr_caps(struct dc_link *link)
63f4594cd1SRoman Li {
646094b913SShirish S 	if (!(link->connector_signal & SIGNAL_TYPE_EDP)) {
656094b913SShirish S 		link->psr_settings.psr_feature_enabled = false;
66f4594cd1SRoman Li 		return;
676094b913SShirish S 	}
68cd9a0d02SMikita Lipski 
696094b913SShirish S 	if (link->type == dc_connection_none) {
706094b913SShirish S 		link->psr_settings.psr_feature_enabled = false;
71f4594cd1SRoman Li 		return;
726094b913SShirish S 	}
73f4594cd1SRoman Li 
743e6084aeSPo Ting Chen 	if (link->dpcd_caps.psr_info.psr_version == 0) {
75f4594cd1SRoman Li 		link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
76f4594cd1SRoman Li 		link->psr_settings.psr_feature_enabled = false;
77cd9a0d02SMikita Lipski 
78f4594cd1SRoman Li 	} else {
79cd9a0d02SMikita Lipski 		if (link_supports_psrsu(link))
80cd9a0d02SMikita Lipski 			link->psr_settings.psr_version = DC_PSR_VERSION_SU_1;
81cd9a0d02SMikita Lipski 		else
82f4594cd1SRoman Li 			link->psr_settings.psr_version = DC_PSR_VERSION_1;
83cd9a0d02SMikita Lipski 
84f4594cd1SRoman Li 		link->psr_settings.psr_feature_enabled = true;
85f4594cd1SRoman Li 	}
86f4594cd1SRoman Li 
877cc191eeSLeo Li 	DRM_INFO("PSR support %d, DC PSR ver %d, sink PSR ver %d DPCD caps 0x%x su_y_granularity %d\n",
889dd94101SDavid Zhang 		link->psr_settings.psr_feature_enabled,
899dd94101SDavid Zhang 		link->psr_settings.psr_version,
907cc191eeSLeo Li 		link->dpcd_caps.psr_info.psr_version,
917cc191eeSLeo Li 		link->dpcd_caps.psr_info.psr_dpcd_caps.raw,
927cc191eeSLeo Li 		link->dpcd_caps.psr_info.psr2_su_y_granularity_cap);
93cd9a0d02SMikita Lipski 
94f4594cd1SRoman Li }
95f4594cd1SRoman Li 
96f4594cd1SRoman Li /*
97f4594cd1SRoman Li  * amdgpu_dm_link_setup_psr() - configure psr link
98f4594cd1SRoman Li  * @stream: stream state
99f4594cd1SRoman Li  *
100f4594cd1SRoman Li  * Return: true if success
101f4594cd1SRoman Li  */
amdgpu_dm_link_setup_psr(struct dc_stream_state * stream)102f4594cd1SRoman Li bool amdgpu_dm_link_setup_psr(struct dc_stream_state *stream)
103f4594cd1SRoman Li {
104f4594cd1SRoman Li 	struct dc_link *link = NULL;
105f4594cd1SRoman Li 	struct psr_config psr_config = {0};
106f4594cd1SRoman Li 	struct psr_context psr_context = {0};
107381b590cSDavid Zhang 	struct dc *dc = NULL;
108f4594cd1SRoman Li 	bool ret = false;
109f4594cd1SRoman Li 
110f4594cd1SRoman Li 	if (stream == NULL)
111f4594cd1SRoman Li 		return false;
112f4594cd1SRoman Li 
113f4594cd1SRoman Li 	link = stream->link;
114381b590cSDavid Zhang 	dc = link->ctx->dc;
115f4594cd1SRoman Li 
116b80ddeb2SNicholas Kazlauskas 	if (link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED) {
117381b590cSDavid Zhang 		mod_power_calc_psr_configs(&psr_config, link, stream);
118381b590cSDavid Zhang 
119381b590cSDavid Zhang 		/* linux DM specific updating for psr config fields */
120381b590cSDavid Zhang 		psr_config.allow_smu_optimizations =
121381b590cSDavid Zhang 			(amdgpu_dc_feature_mask & DC_PSR_ALLOW_SMU_OPT) &&
122381b590cSDavid Zhang 			mod_power_only_edp(dc->current_state, stream);
123381b590cSDavid Zhang 		psr_config.allow_multi_disp_optimizations =
124381b590cSDavid Zhang 			(amdgpu_dc_feature_mask & DC_PSR_ALLOW_MULTI_DISP_OPT);
125f4594cd1SRoman Li 
126c84ff24aSRobin Chen 		if (!psr_su_set_dsc_slice_height(dc, link, stream, &psr_config))
12713b90cf9SHamza Mahfooz 			return false;
12813b90cf9SHamza Mahfooz 
129f4594cd1SRoman Li 		ret = dc_link_setup_psr(link, stream, &psr_config, &psr_context);
130f4594cd1SRoman Li 
131f4594cd1SRoman Li 	}
132f4594cd1SRoman Li 	DRM_DEBUG_DRIVER("PSR link: %d\n",	link->psr_settings.psr_feature_enabled);
133f4594cd1SRoman Li 
134f4594cd1SRoman Li 	return ret;
135f4594cd1SRoman Li }
136f4594cd1SRoman Li 
137f4594cd1SRoman Li /*
138f4594cd1SRoman Li  * amdgpu_dm_psr_enable() - enable psr f/w
139f4594cd1SRoman Li  * @stream: stream state
140f4594cd1SRoman Li  *
141f4594cd1SRoman Li  * Return: true if success
142f4594cd1SRoman Li  */
amdgpu_dm_psr_enable(struct dc_stream_state * stream)143f4594cd1SRoman Li bool amdgpu_dm_psr_enable(struct dc_stream_state *stream)
144f4594cd1SRoman Li {
145f4594cd1SRoman Li 	struct dc_link *link = stream->link;
146f4594cd1SRoman Li 	unsigned int vsync_rate_hz = 0;
147f4594cd1SRoman Li 	struct dc_static_screen_params params = {0};
148f4594cd1SRoman Li 	/* Calculate number of static frames before generating interrupt to
149f4594cd1SRoman Li 	 * enter PSR.
150f4594cd1SRoman Li 	 */
151f4594cd1SRoman Li 	// Init fail safe of 2 frames static
152f4594cd1SRoman Li 	unsigned int num_frames_static = 2;
153e5dfcd27SRobin Chen 	unsigned int power_opt = 0;
154e5dfcd27SRobin Chen 	bool psr_enable = true;
155f4594cd1SRoman Li 
156f4594cd1SRoman Li 	DRM_DEBUG_DRIVER("Enabling psr...\n");
157f4594cd1SRoman Li 
158f4594cd1SRoman Li 	vsync_rate_hz = div64_u64(div64_u64((
159f4594cd1SRoman Li 			stream->timing.pix_clk_100hz * 100),
160f4594cd1SRoman Li 			stream->timing.v_total),
161f4594cd1SRoman Li 			stream->timing.h_total);
162f4594cd1SRoman Li 
163f4594cd1SRoman Li 	/* Round up
164f4594cd1SRoman Li 	 * Calculate number of frames such that at least 30 ms of time has
165f4594cd1SRoman Li 	 * passed.
166f4594cd1SRoman Li 	 */
167f4594cd1SRoman Li 	if (vsync_rate_hz != 0) {
168f4594cd1SRoman Li 		unsigned int frame_time_microsec = 1000000 / vsync_rate_hz;
169*788dbb6bSSrinivasan Shanmugam 
170f4594cd1SRoman Li 		num_frames_static = (30000 / frame_time_microsec) + 1;
171f4594cd1SRoman Li 	}
172f4594cd1SRoman Li 
173f4594cd1SRoman Li 	params.triggers.cursor_update = true;
174f4594cd1SRoman Li 	params.triggers.overlay_update = true;
175f4594cd1SRoman Li 	params.triggers.surface_update = true;
176f4594cd1SRoman Li 	params.num_frames = num_frames_static;
177f4594cd1SRoman Li 
178f4594cd1SRoman Li 	dc_stream_set_static_screen_params(link->ctx->dc,
179f4594cd1SRoman Li 					   &stream, 1,
180f4594cd1SRoman Li 					   &params);
181f4594cd1SRoman Li 
182ab5c4670SLeo Li 	/*
183ab5c4670SLeo Li 	 * Only enable static-screen optimizations for PSR1. For PSR SU, this
184ab5c4670SLeo Li 	 * causes vstartup interrupt issues, used by amdgpu_dm to send vblank
185ab5c4670SLeo Li 	 * events.
186ab5c4670SLeo Li 	 */
187ab5c4670SLeo Li 	if (link->psr_settings.psr_version < DC_PSR_VERSION_SU_1)
188e5dfcd27SRobin Chen 		power_opt |= psr_power_opt_z10_static_screen;
189e5dfcd27SRobin Chen 
190e5dfcd27SRobin Chen 	return dc_link_set_psr_allow_active(link, &psr_enable, false, false, &power_opt);
191f4594cd1SRoman Li }
192f4594cd1SRoman Li 
193f4594cd1SRoman Li /*
194f4594cd1SRoman Li  * amdgpu_dm_psr_disable() - disable psr f/w
195f4594cd1SRoman Li  * @stream:  stream state
196f4594cd1SRoman Li  *
197f4594cd1SRoman Li  * Return: true if success
198f4594cd1SRoman Li  */
amdgpu_dm_psr_disable(struct dc_stream_state * stream)199f4594cd1SRoman Li bool amdgpu_dm_psr_disable(struct dc_stream_state *stream)
200f4594cd1SRoman Li {
201e5dfcd27SRobin Chen 	unsigned int power_opt = 0;
202e5dfcd27SRobin Chen 	bool psr_enable = false;
203f4594cd1SRoman Li 
204f4594cd1SRoman Li 	DRM_DEBUG_DRIVER("Disabling psr...\n");
205f4594cd1SRoman Li 
206e5dfcd27SRobin Chen 	return dc_link_set_psr_allow_active(stream->link, &psr_enable, true, false, &power_opt);
207f4594cd1SRoman Li }
208f4594cd1SRoman Li 
209f4594cd1SRoman Li /*
210f4594cd1SRoman Li  * amdgpu_dm_psr_disable() - disable psr f/w
211f4594cd1SRoman Li  * if psr is enabled on any stream
212f4594cd1SRoman Li  *
213f4594cd1SRoman Li  * Return: true if success
214f4594cd1SRoman Li  */
amdgpu_dm_psr_disable_all(struct amdgpu_display_manager * dm)215f4594cd1SRoman Li bool amdgpu_dm_psr_disable_all(struct amdgpu_display_manager *dm)
216f4594cd1SRoman Li {
217f4594cd1SRoman Li 	DRM_DEBUG_DRIVER("Disabling psr if psr is enabled on any stream\n");
218f4594cd1SRoman Li 	return dc_set_psr_allow_active(dm->dc, false);
219f4594cd1SRoman Li }
220f4594cd1SRoman Li 
221