Felix Held | 8602495 | 2021-02-03 23:44:28 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | /* ACPI - create the Fixed ACPI Description Tables (FADT) */ |
| 4 | |
| 5 | #include <acpi/acpi.h> |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 6 | #include <acpi/acpigen.h> |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 7 | #include <amdblocks/acpi.h> |
Felix Held | 8f7f4bf | 2022-08-03 22:10:05 +0200 | [diff] [blame] | 8 | #include <amdblocks/cppc.h> |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 9 | #include <amdblocks/cpu.h> |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 10 | #include <amdblocks/acpimmio.h> |
Raul E Rangel | 65819cd | 2021-02-16 10:37:46 -0700 | [diff] [blame] | 11 | #include <amdblocks/ioapic.h> |
| 12 | #include <arch/ioapic.h> |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 13 | #include <arch/smp/mpspec.h> |
| 14 | #include <console/console.h> |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 15 | #include <cpu/amd/cpuid.h> |
| 16 | #include <cpu/amd/msr.h> |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 17 | #include <cpu/x86/smm.h> |
| 18 | #include <soc/acpi.h> |
| 19 | #include <soc/iomap.h> |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 20 | #include <soc/msr.h> |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 21 | #include <types.h> |
| 22 | #include "chip.h" |
Felix Held | 8602495 | 2021-02-03 23:44:28 +0100 | [diff] [blame] | 23 | |
| 24 | unsigned long acpi_fill_madt(unsigned long current) |
| 25 | { |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 26 | /* create all subtables for processors */ |
Kyösti Mälkki | 66b5e1b | 2022-11-12 21:13:45 +0200 | [diff] [blame] | 27 | current = acpi_create_madt_lapics_with_nmis(current); |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 28 | |
Kyösti Mälkki | 2e65e9c | 2021-06-16 11:00:40 +0300 | [diff] [blame] | 29 | current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current, IO_APIC_ADDR); |
Raul E Rangel | 65819cd | 2021-02-16 10:37:46 -0700 | [diff] [blame] | 30 | |
Kyösti Mälkki | 2e65e9c | 2021-06-16 11:00:40 +0300 | [diff] [blame] | 31 | current += acpi_create_madt_ioapic_from_hw((acpi_madt_ioapic_t *)current, |
| 32 | GNB_IO_APIC_ADDR); |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 33 | |
Felix Held | 69a957f | 2021-06-17 15:48:25 +0200 | [diff] [blame] | 34 | /* PIT is connected to legacy IRQ 0, but IOAPIC GSI 2 */ |
| 35 | current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current, |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 36 | MP_BUS_ISA, 0, 2, |
| 37 | MP_IRQ_TRIGGER_DEFAULT | MP_IRQ_POLARITY_DEFAULT); |
Felix Held | 69a957f | 2021-06-17 15:48:25 +0200 | [diff] [blame] | 38 | /* SCI IRQ type override */ |
| 39 | current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)current, |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 40 | MP_BUS_ISA, ACPI_SCI_IRQ, ACPI_SCI_IRQ, |
| 41 | MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW); |
Raul E Rangel | 9bce1fe | 2021-02-11 11:28:52 -0700 | [diff] [blame] | 42 | current = acpi_fill_madt_irqoverride(current); |
| 43 | |
Felix Held | 8602495 | 2021-02-03 23:44:28 +0100 | [diff] [blame] | 44 | return current; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Reference section 5.2.9 Fixed ACPI Description Table (FADT) |
| 49 | * in the ACPI 3.0b specification. |
| 50 | */ |
| 51 | void acpi_fill_fadt(acpi_fadt_t *fadt) |
| 52 | { |
Jason Glenesk | fff318f | 2021-03-10 02:47:05 -0800 | [diff] [blame] | 53 | const struct soc_amd_cezanne_config *cfg = config_of_soc(); |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 54 | |
| 55 | printk(BIOS_DEBUG, "pm_base: 0x%04x\n", ACPI_IO_BASE); |
| 56 | |
| 57 | fadt->sci_int = ACPI_SCI_IRQ; |
| 58 | |
| 59 | if (permanent_smi_handler()) { |
| 60 | fadt->smi_cmd = APM_CNT; |
| 61 | fadt->acpi_enable = APM_CNT_ACPI_ENABLE; |
| 62 | fadt->acpi_disable = APM_CNT_ACPI_DISABLE; |
| 63 | } |
| 64 | |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 65 | fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK; |
| 66 | fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK; |
| 67 | fadt->pm_tmr_blk = ACPI_PM_TMR_BLK; |
| 68 | fadt->gpe0_blk = ACPI_GPE0_BLK; |
| 69 | |
| 70 | fadt->pm1_evt_len = 4; /* 32 bits */ |
| 71 | fadt->pm1_cnt_len = 2; /* 16 bits */ |
| 72 | fadt->pm_tmr_len = 4; /* 32 bits */ |
| 73 | fadt->gpe0_blk_len = 8; /* 64 bits */ |
| 74 | |
Felix Held | 164c5ed | 2022-10-18 00:11:48 +0200 | [diff] [blame] | 75 | fill_fadt_extended_pm_regs(fadt); |
| 76 | |
Felix Held | 54c80e1 | 2023-02-21 17:59:42 +0100 | [diff] [blame] | 77 | /* p_lvl2_lat and p_lvl3_lat match what the AGESA code does, but those values are |
| 78 | overridden by the _CST packages in the processor devices. */ |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 79 | fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED; |
| 80 | fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED; |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 81 | fadt->day_alrm = RTC_DATE_ALARM; |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 82 | fadt->century = RTC_ALT_CENTURY; |
Jason Glenesk | fff318f | 2021-03-10 02:47:05 -0800 | [diff] [blame] | 83 | fadt->iapc_boot_arch = cfg->common_config.fadt_boot_arch; /* legacy free default */ |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 84 | fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */ |
| 85 | ACPI_FADT_C1_SUPPORTED | |
| 86 | ACPI_FADT_S4_RTC_WAKE | |
| 87 | ACPI_FADT_32BIT_TIMER | |
| 88 | ACPI_FADT_PCI_EXPRESS_WAKE | |
| 89 | ACPI_FADT_PLATFORM_CLOCK | |
| 90 | ACPI_FADT_S4_RTC_VALID | |
| 91 | ACPI_FADT_REMOTE_POWER_ON; |
Jason Glenesk | fff318f | 2021-03-10 02:47:05 -0800 | [diff] [blame] | 92 | if (cfg->s0ix_enable) |
| 93 | fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0; |
| 94 | |
| 95 | fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */ |
Felix Held | 8602495 | 2021-02-03 23:44:28 +0100 | [diff] [blame] | 96 | } |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 97 | |
| 98 | static uint32_t get_pstate_core_freq(msr_t pstate_def) |
| 99 | { |
| 100 | uint32_t core_freq, core_freq_mul, core_freq_div; |
| 101 | bool valid_freq_divisor; |
| 102 | |
| 103 | /* Core frequency multiplier */ |
| 104 | core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK; |
| 105 | |
| 106 | /* Core frequency divisor ID */ |
| 107 | core_freq_div = |
| 108 | (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT; |
| 109 | |
| 110 | if (core_freq_div == 0) { |
| 111 | return 0; |
| 112 | } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN) |
| 113 | && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) { |
| 114 | /* Allow 1/8 integer steps for this range */ |
| 115 | valid_freq_divisor = 1; |
| 116 | } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX) |
| 117 | && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) { |
| 118 | /* Only allow 1/4 integer steps for this range */ |
| 119 | valid_freq_divisor = 1; |
| 120 | } else { |
| 121 | valid_freq_divisor = 0; |
| 122 | } |
| 123 | |
| 124 | if (valid_freq_divisor) { |
| 125 | /* 25 * core_freq_mul / (core_freq_div / 8) */ |
| 126 | core_freq = |
| 127 | ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div)); |
| 128 | } else { |
| 129 | printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n", |
| 130 | core_freq_div); |
| 131 | core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul); |
| 132 | } |
| 133 | return core_freq; |
| 134 | } |
| 135 | |
| 136 | static uint32_t get_pstate_core_power(msr_t pstate_def) |
| 137 | { |
| 138 | uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw; |
| 139 | |
| 140 | /* Core voltage ID */ |
| 141 | core_vid = |
| 142 | (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT; |
| 143 | |
| 144 | /* Current value in amps */ |
| 145 | current_value_amps = |
| 146 | (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT; |
| 147 | |
| 148 | /* Current divisor */ |
| 149 | current_divisor = |
| 150 | (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT; |
| 151 | |
| 152 | /* Voltage */ |
| 153 | if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) { |
| 154 | /* Voltage off for VID codes 0xF8 to 0xFF */ |
| 155 | voltage_in_uvolts = 0; |
| 156 | } else { |
| 157 | voltage_in_uvolts = |
| 158 | SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid); |
| 159 | } |
| 160 | |
| 161 | /* Power in mW */ |
Zheng Bao | 62cd5e8 | 2022-08-25 17:11:38 +0800 | [diff] [blame] | 162 | power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 163 | |
| 164 | switch (current_divisor) { |
| 165 | case 0: |
Zheng Bao | 62cd5e8 | 2022-08-25 17:11:38 +0800 | [diff] [blame] | 166 | power_in_mw = power_in_mw / 100L; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 167 | break; |
| 168 | case 1: |
Zheng Bao | 62cd5e8 | 2022-08-25 17:11:38 +0800 | [diff] [blame] | 169 | power_in_mw = power_in_mw / 1000L; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 170 | break; |
| 171 | case 2: |
Zheng Bao | 62cd5e8 | 2022-08-25 17:11:38 +0800 | [diff] [blame] | 172 | power_in_mw = power_in_mw / 10000L; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 173 | break; |
| 174 | case 3: |
| 175 | /* current_divisor is set to an undefined value.*/ |
| 176 | printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n"); |
| 177 | power_in_mw = 0; |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | return power_in_mw; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Populate structure describing enabled p-states and return count of enabled p-states. |
| 186 | */ |
| 187 | static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values, |
| 188 | struct acpi_xpss_sw_pstate *pstate_xpss_values) |
| 189 | { |
| 190 | msr_t pstate_def; |
| 191 | size_t pstate_count, pstate; |
| 192 | uint32_t pstate_enable, max_pstate; |
| 193 | |
| 194 | pstate_count = 0; |
| 195 | max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT; |
| 196 | |
| 197 | for (pstate = 0; pstate <= max_pstate; pstate++) { |
Felix Held | 0a46604 | 2023-02-27 23:56:39 +0100 | [diff] [blame] | 198 | pstate_def = rdmsr(PSTATE_MSR(pstate)); |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 199 | |
| 200 | pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK) |
| 201 | >> PSTATE_DEF_HI_ENABLE_SHIFT; |
| 202 | if (!pstate_enable) |
| 203 | continue; |
| 204 | |
| 205 | pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def); |
| 206 | pstate_values[pstate_count].power = get_pstate_core_power(pstate_def); |
| 207 | pstate_values[pstate_count].transition_latency = 0; |
| 208 | pstate_values[pstate_count].bus_master_latency = 0; |
| 209 | pstate_values[pstate_count].control_value = pstate; |
| 210 | pstate_values[pstate_count].status_value = pstate; |
| 211 | |
| 212 | pstate_xpss_values[pstate_count].core_freq = |
| 213 | (uint64_t)pstate_values[pstate_count].core_freq; |
| 214 | pstate_xpss_values[pstate_count].power = |
| 215 | (uint64_t)pstate_values[pstate_count].power; |
| 216 | pstate_xpss_values[pstate_count].transition_latency = 0; |
| 217 | pstate_xpss_values[pstate_count].bus_master_latency = 0; |
| 218 | pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate; |
| 219 | pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate; |
| 220 | pstate_count++; |
| 221 | } |
| 222 | |
| 223 | return pstate_count; |
| 224 | } |
| 225 | |
Felix Held | ceafcae | 2023-03-07 00:00:15 +0100 | [diff] [blame^] | 226 | const acpi_cstate_t cstate_cfg_table[] = { |
| 227 | [0] = { |
| 228 | .ctype = 1, |
| 229 | .latency = 1, |
| 230 | .power = 0, |
| 231 | }, |
| 232 | [1] = { |
| 233 | .ctype = 2, |
| 234 | .latency = 0x12, |
| 235 | .power = 0, |
| 236 | }, |
| 237 | [2] = { |
| 238 | .ctype = 3, |
| 239 | .latency = 350, |
| 240 | .power = 0, |
| 241 | }, |
| 242 | }; |
| 243 | |
| 244 | const acpi_cstate_t *get_cstate_config_data(size_t *size) |
| 245 | { |
| 246 | *size = ARRAY_SIZE(cstate_cfg_table); |
| 247 | return cstate_cfg_table; |
| 248 | } |
| 249 | |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 250 | void generate_cpu_entries(const struct device *device) |
| 251 | { |
| 252 | int logical_cores; |
Felix Held | ceafcae | 2023-03-07 00:00:15 +0100 | [diff] [blame^] | 253 | size_t cstate_count, pstate_count, cpu; |
| 254 | acpi_cstate_t cstate_values[MAX_CSTATE_COUNT] = { {0} }; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 255 | struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} }; |
| 256 | struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} }; |
Felix Held | c3fec86 | 2023-01-28 04:15:57 +0100 | [diff] [blame] | 257 | uint32_t threads_per_core; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 258 | |
| 259 | const acpi_addr_t perf_ctrl = { |
| 260 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 261 | .bit_width = 64, |
| 262 | .addrl = PS_CTL_REG, |
| 263 | }; |
| 264 | const acpi_addr_t perf_sts = { |
| 265 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 266 | .bit_width = 64, |
| 267 | .addrl = PS_STS_REG, |
| 268 | }; |
| 269 | |
Felix Held | d4b5ad0 | 2022-01-25 04:14:05 +0100 | [diff] [blame] | 270 | threads_per_core = get_threads_per_core(); |
Felix Held | ceafcae | 2023-03-07 00:00:15 +0100 | [diff] [blame^] | 271 | cstate_count = get_cstate_info(cstate_values); |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 272 | pstate_count = get_pstate_info(pstate_values, pstate_xpss_values); |
| 273 | logical_cores = get_cpu_count(); |
| 274 | |
| 275 | for (cpu = 0; cpu < logical_cores; cpu++) { |
Felix Held | c3fec86 | 2023-01-28 04:15:57 +0100 | [diff] [blame] | 276 | acpigen_write_processor_device(cpu); |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 277 | |
| 278 | acpigen_write_pct_package(&perf_ctrl, &perf_sts); |
| 279 | |
| 280 | acpigen_write_pss_object(pstate_values, pstate_count); |
| 281 | |
| 282 | acpigen_write_xpss_object(pstate_xpss_values, pstate_count); |
| 283 | |
| 284 | if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT)) |
| 285 | acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core, |
| 286 | HW_ALL); |
| 287 | else |
| 288 | acpigen_write_PSD_package(0, logical_cores, SW_ALL); |
| 289 | |
| 290 | acpigen_write_PPC(0); |
| 291 | |
Felix Held | ceafcae | 2023-03-07 00:00:15 +0100 | [diff] [blame^] | 292 | acpigen_write_CST_package(cstate_values, cstate_count); |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 293 | |
| 294 | acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core, |
| 295 | CSD_HW_ALL, 0); |
| 296 | |
Julian Schroeder | 577e146 | 2021-07-09 16:10:08 -0500 | [diff] [blame] | 297 | generate_cppc_entries(cpu); |
| 298 | |
Felix Held | c3fec86 | 2023-01-28 04:15:57 +0100 | [diff] [blame] | 299 | acpigen_write_processor_device_end(); |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | acpigen_write_processor_package("PPKG", 0, logical_cores); |
| 303 | } |