1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ARM64 Specific Low-Level ACPI Boot Support 4 * 5 * Copyright (C) 2013-2014, Linaro Ltd. 6 * Author: Al Stone <al.stone@linaro.org> 7 * Author: Graeme Gregory <graeme.gregory@linaro.org> 8 * Author: Hanjun Guo <hanjun.guo@linaro.org> 9 * Author: Tomasz Nowicki <tomasz.nowicki@linaro.org> 10 * Author: Naresh Bhat <naresh.bhat@linaro.org> 11 */ 12 13 #define pr_fmt(fmt) "ACPI: " fmt 14 15 #include <linux/acpi.h> 16 #include <linux/cpumask.h> 17 #include <linux/efi.h> 18 #include <linux/efi-bgrt.h> 19 #include <linux/init.h> 20 #include <linux/irq.h> 21 #include <linux/irqdomain.h> 22 #include <linux/irq_work.h> 23 #include <linux/memblock.h> 24 #include <linux/of_fdt.h> 25 #include <linux/libfdt.h> 26 #include <linux/smp.h> 27 #include <linux/serial_core.h> 28 #include <linux/pgtable.h> 29 30 #include <acpi/ghes.h> 31 #include <asm/cputype.h> 32 #include <asm/cpu_ops.h> 33 #include <asm/daifflags.h> 34 #include <asm/smp_plat.h> 35 36 int acpi_noirq = 1; /* skip ACPI IRQ initialization */ 37 int acpi_disabled = 1; 38 EXPORT_SYMBOL(acpi_disabled); 39 40 int acpi_pci_disabled = 1; /* skip ACPI PCI scan and IRQ initialization */ 41 EXPORT_SYMBOL(acpi_pci_disabled); 42 43 static bool param_acpi_off __initdata; 44 static bool param_acpi_on __initdata; 45 static bool param_acpi_force __initdata; 46 47 static int __init parse_acpi(char *arg) 48 { 49 if (!arg) 50 return -EINVAL; 51 52 /* "acpi=off" disables both ACPI table parsing and interpreter */ 53 if (strcmp(arg, "off") == 0) 54 param_acpi_off = true; 55 else if (strcmp(arg, "on") == 0) /* prefer ACPI over DT */ 56 param_acpi_on = true; 57 else if (strcmp(arg, "force") == 0) /* force ACPI to be enabled */ 58 param_acpi_force = true; 59 else 60 return -EINVAL; /* Core will print when we return error */ 61 62 return 0; 63 } 64 early_param("acpi", parse_acpi); 65 66 static bool __init dt_is_stub(void) 67 { 68 int node; 69 70 fdt_for_each_subnode(node, initial_boot_params, 0) { 71 const char *name = fdt_get_name(initial_boot_params, node, NULL); 72 if (strcmp(name, "chosen") == 0) 73 continue; 74 if (strcmp(name, "hypervisor") == 0 && 75 of_flat_dt_is_compatible(node, "xen,xen")) 76 continue; 77 78 return false; 79 } 80 81 return true; 82 } 83 84 /* 85 * __acpi_map_table() will be called before page_init(), so early_ioremap() 86 * or early_memremap() should be called here to for ACPI table mapping. 87 */ 88 void __init __iomem *__acpi_map_table(unsigned long phys, unsigned long size) 89 { 90 if (!size) 91 return NULL; 92 93 return early_memremap(phys, size); 94 } 95 96 void __init __acpi_unmap_table(void __iomem *map, unsigned long size) 97 { 98 if (!map || !size) 99 return; 100 101 early_memunmap(map, size); 102 } 103 104 bool __init acpi_psci_present(void) 105 { 106 return acpi_gbl_FADT.arm_boot_flags & ACPI_FADT_PSCI_COMPLIANT; 107 } 108 109 /* Whether HVC must be used instead of SMC as the PSCI conduit */ 110 bool acpi_psci_use_hvc(void) 111 { 112 return acpi_gbl_FADT.arm_boot_flags & ACPI_FADT_PSCI_USE_HVC; 113 } 114 115 /* 116 * acpi_fadt_sanity_check() - Check FADT presence and carry out sanity 117 * checks on it 118 * 119 * Return 0 on success, <0 on failure 120 */ 121 static int __init acpi_fadt_sanity_check(void) 122 { 123 struct acpi_table_header *table; 124 struct acpi_table_fadt *fadt; 125 acpi_status status; 126 int ret = 0; 127 128 /* 129 * FADT is required on arm64; retrieve it to check its presence 130 * and carry out revision and ACPI HW reduced compliancy tests 131 */ 132 status = acpi_get_table(ACPI_SIG_FADT, 0, &table); 133 if (ACPI_FAILURE(status)) { 134 const char *msg = acpi_format_exception(status); 135 136 pr_err("Failed to get FADT table, %s\n", msg); 137 return -ENODEV; 138 } 139 140 fadt = (struct acpi_table_fadt *)table; 141 142 /* 143 * Revision in table header is the FADT Major revision, and there 144 * is a minor revision of FADT which was introduced by ACPI 5.1, 145 * we only deal with ACPI 5.1 or newer revision to get GIC and SMP 146 * boot protocol configuration data. 147 */ 148 if (table->revision < 5 || 149 (table->revision == 5 && fadt->minor_revision < 1)) { 150 pr_err(FW_BUG "Unsupported FADT revision %d.%d, should be 5.1+\n", 151 table->revision, fadt->minor_revision); 152 153 if (!fadt->arm_boot_flags) { 154 ret = -EINVAL; 155 goto out; 156 } 157 pr_err("FADT has ARM boot flags set, assuming 5.1\n"); 158 } 159 160 if (!(fadt->flags & ACPI_FADT_HW_REDUCED)) { 161 pr_err("FADT not ACPI hardware reduced compliant\n"); 162 ret = -EINVAL; 163 } 164 165 out: 166 /* 167 * acpi_get_table() creates FADT table mapping that 168 * should be released after parsing and before resuming boot 169 */ 170 acpi_put_table(table); 171 return ret; 172 } 173 174 /* 175 * acpi_boot_table_init() called from setup_arch(), always. 176 * 1. find RSDP and get its address, and then find XSDT 177 * 2. extract all tables and checksums them all 178 * 3. check ACPI FADT revision 179 * 4. check ACPI FADT HW reduced flag 180 * 181 * We can parse ACPI boot-time tables such as MADT after 182 * this function is called. 183 * 184 * On return ACPI is enabled if either: 185 * 186 * - ACPI tables are initialized and sanity checks passed 187 * - acpi=force was passed in the command line and ACPI was not disabled 188 * explicitly through acpi=off command line parameter 189 * 190 * ACPI is disabled on function return otherwise 191 */ 192 void __init acpi_boot_table_init(void) 193 { 194 /* 195 * Enable ACPI instead of device tree unless 196 * - ACPI has been disabled explicitly (acpi=off), or 197 * - the device tree is not empty (it has more than just a /chosen node, 198 * and a /hypervisor node when running on Xen) 199 * and ACPI has not been [force] enabled (acpi=on|force) 200 */ 201 if (param_acpi_off || 202 (!param_acpi_on && !param_acpi_force && !dt_is_stub())) 203 goto done; 204 205 /* 206 * ACPI is disabled at this point. Enable it in order to parse 207 * the ACPI tables and carry out sanity checks 208 */ 209 enable_acpi(); 210 211 /* 212 * If ACPI tables are initialized and FADT sanity checks passed, 213 * leave ACPI enabled and carry on booting; otherwise disable ACPI 214 * on initialization error. 215 * If acpi=force was passed on the command line it forces ACPI 216 * to be enabled even if its initialization failed. 217 */ 218 if (acpi_table_init() || acpi_fadt_sanity_check()) { 219 pr_err("Failed to init ACPI tables\n"); 220 if (!param_acpi_force) 221 disable_acpi(); 222 } 223 224 done: 225 if (acpi_disabled) { 226 if (earlycon_acpi_spcr_enable) 227 early_init_dt_scan_chosen_stdout(); 228 } else { 229 acpi_parse_spcr(earlycon_acpi_spcr_enable, true); 230 if (IS_ENABLED(CONFIG_ACPI_BGRT)) 231 acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt); 232 } 233 } 234 235 static pgprot_t __acpi_get_writethrough_mem_attribute(void) 236 { 237 /* 238 * Although UEFI specifies the use of Normal Write-through for 239 * EFI_MEMORY_WT, it is seldom used in practice and not implemented 240 * by most (all?) CPUs. Rather than allocate a MAIR just for this 241 * purpose, emit a warning and use Normal Non-cacheable instead. 242 */ 243 pr_warn_once("No MAIR allocation for EFI_MEMORY_WT; treating as Normal Non-cacheable\n"); 244 return __pgprot(PROT_NORMAL_NC); 245 } 246 247 pgprot_t __acpi_get_mem_attribute(phys_addr_t addr) 248 { 249 /* 250 * According to "Table 8 Map: EFI memory types to AArch64 memory 251 * types" of UEFI 2.5 section 2.3.6.1, each EFI memory type is 252 * mapped to a corresponding MAIR attribute encoding. 253 * The EFI memory attribute advises all possible capabilities 254 * of a memory region. 255 */ 256 257 u64 attr; 258 259 attr = efi_mem_attributes(addr); 260 if (attr & EFI_MEMORY_WB) 261 return PAGE_KERNEL; 262 if (attr & EFI_MEMORY_WC) 263 return __pgprot(PROT_NORMAL_NC); 264 if (attr & EFI_MEMORY_WT) 265 return __acpi_get_writethrough_mem_attribute(); 266 return __pgprot(PROT_DEVICE_nGnRnE); 267 } 268 269 void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size) 270 { 271 efi_memory_desc_t *md, *region = NULL; 272 pgprot_t prot; 273 274 if (WARN_ON_ONCE(!efi_enabled(EFI_MEMMAP))) 275 return NULL; 276 277 for_each_efi_memory_desc(md) { 278 u64 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT); 279 280 if (phys < md->phys_addr || phys >= end) 281 continue; 282 283 if (phys + size > end) { 284 pr_warn(FW_BUG "requested region covers multiple EFI memory regions\n"); 285 return NULL; 286 } 287 region = md; 288 break; 289 } 290 291 /* 292 * It is fine for AML to remap regions that are not represented in the 293 * EFI memory map at all, as it only describes normal memory, and MMIO 294 * regions that require a virtual mapping to make them accessible to 295 * the EFI runtime services. 296 */ 297 prot = __pgprot(PROT_DEVICE_nGnRnE); 298 if (region) { 299 switch (region->type) { 300 case EFI_LOADER_CODE: 301 case EFI_LOADER_DATA: 302 case EFI_BOOT_SERVICES_CODE: 303 case EFI_BOOT_SERVICES_DATA: 304 case EFI_CONVENTIONAL_MEMORY: 305 case EFI_PERSISTENT_MEMORY: 306 if (memblock_is_map_memory(phys) || 307 !memblock_is_region_memory(phys, size)) { 308 pr_warn(FW_BUG "requested region covers kernel memory @ %pa\n", &phys); 309 return NULL; 310 } 311 /* 312 * Mapping kernel memory is permitted if the region in 313 * question is covered by a single memblock with the 314 * NOMAP attribute set: this enables the use of ACPI 315 * table overrides passed via initramfs, which are 316 * reserved in memory using arch_reserve_mem_area() 317 * below. As this particular use case only requires 318 * read access, fall through to the R/O mapping case. 319 */ 320 fallthrough; 321 322 case EFI_RUNTIME_SERVICES_CODE: 323 /* 324 * This would be unusual, but not problematic per se, 325 * as long as we take care not to create a writable 326 * mapping for executable code. 327 */ 328 prot = PAGE_KERNEL_RO; 329 break; 330 331 case EFI_ACPI_RECLAIM_MEMORY: 332 /* 333 * ACPI reclaim memory is used to pass firmware tables 334 * and other data that is intended for consumption by 335 * the OS only, which may decide it wants to reclaim 336 * that memory and use it for something else. We never 337 * do that, but we usually add it to the linear map 338 * anyway, in which case we should use the existing 339 * mapping. 340 */ 341 if (memblock_is_map_memory(phys)) 342 return (void __iomem *)__phys_to_virt(phys); 343 fallthrough; 344 345 default: 346 if (region->attribute & EFI_MEMORY_WB) 347 prot = PAGE_KERNEL; 348 else if (region->attribute & EFI_MEMORY_WC) 349 prot = __pgprot(PROT_NORMAL_NC); 350 else if (region->attribute & EFI_MEMORY_WT) 351 prot = __acpi_get_writethrough_mem_attribute(); 352 } 353 } 354 return __ioremap(phys, size, prot); 355 } 356 357 /* 358 * Claim Synchronous External Aborts as a firmware first notification. 359 * 360 * Used by KVM and the arch do_sea handler. 361 * @regs may be NULL when called from process context. 362 */ 363 int apei_claim_sea(struct pt_regs *regs) 364 { 365 int err = -ENOENT; 366 bool return_to_irqs_enabled; 367 unsigned long current_flags; 368 369 if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES)) 370 return err; 371 372 current_flags = local_daif_save_flags(); 373 374 /* current_flags isn't useful here as daif doesn't tell us about pNMI */ 375 return_to_irqs_enabled = !irqs_disabled_flags(arch_local_save_flags()); 376 377 if (regs) 378 return_to_irqs_enabled = interrupts_enabled(regs); 379 380 /* 381 * SEA can interrupt SError, mask it and describe this as an NMI so 382 * that APEI defers the handling. 383 */ 384 local_daif_restore(DAIF_ERRCTX); 385 nmi_enter(); 386 err = ghes_notify_sea(); 387 nmi_exit(); 388 389 /* 390 * APEI NMI-like notifications are deferred to irq_work. Unless 391 * we interrupted irqs-masked code, we can do that now. 392 */ 393 if (!err) { 394 if (return_to_irqs_enabled) { 395 local_daif_restore(DAIF_PROCCTX_NOIRQ); 396 __irq_enter(); 397 irq_work_run(); 398 __irq_exit(); 399 } else { 400 pr_warn_ratelimited("APEI work queued but not completed"); 401 err = -EINPROGRESS; 402 } 403 } 404 405 local_daif_restore(current_flags); 406 407 return err; 408 } 409 410 void arch_reserve_mem_area(acpi_physical_address addr, size_t size) 411 { 412 memblock_mark_nomap(addr, size); 413 } 414