blob: 6ce041b85070ab4045f43867fa3fee778268051e [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>
Felix Held8f7f4bf2022-08-03 22:10:05 +02008#include <amdblocks/cppc.h>
Jason Glenesk79542fa2021-03-10 03:50:57 -08009#include <amdblocks/cpu.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -070010#include <amdblocks/acpimmio.h>
Raul E Rangel65819cd2021-02-16 10:37:46 -070011#include <amdblocks/ioapic.h>
12#include <arch/ioapic.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -070013#include <arch/smp/mpspec.h>
14#include <console/console.h>
Jason Glenesk79542fa2021-03-10 03:50:57 -080015#include <cpu/amd/cpuid.h>
16#include <cpu/amd/msr.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -070017#include <cpu/x86/smm.h>
18#include <soc/acpi.h>
19#include <soc/iomap.h>
Jason Glenesk79542fa2021-03-10 03:50:57 -080020#include <soc/msr.h>
Raul E Rangel12c6a582021-02-10 16:45:49 -070021#include <types.h>
22#include "chip.h"
Felix Held86024952021-02-03 23:44:28 +010023
24unsigned long acpi_fill_madt(unsigned long current)
25{
Raul E Rangel12c6a582021-02-10 16:45:49 -070026 /* create all subtables for processors */
27 current = acpi_create_madt_lapics(current);
28
Raul E Rangel65819cd2021-02-16 10:37:46 -070029 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
30 FCH_IOAPIC_ID, IO_APIC_ADDR, 0);
31
Felix Held1ed5a632021-05-04 21:51:43 +020032 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
33 GNB_IOAPIC_ID, GNB_IO_APIC_ADDR, IO_APIC_INTERRUPTS);
Raul E Rangel12c6a582021-02-10 16:45:49 -070034
Felix Held69a957f2021-06-17 15:48:25 +020035 /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
36 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
Raul E Rangel12c6a582021-02-10 16:45:49 -070037 MP_BUS_ISA, 0, 2,
38 MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT);
Felix Held69a957f2021-06-17 15:48:25 +020039 /* SCI IRQ type override */
40 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
Raul E Rangel12c6a582021-02-10 16:45:49 -070041 MP_BUS_ISA, ACPI_SCI_IRQ, ACPI_SCI_IRQ,
42 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
Raul E Rangel9bce1fe2021-02-11 11:28:52 -070043 current = acpi_fill_madt_irqoverride(current);
44
45 /* create all subtables for processors */
Felix Held69a957f2021-06-17 15:48:25 +020046 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
Raul E Rangel9bce1fe2021-02-11 11:28:52 -070047 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS,
48 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
49 1 /* 1: LINT1 connect to NMI */);
Raul E Rangel12c6a582021-02-10 16:45:49 -070050
Felix Held86024952021-02-03 23:44:28 +010051 return current;
52}
53
54/*
55 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
56 * in the ACPI 3.0b specification.
57 */
58void acpi_fill_fadt(acpi_fadt_t *fadt)
59{
Jason Gleneskfff318f2021-03-10 02:47:05 -080060 const struct soc_amd_cezanne_config *cfg = config_of_soc();
Raul E Rangel12c6a582021-02-10 16:45:49 -070061
62 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
63
64 fadt->sci_int = ACPI_SCI_IRQ;
65
66 if (permanent_smi_handler()) {
67 fadt->smi_cmd = APM_CNT;
68 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
69 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
70 }
71
72 fadt->pstate_cnt = 0;
73
74 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
75 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
76 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
77 fadt->gpe0_blk = ACPI_GPE0_BLK;
78
79 fadt->pm1_evt_len = 4; /* 32 bits */
80 fadt->pm1_cnt_len = 2; /* 16 bits */
81 fadt->pm_tmr_len = 4; /* 32 bits */
82 fadt->gpe0_blk_len = 8; /* 64 bits */
83
84 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
85 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
86 fadt->duty_offset = 0; /* Not supported */
87 fadt->duty_width = 0; /* Not supported */
88 fadt->day_alrm = RTC_DATE_ALARM;
89 fadt->mon_alrm = 0;
90 fadt->century = RTC_ALT_CENTURY;
Jason Gleneskfff318f2021-03-10 02:47:05 -080091 fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
Raul E Rangel12c6a582021-02-10 16:45:49 -070092 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
93 ACPI_FADT_C1_SUPPORTED |
94 ACPI_FADT_S4_RTC_WAKE |
95 ACPI_FADT_32BIT_TIMER |
96 ACPI_FADT_PCI_EXPRESS_WAKE |
97 ACPI_FADT_PLATFORM_CLOCK |
98 ACPI_FADT_S4_RTC_VALID |
99 ACPI_FADT_REMOTE_POWER_ON;
Jason Gleneskfff318f2021-03-10 02:47:05 -0800100 if (cfg->s0ix_enable)
101 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
102
103 fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
Raul E Rangel12c6a582021-02-10 16:45:49 -0700104
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700105 fadt->x_pm1a_evt_blk.space_id = ACPI_ADDRESS_SPACE_IO;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700106 fadt->x_pm1a_evt_blk.bit_width = 32;
107 fadt->x_pm1a_evt_blk.bit_offset = 0;
108 fadt->x_pm1a_evt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700109 fadt->x_pm1a_evt_blk.addrl = ACPI_PM_EVT_BLK;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700110 fadt->x_pm1a_evt_blk.addrh = 0x0;
111
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700112 fadt->x_pm1a_cnt_blk.space_id = ACPI_ADDRESS_SPACE_IO;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700113 fadt->x_pm1a_cnt_blk.bit_width = 16;
114 fadt->x_pm1a_cnt_blk.bit_offset = 0;
115 fadt->x_pm1a_cnt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700116 fadt->x_pm1a_cnt_blk.addrl = ACPI_PM1_CNT_BLK;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700117 fadt->x_pm1a_cnt_blk.addrh = 0x0;
118
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700119 fadt->x_pm_tmr_blk.space_id = ACPI_ADDRESS_SPACE_IO;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700120 fadt->x_pm_tmr_blk.bit_width = 32;
121 fadt->x_pm_tmr_blk.bit_offset = 0;
122 fadt->x_pm_tmr_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700123 fadt->x_pm_tmr_blk.addrl = ACPI_PM_TMR_BLK;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700124 fadt->x_pm_tmr_blk.addrh = 0x0;
125
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700126 fadt->x_gpe0_blk.space_id = ACPI_ADDRESS_SPACE_IO;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700127 fadt->x_gpe0_blk.bit_width = 64;
128 fadt->x_gpe0_blk.bit_offset = 0;
Raul E Rangel6e3f3832021-02-19 16:01:05 -0700129 fadt->x_gpe0_blk.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
130 fadt->x_gpe0_blk.addrl = ACPI_GPE0_BLK;
Raul E Rangel12c6a582021-02-10 16:45:49 -0700131 fadt->x_gpe0_blk.addrh = 0x0;
Felix Held86024952021-02-03 23:44:28 +0100132}
Jason Glenesk79542fa2021-03-10 03:50:57 -0800133
134static uint32_t get_pstate_core_freq(msr_t pstate_def)
135{
136 uint32_t core_freq, core_freq_mul, core_freq_div;
137 bool valid_freq_divisor;
138
139 /* Core frequency multiplier */
140 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
141
142 /* Core frequency divisor ID */
143 core_freq_div =
144 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
145
146 if (core_freq_div == 0) {
147 return 0;
148 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
149 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
150 /* Allow 1/8 integer steps for this range */
151 valid_freq_divisor = 1;
152 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
153 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
154 /* Only allow 1/4 integer steps for this range */
155 valid_freq_divisor = 1;
156 } else {
157 valid_freq_divisor = 0;
158 }
159
160 if (valid_freq_divisor) {
161 /* 25 * core_freq_mul / (core_freq_div / 8) */
162 core_freq =
163 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
164 } else {
165 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
166 core_freq_div);
167 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
168 }
169 return core_freq;
170}
171
172static uint32_t get_pstate_core_power(msr_t pstate_def)
173{
174 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
175
176 /* Core voltage ID */
177 core_vid =
178 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
179
180 /* Current value in amps */
181 current_value_amps =
182 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
183
184 /* Current divisor */
185 current_divisor =
186 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
187
188 /* Voltage */
189 if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
190 /* Voltage off for VID codes 0xF8 to 0xFF */
191 voltage_in_uvolts = 0;
192 } else {
193 voltage_in_uvolts =
194 SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
195 }
196
197 /* Power in mW */
198 power_in_mw = (voltage_in_uvolts) / 1000 * current_value_amps;
199
200 switch (current_divisor) {
201 case 0:
202 break;
203 case 1:
204 power_in_mw = power_in_mw / 10L;
205 break;
206 case 2:
207 power_in_mw = power_in_mw / 100L;
208 break;
209 case 3:
210 /* current_divisor is set to an undefined value.*/
211 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
212 power_in_mw = 0;
213 break;
214 }
215
216 return power_in_mw;
217}
218
219/*
220 * Populate structure describing enabled p-states and return count of enabled p-states.
221 */
222static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
223 struct acpi_xpss_sw_pstate *pstate_xpss_values)
224{
225 msr_t pstate_def;
226 size_t pstate_count, pstate;
227 uint32_t pstate_enable, max_pstate;
228
229 pstate_count = 0;
230 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
231
232 for (pstate = 0; pstate <= max_pstate; pstate++) {
233 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
234
235 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
236 >> PSTATE_DEF_HI_ENABLE_SHIFT;
237 if (!pstate_enable)
238 continue;
239
240 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
241 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
242 pstate_values[pstate_count].transition_latency = 0;
243 pstate_values[pstate_count].bus_master_latency = 0;
244 pstate_values[pstate_count].control_value = pstate;
245 pstate_values[pstate_count].status_value = pstate;
246
247 pstate_xpss_values[pstate_count].core_freq =
248 (uint64_t)pstate_values[pstate_count].core_freq;
249 pstate_xpss_values[pstate_count].power =
250 (uint64_t)pstate_values[pstate_count].power;
251 pstate_xpss_values[pstate_count].transition_latency = 0;
252 pstate_xpss_values[pstate_count].bus_master_latency = 0;
253 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
254 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
255 pstate_count++;
256 }
257
258 return pstate_count;
259}
260
261void generate_cpu_entries(const struct device *device)
262{
263 int logical_cores;
264 size_t pstate_count, cpu, proc_blk_len;
265 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
266 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
267 uint32_t threads_per_core, proc_blk_addr;
268 uint32_t cstate_base_address =
269 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
270
271 const acpi_addr_t perf_ctrl = {
272 .space_id = ACPI_ADDRESS_SPACE_FIXED,
273 .bit_width = 64,
274 .addrl = PS_CTL_REG,
275 };
276 const acpi_addr_t perf_sts = {
277 .space_id = ACPI_ADDRESS_SPACE_FIXED,
278 .bit_width = 64,
279 .addrl = PS_STS_REG,
280 };
281
Angel Ponsd2794ce2021-10-17 12:59:43 +0200282 const acpi_cstate_t cstate_info[] = {
Jason Glenesk79542fa2021-03-10 03:50:57 -0800283 [0] = {
284 .ctype = 1,
285 .latency = 1,
286 .power = 0,
287 .resource = {
288 .space_id = ACPI_ADDRESS_SPACE_FIXED,
289 .bit_width = 2,
290 .bit_offset = 2,
291 .addrl = 0,
292 .addrh = 0,
293 },
294 },
295 [1] = {
296 .ctype = 2,
297 .latency = 0x12,
298 .power = 0,
299 .resource = {
300 .space_id = ACPI_ADDRESS_SPACE_IO,
301 .bit_width = 8,
302 .bit_offset = 0,
303 .addrl = cstate_base_address + 1,
304 .addrh = 0,
305 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
306 },
307 },
Raul E Rangeld8956f72021-04-19 17:00:58 -0600308 [2] = {
309 .ctype = 3,
310 .latency = 350,
311 .power = 0,
312 .resource = {
313 .space_id = ACPI_ADDRESS_SPACE_IO,
314 .bit_width = 8,
315 .bit_offset = 0,
316 .addrl = cstate_base_address + 2,
317 .addrh = 0,
318 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
319 },
320 },
Jason Glenesk79542fa2021-03-10 03:50:57 -0800321 };
322
Felix Heldd4b5ad02022-01-25 04:14:05 +0100323 threads_per_core = get_threads_per_core();
Jason Glenesk79542fa2021-03-10 03:50:57 -0800324 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
357 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
358 CSD_HW_ALL, 0);
359
Julian Schroeder577e1462021-07-09 16:10:08 -0500360 generate_cppc_entries(cpu);
361
Jason Glenesk79542fa2021-03-10 03:50:57 -0800362 acpigen_pop_len();
363 }
364
365 acpigen_write_processor_package("PPKG", 0, logical_cores);
366}