Commit | Line | Data |
---|---|---|
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 |
23 | struct 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 | 41 | static inline struct io_mapping * |
cafaf14a CW |
42 | io_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 | ||
57 | static inline void | |
cafaf14a | 58 | io_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 | 64 | static inline void __iomem * |
fca3ec01 | 65 | io_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; | |
2b755626 | 72 | return iomap_atomic_prot_pfn(PHYS_PFN(phys_addr), mapping->prot); |
9663f2e6 KP |
73 | } |
74 | ||
75 | static inline void | |
3e4d3af5 | 76 | io_mapping_unmap_atomic(void __iomem *vaddr) |
9663f2e6 | 77 | { |
3e4d3af5 | 78 | iounmap_atomic(vaddr); |
9663f2e6 KP |
79 | } |
80 | ||
29bc17ec | 81 | static inline void __iomem * |
d8dab00d CW |
82 | io_mapping_map_wc(struct io_mapping *mapping, |
83 | unsigned long offset, | |
84 | unsigned long size) | |
9663f2e6 | 85 | { |
5ce04e3d PV |
86 | resource_size_t phys_addr; |
87 | ||
4ab0d47d | 88 | BUG_ON(offset >= mapping->size); |
5ce04e3d PV |
89 | phys_addr = mapping->base + offset; |
90 | ||
d8dab00d | 91 | return ioremap_wc(phys_addr, size); |
9663f2e6 KP |
92 | } |
93 | ||
94 | static inline void | |
29bc17ec | 95 | io_mapping_unmap(void __iomem *vaddr) |
9663f2e6 | 96 | { |
e5beae16 | 97 | iounmap(vaddr); |
9663f2e6 KP |
98 | } |
99 | ||
e5beae16 | 100 | #else |
9663f2e6 | 101 | |
24dd85ff | 102 | #include <linux/uaccess.h> |
4ab0d47d | 103 | |
e5beae16 | 104 | /* Create the io_mapping object*/ |
9663f2e6 | 105 | static inline struct io_mapping * |
cafaf14a CW |
106 | io_mapping_init_wc(struct io_mapping *iomap, |
107 | resource_size_t base, | |
108 | unsigned long size) | |
109 | { | |
e0b3e0b1 MR |
110 | iomap->iomem = ioremap_wc(base, size); |
111 | if (!iomap->iomem) | |
112 | return NULL; | |
113 | ||
cafaf14a CW |
114 | iomap->base = base; |
115 | iomap->size = size; | |
35124389 DV |
116 | #if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */ |
117 | iomap->prot = pgprot_noncached_wc(PAGE_KERNEL); | |
118 | #elif defined(pgprot_writecombine) | |
bcaaa0c4 | 119 | iomap->prot = pgprot_writecombine(PAGE_KERNEL); |
35124389 DV |
120 | #else |
121 | iomap->prot = pgprot_noncached(PAGE_KERNEL); | |
122 | #endif | |
cafaf14a CW |
123 | |
124 | return iomap; | |
125 | } | |
126 | ||
127 | static inline void | |
128 | io_mapping_fini(struct io_mapping *mapping) | |
129 | { | |
130 | iounmap(mapping->iomem); | |
131 | } | |
132 | ||
133 | /* Non-atomic map/unmap */ | |
134 | static inline void __iomem * | |
135 | io_mapping_map_wc(struct io_mapping *mapping, | |
136 | unsigned long offset, | |
137 | unsigned long size) | |
9663f2e6 | 138 | { |
cafaf14a | 139 | return mapping->iomem + offset; |
9663f2e6 KP |
140 | } |
141 | ||
142 | static inline void | |
cafaf14a | 143 | io_mapping_unmap(void __iomem *vaddr) |
9663f2e6 KP |
144 | { |
145 | } | |
146 | ||
147 | /* Atomic map/unmap */ | |
29bc17ec | 148 | static inline void __iomem * |
fca3ec01 | 149 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
3e4d3af5 | 150 | unsigned long offset) |
9663f2e6 | 151 | { |
2cb7c9cb | 152 | preempt_disable(); |
24dd85ff | 153 | pagefault_disable(); |
cafaf14a | 154 | return io_mapping_map_wc(mapping, offset, PAGE_SIZE); |
9663f2e6 KP |
155 | } |
156 | ||
157 | static inline void | |
3e4d3af5 | 158 | io_mapping_unmap_atomic(void __iomem *vaddr) |
9663f2e6 | 159 | { |
cafaf14a | 160 | io_mapping_unmap(vaddr); |
24dd85ff | 161 | pagefault_enable(); |
2cb7c9cb | 162 | preempt_enable(); |
9663f2e6 KP |
163 | } |
164 | ||
cafaf14a CW |
165 | #endif /* HAVE_ATOMIC_IOMAP */ |
166 | ||
167 | static inline struct io_mapping * | |
168 | io_mapping_create_wc(resource_size_t base, | |
169 | unsigned long size) | |
9663f2e6 | 170 | { |
cafaf14a CW |
171 | struct io_mapping *iomap; |
172 | ||
173 | iomap = kmalloc(sizeof(*iomap), GFP_KERNEL); | |
174 | if (!iomap) | |
175 | return NULL; | |
176 | ||
177 | if (!io_mapping_init_wc(iomap, base, size)) { | |
178 | kfree(iomap); | |
179 | return NULL; | |
180 | } | |
181 | ||
182 | return iomap; | |
9663f2e6 KP |
183 | } |
184 | ||
185 | static inline void | |
cafaf14a | 186 | io_mapping_free(struct io_mapping *iomap) |
9663f2e6 | 187 | { |
cafaf14a CW |
188 | io_mapping_fini(iomap); |
189 | kfree(iomap); | |
9663f2e6 | 190 | } |
e5beae16 | 191 | |
9663f2e6 | 192 | #endif /* _LINUX_IO_MAPPING_H */ |