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. |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 6 | * |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 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 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/bitops.h> |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 16 | #include <linux/lmb.h> |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 17 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 18 | #undef DEBUG |
| 19 | |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 20 | #ifdef DEBUG |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 21 | #define DBG(fmt...) LMB_DBG(fmt) |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 22 | #else |
| 23 | #define DBG(fmt...) |
| 24 | #endif |
| 25 | |
Michael Ellerman | 3b9331d | 2006-01-25 21:31:30 +1300 | [diff] [blame] | 26 | #define LMB_ALLOC_ANYWHERE 0 |
| 27 | |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 28 | struct lmb lmb; |
| 29 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 30 | void lmb_dump_all(void) |
| 31 | { |
| 32 | #ifdef DEBUG |
| 33 | unsigned long i; |
| 34 | |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 35 | DBG("lmb_dump_all:\n"); |
| 36 | DBG(" memory.cnt = 0x%lx\n", lmb.memory.cnt); |
| 37 | DBG(" memory.size = 0x%lx\n", lmb.memory.size); |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 38 | for (i=0; i < lmb.memory.cnt ;i++) { |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 39 | DBG(" memory.region[0x%x].base = 0x%lx\n", |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 40 | i, lmb.memory.region[i].base); |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 41 | DBG(" .size = 0x%lx\n", |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 42 | lmb.memory.region[i].size); |
| 43 | } |
| 44 | |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 45 | DBG("\n reserved.cnt = 0x%lx\n", lmb.reserved.cnt); |
| 46 | DBG(" reserved.size = 0x%lx\n", lmb.reserved.size); |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 47 | for (i=0; i < lmb.reserved.cnt ;i++) { |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 48 | DBG(" reserved.region[0x%x].base = 0x%lx\n", |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 49 | i, lmb.reserved.region[i].base); |
Michael Ellerman | eb48189 | 2005-11-15 14:49:22 +1100 | [diff] [blame] | 50 | DBG(" .size = 0x%lx\n", |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 51 | lmb.reserved.region[i].size); |
| 52 | } |
| 53 | #endif /* DEBUG */ |
| 54 | } |
| 55 | |
| 56 | static unsigned long __init lmb_addrs_overlap(unsigned long base1, |
| 57 | unsigned long size1, unsigned long base2, unsigned long size2) |
| 58 | { |
| 59 | return ((base1 < (base2+size2)) && (base2 < (base1+size1))); |
| 60 | } |
| 61 | |
| 62 | static long __init lmb_addrs_adjacent(unsigned long base1, unsigned long size1, |
| 63 | unsigned long base2, unsigned long size2) |
| 64 | { |
| 65 | if (base2 == base1 + size1) |
| 66 | return 1; |
| 67 | else if (base1 == base2 + size2) |
| 68 | return -1; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static long __init lmb_regions_adjacent(struct lmb_region *rgn, |
| 74 | unsigned long r1, unsigned long r2) |
| 75 | { |
| 76 | unsigned long base1 = rgn->region[r1].base; |
| 77 | unsigned long size1 = rgn->region[r1].size; |
| 78 | unsigned long base2 = rgn->region[r2].base; |
| 79 | unsigned long size2 = rgn->region[r2].size; |
| 80 | |
| 81 | return lmb_addrs_adjacent(base1, size1, base2, size2); |
| 82 | } |
| 83 | |
Michael Ellerman | 2babf5c | 2006-05-17 18:00:46 +1000 | [diff] [blame] | 84 | static void __init lmb_remove_region(struct lmb_region *rgn, unsigned long r) |
| 85 | { |
| 86 | unsigned long i; |
| 87 | |
| 88 | for (i = r; i < rgn->cnt - 1; i++) { |
| 89 | rgn->region[i].base = rgn->region[i + 1].base; |
| 90 | rgn->region[i].size = rgn->region[i + 1].size; |
| 91 | } |
| 92 | rgn->cnt--; |
| 93 | } |
| 94 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 95 | /* Assumption: base addr of region 1 < base addr of region 2 */ |
| 96 | static void __init lmb_coalesce_regions(struct lmb_region *rgn, |
| 97 | unsigned long r1, unsigned long r2) |
| 98 | { |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 99 | rgn->region[r1].size += rgn->region[r2].size; |
Michael Ellerman | 2babf5c | 2006-05-17 18:00:46 +1000 | [diff] [blame] | 100 | lmb_remove_region(rgn, r2); |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* This routine called with relocation disabled. */ |
| 104 | void __init lmb_init(void) |
| 105 | { |
| 106 | /* Create a dummy zero size LMB which will get coalesced away later. |
| 107 | * This simplifies the lmb_add() code below... |
| 108 | */ |
| 109 | lmb.memory.region[0].base = 0; |
| 110 | lmb.memory.region[0].size = 0; |
| 111 | lmb.memory.cnt = 1; |
| 112 | |
| 113 | /* Ditto. */ |
| 114 | lmb.reserved.region[0].base = 0; |
| 115 | lmb.reserved.region[0].size = 0; |
| 116 | lmb.reserved.cnt = 1; |
| 117 | } |
| 118 | |
| 119 | /* This routine may be called with relocation disabled. */ |
| 120 | void __init lmb_analyze(void) |
| 121 | { |
| 122 | int i; |
| 123 | |
| 124 | lmb.memory.size = 0; |
| 125 | |
| 126 | for (i = 0; i < lmb.memory.cnt; i++) |
| 127 | lmb.memory.size += lmb.memory.region[i].size; |
| 128 | } |
| 129 | |
| 130 | /* This routine called with relocation disabled. */ |
| 131 | static long __init lmb_add_region(struct lmb_region *rgn, unsigned long base, |
| 132 | unsigned long size) |
| 133 | { |
Manish Ahuja | 56d6d1a | 2007-07-10 05:03:45 +1000 | [diff] [blame] | 134 | unsigned long coalesced = 0; |
| 135 | long adjacent, i; |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 136 | |
| 137 | /* First try and coalesce this LMB with another. */ |
| 138 | for (i=0; i < rgn->cnt; i++) { |
| 139 | unsigned long rgnbase = rgn->region[i].base; |
| 140 | unsigned long rgnsize = rgn->region[i].size; |
| 141 | |
David Gibson | eb6de28 | 2007-02-28 14:12:29 +1100 | [diff] [blame] | 142 | if ((rgnbase == base) && (rgnsize == size)) |
| 143 | /* Already have this region, so we're done */ |
| 144 | return 0; |
| 145 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 146 | adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize); |
| 147 | if ( adjacent > 0 ) { |
| 148 | rgn->region[i].base -= size; |
| 149 | rgn->region[i].size += size; |
| 150 | coalesced++; |
| 151 | break; |
| 152 | } |
| 153 | else if ( adjacent < 0 ) { |
| 154 | rgn->region[i].size += size; |
| 155 | coalesced++; |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) { |
| 161 | lmb_coalesce_regions(rgn, i, i+1); |
| 162 | coalesced++; |
| 163 | } |
| 164 | |
| 165 | if (coalesced) |
| 166 | return coalesced; |
| 167 | if (rgn->cnt >= MAX_LMB_REGIONS) |
| 168 | return -1; |
| 169 | |
| 170 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
| 171 | for (i = rgn->cnt-1; i >= 0; i--) { |
| 172 | if (base < rgn->region[i].base) { |
| 173 | rgn->region[i+1].base = rgn->region[i].base; |
| 174 | rgn->region[i+1].size = rgn->region[i].size; |
| 175 | } else { |
| 176 | rgn->region[i+1].base = base; |
| 177 | rgn->region[i+1].size = size; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | rgn->cnt++; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* This routine may be called with relocation disabled. */ |
| 187 | long __init lmb_add(unsigned long base, unsigned long size) |
| 188 | { |
| 189 | struct lmb_region *_rgn = &(lmb.memory); |
| 190 | |
| 191 | /* On pSeries LPAR systems, the first LMB is our RMO region. */ |
| 192 | if (base == 0) |
| 193 | lmb.rmo_size = size; |
| 194 | |
| 195 | return lmb_add_region(_rgn, base, size); |
| 196 | |
| 197 | } |
| 198 | |
| 199 | long __init lmb_reserve(unsigned long base, unsigned long size) |
| 200 | { |
| 201 | struct lmb_region *_rgn = &(lmb.reserved); |
| 202 | |
Michael Ellerman | 8c20faf | 2006-01-25 21:31:26 +1300 | [diff] [blame] | 203 | BUG_ON(0 == size); |
| 204 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 205 | return lmb_add_region(_rgn, base, size); |
| 206 | } |
| 207 | |
| 208 | long __init lmb_overlaps_region(struct lmb_region *rgn, unsigned long base, |
| 209 | unsigned long size) |
| 210 | { |
| 211 | unsigned long i; |
| 212 | |
| 213 | for (i=0; i < rgn->cnt; i++) { |
| 214 | unsigned long rgnbase = rgn->region[i].base; |
| 215 | unsigned long rgnsize = rgn->region[i].size; |
| 216 | if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) { |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return (i < rgn->cnt) ? i : -1; |
| 222 | } |
| 223 | |
| 224 | unsigned long __init lmb_alloc(unsigned long size, unsigned long align) |
| 225 | { |
| 226 | return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE); |
| 227 | } |
| 228 | |
| 229 | unsigned long __init lmb_alloc_base(unsigned long size, unsigned long align, |
| 230 | unsigned long max_addr) |
| 231 | { |
Michael Ellerman | d7a5b2f | 2006-01-25 21:31:28 +1300 | [diff] [blame] | 232 | unsigned long alloc; |
| 233 | |
| 234 | alloc = __lmb_alloc_base(size, align, max_addr); |
| 235 | |
Michael Ellerman | 2c276603 | 2006-03-16 14:47:20 +1100 | [diff] [blame] | 236 | if (alloc == 0) |
Michael Ellerman | d7a5b2f | 2006-01-25 21:31:28 +1300 | [diff] [blame] | 237 | panic("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
| 238 | size, max_addr); |
| 239 | |
| 240 | return alloc; |
| 241 | } |
| 242 | |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 243 | static unsigned long lmb_align_down(unsigned long addr, unsigned long size) |
| 244 | { |
| 245 | return addr & ~(size - 1); |
| 246 | } |
| 247 | |
David S. Miller | eea89e1 | 2008-02-13 16:57:09 -0800 | [diff] [blame^] | 248 | static unsigned long lmb_align_up(unsigned long addr, unsigned long size) |
| 249 | { |
| 250 | return (addr + (size - 1)) & ~(size - 1); |
| 251 | } |
| 252 | |
Michael Ellerman | d7a5b2f | 2006-01-25 21:31:28 +1300 | [diff] [blame] | 253 | unsigned long __init __lmb_alloc_base(unsigned long size, unsigned long align, |
| 254 | unsigned long max_addr) |
| 255 | { |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 256 | long i, j; |
| 257 | unsigned long base = 0; |
| 258 | |
Michael Ellerman | 8c20faf | 2006-01-25 21:31:26 +1300 | [diff] [blame] | 259 | BUG_ON(0 == size); |
| 260 | |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 261 | /* On some platforms, make sure we allocate lowmem */ |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 262 | if (max_addr == LMB_ALLOC_ANYWHERE) |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 263 | max_addr = LMB_REAL_LIMIT; |
| 264 | |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 265 | for (i = lmb.memory.cnt-1; i >= 0; i--) { |
| 266 | unsigned long lmbbase = lmb.memory.region[i].base; |
| 267 | unsigned long lmbsize = lmb.memory.region[i].size; |
| 268 | |
| 269 | if (max_addr == LMB_ALLOC_ANYWHERE) |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 270 | base = lmb_align_down(lmbbase + lmbsize - size, align); |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 271 | else if (lmbbase < max_addr) { |
| 272 | base = min(lmbbase + lmbsize, max_addr); |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 273 | base = lmb_align_down(base - size, align); |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 274 | } else |
| 275 | continue; |
| 276 | |
| 277 | while ((lmbbase <= base) && |
| 278 | ((j = lmb_overlaps_region(&lmb.reserved, base, size)) >= 0) ) |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 279 | base = lmb_align_down(lmb.reserved.region[j].base - size, |
| 280 | align); |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 281 | |
| 282 | if ((base != 0) && (lmbbase <= base)) |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | if (i < 0) |
| 287 | return 0; |
| 288 | |
David S. Miller | eea89e1 | 2008-02-13 16:57:09 -0800 | [diff] [blame^] | 289 | if (lmb_add_region(&lmb.reserved, base, lmb_align_up(size, align)) < 0) |
| 290 | return 0; |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 291 | |
| 292 | return base; |
| 293 | } |
| 294 | |
| 295 | /* You must call lmb_analyze() before this. */ |
| 296 | unsigned long __init lmb_phys_mem_size(void) |
| 297 | { |
| 298 | return lmb.memory.size; |
| 299 | } |
| 300 | |
| 301 | unsigned long __init lmb_end_of_DRAM(void) |
| 302 | { |
| 303 | int idx = lmb.memory.cnt - 1; |
| 304 | |
| 305 | return (lmb.memory.region[idx].base + lmb.memory.region[idx].size); |
| 306 | } |
| 307 | |
Michael Ellerman | 2babf5c | 2006-05-17 18:00:46 +1000 | [diff] [blame] | 308 | /* You must call lmb_analyze() after this. */ |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 309 | void __init lmb_enforce_memory_limit(unsigned long memory_limit) |
| 310 | { |
| 311 | unsigned long i, limit; |
Michael Ellerman | 2babf5c | 2006-05-17 18:00:46 +1000 | [diff] [blame] | 312 | struct lmb_property *p; |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 313 | |
| 314 | if (! memory_limit) |
| 315 | return; |
| 316 | |
Michael Ellerman | 2babf5c | 2006-05-17 18:00:46 +1000 | [diff] [blame] | 317 | /* Truncate the lmb regions to satisfy the memory limit. */ |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 318 | limit = memory_limit; |
| 319 | for (i = 0; i < lmb.memory.cnt; i++) { |
| 320 | if (limit > lmb.memory.region[i].size) { |
| 321 | limit -= lmb.memory.region[i].size; |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | lmb.memory.region[i].size = limit; |
| 326 | lmb.memory.cnt = i + 1; |
| 327 | break; |
| 328 | } |
Michael Ellerman | 2babf5c | 2006-05-17 18:00:46 +1000 | [diff] [blame] | 329 | |
Michael Ellerman | 30f30e1 | 2006-07-04 17:13:23 +1000 | [diff] [blame] | 330 | if (lmb.memory.region[0].size < lmb.rmo_size) |
| 331 | lmb.rmo_size = lmb.memory.region[0].size; |
Michael Ellerman | 2babf5c | 2006-05-17 18:00:46 +1000 | [diff] [blame] | 332 | |
| 333 | /* And truncate any reserves above the limit also. */ |
| 334 | for (i = 0; i < lmb.reserved.cnt; i++) { |
| 335 | p = &lmb.reserved.region[i]; |
| 336 | |
| 337 | if (p->base > memory_limit) |
| 338 | p->size = 0; |
| 339 | else if ((p->base + p->size) > memory_limit) |
| 340 | p->size = memory_limit - p->base; |
| 341 | |
| 342 | if (p->size == 0) { |
| 343 | lmb_remove_region(&lmb.reserved, i); |
| 344 | i--; |
| 345 | } |
| 346 | } |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 347 | } |
Kumar Gala | f98eeb4 | 2008-01-09 11:27:23 -0600 | [diff] [blame] | 348 | |
| 349 | int __init lmb_is_reserved(unsigned long addr) |
| 350 | { |
| 351 | int i; |
| 352 | |
| 353 | for (i = 0; i < lmb.reserved.cnt; i++) { |
| 354 | unsigned long upper = lmb.reserved.region[i].base + |
| 355 | lmb.reserved.region[i].size - 1; |
| 356 | if ((addr >= lmb.reserved.region[i].base) && (addr <= upper)) |
| 357 | return 1; |
| 358 | } |
| 359 | return 0; |
| 360 | } |