arm-init.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include <linux/efi.h>
  14. #include <linux/init.h>
  15. #include <linux/memblock.h>
  16. #include <linux/mm_types.h>
  17. #include <linux/of.h>
  18. #include <linux/of_fdt.h>
  19. #include <asm/efi.h>
  20. struct efi_memory_map memmap;
  21. u64 efi_system_table;
  22. static int __init is_normal_ram(efi_memory_desc_t *md)
  23. {
  24. if (md->attribute & EFI_MEMORY_WB)
  25. return 1;
  26. return 0;
  27. }
  28. /*
  29. * Translate a EFI virtual address into a physical address: this is necessary,
  30. * as some data members of the EFI system table are virtually remapped after
  31. * SetVirtualAddressMap() has been called.
  32. */
  33. static phys_addr_t efi_to_phys(unsigned long addr)
  34. {
  35. efi_memory_desc_t *md;
  36. for_each_efi_memory_desc_in_map(&memmap, md) {
  37. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  38. continue;
  39. if (md->virt_addr == 0)
  40. /* no virtual mapping has been installed by the stub */
  41. break;
  42. if (md->virt_addr <= addr &&
  43. (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
  44. return md->phys_addr + addr - md->virt_addr;
  45. }
  46. return addr;
  47. }
  48. static int __init uefi_init(void)
  49. {
  50. efi_char16_t *c16;
  51. void *config_tables;
  52. size_t table_size;
  53. char vendor[100] = "unknown";
  54. int i, retval;
  55. efi.systab = early_memremap_ro(efi_system_table,
  56. sizeof(efi_system_table_t));
  57. if (efi.systab == NULL) {
  58. pr_warn("Unable to map EFI system table.\n");
  59. return -ENOMEM;
  60. }
  61. set_bit(EFI_BOOT, &efi.flags);
  62. if (IS_ENABLED(CONFIG_64BIT))
  63. set_bit(EFI_64BIT, &efi.flags);
  64. /*
  65. * Verify the EFI Table
  66. */
  67. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  68. pr_err("System table signature incorrect\n");
  69. retval = -EINVAL;
  70. goto out;
  71. }
  72. if ((efi.systab->hdr.revision >> 16) < 2)
  73. pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
  74. efi.systab->hdr.revision >> 16,
  75. efi.systab->hdr.revision & 0xffff);
  76. efi.runtime_version = efi.systab->hdr.revision;
  77. /* Show what we know for posterity */
  78. c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor),
  79. sizeof(vendor) * sizeof(efi_char16_t));
  80. if (c16) {
  81. for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
  82. vendor[i] = c16[i];
  83. vendor[i] = '\0';
  84. early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
  85. }
  86. pr_info("EFI v%u.%.02u by %s\n",
  87. efi.systab->hdr.revision >> 16,
  88. efi.systab->hdr.revision & 0xffff, vendor);
  89. table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
  90. config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables),
  91. table_size);
  92. if (config_tables == NULL) {
  93. pr_warn("Unable to map EFI config table array.\n");
  94. retval = -ENOMEM;
  95. goto out;
  96. }
  97. retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
  98. sizeof(efi_config_table_t), NULL);
  99. early_memunmap(config_tables, table_size);
  100. out:
  101. early_memunmap(efi.systab, sizeof(efi_system_table_t));
  102. return retval;
  103. }
  104. /*
  105. * Return true for RAM regions we want to permanently reserve.
  106. */
  107. static __init int is_reserve_region(efi_memory_desc_t *md)
  108. {
  109. switch (md->type) {
  110. case EFI_LOADER_CODE:
  111. case EFI_LOADER_DATA:
  112. case EFI_BOOT_SERVICES_CODE:
  113. case EFI_BOOT_SERVICES_DATA:
  114. case EFI_CONVENTIONAL_MEMORY:
  115. case EFI_PERSISTENT_MEMORY:
  116. return 0;
  117. default:
  118. break;
  119. }
  120. return is_normal_ram(md);
  121. }
  122. static __init void reserve_regions(void)
  123. {
  124. efi_memory_desc_t *md;
  125. u64 paddr, npages, size;
  126. if (efi_enabled(EFI_DBG))
  127. pr_info("Processing EFI memory map:\n");
  128. for_each_efi_memory_desc_in_map(&memmap, md) {
  129. paddr = md->phys_addr;
  130. npages = md->num_pages;
  131. if (efi_enabled(EFI_DBG)) {
  132. char buf[64];
  133. pr_info(" 0x%012llx-0x%012llx %s",
  134. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  135. efi_md_typeattr_format(buf, sizeof(buf), md));
  136. }
  137. memrange_efi_to_native(&paddr, &npages);
  138. size = npages << PAGE_SHIFT;
  139. if (is_normal_ram(md))
  140. early_init_dt_add_memory_arch(paddr, size);
  141. if (is_reserve_region(md)) {
  142. memblock_mark_nomap(paddr, size);
  143. if (efi_enabled(EFI_DBG))
  144. pr_cont("*");
  145. }
  146. if (efi_enabled(EFI_DBG))
  147. pr_cont("\n");
  148. }
  149. set_bit(EFI_MEMMAP, &efi.flags);
  150. }
  151. void __init efi_init(void)
  152. {
  153. struct efi_fdt_params params;
  154. /* Grab UEFI information placed in FDT by stub */
  155. if (!efi_get_fdt_params(&params))
  156. return;
  157. efi_system_table = params.system_table;
  158. memmap.phys_map = params.mmap;
  159. memmap.map = early_memremap_ro(params.mmap, params.mmap_size);
  160. if (memmap.map == NULL) {
  161. /*
  162. * If we are booting via UEFI, the UEFI memory map is the only
  163. * description of memory we have, so there is little point in
  164. * proceeding if we cannot access it.
  165. */
  166. panic("Unable to map EFI memory map.\n");
  167. }
  168. memmap.map_end = memmap.map + params.mmap_size;
  169. memmap.desc_size = params.desc_size;
  170. memmap.desc_version = params.desc_ver;
  171. if (uefi_init() < 0)
  172. return;
  173. reserve_regions();
  174. early_memunmap(memmap.map, params.mmap_size);
  175. if (IS_ENABLED(CONFIG_ARM)) {
  176. /*
  177. * ARM currently does not allow ioremap_cache() to be called on
  178. * memory regions that are covered by struct page. So remove the
  179. * UEFI memory map from the linear mapping.
  180. */
  181. memblock_mark_nomap(params.mmap & PAGE_MASK,
  182. PAGE_ALIGN(params.mmap_size +
  183. (params.mmap & ~PAGE_MASK)));
  184. } else {
  185. memblock_reserve(params.mmap & PAGE_MASK,
  186. PAGE_ALIGN(params.mmap_size +
  187. (params.mmap & ~PAGE_MASK)));
  188. }
  189. }