Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 23 | #include <linux/bug.h> |
Dan Williams | 2584cf8 | 2015-08-10 23:07:05 -0400 | [diff] [blame] | 24 | #include <linux/io.h> |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 25 | #include <asm/page.h> |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 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 | * |
Paul Bolle | 395cf96 | 2011-08-15 02:02:26 +0200 | [diff] [blame] | 31 | * See Documentation/io-mapping.txt |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 32 | */ |
| 33 | |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 34 | struct io_mapping { |
| 35 | resource_size_t base; |
| 36 | unsigned long size; |
| 37 | pgprot_t prot; |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 38 | void __iomem *iomem; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 41 | #ifdef CONFIG_HAVE_ATOMIC_IOMAP |
| 42 | |
| 43 | #include <asm/iomap.h> |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 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 | */ |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 50 | |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 51 | static inline struct io_mapping * |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 52 | io_mapping_init_wc(struct io_mapping *iomap, |
| 53 | resource_size_t base, |
| 54 | unsigned long size) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 55 | { |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 56 | pgprot_t prot; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 57 | |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 58 | if (iomap_create_wc(base, size, &prot)) |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 59 | return NULL; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 60 | |
| 61 | iomap->base = base; |
| 62 | iomap->size = size; |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 63 | iomap->prot = prot; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 64 | return iomap; |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 68 | io_mapping_fini(struct io_mapping *mapping) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 69 | { |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 70 | iomap_free(mapping->base, mapping->size); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* Atomic map/unmap */ |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 74 | static inline void __iomem * |
Chris Wilson | fca3ec0 | 2010-08-04 14:34:24 +0100 | [diff] [blame] | 75 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 76 | unsigned long offset) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 77 | { |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 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); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 84 | return iomap_atomic_prot_pfn(pfn, mapping->prot); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static inline void |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 88 | io_mapping_unmap_atomic(void __iomem *vaddr) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 89 | { |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 90 | iounmap_atomic(vaddr); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 93 | static inline void __iomem * |
Chris Wilson | d8dab00 | 2016-04-28 09:56:37 +0100 | [diff] [blame] | 94 | io_mapping_map_wc(struct io_mapping *mapping, |
| 95 | unsigned long offset, |
| 96 | unsigned long size) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 97 | { |
Pallipadi, Venkatesh | 5ce04e3 | 2009-03-01 08:53:27 -0800 | [diff] [blame] | 98 | resource_size_t phys_addr; |
| 99 | |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 100 | BUG_ON(offset >= mapping->size); |
Pallipadi, Venkatesh | 5ce04e3 | 2009-03-01 08:53:27 -0800 | [diff] [blame] | 101 | phys_addr = mapping->base + offset; |
| 102 | |
Chris Wilson | d8dab00 | 2016-04-28 09:56:37 +0100 | [diff] [blame] | 103 | return ioremap_wc(phys_addr, size); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static inline void |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 107 | io_mapping_unmap(void __iomem *vaddr) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 108 | { |
| 109 | iounmap(vaddr); |
| 110 | } |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 111 | |
| 112 | #else |
| 113 | |
Daniel Vetter | 24dd85f | 2011-09-28 11:57:23 +0200 | [diff] [blame] | 114 | #include <linux/uaccess.h> |
Chris Wilson | ac96b55 | 2016-08-23 16:50:24 +0100 | [diff] [blame] | 115 | #include <asm/pgtable.h> |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 116 | |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 117 | /* Create the io_mapping object*/ |
| 118 | static inline struct io_mapping * |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 119 | io_mapping_init_wc(struct io_mapping *iomap, |
| 120 | resource_size_t base, |
| 121 | unsigned long size) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 122 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 123 | iomap->base = base; |
| 124 | iomap->size = size; |
| 125 | iomap->iomem = ioremap_wc(base, size); |
Daniel Vetter | 80c3362 | 2016-08-23 22:15:02 +0200 | [diff] [blame] | 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) |
Chris Wilson | ac96b55 | 2016-08-23 16:50:24 +0100 | [diff] [blame] | 129 | iomap->prot = pgprot_writecombine(PAGE_KERNEL); |
Daniel Vetter | 80c3362 | 2016-08-23 22:15:02 +0200 | [diff] [blame] | 130 | #else |
| 131 | iomap->prot = pgprot_noncached(PAGE_KERNEL); |
| 132 | #endif |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 133 | |
| 134 | return iomap; |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 138 | io_mapping_fini(struct io_mapping *mapping) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 139 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 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) |
| 148 | { |
| 149 | return mapping->iomem + offset; |
| 150 | } |
| 151 | |
| 152 | static inline void |
| 153 | io_mapping_unmap(void __iomem *vaddr) |
| 154 | { |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* Atomic map/unmap */ |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 158 | static inline void __iomem * |
Chris Wilson | fca3ec0 | 2010-08-04 14:34:24 +0100 | [diff] [blame] | 159 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 160 | unsigned long offset) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 161 | { |
David Hildenbrand | 2cb7c9c | 2015-05-11 17:52:09 +0200 | [diff] [blame] | 162 | preempt_disable(); |
Daniel Vetter | 24dd85f | 2011-09-28 11:57:23 +0200 | [diff] [blame] | 163 | pagefault_disable(); |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 164 | return io_mapping_map_wc(mapping, offset, PAGE_SIZE); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static inline void |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 168 | io_mapping_unmap_atomic(void __iomem *vaddr) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 169 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 170 | io_mapping_unmap(vaddr); |
Daniel Vetter | 24dd85f | 2011-09-28 11:57:23 +0200 | [diff] [blame] | 171 | pagefault_enable(); |
David Hildenbrand | 2cb7c9c | 2015-05-11 17:52:09 +0200 | [diff] [blame] | 172 | preempt_enable(); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 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) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 180 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 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; |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 196 | io_mapping_free(struct io_mapping *iomap) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 197 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 198 | io_mapping_fini(iomap); |
| 199 | kfree(iomap); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 202 | #endif /* _LINUX_IO_MAPPING_H */ |