memblock.c 50 KB

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