memblock.c 29 KB

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