1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/firmware.h>
25#include <linux/pci.h>
26#include <linux/seq_file.h>
27
28#include "atom.h"
29#include "ci_dpm.h"
30#include "cik.h"
31#include "cikd.h"
32#include "r600_dpm.h"
33#include "radeon.h"
34#include "radeon_asic.h"
35#include "radeon_ucode.h"
36#include "si_dpm.h"
37
38#define MC_CG_ARB_FREQ_F0 0x0a
39#define MC_CG_ARB_FREQ_F1 0x0b
40#define MC_CG_ARB_FREQ_F2 0x0c
41#define MC_CG_ARB_FREQ_F3 0x0d
42
43#define SMC_RAM_END 0x40000
44
45#define VOLTAGE_SCALE 4
46#define VOLTAGE_VID_OFFSET_SCALE1 625
47#define VOLTAGE_VID_OFFSET_SCALE2 100
48
49static const struct ci_pt_defaults defaults_hawaii_xt =
50{
51 1, 0xF, 0xFD, 0x19, 5, 0x14, 0, 0xB0000,
52 { 0x2E, 0x00, 0x00, 0x88, 0x00, 0x00, 0x72, 0x60, 0x51, 0xA7, 0x79, 0x6B, 0x90, 0xBD, 0x79 },
53 { 0x217, 0x217, 0x217, 0x242, 0x242, 0x242, 0x269, 0x269, 0x269, 0x2A1, 0x2A1, 0x2A1, 0x2C9, 0x2C9, 0x2C9 }
54};
55
56static const struct ci_pt_defaults defaults_hawaii_pro =
57{
58 1, 0xF, 0xFD, 0x19, 5, 0x14, 0, 0x65062,
59 { 0x2E, 0x00, 0x00, 0x88, 0x00, 0x00, 0x72, 0x60, 0x51, 0xA7, 0x79, 0x6B, 0x90, 0xBD, 0x79 },
60 { 0x217, 0x217, 0x217, 0x242, 0x242, 0x242, 0x269, 0x269, 0x269, 0x2A1, 0x2A1, 0x2A1, 0x2C9, 0x2C9, 0x2C9 }
61};
62
63static const struct ci_pt_defaults defaults_bonaire_xt =
64{
65 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0xB0000,
66 { 0x79, 0x253, 0x25D, 0xAE, 0x72, 0x80, 0x83, 0x86, 0x6F, 0xC8, 0xC9, 0xC9, 0x2F, 0x4D, 0x61 },
67 { 0x17C, 0x172, 0x180, 0x1BC, 0x1B3, 0x1BD, 0x206, 0x200, 0x203, 0x25D, 0x25A, 0x255, 0x2C3, 0x2C5, 0x2B4 }
68};
69
70static const struct ci_pt_defaults defaults_saturn_xt =
71{
72 1, 0xF, 0xFD, 0x19, 5, 55, 0, 0x70000,
73 { 0x8C, 0x247, 0x249, 0xA6, 0x80, 0x81, 0x8B, 0x89, 0x86, 0xC9, 0xCA, 0xC9, 0x4D, 0x4D, 0x4D },
74 { 0x187, 0x187, 0x187, 0x1C7, 0x1C7, 0x1C7, 0x210, 0x210, 0x210, 0x266, 0x266, 0x266, 0x2C9, 0x2C9, 0x2C9 }
75};
76
77static const struct ci_pt_config_reg didt_config_ci[] =
78{
79 { 0x10, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
80 { 0x10, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
81 { 0x10, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
82 { 0x10, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
83 { 0x11, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
84 { 0x11, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
85 { 0x11, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
86 { 0x11, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
87 { 0x12, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
88 { 0x12, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
89 { 0x12, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
90 { 0x12, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
91 { 0x2, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
92 { 0x2, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
93 { 0x2, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
94 { 0x1, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
95 { 0x1, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
96 { 0x0, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
97 { 0x30, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
98 { 0x30, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
99 { 0x30, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
100 { 0x30, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
101 { 0x31, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
102 { 0x31, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
103 { 0x31, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
104 { 0x31, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
105 { 0x32, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
106 { 0x32, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
107 { 0x32, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
108 { 0x32, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
109 { 0x22, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
110 { 0x22, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
111 { 0x22, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
112 { 0x21, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
113 { 0x21, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
114 { 0x20, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
115 { 0x50, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
116 { 0x50, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
117 { 0x50, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
118 { 0x50, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
119 { 0x51, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
120 { 0x51, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
121 { 0x51, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
122 { 0x51, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
123 { 0x52, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
124 { 0x52, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
125 { 0x52, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
126 { 0x52, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
127 { 0x42, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
128 { 0x42, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
129 { 0x42, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
130 { 0x41, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
131 { 0x41, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
132 { 0x40, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
133 { 0x70, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
134 { 0x70, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
135 { 0x70, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
136 { 0x70, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
137 { 0x71, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
138 { 0x71, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
139 { 0x71, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
140 { 0x71, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
141 { 0x72, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
142 { 0x72, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
143 { 0x72, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
144 { 0x72, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
145 { 0x62, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
146 { 0x62, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
147 { 0x62, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
148 { 0x61, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
149 { 0x61, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
150 { 0x60, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
151 { 0xFFFFFFFF }
152};
153
154extern u8 rv770_get_memory_module_index(struct radeon_device *rdev);
155extern int ni_copy_and_switch_arb_sets(struct radeon_device *rdev,
156 u32 arb_freq_src, u32 arb_freq_dest);
157static int ci_get_std_voltage_value_sidd(struct radeon_device *rdev,
158 struct atom_voltage_table_entry *voltage_table,
159 u16 *std_voltage_hi_sidd, u16 *std_voltage_lo_sidd);
160static int ci_set_power_limit(struct radeon_device *rdev, u32 n);
161static int ci_set_overdrive_target_tdp(struct radeon_device *rdev,
162 u32 target_tdp);
163static int ci_update_uvd_dpm(struct radeon_device *rdev, bool gate);
164
165static PPSMC_Result ci_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg);
166static PPSMC_Result ci_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
167 PPSMC_Msg msg, u32 parameter);
168
169static void ci_thermal_start_smc_fan_control(struct radeon_device *rdev);
170static void ci_fan_ctrl_set_default_mode(struct radeon_device *rdev);
171
172static struct ci_power_info *ci_get_pi(struct radeon_device *rdev)
173{
174 struct ci_power_info *pi = rdev->pm.dpm.priv;
175
176 return pi;
177}
178
179static struct ci_ps *ci_get_ps(struct radeon_ps *rps)
180{
181 struct ci_ps *ps = rps->ps_priv;
182
183 return ps;
184}
185
186static void ci_initialize_powertune_defaults(struct radeon_device *rdev)
187{
188 struct ci_power_info *pi = ci_get_pi(rdev);
189
190 switch (rdev->pdev->device) {
191 case 0x6649:
192 case 0x6650:
193 case 0x6651:
194 case 0x6658:
195 case 0x665C:
196 case 0x665D:
197 default:
198 pi->powertune_defaults = &defaults_bonaire_xt;
199 break;
200 case 0x6640:
201 case 0x6641:
202 case 0x6646:
203 case 0x6647:
204 pi->powertune_defaults = &defaults_saturn_xt;
205 break;
206 case 0x67B8:
207 case 0x67B0:
208 pi->powertune_defaults = &defaults_hawaii_xt;
209 break;
210 case 0x67BA:
211 case 0x67B1:
212 pi->powertune_defaults = &defaults_hawaii_pro;
213 break;
214 case 0x67A0:
215 case 0x67A1:
216 case 0x67A2:
217 case 0x67A8:
218 case 0x67A9:
219 case 0x67AA:
220 case 0x67B9:
221 case 0x67BE:
222 pi->powertune_defaults = &defaults_bonaire_xt;
223 break;
224 }
225
226 pi->dte_tj_offset = 0;
227
228 pi->caps_power_containment = true;
229 pi->caps_cac = false;
230 pi->caps_sq_ramping = false;
231 pi->caps_db_ramping = false;
232 pi->caps_td_ramping = false;
233 pi->caps_tcp_ramping = false;
234
235 if (pi->caps_power_containment) {
236 pi->caps_cac = true;
237 if (rdev->family == CHIP_HAWAII)
238 pi->enable_bapm_feature = false;
239 else
240 pi->enable_bapm_feature = true;
241 pi->enable_tdc_limit_feature = true;
242 pi->enable_pkg_pwr_tracking_feature = true;
243 }
244}
245
246static u8 ci_convert_to_vid(u16 vddc)
247{
248 return (6200 - (vddc * VOLTAGE_SCALE)) / 25;
249}
250
251static int ci_populate_bapm_vddc_vid_sidd(struct radeon_device *rdev)
252{
253 struct ci_power_info *pi = ci_get_pi(rdev);
254 u8 *hi_vid = pi->smc_powertune_table.BapmVddCVidHiSidd;
255 u8 *lo_vid = pi->smc_powertune_table.BapmVddCVidLoSidd;
256 u8 *hi2_vid = pi->smc_powertune_table.BapmVddCVidHiSidd2;
257 u32 i;
258
259 if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries == NULL)
260 return -EINVAL;
261 if (rdev->pm.dpm.dyn_state.cac_leakage_table.count > 8)
262 return -EINVAL;
263 if (rdev->pm.dpm.dyn_state.cac_leakage_table.count !=
264 rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count)
265 return -EINVAL;
266
267 for (i = 0; i < rdev->pm.dpm.dyn_state.cac_leakage_table.count; i++) {
268 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_EVV) {
269 lo_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc1);
270 hi_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc2);
271 hi2_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc3);
272 } else {
273 lo_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc);
274 hi_vid[i] = ci_convert_to_vid((u16)rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].leakage);
275 }
276 }
277 return 0;
278}
279
280static int ci_populate_vddc_vid(struct radeon_device *rdev)
281{
282 struct ci_power_info *pi = ci_get_pi(rdev);
283 u8 *vid = pi->smc_powertune_table.VddCVid;
284 u32 i;
285
286 if (pi->vddc_voltage_table.count > 8)
287 return -EINVAL;
288
289 for (i = 0; i < pi->vddc_voltage_table.count; i++)
290 vid[i] = ci_convert_to_vid(pi->vddc_voltage_table.entries[i].value);
291
292 return 0;
293}
294
295static int ci_populate_svi_load_line(struct radeon_device *rdev)
296{
297 struct ci_power_info *pi = ci_get_pi(rdev);
298 const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
299
300 pi->smc_powertune_table.SviLoadLineEn = pt_defaults->svi_load_line_en;
301 pi->smc_powertune_table.SviLoadLineVddC = pt_defaults->svi_load_line_vddc;
302 pi->smc_powertune_table.SviLoadLineTrimVddC = 3;
303 pi->smc_powertune_table.SviLoadLineOffsetVddC = 0;
304
305 return 0;
306}
307
308static int ci_populate_tdc_limit(struct radeon_device *rdev)
309{
310 struct ci_power_info *pi = ci_get_pi(rdev);
311 const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
312 u16 tdc_limit;
313
314 tdc_limit = rdev->pm.dpm.dyn_state.cac_tdp_table->tdc * 256;
315 pi->smc_powertune_table.TDC_VDDC_PkgLimit = cpu_to_be16(tdc_limit);
316 pi->smc_powertune_table.TDC_VDDC_ThrottleReleaseLimitPerc =
317 pt_defaults->tdc_vddc_throttle_release_limit_perc;
318 pi->smc_powertune_table.TDC_MAWt = pt_defaults->tdc_mawt;
319
320 return 0;
321}
322
323static int ci_populate_dw8(struct radeon_device *rdev)
324{
325 struct ci_power_info *pi = ci_get_pi(rdev);
326 const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
327 int ret;
328
329 ret = ci_read_smc_sram_dword(rdev,
330 SMU7_FIRMWARE_HEADER_LOCATION +
331 offsetof(SMU7_Firmware_Header, PmFuseTable) +
332 offsetof(SMU7_Discrete_PmFuses, TdcWaterfallCtl),
333 (u32 *)&pi->smc_powertune_table.TdcWaterfallCtl,
334 pi->sram_end);
335 if (ret)
336 return -EINVAL;
337 else
338 pi->smc_powertune_table.TdcWaterfallCtl = pt_defaults->tdc_waterfall_ctl;
339
340 return 0;
341}
342
343static int ci_populate_fuzzy_fan(struct radeon_device *rdev)
344{
345 struct ci_power_info *pi = ci_get_pi(rdev);
346
347 if ((rdev->pm.dpm.fan.fan_output_sensitivity & (1 << 15)) ||
348 (rdev->pm.dpm.fan.fan_output_sensitivity == 0))
349 rdev->pm.dpm.fan.fan_output_sensitivity =
350 rdev->pm.dpm.fan.default_fan_output_sensitivity;
351
352 pi->smc_powertune_table.FuzzyFan_PwmSetDelta =
353 cpu_to_be16(rdev->pm.dpm.fan.fan_output_sensitivity);
354
355 return 0;
356}
357
358static int ci_min_max_v_gnbl_pm_lid_from_bapm_vddc(struct radeon_device *rdev)
359{
360 struct ci_power_info *pi = ci_get_pi(rdev);
361 u8 *hi_vid = pi->smc_powertune_table.BapmVddCVidHiSidd;
362 u8 *lo_vid = pi->smc_powertune_table.BapmVddCVidLoSidd;
363 int i, min, max;
364
365 min = max = hi_vid[0];
366 for (i = 0; i < 8; i++) {
367 if (0 != hi_vid[i]) {
368 if (min > hi_vid[i])
369 min = hi_vid[i];
370 if (max < hi_vid[i])
371 max = hi_vid[i];
372 }
373
374 if (0 != lo_vid[i]) {
375 if (min > lo_vid[i])
376 min = lo_vid[i];
377 if (max < lo_vid[i])
378 max = lo_vid[i];
379 }
380 }
381
382 if ((min == 0) || (max == 0))
383 return -EINVAL;
384 pi->smc_powertune_table.GnbLPMLMaxVid = (u8)max;
385 pi->smc_powertune_table.GnbLPMLMinVid = (u8)min;
386
387 return 0;
388}
389
390static int ci_populate_bapm_vddc_base_leakage_sidd(struct radeon_device *rdev)
391{
392 struct ci_power_info *pi = ci_get_pi(rdev);
393 u16 hi_sidd = pi->smc_powertune_table.BapmVddCBaseLeakageHiSidd;
394 u16 lo_sidd = pi->smc_powertune_table.BapmVddCBaseLeakageLoSidd;
395 struct radeon_cac_tdp_table *cac_tdp_table =
396 rdev->pm.dpm.dyn_state.cac_tdp_table;
397
398 hi_sidd = cac_tdp_table->high_cac_leakage / 100 * 256;
399 lo_sidd = cac_tdp_table->low_cac_leakage / 100 * 256;
400
401 pi->smc_powertune_table.BapmVddCBaseLeakageHiSidd = cpu_to_be16(hi_sidd);
402 pi->smc_powertune_table.BapmVddCBaseLeakageLoSidd = cpu_to_be16(lo_sidd);
403
404 return 0;
405}
406
407static int ci_populate_bapm_parameters_in_dpm_table(struct radeon_device *rdev)
408{
409 struct ci_power_info *pi = ci_get_pi(rdev);
410 const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
411 SMU7_Discrete_DpmTable *dpm_table = &pi->smc_state_table;
412 struct radeon_cac_tdp_table *cac_tdp_table =
413 rdev->pm.dpm.dyn_state.cac_tdp_table;
414 struct radeon_ppm_table *ppm = rdev->pm.dpm.dyn_state.ppm_table;
415 int i, j, k;
416 const u16 *def1;
417 const u16 *def2;
418
419 dpm_table->DefaultTdp = cac_tdp_table->tdp * 256;
420 dpm_table->TargetTdp = cac_tdp_table->configurable_tdp * 256;
421
422 dpm_table->DTETjOffset = (u8)pi->dte_tj_offset;
423 dpm_table->GpuTjMax =
424 (u8)(pi->thermal_temp_setting.temperature_high / 1000);
425 dpm_table->GpuTjHyst = 8;
426
427 dpm_table->DTEAmbientTempBase = pt_defaults->dte_ambient_temp_base;
428
429 if (ppm) {
430 dpm_table->PPM_PkgPwrLimit = cpu_to_be16((u16)ppm->dgpu_tdp * 256 / 1000);
431 dpm_table->PPM_TemperatureLimit = cpu_to_be16((u16)ppm->tj_max * 256);
432 } else {
433 dpm_table->PPM_PkgPwrLimit = cpu_to_be16(0);
434 dpm_table->PPM_TemperatureLimit = cpu_to_be16(0);
435 }
436
437 dpm_table->BAPM_TEMP_GRADIENT = cpu_to_be32(pt_defaults->bapm_temp_gradient);
438 def1 = pt_defaults->bapmti_r;
439 def2 = pt_defaults->bapmti_rc;
440
441 for (i = 0; i < SMU7_DTE_ITERATIONS; i++) {
442 for (j = 0; j < SMU7_DTE_SOURCES; j++) {
443 for (k = 0; k < SMU7_DTE_SINKS; k++) {
444 dpm_table->BAPMTI_R[i][j][k] = cpu_to_be16(*def1);
445 dpm_table->BAPMTI_RC[i][j][k] = cpu_to_be16(*def2);
446 def1++;
447 def2++;
448 }
449 }
450 }
451
452 return 0;
453}
454
455static int ci_populate_pm_base(struct radeon_device *rdev)
456{
457 struct ci_power_info *pi = ci_get_pi(rdev);
458 u32 pm_fuse_table_offset;
459 int ret;
460
461 if (pi->caps_power_containment) {
462 ret = ci_read_smc_sram_dword(rdev,
463 SMU7_FIRMWARE_HEADER_LOCATION +
464 offsetof(SMU7_Firmware_Header, PmFuseTable),
465 &pm_fuse_table_offset, pi->sram_end);
466 if (ret)
467 return ret;
468 ret = ci_populate_bapm_vddc_vid_sidd(rdev);
469 if (ret)
470 return ret;
471 ret = ci_populate_vddc_vid(rdev);
472 if (ret)
473 return ret;
474 ret = ci_populate_svi_load_line(rdev);
475 if (ret)
476 return ret;
477 ret = ci_populate_tdc_limit(rdev);
478 if (ret)
479 return ret;
480 ret = ci_populate_dw8(rdev);
481 if (ret)
482 return ret;
483 ret = ci_populate_fuzzy_fan(rdev);
484 if (ret)
485 return ret;
486 ret = ci_min_max_v_gnbl_pm_lid_from_bapm_vddc(rdev);
487 if (ret)
488 return ret;
489 ret = ci_populate_bapm_vddc_base_leakage_sidd(rdev);
490 if (ret)
491 return ret;
492 ret = ci_copy_bytes_to_smc(rdev, pm_fuse_table_offset,
493 (u8 *)&pi->smc_powertune_table,
494 sizeof(SMU7_Discrete_PmFuses), pi->sram_end);
495 if (ret)
496 return ret;
497 }
498
499 return 0;
500}
501
502static void ci_do_enable_didt(struct radeon_device *rdev, const bool enable)
503{
504 struct ci_power_info *pi = ci_get_pi(rdev);
505 u32 data;
506
507 if (pi->caps_sq_ramping) {
508 data = RREG32_DIDT(DIDT_SQ_CTRL0);
509 if (enable)
510 data |= DIDT_CTRL_EN;
511 else
512 data &= ~DIDT_CTRL_EN;
513 WREG32_DIDT(DIDT_SQ_CTRL0, data);
514 }
515
516 if (pi->caps_db_ramping) {
517 data = RREG32_DIDT(DIDT_DB_CTRL0);
518 if (enable)
519 data |= DIDT_CTRL_EN;
520 else
521 data &= ~DIDT_CTRL_EN;
522 WREG32_DIDT(DIDT_DB_CTRL0, data);
523 }
524
525 if (pi->caps_td_ramping) {
526 data = RREG32_DIDT(DIDT_TD_CTRL0);
527 if (enable)
528 data |= DIDT_CTRL_EN;
529 else
530 data &= ~DIDT_CTRL_EN;
531 WREG32_DIDT(DIDT_TD_CTRL0, data);
532 }
533
534 if (pi->caps_tcp_ramping) {
535 data = RREG32_DIDT(DIDT_TCP_CTRL0);
536 if (enable)
537 data |= DIDT_CTRL_EN;
538 else
539 data &= ~DIDT_CTRL_EN;
540 WREG32_DIDT(DIDT_TCP_CTRL0, data);
541 }
542}
543
544static int ci_program_pt_config_registers(struct radeon_device *rdev,
545 const struct ci_pt_config_reg *cac_config_regs)
546{
547 const struct ci_pt_config_reg *config_regs = cac_config_regs;
548 u32 data;
549 u32 cache = 0;
550
551 if (config_regs == NULL)
552 return -EINVAL;
553
554 while (config_regs->offset != 0xFFFFFFFF) {
555 if (config_regs->type == CISLANDS_CONFIGREG_CACHE) {
556 cache |= ((config_regs->value << config_regs->shift) & config_regs->mask);
557 } else {
558 switch (config_regs->type) {
559 case CISLANDS_CONFIGREG_SMC_IND:
560 data = RREG32_SMC(config_regs->offset);
561 break;
562 case CISLANDS_CONFIGREG_DIDT_IND:
563 data = RREG32_DIDT(config_regs->offset);
564 break;
565 default:
566 data = RREG32(config_regs->offset << 2);
567 break;
568 }
569
570 data &= ~config_regs->mask;
571 data |= ((config_regs->value << config_regs->shift) & config_regs->mask);
572 data |= cache;
573
574 switch (config_regs->type) {
575 case CISLANDS_CONFIGREG_SMC_IND:
576 WREG32_SMC(config_regs->offset, data);
577 break;
578 case CISLANDS_CONFIGREG_DIDT_IND:
579 WREG32_DIDT(config_regs->offset, data);
580 break;
581 default:
582 WREG32(config_regs->offset << 2, data);
583 break;
584 }
585 cache = 0;
586 }
587 config_regs++;
588 }
589 return 0;
590}
591
592static int ci_enable_didt(struct radeon_device *rdev, bool enable)
593{
594 struct ci_power_info *pi = ci_get_pi(rdev);
595 int ret;
596
597 if (pi->caps_sq_ramping || pi->caps_db_ramping ||
598 pi->caps_td_ramping || pi->caps_tcp_ramping) {
599 cik_enter_rlc_safe_mode(rdev);
600
601 if (enable) {
602 ret = ci_program_pt_config_registers(rdev, didt_config_ci);
603 if (ret) {
604 cik_exit_rlc_safe_mode(rdev);
605 return ret;
606 }
607 }
608
609 ci_do_enable_didt(rdev, enable);
610
611 cik_exit_rlc_safe_mode(rdev);
612 }
613
614 return 0;
615}
616
617static int ci_enable_power_containment(struct radeon_device *rdev, bool enable)
618{
619 struct ci_power_info *pi = ci_get_pi(rdev);
620 PPSMC_Result smc_result;
621 int ret = 0;
622
623 if (enable) {
624 pi->power_containment_features = 0;
625 if (pi->caps_power_containment) {
626 if (pi->enable_bapm_feature) {
627 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableDTE);
628 if (smc_result != PPSMC_Result_OK)
629 ret = -EINVAL;
630 else
631 pi->power_containment_features |= POWERCONTAINMENT_FEATURE_BAPM;
632 }
633
634 if (pi->enable_tdc_limit_feature) {
635 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_TDCLimitEnable);
636 if (smc_result != PPSMC_Result_OK)
637 ret = -EINVAL;
638 else
639 pi->power_containment_features |= POWERCONTAINMENT_FEATURE_TDCLimit;
640 }
641
642 if (pi->enable_pkg_pwr_tracking_feature) {
643 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PkgPwrLimitEnable);
644 if (smc_result != PPSMC_Result_OK) {
645 ret = -EINVAL;
646 } else {
647 struct radeon_cac_tdp_table *cac_tdp_table =
648 rdev->pm.dpm.dyn_state.cac_tdp_table;
649 u32 default_pwr_limit =
650 (u32)(cac_tdp_table->maximum_power_delivery_limit * 256);
651
652 pi->power_containment_features |= POWERCONTAINMENT_FEATURE_PkgPwrLimit;
653
654 ci_set_power_limit(rdev, default_pwr_limit);
655 }
656 }
657 }
658 } else {
659 if (pi->caps_power_containment && pi->power_containment_features) {
660 if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_TDCLimit)
661 ci_send_msg_to_smc(rdev, PPSMC_MSG_TDCLimitDisable);
662
663 if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_BAPM)
664 ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableDTE);
665
666 if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_PkgPwrLimit)
667 ci_send_msg_to_smc(rdev, PPSMC_MSG_PkgPwrLimitDisable);
668 pi->power_containment_features = 0;
669 }
670 }
671
672 return ret;
673}
674
675static int ci_enable_smc_cac(struct radeon_device *rdev, bool enable)
676{
677 struct ci_power_info *pi = ci_get_pi(rdev);
678 PPSMC_Result smc_result;
679 int ret = 0;
680
681 if (pi->caps_cac) {
682 if (enable) {
683 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableCac);
684 if (smc_result != PPSMC_Result_OK) {
685 ret = -EINVAL;
686 pi->cac_enabled = false;
687 } else {
688 pi->cac_enabled = true;
689 }
690 } else if (pi->cac_enabled) {
691 ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableCac);
692 pi->cac_enabled = false;
693 }
694 }
695
696 return ret;
697}
698
699static int ci_enable_thermal_based_sclk_dpm(struct radeon_device *rdev,
700 bool enable)
701{
702 struct ci_power_info *pi = ci_get_pi(rdev);
703 PPSMC_Result smc_result = PPSMC_Result_OK;
704
705 if (pi->thermal_sclk_dpm_enabled) {
706 if (enable)
707 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_ENABLE_THERMAL_DPM);
708 else
709 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DISABLE_THERMAL_DPM);
710 }
711
712 if (smc_result == PPSMC_Result_OK)
713 return 0;
714 else
715 return -EINVAL;
716}
717
718static int ci_power_control_set_level(struct radeon_device *rdev)
719{
720 struct ci_power_info *pi = ci_get_pi(rdev);
721 struct radeon_cac_tdp_table *cac_tdp_table =
722 rdev->pm.dpm.dyn_state.cac_tdp_table;
723 s32 adjust_percent;
724 s32 target_tdp;
725 int ret = 0;
726 bool adjust_polarity = false;
727
728 if (pi->caps_power_containment) {
729 adjust_percent = adjust_polarity ?
730 rdev->pm.dpm.tdp_adjustment : (-1 * rdev->pm.dpm.tdp_adjustment);
731 target_tdp = ((100 + adjust_percent) *
732 (s32)cac_tdp_table->configurable_tdp) / 100;
733
734 ret = ci_set_overdrive_target_tdp(rdev, (u32)target_tdp);
735 }
736
737 return ret;
738}
739
740void ci_dpm_powergate_uvd(struct radeon_device *rdev, bool gate)
741{
742 struct ci_power_info *pi = ci_get_pi(rdev);
743
744 if (pi->uvd_power_gated == gate)
745 return;
746
747 pi->uvd_power_gated = gate;
748
749 ci_update_uvd_dpm(rdev, gate);
750}
751
752bool ci_dpm_vblank_too_short(struct radeon_device *rdev)
753{
754 struct ci_power_info *pi = ci_get_pi(rdev);
755 u32 vblank_time = r600_dpm_get_vblank_time(rdev);
756 u32 switch_limit = pi->mem_gddr5 ? 450 : 300;
757
758
759
760
761 if (r600_dpm_get_vrefresh(rdev) > 120)
762 return true;
763
764 if (vblank_time < switch_limit)
765 return true;
766 else
767 return false;
768
769}
770
771static void ci_apply_state_adjust_rules(struct radeon_device *rdev,
772 struct radeon_ps *rps)
773{
774 struct ci_ps *ps = ci_get_ps(rps);
775 struct ci_power_info *pi = ci_get_pi(rdev);
776 struct radeon_clock_and_voltage_limits *max_limits;
777 bool disable_mclk_switching;
778 u32 sclk, mclk;
779 int i;
780
781 if (rps->vce_active) {
782 rps->evclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].evclk;
783 rps->ecclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].ecclk;
784 } else {
785 rps->evclk = 0;
786 rps->ecclk = 0;
787 }
788
789 if ((rdev->pm.dpm.new_active_crtc_count > 1) ||
790 ci_dpm_vblank_too_short(rdev))
791 disable_mclk_switching = true;
792 else
793 disable_mclk_switching = false;
794
795 if ((rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
796 pi->battery_state = true;
797 else
798 pi->battery_state = false;
799
800 if (rdev->pm.dpm.ac_power)
801 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
802 else
803 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
804
805 if (rdev->pm.dpm.ac_power == false) {
806 for (i = 0; i < ps->performance_level_count; i++) {
807 if (ps->performance_levels[i].mclk > max_limits->mclk)
808 ps->performance_levels[i].mclk = max_limits->mclk;
809 if (ps->performance_levels[i].sclk > max_limits->sclk)
810 ps->performance_levels[i].sclk = max_limits->sclk;
811 }
812 }
813
814
815
816 if (disable_mclk_switching) {
817 mclk = ps->performance_levels[ps->performance_level_count - 1].mclk;
818 sclk = ps->performance_levels[0].sclk;
819 } else {
820 mclk = ps->performance_levels[0].mclk;
821 sclk = ps->performance_levels[0].sclk;
822 }
823
824 if (rps->vce_active) {
825 if (sclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk)
826 sclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk;
827 if (mclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].mclk)
828 mclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].mclk;
829 }
830
831 ps->performance_levels[0].sclk = sclk;
832 ps->performance_levels[0].mclk = mclk;
833
834 if (ps->performance_levels[1].sclk < ps->performance_levels[0].sclk)
835 ps->performance_levels[1].sclk = ps->performance_levels[0].sclk;
836
837 if (disable_mclk_switching) {
838 if (ps->performance_levels[0].mclk < ps->performance_levels[1].mclk)
839 ps->performance_levels[0].mclk = ps->performance_levels[1].mclk;
840 } else {
841 if (ps->performance_levels[1].mclk < ps->performance_levels[0].mclk)
842 ps->performance_levels[1].mclk = ps->performance_levels[0].mclk;
843 }
844}
845
846static int ci_thermal_set_temperature_range(struct radeon_device *rdev,
847 int min_temp, int max_temp)
848{
849 int low_temp = 0 * 1000;
850 int high_temp = 255 * 1000;
851 u32 tmp;
852
853 if (low_temp < min_temp)
854 low_temp = min_temp;
855 if (high_temp > max_temp)
856 high_temp = max_temp;
857 if (high_temp < low_temp) {
858 DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
859 return -EINVAL;
860 }
861
862 tmp = RREG32_SMC(CG_THERMAL_INT);
863 tmp &= ~(CI_DIG_THERM_INTH_MASK | CI_DIG_THERM_INTL_MASK);
864 tmp |= CI_DIG_THERM_INTH(high_temp / 1000) |
865 CI_DIG_THERM_INTL(low_temp / 1000);
866 WREG32_SMC(CG_THERMAL_INT, tmp);
867
868#if 0
869
870 tmp = RREG32_SMC(CG_THERMAL_CTRL);
871 tmp &= DIG_THERM_DPM_MASK;
872 tmp |= DIG_THERM_DPM(high_temp / 1000);
873 WREG32_SMC(CG_THERMAL_CTRL, tmp);
874#endif
875
876 rdev->pm.dpm.thermal.min_temp = low_temp;
877 rdev->pm.dpm.thermal.max_temp = high_temp;
878
879 return 0;
880}
881
882static int ci_thermal_enable_alert(struct radeon_device *rdev,
883 bool enable)
884{
885 u32 thermal_int = RREG32_SMC(CG_THERMAL_INT);
886 PPSMC_Result result;
887
888 if (enable) {
889 thermal_int &= ~(THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW);
890 WREG32_SMC(CG_THERMAL_INT, thermal_int);
891 rdev->irq.dpm_thermal = false;
892 result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Thermal_Cntl_Enable);
893 if (result != PPSMC_Result_OK) {
894 DRM_DEBUG_KMS("Could not enable thermal interrupts.\n");
895 return -EINVAL;
896 }
897 } else {
898 thermal_int |= THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW;
899 WREG32_SMC(CG_THERMAL_INT, thermal_int);
900 rdev->irq.dpm_thermal = true;
901 result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Thermal_Cntl_Disable);
902 if (result != PPSMC_Result_OK) {
903 DRM_DEBUG_KMS("Could not disable thermal interrupts.\n");
904 return -EINVAL;
905 }
906 }
907
908 return 0;
909}
910
911static void ci_fan_ctrl_set_static_mode(struct radeon_device *rdev, u32 mode)
912{
913 struct ci_power_info *pi = ci_get_pi(rdev);
914 u32 tmp;
915
916 if (pi->fan_ctrl_is_in_default_mode) {
917 tmp = (RREG32_SMC(CG_FDO_CTRL2) & FDO_PWM_MODE_MASK) >> FDO_PWM_MODE_SHIFT;
918 pi->fan_ctrl_default_mode = tmp;
919 tmp = (RREG32_SMC(CG_FDO_CTRL2) & TMIN_MASK) >> TMIN_SHIFT;
920 pi->t_min = tmp;
921 pi->fan_ctrl_is_in_default_mode = false;
922 }
923
924 tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TMIN_MASK;
925 tmp |= TMIN(0);
926 WREG32_SMC(CG_FDO_CTRL2, tmp);
927
928 tmp = RREG32_SMC(CG_FDO_CTRL2) & ~FDO_PWM_MODE_MASK;
929 tmp |= FDO_PWM_MODE(mode);
930 WREG32_SMC(CG_FDO_CTRL2, tmp);
931}
932
933static int ci_thermal_setup_fan_table(struct radeon_device *rdev)
934{
935 struct ci_power_info *pi = ci_get_pi(rdev);
936 SMU7_Discrete_FanTable fan_table = { FDO_MODE_HARDWARE };
937 u32 duty100;
938 u32 t_diff1, t_diff2, pwm_diff1, pwm_diff2;
939 u16 fdo_min, slope1, slope2;
940 u32 reference_clock, tmp;
941 int ret;
942 u64 tmp64;
943
944 if (!pi->fan_table_start) {
945 rdev->pm.dpm.fan.ucode_fan_control = false;
946 return 0;
947 }
948
949 duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
950
951 if (duty100 == 0) {
952 rdev->pm.dpm.fan.ucode_fan_control = false;
953 return 0;
954 }
955
956 tmp64 = (u64)rdev->pm.dpm.fan.pwm_min * duty100;
957 do_div(tmp64, 10000);
958 fdo_min = (u16)tmp64;
959
960 t_diff1 = rdev->pm.dpm.fan.t_med - rdev->pm.dpm.fan.t_min;
961 t_diff2 = rdev->pm.dpm.fan.t_high - rdev->pm.dpm.fan.t_med;
962
963 pwm_diff1 = rdev->pm.dpm.fan.pwm_med - rdev->pm.dpm.fan.pwm_min;
964 pwm_diff2 = rdev->pm.dpm.fan.pwm_high - rdev->pm.dpm.fan.pwm_med;
965
966 slope1 = (u16)((50 + ((16 * duty100 * pwm_diff1) / t_diff1)) / 100);
967 slope2 = (u16)((50 + ((16 * duty100 * pwm_diff2) / t_diff2)) / 100);
968
969 fan_table.TempMin = cpu_to_be16((50 + rdev->pm.dpm.fan.t_min) / 100);
970 fan_table.TempMed = cpu_to_be16((50 + rdev->pm.dpm.fan.t_med) / 100);
971 fan_table.TempMax = cpu_to_be16((50 + rdev->pm.dpm.fan.t_max) / 100);
972
973 fan_table.Slope1 = cpu_to_be16(slope1);
974 fan_table.Slope2 = cpu_to_be16(slope2);
975
976 fan_table.FdoMin = cpu_to_be16(fdo_min);
977
978 fan_table.HystDown = cpu_to_be16(rdev->pm.dpm.fan.t_hyst);
979
980 fan_table.HystUp = cpu_to_be16(1);
981
982 fan_table.HystSlope = cpu_to_be16(1);
983
984 fan_table.TempRespLim = cpu_to_be16(5);
985
986 reference_clock = radeon_get_xclk(rdev);
987
988 fan_table.RefreshPeriod = cpu_to_be32((rdev->pm.dpm.fan.cycle_delay *
989 reference_clock) / 1600);
990
991 fan_table.FdoMax = cpu_to_be16((u16)duty100);
992
993 tmp = (RREG32_SMC(CG_MULT_THERMAL_CTRL) & TEMP_SEL_MASK) >> TEMP_SEL_SHIFT;
994 fan_table.TempSrc = (uint8_t)tmp;
995
996 ret = ci_copy_bytes_to_smc(rdev,
997 pi->fan_table_start,
998 (u8 *)(&fan_table),
999 sizeof(fan_table),
1000 pi->sram_end);
1001
1002 if (ret) {
1003 DRM_ERROR("Failed to load fan table to the SMC.");
1004 rdev->pm.dpm.fan.ucode_fan_control = false;
1005 }
1006
1007 return 0;
1008}
1009
1010static int ci_fan_ctrl_start_smc_fan_control(struct radeon_device *rdev)
1011{
1012 struct ci_power_info *pi = ci_get_pi(rdev);
1013 PPSMC_Result ret;
1014
1015 if (pi->caps_od_fuzzy_fan_control_support) {
1016 ret = ci_send_msg_to_smc_with_parameter(rdev,
1017 PPSMC_StartFanControl,
1018 FAN_CONTROL_FUZZY);
1019 if (ret != PPSMC_Result_OK)
1020 return -EINVAL;
1021 ret = ci_send_msg_to_smc_with_parameter(rdev,
1022 PPSMC_MSG_SetFanPwmMax,
1023 rdev->pm.dpm.fan.default_max_fan_pwm);
1024 if (ret != PPSMC_Result_OK)
1025 return -EINVAL;
1026 } else {
1027 ret = ci_send_msg_to_smc_with_parameter(rdev,
1028 PPSMC_StartFanControl,
1029 FAN_CONTROL_TABLE);
1030 if (ret != PPSMC_Result_OK)
1031 return -EINVAL;
1032 }
1033
1034 pi->fan_is_controlled_by_smc = true;
1035 return 0;
1036}
1037
1038static int ci_fan_ctrl_stop_smc_fan_control(struct radeon_device *rdev)
1039{
1040 PPSMC_Result ret;
1041 struct ci_power_info *pi = ci_get_pi(rdev);
1042
1043 ret = ci_send_msg_to_smc(rdev, PPSMC_StopFanControl);
1044 if (ret == PPSMC_Result_OK) {
1045 pi->fan_is_controlled_by_smc = false;
1046 return 0;
1047 } else
1048 return -EINVAL;
1049}
1050
1051int ci_fan_ctrl_get_fan_speed_percent(struct radeon_device *rdev,
1052 u32 *speed)
1053{
1054 u32 duty, duty100;
1055 u64 tmp64;
1056
1057 if (rdev->pm.no_fan)
1058 return -ENOENT;
1059
1060 duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
1061 duty = (RREG32_SMC(CG_THERMAL_STATUS) & FDO_PWM_DUTY_MASK) >> FDO_PWM_DUTY_SHIFT;
1062
1063 if (duty100 == 0)
1064 return -EINVAL;
1065
1066 tmp64 = (u64)duty * 100;
1067 do_div(tmp64, duty100);
1068 *speed = (u32)tmp64;
1069
1070 if (*speed > 100)
1071 *speed = 100;
1072
1073 return 0;
1074}
1075
1076int ci_fan_ctrl_set_fan_speed_percent(struct radeon_device *rdev,
1077 u32 speed)
1078{
1079 u32 tmp;
1080 u32 duty, duty100;
1081 u64 tmp64;
1082 struct ci_power_info *pi = ci_get_pi(rdev);
1083
1084 if (rdev->pm.no_fan)
1085 return -ENOENT;
1086
1087 if (pi->fan_is_controlled_by_smc)
1088 return -EINVAL;
1089
1090 if (speed > 100)
1091 return -EINVAL;
1092
1093 duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
1094
1095 if (duty100 == 0)
1096 return -EINVAL;
1097
1098 tmp64 = (u64)speed * duty100;
1099 do_div(tmp64, 100);
1100 duty = (u32)tmp64;
1101
1102 tmp = RREG32_SMC(CG_FDO_CTRL0) & ~FDO_STATIC_DUTY_MASK;
1103 tmp |= FDO_STATIC_DUTY(duty);
1104 WREG32_SMC(CG_FDO_CTRL0, tmp);
1105
1106 return 0;
1107}
1108
1109void ci_fan_ctrl_set_mode(struct radeon_device *rdev, u32 mode)
1110{
1111 if (mode) {
1112
1113 if (rdev->pm.dpm.fan.ucode_fan_control)
1114 ci_fan_ctrl_stop_smc_fan_control(rdev);
1115 ci_fan_ctrl_set_static_mode(rdev, mode);
1116 } else {
1117
1118 if (rdev->pm.dpm.fan.ucode_fan_control)
1119 ci_thermal_start_smc_fan_control(rdev);
1120 else
1121 ci_fan_ctrl_set_default_mode(rdev);
1122 }
1123}
1124
1125u32 ci_fan_ctrl_get_mode(struct radeon_device *rdev)
1126{
1127 struct ci_power_info *pi = ci_get_pi(rdev);
1128 u32 tmp;
1129
1130 if (pi->fan_is_controlled_by_smc)
1131 return 0;
1132
1133 tmp = RREG32_SMC(CG_FDO_CTRL2) & FDO_PWM_MODE_MASK;
1134 return (tmp >> FDO_PWM_MODE_SHIFT);
1135}
1136
1137#if 0
1138static int ci_fan_ctrl_get_fan_speed_rpm(struct radeon_device *rdev,
1139 u32 *speed)
1140{
1141 u32 tach_period;
1142 u32 xclk = radeon_get_xclk(rdev);
1143
1144 if (rdev->pm.no_fan)
1145 return -ENOENT;
1146
1147 if (rdev->pm.fan_pulses_per_revolution == 0)
1148 return -ENOENT;
1149
1150 tach_period = (RREG32_SMC(CG_TACH_STATUS) & TACH_PERIOD_MASK) >> TACH_PERIOD_SHIFT;
1151 if (tach_period == 0)
1152 return -ENOENT;
1153
1154 *speed = 60 * xclk * 10000 / tach_period;
1155
1156 return 0;
1157}
1158
1159static int ci_fan_ctrl_set_fan_speed_rpm(struct radeon_device *rdev,
1160 u32 speed)
1161{
1162 u32 tach_period, tmp;
1163 u32 xclk = radeon_get_xclk(rdev);
1164
1165 if (rdev->pm.no_fan)
1166 return -ENOENT;
1167
1168 if (rdev->pm.fan_pulses_per_revolution == 0)
1169 return -ENOENT;
1170
1171 if ((speed < rdev->pm.fan_min_rpm) ||
1172 (speed > rdev->pm.fan_max_rpm))
1173 return -EINVAL;
1174
1175 if (rdev->pm.dpm.fan.ucode_fan_control)
1176 ci_fan_ctrl_stop_smc_fan_control(rdev);
1177
1178 tach_period = 60 * xclk * 10000 / (8 * speed);
1179 tmp = RREG32_SMC(CG_TACH_CTRL) & ~TARGET_PERIOD_MASK;
1180 tmp |= TARGET_PERIOD(tach_period);
1181 WREG32_SMC(CG_TACH_CTRL, tmp);
1182
1183 ci_fan_ctrl_set_static_mode(rdev, FDO_PWM_MODE_STATIC_RPM);
1184
1185 return 0;
1186}
1187#endif
1188
1189static void ci_fan_ctrl_set_default_mode(struct radeon_device *rdev)
1190{
1191 struct ci_power_info *pi = ci_get_pi(rdev);
1192 u32 tmp;
1193
1194 if (!pi->fan_ctrl_is_in_default_mode) {
1195 tmp = RREG32_SMC(CG_FDO_CTRL2) & ~FDO_PWM_MODE_MASK;
1196 tmp |= FDO_PWM_MODE(pi->fan_ctrl_default_mode);
1197 WREG32_SMC(CG_FDO_CTRL2, tmp);
1198
1199 tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TMIN_MASK;
1200 tmp |= TMIN(pi->t_min);
1201 WREG32_SMC(CG_FDO_CTRL2, tmp);
1202 pi->fan_ctrl_is_in_default_mode = true;
1203 }
1204}
1205
1206static void ci_thermal_start_smc_fan_control(struct radeon_device *rdev)
1207{
1208 if (rdev->pm.dpm.fan.ucode_fan_control) {
1209 ci_fan_ctrl_start_smc_fan_control(rdev);
1210 ci_fan_ctrl_set_static_mode(rdev, FDO_PWM_MODE_STATIC);
1211 }
1212}
1213
1214static void ci_thermal_initialize(struct radeon_device *rdev)
1215{
1216 u32 tmp;
1217
1218 if (rdev->pm.fan_pulses_per_revolution) {
1219 tmp = RREG32_SMC(CG_TACH_CTRL) & ~EDGE_PER_REV_MASK;
1220 tmp |= EDGE_PER_REV(rdev->pm.fan_pulses_per_revolution -1);
1221 WREG32_SMC(CG_TACH_CTRL, tmp);
1222 }
1223
1224 tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TACH_PWM_RESP_RATE_MASK;
1225 tmp |= TACH_PWM_RESP_RATE(0x28);
1226 WREG32_SMC(CG_FDO_CTRL2, tmp);
1227}
1228
1229static int ci_thermal_start_thermal_controller(struct radeon_device *rdev)
1230{
1231 int ret;
1232
1233 ci_thermal_initialize(rdev);
1234 ret = ci_thermal_set_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
1235 if (ret)
1236 return ret;
1237 ret = ci_thermal_enable_alert(rdev, true);
1238 if (ret)
1239 return ret;
1240 if (rdev->pm.dpm.fan.ucode_fan_control) {
1241 ret = ci_thermal_setup_fan_table(rdev);
1242 if (ret)
1243 return ret;
1244 ci_thermal_start_smc_fan_control(rdev);
1245 }
1246
1247 return 0;
1248}
1249
1250static void ci_thermal_stop_thermal_controller(struct radeon_device *rdev)
1251{
1252 if (!rdev->pm.no_fan)
1253 ci_fan_ctrl_set_default_mode(rdev);
1254}
1255
1256#if 0
1257static int ci_read_smc_soft_register(struct radeon_device *rdev,
1258 u16 reg_offset, u32 *value)
1259{
1260 struct ci_power_info *pi = ci_get_pi(rdev);
1261
1262 return ci_read_smc_sram_dword(rdev,
1263 pi->soft_regs_start + reg_offset,
1264 value, pi->sram_end);
1265}
1266#endif
1267
1268static int ci_write_smc_soft_register(struct radeon_device *rdev,
1269 u16 reg_offset, u32 value)
1270{
1271 struct ci_power_info *pi = ci_get_pi(rdev);
1272
1273 return ci_write_smc_sram_dword(rdev,
1274 pi->soft_regs_start + reg_offset,
1275 value, pi->sram_end);
1276}
1277
1278static void ci_init_fps_limits(struct radeon_device *rdev)
1279{
1280 struct ci_power_info *pi = ci_get_pi(rdev);
1281 SMU7_Discrete_DpmTable *table = &pi->smc_state_table;
1282
1283 if (pi->caps_fps) {
1284 u16 tmp;
1285
1286 tmp = 45;
1287 table->FpsHighT = cpu_to_be16(tmp);
1288
1289 tmp = 30;
1290 table->FpsLowT = cpu_to_be16(tmp);
1291 }
1292}
1293
1294static int ci_update_sclk_t(struct radeon_device *rdev)
1295{
1296 struct ci_power_info *pi = ci_get_pi(rdev);
1297 int ret = 0;
1298 u32 low_sclk_interrupt_t = 0;
1299
1300 if (pi->caps_sclk_throttle_low_notification) {
1301 low_sclk_interrupt_t = cpu_to_be32(pi->low_sclk_interrupt_t);
1302
1303 ret = ci_copy_bytes_to_smc(rdev,
1304 pi->dpm_table_start +
1305 offsetof(SMU7_Discrete_DpmTable, LowSclkInterruptT),
1306 (u8 *)&low_sclk_interrupt_t,
1307 sizeof(u32), pi->sram_end);
1308
1309 }
1310
1311 return ret;
1312}
1313
1314static void ci_get_leakage_voltages(struct radeon_device *rdev)
1315{
1316 struct ci_power_info *pi = ci_get_pi(rdev);
1317 u16 leakage_id, virtual_voltage_id;
1318 u16 vddc, vddci;
1319 int i;
1320
1321 pi->vddc_leakage.count = 0;
1322 pi->vddci_leakage.count = 0;
1323
1324 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_EVV) {
1325 for (i = 0; i < CISLANDS_MAX_LEAKAGE_COUNT; i++) {
1326 virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
1327 if (radeon_atom_get_voltage_evv(rdev, virtual_voltage_id, &vddc) != 0)
1328 continue;
1329 if (vddc != 0 && vddc != virtual_voltage_id) {
1330 pi->vddc_leakage.actual_voltage[pi->vddc_leakage.count] = vddc;
1331 pi->vddc_leakage.leakage_id[pi->vddc_leakage.count] = virtual_voltage_id;
1332 pi->vddc_leakage.count++;
1333 }
1334 }
1335 } else if (radeon_atom_get_leakage_id_from_vbios(rdev, &leakage_id) == 0) {
1336 for (i = 0; i < CISLANDS_MAX_LEAKAGE_COUNT; i++) {
1337 virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
1338 if (radeon_atom_get_leakage_vddc_based_on_leakage_params(rdev, &vddc, &vddci,
1339 virtual_voltage_id,
1340 leakage_id) == 0) {
1341 if (vddc != 0 && vddc != virtual_voltage_id) {
1342 pi->vddc_leakage.actual_voltage[pi->vddc_leakage.count] = vddc;
1343 pi->vddc_leakage.leakage_id[pi->vddc_leakage.count] = virtual_voltage_id;
1344 pi->vddc_leakage.count++;
1345 }
1346 if (vddci != 0 && vddci != virtual_voltage_id) {
1347 pi->vddci_leakage.actual_voltage[pi->vddci_leakage.count] = vddci;
1348 pi->vddci_leakage.leakage_id[pi->vddci_leakage.count] = virtual_voltage_id;
1349 pi->vddci_leakage.count++;
1350 }
1351 }
1352 }
1353 }
1354}
1355
1356static void ci_set_dpm_event_sources(struct radeon_device *rdev, u32 sources)
1357{
1358 struct ci_power_info *pi = ci_get_pi(rdev);
1359 bool want_thermal_protection;
1360 u32 tmp;
1361
1362 switch (sources) {
1363 case 0:
1364 default:
1365 want_thermal_protection = false;
1366 break;
1367 case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL):
1368 want_thermal_protection = true;
1369 break;
1370 case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL):
1371 want_thermal_protection = true;
1372 break;
1373 case ((1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL) |
1374 (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL)):
1375 want_thermal_protection = true;
1376 break;
1377 }
1378
1379 if (want_thermal_protection) {
1380 tmp = RREG32_SMC(GENERAL_PWRMGT);
1381 if (pi->thermal_protection)
1382 tmp &= ~THERMAL_PROTECTION_DIS;
1383 else
1384 tmp |= THERMAL_PROTECTION_DIS;
1385 WREG32_SMC(GENERAL_PWRMGT, tmp);
1386 } else {
1387 tmp = RREG32_SMC(GENERAL_PWRMGT);
1388 tmp |= THERMAL_PROTECTION_DIS;
1389 WREG32_SMC(GENERAL_PWRMGT, tmp);
1390 }
1391}
1392
1393static void ci_enable_auto_throttle_source(struct radeon_device *rdev,
1394 enum radeon_dpm_auto_throttle_src source,
1395 bool enable)
1396{
1397 struct ci_power_info *pi = ci_get_pi(rdev);
1398
1399 if (enable) {
1400 if (!(pi->active_auto_throttle_sources & (1 << source))) {
1401 pi->active_auto_throttle_sources |= 1 << source;
1402 ci_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
1403 }
1404 } else {
1405 if (pi->active_auto_throttle_sources & (1 << source)) {
1406 pi->active_auto_throttle_sources &= ~(1 << source);
1407 ci_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
1408 }
1409 }
1410}
1411
1412static void ci_enable_vr_hot_gpio_interrupt(struct radeon_device *rdev)
1413{
1414 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_REGULATOR_HOT)
1415 ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableVRHotGPIOInterrupt);
1416}
1417
1418static int ci_unfreeze_sclk_mclk_dpm(struct radeon_device *rdev)
1419{
1420 struct ci_power_info *pi = ci_get_pi(rdev);
1421 PPSMC_Result smc_result;
1422
1423 if (!pi->need_update_smu7_dpm_table)
1424 return 0;
1425
1426 if ((!pi->sclk_dpm_key_disabled) &&
1427 (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK))) {
1428 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_SCLKDPM_UnfreezeLevel);
1429 if (smc_result != PPSMC_Result_OK)
1430 return -EINVAL;
1431 }
1432
1433 if ((!pi->mclk_dpm_key_disabled) &&
1434 (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
1435 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_UnfreezeLevel);
1436 if (smc_result != PPSMC_Result_OK)
1437 return -EINVAL;
1438 }
1439
1440 pi->need_update_smu7_dpm_table = 0;
1441 return 0;
1442}
1443
1444static int ci_enable_sclk_mclk_dpm(struct radeon_device *rdev, bool enable)
1445{
1446 struct ci_power_info *pi = ci_get_pi(rdev);
1447 PPSMC_Result smc_result;
1448
1449 if (enable) {
1450 if (!pi->sclk_dpm_key_disabled) {
1451 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DPM_Enable);
1452 if (smc_result != PPSMC_Result_OK)
1453 return -EINVAL;
1454 }
1455
1456 if (!pi->mclk_dpm_key_disabled) {
1457 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_Enable);
1458 if (smc_result != PPSMC_Result_OK)
1459 return -EINVAL;
1460
1461 WREG32_P(MC_SEQ_CNTL_3, CAC_EN, ~CAC_EN);
1462
1463 WREG32_SMC(LCAC_MC0_CNTL, 0x05);
1464 WREG32_SMC(LCAC_MC1_CNTL, 0x05);
1465 WREG32_SMC(LCAC_CPL_CNTL, 0x100005);
1466
1467 udelay(10);
1468
1469 WREG32_SMC(LCAC_MC0_CNTL, 0x400005);
1470 WREG32_SMC(LCAC_MC1_CNTL, 0x400005);
1471 WREG32_SMC(LCAC_CPL_CNTL, 0x500005);
1472 }
1473 } else {
1474 if (!pi->sclk_dpm_key_disabled) {
1475 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DPM_Disable);
1476 if (smc_result != PPSMC_Result_OK)
1477 return -EINVAL;
1478 }
1479
1480 if (!pi->mclk_dpm_key_disabled) {
1481 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_Disable);
1482 if (smc_result != PPSMC_Result_OK)
1483 return -EINVAL;
1484 }
1485 }
1486
1487 return 0;
1488}
1489
1490static int ci_start_dpm(struct radeon_device *rdev)
1491{
1492 struct ci_power_info *pi = ci_get_pi(rdev);
1493 PPSMC_Result smc_result;
1494 int ret;
1495 u32 tmp;
1496
1497 tmp = RREG32_SMC(GENERAL_PWRMGT);
1498 tmp |= GLOBAL_PWRMGT_EN;
1499 WREG32_SMC(GENERAL_PWRMGT, tmp);
1500
1501 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
1502 tmp |= DYNAMIC_PM_EN;
1503 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
1504
1505 ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, VoltageChangeTimeout), 0x1000);
1506
1507 WREG32_P(BIF_LNCNT_RESET, 0, ~RESET_LNCNT_EN);
1508
1509 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Voltage_Cntl_Enable);
1510 if (smc_result != PPSMC_Result_OK)
1511 return -EINVAL;
1512
1513 ret = ci_enable_sclk_mclk_dpm(rdev, true);
1514 if (ret)
1515 return ret;
1516
1517 if (!pi->pcie_dpm_key_disabled) {
1518 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PCIeDPM_Enable);
1519 if (smc_result != PPSMC_Result_OK)
1520 return -EINVAL;
1521 }
1522
1523 return 0;
1524}
1525
1526static int ci_freeze_sclk_mclk_dpm(struct radeon_device *rdev)
1527{
1528 struct ci_power_info *pi = ci_get_pi(rdev);
1529 PPSMC_Result smc_result;
1530
1531 if (!pi->need_update_smu7_dpm_table)
1532 return 0;
1533
1534 if ((!pi->sclk_dpm_key_disabled) &&
1535 (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK))) {
1536 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_SCLKDPM_FreezeLevel);
1537 if (smc_result != PPSMC_Result_OK)
1538 return -EINVAL;
1539 }
1540
1541 if ((!pi->mclk_dpm_key_disabled) &&
1542 (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
1543 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_FreezeLevel);
1544 if (smc_result != PPSMC_Result_OK)
1545 return -EINVAL;
1546 }
1547
1548 return 0;
1549}
1550
1551static int ci_stop_dpm(struct radeon_device *rdev)
1552{
1553 struct ci_power_info *pi = ci_get_pi(rdev);
1554 PPSMC_Result smc_result;
1555 int ret;
1556 u32 tmp;
1557
1558 tmp = RREG32_SMC(GENERAL_PWRMGT);
1559 tmp &= ~GLOBAL_PWRMGT_EN;
1560 WREG32_SMC(GENERAL_PWRMGT, tmp);
1561
1562 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
1563 tmp &= ~DYNAMIC_PM_EN;
1564 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
1565
1566 if (!pi->pcie_dpm_key_disabled) {
1567 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PCIeDPM_Disable);
1568 if (smc_result != PPSMC_Result_OK)
1569 return -EINVAL;
1570 }
1571
1572 ret = ci_enable_sclk_mclk_dpm(rdev, false);
1573 if (ret)
1574 return ret;
1575
1576 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Voltage_Cntl_Disable);
1577 if (smc_result != PPSMC_Result_OK)
1578 return -EINVAL;
1579
1580 return 0;
1581}
1582
1583static void ci_enable_sclk_control(struct radeon_device *rdev, bool enable)
1584{
1585 u32 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
1586
1587 if (enable)
1588 tmp &= ~SCLK_PWRMGT_OFF;
1589 else
1590 tmp |= SCLK_PWRMGT_OFF;
1591 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
1592}
1593
1594#if 0
1595static int ci_notify_hw_of_power_source(struct radeon_device *rdev,
1596 bool ac_power)
1597{
1598 struct ci_power_info *pi = ci_get_pi(rdev);
1599 struct radeon_cac_tdp_table *cac_tdp_table =
1600 rdev->pm.dpm.dyn_state.cac_tdp_table;
1601 u32 power_limit;
1602
1603 if (ac_power)
1604 power_limit = (u32)(cac_tdp_table->maximum_power_delivery_limit * 256);
1605 else
1606 power_limit = (u32)(cac_tdp_table->battery_power_limit * 256);
1607
1608 ci_set_power_limit(rdev, power_limit);
1609
1610 if (pi->caps_automatic_dc_transition) {
1611 if (ac_power)
1612 ci_send_msg_to_smc(rdev, PPSMC_MSG_RunningOnAC);
1613 else
1614 ci_send_msg_to_smc(rdev, PPSMC_MSG_Remove_DC_Clamp);
1615 }
1616
1617 return 0;
1618}
1619#endif
1620
1621static PPSMC_Result ci_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg)
1622{
1623 u32 tmp;
1624 int i;
1625
1626 if (!ci_is_smc_running(rdev))
1627 return PPSMC_Result_Failed;
1628
1629 WREG32(SMC_MESSAGE_0, msg);
1630
1631 for (i = 0; i < rdev->usec_timeout; i++) {
1632 tmp = RREG32(SMC_RESP_0);
1633 if (tmp != 0)
1634 break;
1635 udelay(1);
1636 }
1637 tmp = RREG32(SMC_RESP_0);
1638
1639 return (PPSMC_Result)tmp;
1640}
1641
1642static PPSMC_Result ci_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
1643 PPSMC_Msg msg, u32 parameter)
1644{
1645 WREG32(SMC_MSG_ARG_0, parameter);
1646 return ci_send_msg_to_smc(rdev, msg);
1647}
1648
1649static PPSMC_Result ci_send_msg_to_smc_return_parameter(struct radeon_device *rdev,
1650 PPSMC_Msg msg, u32 *parameter)
1651{
1652 PPSMC_Result smc_result;
1653
1654 smc_result = ci_send_msg_to_smc(rdev, msg);
1655
1656 if ((smc_result == PPSMC_Result_OK) && parameter)
1657 *parameter = RREG32(SMC_MSG_ARG_0);
1658
1659 return smc_result;
1660}
1661
1662static int ci_dpm_force_state_sclk(struct radeon_device *rdev, u32 n)
1663{
1664 struct ci_power_info *pi = ci_get_pi(rdev);
1665
1666 if (!pi->sclk_dpm_key_disabled) {
1667 PPSMC_Result smc_result =
1668 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SCLKDPM_SetEnabledMask, 1 << n);
1669 if (smc_result != PPSMC_Result_OK)
1670 return -EINVAL;
1671 }
1672
1673 return 0;
1674}
1675
1676static int ci_dpm_force_state_mclk(struct radeon_device *rdev, u32 n)
1677{
1678 struct ci_power_info *pi = ci_get_pi(rdev);
1679
1680 if (!pi->mclk_dpm_key_disabled) {
1681 PPSMC_Result smc_result =
1682 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_MCLKDPM_SetEnabledMask, 1 << n);
1683 if (smc_result != PPSMC_Result_OK)
1684 return -EINVAL;
1685 }
1686
1687 return 0;
1688}
1689
1690static int ci_dpm_force_state_pcie(struct radeon_device *rdev, u32 n)
1691{
1692 struct ci_power_info *pi = ci_get_pi(rdev);
1693
1694 if (!pi->pcie_dpm_key_disabled) {
1695 PPSMC_Result smc_result =
1696 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_PCIeDPM_ForceLevel, n);
1697 if (smc_result != PPSMC_Result_OK)
1698 return -EINVAL;
1699 }
1700
1701 return 0;
1702}
1703
1704static int ci_set_power_limit(struct radeon_device *rdev, u32 n)
1705{
1706 struct ci_power_info *pi = ci_get_pi(rdev);
1707
1708 if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_PkgPwrLimit) {
1709 PPSMC_Result smc_result =
1710 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_PkgPwrSetLimit, n);
1711 if (smc_result != PPSMC_Result_OK)
1712 return -EINVAL;
1713 }
1714
1715 return 0;
1716}
1717
1718static int ci_set_overdrive_target_tdp(struct radeon_device *rdev,
1719 u32 target_tdp)
1720{
1721 PPSMC_Result smc_result =
1722 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_OverDriveSetTargetTdp, target_tdp);
1723 if (smc_result != PPSMC_Result_OK)
1724 return -EINVAL;
1725 return 0;
1726}
1727
1728#if 0
1729static int ci_set_boot_state(struct radeon_device *rdev)
1730{
1731 return ci_enable_sclk_mclk_dpm(rdev, false);
1732}
1733#endif
1734
1735static u32 ci_get_average_sclk_freq(struct radeon_device *rdev)
1736{
1737 u32 sclk_freq;
1738 PPSMC_Result smc_result =
1739 ci_send_msg_to_smc_return_parameter(rdev,
1740 PPSMC_MSG_API_GetSclkFrequency,
1741 &sclk_freq);
1742 if (smc_result != PPSMC_Result_OK)
1743 sclk_freq = 0;
1744
1745 return sclk_freq;
1746}
1747
1748static u32 ci_get_average_mclk_freq(struct radeon_device *rdev)
1749{
1750 u32 mclk_freq;
1751 PPSMC_Result smc_result =
1752 ci_send_msg_to_smc_return_parameter(rdev,
1753 PPSMC_MSG_API_GetMclkFrequency,
1754 &mclk_freq);
1755 if (smc_result != PPSMC_Result_OK)
1756 mclk_freq = 0;
1757
1758 return mclk_freq;
1759}
1760
1761static void ci_dpm_start_smc(struct radeon_device *rdev)
1762{
1763 int i;
1764
1765 ci_program_jump_on_start(rdev);
1766 ci_start_smc_clock(rdev);
1767 ci_start_smc(rdev);
1768 for (i = 0; i < rdev->usec_timeout; i++) {
1769 if (RREG32_SMC(FIRMWARE_FLAGS) & INTERRUPTS_ENABLED)
1770 break;
1771 }
1772}
1773
1774static void ci_dpm_stop_smc(struct radeon_device *rdev)
1775{
1776 ci_reset_smc(rdev);
1777 ci_stop_smc_clock(rdev);
1778}
1779
1780static int ci_process_firmware_header(struct radeon_device *rdev)
1781{
1782 struct ci_power_info *pi = ci_get_pi(rdev);
1783 u32 tmp;
1784 int ret;
1785
1786 ret = ci_read_smc_sram_dword(rdev,
1787 SMU7_FIRMWARE_HEADER_LOCATION +
1788 offsetof(SMU7_Firmware_Header, DpmTable),
1789 &tmp, pi->sram_end);
1790 if (ret)
1791 return ret;
1792
1793 pi->dpm_table_start = tmp;
1794
1795 ret = ci_read_smc_sram_dword(rdev,
1796 SMU7_FIRMWARE_HEADER_LOCATION +
1797 offsetof(SMU7_Firmware_Header, SoftRegisters),
1798 &tmp, pi->sram_end);
1799 if (ret)
1800 return ret;
1801
1802 pi->soft_regs_start = tmp;
1803
1804 ret = ci_read_smc_sram_dword(rdev,
1805 SMU7_FIRMWARE_HEADER_LOCATION +
1806 offsetof(SMU7_Firmware_Header, mcRegisterTable),
1807 &tmp, pi->sram_end);
1808 if (ret)
1809 return ret;
1810
1811 pi->mc_reg_table_start = tmp;
1812
1813 ret = ci_read_smc_sram_dword(rdev,
1814 SMU7_FIRMWARE_HEADER_LOCATION +
1815 offsetof(SMU7_Firmware_Header, FanTable),
1816 &tmp, pi->sram_end);
1817 if (ret)
1818 return ret;
1819
1820 pi->fan_table_start = tmp;
1821
1822 ret = ci_read_smc_sram_dword(rdev,
1823 SMU7_FIRMWARE_HEADER_LOCATION +
1824 offsetof(SMU7_Firmware_Header, mcArbDramTimingTable),
1825 &tmp, pi->sram_end);
1826 if (ret)
1827 return ret;
1828
1829 pi->arb_table_start = tmp;
1830
1831 return 0;
1832}
1833
1834static void ci_read_clock_registers(struct radeon_device *rdev)
1835{
1836 struct ci_power_info *pi = ci_get_pi(rdev);
1837
1838 pi->clock_registers.cg_spll_func_cntl =
1839 RREG32_SMC(CG_SPLL_FUNC_CNTL);
1840 pi->clock_registers.cg_spll_func_cntl_2 =
1841 RREG32_SMC(CG_SPLL_FUNC_CNTL_2);
1842 pi->clock_registers.cg_spll_func_cntl_3 =
1843 RREG32_SMC(CG_SPLL_FUNC_CNTL_3);
1844 pi->clock_registers.cg_spll_func_cntl_4 =
1845 RREG32_SMC(CG_SPLL_FUNC_CNTL_4);
1846 pi->clock_registers.cg_spll_spread_spectrum =
1847 RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM);
1848 pi->clock_registers.cg_spll_spread_spectrum_2 =
1849 RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM_2);
1850 pi->clock_registers.dll_cntl = RREG32(DLL_CNTL);
1851 pi->clock_registers.mclk_pwrmgt_cntl = RREG32(MCLK_PWRMGT_CNTL);
1852 pi->clock_registers.mpll_ad_func_cntl = RREG32(MPLL_AD_FUNC_CNTL);
1853 pi->clock_registers.mpll_dq_func_cntl = RREG32(MPLL_DQ_FUNC_CNTL);
1854 pi->clock_registers.mpll_func_cntl = RREG32(MPLL_FUNC_CNTL);
1855 pi->clock_registers.mpll_func_cntl_1 = RREG32(MPLL_FUNC_CNTL_1);
1856 pi->clock_registers.mpll_func_cntl_2 = RREG32(MPLL_FUNC_CNTL_2);
1857 pi->clock_registers.mpll_ss1 = RREG32(MPLL_SS1);
1858 pi->clock_registers.mpll_ss2 = RREG32(MPLL_SS2);
1859}
1860
1861static void ci_init_sclk_t(struct radeon_device *rdev)
1862{
1863 struct ci_power_info *pi = ci_get_pi(rdev);
1864
1865 pi->low_sclk_interrupt_t = 0;
1866}
1867
1868static void ci_enable_thermal_protection(struct radeon_device *rdev,
1869 bool enable)
1870{
1871 u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
1872
1873 if (enable)
1874 tmp &= ~THERMAL_PROTECTION_DIS;
1875 else
1876 tmp |= THERMAL_PROTECTION_DIS;
1877 WREG32_SMC(GENERAL_PWRMGT, tmp);
1878}
1879
1880static void ci_enable_acpi_power_management(struct radeon_device *rdev)
1881{
1882 u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
1883
1884 tmp |= STATIC_PM_EN;
1885
1886 WREG32_SMC(GENERAL_PWRMGT, tmp);
1887}
1888
1889#if 0
1890static int ci_enter_ulp_state(struct radeon_device *rdev)
1891{
1892
1893 WREG32(SMC_MESSAGE_0, PPSMC_MSG_SwitchToMinimumPower);
1894
1895 udelay(25000);
1896
1897 return 0;
1898}
1899
1900static int ci_exit_ulp_state(struct radeon_device *rdev)
1901{
1902 int i;
1903
1904 WREG32(SMC_MESSAGE_0, PPSMC_MSG_ResumeFromMinimumPower);
1905
1906 udelay(7000);
1907
1908 for (i = 0; i < rdev->usec_timeout; i++) {
1909 if (RREG32(SMC_RESP_0) == 1)
1910 break;
1911 udelay(1000);
1912 }
1913
1914 return 0;
1915}
1916#endif
1917
1918static int ci_notify_smc_display_change(struct radeon_device *rdev,
1919 bool has_display)
1920{
1921 PPSMC_Msg msg = has_display ? PPSMC_MSG_HasDisplay : PPSMC_MSG_NoDisplay;
1922
1923 return (ci_send_msg_to_smc(rdev, msg) == PPSMC_Result_OK) ? 0 : -EINVAL;
1924}
1925
1926static int ci_enable_ds_master_switch(struct radeon_device *rdev,
1927 bool enable)
1928{
1929 struct ci_power_info *pi = ci_get_pi(rdev);
1930
1931 if (enable) {
1932 if (pi->caps_sclk_ds) {
1933 if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_ON) != PPSMC_Result_OK)
1934 return -EINVAL;
1935 } else {
1936 if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_OFF) != PPSMC_Result_OK)
1937 return -EINVAL;
1938 }
1939 } else {
1940 if (pi->caps_sclk_ds) {
1941 if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_OFF) != PPSMC_Result_OK)
1942 return -EINVAL;
1943 }
1944 }
1945
1946 return 0;
1947}
1948
1949static void ci_program_display_gap(struct radeon_device *rdev)
1950{
1951 u32 tmp = RREG32_SMC(CG_DISPLAY_GAP_CNTL);
1952 u32 pre_vbi_time_in_us;
1953 u32 frame_time_in_us;
1954 u32 ref_clock = rdev->clock.spll.reference_freq;
1955 u32 refresh_rate = r600_dpm_get_vrefresh(rdev);
1956 u32 vblank_time = r600_dpm_get_vblank_time(rdev);
1957
1958 tmp &= ~DISP_GAP_MASK;
1959 if (rdev->pm.dpm.new_active_crtc_count > 0)
1960 tmp |= DISP_GAP(R600_PM_DISPLAY_GAP_VBLANK_OR_WM);
1961 else
1962 tmp |= DISP_GAP(R600_PM_DISPLAY_GAP_IGNORE);
1963 WREG32_SMC(CG_DISPLAY_GAP_CNTL, tmp);
1964
1965 if (refresh_rate == 0)
1966 refresh_rate = 60;
1967 if (vblank_time == 0xffffffff)
1968 vblank_time = 500;
1969 frame_time_in_us = 1000000 / refresh_rate;
1970 pre_vbi_time_in_us =
1971 frame_time_in_us - 200 - vblank_time;
1972 tmp = pre_vbi_time_in_us * (ref_clock / 100);
1973
1974 WREG32_SMC(CG_DISPLAY_GAP_CNTL2, tmp);
1975 ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, PreVBlankGap), 0x64);
1976 ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, VBlankTimeout), (frame_time_in_us - pre_vbi_time_in_us));
1977
1978
1979 ci_notify_smc_display_change(rdev, (rdev->pm.dpm.new_active_crtc_count == 1));
1980
1981}
1982
1983static void ci_enable_spread_spectrum(struct radeon_device *rdev, bool enable)
1984{
1985 struct ci_power_info *pi = ci_get_pi(rdev);
1986 u32 tmp;
1987
1988 if (enable) {
1989 if (pi->caps_sclk_ss_support) {
1990 tmp = RREG32_SMC(GENERAL_PWRMGT);
1991 tmp |= DYN_SPREAD_SPECTRUM_EN;
1992 WREG32_SMC(GENERAL_PWRMGT, tmp);
1993 }
1994 } else {
1995 tmp = RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM);
1996 tmp &= ~SSEN;
1997 WREG32_SMC(CG_SPLL_SPREAD_SPECTRUM, tmp);
1998
1999 tmp = RREG32_SMC(GENERAL_PWRMGT);
2000 tmp &= ~DYN_SPREAD_SPECTRUM_EN;
2001 WREG32_SMC(GENERAL_PWRMGT, tmp);
2002 }
2003}
2004
2005static void ci_program_sstp(struct radeon_device *rdev)
2006{
2007 WREG32_SMC(CG_SSP, (SSTU(R600_SSTU_DFLT) | SST(R600_SST_DFLT)));
2008}
2009
2010static void ci_enable_display_gap(struct radeon_device *rdev)
2011{
2012 u32 tmp = RREG32_SMC(CG_DISPLAY_GAP_CNTL);
2013
2014 tmp &= ~(DISP_GAP_MASK | DISP_GAP_MCHG_MASK);
2015 tmp |= (DISP_GAP(R600_PM_DISPLAY_GAP_IGNORE) |
2016 DISP_GAP_MCHG(R600_PM_DISPLAY_GAP_VBLANK));
2017
2018 WREG32_SMC(CG_DISPLAY_GAP_CNTL, tmp);
2019}
2020
2021static void ci_program_vc(struct radeon_device *rdev)
2022{
2023 u32 tmp;
2024
2025 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
2026 tmp &= ~(RESET_SCLK_CNT | RESET_BUSY_CNT);
2027 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
2028
2029 WREG32_SMC(CG_FTV_0, CISLANDS_VRC_DFLT0);
2030 WREG32_SMC(CG_FTV_1, CISLANDS_VRC_DFLT1);
2031 WREG32_SMC(CG_FTV_2, CISLANDS_VRC_DFLT2);
2032 WREG32_SMC(CG_FTV_3, CISLANDS_VRC_DFLT3);
2033 WREG32_SMC(CG_FTV_4, CISLANDS_VRC_DFLT4);
2034 WREG32_SMC(CG_FTV_5, CISLANDS_VRC_DFLT5);
2035 WREG32_SMC(CG_FTV_6, CISLANDS_VRC_DFLT6);
2036 WREG32_SMC(CG_FTV_7, CISLANDS_VRC_DFLT7);
2037}
2038
2039static void ci_clear_vc(struct radeon_device *rdev)
2040{
2041 u32 tmp;
2042
2043 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
2044 tmp |= (RESET_SCLK_CNT | RESET_BUSY_CNT);
2045 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
2046
2047 WREG32_SMC(CG_FTV_0, 0);
2048 WREG32_SMC(CG_FTV_1, 0);
2049 WREG32_SMC(CG_FTV_2, 0);
2050 WREG32_SMC(CG_FTV_3, 0);
2051 WREG32_SMC(CG_FTV_4, 0);
2052 WREG32_SMC(CG_FTV_5, 0);
2053 WREG32_SMC(CG_FTV_6, 0);
2054 WREG32_SMC(CG_FTV_7, 0);
2055}
2056
2057static int ci_upload_firmware(struct radeon_device *rdev)
2058{
2059 struct ci_power_info *pi = ci_get_pi(rdev);
2060 int i, ret;
2061
2062 for (i = 0; i < rdev->usec_timeout; i++) {
2063 if (RREG32_SMC(RCU_UC_EVENTS) & BOOT_SEQ_DONE)
2064 break;
2065 }
2066 WREG32_SMC(SMC_SYSCON_MISC_CNTL, 1);
2067
2068 ci_stop_smc_clock(rdev);
2069 ci_reset_smc(rdev);
2070
2071 ret = ci_load_smc_ucode(rdev, pi->sram_end);
2072
2073 return ret;
2074
2075}
2076
2077static int ci_get_svi2_voltage_table(struct radeon_device *rdev,
2078 struct radeon_clock_voltage_dependency_table *voltage_dependency_table,
2079 struct atom_voltage_table *voltage_table)
2080{
2081 u32 i;
2082
2083 if (voltage_dependency_table == NULL)
2084 return -EINVAL;
2085
2086 voltage_table->mask_low = 0;
2087 voltage_table->phase_delay = 0;
2088
2089 voltage_table->count = voltage_dependency_table->count;
2090 for (i = 0; i < voltage_table->count; i++) {
2091 voltage_table->entries[i].value = voltage_dependency_table->entries[i].v;
2092 voltage_table->entries[i].smio_low = 0;
2093 }
2094
2095 return 0;
2096}
2097
2098static int ci_construct_voltage_tables(struct radeon_device *rdev)
2099{
2100 struct ci_power_info *pi = ci_get_pi(rdev);
2101 int ret;
2102
2103 if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
2104 ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_VDDC,
2105 VOLTAGE_OBJ_GPIO_LUT,
2106 &pi->vddc_voltage_table);
2107 if (ret)
2108 return ret;
2109 } else if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
2110 ret = ci_get_svi2_voltage_table(rdev,
2111 &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
2112 &pi->vddc_voltage_table);
2113 if (ret)
2114 return ret;
2115 }
2116
2117 if (pi->vddc_voltage_table.count > SMU7_MAX_LEVELS_VDDC)
2118 si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_VDDC,
2119 &pi->vddc_voltage_table);
2120
2121 if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
2122 ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_VDDCI,
2123 VOLTAGE_OBJ_GPIO_LUT,
2124 &pi->vddci_voltage_table);
2125 if (ret)
2126 return ret;
2127 } else if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
2128 ret = ci_get_svi2_voltage_table(rdev,
2129 &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
2130 &pi->vddci_voltage_table);
2131 if (ret)
2132 return ret;
2133 }
2134
2135 if (pi->vddci_voltage_table.count > SMU7_MAX_LEVELS_VDDCI)
2136 si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_VDDCI,
2137 &pi->vddci_voltage_table);
2138
2139 if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
2140 ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_MVDDC,
2141 VOLTAGE_OBJ_GPIO_LUT,
2142 &pi->mvdd_voltage_table);
2143 if (ret)
2144 return ret;
2145 } else if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
2146 ret = ci_get_svi2_voltage_table(rdev,
2147 &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk,
2148 &pi->mvdd_voltage_table);
2149 if (ret)
2150 return ret;
2151 }
2152
2153 if (pi->mvdd_voltage_table.count > SMU7_MAX_LEVELS_MVDD)
2154 si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_MVDD,
2155 &pi->mvdd_voltage_table);
2156
2157 return 0;
2158}
2159
2160static void ci_populate_smc_voltage_table(struct radeon_device *rdev,
2161 struct atom_voltage_table_entry *voltage_table,
2162 SMU7_Discrete_VoltageLevel *smc_voltage_table)
2163{
2164 int ret;
2165
2166 ret = ci_get_std_voltage_value_sidd(rdev, voltage_table,
2167 &smc_voltage_table->StdVoltageHiSidd,
2168 &smc_voltage_table->StdVoltageLoSidd);
2169
2170 if (ret) {
2171 smc_voltage_table->StdVoltageHiSidd = voltage_table->value * VOLTAGE_SCALE;
2172 smc_voltage_table->StdVoltageLoSidd = voltage_table->value * VOLTAGE_SCALE;
2173 }
2174
2175 smc_voltage_table->Voltage = cpu_to_be16(voltage_table->value * VOLTAGE_SCALE);
2176 smc_voltage_table->StdVoltageHiSidd =
2177 cpu_to_be16(smc_voltage_table->StdVoltageHiSidd);
2178 smc_voltage_table->StdVoltageLoSidd =
2179 cpu_to_be16(smc_voltage_table->StdVoltageLoSidd);
2180}
2181
2182static int ci_populate_smc_vddc_table(struct radeon_device *rdev,
2183 SMU7_Discrete_DpmTable *table)
2184{
2185 struct ci_power_info *pi = ci_get_pi(rdev);
2186 unsigned int count;
2187
2188 table->VddcLevelCount = pi->vddc_voltage_table.count;
2189 for (count = 0; count < table->VddcLevelCount; count++) {
2190 ci_populate_smc_voltage_table(rdev,
2191 &pi->vddc_voltage_table.entries[count],
2192 &table->VddcLevel[count]);
2193
2194 if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
2195 table->VddcLevel[count].Smio |=
2196 pi->vddc_voltage_table.entries[count].smio_low;
2197 else
2198 table->VddcLevel[count].Smio = 0;
2199 }
2200 table->VddcLevelCount = cpu_to_be32(table->VddcLevelCount);
2201
2202 return 0;
2203}
2204
2205static int ci_populate_smc_vddci_table(struct radeon_device *rdev,
2206 SMU7_Discrete_DpmTable *table)
2207{
2208 unsigned int count;
2209 struct ci_power_info *pi = ci_get_pi(rdev);
2210
2211 table->VddciLevelCount = pi->vddci_voltage_table.count;
2212 for (count = 0; count < table->VddciLevelCount; count++) {
2213 ci_populate_smc_voltage_table(rdev,
2214 &pi->vddci_voltage_table.entries[count],
2215 &table->VddciLevel[count]);
2216
2217 if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
2218 table->VddciLevel[count].Smio |=
2219 pi->vddci_voltage_table.entries[count].smio_low;
2220 else
2221 table->VddciLevel[count].Smio = 0;
2222 }
2223 table->VddciLevelCount = cpu_to_be32(table->VddciLevelCount);
2224
2225 return 0;
2226}
2227
2228static int ci_populate_smc_mvdd_table(struct radeon_device *rdev,
2229 SMU7_Discrete_DpmTable *table)
2230{
2231 struct ci_power_info *pi = ci_get_pi(rdev);
2232 unsigned int count;
2233
2234 table->MvddLevelCount = pi->mvdd_voltage_table.count;
2235 for (count = 0; count < table->MvddLevelCount; count++) {
2236 ci_populate_smc_voltage_table(rdev,
2237 &pi->mvdd_voltage_table.entries[count],
2238 &table->MvddLevel[count]);
2239
2240 if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
2241 table->MvddLevel[count].Smio |=
2242 pi->mvdd_voltage_table.entries[count].smio_low;
2243 else
2244 table->MvddLevel[count].Smio = 0;
2245 }
2246 table->MvddLevelCount = cpu_to_be32(table->MvddLevelCount);
2247
2248 return 0;
2249}
2250
2251static int ci_populate_smc_voltage_tables(struct radeon_device *rdev,
2252 SMU7_Discrete_DpmTable *table)
2253{
2254 int ret;
2255
2256 ret = ci_populate_smc_vddc_table(rdev, table);
2257 if (ret)
2258 return ret;
2259
2260 ret = ci_populate_smc_vddci_table(rdev, table);
2261 if (ret)
2262 return ret;
2263
2264 ret = ci_populate_smc_mvdd_table(rdev, table);
2265 if (ret)
2266 return ret;
2267
2268 return 0;
2269}
2270
2271static int ci_populate_mvdd_value(struct radeon_device *rdev, u32 mclk,
2272 SMU7_Discrete_VoltageLevel *voltage)
2273{
2274 struct ci_power_info *pi = ci_get_pi(rdev);
2275 u32 i = 0;
2276
2277 if (pi->mvdd_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
2278 for (i = 0; i < rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.count; i++) {
2279 if (mclk <= rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.entries[i].clk) {
2280 voltage->Voltage = pi->mvdd_voltage_table.entries[i].value;
2281 break;
2282 }
2283 }
2284
2285 if (i >= rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.count)
2286 return -EINVAL;
2287 }
2288
2289 return -EINVAL;
2290}
2291
2292static int ci_get_std_voltage_value_sidd(struct radeon_device *rdev,
2293 struct atom_voltage_table_entry *voltage_table,
2294 u16 *std_voltage_hi_sidd, u16 *std_voltage_lo_sidd)
2295{
2296 u16 v_index, idx;
2297 bool voltage_found = false;
2298 *std_voltage_hi_sidd = voltage_table->value * VOLTAGE_SCALE;
2299 *std_voltage_lo_sidd = voltage_table->value * VOLTAGE_SCALE;
2300
2301 if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries == NULL)
2302 return -EINVAL;
2303
2304 if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries) {
2305 for (v_index = 0; (u32)v_index < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; v_index++) {
2306 if (voltage_table->value ==
2307 rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[v_index].v) {
2308 voltage_found = true;
2309 if ((u32)v_index < rdev->pm.dpm.dyn_state.cac_leakage_table.count)
2310 idx = v_index;
2311 else
2312 idx = rdev->pm.dpm.dyn_state.cac_leakage_table.count - 1;
2313 *std_voltage_lo_sidd =
2314 rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].vddc * VOLTAGE_SCALE;
2315 *std_voltage_hi_sidd =
2316 rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].leakage * VOLTAGE_SCALE;
2317 break;
2318 }
2319 }
2320
2321 if (!voltage_found) {
2322 for (v_index = 0; (u32)v_index < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; v_index++) {
2323 if (voltage_table->value <=
2324 rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[v_index].v) {
2325 voltage_found = true;
2326 if ((u32)v_index < rdev->pm.dpm.dyn_state.cac_leakage_table.count)
2327 idx = v_index;
2328 else
2329 idx = rdev->pm.dpm.dyn_state.cac_leakage_table.count - 1;
2330 *std_voltage_lo_sidd =
2331 rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].vddc * VOLTAGE_SCALE;
2332 *std_voltage_hi_sidd =
2333 rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].leakage * VOLTAGE_SCALE;
2334 break;
2335 }
2336 }
2337 }
2338 }
2339
2340 return 0;
2341}
2342
2343static void ci_populate_phase_value_based_on_sclk(struct radeon_device *rdev,
2344 const struct radeon_phase_shedding_limits_table *limits,
2345 u32 sclk,
2346 u32 *phase_shedding)
2347{
2348 unsigned int i;
2349
2350 *phase_shedding = 1;
2351
2352 for (i = 0; i < limits->count; i++) {
2353 if (sclk < limits->entries[i].sclk) {
2354 *phase_shedding = i;
2355 break;
2356 }
2357 }
2358}
2359
2360static void ci_populate_phase_value_based_on_mclk(struct radeon_device *rdev,
2361 const struct radeon_phase_shedding_limits_table *limits,
2362 u32 mclk,
2363 u32 *phase_shedding)
2364{
2365 unsigned int i;
2366
2367 *phase_shedding = 1;
2368
2369 for (i = 0; i < limits->count; i++) {
2370 if (mclk < limits->entries[i].mclk) {
2371 *phase_shedding = i;
2372 break;
2373 }
2374 }
2375}
2376
2377static int ci_init_arb_table_index(struct radeon_device *rdev)
2378{
2379 struct ci_power_info *pi = ci_get_pi(rdev);
2380 u32 tmp;
2381 int ret;
2382
2383 ret = ci_read_smc_sram_dword(rdev, pi->arb_table_start,
2384 &tmp, pi->sram_end);
2385 if (ret)
2386 return ret;
2387
2388 tmp &= 0x00FFFFFF;
2389 tmp |= MC_CG_ARB_FREQ_F1 << 24;
2390
2391 return ci_write_smc_sram_dword(rdev, pi->arb_table_start,
2392 tmp, pi->sram_end);
2393}
2394
2395static int ci_get_dependency_volt_by_clk(struct radeon_device *rdev,
2396 struct radeon_clock_voltage_dependency_table *allowed_clock_voltage_table,
2397 u32 clock, u32 *voltage)
2398{
2399 u32 i = 0;
2400
2401 if (allowed_clock_voltage_table->count == 0)
2402 return -EINVAL;
2403
2404 for (i = 0; i < allowed_clock_voltage_table->count; i++) {
2405 if (allowed_clock_voltage_table->entries[i].clk >= clock) {
2406 *voltage = allowed_clock_voltage_table->entries[i].v;
2407 return 0;
2408 }
2409 }
2410
2411 *voltage = allowed_clock_voltage_table->entries[i-1].v;
2412
2413 return 0;
2414}
2415
2416static u8 ci_get_sleep_divider_id_from_clock(struct radeon_device *rdev,
2417 u32 sclk, u32 min_sclk_in_sr)
2418{
2419 u32 i;
2420 u32 tmp;
2421 u32 min = (min_sclk_in_sr > CISLAND_MINIMUM_ENGINE_CLOCK) ?
2422 min_sclk_in_sr : CISLAND_MINIMUM_ENGINE_CLOCK;
2423
2424 if (sclk < min)
2425 return 0;
2426
2427 for (i = CISLAND_MAX_DEEPSLEEP_DIVIDER_ID; ; i--) {
2428 tmp = sclk / (1 << i);
2429 if (tmp >= min || i == 0)
2430 break;
2431 }
2432
2433 return (u8)i;
2434}
2435
2436static int ci_initial_switch_from_arb_f0_to_f1(struct radeon_device *rdev)
2437{
2438 return ni_copy_and_switch_arb_sets(rdev, MC_CG_ARB_FREQ_F0, MC_CG_ARB_FREQ_F1);
2439}
2440
2441static int ci_reset_to_default(struct radeon_device *rdev)
2442{
2443 return (ci_send_msg_to_smc(rdev, PPSMC_MSG_ResetToDefaults) == PPSMC_Result_OK) ?
2444 0 : -EINVAL;
2445}
2446
2447static int ci_force_switch_to_arb_f0(struct radeon_device *rdev)
2448{
2449 u32 tmp;
2450
2451 tmp = (RREG32_SMC(SMC_SCRATCH9) & 0x0000ff00) >> 8;
2452
2453 if (tmp == MC_CG_ARB_FREQ_F0)
2454 return 0;
2455
2456 return ni_copy_and_switch_arb_sets(rdev, tmp, MC_CG_ARB_FREQ_F0);
2457}
2458
2459static void ci_register_patching_mc_arb(struct radeon_device *rdev,
2460 const u32 engine_clock,
2461 const u32 memory_clock,
2462 u32 *dram_timimg2)
2463{
2464 bool patch;
2465 u32 tmp, tmp2;
2466
2467 tmp = RREG32(MC_SEQ_MISC0);
2468 patch = ((tmp & 0x0000f00) == 0x300) ? true : false;
2469
2470 if (patch &&
2471 ((rdev->pdev->device == 0x67B0) ||
2472 (rdev->pdev->device == 0x67B1))) {
2473 if ((memory_clock > 100000) && (memory_clock <= 125000)) {
2474 tmp2 = (((0x31 * engine_clock) / 125000) - 1) & 0xff;
2475 *dram_timimg2 &= ~0x00ff0000;
2476 *dram_timimg2 |= tmp2 << 16;
2477 } else if ((memory_clock > 125000) && (memory_clock <= 137500)) {
2478 tmp2 = (((0x36 * engine_clock) / 137500) - 1) & 0xff;
2479 *dram_timimg2 &= ~0x00ff0000;
2480 *dram_timimg2 |= tmp2 << 16;
2481 }
2482 }
2483}
2484
2485
2486static int ci_populate_memory_timing_parameters(struct radeon_device *rdev,
2487 u32 sclk,
2488 u32 mclk,
2489 SMU7_Discrete_MCArbDramTimingTableEntry *arb_regs)
2490{
2491 u32 dram_timing;
2492 u32 dram_timing2;
2493 u32 burst_time;
2494
2495 radeon_atom_set_engine_dram_timings(rdev, sclk, mclk);
2496
2497 dram_timing = RREG32(MC_ARB_DRAM_TIMING);
2498 dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2);
2499 burst_time = RREG32(MC_ARB_BURST_TIME) & STATE0_MASK;
2500
2501 ci_register_patching_mc_arb(rdev, sclk, mclk, &dram_timing2);
2502
2503 arb_regs->McArbDramTiming = cpu_to_be32(dram_timing);
2504 arb_regs->McArbDramTiming2 = cpu_to_be32(dram_timing2);
2505 arb_regs->McArbBurstTime = (u8)burst_time;
2506
2507 return 0;
2508}
2509
2510static int ci_do_program_memory_timing_parameters(struct radeon_device *rdev)
2511{
2512 struct ci_power_info *pi = ci_get_pi(rdev);
2513 SMU7_Discrete_MCArbDramTimingTable arb_regs;
2514 u32 i, j;
2515 int ret = 0;
2516
2517 memset(&arb_regs, 0, sizeof(SMU7_Discrete_MCArbDramTimingTable));
2518
2519 for (i = 0; i < pi->dpm_table.sclk_table.count; i++) {
2520 for (j = 0; j < pi->dpm_table.mclk_table.count; j++) {
2521 ret = ci_populate_memory_timing_parameters(rdev,
2522 pi->dpm_table.sclk_table.dpm_levels[i].value,
2523 pi->dpm_table.mclk_table.dpm_levels[j].value,
2524 &arb_regs.entries[i][j]);
2525 if (ret)
2526 break;
2527 }
2528 }
2529
2530 if (ret == 0)
2531 ret = ci_copy_bytes_to_smc(rdev,
2532 pi->arb_table_start,
2533 (u8 *)&arb_regs,
2534 sizeof(SMU7_Discrete_MCArbDramTimingTable),
2535 pi->sram_end);
2536
2537 return ret;
2538}
2539
2540static int ci_program_memory_timing_parameters(struct radeon_device *rdev)
2541{
2542 struct ci_power_info *pi = ci_get_pi(rdev);
2543
2544 if (pi->need_update_smu7_dpm_table == 0)
2545 return 0;
2546
2547 return ci_do_program_memory_timing_parameters(rdev);
2548}
2549
2550static void ci_populate_smc_initial_state(struct radeon_device *rdev,
2551 struct radeon_ps *radeon_boot_state)
2552{
2553 struct ci_ps *boot_state = ci_get_ps(radeon_boot_state);
2554 struct ci_power_info *pi = ci_get_pi(rdev);
2555 u32 level = 0;
2556
2557 for (level = 0; level < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; level++) {
2558 if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[level].clk >=
2559 boot_state->performance_levels[0].sclk) {
2560 pi->smc_state_table.GraphicsBootLevel = level;
2561 break;
2562 }
2563 }
2564
2565 for (level = 0; level < rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.count; level++) {
2566 if (rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.entries[level].clk >=
2567 boot_state->performance_levels[0].mclk) {
2568 pi->smc_state_table.MemoryBootLevel = level;
2569 break;
2570 }
2571 }
2572}
2573
2574static u32 ci_get_dpm_level_enable_mask_value(struct ci_single_dpm_table *dpm_table)
2575{
2576 u32 i;
2577 u32 mask_value = 0;
2578
2579 for (i = dpm_table->count; i > 0; i--) {
2580 mask_value = mask_value << 1;
2581 if (dpm_table->dpm_levels[i-1].enabled)
2582 mask_value |= 0x1;
2583 else
2584 mask_value &= 0xFFFFFFFE;
2585 }
2586
2587 return mask_value;
2588}
2589
2590static void ci_populate_smc_link_level(struct radeon_device *rdev,
2591 SMU7_Discrete_DpmTable *table)
2592{
2593 struct ci_power_info *pi = ci_get_pi(rdev);
2594 struct ci_dpm_table *dpm_table = &pi->dpm_table;
2595 u32 i;
2596
2597 for (i = 0; i < dpm_table->pcie_speed_table.count; i++) {
2598 table->LinkLevel[i].PcieGenSpeed =
2599 (u8)dpm_table->pcie_speed_table.dpm_levels[i].value;
2600 table->LinkLevel[i].PcieLaneCount =
2601 r600_encode_pci_lane_width(dpm_table->pcie_speed_table.dpm_levels[i].param1);
2602 table->LinkLevel[i].EnabledForActivity = 1;
2603 table->LinkLevel[i].DownT = cpu_to_be32(5);
2604 table->LinkLevel[i].UpT = cpu_to_be32(30);
2605 }
2606
2607 pi->smc_state_table.LinkLevelCount = (u8)dpm_table->pcie_speed_table.count;
2608 pi->dpm_level_enable_mask.pcie_dpm_enable_mask =
2609 ci_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
2610}
2611
2612static int ci_populate_smc_uvd_level(struct radeon_device *rdev,
2613 SMU7_Discrete_DpmTable *table)
2614{
2615 u32 count;
2616 struct atom_clock_dividers dividers;
2617 int ret = -EINVAL;
2618
2619 table->UvdLevelCount =
2620 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count;
2621
2622 for (count = 0; count < table->UvdLevelCount; count++) {
2623 table->UvdLevel[count].VclkFrequency =
2624 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].vclk;
2625 table->UvdLevel[count].DclkFrequency =
2626 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].dclk;
2627 table->UvdLevel[count].MinVddc =
2628 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
2629 table->UvdLevel[count].MinVddcPhases = 1;
2630
2631 ret = radeon_atom_get_clock_dividers(rdev,
2632 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2633 table->UvdLevel[count].VclkFrequency, false, ÷rs);
2634 if (ret)
2635 return ret;
2636
2637 table->UvdLevel[count].VclkDivider = (u8)dividers.post_divider;
2638
2639 ret = radeon_atom_get_clock_dividers(rdev,
2640 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2641 table->UvdLevel[count].DclkFrequency, false, ÷rs);
2642 if (ret)
2643 return ret;
2644
2645 table->UvdLevel[count].DclkDivider = (u8)dividers.post_divider;
2646
2647 table->UvdLevel[count].VclkFrequency = cpu_to_be32(table->UvdLevel[count].VclkFrequency);
2648 table->UvdLevel[count].DclkFrequency = cpu_to_be32(table->UvdLevel[count].DclkFrequency);
2649 table->UvdLevel[count].MinVddc = cpu_to_be16(table->UvdLevel[count].MinVddc);
2650 }
2651
2652 return ret;
2653}
2654
2655static int ci_populate_smc_vce_level(struct radeon_device *rdev,
2656 SMU7_Discrete_DpmTable *table)
2657{
2658 u32 count;
2659 struct atom_clock_dividers dividers;
2660 int ret = -EINVAL;
2661
2662 table->VceLevelCount =
2663 rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.count;
2664
2665 for (count = 0; count < table->VceLevelCount; count++) {
2666 table->VceLevel[count].Frequency =
2667 rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[count].evclk;
2668 table->VceLevel[count].MinVoltage =
2669 (u16)rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
2670 table->VceLevel[count].MinPhases = 1;
2671
2672 ret = radeon_atom_get_clock_dividers(rdev,
2673 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2674 table->VceLevel[count].Frequency, false, ÷rs);
2675 if (ret)
2676 return ret;
2677
2678 table->VceLevel[count].Divider = (u8)dividers.post_divider;
2679
2680 table->VceLevel[count].Frequency = cpu_to_be32(table->VceLevel[count].Frequency);
2681 table->VceLevel[count].MinVoltage = cpu_to_be16(table->VceLevel[count].MinVoltage);
2682 }
2683
2684 return ret;
2685
2686}
2687
2688static int ci_populate_smc_acp_level(struct radeon_device *rdev,
2689 SMU7_Discrete_DpmTable *table)
2690{
2691 u32 count;
2692 struct atom_clock_dividers dividers;
2693 int ret = -EINVAL;
2694
2695 table->AcpLevelCount = (u8)
2696 (rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.count);
2697
2698 for (count = 0; count < table->AcpLevelCount; count++) {
2699 table->AcpLevel[count].Frequency =
2700 rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[count].clk;
2701 table->AcpLevel[count].MinVoltage =
2702 rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[count].v;
2703 table->AcpLevel[count].MinPhases = 1;
2704
2705 ret = radeon_atom_get_clock_dividers(rdev,
2706 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2707 table->AcpLevel[count].Frequency, false, ÷rs);
2708 if (ret)
2709 return ret;
2710
2711 table->AcpLevel[count].Divider = (u8)dividers.post_divider;
2712
2713 table->AcpLevel[count].Frequency = cpu_to_be32(table->AcpLevel[count].Frequency);
2714 table->AcpLevel[count].MinVoltage = cpu_to_be16(table->AcpLevel[count].MinVoltage);
2715 }
2716
2717 return ret;
2718}
2719
2720static int ci_populate_smc_samu_level(struct radeon_device *rdev,
2721 SMU7_Discrete_DpmTable *table)
2722{
2723 u32 count;
2724 struct atom_clock_dividers dividers;
2725 int ret = -EINVAL;
2726
2727 table->SamuLevelCount =
2728 rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.count;
2729
2730 for (count = 0; count < table->SamuLevelCount; count++) {
2731 table->SamuLevel[count].Frequency =
2732 rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[count].clk;
2733 table->SamuLevel[count].MinVoltage =
2734 rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
2735 table->SamuLevel[count].MinPhases = 1;
2736
2737 ret = radeon_atom_get_clock_dividers(rdev,
2738 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2739 table->SamuLevel[count].Frequency, false, ÷rs);
2740 if (ret)
2741 return ret;
2742
2743 table->SamuLevel[count].Divider = (u8)dividers.post_divider;
2744
2745 table->SamuLevel[count].Frequency = cpu_to_be32(table->SamuLevel[count].Frequency);
2746 table->SamuLevel[count].MinVoltage = cpu_to_be16(table->SamuLevel[count].MinVoltage);
2747 }
2748
2749 return ret;
2750}
2751
2752static int ci_calculate_mclk_params(struct radeon_device *rdev,
2753 u32 memory_clock,
2754 SMU7_Discrete_MemoryLevel *mclk,
2755 bool strobe_mode,
2756 bool dll_state_on)
2757{
2758 struct ci_power_info *pi = ci_get_pi(rdev);
2759 u32 dll_cntl = pi->clock_registers.dll_cntl;
2760 u32 mclk_pwrmgt_cntl = pi->clock_registers.mclk_pwrmgt_cntl;
2761 u32 mpll_ad_func_cntl = pi->clock_registers.mpll_ad_func_cntl;
2762 u32 mpll_dq_func_cntl = pi->clock_registers.mpll_dq_func_cntl;
2763 u32 mpll_func_cntl = pi->clock_registers.mpll_func_cntl;
2764 u32 mpll_func_cntl_1 = pi->clock_registers.mpll_func_cntl_1;
2765 u32 mpll_func_cntl_2 = pi->clock_registers.mpll_func_cntl_2;
2766 u32 mpll_ss1 = pi->clock_registers.mpll_ss1;
2767 u32 mpll_ss2 = pi->clock_registers.mpll_ss2;
2768 struct atom_mpll_param mpll_param;
2769 int ret;
2770
2771 ret = radeon_atom_get_memory_pll_dividers(rdev, memory_clock, strobe_mode, &mpll_param);
2772 if (ret)
2773 return ret;
2774
2775 mpll_func_cntl &= ~BWCTRL_MASK;
2776 mpll_func_cntl |= BWCTRL(mpll_param.bwcntl);
2777
2778 mpll_func_cntl_1 &= ~(CLKF_MASK | CLKFRAC_MASK | VCO_MODE_MASK);
2779 mpll_func_cntl_1 |= CLKF(mpll_param.clkf) |
2780 CLKFRAC(mpll_param.clkfrac) | VCO_MODE(mpll_param.vco_mode);
2781
2782 mpll_ad_func_cntl &= ~YCLK_POST_DIV_MASK;
2783 mpll_ad_func_cntl |= YCLK_POST_DIV(mpll_param.post_div);
2784
2785 if (pi->mem_gddr5) {
2786 mpll_dq_func_cntl &= ~(YCLK_SEL_MASK | YCLK_POST_DIV_MASK);
2787 mpll_dq_func_cntl |= YCLK_SEL(mpll_param.yclk_sel) |
2788 YCLK_POST_DIV(mpll_param.post_div);
2789 }
2790
2791 if (pi->caps_mclk_ss_support) {
2792 struct radeon_atom_ss ss;
2793 u32 freq_nom;
2794 u32 tmp;
2795 u32 reference_clock = rdev->clock.mpll.reference_freq;
2796
2797 if (mpll_param.qdr == 1)
2798 freq_nom = memory_clock * 4 * (1 << mpll_param.post_div);
2799 else
2800 freq_nom = memory_clock * 2 * (1 << mpll_param.post_div);
2801
2802 tmp = (freq_nom / reference_clock);
2803 tmp = tmp * tmp;
2804 if (radeon_atombios_get_asic_ss_info(rdev, &ss,
2805 ASIC_INTERNAL_MEMORY_SS, freq_nom)) {
2806 u32 clks = reference_clock * 5 / ss.rate;
2807 u32 clkv = (u32)((((131 * ss.percentage * ss.rate) / 100) * tmp) / freq_nom);
2808
2809 mpll_ss1 &= ~CLKV_MASK;
2810 mpll_ss1 |= CLKV(clkv);
2811
2812 mpll_ss2 &= ~CLKS_MASK;
2813 mpll_ss2 |= CLKS(clks);
2814 }
2815 }
2816
2817 mclk_pwrmgt_cntl &= ~DLL_SPEED_MASK;
2818 mclk_pwrmgt_cntl |= DLL_SPEED(mpll_param.dll_speed);
2819
2820 if (dll_state_on)
2821 mclk_pwrmgt_cntl |= MRDCK0_PDNB | MRDCK1_PDNB;
2822 else
2823 mclk_pwrmgt_cntl &= ~(MRDCK0_PDNB | MRDCK1_PDNB);
2824
2825 mclk->MclkFrequency = memory_clock;
2826 mclk->MpllFuncCntl = mpll_func_cntl;
2827 mclk->MpllFuncCntl_1 = mpll_func_cntl_1;
2828 mclk->MpllFuncCntl_2 = mpll_func_cntl_2;
2829 mclk->MpllAdFuncCntl = mpll_ad_func_cntl;
2830 mclk->MpllDqFuncCntl = mpll_dq_func_cntl;
2831 mclk->MclkPwrmgtCntl = mclk_pwrmgt_cntl;
2832 mclk->DllCntl = dll_cntl;
2833 mclk->MpllSs1 = mpll_ss1;
2834 mclk->MpllSs2 = mpll_ss2;
2835
2836 return 0;
2837}
2838
2839static int ci_populate_single_memory_level(struct radeon_device *rdev,
2840 u32 memory_clock,
2841 SMU7_Discrete_MemoryLevel *memory_level)
2842{
2843 struct ci_power_info *pi = ci_get_pi(rdev);
2844 int ret;
2845 bool dll_state_on;
2846
2847 if (rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.entries) {
2848 ret = ci_get_dependency_volt_by_clk(rdev,
2849 &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
2850 memory_clock, &memory_level->MinVddc);
2851 if (ret)
2852 return ret;
2853 }
2854
2855 if (rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk.entries) {
2856 ret = ci_get_dependency_volt_by_clk(rdev,
2857 &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
2858 memory_clock, &memory_level->MinVddci);
2859 if (ret)
2860 return ret;
2861 }
2862
2863 if (rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.entries) {
2864 ret = ci_get_dependency_volt_by_clk(rdev,
2865 &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk,
2866 memory_clock, &memory_level->MinMvdd);
2867 if (ret)
2868 return ret;
2869 }
2870
2871 memory_level->MinVddcPhases = 1;
2872
2873 if (pi->vddc_phase_shed_control)
2874 ci_populate_phase_value_based_on_mclk(rdev,
2875 &rdev->pm.dpm.dyn_state.phase_shedding_limits_table,
2876 memory_clock,
2877 &memory_level->MinVddcPhases);
2878
2879 memory_level->EnabledForThrottle = 1;
2880 memory_level->UpH = 0;
2881 memory_level->DownH = 100;
2882 memory_level->VoltageDownH = 0;
2883 memory_level->ActivityLevel = (u16)pi->mclk_activity_target;
2884
2885 memory_level->StutterEnable = false;
2886 memory_level->StrobeEnable = false;
2887 memory_level->EdcReadEnable = false;
2888 memory_level->EdcWriteEnable = false;
2889 memory_level->RttEnable = false;
2890
2891 memory_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
2892
2893 if (pi->mclk_stutter_mode_threshold &&
2894 (memory_clock <= pi->mclk_stutter_mode_threshold) &&
2895 (pi->uvd_enabled == false) &&
2896 (RREG32(DPG_PIPE_STUTTER_CONTROL) & STUTTER_ENABLE) &&
2897 (rdev->pm.dpm.new_active_crtc_count <= 2))
2898 memory_level->StutterEnable = true;
2899
2900 if (pi->mclk_strobe_mode_threshold &&
2901 (memory_clock <= pi->mclk_strobe_mode_threshold))
2902 memory_level->StrobeEnable = 1;
2903
2904 if (pi->mem_gddr5) {
2905 memory_level->StrobeRatio =
2906 si_get_mclk_frequency_ratio(memory_clock, memory_level->StrobeEnable);
2907 if (pi->mclk_edc_enable_threshold &&
2908 (memory_clock > pi->mclk_edc_enable_threshold))
2909 memory_level->EdcReadEnable = true;
2910
2911 if (pi->mclk_edc_wr_enable_threshold &&
2912 (memory_clock > pi->mclk_edc_wr_enable_threshold))
2913 memory_level->EdcWriteEnable = true;
2914
2915 if (memory_level->StrobeEnable) {
2916 if (si_get_mclk_frequency_ratio(memory_clock, true) >=
2917 ((RREG32(MC_SEQ_MISC7) >> 16) & 0xf))
2918 dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
2919 else
2920 dll_state_on = ((RREG32(MC_SEQ_MISC6) >> 1) & 0x1) ? true : false;
2921 } else {
2922 dll_state_on = pi->dll_default_on;
2923 }
2924 } else {
2925 memory_level->StrobeRatio = si_get_ddr3_mclk_frequency_ratio(memory_clock);
2926 dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
2927 }
2928
2929 ret = ci_calculate_mclk_params(rdev, memory_clock, memory_level, memory_level->StrobeEnable, dll_state_on);
2930 if (ret)
2931 return ret;
2932
2933 memory_level->MinVddc = cpu_to_be32(memory_level->MinVddc * VOLTAGE_SCALE);
2934 memory_level->MinVddcPhases = cpu_to_be32(memory_level->MinVddcPhases);
2935 memory_level->MinVddci = cpu_to_be32(memory_level->MinVddci * VOLTAGE_SCALE);
2936 memory_level->MinMvdd = cpu_to_be32(memory_level->MinMvdd * VOLTAGE_SCALE);
2937
2938 memory_level->MclkFrequency = cpu_to_be32(memory_level->MclkFrequency);
2939 memory_level->ActivityLevel = cpu_to_be16(memory_level->ActivityLevel);
2940 memory_level->MpllFuncCntl = cpu_to_be32(memory_level->MpllFuncCntl);
2941 memory_level->MpllFuncCntl_1 = cpu_to_be32(memory_level->MpllFuncCntl_1);
2942 memory_level->MpllFuncCntl_2 = cpu_to_be32(memory_level->MpllFuncCntl_2);
2943 memory_level->MpllAdFuncCntl = cpu_to_be32(memory_level->MpllAdFuncCntl);
2944 memory_level->MpllDqFuncCntl = cpu_to_be32(memory_level->MpllDqFuncCntl);
2945 memory_level->MclkPwrmgtCntl = cpu_to_be32(memory_level->MclkPwrmgtCntl);
2946 memory_level->DllCntl = cpu_to_be32(memory_level->DllCntl);
2947 memory_level->MpllSs1 = cpu_to_be32(memory_level->MpllSs1);
2948 memory_level->MpllSs2 = cpu_to_be32(memory_level->MpllSs2);
2949
2950 return 0;
2951}
2952
2953static int ci_populate_smc_acpi_level(struct radeon_device *rdev,
2954 SMU7_Discrete_DpmTable *table)
2955{
2956 struct ci_power_info *pi = ci_get_pi(rdev);
2957 struct atom_clock_dividers dividers;
2958 SMU7_Discrete_VoltageLevel voltage_level;
2959 u32 spll_func_cntl = pi->clock_registers.cg_spll_func_cntl;
2960 u32 spll_func_cntl_2 = pi->clock_registers.cg_spll_func_cntl_2;
2961 u32 dll_cntl = pi->clock_registers.dll_cntl;
2962 u32 mclk_pwrmgt_cntl = pi->clock_registers.mclk_pwrmgt_cntl;
2963 int ret;
2964
2965 table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
2966
2967 if (pi->acpi_vddc)
2968 table->ACPILevel.MinVddc = cpu_to_be32(pi->acpi_vddc * VOLTAGE_SCALE);
2969 else
2970 table->ACPILevel.MinVddc = cpu_to_be32(pi->min_vddc_in_pp_table * VOLTAGE_SCALE);
2971
2972 table->ACPILevel.MinVddcPhases = pi->vddc_phase_shed_control ? 0 : 1;
2973
2974 table->ACPILevel.SclkFrequency = rdev->clock.spll.reference_freq;
2975
2976 ret = radeon_atom_get_clock_dividers(rdev,
2977 COMPUTE_GPUCLK_INPUT_FLAG_SCLK,
2978 table->ACPILevel.SclkFrequency, false, ÷rs);
2979 if (ret)
2980 return ret;
2981
2982 table->ACPILevel.SclkDid = (u8)dividers.post_divider;
2983 table->ACPILevel.DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
2984 table->ACPILevel.DeepSleepDivId = 0;
2985
2986 spll_func_cntl &= ~SPLL_PWRON;
2987 spll_func_cntl |= SPLL_RESET;
2988
2989 spll_func_cntl_2 &= ~SCLK_MUX_SEL_MASK;
2990 spll_func_cntl_2 |= SCLK_MUX_SEL(4);
2991
2992 table->ACPILevel.CgSpllFuncCntl = spll_func_cntl;
2993 table->ACPILevel.CgSpllFuncCntl2 = spll_func_cntl_2;
2994 table->ACPILevel.CgSpllFuncCntl3 = pi->clock_registers.cg_spll_func_cntl_3;
2995 table->ACPILevel.CgSpllFuncCntl4 = pi->clock_registers.cg_spll_func_cntl_4;
2996 table->ACPILevel.SpllSpreadSpectrum = pi->clock_registers.cg_spll_spread_spectrum;
2997 table->ACPILevel.SpllSpreadSpectrum2 = pi->clock_registers.cg_spll_spread_spectrum_2;
2998 table->ACPILevel.CcPwrDynRm = 0;
2999 table->ACPILevel.CcPwrDynRm1 = 0;
3000
3001 table->ACPILevel.Flags = cpu_to_be32(table->ACPILevel.Flags);
3002 table->ACPILevel.MinVddcPhases = cpu_to_be32(table->ACPILevel.MinVddcPhases);
3003 table->ACPILevel.SclkFrequency = cpu_to_be32(table->ACPILevel.SclkFrequency);
3004 table->ACPILevel.CgSpllFuncCntl = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl);
3005 table->ACPILevel.CgSpllFuncCntl2 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl2);
3006 table->ACPILevel.CgSpllFuncCntl3 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl3);
3007 table->ACPILevel.CgSpllFuncCntl4 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl4);
3008 table->ACPILevel.SpllSpreadSpectrum = cpu_to_be32(table->ACPILevel.SpllSpreadSpectrum);
3009 table->ACPILevel.SpllSpreadSpectrum2 = cpu_to_be32(table->ACPILevel.SpllSpreadSpectrum2);
3010 table->ACPILevel.CcPwrDynRm = cpu_to_be32(table->ACPILevel.CcPwrDynRm);
3011 table->ACPILevel.CcPwrDynRm1 = cpu_to_be32(table->ACPILevel.CcPwrDynRm1);
3012
3013 table->MemoryACPILevel.MinVddc = table->ACPILevel.MinVddc;
3014 table->MemoryACPILevel.MinVddcPhases = table->ACPILevel.MinVddcPhases;
3015
3016 if (pi->vddci_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
3017 if (pi->acpi_vddci)
3018 table->MemoryACPILevel.MinVddci =
3019 cpu_to_be32(pi->acpi_vddci * VOLTAGE_SCALE);
3020 else
3021 table->MemoryACPILevel.MinVddci =
3022 cpu_to_be32(pi->min_vddci_in_pp_table * VOLTAGE_SCALE);
3023 }
3024
3025 if (ci_populate_mvdd_value(rdev, 0, &voltage_level))
3026 table->MemoryACPILevel.MinMvdd = 0;
3027 else
3028 table->MemoryACPILevel.MinMvdd =
3029 cpu_to_be32(voltage_level.Voltage * VOLTAGE_SCALE);
3030
3031 mclk_pwrmgt_cntl |= MRDCK0_RESET | MRDCK1_RESET;
3032 mclk_pwrmgt_cntl &= ~(MRDCK0_PDNB | MRDCK1_PDNB);
3033
3034 dll_cntl &= ~(MRDCK0_BYPASS | MRDCK1_BYPASS);
3035
3036 table->MemoryACPILevel.DllCntl = cpu_to_be32(dll_cntl);
3037 table->MemoryACPILevel.MclkPwrmgtCntl = cpu_to_be32(mclk_pwrmgt_cntl);
3038 table->MemoryACPILevel.MpllAdFuncCntl =
3039 cpu_to_be32(pi->clock_registers.mpll_ad_func_cntl);
3040 table->MemoryACPILevel.MpllDqFuncCntl =
3041 cpu_to_be32(pi->clock_registers.mpll_dq_func_cntl);
3042 table->MemoryACPILevel.MpllFuncCntl =
3043 cpu_to_be32(pi->clock_registers.mpll_func_cntl);
3044 table->MemoryACPILevel.MpllFuncCntl_1 =
3045 cpu_to_be32(pi->clock_registers.mpll_func_cntl_1);
3046 table->MemoryACPILevel.MpllFuncCntl_2 =
3047 cpu_to_be32(pi->clock_registers.mpll_func_cntl_2);
3048 table->MemoryACPILevel.MpllSs1 = cpu_to_be32(pi->clock_registers.mpll_ss1);
3049 table->MemoryACPILevel.MpllSs2 = cpu_to_be32(pi->clock_registers.mpll_ss2);
3050
3051 table->MemoryACPILevel.EnabledForThrottle = 0;
3052 table->MemoryACPILevel.EnabledForActivity = 0;
3053 table->MemoryACPILevel.UpH = 0;
3054 table->MemoryACPILevel.DownH = 100;
3055 table->MemoryACPILevel.VoltageDownH = 0;
3056 table->MemoryACPILevel.ActivityLevel =
3057 cpu_to_be16((u16)pi->mclk_activity_target);
3058
3059 table->MemoryACPILevel.StutterEnable = false;
3060 table->MemoryACPILevel.StrobeEnable = false;
3061 table->MemoryACPILevel.EdcReadEnable = false;
3062 table->MemoryACPILevel.EdcWriteEnable = false;
3063 table->MemoryACPILevel.RttEnable = false;
3064
3065 return 0;
3066}
3067
3068
3069static int ci_enable_ulv(struct radeon_device *rdev, bool enable)
3070{
3071 struct ci_power_info *pi = ci_get_pi(rdev);
3072 struct ci_ulv_parm *ulv = &pi->ulv;
3073
3074 if (ulv->supported) {
3075 if (enable)
3076 return (ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableULV) == PPSMC_Result_OK) ?
3077 0 : -EINVAL;
3078 else
3079 return (ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableULV) == PPSMC_Result_OK) ?
3080 0 : -EINVAL;
3081 }
3082
3083 return 0;
3084}
3085
3086static int ci_populate_ulv_level(struct radeon_device *rdev,
3087 SMU7_Discrete_Ulv *state)
3088{
3089 struct ci_power_info *pi = ci_get_pi(rdev);
3090 u16 ulv_voltage = rdev->pm.dpm.backbias_response_time;
3091
3092 state->CcPwrDynRm = 0;
3093 state->CcPwrDynRm1 = 0;
3094
3095 if (ulv_voltage == 0) {
3096 pi->ulv.supported = false;
3097 return 0;
3098 }
3099
3100 if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
3101 if (ulv_voltage > rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v)
3102 state->VddcOffset = 0;
3103 else
3104 state->VddcOffset =
3105 rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v - ulv_voltage;
3106 } else {
3107 if (ulv_voltage > rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v)
3108 state->VddcOffsetVid = 0;
3109 else
3110 state->VddcOffsetVid = (u8)
3111 ((rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v - ulv_voltage) *
3112 VOLTAGE_VID_OFFSET_SCALE2 / VOLTAGE_VID_OFFSET_SCALE1);
3113 }
3114 state->VddcPhase = pi->vddc_phase_shed_control ? 0 : 1;
3115
3116 state->CcPwrDynRm = cpu_to_be32(state->CcPwrDynRm);
3117 state->CcPwrDynRm1 = cpu_to_be32(state->CcPwrDynRm1);
3118 state->VddcOffset = cpu_to_be16(state->VddcOffset);
3119
3120 return 0;
3121}
3122
3123static int ci_calculate_sclk_params(struct radeon_device *rdev,
3124 u32 engine_clock,
3125 SMU7_Discrete_GraphicsLevel *sclk)
3126{
3127 struct ci_power_info *pi = ci_get_pi(rdev);
3128 struct atom_clock_dividers dividers;
3129 u32 spll_func_cntl_3 = pi->clock_registers.cg_spll_func_cntl_3;
3130 u32 spll_func_cntl_4 = pi->clock_registers.cg_spll_func_cntl_4;
3131 u32 cg_spll_spread_spectrum = pi->clock_registers.cg_spll_spread_spectrum;
3132 u32 cg_spll_spread_spectrum_2 = pi->clock_registers.cg_spll_spread_spectrum_2;
3133 u32 reference_clock = rdev->clock.spll.reference_freq;
3134 u32 reference_divider;
3135 u32 fbdiv;
3136 int ret;
3137
3138 ret = radeon_atom_get_clock_dividers(rdev,
3139 COMPUTE_GPUCLK_INPUT_FLAG_SCLK,
3140 engine_clock, false, ÷rs);
3141 if (ret)
3142 return ret;
3143
3144 reference_divider = 1 + dividers.ref_div;
3145 fbdiv = dividers.fb_div & 0x3FFFFFF;
3146
3147 spll_func_cntl_3 &= ~SPLL_FB_DIV_MASK;
3148 spll_func_cntl_3 |= SPLL_FB_DIV(fbdiv);
3149 spll_func_cntl_3 |= SPLL_DITHEN;
3150
3151 if (pi->caps_sclk_ss_support) {
3152 struct radeon_atom_ss ss;
3153 u32 vco_freq = engine_clock * dividers.post_div;
3154
3155 if (radeon_atombios_get_asic_ss_info(rdev, &ss,
3156 ASIC_INTERNAL_ENGINE_SS, vco_freq)) {
3157 u32 clk_s = reference_clock * 5 / (reference_divider * ss.rate);
3158 u32 clk_v = 4 * ss.percentage * fbdiv / (clk_s * 10000);
3159
3160 cg_spll_spread_spectrum &= ~CLK_S_MASK;
3161 cg_spll_spread_spectrum |= CLK_S(clk_s);
3162 cg_spll_spread_spectrum |= SSEN;
3163
3164 cg_spll_spread_spectrum_2 &= ~CLK_V_MASK;
3165 cg_spll_spread_spectrum_2 |= CLK_V(clk_v);
3166 }
3167 }
3168
3169 sclk->SclkFrequency = engine_clock;
3170 sclk->CgSpllFuncCntl3 = spll_func_cntl_3;
3171 sclk->CgSpllFuncCntl4 = spll_func_cntl_4;
3172 sclk->SpllSpreadSpectrum = cg_spll_spread_spectrum;
3173 sclk->SpllSpreadSpectrum2 = cg_spll_spread_spectrum_2;
3174 sclk->SclkDid = (u8)dividers.post_divider;
3175
3176 return 0;
3177}
3178
3179static int ci_populate_single_graphic_level(struct radeon_device *rdev,
3180 u32 engine_clock,
3181 u16 sclk_activity_level_t,
3182 SMU7_Discrete_GraphicsLevel *graphic_level)
3183{
3184 struct ci_power_info *pi = ci_get_pi(rdev);
3185 int ret;
3186
3187 ret = ci_calculate_sclk_params(rdev, engine_clock, graphic_level);
3188 if (ret)
3189 return ret;
3190
3191 ret = ci_get_dependency_volt_by_clk(rdev,
3192 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk,
3193 engine_clock, &graphic_level->MinVddc);
3194 if (ret)
3195 return ret;
3196
3197 graphic_level->SclkFrequency = engine_clock;
3198
3199 graphic_level->Flags = 0;
3200 graphic_level->MinVddcPhases = 1;
3201
3202 if (pi->vddc_phase_shed_control)
3203 ci_populate_phase_value_based_on_sclk(rdev,
3204 &rdev->pm.dpm.dyn_state.phase_shedding_limits_table,
3205 engine_clock,
3206 &graphic_level->MinVddcPhases);
3207
3208 graphic_level->ActivityLevel = sclk_activity_level_t;
3209
3210 graphic_level->CcPwrDynRm = 0;
3211 graphic_level->CcPwrDynRm1 = 0;
3212 graphic_level->EnabledForThrottle = 1;
3213 graphic_level->UpH = 0;
3214 graphic_level->DownH = 0;
3215 graphic_level->VoltageDownH = 0;
3216 graphic_level->PowerThrottle = 0;
3217
3218 if (pi->caps_sclk_ds)
3219 graphic_level->DeepSleepDivId = ci_get_sleep_divider_id_from_clock(rdev,
3220 engine_clock,
3221 CISLAND_MINIMUM_ENGINE_CLOCK);
3222
3223 graphic_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
3224
3225 graphic_level->Flags = cpu_to_be32(graphic_level->Flags);
3226 graphic_level->MinVddc = cpu_to_be32(graphic_level->MinVddc * VOLTAGE_SCALE);
3227 graphic_level->MinVddcPhases = cpu_to_be32(graphic_level->MinVddcPhases);
3228 graphic_level->SclkFrequency = cpu_to_be32(graphic_level->SclkFrequency);
3229 graphic_level->ActivityLevel = cpu_to_be16(graphic_level->ActivityLevel);
3230 graphic_level->CgSpllFuncCntl3 = cpu_to_be32(graphic_level->CgSpllFuncCntl3);
3231 graphic_level->CgSpllFuncCntl4 = cpu_to_be32(graphic_level->CgSpllFuncCntl4);
3232 graphic_level->SpllSpreadSpectrum = cpu_to_be32(graphic_level->SpllSpreadSpectrum);
3233 graphic_level->SpllSpreadSpectrum2 = cpu_to_be32(graphic_level->SpllSpreadSpectrum2);
3234 graphic_level->CcPwrDynRm = cpu_to_be32(graphic_level->CcPwrDynRm);
3235 graphic_level->CcPwrDynRm1 = cpu_to_be32(graphic_level->CcPwrDynRm1);
3236
3237 return 0;
3238}
3239
3240static int ci_populate_all_graphic_levels(struct radeon_device *rdev)
3241{
3242 struct ci_power_info *pi = ci_get_pi(rdev);
3243 struct ci_dpm_table *dpm_table = &pi->dpm_table;
3244 u32 level_array_address = pi->dpm_table_start +
3245 offsetof(SMU7_Discrete_DpmTable, GraphicsLevel);
3246 u32 level_array_size = sizeof(SMU7_Discrete_GraphicsLevel) *
3247 SMU7_MAX_LEVELS_GRAPHICS;
3248 SMU7_Discrete_GraphicsLevel *levels = pi->smc_state_table.GraphicsLevel;
3249 u32 i, ret;
3250
3251 memset(levels, 0, level_array_size);
3252
3253 for (i = 0; i < dpm_table->sclk_table.count; i++) {
3254 ret = ci_populate_single_graphic_level(rdev,
3255 dpm_table->sclk_table.dpm_levels[i].value,
3256 (u16)pi->activity_target[i],
3257 &pi->smc_state_table.GraphicsLevel[i]);
3258 if (ret)
3259 return ret;
3260 if (i > 1)
3261 pi->smc_state_table.GraphicsLevel[i].DeepSleepDivId = 0;
3262 if (i == (dpm_table->sclk_table.count - 1))
3263 pi->smc_state_table.GraphicsLevel[i].DisplayWatermark =
3264 PPSMC_DISPLAY_WATERMARK_HIGH;
3265 }
3266 pi->smc_state_table.GraphicsLevel[0].EnabledForActivity = 1;
3267
3268 pi->smc_state_table.GraphicsDpmLevelCount = (u8)dpm_table->sclk_table.count;
3269 pi->dpm_level_enable_mask.sclk_dpm_enable_mask =
3270 ci_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
3271
3272 ret = ci_copy_bytes_to_smc(rdev, level_array_address,
3273 (u8 *)levels, level_array_size,
3274 pi->sram_end);
3275 if (ret)
3276 return ret;
3277
3278 return 0;
3279}
3280
3281static int ci_populate_ulv_state(struct radeon_device *rdev,
3282 SMU7_Discrete_Ulv *ulv_level)
3283{
3284 return ci_populate_ulv_level(rdev, ulv_level);
3285}
3286
3287static int ci_populate_all_memory_levels(struct radeon_device *rdev)
3288{
3289 struct ci_power_info *pi = ci_get_pi(rdev);
3290 struct ci_dpm_table *dpm_table = &pi->dpm_table;
3291 u32 level_array_address = pi->dpm_table_start +
3292 offsetof(SMU7_Discrete_DpmTable, MemoryLevel);
3293 u32 level_array_size = sizeof(SMU7_Discrete_MemoryLevel) *
3294 SMU7_MAX_LEVELS_MEMORY;
3295 SMU7_Discrete_MemoryLevel *levels = pi->smc_state_table.MemoryLevel;
3296 u32 i, ret;
3297
3298 memset(levels, 0, level_array_size);
3299
3300 for (i = 0; i < dpm_table->mclk_table.count; i++) {
3301 if (dpm_table->mclk_table.dpm_levels[i].value == 0)
3302 return -EINVAL;
3303 ret = ci_populate_single_memory_level(rdev,
3304 dpm_table->mclk_table.dpm_levels[i].value,
3305 &pi->smc_state_table.MemoryLevel[i]);
3306 if (ret)
3307 return ret;
3308 }
3309
3310 pi->smc_state_table.MemoryLevel[0].EnabledForActivity = 1;
3311
3312 if ((dpm_table->mclk_table.count >= 2) &&
3313 ((rdev->pdev->device == 0x67B0) || (rdev->pdev->device == 0x67B1))) {
3314 pi->smc_state_table.MemoryLevel[1].MinVddc =
3315 pi->smc_state_table.MemoryLevel[0].MinVddc;
3316 pi->smc_state_table.MemoryLevel[1].MinVddcPhases =
3317 pi->smc_state_table.MemoryLevel[0].MinVddcPhases;
3318 }
3319
3320 pi->smc_state_table.MemoryLevel[0].ActivityLevel = cpu_to_be16(0x1F);
3321
3322 pi->smc_state_table.MemoryDpmLevelCount = (u8)dpm_table->mclk_table.count;
3323 pi->dpm_level_enable_mask.mclk_dpm_enable_mask =
3324 ci_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
3325
3326 pi->smc_state_table.MemoryLevel[dpm_table->mclk_table.count - 1].DisplayWatermark =
3327 PPSMC_DISPLAY_WATERMARK_HIGH;
3328
3329 ret = ci_copy_bytes_to_smc(rdev, level_array_address,
3330 (u8 *)levels, level_array_size,
3331 pi->sram_end);
3332 if (ret)
3333 return ret;
3334
3335 return 0;
3336}
3337
3338static void ci_reset_single_dpm_table(struct radeon_device *rdev,
3339 struct ci_single_dpm_table* dpm_table,
3340 u32 count)
3341{
3342 u32 i;
3343
3344 dpm_table->count = count;
3345 for (i = 0; i < MAX_REGULAR_DPM_NUMBER; i++)
3346 dpm_table->dpm_levels[i].enabled = false;
3347}
3348
3349static void ci_setup_pcie_table_entry(struct ci_single_dpm_table* dpm_table,
3350 u32 index, u32 pcie_gen, u32 pcie_lanes)
3351{
3352 dpm_table->dpm_levels[index].value = pcie_gen;
3353 dpm_table->dpm_levels[index].param1 = pcie_lanes;
3354 dpm_table->dpm_levels[index].enabled = true;
3355}
3356
3357static int ci_setup_default_pcie_tables(struct radeon_device *rdev)
3358{
3359 struct ci_power_info *pi = ci_get_pi(rdev);
3360
3361 if (!pi->use_pcie_performance_levels && !pi->use_pcie_powersaving_levels)
3362 return -EINVAL;
3363
3364 if (pi->use_pcie_performance_levels && !pi->use_pcie_powersaving_levels) {
3365 pi->pcie_gen_powersaving = pi->pcie_gen_performance;
3366 pi->pcie_lane_powersaving = pi->pcie_lane_performance;
3367 } else if (!pi->use_pcie_performance_levels && pi->use_pcie_powersaving_levels) {
3368 pi->pcie_gen_performance = pi->pcie_gen_powersaving;
3369 pi->pcie_lane_performance = pi->pcie_lane_powersaving;
3370 }
3371
3372 ci_reset_single_dpm_table(rdev,
3373 &pi->dpm_table.pcie_speed_table,
3374 SMU7_MAX_LEVELS_LINK);
3375
3376 if (rdev->family == CHIP_BONAIRE)
3377 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 0,
3378 pi->pcie_gen_powersaving.min,
3379 pi->pcie_lane_powersaving.max);
3380 else
3381 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 0,
3382 pi->pcie_gen_powersaving.min,
3383 pi->pcie_lane_powersaving.min);
3384 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 1,
3385 pi->pcie_gen_performance.min,
3386 pi->pcie_lane_performance.min);
3387 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 2,
3388 pi->pcie_gen_powersaving.min,
3389 pi->pcie_lane_powersaving.max);
3390 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 3,
3391 pi->pcie_gen_performance.min,
3392 pi->pcie_lane_performance.max);
3393 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 4,
3394 pi->pcie_gen_powersaving.max,
3395 pi->pcie_lane_powersaving.max);
3396 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 5,
3397 pi->pcie_gen_performance.max,
3398 pi->pcie_lane_performance.max);
3399
3400 pi->dpm_table.pcie_speed_table.count = 6;
3401
3402 return 0;
3403}
3404
3405static int ci_setup_default_dpm_tables(struct radeon_device *rdev)
3406{
3407 struct ci_power_info *pi = ci_get_pi(rdev);
3408 struct radeon_clock_voltage_dependency_table *allowed_sclk_vddc_table =
3409 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
3410 struct radeon_clock_voltage_dependency_table *allowed_mclk_table =
3411 &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk;
3412 struct radeon_cac_leakage_table *std_voltage_table =
3413 &rdev->pm.dpm.dyn_state.cac_leakage_table;
3414 u32 i;
3415
3416 if (allowed_sclk_vddc_table == NULL)
3417 return -EINVAL;
3418 if (allowed_sclk_vddc_table->count < 1)
3419 return -EINVAL;
3420 if (allowed_mclk_table == NULL)
3421 return -EINVAL;
3422 if (allowed_mclk_table->count < 1)
3423 return -EINVAL;
3424
3425 memset(&pi->dpm_table, 0, sizeof(struct ci_dpm_table));
3426
3427 ci_reset_single_dpm_table(rdev,
3428 &pi->dpm_table.sclk_table,
3429 SMU7_MAX_LEVELS_GRAPHICS);
3430 ci_reset_single_dpm_table(rdev,
3431 &pi->dpm_table.mclk_table,
3432 SMU7_MAX_LEVELS_MEMORY);
3433 ci_reset_single_dpm_table(rdev,
3434 &pi->dpm_table.vddc_table,
3435 SMU7_MAX_LEVELS_VDDC);
3436 ci_reset_single_dpm_table(rdev,
3437 &pi->dpm_table.vddci_table,
3438 SMU7_MAX_LEVELS_VDDCI);
3439 ci_reset_single_dpm_table(rdev,
3440 &pi->dpm_table.mvdd_table,
3441 SMU7_MAX_LEVELS_MVDD);
3442
3443 pi->dpm_table.sclk_table.count = 0;
3444 for (i = 0; i < allowed_sclk_vddc_table->count; i++) {
3445 if ((i == 0) ||
3446 (pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count-1].value !=
3447 allowed_sclk_vddc_table->entries[i].clk)) {
3448 pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count].value =
3449 allowed_sclk_vddc_table->entries[i].clk;
3450 pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count].enabled =
3451 (i == 0) ? true : false;
3452 pi->dpm_table.sclk_table.count++;
3453 }
3454 }
3455
3456 pi->dpm_table.mclk_table.count = 0;
3457 for (i = 0; i < allowed_mclk_table->count; i++) {
3458 if ((i == 0) ||
3459 (pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count-1].value !=
3460 allowed_mclk_table->entries[i].clk)) {
3461 pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count].value =
3462 allowed_mclk_table->entries[i].clk;
3463 pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count].enabled =
3464 (i == 0) ? true : false;
3465 pi->dpm_table.mclk_table.count++;
3466 }
3467 }
3468
3469 for (i = 0; i < allowed_sclk_vddc_table->count; i++) {
3470 pi->dpm_table.vddc_table.dpm_levels[i].value =
3471 allowed_sclk_vddc_table->entries[i].v;
3472 pi->dpm_table.vddc_table.dpm_levels[i].param1 =
3473 std_voltage_table->entries[i].leakage;
3474 pi->dpm_table.vddc_table.dpm_levels[i].enabled = true;
3475 }
3476 pi->dpm_table.vddc_table.count = allowed_sclk_vddc_table->count;
3477
3478 allowed_mclk_table = &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk;
3479 if (allowed_mclk_table) {
3480 for (i = 0; i < allowed_mclk_table->count; i++) {
3481 pi->dpm_table.vddci_table.dpm_levels[i].value =
3482 allowed_mclk_table->entries[i].v;
3483 pi->dpm_table.vddci_table.dpm_levels[i].enabled = true;
3484 }
3485 pi->dpm_table.vddci_table.count = allowed_mclk_table->count;
3486 }
3487
3488 allowed_mclk_table = &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk;
3489 if (allowed_mclk_table) {
3490 for (i = 0; i < allowed_mclk_table->count; i++) {
3491 pi->dpm_table.mvdd_table.dpm_levels[i].value =
3492 allowed_mclk_table->entries[i].v;
3493 pi->dpm_table.mvdd_table.dpm_levels[i].enabled = true;
3494 }
3495 pi->dpm_table.mvdd_table.count = allowed_mclk_table->count;
3496 }
3497
3498 ci_setup_default_pcie_tables(rdev);
3499
3500 return 0;
3501}
3502
3503static int ci_find_boot_level(struct ci_single_dpm_table *table,
3504 u32 value, u32 *boot_level)
3505{
3506 u32 i;
3507 int ret = -EINVAL;
3508
3509 for(i = 0; i < table->count; i++) {
3510 if (value == table->dpm_levels[i].value) {
3511 *boot_level = i;
3512 ret = 0;
3513 }
3514 }
3515
3516 return ret;
3517}
3518
3519static int ci_init_smc_table(struct radeon_device *rdev)
3520{
3521 struct ci_power_info *pi = ci_get_pi(rdev);
3522 struct ci_ulv_parm *ulv = &pi->ulv;
3523 struct radeon_ps *radeon_boot_state = rdev->pm.dpm.boot_ps;
3524 SMU7_Discrete_DpmTable *table = &pi->smc_state_table;
3525 int ret;
3526
3527 ret = ci_setup_default_dpm_tables(rdev);
3528 if (ret)
3529 return ret;
3530
3531 if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_NONE)
3532 ci_populate_smc_voltage_tables(rdev, table);
3533
3534 ci_init_fps_limits(rdev);
3535
3536 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_HARDWAREDC)
3537 table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
3538
3539 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC)
3540 table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
3541
3542 if (pi->mem_gddr5)
3543 table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
3544
3545 if (ulv->supported) {
3546 ret = ci_populate_ulv_state(rdev, &pi->smc_state_table.Ulv);
3547 if (ret)
3548 return ret;
3549 WREG32_SMC(CG_ULV_PARAMETER, ulv->cg_ulv_parameter);
3550 }
3551
3552 ret = ci_populate_all_graphic_levels(rdev);
3553 if (ret)
3554 return ret;
3555
3556 ret = ci_populate_all_memory_levels(rdev);
3557 if (ret)
3558 return ret;
3559
3560 ci_populate_smc_link_level(rdev, table);
3561
3562 ret = ci_populate_smc_acpi_level(rdev, table);
3563 if (ret)
3564 return ret;
3565
3566 ret = ci_populate_smc_vce_level(rdev, table);
3567 if (ret)
3568 return ret;
3569
3570 ret = ci_populate_smc_acp_level(rdev, table);
3571 if (ret)
3572 return ret;
3573
3574 ret = ci_populate_smc_samu_level(rdev, table);
3575 if (ret)
3576 return ret;
3577
3578 ret = ci_do_program_memory_timing_parameters(rdev);
3579 if (ret)
3580 return ret;
3581
3582 ret = ci_populate_smc_uvd_level(rdev, table);
3583 if (ret)
3584 return ret;
3585
3586 table->UvdBootLevel = 0;
3587 table->VceBootLevel = 0;
3588 table->AcpBootLevel = 0;
3589 table->SamuBootLevel = 0;
3590 table->GraphicsBootLevel = 0;
3591 table->MemoryBootLevel = 0;
3592
3593 ret = ci_find_boot_level(&pi->dpm_table.sclk_table,
3594 pi->vbios_boot_state.sclk_bootup_value,
3595 (u32 *)&pi->smc_state_table.GraphicsBootLevel);
3596
3597 ret = ci_find_boot_level(&pi->dpm_table.mclk_table,
3598 pi->vbios_boot_state.mclk_bootup_value,
3599 (u32 *)&pi->smc_state_table.MemoryBootLevel);
3600
3601 table->BootVddc = pi->vbios_boot_state.vddc_bootup_value;
3602 table->BootVddci = pi->vbios_boot_state.vddci_bootup_value;
3603 table->BootMVdd = pi->vbios_boot_state.mvdd_bootup_value;
3604
3605 ci_populate_smc_initial_state(rdev, radeon_boot_state);
3606
3607 ret = ci_populate_bapm_parameters_in_dpm_table(rdev);
3608 if (ret)
3609 return ret;
3610
3611 table->UVDInterval = 1;
3612 table->VCEInterval = 1;
3613 table->ACPInterval = 1;
3614 table->SAMUInterval = 1;
3615 table->GraphicsVoltageChangeEnable = 1;
3616 table->GraphicsThermThrottleEnable = 1;
3617 table->GraphicsInterval = 1;
3618 table->VoltageInterval = 1;
3619 table->ThermalInterval = 1;
3620 table->TemperatureLimitHigh = (u16)((pi->thermal_temp_setting.temperature_high *
3621 CISLANDS_Q88_FORMAT_CONVERSION_UNIT) / 1000);
3622 table->TemperatureLimitLow = (u16)((pi->thermal_temp_setting.temperature_low *
3623 CISLANDS_Q88_FORMAT_CONVERSION_UNIT) / 1000);
3624 table->MemoryVoltageChangeEnable = 1;
3625 table->MemoryInterval = 1;
3626 table->VoltageResponseTime = 0;
3627 table->VddcVddciDelta = 4000;
3628 table->PhaseResponseTime = 0;
3629 table->MemoryThermThrottleEnable = 1;
3630 table->PCIeBootLinkLevel = pi->dpm_table.pcie_speed_table.count - 1;
3631 table->PCIeGenInterval = 1;
3632 if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2)
3633 table->SVI2Enable = 1;
3634 else
3635 table->SVI2Enable = 0;
3636
3637 table->ThermGpio = 17;
3638 table->SclkStepSize = 0x4000;
3639
3640 table->SystemFlags = cpu_to_be32(table->SystemFlags);
3641 table->SmioMaskVddcVid = cpu_to_be32(table->SmioMaskVddcVid);
3642 table->SmioMaskVddcPhase = cpu_to_be32(table->SmioMaskVddcPhase);
3643 table->SmioMaskVddciVid = cpu_to_be32(table->SmioMaskVddciVid);
3644 table->SmioMaskMvddVid = cpu_to_be32(table->SmioMaskMvddVid);
3645 table->SclkStepSize = cpu_to_be32(table->SclkStepSize);
3646 table->TemperatureLimitHigh = cpu_to_be16(table->TemperatureLimitHigh);
3647 table->TemperatureLimitLow = cpu_to_be16(table->TemperatureLimitLow);
3648 table->VddcVddciDelta = cpu_to_be16(table->VddcVddciDelta);
3649 table->VoltageResponseTime = cpu_to_be16(table->VoltageResponseTime);
3650 table->PhaseResponseTime = cpu_to_be16(table->PhaseResponseTime);
3651 table->BootVddc = cpu_to_be16(table->BootVddc * VOLTAGE_SCALE);
3652 table->BootVddci = cpu_to_be16(table->BootVddci * VOLTAGE_SCALE);
3653 table->BootMVdd = cpu_to_be16(table->BootMVdd * VOLTAGE_SCALE);
3654
3655 ret = ci_copy_bytes_to_smc(rdev,
3656 pi->dpm_table_start +
3657 offsetof(SMU7_Discrete_DpmTable, SystemFlags),
3658 (u8 *)&table->SystemFlags,
3659 sizeof(SMU7_Discrete_DpmTable) - 3 * sizeof(SMU7_PIDController),
3660 pi->sram_end);
3661 if (ret)
3662 return ret;
3663
3664 return 0;
3665}
3666
3667static void ci_trim_single_dpm_states(struct radeon_device *rdev,
3668 struct ci_single_dpm_table *dpm_table,
3669 u32 low_limit, u32 high_limit)
3670{
3671 u32 i;
3672
3673 for (i = 0; i < dpm_table->count; i++) {
3674 if ((dpm_table->dpm_levels[i].value < low_limit) ||
3675 (dpm_table->dpm_levels[i].value > high_limit))
3676 dpm_table->dpm_levels[i].enabled = false;
3677 else
3678 dpm_table->dpm_levels[i].enabled = true;
3679 }
3680}
3681
3682static void ci_trim_pcie_dpm_states(struct radeon_device *rdev,
3683 u32 speed_low, u32 lanes_low,
3684 u32 speed_high, u32 lanes_high)
3685{
3686 struct ci_power_info *pi = ci_get_pi(rdev);
3687 struct ci_single_dpm_table *pcie_table = &pi->dpm_table.pcie_speed_table;
3688 u32 i, j;
3689
3690 for (i = 0; i < pcie_table->count; i++) {
3691 if ((pcie_table->dpm_levels[i].value < speed_low) ||
3692 (pcie_table->dpm_levels[i].param1 < lanes_low) ||
3693 (pcie_table->dpm_levels[i].value > speed_high) ||
3694 (pcie_table->dpm_levels[i].param1 > lanes_high))
3695 pcie_table->dpm_levels[i].enabled = false;
3696 else
3697 pcie_table->dpm_levels[i].enabled = true;
3698 }
3699