memblock.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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. #include <linux/io.h>
  23. #include "internal.h"
  24. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  25. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  26. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  27. static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
  28. #endif
  29. struct memblock memblock __initdata_memblock = {
  30. .memory.regions = memblock_memory_init_regions,
  31. .memory.cnt = 1, /* empty dummy entry */
  32. .memory.max = INIT_MEMBLOCK_REGIONS,
  33. .reserved.regions = memblock_reserved_init_regions,
  34. .reserved.cnt = 1, /* empty dummy entry */
  35. .reserved.max = INIT_MEMBLOCK_REGIONS,
  36. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  37. .physmem.regions = memblock_physmem_init_regions,
  38. .physmem.cnt = 1, /* empty dummy entry */
  39. .physmem.max = INIT_PHYSMEM_REGIONS,
  40. #endif
  41. .bottom_up = false,
  42. .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
  43. };
  44. int memblock_debug __initdata_memblock;
  45. #ifdef CONFIG_MOVABLE_NODE
  46. bool movable_node_enabled __initdata_memblock = false;
  47. #endif
  48. static int memblock_can_resize __initdata_memblock;
  49. static int memblock_memory_in_slab __initdata_memblock = 0;
  50. static int memblock_reserved_in_slab __initdata_memblock = 0;
  51. /* inline so we don't get a warning when pr_debug is compiled out */
  52. static __init_memblock const char *
  53. memblock_type_name(struct memblock_type *type)
  54. {
  55. if (type == &memblock.memory)
  56. return "memory";
  57. else if (type == &memblock.reserved)
  58. return "reserved";
  59. else
  60. return "unknown";
  61. }
  62. /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
  63. static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
  64. {
  65. return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
  66. }
  67. /*
  68. * Address comparison utilities
  69. */
  70. static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
  71. phys_addr_t base2, phys_addr_t size2)
  72. {
  73. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  74. }
  75. static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
  76. phys_addr_t base, phys_addr_t size)
  77. {
  78. unsigned long i;
  79. for (i = 0; i < type->cnt; i++) {
  80. phys_addr_t rgnbase = type->regions[i].base;
  81. phys_addr_t rgnsize = type->regions[i].size;
  82. if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
  83. break;
  84. }
  85. return (i < type->cnt) ? i : -1;
  86. }
  87. /*
  88. * __memblock_find_range_bottom_up - find free area utility in bottom-up
  89. * @start: start of candidate range
  90. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  91. * @size: size of free area to find
  92. * @align: alignment of free area to find
  93. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  94. *
  95. * Utility called from memblock_find_in_range_node(), find free area bottom-up.
  96. *
  97. * RETURNS:
  98. * Found address on success, 0 on failure.
  99. */
  100. static phys_addr_t __init_memblock
  101. __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
  102. phys_addr_t size, phys_addr_t align, int nid)
  103. {
  104. phys_addr_t this_start, this_end, cand;
  105. u64 i;
  106. for_each_free_mem_range(i, nid, &this_start, &this_end, NULL) {
  107. this_start = clamp(this_start, start, end);
  108. this_end = clamp(this_end, start, end);
  109. cand = round_up(this_start, align);
  110. if (cand < this_end && this_end - cand >= size)
  111. return cand;
  112. }
  113. return 0;
  114. }
  115. /**
  116. * __memblock_find_range_top_down - find free area utility, in top-down
  117. * @start: start of candidate range
  118. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  119. * @size: size of free area to find
  120. * @align: alignment of free area to find
  121. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  122. *
  123. * Utility called from memblock_find_in_range_node(), find free area top-down.
  124. *
  125. * RETURNS:
  126. * Found address on success, 0 on failure.
  127. */
  128. static phys_addr_t __init_memblock
  129. __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  130. phys_addr_t size, phys_addr_t align, int nid)
  131. {
  132. phys_addr_t this_start, this_end, cand;
  133. u64 i;
  134. for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
  135. this_start = clamp(this_start, start, end);
  136. this_end = clamp(this_end, start, end);
  137. if (this_end < size)
  138. continue;
  139. cand = round_down(this_end - size, align);
  140. if (cand >= this_start)
  141. return cand;
  142. }
  143. return 0;
  144. }
  145. /**
  146. * memblock_find_in_range_node - find free area in given range and node
  147. * @size: size of free area to find
  148. * @align: alignment of free area to find
  149. * @start: start of candidate range
  150. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  151. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  152. *
  153. * Find @size free area aligned to @align in the specified range and node.
  154. *
  155. * When allocation direction is bottom-up, the @start should be greater
  156. * than the end of the kernel image. Otherwise, it will be trimmed. The
  157. * reason is that we want the bottom-up allocation just near the kernel
  158. * image so it is highly likely that the allocated memory and the kernel
  159. * will reside in the same node.
  160. *
  161. * If bottom-up allocation failed, will try to allocate memory top-down.
  162. *
  163. * RETURNS:
  164. * Found address on success, 0 on failure.
  165. */
  166. phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
  167. phys_addr_t align, phys_addr_t start,
  168. phys_addr_t end, int nid)
  169. {
  170. int ret;
  171. phys_addr_t kernel_end;
  172. /* pump up @end */
  173. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  174. end = memblock.current_limit;
  175. /* avoid allocating the first page */
  176. start = max_t(phys_addr_t, start, PAGE_SIZE);
  177. end = max(start, end);
  178. kernel_end = __pa_symbol(_end);
  179. /*
  180. * try bottom-up allocation only when bottom-up mode
  181. * is set and @end is above the kernel image.
  182. */
  183. if (memblock_bottom_up() && end > kernel_end) {
  184. phys_addr_t bottom_up_start;
  185. /* make sure we will allocate above the kernel */
  186. bottom_up_start = max(start, kernel_end);
  187. /* ok, try bottom-up allocation first */
  188. ret = __memblock_find_range_bottom_up(bottom_up_start, end,
  189. size, align, nid);
  190. if (ret)
  191. return ret;
  192. /*
  193. * we always limit bottom-up allocation above the kernel,
  194. * but top-down allocation doesn't have the limit, so
  195. * retrying top-down allocation may succeed when bottom-up
  196. * allocation failed.
  197. *
  198. * bottom-up allocation is expected to be fail very rarely,
  199. * so we use WARN_ONCE() here to see the stack trace if
  200. * fail happens.
  201. */
  202. WARN_ONCE(1, "memblock: bottom-up allocation failed, "
  203. "memory hotunplug may be affected\n");
  204. }
  205. return __memblock_find_range_top_down(start, end, size, align, nid);
  206. }
  207. /**
  208. * memblock_find_in_range - find free area in given range
  209. * @start: start of candidate range
  210. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  211. * @size: size of free area to find
  212. * @align: alignment of free area to find
  213. *
  214. * Find @size free area aligned to @align in the specified range.
  215. *
  216. * RETURNS:
  217. * Found address on success, 0 on failure.
  218. */
  219. phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
  220. phys_addr_t end, phys_addr_t size,
  221. phys_addr_t align)
  222. {
  223. return memblock_find_in_range_node(size, align, start, end,
  224. NUMA_NO_NODE);
  225. }
  226. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  227. {
  228. type->total_size -= type->regions[r].size;
  229. memmove(&type->regions[r], &type->regions[r + 1],
  230. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  231. type->cnt--;
  232. /* Special case for empty arrays */
  233. if (type->cnt == 0) {
  234. WARN_ON(type->total_size != 0);
  235. type->cnt = 1;
  236. type->regions[0].base = 0;
  237. type->regions[0].size = 0;
  238. type->regions[0].flags = 0;
  239. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  240. }
  241. }
  242. #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
  243. phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
  244. phys_addr_t *addr)
  245. {
  246. if (memblock.reserved.regions == memblock_reserved_init_regions)
  247. return 0;
  248. *addr = __pa(memblock.reserved.regions);
  249. return PAGE_ALIGN(sizeof(struct memblock_region) *
  250. memblock.reserved.max);
  251. }
  252. phys_addr_t __init_memblock get_allocated_memblock_memory_regions_info(
  253. phys_addr_t *addr)
  254. {
  255. if (memblock.memory.regions == memblock_memory_init_regions)
  256. return 0;
  257. *addr = __pa(memblock.memory.regions);
  258. return PAGE_ALIGN(sizeof(struct memblock_region) *
  259. memblock.memory.max);
  260. }
  261. #endif
  262. /**
  263. * memblock_double_array - double the size of the memblock regions array
  264. * @type: memblock type of the regions array being doubled
  265. * @new_area_start: starting address of memory range to avoid overlap with
  266. * @new_area_size: size of memory range to avoid overlap with
  267. *
  268. * Double the size of the @type regions array. If memblock is being used to
  269. * allocate memory for a new reserved regions array and there is a previously
  270. * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
  271. * waiting to be reserved, ensure the memory used by the new array does
  272. * not overlap.
  273. *
  274. * RETURNS:
  275. * 0 on success, -1 on failure.
  276. */
  277. static int __init_memblock memblock_double_array(struct memblock_type *type,
  278. phys_addr_t new_area_start,
  279. phys_addr_t new_area_size)
  280. {
  281. struct memblock_region *new_array, *old_array;
  282. phys_addr_t old_alloc_size, new_alloc_size;
  283. phys_addr_t old_size, new_size, addr;
  284. int use_slab = slab_is_available();
  285. int *in_slab;
  286. /* We don't allow resizing until we know about the reserved regions
  287. * of memory that aren't suitable for allocation
  288. */
  289. if (!memblock_can_resize)
  290. return -1;
  291. /* Calculate new doubled size */
  292. old_size = type->max * sizeof(struct memblock_region);
  293. new_size = old_size << 1;
  294. /*
  295. * We need to allocated new one align to PAGE_SIZE,
  296. * so we can free them completely later.
  297. */
  298. old_alloc_size = PAGE_ALIGN(old_size);
  299. new_alloc_size = PAGE_ALIGN(new_size);
  300. /* Retrieve the slab flag */
  301. if (type == &memblock.memory)
  302. in_slab = &memblock_memory_in_slab;
  303. else
  304. in_slab = &memblock_reserved_in_slab;
  305. /* Try to find some space for it.
  306. *
  307. * WARNING: We assume that either slab_is_available() and we use it or
  308. * we use MEMBLOCK for allocations. That means that this is unsafe to
  309. * use when bootmem is currently active (unless bootmem itself is
  310. * implemented on top of MEMBLOCK which isn't the case yet)
  311. *
  312. * This should however not be an issue for now, as we currently only
  313. * call into MEMBLOCK while it's still active, or much later when slab
  314. * is active for memory hotplug operations
  315. */
  316. if (use_slab) {
  317. new_array = kmalloc(new_size, GFP_KERNEL);
  318. addr = new_array ? __pa(new_array) : 0;
  319. } else {
  320. /* only exclude range when trying to double reserved.regions */
  321. if (type != &memblock.reserved)
  322. new_area_start = new_area_size = 0;
  323. addr = memblock_find_in_range(new_area_start + new_area_size,
  324. memblock.current_limit,
  325. new_alloc_size, PAGE_SIZE);
  326. if (!addr && new_area_size)
  327. addr = memblock_find_in_range(0,
  328. min(new_area_start, memblock.current_limit),
  329. new_alloc_size, PAGE_SIZE);
  330. new_array = addr ? __va(addr) : NULL;
  331. }
  332. if (!addr) {
  333. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  334. memblock_type_name(type), type->max, type->max * 2);
  335. return -1;
  336. }
  337. memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
  338. memblock_type_name(type), type->max * 2, (u64)addr,
  339. (u64)addr + new_size - 1);
  340. /*
  341. * Found space, we now need to move the array over before we add the
  342. * reserved region since it may be our reserved array itself that is
  343. * full.
  344. */
  345. memcpy(new_array, type->regions, old_size);
  346. memset(new_array + type->max, 0, old_size);
  347. old_array = type->regions;
  348. type->regions = new_array;
  349. type->max <<= 1;
  350. /* Free old array. We needn't free it if the array is the static one */
  351. if (*in_slab)
  352. kfree(old_array);
  353. else if (old_array != memblock_memory_init_regions &&
  354. old_array != memblock_reserved_init_regions)
  355. memblock_free(__pa(old_array), old_alloc_size);
  356. /*
  357. * Reserve the new array if that comes from the memblock. Otherwise, we
  358. * needn't do it
  359. */
  360. if (!use_slab)
  361. BUG_ON(memblock_reserve(addr, new_alloc_size));
  362. /* Update slab flag */
  363. *in_slab = use_slab;
  364. return 0;
  365. }
  366. /**
  367. * memblock_merge_regions - merge neighboring compatible regions
  368. * @type: memblock type to scan
  369. *
  370. * Scan @type and merge neighboring compatible regions.
  371. */
  372. static void __init_memblock memblock_merge_regions(struct memblock_type *type)
  373. {
  374. int i = 0;
  375. /* cnt never goes below 1 */
  376. while (i < type->cnt - 1) {
  377. struct memblock_region *this = &type->regions[i];
  378. struct memblock_region *next = &type->regions[i + 1];
  379. if (this->base + this->size != next->base ||
  380. memblock_get_region_node(this) !=
  381. memblock_get_region_node(next) ||
  382. this->flags != next->flags) {
  383. BUG_ON(this->base + this->size > next->base);
  384. i++;
  385. continue;
  386. }
  387. this->size += next->size;
  388. /* move forward from next + 1, index of which is i + 2 */
  389. memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
  390. type->cnt--;
  391. }
  392. }
  393. /**
  394. * memblock_insert_region - insert new memblock region
  395. * @type: memblock type to insert into
  396. * @idx: index for the insertion point
  397. * @base: base address of the new region
  398. * @size: size of the new region
  399. * @nid: node id of the new region
  400. * @flags: flags of the new region
  401. *
  402. * Insert new memblock region [@base,@base+@size) into @type at @idx.
  403. * @type must already have extra room to accomodate the new region.
  404. */
  405. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  406. int idx, phys_addr_t base,
  407. phys_addr_t size,
  408. int nid, unsigned long flags)
  409. {
  410. struct memblock_region *rgn = &type->regions[idx];
  411. BUG_ON(type->cnt >= type->max);
  412. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  413. rgn->base = base;
  414. rgn->size = size;
  415. rgn->flags = flags;
  416. memblock_set_region_node(rgn, nid);
  417. type->cnt++;
  418. type->total_size += size;
  419. }
  420. /**
  421. * memblock_add_range - add new memblock region
  422. * @type: memblock type to add new region into
  423. * @base: base address of the new region
  424. * @size: size of the new region
  425. * @nid: nid of the new region
  426. * @flags: flags of the new region
  427. *
  428. * Add new memblock region [@base,@base+@size) into @type. The new region
  429. * is allowed to overlap with existing ones - overlaps don't affect already
  430. * existing regions. @type is guaranteed to be minimal (all neighbouring
  431. * compatible regions are merged) after the addition.
  432. *
  433. * RETURNS:
  434. * 0 on success, -errno on failure.
  435. */
  436. int __init_memblock memblock_add_range(struct memblock_type *type,
  437. phys_addr_t base, phys_addr_t size,
  438. int nid, unsigned long flags)
  439. {
  440. bool insert = false;
  441. phys_addr_t obase = base;
  442. phys_addr_t end = base + memblock_cap_size(base, &size);
  443. int i, nr_new;
  444. if (!size)
  445. return 0;
  446. /* special case for empty array */
  447. if (type->regions[0].size == 0) {
  448. WARN_ON(type->cnt != 1 || type->total_size);
  449. type->regions[0].base = base;
  450. type->regions[0].size = size;
  451. type->regions[0].flags = flags;
  452. memblock_set_region_node(&type->regions[0], nid);
  453. type->total_size = size;
  454. return 0;
  455. }
  456. repeat:
  457. /*
  458. * The following is executed twice. Once with %false @insert and
  459. * then with %true. The first counts the number of regions needed
  460. * to accomodate the new area. The second actually inserts them.
  461. */
  462. base = obase;
  463. nr_new = 0;
  464. for (i = 0; i < type->cnt; i++) {
  465. struct memblock_region *rgn = &type->regions[i];
  466. phys_addr_t rbase = rgn->base;
  467. phys_addr_t rend = rbase + rgn->size;
  468. if (rbase >= end)
  469. break;
  470. if (rend <= base)
  471. continue;
  472. /*
  473. * @rgn overlaps. If it separates the lower part of new
  474. * area, insert that portion.
  475. */
  476. if (rbase > base) {
  477. nr_new++;
  478. if (insert)
  479. memblock_insert_region(type, i++, base,
  480. rbase - base, nid,
  481. flags);
  482. }
  483. /* area below @rend is dealt with, forget about it */
  484. base = min(rend, end);
  485. }
  486. /* insert the remaining portion */
  487. if (base < end) {
  488. nr_new++;
  489. if (insert)
  490. memblock_insert_region(type, i, base, end - base,
  491. nid, flags);
  492. }
  493. /*
  494. * If this was the first round, resize array and repeat for actual
  495. * insertions; otherwise, merge and return.
  496. */
  497. if (!insert) {
  498. while (type->cnt + nr_new > type->max)
  499. if (memblock_double_array(type, obase, size) < 0)
  500. return -ENOMEM;
  501. insert = true;
  502. goto repeat;
  503. } else {
  504. memblock_merge_regions(type);
  505. return 0;
  506. }
  507. }
  508. int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
  509. int nid)
  510. {
  511. return memblock_add_range(&memblock.memory, base, size, nid, 0);
  512. }
  513. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  514. {
  515. return memblock_add_range(&memblock.memory, base, size,
  516. MAX_NUMNODES, 0);
  517. }
  518. /**
  519. * memblock_isolate_range - isolate given range into disjoint memblocks
  520. * @type: memblock type to isolate range for
  521. * @base: base of range to isolate
  522. * @size: size of range to isolate
  523. * @start_rgn: out parameter for the start of isolated region
  524. * @end_rgn: out parameter for the end of isolated region
  525. *
  526. * Walk @type and ensure that regions don't cross the boundaries defined by
  527. * [@base,@base+@size). Crossing regions are split at the boundaries,
  528. * which may create at most two more regions. The index of the first
  529. * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  530. *
  531. * RETURNS:
  532. * 0 on success, -errno on failure.
  533. */
  534. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  535. phys_addr_t base, phys_addr_t size,
  536. int *start_rgn, int *end_rgn)
  537. {
  538. phys_addr_t end = base + memblock_cap_size(base, &size);
  539. int i;
  540. *start_rgn = *end_rgn = 0;
  541. if (!size)
  542. return 0;
  543. /* we'll create at most two more regions */
  544. while (type->cnt + 2 > type->max)
  545. if (memblock_double_array(type, base, size) < 0)
  546. return -ENOMEM;
  547. for (i = 0; i < type->cnt; i++) {
  548. struct memblock_region *rgn = &type->regions[i];
  549. phys_addr_t rbase = rgn->base;
  550. phys_addr_t rend = rbase + rgn->size;
  551. if (rbase >= end)
  552. break;
  553. if (rend <= base)
  554. continue;
  555. if (rbase < base) {
  556. /*
  557. * @rgn intersects from below. Split and continue
  558. * to process the next region - the new top half.
  559. */
  560. rgn->base = base;
  561. rgn->size -= base - rbase;
  562. type->total_size -= base - rbase;
  563. memblock_insert_region(type, i, rbase, base - rbase,
  564. memblock_get_region_node(rgn),
  565. rgn->flags);
  566. } else if (rend > end) {
  567. /*
  568. * @rgn intersects from above. Split and redo the
  569. * current region - the new bottom half.
  570. */
  571. rgn->base = end;
  572. rgn->size -= end - rbase;
  573. type->total_size -= end - rbase;
  574. memblock_insert_region(type, i--, rbase, end - rbase,
  575. memblock_get_region_node(rgn),
  576. rgn->flags);
  577. } else {
  578. /* @rgn is fully contained, record it */
  579. if (!*end_rgn)
  580. *start_rgn = i;
  581. *end_rgn = i + 1;
  582. }
  583. }
  584. return 0;
  585. }
  586. int __init_memblock memblock_remove_range(struct memblock_type *type,
  587. phys_addr_t base, phys_addr_t size)
  588. {
  589. int start_rgn, end_rgn;
  590. int i, ret;
  591. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  592. if (ret)
  593. return ret;
  594. for (i = end_rgn - 1; i >= start_rgn; i--)
  595. memblock_remove_region(type, i);
  596. return 0;
  597. }
  598. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  599. {
  600. return memblock_remove_range(&memblock.memory, base, size);
  601. }
  602. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  603. {
  604. memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  605. (unsigned long long)base,
  606. (unsigned long long)base + size - 1,
  607. (void *)_RET_IP_);
  608. kmemleak_free_part(__va(base), size);
  609. return memblock_remove_range(&memblock.reserved, base, size);
  610. }
  611. static int __init_memblock memblock_reserve_region(phys_addr_t base,
  612. phys_addr_t size,
  613. int nid,
  614. unsigned long flags)
  615. {
  616. struct memblock_type *_rgn = &memblock.reserved;
  617. memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
  618. (unsigned long long)base,
  619. (unsigned long long)base + size - 1,
  620. flags, (void *)_RET_IP_);
  621. return memblock_add_range(_rgn, base, size, nid, flags);
  622. }
  623. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  624. {
  625. return memblock_reserve_region(base, size, MAX_NUMNODES, 0);
  626. }
  627. /**
  628. * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
  629. * @base: the base phys addr of the region
  630. * @size: the size of the region
  631. *
  632. * This function isolates region [@base, @base + @size), and mark it with flag
  633. * MEMBLOCK_HOTPLUG.
  634. *
  635. * Return 0 on succees, -errno on failure.
  636. */
  637. int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
  638. {
  639. struct memblock_type *type = &memblock.memory;
  640. int i, ret, start_rgn, end_rgn;
  641. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  642. if (ret)
  643. return ret;
  644. for (i = start_rgn; i < end_rgn; i++)
  645. memblock_set_region_flags(&type->regions[i], MEMBLOCK_HOTPLUG);
  646. memblock_merge_regions(type);
  647. return 0;
  648. }
  649. /**
  650. * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
  651. * @base: the base phys addr of the region
  652. * @size: the size of the region
  653. *
  654. * This function isolates region [@base, @base + @size), and clear flag
  655. * MEMBLOCK_HOTPLUG for the isolated regions.
  656. *
  657. * Return 0 on succees, -errno on failure.
  658. */
  659. int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
  660. {
  661. struct memblock_type *type = &memblock.memory;
  662. int i, ret, start_rgn, end_rgn;
  663. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  664. if (ret)
  665. return ret;
  666. for (i = start_rgn; i < end_rgn; i++)
  667. memblock_clear_region_flags(&type->regions[i],
  668. MEMBLOCK_HOTPLUG);
  669. memblock_merge_regions(type);
  670. return 0;
  671. }
  672. /**
  673. * __next__mem_range - next function for for_each_free_mem_range() etc.
  674. * @idx: pointer to u64 loop variable
  675. * @nid: node selector, %NUMA_NO_NODE for all nodes
  676. * @type_a: pointer to memblock_type from where the range is taken
  677. * @type_b: pointer to memblock_type which excludes memory from being taken
  678. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  679. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  680. * @out_nid: ptr to int for nid of the range, can be %NULL
  681. *
  682. * Find the first area from *@idx which matches @nid, fill the out
  683. * parameters, and update *@idx for the next iteration. The lower 32bit of
  684. * *@idx contains index into type_a and the upper 32bit indexes the
  685. * areas before each region in type_b. For example, if type_b regions
  686. * look like the following,
  687. *
  688. * 0:[0-16), 1:[32-48), 2:[128-130)
  689. *
  690. * The upper 32bit indexes the following regions.
  691. *
  692. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  693. *
  694. * As both region arrays are sorted, the function advances the two indices
  695. * in lockstep and returns each intersection.
  696. */
  697. void __init_memblock __next_mem_range(u64 *idx, int nid,
  698. struct memblock_type *type_a,
  699. struct memblock_type *type_b,
  700. phys_addr_t *out_start,
  701. phys_addr_t *out_end, int *out_nid)
  702. {
  703. int idx_a = *idx & 0xffffffff;
  704. int idx_b = *idx >> 32;
  705. if (WARN_ONCE(nid == MAX_NUMNODES,
  706. "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
  707. nid = NUMA_NO_NODE;
  708. for (; idx_a < type_a->cnt; idx_a++) {
  709. struct memblock_region *m = &type_a->regions[idx_a];
  710. phys_addr_t m_start = m->base;
  711. phys_addr_t m_end = m->base + m->size;
  712. int m_nid = memblock_get_region_node(m);
  713. /* only memory regions are associated with nodes, check it */
  714. if (nid != NUMA_NO_NODE && nid != m_nid)
  715. continue;
  716. if (!type_b) {
  717. if (out_start)
  718. *out_start = m_start;
  719. if (out_end)
  720. *out_end = m_end;
  721. if (out_nid)
  722. *out_nid = m_nid;
  723. idx_a++;
  724. *idx = (u32)idx_a | (u64)idx_b << 32;
  725. return;
  726. }
  727. /* scan areas before each reservation */
  728. for (; idx_b < type_b->cnt + 1; idx_b++) {
  729. struct memblock_region *r;
  730. phys_addr_t r_start;
  731. phys_addr_t r_end;
  732. r = &type_b->regions[idx_b];
  733. r_start = idx_b ? r[-1].base + r[-1].size : 0;
  734. r_end = idx_b < type_b->cnt ?
  735. r->base : ULLONG_MAX;
  736. /*
  737. * if idx_b advanced past idx_a,
  738. * break out to advance idx_a
  739. */
  740. if (r_start >= m_end)
  741. break;
  742. /* if the two regions intersect, we're done */
  743. if (m_start < r_end) {
  744. if (out_start)
  745. *out_start =
  746. max(m_start, r_start);
  747. if (out_end)
  748. *out_end = min(m_end, r_end);
  749. if (out_nid)
  750. *out_nid = m_nid;
  751. /*
  752. * The region which ends first is
  753. * advanced for the next iteration.
  754. */
  755. if (m_end <= r_end)
  756. idx_a++;
  757. else
  758. idx_b++;
  759. *idx = (u32)idx_a | (u64)idx_b << 32;
  760. return;
  761. }
  762. }
  763. }
  764. /* signal end of iteration */
  765. *idx = ULLONG_MAX;
  766. }
  767. /**
  768. * __next_mem_range_rev - generic next function for for_each_*_range_rev()
  769. *
  770. * Finds the next range from type_a which is not marked as unsuitable
  771. * in type_b.
  772. *
  773. * @idx: pointer to u64 loop variable
  774. * @nid: nid: node selector, %NUMA_NO_NODE for all nodes
  775. * @type_a: pointer to memblock_type from where the range is taken
  776. * @type_b: pointer to memblock_type which excludes memory from being taken
  777. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  778. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  779. * @out_nid: ptr to int for nid of the range, can be %NULL
  780. *
  781. * Reverse of __next_mem_range().
  782. */
  783. void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
  784. struct memblock_type *type_a,
  785. struct memblock_type *type_b,
  786. phys_addr_t *out_start,
  787. phys_addr_t *out_end, int *out_nid)
  788. {
  789. int idx_a = *idx & 0xffffffff;
  790. int idx_b = *idx >> 32;
  791. if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
  792. nid = NUMA_NO_NODE;
  793. if (*idx == (u64)ULLONG_MAX) {
  794. idx_a = type_a->cnt - 1;
  795. idx_b = type_b->cnt;
  796. }
  797. for (; idx_a >= 0; idx_a--) {
  798. struct memblock_region *m = &type_a->regions[idx_a];
  799. phys_addr_t m_start = m->base;
  800. phys_addr_t m_end = m->base + m->size;
  801. int m_nid = memblock_get_region_node(m);
  802. /* only memory regions are associated with nodes, check it */
  803. if (nid != NUMA_NO_NODE && nid != m_nid)
  804. continue;
  805. /* skip hotpluggable memory regions if needed */
  806. if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
  807. continue;
  808. if (!type_b) {
  809. if (out_start)
  810. *out_start = m_start;
  811. if (out_end)
  812. *out_end = m_end;
  813. if (out_nid)
  814. *out_nid = m_nid;
  815. idx_a++;
  816. *idx = (u32)idx_a | (u64)idx_b << 32;
  817. return;
  818. }
  819. /* scan areas before each reservation */
  820. for (; idx_b >= 0; idx_b--) {
  821. struct memblock_region *r;
  822. phys_addr_t r_start;
  823. phys_addr_t r_end;
  824. r = &type_b->regions[idx_b];
  825. r_start = idx_b ? r[-1].base + r[-1].size : 0;
  826. r_end = idx_b < type_b->cnt ?
  827. r->base : ULLONG_MAX;
  828. /*
  829. * if idx_b advanced past idx_a,
  830. * break out to advance idx_a
  831. */
  832. if (r_end <= m_start)
  833. break;
  834. /* if the two regions intersect, we're done */
  835. if (m_end > r_start) {
  836. if (out_start)
  837. *out_start = max(m_start, r_start);
  838. if (out_end)
  839. *out_end = min(m_end, r_end);
  840. if (out_nid)
  841. *out_nid = m_nid;
  842. if (m_start >= r_start)
  843. idx_a--;
  844. else
  845. idx_b--;
  846. *idx = (u32)idx_a | (u64)idx_b << 32;
  847. return;
  848. }
  849. }
  850. }
  851. /* signal end of iteration */
  852. *idx = ULLONG_MAX;
  853. }
  854. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  855. /*
  856. * Common iterator interface used to define for_each_mem_range().
  857. */
  858. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  859. unsigned long *out_start_pfn,
  860. unsigned long *out_end_pfn, int *out_nid)
  861. {
  862. struct memblock_type *type = &memblock.memory;
  863. struct memblock_region *r;
  864. while (++*idx < type->cnt) {
  865. r = &type->regions[*idx];
  866. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  867. continue;
  868. if (nid == MAX_NUMNODES || nid == r->nid)
  869. break;
  870. }
  871. if (*idx >= type->cnt) {
  872. *idx = -1;
  873. return;
  874. }
  875. if (out_start_pfn)
  876. *out_start_pfn = PFN_UP(r->base);
  877. if (out_end_pfn)
  878. *out_end_pfn = PFN_DOWN(r->base + r->size);
  879. if (out_nid)
  880. *out_nid = r->nid;
  881. }
  882. /**
  883. * memblock_set_node - set node ID on memblock regions
  884. * @base: base of area to set node ID for
  885. * @size: size of area to set node ID for
  886. * @type: memblock type to set node ID for
  887. * @nid: node ID to set
  888. *
  889. * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
  890. * Regions which cross the area boundaries are split as necessary.
  891. *
  892. * RETURNS:
  893. * 0 on success, -errno on failure.
  894. */
  895. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  896. struct memblock_type *type, int nid)
  897. {
  898. int start_rgn, end_rgn;
  899. int i, ret;
  900. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  901. if (ret)
  902. return ret;
  903. for (i = start_rgn; i < end_rgn; i++)
  904. memblock_set_region_node(&type->regions[i], nid);
  905. memblock_merge_regions(type);
  906. return 0;
  907. }
  908. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  909. static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
  910. phys_addr_t align, phys_addr_t start,
  911. phys_addr_t end, int nid)
  912. {
  913. phys_addr_t found;
  914. if (!align)
  915. align = SMP_CACHE_BYTES;
  916. found = memblock_find_in_range_node(size, align, start, end, nid);
  917. if (found && !memblock_reserve(found, size)) {
  918. /*
  919. * The min_count is set to 0 so that memblock allocations are
  920. * never reported as leaks.
  921. */
  922. kmemleak_alloc(__va(found), size, 0, 0);
  923. return found;
  924. }
  925. return 0;
  926. }
  927. phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
  928. phys_addr_t start, phys_addr_t end)
  929. {
  930. return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE);
  931. }
  932. static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
  933. phys_addr_t align, phys_addr_t max_addr,
  934. int nid)
  935. {
  936. return memblock_alloc_range_nid(size, align, 0, max_addr, nid);
  937. }
  938. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  939. {
  940. return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  941. }
  942. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  943. {
  944. return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE);
  945. }
  946. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  947. {
  948. phys_addr_t alloc;
  949. alloc = __memblock_alloc_base(size, align, max_addr);
  950. if (alloc == 0)
  951. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  952. (unsigned long long) size, (unsigned long long) max_addr);
  953. return alloc;
  954. }
  955. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  956. {
  957. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  958. }
  959. phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  960. {
  961. phys_addr_t res = memblock_alloc_nid(size, align, nid);
  962. if (res)
  963. return res;
  964. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  965. }
  966. /**
  967. * memblock_virt_alloc_internal - allocate boot memory block
  968. * @size: size of memory block to be allocated in bytes
  969. * @align: alignment of the region and block's size
  970. * @min_addr: the lower bound of the memory region to allocate (phys address)
  971. * @max_addr: the upper bound of the memory region to allocate (phys address)
  972. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  973. *
  974. * The @min_addr limit is dropped if it can not be satisfied and the allocation
  975. * will fall back to memory below @min_addr. Also, allocation may fall back
  976. * to any node in the system if the specified node can not
  977. * hold the requested memory.
  978. *
  979. * The allocation is performed from memory region limited by
  980. * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
  981. *
  982. * The memory block is aligned on SMP_CACHE_BYTES if @align == 0.
  983. *
  984. * The phys address of allocated boot memory block is converted to virtual and
  985. * allocated memory is reset to 0.
  986. *
  987. * In addition, function sets the min_count to 0 using kmemleak_alloc for
  988. * allocated boot memory block, so that it is never reported as leaks.
  989. *
  990. * RETURNS:
  991. * Virtual address of allocated memory block on success, NULL on failure.
  992. */
  993. static void * __init memblock_virt_alloc_internal(
  994. phys_addr_t size, phys_addr_t align,
  995. phys_addr_t min_addr, phys_addr_t max_addr,
  996. int nid)
  997. {
  998. phys_addr_t alloc;
  999. void *ptr;
  1000. if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
  1001. nid = NUMA_NO_NODE;
  1002. /*
  1003. * Detect any accidental use of these APIs after slab is ready, as at
  1004. * this moment memblock may be deinitialized already and its
  1005. * internal data may be destroyed (after execution of free_all_bootmem)
  1006. */
  1007. if (WARN_ON_ONCE(slab_is_available()))
  1008. return kzalloc_node(size, GFP_NOWAIT, nid);
  1009. if (!align)
  1010. align = SMP_CACHE_BYTES;
  1011. if (max_addr > memblock.current_limit)
  1012. max_addr = memblock.current_limit;
  1013. again:
  1014. alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
  1015. nid);
  1016. if (alloc)
  1017. goto done;
  1018. if (nid != NUMA_NO_NODE) {
  1019. alloc = memblock_find_in_range_node(size, align, min_addr,
  1020. max_addr, NUMA_NO_NODE);
  1021. if (alloc)
  1022. goto done;
  1023. }
  1024. if (min_addr) {
  1025. min_addr = 0;
  1026. goto again;
  1027. } else {
  1028. goto error;
  1029. }
  1030. done:
  1031. memblock_reserve(alloc, size);
  1032. ptr = phys_to_virt(alloc);
  1033. memset(ptr, 0, size);
  1034. /*
  1035. * The min_count is set to 0 so that bootmem allocated blocks
  1036. * are never reported as leaks. This is because many of these blocks
  1037. * are only referred via the physical address which is not
  1038. * looked up by kmemleak.
  1039. */
  1040. kmemleak_alloc(ptr, size, 0, 0);
  1041. return ptr;
  1042. error:
  1043. return NULL;
  1044. }
  1045. /**
  1046. * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block
  1047. * @size: size of memory block to be allocated in bytes
  1048. * @align: alignment of the region and block's size
  1049. * @min_addr: the lower bound of the memory region from where the allocation
  1050. * is preferred (phys address)
  1051. * @max_addr: the upper bound of the memory region from where the allocation
  1052. * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
  1053. * allocate only from memory limited by memblock.current_limit value
  1054. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1055. *
  1056. * Public version of _memblock_virt_alloc_try_nid_nopanic() which provides
  1057. * additional debug information (including caller info), if enabled.
  1058. *
  1059. * RETURNS:
  1060. * Virtual address of allocated memory block on success, NULL on failure.
  1061. */
  1062. void * __init memblock_virt_alloc_try_nid_nopanic(
  1063. phys_addr_t size, phys_addr_t align,
  1064. phys_addr_t min_addr, phys_addr_t max_addr,
  1065. int nid)
  1066. {
  1067. memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
  1068. __func__, (u64)size, (u64)align, nid, (u64)min_addr,
  1069. (u64)max_addr, (void *)_RET_IP_);
  1070. return memblock_virt_alloc_internal(size, align, min_addr,
  1071. max_addr, nid);
  1072. }
  1073. /**
  1074. * memblock_virt_alloc_try_nid - allocate boot memory block with panicking
  1075. * @size: size of memory block to be allocated in bytes
  1076. * @align: alignment of the region and block's size
  1077. * @min_addr: the lower bound of the memory region from where the allocation
  1078. * is preferred (phys address)
  1079. * @max_addr: the upper bound of the memory region from where the allocation
  1080. * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
  1081. * allocate only from memory limited by memblock.current_limit value
  1082. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1083. *
  1084. * Public panicking version of _memblock_virt_alloc_try_nid_nopanic()
  1085. * which provides debug information (including caller info), if enabled,
  1086. * and panics if the request can not be satisfied.
  1087. *
  1088. * RETURNS:
  1089. * Virtual address of allocated memory block on success, NULL on failure.
  1090. */
  1091. void * __init memblock_virt_alloc_try_nid(
  1092. phys_addr_t size, phys_addr_t align,
  1093. phys_addr_t min_addr, phys_addr_t max_addr,
  1094. int nid)
  1095. {
  1096. void *ptr;
  1097. memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
  1098. __func__, (u64)size, (u64)align, nid, (u64)min_addr,
  1099. (u64)max_addr, (void *)_RET_IP_);
  1100. ptr = memblock_virt_alloc_internal(size, align,
  1101. min_addr, max_addr, nid);
  1102. if (ptr)
  1103. return ptr;
  1104. panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n",
  1105. __func__, (u64)size, (u64)align, nid, (u64)min_addr,
  1106. (u64)max_addr);
  1107. return NULL;
  1108. }
  1109. /**
  1110. * __memblock_free_early - free boot memory block
  1111. * @base: phys starting address of the boot memory block
  1112. * @size: size of the boot memory block in bytes
  1113. *
  1114. * Free boot memory block previously allocated by memblock_virt_alloc_xx() API.
  1115. * The freeing memory will not be released to the buddy allocator.
  1116. */
  1117. void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
  1118. {
  1119. memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
  1120. __func__, (u64)base, (u64)base + size - 1,
  1121. (void *)_RET_IP_);
  1122. kmemleak_free_part(__va(base), size);
  1123. memblock_remove_range(&memblock.reserved, base, size);
  1124. }
  1125. /*
  1126. * __memblock_free_late - free bootmem block pages directly to buddy allocator
  1127. * @addr: phys starting address of the boot memory block
  1128. * @size: size of the boot memory block in bytes
  1129. *
  1130. * This is only useful when the bootmem allocator has already been torn
  1131. * down, but we are still initializing the system. Pages are released directly
  1132. * to the buddy allocator, no bootmem metadata is updated because it is gone.
  1133. */
  1134. void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
  1135. {
  1136. u64 cursor, end;
  1137. memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
  1138. __func__, (u64)base, (u64)base + size - 1,
  1139. (void *)_RET_IP_);
  1140. kmemleak_free_part(__va(base), size);
  1141. cursor = PFN_UP(base);
  1142. end = PFN_DOWN(base + size);
  1143. for (; cursor < end; cursor++) {
  1144. __free_pages_bootmem(pfn_to_page(cursor), 0);
  1145. totalram_pages++;
  1146. }
  1147. }
  1148. /*
  1149. * Remaining API functions
  1150. */
  1151. phys_addr_t __init memblock_phys_mem_size(void)
  1152. {
  1153. return memblock.memory.total_size;
  1154. }
  1155. phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
  1156. {
  1157. unsigned long pages = 0;
  1158. struct memblock_region *r;
  1159. unsigned long start_pfn, end_pfn;
  1160. for_each_memblock(memory, r) {
  1161. start_pfn = memblock_region_memory_base_pfn(r);
  1162. end_pfn = memblock_region_memory_end_pfn(r);
  1163. start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
  1164. end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
  1165. pages += end_pfn - start_pfn;
  1166. }
  1167. return PFN_PHYS(pages);
  1168. }
  1169. /* lowest address */
  1170. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  1171. {
  1172. return memblock.memory.regions[0].base;
  1173. }
  1174. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  1175. {
  1176. int idx = memblock.memory.cnt - 1;
  1177. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  1178. }
  1179. void __init memblock_enforce_memory_limit(phys_addr_t limit)
  1180. {
  1181. phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
  1182. struct memblock_region *r;
  1183. if (!limit)
  1184. return;
  1185. /* find out max address */
  1186. for_each_memblock(memory, r) {
  1187. if (limit <= r->size) {
  1188. max_addr = r->base + limit;
  1189. break;
  1190. }
  1191. limit -= r->size;
  1192. }
  1193. /* truncate both memory and reserved regions */
  1194. memblock_remove_range(&memblock.memory, max_addr,
  1195. (phys_addr_t)ULLONG_MAX);
  1196. memblock_remove_range(&memblock.reserved, max_addr,
  1197. (phys_addr_t)ULLONG_MAX);
  1198. }
  1199. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  1200. {
  1201. unsigned int left = 0, right = type->cnt;
  1202. do {
  1203. unsigned int mid = (right + left) / 2;
  1204. if (addr < type->regions[mid].base)
  1205. right = mid;
  1206. else if (addr >= (type->regions[mid].base +
  1207. type->regions[mid].size))
  1208. left = mid + 1;
  1209. else
  1210. return mid;
  1211. } while (left < right);
  1212. return -1;
  1213. }
  1214. int __init memblock_is_reserved(phys_addr_t addr)
  1215. {
  1216. return memblock_search(&memblock.reserved, addr) != -1;
  1217. }
  1218. int __init_memblock memblock_is_memory(phys_addr_t addr)
  1219. {
  1220. return memblock_search(&memblock.memory, addr) != -1;
  1221. }
  1222. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  1223. int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
  1224. unsigned long *start_pfn, unsigned long *end_pfn)
  1225. {
  1226. struct memblock_type *type = &memblock.memory;
  1227. int mid = memblock_search(type, PFN_PHYS(pfn));
  1228. if (mid == -1)
  1229. return -1;
  1230. *start_pfn = PFN_DOWN(type->regions[mid].base);
  1231. *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
  1232. return type->regions[mid].nid;
  1233. }
  1234. #endif
  1235. /**
  1236. * memblock_is_region_memory - check if a region is a subset of memory
  1237. * @base: base of region to check
  1238. * @size: size of region to check
  1239. *
  1240. * Check if the region [@base, @base+@size) is a subset of a memory block.
  1241. *
  1242. * RETURNS:
  1243. * 0 if false, non-zero if true
  1244. */
  1245. int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  1246. {
  1247. int idx = memblock_search(&memblock.memory, base);
  1248. phys_addr_t end = base + memblock_cap_size(base, &size);
  1249. if (idx == -1)
  1250. return 0;
  1251. return memblock.memory.regions[idx].base <= base &&
  1252. (memblock.memory.regions[idx].base +
  1253. memblock.memory.regions[idx].size) >= end;
  1254. }
  1255. /**
  1256. * memblock_is_region_reserved - check if a region intersects reserved memory
  1257. * @base: base of region to check
  1258. * @size: size of region to check
  1259. *
  1260. * Check if the region [@base, @base+@size) intersects a reserved memory block.
  1261. *
  1262. * RETURNS:
  1263. * 0 if false, non-zero if true
  1264. */
  1265. int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  1266. {
  1267. memblock_cap_size(base, &size);
  1268. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  1269. }
  1270. void __init_memblock memblock_trim_memory(phys_addr_t align)
  1271. {
  1272. phys_addr_t start, end, orig_start, orig_end;
  1273. struct memblock_region *r;
  1274. for_each_memblock(memory, r) {
  1275. orig_start = r->base;
  1276. orig_end = r->base + r->size;
  1277. start = round_up(orig_start, align);
  1278. end = round_down(orig_end, align);
  1279. if (start == orig_start && end == orig_end)
  1280. continue;
  1281. if (start < end) {
  1282. r->base = start;
  1283. r->size = end - start;
  1284. } else {
  1285. memblock_remove_region(&memblock.memory,
  1286. r - memblock.memory.regions);
  1287. r--;
  1288. }
  1289. }
  1290. }
  1291. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  1292. {
  1293. memblock.current_limit = limit;
  1294. }
  1295. phys_addr_t __init_memblock memblock_get_current_limit(void)
  1296. {
  1297. return memblock.current_limit;
  1298. }
  1299. static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
  1300. {
  1301. unsigned long long base, size;
  1302. unsigned long flags;
  1303. int i;
  1304. pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
  1305. for (i = 0; i < type->cnt; i++) {
  1306. struct memblock_region *rgn = &type->regions[i];
  1307. char nid_buf[32] = "";
  1308. base = rgn->base;
  1309. size = rgn->size;
  1310. flags = rgn->flags;
  1311. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  1312. if (memblock_get_region_node(rgn) != MAX_NUMNODES)
  1313. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  1314. memblock_get_region_node(rgn));
  1315. #endif
  1316. pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
  1317. name, i, base, base + size - 1, size, nid_buf, flags);
  1318. }
  1319. }
  1320. void __init_memblock __memblock_dump_all(void)
  1321. {
  1322. pr_info("MEMBLOCK configuration:\n");
  1323. pr_info(" memory size = %#llx reserved size = %#llx\n",
  1324. (unsigned long long)memblock.memory.total_size,
  1325. (unsigned long long)memblock.reserved.total_size);
  1326. memblock_dump(&memblock.memory, "memory");
  1327. memblock_dump(&memblock.reserved, "reserved");
  1328. }
  1329. void __init memblock_allow_resize(void)
  1330. {
  1331. memblock_can_resize = 1;
  1332. }
  1333. static int __init early_memblock(char *p)
  1334. {
  1335. if (p && strstr(p, "debug"))
  1336. memblock_debug = 1;
  1337. return 0;
  1338. }
  1339. early_param("memblock", early_memblock);
  1340. #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
  1341. static int memblock_debug_show(struct seq_file *m, void *private)
  1342. {
  1343. struct memblock_type *type = m->private;
  1344. struct memblock_region *reg;
  1345. int i;
  1346. for (i = 0; i < type->cnt; i++) {
  1347. reg = &type->regions[i];
  1348. seq_printf(m, "%4d: ", i);
  1349. if (sizeof(phys_addr_t) == 4)
  1350. seq_printf(m, "0x%08lx..0x%08lx\n",
  1351. (unsigned long)reg->base,
  1352. (unsigned long)(reg->base + reg->size - 1));
  1353. else
  1354. seq_printf(m, "0x%016llx..0x%016llx\n",
  1355. (unsigned long long)reg->base,
  1356. (unsigned long long)(reg->base + reg->size - 1));
  1357. }
  1358. return 0;
  1359. }
  1360. static int memblock_debug_open(struct inode *inode, struct file *file)
  1361. {
  1362. return single_open(file, memblock_debug_show, inode->i_private);
  1363. }
  1364. static const struct file_operations memblock_debug_fops = {
  1365. .open = memblock_debug_open,
  1366. .read = seq_read,
  1367. .llseek = seq_lseek,
  1368. .release = single_release,
  1369. };
  1370. static int __init memblock_init_debugfs(void)
  1371. {
  1372. struct dentry *root = debugfs_create_dir("memblock", NULL);
  1373. if (!root)
  1374. return -ENXIO;
  1375. debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
  1376. debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
  1377. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  1378. debugfs_create_file("physmem", S_IRUGO, root, &memblock.physmem, &memblock_debug_fops);
  1379. #endif
  1380. return 0;
  1381. }
  1382. __initcall(memblock_init_debugfs);
  1383. #endif /* CONFIG_DEBUG_FS */