blob: 549fbb3d70cf523f404c9e1ee7fd78158684c77f [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
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100018#undef DEBUG
19
Michael Ellermaneb481892005-11-15 14:49:22 +110020#ifdef DEBUG
David S. Millerd9b2b2a2008-02-13 16:56:49 -080021#define DBG(fmt...) LMB_DBG(fmt)
Michael Ellermaneb481892005-11-15 14:49:22 +110022#else
23#define DBG(fmt...)
24#endif
25
Michael Ellerman3b9331d2006-01-25 21:31:30 +130026#define LMB_ALLOC_ANYWHERE 0
27
Michael Ellermaneb481892005-11-15 14:49:22 +110028struct lmb lmb;
29
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100030void lmb_dump_all(void)
31{
32#ifdef DEBUG
33 unsigned long i;
34
Michael Ellermaneb481892005-11-15 14:49:22 +110035 DBG("lmb_dump_all:\n");
36 DBG(" memory.cnt = 0x%lx\n", lmb.memory.cnt);
Becky Brucee5f27092008-02-13 16:58:39 -080037 DBG(" memory.size = 0x%llx\n",
38 (unsigned long long)lmb.memory.size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100039 for (i=0; i < lmb.memory.cnt ;i++) {
Becky Brucee5f27092008-02-13 16:58:39 -080040 DBG(" memory.region[0x%x].base = 0x%llx\n",
41 i, (unsigned long long)lmb.memory.region[i].base);
42 DBG(" .size = 0x%llx\n",
43 (unsigned long long)lmb.memory.region[i].size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100044 }
45
Michael Ellermaneb481892005-11-15 14:49:22 +110046 DBG("\n reserved.cnt = 0x%lx\n", lmb.reserved.cnt);
47 DBG(" reserved.size = 0x%lx\n", lmb.reserved.size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100048 for (i=0; i < lmb.reserved.cnt ;i++) {
Becky Brucee5f27092008-02-13 16:58:39 -080049 DBG(" reserved.region[0x%x].base = 0x%llx\n",
50 i, (unsigned long long)lmb.reserved.region[i].base);
51 DBG(" .size = 0x%llx\n",
52 (unsigned long long)lmb.reserved.region[i].size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100053 }
54#endif /* DEBUG */
55}
56
Becky Brucee5f27092008-02-13 16:58:39 -080057static unsigned long __init lmb_addrs_overlap(u64 base1,
58 u64 size1, u64 base2, u64 size2)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100059{
60 return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
61}
62
Becky Brucee5f27092008-02-13 16:58:39 -080063static long __init lmb_addrs_adjacent(u64 base1, u64 size1,
64 u64 base2, u64 size2)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100065{
66 if (base2 == base1 + size1)
67 return 1;
68 else if (base1 == base2 + size2)
69 return -1;
70
71 return 0;
72}
73
74static long __init lmb_regions_adjacent(struct lmb_region *rgn,
75 unsigned long r1, unsigned long r2)
76{
Becky Brucee5f27092008-02-13 16:58:39 -080077 u64 base1 = rgn->region[r1].base;
78 u64 size1 = rgn->region[r1].size;
79 u64 base2 = rgn->region[r2].base;
80 u64 size2 = rgn->region[r2].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100081
82 return lmb_addrs_adjacent(base1, size1, base2, size2);
83}
84
Michael Ellerman2babf5c2006-05-17 18:00:46 +100085static void __init lmb_remove_region(struct lmb_region *rgn, unsigned long r)
86{
87 unsigned long i;
88
89 for (i = r; i < rgn->cnt - 1; i++) {
90 rgn->region[i].base = rgn->region[i + 1].base;
91 rgn->region[i].size = rgn->region[i + 1].size;
92 }
93 rgn->cnt--;
94}
95
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100096/* Assumption: base addr of region 1 < base addr of region 2 */
97static void __init lmb_coalesce_regions(struct lmb_region *rgn,
98 unsigned long r1, unsigned long r2)
99{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000100 rgn->region[r1].size += rgn->region[r2].size;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000101 lmb_remove_region(rgn, r2);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000102}
103
104/* This routine called with relocation disabled. */
105void __init lmb_init(void)
106{
107 /* Create a dummy zero size LMB which will get coalesced away later.
108 * This simplifies the lmb_add() code below...
109 */
110 lmb.memory.region[0].base = 0;
111 lmb.memory.region[0].size = 0;
112 lmb.memory.cnt = 1;
113
114 /* Ditto. */
115 lmb.reserved.region[0].base = 0;
116 lmb.reserved.region[0].size = 0;
117 lmb.reserved.cnt = 1;
118}
119
120/* This routine may be called with relocation disabled. */
121void __init lmb_analyze(void)
122{
123 int i;
124
125 lmb.memory.size = 0;
126
127 for (i = 0; i < lmb.memory.cnt; i++)
128 lmb.memory.size += lmb.memory.region[i].size;
129}
130
131/* This routine called with relocation disabled. */
Becky Brucee5f27092008-02-13 16:58:39 -0800132static long __init lmb_add_region(struct lmb_region *rgn, u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000133{
Manish Ahuja56d6d1a2007-07-10 05:03:45 +1000134 unsigned long coalesced = 0;
135 long adjacent, i;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000136
Kumar Gala27e66722008-02-13 16:58:11 -0800137 if ((rgn->cnt == 1) && (rgn->region[0].size == 0)) {
138 rgn->region[0].base = base;
139 rgn->region[0].size = size;
140 return 0;
141 }
142
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000143 /* First try and coalesce this LMB with another. */
144 for (i=0; i < rgn->cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800145 u64 rgnbase = rgn->region[i].base;
146 u64 rgnsize = rgn->region[i].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000147
David Gibsoneb6de282007-02-28 14:12:29 +1100148 if ((rgnbase == base) && (rgnsize == size))
149 /* Already have this region, so we're done */
150 return 0;
151
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000152 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;
158 }
159 else if ( adjacent < 0 ) {
160 rgn->region[i].size += size;
161 coalesced++;
162 break;
163 }
164 }
165
166 if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
167 lmb_coalesce_regions(rgn, i, i+1);
168 coalesced++;
169 }
170
171 if (coalesced)
172 return coalesced;
173 if (rgn->cnt >= MAX_LMB_REGIONS)
174 return -1;
175
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;
185 }
186 }
Kumar Gala74b20da2008-02-19 21:28:18 -0800187
188 if (base < rgn->region[0].base) {
189 rgn->region[0].base = base;
190 rgn->region[0].size = size;
191 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000192 rgn->cnt++;
193
194 return 0;
195}
196
197/* This routine may be called with relocation disabled. */
Becky Brucee5f27092008-02-13 16:58:39 -0800198long __init lmb_add(u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000199{
200 struct lmb_region *_rgn = &(lmb.memory);
201
202 /* On pSeries LPAR systems, the first LMB is our RMO region. */
203 if (base == 0)
204 lmb.rmo_size = size;
205
206 return lmb_add_region(_rgn, base, size);
207
208}
209
Becky Brucee5f27092008-02-13 16:58:39 -0800210long __init lmb_reserve(u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000211{
212 struct lmb_region *_rgn = &(lmb.reserved);
213
Michael Ellerman8c20faf2006-01-25 21:31:26 +1300214 BUG_ON(0 == size);
215
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000216 return lmb_add_region(_rgn, base, size);
217}
218
Becky Brucee5f27092008-02-13 16:58:39 -0800219long __init lmb_overlaps_region(struct lmb_region *rgn, u64 base,
220 u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000221{
222 unsigned long i;
223
224 for (i=0; i < rgn->cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800225 u64 rgnbase = rgn->region[i].base;
226 u64 rgnsize = rgn->region[i].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000227 if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
228 break;
229 }
230 }
231
232 return (i < rgn->cnt) ? i : -1;
233}
234
David S. Millerc50f68c2008-03-24 20:50:48 +1100235static u64 lmb_align_down(u64 addr, u64 size)
236{
237 return addr & ~(size - 1);
238}
239
240static u64 lmb_align_up(u64 addr, u64 size)
241{
242 return (addr + (size - 1)) & ~(size - 1);
243}
244
245static u64 __init lmb_alloc_nid_unreserved(u64 start, u64 end,
246 u64 size, u64 align)
247{
248 u64 base;
249 long j;
250
251 base = lmb_align_down((end - size), align);
252 while (start <= base &&
253 ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0))
254 base = lmb_align_down(lmb.reserved.region[j].base - size,
255 align);
256
257 if (base != 0 && start <= base) {
258 if (lmb_add_region(&lmb.reserved, base,
259 lmb_align_up(size, align)) < 0)
260 base = ~(u64)0;
261 return base;
262 }
263
264 return ~(u64)0;
265}
266
267static u64 __init lmb_alloc_nid_region(struct lmb_property *mp,
268 u64 (*nid_range)(u64, u64, int *),
269 u64 size, u64 align, int nid)
270{
271 u64 start, end;
272
273 start = mp->base;
274 end = start + mp->size;
275
276 start = lmb_align_up(start, align);
277 while (start < end) {
278 u64 this_end;
279 int this_nid;
280
281 this_end = nid_range(start, end, &this_nid);
282 if (this_nid == nid) {
283 u64 ret = lmb_alloc_nid_unreserved(start, this_end,
284 size, align);
285 if (ret != ~(u64)0)
286 return ret;
287 }
288 start = this_end;
289 }
290
291 return ~(u64)0;
292}
293
294u64 __init lmb_alloc_nid(u64 size, u64 align, int nid,
295 u64 (*nid_range)(u64 start, u64 end, int *nid))
296{
297 struct lmb_region *mem = &lmb.memory;
298 int i;
299
300 for (i = 0; i < mem->cnt; i++) {
301 u64 ret = lmb_alloc_nid_region(&mem->region[i],
302 nid_range,
303 size, align, nid);
304 if (ret != ~(u64)0)
305 return ret;
306 }
307
308 return lmb_alloc(size, align);
309}
310
Becky Brucee5f27092008-02-13 16:58:39 -0800311u64 __init lmb_alloc(u64 size, u64 align)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000312{
313 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
314}
315
Becky Brucee5f27092008-02-13 16:58:39 -0800316u64 __init lmb_alloc_base(u64 size, u64 align, u64 max_addr)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000317{
Becky Brucee5f27092008-02-13 16:58:39 -0800318 u64 alloc;
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300319
320 alloc = __lmb_alloc_base(size, align, max_addr);
321
Michael Ellerman2c276602006-03-16 14:47:20 +1100322 if (alloc == 0)
Becky Brucee5f27092008-02-13 16:58:39 -0800323 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
324 (unsigned long long) size, (unsigned long long) max_addr);
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300325
326 return alloc;
327}
328
Becky Brucee5f27092008-02-13 16:58:39 -0800329u64 __init __lmb_alloc_base(u64 size, u64 align, u64 max_addr)
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300330{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000331 long i, j;
Becky Brucee5f27092008-02-13 16:58:39 -0800332 u64 base = 0;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000333
Michael Ellerman8c20faf2006-01-25 21:31:26 +1300334 BUG_ON(0 == size);
335
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800336 /* On some platforms, make sure we allocate lowmem */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000337 if (max_addr == LMB_ALLOC_ANYWHERE)
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800338 max_addr = LMB_REAL_LIMIT;
339
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000340 for (i = lmb.memory.cnt-1; i >= 0; i--) {
Becky Brucee5f27092008-02-13 16:58:39 -0800341 u64 lmbbase = lmb.memory.region[i].base;
342 u64 lmbsize = lmb.memory.region[i].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000343
344 if (max_addr == LMB_ALLOC_ANYWHERE)
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800345 base = lmb_align_down(lmbbase + lmbsize - size, align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000346 else if (lmbbase < max_addr) {
347 base = min(lmbbase + lmbsize, max_addr);
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800348 base = lmb_align_down(base - size, align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000349 } else
350 continue;
351
352 while ((lmbbase <= base) &&
353 ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) )
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800354 base = lmb_align_down(lmb.reserved.region[j].base - size,
355 align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000356
357 if ((base != 0) && (lmbbase <= base))
358 break;
359 }
360
361 if (i < 0)
362 return 0;
363
David S. Millereea89e12008-02-13 16:57:09 -0800364 if (lmb_add_region(&lmb.reserved, base, lmb_align_up(size, align)) < 0)
365 return 0;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000366
367 return base;
368}
369
370/* You must call lmb_analyze() before this. */
Becky Brucee5f27092008-02-13 16:58:39 -0800371u64 __init lmb_phys_mem_size(void)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000372{
373 return lmb.memory.size;
374}
375
Becky Brucee5f27092008-02-13 16:58:39 -0800376u64 __init lmb_end_of_DRAM(void)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000377{
378 int idx = lmb.memory.cnt - 1;
379
380 return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
381}
382
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000383/* You must call lmb_analyze() after this. */
Becky Brucee5f27092008-02-13 16:58:39 -0800384void __init lmb_enforce_memory_limit(u64 memory_limit)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000385{
Becky Brucee5f27092008-02-13 16:58:39 -0800386 unsigned long i;
387 u64 limit;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000388 struct lmb_property *p;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000389
390 if (! memory_limit)
391 return;
392
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000393 /* Truncate the lmb regions to satisfy the memory limit. */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000394 limit = memory_limit;
395 for (i = 0; i < lmb.memory.cnt; i++) {
396 if (limit > lmb.memory.region[i].size) {
397 limit -= lmb.memory.region[i].size;
398 continue;
399 }
400
401 lmb.memory.region[i].size = limit;
402 lmb.memory.cnt = i + 1;
403 break;
404 }
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000405
Michael Ellerman30f30e12006-07-04 17:13:23 +1000406 if (lmb.memory.region[0].size < lmb.rmo_size)
407 lmb.rmo_size = lmb.memory.region[0].size;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000408
409 /* And truncate any reserves above the limit also. */
410 for (i = 0; i < lmb.reserved.cnt; i++) {
411 p = &lmb.reserved.region[i];
412
413 if (p->base > memory_limit)
414 p->size = 0;
415 else if ((p->base + p->size) > memory_limit)
416 p->size = memory_limit - p->base;
417
418 if (p->size == 0) {
419 lmb_remove_region(&lmb.reserved, i);
420 i--;
421 }
422 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000423}
Kumar Galaf98eeb42008-01-09 11:27:23 -0600424
Becky Brucee5f27092008-02-13 16:58:39 -0800425int __init lmb_is_reserved(u64 addr)
Kumar Galaf98eeb42008-01-09 11:27:23 -0600426{
427 int i;
428
429 for (i = 0; i < lmb.reserved.cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800430 u64 upper = lmb.reserved.region[i].base +
431 lmb.reserved.region[i].size - 1;
Kumar Galaf98eeb42008-01-09 11:27:23 -0600432 if ((addr >= lmb.reserved.region[i].base) && (addr <= upper))
433 return 1;
434 }
435 return 0;
436}