io.h 11 KB

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