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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
| 4 | void *kmap(struct page *page) |
| 5 | { |
| 6 | might_sleep(); |
| 7 | if (!PageHighMem(page)) |
| 8 | return page_address(page); |
| 9 | return kmap_high(page); |
| 10 | } |
| 11 | |
| 12 | void kunmap(struct page *page) |
| 13 | { |
| 14 | if (in_interrupt()) |
| 15 | BUG(); |
| 16 | if (!PageHighMem(page)) |
| 17 | return; |
| 18 | kunmap_high(page); |
| 19 | } |
| 20 | |
Andrew Morton | 022eb43 | 2008-01-30 13:30:47 +0100 | [diff] [blame] | 21 | static void debug_kmap_atomic_prot(enum km_type type) |
| 22 | { |
| 23 | #ifdef CONFIG_DEBUG_HIGHMEM |
| 24 | static unsigned warn_count = 10; |
| 25 | |
| 26 | if (unlikely(warn_count == 0)) |
| 27 | return; |
| 28 | |
| 29 | if (unlikely(in_interrupt())) { |
| 30 | if (in_irq()) { |
| 31 | if (type != KM_IRQ0 && type != KM_IRQ1 && |
| 32 | type != KM_BIO_SRC_IRQ && type != KM_BIO_DST_IRQ && |
| 33 | type != KM_BOUNCE_READ) { |
| 34 | WARN_ON(1); |
| 35 | warn_count--; |
| 36 | } |
| 37 | } else if (!irqs_disabled()) { /* softirq */ |
| 38 | if (type != KM_IRQ0 && type != KM_IRQ1 && |
| 39 | type != KM_SOFTIRQ0 && type != KM_SOFTIRQ1 && |
| 40 | type != KM_SKB_SUNRPC_DATA && |
| 41 | type != KM_SKB_DATA_SOFTIRQ && |
| 42 | type != KM_BOUNCE_READ) { |
| 43 | WARN_ON(1); |
| 44 | warn_count--; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (type == KM_IRQ0 || type == KM_IRQ1 || type == KM_BOUNCE_READ || |
| 50 | type == KM_BIO_SRC_IRQ || type == KM_BIO_DST_IRQ) { |
| 51 | if (!irqs_disabled()) { |
| 52 | WARN_ON(1); |
| 53 | warn_count--; |
| 54 | } |
| 55 | } else if (type == KM_SOFTIRQ0 || type == KM_SOFTIRQ1) { |
| 56 | if (irq_count() == 0 && !irqs_disabled()) { |
| 57 | WARN_ON(1); |
| 58 | warn_count--; |
| 59 | } |
| 60 | } |
| 61 | #endif |
| 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /* |
| 65 | * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because |
| 66 | * no global lock is needed and because the kmap code must perform a global TLB |
| 67 | * invalidation when the kmap pool wraps. |
| 68 | * |
| 69 | * However when holding an atomic kmap is is not legal to sleep, so atomic |
| 70 | * kmaps are appropriate for short, tight code paths only. |
| 71 | */ |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 72 | 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] | 73 | { |
| 74 | enum fixed_addresses idx; |
| 75 | unsigned long vaddr; |
Andrew Morton | 9c31205 | 2008-03-28 11:47:34 -0700 | [diff] [blame^] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */ |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 78 | pagefault_disable(); |
Ingo Molnar | 656dad3 | 2007-02-10 01:46:36 -0800 | [diff] [blame] | 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (!PageHighMem(page)) |
| 81 | return page_address(page); |
| 82 | |
Andrew Morton | 9c31205 | 2008-03-28 11:47:34 -0700 | [diff] [blame^] | 83 | debug_kmap_atomic_prot(type); |
| 84 | |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 85 | idx = type + KM_TYPE_NR*smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 87 | BUG_ON(!pte_none(*(kmap_pte-idx))); |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 88 | set_pte(kmap_pte-idx, mk_pte(page, prot)); |
Zachary Amsden | 49f1971 | 2007-04-08 16:04:01 -0700 | [diff] [blame] | 89 | arch_flush_lazy_mmu_mode(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 91 | return (void *)vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 94 | void *kmap_atomic(struct page *page, enum km_type type) |
| 95 | { |
| 96 | return kmap_atomic_prot(page, type, kmap_prot); |
| 97 | } |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | void kunmap_atomic(void *kvaddr, enum km_type type) |
| 100 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK; |
| 102 | enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id(); |
| 103 | |
Zachary Amsden | 23002d8 | 2006-09-30 23:29:35 -0700 | [diff] [blame] | 104 | /* |
| 105 | * Force other mappings to Oops if they'll try to access this pte |
| 106 | * without first remap it. Keeping stale mappings around is a bad idea |
| 107 | * also, in case the page changes cacheability attributes or becomes |
| 108 | * a protected page in a hypervisor. |
| 109 | */ |
Jeremy Fitzhardinge | 3b17979 | 2006-12-06 20:32:22 -0800 | [diff] [blame] | 110 | if (vaddr == __fix_to_virt(FIX_KMAP_BEGIN+idx)) |
| 111 | kpte_clear_flush(kmap_pte-idx, vaddr); |
| 112 | else { |
| 113 | #ifdef CONFIG_DEBUG_HIGHMEM |
| 114 | BUG_ON(vaddr < PAGE_OFFSET); |
| 115 | BUG_ON(vaddr >= (unsigned long)high_memory); |
| 116 | #endif |
| 117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Jeremy Fitzhardinge | 7b2f27f | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 119 | arch_flush_lazy_mmu_mode(); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 120 | pagefault_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 123 | /* This is the same as kmap_atomic() but can map memory that doesn't |
| 124 | * have a struct page associated with it. |
| 125 | */ |
| 126 | void *kmap_atomic_pfn(unsigned long pfn, enum km_type type) |
| 127 | { |
| 128 | enum fixed_addresses idx; |
| 129 | unsigned long vaddr; |
| 130 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 131 | pagefault_disable(); |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 132 | |
| 133 | idx = type + KM_TYPE_NR*smp_processor_id(); |
| 134 | vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); |
| 135 | set_pte(kmap_pte-idx, pfn_pte(pfn, kmap_prot)); |
Zachary Amsden | 49f1971 | 2007-04-08 16:04:01 -0700 | [diff] [blame] | 136 | arch_flush_lazy_mmu_mode(); |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 137 | |
| 138 | return (void*) vaddr; |
| 139 | } |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct page *kmap_atomic_to_page(void *ptr) |
| 142 | { |
| 143 | unsigned long idx, vaddr = (unsigned long)ptr; |
| 144 | pte_t *pte; |
| 145 | |
| 146 | if (vaddr < FIXADDR_START) |
| 147 | return virt_to_page(ptr); |
| 148 | |
| 149 | idx = virt_to_fix(vaddr); |
| 150 | pte = kmap_pte - (idx - FIX_KMAP_BEGIN); |
| 151 | return pte_page(*pte); |
| 152 | } |
| 153 | |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 154 | EXPORT_SYMBOL(kmap); |
| 155 | EXPORT_SYMBOL(kunmap); |
| 156 | EXPORT_SYMBOL(kmap_atomic); |
| 157 | EXPORT_SYMBOL(kunmap_atomic); |
| 158 | EXPORT_SYMBOL(kmap_atomic_to_page); |