memblock.c 27 KB

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