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 | |
| 65 | fadt->pstate_cnt = 0; |
| 66 | |
| 67 | fadt->pm1a_evt_blk = ACPI_PM_EVT_BLK; |
| 68 | fadt->pm1a_cnt_blk = ACPI_PM1_CNT_BLK; |
| 69 | fadt->pm_tmr_blk = ACPI_PM_TMR_BLK; |
| 70 | fadt->gpe0_blk = ACPI_GPE0_BLK; |
| 71 | |
| 72 | fadt->pm1_evt_len = 4; /* 32 bits */ |
| 73 | fadt->pm1_cnt_len = 2; /* 16 bits */ |
| 74 | fadt->pm_tmr_len = 4; /* 32 bits */ |
| 75 | fadt->gpe0_blk_len = 8; /* 64 bits */ |
| 76 | |
Felix Held | 164c5ed | 2022-10-18 00:11:48 +0200 | [diff] [blame] | 77 | fill_fadt_extended_pm_regs(fadt); |
| 78 | |
Felix Held | 54c80e1 | 2023-02-21 17:59:42 +0100 | [diff] [blame^] | 79 | /* p_lvl2_lat and p_lvl3_lat match what the AGESA code does, but those values are |
| 80 | overridden by the _CST packages in the processor devices. */ |
Raul E Rangel | 12c6a58 | 2021-02-10 16:45:49 -0700 | [diff] [blame] | 81 | fadt->p_lvl2_lat = ACPI_FADT_C2_NOT_SUPPORTED; |
| 82 | fadt->p_lvl3_lat = ACPI_FADT_C3_NOT_SUPPORTED; |
| 83 | fadt->duty_offset = 0; /* Not supported */ |
| 84 | fadt->duty_width = 0; /* Not supported */ |
| 85 | fadt->day_alrm = RTC_DATE_ALARM; |
| 86 | fadt->mon_alrm = 0; |
| 87 | fadt->century = RTC_ALT_CENTURY; |
Jason Glenesk | fff318f | 2021-03-10 02:47:05 -0800 | [diff] [blame] | 88 | 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] | 89 | fadt->flags |= ACPI_FADT_WBINVD | /* See table 5-34 ACPI 6.3 spec */ |
| 90 | ACPI_FADT_C1_SUPPORTED | |
| 91 | ACPI_FADT_S4_RTC_WAKE | |
| 92 | ACPI_FADT_32BIT_TIMER | |
| 93 | ACPI_FADT_PCI_EXPRESS_WAKE | |
| 94 | ACPI_FADT_PLATFORM_CLOCK | |
| 95 | ACPI_FADT_S4_RTC_VALID | |
| 96 | ACPI_FADT_REMOTE_POWER_ON; |
Jason Glenesk | fff318f | 2021-03-10 02:47:05 -0800 | [diff] [blame] | 97 | if (cfg->s0ix_enable) |
| 98 | fadt->flags |= ACPI_FADT_LOW_PWR_IDLE_S0; |
| 99 | |
| 100 | fadt->flags |= cfg->common_config.fadt_flags; /* additional board-specific flags */ |
Felix Held | 8602495 | 2021-02-03 23:44:28 +0100 | [diff] [blame] | 101 | } |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 102 | |
| 103 | static uint32_t get_pstate_core_freq(msr_t pstate_def) |
| 104 | { |
| 105 | uint32_t core_freq, core_freq_mul, core_freq_div; |
| 106 | bool valid_freq_divisor; |
| 107 | |
| 108 | /* Core frequency multiplier */ |
| 109 | core_freq_mul = pstate_def.lo & PSTATE_DEF_LO_FREQ_MUL_MASK; |
| 110 | |
| 111 | /* Core frequency divisor ID */ |
| 112 | core_freq_div = |
| 113 | (pstate_def.lo & PSTATE_DEF_LO_FREQ_DIV_MASK) >> PSTATE_DEF_LO_FREQ_DIV_SHIFT; |
| 114 | |
| 115 | if (core_freq_div == 0) { |
| 116 | return 0; |
| 117 | } else if ((core_freq_div >= PSTATE_DEF_LO_FREQ_DIV_MIN) |
| 118 | && (core_freq_div <= PSTATE_DEF_LO_EIGHTH_STEP_MAX)) { |
| 119 | /* Allow 1/8 integer steps for this range */ |
| 120 | valid_freq_divisor = 1; |
| 121 | } else if ((core_freq_div > PSTATE_DEF_LO_EIGHTH_STEP_MAX) |
| 122 | && (core_freq_div <= PSTATE_DEF_LO_FREQ_DIV_MAX) && !(core_freq_div & 0x1)) { |
| 123 | /* Only allow 1/4 integer steps for this range */ |
| 124 | valid_freq_divisor = 1; |
| 125 | } else { |
| 126 | valid_freq_divisor = 0; |
| 127 | } |
| 128 | |
| 129 | if (valid_freq_divisor) { |
| 130 | /* 25 * core_freq_mul / (core_freq_div / 8) */ |
| 131 | core_freq = |
| 132 | ((PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul * 8) / (core_freq_div)); |
| 133 | } else { |
| 134 | printk(BIOS_WARNING, "Undefined core_freq_div %x used. Force to 1.\n", |
| 135 | core_freq_div); |
| 136 | core_freq = (PSTATE_DEF_LO_CORE_FREQ_BASE * core_freq_mul); |
| 137 | } |
| 138 | return core_freq; |
| 139 | } |
| 140 | |
| 141 | static uint32_t get_pstate_core_power(msr_t pstate_def) |
| 142 | { |
| 143 | uint32_t voltage_in_uvolts, core_vid, current_value_amps, current_divisor, power_in_mw; |
| 144 | |
| 145 | /* Core voltage ID */ |
| 146 | core_vid = |
| 147 | (pstate_def.lo & PSTATE_DEF_LO_CORE_VID_MASK) >> PSTATE_DEF_LO_CORE_VID_SHIFT; |
| 148 | |
| 149 | /* Current value in amps */ |
| 150 | current_value_amps = |
| 151 | (pstate_def.lo & PSTATE_DEF_LO_CUR_VAL_MASK) >> PSTATE_DEF_LO_CUR_VAL_SHIFT; |
| 152 | |
| 153 | /* Current divisor */ |
| 154 | current_divisor = |
| 155 | (pstate_def.lo & PSTATE_DEF_LO_CUR_DIV_MASK) >> PSTATE_DEF_LO_CUR_DIV_SHIFT; |
| 156 | |
| 157 | /* Voltage */ |
| 158 | if ((core_vid >= 0xF8) && (core_vid <= 0xFF)) { |
| 159 | /* Voltage off for VID codes 0xF8 to 0xFF */ |
| 160 | voltage_in_uvolts = 0; |
| 161 | } else { |
| 162 | voltage_in_uvolts = |
| 163 | SERIAL_VID_MAX_MICROVOLTS - (SERIAL_VID_DECODE_MICROVOLTS * core_vid); |
| 164 | } |
| 165 | |
| 166 | /* Power in mW */ |
Zheng Bao | 62cd5e8 | 2022-08-25 17:11:38 +0800 | [diff] [blame] | 167 | power_in_mw = (voltage_in_uvolts) / 10 * current_value_amps; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 168 | |
| 169 | switch (current_divisor) { |
| 170 | case 0: |
Zheng Bao | 62cd5e8 | 2022-08-25 17:11:38 +0800 | [diff] [blame] | 171 | power_in_mw = power_in_mw / 100L; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 172 | break; |
| 173 | case 1: |
Zheng Bao | 62cd5e8 | 2022-08-25 17:11:38 +0800 | [diff] [blame] | 174 | power_in_mw = power_in_mw / 1000L; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 175 | break; |
| 176 | case 2: |
Zheng Bao | 62cd5e8 | 2022-08-25 17:11:38 +0800 | [diff] [blame] | 177 | power_in_mw = power_in_mw / 10000L; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 178 | break; |
| 179 | case 3: |
| 180 | /* current_divisor is set to an undefined value.*/ |
| 181 | printk(BIOS_WARNING, "Undefined current_divisor set for enabled P-state .\n"); |
| 182 | power_in_mw = 0; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | return power_in_mw; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Populate structure describing enabled p-states and return count of enabled p-states. |
| 191 | */ |
| 192 | static size_t get_pstate_info(struct acpi_sw_pstate *pstate_values, |
| 193 | struct acpi_xpss_sw_pstate *pstate_xpss_values) |
| 194 | { |
| 195 | msr_t pstate_def; |
| 196 | size_t pstate_count, pstate; |
| 197 | uint32_t pstate_enable, max_pstate; |
| 198 | |
| 199 | pstate_count = 0; |
| 200 | max_pstate = (rdmsr(PS_LIM_REG).lo & PS_LIM_MAX_VAL_MASK) >> PS_MAX_VAL_SHFT; |
| 201 | |
| 202 | for (pstate = 0; pstate <= max_pstate; pstate++) { |
| 203 | pstate_def = rdmsr(PSTATE_0_MSR + pstate); |
| 204 | |
| 205 | pstate_enable = (pstate_def.hi & PSTATE_DEF_HI_ENABLE_MASK) |
| 206 | >> PSTATE_DEF_HI_ENABLE_SHIFT; |
| 207 | if (!pstate_enable) |
| 208 | continue; |
| 209 | |
| 210 | pstate_values[pstate_count].core_freq = get_pstate_core_freq(pstate_def); |
| 211 | pstate_values[pstate_count].power = get_pstate_core_power(pstate_def); |
| 212 | pstate_values[pstate_count].transition_latency = 0; |
| 213 | pstate_values[pstate_count].bus_master_latency = 0; |
| 214 | pstate_values[pstate_count].control_value = pstate; |
| 215 | pstate_values[pstate_count].status_value = pstate; |
| 216 | |
| 217 | pstate_xpss_values[pstate_count].core_freq = |
| 218 | (uint64_t)pstate_values[pstate_count].core_freq; |
| 219 | pstate_xpss_values[pstate_count].power = |
| 220 | (uint64_t)pstate_values[pstate_count].power; |
| 221 | pstate_xpss_values[pstate_count].transition_latency = 0; |
| 222 | pstate_xpss_values[pstate_count].bus_master_latency = 0; |
| 223 | pstate_xpss_values[pstate_count].control_value = (uint64_t)pstate; |
| 224 | pstate_xpss_values[pstate_count].status_value = (uint64_t)pstate; |
| 225 | pstate_count++; |
| 226 | } |
| 227 | |
| 228 | return pstate_count; |
| 229 | } |
| 230 | |
| 231 | void generate_cpu_entries(const struct device *device) |
| 232 | { |
| 233 | int logical_cores; |
Felix Held | c3fec86 | 2023-01-28 04:15:57 +0100 | [diff] [blame] | 234 | size_t pstate_count, cpu; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 235 | struct acpi_sw_pstate pstate_values[MAX_PSTATES] = { {0} }; |
| 236 | struct acpi_xpss_sw_pstate pstate_xpss_values[MAX_PSTATES] = { {0} }; |
Felix Held | c3fec86 | 2023-01-28 04:15:57 +0100 | [diff] [blame] | 237 | uint32_t threads_per_core; |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 238 | uint32_t cstate_base_address = |
| 239 | rdmsr(MSR_CSTATE_ADDRESS).lo & MSR_CSTATE_ADDRESS_MASK; |
| 240 | |
| 241 | const acpi_addr_t perf_ctrl = { |
| 242 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 243 | .bit_width = 64, |
| 244 | .addrl = PS_CTL_REG, |
| 245 | }; |
| 246 | const acpi_addr_t perf_sts = { |
| 247 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 248 | .bit_width = 64, |
| 249 | .addrl = PS_STS_REG, |
| 250 | }; |
| 251 | |
Angel Pons | d2794ce | 2021-10-17 12:59:43 +0200 | [diff] [blame] | 252 | const acpi_cstate_t cstate_info[] = { |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 253 | [0] = { |
| 254 | .ctype = 1, |
| 255 | .latency = 1, |
| 256 | .power = 0, |
| 257 | .resource = { |
| 258 | .space_id = ACPI_ADDRESS_SPACE_FIXED, |
| 259 | .bit_width = 2, |
| 260 | .bit_offset = 2, |
| 261 | .addrl = 0, |
| 262 | .addrh = 0, |
| 263 | }, |
| 264 | }, |
| 265 | [1] = { |
| 266 | .ctype = 2, |
| 267 | .latency = 0x12, |
| 268 | .power = 0, |
| 269 | .resource = { |
| 270 | .space_id = ACPI_ADDRESS_SPACE_IO, |
| 271 | .bit_width = 8, |
| 272 | .bit_offset = 0, |
| 273 | .addrl = cstate_base_address + 1, |
| 274 | .addrh = 0, |
| 275 | .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS, |
| 276 | }, |
| 277 | }, |
Raul E Rangel | d8956f7 | 2021-04-19 17:00:58 -0600 | [diff] [blame] | 278 | [2] = { |
| 279 | .ctype = 3, |
| 280 | .latency = 350, |
| 281 | .power = 0, |
| 282 | .resource = { |
| 283 | .space_id = ACPI_ADDRESS_SPACE_IO, |
| 284 | .bit_width = 8, |
| 285 | .bit_offset = 0, |
| 286 | .addrl = cstate_base_address + 2, |
| 287 | .addrh = 0, |
| 288 | .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS, |
| 289 | }, |
| 290 | }, |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 291 | }; |
| 292 | |
Felix Held | d4b5ad0 | 2022-01-25 04:14:05 +0100 | [diff] [blame] | 293 | threads_per_core = get_threads_per_core(); |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 294 | pstate_count = get_pstate_info(pstate_values, pstate_xpss_values); |
| 295 | logical_cores = get_cpu_count(); |
| 296 | |
| 297 | for (cpu = 0; cpu < logical_cores; cpu++) { |
Felix Held | c3fec86 | 2023-01-28 04:15:57 +0100 | [diff] [blame] | 298 | acpigen_write_processor_device(cpu); |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 299 | |
| 300 | acpigen_write_pct_package(&perf_ctrl, &perf_sts); |
| 301 | |
| 302 | acpigen_write_pss_object(pstate_values, pstate_count); |
| 303 | |
| 304 | acpigen_write_xpss_object(pstate_xpss_values, pstate_count); |
| 305 | |
| 306 | if (CONFIG(ACPI_SSDT_PSD_INDEPENDENT)) |
| 307 | acpigen_write_PSD_package(cpu / threads_per_core, threads_per_core, |
| 308 | HW_ALL); |
| 309 | else |
| 310 | acpigen_write_PSD_package(0, logical_cores, SW_ALL); |
| 311 | |
| 312 | acpigen_write_PPC(0); |
| 313 | |
| 314 | acpigen_write_CST_package(cstate_info, ARRAY_SIZE(cstate_info)); |
| 315 | |
| 316 | acpigen_write_CSD_package(cpu / threads_per_core, threads_per_core, |
| 317 | CSD_HW_ALL, 0); |
| 318 | |
Julian Schroeder | 577e146 | 2021-07-09 16:10:08 -0500 | [diff] [blame] | 319 | generate_cppc_entries(cpu); |
| 320 | |
Felix Held | c3fec86 | 2023-01-28 04:15:57 +0100 | [diff] [blame] | 321 | acpigen_write_processor_device_end(); |
Jason Glenesk | 79542fa | 2021-03-10 03:50:57 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | acpigen_write_processor_package("PPKG", 0, logical_cores); |
| 325 | } |