lmb.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <asm/types.h>
  16. #include <asm/page.h>
  17. #include <asm/prom.h>
  18. #include <asm/lmb.h>
  19. #ifdef CONFIG_PPC32
  20. #include "mmu_decl.h" /* for __max_low_memory */
  21. #endif
  22. #undef DEBUG
  23. #ifdef DEBUG
  24. #include <asm/udbg.h>
  25. #define DBG(fmt...) udbg_printf(fmt)
  26. #else
  27. #define DBG(fmt...)
  28. #endif
  29. #define LMB_ALLOC_ANYWHERE 0
  30. struct lmb lmb;
  31. void lmb_dump_all(void)
  32. {
  33. #ifdef DEBUG
  34. unsigned long i;
  35. DBG("lmb_dump_all:\n");
  36. DBG(" memory.cnt = 0x%lx\n", lmb.memory.cnt);
  37. DBG(" memory.size = 0x%lx\n", lmb.memory.size);
  38. for (i=0; i < lmb.memory.cnt ;i++) {
  39. DBG(" memory.region[0x%x].base = 0x%lx\n",
  40. i, lmb.memory.region[i].base);
  41. DBG(" .size = 0x%lx\n",
  42. lmb.memory.region[i].size);
  43. }
  44. DBG("\n reserved.cnt = 0x%lx\n", lmb.reserved.cnt);
  45. DBG(" reserved.size = 0x%lx\n", lmb.reserved.size);
  46. for (i=0; i < lmb.reserved.cnt ;i++) {
  47. DBG(" reserved.region[0x%x].base = 0x%lx\n",
  48. i, lmb.reserved.region[i].base);
  49. DBG(" .size = 0x%lx\n",
  50. lmb.reserved.region[i].size);
  51. }
  52. #endif /* DEBUG */
  53. }
  54. static unsigned long __init lmb_addrs_overlap(unsigned long base1,
  55. unsigned long size1, unsigned long base2, unsigned long size2)
  56. {
  57. return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
  58. }
  59. static long __init lmb_addrs_adjacent(unsigned long base1, unsigned long size1,
  60. unsigned long base2, unsigned long size2)
  61. {
  62. if (base2 == base1 + size1)
  63. return 1;
  64. else if (base1 == base2 + size2)
  65. return -1;
  66. return 0;
  67. }
  68. static long __init lmb_regions_adjacent(struct lmb_region *rgn,
  69. unsigned long r1, unsigned long r2)
  70. {
  71. unsigned long base1 = rgn->region[r1].base;
  72. unsigned long size1 = rgn->region[r1].size;
  73. unsigned long base2 = rgn->region[r2].base;
  74. unsigned long size2 = rgn->region[r2].size;
  75. return lmb_addrs_adjacent(base1, size1, base2, size2);
  76. }
  77. static void __init lmb_remove_region(struct lmb_region *rgn, unsigned long r)
  78. {
  79. unsigned long i;
  80. for (i = r; i < rgn->cnt - 1; i++) {
  81. rgn->region[i].base = rgn->region[i + 1].base;
  82. rgn->region[i].size = rgn->region[i + 1].size;
  83. }
  84. rgn->cnt--;
  85. }
  86. /* Assumption: base addr of region 1 < base addr of region 2 */
  87. static void __init lmb_coalesce_regions(struct lmb_region *rgn,
  88. unsigned long r1, unsigned long r2)
  89. {
  90. rgn->region[r1].size += rgn->region[r2].size;
  91. lmb_remove_region(rgn, r2);
  92. }
  93. /* This routine called with relocation disabled. */
  94. void __init lmb_init(void)
  95. {
  96. /* Create a dummy zero size LMB which will get coalesced away later.
  97. * This simplifies the lmb_add() code below...
  98. */
  99. lmb.memory.region[0].base = 0;
  100. lmb.memory.region[0].size = 0;
  101. lmb.memory.cnt = 1;
  102. /* Ditto. */
  103. lmb.reserved.region[0].base = 0;
  104. lmb.reserved.region[0].size = 0;
  105. lmb.reserved.cnt = 1;
  106. }
  107. /* This routine may be called with relocation disabled. */
  108. void __init lmb_analyze(void)
  109. {
  110. int i;
  111. lmb.memory.size = 0;
  112. for (i = 0; i < lmb.memory.cnt; i++)
  113. lmb.memory.size += lmb.memory.region[i].size;
  114. }
  115. /* This routine called with relocation disabled. */
  116. static long __init lmb_add_region(struct lmb_region *rgn, unsigned long base,
  117. unsigned long size)
  118. {
  119. unsigned long coalesced = 0;
  120. long adjacent, i;
  121. /* First try and coalesce this LMB with another. */
  122. for (i=0; i < rgn->cnt; i++) {
  123. unsigned long rgnbase = rgn->region[i].base;
  124. unsigned long rgnsize = rgn->region[i].size;
  125. if ((rgnbase == base) && (rgnsize == size))
  126. /* Already have this region, so we're done */
  127. return 0;
  128. adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize);
  129. if ( adjacent > 0 ) {
  130. rgn->region[i].base -= size;
  131. rgn->region[i].size += size;
  132. coalesced++;
  133. break;
  134. }
  135. else if ( adjacent < 0 ) {
  136. rgn->region[i].size += size;
  137. coalesced++;
  138. break;
  139. }
  140. }
  141. if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
  142. lmb_coalesce_regions(rgn, i, i+1);
  143. coalesced++;
  144. }
  145. if (coalesced)
  146. return coalesced;
  147. if (rgn->cnt >= MAX_LMB_REGIONS)
  148. return -1;
  149. /* Couldn't coalesce the LMB, so add it to the sorted table. */
  150. for (i = rgn->cnt-1; i >= 0; i--) {
  151. if (base < rgn->region[i].base) {
  152. rgn->region[i+1].base = rgn->region[i].base;
  153. rgn->region[i+1].size = rgn->region[i].size;
  154. } else {
  155. rgn->region[i+1].base = base;
  156. rgn->region[i+1].size = size;
  157. break;
  158. }
  159. }
  160. rgn->cnt++;
  161. return 0;
  162. }
  163. /* This routine may be called with relocation disabled. */
  164. long __init lmb_add(unsigned long base, unsigned long size)
  165. {
  166. struct lmb_region *_rgn = &(lmb.memory);
  167. /* On pSeries LPAR systems, the first LMB is our RMO region. */
  168. if (base == 0)
  169. lmb.rmo_size = size;
  170. return lmb_add_region(_rgn, base, size);
  171. }
  172. long __init lmb_reserve(unsigned long base, unsigned long size)
  173. {
  174. struct lmb_region *_rgn = &(lmb.reserved);
  175. BUG_ON(0 == size);
  176. return lmb_add_region(_rgn, base, size);
  177. }
  178. long __init lmb_overlaps_region(struct lmb_region *rgn, unsigned long base,
  179. unsigned long size)
  180. {
  181. unsigned long i;
  182. for (i=0; i < rgn->cnt; i++) {
  183. unsigned long rgnbase = rgn->region[i].base;
  184. unsigned long rgnsize = rgn->region[i].size;
  185. if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
  186. break;
  187. }
  188. }
  189. return (i < rgn->cnt) ? i : -1;
  190. }
  191. unsigned long __init lmb_alloc(unsigned long size, unsigned long align)
  192. {
  193. return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
  194. }
  195. unsigned long __init lmb_alloc_base(unsigned long size, unsigned long align,
  196. unsigned long max_addr)
  197. {
  198. unsigned long alloc;
  199. alloc = __lmb_alloc_base(size, align, max_addr);
  200. if (alloc == 0)
  201. panic("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
  202. size, max_addr);
  203. return alloc;
  204. }
  205. unsigned long __init __lmb_alloc_base(unsigned long size, unsigned long align,
  206. unsigned long max_addr)
  207. {
  208. long i, j;
  209. unsigned long base = 0;
  210. BUG_ON(0 == size);
  211. #ifdef CONFIG_PPC32
  212. /* On 32-bit, make sure we allocate lowmem */
  213. if (max_addr == LMB_ALLOC_ANYWHERE)
  214. max_addr = __max_low_memory;
  215. #endif
  216. for (i = lmb.memory.cnt-1; i >= 0; i--) {
  217. unsigned long lmbbase = lmb.memory.region[i].base;
  218. unsigned long lmbsize = lmb.memory.region[i].size;
  219. if (max_addr == LMB_ALLOC_ANYWHERE)
  220. base = _ALIGN_DOWN(lmbbase + lmbsize - size, align);
  221. else if (lmbbase < max_addr) {
  222. base = min(lmbbase + lmbsize, max_addr);
  223. base = _ALIGN_DOWN(base - size, align);
  224. } else
  225. continue;
  226. while ((lmbbase <= base) &&
  227. ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) )
  228. base = _ALIGN_DOWN(lmb.reserved.region[j].base - size,
  229. align);
  230. if ((base != 0) && (lmbbase <= base))
  231. break;
  232. }
  233. if (i < 0)
  234. return 0;
  235. lmb_add_region(&lmb.reserved, base, size);
  236. return base;
  237. }
  238. /* You must call lmb_analyze() before this. */
  239. unsigned long __init lmb_phys_mem_size(void)
  240. {
  241. return lmb.memory.size;
  242. }
  243. unsigned long __init lmb_end_of_DRAM(void)
  244. {
  245. int idx = lmb.memory.cnt - 1;
  246. return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
  247. }
  248. /* You must call lmb_analyze() after this. */
  249. void __init lmb_enforce_memory_limit(unsigned long memory_limit)
  250. {
  251. unsigned long i, limit;
  252. struct lmb_property *p;
  253. if (! memory_limit)
  254. return;
  255. /* Truncate the lmb regions to satisfy the memory limit. */
  256. limit = memory_limit;
  257. for (i = 0; i < lmb.memory.cnt; i++) {
  258. if (limit > lmb.memory.region[i].size) {
  259. limit -= lmb.memory.region[i].size;
  260. continue;
  261. }
  262. lmb.memory.region[i].size = limit;
  263. lmb.memory.cnt = i + 1;
  264. break;
  265. }
  266. if (lmb.memory.region[0].size < lmb.rmo_size)
  267. lmb.rmo_size = lmb.memory.region[0].size;
  268. /* And truncate any reserves above the limit also. */
  269. for (i = 0; i < lmb.reserved.cnt; i++) {
  270. p = &lmb.reserved.region[i];
  271. if (p->base > memory_limit)
  272. p->size = 0;
  273. else if ((p->base + p->size) > memory_limit)
  274. p->size = memory_limit - p->base;
  275. if (p->size == 0) {
  276. lmb_remove_region(&lmb.reserved, i);
  277. i--;
  278. }
  279. }
  280. }