memblock.c 28 KB

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