Commit | Line | Data |
---|---|---|
9663f2e6 KP |
1 | /* |
2 | * Copyright © 2008 Keith Packard <keithp@keithp.com> | |
3 | * | |
4 | * This file is free software; you can redistribute it and/or modify | |
5 | * it under the terms of version 2 of the GNU General Public License | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software Foundation, | |
15 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
16 | */ | |
17 | ||
18 | #ifndef _LINUX_IO_MAPPING_H | |
19 | #define _LINUX_IO_MAPPING_H | |
20 | ||
21 | #include <linux/types.h> | |
5a0e3ad6 | 22 | #include <linux/slab.h> |
187f1882 | 23 | #include <linux/bug.h> |
2584cf83 | 24 | #include <linux/io.h> |
9663f2e6 | 25 | #include <asm/page.h> |
9663f2e6 KP |
26 | |
27 | /* | |
28 | * The io_mapping mechanism provides an abstraction for mapping | |
29 | * individual pages from an io device to the CPU in an efficient fashion. | |
30 | * | |
395cf969 | 31 | * See Documentation/io-mapping.txt |
9663f2e6 KP |
32 | */ |
33 | ||
4ab0d47d VP |
34 | struct io_mapping { |
35 | resource_size_t base; | |
36 | unsigned long size; | |
37 | pgprot_t prot; | |
cafaf14a | 38 | void __iomem *iomem; |
4ab0d47d VP |
39 | }; |
40 | ||
cafaf14a CW |
41 | #ifdef CONFIG_HAVE_ATOMIC_IOMAP |
42 | ||
43 | #include <asm/iomap.h> | |
e5beae16 KP |
44 | /* |
45 | * For small address space machines, mapping large objects | |
46 | * into the kernel virtual space isn't practical. Where | |
47 | * available, use fixmap support to dynamically map pages | |
48 | * of the object at run time. | |
49 | */ | |
9663f2e6 | 50 | |
9663f2e6 | 51 | static inline struct io_mapping * |
cafaf14a CW |
52 | io_mapping_init_wc(struct io_mapping *iomap, |
53 | resource_size_t base, | |
54 | unsigned long size) | |
9663f2e6 | 55 | { |
9e36fda0 | 56 | pgprot_t prot; |
4ab0d47d | 57 | |
9e36fda0 | 58 | if (iomap_create_wc(base, size, &prot)) |
cafaf14a | 59 | return NULL; |
4ab0d47d VP |
60 | |
61 | iomap->base = base; | |
62 | iomap->size = size; | |
9e36fda0 | 63 | iomap->prot = prot; |
4ab0d47d | 64 | return iomap; |
9663f2e6 KP |
65 | } |
66 | ||
67 | static inline void | |
cafaf14a | 68 | io_mapping_fini(struct io_mapping *mapping) |
9663f2e6 | 69 | { |
9e36fda0 | 70 | iomap_free(mapping->base, mapping->size); |
9663f2e6 KP |
71 | } |
72 | ||
73 | /* Atomic map/unmap */ | |
29bc17ec | 74 | static inline void __iomem * |
fca3ec01 | 75 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
3e4d3af5 | 76 | unsigned long offset) |
9663f2e6 | 77 | { |
4ab0d47d VP |
78 | resource_size_t phys_addr; |
79 | unsigned long pfn; | |
80 | ||
81 | BUG_ON(offset >= mapping->size); | |
82 | phys_addr = mapping->base + offset; | |
83 | pfn = (unsigned long) (phys_addr >> PAGE_SHIFT); | |
3e4d3af5 | 84 | return iomap_atomic_prot_pfn(pfn, mapping->prot); |
9663f2e6 KP |
85 | } |
86 | ||
87 | static inline void | |
3e4d3af5 | 88 | io_mapping_unmap_atomic(void __iomem *vaddr) |
9663f2e6 | 89 | { |
3e4d3af5 | 90 | iounmap_atomic(vaddr); |
9663f2e6 KP |
91 | } |
92 | ||
29bc17ec | 93 | static inline void __iomem * |
d8dab00d CW |
94 | io_mapping_map_wc(struct io_mapping *mapping, |
95 | unsigned long offset, | |
96 | unsigned long size) | |
9663f2e6 | 97 | { |
5ce04e3d PV |
98 | resource_size_t phys_addr; |
99 | ||
4ab0d47d | 100 | BUG_ON(offset >= mapping->size); |
5ce04e3d PV |
101 | phys_addr = mapping->base + offset; |
102 | ||
d8dab00d | 103 | return ioremap_wc(phys_addr, size); |
9663f2e6 KP |
104 | } |
105 | ||
106 | static inline void | |
29bc17ec | 107 | io_mapping_unmap(void __iomem *vaddr) |
9663f2e6 | 108 | { |
e5beae16 | 109 | iounmap(vaddr); |
9663f2e6 KP |
110 | } |
111 | ||
e5beae16 | 112 | #else |
9663f2e6 | 113 | |
24dd85ff | 114 | #include <linux/uaccess.h> |
bcaaa0c4 | 115 | #include <asm/pgtable.h> |
4ab0d47d | 116 | |
e5beae16 | 117 | /* Create the io_mapping object*/ |
9663f2e6 | 118 | static inline struct io_mapping * |
cafaf14a CW |
119 | io_mapping_init_wc(struct io_mapping *iomap, |
120 | resource_size_t base, | |
121 | unsigned long size) | |
122 | { | |
123 | iomap->base = base; | |
124 | iomap->size = size; | |
125 | iomap->iomem = ioremap_wc(base, size); | |
35124389 DV |
126 | #if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */ |
127 | iomap->prot = pgprot_noncached_wc(PAGE_KERNEL); | |
128 | #elif defined(pgprot_writecombine) | |
bcaaa0c4 | 129 | iomap->prot = pgprot_writecombine(PAGE_KERNEL); |
35124389 DV |
130 | #else |
131 | iomap->prot = pgprot_noncached(PAGE_KERNEL); | |
132 | #endif | |
cafaf14a CW |
133 | |
134 | return iomap; | |
135 | } | |
136 | ||
137 | static inline void | |
138 | io_mapping_fini(struct io_mapping *mapping) | |
139 | { | |
140 | iounmap(mapping->iomem); | |
141 | } | |
142 | ||
143 | /* Non-atomic map/unmap */ | |
144 | static inline void __iomem * | |
145 | io_mapping_map_wc(struct io_mapping *mapping, | |
146 | unsigned long offset, | |
147 | unsigned long size) | |
9663f2e6 | 148 | { |
cafaf14a | 149 | return mapping->iomem + offset; |
9663f2e6 KP |
150 | } |
151 | ||
152 | static inline void | |
cafaf14a | 153 | io_mapping_unmap(void __iomem *vaddr) |
9663f2e6 KP |
154 | { |
155 | } | |
156 | ||
157 | /* Atomic map/unmap */ | |
29bc17ec | 158 | static inline void __iomem * |
fca3ec01 | 159 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
3e4d3af5 | 160 | unsigned long offset) |
9663f2e6 | 161 | { |
2cb7c9cb | 162 | preempt_disable(); |
24dd85ff | 163 | pagefault_disable(); |
cafaf14a | 164 | return io_mapping_map_wc(mapping, offset, PAGE_SIZE); |
9663f2e6 KP |
165 | } |
166 | ||
167 | static inline void | |
3e4d3af5 | 168 | io_mapping_unmap_atomic(void __iomem *vaddr) |
9663f2e6 | 169 | { |
cafaf14a | 170 | io_mapping_unmap(vaddr); |
24dd85ff | 171 | pagefault_enable(); |
2cb7c9cb | 172 | preempt_enable(); |
9663f2e6 KP |
173 | } |
174 | ||
cafaf14a CW |
175 | #endif /* HAVE_ATOMIC_IOMAP */ |
176 | ||
177 | static inline struct io_mapping * | |
178 | io_mapping_create_wc(resource_size_t base, | |
179 | unsigned long size) | |
9663f2e6 | 180 | { |
cafaf14a CW |
181 | struct io_mapping *iomap; |
182 | ||
183 | iomap = kmalloc(sizeof(*iomap), GFP_KERNEL); | |
184 | if (!iomap) | |
185 | return NULL; | |
186 | ||
187 | if (!io_mapping_init_wc(iomap, base, size)) { | |
188 | kfree(iomap); | |
189 | return NULL; | |
190 | } | |
191 | ||
192 | return iomap; | |
9663f2e6 KP |
193 | } |
194 | ||
195 | static inline void | |
cafaf14a | 196 | io_mapping_free(struct io_mapping *iomap) |
9663f2e6 | 197 | { |
cafaf14a CW |
198 | io_mapping_fini(iomap); |
199 | kfree(iomap); | |
9663f2e6 | 200 | } |
e5beae16 | 201 | |
9663f2e6 | 202 | #endif /* _LINUX_IO_MAPPING_H */ |