Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Procedures for maintaining information about logical memory blocks. |
| 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 | #ifdef CONFIG_PPC32 |
| 22 | #include "mmu_decl.h" /* for __max_low_memory */ |
| 23 | #endif |
| 24 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 25 | #undef DEBUG |
| 26 | |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 27 | #ifdef DEBUG |
| 28 | #include <asm/udbg.h> |
| 29 | #define DBG(fmt...) udbg_printf(fmt) |
| 30 | #else |
| 31 | #define DBG(fmt...) |
| 32 | #endif |
| 33 | |
Michael Ellerman | 3b9331d | 2006-01-25 21:31:30 +1300 | [diff] [blame] | 34 | #define LMB_ALLOC_ANYWHERE 0 |
| 35 | |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 36 | struct lmb lmb; |
| 37 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 38 | void lmb_dump_all(void) |
| 39 | { |
| 40 | #ifdef DEBUG |
| 41 | unsigned long i; |
| 42 | |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 43 | DBG("lmb_dump_all:\n"); |
| 44 | DBG(" memory.cnt = 0x%lx\n", lmb.memory.cnt); |
| 45 | DBG(" memory.size = 0x%lx\n", lmb.memory.size); |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 46 | for (i=0; i < lmb.memory.cnt ;i++) { |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 47 | DBG(" memory.region[0x%x].base = 0x%lx\n", |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 48 | i, lmb.memory.region[i].base); |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 49 | DBG(" .size = 0x%lx\n", |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 50 | lmb.memory.region[i].size); |
| 51 | } |
| 52 | |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 53 | DBG("\n reserved.cnt = 0x%lx\n", lmb.reserved.cnt); |
| 54 | DBG(" reserved.size = 0x%lx\n", lmb.reserved.size); |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 55 | for (i=0; i < lmb.reserved.cnt ;i++) { |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 56 | DBG(" reserved.region[0x%x].base = 0x%lx\n", |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 57 | i, lmb.reserved.region[i].base); |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 58 | DBG(" .size = 0x%lx\n", |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 59 | lmb.reserved.region[i].size); |
| 60 | } |
| 61 | #endif /* DEBUG */ |
| 62 | } |
| 63 | |
| 64 | static unsigned long __init lmb_addrs_overlap(unsigned long base1, |
| 65 | unsigned long size1, unsigned long base2, unsigned long size2) |
| 66 | { |
| 67 | return ((base1 < (base2+size2)) && (base2 < (base1+size1))); |
| 68 | } |
| 69 | |
| 70 | static long __init 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 lmb_regions_adjacent(struct lmb_region *rgn, |
| 82 | 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 lmb_coalesce_regions(struct lmb_region *rgn, |
| 94 | 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].size = rgn->region[i+1].size; |
| 102 | } |
| 103 | rgn->cnt--; |
| 104 | } |
| 105 | |
| 106 | /* This routine called with relocation disabled. */ |
| 107 | void __init lmb_init(void) |
| 108 | { |
| 109 | /* Create a dummy zero size LMB which will get coalesced away later. |
| 110 | * This simplifies the lmb_add() code below... |
| 111 | */ |
| 112 | lmb.memory.region[0].base = 0; |
| 113 | lmb.memory.region[0].size = 0; |
| 114 | lmb.memory.cnt = 1; |
| 115 | |
| 116 | /* Ditto. */ |
| 117 | lmb.reserved.region[0].base = 0; |
| 118 | lmb.reserved.region[0].size = 0; |
| 119 | lmb.reserved.cnt = 1; |
| 120 | } |
| 121 | |
| 122 | /* This routine may be called with relocation disabled. */ |
| 123 | void __init lmb_analyze(void) |
| 124 | { |
| 125 | int i; |
| 126 | |
| 127 | lmb.memory.size = 0; |
| 128 | |
| 129 | for (i = 0; i < lmb.memory.cnt; i++) |
| 130 | lmb.memory.size += lmb.memory.region[i].size; |
| 131 | } |
| 132 | |
| 133 | /* This routine called with relocation disabled. */ |
| 134 | static long __init lmb_add_region(struct lmb_region *rgn, unsigned long base, |
| 135 | unsigned long size) |
| 136 | { |
| 137 | unsigned long i, coalesced = 0; |
| 138 | long adjacent; |
| 139 | |
| 140 | /* First try and coalesce this LMB with another. */ |
| 141 | for (i=0; i < rgn->cnt; i++) { |
| 142 | unsigned long rgnbase = rgn->region[i].base; |
| 143 | unsigned long rgnsize = rgn->region[i].size; |
| 144 | |
| 145 | adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize); |
| 146 | if ( adjacent > 0 ) { |
| 147 | rgn->region[i].base -= size; |
| 148 | rgn->region[i].size += size; |
| 149 | coalesced++; |
| 150 | break; |
| 151 | } |
| 152 | else if ( adjacent < 0 ) { |
| 153 | rgn->region[i].size += size; |
| 154 | coalesced++; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) { |
| 160 | lmb_coalesce_regions(rgn, i, i+1); |
| 161 | coalesced++; |
| 162 | } |
| 163 | |
| 164 | if (coalesced) |
| 165 | return coalesced; |
| 166 | if (rgn->cnt >= MAX_LMB_REGIONS) |
| 167 | return -1; |
| 168 | |
| 169 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
| 170 | for (i = rgn->cnt-1; i >= 0; i--) { |
| 171 | if (base < rgn->region[i].base) { |
| 172 | rgn->region[i+1].base = rgn->region[i].base; |
| 173 | rgn->region[i+1].size = rgn->region[i].size; |
| 174 | } else { |
| 175 | rgn->region[i+1].base = base; |
| 176 | rgn->region[i+1].size = size; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | rgn->cnt++; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* This routine may be called with relocation disabled. */ |
| 186 | long __init lmb_add(unsigned long base, unsigned long size) |
| 187 | { |
| 188 | struct lmb_region *_rgn = &(lmb.memory); |
| 189 | |
| 190 | /* On pSeries LPAR systems, the first LMB is our RMO region. */ |
| 191 | if (base == 0) |
| 192 | lmb.rmo_size = size; |
| 193 | |
| 194 | return lmb_add_region(_rgn, base, size); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | long __init lmb_reserve(unsigned long base, unsigned long size) |
| 199 | { |
| 200 | struct lmb_region *_rgn = &(lmb.reserved); |
| 201 | |
Michael Ellerman | 8c20faf | 2006-01-25 21:31:26 +1300 | [diff] [blame] | 202 | BUG_ON(0 == size); |
| 203 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 204 | return lmb_add_region(_rgn, base, size); |
| 205 | } |
| 206 | |
| 207 | long __init lmb_overlaps_region(struct lmb_region *rgn, unsigned long base, |
| 208 | unsigned long size) |
| 209 | { |
| 210 | unsigned long i; |
| 211 | |
| 212 | for (i=0; i < rgn->cnt; i++) { |
| 213 | unsigned long rgnbase = rgn->region[i].base; |
| 214 | unsigned long rgnsize = rgn->region[i].size; |
| 215 | if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) { |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return (i < rgn->cnt) ? i : -1; |
| 221 | } |
| 222 | |
| 223 | unsigned long __init lmb_alloc(unsigned long size, unsigned long align) |
| 224 | { |
| 225 | return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE); |
| 226 | } |
| 227 | |
| 228 | unsigned long __init lmb_alloc_base(unsigned long size, unsigned long align, |
| 229 | unsigned long max_addr) |
| 230 | { |
Michael Ellerman | d7a5b2f | 2006-01-25 21:31:28 +1300 | [diff] [blame] | 231 | unsigned long alloc; |
| 232 | |
| 233 | alloc = __lmb_alloc_base(size, align, max_addr); |
| 234 | |
Michael Ellerman | 2c27660 | 2006-03-16 14:47:20 +1100 | [diff] [blame] | 235 | if (alloc == 0) |
Michael Ellerman | d7a5b2f | 2006-01-25 21:31:28 +1300 | [diff] [blame] | 236 | panic("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
| 237 | size, max_addr); |
| 238 | |
| 239 | return alloc; |
| 240 | } |
| 241 | |
| 242 | unsigned long __init __lmb_alloc_base(unsigned long size, unsigned long align, |
| 243 | unsigned long max_addr) |
| 244 | { |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 245 | long i, j; |
| 246 | unsigned long base = 0; |
| 247 | |
Michael Ellerman | 8c20faf | 2006-01-25 21:31:26 +1300 | [diff] [blame] | 248 | BUG_ON(0 == size); |
| 249 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 250 | #ifdef CONFIG_PPC32 |
| 251 | /* On 32-bit, make sure we allocate lowmem */ |
| 252 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 253 | max_addr = __max_low_memory; |
| 254 | #endif |
| 255 | for (i = lmb.memory.cnt-1; i >= 0; i--) { |
| 256 | unsigned long lmbbase = lmb.memory.region[i].base; |
| 257 | unsigned long lmbsize = lmb.memory.region[i].size; |
| 258 | |
| 259 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 260 | base = _ALIGN_DOWN(lmbbase + lmbsize - size, align); |
| 261 | else if (lmbbase < max_addr) { |
| 262 | base = min(lmbbase + lmbsize, max_addr); |
| 263 | base = _ALIGN_DOWN(base - size, align); |
| 264 | } else |
| 265 | continue; |
| 266 | |
| 267 | while ((lmbbase <= base) && |
| 268 | ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) ) |
| 269 | base = _ALIGN_DOWN(lmb.reserved.region[j].base - size, |
| 270 | align); |
| 271 | |
| 272 | if ((base != 0) && (lmbbase <= base)) |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | if (i < 0) |
| 277 | return 0; |
| 278 | |
| 279 | lmb_add_region(&lmb.reserved, base, size); |
| 280 | |
| 281 | return base; |
| 282 | } |
| 283 | |
| 284 | /* You must call lmb_analyze() before this. */ |
| 285 | unsigned long __init lmb_phys_mem_size(void) |
| 286 | { |
| 287 | return lmb.memory.size; |
| 288 | } |
| 289 | |
| 290 | unsigned long __init lmb_end_of_DRAM(void) |
| 291 | { |
| 292 | int idx = lmb.memory.cnt - 1; |
| 293 | |
| 294 | return (lmb.memory.region[idx].base + lmb.memory.region[idx].size); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Truncate the lmb list to memory_limit if it's set |
| 299 | * You must call lmb_analyze() after this. |
| 300 | */ |
| 301 | void __init lmb_enforce_memory_limit(unsigned long memory_limit) |
| 302 | { |
| 303 | unsigned long i, limit; |
| 304 | |
| 305 | if (! memory_limit) |
| 306 | return; |
| 307 | |
| 308 | limit = memory_limit; |
| 309 | for (i = 0; i < lmb.memory.cnt; i++) { |
| 310 | if (limit > lmb.memory.region[i].size) { |
| 311 | limit -= lmb.memory.region[i].size; |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | lmb.memory.region[i].size = limit; |
| 316 | lmb.memory.cnt = i + 1; |
| 317 | break; |
| 318 | } |
| 319 | } |