efi-init.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Extensible Firmware Interface
  4. *
  5. * Based on Extensible Firmware Interface Specification version 2.4
  6. *
  7. * Copyright (C) 2013 - 2015 Linaro Ltd.
  8. */
  9. #define pr_fmt(fmt) "efi: " fmt
  10. #include <linux/efi.h>
  11. #include <linux/fwnode.h>
  12. #include <linux/init.h>
  13. #include <linux/memblock.h>
  14. #include <linux/mm_types.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_fdt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/screen_info.h>
  20. #include <asm/efi.h>
  21. static int __init is_memory(efi_memory_desc_t *md)
  22. {
  23. if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
  24. return 1;
  25. return 0;
  26. }
  27. /*
  28. * Translate a EFI virtual address into a physical address: this is necessary,
  29. * as some data members of the EFI system table are virtually remapped after
  30. * SetVirtualAddressMap() has been called.
  31. */
  32. static phys_addr_t __init efi_to_phys(unsigned long addr)
  33. {
  34. efi_memory_desc_t *md;
  35. for_each_efi_memory_desc(md) {
  36. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  37. continue;
  38. if (md->virt_addr == 0)
  39. /* no virtual mapping has been installed by the stub */
  40. break;
  41. if (md->virt_addr <= addr &&
  42. (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
  43. return md->phys_addr + addr - md->virt_addr;
  44. }
  45. return addr;
  46. }
  47. static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
  48. static __initdata unsigned long cpu_state_table = EFI_INVALID_TABLE_ADDR;
  49. static const efi_config_table_type_t arch_tables[] __initconst = {
  50. {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, &screen_info_table},
  51. {LINUX_EFI_ARM_CPU_STATE_TABLE_GUID, &cpu_state_table},
  52. {}
  53. };
  54. static void __init init_screen_info(void)
  55. {
  56. struct screen_info *si;
  57. if (IS_ENABLED(CONFIG_ARM) &&
  58. screen_info_table != EFI_INVALID_TABLE_ADDR) {
  59. si = early_memremap_ro(screen_info_table, sizeof(*si));
  60. if (!si) {
  61. pr_err("Could not map screen_info config table\n");
  62. return;
  63. }
  64. screen_info = *si;
  65. early_memunmap(si, sizeof(*si));
  66. /* dummycon on ARM needs non-zero values for columns/lines */
  67. screen_info.orig_video_cols = 80;
  68. screen_info.orig_video_lines = 25;
  69. }
  70. if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
  71. memblock_is_map_memory(screen_info.lfb_base))
  72. memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
  73. }
  74. static int __init uefi_init(u64 efi_system_table)
  75. {
  76. efi_config_table_t *config_tables;
  77. efi_system_table_t *systab;
  78. size_t table_size;
  79. int retval;
  80. systab = early_memremap_ro(efi_system_table, sizeof(efi_system_table_t));
  81. if (systab == NULL) {
  82. pr_warn("Unable to map EFI system table.\n");
  83. return -ENOMEM;
  84. }
  85. set_bit(EFI_BOOT, &efi.flags);
  86. if (IS_ENABLED(CONFIG_64BIT))
  87. set_bit(EFI_64BIT, &efi.flags);
  88. retval = efi_systab_check_header(&systab->hdr, 2);
  89. if (retval)
  90. goto out;
  91. efi.runtime = systab->runtime;
  92. efi.runtime_version = systab->hdr.revision;
  93. efi_systab_report_header(&systab->hdr, efi_to_phys(systab->fw_vendor));
  94. table_size = sizeof(efi_config_table_t) * systab->nr_tables;
  95. config_tables = early_memremap_ro(efi_to_phys(systab->tables),
  96. table_size);
  97. if (config_tables == NULL) {
  98. pr_warn("Unable to map EFI config table array.\n");
  99. retval = -ENOMEM;
  100. goto out;
  101. }
  102. retval = efi_config_parse_tables(config_tables, systab->nr_tables,
  103. IS_ENABLED(CONFIG_ARM) ? arch_tables
  104. : NULL);
  105. early_memunmap(config_tables, table_size);
  106. out:
  107. early_memunmap(systab, sizeof(efi_system_table_t));
  108. return retval;
  109. }
  110. /*
  111. * Return true for regions that can be used as System RAM.
  112. */
  113. static __init int is_usable_memory(efi_memory_desc_t *md)
  114. {
  115. switch (md->type) {
  116. case EFI_LOADER_CODE:
  117. case EFI_LOADER_DATA:
  118. case EFI_ACPI_RECLAIM_MEMORY:
  119. case EFI_BOOT_SERVICES_CODE:
  120. case EFI_BOOT_SERVICES_DATA:
  121. case EFI_CONVENTIONAL_MEMORY:
  122. case EFI_PERSISTENT_MEMORY:
  123. /*
  124. * Special purpose memory is 'soft reserved', which means it
  125. * is set aside initially, but can be hotplugged back in or
  126. * be assigned to the dax driver after boot.
  127. */
  128. if (efi_soft_reserve_enabled() &&
  129. (md->attribute & EFI_MEMORY_SP))
  130. return false;
  131. /*
  132. * According to the spec, these regions are no longer reserved
  133. * after calling ExitBootServices(). However, we can only use
  134. * them as System RAM if they can be mapped writeback cacheable.
  135. */
  136. return (md->attribute & EFI_MEMORY_WB);
  137. default:
  138. break;
  139. }
  140. return false;
  141. }
  142. static __init void reserve_regions(void)
  143. {
  144. efi_memory_desc_t *md;
  145. u64 paddr, npages, size;
  146. if (efi_enabled(EFI_DBG))
  147. pr_info("Processing EFI memory map:\n");
  148. /*
  149. * Discard memblocks discovered so far: if there are any at this
  150. * point, they originate from memory nodes in the DT, and UEFI
  151. * uses its own memory map instead.
  152. */
  153. memblock_dump_all();
  154. memblock_remove(0, PHYS_ADDR_MAX);
  155. for_each_efi_memory_desc(md) {
  156. paddr = md->phys_addr;
  157. npages = md->num_pages;
  158. if (efi_enabled(EFI_DBG)) {
  159. char buf[64];
  160. pr_info(" 0x%012llx-0x%012llx %s\n",
  161. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  162. efi_md_typeattr_format(buf, sizeof(buf), md));
  163. }
  164. memrange_efi_to_native(&paddr, &npages);
  165. size = npages << PAGE_SHIFT;
  166. if (is_memory(md)) {
  167. early_init_dt_add_memory_arch(paddr, size);
  168. if (!is_usable_memory(md))
  169. memblock_mark_nomap(paddr, size);
  170. /* keep ACPI reclaim memory intact for kexec etc. */
  171. if (md->type == EFI_ACPI_RECLAIM_MEMORY)
  172. memblock_reserve(paddr, size);
  173. }
  174. }
  175. }
  176. void __init efi_init(void)
  177. {
  178. struct efi_memory_map_data data;
  179. u64 efi_system_table;
  180. /* Grab UEFI information placed in FDT by stub */
  181. efi_system_table = efi_get_fdt_params(&data);
  182. if (!efi_system_table)
  183. return;
  184. if (efi_memmap_init_early(&data) < 0) {
  185. /*
  186. * If we are booting via UEFI, the UEFI memory map is the only
  187. * description of memory we have, so there is little point in
  188. * proceeding if we cannot access it.
  189. */
  190. panic("Unable to map EFI memory map.\n");
  191. }
  192. WARN(efi.memmap.desc_version != 1,
  193. "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
  194. efi.memmap.desc_version);
  195. if (uefi_init(efi_system_table) < 0) {
  196. efi_memmap_unmap();
  197. return;
  198. }
  199. reserve_regions();
  200. efi_esrt_init();
  201. efi_mokvar_table_init();
  202. memblock_reserve(data.phys_map & PAGE_MASK,
  203. PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
  204. init_screen_info();
  205. #ifdef CONFIG_ARM
  206. /* ARM does not permit early mappings to persist across paging_init() */
  207. efi_memmap_unmap();
  208. if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
  209. struct efi_arm_entry_state *state;
  210. bool dump_state = true;
  211. state = early_memremap_ro(cpu_state_table,
  212. sizeof(struct efi_arm_entry_state));
  213. if (state == NULL) {
  214. pr_warn("Unable to map CPU entry state table.\n");
  215. return;
  216. }
  217. if ((state->sctlr_before_ebs & 1) == 0)
  218. pr_warn(FW_BUG "EFI stub was entered with MMU and Dcache disabled, please fix your firmware!\n");
  219. else if ((state->sctlr_after_ebs & 1) == 0)
  220. pr_warn(FW_BUG "ExitBootServices() returned with MMU and Dcache disabled, please fix your firmware!\n");
  221. else
  222. dump_state = false;
  223. if (dump_state || efi_enabled(EFI_DBG)) {
  224. pr_info("CPSR at EFI stub entry : 0x%08x\n", state->cpsr_before_ebs);
  225. pr_info("SCTLR at EFI stub entry : 0x%08x\n", state->sctlr_before_ebs);
  226. pr_info("CPSR after ExitBootServices() : 0x%08x\n", state->cpsr_after_ebs);
  227. pr_info("SCTLR after ExitBootServices(): 0x%08x\n", state->sctlr_after_ebs);
  228. }
  229. early_memunmap(state, sizeof(struct efi_arm_entry_state));
  230. }
  231. #endif
  232. }
  233. static bool efifb_overlaps_pci_range(const struct of_pci_range *range)
  234. {
  235. u64 fb_base = screen_info.lfb_base;
  236. if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  237. fb_base |= (u64)(unsigned long)screen_info.ext_lfb_base << 32;
  238. return fb_base >= range->cpu_addr &&
  239. fb_base < (range->cpu_addr + range->size);
  240. }
  241. static struct device_node *find_pci_overlap_node(void)
  242. {
  243. struct device_node *np;
  244. for_each_node_by_type(np, "pci") {
  245. struct of_pci_range_parser parser;
  246. struct of_pci_range range;
  247. int err;
  248. err = of_pci_range_parser_init(&parser, np);
  249. if (err) {
  250. pr_warn("of_pci_range_parser_init() failed: %d\n", err);
  251. continue;
  252. }
  253. for_each_of_pci_range(&parser, &range)
  254. if (efifb_overlaps_pci_range(&range))
  255. return np;
  256. }
  257. return NULL;
  258. }
  259. /*
  260. * If the efifb framebuffer is backed by a PCI graphics controller, we have
  261. * to ensure that this relation is expressed using a device link when
  262. * running in DT mode, or the probe order may be reversed, resulting in a
  263. * resource reservation conflict on the memory window that the efifb
  264. * framebuffer steals from the PCIe host bridge.
  265. */
  266. static int efifb_add_links(struct fwnode_handle *fwnode)
  267. {
  268. struct device_node *sup_np;
  269. sup_np = find_pci_overlap_node();
  270. /*
  271. * If there's no PCI graphics controller backing the efifb, we are
  272. * done here.
  273. */
  274. if (!sup_np)
  275. return 0;
  276. fwnode_link_add(fwnode, of_fwnode_handle(sup_np));
  277. of_node_put(sup_np);
  278. return 0;
  279. }
  280. static const struct fwnode_operations efifb_fwnode_ops = {
  281. .add_links = efifb_add_links,
  282. };
  283. static struct fwnode_handle efifb_fwnode;
  284. static int __init register_gop_device(void)
  285. {
  286. struct platform_device *pd;
  287. int err;
  288. if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
  289. return 0;
  290. pd = platform_device_alloc("efi-framebuffer", 0);
  291. if (!pd)
  292. return -ENOMEM;
  293. if (IS_ENABLED(CONFIG_PCI)) {
  294. fwnode_init(&efifb_fwnode, &efifb_fwnode_ops);
  295. pd->dev.fwnode = &efifb_fwnode;
  296. }
  297. err = platform_device_add_data(pd, &screen_info, sizeof(screen_info));
  298. if (err)
  299. return err;
  300. return platform_device_add(pd);
  301. }
  302. subsys_initcall(register_gop_device);