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. /* pump up @end */
  89. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  90. end = memblock.current_limit;
  91. /* avoid allocating the first page */
  92. start = max_t(phys_addr_t, start, PAGE_SIZE);
  93. end = max(start, end);
  94. for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
  95. this_start = clamp(this_start, start, end);
  96. this_end = clamp(this_end, start, end);
  97. if (this_end < size)
  98. continue;
  99. cand = round_down(this_end - size, align);
  100. if (cand >= this_start)
  101. return cand;
  102. }
  103. return 0;
  104. }
  105. /**
  106. * memblock_find_in_range - find free area in given range
  107. * @start: start of candidate range
  108. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  109. * @size: size of free area to find
  110. * @align: alignment of free area to find
  111. *
  112. * Find @size free area aligned to @align in the specified range.
  113. *
  114. * RETURNS:
  115. * Found address on success, %0 on failure.
  116. */
  117. phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
  118. phys_addr_t end, phys_addr_t size,
  119. phys_addr_t align)
  120. {
  121. return memblock_find_in_range_node(start, end, size, align,
  122. MAX_NUMNODES);
  123. }
  124. /*
  125. * Free memblock.reserved.regions
  126. */
  127. int __init_memblock memblock_free_reserved_regions(void)
  128. {
  129. if (memblock.reserved.regions == memblock_reserved_init_regions)
  130. return 0;
  131. return memblock_free(__pa(memblock.reserved.regions),
  132. sizeof(struct memblock_region) * memblock.reserved.max);
  133. }
  134. /*
  135. * Reserve memblock.reserved.regions
  136. */
  137. int __init_memblock memblock_reserve_reserved_regions(void)
  138. {
  139. if (memblock.reserved.regions == memblock_reserved_init_regions)
  140. return 0;
  141. return memblock_reserve(__pa(memblock.reserved.regions),
  142. sizeof(struct memblock_region) * memblock.reserved.max);
  143. }
  144. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  145. {
  146. type->total_size -= type->regions[r].size;
  147. memmove(&type->regions[r], &type->regions[r + 1],
  148. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  149. type->cnt--;
  150. /* Special case for empty arrays */
  151. if (type->cnt == 0) {
  152. WARN_ON(type->total_size != 0);
  153. type->cnt = 1;
  154. type->regions[0].base = 0;
  155. type->regions[0].size = 0;
  156. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  157. }
  158. }
  159. static int __init_memblock memblock_double_array(struct memblock_type *type)
  160. {
  161. struct memblock_region *new_array, *old_array;
  162. phys_addr_t old_size, new_size, addr;
  163. int use_slab = slab_is_available();
  164. /* We don't allow resizing until we know about the reserved regions
  165. * of memory that aren't suitable for allocation
  166. */
  167. if (!memblock_can_resize)
  168. return -1;
  169. /* Calculate new doubled size */
  170. old_size = type->max * sizeof(struct memblock_region);
  171. new_size = old_size << 1;
  172. /* Try to find some space for it.
  173. *
  174. * WARNING: We assume that either slab_is_available() and we use it or
  175. * we use MEMBLOCK for allocations. That means that this is unsafe to use
  176. * when bootmem is currently active (unless bootmem itself is implemented
  177. * on top of MEMBLOCK which isn't the case yet)
  178. *
  179. * This should however not be an issue for now, as we currently only
  180. * call into MEMBLOCK while it's still active, or much later when slab is
  181. * active for memory hotplug operations
  182. */
  183. if (use_slab) {
  184. new_array = kmalloc(new_size, GFP_KERNEL);
  185. addr = new_array ? __pa(new_array) : 0;
  186. } else
  187. addr = memblock_find_in_range(0, MEMBLOCK_ALLOC_ACCESSIBLE, new_size, sizeof(phys_addr_t));
  188. if (!addr) {
  189. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  190. memblock_type_name(type), type->max, type->max * 2);
  191. return -1;
  192. }
  193. new_array = __va(addr);
  194. memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]",
  195. memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1);
  196. /* Found space, we now need to move the array over before
  197. * we add the reserved region since it may be our reserved
  198. * array itself that is full.
  199. */
  200. memcpy(new_array, type->regions, old_size);
  201. memset(new_array + type->max, 0, old_size);
  202. old_array = type->regions;
  203. type->regions = new_array;
  204. type->max <<= 1;
  205. /* If we use SLAB that's it, we are done */
  206. if (use_slab)
  207. return 0;
  208. /* Add the new reserved region now. Should not fail ! */
  209. BUG_ON(memblock_reserve(addr, new_size));
  210. /* If the array wasn't our static init one, then free it. We only do
  211. * that before SLAB is available as later on, we don't know whether
  212. * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
  213. * anyways
  214. */
  215. if (old_array != memblock_memory_init_regions &&
  216. old_array != memblock_reserved_init_regions)
  217. memblock_free(__pa(old_array), old_size);
  218. return 0;
  219. }
  220. /**
  221. * memblock_merge_regions - merge neighboring compatible regions
  222. * @type: memblock type to scan
  223. *
  224. * Scan @type and merge neighboring compatible regions.
  225. */
  226. static void __init_memblock memblock_merge_regions(struct memblock_type *type)
  227. {
  228. int i = 0;
  229. /* cnt never goes below 1 */
  230. while (i < type->cnt - 1) {
  231. struct memblock_region *this = &type->regions[i];
  232. struct memblock_region *next = &type->regions[i + 1];
  233. if (this->base + this->size != next->base ||
  234. memblock_get_region_node(this) !=
  235. memblock_get_region_node(next)) {
  236. BUG_ON(this->base + this->size > next->base);
  237. i++;
  238. continue;
  239. }
  240. this->size += next->size;
  241. memmove(next, next + 1, (type->cnt - (i + 1)) * sizeof(*next));
  242. type->cnt--;
  243. }
  244. }
  245. /**
  246. * memblock_insert_region - insert new memblock region
  247. * @type: memblock type to insert into
  248. * @idx: index for the insertion point
  249. * @base: base address of the new region
  250. * @size: size of the new region
  251. *
  252. * Insert new memblock region [@base,@base+@size) into @type at @idx.
  253. * @type must already have extra room to accomodate the new region.
  254. */
  255. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  256. int idx, phys_addr_t base,
  257. phys_addr_t size, int nid)
  258. {
  259. struct memblock_region *rgn = &type->regions[idx];
  260. BUG_ON(type->cnt >= type->max);
  261. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  262. rgn->base = base;
  263. rgn->size = size;
  264. memblock_set_region_node(rgn, nid);
  265. type->cnt++;
  266. type->total_size += size;
  267. }
  268. /**
  269. * memblock_add_region - add new memblock region
  270. * @type: memblock type to add new region into
  271. * @base: base address of the new region
  272. * @size: size of the new region
  273. * @nid: nid of the new region
  274. *
  275. * Add new memblock region [@base,@base+@size) into @type. The new region
  276. * is allowed to overlap with existing ones - overlaps don't affect already
  277. * existing regions. @type is guaranteed to be minimal (all neighbouring
  278. * compatible regions are merged) after the addition.
  279. *
  280. * RETURNS:
  281. * 0 on success, -errno on failure.
  282. */
  283. static int __init_memblock memblock_add_region(struct memblock_type *type,
  284. phys_addr_t base, phys_addr_t size, int nid)
  285. {
  286. bool insert = false;
  287. phys_addr_t obase = base;
  288. phys_addr_t end = base + memblock_cap_size(base, &size);
  289. int i, nr_new;
  290. /* special case for empty array */
  291. if (type->regions[0].size == 0) {
  292. WARN_ON(type->cnt != 1 || type->total_size);
  293. type->regions[0].base = base;
  294. type->regions[0].size = size;
  295. memblock_set_region_node(&type->regions[0], nid);
  296. type->total_size = size;
  297. return 0;
  298. }
  299. repeat:
  300. /*
  301. * The following is executed twice. Once with %false @insert and
  302. * then with %true. The first counts the number of regions needed
  303. * to accomodate the new area. The second actually inserts them.
  304. */
  305. base = obase;
  306. nr_new = 0;
  307. for (i = 0; i < type->cnt; i++) {
  308. struct memblock_region *rgn = &type->regions[i];
  309. phys_addr_t rbase = rgn->base;
  310. phys_addr_t rend = rbase + rgn->size;
  311. if (rbase >= end)
  312. break;
  313. if (rend <= base)
  314. continue;
  315. /*
  316. * @rgn overlaps. If it separates the lower part of new
  317. * area, insert that portion.
  318. */
  319. if (rbase > base) {
  320. nr_new++;
  321. if (insert)
  322. memblock_insert_region(type, i++, base,
  323. rbase - base, nid);
  324. }
  325. /* area below @rend is dealt with, forget about it */
  326. base = min(rend, end);
  327. }
  328. /* insert the remaining portion */
  329. if (base < end) {
  330. nr_new++;
  331. if (insert)
  332. memblock_insert_region(type, i, base, end - base, nid);
  333. }
  334. /*
  335. * If this was the first round, resize array and repeat for actual
  336. * insertions; otherwise, merge and return.
  337. */
  338. if (!insert) {
  339. while (type->cnt + nr_new > type->max)
  340. if (memblock_double_array(type) < 0)
  341. return -ENOMEM;
  342. insert = true;
  343. goto repeat;
  344. } else {
  345. memblock_merge_regions(type);
  346. return 0;
  347. }
  348. }
  349. int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
  350. int nid)
  351. {
  352. return memblock_add_region(&memblock.memory, base, size, nid);
  353. }
  354. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  355. {
  356. return memblock_add_region(&memblock.memory, base, size, MAX_NUMNODES);
  357. }
  358. /**
  359. * memblock_isolate_range - isolate given range into disjoint memblocks
  360. * @type: memblock type to isolate range for
  361. * @base: base of range to isolate
  362. * @size: size of range to isolate
  363. * @start_rgn: out parameter for the start of isolated region
  364. * @end_rgn: out parameter for the end of isolated region
  365. *
  366. * Walk @type and ensure that regions don't cross the boundaries defined by
  367. * [@base,@base+@size). Crossing regions are split at the boundaries,
  368. * which may create at most two more regions. The index of the first
  369. * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  370. *
  371. * RETURNS:
  372. * 0 on success, -errno on failure.
  373. */
  374. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  375. phys_addr_t base, phys_addr_t size,
  376. int *start_rgn, int *end_rgn)
  377. {
  378. phys_addr_t end = base + memblock_cap_size(base, &size);
  379. int i;
  380. *start_rgn = *end_rgn = 0;
  381. /* we'll create at most two more regions */
  382. while (type->cnt + 2 > type->max)
  383. if (memblock_double_array(type) < 0)
  384. return -ENOMEM;
  385. for (i = 0; i < type->cnt; i++) {
  386. struct memblock_region *rgn = &type->regions[i];
  387. phys_addr_t rbase = rgn->base;
  388. phys_addr_t rend = rbase + rgn->size;
  389. if (rbase >= end)
  390. break;
  391. if (rend <= base)
  392. continue;
  393. if (rbase < base) {
  394. /*
  395. * @rgn intersects from below. Split and continue
  396. * to process the next region - the new top half.
  397. */
  398. rgn->base = base;
  399. rgn->size -= base - rbase;
  400. type->total_size -= base - rbase;
  401. memblock_insert_region(type, i, rbase, base - rbase,
  402. memblock_get_region_node(rgn));
  403. } else if (rend > end) {
  404. /*
  405. * @rgn intersects from above. Split and redo the
  406. * current region - the new bottom half.
  407. */
  408. rgn->base = end;
  409. rgn->size -= end - rbase;
  410. type->total_size -= end - rbase;
  411. memblock_insert_region(type, i--, rbase, end - rbase,
  412. memblock_get_region_node(rgn));
  413. } else {
  414. /* @rgn is fully contained, record it */
  415. if (!*end_rgn)
  416. *start_rgn = i;
  417. *end_rgn = i + 1;
  418. }
  419. }
  420. return 0;
  421. }
  422. static int __init_memblock __memblock_remove(struct memblock_type *type,
  423. phys_addr_t base, phys_addr_t size)
  424. {
  425. int start_rgn, end_rgn;
  426. int i, ret;
  427. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  428. if (ret)
  429. return ret;
  430. for (i = end_rgn - 1; i >= start_rgn; i--)
  431. memblock_remove_region(type, i);
  432. return 0;
  433. }
  434. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  435. {
  436. return __memblock_remove(&memblock.memory, base, size);
  437. }
  438. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  439. {
  440. memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  441. (unsigned long long)base,
  442. (unsigned long long)base + size,
  443. (void *)_RET_IP_);
  444. return __memblock_remove(&memblock.reserved, base, size);
  445. }
  446. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  447. {
  448. struct memblock_type *_rgn = &memblock.reserved;
  449. memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
  450. (unsigned long long)base,
  451. (unsigned long long)base + size,
  452. (void *)_RET_IP_);
  453. BUG_ON(0 == size);
  454. return memblock_add_region(_rgn, base, size, MAX_NUMNODES);
  455. }
  456. /**
  457. * __next_free_mem_range - next function for for_each_free_mem_range()
  458. * @idx: pointer to u64 loop variable
  459. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  460. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  461. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  462. * @p_nid: ptr to int for nid of the range, can be %NULL
  463. *
  464. * Find the first free area from *@idx which matches @nid, fill the out
  465. * parameters, and update *@idx for the next iteration. The lower 32bit of
  466. * *@idx contains index into memory region and the upper 32bit indexes the
  467. * areas before each reserved region. For example, if reserved regions
  468. * look like the following,
  469. *
  470. * 0:[0-16), 1:[32-48), 2:[128-130)
  471. *
  472. * The upper 32bit indexes the following regions.
  473. *
  474. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  475. *
  476. * As both region arrays are sorted, the function advances the two indices
  477. * in lockstep and returns each intersection.
  478. */
  479. void __init_memblock __next_free_mem_range(u64 *idx, int nid,
  480. phys_addr_t *out_start,
  481. phys_addr_t *out_end, int *out_nid)
  482. {
  483. struct memblock_type *mem = &memblock.memory;
  484. struct memblock_type *rsv = &memblock.reserved;
  485. int mi = *idx & 0xffffffff;
  486. int ri = *idx >> 32;
  487. for ( ; mi < mem->cnt; mi++) {
  488. struct memblock_region *m = &mem->regions[mi];
  489. phys_addr_t m_start = m->base;
  490. phys_addr_t m_end = m->base + m->size;
  491. /* only memory regions are associated with nodes, check it */
  492. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  493. continue;
  494. /* scan areas before each reservation for intersection */
  495. for ( ; ri < rsv->cnt + 1; ri++) {
  496. struct memblock_region *r = &rsv->regions[ri];
  497. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  498. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  499. /* if ri advanced past mi, break out to advance mi */
  500. if (r_start >= m_end)
  501. break;
  502. /* if the two regions intersect, we're done */
  503. if (m_start < r_end) {
  504. if (out_start)
  505. *out_start = max(m_start, r_start);
  506. if (out_end)
  507. *out_end = min(m_end, r_end);
  508. if (out_nid)
  509. *out_nid = memblock_get_region_node(m);
  510. /*
  511. * The region which ends first is advanced
  512. * for the next iteration.
  513. */
  514. if (m_end <= r_end)
  515. mi++;
  516. else
  517. ri++;
  518. *idx = (u32)mi | (u64)ri << 32;
  519. return;
  520. }
  521. }
  522. }
  523. /* signal end of iteration */
  524. *idx = ULLONG_MAX;
  525. }
  526. /**
  527. * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
  528. * @idx: pointer to u64 loop variable
  529. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  530. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  531. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  532. * @p_nid: ptr to int for nid of the range, can be %NULL
  533. *
  534. * Reverse of __next_free_mem_range().
  535. */
  536. void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
  537. phys_addr_t *out_start,
  538. phys_addr_t *out_end, int *out_nid)
  539. {
  540. struct memblock_type *mem = &memblock.memory;
  541. struct memblock_type *rsv = &memblock.reserved;
  542. int mi = *idx & 0xffffffff;
  543. int ri = *idx >> 32;
  544. if (*idx == (u64)ULLONG_MAX) {
  545. mi = mem->cnt - 1;
  546. ri = rsv->cnt;
  547. }
  548. for ( ; mi >= 0; mi--) {
  549. struct memblock_region *m = &mem->regions[mi];
  550. phys_addr_t m_start = m->base;
  551. phys_addr_t m_end = m->base + m->size;
  552. /* only memory regions are associated with nodes, check it */
  553. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  554. continue;
  555. /* scan areas before each reservation for intersection */
  556. for ( ; ri >= 0; ri--) {
  557. struct memblock_region *r = &rsv->regions[ri];
  558. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  559. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  560. /* if ri advanced past mi, break out to advance mi */
  561. if (r_end <= m_start)
  562. break;
  563. /* if the two regions intersect, we're done */
  564. if (m_end > r_start) {
  565. if (out_start)
  566. *out_start = max(m_start, r_start);
  567. if (out_end)
  568. *out_end = min(m_end, r_end);
  569. if (out_nid)
  570. *out_nid = memblock_get_region_node(m);
  571. if (m_start >= r_start)
  572. mi--;
  573. else
  574. ri--;
  575. *idx = (u32)mi | (u64)ri << 32;
  576. return;
  577. }
  578. }
  579. }
  580. *idx = ULLONG_MAX;
  581. }
  582. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  583. /*
  584. * Common iterator interface used to define for_each_mem_range().
  585. */
  586. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  587. unsigned long *out_start_pfn,
  588. unsigned long *out_end_pfn, int *out_nid)
  589. {
  590. struct memblock_type *type = &memblock.memory;
  591. struct memblock_region *r;
  592. while (++*idx < type->cnt) {
  593. r = &type->regions[*idx];
  594. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  595. continue;
  596. if (nid == MAX_NUMNODES || nid == r->nid)
  597. break;
  598. }
  599. if (*idx >= type->cnt) {
  600. *idx = -1;
  601. return;
  602. }
  603. if (out_start_pfn)
  604. *out_start_pfn = PFN_UP(r->base);
  605. if (out_end_pfn)
  606. *out_end_pfn = PFN_DOWN(r->base + r->size);
  607. if (out_nid)
  608. *out_nid = r->nid;
  609. }
  610. /**
  611. * memblock_set_node - set node ID on memblock regions
  612. * @base: base of area to set node ID for
  613. * @size: size of area to set node ID for
  614. * @nid: node ID to set
  615. *
  616. * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
  617. * Regions which cross the area boundaries are split as necessary.
  618. *
  619. * RETURNS:
  620. * 0 on success, -errno on failure.
  621. */
  622. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  623. int nid)
  624. {
  625. struct memblock_type *type = &memblock.memory;
  626. int start_rgn, end_rgn;
  627. int i, ret;
  628. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  629. if (ret)
  630. return ret;
  631. for (i = start_rgn; i < end_rgn; i++)
  632. type->regions[i].nid = nid;
  633. memblock_merge_regions(type);
  634. return 0;
  635. }
  636. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  637. static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
  638. phys_addr_t align, phys_addr_t max_addr,
  639. int nid)
  640. {
  641. phys_addr_t found;
  642. /* align @size to avoid excessive fragmentation on reserved array */
  643. size = round_up(size, align);
  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 */