memblock.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Procedures for maintaining information about logical memory blocks.
  4. *
  5. * Peter Bergner, IBM Corp. June 2001.
  6. * Copyright (C) 2001 Peter Bergner.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/bitops.h>
  12. #include <linux/poison.h>
  13. #include <linux/pfn.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/kmemleak.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/memblock.h>
  18. #include <asm/sections.h>
  19. #include <linux/io.h>
  20. #include "internal.h"
  21. #define INIT_MEMBLOCK_REGIONS 128
  22. #define INIT_PHYSMEM_REGIONS 4
  23. #ifndef INIT_MEMBLOCK_RESERVED_REGIONS
  24. # define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
  25. #endif
  26. #ifndef INIT_MEMBLOCK_MEMORY_REGIONS
  27. #define INIT_MEMBLOCK_MEMORY_REGIONS INIT_MEMBLOCK_REGIONS
  28. #endif
  29. /**
  30. * DOC: memblock overview
  31. *
  32. * Memblock is a method of managing memory regions during the early
  33. * boot period when the usual kernel memory allocators are not up and
  34. * running.
  35. *
  36. * Memblock views the system memory as collections of contiguous
  37. * regions. There are several types of these collections:
  38. *
  39. * * ``memory`` - describes the physical memory available to the
  40. * kernel; this may differ from the actual physical memory installed
  41. * in the system, for instance when the memory is restricted with
  42. * ``mem=`` command line parameter
  43. * * ``reserved`` - describes the regions that were allocated
  44. * * ``physmem`` - describes the actual physical memory available during
  45. * boot regardless of the possible restrictions and memory hot(un)plug;
  46. * the ``physmem`` type is only available on some architectures.
  47. *
  48. * Each region is represented by struct memblock_region that
  49. * defines the region extents, its attributes and NUMA node id on NUMA
  50. * systems. Every memory type is described by the struct memblock_type
  51. * which contains an array of memory regions along with
  52. * the allocator metadata. The "memory" and "reserved" types are nicely
  53. * wrapped with struct memblock. This structure is statically
  54. * initialized at build time. The region arrays are initially sized to
  55. * %INIT_MEMBLOCK_MEMORY_REGIONS for "memory" and
  56. * %INIT_MEMBLOCK_RESERVED_REGIONS for "reserved". The region array
  57. * for "physmem" is initially sized to %INIT_PHYSMEM_REGIONS.
  58. * The memblock_allow_resize() enables automatic resizing of the region
  59. * arrays during addition of new regions. This feature should be used
  60. * with care so that memory allocated for the region array will not
  61. * overlap with areas that should be reserved, for example initrd.
  62. *
  63. * The early architecture setup should tell memblock what the physical
  64. * memory layout is by using memblock_add() or memblock_add_node()
  65. * functions. The first function does not assign the region to a NUMA
  66. * node and it is appropriate for UMA systems. Yet, it is possible to
  67. * use it on NUMA systems as well and assign the region to a NUMA node
  68. * later in the setup process using memblock_set_node(). The
  69. * memblock_add_node() performs such an assignment directly.
  70. *
  71. * Once memblock is setup the memory can be allocated using one of the
  72. * API variants:
  73. *
  74. * * memblock_phys_alloc*() - these functions return the **physical**
  75. * address of the allocated memory
  76. * * memblock_alloc*() - these functions return the **virtual** address
  77. * of the allocated memory.
  78. *
  79. * Note, that both API variants use implicit assumptions about allowed
  80. * memory ranges and the fallback methods. Consult the documentation
  81. * of memblock_alloc_internal() and memblock_alloc_range_nid()
  82. * functions for more elaborate description.
  83. *
  84. * As the system boot progresses, the architecture specific mem_init()
  85. * function frees all the memory to the buddy page allocator.
  86. *
  87. * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
  88. * memblock data structures (except "physmem") will be discarded after the
  89. * system initialization completes.
  90. */
  91. #ifndef CONFIG_NUMA
  92. struct pglist_data __refdata contig_page_data;
  93. EXPORT_SYMBOL(contig_page_data);
  94. #endif
  95. unsigned long max_low_pfn;
  96. unsigned long min_low_pfn;
  97. unsigned long max_pfn;
  98. unsigned long long max_possible_pfn;
  99. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock;
  100. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
  101. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  102. static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS];
  103. #endif
  104. struct memblock memblock __initdata_memblock = {
  105. .memory.regions = memblock_memory_init_regions,
  106. .memory.max = INIT_MEMBLOCK_MEMORY_REGIONS,
  107. .memory.name = "memory",
  108. .reserved.regions = memblock_reserved_init_regions,
  109. .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS,
  110. .reserved.name = "reserved",
  111. .bottom_up = false,
  112. .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
  113. };
  114. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  115. struct memblock_type physmem = {
  116. .regions = memblock_physmem_init_regions,
  117. .max = INIT_PHYSMEM_REGIONS,
  118. .name = "physmem",
  119. };
  120. #endif
  121. /*
  122. * keep a pointer to &memblock.memory in the text section to use it in
  123. * __next_mem_range() and its helpers.
  124. * For architectures that do not keep memblock data after init, this
  125. * pointer will be reset to NULL at memblock_discard()
  126. */
  127. static __refdata struct memblock_type *memblock_memory = &memblock.memory;
  128. #define for_each_memblock_type(i, memblock_type, rgn) \
  129. for (i = 0, rgn = &memblock_type->regions[0]; \
  130. i < memblock_type->cnt; \
  131. i++, rgn = &memblock_type->regions[i])
  132. #define memblock_dbg(fmt, ...) \
  133. do { \
  134. if (memblock_debug) \
  135. pr_info(fmt, ##__VA_ARGS__); \
  136. } while (0)
  137. static int memblock_debug __initdata_memblock;
  138. static bool system_has_some_mirror __initdata_memblock;
  139. static int memblock_can_resize __initdata_memblock;
  140. static int memblock_memory_in_slab __initdata_memblock;
  141. static int memblock_reserved_in_slab __initdata_memblock;
  142. bool __init_memblock memblock_has_mirror(void)
  143. {
  144. return system_has_some_mirror;
  145. }
  146. static enum memblock_flags __init_memblock choose_memblock_flags(void)
  147. {
  148. return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
  149. }
  150. /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
  151. static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
  152. {
  153. return *size = min(*size, PHYS_ADDR_MAX - base);
  154. }
  155. /*
  156. * Address comparison utilities
  157. */
  158. unsigned long __init_memblock
  159. memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, phys_addr_t base2,
  160. phys_addr_t size2)
  161. {
  162. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  163. }
  164. bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
  165. phys_addr_t base, phys_addr_t size)
  166. {
  167. unsigned long i;
  168. memblock_cap_size(base, &size);
  169. for (i = 0; i < type->cnt; i++)
  170. if (memblock_addrs_overlap(base, size, type->regions[i].base,
  171. type->regions[i].size))
  172. return true;
  173. return false;
  174. }
  175. /**
  176. * __memblock_find_range_bottom_up - find free area utility in bottom-up
  177. * @start: start of candidate range
  178. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
  179. * %MEMBLOCK_ALLOC_ACCESSIBLE
  180. * @size: size of free area to find
  181. * @align: alignment of free area to find
  182. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  183. * @flags: pick from blocks based on memory attributes
  184. *
  185. * Utility called from memblock_find_in_range_node(), find free area bottom-up.
  186. *
  187. * Return:
  188. * Found address on success, 0 on failure.
  189. */
  190. static phys_addr_t __init_memblock
  191. __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
  192. phys_addr_t size, phys_addr_t align, int nid,
  193. enum memblock_flags flags)
  194. {
  195. phys_addr_t this_start, this_end, cand;
  196. u64 i;
  197. for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
  198. this_start = clamp(this_start, start, end);
  199. this_end = clamp(this_end, start, end);
  200. cand = round_up(this_start, align);
  201. if (cand < this_end && this_end - cand >= size)
  202. return cand;
  203. }
  204. return 0;
  205. }
  206. /**
  207. * __memblock_find_range_top_down - find free area utility, in top-down
  208. * @start: start of candidate range
  209. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
  210. * %MEMBLOCK_ALLOC_ACCESSIBLE
  211. * @size: size of free area to find
  212. * @align: alignment of free area to find
  213. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  214. * @flags: pick from blocks based on memory attributes
  215. *
  216. * Utility called from memblock_find_in_range_node(), find free area top-down.
  217. *
  218. * Return:
  219. * Found address on success, 0 on failure.
  220. */
  221. static phys_addr_t __init_memblock
  222. __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  223. phys_addr_t size, phys_addr_t align, int nid,
  224. enum memblock_flags flags)
  225. {
  226. phys_addr_t this_start, this_end, cand;
  227. u64 i;
  228. for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
  229. NULL) {
  230. this_start = clamp(this_start, start, end);
  231. this_end = clamp(this_end, start, end);
  232. if (this_end < size)
  233. continue;
  234. cand = round_down(this_end - size, align);
  235. if (cand >= this_start)
  236. return cand;
  237. }
  238. return 0;
  239. }
  240. /**
  241. * memblock_find_in_range_node - find free area in given range and node
  242. * @size: size of free area to find
  243. * @align: alignment of free area to find
  244. * @start: start of candidate range
  245. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
  246. * %MEMBLOCK_ALLOC_ACCESSIBLE
  247. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  248. * @flags: pick from blocks based on memory attributes
  249. *
  250. * Find @size free area aligned to @align in the specified range and node.
  251. *
  252. * Return:
  253. * Found address on success, 0 on failure.
  254. */
  255. static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
  256. phys_addr_t align, phys_addr_t start,
  257. phys_addr_t end, int nid,
  258. enum memblock_flags flags)
  259. {
  260. /* pump up @end */
  261. if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
  262. end == MEMBLOCK_ALLOC_NOLEAKTRACE)
  263. end = memblock.current_limit;
  264. /* avoid allocating the first page */
  265. start = max_t(phys_addr_t, start, PAGE_SIZE);
  266. end = max(start, end);
  267. if (memblock_bottom_up())
  268. return __memblock_find_range_bottom_up(start, end, size, align,
  269. nid, flags);
  270. else
  271. return __memblock_find_range_top_down(start, end, size, align,
  272. nid, flags);
  273. }
  274. /**
  275. * memblock_find_in_range - find free area in given range
  276. * @start: start of candidate range
  277. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
  278. * %MEMBLOCK_ALLOC_ACCESSIBLE
  279. * @size: size of free area to find
  280. * @align: alignment of free area to find
  281. *
  282. * Find @size free area aligned to @align in the specified range.
  283. *
  284. * Return:
  285. * Found address on success, 0 on failure.
  286. */
  287. static phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
  288. phys_addr_t end, phys_addr_t size,
  289. phys_addr_t align)
  290. {
  291. phys_addr_t ret;
  292. enum memblock_flags flags = choose_memblock_flags();
  293. again:
  294. ret = memblock_find_in_range_node(size, align, start, end,
  295. NUMA_NO_NODE, flags);
  296. if (!ret && (flags & MEMBLOCK_MIRROR)) {
  297. pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n",
  298. &size);
  299. flags &= ~MEMBLOCK_MIRROR;
  300. goto again;
  301. }
  302. return ret;
  303. }
  304. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  305. {
  306. type->total_size -= type->regions[r].size;
  307. memmove(&type->regions[r], &type->regions[r + 1],
  308. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  309. type->cnt--;
  310. /* Special case for empty arrays */
  311. if (type->cnt == 0) {
  312. WARN_ON(type->total_size != 0);
  313. type->regions[0].base = 0;
  314. type->regions[0].size = 0;
  315. type->regions[0].flags = 0;
  316. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  317. }
  318. }
  319. #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
  320. /**
  321. * memblock_discard - discard memory and reserved arrays if they were allocated
  322. */
  323. void __init memblock_discard(void)
  324. {
  325. phys_addr_t addr, size;
  326. if (memblock.reserved.regions != memblock_reserved_init_regions) {
  327. addr = __pa(memblock.reserved.regions);
  328. size = PAGE_ALIGN(sizeof(struct memblock_region) *
  329. memblock.reserved.max);
  330. if (memblock_reserved_in_slab)
  331. kfree(memblock.reserved.regions);
  332. else
  333. memblock_free_late(addr, size);
  334. }
  335. if (memblock.memory.regions != memblock_memory_init_regions) {
  336. addr = __pa(memblock.memory.regions);
  337. size = PAGE_ALIGN(sizeof(struct memblock_region) *
  338. memblock.memory.max);
  339. if (memblock_memory_in_slab)
  340. kfree(memblock.memory.regions);
  341. else
  342. memblock_free_late(addr, size);
  343. }
  344. memblock_memory = NULL;
  345. }
  346. #endif
  347. /**
  348. * memblock_double_array - double the size of the memblock regions array
  349. * @type: memblock type of the regions array being doubled
  350. * @new_area_start: starting address of memory range to avoid overlap with
  351. * @new_area_size: size of memory range to avoid overlap with
  352. *
  353. * Double the size of the @type regions array. If memblock is being used to
  354. * allocate memory for a new reserved regions array and there is a previously
  355. * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
  356. * waiting to be reserved, ensure the memory used by the new array does
  357. * not overlap.
  358. *
  359. * Return:
  360. * 0 on success, -1 on failure.
  361. */
  362. static int __init_memblock memblock_double_array(struct memblock_type *type,
  363. phys_addr_t new_area_start,
  364. phys_addr_t new_area_size)
  365. {
  366. struct memblock_region *new_array, *old_array;
  367. phys_addr_t old_alloc_size, new_alloc_size;
  368. phys_addr_t old_size, new_size, addr, new_end;
  369. int use_slab = slab_is_available();
  370. int *in_slab;
  371. /* We don't allow resizing until we know about the reserved regions
  372. * of memory that aren't suitable for allocation
  373. */
  374. if (!memblock_can_resize)
  375. panic("memblock: cannot resize %s array\n", type->name);
  376. /* Calculate new doubled size */
  377. old_size = type->max * sizeof(struct memblock_region);
  378. new_size = old_size << 1;
  379. /*
  380. * We need to allocated new one align to PAGE_SIZE,
  381. * so we can free them completely later.
  382. */
  383. old_alloc_size = PAGE_ALIGN(old_size);
  384. new_alloc_size = PAGE_ALIGN(new_size);
  385. /* Retrieve the slab flag */
  386. if (type == &memblock.memory)
  387. in_slab = &memblock_memory_in_slab;
  388. else
  389. in_slab = &memblock_reserved_in_slab;
  390. /* Try to find some space for it */
  391. if (use_slab) {
  392. new_array = kmalloc(new_size, GFP_KERNEL);
  393. addr = new_array ? __pa(new_array) : 0;
  394. } else {
  395. /* only exclude range when trying to double reserved.regions */
  396. if (type != &memblock.reserved)
  397. new_area_start = new_area_size = 0;
  398. addr = memblock_find_in_range(new_area_start + new_area_size,
  399. memblock.current_limit,
  400. new_alloc_size, PAGE_SIZE);
  401. if (!addr && new_area_size)
  402. addr = memblock_find_in_range(0,
  403. min(new_area_start, memblock.current_limit),
  404. new_alloc_size, PAGE_SIZE);
  405. if (addr) {
  406. /* The memory may not have been accepted, yet. */
  407. accept_memory(addr, new_alloc_size);
  408. new_array = __va(addr);
  409. } else {
  410. new_array = NULL;
  411. }
  412. }
  413. if (!addr) {
  414. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  415. type->name, type->max, type->max * 2);
  416. return -1;
  417. }
  418. new_end = addr + new_size - 1;
  419. memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
  420. type->name, type->max * 2, &addr, &new_end);
  421. /*
  422. * Found space, we now need to move the array over before we add the
  423. * reserved region since it may be our reserved array itself that is
  424. * full.
  425. */
  426. memcpy(new_array, type->regions, old_size);
  427. memset(new_array + type->max, 0, old_size);
  428. old_array = type->regions;
  429. type->regions = new_array;
  430. type->max <<= 1;
  431. /* Free old array. We needn't free it if the array is the static one */
  432. if (*in_slab)
  433. kfree(old_array);
  434. else if (old_array != memblock_memory_init_regions &&
  435. old_array != memblock_reserved_init_regions)
  436. memblock_free(old_array, old_alloc_size);
  437. /*
  438. * Reserve the new array if that comes from the memblock. Otherwise, we
  439. * needn't do it
  440. */
  441. if (!use_slab)
  442. BUG_ON(memblock_reserve(addr, new_alloc_size));
  443. /* Update slab flag */
  444. *in_slab = use_slab;
  445. return 0;
  446. }
  447. /**
  448. * memblock_merge_regions - merge neighboring compatible regions
  449. * @type: memblock type to scan
  450. * @start_rgn: start scanning from (@start_rgn - 1)
  451. * @end_rgn: end scanning at (@end_rgn - 1)
  452. * Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn)
  453. */
  454. static void __init_memblock memblock_merge_regions(struct memblock_type *type,
  455. unsigned long start_rgn,
  456. unsigned long end_rgn)
  457. {
  458. int i = 0;
  459. if (start_rgn)
  460. i = start_rgn - 1;
  461. end_rgn = min(end_rgn, type->cnt - 1);
  462. while (i < end_rgn) {
  463. struct memblock_region *this = &type->regions[i];
  464. struct memblock_region *next = &type->regions[i + 1];
  465. if (this->base + this->size != next->base ||
  466. memblock_get_region_node(this) !=
  467. memblock_get_region_node(next) ||
  468. this->flags != next->flags) {
  469. BUG_ON(this->base + this->size > next->base);
  470. i++;
  471. continue;
  472. }
  473. this->size += next->size;
  474. /* move forward from next + 1, index of which is i + 2 */
  475. memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
  476. type->cnt--;
  477. end_rgn--;
  478. }
  479. }
  480. /**
  481. * memblock_insert_region - insert new memblock region
  482. * @type: memblock type to insert into
  483. * @idx: index for the insertion point
  484. * @base: base address of the new region
  485. * @size: size of the new region
  486. * @nid: node id of the new region
  487. * @flags: flags of the new region
  488. *
  489. * Insert new memblock region [@base, @base + @size) into @type at @idx.
  490. * @type must already have extra room to accommodate the new region.
  491. */
  492. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  493. int idx, phys_addr_t base,
  494. phys_addr_t size,
  495. int nid,
  496. enum memblock_flags flags)
  497. {
  498. struct memblock_region *rgn = &type->regions[idx];
  499. BUG_ON(type->cnt >= type->max);
  500. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  501. rgn->base = base;
  502. rgn->size = size;
  503. rgn->flags = flags;
  504. memblock_set_region_node(rgn, nid);
  505. type->cnt++;
  506. type->total_size += size;
  507. }
  508. /**
  509. * memblock_add_range - add new memblock region
  510. * @type: memblock type to add new region into
  511. * @base: base address of the new region
  512. * @size: size of the new region
  513. * @nid: nid of the new region
  514. * @flags: flags of the new region
  515. *
  516. * Add new memblock region [@base, @base + @size) into @type. The new region
  517. * is allowed to overlap with existing ones - overlaps don't affect already
  518. * existing regions. @type is guaranteed to be minimal (all neighbouring
  519. * compatible regions are merged) after the addition.
  520. *
  521. * Return:
  522. * 0 on success, -errno on failure.
  523. */
  524. static int __init_memblock memblock_add_range(struct memblock_type *type,
  525. phys_addr_t base, phys_addr_t size,
  526. int nid, enum memblock_flags flags)
  527. {
  528. bool insert = false;
  529. phys_addr_t obase = base;
  530. phys_addr_t end = base + memblock_cap_size(base, &size);
  531. int idx, nr_new, start_rgn = -1, end_rgn;
  532. struct memblock_region *rgn;
  533. if (!size)
  534. return 0;
  535. /* special case for empty array */
  536. if (type->regions[0].size == 0) {
  537. WARN_ON(type->cnt != 0 || type->total_size);
  538. type->regions[0].base = base;
  539. type->regions[0].size = size;
  540. type->regions[0].flags = flags;
  541. memblock_set_region_node(&type->regions[0], nid);
  542. type->total_size = size;
  543. type->cnt = 1;
  544. return 0;
  545. }
  546. /*
  547. * The worst case is when new range overlaps all existing regions,
  548. * then we'll need type->cnt + 1 empty regions in @type. So if
  549. * type->cnt * 2 + 1 is less than or equal to type->max, we know
  550. * that there is enough empty regions in @type, and we can insert
  551. * regions directly.
  552. */
  553. if (type->cnt * 2 + 1 <= type->max)
  554. insert = true;
  555. repeat:
  556. /*
  557. * The following is executed twice. Once with %false @insert and
  558. * then with %true. The first counts the number of regions needed
  559. * to accommodate the new area. The second actually inserts them.
  560. */
  561. base = obase;
  562. nr_new = 0;
  563. for_each_memblock_type(idx, type, rgn) {
  564. phys_addr_t rbase = rgn->base;
  565. phys_addr_t rend = rbase + rgn->size;
  566. if (rbase >= end)
  567. break;
  568. if (rend <= base)
  569. continue;
  570. /*
  571. * @rgn overlaps. If it separates the lower part of new
  572. * area, insert that portion.
  573. */
  574. if (rbase > base) {
  575. #ifdef CONFIG_NUMA
  576. WARN_ON(nid != memblock_get_region_node(rgn));
  577. #endif
  578. WARN_ON(flags != rgn->flags);
  579. nr_new++;
  580. if (insert) {
  581. if (start_rgn == -1)
  582. start_rgn = idx;
  583. end_rgn = idx + 1;
  584. memblock_insert_region(type, idx++, base,
  585. rbase - base, nid,
  586. flags);
  587. }
  588. }
  589. /* area below @rend is dealt with, forget about it */
  590. base = min(rend, end);
  591. }
  592. /* insert the remaining portion */
  593. if (base < end) {
  594. nr_new++;
  595. if (insert) {
  596. if (start_rgn == -1)
  597. start_rgn = idx;
  598. end_rgn = idx + 1;
  599. memblock_insert_region(type, idx, base, end - base,
  600. nid, flags);
  601. }
  602. }
  603. if (!nr_new)
  604. return 0;
  605. /*
  606. * If this was the first round, resize array and repeat for actual
  607. * insertions; otherwise, merge and return.
  608. */
  609. if (!insert) {
  610. while (type->cnt + nr_new > type->max)
  611. if (memblock_double_array(type, obase, size) < 0)
  612. return -ENOMEM;
  613. insert = true;
  614. goto repeat;
  615. } else {
  616. memblock_merge_regions(type, start_rgn, end_rgn);
  617. return 0;
  618. }
  619. }
  620. /**
  621. * memblock_add_node - add new memblock region within a NUMA node
  622. * @base: base address of the new region
  623. * @size: size of the new region
  624. * @nid: nid of the new region
  625. * @flags: flags of the new region
  626. *
  627. * Add new memblock region [@base, @base + @size) to the "memory"
  628. * type. See memblock_add_range() description for mode details
  629. *
  630. * Return:
  631. * 0 on success, -errno on failure.
  632. */
  633. int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
  634. int nid, enum memblock_flags flags)
  635. {
  636. phys_addr_t end = base + size - 1;
  637. memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__,
  638. &base, &end, nid, flags, (void *)_RET_IP_);
  639. return memblock_add_range(&memblock.memory, base, size, nid, flags);
  640. }
  641. /**
  642. * memblock_add - add new memblock region
  643. * @base: base address of the new region
  644. * @size: size of the new region
  645. *
  646. * Add new memblock region [@base, @base + @size) to the "memory"
  647. * type. See memblock_add_range() description for mode details
  648. *
  649. * Return:
  650. * 0 on success, -errno on failure.
  651. */
  652. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  653. {
  654. phys_addr_t end = base + size - 1;
  655. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  656. &base, &end, (void *)_RET_IP_);
  657. return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
  658. }
  659. /**
  660. * memblock_validate_numa_coverage - check if amount of memory with
  661. * no node ID assigned is less than a threshold
  662. * @threshold_bytes: maximal memory size that can have unassigned node
  663. * ID (in bytes).
  664. *
  665. * A buggy firmware may report memory that does not belong to any node.
  666. * Check if amount of such memory is below @threshold_bytes.
  667. *
  668. * Return: true on success, false on failure.
  669. */
  670. bool __init_memblock memblock_validate_numa_coverage(unsigned long threshold_bytes)
  671. {
  672. unsigned long nr_pages = 0;
  673. unsigned long start_pfn, end_pfn, mem_size_mb;
  674. int nid, i;
  675. /* calculate lose page */
  676. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
  677. if (!numa_valid_node(nid))
  678. nr_pages += end_pfn - start_pfn;
  679. }
  680. if ((nr_pages << PAGE_SHIFT) > threshold_bytes) {
  681. mem_size_mb = memblock_phys_mem_size() >> 20;
  682. pr_err("NUMA: no nodes coverage for %luMB of %luMB RAM\n",
  683. (nr_pages << PAGE_SHIFT) >> 20, mem_size_mb);
  684. return false;
  685. }
  686. return true;
  687. }
  688. /**
  689. * memblock_isolate_range - isolate given range into disjoint memblocks
  690. * @type: memblock type to isolate range for
  691. * @base: base of range to isolate
  692. * @size: size of range to isolate
  693. * @start_rgn: out parameter for the start of isolated region
  694. * @end_rgn: out parameter for the end of isolated region
  695. *
  696. * Walk @type and ensure that regions don't cross the boundaries defined by
  697. * [@base, @base + @size). Crossing regions are split at the boundaries,
  698. * which may create at most two more regions. The index of the first
  699. * region inside the range is returned in *@start_rgn and the index of the
  700. * first region after the range is returned in *@end_rgn.
  701. *
  702. * Return:
  703. * 0 on success, -errno on failure.
  704. */
  705. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  706. phys_addr_t base, phys_addr_t size,
  707. int *start_rgn, int *end_rgn)
  708. {
  709. phys_addr_t end = base + memblock_cap_size(base, &size);
  710. int idx;
  711. struct memblock_region *rgn;
  712. *start_rgn = *end_rgn = 0;
  713. if (!size)
  714. return 0;
  715. /* we'll create at most two more regions */
  716. while (type->cnt + 2 > type->max)
  717. if (memblock_double_array(type, base, size) < 0)
  718. return -ENOMEM;
  719. for_each_memblock_type(idx, type, rgn) {
  720. phys_addr_t rbase = rgn->base;
  721. phys_addr_t rend = rbase + rgn->size;
  722. if (rbase >= end)
  723. break;
  724. if (rend <= base)
  725. continue;
  726. if (rbase < base) {
  727. /*
  728. * @rgn intersects from below. Split and continue
  729. * to process the next region - the new top half.
  730. */
  731. rgn->base = base;
  732. rgn->size -= base - rbase;
  733. type->total_size -= base - rbase;
  734. memblock_insert_region(type, idx, rbase, base - rbase,
  735. memblock_get_region_node(rgn),
  736. rgn->flags);
  737. } else if (rend > end) {
  738. /*
  739. * @rgn intersects from above. Split and redo the
  740. * current region - the new bottom half.
  741. */
  742. rgn->base = end;
  743. rgn->size -= end - rbase;
  744. type->total_size -= end - rbase;
  745. memblock_insert_region(type, idx--, rbase, end - rbase,
  746. memblock_get_region_node(rgn),
  747. rgn->flags);
  748. } else {
  749. /* @rgn is fully contained, record it */
  750. if (!*end_rgn)
  751. *start_rgn = idx;
  752. *end_rgn = idx + 1;
  753. }
  754. }
  755. return 0;
  756. }
  757. static int __init_memblock memblock_remove_range(struct memblock_type *type,
  758. phys_addr_t base, phys_addr_t size)
  759. {
  760. int start_rgn, end_rgn;
  761. int i, ret;
  762. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  763. if (ret)
  764. return ret;
  765. for (i = end_rgn - 1; i >= start_rgn; i--)
  766. memblock_remove_region(type, i);
  767. return 0;
  768. }
  769. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  770. {
  771. phys_addr_t end = base + size - 1;
  772. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  773. &base, &end, (void *)_RET_IP_);
  774. return memblock_remove_range(&memblock.memory, base, size);
  775. }
  776. /**
  777. * memblock_free - free boot memory allocation
  778. * @ptr: starting address of the boot memory allocation
  779. * @size: size of the boot memory block in bytes
  780. *
  781. * Free boot memory block previously allocated by memblock_alloc_xx() API.
  782. * The freeing memory will not be released to the buddy allocator.
  783. */
  784. void __init_memblock memblock_free(void *ptr, size_t size)
  785. {
  786. if (ptr)
  787. memblock_phys_free(__pa(ptr), size);
  788. }
  789. /**
  790. * memblock_phys_free - free boot memory block
  791. * @base: phys starting address of the boot memory block
  792. * @size: size of the boot memory block in bytes
  793. *
  794. * Free boot memory block previously allocated by memblock_phys_alloc_xx() API.
  795. * The freeing memory will not be released to the buddy allocator.
  796. */
  797. int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size)
  798. {
  799. phys_addr_t end = base + size - 1;
  800. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  801. &base, &end, (void *)_RET_IP_);
  802. kmemleak_free_part_phys(base, size);
  803. return memblock_remove_range(&memblock.reserved, base, size);
  804. }
  805. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  806. {
  807. phys_addr_t end = base + size - 1;
  808. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  809. &base, &end, (void *)_RET_IP_);
  810. return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
  811. }
  812. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  813. int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
  814. {
  815. phys_addr_t end = base + size - 1;
  816. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  817. &base, &end, (void *)_RET_IP_);
  818. return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0);
  819. }
  820. #endif
  821. /**
  822. * memblock_setclr_flag - set or clear flag for a memory region
  823. * @type: memblock type to set/clear flag for
  824. * @base: base address of the region
  825. * @size: size of the region
  826. * @set: set or clear the flag
  827. * @flag: the flag to update
  828. *
  829. * This function isolates region [@base, @base + @size), and sets/clears flag
  830. *
  831. * Return: 0 on success, -errno on failure.
  832. */
  833. static int __init_memblock memblock_setclr_flag(struct memblock_type *type,
  834. phys_addr_t base, phys_addr_t size, int set, int flag)
  835. {
  836. int i, ret, start_rgn, end_rgn;
  837. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  838. if (ret)
  839. return ret;
  840. for (i = start_rgn; i < end_rgn; i++) {
  841. struct memblock_region *r = &type->regions[i];
  842. if (set)
  843. r->flags |= flag;
  844. else
  845. r->flags &= ~flag;
  846. }
  847. memblock_merge_regions(type, start_rgn, end_rgn);
  848. return 0;
  849. }
  850. /**
  851. * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
  852. * @base: the base phys addr of the region
  853. * @size: the size of the region
  854. *
  855. * Return: 0 on success, -errno on failure.
  856. */
  857. int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
  858. {
  859. return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_HOTPLUG);
  860. }
  861. /**
  862. * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
  863. * @base: the base phys addr of the region
  864. * @size: the size of the region
  865. *
  866. * Return: 0 on success, -errno on failure.
  867. */
  868. int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
  869. {
  870. return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_HOTPLUG);
  871. }
  872. /**
  873. * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
  874. * @base: the base phys addr of the region
  875. * @size: the size of the region
  876. *
  877. * Return: 0 on success, -errno on failure.
  878. */
  879. int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
  880. {
  881. if (!mirrored_kernelcore)
  882. return 0;
  883. system_has_some_mirror = true;
  884. return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_MIRROR);
  885. }
  886. /**
  887. * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
  888. * @base: the base phys addr of the region
  889. * @size: the size of the region
  890. *
  891. * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the
  892. * direct mapping of the physical memory. These regions will still be
  893. * covered by the memory map. The struct page representing NOMAP memory
  894. * frames in the memory map will be PageReserved()
  895. *
  896. * Note: if the memory being marked %MEMBLOCK_NOMAP was allocated from
  897. * memblock, the caller must inform kmemleak to ignore that memory
  898. *
  899. * Return: 0 on success, -errno on failure.
  900. */
  901. int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
  902. {
  903. return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
  904. }
  905. /**
  906. * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
  907. * @base: the base phys addr of the region
  908. * @size: the size of the region
  909. *
  910. * Return: 0 on success, -errno on failure.
  911. */
  912. int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
  913. {
  914. return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_NOMAP);
  915. }
  916. /**
  917. * memblock_reserved_mark_noinit - Mark a reserved memory region with flag
  918. * MEMBLOCK_RSRV_NOINIT which results in the struct pages not being initialized
  919. * for this region.
  920. * @base: the base phys addr of the region
  921. * @size: the size of the region
  922. *
  923. * struct pages will not be initialized for reserved memory regions marked with
  924. * %MEMBLOCK_RSRV_NOINIT.
  925. *
  926. * Return: 0 on success, -errno on failure.
  927. */
  928. int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size)
  929. {
  930. return memblock_setclr_flag(&memblock.reserved, base, size, 1,
  931. MEMBLOCK_RSRV_NOINIT);
  932. }
  933. static bool should_skip_region(struct memblock_type *type,
  934. struct memblock_region *m,
  935. int nid, int flags)
  936. {
  937. int m_nid = memblock_get_region_node(m);
  938. /* we never skip regions when iterating memblock.reserved or physmem */
  939. if (type != memblock_memory)
  940. return false;
  941. /* only memory regions are associated with nodes, check it */
  942. if (numa_valid_node(nid) && nid != m_nid)
  943. return true;
  944. /* skip hotpluggable memory regions if needed */
  945. if (movable_node_is_enabled() && memblock_is_hotpluggable(m) &&
  946. !(flags & MEMBLOCK_HOTPLUG))
  947. return true;
  948. /* if we want mirror memory skip non-mirror memory regions */
  949. if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
  950. return true;
  951. /* skip nomap memory unless we were asked for it explicitly */
  952. if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
  953. return true;
  954. /* skip driver-managed memory unless we were asked for it explicitly */
  955. if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m))
  956. return true;
  957. return false;
  958. }
  959. /**
  960. * __next_mem_range - next function for for_each_free_mem_range() etc.
  961. * @idx: pointer to u64 loop variable
  962. * @nid: node selector, %NUMA_NO_NODE for all nodes
  963. * @flags: pick from blocks based on memory attributes
  964. * @type_a: pointer to memblock_type from where the range is taken
  965. * @type_b: pointer to memblock_type which excludes memory from being taken
  966. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  967. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  968. * @out_nid: ptr to int for nid of the range, can be %NULL
  969. *
  970. * Find the first area from *@idx which matches @nid, fill the out
  971. * parameters, and update *@idx for the next iteration. The lower 32bit of
  972. * *@idx contains index into type_a and the upper 32bit indexes the
  973. * areas before each region in type_b. For example, if type_b regions
  974. * look like the following,
  975. *
  976. * 0:[0-16), 1:[32-48), 2:[128-130)
  977. *
  978. * The upper 32bit indexes the following regions.
  979. *
  980. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  981. *
  982. * As both region arrays are sorted, the function advances the two indices
  983. * in lockstep and returns each intersection.
  984. */
  985. void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
  986. struct memblock_type *type_a,
  987. struct memblock_type *type_b, phys_addr_t *out_start,
  988. phys_addr_t *out_end, int *out_nid)
  989. {
  990. int idx_a = *idx & 0xffffffff;
  991. int idx_b = *idx >> 32;
  992. for (; idx_a < type_a->cnt; idx_a++) {
  993. struct memblock_region *m = &type_a->regions[idx_a];
  994. phys_addr_t m_start = m->base;
  995. phys_addr_t m_end = m->base + m->size;
  996. int m_nid = memblock_get_region_node(m);
  997. if (should_skip_region(type_a, m, nid, flags))
  998. continue;
  999. if (!type_b) {
  1000. if (out_start)
  1001. *out_start = m_start;
  1002. if (out_end)
  1003. *out_end = m_end;
  1004. if (out_nid)
  1005. *out_nid = m_nid;
  1006. idx_a++;
  1007. *idx = (u32)idx_a | (u64)idx_b << 32;
  1008. return;
  1009. }
  1010. /* scan areas before each reservation */
  1011. for (; idx_b < type_b->cnt + 1; idx_b++) {
  1012. struct memblock_region *r;
  1013. phys_addr_t r_start;
  1014. phys_addr_t r_end;
  1015. r = &type_b->regions[idx_b];
  1016. r_start = idx_b ? r[-1].base + r[-1].size : 0;
  1017. r_end = idx_b < type_b->cnt ?
  1018. r->base : PHYS_ADDR_MAX;
  1019. /*
  1020. * if idx_b advanced past idx_a,
  1021. * break out to advance idx_a
  1022. */
  1023. if (r_start >= m_end)
  1024. break;
  1025. /* if the two regions intersect, we're done */
  1026. if (m_start < r_end) {
  1027. if (out_start)
  1028. *out_start =
  1029. max(m_start, r_start);
  1030. if (out_end)
  1031. *out_end = min(m_end, r_end);
  1032. if (out_nid)
  1033. *out_nid = m_nid;
  1034. /*
  1035. * The region which ends first is
  1036. * advanced for the next iteration.
  1037. */
  1038. if (m_end <= r_end)
  1039. idx_a++;
  1040. else
  1041. idx_b++;
  1042. *idx = (u32)idx_a | (u64)idx_b << 32;
  1043. return;
  1044. }
  1045. }
  1046. }
  1047. /* signal end of iteration */
  1048. *idx = ULLONG_MAX;
  1049. }
  1050. /**
  1051. * __next_mem_range_rev - generic next function for for_each_*_range_rev()
  1052. *
  1053. * @idx: pointer to u64 loop variable
  1054. * @nid: node selector, %NUMA_NO_NODE for all nodes
  1055. * @flags: pick from blocks based on memory attributes
  1056. * @type_a: pointer to memblock_type from where the range is taken
  1057. * @type_b: pointer to memblock_type which excludes memory from being taken
  1058. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  1059. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  1060. * @out_nid: ptr to int for nid of the range, can be %NULL
  1061. *
  1062. * Finds the next range from type_a which is not marked as unsuitable
  1063. * in type_b.
  1064. *
  1065. * Reverse of __next_mem_range().
  1066. */
  1067. void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
  1068. enum memblock_flags flags,
  1069. struct memblock_type *type_a,
  1070. struct memblock_type *type_b,
  1071. phys_addr_t *out_start,
  1072. phys_addr_t *out_end, int *out_nid)
  1073. {
  1074. int idx_a = *idx & 0xffffffff;
  1075. int idx_b = *idx >> 32;
  1076. if (*idx == (u64)ULLONG_MAX) {
  1077. idx_a = type_a->cnt - 1;
  1078. if (type_b != NULL)
  1079. idx_b = type_b->cnt;
  1080. else
  1081. idx_b = 0;
  1082. }
  1083. for (; idx_a >= 0; idx_a--) {
  1084. struct memblock_region *m = &type_a->regions[idx_a];
  1085. phys_addr_t m_start = m->base;
  1086. phys_addr_t m_end = m->base + m->size;
  1087. int m_nid = memblock_get_region_node(m);
  1088. if (should_skip_region(type_a, m, nid, flags))
  1089. continue;
  1090. if (!type_b) {
  1091. if (out_start)
  1092. *out_start = m_start;
  1093. if (out_end)
  1094. *out_end = m_end;
  1095. if (out_nid)
  1096. *out_nid = m_nid;
  1097. idx_a--;
  1098. *idx = (u32)idx_a | (u64)idx_b << 32;
  1099. return;
  1100. }
  1101. /* scan areas before each reservation */
  1102. for (; idx_b >= 0; idx_b--) {
  1103. struct memblock_region *r;
  1104. phys_addr_t r_start;
  1105. phys_addr_t r_end;
  1106. r = &type_b->regions[idx_b];
  1107. r_start = idx_b ? r[-1].base + r[-1].size : 0;
  1108. r_end = idx_b < type_b->cnt ?
  1109. r->base : PHYS_ADDR_MAX;
  1110. /*
  1111. * if idx_b advanced past idx_a,
  1112. * break out to advance idx_a
  1113. */
  1114. if (r_end <= m_start)
  1115. break;
  1116. /* if the two regions intersect, we're done */
  1117. if (m_end > r_start) {
  1118. if (out_start)
  1119. *out_start = max(m_start, r_start);
  1120. if (out_end)
  1121. *out_end = min(m_end, r_end);
  1122. if (out_nid)
  1123. *out_nid = m_nid;
  1124. if (m_start >= r_start)
  1125. idx_a--;
  1126. else
  1127. idx_b--;
  1128. *idx = (u32)idx_a | (u64)idx_b << 32;
  1129. return;
  1130. }
  1131. }
  1132. }
  1133. /* signal end of iteration */
  1134. *idx = ULLONG_MAX;
  1135. }
  1136. /*
  1137. * Common iterator interface used to define for_each_mem_pfn_range().
  1138. */
  1139. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  1140. unsigned long *out_start_pfn,
  1141. unsigned long *out_end_pfn, int *out_nid)
  1142. {
  1143. struct memblock_type *type = &memblock.memory;
  1144. struct memblock_region *r;
  1145. int r_nid;
  1146. while (++*idx < type->cnt) {
  1147. r = &type->regions[*idx];
  1148. r_nid = memblock_get_region_node(r);
  1149. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  1150. continue;
  1151. if (!numa_valid_node(nid) || nid == r_nid)
  1152. break;
  1153. }
  1154. if (*idx >= type->cnt) {
  1155. *idx = -1;
  1156. return;
  1157. }
  1158. if (out_start_pfn)
  1159. *out_start_pfn = PFN_UP(r->base);
  1160. if (out_end_pfn)
  1161. *out_end_pfn = PFN_DOWN(r->base + r->size);
  1162. if (out_nid)
  1163. *out_nid = r_nid;
  1164. }
  1165. /**
  1166. * memblock_set_node - set node ID on memblock regions
  1167. * @base: base of area to set node ID for
  1168. * @size: size of area to set node ID for
  1169. * @type: memblock type to set node ID for
  1170. * @nid: node ID to set
  1171. *
  1172. * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
  1173. * Regions which cross the area boundaries are split as necessary.
  1174. *
  1175. * Return:
  1176. * 0 on success, -errno on failure.
  1177. */
  1178. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  1179. struct memblock_type *type, int nid)
  1180. {
  1181. #ifdef CONFIG_NUMA
  1182. int start_rgn, end_rgn;
  1183. int i, ret;
  1184. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  1185. if (ret)
  1186. return ret;
  1187. for (i = start_rgn; i < end_rgn; i++)
  1188. memblock_set_region_node(&type->regions[i], nid);
  1189. memblock_merge_regions(type, start_rgn, end_rgn);
  1190. #endif
  1191. return 0;
  1192. }
  1193. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  1194. /**
  1195. * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
  1196. *
  1197. * @idx: pointer to u64 loop variable
  1198. * @zone: zone in which all of the memory blocks reside
  1199. * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
  1200. * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
  1201. *
  1202. * This function is meant to be a zone/pfn specific wrapper for the
  1203. * for_each_mem_range type iterators. Specifically they are used in the
  1204. * deferred memory init routines and as such we were duplicating much of
  1205. * this logic throughout the code. So instead of having it in multiple
  1206. * locations it seemed like it would make more sense to centralize this to
  1207. * one new iterator that does everything they need.
  1208. */
  1209. void __init_memblock
  1210. __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
  1211. unsigned long *out_spfn, unsigned long *out_epfn)
  1212. {
  1213. int zone_nid = zone_to_nid(zone);
  1214. phys_addr_t spa, epa;
  1215. __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
  1216. &memblock.memory, &memblock.reserved,
  1217. &spa, &epa, NULL);
  1218. while (*idx != U64_MAX) {
  1219. unsigned long epfn = PFN_DOWN(epa);
  1220. unsigned long spfn = PFN_UP(spa);
  1221. /*
  1222. * Verify the end is at least past the start of the zone and
  1223. * that we have at least one PFN to initialize.
  1224. */
  1225. if (zone->zone_start_pfn < epfn && spfn < epfn) {
  1226. /* if we went too far just stop searching */
  1227. if (zone_end_pfn(zone) <= spfn) {
  1228. *idx = U64_MAX;
  1229. break;
  1230. }
  1231. if (out_spfn)
  1232. *out_spfn = max(zone->zone_start_pfn, spfn);
  1233. if (out_epfn)
  1234. *out_epfn = min(zone_end_pfn(zone), epfn);
  1235. return;
  1236. }
  1237. __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
  1238. &memblock.memory, &memblock.reserved,
  1239. &spa, &epa, NULL);
  1240. }
  1241. /* signal end of iteration */
  1242. if (out_spfn)
  1243. *out_spfn = ULONG_MAX;
  1244. if (out_epfn)
  1245. *out_epfn = 0;
  1246. }
  1247. #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
  1248. /**
  1249. * memblock_alloc_range_nid - allocate boot memory block
  1250. * @size: size of memory block to be allocated in bytes
  1251. * @align: alignment of the region and block's size
  1252. * @start: the lower bound of the memory region to allocate (phys address)
  1253. * @end: the upper bound of the memory region to allocate (phys address)
  1254. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1255. * @exact_nid: control the allocation fall back to other nodes
  1256. *
  1257. * The allocation is performed from memory region limited by
  1258. * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
  1259. *
  1260. * If the specified node can not hold the requested memory and @exact_nid
  1261. * is false, the allocation falls back to any node in the system.
  1262. *
  1263. * For systems with memory mirroring, the allocation is attempted first
  1264. * from the regions with mirroring enabled and then retried from any
  1265. * memory region.
  1266. *
  1267. * In addition, function using kmemleak_alloc_phys for allocated boot
  1268. * memory block, it is never reported as leaks.
  1269. *
  1270. * Return:
  1271. * Physical address of allocated memory block on success, %0 on failure.
  1272. */
  1273. phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
  1274. phys_addr_t align, phys_addr_t start,
  1275. phys_addr_t end, int nid,
  1276. bool exact_nid)
  1277. {
  1278. enum memblock_flags flags = choose_memblock_flags();
  1279. phys_addr_t found;
  1280. /*
  1281. * Detect any accidental use of these APIs after slab is ready, as at
  1282. * this moment memblock may be deinitialized already and its
  1283. * internal data may be destroyed (after execution of memblock_free_all)
  1284. */
  1285. if (WARN_ON_ONCE(slab_is_available())) {
  1286. void *vaddr = kzalloc_node(size, GFP_NOWAIT, nid);
  1287. return vaddr ? virt_to_phys(vaddr) : 0;
  1288. }
  1289. if (!align) {
  1290. /* Can't use WARNs this early in boot on powerpc */
  1291. dump_stack();
  1292. align = SMP_CACHE_BYTES;
  1293. }
  1294. again:
  1295. found = memblock_find_in_range_node(size, align, start, end, nid,
  1296. flags);
  1297. if (found && !memblock_reserve(found, size))
  1298. goto done;
  1299. if (numa_valid_node(nid) && !exact_nid) {
  1300. found = memblock_find_in_range_node(size, align, start,
  1301. end, NUMA_NO_NODE,
  1302. flags);
  1303. if (found && !memblock_reserve(found, size))
  1304. goto done;
  1305. }
  1306. if (flags & MEMBLOCK_MIRROR) {
  1307. flags &= ~MEMBLOCK_MIRROR;
  1308. pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n",
  1309. &size);
  1310. goto again;
  1311. }
  1312. return 0;
  1313. done:
  1314. /*
  1315. * Skip kmemleak for those places like kasan_init() and
  1316. * early_pgtable_alloc() due to high volume.
  1317. */
  1318. if (end != MEMBLOCK_ALLOC_NOLEAKTRACE)
  1319. /*
  1320. * Memblock allocated blocks are never reported as
  1321. * leaks. This is because many of these blocks are
  1322. * only referred via the physical address which is
  1323. * not looked up by kmemleak.
  1324. */
  1325. kmemleak_alloc_phys(found, size, 0);
  1326. /*
  1327. * Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP,
  1328. * require memory to be accepted before it can be used by the
  1329. * guest.
  1330. *
  1331. * Accept the memory of the allocated buffer.
  1332. */
  1333. accept_memory(found, size);
  1334. return found;
  1335. }
  1336. /**
  1337. * memblock_phys_alloc_range - allocate a memory block inside specified range
  1338. * @size: size of memory block to be allocated in bytes
  1339. * @align: alignment of the region and block's size
  1340. * @start: the lower bound of the memory region to allocate (physical address)
  1341. * @end: the upper bound of the memory region to allocate (physical address)
  1342. *
  1343. * Allocate @size bytes in the between @start and @end.
  1344. *
  1345. * Return: physical address of the allocated memory block on success,
  1346. * %0 on failure.
  1347. */
  1348. phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
  1349. phys_addr_t align,
  1350. phys_addr_t start,
  1351. phys_addr_t end)
  1352. {
  1353. memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n",
  1354. __func__, (u64)size, (u64)align, &start, &end,
  1355. (void *)_RET_IP_);
  1356. return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
  1357. false);
  1358. }
  1359. /**
  1360. * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node
  1361. * @size: size of memory block to be allocated in bytes
  1362. * @align: alignment of the region and block's size
  1363. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1364. *
  1365. * Allocates memory block from the specified NUMA node. If the node
  1366. * has no available memory, attempts to allocated from any node in the
  1367. * system.
  1368. *
  1369. * Return: physical address of the allocated memory block on success,
  1370. * %0 on failure.
  1371. */
  1372. phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  1373. {
  1374. return memblock_alloc_range_nid(size, align, 0,
  1375. MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
  1376. }
  1377. /**
  1378. * memblock_alloc_internal - allocate boot memory block
  1379. * @size: size of memory block to be allocated in bytes
  1380. * @align: alignment of the region and block's size
  1381. * @min_addr: the lower bound of the memory region to allocate (phys address)
  1382. * @max_addr: the upper bound of the memory region to allocate (phys address)
  1383. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1384. * @exact_nid: control the allocation fall back to other nodes
  1385. *
  1386. * Allocates memory block using memblock_alloc_range_nid() and
  1387. * converts the returned physical address to virtual.
  1388. *
  1389. * The @min_addr limit is dropped if it can not be satisfied and the allocation
  1390. * will fall back to memory below @min_addr. Other constraints, such
  1391. * as node and mirrored memory will be handled again in
  1392. * memblock_alloc_range_nid().
  1393. *
  1394. * Return:
  1395. * Virtual address of allocated memory block on success, NULL on failure.
  1396. */
  1397. static void * __init memblock_alloc_internal(
  1398. phys_addr_t size, phys_addr_t align,
  1399. phys_addr_t min_addr, phys_addr_t max_addr,
  1400. int nid, bool exact_nid)
  1401. {
  1402. phys_addr_t alloc;
  1403. if (max_addr > memblock.current_limit)
  1404. max_addr = memblock.current_limit;
  1405. alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
  1406. exact_nid);
  1407. /* retry allocation without lower limit */
  1408. if (!alloc && min_addr)
  1409. alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
  1410. exact_nid);
  1411. if (!alloc)
  1412. return NULL;
  1413. return phys_to_virt(alloc);
  1414. }
  1415. /**
  1416. * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
  1417. * without zeroing memory
  1418. * @size: size of memory block to be allocated in bytes
  1419. * @align: alignment of the region and block's size
  1420. * @min_addr: the lower bound of the memory region from where the allocation
  1421. * is preferred (phys address)
  1422. * @max_addr: the upper bound of the memory region from where the allocation
  1423. * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
  1424. * allocate only from memory limited by memblock.current_limit value
  1425. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1426. *
  1427. * Public function, provides additional debug information (including caller
  1428. * info), if enabled. Does not zero allocated memory.
  1429. *
  1430. * Return:
  1431. * Virtual address of allocated memory block on success, NULL on failure.
  1432. */
  1433. void * __init memblock_alloc_exact_nid_raw(
  1434. phys_addr_t size, phys_addr_t align,
  1435. phys_addr_t min_addr, phys_addr_t max_addr,
  1436. int nid)
  1437. {
  1438. memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
  1439. __func__, (u64)size, (u64)align, nid, &min_addr,
  1440. &max_addr, (void *)_RET_IP_);
  1441. return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
  1442. true);
  1443. }
  1444. /**
  1445. * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
  1446. * memory and without panicking
  1447. * @size: size of memory block to be allocated in bytes
  1448. * @align: alignment of the region and block's size
  1449. * @min_addr: the lower bound of the memory region from where the allocation
  1450. * is preferred (phys address)
  1451. * @max_addr: the upper bound of the memory region from where the allocation
  1452. * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
  1453. * allocate only from memory limited by memblock.current_limit value
  1454. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1455. *
  1456. * Public function, provides additional debug information (including caller
  1457. * info), if enabled. Does not zero allocated memory, does not panic if request
  1458. * cannot be satisfied.
  1459. *
  1460. * Return:
  1461. * Virtual address of allocated memory block on success, NULL on failure.
  1462. */
  1463. void * __init memblock_alloc_try_nid_raw(
  1464. phys_addr_t size, phys_addr_t align,
  1465. phys_addr_t min_addr, phys_addr_t max_addr,
  1466. int nid)
  1467. {
  1468. memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
  1469. __func__, (u64)size, (u64)align, nid, &min_addr,
  1470. &max_addr, (void *)_RET_IP_);
  1471. return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
  1472. false);
  1473. }
  1474. /**
  1475. * memblock_alloc_try_nid - allocate boot memory block
  1476. * @size: size of memory block to be allocated in bytes
  1477. * @align: alignment of the region and block's size
  1478. * @min_addr: the lower bound of the memory region from where the allocation
  1479. * is preferred (phys address)
  1480. * @max_addr: the upper bound of the memory region from where the allocation
  1481. * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
  1482. * allocate only from memory limited by memblock.current_limit value
  1483. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1484. *
  1485. * Public function, provides additional debug information (including caller
  1486. * info), if enabled. This function zeroes the allocated memory.
  1487. *
  1488. * Return:
  1489. * Virtual address of allocated memory block on success, NULL on failure.
  1490. */
  1491. void * __init memblock_alloc_try_nid(
  1492. phys_addr_t size, phys_addr_t align,
  1493. phys_addr_t min_addr, phys_addr_t max_addr,
  1494. int nid)
  1495. {
  1496. void *ptr;
  1497. memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
  1498. __func__, (u64)size, (u64)align, nid, &min_addr,
  1499. &max_addr, (void *)_RET_IP_);
  1500. ptr = memblock_alloc_internal(size, align,
  1501. min_addr, max_addr, nid, false);
  1502. if (ptr)
  1503. memset(ptr, 0, size);
  1504. return ptr;
  1505. }
  1506. /**
  1507. * memblock_free_late - free pages directly to buddy allocator
  1508. * @base: phys starting address of the boot memory block
  1509. * @size: size of the boot memory block in bytes
  1510. *
  1511. * This is only useful when the memblock allocator has already been torn
  1512. * down, but we are still initializing the system. Pages are released directly
  1513. * to the buddy allocator.
  1514. */
  1515. void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
  1516. {
  1517. phys_addr_t cursor, end;
  1518. end = base + size - 1;
  1519. memblock_dbg("%s: [%pa-%pa] %pS\n",
  1520. __func__, &base, &end, (void *)_RET_IP_);
  1521. kmemleak_free_part_phys(base, size);
  1522. cursor = PFN_UP(base);
  1523. end = PFN_DOWN(base + size);
  1524. for (; cursor < end; cursor++) {
  1525. memblock_free_pages(pfn_to_page(cursor), cursor, 0);
  1526. totalram_pages_inc();
  1527. }
  1528. }
  1529. /*
  1530. * Remaining API functions
  1531. */
  1532. phys_addr_t __init_memblock memblock_phys_mem_size(void)
  1533. {
  1534. return memblock.memory.total_size;
  1535. }
  1536. phys_addr_t __init_memblock memblock_reserved_size(void)
  1537. {
  1538. return memblock.reserved.total_size;
  1539. }
  1540. /**
  1541. * memblock_estimated_nr_free_pages - return estimated number of free pages
  1542. * from memblock point of view
  1543. *
  1544. * During bootup, subsystems might need a rough estimate of the number of free
  1545. * pages in the whole system, before precise numbers are available from the
  1546. * buddy. Especially with CONFIG_DEFERRED_STRUCT_PAGE_INIT, the numbers
  1547. * obtained from the buddy might be very imprecise during bootup.
  1548. *
  1549. * Return:
  1550. * An estimated number of free pages from memblock point of view.
  1551. */
  1552. unsigned long __init memblock_estimated_nr_free_pages(void)
  1553. {
  1554. return PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size());
  1555. }
  1556. /* lowest address */
  1557. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  1558. {
  1559. return memblock.memory.regions[0].base;
  1560. }
  1561. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  1562. {
  1563. int idx = memblock.memory.cnt - 1;
  1564. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  1565. }
  1566. static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
  1567. {
  1568. phys_addr_t max_addr = PHYS_ADDR_MAX;
  1569. struct memblock_region *r;
  1570. /*
  1571. * translate the memory @limit size into the max address within one of
  1572. * the memory memblock regions, if the @limit exceeds the total size
  1573. * of those regions, max_addr will keep original value PHYS_ADDR_MAX
  1574. */
  1575. for_each_mem_region(r) {
  1576. if (limit <= r->size) {
  1577. max_addr = r->base + limit;
  1578. break;
  1579. }
  1580. limit -= r->size;
  1581. }
  1582. return max_addr;
  1583. }
  1584. void __init memblock_enforce_memory_limit(phys_addr_t limit)
  1585. {
  1586. phys_addr_t max_addr;
  1587. if (!limit)
  1588. return;
  1589. max_addr = __find_max_addr(limit);
  1590. /* @limit exceeds the total size of the memory, do nothing */
  1591. if (max_addr == PHYS_ADDR_MAX)
  1592. return;
  1593. /* truncate both memory and reserved regions */
  1594. memblock_remove_range(&memblock.memory, max_addr,
  1595. PHYS_ADDR_MAX);
  1596. memblock_remove_range(&memblock.reserved, max_addr,
  1597. PHYS_ADDR_MAX);
  1598. }
  1599. void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
  1600. {
  1601. int start_rgn, end_rgn;
  1602. int i, ret;
  1603. if (!size)
  1604. return;
  1605. if (!memblock_memory->total_size) {
  1606. pr_warn("%s: No memory registered yet\n", __func__);
  1607. return;
  1608. }
  1609. ret = memblock_isolate_range(&memblock.memory, base, size,
  1610. &start_rgn, &end_rgn);
  1611. if (ret)
  1612. return;
  1613. /* remove all the MAP regions */
  1614. for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
  1615. if (!memblock_is_nomap(&memblock.memory.regions[i]))
  1616. memblock_remove_region(&memblock.memory, i);
  1617. for (i = start_rgn - 1; i >= 0; i--)
  1618. if (!memblock_is_nomap(&memblock.memory.regions[i]))
  1619. memblock_remove_region(&memblock.memory, i);
  1620. /* truncate the reserved regions */
  1621. memblock_remove_range(&memblock.reserved, 0, base);
  1622. memblock_remove_range(&memblock.reserved,
  1623. base + size, PHYS_ADDR_MAX);
  1624. }
  1625. void __init memblock_mem_limit_remove_map(phys_addr_t limit)
  1626. {
  1627. phys_addr_t max_addr;
  1628. if (!limit)
  1629. return;
  1630. max_addr = __find_max_addr(limit);
  1631. /* @limit exceeds the total size of the memory, do nothing */
  1632. if (max_addr == PHYS_ADDR_MAX)
  1633. return;
  1634. memblock_cap_memory_range(0, max_addr);
  1635. }
  1636. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  1637. {
  1638. unsigned int left = 0, right = type->cnt;
  1639. do {
  1640. unsigned int mid = (right + left) / 2;
  1641. if (addr < type->regions[mid].base)
  1642. right = mid;
  1643. else if (addr >= (type->regions[mid].base +
  1644. type->regions[mid].size))
  1645. left = mid + 1;
  1646. else
  1647. return mid;
  1648. } while (left < right);
  1649. return -1;
  1650. }
  1651. bool __init_memblock memblock_is_reserved(phys_addr_t addr)
  1652. {
  1653. return memblock_search(&memblock.reserved, addr) != -1;
  1654. }
  1655. bool __init_memblock memblock_is_memory(phys_addr_t addr)
  1656. {
  1657. return memblock_search(&memblock.memory, addr) != -1;
  1658. }
  1659. bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
  1660. {
  1661. int i = memblock_search(&memblock.memory, addr);
  1662. if (i == -1)
  1663. return false;
  1664. return !memblock_is_nomap(&memblock.memory.regions[i]);
  1665. }
  1666. int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
  1667. unsigned long *start_pfn, unsigned long *end_pfn)
  1668. {
  1669. struct memblock_type *type = &memblock.memory;
  1670. int mid = memblock_search(type, PFN_PHYS(pfn));
  1671. if (mid == -1)
  1672. return NUMA_NO_NODE;
  1673. *start_pfn = PFN_DOWN(type->regions[mid].base);
  1674. *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
  1675. return memblock_get_region_node(&type->regions[mid]);
  1676. }
  1677. /**
  1678. * memblock_is_region_memory - check if a region is a subset of memory
  1679. * @base: base of region to check
  1680. * @size: size of region to check
  1681. *
  1682. * Check if the region [@base, @base + @size) is a subset of a memory block.
  1683. *
  1684. * Return:
  1685. * 0 if false, non-zero if true
  1686. */
  1687. bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  1688. {
  1689. int idx = memblock_search(&memblock.memory, base);
  1690. phys_addr_t end = base + memblock_cap_size(base, &size);
  1691. if (idx == -1)
  1692. return false;
  1693. return (memblock.memory.regions[idx].base +
  1694. memblock.memory.regions[idx].size) >= end;
  1695. }
  1696. /**
  1697. * memblock_is_region_reserved - check if a region intersects reserved memory
  1698. * @base: base of region to check
  1699. * @size: size of region to check
  1700. *
  1701. * Check if the region [@base, @base + @size) intersects a reserved
  1702. * memory block.
  1703. *
  1704. * Return:
  1705. * True if they intersect, false if not.
  1706. */
  1707. bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  1708. {
  1709. return memblock_overlaps_region(&memblock.reserved, base, size);
  1710. }
  1711. void __init_memblock memblock_trim_memory(phys_addr_t align)
  1712. {
  1713. phys_addr_t start, end, orig_start, orig_end;
  1714. struct memblock_region *r;
  1715. for_each_mem_region(r) {
  1716. orig_start = r->base;
  1717. orig_end = r->base + r->size;
  1718. start = round_up(orig_start, align);
  1719. end = round_down(orig_end, align);
  1720. if (start == orig_start && end == orig_end)
  1721. continue;
  1722. if (start < end) {
  1723. r->base = start;
  1724. r->size = end - start;
  1725. } else {
  1726. memblock_remove_region(&memblock.memory,
  1727. r - memblock.memory.regions);
  1728. r--;
  1729. }
  1730. }
  1731. }
  1732. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  1733. {
  1734. memblock.current_limit = limit;
  1735. }
  1736. phys_addr_t __init_memblock memblock_get_current_limit(void)
  1737. {
  1738. return memblock.current_limit;
  1739. }
  1740. static void __init_memblock memblock_dump(struct memblock_type *type)
  1741. {
  1742. phys_addr_t base, end, size;
  1743. enum memblock_flags flags;
  1744. int idx;
  1745. struct memblock_region *rgn;
  1746. pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
  1747. for_each_memblock_type(idx, type, rgn) {
  1748. char nid_buf[32] = "";
  1749. base = rgn->base;
  1750. size = rgn->size;
  1751. end = base + size - 1;
  1752. flags = rgn->flags;
  1753. #ifdef CONFIG_NUMA
  1754. if (numa_valid_node(memblock_get_region_node(rgn)))
  1755. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  1756. memblock_get_region_node(rgn));
  1757. #endif
  1758. pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
  1759. type->name, idx, &base, &end, &size, nid_buf, flags);
  1760. }
  1761. }
  1762. static void __init_memblock __memblock_dump_all(void)
  1763. {
  1764. pr_info("MEMBLOCK configuration:\n");
  1765. pr_info(" memory size = %pa reserved size = %pa\n",
  1766. &memblock.memory.total_size,
  1767. &memblock.reserved.total_size);
  1768. memblock_dump(&memblock.memory);
  1769. memblock_dump(&memblock.reserved);
  1770. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  1771. memblock_dump(&physmem);
  1772. #endif
  1773. }
  1774. void __init_memblock memblock_dump_all(void)
  1775. {
  1776. if (memblock_debug)
  1777. __memblock_dump_all();
  1778. }
  1779. void __init memblock_allow_resize(void)
  1780. {
  1781. memblock_can_resize = 1;
  1782. }
  1783. static int __init early_memblock(char *p)
  1784. {
  1785. if (p && strstr(p, "debug"))
  1786. memblock_debug = 1;
  1787. return 0;
  1788. }
  1789. early_param("memblock", early_memblock);
  1790. static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn)
  1791. {
  1792. struct page *start_pg, *end_pg;
  1793. phys_addr_t pg, pgend;
  1794. /*
  1795. * Convert start_pfn/end_pfn to a struct page pointer.
  1796. */
  1797. start_pg = pfn_to_page(start_pfn - 1) + 1;
  1798. end_pg = pfn_to_page(end_pfn - 1) + 1;
  1799. /*
  1800. * Convert to physical addresses, and round start upwards and end
  1801. * downwards.
  1802. */
  1803. pg = PAGE_ALIGN(__pa(start_pg));
  1804. pgend = PAGE_ALIGN_DOWN(__pa(end_pg));
  1805. /*
  1806. * If there are free pages between these, free the section of the
  1807. * memmap array.
  1808. */
  1809. if (pg < pgend)
  1810. memblock_phys_free(pg, pgend - pg);
  1811. }
  1812. /*
  1813. * The mem_map array can get very big. Free the unused area of the memory map.
  1814. */
  1815. static void __init free_unused_memmap(void)
  1816. {
  1817. unsigned long start, end, prev_end = 0;
  1818. int i;
  1819. if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) ||
  1820. IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
  1821. return;
  1822. /*
  1823. * This relies on each bank being in address order.
  1824. * The banks are sorted previously in bootmem_init().
  1825. */
  1826. for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
  1827. #ifdef CONFIG_SPARSEMEM
  1828. /*
  1829. * Take care not to free memmap entries that don't exist
  1830. * due to SPARSEMEM sections which aren't present.
  1831. */
  1832. start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
  1833. #endif
  1834. /*
  1835. * Align down here since many operations in VM subsystem
  1836. * presume that there are no holes in the memory map inside
  1837. * a pageblock
  1838. */
  1839. start = pageblock_start_pfn(start);
  1840. /*
  1841. * If we had a previous bank, and there is a space
  1842. * between the current bank and the previous, free it.
  1843. */
  1844. if (prev_end && prev_end < start)
  1845. free_memmap(prev_end, start);
  1846. /*
  1847. * Align up here since many operations in VM subsystem
  1848. * presume that there are no holes in the memory map inside
  1849. * a pageblock
  1850. */
  1851. prev_end = pageblock_align(end);
  1852. }
  1853. #ifdef CONFIG_SPARSEMEM
  1854. if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) {
  1855. prev_end = pageblock_align(end);
  1856. free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
  1857. }
  1858. #endif
  1859. }
  1860. static void __init __free_pages_memory(unsigned long start, unsigned long end)
  1861. {
  1862. int order;
  1863. while (start < end) {
  1864. /*
  1865. * Free the pages in the largest chunks alignment allows.
  1866. *
  1867. * __ffs() behaviour is undefined for 0. start == 0 is
  1868. * MAX_PAGE_ORDER-aligned, set order to MAX_PAGE_ORDER for
  1869. * the case.
  1870. */
  1871. if (start)
  1872. order = min_t(int, MAX_PAGE_ORDER, __ffs(start));
  1873. else
  1874. order = MAX_PAGE_ORDER;
  1875. while (start + (1UL << order) > end)
  1876. order--;
  1877. memblock_free_pages(pfn_to_page(start), start, order);
  1878. start += (1UL << order);
  1879. }
  1880. }
  1881. static unsigned long __init __free_memory_core(phys_addr_t start,
  1882. phys_addr_t end)
  1883. {
  1884. unsigned long start_pfn = PFN_UP(start);
  1885. unsigned long end_pfn = min_t(unsigned long,
  1886. PFN_DOWN(end), max_low_pfn);
  1887. if (start_pfn >= end_pfn)
  1888. return 0;
  1889. __free_pages_memory(start_pfn, end_pfn);
  1890. return end_pfn - start_pfn;
  1891. }
  1892. static void __init memmap_init_reserved_pages(void)
  1893. {
  1894. struct memblock_region *region;
  1895. phys_addr_t start, end;
  1896. int nid;
  1897. unsigned long max_reserved;
  1898. /*
  1899. * set nid on all reserved pages and also treat struct
  1900. * pages for the NOMAP regions as PageReserved
  1901. */
  1902. repeat:
  1903. max_reserved = memblock.reserved.max;
  1904. for_each_mem_region(region) {
  1905. nid = memblock_get_region_node(region);
  1906. start = region->base;
  1907. end = start + region->size;
  1908. if (memblock_is_nomap(region))
  1909. reserve_bootmem_region(start, end, nid);
  1910. memblock_set_node(start, region->size, &memblock.reserved, nid);
  1911. }
  1912. /*
  1913. * 'max' is changed means memblock.reserved has been doubled its
  1914. * array, which may result a new reserved region before current
  1915. * 'start'. Now we should repeat the procedure to set its node id.
  1916. */
  1917. if (max_reserved != memblock.reserved.max)
  1918. goto repeat;
  1919. /*
  1920. * initialize struct pages for reserved regions that don't have
  1921. * the MEMBLOCK_RSRV_NOINIT flag set
  1922. */
  1923. for_each_reserved_mem_region(region) {
  1924. if (!memblock_is_reserved_noinit(region)) {
  1925. nid = memblock_get_region_node(region);
  1926. start = region->base;
  1927. end = start + region->size;
  1928. if (!numa_valid_node(nid))
  1929. nid = early_pfn_to_nid(PFN_DOWN(start));
  1930. reserve_bootmem_region(start, end, nid);
  1931. }
  1932. }
  1933. }
  1934. static unsigned long __init free_low_memory_core_early(void)
  1935. {
  1936. unsigned long count = 0;
  1937. phys_addr_t start, end;
  1938. u64 i;
  1939. memblock_clear_hotplug(0, -1);
  1940. memmap_init_reserved_pages();
  1941. /*
  1942. * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
  1943. * because in some case like Node0 doesn't have RAM installed
  1944. * low ram will be on Node1
  1945. */
  1946. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
  1947. NULL)
  1948. count += __free_memory_core(start, end);
  1949. return count;
  1950. }
  1951. static int reset_managed_pages_done __initdata;
  1952. static void __init reset_node_managed_pages(pg_data_t *pgdat)
  1953. {
  1954. struct zone *z;
  1955. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  1956. atomic_long_set(&z->managed_pages, 0);
  1957. }
  1958. void __init reset_all_zones_managed_pages(void)
  1959. {
  1960. struct pglist_data *pgdat;
  1961. if (reset_managed_pages_done)
  1962. return;
  1963. for_each_online_pgdat(pgdat)
  1964. reset_node_managed_pages(pgdat);
  1965. reset_managed_pages_done = 1;
  1966. }
  1967. /**
  1968. * memblock_free_all - release free pages to the buddy allocator
  1969. */
  1970. void __init memblock_free_all(void)
  1971. {
  1972. unsigned long pages;
  1973. free_unused_memmap();
  1974. reset_all_zones_managed_pages();
  1975. pages = free_low_memory_core_early();
  1976. totalram_pages_add(pages);
  1977. }
  1978. /* Keep a table to reserve named memory */
  1979. #define RESERVE_MEM_MAX_ENTRIES 8
  1980. #define RESERVE_MEM_NAME_SIZE 16
  1981. struct reserve_mem_table {
  1982. char name[RESERVE_MEM_NAME_SIZE];
  1983. phys_addr_t start;
  1984. phys_addr_t size;
  1985. };
  1986. static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES];
  1987. static int reserved_mem_count;
  1988. /* Add wildcard region with a lookup name */
  1989. static void __init reserved_mem_add(phys_addr_t start, phys_addr_t size,
  1990. const char *name)
  1991. {
  1992. struct reserve_mem_table *map;
  1993. map = &reserved_mem_table[reserved_mem_count++];
  1994. map->start = start;
  1995. map->size = size;
  1996. strscpy(map->name, name);
  1997. }
  1998. /**
  1999. * reserve_mem_find_by_name - Find reserved memory region with a given name
  2000. * @name: The name that is attached to a reserved memory region
  2001. * @start: If found, holds the start address
  2002. * @size: If found, holds the size of the address.
  2003. *
  2004. * @start and @size are only updated if @name is found.
  2005. *
  2006. * Returns: 1 if found or 0 if not found.
  2007. */
  2008. int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t *size)
  2009. {
  2010. struct reserve_mem_table *map;
  2011. int i;
  2012. for (i = 0; i < reserved_mem_count; i++) {
  2013. map = &reserved_mem_table[i];
  2014. if (!map->size)
  2015. continue;
  2016. if (strcmp(name, map->name) == 0) {
  2017. *start = map->start;
  2018. *size = map->size;
  2019. return 1;
  2020. }
  2021. }
  2022. return 0;
  2023. }
  2024. EXPORT_SYMBOL_GPL(reserve_mem_find_by_name);
  2025. /*
  2026. * Parse reserve_mem=nn:align:name
  2027. */
  2028. static int __init reserve_mem(char *p)
  2029. {
  2030. phys_addr_t start, size, align, tmp;
  2031. char *name;
  2032. char *oldp;
  2033. int len;
  2034. if (!p)
  2035. return -EINVAL;
  2036. /* Check if there's room for more reserved memory */
  2037. if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
  2038. return -EBUSY;
  2039. oldp = p;
  2040. size = memparse(p, &p);
  2041. if (!size || p == oldp)
  2042. return -EINVAL;
  2043. if (*p != ':')
  2044. return -EINVAL;
  2045. align = memparse(p+1, &p);
  2046. if (*p != ':')
  2047. return -EINVAL;
  2048. /*
  2049. * memblock_phys_alloc() doesn't like a zero size align,
  2050. * but it is OK for this command to have it.
  2051. */
  2052. if (align < SMP_CACHE_BYTES)
  2053. align = SMP_CACHE_BYTES;
  2054. name = p + 1;
  2055. len = strlen(name);
  2056. /* name needs to have length but not too big */
  2057. if (!len || len >= RESERVE_MEM_NAME_SIZE)
  2058. return -EINVAL;
  2059. /* Make sure that name has text */
  2060. for (p = name; *p; p++) {
  2061. if (!isspace(*p))
  2062. break;
  2063. }
  2064. if (!*p)
  2065. return -EINVAL;
  2066. /* Make sure the name is not already used */
  2067. if (reserve_mem_find_by_name(name, &start, &tmp))
  2068. return -EBUSY;
  2069. start = memblock_phys_alloc(size, align);
  2070. if (!start)
  2071. return -ENOMEM;
  2072. reserved_mem_add(start, size, name);
  2073. return 1;
  2074. }
  2075. __setup("reserve_mem=", reserve_mem);
  2076. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
  2077. static const char * const flagname[] = {
  2078. [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
  2079. [ilog2(MEMBLOCK_MIRROR)] = "MIRROR",
  2080. [ilog2(MEMBLOCK_NOMAP)] = "NOMAP",
  2081. [ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG",
  2082. [ilog2(MEMBLOCK_RSRV_NOINIT)] = "RSV_NIT",
  2083. };
  2084. static int memblock_debug_show(struct seq_file *m, void *private)
  2085. {
  2086. struct memblock_type *type = m->private;
  2087. struct memblock_region *reg;
  2088. int i, j, nid;
  2089. unsigned int count = ARRAY_SIZE(flagname);
  2090. phys_addr_t end;
  2091. for (i = 0; i < type->cnt; i++) {
  2092. reg = &type->regions[i];
  2093. end = reg->base + reg->size - 1;
  2094. nid = memblock_get_region_node(reg);
  2095. seq_printf(m, "%4d: ", i);
  2096. seq_printf(m, "%pa..%pa ", &reg->base, &end);
  2097. if (numa_valid_node(nid))
  2098. seq_printf(m, "%4d ", nid);
  2099. else
  2100. seq_printf(m, "%4c ", 'x');
  2101. if (reg->flags) {
  2102. for (j = 0; j < count; j++) {
  2103. if (reg->flags & (1U << j)) {
  2104. seq_printf(m, "%s\n", flagname[j]);
  2105. break;
  2106. }
  2107. }
  2108. if (j == count)
  2109. seq_printf(m, "%s\n", "UNKNOWN");
  2110. } else {
  2111. seq_printf(m, "%s\n", "NONE");
  2112. }
  2113. }
  2114. return 0;
  2115. }
  2116. DEFINE_SHOW_ATTRIBUTE(memblock_debug);
  2117. static int __init memblock_init_debugfs(void)
  2118. {
  2119. struct dentry *root = debugfs_create_dir("memblock", NULL);
  2120. debugfs_create_file("memory", 0444, root,
  2121. &memblock.memory, &memblock_debug_fops);
  2122. debugfs_create_file("reserved", 0444, root,
  2123. &memblock.reserved, &memblock_debug_fops);
  2124. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  2125. debugfs_create_file("physmem", 0444, root, &physmem,
  2126. &memblock_debug_fops);
  2127. #endif
  2128. return 0;
  2129. }
  2130. __initcall(memblock_init_debugfs);
  2131. #endif /* CONFIG_DEBUG_FS */