memblock.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. 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. /*
  45. * Address comparison utilities
  46. */
  47. static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
  48. phys_addr_t base2, phys_addr_t size2)
  49. {
  50. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  51. }
  52. static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
  53. phys_addr_t base, phys_addr_t size)
  54. {
  55. unsigned long i;
  56. for (i = 0; i < type->cnt; i++) {
  57. phys_addr_t rgnbase = type->regions[i].base;
  58. phys_addr_t rgnsize = type->regions[i].size;
  59. if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
  60. break;
  61. }
  62. return (i < type->cnt) ? i : -1;
  63. }
  64. /*
  65. * Find, allocate, deallocate or reserve unreserved regions. All allocations
  66. * are top-down.
  67. */
  68. static phys_addr_t __init_memblock memblock_find_region(phys_addr_t start, phys_addr_t end,
  69. phys_addr_t size, phys_addr_t align)
  70. {
  71. phys_addr_t base, res_base;
  72. long j;
  73. /* In case, huge size is requested */
  74. if (end < size)
  75. return 0;
  76. base = round_down(end - size, align);
  77. /* Prevent allocations returning 0 as it's also used to
  78. * indicate an allocation failure
  79. */
  80. if (start == 0)
  81. start = PAGE_SIZE;
  82. while (start <= base) {
  83. j = memblock_overlaps_region(&memblock.reserved, base, size);
  84. if (j < 0)
  85. return base;
  86. res_base = memblock.reserved.regions[j].base;
  87. if (res_base < size)
  88. break;
  89. base = round_down(res_base - size, align);
  90. }
  91. return 0;
  92. }
  93. /*
  94. * Find a free area with specified alignment in a specific range.
  95. */
  96. phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, phys_addr_t end,
  97. phys_addr_t size, phys_addr_t align)
  98. {
  99. long i;
  100. BUG_ON(0 == size);
  101. /* Pump up max_addr */
  102. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  103. end = memblock.current_limit;
  104. /* We do a top-down search, this tends to limit memory
  105. * fragmentation by keeping early boot allocs near the
  106. * top of memory
  107. */
  108. for (i = memblock.memory.cnt - 1; i >= 0; i--) {
  109. phys_addr_t memblockbase = memblock.memory.regions[i].base;
  110. phys_addr_t memblocksize = memblock.memory.regions[i].size;
  111. phys_addr_t bottom, top, found;
  112. if (memblocksize < size)
  113. continue;
  114. if ((memblockbase + memblocksize) <= start)
  115. break;
  116. bottom = max(memblockbase, start);
  117. top = min(memblockbase + memblocksize, end);
  118. if (bottom >= top)
  119. continue;
  120. found = memblock_find_region(bottom, top, size, align);
  121. if (found)
  122. return found;
  123. }
  124. return 0;
  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. memmove(&type->regions[r], &type->regions[r + 1],
  149. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  150. type->cnt--;
  151. /* Special case for empty arrays */
  152. if (type->cnt == 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. }
  267. /**
  268. * memblock_add_region - add new memblock region
  269. * @type: memblock type to add new region into
  270. * @base: base address of the new region
  271. * @size: size of the new region
  272. *
  273. * Add new memblock region [@base,@base+@size) into @type. The new region
  274. * is allowed to overlap with existing ones - overlaps don't affect already
  275. * existing regions. @type is guaranteed to be minimal (all neighbouring
  276. * compatible regions are merged) after the addition.
  277. *
  278. * RETURNS:
  279. * 0 on success, -errno on failure.
  280. */
  281. static int __init_memblock memblock_add_region(struct memblock_type *type,
  282. phys_addr_t base, phys_addr_t size)
  283. {
  284. bool insert = false;
  285. phys_addr_t obase = base, end = base + size;
  286. int i, nr_new;
  287. /* special case for empty array */
  288. if (type->regions[0].size == 0) {
  289. WARN_ON(type->cnt != 1);
  290. type->regions[0].base = base;
  291. type->regions[0].size = size;
  292. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  293. return 0;
  294. }
  295. repeat:
  296. /*
  297. * The following is executed twice. Once with %false @insert and
  298. * then with %true. The first counts the number of regions needed
  299. * to accomodate the new area. The second actually inserts them.
  300. */
  301. base = obase;
  302. nr_new = 0;
  303. for (i = 0; i < type->cnt; i++) {
  304. struct memblock_region *rgn = &type->regions[i];
  305. phys_addr_t rbase = rgn->base;
  306. phys_addr_t rend = rbase + rgn->size;
  307. if (rbase >= end)
  308. break;
  309. if (rend <= base)
  310. continue;
  311. /*
  312. * @rgn overlaps. If it separates the lower part of new
  313. * area, insert that portion.
  314. */
  315. if (rbase > base) {
  316. nr_new++;
  317. if (insert)
  318. memblock_insert_region(type, i++, base,
  319. rbase - base, MAX_NUMNODES);
  320. }
  321. /* area below @rend is dealt with, forget about it */
  322. base = min(rend, end);
  323. }
  324. /* insert the remaining portion */
  325. if (base < end) {
  326. nr_new++;
  327. if (insert)
  328. memblock_insert_region(type, i, base, end - base,
  329. MAX_NUMNODES);
  330. }
  331. /*
  332. * If this was the first round, resize array and repeat for actual
  333. * insertions; otherwise, merge and return.
  334. */
  335. if (!insert) {
  336. while (type->cnt + nr_new > type->max)
  337. if (memblock_double_array(type) < 0)
  338. return -ENOMEM;
  339. insert = true;
  340. goto repeat;
  341. } else {
  342. memblock_merge_regions(type);
  343. return 0;
  344. }
  345. }
  346. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  347. {
  348. return memblock_add_region(&memblock.memory, base, size);
  349. }
  350. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  351. /**
  352. * memblock_isolate_range - isolate given range into disjoint memblocks
  353. * @type: memblock type to isolate range for
  354. * @base: base of range to isolate
  355. * @size: size of range to isolate
  356. * @start_rgn: out parameter for the start of isolated region
  357. * @end_rgn: out parameter for the end of isolated region
  358. *
  359. * Walk @type and ensure that regions don't cross the boundaries defined by
  360. * [@base,@base+@size). Crossing regions are split at the boundaries,
  361. * which may create at most two more regions. The index of the first
  362. * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  363. *
  364. * RETURNS:
  365. * 0 on success, -errno on failure.
  366. */
  367. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  368. phys_addr_t base, phys_addr_t size,
  369. int *start_rgn, int *end_rgn)
  370. {
  371. phys_addr_t end = base + size;
  372. int i;
  373. *start_rgn = *end_rgn = 0;
  374. /* we'll create at most two more regions */
  375. while (type->cnt + 2 > type->max)
  376. if (memblock_double_array(type) < 0)
  377. return -ENOMEM;
  378. for (i = 0; i < type->cnt; i++) {
  379. struct memblock_region *rgn = &type->regions[i];
  380. phys_addr_t rbase = rgn->base;
  381. phys_addr_t rend = rbase + rgn->size;
  382. if (rbase >= end)
  383. break;
  384. if (rend <= base)
  385. continue;
  386. if (rbase < base) {
  387. /*
  388. * @rgn intersects from below. Split and continue
  389. * to process the next region - the new top half.
  390. */
  391. rgn->base = base;
  392. rgn->size = rend - rgn->base;
  393. memblock_insert_region(type, i, rbase, base - rbase,
  394. rgn->nid);
  395. } else if (rend > end) {
  396. /*
  397. * @rgn intersects from above. Split and redo the
  398. * current region - the new bottom half.
  399. */
  400. rgn->base = end;
  401. rgn->size = rend - rgn->base;
  402. memblock_insert_region(type, i--, rbase, end - rbase,
  403. rgn->nid);
  404. } else {
  405. /* @rgn is fully contained, record it */
  406. if (!*end_rgn)
  407. *start_rgn = i;
  408. *end_rgn = i + 1;
  409. }
  410. }
  411. return 0;
  412. }
  413. #endif
  414. static int __init_memblock __memblock_remove(struct memblock_type *type,
  415. phys_addr_t base, phys_addr_t size)
  416. {
  417. phys_addr_t end = base + size;
  418. int i;
  419. /* Walk through the array for collisions */
  420. for (i = 0; i < type->cnt; i++) {
  421. struct memblock_region *rgn = &type->regions[i];
  422. phys_addr_t rend = rgn->base + rgn->size;
  423. /* Nothing more to do, exit */
  424. if (rgn->base > end || rgn->size == 0)
  425. break;
  426. /* If we fully enclose the block, drop it */
  427. if (base <= rgn->base && end >= rend) {
  428. memblock_remove_region(type, i--);
  429. continue;
  430. }
  431. /* If we are fully enclosed within a block
  432. * then we need to split it and we are done
  433. */
  434. if (base > rgn->base && end < rend) {
  435. rgn->size = base - rgn->base;
  436. if (!memblock_add_region(type, end, rend - end))
  437. return 0;
  438. /* Failure to split is bad, we at least
  439. * restore the block before erroring
  440. */
  441. rgn->size = rend - rgn->base;
  442. WARN_ON(1);
  443. return -1;
  444. }
  445. /* Check if we need to trim the bottom of a block */
  446. if (rgn->base < end && rend > end) {
  447. rgn->size -= end - rgn->base;
  448. rgn->base = end;
  449. break;
  450. }
  451. /* And check if we need to trim the top of a block */
  452. if (base < rend)
  453. rgn->size -= rend - base;
  454. }
  455. return 0;
  456. }
  457. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  458. {
  459. return __memblock_remove(&memblock.memory, base, size);
  460. }
  461. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  462. {
  463. memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  464. (unsigned long long)base,
  465. (unsigned long long)base + size,
  466. (void *)_RET_IP_);
  467. return __memblock_remove(&memblock.reserved, base, size);
  468. }
  469. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  470. {
  471. struct memblock_type *_rgn = &memblock.reserved;
  472. memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
  473. (unsigned long long)base,
  474. (unsigned long long)base + size,
  475. (void *)_RET_IP_);
  476. BUG_ON(0 == size);
  477. return memblock_add_region(_rgn, base, size);
  478. }
  479. /**
  480. * __next_free_mem_range - next function for for_each_free_mem_range()
  481. * @idx: pointer to u64 loop variable
  482. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  483. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  484. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  485. * @p_nid: ptr to int for nid of the range, can be %NULL
  486. *
  487. * Find the first free area from *@idx which matches @nid, fill the out
  488. * parameters, and update *@idx for the next iteration. The lower 32bit of
  489. * *@idx contains index into memory region and the upper 32bit indexes the
  490. * areas before each reserved region. For example, if reserved regions
  491. * look like the following,
  492. *
  493. * 0:[0-16), 1:[32-48), 2:[128-130)
  494. *
  495. * The upper 32bit indexes the following regions.
  496. *
  497. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  498. *
  499. * As both region arrays are sorted, the function advances the two indices
  500. * in lockstep and returns each intersection.
  501. */
  502. void __init_memblock __next_free_mem_range(u64 *idx, int nid,
  503. phys_addr_t *out_start,
  504. phys_addr_t *out_end, int *out_nid)
  505. {
  506. struct memblock_type *mem = &memblock.memory;
  507. struct memblock_type *rsv = &memblock.reserved;
  508. int mi = *idx & 0xffffffff;
  509. int ri = *idx >> 32;
  510. for ( ; mi < mem->cnt; mi++) {
  511. struct memblock_region *m = &mem->regions[mi];
  512. phys_addr_t m_start = m->base;
  513. phys_addr_t m_end = m->base + m->size;
  514. /* only memory regions are associated with nodes, check it */
  515. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  516. continue;
  517. /* scan areas before each reservation for intersection */
  518. for ( ; ri < rsv->cnt + 1; ri++) {
  519. struct memblock_region *r = &rsv->regions[ri];
  520. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  521. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  522. /* if ri advanced past mi, break out to advance mi */
  523. if (r_start >= m_end)
  524. break;
  525. /* if the two regions intersect, we're done */
  526. if (m_start < r_end) {
  527. if (out_start)
  528. *out_start = max(m_start, r_start);
  529. if (out_end)
  530. *out_end = min(m_end, r_end);
  531. if (out_nid)
  532. *out_nid = memblock_get_region_node(m);
  533. /*
  534. * The region which ends first is advanced
  535. * for the next iteration.
  536. */
  537. if (m_end <= r_end)
  538. mi++;
  539. else
  540. ri++;
  541. *idx = (u32)mi | (u64)ri << 32;
  542. return;
  543. }
  544. }
  545. }
  546. /* signal end of iteration */
  547. *idx = ULLONG_MAX;
  548. }
  549. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  550. /*
  551. * Common iterator interface used to define for_each_mem_range().
  552. */
  553. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  554. unsigned long *out_start_pfn,
  555. unsigned long *out_end_pfn, int *out_nid)
  556. {
  557. struct memblock_type *type = &memblock.memory;
  558. struct memblock_region *r;
  559. while (++*idx < type->cnt) {
  560. r = &type->regions[*idx];
  561. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  562. continue;
  563. if (nid == MAX_NUMNODES || nid == r->nid)
  564. break;
  565. }
  566. if (*idx >= type->cnt) {
  567. *idx = -1;
  568. return;
  569. }
  570. if (out_start_pfn)
  571. *out_start_pfn = PFN_UP(r->base);
  572. if (out_end_pfn)
  573. *out_end_pfn = PFN_DOWN(r->base + r->size);
  574. if (out_nid)
  575. *out_nid = r->nid;
  576. }
  577. /**
  578. * memblock_set_node - set node ID on memblock regions
  579. * @base: base of area to set node ID for
  580. * @size: size of area to set node ID for
  581. * @nid: node ID to set
  582. *
  583. * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
  584. * Regions which cross the area boundaries are split as necessary.
  585. *
  586. * RETURNS:
  587. * 0 on success, -errno on failure.
  588. */
  589. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  590. int nid)
  591. {
  592. struct memblock_type *type = &memblock.memory;
  593. int start_rgn, end_rgn;
  594. int i, ret;
  595. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  596. if (ret)
  597. return ret;
  598. for (i = start_rgn; i < end_rgn; i++)
  599. type->regions[i].nid = nid;
  600. memblock_merge_regions(type);
  601. return 0;
  602. }
  603. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  604. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  605. {
  606. phys_addr_t found;
  607. /* We align the size to limit fragmentation. Without this, a lot of
  608. * small allocs quickly eat up the whole reserve array on sparc
  609. */
  610. size = round_up(size, align);
  611. found = memblock_find_in_range(0, max_addr, size, align);
  612. if (found && !memblock_reserve(found, size))
  613. return found;
  614. return 0;
  615. }
  616. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  617. {
  618. phys_addr_t alloc;
  619. alloc = __memblock_alloc_base(size, align, max_addr);
  620. if (alloc == 0)
  621. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  622. (unsigned long long) size, (unsigned long long) max_addr);
  623. return alloc;
  624. }
  625. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  626. {
  627. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  628. }
  629. /*
  630. * Additional node-local top-down allocators.
  631. *
  632. * WARNING: Only available after early_node_map[] has been populated,
  633. * on some architectures, that is after all the calls to add_active_range()
  634. * have been done to populate it.
  635. */
  636. static phys_addr_t __init memblock_nid_range_rev(phys_addr_t start,
  637. phys_addr_t end, int *nid)
  638. {
  639. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  640. unsigned long start_pfn, end_pfn;
  641. int i;
  642. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, nid)
  643. if (end > PFN_PHYS(start_pfn) && end <= PFN_PHYS(end_pfn))
  644. return max(start, PFN_PHYS(start_pfn));
  645. #endif
  646. *nid = 0;
  647. return start;
  648. }
  649. phys_addr_t __init memblock_find_in_range_node(phys_addr_t start,
  650. phys_addr_t end,
  651. phys_addr_t size,
  652. phys_addr_t align, int nid)
  653. {
  654. struct memblock_type *mem = &memblock.memory;
  655. int i;
  656. BUG_ON(0 == size);
  657. /* Pump up max_addr */
  658. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  659. end = memblock.current_limit;
  660. for (i = mem->cnt - 1; i >= 0; i--) {
  661. struct memblock_region *r = &mem->regions[i];
  662. phys_addr_t base = max(start, r->base);
  663. phys_addr_t top = min(end, r->base + r->size);
  664. while (base < top) {
  665. phys_addr_t tbase, ret;
  666. int tnid;
  667. tbase = memblock_nid_range_rev(base, top, &tnid);
  668. if (nid == MAX_NUMNODES || tnid == nid) {
  669. ret = memblock_find_region(tbase, top, size, align);
  670. if (ret)
  671. return ret;
  672. }
  673. top = tbase;
  674. }
  675. }
  676. return 0;
  677. }
  678. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  679. {
  680. phys_addr_t found;
  681. /*
  682. * We align the size to limit fragmentation. Without this, a lot of
  683. * small allocs quickly eat up the whole reserve array on sparc
  684. */
  685. size = round_up(size, align);
  686. found = memblock_find_in_range_node(0, MEMBLOCK_ALLOC_ACCESSIBLE,
  687. size, align, nid);
  688. if (found && !memblock_reserve(found, size))
  689. return found;
  690. return 0;
  691. }
  692. phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  693. {
  694. phys_addr_t res = memblock_alloc_nid(size, align, nid);
  695. if (res)
  696. return res;
  697. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  698. }
  699. /*
  700. * Remaining API functions
  701. */
  702. /* You must call memblock_analyze() before this. */
  703. phys_addr_t __init memblock_phys_mem_size(void)
  704. {
  705. return memblock.memory_size;
  706. }
  707. /* lowest address */
  708. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  709. {
  710. return memblock.memory.regions[0].base;
  711. }
  712. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  713. {
  714. int idx = memblock.memory.cnt - 1;
  715. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  716. }
  717. /* You must call memblock_analyze() after this. */
  718. void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
  719. {
  720. unsigned long i;
  721. phys_addr_t limit;
  722. struct memblock_region *p;
  723. if (!memory_limit)
  724. return;
  725. /* Truncate the memblock regions to satisfy the memory limit. */
  726. limit = memory_limit;
  727. for (i = 0; i < memblock.memory.cnt; i++) {
  728. if (limit > memblock.memory.regions[i].size) {
  729. limit -= memblock.memory.regions[i].size;
  730. continue;
  731. }
  732. memblock.memory.regions[i].size = limit;
  733. memblock.memory.cnt = i + 1;
  734. break;
  735. }
  736. memory_limit = memblock_end_of_DRAM();
  737. /* And truncate any reserves above the limit also. */
  738. for (i = 0; i < memblock.reserved.cnt; i++) {
  739. p = &memblock.reserved.regions[i];
  740. if (p->base > memory_limit)
  741. p->size = 0;
  742. else if ((p->base + p->size) > memory_limit)
  743. p->size = memory_limit - p->base;
  744. if (p->size == 0) {
  745. memblock_remove_region(&memblock.reserved, i);
  746. i--;
  747. }
  748. }
  749. }
  750. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  751. {
  752. unsigned int left = 0, right = type->cnt;
  753. do {
  754. unsigned int mid = (right + left) / 2;
  755. if (addr < type->regions[mid].base)
  756. right = mid;
  757. else if (addr >= (type->regions[mid].base +
  758. type->regions[mid].size))
  759. left = mid + 1;
  760. else
  761. return mid;
  762. } while (left < right);
  763. return -1;
  764. }
  765. int __init memblock_is_reserved(phys_addr_t addr)
  766. {
  767. return memblock_search(&memblock.reserved, addr) != -1;
  768. }
  769. int __init_memblock memblock_is_memory(phys_addr_t addr)
  770. {
  771. return memblock_search(&memblock.memory, addr) != -1;
  772. }
  773. int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  774. {
  775. int idx = memblock_search(&memblock.memory, base);
  776. if (idx == -1)
  777. return 0;
  778. return memblock.memory.regions[idx].base <= base &&
  779. (memblock.memory.regions[idx].base +
  780. memblock.memory.regions[idx].size) >= (base + size);
  781. }
  782. int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  783. {
  784. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  785. }
  786. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  787. {
  788. memblock.current_limit = limit;
  789. }
  790. static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
  791. {
  792. unsigned long long base, size;
  793. int i;
  794. pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
  795. for (i = 0; i < type->cnt; i++) {
  796. struct memblock_region *rgn = &type->regions[i];
  797. char nid_buf[32] = "";
  798. base = rgn->base;
  799. size = rgn->size;
  800. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  801. if (memblock_get_region_node(rgn) != MAX_NUMNODES)
  802. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  803. memblock_get_region_node(rgn));
  804. #endif
  805. pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
  806. name, i, base, base + size - 1, size, nid_buf);
  807. }
  808. }
  809. void __init_memblock __memblock_dump_all(void)
  810. {
  811. pr_info("MEMBLOCK configuration:\n");
  812. pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
  813. memblock_dump(&memblock.memory, "memory");
  814. memblock_dump(&memblock.reserved, "reserved");
  815. }
  816. void __init memblock_analyze(void)
  817. {
  818. int i;
  819. memblock.memory_size = 0;
  820. for (i = 0; i < memblock.memory.cnt; i++)
  821. memblock.memory_size += memblock.memory.regions[i].size;
  822. /* We allow resizing from there */
  823. memblock_can_resize = 1;
  824. }
  825. static int __init early_memblock(char *p)
  826. {
  827. if (p && strstr(p, "debug"))
  828. memblock_debug = 1;
  829. return 0;
  830. }
  831. early_param("memblock", early_memblock);
  832. #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
  833. static int memblock_debug_show(struct seq_file *m, void *private)
  834. {
  835. struct memblock_type *type = m->private;
  836. struct memblock_region *reg;
  837. int i;
  838. for (i = 0; i < type->cnt; i++) {
  839. reg = &type->regions[i];
  840. seq_printf(m, "%4d: ", i);
  841. if (sizeof(phys_addr_t) == 4)
  842. seq_printf(m, "0x%08lx..0x%08lx\n",
  843. (unsigned long)reg->base,
  844. (unsigned long)(reg->base + reg->size - 1));
  845. else
  846. seq_printf(m, "0x%016llx..0x%016llx\n",
  847. (unsigned long long)reg->base,
  848. (unsigned long long)(reg->base + reg->size - 1));
  849. }
  850. return 0;
  851. }
  852. static int memblock_debug_open(struct inode *inode, struct file *file)
  853. {
  854. return single_open(file, memblock_debug_show, inode->i_private);
  855. }
  856. static const struct file_operations memblock_debug_fops = {
  857. .open = memblock_debug_open,
  858. .read = seq_read,
  859. .llseek = seq_lseek,
  860. .release = single_release,
  861. };
  862. static int __init memblock_init_debugfs(void)
  863. {
  864. struct dentry *root = debugfs_create_dir("memblock", NULL);
  865. if (!root)
  866. return -ENXIO;
  867. debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
  868. debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
  869. return 0;
  870. }
  871. __initcall(memblock_init_debugfs);
  872. #endif /* CONFIG_DEBUG_FS */