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