blob: 2b1309b2580a79e09a4be60d315317842f1a9295 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/module.h>
2#include <linux/highmem.h>
Ralf Baechlebb86bf22009-04-25 11:25:34 +02003#include <asm/fixmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <asm/tlbflush.h>
5
Ralf Baechlebb86bf22009-04-25 11:25:34 +02006static pte_t *kmap_pte;
7
8unsigned long highstart_pfn, highend_pfn;
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010void *__kmap(struct page *page)
11{
12 void *addr;
13
14 might_sleep();
15 if (!PageHighMem(page))
16 return page_address(page);
17 addr = kmap_high(page);
18 flush_tlb_one((unsigned long)addr);
19
20 return addr;
21}
Ralf Baechlebb86bf22009-04-25 11:25:34 +020022EXPORT_SYMBOL(__kmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24void __kunmap(struct page *page)
25{
Ralf Baechleb72b7092009-03-30 14:49:44 +020026 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 if (!PageHighMem(page))
28 return;
29 kunmap_high(page);
30}
Ralf Baechlebb86bf22009-04-25 11:25:34 +020031EXPORT_SYMBOL(__kunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because
35 * no global lock is needed and because the kmap code must perform a global TLB
36 * invalidation when the kmap pool wraps.
37 *
38 * However when holding an atomic kmap is is not legal to sleep, so atomic
39 * kmaps are appropriate for short, tight code paths only.
40 */
41
42void *__kmap_atomic(struct page *page, enum km_type type)
43{
44 enum fixed_addresses idx;
45 unsigned long vaddr;
46
47 /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
Peter Zijlstraa8663742006-12-06 20:32:20 -080048 pagefault_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (!PageHighMem(page))
50 return page_address(page);
51
Akinobu Mita7ca43e72009-03-31 15:23:25 -070052 debug_kmap_atomic(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 idx = type + KM_TYPE_NR*smp_processor_id();
54 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
55#ifdef CONFIG_DEBUG_HIGHMEM
Ralf Baechleb72b7092009-03-30 14:49:44 +020056 BUG_ON(!pte_none(*(kmap_pte - idx)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
Ralf Baechlebb86bf22009-04-25 11:25:34 +020058 set_pte(kmap_pte-idx, mk_pte(page, PAGE_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 local_flush_tlb_one((unsigned long)vaddr);
60
61 return (void*) vaddr;
62}
Ralf Baechlebb86bf22009-04-25 11:25:34 +020063EXPORT_SYMBOL(__kmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65void __kunmap_atomic(void *kvaddr, enum km_type type)
66{
67#ifdef CONFIG_DEBUG_HIGHMEM
68 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
69 enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id();
70
71 if (vaddr < FIXADDR_START) { // FIXME
Peter Zijlstraa8663742006-12-06 20:32:20 -080072 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return;
74 }
75
Ralf Baechleb72b7092009-03-30 14:49:44 +020076 BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /*
79 * force other mappings to Oops if they'll try to access
80 * this pte without first remap it
81 */
82 pte_clear(&init_mm, vaddr, kmap_pte-idx);
83 local_flush_tlb_one(vaddr);
84#endif
85
Peter Zijlstraa8663742006-12-06 20:32:20 -080086 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
Ralf Baechlebb86bf22009-04-25 11:25:34 +020088EXPORT_SYMBOL(__kunmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Ralf Baechle60080262005-07-11 20:45:51 +000090/*
91 * This is the same as kmap_atomic() but can map memory that doesn't
92 * have a struct page associated with it.
93 */
94void *kmap_atomic_pfn(unsigned long pfn, enum km_type type)
95{
96 enum fixed_addresses idx;
97 unsigned long vaddr;
98
Peter Zijlstraa8663742006-12-06 20:32:20 -080099 pagefault_disable();
Ralf Baechle60080262005-07-11 20:45:51 +0000100
Akinobu Mita7ca43e72009-03-31 15:23:25 -0700101 debug_kmap_atomic(type);
Ralf Baechle60080262005-07-11 20:45:51 +0000102 idx = type + KM_TYPE_NR*smp_processor_id();
103 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
Ralf Baechlebb86bf22009-04-25 11:25:34 +0200104 set_pte(kmap_pte-idx, pfn_pte(pfn, PAGE_KERNEL));
Ralf Baechle60080262005-07-11 20:45:51 +0000105 flush_tlb_one(vaddr);
106
107 return (void*) vaddr;
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct page *__kmap_atomic_to_page(void *ptr)
111{
112 unsigned long idx, vaddr = (unsigned long)ptr;
113 pte_t *pte;
114
115 if (vaddr < FIXADDR_START)
116 return virt_to_page(ptr);
117
118 idx = virt_to_fix(vaddr);
119 pte = kmap_pte - (idx - FIX_KMAP_BEGIN);
120 return pte_page(*pte);
121}
122
Ralf Baechlebb86bf22009-04-25 11:25:34 +0200123void __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}