io.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_IO_H
  3. #define _ASM_X86_IO_H
  4. /*
  5. * This file contains the definitions for the x86 IO instructions
  6. * inb/inw/inl/outb/outw/outl and the "string versions" of the same
  7. * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
  8. * versions of the single-IO instructions (inb_p/inw_p/..).
  9. *
  10. * This file is not meant to be obfuscating: it's just complicated
  11. * to (a) handle it all in a way that makes gcc able to optimize it
  12. * as well as possible and (b) trying to avoid writing the same thing
  13. * over and over again with slight variations and possibly making a
  14. * mistake somewhere.
  15. */
  16. /*
  17. * Thanks to James van Artsdalen for a better timing-fix than
  18. * the two short jumps: using outb's to a nonexistent port seems
  19. * to guarantee better timings even on fast machines.
  20. *
  21. * On the other hand, I'd like to be sure of a non-existent port:
  22. * I feel a bit unsafe about using 0x80 (should be safe, though)
  23. *
  24. * Linus
  25. */
  26. /*
  27. * Bit simplified and optimized by Jan Hubicka
  28. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999.
  29. *
  30. * isa_memset_io, isa_memcpy_fromio, isa_memcpy_toio added,
  31. * isa_read[wl] and isa_write[wl] fixed
  32. * - Arnaldo Carvalho de Melo <[email protected]>
  33. */
  34. #define ARCH_HAS_IOREMAP_WC
  35. #define ARCH_HAS_IOREMAP_WT
  36. #include <linux/string.h>
  37. #include <linux/compiler.h>
  38. #include <linux/cc_platform.h>
  39. #include <asm/page.h>
  40. #include <asm/early_ioremap.h>
  41. #include <asm/pgtable_types.h>
  42. #include <asm/shared/io.h>
  43. #define build_mmio_read(name, size, type, reg, barrier) \
  44. static inline type name(const volatile void __iomem *addr) \
  45. { type ret; asm volatile("mov" size " %1,%0":reg (ret) \
  46. :"m" (*(volatile type __force *)addr) barrier); return ret; }
  47. #define build_mmio_write(name, size, type, reg, barrier) \
  48. static inline void name(type val, volatile void __iomem *addr) \
  49. { asm volatile("mov" size " %0,%1": :reg (val), \
  50. "m" (*(volatile type __force *)addr) barrier); }
  51. build_mmio_read(readb, "b", unsigned char, "=q", :"memory")
  52. build_mmio_read(readw, "w", unsigned short, "=r", :"memory")
  53. build_mmio_read(readl, "l", unsigned int, "=r", :"memory")
  54. build_mmio_read(__readb, "b", unsigned char, "=q", )
  55. build_mmio_read(__readw, "w", unsigned short, "=r", )
  56. build_mmio_read(__readl, "l", unsigned int, "=r", )
  57. build_mmio_write(writeb, "b", unsigned char, "q", :"memory")
  58. build_mmio_write(writew, "w", unsigned short, "r", :"memory")
  59. build_mmio_write(writel, "l", unsigned int, "r", :"memory")
  60. build_mmio_write(__writeb, "b", unsigned char, "q", )
  61. build_mmio_write(__writew, "w", unsigned short, "r", )
  62. build_mmio_write(__writel, "l", unsigned int, "r", )
  63. #define readb readb
  64. #define readw readw
  65. #define readl readl
  66. #define readb_relaxed(a) __readb(a)
  67. #define readw_relaxed(a) __readw(a)
  68. #define readl_relaxed(a) __readl(a)
  69. #define __raw_readb __readb
  70. #define __raw_readw __readw
  71. #define __raw_readl __readl
  72. #define writeb writeb
  73. #define writew writew
  74. #define writel writel
  75. #define writeb_relaxed(v, a) __writeb(v, a)
  76. #define writew_relaxed(v, a) __writew(v, a)
  77. #define writel_relaxed(v, a) __writel(v, a)
  78. #define __raw_writeb __writeb
  79. #define __raw_writew __writew
  80. #define __raw_writel __writel
  81. #ifdef CONFIG_X86_64
  82. build_mmio_read(readq, "q", u64, "=r", :"memory")
  83. build_mmio_read(__readq, "q", u64, "=r", )
  84. build_mmio_write(writeq, "q", u64, "r", :"memory")
  85. build_mmio_write(__writeq, "q", u64, "r", )
  86. #define readq_relaxed(a) __readq(a)
  87. #define writeq_relaxed(v, a) __writeq(v, a)
  88. #define __raw_readq __readq
  89. #define __raw_writeq __writeq
  90. /* Let people know that we have them */
  91. #define readq readq
  92. #define writeq writeq
  93. #endif
  94. #define ARCH_HAS_VALID_PHYS_ADDR_RANGE
  95. extern int valid_phys_addr_range(phys_addr_t addr, size_t size);
  96. extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
  97. /**
  98. * virt_to_phys - map virtual addresses to physical
  99. * @address: address to remap
  100. *
  101. * The returned physical address is the physical (CPU) mapping for
  102. * the memory address given. It is only valid to use this function on
  103. * addresses directly mapped or allocated via kmalloc.
  104. *
  105. * This function does not give bus mappings for DMA transfers. In
  106. * almost all conceivable cases a device driver should not be using
  107. * this function
  108. */
  109. static inline phys_addr_t virt_to_phys(volatile void *address)
  110. {
  111. return __pa(address);
  112. }
  113. #define virt_to_phys virt_to_phys
  114. /**
  115. * phys_to_virt - map physical address to virtual
  116. * @address: address to remap
  117. *
  118. * The returned virtual address is a current CPU mapping for
  119. * the memory address given. It is only valid to use this function on
  120. * addresses that have a kernel mapping
  121. *
  122. * This function does not handle bus mappings for DMA transfers. In
  123. * almost all conceivable cases a device driver should not be using
  124. * this function
  125. */
  126. static inline void *phys_to_virt(phys_addr_t address)
  127. {
  128. return __va(address);
  129. }
  130. #define phys_to_virt phys_to_virt
  131. /*
  132. * Change "struct page" to physical address.
  133. */
  134. #define page_to_phys(page) ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
  135. /*
  136. * ISA I/O bus memory addresses are 1:1 with the physical address.
  137. * However, we truncate the address to unsigned int to avoid undesirable
  138. * promotions in legacy drivers.
  139. */
  140. static inline unsigned int isa_virt_to_bus(volatile void *address)
  141. {
  142. return (unsigned int)virt_to_phys(address);
  143. }
  144. #define isa_bus_to_virt phys_to_virt
  145. /*
  146. * The default ioremap() behavior is non-cached; if you need something
  147. * else, you probably want one of the following.
  148. */
  149. extern void __iomem *ioremap_uc(resource_size_t offset, unsigned long size);
  150. #define ioremap_uc ioremap_uc
  151. extern void __iomem *ioremap_cache(resource_size_t offset, unsigned long size);
  152. #define ioremap_cache ioremap_cache
  153. extern void __iomem *ioremap_prot(resource_size_t offset, unsigned long size, unsigned long prot_val);
  154. #define ioremap_prot ioremap_prot
  155. extern void __iomem *ioremap_encrypted(resource_size_t phys_addr, unsigned long size);
  156. #define ioremap_encrypted ioremap_encrypted
  157. /**
  158. * ioremap - map bus memory into CPU space
  159. * @offset: bus address of the memory
  160. * @size: size of the resource to map
  161. *
  162. * ioremap performs a platform specific sequence of operations to
  163. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  164. * writew/writel functions and the other mmio helpers. The returned
  165. * address is not guaranteed to be usable directly as a virtual
  166. * address.
  167. *
  168. * If the area you are trying to map is a PCI BAR you should have a
  169. * look at pci_iomap().
  170. */
  171. void __iomem *ioremap(resource_size_t offset, unsigned long size);
  172. #define ioremap ioremap
  173. extern void iounmap(volatile void __iomem *addr);
  174. #define iounmap iounmap
  175. #ifdef __KERNEL__
  176. void memcpy_fromio(void *, const volatile void __iomem *, size_t);
  177. void memcpy_toio(volatile void __iomem *, const void *, size_t);
  178. void memset_io(volatile void __iomem *, int, size_t);
  179. #define memcpy_fromio memcpy_fromio
  180. #define memcpy_toio memcpy_toio
  181. #define memset_io memset_io
  182. #include <asm-generic/iomap.h>
  183. /*
  184. * ISA space is 'always mapped' on a typical x86 system, no need to
  185. * explicitly ioremap() it. The fact that the ISA IO space is mapped
  186. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  187. * are physical addresses. The following constant pointer can be
  188. * used as the IO-area pointer (it can be iounmapped as well, so the
  189. * analogy with PCI is quite large):
  190. */
  191. #define __ISA_IO_base ((char __iomem *)(PAGE_OFFSET))
  192. #endif /* __KERNEL__ */
  193. extern void native_io_delay(void);
  194. extern int io_delay_type;
  195. extern void io_delay_init(void);
  196. #if defined(CONFIG_PARAVIRT)
  197. #include <asm/paravirt.h>
  198. #else
  199. static inline void slow_down_io(void)
  200. {
  201. native_io_delay();
  202. #ifdef REALLY_SLOW_IO
  203. native_io_delay();
  204. native_io_delay();
  205. native_io_delay();
  206. #endif
  207. }
  208. #endif
  209. #define BUILDIO(bwl, bw, type) \
  210. static inline void out##bwl##_p(type value, u16 port) \
  211. { \
  212. out##bwl(value, port); \
  213. slow_down_io(); \
  214. } \
  215. \
  216. static inline type in##bwl##_p(u16 port) \
  217. { \
  218. type value = in##bwl(port); \
  219. slow_down_io(); \
  220. return value; \
  221. } \
  222. \
  223. static inline void outs##bwl(u16 port, const void *addr, unsigned long count) \
  224. { \
  225. if (cc_platform_has(CC_ATTR_GUEST_UNROLL_STRING_IO)) { \
  226. type *value = (type *)addr; \
  227. while (count) { \
  228. out##bwl(*value, port); \
  229. value++; \
  230. count--; \
  231. } \
  232. } else { \
  233. asm volatile("rep; outs" #bwl \
  234. : "+S"(addr), "+c"(count) \
  235. : "d"(port) : "memory"); \
  236. } \
  237. } \
  238. \
  239. static inline void ins##bwl(u16 port, void *addr, unsigned long count) \
  240. { \
  241. if (cc_platform_has(CC_ATTR_GUEST_UNROLL_STRING_IO)) { \
  242. type *value = (type *)addr; \
  243. while (count) { \
  244. *value = in##bwl(port); \
  245. value++; \
  246. count--; \
  247. } \
  248. } else { \
  249. asm volatile("rep; ins" #bwl \
  250. : "+D"(addr), "+c"(count) \
  251. : "d"(port) : "memory"); \
  252. } \
  253. }
  254. BUILDIO(b, b, u8)
  255. BUILDIO(w, w, u16)
  256. BUILDIO(l, , u32)
  257. #undef BUILDIO
  258. #define inb_p inb_p
  259. #define inw_p inw_p
  260. #define inl_p inl_p
  261. #define insb insb
  262. #define insw insw
  263. #define insl insl
  264. #define outb_p outb_p
  265. #define outw_p outw_p
  266. #define outl_p outl_p
  267. #define outsb outsb
  268. #define outsw outsw
  269. #define outsl outsl
  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. #define xlate_dev_mem_ptr xlate_dev_mem_ptr
  273. #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
  274. extern int ioremap_change_attr(unsigned long vaddr, unsigned long size,
  275. enum page_cache_mode pcm);
  276. extern void __iomem *ioremap_wc(resource_size_t offset, unsigned long size);
  277. #define ioremap_wc ioremap_wc
  278. extern void __iomem *ioremap_wt(resource_size_t offset, unsigned long size);
  279. #define ioremap_wt ioremap_wt
  280. extern bool is_early_ioremap_ptep(pte_t *ptep);
  281. #define IO_SPACE_LIMIT 0xffff
  282. #include <asm-generic/io.h>
  283. #undef PCI_IOBASE
  284. #ifdef CONFIG_MTRR
  285. extern int __must_check arch_phys_wc_index(int handle);
  286. #define arch_phys_wc_index arch_phys_wc_index
  287. extern int __must_check arch_phys_wc_add(unsigned long base,
  288. unsigned long size);
  289. extern void arch_phys_wc_del(int handle);
  290. #define arch_phys_wc_add arch_phys_wc_add
  291. #endif
  292. #ifdef CONFIG_X86_PAT
  293. extern int arch_io_reserve_memtype_wc(resource_size_t start, resource_size_t size);
  294. extern void arch_io_free_memtype_wc(resource_size_t start, resource_size_t size);
  295. #define arch_io_reserve_memtype_wc arch_io_reserve_memtype_wc
  296. #endif
  297. #ifdef CONFIG_AMD_MEM_ENCRYPT
  298. extern bool arch_memremap_can_ram_remap(resource_size_t offset,
  299. unsigned long size,
  300. unsigned long flags);
  301. #define arch_memremap_can_ram_remap arch_memremap_can_ram_remap
  302. extern bool phys_mem_access_encrypted(unsigned long phys_addr,
  303. unsigned long size);
  304. #else
  305. static inline bool phys_mem_access_encrypted(unsigned long phys_addr,
  306. unsigned long size)
  307. {
  308. return true;
  309. }
  310. #endif
  311. /**
  312. * iosubmit_cmds512 - copy data to single MMIO location, in 512-bit units
  313. * @dst: destination, in MMIO space (must be 512-bit aligned)
  314. * @src: source
  315. * @count: number of 512 bits quantities to submit
  316. *
  317. * Submit data from kernel space to MMIO space, in units of 512 bits at a
  318. * time. Order of access is not guaranteed, nor is a memory barrier
  319. * performed afterwards.
  320. *
  321. * Warning: Do not use this helper unless your driver has checked that the CPU
  322. * instruction is supported on the platform.
  323. */
  324. static inline void iosubmit_cmds512(void __iomem *dst, const void *src,
  325. size_t count)
  326. {
  327. const u8 *from = src;
  328. const u8 *end = from + count * 64;
  329. while (from < end) {
  330. movdir64b(dst, from);
  331. from += 64;
  332. }
  333. }
  334. #endif /* _ASM_X86_IO_H */