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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #ifndef ETHERBOOT_IO_H
  2. #define ETHERBOOT_IO_H
  3. #include <stdint.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. static inline __attribute__ (( always_inline )) unsigned long
  65. _readb ( volatile uint8_t *addr ) {
  66. unsigned long data = *addr;
  67. DBGIO ( "[%08lx] => %02lx\n", virt_to_phys ( addr ), data );
  68. return data;
  69. }
  70. static inline __attribute__ (( always_inline )) unsigned long
  71. _readw ( volatile uint16_t *addr ) {
  72. unsigned long data = *addr;
  73. DBGIO ( "[%08lx] => %04lx\n", virt_to_phys ( addr ), data );
  74. return data;
  75. }
  76. static inline __attribute__ (( always_inline )) unsigned long
  77. _readl ( volatile uint32_t *addr ) {
  78. unsigned long data = *addr;
  79. DBGIO ( "[%08lx] => %08lx\n", virt_to_phys ( addr ), data );
  80. return data;
  81. }
  82. #define readb( addr ) _readb ( ( volatile uint8_t * ) (addr) )
  83. #define readw( addr ) _readw ( ( volatile uint16_t * ) (addr) )
  84. #define readl( addr ) _readl ( ( volatile uint32_t * ) (addr) )
  85. static inline __attribute__ (( always_inline )) void
  86. _writeb ( unsigned long data, volatile uint8_t *addr ) {
  87. DBGIO ( "[%08lx] <= %02lx\n", virt_to_phys ( addr ), data );
  88. *addr = data;
  89. }
  90. static inline __attribute__ (( always_inline )) void
  91. _writew ( unsigned long data, volatile uint16_t *addr ) {
  92. DBGIO ( "[%08lx] <= %04lx\n", virt_to_phys ( addr ), data );
  93. *addr = data;
  94. }
  95. static inline __attribute__ (( always_inline )) void
  96. _writel ( unsigned long data, volatile uint32_t *addr ) {
  97. DBGIO ( "[%08lx] <= %08lx\n", virt_to_phys ( addr ), data );
  98. *addr = data;
  99. }
  100. #define writeb( b, addr ) _writeb ( (b), ( volatile uint8_t * ) (addr) )
  101. #define writew( b, addr ) _writew ( (b), ( volatile uint16_t * ) (addr) )
  102. #define writel( b, addr ) _writel ( (b), ( volatile uint32_t * ) (addr) )
  103. #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
  104. #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
  105. /*
  106. * Force strict CPU ordering.
  107. * And yes, this is required on UP too when we're talking
  108. * to devices.
  109. *
  110. * For now, "wmb()" doesn't actually do anything, as all
  111. * Intel CPU's follow what Intel calls a *Processor Order*,
  112. * in which all writes are seen in the program order even
  113. * outside the CPU.
  114. *
  115. * I expect future Intel CPU's to have a weaker ordering,
  116. * but I'd also expect them to finally get their act together
  117. * and add some real memory barriers if so.
  118. *
  119. * Some non intel clones support out of order store. wmb() ceases to be a
  120. * nop for these.
  121. */
  122. #define mb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
  123. #define rmb() mb()
  124. #define wmb() mb();
  125. /*
  126. * Talk about misusing macros..
  127. */
  128. #define __OUT1(s,x) \
  129. extern void __out##s(unsigned x value, unsigned short port); \
  130. extern inline void __out##s(unsigned x value, unsigned short port) {
  131. #define __OUT2(s,s1,s2) \
  132. __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
  133. #define __OUT(s,s1,x) \
  134. __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); } \
  135. __OUT1(s##c,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); } \
  136. __OUT1(s##_p,x) __OUT2(s,s1,"w") : : "a" (value), "d" (port)); SLOW_DOWN_IO; } \
  137. __OUT1(s##c_p,x) __OUT2(s,s1,"") : : "a" (value), "id" (port)); SLOW_DOWN_IO; }
  138. #define __IN1(s,x) \
  139. extern unsigned x __in##s(unsigned short port); \
  140. extern inline unsigned x __in##s(unsigned short port) { unsigned x _v;
  141. #define __IN2(s,s1,s2) \
  142. __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
  143. #define __IN(s,s1,x,i...) \
  144. __IN1(s,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); return _v; } \
  145. __IN1(s##c,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); return _v; } \
  146. __IN1(s##_p,x) __IN2(s,s1,"w") : "=a" (_v) : "d" (port) ,##i ); SLOW_DOWN_IO; return _v; } \
  147. __IN1(s##c_p,x) __IN2(s,s1,"") : "=a" (_v) : "id" (port) ,##i ); SLOW_DOWN_IO; return _v; }
  148. #define __INS(s) \
  149. extern void ins##s(unsigned short port, void * addr, unsigned long count); \
  150. extern inline void ins##s(unsigned short port, void * addr, unsigned long count) \
  151. { __asm__ __volatile__ ("cld ; rep ; ins" #s \
  152. : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  153. #define __OUTS(s) \
  154. extern void outs##s(unsigned short port, const void * addr, unsigned long count); \
  155. extern inline void outs##s(unsigned short port, const void * addr, unsigned long count) \
  156. { __asm__ __volatile__ ("cld ; rep ; outs" #s \
  157. : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  158. __IN(b,"", char)
  159. __IN(w,"",short)
  160. __IN(l,"", long)
  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. /*
  171. * Note that due to the way __builtin_constant_p() works, you
  172. * - can't use it inside a inline function (it will never be true)
  173. * - you don't have to worry about side effects within the __builtin..
  174. */
  175. #define outb(val,port) \
  176. ((__builtin_constant_p((port)) && (port) < 256) ? \
  177. __outbc((val),(port)) : \
  178. __outb((val),(port)))
  179. #define inb(port) \
  180. ((__builtin_constant_p((port)) && (port) < 256) ? \
  181. __inbc(port) : \
  182. __inb(port))
  183. #define outb_p(val,port) \
  184. ((__builtin_constant_p((port)) && (port) < 256) ? \
  185. __outbc_p((val),(port)) : \
  186. __outb_p((val),(port)))
  187. #define inb_p(port) \
  188. ((__builtin_constant_p((port)) && (port) < 256) ? \
  189. __inbc_p(port) : \
  190. __inb_p(port))
  191. #define outw(val,port) \
  192. ((__builtin_constant_p((port)) && (port) < 256) ? \
  193. __outwc((val),(port)) : \
  194. __outw((val),(port)))
  195. #define inw(port) \
  196. ((__builtin_constant_p((port)) && (port) < 256) ? \
  197. __inwc(port) : \
  198. __inw(port))
  199. #define outw_p(val,port) \
  200. ((__builtin_constant_p((port)) && (port) < 256) ? \
  201. __outwc_p((val),(port)) : \
  202. __outw_p((val),(port)))
  203. #define inw_p(port) \
  204. ((__builtin_constant_p((port)) && (port) < 256) ? \
  205. __inwc_p(port) : \
  206. __inw_p(port))
  207. #define outl(val,port) \
  208. ((__builtin_constant_p((port)) && (port) < 256) ? \
  209. __outlc((val),(port)) : \
  210. __outl((val),(port)))
  211. #define inl(port) \
  212. ((__builtin_constant_p((port)) && (port) < 256) ? \
  213. __inlc(port) : \
  214. __inl(port))
  215. #define outl_p(val,port) \
  216. ((__builtin_constant_p((port)) && (port) < 256) ? \
  217. __outlc_p((val),(port)) : \
  218. __outl_p((val),(port)))
  219. #define inl_p(port) \
  220. ((__builtin_constant_p((port)) && (port) < 256) ? \
  221. __inlc_p(port) : \
  222. __inl_p(port))
  223. #endif /* ETHERBOOT_IO_H */