io.h

Go to the documentation of this file.
00001 #ifndef _ASM_IO_H
00002 #define _ASM_IO_H
00003 
00004 /**
00005 *
00006 
00007  * This file contains the definitions for the x86 IO instructions
00008  * inb/inw/inl/outb/outw/outl and the "string versions" of the same
00009  * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
00010  * versions of the single-IO instructions (inb_p/inw_p/..).
00011  *
00012  * This file is not meant to be obfuscating: it's just complicated
00013  * to (a) handle it all in a way that makes gcc able to optimize it
00014  * as well as possible and (b) trying to avoid writing the same thing
00015  * over and over again with slight variations and possibly making a
00016  * mistake somewhere.
00017 
00018 
00019 */
00020 
00021 /**
00022 *
00023 
00024  * Thanks to James van Artsdalen for a better timing-fix than
00025  * the two short jumps: using outb's to a nonexistent port seems
00026  * to guarantee better timings even on fast machines.
00027  *
00028  * On the other hand, I'd like to be sure of a non-existent port:
00029  * I feel a bit unsafe about using 0x80 (should be safe, though)
00030  *
00031  *              Linus
00032 
00033 
00034 */
00035 
00036 #ifdef SLOW_IO_BY_JUMPING
00037 #define __SLOW_DOWN_IO __asm__ __volatile__("jmp 1f\n1:\tjmp 1f\n1:")
00038 #else
00039 #define __SLOW_DOWN_IO __asm__ __volatile__("outb %al,$0x80")
00040 #endif
00041 
00042 #ifdef REALLY_SLOW_IO
00043 #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
00044 #else
00045 #define SLOW_DOWN_IO __SLOW_DOWN_IO
00046 #endif
00047 
00048 /**
00049 *
00050 
00051  * Talk about misusing macros..
00052 
00053 
00054 */
00055 
00056 #define __OUT1(s,x) \
00057 extern inline void __out##s(unsigned x value, unsigned short port) {
00058 
00059 #define __OUT2(s,s1,s2) \
00060 __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
00061 
00062 #define __OUT(s,s1,x) \
00063 __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); } \
00064 __OUT1(s##c,x) __OUT2(s,s1,"") : : "a" (value), "i" (port)); } \
00065 __OUT1(s##_p,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); SLOW_DOWN_IO; } \
00066 __OUT1(s##c_p,x) __OUT2(s,s1,"") : : "a" (value), "i" (port)); SLOW_DOWN_IO; }
00067 
00068 #define __IN1(s) \
00069 extern inline unsigned int __in##s(unsigned short port) { unsigned int _v;
00070 
00071 #define __IN2(s,s1,s2) \
00072 __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
00073 
00074 #define __IN(s,s1,i...) \
00075 __IN1(s) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); return _v; } \
00076 __IN1(s##c) __IN2(s,s1,"") : "=a" (_v) : "i" (port) ,##i ); return _v; } \
00077 __IN1(s##_p) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); SLOW_DOWN_IO; return _v; } \
00078 __IN1(s##c_p) __IN2(s,s1,"") : "=a" (_v) : "i" (port) ,##i ); SLOW_DOWN_IO; return _v; }
00079 
00080 #define __INS(s) \
00081 extern inline void ins##s(unsigned short port, void * addr, unsigned long count) \
00082 { __asm__ __volatile__ ("cld ; rep ; ins" #s \
00083 : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
00084 
00085 #define __OUTS(s) \
00086 extern inline void outs##s(unsigned short port, const void * addr, unsigned long count) \
00087 { __asm__ __volatile__ ("cld ; rep ; outs" #s \
00088 : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
00089 
00090 __IN(b,"b","0" (0))
00091 __IN(w,"w","0" (0))
00092 __IN(l,"")
00093 
00094 __OUT(b,"b",char)
00095 __OUT(w,"w",short)
00096 __OUT(l,,int)
00097 
00098 __INS(b)
00099 __INS(w)
00100 __INS(l)
00101 
00102 __OUTS(b)
00103 __OUTS(w)
00104 __OUTS(l)
00105 
00106 /**
00107 *
00108 
00109  * Note that due to the way __builtin_constant_p() works, you
00110  *  - can't use it inside a inline function (it will never be true)
00111  *  - you don't have to worry about side effects within the __builtin..
00112 
00113 
00114 */
00115 #define outb(val,port) \
00116 ((__builtin_constant_p((port)) && (port) < 256) ? \
00117         __outbc((val),(port)) : \
00118         __outb((val),(port)))
00119 
00120 #define inb(port) \
00121 ((__builtin_constant_p((port)) && (port) < 256) ? \
00122         __inbc(port) : \
00123         __inb(port))
00124 
00125 #define outb_p(val,port) \
00126 ((__builtin_constant_p((port)) && (port) < 256) ? \
00127         __outbc_p((val),(port)) : \
00128         __outb_p((val),(port)))
00129 
00130 #define inb_p(port) \
00131 ((__builtin_constant_p((port)) && (port) < 256) ? \
00132         __inbc_p(port) : \
00133         __inb_p(port))
00134 
00135 #define outw(val,port) \
00136 ((__builtin_constant_p((port)) && (port) < 256) ? \
00137         __outwc((val),(port)) : \
00138         __outw((val),(port)))
00139 
00140 #define inw(port) \
00141 ((__builtin_constant_p((port)) && (port) < 256) ? \
00142         __inwc(port) : \
00143         __inw(port))
00144 
00145 #define outw_p(val,port) \
00146 ((__builtin_constant_p((port)) && (port) < 256) ? \
00147         __outwc_p((val),(port)) : \
00148         __outw_p((val),(port)))
00149 
00150 #define inw_p(port) \
00151 ((__builtin_constant_p((port)) && (port) < 256) ? \
00152         __inwc_p(port) : \
00153         __inw_p(port))
00154 
00155 #define outl(val,port) \
00156 ((__builtin_constant_p((port)) && (port) < 256) ? \
00157         __outlc((val),(port)) : \
00158         __outl((val),(port)))
00159 
00160 #define inl(port) \
00161 ((__builtin_constant_p((port)) && (port) < 256) ? \
00162         __inlc(port) : \
00163         __inl(port))
00164 
00165 #define outl_p(val,port) \
00166 ((__builtin_constant_p((port)) && (port) < 256) ? \
00167         __outlc_p((val),(port)) : \
00168         __outl_p((val),(port)))
00169 
00170 #define inl_p(port) \
00171 ((__builtin_constant_p((port)) && (port) < 256) ? \
00172         __inlc_p(port) : \
00173         __inl_p(port))
00174 
00175 #endif

Generated on Mon May 1 21:46:59 2006 for KernelAPI by  doxygen 1.4.6-5