memblock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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/init.h>
  14. #include <linux/bitops.h>
  15. #include <linux/poison.h>
  16. #include <linux/memblock.h>
  17. struct memblock memblock;
  18. static int memblock_debug;
  19. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1];
  20. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1];
  21. #define MEMBLOCK_ERROR (~(phys_addr_t)0)
  22. /*
  23. * Address comparison utilities
  24. */
  25. static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
  26. {
  27. return addr & ~(size - 1);
  28. }
  29. static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
  30. {
  31. return (addr + (size - 1)) & ~(size - 1);
  32. }
  33. static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
  34. phys_addr_t base2, phys_addr_t size2)
  35. {
  36. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  37. }
  38. static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
  39. phys_addr_t base2, phys_addr_t size2)
  40. {
  41. if (base2 == base1 + size1)
  42. return 1;
  43. else if (base1 == base2 + size2)
  44. return -1;
  45. return 0;
  46. }
  47. static long memblock_regions_adjacent(struct memblock_type *type,
  48. unsigned long r1, unsigned long r2)
  49. {
  50. phys_addr_t base1 = type->regions[r1].base;
  51. phys_addr_t size1 = type->regions[r1].size;
  52. phys_addr_t base2 = type->regions[r2].base;
  53. phys_addr_t size2 = type->regions[r2].size;
  54. return memblock_addrs_adjacent(base1, size1, base2, size2);
  55. }
  56. long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
  57. {
  58. unsigned long i;
  59. for (i = 0; i < type->cnt; i++) {
  60. phys_addr_t rgnbase = type->regions[i].base;
  61. phys_addr_t rgnsize = type->regions[i].size;
  62. if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
  63. break;
  64. }
  65. return (i < type->cnt) ? i : -1;
  66. }
  67. /*
  68. * Find, allocate, deallocate or reserve unreserved regions. All allocations
  69. * are top-down.
  70. */
  71. static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
  72. phys_addr_t size, phys_addr_t align)
  73. {
  74. phys_addr_t base, res_base;
  75. long j;
  76. base = memblock_align_down((end - size), align);
  77. while (start <= base) {
  78. j = memblock_overlaps_region(&memblock.reserved, base, size);
  79. if (j < 0)
  80. return base;
  81. res_base = memblock.reserved.regions[j].base;
  82. if (res_base < size)
  83. break;
  84. base = memblock_align_down(res_base - size, align);
  85. }
  86. return MEMBLOCK_ERROR;
  87. }
  88. static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  89. {
  90. long i;
  91. phys_addr_t base = 0;
  92. phys_addr_t res_base;
  93. BUG_ON(0 == size);
  94. size = memblock_align_up(size, align);
  95. /* Pump up max_addr */
  96. if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE)
  97. max_addr = memblock.current_limit;
  98. /* We do a top-down search, this tends to limit memory
  99. * fragmentation by keeping early boot allocs near the
  100. * top of memory
  101. */
  102. for (i = memblock.memory.cnt - 1; i >= 0; i--) {
  103. phys_addr_t memblockbase = memblock.memory.regions[i].base;
  104. phys_addr_t memblocksize = memblock.memory.regions[i].size;
  105. if (memblocksize < size)
  106. continue;
  107. base = min(memblockbase + memblocksize, max_addr);
  108. res_base = memblock_find_region(memblockbase, base, size, align);
  109. if (res_base != MEMBLOCK_ERROR)
  110. return res_base;
  111. }
  112. return MEMBLOCK_ERROR;
  113. }
  114. static void memblock_remove_region(struct memblock_type *type, unsigned long r)
  115. {
  116. unsigned long i;
  117. for (i = r; i < type->cnt - 1; i++) {
  118. type->regions[i].base = type->regions[i + 1].base;
  119. type->regions[i].size = type->regions[i + 1].size;
  120. }
  121. type->cnt--;
  122. }
  123. /* Assumption: base addr of region 1 < base addr of region 2 */
  124. static void memblock_coalesce_regions(struct memblock_type *type,
  125. unsigned long r1, unsigned long r2)
  126. {
  127. type->regions[r1].size += type->regions[r2].size;
  128. memblock_remove_region(type, r2);
  129. }
  130. static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
  131. {
  132. unsigned long coalesced = 0;
  133. long adjacent, i;
  134. if ((type->cnt == 1) && (type->regions[0].size == 0)) {
  135. type->regions[0].base = base;
  136. type->regions[0].size = size;
  137. return 0;
  138. }
  139. /* First try and coalesce this MEMBLOCK with another. */
  140. for (i = 0; i < type->cnt; i++) {
  141. phys_addr_t rgnbase = type->regions[i].base;
  142. phys_addr_t rgnsize = type->regions[i].size;
  143. if ((rgnbase == base) && (rgnsize == size))
  144. /* Already have this region, so we're done */
  145. return 0;
  146. adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
  147. if (adjacent > 0) {
  148. type->regions[i].base -= size;
  149. type->regions[i].size += size;
  150. coalesced++;
  151. break;
  152. } else if (adjacent < 0) {
  153. type->regions[i].size += size;
  154. coalesced++;
  155. break;
  156. }
  157. }
  158. if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) {
  159. memblock_coalesce_regions(type, i, i+1);
  160. coalesced++;
  161. }
  162. if (coalesced)
  163. return coalesced;
  164. if (type->cnt >= type->max)
  165. return -1;
  166. /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
  167. for (i = type->cnt - 1; i >= 0; i--) {
  168. if (base < type->regions[i].base) {
  169. type->regions[i+1].base = type->regions[i].base;
  170. type->regions[i+1].size = type->regions[i].size;
  171. } else {
  172. type->regions[i+1].base = base;
  173. type->regions[i+1].size = size;
  174. break;
  175. }
  176. }
  177. if (base < type->regions[0].base) {
  178. type->regions[0].base = base;
  179. type->regions[0].size = size;
  180. }
  181. type->cnt++;
  182. return 0;
  183. }
  184. long memblock_add(phys_addr_t base, phys_addr_t size)
  185. {
  186. return memblock_add_region(&memblock.memory, base, size);
  187. }
  188. static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
  189. {
  190. phys_addr_t rgnbegin, rgnend;
  191. phys_addr_t end = base + size;
  192. int i;
  193. rgnbegin = rgnend = 0; /* supress gcc warnings */
  194. /* Find the region where (base, size) belongs to */
  195. for (i=0; i < type->cnt; i++) {
  196. rgnbegin = type->regions[i].base;
  197. rgnend = rgnbegin + type->regions[i].size;
  198. if ((rgnbegin <= base) && (end <= rgnend))
  199. break;
  200. }
  201. /* Didn't find the region */
  202. if (i == type->cnt)
  203. return -1;
  204. /* Check to see if we are removing entire region */
  205. if ((rgnbegin == base) && (rgnend == end)) {
  206. memblock_remove_region(type, i);
  207. return 0;
  208. }
  209. /* Check to see if region is matching at the front */
  210. if (rgnbegin == base) {
  211. type->regions[i].base = end;
  212. type->regions[i].size -= size;
  213. return 0;
  214. }
  215. /* Check to see if the region is matching at the end */
  216. if (rgnend == end) {
  217. type->regions[i].size -= size;
  218. return 0;
  219. }
  220. /*
  221. * We need to split the entry - adjust the current one to the
  222. * beginging of the hole and add the region after hole.
  223. */
  224. type->regions[i].size = base - type->regions[i].base;
  225. return memblock_add_region(type, end, rgnend - end);
  226. }
  227. long memblock_remove(phys_addr_t base, phys_addr_t size)
  228. {
  229. return __memblock_remove(&memblock.memory, base, size);
  230. }
  231. long __init memblock_free(phys_addr_t base, phys_addr_t size)
  232. {
  233. return __memblock_remove(&memblock.reserved, base, size);
  234. }
  235. long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
  236. {
  237. struct memblock_type *_rgn = &memblock.reserved;
  238. BUG_ON(0 == size);
  239. return memblock_add_region(_rgn, base, size);
  240. }
  241. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  242. {
  243. phys_addr_t found;
  244. /* We align the size to limit fragmentation. Without this, a lot of
  245. * small allocs quickly eat up the whole reserve array on sparc
  246. */
  247. size = memblock_align_up(size, align);
  248. found = memblock_find_base(size, align, max_addr);
  249. if (found != MEMBLOCK_ERROR &&
  250. memblock_add_region(&memblock.reserved, found, size) >= 0)
  251. return found;
  252. return 0;
  253. }
  254. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  255. {
  256. phys_addr_t alloc;
  257. alloc = __memblock_alloc_base(size, align, max_addr);
  258. if (alloc == 0)
  259. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  260. (unsigned long long) size, (unsigned long long) max_addr);
  261. return alloc;
  262. }
  263. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  264. {
  265. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  266. }
  267. /*
  268. * Additional node-local allocators. Search for node memory is bottom up
  269. * and walks memblock regions within that node bottom-up as well, but allocation
  270. * within an memblock region is top-down.
  271. */
  272. phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
  273. {
  274. *nid = 0;
  275. return end;
  276. }
  277. static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
  278. phys_addr_t size,
  279. phys_addr_t align, int nid)
  280. {
  281. phys_addr_t start, end;
  282. start = mp->base;
  283. end = start + mp->size;
  284. start = memblock_align_up(start, align);
  285. while (start < end) {
  286. phys_addr_t this_end;
  287. int this_nid;
  288. this_end = memblock_nid_range(start, end, &this_nid);
  289. if (this_nid == nid) {
  290. phys_addr_t ret = memblock_find_region(start, this_end, size, align);
  291. if (ret != MEMBLOCK_ERROR &&
  292. memblock_add_region(&memblock.reserved, ret, size) >= 0)
  293. return ret;
  294. }
  295. start = this_end;
  296. }
  297. return MEMBLOCK_ERROR;
  298. }
  299. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  300. {
  301. struct memblock_type *mem = &memblock.memory;
  302. int i;
  303. BUG_ON(0 == size);
  304. /* We align the size to limit fragmentation. Without this, a lot of
  305. * small allocs quickly eat up the whole reserve array on sparc
  306. */
  307. size = memblock_align_up(size, align);
  308. /* We do a bottom-up search for a region with the right
  309. * nid since that's easier considering how memblock_nid_range()
  310. * works
  311. */
  312. for (i = 0; i < mem->cnt; i++) {
  313. phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
  314. size, align, nid);
  315. if (ret != MEMBLOCK_ERROR)
  316. return ret;
  317. }
  318. return memblock_alloc(size, align);
  319. }
  320. /* You must call memblock_analyze() before this. */
  321. phys_addr_t __init memblock_phys_mem_size(void)
  322. {
  323. return memblock.memory_size;
  324. }
  325. phys_addr_t memblock_end_of_DRAM(void)
  326. {
  327. int idx = memblock.memory.cnt - 1;
  328. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  329. }
  330. /* You must call memblock_analyze() after this. */
  331. void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
  332. {
  333. unsigned long i;
  334. phys_addr_t limit;
  335. struct memblock_region *p;
  336. if (!memory_limit)
  337. return;
  338. /* Truncate the memblock regions to satisfy the memory limit. */
  339. limit = memory_limit;
  340. for (i = 0; i < memblock.memory.cnt; i++) {
  341. if (limit > memblock.memory.regions[i].size) {
  342. limit -= memblock.memory.regions[i].size;
  343. continue;
  344. }
  345. memblock.memory.regions[i].size = limit;
  346. memblock.memory.cnt = i + 1;
  347. break;
  348. }
  349. memory_limit = memblock_end_of_DRAM();
  350. /* And truncate any reserves above the limit also. */
  351. for (i = 0; i < memblock.reserved.cnt; i++) {
  352. p = &memblock.reserved.regions[i];
  353. if (p->base > memory_limit)
  354. p->size = 0;
  355. else if ((p->base + p->size) > memory_limit)
  356. p->size = memory_limit - p->base;
  357. if (p->size == 0) {
  358. memblock_remove_region(&memblock.reserved, i);
  359. i--;
  360. }
  361. }
  362. }
  363. static int memblock_search(struct memblock_type *type, phys_addr_t addr)
  364. {
  365. unsigned int left = 0, right = type->cnt;
  366. do {
  367. unsigned int mid = (right + left) / 2;
  368. if (addr < type->regions[mid].base)
  369. right = mid;
  370. else if (addr >= (type->regions[mid].base +
  371. type->regions[mid].size))
  372. left = mid + 1;
  373. else
  374. return mid;
  375. } while (left < right);
  376. return -1;
  377. }
  378. int __init memblock_is_reserved(phys_addr_t addr)
  379. {
  380. return memblock_search(&memblock.reserved, addr) != -1;
  381. }
  382. int memblock_is_memory(phys_addr_t addr)
  383. {
  384. return memblock_search(&memblock.memory, addr) != -1;
  385. }
  386. int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  387. {
  388. int idx = memblock_search(&memblock.reserved, base);
  389. if (idx == -1)
  390. return 0;
  391. return memblock.reserved.regions[idx].base <= base &&
  392. (memblock.reserved.regions[idx].base +
  393. memblock.reserved.regions[idx].size) >= (base + size);
  394. }
  395. int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  396. {
  397. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  398. }
  399. void __init memblock_set_current_limit(phys_addr_t limit)
  400. {
  401. memblock.current_limit = limit;
  402. }
  403. static void memblock_dump(struct memblock_type *region, char *name)
  404. {
  405. unsigned long long base, size;
  406. int i;
  407. pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
  408. for (i = 0; i < region->cnt; i++) {
  409. base = region->regions[i].base;
  410. size = region->regions[i].size;
  411. pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
  412. name, i, base, base + size - 1, size);
  413. }
  414. }
  415. void memblock_dump_all(void)
  416. {
  417. if (!memblock_debug)
  418. return;
  419. pr_info("MEMBLOCK configuration:\n");
  420. pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
  421. memblock_dump(&memblock.memory, "memory");
  422. memblock_dump(&memblock.reserved, "reserved");
  423. }
  424. void __init memblock_analyze(void)
  425. {
  426. int i;
  427. /* Check marker in the unused last array entry */
  428. WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
  429. != (phys_addr_t)RED_INACTIVE);
  430. WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
  431. != (phys_addr_t)RED_INACTIVE);
  432. memblock.memory_size = 0;
  433. for (i = 0; i < memblock.memory.cnt; i++)
  434. memblock.memory_size += memblock.memory.regions[i].size;
  435. }
  436. void __init memblock_init(void)
  437. {
  438. /* Hookup the initial arrays */
  439. memblock.memory.regions = memblock_memory_init_regions;
  440. memblock.memory.max = INIT_MEMBLOCK_REGIONS;
  441. memblock.reserved.regions = memblock_reserved_init_regions;
  442. memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
  443. /* Write a marker in the unused last array entry */
  444. memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
  445. memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
  446. /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
  447. * This simplifies the memblock_add() code below...
  448. */
  449. memblock.memory.regions[0].base = 0;
  450. memblock.memory.regions[0].size = 0;
  451. memblock.memory.cnt = 1;
  452. /* Ditto. */
  453. memblock.reserved.regions[0].base = 0;
  454. memblock.reserved.regions[0].size = 0;
  455. memblock.reserved.cnt = 1;
  456. memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
  457. }
  458. static int __init early_memblock(char *p)
  459. {
  460. if (p && strstr(p, "debug"))
  461. memblock_debug = 1;
  462. return 0;
  463. }
  464. early_param("memblock", early_memblock);