memblock.c 20 KB

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