memblock.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  22. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  23. struct memblock memblock __initdata_memblock = {
  24. .memory.regions = memblock_memory_init_regions,
  25. .memory.cnt = 1, /* empty dummy entry */
  26. .memory.max = INIT_MEMBLOCK_REGIONS,
  27. .reserved.regions = memblock_reserved_init_regions,
  28. .reserved.cnt = 1, /* empty dummy entry */
  29. .reserved.max = INIT_MEMBLOCK_REGIONS,
  30. .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
  31. };
  32. int memblock_debug __initdata_memblock;
  33. static int memblock_can_resize __initdata_memblock;
  34. static int memblock_memory_in_slab __initdata_memblock = 0;
  35. static int memblock_reserved_in_slab __initdata_memblock = 0;
  36. /* inline so we don't get a warning when pr_debug is compiled out */
  37. static inline const char *memblock_type_name(struct memblock_type *type)
  38. {
  39. if (type == &memblock.memory)
  40. return "memory";
  41. else if (type == &memblock.reserved)
  42. return "reserved";
  43. else
  44. return "unknown";
  45. }
  46. /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
  47. static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
  48. {
  49. return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
  50. }
  51. /*
  52. * Address comparison utilities
  53. */
  54. static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
  55. phys_addr_t base2, phys_addr_t size2)
  56. {
  57. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  58. }
  59. static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
  60. phys_addr_t base, phys_addr_t size)
  61. {
  62. unsigned long i;
  63. for (i = 0; i < type->cnt; i++) {
  64. phys_addr_t rgnbase = type->regions[i].base;
  65. phys_addr_t rgnsize = type->regions[i].size;
  66. if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
  67. break;
  68. }
  69. return (i < type->cnt) ? i : -1;
  70. }
  71. /**
  72. * memblock_find_in_range_node - find free area in given range and node
  73. * @start: start of candidate range
  74. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  75. * @size: size of free area to find
  76. * @align: alignment of free area to find
  77. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  78. *
  79. * Find @size free area aligned to @align in the specified range and node.
  80. *
  81. * RETURNS:
  82. * Found address on success, %0 on failure.
  83. */
  84. phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,
  85. phys_addr_t end, phys_addr_t size,
  86. phys_addr_t align, int nid)
  87. {
  88. phys_addr_t this_start, this_end, cand;
  89. u64 i;
  90. /* pump up @end */
  91. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  92. end = memblock.current_limit;
  93. /* avoid allocating the first page */
  94. start = max_t(phys_addr_t, start, PAGE_SIZE);
  95. end = max(start, end);
  96. for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
  97. this_start = clamp(this_start, start, end);
  98. this_end = clamp(this_end, start, end);
  99. if (this_end < size)
  100. continue;
  101. cand = round_down(this_end - size, align);
  102. if (cand >= this_start)
  103. return cand;
  104. }
  105. return 0;
  106. }
  107. /**
  108. * memblock_find_in_range - find free area in given range
  109. * @start: start of candidate range
  110. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  111. * @size: size of free area to find
  112. * @align: alignment of free area to find
  113. *
  114. * Find @size free area aligned to @align in the specified range.
  115. *
  116. * RETURNS:
  117. * Found address on success, %0 on failure.
  118. */
  119. phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
  120. phys_addr_t end, phys_addr_t size,
  121. phys_addr_t align)
  122. {
  123. return memblock_find_in_range_node(start, end, size, align,
  124. MAX_NUMNODES);
  125. }
  126. /*
  127. * Free memblock.reserved.regions
  128. */
  129. int __init_memblock memblock_free_reserved_regions(void)
  130. {
  131. if (memblock.reserved.regions == memblock_reserved_init_regions)
  132. return 0;
  133. return memblock_free(__pa(memblock.reserved.regions),
  134. sizeof(struct memblock_region) * memblock.reserved.max);
  135. }
  136. /*
  137. * Reserve memblock.reserved.regions
  138. */
  139. int __init_memblock memblock_reserve_reserved_regions(void)
  140. {
  141. if (memblock.reserved.regions == memblock_reserved_init_regions)
  142. return 0;
  143. return memblock_reserve(__pa(memblock.reserved.regions),
  144. sizeof(struct memblock_region) * memblock.reserved.max);
  145. }
  146. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  147. {
  148. type->total_size -= type->regions[r].size;
  149. memmove(&type->regions[r], &type->regions[r + 1],
  150. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  151. type->cnt--;
  152. /* Special case for empty arrays */
  153. if (type->cnt == 0) {
  154. WARN_ON(type->total_size != 0);
  155. type->cnt = 1;
  156. type->regions[0].base = 0;
  157. type->regions[0].size = 0;
  158. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  159. }
  160. }
  161. static int __init_memblock memblock_double_array(struct memblock_type *type)
  162. {
  163. struct memblock_region *new_array, *old_array;
  164. phys_addr_t old_size, new_size, addr;
  165. int use_slab = slab_is_available();
  166. int *in_slab;
  167. /* We don't allow resizing until we know about the reserved regions
  168. * of memory that aren't suitable for allocation
  169. */
  170. if (!memblock_can_resize)
  171. return -1;
  172. /* Calculate new doubled size */
  173. old_size = type->max * sizeof(struct memblock_region);
  174. new_size = old_size << 1;
  175. /* Retrieve the slab flag */
  176. if (type == &memblock.memory)
  177. in_slab = &memblock_memory_in_slab;
  178. else
  179. in_slab = &memblock_reserved_in_slab;
  180. /* Try to find some space for it.
  181. *
  182. * WARNING: We assume that either slab_is_available() and we use it or
  183. * we use MEMBLOCK for allocations. That means that this is unsafe to use
  184. * when bootmem is currently active (unless bootmem itself is implemented
  185. * on top of MEMBLOCK which isn't the case yet)
  186. *
  187. * This should however not be an issue for now, as we currently only
  188. * call into MEMBLOCK while it's still active, or much later when slab is
  189. * active for memory hotplug operations
  190. */
  191. if (use_slab) {
  192. new_array = kmalloc(new_size, GFP_KERNEL);
  193. addr = new_array ? __pa(new_array) : 0;
  194. } else {
  195. addr = memblock_find_in_range(0, MEMBLOCK_ALLOC_ACCESSIBLE, new_size, sizeof(phys_addr_t));
  196. new_array = addr ? __va(addr) : 0;
  197. }
  198. if (!addr) {
  199. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  200. memblock_type_name(type), type->max, type->max * 2);
  201. return -1;
  202. }
  203. memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]",
  204. memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1);
  205. /* Found space, we now need to move the array over before
  206. * we add the reserved region since it may be our reserved
  207. * array itself that is full.
  208. */
  209. memcpy(new_array, type->regions, old_size);
  210. memset(new_array + type->max, 0, old_size);
  211. old_array = type->regions;
  212. type->regions = new_array;
  213. type->max <<= 1;
  214. /* Free old array. We needn't free it if the array is the
  215. * static one
  216. */
  217. if (*in_slab)
  218. kfree(old_array);
  219. else if (old_array != memblock_memory_init_regions &&
  220. old_array != memblock_reserved_init_regions)
  221. memblock_free(__pa(old_array), old_size);
  222. /* Reserve the new array if that comes from the memblock.
  223. * Otherwise, we needn't do it
  224. */
  225. if (!use_slab)
  226. BUG_ON(memblock_reserve(addr, new_size));
  227. /* Update slab flag */
  228. *in_slab = use_slab;
  229. return 0;
  230. }
  231. /**
  232. * memblock_merge_regions - merge neighboring compatible regions
  233. * @type: memblock type to scan
  234. *
  235. * Scan @type and merge neighboring compatible regions.
  236. */
  237. static void __init_memblock memblock_merge_regions(struct memblock_type *type)
  238. {
  239. int i = 0;
  240. /* cnt never goes below 1 */
  241. while (i < type->cnt - 1) {
  242. struct memblock_region *this = &type->regions[i];
  243. struct memblock_region *next = &type->regions[i + 1];
  244. if (this->base + this->size != next->base ||
  245. memblock_get_region_node(this) !=
  246. memblock_get_region_node(next)) {
  247. BUG_ON(this->base + this->size > next->base);
  248. i++;
  249. continue;
  250. }
  251. this->size += next->size;
  252. memmove(next, next + 1, (type->cnt - (i + 1)) * sizeof(*next));
  253. type->cnt--;
  254. }
  255. }
  256. /**
  257. * memblock_insert_region - insert new memblock region
  258. * @type: memblock type to insert into
  259. * @idx: index for the insertion point
  260. * @base: base address of the new region
  261. * @size: size of the new region
  262. *
  263. * Insert new memblock region [@base,@base+@size) into @type at @idx.
  264. * @type must already have extra room to accomodate the new region.
  265. */
  266. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  267. int idx, phys_addr_t base,
  268. phys_addr_t size, int nid)
  269. {
  270. struct memblock_region *rgn = &type->regions[idx];
  271. BUG_ON(type->cnt >= type->max);
  272. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  273. rgn->base = base;
  274. rgn->size = size;
  275. memblock_set_region_node(rgn, nid);
  276. type->cnt++;
  277. type->total_size += size;
  278. }
  279. /**
  280. * memblock_add_region - add new memblock region
  281. * @type: memblock type to add new region into
  282. * @base: base address of the new region
  283. * @size: size of the new region
  284. * @nid: nid of the new region
  285. *
  286. * Add new memblock region [@base,@base+@size) into @type. The new region
  287. * is allowed to overlap with existing ones - overlaps don't affect already
  288. * existing regions. @type is guaranteed to be minimal (all neighbouring
  289. * compatible regions are merged) after the addition.
  290. *
  291. * RETURNS:
  292. * 0 on success, -errno on failure.
  293. */
  294. static int __init_memblock memblock_add_region(struct memblock_type *type,
  295. phys_addr_t base, phys_addr_t size, int nid)
  296. {
  297. bool insert = false;
  298. phys_addr_t obase = base;
  299. phys_addr_t end = base + memblock_cap_size(base, &size);
  300. int i, nr_new;
  301. if (!size)
  302. return 0;
  303. /* special case for empty array */
  304. if (type->regions[0].size == 0) {
  305. WARN_ON(type->cnt != 1 || type->total_size);
  306. type->regions[0].base = base;
  307. type->regions[0].size = size;
  308. memblock_set_region_node(&type->regions[0], nid);
  309. type->total_size = size;
  310. return 0;
  311. }
  312. repeat:
  313. /*
  314. * The following is executed twice. Once with %false @insert and
  315. * then with %true. The first counts the number of regions needed
  316. * to accomodate the new area. The second actually inserts them.
  317. */
  318. base = obase;
  319. nr_new = 0;
  320. for (i = 0; i < type->cnt; i++) {
  321. struct memblock_region *rgn = &type->regions[i];
  322. phys_addr_t rbase = rgn->base;
  323. phys_addr_t rend = rbase + rgn->size;
  324. if (rbase >= end)
  325. break;
  326. if (rend <= base)
  327. continue;
  328. /*
  329. * @rgn overlaps. If it separates the lower part of new
  330. * area, insert that portion.
  331. */
  332. if (rbase > base) {
  333. nr_new++;
  334. if (insert)
  335. memblock_insert_region(type, i++, base,
  336. rbase - base, nid);
  337. }
  338. /* area below @rend is dealt with, forget about it */
  339. base = min(rend, end);
  340. }
  341. /* insert the remaining portion */
  342. if (base < end) {
  343. nr_new++;
  344. if (insert)
  345. memblock_insert_region(type, i, base, end - base, nid);
  346. }
  347. /*
  348. * If this was the first round, resize array and repeat for actual
  349. * insertions; otherwise, merge and return.
  350. */
  351. if (!insert) {
  352. while (type->cnt + nr_new > type->max)
  353. if (memblock_double_array(type) < 0)
  354. return -ENOMEM;
  355. insert = true;
  356. goto repeat;
  357. } else {
  358. memblock_merge_regions(type);
  359. return 0;
  360. }
  361. }
  362. int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
  363. int nid)
  364. {
  365. return memblock_add_region(&memblock.memory, base, size, nid);
  366. }
  367. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  368. {
  369. return memblock_add_region(&memblock.memory, base, size, MAX_NUMNODES);
  370. }
  371. /**
  372. * memblock_isolate_range - isolate given range into disjoint memblocks
  373. * @type: memblock type to isolate range for
  374. * @base: base of range to isolate
  375. * @size: size of range to isolate
  376. * @start_rgn: out parameter for the start of isolated region
  377. * @end_rgn: out parameter for the end of isolated region
  378. *
  379. * Walk @type and ensure that regions don't cross the boundaries defined by
  380. * [@base,@base+@size). Crossing regions are split at the boundaries,
  381. * which may create at most two more regions. The index of the first
  382. * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  383. *
  384. * RETURNS:
  385. * 0 on success, -errno on failure.
  386. */
  387. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  388. phys_addr_t base, phys_addr_t size,
  389. int *start_rgn, int *end_rgn)
  390. {
  391. phys_addr_t end = base + memblock_cap_size(base, &size);
  392. int i;
  393. *start_rgn = *end_rgn = 0;
  394. if (!size)
  395. return 0;
  396. /* we'll create at most two more regions */
  397. while (type->cnt + 2 > type->max)
  398. if (memblock_double_array(type) < 0)
  399. return -ENOMEM;
  400. for (i = 0; i < type->cnt; i++) {
  401. struct memblock_region *rgn = &type->regions[i];
  402. phys_addr_t rbase = rgn->base;
  403. phys_addr_t rend = rbase + rgn->size;
  404. if (rbase >= end)
  405. break;
  406. if (rend <= base)
  407. continue;
  408. if (rbase < base) {
  409. /*
  410. * @rgn intersects from below. Split and continue
  411. * to process the next region - the new top half.
  412. */
  413. rgn->base = base;
  414. rgn->size -= base - rbase;
  415. type->total_size -= base - rbase;
  416. memblock_insert_region(type, i, rbase, base - rbase,
  417. memblock_get_region_node(rgn));
  418. } else if (rend > end) {
  419. /*
  420. * @rgn intersects from above. Split and redo the
  421. * current region - the new bottom half.
  422. */
  423. rgn->base = end;
  424. rgn->size -= end - rbase;
  425. type->total_size -= end - rbase;
  426. memblock_insert_region(type, i--, rbase, end - rbase,
  427. memblock_get_region_node(rgn));
  428. } else {
  429. /* @rgn is fully contained, record it */
  430. if (!*end_rgn)
  431. *start_rgn = i;
  432. *end_rgn = i + 1;
  433. }
  434. }
  435. return 0;
  436. }
  437. static int __init_memblock __memblock_remove(struct memblock_type *type,
  438. phys_addr_t base, phys_addr_t size)
  439. {
  440. int start_rgn, end_rgn;
  441. int i, ret;
  442. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  443. if (ret)
  444. return ret;
  445. for (i = end_rgn - 1; i >= start_rgn; i--)
  446. memblock_remove_region(type, i);
  447. return 0;
  448. }
  449. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  450. {
  451. return __memblock_remove(&memblock.memory, base, size);
  452. }
  453. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  454. {
  455. memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  456. (unsigned long long)base,
  457. (unsigned long long)base + size,
  458. (void *)_RET_IP_);
  459. return __memblock_remove(&memblock.reserved, base, size);
  460. }
  461. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  462. {
  463. struct memblock_type *_rgn = &memblock.reserved;
  464. memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
  465. (unsigned long long)base,
  466. (unsigned long long)base + size,
  467. (void *)_RET_IP_);
  468. return memblock_add_region(_rgn, base, size, MAX_NUMNODES);
  469. }
  470. /**
  471. * __next_free_mem_range - next function for for_each_free_mem_range()
  472. * @idx: pointer to u64 loop variable
  473. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  474. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  475. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  476. * @p_nid: ptr to int for nid of the range, can be %NULL
  477. *
  478. * Find the first free area from *@idx which matches @nid, fill the out
  479. * parameters, and update *@idx for the next iteration. The lower 32bit of
  480. * *@idx contains index into memory region and the upper 32bit indexes the
  481. * areas before each reserved region. For example, if reserved regions
  482. * look like the following,
  483. *
  484. * 0:[0-16), 1:[32-48), 2:[128-130)
  485. *
  486. * The upper 32bit indexes the following regions.
  487. *
  488. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  489. *
  490. * As both region arrays are sorted, the function advances the two indices
  491. * in lockstep and returns each intersection.
  492. */
  493. void __init_memblock __next_free_mem_range(u64 *idx, int nid,
  494. phys_addr_t *out_start,
  495. phys_addr_t *out_end, int *out_nid)
  496. {
  497. struct memblock_type *mem = &memblock.memory;
  498. struct memblock_type *rsv = &memblock.reserved;
  499. int mi = *idx & 0xffffffff;
  500. int ri = *idx >> 32;
  501. for ( ; mi < mem->cnt; mi++) {
  502. struct memblock_region *m = &mem->regions[mi];
  503. phys_addr_t m_start = m->base;
  504. phys_addr_t m_end = m->base + m->size;
  505. /* only memory regions are associated with nodes, check it */
  506. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  507. continue;
  508. /* scan areas before each reservation for intersection */
  509. for ( ; ri < rsv->cnt + 1; ri++) {
  510. struct memblock_region *r = &rsv->regions[ri];
  511. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  512. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  513. /* if ri advanced past mi, break out to advance mi */
  514. if (r_start >= m_end)
  515. break;
  516. /* if the two regions intersect, we're done */
  517. if (m_start < r_end) {
  518. if (out_start)
  519. *out_start = max(m_start, r_start);
  520. if (out_end)
  521. *out_end = min(m_end, r_end);
  522. if (out_nid)
  523. *out_nid = memblock_get_region_node(m);
  524. /*
  525. * The region which ends first is advanced
  526. * for the next iteration.
  527. */
  528. if (m_end <= r_end)
  529. mi++;
  530. else
  531. ri++;
  532. *idx = (u32)mi | (u64)ri << 32;
  533. return;
  534. }
  535. }
  536. }
  537. /* signal end of iteration */
  538. *idx = ULLONG_MAX;
  539. }
  540. /**
  541. * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
  542. * @idx: pointer to u64 loop variable
  543. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  544. * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
  545. * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
  546. * @p_nid: ptr to int for nid of the range, can be %NULL
  547. *
  548. * Reverse of __next_free_mem_range().
  549. */
  550. void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
  551. phys_addr_t *out_start,
  552. phys_addr_t *out_end, int *out_nid)
  553. {
  554. struct memblock_type *mem = &memblock.memory;
  555. struct memblock_type *rsv = &memblock.reserved;
  556. int mi = *idx & 0xffffffff;
  557. int ri = *idx >> 32;
  558. if (*idx == (u64)ULLONG_MAX) {
  559. mi = mem->cnt - 1;
  560. ri = rsv->cnt;
  561. }
  562. for ( ; mi >= 0; mi--) {
  563. struct memblock_region *m = &mem->regions[mi];
  564. phys_addr_t m_start = m->base;
  565. phys_addr_t m_end = m->base + m->size;
  566. /* only memory regions are associated with nodes, check it */
  567. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  568. continue;
  569. /* scan areas before each reservation for intersection */
  570. for ( ; ri >= 0; ri--) {
  571. struct memblock_region *r = &rsv->regions[ri];
  572. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  573. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  574. /* if ri advanced past mi, break out to advance mi */
  575. if (r_end <= m_start)
  576. break;
  577. /* if the two regions intersect, we're done */
  578. if (m_end > r_start) {
  579. if (out_start)
  580. *out_start = max(m_start, r_start);
  581. if (out_end)
  582. *out_end = min(m_end, r_end);
  583. if (out_nid)
  584. *out_nid = memblock_get_region_node(m);
  585. if (m_start >= r_start)
  586. mi--;
  587. else
  588. ri--;
  589. *idx = (u32)mi | (u64)ri << 32;
  590. return;
  591. }
  592. }
  593. }
  594. *idx = ULLONG_MAX;
  595. }
  596. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  597. /*
  598. * Common iterator interface used to define for_each_mem_range().
  599. */
  600. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  601. unsigned long *out_start_pfn,
  602. unsigned long *out_end_pfn, int *out_nid)
  603. {
  604. struct memblock_type *type = &memblock.memory;
  605. struct memblock_region *r;
  606. while (++*idx < type->cnt) {
  607. r = &type->regions[*idx];
  608. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  609. continue;
  610. if (nid == MAX_NUMNODES || nid == r->nid)
  611. break;
  612. }
  613. if (*idx >= type->cnt) {
  614. *idx = -1;
  615. return;
  616. }
  617. if (out_start_pfn)
  618. *out_start_pfn = PFN_UP(r->base);
  619. if (out_end_pfn)
  620. *out_end_pfn = PFN_DOWN(r->base + r->size);
  621. if (out_nid)
  622. *out_nid = r->nid;
  623. }
  624. /**
  625. * memblock_set_node - set node ID on memblock regions
  626. * @base: base of area to set node ID for
  627. * @size: size of area to set node ID for
  628. * @nid: node ID to set
  629. *
  630. * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
  631. * Regions which cross the area boundaries are split as necessary.
  632. *
  633. * RETURNS:
  634. * 0 on success, -errno on failure.
  635. */
  636. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  637. int nid)
  638. {
  639. struct memblock_type *type = &memblock.memory;
  640. int start_rgn, end_rgn;
  641. int i, ret;
  642. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  643. if (ret)
  644. return ret;
  645. for (i = start_rgn; i < end_rgn; i++)
  646. type->regions[i].nid = nid;
  647. memblock_merge_regions(type);
  648. return 0;
  649. }
  650. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  651. static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
  652. phys_addr_t align, phys_addr_t max_addr,
  653. int nid)
  654. {
  655. phys_addr_t found;
  656. /* align @size to avoid excessive fragmentation on reserved array */
  657. size = round_up(size, align);
  658. found = memblock_find_in_range_node(0, max_addr, size, align, nid);
  659. if (found && !memblock_reserve(found, size))
  660. return found;
  661. return 0;
  662. }
  663. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  664. {
  665. return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  666. }
  667. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  668. {
  669. return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
  670. }
  671. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  672. {
  673. phys_addr_t alloc;
  674. alloc = __memblock_alloc_base(size, align, max_addr);
  675. if (alloc == 0)
  676. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  677. (unsigned long long) size, (unsigned long long) max_addr);
  678. return alloc;
  679. }
  680. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  681. {
  682. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  683. }
  684. phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  685. {
  686. phys_addr_t res = memblock_alloc_nid(size, align, nid);
  687. if (res)
  688. return res;
  689. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  690. }
  691. /*
  692. * Remaining API functions
  693. */
  694. phys_addr_t __init memblock_phys_mem_size(void)
  695. {
  696. return memblock.memory.total_size;
  697. }
  698. /* lowest address */
  699. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  700. {
  701. return memblock.memory.regions[0].base;
  702. }
  703. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  704. {
  705. int idx = memblock.memory.cnt - 1;
  706. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  707. }
  708. void __init memblock_enforce_memory_limit(phys_addr_t limit)
  709. {
  710. unsigned long i;
  711. phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
  712. if (!limit)
  713. return;
  714. /* find out max address */
  715. for (i = 0; i < memblock.memory.cnt; i++) {
  716. struct memblock_region *r = &memblock.memory.regions[i];
  717. if (limit <= r->size) {
  718. max_addr = r->base + limit;
  719. break;
  720. }
  721. limit -= r->size;
  722. }
  723. /* truncate both memory and reserved regions */
  724. __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
  725. __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
  726. }
  727. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  728. {
  729. unsigned int left = 0, right = type->cnt;
  730. do {
  731. unsigned int mid = (right + left) / 2;
  732. if (addr < type->regions[mid].base)
  733. right = mid;
  734. else if (addr >= (type->regions[mid].base +
  735. type->regions[mid].size))
  736. left = mid + 1;
  737. else
  738. return mid;
  739. } while (left < right);
  740. return -1;
  741. }
  742. int __init memblock_is_reserved(phys_addr_t addr)
  743. {
  744. return memblock_search(&memblock.reserved, addr) != -1;
  745. }
  746. int __init_memblock memblock_is_memory(phys_addr_t addr)
  747. {
  748. return memblock_search(&memblock.memory, addr) != -1;
  749. }
  750. int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  751. {
  752. int idx = memblock_search(&memblock.memory, base);
  753. phys_addr_t end = base + memblock_cap_size(base, &size);
  754. if (idx == -1)
  755. return 0;
  756. return memblock.memory.regions[idx].base <= base &&
  757. (memblock.memory.regions[idx].base +
  758. memblock.memory.regions[idx].size) >= end;
  759. }
  760. int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  761. {
  762. memblock_cap_size(base, &size);
  763. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  764. }
  765. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  766. {
  767. memblock.current_limit = limit;
  768. }
  769. static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
  770. {
  771. unsigned long long base, size;
  772. int i;
  773. pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
  774. for (i = 0; i < type->cnt; i++) {
  775. struct memblock_region *rgn = &type->regions[i];
  776. char nid_buf[32] = "";
  777. base = rgn->base;
  778. size = rgn->size;
  779. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  780. if (memblock_get_region_node(rgn) != MAX_NUMNODES)
  781. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  782. memblock_get_region_node(rgn));
  783. #endif
  784. pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
  785. name, i, base, base + size - 1, size, nid_buf);
  786. }
  787. }
  788. void __init_memblock __memblock_dump_all(void)
  789. {
  790. pr_info("MEMBLOCK configuration:\n");
  791. pr_info(" memory size = %#llx reserved size = %#llx\n",
  792. (unsigned long long)memblock.memory.total_size,
  793. (unsigned long long)memblock.reserved.total_size);
  794. memblock_dump(&memblock.memory, "memory");
  795. memblock_dump(&memblock.reserved, "reserved");
  796. }
  797. void __init memblock_allow_resize(void)
  798. {
  799. memblock_can_resize = 1;
  800. }
  801. static int __init early_memblock(char *p)
  802. {
  803. if (p && strstr(p, "debug"))
  804. memblock_debug = 1;
  805. return 0;
  806. }
  807. early_param("memblock", early_memblock);
  808. #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
  809. static int memblock_debug_show(struct seq_file *m, void *private)
  810. {
  811. struct memblock_type *type = m->private;
  812. struct memblock_region *reg;
  813. int i;
  814. for (i = 0; i < type->cnt; i++) {
  815. reg = &type->regions[i];
  816. seq_printf(m, "%4d: ", i);
  817. if (sizeof(phys_addr_t) == 4)
  818. seq_printf(m, "0x%08lx..0x%08lx\n",
  819. (unsigned long)reg->base,
  820. (unsigned long)(reg->base + reg->size - 1));
  821. else
  822. seq_printf(m, "0x%016llx..0x%016llx\n",
  823. (unsigned long long)reg->base,
  824. (unsigned long long)(reg->base + reg->size - 1));
  825. }
  826. return 0;
  827. }
  828. static int memblock_debug_open(struct inode *inode, struct file *file)
  829. {
  830. return single_open(file, memblock_debug_show, inode->i_private);
  831. }
  832. static const struct file_operations memblock_debug_fops = {
  833. .open = memblock_debug_open,
  834. .read = seq_read,
  835. .llseek = seq_lseek,
  836. .release = single_release,
  837. };
  838. static int __init memblock_init_debugfs(void)
  839. {
  840. struct dentry *root = debugfs_create_dir("memblock", NULL);
  841. if (!root)
  842. return -ENXIO;
  843. debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
  844. debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
  845. return 0;
  846. }
  847. __initcall(memblock_init_debugfs);
  848. #endif /* CONFIG_DEBUG_FS */