5 * This file contains the definitions for the x86 IO instructions
6 * inb/inw/inl/outb/outw/outl and the "string versions" of the same
7 * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
8 * versions of the single-IO instructions (inb_p/inw_p/..).
10 * This file is not meant to be obfuscating: it's just complicated
11 * to (a) handle it all in a way that makes gcc able to optimize it
12 * as well as possible and (b) trying to avoid writing the same thing
13 * over and over again with slight variations and possibly making a
18 * Thanks to James van Artsdalen for a better timing-fix than
19 * the two short jumps: using outb's to a nonexistent port seems
20 * to guarantee better timings even on fast machines.
22 * On the other hand, I'd like to be sure of a non-existent port:
23 * I feel a bit unsafe about using 0x80 (should be safe, though)
28 #ifdef SLOW_IO_BY_JUMPING
29 #define __SLOW_DOWN_IO __asm__ __volatile__("jmp 1f\n1:\tjmp 1f\n1:")
31 #define __SLOW_DOWN_IO __asm__ __volatile__("outb %al,$0x80")
35 #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
37 #define SLOW_DOWN_IO __SLOW_DOWN_IO
41 * readX/writeX() are used to access memory mapped devices. On some
42 * architectures the memory mapped IO stuff needs to be accessed
43 * differently. On the x86 architecture, we just read/write the
44 * memory location directly.
46 #define readb(addr) (*(volatile unsigned char *) (addr))
47 #define readw(addr) (*(volatile unsigned short *) (addr))
48 #define readl(addr) (*(volatile unsigned int *) (addr))
50 #define writeb(b,addr) ((*(volatile unsigned char *) (addr)) = (b))
51 #define writew(b,addr) ((*(volatile unsigned short *) (addr)) = (b))
52 #define writel(b,addr) ((*(volatile unsigned int *) (addr)) = (b))
54 #define memset_io(a,b,c) memset((void *)(a),(b),(c))
55 #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
56 #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
59 * Again, i386 does not require mem IO specific function.
62 #define eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(void *)(b),(c),(d))
65 * Talk about misusing macros..
69 extern void __out##s(unsigned x value, unsigned short port); \
70 extern inline void __out##s(unsigned x value, unsigned short port) {
72 #define __OUT2(s,s1,s2) \
73 __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
75 #define __OUT(s,s1,x) \
76 __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); } \
77 __OUT1(s##c,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); } \
78 __OUT1(s##_p,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); SLOW_DOWN_IO; } \
79 __OUT1(s##c_p,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); SLOW_DOWN_IO; }
82 extern unsigned x __in##s(unsigned short port); \
83 extern inline unsigned x __in##s(unsigned short port) { unsigned x _v;
85 #define __IN2(s,s1,s2) \
86 __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
88 #define __IN(s,s1,x,i...) \
89 __IN1(s,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); return _v; } \
90 __IN1(s##c,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); return _v; } \
91 __IN1(s##_p,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); SLOW_DOWN_IO; return _v; } \
92 __IN1(s##c_p,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); SLOW_DOWN_IO; return _v; }
95 extern void ins##s(unsigned short port, void * addr, unsigned long count); \
96 extern inline void ins##s(unsigned short port, void * addr, unsigned long count) \
97 { __asm__ __volatile__ ("cld ; rep ; ins" #s \
98 : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
101 extern void outs##s(unsigned short port, const void * addr, unsigned long count); \
102 extern inline void outs##s(unsigned short port, const void * addr, unsigned long count) \
103 { __asm__ __volatile__ ("cld ; rep ; outs" #s \
104 : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
123 * Note that due to the way __builtin_constant_p() works, you
124 * - can't use it inside a inline function (it will never be true)
125 * - you don't have to worry about side effects within the __builtin..
127 #define outb(val,port) \
128 ((__builtin_constant_p((port)) && (port) < 256) ? \
129 __outbc((val),(port)) : \
130 __outb((val),(port)))
133 ((__builtin_constant_p((port)) && (port) < 256) ? \
137 #define outb_p(val,port) \
138 ((__builtin_constant_p((port)) && (port) < 256) ? \
139 __outbc_p((val),(port)) : \
140 __outb_p((val),(port)))
142 #define inb_p(port) \
143 ((__builtin_constant_p((port)) && (port) < 256) ? \
147 #define outw(val,port) \
148 ((__builtin_constant_p((port)) && (port) < 256) ? \
149 __outwc((val),(port)) : \
150 __outw((val),(port)))
153 ((__builtin_constant_p((port)) && (port) < 256) ? \
157 #define outw_p(val,port) \
158 ((__builtin_constant_p((port)) && (port) < 256) ? \
159 __outwc_p((val),(port)) : \
160 __outw_p((val),(port)))
162 #define inw_p(port) \
163 ((__builtin_constant_p((port)) && (port) < 256) ? \
167 #define outl(val,port) \
168 ((__builtin_constant_p((port)) && (port) < 256) ? \
169 __outlc((val),(port)) : \
170 __outl((val),(port)))
173 ((__builtin_constant_p((port)) && (port) < 256) ? \
177 #define outl_p(val,port) \
178 ((__builtin_constant_p((port)) && (port) < 256) ? \
179 __outlc_p((val),(port)) : \
180 __outl_p((val),(port)))
182 #define inl_p(port) \
183 ((__builtin_constant_p((port)) && (port) < 256) ? \