memblock.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /*
  2. * Procedures for maintaining information about logical memory blocks.
  3. *
  4. * Peter Bergner, IBM Corp. June 2001.
  5. * Copyright (C) 2001 Peter Bergner.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/bitops.h>
  16. #include <linux/poison.h>
  17. #include <linux/pfn.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/memblock.h>
  21. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  22. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  23. struct memblock memblock __initdata_memblock = {
  24. .memory.regions = memblock_memory_init_regions,
  25. .memory.cnt = 1, /* empty dummy entry */
  26. .memory.max = INIT_MEMBLOCK_REGIONS,
  27. .reserved.regions = memblock_reserved_init_regions,
  28. .reserved.cnt = 1, /* empty dummy entry */
  29. .reserved.max = INIT_MEMBLOCK_REGIONS,
  30. .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
  31. };
  32. int memblock_debug __initdata_memblock;
  33. static int memblock_can_resize __initdata_memblock;
  34. static int memblock_memory_in_slab __initdata_memblock = 0;
  35. static int memblock_reserved_in_slab __initdata_memblock = 0;
  36. /* inline so we don't get a warning when pr_debug is compiled out */
  37. static __init_memblock const char *
  38. memblock_type_name(struct memblock_type *type)
  39. {
  40. if (type == &memblock.memory)
  41. return "memory";
  42. else if (type == &memblock.reserved)
  43. return "reserved";
  44. else
  45. return "unknown";
  46. }
  47. /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
  48. static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
  49. {
  50. return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
  51. }
  52. /*
  53. * Address comparison utilities
  54. */
  55. static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
  56. phys_addr_t base2, phys_addr_t size2)
  57. {
  58. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  59. }
  60. static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
  61. phys_addr_t base, phys_addr_t size)
  62. {
  63. unsigned long i;
  64. for (i = 0; i < type->cnt; i++) {
  65. phys_addr_t rgnbase = type->regions[i].base;
  66. phys_addr_t rgnsize = type->regions[i].size;
  67. if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
  68. break;
  69. }
  70. return (i < type->cnt) ? i : -1;
  71. }
  72. /**
  73. * __memblock_find_range_top_down - find free area utility, in top-down
  74. * @start: start of candidate range
  75. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  76. * @size: size of free area to find
  77. * @align: alignment of free area to find
  78. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  79. *
  80. * Utility called from memblock_find_in_range_node(), find free area top-down.
  81. *
  82. * RETURNS:
  83. * Found address on success, %0 on failure.
  84. */
  85. static phys_addr_t __init_memblock
  86. __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  87. phys_addr_t size, phys_addr_t align, int nid)
  88. {
  89. phys_addr_t this_start, this_end, cand;
  90. u64 i;
  91. for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
  92. this_start = clamp(this_start, start, end);
  93. this_end = clamp(this_end, start, end);
  94. if (this_end < size)
  95. continue;
  96. cand = round_down(this_end - size, align);
  97. if (cand >= this_start)
  98. return cand;
  99. }
  100. return 0;
  101. }
  102. /**
  103. * memblock_find_in_range_node - find free area in given range and node
  104. * @start: start of candidate range
  105. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  106. * @size: size of free area to find
  107. * @align: alignment of free area to find
  108. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  109. *
  110. * Find @size free area aligned to @align in the specified range and node.
  111. *
  112. * RETURNS:
  113. * Found address on success, %0 on failure.
  114. */
  115. phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,
  116. phys_addr_t end, phys_addr_t size,
  117. phys_addr_t align, int nid)
  118. {
  119. /* pump up @end */
  120. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  121. end = memblock.current_limit;
  122. /* avoid allocating the first page */
  123. start = max_t(phys_addr_t, start, PAGE_SIZE);
  124. end = max(start, end);
  125. return __memblock_find_range_top_down(start, end, size, align, nid);
  126. }
  127. /**
  128. * memblock_find_in_range - find free area in given range
  129. * @start: start of candidate range
  130. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  131. * @size: size of free area to find
  132. * @align: alignment of free area to find
  133. *
  134. * Find @size free area aligned to @align in the specified range.
  135. *
  136. * RETURNS:
  137. * Found address on success, %0 on failure.
  138. */
  139. phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
  140. phys_addr_t end, phys_addr_t size,
  141. phys_addr_t align)
  142. {
  143. return memblock_find_in_range_node(start, end, size, align,
  144. MAX_NUMNODES);
  145. }
  146. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  147. {
  148. type->total_size -= type->regions[r].size;
  149. memmove(&type->regions[r], &type->regions[r + 1],
  150. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  151. type->cnt--;
  152. /* Special case for empty arrays */
  153. if (type->cnt == 0) {
  154. WARN_ON(type->total_size != 0);
  155. type->cnt = 1;
  156. type->regions[0].base = 0;
  157. type->regions[0].size = 0;
  158. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  159. }
  160. }
  161. phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
  162. phys_addr_t *addr)
  163. {
  164. if (memblock.reserved.regions == memblock_reserved_init_regions)
  165. return 0;
  166. *addr = __pa(memblock.reserved.regions);
  167. return PAGE_ALIGN(sizeof(struct memblock_region) *
  168. memblock.reserved.max);
  169. }
  170. /**
  171. * memblock_double_array - double the size of the memblock regions array
  172. * @type: memblock type of the regions array being doubled
  173. * @new_area_start: starting address of memory range to avoid overlap with
  174. * @new_area_size: size of memory range to avoid overlap with
  175. *
  176. * Double the size of the @type regions array. If memblock is being used to
  177. * allocate memory for a new reserved regions array and there is a previously
  178. * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
  179. * waiting to be reserved, ensure the memory used by the new array does
  180. * not overlap.
  181. *
  182. * RETURNS:
  183. * 0 on success, -1 on failure.
  184. */
  185. static int __init_memblock memblock_double_array(struct memblock_type *type,
  186. phys_addr_t new_area_start,
  187. phys_addr_t new_area_size)
  188. {
  189. struct memblock_region *new_array, *old_array;
  190. phys_addr_t old_alloc_size, new_alloc_size;
  191. phys_addr_t old_size, new_size, addr;
  192. int use_slab = slab_is_available();
  193. int *in_slab;
  194. /* We don't allow resizing until we know about the reserved regions
  195. * of memory that aren't suitable for allocation
  196. */
  197. if (!memblock_can_resize)
  198. return -1;
  199. /* Calculate new doubled size */
  200. old_size = type->max * sizeof(struct memblock_region);
  201. new_size = old_size << 1;
  202. /*
  203. * We need to allocated new one align to PAGE_SIZE,
  204. * so we can free them completely later.
  205. */
  206. old_alloc_size = PAGE_ALIGN(old_size);
  207. new_alloc_size = PAGE_ALIGN(new_size);
  208. /* Retrieve the slab flag */
  209. if (type == &memblock.memory)
  210. in_slab = &memblock_memory_in_slab;
  211. else
  212. in_slab = &memblock_reserved_in_slab;
  213. /* Try to find some space for it.
  214. *
  215. * WARNING: We assume that either slab_is_available() and we use it or
  216. * we use MEMBLOCK for allocations. That means that this is unsafe to
  217. * use when bootmem is currently active (unless bootmem itself is
  218. * implemented on top of MEMBLOCK which isn't the case yet)
  219. *
  220. * This should however not be an issue for now, as we currently only
  221. * call into MEMBLOCK while it's still active, or much later when slab
  222. * is active for memory hotplug operations
  223. */
  224. if (use_slab) {
  225. new_array = kmalloc(new_size, GFP_KERNEL);
  226. addr = new_array ? __pa(new_array) : 0;
  227. } else {
  228. /* only exclude range when trying to double reserved.regions */
  229. if (type != &memblock.reserved)
  230. new_area_start = new_area_size = 0;
  231. addr = memblock_find_in_range(new_area_start + new_area_size,
  232. memblock.current_limit,
  233. new_alloc_size, PAGE_SIZE);
  234. if (!addr && new_area_size)
  235. addr = memblock_find_in_range(0,
  236. min(new_area_start, memblock.current_limit),
  237. new_alloc_size, PAGE_SIZE);
  238. new_array = addr ? __va(addr) : NULL;
  239. }
  240. if (!addr) {
  241. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  242. memblock_type_name(type), type->max, type->max * 2);
  243. return -1;
  244. }
  245. memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
  246. memblock_type_name(type), type->max * 2, (u64)addr,
  247. (u64)addr + new_size - 1);
  248. /*
  249. * Found space, we now need to move the array over before we add the
  250. * reserved region since it may be our reserved array itself that is
  251. * full.
  252. */
  253. memcpy(new_array, type->regions, old_size);
  254. memset(new_array + type->max, 0, old_size);
  255. old_array = type->regions;
  256. type->regions = new_array;
  257. type->max <<= 1;
  258. /* Free old array. We needn't free it if the array is the static one */
  259. if (*in_slab)
  260. kfree(old_array);
  261. else if (old_array != memblock_memory_init_regions &&
  262. old_array != memblock_reserved_init_regions)
  263. memblock_free(__pa(old_array), old_alloc_size);
  264. /*
  265. * Reserve the new array if that comes from the memblock. Otherwise, we
  266. * needn't do it
  267. */
  268. if (!use_slab)
  269. BUG_ON(memblock_reserve(addr, new_alloc_size));
  270. /* Update slab flag */
  271. *in_slab = use_slab;
  272. return 0;
  273. }
  274. /**
  275. * memblock_merge_regions - merge neighboring compatible regions
  276. * @type: memblock type to scan
  277. *
  278. * Scan @type and merge neighboring compatible regions.
  279. */
  280. static void __init_memblock memblock_merge_regions(struct memblock_type *type)
  281. {
  282. int i = 0;
  283. /* cnt never goes below 1 */
  284. while (i < type->cnt - 1) {
  285. struct memblock_region *this = &type->regions[i];
  286. struct memblock_region *next = &type->regions[i + 1];
  287. if (this->base + this->size != next->base ||
  288. memblock_get_region_node(this) !=
  289. memblock_get_region_node(next)) {
  290. BUG_ON(this->base + this->size > next->base);
  291. i++;
  292. continue;
  293. }
  294. this->size += next->size;
  295. /* move forward from next + 1, index of which is i + 2 */
  296. memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
  297. type->cnt--;
  298. }
  299. }
  300. /**
  301. * memblock_insert_region - insert new memblock region
  302. * @type: memblock type to insert into
  303. * @idx: index for the insertion point
  304. * @base: base address of the new region
  305. * @size: size of the new region
  306. * @nid: node id of the new region
  307. *
  308. * Insert new memblock region [@base,@base+@size) into @type at @idx.
  309. * @type must already have extra room to accomodate the new region.
  310. */
  311. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  312. int idx, phys_addr_t base,
  313. phys_addr_t size, int nid)
  314. {
  315. struct memblock_region *rgn = &type->regions[idx];
  316. BUG_ON(type->cnt >= type->max);
  317. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  318. rgn->base = base;
  319. rgn->size = size;
  320. memblock_set_region_node(rgn, nid);
  321. type->cnt++;
  322. type->total_size += size;
  323. }
  324. /**
  325. * memblock_add_region - add new memblock region
  326. * @type: memblock type to add new region into
  327. * @base: base address of the new region
  328. * @size: size of the new region
  329. * @nid: nid of the new region
  330. *
  331. * Add new memblock region [@base,@base+@size) into @type. The new region
  332. * is allowed to overlap with existing ones - overlaps don't affect already
  333. * existing regions. @type is guaranteed to be minimal (all neighbouring
  334. * compatible regions are merged) after the addition.
  335. *
  336. * RETURNS:
  337. * 0 on success, -errno on failure.
  338. */
  339. static int __init_memblock memblock_add_region(struct memblock_type *type,
  340. phys_addr_t base, phys_addr_t size, int nid)
  341. {
  342. bool insert = false;
  343. phys_addr_t obase = base;
  344. phys_addr_t end = base + memblock_cap_size(base, &size);
  345. int i, nr_new;
  346. if (!size)
  347. return 0;
  348. /* special case for empty array */
  349. if (type->regions[0].size == 0) {
  350. WARN_ON(type->cnt != 1 || type->total_size);
  351. type->regions[0].base = base;
  352. type->regions[0].size = size;
  353. memblock_set_region_node(&type->regions[0], nid);
  354. type->total_size = size;
  355. return 0;
  356. }
  357. repeat:
  358. /*
  359. * The following is executed twice. Once with %false @insert and
  360. * then with %true. The first counts the number of regions needed
  361. * to accomodate the new area. The second actually inserts them.
  362. */
  363. base = obase;
  364. nr_new = 0;
  365. for (i = 0; i < type->cnt; i++) {
  366. struct memblock_region *rgn = &type->regions[i];
  367. phys_addr_t rbase = rgn->base;
  368. phys_addr_t rend = rbase + rgn->size;
  369. if (rbase >= end)
  370. break;
  371. if (rend <= base)
  372. continue;
  373. /*
  374. * @rgn overlaps. If it separates the lower part of new
  375. * area, insert that portion.
  376. */
  377. if (rbase > base) {
  378. nr_new++;
  379. if (insert)
  380. memblock_insert_region(type, i++, base,
  381. rbase - base, nid);
  382. }
  383. /* area below @rend is dealt with, forget about it */
  384. base = min(rend, end);
  385. }
  386. /* insert the remaining portion */
  387. if (base < end) {
  388. nr_new++;
  389. if (insert)
  390. memblock_insert_region(type, i, base, end - base, nid);
  391. }
  392. /*
  393. * If this was the first round, resize array and repeat for actual
  394. * insertions; otherwise, merge and return.
  395. */
  396. if (!insert) {
  397. while (type->cnt + nr_new > type->max)
  398. if (memblock_double_array(type, obase, size) < 0)
  399. return -ENOMEM;
  400. insert = true;
  401. goto repeat;
  402. } else {
  403. memblock_merge_regions(type);
  404. return 0;
  405. }
  406. }
  407. int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
  408. int nid)
  409. {
  410. return memblock_add_region(&memblock.memory, base, size, nid);
  411. }
  412. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  413. {
  414. return memblock_add_region(&memblock.memory, base, size, MAX_NUMNODES);
  415. }
  416. /**
  417. * memblock_isolate_range - isolate given range into disjoint memblocks
  418. * @type: memblock type to isolate range for
  419. * @base: base of range to isolate
  420. * @size: size of range to isolate
  421. * @start_rgn: out parameter for the start of isolated region
  422. * @end_rgn: out parameter for the end of isolated region
  423. *
  424. * Walk @type and ensure that regions don't cross the boundaries defined by
  425. * [@base,@base+@size). Crossing regions are split at the boundaries,
  426. * which may create at most two more regions. The index of the first
  427. * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  428. *
  429. * RETURNS:
  430. * 0 on success, -errno on failure.
  431. */
  432. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  433. phys_addr_t base, phys_addr_t size,
  434. int *start_rgn, int *end_rgn)
  435. {
  436. phys_addr_t end = base + memblock_cap_size(base, &size);
  437. int i;
  438. *start_rgn = *end_rgn = 0;
  439. if (!size)
  440. return 0;
  441. /* we'll create at most two more regions */
  442. while (type->cnt + 2 > type->max)
  443. if (memblock_double_array(type, base, size) < 0)
  444. return -ENOMEM;
  445. for (i = 0; i < type->cnt; i++) {
  446. struct memblock_region *rgn = &type->regions[i];
  447. phys_addr_t rbase = rgn->base;
  448. phys_addr_t rend = rbase + rgn->size;
  449. if (rbase >= end)
  450. break;
  451. if (rend <= base)
  452. continue;
  453. if (rbase < base) {
  454. /*
  455. * @rgn intersects from below. Split and continue
  456. * to process the next region - the new top half.
  457. */
  458. rgn->base = base;
  459. rgn->size -= base - rbase;
  460. type->total_size -= base - rbase;
  461. memblock_insert_region(type, i, rbase, base - rbase,
  462. memblock_get_region_node(rgn));
  463. } else if (rend > end) {
  464. /*
  465. * @rgn intersects from above. Split and redo the
  466. * current region - the new bottom half.
  467. */
  468. rgn->base = end;
  469. rgn->size -= end - rbase;
  470. type->total_size -= end - rbase;
  471. memblock_insert_region(type, i--, rbase, end - rbase,
  472. memblock_get_region_node(rgn));
  473. } else {
  474. /* @rgn is fully contained, record it */
  475. if (!*end_rgn)
  476. *start_rgn = i;
  477. *end_rgn = i + 1;
  478. }
  479. }
  480. return 0;
  481. }
  482. static int __init_memblock __memblock_remove(struct memblock_type *type,
  483. phys_addr_t base, phys_addr_t size)
  484. {
  485. int start_rgn, end_rgn;
  486. int i, ret;
  487. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  488. if (ret)
  489. return ret;
  490. for (i = end_rgn - 1; i >= start_rgn; i--)
  491. memblock_remove_region(type, i);
  492. return 0;
  493. }
  494. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  495. {
  496. return __memblock_remove(&memblock.memory, base, size);
  497. }
  498. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  499. {
  500. memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  501. (unsigned long long)base,
  502. (unsigned long long)base + size,
  503. (void *)_RET_IP_);
  504. return __memblock_remove(&memblock.reserved, base, size);
  505. }
  506. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  507. {
  508. struct memblock_type *_rgn = &memblock.reserved;
  509. memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
  510. (unsigned long long)base,
  511. (unsigned long long)base + size,
  512. (void *)_RET_IP_);
  513. return memblock_add_region(_rgn, base, size, MAX_NUMNODES);
  514. }
  515. /**
  516. * __next_free_mem_range - next function for for_each_free_mem_range()
  517. * @idx: pointer to u64 loop variable
  518. * @nid: node selector, %MAX_NUMNODES for all nodes
  519. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  520. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  521. * @out_nid: ptr to int for nid of the range, can be %NULL
  522. *
  523. * Find the first free area from *@idx which matches @nid, fill the out
  524. * parameters, and update *@idx for the next iteration. The lower 32bit of
  525. * *@idx contains index into memory region and the upper 32bit indexes the
  526. * areas before each reserved region. For example, if reserved regions
  527. * look like the following,
  528. *
  529. * 0:[0-16), 1:[32-48), 2:[128-130)
  530. *
  531. * The upper 32bit indexes the following regions.
  532. *
  533. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  534. *
  535. * As both region arrays are sorted, the function advances the two indices
  536. * in lockstep and returns each intersection.
  537. */
  538. void __init_memblock __next_free_mem_range(u64 *idx, int nid,
  539. phys_addr_t *out_start,
  540. phys_addr_t *out_end, int *out_nid)
  541. {
  542. struct memblock_type *mem = &memblock.memory;
  543. struct memblock_type *rsv = &memblock.reserved;
  544. int mi = *idx & 0xffffffff;
  545. int ri = *idx >> 32;
  546. for ( ; mi < mem->cnt; mi++) {
  547. struct memblock_region *m = &mem->regions[mi];
  548. phys_addr_t m_start = m->base;
  549. phys_addr_t m_end = m->base + m->size;
  550. /* only memory regions are associated with nodes, check it */
  551. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  552. continue;
  553. /* scan areas before each reservation for intersection */
  554. for ( ; ri < rsv->cnt + 1; ri++) {
  555. struct memblock_region *r = &rsv->regions[ri];
  556. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  557. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  558. /* if ri advanced past mi, break out to advance mi */
  559. if (r_start >= m_end)
  560. break;
  561. /* if the two regions intersect, we're done */
  562. if (m_start < r_end) {
  563. if (out_start)
  564. *out_start = max(m_start, r_start);
  565. if (out_end)
  566. *out_end = min(m_end, r_end);
  567. if (out_nid)
  568. *out_nid = memblock_get_region_node(m);
  569. /*
  570. * The region which ends first is advanced
  571. * for the next iteration.
  572. */
  573. if (m_end <= r_end)
  574. mi++;
  575. else
  576. ri++;
  577. *idx = (u32)mi | (u64)ri << 32;
  578. return;
  579. }
  580. }
  581. }
  582. /* signal end of iteration */
  583. *idx = ULLONG_MAX;
  584. }
  585. /**
  586. * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
  587. * @idx: pointer to u64 loop variable
  588. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  589. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  590. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  591. * @out_nid: ptr to int for nid of the range, can be %NULL
  592. *
  593. * Reverse of __next_free_mem_range().
  594. */
  595. void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
  596. phys_addr_t *out_start,
  597. phys_addr_t *out_end, int *out_nid)
  598. {
  599. struct memblock_type *mem = &memblock.memory;
  600. struct memblock_type *rsv = &memblock.reserved;
  601. int mi = *idx & 0xffffffff;
  602. int ri = *idx >> 32;
  603. if (*idx == (u64)ULLONG_MAX) {
  604. mi = mem->cnt - 1;
  605. ri = rsv->cnt;
  606. }
  607. for ( ; mi >= 0; mi--) {
  608. struct memblock_region *m = &mem->regions[mi];
  609. phys_addr_t m_start = m->base;
  610. phys_addr_t m_end = m->base + m->size;
  611. /* only memory regions are associated with nodes, check it */
  612. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  613. continue;
  614. /* scan areas before each reservation for intersection */
  615. for ( ; ri >= 0; ri--) {
  616. struct memblock_region *r = &rsv->regions[ri];
  617. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  618. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  619. /* if ri advanced past mi, break out to advance mi */
  620. if (r_end <= m_start)
  621. break;
  622. /* if the two regions intersect, we're done */
  623. if (m_end > r_start) {
  624. if (out_start)
  625. *out_start = max(m_start, r_start);
  626. if (out_end)
  627. *out_end = min(m_end, r_end);
  628. if (out_nid)
  629. *out_nid = memblock_get_region_node(m);
  630. if (m_start >= r_start)
  631. mi--;
  632. else
  633. ri--;
  634. *idx = (u32)mi | (u64)ri << 32;
  635. return;
  636. }
  637. }
  638. }
  639. *idx = ULLONG_MAX;
  640. }
  641. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  642. /*
  643. * Common iterator interface used to define for_each_mem_range().
  644. */
  645. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  646. unsigned long *out_start_pfn,
  647. unsigned long *out_end_pfn, int *out_nid)
  648. {
  649. struct memblock_type *type = &memblock.memory;
  650. struct memblock_region *r;
  651. while (++*idx < type->cnt) {
  652. r = &type->regions[*idx];
  653. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  654. continue;
  655. if (nid == MAX_NUMNODES || nid == r->nid)
  656. break;
  657. }
  658. if (*idx >= type->cnt) {
  659. *idx = -1;
  660. return;
  661. }
  662. if (out_start_pfn)
  663. *out_start_pfn = PFN_UP(r->base);
  664. if (out_end_pfn)
  665. *out_end_pfn = PFN_DOWN(r->base + r->size);
  666. if (out_nid)
  667. *out_nid = r->nid;
  668. }
  669. /**
  670. * memblock_set_node - set node ID on memblock regions
  671. * @base: base of area to set node ID for
  672. * @size: size of area to set node ID for
  673. * @nid: node ID to set
  674. *
  675. * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
  676. * Regions which cross the area boundaries are split as necessary.
  677. *
  678. * RETURNS:
  679. * 0 on success, -errno on failure.
  680. */
  681. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  682. int nid)
  683. {
  684. struct memblock_type *type = &memblock.memory;
  685. int start_rgn, end_rgn;
  686. int i, ret;
  687. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  688. if (ret)
  689. return ret;
  690. for (i = start_rgn; i < end_rgn; i++)
  691. memblock_set_region_node(&type->regions[i], nid);
  692. memblock_merge_regions(type);
  693. return 0;
  694. }
  695. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  696. static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
  697. phys_addr_t align, phys_addr_t max_addr,
  698. int nid)
  699. {
  700. phys_addr_t found;
  701. if (WARN_ON(!align))
  702. align = __alignof__(long long);
  703. /* align @size to avoid excessive fragmentation on reserved array */
  704. size = round_up(size, align);
  705. found = memblock_find_in_range_node(0, max_addr, size, align, nid);
  706. if (found && !memblock_reserve(found, size))
  707. return found;
  708. return 0;
  709. }
  710. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  711. {
  712. return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  713. }
  714. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  715. {
  716. return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
  717. }
  718. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  719. {
  720. phys_addr_t alloc;
  721. alloc = __memblock_alloc_base(size, align, max_addr);
  722. if (alloc == 0)
  723. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  724. (unsigned long long) size, (unsigned long long) max_addr);
  725. return alloc;
  726. }
  727. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  728. {
  729. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  730. }
  731. phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  732. {
  733. phys_addr_t res = memblock_alloc_nid(size, align, nid);
  734. if (res)
  735. return res;
  736. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  737. }
  738. /*
  739. * Remaining API functions
  740. */
  741. phys_addr_t __init memblock_phys_mem_size(void)
  742. {
  743. return memblock.memory.total_size;
  744. }
  745. phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
  746. {
  747. unsigned long pages = 0;
  748. struct memblock_region *r;
  749. unsigned long start_pfn, end_pfn;
  750. for_each_memblock(memory, r) {
  751. start_pfn = memblock_region_memory_base_pfn(r);
  752. end_pfn = memblock_region_memory_end_pfn(r);
  753. start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
  754. end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
  755. pages += end_pfn - start_pfn;
  756. }
  757. return (phys_addr_t)pages << PAGE_SHIFT;
  758. }
  759. /* lowest address */
  760. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  761. {
  762. return memblock.memory.regions[0].base;
  763. }
  764. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  765. {
  766. int idx = memblock.memory.cnt - 1;
  767. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  768. }
  769. void __init memblock_enforce_memory_limit(phys_addr_t limit)
  770. {
  771. unsigned long i;
  772. phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
  773. if (!limit)
  774. return;
  775. /* find out max address */
  776. for (i = 0; i < memblock.memory.cnt; i++) {
  777. struct memblock_region *r = &memblock.memory.regions[i];
  778. if (limit <= r->size) {
  779. max_addr = r->base + limit;
  780. break;
  781. }
  782. limit -= r->size;
  783. }
  784. /* truncate both memory and reserved regions */
  785. __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
  786. __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
  787. }
  788. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  789. {
  790. unsigned int left = 0, right = type->cnt;
  791. do {
  792. unsigned int mid = (right + left) / 2;
  793. if (addr < type->regions[mid].base)
  794. right = mid;
  795. else if (addr >= (type->regions[mid].base +
  796. type->regions[mid].size))
  797. left = mid + 1;
  798. else
  799. return mid;
  800. } while (left < right);
  801. return -1;
  802. }
  803. int __init memblock_is_reserved(phys_addr_t addr)
  804. {
  805. return memblock_search(&memblock.reserved, addr) != -1;
  806. }
  807. int __init_memblock memblock_is_memory(phys_addr_t addr)
  808. {
  809. return memblock_search(&memblock.memory, addr) != -1;
  810. }
  811. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  812. int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
  813. unsigned long *start_pfn, unsigned long *end_pfn)
  814. {
  815. struct memblock_type *type = &memblock.memory;
  816. int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT);
  817. if (mid == -1)
  818. return -1;
  819. *start_pfn = type->regions[mid].base >> PAGE_SHIFT;
  820. *end_pfn = (type->regions[mid].base + type->regions[mid].size)
  821. >> PAGE_SHIFT;
  822. return type->regions[mid].nid;
  823. }
  824. #endif
  825. /**
  826. * memblock_is_region_memory - check if a region is a subset of memory
  827. * @base: base of region to check
  828. * @size: size of region to check
  829. *
  830. * Check if the region [@base, @base+@size) is a subset of a memory block.
  831. *
  832. * RETURNS:
  833. * 0 if false, non-zero if true
  834. */
  835. int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  836. {
  837. int idx = memblock_search(&memblock.memory, base);
  838. phys_addr_t end = base + memblock_cap_size(base, &size);
  839. if (idx == -1)
  840. return 0;
  841. return memblock.memory.regions[idx].base <= base &&
  842. (memblock.memory.regions[idx].base +
  843. memblock.memory.regions[idx].size) >= end;
  844. }
  845. /**
  846. * memblock_is_region_reserved - check if a region intersects reserved memory
  847. * @base: base of region to check
  848. * @size: size of region to check
  849. *
  850. * Check if the region [@base, @base+@size) intersects a reserved memory block.
  851. *
  852. * RETURNS:
  853. * 0 if false, non-zero if true
  854. */
  855. int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  856. {
  857. memblock_cap_size(base, &size);
  858. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  859. }
  860. void __init_memblock memblock_trim_memory(phys_addr_t align)
  861. {
  862. int i;
  863. phys_addr_t start, end, orig_start, orig_end;
  864. struct memblock_type *mem = &memblock.memory;
  865. for (i = 0; i < mem->cnt; i++) {
  866. orig_start = mem->regions[i].base;
  867. orig_end = mem->regions[i].base + mem->regions[i].size;
  868. start = round_up(orig_start, align);
  869. end = round_down(orig_end, align);
  870. if (start == orig_start && end == orig_end)
  871. continue;
  872. if (start < end) {
  873. mem->regions[i].base = start;
  874. mem->regions[i].size = end - start;
  875. } else {
  876. memblock_remove_region(mem, i);
  877. i--;
  878. }
  879. }
  880. }
  881. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  882. {
  883. memblock.current_limit = limit;
  884. }
  885. static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
  886. {
  887. unsigned long long base, size;
  888. int i;
  889. pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
  890. for (i = 0; i < type->cnt; i++) {
  891. struct memblock_region *rgn = &type->regions[i];
  892. char nid_buf[32] = "";
  893. base = rgn->base;
  894. size = rgn->size;
  895. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  896. if (memblock_get_region_node(rgn) != MAX_NUMNODES)
  897. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  898. memblock_get_region_node(rgn));
  899. #endif
  900. pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
  901. name, i, base, base + size - 1, size, nid_buf);
  902. }
  903. }
  904. void __init_memblock __memblock_dump_all(void)
  905. {
  906. pr_info("MEMBLOCK configuration:\n");
  907. pr_info(" memory size = %#llx reserved size = %#llx\n",
  908. (unsigned long long)memblock.memory.total_size,
  909. (unsigned long long)memblock.reserved.total_size);
  910. memblock_dump(&memblock.memory, "memory");
  911. memblock_dump(&memblock.reserved, "reserved");
  912. }
  913. void __init memblock_allow_resize(void)
  914. {
  915. memblock_can_resize = 1;
  916. }
  917. static int __init early_memblock(char *p)
  918. {
  919. if (p && strstr(p, "debug"))
  920. memblock_debug = 1;
  921. return 0;
  922. }
  923. early_param("memblock", early_memblock);
  924. #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
  925. static int memblock_debug_show(struct seq_file *m, void *private)
  926. {
  927. struct memblock_type *type = m->private;
  928. struct memblock_region *reg;
  929. int i;
  930. for (i = 0; i < type->cnt; i++) {
  931. reg = &type->regions[i];
  932. seq_printf(m, "%4d: ", i);
  933. if (sizeof(phys_addr_t) == 4)
  934. seq_printf(m, "0x%08lx..0x%08lx\n",
  935. (unsigned long)reg->base,
  936. (unsigned long)(reg->base + reg->size - 1));
  937. else
  938. seq_printf(m, "0x%016llx..0x%016llx\n",
  939. (unsigned long long)reg->base,
  940. (unsigned long long)(reg->base + reg->size - 1));
  941. }
  942. return 0;
  943. }
  944. static int memblock_debug_open(struct inode *inode, struct file *file)
  945. {
  946. return single_open(file, memblock_debug_show, inode->i_private);
  947. }
  948. static const struct file_operations memblock_debug_fops = {
  949. .open = memblock_debug_open,
  950. .read = seq_read,
  951. .llseek = seq_lseek,
  952. .release = single_release,
  953. };
  954. static int __init memblock_init_debugfs(void)
  955. {
  956. struct dentry *root = debugfs_create_dir("memblock", NULL);
  957. if (!root)
  958. return -ENXIO;
  959. debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
  960. debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
  961. return 0;
  962. }
  963. __initcall(memblock_init_debugfs);
  964. #endif /* CONFIG_DEBUG_FS */