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