io.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #include <xen/xen.h>
  39. #define build_mmio_read(name, size, type, reg, barrier) \
  40. static inline type name(const volatile void __iomem *addr) \
  41. { type ret; asm volatile("mov" size " %1,%0":reg (ret) \
  42. :"m" (*(volatile type __force *)addr) barrier); return ret; }
  43. #define build_mmio_write(name, size, type, reg, barrier) \
  44. static inline void name(type val, volatile void __iomem *addr) \
  45. { asm volatile("mov" size " %0,%1": :reg (val), \
  46. "m" (*(volatile type __force *)addr) barrier); }
  47. build_mmio_read(readb, "b", unsigned char, "=q", :"memory")
  48. build_mmio_read(readw, "w", unsigned short, "=r", :"memory")
  49. build_mmio_read(readl, "l", unsigned int, "=r", :"memory")
  50. build_mmio_read(__readb, "b", unsigned char, "=q", )
  51. build_mmio_read(__readw, "w", unsigned short, "=r", )
  52. build_mmio_read(__readl, "l", unsigned int, "=r", )
  53. build_mmio_write(writeb, "b", unsigned char, "q", :"memory")
  54. build_mmio_write(writew, "w", unsigned short, "r", :"memory")
  55. build_mmio_write(writel, "l", unsigned int, "r", :"memory")
  56. build_mmio_write(__writeb, "b", unsigned char, "q", )
  57. build_mmio_write(__writew, "w", unsigned short, "r", )
  58. build_mmio_write(__writel, "l", unsigned int, "r", )
  59. #define readb_relaxed(a) __readb(a)
  60. #define readw_relaxed(a) __readw(a)
  61. #define readl_relaxed(a) __readl(a)
  62. #define __raw_readb __readb
  63. #define __raw_readw __readw
  64. #define __raw_readl __readl
  65. #define __raw_writeb __writeb
  66. #define __raw_writew __writew
  67. #define __raw_writel __writel
  68. #define mmiowb() barrier()
  69. #ifdef CONFIG_X86_64
  70. build_mmio_read(readq, "q", unsigned long, "=r", :"memory")
  71. build_mmio_write(writeq, "q", unsigned long, "r", :"memory")
  72. #else
  73. static inline __u64 readq(const volatile void __iomem *addr)
  74. {
  75. const volatile u32 __iomem *p = addr;
  76. u32 low, high;
  77. low = readl(p);
  78. high = readl(p + 1);
  79. return low + ((u64)high << 32);
  80. }
  81. static inline void writeq(__u64 val, volatile void __iomem *addr)
  82. {
  83. writel(val, addr);
  84. writel(val >> 32, addr+4);
  85. }
  86. #endif
  87. #define readq_relaxed(a) readq(a)
  88. #define __raw_readq(a) readq(a)
  89. #define __raw_writeq(val, addr) writeq(val, addr)
  90. /* Let people know that we have them */
  91. #define readq readq
  92. #define writeq writeq
  93. /**
  94. * virt_to_phys - map virtual addresses to physical
  95. * @address: address to remap
  96. *
  97. * The returned physical address is the physical (CPU) mapping for
  98. * the memory address given. It is only valid to use this function on
  99. * addresses directly mapped or allocated via kmalloc.
  100. *
  101. * This function does not give bus mappings for DMA transfers. In
  102. * almost all conceivable cases a device driver should not be using
  103. * this function
  104. */
  105. static inline phys_addr_t virt_to_phys(volatile void *address)
  106. {
  107. return __pa(address);
  108. }
  109. /**
  110. * phys_to_virt - map physical address to virtual
  111. * @address: address to remap
  112. *
  113. * The returned virtual address is a current CPU mapping for
  114. * the memory address given. It is only valid to use this function on
  115. * addresses that have a kernel mapping
  116. *
  117. * This function does not handle bus mappings for DMA transfers. In
  118. * almost all conceivable cases a device driver should not be using
  119. * this function
  120. */
  121. static inline void *phys_to_virt(phys_addr_t address)
  122. {
  123. return __va(address);
  124. }
  125. /*
  126. * Change "struct page" to physical address.
  127. */
  128. #define page_to_phys(page) ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
  129. /*
  130. * ISA I/O bus memory addresses are 1:1 with the physical address.
  131. * However, we truncate the address to unsigned int to avoid undesirable
  132. * promitions in legacy drivers.
  133. */
  134. static inline unsigned int isa_virt_to_bus(volatile void *address)
  135. {
  136. return (unsigned int)virt_to_phys(address);
  137. }
  138. #define isa_page_to_bus(page) ((unsigned int)page_to_phys(page))
  139. #define isa_bus_to_virt phys_to_virt
  140. /*
  141. * However PCI ones are not necessarily 1:1 and therefore these interfaces
  142. * are forbidden in portable PCI drivers.
  143. *
  144. * Allow them on x86 for legacy drivers, though.
  145. */
  146. #define virt_to_bus virt_to_phys
  147. #define bus_to_virt phys_to_virt
  148. /**
  149. * ioremap - map bus memory into CPU space
  150. * @offset: bus address of the memory
  151. * @size: size of the resource to map
  152. *
  153. * ioremap performs a platform specific sequence of operations to
  154. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  155. * writew/writel functions and the other mmio helpers. The returned
  156. * address is not guaranteed to be usable directly as a virtual
  157. * address.
  158. *
  159. * If the area you are trying to map is a PCI BAR you should have a
  160. * look at pci_iomap().
  161. */
  162. extern void __iomem *ioremap_nocache(resource_size_t offset, unsigned long size);
  163. extern void __iomem *ioremap_cache(resource_size_t offset, unsigned long size);
  164. extern void __iomem *ioremap_prot(resource_size_t offset, unsigned long size,
  165. unsigned long prot_val);
  166. /*
  167. * The default ioremap() behavior is non-cached:
  168. */
  169. static inline void __iomem *ioremap(resource_size_t offset, unsigned long size)
  170. {
  171. return ioremap_nocache(offset, size);
  172. }
  173. extern void iounmap(volatile void __iomem *addr);
  174. extern void set_iounmap_nonlazy(void);
  175. #ifdef __KERNEL__
  176. #include <asm-generic/iomap.h>
  177. #include <linux/vmalloc.h>
  178. /*
  179. * Convert a virtual cached pointer to an uncached pointer
  180. */
  181. #define xlate_dev_kmem_ptr(p) p
  182. static inline void
  183. memset_io(volatile void __iomem *addr, unsigned char val, size_t count)
  184. {
  185. memset((void __force *)addr, val, count);
  186. }
  187. static inline void
  188. memcpy_fromio(void *dst, const volatile void __iomem *src, size_t count)
  189. {
  190. memcpy(dst, (const void __force *)src, count);
  191. }
  192. static inline void
  193. memcpy_toio(volatile void __iomem *dst, const void *src, size_t count)
  194. {
  195. memcpy((void __force *)dst, src, count);
  196. }
  197. /*
  198. * ISA space is 'always mapped' on a typical x86 system, no need to
  199. * explicitly ioremap() it. The fact that the ISA IO space is mapped
  200. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  201. * are physical addresses. The following constant pointer can be
  202. * used as the IO-area pointer (it can be iounmapped as well, so the
  203. * analogy with PCI is quite large):
  204. */
  205. #define __ISA_IO_base ((char __iomem *)(PAGE_OFFSET))
  206. /*
  207. * Cache management
  208. *
  209. * This needed for two cases
  210. * 1. Out of order aware processors
  211. * 2. Accidentally out of order processors (PPro errata #51)
  212. */
  213. static inline void flush_write_buffers(void)
  214. {
  215. #if defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE)
  216. asm volatile("lock; addl $0,0(%%esp)": : :"memory");
  217. #endif
  218. }
  219. #endif /* __KERNEL__ */
  220. extern void native_io_delay(void);
  221. extern int io_delay_type;
  222. extern void io_delay_init(void);
  223. #if defined(CONFIG_PARAVIRT)
  224. #include <asm/paravirt.h>
  225. #else
  226. static inline void slow_down_io(void)
  227. {
  228. native_io_delay();
  229. #ifdef REALLY_SLOW_IO
  230. native_io_delay();
  231. native_io_delay();
  232. native_io_delay();
  233. #endif
  234. }
  235. #endif
  236. #define BUILDIO(bwl, bw, type) \
  237. static inline void out##bwl(unsigned type value, int port) \
  238. { \
  239. asm volatile("out" #bwl " %" #bw "0, %w1" \
  240. : : "a"(value), "Nd"(port)); \
  241. } \
  242. \
  243. static inline unsigned type in##bwl(int port) \
  244. { \
  245. unsigned type value; \
  246. asm volatile("in" #bwl " %w1, %" #bw "0" \
  247. : "=a"(value) : "Nd"(port)); \
  248. return value; \
  249. } \
  250. \
  251. static inline void out##bwl##_p(unsigned type value, int port) \
  252. { \
  253. out##bwl(value, port); \
  254. slow_down_io(); \
  255. } \
  256. \
  257. static inline unsigned type in##bwl##_p(int port) \
  258. { \
  259. unsigned type value = in##bwl(port); \
  260. slow_down_io(); \
  261. return value; \
  262. } \
  263. \
  264. static inline void outs##bwl(int port, const void *addr, unsigned long count) \
  265. { \
  266. asm volatile("rep; outs" #bwl \
  267. : "+S"(addr), "+c"(count) : "d"(port)); \
  268. } \
  269. \
  270. static inline void ins##bwl(int port, void *addr, unsigned long count) \
  271. { \
  272. asm volatile("rep; ins" #bwl \
  273. : "+D"(addr), "+c"(count) : "d"(port)); \
  274. }
  275. BUILDIO(b, b, char)
  276. BUILDIO(w, w, short)
  277. BUILDIO(l, , int)
  278. extern void *xlate_dev_mem_ptr(unsigned long phys);
  279. extern void unxlate_dev_mem_ptr(unsigned long phys, void *addr);
  280. extern int ioremap_change_attr(unsigned long vaddr, unsigned long size,
  281. unsigned long prot_val);
  282. extern void __iomem *ioremap_wc(resource_size_t offset, unsigned long size);
  283. /*
  284. * early_ioremap() and early_iounmap() are for temporary early boot-time
  285. * mappings, before the real ioremap() is functional.
  286. * A boot-time mapping is currently limited to at most 16 pages.
  287. */
  288. extern void early_ioremap_init(void);
  289. extern void early_ioremap_reset(void);
  290. extern void __iomem *early_ioremap(resource_size_t phys_addr,
  291. unsigned long size);
  292. extern void __iomem *early_memremap(resource_size_t phys_addr,
  293. unsigned long size);
  294. extern void early_iounmap(void __iomem *addr, unsigned long size);
  295. extern void fixup_early_ioremap(void);
  296. extern bool is_early_ioremap_ptep(pte_t *ptep);
  297. #ifdef CONFIG_XEN
  298. struct bio_vec;
  299. extern bool xen_biovec_phys_mergeable(const struct bio_vec *vec1,
  300. const struct bio_vec *vec2);
  301. #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
  302. (__BIOVEC_PHYS_MERGEABLE(vec1, vec2) && \
  303. (!xen_domain() || xen_biovec_phys_mergeable(vec1, vec2)))
  304. #endif /* CONFIG_XEN */
  305. #define IO_SPACE_LIMIT 0xffff
  306. #endif /* _ASM_X86_IO_H */