blob: 893ed68cbaed89a593f70e5957c4221b15c8b18f [file] [log] [blame]
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04001/*
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
Alex Deucher7e1858f2014-04-11 11:21:51 -040024#include <linux/firmware.h>
Alex Deuchercc8dbbb2013-08-14 01:03:41 -040025#include "drmP.h"
26#include "radeon.h"
Michele Curti01467a92014-10-14 18:25:09 +020027#include "radeon_asic.h"
Alex Deucher7e1858f2014-04-11 11:21:51 -040028#include "radeon_ucode.h"
Alex Deuchercc8dbbb2013-08-14 01:03:41 -040029#include "cikd.h"
30#include "r600_dpm.h"
31#include "ci_dpm.h"
32#include "atom.h"
Alex Deucher94b4adc2013-07-15 17:34:33 -040033#include <linux/seq_file.h>
Alex Deuchercc8dbbb2013-08-14 01:03:41 -040034
35#define MC_CG_ARB_FREQ_F0 0x0a
36#define MC_CG_ARB_FREQ_F1 0x0b
37#define MC_CG_ARB_FREQ_F2 0x0c
38#define MC_CG_ARB_FREQ_F3 0x0d
39
40#define SMC_RAM_END 0x40000
41
42#define VOLTAGE_SCALE 4
43#define VOLTAGE_VID_OFFSET_SCALE1 625
44#define VOLTAGE_VID_OFFSET_SCALE2 100
45
Alex Deucher2d400382013-08-09 18:27:47 -040046static const struct ci_pt_defaults defaults_hawaii_xt =
47{
48 1, 0xF, 0xFD, 0x19, 5, 0x14, 0, 0xB0000,
49 { 0x84, 0x0, 0x0, 0x7F, 0x0, 0x0, 0x5A, 0x60, 0x51, 0x8E, 0x79, 0x6B, 0x5F, 0x90, 0x79 },
50 { 0x1EA, 0x1EA, 0x1EA, 0x224, 0x224, 0x224, 0x24F, 0x24F, 0x24F, 0x28E, 0x28E, 0x28E, 0x2BC, 0x2BC, 0x2BC }
51};
52
53static const struct ci_pt_defaults defaults_hawaii_pro =
54{
55 1, 0xF, 0xFD, 0x19, 5, 0x14, 0, 0x65062,
56 { 0x93, 0x0, 0x0, 0x97, 0x0, 0x0, 0x6B, 0x60, 0x51, 0x95, 0x79, 0x6B, 0x5F, 0x90, 0x79 },
57 { 0x1EA, 0x1EA, 0x1EA, 0x224, 0x224, 0x224, 0x24F, 0x24F, 0x24F, 0x28E, 0x28E, 0x28E, 0x2BC, 0x2BC, 0x2BC }
58};
59
Alex Deuchercc8dbbb2013-08-14 01:03:41 -040060static const struct ci_pt_defaults defaults_bonaire_xt =
61{
62 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0xB0000,
63 { 0x79, 0x253, 0x25D, 0xAE, 0x72, 0x80, 0x83, 0x86, 0x6F, 0xC8, 0xC9, 0xC9, 0x2F, 0x4D, 0x61 },
64 { 0x17C, 0x172, 0x180, 0x1BC, 0x1B3, 0x1BD, 0x206, 0x200, 0x203, 0x25D, 0x25A, 0x255, 0x2C3, 0x2C5, 0x2B4 }
65};
66
67static const struct ci_pt_defaults defaults_bonaire_pro =
68{
69 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0x65062,
70 { 0x8C, 0x23F, 0x244, 0xA6, 0x83, 0x85, 0x86, 0x86, 0x83, 0xDB, 0xDB, 0xDA, 0x67, 0x60, 0x5F },
71 { 0x187, 0x193, 0x193, 0x1C7, 0x1D1, 0x1D1, 0x210, 0x219, 0x219, 0x266, 0x26C, 0x26C, 0x2C9, 0x2CB, 0x2CB }
72};
73
74static const struct ci_pt_defaults defaults_saturn_xt =
75{
76 1, 0xF, 0xFD, 0x19, 5, 55, 0, 0x70000,
77 { 0x8C, 0x247, 0x249, 0xA6, 0x80, 0x81, 0x8B, 0x89, 0x86, 0xC9, 0xCA, 0xC9, 0x4D, 0x4D, 0x4D },
78 { 0x187, 0x187, 0x187, 0x1C7, 0x1C7, 0x1C7, 0x210, 0x210, 0x210, 0x266, 0x266, 0x266, 0x2C9, 0x2C9, 0x2C9 }
79};
80
81static const struct ci_pt_defaults defaults_saturn_pro =
82{
83 1, 0xF, 0xFD, 0x19, 5, 55, 0, 0x30000,
84 { 0x96, 0x21D, 0x23B, 0xA1, 0x85, 0x87, 0x83, 0x84, 0x81, 0xE6, 0xE6, 0xE6, 0x71, 0x6A, 0x6A },
85 { 0x193, 0x19E, 0x19E, 0x1D2, 0x1DC, 0x1DC, 0x21A, 0x223, 0x223, 0x26E, 0x27E, 0x274, 0x2CF, 0x2D2, 0x2D2 }
86};
87
88static const struct ci_pt_config_reg didt_config_ci[] =
89{
90 { 0x10, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
91 { 0x10, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
92 { 0x10, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
93 { 0x10, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
94 { 0x11, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
95 { 0x11, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
96 { 0x11, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
97 { 0x11, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
98 { 0x12, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
99 { 0x12, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
100 { 0x12, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
101 { 0x12, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
102 { 0x2, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
103 { 0x2, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
104 { 0x2, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
105 { 0x1, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
106 { 0x1, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
107 { 0x0, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
108 { 0x30, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
109 { 0x30, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
110 { 0x30, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
111 { 0x30, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
112 { 0x31, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
113 { 0x31, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
114 { 0x31, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
115 { 0x31, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
116 { 0x32, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
117 { 0x32, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
118 { 0x32, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
119 { 0x32, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
120 { 0x22, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
121 { 0x22, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
122 { 0x22, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
123 { 0x21, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
124 { 0x21, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
125 { 0x20, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
126 { 0x50, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
127 { 0x50, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
128 { 0x50, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
129 { 0x50, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
130 { 0x51, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
131 { 0x51, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
132 { 0x51, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
133 { 0x51, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
134 { 0x52, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
135 { 0x52, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
136 { 0x52, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
137 { 0x52, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
138 { 0x42, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
139 { 0x42, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
140 { 0x42, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
141 { 0x41, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
142 { 0x41, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
143 { 0x40, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
144 { 0x70, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
145 { 0x70, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
146 { 0x70, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
147 { 0x70, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
148 { 0x71, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
149 { 0x71, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
150 { 0x71, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
151 { 0x71, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
152 { 0x72, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
153 { 0x72, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
154 { 0x72, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
155 { 0x72, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
156 { 0x62, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
157 { 0x62, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
158 { 0x62, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
159 { 0x61, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
160 { 0x61, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
161 { 0x60, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
162 { 0xFFFFFFFF }
163};
164
165extern u8 rv770_get_memory_module_index(struct radeon_device *rdev);
166extern int ni_copy_and_switch_arb_sets(struct radeon_device *rdev,
167 u32 arb_freq_src, u32 arb_freq_dest);
168extern u8 si_get_ddr3_mclk_frequency_ratio(u32 memory_clock);
169extern u8 si_get_mclk_frequency_ratio(u32 memory_clock, bool strobe_mode);
170extern void si_trim_voltage_table_to_fit_state_table(struct radeon_device *rdev,
171 u32 max_voltage_steps,
172 struct atom_voltage_table *voltage_table);
173extern void cik_enter_rlc_safe_mode(struct radeon_device *rdev);
174extern void cik_exit_rlc_safe_mode(struct radeon_device *rdev);
Alex Deucher6c7bcce2013-12-18 14:07:14 -0500175extern int ci_mc_load_microcode(struct radeon_device *rdev);
Alex Deuchera1d6f972013-09-06 12:33:04 -0400176extern void cik_update_cg(struct radeon_device *rdev,
177 u32 block, bool enable);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400178
179static int ci_get_std_voltage_value_sidd(struct radeon_device *rdev,
180 struct atom_voltage_table_entry *voltage_table,
181 u16 *std_voltage_hi_sidd, u16 *std_voltage_lo_sidd);
182static int ci_set_power_limit(struct radeon_device *rdev, u32 n);
183static int ci_set_overdrive_target_tdp(struct radeon_device *rdev,
184 u32 target_tdp);
185static int ci_update_uvd_dpm(struct radeon_device *rdev, bool gate);
186
187static struct ci_power_info *ci_get_pi(struct radeon_device *rdev)
188{
189 struct ci_power_info *pi = rdev->pm.dpm.priv;
190
191 return pi;
192}
193
194static struct ci_ps *ci_get_ps(struct radeon_ps *rps)
195{
196 struct ci_ps *ps = rps->ps_priv;
197
198 return ps;
199}
200
201static void ci_initialize_powertune_defaults(struct radeon_device *rdev)
202{
203 struct ci_power_info *pi = ci_get_pi(rdev);
204
205 switch (rdev->pdev->device) {
Alex Deucher6abc6d52014-04-10 22:29:02 -0400206 case 0x6649:
Alex Deucher2d400382013-08-09 18:27:47 -0400207 case 0x6650:
Alex Deucher6abc6d52014-04-10 22:29:02 -0400208 case 0x6651:
Alex Deucher2d400382013-08-09 18:27:47 -0400209 case 0x6658:
210 case 0x665C:
Alex Deucher6abc6d52014-04-10 22:29:02 -0400211 case 0x665D:
Alex Deucher2d400382013-08-09 18:27:47 -0400212 default:
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400213 pi->powertune_defaults = &defaults_bonaire_xt;
214 break;
Alex Deucher2d400382013-08-09 18:27:47 -0400215 case 0x6640:
Alex Deucher2d400382013-08-09 18:27:47 -0400216 case 0x6641:
Alex Deucher6abc6d52014-04-10 22:29:02 -0400217 case 0x6646:
218 case 0x6647:
219 pi->powertune_defaults = &defaults_saturn_xt;
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400220 break;
Alex Deucher2d400382013-08-09 18:27:47 -0400221 case 0x67B8:
222 case 0x67B0:
Alex Deucher6abc6d52014-04-10 22:29:02 -0400223 pi->powertune_defaults = &defaults_hawaii_xt;
224 break;
225 case 0x67BA:
226 case 0x67B1:
227 pi->powertune_defaults = &defaults_hawaii_pro;
228 break;
Alex Deucher2d400382013-08-09 18:27:47 -0400229 case 0x67A0:
230 case 0x67A1:
231 case 0x67A2:
232 case 0x67A8:
233 case 0x67A9:
234 case 0x67AA:
235 case 0x67B9:
236 case 0x67BE:
Alex Deucher6abc6d52014-04-10 22:29:02 -0400237 pi->powertune_defaults = &defaults_bonaire_xt;
Alex Deucher2d400382013-08-09 18:27:47 -0400238 break;
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400239 }
240
241 pi->dte_tj_offset = 0;
242
243 pi->caps_power_containment = true;
244 pi->caps_cac = false;
245 pi->caps_sq_ramping = false;
246 pi->caps_db_ramping = false;
247 pi->caps_td_ramping = false;
248 pi->caps_tcp_ramping = false;
249
250 if (pi->caps_power_containment) {
251 pi->caps_cac = true;
252 pi->enable_bapm_feature = true;
253 pi->enable_tdc_limit_feature = true;
254 pi->enable_pkg_pwr_tracking_feature = true;
255 }
256}
257
258static u8 ci_convert_to_vid(u16 vddc)
259{
260 return (6200 - (vddc * VOLTAGE_SCALE)) / 25;
261}
262
263static int ci_populate_bapm_vddc_vid_sidd(struct radeon_device *rdev)
264{
265 struct ci_power_info *pi = ci_get_pi(rdev);
266 u8 *hi_vid = pi->smc_powertune_table.BapmVddCVidHiSidd;
267 u8 *lo_vid = pi->smc_powertune_table.BapmVddCVidLoSidd;
268 u8 *hi2_vid = pi->smc_powertune_table.BapmVddCVidHiSidd2;
269 u32 i;
270
271 if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries == NULL)
272 return -EINVAL;
273 if (rdev->pm.dpm.dyn_state.cac_leakage_table.count > 8)
274 return -EINVAL;
275 if (rdev->pm.dpm.dyn_state.cac_leakage_table.count !=
276 rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count)
277 return -EINVAL;
278
279 for (i = 0; i < rdev->pm.dpm.dyn_state.cac_leakage_table.count; i++) {
280 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_EVV) {
281 lo_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc1);
282 hi_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc2);
283 hi2_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc3);
284 } else {
285 lo_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc);
286 hi_vid[i] = ci_convert_to_vid((u16)rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].leakage);
287 }
288 }
289 return 0;
290}
291
292static int ci_populate_vddc_vid(struct radeon_device *rdev)
293{
294 struct ci_power_info *pi = ci_get_pi(rdev);
295 u8 *vid = pi->smc_powertune_table.VddCVid;
296 u32 i;
297
298 if (pi->vddc_voltage_table.count > 8)
299 return -EINVAL;
300
301 for (i = 0; i < pi->vddc_voltage_table.count; i++)
302 vid[i] = ci_convert_to_vid(pi->vddc_voltage_table.entries[i].value);
303
304 return 0;
305}
306
307static int ci_populate_svi_load_line(struct radeon_device *rdev)
308{
309 struct ci_power_info *pi = ci_get_pi(rdev);
310 const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
311
312 pi->smc_powertune_table.SviLoadLineEn = pt_defaults->svi_load_line_en;
313 pi->smc_powertune_table.SviLoadLineVddC = pt_defaults->svi_load_line_vddc;
314 pi->smc_powertune_table.SviLoadLineTrimVddC = 3;
315 pi->smc_powertune_table.SviLoadLineOffsetVddC = 0;
316
317 return 0;
318}
319
320static int ci_populate_tdc_limit(struct radeon_device *rdev)
321{
322 struct ci_power_info *pi = ci_get_pi(rdev);
323 const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
324 u16 tdc_limit;
325
326 tdc_limit = rdev->pm.dpm.dyn_state.cac_tdp_table->tdc * 256;
327 pi->smc_powertune_table.TDC_VDDC_PkgLimit = cpu_to_be16(tdc_limit);
328 pi->smc_powertune_table.TDC_VDDC_ThrottleReleaseLimitPerc =
329 pt_defaults->tdc_vddc_throttle_release_limit_perc;
330 pi->smc_powertune_table.TDC_MAWt = pt_defaults->tdc_mawt;
331
332 return 0;
333}
334
335static int ci_populate_dw8(struct radeon_device *rdev)
336{
337 struct ci_power_info *pi = ci_get_pi(rdev);
338 const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
339 int ret;
340
341 ret = ci_read_smc_sram_dword(rdev,
342 SMU7_FIRMWARE_HEADER_LOCATION +
343 offsetof(SMU7_Firmware_Header, PmFuseTable) +
344 offsetof(SMU7_Discrete_PmFuses, TdcWaterfallCtl),
345 (u32 *)&pi->smc_powertune_table.TdcWaterfallCtl,
346 pi->sram_end);
347 if (ret)
348 return -EINVAL;
349 else
350 pi->smc_powertune_table.TdcWaterfallCtl = pt_defaults->tdc_waterfall_ctl;
351
352 return 0;
353}
354
355static int ci_min_max_v_gnbl_pm_lid_from_bapm_vddc(struct radeon_device *rdev)
356{
357 struct ci_power_info *pi = ci_get_pi(rdev);
358 u8 *hi_vid = pi->smc_powertune_table.BapmVddCVidHiSidd;
359 u8 *lo_vid = pi->smc_powertune_table.BapmVddCVidLoSidd;
360 int i, min, max;
361
362 min = max = hi_vid[0];
363 for (i = 0; i < 8; i++) {
364 if (0 != hi_vid[i]) {
365 if (min > hi_vid[i])
366 min = hi_vid[i];
367 if (max < hi_vid[i])
368 max = hi_vid[i];
369 }
370
371 if (0 != lo_vid[i]) {
372 if (min > lo_vid[i])
373 min = lo_vid[i];
374 if (max < lo_vid[i])
375 max = lo_vid[i];
376 }
377 }
378
379 if ((min == 0) || (max == 0))
380 return -EINVAL;
381 pi->smc_powertune_table.GnbLPMLMaxVid = (u8)max;
382 pi->smc_powertune_table.GnbLPMLMinVid = (u8)min;
383
384 return 0;
385}
386
387static int ci_populate_bapm_vddc_base_leakage_sidd(struct radeon_device *rdev)
388{
389 struct ci_power_info *pi = ci_get_pi(rdev);
390 u16 hi_sidd = pi->smc_powertune_table.BapmVddCBaseLeakageHiSidd;
391 u16 lo_sidd = pi->smc_powertune_table.BapmVddCBaseLeakageLoSidd;
392 struct radeon_cac_tdp_table *cac_tdp_table =
393 rdev->pm.dpm.dyn_state.cac_tdp_table;
394
395 hi_sidd = cac_tdp_table->high_cac_leakage / 100 * 256;
396 lo_sidd = cac_tdp_table->low_cac_leakage / 100 * 256;
397
398 pi->smc_powertune_table.BapmVddCBaseLeakageHiSidd = cpu_to_be16(hi_sidd);
399 pi->smc_powertune_table.BapmVddCBaseLeakageLoSidd = cpu_to_be16(lo_sidd);
400
401 return 0;
402}
403
404static int ci_populate_bapm_parameters_in_dpm_table(struct radeon_device *rdev)
405{
406 struct ci_power_info *pi = ci_get_pi(rdev);
407 const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
408 SMU7_Discrete_DpmTable *dpm_table = &pi->smc_state_table;
409 struct radeon_cac_tdp_table *cac_tdp_table =
410 rdev->pm.dpm.dyn_state.cac_tdp_table;
411 struct radeon_ppm_table *ppm = rdev->pm.dpm.dyn_state.ppm_table;
412 int i, j, k;
413 const u16 *def1;
414 const u16 *def2;
415
416 dpm_table->DefaultTdp = cac_tdp_table->tdp * 256;
417 dpm_table->TargetTdp = cac_tdp_table->configurable_tdp * 256;
418
419 dpm_table->DTETjOffset = (u8)pi->dte_tj_offset;
420 dpm_table->GpuTjMax =
421 (u8)(pi->thermal_temp_setting.temperature_high / 1000);
422 dpm_table->GpuTjHyst = 8;
423
424 dpm_table->DTEAmbientTempBase = pt_defaults->dte_ambient_temp_base;
425
426 if (ppm) {
427 dpm_table->PPM_PkgPwrLimit = cpu_to_be16((u16)ppm->dgpu_tdp * 256 / 1000);
428 dpm_table->PPM_TemperatureLimit = cpu_to_be16((u16)ppm->tj_max * 256);
429 } else {
430 dpm_table->PPM_PkgPwrLimit = cpu_to_be16(0);
431 dpm_table->PPM_TemperatureLimit = cpu_to_be16(0);
432 }
433
434 dpm_table->BAPM_TEMP_GRADIENT = cpu_to_be32(pt_defaults->bapm_temp_gradient);
435 def1 = pt_defaults->bapmti_r;
436 def2 = pt_defaults->bapmti_rc;
437
438 for (i = 0; i < SMU7_DTE_ITERATIONS; i++) {
439 for (j = 0; j < SMU7_DTE_SOURCES; j++) {
440 for (k = 0; k < SMU7_DTE_SINKS; k++) {
441 dpm_table->BAPMTI_R[i][j][k] = cpu_to_be16(*def1);
442 dpm_table->BAPMTI_RC[i][j][k] = cpu_to_be16(*def2);
443 def1++;
444 def2++;
445 }
446 }
447 }
448
449 return 0;
450}
451
452static int ci_populate_pm_base(struct radeon_device *rdev)
453{
454 struct ci_power_info *pi = ci_get_pi(rdev);
455 u32 pm_fuse_table_offset;
456 int ret;
457
458 if (pi->caps_power_containment) {
459 ret = ci_read_smc_sram_dword(rdev,
460 SMU7_FIRMWARE_HEADER_LOCATION +
461 offsetof(SMU7_Firmware_Header, PmFuseTable),
462 &pm_fuse_table_offset, pi->sram_end);
463 if (ret)
464 return ret;
465 ret = ci_populate_bapm_vddc_vid_sidd(rdev);
466 if (ret)
467 return ret;
468 ret = ci_populate_vddc_vid(rdev);
469 if (ret)
470 return ret;
471 ret = ci_populate_svi_load_line(rdev);
472 if (ret)
473 return ret;
474 ret = ci_populate_tdc_limit(rdev);
475 if (ret)
476 return ret;
477 ret = ci_populate_dw8(rdev);
478 if (ret)
479 return ret;
480 ret = ci_min_max_v_gnbl_pm_lid_from_bapm_vddc(rdev);
481 if (ret)
482 return ret;
483 ret = ci_populate_bapm_vddc_base_leakage_sidd(rdev);
484 if (ret)
485 return ret;
486 ret = ci_copy_bytes_to_smc(rdev, pm_fuse_table_offset,
487 (u8 *)&pi->smc_powertune_table,
488 sizeof(SMU7_Discrete_PmFuses), pi->sram_end);
489 if (ret)
490 return ret;
491 }
492
493 return 0;
494}
495
496static void ci_do_enable_didt(struct radeon_device *rdev, const bool enable)
497{
498 struct ci_power_info *pi = ci_get_pi(rdev);
499 u32 data;
500
501 if (pi->caps_sq_ramping) {
502 data = RREG32_DIDT(DIDT_SQ_CTRL0);
503 if (enable)
504 data |= DIDT_CTRL_EN;
505 else
506 data &= ~DIDT_CTRL_EN;
507 WREG32_DIDT(DIDT_SQ_CTRL0, data);
508 }
509
510 if (pi->caps_db_ramping) {
511 data = RREG32_DIDT(DIDT_DB_CTRL0);
512 if (enable)
513 data |= DIDT_CTRL_EN;
514 else
515 data &= ~DIDT_CTRL_EN;
516 WREG32_DIDT(DIDT_DB_CTRL0, data);
517 }
518
519 if (pi->caps_td_ramping) {
520 data = RREG32_DIDT(DIDT_TD_CTRL0);
521 if (enable)
522 data |= DIDT_CTRL_EN;
523 else
524 data &= ~DIDT_CTRL_EN;
525 WREG32_DIDT(DIDT_TD_CTRL0, data);
526 }
527
528 if (pi->caps_tcp_ramping) {
529 data = RREG32_DIDT(DIDT_TCP_CTRL0);
530 if (enable)
531 data |= DIDT_CTRL_EN;
532 else
533 data &= ~DIDT_CTRL_EN;
534 WREG32_DIDT(DIDT_TCP_CTRL0, data);
535 }
536}
537
538static int ci_program_pt_config_registers(struct radeon_device *rdev,
539 const struct ci_pt_config_reg *cac_config_regs)
540{
541 const struct ci_pt_config_reg *config_regs = cac_config_regs;
542 u32 data;
543 u32 cache = 0;
544
545 if (config_regs == NULL)
546 return -EINVAL;
547
548 while (config_regs->offset != 0xFFFFFFFF) {
549 if (config_regs->type == CISLANDS_CONFIGREG_CACHE) {
550 cache |= ((config_regs->value << config_regs->shift) & config_regs->mask);
551 } else {
552 switch (config_regs->type) {
553 case CISLANDS_CONFIGREG_SMC_IND:
554 data = RREG32_SMC(config_regs->offset);
555 break;
556 case CISLANDS_CONFIGREG_DIDT_IND:
557 data = RREG32_DIDT(config_regs->offset);
558 break;
559 default:
560 data = RREG32(config_regs->offset << 2);
561 break;
562 }
563
564 data &= ~config_regs->mask;
565 data |= ((config_regs->value << config_regs->shift) & config_regs->mask);
566 data |= cache;
567
568 switch (config_regs->type) {
569 case CISLANDS_CONFIGREG_SMC_IND:
570 WREG32_SMC(config_regs->offset, data);
571 break;
572 case CISLANDS_CONFIGREG_DIDT_IND:
573 WREG32_DIDT(config_regs->offset, data);
574 break;
575 default:
576 WREG32(config_regs->offset << 2, data);
577 break;
578 }
579 cache = 0;
580 }
581 config_regs++;
582 }
583 return 0;
584}
585
586static int ci_enable_didt(struct radeon_device *rdev, bool enable)
587{
588 struct ci_power_info *pi = ci_get_pi(rdev);
589 int ret;
590
591 if (pi->caps_sq_ramping || pi->caps_db_ramping ||
592 pi->caps_td_ramping || pi->caps_tcp_ramping) {
593 cik_enter_rlc_safe_mode(rdev);
594
595 if (enable) {
596 ret = ci_program_pt_config_registers(rdev, didt_config_ci);
597 if (ret) {
598 cik_exit_rlc_safe_mode(rdev);
599 return ret;
600 }
601 }
602
603 ci_do_enable_didt(rdev, enable);
604
605 cik_exit_rlc_safe_mode(rdev);
606 }
607
608 return 0;
609}
610
611static int ci_enable_power_containment(struct radeon_device *rdev, bool enable)
612{
613 struct ci_power_info *pi = ci_get_pi(rdev);
614 PPSMC_Result smc_result;
615 int ret = 0;
616
617 if (enable) {
618 pi->power_containment_features = 0;
619 if (pi->caps_power_containment) {
620 if (pi->enable_bapm_feature) {
621 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableDTE);
622 if (smc_result != PPSMC_Result_OK)
623 ret = -EINVAL;
624 else
625 pi->power_containment_features |= POWERCONTAINMENT_FEATURE_BAPM;
626 }
627
628 if (pi->enable_tdc_limit_feature) {
629 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_TDCLimitEnable);
630 if (smc_result != PPSMC_Result_OK)
631 ret = -EINVAL;
632 else
633 pi->power_containment_features |= POWERCONTAINMENT_FEATURE_TDCLimit;
634 }
635
636 if (pi->enable_pkg_pwr_tracking_feature) {
637 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PkgPwrLimitEnable);
638 if (smc_result != PPSMC_Result_OK) {
639 ret = -EINVAL;
640 } else {
641 struct radeon_cac_tdp_table *cac_tdp_table =
642 rdev->pm.dpm.dyn_state.cac_tdp_table;
643 u32 default_pwr_limit =
644 (u32)(cac_tdp_table->maximum_power_delivery_limit * 256);
645
646 pi->power_containment_features |= POWERCONTAINMENT_FEATURE_PkgPwrLimit;
647
648 ci_set_power_limit(rdev, default_pwr_limit);
649 }
650 }
651 }
652 } else {
653 if (pi->caps_power_containment && pi->power_containment_features) {
654 if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_TDCLimit)
655 ci_send_msg_to_smc(rdev, PPSMC_MSG_TDCLimitDisable);
656
657 if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_BAPM)
658 ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableDTE);
659
660 if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_PkgPwrLimit)
661 ci_send_msg_to_smc(rdev, PPSMC_MSG_PkgPwrLimitDisable);
662 pi->power_containment_features = 0;
663 }
664 }
665
666 return ret;
667}
668
669static int ci_enable_smc_cac(struct radeon_device *rdev, bool enable)
670{
671 struct ci_power_info *pi = ci_get_pi(rdev);
672 PPSMC_Result smc_result;
673 int ret = 0;
674
675 if (pi->caps_cac) {
676 if (enable) {
677 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableCac);
678 if (smc_result != PPSMC_Result_OK) {
679 ret = -EINVAL;
680 pi->cac_enabled = false;
681 } else {
682 pi->cac_enabled = true;
683 }
684 } else if (pi->cac_enabled) {
685 ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableCac);
686 pi->cac_enabled = false;
687 }
688 }
689
690 return ret;
691}
692
693static int ci_power_control_set_level(struct radeon_device *rdev)
694{
695 struct ci_power_info *pi = ci_get_pi(rdev);
696 struct radeon_cac_tdp_table *cac_tdp_table =
697 rdev->pm.dpm.dyn_state.cac_tdp_table;
698 s32 adjust_percent;
699 s32 target_tdp;
700 int ret = 0;
701 bool adjust_polarity = false; /* ??? */
702
703 if (pi->caps_power_containment &&
704 (pi->power_containment_features & POWERCONTAINMENT_FEATURE_BAPM)) {
705 adjust_percent = adjust_polarity ?
706 rdev->pm.dpm.tdp_adjustment : (-1 * rdev->pm.dpm.tdp_adjustment);
707 target_tdp = ((100 + adjust_percent) *
708 (s32)cac_tdp_table->configurable_tdp) / 100;
709 target_tdp *= 256;
710
711 ret = ci_set_overdrive_target_tdp(rdev, (u32)target_tdp);
712 }
713
714 return ret;
715}
716
Alex Deucher942bdf72013-08-09 10:05:24 -0400717void ci_dpm_powergate_uvd(struct radeon_device *rdev, bool gate)
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400718{
Alex Deucher47acb1f2013-08-26 09:43:24 -0400719 struct ci_power_info *pi = ci_get_pi(rdev);
720
721 if (pi->uvd_power_gated == gate)
722 return;
723
724 pi->uvd_power_gated = gate;
725
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400726 ci_update_uvd_dpm(rdev, gate);
727}
728
Alex Deucher54961312013-07-15 18:24:31 -0400729bool ci_dpm_vblank_too_short(struct radeon_device *rdev)
730{
731 struct ci_power_info *pi = ci_get_pi(rdev);
732 u32 vblank_time = r600_dpm_get_vblank_time(rdev);
733 u32 switch_limit = pi->mem_gddr5 ? 450 : 300;
734
735 if (vblank_time < switch_limit)
736 return true;
737 else
738 return false;
739
740}
741
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400742static void ci_apply_state_adjust_rules(struct radeon_device *rdev,
743 struct radeon_ps *rps)
744{
745 struct ci_ps *ps = ci_get_ps(rps);
746 struct ci_power_info *pi = ci_get_pi(rdev);
747 struct radeon_clock_and_voltage_limits *max_limits;
748 bool disable_mclk_switching;
749 u32 sclk, mclk;
750 int i;
751
Alex Deucher8cd36682013-08-23 11:05:24 -0400752 if (rps->vce_active) {
753 rps->evclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].evclk;
754 rps->ecclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].ecclk;
755 } else {
756 rps->evclk = 0;
757 rps->ecclk = 0;
758 }
759
Alex Deucher54961312013-07-15 18:24:31 -0400760 if ((rdev->pm.dpm.new_active_crtc_count > 1) ||
761 ci_dpm_vblank_too_short(rdev))
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400762 disable_mclk_switching = true;
763 else
764 disable_mclk_switching = false;
765
766 if ((rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
767 pi->battery_state = true;
768 else
769 pi->battery_state = false;
770
771 if (rdev->pm.dpm.ac_power)
772 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
773 else
774 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
775
776 if (rdev->pm.dpm.ac_power == false) {
777 for (i = 0; i < ps->performance_level_count; i++) {
778 if (ps->performance_levels[i].mclk > max_limits->mclk)
779 ps->performance_levels[i].mclk = max_limits->mclk;
780 if (ps->performance_levels[i].sclk > max_limits->sclk)
781 ps->performance_levels[i].sclk = max_limits->sclk;
782 }
783 }
784
785 /* XXX validate the min clocks required for display */
786
787 if (disable_mclk_switching) {
788 mclk = ps->performance_levels[ps->performance_level_count - 1].mclk;
789 sclk = ps->performance_levels[0].sclk;
790 } else {
791 mclk = ps->performance_levels[0].mclk;
792 sclk = ps->performance_levels[0].sclk;
793 }
794
Alex Deucher8cd36682013-08-23 11:05:24 -0400795 if (rps->vce_active) {
796 if (sclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk)
797 sclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk;
798 if (mclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].mclk)
799 mclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].mclk;
800 }
801
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400802 ps->performance_levels[0].sclk = sclk;
803 ps->performance_levels[0].mclk = mclk;
804
805 if (ps->performance_levels[1].sclk < ps->performance_levels[0].sclk)
806 ps->performance_levels[1].sclk = ps->performance_levels[0].sclk;
807
808 if (disable_mclk_switching) {
809 if (ps->performance_levels[0].mclk < ps->performance_levels[1].mclk)
810 ps->performance_levels[0].mclk = ps->performance_levels[1].mclk;
811 } else {
812 if (ps->performance_levels[1].mclk < ps->performance_levels[0].mclk)
813 ps->performance_levels[1].mclk = ps->performance_levels[0].mclk;
814 }
815}
816
Alex Deucher1955f102014-09-14 23:45:30 -0400817static int ci_thermal_set_temperature_range(struct radeon_device *rdev,
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400818 int min_temp, int max_temp)
819{
820 int low_temp = 0 * 1000;
821 int high_temp = 255 * 1000;
822 u32 tmp;
823
824 if (low_temp < min_temp)
825 low_temp = min_temp;
826 if (high_temp > max_temp)
827 high_temp = max_temp;
828 if (high_temp < low_temp) {
829 DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
830 return -EINVAL;
831 }
832
833 tmp = RREG32_SMC(CG_THERMAL_INT);
834 tmp &= ~(CI_DIG_THERM_INTH_MASK | CI_DIG_THERM_INTL_MASK);
835 tmp |= CI_DIG_THERM_INTH(high_temp / 1000) |
836 CI_DIG_THERM_INTL(low_temp / 1000);
837 WREG32_SMC(CG_THERMAL_INT, tmp);
838
839#if 0
840 /* XXX: need to figure out how to handle this properly */
841 tmp = RREG32_SMC(CG_THERMAL_CTRL);
842 tmp &= DIG_THERM_DPM_MASK;
843 tmp |= DIG_THERM_DPM(high_temp / 1000);
844 WREG32_SMC(CG_THERMAL_CTRL, tmp);
845#endif
846
Oleg Chernovskiy6bce8d92014-08-11 21:53:46 +0400847 rdev->pm.dpm.thermal.min_temp = low_temp;
848 rdev->pm.dpm.thermal.max_temp = high_temp;
849
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400850 return 0;
851}
852
Alex Deucher1955f102014-09-14 23:45:30 -0400853static int ci_thermal_enable_alert(struct radeon_device *rdev,
854 bool enable)
855{
856 u32 thermal_int = RREG32_SMC(CG_THERMAL_INT);
857 PPSMC_Result result;
858
859 if (enable) {
860 thermal_int &= ~(THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW);
861 rdev->irq.dpm_thermal = false;
862 result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Thermal_Cntl_Enable);
863 if (result != PPSMC_Result_OK) {
864 DRM_DEBUG_KMS("Could not enable thermal interrupts.\n");
865 return -EINVAL;
866 }
867 } else {
868 thermal_int |= THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW;
869 rdev->irq.dpm_thermal = true;
870 result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Thermal_Cntl_Disable);
871 if (result != PPSMC_Result_OK) {
872 DRM_DEBUG_KMS("Could not disable thermal interrupts.\n");
873 return -EINVAL;
874 }
875 }
876
877 WREG32_SMC(CG_THERMAL_INT, thermal_int);
878
879 return 0;
880}
881
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400882#if 0
883static int ci_read_smc_soft_register(struct radeon_device *rdev,
884 u16 reg_offset, u32 *value)
885{
886 struct ci_power_info *pi = ci_get_pi(rdev);
887
888 return ci_read_smc_sram_dword(rdev,
889 pi->soft_regs_start + reg_offset,
890 value, pi->sram_end);
891}
892#endif
893
894static int ci_write_smc_soft_register(struct radeon_device *rdev,
895 u16 reg_offset, u32 value)
896{
897 struct ci_power_info *pi = ci_get_pi(rdev);
898
899 return ci_write_smc_sram_dword(rdev,
900 pi->soft_regs_start + reg_offset,
901 value, pi->sram_end);
902}
903
904static void ci_init_fps_limits(struct radeon_device *rdev)
905{
906 struct ci_power_info *pi = ci_get_pi(rdev);
907 SMU7_Discrete_DpmTable *table = &pi->smc_state_table;
908
909 if (pi->caps_fps) {
910 u16 tmp;
911
912 tmp = 45;
913 table->FpsHighT = cpu_to_be16(tmp);
914
915 tmp = 30;
916 table->FpsLowT = cpu_to_be16(tmp);
917 }
918}
919
920static int ci_update_sclk_t(struct radeon_device *rdev)
921{
922 struct ci_power_info *pi = ci_get_pi(rdev);
923 int ret = 0;
924 u32 low_sclk_interrupt_t = 0;
925
926 if (pi->caps_sclk_throttle_low_notification) {
927 low_sclk_interrupt_t = cpu_to_be32(pi->low_sclk_interrupt_t);
928
929 ret = ci_copy_bytes_to_smc(rdev,
930 pi->dpm_table_start +
931 offsetof(SMU7_Discrete_DpmTable, LowSclkInterruptT),
932 (u8 *)&low_sclk_interrupt_t,
933 sizeof(u32), pi->sram_end);
934
935 }
936
937 return ret;
938}
939
940static void ci_get_leakage_voltages(struct radeon_device *rdev)
941{
942 struct ci_power_info *pi = ci_get_pi(rdev);
943 u16 leakage_id, virtual_voltage_id;
944 u16 vddc, vddci;
945 int i;
946
947 pi->vddc_leakage.count = 0;
948 pi->vddci_leakage.count = 0;
949
Alex Deucher6b57f202014-07-31 18:07:17 -0400950 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_EVV) {
951 for (i = 0; i < CISLANDS_MAX_LEAKAGE_COUNT; i++) {
952 virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
953 if (radeon_atom_get_voltage_evv(rdev, virtual_voltage_id, &vddc) != 0)
954 continue;
955 if (vddc != 0 && vddc != virtual_voltage_id) {
956 pi->vddc_leakage.actual_voltage[pi->vddc_leakage.count] = vddc;
957 pi->vddc_leakage.leakage_id[pi->vddc_leakage.count] = virtual_voltage_id;
958 pi->vddc_leakage.count++;
959 }
960 }
961 } else if (radeon_atom_get_leakage_id_from_vbios(rdev, &leakage_id) == 0) {
Alex Deuchercc8dbbb2013-08-14 01:03:41 -0400962 for (i = 0; i < CISLANDS_MAX_LEAKAGE_COUNT; i++) {
963 virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
964 if (radeon_atom_get_leakage_vddc_based_on_leakage_params(rdev, &vddc, &vddci,
965 virtual_voltage_id,
966 leakage_id) == 0) {
967 if (vddc != 0 && vddc != virtual_voltage_id) {
968 pi->vddc_leakage.actual_voltage[pi->vddc_leakage.count] = vddc;
969 pi->vddc_leakage.leakage_id[pi->vddc_leakage.count] = virtual_voltage_id;
970 pi->vddc_leakage.count++;
971 }
972 if (vddci != 0 && vddci != virtual_voltage_id) {
973 pi->vddci_leakage.actual_voltage[pi->vddci_leakage.count] = vddci;
974 pi->vddci_leakage.leakage_id[pi->vddci_leakage.count] = virtual_voltage_id;
975 pi->vddci_leakage.count++;
976 }
977 }
978 }
979 }
980}
981
982static void ci_set_dpm_event_sources(struct radeon_device *rdev, u32 sources)
983{
984 struct ci_power_info *pi = ci_get_pi(rdev);
985 bool want_thermal_protection;
986 enum radeon_dpm_event_src dpm_event_src;
987 u32 tmp;
988
989 switch (sources) {
990 case 0:
991 default:
992 want_thermal_protection = false;
993 break;
994 case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL):
995 want_thermal_protection = true;
996 dpm_event_src = RADEON_DPM_EVENT_SRC_DIGITAL;
997 break;
998 case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL):
999 want_thermal_protection = true;
1000 dpm_event_src = RADEON_DPM_EVENT_SRC_EXTERNAL;
1001 break;
1002 case ((1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL) |
1003 (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL)):
1004 want_thermal_protection = true;
1005 dpm_event_src = RADEON_DPM_EVENT_SRC_DIGIAL_OR_EXTERNAL;
1006 break;
1007 }
1008
1009 if (want_thermal_protection) {
1010#if 0
1011 /* XXX: need to figure out how to handle this properly */
1012 tmp = RREG32_SMC(CG_THERMAL_CTRL);
1013 tmp &= DPM_EVENT_SRC_MASK;
1014 tmp |= DPM_EVENT_SRC(dpm_event_src);
1015 WREG32_SMC(CG_THERMAL_CTRL, tmp);
1016#endif
1017
1018 tmp = RREG32_SMC(GENERAL_PWRMGT);
1019 if (pi->thermal_protection)
1020 tmp &= ~THERMAL_PROTECTION_DIS;
1021 else
1022 tmp |= THERMAL_PROTECTION_DIS;
1023 WREG32_SMC(GENERAL_PWRMGT, tmp);
1024 } else {
1025 tmp = RREG32_SMC(GENERAL_PWRMGT);
1026 tmp |= THERMAL_PROTECTION_DIS;
1027 WREG32_SMC(GENERAL_PWRMGT, tmp);
1028 }
1029}
1030
1031static void ci_enable_auto_throttle_source(struct radeon_device *rdev,
1032 enum radeon_dpm_auto_throttle_src source,
1033 bool enable)
1034{
1035 struct ci_power_info *pi = ci_get_pi(rdev);
1036
1037 if (enable) {
1038 if (!(pi->active_auto_throttle_sources & (1 << source))) {
1039 pi->active_auto_throttle_sources |= 1 << source;
1040 ci_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
1041 }
1042 } else {
1043 if (pi->active_auto_throttle_sources & (1 << source)) {
1044 pi->active_auto_throttle_sources &= ~(1 << source);
1045 ci_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
1046 }
1047 }
1048}
1049
1050static void ci_enable_vr_hot_gpio_interrupt(struct radeon_device *rdev)
1051{
1052 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_REGULATOR_HOT)
1053 ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableVRHotGPIOInterrupt);
1054}
1055
1056static int ci_unfreeze_sclk_mclk_dpm(struct radeon_device *rdev)
1057{
1058 struct ci_power_info *pi = ci_get_pi(rdev);
1059 PPSMC_Result smc_result;
1060
1061 if (!pi->need_update_smu7_dpm_table)
1062 return 0;
1063
1064 if ((!pi->sclk_dpm_key_disabled) &&
1065 (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK))) {
1066 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_SCLKDPM_UnfreezeLevel);
1067 if (smc_result != PPSMC_Result_OK)
1068 return -EINVAL;
1069 }
1070
1071 if ((!pi->mclk_dpm_key_disabled) &&
1072 (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
1073 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_UnfreezeLevel);
1074 if (smc_result != PPSMC_Result_OK)
1075 return -EINVAL;
1076 }
1077
1078 pi->need_update_smu7_dpm_table = 0;
1079 return 0;
1080}
1081
1082static int ci_enable_sclk_mclk_dpm(struct radeon_device *rdev, bool enable)
1083{
1084 struct ci_power_info *pi = ci_get_pi(rdev);
1085 PPSMC_Result smc_result;
1086
1087 if (enable) {
1088 if (!pi->sclk_dpm_key_disabled) {
1089 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DPM_Enable);
1090 if (smc_result != PPSMC_Result_OK)
1091 return -EINVAL;
1092 }
1093
1094 if (!pi->mclk_dpm_key_disabled) {
1095 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_Enable);
1096 if (smc_result != PPSMC_Result_OK)
1097 return -EINVAL;
1098
1099 WREG32_P(MC_SEQ_CNTL_3, CAC_EN, ~CAC_EN);
1100
1101 WREG32_SMC(LCAC_MC0_CNTL, 0x05);
1102 WREG32_SMC(LCAC_MC1_CNTL, 0x05);
1103 WREG32_SMC(LCAC_CPL_CNTL, 0x100005);
1104
1105 udelay(10);
1106
1107 WREG32_SMC(LCAC_MC0_CNTL, 0x400005);
1108 WREG32_SMC(LCAC_MC1_CNTL, 0x400005);
1109 WREG32_SMC(LCAC_CPL_CNTL, 0x500005);
1110 }
1111 } else {
1112 if (!pi->sclk_dpm_key_disabled) {
1113 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DPM_Disable);
1114 if (smc_result != PPSMC_Result_OK)
1115 return -EINVAL;
1116 }
1117
1118 if (!pi->mclk_dpm_key_disabled) {
1119 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_Disable);
1120 if (smc_result != PPSMC_Result_OK)
1121 return -EINVAL;
1122 }
1123 }
1124
1125 return 0;
1126}
1127
1128static int ci_start_dpm(struct radeon_device *rdev)
1129{
1130 struct ci_power_info *pi = ci_get_pi(rdev);
1131 PPSMC_Result smc_result;
1132 int ret;
1133 u32 tmp;
1134
1135 tmp = RREG32_SMC(GENERAL_PWRMGT);
1136 tmp |= GLOBAL_PWRMGT_EN;
1137 WREG32_SMC(GENERAL_PWRMGT, tmp);
1138
1139 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
1140 tmp |= DYNAMIC_PM_EN;
1141 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
1142
1143 ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, VoltageChangeTimeout), 0x1000);
1144
1145 WREG32_P(BIF_LNCNT_RESET, 0, ~RESET_LNCNT_EN);
1146
1147 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Voltage_Cntl_Enable);
1148 if (smc_result != PPSMC_Result_OK)
1149 return -EINVAL;
1150
1151 ret = ci_enable_sclk_mclk_dpm(rdev, true);
1152 if (ret)
1153 return ret;
1154
1155 if (!pi->pcie_dpm_key_disabled) {
1156 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PCIeDPM_Enable);
1157 if (smc_result != PPSMC_Result_OK)
1158 return -EINVAL;
1159 }
1160
1161 return 0;
1162}
1163
1164static int ci_freeze_sclk_mclk_dpm(struct radeon_device *rdev)
1165{
1166 struct ci_power_info *pi = ci_get_pi(rdev);
1167 PPSMC_Result smc_result;
1168
1169 if (!pi->need_update_smu7_dpm_table)
1170 return 0;
1171
1172 if ((!pi->sclk_dpm_key_disabled) &&
1173 (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK))) {
1174 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_SCLKDPM_FreezeLevel);
1175 if (smc_result != PPSMC_Result_OK)
1176 return -EINVAL;
1177 }
1178
1179 if ((!pi->mclk_dpm_key_disabled) &&
1180 (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
1181 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_FreezeLevel);
1182 if (smc_result != PPSMC_Result_OK)
1183 return -EINVAL;
1184 }
1185
1186 return 0;
1187}
1188
1189static int ci_stop_dpm(struct radeon_device *rdev)
1190{
1191 struct ci_power_info *pi = ci_get_pi(rdev);
1192 PPSMC_Result smc_result;
1193 int ret;
1194 u32 tmp;
1195
1196 tmp = RREG32_SMC(GENERAL_PWRMGT);
1197 tmp &= ~GLOBAL_PWRMGT_EN;
1198 WREG32_SMC(GENERAL_PWRMGT, tmp);
1199
Alex Deuchered963772014-07-08 18:25:25 -04001200 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04001201 tmp &= ~DYNAMIC_PM_EN;
1202 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
1203
1204 if (!pi->pcie_dpm_key_disabled) {
1205 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PCIeDPM_Disable);
1206 if (smc_result != PPSMC_Result_OK)
1207 return -EINVAL;
1208 }
1209
1210 ret = ci_enable_sclk_mclk_dpm(rdev, false);
1211 if (ret)
1212 return ret;
1213
1214 smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Voltage_Cntl_Disable);
1215 if (smc_result != PPSMC_Result_OK)
1216 return -EINVAL;
1217
1218 return 0;
1219}
1220
1221static void ci_enable_sclk_control(struct radeon_device *rdev, bool enable)
1222{
1223 u32 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
1224
1225 if (enable)
1226 tmp &= ~SCLK_PWRMGT_OFF;
1227 else
1228 tmp |= SCLK_PWRMGT_OFF;
1229 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
1230}
1231
1232#if 0
1233static int ci_notify_hw_of_power_source(struct radeon_device *rdev,
1234 bool ac_power)
1235{
1236 struct ci_power_info *pi = ci_get_pi(rdev);
1237 struct radeon_cac_tdp_table *cac_tdp_table =
1238 rdev->pm.dpm.dyn_state.cac_tdp_table;
1239 u32 power_limit;
1240
1241 if (ac_power)
1242 power_limit = (u32)(cac_tdp_table->maximum_power_delivery_limit * 256);
1243 else
1244 power_limit = (u32)(cac_tdp_table->battery_power_limit * 256);
1245
1246 ci_set_power_limit(rdev, power_limit);
1247
1248 if (pi->caps_automatic_dc_transition) {
1249 if (ac_power)
1250 ci_send_msg_to_smc(rdev, PPSMC_MSG_RunningOnAC);
1251 else
1252 ci_send_msg_to_smc(rdev, PPSMC_MSG_Remove_DC_Clamp);
1253 }
1254
1255 return 0;
1256}
1257#endif
1258
1259static PPSMC_Result ci_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
1260 PPSMC_Msg msg, u32 parameter)
1261{
1262 WREG32(SMC_MSG_ARG_0, parameter);
1263 return ci_send_msg_to_smc(rdev, msg);
1264}
1265
1266static PPSMC_Result ci_send_msg_to_smc_return_parameter(struct radeon_device *rdev,
1267 PPSMC_Msg msg, u32 *parameter)
1268{
1269 PPSMC_Result smc_result;
1270
1271 smc_result = ci_send_msg_to_smc(rdev, msg);
1272
1273 if ((smc_result == PPSMC_Result_OK) && parameter)
1274 *parameter = RREG32(SMC_MSG_ARG_0);
1275
1276 return smc_result;
1277}
1278
1279static int ci_dpm_force_state_sclk(struct radeon_device *rdev, u32 n)
1280{
1281 struct ci_power_info *pi = ci_get_pi(rdev);
1282
1283 if (!pi->sclk_dpm_key_disabled) {
1284 PPSMC_Result smc_result =
Alex Deucher1c522792014-11-07 12:06:56 -05001285 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SCLKDPM_SetEnabledMask, 1 << n);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04001286 if (smc_result != PPSMC_Result_OK)
1287 return -EINVAL;
1288 }
1289
1290 return 0;
1291}
1292
1293static int ci_dpm_force_state_mclk(struct radeon_device *rdev, u32 n)
1294{
1295 struct ci_power_info *pi = ci_get_pi(rdev);
1296
1297 if (!pi->mclk_dpm_key_disabled) {
1298 PPSMC_Result smc_result =
Alex Deucher1c522792014-11-07 12:06:56 -05001299 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_MCLKDPM_SetEnabledMask, 1 << n);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04001300 if (smc_result != PPSMC_Result_OK)
1301 return -EINVAL;
1302 }
1303
1304 return 0;
1305}
1306
1307static int ci_dpm_force_state_pcie(struct radeon_device *rdev, u32 n)
1308{
1309 struct ci_power_info *pi = ci_get_pi(rdev);
1310
1311 if (!pi->pcie_dpm_key_disabled) {
1312 PPSMC_Result smc_result =
1313 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_PCIeDPM_ForceLevel, n);
1314 if (smc_result != PPSMC_Result_OK)
1315 return -EINVAL;
1316 }
1317
1318 return 0;
1319}
1320
1321static int ci_set_power_limit(struct radeon_device *rdev, u32 n)
1322{
1323 struct ci_power_info *pi = ci_get_pi(rdev);
1324
1325 if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_PkgPwrLimit) {
1326 PPSMC_Result smc_result =
1327 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_PkgPwrSetLimit, n);
1328 if (smc_result != PPSMC_Result_OK)
1329 return -EINVAL;
1330 }
1331
1332 return 0;
1333}
1334
1335static int ci_set_overdrive_target_tdp(struct radeon_device *rdev,
1336 u32 target_tdp)
1337{
1338 PPSMC_Result smc_result =
1339 ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_OverDriveSetTargetTdp, target_tdp);
1340 if (smc_result != PPSMC_Result_OK)
1341 return -EINVAL;
1342 return 0;
1343}
1344
1345static int ci_set_boot_state(struct radeon_device *rdev)
1346{
1347 return ci_enable_sclk_mclk_dpm(rdev, false);
1348}
1349
1350static u32 ci_get_average_sclk_freq(struct radeon_device *rdev)
1351{
1352 u32 sclk_freq;
1353 PPSMC_Result smc_result =
1354 ci_send_msg_to_smc_return_parameter(rdev,
1355 PPSMC_MSG_API_GetSclkFrequency,
1356 &sclk_freq);
1357 if (smc_result != PPSMC_Result_OK)
1358 sclk_freq = 0;
1359
1360 return sclk_freq;
1361}
1362
1363static u32 ci_get_average_mclk_freq(struct radeon_device *rdev)
1364{
1365 u32 mclk_freq;
1366 PPSMC_Result smc_result =
1367 ci_send_msg_to_smc_return_parameter(rdev,
1368 PPSMC_MSG_API_GetMclkFrequency,
1369 &mclk_freq);
1370 if (smc_result != PPSMC_Result_OK)
1371 mclk_freq = 0;
1372
1373 return mclk_freq;
1374}
1375
1376static void ci_dpm_start_smc(struct radeon_device *rdev)
1377{
1378 int i;
1379
1380 ci_program_jump_on_start(rdev);
1381 ci_start_smc_clock(rdev);
1382 ci_start_smc(rdev);
1383 for (i = 0; i < rdev->usec_timeout; i++) {
1384 if (RREG32_SMC(FIRMWARE_FLAGS) & INTERRUPTS_ENABLED)
1385 break;
1386 }
1387}
1388
1389static void ci_dpm_stop_smc(struct radeon_device *rdev)
1390{
1391 ci_reset_smc(rdev);
1392 ci_stop_smc_clock(rdev);
1393}
1394
1395static int ci_process_firmware_header(struct radeon_device *rdev)
1396{
1397 struct ci_power_info *pi = ci_get_pi(rdev);
1398 u32 tmp;
1399 int ret;
1400
1401 ret = ci_read_smc_sram_dword(rdev,
1402 SMU7_FIRMWARE_HEADER_LOCATION +
1403 offsetof(SMU7_Firmware_Header, DpmTable),
1404 &tmp, pi->sram_end);
1405 if (ret)
1406 return ret;
1407
1408 pi->dpm_table_start = tmp;
1409
1410 ret = ci_read_smc_sram_dword(rdev,
1411 SMU7_FIRMWARE_HEADER_LOCATION +
1412 offsetof(SMU7_Firmware_Header, SoftRegisters),
1413 &tmp, pi->sram_end);
1414 if (ret)
1415 return ret;
1416
1417 pi->soft_regs_start = tmp;
1418
1419 ret = ci_read_smc_sram_dword(rdev,
1420 SMU7_FIRMWARE_HEADER_LOCATION +
1421 offsetof(SMU7_Firmware_Header, mcRegisterTable),
1422 &tmp, pi->sram_end);
1423 if (ret)
1424 return ret;
1425
1426 pi->mc_reg_table_start = tmp;
1427
1428 ret = ci_read_smc_sram_dword(rdev,
1429 SMU7_FIRMWARE_HEADER_LOCATION +
1430 offsetof(SMU7_Firmware_Header, FanTable),
1431 &tmp, pi->sram_end);
1432 if (ret)
1433 return ret;
1434
1435 pi->fan_table_start = tmp;
1436
1437 ret = ci_read_smc_sram_dword(rdev,
1438 SMU7_FIRMWARE_HEADER_LOCATION +
1439 offsetof(SMU7_Firmware_Header, mcArbDramTimingTable),
1440 &tmp, pi->sram_end);
1441 if (ret)
1442 return ret;
1443
1444 pi->arb_table_start = tmp;
1445
1446 return 0;
1447}
1448
1449static void ci_read_clock_registers(struct radeon_device *rdev)
1450{
1451 struct ci_power_info *pi = ci_get_pi(rdev);
1452
1453 pi->clock_registers.cg_spll_func_cntl =
1454 RREG32_SMC(CG_SPLL_FUNC_CNTL);
1455 pi->clock_registers.cg_spll_func_cntl_2 =
1456 RREG32_SMC(CG_SPLL_FUNC_CNTL_2);
1457 pi->clock_registers.cg_spll_func_cntl_3 =
1458 RREG32_SMC(CG_SPLL_FUNC_CNTL_3);
1459 pi->clock_registers.cg_spll_func_cntl_4 =
1460 RREG32_SMC(CG_SPLL_FUNC_CNTL_4);
1461 pi->clock_registers.cg_spll_spread_spectrum =
1462 RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM);
1463 pi->clock_registers.cg_spll_spread_spectrum_2 =
1464 RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM_2);
1465 pi->clock_registers.dll_cntl = RREG32(DLL_CNTL);
1466 pi->clock_registers.mclk_pwrmgt_cntl = RREG32(MCLK_PWRMGT_CNTL);
1467 pi->clock_registers.mpll_ad_func_cntl = RREG32(MPLL_AD_FUNC_CNTL);
1468 pi->clock_registers.mpll_dq_func_cntl = RREG32(MPLL_DQ_FUNC_CNTL);
1469 pi->clock_registers.mpll_func_cntl = RREG32(MPLL_FUNC_CNTL);
1470 pi->clock_registers.mpll_func_cntl_1 = RREG32(MPLL_FUNC_CNTL_1);
1471 pi->clock_registers.mpll_func_cntl_2 = RREG32(MPLL_FUNC_CNTL_2);
1472 pi->clock_registers.mpll_ss1 = RREG32(MPLL_SS1);
1473 pi->clock_registers.mpll_ss2 = RREG32(MPLL_SS2);
1474}
1475
1476static void ci_init_sclk_t(struct radeon_device *rdev)
1477{
1478 struct ci_power_info *pi = ci_get_pi(rdev);
1479
1480 pi->low_sclk_interrupt_t = 0;
1481}
1482
1483static void ci_enable_thermal_protection(struct radeon_device *rdev,
1484 bool enable)
1485{
1486 u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
1487
1488 if (enable)
1489 tmp &= ~THERMAL_PROTECTION_DIS;
1490 else
1491 tmp |= THERMAL_PROTECTION_DIS;
1492 WREG32_SMC(GENERAL_PWRMGT, tmp);
1493}
1494
1495static void ci_enable_acpi_power_management(struct radeon_device *rdev)
1496{
1497 u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
1498
1499 tmp |= STATIC_PM_EN;
1500
1501 WREG32_SMC(GENERAL_PWRMGT, tmp);
1502}
1503
1504#if 0
1505static int ci_enter_ulp_state(struct radeon_device *rdev)
1506{
1507
1508 WREG32(SMC_MESSAGE_0, PPSMC_MSG_SwitchToMinimumPower);
1509
1510 udelay(25000);
1511
1512 return 0;
1513}
1514
1515static int ci_exit_ulp_state(struct radeon_device *rdev)
1516{
1517 int i;
1518
1519 WREG32(SMC_MESSAGE_0, PPSMC_MSG_ResumeFromMinimumPower);
1520
1521 udelay(7000);
1522
1523 for (i = 0; i < rdev->usec_timeout; i++) {
1524 if (RREG32(SMC_RESP_0) == 1)
1525 break;
1526 udelay(1000);
1527 }
1528
1529 return 0;
1530}
1531#endif
1532
1533static int ci_notify_smc_display_change(struct radeon_device *rdev,
1534 bool has_display)
1535{
1536 PPSMC_Msg msg = has_display ? PPSMC_MSG_HasDisplay : PPSMC_MSG_NoDisplay;
1537
1538 return (ci_send_msg_to_smc(rdev, msg) == PPSMC_Result_OK) ? 0 : -EINVAL;
1539}
1540
1541static int ci_enable_ds_master_switch(struct radeon_device *rdev,
1542 bool enable)
1543{
1544 struct ci_power_info *pi = ci_get_pi(rdev);
1545
1546 if (enable) {
1547 if (pi->caps_sclk_ds) {
1548 if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_ON) != PPSMC_Result_OK)
1549 return -EINVAL;
1550 } else {
1551 if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_OFF) != PPSMC_Result_OK)
1552 return -EINVAL;
1553 }
1554 } else {
1555 if (pi->caps_sclk_ds) {
1556 if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_OFF) != PPSMC_Result_OK)
1557 return -EINVAL;
1558 }
1559 }
1560
1561 return 0;
1562}
1563
1564static void ci_program_display_gap(struct radeon_device *rdev)
1565{
1566 u32 tmp = RREG32_SMC(CG_DISPLAY_GAP_CNTL);
1567 u32 pre_vbi_time_in_us;
1568 u32 frame_time_in_us;
1569 u32 ref_clock = rdev->clock.spll.reference_freq;
1570 u32 refresh_rate = r600_dpm_get_vrefresh(rdev);
1571 u32 vblank_time = r600_dpm_get_vblank_time(rdev);
1572
1573 tmp &= ~DISP_GAP_MASK;
1574 if (rdev->pm.dpm.new_active_crtc_count > 0)
1575 tmp |= DISP_GAP(R600_PM_DISPLAY_GAP_VBLANK_OR_WM);
1576 else
1577 tmp |= DISP_GAP(R600_PM_DISPLAY_GAP_IGNORE);
1578 WREG32_SMC(CG_DISPLAY_GAP_CNTL, tmp);
1579
1580 if (refresh_rate == 0)
1581 refresh_rate = 60;
1582 if (vblank_time == 0xffffffff)
1583 vblank_time = 500;
1584 frame_time_in_us = 1000000 / refresh_rate;
1585 pre_vbi_time_in_us =
1586 frame_time_in_us - 200 - vblank_time;
1587 tmp = pre_vbi_time_in_us * (ref_clock / 100);
1588
1589 WREG32_SMC(CG_DISPLAY_GAP_CNTL2, tmp);
1590 ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, PreVBlankGap), 0x64);
1591 ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, VBlankTimeout), (frame_time_in_us - pre_vbi_time_in_us));
1592
1593
1594 ci_notify_smc_display_change(rdev, (rdev->pm.dpm.new_active_crtc_count == 1));
1595
1596}
1597
1598static void ci_enable_spread_spectrum(struct radeon_device *rdev, bool enable)
1599{
1600 struct ci_power_info *pi = ci_get_pi(rdev);
1601 u32 tmp;
1602
1603 if (enable) {
1604 if (pi->caps_sclk_ss_support) {
1605 tmp = RREG32_SMC(GENERAL_PWRMGT);
1606 tmp |= DYN_SPREAD_SPECTRUM_EN;
1607 WREG32_SMC(GENERAL_PWRMGT, tmp);
1608 }
1609 } else {
1610 tmp = RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM);
1611 tmp &= ~SSEN;
1612 WREG32_SMC(CG_SPLL_SPREAD_SPECTRUM, tmp);
1613
1614 tmp = RREG32_SMC(GENERAL_PWRMGT);
1615 tmp &= ~DYN_SPREAD_SPECTRUM_EN;
1616 WREG32_SMC(GENERAL_PWRMGT, tmp);
1617 }
1618}
1619
1620static void ci_program_sstp(struct radeon_device *rdev)
1621{
1622 WREG32_SMC(CG_SSP, (SSTU(R600_SSTU_DFLT) | SST(R600_SST_DFLT)));
1623}
1624
1625static void ci_enable_display_gap(struct radeon_device *rdev)
1626{
1627 u32 tmp = RREG32_SMC(CG_DISPLAY_GAP_CNTL);
1628
1629 tmp &= ~(DISP_GAP_MASK | DISP_GAP_MCHG_MASK);
1630 tmp |= (DISP_GAP(R600_PM_DISPLAY_GAP_IGNORE) |
1631 DISP_GAP_MCHG(R600_PM_DISPLAY_GAP_VBLANK));
1632
1633 WREG32_SMC(CG_DISPLAY_GAP_CNTL, tmp);
1634}
1635
1636static void ci_program_vc(struct radeon_device *rdev)
1637{
1638 u32 tmp;
1639
1640 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
1641 tmp &= ~(RESET_SCLK_CNT | RESET_BUSY_CNT);
1642 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
1643
1644 WREG32_SMC(CG_FTV_0, CISLANDS_VRC_DFLT0);
1645 WREG32_SMC(CG_FTV_1, CISLANDS_VRC_DFLT1);
1646 WREG32_SMC(CG_FTV_2, CISLANDS_VRC_DFLT2);
1647 WREG32_SMC(CG_FTV_3, CISLANDS_VRC_DFLT3);
1648 WREG32_SMC(CG_FTV_4, CISLANDS_VRC_DFLT4);
1649 WREG32_SMC(CG_FTV_5, CISLANDS_VRC_DFLT5);
1650 WREG32_SMC(CG_FTV_6, CISLANDS_VRC_DFLT6);
1651 WREG32_SMC(CG_FTV_7, CISLANDS_VRC_DFLT7);
1652}
1653
1654static void ci_clear_vc(struct radeon_device *rdev)
1655{
1656 u32 tmp;
1657
1658 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
1659 tmp |= (RESET_SCLK_CNT | RESET_BUSY_CNT);
1660 WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
1661
1662 WREG32_SMC(CG_FTV_0, 0);
1663 WREG32_SMC(CG_FTV_1, 0);
1664 WREG32_SMC(CG_FTV_2, 0);
1665 WREG32_SMC(CG_FTV_3, 0);
1666 WREG32_SMC(CG_FTV_4, 0);
1667 WREG32_SMC(CG_FTV_5, 0);
1668 WREG32_SMC(CG_FTV_6, 0);
1669 WREG32_SMC(CG_FTV_7, 0);
1670}
1671
1672static int ci_upload_firmware(struct radeon_device *rdev)
1673{
1674 struct ci_power_info *pi = ci_get_pi(rdev);
1675 int i, ret;
1676
1677 for (i = 0; i < rdev->usec_timeout; i++) {
1678 if (RREG32_SMC(RCU_UC_EVENTS) & BOOT_SEQ_DONE)
1679 break;
1680 }
1681 WREG32_SMC(SMC_SYSCON_MISC_CNTL, 1);
1682
1683 ci_stop_smc_clock(rdev);
1684 ci_reset_smc(rdev);
1685
1686 ret = ci_load_smc_ucode(rdev, pi->sram_end);
1687
1688 return ret;
1689
1690}
1691
1692static int ci_get_svi2_voltage_table(struct radeon_device *rdev,
1693 struct radeon_clock_voltage_dependency_table *voltage_dependency_table,
1694 struct atom_voltage_table *voltage_table)
1695{
1696 u32 i;
1697
1698 if (voltage_dependency_table == NULL)
1699 return -EINVAL;
1700
1701 voltage_table->mask_low = 0;
1702 voltage_table->phase_delay = 0;
1703
1704 voltage_table->count = voltage_dependency_table->count;
1705 for (i = 0; i < voltage_table->count; i++) {
1706 voltage_table->entries[i].value = voltage_dependency_table->entries[i].v;
1707 voltage_table->entries[i].smio_low = 0;
1708 }
1709
1710 return 0;
1711}
1712
1713static int ci_construct_voltage_tables(struct radeon_device *rdev)
1714{
1715 struct ci_power_info *pi = ci_get_pi(rdev);
1716 int ret;
1717
1718 if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
1719 ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_VDDC,
1720 VOLTAGE_OBJ_GPIO_LUT,
1721 &pi->vddc_voltage_table);
1722 if (ret)
1723 return ret;
1724 } else if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
1725 ret = ci_get_svi2_voltage_table(rdev,
1726 &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
1727 &pi->vddc_voltage_table);
1728 if (ret)
1729 return ret;
1730 }
1731
1732 if (pi->vddc_voltage_table.count > SMU7_MAX_LEVELS_VDDC)
1733 si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_VDDC,
1734 &pi->vddc_voltage_table);
1735
1736 if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
1737 ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_VDDCI,
1738 VOLTAGE_OBJ_GPIO_LUT,
1739 &pi->vddci_voltage_table);
1740 if (ret)
1741 return ret;
1742 } else if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
1743 ret = ci_get_svi2_voltage_table(rdev,
1744 &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
1745 &pi->vddci_voltage_table);
1746 if (ret)
1747 return ret;
1748 }
1749
1750 if (pi->vddci_voltage_table.count > SMU7_MAX_LEVELS_VDDCI)
1751 si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_VDDCI,
1752 &pi->vddci_voltage_table);
1753
1754 if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
1755 ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_MVDDC,
1756 VOLTAGE_OBJ_GPIO_LUT,
1757 &pi->mvdd_voltage_table);
1758 if (ret)
1759 return ret;
1760 } else if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
1761 ret = ci_get_svi2_voltage_table(rdev,
1762 &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk,
1763 &pi->mvdd_voltage_table);
1764 if (ret)
1765 return ret;
1766 }
1767
1768 if (pi->mvdd_voltage_table.count > SMU7_MAX_LEVELS_MVDD)
1769 si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_MVDD,
1770 &pi->mvdd_voltage_table);
1771
1772 return 0;
1773}
1774
1775static void ci_populate_smc_voltage_table(struct radeon_device *rdev,
1776 struct atom_voltage_table_entry *voltage_table,
1777 SMU7_Discrete_VoltageLevel *smc_voltage_table)
1778{
1779 int ret;
1780
1781 ret = ci_get_std_voltage_value_sidd(rdev, voltage_table,
1782 &smc_voltage_table->StdVoltageHiSidd,
1783 &smc_voltage_table->StdVoltageLoSidd);
1784
1785 if (ret) {
1786 smc_voltage_table->StdVoltageHiSidd = voltage_table->value * VOLTAGE_SCALE;
1787 smc_voltage_table->StdVoltageLoSidd = voltage_table->value * VOLTAGE_SCALE;
1788 }
1789
1790 smc_voltage_table->Voltage = cpu_to_be16(voltage_table->value * VOLTAGE_SCALE);
1791 smc_voltage_table->StdVoltageHiSidd =
1792 cpu_to_be16(smc_voltage_table->StdVoltageHiSidd);
1793 smc_voltage_table->StdVoltageLoSidd =
1794 cpu_to_be16(smc_voltage_table->StdVoltageLoSidd);
1795}
1796
1797static int ci_populate_smc_vddc_table(struct radeon_device *rdev,
1798 SMU7_Discrete_DpmTable *table)
1799{
1800 struct ci_power_info *pi = ci_get_pi(rdev);
1801 unsigned int count;
1802
1803 table->VddcLevelCount = pi->vddc_voltage_table.count;
1804 for (count = 0; count < table->VddcLevelCount; count++) {
1805 ci_populate_smc_voltage_table(rdev,
1806 &pi->vddc_voltage_table.entries[count],
1807 &table->VddcLevel[count]);
1808
1809 if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
1810 table->VddcLevel[count].Smio |=
1811 pi->vddc_voltage_table.entries[count].smio_low;
1812 else
1813 table->VddcLevel[count].Smio = 0;
1814 }
1815 table->VddcLevelCount = cpu_to_be32(table->VddcLevelCount);
1816
1817 return 0;
1818}
1819
1820static int ci_populate_smc_vddci_table(struct radeon_device *rdev,
1821 SMU7_Discrete_DpmTable *table)
1822{
1823 unsigned int count;
1824 struct ci_power_info *pi = ci_get_pi(rdev);
1825
1826 table->VddciLevelCount = pi->vddci_voltage_table.count;
1827 for (count = 0; count < table->VddciLevelCount; count++) {
1828 ci_populate_smc_voltage_table(rdev,
1829 &pi->vddci_voltage_table.entries[count],
1830 &table->VddciLevel[count]);
1831
1832 if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
1833 table->VddciLevel[count].Smio |=
1834 pi->vddci_voltage_table.entries[count].smio_low;
1835 else
1836 table->VddciLevel[count].Smio = 0;
1837 }
1838 table->VddciLevelCount = cpu_to_be32(table->VddciLevelCount);
1839
1840 return 0;
1841}
1842
1843static int ci_populate_smc_mvdd_table(struct radeon_device *rdev,
1844 SMU7_Discrete_DpmTable *table)
1845{
1846 struct ci_power_info *pi = ci_get_pi(rdev);
1847 unsigned int count;
1848
1849 table->MvddLevelCount = pi->mvdd_voltage_table.count;
1850 for (count = 0; count < table->MvddLevelCount; count++) {
1851 ci_populate_smc_voltage_table(rdev,
1852 &pi->mvdd_voltage_table.entries[count],
1853 &table->MvddLevel[count]);
1854
1855 if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
1856 table->MvddLevel[count].Smio |=
1857 pi->mvdd_voltage_table.entries[count].smio_low;
1858 else
1859 table->MvddLevel[count].Smio = 0;
1860 }
1861 table->MvddLevelCount = cpu_to_be32(table->MvddLevelCount);
1862
1863 return 0;
1864}
1865
1866static int ci_populate_smc_voltage_tables(struct radeon_device *rdev,
1867 SMU7_Discrete_DpmTable *table)
1868{
1869 int ret;
1870
1871 ret = ci_populate_smc_vddc_table(rdev, table);
1872 if (ret)
1873 return ret;
1874
1875 ret = ci_populate_smc_vddci_table(rdev, table);
1876 if (ret)
1877 return ret;
1878
1879 ret = ci_populate_smc_mvdd_table(rdev, table);
1880 if (ret)
1881 return ret;
1882
1883 return 0;
1884}
1885
1886static int ci_populate_mvdd_value(struct radeon_device *rdev, u32 mclk,
1887 SMU7_Discrete_VoltageLevel *voltage)
1888{
1889 struct ci_power_info *pi = ci_get_pi(rdev);
1890 u32 i = 0;
1891
1892 if (pi->mvdd_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
1893 for (i = 0; i < rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.count; i++) {
1894 if (mclk <= rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.entries[i].clk) {
1895 voltage->Voltage = pi->mvdd_voltage_table.entries[i].value;
1896 break;
1897 }
1898 }
1899
1900 if (i >= rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.count)
1901 return -EINVAL;
1902 }
1903
1904 return -EINVAL;
1905}
1906
1907static int ci_get_std_voltage_value_sidd(struct radeon_device *rdev,
1908 struct atom_voltage_table_entry *voltage_table,
1909 u16 *std_voltage_hi_sidd, u16 *std_voltage_lo_sidd)
1910{
1911 u16 v_index, idx;
1912 bool voltage_found = false;
1913 *std_voltage_hi_sidd = voltage_table->value * VOLTAGE_SCALE;
1914 *std_voltage_lo_sidd = voltage_table->value * VOLTAGE_SCALE;
1915
1916 if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries == NULL)
1917 return -EINVAL;
1918
1919 if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries) {
1920 for (v_index = 0; (u32)v_index < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; v_index++) {
1921 if (voltage_table->value ==
1922 rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[v_index].v) {
1923 voltage_found = true;
1924 if ((u32)v_index < rdev->pm.dpm.dyn_state.cac_leakage_table.count)
1925 idx = v_index;
1926 else
1927 idx = rdev->pm.dpm.dyn_state.cac_leakage_table.count - 1;
1928 *std_voltage_lo_sidd =
1929 rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].vddc * VOLTAGE_SCALE;
1930 *std_voltage_hi_sidd =
1931 rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].leakage * VOLTAGE_SCALE;
1932 break;
1933 }
1934 }
1935
1936 if (!voltage_found) {
1937 for (v_index = 0; (u32)v_index < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; v_index++) {
1938 if (voltage_table->value <=
1939 rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[v_index].v) {
1940 voltage_found = true;
1941 if ((u32)v_index < rdev->pm.dpm.dyn_state.cac_leakage_table.count)
1942 idx = v_index;
1943 else
1944 idx = rdev->pm.dpm.dyn_state.cac_leakage_table.count - 1;
1945 *std_voltage_lo_sidd =
1946 rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].vddc * VOLTAGE_SCALE;
1947 *std_voltage_hi_sidd =
1948 rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].leakage * VOLTAGE_SCALE;
1949 break;
1950 }
1951 }
1952 }
1953 }
1954
1955 return 0;
1956}
1957
1958static void ci_populate_phase_value_based_on_sclk(struct radeon_device *rdev,
1959 const struct radeon_phase_shedding_limits_table *limits,
1960 u32 sclk,
1961 u32 *phase_shedding)
1962{
1963 unsigned int i;
1964
1965 *phase_shedding = 1;
1966
1967 for (i = 0; i < limits->count; i++) {
1968 if (sclk < limits->entries[i].sclk) {
1969 *phase_shedding = i;
1970 break;
1971 }
1972 }
1973}
1974
1975static void ci_populate_phase_value_based_on_mclk(struct radeon_device *rdev,
1976 const struct radeon_phase_shedding_limits_table *limits,
1977 u32 mclk,
1978 u32 *phase_shedding)
1979{
1980 unsigned int i;
1981
1982 *phase_shedding = 1;
1983
1984 for (i = 0; i < limits->count; i++) {
1985 if (mclk < limits->entries[i].mclk) {
1986 *phase_shedding = i;
1987 break;
1988 }
1989 }
1990}
1991
1992static int ci_init_arb_table_index(struct radeon_device *rdev)
1993{
1994 struct ci_power_info *pi = ci_get_pi(rdev);
1995 u32 tmp;
1996 int ret;
1997
1998 ret = ci_read_smc_sram_dword(rdev, pi->arb_table_start,
1999 &tmp, pi->sram_end);
2000 if (ret)
2001 return ret;
2002
2003 tmp &= 0x00FFFFFF;
2004 tmp |= MC_CG_ARB_FREQ_F1 << 24;
2005
2006 return ci_write_smc_sram_dword(rdev, pi->arb_table_start,
2007 tmp, pi->sram_end);
2008}
2009
2010static int ci_get_dependency_volt_by_clk(struct radeon_device *rdev,
2011 struct radeon_clock_voltage_dependency_table *allowed_clock_voltage_table,
2012 u32 clock, u32 *voltage)
2013{
2014 u32 i = 0;
2015
2016 if (allowed_clock_voltage_table->count == 0)
2017 return -EINVAL;
2018
2019 for (i = 0; i < allowed_clock_voltage_table->count; i++) {
2020 if (allowed_clock_voltage_table->entries[i].clk >= clock) {
2021 *voltage = allowed_clock_voltage_table->entries[i].v;
2022 return 0;
2023 }
2024 }
2025
2026 *voltage = allowed_clock_voltage_table->entries[i-1].v;
2027
2028 return 0;
2029}
2030
2031static u8 ci_get_sleep_divider_id_from_clock(struct radeon_device *rdev,
2032 u32 sclk, u32 min_sclk_in_sr)
2033{
2034 u32 i;
2035 u32 tmp;
2036 u32 min = (min_sclk_in_sr > CISLAND_MINIMUM_ENGINE_CLOCK) ?
2037 min_sclk_in_sr : CISLAND_MINIMUM_ENGINE_CLOCK;
2038
2039 if (sclk < min)
2040 return 0;
2041
2042 for (i = CISLAND_MAX_DEEPSLEEP_DIVIDER_ID; ; i--) {
2043 tmp = sclk / (1 << i);
2044 if (tmp >= min || i == 0)
2045 break;
2046 }
2047
2048 return (u8)i;
2049}
2050
2051static int ci_initial_switch_from_arb_f0_to_f1(struct radeon_device *rdev)
2052{
2053 return ni_copy_and_switch_arb_sets(rdev, MC_CG_ARB_FREQ_F0, MC_CG_ARB_FREQ_F1);
2054}
2055
2056static int ci_reset_to_default(struct radeon_device *rdev)
2057{
2058 return (ci_send_msg_to_smc(rdev, PPSMC_MSG_ResetToDefaults) == PPSMC_Result_OK) ?
2059 0 : -EINVAL;
2060}
2061
2062static int ci_force_switch_to_arb_f0(struct radeon_device *rdev)
2063{
2064 u32 tmp;
2065
2066 tmp = (RREG32_SMC(SMC_SCRATCH9) & 0x0000ff00) >> 8;
2067
2068 if (tmp == MC_CG_ARB_FREQ_F0)
2069 return 0;
2070
2071 return ni_copy_and_switch_arb_sets(rdev, tmp, MC_CG_ARB_FREQ_F0);
2072}
2073
2074static int ci_populate_memory_timing_parameters(struct radeon_device *rdev,
2075 u32 sclk,
2076 u32 mclk,
2077 SMU7_Discrete_MCArbDramTimingTableEntry *arb_regs)
2078{
2079 u32 dram_timing;
2080 u32 dram_timing2;
2081 u32 burst_time;
2082
2083 radeon_atom_set_engine_dram_timings(rdev, sclk, mclk);
2084
2085 dram_timing = RREG32(MC_ARB_DRAM_TIMING);
2086 dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2);
2087 burst_time = RREG32(MC_ARB_BURST_TIME) & STATE0_MASK;
2088
2089 arb_regs->McArbDramTiming = cpu_to_be32(dram_timing);
2090 arb_regs->McArbDramTiming2 = cpu_to_be32(dram_timing2);
2091 arb_regs->McArbBurstTime = (u8)burst_time;
2092
2093 return 0;
2094}
2095
2096static int ci_do_program_memory_timing_parameters(struct radeon_device *rdev)
2097{
2098 struct ci_power_info *pi = ci_get_pi(rdev);
2099 SMU7_Discrete_MCArbDramTimingTable arb_regs;
2100 u32 i, j;
2101 int ret = 0;
2102
2103 memset(&arb_regs, 0, sizeof(SMU7_Discrete_MCArbDramTimingTable));
2104
2105 for (i = 0; i < pi->dpm_table.sclk_table.count; i++) {
2106 for (j = 0; j < pi->dpm_table.mclk_table.count; j++) {
2107 ret = ci_populate_memory_timing_parameters(rdev,
2108 pi->dpm_table.sclk_table.dpm_levels[i].value,
2109 pi->dpm_table.mclk_table.dpm_levels[j].value,
2110 &arb_regs.entries[i][j]);
2111 if (ret)
2112 break;
2113 }
2114 }
2115
2116 if (ret == 0)
2117 ret = ci_copy_bytes_to_smc(rdev,
2118 pi->arb_table_start,
2119 (u8 *)&arb_regs,
2120 sizeof(SMU7_Discrete_MCArbDramTimingTable),
2121 pi->sram_end);
2122
2123 return ret;
2124}
2125
2126static int ci_program_memory_timing_parameters(struct radeon_device *rdev)
2127{
2128 struct ci_power_info *pi = ci_get_pi(rdev);
2129
2130 if (pi->need_update_smu7_dpm_table == 0)
2131 return 0;
2132
2133 return ci_do_program_memory_timing_parameters(rdev);
2134}
2135
2136static void ci_populate_smc_initial_state(struct radeon_device *rdev,
2137 struct radeon_ps *radeon_boot_state)
2138{
2139 struct ci_ps *boot_state = ci_get_ps(radeon_boot_state);
2140 struct ci_power_info *pi = ci_get_pi(rdev);
2141 u32 level = 0;
2142
2143 for (level = 0; level < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; level++) {
2144 if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[level].clk >=
2145 boot_state->performance_levels[0].sclk) {
2146 pi->smc_state_table.GraphicsBootLevel = level;
2147 break;
2148 }
2149 }
2150
2151 for (level = 0; level < rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.count; level++) {
2152 if (rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.entries[level].clk >=
2153 boot_state->performance_levels[0].mclk) {
2154 pi->smc_state_table.MemoryBootLevel = level;
2155 break;
2156 }
2157 }
2158}
2159
2160static u32 ci_get_dpm_level_enable_mask_value(struct ci_single_dpm_table *dpm_table)
2161{
2162 u32 i;
2163 u32 mask_value = 0;
2164
2165 for (i = dpm_table->count; i > 0; i--) {
2166 mask_value = mask_value << 1;
2167 if (dpm_table->dpm_levels[i-1].enabled)
2168 mask_value |= 0x1;
2169 else
2170 mask_value &= 0xFFFFFFFE;
2171 }
2172
2173 return mask_value;
2174}
2175
2176static void ci_populate_smc_link_level(struct radeon_device *rdev,
2177 SMU7_Discrete_DpmTable *table)
2178{
2179 struct ci_power_info *pi = ci_get_pi(rdev);
2180 struct ci_dpm_table *dpm_table = &pi->dpm_table;
2181 u32 i;
2182
2183 for (i = 0; i < dpm_table->pcie_speed_table.count; i++) {
2184 table->LinkLevel[i].PcieGenSpeed =
2185 (u8)dpm_table->pcie_speed_table.dpm_levels[i].value;
2186 table->LinkLevel[i].PcieLaneCount =
2187 r600_encode_pci_lane_width(dpm_table->pcie_speed_table.dpm_levels[i].param1);
2188 table->LinkLevel[i].EnabledForActivity = 1;
2189 table->LinkLevel[i].DownT = cpu_to_be32(5);
2190 table->LinkLevel[i].UpT = cpu_to_be32(30);
2191 }
2192
2193 pi->smc_state_table.LinkLevelCount = (u8)dpm_table->pcie_speed_table.count;
2194 pi->dpm_level_enable_mask.pcie_dpm_enable_mask =
2195 ci_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
2196}
2197
2198static int ci_populate_smc_uvd_level(struct radeon_device *rdev,
2199 SMU7_Discrete_DpmTable *table)
2200{
2201 u32 count;
2202 struct atom_clock_dividers dividers;
2203 int ret = -EINVAL;
2204
2205 table->UvdLevelCount =
2206 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count;
2207
2208 for (count = 0; count < table->UvdLevelCount; count++) {
2209 table->UvdLevel[count].VclkFrequency =
2210 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].vclk;
2211 table->UvdLevel[count].DclkFrequency =
2212 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].dclk;
2213 table->UvdLevel[count].MinVddc =
2214 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
2215 table->UvdLevel[count].MinVddcPhases = 1;
2216
2217 ret = radeon_atom_get_clock_dividers(rdev,
2218 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2219 table->UvdLevel[count].VclkFrequency, false, &dividers);
2220 if (ret)
2221 return ret;
2222
2223 table->UvdLevel[count].VclkDivider = (u8)dividers.post_divider;
2224
2225 ret = radeon_atom_get_clock_dividers(rdev,
2226 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2227 table->UvdLevel[count].DclkFrequency, false, &dividers);
2228 if (ret)
2229 return ret;
2230
2231 table->UvdLevel[count].DclkDivider = (u8)dividers.post_divider;
2232
2233 table->UvdLevel[count].VclkFrequency = cpu_to_be32(table->UvdLevel[count].VclkFrequency);
2234 table->UvdLevel[count].DclkFrequency = cpu_to_be32(table->UvdLevel[count].DclkFrequency);
2235 table->UvdLevel[count].MinVddc = cpu_to_be16(table->UvdLevel[count].MinVddc);
2236 }
2237
2238 return ret;
2239}
2240
2241static int ci_populate_smc_vce_level(struct radeon_device *rdev,
2242 SMU7_Discrete_DpmTable *table)
2243{
2244 u32 count;
2245 struct atom_clock_dividers dividers;
2246 int ret = -EINVAL;
2247
2248 table->VceLevelCount =
2249 rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.count;
2250
2251 for (count = 0; count < table->VceLevelCount; count++) {
2252 table->VceLevel[count].Frequency =
2253 rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[count].evclk;
2254 table->VceLevel[count].MinVoltage =
2255 (u16)rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
2256 table->VceLevel[count].MinPhases = 1;
2257
2258 ret = radeon_atom_get_clock_dividers(rdev,
2259 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2260 table->VceLevel[count].Frequency, false, &dividers);
2261 if (ret)
2262 return ret;
2263
2264 table->VceLevel[count].Divider = (u8)dividers.post_divider;
2265
2266 table->VceLevel[count].Frequency = cpu_to_be32(table->VceLevel[count].Frequency);
2267 table->VceLevel[count].MinVoltage = cpu_to_be16(table->VceLevel[count].MinVoltage);
2268 }
2269
2270 return ret;
2271
2272}
2273
2274static int ci_populate_smc_acp_level(struct radeon_device *rdev,
2275 SMU7_Discrete_DpmTable *table)
2276{
2277 u32 count;
2278 struct atom_clock_dividers dividers;
2279 int ret = -EINVAL;
2280
2281 table->AcpLevelCount = (u8)
2282 (rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.count);
2283
2284 for (count = 0; count < table->AcpLevelCount; count++) {
2285 table->AcpLevel[count].Frequency =
2286 rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[count].clk;
2287 table->AcpLevel[count].MinVoltage =
2288 rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[count].v;
2289 table->AcpLevel[count].MinPhases = 1;
2290
2291 ret = radeon_atom_get_clock_dividers(rdev,
2292 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2293 table->AcpLevel[count].Frequency, false, &dividers);
2294 if (ret)
2295 return ret;
2296
2297 table->AcpLevel[count].Divider = (u8)dividers.post_divider;
2298
2299 table->AcpLevel[count].Frequency = cpu_to_be32(table->AcpLevel[count].Frequency);
2300 table->AcpLevel[count].MinVoltage = cpu_to_be16(table->AcpLevel[count].MinVoltage);
2301 }
2302
2303 return ret;
2304}
2305
2306static int ci_populate_smc_samu_level(struct radeon_device *rdev,
2307 SMU7_Discrete_DpmTable *table)
2308{
2309 u32 count;
2310 struct atom_clock_dividers dividers;
2311 int ret = -EINVAL;
2312
2313 table->SamuLevelCount =
2314 rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.count;
2315
2316 for (count = 0; count < table->SamuLevelCount; count++) {
2317 table->SamuLevel[count].Frequency =
2318 rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[count].clk;
2319 table->SamuLevel[count].MinVoltage =
2320 rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
2321 table->SamuLevel[count].MinPhases = 1;
2322
2323 ret = radeon_atom_get_clock_dividers(rdev,
2324 COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
2325 table->SamuLevel[count].Frequency, false, &dividers);
2326 if (ret)
2327 return ret;
2328
2329 table->SamuLevel[count].Divider = (u8)dividers.post_divider;
2330
2331 table->SamuLevel[count].Frequency = cpu_to_be32(table->SamuLevel[count].Frequency);
2332 table->SamuLevel[count].MinVoltage = cpu_to_be16(table->SamuLevel[count].MinVoltage);
2333 }
2334
2335 return ret;
2336}
2337
2338static int ci_calculate_mclk_params(struct radeon_device *rdev,
2339 u32 memory_clock,
2340 SMU7_Discrete_MemoryLevel *mclk,
2341 bool strobe_mode,
2342 bool dll_state_on)
2343{
2344 struct ci_power_info *pi = ci_get_pi(rdev);
2345 u32 dll_cntl = pi->clock_registers.dll_cntl;
2346 u32 mclk_pwrmgt_cntl = pi->clock_registers.mclk_pwrmgt_cntl;
2347 u32 mpll_ad_func_cntl = pi->clock_registers.mpll_ad_func_cntl;
2348 u32 mpll_dq_func_cntl = pi->clock_registers.mpll_dq_func_cntl;
2349 u32 mpll_func_cntl = pi->clock_registers.mpll_func_cntl;
2350 u32 mpll_func_cntl_1 = pi->clock_registers.mpll_func_cntl_1;
2351 u32 mpll_func_cntl_2 = pi->clock_registers.mpll_func_cntl_2;
2352 u32 mpll_ss1 = pi->clock_registers.mpll_ss1;
2353 u32 mpll_ss2 = pi->clock_registers.mpll_ss2;
2354 struct atom_mpll_param mpll_param;
2355 int ret;
2356
2357 ret = radeon_atom_get_memory_pll_dividers(rdev, memory_clock, strobe_mode, &mpll_param);
2358 if (ret)
2359 return ret;
2360
2361 mpll_func_cntl &= ~BWCTRL_MASK;
2362 mpll_func_cntl |= BWCTRL(mpll_param.bwcntl);
2363
2364 mpll_func_cntl_1 &= ~(CLKF_MASK | CLKFRAC_MASK | VCO_MODE_MASK);
2365 mpll_func_cntl_1 |= CLKF(mpll_param.clkf) |
2366 CLKFRAC(mpll_param.clkfrac) | VCO_MODE(mpll_param.vco_mode);
2367
2368 mpll_ad_func_cntl &= ~YCLK_POST_DIV_MASK;
2369 mpll_ad_func_cntl |= YCLK_POST_DIV(mpll_param.post_div);
2370
2371 if (pi->mem_gddr5) {
2372 mpll_dq_func_cntl &= ~(YCLK_SEL_MASK | YCLK_POST_DIV_MASK);
2373 mpll_dq_func_cntl |= YCLK_SEL(mpll_param.yclk_sel) |
2374 YCLK_POST_DIV(mpll_param.post_div);
2375 }
2376
2377 if (pi->caps_mclk_ss_support) {
2378 struct radeon_atom_ss ss;
2379 u32 freq_nom;
2380 u32 tmp;
2381 u32 reference_clock = rdev->clock.mpll.reference_freq;
2382
2383 if (pi->mem_gddr5)
2384 freq_nom = memory_clock * 4;
2385 else
2386 freq_nom = memory_clock * 2;
2387
2388 tmp = (freq_nom / reference_clock);
2389 tmp = tmp * tmp;
2390 if (radeon_atombios_get_asic_ss_info(rdev, &ss,
2391 ASIC_INTERNAL_MEMORY_SS, freq_nom)) {
2392 u32 clks = reference_clock * 5 / ss.rate;
2393 u32 clkv = (u32)((((131 * ss.percentage * ss.rate) / 100) * tmp) / freq_nom);
2394
2395 mpll_ss1 &= ~CLKV_MASK;
2396 mpll_ss1 |= CLKV(clkv);
2397
2398 mpll_ss2 &= ~CLKS_MASK;
2399 mpll_ss2 |= CLKS(clks);
2400 }
2401 }
2402
2403 mclk_pwrmgt_cntl &= ~DLL_SPEED_MASK;
2404 mclk_pwrmgt_cntl |= DLL_SPEED(mpll_param.dll_speed);
2405
2406 if (dll_state_on)
2407 mclk_pwrmgt_cntl |= MRDCK0_PDNB | MRDCK1_PDNB;
2408 else
2409 mclk_pwrmgt_cntl &= ~(MRDCK0_PDNB | MRDCK1_PDNB);
2410
2411 mclk->MclkFrequency = memory_clock;
2412 mclk->MpllFuncCntl = mpll_func_cntl;
2413 mclk->MpllFuncCntl_1 = mpll_func_cntl_1;
2414 mclk->MpllFuncCntl_2 = mpll_func_cntl_2;
2415 mclk->MpllAdFuncCntl = mpll_ad_func_cntl;
2416 mclk->MpllDqFuncCntl = mpll_dq_func_cntl;
2417 mclk->MclkPwrmgtCntl = mclk_pwrmgt_cntl;
2418 mclk->DllCntl = dll_cntl;
2419 mclk->MpllSs1 = mpll_ss1;
2420 mclk->MpllSs2 = mpll_ss2;
2421
2422 return 0;
2423}
2424
2425static int ci_populate_single_memory_level(struct radeon_device *rdev,
2426 u32 memory_clock,
2427 SMU7_Discrete_MemoryLevel *memory_level)
2428{
2429 struct ci_power_info *pi = ci_get_pi(rdev);
2430 int ret;
2431 bool dll_state_on;
2432
2433 if (rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.entries) {
2434 ret = ci_get_dependency_volt_by_clk(rdev,
2435 &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
2436 memory_clock, &memory_level->MinVddc);
2437 if (ret)
2438 return ret;
2439 }
2440
2441 if (rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk.entries) {
2442 ret = ci_get_dependency_volt_by_clk(rdev,
2443 &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
2444 memory_clock, &memory_level->MinVddci);
2445 if (ret)
2446 return ret;
2447 }
2448
2449 if (rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.entries) {
2450 ret = ci_get_dependency_volt_by_clk(rdev,
2451 &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk,
2452 memory_clock, &memory_level->MinMvdd);
2453 if (ret)
2454 return ret;
2455 }
2456
2457 memory_level->MinVddcPhases = 1;
2458
2459 if (pi->vddc_phase_shed_control)
2460 ci_populate_phase_value_based_on_mclk(rdev,
2461 &rdev->pm.dpm.dyn_state.phase_shedding_limits_table,
2462 memory_clock,
2463 &memory_level->MinVddcPhases);
2464
2465 memory_level->EnabledForThrottle = 1;
2466 memory_level->EnabledForActivity = 1;
2467 memory_level->UpH = 0;
2468 memory_level->DownH = 100;
2469 memory_level->VoltageDownH = 0;
2470 memory_level->ActivityLevel = (u16)pi->mclk_activity_target;
2471
2472 memory_level->StutterEnable = false;
2473 memory_level->StrobeEnable = false;
2474 memory_level->EdcReadEnable = false;
2475 memory_level->EdcWriteEnable = false;
2476 memory_level->RttEnable = false;
2477
2478 memory_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
2479
2480 if (pi->mclk_stutter_mode_threshold &&
2481 (memory_clock <= pi->mclk_stutter_mode_threshold) &&
2482 (pi->uvd_enabled == false) &&
2483 (RREG32(DPG_PIPE_STUTTER_CONTROL) & STUTTER_ENABLE) &&
2484 (rdev->pm.dpm.new_active_crtc_count <= 2))
2485 memory_level->StutterEnable = true;
2486
2487 if (pi->mclk_strobe_mode_threshold &&
2488 (memory_clock <= pi->mclk_strobe_mode_threshold))
2489 memory_level->StrobeEnable = 1;
2490
2491 if (pi->mem_gddr5) {
2492 memory_level->StrobeRatio =
2493 si_get_mclk_frequency_ratio(memory_clock, memory_level->StrobeEnable);
2494 if (pi->mclk_edc_enable_threshold &&
2495 (memory_clock > pi->mclk_edc_enable_threshold))
2496 memory_level->EdcReadEnable = true;
2497
2498 if (pi->mclk_edc_wr_enable_threshold &&
2499 (memory_clock > pi->mclk_edc_wr_enable_threshold))
2500 memory_level->EdcWriteEnable = true;
2501
2502 if (memory_level->StrobeEnable) {
2503 if (si_get_mclk_frequency_ratio(memory_clock, true) >=
2504 ((RREG32(MC_SEQ_MISC7) >> 16) & 0xf))
2505 dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
2506 else
2507 dll_state_on = ((RREG32(MC_SEQ_MISC6) >> 1) & 0x1) ? true : false;
2508 } else {
2509 dll_state_on = pi->dll_default_on;
2510 }
2511 } else {
2512 memory_level->StrobeRatio = si_get_ddr3_mclk_frequency_ratio(memory_clock);
2513 dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
2514 }
2515
2516 ret = ci_calculate_mclk_params(rdev, memory_clock, memory_level, memory_level->StrobeEnable, dll_state_on);
2517 if (ret)
2518 return ret;
2519
2520 memory_level->MinVddc = cpu_to_be32(memory_level->MinVddc * VOLTAGE_SCALE);
2521 memory_level->MinVddcPhases = cpu_to_be32(memory_level->MinVddcPhases);
2522 memory_level->MinVddci = cpu_to_be32(memory_level->MinVddci * VOLTAGE_SCALE);
2523 memory_level->MinMvdd = cpu_to_be32(memory_level->MinMvdd * VOLTAGE_SCALE);
2524
2525 memory_level->MclkFrequency = cpu_to_be32(memory_level->MclkFrequency);
2526 memory_level->ActivityLevel = cpu_to_be16(memory_level->ActivityLevel);
2527 memory_level->MpllFuncCntl = cpu_to_be32(memory_level->MpllFuncCntl);
2528 memory_level->MpllFuncCntl_1 = cpu_to_be32(memory_level->MpllFuncCntl_1);
2529 memory_level->MpllFuncCntl_2 = cpu_to_be32(memory_level->MpllFuncCntl_2);
2530 memory_level->MpllAdFuncCntl = cpu_to_be32(memory_level->MpllAdFuncCntl);
2531 memory_level->MpllDqFuncCntl = cpu_to_be32(memory_level->MpllDqFuncCntl);
2532 memory_level->MclkPwrmgtCntl = cpu_to_be32(memory_level->MclkPwrmgtCntl);
2533 memory_level->DllCntl = cpu_to_be32(memory_level->DllCntl);
2534 memory_level->MpllSs1 = cpu_to_be32(memory_level->MpllSs1);
2535 memory_level->MpllSs2 = cpu_to_be32(memory_level->MpllSs2);
2536
2537 return 0;
2538}
2539
2540static int ci_populate_smc_acpi_level(struct radeon_device *rdev,
2541 SMU7_Discrete_DpmTable *table)
2542{
2543 struct ci_power_info *pi = ci_get_pi(rdev);
2544 struct atom_clock_dividers dividers;
2545 SMU7_Discrete_VoltageLevel voltage_level;
2546 u32 spll_func_cntl = pi->clock_registers.cg_spll_func_cntl;
2547 u32 spll_func_cntl_2 = pi->clock_registers.cg_spll_func_cntl_2;
2548 u32 dll_cntl = pi->clock_registers.dll_cntl;
2549 u32 mclk_pwrmgt_cntl = pi->clock_registers.mclk_pwrmgt_cntl;
2550 int ret;
2551
2552 table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
2553
2554 if (pi->acpi_vddc)
2555 table->ACPILevel.MinVddc = cpu_to_be32(pi->acpi_vddc * VOLTAGE_SCALE);
2556 else
2557 table->ACPILevel.MinVddc = cpu_to_be32(pi->min_vddc_in_pp_table * VOLTAGE_SCALE);
2558
2559 table->ACPILevel.MinVddcPhases = pi->vddc_phase_shed_control ? 0 : 1;
2560
2561 table->ACPILevel.SclkFrequency = rdev->clock.spll.reference_freq;
2562
2563 ret = radeon_atom_get_clock_dividers(rdev,
2564 COMPUTE_GPUCLK_INPUT_FLAG_SCLK,
2565 table->ACPILevel.SclkFrequency, false, &dividers);
2566 if (ret)
2567 return ret;
2568
2569 table->ACPILevel.SclkDid = (u8)dividers.post_divider;
2570 table->ACPILevel.DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
2571 table->ACPILevel.DeepSleepDivId = 0;
2572
2573 spll_func_cntl &= ~SPLL_PWRON;
2574 spll_func_cntl |= SPLL_RESET;
2575
2576 spll_func_cntl_2 &= ~SCLK_MUX_SEL_MASK;
2577 spll_func_cntl_2 |= SCLK_MUX_SEL(4);
2578
2579 table->ACPILevel.CgSpllFuncCntl = spll_func_cntl;
2580 table->ACPILevel.CgSpllFuncCntl2 = spll_func_cntl_2;
2581 table->ACPILevel.CgSpllFuncCntl3 = pi->clock_registers.cg_spll_func_cntl_3;
2582 table->ACPILevel.CgSpllFuncCntl4 = pi->clock_registers.cg_spll_func_cntl_4;
2583 table->ACPILevel.SpllSpreadSpectrum = pi->clock_registers.cg_spll_spread_spectrum;
2584 table->ACPILevel.SpllSpreadSpectrum2 = pi->clock_registers.cg_spll_spread_spectrum_2;
2585 table->ACPILevel.CcPwrDynRm = 0;
2586 table->ACPILevel.CcPwrDynRm1 = 0;
2587
2588 table->ACPILevel.Flags = cpu_to_be32(table->ACPILevel.Flags);
2589 table->ACPILevel.MinVddcPhases = cpu_to_be32(table->ACPILevel.MinVddcPhases);
2590 table->ACPILevel.SclkFrequency = cpu_to_be32(table->ACPILevel.SclkFrequency);
2591 table->ACPILevel.CgSpllFuncCntl = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl);
2592 table->ACPILevel.CgSpllFuncCntl2 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl2);
2593 table->ACPILevel.CgSpllFuncCntl3 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl3);
2594 table->ACPILevel.CgSpllFuncCntl4 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl4);
2595 table->ACPILevel.SpllSpreadSpectrum = cpu_to_be32(table->ACPILevel.SpllSpreadSpectrum);
2596 table->ACPILevel.SpllSpreadSpectrum2 = cpu_to_be32(table->ACPILevel.SpllSpreadSpectrum2);
2597 table->ACPILevel.CcPwrDynRm = cpu_to_be32(table->ACPILevel.CcPwrDynRm);
2598 table->ACPILevel.CcPwrDynRm1 = cpu_to_be32(table->ACPILevel.CcPwrDynRm1);
2599
2600 table->MemoryACPILevel.MinVddc = table->ACPILevel.MinVddc;
2601 table->MemoryACPILevel.MinVddcPhases = table->ACPILevel.MinVddcPhases;
2602
2603 if (pi->vddci_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
2604 if (pi->acpi_vddci)
2605 table->MemoryACPILevel.MinVddci =
2606 cpu_to_be32(pi->acpi_vddci * VOLTAGE_SCALE);
2607 else
2608 table->MemoryACPILevel.MinVddci =
2609 cpu_to_be32(pi->min_vddci_in_pp_table * VOLTAGE_SCALE);
2610 }
2611
2612 if (ci_populate_mvdd_value(rdev, 0, &voltage_level))
2613 table->MemoryACPILevel.MinMvdd = 0;
2614 else
2615 table->MemoryACPILevel.MinMvdd =
2616 cpu_to_be32(voltage_level.Voltage * VOLTAGE_SCALE);
2617
2618 mclk_pwrmgt_cntl |= MRDCK0_RESET | MRDCK1_RESET;
2619 mclk_pwrmgt_cntl &= ~(MRDCK0_PDNB | MRDCK1_PDNB);
2620
2621 dll_cntl &= ~(MRDCK0_BYPASS | MRDCK1_BYPASS);
2622
2623 table->MemoryACPILevel.DllCntl = cpu_to_be32(dll_cntl);
2624 table->MemoryACPILevel.MclkPwrmgtCntl = cpu_to_be32(mclk_pwrmgt_cntl);
2625 table->MemoryACPILevel.MpllAdFuncCntl =
2626 cpu_to_be32(pi->clock_registers.mpll_ad_func_cntl);
2627 table->MemoryACPILevel.MpllDqFuncCntl =
2628 cpu_to_be32(pi->clock_registers.mpll_dq_func_cntl);
2629 table->MemoryACPILevel.MpllFuncCntl =
2630 cpu_to_be32(pi->clock_registers.mpll_func_cntl);
2631 table->MemoryACPILevel.MpllFuncCntl_1 =
2632 cpu_to_be32(pi->clock_registers.mpll_func_cntl_1);
2633 table->MemoryACPILevel.MpllFuncCntl_2 =
2634 cpu_to_be32(pi->clock_registers.mpll_func_cntl_2);
2635 table->MemoryACPILevel.MpllSs1 = cpu_to_be32(pi->clock_registers.mpll_ss1);
2636 table->MemoryACPILevel.MpllSs2 = cpu_to_be32(pi->clock_registers.mpll_ss2);
2637
2638 table->MemoryACPILevel.EnabledForThrottle = 0;
2639 table->MemoryACPILevel.EnabledForActivity = 0;
2640 table->MemoryACPILevel.UpH = 0;
2641 table->MemoryACPILevel.DownH = 100;
2642 table->MemoryACPILevel.VoltageDownH = 0;
2643 table->MemoryACPILevel.ActivityLevel =
2644 cpu_to_be16((u16)pi->mclk_activity_target);
2645
2646 table->MemoryACPILevel.StutterEnable = false;
2647 table->MemoryACPILevel.StrobeEnable = false;
2648 table->MemoryACPILevel.EdcReadEnable = false;
2649 table->MemoryACPILevel.EdcWriteEnable = false;
2650 table->MemoryACPILevel.RttEnable = false;
2651
2652 return 0;
2653}
2654
2655
2656static int ci_enable_ulv(struct radeon_device *rdev, bool enable)
2657{
2658 struct ci_power_info *pi = ci_get_pi(rdev);
2659 struct ci_ulv_parm *ulv = &pi->ulv;
2660
2661 if (ulv->supported) {
2662 if (enable)
2663 return (ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableULV) == PPSMC_Result_OK) ?
2664 0 : -EINVAL;
2665 else
2666 return (ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableULV) == PPSMC_Result_OK) ?
2667 0 : -EINVAL;
2668 }
2669
2670 return 0;
2671}
2672
2673static int ci_populate_ulv_level(struct radeon_device *rdev,
2674 SMU7_Discrete_Ulv *state)
2675{
2676 struct ci_power_info *pi = ci_get_pi(rdev);
2677 u16 ulv_voltage = rdev->pm.dpm.backbias_response_time;
2678
2679 state->CcPwrDynRm = 0;
2680 state->CcPwrDynRm1 = 0;
2681
2682 if (ulv_voltage == 0) {
2683 pi->ulv.supported = false;
2684 return 0;
2685 }
2686
2687 if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
2688 if (ulv_voltage > rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v)
2689 state->VddcOffset = 0;
2690 else
2691 state->VddcOffset =
2692 rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v - ulv_voltage;
2693 } else {
2694 if (ulv_voltage > rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v)
2695 state->VddcOffsetVid = 0;
2696 else
2697 state->VddcOffsetVid = (u8)
2698 ((rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v - ulv_voltage) *
2699 VOLTAGE_VID_OFFSET_SCALE2 / VOLTAGE_VID_OFFSET_SCALE1);
2700 }
2701 state->VddcPhase = pi->vddc_phase_shed_control ? 0 : 1;
2702
2703 state->CcPwrDynRm = cpu_to_be32(state->CcPwrDynRm);
2704 state->CcPwrDynRm1 = cpu_to_be32(state->CcPwrDynRm1);
2705 state->VddcOffset = cpu_to_be16(state->VddcOffset);
2706
2707 return 0;
2708}
2709
2710static int ci_calculate_sclk_params(struct radeon_device *rdev,
2711 u32 engine_clock,
2712 SMU7_Discrete_GraphicsLevel *sclk)
2713{
2714 struct ci_power_info *pi = ci_get_pi(rdev);
2715 struct atom_clock_dividers dividers;
2716 u32 spll_func_cntl_3 = pi->clock_registers.cg_spll_func_cntl_3;
2717 u32 spll_func_cntl_4 = pi->clock_registers.cg_spll_func_cntl_4;
2718 u32 cg_spll_spread_spectrum = pi->clock_registers.cg_spll_spread_spectrum;
2719 u32 cg_spll_spread_spectrum_2 = pi->clock_registers.cg_spll_spread_spectrum_2;
2720 u32 reference_clock = rdev->clock.spll.reference_freq;
2721 u32 reference_divider;
2722 u32 fbdiv;
2723 int ret;
2724
2725 ret = radeon_atom_get_clock_dividers(rdev,
2726 COMPUTE_GPUCLK_INPUT_FLAG_SCLK,
2727 engine_clock, false, &dividers);
2728 if (ret)
2729 return ret;
2730
2731 reference_divider = 1 + dividers.ref_div;
2732 fbdiv = dividers.fb_div & 0x3FFFFFF;
2733
2734 spll_func_cntl_3 &= ~SPLL_FB_DIV_MASK;
2735 spll_func_cntl_3 |= SPLL_FB_DIV(fbdiv);
2736 spll_func_cntl_3 |= SPLL_DITHEN;
2737
2738 if (pi->caps_sclk_ss_support) {
2739 struct radeon_atom_ss ss;
2740 u32 vco_freq = engine_clock * dividers.post_div;
2741
2742 if (radeon_atombios_get_asic_ss_info(rdev, &ss,
2743 ASIC_INTERNAL_ENGINE_SS, vco_freq)) {
2744 u32 clk_s = reference_clock * 5 / (reference_divider * ss.rate);
2745 u32 clk_v = 4 * ss.percentage * fbdiv / (clk_s * 10000);
2746
2747 cg_spll_spread_spectrum &= ~CLK_S_MASK;
2748 cg_spll_spread_spectrum |= CLK_S(clk_s);
2749 cg_spll_spread_spectrum |= SSEN;
2750
2751 cg_spll_spread_spectrum_2 &= ~CLK_V_MASK;
2752 cg_spll_spread_spectrum_2 |= CLK_V(clk_v);
2753 }
2754 }
2755
2756 sclk->SclkFrequency = engine_clock;
2757 sclk->CgSpllFuncCntl3 = spll_func_cntl_3;
2758 sclk->CgSpllFuncCntl4 = spll_func_cntl_4;
2759 sclk->SpllSpreadSpectrum = cg_spll_spread_spectrum;
2760 sclk->SpllSpreadSpectrum2 = cg_spll_spread_spectrum_2;
2761 sclk->SclkDid = (u8)dividers.post_divider;
2762
2763 return 0;
2764}
2765
2766static int ci_populate_single_graphic_level(struct radeon_device *rdev,
2767 u32 engine_clock,
2768 u16 sclk_activity_level_t,
2769 SMU7_Discrete_GraphicsLevel *graphic_level)
2770{
2771 struct ci_power_info *pi = ci_get_pi(rdev);
2772 int ret;
2773
2774 ret = ci_calculate_sclk_params(rdev, engine_clock, graphic_level);
2775 if (ret)
2776 return ret;
2777
2778 ret = ci_get_dependency_volt_by_clk(rdev,
2779 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk,
2780 engine_clock, &graphic_level->MinVddc);
2781 if (ret)
2782 return ret;
2783
2784 graphic_level->SclkFrequency = engine_clock;
2785
2786 graphic_level->Flags = 0;
2787 graphic_level->MinVddcPhases = 1;
2788
2789 if (pi->vddc_phase_shed_control)
2790 ci_populate_phase_value_based_on_sclk(rdev,
2791 &rdev->pm.dpm.dyn_state.phase_shedding_limits_table,
2792 engine_clock,
2793 &graphic_level->MinVddcPhases);
2794
2795 graphic_level->ActivityLevel = sclk_activity_level_t;
2796
2797 graphic_level->CcPwrDynRm = 0;
2798 graphic_level->CcPwrDynRm1 = 0;
2799 graphic_level->EnabledForActivity = 1;
2800 graphic_level->EnabledForThrottle = 1;
2801 graphic_level->UpH = 0;
2802 graphic_level->DownH = 0;
2803 graphic_level->VoltageDownH = 0;
2804 graphic_level->PowerThrottle = 0;
2805
2806 if (pi->caps_sclk_ds)
2807 graphic_level->DeepSleepDivId = ci_get_sleep_divider_id_from_clock(rdev,
2808 engine_clock,
2809 CISLAND_MINIMUM_ENGINE_CLOCK);
2810
2811 graphic_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
2812
2813 graphic_level->Flags = cpu_to_be32(graphic_level->Flags);
2814 graphic_level->MinVddc = cpu_to_be32(graphic_level->MinVddc * VOLTAGE_SCALE);
2815 graphic_level->MinVddcPhases = cpu_to_be32(graphic_level->MinVddcPhases);
2816 graphic_level->SclkFrequency = cpu_to_be32(graphic_level->SclkFrequency);
2817 graphic_level->ActivityLevel = cpu_to_be16(graphic_level->ActivityLevel);
2818 graphic_level->CgSpllFuncCntl3 = cpu_to_be32(graphic_level->CgSpllFuncCntl3);
2819 graphic_level->CgSpllFuncCntl4 = cpu_to_be32(graphic_level->CgSpllFuncCntl4);
2820 graphic_level->SpllSpreadSpectrum = cpu_to_be32(graphic_level->SpllSpreadSpectrum);
2821 graphic_level->SpllSpreadSpectrum2 = cpu_to_be32(graphic_level->SpllSpreadSpectrum2);
2822 graphic_level->CcPwrDynRm = cpu_to_be32(graphic_level->CcPwrDynRm);
2823 graphic_level->CcPwrDynRm1 = cpu_to_be32(graphic_level->CcPwrDynRm1);
2824
2825 return 0;
2826}
2827
2828static int ci_populate_all_graphic_levels(struct radeon_device *rdev)
2829{
2830 struct ci_power_info *pi = ci_get_pi(rdev);
2831 struct ci_dpm_table *dpm_table = &pi->dpm_table;
2832 u32 level_array_address = pi->dpm_table_start +
2833 offsetof(SMU7_Discrete_DpmTable, GraphicsLevel);
2834 u32 level_array_size = sizeof(SMU7_Discrete_GraphicsLevel) *
2835 SMU7_MAX_LEVELS_GRAPHICS;
2836 SMU7_Discrete_GraphicsLevel *levels = pi->smc_state_table.GraphicsLevel;
2837 u32 i, ret;
2838
2839 memset(levels, 0, level_array_size);
2840
2841 for (i = 0; i < dpm_table->sclk_table.count; i++) {
2842 ret = ci_populate_single_graphic_level(rdev,
2843 dpm_table->sclk_table.dpm_levels[i].value,
2844 (u16)pi->activity_target[i],
2845 &pi->smc_state_table.GraphicsLevel[i]);
2846 if (ret)
2847 return ret;
2848 if (i == (dpm_table->sclk_table.count - 1))
2849 pi->smc_state_table.GraphicsLevel[i].DisplayWatermark =
2850 PPSMC_DISPLAY_WATERMARK_HIGH;
2851 }
2852
2853 pi->smc_state_table.GraphicsDpmLevelCount = (u8)dpm_table->sclk_table.count;
2854 pi->dpm_level_enable_mask.sclk_dpm_enable_mask =
2855 ci_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
2856
2857 ret = ci_copy_bytes_to_smc(rdev, level_array_address,
2858 (u8 *)levels, level_array_size,
2859 pi->sram_end);
2860 if (ret)
2861 return ret;
2862
2863 return 0;
2864}
2865
2866static int ci_populate_ulv_state(struct radeon_device *rdev,
2867 SMU7_Discrete_Ulv *ulv_level)
2868{
2869 return ci_populate_ulv_level(rdev, ulv_level);
2870}
2871
2872static int ci_populate_all_memory_levels(struct radeon_device *rdev)
2873{
2874 struct ci_power_info *pi = ci_get_pi(rdev);
2875 struct ci_dpm_table *dpm_table = &pi->dpm_table;
2876 u32 level_array_address = pi->dpm_table_start +
2877 offsetof(SMU7_Discrete_DpmTable, MemoryLevel);
2878 u32 level_array_size = sizeof(SMU7_Discrete_MemoryLevel) *
2879 SMU7_MAX_LEVELS_MEMORY;
2880 SMU7_Discrete_MemoryLevel *levels = pi->smc_state_table.MemoryLevel;
2881 u32 i, ret;
2882
2883 memset(levels, 0, level_array_size);
2884
2885 for (i = 0; i < dpm_table->mclk_table.count; i++) {
2886 if (dpm_table->mclk_table.dpm_levels[i].value == 0)
2887 return -EINVAL;
2888 ret = ci_populate_single_memory_level(rdev,
2889 dpm_table->mclk_table.dpm_levels[i].value,
2890 &pi->smc_state_table.MemoryLevel[i]);
2891 if (ret)
2892 return ret;
2893 }
2894
2895 pi->smc_state_table.MemoryLevel[0].ActivityLevel = cpu_to_be16(0x1F);
2896
2897 pi->smc_state_table.MemoryDpmLevelCount = (u8)dpm_table->mclk_table.count;
2898 pi->dpm_level_enable_mask.mclk_dpm_enable_mask =
2899 ci_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
2900
2901 pi->smc_state_table.MemoryLevel[dpm_table->mclk_table.count - 1].DisplayWatermark =
2902 PPSMC_DISPLAY_WATERMARK_HIGH;
2903
2904 ret = ci_copy_bytes_to_smc(rdev, level_array_address,
2905 (u8 *)levels, level_array_size,
2906 pi->sram_end);
2907 if (ret)
2908 return ret;
2909
2910 return 0;
2911}
2912
2913static void ci_reset_single_dpm_table(struct radeon_device *rdev,
2914 struct ci_single_dpm_table* dpm_table,
2915 u32 count)
2916{
2917 u32 i;
2918
2919 dpm_table->count = count;
2920 for (i = 0; i < MAX_REGULAR_DPM_NUMBER; i++)
2921 dpm_table->dpm_levels[i].enabled = false;
2922}
2923
2924static void ci_setup_pcie_table_entry(struct ci_single_dpm_table* dpm_table,
2925 u32 index, u32 pcie_gen, u32 pcie_lanes)
2926{
2927 dpm_table->dpm_levels[index].value = pcie_gen;
2928 dpm_table->dpm_levels[index].param1 = pcie_lanes;
2929 dpm_table->dpm_levels[index].enabled = true;
2930}
2931
2932static int ci_setup_default_pcie_tables(struct radeon_device *rdev)
2933{
2934 struct ci_power_info *pi = ci_get_pi(rdev);
2935
2936 if (!pi->use_pcie_performance_levels && !pi->use_pcie_powersaving_levels)
2937 return -EINVAL;
2938
2939 if (pi->use_pcie_performance_levels && !pi->use_pcie_powersaving_levels) {
2940 pi->pcie_gen_powersaving = pi->pcie_gen_performance;
2941 pi->pcie_lane_powersaving = pi->pcie_lane_performance;
2942 } else if (!pi->use_pcie_performance_levels && pi->use_pcie_powersaving_levels) {
2943 pi->pcie_gen_performance = pi->pcie_gen_powersaving;
2944 pi->pcie_lane_performance = pi->pcie_lane_powersaving;
2945 }
2946
2947 ci_reset_single_dpm_table(rdev,
2948 &pi->dpm_table.pcie_speed_table,
2949 SMU7_MAX_LEVELS_LINK);
2950
2951 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 0,
2952 pi->pcie_gen_powersaving.min,
2953 pi->pcie_lane_powersaving.min);
2954 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 1,
2955 pi->pcie_gen_performance.min,
2956 pi->pcie_lane_performance.min);
2957 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 2,
2958 pi->pcie_gen_powersaving.min,
2959 pi->pcie_lane_powersaving.max);
2960 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 3,
2961 pi->pcie_gen_performance.min,
2962 pi->pcie_lane_performance.max);
2963 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 4,
2964 pi->pcie_gen_powersaving.max,
2965 pi->pcie_lane_powersaving.max);
2966 ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 5,
2967 pi->pcie_gen_performance.max,
2968 pi->pcie_lane_performance.max);
2969
2970 pi->dpm_table.pcie_speed_table.count = 6;
2971
2972 return 0;
2973}
2974
2975static int ci_setup_default_dpm_tables(struct radeon_device *rdev)
2976{
2977 struct ci_power_info *pi = ci_get_pi(rdev);
2978 struct radeon_clock_voltage_dependency_table *allowed_sclk_vddc_table =
2979 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
2980 struct radeon_clock_voltage_dependency_table *allowed_mclk_table =
2981 &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk;
2982 struct radeon_cac_leakage_table *std_voltage_table =
2983 &rdev->pm.dpm.dyn_state.cac_leakage_table;
2984 u32 i;
2985
2986 if (allowed_sclk_vddc_table == NULL)
2987 return -EINVAL;
2988 if (allowed_sclk_vddc_table->count < 1)
2989 return -EINVAL;
2990 if (allowed_mclk_table == NULL)
2991 return -EINVAL;
2992 if (allowed_mclk_table->count < 1)
2993 return -EINVAL;
2994
2995 memset(&pi->dpm_table, 0, sizeof(struct ci_dpm_table));
2996
2997 ci_reset_single_dpm_table(rdev,
2998 &pi->dpm_table.sclk_table,
2999 SMU7_MAX_LEVELS_GRAPHICS);
3000 ci_reset_single_dpm_table(rdev,
3001 &pi->dpm_table.mclk_table,
3002 SMU7_MAX_LEVELS_MEMORY);
3003 ci_reset_single_dpm_table(rdev,
3004 &pi->dpm_table.vddc_table,
3005 SMU7_MAX_LEVELS_VDDC);
3006 ci_reset_single_dpm_table(rdev,
3007 &pi->dpm_table.vddci_table,
3008 SMU7_MAX_LEVELS_VDDCI);
3009 ci_reset_single_dpm_table(rdev,
3010 &pi->dpm_table.mvdd_table,
3011 SMU7_MAX_LEVELS_MVDD);
3012
3013 pi->dpm_table.sclk_table.count = 0;
3014 for (i = 0; i < allowed_sclk_vddc_table->count; i++) {
3015 if ((i == 0) ||
3016 (pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count-1].value !=
3017 allowed_sclk_vddc_table->entries[i].clk)) {
3018 pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count].value =
3019 allowed_sclk_vddc_table->entries[i].clk;
3020 pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count].enabled = true;
3021 pi->dpm_table.sclk_table.count++;
3022 }
3023 }
3024
3025 pi->dpm_table.mclk_table.count = 0;
3026 for (i = 0; i < allowed_mclk_table->count; i++) {
3027 if ((i==0) ||
3028 (pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count-1].value !=
3029 allowed_mclk_table->entries[i].clk)) {
3030 pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count].value =
3031 allowed_mclk_table->entries[i].clk;
3032 pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count].enabled = true;
3033 pi->dpm_table.mclk_table.count++;
3034 }
3035 }
3036
3037 for (i = 0; i < allowed_sclk_vddc_table->count; i++) {
3038 pi->dpm_table.vddc_table.dpm_levels[i].value =
3039 allowed_sclk_vddc_table->entries[i].v;
3040 pi->dpm_table.vddc_table.dpm_levels[i].param1 =
3041 std_voltage_table->entries[i].leakage;
3042 pi->dpm_table.vddc_table.dpm_levels[i].enabled = true;
3043 }
3044 pi->dpm_table.vddc_table.count = allowed_sclk_vddc_table->count;
3045
3046 allowed_mclk_table = &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk;
3047 if (allowed_mclk_table) {
3048 for (i = 0; i < allowed_mclk_table->count; i++) {
3049 pi->dpm_table.vddci_table.dpm_levels[i].value =
3050 allowed_mclk_table->entries[i].v;
3051 pi->dpm_table.vddci_table.dpm_levels[i].enabled = true;
3052 }
3053 pi->dpm_table.vddci_table.count = allowed_mclk_table->count;
3054 }
3055
3056 allowed_mclk_table = &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk;
3057 if (allowed_mclk_table) {
3058 for (i = 0; i < allowed_mclk_table->count; i++) {
3059 pi->dpm_table.mvdd_table.dpm_levels[i].value =
3060 allowed_mclk_table->entries[i].v;
3061 pi->dpm_table.mvdd_table.dpm_levels[i].enabled = true;
3062 }
3063 pi->dpm_table.mvdd_table.count = allowed_mclk_table->count;
3064 }
3065
3066 ci_setup_default_pcie_tables(rdev);
3067
3068 return 0;
3069}
3070
3071static int ci_find_boot_level(struct ci_single_dpm_table *table,
3072 u32 value, u32 *boot_level)
3073{
3074 u32 i;
3075 int ret = -EINVAL;
3076
3077 for(i = 0; i < table->count; i++) {
3078 if (value == table->dpm_levels[i].value) {
3079 *boot_level = i;
3080 ret = 0;
3081 }
3082 }
3083
3084 return ret;
3085}
3086
3087static int ci_init_smc_table(struct radeon_device *rdev)
3088{
3089 struct ci_power_info *pi = ci_get_pi(rdev);
3090 struct ci_ulv_parm *ulv = &pi->ulv;
3091 struct radeon_ps *radeon_boot_state = rdev->pm.dpm.boot_ps;
3092 SMU7_Discrete_DpmTable *table = &pi->smc_state_table;
3093 int ret;
3094
3095 ret = ci_setup_default_dpm_tables(rdev);
3096 if (ret)
3097 return ret;
3098
3099 if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_NONE)
3100 ci_populate_smc_voltage_tables(rdev, table);
3101
3102 ci_init_fps_limits(rdev);
3103
3104 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_HARDWAREDC)
3105 table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
3106
3107 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC)
3108 table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
3109
3110 if (pi->mem_gddr5)
3111 table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
3112
3113 if (ulv->supported) {
3114 ret = ci_populate_ulv_state(rdev, &pi->smc_state_table.Ulv);
3115 if (ret)
3116 return ret;
3117 WREG32_SMC(CG_ULV_PARAMETER, ulv->cg_ulv_parameter);
3118 }
3119
3120 ret = ci_populate_all_graphic_levels(rdev);
3121 if (ret)
3122 return ret;
3123
3124 ret = ci_populate_all_memory_levels(rdev);
3125 if (ret)
3126 return ret;
3127
3128 ci_populate_smc_link_level(rdev, table);
3129
3130 ret = ci_populate_smc_acpi_level(rdev, table);
3131 if (ret)
3132 return ret;
3133
3134 ret = ci_populate_smc_vce_level(rdev, table);
3135 if (ret)
3136 return ret;
3137
3138 ret = ci_populate_smc_acp_level(rdev, table);
3139 if (ret)
3140 return ret;
3141
3142 ret = ci_populate_smc_samu_level(rdev, table);
3143 if (ret)
3144 return ret;
3145
3146 ret = ci_do_program_memory_timing_parameters(rdev);
3147 if (ret)
3148 return ret;
3149
3150 ret = ci_populate_smc_uvd_level(rdev, table);
3151 if (ret)
3152 return ret;
3153
3154 table->UvdBootLevel = 0;
3155 table->VceBootLevel = 0;
3156 table->AcpBootLevel = 0;
3157 table->SamuBootLevel = 0;
3158 table->GraphicsBootLevel = 0;
3159 table->MemoryBootLevel = 0;
3160
3161 ret = ci_find_boot_level(&pi->dpm_table.sclk_table,
3162 pi->vbios_boot_state.sclk_bootup_value,
3163 (u32 *)&pi->smc_state_table.GraphicsBootLevel);
3164
3165 ret = ci_find_boot_level(&pi->dpm_table.mclk_table,
3166 pi->vbios_boot_state.mclk_bootup_value,
3167 (u32 *)&pi->smc_state_table.MemoryBootLevel);
3168
3169 table->BootVddc = pi->vbios_boot_state.vddc_bootup_value;
3170 table->BootVddci = pi->vbios_boot_state.vddci_bootup_value;
3171 table->BootMVdd = pi->vbios_boot_state.mvdd_bootup_value;
3172
3173 ci_populate_smc_initial_state(rdev, radeon_boot_state);
3174
3175 ret = ci_populate_bapm_parameters_in_dpm_table(rdev);
3176 if (ret)
3177 return ret;
3178
3179 table->UVDInterval = 1;
3180 table->VCEInterval = 1;
3181 table->ACPInterval = 1;
3182 table->SAMUInterval = 1;
3183 table->GraphicsVoltageChangeEnable = 1;
3184 table->GraphicsThermThrottleEnable = 1;
3185 table->GraphicsInterval = 1;
3186 table->VoltageInterval = 1;
3187 table->ThermalInterval = 1;
3188 table->TemperatureLimitHigh = (u16)((pi->thermal_temp_setting.temperature_high *
3189 CISLANDS_Q88_FORMAT_CONVERSION_UNIT) / 1000);
3190 table->TemperatureLimitLow = (u16)((pi->thermal_temp_setting.temperature_low *
3191 CISLANDS_Q88_FORMAT_CONVERSION_UNIT) / 1000);
3192 table->MemoryVoltageChangeEnable = 1;
3193 table->MemoryInterval = 1;
3194 table->VoltageResponseTime = 0;
3195 table->VddcVddciDelta = 4000;
3196 table->PhaseResponseTime = 0;
3197 table->MemoryThermThrottleEnable = 1;
3198 table->PCIeBootLinkLevel = 0;
3199 table->PCIeGenInterval = 1;
3200 if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2)
3201 table->SVI2Enable = 1;
3202 else
3203 table->SVI2Enable = 0;
3204
3205 table->ThermGpio = 17;
3206 table->SclkStepSize = 0x4000;
3207
3208 table->SystemFlags = cpu_to_be32(table->SystemFlags);
3209 table->SmioMaskVddcVid = cpu_to_be32(table->SmioMaskVddcVid);
3210 table->SmioMaskVddcPhase = cpu_to_be32(table->SmioMaskVddcPhase);
3211 table->SmioMaskVddciVid = cpu_to_be32(table->SmioMaskVddciVid);
3212 table->SmioMaskMvddVid = cpu_to_be32(table->SmioMaskMvddVid);
3213 table->SclkStepSize = cpu_to_be32(table->SclkStepSize);
3214 table->TemperatureLimitHigh = cpu_to_be16(table->TemperatureLimitHigh);
3215 table->TemperatureLimitLow = cpu_to_be16(table->TemperatureLimitLow);
3216 table->VddcVddciDelta = cpu_to_be16(table->VddcVddciDelta);
3217 table->VoltageResponseTime = cpu_to_be16(table->VoltageResponseTime);
3218 table->PhaseResponseTime = cpu_to_be16(table->PhaseResponseTime);
3219 table->BootVddc = cpu_to_be16(table->BootVddc * VOLTAGE_SCALE);
3220 table->BootVddci = cpu_to_be16(table->BootVddci * VOLTAGE_SCALE);
3221 table->BootMVdd = cpu_to_be16(table->BootMVdd * VOLTAGE_SCALE);
3222
3223 ret = ci_copy_bytes_to_smc(rdev,
3224 pi->dpm_table_start +
3225 offsetof(SMU7_Discrete_DpmTable, SystemFlags),
3226 (u8 *)&table->SystemFlags,
3227 sizeof(SMU7_Discrete_DpmTable) - 3 * sizeof(SMU7_PIDController),
3228 pi->sram_end);
3229 if (ret)
3230 return ret;
3231
3232 return 0;
3233}
3234
3235static void ci_trim_single_dpm_states(struct radeon_device *rdev,
3236 struct ci_single_dpm_table *dpm_table,
3237 u32 low_limit, u32 high_limit)
3238{
3239 u32 i;
3240
3241 for (i = 0; i < dpm_table->count; i++) {
3242 if ((dpm_table->dpm_levels[i].value < low_limit) ||
3243 (dpm_table->dpm_levels[i].value > high_limit))
3244 dpm_table->dpm_levels[i].enabled = false;
3245 else
3246 dpm_table->dpm_levels[i].enabled = true;
3247 }
3248}
3249
3250static void ci_trim_pcie_dpm_states(struct radeon_device *rdev,
3251 u32 speed_low, u32 lanes_low,
3252 u32 speed_high, u32 lanes_high)
3253{
3254 struct ci_power_info *pi = ci_get_pi(rdev);
3255 struct ci_single_dpm_table *pcie_table = &pi->dpm_table.pcie_speed_table;
3256 u32 i, j;
3257
3258 for (i = 0; i < pcie_table->count; i++) {
3259 if ((pcie_table->dpm_levels[i].value < speed_low) ||
3260 (pcie_table->dpm_levels[i].param1 < lanes_low) ||
3261 (pcie_table->dpm_levels[i].value > speed_high) ||
3262 (pcie_table->dpm_levels[i].param1 > lanes_high))
3263 pcie_table->dpm_levels[i].enabled = false;
3264 else
3265 pcie_table->dpm_levels[i].enabled = true;
3266 }
3267
3268 for (i = 0; i < pcie_table->count; i++) {
3269 if (pcie_table->dpm_levels[i].enabled) {
3270 for (j = i + 1; j < pcie_table->count; j++) {
3271 if (pcie_table->dpm_levels[j].enabled) {
3272 if ((pcie_table->dpm_levels[i].value == pcie_table->dpm_levels[j].value) &&
3273 (pcie_table->dpm_levels[i].param1 == pcie_table->dpm_levels[j].param1))
3274 pcie_table->dpm_levels[j].enabled = false;
3275 }
3276 }
3277 }
3278 }
3279}
3280
3281static int ci_trim_dpm_states(struct radeon_device *rdev,
3282 struct radeon_ps *radeon_state)
3283{
3284 struct ci_ps *state = ci_get_ps(radeon_state);
3285 struct ci_power_info *pi = ci_get_pi(rdev);
3286 u32 high_limit_count;
3287
3288 if (state->performance_level_count < 1)
3289 return -EINVAL;
3290
3291 if (state->performance_level_count == 1)
3292 high_limit_count = 0;
3293 else
3294 high_limit_count = 1;
3295
3296 ci_trim_single_dpm_states(rdev,
3297 &pi->dpm_table.sclk_table,
3298 state->performance_levels[0].sclk,
3299 state->performance_levels[high_limit_count].sclk);
3300
3301 ci_trim_single_dpm_states(rdev,
3302 &pi->dpm_table.mclk_table,
3303 state->performance_levels[0].mclk,
3304 state->performance_levels[high_limit_count].mclk);
3305
3306 ci_trim_pcie_dpm_states(rdev,
3307 state->performance_levels[0].pcie_gen,
3308 state->performance_levels[0].pcie_lane,
3309 state->performance_levels[high_limit_count].pcie_gen,
3310 state->performance_levels[high_limit_count].pcie_lane);
3311
3312 return 0;
3313}
3314
3315static int ci_apply_disp_minimum_voltage_request(struct radeon_device *rdev)
3316{
3317 struct radeon_clock_voltage_dependency_table *disp_voltage_table =
3318 &rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk;
3319 struct radeon_clock_voltage_dependency_table *vddc_table =
3320 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
3321 u32 requested_voltage = 0;
3322 u32 i;
3323
3324 if (disp_voltage_table == NULL)
3325 return -EINVAL;
3326 if (!disp_voltage_table->count)
3327 return -EINVAL;
3328
3329 for (i = 0; i < disp_voltage_table->count; i++) {
3330 if (rdev->clock.current_dispclk == disp_voltage_table->entries[i].clk)
3331 requested_voltage = disp_voltage_table->entries[i].v;
3332 }
3333
3334 for (i = 0; i < vddc_table->count; i++) {
3335 if (requested_voltage <= vddc_table->entries[i].v) {
3336 requested_voltage = vddc_table->entries[i].v;
3337 return (ci_send_msg_to_smc_with_parameter(rdev,
3338 PPSMC_MSG_VddC_Request,
3339 requested_voltage * VOLTAGE_SCALE) == PPSMC_Result_OK) ?
3340 0 : -EINVAL;
3341 }
3342 }
3343
3344 return -EINVAL;
3345}
3346
3347static int ci_upload_dpm_level_enable_mask(struct radeon_device *rdev)
3348{
3349 struct ci_power_info *pi = ci_get_pi(rdev);
3350 PPSMC_Result result;
3351
3352 if (!pi->sclk_dpm_key_disabled) {
3353 if (pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
3354 result = ci_send_msg_to_smc_with_parameter(rdev,
3355 PPSMC_MSG_SCLKDPM_SetEnabledMask,
3356 pi->dpm_level_enable_mask.sclk_dpm_enable_mask);
3357 if (result != PPSMC_Result_OK)
3358 return -EINVAL;
3359 }
3360 }
3361
3362 if (!pi->mclk_dpm_key_disabled) {
3363 if (pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
3364 result = ci_send_msg_to_smc_with_parameter(rdev,
3365 PPSMC_MSG_MCLKDPM_SetEnabledMask,
3366 pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
3367 if (result != PPSMC_Result_OK)
3368 return -EINVAL;
3369 }
3370 }
3371
3372 if (!pi->pcie_dpm_key_disabled) {
3373 if (pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
3374 result = ci_send_msg_to_smc_with_parameter(rdev,
3375 PPSMC_MSG_PCIeDPM_SetEnabledMask,
3376 pi->dpm_level_enable_mask.pcie_dpm_enable_mask);
3377 if (result != PPSMC_Result_OK)
3378 return -EINVAL;
3379 }
3380 }
3381
3382 ci_apply_disp_minimum_voltage_request(rdev);
3383
3384 return 0;
3385}
3386
3387static void ci_find_dpm_states_clocks_in_dpm_table(struct radeon_device *rdev,
3388 struct radeon_ps *radeon_state)
3389{
3390 struct ci_power_info *pi = ci_get_pi(rdev);
3391 struct ci_ps *state = ci_get_ps(radeon_state);
3392 struct ci_single_dpm_table *sclk_table = &pi->dpm_table.sclk_table;
3393 u32 sclk = state->performance_levels[state->performance_level_count-1].sclk;
3394 struct ci_single_dpm_table *mclk_table = &pi->dpm_table.mclk_table;
3395 u32 mclk = state->performance_levels[state->performance_level_count-1].mclk;
3396 u32 i;
3397
3398 pi->need_update_smu7_dpm_table = 0;
3399
3400 for (i = 0; i < sclk_table->count; i++) {
3401 if (sclk == sclk_table->dpm_levels[i].value)
3402 break;
3403 }
3404
3405 if (i >= sclk_table->count) {
3406 pi->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_SCLK;
3407 } else {
3408 /* XXX check display min clock requirements */
3409 if (0 != CISLAND_MINIMUM_ENGINE_CLOCK)
3410 pi->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_SCLK;
3411 }
3412
3413 for (i = 0; i < mclk_table->count; i++) {
3414 if (mclk == mclk_table->dpm_levels[i].value)
3415 break;
3416 }
3417
3418 if (i >= mclk_table->count)
3419 pi->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_MCLK;
3420
3421 if (rdev->pm.dpm.current_active_crtc_count !=
3422 rdev->pm.dpm.new_active_crtc_count)
3423 pi->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_MCLK;
3424}
3425
3426static int ci_populate_and_upload_sclk_mclk_dpm_levels(struct radeon_device *rdev,
3427 struct radeon_ps *radeon_state)
3428{
3429 struct ci_power_info *pi = ci_get_pi(rdev);
3430 struct ci_ps *state = ci_get_ps(radeon_state);
3431 u32 sclk = state->performance_levels[state->performance_level_count-1].sclk;
3432 u32 mclk = state->performance_levels[state->performance_level_count-1].mclk;
3433 struct ci_dpm_table *dpm_table = &pi->dpm_table;
3434 int ret;
3435
3436 if (!pi->need_update_smu7_dpm_table)
3437 return 0;
3438
3439 if (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_SCLK)
3440 dpm_table->sclk_table.dpm_levels[dpm_table->sclk_table.count-1].value = sclk;
3441
3442 if (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)
3443 dpm_table->mclk_table.dpm_levels[dpm_table->mclk_table.count-1].value = mclk;
3444
3445 if (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK)) {
3446 ret = ci_populate_all_graphic_levels(rdev);
3447 if (ret)
3448 return ret;
3449 }
3450
3451 if (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_MCLK | DPMTABLE_UPDATE_MCLK)) {
3452 ret = ci_populate_all_memory_levels(rdev);
3453 if (ret)
3454 return ret;
3455 }
3456
3457 return 0;
3458}
3459
3460static int ci_enable_uvd_dpm(struct radeon_device *rdev, bool enable)
3461{
3462 struct ci_power_info *pi = ci_get_pi(rdev);
3463 const struct radeon_clock_and_voltage_limits *max_limits;
3464 int i;
3465
3466 if (rdev->pm.dpm.ac_power)
3467 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
3468 else
3469 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
3470
3471 if (enable) {
3472 pi->dpm_level_enable_mask.uvd_dpm_enable_mask = 0;
3473
3474 for (i = rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
3475 if (rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
3476 pi->dpm_level_enable_mask.uvd_dpm_enable_mask |= 1 << i;
3477
3478 if (!pi->caps_uvd_dpm)
3479 break;
3480 }
3481 }
3482
3483 ci_send_msg_to_smc_with_parameter(rdev,
3484 PPSMC_MSG_UVDDPM_SetEnabledMask,
3485 pi->dpm_level_enable_mask.uvd_dpm_enable_mask);
3486
3487 if (pi->last_mclk_dpm_enable_mask & 0x1) {
3488 pi->uvd_enabled = true;
3489 pi->dpm_level_enable_mask.mclk_dpm_enable_mask &= 0xFFFFFFFE;
3490 ci_send_msg_to_smc_with_parameter(rdev,
3491 PPSMC_MSG_MCLKDPM_SetEnabledMask,
3492 pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
3493 }
3494 } else {
3495 if (pi->last_mclk_dpm_enable_mask & 0x1) {
3496 pi->uvd_enabled = false;
3497 pi->dpm_level_enable_mask.mclk_dpm_enable_mask |= 1;
3498 ci_send_msg_to_smc_with_parameter(rdev,
3499 PPSMC_MSG_MCLKDPM_SetEnabledMask,
3500 pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
3501 }
3502 }
3503
3504 return (ci_send_msg_to_smc(rdev, enable ?
3505 PPSMC_MSG_UVDDPM_Enable : PPSMC_MSG_UVDDPM_Disable) == PPSMC_Result_OK) ?
3506 0 : -EINVAL;
3507}
3508
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003509static int ci_enable_vce_dpm(struct radeon_device *rdev, bool enable)
3510{
3511 struct ci_power_info *pi = ci_get_pi(rdev);
3512 const struct radeon_clock_and_voltage_limits *max_limits;
3513 int i;
3514
3515 if (rdev->pm.dpm.ac_power)
3516 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
3517 else
3518 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
3519
3520 if (enable) {
3521 pi->dpm_level_enable_mask.vce_dpm_enable_mask = 0;
3522 for (i = rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
3523 if (rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
3524 pi->dpm_level_enable_mask.vce_dpm_enable_mask |= 1 << i;
3525
3526 if (!pi->caps_vce_dpm)
3527 break;
3528 }
3529 }
3530
3531 ci_send_msg_to_smc_with_parameter(rdev,
3532 PPSMC_MSG_VCEDPM_SetEnabledMask,
3533 pi->dpm_level_enable_mask.vce_dpm_enable_mask);
3534 }
3535
3536 return (ci_send_msg_to_smc(rdev, enable ?
3537 PPSMC_MSG_VCEDPM_Enable : PPSMC_MSG_VCEDPM_Disable) == PPSMC_Result_OK) ?
3538 0 : -EINVAL;
3539}
3540
Alex Deucher8cd36682013-08-23 11:05:24 -04003541#if 0
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003542static int ci_enable_samu_dpm(struct radeon_device *rdev, bool enable)
3543{
3544 struct ci_power_info *pi = ci_get_pi(rdev);
3545 const struct radeon_clock_and_voltage_limits *max_limits;
3546 int i;
3547
3548 if (rdev->pm.dpm.ac_power)
3549 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
3550 else
3551 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
3552
3553 if (enable) {
3554 pi->dpm_level_enable_mask.samu_dpm_enable_mask = 0;
3555 for (i = rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
3556 if (rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
3557 pi->dpm_level_enable_mask.samu_dpm_enable_mask |= 1 << i;
3558
3559 if (!pi->caps_samu_dpm)
3560 break;
3561 }
3562 }
3563
3564 ci_send_msg_to_smc_with_parameter(rdev,
3565 PPSMC_MSG_SAMUDPM_SetEnabledMask,
3566 pi->dpm_level_enable_mask.samu_dpm_enable_mask);
3567 }
3568 return (ci_send_msg_to_smc(rdev, enable ?
3569 PPSMC_MSG_SAMUDPM_Enable : PPSMC_MSG_SAMUDPM_Disable) == PPSMC_Result_OK) ?
3570 0 : -EINVAL;
3571}
3572
3573static int ci_enable_acp_dpm(struct radeon_device *rdev, bool enable)
3574{
3575 struct ci_power_info *pi = ci_get_pi(rdev);
3576 const struct radeon_clock_and_voltage_limits *max_limits;
3577 int i;
3578
3579 if (rdev->pm.dpm.ac_power)
3580 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
3581 else
3582 max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
3583
3584 if (enable) {
3585 pi->dpm_level_enable_mask.acp_dpm_enable_mask = 0;
3586 for (i = rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
3587 if (rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
3588 pi->dpm_level_enable_mask.acp_dpm_enable_mask |= 1 << i;
3589
3590 if (!pi->caps_acp_dpm)
3591 break;
3592 }
3593 }
3594
3595 ci_send_msg_to_smc_with_parameter(rdev,
3596 PPSMC_MSG_ACPDPM_SetEnabledMask,
3597 pi->dpm_level_enable_mask.acp_dpm_enable_mask);
3598 }
3599
3600 return (ci_send_msg_to_smc(rdev, enable ?
3601 PPSMC_MSG_ACPDPM_Enable : PPSMC_MSG_ACPDPM_Disable) == PPSMC_Result_OK) ?
3602 0 : -EINVAL;
3603}
3604#endif
3605
3606static int ci_update_uvd_dpm(struct radeon_device *rdev, bool gate)
3607{
3608 struct ci_power_info *pi = ci_get_pi(rdev);
3609 u32 tmp;
3610
3611 if (!gate) {
3612 if (pi->caps_uvd_dpm ||
3613 (rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count <= 0))
3614 pi->smc_state_table.UvdBootLevel = 0;
3615 else
3616 pi->smc_state_table.UvdBootLevel =
3617 rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count - 1;
3618
3619 tmp = RREG32_SMC(DPM_TABLE_475);
3620 tmp &= ~UvdBootLevel_MASK;
3621 tmp |= UvdBootLevel(pi->smc_state_table.UvdBootLevel);
3622 WREG32_SMC(DPM_TABLE_475, tmp);
3623 }
3624
3625 return ci_enable_uvd_dpm(rdev, !gate);
3626}
3627
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003628static u8 ci_get_vce_boot_level(struct radeon_device *rdev)
3629{
3630 u8 i;
3631 u32 min_evclk = 30000; /* ??? */
3632 struct radeon_vce_clock_voltage_dependency_table *table =
3633 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
3634
3635 for (i = 0; i < table->count; i++) {
3636 if (table->entries[i].evclk >= min_evclk)
3637 return i;
3638 }
3639
3640 return table->count - 1;
3641}
3642
3643static int ci_update_vce_dpm(struct radeon_device *rdev,
3644 struct radeon_ps *radeon_new_state,
3645 struct radeon_ps *radeon_current_state)
3646{
3647 struct ci_power_info *pi = ci_get_pi(rdev);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003648 int ret = 0;
3649 u32 tmp;
3650
Alex Deucher8cd36682013-08-23 11:05:24 -04003651 if (radeon_current_state->evclk != radeon_new_state->evclk) {
3652 if (radeon_new_state->evclk) {
Alex Deuchera1d6f972013-09-06 12:33:04 -04003653 /* turn the clocks on when encoding */
3654 cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, false);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003655
Alex Deuchera1d6f972013-09-06 12:33:04 -04003656 pi->smc_state_table.VceBootLevel = ci_get_vce_boot_level(rdev);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003657 tmp = RREG32_SMC(DPM_TABLE_475);
3658 tmp &= ~VceBootLevel_MASK;
3659 tmp |= VceBootLevel(pi->smc_state_table.VceBootLevel);
3660 WREG32_SMC(DPM_TABLE_475, tmp);
3661
3662 ret = ci_enable_vce_dpm(rdev, true);
3663 } else {
Alex Deuchera1d6f972013-09-06 12:33:04 -04003664 /* turn the clocks off when not encoding */
3665 cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, true);
3666
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003667 ret = ci_enable_vce_dpm(rdev, false);
3668 }
3669 }
3670 return ret;
3671}
3672
Alex Deucher8cd36682013-08-23 11:05:24 -04003673#if 0
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003674static int ci_update_samu_dpm(struct radeon_device *rdev, bool gate)
3675{
3676 return ci_enable_samu_dpm(rdev, gate);
3677}
3678
3679static int ci_update_acp_dpm(struct radeon_device *rdev, bool gate)
3680{
3681 struct ci_power_info *pi = ci_get_pi(rdev);
3682 u32 tmp;
3683
3684 if (!gate) {
3685 pi->smc_state_table.AcpBootLevel = 0;
3686
3687 tmp = RREG32_SMC(DPM_TABLE_475);
3688 tmp &= ~AcpBootLevel_MASK;
3689 tmp |= AcpBootLevel(pi->smc_state_table.AcpBootLevel);
3690 WREG32_SMC(DPM_TABLE_475, tmp);
3691 }
3692
3693 return ci_enable_acp_dpm(rdev, !gate);
3694}
3695#endif
3696
3697static int ci_generate_dpm_level_enable_mask(struct radeon_device *rdev,
3698 struct radeon_ps *radeon_state)
3699{
3700 struct ci_power_info *pi = ci_get_pi(rdev);
3701 int ret;
3702
3703 ret = ci_trim_dpm_states(rdev, radeon_state);
3704 if (ret)
3705 return ret;
3706
3707 pi->dpm_level_enable_mask.sclk_dpm_enable_mask =
3708 ci_get_dpm_level_enable_mask_value(&pi->dpm_table.sclk_table);
3709 pi->dpm_level_enable_mask.mclk_dpm_enable_mask =
3710 ci_get_dpm_level_enable_mask_value(&pi->dpm_table.mclk_table);
3711 pi->last_mclk_dpm_enable_mask =
3712 pi->dpm_level_enable_mask.mclk_dpm_enable_mask;
3713 if (pi->uvd_enabled) {
3714 if (pi->dpm_level_enable_mask.mclk_dpm_enable_mask & 1)
3715 pi->dpm_level_enable_mask.mclk_dpm_enable_mask &= 0xFFFFFFFE;
3716 }
3717 pi->dpm_level_enable_mask.pcie_dpm_enable_mask =
3718 ci_get_dpm_level_enable_mask_value(&pi->dpm_table.pcie_speed_table);
3719
3720 return 0;
3721}
3722
Alex Deucher89536fd2013-07-15 18:14:24 -04003723static u32 ci_get_lowest_enabled_level(struct radeon_device *rdev,
3724 u32 level_mask)
3725{
3726 u32 level = 0;
3727
3728 while ((level_mask & (1 << level)) == 0)
3729 level++;
3730
3731 return level;
3732}
3733
3734
3735int ci_dpm_force_performance_level(struct radeon_device *rdev,
3736 enum radeon_dpm_forced_level level)
3737{
3738 struct ci_power_info *pi = ci_get_pi(rdev);
Alex Deucher89536fd2013-07-15 18:14:24 -04003739 u32 tmp, levels, i;
3740 int ret;
3741
3742 if (level == RADEON_DPM_FORCED_LEVEL_HIGH) {
3743 if ((!pi->sclk_dpm_key_disabled) &&
3744 pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
3745 levels = 0;
3746 tmp = pi->dpm_level_enable_mask.sclk_dpm_enable_mask;
3747 while (tmp >>= 1)
3748 levels++;
3749 if (levels) {
3750 ret = ci_dpm_force_state_sclk(rdev, levels);
3751 if (ret)
3752 return ret;
3753 for (i = 0; i < rdev->usec_timeout; i++) {
3754 tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
3755 CURR_SCLK_INDEX_MASK) >> CURR_SCLK_INDEX_SHIFT;
3756 if (tmp == levels)
3757 break;
3758 udelay(1);
3759 }
3760 }
3761 }
3762 if ((!pi->mclk_dpm_key_disabled) &&
3763 pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
3764 levels = 0;
3765 tmp = pi->dpm_level_enable_mask.mclk_dpm_enable_mask;
3766 while (tmp >>= 1)
3767 levels++;
3768 if (levels) {
3769 ret = ci_dpm_force_state_mclk(rdev, levels);
3770 if (ret)
3771 return ret;
3772 for (i = 0; i < rdev->usec_timeout; i++) {
3773 tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
3774 CURR_MCLK_INDEX_MASK) >> CURR_MCLK_INDEX_SHIFT;
3775 if (tmp == levels)
3776 break;
3777 udelay(1);
3778 }
3779 }
3780 }
3781 if ((!pi->pcie_dpm_key_disabled) &&
3782 pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
3783 levels = 0;
3784 tmp = pi->dpm_level_enable_mask.pcie_dpm_enable_mask;
3785 while (tmp >>= 1)
3786 levels++;
3787 if (levels) {
3788 ret = ci_dpm_force_state_pcie(rdev, level);
3789 if (ret)
3790 return ret;
3791 for (i = 0; i < rdev->usec_timeout; i++) {
3792 tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX_1) &
3793 CURR_PCIE_INDEX_MASK) >> CURR_PCIE_INDEX_SHIFT;
3794 if (tmp == levels)
3795 break;
3796 udelay(1);
3797 }
3798 }
3799 }
3800 } else if (level == RADEON_DPM_FORCED_LEVEL_LOW) {
3801 if ((!pi->sclk_dpm_key_disabled) &&
3802 pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
3803 levels = ci_get_lowest_enabled_level(rdev,
3804 pi->dpm_level_enable_mask.sclk_dpm_enable_mask);
3805 ret = ci_dpm_force_state_sclk(rdev, levels);
3806 if (ret)
3807 return ret;
3808 for (i = 0; i < rdev->usec_timeout; i++) {
3809 tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
3810 CURR_SCLK_INDEX_MASK) >> CURR_SCLK_INDEX_SHIFT;
3811 if (tmp == levels)
3812 break;
3813 udelay(1);
3814 }
3815 }
3816 if ((!pi->mclk_dpm_key_disabled) &&
3817 pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
3818 levels = ci_get_lowest_enabled_level(rdev,
3819 pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
3820 ret = ci_dpm_force_state_mclk(rdev, levels);
3821 if (ret)
3822 return ret;
3823 for (i = 0; i < rdev->usec_timeout; i++) {
3824 tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
3825 CURR_MCLK_INDEX_MASK) >> CURR_MCLK_INDEX_SHIFT;
3826 if (tmp == levels)
3827 break;
3828 udelay(1);
3829 }
3830 }
3831 if ((!pi->pcie_dpm_key_disabled) &&
3832 pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
3833 levels = ci_get_lowest_enabled_level(rdev,
3834 pi->dpm_level_enable_mask.pcie_dpm_enable_mask);
3835 ret = ci_dpm_force_state_pcie(rdev, levels);
3836 if (ret)
3837 return ret;
3838 for (i = 0; i < rdev->usec_timeout; i++) {
3839 tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX_1) &
3840 CURR_PCIE_INDEX_MASK) >> CURR_PCIE_INDEX_SHIFT;
3841 if (tmp == levels)
3842 break;
3843 udelay(1);
3844 }
3845 }
3846 } else if (level == RADEON_DPM_FORCED_LEVEL_AUTO) {
Alex Deucher1c522792014-11-07 12:06:56 -05003847 ret = ci_upload_dpm_level_enable_mask(rdev);
3848 if (ret)
3849 return ret;
Alex Deucher89536fd2013-07-15 18:14:24 -04003850 }
3851
3852 rdev->pm.dpm.forced_level = level;
3853
3854 return 0;
3855}
3856
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04003857static int ci_set_mc_special_registers(struct radeon_device *rdev,
3858 struct ci_mc_reg_table *table)
3859{
3860 struct ci_power_info *pi = ci_get_pi(rdev);
3861 u8 i, j, k;
3862 u32 temp_reg;
3863
3864 for (i = 0, j = table->last; i < table->last; i++) {
3865 if (j >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
3866 return -EINVAL;
3867 switch(table->mc_reg_address[i].s1 << 2) {
3868 case MC_SEQ_MISC1:
3869 temp_reg = RREG32(MC_PMG_CMD_EMRS);
3870 table->mc_reg_address[j].s1 = MC_PMG_CMD_EMRS >> 2;
3871 table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_EMRS_LP >> 2;
3872 for (k = 0; k < table->num_entries; k++) {
3873 table->mc_reg_table_entry[k].mc_data[j] =
3874 ((temp_reg & 0xffff0000)) | ((table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16);
3875 }
3876 j++;
3877 if (j >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
3878 return -EINVAL;
3879
3880 temp_reg = RREG32(MC_PMG_CMD_MRS);
3881 table->mc_reg_address[j].s1 = MC_PMG_CMD_MRS >> 2;
3882 table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_MRS_LP >> 2;
3883 for (k = 0; k < table->num_entries; k++) {
3884 table->mc_reg_table_entry[k].mc_data[j] =
3885 (temp_reg & 0xffff0000) | (table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
3886 if (!pi->mem_gddr5)
3887 table->mc_reg_table_entry[k].mc_data[j] |= 0x100;
3888 }
3889 j++;
3890 if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
3891 return -EINVAL;
3892
3893 if (!pi->mem_gddr5) {
3894 table->mc_reg_address[j].s1 = MC_PMG_AUTO_CMD >> 2;
3895 table->mc_reg_address[j].s0 = MC_PMG_AUTO_CMD >> 2;
3896 for (k = 0; k < table->num_entries; k++) {
3897 table->mc_reg_table_entry[k].mc_data[j] =
3898 (table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16;
3899 }
3900 j++;
3901 if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
3902 return -EINVAL;
3903 }
3904 break;
3905 case MC_SEQ_RESERVE_M:
3906 temp_reg = RREG32(MC_PMG_CMD_MRS1);
3907 table->mc_reg_address[j].s1 = MC_PMG_CMD_MRS1 >> 2;
3908 table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_MRS1_LP >> 2;
3909 for (k = 0; k < table->num_entries; k++) {
3910 table->mc_reg_table_entry[k].mc_data[j] =
3911 (temp_reg & 0xffff0000) | (table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
3912 }
3913 j++;
3914 if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
3915 return -EINVAL;
3916 break;
3917 default:
3918 break;
3919 }
3920
3921 }
3922
3923 table->last = j;
3924
3925 return 0;
3926}
3927
3928static bool ci_check_s0_mc_reg_index(u16 in_reg, u16 *out_reg)
3929{
3930 bool result = true;
3931
3932 switch(in_reg) {
3933 case MC_SEQ_RAS_TIMING >> 2:
3934 *out_reg = MC_SEQ_RAS_TIMING_LP >> 2;
3935 break;
3936 case MC_SEQ_DLL_STBY >> 2:
3937 *out_reg = MC_SEQ_DLL_STBY_LP >> 2;
3938 break;
3939 case MC_SEQ_G5PDX_CMD0 >> 2:
3940 *out_reg = MC_SEQ_G5PDX_CMD0_LP >> 2;
3941 break;
3942 case MC_SEQ_G5PDX_CMD1 >> 2:
3943 *out_reg = MC_SEQ_G5PDX_CMD1_LP >> 2;
3944 break;
3945 case MC_SEQ_G5PDX_CTRL >> 2:
3946 *out_reg = MC_SEQ_G5PDX_CTRL_LP >> 2;
3947 break;
3948 case MC_SEQ_CAS_TIMING >> 2:
3949 *out_reg = MC_SEQ_CAS_TIMING_LP >> 2;
3950 break;
3951 case MC_SEQ_MISC_TIMING >> 2:
3952 *out_reg = MC_SEQ_MISC_TIMING_LP >> 2;
3953 break;
3954 case MC_SEQ_MISC_TIMING2 >> 2:
3955 *out_reg = MC_SEQ_MISC_TIMING2_LP >> 2;
3956 break;
3957 case MC_SEQ_PMG_DVS_CMD >> 2:
3958 *out_reg = MC_SEQ_PMG_DVS_CMD_LP >> 2;
3959 break;
3960 case MC_SEQ_PMG_DVS_CTL >> 2:
3961 *out_reg = MC_SEQ_PMG_DVS_CTL_LP >> 2;
3962 break;
3963 case MC_SEQ_RD_CTL_D0 >> 2:
3964 *out_reg = MC_SEQ_RD_CTL_D0_LP >> 2;
3965 break;
3966 case MC_SEQ_RD_CTL_D1 >> 2:
3967 *out_reg = MC_SEQ_RD_CTL_D1_LP >> 2;
3968 break;
3969 case MC_SEQ_WR_CTL_D0 >> 2:
3970 *out_reg = MC_SEQ_WR_CTL_D0_LP >> 2;
3971 break;
3972 case MC_SEQ_WR_CTL_D1 >> 2:
3973 *out_reg = MC_SEQ_WR_CTL_D1_LP >> 2;
3974 break;
3975 case MC_PMG_CMD_EMRS >> 2:
3976 *out_reg = MC_SEQ_PMG_CMD_EMRS_LP >> 2;
3977 break;
3978 case MC_PMG_CMD_MRS >> 2:
3979 *out_reg = MC_SEQ_PMG_CMD_MRS_LP >> 2;
3980 break;
3981 case MC_PMG_CMD_MRS1 >> 2:
3982 *out_reg = MC_SEQ_PMG_CMD_MRS1_LP >> 2;
3983 break;
3984 case MC_SEQ_PMG_TIMING >> 2:
3985 *out_reg = MC_SEQ_PMG_TIMING_LP >> 2;
3986 break;
3987 case MC_PMG_CMD_MRS2 >> 2:
3988 *out_reg = MC_SEQ_PMG_CMD_MRS2_LP >> 2;
3989 break;
3990 case MC_SEQ_WR_CTL_2 >> 2:
3991 *out_reg = MC_SEQ_WR_CTL_2_LP >> 2;
3992 break;
3993 default:
3994 result = false;
3995 break;
3996 }
3997
3998 return result;
3999}
4000
4001static void ci_set_valid_flag(struct ci_mc_reg_table *table)
4002{
4003 u8 i, j;
4004
4005 for (i = 0; i < table->last; i++) {
4006 for (j = 1; j < table->num_entries; j++) {
4007 if (table->mc_reg_table_entry[j-1].mc_data[i] !=
4008 table->mc_reg_table_entry[j].mc_data[i]) {
4009 table->valid_flag |= 1 << i;
4010 break;
4011 }
4012 }
4013 }
4014}
4015
4016static void ci_set_s0_mc_reg_index(struct ci_mc_reg_table *table)
4017{
4018 u32 i;
4019 u16 address;
4020
4021 for (i = 0; i < table->last; i++) {
4022 table->mc_reg_address[i].s0 =
4023 ci_check_s0_mc_reg_index(table->mc_reg_address[i].s1, &address) ?
4024 address : table->mc_reg_address[i].s1;
4025 }
4026}
4027
4028static int ci_copy_vbios_mc_reg_table(const struct atom_mc_reg_table *table,
4029 struct ci_mc_reg_table *ci_table)
4030{
4031 u8 i, j;
4032
4033 if (table->last > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
4034 return -EINVAL;
4035 if (table->num_entries > MAX_AC_TIMING_ENTRIES)
4036 return -EINVAL;
4037
4038 for (i = 0; i < table->last; i++)
4039 ci_table->mc_reg_address[i].s1 = table->mc_reg_address[i].s1;
4040
4041 ci_table->last = table->last;
4042
4043 for (i = 0; i < table->num_entries; i++) {
4044 ci_table->mc_reg_table_entry[i].mclk_max =
4045 table->mc_reg_table_entry[i].mclk_max;
4046 for (j = 0; j < table->last; j++)
4047 ci_table->mc_reg_table_entry[i].mc_data[j] =
4048 table->mc_reg_table_entry[i].mc_data[j];
4049 }
4050 ci_table->num_entries = table->num_entries;
4051
4052 return 0;
4053}
4054
4055static int ci_initialize_mc_reg_table(struct radeon_device *rdev)
4056{
4057 struct ci_power_info *pi = ci_get_pi(rdev);
4058 struct atom_mc_reg_table *table;
4059 struct ci_mc_reg_table *ci_table = &pi->mc_reg_table;
4060 u8 module_index = rv770_get_memory_module_index(rdev);
4061 int ret;
4062
4063 table = kzalloc(sizeof(struct atom_mc_reg_table), GFP_KERNEL);
4064 if (!table)
4065 return -ENOMEM;
4066
4067 WREG32(MC_SEQ_RAS_TIMING_LP, RREG32(MC_SEQ_RAS_TIMING));
4068 WREG32(MC_SEQ_CAS_TIMING_LP, RREG32(MC_SEQ_CAS_TIMING));
4069 WREG32(MC_SEQ_DLL_STBY_LP, RREG32(MC_SEQ_DLL_STBY));
4070 WREG32(MC_SEQ_G5PDX_CMD0_LP, RREG32(MC_SEQ_G5PDX_CMD0));
4071 WREG32(MC_SEQ_G5PDX_CMD1_LP, RREG32(MC_SEQ_G5PDX_CMD1));
4072 WREG32(MC_SEQ_G5PDX_CTRL_LP, RREG32(MC_SEQ_G5PDX_CTRL));
4073 WREG32(MC_SEQ_PMG_DVS_CMD_LP, RREG32(MC_SEQ_PMG_DVS_CMD));
4074 WREG32(MC_SEQ_PMG_DVS_CTL_LP, RREG32(MC_SEQ_PMG_DVS_CTL));
4075 WREG32(MC_SEQ_MISC_TIMING_LP, RREG32(MC_SEQ_MISC_TIMING));
4076 WREG32(MC_SEQ_MISC_TIMING2_LP, RREG32(MC_SEQ_MISC_TIMING2));
4077 WREG32(MC_SEQ_PMG_CMD_EMRS_LP, RREG32(MC_PMG_CMD_EMRS));
4078 WREG32(MC_SEQ_PMG_CMD_MRS_LP, RREG32(MC_PMG_CMD_MRS));
4079 WREG32(MC_SEQ_PMG_CMD_MRS1_LP, RREG32(MC_PMG_CMD_MRS1));
4080 WREG32(MC_SEQ_WR_CTL_D0_LP, RREG32(MC_SEQ_WR_CTL_D0));
4081 WREG32(MC_SEQ_WR_CTL_D1_LP, RREG32(MC_SEQ_WR_CTL_D1));
4082 WREG32(MC_SEQ_RD_CTL_D0_LP, RREG32(MC_SEQ_RD_CTL_D0));
4083 WREG32(MC_SEQ_RD_CTL_D1_LP, RREG32(MC_SEQ_RD_CTL_D1));
4084 WREG32(MC_SEQ_PMG_TIMING_LP, RREG32(MC_SEQ_PMG_TIMING));
4085 WREG32(MC_SEQ_PMG_CMD_MRS2_LP, RREG32(MC_PMG_CMD_MRS2));
4086 WREG32(MC_SEQ_WR_CTL_2_LP, RREG32(MC_SEQ_WR_CTL_2));
4087
4088 ret = radeon_atom_init_mc_reg_table(rdev, module_index, table);
4089 if (ret)
4090 goto init_mc_done;
4091
4092 ret = ci_copy_vbios_mc_reg_table(table, ci_table);
4093 if (ret)
4094 goto init_mc_done;
4095
4096 ci_set_s0_mc_reg_index(ci_table);
4097
4098 ret = ci_set_mc_special_registers(rdev, ci_table);
4099 if (ret)
4100 goto init_mc_done;
4101
4102 ci_set_valid_flag(ci_table);
4103
4104init_mc_done:
4105 kfree(table);
4106
4107 return ret;
4108}
4109
4110static int ci_populate_mc_reg_addresses(struct radeon_device *rdev,
4111 SMU7_Discrete_MCRegisters *mc_reg_table)
4112{
4113 struct ci_power_info *pi = ci_get_pi(rdev);
4114 u32 i, j;
4115
4116 for (i = 0, j = 0; j < pi->mc_reg_table.last; j++) {
4117 if (pi->mc_reg_table.valid_flag & (1 << j)) {
4118 if (i >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
4119 return -EINVAL;
4120 mc_reg_table->address[i].s0 = cpu_to_be16(pi->mc_reg_table.mc_reg_address[j].s0);
4121 mc_reg_table->address[i].s1 = cpu_to_be16(pi->mc_reg_table.mc_reg_address[j].s1);
4122 i++;
4123 }
4124 }
4125
4126 mc_reg_table->last = (u8)i;
4127
4128 return 0;
4129}
4130
4131static void ci_convert_mc_registers(const struct ci_mc_reg_entry *entry,
4132 SMU7_Discrete_MCRegisterSet *data,
4133 u32 num_entries, u32 valid_flag)
4134{
4135 u32 i, j;
4136
4137 for (i = 0, j = 0; j < num_entries; j++) {
4138 if (valid_flag & (1 << j)) {
4139 data->value[i] = cpu_to_be32(entry->mc_data[j]);
4140 i++;
4141 }
4142 }
4143}
4144
4145static void ci_convert_mc_reg_table_entry_to_smc(struct radeon_device *rdev,
4146 const u32 memory_clock,
4147 SMU7_Discrete_MCRegisterSet *mc_reg_table_data)
4148{
4149 struct ci_power_info *pi = ci_get_pi(rdev);
4150 u32 i = 0;
4151
4152 for(i = 0; i < pi->mc_reg_table.num_entries; i++) {
4153 if (memory_clock <= pi->mc_reg_table.mc_reg_table_entry[i].mclk_max)
4154 break;
4155 }
4156
4157 if ((i == pi->mc_reg_table.num_entries) && (i > 0))
4158 --i;
4159
4160 ci_convert_mc_registers(&pi->mc_reg_table.mc_reg_table_entry[i],
4161 mc_reg_table_data, pi->mc_reg_table.last,
4162 pi->mc_reg_table.valid_flag);
4163}
4164
4165static void ci_convert_mc_reg_table_to_smc(struct radeon_device *rdev,
4166 SMU7_Discrete_MCRegisters *mc_reg_table)
4167{
4168 struct ci_power_info *pi = ci_get_pi(rdev);
4169 u32 i;
4170
4171 for (i = 0; i < pi->dpm_table.mclk_table.count; i++)
4172 ci_convert_mc_reg_table_entry_to_smc(rdev,
4173 pi->dpm_table.mclk_table.dpm_levels[i].value,
4174 &mc_reg_table->data[i]);
4175}
4176
4177static int ci_populate_initial_mc_reg_table(struct radeon_device *rdev)
4178{
4179 struct ci_power_info *pi = ci_get_pi(rdev);
4180 int ret;
4181
4182 memset(&pi->smc_mc_reg_table, 0, sizeof(SMU7_Discrete_MCRegisters));
4183
4184 ret = ci_populate_mc_reg_addresses(rdev, &pi->smc_mc_reg_table);
4185 if (ret)
4186 return ret;
4187 ci_convert_mc_reg_table_to_smc(rdev, &pi->smc_mc_reg_table);
4188
4189 return ci_copy_bytes_to_smc(rdev,
4190 pi->mc_reg_table_start,
4191 (u8 *)&pi->smc_mc_reg_table,
4192 sizeof(SMU7_Discrete_MCRegisters),
4193 pi->sram_end);
4194}
4195
4196static int ci_update_and_upload_mc_reg_table(struct radeon_device *rdev)
4197{
4198 struct ci_power_info *pi = ci_get_pi(rdev);
4199
4200 if (!(pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK))
4201 return 0;
4202
4203 memset(&pi->smc_mc_reg_table, 0, sizeof(SMU7_Discrete_MCRegisters));
4204
4205 ci_convert_mc_reg_table_to_smc(rdev, &pi->smc_mc_reg_table);
4206
4207 return ci_copy_bytes_to_smc(rdev,
4208 pi->mc_reg_table_start +
4209 offsetof(SMU7_Discrete_MCRegisters, data[0]),
4210 (u8 *)&pi->smc_mc_reg_table.data[0],
4211 sizeof(SMU7_Discrete_MCRegisterSet) *
4212 pi->dpm_table.mclk_table.count,
4213 pi->sram_end);
4214}
4215
4216static void ci_enable_voltage_control(struct radeon_device *rdev)
4217{
4218 u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
4219
4220 tmp |= VOLT_PWRMGT_EN;
4221 WREG32_SMC(GENERAL_PWRMGT, tmp);
4222}
4223
4224static enum radeon_pcie_gen ci_get_maximum_link_speed(struct radeon_device *rdev,
4225 struct radeon_ps *radeon_state)
4226{
4227 struct ci_ps *state = ci_get_ps(radeon_state);
4228 int i;
4229 u16 pcie_speed, max_speed = 0;
4230
4231 for (i = 0; i < state->performance_level_count; i++) {
4232 pcie_speed = state->performance_levels[i].pcie_gen;
4233 if (max_speed < pcie_speed)
4234 max_speed = pcie_speed;
4235 }
4236
4237 return max_speed;
4238}
4239
4240static u16 ci_get_current_pcie_speed(struct radeon_device *rdev)
4241{
4242 u32 speed_cntl = 0;
4243
4244 speed_cntl = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL) & LC_CURRENT_DATA_RATE_MASK;
4245 speed_cntl >>= LC_CURRENT_DATA_RATE_SHIFT;
4246
4247 return (u16)speed_cntl;
4248}
4249
4250static int ci_get_current_pcie_lane_number(struct radeon_device *rdev)
4251{
4252 u32 link_width = 0;
4253
4254 link_width = RREG32_PCIE_PORT(PCIE_LC_LINK_WIDTH_CNTL) & LC_LINK_WIDTH_RD_MASK;
4255 link_width >>= LC_LINK_WIDTH_RD_SHIFT;
4256
4257 switch (link_width) {
4258 case RADEON_PCIE_LC_LINK_WIDTH_X1:
4259 return 1;
4260 case RADEON_PCIE_LC_LINK_WIDTH_X2:
4261 return 2;
4262 case RADEON_PCIE_LC_LINK_WIDTH_X4:
4263 return 4;
4264 case RADEON_PCIE_LC_LINK_WIDTH_X8:
4265 return 8;
4266 case RADEON_PCIE_LC_LINK_WIDTH_X12:
4267 /* not actually supported */
4268 return 12;
4269 case RADEON_PCIE_LC_LINK_WIDTH_X0:
4270 case RADEON_PCIE_LC_LINK_WIDTH_X16:
4271 default:
4272 return 16;
4273 }
4274}
4275
4276static void ci_request_link_speed_change_before_state_change(struct radeon_device *rdev,
4277 struct radeon_ps *radeon_new_state,
4278 struct radeon_ps *radeon_current_state)
4279{
4280 struct ci_power_info *pi = ci_get_pi(rdev);
4281 enum radeon_pcie_gen target_link_speed =
4282 ci_get_maximum_link_speed(rdev, radeon_new_state);
4283 enum radeon_pcie_gen current_link_speed;
4284
4285 if (pi->force_pcie_gen == RADEON_PCIE_GEN_INVALID)
4286 current_link_speed = ci_get_maximum_link_speed(rdev, radeon_current_state);
4287 else
4288 current_link_speed = pi->force_pcie_gen;
4289
4290 pi->force_pcie_gen = RADEON_PCIE_GEN_INVALID;
4291 pi->pspp_notify_required = false;
4292 if (target_link_speed > current_link_speed) {
4293 switch (target_link_speed) {
Stephen Rothwellab62e762013-09-02 19:01:23 +10004294#ifdef CONFIG_ACPI
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004295 case RADEON_PCIE_GEN3:
4296 if (radeon_acpi_pcie_performance_request(rdev, PCIE_PERF_REQ_PECI_GEN3, false) == 0)
4297 break;
4298 pi->force_pcie_gen = RADEON_PCIE_GEN2;
4299 if (current_link_speed == RADEON_PCIE_GEN2)
4300 break;
4301 case RADEON_PCIE_GEN2:
4302 if (radeon_acpi_pcie_performance_request(rdev, PCIE_PERF_REQ_PECI_GEN2, false) == 0)
4303 break;
Stephen Rothwellab62e762013-09-02 19:01:23 +10004304#endif
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004305 default:
4306 pi->force_pcie_gen = ci_get_current_pcie_speed(rdev);
4307 break;
4308 }
4309 } else {
4310 if (target_link_speed < current_link_speed)
4311 pi->pspp_notify_required = true;
4312 }
4313}
4314
4315static void ci_notify_link_speed_change_after_state_change(struct radeon_device *rdev,
4316 struct radeon_ps *radeon_new_state,
4317 struct radeon_ps *radeon_current_state)
4318{
4319 struct ci_power_info *pi = ci_get_pi(rdev);
4320 enum radeon_pcie_gen target_link_speed =
4321 ci_get_maximum_link_speed(rdev, radeon_new_state);
4322 u8 request;
4323
4324 if (pi->pspp_notify_required) {
4325 if (target_link_speed == RADEON_PCIE_GEN3)
4326 request = PCIE_PERF_REQ_PECI_GEN3;
4327 else if (target_link_speed == RADEON_PCIE_GEN2)
4328 request = PCIE_PERF_REQ_PECI_GEN2;
4329 else
4330 request = PCIE_PERF_REQ_PECI_GEN1;
4331
4332 if ((request == PCIE_PERF_REQ_PECI_GEN1) &&
4333 (ci_get_current_pcie_speed(rdev) > 0))
4334 return;
4335
Stephen Rothwellab62e762013-09-02 19:01:23 +10004336#ifdef CONFIG_ACPI
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004337 radeon_acpi_pcie_performance_request(rdev, request, false);
Stephen Rothwellab62e762013-09-02 19:01:23 +10004338#endif
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004339 }
4340}
4341
4342static int ci_set_private_data_variables_based_on_pptable(struct radeon_device *rdev)
4343{
4344 struct ci_power_info *pi = ci_get_pi(rdev);
4345 struct radeon_clock_voltage_dependency_table *allowed_sclk_vddc_table =
4346 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
4347 struct radeon_clock_voltage_dependency_table *allowed_mclk_vddc_table =
4348 &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk;
4349 struct radeon_clock_voltage_dependency_table *allowed_mclk_vddci_table =
4350 &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk;
4351
4352 if (allowed_sclk_vddc_table == NULL)
4353 return -EINVAL;
4354 if (allowed_sclk_vddc_table->count < 1)
4355 return -EINVAL;
4356 if (allowed_mclk_vddc_table == NULL)
4357 return -EINVAL;
4358 if (allowed_mclk_vddc_table->count < 1)
4359 return -EINVAL;
4360 if (allowed_mclk_vddci_table == NULL)
4361 return -EINVAL;
4362 if (allowed_mclk_vddci_table->count < 1)
4363 return -EINVAL;
4364
4365 pi->min_vddc_in_pp_table = allowed_sclk_vddc_table->entries[0].v;
4366 pi->max_vddc_in_pp_table =
4367 allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v;
4368
4369 pi->min_vddci_in_pp_table = allowed_mclk_vddci_table->entries[0].v;
4370 pi->max_vddci_in_pp_table =
4371 allowed_mclk_vddci_table->entries[allowed_mclk_vddci_table->count - 1].v;
4372
4373 rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk =
4374 allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].clk;
4375 rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.mclk =
4376 allowed_mclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].clk;
4377 rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddc =
4378 allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v;
4379 rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddci =
4380 allowed_mclk_vddci_table->entries[allowed_mclk_vddci_table->count - 1].v;
4381
4382 return 0;
4383}
4384
4385static void ci_patch_with_vddc_leakage(struct radeon_device *rdev, u16 *vddc)
4386{
4387 struct ci_power_info *pi = ci_get_pi(rdev);
4388 struct ci_leakage_voltage *leakage_table = &pi->vddc_leakage;
4389 u32 leakage_index;
4390
4391 for (leakage_index = 0; leakage_index < leakage_table->count; leakage_index++) {
4392 if (leakage_table->leakage_id[leakage_index] == *vddc) {
4393 *vddc = leakage_table->actual_voltage[leakage_index];
4394 break;
4395 }
4396 }
4397}
4398
4399static void ci_patch_with_vddci_leakage(struct radeon_device *rdev, u16 *vddci)
4400{
4401 struct ci_power_info *pi = ci_get_pi(rdev);
4402 struct ci_leakage_voltage *leakage_table = &pi->vddci_leakage;
4403 u32 leakage_index;
4404
4405 for (leakage_index = 0; leakage_index < leakage_table->count; leakage_index++) {
4406 if (leakage_table->leakage_id[leakage_index] == *vddci) {
4407 *vddci = leakage_table->actual_voltage[leakage_index];
4408 break;
4409 }
4410 }
4411}
4412
4413static void ci_patch_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
4414 struct radeon_clock_voltage_dependency_table *table)
4415{
4416 u32 i;
4417
4418 if (table) {
4419 for (i = 0; i < table->count; i++)
4420 ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
4421 }
4422}
4423
4424static void ci_patch_clock_voltage_dependency_table_with_vddci_leakage(struct radeon_device *rdev,
4425 struct radeon_clock_voltage_dependency_table *table)
4426{
4427 u32 i;
4428
4429 if (table) {
4430 for (i = 0; i < table->count; i++)
4431 ci_patch_with_vddci_leakage(rdev, &table->entries[i].v);
4432 }
4433}
4434
4435static void ci_patch_vce_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
4436 struct radeon_vce_clock_voltage_dependency_table *table)
4437{
4438 u32 i;
4439
4440 if (table) {
4441 for (i = 0; i < table->count; i++)
4442 ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
4443 }
4444}
4445
4446static void ci_patch_uvd_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
4447 struct radeon_uvd_clock_voltage_dependency_table *table)
4448{
4449 u32 i;
4450
4451 if (table) {
4452 for (i = 0; i < table->count; i++)
4453 ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
4454 }
4455}
4456
4457static void ci_patch_vddc_phase_shed_limit_table_with_vddc_leakage(struct radeon_device *rdev,
4458 struct radeon_phase_shedding_limits_table *table)
4459{
4460 u32 i;
4461
4462 if (table) {
4463 for (i = 0; i < table->count; i++)
4464 ci_patch_with_vddc_leakage(rdev, &table->entries[i].voltage);
4465 }
4466}
4467
4468static void ci_patch_clock_voltage_limits_with_vddc_leakage(struct radeon_device *rdev,
4469 struct radeon_clock_and_voltage_limits *table)
4470{
4471 if (table) {
4472 ci_patch_with_vddc_leakage(rdev, (u16 *)&table->vddc);
4473 ci_patch_with_vddci_leakage(rdev, (u16 *)&table->vddci);
4474 }
4475}
4476
4477static void ci_patch_cac_leakage_table_with_vddc_leakage(struct radeon_device *rdev,
4478 struct radeon_cac_leakage_table *table)
4479{
4480 u32 i;
4481
4482 if (table) {
4483 for (i = 0; i < table->count; i++)
4484 ci_patch_with_vddc_leakage(rdev, &table->entries[i].vddc);
4485 }
4486}
4487
4488static void ci_patch_dependency_tables_with_leakage(struct radeon_device *rdev)
4489{
4490
4491 ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
4492 &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk);
4493 ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
4494 &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk);
4495 ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
4496 &rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk);
4497 ci_patch_clock_voltage_dependency_table_with_vddci_leakage(rdev,
4498 &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk);
4499 ci_patch_vce_clock_voltage_dependency_table_with_vddc_leakage(rdev,
4500 &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table);
4501 ci_patch_uvd_clock_voltage_dependency_table_with_vddc_leakage(rdev,
4502 &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table);
4503 ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
4504 &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table);
4505 ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
4506 &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table);
4507 ci_patch_vddc_phase_shed_limit_table_with_vddc_leakage(rdev,
4508 &rdev->pm.dpm.dyn_state.phase_shedding_limits_table);
4509 ci_patch_clock_voltage_limits_with_vddc_leakage(rdev,
4510 &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
4511 ci_patch_clock_voltage_limits_with_vddc_leakage(rdev,
4512 &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc);
4513 ci_patch_cac_leakage_table_with_vddc_leakage(rdev,
4514 &rdev->pm.dpm.dyn_state.cac_leakage_table);
4515
4516}
4517
4518static void ci_get_memory_type(struct radeon_device *rdev)
4519{
4520 struct ci_power_info *pi = ci_get_pi(rdev);
4521 u32 tmp;
4522
4523 tmp = RREG32(MC_SEQ_MISC0);
4524
4525 if (((tmp & MC_SEQ_MISC0_GDDR5_MASK) >> MC_SEQ_MISC0_GDDR5_SHIFT) ==
4526 MC_SEQ_MISC0_GDDR5_VALUE)
4527 pi->mem_gddr5 = true;
4528 else
4529 pi->mem_gddr5 = false;
4530
4531}
4532
Alex Deucher9a04dad2014-01-07 12:16:05 -05004533static void ci_update_current_ps(struct radeon_device *rdev,
4534 struct radeon_ps *rps)
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004535{
4536 struct ci_ps *new_ps = ci_get_ps(rps);
4537 struct ci_power_info *pi = ci_get_pi(rdev);
4538
4539 pi->current_rps = *rps;
4540 pi->current_ps = *new_ps;
4541 pi->current_rps.ps_priv = &pi->current_ps;
4542}
4543
Alex Deucher9a04dad2014-01-07 12:16:05 -05004544static void ci_update_requested_ps(struct radeon_device *rdev,
4545 struct radeon_ps *rps)
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004546{
4547 struct ci_ps *new_ps = ci_get_ps(rps);
4548 struct ci_power_info *pi = ci_get_pi(rdev);
4549
4550 pi->requested_rps = *rps;
4551 pi->requested_ps = *new_ps;
4552 pi->requested_rps.ps_priv = &pi->requested_ps;
4553}
4554
4555int ci_dpm_pre_set_power_state(struct radeon_device *rdev)
4556{
4557 struct ci_power_info *pi = ci_get_pi(rdev);
4558 struct radeon_ps requested_ps = *rdev->pm.dpm.requested_ps;
4559 struct radeon_ps *new_ps = &requested_ps;
4560
4561 ci_update_requested_ps(rdev, new_ps);
4562
4563 ci_apply_state_adjust_rules(rdev, &pi->requested_rps);
4564
4565 return 0;
4566}
4567
4568void ci_dpm_post_set_power_state(struct radeon_device *rdev)
4569{
4570 struct ci_power_info *pi = ci_get_pi(rdev);
4571 struct radeon_ps *new_ps = &pi->requested_rps;
4572
4573 ci_update_current_ps(rdev, new_ps);
4574}
4575
4576
4577void ci_dpm_setup_asic(struct radeon_device *rdev)
4578{
Alex Deucher6c7bcce2013-12-18 14:07:14 -05004579 int r;
4580
4581 r = ci_mc_load_microcode(rdev);
4582 if (r)
4583 DRM_ERROR("Failed to load MC firmware!\n");
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004584 ci_read_clock_registers(rdev);
4585 ci_get_memory_type(rdev);
4586 ci_enable_acpi_power_management(rdev);
4587 ci_init_sclk_t(rdev);
4588}
4589
4590int ci_dpm_enable(struct radeon_device *rdev)
4591{
4592 struct ci_power_info *pi = ci_get_pi(rdev);
4593 struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
4594 int ret;
4595
4596 if (ci_is_smc_running(rdev))
4597 return -EINVAL;
4598 if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
4599 ci_enable_voltage_control(rdev);
4600 ret = ci_construct_voltage_tables(rdev);
4601 if (ret) {
4602 DRM_ERROR("ci_construct_voltage_tables failed\n");
4603 return ret;
4604 }
4605 }
4606 if (pi->caps_dynamic_ac_timing) {
4607 ret = ci_initialize_mc_reg_table(rdev);
4608 if (ret)
4609 pi->caps_dynamic_ac_timing = false;
4610 }
4611 if (pi->dynamic_ss)
4612 ci_enable_spread_spectrum(rdev, true);
4613 if (pi->thermal_protection)
4614 ci_enable_thermal_protection(rdev, true);
4615 ci_program_sstp(rdev);
4616 ci_enable_display_gap(rdev);
4617 ci_program_vc(rdev);
4618 ret = ci_upload_firmware(rdev);
4619 if (ret) {
4620 DRM_ERROR("ci_upload_firmware failed\n");
4621 return ret;
4622 }
4623 ret = ci_process_firmware_header(rdev);
4624 if (ret) {
4625 DRM_ERROR("ci_process_firmware_header failed\n");
4626 return ret;
4627 }
4628 ret = ci_initial_switch_from_arb_f0_to_f1(rdev);
4629 if (ret) {
4630 DRM_ERROR("ci_initial_switch_from_arb_f0_to_f1 failed\n");
4631 return ret;
4632 }
4633 ret = ci_init_smc_table(rdev);
4634 if (ret) {
4635 DRM_ERROR("ci_init_smc_table failed\n");
4636 return ret;
4637 }
4638 ret = ci_init_arb_table_index(rdev);
4639 if (ret) {
4640 DRM_ERROR("ci_init_arb_table_index failed\n");
4641 return ret;
4642 }
4643 if (pi->caps_dynamic_ac_timing) {
4644 ret = ci_populate_initial_mc_reg_table(rdev);
4645 if (ret) {
4646 DRM_ERROR("ci_populate_initial_mc_reg_table failed\n");
4647 return ret;
4648 }
4649 }
4650 ret = ci_populate_pm_base(rdev);
4651 if (ret) {
4652 DRM_ERROR("ci_populate_pm_base failed\n");
4653 return ret;
4654 }
4655 ci_dpm_start_smc(rdev);
4656 ci_enable_vr_hot_gpio_interrupt(rdev);
4657 ret = ci_notify_smc_display_change(rdev, false);
4658 if (ret) {
4659 DRM_ERROR("ci_notify_smc_display_change failed\n");
4660 return ret;
4661 }
4662 ci_enable_sclk_control(rdev, true);
4663 ret = ci_enable_ulv(rdev, true);
4664 if (ret) {
4665 DRM_ERROR("ci_enable_ulv failed\n");
4666 return ret;
4667 }
4668 ret = ci_enable_ds_master_switch(rdev, true);
4669 if (ret) {
4670 DRM_ERROR("ci_enable_ds_master_switch failed\n");
4671 return ret;
4672 }
4673 ret = ci_start_dpm(rdev);
4674 if (ret) {
4675 DRM_ERROR("ci_start_dpm failed\n");
4676 return ret;
4677 }
4678 ret = ci_enable_didt(rdev, true);
4679 if (ret) {
4680 DRM_ERROR("ci_enable_didt failed\n");
4681 return ret;
4682 }
4683 ret = ci_enable_smc_cac(rdev, true);
4684 if (ret) {
4685 DRM_ERROR("ci_enable_smc_cac failed\n");
4686 return ret;
4687 }
4688 ret = ci_enable_power_containment(rdev, true);
4689 if (ret) {
4690 DRM_ERROR("ci_enable_power_containment failed\n");
4691 return ret;
4692 }
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004693
4694 ci_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, true);
4695
4696 ci_update_current_ps(rdev, boot_ps);
4697
4698 return 0;
4699}
4700
Alex Deucher1955f102014-09-14 23:45:30 -04004701static int ci_set_temperature_range(struct radeon_device *rdev)
4702{
4703 int ret;
4704
4705 ret = ci_thermal_enable_alert(rdev, false);
4706 if (ret)
4707 return ret;
4708 ret = ci_thermal_set_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
4709 if (ret)
4710 return ret;
4711 ret = ci_thermal_enable_alert(rdev, true);
4712 if (ret)
4713 return ret;
4714
4715 return ret;
4716}
4717
Alex Deucher90208422013-12-19 13:59:46 -05004718int ci_dpm_late_enable(struct radeon_device *rdev)
4719{
4720 int ret;
4721
Alex Deucher1955f102014-09-14 23:45:30 -04004722 ret = ci_set_temperature_range(rdev);
4723 if (ret)
4724 return ret;
Alex Deucher90208422013-12-19 13:59:46 -05004725
4726 ci_dpm_powergate_uvd(rdev, true);
4727
4728 return 0;
4729}
4730
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004731void ci_dpm_disable(struct radeon_device *rdev)
4732{
4733 struct ci_power_info *pi = ci_get_pi(rdev);
4734 struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
4735
Alex Deucher47acb1f2013-08-26 09:43:24 -04004736 ci_dpm_powergate_uvd(rdev, false);
4737
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004738 if (!ci_is_smc_running(rdev))
4739 return;
4740
4741 if (pi->thermal_protection)
4742 ci_enable_thermal_protection(rdev, false);
4743 ci_enable_power_containment(rdev, false);
4744 ci_enable_smc_cac(rdev, false);
4745 ci_enable_didt(rdev, false);
4746 ci_enable_spread_spectrum(rdev, false);
4747 ci_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, false);
4748 ci_stop_dpm(rdev);
Alex Deucher129acb72014-11-07 11:05:04 -05004749 ci_enable_ds_master_switch(rdev, false);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004750 ci_enable_ulv(rdev, false);
4751 ci_clear_vc(rdev);
4752 ci_reset_to_default(rdev);
4753 ci_dpm_stop_smc(rdev);
4754 ci_force_switch_to_arb_f0(rdev);
4755
4756 ci_update_current_ps(rdev, boot_ps);
4757}
4758
4759int ci_dpm_set_power_state(struct radeon_device *rdev)
4760{
4761 struct ci_power_info *pi = ci_get_pi(rdev);
4762 struct radeon_ps *new_ps = &pi->requested_rps;
4763 struct radeon_ps *old_ps = &pi->current_rps;
4764 int ret;
4765
4766 ci_find_dpm_states_clocks_in_dpm_table(rdev, new_ps);
4767 if (pi->pcie_performance_request)
4768 ci_request_link_speed_change_before_state_change(rdev, new_ps, old_ps);
4769 ret = ci_freeze_sclk_mclk_dpm(rdev);
4770 if (ret) {
4771 DRM_ERROR("ci_freeze_sclk_mclk_dpm failed\n");
4772 return ret;
4773 }
4774 ret = ci_populate_and_upload_sclk_mclk_dpm_levels(rdev, new_ps);
4775 if (ret) {
4776 DRM_ERROR("ci_populate_and_upload_sclk_mclk_dpm_levels failed\n");
4777 return ret;
4778 }
4779 ret = ci_generate_dpm_level_enable_mask(rdev, new_ps);
4780 if (ret) {
4781 DRM_ERROR("ci_generate_dpm_level_enable_mask failed\n");
4782 return ret;
4783 }
Alex Deucher8cd36682013-08-23 11:05:24 -04004784
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004785 ret = ci_update_vce_dpm(rdev, new_ps, old_ps);
4786 if (ret) {
4787 DRM_ERROR("ci_update_vce_dpm failed\n");
4788 return ret;
4789 }
Alex Deucher8cd36682013-08-23 11:05:24 -04004790
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004791 ret = ci_update_sclk_t(rdev);
4792 if (ret) {
4793 DRM_ERROR("ci_update_sclk_t failed\n");
4794 return ret;
4795 }
4796 if (pi->caps_dynamic_ac_timing) {
4797 ret = ci_update_and_upload_mc_reg_table(rdev);
4798 if (ret) {
4799 DRM_ERROR("ci_update_and_upload_mc_reg_table failed\n");
4800 return ret;
4801 }
4802 }
4803 ret = ci_program_memory_timing_parameters(rdev);
4804 if (ret) {
4805 DRM_ERROR("ci_program_memory_timing_parameters failed\n");
4806 return ret;
4807 }
4808 ret = ci_unfreeze_sclk_mclk_dpm(rdev);
4809 if (ret) {
4810 DRM_ERROR("ci_unfreeze_sclk_mclk_dpm failed\n");
4811 return ret;
4812 }
4813 ret = ci_upload_dpm_level_enable_mask(rdev);
4814 if (ret) {
4815 DRM_ERROR("ci_upload_dpm_level_enable_mask failed\n");
4816 return ret;
4817 }
4818 if (pi->pcie_performance_request)
4819 ci_notify_link_speed_change_after_state_change(rdev, new_ps, old_ps);
4820
4821 return 0;
4822}
4823
4824int ci_dpm_power_control_set_level(struct radeon_device *rdev)
4825{
4826 return ci_power_control_set_level(rdev);
4827}
4828
4829void ci_dpm_reset_asic(struct radeon_device *rdev)
4830{
4831 ci_set_boot_state(rdev);
4832}
4833
4834void ci_dpm_display_configuration_changed(struct radeon_device *rdev)
4835{
4836 ci_program_display_gap(rdev);
4837}
4838
4839union power_info {
4840 struct _ATOM_POWERPLAY_INFO info;
4841 struct _ATOM_POWERPLAY_INFO_V2 info_2;
4842 struct _ATOM_POWERPLAY_INFO_V3 info_3;
4843 struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
4844 struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
4845 struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
4846};
4847
4848union pplib_clock_info {
4849 struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
4850 struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
4851 struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
4852 struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
4853 struct _ATOM_PPLIB_SI_CLOCK_INFO si;
4854 struct _ATOM_PPLIB_CI_CLOCK_INFO ci;
4855};
4856
4857union pplib_power_state {
4858 struct _ATOM_PPLIB_STATE v1;
4859 struct _ATOM_PPLIB_STATE_V2 v2;
4860};
4861
4862static void ci_parse_pplib_non_clock_info(struct radeon_device *rdev,
4863 struct radeon_ps *rps,
4864 struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
4865 u8 table_rev)
4866{
4867 rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
4868 rps->class = le16_to_cpu(non_clock_info->usClassification);
4869 rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
4870
4871 if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
4872 rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
4873 rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
4874 } else {
4875 rps->vclk = 0;
4876 rps->dclk = 0;
4877 }
4878
4879 if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT)
4880 rdev->pm.dpm.boot_ps = rps;
4881 if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
4882 rdev->pm.dpm.uvd_ps = rps;
4883}
4884
4885static void ci_parse_pplib_clock_info(struct radeon_device *rdev,
4886 struct radeon_ps *rps, int index,
4887 union pplib_clock_info *clock_info)
4888{
4889 struct ci_power_info *pi = ci_get_pi(rdev);
4890 struct ci_ps *ps = ci_get_ps(rps);
4891 struct ci_pl *pl = &ps->performance_levels[index];
4892
4893 ps->performance_level_count = index + 1;
4894
4895 pl->sclk = le16_to_cpu(clock_info->ci.usEngineClockLow);
4896 pl->sclk |= clock_info->ci.ucEngineClockHigh << 16;
4897 pl->mclk = le16_to_cpu(clock_info->ci.usMemoryClockLow);
4898 pl->mclk |= clock_info->ci.ucMemoryClockHigh << 16;
4899
4900 pl->pcie_gen = r600_get_pcie_gen_support(rdev,
4901 pi->sys_pcie_mask,
4902 pi->vbios_boot_state.pcie_gen_bootup_value,
4903 clock_info->ci.ucPCIEGen);
4904 pl->pcie_lane = r600_get_pcie_lane_support(rdev,
4905 pi->vbios_boot_state.pcie_lane_bootup_value,
4906 le16_to_cpu(clock_info->ci.usPCIELane));
4907
4908 if (rps->class & ATOM_PPLIB_CLASSIFICATION_ACPI) {
4909 pi->acpi_pcie_gen = pl->pcie_gen;
4910 }
4911
4912 if (rps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV) {
4913 pi->ulv.supported = true;
4914 pi->ulv.pl = *pl;
4915 pi->ulv.cg_ulv_parameter = CISLANDS_CGULVPARAMETER_DFLT;
4916 }
4917
4918 /* patch up boot state */
4919 if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
4920 pl->mclk = pi->vbios_boot_state.mclk_bootup_value;
4921 pl->sclk = pi->vbios_boot_state.sclk_bootup_value;
4922 pl->pcie_gen = pi->vbios_boot_state.pcie_gen_bootup_value;
4923 pl->pcie_lane = pi->vbios_boot_state.pcie_lane_bootup_value;
4924 }
4925
4926 switch (rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) {
4927 case ATOM_PPLIB_CLASSIFICATION_UI_BATTERY:
4928 pi->use_pcie_powersaving_levels = true;
4929 if (pi->pcie_gen_powersaving.max < pl->pcie_gen)
4930 pi->pcie_gen_powersaving.max = pl->pcie_gen;
4931 if (pi->pcie_gen_powersaving.min > pl->pcie_gen)
4932 pi->pcie_gen_powersaving.min = pl->pcie_gen;
4933 if (pi->pcie_lane_powersaving.max < pl->pcie_lane)
4934 pi->pcie_lane_powersaving.max = pl->pcie_lane;
4935 if (pi->pcie_lane_powersaving.min > pl->pcie_lane)
4936 pi->pcie_lane_powersaving.min = pl->pcie_lane;
4937 break;
4938 case ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE:
4939 pi->use_pcie_performance_levels = true;
4940 if (pi->pcie_gen_performance.max < pl->pcie_gen)
4941 pi->pcie_gen_performance.max = pl->pcie_gen;
4942 if (pi->pcie_gen_performance.min > pl->pcie_gen)
4943 pi->pcie_gen_performance.min = pl->pcie_gen;
4944 if (pi->pcie_lane_performance.max < pl->pcie_lane)
4945 pi->pcie_lane_performance.max = pl->pcie_lane;
4946 if (pi->pcie_lane_performance.min > pl->pcie_lane)
4947 pi->pcie_lane_performance.min = pl->pcie_lane;
4948 break;
4949 default:
4950 break;
4951 }
4952}
4953
4954static int ci_parse_power_table(struct radeon_device *rdev)
4955{
4956 struct radeon_mode_info *mode_info = &rdev->mode_info;
4957 struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
4958 union pplib_power_state *power_state;
4959 int i, j, k, non_clock_array_index, clock_array_index;
4960 union pplib_clock_info *clock_info;
4961 struct _StateArray *state_array;
4962 struct _ClockInfoArray *clock_info_array;
4963 struct _NonClockInfoArray *non_clock_info_array;
4964 union power_info *power_info;
4965 int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
4966 u16 data_offset;
4967 u8 frev, crev;
4968 u8 *power_state_offset;
4969 struct ci_ps *ps;
4970
4971 if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
4972 &frev, &crev, &data_offset))
4973 return -EINVAL;
4974 power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
4975
4976 state_array = (struct _StateArray *)
4977 (mode_info->atom_context->bios + data_offset +
4978 le16_to_cpu(power_info->pplib.usStateArrayOffset));
4979 clock_info_array = (struct _ClockInfoArray *)
4980 (mode_info->atom_context->bios + data_offset +
4981 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
4982 non_clock_info_array = (struct _NonClockInfoArray *)
4983 (mode_info->atom_context->bios + data_offset +
4984 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
4985
4986 rdev->pm.dpm.ps = kzalloc(sizeof(struct radeon_ps) *
4987 state_array->ucNumEntries, GFP_KERNEL);
4988 if (!rdev->pm.dpm.ps)
4989 return -ENOMEM;
4990 power_state_offset = (u8 *)state_array->states;
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004991 for (i = 0; i < state_array->ucNumEntries; i++) {
Alex Deucherb309ed92013-08-20 19:08:22 -04004992 u8 *idx;
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04004993 power_state = (union pplib_power_state *)power_state_offset;
4994 non_clock_array_index = power_state->v2.nonClockInfoIndex;
4995 non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
4996 &non_clock_info_array->nonClockInfo[non_clock_array_index];
4997 if (!rdev->pm.power_state[i].clock_info)
4998 return -EINVAL;
4999 ps = kzalloc(sizeof(struct ci_ps), GFP_KERNEL);
5000 if (ps == NULL) {
5001 kfree(rdev->pm.dpm.ps);
5002 return -ENOMEM;
5003 }
5004 rdev->pm.dpm.ps[i].ps_priv = ps;
5005 ci_parse_pplib_non_clock_info(rdev, &rdev->pm.dpm.ps[i],
5006 non_clock_info,
5007 non_clock_info_array->ucEntrySize);
5008 k = 0;
Alex Deucherb309ed92013-08-20 19:08:22 -04005009 idx = (u8 *)&power_state->v2.clockInfoIndex[0];
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005010 for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
Alex Deucherb309ed92013-08-20 19:08:22 -04005011 clock_array_index = idx[j];
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005012 if (clock_array_index >= clock_info_array->ucNumEntries)
5013 continue;
5014 if (k >= CISLANDS_MAX_HARDWARE_POWERLEVELS)
5015 break;
5016 clock_info = (union pplib_clock_info *)
Alex Deucherb309ed92013-08-20 19:08:22 -04005017 ((u8 *)&clock_info_array->clockInfo[0] +
5018 (clock_array_index * clock_info_array->ucEntrySize));
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005019 ci_parse_pplib_clock_info(rdev,
5020 &rdev->pm.dpm.ps[i], k,
5021 clock_info);
5022 k++;
5023 }
5024 power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
5025 }
5026 rdev->pm.dpm.num_ps = state_array->ucNumEntries;
Alex Deucher8cd36682013-08-23 11:05:24 -04005027
5028 /* fill in the vce power states */
5029 for (i = 0; i < RADEON_MAX_VCE_LEVELS; i++) {
5030 u32 sclk, mclk;
5031 clock_array_index = rdev->pm.dpm.vce_states[i].clk_idx;
5032 clock_info = (union pplib_clock_info *)
5033 &clock_info_array->clockInfo[clock_array_index * clock_info_array->ucEntrySize];
5034 sclk = le16_to_cpu(clock_info->ci.usEngineClockLow);
5035 sclk |= clock_info->ci.ucEngineClockHigh << 16;
5036 mclk = le16_to_cpu(clock_info->ci.usMemoryClockLow);
5037 mclk |= clock_info->ci.ucMemoryClockHigh << 16;
5038 rdev->pm.dpm.vce_states[i].sclk = sclk;
5039 rdev->pm.dpm.vce_states[i].mclk = mclk;
5040 }
5041
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005042 return 0;
5043}
5044
Alex Deucher9a04dad2014-01-07 12:16:05 -05005045static int ci_get_vbios_boot_values(struct radeon_device *rdev,
5046 struct ci_vbios_boot_state *boot_state)
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005047{
5048 struct radeon_mode_info *mode_info = &rdev->mode_info;
5049 int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
5050 ATOM_FIRMWARE_INFO_V2_2 *firmware_info;
5051 u8 frev, crev;
5052 u16 data_offset;
5053
5054 if (atom_parse_data_header(mode_info->atom_context, index, NULL,
5055 &frev, &crev, &data_offset)) {
5056 firmware_info =
5057 (ATOM_FIRMWARE_INFO_V2_2 *)(mode_info->atom_context->bios +
5058 data_offset);
5059 boot_state->mvdd_bootup_value = le16_to_cpu(firmware_info->usBootUpMVDDCVoltage);
5060 boot_state->vddc_bootup_value = le16_to_cpu(firmware_info->usBootUpVDDCVoltage);
5061 boot_state->vddci_bootup_value = le16_to_cpu(firmware_info->usBootUpVDDCIVoltage);
5062 boot_state->pcie_gen_bootup_value = ci_get_current_pcie_speed(rdev);
5063 boot_state->pcie_lane_bootup_value = ci_get_current_pcie_lane_number(rdev);
5064 boot_state->sclk_bootup_value = le32_to_cpu(firmware_info->ulDefaultEngineClock);
5065 boot_state->mclk_bootup_value = le32_to_cpu(firmware_info->ulDefaultMemoryClock);
5066
5067 return 0;
5068 }
5069 return -EINVAL;
5070}
5071
5072void ci_dpm_fini(struct radeon_device *rdev)
5073{
5074 int i;
5075
5076 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
5077 kfree(rdev->pm.dpm.ps[i].ps_priv);
5078 }
5079 kfree(rdev->pm.dpm.ps);
5080 kfree(rdev->pm.dpm.priv);
5081 kfree(rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries);
5082 r600_free_extended_power_table(rdev);
5083}
5084
5085int ci_dpm_init(struct radeon_device *rdev)
5086{
5087 int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
Alex Deucher34fc0b52014-11-07 11:52:12 -05005088 SMU7_Discrete_DpmTable *dpm_table;
5089 struct radeon_gpio_rec gpio;
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005090 u16 data_offset, size;
5091 u8 frev, crev;
5092 struct ci_power_info *pi;
5093 int ret;
5094 u32 mask;
5095
5096 pi = kzalloc(sizeof(struct ci_power_info), GFP_KERNEL);
5097 if (pi == NULL)
5098 return -ENOMEM;
5099 rdev->pm.dpm.priv = pi;
5100
5101 ret = drm_pcie_get_speed_cap_mask(rdev->ddev, &mask);
5102 if (ret)
5103 pi->sys_pcie_mask = 0;
5104 else
5105 pi->sys_pcie_mask = mask;
5106 pi->force_pcie_gen = RADEON_PCIE_GEN_INVALID;
5107
5108 pi->pcie_gen_performance.max = RADEON_PCIE_GEN1;
5109 pi->pcie_gen_performance.min = RADEON_PCIE_GEN3;
5110 pi->pcie_gen_powersaving.max = RADEON_PCIE_GEN1;
5111 pi->pcie_gen_powersaving.min = RADEON_PCIE_GEN3;
5112
5113 pi->pcie_lane_performance.max = 0;
5114 pi->pcie_lane_performance.min = 16;
5115 pi->pcie_lane_powersaving.max = 0;
5116 pi->pcie_lane_powersaving.min = 16;
5117
5118 ret = ci_get_vbios_boot_values(rdev, &pi->vbios_boot_state);
5119 if (ret) {
5120 ci_dpm_fini(rdev);
5121 return ret;
5122 }
Alex Deucher82f79cc2013-08-21 10:02:32 -04005123
5124 ret = r600_get_platform_caps(rdev);
5125 if (ret) {
5126 ci_dpm_fini(rdev);
5127 return ret;
5128 }
Alex Deucher8cd36682013-08-23 11:05:24 -04005129
5130 ret = r600_parse_extended_power_table(rdev);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005131 if (ret) {
5132 ci_dpm_fini(rdev);
5133 return ret;
5134 }
Alex Deucher8cd36682013-08-23 11:05:24 -04005135
5136 ret = ci_parse_power_table(rdev);
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005137 if (ret) {
5138 ci_dpm_fini(rdev);
5139 return ret;
5140 }
5141
5142 pi->dll_default_on = false;
5143 pi->sram_end = SMC_RAM_END;
5144
5145 pi->activity_target[0] = CISLAND_TARGETACTIVITY_DFLT;
5146 pi->activity_target[1] = CISLAND_TARGETACTIVITY_DFLT;
5147 pi->activity_target[2] = CISLAND_TARGETACTIVITY_DFLT;
5148 pi->activity_target[3] = CISLAND_TARGETACTIVITY_DFLT;
5149 pi->activity_target[4] = CISLAND_TARGETACTIVITY_DFLT;
5150 pi->activity_target[5] = CISLAND_TARGETACTIVITY_DFLT;
5151 pi->activity_target[6] = CISLAND_TARGETACTIVITY_DFLT;
5152 pi->activity_target[7] = CISLAND_TARGETACTIVITY_DFLT;
5153
5154 pi->mclk_activity_target = CISLAND_MCLK_TARGETACTIVITY_DFLT;
5155
5156 pi->sclk_dpm_key_disabled = 0;
5157 pi->mclk_dpm_key_disabled = 0;
5158 pi->pcie_dpm_key_disabled = 0;
5159
Alex Deucher7e1858f2014-04-11 11:21:51 -04005160 /* mclk dpm is unstable on some R7 260X cards with the old mc ucode */
5161 if ((rdev->pdev->device == 0x6658) &&
5162 (rdev->mc_fw->size == (BONAIRE_MC_UCODE_SIZE * 4))) {
Alex Deucher57700ad2014-04-10 22:29:03 -04005163 pi->mclk_dpm_key_disabled = 1;
Alex Deucher7e1858f2014-04-11 11:21:51 -04005164 }
Alex Deucher57700ad2014-04-10 22:29:03 -04005165
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005166 pi->caps_sclk_ds = true;
5167
5168 pi->mclk_strobe_mode_threshold = 40000;
5169 pi->mclk_stutter_mode_threshold = 40000;
5170 pi->mclk_edc_enable_threshold = 40000;
5171 pi->mclk_edc_wr_enable_threshold = 40000;
5172
5173 ci_initialize_powertune_defaults(rdev);
5174
5175 pi->caps_fps = false;
5176
5177 pi->caps_sclk_throttle_low_notification = false;
5178
Alex Deucher9597fe12013-08-23 11:06:12 -04005179 pi->caps_uvd_dpm = true;
Alex Deucheree35b002013-08-23 11:09:21 -04005180 pi->caps_vce_dpm = true;
Alex Deucher9597fe12013-08-23 11:06:12 -04005181
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005182 ci_get_leakage_voltages(rdev);
5183 ci_patch_dependency_tables_with_leakage(rdev);
5184 ci_set_private_data_variables_based_on_pptable(rdev);
5185
5186 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries =
5187 kzalloc(4 * sizeof(struct radeon_clock_voltage_dependency_entry), GFP_KERNEL);
5188 if (!rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries) {
5189 ci_dpm_fini(rdev);
5190 return -ENOMEM;
5191 }
5192 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.count = 4;
5193 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[0].clk = 0;
5194 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[0].v = 0;
5195 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[1].clk = 36000;
5196 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[1].v = 720;
5197 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[2].clk = 54000;
5198 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[2].v = 810;
5199 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[3].clk = 72000;
5200 rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[3].v = 900;
5201
5202 rdev->pm.dpm.dyn_state.mclk_sclk_ratio = 4;
5203 rdev->pm.dpm.dyn_state.sclk_mclk_delta = 15000;
5204 rdev->pm.dpm.dyn_state.vddc_vddci_delta = 200;
5205
5206 rdev->pm.dpm.dyn_state.valid_sclk_values.count = 0;
5207 rdev->pm.dpm.dyn_state.valid_sclk_values.values = NULL;
5208 rdev->pm.dpm.dyn_state.valid_mclk_values.count = 0;
5209 rdev->pm.dpm.dyn_state.valid_mclk_values.values = NULL;
5210
Alex Deucher2d400382013-08-09 18:27:47 -04005211 if (rdev->family == CHIP_HAWAII) {
5212 pi->thermal_temp_setting.temperature_low = 94500;
5213 pi->thermal_temp_setting.temperature_high = 95000;
5214 pi->thermal_temp_setting.temperature_shutdown = 104000;
5215 } else {
5216 pi->thermal_temp_setting.temperature_low = 99500;
5217 pi->thermal_temp_setting.temperature_high = 100000;
5218 pi->thermal_temp_setting.temperature_shutdown = 104000;
5219 }
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005220
5221 pi->uvd_enabled = false;
5222
Alex Deucher34fc0b52014-11-07 11:52:12 -05005223 dpm_table = &pi->smc_state_table;
5224
5225 gpio = radeon_atombios_lookup_gpio(rdev, VDDC_VRHOT_GPIO_PINID);
5226 if (gpio.valid) {
5227 dpm_table->VRHotGpio = gpio.shift;
5228 rdev->pm.dpm.platform_caps |= ATOM_PP_PLATFORM_CAP_REGULATOR_HOT;
5229 } else {
5230 dpm_table->VRHotGpio = CISLANDS_UNUSED_GPIO_PIN;
5231 rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_REGULATOR_HOT;
5232 }
5233
5234 gpio = radeon_atombios_lookup_gpio(rdev, PP_AC_DC_SWITCH_GPIO_PINID);
5235 if (gpio.valid) {
5236 dpm_table->AcDcGpio = gpio.shift;
5237 rdev->pm.dpm.platform_caps |= ATOM_PP_PLATFORM_CAP_HARDWAREDC;
5238 } else {
5239 dpm_table->AcDcGpio = CISLANDS_UNUSED_GPIO_PIN;
5240 rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_HARDWAREDC;
5241 }
5242
5243 gpio = radeon_atombios_lookup_gpio(rdev, VDDC_PCC_GPIO_PINID);
5244 if (gpio.valid) {
5245 u32 tmp = RREG32_SMC(CNB_PWRMGT_CNTL);
5246
5247 switch (gpio.shift) {
5248 case 0:
5249 tmp &= ~GNB_SLOW_MODE_MASK;
5250 tmp |= GNB_SLOW_MODE(1);
5251 break;
5252 case 1:
5253 tmp &= ~GNB_SLOW_MODE_MASK;
5254 tmp |= GNB_SLOW_MODE(2);
5255 break;
5256 case 2:
5257 tmp |= GNB_SLOW;
5258 break;
5259 case 3:
5260 tmp |= FORCE_NB_PS1;
5261 break;
5262 case 4:
5263 tmp |= DPM_ENABLED;
5264 break;
5265 default:
5266 DRM_ERROR("Invalid PCC GPIO!");
5267 break;
5268 }
5269 WREG32_SMC(CNB_PWRMGT_CNTL, tmp);
5270 }
5271
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005272 pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_NONE;
5273 pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_NONE;
5274 pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_NONE;
5275 if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_GPIO_LUT))
5276 pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
5277 else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_SVID2))
5278 pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
5279
5280 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_VDDCI_CONTROL) {
5281 if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT))
5282 pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
5283 else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_SVID2))
5284 pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
5285 else
5286 rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_VDDCI_CONTROL;
5287 }
5288
5289 if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_MVDDCONTROL) {
5290 if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT))
5291 pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
5292 else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_SVID2))
5293 pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
5294 else
5295 rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_MVDDCONTROL;
5296 }
5297
5298 pi->vddc_phase_shed_control = true;
5299
5300#if defined(CONFIG_ACPI)
5301 pi->pcie_performance_request =
5302 radeon_acpi_is_pcie_performance_request_supported(rdev);
5303#else
5304 pi->pcie_performance_request = false;
5305#endif
5306
5307 if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
5308 &frev, &crev, &data_offset)) {
5309 pi->caps_sclk_ss_support = true;
5310 pi->caps_mclk_ss_support = true;
5311 pi->dynamic_ss = true;
5312 } else {
5313 pi->caps_sclk_ss_support = false;
5314 pi->caps_mclk_ss_support = false;
5315 pi->dynamic_ss = true;
5316 }
5317
5318 if (rdev->pm.int_thermal_type != THERMAL_TYPE_NONE)
5319 pi->thermal_protection = true;
5320 else
5321 pi->thermal_protection = false;
5322
5323 pi->caps_dynamic_ac_timing = true;
5324
Alex Deucher47acb1f2013-08-26 09:43:24 -04005325 pi->uvd_power_gated = false;
5326
Alex Deucher679fe802013-08-30 16:24:33 -04005327 /* make sure dc limits are valid */
5328 if ((rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.sclk == 0) ||
5329 (rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.mclk == 0))
5330 rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc =
5331 rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
5332
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005333 return 0;
5334}
5335
Alex Deucher94b4adc2013-07-15 17:34:33 -04005336void ci_dpm_debugfs_print_current_performance_level(struct radeon_device *rdev,
5337 struct seq_file *m)
5338{
Alex Deucher3e15c352014-10-02 10:28:57 -04005339 struct ci_power_info *pi = ci_get_pi(rdev);
5340 struct radeon_ps *rps = &pi->current_rps;
Alex Deucher94b4adc2013-07-15 17:34:33 -04005341 u32 sclk = ci_get_average_sclk_freq(rdev);
5342 u32 mclk = ci_get_average_mclk_freq(rdev);
5343
Alex Deucher3e15c352014-10-02 10:28:57 -04005344 seq_printf(m, "uvd %sabled\n", pi->uvd_enabled ? "en" : "dis");
5345 seq_printf(m, "vce %sabled\n", rps->vce_active ? "en" : "dis");
Alex Deucher94b4adc2013-07-15 17:34:33 -04005346 seq_printf(m, "power level avg sclk: %u mclk: %u\n",
5347 sclk, mclk);
5348}
5349
Alex Deuchercc8dbbb2013-08-14 01:03:41 -04005350void ci_dpm_print_power_state(struct radeon_device *rdev,
5351 struct radeon_ps *rps)
5352{
5353 struct ci_ps *ps = ci_get_ps(rps);
5354 struct ci_pl *pl;
5355 int i;
5356
5357 r600_dpm_print_class_info(rps->class, rps->class2);
5358 r600_dpm_print_cap_info(rps->caps);
5359 printk("\tuvd vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
5360 for (i = 0; i < ps->performance_level_count; i++) {
5361 pl = &ps->performance_levels[i];
5362 printk("\t\tpower level %d sclk: %u mclk: %u pcie gen: %u pcie lanes: %u\n",
5363 i, pl->sclk, pl->mclk, pl->pcie_gen + 1, pl->pcie_lane);
5364 }
5365 r600_dpm_print_ps_status(rdev, rps);
5366}
5367
5368u32 ci_dpm_get_sclk(struct radeon_device *rdev, bool low)
5369{
5370 struct ci_power_info *pi = ci_get_pi(rdev);
5371 struct ci_ps *requested_state = ci_get_ps(&pi->requested_rps);
5372
5373 if (low)
5374 return requested_state->performance_levels[0].sclk;
5375 else
5376 return requested_state->performance_levels[requested_state->performance_level_count - 1].sclk;
5377}
5378
5379u32 ci_dpm_get_mclk(struct radeon_device *rdev, bool low)
5380{
5381 struct ci_power_info *pi = ci_get_pi(rdev);
5382 struct ci_ps *requested_state = ci_get_ps(&pi->requested_rps);
5383
5384 if (low)
5385 return requested_state->performance_levels[0].mclk;
5386 else
5387 return requested_state->performance_levels[requested_state->performance_level_count - 1].mclk;
5388}