io.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #ifndef _ASM_X86_IO_H
  2. #define _ASM_X86_IO_H
  3. /*
  4. * This file contains the definitions for the x86 IO instructions
  5. * inb/inw/inl/outb/outw/outl and the "string versions" of the same
  6. * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
  7. * versions of the single-IO instructions (inb_p/inw_p/..).
  8. *
  9. * This file is not meant to be obfuscating: it's just complicated
  10. * to (a) handle it all in a way that makes gcc able to optimize it
  11. * as well as possible and (b) trying to avoid writing the same thing
  12. * over and over again with slight variations and possibly making a
  13. * mistake somewhere.
  14. */
  15. /*
  16. * Thanks to James van Artsdalen for a better timing-fix than
  17. * the two short jumps: using outb's to a nonexistent port seems
  18. * to guarantee better timings even on fast machines.
  19. *
  20. * On the other hand, I'd like to be sure of a non-existent port:
  21. * I feel a bit unsafe about using 0x80 (should be safe, though)
  22. *
  23. * Linus
  24. */
  25. /*
  26. * Bit simplified and optimized by Jan Hubicka
  27. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999.
  28. *
  29. * isa_memset_io, isa_memcpy_fromio, isa_memcpy_toio added,
  30. * isa_read[wl] and isa_write[wl] fixed
  31. * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  32. */
  33. #define ARCH_HAS_IOREMAP_WC
  34. #include <linux/string.h>
  35. #include <linux/compiler.h>
  36. #include <asm-generic/int-ll64.h>
  37. #include <asm/page.h>
  38. #define build_mmio_read(name, size, type, reg, barrier) \
  39. static inline type name(const volatile void __iomem *addr) \
  40. { type ret; asm volatile("mov" size " %1,%0":reg (ret) \
  41. :"m" (*(volatile type __force *)addr) barrier); return ret; }
  42. #define build_mmio_write(name, size, type, reg, barrier) \
  43. static inline void name(type val, volatile void __iomem *addr) \
  44. { asm volatile("mov" size " %0,%1": :reg (val), \
  45. "m" (*(volatile type __force *)addr) barrier); }
  46. build_mmio_read(readb, "b", unsigned char, "=q", :"memory")
  47. build_mmio_read(readw, "w", unsigned short, "=r", :"memory")
  48. build_mmio_read(readl, "l", unsigned int, "=r", :"memory")
  49. build_mmio_read(__readb, "b", unsigned char, "=q", )
  50. build_mmio_read(__readw, "w", unsigned short, "=r", )
  51. build_mmio_read(__readl, "l", unsigned int, "=r", )
  52. build_mmio_write(writeb, "b", unsigned char, "q", :"memory")
  53. build_mmio_write(writew, "w", unsigned short, "r", :"memory")
  54. build_mmio_write(writel, "l", unsigned int, "r", :"memory")
  55. build_mmio_write(__writeb, "b", unsigned char, "q", )
  56. build_mmio_write(__writew, "w", unsigned short, "r", )
  57. build_mmio_write(__writel, "l", unsigned int, "r", )
  58. #define readb_relaxed(a) __readb(a)
  59. #define readw_relaxed(a) __readw(a)
  60. #define readl_relaxed(a) __readl(a)
  61. #define __raw_readb __readb
  62. #define __raw_readw __readw
  63. #define __raw_readl __readl
  64. #define __raw_writeb __writeb
  65. #define __raw_writew __writew
  66. #define __raw_writel __writel
  67. #define mmiowb() barrier()
  68. #ifdef CONFIG_X86_64
  69. build_mmio_read(readq, "q", unsigned long, "=r", :"memory")
  70. build_mmio_write(writeq, "q", unsigned long, "r", :"memory")
  71. #else
  72. static inline __u64 readq(const volatile void __iomem *addr)
  73. {
  74. const volatile u32 __iomem *p = addr;
  75. u32 low, high;
  76. low = readl(p);
  77. high = readl(p + 1);
  78. return low + ((u64)high << 32);
  79. }
  80. static inline void writeq(__u64 val, volatile void __iomem *addr)
  81. {
  82. writel(val, addr);
  83. writel(val >> 32, addr+4);
  84. }
  85. #endif
  86. #define readq_relaxed(a) readq(a)
  87. #define __raw_readq(a) readq(a)
  88. #define __raw_writeq(val, addr) writeq(val, addr)
  89. /* Let people know that we have them */
  90. #define readq readq
  91. #define writeq writeq
  92. /**
  93. * virt_to_phys - map virtual addresses to physical
  94. * @address: address to remap
  95. *
  96. * The returned physical address is the physical (CPU) mapping for
  97. * the memory address given. It is only valid to use this function on
  98. * addresses directly mapped or allocated via kmalloc.
  99. *
  100. * This function does not give bus mappings for DMA transfers. In
  101. * almost all conceivable cases a device driver should not be using
  102. * this function
  103. */
  104. static inline phys_addr_t virt_to_phys(volatile void *address)
  105. {
  106. return __pa(address);
  107. }
  108. /**
  109. * phys_to_virt - map physical address to virtual
  110. * @address: address to remap
  111. *
  112. * The returned virtual address is a current CPU mapping for
  113. * the memory address given. It is only valid to use this function on
  114. * addresses that have a kernel mapping
  115. *
  116. * This function does not handle bus mappings for DMA transfers. In
  117. * almost all conceivable cases a device driver should not be using
  118. * this function
  119. */
  120. static inline void *phys_to_virt(phys_addr_t address)
  121. {
  122. return __va(address);
  123. }
  124. /*
  125. * Change "struct page" to physical address.
  126. */
  127. #define page_to_phys(page) ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
  128. /*
  129. * ISA I/O bus memory addresses are 1:1 with the physical address.
  130. * However, we truncate the address to unsigned int to avoid undesirable
  131. * promitions in legacy drivers.
  132. */
  133. static inline unsigned int isa_virt_to_bus(volatile void *address)
  134. {
  135. return (unsigned int)virt_to_phys(address);
  136. }
  137. #define isa_page_to_bus(page) ((unsigned int)page_to_phys(page))
  138. #define isa_bus_to_virt phys_to_virt
  139. /*
  140. * However PCI ones are not necessarily 1:1 and therefore these interfaces
  141. * are forbidden in portable PCI drivers.
  142. *
  143. * Allow them on x86 for legacy drivers, though.
  144. */
  145. #define virt_to_bus virt_to_phys
  146. #define bus_to_virt phys_to_virt
  147. /**
  148. * ioremap - map bus memory into CPU space
  149. * @offset: bus address of the memory
  150. * @size: size of the resource to map
  151. *
  152. * ioremap performs a platform specific sequence of operations to
  153. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  154. * writew/writel functions and the other mmio helpers. The returned
  155. * address is not guaranteed to be usable directly as a virtual
  156. * address.
  157. *
  158. * If the area you are trying to map is a PCI BAR you should have a
  159. * look at pci_iomap().
  160. */
  161. extern void __iomem *ioremap_nocache(resource_size_t offset, unsigned long size);
  162. extern void __iomem *ioremap_cache(resource_size_t offset, unsigned long size);
  163. extern void __iomem *ioremap_prot(resource_size_t offset, unsigned long size,
  164. unsigned long prot_val);
  165. /*
  166. * The default ioremap() behavior is non-cached:
  167. */
  168. static inline void __iomem *ioremap(resource_size_t offset, unsigned long size)
  169. {
  170. return ioremap_nocache(offset, size);
  171. }
  172. extern void iounmap(volatile void __iomem *addr);
  173. #ifdef __KERNEL__
  174. #include <asm-generic/iomap.h>
  175. #include <linux/vmalloc.h>
  176. /*
  177. * Convert a virtual cached pointer to an uncached pointer
  178. */
  179. #define xlate_dev_kmem_ptr(p) p
  180. static inline void
  181. memset_io(volatile void __iomem *addr, unsigned char val, size_t count)
  182. {
  183. memset((void __force *)addr, val, count);
  184. }
  185. static inline void
  186. memcpy_fromio(void *dst, const volatile void __iomem *src, size_t count)
  187. {
  188. memcpy(dst, (const void __force *)src, count);
  189. }
  190. static inline void
  191. memcpy_toio(volatile void __iomem *dst, const void *src, size_t count)
  192. {
  193. memcpy((void __force *)dst, src, count);
  194. }
  195. /*
  196. * ISA space is 'always mapped' on a typical x86 system, no need to
  197. * explicitly ioremap() it. The fact that the ISA IO space is mapped
  198. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  199. * are physical addresses. The following constant pointer can be
  200. * used as the IO-area pointer (it can be iounmapped as well, so the
  201. * analogy with PCI is quite large):
  202. */
  203. #define __ISA_IO_base ((char __iomem *)(PAGE_OFFSET))
  204. /*
  205. * Cache management
  206. *
  207. * This needed for two cases
  208. * 1. Out of order aware processors
  209. * 2. Accidentally out of order processors (PPro errata #51)
  210. */
  211. static inline void flush_write_buffers(void)
  212. {
  213. #if defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE)
  214. asm volatile("lock; addl $0,0(%%esp)": : :"memory");
  215. #endif
  216. }
  217. #endif /* __KERNEL__ */
  218. extern void native_io_delay(void);
  219. extern int io_delay_type;
  220. extern void io_delay_init(void);
  221. #if defined(CONFIG_PARAVIRT)
  222. #include <asm/paravirt.h>
  223. #else
  224. static inline void slow_down_io(void)
  225. {
  226. native_io_delay();
  227. #ifdef REALLY_SLOW_IO
  228. native_io_delay();
  229. native_io_delay();
  230. native_io_delay();
  231. #endif
  232. }
  233. #endif
  234. #define BUILDIO(bwl, bw, type) \
  235. static inline void out##bwl(unsigned type value, int port) \
  236. { \
  237. asm volatile("out" #bwl " %" #bw "0, %w1" \
  238. : : "a"(value), "Nd"(port)); \
  239. } \
  240. \
  241. static inline unsigned type in##bwl(int port) \
  242. { \
  243. unsigned type value; \
  244. asm volatile("in" #bwl " %w1, %" #bw "0" \
  245. : "=a"(value) : "Nd"(port)); \
  246. return value; \
  247. } \
  248. \
  249. static inline void out##bwl##_p(unsigned type value, int port) \
  250. { \
  251. out##bwl(value, port); \
  252. slow_down_io(); \
  253. } \
  254. \
  255. static inline unsigned type in##bwl##_p(int port) \
  256. { \
  257. unsigned type value = in##bwl(port); \
  258. slow_down_io(); \
  259. return value; \
  260. } \
  261. \
  262. static inline void outs##bwl(int port, const void *addr, unsigned long count) \
  263. { \
  264. asm volatile("rep; outs" #bwl \
  265. : "+S"(addr), "+c"(count) : "d"(port)); \
  266. } \
  267. \
  268. static inline void ins##bwl(int port, void *addr, unsigned long count) \
  269. { \
  270. asm volatile("rep; ins" #bwl \
  271. : "+D"(addr), "+c"(count) : "d"(port)); \
  272. }
  273. BUILDIO(b, b, char)
  274. BUILDIO(w, w, short)
  275. BUILDIO(l, , int)
  276. extern void *xlate_dev_mem_ptr(unsigned long phys);
  277. extern void unxlate_dev_mem_ptr(unsigned long phys, void *addr);
  278. extern int ioremap_change_attr(unsigned long vaddr, unsigned long size,
  279. unsigned long prot_val);
  280. extern void __iomem *ioremap_wc(resource_size_t offset, unsigned long size);
  281. /*
  282. * early_ioremap() and early_iounmap() are for temporary early boot-time
  283. * mappings, before the real ioremap() is functional.
  284. * A boot-time mapping is currently limited to at most 16 pages.
  285. */
  286. extern void early_ioremap_init(void);
  287. extern void early_ioremap_reset(void);
  288. extern void __iomem *early_ioremap(resource_size_t phys_addr,
  289. unsigned long size);
  290. extern void __iomem *early_memremap(resource_size_t phys_addr,
  291. unsigned long size);
  292. extern void early_iounmap(void __iomem *addr, unsigned long size);
  293. extern void fixup_early_ioremap(void);
  294. #define IO_SPACE_LIMIT 0xffff
  295. #endif /* _ASM_X86_IO_H */