blob: a454ec5ff07af7f33d8ec548051101c3895e9b82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * highmem.c: virtual kernel memory mappings for high memory
3 *
4 * Provides kernel-static versions of atomic kmap functions originally
5 * found as inlines in include/asm-sparc/highmem.h. These became
6 * needed as kmap_atomic() and kunmap_atomic() started getting
7 * called from within modules.
8 * -- Tomas Szepe <szepe@pinerecords.com>, September 2002
9 *
10 * But kmap_atomic() and kunmap_atomic() cannot be inlined in
11 * modules because they are loaded with btfixup-ped functions.
12 */
13
14/*
15 * The use of kmap_atomic/kunmap_atomic is discouraged - kmap/kunmap
16 * gives a more generic (and caching) interface. But kmap_atomic can
17 * be used in IRQ contexts, so in some (very limited) cases we need it.
18 *
19 * XXX This is an old text. Actually, it's good to use atomic kmaps,
20 * provided you remember that they are atomic and not try to sleep
21 * with a kmap taken, much like a spinlock. Non-atomic kmaps are
22 * shared by CPUs, and so precious, and establishing them requires IPI.
23 * Atomic kmaps are lightweight and we may have NCPUS more of them.
24 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/highmem.h>
Paul Gortmaker7b64db62011-07-18 15:57:46 -040026#include <linux/export.h>
Sam Ravnborg1b6d06d82012-07-26 11:02:19 +000027#include <linux/mm.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/cacheflush.h>
30#include <asm/tlbflush.h>
Sam Ravnborg1b6d06d82012-07-26 11:02:19 +000031#include <asm/pgalloc.h>
32#include <asm/vaddrs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Sam Ravnborg9a4d5b92012-07-26 11:02:27 +000034pgprot_t kmap_prot;
35
36static pte_t *kmap_pte;
37
38void __init kmap_init(void)
39{
40 unsigned long address;
41 pmd_t *dir;
42
43 address = __fix_to_virt(FIX_KMAP_BEGIN);
44 dir = pmd_offset(pgd_offset_k(address), address);
45
46 /* cache the first kmap pte */
47 kmap_pte = pte_offset_kernel(dir, address);
48 kmap_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE);
49}
50
Cong Wanga24401b2011-11-26 10:53:39 +080051void *kmap_atomic(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 unsigned long vaddr;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070054 long idx, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +020056 preempt_disable();
Peter Zijlstraa8663742006-12-06 20:32:20 -080057 pagefault_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (!PageHighMem(page))
59 return page_address(page);
60
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070061 type = kmap_atomic_idx_push();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 idx = type + KM_TYPE_NR*smp_processor_id();
63 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
64
65/* XXX Fix - Anton */
66#if 0
67 __flush_cache_one(vaddr);
68#else
69 flush_cache_all();
70#endif
71
72#ifdef CONFIG_DEBUG_HIGHMEM
73 BUG_ON(!pte_none(*(kmap_pte-idx)));
74#endif
75 set_pte(kmap_pte-idx, mk_pte(page, kmap_prot));
76/* XXX Fix - Anton */
77#if 0
78 __flush_tlb_one(vaddr);
79#else
80 flush_tlb_all();
81#endif
82
83 return (void*) vaddr;
84}
Cong Wanga24401b2011-11-26 10:53:39 +080085EXPORT_SYMBOL(kmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070087void __kunmap_atomic(void *kvaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070090 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (vaddr < FIXADDR_START) { // FIXME
Peter Zijlstraa8663742006-12-06 20:32:20 -080093 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +020094 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return;
96 }
97
Peter Zijlstra20273942010-10-27 15:32:58 -070098 type = kmap_atomic_idx();
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700100#ifdef CONFIG_DEBUG_HIGHMEM
101 {
102 unsigned long idx;
103
104 idx = type + KM_TYPE_NR * smp_processor_id();
105 BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN+idx));
106
107 /* XXX Fix - Anton */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#if 0
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700109 __flush_cache_one(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#else
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700111 flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700114 /*
115 * force other mappings to Oops if they'll try to access
116 * this pte without first remap it
117 */
118 pte_clear(&init_mm, vaddr, kmap_pte-idx);
119 /* XXX Fix - Anton */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#if 0
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700121 __flush_tlb_one(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#else
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700123 flush_tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#endif
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif
Peter Zijlstra20273942010-10-27 15:32:58 -0700127
128 kmap_atomic_idx_pop();
Peter Zijlstraa8663742006-12-06 20:32:20 -0800129 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200130 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700132EXPORT_SYMBOL(__kunmap_atomic);