memblock.c 51 KB

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