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