blob: 779ea4ebb088f34fd0d785a90fb78fb252e53b1a [file] [log] [blame]
Felix Held3c44c622022-01-10 20:57:29 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2
3/* TODO: Check if this is still correct */
4
5/* ACPI - create the Fixed ACPI Description Tables (FADT) */
6
7#include <acpi/acpi.h>
8#include <acpi/acpigen.h>
9#include <amdblocks/acpi.h>
Felix Held665476d2022-08-03 22:18:18 +020010#include <amdblocks/cppc.h>
Felix Held3c44c622022-01-10 20:57:29 +010011#include <amdblocks/cpu.h>
12#include <amdblocks/acpimmio.h>
13#include <amdblocks/ioapic.h>
14#include <arch/ioapic.h>
15#include <arch/smp/mpspec.h>
16#include <console/console.h>
17#include <cpu/amd/cpuid.h>
18#include <cpu/amd/msr.h>
19#include <cpu/x86/smm.h>
20#include <soc/acpi.h>
21#include <soc/iomap.h>
22#include <soc/msr.h>
23#include <types.h>
24#include "chip.h"
Felix Held3c44c622022-01-10 20:57:29 +010025
26unsigned long acpi_fill_madt(unsigned long current)
27{
28 /* create all subtables for processors */
29 current = acpi_create_madt_lapics(current);
30
31 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
32 FCH_IOAPIC_ID, IO_APIC_ADDR, 0);
33
34 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
35 GNB_IOAPIC_ID, GNB_IO_APIC_ADDR, IO_APIC_INTERRUPTS);
36
37 /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
38 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
39 MP_BUS_ISA, 0, 2,
40 MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT);
41 /* SCI IRQ type override */
42 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
43 MP_BUS_ISA, ACPI_SCI_IRQ, ACPI_SCI_IRQ,
44 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
45 current = acpi_fill_madt_irqoverride(current);
46
47 /* create all subtables for processors */
48 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
49 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS,
50 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
51 1 /* 1: LINT1 connect to NMI */);
52
53 return current;
54}
55
56/*
57 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
58 * in the ACPI 3.0b specification.
59 */
60void acpi_fill_fadt(acpi_fadt_t *fadt)
61{
Jon Murphy4f732422022-08-05 15:43:44 -060062 const struct soc_amd_mendocino_config *cfg = config_of_soc();
Felix Held3c44c622022-01-10 20:57:29 +010063
64 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
65
66 fadt->sci_int = ACPI_SCI_IRQ;
67
68 if (permanent_smi_handler()) {
69 fadt->smi_cmd = APM_CNT;
70 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
71 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
72 }
73
74 fadt->pstate_cnt = 0;
75
76 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
77 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
78 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
79 fadt->gpe0_blk = ACPI_GPE0_BLK;
80
81 fadt->pm1_evt_len = 4; /* 32 bits */
82 fadt->pm1_cnt_len = 2; /* 16 bits */
83 fadt->pm_tmr_len = 4; /* 32 bits */
84 fadt->gpe0_blk_len = 8; /* 64 bits */
85
Felix Held164c5ed2022-10-18 00:11:48 +020086 fill_fadt_extended_pm_regs(fadt);
87
Felix Held3c44c622022-01-10 20:57:29 +010088 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
89 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
90 fadt->duty_offset = 0; /* Not supported */
91 fadt->duty_width = 0; /* Not supported */
92 fadt->day_alrm = RTC_DATE_ALARM;
93 fadt->mon_alrm = 0;
94 fadt->century = RTC_ALT_CENTURY;
95 fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
96 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
97 ACPI_FADT_C1_SUPPORTED |
98 ACPI_FADT_S4_RTC_WAKE |
99 ACPI_FADT_32BIT_TIMER |
100 ACPI_FADT_PCI_EXPRESS_WAKE |
101 ACPI_FADT_PLATFORM_CLOCK |
102 ACPI_FADT_S4_RTC_VALID |
103 ACPI_FADT_REMOTE_POWER_ON;
104 if (cfg->s0ix_enable)
105 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
106
107 fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
Felix Held3c44c622022-01-10 20:57:29 +0100108}
109
110static uint32_t get_pstate_core_freq(msr_t pstate_def)
111{
112 uint32_t core_freq, core_freq_mul, core_freq_div;
113 bool valid_freq_divisor;
114
115 /* Core frequency multiplier */
116 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
117
118 /* Core frequency divisor ID */
119 core_freq_div =
120 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
121
122 if (core_freq_div == 0) {
123 return 0;
124 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
125 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
126 /* Allow 1/8 integer steps for this range */
127 valid_freq_divisor = 1;
128 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
129 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
130 /* Only allow 1/4 integer steps for this range */
131 valid_freq_divisor = 1;
132 } else {
133 valid_freq_divisor = 0;
134 }
135
136 if (valid_freq_divisor) {
137 /* 25 * core_freq_mul / (core_freq_div / 8) */
138 core_freq =
139 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
140 } else {
141 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
142 core_freq_div);
143 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
144 }
145 return core_freq;
146}
147
148static uint32_t get_pstate_core_power(msr_t pstate_def)
149{
150 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
151
152 /* Core voltage ID */
153 core_vid =
154 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
155
156 /* Current value in amps */
157 current_value_amps =
158 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
159
160 /* Current divisor */
161 current_divisor =
162 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
163
164 /* Voltage */
Fred Reitberger8d2bfbc2022-06-07 11:34:28 -0400165 if (core_vid == 0x00) {
166 /* Voltage off for VID code 0x00 */
Felix Held3c44c622022-01-10 20:57:29 +0100167 voltage_in_uvolts = 0;
168 } else {
169 voltage_in_uvolts =
Fred Reitberger8d2bfbc2022-06-07 11:34:28 -0400170 SERIAL_VID_BASE_MICROVOLTS + (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
Felix Held3c44c622022-01-10 20:57:29 +0100171 }
172
173 /* Power in mW */
Zheng Bao62cd5e82022-08-25 17:11:38 +0800174 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
Felix Held3c44c622022-01-10 20:57:29 +0100175
176 switch (current_divisor) {
177 case 0:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800178 power_in_mw = power_in_mw / 100L;
Felix Held3c44c622022-01-10 20:57:29 +0100179 break;
180 case 1:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800181 power_in_mw = power_in_mw / 1000L;
Felix Held3c44c622022-01-10 20:57:29 +0100182 break;
183 case 2:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800184 power_in_mw = power_in_mw / 10000L;
Felix Held3c44c622022-01-10 20:57:29 +0100185 break;
186 case 3:
187 /* current_divisor is set to an undefined value.*/
188 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
189 power_in_mw = 0;
190 break;
191 }
192
193 return power_in_mw;
194}
195
196/*
197 * Populate structure describing enabled p-states and return count of enabled p-states.
198 */
199static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
200 struct acpi_xpss_sw_pstate *pstate_xpss_values)
201{
202 msr_t pstate_def;
203 size_t pstate_count, pstate;
204 uint32_t pstate_enable, max_pstate;
205
206 pstate_count = 0;
207 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
208
209 for (pstate = 0; pstate <= max_pstate; pstate++) {
210 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
211
212 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
213 >> PSTATE_DEF_HI_ENABLE_SHIFT;
214 if (!pstate_enable)
215 continue;
216
217 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
218 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
219 pstate_values[pstate_count].transition_latency = 0;
220 pstate_values[pstate_count].bus_master_latency = 0;
221 pstate_values[pstate_count].control_value = pstate;
222 pstate_values[pstate_count].status_value = pstate;
223
224 pstate_xpss_values[pstate_count].core_freq =
225 (uint64_t)pstate_values[pstate_count].core_freq;
226 pstate_xpss_values[pstate_count].power =
227 (uint64_t)pstate_values[pstate_count].power;
228 pstate_xpss_values[pstate_count].transition_latency = 0;
229 pstate_xpss_values[pstate_count].bus_master_latency = 0;
230 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
231 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
232 pstate_count++;
233 }
234
235 return pstate_count;
236}
237
238void generate_cpu_entries(const struct device *device)
239{
240 int logical_cores;
241 size_t pstate_count, cpu, proc_blk_len;
242 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
243 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
244 uint32_t threads_per_core, proc_blk_addr;
245 uint32_t cstate_base_address =
246 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
247
248 const acpi_addr_t perf_ctrl = {
249 .space_id = ACPI_ADDRESS_SPACE_FIXED,
250 .bit_width = 64,
251 .addrl = PS_CTL_REG,
252 };
253 const acpi_addr_t perf_sts = {
254 .space_id = ACPI_ADDRESS_SPACE_FIXED,
255 .bit_width = 64,
256 .addrl = PS_STS_REG,
257 };
258
259 const acpi_cstate_t cstate_info[] = {
260 [0] = {
261 .ctype = 1,
262 .latency = 1,
263 .power = 0,
264 .resource = {
265 .space_id = ACPI_ADDRESS_SPACE_FIXED,
266 .bit_width = 2,
267 .bit_offset = 2,
268 .addrl = 0,
269 .addrh = 0,
270 },
271 },
272 [1] = {
273 .ctype = 2,
274 .latency = 0x12,
275 .power = 0,
276 .resource = {
277 .space_id = ACPI_ADDRESS_SPACE_IO,
278 .bit_width = 8,
279 .bit_offset = 0,
280 .addrl = cstate_base_address + 1,
281 .addrh = 0,
282 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
283 },
284 },
285 [2] = {
286 .ctype = 3,
287 .latency = 350,
288 .power = 0,
289 .resource = {
290 .space_id = ACPI_ADDRESS_SPACE_IO,
291 .bit_width = 8,
292 .bit_offset = 0,
293 .addrl = cstate_base_address + 2,
294 .addrh = 0,
295 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
296 },
297 },
298 };
299
Felix Heldd4b5ad02022-01-25 04:14:05 +0100300 threads_per_core = get_threads_per_core();
Felix Held3c44c622022-01-10 20:57:29 +0100301 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
302 logical_cores = get_cpu_count();
303
304 for (cpu = 0; cpu < logical_cores; cpu++) {
305
306 if (cpu == 0) {
307 /* BSP values for \_SB.Pxxx */
308 proc_blk_len = 6;
309 proc_blk_addr = ACPI_GPE0_BLK;
310 } else {
311 /* AP values for \_SB.Pxxx */
312 proc_blk_addr = 0;
313 proc_blk_len = 0;
314 }
315
316 acpigen_write_processor(cpu, proc_blk_addr, proc_blk_len);
317
318 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
319
320 acpigen_write_pss_object(pstate_values, pstate_count);
321
322 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
323
324 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
325 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
326 HW_ALL);
327 else
328 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
329
330 acpigen_write_PPC(0);
331
332 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
333
334 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
335 CSD_HW_ALL, 0);
336
Felix Held665476d2022-08-03 22:18:18 +0200337 generate_cppc_entries(cpu);
338
Felix Held3c44c622022-01-10 20:57:29 +0100339 acpigen_pop_len();
340 }
341
342 acpigen_write_processor_package("PPKG", 0, logical_cores);
343}