memblock.c 76 KB

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