acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/cpumask.h>
  15. #include <linux/efi.h>
  16. #include <linux/efi-bgrt.h>
  17. #include <linux/init.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/irq_work.h>
  21. #include <linux/memblock.h>
  22. #include <linux/of_fdt.h>
  23. #include <linux/smp.h>
  24. #include <linux/serial_core.h>
  25. #include <linux/pgtable.h>
  26. #include <acpi/ghes.h>
  27. #include <asm/cputype.h>
  28. #include <asm/cpu_ops.h>
  29. #include <asm/daifflags.h>
  30. #include <asm/smp_plat.h>
  31. int acpi_noirq = 1; /* skip ACPI IRQ initialization */
  32. int acpi_disabled = 1;
  33. EXPORT_SYMBOL(acpi_disabled);
  34. int acpi_pci_disabled = 1; /* skip ACPI PCI scan and IRQ initialization */
  35. EXPORT_SYMBOL(acpi_pci_disabled);
  36. static bool param_acpi_off __initdata;
  37. static bool param_acpi_on __initdata;
  38. static bool param_acpi_force __initdata;
  39. static int __init parse_acpi(char *arg)
  40. {
  41. if (!arg)
  42. return -EINVAL;
  43. /* "acpi=off" disables both ACPI table parsing and interpreter */
  44. if (strcmp(arg, "off") == 0)
  45. param_acpi_off = true;
  46. else if (strcmp(arg, "on") == 0) /* prefer ACPI over DT */
  47. param_acpi_on = true;
  48. else if (strcmp(arg, "force") == 0) /* force ACPI to be enabled */
  49. param_acpi_force = true;
  50. else
  51. return -EINVAL; /* Core will print when we return error */
  52. return 0;
  53. }
  54. early_param("acpi", parse_acpi);
  55. static int __init dt_scan_depth1_nodes(unsigned long node,
  56. const char *uname, int depth,
  57. void *data)
  58. {
  59. /*
  60. * Ignore anything not directly under the root node; we'll
  61. * catch its parent instead.
  62. */
  63. if (depth != 1)
  64. return 0;
  65. if (strcmp(uname, "chosen") == 0)
  66. return 0;
  67. if (strcmp(uname, "hypervisor") == 0 &&
  68. of_flat_dt_is_compatible(node, "xen,xen"))
  69. return 0;
  70. /*
  71. * This node at depth 1 is neither a chosen node nor a xen node,
  72. * which we do not expect.
  73. */
  74. return 1;
  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. /*
  173. * Enable ACPI instead of device tree unless
  174. * - ACPI has been disabled explicitly (acpi=off), or
  175. * - the device tree is not empty (it has more than just a /chosen node,
  176. * and a /hypervisor node when running on Xen)
  177. * and ACPI has not been [force] enabled (acpi=on|force)
  178. */
  179. if (param_acpi_off ||
  180. (!param_acpi_on && !param_acpi_force &&
  181. of_scan_flat_dt(dt_scan_depth1_nodes, NULL)))
  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. acpi_parse_spcr(earlycon_acpi_spcr_enable, true);
  206. if (IS_ENABLED(CONFIG_ACPI_BGRT))
  207. acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt);
  208. }
  209. }
  210. pgprot_t __acpi_get_mem_attribute(phys_addr_t addr)
  211. {
  212. /*
  213. * According to "Table 8 Map: EFI memory types to AArch64 memory
  214. * types" of UEFI 2.5 section 2.3.6.1, each EFI memory type is
  215. * mapped to a corresponding MAIR attribute encoding.
  216. * The EFI memory attribute advises all possible capabilities
  217. * of a memory region. We use the most efficient capability.
  218. */
  219. u64 attr;
  220. attr = efi_mem_attributes(addr);
  221. if (attr & EFI_MEMORY_WB)
  222. return PAGE_KERNEL;
  223. if (attr & EFI_MEMORY_WT)
  224. return __pgprot(PROT_NORMAL_WT);
  225. if (attr & EFI_MEMORY_WC)
  226. return __pgprot(PROT_NORMAL_NC);
  227. return __pgprot(PROT_DEVICE_nGnRnE);
  228. }
  229. void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
  230. {
  231. efi_memory_desc_t *md, *region = NULL;
  232. pgprot_t prot;
  233. if (WARN_ON_ONCE(!efi_enabled(EFI_MEMMAP)))
  234. return NULL;
  235. for_each_efi_memory_desc(md) {
  236. u64 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
  237. if (phys < md->phys_addr || phys >= end)
  238. continue;
  239. if (phys + size > end) {
  240. pr_warn(FW_BUG "requested region covers multiple EFI memory regions\n");
  241. return NULL;
  242. }
  243. region = md;
  244. break;
  245. }
  246. /*
  247. * It is fine for AML to remap regions that are not represented in the
  248. * EFI memory map at all, as it only describes normal memory, and MMIO
  249. * regions that require a virtual mapping to make them accessible to
  250. * the EFI runtime services.
  251. */
  252. prot = __pgprot(PROT_DEVICE_nGnRnE);
  253. if (region) {
  254. switch (region->type) {
  255. case EFI_LOADER_CODE:
  256. case EFI_LOADER_DATA:
  257. case EFI_BOOT_SERVICES_CODE:
  258. case EFI_BOOT_SERVICES_DATA:
  259. case EFI_CONVENTIONAL_MEMORY:
  260. case EFI_PERSISTENT_MEMORY:
  261. if (memblock_is_map_memory(phys) ||
  262. !memblock_is_region_memory(phys, size)) {
  263. pr_warn(FW_BUG "requested region covers kernel memory @ %pa\n", &phys);
  264. return NULL;
  265. }
  266. /*
  267. * Mapping kernel memory is permitted if the region in
  268. * question is covered by a single memblock with the
  269. * NOMAP attribute set: this enables the use of ACPI
  270. * table overrides passed via initramfs, which are
  271. * reserved in memory using arch_reserve_mem_area()
  272. * below. As this particular use case only requires
  273. * read access, fall through to the R/O mapping case.
  274. */
  275. fallthrough;
  276. case EFI_RUNTIME_SERVICES_CODE:
  277. /*
  278. * This would be unusual, but not problematic per se,
  279. * as long as we take care not to create a writable
  280. * mapping for executable code.
  281. */
  282. prot = PAGE_KERNEL_RO;
  283. break;
  284. case EFI_ACPI_RECLAIM_MEMORY:
  285. /*
  286. * ACPI reclaim memory is used to pass firmware tables
  287. * and other data that is intended for consumption by
  288. * the OS only, which may decide it wants to reclaim
  289. * that memory and use it for something else. We never
  290. * do that, but we usually add it to the linear map
  291. * anyway, in which case we should use the existing
  292. * mapping.
  293. */
  294. if (memblock_is_map_memory(phys))
  295. return (void __iomem *)__phys_to_virt(phys);
  296. fallthrough;
  297. default:
  298. if (region->attribute & EFI_MEMORY_WB)
  299. prot = PAGE_KERNEL;
  300. else if (region->attribute & EFI_MEMORY_WT)
  301. prot = __pgprot(PROT_NORMAL_WT);
  302. else if (region->attribute & EFI_MEMORY_WC)
  303. prot = __pgprot(PROT_NORMAL_NC);
  304. }
  305. }
  306. return __ioremap(phys, size, prot);
  307. }
  308. /*
  309. * Claim Synchronous External Aborts as a firmware first notification.
  310. *
  311. * Used by KVM and the arch do_sea handler.
  312. * @regs may be NULL when called from process context.
  313. */
  314. int apei_claim_sea(struct pt_regs *regs)
  315. {
  316. int err = -ENOENT;
  317. bool return_to_irqs_enabled;
  318. unsigned long current_flags;
  319. if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES))
  320. return err;
  321. current_flags = local_daif_save_flags();
  322. /* current_flags isn't useful here as daif doesn't tell us about pNMI */
  323. return_to_irqs_enabled = !irqs_disabled_flags(arch_local_save_flags());
  324. if (regs)
  325. return_to_irqs_enabled = interrupts_enabled(regs);
  326. /*
  327. * SEA can interrupt SError, mask it and describe this as an NMI so
  328. * that APEI defers the handling.
  329. */
  330. local_daif_restore(DAIF_ERRCTX);
  331. nmi_enter();
  332. err = ghes_notify_sea();
  333. nmi_exit();
  334. /*
  335. * APEI NMI-like notifications are deferred to irq_work. Unless
  336. * we interrupted irqs-masked code, we can do that now.
  337. */
  338. if (!err) {
  339. if (return_to_irqs_enabled) {
  340. local_daif_restore(DAIF_PROCCTX_NOIRQ);
  341. __irq_enter();
  342. irq_work_run();
  343. __irq_exit();
  344. } else {
  345. pr_warn_ratelimited("APEI work queued but not completed");
  346. err = -EINPROGRESS;
  347. }
  348. }
  349. local_daif_restore(current_flags);
  350. return err;
  351. }
  352. void arch_reserve_mem_area(acpi_physical_address addr, size_t size)
  353. {
  354. memblock_mark_nomap(addr, size);
  355. }