blob: 044b345df1fd620617be501d400c90172f45a8a7 [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 */
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +020029 current = acpi_create_madt_lapics_with_nmis(current);
Felix Held3c44c622022-01-10 20:57:29 +010030
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030031 current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current, IO_APIC_ADDR);
Felix Held3c44c622022-01-10 20:57:29 +010032
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030033 current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current,
34 GNB_IO_APIC_ADDR);
Felix Held3c44c622022-01-10 20:57:29 +010035
36 /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
37 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
38 MP_BUS_ISA, 0, 2,
39 MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT);
40 /* SCI IRQ type override */
41 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
42 MP_BUS_ISA, ACPI_SCI_IRQ, ACPI_SCI_IRQ,
43 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
44 current = acpi_fill_madt_irqoverride(current);
45
Felix Held3c44c622022-01-10 20:57:29 +010046 return current;
47}
48
49/*
50 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
51 * in the ACPI 3.0b specification.
52 */
53void acpi_fill_fadt(acpi_fadt_t *fadt)
54{
Jon Murphy4f732422022-08-05 15:43:44 -060055 const struct soc_amd_mendocino_config *cfg = config_of_soc();
Felix Held3c44c622022-01-10 20:57:29 +010056
57 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
58
59 fadt->sci_int = ACPI_SCI_IRQ;
60
61 if (permanent_smi_handler()) {
62 fadt->smi_cmd = APM_CNT;
63 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
64 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
65 }
66
67 fadt->pstate_cnt = 0;
68
69 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
70 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
71 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
72 fadt->gpe0_blk = ACPI_GPE0_BLK;
73
74 fadt->pm1_evt_len = 4; /* 32 bits */
75 fadt->pm1_cnt_len = 2; /* 16 bits */
76 fadt->pm_tmr_len = 4; /* 32 bits */
77 fadt->gpe0_blk_len = 8; /* 64 bits */
78
Felix Held164c5ed2022-10-18 00:11:48 +020079 fill_fadt_extended_pm_regs(fadt);
80
Felix Held54c80e12023-02-21 17:59:42 +010081 /* p_lvl2_lat and p_lvl3_lat match what the AGESA code does, but those values are
82 overridden by the _CST packages in the processor devices. */
Felix Held3c44c622022-01-10 20:57:29 +010083 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->common_config.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 if (cfg->s0ix_enable)
100 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
101
102 fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
Felix Held3c44c622022-01-10 20:57:29 +0100103}
104
105static uint32_t get_pstate_core_freq(msr_t pstate_def)
106{
107 uint32_t core_freq, core_freq_mul, core_freq_div;
108 bool valid_freq_divisor;
109
110 /* Core frequency multiplier */
111 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
112
113 /* Core frequency divisor ID */
114 core_freq_div =
115 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
116
117 if (core_freq_div == 0) {
118 return 0;
119 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
120 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
121 /* Allow 1/8 integer steps for this range */
122 valid_freq_divisor = 1;
123 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
124 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
125 /* Only allow 1/4 integer steps for this range */
126 valid_freq_divisor = 1;
127 } else {
128 valid_freq_divisor = 0;
129 }
130
131 if (valid_freq_divisor) {
132 /* 25 * core_freq_mul / (core_freq_div / 8) */
133 core_freq =
134 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
135 } else {
136 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
137 core_freq_div);
138 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
139 }
140 return core_freq;
141}
142
143static uint32_t get_pstate_core_power(msr_t pstate_def)
144{
145 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
146
147 /* Core voltage ID */
148 core_vid =
149 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
150
151 /* Current value in amps */
152 current_value_amps =
153 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
154
155 /* Current divisor */
156 current_divisor =
157 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
158
159 /* Voltage */
Fred Reitberger8d2bfbc2022-06-07 11:34:28 -0400160 if (core_vid == 0x00) {
161 /* Voltage off for VID code 0x00 */
Felix Held3c44c622022-01-10 20:57:29 +0100162 voltage_in_uvolts = 0;
163 } else {
164 voltage_in_uvolts =
Fred Reitberger8d2bfbc2022-06-07 11:34:28 -0400165 SERIAL_VID_BASE_MICROVOLTS + (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
Felix Held3c44c622022-01-10 20:57:29 +0100166 }
167
168 /* Power in mW */
Zheng Bao62cd5e82022-08-25 17:11:38 +0800169 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
Felix Held3c44c622022-01-10 20:57:29 +0100170
171 switch (current_divisor) {
172 case 0:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800173 power_in_mw = power_in_mw / 100L;
Felix Held3c44c622022-01-10 20:57:29 +0100174 break;
175 case 1:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800176 power_in_mw = power_in_mw / 1000L;
Felix Held3c44c622022-01-10 20:57:29 +0100177 break;
178 case 2:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800179 power_in_mw = power_in_mw / 10000L;
Felix Held3c44c622022-01-10 20:57:29 +0100180 break;
181 case 3:
182 /* current_divisor is set to an undefined value.*/
183 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
184 power_in_mw = 0;
185 break;
186 }
187
188 return power_in_mw;
189}
190
191/*
192 * Populate structure describing enabled p-states and return count of enabled p-states.
193 */
194static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
195 struct acpi_xpss_sw_pstate *pstate_xpss_values)
196{
197 msr_t pstate_def;
198 size_t pstate_count, pstate;
199 uint32_t pstate_enable, max_pstate;
200
201 pstate_count = 0;
202 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
203
204 for (pstate = 0; pstate <= max_pstate; pstate++) {
205 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
206
207 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
208 >> PSTATE_DEF_HI_ENABLE_SHIFT;
209 if (!pstate_enable)
210 continue;
211
212 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
213 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
214 pstate_values[pstate_count].transition_latency = 0;
215 pstate_values[pstate_count].bus_master_latency = 0;
216 pstate_values[pstate_count].control_value = pstate;
217 pstate_values[pstate_count].status_value = pstate;
218
219 pstate_xpss_values[pstate_count].core_freq =
220 (uint64_t)pstate_values[pstate_count].core_freq;
221 pstate_xpss_values[pstate_count].power =
222 (uint64_t)pstate_values[pstate_count].power;
223 pstate_xpss_values[pstate_count].transition_latency = 0;
224 pstate_xpss_values[pstate_count].bus_master_latency = 0;
225 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
226 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
227 pstate_count++;
228 }
229
230 return pstate_count;
231}
232
233void generate_cpu_entries(const struct device *device)
234{
235 int logical_cores;
Felix Held7c269602023-01-28 04:17:40 +0100236 size_t pstate_count, cpu;
Felix Held3c44c622022-01-10 20:57:29 +0100237 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
238 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
Felix Held7c269602023-01-28 04:17:40 +0100239 uint32_t threads_per_core;
Felix Held3c44c622022-01-10 20:57:29 +0100240 uint32_t cstate_base_address =
241 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
242
243 const acpi_addr_t perf_ctrl = {
244 .space_id = ACPI_ADDRESS_SPACE_FIXED,
245 .bit_width = 64,
246 .addrl = PS_CTL_REG,
247 };
248 const acpi_addr_t perf_sts = {
249 .space_id = ACPI_ADDRESS_SPACE_FIXED,
250 .bit_width = 64,
251 .addrl = PS_STS_REG,
252 };
253
254 const acpi_cstate_t cstate_info[] = {
255 [0] = {
256 .ctype = 1,
257 .latency = 1,
258 .power = 0,
259 .resource = {
260 .space_id = ACPI_ADDRESS_SPACE_FIXED,
261 .bit_width = 2,
262 .bit_offset = 2,
263 .addrl = 0,
264 .addrh = 0,
265 },
266 },
267 [1] = {
268 .ctype = 2,
269 .latency = 0x12,
270 .power = 0,
271 .resource = {
272 .space_id = ACPI_ADDRESS_SPACE_IO,
273 .bit_width = 8,
274 .bit_offset = 0,
275 .addrl = cstate_base_address + 1,
276 .addrh = 0,
277 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
278 },
279 },
280 [2] = {
281 .ctype = 3,
282 .latency = 350,
283 .power = 0,
284 .resource = {
285 .space_id = ACPI_ADDRESS_SPACE_IO,
286 .bit_width = 8,
287 .bit_offset = 0,
288 .addrl = cstate_base_address + 2,
289 .addrh = 0,
290 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
291 },
292 },
293 };
294
Felix Heldd4b5ad02022-01-25 04:14:05 +0100295 threads_per_core = get_threads_per_core();
Felix Held3c44c622022-01-10 20:57:29 +0100296 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
297 logical_cores = get_cpu_count();
298
299 for (cpu = 0; cpu < logical_cores; cpu++) {
Felix Held7c269602023-01-28 04:17:40 +0100300 acpigen_write_processor_device(cpu);
Felix Held3c44c622022-01-10 20:57:29 +0100301
302 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
303
304 acpigen_write_pss_object(pstate_values, pstate_count);
305
306 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
307
308 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
309 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
310 HW_ALL);
311 else
312 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
313
314 acpigen_write_PPC(0);
315
316 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
317
318 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
319 CSD_HW_ALL, 0);
320
Felix Held665476d2022-08-03 22:18:18 +0200321 generate_cppc_entries(cpu);
322
Felix Held7c269602023-01-28 04:17:40 +0100323 acpigen_write_processor_device_end();
Felix Held3c44c622022-01-10 20:57:29 +0100324 }
325
326 acpigen_write_processor_package("PPKG", 0, logical_cores);
327}