diff options
Diffstat (limited to 'arch/ppc64/kernel/lmb.c')
-rw-r--r-- | arch/ppc64/kernel/lmb.c | 372 |
1 files changed, 372 insertions, 0 deletions
diff --git a/arch/ppc64/kernel/lmb.c b/arch/ppc64/kernel/lmb.c new file mode 100644 index 000000000000..d6c6bd03d2a4 --- /dev/null +++ b/arch/ppc64/kernel/lmb.c | |||
@@ -0,0 +1,372 @@ | |||
1 | /* | ||
2 | * Procedures for interfacing to Open Firmware. | ||
3 | * | ||
4 | * Peter Bergner, IBM Corp. June 2001. | ||
5 | * Copyright (C) 2001 Peter Bergner. | ||
6 | * | ||
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. | ||
11 | */ | ||
12 | |||
13 | #include <linux/config.h> | ||
14 | #include <linux/kernel.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/bitops.h> | ||
17 | #include <asm/types.h> | ||
18 | #include <asm/page.h> | ||
19 | #include <asm/prom.h> | ||
20 | #include <asm/lmb.h> | ||
21 | #include <asm/abs_addr.h> | ||
22 | |||
23 | struct lmb lmb; | ||
24 | |||
25 | #undef DEBUG | ||
26 | |||
27 | void lmb_dump_all(void) | ||
28 | { | ||
29 | #ifdef DEBUG | ||
30 | unsigned long i; | ||
31 | struct lmb *_lmb = &lmb; | ||
32 | |||
33 | udbg_printf("lmb_dump_all:\n"); | ||
34 | udbg_printf(" memory.cnt = 0x%lx\n", | ||
35 | _lmb->memory.cnt); | ||
36 | udbg_printf(" memory.size = 0x%lx\n", | ||
37 | _lmb->memory.size); | ||
38 | for (i=0; i < _lmb->memory.cnt ;i++) { | ||
39 | udbg_printf(" memory.region[0x%x].base = 0x%lx\n", | ||
40 | i, _lmb->memory.region[i].base); | ||
41 | udbg_printf(" .physbase = 0x%lx\n", | ||
42 | _lmb->memory.region[i].physbase); | ||
43 | udbg_printf(" .size = 0x%lx\n", | ||
44 | _lmb->memory.region[i].size); | ||
45 | } | ||
46 | |||
47 | udbg_printf("\n reserved.cnt = 0x%lx\n", | ||
48 | _lmb->reserved.cnt); | ||
49 | udbg_printf(" reserved.size = 0x%lx\n", | ||
50 | _lmb->reserved.size); | ||
51 | for (i=0; i < _lmb->reserved.cnt ;i++) { | ||
52 | udbg_printf(" reserved.region[0x%x].base = 0x%lx\n", | ||
53 | i, _lmb->reserved.region[i].base); | ||
54 | udbg_printf(" .physbase = 0x%lx\n", | ||
55 | _lmb->reserved.region[i].physbase); | ||
56 | udbg_printf(" .size = 0x%lx\n", | ||
57 | _lmb->reserved.region[i].size); | ||
58 | } | ||
59 | #endif /* DEBUG */ | ||
60 | } | ||
61 | |||
62 | static unsigned long __init | ||
63 | lmb_addrs_overlap(unsigned long base1, unsigned long size1, | ||
64 | unsigned long base2, unsigned long size2) | ||
65 | { | ||
66 | return ((base1 < (base2+size2)) && (base2 < (base1+size1))); | ||
67 | } | ||
68 | |||
69 | static long __init | ||
70 | lmb_addrs_adjacent(unsigned long base1, unsigned long size1, | ||
71 | unsigned long base2, unsigned long size2) | ||
72 | { | ||
73 | if (base2 == base1 + size1) | ||
74 | return 1; | ||
75 | else if (base1 == base2 + size2) | ||
76 | return -1; | ||
77 | |||
78 | return 0; | ||
79 | } | ||
80 | |||
81 | static long __init | ||
82 | lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1, unsigned long r2) | ||
83 | { | ||
84 | unsigned long base1 = rgn->region[r1].base; | ||
85 | unsigned long size1 = rgn->region[r1].size; | ||
86 | unsigned long base2 = rgn->region[r2].base; | ||
87 | unsigned long size2 = rgn->region[r2].size; | ||
88 | |||
89 | return lmb_addrs_adjacent(base1, size1, base2, size2); | ||
90 | } | ||
91 | |||
92 | /* Assumption: base addr of region 1 < base addr of region 2 */ | ||
93 | static void __init | ||
94 | lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1, unsigned long r2) | ||
95 | { | ||
96 | unsigned long i; | ||
97 | |||
98 | rgn->region[r1].size += rgn->region[r2].size; | ||
99 | for (i=r2; i < rgn->cnt-1; i++) { | ||
100 | rgn->region[i].base = rgn->region[i+1].base; | ||
101 | rgn->region[i].physbase = rgn->region[i+1].physbase; | ||
102 | rgn->region[i].size = rgn->region[i+1].size; | ||
103 | } | ||
104 | rgn->cnt--; | ||
105 | } | ||
106 | |||
107 | /* This routine called with relocation disabled. */ | ||
108 | void __init | ||
109 | lmb_init(void) | ||
110 | { | ||
111 | struct lmb *_lmb = &lmb; | ||
112 | |||
113 | /* Create a dummy zero size LMB which will get coalesced away later. | ||
114 | * This simplifies the lmb_add() code below... | ||
115 | */ | ||
116 | _lmb->memory.region[0].base = 0; | ||
117 | _lmb->memory.region[0].size = 0; | ||
118 | _lmb->memory.cnt = 1; | ||
119 | |||
120 | /* Ditto. */ | ||
121 | _lmb->reserved.region[0].base = 0; | ||
122 | _lmb->reserved.region[0].size = 0; | ||
123 | _lmb->reserved.cnt = 1; | ||
124 | } | ||
125 | |||
126 | /* This routine called with relocation disabled. */ | ||
127 | void __init | ||
128 | lmb_analyze(void) | ||
129 | { | ||
130 | unsigned long i; | ||
131 | unsigned long mem_size = 0; | ||
132 | unsigned long size_mask = 0; | ||
133 | struct lmb *_lmb = &lmb; | ||
134 | #ifdef CONFIG_MSCHUNKS | ||
135 | unsigned long physbase = 0; | ||
136 | #endif | ||
137 | |||
138 | for (i=0; i < _lmb->memory.cnt; i++) { | ||
139 | unsigned long lmb_size; | ||
140 | |||
141 | lmb_size = _lmb->memory.region[i].size; | ||
142 | |||
143 | #ifdef CONFIG_MSCHUNKS | ||
144 | _lmb->memory.region[i].physbase = physbase; | ||
145 | physbase += lmb_size; | ||
146 | #else | ||
147 | _lmb->memory.region[i].physbase = _lmb->memory.region[i].base; | ||
148 | #endif | ||
149 | mem_size += lmb_size; | ||
150 | size_mask |= lmb_size; | ||
151 | } | ||
152 | |||
153 | _lmb->memory.size = mem_size; | ||
154 | } | ||
155 | |||
156 | /* This routine called with relocation disabled. */ | ||
157 | static long __init | ||
158 | lmb_add_region(struct lmb_region *rgn, unsigned long base, unsigned long size) | ||
159 | { | ||
160 | unsigned long i, coalesced = 0; | ||
161 | long adjacent; | ||
162 | |||
163 | /* First try and coalesce this LMB with another. */ | ||
164 | for (i=0; i < rgn->cnt; i++) { | ||
165 | unsigned long rgnbase = rgn->region[i].base; | ||
166 | unsigned long rgnsize = rgn->region[i].size; | ||
167 | |||
168 | adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize); | ||
169 | if ( adjacent > 0 ) { | ||
170 | rgn->region[i].base -= size; | ||
171 | rgn->region[i].physbase -= size; | ||
172 | rgn->region[i].size += size; | ||
173 | coalesced++; | ||
174 | break; | ||
175 | } | ||
176 | else if ( adjacent < 0 ) { | ||
177 | rgn->region[i].size += size; | ||
178 | coalesced++; | ||
179 | break; | ||
180 | } | ||
181 | } | ||
182 | |||
183 | if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) { | ||
184 | lmb_coalesce_regions(rgn, i, i+1); | ||
185 | coalesced++; | ||
186 | } | ||
187 | |||
188 | if ( coalesced ) { | ||
189 | return coalesced; | ||
190 | } else if ( rgn->cnt >= MAX_LMB_REGIONS ) { | ||
191 | return -1; | ||
192 | } | ||
193 | |||
194 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ | ||
195 | for (i=rgn->cnt-1; i >= 0; i--) { | ||
196 | if (base < rgn->region[i].base) { | ||
197 | rgn->region[i+1].base = rgn->region[i].base; | ||
198 | rgn->region[i+1].physbase = rgn->region[i].physbase; | ||
199 | rgn->region[i+1].size = rgn->region[i].size; | ||
200 | } else { | ||
201 | rgn->region[i+1].base = base; | ||
202 | rgn->region[i+1].physbase = lmb_abs_to_phys(base); | ||
203 | rgn->region[i+1].size = size; | ||
204 | break; | ||
205 | } | ||
206 | } | ||
207 | rgn->cnt++; | ||
208 | |||
209 | return 0; | ||
210 | } | ||
211 | |||
212 | /* This routine called with relocation disabled. */ | ||
213 | long __init | ||
214 | lmb_add(unsigned long base, unsigned long size) | ||
215 | { | ||
216 | struct lmb *_lmb = &lmb; | ||
217 | struct lmb_region *_rgn = &(_lmb->memory); | ||
218 | |||
219 | /* On pSeries LPAR systems, the first LMB is our RMO region. */ | ||
220 | if ( base == 0 ) | ||
221 | _lmb->rmo_size = size; | ||
222 | |||
223 | return lmb_add_region(_rgn, base, size); | ||
224 | |||
225 | } | ||
226 | |||
227 | long __init | ||
228 | lmb_reserve(unsigned long base, unsigned long size) | ||
229 | { | ||
230 | struct lmb *_lmb = &lmb; | ||
231 | struct lmb_region *_rgn = &(_lmb->reserved); | ||
232 | |||
233 | return lmb_add_region(_rgn, base, size); | ||
234 | } | ||
235 | |||
236 | long __init | ||
237 | lmb_overlaps_region(struct lmb_region *rgn, unsigned long base, unsigned long size) | ||
238 | { | ||
239 | unsigned long i; | ||
240 | |||
241 | for (i=0; i < rgn->cnt; i++) { | ||
242 | unsigned long rgnbase = rgn->region[i].base; | ||
243 | unsigned long rgnsize = rgn->region[i].size; | ||
244 | if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) { | ||
245 | break; | ||
246 | } | ||
247 | } | ||
248 | |||
249 | return (i < rgn->cnt) ? i : -1; | ||
250 | } | ||
251 | |||
252 | unsigned long __init | ||
253 | lmb_alloc(unsigned long size, unsigned long align) | ||
254 | { | ||
255 | return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE); | ||
256 | } | ||
257 | |||
258 | unsigned long __init | ||
259 | lmb_alloc_base(unsigned long size, unsigned long align, unsigned long max_addr) | ||
260 | { | ||
261 | long i, j; | ||
262 | unsigned long base = 0; | ||
263 | struct lmb *_lmb = &lmb; | ||
264 | struct lmb_region *_mem = &(_lmb->memory); | ||
265 | struct lmb_region *_rsv = &(_lmb->reserved); | ||
266 | |||
267 | for (i=_mem->cnt-1; i >= 0; i--) { | ||
268 | unsigned long lmbbase = _mem->region[i].base; | ||
269 | unsigned long lmbsize = _mem->region[i].size; | ||
270 | |||
271 | if ( max_addr == LMB_ALLOC_ANYWHERE ) | ||
272 | base = _ALIGN_DOWN(lmbbase+lmbsize-size, align); | ||
273 | else if ( lmbbase < max_addr ) | ||
274 | base = _ALIGN_DOWN(min(lmbbase+lmbsize,max_addr)-size, align); | ||
275 | else | ||
276 | continue; | ||
277 | |||
278 | while ( (lmbbase <= base) && | ||
279 | ((j = lmb_overlaps_region(_rsv,base,size)) >= 0) ) { | ||
280 | base = _ALIGN_DOWN(_rsv->region[j].base-size, align); | ||
281 | } | ||
282 | |||
283 | if ( (base != 0) && (lmbbase <= base) ) | ||
284 | break; | ||
285 | } | ||
286 | |||
287 | if ( i < 0 ) | ||
288 | return 0; | ||
289 | |||
290 | lmb_add_region(_rsv, base, size); | ||
291 | |||
292 | return base; | ||
293 | } | ||
294 | |||
295 | unsigned long __init | ||
296 | lmb_phys_mem_size(void) | ||
297 | { | ||
298 | struct lmb *_lmb = &lmb; | ||
299 | #ifdef CONFIG_MSCHUNKS | ||
300 | return _lmb->memory.size; | ||
301 | #else | ||
302 | struct lmb_region *_mem = &(_lmb->memory); | ||
303 | unsigned long total = 0; | ||
304 | int i; | ||
305 | |||
306 | /* add all physical memory to the bootmem map */ | ||
307 | for (i=0; i < _mem->cnt; i++) | ||
308 | total += _mem->region[i].size; | ||
309 | return total; | ||
310 | #endif /* CONFIG_MSCHUNKS */ | ||
311 | } | ||
312 | |||
313 | unsigned long __init | ||
314 | lmb_end_of_DRAM(void) | ||
315 | { | ||
316 | struct lmb *_lmb = &lmb; | ||
317 | struct lmb_region *_mem = &(_lmb->memory); | ||
318 | int idx = _mem->cnt - 1; | ||
319 | |||
320 | #ifdef CONFIG_MSCHUNKS | ||
321 | return (_mem->region[idx].physbase + _mem->region[idx].size); | ||
322 | #else | ||
323 | return (_mem->region[idx].base + _mem->region[idx].size); | ||
324 | #endif /* CONFIG_MSCHUNKS */ | ||
325 | |||
326 | return 0; | ||
327 | } | ||
328 | |||
329 | unsigned long __init | ||
330 | lmb_abs_to_phys(unsigned long aa) | ||
331 | { | ||
332 | unsigned long i, pa = aa; | ||
333 | struct lmb *_lmb = &lmb; | ||
334 | struct lmb_region *_mem = &(_lmb->memory); | ||
335 | |||
336 | for (i=0; i < _mem->cnt; i++) { | ||
337 | unsigned long lmbbase = _mem->region[i].base; | ||
338 | unsigned long lmbsize = _mem->region[i].size; | ||
339 | if ( lmb_addrs_overlap(aa,1,lmbbase,lmbsize) ) { | ||
340 | pa = _mem->region[i].physbase + (aa - lmbbase); | ||
341 | break; | ||
342 | } | ||
343 | } | ||
344 | |||
345 | return pa; | ||
346 | } | ||
347 | |||
348 | /* | ||
349 | * Truncate the lmb list to memory_limit if it's set | ||
350 | * You must call lmb_analyze() after this. | ||
351 | */ | ||
352 | void __init lmb_enforce_memory_limit(void) | ||
353 | { | ||
354 | extern unsigned long memory_limit; | ||
355 | unsigned long i, limit; | ||
356 | struct lmb_region *mem = &(lmb.memory); | ||
357 | |||
358 | if (! memory_limit) | ||
359 | return; | ||
360 | |||
361 | limit = memory_limit; | ||
362 | for (i = 0; i < mem->cnt; i++) { | ||
363 | if (limit > mem->region[i].size) { | ||
364 | limit -= mem->region[i].size; | ||
365 | continue; | ||
366 | } | ||
367 | |||
368 | mem->region[i].size = limit; | ||
369 | mem->cnt = i + 1; | ||
370 | break; | ||
371 | } | ||
372 | } | ||