io.h 12 KB

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