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 void lmb_dump_all(void)
34 udbg_printf("lmb_dump_all:\n");
35 udbg_printf(" memory.cnt = 0x%lx\n",
37 udbg_printf(" memory.size = 0x%lx\n",
39 for (i=0; i < lmb.memory.cnt ;i++) {
40 udbg_printf(" memory.region[0x%x].base = 0x%lx\n",
41 i, lmb.memory.region[i].base);
42 udbg_printf(" .size = 0x%lx\n",
43 lmb.memory.region[i].size);
46 udbg_printf("\n reserved.cnt = 0x%lx\n",
48 udbg_printf(" reserved.size = 0x%lx\n",
50 for (i=0; i < lmb.reserved.cnt ;i++) {
51 udbg_printf(" reserved.region[0x%x].base = 0x%lx\n",
52 i, lmb.reserved.region[i].base);
53 udbg_printf(" .size = 0x%lx\n",
54 lmb.reserved.region[i].size);
59 static unsigned long __init lmb_addrs_overlap(unsigned long base1,
60 unsigned long size1, unsigned long base2, unsigned long size2)
62 return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
65 static long __init lmb_addrs_adjacent(unsigned long base1, unsigned long size1,
66 unsigned long base2, unsigned long size2)
68 if (base2 == base1 + size1)
70 else if (base1 == base2 + size2)
76 static long __init lmb_regions_adjacent(struct lmb_region *rgn,
77 unsigned long r1, unsigned long r2)
79 unsigned long base1 = rgn->region[r1].base;
80 unsigned long size1 = rgn->region[r1].size;
81 unsigned long base2 = rgn->region[r2].base;
82 unsigned long size2 = rgn->region[r2].size;
84 return lmb_addrs_adjacent(base1, size1, base2, size2);
87 /* Assumption: base addr of region 1 < base addr of region 2 */
88 static void __init lmb_coalesce_regions(struct lmb_region *rgn,
89 unsigned long r1, unsigned long r2)
93 rgn->region[r1].size += rgn->region[r2].size;
94 for (i=r2; i < rgn->cnt-1; i++) {
95 rgn->region[i].base = rgn->region[i+1].base;
96 rgn->region[i].size = rgn->region[i+1].size;
101 /* This routine called with relocation disabled. */
102 void __init lmb_init(void)
104 /* Create a dummy zero size LMB which will get coalesced away later.
105 * This simplifies the lmb_add() code below...
107 lmb.memory.region[0].base = 0;
108 lmb.memory.region[0].size = 0;
112 lmb.reserved.region[0].base = 0;
113 lmb.reserved.region[0].size = 0;
114 lmb.reserved.cnt = 1;
117 /* This routine may be called with relocation disabled. */
118 void __init lmb_analyze(void)
124 for (i = 0; i < lmb.memory.cnt; i++)
125 lmb.memory.size += lmb.memory.region[i].size;
128 /* This routine called with relocation disabled. */
129 static long __init lmb_add_region(struct lmb_region *rgn, unsigned long base,
132 unsigned long i, coalesced = 0;
135 /* First try and coalesce this LMB with another. */
136 for (i=0; i < rgn->cnt; i++) {
137 unsigned long rgnbase = rgn->region[i].base;
138 unsigned long rgnsize = rgn->region[i].size;
140 adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize);
141 if ( adjacent > 0 ) {
142 rgn->region[i].base -= size;
143 rgn->region[i].size += size;
147 else if ( adjacent < 0 ) {
148 rgn->region[i].size += size;
154 if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
155 lmb_coalesce_regions(rgn, i, i+1);
161 if (rgn->cnt >= MAX_LMB_REGIONS)
164 /* Couldn't coalesce the LMB, so add it to the sorted table. */
165 for (i = rgn->cnt-1; i >= 0; i--) {
166 if (base < rgn->region[i].base) {
167 rgn->region[i+1].base = rgn->region[i].base;
168 rgn->region[i+1].size = rgn->region[i].size;
170 rgn->region[i+1].base = base;
171 rgn->region[i+1].size = size;
180 /* This routine may be called with relocation disabled. */
181 long __init lmb_add(unsigned long base, unsigned long size)
183 struct lmb_region *_rgn = &(lmb.memory);
185 /* On pSeries LPAR systems, the first LMB is our RMO region. */
189 return lmb_add_region(_rgn, base, size);
193 long __init lmb_reserve(unsigned long base, unsigned long size)
195 struct lmb_region *_rgn = &(lmb.reserved);
197 return lmb_add_region(_rgn, base, size);
200 long __init lmb_overlaps_region(struct lmb_region *rgn, unsigned long base,
205 for (i=0; i < rgn->cnt; i++) {
206 unsigned long rgnbase = rgn->region[i].base;
207 unsigned long rgnsize = rgn->region[i].size;
208 if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
213 return (i < rgn->cnt) ? i : -1;
216 unsigned long __init lmb_alloc(unsigned long size, unsigned long align)
218 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
221 unsigned long __init lmb_alloc_base(unsigned long size, unsigned long align,
222 unsigned long max_addr)
225 unsigned long base = 0;
228 /* On 32-bit, make sure we allocate lowmem */
229 if (max_addr == LMB_ALLOC_ANYWHERE)
230 max_addr = __max_low_memory;
232 for (i = lmb.memory.cnt-1; i >= 0; i--) {
233 unsigned long lmbbase = lmb.memory.region[i].base;
234 unsigned long lmbsize = lmb.memory.region[i].size;
236 if (max_addr == LMB_ALLOC_ANYWHERE)
237 base = _ALIGN_DOWN(lmbbase + lmbsize - size, align);
238 else if (lmbbase < max_addr) {
239 base = min(lmbbase + lmbsize, max_addr);
240 base = _ALIGN_DOWN(base - size, align);
244 while ((lmbbase <= base) &&
245 ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) )
246 base = _ALIGN_DOWN(lmb.reserved.region[j].base - size,
249 if ((base != 0) && (lmbbase <= base))
256 lmb_add_region(&lmb.reserved, base, size);
261 /* You must call lmb_analyze() before this. */
262 unsigned long __init lmb_phys_mem_size(void)
264 return lmb.memory.size;
267 unsigned long __init lmb_end_of_DRAM(void)
269 int idx = lmb.memory.cnt - 1;
271 return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
275 * Truncate the lmb list to memory_limit if it's set
276 * You must call lmb_analyze() after this.
278 void __init lmb_enforce_memory_limit(unsigned long memory_limit)
280 unsigned long i, limit;
285 limit = memory_limit;
286 for (i = 0; i < lmb.memory.cnt; i++) {
287 if (limit > lmb.memory.region[i].size) {
288 limit -= lmb.memory.region[i].size;
292 lmb.memory.region[i].size = limit;
293 lmb.memory.cnt = i + 1;