arm-init.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Extensible Firmware Interface
  3. *
  4. * Based on Extensible Firmware Interface Specification version 2.4
  5. *
  6. * Copyright (C) 2013 - 2015 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. #define pr_fmt(fmt) "efi: " fmt
  14. #include <linux/efi.h>
  15. #include <linux/init.h>
  16. #include <linux/memblock.h>
  17. #include <linux/mm_types.h>
  18. #include <linux/of.h>
  19. #include <linux/of_fdt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/screen_info.h>
  22. #include <linux/security.h>
  23. #include <asm/efi.h>
  24. u64 efi_system_table;
  25. static int __init is_memory(efi_memory_desc_t *md)
  26. {
  27. if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
  28. return 1;
  29. return 0;
  30. }
  31. /*
  32. * Translate a EFI virtual address into a physical address: this is necessary,
  33. * as some data members of the EFI system table are virtually remapped after
  34. * SetVirtualAddressMap() has been called.
  35. */
  36. static phys_addr_t efi_to_phys(unsigned long addr)
  37. {
  38. efi_memory_desc_t *md;
  39. for_each_efi_memory_desc(md) {
  40. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  41. continue;
  42. if (md->virt_addr == 0)
  43. /* no virtual mapping has been installed by the stub */
  44. break;
  45. if (md->virt_addr <= addr &&
  46. (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
  47. return md->phys_addr + addr - md->virt_addr;
  48. }
  49. return addr;
  50. }
  51. static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
  52. static __initdata efi_config_table_type_t arch_tables[] = {
  53. {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, NULL, &screen_info_table},
  54. {NULL_GUID, NULL, NULL}
  55. };
  56. static void __init init_screen_info(void)
  57. {
  58. struct screen_info *si;
  59. if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
  60. si = early_memremap_ro(screen_info_table, sizeof(*si));
  61. if (!si) {
  62. pr_err("Could not map screen_info config table\n");
  63. return;
  64. }
  65. screen_info = *si;
  66. early_memunmap(si, sizeof(*si));
  67. /* dummycon on ARM needs non-zero values for columns/lines */
  68. screen_info.orig_video_cols = 80;
  69. screen_info.orig_video_lines = 25;
  70. }
  71. if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
  72. memblock_is_map_memory(screen_info.lfb_base))
  73. memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
  74. }
  75. static int __init uefi_init(void)
  76. {
  77. efi_char16_t *c16;
  78. void *config_tables;
  79. size_t table_size;
  80. char vendor[100] = "unknown";
  81. int i, retval;
  82. efi.systab = early_memremap_ro(efi_system_table,
  83. sizeof(efi_system_table_t));
  84. if (efi.systab == NULL) {
  85. pr_warn("Unable to map EFI system table.\n");
  86. return -ENOMEM;
  87. }
  88. set_bit(EFI_BOOT, &efi.flags);
  89. if (IS_ENABLED(CONFIG_64BIT))
  90. set_bit(EFI_64BIT, &efi.flags);
  91. /*
  92. * Verify the EFI Table
  93. */
  94. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  95. pr_err("System table signature incorrect\n");
  96. retval = -EINVAL;
  97. goto out;
  98. }
  99. if ((efi.systab->hdr.revision >> 16) < 2)
  100. pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
  101. efi.systab->hdr.revision >> 16,
  102. efi.systab->hdr.revision & 0xffff);
  103. efi.runtime_version = efi.systab->hdr.revision;
  104. /* Show what we know for posterity */
  105. c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor),
  106. sizeof(vendor) * sizeof(efi_char16_t));
  107. if (c16) {
  108. for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
  109. vendor[i] = c16[i];
  110. vendor[i] = '\0';
  111. early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
  112. }
  113. pr_info("EFI v%u.%.02u by %s\n",
  114. efi.systab->hdr.revision >> 16,
  115. efi.systab->hdr.revision & 0xffff, vendor);
  116. table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
  117. config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables),
  118. table_size);
  119. if (config_tables == NULL) {
  120. pr_warn("Unable to map EFI config table array.\n");
  121. retval = -ENOMEM;
  122. goto out;
  123. }
  124. retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
  125. sizeof(efi_config_table_t),
  126. arch_tables);
  127. if (!retval)
  128. efi.config_table = (unsigned long)efi.systab->tables;
  129. early_memunmap(config_tables, table_size);
  130. out:
  131. early_memunmap(efi.systab, sizeof(efi_system_table_t));
  132. return retval;
  133. }
  134. /*
  135. * Return true for regions that can be used as System RAM.
  136. */
  137. static __init int is_usable_memory(efi_memory_desc_t *md)
  138. {
  139. switch (md->type) {
  140. case EFI_LOADER_CODE:
  141. case EFI_LOADER_DATA:
  142. case EFI_ACPI_RECLAIM_MEMORY:
  143. case EFI_BOOT_SERVICES_CODE:
  144. case EFI_BOOT_SERVICES_DATA:
  145. case EFI_CONVENTIONAL_MEMORY:
  146. case EFI_PERSISTENT_MEMORY:
  147. /*
  148. * According to the spec, these regions are no longer reserved
  149. * after calling ExitBootServices(). However, we can only use
  150. * them as System RAM if they can be mapped writeback cacheable.
  151. */
  152. return (md->attribute & EFI_MEMORY_WB);
  153. default:
  154. break;
  155. }
  156. return false;
  157. }
  158. static __init void reserve_regions(void)
  159. {
  160. efi_memory_desc_t *md;
  161. u64 paddr, npages, size;
  162. if (efi_enabled(EFI_DBG))
  163. pr_info("Processing EFI memory map:\n");
  164. /*
  165. * Discard memblocks discovered so far: if there are any at this
  166. * point, they originate from memory nodes in the DT, and UEFI
  167. * uses its own memory map instead.
  168. */
  169. memblock_dump_all();
  170. memblock_remove(0, PHYS_ADDR_MAX);
  171. for_each_efi_memory_desc(md) {
  172. paddr = md->phys_addr;
  173. npages = md->num_pages;
  174. if (efi_enabled(EFI_DBG)) {
  175. char buf[64];
  176. pr_info(" 0x%012llx-0x%012llx %s\n",
  177. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  178. efi_md_typeattr_format(buf, sizeof(buf), md));
  179. }
  180. memrange_efi_to_native(&paddr, &npages);
  181. size = npages << PAGE_SHIFT;
  182. if (is_memory(md)) {
  183. early_init_dt_add_memory_arch(paddr, size);
  184. if (!is_usable_memory(md))
  185. memblock_mark_nomap(paddr, size);
  186. /* keep ACPI reclaim memory intact for kexec etc. */
  187. if (md->type == EFI_ACPI_RECLAIM_MEMORY)
  188. memblock_reserve(paddr, size);
  189. }
  190. }
  191. }
  192. void __init efi_init(void)
  193. {
  194. struct efi_memory_map_data data;
  195. struct efi_fdt_params params;
  196. /* Grab UEFI information placed in FDT by stub */
  197. if (!efi_get_fdt_params(&params))
  198. return;
  199. efi_system_table = params.system_table;
  200. data.desc_version = params.desc_ver;
  201. data.desc_size = params.desc_size;
  202. data.size = params.mmap_size;
  203. data.phys_map = params.mmap;
  204. if (efi_memmap_init_early(&data) < 0) {
  205. /*
  206. * If we are booting via UEFI, the UEFI memory map is the only
  207. * description of memory we have, so there is little point in
  208. * proceeding if we cannot access it.
  209. */
  210. panic("Unable to map EFI memory map.\n");
  211. }
  212. WARN(efi.memmap.desc_version != 1,
  213. "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
  214. efi.memmap.desc_version);
  215. if (uefi_init() < 0) {
  216. efi_memmap_unmap();
  217. return;
  218. }
  219. efi_set_secure_boot(params.secure_boot);
  220. init_lockdown();
  221. reserve_regions();
  222. efi_esrt_init();
  223. memblock_reserve(params.mmap & PAGE_MASK,
  224. PAGE_ALIGN(params.mmap_size +
  225. (params.mmap & ~PAGE_MASK)));
  226. init_screen_info();
  227. /* ARM does not permit early mappings to persist across paging_init() */
  228. if (IS_ENABLED(CONFIG_ARM))
  229. efi_memmap_unmap();
  230. }
  231. static int __init register_gop_device(void)
  232. {
  233. void *pd;
  234. if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
  235. return 0;
  236. pd = platform_device_register_data(NULL, "efi-framebuffer", 0,
  237. &screen_info, sizeof(screen_info));
  238. return PTR_ERR_OR_ZERO(pd);
  239. }
  240. subsys_initcall(register_gop_device);