memblock.c 21 KB

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