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