memblock.c 28 KB

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