memblock.c 21 KB

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