memblock.c 23 KB

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