blob: 06b7f041371f195af3a7e143b4bfcc0cd48668dc [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
Martin Roth1a3de8e2022-10-06 15:57:21 -060082 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
83 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
84 fadt->duty_offset = 0; /* Not supported */
85 fadt->duty_width = 0; /* Not supported */
86 fadt->day_alrm = RTC_DATE_ALARM;
87 fadt->mon_alrm = 0;
88 fadt->century = RTC_ALT_CENTURY;
89 fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
90 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
91 ACPI_FADT_C1_SUPPORTED |
92 ACPI_FADT_S4_RTC_WAKE |
93 ACPI_FADT_32BIT_TIMER |
94 ACPI_FADT_PCI_EXPRESS_WAKE |
95 ACPI_FADT_PLATFORM_CLOCK |
96 ACPI_FADT_S4_RTC_VALID |
97 ACPI_FADT_REMOTE_POWER_ON;
98 if (cfg->s0ix_enable)
99 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
100
101 fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
Martin Roth1a3de8e2022-10-06 15:57:21 -0600102}
103
104static uint32_t get_pstate_core_freq(msr_t pstate_def)
105{
106 uint32_t core_freq, core_freq_mul, core_freq_div;
107 bool valid_freq_divisor;
108
109 /* Core frequency multiplier */
110 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
111
112 /* Core frequency divisor ID */
113 core_freq_div =
114 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
115
116 if (core_freq_div == 0) {
117 return 0;
118 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
119 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
120 /* Allow 1/8 integer steps for this range */
121 valid_freq_divisor = 1;
122 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
123 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
124 /* Only allow 1/4 integer steps for this range */
125 valid_freq_divisor = 1;
126 } else {
127 valid_freq_divisor = 0;
128 }
129
130 if (valid_freq_divisor) {
131 /* 25 * core_freq_mul / (core_freq_div / 8) */
132 core_freq =
133 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
134 } else {
135 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
136 core_freq_div);
137 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
138 }
139 return core_freq;
140}
141
142static uint32_t get_pstate_core_power(msr_t pstate_def)
143{
144 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
145
146 /* Core voltage ID */
147 core_vid =
148 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
149
150 /* Current value in amps */
151 current_value_amps =
152 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
153
154 /* Current divisor */
155 current_divisor =
156 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
157
158 /* Voltage */
159 if (core_vid == 0x00) {
160 /* Voltage off for VID code 0x00 */
161 voltage_in_uvolts = 0;
162 } else {
163 voltage_in_uvolts =
164 SERIAL_VID_BASE_MICROVOLTS + (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
165 }
166
167 /* Power in mW */
168 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
169
170 switch (current_divisor) {
171 case 0:
172 power_in_mw = power_in_mw / 100L;
173 break;
174 case 1:
175 power_in_mw = power_in_mw / 1000L;
176 break;
177 case 2:
178 power_in_mw = power_in_mw / 10000L;
179 break;
180 case 3:
181 /* current_divisor is set to an undefined value.*/
182 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
183 power_in_mw = 0;
184 break;
185 }
186
187 return power_in_mw;
188}
189
190/*
191 * Populate structure describing enabled p-states and return count of enabled p-states.
192 */
193static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
194 struct acpi_xpss_sw_pstate *pstate_xpss_values)
195{
196 msr_t pstate_def;
197 size_t pstate_count, pstate;
198 uint32_t pstate_enable, max_pstate;
199
200 pstate_count = 0;
201 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
202
203 for (pstate = 0; pstate <= max_pstate; pstate++) {
204 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
205
206 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
207 >> PSTATE_DEF_HI_ENABLE_SHIFT;
208 if (!pstate_enable)
209 continue;
210
211 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
212 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
213 pstate_values[pstate_count].transition_latency = 0;
214 pstate_values[pstate_count].bus_master_latency = 0;
215 pstate_values[pstate_count].control_value = pstate;
216 pstate_values[pstate_count].status_value = pstate;
217
218 pstate_xpss_values[pstate_count].core_freq =
219 (uint64_t)pstate_values[pstate_count].core_freq;
220 pstate_xpss_values[pstate_count].power =
221 (uint64_t)pstate_values[pstate_count].power;
222 pstate_xpss_values[pstate_count].transition_latency = 0;
223 pstate_xpss_values[pstate_count].bus_master_latency = 0;
224 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
225 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
226 pstate_count++;
227 }
228
229 return pstate_count;
230}
231
232void generate_cpu_entries(const struct device *device)
233{
234 int logical_cores;
235 size_t pstate_count, cpu, proc_blk_len;
236 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
237 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
238 uint32_t threads_per_core, proc_blk_addr;
239 uint32_t cstate_base_address =
240 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
241
242 const acpi_addr_t perf_ctrl = {
243 .space_id = ACPI_ADDRESS_SPACE_FIXED,
244 .bit_width = 64,
245 .addrl = PS_CTL_REG,
246 };
247 const acpi_addr_t perf_sts = {
248 .space_id = ACPI_ADDRESS_SPACE_FIXED,
249 .bit_width = 64,
250 .addrl = PS_STS_REG,
251 };
252
253 const acpi_cstate_t cstate_info[] = {
254 [0] = {
255 .ctype = 1,
256 .latency = 1,
257 .power = 0,
258 .resource = {
259 .space_id = ACPI_ADDRESS_SPACE_FIXED,
260 .bit_width = 2,
261 .bit_offset = 2,
262 .addrl = 0,
263 .addrh = 0,
264 },
265 },
266 [1] = {
267 .ctype = 2,
268 .latency = 0x12,
269 .power = 0,
270 .resource = {
271 .space_id = ACPI_ADDRESS_SPACE_IO,
272 .bit_width = 8,
273 .bit_offset = 0,
274 .addrl = cstate_base_address + 1,
275 .addrh = 0,
276 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
277 },
278 },
279 [2] = {
280 .ctype = 3,
281 .latency = 350,
282 .power = 0,
283 .resource = {
284 .space_id = ACPI_ADDRESS_SPACE_IO,
285 .bit_width = 8,
286 .bit_offset = 0,
287 .addrl = cstate_base_address + 2,
288 .addrh = 0,
289 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
290 },
291 },
292 };
293
294 threads_per_core = get_threads_per_core();
295 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
296 logical_cores = get_cpu_count();
297
298 for (cpu = 0; cpu < logical_cores; cpu++) {
299
300 if (cpu == 0) {
301 /* BSP values for \_SB.Pxxx */
302 proc_blk_len = 6;
303 proc_blk_addr = ACPI_GPE0_BLK;
304 } else {
305 /* AP values for \_SB.Pxxx */
306 proc_blk_addr = 0;
307 proc_blk_len = 0;
308 }
309
310 acpigen_write_processor(cpu, proc_blk_addr, proc_blk_len);
311
312 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
313
314 acpigen_write_pss_object(pstate_values, pstate_count);
315
316 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
317
318 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
319 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
320 HW_ALL);
321 else
322 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
323
324 acpigen_write_PPC(0);
325
326 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
327
328 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
329 CSD_HW_ALL, 0);
330
331 generate_cppc_entries(cpu);
332
333 acpigen_pop_len();
334 }
335
336 acpigen_write_processor_package("PPKG", 0, logical_cores);
337}