memblock.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. struct memblock memblock __initdata_memblock;
  22. int memblock_debug __initdata_memblock;
  23. int memblock_can_resize __initdata_memblock;
  24. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1] __initdata_memblock;
  25. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1] __initdata_memblock;
  26. /* inline so we don't get a warning when pr_debug is compiled out */
  27. static inline const char *memblock_type_name(struct memblock_type *type)
  28. {
  29. if (type == &memblock.memory)
  30. return "memory";
  31. else if (type == &memblock.reserved)
  32. return "reserved";
  33. else
  34. return "unknown";
  35. }
  36. /*
  37. * Address comparison utilities
  38. */
  39. static phys_addr_t __init_memblock memblock_align_down(phys_addr_t addr, phys_addr_t size)
  40. {
  41. return addr & ~(size - 1);
  42. }
  43. static phys_addr_t __init_memblock memblock_align_up(phys_addr_t addr, phys_addr_t size)
  44. {
  45. return (addr + (size - 1)) & ~(size - 1);
  46. }
  47. static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
  48. phys_addr_t base2, phys_addr_t size2)
  49. {
  50. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  51. }
  52. static long __init_memblock memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
  53. phys_addr_t base2, phys_addr_t size2)
  54. {
  55. if (base2 == base1 + size1)
  56. return 1;
  57. else if (base1 == base2 + size2)
  58. return -1;
  59. return 0;
  60. }
  61. static long __init_memblock memblock_regions_adjacent(struct memblock_type *type,
  62. unsigned long r1, unsigned long r2)
  63. {
  64. phys_addr_t base1 = type->regions[r1].base;
  65. phys_addr_t size1 = type->regions[r1].size;
  66. phys_addr_t base2 = type->regions[r2].base;
  67. phys_addr_t size2 = type->regions[r2].size;
  68. return memblock_addrs_adjacent(base1, size1, base2, size2);
  69. }
  70. long __init_memblock memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
  71. {
  72. unsigned long i;
  73. for (i = 0; i < type->cnt; i++) {
  74. phys_addr_t rgnbase = type->regions[i].base;
  75. phys_addr_t rgnsize = type->regions[i].size;
  76. if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
  77. break;
  78. }
  79. return (i < type->cnt) ? i : -1;
  80. }
  81. /*
  82. * Find, allocate, deallocate or reserve unreserved regions. All allocations
  83. * are top-down.
  84. */
  85. static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
  86. phys_addr_t size, phys_addr_t align)
  87. {
  88. phys_addr_t base, res_base;
  89. long j;
  90. /* Prevent allocations returning 0 as it's also used to
  91. * indicate an allocation failure
  92. */
  93. if (start == 0)
  94. start = PAGE_SIZE;
  95. base = memblock_align_down((end - size), align);
  96. while (start <= base) {
  97. j = memblock_overlaps_region(&memblock.reserved, base, size);
  98. if (j < 0)
  99. return base;
  100. res_base = memblock.reserved.regions[j].base;
  101. if (res_base < size)
  102. break;
  103. base = memblock_align_down(res_base - size, align);
  104. }
  105. return MEMBLOCK_ERROR;
  106. }
  107. static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align,
  108. phys_addr_t start, phys_addr_t end)
  109. {
  110. long i;
  111. BUG_ON(0 == size);
  112. size = memblock_align_up(size, align);
  113. /* Pump up max_addr */
  114. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  115. end = memblock.current_limit;
  116. /* We do a top-down search, this tends to limit memory
  117. * fragmentation by keeping early boot allocs near the
  118. * top of memory
  119. */
  120. for (i = memblock.memory.cnt - 1; i >= 0; i--) {
  121. phys_addr_t memblockbase = memblock.memory.regions[i].base;
  122. phys_addr_t memblocksize = memblock.memory.regions[i].size;
  123. phys_addr_t bottom, top, found;
  124. if (memblocksize < size)
  125. continue;
  126. if ((memblockbase + memblocksize) <= start)
  127. break;
  128. bottom = max(memblockbase, start);
  129. top = min(memblockbase + memblocksize, end);
  130. if (bottom >= top)
  131. continue;
  132. found = memblock_find_region(bottom, top, size, align);
  133. if (found != MEMBLOCK_ERROR)
  134. return found;
  135. }
  136. return MEMBLOCK_ERROR;
  137. }
  138. /*
  139. * Find a free area with specified alignment in a specific range.
  140. */
  141. u64 __init_memblock memblock_find_in_range(u64 start, u64 end, u64 size, u64 align)
  142. {
  143. return memblock_find_base(size, align, start, end);
  144. }
  145. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  146. {
  147. unsigned long i;
  148. for (i = r; i < type->cnt - 1; i++) {
  149. type->regions[i].base = type->regions[i + 1].base;
  150. type->regions[i].size = type->regions[i + 1].size;
  151. }
  152. type->cnt--;
  153. }
  154. /* Assumption: base addr of region 1 < base addr of region 2 */
  155. static void __init_memblock memblock_coalesce_regions(struct memblock_type *type,
  156. unsigned long r1, unsigned long r2)
  157. {
  158. type->regions[r1].size += type->regions[r2].size;
  159. memblock_remove_region(type, r2);
  160. }
  161. /* Defined below but needed now */
  162. static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size);
  163. static int __init_memblock memblock_double_array(struct memblock_type *type)
  164. {
  165. struct memblock_region *new_array, *old_array;
  166. phys_addr_t old_size, new_size, addr;
  167. int use_slab = slab_is_available();
  168. /* We don't allow resizing until we know about the reserved regions
  169. * of memory that aren't suitable for allocation
  170. */
  171. if (!memblock_can_resize)
  172. return -1;
  173. /* Calculate new doubled size */
  174. old_size = type->max * sizeof(struct memblock_region);
  175. new_size = old_size << 1;
  176. /* Try to find some space for it.
  177. *
  178. * WARNING: We assume that either slab_is_available() and we use it or
  179. * we use MEMBLOCK for allocations. That means that this is unsafe to use
  180. * when bootmem is currently active (unless bootmem itself is implemented
  181. * on top of MEMBLOCK which isn't the case yet)
  182. *
  183. * This should however not be an issue for now, as we currently only
  184. * call into MEMBLOCK while it's still active, or much later when slab is
  185. * active for memory hotplug operations
  186. */
  187. if (use_slab) {
  188. new_array = kmalloc(new_size, GFP_KERNEL);
  189. addr = new_array == NULL ? MEMBLOCK_ERROR : __pa(new_array);
  190. } else
  191. addr = memblock_find_base(new_size, sizeof(phys_addr_t), 0, MEMBLOCK_ALLOC_ACCESSIBLE);
  192. if (addr == MEMBLOCK_ERROR) {
  193. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  194. memblock_type_name(type), type->max, type->max * 2);
  195. return -1;
  196. }
  197. new_array = __va(addr);
  198. memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]",
  199. memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1);
  200. /* Found space, we now need to move the array over before
  201. * we add the reserved region since it may be our reserved
  202. * array itself that is full.
  203. */
  204. memcpy(new_array, type->regions, old_size);
  205. memset(new_array + type->max, 0, old_size);
  206. old_array = type->regions;
  207. type->regions = new_array;
  208. type->max <<= 1;
  209. /* If we use SLAB that's it, we are done */
  210. if (use_slab)
  211. return 0;
  212. /* Add the new reserved region now. Should not fail ! */
  213. BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size) < 0);
  214. /* If the array wasn't our static init one, then free it. We only do
  215. * that before SLAB is available as later on, we don't know whether
  216. * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
  217. * anyways
  218. */
  219. if (old_array != memblock_memory_init_regions &&
  220. old_array != memblock_reserved_init_regions)
  221. memblock_free(__pa(old_array), old_size);
  222. return 0;
  223. }
  224. extern int __init_memblock __weak memblock_memory_can_coalesce(phys_addr_t addr1, phys_addr_t size1,
  225. phys_addr_t addr2, phys_addr_t size2)
  226. {
  227. return 1;
  228. }
  229. static long __init_memblock memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
  230. {
  231. unsigned long coalesced = 0;
  232. long adjacent, i;
  233. if ((type->cnt == 1) && (type->regions[0].size == 0)) {
  234. type->regions[0].base = base;
  235. type->regions[0].size = size;
  236. return 0;
  237. }
  238. /* First try and coalesce this MEMBLOCK with another. */
  239. for (i = 0; i < type->cnt; i++) {
  240. phys_addr_t rgnbase = type->regions[i].base;
  241. phys_addr_t rgnsize = type->regions[i].size;
  242. if ((rgnbase == base) && (rgnsize == size))
  243. /* Already have this region, so we're done */
  244. return 0;
  245. adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
  246. /* Check if arch allows coalescing */
  247. if (adjacent != 0 && type == &memblock.memory &&
  248. !memblock_memory_can_coalesce(base, size, rgnbase, rgnsize))
  249. break;
  250. if (adjacent > 0) {
  251. type->regions[i].base -= size;
  252. type->regions[i].size += size;
  253. coalesced++;
  254. break;
  255. } else if (adjacent < 0) {
  256. type->regions[i].size += size;
  257. coalesced++;
  258. break;
  259. }
  260. }
  261. /* If we plugged a hole, we may want to also coalesce with the
  262. * next region
  263. */
  264. if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1) &&
  265. ((type != &memblock.memory || memblock_memory_can_coalesce(type->regions[i].base,
  266. type->regions[i].size,
  267. type->regions[i+1].base,
  268. type->regions[i+1].size)))) {
  269. memblock_coalesce_regions(type, i, i+1);
  270. coalesced++;
  271. }
  272. if (coalesced)
  273. return coalesced;
  274. /* If we are out of space, we fail. It's too late to resize the array
  275. * but then this shouldn't have happened in the first place.
  276. */
  277. if (WARN_ON(type->cnt >= type->max))
  278. return -1;
  279. /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
  280. for (i = type->cnt - 1; i >= 0; i--) {
  281. if (base < type->regions[i].base) {
  282. type->regions[i+1].base = type->regions[i].base;
  283. type->regions[i+1].size = type->regions[i].size;
  284. } else {
  285. type->regions[i+1].base = base;
  286. type->regions[i+1].size = size;
  287. break;
  288. }
  289. }
  290. if (base < type->regions[0].base) {
  291. type->regions[0].base = base;
  292. type->regions[0].size = size;
  293. }
  294. type->cnt++;
  295. /* The array is full ? Try to resize it. If that fails, we undo
  296. * our allocation and return an error
  297. */
  298. if (type->cnt == type->max && memblock_double_array(type)) {
  299. type->cnt--;
  300. return -1;
  301. }
  302. return 0;
  303. }
  304. long __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  305. {
  306. return memblock_add_region(&memblock.memory, base, size);
  307. }
  308. static long __init_memblock __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
  309. {
  310. phys_addr_t rgnbegin, rgnend;
  311. phys_addr_t end = base + size;
  312. int i;
  313. rgnbegin = rgnend = 0; /* supress gcc warnings */
  314. /* Find the region where (base, size) belongs to */
  315. for (i=0; i < type->cnt; i++) {
  316. rgnbegin = type->regions[i].base;
  317. rgnend = rgnbegin + type->regions[i].size;
  318. if ((rgnbegin <= base) && (end <= rgnend))
  319. break;
  320. }
  321. /* Didn't find the region */
  322. if (i == type->cnt)
  323. return -1;
  324. /* Check to see if we are removing entire region */
  325. if ((rgnbegin == base) && (rgnend == end)) {
  326. memblock_remove_region(type, i);
  327. return 0;
  328. }
  329. /* Check to see if region is matching at the front */
  330. if (rgnbegin == base) {
  331. type->regions[i].base = end;
  332. type->regions[i].size -= size;
  333. return 0;
  334. }
  335. /* Check to see if the region is matching at the end */
  336. if (rgnend == end) {
  337. type->regions[i].size -= size;
  338. return 0;
  339. }
  340. /*
  341. * We need to split the entry - adjust the current one to the
  342. * beginging of the hole and add the region after hole.
  343. */
  344. type->regions[i].size = base - type->regions[i].base;
  345. return memblock_add_region(type, end, rgnend - end);
  346. }
  347. long __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  348. {
  349. return __memblock_remove(&memblock.memory, base, size);
  350. }
  351. long __init memblock_free(phys_addr_t base, phys_addr_t size)
  352. {
  353. return __memblock_remove(&memblock.reserved, base, size);
  354. }
  355. long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
  356. {
  357. struct memblock_type *_rgn = &memblock.reserved;
  358. BUG_ON(0 == size);
  359. return memblock_add_region(_rgn, base, size);
  360. }
  361. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  362. {
  363. phys_addr_t found;
  364. /* We align the size to limit fragmentation. Without this, a lot of
  365. * small allocs quickly eat up the whole reserve array on sparc
  366. */
  367. size = memblock_align_up(size, align);
  368. found = memblock_find_base(size, align, 0, max_addr);
  369. if (found != MEMBLOCK_ERROR &&
  370. memblock_add_region(&memblock.reserved, found, size) >= 0)
  371. return found;
  372. return 0;
  373. }
  374. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  375. {
  376. phys_addr_t alloc;
  377. alloc = __memblock_alloc_base(size, align, max_addr);
  378. if (alloc == 0)
  379. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  380. (unsigned long long) size, (unsigned long long) max_addr);
  381. return alloc;
  382. }
  383. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  384. {
  385. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  386. }
  387. /*
  388. * Additional node-local allocators. Search for node memory is bottom up
  389. * and walks memblock regions within that node bottom-up as well, but allocation
  390. * within an memblock region is top-down. XXX I plan to fix that at some stage
  391. *
  392. * WARNING: Only available after early_node_map[] has been populated,
  393. * on some architectures, that is after all the calls to add_active_range()
  394. * have been done to populate it.
  395. */
  396. phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
  397. {
  398. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  399. /*
  400. * This code originates from sparc which really wants use to walk by addresses
  401. * and returns the nid. This is not very convenient for early_pfn_map[] users
  402. * as the map isn't sorted yet, and it really wants to be walked by nid.
  403. *
  404. * For now, I implement the inefficient method below which walks the early
  405. * map multiple times. Eventually we may want to use an ARCH config option
  406. * to implement a completely different method for both case.
  407. */
  408. unsigned long start_pfn, end_pfn;
  409. int i;
  410. for (i = 0; i < MAX_NUMNODES; i++) {
  411. get_pfn_range_for_nid(i, &start_pfn, &end_pfn);
  412. if (start < PFN_PHYS(start_pfn) || start >= PFN_PHYS(end_pfn))
  413. continue;
  414. *nid = i;
  415. return min(end, PFN_PHYS(end_pfn));
  416. }
  417. #endif
  418. *nid = 0;
  419. return end;
  420. }
  421. static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
  422. phys_addr_t size,
  423. phys_addr_t align, int nid)
  424. {
  425. phys_addr_t start, end;
  426. start = mp->base;
  427. end = start + mp->size;
  428. start = memblock_align_up(start, align);
  429. while (start < end) {
  430. phys_addr_t this_end;
  431. int this_nid;
  432. this_end = memblock_nid_range(start, end, &this_nid);
  433. if (this_nid == nid) {
  434. phys_addr_t ret = memblock_find_region(start, this_end, size, align);
  435. if (ret != MEMBLOCK_ERROR &&
  436. memblock_add_region(&memblock.reserved, ret, size) >= 0)
  437. return ret;
  438. }
  439. start = this_end;
  440. }
  441. return MEMBLOCK_ERROR;
  442. }
  443. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  444. {
  445. struct memblock_type *mem = &memblock.memory;
  446. int i;
  447. BUG_ON(0 == size);
  448. /* We align the size to limit fragmentation. Without this, a lot of
  449. * small allocs quickly eat up the whole reserve array on sparc
  450. */
  451. size = memblock_align_up(size, align);
  452. /* We do a bottom-up search for a region with the right
  453. * nid since that's easier considering how memblock_nid_range()
  454. * works
  455. */
  456. for (i = 0; i < mem->cnt; i++) {
  457. phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
  458. size, align, nid);
  459. if (ret != MEMBLOCK_ERROR)
  460. return ret;
  461. }
  462. return 0;
  463. }
  464. phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  465. {
  466. phys_addr_t res = memblock_alloc_nid(size, align, nid);
  467. if (res)
  468. return res;
  469. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE);
  470. }
  471. /*
  472. * Remaining API functions
  473. */
  474. /* You must call memblock_analyze() before this. */
  475. phys_addr_t __init memblock_phys_mem_size(void)
  476. {
  477. return memblock.memory_size;
  478. }
  479. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  480. {
  481. int idx = memblock.memory.cnt - 1;
  482. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  483. }
  484. /* You must call memblock_analyze() after this. */
  485. void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
  486. {
  487. unsigned long i;
  488. phys_addr_t limit;
  489. struct memblock_region *p;
  490. if (!memory_limit)
  491. return;
  492. /* Truncate the memblock regions to satisfy the memory limit. */
  493. limit = memory_limit;
  494. for (i = 0; i < memblock.memory.cnt; i++) {
  495. if (limit > memblock.memory.regions[i].size) {
  496. limit -= memblock.memory.regions[i].size;
  497. continue;
  498. }
  499. memblock.memory.regions[i].size = limit;
  500. memblock.memory.cnt = i + 1;
  501. break;
  502. }
  503. memory_limit = memblock_end_of_DRAM();
  504. /* And truncate any reserves above the limit also. */
  505. for (i = 0; i < memblock.reserved.cnt; i++) {
  506. p = &memblock.reserved.regions[i];
  507. if (p->base > memory_limit)
  508. p->size = 0;
  509. else if ((p->base + p->size) > memory_limit)
  510. p->size = memory_limit - p->base;
  511. if (p->size == 0) {
  512. memblock_remove_region(&memblock.reserved, i);
  513. i--;
  514. }
  515. }
  516. }
  517. static int memblock_search(struct memblock_type *type, phys_addr_t addr)
  518. {
  519. unsigned int left = 0, right = type->cnt;
  520. do {
  521. unsigned int mid = (right + left) / 2;
  522. if (addr < type->regions[mid].base)
  523. right = mid;
  524. else if (addr >= (type->regions[mid].base +
  525. type->regions[mid].size))
  526. left = mid + 1;
  527. else
  528. return mid;
  529. } while (left < right);
  530. return -1;
  531. }
  532. int __init memblock_is_reserved(phys_addr_t addr)
  533. {
  534. return memblock_search(&memblock.reserved, addr) != -1;
  535. }
  536. int memblock_is_memory(phys_addr_t addr)
  537. {
  538. return memblock_search(&memblock.memory, addr) != -1;
  539. }
  540. int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  541. {
  542. int idx = memblock_search(&memblock.reserved, base);
  543. if (idx == -1)
  544. return 0;
  545. return memblock.reserved.regions[idx].base <= base &&
  546. (memblock.reserved.regions[idx].base +
  547. memblock.reserved.regions[idx].size) >= (base + size);
  548. }
  549. int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  550. {
  551. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  552. }
  553. void __init memblock_set_current_limit(phys_addr_t limit)
  554. {
  555. memblock.current_limit = limit;
  556. }
  557. static void __init_memblock memblock_dump(struct memblock_type *region, char *name)
  558. {
  559. unsigned long long base, size;
  560. int i;
  561. pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
  562. for (i = 0; i < region->cnt; i++) {
  563. base = region->regions[i].base;
  564. size = region->regions[i].size;
  565. pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes\n",
  566. name, i, base, base + size - 1, size);
  567. }
  568. }
  569. void __init_memblock memblock_dump_all(void)
  570. {
  571. if (!memblock_debug)
  572. return;
  573. pr_info("MEMBLOCK configuration:\n");
  574. pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
  575. memblock_dump(&memblock.memory, "memory");
  576. memblock_dump(&memblock.reserved, "reserved");
  577. }
  578. void __init memblock_analyze(void)
  579. {
  580. int i;
  581. /* Check marker in the unused last array entry */
  582. WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
  583. != (phys_addr_t)RED_INACTIVE);
  584. WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
  585. != (phys_addr_t)RED_INACTIVE);
  586. memblock.memory_size = 0;
  587. for (i = 0; i < memblock.memory.cnt; i++)
  588. memblock.memory_size += memblock.memory.regions[i].size;
  589. /* We allow resizing from there */
  590. memblock_can_resize = 1;
  591. }
  592. void __init memblock_init(void)
  593. {
  594. /* Hookup the initial arrays */
  595. memblock.memory.regions = memblock_memory_init_regions;
  596. memblock.memory.max = INIT_MEMBLOCK_REGIONS;
  597. memblock.reserved.regions = memblock_reserved_init_regions;
  598. memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
  599. /* Write a marker in the unused last array entry */
  600. memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
  601. memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
  602. /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
  603. * This simplifies the memblock_add() code below...
  604. */
  605. memblock.memory.regions[0].base = 0;
  606. memblock.memory.regions[0].size = 0;
  607. memblock.memory.cnt = 1;
  608. /* Ditto. */
  609. memblock.reserved.regions[0].base = 0;
  610. memblock.reserved.regions[0].size = 0;
  611. memblock.reserved.cnt = 1;
  612. memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
  613. }
  614. static int __init early_memblock(char *p)
  615. {
  616. if (p && strstr(p, "debug"))
  617. memblock_debug = 1;
  618. return 0;
  619. }
  620. early_param("memblock", early_memblock);
  621. #if defined(CONFIG_DEBUG_FS) && !defined(ARCH_DISCARD_MEMBLOCK)
  622. static int memblock_debug_show(struct seq_file *m, void *private)
  623. {
  624. struct memblock_type *type = m->private;
  625. struct memblock_region *reg;
  626. int i;
  627. for (i = 0; i < type->cnt; i++) {
  628. reg = &type->regions[i];
  629. seq_printf(m, "%4d: ", i);
  630. if (sizeof(phys_addr_t) == 4)
  631. seq_printf(m, "0x%08lx..0x%08lx\n",
  632. (unsigned long)reg->base,
  633. (unsigned long)(reg->base + reg->size - 1));
  634. else
  635. seq_printf(m, "0x%016llx..0x%016llx\n",
  636. (unsigned long long)reg->base,
  637. (unsigned long long)(reg->base + reg->size - 1));
  638. }
  639. return 0;
  640. }
  641. static int memblock_debug_open(struct inode *inode, struct file *file)
  642. {
  643. return single_open(file, memblock_debug_show, inode->i_private);
  644. }
  645. static const struct file_operations memblock_debug_fops = {
  646. .open = memblock_debug_open,
  647. .read = seq_read,
  648. .llseek = seq_lseek,
  649. .release = single_release,
  650. };
  651. static int __init memblock_init_debugfs(void)
  652. {
  653. struct dentry *root = debugfs_create_dir("memblock", NULL);
  654. if (!root)
  655. return -ENXIO;
  656. debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
  657. debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
  658. return 0;
  659. }
  660. __initcall(memblock_init_debugfs);
  661. #endif /* CONFIG_DEBUG_FS */