memblock.c 27 KB

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