memblock.c 45 KB

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