blob: eca33332778563dde7685e9cf34fa7b1c90aae10 [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Roth5c354b92019-04-22 14:55:16 -06002
3/*
4 * ACPI - create the Fixed ACPI Description Tables (FADT)
5 */
6
7#include <string.h>
8#include <console/console.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07009#include <acpi/acpi.h>
10#include <acpi/acpigen.h>
Martin Roth5c354b92019-04-22 14:55:16 -060011#include <device/pci_ops.h>
12#include <arch/ioapic.h>
Raul E Rangel93b62e62020-01-31 12:53:45 -070013#include <arch/smp/mpspec.h>
Jason Glenesk498015d2020-12-10 03:28:38 -080014#include <cpu/amd/cpuid.h>
Jason Gleneskbc521432020-09-14 05:22:47 -070015#include <cpu/amd/msr.h>
Martin Roth5c354b92019-04-22 14:55:16 -060016#include <cpu/x86/smm.h>
Martin Roth5c354b92019-04-22 14:55:16 -060017#include <device/device.h>
18#include <device/pci.h>
19#include <amdblocks/acpimmio.h>
20#include <amdblocks/acpi.h>
Raul E Rangel1c88b102021-02-11 10:35:32 -070021#include <amdblocks/chip.h>
Felix Helddd2f3fa2021-02-08 22:23:54 +010022#include <amdblocks/cpu.h>
Felix Held604ffa62021-02-12 00:43:20 +010023#include <amdblocks/ioapic.h>
Martin Roth5c354b92019-04-22 14:55:16 -060024#include <soc/acpi.h>
25#include <soc/pci_devs.h>
Jason Gleneskbc521432020-09-14 05:22:47 -070026#include <soc/msr.h>
Martin Roth5c354b92019-04-22 14:55:16 -060027#include <soc/southbridge.h>
Martin Roth5c354b92019-04-22 14:55:16 -060028#include <soc/gpio.h>
29#include <version.h>
Raul E Rangel93b62e62020-01-31 12:53:45 -070030#include "chip.h"
Martin Roth5c354b92019-04-22 14:55:16 -060031
32unsigned long acpi_fill_madt(unsigned long current)
33{
34 /* create all subtables for processors */
35 current = acpi_create_madt_lapics(current);
36
Martin Roth5c354b92019-04-22 14:55:16 -060037 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
Felix Held604ffa62021-02-12 00:43:20 +010038 FCH_IOAPIC_ID, IO_APIC_ADDR, 0);
Martin Roth5c354b92019-04-22 14:55:16 -060039
Jason Gleneskf459a402020-09-02 16:49:10 -070040 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
Felix Held604ffa62021-02-12 00:43:20 +010041 GNB_IOAPIC_ID, GNB_IO_APIC_ADDR, IO_APIC_INTERRUPTS);
Jason Gleneskf459a402020-09-02 16:49:10 -070042
Felix Held69a957f2021-06-17 15:48:25 +020043 /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
44 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
45 MP_BUS_ISA, 0, 2,
46 MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT);
47 /* SCI IRQ type override */
48 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
49 MP_BUS_ISA, 9, 9,
50 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
Raul E Rangel93b62e62020-01-31 12:53:45 -070051
Raul E Rangelffab5e62021-02-11 11:07:11 -070052 current = acpi_fill_madt_irqoverride(current);
Martin Roth5c354b92019-04-22 14:55:16 -060053
54 /* create all subtables for processors */
55 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
Felix Held69a957f2021-06-17 15:48:25 +020056 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS,
57 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
58 1 /* 1: LINT1 connect to NMI */);
Martin Roth5c354b92019-04-22 14:55:16 -060059
60 return current;
61}
62
63/*
64 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
65 * in the ACPI 3.0b specification.
66 */
Kyösti Mälkki61ef71b2020-05-30 18:54:39 +030067void acpi_fill_fadt(acpi_fadt_t *fadt)
Martin Roth5c354b92019-04-22 14:55:16 -060068{
Raul E Rangel1c88b102021-02-11 10:35:32 -070069 const struct soc_amd_common_config *cfg = soc_get_common_config();
Martin Rotheca8faa2019-12-01 16:49:19 -070070
Felix Held757d6452021-02-04 21:31:49 +010071 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
Martin Roth5c354b92019-04-22 14:55:16 -060072
Martin Roth5c354b92019-04-22 14:55:16 -060073 fadt->sci_int = 9; /* IRQ 09 - ACPI SCI */
74
Kyösti Mälkki0a9e72e2019-08-11 01:22:28 +030075 if (permanent_smi_handler()) {
Martin Roth5c354b92019-04-22 14:55:16 -060076 fadt->smi_cmd = APM_CNT;
77 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
78 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
Martin Roth5c354b92019-04-22 14:55:16 -060079 }
80
81 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060082 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060083 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
84 fadt->gpe0_blk = ACPI_GPE0_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060085
86 fadt->pm1_evt_len = 4; /* 32 bits */
87 fadt->pm1_cnt_len = 2; /* 16 bits */
Martin Roth5c354b92019-04-22 14:55:16 -060088 fadt->pm_tmr_len = 4; /* 32 bits */
89 fadt->gpe0_blk_len = 8; /* 64 bits */
Martin Roth5c354b92019-04-22 14:55:16 -060090
91 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
92 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
Martin Roth5c354b92019-04-22 14:55:16 -060093 fadt->duty_offset = 1; /* CLK_VAL bits 3:1 */
94 fadt->duty_width = 3; /* CLK_VAL bits 3:1 */
Raul E Rangel041fcf52020-08-12 12:13:35 -060095 fadt->day_alrm = 0x0d;
96 fadt->mon_alrm = 0;
97 fadt->century = 0x32;
Martin Rotheca8faa2019-12-01 16:49:19 -070098 fadt->iapc_boot_arch = cfg->fadt_boot_arch; /* legacy free default */
Martin Roth5c354b92019-04-22 14:55:16 -060099 fadt->res2 = 0; /* reserved, MUST be 0 ACPI 3.0 */
Martin Rotheca8faa2019-12-01 16:49:19 -0700100 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
101 ACPI_FADT_C1_SUPPORTED |
102 ACPI_FADT_S4_RTC_WAKE |
103 ACPI_FADT_32BIT_TIMER |
104 ACPI_FADT_PCI_EXPRESS_WAKE |
105 ACPI_FADT_PLATFORM_CLOCK |
106 ACPI_FADT_S4_RTC_VALID |
107 ACPI_FADT_REMOTE_POWER_ON;
108 fadt->flags |= cfg->fadt_flags; /* additional board-specific flags */
Martin Roth5c354b92019-04-22 14:55:16 -0600109
Martin Roth5c354b92019-04-22 14:55:16 -0600110 fadt->ARM_boot_arch = 0; /* MUST be 0 ACPI 3.0 */
111 fadt->FADT_MinorVersion = 0; /* MUST be 0 ACPI 3.0 */
112
113 fadt->x_firmware_ctl_l = 0; /* set to 0 if firmware_ctrl is used */
114 fadt->x_firmware_ctl_h = 0;
Martin Roth5c354b92019-04-22 14:55:16 -0600115
116 fadt->x_pm1a_evt_blk.space_id = ACPI_ADDRESS_SPACE_IO;
117 fadt->x_pm1a_evt_blk.bit_width = 32;
118 fadt->x_pm1a_evt_blk.bit_offset = 0;
119 fadt->x_pm1a_evt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
120 fadt->x_pm1a_evt_blk.addrl = ACPI_PM_EVT_BLK;
121 fadt->x_pm1a_evt_blk.addrh = 0x0;
122
Martin Roth5c354b92019-04-22 14:55:16 -0600123 fadt->x_pm1a_cnt_blk.space_id = ACPI_ADDRESS_SPACE_IO;
124 fadt->x_pm1a_cnt_blk.bit_width = 16;
125 fadt->x_pm1a_cnt_blk.bit_offset = 0;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100126 fadt->x_pm1a_cnt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Martin Roth5c354b92019-04-22 14:55:16 -0600127 fadt->x_pm1a_cnt_blk.addrl = ACPI_PM1_CNT_BLK;
128 fadt->x_pm1a_cnt_blk.addrh = 0x0;
129
Martin Roth5c354b92019-04-22 14:55:16 -0600130 fadt->x_pm_tmr_blk.space_id = ACPI_ADDRESS_SPACE_IO;
131 fadt->x_pm_tmr_blk.bit_width = 32;
132 fadt->x_pm_tmr_blk.bit_offset = 0;
133 fadt->x_pm_tmr_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
134 fadt->x_pm_tmr_blk.addrl = ACPI_PM_TMR_BLK;
135 fadt->x_pm_tmr_blk.addrh = 0x0;
136
Martin Roth5c354b92019-04-22 14:55:16 -0600137 fadt->x_gpe0_blk.space_id = ACPI_ADDRESS_SPACE_IO;
138 fadt->x_gpe0_blk.bit_width = 64; /* EventStatus + Event Enable */
139 fadt->x_gpe0_blk.bit_offset = 0;
Angel Ponsa23aff32020-06-21 20:47:54 +0200140 fadt->x_gpe0_blk.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
Martin Roth5c354b92019-04-22 14:55:16 -0600141 fadt->x_gpe0_blk.addrl = ACPI_GPE0_BLK;
142 fadt->x_gpe0_blk.addrh = 0x0;
Martin Roth5c354b92019-04-22 14:55:16 -0600143}
144
Jason Gleneskbc521432020-09-14 05:22:47 -0700145static uint32_t get_pstate_core_freq(msr_t pstate_def)
146{
147 uint32_t core_freq, core_freq_mul, core_freq_div;
148 bool valid_freq_divisor;
149
150 /* Core frequency multiplier */
151 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
152
153 /* Core frequency divisor ID */
154 core_freq_div =
155 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
156
157 if (core_freq_div == 0) {
158 return 0;
159 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
160 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
161 /* Allow 1/8 integer steps for this range */
162 valid_freq_divisor = 1;
163 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
164 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
165 /* Only allow 1/4 integer steps for this range */
166 valid_freq_divisor = 1;
167 } else {
168 valid_freq_divisor = 0;
169 }
170
171 if (valid_freq_divisor) {
172 /* 25 * core_freq_mul / (core_freq_div / 8) */
173 core_freq =
174 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
175 } else {
176 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
177 core_freq_div);
178 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
179 }
180 return core_freq;
181}
182
183static uint32_t get_pstate_core_power(msr_t pstate_def)
184{
185 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
186
187 /* Core voltage ID */
188 core_vid =
189 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
190
191 /* Current value in amps */
192 current_value_amps =
193 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
194
195 /* Current divisor */
196 current_divisor =
197 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
198
199 /* Voltage */
200 if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
201 /* Voltage off for VID codes 0xF8 to 0xFF */
202 voltage_in_uvolts = 0;
203 } else {
204 voltage_in_uvolts =
205 SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
206 }
207
208 /* Power in mW */
209 power_in_mw = (voltage_in_uvolts) / 1000 * current_value_amps;
210
211 switch (current_divisor) {
212 case 0:
213 break;
214 case 1:
215 power_in_mw = power_in_mw / 10L;
216 break;
217 case 2:
218 power_in_mw = power_in_mw / 100L;
219 break;
220 case 3:
221 /* current_divisor is set to an undefined value.*/
222 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
223 power_in_mw = 0;
224 break;
225 }
226
227 return power_in_mw;
228}
229
230/*
231 * Populate structure describing enabled p-states and return count of enabled p-states.
232 */
233static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
234 struct acpi_xpss_sw_pstate *pstate_xpss_values)
235{
236 msr_t pstate_def;
237 size_t pstate_count, pstate;
238 uint32_t pstate_enable, max_pstate;
239
240 pstate_count = 0;
241 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
242
243 for (pstate = 0; pstate <= max_pstate; pstate++) {
244 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
245
246 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
247 >> PSTATE_DEF_HI_ENABLE_SHIFT;
248 if (!pstate_enable)
249 continue;
250
251 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
252 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
253 pstate_values[pstate_count].transition_latency = 0;
254 pstate_values[pstate_count].bus_master_latency = 0;
255 pstate_values[pstate_count].control_value = pstate;
256 pstate_values[pstate_count].status_value = pstate;
257
258 pstate_xpss_values[pstate_count].core_freq =
259 (uint64_t)pstate_values[pstate_count].core_freq;
260 pstate_xpss_values[pstate_count].power =
261 (uint64_t)pstate_values[pstate_count].power;
262 pstate_xpss_values[pstate_count].transition_latency = 0;
263 pstate_xpss_values[pstate_count].bus_master_latency = 0;
264 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
265 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
266 pstate_count++;
267 }
268
269 return pstate_count;
270}
271
Furquan Shaikh7536a392020-04-24 21:59:21 -0700272void generate_cpu_entries(const struct device *device)
Martin Roth5c354b92019-04-22 14:55:16 -0600273{
Jason Gleneskbc521432020-09-14 05:22:47 -0700274 int logical_cores;
275 size_t pstate_count, cpu, proc_blk_len;
276 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
277 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
278 uint32_t threads_per_core, proc_blk_addr;
279 uint32_t cstate_base_address =
280 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
Martin Roth5c354b92019-04-22 14:55:16 -0600281
Jason Gleneskbc521432020-09-14 05:22:47 -0700282 const acpi_addr_t perf_ctrl = {
283 .space_id = ACPI_ADDRESS_SPACE_FIXED,
284 .bit_width = 64,
285 .addrl = PS_CTL_REG,
286 };
287 const acpi_addr_t perf_sts = {
288 .space_id = ACPI_ADDRESS_SPACE_FIXED,
289 .bit_width = 64,
290 .addrl = PS_STS_REG,
291 };
Martin Roth5c354b92019-04-22 14:55:16 -0600292
Jason Gleneskbc521432020-09-14 05:22:47 -0700293 acpi_cstate_t cstate_info[] = {
294 [0] = {
295 .ctype = 1,
296 .latency = 1,
297 .power = 0,
298 .resource = {
299 .space_id = ACPI_ADDRESS_SPACE_FIXED,
300 .bit_width = 2,
301 .bit_offset = 2,
302 .addrl = 0,
303 .addrh = 0,
304 },
305 },
306 [1] = {
307 .ctype = 2,
308 .latency = 400,
309 .power = 0,
310 .resource = {
311 .space_id = ACPI_ADDRESS_SPACE_IO,
312 .bit_width = 8,
313 .bit_offset = 0,
314 .addrl = cstate_base_address + 1,
315 .addrh = 0,
316 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
317 },
318 },
319 };
Martin Roth5c354b92019-04-22 14:55:16 -0600320
Jason Gleneskbc521432020-09-14 05:22:47 -0700321 threads_per_core = ((cpuid_ebx(CPUID_EBX_CORE_ID) & CPUID_EBX_THREADS_MASK)
322 >> CPUID_EBX_THREADS_SHIFT)
323 + 1;
324 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
325 logical_cores = get_cpu_count();
326
327 for (cpu = 0; cpu < logical_cores; cpu++) {
328
329 if (cpu == 0) {
330 /* BSP values for \_SB.Pxxx */
331 proc_blk_len = 6;
332 proc_blk_addr = ACPI_GPE0_BLK;
333 } else {
334 /* AP values for \_SB.Pxxx */
335 proc_blk_addr = 0;
336 proc_blk_len = 0;
337 }
338
339 acpigen_write_processor(cpu, proc_blk_addr, proc_blk_len);
340
341 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
342
343 acpigen_write_pss_object(pstate_values, pstate_count);
344
345 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
346
347 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
348 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
349 HW_ALL);
350 else
351 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
352
353 acpigen_write_PPC(0);
354
355 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
356
Felix Heldc5635082021-03-30 02:04:02 +0200357 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
358 CSD_HW_ALL, 0);
Jason Gleneskbc521432020-09-14 05:22:47 -0700359
Martin Roth5c354b92019-04-22 14:55:16 -0600360 acpigen_pop_len();
361 }
Kyösti Mälkkida321d82021-01-27 20:22:33 +0200362
363 acpigen_write_scope("\\");
364 acpigen_write_name_integer("PCNT", logical_cores);
365 acpigen_pop_len();
Martin Roth5c354b92019-04-22 14:55:16 -0600366}