blob: f60398aeb6445fb4537fb7be3fdac7f12e411f81 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Keith Packardfd940932008-10-30 19:37:09 -07002/*
3 * Copyright © 2008 Ingo Molnar
Keith Packardfd940932008-10-30 19:37:09 -07004 */
5
6#include <asm/iomap.h>
Ingo Molnareb243d12019-11-20 15:33:57 +01007#include <asm/memtype.h>
Paul Gortmaker4b599fed2016-07-13 20:18:55 -04008#include <linux/export.h>
Akinobu Mita7ca43e72009-03-31 15:23:25 -07009#include <linux/highmem.h>
Keith Packardfd940932008-10-30 19:37:09 -070010
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070011static int is_io_mapping_possible(resource_size_t base, unsigned long size)
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080012{
Andrew Morton6a491e22009-04-02 16:44:38 -070013#if !defined(CONFIG_X86_PAE) && defined(CONFIG_PHYS_ADDR_T_64BIT)
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080014 /* There is no way to map greater than 1 << 32 address without PAE */
15 if (base + size > 0x100000000ULL)
16 return 0;
Ingo Molnar92b9af92009-02-28 14:09:27 +010017#endif
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080018 return 1;
19}
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070020
21int iomap_create_wc(resource_size_t base, unsigned long size, pgprot_t *prot)
22{
Juergen Gross49a3b3c2014-11-03 14:01:54 +010023 enum page_cache_mode pcm = _PAGE_CACHE_MODE_WC;
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070024 int ret;
25
26 if (!is_io_mapping_possible(base, size))
27 return -EINVAL;
28
Ingo Molnarecdd6ee2019-11-20 15:30:44 +010029 ret = memtype_reserve_io(base, base + size, &pcm);
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070030 if (ret)
31 return ret;
32
Juergen Gross49a3b3c2014-11-03 14:01:54 +010033 *prot = __pgprot(__PAGE_KERNEL | cachemode2protval(pcm));
Dave Hansenfb43d6c2018-04-06 13:55:09 -070034 /* Filter out unsupported __PAGE_KERNEL* bits: */
35 pgprot_val(*prot) &= __default_kernel_pte_mask;
36
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070037 return 0;
38}
39EXPORT_SYMBOL_GPL(iomap_create_wc);
40
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070041void iomap_free(resource_size_t base, unsigned long size)
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070042{
Ingo Molnarecdd6ee2019-11-20 15:30:44 +010043 memtype_free_io(base, base + size);
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070044}
45EXPORT_SYMBOL_GPL(iomap_free);
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080046
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070047void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
Keith Packardfd940932008-10-30 19:37:09 -070048{
Keith Packardfd940932008-10-30 19:37:09 -070049 unsigned long vaddr;
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070050 int idx, type;
Keith Packardfd940932008-10-30 19:37:09 -070051
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +020052 preempt_disable();
Keith Packardfd940932008-10-30 19:37:09 -070053 pagefault_disable();
54
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070055 type = kmap_atomic_idx_push();
Ingo Molnardd63fdc2009-03-13 03:20:49 +010056 idx = type + KM_TYPE_NR * smp_processor_id();
57 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
58 set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
59 arch_flush_lazy_mmu_mode();
60
61 return (void *)vaddr;
62}
63
64/*
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070065 * Map 'pfn' using protections 'prot'
Keith Packardfd940932008-10-30 19:37:09 -070066 */
Francisco Jerezcc1a8e52010-09-04 22:56:43 +020067void __iomem *
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070068iomap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
Keith Packardfd940932008-10-30 19:37:09 -070069{
Eric Anholtef5fa0a2009-01-23 14:14:21 -080070 /*
Borislav Petkov7202fdb2015-06-04 18:55:11 +020071 * For non-PAT systems, translate non-WB request to UC- just in
72 * case the caller set the PWT bit to prot directly without using
73 * pgprot_writecombine(). UC- translates to uncached if the MTRR
74 * is UC or WC. UC- gets the real intention, of the user, which is
75 * "WC if the MTRR is WC, UC if you can't do that."
Eric Anholtef5fa0a2009-01-23 14:14:21 -080076 */
Borislav Petkov7202fdb2015-06-04 18:55:11 +020077 if (!pat_enabled() && pgprot2cachemode(prot) != _PAGE_CACHE_MODE_WB)
Juergen Gross49a3b3c2014-11-03 14:01:54 +010078 prot = __pgprot(__PAGE_KERNEL |
79 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS));
Eric Anholtef5fa0a2009-01-23 14:14:21 -080080
Dave Hansenfb43d6c2018-04-06 13:55:09 -070081 /* Filter out unsupported __PAGE_KERNEL* bits: */
82 pgprot_val(prot) &= __default_kernel_pte_mask;
83
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070084 return (void __force __iomem *) kmap_atomic_prot_pfn(pfn, prot);
Keith Packardfd940932008-10-30 19:37:09 -070085}
86EXPORT_SYMBOL_GPL(iomap_atomic_prot_pfn);
87
88void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070089iounmap_atomic(void __iomem *kvaddr)
Keith Packardfd940932008-10-30 19:37:09 -070090{
91 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
Keith Packardfd940932008-10-30 19:37:09 -070092
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070093 if (vaddr >= __fix_to_virt(FIX_KMAP_END) &&
94 vaddr <= __fix_to_virt(FIX_KMAP_BEGIN)) {
95 int idx, type;
96
Peter Zijlstra20273942010-10-27 15:32:58 -070097 type = kmap_atomic_idx();
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070098 idx = type + KM_TYPE_NR * smp_processor_id();
99
100#ifdef CONFIG_DEBUG_HIGHMEM
101 WARN_ON_ONCE(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
102#endif
103 /*
104 * Force other mappings to Oops if they'll try to access this
105 * pte without first remap it. Keeping stale mappings around
106 * is a bad idea also, in case the page changes cacheability
107 * attributes or becomes a protected page in a hypervisor.
108 */
Keith Packardfd940932008-10-30 19:37:09 -0700109 kpte_clear_flush(kmap_pte-idx, vaddr);
Peter Zijlstra20273942010-10-27 15:32:58 -0700110 kmap_atomic_idx_pop();
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700111 }
Keith Packardfd940932008-10-30 19:37:09 -0700112
Keith Packardfd940932008-10-30 19:37:09 -0700113 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200114 preempt_enable();
Keith Packardfd940932008-10-30 19:37:09 -0700115}
116EXPORT_SYMBOL_GPL(iounmap_atomic);