memblock.c 34 KB

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