Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/highmem.h> |
Yoichi Yuasa | 52ab320 | 2010-02-20 21:23:22 +0900 | [diff] [blame] | 3 | #include <linux/sched.h> |
Ralf Baechle | 631330f | 2009-06-19 14:05:26 +0100 | [diff] [blame] | 4 | #include <linux/smp.h> |
Ralf Baechle | bb86bf2 | 2009-04-25 11:25:34 +0200 | [diff] [blame] | 5 | #include <asm/fixmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <asm/tlbflush.h> |
| 7 | |
Ralf Baechle | bb86bf2 | 2009-04-25 11:25:34 +0200 | [diff] [blame] | 8 | static pte_t *kmap_pte; |
| 9 | |
| 10 | unsigned long highstart_pfn, highend_pfn; |
| 11 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 12 | void *kmap(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | { |
| 14 | void *addr; |
| 15 | |
| 16 | might_sleep(); |
| 17 | if (!PageHighMem(page)) |
| 18 | return page_address(page); |
| 19 | addr = kmap_high(page); |
| 20 | flush_tlb_one((unsigned long)addr); |
| 21 | |
| 22 | return addr; |
| 23 | } |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 24 | EXPORT_SYMBOL(kmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 26 | void kunmap(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Ralf Baechle | b72b709 | 2009-03-30 14:49:44 +0200 | [diff] [blame] | 28 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | if (!PageHighMem(page)) |
| 30 | return; |
| 31 | kunmap_high(page); |
| 32 | } |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 33 | EXPORT_SYMBOL(kunmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because |
| 37 | * no global lock is needed and because the kmap code must perform a global TLB |
| 38 | * invalidation when the kmap pool wraps. |
| 39 | * |
| 40 | * However when holding an atomic kmap is is not legal to sleep, so atomic |
| 41 | * kmaps are appropriate for short, tight code paths only. |
| 42 | */ |
| 43 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 44 | void *__kmap_atomic(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | unsigned long vaddr; |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 47 | int idx, type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */ |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 50 | pagefault_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | if (!PageHighMem(page)) |
| 52 | return page_address(page); |
| 53 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 54 | type = kmap_atomic_idx_push(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | idx = type + KM_TYPE_NR*smp_processor_id(); |
| 56 | vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); |
| 57 | #ifdef CONFIG_DEBUG_HIGHMEM |
Ralf Baechle | b72b709 | 2009-03-30 14:49:44 +0200 | [diff] [blame] | 58 | BUG_ON(!pte_none(*(kmap_pte - idx))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
Ralf Baechle | bb86bf2 | 2009-04-25 11:25:34 +0200 | [diff] [blame] | 60 | set_pte(kmap_pte-idx, mk_pte(page, PAGE_KERNEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | local_flush_tlb_one((unsigned long)vaddr); |
| 62 | |
| 63 | return (void*) vaddr; |
| 64 | } |
Ralf Baechle | bb86bf2 | 2009-04-25 11:25:34 +0200 | [diff] [blame] | 65 | EXPORT_SYMBOL(__kmap_atomic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 67 | void __kunmap_atomic(void *kvaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK; |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 70 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | if (vaddr < FIXADDR_START) { // FIXME |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 73 | pagefault_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
Peter Zijlstra | 2027394 | 2010-10-27 15:32:58 -0700 | [diff] [blame] | 77 | type = kmap_atomic_idx(); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 78 | #ifdef CONFIG_DEBUG_HIGHMEM |
| 79 | { |
| 80 | int idx = type + KM_TYPE_NR * smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 82 | BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx)); |
| 83 | |
| 84 | /* |
| 85 | * force other mappings to Oops if they'll try to access |
| 86 | * this pte without first remap it |
| 87 | */ |
| 88 | pte_clear(&init_mm, vaddr, kmap_pte-idx); |
| 89 | local_flush_tlb_one(vaddr); |
| 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | #endif |
Peter Zijlstra | 2027394 | 2010-10-27 15:32:58 -0700 | [diff] [blame] | 92 | kmap_atomic_idx_pop(); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 93 | pagefault_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 95 | EXPORT_SYMBOL(__kunmap_atomic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Ralf Baechle | 6008026 | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 97 | /* |
| 98 | * This is the same as kmap_atomic() but can map memory that doesn't |
| 99 | * have a struct page associated with it. |
| 100 | */ |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 101 | void *kmap_atomic_pfn(unsigned long pfn) |
Ralf Baechle | 6008026 | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 102 | { |
Ralf Baechle | 6008026 | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 103 | unsigned long vaddr; |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 104 | int idx, type; |
Ralf Baechle | 6008026 | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 105 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 106 | pagefault_disable(); |
Ralf Baechle | 6008026 | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 107 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 108 | type = kmap_atomic_idx_push(); |
Ralf Baechle | 6008026 | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 109 | idx = type + KM_TYPE_NR*smp_processor_id(); |
| 110 | vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); |
Ralf Baechle | bb86bf2 | 2009-04-25 11:25:34 +0200 | [diff] [blame] | 111 | set_pte(kmap_pte-idx, pfn_pte(pfn, PAGE_KERNEL)); |
Ralf Baechle | 6008026 | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 112 | flush_tlb_one(vaddr); |
| 113 | |
| 114 | return (void*) vaddr; |
| 115 | } |
| 116 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 117 | struct page *kmap_atomic_to_page(void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | unsigned long idx, vaddr = (unsigned long)ptr; |
| 120 | pte_t *pte; |
| 121 | |
| 122 | if (vaddr < FIXADDR_START) |
| 123 | return virt_to_page(ptr); |
| 124 | |
| 125 | idx = virt_to_fix(vaddr); |
| 126 | pte = kmap_pte - (idx - FIX_KMAP_BEGIN); |
| 127 | return pte_page(*pte); |
| 128 | } |
| 129 | |
Ralf Baechle | bb86bf2 | 2009-04-25 11:25:34 +0200 | [diff] [blame] | 130 | void __init kmap_init(void) |
| 131 | { |
| 132 | unsigned long kmap_vstart; |
| 133 | |
| 134 | /* cache the first kmap pte */ |
| 135 | kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN); |
| 136 | kmap_pte = kmap_get_fixmap_pte(kmap_vstart); |
| 137 | } |