efi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. INIT_MM_CONTEXT(efi_mm)
  46. };
  47. static int uefi_debug __initdata;
  48. static int __init uefi_debug_setup(char *str)
  49. {
  50. uefi_debug = 1;
  51. return 0;
  52. }
  53. early_param("uefi_debug", uefi_debug_setup);
  54. static int __init is_normal_ram(efi_memory_desc_t *md)
  55. {
  56. if (md->attribute & EFI_MEMORY_WB)
  57. return 1;
  58. return 0;
  59. }
  60. /*
  61. * Translate a EFI virtual address into a physical address: this is necessary,
  62. * as some data members of the EFI system table are virtually remapped after
  63. * SetVirtualAddressMap() has been called.
  64. */
  65. static phys_addr_t efi_to_phys(unsigned long addr)
  66. {
  67. efi_memory_desc_t *md;
  68. for_each_efi_memory_desc(&memmap, md) {
  69. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  70. continue;
  71. if (md->virt_addr == 0)
  72. /* no virtual mapping has been installed by the stub */
  73. break;
  74. if (md->virt_addr <= addr &&
  75. (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
  76. return md->phys_addr + addr - md->virt_addr;
  77. }
  78. return addr;
  79. }
  80. static int __init uefi_init(void)
  81. {
  82. efi_char16_t *c16;
  83. void *config_tables;
  84. u64 table_size;
  85. char vendor[100] = "unknown";
  86. int i, retval;
  87. efi.systab = early_memremap(efi_system_table,
  88. sizeof(efi_system_table_t));
  89. if (efi.systab == NULL) {
  90. pr_warn("Unable to map EFI system table.\n");
  91. return -ENOMEM;
  92. }
  93. set_bit(EFI_BOOT, &efi.flags);
  94. set_bit(EFI_64BIT, &efi.flags);
  95. /*
  96. * Verify the EFI Table
  97. */
  98. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  99. pr_err("System table signature incorrect\n");
  100. retval = -EINVAL;
  101. goto out;
  102. }
  103. if ((efi.systab->hdr.revision >> 16) < 2)
  104. pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
  105. efi.systab->hdr.revision >> 16,
  106. efi.systab->hdr.revision & 0xffff);
  107. /* Show what we know for posterity */
  108. c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),
  109. sizeof(vendor));
  110. if (c16) {
  111. for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
  112. vendor[i] = c16[i];
  113. vendor[i] = '\0';
  114. early_memunmap(c16, sizeof(vendor));
  115. }
  116. pr_info("EFI v%u.%.02u by %s\n",
  117. efi.systab->hdr.revision >> 16,
  118. efi.systab->hdr.revision & 0xffff, vendor);
  119. table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
  120. config_tables = early_memremap(efi_to_phys(efi.systab->tables),
  121. table_size);
  122. retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
  123. sizeof(efi_config_table_64_t), NULL);
  124. early_memunmap(config_tables, table_size);
  125. out:
  126. early_memunmap(efi.systab, sizeof(efi_system_table_t));
  127. return retval;
  128. }
  129. /*
  130. * Return true for RAM regions we want to permanently reserve.
  131. */
  132. static __init int is_reserve_region(efi_memory_desc_t *md)
  133. {
  134. switch (md->type) {
  135. case EFI_LOADER_CODE:
  136. case EFI_LOADER_DATA:
  137. case EFI_BOOT_SERVICES_CODE:
  138. case EFI_BOOT_SERVICES_DATA:
  139. case EFI_CONVENTIONAL_MEMORY:
  140. case EFI_PERSISTENT_MEMORY:
  141. return 0;
  142. default:
  143. break;
  144. }
  145. return is_normal_ram(md);
  146. }
  147. static __init void reserve_regions(void)
  148. {
  149. efi_memory_desc_t *md;
  150. u64 paddr, npages, size;
  151. if (uefi_debug)
  152. pr_info("Processing EFI memory map:\n");
  153. for_each_efi_memory_desc(&memmap, md) {
  154. paddr = md->phys_addr;
  155. npages = md->num_pages;
  156. if (uefi_debug) {
  157. char buf[64];
  158. pr_info(" 0x%012llx-0x%012llx %s",
  159. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  160. efi_md_typeattr_format(buf, sizeof(buf), md));
  161. }
  162. memrange_efi_to_native(&paddr, &npages);
  163. size = npages << PAGE_SHIFT;
  164. if (is_normal_ram(md))
  165. early_init_dt_add_memory_arch(paddr, size);
  166. if (is_reserve_region(md)) {
  167. memblock_reserve(paddr, size);
  168. if (uefi_debug)
  169. pr_cont("*");
  170. }
  171. if (uefi_debug)
  172. pr_cont("\n");
  173. }
  174. set_bit(EFI_MEMMAP, &efi.flags);
  175. }
  176. void __init efi_init(void)
  177. {
  178. struct efi_fdt_params params;
  179. /* Grab UEFI information placed in FDT by stub */
  180. if (!efi_get_fdt_params(&params, uefi_debug))
  181. return;
  182. efi_system_table = params.system_table;
  183. memblock_reserve(params.mmap & PAGE_MASK,
  184. PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
  185. memmap.phys_map = (void *)params.mmap;
  186. memmap.map = early_memremap(params.mmap, params.mmap_size);
  187. memmap.map_end = memmap.map + params.mmap_size;
  188. memmap.desc_size = params.desc_size;
  189. memmap.desc_version = params.desc_ver;
  190. if (uefi_init() < 0)
  191. return;
  192. reserve_regions();
  193. early_memunmap(memmap.map, params.mmap_size);
  194. }
  195. static bool __init efi_virtmap_init(void)
  196. {
  197. efi_memory_desc_t *md;
  198. for_each_efi_memory_desc(&memmap, md) {
  199. u64 paddr, npages, size;
  200. pgprot_t prot;
  201. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  202. continue;
  203. if (md->virt_addr == 0)
  204. return false;
  205. paddr = md->phys_addr;
  206. npages = md->num_pages;
  207. memrange_efi_to_native(&paddr, &npages);
  208. size = npages << PAGE_SHIFT;
  209. pr_info(" EFI remap 0x%016llx => %p\n",
  210. md->phys_addr, (void *)md->virt_addr);
  211. /*
  212. * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
  213. * executable, everything else can be mapped with the XN bits
  214. * set.
  215. */
  216. if (!is_normal_ram(md))
  217. prot = __pgprot(PROT_DEVICE_nGnRE);
  218. else if (md->type == EFI_RUNTIME_SERVICES_CODE)
  219. prot = PAGE_KERNEL_EXEC;
  220. else
  221. prot = PAGE_KERNEL;
  222. create_pgd_mapping(&efi_mm, paddr, md->virt_addr, size, prot);
  223. }
  224. return true;
  225. }
  226. /*
  227. * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
  228. * non-early mapping of the UEFI system table and virtual mappings for all
  229. * EFI_MEMORY_RUNTIME regions.
  230. */
  231. static int __init arm64_enable_runtime_services(void)
  232. {
  233. u64 mapsize;
  234. if (!efi_enabled(EFI_BOOT)) {
  235. pr_info("EFI services will not be available.\n");
  236. return -1;
  237. }
  238. if (efi_runtime_disabled()) {
  239. pr_info("EFI runtime services will be disabled.\n");
  240. return -1;
  241. }
  242. pr_info("Remapping and enabling EFI services.\n");
  243. mapsize = memmap.map_end - memmap.map;
  244. memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
  245. mapsize);
  246. if (!memmap.map) {
  247. pr_err("Failed to remap EFI memory map\n");
  248. return -1;
  249. }
  250. memmap.map_end = memmap.map + mapsize;
  251. efi.memmap = &memmap;
  252. efi.systab = (__force void *)ioremap_cache(efi_system_table,
  253. sizeof(efi_system_table_t));
  254. if (!efi.systab) {
  255. pr_err("Failed to remap EFI System Table\n");
  256. return -1;
  257. }
  258. set_bit(EFI_SYSTEM_TABLES, &efi.flags);
  259. if (!efi_virtmap_init()) {
  260. pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
  261. return -1;
  262. }
  263. /* Set up runtime services function pointers */
  264. efi_native_runtime_setup();
  265. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  266. efi.runtime_version = efi.systab->hdr.revision;
  267. return 0;
  268. }
  269. early_initcall(arm64_enable_runtime_services);
  270. static int __init arm64_dmi_init(void)
  271. {
  272. /*
  273. * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
  274. * be called early because dmi_id_init(), which is an arch_initcall
  275. * itself, depends on dmi_scan_machine() having been called already.
  276. */
  277. dmi_scan_machine();
  278. if (dmi_available)
  279. dmi_set_dump_stack_arch_desc();
  280. return 0;
  281. }
  282. core_initcall(arm64_dmi_init);
  283. static void efi_set_pgd(struct mm_struct *mm)
  284. {
  285. if (mm == &init_mm)
  286. cpu_set_reserved_ttbr0();
  287. else
  288. cpu_switch_mm(mm->pgd, mm);
  289. flush_tlb_all();
  290. if (icache_is_aivivt())
  291. __flush_icache_all();
  292. }
  293. void efi_virtmap_load(void)
  294. {
  295. preempt_disable();
  296. efi_set_pgd(&efi_mm);
  297. }
  298. void efi_virtmap_unload(void)
  299. {
  300. efi_set_pgd(current->active_mm);
  301. preempt_enable();
  302. }
  303. /*
  304. * UpdateCapsule() depends on the system being shutdown via
  305. * ResetSystem().
  306. */
  307. bool efi_poweroff_required(void)
  308. {
  309. return efi_enabled(EFI_RUNTIME_SERVICES);
  310. }