blob: 4500142bc4aa46429cb2be41a7ee3407426f6155 [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>
Pekka Enberg867c5b522009-03-03 14:10:12 +02003#include <linux/swap.h> /* for totalram_pages */
Jiang Liu7b4b2a02013-07-03 15:03:11 -07004#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6void *kmap(struct page *page)
7{
8 might_sleep();
9 if (!PageHighMem(page))
10 return page_address(page);
11 return kmap_high(page);
12}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070013EXPORT_SYMBOL(kmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15void kunmap(struct page *page)
16{
17 if (in_interrupt())
18 BUG();
19 if (!PageHighMem(page))
20 return;
21 kunmap_high(page);
22}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070023EXPORT_SYMBOL(kunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because
27 * no global lock is needed and because the kmap code must perform a global TLB
28 * invalidation when the kmap pool wraps.
29 *
Figo.zhang565b0c12009-06-29 12:02:55 +080030 * However when holding an atomic kmap it is not legal to sleep, so atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * kmaps are appropriate for short, tight code paths only.
32 */
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070033void *kmap_atomic_prot(struct page *page, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned long vaddr;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070036 int idx, type;
Andrew Morton9c312052008-03-28 11:47:34 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
Peter Zijlstraa8663742006-12-06 20:32:20 -080039 pagefault_disable();
Ingo Molnar656dad32007-02-10 01:46:36 -080040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (!PageHighMem(page))
42 return page_address(page);
43
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070044 type = kmap_atomic_idx_push();
Andrew Morton4150d3f2007-09-11 15:24:10 -070045 idx = type + KM_TYPE_NR*smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
Andrew Morton4150d3f2007-09-11 15:24:10 -070047 BUG_ON(!pte_none(*(kmap_pte-idx)));
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020048 set_pte(kmap_pte-idx, mk_pte(page, prot));
Konrad Rzeszutek Wilk2cd1c8d2011-11-15 14:49:09 -080049 arch_flush_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andrew Morton4150d3f2007-09-11 15:24:10 -070051 return (void *)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070053EXPORT_SYMBOL(kmap_atomic_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Cong Wanga24401b2011-11-26 10:53:39 +080055void *kmap_atomic(struct page *page)
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020056{
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070057 return kmap_atomic_prot(page, kmap_prot);
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020058}
Cong Wanga24401b2011-11-26 10:53:39 +080059EXPORT_SYMBOL(kmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ingo Molnardd63fdc2009-03-13 03:20:49 +010061/*
62 * This is the same as kmap_atomic() but can map memory that doesn't
Vivek Goyal60e64d42005-06-25 14:58:19 -070063 * have a struct page associated with it.
64 */
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070065void *kmap_atomic_pfn(unsigned long pfn)
Vivek Goyal60e64d42005-06-25 14:58:19 -070066{
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070067 return kmap_atomic_prot_pfn(pfn, kmap_prot);
Vivek Goyal60e64d42005-06-25 14:58:19 -070068}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070069EXPORT_SYMBOL_GPL(kmap_atomic_pfn);
70
71void __kunmap_atomic(void *kvaddr)
72{
73 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
74
75 if (vaddr >= __fix_to_virt(FIX_KMAP_END) &&
76 vaddr <= __fix_to_virt(FIX_KMAP_BEGIN)) {
77 int idx, type;
78
Peter Zijlstra20273942010-10-27 15:32:58 -070079 type = kmap_atomic_idx();
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070080 idx = type + KM_TYPE_NR * smp_processor_id();
81
82#ifdef CONFIG_DEBUG_HIGHMEM
83 WARN_ON_ONCE(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
84#endif
85 /*
86 * Force other mappings to Oops if they'll try to access this
87 * pte without first remap it. Keeping stale mappings around
88 * is a bad idea also, in case the page changes cacheability
89 * attributes or becomes a protected page in a hypervisor.
90 */
91 kpte_clear_flush(kmap_pte-idx, vaddr);
Peter Zijlstra20273942010-10-27 15:32:58 -070092 kmap_atomic_idx_pop();
Konrad Rzeszutek Wilk2cd1c8d2011-11-15 14:49:09 -080093 arch_flush_lazy_mmu_mode();
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070094 }
95#ifdef CONFIG_DEBUG_HIGHMEM
96 else {
97 BUG_ON(vaddr < PAGE_OFFSET);
98 BUG_ON(vaddr >= (unsigned long)high_memory);
99 }
100#endif
101
102 pagefault_enable();
103}
104EXPORT_SYMBOL(__kunmap_atomic);
Vivek Goyal60e64d42005-06-25 14:58:19 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct page *kmap_atomic_to_page(void *ptr)
107{
108 unsigned long idx, vaddr = (unsigned long)ptr;
109 pte_t *pte;
110
111 if (vaddr < FIXADDR_START)
112 return virt_to_page(ptr);
113
114 idx = virt_to_fix(vaddr);
115 pte = kmap_pte - (idx - FIX_KMAP_BEGIN);
116 return pte_page(*pte);
117}
Avi Kivity256cd2e2009-08-10 10:41:27 +0300118EXPORT_SYMBOL(kmap_atomic_to_page);
Pekka Enberg867c5b522009-03-03 14:10:12 +0200119
Pekka Enberg867c5b522009-03-03 14:10:12 +0200120void __init set_highmem_pages_init(void)
121{
122 struct zone *zone;
123 int nid;
124
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700125 /*
126 * Explicitly reset zone->managed_pages because set_highmem_pages_init()
127 * is invoked before free_all_bootmem()
128 */
129 reset_all_zones_managed_pages();
Pekka Enberg867c5b522009-03-03 14:10:12 +0200130 for_each_zone(zone) {
131 unsigned long zone_start_pfn, zone_end_pfn;
132
133 if (!is_highmem(zone))
134 continue;
135
136 zone_start_pfn = zone->zone_start_pfn;
137 zone_end_pfn = zone_start_pfn + zone->spanned_pages;
138
139 nid = zone_to_nid(zone);
140 printk(KERN_INFO "Initializing %s for node %d (%08lx:%08lx)\n",
141 zone->name, nid, zone_start_pfn, zone_end_pfn);
142
143 add_highpages_with_active_regions(nid, zone_start_pfn,
144 zone_end_pfn);
145 }
Pekka Enberg867c5b522009-03-03 14:10:12 +0200146}