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); |
| 205 | memset(gnvs, '\0', sizeof(*gnvs)); |
| 206 | |
| 207 | ret = acpi_create_gnvs(gnvs); |
| 208 | if (ret) |
| 209 | return log_msg_ret("gnvs", ret); |
| 210 | |
| 211 | /* |
| 212 | * TODO(sjg@chromum.org): tell SMI about it |
| 213 | * smm_setup_structures(gnvs, NULL, NULL); |
| 214 | */ |
| 215 | |
| 216 | /* Add it to DSDT */ |
| 217 | acpigen_write_scope(ctx, "\\"); |
| 218 | acpigen_write_name_dword(ctx, "NVSA", (uintptr_t)gnvs); |
| 219 | acpigen_pop_len(ctx); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int calculate_power(int tdp, int p1_ratio, int ratio) |
| 225 | { |
| 226 | u32 m; |
| 227 | u32 power; |
| 228 | |
| 229 | /* |
| 230 | * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2 |
| 231 | * |
| 232 | * Power = (ratio / p1_ratio) * m * tdp |
| 233 | */ |
| 234 | |
| 235 | m = (110000 - ((p1_ratio - ratio) * 625)) / 11; |
| 236 | m = (m * m) / 1000; |
| 237 | |
| 238 | power = ((ratio * 100000 / p1_ratio) / 100); |
| 239 | power *= (m / 100) * (tdp / 1000); |
| 240 | power /= 1000; |
| 241 | |
| 242 | return power; |
| 243 | } |
| 244 | |
| 245 | void generate_p_state_entries(struct acpi_ctx *ctx, int core, |
| 246 | int cores_per_package) |
| 247 | { |
| 248 | int ratio_min, ratio_max, ratio_turbo, ratio_step; |
| 249 | int coord_type, power_max, num_entries; |
| 250 | int ratio, power, clock, clock_max; |
| 251 | bool turbo; |
| 252 | |
| 253 | coord_type = cpu_get_coord_type(); |
| 254 | ratio_min = cpu_get_min_ratio(); |
| 255 | ratio_max = cpu_get_max_ratio(); |
| 256 | clock_max = (ratio_max * cpu_get_bus_clock_khz()) / 1000; |
| 257 | turbo = (turbo_get_state() == TURBO_ENABLED); |
| 258 | |
| 259 | /* Calculate CPU TDP in mW */ |
| 260 | power_max = cpu_get_power_max(); |
| 261 | |
| 262 | /* Write _PCT indicating use of FFixedHW */ |
| 263 | acpigen_write_empty_pct(ctx); |
| 264 | |
| 265 | /* Write _PPC with no limit on supported P-state */ |
| 266 | acpigen_write_ppc_nvs(ctx); |
| 267 | /* Write PSD indicating configured coordination type */ |
| 268 | acpigen_write_psd_package(ctx, core, 1, coord_type); |
| 269 | |
| 270 | /* Add P-state entries in _PSS table */ |
| 271 | acpigen_write_name(ctx, "_PSS"); |
| 272 | |
| 273 | /* Determine ratio points */ |
| 274 | ratio_step = PSS_RATIO_STEP; |
| 275 | do { |
| 276 | num_entries = ((ratio_max - ratio_min) / ratio_step) + 1; |
| 277 | if (((ratio_max - ratio_min) % ratio_step) > 0) |
| 278 | num_entries += 1; |
| 279 | if (turbo) |
| 280 | num_entries += 1; |
| 281 | if (num_entries > PSS_MAX_ENTRIES) |
| 282 | ratio_step += 1; |
| 283 | } while (num_entries > PSS_MAX_ENTRIES); |
| 284 | |
| 285 | /* _PSS package count depends on Turbo */ |
| 286 | acpigen_write_package(ctx, num_entries); |
| 287 | |
| 288 | /* P[T] is Turbo state if enabled */ |
| 289 | if (turbo) { |
| 290 | ratio_turbo = cpu_get_max_turbo_ratio(); |
| 291 | |
| 292 | /* Add entry for Turbo ratio */ |
| 293 | acpigen_write_pss_package(ctx, clock_max + 1, /* MHz */ |
| 294 | power_max, /* mW */ |
| 295 | PSS_LATENCY_TRANSITION,/* lat1 */ |
| 296 | PSS_LATENCY_BUSMASTER,/* lat2 */ |
| 297 | ratio_turbo << 8, /* control */ |
| 298 | ratio_turbo << 8); /* status */ |
| 299 | num_entries -= 1; |
| 300 | } |
| 301 | |
| 302 | /* First regular entry is max non-turbo ratio */ |
| 303 | acpigen_write_pss_package(ctx, clock_max, /* MHz */ |
| 304 | power_max, /* mW */ |
| 305 | PSS_LATENCY_TRANSITION,/* lat1 */ |
| 306 | PSS_LATENCY_BUSMASTER,/* lat2 */ |
| 307 | ratio_max << 8, /* control */ |
| 308 | ratio_max << 8); /* status */ |
| 309 | num_entries -= 1; |
| 310 | |
| 311 | /* Generate the remaining entries */ |
| 312 | for (ratio = ratio_min + ((num_entries - 1) * ratio_step); |
| 313 | ratio >= ratio_min; ratio -= ratio_step) { |
| 314 | /* Calculate power at this ratio */ |
| 315 | power = calculate_power(power_max, ratio_max, ratio); |
| 316 | clock = (ratio * cpu_get_bus_clock_khz()) / 1000; |
| 317 | |
| 318 | acpigen_write_pss_package(ctx, clock, /* MHz */ |
| 319 | power, /* mW */ |
| 320 | PSS_LATENCY_TRANSITION,/* lat1 */ |
| 321 | PSS_LATENCY_BUSMASTER,/* lat2 */ |
| 322 | ratio << 8, /* control */ |
| 323 | ratio << 8); /* status */ |
| 324 | } |
| 325 | /* Fix package length */ |
| 326 | acpigen_pop_len(ctx); |
| 327 | } |
| 328 | |
| 329 | void generate_t_state_entries(struct acpi_ctx *ctx, int core, |
| 330 | int cores_per_package, struct acpi_tstate *entry, |
| 331 | int nentries) |
| 332 | { |
| 333 | if (!nentries) |
| 334 | return; |
| 335 | |
| 336 | /* Indicate SW_ALL coordination for T-states */ |
| 337 | acpigen_write_tsd_package(ctx, core, cores_per_package, SW_ALL); |
| 338 | |
| 339 | /* Indicate FixedHW so OS will use MSR */ |
| 340 | acpigen_write_empty_ptc(ctx); |
| 341 | |
| 342 | /* Set NVS controlled T-state limit */ |
| 343 | acpigen_write_tpc(ctx, "\\TLVL"); |
| 344 | |
| 345 | /* Write TSS table for MSR access */ |
| 346 | acpigen_write_tss_package(ctx, entry, nentries); |
| 347 | } |
| 348 | |
| 349 | int acpi_generate_cpu_header(struct acpi_ctx *ctx, int core_id, |
| 350 | const struct acpi_cstate *c_state_map, |
| 351 | int num_cstates) |
| 352 | { |
| 353 | bool is_first = !core_id; |
| 354 | |
| 355 | /* Generate processor \_PR.CPUx */ |
| 356 | acpigen_write_processor(ctx, core_id, is_first ? ACPI_BASE_ADDRESS : 0, |
| 357 | is_first ? 6 : 0); |
| 358 | |
| 359 | /* Generate C-state tables */ |
| 360 | acpigen_write_cst_package(ctx, c_state_map, num_cstates); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | int acpi_generate_cpu_package_final(struct acpi_ctx *ctx, int cores_per_package) |
| 366 | { |
| 367 | /* |
| 368 | * PPKG is usually used for thermal management of the first and only |
| 369 | * package |
| 370 | */ |
| 371 | acpigen_write_processor_package(ctx, "PPKG", 0, cores_per_package); |
| 372 | |
| 373 | /* Add a method to notify processor nodes */ |
| 374 | acpigen_write_processor_cnot(ctx, cores_per_package); |
| 375 | |
| 376 | return 0; |
| 377 | } |