io.h 10 KB

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