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