Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/bootmem.c |
| 3 | * |
| 4 | * Copyright (C) 1999 Ingo Molnar |
| 5 | * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999 |
| 6 | * |
| 7 | * simple boot-time physical memory area allocator and |
| 8 | * free memory collector. It's used to deal with reserved |
| 9 | * system memory and memory holes as well. |
| 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 12 | #include <linux/pfn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Franck Bui-Huu | e786e86 | 2006-09-25 23:31:06 -0700 | [diff] [blame] | 15 | |
| 16 | #include <asm/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/io.h> |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 18 | #include <asm/processor.h> |
Franck Bui-Huu | e786e86 | 2006-09-25 23:31:06 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
| 22 | /* |
| 23 | * Access to this subsystem has to be serialized externally. (this is |
| 24 | * true for the boot process anyway) |
| 25 | */ |
| 26 | unsigned long max_low_pfn; |
| 27 | unsigned long min_low_pfn; |
| 28 | unsigned long max_pfn; |
| 29 | |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 30 | static LIST_HEAD(bdata_list); |
Vivek Goyal | 92aa63a | 2005-06-25 14:58:18 -0700 | [diff] [blame] | 31 | #ifdef CONFIG_CRASH_DUMP |
| 32 | /* |
| 33 | * If we have booted due to a crash, max_pfn will be a very low value. We need |
| 34 | * to know the amount of memory that the previous kernel used. |
| 35 | */ |
| 36 | unsigned long saved_max_pfn; |
| 37 | #endif |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* return the number of _pages_ that will be allocated for the boot bitmap */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 40 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | unsigned long mapsize; |
| 43 | |
| 44 | mapsize = (pages+7)/8; |
| 45 | mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK; |
| 46 | mapsize >>= PAGE_SHIFT; |
| 47 | |
| 48 | return mapsize; |
| 49 | } |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 50 | |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 51 | /* |
| 52 | * link bdata in order |
| 53 | */ |
Franck Bui-Huu | 69d49e6 | 2006-09-25 23:31:04 -0700 | [diff] [blame] | 54 | static void __init link_bootmem(bootmem_data_t *bdata) |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 55 | { |
| 56 | bootmem_data_t *ent; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 57 | |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 58 | if (list_empty(&bdata_list)) { |
| 59 | list_add(&bdata->list, &bdata_list); |
| 60 | return; |
| 61 | } |
| 62 | /* insert in order */ |
| 63 | list_for_each_entry(ent, &bdata_list, list) { |
| 64 | if (bdata->node_boot_start < ent->node_boot_start) { |
| 65 | list_add_tail(&bdata->list, &ent->list); |
| 66 | return; |
| 67 | } |
| 68 | } |
| 69 | list_add_tail(&bdata->list, &bdata_list); |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Given an initialised bdata, it returns the size of the boot bitmap |
| 74 | */ |
| 75 | static unsigned long __init get_mapsize(bootmem_data_t *bdata) |
| 76 | { |
| 77 | unsigned long mapsize; |
| 78 | unsigned long start = PFN_DOWN(bdata->node_boot_start); |
| 79 | unsigned long end = bdata->node_low_pfn; |
| 80 | |
| 81 | mapsize = ((end - start) + 7) / 8; |
| 82 | return ALIGN(mapsize, sizeof(long)); |
| 83 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Called once to set up the allocator itself. |
| 87 | */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 88 | static unsigned long __init init_bootmem_core(pg_data_t *pgdat, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | unsigned long mapstart, unsigned long start, unsigned long end) |
| 90 | { |
| 91 | bootmem_data_t *bdata = pgdat->bdata; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 92 | unsigned long mapsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 94 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
| 95 | bdata->node_boot_start = PFN_PHYS(start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | bdata->node_low_pfn = end; |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 97 | link_bootmem(bdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Initially all pages are reserved - setup_arch() has to |
| 101 | * register free RAM areas explicitly. |
| 102 | */ |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 103 | mapsize = get_mapsize(bdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
| 105 | |
| 106 | return mapsize; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Marks a particular physical memory range as unallocatable. Usable RAM |
| 111 | * might be used for boot-time allocations - or it might get added |
| 112 | * to the free page pool later on. |
| 113 | */ |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 114 | static int __init can_reserve_bootmem_core(bootmem_data_t *bdata, |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 115 | unsigned long addr, unsigned long size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 117 | unsigned long sidx, eidx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | unsigned long i; |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 119 | |
| 120 | BUG_ON(!size); |
| 121 | |
| 122 | /* out of range, don't hold other */ |
| 123 | if (addr + size < bdata->node_boot_start || |
| 124 | PFN_DOWN(addr) > bdata->node_low_pfn) |
| 125 | return 0; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 128 | * Round up to index to the range. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | */ |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 130 | if (addr > bdata->node_boot_start) |
| 131 | sidx= PFN_DOWN(addr - bdata->node_boot_start); |
| 132 | else |
| 133 | sidx = 0; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 134 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 135 | eidx = PFN_UP(addr + size - bdata->node_boot_start); |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 136 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
| 137 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 139 | for (i = sidx; i < eidx; i++) { |
| 140 | if (test_bit(i, bdata->node_bootmem_map)) { |
| 141 | if (flags & BOOTMEM_EXCLUSIVE) |
| 142 | return -EBUSY; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | |
| 148 | } |
| 149 | |
| 150 | static void __init reserve_bootmem_core(bootmem_data_t *bdata, |
| 151 | unsigned long addr, unsigned long size, int flags) |
| 152 | { |
| 153 | unsigned long sidx, eidx; |
| 154 | unsigned long i; |
| 155 | |
| 156 | BUG_ON(!size); |
| 157 | |
| 158 | /* out of range */ |
| 159 | if (addr + size < bdata->node_boot_start || |
| 160 | PFN_DOWN(addr) > bdata->node_low_pfn) |
| 161 | return; |
| 162 | |
| 163 | /* |
| 164 | * Round up to index to the range. |
| 165 | */ |
| 166 | if (addr > bdata->node_boot_start) |
| 167 | sidx= PFN_DOWN(addr - bdata->node_boot_start); |
| 168 | else |
| 169 | sidx = 0; |
| 170 | |
| 171 | eidx = PFN_UP(addr + size - bdata->node_boot_start); |
| 172 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
| 173 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); |
| 174 | |
| 175 | for (i = sidx; i < eidx; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (test_and_set_bit(i, bdata->node_bootmem_map)) { |
| 177 | #ifdef CONFIG_DEBUG_BOOTMEM |
| 178 | printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE); |
| 179 | #endif |
| 180 | } |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 184 | static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, |
| 185 | unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 187 | unsigned long sidx, eidx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | unsigned long i; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 189 | |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 190 | BUG_ON(!size); |
| 191 | |
| 192 | /* out range */ |
| 193 | if (addr + size < bdata->node_boot_start || |
| 194 | PFN_DOWN(addr) > bdata->node_low_pfn) |
| 195 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /* |
| 197 | * round down end of usable mem, partially free pages are |
| 198 | * considered reserved. |
| 199 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 201 | if (addr >= bdata->node_boot_start && addr < bdata->last_success) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | bdata->last_success = addr; |
| 203 | |
| 204 | /* |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 205 | * Round up to index to the range. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | */ |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 207 | if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start)) |
| 208 | sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start); |
| 209 | else |
| 210 | sidx = 0; |
| 211 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 212 | eidx = PFN_DOWN(addr + size - bdata->node_boot_start); |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 213 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
| 214 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | for (i = sidx; i < eidx; i++) { |
| 217 | if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map))) |
| 218 | BUG(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * We 'merge' subsequent allocations to save space. We might 'lose' |
| 224 | * some fraction of a page if allocations cannot be satisfied due to |
| 225 | * size constraints on boxes where there is physical RAM space |
| 226 | * fragmentation - in these cases (mostly large memory boxes) this |
| 227 | * is not a problem. |
| 228 | * |
| 229 | * On low memory boxes we get it right in 100% of the cases. |
| 230 | * |
| 231 | * alignment has to be a power of 2 value. |
| 232 | * |
| 233 | * NOTE: This function is _not_ reentrant. |
| 234 | */ |
Andi Kleen | 267b480 | 2006-03-25 16:31:10 +0100 | [diff] [blame] | 235 | void * __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 237 | unsigned long align, unsigned long goal, unsigned long limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 239 | unsigned long areasize, preferred; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 240 | unsigned long i, start = 0, incr, eidx, end_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | void *ret; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 242 | unsigned long node_boot_start; |
| 243 | void *node_bootmem_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 245 | if (!size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | printk("__alloc_bootmem_core(): zero-sized request\n"); |
| 247 | BUG(); |
| 248 | } |
| 249 | BUG_ON(align & (align-1)); |
| 250 | |
Christian Krafft | 7c309a6 | 2006-12-06 20:32:41 -0800 | [diff] [blame] | 251 | /* on nodes without memory - bootmem_map is NULL */ |
| 252 | if (!bdata->node_bootmem_map) |
| 253 | return NULL; |
| 254 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 255 | /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */ |
| 256 | node_boot_start = bdata->node_boot_start; |
| 257 | node_bootmem_map = bdata->node_bootmem_map; |
| 258 | if (align) { |
| 259 | node_boot_start = ALIGN(bdata->node_boot_start, align); |
| 260 | if (node_boot_start > bdata->node_boot_start) |
| 261 | node_bootmem_map = (unsigned long *)bdata->node_bootmem_map + |
| 262 | PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG; |
| 263 | } |
| 264 | |
| 265 | if (limit && node_boot_start >= limit) |
| 266 | return NULL; |
| 267 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 268 | end_pfn = bdata->node_low_pfn; |
| 269 | limit = PFN_DOWN(limit); |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 270 | if (limit && end_pfn > limit) |
| 271 | end_pfn = limit; |
| 272 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 273 | eidx = end_pfn - PFN_DOWN(node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * We try to allocate bootmem pages above 'goal' |
| 277 | * first, then we try to allocate lower pages. |
| 278 | */ |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 279 | preferred = 0; |
| 280 | if (goal && PFN_DOWN(goal) < end_pfn) { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 281 | if (goal > node_boot_start) |
| 282 | preferred = goal - node_boot_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 284 | if (bdata->last_success > node_boot_start && |
| 285 | bdata->last_success - node_boot_start >= preferred) |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 286 | if (!limit || (limit && limit > bdata->last_success)) |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 287 | preferred = bdata->last_success - node_boot_start; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 290 | preferred = PFN_DOWN(ALIGN(preferred, align)); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 291 | areasize = (size + PAGE_SIZE-1) / PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | incr = align >> PAGE_SHIFT ? : 1; |
| 293 | |
| 294 | restart_scan: |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 295 | for (i = preferred; i < eidx;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | unsigned long j; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 297 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 298 | i = find_next_zero_bit(node_bootmem_map, eidx, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | i = ALIGN(i, incr); |
Haren Myneni | 66d43e9 | 2005-12-12 00:37:39 -0800 | [diff] [blame] | 300 | if (i >= eidx) |
| 301 | break; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 302 | if (test_bit(i, node_bootmem_map)) { |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 303 | i += incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | continue; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | for (j = i + 1; j < i + areasize; ++j) { |
| 307 | if (j >= eidx) |
| 308 | goto fail_block; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 309 | if (test_bit(j, node_bootmem_map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | goto fail_block; |
| 311 | } |
| 312 | start = i; |
| 313 | goto found; |
| 314 | fail_block: |
| 315 | i = ALIGN(j, incr); |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 316 | if (i == j) |
| 317 | i += incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 320 | if (preferred > 0) { |
| 321 | preferred = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | goto restart_scan; |
| 323 | } |
| 324 | return NULL; |
| 325 | |
| 326 | found: |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 327 | bdata->last_success = PFN_PHYS(start) + node_boot_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | BUG_ON(start >= eidx); |
| 329 | |
| 330 | /* |
| 331 | * Is the next page of the previous allocation-end the start |
| 332 | * of this allocation's buffer? If yes then we can 'merge' |
| 333 | * the previous partial page with this allocation. |
| 334 | */ |
| 335 | if (align < PAGE_SIZE && |
| 336 | bdata->last_offset && bdata->last_pos+1 == start) { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 337 | unsigned long offset, remaining_size; |
Nick Wilson | 8c0e33c | 2005-06-25 14:59:00 -0700 | [diff] [blame] | 338 | offset = ALIGN(bdata->last_offset, align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | BUG_ON(offset > PAGE_SIZE); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 340 | remaining_size = PAGE_SIZE - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (size < remaining_size) { |
| 342 | areasize = 0; |
| 343 | /* last_pos unchanged */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 344 | bdata->last_offset = offset + size; |
| 345 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 346 | offset + node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } else { |
| 348 | remaining_size = size - remaining_size; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 349 | areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE; |
| 350 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 351 | offset + node_boot_start); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 352 | bdata->last_pos = start + areasize - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | bdata->last_offset = remaining_size; |
| 354 | } |
| 355 | bdata->last_offset &= ~PAGE_MASK; |
| 356 | } else { |
| 357 | bdata->last_pos = start + areasize - 1; |
| 358 | bdata->last_offset = size & ~PAGE_MASK; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 359 | ret = phys_to_virt(start * PAGE_SIZE + node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Reserve the area now: |
| 364 | */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 365 | for (i = start; i < start + areasize; i++) |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 366 | if (unlikely(test_and_set_bit(i, node_bootmem_map))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | BUG(); |
| 368 | memset(ret, 0, size); |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat) |
| 373 | { |
| 374 | struct page *page; |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 375 | unsigned long pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | bootmem_data_t *bdata = pgdat->bdata; |
| 377 | unsigned long i, count, total = 0; |
| 378 | unsigned long idx; |
| 379 | unsigned long *map; |
| 380 | int gofast = 0; |
| 381 | |
| 382 | BUG_ON(!bdata->node_bootmem_map); |
| 383 | |
| 384 | count = 0; |
| 385 | /* first extant page of the node */ |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 386 | pfn = PFN_DOWN(bdata->node_boot_start); |
| 387 | idx = bdata->node_low_pfn - pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | map = bdata->node_bootmem_map; |
| 389 | /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */ |
| 390 | if (bdata->node_boot_start == 0 || |
| 391 | ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG)) |
| 392 | gofast = 1; |
| 393 | for (i = 0; i < idx; ) { |
| 394 | unsigned long v = ~map[i / BITS_PER_LONG]; |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if (gofast && v == ~0UL) { |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 397 | int order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 399 | page = pfn_to_page(pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | count += BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | order = ffs(BITS_PER_LONG) - 1; |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 402 | __free_pages_bootmem(page, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | i += BITS_PER_LONG; |
| 404 | page += BITS_PER_LONG; |
| 405 | } else if (v) { |
| 406 | unsigned long m; |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 407 | |
| 408 | page = pfn_to_page(pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | for (m = 1; m && i < idx; m<<=1, page++, i++) { |
| 410 | if (v & m) { |
| 411 | count++; |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 412 | __free_pages_bootmem(page, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } else { |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 416 | i += BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 418 | pfn += BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | total += count; |
| 421 | |
| 422 | /* |
| 423 | * Now free the allocator bitmap itself, it's not |
| 424 | * needed anymore: |
| 425 | */ |
| 426 | page = virt_to_page(bdata->node_bootmem_map); |
| 427 | count = 0; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 428 | idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT; |
| 429 | for (i = 0; i < idx; i++, page++) { |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 430 | __free_pages_bootmem(page, 0); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 431 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | total += count; |
| 434 | bdata->node_bootmem_map = NULL; |
| 435 | |
| 436 | return total; |
| 437 | } |
| 438 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 439 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 440 | unsigned long startpfn, unsigned long endpfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 442 | return init_bootmem_core(pgdat, freepfn, startpfn, endpfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 445 | void __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 446 | unsigned long size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 448 | int ret; |
| 449 | |
| 450 | ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); |
| 451 | if (ret < 0) |
| 452 | return; |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 453 | reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 456 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 457 | unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
| 459 | free_bootmem_core(pgdat->bdata, physaddr, size); |
| 460 | } |
| 461 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 462 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 464 | return free_all_bootmem_core(pgdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 467 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
| 469 | max_low_pfn = pages; |
| 470 | min_low_pfn = start; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 471 | return init_bootmem_core(NODE_DATA(0), start, 0, pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 475 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
| 476 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 478 | bootmem_data_t *bdata; |
| 479 | int ret; |
| 480 | |
| 481 | list_for_each_entry(bdata, &bdata_list, list) { |
| 482 | ret = can_reserve_bootmem_core(bdata, addr, size, flags); |
| 483 | if (ret < 0) |
| 484 | return ret; |
| 485 | } |
| 486 | list_for_each_entry(bdata, &bdata_list, list) |
| 487 | reserve_bootmem_core(bdata, addr, size, flags); |
| 488 | |
| 489 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */ |
| 492 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 493 | void __init free_bootmem(unsigned long addr, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 495 | bootmem_data_t *bdata; |
| 496 | list_for_each_entry(bdata, &bdata_list, list) |
| 497 | free_bootmem_core(bdata, addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 500 | unsigned long __init free_all_bootmem(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 502 | return free_all_bootmem_core(NODE_DATA(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 505 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
| 506 | unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 508 | bootmem_data_t *bdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | void *ptr; |
| 510 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 511 | list_for_each_entry(bdata, &bdata_list, list) { |
| 512 | ptr = __alloc_bootmem_core(bdata, size, align, goal, 0); |
| 513 | if (ptr) |
| 514 | return ptr; |
| 515 | } |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 516 | return NULL; |
| 517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 519 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
| 520 | unsigned long goal) |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 521 | { |
| 522 | void *mem = __alloc_bootmem_nopanic(size,align,goal); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 523 | |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 524 | if (mem) |
| 525 | return mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /* |
| 527 | * Whoops, we cannot satisfy the allocation request. |
| 528 | */ |
| 529 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); |
| 530 | panic("Out of memory"); |
| 531 | return NULL; |
| 532 | } |
| 533 | |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 534 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 535 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
| 536 | unsigned long align, unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | { |
| 538 | void *ptr; |
| 539 | |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 540 | ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if (ptr) |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 542 | return ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 544 | return __alloc_bootmem(size, align, goal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 547 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
| 548 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL |
| 549 | #endif |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 550 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 551 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
| 552 | unsigned long goal) |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 553 | { |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 554 | bootmem_data_t *bdata; |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 555 | void *ptr; |
| 556 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 557 | list_for_each_entry(bdata, &bdata_list, list) { |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 558 | ptr = __alloc_bootmem_core(bdata, size, align, goal, |
| 559 | ARCH_LOW_ADDRESS_LIMIT); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 560 | if (ptr) |
| 561 | return ptr; |
| 562 | } |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 563 | |
| 564 | /* |
| 565 | * Whoops, we cannot satisfy the allocation request. |
| 566 | */ |
| 567 | printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size); |
| 568 | panic("Out of low memory"); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
| 573 | unsigned long align, unsigned long goal) |
| 574 | { |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 575 | return __alloc_bootmem_core(pgdat->bdata, size, align, goal, |
| 576 | ARCH_LOW_ADDRESS_LIMIT); |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 577 | } |