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> |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/bitops.h> |
Benjamin Herrenschmidt | 449e8df | 2010-07-06 15:39:07 -0700 | [diff] [blame] | 17 | #include <linux/poison.h> |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame^] | 18 | #include <linux/pfn.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 19 | #include <linux/memblock.h> |
| 20 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 21 | struct memblock memblock; |
| 22 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 23 | static int memblock_debug, memblock_can_resize; |
Benjamin Herrenschmidt | bf23c51 | 2010-07-06 15:39:06 -0700 | [diff] [blame] | 24 | static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1]; |
| 25 | static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1]; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 26 | |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 27 | #define MEMBLOCK_ERROR (~(phys_addr_t)0) |
| 28 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 29 | /* inline so we don't get a warning when pr_debug is compiled out */ |
| 30 | static inline const char *memblock_type_name(struct memblock_type *type) |
| 31 | { |
| 32 | if (type == &memblock.memory) |
| 33 | return "memory"; |
| 34 | else if (type == &memblock.reserved) |
| 35 | return "reserved"; |
| 36 | else |
| 37 | return "unknown"; |
| 38 | } |
| 39 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 40 | /* |
| 41 | * Address comparison utilities |
| 42 | */ |
| 43 | |
| 44 | 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] | 45 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 46 | return addr & ~(size - 1); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 47 | } |
| 48 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 49 | 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] | 50 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 51 | return (addr + (size - 1)) & ~(size - 1); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 52 | } |
| 53 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 54 | static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, |
| 55 | phys_addr_t base2, phys_addr_t size2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 56 | { |
| 57 | return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); |
| 58 | } |
| 59 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 60 | static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1, |
| 61 | phys_addr_t base2, phys_addr_t size2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 62 | { |
| 63 | if (base2 == base1 + size1) |
| 64 | return 1; |
| 65 | else if (base1 == base2 + size2) |
| 66 | return -1; |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 71 | static long memblock_regions_adjacent(struct memblock_type *type, |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 72 | unsigned long r1, unsigned long r2) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 73 | { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 74 | phys_addr_t base1 = type->regions[r1].base; |
| 75 | phys_addr_t size1 = type->regions[r1].size; |
| 76 | phys_addr_t base2 = type->regions[r2].base; |
| 77 | phys_addr_t size2 = type->regions[r2].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 78 | |
| 79 | return memblock_addrs_adjacent(base1, size1, base2, size2); |
| 80 | } |
| 81 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 82 | long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size) |
| 83 | { |
| 84 | unsigned long i; |
| 85 | |
| 86 | for (i = 0; i < type->cnt; i++) { |
| 87 | phys_addr_t rgnbase = type->regions[i].base; |
| 88 | phys_addr_t rgnsize = type->regions[i].size; |
| 89 | if (memblock_addrs_overlap(base, size, rgnbase, rgnsize)) |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | return (i < type->cnt) ? i : -1; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Find, allocate, deallocate or reserve unreserved regions. All allocations |
| 98 | * are top-down. |
| 99 | */ |
| 100 | |
| 101 | static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end, |
| 102 | phys_addr_t size, phys_addr_t align) |
| 103 | { |
| 104 | phys_addr_t base, res_base; |
| 105 | long j; |
| 106 | |
| 107 | base = memblock_align_down((end - size), align); |
| 108 | while (start <= base) { |
| 109 | j = memblock_overlaps_region(&memblock.reserved, base, size); |
| 110 | if (j < 0) |
| 111 | return base; |
| 112 | res_base = memblock.reserved.regions[j].base; |
| 113 | if (res_base < size) |
| 114 | break; |
| 115 | base = memblock_align_down(res_base - size, align); |
| 116 | } |
| 117 | |
| 118 | return MEMBLOCK_ERROR; |
| 119 | } |
| 120 | |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 121 | static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align, |
| 122 | phys_addr_t start, phys_addr_t end) |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 123 | { |
| 124 | long i; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 125 | |
| 126 | BUG_ON(0 == size); |
| 127 | |
| 128 | size = memblock_align_up(size, align); |
| 129 | |
| 130 | /* Pump up max_addr */ |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 131 | if (end == MEMBLOCK_ALLOC_ACCESSIBLE) |
| 132 | end = memblock.current_limit; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 133 | |
| 134 | /* We do a top-down search, this tends to limit memory |
| 135 | * fragmentation by keeping early boot allocs near the |
| 136 | * top of memory |
| 137 | */ |
| 138 | for (i = memblock.memory.cnt - 1; i >= 0; i--) { |
| 139 | phys_addr_t memblockbase = memblock.memory.regions[i].base; |
| 140 | phys_addr_t memblocksize = memblock.memory.regions[i].size; |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 141 | phys_addr_t bottom, top, found; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 142 | |
| 143 | if (memblocksize < size) |
| 144 | continue; |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 145 | if ((memblockbase + memblocksize) <= start) |
| 146 | break; |
| 147 | bottom = max(memblockbase, start); |
| 148 | top = min(memblockbase + memblocksize, end); |
| 149 | if (bottom >= top) |
| 150 | continue; |
| 151 | found = memblock_find_region(bottom, top, size, align); |
| 152 | if (found != MEMBLOCK_ERROR) |
| 153 | return found; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 154 | } |
| 155 | return MEMBLOCK_ERROR; |
| 156 | } |
| 157 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 158 | static void memblock_remove_region(struct memblock_type *type, unsigned long r) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 159 | { |
| 160 | unsigned long i; |
| 161 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 162 | for (i = r; i < type->cnt - 1; i++) { |
| 163 | type->regions[i].base = type->regions[i + 1].base; |
| 164 | type->regions[i].size = type->regions[i + 1].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 165 | } |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 166 | type->cnt--; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* Assumption: base addr of region 1 < base addr of region 2 */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 170 | static void memblock_coalesce_regions(struct memblock_type *type, |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 171 | unsigned long r1, unsigned long r2) |
| 172 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 173 | type->regions[r1].size += type->regions[r2].size; |
| 174 | memblock_remove_region(type, r2); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 175 | } |
| 176 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 177 | /* Defined below but needed now */ |
| 178 | static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size); |
| 179 | |
| 180 | static int memblock_double_array(struct memblock_type *type) |
| 181 | { |
| 182 | struct memblock_region *new_array, *old_array; |
| 183 | phys_addr_t old_size, new_size, addr; |
| 184 | int use_slab = slab_is_available(); |
| 185 | |
| 186 | /* We don't allow resizing until we know about the reserved regions |
| 187 | * of memory that aren't suitable for allocation |
| 188 | */ |
| 189 | if (!memblock_can_resize) |
| 190 | return -1; |
| 191 | |
| 192 | pr_debug("memblock: %s array full, doubling...", memblock_type_name(type)); |
| 193 | |
| 194 | /* Calculate new doubled size */ |
| 195 | old_size = type->max * sizeof(struct memblock_region); |
| 196 | new_size = old_size << 1; |
| 197 | |
| 198 | /* Try to find some space for it. |
| 199 | * |
| 200 | * WARNING: We assume that either slab_is_available() and we use it or |
| 201 | * we use MEMBLOCK for allocations. That means that this is unsafe to use |
| 202 | * when bootmem is currently active (unless bootmem itself is implemented |
| 203 | * on top of MEMBLOCK which isn't the case yet) |
| 204 | * |
| 205 | * This should however not be an issue for now, as we currently only |
| 206 | * call into MEMBLOCK while it's still active, or much later when slab is |
| 207 | * active for memory hotplug operations |
| 208 | */ |
| 209 | if (use_slab) { |
| 210 | new_array = kmalloc(new_size, GFP_KERNEL); |
| 211 | addr = new_array == NULL ? MEMBLOCK_ERROR : __pa(new_array); |
| 212 | } else |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 213 | addr = memblock_find_base(new_size, sizeof(phys_addr_t), 0, MEMBLOCK_ALLOC_ACCESSIBLE); |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 214 | if (addr == MEMBLOCK_ERROR) { |
| 215 | pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", |
| 216 | memblock_type_name(type), type->max, type->max * 2); |
| 217 | return -1; |
| 218 | } |
| 219 | new_array = __va(addr); |
| 220 | |
| 221 | /* Found space, we now need to move the array over before |
| 222 | * we add the reserved region since it may be our reserved |
| 223 | * array itself that is full. |
| 224 | */ |
| 225 | memcpy(new_array, type->regions, old_size); |
| 226 | memset(new_array + type->max, 0, old_size); |
| 227 | old_array = type->regions; |
| 228 | type->regions = new_array; |
| 229 | type->max <<= 1; |
| 230 | |
| 231 | /* If we use SLAB that's it, we are done */ |
| 232 | if (use_slab) |
| 233 | return 0; |
| 234 | |
| 235 | /* Add the new reserved region now. Should not fail ! */ |
| 236 | BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size) < 0); |
| 237 | |
| 238 | /* If the array wasn't our static init one, then free it. We only do |
| 239 | * that before SLAB is available as later on, we don't know whether |
| 240 | * to use kfree or free_bootmem_pages(). Shouldn't be a big deal |
| 241 | * anyways |
| 242 | */ |
| 243 | if (old_array != memblock_memory_init_regions && |
| 244 | old_array != memblock_reserved_init_regions) |
| 245 | memblock_free(__pa(old_array), old_size); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Benjamin Herrenschmidt | d2cd563 | 2010-07-06 15:39:14 -0700 | [diff] [blame] | 250 | extern int __weak memblock_memory_can_coalesce(phys_addr_t addr1, phys_addr_t size1, |
| 251 | phys_addr_t addr2, phys_addr_t size2) |
| 252 | { |
| 253 | return 1; |
| 254 | } |
| 255 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 256 | 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] | 257 | { |
| 258 | unsigned long coalesced = 0; |
| 259 | long adjacent, i; |
| 260 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 261 | if ((type->cnt == 1) && (type->regions[0].size == 0)) { |
| 262 | type->regions[0].base = base; |
| 263 | type->regions[0].size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* First try and coalesce this MEMBLOCK with another. */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 268 | for (i = 0; i < type->cnt; i++) { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 269 | phys_addr_t rgnbase = type->regions[i].base; |
| 270 | phys_addr_t rgnsize = type->regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 271 | |
| 272 | if ((rgnbase == base) && (rgnsize == size)) |
| 273 | /* Already have this region, so we're done */ |
| 274 | return 0; |
| 275 | |
| 276 | adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize); |
Benjamin Herrenschmidt | d2cd563 | 2010-07-06 15:39:14 -0700 | [diff] [blame] | 277 | /* Check if arch allows coalescing */ |
| 278 | if (adjacent != 0 && type == &memblock.memory && |
| 279 | !memblock_memory_can_coalesce(base, size, rgnbase, rgnsize)) |
| 280 | break; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 281 | if (adjacent > 0) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 282 | type->regions[i].base -= size; |
| 283 | type->regions[i].size += size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 284 | coalesced++; |
| 285 | break; |
| 286 | } else if (adjacent < 0) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 287 | type->regions[i].size += size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 288 | coalesced++; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
Benjamin Herrenschmidt | d2cd563 | 2010-07-06 15:39:14 -0700 | [diff] [blame] | 293 | /* If we plugged a hole, we may want to also coalesce with the |
| 294 | * next region |
| 295 | */ |
| 296 | if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1) && |
| 297 | ((type != &memblock.memory || memblock_memory_can_coalesce(type->regions[i].base, |
| 298 | type->regions[i].size, |
| 299 | type->regions[i+1].base, |
| 300 | type->regions[i+1].size)))) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 301 | memblock_coalesce_regions(type, i, i+1); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 302 | coalesced++; |
| 303 | } |
| 304 | |
| 305 | if (coalesced) |
| 306 | return coalesced; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 307 | |
| 308 | /* If we are out of space, we fail. It's too late to resize the array |
| 309 | * but then this shouldn't have happened in the first place. |
| 310 | */ |
| 311 | if (WARN_ON(type->cnt >= type->max)) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 312 | return -1; |
| 313 | |
| 314 | /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 315 | for (i = type->cnt - 1; i >= 0; i--) { |
| 316 | if (base < type->regions[i].base) { |
| 317 | type->regions[i+1].base = type->regions[i].base; |
| 318 | type->regions[i+1].size = type->regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 319 | } else { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 320 | type->regions[i+1].base = base; |
| 321 | type->regions[i+1].size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | } |
| 325 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 326 | if (base < type->regions[0].base) { |
| 327 | type->regions[0].base = base; |
| 328 | type->regions[0].size = size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 329 | } |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 330 | type->cnt++; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 331 | |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 332 | /* The array is full ? Try to resize it. If that fails, we undo |
| 333 | * our allocation and return an error |
| 334 | */ |
| 335 | if (type->cnt == type->max && memblock_double_array(type)) { |
| 336 | type->cnt--; |
| 337 | return -1; |
| 338 | } |
| 339 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 343 | long memblock_add(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 344 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 345 | return memblock_add_region(&memblock.memory, base, size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 346 | |
| 347 | } |
| 348 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 349 | 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] | 350 | { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 351 | phys_addr_t rgnbegin, rgnend; |
| 352 | phys_addr_t end = base + size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 353 | int i; |
| 354 | |
| 355 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
| 356 | |
| 357 | /* Find the region where (base, size) belongs to */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 358 | for (i=0; i < type->cnt; i++) { |
| 359 | rgnbegin = type->regions[i].base; |
| 360 | rgnend = rgnbegin + type->regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 361 | |
| 362 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | /* Didn't find the region */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 367 | if (i == type->cnt) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 368 | return -1; |
| 369 | |
| 370 | /* Check to see if we are removing entire region */ |
| 371 | if ((rgnbegin == base) && (rgnend == end)) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 372 | memblock_remove_region(type, i); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* Check to see if region is matching at the front */ |
| 377 | if (rgnbegin == base) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 378 | type->regions[i].base = end; |
| 379 | type->regions[i].size -= size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | /* Check to see if the region is matching at the end */ |
| 384 | if (rgnend == end) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 385 | type->regions[i].size -= size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * We need to split the entry - adjust the current one to the |
| 391 | * beginging of the hole and add the region after hole. |
| 392 | */ |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 393 | type->regions[i].size = base - type->regions[i].base; |
| 394 | return memblock_add_region(type, end, rgnend - end); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 395 | } |
| 396 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 397 | long memblock_remove(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 398 | { |
| 399 | return __memblock_remove(&memblock.memory, base, size); |
| 400 | } |
| 401 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 402 | long __init memblock_free(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 403 | { |
| 404 | return __memblock_remove(&memblock.reserved, base, size); |
| 405 | } |
| 406 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 407 | long __init memblock_reserve(phys_addr_t base, phys_addr_t size) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 408 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 409 | struct memblock_type *_rgn = &memblock.reserved; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 410 | |
| 411 | BUG_ON(0 == size); |
| 412 | |
| 413 | return memblock_add_region(_rgn, base, size); |
| 414 | } |
| 415 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 416 | 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] | 417 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 418 | phys_addr_t found; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 419 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 420 | /* We align the size to limit fragmentation. Without this, a lot of |
| 421 | * small allocs quickly eat up the whole reserve array on sparc |
| 422 | */ |
| 423 | size = memblock_align_up(size, align); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 424 | |
Benjamin Herrenschmidt | fef501d | 2010-07-12 15:00:34 +1000 | [diff] [blame] | 425 | found = memblock_find_base(size, align, 0, max_addr); |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 426 | if (found != MEMBLOCK_ERROR && |
| 427 | memblock_add_region(&memblock.reserved, found, size) >= 0) |
| 428 | return found; |
| 429 | |
| 430 | return 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 431 | } |
| 432 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 433 | 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] | 434 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 435 | phys_addr_t alloc; |
| 436 | |
| 437 | alloc = __memblock_alloc_base(size, align, max_addr); |
| 438 | |
| 439 | if (alloc == 0) |
| 440 | panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n", |
| 441 | (unsigned long long) size, (unsigned long long) max_addr); |
| 442 | |
| 443 | return alloc; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 444 | } |
| 445 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 446 | 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] | 447 | { |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 448 | return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 449 | } |
| 450 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 451 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 452 | /* |
| 453 | * Additional node-local allocators. Search for node memory is bottom up |
| 454 | * and walks memblock regions within that node bottom-up as well, but allocation |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame^] | 455 | * within an memblock region is top-down. XXX I plan to fix that at some stage |
| 456 | * |
| 457 | * WARNING: Only available after early_node_map[] has been populated, |
| 458 | * on some architectures, that is after all the calls to add_active_range() |
| 459 | * have been done to populate it. |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 460 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 461 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 462 | 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] | 463 | { |
Benjamin Herrenschmidt | c196f76 | 2010-07-06 15:39:16 -0700 | [diff] [blame^] | 464 | #ifdef CONFIG_ARCH_POPULATES_NODE_MAP |
| 465 | /* |
| 466 | * This code originates from sparc which really wants use to walk by addresses |
| 467 | * and returns the nid. This is not very convenient for early_pfn_map[] users |
| 468 | * as the map isn't sorted yet, and it really wants to be walked by nid. |
| 469 | * |
| 470 | * For now, I implement the inefficient method below which walks the early |
| 471 | * map multiple times. Eventually we may want to use an ARCH config option |
| 472 | * to implement a completely different method for both case. |
| 473 | */ |
| 474 | unsigned long start_pfn, end_pfn; |
| 475 | int i; |
| 476 | |
| 477 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 478 | get_pfn_range_for_nid(i, &start_pfn, &end_pfn); |
| 479 | if (start < PFN_PHYS(start_pfn) || start >= PFN_PHYS(end_pfn)) |
| 480 | continue; |
| 481 | *nid = i; |
| 482 | return min(end, PFN_PHYS(end_pfn)); |
| 483 | } |
| 484 | #endif |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 485 | *nid = 0; |
| 486 | |
| 487 | return end; |
| 488 | } |
| 489 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 490 | static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp, |
| 491 | phys_addr_t size, |
| 492 | phys_addr_t align, int nid) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 493 | { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 494 | phys_addr_t start, end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 495 | |
| 496 | start = mp->base; |
| 497 | end = start + mp->size; |
| 498 | |
| 499 | start = memblock_align_up(start, align); |
| 500 | while (start < end) { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 501 | phys_addr_t this_end; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 502 | int this_nid; |
| 503 | |
Benjamin Herrenschmidt | 35a1f0b | 2010-07-06 15:38:58 -0700 | [diff] [blame] | 504 | this_end = memblock_nid_range(start, end, &this_nid); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 505 | if (this_nid == nid) { |
Benjamin Herrenschmidt | 3a9c2c8 | 2010-07-12 13:28:15 +1000 | [diff] [blame] | 506 | phys_addr_t ret = memblock_find_region(start, this_end, size, align); |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 507 | if (ret != MEMBLOCK_ERROR && |
Benjamin Herrenschmidt | 3a9c2c8 | 2010-07-12 13:28:15 +1000 | [diff] [blame] | 508 | memblock_add_region(&memblock.reserved, ret, size) >= 0) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 509 | return ret; |
| 510 | } |
| 511 | start = this_end; |
| 512 | } |
| 513 | |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 514 | return MEMBLOCK_ERROR; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 515 | } |
| 516 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 517 | 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] | 518 | { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 519 | struct memblock_type *mem = &memblock.memory; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 520 | int i; |
| 521 | |
| 522 | BUG_ON(0 == size); |
| 523 | |
Benjamin Herrenschmidt | 7f219c7 | 2010-07-12 14:24:57 +1000 | [diff] [blame] | 524 | /* We align the size to limit fragmentation. Without this, a lot of |
| 525 | * small allocs quickly eat up the whole reserve array on sparc |
| 526 | */ |
| 527 | size = memblock_align_up(size, align); |
| 528 | |
Benjamin Herrenschmidt | c3f72b5 | 2010-07-06 15:38:59 -0700 | [diff] [blame] | 529 | /* We do a bottom-up search for a region with the right |
| 530 | * nid since that's easier considering how memblock_nid_range() |
| 531 | * works |
| 532 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 533 | for (i = 0; i < mem->cnt; i++) { |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 534 | phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i], |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 535 | size, align, nid); |
Benjamin Herrenschmidt | 4d629f9 | 2010-07-06 15:39:09 -0700 | [diff] [blame] | 536 | if (ret != MEMBLOCK_ERROR) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | return memblock_alloc(size, align); |
| 541 | } |
| 542 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 543 | /* You must call memblock_analyze() before this. */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 544 | phys_addr_t __init memblock_phys_mem_size(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 545 | { |
Benjamin Herrenschmidt | 4734b59 | 2010-07-28 14:31:29 +1000 | [diff] [blame] | 546 | return memblock.memory_size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 547 | } |
| 548 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 549 | phys_addr_t memblock_end_of_DRAM(void) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 550 | { |
| 551 | int idx = memblock.memory.cnt - 1; |
| 552 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 553 | return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* You must call memblock_analyze() after this. */ |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 557 | void __init memblock_enforce_memory_limit(phys_addr_t memory_limit) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 558 | { |
| 559 | unsigned long i; |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 560 | phys_addr_t limit; |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 561 | struct memblock_region *p; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 562 | |
| 563 | if (!memory_limit) |
| 564 | return; |
| 565 | |
| 566 | /* Truncate the memblock regions to satisfy the memory limit. */ |
| 567 | limit = memory_limit; |
| 568 | for (i = 0; i < memblock.memory.cnt; i++) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 569 | if (limit > memblock.memory.regions[i].size) { |
| 570 | limit -= memblock.memory.regions[i].size; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 571 | continue; |
| 572 | } |
| 573 | |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 574 | memblock.memory.regions[i].size = limit; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 575 | memblock.memory.cnt = i + 1; |
| 576 | break; |
| 577 | } |
| 578 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 579 | memory_limit = memblock_end_of_DRAM(); |
| 580 | |
| 581 | /* And truncate any reserves above the limit also. */ |
| 582 | for (i = 0; i < memblock.reserved.cnt; i++) { |
Benjamin Herrenschmidt | e3239ff | 2010-08-04 14:06:41 +1000 | [diff] [blame] | 583 | p = &memblock.reserved.regions[i]; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 584 | |
| 585 | if (p->base > memory_limit) |
| 586 | p->size = 0; |
| 587 | else if ((p->base + p->size) > memory_limit) |
| 588 | p->size = memory_limit - p->base; |
| 589 | |
| 590 | if (p->size == 0) { |
| 591 | memblock_remove_region(&memblock.reserved, i); |
| 592 | i--; |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 597 | static int memblock_search(struct memblock_type *type, phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 598 | { |
| 599 | unsigned int left = 0, right = type->cnt; |
| 600 | |
| 601 | do { |
| 602 | unsigned int mid = (right + left) / 2; |
| 603 | |
| 604 | if (addr < type->regions[mid].base) |
| 605 | right = mid; |
| 606 | else if (addr >= (type->regions[mid].base + |
| 607 | type->regions[mid].size)) |
| 608 | left = mid + 1; |
| 609 | else |
| 610 | return mid; |
| 611 | } while (left < right); |
| 612 | return -1; |
| 613 | } |
| 614 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 615 | int __init memblock_is_reserved(phys_addr_t addr) |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 616 | { |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 617 | return memblock_search(&memblock.reserved, addr) != -1; |
| 618 | } |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 619 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 620 | int memblock_is_memory(phys_addr_t addr) |
Benjamin Herrenschmidt | 72d4b0b | 2010-08-04 14:38:47 +1000 | [diff] [blame] | 621 | { |
| 622 | return memblock_search(&memblock.memory, addr) != -1; |
| 623 | } |
| 624 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 625 | 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] | 626 | { |
| 627 | int idx = memblock_search(&memblock.reserved, base); |
| 628 | |
| 629 | if (idx == -1) |
| 630 | return 0; |
| 631 | return memblock.reserved.regions[idx].base <= base && |
| 632 | (memblock.reserved.regions[idx].base + |
| 633 | memblock.reserved.regions[idx].size) >= (base + size); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 634 | } |
| 635 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 636 | 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] | 637 | { |
Benjamin Herrenschmidt | f1c2c19 | 2010-08-04 14:17:17 +1000 | [diff] [blame] | 638 | return memblock_overlaps_region(&memblock.reserved, base, size) >= 0; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 639 | } |
| 640 | |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 641 | |
Benjamin Herrenschmidt | 2898cc4 | 2010-08-04 13:34:42 +1000 | [diff] [blame] | 642 | void __init memblock_set_current_limit(phys_addr_t limit) |
Benjamin Herrenschmidt | e63075a | 2010-07-06 15:39:01 -0700 | [diff] [blame] | 643 | { |
| 644 | memblock.current_limit = limit; |
| 645 | } |
| 646 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 647 | static void memblock_dump(struct memblock_type *region, char *name) |
| 648 | { |
| 649 | unsigned long long base, size; |
| 650 | int i; |
| 651 | |
| 652 | pr_info(" %s.cnt = 0x%lx\n", name, region->cnt); |
| 653 | |
| 654 | for (i = 0; i < region->cnt; i++) { |
| 655 | base = region->regions[i].base; |
| 656 | size = region->regions[i].size; |
| 657 | |
| 658 | pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n", |
| 659 | name, i, base, base + size - 1, size); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | void memblock_dump_all(void) |
| 664 | { |
| 665 | if (!memblock_debug) |
| 666 | return; |
| 667 | |
| 668 | pr_info("MEMBLOCK configuration:\n"); |
| 669 | pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size); |
| 670 | |
| 671 | memblock_dump(&memblock.memory, "memory"); |
| 672 | memblock_dump(&memblock.reserved, "reserved"); |
| 673 | } |
| 674 | |
| 675 | void __init memblock_analyze(void) |
| 676 | { |
| 677 | int i; |
| 678 | |
| 679 | /* Check marker in the unused last array entry */ |
| 680 | WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base |
| 681 | != (phys_addr_t)RED_INACTIVE); |
| 682 | WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base |
| 683 | != (phys_addr_t)RED_INACTIVE); |
| 684 | |
| 685 | memblock.memory_size = 0; |
| 686 | |
| 687 | for (i = 0; i < memblock.memory.cnt; i++) |
| 688 | memblock.memory_size += memblock.memory.regions[i].size; |
Benjamin Herrenschmidt | 142b45a | 2010-07-06 15:39:13 -0700 | [diff] [blame] | 689 | |
| 690 | /* We allow resizing from there */ |
| 691 | memblock_can_resize = 1; |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 692 | } |
| 693 | |
Benjamin Herrenschmidt | 7590abe | 2010-07-06 15:39:10 -0700 | [diff] [blame] | 694 | void __init memblock_init(void) |
| 695 | { |
| 696 | /* Hookup the initial arrays */ |
| 697 | memblock.memory.regions = memblock_memory_init_regions; |
| 698 | memblock.memory.max = INIT_MEMBLOCK_REGIONS; |
| 699 | memblock.reserved.regions = memblock_reserved_init_regions; |
| 700 | memblock.reserved.max = INIT_MEMBLOCK_REGIONS; |
| 701 | |
| 702 | /* Write a marker in the unused last array entry */ |
| 703 | memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE; |
| 704 | memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE; |
| 705 | |
| 706 | /* Create a dummy zero size MEMBLOCK which will get coalesced away later. |
| 707 | * This simplifies the memblock_add() code below... |
| 708 | */ |
| 709 | memblock.memory.regions[0].base = 0; |
| 710 | memblock.memory.regions[0].size = 0; |
| 711 | memblock.memory.cnt = 1; |
| 712 | |
| 713 | /* Ditto. */ |
| 714 | memblock.reserved.regions[0].base = 0; |
| 715 | memblock.reserved.regions[0].size = 0; |
| 716 | memblock.reserved.cnt = 1; |
| 717 | |
| 718 | memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE; |
| 719 | } |
| 720 | |
Benjamin Herrenschmidt | 6ed311b | 2010-07-12 14:36:48 +1000 | [diff] [blame] | 721 | static int __init early_memblock(char *p) |
| 722 | { |
| 723 | if (p && strstr(p, "debug")) |
| 724 | memblock_debug = 1; |
| 725 | return 0; |
| 726 | } |
| 727 | early_param("memblock", early_memblock); |
| 728 | |