io.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #ifndef _ASM_IO_H
  2. #define _ASM_IO_H
  3. #include <linux/config.h>
  4. /*
  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/..).
  9. *
  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
  14. * mistake somewhere.
  15. */
  16. /*
  17. * Thanks to James van Artsdalen for a better timing-fix than
  18. * the two short jumps: using outb's to a nonexistent port seems
  19. * to guarantee better timings even on fast machines.
  20. *
  21. * On the other hand, I'd like to be sure of a non-existent port:
  22. * I feel a bit unsafe about using 0x80 (should be safe, though)
  23. *
  24. * Linus
  25. */
  26. /*
  27. * Bit simplified and optimized by Jan Hubicka
  28. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999.
  29. *
  30. * isa_memset_io, isa_memcpy_fromio, isa_memcpy_toio added,
  31. * isa_read[wl] and isa_write[wl] fixed
  32. * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  33. */
  34. #define IO_SPACE_LIMIT 0xffff
  35. #ifdef __KERNEL__
  36. /*
  37. * readX/writeX() are used to access memory mapped devices. On some
  38. * architectures the memory mapped IO stuff needs to be accessed
  39. * differently. On the x86 architecture, we just read/write the
  40. * memory location directly.
  41. */
  42. #define readb(addr) (*(volatile unsigned char *) (addr))
  43. #define readw(addr) (*(volatile unsigned short *) (addr))
  44. #define readl(addr) (*(volatile unsigned int *) (addr))
  45. #define __raw_readb readb
  46. #define __raw_readw readw
  47. #define __raw_readl readl
  48. #define writeb(b,addr) (*(volatile unsigned char *) (addr) = (b))
  49. #define writew(b,addr) (*(volatile unsigned short *) (addr) = (b))
  50. #define writel(b,addr) (*(volatile unsigned int *) (addr) = (b))
  51. #define __raw_writeb writeb
  52. #define __raw_writew writew
  53. #define __raw_writel writel
  54. #define memset_io(a,b,c) memset((a),(b),(c))
  55. #define memcpy_fromio(a,b,c) memcpy((a),(b),(c))
  56. #define memcpy_toio(a,b,c) memcpy((a),(b),(c))
  57. /*
  58. * ISA space is 'always mapped' on a typical x86 system, no need to
  59. * explicitly ioremap() it. The fact that the ISA IO space is mapped
  60. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  61. * are physical addresses. The following constant pointer can be
  62. * used as the IO-area pointer (it can be iounmapped as well, so the
  63. * analogy with PCI is quite large):
  64. */
  65. #define isa_readb(a) readb((a))
  66. #define isa_readw(a) readw((a))
  67. #define isa_readl(a) readl((a))
  68. #define isa_writeb(b,a) writeb(b,(a))
  69. #define isa_writew(w,a) writew(w,(a))
  70. #define isa_writel(l,a) writel(l,(a))
  71. #define isa_memset_io(a,b,c) memset_io((a),(b),(c))
  72. #define isa_memcpy_fromio(a,b,c) memcpy_fromio((a),(b),(c))
  73. #define isa_memcpy_toio(a,b,c) memcpy_toio((a),(b),(c))
  74. static inline int check_signature(unsigned long io_addr,
  75. const unsigned char *signature, int length)
  76. {
  77. int retval = 0;
  78. do {
  79. if (readb(io_addr) != *signature)
  80. goto out;
  81. io_addr++;
  82. signature++;
  83. length--;
  84. } while (length);
  85. retval = 1;
  86. out:
  87. return retval;
  88. }
  89. /**
  90. * isa_check_signature - find BIOS signatures
  91. * @io_addr: mmio address to check
  92. * @signature: signature block
  93. * @length: length of signature
  94. *
  95. * Perform a signature comparison with the ISA mmio address io_addr.
  96. * Returns 1 on a match.
  97. *
  98. * This function is deprecated. New drivers should use ioremap and
  99. * check_signature.
  100. */
  101. static inline int isa_check_signature(unsigned long io_addr,
  102. const unsigned char *signature, int length)
  103. {
  104. int retval = 0;
  105. do {
  106. if (isa_readb(io_addr) != *signature)
  107. goto out;
  108. io_addr++;
  109. signature++;
  110. length--;
  111. } while (length);
  112. retval = 1;
  113. out:
  114. return retval;
  115. }
  116. #endif /* __KERNEL__ */
  117. #ifdef SLOW_IO_BY_JUMPING
  118. #define __SLOW_DOWN_IO "\njmp 1f\n1:\tjmp 1f\n1:"
  119. #else
  120. #define __SLOW_DOWN_IO "\noutb %%al,$0x80"
  121. #endif
  122. #ifdef REALLY_SLOW_IO
  123. #define __FULL_SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO
  124. #else
  125. #define __FULL_SLOW_DOWN_IO __SLOW_DOWN_IO
  126. #endif
  127. /*
  128. * Talk about misusing macros..
  129. */
  130. #define __OUT1(s,x) \
  131. static inline void out##s(unsigned x value, unsigned short port) {
  132. #define __OUT2(s,s1,s2) \
  133. __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
  134. #define __OUT(s,s1,x) \
  135. __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "Nd" (port)); } \
  136. __OUT1(s##_p,x) __OUT2(s,s1,"w") __FULL_SLOW_DOWN_IO : : "a" (value), "Nd" (port));}
  137. #define __IN1(s) \
  138. static inline RETURN_TYPE in##s(unsigned short port) { RETURN_TYPE _v;
  139. #define __IN2(s,s1,s2) \
  140. __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
  141. #define __IN(s,s1,i...) \
  142. __IN1(s) __IN2(s,s1,"w") : "=a" (_v) : "Nd" (port) ,##i ); return _v; } \
  143. __IN1(s##_p) __IN2(s,s1,"w") __FULL_SLOW_DOWN_IO : "=a" (_v) : "Nd" (port) ,##i ); return _v; }
  144. #define __INS(s) \
  145. static inline void ins##s(unsigned short port, void * addr, unsigned long count) \
  146. { __asm__ __volatile__ ("rep ; ins" #s \
  147. : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  148. #define __OUTS(s) \
  149. static inline void outs##s(unsigned short port, const void * addr, unsigned long count) \
  150. { __asm__ __volatile__ ("rep ; outs" #s \
  151. : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  152. #define RETURN_TYPE unsigned char
  153. __IN(b,"")
  154. #undef RETURN_TYPE
  155. #define RETURN_TYPE unsigned short
  156. __IN(w,"")
  157. #undef RETURN_TYPE
  158. #define RETURN_TYPE unsigned int
  159. __IN(l,"")
  160. #undef RETURN_TYPE
  161. __OUT(b,"b",char)
  162. __OUT(w,"w",short)
  163. __OUT(l,,int)
  164. __INS(b)
  165. __INS(w)
  166. __INS(l)
  167. __OUTS(b)
  168. __OUTS(w)
  169. __OUTS(l)
  170. #endif