memblock.c 53 KB

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