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 | */ |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 114 | static int __init reserve_bootmem_core(bootmem_data_t *bdata, |
| 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; |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 119 | int ret; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* |
| 122 | * round up, partially reserved pages are considered |
| 123 | * fully reserved. |
| 124 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | BUG_ON(!size); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 126 | BUG_ON(PFN_DOWN(addr) >= bdata->node_low_pfn); |
| 127 | BUG_ON(PFN_UP(addr + size) > bdata->node_low_pfn); |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 128 | BUG_ON(addr < bdata->node_boot_start); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 129 | |
| 130 | sidx = PFN_DOWN(addr - bdata->node_boot_start); |
| 131 | eidx = PFN_UP(addr + size - bdata->node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | for (i = sidx; i < eidx; i++) |
| 134 | if (test_and_set_bit(i, bdata->node_bootmem_map)) { |
| 135 | #ifdef CONFIG_DEBUG_BOOTMEM |
| 136 | printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE); |
| 137 | #endif |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 138 | if (flags & BOOTMEM_EXCLUSIVE) { |
| 139 | ret = -EBUSY; |
| 140 | goto err; |
| 141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | |
| 146 | err: |
| 147 | /* unreserve memory we accidentally reserved */ |
| 148 | for (i--; i >= sidx; i--) |
| 149 | clear_bit(i, bdata->node_bootmem_map); |
| 150 | |
| 151 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 154 | static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, |
| 155 | unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 157 | unsigned long sidx, eidx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | unsigned long i; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 159 | |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 160 | BUG_ON(!size); |
| 161 | |
| 162 | /* out range */ |
| 163 | if (addr + size < bdata->node_boot_start || |
| 164 | PFN_DOWN(addr) > bdata->node_low_pfn) |
| 165 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* |
| 167 | * round down end of usable mem, partially free pages are |
| 168 | * considered reserved. |
| 169 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 171 | if (addr >= bdata->node_boot_start && addr < bdata->last_success) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | bdata->last_success = addr; |
| 173 | |
| 174 | /* |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 175 | * Round up to index to the range. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | */ |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 177 | if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start)) |
| 178 | sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start); |
| 179 | else |
| 180 | sidx = 0; |
| 181 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 182 | eidx = PFN_DOWN(addr + size - bdata->node_boot_start); |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 183 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
| 184 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | for (i = sidx; i < eidx; i++) { |
| 187 | if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map))) |
| 188 | BUG(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * We 'merge' subsequent allocations to save space. We might 'lose' |
| 194 | * some fraction of a page if allocations cannot be satisfied due to |
| 195 | * size constraints on boxes where there is physical RAM space |
| 196 | * fragmentation - in these cases (mostly large memory boxes) this |
| 197 | * is not a problem. |
| 198 | * |
| 199 | * On low memory boxes we get it right in 100% of the cases. |
| 200 | * |
| 201 | * alignment has to be a power of 2 value. |
| 202 | * |
| 203 | * NOTE: This function is _not_ reentrant. |
| 204 | */ |
Andi Kleen | 267b480 | 2006-03-25 16:31:10 +0100 | [diff] [blame] | 205 | void * __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 207 | unsigned long align, unsigned long goal, unsigned long limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 209 | unsigned long areasize, preferred; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 210 | unsigned long i, start = 0, incr, eidx, end_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | void *ret; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 212 | unsigned long node_boot_start; |
| 213 | void *node_bootmem_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 215 | if (!size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | printk("__alloc_bootmem_core(): zero-sized request\n"); |
| 217 | BUG(); |
| 218 | } |
| 219 | BUG_ON(align & (align-1)); |
| 220 | |
Christian Krafft | 7c309a6 | 2006-12-06 20:32:41 -0800 | [diff] [blame] | 221 | /* on nodes without memory - bootmem_map is NULL */ |
| 222 | if (!bdata->node_bootmem_map) |
| 223 | return NULL; |
| 224 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 225 | /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */ |
| 226 | node_boot_start = bdata->node_boot_start; |
| 227 | node_bootmem_map = bdata->node_bootmem_map; |
| 228 | if (align) { |
| 229 | node_boot_start = ALIGN(bdata->node_boot_start, align); |
| 230 | if (node_boot_start > bdata->node_boot_start) |
| 231 | node_bootmem_map = (unsigned long *)bdata->node_bootmem_map + |
| 232 | PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG; |
| 233 | } |
| 234 | |
| 235 | if (limit && node_boot_start >= limit) |
| 236 | return NULL; |
| 237 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 238 | end_pfn = bdata->node_low_pfn; |
| 239 | limit = PFN_DOWN(limit); |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 240 | if (limit && end_pfn > limit) |
| 241 | end_pfn = limit; |
| 242 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 243 | eidx = end_pfn - PFN_DOWN(node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * We try to allocate bootmem pages above 'goal' |
| 247 | * first, then we try to allocate lower pages. |
| 248 | */ |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 249 | preferred = 0; |
| 250 | if (goal && PFN_DOWN(goal) < end_pfn) { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 251 | if (goal > node_boot_start) |
| 252 | preferred = goal - node_boot_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 254 | if (bdata->last_success > node_boot_start && |
| 255 | bdata->last_success - node_boot_start >= preferred) |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 256 | if (!limit || (limit && limit > bdata->last_success)) |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 257 | preferred = bdata->last_success - node_boot_start; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 260 | preferred = PFN_DOWN(ALIGN(preferred, align)); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 261 | areasize = (size + PAGE_SIZE-1) / PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | incr = align >> PAGE_SHIFT ? : 1; |
| 263 | |
| 264 | restart_scan: |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 265 | for (i = preferred; i < eidx;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | unsigned long j; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 267 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 268 | i = find_next_zero_bit(node_bootmem_map, eidx, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | i = ALIGN(i, incr); |
Haren Myneni | 66d43e9 | 2005-12-12 00:37:39 -0800 | [diff] [blame] | 270 | if (i >= eidx) |
| 271 | break; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 272 | if (test_bit(i, node_bootmem_map)) { |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 273 | i += incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | continue; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | for (j = i + 1; j < i + areasize; ++j) { |
| 277 | if (j >= eidx) |
| 278 | goto fail_block; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 279 | if (test_bit(j, node_bootmem_map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | goto fail_block; |
| 281 | } |
| 282 | start = i; |
| 283 | goto found; |
| 284 | fail_block: |
| 285 | i = ALIGN(j, incr); |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 286 | if (i == j) |
| 287 | i += incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 290 | if (preferred > 0) { |
| 291 | preferred = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | goto restart_scan; |
| 293 | } |
| 294 | return NULL; |
| 295 | |
| 296 | found: |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 297 | bdata->last_success = PFN_PHYS(start) + node_boot_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | BUG_ON(start >= eidx); |
| 299 | |
| 300 | /* |
| 301 | * Is the next page of the previous allocation-end the start |
| 302 | * of this allocation's buffer? If yes then we can 'merge' |
| 303 | * the previous partial page with this allocation. |
| 304 | */ |
| 305 | if (align < PAGE_SIZE && |
| 306 | bdata->last_offset && bdata->last_pos+1 == start) { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 307 | unsigned long offset, remaining_size; |
Nick Wilson | 8c0e33c | 2005-06-25 14:59:00 -0700 | [diff] [blame] | 308 | offset = ALIGN(bdata->last_offset, align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | BUG_ON(offset > PAGE_SIZE); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 310 | remaining_size = PAGE_SIZE - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (size < remaining_size) { |
| 312 | areasize = 0; |
| 313 | /* last_pos unchanged */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 314 | bdata->last_offset = offset + size; |
| 315 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 316 | offset + node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } else { |
| 318 | remaining_size = size - remaining_size; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 319 | areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE; |
| 320 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 321 | offset + node_boot_start); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 322 | bdata->last_pos = start + areasize - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | bdata->last_offset = remaining_size; |
| 324 | } |
| 325 | bdata->last_offset &= ~PAGE_MASK; |
| 326 | } else { |
| 327 | bdata->last_pos = start + areasize - 1; |
| 328 | bdata->last_offset = size & ~PAGE_MASK; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 329 | ret = phys_to_virt(start * PAGE_SIZE + node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Reserve the area now: |
| 334 | */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 335 | for (i = start; i < start + areasize; i++) |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame^] | 336 | if (unlikely(test_and_set_bit(i, node_bootmem_map))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | BUG(); |
| 338 | memset(ret, 0, size); |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat) |
| 343 | { |
| 344 | struct page *page; |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 345 | unsigned long pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | bootmem_data_t *bdata = pgdat->bdata; |
| 347 | unsigned long i, count, total = 0; |
| 348 | unsigned long idx; |
| 349 | unsigned long *map; |
| 350 | int gofast = 0; |
| 351 | |
| 352 | BUG_ON(!bdata->node_bootmem_map); |
| 353 | |
| 354 | count = 0; |
| 355 | /* first extant page of the node */ |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 356 | pfn = PFN_DOWN(bdata->node_boot_start); |
| 357 | idx = bdata->node_low_pfn - pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | map = bdata->node_bootmem_map; |
| 359 | /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */ |
| 360 | if (bdata->node_boot_start == 0 || |
| 361 | ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG)) |
| 362 | gofast = 1; |
| 363 | for (i = 0; i < idx; ) { |
| 364 | unsigned long v = ~map[i / BITS_PER_LONG]; |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (gofast && v == ~0UL) { |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 367 | int order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 369 | page = pfn_to_page(pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | count += BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | order = ffs(BITS_PER_LONG) - 1; |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 372 | __free_pages_bootmem(page, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | i += BITS_PER_LONG; |
| 374 | page += BITS_PER_LONG; |
| 375 | } else if (v) { |
| 376 | unsigned long m; |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 377 | |
| 378 | page = pfn_to_page(pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | for (m = 1; m && i < idx; m<<=1, page++, i++) { |
| 380 | if (v & m) { |
| 381 | count++; |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 382 | __free_pages_bootmem(page, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } else { |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 386 | i += BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 388 | pfn += BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | total += count; |
| 391 | |
| 392 | /* |
| 393 | * Now free the allocator bitmap itself, it's not |
| 394 | * needed anymore: |
| 395 | */ |
| 396 | page = virt_to_page(bdata->node_bootmem_map); |
| 397 | count = 0; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 398 | idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT; |
| 399 | for (i = 0; i < idx; i++, page++) { |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 400 | __free_pages_bootmem(page, 0); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 401 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | total += count; |
| 404 | bdata->node_bootmem_map = NULL; |
| 405 | |
| 406 | return total; |
| 407 | } |
| 408 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 409 | 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] | 410 | unsigned long startpfn, unsigned long endpfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 412 | return init_bootmem_core(pgdat, freepfn, startpfn, endpfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 415 | void __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 416 | unsigned long size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 418 | reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 421 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 422 | unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
| 424 | free_bootmem_core(pgdat->bdata, physaddr, size); |
| 425 | } |
| 426 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 427 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 429 | return free_all_bootmem_core(pgdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 432 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
| 434 | max_low_pfn = pages; |
| 435 | min_low_pfn = start; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 436 | return init_bootmem_core(NODE_DATA(0), start, 0, pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 440 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
| 441 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 443 | return reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */ |
| 446 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 447 | void __init free_bootmem(unsigned long addr, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
Yinghai Lu | 5a982cb | 2008-03-24 12:29:45 -0700 | [diff] [blame] | 449 | bootmem_data_t *bdata; |
| 450 | list_for_each_entry(bdata, &bdata_list, list) |
| 451 | free_bootmem_core(bdata, addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 454 | unsigned long __init free_all_bootmem(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 456 | return free_all_bootmem_core(NODE_DATA(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 459 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
| 460 | unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 462 | bootmem_data_t *bdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | void *ptr; |
| 464 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 465 | list_for_each_entry(bdata, &bdata_list, list) { |
| 466 | ptr = __alloc_bootmem_core(bdata, size, align, goal, 0); |
| 467 | if (ptr) |
| 468 | return ptr; |
| 469 | } |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 470 | return NULL; |
| 471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 473 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
| 474 | unsigned long goal) |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 475 | { |
| 476 | void *mem = __alloc_bootmem_nopanic(size,align,goal); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 477 | |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 478 | if (mem) |
| 479 | return mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | /* |
| 481 | * Whoops, we cannot satisfy the allocation request. |
| 482 | */ |
| 483 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); |
| 484 | panic("Out of memory"); |
| 485 | return NULL; |
| 486 | } |
| 487 | |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 488 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 489 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
| 490 | unsigned long align, unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
| 492 | void *ptr; |
| 493 | |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 494 | ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (ptr) |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 496 | return ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 498 | return __alloc_bootmem(size, align, goal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 501 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
| 502 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL |
| 503 | #endif |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 504 | |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 505 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
| 506 | unsigned long goal) |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 507 | { |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 508 | bootmem_data_t *bdata; |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [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) { |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 512 | ptr = __alloc_bootmem_core(bdata, size, align, goal, |
| 513 | ARCH_LOW_ADDRESS_LIMIT); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 514 | if (ptr) |
| 515 | return ptr; |
| 516 | } |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * Whoops, we cannot satisfy the allocation request. |
| 520 | */ |
| 521 | printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size); |
| 522 | panic("Out of low memory"); |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
| 527 | unsigned long align, unsigned long goal) |
| 528 | { |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 529 | return __alloc_bootmem_core(pgdat->bdata, size, align, goal, |
| 530 | ARCH_LOW_ADDRESS_LIMIT); |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 531 | } |