blob: 30201f7b2c33bbab3fd2cc5ed26134c86b13d00c [file] [log] [blame]
Felix Held86024952021-02-03 23:44:28 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2
3/* ACPI - create the Fixed ACPI Description Tables (FADT) */
4
5#include <acpi/acpi.h>
Jason Glenesk79542fa2021-03-10 03:50:57 -08006#include <acpi/acpigen.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -07007#include <amdblocks/acpi.h>
Jason Glenesk79542fa2021-03-10 03:50:57 -08008#include <amdblocks/cpu.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -07009#include <amdblocks/acpimmio.h>
Raul E Rangel65819cd2021-02-16 10:37:46 -070010#include <amdblocks/ioapic.h>
11#include <arch/ioapic.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -070012#include <arch/smp/mpspec.h>
13#include <console/console.h>
Jason Glenesk79542fa2021-03-10 03:50:57 -080014#include <cpu/amd/cpuid.h>
15#include <cpu/amd/msr.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -070016#include <cpu/x86/smm.h>
17#include <soc/acpi.h>
18#include <soc/iomap.h>
Jason Glenesk79542fa2021-03-10 03:50:57 -080019#include <soc/msr.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -070020#include <types.h>
21#include "chip.h"
Felix Held86024952021-02-03 23:44:28 +010022
23unsigned long acpi_fill_madt(unsigned long current)
24{
Raul E Rangel12c6a582021-02-10 16:45:49 -070025 /* create all subtables for processors */
26 current = acpi_create_madt_lapics(current);
27
Raul E Rangel65819cd2021-02-16 10:37:46 -070028 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
29 FCH_IOAPIC_ID, IO_APIC_ADDR, 0);
30
31 /* TODO: Add GNB-IOAPIC */
Raul E Rangel12c6a582021-02-10 16:45:49 -070032
33 current += acpi_create_madt_irqoverride(
34 (acpi_madt_irqoverride_t *)current,
35 MP_BUS_ISA, 0, 2,
36 MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT);
37 current += acpi_create_madt_irqoverride(
38 (acpi_madt_irqoverride_t *)current,
39 MP_BUS_ISA, ACPI_SCI_IRQ, ACPI_SCI_IRQ,
40 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
Raul E Rangel9bce1fe2021-02-11 11:28:52 -070041 current = acpi_fill_madt_irqoverride(current);
42
43 /* create all subtables for processors */
44 current += acpi_create_madt_lapic_nmi(
45 (acpi_madt_lapic_nmi_t *)current,
46 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS,
47 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
48 1 /* 1: LINT1 connect to NMI */);
Raul E Rangel12c6a582021-02-10 16:45:49 -070049
Felix Held86024952021-02-03 23:44:28 +010050 return current;
51}
52
53/*
54 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
55 * in the ACPI 3.0b specification.
56 */
57void acpi_fill_fadt(acpi_fadt_t *fadt)
58{
Raul E Rangel12c6a582021-02-10 16:45:49 -070059 const struct soc_amd_common_config *cfg = soc_get_common_config();
60
61 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
62
63 fadt->sci_int = ACPI_SCI_IRQ;
64
65 if (permanent_smi_handler()) {
66 fadt->smi_cmd = APM_CNT;
67 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
68 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
69 }
70
71 fadt->pstate_cnt = 0;
72
73 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
74 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
75 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
76 fadt->gpe0_blk = ACPI_GPE0_BLK;
77
78 fadt->pm1_evt_len = 4; /* 32 bits */
79 fadt->pm1_cnt_len = 2; /* 16 bits */
80 fadt->pm_tmr_len = 4; /* 32 bits */
81 fadt->gpe0_blk_len = 8; /* 64 bits */
82
83 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
84 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
85 fadt->duty_offset = 0; /* Not supported */
86 fadt->duty_width = 0; /* Not supported */
87 fadt->day_alrm = RTC_DATE_ALARM;
88 fadt->mon_alrm = 0;
89 fadt->century = RTC_ALT_CENTURY;
90 fadt->iapc_boot_arch = cfg->fadt_boot_arch; /* legacy free default */
91 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
92 ACPI_FADT_C1_SUPPORTED |
93 ACPI_FADT_S4_RTC_WAKE |
94 ACPI_FADT_32BIT_TIMER |
95 ACPI_FADT_PCI_EXPRESS_WAKE |
96 ACPI_FADT_PLATFORM_CLOCK |
97 ACPI_FADT_S4_RTC_VALID |
98 ACPI_FADT_REMOTE_POWER_ON;
99 fadt->flags |= cfg->fadt_flags; /* additional board-specific flags */
100
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700101 fadt->x_pm1a_evt_blk.space_id = ACPI_ADDRESS_SPACE_IO;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700102 fadt->x_pm1a_evt_blk.bit_width = 32;
103 fadt->x_pm1a_evt_blk.bit_offset = 0;
104 fadt->x_pm1a_evt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700105 fadt->x_pm1a_evt_blk.addrl = ACPI_PM_EVT_BLK;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700106 fadt->x_pm1a_evt_blk.addrh = 0x0;
107
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700108 fadt->x_pm1a_cnt_blk.space_id = ACPI_ADDRESS_SPACE_IO;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700109 fadt->x_pm1a_cnt_blk.bit_width = 16;
110 fadt->x_pm1a_cnt_blk.bit_offset = 0;
111 fadt->x_pm1a_cnt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700112 fadt->x_pm1a_cnt_blk.addrl = ACPI_PM1_CNT_BLK;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700113 fadt->x_pm1a_cnt_blk.addrh = 0x0;
114
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700115 fadt->x_pm_tmr_blk.space_id = ACPI_ADDRESS_SPACE_IO;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700116 fadt->x_pm_tmr_blk.bit_width = 32;
117 fadt->x_pm_tmr_blk.bit_offset = 0;
118 fadt->x_pm_tmr_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700119 fadt->x_pm_tmr_blk.addrl = ACPI_PM_TMR_BLK;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700120 fadt->x_pm_tmr_blk.addrh = 0x0;
121
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700122 fadt->x_gpe0_blk.space_id = ACPI_ADDRESS_SPACE_IO;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700123 fadt->x_gpe0_blk.bit_width = 64;
124 fadt->x_gpe0_blk.bit_offset = 0;
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700125 fadt->x_gpe0_blk.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
126 fadt->x_gpe0_blk.addrl = ACPI_GPE0_BLK;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700127 fadt->x_gpe0_blk.addrh = 0x0;
Felix Held86024952021-02-03 23:44:28 +0100128}
Jason Glenesk79542fa2021-03-10 03:50:57 -0800129
130static uint32_t get_pstate_core_freq(msr_t pstate_def)
131{
132 uint32_t core_freq, core_freq_mul, core_freq_div;
133 bool valid_freq_divisor;
134
135 /* Core frequency multiplier */
136 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
137
138 /* Core frequency divisor ID */
139 core_freq_div =
140 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
141
142 if (core_freq_div == 0) {
143 return 0;
144 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
145 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
146 /* Allow 1/8 integer steps for this range */
147 valid_freq_divisor = 1;
148 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
149 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
150 /* Only allow 1/4 integer steps for this range */
151 valid_freq_divisor = 1;
152 } else {
153 valid_freq_divisor = 0;
154 }
155
156 if (valid_freq_divisor) {
157 /* 25 * core_freq_mul / (core_freq_div / 8) */
158 core_freq =
159 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
160 } else {
161 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
162 core_freq_div);
163 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
164 }
165 return core_freq;
166}
167
168static uint32_t get_pstate_core_power(msr_t pstate_def)
169{
170 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
171
172 /* Core voltage ID */
173 core_vid =
174 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
175
176 /* Current value in amps */
177 current_value_amps =
178 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
179
180 /* Current divisor */
181 current_divisor =
182 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
183
184 /* Voltage */
185 if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
186 /* Voltage off for VID codes 0xF8 to 0xFF */
187 voltage_in_uvolts = 0;
188 } else {
189 voltage_in_uvolts =
190 SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
191 }
192
193 /* Power in mW */
194 power_in_mw = (voltage_in_uvolts) / 1000 * current_value_amps;
195
196 switch (current_divisor) {
197 case 0:
198 break;
199 case 1:
200 power_in_mw = power_in_mw / 10L;
201 break;
202 case 2:
203 power_in_mw = power_in_mw / 100L;
204 break;
205 case 3:
206 /* current_divisor is set to an undefined value.*/
207 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
208 power_in_mw = 0;
209 break;
210 }
211
212 return power_in_mw;
213}
214
215/*
216 * Populate structure describing enabled p-states and return count of enabled p-states.
217 */
218static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
219 struct acpi_xpss_sw_pstate *pstate_xpss_values)
220{
221 msr_t pstate_def;
222 size_t pstate_count, pstate;
223 uint32_t pstate_enable, max_pstate;
224
225 pstate_count = 0;
226 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
227
228 for (pstate = 0; pstate <= max_pstate; pstate++) {
229 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
230
231 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
232 >> PSTATE_DEF_HI_ENABLE_SHIFT;
233 if (!pstate_enable)
234 continue;
235
236 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
237 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
238 pstate_values[pstate_count].transition_latency = 0;
239 pstate_values[pstate_count].bus_master_latency = 0;
240 pstate_values[pstate_count].control_value = pstate;
241 pstate_values[pstate_count].status_value = pstate;
242
243 pstate_xpss_values[pstate_count].core_freq =
244 (uint64_t)pstate_values[pstate_count].core_freq;
245 pstate_xpss_values[pstate_count].power =
246 (uint64_t)pstate_values[pstate_count].power;
247 pstate_xpss_values[pstate_count].transition_latency = 0;
248 pstate_xpss_values[pstate_count].bus_master_latency = 0;
249 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
250 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
251 pstate_count++;
252 }
253
254 return pstate_count;
255}
256
257void generate_cpu_entries(const struct device *device)
258{
259 int logical_cores;
260 size_t pstate_count, cpu, proc_blk_len;
261 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
262 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
263 uint32_t threads_per_core, proc_blk_addr;
264 uint32_t cstate_base_address =
265 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
266
267 const acpi_addr_t perf_ctrl = {
268 .space_id = ACPI_ADDRESS_SPACE_FIXED,
269 .bit_width = 64,
270 .addrl = PS_CTL_REG,
271 };
272 const acpi_addr_t perf_sts = {
273 .space_id = ACPI_ADDRESS_SPACE_FIXED,
274 .bit_width = 64,
275 .addrl = PS_STS_REG,
276 };
277
278 acpi_cstate_t cstate_info[] = {
279 [0] = {
280 .ctype = 1,
281 .latency = 1,
282 .power = 0,
283 .resource = {
284 .space_id = ACPI_ADDRESS_SPACE_FIXED,
285 .bit_width = 2,
286 .bit_offset = 2,
287 .addrl = 0,
288 .addrh = 0,
289 },
290 },
291 [1] = {
292 .ctype = 2,
293 .latency = 0x12,
294 .power = 0,
295 .resource = {
296 .space_id = ACPI_ADDRESS_SPACE_IO,
297 .bit_width = 8,
298 .bit_offset = 0,
299 .addrl = cstate_base_address + 1,
300 .addrh = 0,
301 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
302 },
303 },
304 };
305
306 threads_per_core = ((cpuid_ebx(CPUID_EBX_CORE_ID) & CPUID_EBX_THREADS_MASK)
307 >> CPUID_EBX_THREADS_SHIFT)
308 + 1;
309 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
310 logical_cores = get_cpu_count();
311
312 for (cpu = 0; cpu < logical_cores; cpu++) {
313
314 if (cpu == 0) {
315 /* BSP values for \_SB.Pxxx */
316 proc_blk_len = 6;
317 proc_blk_addr = ACPI_GPE0_BLK;
318 } else {
319 /* AP values for \_SB.Pxxx */
320 proc_blk_addr = 0;
321 proc_blk_len = 0;
322 }
323
324 acpigen_write_processor(cpu, proc_blk_addr, proc_blk_len);
325
326 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
327
328 acpigen_write_pss_object(pstate_values, pstate_count);
329
330 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
331
332 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
333 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
334 HW_ALL);
335 else
336 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
337
338 acpigen_write_PPC(0);
339
340 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
341
342 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
343 CSD_HW_ALL, 0);
344
345 acpigen_pop_len();
346 }
347
348 acpigen_write_processor_package("PPKG", 0, logical_cores);
349}