1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2015 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 #ifndef _LINUX_IO_H_ 32 #define _LINUX_IO_H_ 33 34 #include <machine/vm.h> 35 #include <sys/endian.h> 36 #include <sys/types.h> 37 38 #include <linux/compiler.h> 39 40 static inline uint32_t 41 __raw_readl(const volatile void *addr) 42 { 43 return *(const volatile uint32_t *)addr; 44 } 45 46 static inline void 47 __raw_writel(uint32_t b, volatile void *addr) 48 { 49 *(volatile uint32_t *)addr = b; 50 } 51 52 static inline uint64_t 53 __raw_readq(const volatile void *addr) 54 { 55 return *(const volatile uint64_t *)addr; 56 } 57 58 static inline void 59 __raw_writeq(uint64_t b, volatile void *addr) 60 { 61 *(volatile uint64_t *)addr = b; 62 } 63 64 /* 65 * XXX This is all x86 specific. It should be bus space access. 66 */ 67 #define mmiowb() barrier() 68 69 #undef writel 70 static inline void 71 writel(uint32_t b, void *addr) 72 { 73 *(volatile uint32_t *)addr = b; 74 } 75 76 #undef writeq 77 static inline void 78 writeq(uint64_t b, void *addr) 79 { 80 *(volatile uint64_t *)addr = b; 81 } 82 83 #undef writeb 84 static inline void 85 writeb(uint8_t b, void *addr) 86 { 87 *(volatile uint8_t *)addr = b; 88 } 89 90 #undef writew 91 static inline void 92 writew(uint16_t b, void *addr) 93 { 94 *(volatile uint16_t *)addr = b; 95 } 96 97 #undef ioread8 98 static inline uint8_t 99 ioread8(const volatile void *addr) 100 { 101 return *(const volatile uint8_t *)addr; 102 } 103 104 #undef ioread16 105 static inline uint16_t 106 ioread16(const volatile void *addr) 107 { 108 return *(const volatile uint16_t *)addr; 109 } 110 111 #undef ioread32 112 static inline uint32_t 113 ioread32(const volatile void *addr) 114 { 115 return *(const volatile uint32_t *)addr; 116 } 117 118 #undef ioread32be 119 static inline uint32_t 120 ioread32be(const volatile void *addr) 121 { 122 return be32toh(*(const volatile uint32_t *)addr); 123 } 124 125 #undef iowrite8 126 static inline void 127 iowrite8(uint8_t v, volatile void *addr) 128 { 129 *(volatile uint8_t *)addr = v; 130 } 131 132 #undef iowrite16 133 static inline void 134 iowrite16(uint16_t v, volatile void *addr) 135 { 136 *(volatile uint16_t *)addr = v; 137 } 138 139 #undef iowrite32 140 static inline void 141 iowrite32(uint32_t v, volatile void *addr) 142 { 143 *(volatile uint32_t *)addr = v; 144 } 145 146 #undef iowrite32be 147 static inline void 148 iowrite32be(uint32_t v, volatile void *addr) 149 { 150 *(volatile uint32_t *)addr = htobe32(v); 151 } 152 153 #undef readb 154 static inline uint8_t 155 readb(const volatile void *addr) 156 { 157 return *(const volatile uint8_t *)addr; 158 } 159 160 #undef readw 161 static inline uint16_t 162 readw(const volatile void *addr) 163 { 164 return *(const volatile uint16_t *)addr; 165 } 166 167 #undef readl 168 static inline uint32_t 169 readl(const volatile void *addr) 170 { 171 return *(const volatile uint32_t *)addr; 172 } 173 174 #if defined(__i386__) || defined(__amd64__) 175 void *_ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr); 176 #else 177 #define _ioremap_attr(...) NULL 178 #endif 179 180 #define ioremap_nocache(addr, size) \ 181 _ioremap_attr((addr), (size), VM_MEMATTR_UNCACHEABLE) 182 #define ioremap_wc(addr, size) \ 183 _ioremap_attr((addr), (size), VM_MEMATTR_WRITE_COMBINING) 184 #define ioremap_wb(addr, size) \ 185 _ioremap_attr((addr), (size), VM_MEMATTR_WRITE_BACK) 186 #define ioremap(addr, size) \ 187 _ioremap_attr((addr), (size), VM_MEMATTR_UNCACHEABLE) 188 void iounmap(void *addr); 189 190 #define memset_io(a, b, c) memset((a), (b), (c)) 191 #define memcpy_fromio(a, b, c) memcpy((a), (b), (c)) 192 #define memcpy_toio(a, b, c) memcpy((a), (b), (c)) 193 194 static inline void 195 __iowrite64_copy(void *to, void *from, size_t count) 196 { 197 #ifdef __LP64__ 198 uint64_t *src; 199 uint64_t *dst; 200 int i; 201 202 for (i = 0, src = from, dst = to; i < count; i++, src++, dst++) 203 __raw_writeq(*src, dst); 204 #else 205 uint32_t *src; 206 uint32_t *dst; 207 int i; 208 209 count *= 2; 210 for (i = 0, src = from, dst = to; i < count; i++, src++, dst++) 211 __raw_writel(*src, dst); 212 #endif 213 } 214 215 enum { 216 MEMREMAP_WB = 1 << 0, 217 MEMREMAP_WT = 1 << 1, 218 MEMREMAP_WC = 1 << 2, 219 }; 220 221 static inline void * 222 memremap(resource_size_t offset, size_t size, unsigned long flags) 223 { 224 void *addr = NULL; 225 226 if ((flags & MEMREMAP_WB) && 227 (addr = ioremap_wb(offset, size)) != NULL) 228 goto done; 229 if ((flags & MEMREMAP_WT) && 230 (addr = ioremap_nocache(offset, size)) != NULL) 231 goto done; 232 if ((flags & MEMREMAP_WC) && 233 (addr = ioremap_wc(offset, size)) != NULL) 234 goto done; 235 done: 236 return (addr); 237 } 238 239 static inline void 240 memunmap(void *addr) 241 { 242 /* XXX May need to check if this is RAM */ 243 iounmap(addr); 244 } 245 246 #endif /* _LINUX_IO_H_ */ 247