Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +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/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/bitops.h> |
Benjamin Herrenschmidt | 449e8df | 2010-07-06 15:39:07 -0700 | [diff] [blame] | 16 | #include <linux/poison.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 17 | #include <linux/memblock.h> |
| 18 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 19 | struct memblock memblock; |
| 20 | |
| 21 | static int memblock_debug; |
Benjamin Herrenschmidt | bf23c51 | 2010-07-06 15:39:06 -0700 | [diff] [blame] | 22 | static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1]; |
| 23 | static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1]; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 24 | |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 25 | #define MEMBLOCK_ERROR (~(phys_addr_t)0) |
| 26 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 27 | /* |
| 28 | * Address comparison utilities |
| 29 | */ |
| 30 | |
| 31 | static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 32 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 33 | return addr & ~(size - 1); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 34 | } |
| 35 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 36 | static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 37 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 38 | return (addr + (size - 1)) & ~(size - 1); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 39 | } |
| 40 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 41 | static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, |
| 42 | phys_addr_t base2, phys_addr_t size2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 43 | { |
| 44 | return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); |
| 45 | } |
| 46 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 47 | static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1, |
| 48 | phys_addr_t base2, phys_addr_t size2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 49 | { |
| 50 | if (base2 == base1 + size1) |
| 51 | return 1; |
| 52 | else if (base1 == base2 + size2) |
| 53 | return -1; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 58 | static long memblock_regions_adjacent(struct memblock_type *type, |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 59 | unsigned long r1, unsigned long r2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 60 | { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 61 | phys_addr_t base1 = type->regions[r1].base; |
| 62 | phys_addr_t size1 = type->regions[r1].size; |
| 63 | phys_addr_t base2 = type->regions[r2].base; |
| 64 | phys_addr_t size2 = type->regions[r2].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 65 | |
| 66 | return memblock_addrs_adjacent(base1, size1, base2, size2); |
| 67 | } |
| 68 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 69 | long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size) |
| 70 | { |
| 71 | unsigned long i; |
| 72 | |
| 73 | for (i = 0; i < type->cnt; i++) { |
| 74 | phys_addr_t rgnbase = type->regions[i].base; |
| 75 | phys_addr_t rgnsize = type->regions[i].size; |
| 76 | if (memblock_addrs_overlap(base, size, rgnbase, rgnsize)) |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | return (i < type->cnt) ? i : -1; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Find, allocate, deallocate or reserve unreserved regions. All allocations |
| 85 | * are top-down. |
| 86 | */ |
| 87 | |
| 88 | static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end, |
| 89 | phys_addr_t size, phys_addr_t align) |
| 90 | { |
| 91 | phys_addr_t base, res_base; |
| 92 | long j; |
| 93 | |
| 94 | base = memblock_align_down((end - size), align); |
| 95 | while (start <= base) { |
| 96 | j = memblock_overlaps_region(&memblock.reserved, base, size); |
| 97 | if (j < 0) |
| 98 | return base; |
| 99 | res_base = memblock.reserved.regions[j].base; |
| 100 | if (res_base < size) |
| 101 | break; |
| 102 | base = memblock_align_down(res_base - size, align); |
| 103 | } |
| 104 | |
| 105 | return MEMBLOCK_ERROR; |
| 106 | } |
| 107 | |
| 108 | static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
| 109 | { |
| 110 | long i; |
| 111 | phys_addr_t base = 0; |
| 112 | phys_addr_t res_base; |
| 113 | |
| 114 | BUG_ON(0 == size); |
| 115 | |
| 116 | size = memblock_align_up(size, align); |
| 117 | |
| 118 | /* Pump up max_addr */ |
| 119 | if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE) |
| 120 | max_addr = memblock.current_limit; |
| 121 | |
| 122 | /* We do a top-down search, this tends to limit memory |
| 123 | * fragmentation by keeping early boot allocs near the |
| 124 | * top of memory |
| 125 | */ |
| 126 | for (i = memblock.memory.cnt - 1; i >= 0; i--) { |
| 127 | phys_addr_t memblockbase = memblock.memory.regions[i].base; |
| 128 | phys_addr_t memblocksize = memblock.memory.regions[i].size; |
| 129 | |
| 130 | if (memblocksize < size) |
| 131 | continue; |
| 132 | base = min(memblockbase + memblocksize, max_addr); |
| 133 | res_base = memblock_find_region(memblockbase, base, size, align); |
| 134 | if (res_base != MEMBLOCK_ERROR) |
| 135 | return res_base; |
| 136 | } |
| 137 | return MEMBLOCK_ERROR; |
| 138 | } |
| 139 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 140 | static void memblock_remove_region(struct memblock_type *type, unsigned long r) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 141 | { |
| 142 | unsigned long i; |
| 143 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 144 | for (i = r; i < type->cnt - 1; i++) { |
| 145 | type->regions[i].base = type->regions[i + 1].base; |
| 146 | type->regions[i].size = type->regions[i + 1].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 147 | } |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 148 | type->cnt--; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* Assumption: base addr of region 1 < base addr of region 2 */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 152 | static void memblock_coalesce_regions(struct memblock_type *type, |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 153 | unsigned long r1, unsigned long r2) |
| 154 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 155 | type->regions[r1].size += type->regions[r2].size; |
| 156 | memblock_remove_region(type, r2); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 157 | } |
| 158 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 159 | static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 160 | { |
| 161 | unsigned long coalesced = 0; |
| 162 | long adjacent, i; |
| 163 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 164 | if ((type->cnt == 1) && (type->regions[0].size == 0)) { |
| 165 | type->regions[0].base = base; |
| 166 | type->regions[0].size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* First try and coalesce this MEMBLOCK with another. */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 171 | for (i = 0; i < type->cnt; i++) { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 172 | phys_addr_t rgnbase = type->regions[i].base; |
| 173 | phys_addr_t rgnsize = type->regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 174 | |
| 175 | if ((rgnbase == base) && (rgnsize == size)) |
| 176 | /* Already have this region, so we're done */ |
| 177 | return 0; |
| 178 | |
| 179 | adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize); |
| 180 | if (adjacent > 0) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 181 | type->regions[i].base -= size; |
| 182 | type->regions[i].size += size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 183 | coalesced++; |
| 184 | break; |
| 185 | } else if (adjacent < 0) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 186 | type->regions[i].size += size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 187 | coalesced++; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 192 | if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) { |
| 193 | memblock_coalesce_regions(type, i, i+1); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 194 | coalesced++; |
| 195 | } |
| 196 | |
| 197 | if (coalesced) |
| 198 | return coalesced; |
Benjamin Herrenschmidt | bf23c51 | 2010-07-06 15:39:06 -0700 | [diff] [blame] | 199 | if (type->cnt >= type->max) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 200 | return -1; |
| 201 | |
| 202 | /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 203 | for (i = type->cnt - 1; i >= 0; i--) { |
| 204 | if (base < type->regions[i].base) { |
| 205 | type->regions[i+1].base = type->regions[i].base; |
| 206 | type->regions[i+1].size = type->regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 207 | } else { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 208 | type->regions[i+1].base = base; |
| 209 | type->regions[i+1].size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 214 | if (base < type->regions[0].base) { |
| 215 | type->regions[0].base = base; |
| 216 | type->regions[0].size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 217 | } |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 218 | type->cnt++; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 223 | long memblock_add(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 224 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 225 | return memblock_add_region(&memblock.memory, base, size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 226 | |
| 227 | } |
| 228 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 229 | static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 230 | { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 231 | phys_addr_t rgnbegin, rgnend; |
| 232 | phys_addr_t end = base + size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 233 | int i; |
| 234 | |
| 235 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
| 236 | |
| 237 | /* Find the region where (base, size) belongs to */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 238 | for (i=0; i < type->cnt; i++) { |
| 239 | rgnbegin = type->regions[i].base; |
| 240 | rgnend = rgnbegin + type->regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 241 | |
| 242 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | /* Didn't find the region */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 247 | if (i == type->cnt) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 248 | return -1; |
| 249 | |
| 250 | /* Check to see if we are removing entire region */ |
| 251 | if ((rgnbegin == base) && (rgnend == end)) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 252 | memblock_remove_region(type, i); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* Check to see if region is matching at the front */ |
| 257 | if (rgnbegin == base) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 258 | type->regions[i].base = end; |
| 259 | type->regions[i].size -= size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /* Check to see if the region is matching at the end */ |
| 264 | if (rgnend == end) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 265 | type->regions[i].size -= size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * We need to split the entry - adjust the current one to the |
| 271 | * beginging of the hole and add the region after hole. |
| 272 | */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 273 | type->regions[i].size = base - type->regions[i].base; |
| 274 | return memblock_add_region(type, end, rgnend - end); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 275 | } |
| 276 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 277 | long memblock_remove(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 278 | { |
| 279 | return __memblock_remove(&memblock.memory, base, size); |
| 280 | } |
| 281 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 282 | long __init memblock_free(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 283 | { |
| 284 | return __memblock_remove(&memblock.reserved, base, size); |
| 285 | } |
| 286 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 287 | long __init memblock_reserve(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 288 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 289 | struct memblock_type *_rgn = &memblock.reserved; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 290 | |
| 291 | BUG_ON(0 == size); |
| 292 | |
| 293 | return memblock_add_region(_rgn, base, size); |
| 294 | } |
| 295 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 296 | phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 297 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 298 | phys_addr_t found; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 299 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 300 | /* We align the size to limit fragmentation. Without this, a lot of |
| 301 | * small allocs quickly eat up the whole reserve array on sparc |
| 302 | */ |
| 303 | size = memblock_align_up(size, align); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 304 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 305 | found = memblock_find_base(size, align, max_addr); |
| 306 | if (found != MEMBLOCK_ERROR && |
| 307 | memblock_add_region(&memblock.reserved, found, size) >= 0) |
| 308 | return found; |
| 309 | |
| 310 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 311 | } |
| 312 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 313 | phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 314 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 315 | phys_addr_t alloc; |
| 316 | |
| 317 | alloc = __memblock_alloc_base(size, align, max_addr); |
| 318 | |
| 319 | if (alloc == 0) |
| 320 | panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n", |
| 321 | (unsigned long long) size, (unsigned long long) max_addr); |
| 322 | |
| 323 | return alloc; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 324 | } |
| 325 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 326 | phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 327 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 328 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 329 | } |
| 330 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 331 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 332 | /* |
| 333 | * Additional node-local allocators. Search for node memory is bottom up |
| 334 | * and walks memblock regions within that node bottom-up as well, but allocation |
| 335 | * within an memblock region is top-down. |
| 336 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 337 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 338 | phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid) |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 339 | { |
| 340 | *nid = 0; |
| 341 | |
| 342 | return end; |
| 343 | } |
| 344 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 345 | static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp, |
| 346 | phys_addr_t size, |
| 347 | phys_addr_t align, int nid) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 348 | { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 349 | phys_addr_t start, end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 350 | |
| 351 | start = mp->base; |
| 352 | end = start + mp->size; |
| 353 | |
| 354 | start = memblock_align_up(start, align); |
| 355 | while (start < end) { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 356 | phys_addr_t this_end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 357 | int this_nid; |
| 358 | |
Benjamin Herrenschmidt | 35a1f0b | 2010-07-06 15:38:58 -0700 | [diff] [blame] | 359 | this_end = memblock_nid_range(start, end, &this_nid); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 360 | if (this_nid == nid) { |
Benjamin Herrenschmidt | 3a9c2c8 | 2010-07-12 13:28:15 +1000 | [diff] [blame] | 361 | phys_addr_t ret = memblock_find_region(start, this_end, size, align); |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 362 | if (ret != MEMBLOCK_ERROR && |
Benjamin Herrenschmidt | 3a9c2c8 | 2010-07-12 13:28:15 +1000 | [diff] [blame] | 363 | memblock_add_region(&memblock.reserved, ret, size) >= 0) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 364 | return ret; |
| 365 | } |
| 366 | start = this_end; |
| 367 | } |
| 368 | |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 369 | return MEMBLOCK_ERROR; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 370 | } |
| 371 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 372 | phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 373 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 374 | struct memblock_type *mem = &memblock.memory; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 375 | int i; |
| 376 | |
| 377 | BUG_ON(0 == size); |
| 378 | |
Benjamin Herrenschmidt | 7f219c7 | 2010-07-12 14:24:57 +1000 | [diff] [blame] | 379 | /* We align the size to limit fragmentation. Without this, a lot of |
| 380 | * small allocs quickly eat up the whole reserve array on sparc |
| 381 | */ |
| 382 | size = memblock_align_up(size, align); |
| 383 | |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 384 | /* We do a bottom-up search for a region with the right |
| 385 | * nid since that's easier considering how memblock_nid_range() |
| 386 | * works |
| 387 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 388 | for (i = 0; i < mem->cnt; i++) { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 389 | phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i], |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 390 | size, align, nid); |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 391 | if (ret != MEMBLOCK_ERROR) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | return memblock_alloc(size, align); |
| 396 | } |
| 397 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 398 | /* You must call memblock_analyze() before this. */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 399 | phys_addr_t __init memblock_phys_mem_size(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 400 | { |
Benjamin Herrenschmidt | 4734b59 | 2010-07-28 14:31:29 +1000 | [diff] [blame] | 401 | return memblock.memory_size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 402 | } |
| 403 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 404 | phys_addr_t memblock_end_of_DRAM(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 405 | { |
| 406 | int idx = memblock.memory.cnt - 1; |
| 407 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 408 | return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* You must call memblock_analyze() after this. */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 412 | void __init memblock_enforce_memory_limit(phys_addr_t memory_limit) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 413 | { |
| 414 | unsigned long i; |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 415 | phys_addr_t limit; |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 416 | struct memblock_region *p; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 417 | |
| 418 | if (!memory_limit) |
| 419 | return; |
| 420 | |
| 421 | /* Truncate the memblock regions to satisfy the memory limit. */ |
| 422 | limit = memory_limit; |
| 423 | for (i = 0; i < memblock.memory.cnt; i++) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 424 | if (limit > memblock.memory.regions[i].size) { |
| 425 | limit -= memblock.memory.regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 426 | continue; |
| 427 | } |
| 428 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 429 | memblock.memory.regions[i].size = limit; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 430 | memblock.memory.cnt = i + 1; |
| 431 | break; |
| 432 | } |
| 433 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 434 | memory_limit = memblock_end_of_DRAM(); |
| 435 | |
| 436 | /* And truncate any reserves above the limit also. */ |
| 437 | for (i = 0; i < memblock.reserved.cnt; i++) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 438 | p = &memblock.reserved.regions[i]; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 439 | |
| 440 | if (p->base > memory_limit) |
| 441 | p->size = 0; |
| 442 | else if ((p->base + p->size) > memory_limit) |
| 443 | p->size = memory_limit - p->base; |
| 444 | |
| 445 | if (p->size == 0) { |
| 446 | memblock_remove_region(&memblock.reserved, i); |
| 447 | i--; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 452 | static int memblock_search(struct memblock_type *type, phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 453 | { |
| 454 | unsigned int left = 0, right = type->cnt; |
| 455 | |
| 456 | do { |
| 457 | unsigned int mid = (right + left) / 2; |
| 458 | |
| 459 | if (addr < type->regions[mid].base) |
| 460 | right = mid; |
| 461 | else if (addr >= (type->regions[mid].base + |
| 462 | type->regions[mid].size)) |
| 463 | left = mid + 1; |
| 464 | else |
| 465 | return mid; |
| 466 | } while (left < right); |
| 467 | return -1; |
| 468 | } |
| 469 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 470 | int __init memblock_is_reserved(phys_addr_t addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 471 | { |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 472 | return memblock_search(&memblock.reserved, addr) != -1; |
| 473 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 474 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 475 | int memblock_is_memory(phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 476 | { |
| 477 | return memblock_search(&memblock.memory, addr) != -1; |
| 478 | } |
| 479 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 480 | int memblock_is_region_memory(phys_addr_t base, phys_addr_t size) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 481 | { |
| 482 | int idx = memblock_search(&memblock.reserved, base); |
| 483 | |
| 484 | if (idx == -1) |
| 485 | return 0; |
| 486 | return memblock.reserved.regions[idx].base <= base && |
| 487 | (memblock.reserved.regions[idx].base + |
| 488 | memblock.reserved.regions[idx].size) >= (base + size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 489 | } |
| 490 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 491 | int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 492 | { |
Benjamin Herrenschmidt | f1c2c19 | 2010-08-04 14:17:17 +1000 | [diff] [blame] | 493 | return memblock_overlaps_region(&memblock.reserved, base, size) >= 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 494 | } |
| 495 | |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 496 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 497 | void __init memblock_set_current_limit(phys_addr_t limit) |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 498 | { |
| 499 | memblock.current_limit = limit; |
| 500 | } |
| 501 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 502 | static void memblock_dump(struct memblock_type *region, char *name) |
| 503 | { |
| 504 | unsigned long long base, size; |
| 505 | int i; |
| 506 | |
| 507 | pr_info(" %s.cnt = 0x%lx\n", name, region->cnt); |
| 508 | |
| 509 | for (i = 0; i < region->cnt; i++) { |
| 510 | base = region->regions[i].base; |
| 511 | size = region->regions[i].size; |
| 512 | |
| 513 | pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n", |
| 514 | name, i, base, base + size - 1, size); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | void memblock_dump_all(void) |
| 519 | { |
| 520 | if (!memblock_debug) |
| 521 | return; |
| 522 | |
| 523 | pr_info("MEMBLOCK configuration:\n"); |
| 524 | pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size); |
| 525 | |
| 526 | memblock_dump(&memblock.memory, "memory"); |
| 527 | memblock_dump(&memblock.reserved, "reserved"); |
| 528 | } |
| 529 | |
| 530 | void __init memblock_analyze(void) |
| 531 | { |
| 532 | int i; |
| 533 | |
| 534 | /* Check marker in the unused last array entry */ |
| 535 | WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base |
| 536 | != (phys_addr_t)RED_INACTIVE); |
| 537 | WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base |
| 538 | != (phys_addr_t)RED_INACTIVE); |
| 539 | |
| 540 | memblock.memory_size = 0; |
| 541 | |
| 542 | for (i = 0; i < memblock.memory.cnt; i++) |
| 543 | memblock.memory_size += memblock.memory.regions[i].size; |
| 544 | } |
| 545 | |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 546 | void __init memblock_init(void) |
| 547 | { |
| 548 | /* Hookup the initial arrays */ |
| 549 | memblock.memory.regions = memblock_memory_init_regions; |
| 550 | memblock.memory.max = INIT_MEMBLOCK_REGIONS; |
| 551 | memblock.reserved.regions = memblock_reserved_init_regions; |
| 552 | memblock.reserved.max = INIT_MEMBLOCK_REGIONS; |
| 553 | |
| 554 | /* Write a marker in the unused last array entry */ |
| 555 | memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE; |
| 556 | memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE; |
| 557 | |
| 558 | /* Create a dummy zero size MEMBLOCK which will get coalesced away later. |
| 559 | * This simplifies the memblock_add() code below... |
| 560 | */ |
| 561 | memblock.memory.regions[0].base = 0; |
| 562 | memblock.memory.regions[0].size = 0; |
| 563 | memblock.memory.cnt = 1; |
| 564 | |
| 565 | /* Ditto. */ |
| 566 | memblock.reserved.regions[0].base = 0; |
| 567 | memblock.reserved.regions[0].size = 0; |
| 568 | memblock.reserved.cnt = 1; |
| 569 | |
| 570 | memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE; |
| 571 | } |
| 572 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame^] | 573 | static int __init early_memblock(char *p) |
| 574 | { |
| 575 | if (p && strstr(p, "debug")) |
| 576 | memblock_debug = 1; |
| 577 | return 0; |
| 578 | } |
| 579 | early_param("memblock", early_memblock); |
| 580 | |