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