blob: 4d5f8b3c4090530b74ef42cab91e3020d7358a2c [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 Ravnborg1b6d06d2012-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 Ravnborg1b6d06d2012-07-26 11:02:19 +000031#include <asm/pgalloc.h>
32#include <asm/vaddrs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Cong Wanga24401b2011-11-26 10:53:39 +080034void *kmap_atomic(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 unsigned long vaddr;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070037 long idx, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
Peter Zijlstraa8663742006-12-06 20:32:20 -080040 pagefault_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (!PageHighMem(page))
42 return page_address(page);
43
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070044 type = kmap_atomic_idx_push();
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 idx = type + KM_TYPE_NR*smp_processor_id();
46 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
47
48/* XXX Fix - Anton */
49#if 0
50 __flush_cache_one(vaddr);
51#else
52 flush_cache_all();
53#endif
54
55#ifdef CONFIG_DEBUG_HIGHMEM
56 BUG_ON(!pte_none(*(kmap_pte-idx)));
57#endif
58 set_pte(kmap_pte-idx, mk_pte(page, kmap_prot));
59/* XXX Fix - Anton */
60#if 0
61 __flush_tlb_one(vaddr);
62#else
63 flush_tlb_all();
64#endif
65
66 return (void*) vaddr;
67}
Cong Wanga24401b2011-11-26 10:53:39 +080068EXPORT_SYMBOL(kmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070070void __kunmap_atomic(void *kvaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070073 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (vaddr < FIXADDR_START) { // FIXME
Peter Zijlstraa8663742006-12-06 20:32:20 -080076 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return;
78 }
79
Peter Zijlstra20273942010-10-27 15:32:58 -070080 type = kmap_atomic_idx();
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070082#ifdef CONFIG_DEBUG_HIGHMEM
83 {
84 unsigned long idx;
85
86 idx = type + KM_TYPE_NR * smp_processor_id();
87 BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN+idx));
88
89 /* XXX Fix - Anton */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#if 0
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070091 __flush_cache_one(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#else
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070093 flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#endif
95
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070096 /*
97 * force other mappings to Oops if they'll try to access
98 * this pte without first remap it
99 */
100 pte_clear(&init_mm, vaddr, kmap_pte-idx);
101 /* XXX Fix - Anton */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#if 0
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700103 __flush_tlb_one(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#else
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700105 flush_tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#endif
Peter Zijlstra20273942010-10-27 15:32:58 -0700109
110 kmap_atomic_idx_pop();
Peter Zijlstraa8663742006-12-06 20:32:20 -0800111 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700113EXPORT_SYMBOL(__kunmap_atomic);