1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright © 2008 Keith Packard <keithp@keithp.com> 4 */ 5 6 #ifndef _LINUX_IO_MAPPING_H 7 #define _LINUX_IO_MAPPING_H 8 9 #include <linux/types.h> 10 #include <linux/slab.h> 11 #include <linux/bug.h> 12 #include <linux/io.h> 13 #include <linux/pgtable.h> 14 #include <asm/page.h> 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 * 20 * See Documentation/driver-api/io-mapping.rst 21 */ 22 23 struct io_mapping { 24 resource_size_t base; 25 unsigned long size; 26 pgprot_t prot; 27 void __iomem *iomem; 28 }; 29 30 #ifdef CONFIG_HAVE_ATOMIC_IOMAP 31 32 #include <linux/pfn.h> 33 #include <asm/iomap.h> 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 */ 40 41 static inline struct io_mapping * 42 io_mapping_init_wc(struct io_mapping *iomap, 43 resource_size_t base, 44 unsigned long size) 45 { 46 pgprot_t prot; 47 48 if (iomap_create_wc(base, size, &prot)) 49 return NULL; 50 51 iomap->base = base; 52 iomap->size = size; 53 iomap->prot = prot; 54 return iomap; 55 } 56 57 static inline void 58 io_mapping_fini(struct io_mapping *mapping) 59 { 60 iomap_free(mapping->base, mapping->size); 61 } 62 63 /* Atomic map/unmap */ 64 static inline void __iomem * 65 io_mapping_map_atomic_wc(struct io_mapping *mapping, 66 unsigned long offset) 67 { 68 resource_size_t phys_addr; 69 70 BUG_ON(offset >= mapping->size); 71 phys_addr = mapping->base + offset; 72 return iomap_atomic_prot_pfn(PHYS_PFN(phys_addr), mapping->prot); 73 } 74 75 static inline void 76 io_mapping_unmap_atomic(void __iomem *vaddr) 77 { 78 iounmap_atomic(vaddr); 79 } 80 81 static inline void __iomem * 82 io_mapping_map_wc(struct io_mapping *mapping, 83 unsigned long offset, 84 unsigned long size) 85 { 86 resource_size_t phys_addr; 87 88 BUG_ON(offset >= mapping->size); 89 phys_addr = mapping->base + offset; 90 91 return ioremap_wc(phys_addr, size); 92 } 93 94 static inline void 95 io_mapping_unmap(void __iomem *vaddr) 96 { 97 iounmap(vaddr); 98 } 99 100 #else 101 102 #include <linux/uaccess.h> 103 104 /* Create the io_mapping object*/ 105 static inline struct io_mapping * 106 io_mapping_init_wc(struct io_mapping *iomap, 107 resource_size_t base, 108 unsigned long size) 109 { 110 iomap->base = base; 111 iomap->size = size; 112 iomap->iomem = ioremap_wc(base, size); 113 #if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */ 114 iomap->prot = pgprot_noncached_wc(PAGE_KERNEL); 115 #elif defined(pgprot_writecombine) 116 iomap->prot = pgprot_writecombine(PAGE_KERNEL); 117 #else 118 iomap->prot = pgprot_noncached(PAGE_KERNEL); 119 #endif 120 121 return iomap; 122 } 123 124 static inline void 125 io_mapping_fini(struct io_mapping *mapping) 126 { 127 iounmap(mapping->iomem); 128 } 129 130 /* Non-atomic map/unmap */ 131 static inline void __iomem * 132 io_mapping_map_wc(struct io_mapping *mapping, 133 unsigned long offset, 134 unsigned long size) 135 { 136 return mapping->iomem + offset; 137 } 138 139 static inline void 140 io_mapping_unmap(void __iomem *vaddr) 141 { 142 } 143 144 /* Atomic map/unmap */ 145 static inline void __iomem * 146 io_mapping_map_atomic_wc(struct io_mapping *mapping, 147 unsigned long offset) 148 { 149 preempt_disable(); 150 pagefault_disable(); 151 return io_mapping_map_wc(mapping, offset, PAGE_SIZE); 152 } 153 154 static inline void 155 io_mapping_unmap_atomic(void __iomem *vaddr) 156 { 157 io_mapping_unmap(vaddr); 158 pagefault_enable(); 159 preempt_enable(); 160 } 161 162 #endif /* HAVE_ATOMIC_IOMAP */ 163 164 static inline struct io_mapping * 165 io_mapping_create_wc(resource_size_t base, 166 unsigned long size) 167 { 168 struct io_mapping *iomap; 169 170 iomap = kmalloc(sizeof(*iomap), GFP_KERNEL); 171 if (!iomap) 172 return NULL; 173 174 if (!io_mapping_init_wc(iomap, base, size)) { 175 kfree(iomap); 176 return NULL; 177 } 178 179 return iomap; 180 } 181 182 static inline void 183 io_mapping_free(struct io_mapping *iomap) 184 { 185 io_mapping_fini(iomap); 186 kfree(iomap); 187 } 188 189 #endif /* _LINUX_IO_MAPPING_H */ 190