lmb.c 8.6 KB

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