1 // SPDX-License-Identifier: GPL-2.0-only
3 * ARM64 Specific Low-Level ACPI Boot Support
5 * Copyright (C) 2013-2014, Linaro Ltd.
13 #define pr_fmt(fmt) "ACPI: " fmt
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>
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>
36 int acpi_noirq = 1; /* skip ACPI IRQ initialization */
37 int acpi_disabled = 1;
38 EXPORT_SYMBOL(acpi_disabled);
40 int acpi_pci_disabled = 1; /* skip ACPI PCI scan and IRQ initialization */
41 EXPORT_SYMBOL(acpi_pci_disabled);
43 static bool param_acpi_off __initdata;
44 static bool param_acpi_on __initdata;
45 static bool param_acpi_force __initdata;
47 static int __init parse_acpi(char *arg)
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 */
57 else if (strcmp(arg, "force") == 0) /* force ACPI to be enabled */
58 param_acpi_force = true;
60 return -EINVAL; /* Core will print when we return error */
64 early_param("acpi", parse_acpi);
66 static bool __init dt_is_stub(void)
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)
74 if (strcmp(name, "hypervisor") == 0 &&
75 of_flat_dt_is_compatible(node, "xen,xen"))
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.
88 void __init __iomem *__acpi_map_table(unsigned long phys, unsigned long size)
93 return early_memremap(phys, size);
96 void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
101 early_memunmap(map, size);
104 bool __init acpi_psci_present(void)
106 return acpi_gbl_FADT.arm_boot_flags & ACPI_FADT_PSCI_COMPLIANT;
109 /* Whether HVC must be used instead of SMC as the PSCI conduit */
110 bool acpi_psci_use_hvc(void)
112 return acpi_gbl_FADT.arm_boot_flags & ACPI_FADT_PSCI_USE_HVC;
116 * acpi_fadt_sanity_check() - Check FADT presence and carry out sanity
119 * Return 0 on success, <0 on failure
121 static int __init acpi_fadt_sanity_check(void)
123 struct acpi_table_header *table;
124 struct acpi_table_fadt *fadt;
129 * FADT is required on arm64; retrieve it to check its presence
130 * and carry out revision and ACPI HW reduced compliancy tests
132 status = acpi_get_table(ACPI_SIG_FADT, 0, &table);
133 if (ACPI_FAILURE(status)) {
134 const char *msg = acpi_format_exception(status);
136 pr_err("Failed to get FADT table, %s\n", msg);
140 fadt = (struct acpi_table_fadt *)table;
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.
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);
153 if (!fadt->arm_boot_flags) {
157 pr_err("FADT has ARM boot flags set, assuming 5.1\n");
160 if (!(fadt->flags & ACPI_FADT_HW_REDUCED)) {
161 pr_err("FADT not ACPI hardware reduced compliant\n");
167 * acpi_get_table() creates FADT table mapping that
168 * should be released after parsing and before resuming boot
170 acpi_put_table(table);
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
181 * We can parse ACPI boot-time tables such as MADT after
182 * this function is called.
184 * On return ACPI is enabled if either:
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
190 * ACPI is disabled on function return otherwise
192 void __init acpi_boot_table_init(void)
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)
201 if (param_acpi_off ||
202 (!param_acpi_on && !param_acpi_force && !dt_is_stub()))
206 * ACPI is disabled at this point. Enable it in order to parse
207 * the ACPI tables and carry out sanity checks
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.
218 if (acpi_table_init() || acpi_fadt_sanity_check()) {
219 pr_err("Failed to init ACPI tables\n");
220 if (!param_acpi_force)
226 if (earlycon_acpi_spcr_enable)
227 early_init_dt_scan_chosen_stdout();
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);
235 static pgprot_t __acpi_get_writethrough_mem_attribute(void)
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.
243 pr_warn_once("No MAIR allocation for EFI_MEMORY_WT; treating as Normal Non-cacheable\n");
244 return __pgprot(PROT_NORMAL_NC);
247 pgprot_t __acpi_get_mem_attribute(phys_addr_t addr)
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.
259 attr = efi_mem_attributes(addr);
260 if (attr & EFI_MEMORY_WB)
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);
269 void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
271 efi_memory_desc_t *md, *region = NULL;
274 if (WARN_ON_ONCE(!efi_enabled(EFI_MEMMAP)))
277 for_each_efi_memory_desc(md) {
278 u64 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
280 if (phys < md->phys_addr || phys >= end)
283 if (phys + size > end) {
284 pr_warn(FW_BUG "requested region covers multiple EFI memory regions\n");
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.
297 prot = __pgprot(PROT_DEVICE_nGnRnE);
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);
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.
322 case EFI_RUNTIME_SERVICES_CODE:
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.
328 prot = PAGE_KERNEL_RO;
331 case EFI_ACPI_RECLAIM_MEMORY:
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
341 if (memblock_is_map_memory(phys))
342 return (void __iomem *)__phys_to_virt(phys);
346 if (region->attribute & EFI_MEMORY_WB)
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();
354 return __ioremap(phys, size, prot);
358 * Claim Synchronous External Aborts as a firmware first notification.
360 * Used by KVM and the arch do_sea handler.
361 * @regs may be NULL when called from process context.
363 int apei_claim_sea(struct pt_regs *regs)
366 bool return_to_irqs_enabled;
367 unsigned long current_flags;
369 if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES))
372 current_flags = local_daif_save_flags();
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());
378 return_to_irqs_enabled = interrupts_enabled(regs);
381 * SEA can interrupt SError, mask it and describe this as an NMI so
382 * that APEI defers the handling.
384 local_daif_restore(DAIF_ERRCTX);
386 err = ghes_notify_sea();
390 * APEI NMI-like notifications are deferred to irq_work. Unless
391 * we interrupted irqs-masked code, we can do that now.
394 if (return_to_irqs_enabled) {
395 local_daif_restore(DAIF_PROCCTX_NOIRQ);
400 pr_warn_ratelimited("APEI work queued but not completed");
405 local_daif_restore(current_flags);
410 void arch_reserve_mem_area(acpi_physical_address addr, size_t size)
412 memblock_mark_nomap(addr, size);