arm-init.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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(&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. /* Show what we know for posterity */
  77. c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor),
  78. sizeof(vendor) * sizeof(efi_char16_t));
  79. if (c16) {
  80. for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
  81. vendor[i] = c16[i];
  82. vendor[i] = '\0';
  83. early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
  84. }
  85. pr_info("EFI v%u.%.02u by %s\n",
  86. efi.systab->hdr.revision >> 16,
  87. efi.systab->hdr.revision & 0xffff, vendor);
  88. table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
  89. config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables),
  90. table_size);
  91. if (config_tables == NULL) {
  92. pr_warn("Unable to map EFI config table array.\n");
  93. retval = -ENOMEM;
  94. goto out;
  95. }
  96. retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
  97. sizeof(efi_config_table_t), NULL);
  98. early_memunmap(config_tables, table_size);
  99. out:
  100. early_memunmap(efi.systab, sizeof(efi_system_table_t));
  101. return retval;
  102. }
  103. /*
  104. * Return true for RAM regions we want to permanently reserve.
  105. */
  106. static __init int is_reserve_region(efi_memory_desc_t *md)
  107. {
  108. switch (md->type) {
  109. case EFI_LOADER_CODE:
  110. case EFI_LOADER_DATA:
  111. case EFI_BOOT_SERVICES_CODE:
  112. case EFI_BOOT_SERVICES_DATA:
  113. case EFI_CONVENTIONAL_MEMORY:
  114. case EFI_PERSISTENT_MEMORY:
  115. return 0;
  116. default:
  117. break;
  118. }
  119. return is_normal_ram(md);
  120. }
  121. static __init void reserve_regions(void)
  122. {
  123. efi_memory_desc_t *md;
  124. u64 paddr, npages, size;
  125. if (efi_enabled(EFI_DBG))
  126. pr_info("Processing EFI memory map:\n");
  127. for_each_efi_memory_desc(&memmap, md) {
  128. paddr = md->phys_addr;
  129. npages = md->num_pages;
  130. if (efi_enabled(EFI_DBG)) {
  131. char buf[64];
  132. pr_info(" 0x%012llx-0x%012llx %s",
  133. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  134. efi_md_typeattr_format(buf, sizeof(buf), md));
  135. }
  136. memrange_efi_to_native(&paddr, &npages);
  137. size = npages << PAGE_SHIFT;
  138. if (is_normal_ram(md))
  139. early_init_dt_add_memory_arch(paddr, size);
  140. if (is_reserve_region(md)) {
  141. memblock_mark_nomap(paddr, size);
  142. if (efi_enabled(EFI_DBG))
  143. pr_cont("*");
  144. }
  145. if (efi_enabled(EFI_DBG))
  146. pr_cont("\n");
  147. }
  148. set_bit(EFI_MEMMAP, &efi.flags);
  149. }
  150. void __init efi_init(void)
  151. {
  152. struct efi_fdt_params params;
  153. /* Grab UEFI information placed in FDT by stub */
  154. if (!efi_get_fdt_params(&params))
  155. return;
  156. efi_system_table = params.system_table;
  157. memmap.phys_map = params.mmap;
  158. memmap.map = early_memremap_ro(params.mmap, params.mmap_size);
  159. if (memmap.map == NULL) {
  160. /*
  161. * If we are booting via UEFI, the UEFI memory map is the only
  162. * description of memory we have, so there is little point in
  163. * proceeding if we cannot access it.
  164. */
  165. panic("Unable to map EFI memory map.\n");
  166. }
  167. memmap.map_end = memmap.map + params.mmap_size;
  168. memmap.desc_size = params.desc_size;
  169. memmap.desc_version = params.desc_ver;
  170. if (uefi_init() < 0)
  171. return;
  172. reserve_regions();
  173. early_memunmap(memmap.map, params.mmap_size);
  174. memblock_mark_nomap(params.mmap & PAGE_MASK,
  175. PAGE_ALIGN(params.mmap_size +
  176. (params.mmap & ~PAGE_MASK)));
  177. }