Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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