memblock.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. /* 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. * Find, allocate, deallocate or reserve unreserved regions. All allocations
  71. * are top-down.
  72. */
  73. static phys_addr_t __init_memblock memblock_find_region(phys_addr_t start, phys_addr_t end,
  74. phys_addr_t size, phys_addr_t align)
  75. {
  76. phys_addr_t base, res_base;
  77. long j;
  78. /* In case, huge size is requested */
  79. if (end < size)
  80. return 0;
  81. base = round_down(end - size, align);
  82. /* Prevent allocations returning 0 as it's also used to
  83. * indicate an allocation failure
  84. */
  85. if (start == 0)
  86. start = PAGE_SIZE;
  87. while (start <= base) {
  88. j = memblock_overlaps_region(&memblock.reserved, base, size);
  89. if (j < 0)
  90. return base;
  91. res_base = memblock.reserved.regions[j].base;
  92. if (res_base < size)
  93. break;
  94. base = round_down(res_base - size, align);
  95. }
  96. return 0;
  97. }
  98. /*
  99. * Find a free area with specified alignment in a specific range.
  100. */
  101. phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, phys_addr_t end,
  102. phys_addr_t size, phys_addr_t align)
  103. {
  104. long i;
  105. BUG_ON(0 == size);
  106. /* Pump up max_addr */
  107. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  108. end = memblock.current_limit;
  109. /* We do a top-down search, this tends to limit memory
  110. * fragmentation by keeping early boot allocs near the
  111. * top of memory
  112. */
  113. for (i = memblock.memory.cnt - 1; i >= 0; i--) {
  114. phys_addr_t memblockbase = memblock.memory.regions[i].base;
  115. phys_addr_t memblocksize = memblock.memory.regions[i].size;
  116. phys_addr_t bottom, top, found;
  117. if (memblocksize < size)
  118. continue;
  119. if ((memblockbase + memblocksize) <= start)
  120. break;
  121. bottom = max(memblockbase, start);
  122. top = min(memblockbase + memblocksize, end);
  123. if (bottom >= top)
  124. continue;
  125. found = memblock_find_region(bottom, top, size, align);
  126. if (found)
  127. return found;
  128. }
  129. return 0;
  130. }
  131. /*
  132. * Free memblock.reserved.regions
  133. */
  134. int __init_memblock memblock_free_reserved_regions(void)
  135. {
  136. if (memblock.reserved.regions == memblock_reserved_init_regions)
  137. return 0;
  138. return memblock_free(__pa(memblock.reserved.regions),
  139. sizeof(struct memblock_region) * memblock.reserved.max);
  140. }
  141. /*
  142. * Reserve memblock.reserved.regions
  143. */
  144. int __init_memblock memblock_reserve_reserved_regions(void)
  145. {
  146. if (memblock.reserved.regions == memblock_reserved_init_regions)
  147. return 0;
  148. return memblock_reserve(__pa(memblock.reserved.regions),
  149. sizeof(struct memblock_region) * memblock.reserved.max);
  150. }
  151. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  152. {
  153. memmove(&type->regions[r], &type->regions[r + 1],
  154. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  155. type->cnt--;
  156. /* Special case for empty arrays */
  157. if (type->cnt == 0) {
  158. type->cnt = 1;
  159. type->regions[0].base = 0;
  160. type->regions[0].size = 0;
  161. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  162. }
  163. }
  164. static int __init_memblock memblock_double_array(struct memblock_type *type)
  165. {
  166. struct memblock_region *new_array, *old_array;
  167. phys_addr_t old_size, new_size, addr;
  168. int use_slab = slab_is_available();
  169. /* We don't allow resizing until we know about the reserved regions
  170. * of memory that aren't suitable for allocation
  171. */
  172. if (!memblock_can_resize)
  173. return -1;
  174. /* Calculate new doubled size */
  175. old_size = type->max * sizeof(struct memblock_region);
  176. new_size = old_size << 1;
  177. /* Try to find some space for it.
  178. *
  179. * WARNING: We assume that either slab_is_available() and we use it or
  180. * we use MEMBLOCK for allocations. That means that this is unsafe to use
  181. * when bootmem is currently active (unless bootmem itself is implemented
  182. * on top of MEMBLOCK which isn't the case yet)
  183. *
  184. * This should however not be an issue for now, as we currently only
  185. * call into MEMBLOCK while it's still active, or much later when slab is
  186. * active for memory hotplug operations
  187. */
  188. if (use_slab) {
  189. new_array = kmalloc(new_size, GFP_KERNEL);
  190. addr = new_array ? __pa(new_array) : 0;
  191. } else
  192. addr = memblock_find_in_range(0, MEMBLOCK_ALLOC_ACCESSIBLE, new_size, sizeof(phys_addr_t));
  193. if (!addr) {
  194. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  195. memblock_type_name(type), type->max, type->max * 2);
  196. return -1;
  197. }
  198. new_array = __va(addr);
  199. memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]",
  200. memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1);
  201. /* Found space, we now need to move the array over before
  202. * we add the reserved region since it may be our reserved
  203. * array itself that is full.
  204. */
  205. memcpy(new_array, type->regions, old_size);
  206. memset(new_array + type->max, 0, old_size);
  207. old_array = type->regions;
  208. type->regions = new_array;
  209. type->max <<= 1;
  210. /* If we use SLAB that's it, we are done */
  211. if (use_slab)
  212. return 0;
  213. /* Add the new reserved region now. Should not fail ! */
  214. BUG_ON(memblock_reserve(addr, new_size));
  215. /* If the array wasn't our static init one, then free it. We only do
  216. * that before SLAB is available as later on, we don't know whether
  217. * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
  218. * anyways
  219. */
  220. if (old_array != memblock_memory_init_regions &&
  221. old_array != memblock_reserved_init_regions)
  222. memblock_free(__pa(old_array), old_size);
  223. return 0;
  224. }
  225. /**
  226. * memblock_merge_regions - merge neighboring compatible regions
  227. * @type: memblock type to scan
  228. *
  229. * Scan @type and merge neighboring compatible regions.
  230. */
  231. static void __init_memblock memblock_merge_regions(struct memblock_type *type)
  232. {
  233. int i = 0;
  234. /* cnt never goes below 1 */
  235. while (i < type->cnt - 1) {
  236. struct memblock_region *this = &type->regions[i];
  237. struct memblock_region *next = &type->regions[i + 1];
  238. if (this->base + this->size != next->base ||
  239. memblock_get_region_node(this) !=
  240. memblock_get_region_node(next)) {
  241. BUG_ON(this->base + this->size > next->base);
  242. i++;
  243. continue;
  244. }
  245. this->size += next->size;
  246. memmove(next, next + 1, (type->cnt - (i + 1)) * sizeof(*next));
  247. type->cnt--;
  248. }
  249. }
  250. /**
  251. * memblock_insert_region - insert new memblock region
  252. * @type: memblock type to insert into
  253. * @idx: index for the insertion point
  254. * @base: base address of the new region
  255. * @size: size of the new region
  256. *
  257. * Insert new memblock region [@base,@base+@size) into @type at @idx.
  258. * @type must already have extra room to accomodate the new region.
  259. */
  260. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  261. int idx, phys_addr_t base,
  262. phys_addr_t size, int nid)
  263. {
  264. struct memblock_region *rgn = &type->regions[idx];
  265. BUG_ON(type->cnt >= type->max);
  266. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  267. rgn->base = base;
  268. rgn->size = size;
  269. memblock_set_region_node(rgn, nid);
  270. type->cnt++;
  271. }
  272. /**
  273. * memblock_add_region - add new memblock region
  274. * @type: memblock type to add new region into
  275. * @base: base address of the new region
  276. * @size: size of the new region
  277. *
  278. * Add new memblock region [@base,@base+@size) into @type. The new region
  279. * is allowed to overlap with existing ones - overlaps don't affect already
  280. * existing regions. @type is guaranteed to be minimal (all neighbouring
  281. * compatible regions are merged) after the addition.
  282. *
  283. * RETURNS:
  284. * 0 on success, -errno on failure.
  285. */
  286. static int __init_memblock memblock_add_region(struct memblock_type *type,
  287. phys_addr_t base, phys_addr_t size)
  288. {
  289. bool insert = false;
  290. phys_addr_t obase = base;
  291. phys_addr_t end = base + memblock_cap_size(base, &size);
  292. int i, nr_new;
  293. /* special case for empty array */
  294. if (type->regions[0].size == 0) {
  295. WARN_ON(type->cnt != 1);
  296. type->regions[0].base = base;
  297. type->regions[0].size = size;
  298. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  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, MAX_NUMNODES);
  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,
  335. MAX_NUMNODES);
  336. }
  337. /*
  338. * If this was the first round, resize array and repeat for actual
  339. * insertions; otherwise, merge and return.
  340. */
  341. if (!insert) {
  342. while (type->cnt + nr_new > type->max)
  343. if (memblock_double_array(type) < 0)
  344. return -ENOMEM;
  345. insert = true;
  346. goto repeat;
  347. } else {
  348. memblock_merge_regions(type);
  349. return 0;
  350. }
  351. }
  352. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  353. {
  354. return memblock_add_region(&memblock.memory, base, size);
  355. }
  356. /**
  357. * memblock_isolate_range - isolate given range into disjoint memblocks
  358. * @type: memblock type to isolate range for
  359. * @base: base of range to isolate
  360. * @size: size of range to isolate
  361. * @start_rgn: out parameter for the start of isolated region
  362. * @end_rgn: out parameter for the end of isolated region
  363. *
  364. * Walk @type and ensure that regions don't cross the boundaries defined by
  365. * [@base,@base+@size). Crossing regions are split at the boundaries,
  366. * which may create at most two more regions. The index of the first
  367. * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  368. *
  369. * RETURNS:
  370. * 0 on success, -errno on failure.
  371. */
  372. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  373. phys_addr_t base, phys_addr_t size,
  374. int *start_rgn, int *end_rgn)
  375. {
  376. phys_addr_t end = base + memblock_cap_size(base, &size);
  377. int i;
  378. *start_rgn = *end_rgn = 0;
  379. /* we'll create at most two more regions */
  380. while (type->cnt + 2 > type->max)
  381. if (memblock_double_array(type) < 0)
  382. return -ENOMEM;
  383. for (i = 0; i < type->cnt; i++) {
  384. struct memblock_region *rgn = &type->regions[i];
  385. phys_addr_t rbase = rgn->base;
  386. phys_addr_t rend = rbase + rgn->size;
  387. if (rbase >= end)
  388. break;
  389. if (rend <= base)
  390. continue;
  391. if (rbase < base) {
  392. /*
  393. * @rgn intersects from below. Split and continue
  394. * to process the next region - the new top half.
  395. */
  396. rgn->base = base;
  397. rgn->size = rend - rgn->base;
  398. memblock_insert_region(type, i, rbase, base - rbase,
  399. memblock_get_region_node(rgn));
  400. } else if (rend > end) {
  401. /*
  402. * @rgn intersects from above. Split and redo the
  403. * current region - the new bottom half.
  404. */
  405. rgn->base = end;
  406. rgn->size = rend - rgn->base;
  407. memblock_insert_region(type, i--, rbase, end - rbase,
  408. memblock_get_region_node(rgn));
  409. } else {
  410. /* @rgn is fully contained, record it */
  411. if (!*end_rgn)
  412. *start_rgn = i;
  413. *end_rgn = i + 1;
  414. }
  415. }
  416. return 0;
  417. }
  418. static int __init_memblock __memblock_remove(struct memblock_type *type,
  419. phys_addr_t base, phys_addr_t size)
  420. {
  421. int start_rgn, end_rgn;
  422. int i, ret;
  423. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  424. if (ret)
  425. return ret;
  426. for (i = end_rgn - 1; i >= start_rgn; i--)
  427. memblock_remove_region(type, i);
  428. return 0;
  429. }
  430. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  431. {
  432. return __memblock_remove(&memblock.memory, base, size);
  433. }
  434. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  435. {
  436. memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  437. (unsigned long long)base,
  438. (unsigned long long)base + size,
  439. (void *)_RET_IP_);
  440. return __memblock_remove(&memblock.reserved, base, size);
  441. }
  442. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  443. {
  444. struct memblock_type *_rgn = &memblock.reserved;
  445. memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
  446. (unsigned long long)base,
  447. (unsigned long long)base + size,
  448. (void *)_RET_IP_);
  449. BUG_ON(0 == size);
  450. return memblock_add_region(_rgn, base, size);
  451. }
  452. /**
  453. * __next_free_mem_range - next function for for_each_free_mem_range()
  454. * @idx: pointer to u64 loop variable
  455. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  456. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  457. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  458. * @p_nid: ptr to int for nid of the range, can be %NULL
  459. *
  460. * Find the first free area from *@idx which matches @nid, fill the out
  461. * parameters, and update *@idx for the next iteration. The lower 32bit of
  462. * *@idx contains index into memory region and the upper 32bit indexes the
  463. * areas before each reserved region. For example, if reserved regions
  464. * look like the following,
  465. *
  466. * 0:[0-16), 1:[32-48), 2:[128-130)
  467. *
  468. * The upper 32bit indexes the following regions.
  469. *
  470. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  471. *
  472. * As both region arrays are sorted, the function advances the two indices
  473. * in lockstep and returns each intersection.
  474. */
  475. void __init_memblock __next_free_mem_range(u64 *idx, int nid,
  476. phys_addr_t *out_start,
  477. phys_addr_t *out_end, int *out_nid)
  478. {
  479. struct memblock_type *mem = &memblock.memory;
  480. struct memblock_type *rsv = &memblock.reserved;
  481. int mi = *idx & 0xffffffff;
  482. int ri = *idx >> 32;
  483. for ( ; mi < mem->cnt; mi++) {
  484. struct memblock_region *m = &mem->regions[mi];
  485. phys_addr_t m_start = m->base;
  486. phys_addr_t m_end = m->base + m->size;
  487. /* only memory regions are associated with nodes, check it */
  488. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  489. continue;
  490. /* scan areas before each reservation for intersection */
  491. for ( ; ri < rsv->cnt + 1; ri++) {
  492. struct memblock_region *r = &rsv->regions[ri];
  493. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  494. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  495. /* if ri advanced past mi, break out to advance mi */
  496. if (r_start >= m_end)
  497. break;
  498. /* if the two regions intersect, we're done */
  499. if (m_start < r_end) {
  500. if (out_start)
  501. *out_start = max(m_start, r_start);
  502. if (out_end)
  503. *out_end = min(m_end, r_end);
  504. if (out_nid)
  505. *out_nid = memblock_get_region_node(m);
  506. /*
  507. * The region which ends first is advanced
  508. * for the next iteration.
  509. */
  510. if (m_end <= r_end)
  511. mi++;
  512. else
  513. ri++;
  514. *idx = (u32)mi | (u64)ri << 32;
  515. return;
  516. }
  517. }
  518. }
  519. /* signal end of iteration */
  520. *idx = ULLONG_MAX;
  521. }
  522. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  523. /*
  524. * Common iterator interface used to define for_each_mem_range().
  525. */
  526. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  527. unsigned long *out_start_pfn,
  528. unsigned long *out_end_pfn, int *out_nid)
  529. {
  530. struct memblock_type *type = &memblock.memory;
  531. struct memblock_region *r;
  532. while (++*idx < type->cnt) {
  533. r = &type->regions[*idx];
  534. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  535. continue;
  536. if (nid == MAX_NUMNODES || nid == r->nid)
  537. break;
  538. }
  539. if (*idx >= type->cnt) {
  540. *idx = -1;
  541. return;
  542. }
  543. if (out_start_pfn)
  544. *out_start_pfn = PFN_UP(r->base);
  545. if (out_end_pfn)
  546. *out_end_pfn = PFN_DOWN(r->base + r->size);
  547. if (out_nid)
  548. *out_nid = r->nid;
  549. }
  550. /**
  551. * memblock_set_node - set node ID on memblock regions
  552. * @base: base of area to set node ID for
  553. * @size: size of area to set node ID for
  554. * @nid: node ID to set
  555. *
  556. * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
  557. * Regions which cross the area boundaries are split as necessary.
  558. *
  559. * RETURNS:
  560. * 0 on success, -errno on failure.
  561. */
  562. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  563. int nid)
  564. {
  565. struct memblock_type *type = &memblock.memory;
  566. int start_rgn, end_rgn;
  567. int i, ret;
  568. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  569. if (ret)
  570. return ret;
  571. for (i = start_rgn; i < end_rgn; i++)
  572. type->regions[i].nid = nid;
  573. memblock_merge_regions(type);
  574. return 0;
  575. }
  576. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  577. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  578. {
  579. phys_addr_t found;
  580. /* We align the size to limit fragmentation. Without this, a lot of
  581. * small allocs quickly eat up the whole reserve array on sparc
  582. */
  583. size = round_up(size, align);
  584. found = memblock_find_in_range(0, max_addr, size, align);
  585. if (found && !memblock_reserve(found, size))
  586. return found;
  587. return 0;
  588. }
  589. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  590. {
  591. phys_addr_t alloc;
  592. alloc = __memblock_alloc_base(size, align, max_addr);
  593. if (alloc == 0)
  594. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  595. (unsigned long long) size, (unsigned long long) max_addr);
  596. return alloc;
  597. }
  598. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  599. {
  600. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  601. }
  602. /*
  603. * Additional node-local top-down allocators.
  604. *
  605. * WARNING: Only available after early_node_map[] has been populated,
  606. * on some architectures, that is after all the calls to add_active_range()
  607. * have been done to populate it.
  608. */
  609. static phys_addr_t __init memblock_nid_range_rev(phys_addr_t start,
  610. phys_addr_t end, int *nid)
  611. {
  612. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  613. unsigned long start_pfn, end_pfn;
  614. int i;
  615. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, nid)
  616. if (end > PFN_PHYS(start_pfn) && end <= PFN_PHYS(end_pfn))
  617. return max(start, PFN_PHYS(start_pfn));
  618. #endif
  619. *nid = 0;
  620. return start;
  621. }
  622. phys_addr_t __init memblock_find_in_range_node(phys_addr_t start,
  623. phys_addr_t end,
  624. phys_addr_t size,
  625. phys_addr_t align, int nid)
  626. {
  627. struct memblock_type *mem = &memblock.memory;
  628. int i;
  629. BUG_ON(0 == size);
  630. /* Pump up max_addr */
  631. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  632. end = memblock.current_limit;
  633. for (i = mem->cnt - 1; i >= 0; i--) {
  634. struct memblock_region *r = &mem->regions[i];
  635. phys_addr_t base = max(start, r->base);
  636. phys_addr_t top = min(end, r->base + r->size);
  637. while (base < top) {
  638. phys_addr_t tbase, ret;
  639. int tnid;
  640. tbase = memblock_nid_range_rev(base, top, &tnid);
  641. if (nid == MAX_NUMNODES || tnid == nid) {
  642. ret = memblock_find_region(tbase, top, size, align);
  643. if (ret)
  644. return ret;
  645. }
  646. top = tbase;
  647. }
  648. }
  649. return 0;
  650. }
  651. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  652. {
  653. phys_addr_t found;
  654. /*
  655. * We align the size to limit fragmentation. Without this, a lot of
  656. * small allocs quickly eat up the whole reserve array on sparc
  657. */
  658. size = round_up(size, align);
  659. found = memblock_find_in_range_node(0, MEMBLOCK_ALLOC_ACCESSIBLE,
  660. size, align, nid);
  661. if (found && !memblock_reserve(found, size))
  662. return found;
  663. return 0;
  664. }
  665. phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  666. {
  667. phys_addr_t res = memblock_alloc_nid(size, align, nid);
  668. if (res)
  669. return res;
  670. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  671. }
  672. /*
  673. * Remaining API functions
  674. */
  675. /* You must call memblock_analyze() before this. */
  676. phys_addr_t __init memblock_phys_mem_size(void)
  677. {
  678. return memblock.memory_size;
  679. }
  680. /* lowest address */
  681. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  682. {
  683. return memblock.memory.regions[0].base;
  684. }
  685. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  686. {
  687. int idx = memblock.memory.cnt - 1;
  688. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  689. }
  690. /* You must call memblock_analyze() after this. */
  691. void __init memblock_enforce_memory_limit(phys_addr_t limit)
  692. {
  693. unsigned long i;
  694. phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
  695. if (!limit)
  696. return;
  697. /* find out max address */
  698. for (i = 0; i < memblock.memory.cnt; i++) {
  699. struct memblock_region *r = &memblock.memory.regions[i];
  700. if (limit <= r->size) {
  701. max_addr = r->base + limit;
  702. break;
  703. }
  704. limit -= r->size;
  705. }
  706. /* truncate both memory and reserved regions */
  707. __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
  708. __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
  709. }
  710. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  711. {
  712. unsigned int left = 0, right = type->cnt;
  713. do {
  714. unsigned int mid = (right + left) / 2;
  715. if (addr < type->regions[mid].base)
  716. right = mid;
  717. else if (addr >= (type->regions[mid].base +
  718. type->regions[mid].size))
  719. left = mid + 1;
  720. else
  721. return mid;
  722. } while (left < right);
  723. return -1;
  724. }
  725. int __init memblock_is_reserved(phys_addr_t addr)
  726. {
  727. return memblock_search(&memblock.reserved, addr) != -1;
  728. }
  729. int __init_memblock memblock_is_memory(phys_addr_t addr)
  730. {
  731. return memblock_search(&memblock.memory, addr) != -1;
  732. }
  733. int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  734. {
  735. int idx = memblock_search(&memblock.memory, base);
  736. phys_addr_t end = base + memblock_cap_size(base, &size);
  737. if (idx == -1)
  738. return 0;
  739. return memblock.memory.regions[idx].base <= base &&
  740. (memblock.memory.regions[idx].base +
  741. memblock.memory.regions[idx].size) >= end;
  742. }
  743. int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  744. {
  745. memblock_cap_size(base, &size);
  746. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  747. }
  748. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  749. {
  750. memblock.current_limit = limit;
  751. }
  752. static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
  753. {
  754. unsigned long long base, size;
  755. int i;
  756. pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
  757. for (i = 0; i < type->cnt; i++) {
  758. struct memblock_region *rgn = &type->regions[i];
  759. char nid_buf[32] = "";
  760. base = rgn->base;
  761. size = rgn->size;
  762. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  763. if (memblock_get_region_node(rgn) != MAX_NUMNODES)
  764. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  765. memblock_get_region_node(rgn));
  766. #endif
  767. pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
  768. name, i, base, base + size - 1, size, nid_buf);
  769. }
  770. }
  771. void __init_memblock __memblock_dump_all(void)
  772. {
  773. pr_info("MEMBLOCK configuration:\n");
  774. pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
  775. memblock_dump(&memblock.memory, "memory");
  776. memblock_dump(&memblock.reserved, "reserved");
  777. }
  778. void __init memblock_analyze(void)
  779. {
  780. int i;
  781. memblock.memory_size = 0;
  782. for (i = 0; i < memblock.memory.cnt; i++)
  783. memblock.memory_size += memblock.memory.regions[i].size;
  784. /* We allow resizing from there */
  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 */