blob: 884552900ed233a5b402a6c01382ca85e71f53dc [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
Raul E Rangel12c6a582021-02-10 16:45:49 -070065 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
66 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
67 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
68 fadt->gpe0_blk = ACPI_GPE0_BLK;
69
70 fadt->pm1_evt_len = 4; /* 32 bits */
71 fadt->pm1_cnt_len = 2; /* 16 bits */
72 fadt->pm_tmr_len = 4; /* 32 bits */
73 fadt->gpe0_blk_len = 8; /* 64 bits */
74
Felix Held164c5ed2022-10-18 00:11:48 +020075 fill_fadt_extended_pm_regs(fadt);
76
Felix Held54c80e12023-02-21 17:59:42 +010077 /* p_lvl2_lat and p_lvl3_lat match what the AGESA code does, but those values are
78 overridden by the _CST packages in the processor devices. */
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;
Raul E Rangel12c6a582021-02-10 16:45:49 -070081 fadt->day_alrm = RTC_DATE_ALARM;
Raul E Rangel12c6a582021-02-10 16:45:49 -070082 fadt->century = RTC_ALT_CENTURY;
Jason Gleneskfff318f2021-03-10 02:47:05 -080083 fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
Raul E Rangel12c6a582021-02-10 16:45:49 -070084 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
85 ACPI_FADT_C1_SUPPORTED |
86 ACPI_FADT_S4_RTC_WAKE |
87 ACPI_FADT_32BIT_TIMER |
88 ACPI_FADT_PCI_EXPRESS_WAKE |
89 ACPI_FADT_PLATFORM_CLOCK |
90 ACPI_FADT_S4_RTC_VALID |
91 ACPI_FADT_REMOTE_POWER_ON;
Jason Gleneskfff318f2021-03-10 02:47:05 -080092 if (cfg->s0ix_enable)
93 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
94
95 fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
Felix Held86024952021-02-03 23:44:28 +010096}
Jason Glenesk79542fa2021-03-10 03:50:57 -080097
98static uint32_t get_pstate_core_freq(msr_t pstate_def)
99{
100 uint32_t core_freq, core_freq_mul, core_freq_div;
101 bool valid_freq_divisor;
102
103 /* Core frequency multiplier */
104 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
105
106 /* Core frequency divisor ID */
107 core_freq_div =
108 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
109
110 if (core_freq_div == 0) {
111 return 0;
112 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
113 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
114 /* Allow 1/8 integer steps for this range */
115 valid_freq_divisor = 1;
116 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
117 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
118 /* Only allow 1/4 integer steps for this range */
119 valid_freq_divisor = 1;
120 } else {
121 valid_freq_divisor = 0;
122 }
123
124 if (valid_freq_divisor) {
125 /* 25 * core_freq_mul / (core_freq_div / 8) */
126 core_freq =
127 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
128 } else {
129 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
130 core_freq_div);
131 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
132 }
133 return core_freq;
134}
135
136static uint32_t get_pstate_core_power(msr_t pstate_def)
137{
138 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
139
140 /* Core voltage ID */
141 core_vid =
142 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
143
144 /* Current value in amps */
145 current_value_amps =
146 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
147
148 /* Current divisor */
149 current_divisor =
150 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
151
152 /* Voltage */
153 if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
154 /* Voltage off for VID codes 0xF8 to 0xFF */
155 voltage_in_uvolts = 0;
156 } else {
157 voltage_in_uvolts =
158 SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
159 }
160
161 /* Power in mW */
Zheng Bao62cd5e82022-08-25 17:11:38 +0800162 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800163
164 switch (current_divisor) {
165 case 0:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800166 power_in_mw = power_in_mw / 100L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800167 break;
168 case 1:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800169 power_in_mw = power_in_mw / 1000L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800170 break;
171 case 2:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800172 power_in_mw = power_in_mw / 10000L;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800173 break;
174 case 3:
175 /* current_divisor is set to an undefined value.*/
176 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
177 power_in_mw = 0;
178 break;
179 }
180
181 return power_in_mw;
182}
183
184/*
185 * Populate structure describing enabled p-states and return count of enabled p-states.
186 */
187static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
188 struct acpi_xpss_sw_pstate *pstate_xpss_values)
189{
190 msr_t pstate_def;
191 size_t pstate_count, pstate;
192 uint32_t pstate_enable, max_pstate;
193
194 pstate_count = 0;
195 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
196
197 for (pstate = 0; pstate <= max_pstate; pstate++) {
Felix Held0a466042023-02-27 23:56:39 +0100198 pstate_def = rdmsr(PSTATE_MSR(pstate));
Jason Glenesk79542fa2021-03-10 03:50:57 -0800199
200 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
201 >> PSTATE_DEF_HI_ENABLE_SHIFT;
202 if (!pstate_enable)
203 continue;
204
205 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
206 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
207 pstate_values[pstate_count].transition_latency = 0;
208 pstate_values[pstate_count].bus_master_latency = 0;
209 pstate_values[pstate_count].control_value = pstate;
210 pstate_values[pstate_count].status_value = pstate;
211
212 pstate_xpss_values[pstate_count].core_freq =
213 (uint64_t)pstate_values[pstate_count].core_freq;
214 pstate_xpss_values[pstate_count].power =
215 (uint64_t)pstate_values[pstate_count].power;
216 pstate_xpss_values[pstate_count].transition_latency = 0;
217 pstate_xpss_values[pstate_count].bus_master_latency = 0;
218 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
219 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
220 pstate_count++;
221 }
222
223 return pstate_count;
224}
225
Felix Heldceafcae2023-03-07 00:00:15 +0100226const acpi_cstate_t cstate_cfg_table[] = {
227 [0] = {
228 .ctype = 1,
229 .latency = 1,
230 .power = 0,
231 },
232 [1] = {
233 .ctype = 2,
234 .latency = 0x12,
235 .power = 0,
236 },
237 [2] = {
238 .ctype = 3,
239 .latency = 350,
240 .power = 0,
241 },
242};
243
244const acpi_cstate_t *get_cstate_config_data(size_t *size)
245{
246 *size = ARRAY_SIZE(cstate_cfg_table);
247 return cstate_cfg_table;
248}
249
Jason Glenesk79542fa2021-03-10 03:50:57 -0800250void generate_cpu_entries(const struct device *device)
251{
252 int logical_cores;
Felix Heldceafcae2023-03-07 00:00:15 +0100253 size_t cstate_count, pstate_count, cpu;
254 acpi_cstate_t cstate_values[MAX_CSTATE_COUNT] = { {0} };
Jason Glenesk79542fa2021-03-10 03:50:57 -0800255 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
256 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
Felix Heldc3fec862023-01-28 04:15:57 +0100257 uint32_t threads_per_core;
Jason Glenesk79542fa2021-03-10 03:50:57 -0800258
259 const acpi_addr_t perf_ctrl = {
260 .space_id = ACPI_ADDRESS_SPACE_FIXED,
261 .bit_width = 64,
262 .addrl = PS_CTL_REG,
263 };
264 const acpi_addr_t perf_sts = {
265 .space_id = ACPI_ADDRESS_SPACE_FIXED,
266 .bit_width = 64,
267 .addrl = PS_STS_REG,
268 };
269
Felix Heldd4b5ad02022-01-25 04:14:05 +0100270 threads_per_core = get_threads_per_core();
Felix Heldceafcae2023-03-07 00:00:15 +0100271 cstate_count = get_cstate_info(cstate_values);
Jason Glenesk79542fa2021-03-10 03:50:57 -0800272 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
273 logical_cores = get_cpu_count();
274
275 for (cpu = 0; cpu < logical_cores; cpu++) {
Felix Heldc3fec862023-01-28 04:15:57 +0100276 acpigen_write_processor_device(cpu);
Jason Glenesk79542fa2021-03-10 03:50:57 -0800277
278 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
279
280 acpigen_write_pss_object(pstate_values, pstate_count);
281
282 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
283
284 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
285 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
286 HW_ALL);
287 else
288 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
289
290 acpigen_write_PPC(0);
291
Felix Heldceafcae2023-03-07 00:00:15 +0100292 acpigen_write_CST_package(cstate_values, cstate_count);
Jason Glenesk79542fa2021-03-10 03:50:57 -0800293
294 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
295 CSD_HW_ALL, 0);
296
Julian Schroeder577e1462021-07-09 16:10:08 -0500297 generate_cppc_entries(cpu);
298
Felix Heldc3fec862023-01-28 04:15:57 +0100299 acpigen_write_processor_device_end();
Jason Glenesk79542fa2021-03-10 03:50:57 -0800300 }
301
302 acpigen_write_processor_package("PPKG", 0, logical_cores);
303}