efi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. return 0;
  141. default:
  142. break;
  143. }
  144. return is_normal_ram(md);
  145. }
  146. static __init void reserve_regions(void)
  147. {
  148. efi_memory_desc_t *md;
  149. u64 paddr, npages, size;
  150. if (uefi_debug)
  151. pr_info("Processing EFI memory map:\n");
  152. for_each_efi_memory_desc(&memmap, md) {
  153. paddr = md->phys_addr;
  154. npages = md->num_pages;
  155. if (uefi_debug) {
  156. char buf[64];
  157. pr_info(" 0x%012llx-0x%012llx %s",
  158. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  159. efi_md_typeattr_format(buf, sizeof(buf), md));
  160. }
  161. memrange_efi_to_native(&paddr, &npages);
  162. size = npages << PAGE_SHIFT;
  163. if (is_normal_ram(md))
  164. early_init_dt_add_memory_arch(paddr, size);
  165. if (is_reserve_region(md)) {
  166. memblock_reserve(paddr, size);
  167. if (uefi_debug)
  168. pr_cont("*");
  169. }
  170. if (uefi_debug)
  171. pr_cont("\n");
  172. }
  173. set_bit(EFI_MEMMAP, &efi.flags);
  174. }
  175. void __init efi_init(void)
  176. {
  177. struct efi_fdt_params params;
  178. /* Grab UEFI information placed in FDT by stub */
  179. if (!efi_get_fdt_params(&params, uefi_debug))
  180. return;
  181. efi_system_table = params.system_table;
  182. memblock_reserve(params.mmap & PAGE_MASK,
  183. PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
  184. memmap.phys_map = (void *)params.mmap;
  185. memmap.map = early_memremap(params.mmap, params.mmap_size);
  186. memmap.map_end = memmap.map + params.mmap_size;
  187. memmap.desc_size = params.desc_size;
  188. memmap.desc_version = params.desc_ver;
  189. if (uefi_init() < 0)
  190. return;
  191. reserve_regions();
  192. early_memunmap(memmap.map, params.mmap_size);
  193. }
  194. static bool __init efi_virtmap_init(void)
  195. {
  196. efi_memory_desc_t *md;
  197. for_each_efi_memory_desc(&memmap, md) {
  198. u64 paddr, npages, size;
  199. pgprot_t prot;
  200. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  201. continue;
  202. if (md->virt_addr == 0)
  203. return false;
  204. paddr = md->phys_addr;
  205. npages = md->num_pages;
  206. memrange_efi_to_native(&paddr, &npages);
  207. size = npages << PAGE_SHIFT;
  208. pr_info(" EFI remap 0x%016llx => %p\n",
  209. md->phys_addr, (void *)md->virt_addr);
  210. /*
  211. * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
  212. * executable, everything else can be mapped with the XN bits
  213. * set.
  214. */
  215. if (!is_normal_ram(md))
  216. prot = __pgprot(PROT_DEVICE_nGnRE);
  217. else if (md->type == EFI_RUNTIME_SERVICES_CODE)
  218. prot = PAGE_KERNEL_EXEC;
  219. else
  220. prot = PAGE_KERNEL;
  221. create_pgd_mapping(&efi_mm, paddr, md->virt_addr, size, prot);
  222. }
  223. return true;
  224. }
  225. /*
  226. * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
  227. * non-early mapping of the UEFI system table and virtual mappings for all
  228. * EFI_MEMORY_RUNTIME regions.
  229. */
  230. static int __init arm64_enable_runtime_services(void)
  231. {
  232. u64 mapsize;
  233. if (!efi_enabled(EFI_BOOT)) {
  234. pr_info("EFI services will not be available.\n");
  235. return -1;
  236. }
  237. if (efi_runtime_disabled()) {
  238. pr_info("EFI runtime services will be disabled.\n");
  239. return -1;
  240. }
  241. pr_info("Remapping and enabling EFI services.\n");
  242. mapsize = memmap.map_end - memmap.map;
  243. memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
  244. mapsize);
  245. if (!memmap.map) {
  246. pr_err("Failed to remap EFI memory map\n");
  247. return -1;
  248. }
  249. memmap.map_end = memmap.map + mapsize;
  250. efi.memmap = &memmap;
  251. efi.systab = (__force void *)ioremap_cache(efi_system_table,
  252. sizeof(efi_system_table_t));
  253. if (!efi.systab) {
  254. pr_err("Failed to remap EFI System Table\n");
  255. return -1;
  256. }
  257. set_bit(EFI_SYSTEM_TABLES, &efi.flags);
  258. if (!efi_virtmap_init()) {
  259. pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
  260. return -1;
  261. }
  262. /* Set up runtime services function pointers */
  263. efi_native_runtime_setup();
  264. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  265. efi.runtime_version = efi.systab->hdr.revision;
  266. return 0;
  267. }
  268. early_initcall(arm64_enable_runtime_services);
  269. static int __init arm64_dmi_init(void)
  270. {
  271. /*
  272. * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
  273. * be called early because dmi_id_init(), which is an arch_initcall
  274. * itself, depends on dmi_scan_machine() having been called already.
  275. */
  276. dmi_scan_machine();
  277. if (dmi_available)
  278. dmi_set_dump_stack_arch_desc();
  279. return 0;
  280. }
  281. core_initcall(arm64_dmi_init);
  282. static void efi_set_pgd(struct mm_struct *mm)
  283. {
  284. if (mm == &init_mm)
  285. cpu_set_reserved_ttbr0();
  286. else
  287. cpu_switch_mm(mm->pgd, mm);
  288. flush_tlb_all();
  289. if (icache_is_aivivt())
  290. __flush_icache_all();
  291. }
  292. void efi_virtmap_load(void)
  293. {
  294. preempt_disable();
  295. efi_set_pgd(&efi_mm);
  296. }
  297. void efi_virtmap_unload(void)
  298. {
  299. efi_set_pgd(current->active_mm);
  300. preempt_enable();
  301. }
  302. /*
  303. * UpdateCapsule() depends on the system being shutdown via
  304. * ResetSystem().
  305. */
  306. bool efi_poweroff_required(void)
  307. {
  308. return efi_enabled(EFI_RUNTIME_SERVICES);
  309. }