memblock.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include <linux/kernel.h>
  2. #include <linux/types.h>
  3. #include <linux/init.h>
  4. #include <linux/bitops.h>
  5. #include <linux/memblock.h>
  6. #include <linux/bootmem.h>
  7. #include <linux/mm.h>
  8. #include <linux/range.h>
  9. /* Check for already reserved areas */
  10. bool __init memblock_x86_check_reserved_size(u64 *addrp, u64 *sizep, u64 align)
  11. {
  12. struct memblock_region *r;
  13. u64 addr = *addrp, last;
  14. u64 size = *sizep;
  15. bool changed = false;
  16. again:
  17. last = addr + size;
  18. for_each_memblock(reserved, r) {
  19. if (last > r->base && addr < r->base) {
  20. size = r->base - addr;
  21. changed = true;
  22. goto again;
  23. }
  24. if (last > (r->base + r->size) && addr < (r->base + r->size)) {
  25. addr = round_up(r->base + r->size, align);
  26. size = last - addr;
  27. changed = true;
  28. goto again;
  29. }
  30. if (last <= (r->base + r->size) && addr >= r->base) {
  31. *sizep = 0;
  32. return false;
  33. }
  34. }
  35. if (changed) {
  36. *addrp = addr;
  37. *sizep = size;
  38. }
  39. return changed;
  40. }
  41. /*
  42. * Find next free range after start, and size is returned in *sizep
  43. */
  44. u64 __init memblock_x86_find_in_range_size(u64 start, u64 *sizep, u64 align)
  45. {
  46. struct memblock_region *r;
  47. for_each_memblock(memory, r) {
  48. u64 ei_start = r->base;
  49. u64 ei_last = ei_start + r->size;
  50. u64 addr;
  51. addr = round_up(ei_start, align);
  52. if (addr < start)
  53. addr = round_up(start, align);
  54. if (addr >= ei_last)
  55. continue;
  56. *sizep = ei_last - addr;
  57. while (memblock_x86_check_reserved_size(&addr, sizep, align))
  58. ;
  59. if (*sizep)
  60. return addr;
  61. }
  62. return MEMBLOCK_ERROR;
  63. }
  64. static __init struct range *find_range_array(int count)
  65. {
  66. u64 end, size, mem;
  67. struct range *range;
  68. size = sizeof(struct range) * count;
  69. end = memblock.current_limit;
  70. mem = memblock_find_in_range(0, end, size, sizeof(struct range));
  71. if (mem == MEMBLOCK_ERROR)
  72. panic("can not find more space for range array");
  73. /*
  74. * This range is tempoaray, so don't reserve it, it will not be
  75. * overlapped because We will not alloccate new buffer before
  76. * We discard this one
  77. */
  78. range = __va(mem);
  79. memset(range, 0, size);
  80. return range;
  81. }
  82. static void __init memblock_x86_subtract_reserved(struct range *range, int az)
  83. {
  84. u64 final_start, final_end;
  85. struct memblock_region *r;
  86. /* Take out region array itself at first*/
  87. memblock_free_reserved_regions();
  88. memblock_dbg("Subtract (%ld early reservations)\n", memblock.reserved.cnt);
  89. for_each_memblock(reserved, r) {
  90. memblock_dbg(" [%010llx-%010llx]\n", (u64)r->base, (u64)r->base + r->size - 1);
  91. final_start = PFN_DOWN(r->base);
  92. final_end = PFN_UP(r->base + r->size);
  93. if (final_start >= final_end)
  94. continue;
  95. subtract_range(range, az, final_start, final_end);
  96. }
  97. /* Put region array back ? */
  98. memblock_reserve_reserved_regions();
  99. }
  100. struct count_data {
  101. int nr;
  102. };
  103. static int __init count_work_fn(unsigned long start_pfn,
  104. unsigned long end_pfn, void *datax)
  105. {
  106. struct count_data *data = datax;
  107. data->nr++;
  108. return 0;
  109. }
  110. static int __init count_early_node_map(int nodeid)
  111. {
  112. struct count_data data;
  113. data.nr = 0;
  114. work_with_active_regions(nodeid, count_work_fn, &data);
  115. return data.nr;
  116. }
  117. int __init __get_free_all_memory_range(struct range **rangep, int nodeid,
  118. unsigned long start_pfn, unsigned long end_pfn)
  119. {
  120. int count;
  121. struct range *range;
  122. int nr_range;
  123. count = (memblock.reserved.cnt + count_early_node_map(nodeid)) * 2;
  124. range = find_range_array(count);
  125. nr_range = 0;
  126. /*
  127. * Use early_node_map[] and memblock.reserved.region to get range array
  128. * at first
  129. */
  130. nr_range = add_from_early_node_map(range, count, nr_range, nodeid);
  131. subtract_range(range, count, 0, start_pfn);
  132. subtract_range(range, count, end_pfn, -1ULL);
  133. memblock_x86_subtract_reserved(range, count);
  134. nr_range = clean_sort_range(range, count);
  135. *rangep = range;
  136. return nr_range;
  137. }
  138. int __init get_free_all_memory_range(struct range **rangep, int nodeid)
  139. {
  140. unsigned long end_pfn = -1UL;
  141. #ifdef CONFIG_X86_32
  142. end_pfn = max_low_pfn;
  143. #endif
  144. return __get_free_all_memory_range(rangep, nodeid, 0, end_pfn);
  145. }
  146. static u64 __init __memblock_x86_memory_in_range(u64 addr, u64 limit, bool get_free)
  147. {
  148. int i, count;
  149. struct range *range;
  150. int nr_range;
  151. u64 final_start, final_end;
  152. u64 free_size;
  153. struct memblock_region *r;
  154. count = (memblock.reserved.cnt + memblock.memory.cnt) * 2;
  155. range = find_range_array(count);
  156. nr_range = 0;
  157. addr = PFN_UP(addr);
  158. limit = PFN_DOWN(limit);
  159. for_each_memblock(memory, r) {
  160. final_start = PFN_UP(r->base);
  161. final_end = PFN_DOWN(r->base + r->size);
  162. if (final_start >= final_end)
  163. continue;
  164. if (final_start >= limit || final_end <= addr)
  165. continue;
  166. nr_range = add_range(range, count, nr_range, final_start, final_end);
  167. }
  168. subtract_range(range, count, 0, addr);
  169. subtract_range(range, count, limit, -1ULL);
  170. /* Subtract memblock.reserved.region in range ? */
  171. if (!get_free)
  172. goto sort_and_count_them;
  173. for_each_memblock(reserved, r) {
  174. final_start = PFN_DOWN(r->base);
  175. final_end = PFN_UP(r->base + r->size);
  176. if (final_start >= final_end)
  177. continue;
  178. if (final_start >= limit || final_end <= addr)
  179. continue;
  180. subtract_range(range, count, final_start, final_end);
  181. }
  182. sort_and_count_them:
  183. nr_range = clean_sort_range(range, count);
  184. free_size = 0;
  185. for (i = 0; i < nr_range; i++)
  186. free_size += range[i].end - range[i].start;
  187. return free_size << PAGE_SHIFT;
  188. }
  189. u64 __init memblock_x86_free_memory_in_range(u64 addr, u64 limit)
  190. {
  191. return __memblock_x86_memory_in_range(addr, limit, true);
  192. }
  193. u64 __init memblock_x86_memory_in_range(u64 addr, u64 limit)
  194. {
  195. return __memblock_x86_memory_in_range(addr, limit, false);
  196. }
  197. void __init memblock_x86_reserve_range(u64 start, u64 end, char *name)
  198. {
  199. if (start == end)
  200. return;
  201. if (WARN_ONCE(start > end, "memblock_x86_reserve_range: wrong range [%#llx, %#llx)\n", start, end))
  202. return;
  203. memblock_dbg(" memblock_x86_reserve_range: [%#010llx-%#010llx] %16s\n", start, end - 1, name);
  204. memblock_reserve(start, end - start);
  205. }
  206. void __init memblock_x86_free_range(u64 start, u64 end)
  207. {
  208. if (start == end)
  209. return;
  210. if (WARN_ONCE(start > end, "memblock_x86_free_range: wrong range [%#llx, %#llx)\n", start, end))
  211. return;
  212. memblock_dbg(" memblock_x86_free_range: [%#010llx-%#010llx]\n", start, end - 1);
  213. memblock_free(start, end - start);
  214. }
  215. /*
  216. * Need to call this function after memblock_x86_register_active_regions,
  217. * so early_node_map[] is filled already.
  218. */
  219. u64 __init memblock_x86_find_in_range_node(int nid, u64 start, u64 end, u64 size, u64 align)
  220. {
  221. u64 addr;
  222. addr = find_memory_core_early(nid, size, align, start, end);
  223. if (addr != MEMBLOCK_ERROR)
  224. return addr;
  225. /* Fallback, should already have start end within node range */
  226. return memblock_find_in_range(start, end, size, align);
  227. }
  228. /*
  229. * Finds an active region in the address range from start_pfn to last_pfn and
  230. * returns its range in ei_startpfn and ei_endpfn for the memblock entry.
  231. */
  232. static int __init memblock_x86_find_active_region(const struct memblock_region *ei,
  233. unsigned long start_pfn,
  234. unsigned long last_pfn,
  235. unsigned long *ei_startpfn,
  236. unsigned long *ei_endpfn)
  237. {
  238. u64 align = PAGE_SIZE;
  239. *ei_startpfn = round_up(ei->base, align) >> PAGE_SHIFT;
  240. *ei_endpfn = round_down(ei->base + ei->size, align) >> PAGE_SHIFT;
  241. /* Skip map entries smaller than a page */
  242. if (*ei_startpfn >= *ei_endpfn)
  243. return 0;
  244. /* Skip if map is outside the node */
  245. if (*ei_endpfn <= start_pfn || *ei_startpfn >= last_pfn)
  246. return 0;
  247. /* Check for overlaps */
  248. if (*ei_startpfn < start_pfn)
  249. *ei_startpfn = start_pfn;
  250. if (*ei_endpfn > last_pfn)
  251. *ei_endpfn = last_pfn;
  252. return 1;
  253. }
  254. /* Walk the memblock.memory map and register active regions within a node */
  255. void __init memblock_x86_register_active_regions(int nid, unsigned long start_pfn,
  256. unsigned long last_pfn)
  257. {
  258. unsigned long ei_startpfn;
  259. unsigned long ei_endpfn;
  260. struct memblock_region *r;
  261. for_each_memblock(memory, r)
  262. if (memblock_x86_find_active_region(r, start_pfn, last_pfn,
  263. &ei_startpfn, &ei_endpfn))
  264. add_active_range(nid, ei_startpfn, ei_endpfn);
  265. }
  266. /*
  267. * Find the hole size (in bytes) in the memory range.
  268. * @start: starting address of the memory range to scan
  269. * @end: ending address of the memory range to scan
  270. */
  271. u64 __init memblock_x86_hole_size(u64 start, u64 end)
  272. {
  273. unsigned long start_pfn = start >> PAGE_SHIFT;
  274. unsigned long last_pfn = end >> PAGE_SHIFT;
  275. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  276. struct memblock_region *r;
  277. for_each_memblock(memory, r)
  278. if (memblock_x86_find_active_region(r, start_pfn, last_pfn,
  279. &ei_startpfn, &ei_endpfn))
  280. ram += ei_endpfn - ei_startpfn;
  281. return end - start - ((u64)ram << PAGE_SHIFT);
  282. }