memblock.c 30 KB

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