memblock.c 44 KB

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