blob: 568a5ddce780577ddae6c396577c8aee736c50bf [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Roth5c354b92019-04-22 14:55:16 -06002
3/*
4 * ACPI - create the Fixed ACPI Description Tables (FADT)
5 */
6
7#include <string.h>
8#include <console/console.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07009#include <acpi/acpi.h>
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +030010#include <acpi/acpi_gnvs.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070011#include <acpi/acpigen.h>
Martin Roth5c354b92019-04-22 14:55:16 -060012#include <device/pci_ops.h>
13#include <arch/ioapic.h>
Raul E Rangel93b62e62020-01-31 12:53:45 -070014#include <arch/smp/mpspec.h>
Jason Glenesk498015d2020-12-10 03:28:38 -080015#include <cpu/amd/cpuid.h>
Jason Gleneskbc521432020-09-14 05:22:47 -070016#include <cpu/amd/msr.h>
Martin Roth5c354b92019-04-22 14:55:16 -060017#include <cpu/x86/smm.h>
Martin Roth5c354b92019-04-22 14:55:16 -060018#include <device/device.h>
19#include <device/pci.h>
20#include <amdblocks/acpimmio.h>
21#include <amdblocks/acpi.h>
Raul E Rangel1c88b102021-02-11 10:35:32 -070022#include <amdblocks/chip.h>
Felix Helddd2f3fa2021-02-08 22:23:54 +010023#include <amdblocks/cpu.h>
Felix Held604ffa62021-02-12 00:43:20 +010024#include <amdblocks/ioapic.h>
Martin Roth5c354b92019-04-22 14:55:16 -060025#include <soc/acpi.h>
26#include <soc/pci_devs.h>
Jason Gleneskbc521432020-09-14 05:22:47 -070027#include <soc/msr.h>
Martin Roth5c354b92019-04-22 14:55:16 -060028#include <soc/southbridge.h>
Martin Roth5c354b92019-04-22 14:55:16 -060029#include <soc/nvs.h>
30#include <soc/gpio.h>
31#include <version.h>
Raul E Rangel93b62e62020-01-31 12:53:45 -070032#include "chip.h"
Martin Roth5c354b92019-04-22 14:55:16 -060033
34unsigned long acpi_fill_madt(unsigned long current)
35{
36 /* create all subtables for processors */
37 current = acpi_create_madt_lapics(current);
38
Martin Roth5c354b92019-04-22 14:55:16 -060039 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
Felix Held604ffa62021-02-12 00:43:20 +010040 FCH_IOAPIC_ID, IO_APIC_ADDR, 0);
Martin Roth5c354b92019-04-22 14:55:16 -060041
Jason Gleneskf459a402020-09-02 16:49:10 -070042 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
Felix Held604ffa62021-02-12 00:43:20 +010043 GNB_IOAPIC_ID, GNB_IO_APIC_ADDR, IO_APIC_INTERRUPTS);
Jason Gleneskf459a402020-09-02 16:49:10 -070044
Martin Roth5c354b92019-04-22 14:55:16 -060045 /* 0: mean bus 0--->ISA */
46 /* 0: PIC 0 */
47 /* 2: APIC 2 */
48 /* 5 mean: 0101 --> Edge-triggered, Active high */
49 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
50 current, 0, 0, 2, 0);
Raul E Rangel93b62e62020-01-31 12:53:45 -070051 current += acpi_create_madt_irqoverride(
52 (acpi_madt_irqoverride_t *)current, 0, 9, 9,
53 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
54
Raul E Rangelffab5e62021-02-11 11:07:11 -070055 current = acpi_fill_madt_irqoverride(current);
Martin Roth5c354b92019-04-22 14:55:16 -060056
57 /* create all subtables for processors */
58 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current,
59 0xff, 5, 1);
60 /* 1: LINT1 connect to NMI */
61
62 return current;
63}
64
65/*
66 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
67 * in the ACPI 3.0b specification.
68 */
Kyösti Mälkki61ef71b2020-05-30 18:54:39 +030069void acpi_fill_fadt(acpi_fadt_t *fadt)
Martin Roth5c354b92019-04-22 14:55:16 -060070{
Raul E Rangel1c88b102021-02-11 10:35:32 -070071 const struct soc_amd_common_config *cfg = soc_get_common_config();
Martin Rotheca8faa2019-12-01 16:49:19 -070072
Felix Held757d6452021-02-04 21:31:49 +010073 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
Martin Roth5c354b92019-04-22 14:55:16 -060074
Martin Roth5c354b92019-04-22 14:55:16 -060075 fadt->sci_int = 9; /* IRQ 09 - ACPI SCI */
76
Kyösti Mälkki0a9e72e2019-08-11 01:22:28 +030077 if (permanent_smi_handler()) {
Martin Roth5c354b92019-04-22 14:55:16 -060078 fadt->smi_cmd = APM_CNT;
79 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
80 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
Martin Roth5c354b92019-04-22 14:55:16 -060081 }
82
83 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060084 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060085 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
86 fadt->gpe0_blk = ACPI_GPE0_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060087
88 fadt->pm1_evt_len = 4; /* 32 bits */
89 fadt->pm1_cnt_len = 2; /* 16 bits */
Martin Roth5c354b92019-04-22 14:55:16 -060090 fadt->pm_tmr_len = 4; /* 32 bits */
91 fadt->gpe0_blk_len = 8; /* 64 bits */
Martin Roth5c354b92019-04-22 14:55:16 -060092
93 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
94 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
Martin Roth5c354b92019-04-22 14:55:16 -060095 fadt->duty_offset = 1; /* CLK_VAL bits 3:1 */
96 fadt->duty_width = 3; /* CLK_VAL bits 3:1 */
Raul E Rangel041fcf52020-08-12 12:13:35 -060097 fadt->day_alrm = 0x0d;
98 fadt->mon_alrm = 0;
99 fadt->century = 0x32;
Martin Rotheca8faa2019-12-01 16:49:19 -0700100 fadt->iapc_boot_arch = cfg->fadt_boot_arch; /* legacy free default */
Martin Roth5c354b92019-04-22 14:55:16 -0600101 fadt->res2 = 0; /* reserved, MUST be 0 ACPI 3.0 */
Martin Rotheca8faa2019-12-01 16:49:19 -0700102 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
103 ACPI_FADT_C1_SUPPORTED |
104 ACPI_FADT_S4_RTC_WAKE |
105 ACPI_FADT_32BIT_TIMER |
106 ACPI_FADT_PCI_EXPRESS_WAKE |
107 ACPI_FADT_PLATFORM_CLOCK |
108 ACPI_FADT_S4_RTC_VALID |
109 ACPI_FADT_REMOTE_POWER_ON;
110 fadt->flags |= cfg->fadt_flags; /* additional board-specific flags */
Martin Roth5c354b92019-04-22 14:55:16 -0600111
Martin Roth5c354b92019-04-22 14:55:16 -0600112 fadt->ARM_boot_arch = 0; /* MUST be 0 ACPI 3.0 */
113 fadt->FADT_MinorVersion = 0; /* MUST be 0 ACPI 3.0 */
114
115 fadt->x_firmware_ctl_l = 0; /* set to 0 if firmware_ctrl is used */
116 fadt->x_firmware_ctl_h = 0;
Martin Roth5c354b92019-04-22 14:55:16 -0600117
118 fadt->x_pm1a_evt_blk.space_id = ACPI_ADDRESS_SPACE_IO;
119 fadt->x_pm1a_evt_blk.bit_width = 32;
120 fadt->x_pm1a_evt_blk.bit_offset = 0;
121 fadt->x_pm1a_evt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
122 fadt->x_pm1a_evt_blk.addrl = ACPI_PM_EVT_BLK;
123 fadt->x_pm1a_evt_blk.addrh = 0x0;
124
Martin Roth5c354b92019-04-22 14:55:16 -0600125 fadt->x_pm1a_cnt_blk.space_id = ACPI_ADDRESS_SPACE_IO;
126 fadt->x_pm1a_cnt_blk.bit_width = 16;
127 fadt->x_pm1a_cnt_blk.bit_offset = 0;
Patrick Rudolphc02bda02020-02-28 10:19:41 +0100128 fadt->x_pm1a_cnt_blk.access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
Martin Roth5c354b92019-04-22 14:55:16 -0600129 fadt->x_pm1a_cnt_blk.addrl = ACPI_PM1_CNT_BLK;
130 fadt->x_pm1a_cnt_blk.addrh = 0x0;
131
Martin Roth5c354b92019-04-22 14:55:16 -0600132 fadt->x_pm_tmr_blk.space_id = ACPI_ADDRESS_SPACE_IO;
133 fadt->x_pm_tmr_blk.bit_width = 32;
134 fadt->x_pm_tmr_blk.bit_offset = 0;
135 fadt->x_pm_tmr_blk.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
136 fadt->x_pm_tmr_blk.addrl = ACPI_PM_TMR_BLK;
137 fadt->x_pm_tmr_blk.addrh = 0x0;
138
Martin Roth5c354b92019-04-22 14:55:16 -0600139 fadt->x_gpe0_blk.space_id = ACPI_ADDRESS_SPACE_IO;
140 fadt->x_gpe0_blk.bit_width = 64; /* EventStatus + Event Enable */
141 fadt->x_gpe0_blk.bit_offset = 0;
Angel Ponsa23aff32020-06-21 20:47:54 +0200142 fadt->x_gpe0_blk.access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
Martin Roth5c354b92019-04-22 14:55:16 -0600143 fadt->x_gpe0_blk.addrl = ACPI_GPE0_BLK;
144 fadt->x_gpe0_blk.addrh = 0x0;
Martin Roth5c354b92019-04-22 14:55:16 -0600145}
146
Jason Gleneskbc521432020-09-14 05:22:47 -0700147static uint32_t get_pstate_core_freq(msr_t pstate_def)
148{
149 uint32_t core_freq, core_freq_mul, core_freq_div;
150 bool valid_freq_divisor;
151
152 /* Core frequency multiplier */
153 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
154
155 /* Core frequency divisor ID */
156 core_freq_div =
157 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
158
159 if (core_freq_div == 0) {
160 return 0;
161 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
162 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
163 /* Allow 1/8 integer steps for this range */
164 valid_freq_divisor = 1;
165 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
166 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
167 /* Only allow 1/4 integer steps for this range */
168 valid_freq_divisor = 1;
169 } else {
170 valid_freq_divisor = 0;
171 }
172
173 if (valid_freq_divisor) {
174 /* 25 * core_freq_mul / (core_freq_div / 8) */
175 core_freq =
176 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
177 } else {
178 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
179 core_freq_div);
180 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
181 }
182 return core_freq;
183}
184
185static uint32_t get_pstate_core_power(msr_t pstate_def)
186{
187 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
188
189 /* Core voltage ID */
190 core_vid =
191 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
192
193 /* Current value in amps */
194 current_value_amps =
195 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
196
197 /* Current divisor */
198 current_divisor =
199 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
200
201 /* Voltage */
202 if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
203 /* Voltage off for VID codes 0xF8 to 0xFF */
204 voltage_in_uvolts = 0;
205 } else {
206 voltage_in_uvolts =
207 SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
208 }
209
210 /* Power in mW */
211 power_in_mw = (voltage_in_uvolts) / 1000 * current_value_amps;
212
213 switch (current_divisor) {
214 case 0:
215 break;
216 case 1:
217 power_in_mw = power_in_mw / 10L;
218 break;
219 case 2:
220 power_in_mw = power_in_mw / 100L;
221 break;
222 case 3:
223 /* current_divisor is set to an undefined value.*/
224 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
225 power_in_mw = 0;
226 break;
227 }
228
229 return power_in_mw;
230}
231
232/*
233 * Populate structure describing enabled p-states and return count of enabled p-states.
234 */
235static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
236 struct acpi_xpss_sw_pstate *pstate_xpss_values)
237{
238 msr_t pstate_def;
239 size_t pstate_count, pstate;
240 uint32_t pstate_enable, max_pstate;
241
242 pstate_count = 0;
243 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
244
245 for (pstate = 0; pstate <= max_pstate; pstate++) {
246 pstate_def = rdmsr(PSTATE_0_MSR + pstate);
247
248 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
249 >> PSTATE_DEF_HI_ENABLE_SHIFT;
250 if (!pstate_enable)
251 continue;
252
253 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
254 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
255 pstate_values[pstate_count].transition_latency = 0;
256 pstate_values[pstate_count].bus_master_latency = 0;
257 pstate_values[pstate_count].control_value = pstate;
258 pstate_values[pstate_count].status_value = pstate;
259
260 pstate_xpss_values[pstate_count].core_freq =
261 (uint64_t)pstate_values[pstate_count].core_freq;
262 pstate_xpss_values[pstate_count].power =
263 (uint64_t)pstate_values[pstate_count].power;
264 pstate_xpss_values[pstate_count].transition_latency = 0;
265 pstate_xpss_values[pstate_count].bus_master_latency = 0;
266 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
267 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
268 pstate_count++;
269 }
270
271 return pstate_count;
272}
273
Furquan Shaikh7536a392020-04-24 21:59:21 -0700274void generate_cpu_entries(const struct device *device)
Martin Roth5c354b92019-04-22 14:55:16 -0600275{
Jason Gleneskbc521432020-09-14 05:22:47 -0700276 int logical_cores;
277 size_t pstate_count, cpu, proc_blk_len;
278 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
279 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
280 uint32_t threads_per_core, proc_blk_addr;
281 uint32_t cstate_base_address =
282 rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK;
Martin Roth5c354b92019-04-22 14:55:16 -0600283
Jason Gleneskbc521432020-09-14 05:22:47 -0700284 const acpi_addr_t perf_ctrl = {
285 .space_id = ACPI_ADDRESS_SPACE_FIXED,
286 .bit_width = 64,
287 .addrl = PS_CTL_REG,
288 };
289 const acpi_addr_t perf_sts = {
290 .space_id = ACPI_ADDRESS_SPACE_FIXED,
291 .bit_width = 64,
292 .addrl = PS_STS_REG,
293 };
Martin Roth5c354b92019-04-22 14:55:16 -0600294
Jason Gleneskbc521432020-09-14 05:22:47 -0700295 acpi_cstate_t cstate_info[] = {
296 [0] = {
297 .ctype = 1,
298 .latency = 1,
299 .power = 0,
300 .resource = {
301 .space_id = ACPI_ADDRESS_SPACE_FIXED,
302 .bit_width = 2,
303 .bit_offset = 2,
304 .addrl = 0,
305 .addrh = 0,
306 },
307 },
308 [1] = {
309 .ctype = 2,
310 .latency = 400,
311 .power = 0,
312 .resource = {
313 .space_id = ACPI_ADDRESS_SPACE_IO,
314 .bit_width = 8,
315 .bit_offset = 0,
316 .addrl = cstate_base_address + 1,
317 .addrh = 0,
318 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
319 },
320 },
321 };
Martin Roth5c354b92019-04-22 14:55:16 -0600322
Jason Gleneskbc521432020-09-14 05:22:47 -0700323 threads_per_core = ((cpuid_ebx(CPUID_EBX_CORE_ID) & CPUID_EBX_THREADS_MASK)
324 >> CPUID_EBX_THREADS_SHIFT)
325 + 1;
326 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
327 logical_cores = get_cpu_count();
328
329 for (cpu = 0; cpu < logical_cores; cpu++) {
330
331 if (cpu == 0) {
332 /* BSP values for \_SB.Pxxx */
333 proc_blk_len = 6;
334 proc_blk_addr = ACPI_GPE0_BLK;
335 } else {
336 /* AP values for \_SB.Pxxx */
337 proc_blk_addr = 0;
338 proc_blk_len = 0;
339 }
340
341 acpigen_write_processor(cpu, proc_blk_addr, proc_blk_len);
342
343 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
344
345 acpigen_write_pss_object(pstate_values, pstate_count);
346
347 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
348
349 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
350 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
351 HW_ALL);
352 else
353 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
354
355 acpigen_write_PPC(0);
356
357 acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
358
359 acpigen_write_CSD_package(cpu >> 1, threads_per_core, HW_ALL, 0);
360
Martin Roth5c354b92019-04-22 14:55:16 -0600361 acpigen_pop_len();
362 }
Kyösti Mälkkida321d82021-01-27 20:22:33 +0200363
364 acpigen_write_scope("\\");
365 acpigen_write_name_integer("PCNT", logical_cores);
366 acpigen_pop_len();
Martin Roth5c354b92019-04-22 14:55:16 -0600367}
368
Kyösti Mälkkie1ff3cd2020-06-29 03:17:05 +0300369void soc_fill_gnvs(struct global_nvs *gnvs)
Martin Roth5c354b92019-04-22 14:55:16 -0600370{
Martin Roth5c354b92019-04-22 14:55:16 -0600371 /* Set unknown wake source */
372 gnvs->pm1i = ~0ULL;
373 gnvs->gpei = ~0ULL;
Martin Roth5c354b92019-04-22 14:55:16 -0600374}
375
Eric Lai7cee5662020-12-28 14:52:11 +0800376static int acpigen_soc_gpio_op(const char *op, unsigned int gpio_num)
377{
378 if (gpio_num >= SOC_GPIO_TOTAL_PINS) {
379 printk(BIOS_WARNING, "Warning: Pin %d should be smaller than"
380 " %d\n", gpio_num, SOC_GPIO_TOTAL_PINS);
381 return -1;
382 }
383 /* op (gpio_num) */
384 acpigen_emit_namestring(op);
385 acpigen_write_integer(gpio_num);
386 return 0;
387}
388
Eric Laid7a36432020-12-28 15:32:54 +0800389static int acpigen_soc_get_gpio_state(const char *op, unsigned int gpio_num)
390{
391 if (gpio_num >= SOC_GPIO_TOTAL_PINS) {
392 printk(BIOS_WARNING, "Warning: Pin %d should be smaller than"
393 " %d\n", gpio_num, SOC_GPIO_TOTAL_PINS);
394 return -1;
395 }
396 /* Store (op (gpio_num), Local0) */
397 acpigen_write_store();
398 acpigen_soc_gpio_op(op, gpio_num);
399 acpigen_emit_byte(LOCAL0_OP);
400 return 0;
401}
402
403int acpigen_soc_read_rx_gpio(unsigned int gpio_num)
404{
405 return acpigen_soc_get_gpio_state("\\_SB.GRXS", gpio_num);
406}
407
408int acpigen_soc_get_tx_gpio(unsigned int gpio_num)
409{
410 return acpigen_soc_get_gpio_state("\\_SB.GTXS", gpio_num);
411}
412
Martin Roth5c354b92019-04-22 14:55:16 -0600413int acpigen_soc_set_tx_gpio(unsigned int gpio_num)
414{
Eric Lai7cee5662020-12-28 14:52:11 +0800415 return acpigen_soc_gpio_op("\\_SB.STXS", gpio_num);
Martin Roth5c354b92019-04-22 14:55:16 -0600416}
417
418int acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
419{
Eric Lai7cee5662020-12-28 14:52:11 +0800420 return acpigen_soc_gpio_op("\\_SB.CTXS", gpio_num);
Martin Roth5c354b92019-04-22 14:55:16 -0600421}