memmap.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common EFI memory map functions.
  4. */
  5. #define pr_fmt(fmt) "efi: " fmt
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/efi.h>
  9. #include <linux/io.h>
  10. #include <linux/memblock.h>
  11. #include <linux/slab.h>
  12. #include <asm/early_ioremap.h>
  13. #include <asm/efi.h>
  14. /**
  15. * __efi_memmap_init - Common code for mapping the EFI memory map
  16. * @data: EFI memory map data
  17. *
  18. * This function takes care of figuring out which function to use to
  19. * map the EFI memory map in efi.memmap based on how far into the boot
  20. * we are.
  21. *
  22. * During bootup EFI_MEMMAP_LATE in data->flags should be clear since we
  23. * only have access to the early_memremap*() functions as the vmalloc
  24. * space isn't setup. Once the kernel is fully booted we can fallback
  25. * to the more robust memremap*() API.
  26. *
  27. * Returns: zero on success, a negative error code on failure.
  28. */
  29. int __init __efi_memmap_init(struct efi_memory_map_data *data)
  30. {
  31. struct efi_memory_map map;
  32. phys_addr_t phys_map;
  33. phys_map = data->phys_map;
  34. if (data->flags & EFI_MEMMAP_LATE)
  35. map.map = memremap(phys_map, data->size, MEMREMAP_WB);
  36. else
  37. map.map = early_memremap(phys_map, data->size);
  38. if (!map.map) {
  39. pr_err("Could not map the memory map! phys_map=%pa, size=0x%lx\n",
  40. &phys_map, data->size);
  41. return -ENOMEM;
  42. }
  43. map.phys_map = data->phys_map;
  44. map.nr_map = data->size / data->desc_size;
  45. map.map_end = map.map + data->size;
  46. map.desc_version = data->desc_version;
  47. map.desc_size = data->desc_size;
  48. map.flags = data->flags;
  49. set_bit(EFI_MEMMAP, &efi.flags);
  50. efi.memmap = map;
  51. return 0;
  52. }
  53. /**
  54. * efi_memmap_init_early - Map the EFI memory map data structure
  55. * @data: EFI memory map data
  56. *
  57. * Use early_memremap() to map the passed in EFI memory map and assign
  58. * it to efi.memmap.
  59. *
  60. * Returns: zero on success, a negative error code on failure.
  61. */
  62. int __init efi_memmap_init_early(struct efi_memory_map_data *data)
  63. {
  64. /* Cannot go backwards */
  65. WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
  66. data->flags = 0;
  67. return __efi_memmap_init(data);
  68. }
  69. void __init efi_memmap_unmap(void)
  70. {
  71. if (!efi_enabled(EFI_MEMMAP))
  72. return;
  73. if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) {
  74. unsigned long size;
  75. size = efi.memmap.desc_size * efi.memmap.nr_map;
  76. early_memunmap(efi.memmap.map, size);
  77. } else {
  78. memunmap(efi.memmap.map);
  79. }
  80. efi.memmap.map = NULL;
  81. clear_bit(EFI_MEMMAP, &efi.flags);
  82. }
  83. /**
  84. * efi_memmap_init_late - Map efi.memmap with memremap()
  85. * @addr: Physical address of the new EFI memory map
  86. * @size: Size in bytes of the new EFI memory map
  87. *
  88. * Setup a mapping of the EFI memory map using ioremap_cache(). This
  89. * function should only be called once the vmalloc space has been
  90. * setup and is therefore not suitable for calling during early EFI
  91. * initialise, e.g. in efi_init(). Additionally, it expects
  92. * efi_memmap_init_early() to have already been called.
  93. *
  94. * The reason there are two EFI memmap initialisation
  95. * (efi_memmap_init_early() and this late version) is because the
  96. * early EFI memmap should be explicitly unmapped once EFI
  97. * initialisation is complete as the fixmap space used to map the EFI
  98. * memmap (via early_memremap()) is a scarce resource.
  99. *
  100. * This late mapping is intended to persist for the duration of
  101. * runtime so that things like efi_mem_desc_lookup() and
  102. * efi_mem_attributes() always work.
  103. *
  104. * Returns: zero on success, a negative error code on failure.
  105. */
  106. int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)
  107. {
  108. struct efi_memory_map_data data = {
  109. .phys_map = addr,
  110. .size = size,
  111. .flags = EFI_MEMMAP_LATE,
  112. };
  113. /* Did we forget to unmap the early EFI memmap? */
  114. WARN_ON(efi.memmap.map);
  115. /* Were we already called? */
  116. WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
  117. /*
  118. * It makes no sense to allow callers to register different
  119. * values for the following fields. Copy them out of the
  120. * existing early EFI memmap.
  121. */
  122. data.desc_version = efi.memmap.desc_version;
  123. data.desc_size = efi.memmap.desc_size;
  124. return __efi_memmap_init(&data);
  125. }