blob: 945dc5419d8570d7e6051b876714640714acb734 [file] [log] [blame]
Martin Roth1a3de8e2022-10-06 15:57:21 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
3/* TODO: Update for Morgana */
4/* 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 */
30 current = acpi_create_madt_lapics(current);
31
32 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
33 FCH_IOAPIC_ID, IO_APIC_ADDR, 0);
34
35 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
36 GNB_IOAPIC_ID, GNB_IO_APIC_ADDR, IO_APIC_INTERRUPTS);
37
38 /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
39 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
40 MP_BUS_ISA, 0, 2,
41 MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT);
42 /* SCI IRQ type override */
43 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
44 MP_BUS_ISA, ACPI_SCI_IRQ, ACPI_SCI_IRQ,
45 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
46 current = acpi_fill_madt_irqoverride(current);
47
48 /* create all subtables for processors */
49 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
50 ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS,
51 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
52 1 /* 1: LINT1 connect to NMI */);
53
54 return current;
55}
56
57/*
58 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
59 * in the ACPI 3.0b specification.
60 */
61void acpi_fill_fadt(acpi_fadt_t *fadt)
62{
63 const struct soc_amd_morgana_config *cfg = config_of_soc();
64
65 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
66
67 fadt->sci_int = ACPI_SCI_IRQ;
68
69 if (permanent_smi_handler()) {
70 fadt->smi_cmd = APM_CNT;
71 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
72 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
73 }
74
75 fadt->pstate_cnt = 0;
76
77 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
78 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
79 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
80 fadt->gpe0_blk = ACPI_GPE0_BLK;
81
82 fadt->pm1_evt_len = 4; /* 32 bits */
83 fadt->pm1_cnt_len = 2; /* 16 bits */
84 fadt->pm_tmr_len = 4; /* 32 bits */
85 fadt->gpe0_blk_len = 8; /* 64 bits */
86
Felix Held164c5ed2022-10-18 00:11:48 +020087 fill_fadt_extended_pm_regs(fadt);
88
Martin Roth1a3de8e2022-10-06 15:57:21 -060089 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
90 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
91 fadt->duty_offset = 0; /* Not supported */
92 fadt->duty_width = 0; /* Not supported */
93 fadt->day_alrm = RTC_DATE_ALARM;
94 fadt->mon_alrm = 0;
95 fadt->century = RTC_ALT_CENTURY;
96 fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
97 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
98 ACPI_FADT_C1_SUPPORTED |
99 ACPI_FADT_S4_RTC_WAKE |
100 ACPI_FADT_32BIT_TIMER |
101 ACPI_FADT_PCI_EXPRESS_WAKE |
102 ACPI_FADT_PLATFORM_CLOCK |
103 ACPI_FADT_S4_RTC_VALID |
104 ACPI_FADT_REMOTE_POWER_ON;
105 if (cfg->s0ix_enable)
106 fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0;
107
108 fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
Martin Roth1a3de8e2022-10-06 15:57:21 -0600109}
110
111static uint32_t get_pstate_core_freq(msr_t pstate_def)
112{
113 uint32_t core_freq, core_freq_mul, core_freq_div;
114 bool valid_freq_divisor;
115
116 /* Core frequency multiplier */
117 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
118
119 /* Core frequency divisor ID */
120 core_freq_div =
121 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
122
123 if (core_freq_div == 0) {
124 return 0;
125 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
126 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
127 /* Allow 1/8 integer steps for this range */
128 valid_freq_divisor = 1;
129 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
130 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
131 /* Only allow 1/4 integer steps for this range */
132 valid_freq_divisor = 1;
133 } else {
134 valid_freq_divisor = 0;
135 }
136
137 if (valid_freq_divisor) {
138 /* 25 * core_freq_mul / (core_freq_div / 8) */
139 core_freq =
140 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
141 } else {
142 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
143 core_freq_div);
144 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
145 }
146 return core_freq;
147}
148
149static uint32_t get_pstate_core_power(msr_t pstate_def)
150{
151 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
152
153 /* Core voltage ID */
154 core_vid =
155 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
156
157 /* Current value in amps */
158 current_value_amps =
159 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
160
161 /* Current divisor */
162 current_divisor =
163 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
164
165 /* Voltage */
166 if (core_vid == 0x00) {
167 /* Voltage off for VID code 0x00 */
168 voltage_in_uvolts = 0;
169 } else {
170 voltage_in_uvolts =
171 SERIAL_VID_BASE_MICROVOLTS + (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
172 }
173
174 /* Power in mW */
175 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
176
177 switch (current_divisor) {
178 case 0:
179 power_in_mw = power_in_mw / 100L;
180 break;
181 case 1:
182 power_in_mw = power_in_mw / 1000L;
183 break;
184 case 2:
185 power_in_mw = power_in_mw / 10000L;
186 break;
187 case 3:
188 /* current_divisor is set to an undefined value.*/
189 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
190 power_in_mw = 0;
191 break;
192 }
193
194 return power_in_mw;
195}
196
197/*
198 * Populate structure describing enabled p-states and return count of enabled p-states.
199 */
200static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
201 struct acpi_xpss_sw_pstate *pstate_xpss_values)
202{
203 msr_t pstate_def;
204 size_t pstate_count, pstate;
205 uint32_t pstate_enable, max_pstate;
206
207 pstate_count = 0;
208 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
209
210 for (pstate = 0; pstate <= max_pstate; pstate++) {
211 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
212
213 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
214 >> PSTATE_DEF_HI_ENABLE_SHIFT;
215 if (!pstate_enable)
216 continue;
217
218 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
219 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
220 pstate_values[pstate_count].transition_latency = 0;
221 pstate_values[pstate_count].bus_master_latency = 0;
222 pstate_values[pstate_count].control_value = pstate;
223 pstate_values[pstate_count].status_value = pstate;
224
225 pstate_xpss_values[pstate_count].core_freq =
226 (uint64_t)pstate_values[pstate_count].core_freq;
227 pstate_xpss_values[pstate_count].power =
228 (uint64_t)pstate_values[pstate_count].power;
229 pstate_xpss_values[pstate_count].transition_latency = 0;
230 pstate_xpss_values[pstate_count].bus_master_latency = 0;
231 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
232 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
233 pstate_count++;
234 }
235
236 return pstate_count;
237}
238
239void generate_cpu_entries(const struct device *device)
240{
241 int logical_cores;
242 size_t pstate_count, cpu, proc_blk_len;
243 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
244 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
245 uint32_t threads_per_core, proc_blk_addr;
246 uint32_t cstate_base_address =
247 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
248
249 const acpi_addr_t perf_ctrl = {
250 .space_id = ACPI_ADDRESS_SPACE_FIXED,
251 .bit_width = 64,
252 .addrl = PS_CTL_REG,
253 };
254 const acpi_addr_t perf_sts = {
255 .space_id = ACPI_ADDRESS_SPACE_FIXED,
256 .bit_width = 64,
257 .addrl = PS_STS_REG,
258 };
259
260 const acpi_cstate_t cstate_info[] = {
261 [0] = {
262 .ctype = 1,
263 .latency = 1,
264 .power = 0,
265 .resource = {
266 .space_id = ACPI_ADDRESS_SPACE_FIXED,
267 .bit_width = 2,
268 .bit_offset = 2,
269 .addrl = 0,
270 .addrh = 0,
271 },
272 },
273 [1] = {
274 .ctype = 2,
275 .latency = 0x12,
276 .power = 0,
277 .resource = {
278 .space_id = ACPI_ADDRESS_SPACE_IO,
279 .bit_width = 8,
280 .bit_offset = 0,
281 .addrl = cstate_base_address + 1,
282 .addrh = 0,
283 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
284 },
285 },
286 [2] = {
287 .ctype = 3,
288 .latency = 350,
289 .power = 0,
290 .resource = {
291 .space_id = ACPI_ADDRESS_SPACE_IO,
292 .bit_width = 8,
293 .bit_offset = 0,
294 .addrl = cstate_base_address + 2,
295 .addrh = 0,
296 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
297 },
298 },
299 };
300
301 threads_per_core = get_threads_per_core();
302 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
303 logical_cores = get_cpu_count();
304
305 for (cpu = 0; cpu < logical_cores; cpu++) {
306
307 if (cpu == 0) {
308 /* BSP values for \_SB.Pxxx */
309 proc_blk_len = 6;
310 proc_blk_addr = ACPI_GPE0_BLK;
311 } else {
312 /* AP values for \_SB.Pxxx */
313 proc_blk_addr = 0;
314 proc_blk_len = 0;
315 }
316
317 acpigen_write_processor(cpu, proc_blk_addr, proc_blk_len);
318
319 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
320
321 acpigen_write_pss_object(pstate_values, pstate_count);
322
323 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
324
325 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
326 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
327 HW_ALL);
328 else
329 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
330
331 acpigen_write_PPC(0);
332
333 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
334
335 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
336 CSD_HW_ALL, 0);
337
338 generate_cppc_entries(cpu);
339
340 acpigen_pop_len();
341 }
342
343 acpigen_write_processor_package("PPKG", 0, logical_cores);
344}