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