Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _ASM_IO_H |
| 2 | #define _ASM_IO_H |
| 3 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | /* |
| 6 | * This file contains the definitions for the x86 IO instructions |
| 7 | * inb/inw/inl/outb/outw/outl and the "string versions" of the same |
| 8 | * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing" |
| 9 | * versions of the single-IO instructions (inb_p/inw_p/..). |
| 10 | * |
| 11 | * This file is not meant to be obfuscating: it's just complicated |
| 12 | * to (a) handle it all in a way that makes gcc able to optimize it |
| 13 | * as well as possible and (b) trying to avoid writing the same thing |
| 14 | * over and over again with slight variations and possibly making a |
| 15 | * mistake somewhere. |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * Thanks to James van Artsdalen for a better timing-fix than |
| 20 | * the two short jumps: using outb's to a nonexistent port seems |
| 21 | * to guarantee better timings even on fast machines. |
| 22 | * |
| 23 | * On the other hand, I'd like to be sure of a non-existent port: |
| 24 | * I feel a bit unsafe about using 0x80 (should be safe, though) |
| 25 | * |
| 26 | * Linus |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * Bit simplified and optimized by Jan Hubicka |
| 31 | * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999. |
| 32 | * |
| 33 | * isa_memset_io, isa_memcpy_fromio, isa_memcpy_toio added, |
| 34 | * isa_read[wl] and isa_write[wl] fixed |
| 35 | * - Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 36 | */ |
| 37 | |
Rene Herman | b02aae9 | 2008-01-30 13:30:05 +0100 | [diff] [blame] | 38 | extern void native_io_delay(void); |
| 39 | |
Ingo Molnar | 6e7c402 | 2008-01-30 13:30:05 +0100 | [diff] [blame] | 40 | extern int io_delay_type; |
| 41 | extern void io_delay_init(void); |
| 42 | |
Glauber de Oliveira Costa | ba08242 | 2008-01-30 13:31:10 +0100 | [diff] [blame] | 43 | #if defined(CONFIG_PARAVIRT) |
| 44 | #include <asm/paravirt.h> |
| 45 | #else |
| 46 | |
Rene Herman | b02aae9 | 2008-01-30 13:30:05 +0100 | [diff] [blame] | 47 | static inline void slow_down_io(void) |
| 48 | { |
| 49 | native_io_delay(); |
| 50 | #ifdef REALLY_SLOW_IO |
| 51 | native_io_delay(); |
| 52 | native_io_delay(); |
| 53 | native_io_delay(); |
| 54 | #endif |
| 55 | } |
Glauber de Oliveira Costa | ba08242 | 2008-01-30 13:31:10 +0100 | [diff] [blame] | 56 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Talk about misusing macros.. |
| 60 | */ |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 61 | #define __OUT1(s, x) \ |
Adrian Bunk | 9c0aa0f | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 62 | static inline void out##s(unsigned x value, unsigned short port) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 64 | #define __OUT2(s, s1, s2) \ |
| 65 | asm volatile ("out" #s " %" s1 "0,%" s2 "1" |
Glauber de Oliveira Costa | ba08242 | 2008-01-30 13:31:10 +0100 | [diff] [blame] | 66 | |
| 67 | #ifndef REALLY_SLOW_IO |
| 68 | #define REALLY_SLOW_IO |
| 69 | #define UNSET_REALLY_SLOW_IO |
| 70 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 72 | #define __OUT(s, s1, x) \ |
| 73 | __OUT1(s, x) __OUT2(s, s1, "w") : : "a" (value), "Nd" (port)); \ |
| 74 | } \ |
| 75 | __OUT1(s##_p, x) __OUT2(s, s1, "w") : : "a" (value), "Nd" (port)); \ |
| 76 | slow_down_io(); \ |
| 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 79 | #define __IN1(s) \ |
| 80 | static inline RETURN_TYPE in##s(unsigned short port) \ |
| 81 | { \ |
| 82 | RETURN_TYPE _v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 84 | #define __IN2(s, s1, s2) \ |
| 85 | asm volatile ("in" #s " %" s2 "1,%" s1 "0" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 87 | #define __IN(s, s1, i...) \ |
| 88 | __IN1(s) __IN2(s, s1, "w") : "=a" (_v) : "Nd" (port), ##i); \ |
| 89 | return _v; \ |
| 90 | } \ |
| 91 | __IN1(s##_p) __IN2(s, s1, "w") : "=a" (_v) : "Nd" (port), ##i); \ |
| 92 | slow_down_io(); \ |
| 93 | return _v; } |
Glauber de Oliveira Costa | ba08242 | 2008-01-30 13:31:10 +0100 | [diff] [blame] | 94 | |
| 95 | #ifdef UNSET_REALLY_SLOW_IO |
| 96 | #undef REALLY_SLOW_IO |
| 97 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 99 | #define __INS(s) \ |
| 100 | static inline void ins##s(unsigned short port, void *addr, \ |
| 101 | unsigned long count) \ |
| 102 | { \ |
| 103 | asm volatile ("rep ; ins" #s \ |
| 104 | : "=D" (addr), "=c" (count) \ |
| 105 | : "d" (port), "0" (addr), "1" (count)); \ |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 108 | #define __OUTS(s) \ |
| 109 | static inline void outs##s(unsigned short port, const void *addr, \ |
| 110 | unsigned long count) \ |
| 111 | { \ |
| 112 | asm volatile ("rep ; outs" #s \ |
| 113 | : "=S" (addr), "=c" (count) \ |
| 114 | : "d" (port), "0" (addr), "1" (count)); \ |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | #define RETURN_TYPE unsigned char |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 118 | __IN(b, "") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | #undef RETURN_TYPE |
| 120 | #define RETURN_TYPE unsigned short |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 121 | __IN(w, "") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #undef RETURN_TYPE |
| 123 | #define RETURN_TYPE unsigned int |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 124 | __IN(l, "") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | #undef RETURN_TYPE |
| 126 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 127 | __OUT(b, "b", char) |
| 128 | __OUT(w, "w", short) |
| 129 | __OUT(l, , int) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | __INS(b) |
| 132 | __INS(w) |
| 133 | __INS(l) |
| 134 | |
| 135 | __OUTS(b) |
| 136 | __OUTS(w) |
| 137 | __OUTS(l) |
| 138 | |
| 139 | #define IO_SPACE_LIMIT 0xffff |
| 140 | |
Josef 'Jeff' Sipek | b0957f1 | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 141 | #if defined(__KERNEL__) && defined(__x86_64__) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | #include <linux/vmalloc.h> |
| 144 | |
| 145 | #ifndef __i386__ |
| 146 | /* |
| 147 | * Change virtual addresses to physical addresses and vv. |
| 148 | * These are pretty trivial |
| 149 | */ |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 150 | static inline unsigned long virt_to_phys(volatile void *address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | return __pa(address); |
| 153 | } |
| 154 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 155 | static inline void *phys_to_virt(unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | return __va(address); |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | /* |
| 162 | * Change "struct page" to physical address. |
| 163 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | #define page_to_phys(page) ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | #include <asm-generic/iomap.h> |
| 167 | |
Andi Kleen | f2d3efe | 2006-03-25 16:30:22 +0100 | [diff] [blame] | 168 | extern void *early_ioremap(unsigned long addr, unsigned long size); |
| 169 | extern void early_iounmap(void *addr, unsigned long size); |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* |
| 172 | * This one maps high address device memory and turns off caching for that area. |
| 173 | * it's useful if some control registers are in such an area and write combining |
| 174 | * or read caching is not desirable: |
| 175 | */ |
Linus Torvalds | b9e76a0 | 2008-03-24 11:22:39 -0700 | [diff] [blame] | 176 | extern void __iomem *ioremap_nocache(resource_size_t offset, unsigned long size); |
| 177 | extern void __iomem *ioremap_cache(resource_size_t offset, unsigned long size); |
Ingo Molnar | 6371b49 | 2008-01-30 13:33:40 +0100 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * The default ioremap() behavior is non-cached: |
| 181 | */ |
Linus Torvalds | b9e76a0 | 2008-03-24 11:22:39 -0700 | [diff] [blame] | 182 | static inline void __iomem *ioremap(resource_size_t offset, unsigned long size) |
Ingo Molnar | 6371b49 | 2008-01-30 13:33:40 +0100 | [diff] [blame] | 183 | { |
Ingo Molnar | 9af993a | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 184 | return ioremap_nocache(offset, size); |
Ingo Molnar | 6371b49 | 2008-01-30 13:33:40 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | extern void iounmap(volatile void __iomem *addr); |
Ingo Molnar | 6371b49 | 2008-01-30 13:33:40 +0100 | [diff] [blame] | 188 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 189 | extern void __iomem *fix_ioremap(unsigned idx, unsigned long phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /* |
| 192 | * ISA I/O bus memory addresses are 1:1 with the physical address. |
| 193 | */ |
| 194 | #define isa_virt_to_bus virt_to_phys |
| 195 | #define isa_page_to_bus page_to_phys |
| 196 | #define isa_bus_to_virt phys_to_virt |
| 197 | |
| 198 | /* |
| 199 | * However PCI ones are not necessarily 1:1 and therefore these interfaces |
| 200 | * are forbidden in portable PCI drivers. |
| 201 | * |
| 202 | * Allow them on x86 for legacy drivers, though. |
| 203 | */ |
| 204 | #define virt_to_bus virt_to_phys |
| 205 | #define bus_to_virt phys_to_virt |
| 206 | |
| 207 | /* |
| 208 | * readX/writeX() are used to access memory mapped devices. On some |
| 209 | * architectures the memory mapped IO stuff needs to be accessed |
| 210 | * differently. On the x86 architecture, we just read/write the |
| 211 | * memory location directly. |
| 212 | */ |
| 213 | |
| 214 | static inline __u8 __readb(const volatile void __iomem *addr) |
| 215 | { |
| 216 | return *(__force volatile __u8 *)addr; |
| 217 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | static inline __u16 __readw(const volatile void __iomem *addr) |
| 220 | { |
| 221 | return *(__force volatile __u16 *)addr; |
| 222 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 223 | |
mao, bibo | cde227a | 2006-04-11 12:54:54 +0200 | [diff] [blame] | 224 | static __always_inline __u32 __readl(const volatile void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | return *(__force volatile __u32 *)addr; |
| 227 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | static inline __u64 __readq(const volatile void __iomem *addr) |
| 230 | { |
| 231 | return *(__force volatile __u64 *)addr; |
| 232 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | #define readb(x) __readb(x) |
| 235 | #define readw(x) __readw(x) |
| 236 | #define readl(x) __readl(x) |
| 237 | #define readq(x) __readq(x) |
| 238 | #define readb_relaxed(a) readb(a) |
| 239 | #define readw_relaxed(a) readw(a) |
| 240 | #define readl_relaxed(a) readl(a) |
| 241 | #define readq_relaxed(a) readq(a) |
| 242 | #define __raw_readb readb |
| 243 | #define __raw_readw readw |
| 244 | #define __raw_readl readl |
| 245 | #define __raw_readq readq |
| 246 | |
| 247 | #define mmiowb() |
| 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | static inline void __writel(__u32 b, volatile void __iomem *addr) |
| 250 | { |
| 251 | *(__force volatile __u32 *)addr = b; |
| 252 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | static inline void __writeq(__u64 b, volatile void __iomem *addr) |
| 255 | { |
| 256 | *(__force volatile __u64 *)addr = b; |
| 257 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | static inline void __writeb(__u8 b, volatile void __iomem *addr) |
| 260 | { |
| 261 | *(__force volatile __u8 *)addr = b; |
| 262 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | static inline void __writew(__u16 b, volatile void __iomem *addr) |
| 265 | { |
| 266 | *(__force volatile __u16 *)addr = b; |
| 267 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 268 | |
| 269 | #define writeq(val, addr) __writeq((val), (addr)) |
| 270 | #define writel(val, addr) __writel((val), (addr)) |
| 271 | #define writew(val, addr) __writew((val), (addr)) |
| 272 | #define writeb(val, addr) __writeb((val), (addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | #define __raw_writeb writeb |
| 274 | #define __raw_writew writew |
| 275 | #define __raw_writel writel |
| 276 | #define __raw_writeq writeq |
| 277 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 278 | void __memcpy_fromio(void *, unsigned long, unsigned); |
| 279 | void __memcpy_toio(unsigned long, const void *, unsigned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 281 | static inline void memcpy_fromio(void *to, const volatile void __iomem *from, |
| 282 | unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 284 | __memcpy_fromio(to, (unsigned long)from, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 286 | |
| 287 | static inline void memcpy_toio(volatile void __iomem *to, const void *from, |
| 288 | unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 290 | __memcpy_toio((unsigned long)to, from, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void memset_io(volatile void __iomem *a, int b, size_t c); |
| 294 | |
| 295 | /* |
| 296 | * ISA space is 'always mapped' on a typical x86 system, no need to |
| 297 | * explicitly ioremap() it. The fact that the ISA IO space is mapped |
| 298 | * to PAGE_OFFSET is pure coincidence - it does not mean ISA values |
| 299 | * are physical addresses. The following constant pointer can be |
| 300 | * used as the IO-area pointer (it can be iounmapped as well, so the |
| 301 | * analogy with PCI is quite large): |
| 302 | */ |
| 303 | #define __ISA_IO_base ((char __iomem *)(PAGE_OFFSET)) |
| 304 | |
Joe Perches | 126f5f3 | 2008-03-23 01:02:23 -0700 | [diff] [blame] | 305 | #define flush_write_buffers() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | extern int iommu_bio_merge; |
| 308 | #define BIO_VMERGE_BOUNDARY iommu_bio_merge |
| 309 | |
| 310 | /* |
| 311 | * Convert a physical pointer to a virtual kernel pointer for /dev/mem |
| 312 | * access |
| 313 | */ |
| 314 | #define xlate_dev_mem_ptr(p) __va(p) |
| 315 | |
| 316 | /* |
| 317 | * Convert a virtual cached pointer to an uncached pointer |
| 318 | */ |
| 319 | #define xlate_dev_kmem_ptr(p) p |
| 320 | |
| 321 | #endif /* __KERNEL__ */ |
| 322 | |
| 323 | #endif |