io.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #ifndef _ASM_IO_H
  2. #define _ASM_IO_H
  3. #include <linux/config.h>
  4. #include <linux/string.h>
  5. #include <linux/compiler.h>
  6. /*
  7. * This file contains the definitions for the x86 IO instructions
  8. * inb/inw/inl/outb/outw/outl and the "string versions" of the same
  9. * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
  10. * versions of the single-IO instructions (inb_p/inw_p/..).
  11. *
  12. * This file is not meant to be obfuscating: it's just complicated
  13. * to (a) handle it all in a way that makes gcc able to optimize it
  14. * as well as possible and (b) trying to avoid writing the same thing
  15. * over and over again with slight variations and possibly making a
  16. * mistake somewhere.
  17. */
  18. /*
  19. * Thanks to James van Artsdalen for a better timing-fix than
  20. * the two short jumps: using outb's to a nonexistent port seems
  21. * to guarantee better timings even on fast machines.
  22. *
  23. * On the other hand, I'd like to be sure of a non-existent port:
  24. * I feel a bit unsafe about using 0x80 (should be safe, though)
  25. *
  26. * Linus
  27. */
  28. /*
  29. * Bit simplified and optimized by Jan Hubicka
  30. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999.
  31. *
  32. * isa_memset_io, isa_memcpy_fromio, isa_memcpy_toio added,
  33. * isa_read[wl] and isa_write[wl] fixed
  34. * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  35. */
  36. #define IO_SPACE_LIMIT 0xffff
  37. #define XQUAD_PORTIO_BASE 0xfe400000
  38. #define XQUAD_PORTIO_QUAD 0x40000 /* 256k per quad. */
  39. #ifdef __KERNEL__
  40. #include <asm-generic/iomap.h>
  41. #include <linux/vmalloc.h>
  42. /*
  43. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  44. * access
  45. */
  46. #define xlate_dev_mem_ptr(p) __va(p)
  47. /*
  48. * Convert a virtual cached pointer to an uncached pointer
  49. */
  50. #define xlate_dev_kmem_ptr(p) p
  51. /**
  52. * virt_to_phys - map virtual addresses to physical
  53. * @address: address to remap
  54. *
  55. * The returned physical address is the physical (CPU) mapping for
  56. * the memory address given. It is only valid to use this function on
  57. * addresses directly mapped or allocated via kmalloc.
  58. *
  59. * This function does not give bus mappings for DMA transfers. In
  60. * almost all conceivable cases a device driver should not be using
  61. * this function
  62. */
  63. static inline unsigned long virt_to_phys(volatile void * address)
  64. {
  65. return __pa(address);
  66. }
  67. /**
  68. * phys_to_virt - map physical address to virtual
  69. * @address: address to remap
  70. *
  71. * The returned virtual address is a current CPU mapping for
  72. * the memory address given. It is only valid to use this function on
  73. * addresses that have a kernel mapping
  74. *
  75. * This function does not handle bus mappings for DMA transfers. In
  76. * almost all conceivable cases a device driver should not be using
  77. * this function
  78. */
  79. static inline void * phys_to_virt(unsigned long address)
  80. {
  81. return __va(address);
  82. }
  83. /*
  84. * Change "struct page" to physical address.
  85. */
  86. #define page_to_phys(page) ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
  87. extern void __iomem * __ioremap(unsigned long offset, unsigned long size, unsigned long flags);
  88. /**
  89. * ioremap - map bus memory into CPU space
  90. * @offset: bus address of the memory
  91. * @size: size of the resource to map
  92. *
  93. * ioremap performs a platform specific sequence of operations to
  94. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  95. * writew/writel functions and the other mmio helpers. The returned
  96. * address is not guaranteed to be usable directly as a virtual
  97. * address.
  98. */
  99. static inline void __iomem * ioremap(unsigned long offset, unsigned long size)
  100. {
  101. return __ioremap(offset, size, 0);
  102. }
  103. extern void __iomem * ioremap_nocache(unsigned long offset, unsigned long size);
  104. extern void iounmap(volatile void __iomem *addr);
  105. /*
  106. * bt_ioremap() and bt_iounmap() are for temporary early boot-time
  107. * mappings, before the real ioremap() is functional.
  108. * A boot-time mapping is currently limited to at most 16 pages.
  109. */
  110. extern void *bt_ioremap(unsigned long offset, unsigned long size);
  111. extern void bt_iounmap(void *addr, unsigned long size);
  112. /* Use early IO mappings for DMI because it's initialized early */
  113. #define dmi_ioremap bt_ioremap
  114. #define dmi_iounmap bt_iounmap
  115. #define dmi_alloc alloc_bootmem
  116. /*
  117. * ISA I/O bus memory addresses are 1:1 with the physical address.
  118. */
  119. #define isa_virt_to_bus virt_to_phys
  120. #define isa_page_to_bus page_to_phys
  121. #define isa_bus_to_virt phys_to_virt
  122. /*
  123. * However PCI ones are not necessarily 1:1 and therefore these interfaces
  124. * are forbidden in portable PCI drivers.
  125. *
  126. * Allow them on x86 for legacy drivers, though.
  127. */
  128. #define virt_to_bus virt_to_phys
  129. #define bus_to_virt phys_to_virt
  130. /*
  131. * readX/writeX() are used to access memory mapped devices. On some
  132. * architectures the memory mapped IO stuff needs to be accessed
  133. * differently. On the x86 architecture, we just read/write the
  134. * memory location directly.
  135. */
  136. static inline unsigned char readb(const volatile void __iomem *addr)
  137. {
  138. return *(volatile unsigned char __force *) addr;
  139. }
  140. static inline unsigned short readw(const volatile void __iomem *addr)
  141. {
  142. return *(volatile unsigned short __force *) addr;
  143. }
  144. static inline unsigned int readl(const volatile void __iomem *addr)
  145. {
  146. return *(volatile unsigned int __force *) addr;
  147. }
  148. #define readb_relaxed(addr) readb(addr)
  149. #define readw_relaxed(addr) readw(addr)
  150. #define readl_relaxed(addr) readl(addr)
  151. #define __raw_readb readb
  152. #define __raw_readw readw
  153. #define __raw_readl readl
  154. static inline void writeb(unsigned char b, volatile void __iomem *addr)
  155. {
  156. *(volatile unsigned char __force *) addr = b;
  157. }
  158. static inline void writew(unsigned short b, volatile void __iomem *addr)
  159. {
  160. *(volatile unsigned short __force *) addr = b;
  161. }
  162. static inline void writel(unsigned int b, volatile void __iomem *addr)
  163. {
  164. *(volatile unsigned int __force *) addr = b;
  165. }
  166. #define __raw_writeb writeb
  167. #define __raw_writew writew
  168. #define __raw_writel writel
  169. #define mmiowb()
  170. static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
  171. {
  172. memset((void __force *) addr, val, count);
  173. }
  174. static inline void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
  175. {
  176. __memcpy(dst, (void __force *) src, count);
  177. }
  178. static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
  179. {
  180. __memcpy((void __force *) dst, src, count);
  181. }
  182. /*
  183. * ISA space is 'always mapped' on a typical x86 system, no need to
  184. * explicitly ioremap() it. The fact that the ISA IO space is mapped
  185. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  186. * are physical addresses. The following constant pointer can be
  187. * used as the IO-area pointer (it can be iounmapped as well, so the
  188. * analogy with PCI is quite large):
  189. */
  190. #define __ISA_IO_base ((char __iomem *)(PAGE_OFFSET))
  191. #define isa_readb(a) readb(__ISA_IO_base + (a))
  192. #define isa_readw(a) readw(__ISA_IO_base + (a))
  193. #define isa_readl(a) readl(__ISA_IO_base + (a))
  194. #define isa_writeb(b,a) writeb(b,__ISA_IO_base + (a))
  195. #define isa_writew(w,a) writew(w,__ISA_IO_base + (a))
  196. #define isa_writel(l,a) writel(l,__ISA_IO_base + (a))
  197. #define isa_memset_io(a,b,c) memset_io(__ISA_IO_base + (a),(b),(c))
  198. #define isa_memcpy_fromio(a,b,c) memcpy_fromio((a),__ISA_IO_base + (b),(c))
  199. #define isa_memcpy_toio(a,b,c) memcpy_toio(__ISA_IO_base + (a),(b),(c))
  200. /*
  201. * Again, i386 does not require mem IO specific function.
  202. */
  203. #define eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(void __force *)(b),(c),(d))
  204. #define isa_eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(void __force *)(__ISA_IO_base + (b)),(c),(d))
  205. /**
  206. * check_signature - find BIOS signatures
  207. * @io_addr: mmio address to check
  208. * @signature: signature block
  209. * @length: length of signature
  210. *
  211. * Perform a signature comparison with the mmio address io_addr. This
  212. * address should have been obtained by ioremap.
  213. * Returns 1 on a match.
  214. */
  215. static inline int check_signature(volatile void __iomem * io_addr,
  216. const unsigned char *signature, int length)
  217. {
  218. int retval = 0;
  219. do {
  220. if (readb(io_addr) != *signature)
  221. goto out;
  222. io_addr++;
  223. signature++;
  224. length--;
  225. } while (length);
  226. retval = 1;
  227. out:
  228. return retval;
  229. }
  230. /*
  231. * Cache management
  232. *
  233. * This needed for two cases
  234. * 1. Out of order aware processors
  235. * 2. Accidentally out of order processors (PPro errata #51)
  236. */
  237. #if defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE)
  238. static inline void flush_write_buffers(void)
  239. {
  240. __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory");
  241. }
  242. #define dma_cache_inv(_start,_size) flush_write_buffers()
  243. #define dma_cache_wback(_start,_size) flush_write_buffers()
  244. #define dma_cache_wback_inv(_start,_size) flush_write_buffers()
  245. #else
  246. /* Nothing to do */
  247. #define dma_cache_inv(_start,_size) do { } while (0)
  248. #define dma_cache_wback(_start,_size) do { } while (0)
  249. #define dma_cache_wback_inv(_start,_size) do { } while (0)
  250. #define flush_write_buffers()
  251. #endif
  252. #endif /* __KERNEL__ */
  253. #ifdef SLOW_IO_BY_JUMPING
  254. #define __SLOW_DOWN_IO "jmp 1f; 1: jmp 1f; 1:"
  255. #else
  256. #define __SLOW_DOWN_IO "outb %%al,$0x80;"
  257. #endif
  258. static inline void slow_down_io(void) {
  259. __asm__ __volatile__(
  260. __SLOW_DOWN_IO
  261. #ifdef REALLY_SLOW_IO
  262. __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO
  263. #endif
  264. : : );
  265. }
  266. #ifdef CONFIG_X86_NUMAQ
  267. extern void *xquad_portio; /* Where the IO area was mapped */
  268. #define XQUAD_PORT_ADDR(port, quad) (xquad_portio + (XQUAD_PORTIO_QUAD*quad) + port)
  269. #define __BUILDIO(bwl,bw,type) \
  270. static inline void out##bwl##_quad(unsigned type value, int port, int quad) { \
  271. if (xquad_portio) \
  272. write##bwl(value, XQUAD_PORT_ADDR(port, quad)); \
  273. else \
  274. out##bwl##_local(value, port); \
  275. } \
  276. static inline void out##bwl(unsigned type value, int port) { \
  277. out##bwl##_quad(value, port, 0); \
  278. } \
  279. static inline unsigned type in##bwl##_quad(int port, int quad) { \
  280. if (xquad_portio) \
  281. return read##bwl(XQUAD_PORT_ADDR(port, quad)); \
  282. else \
  283. return in##bwl##_local(port); \
  284. } \
  285. static inline unsigned type in##bwl(int port) { \
  286. return in##bwl##_quad(port, 0); \
  287. }
  288. #else
  289. #define __BUILDIO(bwl,bw,type) \
  290. static inline void out##bwl(unsigned type value, int port) { \
  291. out##bwl##_local(value, port); \
  292. } \
  293. static inline unsigned type in##bwl(int port) { \
  294. return in##bwl##_local(port); \
  295. }
  296. #endif
  297. #define BUILDIO(bwl,bw,type) \
  298. static inline void out##bwl##_local(unsigned type value, int port) { \
  299. __asm__ __volatile__("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port)); \
  300. } \
  301. static inline unsigned type in##bwl##_local(int port) { \
  302. unsigned type value; \
  303. __asm__ __volatile__("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port)); \
  304. return value; \
  305. } \
  306. static inline void out##bwl##_local_p(unsigned type value, int port) { \
  307. out##bwl##_local(value, port); \
  308. slow_down_io(); \
  309. } \
  310. static inline unsigned type in##bwl##_local_p(int port) { \
  311. unsigned type value = in##bwl##_local(port); \
  312. slow_down_io(); \
  313. return value; \
  314. } \
  315. __BUILDIO(bwl,bw,type) \
  316. static inline void out##bwl##_p(unsigned type value, int port) { \
  317. out##bwl(value, port); \
  318. slow_down_io(); \
  319. } \
  320. static inline unsigned type in##bwl##_p(int port) { \
  321. unsigned type value = in##bwl(port); \
  322. slow_down_io(); \
  323. return value; \
  324. } \
  325. static inline void outs##bwl(int port, const void *addr, unsigned long count) { \
  326. __asm__ __volatile__("rep; outs" #bwl : "+S"(addr), "+c"(count) : "d"(port)); \
  327. } \
  328. static inline void ins##bwl(int port, void *addr, unsigned long count) { \
  329. __asm__ __volatile__("rep; ins" #bwl : "+D"(addr), "+c"(count) : "d"(port)); \
  330. }
  331. BUILDIO(b,b,char)
  332. BUILDIO(w,w,short)
  333. BUILDIO(l,,int)
  334. #endif