blob: 4730eac0747b50491876a0bc7e3c2c348c02b067 [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 */
25#include <linux/mm.h>
26#include <linux/highmem.h>
27#include <asm/pgalloc.h>
28#include <asm/cacheflush.h>
29#include <asm/tlbflush.h>
30#include <asm/fixmap.h>
31
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070032void *__kmap_atomic(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 unsigned long vaddr;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070035 long idx, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
Peter Zijlstraa8663742006-12-06 20:32:20 -080038 pagefault_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (!PageHighMem(page))
40 return page_address(page);
41
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070042 type = kmap_atomic_idx_push();
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 idx = type + KM_TYPE_NR*smp_processor_id();
44 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
45
46/* XXX Fix - Anton */
47#if 0
48 __flush_cache_one(vaddr);
49#else
50 flush_cache_all();
51#endif
52
53#ifdef CONFIG_DEBUG_HIGHMEM
54 BUG_ON(!pte_none(*(kmap_pte-idx)));
55#endif
56 set_pte(kmap_pte-idx, mk_pte(page, kmap_prot));
57/* XXX Fix - Anton */
58#if 0
59 __flush_tlb_one(vaddr);
60#else
61 flush_tlb_all();
62#endif
63
64 return (void*) vaddr;
65}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070066EXPORT_SYMBOL(__kmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070068void __kunmap_atomic(void *kvaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070071 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (vaddr < FIXADDR_START) { // FIXME
Peter Zijlstraa8663742006-12-06 20:32:20 -080074 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return;
76 }
77
Peter Zijlstra20273942010-10-27 15:32:58 -070078 type = kmap_atomic_idx();
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070080#ifdef CONFIG_DEBUG_HIGHMEM
81 {
82 unsigned long idx;
83
84 idx = type + KM_TYPE_NR * smp_processor_id();
85 BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN+idx));
86
87 /* XXX Fix - Anton */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#if 0
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070089 __flush_cache_one(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#else
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070091 flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif
93
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070094 /*
95 * force other mappings to Oops if they'll try to access
96 * this pte without first remap it
97 */
98 pte_clear(&init_mm, vaddr, kmap_pte-idx);
99 /* XXX Fix - Anton */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#if 0
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700101 __flush_tlb_one(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#else
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700103 flush_tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif
Peter Zijlstra20273942010-10-27 15:32:58 -0700107
108 kmap_atomic_idx_pop();
Peter Zijlstraa8663742006-12-06 20:32:20 -0800109 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700111EXPORT_SYMBOL(__kunmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* We may be fed a pagetable here by ptep_to_xxx and others. */
114struct page *kmap_atomic_to_page(void *ptr)
115{
116 unsigned long idx, vaddr = (unsigned long)ptr;
117 pte_t *pte;
118
119 if (vaddr < SRMMU_NOCACHE_VADDR)
120 return virt_to_page(ptr);
121 if (vaddr < PKMAP_BASE)
122 return pfn_to_page(__nocache_pa(vaddr) >> PAGE_SHIFT);
123 BUG_ON(vaddr < FIXADDR_START);
124 BUG_ON(vaddr > FIXADDR_TOP);
125
126 idx = virt_to_fix(vaddr);
127 pte = kmap_pte - (idx - FIX_KMAP_BEGIN);
128 return pte_page(*pte);
129}