efi.c 11 KB

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