You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

io.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef ETHERBOOT_IO_H
  2. #define ETHERBOOT_IO_H
  3. #include "virtaddr.h"
  4. /* virt_to_bus converts an addresss inside of etherboot [_start, _end]
  5. * into a memory access cards can use.
  6. */
  7. #define virt_to_bus virt_to_phys
  8. /* bus_to_virt reverses virt_to_bus, the address must be output
  9. * from virt_to_bus to be valid. This function does not work on
  10. * all bus addresses.
  11. */
  12. #define bus_to_virt phys_to_virt
  13. /* ioremap converts a random 32bit bus address into something
  14. * etherboot can access.
  15. */
  16. static inline void *ioremap(unsigned long bus_addr, unsigned long length __unused)
  17. {
  18. return bus_to_virt(bus_addr);
  19. }
  20. /* iounmap cleans up anything ioremap had to setup */
  21. static inline void iounmap(void *virt_addr __unused)
  22. {
  23. return;
  24. }
  25. /*
  26. * This file contains the definitions for the x86 IO instructions
  27. * inb/inw/inl/outb/outw/outl and the "string versions" of the same
  28. * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
  29. * versions of the single-IO instructions (inb_p/inw_p/..).
  30. *
  31. * This file is not meant to be obfuscating: it's just complicated
  32. * to (a) handle it all in a way that makes gcc able to optimize it
  33. * as well as possible and (b) trying to avoid writing the same thing
  34. * over and over again with slight variations and possibly making a
  35. * mistake somewhere.
  36. */
  37. /*
  38. * Thanks to James van Artsdalen for a better timing-fix than
  39. * the two short jumps: using outb's to a nonexistent port seems
  40. * to guarantee better timings even on fast machines.
  41. *
  42. * On the other hand, I'd like to be sure of a non-existent port:
  43. * I feel a bit unsafe about using 0x80 (should be safe, though)
  44. *
  45. * Linus
  46. */
  47. #ifdef SLOW_IO_BY_JUMPING
  48. #define __SLOW_DOWN_IO __asm__ __volatile__("jmp 1f\n1:\tjmp 1f\n1:")
  49. #else
  50. #define __SLOW_DOWN_IO __asm__ __volatile__("outb %al,$0x80")
  51. #endif
  52. #ifdef REALLY_SLOW_IO
  53. #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
  54. #else
  55. #define SLOW_DOWN_IO __SLOW_DOWN_IO
  56. #endif
  57. /*
  58. * readX/writeX() are used to access memory mapped devices. On some
  59. * architectures the memory mapped IO stuff needs to be accessed
  60. * differently. On the x86 architecture, we just read/write the
  61. * memory location directly.
  62. */
  63. #define readb(addr) (*(volatile unsigned char *) (addr))
  64. #define readw(addr) (*(volatile unsigned short *) (addr))
  65. #define readl(addr) (*(volatile unsigned int *) (addr))
  66. #define writeb(b,addr) ((*(volatile unsigned char *) (addr)) = (b))
  67. #define writew(b,addr) ((*(volatile unsigned short *) (addr)) = (b))
  68. #define writel(b,addr) ((*(volatile unsigned int *) (addr)) = (b))
  69. #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
  70. #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
  71. /*
  72. * Force strict CPU ordering.
  73. * And yes, this is required on UP too when we're talking
  74. * to devices.
  75. *
  76. * For now, "wmb()" doesn't actually do anything, as all
  77. * Intel CPU's follow what Intel calls a *Processor Order*,
  78. * in which all writes are seen in the program order even
  79. * outside the CPU.
  80. *
  81. * I expect future Intel CPU's to have a weaker ordering,
  82. * but I'd also expect them to finally get their act together
  83. * and add some real memory barriers if so.
  84. *
  85. * Some non intel clones support out of order store. wmb() ceases to be a
  86. * nop for these.
  87. */
  88. #define mb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
  89. #define rmb() mb()
  90. #define wmb() mb();
  91. /*
  92. * Talk about misusing macros..
  93. */
  94. #define __OUT1(s,x) \
  95. extern void __out##s(unsigned x value, unsigned short port); \
  96. extern inline void __out##s(unsigned x value, unsigned short port) {
  97. #define __OUT2(s,s1,s2) \
  98. __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
  99. #define __OUT(s,s1,x) \
  100. __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); } \
  101. __OUT1(s##c,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); } \
  102. __OUT1(s##_p,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); SLOW_DOWN_IO; } \
  103. __OUT1(s##c_p,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); SLOW_DOWN_IO; }
  104. #define __IN1(s,x) \
  105. extern unsigned x __in##s(unsigned short port); \
  106. extern inline unsigned x __in##s(unsigned short port) { unsigned x _v;
  107. #define __IN2(s,s1,s2) \
  108. __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
  109. #define __IN(s,s1,x,i...) \
  110. __IN1(s,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); return _v; } \
  111. __IN1(s##c,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); return _v; } \
  112. __IN1(s##_p,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); SLOW_DOWN_IO; return _v; } \
  113. __IN1(s##c_p,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); SLOW_DOWN_IO; return _v; }
  114. #define __INS(s) \
  115. extern void ins##s(unsigned short port, void * addr, unsigned long count); \
  116. extern inline void ins##s(unsigned short port, void * addr, unsigned long count) \
  117. { __asm__ __volatile__ ("cld ; rep ; ins" #s \
  118. : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  119. #define __OUTS(s) \
  120. extern void outs##s(unsigned short port, const void * addr, unsigned long count); \
  121. extern inline void outs##s(unsigned short port, const void * addr, unsigned long count) \
  122. { __asm__ __volatile__ ("cld ; rep ; outs" #s \
  123. : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  124. __IN(b,"", char)
  125. __IN(w,"",short)
  126. __IN(l,"", long)
  127. __OUT(b,"b",char)
  128. __OUT(w,"w",short)
  129. __OUT(l,,int)
  130. __INS(b)
  131. __INS(w)
  132. __INS(l)
  133. __OUTS(b)
  134. __OUTS(w)
  135. __OUTS(l)
  136. /*
  137. * Note that due to the way __builtin_constant_p() works, you
  138. * - can't use it inside a inline function (it will never be true)
  139. * - you don't have to worry about side effects within the __builtin..
  140. */
  141. #define outb(val,port) \
  142. ((__builtin_constant_p((port)) && (port) < 256) ? \
  143. __outbc((val),(port)) : \
  144. __outb((val),(port)))
  145. #define inb(port) \
  146. ((__builtin_constant_p((port)) && (port) < 256) ? \
  147. __inbc(port) : \
  148. __inb(port))
  149. #define outb_p(val,port) \
  150. ((__builtin_constant_p((port)) && (port) < 256) ? \
  151. __outbc_p((val),(port)) : \
  152. __outb_p((val),(port)))
  153. #define inb_p(port) \
  154. ((__builtin_constant_p((port)) && (port) < 256) ? \
  155. __inbc_p(port) : \
  156. __inb_p(port))
  157. #define outw(val,port) \
  158. ((__builtin_constant_p((port)) && (port) < 256) ? \
  159. __outwc((val),(port)) : \
  160. __outw((val),(port)))
  161. #define inw(port) \
  162. ((__builtin_constant_p((port)) && (port) < 256) ? \
  163. __inwc(port) : \
  164. __inw(port))
  165. #define outw_p(val,port) \
  166. ((__builtin_constant_p((port)) && (port) < 256) ? \
  167. __outwc_p((val),(port)) : \
  168. __outw_p((val),(port)))
  169. #define inw_p(port) \
  170. ((__builtin_constant_p((port)) && (port) < 256) ? \
  171. __inwc_p(port) : \
  172. __inw_p(port))
  173. #define outl(val,port) \
  174. ((__builtin_constant_p((port)) && (port) < 256) ? \
  175. __outlc((val),(port)) : \
  176. __outl((val),(port)))
  177. #define inl(port) \
  178. ((__builtin_constant_p((port)) && (port) < 256) ? \
  179. __inlc(port) : \
  180. __inl(port))
  181. #define outl_p(val,port) \
  182. ((__builtin_constant_p((port)) && (port) < 256) ? \
  183. __outlc_p((val),(port)) : \
  184. __outl_p((val),(port)))
  185. #define inl_p(port) \
  186. ((__builtin_constant_p((port)) && (port) < 256) ? \
  187. __inlc_p(port) : \
  188. __inl_p(port))
  189. #endif /* ETHERBOOT_IO_H */