[LMB]: Fix initial lmb add region with a non-zero base
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / lib / lmb.c
blobe34a9e586c424a3e4e55508c4048115ee6ededf6
1 /*
2 * Procedures for maintaining information about logical memory blocks.
4 * Peter Bergner, IBM Corp. June 2001.
5 * Copyright (C) 2001 Peter Bergner.
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.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/bitops.h>
16 #include <linux/lmb.h>
18 #undef DEBUG
20 #ifdef DEBUG
21 #define DBG(fmt...) LMB_DBG(fmt)
22 #else
23 #define DBG(fmt...)
24 #endif
26 #define LMB_ALLOC_ANYWHERE 0
28 struct lmb lmb;
30 void lmb_dump_all(void)
32 #ifdef DEBUG
33 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);
45 DBG("\n reserved.cnt = 0x%lx\n", lmb.reserved.cnt);
46 DBG(" reserved.size = 0x%lx\n", lmb.reserved.size);
47 for (i=0; i < lmb.reserved.cnt ;i++) {
48 DBG(" reserved.region[0x%x].base = 0x%lx\n",
49 i, lmb.reserved.region[i].base);
50 DBG(" .size = 0x%lx\n",
51 lmb.reserved.region[i].size);
53 #endif /* DEBUG */
56 static unsigned long __init lmb_addrs_overlap(unsigned long base1,
57 unsigned long size1, unsigned long base2, unsigned long size2)
59 return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
62 static long __init lmb_addrs_adjacent(unsigned long base1, unsigned long size1,
63 unsigned long base2, unsigned long size2)
65 if (base2 == base1 + size1)
66 return 1;
67 else if (base1 == base2 + size2)
68 return -1;
70 return 0;
73 static long __init lmb_regions_adjacent(struct lmb_region *rgn,
74 unsigned long r1, unsigned long r2)
76 unsigned long base1 = rgn->region[r1].base;
77 unsigned long size1 = rgn->region[r1].size;
78 unsigned long base2 = rgn->region[r2].base;
79 unsigned long size2 = rgn->region[r2].size;
81 return lmb_addrs_adjacent(base1, size1, base2, size2);
84 static void __init lmb_remove_region(struct lmb_region *rgn, unsigned long r)
86 unsigned long i;
88 for (i = r; i < rgn->cnt - 1; i++) {
89 rgn->region[i].base = rgn->region[i + 1].base;
90 rgn->region[i].size = rgn->region[i + 1].size;
92 rgn->cnt--;
95 /* Assumption: base addr of region 1 < base addr of region 2 */
96 static void __init lmb_coalesce_regions(struct lmb_region *rgn,
97 unsigned long r1, unsigned long r2)
99 rgn->region[r1].size += rgn->region[r2].size;
100 lmb_remove_region(rgn, r2);
103 /* This routine called with relocation disabled. */
104 void __init lmb_init(void)
106 /* Create a dummy zero size LMB which will get coalesced away later.
107 * This simplifies the lmb_add() code below...
109 lmb.memory.region[0].base = 0;
110 lmb.memory.region[0].size = 0;
111 lmb.memory.cnt = 1;
113 /* Ditto. */
114 lmb.reserved.region[0].base = 0;
115 lmb.reserved.region[0].size = 0;
116 lmb.reserved.cnt = 1;
119 /* This routine may be called with relocation disabled. */
120 void __init lmb_analyze(void)
122 int i;
124 lmb.memory.size = 0;
126 for (i = 0; i < lmb.memory.cnt; i++)
127 lmb.memory.size += lmb.memory.region[i].size;
130 /* This routine called with relocation disabled. */
131 static long __init lmb_add_region(struct lmb_region *rgn, unsigned long base,
132 unsigned long size)
134 unsigned long coalesced = 0;
135 long adjacent, i;
137 if ((rgn->cnt == 1) && (rgn->region[0].size == 0)) {
138 rgn->region[0].base = base;
139 rgn->region[0].size = size;
140 return 0;
143 /* First try and coalesce this LMB with another. */
144 for (i=0; i < rgn->cnt; i++) {
145 unsigned long rgnbase = rgn->region[i].base;
146 unsigned long rgnsize = rgn->region[i].size;
148 if ((rgnbase == base) && (rgnsize == size))
149 /* Already have this region, so we're done */
150 return 0;
152 adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize);
153 if ( adjacent > 0 ) {
154 rgn->region[i].base -= size;
155 rgn->region[i].size += size;
156 coalesced++;
157 break;
159 else if ( adjacent < 0 ) {
160 rgn->region[i].size += size;
161 coalesced++;
162 break;
166 if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
167 lmb_coalesce_regions(rgn, i, i+1);
168 coalesced++;
171 if (coalesced)
172 return coalesced;
173 if (rgn->cnt >= MAX_LMB_REGIONS)
174 return -1;
176 /* Couldn't coalesce the LMB, so add it to the sorted table. */
177 for (i = rgn->cnt-1; i >= 0; i--) {
178 if (base < rgn->region[i].base) {
179 rgn->region[i+1].base = rgn->region[i].base;
180 rgn->region[i+1].size = rgn->region[i].size;
181 } else {
182 rgn->region[i+1].base = base;
183 rgn->region[i+1].size = size;
184 break;
187 rgn->cnt++;
189 return 0;
192 /* This routine may be called with relocation disabled. */
193 long __init lmb_add(unsigned long base, unsigned long size)
195 struct lmb_region *_rgn = &(lmb.memory);
197 /* On pSeries LPAR systems, the first LMB is our RMO region. */
198 if (base == 0)
199 lmb.rmo_size = size;
201 return lmb_add_region(_rgn, base, size);
205 long __init lmb_reserve(unsigned long base, unsigned long size)
207 struct lmb_region *_rgn = &(lmb.reserved);
209 BUG_ON(0 == size);
211 return lmb_add_region(_rgn, base, size);
214 long __init lmb_overlaps_region(struct lmb_region *rgn, unsigned long base,
215 unsigned long size)
217 unsigned long i;
219 for (i=0; i < rgn->cnt; i++) {
220 unsigned long rgnbase = rgn->region[i].base;
221 unsigned long rgnsize = rgn->region[i].size;
222 if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
223 break;
227 return (i < rgn->cnt) ? i : -1;
230 unsigned long __init lmb_alloc(unsigned long size, unsigned long align)
232 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
235 unsigned long __init lmb_alloc_base(unsigned long size, unsigned long align,
236 unsigned long max_addr)
238 unsigned long alloc;
240 alloc = __lmb_alloc_base(size, align, max_addr);
242 if (alloc == 0)
243 panic("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
244 size, max_addr);
246 return alloc;
249 static unsigned long lmb_align_down(unsigned long addr, unsigned long size)
251 return addr & ~(size - 1);
254 static unsigned long lmb_align_up(unsigned long addr, unsigned long size)
256 return (addr + (size - 1)) & ~(size - 1);
259 unsigned long __init __lmb_alloc_base(unsigned long size, unsigned long align,
260 unsigned long max_addr)
262 long i, j;
263 unsigned long base = 0;
265 BUG_ON(0 == size);
267 /* On some platforms, make sure we allocate lowmem */
268 if (max_addr == LMB_ALLOC_ANYWHERE)
269 max_addr = LMB_REAL_LIMIT;
271 for (i = lmb.memory.cnt-1; i >= 0; i--) {
272 unsigned long lmbbase = lmb.memory.region[i].base;
273 unsigned long lmbsize = lmb.memory.region[i].size;
275 if (max_addr == LMB_ALLOC_ANYWHERE)
276 base = lmb_align_down(lmbbase + lmbsize - size, align);
277 else if (lmbbase < max_addr) {
278 base = min(lmbbase + lmbsize, max_addr);
279 base = lmb_align_down(base - size, align);
280 } else
281 continue;
283 while ((lmbbase <= base) &&
284 ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) )
285 base = lmb_align_down(lmb.reserved.region[j].base - size,
286 align);
288 if ((base != 0) && (lmbbase <= base))
289 break;
292 if (i < 0)
293 return 0;
295 if (lmb_add_region(&lmb.reserved, base, lmb_align_up(size, align)) < 0)
296 return 0;
298 return base;
301 /* You must call lmb_analyze() before this. */
302 unsigned long __init lmb_phys_mem_size(void)
304 return lmb.memory.size;
307 unsigned long __init lmb_end_of_DRAM(void)
309 int idx = lmb.memory.cnt - 1;
311 return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
314 /* You must call lmb_analyze() after this. */
315 void __init lmb_enforce_memory_limit(unsigned long memory_limit)
317 unsigned long i, limit;
318 struct lmb_property *p;
320 if (! memory_limit)
321 return;
323 /* Truncate the lmb regions to satisfy the memory limit. */
324 limit = memory_limit;
325 for (i = 0; i < lmb.memory.cnt; i++) {
326 if (limit > lmb.memory.region[i].size) {
327 limit -= lmb.memory.region[i].size;
328 continue;
331 lmb.memory.region[i].size = limit;
332 lmb.memory.cnt = i + 1;
333 break;
336 if (lmb.memory.region[0].size < lmb.rmo_size)
337 lmb.rmo_size = lmb.memory.region[0].size;
339 /* And truncate any reserves above the limit also. */
340 for (i = 0; i < lmb.reserved.cnt; i++) {
341 p = &lmb.reserved.region[i];
343 if (p->base > memory_limit)
344 p->size = 0;
345 else if ((p->base + p->size) > memory_limit)
346 p->size = memory_limit - p->base;
348 if (p->size == 0) {
349 lmb_remove_region(&lmb.reserved, i);
350 i--;
355 int __init lmb_is_reserved(unsigned long addr)
357 int i;
359 for (i = 0; i < lmb.reserved.cnt; i++) {
360 unsigned long upper = lmb.reserved.region[i].base +
361 lmb.reserved.region[i].size - 1;
362 if ((addr >= lmb.reserved.region[i].base) && (addr <= upper))
363 return 1;
365 return 0;