Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 2 | * bootmem - A boot-time physical memory allocator and configurator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1999 Ingo Molnar |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 5 | * 1999 Kanoj Sarcar, SGI |
| 6 | * 2008 Johannes Weiner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 8 | * Access to this subsystem has to be serialized externally (which is true |
| 9 | * for the boot process anyway). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | unsigned long max_low_pfn; |
| 23 | unsigned long min_low_pfn; |
| 24 | unsigned long max_pfn; |
| 25 | |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 26 | static LIST_HEAD(bdata_list); |
Vivek Goyal | 92aa63a | 2005-06-25 14:58:18 -0700 | [diff] [blame] | 27 | #ifdef CONFIG_CRASH_DUMP |
| 28 | /* |
| 29 | * If we have booted due to a crash, max_pfn will be a very low value. We need |
| 30 | * to know the amount of memory that the previous kernel used. |
| 31 | */ |
| 32 | unsigned long saved_max_pfn; |
| 33 | #endif |
| 34 | |
Johannes Weiner | b61bfa3 | 2008-07-23 21:26:55 -0700 | [diff] [blame] | 35 | bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata; |
| 36 | |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 37 | /* |
| 38 | * Given an initialised bdata, it returns the size of the boot bitmap |
| 39 | */ |
| 40 | static unsigned long __init get_mapsize(bootmem_data_t *bdata) |
| 41 | { |
| 42 | unsigned long mapsize; |
| 43 | unsigned long start = PFN_DOWN(bdata->node_boot_start); |
| 44 | unsigned long end = bdata->node_low_pfn; |
| 45 | |
| 46 | mapsize = ((end - start) + 7) / 8; |
| 47 | return ALIGN(mapsize, sizeof(long)); |
| 48 | } |
| 49 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 50 | /** |
| 51 | * bootmem_bootmap_pages - calculate bitmap size in pages |
| 52 | * @pages: number of pages the bitmap has to represent |
| 53 | */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 54 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
| 56 | unsigned long mapsize; |
| 57 | |
| 58 | mapsize = (pages+7)/8; |
| 59 | mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK; |
| 60 | mapsize >>= PAGE_SHIFT; |
| 61 | |
| 62 | return mapsize; |
| 63 | } |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 64 | |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 65 | /* |
| 66 | * link bdata in order |
| 67 | */ |
Franck Bui-Huu | 69d49e6 | 2006-09-25 23:31:04 -0700 | [diff] [blame] | 68 | static void __init link_bootmem(bootmem_data_t *bdata) |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 69 | { |
| 70 | bootmem_data_t *ent; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 71 | |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 72 | if (list_empty(&bdata_list)) { |
| 73 | list_add(&bdata->list, &bdata_list); |
| 74 | return; |
| 75 | } |
| 76 | /* insert in order */ |
| 77 | list_for_each_entry(ent, &bdata_list, list) { |
| 78 | if (bdata->node_boot_start < ent->node_boot_start) { |
| 79 | list_add_tail(&bdata->list, &ent->list); |
| 80 | return; |
| 81 | } |
| 82 | } |
| 83 | list_add_tail(&bdata->list, &bdata_list); |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 86 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * Called once to set up the allocator itself. |
| 88 | */ |
Johannes Weiner | 8ae0446 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 89 | static unsigned long __init init_bootmem_core(bootmem_data_t *bdata, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | unsigned long mapstart, unsigned long start, unsigned long end) |
| 91 | { |
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 | |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 94 | mminit_validate_memmodel_limits(&start, &end); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 95 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
| 96 | bdata->node_boot_start = PFN_PHYS(start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | bdata->node_low_pfn = end; |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 98 | link_bootmem(bdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Initially all pages are reserved - setup_arch() has to |
| 102 | * register free RAM areas explicitly. |
| 103 | */ |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 104 | mapsize = get_mapsize(bdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
| 106 | |
| 107 | return mapsize; |
| 108 | } |
| 109 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 110 | /** |
| 111 | * init_bootmem_node - register a node as boot memory |
| 112 | * @pgdat: node to register |
| 113 | * @freepfn: pfn where the bitmap for this node is to be placed |
| 114 | * @startpfn: first pfn on the node |
| 115 | * @endpfn: first pfn after the node |
| 116 | * |
| 117 | * Returns the number of bytes needed to hold the bitmap for this node. |
| 118 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 119 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
| 120 | unsigned long startpfn, unsigned long endpfn) |
| 121 | { |
| 122 | return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn); |
| 123 | } |
| 124 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 125 | /** |
| 126 | * init_bootmem - register boot memory |
| 127 | * @start: pfn where the bitmap is to be placed |
| 128 | * @pages: number of available physical pages |
| 129 | * |
| 130 | * Returns the number of bytes needed to hold the bitmap. |
| 131 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 132 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
| 133 | { |
| 134 | max_low_pfn = pages; |
| 135 | min_low_pfn = start; |
| 136 | return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages); |
| 137 | } |
| 138 | |
| 139 | static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata) |
| 140 | { |
| 141 | struct page *page; |
| 142 | unsigned long pfn; |
| 143 | unsigned long i, count; |
| 144 | unsigned long idx; |
| 145 | unsigned long *map; |
| 146 | int gofast = 0; |
| 147 | |
| 148 | BUG_ON(!bdata->node_bootmem_map); |
| 149 | |
| 150 | count = 0; |
| 151 | /* first extant page of the node */ |
| 152 | pfn = PFN_DOWN(bdata->node_boot_start); |
| 153 | idx = bdata->node_low_pfn - pfn; |
| 154 | map = bdata->node_bootmem_map; |
| 155 | /* |
| 156 | * Check if we are aligned to BITS_PER_LONG pages. If so, we might |
| 157 | * be able to free page orders of that size at once. |
| 158 | */ |
| 159 | if (!(pfn & (BITS_PER_LONG-1))) |
| 160 | gofast = 1; |
| 161 | |
| 162 | for (i = 0; i < idx; ) { |
| 163 | unsigned long v = ~map[i / BITS_PER_LONG]; |
| 164 | |
| 165 | if (gofast && v == ~0UL) { |
| 166 | int order; |
| 167 | |
| 168 | page = pfn_to_page(pfn); |
| 169 | count += BITS_PER_LONG; |
| 170 | order = ffs(BITS_PER_LONG) - 1; |
| 171 | __free_pages_bootmem(page, order); |
| 172 | i += BITS_PER_LONG; |
| 173 | page += BITS_PER_LONG; |
| 174 | } else if (v) { |
| 175 | unsigned long m; |
| 176 | |
| 177 | page = pfn_to_page(pfn); |
| 178 | for (m = 1; m && i < idx; m<<=1, page++, i++) { |
| 179 | if (v & m) { |
| 180 | count++; |
| 181 | __free_pages_bootmem(page, 0); |
| 182 | } |
| 183 | } |
| 184 | } else { |
| 185 | i += BITS_PER_LONG; |
| 186 | } |
| 187 | pfn += BITS_PER_LONG; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Now free the allocator bitmap itself, it's not |
| 192 | * needed anymore: |
| 193 | */ |
| 194 | page = virt_to_page(bdata->node_bootmem_map); |
| 195 | idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT; |
| 196 | for (i = 0; i < idx; i++, page++) |
| 197 | __free_pages_bootmem(page, 0); |
| 198 | count += i; |
| 199 | bdata->node_bootmem_map = NULL; |
| 200 | |
| 201 | return count; |
| 202 | } |
| 203 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 204 | /** |
| 205 | * free_all_bootmem_node - release a node's free pages to the buddy allocator |
| 206 | * @pgdat: node to be released |
| 207 | * |
| 208 | * Returns the number of pages actually released. |
| 209 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 210 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
| 211 | { |
| 212 | register_page_bootmem_info_node(pgdat); |
| 213 | return free_all_bootmem_core(pgdat->bdata); |
| 214 | } |
| 215 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 216 | /** |
| 217 | * free_all_bootmem - release free pages to the buddy allocator |
| 218 | * |
| 219 | * Returns the number of pages actually released. |
| 220 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 221 | unsigned long __init free_all_bootmem(void) |
| 222 | { |
| 223 | return free_all_bootmem_core(NODE_DATA(0)->bdata); |
| 224 | } |
| 225 | |
| 226 | static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, |
| 227 | unsigned long size) |
| 228 | { |
| 229 | unsigned long sidx, eidx; |
| 230 | unsigned long i; |
| 231 | |
| 232 | BUG_ON(!size); |
| 233 | |
| 234 | /* out range */ |
| 235 | if (addr + size < bdata->node_boot_start || |
| 236 | PFN_DOWN(addr) > bdata->node_low_pfn) |
| 237 | return; |
| 238 | /* |
| 239 | * round down end of usable mem, partially free pages are |
| 240 | * considered reserved. |
| 241 | */ |
| 242 | |
| 243 | if (addr >= bdata->node_boot_start && addr < bdata->last_success) |
| 244 | bdata->last_success = addr; |
| 245 | |
| 246 | /* |
| 247 | * Round up to index to the range. |
| 248 | */ |
| 249 | if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start)) |
| 250 | sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start); |
| 251 | else |
| 252 | sidx = 0; |
| 253 | |
| 254 | eidx = PFN_DOWN(addr + size - bdata->node_boot_start); |
| 255 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
| 256 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); |
| 257 | |
| 258 | for (i = sidx; i < eidx; i++) { |
| 259 | if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map))) |
| 260 | BUG(); |
| 261 | } |
| 262 | } |
| 263 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 264 | /** |
| 265 | * free_bootmem_node - mark a page range as usable |
| 266 | * @pgdat: node the range resides on |
| 267 | * @physaddr: starting address of the range |
| 268 | * @size: size of the range in bytes |
| 269 | * |
| 270 | * Partial pages will be considered reserved and left as they are. |
| 271 | * |
| 272 | * Only physical pages that actually reside on @pgdat are marked. |
| 273 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 274 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 275 | unsigned long size) |
| 276 | { |
| 277 | free_bootmem_core(pgdat->bdata, physaddr, size); |
| 278 | } |
| 279 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 280 | /** |
| 281 | * free_bootmem - mark a page range as usable |
| 282 | * @addr: starting address of the range |
| 283 | * @size: size of the range in bytes |
| 284 | * |
| 285 | * Partial pages will be considered reserved and left as they are. |
| 286 | * |
| 287 | * All physical pages within the range are marked, no matter what |
| 288 | * node they reside on. |
| 289 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 290 | void __init free_bootmem(unsigned long addr, unsigned long size) |
| 291 | { |
| 292 | bootmem_data_t *bdata; |
| 293 | list_for_each_entry(bdata, &bdata_list, list) |
| 294 | free_bootmem_core(bdata, addr, size); |
| 295 | } |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | /* |
| 298 | * Marks a particular physical memory range as unallocatable. Usable RAM |
| 299 | * might be used for boot-time allocations - or it might get added |
| 300 | * to the free page pool later on. |
| 301 | */ |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 302 | static int __init can_reserve_bootmem_core(bootmem_data_t *bdata, |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 303 | unsigned long addr, unsigned long size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 305 | unsigned long sidx, eidx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | unsigned long i; |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 307 | |
| 308 | BUG_ON(!size); |
| 309 | |
| 310 | /* out of range, don't hold other */ |
| 311 | if (addr + size < bdata->node_boot_start || |
| 312 | PFN_DOWN(addr) > bdata->node_low_pfn) |
| 313 | return 0; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | /* |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 316 | * Round up to index to the range. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | */ |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 318 | if (addr > bdata->node_boot_start) |
| 319 | sidx= PFN_DOWN(addr - bdata->node_boot_start); |
| 320 | else |
| 321 | sidx = 0; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 322 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 323 | eidx = PFN_UP(addr + size - bdata->node_boot_start); |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 324 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
| 325 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 327 | for (i = sidx; i < eidx; i++) { |
| 328 | if (test_bit(i, bdata->node_bootmem_map)) { |
| 329 | if (flags & BOOTMEM_EXCLUSIVE) |
| 330 | return -EBUSY; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | |
| 336 | } |
| 337 | |
| 338 | static void __init reserve_bootmem_core(bootmem_data_t *bdata, |
| 339 | unsigned long addr, unsigned long size, int flags) |
| 340 | { |
| 341 | unsigned long sidx, eidx; |
| 342 | unsigned long i; |
| 343 | |
| 344 | BUG_ON(!size); |
| 345 | |
| 346 | /* out of range */ |
| 347 | if (addr + size < bdata->node_boot_start || |
| 348 | PFN_DOWN(addr) > bdata->node_low_pfn) |
| 349 | return; |
| 350 | |
| 351 | /* |
| 352 | * Round up to index to the range. |
| 353 | */ |
| 354 | if (addr > bdata->node_boot_start) |
| 355 | sidx= PFN_DOWN(addr - bdata->node_boot_start); |
| 356 | else |
| 357 | sidx = 0; |
| 358 | |
| 359 | eidx = PFN_UP(addr + size - bdata->node_boot_start); |
| 360 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
| 361 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); |
| 362 | |
| 363 | for (i = sidx; i < eidx; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (test_and_set_bit(i, bdata->node_bootmem_map)) { |
| 365 | #ifdef CONFIG_DEBUG_BOOTMEM |
| 366 | printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE); |
| 367 | #endif |
| 368 | } |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 372 | /** |
| 373 | * reserve_bootmem_node - mark a page range as reserved |
| 374 | * @pgdat: node the range resides on |
| 375 | * @physaddr: starting address of the range |
| 376 | * @size: size of the range in bytes |
| 377 | * @flags: reservation flags (see linux/bootmem.h) |
| 378 | * |
| 379 | * Partial pages will be reserved. |
| 380 | * |
| 381 | * Only physical pages that actually reside on @pgdat are marked. |
| 382 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 383 | int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 384 | unsigned long size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 386 | int ret; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 387 | |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 388 | ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); |
| 389 | if (ret < 0) |
| 390 | return -ENOMEM; |
| 391 | reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); |
| 392 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 395 | #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 396 | /** |
| 397 | * reserve_bootmem - mark a page range as usable |
| 398 | * @addr: starting address of the range |
| 399 | * @size: size of the range in bytes |
| 400 | * @flags: reservation flags (see linux/bootmem.h) |
| 401 | * |
| 402 | * Partial pages will be reserved. |
| 403 | * |
| 404 | * All physical pages within the range are marked, no matter what |
| 405 | * node they reside on. |
| 406 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 407 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
| 408 | int flags) |
| 409 | { |
| 410 | bootmem_data_t *bdata; |
| 411 | int ret; |
| 412 | |
| 413 | list_for_each_entry(bdata, &bdata_list, list) { |
| 414 | ret = can_reserve_bootmem_core(bdata, addr, size, flags); |
| 415 | if (ret < 0) |
| 416 | return ret; |
| 417 | } |
| 418 | list_for_each_entry(bdata, &bdata_list, list) |
| 419 | reserve_bootmem_core(bdata, addr, size, flags); |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */ |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | /* |
| 426 | * We 'merge' subsequent allocations to save space. We might 'lose' |
| 427 | * some fraction of a page if allocations cannot be satisfied due to |
| 428 | * size constraints on boxes where there is physical RAM space |
| 429 | * fragmentation - in these cases (mostly large memory boxes) this |
| 430 | * is not a problem. |
| 431 | * |
| 432 | * On low memory boxes we get it right in 100% of the cases. |
| 433 | * |
| 434 | * alignment has to be a power of 2 value. |
| 435 | * |
| 436 | * NOTE: This function is _not_ reentrant. |
| 437 | */ |
Johannes Weiner | ffc6421 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 438 | static void * __init |
| 439 | alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, |
| 440 | unsigned long align, unsigned long goal, unsigned long limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 442 | unsigned long areasize, preferred; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 443 | unsigned long i, start = 0, incr, eidx, end_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | void *ret; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 445 | unsigned long node_boot_start; |
| 446 | void *node_bootmem_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 448 | if (!size) { |
Johannes Weiner | ffc6421 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 449 | printk("alloc_bootmem_core(): zero-sized request\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | BUG(); |
| 451 | } |
| 452 | BUG_ON(align & (align-1)); |
| 453 | |
Christian Krafft | 7c309a6 | 2006-12-06 20:32:41 -0800 | [diff] [blame] | 454 | /* on nodes without memory - bootmem_map is NULL */ |
| 455 | if (!bdata->node_bootmem_map) |
| 456 | return NULL; |
| 457 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 458 | /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */ |
| 459 | node_boot_start = bdata->node_boot_start; |
| 460 | node_bootmem_map = bdata->node_bootmem_map; |
| 461 | if (align) { |
| 462 | node_boot_start = ALIGN(bdata->node_boot_start, align); |
| 463 | if (node_boot_start > bdata->node_boot_start) |
| 464 | node_bootmem_map = (unsigned long *)bdata->node_bootmem_map + |
| 465 | PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG; |
| 466 | } |
| 467 | |
| 468 | if (limit && node_boot_start >= limit) |
| 469 | return NULL; |
| 470 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 471 | end_pfn = bdata->node_low_pfn; |
| 472 | limit = PFN_DOWN(limit); |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 473 | if (limit && end_pfn > limit) |
| 474 | end_pfn = limit; |
| 475 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 476 | eidx = end_pfn - PFN_DOWN(node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * We try to allocate bootmem pages above 'goal' |
| 480 | * first, then we try to allocate lower pages. |
| 481 | */ |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 482 | preferred = 0; |
| 483 | if (goal && PFN_DOWN(goal) < end_pfn) { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 484 | if (goal > node_boot_start) |
| 485 | preferred = goal - node_boot_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 487 | if (bdata->last_success > node_boot_start && |
| 488 | bdata->last_success - node_boot_start >= preferred) |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 489 | if (!limit || (limit && limit > bdata->last_success)) |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 490 | preferred = bdata->last_success - node_boot_start; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 493 | preferred = PFN_DOWN(ALIGN(preferred, align)); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 494 | areasize = (size + PAGE_SIZE-1) / PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | incr = align >> PAGE_SHIFT ? : 1; |
| 496 | |
| 497 | restart_scan: |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 498 | for (i = preferred; i < eidx;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | unsigned long j; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 500 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 501 | i = find_next_zero_bit(node_bootmem_map, eidx, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | i = ALIGN(i, incr); |
Haren Myneni | 66d43e9 | 2005-12-12 00:37:39 -0800 | [diff] [blame] | 503 | if (i >= eidx) |
| 504 | break; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 505 | if (test_bit(i, node_bootmem_map)) { |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 506 | i += incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | continue; |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | for (j = i + 1; j < i + areasize; ++j) { |
| 510 | if (j >= eidx) |
| 511 | goto fail_block; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 512 | if (test_bit(j, node_bootmem_map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | goto fail_block; |
| 514 | } |
| 515 | start = i; |
| 516 | goto found; |
| 517 | fail_block: |
| 518 | i = ALIGN(j, incr); |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 519 | if (i == j) |
| 520 | i += incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 523 | if (preferred > 0) { |
| 524 | preferred = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | goto restart_scan; |
| 526 | } |
| 527 | return NULL; |
| 528 | |
| 529 | found: |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 530 | bdata->last_success = PFN_PHYS(start) + node_boot_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | BUG_ON(start >= eidx); |
| 532 | |
| 533 | /* |
| 534 | * Is the next page of the previous allocation-end the start |
| 535 | * of this allocation's buffer? If yes then we can 'merge' |
| 536 | * the previous partial page with this allocation. |
| 537 | */ |
| 538 | if (align < PAGE_SIZE && |
| 539 | bdata->last_offset && bdata->last_pos+1 == start) { |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 540 | unsigned long offset, remaining_size; |
Nick Wilson | 8c0e33c | 2005-06-25 14:59:00 -0700 | [diff] [blame] | 541 | offset = ALIGN(bdata->last_offset, align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | BUG_ON(offset > PAGE_SIZE); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 543 | remaining_size = PAGE_SIZE - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | if (size < remaining_size) { |
| 545 | areasize = 0; |
| 546 | /* last_pos unchanged */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 547 | bdata->last_offset = offset + size; |
| 548 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 549 | offset + node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } else { |
| 551 | remaining_size = size - remaining_size; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 552 | areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE; |
| 553 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 554 | offset + node_boot_start); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 555 | bdata->last_pos = start + areasize - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | bdata->last_offset = remaining_size; |
| 557 | } |
| 558 | bdata->last_offset &= ~PAGE_MASK; |
| 559 | } else { |
| 560 | bdata->last_pos = start + areasize - 1; |
| 561 | bdata->last_offset = size & ~PAGE_MASK; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 562 | ret = phys_to_virt(start * PAGE_SIZE + node_boot_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Reserve the area now: |
| 567 | */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 568 | for (i = start; i < start + areasize; i++) |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 569 | if (unlikely(test_and_set_bit(i, node_bootmem_map))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | BUG(); |
| 571 | memset(ret, 0, size); |
| 572 | return ret; |
| 573 | } |
| 574 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 575 | /** |
| 576 | * __alloc_bootmem_nopanic - allocate boot memory without panicking |
| 577 | * @size: size of the request in bytes |
| 578 | * @align: alignment of the region |
| 579 | * @goal: preferred starting address of the region |
| 580 | * |
| 581 | * The goal is dropped if it can not be satisfied and the allocation will |
| 582 | * fall back to memory below @goal. |
| 583 | * |
| 584 | * Allocation may happen on any node in the system. |
| 585 | * |
| 586 | * Returns NULL on failure. |
| 587 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 588 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
| 589 | unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 591 | bootmem_data_t *bdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | void *ptr; |
| 593 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 594 | list_for_each_entry(bdata, &bdata_list, list) { |
Johannes Weiner | ffc6421 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 595 | ptr = alloc_bootmem_core(bdata, size, align, goal, 0); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 596 | if (ptr) |
| 597 | return ptr; |
| 598 | } |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 599 | return NULL; |
| 600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 602 | /** |
| 603 | * __alloc_bootmem - allocate boot memory |
| 604 | * @size: size of the request in bytes |
| 605 | * @align: alignment of the region |
| 606 | * @goal: preferred starting address of the region |
| 607 | * |
| 608 | * The goal is dropped if it can not be satisfied and the allocation will |
| 609 | * fall back to memory below @goal. |
| 610 | * |
| 611 | * Allocation may happen on any node in the system. |
| 612 | * |
| 613 | * The function panics if the request can not be satisfied. |
| 614 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 615 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
| 616 | unsigned long goal) |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 617 | { |
| 618 | void *mem = __alloc_bootmem_nopanic(size,align,goal); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 619 | |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 620 | if (mem) |
| 621 | return mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | /* |
| 623 | * Whoops, we cannot satisfy the allocation request. |
| 624 | */ |
| 625 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); |
| 626 | panic("Out of memory"); |
| 627 | return NULL; |
| 628 | } |
| 629 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 630 | /** |
| 631 | * __alloc_bootmem_node - allocate boot memory from a specific node |
| 632 | * @pgdat: node to allocate from |
| 633 | * @size: size of the request in bytes |
| 634 | * @align: alignment of the region |
| 635 | * @goal: preferred starting address of the region |
| 636 | * |
| 637 | * The goal is dropped if it can not be satisfied and the allocation will |
| 638 | * fall back to memory below @goal. |
| 639 | * |
| 640 | * Allocation may fall back to any node in the system if the specified node |
| 641 | * can not hold the requested memory. |
| 642 | * |
| 643 | * The function panics if the request can not be satisfied. |
| 644 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 645 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
| 646 | unsigned long align, unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
| 648 | void *ptr; |
| 649 | |
Johannes Weiner | ffc6421 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 650 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | if (ptr) |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 652 | return ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 654 | return __alloc_bootmem(size, align, goal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Yasunori Goto | e70260a | 2008-04-28 02:13:32 -0700 | [diff] [blame] | 657 | #ifdef CONFIG_SPARSEMEM |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 658 | /** |
| 659 | * alloc_bootmem_section - allocate boot memory from a specific section |
| 660 | * @size: size of the request in bytes |
| 661 | * @section_nr: sparse map section to allocate from |
| 662 | * |
| 663 | * Return NULL on failure. |
| 664 | */ |
Yasunori Goto | e70260a | 2008-04-28 02:13:32 -0700 | [diff] [blame] | 665 | void * __init alloc_bootmem_section(unsigned long size, |
| 666 | unsigned long section_nr) |
| 667 | { |
| 668 | void *ptr; |
| 669 | unsigned long limit, goal, start_nr, end_nr, pfn; |
| 670 | struct pglist_data *pgdat; |
| 671 | |
| 672 | pfn = section_nr_to_pfn(section_nr); |
| 673 | goal = PFN_PHYS(pfn); |
| 674 | limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1; |
| 675 | pgdat = NODE_DATA(early_pfn_to_nid(pfn)); |
Johannes Weiner | ffc6421 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 676 | ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal, |
| 677 | limit); |
Yasunori Goto | e70260a | 2008-04-28 02:13:32 -0700 | [diff] [blame] | 678 | |
| 679 | if (!ptr) |
| 680 | return NULL; |
| 681 | |
| 682 | start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr))); |
| 683 | end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size)); |
| 684 | if (start_nr != section_nr || end_nr != section_nr) { |
| 685 | printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n", |
| 686 | section_nr); |
| 687 | free_bootmem_core(pgdat->bdata, __pa(ptr), size); |
| 688 | ptr = NULL; |
| 689 | } |
| 690 | |
| 691 | return ptr; |
| 692 | } |
| 693 | #endif |
| 694 | |
Andi Kleen | b54bbf7 | 2008-07-23 21:27:45 -0700 | [diff] [blame] | 695 | void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size, |
| 696 | unsigned long align, unsigned long goal) |
| 697 | { |
| 698 | void *ptr; |
| 699 | |
| 700 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
| 701 | if (ptr) |
| 702 | return ptr; |
| 703 | |
| 704 | return __alloc_bootmem_nopanic(size, align, goal); |
| 705 | } |
| 706 | |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 707 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
| 708 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL |
| 709 | #endif |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 710 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 711 | /** |
| 712 | * __alloc_bootmem_low - allocate low boot memory |
| 713 | * @size: size of the request in bytes |
| 714 | * @align: alignment of the region |
| 715 | * @goal: preferred starting address of the region |
| 716 | * |
| 717 | * The goal is dropped if it can not be satisfied and the allocation will |
| 718 | * fall back to memory below @goal. |
| 719 | * |
| 720 | * Allocation may happen on any node in the system. |
| 721 | * |
| 722 | * The function panics if the request can not be satisfied. |
| 723 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 724 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
| 725 | unsigned long goal) |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 726 | { |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 727 | bootmem_data_t *bdata; |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 728 | void *ptr; |
| 729 | |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 730 | list_for_each_entry(bdata, &bdata_list, list) { |
Johannes Weiner | ffc6421 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 731 | ptr = alloc_bootmem_core(bdata, size, align, goal, |
| 732 | ARCH_LOW_ADDRESS_LIMIT); |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 733 | if (ptr) |
| 734 | return ptr; |
| 735 | } |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Whoops, we cannot satisfy the allocation request. |
| 739 | */ |
| 740 | printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size); |
| 741 | panic("Out of low memory"); |
| 742 | return NULL; |
| 743 | } |
| 744 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame^] | 745 | /** |
| 746 | * __alloc_bootmem_low_node - allocate low boot memory from a specific node |
| 747 | * @pgdat: node to allocate from |
| 748 | * @size: size of the request in bytes |
| 749 | * @align: alignment of the region |
| 750 | * @goal: preferred starting address of the region |
| 751 | * |
| 752 | * The goal is dropped if it can not be satisfied and the allocation will |
| 753 | * fall back to memory below @goal. |
| 754 | * |
| 755 | * Allocation may fall back to any node in the system if the specified node |
| 756 | * can not hold the requested memory. |
| 757 | * |
| 758 | * The function panics if the request can not be satisfied. |
| 759 | */ |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 760 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
| 761 | unsigned long align, unsigned long goal) |
| 762 | { |
Johannes Weiner | ffc6421 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 763 | return alloc_bootmem_core(pgdat->bdata, size, align, goal, |
| 764 | ARCH_LOW_ADDRESS_LIMIT); |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 765 | } |