io.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #ifndef _ASM_IO_H
  2. #define _ASM_IO_H
  3. #include <linux/compiler.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. #include <asm/types.h>
  36. #ifdef __KERNEL__
  37. /*
  38. * readX/writeX() are used to access memory mapped devices. On some
  39. * architectures the memory mapped IO stuff needs to be accessed
  40. * differently. On the x86 architecture, we just read/write the
  41. * memory location directly.
  42. */
  43. #define readb(addr) (*(volatile unsigned char *) (addr))
  44. #define readw(addr) (*(volatile unsigned short *) (addr))
  45. #define readl(addr) (*(volatile unsigned int *) (addr))
  46. #define __raw_readb readb
  47. #define __raw_readw readw
  48. #define __raw_readl readl
  49. #define writeb(b,addr) (*(volatile unsigned char *) (addr) = (b))
  50. #define writew(b,addr) (*(volatile unsigned short *) (addr) = (b))
  51. #define writel(b,addr) (*(volatile unsigned int *) (addr) = (b))
  52. #define __raw_writeb writeb
  53. #define __raw_writew writew
  54. #define __raw_writel writel
  55. #define memset_io(a,b,c) memset((a),(b),(c))
  56. #define memcpy_fromio(a,b,c) memcpy((a),(b),(c))
  57. #define memcpy_toio(a,b,c) memcpy((a),(b),(c))
  58. #define write_arch(type, endian, a, v) __raw_write##type(cpu_to_##endian(v), a)
  59. #define read_arch(type, endian, a) endian##_to_cpu(__raw_read##type(a))
  60. #define write_le64(a, v) write_arch(q, le64, a, v)
  61. #define write_le32(a, v) write_arch(l, le32, a, v)
  62. #define write_le16(a, v) write_arch(w, le16, a, v)
  63. #define read_le64(a) read_arch(q, le64, a)
  64. #define read_le32(a) read_arch(l, le32, a)
  65. #define read_le16(a) read_arch(w, le16, a)
  66. #define write_be32(a, v) write_arch(l, be32, a, v)
  67. #define write_be16(a, v) write_arch(w, be16, a, v)
  68. #define read_be32(a) read_arch(l, be32, a)
  69. #define read_be16(a) read_arch(w, be16, a)
  70. #define write_8(a, v) __raw_writeb(v, a)
  71. #define read_8(a) __raw_readb(a)
  72. #define clrbits(type, addr, clear) \
  73. write_##type((addr), read_##type(addr) & ~(clear))
  74. #define setbits(type, addr, set) \
  75. write_##type((addr), read_##type(addr) | (set))
  76. #define clrsetbits(type, addr, clear, set) \
  77. write_##type((addr), (read_##type(addr) & ~(clear)) | (set))
  78. #define clrbits_be32(addr, clear) clrbits(be32, addr, clear)
  79. #define setbits_be32(addr, set) setbits(be32, addr, set)
  80. #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
  81. #define clrbits_le32(addr, clear) clrbits(le32, addr, clear)
  82. #define setbits_le32(addr, set) setbits(le32, addr, set)
  83. #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
  84. #define clrbits_be16(addr, clear) clrbits(be16, addr, clear)
  85. #define setbits_be16(addr, set) setbits(be16, addr, set)
  86. #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
  87. #define clrbits_le16(addr, clear) clrbits(le16, addr, clear)
  88. #define setbits_le16(addr, set) setbits(le16, addr, set)
  89. #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
  90. #define clrbits_8(addr, clear) clrbits(8, addr, clear)
  91. #define setbits_8(addr, set) setbits(8, addr, set)
  92. #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
  93. /*
  94. * ISA space is 'always mapped' on a typical x86 system, no need to
  95. * explicitly ioremap() it. The fact that the ISA IO space is mapped
  96. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  97. * are physical addresses. The following constant pointer can be
  98. * used as the IO-area pointer (it can be iounmapped as well, so the
  99. * analogy with PCI is quite large):
  100. */
  101. #define isa_readb(a) readb((a))
  102. #define isa_readw(a) readw((a))
  103. #define isa_readl(a) readl((a))
  104. #define isa_writeb(b,a) writeb(b,(a))
  105. #define isa_writew(w,a) writew(w,(a))
  106. #define isa_writel(l,a) writel(l,(a))
  107. #define isa_memset_io(a,b,c) memset_io((a),(b),(c))
  108. #define isa_memcpy_fromio(a,b,c) memcpy_fromio((a),(b),(c))
  109. #define isa_memcpy_toio(a,b,c) memcpy_toio((a),(b),(c))
  110. static inline int check_signature(unsigned long io_addr,
  111. const unsigned char *signature, int length)
  112. {
  113. int retval = 0;
  114. do {
  115. if (readb(io_addr) != *signature)
  116. goto out;
  117. io_addr++;
  118. signature++;
  119. length--;
  120. } while (length);
  121. retval = 1;
  122. out:
  123. return retval;
  124. }
  125. /**
  126. * isa_check_signature - find BIOS signatures
  127. * @io_addr: mmio address to check
  128. * @signature: signature block
  129. * @length: length of signature
  130. *
  131. * Perform a signature comparison with the ISA mmio address io_addr.
  132. * Returns 1 on a match.
  133. *
  134. * This function is deprecated. New drivers should use ioremap and
  135. * check_signature.
  136. */
  137. static inline int isa_check_signature(unsigned long io_addr,
  138. const unsigned char *signature, int length)
  139. {
  140. int retval = 0;
  141. do {
  142. if (isa_readb(io_addr) != *signature)
  143. goto out;
  144. io_addr++;
  145. signature++;
  146. length--;
  147. } while (length);
  148. retval = 1;
  149. out:
  150. return retval;
  151. }
  152. #endif /* __KERNEL__ */
  153. #ifdef SLOW_IO_BY_JUMPING
  154. #define __SLOW_DOWN_IO "\njmp 1f\n1:\tjmp 1f\n1:"
  155. #else
  156. #define __SLOW_DOWN_IO "\noutb %%al,$0xed"
  157. #endif
  158. #ifdef REALLY_SLOW_IO
  159. #define __FULL_SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO
  160. #else
  161. #define __FULL_SLOW_DOWN_IO __SLOW_DOWN_IO
  162. #endif
  163. /*
  164. * Talk about misusing macros..
  165. */
  166. #define __OUT1(s,x) \
  167. static inline void _out##s(unsigned x value, unsigned short port) {
  168. #define __OUT2(s,s1,s2) \
  169. __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1"
  170. #define __OUT(s,s1,x) \
  171. __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "Nd" (port)); } \
  172. __OUT1(s##_p,x) __OUT2(s,s1,"w") __FULL_SLOW_DOWN_IO : : "a" (value), "Nd" (port));}
  173. #define __IN1(s) \
  174. static inline RETURN_TYPE _in##s(unsigned short port) { RETURN_TYPE _v;
  175. #define __IN2(s,s1,s2) \
  176. __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0"
  177. #define __IN(s,s1,i...) \
  178. __IN1(s) __IN2(s,s1,"w") : "=a" (_v) : "Nd" (port) ,##i ); return _v; } \
  179. __IN1(s##_p) __IN2(s,s1,"w") __FULL_SLOW_DOWN_IO : "=a" (_v) : "Nd" (port) ,##i ); return _v; }
  180. #define __INS(s) \
  181. static inline void ins##s(unsigned short port, void * addr, unsigned long count) \
  182. { __asm__ __volatile__ ("rep ; ins" #s \
  183. : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  184. #define __OUTS(s) \
  185. static inline void outs##s(unsigned short port, const void * addr, unsigned long count) \
  186. { __asm__ __volatile__ ("rep ; outs" #s \
  187. : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); }
  188. #define RETURN_TYPE unsigned char
  189. __IN(b,"")
  190. #undef RETURN_TYPE
  191. #define RETURN_TYPE unsigned short
  192. __IN(w,"")
  193. #undef RETURN_TYPE
  194. #define RETURN_TYPE unsigned int
  195. __IN(l,"")
  196. #undef RETURN_TYPE
  197. #define inb(port) _inb((uintptr_t)(port))
  198. #define inw(port) _inw((uintptr_t)(port))
  199. #define inl(port) _inl((uintptr_t)(port))
  200. __OUT(b,"b",char)
  201. __OUT(w,"w",short)
  202. __OUT(l,,int)
  203. #define outb(val, port) _outb(val, (uintptr_t)(port))
  204. #define outw(val, port) _outw(val, (uintptr_t)(port))
  205. #define outl(val, port) _outl(val, (uintptr_t)(port))
  206. __INS(b)
  207. __INS(w)
  208. __INS(l)
  209. __OUTS(b)
  210. __OUTS(w)
  211. __OUTS(l)
  212. /* IO space accessors */
  213. #define clrio(type, addr, clear) \
  214. out##type(in##type(addr) & ~(clear), (addr))
  215. #define setio(type, addr, set) \
  216. out##type(in##type(addr) | (set), (addr))
  217. #define clrsetio(type, addr, clear, set) \
  218. out##type((in##type(addr) & ~(clear)) | (set), (addr))
  219. #define clrio_32(addr, clear) clrio(l, addr, clear)
  220. #define clrio_16(addr, clear) clrio(w, addr, clear)
  221. #define clrio_8(addr, clear) clrio(b, addr, clear)
  222. #define setio_32(addr, set) setio(l, addr, set)
  223. #define setio_16(addr, set) setio(w, addr, set)
  224. #define setio_8(addr, set) setio(b, addr, set)
  225. #define clrsetio_32(addr, clear, set) clrsetio(l, addr, clear, set)
  226. #define clrsetio_16(addr, clear, set) clrsetio(w, addr, clear, set)
  227. #define clrsetio_8(addr, clear, set) clrsetio(b, addr, clear, set)
  228. static inline void sync(void)
  229. {
  230. }
  231. /*
  232. * Given a physical address and a length, return a virtual address
  233. * that can be used to access the memory range with the caching
  234. * properties specified by "flags".
  235. */
  236. #define MAP_NOCACHE (0)
  237. #define MAP_WRCOMBINE (0)
  238. #define MAP_WRBACK (0)
  239. #define MAP_WRTHROUGH (0)
  240. static inline void *
  241. map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  242. {
  243. return (void *)(uintptr_t)paddr;
  244. }
  245. /*
  246. * Take down a mapping set up by map_physmem().
  247. */
  248. static inline void unmap_physmem(void *vaddr, unsigned long flags)
  249. {
  250. }
  251. static inline phys_addr_t virt_to_phys(void * vaddr)
  252. {
  253. return (phys_addr_t)(uintptr_t)(vaddr);
  254. }
  255. /*
  256. * TODO: The kernel offers some more advanced versions of barriers, it might
  257. * have some advantages to use them instead of the simple one here.
  258. */
  259. #define dmb() __asm__ __volatile__ ("" : : : "memory")
  260. #define __iormb() dmb()
  261. #define __iowmb() dmb()
  262. #endif