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