efi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Extensible Firmware Interface
  3. *
  4. * Based on Extensible Firmware Interface Specification version 2.4
  5. *
  6. * Copyright (C) 2013, 2014 Linaro Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/atomic.h>
  14. #include <linux/dmi.h>
  15. #include <linux/efi.h>
  16. #include <linux/export.h>
  17. #include <linux/memblock.h>
  18. #include <linux/mm_types.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/of.h>
  21. #include <linux/of_fdt.h>
  22. #include <linux/preempt.h>
  23. #include <linux/rbtree.h>
  24. #include <linux/rwsem.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/efi.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/mmu.h>
  33. #include <asm/pgtable.h>
  34. struct efi_memory_map memmap;
  35. static u64 efi_system_table;
  36. static pgd_t efi_pgd[PTRS_PER_PGD] __page_aligned_bss;
  37. static struct mm_struct efi_mm = {
  38. .mm_rb = RB_ROOT,
  39. .pgd = efi_pgd,
  40. .mm_users = ATOMIC_INIT(2),
  41. .mm_count = ATOMIC_INIT(1),
  42. .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),
  43. .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
  44. .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
  45. };
  46. static int __init is_normal_ram(efi_memory_desc_t *md)
  47. {
  48. if (md->attribute & EFI_MEMORY_WB)
  49. return 1;
  50. return 0;
  51. }
  52. /*
  53. * Translate a EFI virtual address into a physical address: this is necessary,
  54. * as some data members of the EFI system table are virtually remapped after
  55. * SetVirtualAddressMap() has been called.
  56. */
  57. static phys_addr_t efi_to_phys(unsigned long addr)
  58. {
  59. efi_memory_desc_t *md;
  60. for_each_efi_memory_desc(&memmap, md) {
  61. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  62. continue;
  63. if (md->virt_addr == 0)
  64. /* no virtual mapping has been installed by the stub */
  65. break;
  66. if (md->virt_addr <= addr &&
  67. (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
  68. return md->phys_addr + addr - md->virt_addr;
  69. }
  70. return addr;
  71. }
  72. static int __init uefi_init(void)
  73. {
  74. efi_char16_t *c16;
  75. void *config_tables;
  76. u64 table_size;
  77. char vendor[100] = "unknown";
  78. int i, retval;
  79. efi.systab = early_memremap(efi_system_table,
  80. sizeof(efi_system_table_t));
  81. if (efi.systab == NULL) {
  82. pr_warn("Unable to map EFI system table.\n");
  83. return -ENOMEM;
  84. }
  85. set_bit(EFI_BOOT, &efi.flags);
  86. set_bit(EFI_64BIT, &efi.flags);
  87. /*
  88. * Verify the EFI Table
  89. */
  90. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  91. pr_err("System table signature incorrect\n");
  92. retval = -EINVAL;
  93. goto out;
  94. }
  95. if ((efi.systab->hdr.revision >> 16) < 2)
  96. pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
  97. efi.systab->hdr.revision >> 16,
  98. efi.systab->hdr.revision & 0xffff);
  99. /* Show what we know for posterity */
  100. c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),
  101. sizeof(vendor) * sizeof(efi_char16_t));
  102. if (c16) {
  103. for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
  104. vendor[i] = c16[i];
  105. vendor[i] = '\0';
  106. early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
  107. }
  108. pr_info("EFI v%u.%.02u by %s\n",
  109. efi.systab->hdr.revision >> 16,
  110. efi.systab->hdr.revision & 0xffff, vendor);
  111. table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
  112. config_tables = early_memremap(efi_to_phys(efi.systab->tables),
  113. table_size);
  114. if (config_tables == NULL) {
  115. pr_warn("Unable to map EFI config table array.\n");
  116. retval = -ENOMEM;
  117. goto out;
  118. }
  119. retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
  120. sizeof(efi_config_table_64_t), NULL);
  121. early_memunmap(config_tables, table_size);
  122. out:
  123. early_memunmap(efi.systab, sizeof(efi_system_table_t));
  124. return retval;
  125. }
  126. /*
  127. * Return true for RAM regions we want to permanently reserve.
  128. */
  129. static __init int is_reserve_region(efi_memory_desc_t *md)
  130. {
  131. switch (md->type) {
  132. case EFI_LOADER_CODE:
  133. case EFI_LOADER_DATA:
  134. case EFI_BOOT_SERVICES_CODE:
  135. case EFI_BOOT_SERVICES_DATA:
  136. case EFI_CONVENTIONAL_MEMORY:
  137. case EFI_PERSISTENT_MEMORY:
  138. return 0;
  139. default:
  140. break;
  141. }
  142. return is_normal_ram(md);
  143. }
  144. static __init void reserve_regions(void)
  145. {
  146. efi_memory_desc_t *md;
  147. u64 paddr, npages, size;
  148. if (efi_enabled(EFI_DBG))
  149. pr_info("Processing EFI memory map:\n");
  150. for_each_efi_memory_desc(&memmap, md) {
  151. paddr = md->phys_addr;
  152. npages = md->num_pages;
  153. if (efi_enabled(EFI_DBG)) {
  154. char buf[64];
  155. pr_info(" 0x%012llx-0x%012llx %s",
  156. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  157. efi_md_typeattr_format(buf, sizeof(buf), md));
  158. }
  159. memrange_efi_to_native(&paddr, &npages);
  160. size = npages << PAGE_SHIFT;
  161. if (is_normal_ram(md))
  162. early_init_dt_add_memory_arch(paddr, size);
  163. if (is_reserve_region(md)) {
  164. memblock_reserve(paddr, size);
  165. if (efi_enabled(EFI_DBG))
  166. pr_cont("*");
  167. }
  168. if (efi_enabled(EFI_DBG))
  169. pr_cont("\n");
  170. }
  171. set_bit(EFI_MEMMAP, &efi.flags);
  172. }
  173. void __init efi_init(void)
  174. {
  175. struct efi_fdt_params params;
  176. /* Grab UEFI information placed in FDT by stub */
  177. if (!efi_get_fdt_params(&params))
  178. return;
  179. efi_system_table = params.system_table;
  180. memblock_reserve(params.mmap & PAGE_MASK,
  181. PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
  182. memmap.phys_map = params.mmap;
  183. memmap.map = early_memremap(params.mmap, params.mmap_size);
  184. if (memmap.map == NULL) {
  185. /*
  186. * If we are booting via UEFI, the UEFI memory map is the only
  187. * description of memory we have, so there is little point in
  188. * proceeding if we cannot access it.
  189. */
  190. panic("Unable to map EFI memory map.\n");
  191. }
  192. memmap.map_end = memmap.map + params.mmap_size;
  193. memmap.desc_size = params.desc_size;
  194. memmap.desc_version = params.desc_ver;
  195. if (uefi_init() < 0)
  196. return;
  197. reserve_regions();
  198. early_memunmap(memmap.map, params.mmap_size);
  199. }
  200. static bool __init efi_virtmap_init(void)
  201. {
  202. efi_memory_desc_t *md;
  203. init_new_context(NULL, &efi_mm);
  204. for_each_efi_memory_desc(&memmap, md) {
  205. pgprot_t prot;
  206. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  207. continue;
  208. if (md->virt_addr == 0)
  209. return false;
  210. pr_info(" EFI remap 0x%016llx => %p\n",
  211. md->phys_addr, (void *)md->virt_addr);
  212. /*
  213. * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
  214. * executable, everything else can be mapped with the XN bits
  215. * set.
  216. */
  217. if (!is_normal_ram(md))
  218. prot = __pgprot(PROT_DEVICE_nGnRE);
  219. else if (md->type == EFI_RUNTIME_SERVICES_CODE ||
  220. !PAGE_ALIGNED(md->phys_addr))
  221. prot = PAGE_KERNEL_EXEC;
  222. else
  223. prot = PAGE_KERNEL;
  224. create_pgd_mapping(&efi_mm, md->phys_addr, md->virt_addr,
  225. md->num_pages << EFI_PAGE_SHIFT,
  226. __pgprot(pgprot_val(prot) | PTE_NG));
  227. }
  228. return true;
  229. }
  230. /*
  231. * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
  232. * non-early mapping of the UEFI system table and virtual mappings for all
  233. * EFI_MEMORY_RUNTIME regions.
  234. */
  235. static int __init arm64_enable_runtime_services(void)
  236. {
  237. u64 mapsize;
  238. if (!efi_enabled(EFI_BOOT)) {
  239. pr_info("EFI services will not be available.\n");
  240. return 0;
  241. }
  242. if (efi_runtime_disabled()) {
  243. pr_info("EFI runtime services will be disabled.\n");
  244. return 0;
  245. }
  246. pr_info("Remapping and enabling EFI services.\n");
  247. mapsize = memmap.map_end - memmap.map;
  248. memmap.map = (__force void *)ioremap_cache(memmap.phys_map,
  249. mapsize);
  250. if (!memmap.map) {
  251. pr_err("Failed to remap EFI memory map\n");
  252. return -ENOMEM;
  253. }
  254. memmap.map_end = memmap.map + mapsize;
  255. efi.memmap = &memmap;
  256. efi.systab = (__force void *)ioremap_cache(efi_system_table,
  257. sizeof(efi_system_table_t));
  258. if (!efi.systab) {
  259. pr_err("Failed to remap EFI System Table\n");
  260. return -ENOMEM;
  261. }
  262. set_bit(EFI_SYSTEM_TABLES, &efi.flags);
  263. if (!efi_virtmap_init()) {
  264. pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
  265. return -ENOMEM;
  266. }
  267. /* Set up runtime services function pointers */
  268. efi_native_runtime_setup();
  269. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  270. efi.runtime_version = efi.systab->hdr.revision;
  271. return 0;
  272. }
  273. early_initcall(arm64_enable_runtime_services);
  274. static int __init arm64_dmi_init(void)
  275. {
  276. /*
  277. * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
  278. * be called early because dmi_id_init(), which is an arch_initcall
  279. * itself, depends on dmi_scan_machine() having been called already.
  280. */
  281. dmi_scan_machine();
  282. if (dmi_available)
  283. dmi_set_dump_stack_arch_desc();
  284. return 0;
  285. }
  286. core_initcall(arm64_dmi_init);
  287. static void efi_set_pgd(struct mm_struct *mm)
  288. {
  289. switch_mm(NULL, mm, NULL);
  290. }
  291. void efi_virtmap_load(void)
  292. {
  293. preempt_disable();
  294. efi_set_pgd(&efi_mm);
  295. }
  296. void efi_virtmap_unload(void)
  297. {
  298. efi_set_pgd(current->active_mm);
  299. preempt_enable();
  300. }
  301. /*
  302. * UpdateCapsule() depends on the system being shutdown via
  303. * ResetSystem().
  304. */
  305. bool efi_poweroff_required(void)
  306. {
  307. return efi_enabled(EFI_RUNTIME_SERVICES);
  308. }