memblock.c 27 KB

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