memblock.c 22 KB

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