blob: 6390d63a2a0ea7b22381a23d4eb97bd73de61320 [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);
37 DBG(" memory.size = 0x%lx\n", lmb.memory.size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100038 for (i=0; i < lmb.memory.cnt ;i++) {
Michael Ellermaneb481892005-11-15 14:49:22 +110039 DBG(" memory.region[0x%x].base = 0x%lx\n",
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100040 i, lmb.memory.region[i].base);
Michael Ellermaneb481892005-11-15 14:49:22 +110041 DBG(" .size = 0x%lx\n",
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100042 lmb.memory.region[i].size);
43 }
44
Michael Ellermaneb481892005-11-15 14:49:22 +110045 DBG("\n reserved.cnt = 0x%lx\n", lmb.reserved.cnt);
46 DBG(" reserved.size = 0x%lx\n", lmb.reserved.size);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100047 for (i=0; i < lmb.reserved.cnt ;i++) {
Michael Ellermaneb481892005-11-15 14:49:22 +110048 DBG(" reserved.region[0x%x].base = 0x%lx\n",
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100049 i, lmb.reserved.region[i].base);
Michael Ellermaneb481892005-11-15 14:49:22 +110050 DBG(" .size = 0x%lx\n",
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100051 lmb.reserved.region[i].size);
52 }
53#endif /* DEBUG */
54}
55
56static unsigned long __init lmb_addrs_overlap(unsigned long base1,
57 unsigned long size1, unsigned long base2, unsigned long size2)
58{
59 return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
60}
61
62static long __init lmb_addrs_adjacent(unsigned long base1, unsigned long size1,
63 unsigned long base2, unsigned long size2)
64{
65 if (base2 == base1 + size1)
66 return 1;
67 else if (base1 == base2 + size2)
68 return -1;
69
70 return 0;
71}
72
73static long __init lmb_regions_adjacent(struct lmb_region *rgn,
74 unsigned long r1, unsigned long r2)
75{
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;
80
81 return lmb_addrs_adjacent(base1, size1, base2, size2);
82}
83
Michael Ellerman2babf5c2006-05-17 18:00:46 +100084static void __init lmb_remove_region(struct lmb_region *rgn, unsigned long r)
85{
86 unsigned long i;
87
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;
91 }
92 rgn->cnt--;
93}
94
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100095/* Assumption: base addr of region 1 < base addr of region 2 */
96static void __init lmb_coalesce_regions(struct lmb_region *rgn,
97 unsigned long r1, unsigned long r2)
98{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100099 rgn->region[r1].size += rgn->region[r2].size;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000100 lmb_remove_region(rgn, r2);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000101}
102
103/* This routine called with relocation disabled. */
104void __init lmb_init(void)
105{
106 /* Create a dummy zero size LMB which will get coalesced away later.
107 * This simplifies the lmb_add() code below...
108 */
109 lmb.memory.region[0].base = 0;
110 lmb.memory.region[0].size = 0;
111 lmb.memory.cnt = 1;
112
113 /* Ditto. */
114 lmb.reserved.region[0].base = 0;
115 lmb.reserved.region[0].size = 0;
116 lmb.reserved.cnt = 1;
117}
118
119/* This routine may be called with relocation disabled. */
120void __init lmb_analyze(void)
121{
122 int i;
123
124 lmb.memory.size = 0;
125
126 for (i = 0; i < lmb.memory.cnt; i++)
127 lmb.memory.size += lmb.memory.region[i].size;
128}
129
130/* This routine called with relocation disabled. */
131static long __init lmb_add_region(struct lmb_region *rgn, unsigned long base,
132 unsigned long size)
133{
Manish Ahuja56d6d1a2007-07-10 05:03:45 +1000134 unsigned long coalesced = 0;
135 long adjacent, i;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000136
137 /* First try and coalesce this LMB with another. */
138 for (i=0; i < rgn->cnt; i++) {
139 unsigned long rgnbase = rgn->region[i].base;
140 unsigned long rgnsize = rgn->region[i].size;
141
David Gibsoneb6de282007-02-28 14:12:29 +1100142 if ((rgnbase == base) && (rgnsize == size))
143 /* Already have this region, so we're done */
144 return 0;
145
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000146 adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize);
147 if ( adjacent > 0 ) {
148 rgn->region[i].base -= size;
149 rgn->region[i].size += size;
150 coalesced++;
151 break;
152 }
153 else if ( adjacent < 0 ) {
154 rgn->region[i].size += size;
155 coalesced++;
156 break;
157 }
158 }
159
160 if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
161 lmb_coalesce_regions(rgn, i, i+1);
162 coalesced++;
163 }
164
165 if (coalesced)
166 return coalesced;
167 if (rgn->cnt >= MAX_LMB_REGIONS)
168 return -1;
169
170 /* Couldn't coalesce the LMB, so add it to the sorted table. */
171 for (i = rgn->cnt-1; i >= 0; i--) {
172 if (base < rgn->region[i].base) {
173 rgn->region[i+1].base = rgn->region[i].base;
174 rgn->region[i+1].size = rgn->region[i].size;
175 } else {
176 rgn->region[i+1].base = base;
177 rgn->region[i+1].size = size;
178 break;
179 }
180 }
181 rgn->cnt++;
182
183 return 0;
184}
185
186/* This routine may be called with relocation disabled. */
187long __init lmb_add(unsigned long base, unsigned long size)
188{
189 struct lmb_region *_rgn = &(lmb.memory);
190
191 /* On pSeries LPAR systems, the first LMB is our RMO region. */
192 if (base == 0)
193 lmb.rmo_size = size;
194
195 return lmb_add_region(_rgn, base, size);
196
197}
198
199long __init lmb_reserve(unsigned long base, unsigned long size)
200{
201 struct lmb_region *_rgn = &(lmb.reserved);
202
Michael Ellerman8c20faf2006-01-25 21:31:26 +1300203 BUG_ON(0 == size);
204
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000205 return lmb_add_region(_rgn, base, size);
206}
207
208long __init lmb_overlaps_region(struct lmb_region *rgn, unsigned long base,
209 unsigned long size)
210{
211 unsigned long i;
212
213 for (i=0; i < rgn->cnt; i++) {
214 unsigned long rgnbase = rgn->region[i].base;
215 unsigned long rgnsize = rgn->region[i].size;
216 if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
217 break;
218 }
219 }
220
221 return (i < rgn->cnt) ? i : -1;
222}
223
224unsigned long __init lmb_alloc(unsigned long size, unsigned long align)
225{
226 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
227}
228
229unsigned long __init lmb_alloc_base(unsigned long size, unsigned long align,
230 unsigned long max_addr)
231{
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300232 unsigned long alloc;
233
234 alloc = __lmb_alloc_base(size, align, max_addr);
235
Michael Ellerman2c2766032006-03-16 14:47:20 +1100236 if (alloc == 0)
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300237 panic("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
238 size, max_addr);
239
240 return alloc;
241}
242
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800243static unsigned long lmb_align_down(unsigned long addr, unsigned long size)
244{
245 return addr & ~(size - 1);
246}
247
David S. Millereea89e12008-02-13 16:57:09 -0800248static unsigned long lmb_align_up(unsigned long addr, unsigned long size)
249{
250 return (addr + (size - 1)) & ~(size - 1);
251}
252
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300253unsigned long __init __lmb_alloc_base(unsigned long size, unsigned long align,
254 unsigned long max_addr)
255{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000256 long i, j;
257 unsigned long base = 0;
258
Michael Ellerman8c20faf2006-01-25 21:31:26 +1300259 BUG_ON(0 == size);
260
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800261 /* On some platforms, make sure we allocate lowmem */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000262 if (max_addr == LMB_ALLOC_ANYWHERE)
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800263 max_addr = LMB_REAL_LIMIT;
264
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000265 for (i = lmb.memory.cnt-1; i >= 0; i--) {
266 unsigned long lmbbase = lmb.memory.region[i].base;
267 unsigned long lmbsize = lmb.memory.region[i].size;
268
269 if (max_addr == LMB_ALLOC_ANYWHERE)
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800270 base = lmb_align_down(lmbbase + lmbsize - size, align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000271 else if (lmbbase < max_addr) {
272 base = min(lmbbase + lmbsize, max_addr);
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800273 base = lmb_align_down(base - size, align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000274 } else
275 continue;
276
277 while ((lmbbase <= base) &&
278 ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) )
David S. Millerd9b2b2a2008-02-13 16:56:49 -0800279 base = lmb_align_down(lmb.reserved.region[j].base - size,
280 align);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000281
282 if ((base != 0) && (lmbbase <= base))
283 break;
284 }
285
286 if (i < 0)
287 return 0;
288
David S. Millereea89e12008-02-13 16:57:09 -0800289 if (lmb_add_region(&lmb.reserved, base, lmb_align_up(size, align)) < 0)
290 return 0;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000291
292 return base;
293}
294
295/* You must call lmb_analyze() before this. */
296unsigned long __init lmb_phys_mem_size(void)
297{
298 return lmb.memory.size;
299}
300
301unsigned long __init lmb_end_of_DRAM(void)
302{
303 int idx = lmb.memory.cnt - 1;
304
305 return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
306}
307
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000308/* You must call lmb_analyze() after this. */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000309void __init lmb_enforce_memory_limit(unsigned long memory_limit)
310{
311 unsigned long i, limit;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000312 struct lmb_property *p;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000313
314 if (! memory_limit)
315 return;
316
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000317 /* Truncate the lmb regions to satisfy the memory limit. */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000318 limit = memory_limit;
319 for (i = 0; i < lmb.memory.cnt; i++) {
320 if (limit > lmb.memory.region[i].size) {
321 limit -= lmb.memory.region[i].size;
322 continue;
323 }
324
325 lmb.memory.region[i].size = limit;
326 lmb.memory.cnt = i + 1;
327 break;
328 }
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000329
Michael Ellerman30f30e12006-07-04 17:13:23 +1000330 if (lmb.memory.region[0].size < lmb.rmo_size)
331 lmb.rmo_size = lmb.memory.region[0].size;
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000332
333 /* And truncate any reserves above the limit also. */
334 for (i = 0; i < lmb.reserved.cnt; i++) {
335 p = &lmb.reserved.region[i];
336
337 if (p->base > memory_limit)
338 p->size = 0;
339 else if ((p->base + p->size) > memory_limit)
340 p->size = memory_limit - p->base;
341
342 if (p->size == 0) {
343 lmb_remove_region(&lmb.reserved, i);
344 i--;
345 }
346 }
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000347}
Kumar Galaf98eeb42008-01-09 11:27:23 -0600348
349int __init lmb_is_reserved(unsigned long addr)
350{
351 int i;
352
353 for (i = 0; i < lmb.reserved.cnt; i++) {
354 unsigned long upper = lmb.reserved.region[i].base +
355 lmb.reserved.region[i].size - 1;
356 if ((addr >= lmb.reserved.region[i].base) && (addr <= upper))
357 return 1;
358 }
359 return 0;
360}