Simon Glass | d73344b | 2020-09-22 12:45:14 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Generic Intel ACPI table generation |
| 4 | * |
| 5 | * Copyright (C) 2017 Intel Corp. |
| 6 | * Copyright 2019 Google LLC |
| 7 | * |
| 8 | * Modified from coreboot src/soc/intel/common/block/acpi.c |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <bloblist.h> |
| 13 | #include <cpu.h> |
| 14 | #include <dm.h> |
| 15 | #include <acpi/acpigen.h> |
| 16 | #include <asm/acpigen.h> |
| 17 | #include <asm/acpi_table.h> |
| 18 | #include <asm/cpu.h> |
| 19 | #include <asm/cpu_common.h> |
| 20 | #include <asm/intel_acpi.h> |
| 21 | #include <asm/ioapic.h> |
| 22 | #include <asm/mpspec.h> |
| 23 | #include <asm/smm.h> |
| 24 | #include <asm/turbo.h> |
| 25 | #include <asm/intel_gnvs.h> |
| 26 | #include <asm/arch/iomap.h> |
| 27 | #include <asm/arch/pm.h> |
| 28 | #include <asm/arch/systemagent.h> |
| 29 | #include <dm/acpi.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <power/acpi_pmc.h> |
| 32 | |
| 33 | u32 acpi_fill_mcfg(u32 current) |
| 34 | { |
| 35 | /* PCI Segment Group 0, Start Bus Number 0, End Bus Number is 255 */ |
| 36 | current += acpi_create_mcfg_mmconfig((void *)current, |
| 37 | CONFIG_MMCONF_BASE_ADDRESS, 0, 0, |
| 38 | (CONFIG_SA_PCIEX_LENGTH >> 20) |
| 39 | - 1); |
| 40 | return current; |
| 41 | } |
| 42 | |
| 43 | static int acpi_sci_irq(void) |
| 44 | { |
| 45 | int sci_irq = 9; |
| 46 | uint scis; |
| 47 | int ret; |
| 48 | |
| 49 | ret = arch_read_sci_irq_select(); |
| 50 | if (IS_ERR_VALUE(ret)) |
| 51 | return log_msg_ret("sci_irq", ret); |
| 52 | scis = ret; |
| 53 | scis &= SCI_IRQ_MASK; |
| 54 | scis >>= SCI_IRQ_SHIFT; |
| 55 | |
| 56 | /* Determine how SCI is routed. */ |
| 57 | switch (scis) { |
| 58 | case SCIS_IRQ9: |
| 59 | case SCIS_IRQ10: |
| 60 | case SCIS_IRQ11: |
| 61 | sci_irq = scis - SCIS_IRQ9 + 9; |
| 62 | break; |
| 63 | case SCIS_IRQ20: |
| 64 | case SCIS_IRQ21: |
| 65 | case SCIS_IRQ22: |
| 66 | case SCIS_IRQ23: |
| 67 | sci_irq = scis - SCIS_IRQ20 + 20; |
| 68 | break; |
| 69 | default: |
| 70 | log_warning("Invalid SCI route! Defaulting to IRQ9\n"); |
| 71 | sci_irq = 9; |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | log_debug("SCI is IRQ%d\n", sci_irq); |
| 76 | |
| 77 | return sci_irq; |
| 78 | } |
| 79 | |
| 80 | static unsigned long acpi_madt_irq_overrides(unsigned long current) |
| 81 | { |
| 82 | int sci = acpi_sci_irq(); |
| 83 | u16 flags = MP_IRQ_TRIGGER_LEVEL; |
| 84 | |
| 85 | if (sci < 0) |
| 86 | return log_msg_ret("sci irq", sci); |
| 87 | |
| 88 | /* INT_SRC_OVR */ |
| 89 | current += acpi_create_madt_irqoverride((void *)current, 0, 0, 2, 0); |
| 90 | |
| 91 | flags |= arch_madt_sci_irq_polarity(sci); |
| 92 | |
| 93 | /* SCI */ |
| 94 | current += |
| 95 | acpi_create_madt_irqoverride((void *)current, 0, sci, sci, flags); |
| 96 | |
| 97 | return current; |
| 98 | } |
| 99 | |
| 100 | u32 acpi_fill_madt(u32 current) |
| 101 | { |
| 102 | /* Local APICs */ |
| 103 | current += acpi_create_madt_lapics(current); |
| 104 | |
| 105 | /* IOAPIC */ |
| 106 | current += acpi_create_madt_ioapic((void *)current, 2, IO_APIC_ADDR, 0); |
| 107 | |
| 108 | return acpi_madt_irq_overrides(current); |
| 109 | } |
| 110 | |
| 111 | void intel_acpi_fill_fadt(struct acpi_fadt *fadt) |
| 112 | { |
| 113 | const u16 pmbase = IOMAP_ACPI_BASE; |
| 114 | |
| 115 | /* Use ACPI 3.0 revision. */ |
| 116 | fadt->header.revision = acpi_get_table_revision(ACPITAB_FADT); |
| 117 | |
| 118 | fadt->sci_int = acpi_sci_irq(); |
| 119 | fadt->smi_cmd = APM_CNT; |
| 120 | fadt->acpi_enable = APM_CNT_ACPI_ENABLE; |
| 121 | fadt->acpi_disable = APM_CNT_ACPI_DISABLE; |
| 122 | fadt->s4bios_req = 0x0; |
| 123 | fadt->pstate_cnt = 0; |
| 124 | |
| 125 | fadt->pm1a_evt_blk = pmbase + PM1_STS; |
| 126 | fadt->pm1b_evt_blk = 0x0; |
| 127 | fadt->pm1a_cnt_blk = pmbase + PM1_CNT; |
| 128 | fadt->pm1b_cnt_blk = 0x0; |
| 129 | |
| 130 | fadt->gpe0_blk = pmbase + GPE0_STS; |
| 131 | |
| 132 | fadt->pm1_evt_len = 4; |
| 133 | fadt->pm1_cnt_len = 2; |
| 134 | |
| 135 | /* GPE0 STS/EN pairs each 32 bits wide. */ |
| 136 | fadt->gpe0_blk_len = 2 * GPE0_REG_MAX * sizeof(uint32_t); |
| 137 | |
| 138 | fadt->flush_size = 0x400; /* twice of cache size */ |
| 139 | fadt->flush_stride = 0x10; /* Cache line width */ |
| 140 | fadt->duty_offset = 1; |
| 141 | fadt->day_alrm = 0xd; |
| 142 | |
| 143 | fadt->flags = ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED | |
| 144 | ACPI_FADT_C2_MP_SUPPORTED | ACPI_FADT_SLEEP_BUTTON | |
| 145 | ACPI_FADT_RESET_REGISTER | ACPI_FADT_SEALED_CASE | |
| 146 | ACPI_FADT_S4_RTC_WAKE | ACPI_FADT_PLATFORM_CLOCK; |
| 147 | |
| 148 | fadt->reset_reg.space_id = 1; |
| 149 | fadt->reset_reg.bit_width = 8; |
| 150 | fadt->reset_reg.addrl = IO_PORT_RESET; |
| 151 | fadt->reset_value = RST_CPU | SYS_RST; |
| 152 | |
| 153 | fadt->x_pm1a_evt_blk.space_id = 1; |
| 154 | fadt->x_pm1a_evt_blk.bit_width = fadt->pm1_evt_len * 8; |
| 155 | fadt->x_pm1a_evt_blk.addrl = pmbase + PM1_STS; |
| 156 | |
| 157 | fadt->x_pm1b_evt_blk.space_id = 1; |
| 158 | |
| 159 | fadt->x_pm1a_cnt_blk.space_id = 1; |
| 160 | fadt->x_pm1a_cnt_blk.bit_width = fadt->pm1_cnt_len * 8; |
| 161 | fadt->x_pm1a_cnt_blk.addrl = pmbase + PM1_CNT; |
| 162 | |
| 163 | fadt->x_pm1b_cnt_blk.space_id = 1; |
| 164 | |
| 165 | fadt->x_gpe1_blk.space_id = 1; |
| 166 | } |
| 167 | |
| 168 | int intel_southbridge_write_acpi_tables(const struct udevice *dev, |
| 169 | struct acpi_ctx *ctx) |
| 170 | { |
| 171 | int ret; |
| 172 | |
| 173 | ret = acpi_write_dbg2_pci_uart(ctx, gd->cur_serial_dev, |
| 174 | ACPI_ACCESS_SIZE_DWORD_ACCESS); |
| 175 | if (ret) |
| 176 | return log_msg_ret("dbg2", ret); |
| 177 | |
| 178 | ret = acpi_write_hpet(ctx); |
| 179 | if (ret) |
| 180 | return log_msg_ret("hpet", ret); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | __weak u32 acpi_fill_soc_wake(u32 generic_pm1_en, |
| 186 | const struct chipset_power_state *ps) |
| 187 | { |
| 188 | return generic_pm1_en; |
| 189 | } |
| 190 | |
| 191 | __weak int acpi_create_gnvs(struct acpi_global_nvs *gnvs) |
| 192 | { |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int southbridge_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx) |
| 197 | { |
| 198 | struct acpi_global_nvs *gnvs; |
| 199 | int ret; |
| 200 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 201 | ret = bloblist_ensure_size(BLOBLISTT_ACPI_GNVS, sizeof(*gnvs), 0, |
Simon Glass | d73344b | 2020-09-22 12:45:14 -0600 | [diff] [blame] | 202 | (void **)&gnvs); |
| 203 | if (ret) |
| 204 | return log_msg_ret("bloblist", ret); |
Simon Glass | d73344b | 2020-09-22 12:45:14 -0600 | [diff] [blame] | 205 | |
| 206 | ret = acpi_create_gnvs(gnvs); |
| 207 | if (ret) |
| 208 | return log_msg_ret("gnvs", ret); |
| 209 | |
| 210 | /* |
| 211 | * TODO(sjg@chromum.org): tell SMI about it |
| 212 | * smm_setup_structures(gnvs, NULL, NULL); |
| 213 | */ |
| 214 | |
| 215 | /* Add it to DSDT */ |
| 216 | acpigen_write_scope(ctx, "\\"); |
| 217 | acpigen_write_name_dword(ctx, "NVSA", (uintptr_t)gnvs); |
| 218 | acpigen_pop_len(ctx); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int calculate_power(int tdp, int p1_ratio, int ratio) |
| 224 | { |
| 225 | u32 m; |
| 226 | u32 power; |
| 227 | |
| 228 | /* |
| 229 | * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2 |
| 230 | * |
| 231 | * Power = (ratio / p1_ratio) * m * tdp |
| 232 | */ |
| 233 | |
| 234 | m = (110000 - ((p1_ratio - ratio) * 625)) / 11; |
| 235 | m = (m * m) / 1000; |
| 236 | |
| 237 | power = ((ratio * 100000 / p1_ratio) / 100); |
| 238 | power *= (m / 100) * (tdp / 1000); |
| 239 | power /= 1000; |
| 240 | |
| 241 | return power; |
| 242 | } |
| 243 | |
| 244 | void generate_p_state_entries(struct acpi_ctx *ctx, int core, |
| 245 | int cores_per_package) |
| 246 | { |
| 247 | int ratio_min, ratio_max, ratio_turbo, ratio_step; |
| 248 | int coord_type, power_max, num_entries; |
| 249 | int ratio, power, clock, clock_max; |
| 250 | bool turbo; |
| 251 | |
| 252 | coord_type = cpu_get_coord_type(); |
| 253 | ratio_min = cpu_get_min_ratio(); |
| 254 | ratio_max = cpu_get_max_ratio(); |
| 255 | clock_max = (ratio_max * cpu_get_bus_clock_khz()) / 1000; |
| 256 | turbo = (turbo_get_state() == TURBO_ENABLED); |
| 257 | |
| 258 | /* Calculate CPU TDP in mW */ |
| 259 | power_max = cpu_get_power_max(); |
| 260 | |
| 261 | /* Write _PCT indicating use of FFixedHW */ |
| 262 | acpigen_write_empty_pct(ctx); |
| 263 | |
| 264 | /* Write _PPC with no limit on supported P-state */ |
| 265 | acpigen_write_ppc_nvs(ctx); |
| 266 | /* Write PSD indicating configured coordination type */ |
| 267 | acpigen_write_psd_package(ctx, core, 1, coord_type); |
| 268 | |
| 269 | /* Add P-state entries in _PSS table */ |
| 270 | acpigen_write_name(ctx, "_PSS"); |
| 271 | |
| 272 | /* Determine ratio points */ |
| 273 | ratio_step = PSS_RATIO_STEP; |
| 274 | do { |
| 275 | num_entries = ((ratio_max - ratio_min) / ratio_step) + 1; |
| 276 | if (((ratio_max - ratio_min) % ratio_step) > 0) |
| 277 | num_entries += 1; |
| 278 | if (turbo) |
| 279 | num_entries += 1; |
| 280 | if (num_entries > PSS_MAX_ENTRIES) |
| 281 | ratio_step += 1; |
| 282 | } while (num_entries > PSS_MAX_ENTRIES); |
| 283 | |
| 284 | /* _PSS package count depends on Turbo */ |
| 285 | acpigen_write_package(ctx, num_entries); |
| 286 | |
| 287 | /* P[T] is Turbo state if enabled */ |
| 288 | if (turbo) { |
| 289 | ratio_turbo = cpu_get_max_turbo_ratio(); |
| 290 | |
| 291 | /* Add entry for Turbo ratio */ |
| 292 | acpigen_write_pss_package(ctx, clock_max + 1, /* MHz */ |
| 293 | power_max, /* mW */ |
| 294 | PSS_LATENCY_TRANSITION,/* lat1 */ |
| 295 | PSS_LATENCY_BUSMASTER,/* lat2 */ |
| 296 | ratio_turbo << 8, /* control */ |
| 297 | ratio_turbo << 8); /* status */ |
| 298 | num_entries -= 1; |
| 299 | } |
| 300 | |
| 301 | /* First regular entry is max non-turbo ratio */ |
| 302 | acpigen_write_pss_package(ctx, clock_max, /* MHz */ |
| 303 | power_max, /* mW */ |
| 304 | PSS_LATENCY_TRANSITION,/* lat1 */ |
| 305 | PSS_LATENCY_BUSMASTER,/* lat2 */ |
| 306 | ratio_max << 8, /* control */ |
| 307 | ratio_max << 8); /* status */ |
| 308 | num_entries -= 1; |
| 309 | |
| 310 | /* Generate the remaining entries */ |
| 311 | for (ratio = ratio_min + ((num_entries - 1) * ratio_step); |
| 312 | ratio >= ratio_min; ratio -= ratio_step) { |
| 313 | /* Calculate power at this ratio */ |
| 314 | power = calculate_power(power_max, ratio_max, ratio); |
| 315 | clock = (ratio * cpu_get_bus_clock_khz()) / 1000; |
| 316 | |
| 317 | acpigen_write_pss_package(ctx, clock, /* MHz */ |
| 318 | power, /* mW */ |
| 319 | PSS_LATENCY_TRANSITION,/* lat1 */ |
| 320 | PSS_LATENCY_BUSMASTER,/* lat2 */ |
| 321 | ratio << 8, /* control */ |
| 322 | ratio << 8); /* status */ |
| 323 | } |
| 324 | /* Fix package length */ |
| 325 | acpigen_pop_len(ctx); |
| 326 | } |
| 327 | |
| 328 | void generate_t_state_entries(struct acpi_ctx *ctx, int core, |
| 329 | int cores_per_package, struct acpi_tstate *entry, |
| 330 | int nentries) |
| 331 | { |
| 332 | if (!nentries) |
| 333 | return; |
| 334 | |
| 335 | /* Indicate SW_ALL coordination for T-states */ |
| 336 | acpigen_write_tsd_package(ctx, core, cores_per_package, SW_ALL); |
| 337 | |
| 338 | /* Indicate FixedHW so OS will use MSR */ |
| 339 | acpigen_write_empty_ptc(ctx); |
| 340 | |
| 341 | /* Set NVS controlled T-state limit */ |
| 342 | acpigen_write_tpc(ctx, "\\TLVL"); |
| 343 | |
| 344 | /* Write TSS table for MSR access */ |
| 345 | acpigen_write_tss_package(ctx, entry, nentries); |
| 346 | } |
| 347 | |
| 348 | int acpi_generate_cpu_header(struct acpi_ctx *ctx, int core_id, |
| 349 | const struct acpi_cstate *c_state_map, |
| 350 | int num_cstates) |
| 351 | { |
| 352 | bool is_first = !core_id; |
| 353 | |
| 354 | /* Generate processor \_PR.CPUx */ |
| 355 | acpigen_write_processor(ctx, core_id, is_first ? ACPI_BASE_ADDRESS : 0, |
| 356 | is_first ? 6 : 0); |
| 357 | |
| 358 | /* Generate C-state tables */ |
| 359 | acpigen_write_cst_package(ctx, c_state_map, num_cstates); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | int acpi_generate_cpu_package_final(struct acpi_ctx *ctx, int cores_per_package) |
| 365 | { |
| 366 | /* |
| 367 | * PPKG is usually used for thermal management of the first and only |
| 368 | * package |
| 369 | */ |
| 370 | acpigen_write_processor_package(ctx, "PPKG", 0, cores_per_package); |
| 371 | |
| 372 | /* Add a method to notify processor nodes */ |
| 373 | acpigen_write_processor_cnot(ctx, cores_per_package); |
| 374 | |
| 375 | return 0; |
| 376 | } |