2 * Procedures for maintaining information about logical memory blocks.
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>
22 #include "mmu_decl.h" /* for __max_low_memory */
29 #define DBG(fmt...) udbg_printf(fmt)
36 void lmb_dump_all(void)
41 DBG("lmb_dump_all:\n");
42 DBG(" memory.cnt = 0x%lx\n", lmb.memory.cnt);
43 DBG(" memory.size = 0x%lx\n", lmb.memory.size);
44 for (i=0; i < lmb.memory.cnt ;i++) {
45 DBG(" memory.region[0x%x].base = 0x%lx\n",
46 i, lmb.memory.region[i].base);
47 DBG(" .size = 0x%lx\n",
48 lmb.memory.region[i].size);
51 DBG("\n reserved.cnt = 0x%lx\n", lmb.reserved.cnt);
52 DBG(" reserved.size = 0x%lx\n", lmb.reserved.size);
53 for (i=0; i < lmb.reserved.cnt ;i++) {
54 DBG(" reserved.region[0x%x].base = 0x%lx\n",
55 i, lmb.reserved.region[i].base);
56 DBG(" .size = 0x%lx\n",
57 lmb.reserved.region[i].size);
62 static unsigned long __init lmb_addrs_overlap(unsigned long base1,
63 unsigned long size1, unsigned long base2, unsigned long size2)
65 return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
68 static long __init lmb_addrs_adjacent(unsigned long base1, unsigned long size1,
69 unsigned long base2, unsigned long size2)
71 if (base2 == base1 + size1)
73 else if (base1 == base2 + size2)
79 static long __init lmb_regions_adjacent(struct lmb_region *rgn,
80 unsigned long r1, unsigned long r2)
82 unsigned long base1 = rgn->region[r1].base;
83 unsigned long size1 = rgn->region[r1].size;
84 unsigned long base2 = rgn->region[r2].base;
85 unsigned long size2 = rgn->region[r2].size;
87 return lmb_addrs_adjacent(base1, size1, base2, size2);
90 /* Assumption: base addr of region 1 < base addr of region 2 */
91 static void __init lmb_coalesce_regions(struct lmb_region *rgn,
92 unsigned long r1, unsigned long r2)
96 rgn->region[r1].size += rgn->region[r2].size;
97 for (i=r2; i < rgn->cnt-1; i++) {
98 rgn->region[i].base = rgn->region[i+1].base;
99 rgn->region[i].size = rgn->region[i+1].size;
104 /* This routine called with relocation disabled. */
105 void __init lmb_init(void)
107 /* Create a dummy zero size LMB which will get coalesced away later.
108 * This simplifies the lmb_add() code below...
110 lmb.memory.region[0].base = 0;
111 lmb.memory.region[0].size = 0;
115 lmb.reserved.region[0].base = 0;
116 lmb.reserved.region[0].size = 0;
117 lmb.reserved.cnt = 1;
120 /* This routine may be called with relocation disabled. */
121 void __init lmb_analyze(void)
127 for (i = 0; i < lmb.memory.cnt; i++)
128 lmb.memory.size += lmb.memory.region[i].size;
131 /* This routine called with relocation disabled. */
132 static long __init lmb_add_region(struct lmb_region *rgn, unsigned long base,
135 unsigned long i, coalesced = 0;
138 /* First try and coalesce this LMB with another. */
139 for (i=0; i < rgn->cnt; i++) {
140 unsigned long rgnbase = rgn->region[i].base;
141 unsigned long rgnsize = rgn->region[i].size;
143 adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize);
144 if ( adjacent > 0 ) {
145 rgn->region[i].base -= size;
146 rgn->region[i].size += size;
150 else if ( adjacent < 0 ) {
151 rgn->region[i].size += size;
157 if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
158 lmb_coalesce_regions(rgn, i, i+1);
164 if (rgn->cnt >= MAX_LMB_REGIONS)
167 /* Couldn't coalesce the LMB, so add it to the sorted table. */
168 for (i = rgn->cnt-1; i >= 0; i--) {
169 if (base < rgn->region[i].base) {
170 rgn->region[i+1].base = rgn->region[i].base;
171 rgn->region[i+1].size = rgn->region[i].size;
173 rgn->region[i+1].base = base;
174 rgn->region[i+1].size = size;
183 /* This routine may be called with relocation disabled. */
184 long __init lmb_add(unsigned long base, unsigned long size)
186 struct lmb_region *_rgn = &(lmb.memory);
188 /* On pSeries LPAR systems, the first LMB is our RMO region. */
192 return lmb_add_region(_rgn, base, size);
196 long __init lmb_reserve(unsigned long base, unsigned long size)
198 struct lmb_region *_rgn = &(lmb.reserved);
202 return lmb_add_region(_rgn, base, size);
205 long __init lmb_overlaps_region(struct lmb_region *rgn, unsigned long base,
210 for (i=0; i < rgn->cnt; i++) {
211 unsigned long rgnbase = rgn->region[i].base;
212 unsigned long rgnsize = rgn->region[i].size;
213 if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
218 return (i < rgn->cnt) ? i : -1;
221 unsigned long __init lmb_alloc(unsigned long size, unsigned long align)
223 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
226 unsigned long __init lmb_alloc_base(unsigned long size, unsigned long align,
227 unsigned long max_addr)
230 unsigned long base = 0;
235 /* On 32-bit, make sure we allocate lowmem */
236 if (max_addr == LMB_ALLOC_ANYWHERE)
237 max_addr = __max_low_memory;
239 for (i = lmb.memory.cnt-1; i >= 0; i--) {
240 unsigned long lmbbase = lmb.memory.region[i].base;
241 unsigned long lmbsize = lmb.memory.region[i].size;
243 if (max_addr == LMB_ALLOC_ANYWHERE)
244 base = _ALIGN_DOWN(lmbbase + lmbsize - size, align);
245 else if (lmbbase < max_addr) {
246 base = min(lmbbase + lmbsize, max_addr);
247 base = _ALIGN_DOWN(base - size, align);
251 while ((lmbbase <= base) &&
252 ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) )
253 base = _ALIGN_DOWN(lmb.reserved.region[j].base - size,
256 if ((base != 0) && (lmbbase <= base))
263 lmb_add_region(&lmb.reserved, base, size);
268 /* You must call lmb_analyze() before this. */
269 unsigned long __init lmb_phys_mem_size(void)
271 return lmb.memory.size;
274 unsigned long __init lmb_end_of_DRAM(void)
276 int idx = lmb.memory.cnt - 1;
278 return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
282 * Truncate the lmb list to memory_limit if it's set
283 * You must call lmb_analyze() after this.
285 void __init lmb_enforce_memory_limit(unsigned long memory_limit)
287 unsigned long i, limit;
292 limit = memory_limit;
293 for (i = 0; i < lmb.memory.cnt; i++) {
294 if (limit > lmb.memory.region[i].size) {
295 limit -= lmb.memory.region[i].size;
299 lmb.memory.region[i].size = limit;
300 lmb.memory.cnt = i + 1;