linux-asm-io.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef _ASM_IO_H
  2. #define _ASM_IO_H
  3. /*
  4. * This file contains the definitions for the x86 IO instructions
  5. * inb/inw/inl/outb/outw/outl and the "string versions" of the same
  6. * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
  7. * versions of the single-IO instructions (inb_p/inw_p/..).
  8. *
  9. * This file is not meant to be obfuscating: it's just complicated
  10. * to (a) handle it all in a way that makes gcc able to optimize it
  11. * as well as possible and (b) trying to avoid writing the same thing
  12. * over and over again with slight variations and possibly making a
  13. * mistake somewhere.
  14. */
  15. /*
  16. * Thanks to James van Artsdalen for a better timing-fix than
  17. * the two short jumps: using outb's to a nonexistent port seems
  18. * to guarantee better timings even on fast machines.
  19. *
  20. * On the other hand, I'd like to be sure of a non-existent port:
  21. * I feel a bit unsafe about using 0x80 (should be safe, though)
  22. *
  23. * Linus
  24. */
  25. #ifdef SLOW_IO_BY_JUMPING
  26. #define __SLOW_DOWN_IO __asm__ __volatile__("jmp 1f\n1:\tjmp 1f\n1:")
  27. #else
  28. #define __SLOW_DOWN_IO __asm__ __volatile__("outb %al,$0x80")
  29. #endif
  30. #ifdef REALLY_SLOW_IO
  31. #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
  32. #else
  33. #define SLOW_DOWN_IO __SLOW_DOWN_IO
  34. #endif
  35. /*
  36. * readX/writeX() are used to access memory mapped devices. On some
  37. * architectures the memory mapped IO stuff needs to be accessed
  38. * differently. On the x86 architecture, we just read/write the
  39. * memory location directly.
  40. */
  41. #define readb(addr) (*(volatile unsigned char *) (addr))
  42. #define readw(addr) (*(volatile unsigned short *) (addr))
  43. #define readl(addr) (*(volatile unsigned int *) (addr))
  44. #define writeb(b,addr) ((*(volatile unsigned char *) (addr)) = (b))
  45. #define writew(b,addr) ((*(volatile unsigned short *) (addr)) = (b))
  46. #define writel(b,addr) ((*(volatile unsigned int *) (addr)) = (b))
  47. #define memset_io(a,b,c) memset((void *)(a),(b),(c))
  48. #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
  49. #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
  50. /*
  51. * Again, i386 does not require mem IO specific function.
  52. */
  53. #define eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(void *)(b),(c),(d))
  54. /*
  55. * Talk about misusing macros..
  56. */
  57. #define __OUT1(s,x) \
  58. extern void __out##s(unsigned x value, unsigned short port); \
  59. extern inline void __out##s(unsigned x value, unsigned short port) {
  60. #define __OUT2(s,s1,s2) \
  61. __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
  62. #define __OUT(s,s1,x) \
  63. __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); } \
  64. __OUT1(s##c,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); } \
  65. __OUT1(s##_p,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); SLOW_DOWN_IO; } \
  66. __OUT1(s##c_p,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); SLOW_DOWN_IO; }
  67. #define __IN1(s,x) \
  68. extern unsigned x __in##s(unsigned short port); \
  69. extern inline unsigned x __in##s(unsigned short port) { unsigned x _v;
  70. #define __IN2(s,s1,s2) \
  71. __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
  72. #define __IN(s,s1,x,i...) \
  73. __IN1(s,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); return _v; } \
  74. __IN1(s##c,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); return _v; } \
  75. __IN1(s##_p,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); SLOW_DOWN_IO; return _v; } \
  76. __IN1(s##c_p,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); SLOW_DOWN_IO; return _v; }
  77. #define __INS(s) \
  78. extern void ins##s(unsigned short port, void * addr, unsigned long count); \
  79. extern inline void ins##s(unsigned short port, void * addr, unsigned long count) \
  80. { __asm__ __volatile__ ("cld ; rep ; ins" #s \
  81. : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  82. #define __OUTS(s) \
  83. extern void outs##s(unsigned short port, const void * addr, unsigned long count); \
  84. extern inline void outs##s(unsigned short port, const void * addr, unsigned long count) \
  85. { __asm__ __volatile__ ("cld ; rep ; outs" #s \
  86. : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  87. __IN(b,"", char)
  88. __IN(w,"",short)
  89. __IN(l,"", long)
  90. __OUT(b,"b",char)
  91. __OUT(w,"w",short)
  92. __OUT(l,,int)
  93. __INS(b)
  94. __INS(w)
  95. __INS(l)
  96. __OUTS(b)
  97. __OUTS(w)
  98. __OUTS(l)
  99. /*
  100. * Note that due to the way __builtin_constant_p() works, you
  101. * - can't use it inside a inline function (it will never be true)
  102. * - you don't have to worry about side effects within the __builtin..
  103. */
  104. #define outb(val,port) \
  105. ((__builtin_constant_p((port)) && (port) < 256) ? \
  106. __outbc((val),(port)) : \
  107. __outb((val),(port)))
  108. #define inb(port) \
  109. ((__builtin_constant_p((port)) && (port) < 256) ? \
  110. __inbc(port) : \
  111. __inb(port))
  112. #define outb_p(val,port) \
  113. ((__builtin_constant_p((port)) && (port) < 256) ? \
  114. __outbc_p((val),(port)) : \
  115. __outb_p((val),(port)))
  116. #define inb_p(port) \
  117. ((__builtin_constant_p((port)) && (port) < 256) ? \
  118. __inbc_p(port) : \
  119. __inb_p(port))
  120. #define outw(val,port) \
  121. ((__builtin_constant_p((port)) && (port) < 256) ? \
  122. __outwc((val),(port)) : \
  123. __outw((val),(port)))
  124. #define inw(port) \
  125. ((__builtin_constant_p((port)) && (port) < 256) ? \
  126. __inwc(port) : \
  127. __inw(port))
  128. #define outw_p(val,port) \
  129. ((__builtin_constant_p((port)) && (port) < 256) ? \
  130. __outwc_p((val),(port)) : \
  131. __outw_p((val),(port)))
  132. #define inw_p(port) \
  133. ((__builtin_constant_p((port)) && (port) < 256) ? \
  134. __inwc_p(port) : \
  135. __inw_p(port))
  136. #define outl(val,port) \
  137. ((__builtin_constant_p((port)) && (port) < 256) ? \
  138. __outlc((val),(port)) : \
  139. __outl((val),(port)))
  140. #define inl(port) \
  141. ((__builtin_constant_p((port)) && (port) < 256) ? \
  142. __inlc(port) : \
  143. __inl(port))
  144. #define outl_p(val,port) \
  145. ((__builtin_constant_p((port)) && (port) < 256) ? \
  146. __outlc_p((val),(port)) : \
  147. __outl_p((val),(port)))
  148. #define inl_p(port) \
  149. ((__builtin_constant_p((port)) && (port) < 256) ? \
  150. __inlc_p(port) : \
  151. __inl_p(port))
  152. #endif