memblock.c 27 KB

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