Loading...
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 <linux/pci.h>
25#include <linux/seq_file.h>
26
27#include "cikd.h"
28#include "kv_dpm.h"
29#include "r600_dpm.h"
30#include "radeon.h"
31#include "radeon_asic.h"
32
33#define KV_MAX_DEEPSLEEP_DIVIDER_ID 5
34#define KV_MINIMUM_ENGINE_CLOCK 800
35#define SMC_RAM_END 0x40000
36
37static int kv_enable_nb_dpm(struct radeon_device *rdev,
38 bool enable);
39static void kv_init_graphics_levels(struct radeon_device *rdev);
40static int kv_calculate_ds_divider(struct radeon_device *rdev);
41static int kv_calculate_nbps_level_settings(struct radeon_device *rdev);
42static int kv_calculate_dpm_settings(struct radeon_device *rdev);
43static void kv_enable_new_levels(struct radeon_device *rdev);
44static void kv_program_nbps_index_settings(struct radeon_device *rdev,
45 struct radeon_ps *new_rps);
46static int kv_set_enabled_level(struct radeon_device *rdev, u32 level);
47static int kv_set_enabled_levels(struct radeon_device *rdev);
48static int kv_force_dpm_highest(struct radeon_device *rdev);
49static int kv_force_dpm_lowest(struct radeon_device *rdev);
50static void kv_apply_state_adjust_rules(struct radeon_device *rdev,
51 struct radeon_ps *new_rps,
52 struct radeon_ps *old_rps);
53static int kv_set_thermal_temperature_range(struct radeon_device *rdev,
54 int min_temp, int max_temp);
55static int kv_init_fps_limits(struct radeon_device *rdev);
56
57void kv_dpm_powergate_uvd(struct radeon_device *rdev, bool gate);
58static void kv_dpm_powergate_vce(struct radeon_device *rdev, bool gate);
59static void kv_dpm_powergate_samu(struct radeon_device *rdev, bool gate);
60static void kv_dpm_powergate_acp(struct radeon_device *rdev, bool gate);
61
62extern void cik_enter_rlc_safe_mode(struct radeon_device *rdev);
63extern void cik_exit_rlc_safe_mode(struct radeon_device *rdev);
64extern void cik_update_cg(struct radeon_device *rdev,
65 u32 block, bool enable);
66
67static const struct kv_pt_config_reg didt_config_kv[] = {
68 { 0x10, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
69 { 0x10, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
70 { 0x10, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
71 { 0x10, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
72 { 0x11, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
73 { 0x11, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
74 { 0x11, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
75 { 0x11, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
76 { 0x12, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
77 { 0x12, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
78 { 0x12, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
79 { 0x12, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
80 { 0x2, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
81 { 0x2, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
82 { 0x2, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
83 { 0x1, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
84 { 0x1, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
85 { 0x0, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
86 { 0x30, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
87 { 0x30, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
88 { 0x30, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
89 { 0x30, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
90 { 0x31, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
91 { 0x31, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
92 { 0x31, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
93 { 0x31, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
94 { 0x32, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
95 { 0x32, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
96 { 0x32, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
97 { 0x32, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
98 { 0x22, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
99 { 0x22, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
100 { 0x22, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
101 { 0x21, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
102 { 0x21, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
103 { 0x20, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
104 { 0x50, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
105 { 0x50, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
106 { 0x50, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
107 { 0x50, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
108 { 0x51, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
109 { 0x51, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
110 { 0x51, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
111 { 0x51, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
112 { 0x52, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
113 { 0x52, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
114 { 0x52, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
115 { 0x52, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
116 { 0x42, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
117 { 0x42, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
118 { 0x42, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
119 { 0x41, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
120 { 0x41, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
121 { 0x40, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
122 { 0x70, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
123 { 0x70, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
124 { 0x70, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
125 { 0x70, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
126 { 0x71, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
127 { 0x71, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
128 { 0x71, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
129 { 0x71, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
130 { 0x72, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
131 { 0x72, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
132 { 0x72, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
133 { 0x72, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
134 { 0x62, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
135 { 0x62, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
136 { 0x62, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
137 { 0x61, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
138 { 0x61, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
139 { 0x60, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
140 { 0xFFFFFFFF }
141};
142
143static struct kv_ps *kv_get_ps(struct radeon_ps *rps)
144{
145 struct kv_ps *ps = rps->ps_priv;
146
147 return ps;
148}
149
150static struct kv_power_info *kv_get_pi(struct radeon_device *rdev)
151{
152 struct kv_power_info *pi = rdev->pm.dpm.priv;
153
154 return pi;
155}
156
157static int kv_program_pt_config_registers(struct radeon_device *rdev,
158 const struct kv_pt_config_reg *cac_config_regs)
159{
160 const struct kv_pt_config_reg *config_regs = cac_config_regs;
161 u32 data;
162 u32 cache = 0;
163
164 if (config_regs == NULL)
165 return -EINVAL;
166
167 while (config_regs->offset != 0xFFFFFFFF) {
168 if (config_regs->type == KV_CONFIGREG_CACHE) {
169 cache |= ((config_regs->value << config_regs->shift) & config_regs->mask);
170 } else {
171 switch (config_regs->type) {
172 case KV_CONFIGREG_SMC_IND:
173 data = RREG32_SMC(config_regs->offset);
174 break;
175 case KV_CONFIGREG_DIDT_IND:
176 data = RREG32_DIDT(config_regs->offset);
177 break;
178 default:
179 data = RREG32(config_regs->offset << 2);
180 break;
181 }
182
183 data &= ~config_regs->mask;
184 data |= ((config_regs->value << config_regs->shift) & config_regs->mask);
185 data |= cache;
186 cache = 0;
187
188 switch (config_regs->type) {
189 case KV_CONFIGREG_SMC_IND:
190 WREG32_SMC(config_regs->offset, data);
191 break;
192 case KV_CONFIGREG_DIDT_IND:
193 WREG32_DIDT(config_regs->offset, data);
194 break;
195 default:
196 WREG32(config_regs->offset << 2, data);
197 break;
198 }
199 }
200 config_regs++;
201 }
202
203 return 0;
204}
205
206static void kv_do_enable_didt(struct radeon_device *rdev, bool enable)
207{
208 struct kv_power_info *pi = kv_get_pi(rdev);
209 u32 data;
210
211 if (pi->caps_sq_ramping) {
212 data = RREG32_DIDT(DIDT_SQ_CTRL0);
213 if (enable)
214 data |= DIDT_CTRL_EN;
215 else
216 data &= ~DIDT_CTRL_EN;
217 WREG32_DIDT(DIDT_SQ_CTRL0, data);
218 }
219
220 if (pi->caps_db_ramping) {
221 data = RREG32_DIDT(DIDT_DB_CTRL0);
222 if (enable)
223 data |= DIDT_CTRL_EN;
224 else
225 data &= ~DIDT_CTRL_EN;
226 WREG32_DIDT(DIDT_DB_CTRL0, data);
227 }
228
229 if (pi->caps_td_ramping) {
230 data = RREG32_DIDT(DIDT_TD_CTRL0);
231 if (enable)
232 data |= DIDT_CTRL_EN;
233 else
234 data &= ~DIDT_CTRL_EN;
235 WREG32_DIDT(DIDT_TD_CTRL0, data);
236 }
237
238 if (pi->caps_tcp_ramping) {
239 data = RREG32_DIDT(DIDT_TCP_CTRL0);
240 if (enable)
241 data |= DIDT_CTRL_EN;
242 else
243 data &= ~DIDT_CTRL_EN;
244 WREG32_DIDT(DIDT_TCP_CTRL0, data);
245 }
246}
247
248static int kv_enable_didt(struct radeon_device *rdev, bool enable)
249{
250 struct kv_power_info *pi = kv_get_pi(rdev);
251 int ret;
252
253 if (pi->caps_sq_ramping ||
254 pi->caps_db_ramping ||
255 pi->caps_td_ramping ||
256 pi->caps_tcp_ramping) {
257 cik_enter_rlc_safe_mode(rdev);
258
259 if (enable) {
260 ret = kv_program_pt_config_registers(rdev, didt_config_kv);
261 if (ret) {
262 cik_exit_rlc_safe_mode(rdev);
263 return ret;
264 }
265 }
266
267 kv_do_enable_didt(rdev, enable);
268
269 cik_exit_rlc_safe_mode(rdev);
270 }
271
272 return 0;
273}
274
275static int kv_enable_smc_cac(struct radeon_device *rdev, bool enable)
276{
277 struct kv_power_info *pi = kv_get_pi(rdev);
278 int ret = 0;
279
280 if (pi->caps_cac) {
281 if (enable) {
282 ret = kv_notify_message_to_smu(rdev, PPSMC_MSG_EnableCac);
283 if (ret)
284 pi->cac_enabled = false;
285 else
286 pi->cac_enabled = true;
287 } else if (pi->cac_enabled) {
288 kv_notify_message_to_smu(rdev, PPSMC_MSG_DisableCac);
289 pi->cac_enabled = false;
290 }
291 }
292
293 return ret;
294}
295
296static int kv_process_firmware_header(struct radeon_device *rdev)
297{
298 struct kv_power_info *pi = kv_get_pi(rdev);
299 u32 tmp;
300 int ret;
301
302 ret = kv_read_smc_sram_dword(rdev, SMU7_FIRMWARE_HEADER_LOCATION +
303 offsetof(SMU7_Firmware_Header, DpmTable),
304 &tmp, pi->sram_end);
305
306 if (ret == 0)
307 pi->dpm_table_start = tmp;
308
309 ret = kv_read_smc_sram_dword(rdev, SMU7_FIRMWARE_HEADER_LOCATION +
310 offsetof(SMU7_Firmware_Header, SoftRegisters),
311 &tmp, pi->sram_end);
312
313 if (ret == 0)
314 pi->soft_regs_start = tmp;
315
316 return ret;
317}
318
319static int kv_enable_dpm_voltage_scaling(struct radeon_device *rdev)
320{
321 struct kv_power_info *pi = kv_get_pi(rdev);
322 int ret;
323
324 pi->graphics_voltage_change_enable = 1;
325
326 ret = kv_copy_bytes_to_smc(rdev,
327 pi->dpm_table_start +
328 offsetof(SMU7_Fusion_DpmTable, GraphicsVoltageChangeEnable),
329 &pi->graphics_voltage_change_enable,
330 sizeof(u8), pi->sram_end);
331
332 return ret;
333}
334
335static int kv_set_dpm_interval(struct radeon_device *rdev)
336{
337 struct kv_power_info *pi = kv_get_pi(rdev);
338 int ret;
339
340 pi->graphics_interval = 1;
341
342 ret = kv_copy_bytes_to_smc(rdev,
343 pi->dpm_table_start +
344 offsetof(SMU7_Fusion_DpmTable, GraphicsInterval),
345 &pi->graphics_interval,
346 sizeof(u8), pi->sram_end);
347
348 return ret;
349}
350
351static int kv_set_dpm_boot_state(struct radeon_device *rdev)
352{
353 struct kv_power_info *pi = kv_get_pi(rdev);
354 int ret;
355
356 ret = kv_copy_bytes_to_smc(rdev,
357 pi->dpm_table_start +
358 offsetof(SMU7_Fusion_DpmTable, GraphicsBootLevel),
359 &pi->graphics_boot_level,
360 sizeof(u8), pi->sram_end);
361
362 return ret;
363}
364
365static void kv_program_vc(struct radeon_device *rdev)
366{
367 WREG32_SMC(CG_FTV_0, 0x3FFFC100);
368}
369
370static void kv_clear_vc(struct radeon_device *rdev)
371{
372 WREG32_SMC(CG_FTV_0, 0);
373}
374
375static int kv_set_divider_value(struct radeon_device *rdev,
376 u32 index, u32 sclk)
377{
378 struct kv_power_info *pi = kv_get_pi(rdev);
379 struct atom_clock_dividers dividers;
380 int ret;
381
382 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
383 sclk, false, ÷rs);
384 if (ret)
385 return ret;
386
387 pi->graphics_level[index].SclkDid = (u8)dividers.post_div;
388 pi->graphics_level[index].SclkFrequency = cpu_to_be32(sclk);
389
390 return 0;
391}
392
393static u32 kv_convert_vid2_to_vid7(struct radeon_device *rdev,
394 struct sumo_vid_mapping_table *vid_mapping_table,
395 u32 vid_2bit)
396{
397 struct radeon_clock_voltage_dependency_table *vddc_sclk_table =
398 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
399 u32 i;
400
401 if (vddc_sclk_table && vddc_sclk_table->count) {
402 if (vid_2bit < vddc_sclk_table->count)
403 return vddc_sclk_table->entries[vid_2bit].v;
404 else
405 return vddc_sclk_table->entries[vddc_sclk_table->count - 1].v;
406 } else {
407 for (i = 0; i < vid_mapping_table->num_entries; i++) {
408 if (vid_mapping_table->entries[i].vid_2bit == vid_2bit)
409 return vid_mapping_table->entries[i].vid_7bit;
410 }
411 return vid_mapping_table->entries[vid_mapping_table->num_entries - 1].vid_7bit;
412 }
413}
414
415static u32 kv_convert_vid7_to_vid2(struct radeon_device *rdev,
416 struct sumo_vid_mapping_table *vid_mapping_table,
417 u32 vid_7bit)
418{
419 struct radeon_clock_voltage_dependency_table *vddc_sclk_table =
420 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
421 u32 i;
422
423 if (vddc_sclk_table && vddc_sclk_table->count) {
424 for (i = 0; i < vddc_sclk_table->count; i++) {
425 if (vddc_sclk_table->entries[i].v == vid_7bit)
426 return i;
427 }
428 return vddc_sclk_table->count - 1;
429 } else {
430 for (i = 0; i < vid_mapping_table->num_entries; i++) {
431 if (vid_mapping_table->entries[i].vid_7bit == vid_7bit)
432 return vid_mapping_table->entries[i].vid_2bit;
433 }
434
435 return vid_mapping_table->entries[vid_mapping_table->num_entries - 1].vid_2bit;
436 }
437}
438
439static u16 kv_convert_8bit_index_to_voltage(struct radeon_device *rdev,
440 u16 voltage)
441{
442 return 6200 - (voltage * 25);
443}
444
445static u16 kv_convert_2bit_index_to_voltage(struct radeon_device *rdev,
446 u32 vid_2bit)
447{
448 struct kv_power_info *pi = kv_get_pi(rdev);
449 u32 vid_8bit = kv_convert_vid2_to_vid7(rdev,
450 &pi->sys_info.vid_mapping_table,
451 vid_2bit);
452
453 return kv_convert_8bit_index_to_voltage(rdev, (u16)vid_8bit);
454}
455
456
457static int kv_set_vid(struct radeon_device *rdev, u32 index, u32 vid)
458{
459 struct kv_power_info *pi = kv_get_pi(rdev);
460
461 pi->graphics_level[index].VoltageDownH = (u8)pi->voltage_drop_t;
462 pi->graphics_level[index].MinVddNb =
463 cpu_to_be32(kv_convert_2bit_index_to_voltage(rdev, vid));
464
465 return 0;
466}
467
468static int kv_set_at(struct radeon_device *rdev, u32 index, u32 at)
469{
470 struct kv_power_info *pi = kv_get_pi(rdev);
471
472 pi->graphics_level[index].AT = cpu_to_be16((u16)at);
473
474 return 0;
475}
476
477static void kv_dpm_power_level_enable(struct radeon_device *rdev,
478 u32 index, bool enable)
479{
480 struct kv_power_info *pi = kv_get_pi(rdev);
481
482 pi->graphics_level[index].EnabledForActivity = enable ? 1 : 0;
483}
484
485static void kv_start_dpm(struct radeon_device *rdev)
486{
487 u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
488
489 tmp |= GLOBAL_PWRMGT_EN;
490 WREG32_SMC(GENERAL_PWRMGT, tmp);
491
492 kv_smc_dpm_enable(rdev, true);
493}
494
495static void kv_stop_dpm(struct radeon_device *rdev)
496{
497 kv_smc_dpm_enable(rdev, false);
498}
499
500static void kv_start_am(struct radeon_device *rdev)
501{
502 u32 sclk_pwrmgt_cntl = RREG32_SMC(SCLK_PWRMGT_CNTL);
503
504 sclk_pwrmgt_cntl &= ~(RESET_SCLK_CNT | RESET_BUSY_CNT);
505 sclk_pwrmgt_cntl |= DYNAMIC_PM_EN;
506
507 WREG32_SMC(SCLK_PWRMGT_CNTL, sclk_pwrmgt_cntl);
508}
509
510static void kv_reset_am(struct radeon_device *rdev)
511{
512 u32 sclk_pwrmgt_cntl = RREG32_SMC(SCLK_PWRMGT_CNTL);
513
514 sclk_pwrmgt_cntl |= (RESET_SCLK_CNT | RESET_BUSY_CNT);
515
516 WREG32_SMC(SCLK_PWRMGT_CNTL, sclk_pwrmgt_cntl);
517}
518
519static int kv_freeze_sclk_dpm(struct radeon_device *rdev, bool freeze)
520{
521 return kv_notify_message_to_smu(rdev, freeze ?
522 PPSMC_MSG_SCLKDPM_FreezeLevel : PPSMC_MSG_SCLKDPM_UnfreezeLevel);
523}
524
525static int kv_force_lowest_valid(struct radeon_device *rdev)
526{
527 return kv_force_dpm_lowest(rdev);
528}
529
530static int kv_unforce_levels(struct radeon_device *rdev)
531{
532 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
533 return kv_notify_message_to_smu(rdev, PPSMC_MSG_NoForcedLevel);
534 else
535 return kv_set_enabled_levels(rdev);
536}
537
538static int kv_update_sclk_t(struct radeon_device *rdev)
539{
540 struct kv_power_info *pi = kv_get_pi(rdev);
541 u32 low_sclk_interrupt_t = 0;
542 int ret = 0;
543
544 if (pi->caps_sclk_throttle_low_notification) {
545 low_sclk_interrupt_t = cpu_to_be32(pi->low_sclk_interrupt_t);
546
547 ret = kv_copy_bytes_to_smc(rdev,
548 pi->dpm_table_start +
549 offsetof(SMU7_Fusion_DpmTable, LowSclkInterruptT),
550 (u8 *)&low_sclk_interrupt_t,
551 sizeof(u32), pi->sram_end);
552 }
553 return ret;
554}
555
556static int kv_program_bootup_state(struct radeon_device *rdev)
557{
558 struct kv_power_info *pi = kv_get_pi(rdev);
559 u32 i;
560 struct radeon_clock_voltage_dependency_table *table =
561 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
562
563 if (table && table->count) {
564 for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
565 if (table->entries[i].clk == pi->boot_pl.sclk)
566 break;
567 }
568
569 pi->graphics_boot_level = (u8)i;
570 kv_dpm_power_level_enable(rdev, i, true);
571 } else {
572 struct sumo_sclk_voltage_mapping_table *table =
573 &pi->sys_info.sclk_voltage_mapping_table;
574
575 if (table->num_max_dpm_entries == 0)
576 return -EINVAL;
577
578 for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
579 if (table->entries[i].sclk_frequency == pi->boot_pl.sclk)
580 break;
581 }
582
583 pi->graphics_boot_level = (u8)i;
584 kv_dpm_power_level_enable(rdev, i, true);
585 }
586 return 0;
587}
588
589static int kv_enable_auto_thermal_throttling(struct radeon_device *rdev)
590{
591 struct kv_power_info *pi = kv_get_pi(rdev);
592 int ret;
593
594 pi->graphics_therm_throttle_enable = 1;
595
596 ret = kv_copy_bytes_to_smc(rdev,
597 pi->dpm_table_start +
598 offsetof(SMU7_Fusion_DpmTable, GraphicsThermThrottleEnable),
599 &pi->graphics_therm_throttle_enable,
600 sizeof(u8), pi->sram_end);
601
602 return ret;
603}
604
605static int kv_upload_dpm_settings(struct radeon_device *rdev)
606{
607 struct kv_power_info *pi = kv_get_pi(rdev);
608 int ret;
609
610 ret = kv_copy_bytes_to_smc(rdev,
611 pi->dpm_table_start +
612 offsetof(SMU7_Fusion_DpmTable, GraphicsLevel),
613 (u8 *)&pi->graphics_level,
614 sizeof(SMU7_Fusion_GraphicsLevel) * SMU7_MAX_LEVELS_GRAPHICS,
615 pi->sram_end);
616
617 if (ret)
618 return ret;
619
620 ret = kv_copy_bytes_to_smc(rdev,
621 pi->dpm_table_start +
622 offsetof(SMU7_Fusion_DpmTable, GraphicsDpmLevelCount),
623 &pi->graphics_dpm_level_count,
624 sizeof(u8), pi->sram_end);
625
626 return ret;
627}
628
629static u32 kv_get_clock_difference(u32 a, u32 b)
630{
631 return (a >= b) ? a - b : b - a;
632}
633
634static u32 kv_get_clk_bypass(struct radeon_device *rdev, u32 clk)
635{
636 struct kv_power_info *pi = kv_get_pi(rdev);
637 u32 value;
638
639 if (pi->caps_enable_dfs_bypass) {
640 if (kv_get_clock_difference(clk, 40000) < 200)
641 value = 3;
642 else if (kv_get_clock_difference(clk, 30000) < 200)
643 value = 2;
644 else if (kv_get_clock_difference(clk, 20000) < 200)
645 value = 7;
646 else if (kv_get_clock_difference(clk, 15000) < 200)
647 value = 6;
648 else if (kv_get_clock_difference(clk, 10000) < 200)
649 value = 8;
650 else
651 value = 0;
652 } else {
653 value = 0;
654 }
655
656 return value;
657}
658
659static int kv_populate_uvd_table(struct radeon_device *rdev)
660{
661 struct kv_power_info *pi = kv_get_pi(rdev);
662 struct radeon_uvd_clock_voltage_dependency_table *table =
663 &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
664 struct atom_clock_dividers dividers;
665 int ret;
666 u32 i;
667
668 if (table == NULL || table->count == 0)
669 return 0;
670
671 pi->uvd_level_count = 0;
672 for (i = 0; i < table->count; i++) {
673 if (pi->high_voltage_t &&
674 (pi->high_voltage_t < table->entries[i].v))
675 break;
676
677 pi->uvd_level[i].VclkFrequency = cpu_to_be32(table->entries[i].vclk);
678 pi->uvd_level[i].DclkFrequency = cpu_to_be32(table->entries[i].dclk);
679 pi->uvd_level[i].MinVddNb = cpu_to_be16(table->entries[i].v);
680
681 pi->uvd_level[i].VClkBypassCntl =
682 (u8)kv_get_clk_bypass(rdev, table->entries[i].vclk);
683 pi->uvd_level[i].DClkBypassCntl =
684 (u8)kv_get_clk_bypass(rdev, table->entries[i].dclk);
685
686 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
687 table->entries[i].vclk, false, ÷rs);
688 if (ret)
689 return ret;
690 pi->uvd_level[i].VclkDivider = (u8)dividers.post_div;
691
692 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
693 table->entries[i].dclk, false, ÷rs);
694 if (ret)
695 return ret;
696 pi->uvd_level[i].DclkDivider = (u8)dividers.post_div;
697
698 pi->uvd_level_count++;
699 }
700
701 ret = kv_copy_bytes_to_smc(rdev,
702 pi->dpm_table_start +
703 offsetof(SMU7_Fusion_DpmTable, UvdLevelCount),
704 (u8 *)&pi->uvd_level_count,
705 sizeof(u8), pi->sram_end);
706 if (ret)
707 return ret;
708
709 pi->uvd_interval = 1;
710
711 ret = kv_copy_bytes_to_smc(rdev,
712 pi->dpm_table_start +
713 offsetof(SMU7_Fusion_DpmTable, UVDInterval),
714 &pi->uvd_interval,
715 sizeof(u8), pi->sram_end);
716 if (ret)
717 return ret;
718
719 ret = kv_copy_bytes_to_smc(rdev,
720 pi->dpm_table_start +
721 offsetof(SMU7_Fusion_DpmTable, UvdLevel),
722 (u8 *)&pi->uvd_level,
723 sizeof(SMU7_Fusion_UvdLevel) * SMU7_MAX_LEVELS_UVD,
724 pi->sram_end);
725
726 return ret;
727
728}
729
730static int kv_populate_vce_table(struct radeon_device *rdev)
731{
732 struct kv_power_info *pi = kv_get_pi(rdev);
733 int ret;
734 u32 i;
735 struct radeon_vce_clock_voltage_dependency_table *table =
736 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
737 struct atom_clock_dividers dividers;
738
739 if (table == NULL || table->count == 0)
740 return 0;
741
742 pi->vce_level_count = 0;
743 for (i = 0; i < table->count; i++) {
744 if (pi->high_voltage_t &&
745 pi->high_voltage_t < table->entries[i].v)
746 break;
747
748 pi->vce_level[i].Frequency = cpu_to_be32(table->entries[i].evclk);
749 pi->vce_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
750
751 pi->vce_level[i].ClkBypassCntl =
752 (u8)kv_get_clk_bypass(rdev, table->entries[i].evclk);
753
754 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
755 table->entries[i].evclk, false, ÷rs);
756 if (ret)
757 return ret;
758 pi->vce_level[i].Divider = (u8)dividers.post_div;
759
760 pi->vce_level_count++;
761 }
762
763 ret = kv_copy_bytes_to_smc(rdev,
764 pi->dpm_table_start +
765 offsetof(SMU7_Fusion_DpmTable, VceLevelCount),
766 (u8 *)&pi->vce_level_count,
767 sizeof(u8),
768 pi->sram_end);
769 if (ret)
770 return ret;
771
772 pi->vce_interval = 1;
773
774 ret = kv_copy_bytes_to_smc(rdev,
775 pi->dpm_table_start +
776 offsetof(SMU7_Fusion_DpmTable, VCEInterval),
777 (u8 *)&pi->vce_interval,
778 sizeof(u8),
779 pi->sram_end);
780 if (ret)
781 return ret;
782
783 ret = kv_copy_bytes_to_smc(rdev,
784 pi->dpm_table_start +
785 offsetof(SMU7_Fusion_DpmTable, VceLevel),
786 (u8 *)&pi->vce_level,
787 sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_VCE,
788 pi->sram_end);
789
790 return ret;
791}
792
793static int kv_populate_samu_table(struct radeon_device *rdev)
794{
795 struct kv_power_info *pi = kv_get_pi(rdev);
796 struct radeon_clock_voltage_dependency_table *table =
797 &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
798 struct atom_clock_dividers dividers;
799 int ret;
800 u32 i;
801
802 if (table == NULL || table->count == 0)
803 return 0;
804
805 pi->samu_level_count = 0;
806 for (i = 0; i < table->count; i++) {
807 if (pi->high_voltage_t &&
808 pi->high_voltage_t < table->entries[i].v)
809 break;
810
811 pi->samu_level[i].Frequency = cpu_to_be32(table->entries[i].clk);
812 pi->samu_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
813
814 pi->samu_level[i].ClkBypassCntl =
815 (u8)kv_get_clk_bypass(rdev, table->entries[i].clk);
816
817 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
818 table->entries[i].clk, false, ÷rs);
819 if (ret)
820 return ret;
821 pi->samu_level[i].Divider = (u8)dividers.post_div;
822
823 pi->samu_level_count++;
824 }
825
826 ret = kv_copy_bytes_to_smc(rdev,
827 pi->dpm_table_start +
828 offsetof(SMU7_Fusion_DpmTable, SamuLevelCount),
829 (u8 *)&pi->samu_level_count,
830 sizeof(u8),
831 pi->sram_end);
832 if (ret)
833 return ret;
834
835 pi->samu_interval = 1;
836
837 ret = kv_copy_bytes_to_smc(rdev,
838 pi->dpm_table_start +
839 offsetof(SMU7_Fusion_DpmTable, SAMUInterval),
840 (u8 *)&pi->samu_interval,
841 sizeof(u8),
842 pi->sram_end);
843 if (ret)
844 return ret;
845
846 ret = kv_copy_bytes_to_smc(rdev,
847 pi->dpm_table_start +
848 offsetof(SMU7_Fusion_DpmTable, SamuLevel),
849 (u8 *)&pi->samu_level,
850 sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_SAMU,
851 pi->sram_end);
852 if (ret)
853 return ret;
854
855 return ret;
856}
857
858
859static int kv_populate_acp_table(struct radeon_device *rdev)
860{
861 struct kv_power_info *pi = kv_get_pi(rdev);
862 struct radeon_clock_voltage_dependency_table *table =
863 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
864 struct atom_clock_dividers dividers;
865 int ret;
866 u32 i;
867
868 if (table == NULL || table->count == 0)
869 return 0;
870
871 pi->acp_level_count = 0;
872 for (i = 0; i < table->count; i++) {
873 pi->acp_level[i].Frequency = cpu_to_be32(table->entries[i].clk);
874 pi->acp_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
875
876 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
877 table->entries[i].clk, false, ÷rs);
878 if (ret)
879 return ret;
880 pi->acp_level[i].Divider = (u8)dividers.post_div;
881
882 pi->acp_level_count++;
883 }
884
885 ret = kv_copy_bytes_to_smc(rdev,
886 pi->dpm_table_start +
887 offsetof(SMU7_Fusion_DpmTable, AcpLevelCount),
888 (u8 *)&pi->acp_level_count,
889 sizeof(u8),
890 pi->sram_end);
891 if (ret)
892 return ret;
893
894 pi->acp_interval = 1;
895
896 ret = kv_copy_bytes_to_smc(rdev,
897 pi->dpm_table_start +
898 offsetof(SMU7_Fusion_DpmTable, ACPInterval),
899 (u8 *)&pi->acp_interval,
900 sizeof(u8),
901 pi->sram_end);
902 if (ret)
903 return ret;
904
905 ret = kv_copy_bytes_to_smc(rdev,
906 pi->dpm_table_start +
907 offsetof(SMU7_Fusion_DpmTable, AcpLevel),
908 (u8 *)&pi->acp_level,
909 sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_ACP,
910 pi->sram_end);
911 if (ret)
912 return ret;
913
914 return ret;
915}
916
917static void kv_calculate_dfs_bypass_settings(struct radeon_device *rdev)
918{
919 struct kv_power_info *pi = kv_get_pi(rdev);
920 u32 i;
921 struct radeon_clock_voltage_dependency_table *table =
922 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
923
924 if (table && table->count) {
925 for (i = 0; i < pi->graphics_dpm_level_count; i++) {
926 if (pi->caps_enable_dfs_bypass) {
927 if (kv_get_clock_difference(table->entries[i].clk, 40000) < 200)
928 pi->graphics_level[i].ClkBypassCntl = 3;
929 else if (kv_get_clock_difference(table->entries[i].clk, 30000) < 200)
930 pi->graphics_level[i].ClkBypassCntl = 2;
931 else if (kv_get_clock_difference(table->entries[i].clk, 26600) < 200)
932 pi->graphics_level[i].ClkBypassCntl = 7;
933 else if (kv_get_clock_difference(table->entries[i].clk, 20000) < 200)
934 pi->graphics_level[i].ClkBypassCntl = 6;
935 else if (kv_get_clock_difference(table->entries[i].clk, 10000) < 200)
936 pi->graphics_level[i].ClkBypassCntl = 8;
937 else
938 pi->graphics_level[i].ClkBypassCntl = 0;
939 } else {
940 pi->graphics_level[i].ClkBypassCntl = 0;
941 }
942 }
943 } else {
944 struct sumo_sclk_voltage_mapping_table *table =
945 &pi->sys_info.sclk_voltage_mapping_table;
946 for (i = 0; i < pi->graphics_dpm_level_count; i++) {
947 if (pi->caps_enable_dfs_bypass) {
948 if (kv_get_clock_difference(table->entries[i].sclk_frequency, 40000) < 200)
949 pi->graphics_level[i].ClkBypassCntl = 3;
950 else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 30000) < 200)
951 pi->graphics_level[i].ClkBypassCntl = 2;
952 else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 26600) < 200)
953 pi->graphics_level[i].ClkBypassCntl = 7;
954 else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 20000) < 200)
955 pi->graphics_level[i].ClkBypassCntl = 6;
956 else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 10000) < 200)
957 pi->graphics_level[i].ClkBypassCntl = 8;
958 else
959 pi->graphics_level[i].ClkBypassCntl = 0;
960 } else {
961 pi->graphics_level[i].ClkBypassCntl = 0;
962 }
963 }
964 }
965}
966
967static int kv_enable_ulv(struct radeon_device *rdev, bool enable)
968{
969 return kv_notify_message_to_smu(rdev, enable ?
970 PPSMC_MSG_EnableULV : PPSMC_MSG_DisableULV);
971}
972
973static void kv_reset_acp_boot_level(struct radeon_device *rdev)
974{
975 struct kv_power_info *pi = kv_get_pi(rdev);
976
977 pi->acp_boot_level = 0xff;
978}
979
980static void kv_update_current_ps(struct radeon_device *rdev,
981 struct radeon_ps *rps)
982{
983 struct kv_ps *new_ps = kv_get_ps(rps);
984 struct kv_power_info *pi = kv_get_pi(rdev);
985
986 pi->current_rps = *rps;
987 pi->current_ps = *new_ps;
988 pi->current_rps.ps_priv = &pi->current_ps;
989}
990
991static void kv_update_requested_ps(struct radeon_device *rdev,
992 struct radeon_ps *rps)
993{
994 struct kv_ps *new_ps = kv_get_ps(rps);
995 struct kv_power_info *pi = kv_get_pi(rdev);
996
997 pi->requested_rps = *rps;
998 pi->requested_ps = *new_ps;
999 pi->requested_rps.ps_priv = &pi->requested_ps;
1000}
1001
1002void kv_dpm_enable_bapm(struct radeon_device *rdev, bool enable)
1003{
1004 struct kv_power_info *pi = kv_get_pi(rdev);
1005 int ret;
1006
1007 if (pi->bapm_enable) {
1008 ret = kv_smc_bapm_enable(rdev, enable);
1009 if (ret)
1010 DRM_ERROR("kv_smc_bapm_enable failed\n");
1011 }
1012}
1013
1014static void kv_enable_thermal_int(struct radeon_device *rdev, bool enable)
1015{
1016 u32 thermal_int;
1017
1018 thermal_int = RREG32_SMC(CG_THERMAL_INT_CTRL);
1019 if (enable)
1020 thermal_int |= THERM_INTH_MASK | THERM_INTL_MASK;
1021 else
1022 thermal_int &= ~(THERM_INTH_MASK | THERM_INTL_MASK);
1023 WREG32_SMC(CG_THERMAL_INT_CTRL, thermal_int);
1024
1025}
1026
1027int kv_dpm_enable(struct radeon_device *rdev)
1028{
1029 struct kv_power_info *pi = kv_get_pi(rdev);
1030 int ret;
1031
1032 ret = kv_process_firmware_header(rdev);
1033 if (ret) {
1034 DRM_ERROR("kv_process_firmware_header failed\n");
1035 return ret;
1036 }
1037 kv_init_fps_limits(rdev);
1038 kv_init_graphics_levels(rdev);
1039 ret = kv_program_bootup_state(rdev);
1040 if (ret) {
1041 DRM_ERROR("kv_program_bootup_state failed\n");
1042 return ret;
1043 }
1044 kv_calculate_dfs_bypass_settings(rdev);
1045 ret = kv_upload_dpm_settings(rdev);
1046 if (ret) {
1047 DRM_ERROR("kv_upload_dpm_settings failed\n");
1048 return ret;
1049 }
1050 ret = kv_populate_uvd_table(rdev);
1051 if (ret) {
1052 DRM_ERROR("kv_populate_uvd_table failed\n");
1053 return ret;
1054 }
1055 ret = kv_populate_vce_table(rdev);
1056 if (ret) {
1057 DRM_ERROR("kv_populate_vce_table failed\n");
1058 return ret;
1059 }
1060 ret = kv_populate_samu_table(rdev);
1061 if (ret) {
1062 DRM_ERROR("kv_populate_samu_table failed\n");
1063 return ret;
1064 }
1065 ret = kv_populate_acp_table(rdev);
1066 if (ret) {
1067 DRM_ERROR("kv_populate_acp_table failed\n");
1068 return ret;
1069 }
1070 kv_program_vc(rdev);
1071
1072 kv_start_am(rdev);
1073 if (pi->enable_auto_thermal_throttling) {
1074 ret = kv_enable_auto_thermal_throttling(rdev);
1075 if (ret) {
1076 DRM_ERROR("kv_enable_auto_thermal_throttling failed\n");
1077 return ret;
1078 }
1079 }
1080 ret = kv_enable_dpm_voltage_scaling(rdev);
1081 if (ret) {
1082 DRM_ERROR("kv_enable_dpm_voltage_scaling failed\n");
1083 return ret;
1084 }
1085 ret = kv_set_dpm_interval(rdev);
1086 if (ret) {
1087 DRM_ERROR("kv_set_dpm_interval failed\n");
1088 return ret;
1089 }
1090 ret = kv_set_dpm_boot_state(rdev);
1091 if (ret) {
1092 DRM_ERROR("kv_set_dpm_boot_state failed\n");
1093 return ret;
1094 }
1095 ret = kv_enable_ulv(rdev, true);
1096 if (ret) {
1097 DRM_ERROR("kv_enable_ulv failed\n");
1098 return ret;
1099 }
1100 kv_start_dpm(rdev);
1101 ret = kv_enable_didt(rdev, true);
1102 if (ret) {
1103 DRM_ERROR("kv_enable_didt failed\n");
1104 return ret;
1105 }
1106 ret = kv_enable_smc_cac(rdev, true);
1107 if (ret) {
1108 DRM_ERROR("kv_enable_smc_cac failed\n");
1109 return ret;
1110 }
1111
1112 kv_reset_acp_boot_level(rdev);
1113
1114 ret = kv_smc_bapm_enable(rdev, false);
1115 if (ret) {
1116 DRM_ERROR("kv_smc_bapm_enable failed\n");
1117 return ret;
1118 }
1119
1120 kv_update_current_ps(rdev, rdev->pm.dpm.boot_ps);
1121
1122 return ret;
1123}
1124
1125int kv_dpm_late_enable(struct radeon_device *rdev)
1126{
1127 int ret = 0;
1128
1129 if (rdev->irq.installed &&
1130 r600_is_internal_thermal_sensor(rdev->pm.int_thermal_type)) {
1131 ret = kv_set_thermal_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
1132 if (ret) {
1133 DRM_ERROR("kv_set_thermal_temperature_range failed\n");
1134 return ret;
1135 }
1136 kv_enable_thermal_int(rdev, true);
1137 }
1138
1139 /* powerdown unused blocks for now */
1140 kv_dpm_powergate_acp(rdev, true);
1141 kv_dpm_powergate_samu(rdev, true);
1142 kv_dpm_powergate_vce(rdev, true);
1143 kv_dpm_powergate_uvd(rdev, true);
1144
1145 return ret;
1146}
1147
1148void kv_dpm_disable(struct radeon_device *rdev)
1149{
1150 kv_smc_bapm_enable(rdev, false);
1151
1152 if (rdev->family == CHIP_MULLINS)
1153 kv_enable_nb_dpm(rdev, false);
1154
1155 /* powerup blocks */
1156 kv_dpm_powergate_acp(rdev, false);
1157 kv_dpm_powergate_samu(rdev, false);
1158 kv_dpm_powergate_vce(rdev, false);
1159 kv_dpm_powergate_uvd(rdev, false);
1160
1161 kv_enable_smc_cac(rdev, false);
1162 kv_enable_didt(rdev, false);
1163 kv_clear_vc(rdev);
1164 kv_stop_dpm(rdev);
1165 kv_enable_ulv(rdev, false);
1166 kv_reset_am(rdev);
1167 kv_enable_thermal_int(rdev, false);
1168
1169 kv_update_current_ps(rdev, rdev->pm.dpm.boot_ps);
1170}
1171
1172static void kv_init_sclk_t(struct radeon_device *rdev)
1173{
1174 struct kv_power_info *pi = kv_get_pi(rdev);
1175
1176 pi->low_sclk_interrupt_t = 0;
1177}
1178
1179static int kv_init_fps_limits(struct radeon_device *rdev)
1180{
1181 struct kv_power_info *pi = kv_get_pi(rdev);
1182 int ret = 0;
1183
1184 if (pi->caps_fps) {
1185 u16 tmp;
1186
1187 tmp = 45;
1188 pi->fps_high_t = cpu_to_be16(tmp);
1189 ret = kv_copy_bytes_to_smc(rdev,
1190 pi->dpm_table_start +
1191 offsetof(SMU7_Fusion_DpmTable, FpsHighT),
1192 (u8 *)&pi->fps_high_t,
1193 sizeof(u16), pi->sram_end);
1194
1195 tmp = 30;
1196 pi->fps_low_t = cpu_to_be16(tmp);
1197
1198 ret = kv_copy_bytes_to_smc(rdev,
1199 pi->dpm_table_start +
1200 offsetof(SMU7_Fusion_DpmTable, FpsLowT),
1201 (u8 *)&pi->fps_low_t,
1202 sizeof(u16), pi->sram_end);
1203
1204 }
1205 return ret;
1206}
1207
1208static void kv_init_powergate_state(struct radeon_device *rdev)
1209{
1210 struct kv_power_info *pi = kv_get_pi(rdev);
1211
1212 pi->uvd_power_gated = false;
1213 pi->vce_power_gated = false;
1214 pi->samu_power_gated = false;
1215 pi->acp_power_gated = false;
1216
1217}
1218
1219static int kv_enable_uvd_dpm(struct radeon_device *rdev, bool enable)
1220{
1221 return kv_notify_message_to_smu(rdev, enable ?
1222 PPSMC_MSG_UVDDPM_Enable : PPSMC_MSG_UVDDPM_Disable);
1223}
1224
1225static int kv_enable_vce_dpm(struct radeon_device *rdev, bool enable)
1226{
1227 return kv_notify_message_to_smu(rdev, enable ?
1228 PPSMC_MSG_VCEDPM_Enable : PPSMC_MSG_VCEDPM_Disable);
1229}
1230
1231static int kv_enable_samu_dpm(struct radeon_device *rdev, bool enable)
1232{
1233 return kv_notify_message_to_smu(rdev, enable ?
1234 PPSMC_MSG_SAMUDPM_Enable : PPSMC_MSG_SAMUDPM_Disable);
1235}
1236
1237static int kv_enable_acp_dpm(struct radeon_device *rdev, bool enable)
1238{
1239 return kv_notify_message_to_smu(rdev, enable ?
1240 PPSMC_MSG_ACPDPM_Enable : PPSMC_MSG_ACPDPM_Disable);
1241}
1242
1243static int kv_update_uvd_dpm(struct radeon_device *rdev, bool gate)
1244{
1245 struct kv_power_info *pi = kv_get_pi(rdev);
1246 struct radeon_uvd_clock_voltage_dependency_table *table =
1247 &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
1248 int ret;
1249 u32 mask;
1250
1251 if (!gate) {
1252 if (table->count)
1253 pi->uvd_boot_level = table->count - 1;
1254 else
1255 pi->uvd_boot_level = 0;
1256
1257 if (!pi->caps_uvd_dpm || pi->caps_stable_p_state) {
1258 mask = 1 << pi->uvd_boot_level;
1259 } else {
1260 mask = 0x1f;
1261 }
1262
1263 ret = kv_copy_bytes_to_smc(rdev,
1264 pi->dpm_table_start +
1265 offsetof(SMU7_Fusion_DpmTable, UvdBootLevel),
1266 (uint8_t *)&pi->uvd_boot_level,
1267 sizeof(u8), pi->sram_end);
1268 if (ret)
1269 return ret;
1270
1271 kv_send_msg_to_smc_with_parameter(rdev,
1272 PPSMC_MSG_UVDDPM_SetEnabledMask,
1273 mask);
1274 }
1275
1276 return kv_enable_uvd_dpm(rdev, !gate);
1277}
1278
1279static u8 kv_get_vce_boot_level(struct radeon_device *rdev, u32 evclk)
1280{
1281 u8 i;
1282 struct radeon_vce_clock_voltage_dependency_table *table =
1283 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1284
1285 for (i = 0; i < table->count; i++) {
1286 if (table->entries[i].evclk >= evclk)
1287 break;
1288 }
1289
1290 return i;
1291}
1292
1293static int kv_update_vce_dpm(struct radeon_device *rdev,
1294 struct radeon_ps *radeon_new_state,
1295 struct radeon_ps *radeon_current_state)
1296{
1297 struct kv_power_info *pi = kv_get_pi(rdev);
1298 struct radeon_vce_clock_voltage_dependency_table *table =
1299 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1300 int ret;
1301
1302 if (radeon_new_state->evclk > 0 && radeon_current_state->evclk == 0) {
1303 kv_dpm_powergate_vce(rdev, false);
1304 /* turn the clocks on when encoding */
1305 cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, false);
1306 if (pi->caps_stable_p_state)
1307 pi->vce_boot_level = table->count - 1;
1308 else
1309 pi->vce_boot_level = kv_get_vce_boot_level(rdev, radeon_new_state->evclk);
1310
1311 ret = kv_copy_bytes_to_smc(rdev,
1312 pi->dpm_table_start +
1313 offsetof(SMU7_Fusion_DpmTable, VceBootLevel),
1314 (u8 *)&pi->vce_boot_level,
1315 sizeof(u8),
1316 pi->sram_end);
1317 if (ret)
1318 return ret;
1319
1320 if (pi->caps_stable_p_state)
1321 kv_send_msg_to_smc_with_parameter(rdev,
1322 PPSMC_MSG_VCEDPM_SetEnabledMask,
1323 (1 << pi->vce_boot_level));
1324
1325 kv_enable_vce_dpm(rdev, true);
1326 } else if (radeon_new_state->evclk == 0 && radeon_current_state->evclk > 0) {
1327 kv_enable_vce_dpm(rdev, false);
1328 /* turn the clocks off when not encoding */
1329 cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, true);
1330 kv_dpm_powergate_vce(rdev, true);
1331 }
1332
1333 return 0;
1334}
1335
1336static int kv_update_samu_dpm(struct radeon_device *rdev, bool gate)
1337{
1338 struct kv_power_info *pi = kv_get_pi(rdev);
1339 struct radeon_clock_voltage_dependency_table *table =
1340 &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
1341 int ret;
1342
1343 if (!gate) {
1344 if (pi->caps_stable_p_state)
1345 pi->samu_boot_level = table->count - 1;
1346 else
1347 pi->samu_boot_level = 0;
1348
1349 ret = kv_copy_bytes_to_smc(rdev,
1350 pi->dpm_table_start +
1351 offsetof(SMU7_Fusion_DpmTable, SamuBootLevel),
1352 (u8 *)&pi->samu_boot_level,
1353 sizeof(u8),
1354 pi->sram_end);
1355 if (ret)
1356 return ret;
1357
1358 if (pi->caps_stable_p_state)
1359 kv_send_msg_to_smc_with_parameter(rdev,
1360 PPSMC_MSG_SAMUDPM_SetEnabledMask,
1361 (1 << pi->samu_boot_level));
1362 }
1363
1364 return kv_enable_samu_dpm(rdev, !gate);
1365}
1366
1367static u8 kv_get_acp_boot_level(struct radeon_device *rdev)
1368{
1369 u8 i;
1370 struct radeon_clock_voltage_dependency_table *table =
1371 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1372
1373 for (i = 0; i < table->count; i++) {
1374 if (table->entries[i].clk >= 0) /* XXX */
1375 break;
1376 }
1377
1378 if (i >= table->count)
1379 i = table->count - 1;
1380
1381 return i;
1382}
1383
1384static void kv_update_acp_boot_level(struct radeon_device *rdev)
1385{
1386 struct kv_power_info *pi = kv_get_pi(rdev);
1387 u8 acp_boot_level;
1388
1389 if (!pi->caps_stable_p_state) {
1390 acp_boot_level = kv_get_acp_boot_level(rdev);
1391 if (acp_boot_level != pi->acp_boot_level) {
1392 pi->acp_boot_level = acp_boot_level;
1393 kv_send_msg_to_smc_with_parameter(rdev,
1394 PPSMC_MSG_ACPDPM_SetEnabledMask,
1395 (1 << pi->acp_boot_level));
1396 }
1397 }
1398}
1399
1400static int kv_update_acp_dpm(struct radeon_device *rdev, bool gate)
1401{
1402 struct kv_power_info *pi = kv_get_pi(rdev);
1403 struct radeon_clock_voltage_dependency_table *table =
1404 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1405 int ret;
1406
1407 if (!gate) {
1408 if (pi->caps_stable_p_state)
1409 pi->acp_boot_level = table->count - 1;
1410 else
1411 pi->acp_boot_level = kv_get_acp_boot_level(rdev);
1412
1413 ret = kv_copy_bytes_to_smc(rdev,
1414 pi->dpm_table_start +
1415 offsetof(SMU7_Fusion_DpmTable, AcpBootLevel),
1416 (u8 *)&pi->acp_boot_level,
1417 sizeof(u8),
1418 pi->sram_end);
1419 if (ret)
1420 return ret;
1421
1422 if (pi->caps_stable_p_state)
1423 kv_send_msg_to_smc_with_parameter(rdev,
1424 PPSMC_MSG_ACPDPM_SetEnabledMask,
1425 (1 << pi->acp_boot_level));
1426 }
1427
1428 return kv_enable_acp_dpm(rdev, !gate);
1429}
1430
1431void kv_dpm_powergate_uvd(struct radeon_device *rdev, bool gate)
1432{
1433 struct kv_power_info *pi = kv_get_pi(rdev);
1434
1435 if (pi->uvd_power_gated == gate)
1436 return;
1437
1438 pi->uvd_power_gated = gate;
1439
1440 if (gate) {
1441 if (pi->caps_uvd_pg) {
1442 uvd_v1_0_stop(rdev);
1443 cik_update_cg(rdev, RADEON_CG_BLOCK_UVD, false);
1444 }
1445 kv_update_uvd_dpm(rdev, gate);
1446 if (pi->caps_uvd_pg)
1447 kv_notify_message_to_smu(rdev, PPSMC_MSG_UVDPowerOFF);
1448 } else {
1449 if (pi->caps_uvd_pg) {
1450 kv_notify_message_to_smu(rdev, PPSMC_MSG_UVDPowerON);
1451 uvd_v4_2_resume(rdev);
1452 uvd_v1_0_start(rdev);
1453 cik_update_cg(rdev, RADEON_CG_BLOCK_UVD, true);
1454 }
1455 kv_update_uvd_dpm(rdev, gate);
1456 }
1457}
1458
1459static void kv_dpm_powergate_vce(struct radeon_device *rdev, bool gate)
1460{
1461 struct kv_power_info *pi = kv_get_pi(rdev);
1462
1463 if (pi->vce_power_gated == gate)
1464 return;
1465
1466 pi->vce_power_gated = gate;
1467
1468 if (gate) {
1469 if (pi->caps_vce_pg) {
1470 /* XXX do we need a vce_v1_0_stop() ? */
1471 kv_notify_message_to_smu(rdev, PPSMC_MSG_VCEPowerOFF);
1472 }
1473 } else {
1474 if (pi->caps_vce_pg) {
1475 kv_notify_message_to_smu(rdev, PPSMC_MSG_VCEPowerON);
1476 vce_v2_0_resume(rdev);
1477 vce_v1_0_start(rdev);
1478 }
1479 }
1480}
1481
1482static void kv_dpm_powergate_samu(struct radeon_device *rdev, bool gate)
1483{
1484 struct kv_power_info *pi = kv_get_pi(rdev);
1485
1486 if (pi->samu_power_gated == gate)
1487 return;
1488
1489 pi->samu_power_gated = gate;
1490
1491 if (gate) {
1492 kv_update_samu_dpm(rdev, true);
1493 if (pi->caps_samu_pg)
1494 kv_notify_message_to_smu(rdev, PPSMC_MSG_SAMPowerOFF);
1495 } else {
1496 if (pi->caps_samu_pg)
1497 kv_notify_message_to_smu(rdev, PPSMC_MSG_SAMPowerON);
1498 kv_update_samu_dpm(rdev, false);
1499 }
1500}
1501
1502static void kv_dpm_powergate_acp(struct radeon_device *rdev, bool gate)
1503{
1504 struct kv_power_info *pi = kv_get_pi(rdev);
1505
1506 if (pi->acp_power_gated == gate)
1507 return;
1508
1509 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
1510 return;
1511
1512 pi->acp_power_gated = gate;
1513
1514 if (gate) {
1515 kv_update_acp_dpm(rdev, true);
1516 if (pi->caps_acp_pg)
1517 kv_notify_message_to_smu(rdev, PPSMC_MSG_ACPPowerOFF);
1518 } else {
1519 if (pi->caps_acp_pg)
1520 kv_notify_message_to_smu(rdev, PPSMC_MSG_ACPPowerON);
1521 kv_update_acp_dpm(rdev, false);
1522 }
1523}
1524
1525static void kv_set_valid_clock_range(struct radeon_device *rdev,
1526 struct radeon_ps *new_rps)
1527{
1528 struct kv_ps *new_ps = kv_get_ps(new_rps);
1529 struct kv_power_info *pi = kv_get_pi(rdev);
1530 u32 i;
1531 struct radeon_clock_voltage_dependency_table *table =
1532 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1533
1534 if (table && table->count) {
1535 for (i = 0; i < pi->graphics_dpm_level_count; i++) {
1536 if ((table->entries[i].clk >= new_ps->levels[0].sclk) ||
1537 (i == (pi->graphics_dpm_level_count - 1))) {
1538 pi->lowest_valid = i;
1539 break;
1540 }
1541 }
1542
1543 for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
1544 if (table->entries[i].clk <= new_ps->levels[new_ps->num_levels - 1].sclk)
1545 break;
1546 }
1547 pi->highest_valid = i;
1548
1549 if (pi->lowest_valid > pi->highest_valid) {
1550 if ((new_ps->levels[0].sclk - table->entries[pi->highest_valid].clk) >
1551 (table->entries[pi->lowest_valid].clk - new_ps->levels[new_ps->num_levels - 1].sclk))
1552 pi->highest_valid = pi->lowest_valid;
1553 else
1554 pi->lowest_valid = pi->highest_valid;
1555 }
1556 } else {
1557 struct sumo_sclk_voltage_mapping_table *table =
1558 &pi->sys_info.sclk_voltage_mapping_table;
1559
1560 for (i = 0; i < (int)pi->graphics_dpm_level_count; i++) {
1561 if (table->entries[i].sclk_frequency >= new_ps->levels[0].sclk ||
1562 i == (int)(pi->graphics_dpm_level_count - 1)) {
1563 pi->lowest_valid = i;
1564 break;
1565 }
1566 }
1567
1568 for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
1569 if (table->entries[i].sclk_frequency <=
1570 new_ps->levels[new_ps->num_levels - 1].sclk)
1571 break;
1572 }
1573 pi->highest_valid = i;
1574
1575 if (pi->lowest_valid > pi->highest_valid) {
1576 if ((new_ps->levels[0].sclk -
1577 table->entries[pi->highest_valid].sclk_frequency) >
1578 (table->entries[pi->lowest_valid].sclk_frequency -
1579 new_ps->levels[new_ps->num_levels - 1].sclk))
1580 pi->highest_valid = pi->lowest_valid;
1581 else
1582 pi->lowest_valid = pi->highest_valid;
1583 }
1584 }
1585}
1586
1587static int kv_update_dfs_bypass_settings(struct radeon_device *rdev,
1588 struct radeon_ps *new_rps)
1589{
1590 struct kv_ps *new_ps = kv_get_ps(new_rps);
1591 struct kv_power_info *pi = kv_get_pi(rdev);
1592 int ret = 0;
1593 u8 clk_bypass_cntl;
1594
1595 if (pi->caps_enable_dfs_bypass) {
1596 clk_bypass_cntl = new_ps->need_dfs_bypass ?
1597 pi->graphics_level[pi->graphics_boot_level].ClkBypassCntl : 0;
1598 ret = kv_copy_bytes_to_smc(rdev,
1599 (pi->dpm_table_start +
1600 offsetof(SMU7_Fusion_DpmTable, GraphicsLevel) +
1601 (pi->graphics_boot_level * sizeof(SMU7_Fusion_GraphicsLevel)) +
1602 offsetof(SMU7_Fusion_GraphicsLevel, ClkBypassCntl)),
1603 &clk_bypass_cntl,
1604 sizeof(u8), pi->sram_end);
1605 }
1606
1607 return ret;
1608}
1609
1610static int kv_enable_nb_dpm(struct radeon_device *rdev,
1611 bool enable)
1612{
1613 struct kv_power_info *pi = kv_get_pi(rdev);
1614 int ret = 0;
1615
1616 if (enable) {
1617 if (pi->enable_nb_dpm && !pi->nb_dpm_enabled) {
1618 ret = kv_notify_message_to_smu(rdev, PPSMC_MSG_NBDPM_Enable);
1619 if (ret == 0)
1620 pi->nb_dpm_enabled = true;
1621 }
1622 } else {
1623 if (pi->enable_nb_dpm && pi->nb_dpm_enabled) {
1624 ret = kv_notify_message_to_smu(rdev, PPSMC_MSG_NBDPM_Disable);
1625 if (ret == 0)
1626 pi->nb_dpm_enabled = false;
1627 }
1628 }
1629
1630 return ret;
1631}
1632
1633int kv_dpm_force_performance_level(struct radeon_device *rdev,
1634 enum radeon_dpm_forced_level level)
1635{
1636 int ret;
1637
1638 if (level == RADEON_DPM_FORCED_LEVEL_HIGH) {
1639 ret = kv_force_dpm_highest(rdev);
1640 if (ret)
1641 return ret;
1642 } else if (level == RADEON_DPM_FORCED_LEVEL_LOW) {
1643 ret = kv_force_dpm_lowest(rdev);
1644 if (ret)
1645 return ret;
1646 } else if (level == RADEON_DPM_FORCED_LEVEL_AUTO) {
1647 ret = kv_unforce_levels(rdev);
1648 if (ret)
1649 return ret;
1650 }
1651
1652 rdev->pm.dpm.forced_level = level;
1653
1654 return 0;
1655}
1656
1657int kv_dpm_pre_set_power_state(struct radeon_device *rdev)
1658{
1659 struct kv_power_info *pi = kv_get_pi(rdev);
1660 struct radeon_ps requested_ps = *rdev->pm.dpm.requested_ps;
1661 struct radeon_ps *new_ps = &requested_ps;
1662
1663 kv_update_requested_ps(rdev, new_ps);
1664
1665 kv_apply_state_adjust_rules(rdev,
1666 &pi->requested_rps,
1667 &pi->current_rps);
1668
1669 return 0;
1670}
1671
1672int kv_dpm_set_power_state(struct radeon_device *rdev)
1673{
1674 struct kv_power_info *pi = kv_get_pi(rdev);
1675 struct radeon_ps *new_ps = &pi->requested_rps;
1676 struct radeon_ps *old_ps = &pi->current_rps;
1677 int ret;
1678
1679 if (pi->bapm_enable) {
1680 ret = kv_smc_bapm_enable(rdev, rdev->pm.dpm.ac_power);
1681 if (ret) {
1682 DRM_ERROR("kv_smc_bapm_enable failed\n");
1683 return ret;
1684 }
1685 }
1686
1687 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS) {
1688 if (pi->enable_dpm) {
1689 kv_set_valid_clock_range(rdev, new_ps);
1690 kv_update_dfs_bypass_settings(rdev, new_ps);
1691 ret = kv_calculate_ds_divider(rdev);
1692 if (ret) {
1693 DRM_ERROR("kv_calculate_ds_divider failed\n");
1694 return ret;
1695 }
1696 kv_calculate_nbps_level_settings(rdev);
1697 kv_calculate_dpm_settings(rdev);
1698 kv_force_lowest_valid(rdev);
1699 kv_enable_new_levels(rdev);
1700 kv_upload_dpm_settings(rdev);
1701 kv_program_nbps_index_settings(rdev, new_ps);
1702 kv_unforce_levels(rdev);
1703 kv_set_enabled_levels(rdev);
1704 kv_force_lowest_valid(rdev);
1705 kv_unforce_levels(rdev);
1706
1707 ret = kv_update_vce_dpm(rdev, new_ps, old_ps);
1708 if (ret) {
1709 DRM_ERROR("kv_update_vce_dpm failed\n");
1710 return ret;
1711 }
1712 kv_update_sclk_t(rdev);
1713 if (rdev->family == CHIP_MULLINS)
1714 kv_enable_nb_dpm(rdev, true);
1715 }
1716 } else {
1717 if (pi->enable_dpm) {
1718 kv_set_valid_clock_range(rdev, new_ps);
1719 kv_update_dfs_bypass_settings(rdev, new_ps);
1720 ret = kv_calculate_ds_divider(rdev);
1721 if (ret) {
1722 DRM_ERROR("kv_calculate_ds_divider failed\n");
1723 return ret;
1724 }
1725 kv_calculate_nbps_level_settings(rdev);
1726 kv_calculate_dpm_settings(rdev);
1727 kv_freeze_sclk_dpm(rdev, true);
1728 kv_upload_dpm_settings(rdev);
1729 kv_program_nbps_index_settings(rdev, new_ps);
1730 kv_freeze_sclk_dpm(rdev, false);
1731 kv_set_enabled_levels(rdev);
1732 ret = kv_update_vce_dpm(rdev, new_ps, old_ps);
1733 if (ret) {
1734 DRM_ERROR("kv_update_vce_dpm failed\n");
1735 return ret;
1736 }
1737 kv_update_acp_boot_level(rdev);
1738 kv_update_sclk_t(rdev);
1739 kv_enable_nb_dpm(rdev, true);
1740 }
1741 }
1742
1743 return 0;
1744}
1745
1746void kv_dpm_post_set_power_state(struct radeon_device *rdev)
1747{
1748 struct kv_power_info *pi = kv_get_pi(rdev);
1749 struct radeon_ps *new_ps = &pi->requested_rps;
1750
1751 kv_update_current_ps(rdev, new_ps);
1752}
1753
1754void kv_dpm_setup_asic(struct radeon_device *rdev)
1755{
1756 sumo_take_smu_control(rdev, true);
1757 kv_init_powergate_state(rdev);
1758 kv_init_sclk_t(rdev);
1759}
1760
1761//XXX use sumo_dpm_display_configuration_changed
1762
1763static void kv_construct_max_power_limits_table(struct radeon_device *rdev,
1764 struct radeon_clock_and_voltage_limits *table)
1765{
1766 struct kv_power_info *pi = kv_get_pi(rdev);
1767
1768 if (pi->sys_info.sclk_voltage_mapping_table.num_max_dpm_entries > 0) {
1769 int idx = pi->sys_info.sclk_voltage_mapping_table.num_max_dpm_entries - 1;
1770 table->sclk =
1771 pi->sys_info.sclk_voltage_mapping_table.entries[idx].sclk_frequency;
1772 table->vddc =
1773 kv_convert_2bit_index_to_voltage(rdev,
1774 pi->sys_info.sclk_voltage_mapping_table.entries[idx].vid_2bit);
1775 }
1776
1777 table->mclk = pi->sys_info.nbp_memory_clock[0];
1778}
1779
1780static void kv_patch_voltage_values(struct radeon_device *rdev)
1781{
1782 int i;
1783 struct radeon_uvd_clock_voltage_dependency_table *uvd_table =
1784 &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
1785 struct radeon_vce_clock_voltage_dependency_table *vce_table =
1786 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1787 struct radeon_clock_voltage_dependency_table *samu_table =
1788 &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
1789 struct radeon_clock_voltage_dependency_table *acp_table =
1790 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1791
1792 if (uvd_table->count) {
1793 for (i = 0; i < uvd_table->count; i++)
1794 uvd_table->entries[i].v =
1795 kv_convert_8bit_index_to_voltage(rdev,
1796 uvd_table->entries[i].v);
1797 }
1798
1799 if (vce_table->count) {
1800 for (i = 0; i < vce_table->count; i++)
1801 vce_table->entries[i].v =
1802 kv_convert_8bit_index_to_voltage(rdev,
1803 vce_table->entries[i].v);
1804 }
1805
1806 if (samu_table->count) {
1807 for (i = 0; i < samu_table->count; i++)
1808 samu_table->entries[i].v =
1809 kv_convert_8bit_index_to_voltage(rdev,
1810 samu_table->entries[i].v);
1811 }
1812
1813 if (acp_table->count) {
1814 for (i = 0; i < acp_table->count; i++)
1815 acp_table->entries[i].v =
1816 kv_convert_8bit_index_to_voltage(rdev,
1817 acp_table->entries[i].v);
1818 }
1819
1820}
1821
1822static void kv_construct_boot_state(struct radeon_device *rdev)
1823{
1824 struct kv_power_info *pi = kv_get_pi(rdev);
1825
1826 pi->boot_pl.sclk = pi->sys_info.bootup_sclk;
1827 pi->boot_pl.vddc_index = pi->sys_info.bootup_nb_voltage_index;
1828 pi->boot_pl.ds_divider_index = 0;
1829 pi->boot_pl.ss_divider_index = 0;
1830 pi->boot_pl.allow_gnb_slow = 1;
1831 pi->boot_pl.force_nbp_state = 0;
1832 pi->boot_pl.display_wm = 0;
1833 pi->boot_pl.vce_wm = 0;
1834}
1835
1836static int kv_force_dpm_highest(struct radeon_device *rdev)
1837{
1838 int ret;
1839 u32 enable_mask, i;
1840
1841 ret = kv_dpm_get_enable_mask(rdev, &enable_mask);
1842 if (ret)
1843 return ret;
1844
1845 for (i = SMU7_MAX_LEVELS_GRAPHICS - 1; i > 0; i--) {
1846 if (enable_mask & (1 << i))
1847 break;
1848 }
1849
1850 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
1851 return kv_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_DPM_ForceState, i);
1852 else
1853 return kv_set_enabled_level(rdev, i);
1854}
1855
1856static int kv_force_dpm_lowest(struct radeon_device *rdev)
1857{
1858 int ret;
1859 u32 enable_mask, i;
1860
1861 ret = kv_dpm_get_enable_mask(rdev, &enable_mask);
1862 if (ret)
1863 return ret;
1864
1865 for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++) {
1866 if (enable_mask & (1 << i))
1867 break;
1868 }
1869
1870 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
1871 return kv_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_DPM_ForceState, i);
1872 else
1873 return kv_set_enabled_level(rdev, i);
1874}
1875
1876static u8 kv_get_sleep_divider_id_from_clock(struct radeon_device *rdev,
1877 u32 sclk, u32 min_sclk_in_sr)
1878{
1879 struct kv_power_info *pi = kv_get_pi(rdev);
1880 u32 i;
1881 u32 temp;
1882 u32 min = (min_sclk_in_sr > KV_MINIMUM_ENGINE_CLOCK) ?
1883 min_sclk_in_sr : KV_MINIMUM_ENGINE_CLOCK;
1884
1885 if (sclk < min)
1886 return 0;
1887
1888 if (!pi->caps_sclk_ds)
1889 return 0;
1890
1891 for (i = KV_MAX_DEEPSLEEP_DIVIDER_ID; i > 0; i--) {
1892 temp = sclk / sumo_get_sleep_divider_from_id(i);
1893 if (temp >= min)
1894 break;
1895 }
1896
1897 return (u8)i;
1898}
1899
1900static int kv_get_high_voltage_limit(struct radeon_device *rdev, int *limit)
1901{
1902 struct kv_power_info *pi = kv_get_pi(rdev);
1903 struct radeon_clock_voltage_dependency_table *table =
1904 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1905 int i;
1906
1907 if (table && table->count) {
1908 for (i = table->count - 1; i >= 0; i--) {
1909 if (pi->high_voltage_t &&
1910 (kv_convert_8bit_index_to_voltage(rdev, table->entries[i].v) <=
1911 pi->high_voltage_t)) {
1912 *limit = i;
1913 return 0;
1914 }
1915 }
1916 } else {
1917 struct sumo_sclk_voltage_mapping_table *table =
1918 &pi->sys_info.sclk_voltage_mapping_table;
1919
1920 for (i = table->num_max_dpm_entries - 1; i >= 0; i--) {
1921 if (pi->high_voltage_t &&
1922 (kv_convert_2bit_index_to_voltage(rdev, table->entries[i].vid_2bit) <=
1923 pi->high_voltage_t)) {
1924 *limit = i;
1925 return 0;
1926 }
1927 }
1928 }
1929
1930 *limit = 0;
1931 return 0;
1932}
1933
1934static void kv_apply_state_adjust_rules(struct radeon_device *rdev,
1935 struct radeon_ps *new_rps,
1936 struct radeon_ps *old_rps)
1937{
1938 struct kv_ps *ps = kv_get_ps(new_rps);
1939 struct kv_power_info *pi = kv_get_pi(rdev);
1940 u32 min_sclk = 10000; /* ??? */
1941 u32 sclk, mclk = 0;
1942 int i, limit;
1943 bool force_high;
1944 struct radeon_clock_voltage_dependency_table *table =
1945 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1946 u32 stable_p_state_sclk = 0;
1947 struct radeon_clock_and_voltage_limits *max_limits =
1948 &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
1949
1950 if (new_rps->vce_active) {
1951 new_rps->evclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].evclk;
1952 new_rps->ecclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].ecclk;
1953 } else {
1954 new_rps->evclk = 0;
1955 new_rps->ecclk = 0;
1956 }
1957
1958 mclk = max_limits->mclk;
1959 sclk = min_sclk;
1960
1961 if (pi->caps_stable_p_state) {
1962 stable_p_state_sclk = (max_limits->sclk * 75) / 100;
1963
1964 for (i = table->count - 1; i >= 0; i--) {
1965 if (stable_p_state_sclk >= table->entries[i].clk) {
1966 stable_p_state_sclk = table->entries[i].clk;
1967 break;
1968 }
1969 }
1970
1971 if (i > 0)
1972 stable_p_state_sclk = table->entries[0].clk;
1973
1974 sclk = stable_p_state_sclk;
1975 }
1976
1977 if (new_rps->vce_active) {
1978 if (sclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk)
1979 sclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk;
1980 }
1981
1982 ps->need_dfs_bypass = true;
1983
1984 for (i = 0; i < ps->num_levels; i++) {
1985 if (ps->levels[i].sclk < sclk)
1986 ps->levels[i].sclk = sclk;
1987 }
1988
1989 if (table && table->count) {
1990 for (i = 0; i < ps->num_levels; i++) {
1991 if (pi->high_voltage_t &&
1992 (pi->high_voltage_t <
1993 kv_convert_8bit_index_to_voltage(rdev, ps->levels[i].vddc_index))) {
1994 kv_get_high_voltage_limit(rdev, &limit);
1995 ps->levels[i].sclk = table->entries[limit].clk;
1996 }
1997 }
1998 } else {
1999 struct sumo_sclk_voltage_mapping_table *table =
2000 &pi->sys_info.sclk_voltage_mapping_table;
2001
2002 for (i = 0; i < ps->num_levels; i++) {
2003 if (pi->high_voltage_t &&
2004 (pi->high_voltage_t <
2005 kv_convert_8bit_index_to_voltage(rdev, ps->levels[i].vddc_index))) {
2006 kv_get_high_voltage_limit(rdev, &limit);
2007 ps->levels[i].sclk = table->entries[limit].sclk_frequency;
2008 }
2009 }
2010 }
2011
2012 if (pi->caps_stable_p_state) {
2013 for (i = 0; i < ps->num_levels; i++) {
2014 ps->levels[i].sclk = stable_p_state_sclk;
2015 }
2016 }
2017
2018 pi->video_start = new_rps->dclk || new_rps->vclk ||
2019 new_rps->evclk || new_rps->ecclk;
2020
2021 if ((new_rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
2022 ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
2023 pi->battery_state = true;
2024 else
2025 pi->battery_state = false;
2026
2027 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS) {
2028 ps->dpm0_pg_nb_ps_lo = 0x1;
2029 ps->dpm0_pg_nb_ps_hi = 0x0;
2030 ps->dpmx_nb_ps_lo = 0x1;
2031 ps->dpmx_nb_ps_hi = 0x0;
2032 } else {
2033 ps->dpm0_pg_nb_ps_lo = 0x3;
2034 ps->dpm0_pg_nb_ps_hi = 0x0;
2035 ps->dpmx_nb_ps_lo = 0x3;
2036 ps->dpmx_nb_ps_hi = 0x0;
2037
2038 if (pi->sys_info.nb_dpm_enable) {
2039 force_high = (mclk >= pi->sys_info.nbp_memory_clock[3]) ||
2040 pi->video_start || (rdev->pm.dpm.new_active_crtc_count >= 3) ||
2041 pi->disable_nb_ps3_in_battery;
2042 ps->dpm0_pg_nb_ps_lo = force_high ? 0x2 : 0x3;
2043 ps->dpm0_pg_nb_ps_hi = 0x2;
2044 ps->dpmx_nb_ps_lo = force_high ? 0x2 : 0x3;
2045 ps->dpmx_nb_ps_hi = 0x2;
2046 }
2047 }
2048}
2049
2050static void kv_dpm_power_level_enabled_for_throttle(struct radeon_device *rdev,
2051 u32 index, bool enable)
2052{
2053 struct kv_power_info *pi = kv_get_pi(rdev);
2054
2055 pi->graphics_level[index].EnabledForThrottle = enable ? 1 : 0;
2056}
2057
2058static int kv_calculate_ds_divider(struct radeon_device *rdev)
2059{
2060 struct kv_power_info *pi = kv_get_pi(rdev);
2061 u32 sclk_in_sr = 10000; /* ??? */
2062 u32 i;
2063
2064 if (pi->lowest_valid > pi->highest_valid)
2065 return -EINVAL;
2066
2067 for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2068 pi->graphics_level[i].DeepSleepDivId =
2069 kv_get_sleep_divider_id_from_clock(rdev,
2070 be32_to_cpu(pi->graphics_level[i].SclkFrequency),
2071 sclk_in_sr);
2072 }
2073 return 0;
2074}
2075
2076static int kv_calculate_nbps_level_settings(struct radeon_device *rdev)
2077{
2078 struct kv_power_info *pi = kv_get_pi(rdev);
2079 u32 i;
2080 bool force_high;
2081 struct radeon_clock_and_voltage_limits *max_limits =
2082 &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
2083 u32 mclk = max_limits->mclk;
2084
2085 if (pi->lowest_valid > pi->highest_valid)
2086 return -EINVAL;
2087
2088 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS) {
2089 for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2090 pi->graphics_level[i].GnbSlow = 1;
2091 pi->graphics_level[i].ForceNbPs1 = 0;
2092 pi->graphics_level[i].UpH = 0;
2093 }
2094
2095 if (!pi->sys_info.nb_dpm_enable)
2096 return 0;
2097
2098 force_high = ((mclk >= pi->sys_info.nbp_memory_clock[3]) ||
2099 (rdev->pm.dpm.new_active_crtc_count >= 3) || pi->video_start);
2100
2101 if (force_high) {
2102 for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2103 pi->graphics_level[i].GnbSlow = 0;
2104 } else {
2105 if (pi->battery_state)
2106 pi->graphics_level[0].ForceNbPs1 = 1;
2107
2108 pi->graphics_level[1].GnbSlow = 0;
2109 pi->graphics_level[2].GnbSlow = 0;
2110 pi->graphics_level[3].GnbSlow = 0;
2111 pi->graphics_level[4].GnbSlow = 0;
2112 }
2113 } else {
2114 for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2115 pi->graphics_level[i].GnbSlow = 1;
2116 pi->graphics_level[i].ForceNbPs1 = 0;
2117 pi->graphics_level[i].UpH = 0;
2118 }
2119
2120 if (pi->sys_info.nb_dpm_enable && pi->battery_state) {
2121 pi->graphics_level[pi->lowest_valid].UpH = 0x28;
2122 pi->graphics_level[pi->lowest_valid].GnbSlow = 0;
2123 if (pi->lowest_valid != pi->highest_valid)
2124 pi->graphics_level[pi->lowest_valid].ForceNbPs1 = 1;
2125 }
2126 }
2127 return 0;
2128}
2129
2130static int kv_calculate_dpm_settings(struct radeon_device *rdev)
2131{
2132 struct kv_power_info *pi = kv_get_pi(rdev);
2133 u32 i;
2134
2135 if (pi->lowest_valid > pi->highest_valid)
2136 return -EINVAL;
2137
2138 for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2139 pi->graphics_level[i].DisplayWatermark = (i == pi->highest_valid) ? 1 : 0;
2140
2141 return 0;
2142}
2143
2144static void kv_init_graphics_levels(struct radeon_device *rdev)
2145{
2146 struct kv_power_info *pi = kv_get_pi(rdev);
2147 u32 i;
2148 struct radeon_clock_voltage_dependency_table *table =
2149 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
2150
2151 if (table && table->count) {
2152 u32 vid_2bit;
2153
2154 pi->graphics_dpm_level_count = 0;
2155 for (i = 0; i < table->count; i++) {
2156 if (pi->high_voltage_t &&
2157 (pi->high_voltage_t <
2158 kv_convert_8bit_index_to_voltage(rdev, table->entries[i].v)))
2159 break;
2160
2161 kv_set_divider_value(rdev, i, table->entries[i].clk);
2162 vid_2bit = kv_convert_vid7_to_vid2(rdev,
2163 &pi->sys_info.vid_mapping_table,
2164 table->entries[i].v);
2165 kv_set_vid(rdev, i, vid_2bit);
2166 kv_set_at(rdev, i, pi->at[i]);
2167 kv_dpm_power_level_enabled_for_throttle(rdev, i, true);
2168 pi->graphics_dpm_level_count++;
2169 }
2170 } else {
2171 struct sumo_sclk_voltage_mapping_table *table =
2172 &pi->sys_info.sclk_voltage_mapping_table;
2173
2174 pi->graphics_dpm_level_count = 0;
2175 for (i = 0; i < table->num_max_dpm_entries; i++) {
2176 if (pi->high_voltage_t &&
2177 pi->high_voltage_t <
2178 kv_convert_2bit_index_to_voltage(rdev, table->entries[i].vid_2bit))
2179 break;
2180
2181 kv_set_divider_value(rdev, i, table->entries[i].sclk_frequency);
2182 kv_set_vid(rdev, i, table->entries[i].vid_2bit);
2183 kv_set_at(rdev, i, pi->at[i]);
2184 kv_dpm_power_level_enabled_for_throttle(rdev, i, true);
2185 pi->graphics_dpm_level_count++;
2186 }
2187 }
2188
2189 for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++)
2190 kv_dpm_power_level_enable(rdev, i, false);
2191}
2192
2193static void kv_enable_new_levels(struct radeon_device *rdev)
2194{
2195 struct kv_power_info *pi = kv_get_pi(rdev);
2196 u32 i;
2197
2198 for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++) {
2199 if (i >= pi->lowest_valid && i <= pi->highest_valid)
2200 kv_dpm_power_level_enable(rdev, i, true);
2201 }
2202}
2203
2204static int kv_set_enabled_level(struct radeon_device *rdev, u32 level)
2205{
2206 u32 new_mask = (1 << level);
2207
2208 return kv_send_msg_to_smc_with_parameter(rdev,
2209 PPSMC_MSG_SCLKDPM_SetEnabledMask,
2210 new_mask);
2211}
2212
2213static int kv_set_enabled_levels(struct radeon_device *rdev)
2214{
2215 struct kv_power_info *pi = kv_get_pi(rdev);
2216 u32 i, new_mask = 0;
2217
2218 for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2219 new_mask |= (1 << i);
2220
2221 return kv_send_msg_to_smc_with_parameter(rdev,
2222 PPSMC_MSG_SCLKDPM_SetEnabledMask,
2223 new_mask);
2224}
2225
2226static void kv_program_nbps_index_settings(struct radeon_device *rdev,
2227 struct radeon_ps *new_rps)
2228{
2229 struct kv_ps *new_ps = kv_get_ps(new_rps);
2230 struct kv_power_info *pi = kv_get_pi(rdev);
2231 u32 nbdpmconfig1;
2232
2233 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
2234 return;
2235
2236 if (pi->sys_info.nb_dpm_enable) {
2237 nbdpmconfig1 = RREG32_SMC(NB_DPM_CONFIG_1);
2238 nbdpmconfig1 &= ~(Dpm0PgNbPsLo_MASK | Dpm0PgNbPsHi_MASK |
2239 DpmXNbPsLo_MASK | DpmXNbPsHi_MASK);
2240 nbdpmconfig1 |= (Dpm0PgNbPsLo(new_ps->dpm0_pg_nb_ps_lo) |
2241 Dpm0PgNbPsHi(new_ps->dpm0_pg_nb_ps_hi) |
2242 DpmXNbPsLo(new_ps->dpmx_nb_ps_lo) |
2243 DpmXNbPsHi(new_ps->dpmx_nb_ps_hi));
2244 WREG32_SMC(NB_DPM_CONFIG_1, nbdpmconfig1);
2245 }
2246}
2247
2248static int kv_set_thermal_temperature_range(struct radeon_device *rdev,
2249 int min_temp, int max_temp)
2250{
2251 int low_temp = 0 * 1000;
2252 int high_temp = 255 * 1000;
2253 u32 tmp;
2254
2255 if (low_temp < min_temp)
2256 low_temp = min_temp;
2257 if (high_temp > max_temp)
2258 high_temp = max_temp;
2259 if (high_temp < low_temp) {
2260 DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
2261 return -EINVAL;
2262 }
2263
2264 tmp = RREG32_SMC(CG_THERMAL_INT_CTRL);
2265 tmp &= ~(DIG_THERM_INTH_MASK | DIG_THERM_INTL_MASK);
2266 tmp |= (DIG_THERM_INTH(49 + (high_temp / 1000)) |
2267 DIG_THERM_INTL(49 + (low_temp / 1000)));
2268 WREG32_SMC(CG_THERMAL_INT_CTRL, tmp);
2269
2270 rdev->pm.dpm.thermal.min_temp = low_temp;
2271 rdev->pm.dpm.thermal.max_temp = high_temp;
2272
2273 return 0;
2274}
2275
2276union igp_info {
2277 struct _ATOM_INTEGRATED_SYSTEM_INFO info;
2278 struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
2279 struct _ATOM_INTEGRATED_SYSTEM_INFO_V5 info_5;
2280 struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
2281 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
2282 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
2283};
2284
2285static int kv_parse_sys_info_table(struct radeon_device *rdev)
2286{
2287 struct kv_power_info *pi = kv_get_pi(rdev);
2288 struct radeon_mode_info *mode_info = &rdev->mode_info;
2289 int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
2290 union igp_info *igp_info;
2291 u8 frev, crev;
2292 u16 data_offset;
2293 int i;
2294
2295 if (atom_parse_data_header(mode_info->atom_context, index, NULL,
2296 &frev, &crev, &data_offset)) {
2297 igp_info = (union igp_info *)(mode_info->atom_context->bios +
2298 data_offset);
2299
2300 if (crev != 8) {
2301 DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
2302 return -EINVAL;
2303 }
2304 pi->sys_info.bootup_sclk = le32_to_cpu(igp_info->info_8.ulBootUpEngineClock);
2305 pi->sys_info.bootup_uma_clk = le32_to_cpu(igp_info->info_8.ulBootUpUMAClock);
2306 pi->sys_info.bootup_nb_voltage_index =
2307 le16_to_cpu(igp_info->info_8.usBootUpNBVoltage);
2308 if (igp_info->info_8.ucHtcTmpLmt == 0)
2309 pi->sys_info.htc_tmp_lmt = 203;
2310 else
2311 pi->sys_info.htc_tmp_lmt = igp_info->info_8.ucHtcTmpLmt;
2312 if (igp_info->info_8.ucHtcHystLmt == 0)
2313 pi->sys_info.htc_hyst_lmt = 5;
2314 else
2315 pi->sys_info.htc_hyst_lmt = igp_info->info_8.ucHtcHystLmt;
2316 if (pi->sys_info.htc_tmp_lmt <= pi->sys_info.htc_hyst_lmt) {
2317 DRM_ERROR("The htcTmpLmt should be larger than htcHystLmt.\n");
2318 }
2319
2320 if (le32_to_cpu(igp_info->info_8.ulSystemConfig) & (1 << 3))
2321 pi->sys_info.nb_dpm_enable = true;
2322 else
2323 pi->sys_info.nb_dpm_enable = false;
2324
2325 for (i = 0; i < KV_NUM_NBPSTATES; i++) {
2326 pi->sys_info.nbp_memory_clock[i] =
2327 le32_to_cpu(igp_info->info_8.ulNbpStateMemclkFreq[i]);
2328 pi->sys_info.nbp_n_clock[i] =
2329 le32_to_cpu(igp_info->info_8.ulNbpStateNClkFreq[i]);
2330 }
2331 if (le32_to_cpu(igp_info->info_8.ulGPUCapInfo) &
2332 SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS)
2333 pi->caps_enable_dfs_bypass = true;
2334
2335 sumo_construct_sclk_voltage_mapping_table(rdev,
2336 &pi->sys_info.sclk_voltage_mapping_table,
2337 igp_info->info_8.sAvail_SCLK);
2338
2339 sumo_construct_vid_mapping_table(rdev,
2340 &pi->sys_info.vid_mapping_table,
2341 igp_info->info_8.sAvail_SCLK);
2342
2343 kv_construct_max_power_limits_table(rdev,
2344 &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
2345 }
2346 return 0;
2347}
2348
2349union power_info {
2350 struct _ATOM_POWERPLAY_INFO info;
2351 struct _ATOM_POWERPLAY_INFO_V2 info_2;
2352 struct _ATOM_POWERPLAY_INFO_V3 info_3;
2353 struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
2354 struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
2355 struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
2356};
2357
2358union pplib_clock_info {
2359 struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
2360 struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
2361 struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
2362 struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
2363};
2364
2365union pplib_power_state {
2366 struct _ATOM_PPLIB_STATE v1;
2367 struct _ATOM_PPLIB_STATE_V2 v2;
2368};
2369
2370static void kv_patch_boot_state(struct radeon_device *rdev,
2371 struct kv_ps *ps)
2372{
2373 struct kv_power_info *pi = kv_get_pi(rdev);
2374
2375 ps->num_levels = 1;
2376 ps->levels[0] = pi->boot_pl;
2377}
2378
2379static void kv_parse_pplib_non_clock_info(struct radeon_device *rdev,
2380 struct radeon_ps *rps,
2381 struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
2382 u8 table_rev)
2383{
2384 struct kv_ps *ps = kv_get_ps(rps);
2385
2386 rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
2387 rps->class = le16_to_cpu(non_clock_info->usClassification);
2388 rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
2389
2390 if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
2391 rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
2392 rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
2393 } else {
2394 rps->vclk = 0;
2395 rps->dclk = 0;
2396 }
2397
2398 if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
2399 rdev->pm.dpm.boot_ps = rps;
2400 kv_patch_boot_state(rdev, ps);
2401 }
2402 if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
2403 rdev->pm.dpm.uvd_ps = rps;
2404}
2405
2406static void kv_parse_pplib_clock_info(struct radeon_device *rdev,
2407 struct radeon_ps *rps, int index,
2408 union pplib_clock_info *clock_info)
2409{
2410 struct kv_power_info *pi = kv_get_pi(rdev);
2411 struct kv_ps *ps = kv_get_ps(rps);
2412 struct kv_pl *pl = &ps->levels[index];
2413 u32 sclk;
2414
2415 sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
2416 sclk |= clock_info->sumo.ucEngineClockHigh << 16;
2417 pl->sclk = sclk;
2418 pl->vddc_index = clock_info->sumo.vddcIndex;
2419
2420 ps->num_levels = index + 1;
2421
2422 if (pi->caps_sclk_ds) {
2423 pl->ds_divider_index = 5;
2424 pl->ss_divider_index = 5;
2425 }
2426}
2427
2428static int kv_parse_power_table(struct radeon_device *rdev)
2429{
2430 struct radeon_mode_info *mode_info = &rdev->mode_info;
2431 struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
2432 union pplib_power_state *power_state;
2433 int i, j, k, non_clock_array_index, clock_array_index;
2434 union pplib_clock_info *clock_info;
2435 struct _StateArray *state_array;
2436 struct _ClockInfoArray *clock_info_array;
2437 struct _NonClockInfoArray *non_clock_info_array;
2438 union power_info *power_info;
2439 int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2440 u16 data_offset;
2441 u8 frev, crev;
2442 u8 *power_state_offset;
2443 struct kv_ps *ps;
2444
2445 if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
2446 &frev, &crev, &data_offset))
2447 return -EINVAL;
2448 power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
2449
2450 state_array = (struct _StateArray *)
2451 (mode_info->atom_context->bios + data_offset +
2452 le16_to_cpu(power_info->pplib.usStateArrayOffset));
2453 clock_info_array = (struct _ClockInfoArray *)
2454 (mode_info->atom_context->bios + data_offset +
2455 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
2456 non_clock_info_array = (struct _NonClockInfoArray *)
2457 (mode_info->atom_context->bios + data_offset +
2458 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
2459
2460 rdev->pm.dpm.ps = kcalloc(state_array->ucNumEntries,
2461 sizeof(struct radeon_ps),
2462 GFP_KERNEL);
2463 if (!rdev->pm.dpm.ps)
2464 return -ENOMEM;
2465 power_state_offset = (u8 *)state_array->states;
2466 for (i = 0; i < state_array->ucNumEntries; i++) {
2467 u8 *idx;
2468 power_state = (union pplib_power_state *)power_state_offset;
2469 non_clock_array_index = power_state->v2.nonClockInfoIndex;
2470 non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
2471 &non_clock_info_array->nonClockInfo[non_clock_array_index];
2472 if (!rdev->pm.power_state[i].clock_info)
2473 return -EINVAL;
2474 ps = kzalloc(sizeof(struct kv_ps), GFP_KERNEL);
2475 if (ps == NULL) {
2476 kfree(rdev->pm.dpm.ps);
2477 return -ENOMEM;
2478 }
2479 rdev->pm.dpm.ps[i].ps_priv = ps;
2480 k = 0;
2481 idx = (u8 *)&power_state->v2.clockInfoIndex[0];
2482 for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
2483 clock_array_index = idx[j];
2484 if (clock_array_index >= clock_info_array->ucNumEntries)
2485 continue;
2486 if (k >= SUMO_MAX_HARDWARE_POWERLEVELS)
2487 break;
2488 clock_info = (union pplib_clock_info *)
2489 ((u8 *)&clock_info_array->clockInfo[0] +
2490 (clock_array_index * clock_info_array->ucEntrySize));
2491 kv_parse_pplib_clock_info(rdev,
2492 &rdev->pm.dpm.ps[i], k,
2493 clock_info);
2494 k++;
2495 }
2496 kv_parse_pplib_non_clock_info(rdev, &rdev->pm.dpm.ps[i],
2497 non_clock_info,
2498 non_clock_info_array->ucEntrySize);
2499 power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
2500 }
2501 rdev->pm.dpm.num_ps = state_array->ucNumEntries;
2502
2503 /* fill in the vce power states */
2504 for (i = 0; i < RADEON_MAX_VCE_LEVELS; i++) {
2505 u32 sclk;
2506 clock_array_index = rdev->pm.dpm.vce_states[i].clk_idx;
2507 clock_info = (union pplib_clock_info *)
2508 &clock_info_array->clockInfo[clock_array_index * clock_info_array->ucEntrySize];
2509 sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
2510 sclk |= clock_info->sumo.ucEngineClockHigh << 16;
2511 rdev->pm.dpm.vce_states[i].sclk = sclk;
2512 rdev->pm.dpm.vce_states[i].mclk = 0;
2513 }
2514
2515 return 0;
2516}
2517
2518int kv_dpm_init(struct radeon_device *rdev)
2519{
2520 struct kv_power_info *pi;
2521 int ret, i;
2522
2523 pi = kzalloc(sizeof(struct kv_power_info), GFP_KERNEL);
2524 if (pi == NULL)
2525 return -ENOMEM;
2526 rdev->pm.dpm.priv = pi;
2527
2528 ret = r600_get_platform_caps(rdev);
2529 if (ret)
2530 return ret;
2531
2532 ret = r600_parse_extended_power_table(rdev);
2533 if (ret)
2534 return ret;
2535
2536 for (i = 0; i < SUMO_MAX_HARDWARE_POWERLEVELS; i++)
2537 pi->at[i] = TRINITY_AT_DFLT;
2538
2539 pi->sram_end = SMC_RAM_END;
2540
2541 /* Enabling nb dpm on an asrock system prevents dpm from working */
2542 if (rdev->pdev->subsystem_vendor == 0x1849)
2543 pi->enable_nb_dpm = false;
2544 else
2545 pi->enable_nb_dpm = true;
2546
2547 pi->caps_power_containment = true;
2548 pi->caps_cac = true;
2549 pi->enable_didt = false;
2550 if (pi->enable_didt) {
2551 pi->caps_sq_ramping = true;
2552 pi->caps_db_ramping = true;
2553 pi->caps_td_ramping = true;
2554 pi->caps_tcp_ramping = true;
2555 }
2556
2557 pi->caps_sclk_ds = true;
2558 pi->enable_auto_thermal_throttling = true;
2559 pi->disable_nb_ps3_in_battery = false;
2560 if (radeon_bapm == -1) {
2561 /* only enable bapm on KB, ML by default */
2562 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
2563 pi->bapm_enable = true;
2564 else
2565 pi->bapm_enable = false;
2566 } else if (radeon_bapm == 0) {
2567 pi->bapm_enable = false;
2568 } else {
2569 pi->bapm_enable = true;
2570 }
2571 pi->voltage_drop_t = 0;
2572 pi->caps_sclk_throttle_low_notification = false;
2573 pi->caps_fps = false; /* true? */
2574 pi->caps_uvd_pg = true;
2575 pi->caps_uvd_dpm = true;
2576 pi->caps_vce_pg = false; /* XXX true */
2577 pi->caps_samu_pg = false;
2578 pi->caps_acp_pg = false;
2579 pi->caps_stable_p_state = false;
2580
2581 ret = kv_parse_sys_info_table(rdev);
2582 if (ret)
2583 return ret;
2584
2585 kv_patch_voltage_values(rdev);
2586 kv_construct_boot_state(rdev);
2587
2588 ret = kv_parse_power_table(rdev);
2589 if (ret)
2590 return ret;
2591
2592 pi->enable_dpm = true;
2593
2594 return 0;
2595}
2596
2597void kv_dpm_debugfs_print_current_performance_level(struct radeon_device *rdev,
2598 struct seq_file *m)
2599{
2600 struct kv_power_info *pi = kv_get_pi(rdev);
2601 u32 current_index =
2602 (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) & CURR_SCLK_INDEX_MASK) >>
2603 CURR_SCLK_INDEX_SHIFT;
2604 u32 sclk, tmp;
2605 u16 vddc;
2606
2607 if (current_index >= SMU__NUM_SCLK_DPM_STATE) {
2608 seq_printf(m, "invalid dpm profile %d\n", current_index);
2609 } else {
2610 sclk = be32_to_cpu(pi->graphics_level[current_index].SclkFrequency);
2611 tmp = (RREG32_SMC(SMU_VOLTAGE_STATUS) & SMU_VOLTAGE_CURRENT_LEVEL_MASK) >>
2612 SMU_VOLTAGE_CURRENT_LEVEL_SHIFT;
2613 vddc = kv_convert_8bit_index_to_voltage(rdev, (u16)tmp);
2614 seq_printf(m, "uvd %sabled\n", pi->uvd_power_gated ? "dis" : "en");
2615 seq_printf(m, "vce %sabled\n", pi->vce_power_gated ? "dis" : "en");
2616 seq_printf(m, "power level %d sclk: %u vddc: %u\n",
2617 current_index, sclk, vddc);
2618 }
2619}
2620
2621u32 kv_dpm_get_current_sclk(struct radeon_device *rdev)
2622{
2623 struct kv_power_info *pi = kv_get_pi(rdev);
2624 u32 current_index =
2625 (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) & CURR_SCLK_INDEX_MASK) >>
2626 CURR_SCLK_INDEX_SHIFT;
2627 u32 sclk;
2628
2629 if (current_index >= SMU__NUM_SCLK_DPM_STATE) {
2630 return 0;
2631 } else {
2632 sclk = be32_to_cpu(pi->graphics_level[current_index].SclkFrequency);
2633 return sclk;
2634 }
2635}
2636
2637u32 kv_dpm_get_current_mclk(struct radeon_device *rdev)
2638{
2639 struct kv_power_info *pi = kv_get_pi(rdev);
2640
2641 return pi->sys_info.bootup_uma_clk;
2642}
2643
2644void kv_dpm_print_power_state(struct radeon_device *rdev,
2645 struct radeon_ps *rps)
2646{
2647 int i;
2648 struct kv_ps *ps = kv_get_ps(rps);
2649
2650 r600_dpm_print_class_info(rps->class, rps->class2);
2651 r600_dpm_print_cap_info(rps->caps);
2652 printk("\tuvd vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
2653 for (i = 0; i < ps->num_levels; i++) {
2654 struct kv_pl *pl = &ps->levels[i];
2655 printk("\t\tpower level %d sclk: %u vddc: %u\n",
2656 i, pl->sclk,
2657 kv_convert_8bit_index_to_voltage(rdev, pl->vddc_index));
2658 }
2659 r600_dpm_print_ps_status(rdev, rps);
2660}
2661
2662void kv_dpm_fini(struct radeon_device *rdev)
2663{
2664 int i;
2665
2666 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
2667 kfree(rdev->pm.dpm.ps[i].ps_priv);
2668 }
2669 kfree(rdev->pm.dpm.ps);
2670 kfree(rdev->pm.dpm.priv);
2671 r600_free_extended_power_table(rdev);
2672}
2673
2674void kv_dpm_display_configuration_changed(struct radeon_device *rdev)
2675{
2676
2677}
2678
2679u32 kv_dpm_get_sclk(struct radeon_device *rdev, bool low)
2680{
2681 struct kv_power_info *pi = kv_get_pi(rdev);
2682 struct kv_ps *requested_state = kv_get_ps(&pi->requested_rps);
2683
2684 if (low)
2685 return requested_state->levels[0].sclk;
2686 else
2687 return requested_state->levels[requested_state->num_levels - 1].sclk;
2688}
2689
2690u32 kv_dpm_get_mclk(struct radeon_device *rdev, bool low)
2691{
2692 struct kv_power_info *pi = kv_get_pi(rdev);
2693
2694 return pi->sys_info.bootup_uma_clk;
2695}
2696
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 "drmP.h"
25#include "radeon.h"
26#include "cikd.h"
27#include "r600_dpm.h"
28#include "kv_dpm.h"
29#include "radeon_asic.h"
30#include <linux/seq_file.h>
31
32#define KV_MAX_DEEPSLEEP_DIVIDER_ID 5
33#define KV_MINIMUM_ENGINE_CLOCK 800
34#define SMC_RAM_END 0x40000
35
36static void kv_init_graphics_levels(struct radeon_device *rdev);
37static int kv_calculate_ds_divider(struct radeon_device *rdev);
38static int kv_calculate_nbps_level_settings(struct radeon_device *rdev);
39static int kv_calculate_dpm_settings(struct radeon_device *rdev);
40static void kv_enable_new_levels(struct radeon_device *rdev);
41static void kv_program_nbps_index_settings(struct radeon_device *rdev,
42 struct radeon_ps *new_rps);
43static int kv_set_enabled_level(struct radeon_device *rdev, u32 level);
44static int kv_set_enabled_levels(struct radeon_device *rdev);
45static int kv_force_dpm_highest(struct radeon_device *rdev);
46static int kv_force_dpm_lowest(struct radeon_device *rdev);
47static void kv_apply_state_adjust_rules(struct radeon_device *rdev,
48 struct radeon_ps *new_rps,
49 struct radeon_ps *old_rps);
50static int kv_set_thermal_temperature_range(struct radeon_device *rdev,
51 int min_temp, int max_temp);
52static int kv_init_fps_limits(struct radeon_device *rdev);
53
54void kv_dpm_powergate_uvd(struct radeon_device *rdev, bool gate);
55static void kv_dpm_powergate_vce(struct radeon_device *rdev, bool gate);
56static void kv_dpm_powergate_samu(struct radeon_device *rdev, bool gate);
57static void kv_dpm_powergate_acp(struct radeon_device *rdev, bool gate);
58
59extern void cik_enter_rlc_safe_mode(struct radeon_device *rdev);
60extern void cik_exit_rlc_safe_mode(struct radeon_device *rdev);
61extern void cik_update_cg(struct radeon_device *rdev,
62 u32 block, bool enable);
63
64static const struct kv_lcac_config_values sx_local_cac_cfg_kv[] =
65{
66 { 0, 4, 1 },
67 { 1, 4, 1 },
68 { 2, 5, 1 },
69 { 3, 4, 2 },
70 { 4, 1, 1 },
71 { 5, 5, 2 },
72 { 6, 6, 1 },
73 { 7, 9, 2 },
74 { 0xffffffff }
75};
76
77static const struct kv_lcac_config_values mc0_local_cac_cfg_kv[] =
78{
79 { 0, 4, 1 },
80 { 0xffffffff }
81};
82
83static const struct kv_lcac_config_values mc1_local_cac_cfg_kv[] =
84{
85 { 0, 4, 1 },
86 { 0xffffffff }
87};
88
89static const struct kv_lcac_config_values mc2_local_cac_cfg_kv[] =
90{
91 { 0, 4, 1 },
92 { 0xffffffff }
93};
94
95static const struct kv_lcac_config_values mc3_local_cac_cfg_kv[] =
96{
97 { 0, 4, 1 },
98 { 0xffffffff }
99};
100
101static const struct kv_lcac_config_values cpl_local_cac_cfg_kv[] =
102{
103 { 0, 4, 1 },
104 { 1, 4, 1 },
105 { 2, 5, 1 },
106 { 3, 4, 1 },
107 { 4, 1, 1 },
108 { 5, 5, 1 },
109 { 6, 6, 1 },
110 { 7, 9, 1 },
111 { 8, 4, 1 },
112 { 9, 2, 1 },
113 { 10, 3, 1 },
114 { 11, 6, 1 },
115 { 12, 8, 2 },
116 { 13, 1, 1 },
117 { 14, 2, 1 },
118 { 15, 3, 1 },
119 { 16, 1, 1 },
120 { 17, 4, 1 },
121 { 18, 3, 1 },
122 { 19, 1, 1 },
123 { 20, 8, 1 },
124 { 21, 5, 1 },
125 { 22, 1, 1 },
126 { 23, 1, 1 },
127 { 24, 4, 1 },
128 { 27, 6, 1 },
129 { 28, 1, 1 },
130 { 0xffffffff }
131};
132
133static const struct kv_lcac_config_reg sx0_cac_config_reg[] =
134{
135 { 0xc0400d00, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
136};
137
138static const struct kv_lcac_config_reg mc0_cac_config_reg[] =
139{
140 { 0xc0400d30, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
141};
142
143static const struct kv_lcac_config_reg mc1_cac_config_reg[] =
144{
145 { 0xc0400d3c, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
146};
147
148static const struct kv_lcac_config_reg mc2_cac_config_reg[] =
149{
150 { 0xc0400d48, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
151};
152
153static const struct kv_lcac_config_reg mc3_cac_config_reg[] =
154{
155 { 0xc0400d54, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
156};
157
158static const struct kv_lcac_config_reg cpl_cac_config_reg[] =
159{
160 { 0xc0400d80, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
161};
162
163static const struct kv_pt_config_reg didt_config_kv[] =
164{
165 { 0x10, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
166 { 0x10, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
167 { 0x10, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
168 { 0x10, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
169 { 0x11, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
170 { 0x11, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
171 { 0x11, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
172 { 0x11, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
173 { 0x12, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
174 { 0x12, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
175 { 0x12, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
176 { 0x12, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
177 { 0x2, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
178 { 0x2, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
179 { 0x2, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
180 { 0x1, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
181 { 0x1, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
182 { 0x0, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
183 { 0x30, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
184 { 0x30, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
185 { 0x30, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
186 { 0x30, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
187 { 0x31, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
188 { 0x31, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
189 { 0x31, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
190 { 0x31, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
191 { 0x32, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
192 { 0x32, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
193 { 0x32, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
194 { 0x32, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
195 { 0x22, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
196 { 0x22, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
197 { 0x22, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
198 { 0x21, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
199 { 0x21, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
200 { 0x20, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
201 { 0x50, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
202 { 0x50, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
203 { 0x50, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
204 { 0x50, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
205 { 0x51, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
206 { 0x51, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
207 { 0x51, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
208 { 0x51, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
209 { 0x52, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
210 { 0x52, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
211 { 0x52, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
212 { 0x52, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
213 { 0x42, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
214 { 0x42, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
215 { 0x42, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
216 { 0x41, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
217 { 0x41, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
218 { 0x40, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
219 { 0x70, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
220 { 0x70, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
221 { 0x70, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
222 { 0x70, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
223 { 0x71, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
224 { 0x71, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
225 { 0x71, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
226 { 0x71, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
227 { 0x72, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
228 { 0x72, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
229 { 0x72, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
230 { 0x72, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
231 { 0x62, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
232 { 0x62, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
233 { 0x62, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
234 { 0x61, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
235 { 0x61, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
236 { 0x60, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
237 { 0xFFFFFFFF }
238};
239
240static struct kv_ps *kv_get_ps(struct radeon_ps *rps)
241{
242 struct kv_ps *ps = rps->ps_priv;
243
244 return ps;
245}
246
247static struct kv_power_info *kv_get_pi(struct radeon_device *rdev)
248{
249 struct kv_power_info *pi = rdev->pm.dpm.priv;
250
251 return pi;
252}
253
254#if 0
255static void kv_program_local_cac_table(struct radeon_device *rdev,
256 const struct kv_lcac_config_values *local_cac_table,
257 const struct kv_lcac_config_reg *local_cac_reg)
258{
259 u32 i, count, data;
260 const struct kv_lcac_config_values *values = local_cac_table;
261
262 while (values->block_id != 0xffffffff) {
263 count = values->signal_id;
264 for (i = 0; i < count; i++) {
265 data = ((values->block_id << local_cac_reg->block_shift) &
266 local_cac_reg->block_mask);
267 data |= ((i << local_cac_reg->signal_shift) &
268 local_cac_reg->signal_mask);
269 data |= ((values->t << local_cac_reg->t_shift) &
270 local_cac_reg->t_mask);
271 data |= ((1 << local_cac_reg->enable_shift) &
272 local_cac_reg->enable_mask);
273 WREG32_SMC(local_cac_reg->cntl, data);
274 }
275 values++;
276 }
277}
278#endif
279
280static int kv_program_pt_config_registers(struct radeon_device *rdev,
281 const struct kv_pt_config_reg *cac_config_regs)
282{
283 const struct kv_pt_config_reg *config_regs = cac_config_regs;
284 u32 data;
285 u32 cache = 0;
286
287 if (config_regs == NULL)
288 return -EINVAL;
289
290 while (config_regs->offset != 0xFFFFFFFF) {
291 if (config_regs->type == KV_CONFIGREG_CACHE) {
292 cache |= ((config_regs->value << config_regs->shift) & config_regs->mask);
293 } else {
294 switch (config_regs->type) {
295 case KV_CONFIGREG_SMC_IND:
296 data = RREG32_SMC(config_regs->offset);
297 break;
298 case KV_CONFIGREG_DIDT_IND:
299 data = RREG32_DIDT(config_regs->offset);
300 break;
301 default:
302 data = RREG32(config_regs->offset << 2);
303 break;
304 }
305
306 data &= ~config_regs->mask;
307 data |= ((config_regs->value << config_regs->shift) & config_regs->mask);
308 data |= cache;
309 cache = 0;
310
311 switch (config_regs->type) {
312 case KV_CONFIGREG_SMC_IND:
313 WREG32_SMC(config_regs->offset, data);
314 break;
315 case KV_CONFIGREG_DIDT_IND:
316 WREG32_DIDT(config_regs->offset, data);
317 break;
318 default:
319 WREG32(config_regs->offset << 2, data);
320 break;
321 }
322 }
323 config_regs++;
324 }
325
326 return 0;
327}
328
329static void kv_do_enable_didt(struct radeon_device *rdev, bool enable)
330{
331 struct kv_power_info *pi = kv_get_pi(rdev);
332 u32 data;
333
334 if (pi->caps_sq_ramping) {
335 data = RREG32_DIDT(DIDT_SQ_CTRL0);
336 if (enable)
337 data |= DIDT_CTRL_EN;
338 else
339 data &= ~DIDT_CTRL_EN;
340 WREG32_DIDT(DIDT_SQ_CTRL0, data);
341 }
342
343 if (pi->caps_db_ramping) {
344 data = RREG32_DIDT(DIDT_DB_CTRL0);
345 if (enable)
346 data |= DIDT_CTRL_EN;
347 else
348 data &= ~DIDT_CTRL_EN;
349 WREG32_DIDT(DIDT_DB_CTRL0, data);
350 }
351
352 if (pi->caps_td_ramping) {
353 data = RREG32_DIDT(DIDT_TD_CTRL0);
354 if (enable)
355 data |= DIDT_CTRL_EN;
356 else
357 data &= ~DIDT_CTRL_EN;
358 WREG32_DIDT(DIDT_TD_CTRL0, data);
359 }
360
361 if (pi->caps_tcp_ramping) {
362 data = RREG32_DIDT(DIDT_TCP_CTRL0);
363 if (enable)
364 data |= DIDT_CTRL_EN;
365 else
366 data &= ~DIDT_CTRL_EN;
367 WREG32_DIDT(DIDT_TCP_CTRL0, data);
368 }
369}
370
371static int kv_enable_didt(struct radeon_device *rdev, bool enable)
372{
373 struct kv_power_info *pi = kv_get_pi(rdev);
374 int ret;
375
376 if (pi->caps_sq_ramping ||
377 pi->caps_db_ramping ||
378 pi->caps_td_ramping ||
379 pi->caps_tcp_ramping) {
380 cik_enter_rlc_safe_mode(rdev);
381
382 if (enable) {
383 ret = kv_program_pt_config_registers(rdev, didt_config_kv);
384 if (ret) {
385 cik_exit_rlc_safe_mode(rdev);
386 return ret;
387 }
388 }
389
390 kv_do_enable_didt(rdev, enable);
391
392 cik_exit_rlc_safe_mode(rdev);
393 }
394
395 return 0;
396}
397
398#if 0
399static void kv_initialize_hardware_cac_manager(struct radeon_device *rdev)
400{
401 struct kv_power_info *pi = kv_get_pi(rdev);
402
403 if (pi->caps_cac) {
404 WREG32_SMC(LCAC_SX0_OVR_SEL, 0);
405 WREG32_SMC(LCAC_SX0_OVR_VAL, 0);
406 kv_program_local_cac_table(rdev, sx_local_cac_cfg_kv, sx0_cac_config_reg);
407
408 WREG32_SMC(LCAC_MC0_OVR_SEL, 0);
409 WREG32_SMC(LCAC_MC0_OVR_VAL, 0);
410 kv_program_local_cac_table(rdev, mc0_local_cac_cfg_kv, mc0_cac_config_reg);
411
412 WREG32_SMC(LCAC_MC1_OVR_SEL, 0);
413 WREG32_SMC(LCAC_MC1_OVR_VAL, 0);
414 kv_program_local_cac_table(rdev, mc1_local_cac_cfg_kv, mc1_cac_config_reg);
415
416 WREG32_SMC(LCAC_MC2_OVR_SEL, 0);
417 WREG32_SMC(LCAC_MC2_OVR_VAL, 0);
418 kv_program_local_cac_table(rdev, mc2_local_cac_cfg_kv, mc2_cac_config_reg);
419
420 WREG32_SMC(LCAC_MC3_OVR_SEL, 0);
421 WREG32_SMC(LCAC_MC3_OVR_VAL, 0);
422 kv_program_local_cac_table(rdev, mc3_local_cac_cfg_kv, mc3_cac_config_reg);
423
424 WREG32_SMC(LCAC_CPL_OVR_SEL, 0);
425 WREG32_SMC(LCAC_CPL_OVR_VAL, 0);
426 kv_program_local_cac_table(rdev, cpl_local_cac_cfg_kv, cpl_cac_config_reg);
427 }
428}
429#endif
430
431static int kv_enable_smc_cac(struct radeon_device *rdev, bool enable)
432{
433 struct kv_power_info *pi = kv_get_pi(rdev);
434 int ret = 0;
435
436 if (pi->caps_cac) {
437 if (enable) {
438 ret = kv_notify_message_to_smu(rdev, PPSMC_MSG_EnableCac);
439 if (ret)
440 pi->cac_enabled = false;
441 else
442 pi->cac_enabled = true;
443 } else if (pi->cac_enabled) {
444 kv_notify_message_to_smu(rdev, PPSMC_MSG_DisableCac);
445 pi->cac_enabled = false;
446 }
447 }
448
449 return ret;
450}
451
452static int kv_process_firmware_header(struct radeon_device *rdev)
453{
454 struct kv_power_info *pi = kv_get_pi(rdev);
455 u32 tmp;
456 int ret;
457
458 ret = kv_read_smc_sram_dword(rdev, SMU7_FIRMWARE_HEADER_LOCATION +
459 offsetof(SMU7_Firmware_Header, DpmTable),
460 &tmp, pi->sram_end);
461
462 if (ret == 0)
463 pi->dpm_table_start = tmp;
464
465 ret = kv_read_smc_sram_dword(rdev, SMU7_FIRMWARE_HEADER_LOCATION +
466 offsetof(SMU7_Firmware_Header, SoftRegisters),
467 &tmp, pi->sram_end);
468
469 if (ret == 0)
470 pi->soft_regs_start = tmp;
471
472 return ret;
473}
474
475static int kv_enable_dpm_voltage_scaling(struct radeon_device *rdev)
476{
477 struct kv_power_info *pi = kv_get_pi(rdev);
478 int ret;
479
480 pi->graphics_voltage_change_enable = 1;
481
482 ret = kv_copy_bytes_to_smc(rdev,
483 pi->dpm_table_start +
484 offsetof(SMU7_Fusion_DpmTable, GraphicsVoltageChangeEnable),
485 &pi->graphics_voltage_change_enable,
486 sizeof(u8), pi->sram_end);
487
488 return ret;
489}
490
491static int kv_set_dpm_interval(struct radeon_device *rdev)
492{
493 struct kv_power_info *pi = kv_get_pi(rdev);
494 int ret;
495
496 pi->graphics_interval = 1;
497
498 ret = kv_copy_bytes_to_smc(rdev,
499 pi->dpm_table_start +
500 offsetof(SMU7_Fusion_DpmTable, GraphicsInterval),
501 &pi->graphics_interval,
502 sizeof(u8), pi->sram_end);
503
504 return ret;
505}
506
507static int kv_set_dpm_boot_state(struct radeon_device *rdev)
508{
509 struct kv_power_info *pi = kv_get_pi(rdev);
510 int ret;
511
512 ret = kv_copy_bytes_to_smc(rdev,
513 pi->dpm_table_start +
514 offsetof(SMU7_Fusion_DpmTable, GraphicsBootLevel),
515 &pi->graphics_boot_level,
516 sizeof(u8), pi->sram_end);
517
518 return ret;
519}
520
521static void kv_program_vc(struct radeon_device *rdev)
522{
523 WREG32_SMC(CG_FTV_0, 0x3FFFC100);
524}
525
526static void kv_clear_vc(struct radeon_device *rdev)
527{
528 WREG32_SMC(CG_FTV_0, 0);
529}
530
531static int kv_set_divider_value(struct radeon_device *rdev,
532 u32 index, u32 sclk)
533{
534 struct kv_power_info *pi = kv_get_pi(rdev);
535 struct atom_clock_dividers dividers;
536 int ret;
537
538 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
539 sclk, false, ÷rs);
540 if (ret)
541 return ret;
542
543 pi->graphics_level[index].SclkDid = (u8)dividers.post_div;
544 pi->graphics_level[index].SclkFrequency = cpu_to_be32(sclk);
545
546 return 0;
547}
548
549static u32 kv_convert_vid2_to_vid7(struct radeon_device *rdev,
550 struct sumo_vid_mapping_table *vid_mapping_table,
551 u32 vid_2bit)
552{
553 struct radeon_clock_voltage_dependency_table *vddc_sclk_table =
554 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
555 u32 i;
556
557 if (vddc_sclk_table && vddc_sclk_table->count) {
558 if (vid_2bit < vddc_sclk_table->count)
559 return vddc_sclk_table->entries[vid_2bit].v;
560 else
561 return vddc_sclk_table->entries[vddc_sclk_table->count - 1].v;
562 } else {
563 for (i = 0; i < vid_mapping_table->num_entries; i++) {
564 if (vid_mapping_table->entries[i].vid_2bit == vid_2bit)
565 return vid_mapping_table->entries[i].vid_7bit;
566 }
567 return vid_mapping_table->entries[vid_mapping_table->num_entries - 1].vid_7bit;
568 }
569}
570
571static u32 kv_convert_vid7_to_vid2(struct radeon_device *rdev,
572 struct sumo_vid_mapping_table *vid_mapping_table,
573 u32 vid_7bit)
574{
575 struct radeon_clock_voltage_dependency_table *vddc_sclk_table =
576 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
577 u32 i;
578
579 if (vddc_sclk_table && vddc_sclk_table->count) {
580 for (i = 0; i < vddc_sclk_table->count; i++) {
581 if (vddc_sclk_table->entries[i].v == vid_7bit)
582 return i;
583 }
584 return vddc_sclk_table->count - 1;
585 } else {
586 for (i = 0; i < vid_mapping_table->num_entries; i++) {
587 if (vid_mapping_table->entries[i].vid_7bit == vid_7bit)
588 return vid_mapping_table->entries[i].vid_2bit;
589 }
590
591 return vid_mapping_table->entries[vid_mapping_table->num_entries - 1].vid_2bit;
592 }
593}
594
595static u16 kv_convert_8bit_index_to_voltage(struct radeon_device *rdev,
596 u16 voltage)
597{
598 return 6200 - (voltage * 25);
599}
600
601static u16 kv_convert_2bit_index_to_voltage(struct radeon_device *rdev,
602 u32 vid_2bit)
603{
604 struct kv_power_info *pi = kv_get_pi(rdev);
605 u32 vid_8bit = kv_convert_vid2_to_vid7(rdev,
606 &pi->sys_info.vid_mapping_table,
607 vid_2bit);
608
609 return kv_convert_8bit_index_to_voltage(rdev, (u16)vid_8bit);
610}
611
612
613static int kv_set_vid(struct radeon_device *rdev, u32 index, u32 vid)
614{
615 struct kv_power_info *pi = kv_get_pi(rdev);
616
617 pi->graphics_level[index].VoltageDownH = (u8)pi->voltage_drop_t;
618 pi->graphics_level[index].MinVddNb =
619 cpu_to_be32(kv_convert_2bit_index_to_voltage(rdev, vid));
620
621 return 0;
622}
623
624static int kv_set_at(struct radeon_device *rdev, u32 index, u32 at)
625{
626 struct kv_power_info *pi = kv_get_pi(rdev);
627
628 pi->graphics_level[index].AT = cpu_to_be16((u16)at);
629
630 return 0;
631}
632
633static void kv_dpm_power_level_enable(struct radeon_device *rdev,
634 u32 index, bool enable)
635{
636 struct kv_power_info *pi = kv_get_pi(rdev);
637
638 pi->graphics_level[index].EnabledForActivity = enable ? 1 : 0;
639}
640
641static void kv_start_dpm(struct radeon_device *rdev)
642{
643 u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
644
645 tmp |= GLOBAL_PWRMGT_EN;
646 WREG32_SMC(GENERAL_PWRMGT, tmp);
647
648 kv_smc_dpm_enable(rdev, true);
649}
650
651static void kv_stop_dpm(struct radeon_device *rdev)
652{
653 kv_smc_dpm_enable(rdev, false);
654}
655
656static void kv_start_am(struct radeon_device *rdev)
657{
658 u32 sclk_pwrmgt_cntl = RREG32_SMC(SCLK_PWRMGT_CNTL);
659
660 sclk_pwrmgt_cntl &= ~(RESET_SCLK_CNT | RESET_BUSY_CNT);
661 sclk_pwrmgt_cntl |= DYNAMIC_PM_EN;
662
663 WREG32_SMC(SCLK_PWRMGT_CNTL, sclk_pwrmgt_cntl);
664}
665
666static void kv_reset_am(struct radeon_device *rdev)
667{
668 u32 sclk_pwrmgt_cntl = RREG32_SMC(SCLK_PWRMGT_CNTL);
669
670 sclk_pwrmgt_cntl |= (RESET_SCLK_CNT | RESET_BUSY_CNT);
671
672 WREG32_SMC(SCLK_PWRMGT_CNTL, sclk_pwrmgt_cntl);
673}
674
675static int kv_freeze_sclk_dpm(struct radeon_device *rdev, bool freeze)
676{
677 return kv_notify_message_to_smu(rdev, freeze ?
678 PPSMC_MSG_SCLKDPM_FreezeLevel : PPSMC_MSG_SCLKDPM_UnfreezeLevel);
679}
680
681static int kv_force_lowest_valid(struct radeon_device *rdev)
682{
683 return kv_force_dpm_lowest(rdev);
684}
685
686static int kv_unforce_levels(struct radeon_device *rdev)
687{
688 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
689 return kv_notify_message_to_smu(rdev, PPSMC_MSG_NoForcedLevel);
690 else
691 return kv_set_enabled_levels(rdev);
692}
693
694static int kv_update_sclk_t(struct radeon_device *rdev)
695{
696 struct kv_power_info *pi = kv_get_pi(rdev);
697 u32 low_sclk_interrupt_t = 0;
698 int ret = 0;
699
700 if (pi->caps_sclk_throttle_low_notification) {
701 low_sclk_interrupt_t = cpu_to_be32(pi->low_sclk_interrupt_t);
702
703 ret = kv_copy_bytes_to_smc(rdev,
704 pi->dpm_table_start +
705 offsetof(SMU7_Fusion_DpmTable, LowSclkInterruptT),
706 (u8 *)&low_sclk_interrupt_t,
707 sizeof(u32), pi->sram_end);
708 }
709 return ret;
710}
711
712static int kv_program_bootup_state(struct radeon_device *rdev)
713{
714 struct kv_power_info *pi = kv_get_pi(rdev);
715 u32 i;
716 struct radeon_clock_voltage_dependency_table *table =
717 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
718
719 if (table && table->count) {
720 for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
721 if (table->entries[i].clk == pi->boot_pl.sclk)
722 break;
723 }
724
725 pi->graphics_boot_level = (u8)i;
726 kv_dpm_power_level_enable(rdev, i, true);
727 } else {
728 struct sumo_sclk_voltage_mapping_table *table =
729 &pi->sys_info.sclk_voltage_mapping_table;
730
731 if (table->num_max_dpm_entries == 0)
732 return -EINVAL;
733
734 for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
735 if (table->entries[i].sclk_frequency == pi->boot_pl.sclk)
736 break;
737 }
738
739 pi->graphics_boot_level = (u8)i;
740 kv_dpm_power_level_enable(rdev, i, true);
741 }
742 return 0;
743}
744
745static int kv_enable_auto_thermal_throttling(struct radeon_device *rdev)
746{
747 struct kv_power_info *pi = kv_get_pi(rdev);
748 int ret;
749
750 pi->graphics_therm_throttle_enable = 1;
751
752 ret = kv_copy_bytes_to_smc(rdev,
753 pi->dpm_table_start +
754 offsetof(SMU7_Fusion_DpmTable, GraphicsThermThrottleEnable),
755 &pi->graphics_therm_throttle_enable,
756 sizeof(u8), pi->sram_end);
757
758 return ret;
759}
760
761static int kv_upload_dpm_settings(struct radeon_device *rdev)
762{
763 struct kv_power_info *pi = kv_get_pi(rdev);
764 int ret;
765
766 ret = kv_copy_bytes_to_smc(rdev,
767 pi->dpm_table_start +
768 offsetof(SMU7_Fusion_DpmTable, GraphicsLevel),
769 (u8 *)&pi->graphics_level,
770 sizeof(SMU7_Fusion_GraphicsLevel) * SMU7_MAX_LEVELS_GRAPHICS,
771 pi->sram_end);
772
773 if (ret)
774 return ret;
775
776 ret = kv_copy_bytes_to_smc(rdev,
777 pi->dpm_table_start +
778 offsetof(SMU7_Fusion_DpmTable, GraphicsDpmLevelCount),
779 &pi->graphics_dpm_level_count,
780 sizeof(u8), pi->sram_end);
781
782 return ret;
783}
784
785static u32 kv_get_clock_difference(u32 a, u32 b)
786{
787 return (a >= b) ? a - b : b - a;
788}
789
790static u32 kv_get_clk_bypass(struct radeon_device *rdev, u32 clk)
791{
792 struct kv_power_info *pi = kv_get_pi(rdev);
793 u32 value;
794
795 if (pi->caps_enable_dfs_bypass) {
796 if (kv_get_clock_difference(clk, 40000) < 200)
797 value = 3;
798 else if (kv_get_clock_difference(clk, 30000) < 200)
799 value = 2;
800 else if (kv_get_clock_difference(clk, 20000) < 200)
801 value = 7;
802 else if (kv_get_clock_difference(clk, 15000) < 200)
803 value = 6;
804 else if (kv_get_clock_difference(clk, 10000) < 200)
805 value = 8;
806 else
807 value = 0;
808 } else {
809 value = 0;
810 }
811
812 return value;
813}
814
815static int kv_populate_uvd_table(struct radeon_device *rdev)
816{
817 struct kv_power_info *pi = kv_get_pi(rdev);
818 struct radeon_uvd_clock_voltage_dependency_table *table =
819 &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
820 struct atom_clock_dividers dividers;
821 int ret;
822 u32 i;
823
824 if (table == NULL || table->count == 0)
825 return 0;
826
827 pi->uvd_level_count = 0;
828 for (i = 0; i < table->count; i++) {
829 if (pi->high_voltage_t &&
830 (pi->high_voltage_t < table->entries[i].v))
831 break;
832
833 pi->uvd_level[i].VclkFrequency = cpu_to_be32(table->entries[i].vclk);
834 pi->uvd_level[i].DclkFrequency = cpu_to_be32(table->entries[i].dclk);
835 pi->uvd_level[i].MinVddNb = cpu_to_be16(table->entries[i].v);
836
837 pi->uvd_level[i].VClkBypassCntl =
838 (u8)kv_get_clk_bypass(rdev, table->entries[i].vclk);
839 pi->uvd_level[i].DClkBypassCntl =
840 (u8)kv_get_clk_bypass(rdev, table->entries[i].dclk);
841
842 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
843 table->entries[i].vclk, false, ÷rs);
844 if (ret)
845 return ret;
846 pi->uvd_level[i].VclkDivider = (u8)dividers.post_div;
847
848 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
849 table->entries[i].dclk, false, ÷rs);
850 if (ret)
851 return ret;
852 pi->uvd_level[i].DclkDivider = (u8)dividers.post_div;
853
854 pi->uvd_level_count++;
855 }
856
857 ret = kv_copy_bytes_to_smc(rdev,
858 pi->dpm_table_start +
859 offsetof(SMU7_Fusion_DpmTable, UvdLevelCount),
860 (u8 *)&pi->uvd_level_count,
861 sizeof(u8), pi->sram_end);
862 if (ret)
863 return ret;
864
865 pi->uvd_interval = 1;
866
867 ret = kv_copy_bytes_to_smc(rdev,
868 pi->dpm_table_start +
869 offsetof(SMU7_Fusion_DpmTable, UVDInterval),
870 &pi->uvd_interval,
871 sizeof(u8), pi->sram_end);
872 if (ret)
873 return ret;
874
875 ret = kv_copy_bytes_to_smc(rdev,
876 pi->dpm_table_start +
877 offsetof(SMU7_Fusion_DpmTable, UvdLevel),
878 (u8 *)&pi->uvd_level,
879 sizeof(SMU7_Fusion_UvdLevel) * SMU7_MAX_LEVELS_UVD,
880 pi->sram_end);
881
882 return ret;
883
884}
885
886static int kv_populate_vce_table(struct radeon_device *rdev)
887{
888 struct kv_power_info *pi = kv_get_pi(rdev);
889 int ret;
890 u32 i;
891 struct radeon_vce_clock_voltage_dependency_table *table =
892 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
893 struct atom_clock_dividers dividers;
894
895 if (table == NULL || table->count == 0)
896 return 0;
897
898 pi->vce_level_count = 0;
899 for (i = 0; i < table->count; i++) {
900 if (pi->high_voltage_t &&
901 pi->high_voltage_t < table->entries[i].v)
902 break;
903
904 pi->vce_level[i].Frequency = cpu_to_be32(table->entries[i].evclk);
905 pi->vce_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
906
907 pi->vce_level[i].ClkBypassCntl =
908 (u8)kv_get_clk_bypass(rdev, table->entries[i].evclk);
909
910 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
911 table->entries[i].evclk, false, ÷rs);
912 if (ret)
913 return ret;
914 pi->vce_level[i].Divider = (u8)dividers.post_div;
915
916 pi->vce_level_count++;
917 }
918
919 ret = kv_copy_bytes_to_smc(rdev,
920 pi->dpm_table_start +
921 offsetof(SMU7_Fusion_DpmTable, VceLevelCount),
922 (u8 *)&pi->vce_level_count,
923 sizeof(u8),
924 pi->sram_end);
925 if (ret)
926 return ret;
927
928 pi->vce_interval = 1;
929
930 ret = kv_copy_bytes_to_smc(rdev,
931 pi->dpm_table_start +
932 offsetof(SMU7_Fusion_DpmTable, VCEInterval),
933 (u8 *)&pi->vce_interval,
934 sizeof(u8),
935 pi->sram_end);
936 if (ret)
937 return ret;
938
939 ret = kv_copy_bytes_to_smc(rdev,
940 pi->dpm_table_start +
941 offsetof(SMU7_Fusion_DpmTable, VceLevel),
942 (u8 *)&pi->vce_level,
943 sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_VCE,
944 pi->sram_end);
945
946 return ret;
947}
948
949static int kv_populate_samu_table(struct radeon_device *rdev)
950{
951 struct kv_power_info *pi = kv_get_pi(rdev);
952 struct radeon_clock_voltage_dependency_table *table =
953 &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
954 struct atom_clock_dividers dividers;
955 int ret;
956 u32 i;
957
958 if (table == NULL || table->count == 0)
959 return 0;
960
961 pi->samu_level_count = 0;
962 for (i = 0; i < table->count; i++) {
963 if (pi->high_voltage_t &&
964 pi->high_voltage_t < table->entries[i].v)
965 break;
966
967 pi->samu_level[i].Frequency = cpu_to_be32(table->entries[i].clk);
968 pi->samu_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
969
970 pi->samu_level[i].ClkBypassCntl =
971 (u8)kv_get_clk_bypass(rdev, table->entries[i].clk);
972
973 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
974 table->entries[i].clk, false, ÷rs);
975 if (ret)
976 return ret;
977 pi->samu_level[i].Divider = (u8)dividers.post_div;
978
979 pi->samu_level_count++;
980 }
981
982 ret = kv_copy_bytes_to_smc(rdev,
983 pi->dpm_table_start +
984 offsetof(SMU7_Fusion_DpmTable, SamuLevelCount),
985 (u8 *)&pi->samu_level_count,
986 sizeof(u8),
987 pi->sram_end);
988 if (ret)
989 return ret;
990
991 pi->samu_interval = 1;
992
993 ret = kv_copy_bytes_to_smc(rdev,
994 pi->dpm_table_start +
995 offsetof(SMU7_Fusion_DpmTable, SAMUInterval),
996 (u8 *)&pi->samu_interval,
997 sizeof(u8),
998 pi->sram_end);
999 if (ret)
1000 return ret;
1001
1002 ret = kv_copy_bytes_to_smc(rdev,
1003 pi->dpm_table_start +
1004 offsetof(SMU7_Fusion_DpmTable, SamuLevel),
1005 (u8 *)&pi->samu_level,
1006 sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_SAMU,
1007 pi->sram_end);
1008 if (ret)
1009 return ret;
1010
1011 return ret;
1012}
1013
1014
1015static int kv_populate_acp_table(struct radeon_device *rdev)
1016{
1017 struct kv_power_info *pi = kv_get_pi(rdev);
1018 struct radeon_clock_voltage_dependency_table *table =
1019 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1020 struct atom_clock_dividers dividers;
1021 int ret;
1022 u32 i;
1023
1024 if (table == NULL || table->count == 0)
1025 return 0;
1026
1027 pi->acp_level_count = 0;
1028 for (i = 0; i < table->count; i++) {
1029 pi->acp_level[i].Frequency = cpu_to_be32(table->entries[i].clk);
1030 pi->acp_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
1031
1032 ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
1033 table->entries[i].clk, false, ÷rs);
1034 if (ret)
1035 return ret;
1036 pi->acp_level[i].Divider = (u8)dividers.post_div;
1037
1038 pi->acp_level_count++;
1039 }
1040
1041 ret = kv_copy_bytes_to_smc(rdev,
1042 pi->dpm_table_start +
1043 offsetof(SMU7_Fusion_DpmTable, AcpLevelCount),
1044 (u8 *)&pi->acp_level_count,
1045 sizeof(u8),
1046 pi->sram_end);
1047 if (ret)
1048 return ret;
1049
1050 pi->acp_interval = 1;
1051
1052 ret = kv_copy_bytes_to_smc(rdev,
1053 pi->dpm_table_start +
1054 offsetof(SMU7_Fusion_DpmTable, ACPInterval),
1055 (u8 *)&pi->acp_interval,
1056 sizeof(u8),
1057 pi->sram_end);
1058 if (ret)
1059 return ret;
1060
1061 ret = kv_copy_bytes_to_smc(rdev,
1062 pi->dpm_table_start +
1063 offsetof(SMU7_Fusion_DpmTable, AcpLevel),
1064 (u8 *)&pi->acp_level,
1065 sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_ACP,
1066 pi->sram_end);
1067 if (ret)
1068 return ret;
1069
1070 return ret;
1071}
1072
1073static void kv_calculate_dfs_bypass_settings(struct radeon_device *rdev)
1074{
1075 struct kv_power_info *pi = kv_get_pi(rdev);
1076 u32 i;
1077 struct radeon_clock_voltage_dependency_table *table =
1078 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1079
1080 if (table && table->count) {
1081 for (i = 0; i < pi->graphics_dpm_level_count; i++) {
1082 if (pi->caps_enable_dfs_bypass) {
1083 if (kv_get_clock_difference(table->entries[i].clk, 40000) < 200)
1084 pi->graphics_level[i].ClkBypassCntl = 3;
1085 else if (kv_get_clock_difference(table->entries[i].clk, 30000) < 200)
1086 pi->graphics_level[i].ClkBypassCntl = 2;
1087 else if (kv_get_clock_difference(table->entries[i].clk, 26600) < 200)
1088 pi->graphics_level[i].ClkBypassCntl = 7;
1089 else if (kv_get_clock_difference(table->entries[i].clk , 20000) < 200)
1090 pi->graphics_level[i].ClkBypassCntl = 6;
1091 else if (kv_get_clock_difference(table->entries[i].clk , 10000) < 200)
1092 pi->graphics_level[i].ClkBypassCntl = 8;
1093 else
1094 pi->graphics_level[i].ClkBypassCntl = 0;
1095 } else {
1096 pi->graphics_level[i].ClkBypassCntl = 0;
1097 }
1098 }
1099 } else {
1100 struct sumo_sclk_voltage_mapping_table *table =
1101 &pi->sys_info.sclk_voltage_mapping_table;
1102 for (i = 0; i < pi->graphics_dpm_level_count; i++) {
1103 if (pi->caps_enable_dfs_bypass) {
1104 if (kv_get_clock_difference(table->entries[i].sclk_frequency, 40000) < 200)
1105 pi->graphics_level[i].ClkBypassCntl = 3;
1106 else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 30000) < 200)
1107 pi->graphics_level[i].ClkBypassCntl = 2;
1108 else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 26600) < 200)
1109 pi->graphics_level[i].ClkBypassCntl = 7;
1110 else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 20000) < 200)
1111 pi->graphics_level[i].ClkBypassCntl = 6;
1112 else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 10000) < 200)
1113 pi->graphics_level[i].ClkBypassCntl = 8;
1114 else
1115 pi->graphics_level[i].ClkBypassCntl = 0;
1116 } else {
1117 pi->graphics_level[i].ClkBypassCntl = 0;
1118 }
1119 }
1120 }
1121}
1122
1123static int kv_enable_ulv(struct radeon_device *rdev, bool enable)
1124{
1125 return kv_notify_message_to_smu(rdev, enable ?
1126 PPSMC_MSG_EnableULV : PPSMC_MSG_DisableULV);
1127}
1128
1129static void kv_reset_acp_boot_level(struct radeon_device *rdev)
1130{
1131 struct kv_power_info *pi = kv_get_pi(rdev);
1132
1133 pi->acp_boot_level = 0xff;
1134}
1135
1136static void kv_update_current_ps(struct radeon_device *rdev,
1137 struct radeon_ps *rps)
1138{
1139 struct kv_ps *new_ps = kv_get_ps(rps);
1140 struct kv_power_info *pi = kv_get_pi(rdev);
1141
1142 pi->current_rps = *rps;
1143 pi->current_ps = *new_ps;
1144 pi->current_rps.ps_priv = &pi->current_ps;
1145}
1146
1147static void kv_update_requested_ps(struct radeon_device *rdev,
1148 struct radeon_ps *rps)
1149{
1150 struct kv_ps *new_ps = kv_get_ps(rps);
1151 struct kv_power_info *pi = kv_get_pi(rdev);
1152
1153 pi->requested_rps = *rps;
1154 pi->requested_ps = *new_ps;
1155 pi->requested_rps.ps_priv = &pi->requested_ps;
1156}
1157
1158void kv_dpm_enable_bapm(struct radeon_device *rdev, bool enable)
1159{
1160 struct kv_power_info *pi = kv_get_pi(rdev);
1161 int ret;
1162
1163 if (pi->bapm_enable) {
1164 ret = kv_smc_bapm_enable(rdev, enable);
1165 if (ret)
1166 DRM_ERROR("kv_smc_bapm_enable failed\n");
1167 }
1168}
1169
1170int kv_dpm_enable(struct radeon_device *rdev)
1171{
1172 struct kv_power_info *pi = kv_get_pi(rdev);
1173 int ret;
1174
1175 ret = kv_process_firmware_header(rdev);
1176 if (ret) {
1177 DRM_ERROR("kv_process_firmware_header failed\n");
1178 return ret;
1179 }
1180 kv_init_fps_limits(rdev);
1181 kv_init_graphics_levels(rdev);
1182 ret = kv_program_bootup_state(rdev);
1183 if (ret) {
1184 DRM_ERROR("kv_program_bootup_state failed\n");
1185 return ret;
1186 }
1187 kv_calculate_dfs_bypass_settings(rdev);
1188 ret = kv_upload_dpm_settings(rdev);
1189 if (ret) {
1190 DRM_ERROR("kv_upload_dpm_settings failed\n");
1191 return ret;
1192 }
1193 ret = kv_populate_uvd_table(rdev);
1194 if (ret) {
1195 DRM_ERROR("kv_populate_uvd_table failed\n");
1196 return ret;
1197 }
1198 ret = kv_populate_vce_table(rdev);
1199 if (ret) {
1200 DRM_ERROR("kv_populate_vce_table failed\n");
1201 return ret;
1202 }
1203 ret = kv_populate_samu_table(rdev);
1204 if (ret) {
1205 DRM_ERROR("kv_populate_samu_table failed\n");
1206 return ret;
1207 }
1208 ret = kv_populate_acp_table(rdev);
1209 if (ret) {
1210 DRM_ERROR("kv_populate_acp_table failed\n");
1211 return ret;
1212 }
1213 kv_program_vc(rdev);
1214#if 0
1215 kv_initialize_hardware_cac_manager(rdev);
1216#endif
1217 kv_start_am(rdev);
1218 if (pi->enable_auto_thermal_throttling) {
1219 ret = kv_enable_auto_thermal_throttling(rdev);
1220 if (ret) {
1221 DRM_ERROR("kv_enable_auto_thermal_throttling failed\n");
1222 return ret;
1223 }
1224 }
1225 ret = kv_enable_dpm_voltage_scaling(rdev);
1226 if (ret) {
1227 DRM_ERROR("kv_enable_dpm_voltage_scaling failed\n");
1228 return ret;
1229 }
1230 ret = kv_set_dpm_interval(rdev);
1231 if (ret) {
1232 DRM_ERROR("kv_set_dpm_interval failed\n");
1233 return ret;
1234 }
1235 ret = kv_set_dpm_boot_state(rdev);
1236 if (ret) {
1237 DRM_ERROR("kv_set_dpm_boot_state failed\n");
1238 return ret;
1239 }
1240 ret = kv_enable_ulv(rdev, true);
1241 if (ret) {
1242 DRM_ERROR("kv_enable_ulv failed\n");
1243 return ret;
1244 }
1245 kv_start_dpm(rdev);
1246 ret = kv_enable_didt(rdev, true);
1247 if (ret) {
1248 DRM_ERROR("kv_enable_didt failed\n");
1249 return ret;
1250 }
1251 ret = kv_enable_smc_cac(rdev, true);
1252 if (ret) {
1253 DRM_ERROR("kv_enable_smc_cac failed\n");
1254 return ret;
1255 }
1256
1257 kv_reset_acp_boot_level(rdev);
1258
1259 ret = kv_smc_bapm_enable(rdev, false);
1260 if (ret) {
1261 DRM_ERROR("kv_smc_bapm_enable failed\n");
1262 return ret;
1263 }
1264
1265 kv_update_current_ps(rdev, rdev->pm.dpm.boot_ps);
1266
1267 return ret;
1268}
1269
1270int kv_dpm_late_enable(struct radeon_device *rdev)
1271{
1272 int ret = 0;
1273
1274 if (rdev->irq.installed &&
1275 r600_is_internal_thermal_sensor(rdev->pm.int_thermal_type)) {
1276 ret = kv_set_thermal_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
1277 if (ret) {
1278 DRM_ERROR("kv_set_thermal_temperature_range failed\n");
1279 return ret;
1280 }
1281 rdev->irq.dpm_thermal = true;
1282 radeon_irq_set(rdev);
1283 }
1284
1285 /* powerdown unused blocks for now */
1286 kv_dpm_powergate_acp(rdev, true);
1287 kv_dpm_powergate_samu(rdev, true);
1288 kv_dpm_powergate_vce(rdev, true);
1289 kv_dpm_powergate_uvd(rdev, true);
1290
1291 return ret;
1292}
1293
1294void kv_dpm_disable(struct radeon_device *rdev)
1295{
1296 kv_smc_bapm_enable(rdev, false);
1297
1298 /* powerup blocks */
1299 kv_dpm_powergate_acp(rdev, false);
1300 kv_dpm_powergate_samu(rdev, false);
1301 kv_dpm_powergate_vce(rdev, false);
1302 kv_dpm_powergate_uvd(rdev, false);
1303
1304 kv_enable_smc_cac(rdev, false);
1305 kv_enable_didt(rdev, false);
1306 kv_clear_vc(rdev);
1307 kv_stop_dpm(rdev);
1308 kv_enable_ulv(rdev, false);
1309 kv_reset_am(rdev);
1310
1311 kv_update_current_ps(rdev, rdev->pm.dpm.boot_ps);
1312}
1313
1314#if 0
1315static int kv_write_smc_soft_register(struct radeon_device *rdev,
1316 u16 reg_offset, u32 value)
1317{
1318 struct kv_power_info *pi = kv_get_pi(rdev);
1319
1320 return kv_copy_bytes_to_smc(rdev, pi->soft_regs_start + reg_offset,
1321 (u8 *)&value, sizeof(u16), pi->sram_end);
1322}
1323
1324static int kv_read_smc_soft_register(struct radeon_device *rdev,
1325 u16 reg_offset, u32 *value)
1326{
1327 struct kv_power_info *pi = kv_get_pi(rdev);
1328
1329 return kv_read_smc_sram_dword(rdev, pi->soft_regs_start + reg_offset,
1330 value, pi->sram_end);
1331}
1332#endif
1333
1334static void kv_init_sclk_t(struct radeon_device *rdev)
1335{
1336 struct kv_power_info *pi = kv_get_pi(rdev);
1337
1338 pi->low_sclk_interrupt_t = 0;
1339}
1340
1341static int kv_init_fps_limits(struct radeon_device *rdev)
1342{
1343 struct kv_power_info *pi = kv_get_pi(rdev);
1344 int ret = 0;
1345
1346 if (pi->caps_fps) {
1347 u16 tmp;
1348
1349 tmp = 45;
1350 pi->fps_high_t = cpu_to_be16(tmp);
1351 ret = kv_copy_bytes_to_smc(rdev,
1352 pi->dpm_table_start +
1353 offsetof(SMU7_Fusion_DpmTable, FpsHighT),
1354 (u8 *)&pi->fps_high_t,
1355 sizeof(u16), pi->sram_end);
1356
1357 tmp = 30;
1358 pi->fps_low_t = cpu_to_be16(tmp);
1359
1360 ret = kv_copy_bytes_to_smc(rdev,
1361 pi->dpm_table_start +
1362 offsetof(SMU7_Fusion_DpmTable, FpsLowT),
1363 (u8 *)&pi->fps_low_t,
1364 sizeof(u16), pi->sram_end);
1365
1366 }
1367 return ret;
1368}
1369
1370static void kv_init_powergate_state(struct radeon_device *rdev)
1371{
1372 struct kv_power_info *pi = kv_get_pi(rdev);
1373
1374 pi->uvd_power_gated = false;
1375 pi->vce_power_gated = false;
1376 pi->samu_power_gated = false;
1377 pi->acp_power_gated = false;
1378
1379}
1380
1381static int kv_enable_uvd_dpm(struct radeon_device *rdev, bool enable)
1382{
1383 return kv_notify_message_to_smu(rdev, enable ?
1384 PPSMC_MSG_UVDDPM_Enable : PPSMC_MSG_UVDDPM_Disable);
1385}
1386
1387static int kv_enable_vce_dpm(struct radeon_device *rdev, bool enable)
1388{
1389 return kv_notify_message_to_smu(rdev, enable ?
1390 PPSMC_MSG_VCEDPM_Enable : PPSMC_MSG_VCEDPM_Disable);
1391}
1392
1393static int kv_enable_samu_dpm(struct radeon_device *rdev, bool enable)
1394{
1395 return kv_notify_message_to_smu(rdev, enable ?
1396 PPSMC_MSG_SAMUDPM_Enable : PPSMC_MSG_SAMUDPM_Disable);
1397}
1398
1399static int kv_enable_acp_dpm(struct radeon_device *rdev, bool enable)
1400{
1401 return kv_notify_message_to_smu(rdev, enable ?
1402 PPSMC_MSG_ACPDPM_Enable : PPSMC_MSG_ACPDPM_Disable);
1403}
1404
1405static int kv_update_uvd_dpm(struct radeon_device *rdev, bool gate)
1406{
1407 struct kv_power_info *pi = kv_get_pi(rdev);
1408 struct radeon_uvd_clock_voltage_dependency_table *table =
1409 &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
1410 int ret;
1411 u32 mask;
1412
1413 if (!gate) {
1414 if (table->count)
1415 pi->uvd_boot_level = table->count - 1;
1416 else
1417 pi->uvd_boot_level = 0;
1418
1419 if (!pi->caps_uvd_dpm || pi->caps_stable_p_state) {
1420 mask = 1 << pi->uvd_boot_level;
1421 } else {
1422 mask = 0x1f;
1423 }
1424
1425 ret = kv_copy_bytes_to_smc(rdev,
1426 pi->dpm_table_start +
1427 offsetof(SMU7_Fusion_DpmTable, UvdBootLevel),
1428 (uint8_t *)&pi->uvd_boot_level,
1429 sizeof(u8), pi->sram_end);
1430 if (ret)
1431 return ret;
1432
1433 kv_send_msg_to_smc_with_parameter(rdev,
1434 PPSMC_MSG_UVDDPM_SetEnabledMask,
1435 mask);
1436 }
1437
1438 return kv_enable_uvd_dpm(rdev, !gate);
1439}
1440
1441static u8 kv_get_vce_boot_level(struct radeon_device *rdev)
1442{
1443 u8 i;
1444 struct radeon_vce_clock_voltage_dependency_table *table =
1445 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1446
1447 for (i = 0; i < table->count; i++) {
1448 if (table->entries[i].evclk >= 0) /* XXX */
1449 break;
1450 }
1451
1452 return i;
1453}
1454
1455static int kv_update_vce_dpm(struct radeon_device *rdev,
1456 struct radeon_ps *radeon_new_state,
1457 struct radeon_ps *radeon_current_state)
1458{
1459 struct kv_power_info *pi = kv_get_pi(rdev);
1460 struct radeon_vce_clock_voltage_dependency_table *table =
1461 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1462 int ret;
1463
1464 if (radeon_new_state->evclk > 0 && radeon_current_state->evclk == 0) {
1465 kv_dpm_powergate_vce(rdev, false);
1466 /* turn the clocks on when encoding */
1467 cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, false);
1468 if (pi->caps_stable_p_state)
1469 pi->vce_boot_level = table->count - 1;
1470 else
1471 pi->vce_boot_level = kv_get_vce_boot_level(rdev);
1472
1473 ret = kv_copy_bytes_to_smc(rdev,
1474 pi->dpm_table_start +
1475 offsetof(SMU7_Fusion_DpmTable, VceBootLevel),
1476 (u8 *)&pi->vce_boot_level,
1477 sizeof(u8),
1478 pi->sram_end);
1479 if (ret)
1480 return ret;
1481
1482 if (pi->caps_stable_p_state)
1483 kv_send_msg_to_smc_with_parameter(rdev,
1484 PPSMC_MSG_VCEDPM_SetEnabledMask,
1485 (1 << pi->vce_boot_level));
1486
1487 kv_enable_vce_dpm(rdev, true);
1488 } else if (radeon_new_state->evclk == 0 && radeon_current_state->evclk > 0) {
1489 kv_enable_vce_dpm(rdev, false);
1490 /* turn the clocks off when not encoding */
1491 cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, true);
1492 kv_dpm_powergate_vce(rdev, true);
1493 }
1494
1495 return 0;
1496}
1497
1498static int kv_update_samu_dpm(struct radeon_device *rdev, bool gate)
1499{
1500 struct kv_power_info *pi = kv_get_pi(rdev);
1501 struct radeon_clock_voltage_dependency_table *table =
1502 &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
1503 int ret;
1504
1505 if (!gate) {
1506 if (pi->caps_stable_p_state)
1507 pi->samu_boot_level = table->count - 1;
1508 else
1509 pi->samu_boot_level = 0;
1510
1511 ret = kv_copy_bytes_to_smc(rdev,
1512 pi->dpm_table_start +
1513 offsetof(SMU7_Fusion_DpmTable, SamuBootLevel),
1514 (u8 *)&pi->samu_boot_level,
1515 sizeof(u8),
1516 pi->sram_end);
1517 if (ret)
1518 return ret;
1519
1520 if (pi->caps_stable_p_state)
1521 kv_send_msg_to_smc_with_parameter(rdev,
1522 PPSMC_MSG_SAMUDPM_SetEnabledMask,
1523 (1 << pi->samu_boot_level));
1524 }
1525
1526 return kv_enable_samu_dpm(rdev, !gate);
1527}
1528
1529static u8 kv_get_acp_boot_level(struct radeon_device *rdev)
1530{
1531 u8 i;
1532 struct radeon_clock_voltage_dependency_table *table =
1533 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1534
1535 for (i = 0; i < table->count; i++) {
1536 if (table->entries[i].clk >= 0) /* XXX */
1537 break;
1538 }
1539
1540 if (i >= table->count)
1541 i = table->count - 1;
1542
1543 return i;
1544}
1545
1546static void kv_update_acp_boot_level(struct radeon_device *rdev)
1547{
1548 struct kv_power_info *pi = kv_get_pi(rdev);
1549 u8 acp_boot_level;
1550
1551 if (!pi->caps_stable_p_state) {
1552 acp_boot_level = kv_get_acp_boot_level(rdev);
1553 if (acp_boot_level != pi->acp_boot_level) {
1554 pi->acp_boot_level = acp_boot_level;
1555 kv_send_msg_to_smc_with_parameter(rdev,
1556 PPSMC_MSG_ACPDPM_SetEnabledMask,
1557 (1 << pi->acp_boot_level));
1558 }
1559 }
1560}
1561
1562static int kv_update_acp_dpm(struct radeon_device *rdev, bool gate)
1563{
1564 struct kv_power_info *pi = kv_get_pi(rdev);
1565 struct radeon_clock_voltage_dependency_table *table =
1566 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1567 int ret;
1568
1569 if (!gate) {
1570 if (pi->caps_stable_p_state)
1571 pi->acp_boot_level = table->count - 1;
1572 else
1573 pi->acp_boot_level = kv_get_acp_boot_level(rdev);
1574
1575 ret = kv_copy_bytes_to_smc(rdev,
1576 pi->dpm_table_start +
1577 offsetof(SMU7_Fusion_DpmTable, AcpBootLevel),
1578 (u8 *)&pi->acp_boot_level,
1579 sizeof(u8),
1580 pi->sram_end);
1581 if (ret)
1582 return ret;
1583
1584 if (pi->caps_stable_p_state)
1585 kv_send_msg_to_smc_with_parameter(rdev,
1586 PPSMC_MSG_ACPDPM_SetEnabledMask,
1587 (1 << pi->acp_boot_level));
1588 }
1589
1590 return kv_enable_acp_dpm(rdev, !gate);
1591}
1592
1593void kv_dpm_powergate_uvd(struct radeon_device *rdev, bool gate)
1594{
1595 struct kv_power_info *pi = kv_get_pi(rdev);
1596
1597 if (pi->uvd_power_gated == gate)
1598 return;
1599
1600 pi->uvd_power_gated = gate;
1601
1602 if (gate) {
1603 if (pi->caps_uvd_pg) {
1604 uvd_v1_0_stop(rdev);
1605 cik_update_cg(rdev, RADEON_CG_BLOCK_UVD, false);
1606 }
1607 kv_update_uvd_dpm(rdev, gate);
1608 if (pi->caps_uvd_pg)
1609 kv_notify_message_to_smu(rdev, PPSMC_MSG_UVDPowerOFF);
1610 } else {
1611 if (pi->caps_uvd_pg) {
1612 kv_notify_message_to_smu(rdev, PPSMC_MSG_UVDPowerON);
1613 uvd_v4_2_resume(rdev);
1614 uvd_v1_0_start(rdev);
1615 cik_update_cg(rdev, RADEON_CG_BLOCK_UVD, true);
1616 }
1617 kv_update_uvd_dpm(rdev, gate);
1618 }
1619}
1620
1621static void kv_dpm_powergate_vce(struct radeon_device *rdev, bool gate)
1622{
1623 struct kv_power_info *pi = kv_get_pi(rdev);
1624
1625 if (pi->vce_power_gated == gate)
1626 return;
1627
1628 pi->vce_power_gated = gate;
1629
1630 if (gate) {
1631 if (pi->caps_vce_pg) {
1632 /* XXX do we need a vce_v1_0_stop() ? */
1633 kv_notify_message_to_smu(rdev, PPSMC_MSG_VCEPowerOFF);
1634 }
1635 } else {
1636 if (pi->caps_vce_pg) {
1637 kv_notify_message_to_smu(rdev, PPSMC_MSG_VCEPowerON);
1638 vce_v2_0_resume(rdev);
1639 vce_v1_0_start(rdev);
1640 }
1641 }
1642}
1643
1644static void kv_dpm_powergate_samu(struct radeon_device *rdev, bool gate)
1645{
1646 struct kv_power_info *pi = kv_get_pi(rdev);
1647
1648 if (pi->samu_power_gated == gate)
1649 return;
1650
1651 pi->samu_power_gated = gate;
1652
1653 if (gate) {
1654 kv_update_samu_dpm(rdev, true);
1655 if (pi->caps_samu_pg)
1656 kv_notify_message_to_smu(rdev, PPSMC_MSG_SAMPowerOFF);
1657 } else {
1658 if (pi->caps_samu_pg)
1659 kv_notify_message_to_smu(rdev, PPSMC_MSG_SAMPowerON);
1660 kv_update_samu_dpm(rdev, false);
1661 }
1662}
1663
1664static void kv_dpm_powergate_acp(struct radeon_device *rdev, bool gate)
1665{
1666 struct kv_power_info *pi = kv_get_pi(rdev);
1667
1668 if (pi->acp_power_gated == gate)
1669 return;
1670
1671 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
1672 return;
1673
1674 pi->acp_power_gated = gate;
1675
1676 if (gate) {
1677 kv_update_acp_dpm(rdev, true);
1678 if (pi->caps_acp_pg)
1679 kv_notify_message_to_smu(rdev, PPSMC_MSG_ACPPowerOFF);
1680 } else {
1681 if (pi->caps_acp_pg)
1682 kv_notify_message_to_smu(rdev, PPSMC_MSG_ACPPowerON);
1683 kv_update_acp_dpm(rdev, false);
1684 }
1685}
1686
1687static void kv_set_valid_clock_range(struct radeon_device *rdev,
1688 struct radeon_ps *new_rps)
1689{
1690 struct kv_ps *new_ps = kv_get_ps(new_rps);
1691 struct kv_power_info *pi = kv_get_pi(rdev);
1692 u32 i;
1693 struct radeon_clock_voltage_dependency_table *table =
1694 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
1695
1696 if (table && table->count) {
1697 for (i = 0; i < pi->graphics_dpm_level_count; i++) {
1698 if ((table->entries[i].clk >= new_ps->levels[0].sclk) ||
1699 (i == (pi->graphics_dpm_level_count - 1))) {
1700 pi->lowest_valid = i;
1701 break;
1702 }
1703 }
1704
1705 for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
1706 if (table->entries[i].clk <= new_ps->levels[new_ps->num_levels - 1].sclk)
1707 break;
1708 }
1709 pi->highest_valid = i;
1710
1711 if (pi->lowest_valid > pi->highest_valid) {
1712 if ((new_ps->levels[0].sclk - table->entries[pi->highest_valid].clk) >
1713 (table->entries[pi->lowest_valid].clk - new_ps->levels[new_ps->num_levels - 1].sclk))
1714 pi->highest_valid = pi->lowest_valid;
1715 else
1716 pi->lowest_valid = pi->highest_valid;
1717 }
1718 } else {
1719 struct sumo_sclk_voltage_mapping_table *table =
1720 &pi->sys_info.sclk_voltage_mapping_table;
1721
1722 for (i = 0; i < (int)pi->graphics_dpm_level_count; i++) {
1723 if (table->entries[i].sclk_frequency >= new_ps->levels[0].sclk ||
1724 i == (int)(pi->graphics_dpm_level_count - 1)) {
1725 pi->lowest_valid = i;
1726 break;
1727 }
1728 }
1729
1730 for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
1731 if (table->entries[i].sclk_frequency <=
1732 new_ps->levels[new_ps->num_levels - 1].sclk)
1733 break;
1734 }
1735 pi->highest_valid = i;
1736
1737 if (pi->lowest_valid > pi->highest_valid) {
1738 if ((new_ps->levels[0].sclk -
1739 table->entries[pi->highest_valid].sclk_frequency) >
1740 (table->entries[pi->lowest_valid].sclk_frequency -
1741 new_ps->levels[new_ps->num_levels -1].sclk))
1742 pi->highest_valid = pi->lowest_valid;
1743 else
1744 pi->lowest_valid = pi->highest_valid;
1745 }
1746 }
1747}
1748
1749static int kv_update_dfs_bypass_settings(struct radeon_device *rdev,
1750 struct radeon_ps *new_rps)
1751{
1752 struct kv_ps *new_ps = kv_get_ps(new_rps);
1753 struct kv_power_info *pi = kv_get_pi(rdev);
1754 int ret = 0;
1755 u8 clk_bypass_cntl;
1756
1757 if (pi->caps_enable_dfs_bypass) {
1758 clk_bypass_cntl = new_ps->need_dfs_bypass ?
1759 pi->graphics_level[pi->graphics_boot_level].ClkBypassCntl : 0;
1760 ret = kv_copy_bytes_to_smc(rdev,
1761 (pi->dpm_table_start +
1762 offsetof(SMU7_Fusion_DpmTable, GraphicsLevel) +
1763 (pi->graphics_boot_level * sizeof(SMU7_Fusion_GraphicsLevel)) +
1764 offsetof(SMU7_Fusion_GraphicsLevel, ClkBypassCntl)),
1765 &clk_bypass_cntl,
1766 sizeof(u8), pi->sram_end);
1767 }
1768
1769 return ret;
1770}
1771
1772static int kv_enable_nb_dpm(struct radeon_device *rdev)
1773{
1774 struct kv_power_info *pi = kv_get_pi(rdev);
1775 int ret = 0;
1776
1777 if (pi->enable_nb_dpm && !pi->nb_dpm_enabled) {
1778 ret = kv_notify_message_to_smu(rdev, PPSMC_MSG_NBDPM_Enable);
1779 if (ret == 0)
1780 pi->nb_dpm_enabled = true;
1781 }
1782
1783 return ret;
1784}
1785
1786int kv_dpm_force_performance_level(struct radeon_device *rdev,
1787 enum radeon_dpm_forced_level level)
1788{
1789 int ret;
1790
1791 if (level == RADEON_DPM_FORCED_LEVEL_HIGH) {
1792 ret = kv_force_dpm_highest(rdev);
1793 if (ret)
1794 return ret;
1795 } else if (level == RADEON_DPM_FORCED_LEVEL_LOW) {
1796 ret = kv_force_dpm_lowest(rdev);
1797 if (ret)
1798 return ret;
1799 } else if (level == RADEON_DPM_FORCED_LEVEL_AUTO) {
1800 ret = kv_unforce_levels(rdev);
1801 if (ret)
1802 return ret;
1803 }
1804
1805 rdev->pm.dpm.forced_level = level;
1806
1807 return 0;
1808}
1809
1810int kv_dpm_pre_set_power_state(struct radeon_device *rdev)
1811{
1812 struct kv_power_info *pi = kv_get_pi(rdev);
1813 struct radeon_ps requested_ps = *rdev->pm.dpm.requested_ps;
1814 struct radeon_ps *new_ps = &requested_ps;
1815
1816 kv_update_requested_ps(rdev, new_ps);
1817
1818 kv_apply_state_adjust_rules(rdev,
1819 &pi->requested_rps,
1820 &pi->current_rps);
1821
1822 return 0;
1823}
1824
1825int kv_dpm_set_power_state(struct radeon_device *rdev)
1826{
1827 struct kv_power_info *pi = kv_get_pi(rdev);
1828 struct radeon_ps *new_ps = &pi->requested_rps;
1829 struct radeon_ps *old_ps = &pi->current_rps;
1830 int ret;
1831
1832 if (pi->bapm_enable) {
1833 ret = kv_smc_bapm_enable(rdev, rdev->pm.dpm.ac_power);
1834 if (ret) {
1835 DRM_ERROR("kv_smc_bapm_enable failed\n");
1836 return ret;
1837 }
1838 }
1839
1840 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS) {
1841 if (pi->enable_dpm) {
1842 kv_set_valid_clock_range(rdev, new_ps);
1843 kv_update_dfs_bypass_settings(rdev, new_ps);
1844 ret = kv_calculate_ds_divider(rdev);
1845 if (ret) {
1846 DRM_ERROR("kv_calculate_ds_divider failed\n");
1847 return ret;
1848 }
1849 kv_calculate_nbps_level_settings(rdev);
1850 kv_calculate_dpm_settings(rdev);
1851 kv_force_lowest_valid(rdev);
1852 kv_enable_new_levels(rdev);
1853 kv_upload_dpm_settings(rdev);
1854 kv_program_nbps_index_settings(rdev, new_ps);
1855 kv_unforce_levels(rdev);
1856 kv_set_enabled_levels(rdev);
1857 kv_force_lowest_valid(rdev);
1858 kv_unforce_levels(rdev);
1859
1860 ret = kv_update_vce_dpm(rdev, new_ps, old_ps);
1861 if (ret) {
1862 DRM_ERROR("kv_update_vce_dpm failed\n");
1863 return ret;
1864 }
1865 kv_update_sclk_t(rdev);
1866 if (rdev->family == CHIP_MULLINS)
1867 kv_enable_nb_dpm(rdev);
1868 }
1869 } else {
1870 if (pi->enable_dpm) {
1871 kv_set_valid_clock_range(rdev, new_ps);
1872 kv_update_dfs_bypass_settings(rdev, new_ps);
1873 ret = kv_calculate_ds_divider(rdev);
1874 if (ret) {
1875 DRM_ERROR("kv_calculate_ds_divider failed\n");
1876 return ret;
1877 }
1878 kv_calculate_nbps_level_settings(rdev);
1879 kv_calculate_dpm_settings(rdev);
1880 kv_freeze_sclk_dpm(rdev, true);
1881 kv_upload_dpm_settings(rdev);
1882 kv_program_nbps_index_settings(rdev, new_ps);
1883 kv_freeze_sclk_dpm(rdev, false);
1884 kv_set_enabled_levels(rdev);
1885 ret = kv_update_vce_dpm(rdev, new_ps, old_ps);
1886 if (ret) {
1887 DRM_ERROR("kv_update_vce_dpm failed\n");
1888 return ret;
1889 }
1890 kv_update_acp_boot_level(rdev);
1891 kv_update_sclk_t(rdev);
1892 kv_enable_nb_dpm(rdev);
1893 }
1894 }
1895
1896 return 0;
1897}
1898
1899void kv_dpm_post_set_power_state(struct radeon_device *rdev)
1900{
1901 struct kv_power_info *pi = kv_get_pi(rdev);
1902 struct radeon_ps *new_ps = &pi->requested_rps;
1903
1904 kv_update_current_ps(rdev, new_ps);
1905}
1906
1907void kv_dpm_setup_asic(struct radeon_device *rdev)
1908{
1909 sumo_take_smu_control(rdev, true);
1910 kv_init_powergate_state(rdev);
1911 kv_init_sclk_t(rdev);
1912}
1913
1914void kv_dpm_reset_asic(struct radeon_device *rdev)
1915{
1916 struct kv_power_info *pi = kv_get_pi(rdev);
1917
1918 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS) {
1919 kv_force_lowest_valid(rdev);
1920 kv_init_graphics_levels(rdev);
1921 kv_program_bootup_state(rdev);
1922 kv_upload_dpm_settings(rdev);
1923 kv_force_lowest_valid(rdev);
1924 kv_unforce_levels(rdev);
1925 } else {
1926 kv_init_graphics_levels(rdev);
1927 kv_program_bootup_state(rdev);
1928 kv_freeze_sclk_dpm(rdev, true);
1929 kv_upload_dpm_settings(rdev);
1930 kv_freeze_sclk_dpm(rdev, false);
1931 kv_set_enabled_level(rdev, pi->graphics_boot_level);
1932 }
1933}
1934
1935//XXX use sumo_dpm_display_configuration_changed
1936
1937static void kv_construct_max_power_limits_table(struct radeon_device *rdev,
1938 struct radeon_clock_and_voltage_limits *table)
1939{
1940 struct kv_power_info *pi = kv_get_pi(rdev);
1941
1942 if (pi->sys_info.sclk_voltage_mapping_table.num_max_dpm_entries > 0) {
1943 int idx = pi->sys_info.sclk_voltage_mapping_table.num_max_dpm_entries - 1;
1944 table->sclk =
1945 pi->sys_info.sclk_voltage_mapping_table.entries[idx].sclk_frequency;
1946 table->vddc =
1947 kv_convert_2bit_index_to_voltage(rdev,
1948 pi->sys_info.sclk_voltage_mapping_table.entries[idx].vid_2bit);
1949 }
1950
1951 table->mclk = pi->sys_info.nbp_memory_clock[0];
1952}
1953
1954static void kv_patch_voltage_values(struct radeon_device *rdev)
1955{
1956 int i;
1957 struct radeon_uvd_clock_voltage_dependency_table *uvd_table =
1958 &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
1959 struct radeon_vce_clock_voltage_dependency_table *vce_table =
1960 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
1961 struct radeon_clock_voltage_dependency_table *samu_table =
1962 &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
1963 struct radeon_clock_voltage_dependency_table *acp_table =
1964 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
1965
1966 if (uvd_table->count) {
1967 for (i = 0; i < uvd_table->count; i++)
1968 uvd_table->entries[i].v =
1969 kv_convert_8bit_index_to_voltage(rdev,
1970 uvd_table->entries[i].v);
1971 }
1972
1973 if (vce_table->count) {
1974 for (i = 0; i < vce_table->count; i++)
1975 vce_table->entries[i].v =
1976 kv_convert_8bit_index_to_voltage(rdev,
1977 vce_table->entries[i].v);
1978 }
1979
1980 if (samu_table->count) {
1981 for (i = 0; i < samu_table->count; i++)
1982 samu_table->entries[i].v =
1983 kv_convert_8bit_index_to_voltage(rdev,
1984 samu_table->entries[i].v);
1985 }
1986
1987 if (acp_table->count) {
1988 for (i = 0; i < acp_table->count; i++)
1989 acp_table->entries[i].v =
1990 kv_convert_8bit_index_to_voltage(rdev,
1991 acp_table->entries[i].v);
1992 }
1993
1994}
1995
1996static void kv_construct_boot_state(struct radeon_device *rdev)
1997{
1998 struct kv_power_info *pi = kv_get_pi(rdev);
1999
2000 pi->boot_pl.sclk = pi->sys_info.bootup_sclk;
2001 pi->boot_pl.vddc_index = pi->sys_info.bootup_nb_voltage_index;
2002 pi->boot_pl.ds_divider_index = 0;
2003 pi->boot_pl.ss_divider_index = 0;
2004 pi->boot_pl.allow_gnb_slow = 1;
2005 pi->boot_pl.force_nbp_state = 0;
2006 pi->boot_pl.display_wm = 0;
2007 pi->boot_pl.vce_wm = 0;
2008}
2009
2010static int kv_force_dpm_highest(struct radeon_device *rdev)
2011{
2012 int ret;
2013 u32 enable_mask, i;
2014
2015 ret = kv_dpm_get_enable_mask(rdev, &enable_mask);
2016 if (ret)
2017 return ret;
2018
2019 for (i = SMU7_MAX_LEVELS_GRAPHICS - 1; i > 0; i--) {
2020 if (enable_mask & (1 << i))
2021 break;
2022 }
2023
2024 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
2025 return kv_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_DPM_ForceState, i);
2026 else
2027 return kv_set_enabled_level(rdev, i);
2028}
2029
2030static int kv_force_dpm_lowest(struct radeon_device *rdev)
2031{
2032 int ret;
2033 u32 enable_mask, i;
2034
2035 ret = kv_dpm_get_enable_mask(rdev, &enable_mask);
2036 if (ret)
2037 return ret;
2038
2039 for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++) {
2040 if (enable_mask & (1 << i))
2041 break;
2042 }
2043
2044 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
2045 return kv_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_DPM_ForceState, i);
2046 else
2047 return kv_set_enabled_level(rdev, i);
2048}
2049
2050static u8 kv_get_sleep_divider_id_from_clock(struct radeon_device *rdev,
2051 u32 sclk, u32 min_sclk_in_sr)
2052{
2053 struct kv_power_info *pi = kv_get_pi(rdev);
2054 u32 i;
2055 u32 temp;
2056 u32 min = (min_sclk_in_sr > KV_MINIMUM_ENGINE_CLOCK) ?
2057 min_sclk_in_sr : KV_MINIMUM_ENGINE_CLOCK;
2058
2059 if (sclk < min)
2060 return 0;
2061
2062 if (!pi->caps_sclk_ds)
2063 return 0;
2064
2065 for (i = KV_MAX_DEEPSLEEP_DIVIDER_ID; i > 0; i--) {
2066 temp = sclk / sumo_get_sleep_divider_from_id(i);
2067 if (temp >= min)
2068 break;
2069 }
2070
2071 return (u8)i;
2072}
2073
2074static int kv_get_high_voltage_limit(struct radeon_device *rdev, int *limit)
2075{
2076 struct kv_power_info *pi = kv_get_pi(rdev);
2077 struct radeon_clock_voltage_dependency_table *table =
2078 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
2079 int i;
2080
2081 if (table && table->count) {
2082 for (i = table->count - 1; i >= 0; i--) {
2083 if (pi->high_voltage_t &&
2084 (kv_convert_8bit_index_to_voltage(rdev, table->entries[i].v) <=
2085 pi->high_voltage_t)) {
2086 *limit = i;
2087 return 0;
2088 }
2089 }
2090 } else {
2091 struct sumo_sclk_voltage_mapping_table *table =
2092 &pi->sys_info.sclk_voltage_mapping_table;
2093
2094 for (i = table->num_max_dpm_entries - 1; i >= 0; i--) {
2095 if (pi->high_voltage_t &&
2096 (kv_convert_2bit_index_to_voltage(rdev, table->entries[i].vid_2bit) <=
2097 pi->high_voltage_t)) {
2098 *limit = i;
2099 return 0;
2100 }
2101 }
2102 }
2103
2104 *limit = 0;
2105 return 0;
2106}
2107
2108static void kv_apply_state_adjust_rules(struct radeon_device *rdev,
2109 struct radeon_ps *new_rps,
2110 struct radeon_ps *old_rps)
2111{
2112 struct kv_ps *ps = kv_get_ps(new_rps);
2113 struct kv_power_info *pi = kv_get_pi(rdev);
2114 u32 min_sclk = 10000; /* ??? */
2115 u32 sclk, mclk = 0;
2116 int i, limit;
2117 bool force_high;
2118 struct radeon_clock_voltage_dependency_table *table =
2119 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
2120 u32 stable_p_state_sclk = 0;
2121 struct radeon_clock_and_voltage_limits *max_limits =
2122 &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
2123
2124 if (new_rps->vce_active) {
2125 new_rps->evclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].evclk;
2126 new_rps->ecclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].ecclk;
2127 } else {
2128 new_rps->evclk = 0;
2129 new_rps->ecclk = 0;
2130 }
2131
2132 mclk = max_limits->mclk;
2133 sclk = min_sclk;
2134
2135 if (pi->caps_stable_p_state) {
2136 stable_p_state_sclk = (max_limits->sclk * 75) / 100;
2137
2138 for (i = table->count - 1; i >= 0; i++) {
2139 if (stable_p_state_sclk >= table->entries[i].clk) {
2140 stable_p_state_sclk = table->entries[i].clk;
2141 break;
2142 }
2143 }
2144
2145 if (i > 0)
2146 stable_p_state_sclk = table->entries[0].clk;
2147
2148 sclk = stable_p_state_sclk;
2149 }
2150
2151 if (new_rps->vce_active) {
2152 if (sclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk)
2153 sclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk;
2154 }
2155
2156 ps->need_dfs_bypass = true;
2157
2158 for (i = 0; i < ps->num_levels; i++) {
2159 if (ps->levels[i].sclk < sclk)
2160 ps->levels[i].sclk = sclk;
2161 }
2162
2163 if (table && table->count) {
2164 for (i = 0; i < ps->num_levels; i++) {
2165 if (pi->high_voltage_t &&
2166 (pi->high_voltage_t <
2167 kv_convert_8bit_index_to_voltage(rdev, ps->levels[i].vddc_index))) {
2168 kv_get_high_voltage_limit(rdev, &limit);
2169 ps->levels[i].sclk = table->entries[limit].clk;
2170 }
2171 }
2172 } else {
2173 struct sumo_sclk_voltage_mapping_table *table =
2174 &pi->sys_info.sclk_voltage_mapping_table;
2175
2176 for (i = 0; i < ps->num_levels; i++) {
2177 if (pi->high_voltage_t &&
2178 (pi->high_voltage_t <
2179 kv_convert_8bit_index_to_voltage(rdev, ps->levels[i].vddc_index))) {
2180 kv_get_high_voltage_limit(rdev, &limit);
2181 ps->levels[i].sclk = table->entries[limit].sclk_frequency;
2182 }
2183 }
2184 }
2185
2186 if (pi->caps_stable_p_state) {
2187 for (i = 0; i < ps->num_levels; i++) {
2188 ps->levels[i].sclk = stable_p_state_sclk;
2189 }
2190 }
2191
2192 pi->video_start = new_rps->dclk || new_rps->vclk ||
2193 new_rps->evclk || new_rps->ecclk;
2194
2195 if ((new_rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
2196 ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
2197 pi->battery_state = true;
2198 else
2199 pi->battery_state = false;
2200
2201 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS) {
2202 ps->dpm0_pg_nb_ps_lo = 0x1;
2203 ps->dpm0_pg_nb_ps_hi = 0x0;
2204 ps->dpmx_nb_ps_lo = 0x1;
2205 ps->dpmx_nb_ps_hi = 0x0;
2206 } else {
2207 ps->dpm0_pg_nb_ps_lo = 0x3;
2208 ps->dpm0_pg_nb_ps_hi = 0x0;
2209 ps->dpmx_nb_ps_lo = 0x3;
2210 ps->dpmx_nb_ps_hi = 0x0;
2211
2212 if (pi->sys_info.nb_dpm_enable) {
2213 force_high = (mclk >= pi->sys_info.nbp_memory_clock[3]) ||
2214 pi->video_start || (rdev->pm.dpm.new_active_crtc_count >= 3) ||
2215 pi->disable_nb_ps3_in_battery;
2216 ps->dpm0_pg_nb_ps_lo = force_high ? 0x2 : 0x3;
2217 ps->dpm0_pg_nb_ps_hi = 0x2;
2218 ps->dpmx_nb_ps_lo = force_high ? 0x2 : 0x3;
2219 ps->dpmx_nb_ps_hi = 0x2;
2220 }
2221 }
2222}
2223
2224static void kv_dpm_power_level_enabled_for_throttle(struct radeon_device *rdev,
2225 u32 index, bool enable)
2226{
2227 struct kv_power_info *pi = kv_get_pi(rdev);
2228
2229 pi->graphics_level[index].EnabledForThrottle = enable ? 1 : 0;
2230}
2231
2232static int kv_calculate_ds_divider(struct radeon_device *rdev)
2233{
2234 struct kv_power_info *pi = kv_get_pi(rdev);
2235 u32 sclk_in_sr = 10000; /* ??? */
2236 u32 i;
2237
2238 if (pi->lowest_valid > pi->highest_valid)
2239 return -EINVAL;
2240
2241 for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2242 pi->graphics_level[i].DeepSleepDivId =
2243 kv_get_sleep_divider_id_from_clock(rdev,
2244 be32_to_cpu(pi->graphics_level[i].SclkFrequency),
2245 sclk_in_sr);
2246 }
2247 return 0;
2248}
2249
2250static int kv_calculate_nbps_level_settings(struct radeon_device *rdev)
2251{
2252 struct kv_power_info *pi = kv_get_pi(rdev);
2253 u32 i;
2254 bool force_high;
2255 struct radeon_clock_and_voltage_limits *max_limits =
2256 &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
2257 u32 mclk = max_limits->mclk;
2258
2259 if (pi->lowest_valid > pi->highest_valid)
2260 return -EINVAL;
2261
2262 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS) {
2263 for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2264 pi->graphics_level[i].GnbSlow = 1;
2265 pi->graphics_level[i].ForceNbPs1 = 0;
2266 pi->graphics_level[i].UpH = 0;
2267 }
2268
2269 if (!pi->sys_info.nb_dpm_enable)
2270 return 0;
2271
2272 force_high = ((mclk >= pi->sys_info.nbp_memory_clock[3]) ||
2273 (rdev->pm.dpm.new_active_crtc_count >= 3) || pi->video_start);
2274
2275 if (force_high) {
2276 for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2277 pi->graphics_level[i].GnbSlow = 0;
2278 } else {
2279 if (pi->battery_state)
2280 pi->graphics_level[0].ForceNbPs1 = 1;
2281
2282 pi->graphics_level[1].GnbSlow = 0;
2283 pi->graphics_level[2].GnbSlow = 0;
2284 pi->graphics_level[3].GnbSlow = 0;
2285 pi->graphics_level[4].GnbSlow = 0;
2286 }
2287 } else {
2288 for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
2289 pi->graphics_level[i].GnbSlow = 1;
2290 pi->graphics_level[i].ForceNbPs1 = 0;
2291 pi->graphics_level[i].UpH = 0;
2292 }
2293
2294 if (pi->sys_info.nb_dpm_enable && pi->battery_state) {
2295 pi->graphics_level[pi->lowest_valid].UpH = 0x28;
2296 pi->graphics_level[pi->lowest_valid].GnbSlow = 0;
2297 if (pi->lowest_valid != pi->highest_valid)
2298 pi->graphics_level[pi->lowest_valid].ForceNbPs1 = 1;
2299 }
2300 }
2301 return 0;
2302}
2303
2304static int kv_calculate_dpm_settings(struct radeon_device *rdev)
2305{
2306 struct kv_power_info *pi = kv_get_pi(rdev);
2307 u32 i;
2308
2309 if (pi->lowest_valid > pi->highest_valid)
2310 return -EINVAL;
2311
2312 for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2313 pi->graphics_level[i].DisplayWatermark = (i == pi->highest_valid) ? 1 : 0;
2314
2315 return 0;
2316}
2317
2318static void kv_init_graphics_levels(struct radeon_device *rdev)
2319{
2320 struct kv_power_info *pi = kv_get_pi(rdev);
2321 u32 i;
2322 struct radeon_clock_voltage_dependency_table *table =
2323 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
2324
2325 if (table && table->count) {
2326 u32 vid_2bit;
2327
2328 pi->graphics_dpm_level_count = 0;
2329 for (i = 0; i < table->count; i++) {
2330 if (pi->high_voltage_t &&
2331 (pi->high_voltage_t <
2332 kv_convert_8bit_index_to_voltage(rdev, table->entries[i].v)))
2333 break;
2334
2335 kv_set_divider_value(rdev, i, table->entries[i].clk);
2336 vid_2bit = kv_convert_vid7_to_vid2(rdev,
2337 &pi->sys_info.vid_mapping_table,
2338 table->entries[i].v);
2339 kv_set_vid(rdev, i, vid_2bit);
2340 kv_set_at(rdev, i, pi->at[i]);
2341 kv_dpm_power_level_enabled_for_throttle(rdev, i, true);
2342 pi->graphics_dpm_level_count++;
2343 }
2344 } else {
2345 struct sumo_sclk_voltage_mapping_table *table =
2346 &pi->sys_info.sclk_voltage_mapping_table;
2347
2348 pi->graphics_dpm_level_count = 0;
2349 for (i = 0; i < table->num_max_dpm_entries; i++) {
2350 if (pi->high_voltage_t &&
2351 pi->high_voltage_t <
2352 kv_convert_2bit_index_to_voltage(rdev, table->entries[i].vid_2bit))
2353 break;
2354
2355 kv_set_divider_value(rdev, i, table->entries[i].sclk_frequency);
2356 kv_set_vid(rdev, i, table->entries[i].vid_2bit);
2357 kv_set_at(rdev, i, pi->at[i]);
2358 kv_dpm_power_level_enabled_for_throttle(rdev, i, true);
2359 pi->graphics_dpm_level_count++;
2360 }
2361 }
2362
2363 for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++)
2364 kv_dpm_power_level_enable(rdev, i, false);
2365}
2366
2367static void kv_enable_new_levels(struct radeon_device *rdev)
2368{
2369 struct kv_power_info *pi = kv_get_pi(rdev);
2370 u32 i;
2371
2372 for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++) {
2373 if (i >= pi->lowest_valid && i <= pi->highest_valid)
2374 kv_dpm_power_level_enable(rdev, i, true);
2375 }
2376}
2377
2378static int kv_set_enabled_level(struct radeon_device *rdev, u32 level)
2379{
2380 u32 new_mask = (1 << level);
2381
2382 return kv_send_msg_to_smc_with_parameter(rdev,
2383 PPSMC_MSG_SCLKDPM_SetEnabledMask,
2384 new_mask);
2385}
2386
2387static int kv_set_enabled_levels(struct radeon_device *rdev)
2388{
2389 struct kv_power_info *pi = kv_get_pi(rdev);
2390 u32 i, new_mask = 0;
2391
2392 for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
2393 new_mask |= (1 << i);
2394
2395 return kv_send_msg_to_smc_with_parameter(rdev,
2396 PPSMC_MSG_SCLKDPM_SetEnabledMask,
2397 new_mask);
2398}
2399
2400static void kv_program_nbps_index_settings(struct radeon_device *rdev,
2401 struct radeon_ps *new_rps)
2402{
2403 struct kv_ps *new_ps = kv_get_ps(new_rps);
2404 struct kv_power_info *pi = kv_get_pi(rdev);
2405 u32 nbdpmconfig1;
2406
2407 if (rdev->family == CHIP_KABINI || rdev->family == CHIP_MULLINS)
2408 return;
2409
2410 if (pi->sys_info.nb_dpm_enable) {
2411 nbdpmconfig1 = RREG32_SMC(NB_DPM_CONFIG_1);
2412 nbdpmconfig1 &= ~(Dpm0PgNbPsLo_MASK | Dpm0PgNbPsHi_MASK |
2413 DpmXNbPsLo_MASK | DpmXNbPsHi_MASK);
2414 nbdpmconfig1 |= (Dpm0PgNbPsLo(new_ps->dpm0_pg_nb_ps_lo) |
2415 Dpm0PgNbPsHi(new_ps->dpm0_pg_nb_ps_hi) |
2416 DpmXNbPsLo(new_ps->dpmx_nb_ps_lo) |
2417 DpmXNbPsHi(new_ps->dpmx_nb_ps_hi));
2418 WREG32_SMC(NB_DPM_CONFIG_1, nbdpmconfig1);
2419 }
2420}
2421
2422static int kv_set_thermal_temperature_range(struct radeon_device *rdev,
2423 int min_temp, int max_temp)
2424{
2425 int low_temp = 0 * 1000;
2426 int high_temp = 255 * 1000;
2427 u32 tmp;
2428
2429 if (low_temp < min_temp)
2430 low_temp = min_temp;
2431 if (high_temp > max_temp)
2432 high_temp = max_temp;
2433 if (high_temp < low_temp) {
2434 DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
2435 return -EINVAL;
2436 }
2437
2438 tmp = RREG32_SMC(CG_THERMAL_INT_CTRL);
2439 tmp &= ~(DIG_THERM_INTH_MASK | DIG_THERM_INTL_MASK);
2440 tmp |= (DIG_THERM_INTH(49 + (high_temp / 1000)) |
2441 DIG_THERM_INTL(49 + (low_temp / 1000)));
2442 WREG32_SMC(CG_THERMAL_INT_CTRL, tmp);
2443
2444 rdev->pm.dpm.thermal.min_temp = low_temp;
2445 rdev->pm.dpm.thermal.max_temp = high_temp;
2446
2447 return 0;
2448}
2449
2450union igp_info {
2451 struct _ATOM_INTEGRATED_SYSTEM_INFO info;
2452 struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
2453 struct _ATOM_INTEGRATED_SYSTEM_INFO_V5 info_5;
2454 struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
2455 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
2456 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
2457};
2458
2459static int kv_parse_sys_info_table(struct radeon_device *rdev)
2460{
2461 struct kv_power_info *pi = kv_get_pi(rdev);
2462 struct radeon_mode_info *mode_info = &rdev->mode_info;
2463 int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
2464 union igp_info *igp_info;
2465 u8 frev, crev;
2466 u16 data_offset;
2467 int i;
2468
2469 if (atom_parse_data_header(mode_info->atom_context, index, NULL,
2470 &frev, &crev, &data_offset)) {
2471 igp_info = (union igp_info *)(mode_info->atom_context->bios +
2472 data_offset);
2473
2474 if (crev != 8) {
2475 DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
2476 return -EINVAL;
2477 }
2478 pi->sys_info.bootup_sclk = le32_to_cpu(igp_info->info_8.ulBootUpEngineClock);
2479 pi->sys_info.bootup_uma_clk = le32_to_cpu(igp_info->info_8.ulBootUpUMAClock);
2480 pi->sys_info.bootup_nb_voltage_index =
2481 le16_to_cpu(igp_info->info_8.usBootUpNBVoltage);
2482 if (igp_info->info_8.ucHtcTmpLmt == 0)
2483 pi->sys_info.htc_tmp_lmt = 203;
2484 else
2485 pi->sys_info.htc_tmp_lmt = igp_info->info_8.ucHtcTmpLmt;
2486 if (igp_info->info_8.ucHtcHystLmt == 0)
2487 pi->sys_info.htc_hyst_lmt = 5;
2488 else
2489 pi->sys_info.htc_hyst_lmt = igp_info->info_8.ucHtcHystLmt;
2490 if (pi->sys_info.htc_tmp_lmt <= pi->sys_info.htc_hyst_lmt) {
2491 DRM_ERROR("The htcTmpLmt should be larger than htcHystLmt.\n");
2492 }
2493
2494 if (le32_to_cpu(igp_info->info_8.ulSystemConfig) & (1 << 3))
2495 pi->sys_info.nb_dpm_enable = true;
2496 else
2497 pi->sys_info.nb_dpm_enable = false;
2498
2499 for (i = 0; i < KV_NUM_NBPSTATES; i++) {
2500 pi->sys_info.nbp_memory_clock[i] =
2501 le32_to_cpu(igp_info->info_8.ulNbpStateMemclkFreq[i]);
2502 pi->sys_info.nbp_n_clock[i] =
2503 le32_to_cpu(igp_info->info_8.ulNbpStateNClkFreq[i]);
2504 }
2505 if (le32_to_cpu(igp_info->info_8.ulGPUCapInfo) &
2506 SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS)
2507 pi->caps_enable_dfs_bypass = true;
2508
2509 sumo_construct_sclk_voltage_mapping_table(rdev,
2510 &pi->sys_info.sclk_voltage_mapping_table,
2511 igp_info->info_8.sAvail_SCLK);
2512
2513 sumo_construct_vid_mapping_table(rdev,
2514 &pi->sys_info.vid_mapping_table,
2515 igp_info->info_8.sAvail_SCLK);
2516
2517 kv_construct_max_power_limits_table(rdev,
2518 &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
2519 }
2520 return 0;
2521}
2522
2523union power_info {
2524 struct _ATOM_POWERPLAY_INFO info;
2525 struct _ATOM_POWERPLAY_INFO_V2 info_2;
2526 struct _ATOM_POWERPLAY_INFO_V3 info_3;
2527 struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
2528 struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
2529 struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
2530};
2531
2532union pplib_clock_info {
2533 struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
2534 struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
2535 struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
2536 struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
2537};
2538
2539union pplib_power_state {
2540 struct _ATOM_PPLIB_STATE v1;
2541 struct _ATOM_PPLIB_STATE_V2 v2;
2542};
2543
2544static void kv_patch_boot_state(struct radeon_device *rdev,
2545 struct kv_ps *ps)
2546{
2547 struct kv_power_info *pi = kv_get_pi(rdev);
2548
2549 ps->num_levels = 1;
2550 ps->levels[0] = pi->boot_pl;
2551}
2552
2553static void kv_parse_pplib_non_clock_info(struct radeon_device *rdev,
2554 struct radeon_ps *rps,
2555 struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
2556 u8 table_rev)
2557{
2558 struct kv_ps *ps = kv_get_ps(rps);
2559
2560 rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
2561 rps->class = le16_to_cpu(non_clock_info->usClassification);
2562 rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
2563
2564 if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
2565 rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
2566 rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
2567 } else {
2568 rps->vclk = 0;
2569 rps->dclk = 0;
2570 }
2571
2572 if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
2573 rdev->pm.dpm.boot_ps = rps;
2574 kv_patch_boot_state(rdev, ps);
2575 }
2576 if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
2577 rdev->pm.dpm.uvd_ps = rps;
2578}
2579
2580static void kv_parse_pplib_clock_info(struct radeon_device *rdev,
2581 struct radeon_ps *rps, int index,
2582 union pplib_clock_info *clock_info)
2583{
2584 struct kv_power_info *pi = kv_get_pi(rdev);
2585 struct kv_ps *ps = kv_get_ps(rps);
2586 struct kv_pl *pl = &ps->levels[index];
2587 u32 sclk;
2588
2589 sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
2590 sclk |= clock_info->sumo.ucEngineClockHigh << 16;
2591 pl->sclk = sclk;
2592 pl->vddc_index = clock_info->sumo.vddcIndex;
2593
2594 ps->num_levels = index + 1;
2595
2596 if (pi->caps_sclk_ds) {
2597 pl->ds_divider_index = 5;
2598 pl->ss_divider_index = 5;
2599 }
2600}
2601
2602static int kv_parse_power_table(struct radeon_device *rdev)
2603{
2604 struct radeon_mode_info *mode_info = &rdev->mode_info;
2605 struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
2606 union pplib_power_state *power_state;
2607 int i, j, k, non_clock_array_index, clock_array_index;
2608 union pplib_clock_info *clock_info;
2609 struct _StateArray *state_array;
2610 struct _ClockInfoArray *clock_info_array;
2611 struct _NonClockInfoArray *non_clock_info_array;
2612 union power_info *power_info;
2613 int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2614 u16 data_offset;
2615 u8 frev, crev;
2616 u8 *power_state_offset;
2617 struct kv_ps *ps;
2618
2619 if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
2620 &frev, &crev, &data_offset))
2621 return -EINVAL;
2622 power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
2623
2624 state_array = (struct _StateArray *)
2625 (mode_info->atom_context->bios + data_offset +
2626 le16_to_cpu(power_info->pplib.usStateArrayOffset));
2627 clock_info_array = (struct _ClockInfoArray *)
2628 (mode_info->atom_context->bios + data_offset +
2629 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
2630 non_clock_info_array = (struct _NonClockInfoArray *)
2631 (mode_info->atom_context->bios + data_offset +
2632 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
2633
2634 rdev->pm.dpm.ps = kzalloc(sizeof(struct radeon_ps) *
2635 state_array->ucNumEntries, GFP_KERNEL);
2636 if (!rdev->pm.dpm.ps)
2637 return -ENOMEM;
2638 power_state_offset = (u8 *)state_array->states;
2639 for (i = 0; i < state_array->ucNumEntries; i++) {
2640 u8 *idx;
2641 power_state = (union pplib_power_state *)power_state_offset;
2642 non_clock_array_index = power_state->v2.nonClockInfoIndex;
2643 non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
2644 &non_clock_info_array->nonClockInfo[non_clock_array_index];
2645 if (!rdev->pm.power_state[i].clock_info)
2646 return -EINVAL;
2647 ps = kzalloc(sizeof(struct kv_ps), GFP_KERNEL);
2648 if (ps == NULL) {
2649 kfree(rdev->pm.dpm.ps);
2650 return -ENOMEM;
2651 }
2652 rdev->pm.dpm.ps[i].ps_priv = ps;
2653 k = 0;
2654 idx = (u8 *)&power_state->v2.clockInfoIndex[0];
2655 for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
2656 clock_array_index = idx[j];
2657 if (clock_array_index >= clock_info_array->ucNumEntries)
2658 continue;
2659 if (k >= SUMO_MAX_HARDWARE_POWERLEVELS)
2660 break;
2661 clock_info = (union pplib_clock_info *)
2662 ((u8 *)&clock_info_array->clockInfo[0] +
2663 (clock_array_index * clock_info_array->ucEntrySize));
2664 kv_parse_pplib_clock_info(rdev,
2665 &rdev->pm.dpm.ps[i], k,
2666 clock_info);
2667 k++;
2668 }
2669 kv_parse_pplib_non_clock_info(rdev, &rdev->pm.dpm.ps[i],
2670 non_clock_info,
2671 non_clock_info_array->ucEntrySize);
2672 power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
2673 }
2674 rdev->pm.dpm.num_ps = state_array->ucNumEntries;
2675
2676 /* fill in the vce power states */
2677 for (i = 0; i < RADEON_MAX_VCE_LEVELS; i++) {
2678 u32 sclk;
2679 clock_array_index = rdev->pm.dpm.vce_states[i].clk_idx;
2680 clock_info = (union pplib_clock_info *)
2681 &clock_info_array->clockInfo[clock_array_index * clock_info_array->ucEntrySize];
2682 sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
2683 sclk |= clock_info->sumo.ucEngineClockHigh << 16;
2684 rdev->pm.dpm.vce_states[i].sclk = sclk;
2685 rdev->pm.dpm.vce_states[i].mclk = 0;
2686 }
2687
2688 return 0;
2689}
2690
2691int kv_dpm_init(struct radeon_device *rdev)
2692{
2693 struct kv_power_info *pi;
2694 int ret, i;
2695
2696 pi = kzalloc(sizeof(struct kv_power_info), GFP_KERNEL);
2697 if (pi == NULL)
2698 return -ENOMEM;
2699 rdev->pm.dpm.priv = pi;
2700
2701 ret = r600_get_platform_caps(rdev);
2702 if (ret)
2703 return ret;
2704
2705 ret = r600_parse_extended_power_table(rdev);
2706 if (ret)
2707 return ret;
2708
2709 for (i = 0; i < SUMO_MAX_HARDWARE_POWERLEVELS; i++)
2710 pi->at[i] = TRINITY_AT_DFLT;
2711
2712 pi->sram_end = SMC_RAM_END;
2713
2714 pi->enable_nb_dpm = true;
2715
2716 pi->caps_power_containment = true;
2717 pi->caps_cac = true;
2718 pi->enable_didt = false;
2719 if (pi->enable_didt) {
2720 pi->caps_sq_ramping = true;
2721 pi->caps_db_ramping = true;
2722 pi->caps_td_ramping = true;
2723 pi->caps_tcp_ramping = true;
2724 }
2725
2726 pi->caps_sclk_ds = true;
2727 pi->enable_auto_thermal_throttling = true;
2728 pi->disable_nb_ps3_in_battery = false;
2729 pi->bapm_enable = false;
2730 pi->voltage_drop_t = 0;
2731 pi->caps_sclk_throttle_low_notification = false;
2732 pi->caps_fps = false; /* true? */
2733 pi->caps_uvd_pg = true;
2734 pi->caps_uvd_dpm = true;
2735 pi->caps_vce_pg = false; /* XXX true */
2736 pi->caps_samu_pg = false;
2737 pi->caps_acp_pg = false;
2738 pi->caps_stable_p_state = false;
2739
2740 ret = kv_parse_sys_info_table(rdev);
2741 if (ret)
2742 return ret;
2743
2744 kv_patch_voltage_values(rdev);
2745 kv_construct_boot_state(rdev);
2746
2747 ret = kv_parse_power_table(rdev);
2748 if (ret)
2749 return ret;
2750
2751 pi->enable_dpm = true;
2752
2753 return 0;
2754}
2755
2756void kv_dpm_debugfs_print_current_performance_level(struct radeon_device *rdev,
2757 struct seq_file *m)
2758{
2759 struct kv_power_info *pi = kv_get_pi(rdev);
2760 u32 current_index =
2761 (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) & CURR_SCLK_INDEX_MASK) >>
2762 CURR_SCLK_INDEX_SHIFT;
2763 u32 sclk, tmp;
2764 u16 vddc;
2765
2766 if (current_index >= SMU__NUM_SCLK_DPM_STATE) {
2767 seq_printf(m, "invalid dpm profile %d\n", current_index);
2768 } else {
2769 sclk = be32_to_cpu(pi->graphics_level[current_index].SclkFrequency);
2770 tmp = (RREG32_SMC(SMU_VOLTAGE_STATUS) & SMU_VOLTAGE_CURRENT_LEVEL_MASK) >>
2771 SMU_VOLTAGE_CURRENT_LEVEL_SHIFT;
2772 vddc = kv_convert_8bit_index_to_voltage(rdev, (u16)tmp);
2773 seq_printf(m, "power level %d sclk: %u vddc: %u\n",
2774 current_index, sclk, vddc);
2775 }
2776}
2777
2778void kv_dpm_print_power_state(struct radeon_device *rdev,
2779 struct radeon_ps *rps)
2780{
2781 int i;
2782 struct kv_ps *ps = kv_get_ps(rps);
2783
2784 r600_dpm_print_class_info(rps->class, rps->class2);
2785 r600_dpm_print_cap_info(rps->caps);
2786 printk("\tuvd vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
2787 for (i = 0; i < ps->num_levels; i++) {
2788 struct kv_pl *pl = &ps->levels[i];
2789 printk("\t\tpower level %d sclk: %u vddc: %u\n",
2790 i, pl->sclk,
2791 kv_convert_8bit_index_to_voltage(rdev, pl->vddc_index));
2792 }
2793 r600_dpm_print_ps_status(rdev, rps);
2794}
2795
2796void kv_dpm_fini(struct radeon_device *rdev)
2797{
2798 int i;
2799
2800 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
2801 kfree(rdev->pm.dpm.ps[i].ps_priv);
2802 }
2803 kfree(rdev->pm.dpm.ps);
2804 kfree(rdev->pm.dpm.priv);
2805 r600_free_extended_power_table(rdev);
2806}
2807
2808void kv_dpm_display_configuration_changed(struct radeon_device *rdev)
2809{
2810
2811}
2812
2813u32 kv_dpm_get_sclk(struct radeon_device *rdev, bool low)
2814{
2815 struct kv_power_info *pi = kv_get_pi(rdev);
2816 struct kv_ps *requested_state = kv_get_ps(&pi->requested_rps);
2817
2818 if (low)
2819 return requested_state->levels[0].sclk;
2820 else
2821 return requested_state->levels[requested_state->num_levels - 1].sclk;
2822}
2823
2824u32 kv_dpm_get_mclk(struct radeon_device *rdev, bool low)
2825{
2826 struct kv_power_info *pi = kv_get_pi(rdev);
2827
2828 return pi->sys_info.bootup_uma_clk;
2829}
2830