blob: 93445dc8f8180d6184af268cc6954ecca569142c [file] [log] [blame]
Paul Mackerras7c8c6b92005-10-06 12:23:33 +10001/*
2 * Procedures for maintaining information about logical memory blocks.
3 *
4 * Peter Bergner, IBM Corp. June 2001.
5 * Copyright (C) 2001 Peter Bergner.
David S. Millerd9b2b2a2008-02-13 16:56:49 -08006 *
Paul Mackerras7c8c6b92005-10-06 12:23:33 +10007 * 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
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100013#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/bitops.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080016#include <linux/lmb.h>
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100017
Michael Ellerman3b9331d2006-01-25 21:31:30 +130018#define LMB_ALLOC_ANYWHERE 0
19
Michael Ellermaneb481892005-11-15 14:49:22 +110020struct lmb lmb;
21
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100022void lmb_dump_all(void)
23{
24#ifdef DEBUG
25 unsigned long i;
26
Paul Mackerras300613e2008-04-12 15:20:59 +100027 pr_debug("lmb_dump_all:\n");
28 pr_debug(" memory.cnt = 0x%lx\n", lmb.memory.cnt);
29 pr_debug(" memory.size = 0x%llx\n",
Becky Brucee5f27092008-02-13 16:58:39 -080030 (unsigned long long)lmb.memory.size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100031 for (i=0; i < lmb.memory.cnt ;i++) {
Paul Mackerras300613e2008-04-12 15:20:59 +100032 pr_debug(" memory.region[0x%x].base = 0x%llx\n",
Becky Brucee5f27092008-02-13 16:58:39 -080033 i, (unsigned long long)lmb.memory.region[i].base);
Paul Mackerras300613e2008-04-12 15:20:59 +100034 pr_debug(" .size = 0x%llx\n",
Becky Brucee5f27092008-02-13 16:58:39 -080035 (unsigned long long)lmb.memory.region[i].size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100036 }
37
Paul Mackerras300613e2008-04-12 15:20:59 +100038 pr_debug(" reserved.cnt = 0x%lx\n", lmb.reserved.cnt);
39 pr_debug(" reserved.size = 0x%lx\n", lmb.reserved.size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100040 for (i=0; i < lmb.reserved.cnt ;i++) {
Paul Mackerras300613e2008-04-12 15:20:59 +100041 pr_debug(" reserved.region[0x%x].base = 0x%llx\n",
Becky Brucee5f27092008-02-13 16:58:39 -080042 i, (unsigned long long)lmb.reserved.region[i].base);
Paul Mackerras300613e2008-04-12 15:20:59 +100043 pr_debug(" .size = 0x%llx\n",
Becky Brucee5f27092008-02-13 16:58:39 -080044 (unsigned long long)lmb.reserved.region[i].size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100045 }
46#endif /* DEBUG */
47}
48
Badari Pulavarty98d5c212008-04-18 13:33:52 -070049static unsigned long lmb_addrs_overlap(u64 base1, u64 size1, u64 base2,
50 u64 size2)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100051{
Paul Mackerras300613e2008-04-12 15:20:59 +100052 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100053}
54
Badari Pulavarty98d5c212008-04-18 13:33:52 -070055static long lmb_addrs_adjacent(u64 base1, u64 size1, u64 base2, u64 size2)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100056{
57 if (base2 == base1 + size1)
58 return 1;
59 else if (base1 == base2 + size2)
60 return -1;
61
62 return 0;
63}
64
Badari Pulavarty98d5c212008-04-18 13:33:52 -070065static long lmb_regions_adjacent(struct lmb_region *rgn,
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100066 unsigned long r1, unsigned long r2)
67{
Becky Brucee5f27092008-02-13 16:58:39 -080068 u64 base1 = rgn->region[r1].base;
69 u64 size1 = rgn->region[r1].size;
70 u64 base2 = rgn->region[r2].base;
71 u64 size2 = rgn->region[r2].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100072
73 return lmb_addrs_adjacent(base1, size1, base2, size2);
74}
75
Badari Pulavarty98d5c212008-04-18 13:33:52 -070076static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
Michael Ellerman2babf5c2006-05-17 18:00:46 +100077{
78 unsigned long i;
79
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
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100087/* Assumption: base addr of region 1 < base addr of region 2 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -070088static void lmb_coalesce_regions(struct lmb_region *rgn,
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100089 unsigned long r1, unsigned long r2)
90{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100091 rgn->region[r1].size += rgn->region[r2].size;
Michael Ellerman2babf5c2006-05-17 18:00:46 +100092 lmb_remove_region(rgn, r2);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100093}
94
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100095void __init lmb_init(void)
96{
97 /* Create a dummy zero size LMB which will get coalesced away later.
98 * This simplifies the lmb_add() code below...
99 */
100 lmb.memory.region[0].base = 0;
101 lmb.memory.region[0].size = 0;
102 lmb.memory.cnt = 1;
103
104 /* Ditto. */
105 lmb.reserved.region[0].base = 0;
106 lmb.reserved.region[0].size = 0;
107 lmb.reserved.cnt = 1;
108}
109
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000110void __init lmb_analyze(void)
111{
112 int i;
113
114 lmb.memory.size = 0;
115
116 for (i = 0; i < lmb.memory.cnt; i++)
117 lmb.memory.size += lmb.memory.region[i].size;
118}
119
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700120static long lmb_add_region(struct lmb_region *rgn, u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000121{
Manish Ahuja56d6d1a2007-07-10 05:03:45 +1000122 unsigned long coalesced = 0;
123 long adjacent, i;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000124
Kumar Gala27e66722008-02-13 16:58:11 -0800125 if ((rgn->cnt == 1) && (rgn->region[0].size == 0)) {
126 rgn->region[0].base = base;
127 rgn->region[0].size = size;
128 return 0;
129 }
130
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000131 /* First try and coalesce this LMB with another. */
Paul Mackerras300613e2008-04-12 15:20:59 +1000132 for (i = 0; i < rgn->cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800133 u64 rgnbase = rgn->region[i].base;
134 u64 rgnsize = rgn->region[i].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000135
David Gibsoneb6de282007-02-28 14:12:29 +1100136 if ((rgnbase == base) && (rgnsize == size))
137 /* Already have this region, so we're done */
138 return 0;
139
Paul Mackerras300613e2008-04-12 15:20:59 +1000140 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
141 if (adjacent > 0) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000142 rgn->region[i].base -= size;
143 rgn->region[i].size += size;
144 coalesced++;
145 break;
Paul Mackerras300613e2008-04-12 15:20:59 +1000146 } else if (adjacent < 0) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000147 rgn->region[i].size += size;
148 coalesced++;
149 break;
150 }
151 }
152
Paul Mackerras300613e2008-04-12 15:20:59 +1000153 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i+1)) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000154 lmb_coalesce_regions(rgn, i, i+1);
155 coalesced++;
156 }
157
158 if (coalesced)
159 return coalesced;
160 if (rgn->cnt >= MAX_LMB_REGIONS)
161 return -1;
162
163 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Paul Mackerras300613e2008-04-12 15:20:59 +1000164 for (i = rgn->cnt - 1; i >= 0; i--) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000165 if (base < rgn->region[i].base) {
166 rgn->region[i+1].base = rgn->region[i].base;
167 rgn->region[i+1].size = rgn->region[i].size;
168 } else {
169 rgn->region[i+1].base = base;
170 rgn->region[i+1].size = size;
171 break;
172 }
173 }
Kumar Gala74b20da2008-02-19 21:28:18 -0800174
175 if (base < rgn->region[0].base) {
176 rgn->region[0].base = base;
177 rgn->region[0].size = size;
178 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000179 rgn->cnt++;
180
181 return 0;
182}
183
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700184long lmb_add(u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000185{
Paul Mackerras300613e2008-04-12 15:20:59 +1000186 struct lmb_region *_rgn = &lmb.memory;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000187
188 /* On pSeries LPAR systems, the first LMB is our RMO region. */
189 if (base == 0)
190 lmb.rmo_size = size;
191
192 return lmb_add_region(_rgn, base, size);
193
194}
195
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700196long lmb_remove(u64 base, u64 size)
197{
198 struct lmb_region *rgn = &(lmb.memory);
199 u64 rgnbegin, rgnend;
200 u64 end = base + size;
201 int i;
202
203 rgnbegin = rgnend = 0; /* supress gcc warnings */
204
205 /* Find the region where (base, size) belongs to */
206 for (i=0; i < rgn->cnt; i++) {
207 rgnbegin = rgn->region[i].base;
208 rgnend = rgnbegin + rgn->region[i].size;
209
210 if ((rgnbegin <= base) && (end <= rgnend))
211 break;
212 }
213
214 /* Didn't find the region */
215 if (i == rgn->cnt)
216 return -1;
217
218 /* Check to see if we are removing entire region */
219 if ((rgnbegin == base) && (rgnend == end)) {
220 lmb_remove_region(rgn, i);
221 return 0;
222 }
223
224 /* Check to see if region is matching at the front */
225 if (rgnbegin == base) {
226 rgn->region[i].base = end;
227 rgn->region[i].size -= size;
228 return 0;
229 }
230
231 /* Check to see if the region is matching at the end */
232 if (rgnend == end) {
233 rgn->region[i].size -= size;
234 return 0;
235 }
236
237 /*
238 * We need to split the entry - adjust the current one to the
239 * beginging of the hole and add the region after hole.
240 */
241 rgn->region[i].size = base - rgn->region[i].base;
242 return lmb_add_region(rgn, end, rgnend - end);
243}
244
Becky Brucee5f27092008-02-13 16:58:39 -0800245long __init lmb_reserve(u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000246{
Paul Mackerras300613e2008-04-12 15:20:59 +1000247 struct lmb_region *_rgn = &lmb.reserved;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000248
Michael Ellerman8c20faf2006-01-25 21:31:26 +1300249 BUG_ON(0 == size);
250
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000251 return lmb_add_region(_rgn, base, size);
252}
253
Paul Mackerras300613e2008-04-12 15:20:59 +1000254long __init lmb_overlaps_region(struct lmb_region *rgn, u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000255{
256 unsigned long i;
257
Paul Mackerras300613e2008-04-12 15:20:59 +1000258 for (i = 0; i < rgn->cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800259 u64 rgnbase = rgn->region[i].base;
260 u64 rgnsize = rgn->region[i].size;
Paul Mackerras300613e2008-04-12 15:20:59 +1000261 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000262 break;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000263 }
264
265 return (i < rgn->cnt) ? i : -1;
266}
267
David S. Millerc50f68c2008-03-24 20:50:48 +1100268static u64 lmb_align_down(u64 addr, u64 size)
269{
270 return addr & ~(size - 1);
271}
272
273static u64 lmb_align_up(u64 addr, u64 size)
274{
275 return (addr + (size - 1)) & ~(size - 1);
276}
277
278static u64 __init lmb_alloc_nid_unreserved(u64 start, u64 end,
279 u64 size, u64 align)
280{
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000281 u64 base, res_base;
David S. Millerc50f68c2008-03-24 20:50:48 +1100282 long j;
283
284 base = lmb_align_down((end - size), align);
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000285 while (start <= base) {
286 j = lmb_overlaps_region(&lmb.reserved, base, size);
287 if (j < 0) {
288 /* this area isn't reserved, take it */
David S. Miller4978db52008-05-12 16:51:15 -0700289 if (lmb_add_region(&lmb.reserved, base, size) < 0)
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000290 base = ~(u64)0;
291 return base;
292 }
293 res_base = lmb.reserved.region[j].base;
294 if (res_base < size)
295 break;
296 base = lmb_align_down(res_base - size, align);
David S. Millerc50f68c2008-03-24 20:50:48 +1100297 }
298
299 return ~(u64)0;
300}
301
302static u64 __init lmb_alloc_nid_region(struct lmb_property *mp,
303 u64 (*nid_range)(u64, u64, int *),
304 u64 size, u64 align, int nid)
305{
306 u64 start, end;
307
308 start = mp->base;
309 end = start + mp->size;
310
311 start = lmb_align_up(start, align);
312 while (start < end) {
313 u64 this_end;
314 int this_nid;
315
316 this_end = nid_range(start, end, &this_nid);
317 if (this_nid == nid) {
318 u64 ret = lmb_alloc_nid_unreserved(start, this_end,
319 size, align);
320 if (ret != ~(u64)0)
321 return ret;
322 }
323 start = this_end;
324 }
325
326 return ~(u64)0;
327}
328
329u64 __init lmb_alloc_nid(u64 size, u64 align, int nid,
330 u64 (*nid_range)(u64 start, u64 end, int *nid))
331{
332 struct lmb_region *mem = &lmb.memory;
333 int i;
334
David S. Miller4978db52008-05-12 16:51:15 -0700335 BUG_ON(0 == size);
336
337 size = lmb_align_up(size, align);
338
David S. Millerc50f68c2008-03-24 20:50:48 +1100339 for (i = 0; i < mem->cnt; i++) {
340 u64 ret = lmb_alloc_nid_region(&mem->region[i],
341 nid_range,
342 size, align, nid);
343 if (ret != ~(u64)0)
344 return ret;
345 }
346
347 return lmb_alloc(size, align);
348}
349
Becky Brucee5f27092008-02-13 16:58:39 -0800350u64 __init lmb_alloc(u64 size, u64 align)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000351{
352 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
353}
354
Becky Brucee5f27092008-02-13 16:58:39 -0800355u64 __init lmb_alloc_base(u64 size, u64 align, u64 max_addr)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000356{
Becky Brucee5f27092008-02-13 16:58:39 -0800357 u64 alloc;
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300358
359 alloc = __lmb_alloc_base(size, align, max_addr);
360
Michael Ellerman2c276602006-03-16 14:47:20 +1100361 if (alloc == 0)
Becky Brucee5f27092008-02-13 16:58:39 -0800362 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
363 (unsigned long long) size, (unsigned long long) max_addr);
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300364
365 return alloc;
366}
367
Becky Brucee5f27092008-02-13 16:58:39 -0800368u64 __init __lmb_alloc_base(u64 size, u64 align, u64 max_addr)
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300369{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000370 long i, j;
Becky Brucee5f27092008-02-13 16:58:39 -0800371 u64 base = 0;
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000372 u64 res_base;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000373
Michael Ellerman8c20faf2006-01-25 21:31:26 +1300374 BUG_ON(0 == size);
375
David S. Miller4978db52008-05-12 16:51:15 -0700376 size = lmb_align_up(size, align);
377
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800378 /* On some platforms, make sure we allocate lowmem */
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000379 /* Note that LMB_REAL_LIMIT may be LMB_ALLOC_ANYWHERE */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000380 if (max_addr == LMB_ALLOC_ANYWHERE)
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800381 max_addr = LMB_REAL_LIMIT;
382
Paul Mackerras300613e2008-04-12 15:20:59 +1000383 for (i = lmb.memory.cnt - 1; i >= 0; i--) {
Becky Brucee5f27092008-02-13 16:58:39 -0800384 u64 lmbbase = lmb.memory.region[i].base;
385 u64 lmbsize = lmb.memory.region[i].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000386
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000387 if (lmbsize < size)
388 continue;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000389 if (max_addr == LMB_ALLOC_ANYWHERE)
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800390 base = lmb_align_down(lmbbase + lmbsize - size, align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000391 else if (lmbbase < max_addr) {
392 base = min(lmbbase + lmbsize, max_addr);
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800393 base = lmb_align_down(base - size, align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000394 } else
395 continue;
396
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000397 while (base && lmbbase <= base) {
Paul Mackerras300613e2008-04-12 15:20:59 +1000398 j = lmb_overlaps_region(&lmb.reserved, base, size);
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000399 if (j < 0) {
400 /* this area isn't reserved, take it */
David S. Miller4978db52008-05-12 16:51:15 -0700401 if (lmb_add_region(&lmb.reserved, base, size) < 0)
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000402 return 0;
403 return base;
404 }
405 res_base = lmb.reserved.region[j].base;
406 if (res_base < size)
Paul Mackerras300613e2008-04-12 15:20:59 +1000407 break;
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000408 base = lmb_align_down(res_base - size, align);
Paul Mackerras300613e2008-04-12 15:20:59 +1000409 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000410 }
Paul Mackerrasd9024df2008-04-12 15:20:59 +1000411 return 0;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000412}
413
414/* You must call lmb_analyze() before this. */
Becky Brucee5f27092008-02-13 16:58:39 -0800415u64 __init lmb_phys_mem_size(void)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000416{
417 return lmb.memory.size;
418}
419
Becky Brucee5f27092008-02-13 16:58:39 -0800420u64 __init lmb_end_of_DRAM(void)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000421{
422 int idx = lmb.memory.cnt - 1;
423
424 return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
425}
426
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000427/* You must call lmb_analyze() after this. */
Becky Brucee5f27092008-02-13 16:58:39 -0800428void __init lmb_enforce_memory_limit(u64 memory_limit)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000429{
Becky Brucee5f27092008-02-13 16:58:39 -0800430 unsigned long i;
431 u64 limit;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000432 struct lmb_property *p;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000433
Paul Mackerras300613e2008-04-12 15:20:59 +1000434 if (!memory_limit)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000435 return;
436
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000437 /* Truncate the lmb regions to satisfy the memory limit. */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000438 limit = memory_limit;
439 for (i = 0; i < lmb.memory.cnt; i++) {
440 if (limit > lmb.memory.region[i].size) {
441 limit -= lmb.memory.region[i].size;
442 continue;
443 }
444
445 lmb.memory.region[i].size = limit;
446 lmb.memory.cnt = i + 1;
447 break;
448 }
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000449
Michael Ellerman30f30e12006-07-04 17:13:23 +1000450 if (lmb.memory.region[0].size < lmb.rmo_size)
451 lmb.rmo_size = lmb.memory.region[0].size;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000452
453 /* And truncate any reserves above the limit also. */
454 for (i = 0; i < lmb.reserved.cnt; i++) {
455 p = &lmb.reserved.region[i];
456
457 if (p->base > memory_limit)
458 p->size = 0;
459 else if ((p->base + p->size) > memory_limit)
460 p->size = memory_limit - p->base;
461
462 if (p->size == 0) {
463 lmb_remove_region(&lmb.reserved, i);
464 i--;
465 }
466 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000467}
Kumar Galaf98eeb42008-01-09 11:27:23 -0600468
Becky Brucee5f27092008-02-13 16:58:39 -0800469int __init lmb_is_reserved(u64 addr)
Kumar Galaf98eeb42008-01-09 11:27:23 -0600470{
471 int i;
472
473 for (i = 0; i < lmb.reserved.cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800474 u64 upper = lmb.reserved.region[i].base +
475 lmb.reserved.region[i].size - 1;
Kumar Galaf98eeb42008-01-09 11:27:23 -0600476 if ((addr >= lmb.reserved.region[i].base) && (addr <= upper))
477 return 1;
478 }
479 return 0;
480}
Badari Pulavarty9d88a2e2008-04-18 13:33:53 -0700481
482/*
483 * Given a <base, len>, find which memory regions belong to this range.
484 * Adjust the request and return a contiguous chunk.
485 */
486int lmb_find(struct lmb_property *res)
487{
488 int i;
489 u64 rstart, rend;
490
491 rstart = res->base;
492 rend = rstart + res->size - 1;
493
494 for (i = 0; i < lmb.memory.cnt; i++) {
495 u64 start = lmb.memory.region[i].base;
496 u64 end = start + lmb.memory.region[i].size - 1;
497
498 if (start > rend)
499 return -1;
500
501 if ((end >= rstart) && (start < rend)) {
502 /* adjust the request */
503 if (rstart < start)
504 rstart = start;
505 if (rend > end)
506 rend = end;
507 res->base = rstart;
508 res->size = rend - rstart + 1;
509 return 0;
510 }
511 }
512 return -1;
513}