acpi.c 12 KB

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