memblock.c 21 KB

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