blob: 0bac92f13666af660c3cbe7b817f43a2671b2421 [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
Martin Roth5c354b92019-04-22 14:55:16 -06007#include <console/console.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07008#include <acpi/acpi.h>
9#include <acpi/acpigen.h>
Martin Roth5c354b92019-04-22 14:55:16 -060010#include <device/pci_ops.h>
11#include <arch/ioapic.h>
Raul E Rangel93b62e62020-01-31 12:53:45 -070012#include <arch/smp/mpspec.h>
Jason Glenesk498015d2020-12-10 03:28:38 -080013#include <cpu/amd/cpuid.h>
Jason Gleneskbc521432020-09-14 05:22:47 -070014#include <cpu/amd/msr.h>
Martin Roth5c354b92019-04-22 14:55:16 -060015#include <cpu/x86/smm.h>
Martin Roth5c354b92019-04-22 14:55:16 -060016#include <device/device.h>
17#include <device/pci.h>
Elyes Haouas5e2602a2023-01-14 05:46:25 +010018#include <gpio.h>
Martin Roth5c354b92019-04-22 14:55:16 -060019#include <amdblocks/acpimmio.h>
20#include <amdblocks/acpi.h>
Raul E Rangel1c88b102021-02-11 10:35:32 -070021#include <amdblocks/chip.h>
Felix Helddd2f3fa2021-02-08 22:23:54 +010022#include <amdblocks/cpu.h>
Felix Held604ffa62021-02-12 00:43:20 +010023#include <amdblocks/ioapic.h>
Martin Roth5c354b92019-04-22 14:55:16 -060024#include <soc/acpi.h>
25#include <soc/pci_devs.h>
Jason Gleneskbc521432020-09-14 05:22:47 -070026#include <soc/msr.h>
Martin Roth5c354b92019-04-22 14:55:16 -060027#include <soc/southbridge.h>
Martin Roth5c354b92019-04-22 14:55:16 -060028#include <version.h>
Raul E Rangel93b62e62020-01-31 12:53:45 -070029#include "chip.h"
Martin Roth5c354b92019-04-22 14:55:16 -060030
31unsigned long acpi_fill_madt(unsigned long current)
32{
33 /* create all subtables for processors */
Kyösti Mälkki66b5e1b2022-11-12 21:13:45 +020034 current = acpi_create_madt_lapics_with_nmis(current);
Martin Roth5c354b92019-04-22 14:55:16 -060035
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030036 current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current, IO_APIC_ADDR);
Martin Roth5c354b92019-04-22 14:55:16 -060037
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030038 current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current,
39 GNB_IO_APIC_ADDR);
Jason Gleneskf459a402020-09-02 16:49:10 -070040
Felix Held69a957f2021-06-17 15:48:25 +020041 /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */
42 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
43 MP_BUS_ISA, 0, 2,
44 MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT);
45 /* SCI IRQ type override */
46 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current,
Felix Heldc0ae0ba2023-02-27 21:02:48 +010047 MP_BUS_ISA, ACPI_SCI_IRQ, ACPI_SCI_IRQ,
Felix Held69a957f2021-06-17 15:48:25 +020048 MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
Raul E Rangel93b62e62020-01-31 12:53:45 -070049
Raul E Rangelffab5e62021-02-11 11:07:11 -070050 current = acpi_fill_madt_irqoverride(current);
Martin Roth5c354b92019-04-22 14:55:16 -060051
Martin Roth5c354b92019-04-22 14:55:16 -060052 return current;
53}
54
55/*
56 * Reference section 5.2.9 Fixed ACPI Description Table (FADT)
57 * in the ACPI 3.0b specification.
58 */
Kyösti Mälkki61ef71b2020-05-30 18:54:39 +030059void acpi_fill_fadt(acpi_fadt_t *fadt)
Martin Roth5c354b92019-04-22 14:55:16 -060060{
Raul E Rangel1c88b102021-02-11 10:35:32 -070061 const struct soc_amd_common_config *cfg = soc_get_common_config();
Martin Rotheca8faa2019-12-01 16:49:19 -070062
Felix Held757d6452021-02-04 21:31:49 +010063 printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE);
Martin Roth5c354b92019-04-22 14:55:16 -060064
Felix Heldc0ae0ba2023-02-27 21:02:48 +010065 fadt->sci_int = ACPI_SCI_IRQ;
Martin Roth5c354b92019-04-22 14:55:16 -060066
Kyösti Mälkki0a9e72e2019-08-11 01:22:28 +030067 if (permanent_smi_handler()) {
Martin Roth5c354b92019-04-22 14:55:16 -060068 fadt->smi_cmd = APM_CNT;
69 fadt->acpi_enable = APM_CNT_ACPI_ENABLE;
70 fadt->acpi_disable = APM_CNT_ACPI_DISABLE;
Martin Roth5c354b92019-04-22 14:55:16 -060071 }
72
73 fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060074 fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060075 fadt->pm_tmr_blk = ACPI_PM_TMR_BLK;
76 fadt->gpe0_blk = ACPI_GPE0_BLK;
Martin Roth5c354b92019-04-22 14:55:16 -060077
78 fadt->pm1_evt_len = 4; /* 32 bits */
79 fadt->pm1_cnt_len = 2; /* 16 bits */
Martin Roth5c354b92019-04-22 14:55:16 -060080 fadt->pm_tmr_len = 4; /* 32 bits */
81 fadt->gpe0_blk_len = 8; /* 64 bits */
Martin Roth5c354b92019-04-22 14:55:16 -060082
Felix Held164c5ed2022-10-18 00:11:48 +020083 fill_fadt_extended_pm_regs(fadt);
84
Felix Held54c80e12023-02-21 17:59:42 +010085 /* p_lvl2_lat and p_lvl3_lat match what the AGESA code does, but those values are
86 overridden by the _CST packages in the processor devices. */
Martin Roth5c354b92019-04-22 14:55:16 -060087 fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED;
88 fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED;
Felix Held72b92c92021-11-18 20:41:40 +010089 fadt->day_alrm = RTC_DATE_ALARM;
Martin Rotheca8faa2019-12-01 16:49:19 -070090 fadt->iapc_boot_arch = cfg->fadt_boot_arch; /* legacy free default */
Martin Rotheca8faa2019-12-01 16:49:19 -070091 fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */
92 ACPI_FADT_C1_SUPPORTED |
93 ACPI_FADT_S4_RTC_WAKE |
94 ACPI_FADT_32BIT_TIMER |
95 ACPI_FADT_PCI_EXPRESS_WAKE |
96 ACPI_FADT_PLATFORM_CLOCK |
97 ACPI_FADT_S4_RTC_VALID |
98 ACPI_FADT_REMOTE_POWER_ON;
99 fadt->flags |= cfg->fadt_flags; /* additional board-specific flags */
Martin Roth5c354b92019-04-22 14:55:16 -0600100}
101
Jason Gleneskbc521432020-09-14 05:22:47 -0700102static uint32_t get_pstate_core_freq(msr_t pstate_def)
103{
104 uint32_t core_freq, core_freq_mul, core_freq_div;
105 bool valid_freq_divisor;
106
107 /* Core frequency multiplier */
108 core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK;
109
110 /* Core frequency divisor ID */
111 core_freq_div =
112 (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT;
113
114 if (core_freq_div == 0) {
115 return 0;
116 } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN)
117 && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) {
118 /* Allow 1/8 integer steps for this range */
119 valid_freq_divisor = 1;
120 } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX)
121 && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) {
122 /* Only allow 1/4 integer steps for this range */
123 valid_freq_divisor = 1;
124 } else {
125 valid_freq_divisor = 0;
126 }
127
128 if (valid_freq_divisor) {
129 /* 25 * core_freq_mul / (core_freq_div / 8) */
130 core_freq =
131 ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div));
132 } else {
133 printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n",
134 core_freq_div);
135 core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul);
136 }
137 return core_freq;
138}
139
140static uint32_t get_pstate_core_power(msr_t pstate_def)
141{
142 uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw;
143
144 /* Core voltage ID */
145 core_vid =
146 (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT;
147
148 /* Current value in amps */
149 current_value_amps =
150 (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT;
151
152 /* Current divisor */
153 current_divisor =
154 (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT;
155
156 /* Voltage */
157 if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) {
158 /* Voltage off for VID codes 0xF8 to 0xFF */
159 voltage_in_uvolts = 0;
160 } else {
161 voltage_in_uvolts =
162 SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid);
163 }
164
165 /* Power in mW */
Zheng Bao62cd5e82022-08-25 17:11:38 +0800166 power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps;
Jason Gleneskbc521432020-09-14 05:22:47 -0700167
168 switch (current_divisor) {
169 case 0:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800170 power_in_mw = power_in_mw / 100L;
Jason Gleneskbc521432020-09-14 05:22:47 -0700171 break;
172 case 1:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800173 power_in_mw = power_in_mw / 1000L;
Jason Gleneskbc521432020-09-14 05:22:47 -0700174 break;
175 case 2:
Zheng Bao62cd5e82022-08-25 17:11:38 +0800176 power_in_mw = power_in_mw / 10000L;
Jason Gleneskbc521432020-09-14 05:22:47 -0700177 break;
178 case 3:
179 /* current_divisor is set to an undefined value.*/
180 printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n");
181 power_in_mw = 0;
182 break;
183 }
184
185 return power_in_mw;
186}
187
188/*
189 * Populate structure describing enabled p-states and return count of enabled p-states.
190 */
191static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values,
192 struct acpi_xpss_sw_pstate *pstate_xpss_values)
193{
194 msr_t pstate_def;
195 size_t pstate_count, pstate;
196 uint32_t pstate_enable, max_pstate;
197
198 pstate_count = 0;
199 max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT;
200
201 for (pstate = 0; pstate <= max_pstate; pstate++) {
Felix Held0a466042023-02-27 23:56:39 +0100202 pstate_def = rdmsr(PSTATE_MSR(pstate));
Jason Gleneskbc521432020-09-14 05:22:47 -0700203
204 pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK)
205 >> PSTATE_DEF_HI_ENABLE_SHIFT;
206 if (!pstate_enable)
207 continue;
208
209 pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def);
210 pstate_values[pstate_count].power = get_pstate_core_power(pstate_def);
211 pstate_values[pstate_count].transition_latency = 0;
212 pstate_values[pstate_count].bus_master_latency = 0;
213 pstate_values[pstate_count].control_value = pstate;
214 pstate_values[pstate_count].status_value = pstate;
215
216 pstate_xpss_values[pstate_count].core_freq =
217 (uint64_t)pstate_values[pstate_count].core_freq;
218 pstate_xpss_values[pstate_count].power =
219 (uint64_t)pstate_values[pstate_count].power;
220 pstate_xpss_values[pstate_count].transition_latency = 0;
221 pstate_xpss_values[pstate_count].bus_master_latency = 0;
222 pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate;
223 pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate;
224 pstate_count++;
225 }
226
227 return pstate_count;
228}
229
Felix Held9bb66462023-03-04 02:33:28 +0100230const acpi_cstate_t cstate_cfg_table[] = {
231 [0] = {
232 .ctype = 1,
233 .latency = 1,
234 .power = 0,
235 },
236 [1] = {
237 .ctype = 2,
238 .latency = 400,
239 .power = 0,
240 },
241};
242
243const acpi_cstate_t *get_cstate_config_data(size_t *size)
244{
245 *size = ARRAY_SIZE(cstate_cfg_table);
246 return cstate_cfg_table;
247}
248
Furquan Shaikh7536a392020-04-24 21:59:21 -0700249void generate_cpu_entries(const struct device *device)
Martin Roth5c354b92019-04-22 14:55:16 -0600250{
Jason Gleneskbc521432020-09-14 05:22:47 -0700251 int logical_cores;
Felix Held9bb66462023-03-04 02:33:28 +0100252 size_t cstate_count, pstate_count, cpu;
253 acpi_cstate_t cstate_values[MAX_CSTATE_COUNT] = { {0} };
Jason Gleneskbc521432020-09-14 05:22:47 -0700254 struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} };
255 struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} };
Felix Held281be572023-01-28 03:38:20 +0100256 uint32_t threads_per_core;
Martin Roth5c354b92019-04-22 14:55:16 -0600257
Jason Gleneskbc521432020-09-14 05:22:47 -0700258 const acpi_addr_t perf_ctrl = {
259 .space_id = ACPI_ADDRESS_SPACE_FIXED,
260 .bit_width = 64,
261 .addrl = PS_CTL_REG,
262 };
263 const acpi_addr_t perf_sts = {
264 .space_id = ACPI_ADDRESS_SPACE_FIXED,
265 .bit_width = 64,
266 .addrl = PS_STS_REG,
267 };
Martin Roth5c354b92019-04-22 14:55:16 -0600268
Felix Heldd4b5ad02022-01-25 04:14:05 +0100269 threads_per_core = get_threads_per_core();
Felix Held9bb66462023-03-04 02:33:28 +0100270 cstate_count = get_cstate_info(cstate_values);
Jason Gleneskbc521432020-09-14 05:22:47 -0700271 pstate_count = get_pstate_info(pstate_values, pstate_xpss_values);
272 logical_cores = get_cpu_count();
273
274 for (cpu = 0; cpu < logical_cores; cpu++) {
Felix Held281be572023-01-28 03:38:20 +0100275 acpigen_write_processor_device(cpu);
Jason Gleneskbc521432020-09-14 05:22:47 -0700276
277 acpigen_write_pct_package(&perf_ctrl, &perf_sts);
278
279 acpigen_write_pss_object(pstate_values, pstate_count);
280
281 acpigen_write_xpss_object(pstate_xpss_values, pstate_count);
282
283 if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT))
284 acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core,
285 HW_ALL);
286 else
287 acpigen_write_PSD_package(0, logical_cores, SW_ALL);
288
289 acpigen_write_PPC(0);
290
Felix Held9bb66462023-03-04 02:33:28 +0100291 acpigen_write_CST_package(cstate_values, cstate_count);
Jason Gleneskbc521432020-09-14 05:22:47 -0700292
Felix Heldc5635082021-03-30 02:04:02 +0200293 acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core,
294 CSD_HW_ALL, 0);
Jason Gleneskbc521432020-09-14 05:22:47 -0700295
Felix Held281be572023-01-28 03:38:20 +0100296 acpigen_write_processor_device_end();
Martin Roth5c354b92019-04-22 14:55:16 -0600297 }
Kyösti Mälkkida321d82021-01-27 20:22:33 +0200298
Felix Heldcf2eeff2022-03-02 15:00:59 +0100299 acpigen_write_processor_package("PPKG", 0, logical_cores);
Martin Roth5c354b92019-04-22 14:55:16 -0600300}