memblock.c 29 KB

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