memblock.c 56 KB

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