Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/highmem.h> |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 2 | #include <linux/module.h> |
Pekka Enberg | 867c5b52 | 2009-03-03 14:10:12 +0200 | [diff] [blame] | 3 | #include <linux/swap.h> /* for totalram_pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 12 | EXPORT_SYMBOL(kmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | void kunmap(struct page *page) |
| 15 | { |
| 16 | if (in_interrupt()) |
| 17 | BUG(); |
| 18 | if (!PageHighMem(page)) |
| 19 | return; |
| 20 | kunmap_high(page); |
| 21 | } |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 22 | EXPORT_SYMBOL(kunmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because |
| 26 | * no global lock is needed and because the kmap code must perform a global TLB |
| 27 | * invalidation when the kmap pool wraps. |
| 28 | * |
Figo.zhang | 565b0c1 | 2009-06-29 12:02:55 +0800 | [diff] [blame] | 29 | * However when holding an atomic kmap it is not legal to sleep, so atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * kmaps are appropriate for short, tight code paths only. |
| 31 | */ |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 32 | void *kmap_atomic_prot(struct page *page, pgprot_t prot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | unsigned long vaddr; |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 35 | int idx, type; |
Andrew Morton | 9c31205 | 2008-03-28 11:47:34 -0700 | [diff] [blame] | 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */ |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 38 | pagefault_disable(); |
Ingo Molnar | 656dad3 | 2007-02-10 01:46:36 -0800 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | if (!PageHighMem(page)) |
| 41 | return page_address(page); |
| 42 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 43 | type = kmap_atomic_idx_push(); |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 44 | idx = type + KM_TYPE_NR*smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 46 | BUG_ON(!pte_none(*(kmap_pte-idx))); |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 47 | set_pte(kmap_pte-idx, mk_pte(page, prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Andrew Morton | 4150d3f | 2007-09-11 15:24:10 -0700 | [diff] [blame] | 49 | return (void *)vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 51 | EXPORT_SYMBOL(kmap_atomic_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 53 | void *__kmap_atomic(struct page *page) |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 54 | { |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 55 | return kmap_atomic_prot(page, kmap_prot); |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 56 | } |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 57 | EXPORT_SYMBOL(__kmap_atomic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Ingo Molnar | dd63fdc | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 59 | /* |
| 60 | * This is the same as kmap_atomic() but can map memory that doesn't |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 61 | * have a struct page associated with it. |
| 62 | */ |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 63 | void *kmap_atomic_pfn(unsigned long pfn) |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 64 | { |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 65 | return kmap_atomic_prot_pfn(pfn, kmap_prot); |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 66 | } |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 67 | EXPORT_SYMBOL_GPL(kmap_atomic_pfn); |
| 68 | |
| 69 | void __kunmap_atomic(void *kvaddr) |
| 70 | { |
| 71 | unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK; |
| 72 | |
| 73 | if (vaddr >= __fix_to_virt(FIX_KMAP_END) && |
| 74 | vaddr <= __fix_to_virt(FIX_KMAP_BEGIN)) { |
| 75 | int idx, type; |
| 76 | |
Peter Zijlstra | 2027394 | 2010-10-27 15:32:58 -0700 | [diff] [blame] | 77 | type = kmap_atomic_idx(); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 78 | idx = type + KM_TYPE_NR * smp_processor_id(); |
| 79 | |
| 80 | #ifdef CONFIG_DEBUG_HIGHMEM |
| 81 | WARN_ON_ONCE(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx)); |
| 82 | #endif |
| 83 | /* |
| 84 | * Force other mappings to Oops if they'll try to access this |
| 85 | * pte without first remap it. Keeping stale mappings around |
| 86 | * is a bad idea also, in case the page changes cacheability |
| 87 | * attributes or becomes a protected page in a hypervisor. |
| 88 | */ |
| 89 | kpte_clear_flush(kmap_pte-idx, vaddr); |
Peter Zijlstra | 2027394 | 2010-10-27 15:32:58 -0700 | [diff] [blame] | 90 | kmap_atomic_idx_pop(); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 91 | } |
| 92 | #ifdef CONFIG_DEBUG_HIGHMEM |
| 93 | else { |
| 94 | BUG_ON(vaddr < PAGE_OFFSET); |
| 95 | BUG_ON(vaddr >= (unsigned long)high_memory); |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | pagefault_enable(); |
| 100 | } |
| 101 | EXPORT_SYMBOL(__kunmap_atomic); |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | struct page *kmap_atomic_to_page(void *ptr) |
| 104 | { |
| 105 | unsigned long idx, vaddr = (unsigned long)ptr; |
| 106 | pte_t *pte; |
| 107 | |
| 108 | if (vaddr < FIXADDR_START) |
| 109 | return virt_to_page(ptr); |
| 110 | |
| 111 | idx = virt_to_fix(vaddr); |
| 112 | pte = kmap_pte - (idx - FIX_KMAP_BEGIN); |
| 113 | return pte_page(*pte); |
| 114 | } |
Avi Kivity | 256cd2e | 2009-08-10 10:41:27 +0300 | [diff] [blame] | 115 | EXPORT_SYMBOL(kmap_atomic_to_page); |
Pekka Enberg | 867c5b52 | 2009-03-03 14:10:12 +0200 | [diff] [blame] | 116 | |
Pekka Enberg | 867c5b52 | 2009-03-03 14:10:12 +0200 | [diff] [blame] | 117 | void __init set_highmem_pages_init(void) |
| 118 | { |
| 119 | struct zone *zone; |
| 120 | int nid; |
| 121 | |
| 122 | for_each_zone(zone) { |
| 123 | unsigned long zone_start_pfn, zone_end_pfn; |
| 124 | |
| 125 | if (!is_highmem(zone)) |
| 126 | continue; |
| 127 | |
| 128 | zone_start_pfn = zone->zone_start_pfn; |
| 129 | zone_end_pfn = zone_start_pfn + zone->spanned_pages; |
| 130 | |
| 131 | nid = zone_to_nid(zone); |
| 132 | printk(KERN_INFO "Initializing %s for node %d (%08lx:%08lx)\n", |
| 133 | zone->name, nid, zone_start_pfn, zone_end_pfn); |
| 134 | |
| 135 | add_highpages_with_active_regions(nid, zone_start_pfn, |
| 136 | zone_end_pfn); |
| 137 | } |
| 138 | totalram_pages += totalhigh_pages; |
| 139 | } |