blob: da815d295239baaaf6e1d1069e2255baa8d75358 [file] [log] [blame]
Ralf Baechleb99fbc12012-09-06 11:29:53 +02001#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/module.h>
3#include <linux/highmem.h>
Yoichi Yuasa52ab3202010-02-20 21:23:22 +09004#include <linux/sched.h>
Ralf Baechle631330f2009-06-19 14:05:26 +01005#include <linux/smp.h>
Ralf Baechlebb86bf22009-04-25 11:25:34 +02006#include <asm/fixmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <asm/tlbflush.h>
8
Ralf Baechlebb86bf22009-04-25 11:25:34 +02009static pte_t *kmap_pte;
10
11unsigned long highstart_pfn, highend_pfn;
12
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070013void *kmap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014{
15 void *addr;
16
17 might_sleep();
18 if (!PageHighMem(page))
19 return page_address(page);
20 addr = kmap_high(page);
21 flush_tlb_one((unsigned long)addr);
22
23 return addr;
24}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070025EXPORT_SYMBOL(kmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070027void kunmap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Ralf Baechleb72b7092009-03-30 14:49:44 +020029 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 if (!PageHighMem(page))
31 return;
32 kunmap_high(page);
33}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070034EXPORT_SYMBOL(kunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
37 * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because
38 * no global lock is needed and because the kmap code must perform a global TLB
39 * invalidation when the kmap pool wraps.
40 *
41 * However when holding an atomic kmap is is not legal to sleep, so atomic
42 * kmaps are appropriate for short, tight code paths only.
43 */
44
Cong Wanga24401b2011-11-26 10:53:39 +080045void *kmap_atomic(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 unsigned long vaddr;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070048 int idx, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
Peter Zijlstraa8663742006-12-06 20:32:20 -080051 pagefault_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!PageHighMem(page))
53 return page_address(page);
54
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070055 type = kmap_atomic_idx_push();
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 idx = type + KM_TYPE_NR*smp_processor_id();
57 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
58#ifdef CONFIG_DEBUG_HIGHMEM
Ralf Baechleb72b7092009-03-30 14:49:44 +020059 BUG_ON(!pte_none(*(kmap_pte - idx)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
Ralf Baechlebb86bf22009-04-25 11:25:34 +020061 set_pte(kmap_pte-idx, mk_pte(page, PAGE_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 local_flush_tlb_one((unsigned long)vaddr);
63
64 return (void*) vaddr;
65}
Cong Wanga24401b2011-11-26 10:53:39 +080066EXPORT_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;
Ralf Baechleb99fbc12012-09-06 11:29:53 +020071 int type __maybe_unused;
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();
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070079#ifdef CONFIG_DEBUG_HIGHMEM
80 {
81 int idx = type + KM_TYPE_NR * smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070083 BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
84
85 /*
86 * force other mappings to Oops if they'll try to access
87 * this pte without first remap it
88 */
89 pte_clear(&init_mm, vaddr, kmap_pte-idx);
90 local_flush_tlb_one(vaddr);
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif
Peter Zijlstra20273942010-10-27 15:32:58 -070093 kmap_atomic_idx_pop();
Peter Zijlstraa8663742006-12-06 20:32:20 -080094 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070096EXPORT_SYMBOL(__kunmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Ralf Baechle60080262005-07-11 20:45:51 +000098/*
99 * This is the same as kmap_atomic() but can map memory that doesn't
100 * have a struct page associated with it.
101 */
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700102void *kmap_atomic_pfn(unsigned long pfn)
Ralf Baechle60080262005-07-11 20:45:51 +0000103{
Ralf Baechle60080262005-07-11 20:45:51 +0000104 unsigned long vaddr;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700105 int idx, type;
Ralf Baechle60080262005-07-11 20:45:51 +0000106
Peter Zijlstraa8663742006-12-06 20:32:20 -0800107 pagefault_disable();
Ralf Baechle60080262005-07-11 20:45:51 +0000108
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700109 type = kmap_atomic_idx_push();
Ralf Baechle60080262005-07-11 20:45:51 +0000110 idx = type + KM_TYPE_NR*smp_processor_id();
111 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
Ralf Baechlebb86bf22009-04-25 11:25:34 +0200112 set_pte(kmap_pte-idx, pfn_pte(pfn, PAGE_KERNEL));
Ralf Baechle60080262005-07-11 20:45:51 +0000113 flush_tlb_one(vaddr);
114
115 return (void*) vaddr;
116}
117
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700118struct page *kmap_atomic_to_page(void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 unsigned long idx, vaddr = (unsigned long)ptr;
121 pte_t *pte;
122
123 if (vaddr < FIXADDR_START)
124 return virt_to_page(ptr);
125
126 idx = virt_to_fix(vaddr);
127 pte = kmap_pte - (idx - FIX_KMAP_BEGIN);
128 return pte_page(*pte);
129}
130
Ralf Baechlebb86bf22009-04-25 11:25:34 +0200131void __init kmap_init(void)
132{
133 unsigned long kmap_vstart;
134
135 /* cache the first kmap pte */
136 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
137 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
138}