522db5e3d0bf973e54ca71e33201454c4aadefda
[linux-2.6.git] / arch / x86 / mm / highmem_32.c
1 #include <linux/highmem.h>
2 #include <linux/module.h>
3 #include <linux/swap.h> /* for totalram_pages */
4
5 void *kmap(struct page *page)
6 {
7         might_sleep();
8         if (!PageHighMem(page))
9                 return page_address(page);
10         return kmap_high(page);
11 }
12
13 void kunmap(struct page *page)
14 {
15         if (in_interrupt())
16                 BUG();
17         if (!PageHighMem(page))
18                 return;
19         kunmap_high(page);
20 }
21
22 static void debug_kmap_atomic_prot(enum km_type type)
23 {
24 #ifdef CONFIG_DEBUG_HIGHMEM
25         static unsigned warn_count = 10;
26
27         if (unlikely(warn_count == 0))
28                 return;
29
30         if (unlikely(in_interrupt())) {
31                 if (in_irq()) {
32                         if (type != KM_IRQ0 && type != KM_IRQ1 &&
33                             type != KM_BIO_SRC_IRQ && type != KM_BIO_DST_IRQ &&
34                             type != KM_BOUNCE_READ) {
35                                 WARN_ON(1);
36                                 warn_count--;
37                         }
38                 } else if (!irqs_disabled()) {  /* softirq */
39                         if (type != KM_IRQ0 && type != KM_IRQ1 &&
40                             type != KM_SOFTIRQ0 && type != KM_SOFTIRQ1 &&
41                             type != KM_SKB_SUNRPC_DATA &&
42                             type != KM_SKB_DATA_SOFTIRQ &&
43                             type != KM_BOUNCE_READ) {
44                                 WARN_ON(1);
45                                 warn_count--;
46                         }
47                 }
48         }
49
50         if (type == KM_IRQ0 || type == KM_IRQ1 || type == KM_BOUNCE_READ ||
51                         type == KM_BIO_SRC_IRQ || type == KM_BIO_DST_IRQ) {
52                 if (!irqs_disabled()) {
53                         WARN_ON(1);
54                         warn_count--;
55                 }
56         } else if (type == KM_SOFTIRQ0 || type == KM_SOFTIRQ1) {
57                 if (irq_count() == 0 && !irqs_disabled()) {
58                         WARN_ON(1);
59                         warn_count--;
60                 }
61         }
62 #endif
63 }
64
65 /*
66  * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because
67  * no global lock is needed and because the kmap code must perform a global TLB
68  * invalidation when the kmap pool wraps.
69  *
70  * However when holding an atomic kmap is is not legal to sleep, so atomic
71  * kmaps are appropriate for short, tight code paths only.
72  */
73 void *kmap_atomic_prot(struct page *page, enum km_type type, pgprot_t prot)
74 {
75         enum fixed_addresses idx;
76         unsigned long vaddr;
77
78         /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
79         pagefault_disable();
80
81         if (!PageHighMem(page))
82                 return page_address(page);
83
84         debug_kmap_atomic_prot(type);
85
86         idx = type + KM_TYPE_NR*smp_processor_id();
87         vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
88         BUG_ON(!pte_none(*(kmap_pte-idx)));
89         set_pte(kmap_pte-idx, mk_pte(page, prot));
90         arch_flush_lazy_mmu_mode();
91
92         return (void *)vaddr;
93 }
94
95 void *kmap_atomic(struct page *page, enum km_type type)
96 {
97         return kmap_atomic_prot(page, type, kmap_prot);
98 }
99
100 void kunmap_atomic(void *kvaddr, enum km_type type)
101 {
102         unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
103         enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id();
104
105         /*
106          * Force other mappings to Oops if they'll try to access this pte
107          * without first remap it.  Keeping stale mappings around is a bad idea
108          * also, in case the page changes cacheability attributes or becomes
109          * a protected page in a hypervisor.
110          */
111         if (vaddr == __fix_to_virt(FIX_KMAP_BEGIN+idx))
112                 kpte_clear_flush(kmap_pte-idx, vaddr);
113         else {
114 #ifdef CONFIG_DEBUG_HIGHMEM
115                 BUG_ON(vaddr < PAGE_OFFSET);
116                 BUG_ON(vaddr >= (unsigned long)high_memory);
117 #endif
118         }
119
120         arch_flush_lazy_mmu_mode();
121         pagefault_enable();
122 }
123
124 /*
125  * This is the same as kmap_atomic() but can map memory that doesn't
126  * have a struct page associated with it.
127  */
128 void *kmap_atomic_pfn(unsigned long pfn, enum km_type type)
129 {
130         return kmap_atomic_prot_pfn(pfn, type, kmap_prot);
131 }
132 EXPORT_SYMBOL_GPL(kmap_atomic_pfn); /* temporarily in use by i915 GEM until vmap */
133
134 struct page *kmap_atomic_to_page(void *ptr)
135 {
136         unsigned long idx, vaddr = (unsigned long)ptr;
137         pte_t *pte;
138
139         if (vaddr < FIXADDR_START)
140                 return virt_to_page(ptr);
141
142         idx = virt_to_fix(vaddr);
143         pte = kmap_pte - (idx - FIX_KMAP_BEGIN);
144         return pte_page(*pte);
145 }
146
147 EXPORT_SYMBOL(kmap);
148 EXPORT_SYMBOL(kunmap);
149 EXPORT_SYMBOL(kmap_atomic);
150 EXPORT_SYMBOL(kunmap_atomic);
151
152 void __init set_highmem_pages_init(void)
153 {
154         struct zone *zone;
155         int nid;
156
157         for_each_zone(zone) {
158                 unsigned long zone_start_pfn, zone_end_pfn;
159
160                 if (!is_highmem(zone))
161                         continue;
162
163                 zone_start_pfn = zone->zone_start_pfn;
164                 zone_end_pfn = zone_start_pfn + zone->spanned_pages;
165
166                 nid = zone_to_nid(zone);
167                 printk(KERN_INFO "Initializing %s for node %d (%08lx:%08lx)\n",
168                                 zone->name, nid, zone_start_pfn, zone_end_pfn);
169
170                 add_highpages_with_active_regions(nid, zone_start_pfn,
171                                  zone_end_pfn);
172         }
173         totalram_pages += totalhigh_pages;
174 }