memblock.c 22 KB

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