Debian Codegraph @

src/soc/intel/common/block/power_limit/power_limit.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <console/console.h>
4 #include <cpu/x86/msr.h>
5 #include <device/pci_ops.h>
6 #include <drivers/intel/dptf/chip.h>
7 #include <intelblocks/cpulib.h>
8 #include <intelblocks/power_limit.h>
9 #include <soc/msr.h>
10 #include <soc/pci_devs.h>
11 #include <soc/soc_chip.h>
12 #include <soc/systemagent.h>
13 #include <static.h>
14
15 /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
16 static const u8 power_limit_time_sec_to_msr[] = {
17 [0] = 0x00,
18 [1] = 0x0a,
19 [2] = 0x0b,
20 [3] = 0x4b,
21 [4] = 0x0c,
22 [5] = 0x2c,
23 [6] = 0x4c,
24 [7] = 0x6c,
25 [8] = 0x0d,
26 [10] = 0x2d,
27 [12] = 0x4d,
28 [14] = 0x6d,
29 [16] = 0x0e,
30 [20] = 0x2e,
31 [24] = 0x4e,
32 [28] = 0x6e,
33 [32] = 0x0f,
34 [40] = 0x2f,
35 [48] = 0x4f,
36 [56] = 0x6f,
37 [64] = 0x10,
38 [80] = 0x30,
39 [96] = 0x50,
40 [112] = 0x70,
41 [128] = 0x11,
42 };
43
44 /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
45 static const u8 power_limit_time_msr_to_sec[] = {
46 [0x00] = 0,
47 [0x0a] = 1,
48 [0x0b] = 2,
49 [0x4b] = 3,
50 [0x0c] = 4,
51 [0x2c] = 5,
52 [0x4c] = 6,
53 [0x6c] = 7,
54 [0x0d] = 8,
55 [0x2d] = 10,
56 [0x4d] = 12,
57 [0x6d] = 14,
58 [0x0e] = 16,
59 [0x2e] = 20,
60 [0x4e] = 24,
61 [0x6e] = 28,
62 [0x0f] = 32,
63 [0x2f] = 40,
64 [0x4f] = 48,
65 [0x6f] = 56,
66 [0x10] = 64,
67 [0x30] = 80,
68 [0x50] = 96,
69 [0x70] = 112,
70 [0x11] = 128,
71 };
72
73 /*
74 * Configure processor power limits if possible
75 * This must be done AFTER set of BIOS_RESET_CPL
76 */
77 void set_power_limits(u8 power_limit_1_time,
78 struct soc_power_limits_config *conf)
79 {
80 msr_t msr;
81 msr_t limit;
82 unsigned int power_unit;
83 unsigned int tdp, min_power, max_power, max_time, tdp_pl2, tdp_pl1;
84 u8 power_limit_1_val;
85 uint32_t value;
86
87 if (CONFIG(SOC_INTEL_DISABLE_POWER_LIMITS)) {
88 printk(BIOS_INFO, "Disabling RAPL\n");
89 if (CONFIG(SOC_INTEL_RAPL_DISABLE_VIA_MCHBAR)) {
90 value = MCHBAR32(MCH_PKG_POWER_LIMIT_LO);
91 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = value & ~(PKG_POWER_LIMIT_EN);
92 value = MCHBAR32(MCH_PKG_POWER_LIMIT_HI);
93 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = value & ~(PKG_POWER_LIMIT_EN);
94 /* Elkhartlake SoC does not shadow PKG_POWER_LIMIT MCHBAR settings
95 to MSR correctly. */
96 if (CONFIG(SOC_INTEL_ELKHARTLAKE)) {
97 msr = rdmsr(MSR_PKG_POWER_LIMIT);
98 msr.hi = 0;
99 msr.lo = 0;
100 wrmsr(MSR_PKG_POWER_LIMIT, msr);
101 }
102 } else {
103 msr = rdmsr(MSR_PKG_POWER_LIMIT);
104 msr.lo &= ~PKG_POWER_LIMIT_EN;
105 msr.hi &= ~PKG_POWER_LIMIT_EN;
106 wrmsr(MSR_PKG_POWER_LIMIT, msr);
107 }
108 return;
109 }
110
111 if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
112 power_limit_1_time = ARRAY_SIZE(power_limit_time_sec_to_msr) - 1;
113
114 msr = rdmsr(MSR_PLATFORM_INFO);
115 if (!(msr.lo & PLATFORM_INFO_SET_TDP))
116 return;
117
118 /* Get units */
119 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
120 power_unit = 1 << (msr.lo & 0xf);
121
122 /* Get power defaults for this SKU */
123 msr = rdmsr(MSR_PKG_POWER_SKU);
124 tdp = msr.lo & 0x7fff;
125 min_power = (msr.lo >> 16) & 0x7fff;
126 max_power = msr.hi & 0x7fff;
127 max_time = (msr.hi >> 16) & 0x7f;
128
129 printk(BIOS_INFO, "CPU TDP = %u Watts\n", tdp / power_unit);
130
131 if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
132 power_limit_1_time = power_limit_time_msr_to_sec[max_time];
133
134 if (min_power > 0 && tdp < min_power)
135 tdp = min_power;
136
137 if (max_power > 0 && tdp > max_power)
138 tdp = max_power;
139
140 power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
141
142 /* Set long term power limit to TDP */
143 limit.lo = 0;
144 tdp_pl1 = ((conf->tdp_pl1_override == 0) ?
145 tdp : (conf->tdp_pl1_override * power_unit));
146 printk(BIOS_INFO, "CPU PL1 = %u Watts\n", tdp_pl1 / power_unit);
147 limit.lo |= (tdp_pl1 & PKG_POWER_LIMIT_MASK);
148
149 /* Set PL1 Pkg Power clamp bit */
150 limit.lo |= PKG_POWER_LIMIT_CLAMP;
151
152 limit.lo |= PKG_POWER_LIMIT_EN;
153 limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
154 PKG_POWER_LIMIT_TIME_SHIFT;
155
156 /* Set short term power limit to 1.25 * TDP if no config given */
157 limit.hi = 0;
158 tdp_pl2 = (conf->tdp_pl2_override == 0) ?
159 (tdp * 125) / 100 : (conf->tdp_pl2_override * power_unit);
160 printk(BIOS_INFO, "CPU PL2 = %u Watts\n", tdp_pl2 / power_unit);
161 limit.hi |= (tdp_pl2) & PKG_POWER_LIMIT_MASK;
162 limit.hi |= PKG_POWER_LIMIT_CLAMP;
163 limit.hi |= PKG_POWER_LIMIT_EN;
164
165 /* Power limit 2 time is only programmable on server SKU */
166 wrmsr(MSR_PKG_POWER_LIMIT, limit);
167
168 /* Set PL2 power limit values in MCHBAR and disable PL1 */
169 MCHBAR32(MCH_PKG_POWER_LIMIT_LO) = limit.lo & (~(PKG_POWER_LIMIT_EN));
170 MCHBAR32(MCH_PKG_POWER_LIMIT_HI) = limit.hi;
171
172 /* Set PsysPl2 */
173 if (conf->tdp_psyspl2) {
174 limit = rdmsr(MSR_PLATFORM_POWER_LIMIT);
175 limit.hi = 0;
176 printk(BIOS_INFO, "CPU PsysPL2 = %u Watts\n",
177 conf->tdp_psyspl2);
178 limit.hi |= (conf->tdp_psyspl2 * power_unit) &
179 PKG_POWER_LIMIT_MASK;
180 limit.hi |= PKG_POWER_LIMIT_CLAMP;
181 limit.hi |= PKG_POWER_LIMIT_EN;
182 wrmsr(MSR_PLATFORM_POWER_LIMIT, limit);
183 }
184
185 /* Set PsysPl3 */
186 if (conf->tdp_psyspl3) {
187 limit = rdmsr(MSR_PL3_CONTROL);
188 limit.lo = 0;
189 printk(BIOS_INFO, "CPU PsysPL3 = %u Watts\n",
190 conf->tdp_psyspl3);
191 limit.lo |= (conf->tdp_psyspl3 * power_unit) &
192 PKG_POWER_LIMIT_MASK;
193 /* Enable PsysPl3 */
194 limit.lo |= PKG_POWER_LIMIT_EN;
195 /* set PsysPl3 time window */
196 limit.lo |= (conf->tdp_psyspl3_time &
197 PKG_POWER_LIMIT_TIME_MASK) <<
198 PKG_POWER_LIMIT_TIME_SHIFT;
199 /* set PsysPl3 duty cycle */
200 limit.lo |= (conf->tdp_psyspl3_dutycycle &
201 PKG_POWER_LIMIT_DUTYCYCLE_MASK) <<
202 PKG_POWER_LIMIT_DUTYCYCLE_SHIFT;
203 wrmsr(MSR_PL3_CONTROL, limit);
204 }
205
206 /* Set Pl4 */
207 if (conf->tdp_pl4) {
208 limit = rdmsr(MSR_VR_CURRENT_CONFIG);
209 limit.lo = 0;
210 printk(BIOS_INFO, "CPU PL4 = %u Watts\n", conf->tdp_pl4);
211 limit.lo |= (conf->tdp_pl4 * power_unit) &
212 PKG_POWER_LIMIT_MASK;
213 wrmsr(MSR_VR_CURRENT_CONFIG, limit);
214 }
215
216 /* Set DDR RAPL power limit by copying from MMIO to MSR */
217 msr.lo = MCHBAR32(MCH_DDR_POWER_LIMIT_LO);
218 msr.hi = MCHBAR32(MCH_DDR_POWER_LIMIT_HI);
219 wrmsr(MSR_DDR_RAPL_LIMIT, msr);
220
221 /* Use nominal TDP values for CPUs with configurable TDP */
222 if (cpu_config_tdp_levels()) {
223 limit.hi = 0;
224 limit.lo = cpu_get_tdp_nominal_ratio();
225 wrmsr(MSR_TURBO_ACTIVATION_RATIO, limit);
226 }
227 }
228
229 u8 get_cpu_tdp(void)
230 {
231 unsigned int power_unit, cpu_tdp;
232
233 /* Get units */
234 msr_t msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
235 power_unit = 1 << (msr.lo & 0xf);
236
237 /* Get power defaults for this SKU */
238 msr = rdmsr(MSR_PKG_POWER_SKU);
239 cpu_tdp = msr.lo & 0x7fff;
240
241 return cpu_tdp / power_unit;
242 }
243
244 WEAK_DEV_PTR(dptf_policy);
245
246 #if ENV_RAMSTAGE && CONFIG(SOC_INTEL_COMMON_BLOCK_VARIANT_POWER_LIMIT)
247 void variant_update_cpu_power_limits(const struct cpu_tdp_power_limits *limits,
248 size_t num_entries)
249 {
250 if (!num_entries) {
251 printk(BIOS_INFO, "CPU Power limits entry not available\n");
252 return;
253 }
254
255 const struct device *policy_dev = DEV_PTR(dptf_policy);
256 if (!policy_dev)
257 return;
258
259 struct drivers_intel_dptf_config *config = policy_dev->chip_info;
260 if (!config) {
261 printk(BIOS_INFO, "DPTF is not enabled\n");
262 return;
263 }
264
265 uint16_t mch_id = pci_s_read_config16(PCI_DEV(0, 0, 0), PCI_DEVICE_ID);
266 if (mch_id == 0xffff) {
267 printk(BIOS_INFO, "No matching PCI DID present\n");
268 return;
269 }
270
271 uint8_t tdp = get_cpu_tdp();
272
273 for (size_t index = 0; index < num_entries; index++) {
274 if (mch_id != limits[index].mch_id || tdp != limits[index].cpu_tdp) {
275 continue;
276 } else {
277 struct dptf_power_limits *settings = &config->controls.power_limits;
278 config_t *conf = config_of_soc();
279 struct soc_power_limits_config *soc_config =
280 &conf->power_limits_config[limits[index].power_limits_index];
281 settings->pl1.min_power = limits[index].pl1_min_power;
282 settings->pl1.max_power = limits[index].pl1_max_power;
283 settings->pl2.min_power = limits[index].pl2_min_power;
284 settings->pl2.max_power = limits[index].pl2_max_power;
285 soc_config->tdp_pl4 = DIV_ROUND_UP(limits[index].pl4_power, MILLIWATTS_TO_WATTS);
286 soc_config->tdp_pl4_fastvmode = soc_config->tdp_pl4;
287 printk(BIOS_INFO, "Overriding power limits PL1 (mW) (%u, %u) PL2 (mW) (%u, %u) PL4 (W) (%u)\n",
288 settings->pl1.min_power,
289 settings->pl1.max_power,
290 settings->pl2.min_power,
291 settings->pl2.max_power,
292 soc_config->tdp_pl4);
293 }
294 }
295 }
296 #endif