memblock.c 30 KB

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