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