arm-init.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_normal_ram(efi_memory_desc_t *md)
  25. {
  26. if (md->attribute & EFI_MEMORY_WB)
  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. early_memunmap(config_tables, table_size);
  127. out:
  128. early_memunmap(efi.systab, sizeof(efi_system_table_t));
  129. return retval;
  130. }
  131. /*
  132. * Return true for RAM regions we want to permanently reserve.
  133. */
  134. static __init int is_reserve_region(efi_memory_desc_t *md)
  135. {
  136. switch (md->type) {
  137. case EFI_LOADER_CODE:
  138. case EFI_LOADER_DATA:
  139. case EFI_BOOT_SERVICES_CODE:
  140. case EFI_BOOT_SERVICES_DATA:
  141. case EFI_CONVENTIONAL_MEMORY:
  142. case EFI_PERSISTENT_MEMORY:
  143. return 0;
  144. default:
  145. break;
  146. }
  147. return is_normal_ram(md);
  148. }
  149. static __init void reserve_regions(void)
  150. {
  151. efi_memory_desc_t *md;
  152. u64 paddr, npages, size;
  153. int resv;
  154. if (efi_enabled(EFI_DBG))
  155. pr_info("Processing EFI memory map:\n");
  156. /*
  157. * Discard memblocks discovered so far: if there are any at this
  158. * point, they originate from memory nodes in the DT, and UEFI
  159. * uses its own memory map instead.
  160. */
  161. memblock_dump_all();
  162. memblock_remove(0, (phys_addr_t)ULLONG_MAX);
  163. for_each_efi_memory_desc(md) {
  164. paddr = md->phys_addr;
  165. npages = md->num_pages;
  166. resv = is_reserve_region(md);
  167. if (efi_enabled(EFI_DBG)) {
  168. char buf[64];
  169. pr_info(" 0x%012llx-0x%012llx %s%s\n",
  170. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  171. efi_md_typeattr_format(buf, sizeof(buf), md),
  172. resv ? "*" : "");
  173. }
  174. memrange_efi_to_native(&paddr, &npages);
  175. size = npages << PAGE_SHIFT;
  176. if (is_normal_ram(md))
  177. early_init_dt_add_memory_arch(paddr, size);
  178. if (resv)
  179. memblock_mark_nomap(paddr, size);
  180. }
  181. set_bit(EFI_MEMMAP, &efi.flags);
  182. }
  183. void __init efi_init(void)
  184. {
  185. struct efi_fdt_params params;
  186. /* Grab UEFI information placed in FDT by stub */
  187. if (!efi_get_fdt_params(&params))
  188. return;
  189. efi_system_table = params.system_table;
  190. efi.memmap.phys_map = params.mmap;
  191. efi.memmap.map = early_memremap_ro(params.mmap, params.mmap_size);
  192. if (efi.memmap.map == NULL) {
  193. /*
  194. * If we are booting via UEFI, the UEFI memory map is the only
  195. * description of memory we have, so there is little point in
  196. * proceeding if we cannot access it.
  197. */
  198. panic("Unable to map EFI memory map.\n");
  199. }
  200. efi.memmap.map_end = efi.memmap.map + params.mmap_size;
  201. efi.memmap.desc_size = params.desc_size;
  202. efi.memmap.desc_version = params.desc_ver;
  203. WARN(efi.memmap.desc_version != 1,
  204. "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
  205. efi.memmap.desc_version);
  206. if (uefi_init() < 0)
  207. return;
  208. reserve_regions();
  209. efi_memattr_init();
  210. early_memunmap(efi.memmap.map, params.mmap_size);
  211. memblock_reserve(params.mmap & PAGE_MASK,
  212. PAGE_ALIGN(params.mmap_size +
  213. (params.mmap & ~PAGE_MASK)));
  214. init_screen_info();
  215. }
  216. static int __init register_gop_device(void)
  217. {
  218. void *pd;
  219. if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
  220. return 0;
  221. pd = platform_device_register_data(NULL, "efi-framebuffer", 0,
  222. &screen_info, sizeof(screen_info));
  223. return PTR_ERR_OR_ZERO(pd);
  224. }
  225. subsys_initcall(register_gop_device);