memblock.c 31 KB

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