io.h (6187B)
1 /* $Id: //depot/blt/netboot/io.h#2 $ 2 ** 3 ** Copyright 1998 Brian J. Swetland 4 ** All rights reserved. 5 ** 6 ** Redistribution and use in source and binary forms, with or without 7 ** modification, are permitted provided that the following conditions 8 ** are met: 9 ** 1. Redistributions of source code must retain the above copyright 10 ** notice, this list of conditions, and the following disclaimer. 11 ** 2. Redistributions in binary form must reproduce the above copyright 12 ** notice, this list of conditions, and the following disclaimer in the 13 ** documentation and/or other materials provided with the distribution. 14 ** 3. The name of the author may not be used to endorse or promote products 15 ** derived from this software without specific prior written permission. 16 ** 17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #ifndef _ASM_IO_H 29 #define _ASM_IO_H 30 31 #define SLOW_IO_BY_JUMPING 32 33 /* 34 * This file contains the definitions for the x86 IO instructions 35 * inb/inw/inl/outb/outw/outl and the "string versions" of the same 36 * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing" 37 * versions of the single-IO instructions (inb_p/inw_p/..). 38 * 39 * This file is not meant to be obfuscating: it's just complicated 40 * to (a) handle it all in a way that makes gcc able to optimize it 41 * as well as possible and (b) trying to avoid writing the same thing 42 * over and over again with slight variations and possibly making a 43 * mistake somewhere. 44 */ 45 46 /* 47 * Thanks to James van Artsdalen for a better timing-fix than 48 * the two short jumps: using outb's to a nonexistent port seems 49 * to guarantee better timings even on fast machines. 50 * 51 * On the other hand, I'd like to be sure of a non-existent port: 52 * I feel a bit unsafe about using 0x80 (should be safe, though) 53 * 54 * Linus 55 */ 56 57 #ifdef SLOW_IO_BY_JUMPING 58 #define __SLOW_DOWN_IO __asm__ __volatile__("jmp 1f\n1:\tjmp 1f\n1:") 59 #else 60 #define __SLOW_DOWN_IO __asm__ __volatile__("outb %al,$0x80") 61 #endif 62 63 #define SLOW_DOWN_IO __SLOW_DOWN_IO 64 65 /* 66 * Talk about misusing macros.. 67 */ 68 69 #define __OUT1(s,x) \ 70 extern inline void __out##s(unsigned x value, unsigned short port) { 71 72 #define __OUT2(s,s1,s2) \ 73 __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1" 74 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; } 80 81 #define __IN1(s) \ 82 extern inline RETURN_TYPE __in##s(unsigned short port) { RETURN_TYPE _v; 83 84 #define __IN2(s,s1,s2) \ 85 __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0" 86 87 #define __IN(s,s1,i...) \ 88 __IN1(s) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); return _v; } \ 89 __IN1(s##c) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); return _v; } \ 90 __IN1(s##_p) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); SLOW_DOWN_IO; return _v; } \ 91 __IN1(s##c_p) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); SLOW_DOWN_IO; return _v; } 92 93 #define __INS(s) \ 94 extern inline void ins##s(unsigned short port, void * addr, unsigned long count) \ 95 { __asm__ __volatile__ ("cld ; rep ; ins" #s \ 96 : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); } 97 98 #define __OUTS(s) \ 99 extern inline void outs##s(unsigned short port, const void * addr, unsigned long count) \ 100 { __asm__ __volatile__ ("cld ; rep ; outs" #s \ 101 : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); } 102 103 #define RETURN_TYPE unsigned char 104 /* __IN(b,"b","0" (0)) */ 105 __IN(b,"") 106 #undef RETURN_TYPE 107 #define RETURN_TYPE unsigned short 108 /* __IN(w,"w","0" (0)) */ 109 __IN(w,"") 110 #undef RETURN_TYPE 111 #define RETURN_TYPE unsigned int 112 __IN(l,"") 113 #undef RETURN_TYPE 114 115 __OUT(b,"b",char) 116 __OUT(w,"w",short) 117 __OUT(l,,int) 118 119 __INS(b) 120 __INS(w) 121 __INS(l) 122 123 __OUTS(b) 124 __OUTS(w) 125 __OUTS(l) 126 127 /* 128 * Note that due to the way __builtin_constant_p() works, you 129 * - can't use it inside a inline function (it will never be true) 130 * - you don't have to worry about side effects within the __builtin.. 131 */ 132 #define outb(val,port) \ 133 ((__builtin_constant_p((port)) && (port) < 256) ? \ 134 __outbc((val),(port)) : \ 135 __outb((val),(port))) 136 137 #define inb(port) \ 138 ((__builtin_constant_p((port)) && (port) < 256) ? \ 139 __inbc(port) : \ 140 __inb(port)) 141 142 #define outb_p(val,port) \ 143 ((__builtin_constant_p((port)) && (port) < 256) ? \ 144 __outbc_p((val),(port)) : \ 145 __outb_p((val),(port))) 146 147 #define inb_p(port) \ 148 ((__builtin_constant_p((port)) && (port) < 256) ? \ 149 __inbc_p(port) : \ 150 __inb_p(port)) 151 152 #define outw(val,port) \ 153 ((__builtin_constant_p((port)) && (port) < 256) ? \ 154 __outwc((val),(port)) : \ 155 __outw((val),(port))) 156 157 #define inw(port) \ 158 ((__builtin_constant_p((port)) && (port) < 256) ? \ 159 __inwc(port) : \ 160 __inw(port)) 161 162 #define outw_p(val,port) \ 163 ((__builtin_constant_p((port)) && (port) < 256) ? \ 164 __outwc_p((val),(port)) : \ 165 __outw_p((val),(port))) 166 167 #define inw_p(port) \ 168 ((__builtin_constant_p((port)) && (port) < 256) ? \ 169 __inwc_p(port) : \ 170 __inw_p(port)) 171 172 #define outl(val,port) \ 173 ((__builtin_constant_p((port)) && (port) < 256) ? \ 174 __outlc((val),(port)) : \ 175 __outl((val),(port))) 176 177 #define inl(port) \ 178 ((__builtin_constant_p((port)) && (port) < 256) ? \ 179 __inlc(port) : \ 180 __inl(port)) 181 182 #define outl_p(val,port) \ 183 ((__builtin_constant_p((port)) && (port) < 256) ? \ 184 __outlc_p((val),(port)) : \ 185 __outl_p((val),(port))) 186 187 #define inl_p(port) \ 188 ((__builtin_constant_p((port)) && (port) < 256) ? \ 189 __inlc_p(port) : \ 190 __inl_p(port)) 191 192 #endif