efi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/dmi.h>
  14. #include <linux/efi.h>
  15. #include <linux/export.h>
  16. #include <linux/memblock.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/of.h>
  19. #include <linux/of_fdt.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/efi.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/mmu_context.h>
  26. struct efi_memory_map memmap;
  27. static efi_runtime_services_t *runtime;
  28. static u64 efi_system_table;
  29. static int uefi_debug __initdata;
  30. static int __init uefi_debug_setup(char *str)
  31. {
  32. uefi_debug = 1;
  33. return 0;
  34. }
  35. early_param("uefi_debug", uefi_debug_setup);
  36. static int __init is_normal_ram(efi_memory_desc_t *md)
  37. {
  38. if (md->attribute & EFI_MEMORY_WB)
  39. return 1;
  40. return 0;
  41. }
  42. static void __init efi_setup_idmap(void)
  43. {
  44. struct memblock_region *r;
  45. efi_memory_desc_t *md;
  46. u64 paddr, npages, size;
  47. for_each_memblock(memory, r)
  48. create_id_mapping(r->base, r->size, 0);
  49. /* map runtime io spaces */
  50. for_each_efi_memory_desc(&memmap, md) {
  51. if (!(md->attribute & EFI_MEMORY_RUNTIME) || is_normal_ram(md))
  52. continue;
  53. paddr = md->phys_addr;
  54. npages = md->num_pages;
  55. memrange_efi_to_native(&paddr, &npages);
  56. size = npages << PAGE_SHIFT;
  57. create_id_mapping(paddr, size, 1);
  58. }
  59. }
  60. static int __init uefi_init(void)
  61. {
  62. efi_char16_t *c16;
  63. char vendor[100] = "unknown";
  64. int i, retval;
  65. efi.systab = early_memremap(efi_system_table,
  66. sizeof(efi_system_table_t));
  67. if (efi.systab == NULL) {
  68. pr_warn("Unable to map EFI system table.\n");
  69. return -ENOMEM;
  70. }
  71. set_bit(EFI_BOOT, &efi.flags);
  72. set_bit(EFI_64BIT, &efi.flags);
  73. /*
  74. * Verify the EFI Table
  75. */
  76. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  77. pr_err("System table signature incorrect\n");
  78. retval = -EINVAL;
  79. goto out;
  80. }
  81. if ((efi.systab->hdr.revision >> 16) < 2)
  82. pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
  83. efi.systab->hdr.revision >> 16,
  84. efi.systab->hdr.revision & 0xffff);
  85. /* Show what we know for posterity */
  86. c16 = early_memremap(efi.systab->fw_vendor,
  87. sizeof(vendor));
  88. if (c16) {
  89. for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
  90. vendor[i] = c16[i];
  91. vendor[i] = '\0';
  92. early_memunmap(c16, sizeof(vendor));
  93. }
  94. pr_info("EFI v%u.%.02u by %s\n",
  95. efi.systab->hdr.revision >> 16,
  96. efi.systab->hdr.revision & 0xffff, vendor);
  97. retval = efi_config_init(NULL);
  98. out:
  99. early_memunmap(efi.systab, sizeof(efi_system_table_t));
  100. return retval;
  101. }
  102. /*
  103. * Return true for RAM regions we want to permanently reserve.
  104. */
  105. static __init int is_reserve_region(efi_memory_desc_t *md)
  106. {
  107. switch (md->type) {
  108. case EFI_LOADER_CODE:
  109. case EFI_LOADER_DATA:
  110. case EFI_BOOT_SERVICES_CODE:
  111. case EFI_BOOT_SERVICES_DATA:
  112. case EFI_CONVENTIONAL_MEMORY:
  113. return 0;
  114. default:
  115. break;
  116. }
  117. return is_normal_ram(md);
  118. }
  119. static __init void reserve_regions(void)
  120. {
  121. efi_memory_desc_t *md;
  122. u64 paddr, npages, size;
  123. if (uefi_debug)
  124. pr_info("Processing EFI memory map:\n");
  125. for_each_efi_memory_desc(&memmap, md) {
  126. paddr = md->phys_addr;
  127. npages = md->num_pages;
  128. if (uefi_debug) {
  129. char buf[64];
  130. pr_info(" 0x%012llx-0x%012llx %s",
  131. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  132. efi_md_typeattr_format(buf, sizeof(buf), md));
  133. }
  134. memrange_efi_to_native(&paddr, &npages);
  135. size = npages << PAGE_SHIFT;
  136. if (is_normal_ram(md))
  137. early_init_dt_add_memory_arch(paddr, size);
  138. if (is_reserve_region(md) ||
  139. md->type == EFI_BOOT_SERVICES_CODE ||
  140. md->type == EFI_BOOT_SERVICES_DATA) {
  141. memblock_reserve(paddr, size);
  142. if (uefi_debug)
  143. pr_cont("*");
  144. }
  145. if (uefi_debug)
  146. pr_cont("\n");
  147. }
  148. set_bit(EFI_MEMMAP, &efi.flags);
  149. }
  150. static u64 __init free_one_region(u64 start, u64 end)
  151. {
  152. u64 size = end - start;
  153. if (uefi_debug)
  154. pr_info(" EFI freeing: 0x%012llx-0x%012llx\n", start, end - 1);
  155. free_bootmem_late(start, size);
  156. return size;
  157. }
  158. static u64 __init free_region(u64 start, u64 end)
  159. {
  160. u64 map_start, map_end, total = 0;
  161. if (end <= start)
  162. return total;
  163. map_start = (u64)memmap.phys_map;
  164. map_end = PAGE_ALIGN(map_start + (memmap.map_end - memmap.map));
  165. map_start &= PAGE_MASK;
  166. if (start < map_end && end > map_start) {
  167. /* region overlaps UEFI memmap */
  168. if (start < map_start)
  169. total += free_one_region(start, map_start);
  170. if (map_end < end)
  171. total += free_one_region(map_end, end);
  172. } else
  173. total += free_one_region(start, end);
  174. return total;
  175. }
  176. static void __init free_boot_services(void)
  177. {
  178. u64 total_freed = 0;
  179. u64 keep_end, free_start, free_end;
  180. efi_memory_desc_t *md;
  181. /*
  182. * If kernel uses larger pages than UEFI, we have to be careful
  183. * not to inadvertantly free memory we want to keep if there is
  184. * overlap at the kernel page size alignment. We do not want to
  185. * free is_reserve_region() memory nor the UEFI memmap itself.
  186. *
  187. * The memory map is sorted, so we keep track of the end of
  188. * any previous region we want to keep, remember any region
  189. * we want to free and defer freeing it until we encounter
  190. * the next region we want to keep. This way, before freeing
  191. * it, we can clip it as needed to avoid freeing memory we
  192. * want to keep for UEFI.
  193. */
  194. keep_end = 0;
  195. free_start = 0;
  196. for_each_efi_memory_desc(&memmap, md) {
  197. u64 paddr, npages, size;
  198. if (is_reserve_region(md)) {
  199. /*
  200. * We don't want to free any memory from this region.
  201. */
  202. if (free_start) {
  203. /* adjust free_end then free region */
  204. if (free_end > md->phys_addr)
  205. free_end -= PAGE_SIZE;
  206. total_freed += free_region(free_start, free_end);
  207. free_start = 0;
  208. }
  209. keep_end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
  210. continue;
  211. }
  212. if (md->type != EFI_BOOT_SERVICES_CODE &&
  213. md->type != EFI_BOOT_SERVICES_DATA) {
  214. /* no need to free this region */
  215. continue;
  216. }
  217. /*
  218. * We want to free memory from this region.
  219. */
  220. paddr = md->phys_addr;
  221. npages = md->num_pages;
  222. memrange_efi_to_native(&paddr, &npages);
  223. size = npages << PAGE_SHIFT;
  224. if (free_start) {
  225. if (paddr <= free_end)
  226. free_end = paddr + size;
  227. else {
  228. total_freed += free_region(free_start, free_end);
  229. free_start = paddr;
  230. free_end = paddr + size;
  231. }
  232. } else {
  233. free_start = paddr;
  234. free_end = paddr + size;
  235. }
  236. if (free_start < keep_end) {
  237. free_start += PAGE_SIZE;
  238. if (free_start >= free_end)
  239. free_start = 0;
  240. }
  241. }
  242. if (free_start)
  243. total_freed += free_region(free_start, free_end);
  244. if (total_freed)
  245. pr_info("Freed 0x%llx bytes of EFI boot services memory",
  246. total_freed);
  247. }
  248. void __init efi_init(void)
  249. {
  250. struct efi_fdt_params params;
  251. /* Grab UEFI information placed in FDT by stub */
  252. if (!efi_get_fdt_params(&params, uefi_debug))
  253. return;
  254. efi_system_table = params.system_table;
  255. memblock_reserve(params.mmap & PAGE_MASK,
  256. PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
  257. memmap.phys_map = (void *)params.mmap;
  258. memmap.map = early_memremap(params.mmap, params.mmap_size);
  259. memmap.map_end = memmap.map + params.mmap_size;
  260. memmap.desc_size = params.desc_size;
  261. memmap.desc_version = params.desc_ver;
  262. if (uefi_init() < 0)
  263. return;
  264. reserve_regions();
  265. }
  266. void __init efi_idmap_init(void)
  267. {
  268. if (!efi_enabled(EFI_BOOT))
  269. return;
  270. /* boot time idmap_pg_dir is incomplete, so fill in missing parts */
  271. efi_setup_idmap();
  272. }
  273. static int __init remap_region(efi_memory_desc_t *md, void **new)
  274. {
  275. u64 paddr, vaddr, npages, size;
  276. paddr = md->phys_addr;
  277. npages = md->num_pages;
  278. memrange_efi_to_native(&paddr, &npages);
  279. size = npages << PAGE_SHIFT;
  280. if (is_normal_ram(md))
  281. vaddr = (__force u64)ioremap_cache(paddr, size);
  282. else
  283. vaddr = (__force u64)ioremap(paddr, size);
  284. if (!vaddr) {
  285. pr_err("Unable to remap 0x%llx pages @ %p\n",
  286. npages, (void *)paddr);
  287. return 0;
  288. }
  289. /* adjust for any rounding when EFI and system pagesize differs */
  290. md->virt_addr = vaddr + (md->phys_addr - paddr);
  291. if (uefi_debug)
  292. pr_info(" EFI remap 0x%012llx => %p\n",
  293. md->phys_addr, (void *)md->virt_addr);
  294. memcpy(*new, md, memmap.desc_size);
  295. *new += memmap.desc_size;
  296. return 1;
  297. }
  298. /*
  299. * Switch UEFI from an identity map to a kernel virtual map
  300. */
  301. static int __init arm64_enter_virtual_mode(void)
  302. {
  303. efi_memory_desc_t *md;
  304. phys_addr_t virtmap_phys;
  305. void *virtmap, *virt_md;
  306. efi_status_t status;
  307. u64 mapsize;
  308. int count = 0;
  309. unsigned long flags;
  310. if (!efi_enabled(EFI_BOOT)) {
  311. pr_info("EFI services will not be available.\n");
  312. return -1;
  313. }
  314. mapsize = memmap.map_end - memmap.map;
  315. early_memunmap(memmap.map, mapsize);
  316. if (efi_runtime_disabled()) {
  317. pr_info("EFI runtime services will be disabled.\n");
  318. return -1;
  319. }
  320. pr_info("Remapping and enabling EFI services.\n");
  321. /* replace early memmap mapping with permanent mapping */
  322. memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
  323. mapsize);
  324. memmap.map_end = memmap.map + mapsize;
  325. efi.memmap = &memmap;
  326. /* Map the runtime regions */
  327. virtmap = kmalloc(mapsize, GFP_KERNEL);
  328. if (!virtmap) {
  329. pr_err("Failed to allocate EFI virtual memmap\n");
  330. return -1;
  331. }
  332. virtmap_phys = virt_to_phys(virtmap);
  333. virt_md = virtmap;
  334. for_each_efi_memory_desc(&memmap, md) {
  335. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  336. continue;
  337. if (!remap_region(md, &virt_md))
  338. goto err_unmap;
  339. ++count;
  340. }
  341. efi.systab = (__force void *)efi_lookup_mapped_addr(efi_system_table);
  342. if (!efi.systab) {
  343. /*
  344. * If we have no virtual mapping for the System Table at this
  345. * point, the memory map doesn't cover the physical offset where
  346. * it resides. This means the System Table will be inaccessible
  347. * to Runtime Services themselves once the virtual mapping is
  348. * installed.
  349. */
  350. pr_err("Failed to remap EFI System Table -- buggy firmware?\n");
  351. goto err_unmap;
  352. }
  353. set_bit(EFI_SYSTEM_TABLES, &efi.flags);
  354. local_irq_save(flags);
  355. cpu_switch_mm(idmap_pg_dir, &init_mm);
  356. /* Call SetVirtualAddressMap with the physical address of the map */
  357. runtime = efi.systab->runtime;
  358. efi.set_virtual_address_map = runtime->set_virtual_address_map;
  359. status = efi.set_virtual_address_map(count * memmap.desc_size,
  360. memmap.desc_size,
  361. memmap.desc_version,
  362. (efi_memory_desc_t *)virtmap_phys);
  363. cpu_set_reserved_ttbr0();
  364. flush_tlb_all();
  365. local_irq_restore(flags);
  366. kfree(virtmap);
  367. free_boot_services();
  368. if (status != EFI_SUCCESS) {
  369. pr_err("Failed to set EFI virtual address map! [%lx]\n",
  370. status);
  371. return -1;
  372. }
  373. /* Set up runtime services function pointers */
  374. runtime = efi.systab->runtime;
  375. efi_native_runtime_setup();
  376. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  377. efi.runtime_version = efi.systab->hdr.revision;
  378. return 0;
  379. err_unmap:
  380. /* unmap all mappings that succeeded: there are 'count' of those */
  381. for (virt_md = virtmap; count--; virt_md += memmap.desc_size) {
  382. md = virt_md;
  383. iounmap((__force void __iomem *)md->virt_addr);
  384. }
  385. kfree(virtmap);
  386. return -1;
  387. }
  388. early_initcall(arm64_enter_virtual_mode);
  389. static int __init arm64_dmi_init(void)
  390. {
  391. /*
  392. * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
  393. * be called early because dmi_id_init(), which is an arch_initcall
  394. * itself, depends on dmi_scan_machine() having been called already.
  395. */
  396. dmi_scan_machine();
  397. if (dmi_available)
  398. dmi_set_dump_stack_arch_desc();
  399. return 0;
  400. }
  401. core_initcall(arm64_dmi_init);