1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2018 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Authors: AMD
23b843c749SSergey Zigachev  */
24b843c749SSergey Zigachev #include <linux/string.h>
25b843c749SSergey Zigachev #include <linux/acpi.h>
26b843c749SSergey Zigachev 
27b843c749SSergey Zigachev #include <drm/drmP.h>
28b843c749SSergey Zigachev #include <drm/drm_crtc_helper.h>
29b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
30b843c749SSergey Zigachev #include "dm_services.h"
31b843c749SSergey Zigachev #include "amdgpu.h"
32b843c749SSergey Zigachev #include "amdgpu_dm.h"
33b843c749SSergey Zigachev #include "amdgpu_dm_irq.h"
34b843c749SSergey Zigachev #include "amdgpu_pm.h"
35b843c749SSergey Zigachev #include "dm_pp_smu.h"
36b843c749SSergey Zigachev 
37b843c749SSergey Zigachev 
dm_pp_apply_display_requirements(const struct dc_context * ctx,const struct dm_pp_display_configuration * pp_display_cfg)38b843c749SSergey Zigachev bool dm_pp_apply_display_requirements(
39b843c749SSergey Zigachev 		const struct dc_context *ctx,
40b843c749SSergey Zigachev 		const struct dm_pp_display_configuration *pp_display_cfg)
41b843c749SSergey Zigachev {
42b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
43b843c749SSergey Zigachev 	int i;
44b843c749SSergey Zigachev 
45b843c749SSergey Zigachev 	if (adev->pm.dpm_enabled) {
46b843c749SSergey Zigachev 
47b843c749SSergey Zigachev 		memset(&adev->pm.pm_display_cfg, 0,
48b843c749SSergey Zigachev 				sizeof(adev->pm.pm_display_cfg));
49b843c749SSergey Zigachev 
50b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.cpu_cc6_disable =
51b843c749SSergey Zigachev 			pp_display_cfg->cpu_cc6_disable;
52b843c749SSergey Zigachev 
53b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.cpu_pstate_disable =
54b843c749SSergey Zigachev 			pp_display_cfg->cpu_pstate_disable;
55b843c749SSergey Zigachev 
56b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.cpu_pstate_separation_time =
57b843c749SSergey Zigachev 			pp_display_cfg->cpu_pstate_separation_time;
58b843c749SSergey Zigachev 
59b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.nb_pstate_switch_disable =
60b843c749SSergey Zigachev 			pp_display_cfg->nb_pstate_switch_disable;
61b843c749SSergey Zigachev 
62b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.num_display =
63b843c749SSergey Zigachev 				pp_display_cfg->display_count;
64b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.num_path_including_non_display =
65b843c749SSergey Zigachev 				pp_display_cfg->display_count;
66b843c749SSergey Zigachev 
67b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.min_core_set_clock =
68b843c749SSergey Zigachev 				pp_display_cfg->min_engine_clock_khz/10;
69b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.min_core_set_clock_in_sr =
70b843c749SSergey Zigachev 				pp_display_cfg->min_engine_clock_deep_sleep_khz/10;
71b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.min_mem_set_clock =
72b843c749SSergey Zigachev 				pp_display_cfg->min_memory_clock_khz/10;
73b843c749SSergey Zigachev 
74b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.min_dcef_deep_sleep_set_clk =
75b843c749SSergey Zigachev 				pp_display_cfg->min_engine_clock_deep_sleep_khz/10;
76b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.min_dcef_set_clk =
77b843c749SSergey Zigachev 				pp_display_cfg->min_dcfclock_khz/10;
78b843c749SSergey Zigachev 
79b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.multi_monitor_in_sync =
80b843c749SSergey Zigachev 				pp_display_cfg->all_displays_in_sync;
81b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.min_vblank_time =
82b843c749SSergey Zigachev 				pp_display_cfg->avail_mclk_switch_time_us;
83b843c749SSergey Zigachev 
84b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.display_clk =
85b843c749SSergey Zigachev 				pp_display_cfg->disp_clk_khz/10;
86b843c749SSergey Zigachev 
87b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.dce_tolerable_mclk_in_active_latency =
88b843c749SSergey Zigachev 				pp_display_cfg->avail_mclk_switch_time_in_disp_active_us;
89b843c749SSergey Zigachev 
90b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.crtc_index = pp_display_cfg->crtc_index;
91b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.line_time_in_us =
92b843c749SSergey Zigachev 				pp_display_cfg->line_time_in_us;
93b843c749SSergey Zigachev 
94b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.vrefresh = pp_display_cfg->disp_configs[0].v_refresh;
95b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.crossfire_display_index = -1;
96b843c749SSergey Zigachev 		adev->pm.pm_display_cfg.min_bus_bandwidth = 0;
97b843c749SSergey Zigachev 
98b843c749SSergey Zigachev 		for (i = 0; i < pp_display_cfg->display_count; i++) {
99b843c749SSergey Zigachev 			const struct dm_pp_single_disp_config *dc_cfg =
100b843c749SSergey Zigachev 						&pp_display_cfg->disp_configs[i];
101b843c749SSergey Zigachev 			adev->pm.pm_display_cfg.displays[i].controller_id = dc_cfg->pipe_idx + 1;
102b843c749SSergey Zigachev 		}
103b843c749SSergey Zigachev 
104b843c749SSergey Zigachev 		/* TODO: complete implementation of
105b843c749SSergey Zigachev 		 * pp_display_configuration_change().
106b843c749SSergey Zigachev 		 * Follow example of:
107b843c749SSergey Zigachev 		 * PHM_StoreDALConfigurationData - powerplay\hwmgr\hardwaremanager.c
108b843c749SSergey Zigachev 		 * PP_IRI_DisplayConfigurationChange - powerplay\eventmgr\iri.c */
109b843c749SSergey Zigachev 		if (adev->powerplay.pp_funcs->display_configuration_change)
110b843c749SSergey Zigachev 			adev->powerplay.pp_funcs->display_configuration_change(
111b843c749SSergey Zigachev 				adev->powerplay.pp_handle,
112b843c749SSergey Zigachev 				&adev->pm.pm_display_cfg);
113b843c749SSergey Zigachev 
114b843c749SSergey Zigachev 		/* TODO: replace by a separate call to 'apply display cfg'? */
115b843c749SSergey Zigachev 		amdgpu_pm_compute_clocks(adev);
116b843c749SSergey Zigachev 	}
117b843c749SSergey Zigachev 
118b843c749SSergey Zigachev 	return true;
119b843c749SSergey Zigachev }
120b843c749SSergey Zigachev 
get_default_clock_levels(enum dm_pp_clock_type clk_type,struct dm_pp_clock_levels * clks)121b843c749SSergey Zigachev static void get_default_clock_levels(
122b843c749SSergey Zigachev 		enum dm_pp_clock_type clk_type,
123b843c749SSergey Zigachev 		struct dm_pp_clock_levels *clks)
124b843c749SSergey Zigachev {
125b843c749SSergey Zigachev 	uint32_t disp_clks_in_khz[6] = {
126b843c749SSergey Zigachev 			300000, 400000, 496560, 626090, 685720, 757900 };
127b843c749SSergey Zigachev 	uint32_t sclks_in_khz[6] = {
128b843c749SSergey Zigachev 			300000, 360000, 423530, 514290, 626090, 720000 };
129b843c749SSergey Zigachev 	uint32_t mclks_in_khz[2] = { 333000, 800000 };
130b843c749SSergey Zigachev 
131b843c749SSergey Zigachev 	switch (clk_type) {
132b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_DISPLAY_CLK:
133b843c749SSergey Zigachev 		clks->num_levels = 6;
134b843c749SSergey Zigachev 		memmove(clks->clocks_in_khz, disp_clks_in_khz,
135b843c749SSergey Zigachev 				sizeof(disp_clks_in_khz));
136b843c749SSergey Zigachev 		break;
137b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_ENGINE_CLK:
138b843c749SSergey Zigachev 		clks->num_levels = 6;
139b843c749SSergey Zigachev 		memmove(clks->clocks_in_khz, sclks_in_khz,
140b843c749SSergey Zigachev 				sizeof(sclks_in_khz));
141b843c749SSergey Zigachev 		break;
142b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_MEMORY_CLK:
143b843c749SSergey Zigachev 		clks->num_levels = 2;
144b843c749SSergey Zigachev 		memmove(clks->clocks_in_khz, mclks_in_khz,
145b843c749SSergey Zigachev 				sizeof(mclks_in_khz));
146b843c749SSergey Zigachev 		break;
147b843c749SSergey Zigachev 	default:
148b843c749SSergey Zigachev 		clks->num_levels = 0;
149b843c749SSergey Zigachev 		break;
150b843c749SSergey Zigachev 	}
151b843c749SSergey Zigachev }
152b843c749SSergey Zigachev 
dc_to_pp_clock_type(enum dm_pp_clock_type dm_pp_clk_type)153b843c749SSergey Zigachev static enum amd_pp_clock_type dc_to_pp_clock_type(
154b843c749SSergey Zigachev 		enum dm_pp_clock_type dm_pp_clk_type)
155b843c749SSergey Zigachev {
156b843c749SSergey Zigachev 	enum amd_pp_clock_type amd_pp_clk_type = 0;
157b843c749SSergey Zigachev 
158b843c749SSergey Zigachev 	switch (dm_pp_clk_type) {
159b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_DISPLAY_CLK:
160b843c749SSergey Zigachev 		amd_pp_clk_type = amd_pp_disp_clock;
161b843c749SSergey Zigachev 		break;
162b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_ENGINE_CLK:
163b843c749SSergey Zigachev 		amd_pp_clk_type = amd_pp_sys_clock;
164b843c749SSergey Zigachev 		break;
165b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_MEMORY_CLK:
166b843c749SSergey Zigachev 		amd_pp_clk_type = amd_pp_mem_clock;
167b843c749SSergey Zigachev 		break;
168b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_DCEFCLK:
169b843c749SSergey Zigachev 		amd_pp_clk_type  = amd_pp_dcef_clock;
170b843c749SSergey Zigachev 		break;
171b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_DCFCLK:
172b843c749SSergey Zigachev 		amd_pp_clk_type = amd_pp_dcf_clock;
173b843c749SSergey Zigachev 		break;
174b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_PIXELCLK:
175b843c749SSergey Zigachev 		amd_pp_clk_type = amd_pp_pixel_clock;
176b843c749SSergey Zigachev 		break;
177b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_FCLK:
178b843c749SSergey Zigachev 		amd_pp_clk_type = amd_pp_f_clock;
179b843c749SSergey Zigachev 		break;
180b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_DISPLAYPHYCLK:
181b843c749SSergey Zigachev 		amd_pp_clk_type = amd_pp_phy_clock;
182b843c749SSergey Zigachev 		break;
183b843c749SSergey Zigachev 	case DM_PP_CLOCK_TYPE_DPPCLK:
184b843c749SSergey Zigachev 		amd_pp_clk_type = amd_pp_dpp_clock;
185b843c749SSergey Zigachev 		break;
186b843c749SSergey Zigachev 	default:
187b843c749SSergey Zigachev 		DRM_ERROR("DM_PPLIB: invalid clock type: %d!\n",
188b843c749SSergey Zigachev 				dm_pp_clk_type);
189b843c749SSergey Zigachev 		break;
190b843c749SSergey Zigachev 	}
191b843c749SSergey Zigachev 
192b843c749SSergey Zigachev 	return amd_pp_clk_type;
193b843c749SSergey Zigachev }
194b843c749SSergey Zigachev 
pp_to_dc_powerlevel_state(enum PP_DAL_POWERLEVEL max_clocks_state)195b843c749SSergey Zigachev static enum dm_pp_clocks_state pp_to_dc_powerlevel_state(
196b843c749SSergey Zigachev 			enum PP_DAL_POWERLEVEL max_clocks_state)
197b843c749SSergey Zigachev {
198b843c749SSergey Zigachev 	switch (max_clocks_state) {
199b843c749SSergey Zigachev 	case PP_DAL_POWERLEVEL_0:
200b843c749SSergey Zigachev 		return DM_PP_CLOCKS_DPM_STATE_LEVEL_0;
201b843c749SSergey Zigachev 	case PP_DAL_POWERLEVEL_1:
202b843c749SSergey Zigachev 		return DM_PP_CLOCKS_DPM_STATE_LEVEL_1;
203b843c749SSergey Zigachev 	case PP_DAL_POWERLEVEL_2:
204b843c749SSergey Zigachev 		return DM_PP_CLOCKS_DPM_STATE_LEVEL_2;
205b843c749SSergey Zigachev 	case PP_DAL_POWERLEVEL_3:
206b843c749SSergey Zigachev 		return DM_PP_CLOCKS_DPM_STATE_LEVEL_3;
207b843c749SSergey Zigachev 	case PP_DAL_POWERLEVEL_4:
208b843c749SSergey Zigachev 		return DM_PP_CLOCKS_DPM_STATE_LEVEL_4;
209b843c749SSergey Zigachev 	case PP_DAL_POWERLEVEL_5:
210b843c749SSergey Zigachev 		return DM_PP_CLOCKS_DPM_STATE_LEVEL_5;
211b843c749SSergey Zigachev 	case PP_DAL_POWERLEVEL_6:
212b843c749SSergey Zigachev 		return DM_PP_CLOCKS_DPM_STATE_LEVEL_6;
213b843c749SSergey Zigachev 	case PP_DAL_POWERLEVEL_7:
214b843c749SSergey Zigachev 		return DM_PP_CLOCKS_DPM_STATE_LEVEL_7;
215b843c749SSergey Zigachev 	default:
216b843c749SSergey Zigachev 		DRM_ERROR("DM_PPLIB: invalid powerlevel state: %d!\n",
217b843c749SSergey Zigachev 				max_clocks_state);
218b843c749SSergey Zigachev 		return DM_PP_CLOCKS_STATE_INVALID;
219b843c749SSergey Zigachev 	}
220b843c749SSergey Zigachev }
221b843c749SSergey Zigachev 
pp_to_dc_clock_levels(const struct amd_pp_clocks * pp_clks,struct dm_pp_clock_levels * dc_clks,enum dm_pp_clock_type dc_clk_type)222b843c749SSergey Zigachev static void pp_to_dc_clock_levels(
223b843c749SSergey Zigachev 		const struct amd_pp_clocks *pp_clks,
224b843c749SSergey Zigachev 		struct dm_pp_clock_levels *dc_clks,
225b843c749SSergey Zigachev 		enum dm_pp_clock_type dc_clk_type)
226b843c749SSergey Zigachev {
227b843c749SSergey Zigachev 	uint32_t i;
228b843c749SSergey Zigachev 
229b843c749SSergey Zigachev 	if (pp_clks->count > DM_PP_MAX_CLOCK_LEVELS) {
230b843c749SSergey Zigachev 		DRM_INFO("DM_PPLIB: Warning: %s clock: number of levels %d exceeds maximum of %d!\n",
231b843c749SSergey Zigachev 				DC_DECODE_PP_CLOCK_TYPE(dc_clk_type),
232b843c749SSergey Zigachev 				pp_clks->count,
233b843c749SSergey Zigachev 				DM_PP_MAX_CLOCK_LEVELS);
234b843c749SSergey Zigachev 
235b843c749SSergey Zigachev 		dc_clks->num_levels = DM_PP_MAX_CLOCK_LEVELS;
236b843c749SSergey Zigachev 	} else
237b843c749SSergey Zigachev 		dc_clks->num_levels = pp_clks->count;
238b843c749SSergey Zigachev 
239b843c749SSergey Zigachev 	DRM_INFO("DM_PPLIB: values for %s clock\n",
240b843c749SSergey Zigachev 			DC_DECODE_PP_CLOCK_TYPE(dc_clk_type));
241b843c749SSergey Zigachev 
242b843c749SSergey Zigachev 	for (i = 0; i < dc_clks->num_levels; i++) {
243b843c749SSergey Zigachev 		DRM_INFO("DM_PPLIB:\t %d\n", pp_clks->clock[i]);
244b843c749SSergey Zigachev 		dc_clks->clocks_in_khz[i] = pp_clks->clock[i];
245b843c749SSergey Zigachev 	}
246b843c749SSergey Zigachev }
247b843c749SSergey Zigachev 
pp_to_dc_clock_levels_with_latency(const struct pp_clock_levels_with_latency * pp_clks,struct dm_pp_clock_levels_with_latency * clk_level_info,enum dm_pp_clock_type dc_clk_type)248b843c749SSergey Zigachev static void pp_to_dc_clock_levels_with_latency(
249b843c749SSergey Zigachev 		const struct pp_clock_levels_with_latency *pp_clks,
250b843c749SSergey Zigachev 		struct dm_pp_clock_levels_with_latency *clk_level_info,
251b843c749SSergey Zigachev 		enum dm_pp_clock_type dc_clk_type)
252b843c749SSergey Zigachev {
253b843c749SSergey Zigachev 	uint32_t i;
254b843c749SSergey Zigachev 
255b843c749SSergey Zigachev 	if (pp_clks->num_levels > DM_PP_MAX_CLOCK_LEVELS) {
256b843c749SSergey Zigachev 		DRM_INFO("DM_PPLIB: Warning: %s clock: number of levels %d exceeds maximum of %d!\n",
257b843c749SSergey Zigachev 				DC_DECODE_PP_CLOCK_TYPE(dc_clk_type),
258b843c749SSergey Zigachev 				pp_clks->num_levels,
259b843c749SSergey Zigachev 				DM_PP_MAX_CLOCK_LEVELS);
260b843c749SSergey Zigachev 
261b843c749SSergey Zigachev 		clk_level_info->num_levels = DM_PP_MAX_CLOCK_LEVELS;
262b843c749SSergey Zigachev 	} else
263b843c749SSergey Zigachev 		clk_level_info->num_levels = pp_clks->num_levels;
264b843c749SSergey Zigachev 
265b843c749SSergey Zigachev 	DRM_DEBUG("DM_PPLIB: values for %s clock\n",
266b843c749SSergey Zigachev 			DC_DECODE_PP_CLOCK_TYPE(dc_clk_type));
267b843c749SSergey Zigachev 
268b843c749SSergey Zigachev 	for (i = 0; i < clk_level_info->num_levels; i++) {
269b843c749SSergey Zigachev 		DRM_DEBUG("DM_PPLIB:\t %d in kHz\n", pp_clks->data[i].clocks_in_khz);
270b843c749SSergey Zigachev 		clk_level_info->data[i].clocks_in_khz = pp_clks->data[i].clocks_in_khz;
271b843c749SSergey Zigachev 		clk_level_info->data[i].latency_in_us = pp_clks->data[i].latency_in_us;
272b843c749SSergey Zigachev 	}
273b843c749SSergey Zigachev }
274b843c749SSergey Zigachev 
pp_to_dc_clock_levels_with_voltage(const struct pp_clock_levels_with_voltage * pp_clks,struct dm_pp_clock_levels_with_voltage * clk_level_info,enum dm_pp_clock_type dc_clk_type)275b843c749SSergey Zigachev static void pp_to_dc_clock_levels_with_voltage(
276b843c749SSergey Zigachev 		const struct pp_clock_levels_with_voltage *pp_clks,
277b843c749SSergey Zigachev 		struct dm_pp_clock_levels_with_voltage *clk_level_info,
278b843c749SSergey Zigachev 		enum dm_pp_clock_type dc_clk_type)
279b843c749SSergey Zigachev {
280b843c749SSergey Zigachev 	uint32_t i;
281b843c749SSergey Zigachev 
282b843c749SSergey Zigachev 	if (pp_clks->num_levels > DM_PP_MAX_CLOCK_LEVELS) {
283b843c749SSergey Zigachev 		DRM_INFO("DM_PPLIB: Warning: %s clock: number of levels %d exceeds maximum of %d!\n",
284b843c749SSergey Zigachev 				DC_DECODE_PP_CLOCK_TYPE(dc_clk_type),
285b843c749SSergey Zigachev 				pp_clks->num_levels,
286b843c749SSergey Zigachev 				DM_PP_MAX_CLOCK_LEVELS);
287b843c749SSergey Zigachev 
288b843c749SSergey Zigachev 		clk_level_info->num_levels = DM_PP_MAX_CLOCK_LEVELS;
289b843c749SSergey Zigachev 	} else
290b843c749SSergey Zigachev 		clk_level_info->num_levels = pp_clks->num_levels;
291b843c749SSergey Zigachev 
292b843c749SSergey Zigachev 	DRM_INFO("DM_PPLIB: values for %s clock\n",
293b843c749SSergey Zigachev 			DC_DECODE_PP_CLOCK_TYPE(dc_clk_type));
294b843c749SSergey Zigachev 
295b843c749SSergey Zigachev 	for (i = 0; i < clk_level_info->num_levels; i++) {
296b843c749SSergey Zigachev 		DRM_INFO("DM_PPLIB:\t %d in kHz\n", pp_clks->data[i].clocks_in_khz);
297b843c749SSergey Zigachev 		clk_level_info->data[i].clocks_in_khz = pp_clks->data[i].clocks_in_khz;
298b843c749SSergey Zigachev 		clk_level_info->data[i].voltage_in_mv = pp_clks->data[i].voltage_in_mv;
299b843c749SSergey Zigachev 	}
300b843c749SSergey Zigachev }
301b843c749SSergey Zigachev 
dm_pp_get_clock_levels_by_type(const struct dc_context * ctx,enum dm_pp_clock_type clk_type,struct dm_pp_clock_levels * dc_clks)302b843c749SSergey Zigachev bool dm_pp_get_clock_levels_by_type(
303b843c749SSergey Zigachev 		const struct dc_context *ctx,
304b843c749SSergey Zigachev 		enum dm_pp_clock_type clk_type,
305b843c749SSergey Zigachev 		struct dm_pp_clock_levels *dc_clks)
306b843c749SSergey Zigachev {
307b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
308b843c749SSergey Zigachev 	void *pp_handle = adev->powerplay.pp_handle;
309b843c749SSergey Zigachev 	struct amd_pp_clocks pp_clks = { 0 };
310b843c749SSergey Zigachev 	struct amd_pp_simple_clock_info validation_clks = { 0 };
311b843c749SSergey Zigachev 	uint32_t i;
312b843c749SSergey Zigachev 
313b843c749SSergey Zigachev 	if (adev->powerplay.pp_funcs->get_clock_by_type) {
314b843c749SSergey Zigachev 		if (adev->powerplay.pp_funcs->get_clock_by_type(pp_handle,
315b843c749SSergey Zigachev 			dc_to_pp_clock_type(clk_type), &pp_clks)) {
316b843c749SSergey Zigachev 		/* Error in pplib. Provide default values. */
317b843c749SSergey Zigachev 			get_default_clock_levels(clk_type, dc_clks);
318b843c749SSergey Zigachev 			return true;
319b843c749SSergey Zigachev 		}
320b843c749SSergey Zigachev 	}
321b843c749SSergey Zigachev 
322b843c749SSergey Zigachev 	pp_to_dc_clock_levels(&pp_clks, dc_clks, clk_type);
323b843c749SSergey Zigachev 
324b843c749SSergey Zigachev 	if (adev->powerplay.pp_funcs->get_display_mode_validation_clocks) {
325b843c749SSergey Zigachev 		if (adev->powerplay.pp_funcs->get_display_mode_validation_clocks(
326b843c749SSergey Zigachev 						pp_handle, &validation_clks)) {
327b843c749SSergey Zigachev 			/* Error in pplib. Provide default values. */
328b843c749SSergey Zigachev 			DRM_INFO("DM_PPLIB: Warning: using default validation clocks!\n");
329b843c749SSergey Zigachev 			validation_clks.engine_max_clock = 72000;
330b843c749SSergey Zigachev 			validation_clks.memory_max_clock = 80000;
331b843c749SSergey Zigachev 			validation_clks.level = 0;
332b843c749SSergey Zigachev 		}
333b843c749SSergey Zigachev 	}
334b843c749SSergey Zigachev 
335b843c749SSergey Zigachev 	DRM_INFO("DM_PPLIB: Validation clocks:\n");
336b843c749SSergey Zigachev 	DRM_INFO("DM_PPLIB:    engine_max_clock: %d\n",
337b843c749SSergey Zigachev 			validation_clks.engine_max_clock);
338b843c749SSergey Zigachev 	DRM_INFO("DM_PPLIB:    memory_max_clock: %d\n",
339b843c749SSergey Zigachev 			validation_clks.memory_max_clock);
340b843c749SSergey Zigachev 	DRM_INFO("DM_PPLIB:    level           : %d\n",
341b843c749SSergey Zigachev 			validation_clks.level);
342b843c749SSergey Zigachev 
343b843c749SSergey Zigachev 	/* Translate 10 kHz to kHz. */
344b843c749SSergey Zigachev 	validation_clks.engine_max_clock *= 10;
345b843c749SSergey Zigachev 	validation_clks.memory_max_clock *= 10;
346b843c749SSergey Zigachev 
347b843c749SSergey Zigachev 	/* Determine the highest non-boosted level from the Validation Clocks */
348b843c749SSergey Zigachev 	if (clk_type == DM_PP_CLOCK_TYPE_ENGINE_CLK) {
349b843c749SSergey Zigachev 		for (i = 0; i < dc_clks->num_levels; i++) {
350b843c749SSergey Zigachev 			if (dc_clks->clocks_in_khz[i] > validation_clks.engine_max_clock) {
351b843c749SSergey Zigachev 				/* This clock is higher the validation clock.
352b843c749SSergey Zigachev 				 * Than means the previous one is the highest
353b843c749SSergey Zigachev 				 * non-boosted one. */
354b843c749SSergey Zigachev 				DRM_INFO("DM_PPLIB: reducing engine clock level from %d to %d\n",
355b843c749SSergey Zigachev 						dc_clks->num_levels, i);
356b843c749SSergey Zigachev 				dc_clks->num_levels = i > 0 ? i : 1;
357b843c749SSergey Zigachev 				break;
358b843c749SSergey Zigachev 			}
359b843c749SSergey Zigachev 		}
360b843c749SSergey Zigachev 	} else if (clk_type == DM_PP_CLOCK_TYPE_MEMORY_CLK) {
361b843c749SSergey Zigachev 		for (i = 0; i < dc_clks->num_levels; i++) {
362b843c749SSergey Zigachev 			if (dc_clks->clocks_in_khz[i] > validation_clks.memory_max_clock) {
363b843c749SSergey Zigachev 				DRM_INFO("DM_PPLIB: reducing memory clock level from %d to %d\n",
364b843c749SSergey Zigachev 						dc_clks->num_levels, i);
365b843c749SSergey Zigachev 				dc_clks->num_levels = i > 0 ? i : 1;
366b843c749SSergey Zigachev 				break;
367b843c749SSergey Zigachev 			}
368b843c749SSergey Zigachev 		}
369b843c749SSergey Zigachev 	}
370b843c749SSergey Zigachev 
371b843c749SSergey Zigachev 	return true;
372b843c749SSergey Zigachev }
373b843c749SSergey Zigachev 
dm_pp_get_clock_levels_by_type_with_latency(const struct dc_context * ctx,enum dm_pp_clock_type clk_type,struct dm_pp_clock_levels_with_latency * clk_level_info)374b843c749SSergey Zigachev bool dm_pp_get_clock_levels_by_type_with_latency(
375b843c749SSergey Zigachev 	const struct dc_context *ctx,
376b843c749SSergey Zigachev 	enum dm_pp_clock_type clk_type,
377b843c749SSergey Zigachev 	struct dm_pp_clock_levels_with_latency *clk_level_info)
378b843c749SSergey Zigachev {
379b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
380b843c749SSergey Zigachev 	void *pp_handle = adev->powerplay.pp_handle;
381b843c749SSergey Zigachev 	struct pp_clock_levels_with_latency pp_clks = { 0 };
382b843c749SSergey Zigachev 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
383b843c749SSergey Zigachev 
384b843c749SSergey Zigachev 	if (!pp_funcs || !pp_funcs->get_clock_by_type_with_latency)
385b843c749SSergey Zigachev 		return false;
386b843c749SSergey Zigachev 
387b843c749SSergey Zigachev 	if (pp_funcs->get_clock_by_type_with_latency(pp_handle,
388b843c749SSergey Zigachev 						     dc_to_pp_clock_type(clk_type),
389b843c749SSergey Zigachev 						     &pp_clks))
390b843c749SSergey Zigachev 		return false;
391b843c749SSergey Zigachev 
392b843c749SSergey Zigachev 	pp_to_dc_clock_levels_with_latency(&pp_clks, clk_level_info, clk_type);
393b843c749SSergey Zigachev 
394b843c749SSergey Zigachev 	return true;
395b843c749SSergey Zigachev }
396b843c749SSergey Zigachev 
dm_pp_get_clock_levels_by_type_with_voltage(const struct dc_context * ctx,enum dm_pp_clock_type clk_type,struct dm_pp_clock_levels_with_voltage * clk_level_info)397b843c749SSergey Zigachev bool dm_pp_get_clock_levels_by_type_with_voltage(
398b843c749SSergey Zigachev 	const struct dc_context *ctx,
399b843c749SSergey Zigachev 	enum dm_pp_clock_type clk_type,
400b843c749SSergey Zigachev 	struct dm_pp_clock_levels_with_voltage *clk_level_info)
401b843c749SSergey Zigachev {
402b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
403b843c749SSergey Zigachev 	void *pp_handle = adev->powerplay.pp_handle;
404b843c749SSergey Zigachev 	struct pp_clock_levels_with_voltage pp_clk_info = {0};
405b843c749SSergey Zigachev 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
406b843c749SSergey Zigachev 
407b843c749SSergey Zigachev 	if (pp_funcs->get_clock_by_type_with_voltage(pp_handle,
408b843c749SSergey Zigachev 						     dc_to_pp_clock_type(clk_type),
409b843c749SSergey Zigachev 						     &pp_clk_info))
410b843c749SSergey Zigachev 		return false;
411b843c749SSergey Zigachev 
412b843c749SSergey Zigachev 	pp_to_dc_clock_levels_with_voltage(&pp_clk_info, clk_level_info, clk_type);
413b843c749SSergey Zigachev 
414b843c749SSergey Zigachev 	return true;
415b843c749SSergey Zigachev }
416b843c749SSergey Zigachev 
dm_pp_notify_wm_clock_changes(const struct dc_context * ctx,struct dm_pp_wm_sets_with_clock_ranges * wm_with_clock_ranges)417b843c749SSergey Zigachev bool dm_pp_notify_wm_clock_changes(
418b843c749SSergey Zigachev 	const struct dc_context *ctx,
419b843c749SSergey Zigachev 	struct dm_pp_wm_sets_with_clock_ranges *wm_with_clock_ranges)
420b843c749SSergey Zigachev {
421b843c749SSergey Zigachev 	/* TODO: to be implemented */
422b843c749SSergey Zigachev 	return false;
423b843c749SSergey Zigachev }
424b843c749SSergey Zigachev 
dm_pp_apply_power_level_change_request(const struct dc_context * ctx,struct dm_pp_power_level_change_request * level_change_req)425b843c749SSergey Zigachev bool dm_pp_apply_power_level_change_request(
426b843c749SSergey Zigachev 	const struct dc_context *ctx,
427b843c749SSergey Zigachev 	struct dm_pp_power_level_change_request *level_change_req)
428b843c749SSergey Zigachev {
429b843c749SSergey Zigachev 	/* TODO: to be implemented */
430b843c749SSergey Zigachev 	return false;
431b843c749SSergey Zigachev }
432b843c749SSergey Zigachev 
dm_pp_apply_clock_for_voltage_request(const struct dc_context * ctx,struct dm_pp_clock_for_voltage_req * clock_for_voltage_req)433b843c749SSergey Zigachev bool dm_pp_apply_clock_for_voltage_request(
434b843c749SSergey Zigachev 	const struct dc_context *ctx,
435b843c749SSergey Zigachev 	struct dm_pp_clock_for_voltage_req *clock_for_voltage_req)
436b843c749SSergey Zigachev {
437b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
438b843c749SSergey Zigachev 	struct pp_display_clock_request pp_clock_request = {0};
439b843c749SSergey Zigachev 	int ret = 0;
440b843c749SSergey Zigachev 
441b843c749SSergey Zigachev 	pp_clock_request.clock_type = dc_to_pp_clock_type(clock_for_voltage_req->clk_type);
442b843c749SSergey Zigachev 	pp_clock_request.clock_freq_in_khz = clock_for_voltage_req->clocks_in_khz;
443b843c749SSergey Zigachev 
444b843c749SSergey Zigachev 	if (!pp_clock_request.clock_type)
445b843c749SSergey Zigachev 		return false;
446b843c749SSergey Zigachev 
447b843c749SSergey Zigachev 	if (adev->powerplay.pp_funcs->display_clock_voltage_request)
448b843c749SSergey Zigachev 		ret = adev->powerplay.pp_funcs->display_clock_voltage_request(
449b843c749SSergey Zigachev 			adev->powerplay.pp_handle,
450b843c749SSergey Zigachev 			&pp_clock_request);
451b843c749SSergey Zigachev 	if (ret)
452b843c749SSergey Zigachev 		return false;
453b843c749SSergey Zigachev 	return true;
454b843c749SSergey Zigachev }
455b843c749SSergey Zigachev 
dm_pp_get_static_clocks(const struct dc_context * ctx,struct dm_pp_static_clock_info * static_clk_info)456b843c749SSergey Zigachev bool dm_pp_get_static_clocks(
457b843c749SSergey Zigachev 	const struct dc_context *ctx,
458b843c749SSergey Zigachev 	struct dm_pp_static_clock_info *static_clk_info)
459b843c749SSergey Zigachev {
460b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
461b843c749SSergey Zigachev 	struct amd_pp_clock_info pp_clk_info = {0};
462b843c749SSergey Zigachev 	int ret = 0;
463b843c749SSergey Zigachev 
464b843c749SSergey Zigachev 	if (adev->powerplay.pp_funcs->get_current_clocks)
465b843c749SSergey Zigachev 		ret = adev->powerplay.pp_funcs->get_current_clocks(
466b843c749SSergey Zigachev 			adev->powerplay.pp_handle,
467b843c749SSergey Zigachev 			&pp_clk_info);
468b843c749SSergey Zigachev 	if (ret)
469b843c749SSergey Zigachev 		return false;
470b843c749SSergey Zigachev 
471b843c749SSergey Zigachev 	static_clk_info->max_clocks_state = pp_to_dc_powerlevel_state(pp_clk_info.max_clocks_state);
472b843c749SSergey Zigachev 	static_clk_info->max_mclk_khz = pp_clk_info.max_memory_clock * 10;
473b843c749SSergey Zigachev 	static_clk_info->max_sclk_khz = pp_clk_info.max_engine_clock * 10;
474b843c749SSergey Zigachev 
475b843c749SSergey Zigachev 	return true;
476b843c749SSergey Zigachev }
477b843c749SSergey Zigachev 
478*78973132SSergey Zigachev static
pp_rv_set_display_requirement(struct pp_smu * pp,struct pp_smu_display_requirement_rv * req)479b843c749SSergey Zigachev void pp_rv_set_display_requirement(struct pp_smu *pp,
480b843c749SSergey Zigachev 		struct pp_smu_display_requirement_rv *req)
481b843c749SSergey Zigachev {
482b843c749SSergey Zigachev 	struct dc_context *ctx = pp->ctx;
483b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
484b843c749SSergey Zigachev 	void *pp_handle = adev->powerplay.pp_handle;
485b843c749SSergey Zigachev 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
486b843c749SSergey Zigachev 	struct pp_display_clock_request clock = {0};
487b843c749SSergey Zigachev 
488b843c749SSergey Zigachev 	if (!pp_funcs || !pp_funcs->display_clock_voltage_request)
489b843c749SSergey Zigachev 		return;
490b843c749SSergey Zigachev 
491b843c749SSergey Zigachev 	clock.clock_type = amd_pp_dcf_clock;
492b843c749SSergey Zigachev 	clock.clock_freq_in_khz = req->hard_min_dcefclk_khz;
493b843c749SSergey Zigachev 	pp_funcs->display_clock_voltage_request(pp_handle, &clock);
494b843c749SSergey Zigachev 
495b843c749SSergey Zigachev 	clock.clock_type = amd_pp_f_clock;
496b843c749SSergey Zigachev 	clock.clock_freq_in_khz = req->hard_min_fclk_khz;
497b843c749SSergey Zigachev 	pp_funcs->display_clock_voltage_request(pp_handle, &clock);
498b843c749SSergey Zigachev }
499b843c749SSergey Zigachev 
500*78973132SSergey Zigachev static
pp_rv_set_wm_ranges(struct pp_smu * pp,struct pp_smu_wm_range_sets * ranges)501b843c749SSergey Zigachev void pp_rv_set_wm_ranges(struct pp_smu *pp,
502b843c749SSergey Zigachev 		struct pp_smu_wm_range_sets *ranges)
503b843c749SSergey Zigachev {
504b843c749SSergey Zigachev 	struct dc_context *ctx = pp->ctx;
505b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
506b843c749SSergey Zigachev 	void *pp_handle = adev->powerplay.pp_handle;
507b843c749SSergey Zigachev 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
508b843c749SSergey Zigachev 	struct dm_pp_wm_sets_with_clock_ranges_soc15 wm_with_clock_ranges;
509b843c749SSergey Zigachev 	struct dm_pp_clock_range_for_dmif_wm_set_soc15 *wm_dce_clocks = wm_with_clock_ranges.wm_dmif_clocks_ranges;
510b843c749SSergey Zigachev 	struct dm_pp_clock_range_for_mcif_wm_set_soc15 *wm_soc_clocks = wm_with_clock_ranges.wm_mcif_clocks_ranges;
511b843c749SSergey Zigachev 	int32_t i;
512b843c749SSergey Zigachev 
513b843c749SSergey Zigachev 	wm_with_clock_ranges.num_wm_dmif_sets = ranges->num_reader_wm_sets;
514b843c749SSergey Zigachev 	wm_with_clock_ranges.num_wm_mcif_sets = ranges->num_writer_wm_sets;
515b843c749SSergey Zigachev 
516b843c749SSergey Zigachev 	for (i = 0; i < wm_with_clock_ranges.num_wm_dmif_sets; i++) {
517b843c749SSergey Zigachev 		if (ranges->reader_wm_sets[i].wm_inst > 3)
518b843c749SSergey Zigachev 			wm_dce_clocks[i].wm_set_id = WM_SET_A;
519b843c749SSergey Zigachev 		else
520b843c749SSergey Zigachev 			wm_dce_clocks[i].wm_set_id =
521b843c749SSergey Zigachev 					ranges->reader_wm_sets[i].wm_inst;
522b843c749SSergey Zigachev 		wm_dce_clocks[i].wm_max_dcfclk_clk_in_khz =
523b843c749SSergey Zigachev 				ranges->reader_wm_sets[i].max_drain_clk_khz;
524b843c749SSergey Zigachev 		wm_dce_clocks[i].wm_min_dcfclk_clk_in_khz =
525b843c749SSergey Zigachev 				ranges->reader_wm_sets[i].min_drain_clk_khz;
526b843c749SSergey Zigachev 		wm_dce_clocks[i].wm_max_mem_clk_in_khz =
527b843c749SSergey Zigachev 				ranges->reader_wm_sets[i].max_fill_clk_khz;
528b843c749SSergey Zigachev 		wm_dce_clocks[i].wm_min_mem_clk_in_khz =
529b843c749SSergey Zigachev 				ranges->reader_wm_sets[i].min_fill_clk_khz;
530b843c749SSergey Zigachev 	}
531b843c749SSergey Zigachev 
532b843c749SSergey Zigachev 	for (i = 0; i < wm_with_clock_ranges.num_wm_mcif_sets; i++) {
533b843c749SSergey Zigachev 		if (ranges->writer_wm_sets[i].wm_inst > 3)
534b843c749SSergey Zigachev 			wm_soc_clocks[i].wm_set_id = WM_SET_A;
535b843c749SSergey Zigachev 		else
536b843c749SSergey Zigachev 			wm_soc_clocks[i].wm_set_id =
537b843c749SSergey Zigachev 					ranges->writer_wm_sets[i].wm_inst;
538b843c749SSergey Zigachev 		wm_soc_clocks[i].wm_max_socclk_clk_in_khz =
539b843c749SSergey Zigachev 				ranges->writer_wm_sets[i].max_fill_clk_khz;
540b843c749SSergey Zigachev 		wm_soc_clocks[i].wm_min_socclk_clk_in_khz =
541b843c749SSergey Zigachev 				ranges->writer_wm_sets[i].min_fill_clk_khz;
542b843c749SSergey Zigachev 		wm_soc_clocks[i].wm_max_mem_clk_in_khz =
543b843c749SSergey Zigachev 				ranges->writer_wm_sets[i].max_drain_clk_khz;
544b843c749SSergey Zigachev 		wm_soc_clocks[i].wm_min_mem_clk_in_khz =
545b843c749SSergey Zigachev 				ranges->writer_wm_sets[i].min_drain_clk_khz;
546b843c749SSergey Zigachev 	}
547b843c749SSergey Zigachev 
548b843c749SSergey Zigachev 	pp_funcs->set_watermarks_for_clocks_ranges(pp_handle, &wm_with_clock_ranges);
549b843c749SSergey Zigachev }
550b843c749SSergey Zigachev 
551*78973132SSergey Zigachev static
pp_rv_set_pme_wa_enable(struct pp_smu * pp)552b843c749SSergey Zigachev void pp_rv_set_pme_wa_enable(struct pp_smu *pp)
553b843c749SSergey Zigachev {
554b843c749SSergey Zigachev 	struct dc_context *ctx = pp->ctx;
555b843c749SSergey Zigachev 	struct amdgpu_device *adev = ctx->driver_context;
556b843c749SSergey Zigachev 	void *pp_handle = adev->powerplay.pp_handle;
557b843c749SSergey Zigachev 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
558b843c749SSergey Zigachev 
559b843c749SSergey Zigachev 	if (!pp_funcs || !pp_funcs->notify_smu_enable_pwe)
560b843c749SSergey Zigachev 		return;
561b843c749SSergey Zigachev 
562b843c749SSergey Zigachev 	pp_funcs->notify_smu_enable_pwe(pp_handle);
563b843c749SSergey Zigachev }
564b843c749SSergey Zigachev 
dm_pp_get_funcs_rv(struct dc_context * ctx,struct pp_smu_funcs_rv * funcs)565b843c749SSergey Zigachev void dm_pp_get_funcs_rv(
566b843c749SSergey Zigachev 		struct dc_context *ctx,
567b843c749SSergey Zigachev 		struct pp_smu_funcs_rv *funcs)
568b843c749SSergey Zigachev {
569b843c749SSergey Zigachev 	funcs->pp_smu.ctx = ctx;
570b843c749SSergey Zigachev 	funcs->set_display_requirement = pp_rv_set_display_requirement;
571b843c749SSergey Zigachev 	funcs->set_wm_ranges = pp_rv_set_wm_ranges;
572b843c749SSergey Zigachev 	funcs->set_pme_wa_enable = pp_rv_set_pme_wa_enable;
573b843c749SSergey Zigachev }
574