blob: e4bbc617f46845defd69119be27e28cacb464ee8 [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
Paul Mackerras300613e2008-04-12 15:20:59 +100049static unsigned long __init lmb_addrs_overlap(u64 base1, u64 size1,
50 u64 base2, 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
Becky Brucee5f27092008-02-13 16:58:39 -080055static long __init lmb_addrs_adjacent(u64 base1, u64 size1,
56 u64 base2, u64 size2)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100057{
58 if (base2 == base1 + size1)
59 return 1;
60 else if (base1 == base2 + size2)
61 return -1;
62
63 return 0;
64}
65
66static long __init lmb_regions_adjacent(struct lmb_region *rgn,
67 unsigned long r1, unsigned long r2)
68{
Becky Brucee5f27092008-02-13 16:58:39 -080069 u64 base1 = rgn->region[r1].base;
70 u64 size1 = rgn->region[r1].size;
71 u64 base2 = rgn->region[r2].base;
72 u64 size2 = rgn->region[r2].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100073
74 return lmb_addrs_adjacent(base1, size1, base2, size2);
75}
76
Michael Ellerman2babf5c2006-05-17 18:00:46 +100077static void __init lmb_remove_region(struct lmb_region *rgn, unsigned long r)
78{
79 unsigned long i;
80
81 for (i = r; i < rgn->cnt - 1; i++) {
82 rgn->region[i].base = rgn->region[i + 1].base;
83 rgn->region[i].size = rgn->region[i + 1].size;
84 }
85 rgn->cnt--;
86}
87
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100088/* Assumption: base addr of region 1 < base addr of region 2 */
89static void __init lmb_coalesce_regions(struct lmb_region *rgn,
90 unsigned long r1, unsigned long r2)
91{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100092 rgn->region[r1].size += rgn->region[r2].size;
Michael Ellerman2babf5c2006-05-17 18:00:46 +100093 lmb_remove_region(rgn, r2);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100094}
95
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100096void __init lmb_init(void)
97{
98 /* Create a dummy zero size LMB which will get coalesced away later.
99 * This simplifies the lmb_add() code below...
100 */
101 lmb.memory.region[0].base = 0;
102 lmb.memory.region[0].size = 0;
103 lmb.memory.cnt = 1;
104
105 /* Ditto. */
106 lmb.reserved.region[0].base = 0;
107 lmb.reserved.region[0].size = 0;
108 lmb.reserved.cnt = 1;
109}
110
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000111void __init lmb_analyze(void)
112{
113 int i;
114
115 lmb.memory.size = 0;
116
117 for (i = 0; i < lmb.memory.cnt; i++)
118 lmb.memory.size += lmb.memory.region[i].size;
119}
120
Becky Brucee5f27092008-02-13 16:58:39 -0800121static long __init lmb_add_region(struct lmb_region *rgn, u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000122{
Manish Ahuja56d6d1a2007-07-10 05:03:45 +1000123 unsigned long coalesced = 0;
124 long adjacent, i;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000125
Kumar Gala27e66722008-02-13 16:58:11 -0800126 if ((rgn->cnt == 1) && (rgn->region[0].size == 0)) {
127 rgn->region[0].base = base;
128 rgn->region[0].size = size;
129 return 0;
130 }
131
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000132 /* First try and coalesce this LMB with another. */
Paul Mackerras300613e2008-04-12 15:20:59 +1000133 for (i = 0; i < rgn->cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800134 u64 rgnbase = rgn->region[i].base;
135 u64 rgnsize = rgn->region[i].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000136
David Gibsoneb6de282007-02-28 14:12:29 +1100137 if ((rgnbase == base) && (rgnsize == size))
138 /* Already have this region, so we're done */
139 return 0;
140
Paul Mackerras300613e2008-04-12 15:20:59 +1000141 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
142 if (adjacent > 0) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000143 rgn->region[i].base -= size;
144 rgn->region[i].size += size;
145 coalesced++;
146 break;
Paul Mackerras300613e2008-04-12 15:20:59 +1000147 } else if (adjacent < 0) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000148 rgn->region[i].size += size;
149 coalesced++;
150 break;
151 }
152 }
153
Paul Mackerras300613e2008-04-12 15:20:59 +1000154 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i+1)) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000155 lmb_coalesce_regions(rgn, i, i+1);
156 coalesced++;
157 }
158
159 if (coalesced)
160 return coalesced;
161 if (rgn->cnt >= MAX_LMB_REGIONS)
162 return -1;
163
164 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Paul Mackerras300613e2008-04-12 15:20:59 +1000165 for (i = rgn->cnt - 1; i >= 0; i--) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000166 if (base < rgn->region[i].base) {
167 rgn->region[i+1].base = rgn->region[i].base;
168 rgn->region[i+1].size = rgn->region[i].size;
169 } else {
170 rgn->region[i+1].base = base;
171 rgn->region[i+1].size = size;
172 break;
173 }
174 }
Kumar Gala74b20da2008-02-19 21:28:18 -0800175
176 if (base < rgn->region[0].base) {
177 rgn->region[0].base = base;
178 rgn->region[0].size = size;
179 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000180 rgn->cnt++;
181
182 return 0;
183}
184
Becky Brucee5f27092008-02-13 16:58:39 -0800185long __init lmb_add(u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000186{
Paul Mackerras300613e2008-04-12 15:20:59 +1000187 struct lmb_region *_rgn = &lmb.memory;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000188
189 /* On pSeries LPAR systems, the first LMB is our RMO region. */
190 if (base == 0)
191 lmb.rmo_size = size;
192
193 return lmb_add_region(_rgn, base, size);
194
195}
196
Becky Brucee5f27092008-02-13 16:58:39 -0800197long __init lmb_reserve(u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000198{
Paul Mackerras300613e2008-04-12 15:20:59 +1000199 struct lmb_region *_rgn = &lmb.reserved;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000200
Michael Ellerman8c20faf2006-01-25 21:31:26 +1300201 BUG_ON(0 == size);
202
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000203 return lmb_add_region(_rgn, base, size);
204}
205
Paul Mackerras300613e2008-04-12 15:20:59 +1000206long __init lmb_overlaps_region(struct lmb_region *rgn, u64 base, u64 size)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000207{
208 unsigned long i;
209
Paul Mackerras300613e2008-04-12 15:20:59 +1000210 for (i = 0; i < rgn->cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800211 u64 rgnbase = rgn->region[i].base;
212 u64 rgnsize = rgn->region[i].size;
Paul Mackerras300613e2008-04-12 15:20:59 +1000213 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000214 break;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000215 }
216
217 return (i < rgn->cnt) ? i : -1;
218}
219
David S. Millerc50f68c2008-03-24 20:50:48 +1100220static u64 lmb_align_down(u64 addr, u64 size)
221{
222 return addr & ~(size - 1);
223}
224
225static u64 lmb_align_up(u64 addr, u64 size)
226{
227 return (addr + (size - 1)) & ~(size - 1);
228}
229
230static u64 __init lmb_alloc_nid_unreserved(u64 start, u64 end,
231 u64 size, u64 align)
232{
233 u64 base;
234 long j;
235
236 base = lmb_align_down((end - size), align);
237 while (start <= base &&
238 ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0))
239 base = lmb_align_down(lmb.reserved.region[j].base - size,
240 align);
241
242 if (base != 0 && start <= base) {
243 if (lmb_add_region(&lmb.reserved, base,
244 lmb_align_up(size, align)) < 0)
245 base = ~(u64)0;
246 return base;
247 }
248
249 return ~(u64)0;
250}
251
252static u64 __init lmb_alloc_nid_region(struct lmb_property *mp,
253 u64 (*nid_range)(u64, u64, int *),
254 u64 size, u64 align, int nid)
255{
256 u64 start, end;
257
258 start = mp->base;
259 end = start + mp->size;
260
261 start = lmb_align_up(start, align);
262 while (start < end) {
263 u64 this_end;
264 int this_nid;
265
266 this_end = nid_range(start, end, &this_nid);
267 if (this_nid == nid) {
268 u64 ret = lmb_alloc_nid_unreserved(start, this_end,
269 size, align);
270 if (ret != ~(u64)0)
271 return ret;
272 }
273 start = this_end;
274 }
275
276 return ~(u64)0;
277}
278
279u64 __init lmb_alloc_nid(u64 size, u64 align, int nid,
280 u64 (*nid_range)(u64 start, u64 end, int *nid))
281{
282 struct lmb_region *mem = &lmb.memory;
283 int i;
284
285 for (i = 0; i < mem->cnt; i++) {
286 u64 ret = lmb_alloc_nid_region(&mem->region[i],
287 nid_range,
288 size, align, nid);
289 if (ret != ~(u64)0)
290 return ret;
291 }
292
293 return lmb_alloc(size, align);
294}
295
Becky Brucee5f27092008-02-13 16:58:39 -0800296u64 __init lmb_alloc(u64 size, u64 align)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000297{
298 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
299}
300
Becky Brucee5f27092008-02-13 16:58:39 -0800301u64 __init lmb_alloc_base(u64 size, u64 align, u64 max_addr)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000302{
Becky Brucee5f27092008-02-13 16:58:39 -0800303 u64 alloc;
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300304
305 alloc = __lmb_alloc_base(size, align, max_addr);
306
Michael Ellerman2c276602006-03-16 14:47:20 +1100307 if (alloc == 0)
Becky Brucee5f27092008-02-13 16:58:39 -0800308 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
309 (unsigned long long) size, (unsigned long long) max_addr);
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300310
311 return alloc;
312}
313
Becky Brucee5f27092008-02-13 16:58:39 -0800314u64 __init __lmb_alloc_base(u64 size, u64 align, u64 max_addr)
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300315{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000316 long i, j;
Becky Brucee5f27092008-02-13 16:58:39 -0800317 u64 base = 0;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000318
Michael Ellerman8c20faf2006-01-25 21:31:26 +1300319 BUG_ON(0 == size);
320
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800321 /* On some platforms, make sure we allocate lowmem */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000322 if (max_addr == LMB_ALLOC_ANYWHERE)
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800323 max_addr = LMB_REAL_LIMIT;
324
Paul Mackerras300613e2008-04-12 15:20:59 +1000325 for (i = lmb.memory.cnt - 1; i >= 0; i--) {
Becky Brucee5f27092008-02-13 16:58:39 -0800326 u64 lmbbase = lmb.memory.region[i].base;
327 u64 lmbsize = lmb.memory.region[i].size;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000328
329 if (max_addr == LMB_ALLOC_ANYWHERE)
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800330 base = lmb_align_down(lmbbase + lmbsize - size, align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000331 else if (lmbbase < max_addr) {
332 base = min(lmbbase + lmbsize, max_addr);
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800333 base = lmb_align_down(base - size, align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000334 } else
335 continue;
336
Paul Mackerras300613e2008-04-12 15:20:59 +1000337 while (lmbbase <= base) {
338 j = lmb_overlaps_region(&lmb.reserved, base, size);
339 if (j < 0)
340 break;
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800341 base = lmb_align_down(lmb.reserved.region[j].base - size,
342 align);
Paul Mackerras300613e2008-04-12 15:20:59 +1000343 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000344
345 if ((base != 0) && (lmbbase <= base))
346 break;
347 }
348
349 if (i < 0)
350 return 0;
351
David S. Millereea89e12008-02-13 16:57:09 -0800352 if (lmb_add_region(&lmb.reserved, base, lmb_align_up(size, align)) < 0)
353 return 0;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000354
355 return base;
356}
357
358/* You must call lmb_analyze() before this. */
Becky Brucee5f27092008-02-13 16:58:39 -0800359u64 __init lmb_phys_mem_size(void)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000360{
361 return lmb.memory.size;
362}
363
Becky Brucee5f27092008-02-13 16:58:39 -0800364u64 __init lmb_end_of_DRAM(void)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000365{
366 int idx = lmb.memory.cnt - 1;
367
368 return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
369}
370
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000371/* You must call lmb_analyze() after this. */
Becky Brucee5f27092008-02-13 16:58:39 -0800372void __init lmb_enforce_memory_limit(u64 memory_limit)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000373{
Becky Brucee5f27092008-02-13 16:58:39 -0800374 unsigned long i;
375 u64 limit;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000376 struct lmb_property *p;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000377
Paul Mackerras300613e2008-04-12 15:20:59 +1000378 if (!memory_limit)
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000379 return;
380
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000381 /* Truncate the lmb regions to satisfy the memory limit. */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000382 limit = memory_limit;
383 for (i = 0; i < lmb.memory.cnt; i++) {
384 if (limit > lmb.memory.region[i].size) {
385 limit -= lmb.memory.region[i].size;
386 continue;
387 }
388
389 lmb.memory.region[i].size = limit;
390 lmb.memory.cnt = i + 1;
391 break;
392 }
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000393
Michael Ellerman30f30e12006-07-04 17:13:23 +1000394 if (lmb.memory.region[0].size < lmb.rmo_size)
395 lmb.rmo_size = lmb.memory.region[0].size;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000396
397 /* And truncate any reserves above the limit also. */
398 for (i = 0; i < lmb.reserved.cnt; i++) {
399 p = &lmb.reserved.region[i];
400
401 if (p->base > memory_limit)
402 p->size = 0;
403 else if ((p->base + p->size) > memory_limit)
404 p->size = memory_limit - p->base;
405
406 if (p->size == 0) {
407 lmb_remove_region(&lmb.reserved, i);
408 i--;
409 }
410 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000411}
Kumar Galaf98eeb42008-01-09 11:27:23 -0600412
Becky Brucee5f27092008-02-13 16:58:39 -0800413int __init lmb_is_reserved(u64 addr)
Kumar Galaf98eeb42008-01-09 11:27:23 -0600414{
415 int i;
416
417 for (i = 0; i < lmb.reserved.cnt; i++) {
Becky Brucee5f27092008-02-13 16:58:39 -0800418 u64 upper = lmb.reserved.region[i].base +
419 lmb.reserved.region[i].size - 1;
Kumar Galaf98eeb42008-01-09 11:27:23 -0600420 if ((addr >= lmb.reserved.region[i].base) && (addr <= upper))
421 return 1;
422 }
423 return 0;
424}