lmb.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 i, coalesced = 0;
  120. long adjacent;
  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. adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize);
  126. if ( adjacent > 0 ) {
  127. rgn->region[i].base -= size;
  128. rgn->region[i].size += size;
  129. coalesced++;
  130. break;
  131. }
  132. else if ( adjacent < 0 ) {
  133. rgn->region[i].size += size;
  134. coalesced++;
  135. break;
  136. }
  137. }
  138. if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
  139. lmb_coalesce_regions(rgn, i, i+1);
  140. coalesced++;
  141. }
  142. if (coalesced)
  143. return coalesced;
  144. if (rgn->cnt >= MAX_LMB_REGIONS)
  145. return -1;
  146. /* Couldn't coalesce the LMB, so add it to the sorted table. */
  147. for (i = rgn->cnt-1; i >= 0; i--) {
  148. if (base < rgn->region[i].base) {
  149. rgn->region[i+1].base = rgn->region[i].base;
  150. rgn->region[i+1].size = rgn->region[i].size;
  151. } else {
  152. rgn->region[i+1].base = base;
  153. rgn->region[i+1].size = size;
  154. break;
  155. }
  156. }
  157. rgn->cnt++;
  158. return 0;
  159. }
  160. /* This routine may be called with relocation disabled. */
  161. long __init lmb_add(unsigned long base, unsigned long size)
  162. {
  163. struct lmb_region *_rgn = &(lmb.memory);
  164. /* On pSeries LPAR systems, the first LMB is our RMO region. */
  165. if (base == 0)
  166. lmb.rmo_size = size;
  167. return lmb_add_region(_rgn, base, size);
  168. }
  169. long __init lmb_reserve(unsigned long base, unsigned long size)
  170. {
  171. struct lmb_region *_rgn = &(lmb.reserved);
  172. BUG_ON(0 == size);
  173. return lmb_add_region(_rgn, base, size);
  174. }
  175. long __init lmb_overlaps_region(struct lmb_region *rgn, unsigned long base,
  176. unsigned long size)
  177. {
  178. unsigned long i;
  179. for (i=0; i < rgn->cnt; i++) {
  180. unsigned long rgnbase = rgn->region[i].base;
  181. unsigned long rgnsize = rgn->region[i].size;
  182. if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
  183. break;
  184. }
  185. }
  186. return (i < rgn->cnt) ? i : -1;
  187. }
  188. unsigned long __init lmb_alloc(unsigned long size, unsigned long align)
  189. {
  190. return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
  191. }
  192. unsigned long __init lmb_alloc_base(unsigned long size, unsigned long align,
  193. unsigned long max_addr)
  194. {
  195. unsigned long alloc;
  196. alloc = __lmb_alloc_base(size, align, max_addr);
  197. if (alloc == 0)
  198. panic("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
  199. size, max_addr);
  200. return alloc;
  201. }
  202. unsigned long __init __lmb_alloc_base(unsigned long size, unsigned long align,
  203. unsigned long max_addr)
  204. {
  205. long i, j;
  206. unsigned long base = 0;
  207. BUG_ON(0 == size);
  208. #ifdef CONFIG_PPC32
  209. /* On 32-bit, make sure we allocate lowmem */
  210. if (max_addr == LMB_ALLOC_ANYWHERE)
  211. max_addr = __max_low_memory;
  212. #endif
  213. for (i = lmb.memory.cnt-1; i >= 0; i--) {
  214. unsigned long lmbbase = lmb.memory.region[i].base;
  215. unsigned long lmbsize = lmb.memory.region[i].size;
  216. if (max_addr == LMB_ALLOC_ANYWHERE)
  217. base = _ALIGN_DOWN(lmbbase + lmbsize - size, align);
  218. else if (lmbbase < max_addr) {
  219. base = min(lmbbase + lmbsize, max_addr);
  220. base = _ALIGN_DOWN(base - size, align);
  221. } else
  222. continue;
  223. while ((lmbbase <= base) &&
  224. ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) )
  225. base = _ALIGN_DOWN(lmb.reserved.region[j].base - size,
  226. align);
  227. if ((base != 0) && (lmbbase <= base))
  228. break;
  229. }
  230. if (i < 0)
  231. return 0;
  232. lmb_add_region(&lmb.reserved, base, size);
  233. return base;
  234. }
  235. /* You must call lmb_analyze() before this. */
  236. unsigned long __init lmb_phys_mem_size(void)
  237. {
  238. return lmb.memory.size;
  239. }
  240. unsigned long __init lmb_end_of_DRAM(void)
  241. {
  242. int idx = lmb.memory.cnt - 1;
  243. return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
  244. }
  245. /* You must call lmb_analyze() after this. */
  246. void __init lmb_enforce_memory_limit(unsigned long memory_limit)
  247. {
  248. unsigned long i, limit;
  249. struct lmb_property *p;
  250. if (! memory_limit)
  251. return;
  252. /* Truncate the lmb regions to satisfy the memory limit. */
  253. limit = memory_limit;
  254. for (i = 0; i < lmb.memory.cnt; i++) {
  255. if (limit > lmb.memory.region[i].size) {
  256. limit -= lmb.memory.region[i].size;
  257. continue;
  258. }
  259. lmb.memory.region[i].size = limit;
  260. lmb.memory.cnt = i + 1;
  261. break;
  262. }
  263. if (lmb.memory.region[0].size < lmb.rmo_size)
  264. lmb.rmo_size = lmb.memory.region[0].size;
  265. /* And truncate any reserves above the limit also. */
  266. for (i = 0; i < lmb.reserved.cnt; i++) {
  267. p = &lmb.reserved.region[i];
  268. if (p->base > memory_limit)
  269. p->size = 0;
  270. else if ((p->base + p->size) > memory_limit)
  271. p->size = memory_limit - p->base;
  272. if (p->size == 0) {
  273. lmb_remove_region(&lmb.reserved, i);
  274. i--;
  275. }
  276. }
  277. }