blob: 86bc2a58d26c479e23d82b1ad198b51722812013 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * highmem.c: virtual kernel memory mappings for high memory
4 *
5 * Provides kernel-static versions of atomic kmap functions originally
6 * found as inlines in include/asm-sparc/highmem.h. These became
7 * needed as kmap_atomic() and kunmap_atomic() started getting
8 * called from within modules.
9 * -- Tomas Szepe <szepe@pinerecords.com>, September 2002
10 *
11 * But kmap_atomic() and kunmap_atomic() cannot be inlined in
12 * modules because they are loaded with btfixup-ped functions.
13 */
14
15/*
16 * The use of kmap_atomic/kunmap_atomic is discouraged - kmap/kunmap
17 * gives a more generic (and caching) interface. But kmap_atomic can
18 * be used in IRQ contexts, so in some (very limited) cases we need it.
19 *
20 * XXX This is an old text. Actually, it's good to use atomic kmaps,
21 * provided you remember that they are atomic and not try to sleep
22 * with a kmap taken, much like a spinlock. Non-atomic kmaps are
23 * shared by CPUs, and so precious, and establishing them requires IPI.
24 * Atomic kmaps are lightweight and we may have NCPUS more of them.
25 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/highmem.h>
Paul Gortmaker7b64db62011-07-18 15:57:46 -040027#include <linux/export.h>
Sam Ravnborg1b6d06d2012-07-26 11:02:19 +000028#include <linux/mm.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/cacheflush.h>
31#include <asm/tlbflush.h>
Sam Ravnborg1b6d06d2012-07-26 11:02:19 +000032#include <asm/pgalloc.h>
33#include <asm/vaddrs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Sam Ravnborg9a4d5b92012-07-26 11:02:27 +000035pgprot_t kmap_prot;
36
37static pte_t *kmap_pte;
38
39void __init kmap_init(void)
40{
41 unsigned long address;
42 pmd_t *dir;
43
44 address = __fix_to_virt(FIX_KMAP_BEGIN);
45 dir = pmd_offset(pgd_offset_k(address), address);
46
47 /* cache the first kmap pte */
48 kmap_pte = pte_offset_kernel(dir, address);
49 kmap_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE);
50}
51
Cong Wanga24401b2011-11-26 10:53:39 +080052void *kmap_atomic(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 unsigned long vaddr;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070055 long idx, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +020057 preempt_disable();
Peter Zijlstraa8663742006-12-06 20:32:20 -080058 pagefault_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!PageHighMem(page))
60 return page_address(page);
61
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070062 type = kmap_atomic_idx_push();
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 idx = type + KM_TYPE_NR*smp_processor_id();
64 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
65
66/* XXX Fix - Anton */
67#if 0
68 __flush_cache_one(vaddr);
69#else
70 flush_cache_all();
71#endif
72
73#ifdef CONFIG_DEBUG_HIGHMEM
74 BUG_ON(!pte_none(*(kmap_pte-idx)));
75#endif
76 set_pte(kmap_pte-idx, mk_pte(page, kmap_prot));
77/* XXX Fix - Anton */
78#if 0
79 __flush_tlb_one(vaddr);
80#else
81 flush_tlb_all();
82#endif
83
84 return (void*) vaddr;
85}
Cong Wanga24401b2011-11-26 10:53:39 +080086EXPORT_SYMBOL(kmap_atomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070088void __kunmap_atomic(void *kvaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070091 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 if (vaddr < FIXADDR_START) { // FIXME
Peter Zijlstraa8663742006-12-06 20:32:20 -080094 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +020095 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return;
97 }
98
Peter Zijlstra20273942010-10-27 15:32:58 -070099 type = kmap_atomic_idx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700101#ifdef CONFIG_DEBUG_HIGHMEM
102 {
103 unsigned long idx;
104
105 idx = type + KM_TYPE_NR * smp_processor_id();
106 BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN+idx));
107
108 /* XXX Fix - Anton */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#if 0
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700110 __flush_cache_one(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#else
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700112 flush_cache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif
114
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700115 /*
116 * force other mappings to Oops if they'll try to access
117 * this pte without first remap it
118 */
119 pte_clear(&init_mm, vaddr, kmap_pte-idx);
120 /* XXX Fix - Anton */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#if 0
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700122 __flush_tlb_one(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#else
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700124 flush_tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#endif
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#endif
Peter Zijlstra20273942010-10-27 15:32:58 -0700128
129 kmap_atomic_idx_pop();
Peter Zijlstraa8663742006-12-06 20:32:20 -0800130 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200131 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700133EXPORT_SYMBOL(__kunmap_atomic);