io_32.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. * If the area you are trying to map is a PCI BAR you should have a
  99. * look at pci_iomap().
  100. */
  101. static inline void __iomem * ioremap(unsigned long offset, unsigned long size)
  102. {
  103. return __ioremap(offset, size, 0);
  104. }
  105. extern void __iomem * ioremap_nocache(unsigned long offset, unsigned long size);
  106. extern void iounmap(volatile void __iomem *addr);
  107. /*
  108. * bt_ioremap() and bt_iounmap() are for temporary early boot-time
  109. * mappings, before the real ioremap() is functional.
  110. * A boot-time mapping is currently limited to at most 16 pages.
  111. */
  112. extern void *bt_ioremap(unsigned long offset, unsigned long size);
  113. extern void bt_iounmap(void *addr, unsigned long size);
  114. extern void __iomem *fix_ioremap(unsigned idx, unsigned long phys);
  115. /* Use early IO mappings for DMI because it's initialized early */
  116. #define dmi_ioremap bt_ioremap
  117. #define dmi_iounmap bt_iounmap
  118. #define dmi_alloc alloc_bootmem
  119. /*
  120. * ISA I/O bus memory addresses are 1:1 with the physical address.
  121. */
  122. #define isa_virt_to_bus virt_to_phys
  123. #define isa_page_to_bus page_to_phys
  124. #define isa_bus_to_virt phys_to_virt
  125. /*
  126. * However PCI ones are not necessarily 1:1 and therefore these interfaces
  127. * are forbidden in portable PCI drivers.
  128. *
  129. * Allow them on x86 for legacy drivers, though.
  130. */
  131. #define virt_to_bus virt_to_phys
  132. #define bus_to_virt phys_to_virt
  133. /*
  134. * readX/writeX() are used to access memory mapped devices. On some
  135. * architectures the memory mapped IO stuff needs to be accessed
  136. * differently. On the x86 architecture, we just read/write the
  137. * memory location directly.
  138. */
  139. static inline unsigned char readb(const volatile void __iomem *addr)
  140. {
  141. return *(volatile unsigned char __force *) addr;
  142. }
  143. static inline unsigned short readw(const volatile void __iomem *addr)
  144. {
  145. return *(volatile unsigned short __force *) addr;
  146. }
  147. static inline unsigned int readl(const volatile void __iomem *addr)
  148. {
  149. return *(volatile unsigned int __force *) addr;
  150. }
  151. #define readb_relaxed(addr) readb(addr)
  152. #define readw_relaxed(addr) readw(addr)
  153. #define readl_relaxed(addr) readl(addr)
  154. #define __raw_readb readb
  155. #define __raw_readw readw
  156. #define __raw_readl readl
  157. static inline void writeb(unsigned char b, volatile void __iomem *addr)
  158. {
  159. *(volatile unsigned char __force *) addr = b;
  160. }
  161. static inline void writew(unsigned short b, volatile void __iomem *addr)
  162. {
  163. *(volatile unsigned short __force *) addr = b;
  164. }
  165. static inline void writel(unsigned int b, volatile void __iomem *addr)
  166. {
  167. *(volatile unsigned int __force *) addr = b;
  168. }
  169. #define __raw_writeb writeb
  170. #define __raw_writew writew
  171. #define __raw_writel writel
  172. #define mmiowb()
  173. static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
  174. {
  175. memset((void __force *) addr, val, count);
  176. }
  177. static inline void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
  178. {
  179. __memcpy(dst, (void __force *) src, count);
  180. }
  181. static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
  182. {
  183. __memcpy((void __force *) dst, src, count);
  184. }
  185. /*
  186. * ISA space is 'always mapped' on a typical x86 system, no need to
  187. * explicitly ioremap() it. The fact that the ISA IO space is mapped
  188. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  189. * are physical addresses. The following constant pointer can be
  190. * used as the IO-area pointer (it can be iounmapped as well, so the
  191. * analogy with PCI is quite large):
  192. */
  193. #define __ISA_IO_base ((char __iomem *)(PAGE_OFFSET))
  194. /*
  195. * Cache management
  196. *
  197. * This needed for two cases
  198. * 1. Out of order aware processors
  199. * 2. Accidentally out of order processors (PPro errata #51)
  200. */
  201. #if defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE)
  202. static inline void flush_write_buffers(void)
  203. {
  204. __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory");
  205. }
  206. #define dma_cache_inv(_start,_size) flush_write_buffers()
  207. #define dma_cache_wback(_start,_size) flush_write_buffers()
  208. #define dma_cache_wback_inv(_start,_size) flush_write_buffers()
  209. #else
  210. /* Nothing to do */
  211. #define dma_cache_inv(_start,_size) do { } while (0)
  212. #define dma_cache_wback(_start,_size) do { } while (0)
  213. #define dma_cache_wback_inv(_start,_size) do { } while (0)
  214. #define flush_write_buffers()
  215. #endif
  216. #endif /* __KERNEL__ */
  217. static inline void native_io_delay(void)
  218. {
  219. asm volatile("outb %%al,$0x80" : : : "memory");
  220. }
  221. #if defined(CONFIG_PARAVIRT)
  222. #include <asm/paravirt.h>
  223. #else
  224. static inline void slow_down_io(void) {
  225. native_io_delay();
  226. #ifdef REALLY_SLOW_IO
  227. native_io_delay();
  228. native_io_delay();
  229. native_io_delay();
  230. #endif
  231. }
  232. #endif
  233. #ifdef CONFIG_X86_NUMAQ
  234. extern void *xquad_portio; /* Where the IO area was mapped */
  235. #define XQUAD_PORT_ADDR(port, quad) (xquad_portio + (XQUAD_PORTIO_QUAD*quad) + port)
  236. #define __BUILDIO(bwl,bw,type) \
  237. static inline void out##bwl##_quad(unsigned type value, int port, int quad) { \
  238. if (xquad_portio) \
  239. write##bwl(value, XQUAD_PORT_ADDR(port, quad)); \
  240. else \
  241. out##bwl##_local(value, port); \
  242. } \
  243. static inline void out##bwl(unsigned type value, int port) { \
  244. out##bwl##_quad(value, port, 0); \
  245. } \
  246. static inline unsigned type in##bwl##_quad(int port, int quad) { \
  247. if (xquad_portio) \
  248. return read##bwl(XQUAD_PORT_ADDR(port, quad)); \
  249. else \
  250. return in##bwl##_local(port); \
  251. } \
  252. static inline unsigned type in##bwl(int port) { \
  253. return in##bwl##_quad(port, 0); \
  254. }
  255. #else
  256. #define __BUILDIO(bwl,bw,type) \
  257. static inline void out##bwl(unsigned type value, int port) { \
  258. out##bwl##_local(value, port); \
  259. } \
  260. static inline unsigned type in##bwl(int port) { \
  261. return in##bwl##_local(port); \
  262. }
  263. #endif
  264. #define BUILDIO(bwl,bw,type) \
  265. static inline void out##bwl##_local(unsigned type value, int port) { \
  266. __asm__ __volatile__("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port)); \
  267. } \
  268. static inline unsigned type in##bwl##_local(int port) { \
  269. unsigned type value; \
  270. __asm__ __volatile__("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port)); \
  271. return value; \
  272. } \
  273. static inline void out##bwl##_local_p(unsigned type value, int port) { \
  274. out##bwl##_local(value, port); \
  275. slow_down_io(); \
  276. } \
  277. static inline unsigned type in##bwl##_local_p(int port) { \
  278. unsigned type value = in##bwl##_local(port); \
  279. slow_down_io(); \
  280. return value; \
  281. } \
  282. __BUILDIO(bwl,bw,type) \
  283. static inline void out##bwl##_p(unsigned type value, int port) { \
  284. out##bwl(value, port); \
  285. slow_down_io(); \
  286. } \
  287. static inline unsigned type in##bwl##_p(int port) { \
  288. unsigned type value = in##bwl(port); \
  289. slow_down_io(); \
  290. return value; \
  291. } \
  292. static inline void outs##bwl(int port, const void *addr, unsigned long count) { \
  293. __asm__ __volatile__("rep; outs" #bwl : "+S"(addr), "+c"(count) : "d"(port)); \
  294. } \
  295. static inline void ins##bwl(int port, void *addr, unsigned long count) { \
  296. __asm__ __volatile__("rep; ins" #bwl : "+D"(addr), "+c"(count) : "d"(port)); \
  297. }
  298. BUILDIO(b,b,char)
  299. BUILDIO(w,w,short)
  300. BUILDIO(l,,int)
  301. #endif