memblock.c 49 KB

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