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> |
Mike Rapoport | 65fddcf | 2020-06-08 21:32:42 -0700 | [diff] [blame] | 13 | #include <linux/pgtable.h> |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 14 | #include <asm/page.h> |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 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 | * |
Mauro Carvalho Chehab | 7d3d325 | 2020-03-11 12:51:17 +0100 | [diff] [blame] | 20 | * See Documentation/driver-api/io-mapping.rst |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 23 | struct io_mapping { |
| 24 | resource_size_t base; |
| 25 | unsigned long size; |
| 26 | pgprot_t prot; |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 27 | void __iomem *iomem; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 28 | }; |
| 29 | |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 30 | #ifdef CONFIG_HAVE_ATOMIC_IOMAP |
| 31 | |
Andy Shevchenko | 2b75562 | 2020-01-30 22:17:32 -0800 | [diff] [blame] | 32 | #include <linux/pfn.h> |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 33 | #include <asm/iomap.h> |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 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 | */ |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 40 | |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 41 | static inline struct io_mapping * |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 42 | io_mapping_init_wc(struct io_mapping *iomap, |
| 43 | resource_size_t base, |
| 44 | unsigned long size) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 45 | { |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 46 | pgprot_t prot; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 47 | |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 48 | if (iomap_create_wc(base, size, &prot)) |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 49 | return NULL; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 50 | |
| 51 | iomap->base = base; |
| 52 | iomap->size = size; |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 53 | iomap->prot = prot; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 54 | return iomap; |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 58 | io_mapping_fini(struct io_mapping *mapping) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 59 | { |
Venkatesh Pallipadi | 9e36fda0 | 2009-07-10 09:57:35 -0700 | [diff] [blame] | 60 | iomap_free(mapping->base, mapping->size); |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* Atomic map/unmap */ |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 64 | static inline void __iomem * |
Chris Wilson | fca3ec0 | 2010-08-04 14:34:24 +0100 | [diff] [blame] | 65 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 66 | unsigned long offset) |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 67 | { |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 68 | resource_size_t phys_addr; |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 69 | |
| 70 | BUG_ON(offset >= mapping->size); |
| 71 | phys_addr = mapping->base + offset; |
Andy Shevchenko | 2b75562 | 2020-01-30 22:17:32 -0800 | [diff] [blame] | 72 | return iomap_atomic_prot_pfn(PHYS_PFN(phys_addr), 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> |
Venkatesh Pallipadi | 4ab0d47 | 2009-02-24 17:35:12 -0800 | [diff] [blame] | 103 | |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 104 | /* Create the io_mapping object*/ |
| 105 | static inline struct io_mapping * |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 106 | io_mapping_init_wc(struct io_mapping *iomap, |
| 107 | resource_size_t base, |
| 108 | unsigned long size) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 109 | { |
Michael J. Ruhl | e0b3e0b | 2020-07-23 21:15:46 -0700 | [diff] [blame] | 110 | iomap->iomem = ioremap_wc(base, size); |
| 111 | if (!iomap->iomem) |
| 112 | return NULL; |
| 113 | |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 114 | iomap->base = base; |
| 115 | iomap->size = size; |
Daniel Vetter | 3512438 | 2016-08-23 22:15:02 +0200 | [diff] [blame] | 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) |
Chris Wilson | bcaaa0c | 2016-08-23 16:50:24 +0100 | [diff] [blame] | 119 | iomap->prot = pgprot_writecombine(PAGE_KERNEL); |
Daniel Vetter | 3512438 | 2016-08-23 22:15:02 +0200 | [diff] [blame] | 120 | #else |
| 121 | iomap->prot = pgprot_noncached(PAGE_KERNEL); |
| 122 | #endif |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 123 | |
| 124 | return iomap; |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 128 | io_mapping_fini(struct io_mapping *mapping) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 129 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 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) |
| 138 | { |
| 139 | return mapping->iomem + offset; |
| 140 | } |
| 141 | |
| 142 | static inline void |
| 143 | io_mapping_unmap(void __iomem *vaddr) |
| 144 | { |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* Atomic map/unmap */ |
Francisco Jerez | 29bc17e | 2010-09-04 22:56:44 +0200 | [diff] [blame] | 148 | static inline void __iomem * |
Chris Wilson | fca3ec0 | 2010-08-04 14:34:24 +0100 | [diff] [blame] | 149 | io_mapping_map_atomic_wc(struct io_mapping *mapping, |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 150 | unsigned long offset) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 151 | { |
David Hildenbrand | 2cb7c9c | 2015-05-11 17:52:09 +0200 | [diff] [blame] | 152 | preempt_disable(); |
Daniel Vetter | 24dd85f | 2011-09-28 11:57:23 +0200 | [diff] [blame] | 153 | pagefault_disable(); |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 154 | return io_mapping_map_wc(mapping, offset, PAGE_SIZE); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static inline void |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 158 | io_mapping_unmap_atomic(void __iomem *vaddr) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 159 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 160 | io_mapping_unmap(vaddr); |
Daniel Vetter | 24dd85f | 2011-09-28 11:57:23 +0200 | [diff] [blame] | 161 | pagefault_enable(); |
David Hildenbrand | 2cb7c9c | 2015-05-11 17:52:09 +0200 | [diff] [blame] | 162 | preempt_enable(); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 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) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 170 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 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; |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static inline void |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 186 | io_mapping_free(struct io_mapping *iomap) |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 187 | { |
Chris Wilson | cafaf14 | 2016-08-19 16:54:26 +0100 | [diff] [blame] | 188 | io_mapping_fini(iomap); |
| 189 | kfree(iomap); |
Keith Packard | e5beae1 | 2008-11-03 18:21:45 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Keith Packard | 9663f2e | 2008-10-30 19:38:18 -0700 | [diff] [blame] | 192 | #endif /* _LINUX_IO_MAPPING_H */ |