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 /*
4  * ACPI - create the Fixed ACPI Description Tables (FADT)
5  */
6 
7 #include <console/console.h>
8 #include <acpi/acpi.h>
9 #include <acpi/acpigen.h>
10 #include <device/pci_ops.h>
11 #include <arch/ioapic.h>
12 #include <arch/smp/mpspec.h>
13 #include <cpu/amd/cpuid.h>
14 #include <cpu/amd/msr.h>
15 #include <cpu/x86/smm.h>
16 #include <device/device.h>
17 #include <device/pci.h>
18 #include <amdblocks/acpimmio.h>
19 #include <amdblocks/acpi.h>
20 #include <amdblocks/chip.h>
21 #include <amdblocks/cpu.h>
22 #include <amdblocks/ioapic.h>
23 #include <soc/acpi.h>
24 #include <soc/pci_devs.h>
25 #include <soc/msr.h>
26 #include <soc/southbridge.h>
27 #include <soc/gpio.h>
28 #include <version.h>
29 #include "chip.h"
30 
31 unsigned long acpi_fill_madt(unsigned long current)
32 {
33  /* create all subtables for processors */
34  current = acpi_create_madt_lapics(current);
35 
36  current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
38 
39  current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current,
40  GNB_IOAPIC_ID, GNB_IO_APIC_ADDR, IO_APIC_INTERRUPTS);
41 
42  /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
44  MP_BUS_ISA, 0, 2,
46  /* SCI IRQ type override */
48  MP_BUS_ISA, 9, 9,
50 
51  current = acpi_fill_madt_irqoverride(current);
52 
53  /* create all subtables for processors */
57  1 /* 1: LINT1 connect to NMI */);
58 
59  return current;
60 }
61 
62 /*
63  * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
64  * in the ACPI 3.0b specification.
65  */
67 {
68  const struct soc_amd_common_config *cfg = soc_get_common_config();
69 
70  printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
71 
72  fadt->sci_int = 9; /* IRQ 09 - ACPI SCI */
73 
74  if (permanent_smi_handler()) {
75  fadt->smi_cmd = APM_CNT;
78  }
79 
83  fadt->gpe0_blk = ACPI_GPE0_BLK;
84 
85  fadt->pm1_evt_len = 4; /* 32 bits */
86  fadt->pm1_cnt_len = 2; /* 16 bits */
87  fadt->pm_tmr_len = 4; /* 32 bits */
88  fadt->gpe0_blk_len = 8; /* 64 bits */
89 
92  fadt->duty_offset = 1; /* CLK_VAL bits 3:1 */
93  fadt->duty_width = 3; /* CLK_VAL bits 3:1 */
94  fadt->day_alrm = RTC_DATE_ALARM;
95  fadt->mon_alrm = 0;
96  fadt->iapc_boot_arch = cfg->fadt_boot_arch; /* legacy free default */
97  fadt->res2 = 0; /* reserved, MUST be 0 ACPI 3.0 */
98  fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
106  fadt->flags |= cfg->fadt_flags; /* additional board-specific flags */
107 
108  fadt->ARM_boot_arch = 0; /* Must be zero if ACPI Revision <= 5.0 */
109 
110  fadt->x_firmware_ctl_l = 0; /* set to 0 if firmware_ctrl is used */
111  fadt->x_firmware_ctl_h = 0;
112 
114  fadt->x_pm1a_evt_blk.bit_width = 32;
115  fadt->x_pm1a_evt_blk.bit_offset = 0;
118  fadt->x_pm1a_evt_blk.addrh = 0x0;
119 
121  fadt->x_pm1a_cnt_blk.bit_width = 16;
122  fadt->x_pm1a_cnt_blk.bit_offset = 0;
125  fadt->x_pm1a_cnt_blk.addrh = 0x0;
126 
128  fadt->x_pm_tmr_blk.bit_width = 32;
129  fadt->x_pm_tmr_blk.bit_offset = 0;
132  fadt->x_pm_tmr_blk.addrh = 0x0;
133 
135  fadt->x_gpe0_blk.bit_width = 64; /* EventStatus + Event Enable */
136  fadt->x_gpe0_blk.bit_offset = 0;
139  fadt->x_gpe0_blk.addrh = 0x0;
140 }
141 
143 {
144  uint32_t core_freq, core_freq_mul, core_freq_div;
145  bool valid_freq_divisor;
146 
147  /* Core frequency multiplier */
148  core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
149 
150  /* Core frequency divisor ID */
151  core_freq_div =
153 
154  if (core_freq_div == 0) {
155  return 0;
156  } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
157  && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
158  /* Allow 1/8 integer steps for this range */
159  valid_freq_divisor = 1;
160  } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
161  && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
162  /* Only allow 1/4 integer steps for this range */
163  valid_freq_divisor = 1;
164  } else {
165  valid_freq_divisor = 0;
166  }
167 
168  if (valid_freq_divisor) {
169  /* 25 * core_freq_mul / (core_freq_div / 8) */
170  core_freq =
171  ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
172  } else {
173  printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
174  core_freq_div);
175  core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
176  }
177  return core_freq;
178 }
179 
181 {
182  uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
183 
184  /* Core voltage ID */
185  core_vid =
187 
188  /* Current value in amps */
189  current_value_amps =
191 
192  /* Current divisor */
193  current_divisor =
195 
196  /* Voltage */
197  if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
198  /* Voltage off for VID codes 0xF8 to 0xFF */
199  voltage_in_uvolts = 0;
200  } else {
201  voltage_in_uvolts =
203  }
204 
205  /* Power in mW */
206  power_in_mw = (voltage_in_uvolts) / 1000 * current_value_amps;
207 
208  switch (current_divisor) {
209  case 0:
210  break;
211  case 1:
212  power_in_mw = power_in_mw / 10L;
213  break;
214  case 2:
215  power_in_mw = power_in_mw / 100L;
216  break;
217  case 3:
218  /* current_divisor is set to an undefined value.*/
219  printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
220  power_in_mw = 0;
221  break;
222  }
223 
224  return power_in_mw;
225 }
226 
227 /*
228  * Populate structure describing enabled p-states and return count of enabled p-states.
229  */
230 static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
231  struct acpi_xpss_sw_pstate *pstate_xpss_values)
232 {
233  msr_t pstate_def;
234  size_t pstate_count, pstate;
235  uint32_t pstate_enable, max_pstate;
236 
237  pstate_count = 0;
239 
240  for (pstate = 0; pstate <= max_pstate; pstate++) {
241  pstate_def = rdmsr(PSTATE_0_MSR + pstate);
242 
243  pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
245  if (!pstate_enable)
246  continue;
247 
248  pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
249  pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
250  pstate_values[pstate_count].transition_latency = 0;
251  pstate_values[pstate_count].bus_master_latency = 0;
252  pstate_values[pstate_count].control_value = pstate;
253  pstate_values[pstate_count].status_value = pstate;
254 
255  pstate_xpss_values[pstate_count].core_freq =
256  (uint64_t)pstate_values[pstate_count].core_freq;
257  pstate_xpss_values[pstate_count].power =
258  (uint64_t)pstate_values[pstate_count].power;
259  pstate_xpss_values[pstate_count].transition_latency = 0;
260  pstate_xpss_values[pstate_count].bus_master_latency = 0;
261  pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
262  pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
263  pstate_count++;
264  }
265 
266  return pstate_count;
267 }
268 
269 void generate_cpu_entries(const struct device *device)
270 {
271  int logical_cores;
272  size_t pstate_count, cpu, proc_blk_len;
273  struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
274  struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
275  uint32_t threads_per_core, proc_blk_addr;
276  uint32_t cstate_base_address =
278 
279  const acpi_addr_t perf_ctrl = {
281  .bit_width = 64,
282  .addrl = PS_CTL_REG,
283  };
284  const acpi_addr_t perf_sts = {
286  .bit_width = 64,
287  .addrl = PS_STS_REG,
288  };
289 
290  const acpi_cstate_t cstate_info[] = {
291  [0] = {
292  .ctype = 1,
293  .latency = 1,
294  .power = 0,
295  .resource = {
296  .space_id = ACPI_ADDRESS_SPACE_FIXED,
297  .bit_width = 2,
298  .bit_offset = 2,
299  .addrl = 0,
300  .addrh = 0,
301  },
302  },
303  [1] = {
304  .ctype = 2,
305  .latency = 400,
306  .power = 0,
307  .resource = {
308  .space_id = ACPI_ADDRESS_SPACE_IO,
309  .bit_width = 8,
310  .bit_offset = 0,
311  .addrl = cstate_base_address + 1,
312  .addrh = 0,
313  .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
314  },
315  },
316  };
317 
318  threads_per_core = get_threads_per_core();
319  pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
320  logical_cores = get_cpu_count();
321 
322  for (cpu = 0; cpu < logical_cores; cpu++) {
323 
324  if (cpu == 0) {
325  /* BSP values for \_SB.Pxxx */
326  proc_blk_len = 6;
327  proc_blk_addr = ACPI_GPE0_BLK;
328  } else {
329  /* AP values for \_SB.Pxxx */
330  proc_blk_addr = 0;
331  proc_blk_len = 0;
332  }
333 
334  acpigen_write_processor(cpu, proc_blk_addr, proc_blk_len);
335 
336  acpigen_write_pct_package(&perf_ctrl, &perf_sts);
337 
338  acpigen_write_pss_object(pstate_values, pstate_count);
339 
340  acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
341 
342  if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
343  acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
344  HW_ALL);
345  else
346  acpigen_write_PSD_package(0, logical_cores, SW_ALL);
347 
349 
350  acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info));
351 
352  acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
353  CSD_HW_ALL, 0);
354 
355  acpigen_pop_len();
356  }
357 
358  acpigen_write_processor_package("PPKG", 0, logical_cores);
359 }
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
const struct soc_amd_common_config * soc_get_common_config(void)
Definition: config.c:7
#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_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 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_DATE_ALARM
Definition: acpi.h:14
#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:230
static uint32_t get_pstate_core_freq(msr_t pstate_def)
Definition: acpi.c:142
static uint32_t get_pstate_core_power(msr_t pstate_def)
Definition: acpi.c:180
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 x_firmware_ctl_h
Definition: acpi.h:752
u16 ARM_boot_arch
Definition: acpi.h:749
u32 gpe0_blk
Definition: acpi.h:725
acpi_addr_t x_gpe0_blk
Definition: acpi.h:761
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
u32 x_firmware_ctl_l
Definition: acpi.h:751
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 res2
Definition: acpi.h:745
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