memblock.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. #include <asm-generic/sections.h>
  22. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  23. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  24. struct memblock memblock __initdata_memblock = {
  25. .memory.regions = memblock_memory_init_regions,
  26. .memory.cnt = 1, /* empty dummy entry */
  27. .memory.max = INIT_MEMBLOCK_REGIONS,
  28. .reserved.regions = memblock_reserved_init_regions,
  29. .reserved.cnt = 1, /* empty dummy entry */
  30. .reserved.max = INIT_MEMBLOCK_REGIONS,
  31. .bottom_up = false,
  32. .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
  33. };
  34. int memblock_debug __initdata_memblock;
  35. static int memblock_can_resize __initdata_memblock;
  36. static int memblock_memory_in_slab __initdata_memblock = 0;
  37. static int memblock_reserved_in_slab __initdata_memblock = 0;
  38. /* inline so we don't get a warning when pr_debug is compiled out */
  39. static __init_memblock const char *
  40. memblock_type_name(struct memblock_type *type)
  41. {
  42. if (type == &memblock.memory)
  43. return "memory";
  44. else if (type == &memblock.reserved)
  45. return "reserved";
  46. else
  47. return "unknown";
  48. }
  49. /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
  50. static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
  51. {
  52. return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
  53. }
  54. /*
  55. * Address comparison utilities
  56. */
  57. static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
  58. phys_addr_t base2, phys_addr_t size2)
  59. {
  60. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  61. }
  62. static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
  63. phys_addr_t base, phys_addr_t size)
  64. {
  65. unsigned long i;
  66. for (i = 0; i < type->cnt; i++) {
  67. phys_addr_t rgnbase = type->regions[i].base;
  68. phys_addr_t rgnsize = type->regions[i].size;
  69. if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
  70. break;
  71. }
  72. return (i < type->cnt) ? i : -1;
  73. }
  74. /*
  75. * __memblock_find_range_bottom_up - find free area utility in bottom-up
  76. * @start: start of candidate range
  77. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  78. * @size: size of free area to find
  79. * @align: alignment of free area to find
  80. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  81. *
  82. * Utility called from memblock_find_in_range_node(), find free area bottom-up.
  83. *
  84. * RETURNS:
  85. * Found address on success, 0 on failure.
  86. */
  87. static phys_addr_t __init_memblock
  88. __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
  89. phys_addr_t size, phys_addr_t align, int nid)
  90. {
  91. phys_addr_t this_start, this_end, cand;
  92. u64 i;
  93. for_each_free_mem_range(i, nid, &this_start, &this_end, NULL) {
  94. this_start = clamp(this_start, start, end);
  95. this_end = clamp(this_end, start, end);
  96. cand = round_up(this_start, align);
  97. if (cand < this_end && this_end - cand >= size)
  98. return cand;
  99. }
  100. return 0;
  101. }
  102. /**
  103. * __memblock_find_range_top_down - find free area utility, in top-down
  104. * @start: start of candidate range
  105. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  106. * @size: size of free area to find
  107. * @align: alignment of free area to find
  108. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  109. *
  110. * Utility called from memblock_find_in_range_node(), find free area top-down.
  111. *
  112. * RETURNS:
  113. * Found address on success, 0 on failure.
  114. */
  115. static phys_addr_t __init_memblock
  116. __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  117. phys_addr_t size, phys_addr_t align, int nid)
  118. {
  119. phys_addr_t this_start, this_end, cand;
  120. u64 i;
  121. for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
  122. this_start = clamp(this_start, start, end);
  123. this_end = clamp(this_end, start, end);
  124. if (this_end < size)
  125. continue;
  126. cand = round_down(this_end - size, align);
  127. if (cand >= this_start)
  128. return cand;
  129. }
  130. return 0;
  131. }
  132. /**
  133. * memblock_find_in_range_node - find free area in given range and node
  134. * @start: start of candidate range
  135. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  136. * @size: size of free area to find
  137. * @align: alignment of free area to find
  138. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  139. *
  140. * Find @size free area aligned to @align in the specified range and node.
  141. *
  142. * When allocation direction is bottom-up, the @start should be greater
  143. * than the end of the kernel image. Otherwise, it will be trimmed. The
  144. * reason is that we want the bottom-up allocation just near the kernel
  145. * image so it is highly likely that the allocated memory and the kernel
  146. * will reside in the same node.
  147. *
  148. * If bottom-up allocation failed, will try to allocate memory top-down.
  149. *
  150. * RETURNS:
  151. * Found address on success, 0 on failure.
  152. */
  153. phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,
  154. phys_addr_t end, phys_addr_t size,
  155. phys_addr_t align, int nid)
  156. {
  157. int ret;
  158. phys_addr_t kernel_end;
  159. /* pump up @end */
  160. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  161. end = memblock.current_limit;
  162. /* avoid allocating the first page */
  163. start = max_t(phys_addr_t, start, PAGE_SIZE);
  164. end = max(start, end);
  165. kernel_end = __pa_symbol(_end);
  166. /*
  167. * try bottom-up allocation only when bottom-up mode
  168. * is set and @end is above the kernel image.
  169. */
  170. if (memblock_bottom_up() && end > kernel_end) {
  171. phys_addr_t bottom_up_start;
  172. /* make sure we will allocate above the kernel */
  173. bottom_up_start = max(start, kernel_end);
  174. /* ok, try bottom-up allocation first */
  175. ret = __memblock_find_range_bottom_up(bottom_up_start, end,
  176. size, align, nid);
  177. if (ret)
  178. return ret;
  179. /*
  180. * we always limit bottom-up allocation above the kernel,
  181. * but top-down allocation doesn't have the limit, so
  182. * retrying top-down allocation may succeed when bottom-up
  183. * allocation failed.
  184. *
  185. * bottom-up allocation is expected to be fail very rarely,
  186. * so we use WARN_ONCE() here to see the stack trace if
  187. * fail happens.
  188. */
  189. WARN_ONCE(1, "memblock: bottom-up allocation failed, "
  190. "memory hotunplug may be affected\n");
  191. }
  192. return __memblock_find_range_top_down(start, end, size, align, nid);
  193. }
  194. /**
  195. * memblock_find_in_range - find free area in given range
  196. * @start: start of candidate range
  197. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  198. * @size: size of free area to find
  199. * @align: alignment of free area to find
  200. *
  201. * Find @size free area aligned to @align in the specified range.
  202. *
  203. * RETURNS:
  204. * Found address on success, 0 on failure.
  205. */
  206. phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
  207. phys_addr_t end, phys_addr_t size,
  208. phys_addr_t align)
  209. {
  210. return memblock_find_in_range_node(start, end, size, align,
  211. MAX_NUMNODES);
  212. }
  213. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  214. {
  215. type->total_size -= type->regions[r].size;
  216. memmove(&type->regions[r], &type->regions[r + 1],
  217. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  218. type->cnt--;
  219. /* Special case for empty arrays */
  220. if (type->cnt == 0) {
  221. WARN_ON(type->total_size != 0);
  222. type->cnt = 1;
  223. type->regions[0].base = 0;
  224. type->regions[0].size = 0;
  225. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  226. }
  227. }
  228. phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
  229. phys_addr_t *addr)
  230. {
  231. if (memblock.reserved.regions == memblock_reserved_init_regions)
  232. return 0;
  233. *addr = __pa(memblock.reserved.regions);
  234. return PAGE_ALIGN(sizeof(struct memblock_region) *
  235. memblock.reserved.max);
  236. }
  237. /**
  238. * memblock_double_array - double the size of the memblock regions array
  239. * @type: memblock type of the regions array being doubled
  240. * @new_area_start: starting address of memory range to avoid overlap with
  241. * @new_area_size: size of memory range to avoid overlap with
  242. *
  243. * Double the size of the @type regions array. If memblock is being used to
  244. * allocate memory for a new reserved regions array and there is a previously
  245. * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
  246. * waiting to be reserved, ensure the memory used by the new array does
  247. * not overlap.
  248. *
  249. * RETURNS:
  250. * 0 on success, -1 on failure.
  251. */
  252. static int __init_memblock memblock_double_array(struct memblock_type *type,
  253. phys_addr_t new_area_start,
  254. phys_addr_t new_area_size)
  255. {
  256. struct memblock_region *new_array, *old_array;
  257. phys_addr_t old_alloc_size, new_alloc_size;
  258. phys_addr_t old_size, new_size, addr;
  259. int use_slab = slab_is_available();
  260. int *in_slab;
  261. /* We don't allow resizing until we know about the reserved regions
  262. * of memory that aren't suitable for allocation
  263. */
  264. if (!memblock_can_resize)
  265. return -1;
  266. /* Calculate new doubled size */
  267. old_size = type->max * sizeof(struct memblock_region);
  268. new_size = old_size << 1;
  269. /*
  270. * We need to allocated new one align to PAGE_SIZE,
  271. * so we can free them completely later.
  272. */
  273. old_alloc_size = PAGE_ALIGN(old_size);
  274. new_alloc_size = PAGE_ALIGN(new_size);
  275. /* Retrieve the slab flag */
  276. if (type == &memblock.memory)
  277. in_slab = &memblock_memory_in_slab;
  278. else
  279. in_slab = &memblock_reserved_in_slab;
  280. /* Try to find some space for it.
  281. *
  282. * WARNING: We assume that either slab_is_available() and we use it or
  283. * we use MEMBLOCK for allocations. That means that this is unsafe to
  284. * use when bootmem is currently active (unless bootmem itself is
  285. * implemented on top of MEMBLOCK which isn't the case yet)
  286. *
  287. * This should however not be an issue for now, as we currently only
  288. * call into MEMBLOCK while it's still active, or much later when slab
  289. * is active for memory hotplug operations
  290. */
  291. if (use_slab) {
  292. new_array = kmalloc(new_size, GFP_KERNEL);
  293. addr = new_array ? __pa(new_array) : 0;
  294. } else {
  295. /* only exclude range when trying to double reserved.regions */
  296. if (type != &memblock.reserved)
  297. new_area_start = new_area_size = 0;
  298. addr = memblock_find_in_range(new_area_start + new_area_size,
  299. memblock.current_limit,
  300. new_alloc_size, PAGE_SIZE);
  301. if (!addr && new_area_size)
  302. addr = memblock_find_in_range(0,
  303. min(new_area_start, memblock.current_limit),
  304. new_alloc_size, PAGE_SIZE);
  305. new_array = addr ? __va(addr) : NULL;
  306. }
  307. if (!addr) {
  308. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  309. memblock_type_name(type), type->max, type->max * 2);
  310. return -1;
  311. }
  312. memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
  313. memblock_type_name(type), type->max * 2, (u64)addr,
  314. (u64)addr + new_size - 1);
  315. /*
  316. * Found space, we now need to move the array over before we add the
  317. * reserved region since it may be our reserved array itself that is
  318. * full.
  319. */
  320. memcpy(new_array, type->regions, old_size);
  321. memset(new_array + type->max, 0, old_size);
  322. old_array = type->regions;
  323. type->regions = new_array;
  324. type->max <<= 1;
  325. /* Free old array. We needn't free it if the array is the static one */
  326. if (*in_slab)
  327. kfree(old_array);
  328. else if (old_array != memblock_memory_init_regions &&
  329. old_array != memblock_reserved_init_regions)
  330. memblock_free(__pa(old_array), old_alloc_size);
  331. /*
  332. * Reserve the new array if that comes from the memblock. Otherwise, we
  333. * needn't do it
  334. */
  335. if (!use_slab)
  336. BUG_ON(memblock_reserve(addr, new_alloc_size));
  337. /* Update slab flag */
  338. *in_slab = use_slab;
  339. return 0;
  340. }
  341. /**
  342. * memblock_merge_regions - merge neighboring compatible regions
  343. * @type: memblock type to scan
  344. *
  345. * Scan @type and merge neighboring compatible regions.
  346. */
  347. static void __init_memblock memblock_merge_regions(struct memblock_type *type)
  348. {
  349. int i = 0;
  350. /* cnt never goes below 1 */
  351. while (i < type->cnt - 1) {
  352. struct memblock_region *this = &type->regions[i];
  353. struct memblock_region *next = &type->regions[i + 1];
  354. if (this->base + this->size != next->base ||
  355. memblock_get_region_node(this) !=
  356. memblock_get_region_node(next)) {
  357. BUG_ON(this->base + this->size > next->base);
  358. i++;
  359. continue;
  360. }
  361. this->size += next->size;
  362. /* move forward from next + 1, index of which is i + 2 */
  363. memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
  364. type->cnt--;
  365. }
  366. }
  367. /**
  368. * memblock_insert_region - insert new memblock region
  369. * @type: memblock type to insert into
  370. * @idx: index for the insertion point
  371. * @base: base address of the new region
  372. * @size: size of the new region
  373. * @nid: node id of the new region
  374. *
  375. * Insert new memblock region [@base,@base+@size) into @type at @idx.
  376. * @type must already have extra room to accomodate the new region.
  377. */
  378. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  379. int idx, phys_addr_t base,
  380. phys_addr_t size, int nid)
  381. {
  382. struct memblock_region *rgn = &type->regions[idx];
  383. BUG_ON(type->cnt >= type->max);
  384. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  385. rgn->base = base;
  386. rgn->size = size;
  387. memblock_set_region_node(rgn, nid);
  388. type->cnt++;
  389. type->total_size += size;
  390. }
  391. /**
  392. * memblock_add_region - add new memblock region
  393. * @type: memblock type to add new region into
  394. * @base: base address of the new region
  395. * @size: size of the new region
  396. * @nid: nid of the new region
  397. *
  398. * Add new memblock region [@base,@base+@size) into @type. The new region
  399. * is allowed to overlap with existing ones - overlaps don't affect already
  400. * existing regions. @type is guaranteed to be minimal (all neighbouring
  401. * compatible regions are merged) after the addition.
  402. *
  403. * RETURNS:
  404. * 0 on success, -errno on failure.
  405. */
  406. static int __init_memblock memblock_add_region(struct memblock_type *type,
  407. phys_addr_t base, phys_addr_t size, int nid)
  408. {
  409. bool insert = false;
  410. phys_addr_t obase = base;
  411. phys_addr_t end = base + memblock_cap_size(base, &size);
  412. int i, nr_new;
  413. if (!size)
  414. return 0;
  415. /* special case for empty array */
  416. if (type->regions[0].size == 0) {
  417. WARN_ON(type->cnt != 1 || type->total_size);
  418. type->regions[0].base = base;
  419. type->regions[0].size = size;
  420. memblock_set_region_node(&type->regions[0], nid);
  421. type->total_size = size;
  422. return 0;
  423. }
  424. repeat:
  425. /*
  426. * The following is executed twice. Once with %false @insert and
  427. * then with %true. The first counts the number of regions needed
  428. * to accomodate the new area. The second actually inserts them.
  429. */
  430. base = obase;
  431. nr_new = 0;
  432. for (i = 0; i < type->cnt; i++) {
  433. struct memblock_region *rgn = &type->regions[i];
  434. phys_addr_t rbase = rgn->base;
  435. phys_addr_t rend = rbase + rgn->size;
  436. if (rbase >= end)
  437. break;
  438. if (rend <= base)
  439. continue;
  440. /*
  441. * @rgn overlaps. If it separates the lower part of new
  442. * area, insert that portion.
  443. */
  444. if (rbase > base) {
  445. nr_new++;
  446. if (insert)
  447. memblock_insert_region(type, i++, base,
  448. rbase - base, nid);
  449. }
  450. /* area below @rend is dealt with, forget about it */
  451. base = min(rend, end);
  452. }
  453. /* insert the remaining portion */
  454. if (base < end) {
  455. nr_new++;
  456. if (insert)
  457. memblock_insert_region(type, i, base, end - base, nid);
  458. }
  459. /*
  460. * If this was the first round, resize array and repeat for actual
  461. * insertions; otherwise, merge and return.
  462. */
  463. if (!insert) {
  464. while (type->cnt + nr_new > type->max)
  465. if (memblock_double_array(type, obase, size) < 0)
  466. return -ENOMEM;
  467. insert = true;
  468. goto repeat;
  469. } else {
  470. memblock_merge_regions(type);
  471. return 0;
  472. }
  473. }
  474. int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
  475. int nid)
  476. {
  477. return memblock_add_region(&memblock.memory, base, size, nid);
  478. }
  479. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  480. {
  481. return memblock_add_region(&memblock.memory, base, size, MAX_NUMNODES);
  482. }
  483. /**
  484. * memblock_isolate_range - isolate given range into disjoint memblocks
  485. * @type: memblock type to isolate range for
  486. * @base: base of range to isolate
  487. * @size: size of range to isolate
  488. * @start_rgn: out parameter for the start of isolated region
  489. * @end_rgn: out parameter for the end of isolated region
  490. *
  491. * Walk @type and ensure that regions don't cross the boundaries defined by
  492. * [@base,@base+@size). Crossing regions are split at the boundaries,
  493. * which may create at most two more regions. The index of the first
  494. * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  495. *
  496. * RETURNS:
  497. * 0 on success, -errno on failure.
  498. */
  499. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  500. phys_addr_t base, phys_addr_t size,
  501. int *start_rgn, int *end_rgn)
  502. {
  503. phys_addr_t end = base + memblock_cap_size(base, &size);
  504. int i;
  505. *start_rgn = *end_rgn = 0;
  506. if (!size)
  507. return 0;
  508. /* we'll create at most two more regions */
  509. while (type->cnt + 2 > type->max)
  510. if (memblock_double_array(type, base, size) < 0)
  511. return -ENOMEM;
  512. for (i = 0; i < type->cnt; i++) {
  513. struct memblock_region *rgn = &type->regions[i];
  514. phys_addr_t rbase = rgn->base;
  515. phys_addr_t rend = rbase + rgn->size;
  516. if (rbase >= end)
  517. break;
  518. if (rend <= base)
  519. continue;
  520. if (rbase < base) {
  521. /*
  522. * @rgn intersects from below. Split and continue
  523. * to process the next region - the new top half.
  524. */
  525. rgn->base = base;
  526. rgn->size -= base - rbase;
  527. type->total_size -= base - rbase;
  528. memblock_insert_region(type, i, rbase, base - rbase,
  529. memblock_get_region_node(rgn));
  530. } else if (rend > end) {
  531. /*
  532. * @rgn intersects from above. Split and redo the
  533. * current region - the new bottom half.
  534. */
  535. rgn->base = end;
  536. rgn->size -= end - rbase;
  537. type->total_size -= end - rbase;
  538. memblock_insert_region(type, i--, rbase, end - rbase,
  539. memblock_get_region_node(rgn));
  540. } else {
  541. /* @rgn is fully contained, record it */
  542. if (!*end_rgn)
  543. *start_rgn = i;
  544. *end_rgn = i + 1;
  545. }
  546. }
  547. return 0;
  548. }
  549. static int __init_memblock __memblock_remove(struct memblock_type *type,
  550. phys_addr_t base, phys_addr_t size)
  551. {
  552. int start_rgn, end_rgn;
  553. int i, ret;
  554. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  555. if (ret)
  556. return ret;
  557. for (i = end_rgn - 1; i >= start_rgn; i--)
  558. memblock_remove_region(type, i);
  559. return 0;
  560. }
  561. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  562. {
  563. return __memblock_remove(&memblock.memory, base, size);
  564. }
  565. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  566. {
  567. memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  568. (unsigned long long)base,
  569. (unsigned long long)base + size,
  570. (void *)_RET_IP_);
  571. return __memblock_remove(&memblock.reserved, base, size);
  572. }
  573. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  574. {
  575. struct memblock_type *_rgn = &memblock.reserved;
  576. memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
  577. (unsigned long long)base,
  578. (unsigned long long)base + size,
  579. (void *)_RET_IP_);
  580. return memblock_add_region(_rgn, base, size, MAX_NUMNODES);
  581. }
  582. /**
  583. * __next_free_mem_range - next function for for_each_free_mem_range()
  584. * @idx: pointer to u64 loop variable
  585. * @nid: node selector, %MAX_NUMNODES for all nodes
  586. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  587. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  588. * @out_nid: ptr to int for nid of the range, can be %NULL
  589. *
  590. * Find the first free area from *@idx which matches @nid, fill the out
  591. * parameters, and update *@idx for the next iteration. The lower 32bit of
  592. * *@idx contains index into memory region and the upper 32bit indexes the
  593. * areas before each reserved region. For example, if reserved regions
  594. * look like the following,
  595. *
  596. * 0:[0-16), 1:[32-48), 2:[128-130)
  597. *
  598. * The upper 32bit indexes the following regions.
  599. *
  600. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  601. *
  602. * As both region arrays are sorted, the function advances the two indices
  603. * in lockstep and returns each intersection.
  604. */
  605. void __init_memblock __next_free_mem_range(u64 *idx, int nid,
  606. phys_addr_t *out_start,
  607. phys_addr_t *out_end, int *out_nid)
  608. {
  609. struct memblock_type *mem = &memblock.memory;
  610. struct memblock_type *rsv = &memblock.reserved;
  611. int mi = *idx & 0xffffffff;
  612. int ri = *idx >> 32;
  613. for ( ; mi < mem->cnt; mi++) {
  614. struct memblock_region *m = &mem->regions[mi];
  615. phys_addr_t m_start = m->base;
  616. phys_addr_t m_end = m->base + m->size;
  617. /* only memory regions are associated with nodes, check it */
  618. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  619. continue;
  620. /* scan areas before each reservation for intersection */
  621. for ( ; ri < rsv->cnt + 1; ri++) {
  622. struct memblock_region *r = &rsv->regions[ri];
  623. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  624. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  625. /* if ri advanced past mi, break out to advance mi */
  626. if (r_start >= m_end)
  627. break;
  628. /* if the two regions intersect, we're done */
  629. if (m_start < r_end) {
  630. if (out_start)
  631. *out_start = max(m_start, r_start);
  632. if (out_end)
  633. *out_end = min(m_end, r_end);
  634. if (out_nid)
  635. *out_nid = memblock_get_region_node(m);
  636. /*
  637. * The region which ends first is advanced
  638. * for the next iteration.
  639. */
  640. if (m_end <= r_end)
  641. mi++;
  642. else
  643. ri++;
  644. *idx = (u32)mi | (u64)ri << 32;
  645. return;
  646. }
  647. }
  648. }
  649. /* signal end of iteration */
  650. *idx = ULLONG_MAX;
  651. }
  652. /**
  653. * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
  654. * @idx: pointer to u64 loop variable
  655. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  656. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  657. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  658. * @out_nid: ptr to int for nid of the range, can be %NULL
  659. *
  660. * Reverse of __next_free_mem_range().
  661. */
  662. void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
  663. phys_addr_t *out_start,
  664. phys_addr_t *out_end, int *out_nid)
  665. {
  666. struct memblock_type *mem = &memblock.memory;
  667. struct memblock_type *rsv = &memblock.reserved;
  668. int mi = *idx & 0xffffffff;
  669. int ri = *idx >> 32;
  670. if (*idx == (u64)ULLONG_MAX) {
  671. mi = mem->cnt - 1;
  672. ri = rsv->cnt;
  673. }
  674. for ( ; mi >= 0; mi--) {
  675. struct memblock_region *m = &mem->regions[mi];
  676. phys_addr_t m_start = m->base;
  677. phys_addr_t m_end = m->base + m->size;
  678. /* only memory regions are associated with nodes, check it */
  679. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  680. continue;
  681. /* scan areas before each reservation for intersection */
  682. for ( ; ri >= 0; ri--) {
  683. struct memblock_region *r = &rsv->regions[ri];
  684. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  685. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  686. /* if ri advanced past mi, break out to advance mi */
  687. if (r_end <= m_start)
  688. break;
  689. /* if the two regions intersect, we're done */
  690. if (m_end > r_start) {
  691. if (out_start)
  692. *out_start = max(m_start, r_start);
  693. if (out_end)
  694. *out_end = min(m_end, r_end);
  695. if (out_nid)
  696. *out_nid = memblock_get_region_node(m);
  697. if (m_start >= r_start)
  698. mi--;
  699. else
  700. ri--;
  701. *idx = (u32)mi | (u64)ri << 32;
  702. return;
  703. }
  704. }
  705. }
  706. *idx = ULLONG_MAX;
  707. }
  708. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  709. /*
  710. * Common iterator interface used to define for_each_mem_range().
  711. */
  712. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  713. unsigned long *out_start_pfn,
  714. unsigned long *out_end_pfn, int *out_nid)
  715. {
  716. struct memblock_type *type = &memblock.memory;
  717. struct memblock_region *r;
  718. while (++*idx < type->cnt) {
  719. r = &type->regions[*idx];
  720. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  721. continue;
  722. if (nid == MAX_NUMNODES || nid == r->nid)
  723. break;
  724. }
  725. if (*idx >= type->cnt) {
  726. *idx = -1;
  727. return;
  728. }
  729. if (out_start_pfn)
  730. *out_start_pfn = PFN_UP(r->base);
  731. if (out_end_pfn)
  732. *out_end_pfn = PFN_DOWN(r->base + r->size);
  733. if (out_nid)
  734. *out_nid = r->nid;
  735. }
  736. /**
  737. * memblock_set_node - set node ID on memblock regions
  738. * @base: base of area to set node ID for
  739. * @size: size of area to set node ID for
  740. * @nid: node ID to set
  741. *
  742. * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
  743. * Regions which cross the area boundaries are split as necessary.
  744. *
  745. * RETURNS:
  746. * 0 on success, -errno on failure.
  747. */
  748. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  749. int nid)
  750. {
  751. struct memblock_type *type = &memblock.memory;
  752. int start_rgn, end_rgn;
  753. int i, ret;
  754. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  755. if (ret)
  756. return ret;
  757. for (i = start_rgn; i < end_rgn; i++)
  758. memblock_set_region_node(&type->regions[i], nid);
  759. memblock_merge_regions(type);
  760. return 0;
  761. }
  762. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  763. static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
  764. phys_addr_t align, phys_addr_t max_addr,
  765. int nid)
  766. {
  767. phys_addr_t found;
  768. if (WARN_ON(!align))
  769. align = __alignof__(long long);
  770. /* align @size to avoid excessive fragmentation on reserved array */
  771. size = round_up(size, align);
  772. found = memblock_find_in_range_node(0, max_addr, size, align, nid);
  773. if (found && !memblock_reserve(found, size))
  774. return found;
  775. return 0;
  776. }
  777. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  778. {
  779. return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  780. }
  781. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  782. {
  783. return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
  784. }
  785. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  786. {
  787. phys_addr_t alloc;
  788. alloc = __memblock_alloc_base(size, align, max_addr);
  789. if (alloc == 0)
  790. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  791. (unsigned long long) size, (unsigned long long) max_addr);
  792. return alloc;
  793. }
  794. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  795. {
  796. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  797. }
  798. phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  799. {
  800. phys_addr_t res = memblock_alloc_nid(size, align, nid);
  801. if (res)
  802. return res;
  803. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  804. }
  805. /*
  806. * Remaining API functions
  807. */
  808. phys_addr_t __init memblock_phys_mem_size(void)
  809. {
  810. return memblock.memory.total_size;
  811. }
  812. phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
  813. {
  814. unsigned long pages = 0;
  815. struct memblock_region *r;
  816. unsigned long start_pfn, end_pfn;
  817. for_each_memblock(memory, r) {
  818. start_pfn = memblock_region_memory_base_pfn(r);
  819. end_pfn = memblock_region_memory_end_pfn(r);
  820. start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
  821. end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
  822. pages += end_pfn - start_pfn;
  823. }
  824. return (phys_addr_t)pages << PAGE_SHIFT;
  825. }
  826. /* lowest address */
  827. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  828. {
  829. return memblock.memory.regions[0].base;
  830. }
  831. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  832. {
  833. int idx = memblock.memory.cnt - 1;
  834. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  835. }
  836. void __init memblock_enforce_memory_limit(phys_addr_t limit)
  837. {
  838. unsigned long i;
  839. phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
  840. if (!limit)
  841. return;
  842. /* find out max address */
  843. for (i = 0; i < memblock.memory.cnt; i++) {
  844. struct memblock_region *r = &memblock.memory.regions[i];
  845. if (limit <= r->size) {
  846. max_addr = r->base + limit;
  847. break;
  848. }
  849. limit -= r->size;
  850. }
  851. /* truncate both memory and reserved regions */
  852. __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
  853. __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
  854. }
  855. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  856. {
  857. unsigned int left = 0, right = type->cnt;
  858. do {
  859. unsigned int mid = (right + left) / 2;
  860. if (addr < type->regions[mid].base)
  861. right = mid;
  862. else if (addr >= (type->regions[mid].base +
  863. type->regions[mid].size))
  864. left = mid + 1;
  865. else
  866. return mid;
  867. } while (left < right);
  868. return -1;
  869. }
  870. int __init memblock_is_reserved(phys_addr_t addr)
  871. {
  872. return memblock_search(&memblock.reserved, addr) != -1;
  873. }
  874. int __init_memblock memblock_is_memory(phys_addr_t addr)
  875. {
  876. return memblock_search(&memblock.memory, addr) != -1;
  877. }
  878. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  879. int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
  880. unsigned long *start_pfn, unsigned long *end_pfn)
  881. {
  882. struct memblock_type *type = &memblock.memory;
  883. int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT);
  884. if (mid == -1)
  885. return -1;
  886. *start_pfn = type->regions[mid].base >> PAGE_SHIFT;
  887. *end_pfn = (type->regions[mid].base + type->regions[mid].size)
  888. >> PAGE_SHIFT;
  889. return type->regions[mid].nid;
  890. }
  891. #endif
  892. /**
  893. * memblock_is_region_memory - check if a region is a subset of memory
  894. * @base: base of region to check
  895. * @size: size of region to check
  896. *
  897. * Check if the region [@base, @base+@size) is a subset of a memory block.
  898. *
  899. * RETURNS:
  900. * 0 if false, non-zero if true
  901. */
  902. int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  903. {
  904. int idx = memblock_search(&memblock.memory, base);
  905. phys_addr_t end = base + memblock_cap_size(base, &size);
  906. if (idx == -1)
  907. return 0;
  908. return memblock.memory.regions[idx].base <= base &&
  909. (memblock.memory.regions[idx].base +
  910. memblock.memory.regions[idx].size) >= end;
  911. }
  912. /**
  913. * memblock_is_region_reserved - check if a region intersects reserved memory
  914. * @base: base of region to check
  915. * @size: size of region to check
  916. *
  917. * Check if the region [@base, @base+@size) intersects a reserved memory block.
  918. *
  919. * RETURNS:
  920. * 0 if false, non-zero if true
  921. */
  922. int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  923. {
  924. memblock_cap_size(base, &size);
  925. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  926. }
  927. void __init_memblock memblock_trim_memory(phys_addr_t align)
  928. {
  929. int i;
  930. phys_addr_t start, end, orig_start, orig_end;
  931. struct memblock_type *mem = &memblock.memory;
  932. for (i = 0; i < mem->cnt; i++) {
  933. orig_start = mem->regions[i].base;
  934. orig_end = mem->regions[i].base + mem->regions[i].size;
  935. start = round_up(orig_start, align);
  936. end = round_down(orig_end, align);
  937. if (start == orig_start && end == orig_end)
  938. continue;
  939. if (start < end) {
  940. mem->regions[i].base = start;
  941. mem->regions[i].size = end - start;
  942. } else {
  943. memblock_remove_region(mem, i);
  944. i--;
  945. }
  946. }
  947. }
  948. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  949. {
  950. memblock.current_limit = limit;
  951. }
  952. static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
  953. {
  954. unsigned long long base, size;
  955. int i;
  956. pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
  957. for (i = 0; i < type->cnt; i++) {
  958. struct memblock_region *rgn = &type->regions[i];
  959. char nid_buf[32] = "";
  960. base = rgn->base;
  961. size = rgn->size;
  962. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  963. if (memblock_get_region_node(rgn) != MAX_NUMNODES)
  964. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  965. memblock_get_region_node(rgn));
  966. #endif
  967. pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
  968. name, i, base, base + size - 1, size, nid_buf);
  969. }
  970. }
  971. void __init_memblock __memblock_dump_all(void)
  972. {
  973. pr_info("MEMBLOCK configuration:\n");
  974. pr_info(" memory size = %#llx reserved size = %#llx\n",
  975. (unsigned long long)memblock.memory.total_size,
  976. (unsigned long long)memblock.reserved.total_size);
  977. memblock_dump(&memblock.memory, "memory");
  978. memblock_dump(&memblock.reserved, "reserved");
  979. }
  980. void __init memblock_allow_resize(void)
  981. {
  982. memblock_can_resize = 1;
  983. }
  984. static int __init early_memblock(char *p)
  985. {
  986. if (p && strstr(p, "debug"))
  987. memblock_debug = 1;
  988. return 0;
  989. }
  990. early_param("memblock", early_memblock);
  991. #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
  992. static int memblock_debug_show(struct seq_file *m, void *private)
  993. {
  994. struct memblock_type *type = m->private;
  995. struct memblock_region *reg;
  996. int i;
  997. for (i = 0; i < type->cnt; i++) {
  998. reg = &type->regions[i];
  999. seq_printf(m, "%4d: ", i);
  1000. if (sizeof(phys_addr_t) == 4)
  1001. seq_printf(m, "0x%08lx..0x%08lx\n",
  1002. (unsigned long)reg->base,
  1003. (unsigned long)(reg->base + reg->size - 1));
  1004. else
  1005. seq_printf(m, "0x%016llx..0x%016llx\n",
  1006. (unsigned long long)reg->base,
  1007. (unsigned long long)(reg->base + reg->size - 1));
  1008. }
  1009. return 0;
  1010. }
  1011. static int memblock_debug_open(struct inode *inode, struct file *file)
  1012. {
  1013. return single_open(file, memblock_debug_show, inode->i_private);
  1014. }
  1015. static const struct file_operations memblock_debug_fops = {
  1016. .open = memblock_debug_open,
  1017. .read = seq_read,
  1018. .llseek = seq_lseek,
  1019. .release = single_release,
  1020. };
  1021. static int __init memblock_init_debugfs(void)
  1022. {
  1023. struct dentry *root = debugfs_create_dir("memblock", NULL);
  1024. if (!root)
  1025. return -ENXIO;
  1026. debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
  1027. debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
  1028. return 0;
  1029. }
  1030. __initcall(memblock_init_debugfs);
  1031. #endif /* CONFIG_DEBUG_FS */