Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/highmem.h> |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 2 | #include <linux/module.h> |
Pekka Enberg | 867c5b52 | 2009-03-03 14:10:12 +0200 | [diff] [blame] | 3 | #include <linux/swap.h> /* for totalram_pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | void *kmap(struct page *page) |
| 6 | { |
| 7 | might_sleep(); |
| 8 | if (!PageHighMem(page)) |
| 9 | return page_address(page); |
| 10 | return kmap_high(page); |
| 11 | } |
| 12 | |
| 13 | void kunmap(struct page *page) |
| 14 | { |
| 15 | if (in_interrupt()) |
| 16 | BUG(); |
| 17 | if (!PageHighMem(page)) |
| 18 | return; |
| 19 | kunmap_high(page); |
| 20 | } |
| 21 | |
| 22 | /* |
| 23 | * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because |
| 24 | * no global lock is needed and because the kmap code must perform a global TLB |
| 25 | * invalidation when the kmap pool wraps. |
| 26 | * |
| 27 | * However when holding an atomic kmap is is not legal to sleep, so atomic |
| 28 | * kmaps are appropriate for short, tight code paths only. |
| 29 | */ |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 30 | void *kmap_atomic_prot(struct page *page, enum km_type type, pgprot_t prot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
| 32 | enum fixed_addresses idx; |
| 33 | unsigned long vaddr; |
Andrew Morton | 9c31205 | 2008-03-28 11:47:34 -0700 | [diff] [blame] | 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */ |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 36 | pagefault_disable(); |
Ingo Molnar | 656dad3 | 2007-02-10 01:46:36 -0800 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | if (!PageHighMem(page)) |
| 39 | return page_address(page); |
| 40 | |
Akinobu Mita | f4112de | 2009-03-31 15:23:25 -0700 | [diff] [blame] | 41 | debug_kmap_atomic(type); |
Andrew Morton | 9c31205 | 2008-03-28 11:47:34 -0700 | [diff] [blame] | 42 | |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 43 | idx = type + KM_TYPE_NR*smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 45 | BUG_ON(!pte_none(*(kmap_pte-idx))); |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 46 | set_pte(kmap_pte-idx, mk_pte(page, prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 48 | return (void *)vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 51 | void *kmap_atomic(struct page *page, enum km_type type) |
| 52 | { |
| 53 | return kmap_atomic_prot(page, type, kmap_prot); |
| 54 | } |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | void kunmap_atomic(void *kvaddr, enum km_type type) |
| 57 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK; |
| 59 | enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id(); |
| 60 | |
Zachary Amsden | 23002d8 | 2006-09-30 23:29:35 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Force other mappings to Oops if they'll try to access this pte |
| 63 | * without first remap it. Keeping stale mappings around is a bad idea |
| 64 | * also, in case the page changes cacheability attributes or becomes |
| 65 | * a protected page in a hypervisor. |
| 66 | */ |
Jeremy Fitzhardinge | 3b17979 | 2006-12-06 20:32:22 -0800 | [diff] [blame] | 67 | if (vaddr == __fix_to_virt(FIX_KMAP_BEGIN+idx)) |
| 68 | kpte_clear_flush(kmap_pte-idx, vaddr); |
| 69 | else { |
| 70 | #ifdef CONFIG_DEBUG_HIGHMEM |
| 71 | BUG_ON(vaddr < PAGE_OFFSET); |
| 72 | BUG_ON(vaddr >= (unsigned long)high_memory); |
| 73 | #endif |
| 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 76 | pagefault_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Ingo Molnar | dd63fdc | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 79 | /* |
| 80 | * This is the same as kmap_atomic() but can map memory that doesn't |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 81 | * have a struct page associated with it. |
| 82 | */ |
| 83 | void *kmap_atomic_pfn(unsigned long pfn, enum km_type type) |
| 84 | { |
Akinobu Mita | bb6d59c | 2009-03-11 23:33:18 +0900 | [diff] [blame] | 85 | return kmap_atomic_prot_pfn(pfn, type, kmap_prot); |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 86 | } |
Eric Anholt | d1d8c92 | 2008-08-21 12:53:33 -0700 | [diff] [blame] | 87 | EXPORT_SYMBOL_GPL(kmap_atomic_pfn); /* temporarily in use by i915 GEM until vmap */ |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | struct page *kmap_atomic_to_page(void *ptr) |
| 90 | { |
| 91 | unsigned long idx, vaddr = (unsigned long)ptr; |
| 92 | pte_t *pte; |
| 93 | |
| 94 | if (vaddr < FIXADDR_START) |
| 95 | return virt_to_page(ptr); |
| 96 | |
| 97 | idx = virt_to_fix(vaddr); |
| 98 | pte = kmap_pte - (idx - FIX_KMAP_BEGIN); |
| 99 | return pte_page(*pte); |
| 100 | } |
| 101 | |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 102 | EXPORT_SYMBOL(kmap); |
| 103 | EXPORT_SYMBOL(kunmap); |
| 104 | EXPORT_SYMBOL(kmap_atomic); |
| 105 | EXPORT_SYMBOL(kunmap_atomic); |
Pekka Enberg | 867c5b52 | 2009-03-03 14:10:12 +0200 | [diff] [blame] | 106 | |
Pekka Enberg | 867c5b52 | 2009-03-03 14:10:12 +0200 | [diff] [blame] | 107 | void __init set_highmem_pages_init(void) |
| 108 | { |
| 109 | struct zone *zone; |
| 110 | int nid; |
| 111 | |
| 112 | for_each_zone(zone) { |
| 113 | unsigned long zone_start_pfn, zone_end_pfn; |
| 114 | |
| 115 | if (!is_highmem(zone)) |
| 116 | continue; |
| 117 | |
| 118 | zone_start_pfn = zone->zone_start_pfn; |
| 119 | zone_end_pfn = zone_start_pfn + zone->spanned_pages; |
| 120 | |
| 121 | nid = zone_to_nid(zone); |
| 122 | printk(KERN_INFO "Initializing %s for node %d (%08lx:%08lx)\n", |
| 123 | zone->name, nid, zone_start_pfn, zone_end_pfn); |
| 124 | |
| 125 | add_highpages_with_active_regions(nid, zone_start_pfn, |
| 126 | zone_end_pfn); |
| 127 | } |
| 128 | totalram_pages += totalhigh_pages; |
| 129 | } |