2 * Procedures for interfacing to Open Firmware.
4 * Peter Bergner, IBM Corp. June 2001.
5 * Copyright (C) 2001 Peter Bergner.
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>
21 #include <asm/abs_addr.h>
27 void lmb_dump_all(void)
31 struct lmb
*_lmb
= &lmb
;
33 udbg_printf("lmb_dump_all:\n");
34 udbg_printf(" memory.cnt = 0x%lx\n",
36 udbg_printf(" memory.size = 0x%lx\n",
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",
49 udbg_printf(" reserved.size = 0x%lx\n",
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
);
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
)));
70 lmb_addrs_adjacent(unsigned long base1
, unsigned long size1
,
71 unsigned long base2
, unsigned long size2
)
73 if (base2
== base1
+ size1
)
75 else if (base1
== base2
+ size2
)
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 */
94 lmb_coalesce_regions(struct lmb_region
*rgn
, unsigned long r1
, unsigned long r2
)
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
;
107 /* This routine called with relocation disabled. */
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;
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. */
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;
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
;
147 _lmb
->memory
.region
[i
].physbase
= _lmb
->memory
.region
[i
].base
;
149 mem_size
+= lmb_size
;
150 size_mask
|= lmb_size
;
153 _lmb
->memory
.size
= mem_size
;
156 /* This routine called with relocation disabled. */
158 lmb_add_region(struct lmb_region
*rgn
, unsigned long base
, unsigned long size
)
160 unsigned long i
, coalesced
= 0;
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
;
176 else if ( adjacent
< 0 ) {
177 rgn
->region
[i
].size
+= size
;
183 if ((i
< rgn
->cnt
-1) && lmb_regions_adjacent(rgn
, i
, i
+1) ) {
184 lmb_coalesce_regions(rgn
, i
, i
+1);
190 } else if ( rgn
->cnt
>= MAX_LMB_REGIONS
) {
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
;
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
;
212 /* This routine called with relocation disabled. */
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. */
221 _lmb
->rmo_size
= size
;
223 return lmb_add_region(_rgn
, base
, size
);
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
);
237 lmb_overlaps_region(struct lmb_region
*rgn
, unsigned long base
, unsigned long size
)
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
) ) {
249 return (i
< rgn
->cnt
) ? i
: -1;
253 lmb_alloc(unsigned long size
, unsigned long align
)
255 return lmb_alloc_base(size
, align
, LMB_ALLOC_ANYWHERE
);
259 lmb_alloc_base(unsigned long size
, unsigned long align
, unsigned long max_addr
)
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
);
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
) )
290 lmb_add_region(_rsv
, base
, size
);
296 lmb_phys_mem_size(void)
298 struct lmb
*_lmb
= &lmb
;
299 #ifdef CONFIG_MSCHUNKS
300 return _lmb
->memory
.size
;
302 struct lmb_region
*_mem
= &(_lmb
->memory
);
303 unsigned long total
= 0;
306 /* add all physical memory to the bootmem map */
307 for (i
=0; i
< _mem
->cnt
; i
++)
308 total
+= _mem
->region
[i
].size
;
310 #endif /* CONFIG_MSCHUNKS */
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
);
323 return (_mem
->region
[idx
].base
+ _mem
->region
[idx
].size
);
324 #endif /* CONFIG_MSCHUNKS */
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
);
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
);
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
;
368 mem
->region
[i
].size
= limit
;