[NETLINK]: w1_int.c: fix default netlink group
[linux-2.6/verdex.git] / arch / ppc64 / kernel / lmb.c
blobd6c6bd03d2a42cf49ec1f8ebeec5ee077a2f683f
1 /*
2 * Procedures for interfacing to Open Firmware.
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.
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>
23 struct lmb lmb;
25 #undef DEBUG
27 void lmb_dump_all(void)
29 #ifdef DEBUG
30 unsigned long i;
31 struct lmb *_lmb = &lmb;
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);
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);
59 #endif /* DEBUG */
62 static unsigned long __init
63 lmb_addrs_overlap(unsigned long base1, unsigned long size1,
64 unsigned long base2, unsigned long size2)
66 return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
69 static long __init
70 lmb_addrs_adjacent(unsigned long base1, unsigned long size1,
71 unsigned long base2, unsigned long size2)
73 if (base2 == base1 + size1)
74 return 1;
75 else if (base1 == base2 + size2)
76 return -1;
78 return 0;
81 static long __init
82 lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1, unsigned long r2)
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;
89 return lmb_addrs_adjacent(base1, size1, base2, size2);
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)
96 unsigned long i;
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;
104 rgn->cnt--;
107 /* This routine called with relocation disabled. */
108 void __init
109 lmb_init(void)
111 struct lmb *_lmb = &lmb;
113 /* Create a dummy zero size LMB which will get coalesced away later.
114 * This simplifies the lmb_add() code below...
116 _lmb->memory.region[0].base = 0;
117 _lmb->memory.region[0].size = 0;
118 _lmb->memory.cnt = 1;
120 /* Ditto. */
121 _lmb->reserved.region[0].base = 0;
122 _lmb->reserved.region[0].size = 0;
123 _lmb->reserved.cnt = 1;
126 /* This routine called with relocation disabled. */
127 void __init
128 lmb_analyze(void)
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
138 for (i=0; i < _lmb->memory.cnt; i++) {
139 unsigned long lmb_size;
141 lmb_size = _lmb->memory.region[i].size;
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;
153 _lmb->memory.size = mem_size;
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)
160 unsigned long i, coalesced = 0;
161 long adjacent;
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;
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;
176 else if ( adjacent < 0 ) {
177 rgn->region[i].size += size;
178 coalesced++;
179 break;
183 if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
184 lmb_coalesce_regions(rgn, i, i+1);
185 coalesced++;
188 if ( coalesced ) {
189 return coalesced;
190 } else if ( rgn->cnt >= MAX_LMB_REGIONS ) {
191 return -1;
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;
207 rgn->cnt++;
209 return 0;
212 /* This routine called with relocation disabled. */
213 long __init
214 lmb_add(unsigned long base, unsigned long size)
216 struct lmb *_lmb = &lmb;
217 struct lmb_region *_rgn = &(_lmb->memory);
219 /* On pSeries LPAR systems, the first LMB is our RMO region. */
220 if ( base == 0 )
221 _lmb->rmo_size = size;
223 return lmb_add_region(_rgn, base, size);
227 long __init
228 lmb_reserve(unsigned long base, unsigned long size)
230 struct lmb *_lmb = &lmb;
231 struct lmb_region *_rgn = &(_lmb->reserved);
233 return lmb_add_region(_rgn, base, size);
236 long __init
237 lmb_overlaps_region(struct lmb_region *rgn, unsigned long base, unsigned long size)
239 unsigned long i;
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;
249 return (i < rgn->cnt) ? i : -1;
252 unsigned long __init
253 lmb_alloc(unsigned long size, unsigned long align)
255 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
258 unsigned long __init
259 lmb_alloc_base(unsigned long size, unsigned long align, unsigned long max_addr)
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);
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;
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;
278 while ( (lmbbase <= base) &&
279 ((j = lmb_overlaps_region(_rsv,base,size)) >= 0) ) {
280 base = _ALIGN_DOWN(_rsv->region[j].base-size, align);
283 if ( (base != 0) && (lmbbase <= base) )
284 break;
287 if ( i < 0 )
288 return 0;
290 lmb_add_region(_rsv, base, size);
292 return base;
295 unsigned long __init
296 lmb_phys_mem_size(void)
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;
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 */
313 unsigned long __init
314 lmb_end_of_DRAM(void)
316 struct lmb *_lmb = &lmb;
317 struct lmb_region *_mem = &(_lmb->memory);
318 int idx = _mem->cnt - 1;
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 */
326 return 0;
329 unsigned long __init
330 lmb_abs_to_phys(unsigned long aa)
332 unsigned long i, pa = aa;
333 struct lmb *_lmb = &lmb;
334 struct lmb_region *_mem = &(_lmb->memory);
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;
345 return pa;
349 * Truncate the lmb list to memory_limit if it's set
350 * You must call lmb_analyze() after this.
352 void __init lmb_enforce_memory_limit(void)
354 extern unsigned long memory_limit;
355 unsigned long i, limit;
356 struct lmb_region *mem = &(lmb.memory);
358 if (! memory_limit)
359 return;
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;
368 mem->region[i].size = limit;
369 mem->cnt = i + 1;
370 break;