memblock.c 21 KB

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