blob: 631f388fdf6098111affb80255182ea2bfa8213d [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 */
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +020027 current = acpi_create_madt_lapics_with_nmis(current);
Raul E Rangel12c6a582021-02-10 16:45:49 -070028
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030029 current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current, IO_APIC_ADDR);
Raul E Rangel65819cd2021-02-16 10:37:46 -070030
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030031 current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current,
32 GNB_IO_APIC_ADDR);
Raul E Rangel12c6a582021-02-10 16:45:49 -070033
Felix Held69a957f2021-06-17 15:48:25 +020034 /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
35 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
Raul E Rangel12c6a582021-02-10 16:45:49 -070036 MP_BUS_ISA, 0, 2,
37 MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT);
Felix Held69a957f2021-06-17 15:48:25 +020038 /* SCI IRQ type override */
39 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
Raul E Rangel12c6a582021-02-10 16:45:49 -070040 MP_BUS_ISA, ACPI_SCI_IRQ, ACPI_SCI_IRQ,
41 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
Raul E Rangel9bce1fe2021-02-11 11:28:52 -070042 current = acpi_fill_madt_irqoverride(current);
43
Felix Held86024952021-02-03 23:44:28 +010044 return current;
45}
46
47/*
48 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
49 * in the ACPI 3.0b specification.
50 */
51void acpi_fill_fadt(acpi_fadt_t *fadt)
52{
Jason Gleneskfff318f2021-03-10 02:47:05 -080053 const struct soc_amd_cezanne_config *cfg = config_of_soc();
Raul E Rangel12c6a582021-02-10 16:45:49 -070054
55 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
56
57 fadt->sci_int = ACPI_SCI_IRQ;
58
59 if (permanent_smi_handler()) {
60 fadt->smi_cmd = APM_CNT;
61 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
62 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
63 }
64
65 fadt->pstate_cnt = 0;
66
67 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
68 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
69 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
70 fadt->gpe0_blk = ACPI_GPE0_BLK;
71
72 fadt->pm1_evt_len = 4; /* 32 bits */
73 fadt->pm1_cnt_len = 2; /* 16 bits */
74 fadt->pm_tmr_len = 4; /* 32 bits */
75 fadt->gpe0_blk_len = 8; /* 64 bits */
76
Felix Held164c5ed2022-10-18 00:11:48 +020077 fill_fadt_extended_pm_regs(fadt);
78
Felix Held54c80e12023-02-21 17:59:42 +010079 /* p_lvl2_lat and p_lvl3_lat match what the AGESA code does, but those values are
80 overridden by the _CST packages in the processor devices. */
Raul E Rangel12c6a582021-02-10 16:45:49 -070081 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
82 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
83 fadt->duty_offset = 0; /* Not supported */
84 fadt->duty_width = 0; /* Not supported */
85 fadt->day_alrm = RTC_DATE_ALARM;
86 fadt->mon_alrm = 0;
87 fadt->century = RTC_ALT_CENTURY;
Jason Gleneskfff318f2021-03-10 02:47:05 -080088 fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
Raul E Rangel12c6a582021-02-10 16:45:49 -070089 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
90 ACPI_FADT_C1_SUPPORTED |
91 ACPI_FADT_S4_RTC_WAKE |
92 ACPI_FADT_32BIT_TIMER |
93 ACPI_FADT_PCI_EXPRESS_WAKE |
94 ACPI_FADT_PLATFORM_CLOCK |
95 ACPI_FADT_S4_RTC_VALID |
96 ACPI_FADT_REMOTE_POWER_ON;
Jason Gleneskfff318f2021-03-10 02:47:05 -080097 if (cfg->s0ix_enable)
98 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
99
100 fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
Felix Held86024952021-02-03 23:44:28 +0100101}
Jason Glenesk79542fa2021-03-10 03:50:57 -0800102
103static uint32_t get_pstate_core_freq(msr_t pstate_def)
104{
105 uint32_t core_freq, core_freq_mul, core_freq_div;
106 bool valid_freq_divisor;
107
108 /* Core frequency multiplier */
109 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
110
111 /* Core frequency divisor ID */
112 core_freq_div =
113 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
114
115 if (core_freq_div == 0) {
116 return 0;
117 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
118 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
119 /* Allow 1/8 integer steps for this range */
120 valid_freq_divisor = 1;
121 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
122 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
123 /* Only allow 1/4 integer steps for this range */
124 valid_freq_divisor = 1;
125 } else {
126 valid_freq_divisor = 0;
127 }
128
129 if (valid_freq_divisor) {
130 /* 25 * core_freq_mul / (core_freq_div / 8) */
131 core_freq =
132 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
133 } else {
134 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
135 core_freq_div);
136 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
137 }
138 return core_freq;
139}
140
141static uint32_t get_pstate_core_power(msr_t pstate_def)
142{
143 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
144
145 /* Core voltage ID */
146 core_vid =
147 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
148
149 /* Current value in amps */
150 current_value_amps =
151 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
152
153 /* Current divisor */
154 current_divisor =
155 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
156
157 /* Voltage */
158 if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
159 /* Voltage off for VID codes 0xF8 to 0xFF */
160 voltage_in_uvolts = 0;
161 } else {
162 voltage_in_uvolts =
163 SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
164 }
165
166 /* Power in mW */
Zheng Bao62cd5e82022-08-25 17:11:38 +0800167 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800168
169 switch (current_divisor) {
170 case 0:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800171 power_in_mw = power_in_mw / 100L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800172 break;
173 case 1:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800174 power_in_mw = power_in_mw / 1000L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800175 break;
176 case 2:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800177 power_in_mw = power_in_mw / 10000L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800178 break;
179 case 3:
180 /* current_divisor is set to an undefined value.*/
181 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
182 power_in_mw = 0;
183 break;
184 }
185
186 return power_in_mw;
187}
188
189/*
190 * Populate structure describing enabled p-states and return count of enabled p-states.
191 */
192static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
193 struct acpi_xpss_sw_pstate *pstate_xpss_values)
194{
195 msr_t pstate_def;
196 size_t pstate_count, pstate;
197 uint32_t pstate_enable, max_pstate;
198
199 pstate_count = 0;
200 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
201
202 for (pstate = 0; pstate <= max_pstate; pstate++) {
203 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
204
205 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
206 >> PSTATE_DEF_HI_ENABLE_SHIFT;
207 if (!pstate_enable)
208 continue;
209
210 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
211 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
212 pstate_values[pstate_count].transition_latency = 0;
213 pstate_values[pstate_count].bus_master_latency = 0;
214 pstate_values[pstate_count].control_value = pstate;
215 pstate_values[pstate_count].status_value = pstate;
216
217 pstate_xpss_values[pstate_count].core_freq =
218 (uint64_t)pstate_values[pstate_count].core_freq;
219 pstate_xpss_values[pstate_count].power =
220 (uint64_t)pstate_values[pstate_count].power;
221 pstate_xpss_values[pstate_count].transition_latency = 0;
222 pstate_xpss_values[pstate_count].bus_master_latency = 0;
223 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
224 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
225 pstate_count++;
226 }
227
228 return pstate_count;
229}
230
231void generate_cpu_entries(const struct device *device)
232{
233 int logical_cores;
Felix Heldc3fec862023-01-28 04:15:57 +0100234 size_t pstate_count, cpu;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800235 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
236 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
Felix Heldc3fec862023-01-28 04:15:57 +0100237 uint32_t threads_per_core;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800238 uint32_t cstate_base_address =
239 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
240
241 const acpi_addr_t perf_ctrl = {
242 .space_id = ACPI_ADDRESS_SPACE_FIXED,
243 .bit_width = 64,
244 .addrl = PS_CTL_REG,
245 };
246 const acpi_addr_t perf_sts = {
247 .space_id = ACPI_ADDRESS_SPACE_FIXED,
248 .bit_width = 64,
249 .addrl = PS_STS_REG,
250 };
251
Angel Ponsd2794ce2021-10-17 12:59:43 +0200252 const acpi_cstate_t cstate_info[] = {
Jason Glenesk79542fa2021-03-10 03:50:57 -0800253 [0] = {
254 .ctype = 1,
255 .latency = 1,
256 .power = 0,
257 .resource = {
258 .space_id = ACPI_ADDRESS_SPACE_FIXED,
259 .bit_width = 2,
260 .bit_offset = 2,
261 .addrl = 0,
262 .addrh = 0,
263 },
264 },
265 [1] = {
266 .ctype = 2,
267 .latency = 0x12,
268 .power = 0,
269 .resource = {
270 .space_id = ACPI_ADDRESS_SPACE_IO,
271 .bit_width = 8,
272 .bit_offset = 0,
273 .addrl = cstate_base_address + 1,
274 .addrh = 0,
275 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
276 },
277 },
Raul E Rangeld8956f72021-04-19 17:00:58 -0600278 [2] = {
279 .ctype = 3,
280 .latency = 350,
281 .power = 0,
282 .resource = {
283 .space_id = ACPI_ADDRESS_SPACE_IO,
284 .bit_width = 8,
285 .bit_offset = 0,
286 .addrl = cstate_base_address + 2,
287 .addrh = 0,
288 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
289 },
290 },
Jason Glenesk79542fa2021-03-10 03:50:57 -0800291 };
292
Felix Heldd4b5ad02022-01-25 04:14:05 +0100293 threads_per_core = get_threads_per_core();
Jason Glenesk79542fa2021-03-10 03:50:57 -0800294 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
295 logical_cores = get_cpu_count();
296
297 for (cpu = 0; cpu < logical_cores; cpu++) {
Felix Heldc3fec862023-01-28 04:15:57 +0100298 acpigen_write_processor_device(cpu);
Jason Glenesk79542fa2021-03-10 03:50:57 -0800299
300 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
301
302 acpigen_write_pss_object(pstate_values, pstate_count);
303
304 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
305
306 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
307 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
308 HW_ALL);
309 else
310 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
311
312 acpigen_write_PPC(0);
313
314 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
315
316 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
317 CSD_HW_ALL, 0);
318
Julian Schroeder577e1462021-07-09 16:10:08 -0500319 generate_cppc_entries(cpu);
320
Felix Heldc3fec862023-01-28 04:15:57 +0100321 acpigen_write_processor_device_end();
Jason Glenesk79542fa2021-03-10 03:50:57 -0800322 }
323
324 acpigen_write_processor_package("PPKG", 0, logical_cores);
325}