io.h 11 KB

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