blob: edf20c9b6f68bd256c2bb35db16bfe2482f0d832 [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
Raul E Rangel12c6a582021-02-10 16:45:49 -070079 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
80 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
81 fadt->duty_offset = 0; /* Not supported */
82 fadt->duty_width = 0; /* Not supported */
83 fadt->day_alrm = RTC_DATE_ALARM;
84 fadt->mon_alrm = 0;
85 fadt->century = RTC_ALT_CENTURY;
Jason Gleneskfff318f2021-03-10 02:47:05 -080086 fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
Raul E Rangel12c6a582021-02-10 16:45:49 -070087 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
88 ACPI_FADT_C1_SUPPORTED |
89 ACPI_FADT_S4_RTC_WAKE |
90 ACPI_FADT_32BIT_TIMER |
91 ACPI_FADT_PCI_EXPRESS_WAKE |
92 ACPI_FADT_PLATFORM_CLOCK |
93 ACPI_FADT_S4_RTC_VALID |
94 ACPI_FADT_REMOTE_POWER_ON;
Jason Gleneskfff318f2021-03-10 02:47:05 -080095 if (cfg->s0ix_enable)
96 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
97
98 fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
Felix Held86024952021-02-03 23:44:28 +010099}
Jason Glenesk79542fa2021-03-10 03:50:57 -0800100
101static uint32_t get_pstate_core_freq(msr_t pstate_def)
102{
103 uint32_t core_freq, core_freq_mul, core_freq_div;
104 bool valid_freq_divisor;
105
106 /* Core frequency multiplier */
107 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
108
109 /* Core frequency divisor ID */
110 core_freq_div =
111 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
112
113 if (core_freq_div == 0) {
114 return 0;
115 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
116 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
117 /* Allow 1/8 integer steps for this range */
118 valid_freq_divisor = 1;
119 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
120 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
121 /* Only allow 1/4 integer steps for this range */
122 valid_freq_divisor = 1;
123 } else {
124 valid_freq_divisor = 0;
125 }
126
127 if (valid_freq_divisor) {
128 /* 25 * core_freq_mul / (core_freq_div / 8) */
129 core_freq =
130 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
131 } else {
132 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
133 core_freq_div);
134 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
135 }
136 return core_freq;
137}
138
139static uint32_t get_pstate_core_power(msr_t pstate_def)
140{
141 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
142
143 /* Core voltage ID */
144 core_vid =
145 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
146
147 /* Current value in amps */
148 current_value_amps =
149 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
150
151 /* Current divisor */
152 current_divisor =
153 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
154
155 /* Voltage */
156 if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
157 /* Voltage off for VID codes 0xF8 to 0xFF */
158 voltage_in_uvolts = 0;
159 } else {
160 voltage_in_uvolts =
161 SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
162 }
163
164 /* Power in mW */
Zheng Bao62cd5e82022-08-25 17:11:38 +0800165 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800166
167 switch (current_divisor) {
168 case 0:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800169 power_in_mw = power_in_mw / 100L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800170 break;
171 case 1:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800172 power_in_mw = power_in_mw / 1000L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800173 break;
174 case 2:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800175 power_in_mw = power_in_mw / 10000L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800176 break;
177 case 3:
178 /* current_divisor is set to an undefined value.*/
179 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
180 power_in_mw = 0;
181 break;
182 }
183
184 return power_in_mw;
185}
186
187/*
188 * Populate structure describing enabled p-states and return count of enabled p-states.
189 */
190static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
191 struct acpi_xpss_sw_pstate *pstate_xpss_values)
192{
193 msr_t pstate_def;
194 size_t pstate_count, pstate;
195 uint32_t pstate_enable, max_pstate;
196
197 pstate_count = 0;
198 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
199
200 for (pstate = 0; pstate <= max_pstate; pstate++) {
201 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
202
203 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
204 >> PSTATE_DEF_HI_ENABLE_SHIFT;
205 if (!pstate_enable)
206 continue;
207
208 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
209 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
210 pstate_values[pstate_count].transition_latency = 0;
211 pstate_values[pstate_count].bus_master_latency = 0;
212 pstate_values[pstate_count].control_value = pstate;
213 pstate_values[pstate_count].status_value = pstate;
214
215 pstate_xpss_values[pstate_count].core_freq =
216 (uint64_t)pstate_values[pstate_count].core_freq;
217 pstate_xpss_values[pstate_count].power =
218 (uint64_t)pstate_values[pstate_count].power;
219 pstate_xpss_values[pstate_count].transition_latency = 0;
220 pstate_xpss_values[pstate_count].bus_master_latency = 0;
221 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
222 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
223 pstate_count++;
224 }
225
226 return pstate_count;
227}
228
229void generate_cpu_entries(const struct device *device)
230{
231 int logical_cores;
Felix Heldc3fec862023-01-28 04:15:57 +0100232 size_t pstate_count, cpu;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800233 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
234 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
Felix Heldc3fec862023-01-28 04:15:57 +0100235 uint32_t threads_per_core;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800236 uint32_t cstate_base_address =
237 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
238
239 const acpi_addr_t perf_ctrl = {
240 .space_id = ACPI_ADDRESS_SPACE_FIXED,
241 .bit_width = 64,
242 .addrl = PS_CTL_REG,
243 };
244 const acpi_addr_t perf_sts = {
245 .space_id = ACPI_ADDRESS_SPACE_FIXED,
246 .bit_width = 64,
247 .addrl = PS_STS_REG,
248 };
249
Angel Ponsd2794ce2021-10-17 12:59:43 +0200250 const acpi_cstate_t cstate_info[] = {
Jason Glenesk79542fa2021-03-10 03:50:57 -0800251 [0] = {
252 .ctype = 1,
253 .latency = 1,
254 .power = 0,
255 .resource = {
256 .space_id = ACPI_ADDRESS_SPACE_FIXED,
257 .bit_width = 2,
258 .bit_offset = 2,
259 .addrl = 0,
260 .addrh = 0,
261 },
262 },
263 [1] = {
264 .ctype = 2,
265 .latency = 0x12,
266 .power = 0,
267 .resource = {
268 .space_id = ACPI_ADDRESS_SPACE_IO,
269 .bit_width = 8,
270 .bit_offset = 0,
271 .addrl = cstate_base_address + 1,
272 .addrh = 0,
273 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
274 },
275 },
Raul E Rangeld8956f72021-04-19 17:00:58 -0600276 [2] = {
277 .ctype = 3,
278 .latency = 350,
279 .power = 0,
280 .resource = {
281 .space_id = ACPI_ADDRESS_SPACE_IO,
282 .bit_width = 8,
283 .bit_offset = 0,
284 .addrl = cstate_base_address + 2,
285 .addrh = 0,
286 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
287 },
288 },
Jason Glenesk79542fa2021-03-10 03:50:57 -0800289 };
290
Felix Heldd4b5ad02022-01-25 04:14:05 +0100291 threads_per_core = get_threads_per_core();
Jason Glenesk79542fa2021-03-10 03:50:57 -0800292 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
293 logical_cores = get_cpu_count();
294
295 for (cpu = 0; cpu < logical_cores; cpu++) {
Felix Heldc3fec862023-01-28 04:15:57 +0100296 acpigen_write_processor_device(cpu);
Jason Glenesk79542fa2021-03-10 03:50:57 -0800297
298 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
299
300 acpigen_write_pss_object(pstate_values, pstate_count);
301
302 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
303
304 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
305 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
306 HW_ALL);
307 else
308 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
309
310 acpigen_write_PPC(0);
311
312 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
313
314 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
315 CSD_HW_ALL, 0);
316
Julian Schroeder577e1462021-07-09 16:10:08 -0500317 generate_cppc_entries(cpu);
318
Felix Heldc3fec862023-01-28 04:15:57 +0100319 acpigen_write_processor_device_end();
Jason Glenesk79542fa2021-03-10 03:50:57 -0800320 }
321
322 acpigen_write_processor_package("PPKG", 0, logical_cores);
323}