memblock.c 51 KB

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