arm-init.c 7.1 KB

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