coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
acpi.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 /* TODO: Check if this is still correct */
4 
5 /* ACPI - create the Fixed ACPI Description Tables (FADT) */
6 
7 #include <acpi/acpi.h>
8 #include <acpi/acpigen.h>
9 #include <amdblocks/acpi.h>
10 #include <amdblocks/cpu.h>
11 #include <amdblocks/acpimmio.h>
12 #include <amdblocks/ioapic.h>
13 #include <arch/ioapic.h>
14 #include <arch/smp/mpspec.h>
15 #include <console/console.h>
16 #include <cpu/amd/cpuid.h>
17 #include <cpu/amd/msr.h>
18 #include <cpu/x86/smm.h>
19 #include <soc/acpi.h>
20 #include <soc/iomap.h>
21 #include <soc/msr.h>
22 #include <types.h>
23 #include "chip.h"
24 
25 unsigned long acpi_fill_madt(unsigned long current)
26 {
27  /* create all subtables for processors */
28  current = acpi_create_madt_lapics(current);
29 
30  current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
32 
33  current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
34  GNB_IOAPIC_ID, GNB_IO_APIC_ADDR, IO_APIC_INTERRUPTS);
35 
36  /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
38  MP_BUS_ISA, 0, 2,
40  /* SCI IRQ type override */
44  current = acpi_fill_madt_irqoverride(current);
45 
46  /* create all subtables for processors */
50  1 /* 1: LINT1 connect to NMI */);
51 
52  return current;
53 }
54 
55 /*
56  * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
57  * in the ACPI 3.0b specification.
58  */
60 {
61  const struct soc_amd_sabrina_config *cfg = config_of_soc();
62 
63  printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
64 
65  fadt->sci_int = ACPI_SCI_IRQ;
66 
67  if (permanent_smi_handler()) {
68  fadt->smi_cmd = APM_CNT;
71  }
72 
73  fadt->pstate_cnt = 0;
74 
78  fadt->gpe0_blk = ACPI_GPE0_BLK;
79 
80  fadt->pm1_evt_len = 4; /* 32 bits */
81  fadt->pm1_cnt_len = 2; /* 16 bits */
82  fadt->pm_tmr_len = 4; /* 32 bits */
83  fadt->gpe0_blk_len = 8; /* 64 bits */
84 
87  fadt->duty_offset = 0; /* Not supported */
88  fadt->duty_width = 0; /* Not supported */
89  fadt->day_alrm = RTC_DATE_ALARM;
90  fadt->mon_alrm = 0;
91  fadt->century = RTC_ALT_CENTURY;
92  fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */
93  fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
101  if (cfg->s0ix_enable)
103 
104  fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */
105 
107  fadt->x_pm1a_evt_blk.bit_width = 32;
108  fadt->x_pm1a_evt_blk.bit_offset = 0;
111  fadt->x_pm1a_evt_blk.addrh = 0x0;
112 
114  fadt->x_pm1a_cnt_blk.bit_width = 16;
115  fadt->x_pm1a_cnt_blk.bit_offset = 0;
118  fadt->x_pm1a_cnt_blk.addrh = 0x0;
119 
121  fadt->x_pm_tmr_blk.bit_width = 32;
122  fadt->x_pm_tmr_blk.bit_offset = 0;
125  fadt->x_pm_tmr_blk.addrh = 0x0;
126 
128  fadt->x_gpe0_blk.bit_width = 64;
129  fadt->x_gpe0_blk.bit_offset = 0;
132  fadt->x_gpe0_blk.addrh = 0x0;
133 }
134 
136 {
137  uint32_t core_freq, core_freq_mul, core_freq_div;
138  bool valid_freq_divisor;
139 
140  /* Core frequency multiplier */
141  core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
142 
143  /* Core frequency divisor ID */
144  core_freq_div =
146 
147  if (core_freq_div == 0) {
148  return 0;
149  } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
150  && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
151  /* Allow 1/8 integer steps for this range */
152  valid_freq_divisor = 1;
153  } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
154  && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
155  /* Only allow 1/4 integer steps for this range */
156  valid_freq_divisor = 1;
157  } else {
158  valid_freq_divisor = 0;
159  }
160 
161  if (valid_freq_divisor) {
162  /* 25 * core_freq_mul / (core_freq_div / 8) */
163  core_freq =
164  ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
165  } else {
166  printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
167  core_freq_div);
168  core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
169  }
170  return core_freq;
171 }
172 
174 {
175  uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
176 
177  /* Core voltage ID */
178  core_vid =
180 
181  /* Current value in amps */
182  current_value_amps =
184 
185  /* Current divisor */
186  current_divisor =
188 
189  /* Voltage */
190  if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
191  /* Voltage off for VID codes 0xF8 to 0xFF */
192  voltage_in_uvolts = 0;
193  } else {
194  voltage_in_uvolts =
196  }
197 
198  /* Power in mW */
199  power_in_mw = (voltage_in_uvolts) / 1000 * current_value_amps;
200 
201  switch (current_divisor) {
202  case 0:
203  break;
204  case 1:
205  power_in_mw = power_in_mw / 10L;
206  break;
207  case 2:
208  power_in_mw = power_in_mw / 100L;
209  break;
210  case 3:
211  /* current_divisor is set to an undefined value.*/
212  printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
213  power_in_mw = 0;
214  break;
215  }
216 
217  return power_in_mw;
218 }
219 
220 /*
221  * Populate structure describing enabled p-states and return count of enabled p-states.
222  */
223 static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
224  struct acpi_xpss_sw_pstate *pstate_xpss_values)
225 {
226  msr_t pstate_def;
227  size_t pstate_count, pstate;
228  uint32_t pstate_enable, max_pstate;
229 
230  pstate_count = 0;
232 
233  for (pstate = 0; pstate <= max_pstate; pstate++) {
234  pstate_def = rdmsr(PSTATE_0_MSR + pstate);
235 
236  pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
238  if (!pstate_enable)
239  continue;
240 
241  pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
242  pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
243  pstate_values[pstate_count].transition_latency = 0;
244  pstate_values[pstate_count].bus_master_latency = 0;
245  pstate_values[pstate_count].control_value = pstate;
246  pstate_values[pstate_count].status_value = pstate;
247 
248  pstate_xpss_values[pstate_count].core_freq =
249  (uint64_t)pstate_values[pstate_count].core_freq;
250  pstate_xpss_values[pstate_count].power =
251  (uint64_t)pstate_values[pstate_count].power;
252  pstate_xpss_values[pstate_count].transition_latency = 0;
253  pstate_xpss_values[pstate_count].bus_master_latency = 0;
254  pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
255  pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
256  pstate_count++;
257  }
258 
259  return pstate_count;
260 }
261 
262 void generate_cpu_entries(const struct device *device)
263 {
264  int logical_cores;
265  size_t pstate_count, cpu, proc_blk_len;
266  struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
267  struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
268  uint32_t threads_per_core, proc_blk_addr;
269  uint32_t cstate_base_address =
271 
272  const acpi_addr_t perf_ctrl = {
274  .bit_width = 64,
275  .addrl = PS_CTL_REG,
276  };
277  const acpi_addr_t perf_sts = {
279  .bit_width = 64,
280  .addrl = PS_STS_REG,
281  };
282 
283  const acpi_cstate_t cstate_info[] = {
284  [0] = {
285  .ctype = 1,
286  .latency = 1,
287  .power = 0,
288  .resource = {
289  .space_id = ACPI_ADDRESS_SPACE_FIXED,
290  .bit_width = 2,
291  .bit_offset = 2,
292  .addrl = 0,
293  .addrh = 0,
294  },
295  },
296  [1] = {
297  .ctype = 2,
298  .latency = 0x12,
299  .power = 0,
300  .resource = {
301  .space_id = ACPI_ADDRESS_SPACE_IO,
302  .bit_width = 8,
303  .bit_offset = 0,
304  .addrl = cstate_base_address + 1,
305  .addrh = 0,
306  .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
307  },
308  },
309  [2] = {
310  .ctype = 3,
311  .latency = 350,
312  .power = 0,
313  .resource = {
314  .space_id = ACPI_ADDRESS_SPACE_IO,
315  .bit_width = 8,
316  .bit_offset = 0,
317  .addrl = cstate_base_address + 2,
318  .addrh = 0,
319  .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
320  },
321  },
322  };
323 
324  threads_per_core = get_threads_per_core();
325  pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
326  logical_cores = get_cpu_count();
327 
328  for (cpu = 0; cpu < logical_cores; cpu++) {
329 
330  if (cpu == 0) {
331  /* BSP values for \_SB.Pxxx */
332  proc_blk_len = 6;
333  proc_blk_addr = ACPI_GPE0_BLK;
334  } else {
335  /* AP values for \_SB.Pxxx */
336  proc_blk_addr = 0;
337  proc_blk_len = 0;
338  }
339 
340  acpigen_write_processor(cpu, proc_blk_addr, proc_blk_len);
341 
342  acpigen_write_pct_package(&perf_ctrl, &perf_sts);
343 
344  acpigen_write_pss_object(pstate_values, pstate_count);
345 
346  acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
347 
348  if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
349  acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
350  HW_ALL);
351  else
352  acpigen_write_PSD_package(0, logical_cores, SW_ALL);
353 
355 
356  acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
357 
358  acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
359  CSD_HW_ALL, 0);
360 
361  acpigen_pop_len();
362  }
363 
364  acpigen_write_processor_package("PPKG", 0, logical_cores);
365 }
unsigned long acpi_create_madt_lapics(unsigned long current)
Definition: acpi.c:144
int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride, u8 bus, u8 source, u32 gsirq, u16 flags)
Definition: acpi.c:187
int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr, u32 gsi_base)
Definition: acpi.c:174
int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu, u16 flags, u8 lint)
Definition: acpi.c:200
void acpigen_write_PPC(u8 nr)
Definition: acpigen.c:880
void acpigen_pop_len(void)
Definition: acpigen.c:37
void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
Definition: acpigen.c:957
void acpigen_write_CST_package(const acpi_cstate_t *cstate, int nentries)
Definition: acpigen.c:998
void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Definition: acpigen.c:391
void acpigen_write_processor_package(const char *const name, const unsigned int first_core, const unsigned int core_count)
Definition: acpigen.c:409
void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
Definition: acpigen.c:2215
void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype, u32 index)
Definition: acpigen.c:1011
void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
Definition: acpigen.c:2189
void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Definition: acpigen.c:974
#define ACPI_PM1_CNT_BLK
Definition: iomap.h:43
#define ACPI_IO_BASE
Definition: iomap.h:39
#define ACPI_PM_TMR_BLK
Definition: iomap.h:44
#define ACPI_GPE0_BLK
Definition: iomap.h:46
#define ACPI_PM_EVT_BLK
Definition: iomap.h:40
#define IO_APIC_ADDR
Definition: ioapic.h:6
#define IO_APIC_INTERRUPTS
Definition: ioapic.h:8
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define printk(level,...)
Definition: stdlib.h:16
void generate_cpu_entries(const struct device *device)
Definition: acpi.c:334
@ CONFIG
Definition: dsi_common.h:201
static int get_cpu_count(void)
Definition: haswell_init.c:584
#define ACPI_FADT_S4_RTC_WAKE
Definition: acpi.h:799
#define ACPI_FADT_PLATFORM_CLOCK
Definition: acpi.h:807
#define ACPI_FADT_32BIT_TIMER
Definition: acpi.h:800
#define ACPI_FADT_C2_NOT_SUPPORTED
Definition: acpi.h:788
static int permanent_smi_handler(void)
Definition: acpi.h:1414
#define ACPI_ACCESS_SIZE_WORD_ACCESS
Definition: acpi.h:128
#define ACPI_MADT_LAPIC_NMI_ALL_PROCESSORS
Definition: acpi.h:627
#define ACPI_FADT_S4_RTC_VALID
Definition: acpi.h:808
#define ACPI_ACCESS_SIZE_DWORD_ACCESS
Definition: acpi.h:129
#define ACPI_FADT_REMOTE_POWER_ON
Definition: acpi.h:809
#define ACPI_FADT_WBINVD
Definition: acpi.h:792
#define ACPI_FADT_PCI_EXPRESS_WAKE
Definition: acpi.h:806
#define ACPI_FADT_LOW_PWR_IDLE_S0
Definition: acpi.h:814
#define ACPI_FADT_C1_SUPPORTED
Definition: acpi.h:794
#define ACPI_ACCESS_SIZE_BYTE_ACCESS
Definition: acpi.h:127
#define ACPI_FADT_C3_NOT_SUPPORTED
Definition: acpi.h:789
#define ACPI_ADDRESS_SPACE_IO
Definition: acpi.h:105
#define ACPI_ADDRESS_SPACE_FIXED
Definition: acpi.h:115
@ HW_ALL
Definition: acpigen.h:375
@ SW_ALL
Definition: acpigen.h:375
@ CSD_HW_ALL
Definition: acpigen.h:379
#define MSR_CSTATE_ADDRESS
Definition: msr.h:53
#define PS_CTL_REG
Definition: msr.h:35
#define SERIAL_VID_DECODE_MICROVOLTS
Definition: msr.h:48
#define PSTATE_0_MSR
Definition: msr.h:42
#define PS_LIM_MAX_VAL_MASK
Definition: msr.h:31
#define PS_LIM_REG
Definition: msr.h:28
#define PS_STS_REG
Definition: msr.h:41
#define MAX_PSTATES
Definition: msr.h:32
#define SERIAL_VID_MAX_MICROVOLTS
Definition: msr.h:49
#define PS_MAX_VAL_SHFT
Definition: msr.h:30
#define MSR_CSTATE_ADDRESS_MASK
Definition: msr.h:54
static __always_inline msr_t rdmsr(unsigned int index)
Definition: msr.h:146
#define APM_CNT
Definition: smm.h:19
#define APM_CNT_ACPI_DISABLE
Definition: smm.h:21
#define APM_CNT_ACPI_ENABLE
Definition: smm.h:22
#define config_of_soc()
Definition: device.h:394
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
#define MP_IRQ_TRIGGER_DEFAULT
Definition: mpspec.h:127
@ MP_BUS_ISA
Definition: mpspec.h:158
#define MP_IRQ_POLARITY_LOW
Definition: mpspec.h:125
#define MP_IRQ_POLARITY_HIGH
Definition: mpspec.h:124
#define MP_IRQ_POLARITY_DEFAULT
Definition: mpspec.h:123
#define MP_IRQ_TRIGGER_EDGE
Definition: mpspec.h:128
#define MP_IRQ_TRIGGER_LEVEL
Definition: mpspec.h:129
static const struct pnpconfig power[]
Definition: pnpconfig.c:14
void acpi_fill_fadt(acpi_fadt_t *fadt)
Definition: acpi.c:58
unsigned long acpi_fill_madt(unsigned long current)
Definition: acpi.c:24
#define RTC_ALT_CENTURY
Definition: acpi.h:15
#define RTC_DATE_ALARM
Definition: acpi.h:14
#define ACPI_SCI_IRQ
Definition: acpi.h:11
#define PSTATE_DEF_HI_ENABLE_SHIFT
Definition: msr.h:7
#define PSTATE_DEF_LO_FREQ_DIV_MASK
Definition: msr.h:16
#define PSTATE_DEF_LO_CUR_VAL_SHIFT
Definition: msr.h:11
#define PSTATE_DEF_LO_FREQ_DIV_SHIFT
Definition: msr.h:15
#define PSTATE_DEF_LO_FREQ_MUL_MASK
Definition: msr.h:21
#define PSTATE_DEF_LO_CUR_VAL_MASK
Definition: msr.h:12
#define PSTATE_DEF_LO_CORE_VID_MASK
Definition: msr.h:14
#define PSTATE_DEF_LO_EIGHTH_STEP_MAX
Definition: msr.h:18
#define PSTATE_DEF_HI_ENABLE_MASK
Definition: msr.h:8
#define PSTATE_DEF_LO_CUR_DIV_SHIFT
Definition: msr.h:9
#define PSTATE_DEF_LO_FREQ_DIV_MAX
Definition: msr.h:19
#define PSTATE_DEF_LO_CORE_FREQ_BASE
Definition: msr.h:22
#define PSTATE_DEF_LO_CUR_DIV_MASK
Definition: msr.h:10
#define PSTATE_DEF_LO_FREQ_DIV_MIN
Definition: msr.h:17
#define PSTATE_DEF_LO_CORE_VID_SHIFT
Definition: msr.h:13
unsigned long acpi_fill_madt_irqoverride(unsigned long current)
Definition: tables.c:20
unsigned int get_threads_per_core(void)
Definition: cpu.c:15
#define FCH_IOAPIC_ID
Definition: ioapic.h:7
#define GNB_IOAPIC_ID
Definition: ioapic.h:8
static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values, struct acpi_xpss_sw_pstate *pstate_xpss_values)
Definition: acpi.c:223
static uint32_t get_pstate_core_freq(msr_t pstate_def)
Definition: acpi.c:135
static uint32_t get_pstate_core_power(msr_t pstate_def)
Definition: acpi.c:173
unsigned int uint32_t
Definition: stdint.h:14
unsigned long long uint64_t
Definition: stdint.h:17
u8 ctype
Definition: acpi.h:984
u32 pm1a_cnt_blk
Definition: acpi.h:721
u32 pm_tmr_blk
Definition: acpi.h:724
u8 acpi_enable
Definition: acpi.h:715
u8 duty_offset
Definition: acpi.h:739
u8 pm1_evt_len
Definition: acpi.h:727
u32 gpe0_blk
Definition: acpi.h:725
acpi_addr_t x_gpe0_blk
Definition: acpi.h:761
u8 century
Definition: acpi.h:743
u8 pm_tmr_len
Definition: acpi.h:730
u32 smi_cmd
Definition: acpi.h:714
u8 day_alrm
Definition: acpi.h:741
u8 duty_width
Definition: acpi.h:740
u8 acpi_disable
Definition: acpi.h:716
u8 pm1_cnt_len
Definition: acpi.h:728
acpi_addr_t x_pm1a_evt_blk
Definition: acpi.h:755
u32 pm1a_evt_blk
Definition: acpi.h:719
acpi_addr_t x_pm1a_cnt_blk
Definition: acpi.h:757
u16 p_lvl2_lat
Definition: acpi.h:735
u8 gpe0_blk_len
Definition: acpi.h:731
u16 p_lvl3_lat
Definition: acpi.h:736
u8 pstate_cnt
Definition: acpi.h:718
u32 flags
Definition: acpi.h:746
u16 iapc_boot_arch
Definition: acpi.h:744
acpi_addr_t x_pm_tmr_blk
Definition: acpi.h:760
u16 sci_int
Definition: acpi.h:713
u8 mon_alrm
Definition: acpi.h:742
u8 bit_offset
Definition: acpi.h:98
u8 bit_width
Definition: acpi.h:97
u8 access_size
Definition: acpi.h:99
u32 status_value
Definition: acpi.h:996
u32 transition_latency
Definition: acpi.h:993
u32 bus_master_latency
Definition: acpi.h:994
u32 core_freq
Definition: acpi.h:991
u32 power
Definition: acpi.h:992
u32 control_value
Definition: acpi.h:995
u64 bus_master_latency
Definition: acpi.h:1003
u64 transition_latency
Definition: acpi.h:1002
Definition: device.h:107
unsigned int hi
Definition: msr.h:112
unsigned int lo
Definition: msr.h:111
uint32_t fadt_flags
Definition: chip.h:28
uint16_t fadt_boot_arch
Definition: chip.h:27
struct soc_amd_common_config common_config
Definition: chip.h:18