blob: 09c4a1540182fbd836d70d189d7a98909b20230f [file] [log] [blame]
Martin Roth1a3de8e2022-10-06 15:57:21 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
Martin Roth20646cd2023-01-04 21:27:06 -07003/* TODO: Update for Phoenix */
Martin Roth1a3de8e2022-10-06 15:57:21 -06004/* TODO: See what can be made common */
5
6/* ACPI - create the Fixed ACPI Description Tables (FADT) */
7
8#include <acpi/acpi.h>
9#include <acpi/acpigen.h>
10#include <amdblocks/acpi.h>
11#include <amdblocks/cppc.h>
12#include <amdblocks/cpu.h>
13#include <amdblocks/acpimmio.h>
14#include <amdblocks/ioapic.h>
15#include <arch/ioapic.h>
16#include <arch/smp/mpspec.h>
17#include <console/console.h>
18#include <cpu/amd/cpuid.h>
19#include <cpu/amd/msr.h>
20#include <cpu/x86/smm.h>
21#include <soc/acpi.h>
22#include <soc/iomap.h>
23#include <soc/msr.h>
24#include <types.h>
25#include "chip.h"
26
27unsigned long acpi_fill_madt(unsigned long current)
28{
29 /* create all subtables for processors */
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +020030 current = acpi_create_madt_lapics_with_nmis(current);
Martin Roth1a3de8e2022-10-06 15:57:21 -060031
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030032 current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current, IO_APIC_ADDR);
Martin Roth1a3de8e2022-10-06 15:57:21 -060033
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030034 current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current,
35 GNB_IO_APIC_ADDR);
Martin Roth1a3de8e2022-10-06 15:57:21 -060036
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
Martin Roth1a3de8e2022-10-06 15:57:21 -060047 return current;
48}
49
50/*
51 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
52 * in the ACPI 3.0b specification.
53 */
54void acpi_fill_fadt(acpi_fadt_t *fadt)
55{
Martin Roth20646cd2023-01-04 21:27:06 -070056 const struct soc_amd_phoenix_config *cfg = config_of_soc();
Martin Roth1a3de8e2022-10-06 15:57:21 -060057
58 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
59
60 fadt->sci_int = ACPI_SCI_IRQ;
61
62 if (permanent_smi_handler()) {
63 fadt->smi_cmd = APM_CNT;
64 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
65 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
66 }
67
68 fadt->pstate_cnt = 0;
69
70 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
71 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
72 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
73 fadt->gpe0_blk = ACPI_GPE0_BLK;
74
75 fadt->pm1_evt_len = 4; /* 32 bits */
76 fadt->pm1_cnt_len = 2; /* 16 bits */
77 fadt->pm_tmr_len = 4; /* 32 bits */
78 fadt->gpe0_blk_len = 8; /* 64 bits */
79
Felix Held164c5ed2022-10-18 00:11:48 +020080 fill_fadt_extended_pm_regs(fadt);
81
Felix Held54c80e12023-02-21 17:59:42 +010082 /* p_lvl2_lat and p_lvl3_lat match what the AGESA code does, but those values are
83 overridden by the _CST packages in the processor devices. */
Martin Roth1a3de8e2022-10-06 15:57:21 -060084 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;
91 fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
92 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;
100 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 */
Martin Roth1a3de8e2022-10-06 15:57:21 -0600104}
105
106static uint32_t get_pstate_core_freq(msr_t pstate_def)
107{
108 uint32_t core_freq, core_freq_mul, core_freq_div;
109 bool valid_freq_divisor;
110
111 /* Core frequency multiplier */
112 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
113
114 /* Core frequency divisor ID */
115 core_freq_div =
116 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
117
118 if (core_freq_div == 0) {
119 return 0;
120 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
121 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
122 /* Allow 1/8 integer steps for this range */
123 valid_freq_divisor = 1;
124 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
125 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
126 /* Only allow 1/4 integer steps for this range */
127 valid_freq_divisor = 1;
128 } else {
129 valid_freq_divisor = 0;
130 }
131
132 if (valid_freq_divisor) {
133 /* 25 * core_freq_mul / (core_freq_div / 8) */
134 core_freq =
135 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
136 } else {
137 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
138 core_freq_div);
139 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
140 }
141 return core_freq;
142}
143
144static uint32_t get_pstate_core_power(msr_t pstate_def)
145{
146 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
147
148 /* Core voltage ID */
149 core_vid =
150 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
151
152 /* Current value in amps */
153 current_value_amps =
154 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
155
156 /* Current divisor */
157 current_divisor =
158 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
159
160 /* Voltage */
161 if (core_vid == 0x00) {
162 /* Voltage off for VID code 0x00 */
163 voltage_in_uvolts = 0;
164 } else {
165 voltage_in_uvolts =
166 SERIAL_VID_BASE_MICROVOLTS + (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
167 }
168
169 /* Power in mW */
170 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
171
172 switch (current_divisor) {
173 case 0:
174 power_in_mw = power_in_mw / 100L;
175 break;
176 case 1:
177 power_in_mw = power_in_mw / 1000L;
178 break;
179 case 2:
180 power_in_mw = power_in_mw / 10000L;
181 break;
182 case 3:
183 /* current_divisor is set to an undefined value.*/
184 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
185 power_in_mw = 0;
186 break;
187 }
188
189 return power_in_mw;
190}
191
192/*
193 * Populate structure describing enabled p-states and return count of enabled p-states.
194 */
195static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
196 struct acpi_xpss_sw_pstate *pstate_xpss_values)
197{
198 msr_t pstate_def;
199 size_t pstate_count, pstate;
200 uint32_t pstate_enable, max_pstate;
201
202 pstate_count = 0;
203 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
204
205 for (pstate = 0; pstate <= max_pstate; pstate++) {
206 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
207
208 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
209 >> PSTATE_DEF_HI_ENABLE_SHIFT;
210 if (!pstate_enable)
211 continue;
212
213 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
214 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
215 pstate_values[pstate_count].transition_latency = 0;
216 pstate_values[pstate_count].bus_master_latency = 0;
217 pstate_values[pstate_count].control_value = pstate;
218 pstate_values[pstate_count].status_value = pstate;
219
220 pstate_xpss_values[pstate_count].core_freq =
221 (uint64_t)pstate_values[pstate_count].core_freq;
222 pstate_xpss_values[pstate_count].power =
223 (uint64_t)pstate_values[pstate_count].power;
224 pstate_xpss_values[pstate_count].transition_latency = 0;
225 pstate_xpss_values[pstate_count].bus_master_latency = 0;
226 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
227 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
228 pstate_count++;
229 }
230
231 return pstate_count;
232}
233
234void generate_cpu_entries(const struct device *device)
235{
236 int logical_cores;
Felix Heldf678ecf2023-01-28 04:18:39 +0100237 size_t pstate_count, cpu;
Martin Roth1a3de8e2022-10-06 15:57:21 -0600238 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
239 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
Felix Heldf678ecf2023-01-28 04:18:39 +0100240 uint32_t threads_per_core;
Martin Roth1a3de8e2022-10-06 15:57:21 -0600241 uint32_t cstate_base_address =
242 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
243
244 const acpi_addr_t perf_ctrl = {
245 .space_id = ACPI_ADDRESS_SPACE_FIXED,
246 .bit_width = 64,
247 .addrl = PS_CTL_REG,
248 };
249 const acpi_addr_t perf_sts = {
250 .space_id = ACPI_ADDRESS_SPACE_FIXED,
251 .bit_width = 64,
252 .addrl = PS_STS_REG,
253 };
254
255 const acpi_cstate_t cstate_info[] = {
256 [0] = {
257 .ctype = 1,
258 .latency = 1,
259 .power = 0,
260 .resource = {
261 .space_id = ACPI_ADDRESS_SPACE_FIXED,
262 .bit_width = 2,
263 .bit_offset = 2,
264 .addrl = 0,
265 .addrh = 0,
266 },
267 },
268 [1] = {
269 .ctype = 2,
270 .latency = 0x12,
271 .power = 0,
272 .resource = {
273 .space_id = ACPI_ADDRESS_SPACE_IO,
274 .bit_width = 8,
275 .bit_offset = 0,
276 .addrl = cstate_base_address + 1,
277 .addrh = 0,
278 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
279 },
280 },
281 [2] = {
282 .ctype = 3,
283 .latency = 350,
284 .power = 0,
285 .resource = {
286 .space_id = ACPI_ADDRESS_SPACE_IO,
287 .bit_width = 8,
288 .bit_offset = 0,
289 .addrl = cstate_base_address + 2,
290 .addrh = 0,
291 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
292 },
293 },
294 };
295
296 threads_per_core = get_threads_per_core();
297 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
298 logical_cores = get_cpu_count();
299
300 for (cpu = 0; cpu < logical_cores; cpu++) {
Felix Heldf678ecf2023-01-28 04:18:39 +0100301 acpigen_write_processor_device(cpu);
Martin Roth1a3de8e2022-10-06 15:57:21 -0600302
303 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
304
305 acpigen_write_pss_object(pstate_values, pstate_count);
306
307 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
308
309 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
310 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
311 HW_ALL);
312 else
313 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
314
315 acpigen_write_PPC(0);
316
317 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
318
319 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
320 CSD_HW_ALL, 0);
321
322 generate_cppc_entries(cpu);
323
Felix Heldf678ecf2023-01-28 04:18:39 +0100324 acpigen_write_processor_device_end();
Martin Roth1a3de8e2022-10-06 15:57:21 -0600325 }
326
327 acpigen_write_processor_package("PPKG", 0, logical_cores);
328}