• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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  */
23 
24 #include "amdgpu.h"
25 #include "amdgpu_pm.h"
26 #include "cikd.h"
27 #include "atom.h"
28 #include "amdgpu_atombios.h"
29 #include "amdgpu_dpm.h"
30 #include "kv_dpm.h"
31 #include "gfx_v7_0.h"
32 #include <linux/seq_file.h>
33 
34 #include "smu/smu_7_0_0_d.h"
35 #include "smu/smu_7_0_0_sh_mask.h"
36 
37 #include "gca/gfx_7_2_d.h"
38 #include "gca/gfx_7_2_sh_mask.h"
39 
40 #define KV_MAX_DEEPSLEEP_DIVIDER_ID     5
41 #define KV_MINIMUM_ENGINE_CLOCK         800
42 #define SMC_RAM_END                     0x40000
43 
44 static const struct amd_pm_funcs kv_dpm_funcs;
45 
46 static void kv_dpm_set_irq_funcs(struct amdgpu_device *adev);
47 static int kv_enable_nb_dpm(struct amdgpu_device *adev,
48 			    bool enable);
49 static void kv_init_graphics_levels(struct amdgpu_device *adev);
50 static int kv_calculate_ds_divider(struct amdgpu_device *adev);
51 static int kv_calculate_nbps_level_settings(struct amdgpu_device *adev);
52 static int kv_calculate_dpm_settings(struct amdgpu_device *adev);
53 static void kv_enable_new_levels(struct amdgpu_device *adev);
54 static void kv_program_nbps_index_settings(struct amdgpu_device *adev,
55 					   struct amdgpu_ps *new_rps);
56 static int kv_set_enabled_level(struct amdgpu_device *adev, u32 level);
57 static int kv_set_enabled_levels(struct amdgpu_device *adev);
58 static int kv_force_dpm_highest(struct amdgpu_device *adev);
59 static int kv_force_dpm_lowest(struct amdgpu_device *adev);
60 static void kv_apply_state_adjust_rules(struct amdgpu_device *adev,
61 					struct amdgpu_ps *new_rps,
62 					struct amdgpu_ps *old_rps);
63 static int kv_set_thermal_temperature_range(struct amdgpu_device *adev,
64 					    int min_temp, int max_temp);
65 static int kv_init_fps_limits(struct amdgpu_device *adev);
66 
67 static void kv_dpm_powergate_samu(struct amdgpu_device *adev, bool gate);
68 static void kv_dpm_powergate_acp(struct amdgpu_device *adev, bool gate);
69 
70 
kv_convert_vid2_to_vid7(struct amdgpu_device * adev,struct sumo_vid_mapping_table * vid_mapping_table,u32 vid_2bit)71 static u32 kv_convert_vid2_to_vid7(struct amdgpu_device *adev,
72 				   struct sumo_vid_mapping_table *vid_mapping_table,
73 				   u32 vid_2bit)
74 {
75 	struct amdgpu_clock_voltage_dependency_table *vddc_sclk_table =
76 		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
77 	u32 i;
78 
79 	if (vddc_sclk_table && vddc_sclk_table->count) {
80 		if (vid_2bit < vddc_sclk_table->count)
81 			return vddc_sclk_table->entries[vid_2bit].v;
82 		else
83 			return vddc_sclk_table->entries[vddc_sclk_table->count - 1].v;
84 	} else {
85 		for (i = 0; i < vid_mapping_table->num_entries; i++) {
86 			if (vid_mapping_table->entries[i].vid_2bit == vid_2bit)
87 				return vid_mapping_table->entries[i].vid_7bit;
88 		}
89 		return vid_mapping_table->entries[vid_mapping_table->num_entries - 1].vid_7bit;
90 	}
91 }
92 
kv_convert_vid7_to_vid2(struct amdgpu_device * adev,struct sumo_vid_mapping_table * vid_mapping_table,u32 vid_7bit)93 static u32 kv_convert_vid7_to_vid2(struct amdgpu_device *adev,
94 				   struct sumo_vid_mapping_table *vid_mapping_table,
95 				   u32 vid_7bit)
96 {
97 	struct amdgpu_clock_voltage_dependency_table *vddc_sclk_table =
98 		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
99 	u32 i;
100 
101 	if (vddc_sclk_table && vddc_sclk_table->count) {
102 		for (i = 0; i < vddc_sclk_table->count; i++) {
103 			if (vddc_sclk_table->entries[i].v == vid_7bit)
104 				return i;
105 		}
106 		return vddc_sclk_table->count - 1;
107 	} else {
108 		for (i = 0; i < vid_mapping_table->num_entries; i++) {
109 			if (vid_mapping_table->entries[i].vid_7bit == vid_7bit)
110 				return vid_mapping_table->entries[i].vid_2bit;
111 		}
112 
113 		return vid_mapping_table->entries[vid_mapping_table->num_entries - 1].vid_2bit;
114 	}
115 }
116 
sumo_take_smu_control(struct amdgpu_device * adev,bool enable)117 static void sumo_take_smu_control(struct amdgpu_device *adev, bool enable)
118 {
119 /* This bit selects who handles display phy powergating.
120  * Clear the bit to let atom handle it.
121  * Set it to let the driver handle it.
122  * For now we just let atom handle it.
123  */
124 #if 0
125 	u32 v = RREG32(mmDOUT_SCRATCH3);
126 
127 	if (enable)
128 		v |= 0x4;
129 	else
130 		v &= 0xFFFFFFFB;
131 
132 	WREG32(mmDOUT_SCRATCH3, v);
133 #endif
134 }
135 
sumo_construct_sclk_voltage_mapping_table(struct amdgpu_device * adev,struct sumo_sclk_voltage_mapping_table * sclk_voltage_mapping_table,ATOM_AVAILABLE_SCLK_LIST * table)136 static void sumo_construct_sclk_voltage_mapping_table(struct amdgpu_device *adev,
137 						      struct sumo_sclk_voltage_mapping_table *sclk_voltage_mapping_table,
138 						      ATOM_AVAILABLE_SCLK_LIST *table)
139 {
140 	u32 i;
141 	u32 n = 0;
142 	u32 prev_sclk = 0;
143 
144 	for (i = 0; i < SUMO_MAX_HARDWARE_POWERLEVELS; i++) {
145 		if (table[i].ulSupportedSCLK > prev_sclk) {
146 			sclk_voltage_mapping_table->entries[n].sclk_frequency =
147 				table[i].ulSupportedSCLK;
148 			sclk_voltage_mapping_table->entries[n].vid_2bit =
149 				table[i].usVoltageIndex;
150 			prev_sclk = table[i].ulSupportedSCLK;
151 			n++;
152 		}
153 	}
154 
155 	sclk_voltage_mapping_table->num_max_dpm_entries = n;
156 }
157 
sumo_construct_vid_mapping_table(struct amdgpu_device * adev,struct sumo_vid_mapping_table * vid_mapping_table,ATOM_AVAILABLE_SCLK_LIST * table)158 static void sumo_construct_vid_mapping_table(struct amdgpu_device *adev,
159 					     struct sumo_vid_mapping_table *vid_mapping_table,
160 					     ATOM_AVAILABLE_SCLK_LIST *table)
161 {
162 	u32 i, j;
163 
164 	for (i = 0; i < SUMO_MAX_HARDWARE_POWERLEVELS; i++) {
165 		if (table[i].ulSupportedSCLK != 0) {
166 			vid_mapping_table->entries[table[i].usVoltageIndex].vid_7bit =
167 				table[i].usVoltageID;
168 			vid_mapping_table->entries[table[i].usVoltageIndex].vid_2bit =
169 				table[i].usVoltageIndex;
170 		}
171 	}
172 
173 	for (i = 0; i < SUMO_MAX_NUMBER_VOLTAGES; i++) {
174 		if (vid_mapping_table->entries[i].vid_7bit == 0) {
175 			for (j = i + 1; j < SUMO_MAX_NUMBER_VOLTAGES; j++) {
176 				if (vid_mapping_table->entries[j].vid_7bit != 0) {
177 					vid_mapping_table->entries[i] =
178 						vid_mapping_table->entries[j];
179 					vid_mapping_table->entries[j].vid_7bit = 0;
180 					break;
181 				}
182 			}
183 
184 			if (j == SUMO_MAX_NUMBER_VOLTAGES)
185 				break;
186 		}
187 	}
188 
189 	vid_mapping_table->num_entries = i;
190 }
191 
192 #if 0
193 static const struct kv_lcac_config_values sx_local_cac_cfg_kv[] =
194 {
195 	{  0,       4,        1    },
196 	{  1,       4,        1    },
197 	{  2,       5,        1    },
198 	{  3,       4,        2    },
199 	{  4,       1,        1    },
200 	{  5,       5,        2    },
201 	{  6,       6,        1    },
202 	{  7,       9,        2    },
203 	{ 0xffffffff }
204 };
205 
206 static const struct kv_lcac_config_values mc0_local_cac_cfg_kv[] =
207 {
208 	{  0,       4,        1    },
209 	{ 0xffffffff }
210 };
211 
212 static const struct kv_lcac_config_values mc1_local_cac_cfg_kv[] =
213 {
214 	{  0,       4,        1    },
215 	{ 0xffffffff }
216 };
217 
218 static const struct kv_lcac_config_values mc2_local_cac_cfg_kv[] =
219 {
220 	{  0,       4,        1    },
221 	{ 0xffffffff }
222 };
223 
224 static const struct kv_lcac_config_values mc3_local_cac_cfg_kv[] =
225 {
226 	{  0,       4,        1    },
227 	{ 0xffffffff }
228 };
229 
230 static const struct kv_lcac_config_values cpl_local_cac_cfg_kv[] =
231 {
232 	{  0,       4,        1    },
233 	{  1,       4,        1    },
234 	{  2,       5,        1    },
235 	{  3,       4,        1    },
236 	{  4,       1,        1    },
237 	{  5,       5,        1    },
238 	{  6,       6,        1    },
239 	{  7,       9,        1    },
240 	{  8,       4,        1    },
241 	{  9,       2,        1    },
242 	{  10,      3,        1    },
243 	{  11,      6,        1    },
244 	{  12,      8,        2    },
245 	{  13,      1,        1    },
246 	{  14,      2,        1    },
247 	{  15,      3,        1    },
248 	{  16,      1,        1    },
249 	{  17,      4,        1    },
250 	{  18,      3,        1    },
251 	{  19,      1,        1    },
252 	{  20,      8,        1    },
253 	{  21,      5,        1    },
254 	{  22,      1,        1    },
255 	{  23,      1,        1    },
256 	{  24,      4,        1    },
257 	{  27,      6,        1    },
258 	{  28,      1,        1    },
259 	{ 0xffffffff }
260 };
261 
262 static const struct kv_lcac_config_reg sx0_cac_config_reg[] =
263 {
264 	{ 0xc0400d00, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
265 };
266 
267 static const struct kv_lcac_config_reg mc0_cac_config_reg[] =
268 {
269 	{ 0xc0400d30, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
270 };
271 
272 static const struct kv_lcac_config_reg mc1_cac_config_reg[] =
273 {
274 	{ 0xc0400d3c, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
275 };
276 
277 static const struct kv_lcac_config_reg mc2_cac_config_reg[] =
278 {
279 	{ 0xc0400d48, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
280 };
281 
282 static const struct kv_lcac_config_reg mc3_cac_config_reg[] =
283 {
284 	{ 0xc0400d54, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
285 };
286 
287 static const struct kv_lcac_config_reg cpl_cac_config_reg[] =
288 {
289 	{ 0xc0400d80, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
290 };
291 #endif
292 
293 static const struct kv_pt_config_reg didt_config_kv[] =
294 {
295 	{ 0x10, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
296 	{ 0x10, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
297 	{ 0x10, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
298 	{ 0x10, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
299 	{ 0x11, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
300 	{ 0x11, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
301 	{ 0x11, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
302 	{ 0x11, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
303 	{ 0x12, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
304 	{ 0x12, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
305 	{ 0x12, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
306 	{ 0x12, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
307 	{ 0x2, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
308 	{ 0x2, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
309 	{ 0x2, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
310 	{ 0x1, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
311 	{ 0x1, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
312 	{ 0x0, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
313 	{ 0x30, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
314 	{ 0x30, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
315 	{ 0x30, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
316 	{ 0x30, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
317 	{ 0x31, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
318 	{ 0x31, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
319 	{ 0x31, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
320 	{ 0x31, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
321 	{ 0x32, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
322 	{ 0x32, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
323 	{ 0x32, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
324 	{ 0x32, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
325 	{ 0x22, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
326 	{ 0x22, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
327 	{ 0x22, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
328 	{ 0x21, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
329 	{ 0x21, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
330 	{ 0x20, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
331 	{ 0x50, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
332 	{ 0x50, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
333 	{ 0x50, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
334 	{ 0x50, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
335 	{ 0x51, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
336 	{ 0x51, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
337 	{ 0x51, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
338 	{ 0x51, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
339 	{ 0x52, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
340 	{ 0x52, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
341 	{ 0x52, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
342 	{ 0x52, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
343 	{ 0x42, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
344 	{ 0x42, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
345 	{ 0x42, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
346 	{ 0x41, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
347 	{ 0x41, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
348 	{ 0x40, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
349 	{ 0x70, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
350 	{ 0x70, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
351 	{ 0x70, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
352 	{ 0x70, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
353 	{ 0x71, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
354 	{ 0x71, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
355 	{ 0x71, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
356 	{ 0x71, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
357 	{ 0x72, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
358 	{ 0x72, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
359 	{ 0x72, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
360 	{ 0x72, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
361 	{ 0x62, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
362 	{ 0x62, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
363 	{ 0x62, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
364 	{ 0x61, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
365 	{ 0x61, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
366 	{ 0x60, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
367 	{ 0xFFFFFFFF }
368 };
369 
kv_get_ps(struct amdgpu_ps * rps)370 static struct kv_ps *kv_get_ps(struct amdgpu_ps *rps)
371 {
372 	struct kv_ps *ps = rps->ps_priv;
373 
374 	return ps;
375 }
376 
kv_get_pi(struct amdgpu_device * adev)377 static struct kv_power_info *kv_get_pi(struct amdgpu_device *adev)
378 {
379 	struct kv_power_info *pi = adev->pm.dpm.priv;
380 
381 	return pi;
382 }
383 
384 #if 0
385 static void kv_program_local_cac_table(struct amdgpu_device *adev,
386 				       const struct kv_lcac_config_values *local_cac_table,
387 				       const struct kv_lcac_config_reg *local_cac_reg)
388 {
389 	u32 i, count, data;
390 	const struct kv_lcac_config_values *values = local_cac_table;
391 
392 	while (values->block_id != 0xffffffff) {
393 		count = values->signal_id;
394 		for (i = 0; i < count; i++) {
395 			data = ((values->block_id << local_cac_reg->block_shift) &
396 				local_cac_reg->block_mask);
397 			data |= ((i << local_cac_reg->signal_shift) &
398 				 local_cac_reg->signal_mask);
399 			data |= ((values->t << local_cac_reg->t_shift) &
400 				 local_cac_reg->t_mask);
401 			data |= ((1 << local_cac_reg->enable_shift) &
402 				 local_cac_reg->enable_mask);
403 			WREG32_SMC(local_cac_reg->cntl, data);
404 		}
405 		values++;
406 	}
407 }
408 #endif
409 
kv_program_pt_config_registers(struct amdgpu_device * adev,const struct kv_pt_config_reg * cac_config_regs)410 static int kv_program_pt_config_registers(struct amdgpu_device *adev,
411 					  const struct kv_pt_config_reg *cac_config_regs)
412 {
413 	const struct kv_pt_config_reg *config_regs = cac_config_regs;
414 	u32 data;
415 	u32 cache = 0;
416 
417 	if (config_regs == NULL)
418 		return -EINVAL;
419 
420 	while (config_regs->offset != 0xFFFFFFFF) {
421 		if (config_regs->type == KV_CONFIGREG_CACHE) {
422 			cache |= ((config_regs->value << config_regs->shift) & config_regs->mask);
423 		} else {
424 			switch (config_regs->type) {
425 			case KV_CONFIGREG_SMC_IND:
426 				data = RREG32_SMC(config_regs->offset);
427 				break;
428 			case KV_CONFIGREG_DIDT_IND:
429 				data = RREG32_DIDT(config_regs->offset);
430 				break;
431 			default:
432 				data = RREG32(config_regs->offset);
433 				break;
434 			}
435 
436 			data &= ~config_regs->mask;
437 			data |= ((config_regs->value << config_regs->shift) & config_regs->mask);
438 			data |= cache;
439 			cache = 0;
440 
441 			switch (config_regs->type) {
442 			case KV_CONFIGREG_SMC_IND:
443 				WREG32_SMC(config_regs->offset, data);
444 				break;
445 			case KV_CONFIGREG_DIDT_IND:
446 				WREG32_DIDT(config_regs->offset, data);
447 				break;
448 			default:
449 				WREG32(config_regs->offset, data);
450 				break;
451 			}
452 		}
453 		config_regs++;
454 	}
455 
456 	return 0;
457 }
458 
kv_do_enable_didt(struct amdgpu_device * adev,bool enable)459 static void kv_do_enable_didt(struct amdgpu_device *adev, bool enable)
460 {
461 	struct kv_power_info *pi = kv_get_pi(adev);
462 	u32 data;
463 
464 	if (pi->caps_sq_ramping) {
465 		data = RREG32_DIDT(ixDIDT_SQ_CTRL0);
466 		if (enable)
467 			data |= DIDT_SQ_CTRL0__DIDT_CTRL_EN_MASK;
468 		else
469 			data &= ~DIDT_SQ_CTRL0__DIDT_CTRL_EN_MASK;
470 		WREG32_DIDT(ixDIDT_SQ_CTRL0, data);
471 	}
472 
473 	if (pi->caps_db_ramping) {
474 		data = RREG32_DIDT(ixDIDT_DB_CTRL0);
475 		if (enable)
476 			data |= DIDT_DB_CTRL0__DIDT_CTRL_EN_MASK;
477 		else
478 			data &= ~DIDT_DB_CTRL0__DIDT_CTRL_EN_MASK;
479 		WREG32_DIDT(ixDIDT_DB_CTRL0, data);
480 	}
481 
482 	if (pi->caps_td_ramping) {
483 		data = RREG32_DIDT(ixDIDT_TD_CTRL0);
484 		if (enable)
485 			data |= DIDT_TD_CTRL0__DIDT_CTRL_EN_MASK;
486 		else
487 			data &= ~DIDT_TD_CTRL0__DIDT_CTRL_EN_MASK;
488 		WREG32_DIDT(ixDIDT_TD_CTRL0, data);
489 	}
490 
491 	if (pi->caps_tcp_ramping) {
492 		data = RREG32_DIDT(ixDIDT_TCP_CTRL0);
493 		if (enable)
494 			data |= DIDT_TCP_CTRL0__DIDT_CTRL_EN_MASK;
495 		else
496 			data &= ~DIDT_TCP_CTRL0__DIDT_CTRL_EN_MASK;
497 		WREG32_DIDT(ixDIDT_TCP_CTRL0, data);
498 	}
499 }
500 
kv_enable_didt(struct amdgpu_device * adev,bool enable)501 static int kv_enable_didt(struct amdgpu_device *adev, bool enable)
502 {
503 	struct kv_power_info *pi = kv_get_pi(adev);
504 	int ret;
505 
506 	if (pi->caps_sq_ramping ||
507 	    pi->caps_db_ramping ||
508 	    pi->caps_td_ramping ||
509 	    pi->caps_tcp_ramping) {
510 		amdgpu_gfx_rlc_enter_safe_mode(adev);
511 
512 		if (enable) {
513 			ret = kv_program_pt_config_registers(adev, didt_config_kv);
514 			if (ret) {
515 				amdgpu_gfx_rlc_exit_safe_mode(adev);
516 				return ret;
517 			}
518 		}
519 
520 		kv_do_enable_didt(adev, enable);
521 
522 		amdgpu_gfx_rlc_exit_safe_mode(adev);
523 	}
524 
525 	return 0;
526 }
527 
528 #if 0
529 static void kv_initialize_hardware_cac_manager(struct amdgpu_device *adev)
530 {
531 	struct kv_power_info *pi = kv_get_pi(adev);
532 
533 	if (pi->caps_cac) {
534 		WREG32_SMC(ixLCAC_SX0_OVR_SEL, 0);
535 		WREG32_SMC(ixLCAC_SX0_OVR_VAL, 0);
536 		kv_program_local_cac_table(adev, sx_local_cac_cfg_kv, sx0_cac_config_reg);
537 
538 		WREG32_SMC(ixLCAC_MC0_OVR_SEL, 0);
539 		WREG32_SMC(ixLCAC_MC0_OVR_VAL, 0);
540 		kv_program_local_cac_table(adev, mc0_local_cac_cfg_kv, mc0_cac_config_reg);
541 
542 		WREG32_SMC(ixLCAC_MC1_OVR_SEL, 0);
543 		WREG32_SMC(ixLCAC_MC1_OVR_VAL, 0);
544 		kv_program_local_cac_table(adev, mc1_local_cac_cfg_kv, mc1_cac_config_reg);
545 
546 		WREG32_SMC(ixLCAC_MC2_OVR_SEL, 0);
547 		WREG32_SMC(ixLCAC_MC2_OVR_VAL, 0);
548 		kv_program_local_cac_table(adev, mc2_local_cac_cfg_kv, mc2_cac_config_reg);
549 
550 		WREG32_SMC(ixLCAC_MC3_OVR_SEL, 0);
551 		WREG32_SMC(ixLCAC_MC3_OVR_VAL, 0);
552 		kv_program_local_cac_table(adev, mc3_local_cac_cfg_kv, mc3_cac_config_reg);
553 
554 		WREG32_SMC(ixLCAC_CPL_OVR_SEL, 0);
555 		WREG32_SMC(ixLCAC_CPL_OVR_VAL, 0);
556 		kv_program_local_cac_table(adev, cpl_local_cac_cfg_kv, cpl_cac_config_reg);
557 	}
558 }
559 #endif
560 
kv_enable_smc_cac(struct amdgpu_device * adev,bool enable)561 static int kv_enable_smc_cac(struct amdgpu_device *adev, bool enable)
562 {
563 	struct kv_power_info *pi = kv_get_pi(adev);
564 	int ret = 0;
565 
566 	if (pi->caps_cac) {
567 		if (enable) {
568 			ret = amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_EnableCac);
569 			if (ret)
570 				pi->cac_enabled = false;
571 			else
572 				pi->cac_enabled = true;
573 		} else if (pi->cac_enabled) {
574 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_DisableCac);
575 			pi->cac_enabled = false;
576 		}
577 	}
578 
579 	return ret;
580 }
581 
kv_process_firmware_header(struct amdgpu_device * adev)582 static int kv_process_firmware_header(struct amdgpu_device *adev)
583 {
584 	struct kv_power_info *pi = kv_get_pi(adev);
585 	u32 tmp;
586 	int ret;
587 
588 	ret = amdgpu_kv_read_smc_sram_dword(adev, SMU7_FIRMWARE_HEADER_LOCATION +
589 				     offsetof(SMU7_Firmware_Header, DpmTable),
590 				     &tmp, pi->sram_end);
591 
592 	if (ret == 0)
593 		pi->dpm_table_start = tmp;
594 
595 	ret = amdgpu_kv_read_smc_sram_dword(adev, SMU7_FIRMWARE_HEADER_LOCATION +
596 				     offsetof(SMU7_Firmware_Header, SoftRegisters),
597 				     &tmp, pi->sram_end);
598 
599 	if (ret == 0)
600 		pi->soft_regs_start = tmp;
601 
602 	return ret;
603 }
604 
kv_enable_dpm_voltage_scaling(struct amdgpu_device * adev)605 static int kv_enable_dpm_voltage_scaling(struct amdgpu_device *adev)
606 {
607 	struct kv_power_info *pi = kv_get_pi(adev);
608 	int ret;
609 
610 	pi->graphics_voltage_change_enable = 1;
611 
612 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
613 				   pi->dpm_table_start +
614 				   offsetof(SMU7_Fusion_DpmTable, GraphicsVoltageChangeEnable),
615 				   &pi->graphics_voltage_change_enable,
616 				   sizeof(u8), pi->sram_end);
617 
618 	return ret;
619 }
620 
kv_set_dpm_interval(struct amdgpu_device * adev)621 static int kv_set_dpm_interval(struct amdgpu_device *adev)
622 {
623 	struct kv_power_info *pi = kv_get_pi(adev);
624 	int ret;
625 
626 	pi->graphics_interval = 1;
627 
628 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
629 				   pi->dpm_table_start +
630 				   offsetof(SMU7_Fusion_DpmTable, GraphicsInterval),
631 				   &pi->graphics_interval,
632 				   sizeof(u8), pi->sram_end);
633 
634 	return ret;
635 }
636 
kv_set_dpm_boot_state(struct amdgpu_device * adev)637 static int kv_set_dpm_boot_state(struct amdgpu_device *adev)
638 {
639 	struct kv_power_info *pi = kv_get_pi(adev);
640 	int ret;
641 
642 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
643 				   pi->dpm_table_start +
644 				   offsetof(SMU7_Fusion_DpmTable, GraphicsBootLevel),
645 				   &pi->graphics_boot_level,
646 				   sizeof(u8), pi->sram_end);
647 
648 	return ret;
649 }
650 
kv_program_vc(struct amdgpu_device * adev)651 static void kv_program_vc(struct amdgpu_device *adev)
652 {
653 	WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, 0x3FFFC100);
654 }
655 
kv_clear_vc(struct amdgpu_device * adev)656 static void kv_clear_vc(struct amdgpu_device *adev)
657 {
658 	WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, 0);
659 }
660 
kv_set_divider_value(struct amdgpu_device * adev,u32 index,u32 sclk)661 static int kv_set_divider_value(struct amdgpu_device *adev,
662 				u32 index, u32 sclk)
663 {
664 	struct kv_power_info *pi = kv_get_pi(adev);
665 	struct atom_clock_dividers dividers;
666 	int ret;
667 
668 	ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
669 						 sclk, false, &dividers);
670 	if (ret)
671 		return ret;
672 
673 	pi->graphics_level[index].SclkDid = (u8)dividers.post_div;
674 	pi->graphics_level[index].SclkFrequency = cpu_to_be32(sclk);
675 
676 	return 0;
677 }
678 
kv_convert_8bit_index_to_voltage(struct amdgpu_device * adev,u16 voltage)679 static u16 kv_convert_8bit_index_to_voltage(struct amdgpu_device *adev,
680 					    u16 voltage)
681 {
682 	return 6200 - (voltage * 25);
683 }
684 
kv_convert_2bit_index_to_voltage(struct amdgpu_device * adev,u32 vid_2bit)685 static u16 kv_convert_2bit_index_to_voltage(struct amdgpu_device *adev,
686 					    u32 vid_2bit)
687 {
688 	struct kv_power_info *pi = kv_get_pi(adev);
689 	u32 vid_8bit = kv_convert_vid2_to_vid7(adev,
690 					       &pi->sys_info.vid_mapping_table,
691 					       vid_2bit);
692 
693 	return kv_convert_8bit_index_to_voltage(adev, (u16)vid_8bit);
694 }
695 
696 
kv_set_vid(struct amdgpu_device * adev,u32 index,u32 vid)697 static int kv_set_vid(struct amdgpu_device *adev, u32 index, u32 vid)
698 {
699 	struct kv_power_info *pi = kv_get_pi(adev);
700 
701 	pi->graphics_level[index].VoltageDownH = (u8)pi->voltage_drop_t;
702 	pi->graphics_level[index].MinVddNb =
703 		cpu_to_be32(kv_convert_2bit_index_to_voltage(adev, vid));
704 
705 	return 0;
706 }
707 
kv_set_at(struct amdgpu_device * adev,u32 index,u32 at)708 static int kv_set_at(struct amdgpu_device *adev, u32 index, u32 at)
709 {
710 	struct kv_power_info *pi = kv_get_pi(adev);
711 
712 	pi->graphics_level[index].AT = cpu_to_be16((u16)at);
713 
714 	return 0;
715 }
716 
kv_dpm_power_level_enable(struct amdgpu_device * adev,u32 index,bool enable)717 static void kv_dpm_power_level_enable(struct amdgpu_device *adev,
718 				      u32 index, bool enable)
719 {
720 	struct kv_power_info *pi = kv_get_pi(adev);
721 
722 	pi->graphics_level[index].EnabledForActivity = enable ? 1 : 0;
723 }
724 
kv_start_dpm(struct amdgpu_device * adev)725 static void kv_start_dpm(struct amdgpu_device *adev)
726 {
727 	u32 tmp = RREG32_SMC(ixGENERAL_PWRMGT);
728 
729 	tmp |= GENERAL_PWRMGT__GLOBAL_PWRMGT_EN_MASK;
730 	WREG32_SMC(ixGENERAL_PWRMGT, tmp);
731 
732 	amdgpu_kv_smc_dpm_enable(adev, true);
733 }
734 
kv_stop_dpm(struct amdgpu_device * adev)735 static void kv_stop_dpm(struct amdgpu_device *adev)
736 {
737 	amdgpu_kv_smc_dpm_enable(adev, false);
738 }
739 
kv_start_am(struct amdgpu_device * adev)740 static void kv_start_am(struct amdgpu_device *adev)
741 {
742 	u32 sclk_pwrmgt_cntl = RREG32_SMC(ixSCLK_PWRMGT_CNTL);
743 
744 	sclk_pwrmgt_cntl &= ~(SCLK_PWRMGT_CNTL__RESET_SCLK_CNT_MASK |
745 			SCLK_PWRMGT_CNTL__RESET_BUSY_CNT_MASK);
746 	sclk_pwrmgt_cntl |= SCLK_PWRMGT_CNTL__DYNAMIC_PM_EN_MASK;
747 
748 	WREG32_SMC(ixSCLK_PWRMGT_CNTL, sclk_pwrmgt_cntl);
749 }
750 
kv_reset_am(struct amdgpu_device * adev)751 static void kv_reset_am(struct amdgpu_device *adev)
752 {
753 	u32 sclk_pwrmgt_cntl = RREG32_SMC(ixSCLK_PWRMGT_CNTL);
754 
755 	sclk_pwrmgt_cntl |= (SCLK_PWRMGT_CNTL__RESET_SCLK_CNT_MASK |
756 			SCLK_PWRMGT_CNTL__RESET_BUSY_CNT_MASK);
757 
758 	WREG32_SMC(ixSCLK_PWRMGT_CNTL, sclk_pwrmgt_cntl);
759 }
760 
kv_freeze_sclk_dpm(struct amdgpu_device * adev,bool freeze)761 static int kv_freeze_sclk_dpm(struct amdgpu_device *adev, bool freeze)
762 {
763 	return amdgpu_kv_notify_message_to_smu(adev, freeze ?
764 					PPSMC_MSG_SCLKDPM_FreezeLevel : PPSMC_MSG_SCLKDPM_UnfreezeLevel);
765 }
766 
kv_force_lowest_valid(struct amdgpu_device * adev)767 static int kv_force_lowest_valid(struct amdgpu_device *adev)
768 {
769 	return kv_force_dpm_lowest(adev);
770 }
771 
kv_unforce_levels(struct amdgpu_device * adev)772 static int kv_unforce_levels(struct amdgpu_device *adev)
773 {
774 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
775 		return amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_NoForcedLevel);
776 	else
777 		return kv_set_enabled_levels(adev);
778 }
779 
kv_update_sclk_t(struct amdgpu_device * adev)780 static int kv_update_sclk_t(struct amdgpu_device *adev)
781 {
782 	struct kv_power_info *pi = kv_get_pi(adev);
783 	u32 low_sclk_interrupt_t = 0;
784 	int ret = 0;
785 
786 	if (pi->caps_sclk_throttle_low_notification) {
787 		low_sclk_interrupt_t = cpu_to_be32(pi->low_sclk_interrupt_t);
788 
789 		ret = amdgpu_kv_copy_bytes_to_smc(adev,
790 					   pi->dpm_table_start +
791 					   offsetof(SMU7_Fusion_DpmTable, LowSclkInterruptT),
792 					   (u8 *)&low_sclk_interrupt_t,
793 					   sizeof(u32), pi->sram_end);
794 	}
795 	return ret;
796 }
797 
kv_program_bootup_state(struct amdgpu_device * adev)798 static int kv_program_bootup_state(struct amdgpu_device *adev)
799 {
800 	struct kv_power_info *pi = kv_get_pi(adev);
801 	u32 i;
802 	struct amdgpu_clock_voltage_dependency_table *table =
803 		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
804 
805 	if (table && table->count) {
806 		for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
807 			if (table->entries[i].clk == pi->boot_pl.sclk)
808 				break;
809 		}
810 
811 		pi->graphics_boot_level = (u8)i;
812 		kv_dpm_power_level_enable(adev, i, true);
813 	} else {
814 		struct sumo_sclk_voltage_mapping_table *table =
815 			&pi->sys_info.sclk_voltage_mapping_table;
816 
817 		if (table->num_max_dpm_entries == 0)
818 			return -EINVAL;
819 
820 		for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
821 			if (table->entries[i].sclk_frequency == pi->boot_pl.sclk)
822 				break;
823 		}
824 
825 		pi->graphics_boot_level = (u8)i;
826 		kv_dpm_power_level_enable(adev, i, true);
827 	}
828 	return 0;
829 }
830 
kv_enable_auto_thermal_throttling(struct amdgpu_device * adev)831 static int kv_enable_auto_thermal_throttling(struct amdgpu_device *adev)
832 {
833 	struct kv_power_info *pi = kv_get_pi(adev);
834 	int ret;
835 
836 	pi->graphics_therm_throttle_enable = 1;
837 
838 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
839 				   pi->dpm_table_start +
840 				   offsetof(SMU7_Fusion_DpmTable, GraphicsThermThrottleEnable),
841 				   &pi->graphics_therm_throttle_enable,
842 				   sizeof(u8), pi->sram_end);
843 
844 	return ret;
845 }
846 
kv_upload_dpm_settings(struct amdgpu_device * adev)847 static int kv_upload_dpm_settings(struct amdgpu_device *adev)
848 {
849 	struct kv_power_info *pi = kv_get_pi(adev);
850 	int ret;
851 
852 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
853 				   pi->dpm_table_start +
854 				   offsetof(SMU7_Fusion_DpmTable, GraphicsLevel),
855 				   (u8 *)&pi->graphics_level,
856 				   sizeof(SMU7_Fusion_GraphicsLevel) * SMU7_MAX_LEVELS_GRAPHICS,
857 				   pi->sram_end);
858 
859 	if (ret)
860 		return ret;
861 
862 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
863 				   pi->dpm_table_start +
864 				   offsetof(SMU7_Fusion_DpmTable, GraphicsDpmLevelCount),
865 				   &pi->graphics_dpm_level_count,
866 				   sizeof(u8), pi->sram_end);
867 
868 	return ret;
869 }
870 
kv_get_clock_difference(u32 a,u32 b)871 static u32 kv_get_clock_difference(u32 a, u32 b)
872 {
873 	return (a >= b) ? a - b : b - a;
874 }
875 
kv_get_clk_bypass(struct amdgpu_device * adev,u32 clk)876 static u32 kv_get_clk_bypass(struct amdgpu_device *adev, u32 clk)
877 {
878 	struct kv_power_info *pi = kv_get_pi(adev);
879 	u32 value;
880 
881 	if (pi->caps_enable_dfs_bypass) {
882 		if (kv_get_clock_difference(clk, 40000) < 200)
883 			value = 3;
884 		else if (kv_get_clock_difference(clk, 30000) < 200)
885 			value = 2;
886 		else if (kv_get_clock_difference(clk, 20000) < 200)
887 			value = 7;
888 		else if (kv_get_clock_difference(clk, 15000) < 200)
889 			value = 6;
890 		else if (kv_get_clock_difference(clk, 10000) < 200)
891 			value = 8;
892 		else
893 			value = 0;
894 	} else {
895 		value = 0;
896 	}
897 
898 	return value;
899 }
900 
kv_populate_uvd_table(struct amdgpu_device * adev)901 static int kv_populate_uvd_table(struct amdgpu_device *adev)
902 {
903 	struct kv_power_info *pi = kv_get_pi(adev);
904 	struct amdgpu_uvd_clock_voltage_dependency_table *table =
905 		&adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
906 	struct atom_clock_dividers dividers;
907 	int ret;
908 	u32 i;
909 
910 	if (table == NULL || table->count == 0)
911 		return 0;
912 
913 	pi->uvd_level_count = 0;
914 	for (i = 0; i < table->count; i++) {
915 		if (pi->high_voltage_t &&
916 		    (pi->high_voltage_t < table->entries[i].v))
917 			break;
918 
919 		pi->uvd_level[i].VclkFrequency = cpu_to_be32(table->entries[i].vclk);
920 		pi->uvd_level[i].DclkFrequency = cpu_to_be32(table->entries[i].dclk);
921 		pi->uvd_level[i].MinVddNb = cpu_to_be16(table->entries[i].v);
922 
923 		pi->uvd_level[i].VClkBypassCntl =
924 			(u8)kv_get_clk_bypass(adev, table->entries[i].vclk);
925 		pi->uvd_level[i].DClkBypassCntl =
926 			(u8)kv_get_clk_bypass(adev, table->entries[i].dclk);
927 
928 		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
929 							 table->entries[i].vclk, false, &dividers);
930 		if (ret)
931 			return ret;
932 		pi->uvd_level[i].VclkDivider = (u8)dividers.post_div;
933 
934 		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
935 							 table->entries[i].dclk, false, &dividers);
936 		if (ret)
937 			return ret;
938 		pi->uvd_level[i].DclkDivider = (u8)dividers.post_div;
939 
940 		pi->uvd_level_count++;
941 	}
942 
943 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
944 				   pi->dpm_table_start +
945 				   offsetof(SMU7_Fusion_DpmTable, UvdLevelCount),
946 				   (u8 *)&pi->uvd_level_count,
947 				   sizeof(u8), pi->sram_end);
948 	if (ret)
949 		return ret;
950 
951 	pi->uvd_interval = 1;
952 
953 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
954 				   pi->dpm_table_start +
955 				   offsetof(SMU7_Fusion_DpmTable, UVDInterval),
956 				   &pi->uvd_interval,
957 				   sizeof(u8), pi->sram_end);
958 	if (ret)
959 		return ret;
960 
961 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
962 				   pi->dpm_table_start +
963 				   offsetof(SMU7_Fusion_DpmTable, UvdLevel),
964 				   (u8 *)&pi->uvd_level,
965 				   sizeof(SMU7_Fusion_UvdLevel) * SMU7_MAX_LEVELS_UVD,
966 				   pi->sram_end);
967 
968 	return ret;
969 
970 }
971 
kv_populate_vce_table(struct amdgpu_device * adev)972 static int kv_populate_vce_table(struct amdgpu_device *adev)
973 {
974 	struct kv_power_info *pi = kv_get_pi(adev);
975 	int ret;
976 	u32 i;
977 	struct amdgpu_vce_clock_voltage_dependency_table *table =
978 		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
979 	struct atom_clock_dividers dividers;
980 
981 	if (table == NULL || table->count == 0)
982 		return 0;
983 
984 	pi->vce_level_count = 0;
985 	for (i = 0; i < table->count; i++) {
986 		if (pi->high_voltage_t &&
987 		    pi->high_voltage_t < table->entries[i].v)
988 			break;
989 
990 		pi->vce_level[i].Frequency = cpu_to_be32(table->entries[i].evclk);
991 		pi->vce_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
992 
993 		pi->vce_level[i].ClkBypassCntl =
994 			(u8)kv_get_clk_bypass(adev, table->entries[i].evclk);
995 
996 		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
997 							 table->entries[i].evclk, false, &dividers);
998 		if (ret)
999 			return ret;
1000 		pi->vce_level[i].Divider = (u8)dividers.post_div;
1001 
1002 		pi->vce_level_count++;
1003 	}
1004 
1005 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1006 				   pi->dpm_table_start +
1007 				   offsetof(SMU7_Fusion_DpmTable, VceLevelCount),
1008 				   (u8 *)&pi->vce_level_count,
1009 				   sizeof(u8),
1010 				   pi->sram_end);
1011 	if (ret)
1012 		return ret;
1013 
1014 	pi->vce_interval = 1;
1015 
1016 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1017 				   pi->dpm_table_start +
1018 				   offsetof(SMU7_Fusion_DpmTable, VCEInterval),
1019 				   (u8 *)&pi->vce_interval,
1020 				   sizeof(u8),
1021 				   pi->sram_end);
1022 	if (ret)
1023 		return ret;
1024 
1025 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1026 				   pi->dpm_table_start +
1027 				   offsetof(SMU7_Fusion_DpmTable, VceLevel),
1028 				   (u8 *)&pi->vce_level,
1029 				   sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_VCE,
1030 				   pi->sram_end);
1031 
1032 	return ret;
1033 }
1034 
kv_populate_samu_table(struct amdgpu_device * adev)1035 static int kv_populate_samu_table(struct amdgpu_device *adev)
1036 {
1037 	struct kv_power_info *pi = kv_get_pi(adev);
1038 	struct amdgpu_clock_voltage_dependency_table *table =
1039 		&adev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
1040 	struct atom_clock_dividers dividers;
1041 	int ret;
1042 	u32 i;
1043 
1044 	if (table == NULL || table->count == 0)
1045 		return 0;
1046 
1047 	pi->samu_level_count = 0;
1048 	for (i = 0; i < table->count; i++) {
1049 		if (pi->high_voltage_t &&
1050 		    pi->high_voltage_t < table->entries[i].v)
1051 			break;
1052 
1053 		pi->samu_level[i].Frequency = cpu_to_be32(table->entries[i].clk);
1054 		pi->samu_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
1055 
1056 		pi->samu_level[i].ClkBypassCntl =
1057 			(u8)kv_get_clk_bypass(adev, table->entries[i].clk);
1058 
1059 		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
1060 							 table->entries[i].clk, false, &dividers);
1061 		if (ret)
1062 			return ret;
1063 		pi->samu_level[i].Divider = (u8)dividers.post_div;
1064 
1065 		pi->samu_level_count++;
1066 	}
1067 
1068 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1069 				   pi->dpm_table_start +
1070 				   offsetof(SMU7_Fusion_DpmTable, SamuLevelCount),
1071 				   (u8 *)&pi->samu_level_count,
1072 				   sizeof(u8),
1073 				   pi->sram_end);
1074 	if (ret)
1075 		return ret;
1076 
1077 	pi->samu_interval = 1;
1078 
1079 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1080 				   pi->dpm_table_start +
1081 				   offsetof(SMU7_Fusion_DpmTable, SAMUInterval),
1082 				   (u8 *)&pi->samu_interval,
1083 				   sizeof(u8),
1084 				   pi->sram_end);
1085 	if (ret)
1086 		return ret;
1087 
1088 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1089 				   pi->dpm_table_start +
1090 				   offsetof(SMU7_Fusion_DpmTable, SamuLevel),
1091 				   (u8 *)&pi->samu_level,
1092 				   sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_SAMU,
1093 				   pi->sram_end);
1094 	if (ret)
1095 		return ret;
1096 
1097 	return ret;
1098 }
1099 
1100 
kv_populate_acp_table(struct amdgpu_device * adev)1101 static int kv_populate_acp_table(struct amdgpu_device *adev)
1102 {
1103 	struct kv_power_info *pi = kv_get_pi(adev);
1104 	struct amdgpu_clock_voltage_dependency_table *table =
1105 		&adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1106 	struct atom_clock_dividers dividers;
1107 	int ret;
1108 	u32 i;
1109 
1110 	if (table == NULL || table->count == 0)
1111 		return 0;
1112 
1113 	pi->acp_level_count = 0;
1114 	for (i = 0; i < table->count; i++) {
1115 		pi->acp_level[i].Frequency = cpu_to_be32(table->entries[i].clk);
1116 		pi->acp_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
1117 
1118 		ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
1119 							 table->entries[i].clk, false, &dividers);
1120 		if (ret)
1121 			return ret;
1122 		pi->acp_level[i].Divider = (u8)dividers.post_div;
1123 
1124 		pi->acp_level_count++;
1125 	}
1126 
1127 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1128 				   pi->dpm_table_start +
1129 				   offsetof(SMU7_Fusion_DpmTable, AcpLevelCount),
1130 				   (u8 *)&pi->acp_level_count,
1131 				   sizeof(u8),
1132 				   pi->sram_end);
1133 	if (ret)
1134 		return ret;
1135 
1136 	pi->acp_interval = 1;
1137 
1138 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1139 				   pi->dpm_table_start +
1140 				   offsetof(SMU7_Fusion_DpmTable, ACPInterval),
1141 				   (u8 *)&pi->acp_interval,
1142 				   sizeof(u8),
1143 				   pi->sram_end);
1144 	if (ret)
1145 		return ret;
1146 
1147 	ret = amdgpu_kv_copy_bytes_to_smc(adev,
1148 				   pi->dpm_table_start +
1149 				   offsetof(SMU7_Fusion_DpmTable, AcpLevel),
1150 				   (u8 *)&pi->acp_level,
1151 				   sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_ACP,
1152 				   pi->sram_end);
1153 	if (ret)
1154 		return ret;
1155 
1156 	return ret;
1157 }
1158 
kv_calculate_dfs_bypass_settings(struct amdgpu_device * adev)1159 static void kv_calculate_dfs_bypass_settings(struct amdgpu_device *adev)
1160 {
1161 	struct kv_power_info *pi = kv_get_pi(adev);
1162 	u32 i;
1163 	struct amdgpu_clock_voltage_dependency_table *table =
1164 		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1165 
1166 	if (table && table->count) {
1167 		for (i = 0; i < pi->graphics_dpm_level_count; i++) {
1168 			if (pi->caps_enable_dfs_bypass) {
1169 				if (kv_get_clock_difference(table->entries[i].clk, 40000) < 200)
1170 					pi->graphics_level[i].ClkBypassCntl = 3;
1171 				else if (kv_get_clock_difference(table->entries[i].clk, 30000) < 200)
1172 					pi->graphics_level[i].ClkBypassCntl = 2;
1173 				else if (kv_get_clock_difference(table->entries[i].clk, 26600) < 200)
1174 					pi->graphics_level[i].ClkBypassCntl = 7;
1175 				else if (kv_get_clock_difference(table->entries[i].clk , 20000) < 200)
1176 					pi->graphics_level[i].ClkBypassCntl = 6;
1177 				else if (kv_get_clock_difference(table->entries[i].clk , 10000) < 200)
1178 					pi->graphics_level[i].ClkBypassCntl = 8;
1179 				else
1180 					pi->graphics_level[i].ClkBypassCntl = 0;
1181 			} else {
1182 				pi->graphics_level[i].ClkBypassCntl = 0;
1183 			}
1184 		}
1185 	} else {
1186 		struct sumo_sclk_voltage_mapping_table *table =
1187 			&pi->sys_info.sclk_voltage_mapping_table;
1188 		for (i = 0; i < pi->graphics_dpm_level_count; i++) {
1189 			if (pi->caps_enable_dfs_bypass) {
1190 				if (kv_get_clock_difference(table->entries[i].sclk_frequency, 40000) < 200)
1191 					pi->graphics_level[i].ClkBypassCntl = 3;
1192 				else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 30000) < 200)
1193 					pi->graphics_level[i].ClkBypassCntl = 2;
1194 				else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 26600) < 200)
1195 					pi->graphics_level[i].ClkBypassCntl = 7;
1196 				else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 20000) < 200)
1197 					pi->graphics_level[i].ClkBypassCntl = 6;
1198 				else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 10000) < 200)
1199 					pi->graphics_level[i].ClkBypassCntl = 8;
1200 				else
1201 					pi->graphics_level[i].ClkBypassCntl = 0;
1202 			} else {
1203 				pi->graphics_level[i].ClkBypassCntl = 0;
1204 			}
1205 		}
1206 	}
1207 }
1208 
kv_enable_ulv(struct amdgpu_device * adev,bool enable)1209 static int kv_enable_ulv(struct amdgpu_device *adev, bool enable)
1210 {
1211 	return amdgpu_kv_notify_message_to_smu(adev, enable ?
1212 					PPSMC_MSG_EnableULV : PPSMC_MSG_DisableULV);
1213 }
1214 
kv_reset_acp_boot_level(struct amdgpu_device * adev)1215 static void kv_reset_acp_boot_level(struct amdgpu_device *adev)
1216 {
1217 	struct kv_power_info *pi = kv_get_pi(adev);
1218 
1219 	pi->acp_boot_level = 0xff;
1220 }
1221 
kv_update_current_ps(struct amdgpu_device * adev,struct amdgpu_ps * rps)1222 static void kv_update_current_ps(struct amdgpu_device *adev,
1223 				 struct amdgpu_ps *rps)
1224 {
1225 	struct kv_ps *new_ps = kv_get_ps(rps);
1226 	struct kv_power_info *pi = kv_get_pi(adev);
1227 
1228 	pi->current_rps = *rps;
1229 	pi->current_ps = *new_ps;
1230 	pi->current_rps.ps_priv = &pi->current_ps;
1231 	adev->pm.dpm.current_ps = &pi->current_rps;
1232 }
1233 
kv_update_requested_ps(struct amdgpu_device * adev,struct amdgpu_ps * rps)1234 static void kv_update_requested_ps(struct amdgpu_device *adev,
1235 				   struct amdgpu_ps *rps)
1236 {
1237 	struct kv_ps *new_ps = kv_get_ps(rps);
1238 	struct kv_power_info *pi = kv_get_pi(adev);
1239 
1240 	pi->requested_rps = *rps;
1241 	pi->requested_ps = *new_ps;
1242 	pi->requested_rps.ps_priv = &pi->requested_ps;
1243 	adev->pm.dpm.requested_ps = &pi->requested_rps;
1244 }
1245 
kv_dpm_enable_bapm(void * handle,bool enable)1246 static void kv_dpm_enable_bapm(void *handle, bool enable)
1247 {
1248 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1249 	struct kv_power_info *pi = kv_get_pi(adev);
1250 	int ret;
1251 
1252 	if (pi->bapm_enable) {
1253 		ret = amdgpu_kv_smc_bapm_enable(adev, enable);
1254 		if (ret)
1255 			DRM_ERROR("amdgpu_kv_smc_bapm_enable failed\n");
1256 	}
1257 }
1258 
kv_dpm_enable(struct amdgpu_device * adev)1259 static int kv_dpm_enable(struct amdgpu_device *adev)
1260 {
1261 	struct kv_power_info *pi = kv_get_pi(adev);
1262 	int ret;
1263 
1264 	ret = kv_process_firmware_header(adev);
1265 	if (ret) {
1266 		DRM_ERROR("kv_process_firmware_header failed\n");
1267 		return ret;
1268 	}
1269 	kv_init_fps_limits(adev);
1270 	kv_init_graphics_levels(adev);
1271 	ret = kv_program_bootup_state(adev);
1272 	if (ret) {
1273 		DRM_ERROR("kv_program_bootup_state failed\n");
1274 		return ret;
1275 	}
1276 	kv_calculate_dfs_bypass_settings(adev);
1277 	ret = kv_upload_dpm_settings(adev);
1278 	if (ret) {
1279 		DRM_ERROR("kv_upload_dpm_settings failed\n");
1280 		return ret;
1281 	}
1282 	ret = kv_populate_uvd_table(adev);
1283 	if (ret) {
1284 		DRM_ERROR("kv_populate_uvd_table failed\n");
1285 		return ret;
1286 	}
1287 	ret = kv_populate_vce_table(adev);
1288 	if (ret) {
1289 		DRM_ERROR("kv_populate_vce_table failed\n");
1290 		return ret;
1291 	}
1292 	ret = kv_populate_samu_table(adev);
1293 	if (ret) {
1294 		DRM_ERROR("kv_populate_samu_table failed\n");
1295 		return ret;
1296 	}
1297 	ret = kv_populate_acp_table(adev);
1298 	if (ret) {
1299 		DRM_ERROR("kv_populate_acp_table failed\n");
1300 		return ret;
1301 	}
1302 	kv_program_vc(adev);
1303 #if 0
1304 	kv_initialize_hardware_cac_manager(adev);
1305 #endif
1306 	kv_start_am(adev);
1307 	if (pi->enable_auto_thermal_throttling) {
1308 		ret = kv_enable_auto_thermal_throttling(adev);
1309 		if (ret) {
1310 			DRM_ERROR("kv_enable_auto_thermal_throttling failed\n");
1311 			return ret;
1312 		}
1313 	}
1314 	ret = kv_enable_dpm_voltage_scaling(adev);
1315 	if (ret) {
1316 		DRM_ERROR("kv_enable_dpm_voltage_scaling failed\n");
1317 		return ret;
1318 	}
1319 	ret = kv_set_dpm_interval(adev);
1320 	if (ret) {
1321 		DRM_ERROR("kv_set_dpm_interval failed\n");
1322 		return ret;
1323 	}
1324 	ret = kv_set_dpm_boot_state(adev);
1325 	if (ret) {
1326 		DRM_ERROR("kv_set_dpm_boot_state failed\n");
1327 		return ret;
1328 	}
1329 	ret = kv_enable_ulv(adev, true);
1330 	if (ret) {
1331 		DRM_ERROR("kv_enable_ulv failed\n");
1332 		return ret;
1333 	}
1334 	kv_start_dpm(adev);
1335 	ret = kv_enable_didt(adev, true);
1336 	if (ret) {
1337 		DRM_ERROR("kv_enable_didt failed\n");
1338 		return ret;
1339 	}
1340 	ret = kv_enable_smc_cac(adev, true);
1341 	if (ret) {
1342 		DRM_ERROR("kv_enable_smc_cac failed\n");
1343 		return ret;
1344 	}
1345 
1346 	kv_reset_acp_boot_level(adev);
1347 
1348 	ret = amdgpu_kv_smc_bapm_enable(adev, false);
1349 	if (ret) {
1350 		DRM_ERROR("amdgpu_kv_smc_bapm_enable failed\n");
1351 		return ret;
1352 	}
1353 
1354 	if (adev->irq.installed &&
1355 	    amdgpu_is_internal_thermal_sensor(adev->pm.int_thermal_type)) {
1356 		ret = kv_set_thermal_temperature_range(adev, KV_TEMP_RANGE_MIN, KV_TEMP_RANGE_MAX);
1357 		if (ret) {
1358 			DRM_ERROR("kv_set_thermal_temperature_range failed\n");
1359 			return ret;
1360 		}
1361 		amdgpu_irq_get(adev, &adev->pm.dpm.thermal.irq,
1362 			       AMDGPU_THERMAL_IRQ_LOW_TO_HIGH);
1363 		amdgpu_irq_get(adev, &adev->pm.dpm.thermal.irq,
1364 			       AMDGPU_THERMAL_IRQ_HIGH_TO_LOW);
1365 	}
1366 
1367 	return ret;
1368 }
1369 
kv_dpm_disable(struct amdgpu_device * adev)1370 static void kv_dpm_disable(struct amdgpu_device *adev)
1371 {
1372 	struct kv_power_info *pi = kv_get_pi(adev);
1373 
1374 	amdgpu_irq_put(adev, &adev->pm.dpm.thermal.irq,
1375 		       AMDGPU_THERMAL_IRQ_LOW_TO_HIGH);
1376 	amdgpu_irq_put(adev, &adev->pm.dpm.thermal.irq,
1377 		       AMDGPU_THERMAL_IRQ_HIGH_TO_LOW);
1378 
1379 	amdgpu_kv_smc_bapm_enable(adev, false);
1380 
1381 	if (adev->asic_type == CHIP_MULLINS)
1382 		kv_enable_nb_dpm(adev, false);
1383 
1384 	/* powerup blocks */
1385 	kv_dpm_powergate_acp(adev, false);
1386 	kv_dpm_powergate_samu(adev, false);
1387 	if (pi->caps_vce_pg) /* power on the VCE block */
1388 		amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_VCEPowerON);
1389 	if (pi->caps_uvd_pg) /* power on the UVD block */
1390 		amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_UVDPowerON);
1391 
1392 	kv_enable_smc_cac(adev, false);
1393 	kv_enable_didt(adev, false);
1394 	kv_clear_vc(adev);
1395 	kv_stop_dpm(adev);
1396 	kv_enable_ulv(adev, false);
1397 	kv_reset_am(adev);
1398 
1399 	kv_update_current_ps(adev, adev->pm.dpm.boot_ps);
1400 }
1401 
1402 #if 0
1403 static int kv_write_smc_soft_register(struct amdgpu_device *adev,
1404 				      u16 reg_offset, u32 value)
1405 {
1406 	struct kv_power_info *pi = kv_get_pi(adev);
1407 
1408 	return amdgpu_kv_copy_bytes_to_smc(adev, pi->soft_regs_start + reg_offset,
1409 				    (u8 *)&value, sizeof(u16), pi->sram_end);
1410 }
1411 
1412 static int kv_read_smc_soft_register(struct amdgpu_device *adev,
1413 				     u16 reg_offset, u32 *value)
1414 {
1415 	struct kv_power_info *pi = kv_get_pi(adev);
1416 
1417 	return amdgpu_kv_read_smc_sram_dword(adev, pi->soft_regs_start + reg_offset,
1418 				      value, pi->sram_end);
1419 }
1420 #endif
1421 
kv_init_sclk_t(struct amdgpu_device * adev)1422 static void kv_init_sclk_t(struct amdgpu_device *adev)
1423 {
1424 	struct kv_power_info *pi = kv_get_pi(adev);
1425 
1426 	pi->low_sclk_interrupt_t = 0;
1427 }
1428 
kv_init_fps_limits(struct amdgpu_device * adev)1429 static int kv_init_fps_limits(struct amdgpu_device *adev)
1430 {
1431 	struct kv_power_info *pi = kv_get_pi(adev);
1432 	int ret = 0;
1433 
1434 	if (pi->caps_fps) {
1435 		u16 tmp;
1436 
1437 		tmp = 45;
1438 		pi->fps_high_t = cpu_to_be16(tmp);
1439 		ret = amdgpu_kv_copy_bytes_to_smc(adev,
1440 					   pi->dpm_table_start +
1441 					   offsetof(SMU7_Fusion_DpmTable, FpsHighT),
1442 					   (u8 *)&pi->fps_high_t,
1443 					   sizeof(u16), pi->sram_end);
1444 
1445 		tmp = 30;
1446 		pi->fps_low_t = cpu_to_be16(tmp);
1447 
1448 		ret = amdgpu_kv_copy_bytes_to_smc(adev,
1449 					   pi->dpm_table_start +
1450 					   offsetof(SMU7_Fusion_DpmTable, FpsLowT),
1451 					   (u8 *)&pi->fps_low_t,
1452 					   sizeof(u16), pi->sram_end);
1453 
1454 	}
1455 	return ret;
1456 }
1457 
kv_init_powergate_state(struct amdgpu_device * adev)1458 static void kv_init_powergate_state(struct amdgpu_device *adev)
1459 {
1460 	struct kv_power_info *pi = kv_get_pi(adev);
1461 
1462 	pi->uvd_power_gated = false;
1463 	pi->vce_power_gated = false;
1464 	pi->samu_power_gated = false;
1465 	pi->acp_power_gated = false;
1466 
1467 }
1468 
kv_enable_uvd_dpm(struct amdgpu_device * adev,bool enable)1469 static int kv_enable_uvd_dpm(struct amdgpu_device *adev, bool enable)
1470 {
1471 	return amdgpu_kv_notify_message_to_smu(adev, enable ?
1472 					PPSMC_MSG_UVDDPM_Enable : PPSMC_MSG_UVDDPM_Disable);
1473 }
1474 
kv_enable_vce_dpm(struct amdgpu_device * adev,bool enable)1475 static int kv_enable_vce_dpm(struct amdgpu_device *adev, bool enable)
1476 {
1477 	return amdgpu_kv_notify_message_to_smu(adev, enable ?
1478 					PPSMC_MSG_VCEDPM_Enable : PPSMC_MSG_VCEDPM_Disable);
1479 }
1480 
kv_enable_samu_dpm(struct amdgpu_device * adev,bool enable)1481 static int kv_enable_samu_dpm(struct amdgpu_device *adev, bool enable)
1482 {
1483 	return amdgpu_kv_notify_message_to_smu(adev, enable ?
1484 					PPSMC_MSG_SAMUDPM_Enable : PPSMC_MSG_SAMUDPM_Disable);
1485 }
1486 
kv_enable_acp_dpm(struct amdgpu_device * adev,bool enable)1487 static int kv_enable_acp_dpm(struct amdgpu_device *adev, bool enable)
1488 {
1489 	return amdgpu_kv_notify_message_to_smu(adev, enable ?
1490 					PPSMC_MSG_ACPDPM_Enable : PPSMC_MSG_ACPDPM_Disable);
1491 }
1492 
kv_update_uvd_dpm(struct amdgpu_device * adev,bool gate)1493 static int kv_update_uvd_dpm(struct amdgpu_device *adev, bool gate)
1494 {
1495 	struct kv_power_info *pi = kv_get_pi(adev);
1496 	struct amdgpu_uvd_clock_voltage_dependency_table *table =
1497 		&adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
1498 	int ret;
1499 	u32 mask;
1500 
1501 	if (!gate) {
1502 		if (table->count)
1503 			pi->uvd_boot_level = table->count - 1;
1504 		else
1505 			pi->uvd_boot_level = 0;
1506 
1507 		if (!pi->caps_uvd_dpm || pi->caps_stable_p_state) {
1508 			mask = 1 << pi->uvd_boot_level;
1509 		} else {
1510 			mask = 0x1f;
1511 		}
1512 
1513 		ret = amdgpu_kv_copy_bytes_to_smc(adev,
1514 					   pi->dpm_table_start +
1515 					   offsetof(SMU7_Fusion_DpmTable, UvdBootLevel),
1516 					   (uint8_t *)&pi->uvd_boot_level,
1517 					   sizeof(u8), pi->sram_end);
1518 		if (ret)
1519 			return ret;
1520 
1521 		amdgpu_kv_send_msg_to_smc_with_parameter(adev,
1522 						  PPSMC_MSG_UVDDPM_SetEnabledMask,
1523 						  mask);
1524 	}
1525 
1526 	return kv_enable_uvd_dpm(adev, !gate);
1527 }
1528 
kv_get_vce_boot_level(struct amdgpu_device * adev,u32 evclk)1529 static u8 kv_get_vce_boot_level(struct amdgpu_device *adev, u32 evclk)
1530 {
1531 	u8 i;
1532 	struct amdgpu_vce_clock_voltage_dependency_table *table =
1533 		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1534 
1535 	for (i = 0; i < table->count; i++) {
1536 		if (table->entries[i].evclk >= evclk)
1537 			break;
1538 	}
1539 
1540 	return i;
1541 }
1542 
kv_update_vce_dpm(struct amdgpu_device * adev,struct amdgpu_ps * amdgpu_new_state,struct amdgpu_ps * amdgpu_current_state)1543 static int kv_update_vce_dpm(struct amdgpu_device *adev,
1544 			     struct amdgpu_ps *amdgpu_new_state,
1545 			     struct amdgpu_ps *amdgpu_current_state)
1546 {
1547 	struct kv_power_info *pi = kv_get_pi(adev);
1548 	struct amdgpu_vce_clock_voltage_dependency_table *table =
1549 		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1550 	int ret;
1551 
1552 	if (amdgpu_new_state->evclk > 0 && amdgpu_current_state->evclk == 0) {
1553 		if (pi->caps_stable_p_state)
1554 			pi->vce_boot_level = table->count - 1;
1555 		else
1556 			pi->vce_boot_level = kv_get_vce_boot_level(adev, amdgpu_new_state->evclk);
1557 
1558 		ret = amdgpu_kv_copy_bytes_to_smc(adev,
1559 					   pi->dpm_table_start +
1560 					   offsetof(SMU7_Fusion_DpmTable, VceBootLevel),
1561 					   (u8 *)&pi->vce_boot_level,
1562 					   sizeof(u8),
1563 					   pi->sram_end);
1564 		if (ret)
1565 			return ret;
1566 
1567 		if (pi->caps_stable_p_state)
1568 			amdgpu_kv_send_msg_to_smc_with_parameter(adev,
1569 							  PPSMC_MSG_VCEDPM_SetEnabledMask,
1570 							  (1 << pi->vce_boot_level));
1571 		kv_enable_vce_dpm(adev, true);
1572 	} else if (amdgpu_new_state->evclk == 0 && amdgpu_current_state->evclk > 0) {
1573 		kv_enable_vce_dpm(adev, false);
1574 	}
1575 
1576 	return 0;
1577 }
1578 
kv_update_samu_dpm(struct amdgpu_device * adev,bool gate)1579 static int kv_update_samu_dpm(struct amdgpu_device *adev, bool gate)
1580 {
1581 	struct kv_power_info *pi = kv_get_pi(adev);
1582 	struct amdgpu_clock_voltage_dependency_table *table =
1583 		&adev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
1584 	int ret;
1585 
1586 	if (!gate) {
1587 		if (pi->caps_stable_p_state)
1588 			pi->samu_boot_level = table->count - 1;
1589 		else
1590 			pi->samu_boot_level = 0;
1591 
1592 		ret = amdgpu_kv_copy_bytes_to_smc(adev,
1593 					   pi->dpm_table_start +
1594 					   offsetof(SMU7_Fusion_DpmTable, SamuBootLevel),
1595 					   (u8 *)&pi->samu_boot_level,
1596 					   sizeof(u8),
1597 					   pi->sram_end);
1598 		if (ret)
1599 			return ret;
1600 
1601 		if (pi->caps_stable_p_state)
1602 			amdgpu_kv_send_msg_to_smc_with_parameter(adev,
1603 							  PPSMC_MSG_SAMUDPM_SetEnabledMask,
1604 							  (1 << pi->samu_boot_level));
1605 	}
1606 
1607 	return kv_enable_samu_dpm(adev, !gate);
1608 }
1609 
kv_get_acp_boot_level(struct amdgpu_device * adev)1610 static u8 kv_get_acp_boot_level(struct amdgpu_device *adev)
1611 {
1612 	return 0;
1613 }
1614 
kv_update_acp_boot_level(struct amdgpu_device * adev)1615 static void kv_update_acp_boot_level(struct amdgpu_device *adev)
1616 {
1617 	struct kv_power_info *pi = kv_get_pi(adev);
1618 	u8 acp_boot_level;
1619 
1620 	if (!pi->caps_stable_p_state) {
1621 		acp_boot_level = kv_get_acp_boot_level(adev);
1622 		if (acp_boot_level != pi->acp_boot_level) {
1623 			pi->acp_boot_level = acp_boot_level;
1624 			amdgpu_kv_send_msg_to_smc_with_parameter(adev,
1625 							  PPSMC_MSG_ACPDPM_SetEnabledMask,
1626 							  (1 << pi->acp_boot_level));
1627 		}
1628 	}
1629 }
1630 
kv_update_acp_dpm(struct amdgpu_device * adev,bool gate)1631 static int kv_update_acp_dpm(struct amdgpu_device *adev, bool gate)
1632 {
1633 	struct kv_power_info *pi = kv_get_pi(adev);
1634 	struct amdgpu_clock_voltage_dependency_table *table =
1635 		&adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1636 	int ret;
1637 
1638 	if (!gate) {
1639 		if (pi->caps_stable_p_state)
1640 			pi->acp_boot_level = table->count - 1;
1641 		else
1642 			pi->acp_boot_level = kv_get_acp_boot_level(adev);
1643 
1644 		ret = amdgpu_kv_copy_bytes_to_smc(adev,
1645 					   pi->dpm_table_start +
1646 					   offsetof(SMU7_Fusion_DpmTable, AcpBootLevel),
1647 					   (u8 *)&pi->acp_boot_level,
1648 					   sizeof(u8),
1649 					   pi->sram_end);
1650 		if (ret)
1651 			return ret;
1652 
1653 		if (pi->caps_stable_p_state)
1654 			amdgpu_kv_send_msg_to_smc_with_parameter(adev,
1655 							  PPSMC_MSG_ACPDPM_SetEnabledMask,
1656 							  (1 << pi->acp_boot_level));
1657 	}
1658 
1659 	return kv_enable_acp_dpm(adev, !gate);
1660 }
1661 
kv_dpm_powergate_uvd(void * handle,bool gate)1662 static void kv_dpm_powergate_uvd(void *handle, bool gate)
1663 {
1664 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1665 	struct kv_power_info *pi = kv_get_pi(adev);
1666 	int ret;
1667 
1668 	pi->uvd_power_gated = gate;
1669 
1670 	if (gate) {
1671 		/* stop the UVD block */
1672 		ret = amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
1673 							     AMD_PG_STATE_GATE);
1674 		kv_update_uvd_dpm(adev, gate);
1675 		if (pi->caps_uvd_pg)
1676 			/* power off the UVD block */
1677 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_UVDPowerOFF);
1678 	} else {
1679 		if (pi->caps_uvd_pg)
1680 			/* power on the UVD block */
1681 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_UVDPowerON);
1682 			/* re-init the UVD block */
1683 		kv_update_uvd_dpm(adev, gate);
1684 
1685 		ret = amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
1686 							     AMD_PG_STATE_UNGATE);
1687 	}
1688 }
1689 
kv_dpm_powergate_vce(void * handle,bool gate)1690 static void kv_dpm_powergate_vce(void *handle, bool gate)
1691 {
1692 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1693 	struct kv_power_info *pi = kv_get_pi(adev);
1694 	int ret;
1695 
1696 	pi->vce_power_gated = gate;
1697 
1698 	if (gate) {
1699 		/* stop the VCE block */
1700 		ret = amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1701 							     AMD_PG_STATE_GATE);
1702 		kv_enable_vce_dpm(adev, false);
1703 		if (pi->caps_vce_pg) /* power off the VCE block */
1704 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_VCEPowerOFF);
1705 	} else {
1706 		if (pi->caps_vce_pg) /* power on the VCE block */
1707 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_VCEPowerON);
1708 		kv_enable_vce_dpm(adev, true);
1709 		/* re-init the VCE block */
1710 		ret = amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1711 							     AMD_PG_STATE_UNGATE);
1712 	}
1713 }
1714 
1715 
kv_dpm_powergate_samu(struct amdgpu_device * adev,bool gate)1716 static void kv_dpm_powergate_samu(struct amdgpu_device *adev, bool gate)
1717 {
1718 	struct kv_power_info *pi = kv_get_pi(adev);
1719 
1720 	if (pi->samu_power_gated == gate)
1721 		return;
1722 
1723 	pi->samu_power_gated = gate;
1724 
1725 	if (gate) {
1726 		kv_update_samu_dpm(adev, true);
1727 		if (pi->caps_samu_pg)
1728 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_SAMPowerOFF);
1729 	} else {
1730 		if (pi->caps_samu_pg)
1731 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_SAMPowerON);
1732 		kv_update_samu_dpm(adev, false);
1733 	}
1734 }
1735 
kv_dpm_powergate_acp(struct amdgpu_device * adev,bool gate)1736 static void kv_dpm_powergate_acp(struct amdgpu_device *adev, bool gate)
1737 {
1738 	struct kv_power_info *pi = kv_get_pi(adev);
1739 
1740 	if (pi->acp_power_gated == gate)
1741 		return;
1742 
1743 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
1744 		return;
1745 
1746 	pi->acp_power_gated = gate;
1747 
1748 	if (gate) {
1749 		kv_update_acp_dpm(adev, true);
1750 		if (pi->caps_acp_pg)
1751 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_ACPPowerOFF);
1752 	} else {
1753 		if (pi->caps_acp_pg)
1754 			amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_ACPPowerON);
1755 		kv_update_acp_dpm(adev, false);
1756 	}
1757 }
1758 
kv_set_valid_clock_range(struct amdgpu_device * adev,struct amdgpu_ps * new_rps)1759 static void kv_set_valid_clock_range(struct amdgpu_device *adev,
1760 				     struct amdgpu_ps *new_rps)
1761 {
1762 	struct kv_ps *new_ps = kv_get_ps(new_rps);
1763 	struct kv_power_info *pi = kv_get_pi(adev);
1764 	u32 i;
1765 	struct amdgpu_clock_voltage_dependency_table *table =
1766 		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1767 
1768 	if (table && table->count) {
1769 		for (i = 0; i < pi->graphics_dpm_level_count; i++) {
1770 			if ((table->entries[i].clk >= new_ps->levels[0].sclk) ||
1771 			    (i == (pi->graphics_dpm_level_count - 1))) {
1772 				pi->lowest_valid = i;
1773 				break;
1774 			}
1775 		}
1776 
1777 		for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
1778 			if (table->entries[i].clk <= new_ps->levels[new_ps->num_levels - 1].sclk)
1779 				break;
1780 		}
1781 		pi->highest_valid = i;
1782 
1783 		if (pi->lowest_valid > pi->highest_valid) {
1784 			if ((new_ps->levels[0].sclk - table->entries[pi->highest_valid].clk) >
1785 			    (table->entries[pi->lowest_valid].clk - new_ps->levels[new_ps->num_levels - 1].sclk))
1786 				pi->highest_valid = pi->lowest_valid;
1787 			else
1788 				pi->lowest_valid =  pi->highest_valid;
1789 		}
1790 	} else {
1791 		struct sumo_sclk_voltage_mapping_table *table =
1792 			&pi->sys_info.sclk_voltage_mapping_table;
1793 
1794 		for (i = 0; i < (int)pi->graphics_dpm_level_count; i++) {
1795 			if (table->entries[i].sclk_frequency >= new_ps->levels[0].sclk ||
1796 			    i == (int)(pi->graphics_dpm_level_count - 1)) {
1797 				pi->lowest_valid = i;
1798 				break;
1799 			}
1800 		}
1801 
1802 		for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
1803 			if (table->entries[i].sclk_frequency <=
1804 			    new_ps->levels[new_ps->num_levels - 1].sclk)
1805 				break;
1806 		}
1807 		pi->highest_valid = i;
1808 
1809 		if (pi->lowest_valid > pi->highest_valid) {
1810 			if ((new_ps->levels[0].sclk -
1811 			     table->entries[pi->highest_valid].sclk_frequency) >
1812 			    (table->entries[pi->lowest_valid].sclk_frequency -
1813 			     new_ps->levels[new_ps->num_levels -1].sclk))
1814 				pi->highest_valid = pi->lowest_valid;
1815 			else
1816 				pi->lowest_valid =  pi->highest_valid;
1817 		}
1818 	}
1819 }
1820 
kv_update_dfs_bypass_settings(struct amdgpu_device * adev,struct amdgpu_ps * new_rps)1821 static int kv_update_dfs_bypass_settings(struct amdgpu_device *adev,
1822 					 struct amdgpu_ps *new_rps)
1823 {
1824 	struct kv_ps *new_ps = kv_get_ps(new_rps);
1825 	struct kv_power_info *pi = kv_get_pi(adev);
1826 	int ret = 0;
1827 	u8 clk_bypass_cntl;
1828 
1829 	if (pi->caps_enable_dfs_bypass) {
1830 		clk_bypass_cntl = new_ps->need_dfs_bypass ?
1831 			pi->graphics_level[pi->graphics_boot_level].ClkBypassCntl : 0;
1832 		ret = amdgpu_kv_copy_bytes_to_smc(adev,
1833 					   (pi->dpm_table_start +
1834 					    offsetof(SMU7_Fusion_DpmTable, GraphicsLevel) +
1835 					    (pi->graphics_boot_level * sizeof(SMU7_Fusion_GraphicsLevel)) +
1836 					    offsetof(SMU7_Fusion_GraphicsLevel, ClkBypassCntl)),
1837 					   &clk_bypass_cntl,
1838 					   sizeof(u8), pi->sram_end);
1839 	}
1840 
1841 	return ret;
1842 }
1843 
kv_enable_nb_dpm(struct amdgpu_device * adev,bool enable)1844 static int kv_enable_nb_dpm(struct amdgpu_device *adev,
1845 			    bool enable)
1846 {
1847 	struct kv_power_info *pi = kv_get_pi(adev);
1848 	int ret = 0;
1849 
1850 	if (enable) {
1851 		if (pi->enable_nb_dpm && !pi->nb_dpm_enabled) {
1852 			ret = amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_NBDPM_Enable);
1853 			if (ret == 0)
1854 				pi->nb_dpm_enabled = true;
1855 		}
1856 	} else {
1857 		if (pi->enable_nb_dpm && pi->nb_dpm_enabled) {
1858 			ret = amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_NBDPM_Disable);
1859 			if (ret == 0)
1860 				pi->nb_dpm_enabled = false;
1861 		}
1862 	}
1863 
1864 	return ret;
1865 }
1866 
kv_dpm_force_performance_level(void * handle,enum amd_dpm_forced_level level)1867 static int kv_dpm_force_performance_level(void *handle,
1868 					  enum amd_dpm_forced_level level)
1869 {
1870 	int ret;
1871 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1872 
1873 	if (level == AMD_DPM_FORCED_LEVEL_HIGH) {
1874 		ret = kv_force_dpm_highest(adev);
1875 		if (ret)
1876 			return ret;
1877 	} else if (level == AMD_DPM_FORCED_LEVEL_LOW) {
1878 		ret = kv_force_dpm_lowest(adev);
1879 		if (ret)
1880 			return ret;
1881 	} else if (level == AMD_DPM_FORCED_LEVEL_AUTO) {
1882 		ret = kv_unforce_levels(adev);
1883 		if (ret)
1884 			return ret;
1885 	}
1886 
1887 	adev->pm.dpm.forced_level = level;
1888 
1889 	return 0;
1890 }
1891 
kv_dpm_pre_set_power_state(void * handle)1892 static int kv_dpm_pre_set_power_state(void *handle)
1893 {
1894 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1895 	struct kv_power_info *pi = kv_get_pi(adev);
1896 	struct amdgpu_ps requested_ps = *adev->pm.dpm.requested_ps;
1897 	struct amdgpu_ps *new_ps = &requested_ps;
1898 
1899 	kv_update_requested_ps(adev, new_ps);
1900 
1901 	kv_apply_state_adjust_rules(adev,
1902 				    &pi->requested_rps,
1903 				    &pi->current_rps);
1904 
1905 	return 0;
1906 }
1907 
kv_dpm_set_power_state(void * handle)1908 static int kv_dpm_set_power_state(void *handle)
1909 {
1910 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1911 	struct kv_power_info *pi = kv_get_pi(adev);
1912 	struct amdgpu_ps *new_ps = &pi->requested_rps;
1913 	struct amdgpu_ps *old_ps = &pi->current_rps;
1914 	int ret;
1915 
1916 	if (pi->bapm_enable) {
1917 		ret = amdgpu_kv_smc_bapm_enable(adev, adev->pm.ac_power);
1918 		if (ret) {
1919 			DRM_ERROR("amdgpu_kv_smc_bapm_enable failed\n");
1920 			return ret;
1921 		}
1922 	}
1923 
1924 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS) {
1925 		if (pi->enable_dpm) {
1926 			kv_set_valid_clock_range(adev, new_ps);
1927 			kv_update_dfs_bypass_settings(adev, new_ps);
1928 			ret = kv_calculate_ds_divider(adev);
1929 			if (ret) {
1930 				DRM_ERROR("kv_calculate_ds_divider failed\n");
1931 				return ret;
1932 			}
1933 			kv_calculate_nbps_level_settings(adev);
1934 			kv_calculate_dpm_settings(adev);
1935 			kv_force_lowest_valid(adev);
1936 			kv_enable_new_levels(adev);
1937 			kv_upload_dpm_settings(adev);
1938 			kv_program_nbps_index_settings(adev, new_ps);
1939 			kv_unforce_levels(adev);
1940 			kv_set_enabled_levels(adev);
1941 			kv_force_lowest_valid(adev);
1942 			kv_unforce_levels(adev);
1943 
1944 			ret = kv_update_vce_dpm(adev, new_ps, old_ps);
1945 			if (ret) {
1946 				DRM_ERROR("kv_update_vce_dpm failed\n");
1947 				return ret;
1948 			}
1949 			kv_update_sclk_t(adev);
1950 			if (adev->asic_type == CHIP_MULLINS)
1951 				kv_enable_nb_dpm(adev, true);
1952 		}
1953 	} else {
1954 		if (pi->enable_dpm) {
1955 			kv_set_valid_clock_range(adev, new_ps);
1956 			kv_update_dfs_bypass_settings(adev, new_ps);
1957 			ret = kv_calculate_ds_divider(adev);
1958 			if (ret) {
1959 				DRM_ERROR("kv_calculate_ds_divider failed\n");
1960 				return ret;
1961 			}
1962 			kv_calculate_nbps_level_settings(adev);
1963 			kv_calculate_dpm_settings(adev);
1964 			kv_freeze_sclk_dpm(adev, true);
1965 			kv_upload_dpm_settings(adev);
1966 			kv_program_nbps_index_settings(adev, new_ps);
1967 			kv_freeze_sclk_dpm(adev, false);
1968 			kv_set_enabled_levels(adev);
1969 			ret = kv_update_vce_dpm(adev, new_ps, old_ps);
1970 			if (ret) {
1971 				DRM_ERROR("kv_update_vce_dpm failed\n");
1972 				return ret;
1973 			}
1974 			kv_update_acp_boot_level(adev);
1975 			kv_update_sclk_t(adev);
1976 			kv_enable_nb_dpm(adev, true);
1977 		}
1978 	}
1979 
1980 	return 0;
1981 }
1982 
kv_dpm_post_set_power_state(void * handle)1983 static void kv_dpm_post_set_power_state(void *handle)
1984 {
1985 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1986 	struct kv_power_info *pi = kv_get_pi(adev);
1987 	struct amdgpu_ps *new_ps = &pi->requested_rps;
1988 
1989 	kv_update_current_ps(adev, new_ps);
1990 }
1991 
kv_dpm_setup_asic(struct amdgpu_device * adev)1992 static void kv_dpm_setup_asic(struct amdgpu_device *adev)
1993 {
1994 	sumo_take_smu_control(adev, true);
1995 	kv_init_powergate_state(adev);
1996 	kv_init_sclk_t(adev);
1997 }
1998 
1999 #if 0
2000 static void kv_dpm_reset_asic(struct amdgpu_device *adev)
2001 {
2002 	struct kv_power_info *pi = kv_get_pi(adev);
2003 
2004 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS) {
2005 		kv_force_lowest_valid(adev);
2006 		kv_init_graphics_levels(adev);
2007 		kv_program_bootup_state(adev);
2008 		kv_upload_dpm_settings(adev);
2009 		kv_force_lowest_valid(adev);
2010 		kv_unforce_levels(adev);
2011 	} else {
2012 		kv_init_graphics_levels(adev);
2013 		kv_program_bootup_state(adev);
2014 		kv_freeze_sclk_dpm(adev, true);
2015 		kv_upload_dpm_settings(adev);
2016 		kv_freeze_sclk_dpm(adev, false);
2017 		kv_set_enabled_level(adev, pi->graphics_boot_level);
2018 	}
2019 }
2020 #endif
2021 
kv_construct_max_power_limits_table(struct amdgpu_device * adev,struct amdgpu_clock_and_voltage_limits * table)2022 static void kv_construct_max_power_limits_table(struct amdgpu_device *adev,
2023 						struct amdgpu_clock_and_voltage_limits *table)
2024 {
2025 	struct kv_power_info *pi = kv_get_pi(adev);
2026 
2027 	if (pi->sys_info.sclk_voltage_mapping_table.num_max_dpm_entries > 0) {
2028 		int idx = pi->sys_info.sclk_voltage_mapping_table.num_max_dpm_entries - 1;
2029 		table->sclk =
2030 			pi->sys_info.sclk_voltage_mapping_table.entries[idx].sclk_frequency;
2031 		table->vddc =
2032 			kv_convert_2bit_index_to_voltage(adev,
2033 							 pi->sys_info.sclk_voltage_mapping_table.entries[idx].vid_2bit);
2034 	}
2035 
2036 	table->mclk = pi->sys_info.nbp_memory_clock[0];
2037 }
2038 
kv_patch_voltage_values(struct amdgpu_device * adev)2039 static void kv_patch_voltage_values(struct amdgpu_device *adev)
2040 {
2041 	int i;
2042 	struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
2043 		&adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
2044 	struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
2045 		&adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
2046 	struct amdgpu_clock_voltage_dependency_table *samu_table =
2047 		&adev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
2048 	struct amdgpu_clock_voltage_dependency_table *acp_table =
2049 		&adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
2050 
2051 	if (uvd_table->count) {
2052 		for (i = 0; i < uvd_table->count; i++)
2053 			uvd_table->entries[i].v =
2054 				kv_convert_8bit_index_to_voltage(adev,
2055 								 uvd_table->entries[i].v);
2056 	}
2057 
2058 	if (vce_table->count) {
2059 		for (i = 0; i < vce_table->count; i++)
2060 			vce_table->entries[i].v =
2061 				kv_convert_8bit_index_to_voltage(adev,
2062 								 vce_table->entries[i].v);
2063 	}
2064 
2065 	if (samu_table->count) {
2066 		for (i = 0; i < samu_table->count; i++)
2067 			samu_table->entries[i].v =
2068 				kv_convert_8bit_index_to_voltage(adev,
2069 								 samu_table->entries[i].v);
2070 	}
2071 
2072 	if (acp_table->count) {
2073 		for (i = 0; i < acp_table->count; i++)
2074 			acp_table->entries[i].v =
2075 				kv_convert_8bit_index_to_voltage(adev,
2076 								 acp_table->entries[i].v);
2077 	}
2078 
2079 }
2080 
kv_construct_boot_state(struct amdgpu_device * adev)2081 static void kv_construct_boot_state(struct amdgpu_device *adev)
2082 {
2083 	struct kv_power_info *pi = kv_get_pi(adev);
2084 
2085 	pi->boot_pl.sclk = pi->sys_info.bootup_sclk;
2086 	pi->boot_pl.vddc_index = pi->sys_info.bootup_nb_voltage_index;
2087 	pi->boot_pl.ds_divider_index = 0;
2088 	pi->boot_pl.ss_divider_index = 0;
2089 	pi->boot_pl.allow_gnb_slow = 1;
2090 	pi->boot_pl.force_nbp_state = 0;
2091 	pi->boot_pl.display_wm = 0;
2092 	pi->boot_pl.vce_wm = 0;
2093 }
2094 
kv_force_dpm_highest(struct amdgpu_device * adev)2095 static int kv_force_dpm_highest(struct amdgpu_device *adev)
2096 {
2097 	int ret;
2098 	u32 enable_mask, i;
2099 
2100 	ret = amdgpu_kv_dpm_get_enable_mask(adev, &enable_mask);
2101 	if (ret)
2102 		return ret;
2103 
2104 	for (i = SMU7_MAX_LEVELS_GRAPHICS - 1; i > 0; i--) {
2105 		if (enable_mask & (1 << i))
2106 			break;
2107 	}
2108 
2109 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
2110 		return amdgpu_kv_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DPM_ForceState, i);
2111 	else
2112 		return kv_set_enabled_level(adev, i);
2113 }
2114 
kv_force_dpm_lowest(struct amdgpu_device * adev)2115 static int kv_force_dpm_lowest(struct amdgpu_device *adev)
2116 {
2117 	int ret;
2118 	u32 enable_mask, i;
2119 
2120 	ret = amdgpu_kv_dpm_get_enable_mask(adev, &enable_mask);
2121 	if (ret)
2122 		return ret;
2123 
2124 	for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++) {
2125 		if (enable_mask & (1 << i))
2126 			break;
2127 	}
2128 
2129 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
2130 		return amdgpu_kv_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DPM_ForceState, i);
2131 	else
2132 		return kv_set_enabled_level(adev, i);
2133 }
2134 
kv_get_sleep_divider_id_from_clock(struct amdgpu_device * adev,u32 sclk,u32 min_sclk_in_sr)2135 static u8 kv_get_sleep_divider_id_from_clock(struct amdgpu_device *adev,
2136 					     u32 sclk, u32 min_sclk_in_sr)
2137 {
2138 	struct kv_power_info *pi = kv_get_pi(adev);
2139 	u32 i;
2140 	u32 temp;
2141 	u32 min = max(min_sclk_in_sr, (u32)KV_MINIMUM_ENGINE_CLOCK);
2142 
2143 	if (sclk < min)
2144 		return 0;
2145 
2146 	if (!pi->caps_sclk_ds)
2147 		return 0;
2148 
2149 	for (i = KV_MAX_DEEPSLEEP_DIVIDER_ID; i > 0; i--) {
2150 		temp = sclk >> i;
2151 		if (temp >= min)
2152 			break;
2153 	}
2154 
2155 	return (u8)i;
2156 }
2157 
kv_get_high_voltage_limit(struct amdgpu_device * adev,int * limit)2158 static int kv_get_high_voltage_limit(struct amdgpu_device *adev, int *limit)
2159 {
2160 	struct kv_power_info *pi = kv_get_pi(adev);
2161 	struct amdgpu_clock_voltage_dependency_table *table =
2162 		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
2163 	int i;
2164 
2165 	if (table && table->count) {
2166 		for (i = table->count - 1; i >= 0; i--) {
2167 			if (pi->high_voltage_t &&
2168 			    (kv_convert_8bit_index_to_voltage(adev, table->entries[i].v) <=
2169 			     pi->high_voltage_t)) {
2170 				*limit = i;
2171 				return 0;
2172 			}
2173 		}
2174 	} else {
2175 		struct sumo_sclk_voltage_mapping_table *table =
2176 			&pi->sys_info.sclk_voltage_mapping_table;
2177 
2178 		for (i = table->num_max_dpm_entries - 1; i >= 0; i--) {
2179 			if (pi->high_voltage_t &&
2180 			    (kv_convert_2bit_index_to_voltage(adev, table->entries[i].vid_2bit) <=
2181 			     pi->high_voltage_t)) {
2182 				*limit = i;
2183 				return 0;
2184 			}
2185 		}
2186 	}
2187 
2188 	*limit = 0;
2189 	return 0;
2190 }
2191 
kv_apply_state_adjust_rules(struct amdgpu_device * adev,struct amdgpu_ps * new_rps,struct amdgpu_ps * old_rps)2192 static void kv_apply_state_adjust_rules(struct amdgpu_device *adev,
2193 					struct amdgpu_ps *new_rps,
2194 					struct amdgpu_ps *old_rps)
2195 {
2196 	struct kv_ps *ps = kv_get_ps(new_rps);
2197 	struct kv_power_info *pi = kv_get_pi(adev);
2198 	u32 min_sclk = 10000; /* ??? */
2199 	u32 sclk, mclk = 0;
2200 	int i, limit;
2201 	bool force_high;
2202 	struct amdgpu_clock_voltage_dependency_table *table =
2203 		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
2204 	u32 stable_p_state_sclk = 0;
2205 	struct amdgpu_clock_and_voltage_limits *max_limits =
2206 		&adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
2207 
2208 	if (new_rps->vce_active) {
2209 		new_rps->evclk = adev->pm.dpm.vce_states[adev->pm.dpm.vce_level].evclk;
2210 		new_rps->ecclk = adev->pm.dpm.vce_states[adev->pm.dpm.vce_level].ecclk;
2211 	} else {
2212 		new_rps->evclk = 0;
2213 		new_rps->ecclk = 0;
2214 	}
2215 
2216 	mclk = max_limits->mclk;
2217 	sclk = min_sclk;
2218 
2219 	if (pi->caps_stable_p_state) {
2220 		stable_p_state_sclk = (max_limits->sclk * 75) / 100;
2221 
2222 		for (i = table->count - 1; i >= 0; i--) {
2223 			if (stable_p_state_sclk >= table->entries[i].clk) {
2224 				stable_p_state_sclk = table->entries[i].clk;
2225 				break;
2226 			}
2227 		}
2228 
2229 		if (i > 0)
2230 			stable_p_state_sclk = table->entries[0].clk;
2231 
2232 		sclk = stable_p_state_sclk;
2233 	}
2234 
2235 	if (new_rps->vce_active) {
2236 		if (sclk < adev->pm.dpm.vce_states[adev->pm.dpm.vce_level].sclk)
2237 			sclk = adev->pm.dpm.vce_states[adev->pm.dpm.vce_level].sclk;
2238 	}
2239 
2240 	ps->need_dfs_bypass = true;
2241 
2242 	for (i = 0; i < ps->num_levels; i++) {
2243 		if (ps->levels[i].sclk < sclk)
2244 			ps->levels[i].sclk = sclk;
2245 	}
2246 
2247 	if (table && table->count) {
2248 		for (i = 0; i < ps->num_levels; i++) {
2249 			if (pi->high_voltage_t &&
2250 			    (pi->high_voltage_t <
2251 			     kv_convert_8bit_index_to_voltage(adev, ps->levels[i].vddc_index))) {
2252 				kv_get_high_voltage_limit(adev, &limit);
2253 				ps->levels[i].sclk = table->entries[limit].clk;
2254 			}
2255 		}
2256 	} else {
2257 		struct sumo_sclk_voltage_mapping_table *table =
2258 			&pi->sys_info.sclk_voltage_mapping_table;
2259 
2260 		for (i = 0; i < ps->num_levels; i++) {
2261 			if (pi->high_voltage_t &&
2262 			    (pi->high_voltage_t <
2263 			     kv_convert_8bit_index_to_voltage(adev, ps->levels[i].vddc_index))) {
2264 				kv_get_high_voltage_limit(adev, &limit);
2265 				ps->levels[i].sclk = table->entries[limit].sclk_frequency;
2266 			}
2267 		}
2268 	}
2269 
2270 	if (pi->caps_stable_p_state) {
2271 		for (i = 0; i < ps->num_levels; i++) {
2272 			ps->levels[i].sclk = stable_p_state_sclk;
2273 		}
2274 	}
2275 
2276 	pi->video_start = new_rps->dclk || new_rps->vclk ||
2277 		new_rps->evclk || new_rps->ecclk;
2278 
2279 	if ((new_rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
2280 	    ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
2281 		pi->battery_state = true;
2282 	else
2283 		pi->battery_state = false;
2284 
2285 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS) {
2286 		ps->dpm0_pg_nb_ps_lo = 0x1;
2287 		ps->dpm0_pg_nb_ps_hi = 0x0;
2288 		ps->dpmx_nb_ps_lo = 0x1;
2289 		ps->dpmx_nb_ps_hi = 0x0;
2290 	} else {
2291 		ps->dpm0_pg_nb_ps_lo = 0x3;
2292 		ps->dpm0_pg_nb_ps_hi = 0x0;
2293 		ps->dpmx_nb_ps_lo = 0x3;
2294 		ps->dpmx_nb_ps_hi = 0x0;
2295 
2296 		if (pi->sys_info.nb_dpm_enable) {
2297 			force_high = (mclk >= pi->sys_info.nbp_memory_clock[3]) ||
2298 				pi->video_start || (adev->pm.dpm.new_active_crtc_count >= 3) ||
2299 				pi->disable_nb_ps3_in_battery;
2300 			ps->dpm0_pg_nb_ps_lo = force_high ? 0x2 : 0x3;
2301 			ps->dpm0_pg_nb_ps_hi = 0x2;
2302 			ps->dpmx_nb_ps_lo = force_high ? 0x2 : 0x3;
2303 			ps->dpmx_nb_ps_hi = 0x2;
2304 		}
2305 	}
2306 }
2307 
kv_dpm_power_level_enabled_for_throttle(struct amdgpu_device * adev,u32 index,bool enable)2308 static void kv_dpm_power_level_enabled_for_throttle(struct amdgpu_device *adev,
2309 						    u32 index, bool enable)
2310 {
2311 	struct kv_power_info *pi = kv_get_pi(adev);
2312 
2313 	pi->graphics_level[index].EnabledForThrottle = enable ? 1 : 0;
2314 }
2315 
kv_calculate_ds_divider(struct amdgpu_device * adev)2316 static int kv_calculate_ds_divider(struct amdgpu_device *adev)
2317 {
2318 	struct kv_power_info *pi = kv_get_pi(adev);
2319 	u32 sclk_in_sr = 10000; /* ??? */
2320 	u32 i;
2321 
2322 	if (pi->lowest_valid > pi->highest_valid)
2323 		return -EINVAL;
2324 
2325 	for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2326 		pi->graphics_level[i].DeepSleepDivId =
2327 			kv_get_sleep_divider_id_from_clock(adev,
2328 							   be32_to_cpu(pi->graphics_level[i].SclkFrequency),
2329 							   sclk_in_sr);
2330 	}
2331 	return 0;
2332 }
2333 
kv_calculate_nbps_level_settings(struct amdgpu_device * adev)2334 static int kv_calculate_nbps_level_settings(struct amdgpu_device *adev)
2335 {
2336 	struct kv_power_info *pi = kv_get_pi(adev);
2337 	u32 i;
2338 	bool force_high;
2339 	struct amdgpu_clock_and_voltage_limits *max_limits =
2340 		&adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
2341 	u32 mclk = max_limits->mclk;
2342 
2343 	if (pi->lowest_valid > pi->highest_valid)
2344 		return -EINVAL;
2345 
2346 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS) {
2347 		for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2348 			pi->graphics_level[i].GnbSlow = 1;
2349 			pi->graphics_level[i].ForceNbPs1 = 0;
2350 			pi->graphics_level[i].UpH = 0;
2351 		}
2352 
2353 		if (!pi->sys_info.nb_dpm_enable)
2354 			return 0;
2355 
2356 		force_high = ((mclk >= pi->sys_info.nbp_memory_clock[3]) ||
2357 			      (adev->pm.dpm.new_active_crtc_count >= 3) || pi->video_start);
2358 
2359 		if (force_high) {
2360 			for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2361 				pi->graphics_level[i].GnbSlow = 0;
2362 		} else {
2363 			if (pi->battery_state)
2364 				pi->graphics_level[0].ForceNbPs1 = 1;
2365 
2366 			pi->graphics_level[1].GnbSlow = 0;
2367 			pi->graphics_level[2].GnbSlow = 0;
2368 			pi->graphics_level[3].GnbSlow = 0;
2369 			pi->graphics_level[4].GnbSlow = 0;
2370 		}
2371 	} else {
2372 		for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2373 			pi->graphics_level[i].GnbSlow = 1;
2374 			pi->graphics_level[i].ForceNbPs1 = 0;
2375 			pi->graphics_level[i].UpH = 0;
2376 		}
2377 
2378 		if (pi->sys_info.nb_dpm_enable && pi->battery_state) {
2379 			pi->graphics_level[pi->lowest_valid].UpH = 0x28;
2380 			pi->graphics_level[pi->lowest_valid].GnbSlow = 0;
2381 			if (pi->lowest_valid != pi->highest_valid)
2382 				pi->graphics_level[pi->lowest_valid].ForceNbPs1 = 1;
2383 		}
2384 	}
2385 	return 0;
2386 }
2387 
kv_calculate_dpm_settings(struct amdgpu_device * adev)2388 static int kv_calculate_dpm_settings(struct amdgpu_device *adev)
2389 {
2390 	struct kv_power_info *pi = kv_get_pi(adev);
2391 	u32 i;
2392 
2393 	if (pi->lowest_valid > pi->highest_valid)
2394 		return -EINVAL;
2395 
2396 	for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2397 		pi->graphics_level[i].DisplayWatermark = (i == pi->highest_valid) ? 1 : 0;
2398 
2399 	return 0;
2400 }
2401 
kv_init_graphics_levels(struct amdgpu_device * adev)2402 static void kv_init_graphics_levels(struct amdgpu_device *adev)
2403 {
2404 	struct kv_power_info *pi = kv_get_pi(adev);
2405 	u32 i;
2406 	struct amdgpu_clock_voltage_dependency_table *table =
2407 		&adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
2408 
2409 	if (table && table->count) {
2410 		u32 vid_2bit;
2411 
2412 		pi->graphics_dpm_level_count = 0;
2413 		for (i = 0; i < table->count; i++) {
2414 			if (pi->high_voltage_t &&
2415 			    (pi->high_voltage_t <
2416 			     kv_convert_8bit_index_to_voltage(adev, table->entries[i].v)))
2417 				break;
2418 
2419 			kv_set_divider_value(adev, i, table->entries[i].clk);
2420 			vid_2bit = kv_convert_vid7_to_vid2(adev,
2421 							   &pi->sys_info.vid_mapping_table,
2422 							   table->entries[i].v);
2423 			kv_set_vid(adev, i, vid_2bit);
2424 			kv_set_at(adev, i, pi->at[i]);
2425 			kv_dpm_power_level_enabled_for_throttle(adev, i, true);
2426 			pi->graphics_dpm_level_count++;
2427 		}
2428 	} else {
2429 		struct sumo_sclk_voltage_mapping_table *table =
2430 			&pi->sys_info.sclk_voltage_mapping_table;
2431 
2432 		pi->graphics_dpm_level_count = 0;
2433 		for (i = 0; i < table->num_max_dpm_entries; i++) {
2434 			if (pi->high_voltage_t &&
2435 			    pi->high_voltage_t <
2436 			    kv_convert_2bit_index_to_voltage(adev, table->entries[i].vid_2bit))
2437 				break;
2438 
2439 			kv_set_divider_value(adev, i, table->entries[i].sclk_frequency);
2440 			kv_set_vid(adev, i, table->entries[i].vid_2bit);
2441 			kv_set_at(adev, i, pi->at[i]);
2442 			kv_dpm_power_level_enabled_for_throttle(adev, i, true);
2443 			pi->graphics_dpm_level_count++;
2444 		}
2445 	}
2446 
2447 	for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++)
2448 		kv_dpm_power_level_enable(adev, i, false);
2449 }
2450 
kv_enable_new_levels(struct amdgpu_device * adev)2451 static void kv_enable_new_levels(struct amdgpu_device *adev)
2452 {
2453 	struct kv_power_info *pi = kv_get_pi(adev);
2454 	u32 i;
2455 
2456 	for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++) {
2457 		if (i >= pi->lowest_valid && i <= pi->highest_valid)
2458 			kv_dpm_power_level_enable(adev, i, true);
2459 	}
2460 }
2461 
kv_set_enabled_level(struct amdgpu_device * adev,u32 level)2462 static int kv_set_enabled_level(struct amdgpu_device *adev, u32 level)
2463 {
2464 	u32 new_mask = (1 << level);
2465 
2466 	return amdgpu_kv_send_msg_to_smc_with_parameter(adev,
2467 						 PPSMC_MSG_SCLKDPM_SetEnabledMask,
2468 						 new_mask);
2469 }
2470 
kv_set_enabled_levels(struct amdgpu_device * adev)2471 static int kv_set_enabled_levels(struct amdgpu_device *adev)
2472 {
2473 	struct kv_power_info *pi = kv_get_pi(adev);
2474 	u32 i, new_mask = 0;
2475 
2476 	for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2477 		new_mask |= (1 << i);
2478 
2479 	return amdgpu_kv_send_msg_to_smc_with_parameter(adev,
2480 						 PPSMC_MSG_SCLKDPM_SetEnabledMask,
2481 						 new_mask);
2482 }
2483 
kv_program_nbps_index_settings(struct amdgpu_device * adev,struct amdgpu_ps * new_rps)2484 static void kv_program_nbps_index_settings(struct amdgpu_device *adev,
2485 					   struct amdgpu_ps *new_rps)
2486 {
2487 	struct kv_ps *new_ps = kv_get_ps(new_rps);
2488 	struct kv_power_info *pi = kv_get_pi(adev);
2489 	u32 nbdpmconfig1;
2490 
2491 	if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
2492 		return;
2493 
2494 	if (pi->sys_info.nb_dpm_enable) {
2495 		nbdpmconfig1 = RREG32_SMC(ixNB_DPM_CONFIG_1);
2496 		nbdpmconfig1 &= ~(NB_DPM_CONFIG_1__Dpm0PgNbPsLo_MASK |
2497 				NB_DPM_CONFIG_1__Dpm0PgNbPsHi_MASK |
2498 				NB_DPM_CONFIG_1__DpmXNbPsLo_MASK |
2499 				NB_DPM_CONFIG_1__DpmXNbPsHi_MASK);
2500 		nbdpmconfig1 |= (new_ps->dpm0_pg_nb_ps_lo << NB_DPM_CONFIG_1__Dpm0PgNbPsLo__SHIFT) |
2501 				(new_ps->dpm0_pg_nb_ps_hi << NB_DPM_CONFIG_1__Dpm0PgNbPsHi__SHIFT) |
2502 				(new_ps->dpmx_nb_ps_lo << NB_DPM_CONFIG_1__DpmXNbPsLo__SHIFT) |
2503 				(new_ps->dpmx_nb_ps_hi << NB_DPM_CONFIG_1__DpmXNbPsHi__SHIFT);
2504 		WREG32_SMC(ixNB_DPM_CONFIG_1, nbdpmconfig1);
2505 	}
2506 }
2507 
kv_set_thermal_temperature_range(struct amdgpu_device * adev,int min_temp,int max_temp)2508 static int kv_set_thermal_temperature_range(struct amdgpu_device *adev,
2509 					    int min_temp, int max_temp)
2510 {
2511 	int low_temp = 0 * 1000;
2512 	int high_temp = 255 * 1000;
2513 	u32 tmp;
2514 
2515 	if (low_temp < min_temp)
2516 		low_temp = min_temp;
2517 	if (high_temp > max_temp)
2518 		high_temp = max_temp;
2519 	if (high_temp < low_temp) {
2520 		DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
2521 		return -EINVAL;
2522 	}
2523 
2524 	tmp = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
2525 	tmp &= ~(CG_THERMAL_INT_CTRL__DIG_THERM_INTH_MASK |
2526 		CG_THERMAL_INT_CTRL__DIG_THERM_INTL_MASK);
2527 	tmp |= ((49 + (high_temp / 1000)) << CG_THERMAL_INT_CTRL__DIG_THERM_INTH__SHIFT) |
2528 		((49 + (low_temp / 1000)) << CG_THERMAL_INT_CTRL__DIG_THERM_INTL__SHIFT);
2529 	WREG32_SMC(ixCG_THERMAL_INT_CTRL, tmp);
2530 
2531 	adev->pm.dpm.thermal.min_temp = low_temp;
2532 	adev->pm.dpm.thermal.max_temp = high_temp;
2533 
2534 	return 0;
2535 }
2536 
2537 union igp_info {
2538 	struct _ATOM_INTEGRATED_SYSTEM_INFO info;
2539 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
2540 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V5 info_5;
2541 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
2542 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
2543 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
2544 };
2545 
kv_parse_sys_info_table(struct amdgpu_device * adev)2546 static int kv_parse_sys_info_table(struct amdgpu_device *adev)
2547 {
2548 	struct kv_power_info *pi = kv_get_pi(adev);
2549 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
2550 	int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
2551 	union igp_info *igp_info;
2552 	u8 frev, crev;
2553 	u16 data_offset;
2554 	int i;
2555 
2556 	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
2557 				   &frev, &crev, &data_offset)) {
2558 		igp_info = (union igp_info *)(mode_info->atom_context->bios +
2559 					      data_offset);
2560 
2561 		if (crev != 8) {
2562 			DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
2563 			return -EINVAL;
2564 		}
2565 		pi->sys_info.bootup_sclk = le32_to_cpu(igp_info->info_8.ulBootUpEngineClock);
2566 		pi->sys_info.bootup_uma_clk = le32_to_cpu(igp_info->info_8.ulBootUpUMAClock);
2567 		pi->sys_info.bootup_nb_voltage_index =
2568 			le16_to_cpu(igp_info->info_8.usBootUpNBVoltage);
2569 		if (igp_info->info_8.ucHtcTmpLmt == 0)
2570 			pi->sys_info.htc_tmp_lmt = 203;
2571 		else
2572 			pi->sys_info.htc_tmp_lmt = igp_info->info_8.ucHtcTmpLmt;
2573 		if (igp_info->info_8.ucHtcHystLmt == 0)
2574 			pi->sys_info.htc_hyst_lmt = 5;
2575 		else
2576 			pi->sys_info.htc_hyst_lmt = igp_info->info_8.ucHtcHystLmt;
2577 		if (pi->sys_info.htc_tmp_lmt <= pi->sys_info.htc_hyst_lmt) {
2578 			DRM_ERROR("The htcTmpLmt should be larger than htcHystLmt.\n");
2579 		}
2580 
2581 		if (le32_to_cpu(igp_info->info_8.ulSystemConfig) & (1 << 3))
2582 			pi->sys_info.nb_dpm_enable = true;
2583 		else
2584 			pi->sys_info.nb_dpm_enable = false;
2585 
2586 		for (i = 0; i < KV_NUM_NBPSTATES; i++) {
2587 			pi->sys_info.nbp_memory_clock[i] =
2588 				le32_to_cpu(igp_info->info_8.ulNbpStateMemclkFreq[i]);
2589 			pi->sys_info.nbp_n_clock[i] =
2590 				le32_to_cpu(igp_info->info_8.ulNbpStateNClkFreq[i]);
2591 		}
2592 		if (le32_to_cpu(igp_info->info_8.ulGPUCapInfo) &
2593 		    SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS)
2594 			pi->caps_enable_dfs_bypass = true;
2595 
2596 		sumo_construct_sclk_voltage_mapping_table(adev,
2597 							  &pi->sys_info.sclk_voltage_mapping_table,
2598 							  igp_info->info_8.sAvail_SCLK);
2599 
2600 		sumo_construct_vid_mapping_table(adev,
2601 						 &pi->sys_info.vid_mapping_table,
2602 						 igp_info->info_8.sAvail_SCLK);
2603 
2604 		kv_construct_max_power_limits_table(adev,
2605 						    &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
2606 	}
2607 	return 0;
2608 }
2609 
2610 union power_info {
2611 	struct _ATOM_POWERPLAY_INFO info;
2612 	struct _ATOM_POWERPLAY_INFO_V2 info_2;
2613 	struct _ATOM_POWERPLAY_INFO_V3 info_3;
2614 	struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
2615 	struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
2616 	struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
2617 };
2618 
2619 union pplib_clock_info {
2620 	struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
2621 	struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
2622 	struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
2623 	struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
2624 };
2625 
2626 union pplib_power_state {
2627 	struct _ATOM_PPLIB_STATE v1;
2628 	struct _ATOM_PPLIB_STATE_V2 v2;
2629 };
2630 
kv_patch_boot_state(struct amdgpu_device * adev,struct kv_ps * ps)2631 static void kv_patch_boot_state(struct amdgpu_device *adev,
2632 				struct kv_ps *ps)
2633 {
2634 	struct kv_power_info *pi = kv_get_pi(adev);
2635 
2636 	ps->num_levels = 1;
2637 	ps->levels[0] = pi->boot_pl;
2638 }
2639 
kv_parse_pplib_non_clock_info(struct amdgpu_device * adev,struct amdgpu_ps * rps,struct _ATOM_PPLIB_NONCLOCK_INFO * non_clock_info,u8 table_rev)2640 static void kv_parse_pplib_non_clock_info(struct amdgpu_device *adev,
2641 					  struct amdgpu_ps *rps,
2642 					  struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
2643 					  u8 table_rev)
2644 {
2645 	struct kv_ps *ps = kv_get_ps(rps);
2646 
2647 	rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
2648 	rps->class = le16_to_cpu(non_clock_info->usClassification);
2649 	rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
2650 
2651 	if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
2652 		rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
2653 		rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
2654 	} else {
2655 		rps->vclk = 0;
2656 		rps->dclk = 0;
2657 	}
2658 
2659 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
2660 		adev->pm.dpm.boot_ps = rps;
2661 		kv_patch_boot_state(adev, ps);
2662 	}
2663 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
2664 		adev->pm.dpm.uvd_ps = rps;
2665 }
2666 
kv_parse_pplib_clock_info(struct amdgpu_device * adev,struct amdgpu_ps * rps,int index,union pplib_clock_info * clock_info)2667 static void kv_parse_pplib_clock_info(struct amdgpu_device *adev,
2668 				      struct amdgpu_ps *rps, int index,
2669 					union pplib_clock_info *clock_info)
2670 {
2671 	struct kv_power_info *pi = kv_get_pi(adev);
2672 	struct kv_ps *ps = kv_get_ps(rps);
2673 	struct kv_pl *pl = &ps->levels[index];
2674 	u32 sclk;
2675 
2676 	sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
2677 	sclk |= clock_info->sumo.ucEngineClockHigh << 16;
2678 	pl->sclk = sclk;
2679 	pl->vddc_index = clock_info->sumo.vddcIndex;
2680 
2681 	ps->num_levels = index + 1;
2682 
2683 	if (pi->caps_sclk_ds) {
2684 		pl->ds_divider_index = 5;
2685 		pl->ss_divider_index = 5;
2686 	}
2687 }
2688 
kv_parse_power_table(struct amdgpu_device * adev)2689 static int kv_parse_power_table(struct amdgpu_device *adev)
2690 {
2691 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
2692 	struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
2693 	union pplib_power_state *power_state;
2694 	int i, j, k, non_clock_array_index, clock_array_index;
2695 	union pplib_clock_info *clock_info;
2696 	struct _StateArray *state_array;
2697 	struct _ClockInfoArray *clock_info_array;
2698 	struct _NonClockInfoArray *non_clock_info_array;
2699 	union power_info *power_info;
2700 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2701 	u16 data_offset;
2702 	u8 frev, crev;
2703 	u8 *power_state_offset;
2704 	struct kv_ps *ps;
2705 
2706 	if (!amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
2707 				   &frev, &crev, &data_offset))
2708 		return -EINVAL;
2709 	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
2710 
2711 	amdgpu_add_thermal_controller(adev);
2712 
2713 	state_array = (struct _StateArray *)
2714 		(mode_info->atom_context->bios + data_offset +
2715 		 le16_to_cpu(power_info->pplib.usStateArrayOffset));
2716 	clock_info_array = (struct _ClockInfoArray *)
2717 		(mode_info->atom_context->bios + data_offset +
2718 		 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
2719 	non_clock_info_array = (struct _NonClockInfoArray *)
2720 		(mode_info->atom_context->bios + data_offset +
2721 		 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
2722 
2723 	adev->pm.dpm.ps = kcalloc(state_array->ucNumEntries,
2724 				  sizeof(struct amdgpu_ps),
2725 				  GFP_KERNEL);
2726 	if (!adev->pm.dpm.ps)
2727 		return -ENOMEM;
2728 	power_state_offset = (u8 *)state_array->states;
2729 	for (i = 0; i < state_array->ucNumEntries; i++) {
2730 		u8 *idx;
2731 		power_state = (union pplib_power_state *)power_state_offset;
2732 		non_clock_array_index = power_state->v2.nonClockInfoIndex;
2733 		non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
2734 			&non_clock_info_array->nonClockInfo[non_clock_array_index];
2735 		ps = kzalloc(sizeof(struct kv_ps), GFP_KERNEL);
2736 		if (ps == NULL) {
2737 			kfree(adev->pm.dpm.ps);
2738 			return -ENOMEM;
2739 		}
2740 		adev->pm.dpm.ps[i].ps_priv = ps;
2741 		k = 0;
2742 		idx = (u8 *)&power_state->v2.clockInfoIndex[0];
2743 		for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
2744 			clock_array_index = idx[j];
2745 			if (clock_array_index >= clock_info_array->ucNumEntries)
2746 				continue;
2747 			if (k >= SUMO_MAX_HARDWARE_POWERLEVELS)
2748 				break;
2749 			clock_info = (union pplib_clock_info *)
2750 				((u8 *)&clock_info_array->clockInfo[0] +
2751 				 (clock_array_index * clock_info_array->ucEntrySize));
2752 			kv_parse_pplib_clock_info(adev,
2753 						  &adev->pm.dpm.ps[i], k,
2754 						  clock_info);
2755 			k++;
2756 		}
2757 		kv_parse_pplib_non_clock_info(adev, &adev->pm.dpm.ps[i],
2758 					      non_clock_info,
2759 					      non_clock_info_array->ucEntrySize);
2760 		power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
2761 	}
2762 	adev->pm.dpm.num_ps = state_array->ucNumEntries;
2763 
2764 	/* fill in the vce power states */
2765 	for (i = 0; i < adev->pm.dpm.num_of_vce_states; i++) {
2766 		u32 sclk;
2767 		clock_array_index = adev->pm.dpm.vce_states[i].clk_idx;
2768 		clock_info = (union pplib_clock_info *)
2769 			&clock_info_array->clockInfo[clock_array_index * clock_info_array->ucEntrySize];
2770 		sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
2771 		sclk |= clock_info->sumo.ucEngineClockHigh << 16;
2772 		adev->pm.dpm.vce_states[i].sclk = sclk;
2773 		adev->pm.dpm.vce_states[i].mclk = 0;
2774 	}
2775 
2776 	return 0;
2777 }
2778 
kv_dpm_init(struct amdgpu_device * adev)2779 static int kv_dpm_init(struct amdgpu_device *adev)
2780 {
2781 	struct kv_power_info *pi;
2782 	int ret, i;
2783 
2784 	pi = kzalloc(sizeof(struct kv_power_info), GFP_KERNEL);
2785 	if (pi == NULL)
2786 		return -ENOMEM;
2787 	adev->pm.dpm.priv = pi;
2788 
2789 	ret = amdgpu_get_platform_caps(adev);
2790 	if (ret)
2791 		return ret;
2792 
2793 	ret = amdgpu_parse_extended_power_table(adev);
2794 	if (ret)
2795 		return ret;
2796 
2797 	for (i = 0; i < SUMO_MAX_HARDWARE_POWERLEVELS; i++)
2798 		pi->at[i] = TRINITY_AT_DFLT;
2799 
2800 	pi->sram_end = SMC_RAM_END;
2801 
2802 	pi->enable_nb_dpm = true;
2803 
2804 	pi->caps_power_containment = true;
2805 	pi->caps_cac = true;
2806 	pi->enable_didt = false;
2807 	if (pi->enable_didt) {
2808 		pi->caps_sq_ramping = true;
2809 		pi->caps_db_ramping = true;
2810 		pi->caps_td_ramping = true;
2811 		pi->caps_tcp_ramping = true;
2812 	}
2813 
2814 	if (adev->pm.pp_feature & PP_SCLK_DEEP_SLEEP_MASK)
2815 		pi->caps_sclk_ds = true;
2816 	else
2817 		pi->caps_sclk_ds = false;
2818 
2819 	pi->enable_auto_thermal_throttling = true;
2820 	pi->disable_nb_ps3_in_battery = false;
2821 	if (amdgpu_bapm == 0)
2822 		pi->bapm_enable = false;
2823 	else
2824 		pi->bapm_enable = true;
2825 	pi->voltage_drop_t = 0;
2826 	pi->caps_sclk_throttle_low_notification = false;
2827 	pi->caps_fps = false; /* true? */
2828 	pi->caps_uvd_pg = (adev->pg_flags & AMD_PG_SUPPORT_UVD) ? true : false;
2829 	pi->caps_uvd_dpm = true;
2830 	pi->caps_vce_pg = (adev->pg_flags & AMD_PG_SUPPORT_VCE) ? true : false;
2831 	pi->caps_samu_pg = (adev->pg_flags & AMD_PG_SUPPORT_SAMU) ? true : false;
2832 	pi->caps_acp_pg = (adev->pg_flags & AMD_PG_SUPPORT_ACP) ? true : false;
2833 	pi->caps_stable_p_state = false;
2834 
2835 	ret = kv_parse_sys_info_table(adev);
2836 	if (ret)
2837 		return ret;
2838 
2839 	kv_patch_voltage_values(adev);
2840 	kv_construct_boot_state(adev);
2841 
2842 	ret = kv_parse_power_table(adev);
2843 	if (ret)
2844 		return ret;
2845 
2846 	pi->enable_dpm = true;
2847 
2848 	return 0;
2849 }
2850 
2851 static void
kv_dpm_debugfs_print_current_performance_level(void * handle,struct seq_file * m)2852 kv_dpm_debugfs_print_current_performance_level(void *handle,
2853 					       struct seq_file *m)
2854 {
2855 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2856 	struct kv_power_info *pi = kv_get_pi(adev);
2857 	u32 current_index =
2858 		(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX) &
2859 		TARGET_AND_CURRENT_PROFILE_INDEX__CURR_SCLK_INDEX_MASK) >>
2860 		TARGET_AND_CURRENT_PROFILE_INDEX__CURR_SCLK_INDEX__SHIFT;
2861 	u32 sclk, tmp;
2862 	u16 vddc;
2863 
2864 	if (current_index >= SMU__NUM_SCLK_DPM_STATE) {
2865 		seq_printf(m, "invalid dpm profile %d\n", current_index);
2866 	} else {
2867 		sclk = be32_to_cpu(pi->graphics_level[current_index].SclkFrequency);
2868 		tmp = (RREG32_SMC(ixSMU_VOLTAGE_STATUS) &
2869 			SMU_VOLTAGE_STATUS__SMU_VOLTAGE_CURRENT_LEVEL_MASK) >>
2870 			SMU_VOLTAGE_STATUS__SMU_VOLTAGE_CURRENT_LEVEL__SHIFT;
2871 		vddc = kv_convert_8bit_index_to_voltage(adev, (u16)tmp);
2872 		seq_printf(m, "uvd    %sabled\n", pi->uvd_power_gated ? "dis" : "en");
2873 		seq_printf(m, "vce    %sabled\n", pi->vce_power_gated ? "dis" : "en");
2874 		seq_printf(m, "power level %d    sclk: %u vddc: %u\n",
2875 			   current_index, sclk, vddc);
2876 	}
2877 }
2878 
2879 static void
kv_dpm_print_power_state(void * handle,void * request_ps)2880 kv_dpm_print_power_state(void *handle, void *request_ps)
2881 {
2882 	int i;
2883 	struct amdgpu_ps *rps = (struct amdgpu_ps *)request_ps;
2884 	struct kv_ps *ps = kv_get_ps(rps);
2885 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2886 
2887 	amdgpu_dpm_print_class_info(rps->class, rps->class2);
2888 	amdgpu_dpm_print_cap_info(rps->caps);
2889 	printk("\tuvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
2890 	for (i = 0; i < ps->num_levels; i++) {
2891 		struct kv_pl *pl = &ps->levels[i];
2892 		printk("\t\tpower level %d    sclk: %u vddc: %u\n",
2893 		       i, pl->sclk,
2894 		       kv_convert_8bit_index_to_voltage(adev, pl->vddc_index));
2895 	}
2896 	amdgpu_dpm_print_ps_status(adev, rps);
2897 }
2898 
kv_dpm_fini(struct amdgpu_device * adev)2899 static void kv_dpm_fini(struct amdgpu_device *adev)
2900 {
2901 	int i;
2902 
2903 	for (i = 0; i < adev->pm.dpm.num_ps; i++) {
2904 		kfree(adev->pm.dpm.ps[i].ps_priv);
2905 	}
2906 	kfree(adev->pm.dpm.ps);
2907 	kfree(adev->pm.dpm.priv);
2908 	amdgpu_free_extended_power_table(adev);
2909 }
2910 
kv_dpm_display_configuration_changed(void * handle)2911 static void kv_dpm_display_configuration_changed(void *handle)
2912 {
2913 
2914 }
2915 
kv_dpm_get_sclk(void * handle,bool low)2916 static u32 kv_dpm_get_sclk(void *handle, bool low)
2917 {
2918 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2919 	struct kv_power_info *pi = kv_get_pi(adev);
2920 	struct kv_ps *requested_state = kv_get_ps(&pi->requested_rps);
2921 
2922 	if (low)
2923 		return requested_state->levels[0].sclk;
2924 	else
2925 		return requested_state->levels[requested_state->num_levels - 1].sclk;
2926 }
2927 
kv_dpm_get_mclk(void * handle,bool low)2928 static u32 kv_dpm_get_mclk(void *handle, bool low)
2929 {
2930 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2931 	struct kv_power_info *pi = kv_get_pi(adev);
2932 
2933 	return pi->sys_info.bootup_uma_clk;
2934 }
2935 
2936 /* get temperature in millidegrees */
kv_dpm_get_temp(void * handle)2937 static int kv_dpm_get_temp(void *handle)
2938 {
2939 	u32 temp;
2940 	int actual_temp = 0;
2941 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2942 
2943 	temp = RREG32_SMC(0xC0300E0C);
2944 
2945 	if (temp)
2946 		actual_temp = (temp / 8) - 49;
2947 	else
2948 		actual_temp = 0;
2949 
2950 	actual_temp = actual_temp * 1000;
2951 
2952 	return actual_temp;
2953 }
2954 
kv_dpm_early_init(void * handle)2955 static int kv_dpm_early_init(void *handle)
2956 {
2957 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2958 
2959 	adev->powerplay.pp_funcs = &kv_dpm_funcs;
2960 	adev->powerplay.pp_handle = adev;
2961 	kv_dpm_set_irq_funcs(adev);
2962 
2963 	return 0;
2964 }
2965 
kv_dpm_late_init(void * handle)2966 static int kv_dpm_late_init(void *handle)
2967 {
2968 	/* powerdown unused blocks for now */
2969 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2970 
2971 	if (!adev->pm.dpm_enabled)
2972 		return 0;
2973 
2974 	kv_dpm_powergate_acp(adev, true);
2975 	kv_dpm_powergate_samu(adev, true);
2976 
2977 	return 0;
2978 }
2979 
kv_dpm_sw_init(void * handle)2980 static int kv_dpm_sw_init(void *handle)
2981 {
2982 	int ret;
2983 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2984 
2985 	ret = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, 230,
2986 				&adev->pm.dpm.thermal.irq);
2987 	if (ret)
2988 		return ret;
2989 
2990 	ret = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, 231,
2991 				&adev->pm.dpm.thermal.irq);
2992 	if (ret)
2993 		return ret;
2994 
2995 	/* default to balanced state */
2996 	adev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
2997 	adev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
2998 	adev->pm.dpm.forced_level = AMD_DPM_FORCED_LEVEL_AUTO;
2999 	adev->pm.default_sclk = adev->clock.default_sclk;
3000 	adev->pm.default_mclk = adev->clock.default_mclk;
3001 	adev->pm.current_sclk = adev->clock.default_sclk;
3002 	adev->pm.current_mclk = adev->clock.default_mclk;
3003 	adev->pm.int_thermal_type = THERMAL_TYPE_NONE;
3004 
3005 	if (amdgpu_dpm == 0)
3006 		return 0;
3007 
3008 	INIT_WORK(&adev->pm.dpm.thermal.work, amdgpu_dpm_thermal_work_handler);
3009 	mutex_lock(&adev->pm.mutex);
3010 	ret = kv_dpm_init(adev);
3011 	if (ret)
3012 		goto dpm_failed;
3013 	adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps = adev->pm.dpm.boot_ps;
3014 	if (amdgpu_dpm == 1)
3015 		amdgpu_pm_print_power_states(adev);
3016 	mutex_unlock(&adev->pm.mutex);
3017 	DRM_INFO("amdgpu: dpm initialized\n");
3018 
3019 	return 0;
3020 
3021 dpm_failed:
3022 	kv_dpm_fini(adev);
3023 	mutex_unlock(&adev->pm.mutex);
3024 	DRM_ERROR("amdgpu: dpm initialization failed\n");
3025 	return ret;
3026 }
3027 
kv_dpm_sw_fini(void * handle)3028 static int kv_dpm_sw_fini(void *handle)
3029 {
3030 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3031 
3032 	flush_work(&adev->pm.dpm.thermal.work);
3033 
3034 	mutex_lock(&adev->pm.mutex);
3035 	kv_dpm_fini(adev);
3036 	mutex_unlock(&adev->pm.mutex);
3037 
3038 	return 0;
3039 }
3040 
kv_dpm_hw_init(void * handle)3041 static int kv_dpm_hw_init(void *handle)
3042 {
3043 	int ret;
3044 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3045 
3046 	if (!amdgpu_dpm)
3047 		return 0;
3048 
3049 	mutex_lock(&adev->pm.mutex);
3050 	kv_dpm_setup_asic(adev);
3051 	ret = kv_dpm_enable(adev);
3052 	if (ret)
3053 		adev->pm.dpm_enabled = false;
3054 	else
3055 		adev->pm.dpm_enabled = true;
3056 	mutex_unlock(&adev->pm.mutex);
3057 	amdgpu_pm_compute_clocks(adev);
3058 	return ret;
3059 }
3060 
kv_dpm_hw_fini(void * handle)3061 static int kv_dpm_hw_fini(void *handle)
3062 {
3063 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3064 
3065 	if (adev->pm.dpm_enabled) {
3066 		mutex_lock(&adev->pm.mutex);
3067 		kv_dpm_disable(adev);
3068 		mutex_unlock(&adev->pm.mutex);
3069 	}
3070 
3071 	return 0;
3072 }
3073 
kv_dpm_suspend(void * handle)3074 static int kv_dpm_suspend(void *handle)
3075 {
3076 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3077 
3078 	if (adev->pm.dpm_enabled) {
3079 		mutex_lock(&adev->pm.mutex);
3080 		/* disable dpm */
3081 		kv_dpm_disable(adev);
3082 		/* reset the power state */
3083 		adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps = adev->pm.dpm.boot_ps;
3084 		mutex_unlock(&adev->pm.mutex);
3085 	}
3086 	return 0;
3087 }
3088 
kv_dpm_resume(void * handle)3089 static int kv_dpm_resume(void *handle)
3090 {
3091 	int ret;
3092 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3093 
3094 	if (adev->pm.dpm_enabled) {
3095 		/* asic init will reset to the boot state */
3096 		mutex_lock(&adev->pm.mutex);
3097 		kv_dpm_setup_asic(adev);
3098 		ret = kv_dpm_enable(adev);
3099 		if (ret)
3100 			adev->pm.dpm_enabled = false;
3101 		else
3102 			adev->pm.dpm_enabled = true;
3103 		mutex_unlock(&adev->pm.mutex);
3104 		if (adev->pm.dpm_enabled)
3105 			amdgpu_pm_compute_clocks(adev);
3106 	}
3107 	return 0;
3108 }
3109 
kv_dpm_is_idle(void * handle)3110 static bool kv_dpm_is_idle(void *handle)
3111 {
3112 	return true;
3113 }
3114 
kv_dpm_wait_for_idle(void * handle)3115 static int kv_dpm_wait_for_idle(void *handle)
3116 {
3117 	return 0;
3118 }
3119 
3120 
kv_dpm_soft_reset(void * handle)3121 static int kv_dpm_soft_reset(void *handle)
3122 {
3123 	return 0;
3124 }
3125 
kv_dpm_set_interrupt_state(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type,enum amdgpu_interrupt_state state)3126 static int kv_dpm_set_interrupt_state(struct amdgpu_device *adev,
3127 				      struct amdgpu_irq_src *src,
3128 				      unsigned type,
3129 				      enum amdgpu_interrupt_state state)
3130 {
3131 	u32 cg_thermal_int;
3132 
3133 	switch (type) {
3134 	case AMDGPU_THERMAL_IRQ_LOW_TO_HIGH:
3135 		switch (state) {
3136 		case AMDGPU_IRQ_STATE_DISABLE:
3137 			cg_thermal_int = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
3138 			cg_thermal_int &= ~CG_THERMAL_INT_CTRL__THERM_INTH_MASK_MASK;
3139 			WREG32_SMC(ixCG_THERMAL_INT_CTRL, cg_thermal_int);
3140 			break;
3141 		case AMDGPU_IRQ_STATE_ENABLE:
3142 			cg_thermal_int = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
3143 			cg_thermal_int |= CG_THERMAL_INT_CTRL__THERM_INTH_MASK_MASK;
3144 			WREG32_SMC(ixCG_THERMAL_INT_CTRL, cg_thermal_int);
3145 			break;
3146 		default:
3147 			break;
3148 		}
3149 		break;
3150 
3151 	case AMDGPU_THERMAL_IRQ_HIGH_TO_LOW:
3152 		switch (state) {
3153 		case AMDGPU_IRQ_STATE_DISABLE:
3154 			cg_thermal_int = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
3155 			cg_thermal_int &= ~CG_THERMAL_INT_CTRL__THERM_INTL_MASK_MASK;
3156 			WREG32_SMC(ixCG_THERMAL_INT_CTRL, cg_thermal_int);
3157 			break;
3158 		case AMDGPU_IRQ_STATE_ENABLE:
3159 			cg_thermal_int = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
3160 			cg_thermal_int |= CG_THERMAL_INT_CTRL__THERM_INTL_MASK_MASK;
3161 			WREG32_SMC(ixCG_THERMAL_INT_CTRL, cg_thermal_int);
3162 			break;
3163 		default:
3164 			break;
3165 		}
3166 		break;
3167 
3168 	default:
3169 		break;
3170 	}
3171 	return 0;
3172 }
3173 
kv_dpm_process_interrupt(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)3174 static int kv_dpm_process_interrupt(struct amdgpu_device *adev,
3175 				    struct amdgpu_irq_src *source,
3176 				    struct amdgpu_iv_entry *entry)
3177 {
3178 	bool queue_thermal = false;
3179 
3180 	if (entry == NULL)
3181 		return -EINVAL;
3182 
3183 	switch (entry->src_id) {
3184 	case 230: /* thermal low to high */
3185 		DRM_DEBUG("IH: thermal low to high\n");
3186 		adev->pm.dpm.thermal.high_to_low = false;
3187 		queue_thermal = true;
3188 		break;
3189 	case 231: /* thermal high to low */
3190 		DRM_DEBUG("IH: thermal high to low\n");
3191 		adev->pm.dpm.thermal.high_to_low = true;
3192 		queue_thermal = true;
3193 		break;
3194 	default:
3195 		break;
3196 	}
3197 
3198 	if (queue_thermal)
3199 		schedule_work(&adev->pm.dpm.thermal.work);
3200 
3201 	return 0;
3202 }
3203 
kv_dpm_set_clockgating_state(void * handle,enum amd_clockgating_state state)3204 static int kv_dpm_set_clockgating_state(void *handle,
3205 					  enum amd_clockgating_state state)
3206 {
3207 	return 0;
3208 }
3209 
kv_dpm_set_powergating_state(void * handle,enum amd_powergating_state state)3210 static int kv_dpm_set_powergating_state(void *handle,
3211 					  enum amd_powergating_state state)
3212 {
3213 	return 0;
3214 }
3215 
kv_are_power_levels_equal(const struct kv_pl * kv_cpl1,const struct kv_pl * kv_cpl2)3216 static inline bool kv_are_power_levels_equal(const struct kv_pl *kv_cpl1,
3217 						const struct kv_pl *kv_cpl2)
3218 {
3219 	return ((kv_cpl1->sclk == kv_cpl2->sclk) &&
3220 		  (kv_cpl1->vddc_index == kv_cpl2->vddc_index) &&
3221 		  (kv_cpl1->ds_divider_index == kv_cpl2->ds_divider_index) &&
3222 		  (kv_cpl1->force_nbp_state == kv_cpl2->force_nbp_state));
3223 }
3224 
kv_check_state_equal(void * handle,void * current_ps,void * request_ps,bool * equal)3225 static int kv_check_state_equal(void *handle,
3226 				void *current_ps,
3227 				void *request_ps,
3228 				bool *equal)
3229 {
3230 	struct kv_ps *kv_cps;
3231 	struct kv_ps *kv_rps;
3232 	int i;
3233 	struct amdgpu_ps *cps = (struct amdgpu_ps *)current_ps;
3234 	struct amdgpu_ps *rps = (struct amdgpu_ps *)request_ps;
3235 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3236 
3237 	if (adev == NULL || cps == NULL || rps == NULL || equal == NULL)
3238 		return -EINVAL;
3239 
3240 	kv_cps = kv_get_ps(cps);
3241 	kv_rps = kv_get_ps(rps);
3242 
3243 	if (kv_cps == NULL) {
3244 		*equal = false;
3245 		return 0;
3246 	}
3247 
3248 	if (kv_cps->num_levels != kv_rps->num_levels) {
3249 		*equal = false;
3250 		return 0;
3251 	}
3252 
3253 	for (i = 0; i < kv_cps->num_levels; i++) {
3254 		if (!kv_are_power_levels_equal(&(kv_cps->levels[i]),
3255 					&(kv_rps->levels[i]))) {
3256 			*equal = false;
3257 			return 0;
3258 		}
3259 	}
3260 
3261 	/* If all performance levels are the same try to use the UVD clocks to break the tie.*/
3262 	*equal = ((cps->vclk == rps->vclk) && (cps->dclk == rps->dclk));
3263 	*equal &= ((cps->evclk == rps->evclk) && (cps->ecclk == rps->ecclk));
3264 
3265 	return 0;
3266 }
3267 
kv_dpm_read_sensor(void * handle,int idx,void * value,int * size)3268 static int kv_dpm_read_sensor(void *handle, int idx,
3269 			      void *value, int *size)
3270 {
3271 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3272 	struct kv_power_info *pi = kv_get_pi(adev);
3273 	uint32_t sclk;
3274 	u32 pl_index =
3275 		(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX) &
3276 		TARGET_AND_CURRENT_PROFILE_INDEX__CURR_SCLK_INDEX_MASK) >>
3277 		TARGET_AND_CURRENT_PROFILE_INDEX__CURR_SCLK_INDEX__SHIFT;
3278 
3279 	/* size must be at least 4 bytes for all sensors */
3280 	if (*size < 4)
3281 		return -EINVAL;
3282 
3283 	switch (idx) {
3284 	case AMDGPU_PP_SENSOR_GFX_SCLK:
3285 		if (pl_index < SMU__NUM_SCLK_DPM_STATE) {
3286 			sclk = be32_to_cpu(
3287 				pi->graphics_level[pl_index].SclkFrequency);
3288 			*((uint32_t *)value) = sclk;
3289 			*size = 4;
3290 			return 0;
3291 		}
3292 		return -EINVAL;
3293 	case AMDGPU_PP_SENSOR_GPU_TEMP:
3294 		*((uint32_t *)value) = kv_dpm_get_temp(adev);
3295 		*size = 4;
3296 		return 0;
3297 	default:
3298 		return -EINVAL;
3299 	}
3300 }
3301 
kv_set_powergating_by_smu(void * handle,uint32_t block_type,bool gate)3302 static int kv_set_powergating_by_smu(void *handle,
3303 				uint32_t block_type, bool gate)
3304 {
3305 	switch (block_type) {
3306 	case AMD_IP_BLOCK_TYPE_UVD:
3307 		kv_dpm_powergate_uvd(handle, gate);
3308 		break;
3309 	case AMD_IP_BLOCK_TYPE_VCE:
3310 		kv_dpm_powergate_vce(handle, gate);
3311 		break;
3312 	default:
3313 		break;
3314 	}
3315 	return 0;
3316 }
3317 
3318 static const struct amd_ip_funcs kv_dpm_ip_funcs = {
3319 	.name = "kv_dpm",
3320 	.early_init = kv_dpm_early_init,
3321 	.late_init = kv_dpm_late_init,
3322 	.sw_init = kv_dpm_sw_init,
3323 	.sw_fini = kv_dpm_sw_fini,
3324 	.hw_init = kv_dpm_hw_init,
3325 	.hw_fini = kv_dpm_hw_fini,
3326 	.suspend = kv_dpm_suspend,
3327 	.resume = kv_dpm_resume,
3328 	.is_idle = kv_dpm_is_idle,
3329 	.wait_for_idle = kv_dpm_wait_for_idle,
3330 	.soft_reset = kv_dpm_soft_reset,
3331 	.set_clockgating_state = kv_dpm_set_clockgating_state,
3332 	.set_powergating_state = kv_dpm_set_powergating_state,
3333 };
3334 
3335 const struct amdgpu_ip_block_version kv_smu_ip_block =
3336 {
3337 	.type = AMD_IP_BLOCK_TYPE_SMC,
3338 	.major = 1,
3339 	.minor = 0,
3340 	.rev = 0,
3341 	.funcs = &kv_dpm_ip_funcs,
3342 };
3343 
3344 static const struct amd_pm_funcs kv_dpm_funcs = {
3345 	.pre_set_power_state = &kv_dpm_pre_set_power_state,
3346 	.set_power_state = &kv_dpm_set_power_state,
3347 	.post_set_power_state = &kv_dpm_post_set_power_state,
3348 	.display_configuration_changed = &kv_dpm_display_configuration_changed,
3349 	.get_sclk = &kv_dpm_get_sclk,
3350 	.get_mclk = &kv_dpm_get_mclk,
3351 	.print_power_state = &kv_dpm_print_power_state,
3352 	.debugfs_print_current_performance_level = &kv_dpm_debugfs_print_current_performance_level,
3353 	.force_performance_level = &kv_dpm_force_performance_level,
3354 	.set_powergating_by_smu = kv_set_powergating_by_smu,
3355 	.enable_bapm = &kv_dpm_enable_bapm,
3356 	.get_vce_clock_state = amdgpu_get_vce_clock_state,
3357 	.check_state_equal = kv_check_state_equal,
3358 	.read_sensor = &kv_dpm_read_sensor,
3359 };
3360 
3361 static const struct amdgpu_irq_src_funcs kv_dpm_irq_funcs = {
3362 	.set = kv_dpm_set_interrupt_state,
3363 	.process = kv_dpm_process_interrupt,
3364 };
3365 
kv_dpm_set_irq_funcs(struct amdgpu_device * adev)3366 static void kv_dpm_set_irq_funcs(struct amdgpu_device *adev)
3367 {
3368 	adev->pm.dpm.thermal.irq.num_types = AMDGPU_THERMAL_IRQ_LAST;
3369 	adev->pm.dpm.thermal.irq.funcs = &kv_dpm_irq_funcs;
3370 }
3371