Thomas Gleixner | 775c8a3 | 2019-06-04 10:11:37 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright © 2008 Keith Packard <keithp@keithp.com> |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef _LINUX_IO_MAPPING_H |
| 7 | #define _LINUX_IO_MAPPING_H |
| 8 | |
| 9 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 11 | #include <linux/bug.h> |
Dan Williams | 2584cf8 | 2015-08-10 23:07:05 -0400 | [diff] [blame] | 12 | #include <linux/io.h> |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 13 | #include <asm/page.h> |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * The io_mapping mechanism provides an abstraction for mapping |
| 17 | * individual pages from an io device to the CPU in an efficient fashion. |
| 18 | * |
Paul Bolle | 395cf96 | 2011-08-15 02:02:26 +0200 | [diff] [blame] | 19 | * See Documentation/io-mapping.txt |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 22 | struct io_mapping { |
| 23 | resource_size_t base; |
| 24 | unsigned long size; |
| 25 | pgprot_t prot; |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 26 | void __iomem *iomem; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 29 | #ifdef CONFIG_HAVE_ATOMIC_IOMAP |
| 30 | |
| 31 | #include <asm/iomap.h> |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 32 | /* |
| 33 | * For small address space machines, mapping large objects |
| 34 | * into the kernel virtual space isn't practical. Where |
| 35 | * available, use fixmap support to dynamically map pages |
| 36 | * of the object at run time. |
| 37 | */ |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 38 | |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 39 | static inline struct io_mapping * |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 40 | io_mapping_init_wc(struct io_mapping *iomap, |
| 41 | resource_size_t base, |
| 42 | unsigned long size) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 43 | { |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 44 | pgprot_t prot; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 45 | |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 46 | if (iomap_create_wc(base, size, &prot)) |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 47 | return NULL; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 48 | |
| 49 | iomap->base = base; |
| 50 | iomap->size = size; |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 51 | iomap->prot = prot; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 52 | return iomap; |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 56 | io_mapping_fini(struct io_mapping *mapping) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 57 | { |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 58 | iomap_free(mapping->base, mapping->size); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* Atomic map/unmap */ |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 62 | static inline void __iomem * |
Chris Wilson | fca3ec0 | 2010-08-04 14:34:24 +0100 | [diff] [blame] | 63 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 64 | unsigned long offset) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 65 | { |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 66 | resource_size_t phys_addr; |
| 67 | unsigned long pfn; |
| 68 | |
| 69 | BUG_ON(offset >= mapping->size); |
| 70 | phys_addr = mapping->base + offset; |
| 71 | pfn = (unsigned long) (phys_addr >> PAGE_SHIFT); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 72 | return iomap_atomic_prot_pfn(pfn, mapping->prot); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static inline void |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 76 | io_mapping_unmap_atomic(void __iomem *vaddr) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 77 | { |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 78 | iounmap_atomic(vaddr); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 81 | static inline void __iomem * |
Chris Wilson | d8dab00 | 2016-04-28 09:56:37 +0100 | [diff] [blame] | 82 | io_mapping_map_wc(struct io_mapping *mapping, |
| 83 | unsigned long offset, |
| 84 | unsigned long size) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 85 | { |
Pallipadi, Venkatesh | 5ce04e3 | 2009-03-01 08:53:27 -0800 | [diff] [blame] | 86 | resource_size_t phys_addr; |
| 87 | |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 88 | BUG_ON(offset >= mapping->size); |
Pallipadi, Venkatesh | 5ce04e3 | 2009-03-01 08:53:27 -0800 | [diff] [blame] | 89 | phys_addr = mapping->base + offset; |
| 90 | |
Chris Wilson | d8dab00 | 2016-04-28 09:56:37 +0100 | [diff] [blame] | 91 | return ioremap_wc(phys_addr, size); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static inline void |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 95 | io_mapping_unmap(void __iomem *vaddr) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 96 | { |
| 97 | iounmap(vaddr); |
| 98 | } |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 99 | |
| 100 | #else |
| 101 | |
Daniel Vetter | 24dd85f | 2011-09-28 11:57:23 +0200 | [diff] [blame] | 102 | #include <linux/uaccess.h> |
Chris Wilson | bcaaa0c | 2016-08-23 16:50:24 +0100 | [diff] [blame] | 103 | #include <asm/pgtable.h> |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 104 | |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 105 | /* Create the io_mapping object*/ |
| 106 | static inline struct io_mapping * |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 107 | io_mapping_init_wc(struct io_mapping *iomap, |
| 108 | resource_size_t base, |
| 109 | unsigned long size) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 110 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 111 | iomap->base = base; |
| 112 | iomap->size = size; |
| 113 | iomap->iomem = ioremap_wc(base, size); |
Daniel Vetter | 3512438 | 2016-08-23 22:15:02 +0200 | [diff] [blame] | 114 | #if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */ |
| 115 | iomap->prot = pgprot_noncached_wc(PAGE_KERNEL); |
| 116 | #elif defined(pgprot_writecombine) |
Chris Wilson | bcaaa0c | 2016-08-23 16:50:24 +0100 | [diff] [blame] | 117 | iomap->prot = pgprot_writecombine(PAGE_KERNEL); |
Daniel Vetter | 3512438 | 2016-08-23 22:15:02 +0200 | [diff] [blame] | 118 | #else |
| 119 | iomap->prot = pgprot_noncached(PAGE_KERNEL); |
| 120 | #endif |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 121 | |
| 122 | return iomap; |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 126 | io_mapping_fini(struct io_mapping *mapping) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 127 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 128 | iounmap(mapping->iomem); |
| 129 | } |
| 130 | |
| 131 | /* Non-atomic map/unmap */ |
| 132 | static inline void __iomem * |
| 133 | io_mapping_map_wc(struct io_mapping *mapping, |
| 134 | unsigned long offset, |
| 135 | unsigned long size) |
| 136 | { |
| 137 | return mapping->iomem + offset; |
| 138 | } |
| 139 | |
| 140 | static inline void |
| 141 | io_mapping_unmap(void __iomem *vaddr) |
| 142 | { |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Atomic map/unmap */ |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 146 | static inline void __iomem * |
Chris Wilson | fca3ec0 | 2010-08-04 14:34:24 +0100 | [diff] [blame] | 147 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 148 | unsigned long offset) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 149 | { |
David Hildenbrand | 2cb7c9c | 2015-05-11 17:52:09 +0200 | [diff] [blame] | 150 | preempt_disable(); |
Daniel Vetter | 24dd85f | 2011-09-28 11:57:23 +0200 | [diff] [blame] | 151 | pagefault_disable(); |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 152 | return io_mapping_map_wc(mapping, offset, PAGE_SIZE); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static inline void |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 156 | io_mapping_unmap_atomic(void __iomem *vaddr) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 157 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 158 | io_mapping_unmap(vaddr); |
Daniel Vetter | 24dd85f | 2011-09-28 11:57:23 +0200 | [diff] [blame] | 159 | pagefault_enable(); |
David Hildenbrand | 2cb7c9c | 2015-05-11 17:52:09 +0200 | [diff] [blame] | 160 | preempt_enable(); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 163 | #endif /* HAVE_ATOMIC_IOMAP */ |
| 164 | |
| 165 | static inline struct io_mapping * |
| 166 | io_mapping_create_wc(resource_size_t base, |
| 167 | unsigned long size) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 168 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 169 | struct io_mapping *iomap; |
| 170 | |
| 171 | iomap = kmalloc(sizeof(*iomap), GFP_KERNEL); |
| 172 | if (!iomap) |
| 173 | return NULL; |
| 174 | |
| 175 | if (!io_mapping_init_wc(iomap, base, size)) { |
| 176 | kfree(iomap); |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | return iomap; |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 184 | io_mapping_free(struct io_mapping *iomap) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 185 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 186 | io_mapping_fini(iomap); |
| 187 | kfree(iomap); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 190 | #endif /* _LINUX_IO_MAPPING_H */ |