Merge tag 'mips_5.18_1' of git://git.kernel.org/pub/scm/linux/kernel/git/mips/linux
[linux-block.git] / include / linux / io-mapping.h
CommitLineData
775c8a3d 1/* SPDX-License-Identifier: GPL-2.0-only */
9663f2e6
KP
2/*
3 * Copyright © 2008 Keith Packard <keithp@keithp.com>
9663f2e6
KP
4 */
5
6#ifndef _LINUX_IO_MAPPING_H
7#define _LINUX_IO_MAPPING_H
8
9#include <linux/types.h>
5a0e3ad6 10#include <linux/slab.h>
187f1882 11#include <linux/bug.h>
2584cf83 12#include <linux/io.h>
65fddcfc 13#include <linux/pgtable.h>
9663f2e6 14#include <asm/page.h>
9663f2e6
KP
15
16/*
17 * The io_mapping mechanism provides an abstraction for mapping
18 * individual pages from an io device to the CPU in an efficient fashion.
19 *
7d3d3254 20 * See Documentation/driver-api/io-mapping.rst
9663f2e6
KP
21 */
22
4ab0d47d
VP
23struct io_mapping {
24 resource_size_t base;
25 unsigned long size;
26 pgprot_t prot;
cafaf14a 27 void __iomem *iomem;
4ab0d47d
VP
28};
29
cafaf14a
CW
30#ifdef CONFIG_HAVE_ATOMIC_IOMAP
31
2b755626 32#include <linux/pfn.h>
cafaf14a 33#include <asm/iomap.h>
e5beae16
KP
34/*
35 * For small address space machines, mapping large objects
36 * into the kernel virtual space isn't practical. Where
37 * available, use fixmap support to dynamically map pages
38 * of the object at run time.
39 */
9663f2e6 40
9663f2e6 41static inline struct io_mapping *
cafaf14a
CW
42io_mapping_init_wc(struct io_mapping *iomap,
43 resource_size_t base,
44 unsigned long size)
9663f2e6 45{
9e36fda0 46 pgprot_t prot;
4ab0d47d 47
9e36fda0 48 if (iomap_create_wc(base, size, &prot))
cafaf14a 49 return NULL;
4ab0d47d
VP
50
51 iomap->base = base;
52 iomap->size = size;
9e36fda0 53 iomap->prot = prot;
4ab0d47d 54 return iomap;
9663f2e6
KP
55}
56
57static inline void
cafaf14a 58io_mapping_fini(struct io_mapping *mapping)
9663f2e6 59{
9e36fda0 60 iomap_free(mapping->base, mapping->size);
9663f2e6
KP
61}
62
63/* Atomic map/unmap */
29bc17ec 64static inline void __iomem *
fca3ec01 65io_mapping_map_atomic_wc(struct io_mapping *mapping,
3e4d3af5 66 unsigned long offset)
9663f2e6 67{
4ab0d47d 68 resource_size_t phys_addr;
4ab0d47d
VP
69
70 BUG_ON(offset >= mapping->size);
71 phys_addr = mapping->base + offset;
351191ad
TG
72 preempt_disable();
73 pagefault_disable();
74 return __iomap_local_pfn_prot(PHYS_PFN(phys_addr), mapping->prot);
9663f2e6
KP
75}
76
77static inline void
3e4d3af5 78io_mapping_unmap_atomic(void __iomem *vaddr)
9663f2e6 79{
351191ad
TG
80 kunmap_local_indexed((void __force *)vaddr);
81 pagefault_enable();
82 preempt_enable();
9663f2e6
KP
83}
84
e66f6e09
TG
85static inline void __iomem *
86io_mapping_map_local_wc(struct io_mapping *mapping, unsigned long offset)
87{
88 resource_size_t phys_addr;
89
90 BUG_ON(offset >= mapping->size);
91 phys_addr = mapping->base + offset;
92 return __iomap_local_pfn_prot(PHYS_PFN(phys_addr), mapping->prot);
93}
94
95static inline void io_mapping_unmap_local(void __iomem *vaddr)
96{
97 kunmap_local_indexed((void __force *)vaddr);
98}
99
29bc17ec 100static inline void __iomem *
d8dab00d
CW
101io_mapping_map_wc(struct io_mapping *mapping,
102 unsigned long offset,
103 unsigned long size)
9663f2e6 104{
5ce04e3d
PV
105 resource_size_t phys_addr;
106
4ab0d47d 107 BUG_ON(offset >= mapping->size);
5ce04e3d
PV
108 phys_addr = mapping->base + offset;
109
d8dab00d 110 return ioremap_wc(phys_addr, size);
9663f2e6
KP
111}
112
113static inline void
29bc17ec 114io_mapping_unmap(void __iomem *vaddr)
9663f2e6 115{
e5beae16 116 iounmap(vaddr);
9663f2e6
KP
117}
118
e66f6e09 119#else /* HAVE_ATOMIC_IOMAP */
9663f2e6 120
24dd85ff 121#include <linux/uaccess.h>
4ab0d47d 122
e5beae16 123/* Create the io_mapping object*/
9663f2e6 124static inline struct io_mapping *
cafaf14a
CW
125io_mapping_init_wc(struct io_mapping *iomap,
126 resource_size_t base,
127 unsigned long size)
128{
e0b3e0b1
MR
129 iomap->iomem = ioremap_wc(base, size);
130 if (!iomap->iomem)
131 return NULL;
132
cafaf14a
CW
133 iomap->base = base;
134 iomap->size = size;
bcaaa0c4 135 iomap->prot = pgprot_writecombine(PAGE_KERNEL);
cafaf14a
CW
136
137 return iomap;
138}
139
140static inline void
141io_mapping_fini(struct io_mapping *mapping)
142{
143 iounmap(mapping->iomem);
144}
145
146/* Non-atomic map/unmap */
147static inline void __iomem *
148io_mapping_map_wc(struct io_mapping *mapping,
149 unsigned long offset,
150 unsigned long size)
9663f2e6 151{
cafaf14a 152 return mapping->iomem + offset;
9663f2e6
KP
153}
154
155static inline void
cafaf14a 156io_mapping_unmap(void __iomem *vaddr)
9663f2e6
KP
157{
158}
159
160/* Atomic map/unmap */
29bc17ec 161static inline void __iomem *
fca3ec01 162io_mapping_map_atomic_wc(struct io_mapping *mapping,
3e4d3af5 163 unsigned long offset)
9663f2e6 164{
2cb7c9cb 165 preempt_disable();
24dd85ff 166 pagefault_disable();
cafaf14a 167 return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
9663f2e6
KP
168}
169
170static inline void
3e4d3af5 171io_mapping_unmap_atomic(void __iomem *vaddr)
9663f2e6 172{
cafaf14a 173 io_mapping_unmap(vaddr);
24dd85ff 174 pagefault_enable();
2cb7c9cb 175 preempt_enable();
9663f2e6
KP
176}
177
e66f6e09
TG
178static inline void __iomem *
179io_mapping_map_local_wc(struct io_mapping *mapping, unsigned long offset)
180{
181 return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
182}
183
184static inline void io_mapping_unmap_local(void __iomem *vaddr)
185{
186 io_mapping_unmap(vaddr);
187}
188
189#endif /* !HAVE_ATOMIC_IOMAP */
cafaf14a
CW
190
191static inline struct io_mapping *
192io_mapping_create_wc(resource_size_t base,
193 unsigned long size)
9663f2e6 194{
cafaf14a
CW
195 struct io_mapping *iomap;
196
197 iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
198 if (!iomap)
199 return NULL;
200
201 if (!io_mapping_init_wc(iomap, base, size)) {
202 kfree(iomap);
203 return NULL;
204 }
205
206 return iomap;
9663f2e6
KP
207}
208
209static inline void
cafaf14a 210io_mapping_free(struct io_mapping *iomap)
9663f2e6 211{
cafaf14a
CW
212 io_mapping_fini(iomap);
213 kfree(iomap);
9663f2e6 214}
e5beae16 215
9663f2e6 216#endif /* _LINUX_IO_MAPPING_H */
1fbaf8fc
CH
217
218int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma,
219 unsigned long addr, unsigned long pfn, unsigned long size);