blob: 1c3bf95f7356826a9243081c8269bd1475df0b1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/highmem.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -07002#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4void *kmap(struct page *page)
5{
6 might_sleep();
7 if (!PageHighMem(page))
8 return page_address(page);
9 return kmap_high(page);
10}
11
12void kunmap(struct page *page)
13{
14 if (in_interrupt())
15 BUG();
16 if (!PageHighMem(page))
17 return;
18 kunmap_high(page);
19}
20
21/*
22 * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because
23 * no global lock is needed and because the kmap code must perform a global TLB
24 * invalidation when the kmap pool wraps.
25 *
26 * However when holding an atomic kmap is is not legal to sleep, so atomic
27 * kmaps are appropriate for short, tight code paths only.
28 */
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020029void *kmap_atomic_prot(struct page *page, enum km_type type, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 enum fixed_addresses idx;
32 unsigned long vaddr;
33
34 /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
Peter Zijlstraa8663742006-12-06 20:32:20 -080035 pagefault_disable();
Ingo Molnar656dad32007-02-10 01:46:36 -080036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 if (!PageHighMem(page))
38 return page_address(page);
39
Andrew Morton4150d3f2007-09-11 15:24:10 -070040 idx = type + KM_TYPE_NR*smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
Andrew Morton4150d3f2007-09-11 15:24:10 -070042 BUG_ON(!pte_none(*(kmap_pte-idx)));
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020043 set_pte(kmap_pte-idx, mk_pte(page, prot));
Zachary Amsden49f19712007-04-08 16:04:01 -070044 arch_flush_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Andrew Morton4150d3f2007-09-11 15:24:10 -070046 return (void *)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020049void *kmap_atomic(struct page *page, enum km_type type)
50{
51 return kmap_atomic_prot(page, type, kmap_prot);
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054void kunmap_atomic(void *kvaddr, enum km_type type)
55{
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
57 enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id();
58
Zachary Amsden23002d82006-09-30 23:29:35 -070059 /*
60 * Force other mappings to Oops if they'll try to access this pte
61 * without first remap it. Keeping stale mappings around is a bad idea
62 * also, in case the page changes cacheability attributes or becomes
63 * a protected page in a hypervisor.
64 */
Jeremy Fitzhardinge3b179792006-12-06 20:32:22 -080065 if (vaddr == __fix_to_virt(FIX_KMAP_BEGIN+idx))
66 kpte_clear_flush(kmap_pte-idx, vaddr);
67 else {
68#ifdef CONFIG_DEBUG_HIGHMEM
69 BUG_ON(vaddr < PAGE_OFFSET);
70 BUG_ON(vaddr >= (unsigned long)high_memory);
71#endif
72 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jeremy Fitzhardinge7b2f27f2007-05-02 19:27:15 +020074 arch_flush_lazy_mmu_mode();
Peter Zijlstraa8663742006-12-06 20:32:20 -080075 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Vivek Goyal60e64d42005-06-25 14:58:19 -070078/* This is the same as kmap_atomic() but can map memory that doesn't
79 * have a struct page associated with it.
80 */
81void *kmap_atomic_pfn(unsigned long pfn, enum km_type type)
82{
83 enum fixed_addresses idx;
84 unsigned long vaddr;
85
Peter Zijlstraa8663742006-12-06 20:32:20 -080086 pagefault_disable();
Vivek Goyal60e64d42005-06-25 14:58:19 -070087
88 idx = type + KM_TYPE_NR*smp_processor_id();
89 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
90 set_pte(kmap_pte-idx, pfn_pte(pfn, kmap_prot));
Zachary Amsden49f19712007-04-08 16:04:01 -070091 arch_flush_lazy_mmu_mode();
Vivek Goyal60e64d42005-06-25 14:58:19 -070092
93 return (void*) vaddr;
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096struct page *kmap_atomic_to_page(void *ptr)
97{
98 unsigned long idx, vaddr = (unsigned long)ptr;
99 pte_t *pte;
100
101 if (vaddr < FIXADDR_START)
102 return virt_to_page(ptr);
103
104 idx = virt_to_fix(vaddr);
105 pte = kmap_pte - (idx - FIX_KMAP_BEGIN);
106 return pte_page(*pte);
107}
108
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700109EXPORT_SYMBOL(kmap);
110EXPORT_SYMBOL(kunmap);
111EXPORT_SYMBOL(kmap_atomic);
112EXPORT_SYMBOL(kunmap_atomic);
113EXPORT_SYMBOL(kmap_atomic_to_page);