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