unaccepted_memory.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/efi.h>
  3. #include <asm/efi.h>
  4. #include "efistub.h"
  5. struct efi_unaccepted_memory *unaccepted_table;
  6. efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc,
  7. struct efi_boot_memmap *map)
  8. {
  9. efi_guid_t unaccepted_table_guid = LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID;
  10. u64 unaccepted_start = ULLONG_MAX, unaccepted_end = 0, bitmap_size;
  11. efi_status_t status;
  12. int i;
  13. /* Check if the table is already installed */
  14. unaccepted_table = get_efi_config_table(unaccepted_table_guid);
  15. if (unaccepted_table) {
  16. if (unaccepted_table->version != 1) {
  17. efi_err("Unknown version of unaccepted memory table\n");
  18. return EFI_UNSUPPORTED;
  19. }
  20. return EFI_SUCCESS;
  21. }
  22. /* Check if there's any unaccepted memory and find the max address */
  23. for (i = 0; i < nr_desc; i++) {
  24. efi_memory_desc_t *d;
  25. unsigned long m = (unsigned long)map->map;
  26. d = efi_memdesc_ptr(m, map->desc_size, i);
  27. if (d->type != EFI_UNACCEPTED_MEMORY)
  28. continue;
  29. unaccepted_start = min(unaccepted_start, d->phys_addr);
  30. unaccepted_end = max(unaccepted_end,
  31. d->phys_addr + d->num_pages * PAGE_SIZE);
  32. }
  33. if (unaccepted_start == ULLONG_MAX)
  34. return EFI_SUCCESS;
  35. unaccepted_start = round_down(unaccepted_start,
  36. EFI_UNACCEPTED_UNIT_SIZE);
  37. unaccepted_end = round_up(unaccepted_end, EFI_UNACCEPTED_UNIT_SIZE);
  38. /*
  39. * If unaccepted memory is present, allocate a bitmap to track what
  40. * memory has to be accepted before access.
  41. *
  42. * One bit in the bitmap represents 2MiB in the address space:
  43. * A 4k bitmap can track 64GiB of physical address space.
  44. *
  45. * In the worst case scenario -- a huge hole in the middle of the
  46. * address space -- It needs 256MiB to handle 4PiB of the address
  47. * space.
  48. *
  49. * The bitmap will be populated in setup_e820() according to the memory
  50. * map after efi_exit_boot_services().
  51. */
  52. bitmap_size = DIV_ROUND_UP(unaccepted_end - unaccepted_start,
  53. EFI_UNACCEPTED_UNIT_SIZE * BITS_PER_BYTE);
  54. status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
  55. sizeof(*unaccepted_table) + bitmap_size,
  56. (void **)&unaccepted_table);
  57. if (status != EFI_SUCCESS) {
  58. efi_err("Failed to allocate unaccepted memory config table\n");
  59. return status;
  60. }
  61. unaccepted_table->version = 1;
  62. unaccepted_table->unit_size = EFI_UNACCEPTED_UNIT_SIZE;
  63. unaccepted_table->phys_base = unaccepted_start;
  64. unaccepted_table->size = bitmap_size;
  65. memset(unaccepted_table->bitmap, 0, bitmap_size);
  66. status = efi_bs_call(install_configuration_table,
  67. &unaccepted_table_guid, unaccepted_table);
  68. if (status != EFI_SUCCESS) {
  69. efi_bs_call(free_pool, unaccepted_table);
  70. efi_err("Failed to install unaccepted memory config table!\n");
  71. }
  72. return status;
  73. }
  74. /*
  75. * The accepted memory bitmap only works at unit_size granularity. Take
  76. * unaligned start/end addresses and either:
  77. * 1. Accepts the memory immediately and in its entirety
  78. * 2. Accepts unaligned parts, and marks *some* aligned part unaccepted
  79. *
  80. * The function will never reach the bitmap_set() with zero bits to set.
  81. */
  82. void process_unaccepted_memory(u64 start, u64 end)
  83. {
  84. u64 unit_size = unaccepted_table->unit_size;
  85. u64 unit_mask = unaccepted_table->unit_size - 1;
  86. u64 bitmap_size = unaccepted_table->size;
  87. /*
  88. * Ensure that at least one bit will be set in the bitmap by
  89. * immediately accepting all regions under 2*unit_size. This is
  90. * imprecise and may immediately accept some areas that could
  91. * have been represented in the bitmap. But, results in simpler
  92. * code below
  93. *
  94. * Consider case like this (assuming unit_size == 2MB):
  95. *
  96. * | 4k | 2044k | 2048k |
  97. * ^ 0x0 ^ 2MB ^ 4MB
  98. *
  99. * Only the first 4k has been accepted. The 0MB->2MB region can not be
  100. * represented in the bitmap. The 2MB->4MB region can be represented in
  101. * the bitmap. But, the 0MB->4MB region is <2*unit_size and will be
  102. * immediately accepted in its entirety.
  103. */
  104. if (end - start < 2 * unit_size) {
  105. arch_accept_memory(start, end);
  106. return;
  107. }
  108. /*
  109. * No matter how the start and end are aligned, at least one unaccepted
  110. * unit_size area will remain to be marked in the bitmap.
  111. */
  112. /* Immediately accept a <unit_size piece at the start: */
  113. if (start & unit_mask) {
  114. arch_accept_memory(start, round_up(start, unit_size));
  115. start = round_up(start, unit_size);
  116. }
  117. /* Immediately accept a <unit_size piece at the end: */
  118. if (end & unit_mask) {
  119. arch_accept_memory(round_down(end, unit_size), end);
  120. end = round_down(end, unit_size);
  121. }
  122. /*
  123. * Accept part of the range that before phys_base and cannot be recorded
  124. * into the bitmap.
  125. */
  126. if (start < unaccepted_table->phys_base) {
  127. arch_accept_memory(start,
  128. min(unaccepted_table->phys_base, end));
  129. start = unaccepted_table->phys_base;
  130. }
  131. /* Nothing to record */
  132. if (end < unaccepted_table->phys_base)
  133. return;
  134. /* Translate to offsets from the beginning of the bitmap */
  135. start -= unaccepted_table->phys_base;
  136. end -= unaccepted_table->phys_base;
  137. /* Accept memory that doesn't fit into bitmap */
  138. if (end > bitmap_size * unit_size * BITS_PER_BYTE) {
  139. unsigned long phys_start, phys_end;
  140. phys_start = bitmap_size * unit_size * BITS_PER_BYTE +
  141. unaccepted_table->phys_base;
  142. phys_end = end + unaccepted_table->phys_base;
  143. arch_accept_memory(phys_start, phys_end);
  144. end = bitmap_size * unit_size * BITS_PER_BYTE;
  145. }
  146. /*
  147. * 'start' and 'end' are now both unit_size-aligned.
  148. * Record the range as being unaccepted:
  149. */
  150. bitmap_set(unaccepted_table->bitmap,
  151. start / unit_size, (end - start) / unit_size);
  152. }
  153. void accept_memory(phys_addr_t start, unsigned long size)
  154. {
  155. unsigned long range_start, range_end;
  156. phys_addr_t end = start + size;
  157. unsigned long bitmap_size;
  158. u64 unit_size;
  159. if (!unaccepted_table)
  160. return;
  161. unit_size = unaccepted_table->unit_size;
  162. /*
  163. * Only care for the part of the range that is represented
  164. * in the bitmap.
  165. */
  166. if (start < unaccepted_table->phys_base)
  167. start = unaccepted_table->phys_base;
  168. if (end < unaccepted_table->phys_base)
  169. return;
  170. /* Translate to offsets from the beginning of the bitmap */
  171. start -= unaccepted_table->phys_base;
  172. end -= unaccepted_table->phys_base;
  173. /* Make sure not to overrun the bitmap */
  174. if (end > unaccepted_table->size * unit_size * BITS_PER_BYTE)
  175. end = unaccepted_table->size * unit_size * BITS_PER_BYTE;
  176. range_start = start / unit_size;
  177. bitmap_size = DIV_ROUND_UP(end, unit_size);
  178. for_each_set_bitrange_from(range_start, range_end,
  179. unaccepted_table->bitmap, bitmap_size) {
  180. unsigned long phys_start, phys_end;
  181. phys_start = range_start * unit_size + unaccepted_table->phys_base;
  182. phys_end = range_end * unit_size + unaccepted_table->phys_base;
  183. arch_accept_memory(phys_start, phys_end);
  184. bitmap_clear(unaccepted_table->bitmap,
  185. range_start, range_end - range_start);
  186. }
  187. }