memblock.c 23 KB

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