1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Generic I/O port emulation.
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
7 #ifndef __ASM_GENERIC_IO_H
8 #define __ASM_GENERIC_IO_H
10 #include <asm/page.h> /* I/O is all done through memory accesses */
11 #include <linux/string.h> /* for memset() and memcpy() */
12 #include <linux/types.h>
14 #ifdef CONFIG_GENERIC_IOMAP
15 #include <asm-generic/iomap.h>
18 #include <asm/mmiowb.h>
19 #include <asm-generic/pci_iomap.h>
22 #define __io_br() barrier()
25 /* prevent prefetching of coherent DMA data ahead of a dma-complete */
28 #define __io_ar(v) rmb()
30 #define __io_ar(v) barrier()
34 /* flush writes to coherent DMA data before possibly triggering a DMA read */
37 #define __io_bw() wmb()
39 #define __io_bw() barrier()
43 /* serialize device access against a spin_unlock, usually handled there. */
45 #define __io_aw() mmiowb_set_pending()
49 #define __io_pbw() __io_bw()
53 #define __io_paw() __io_aw()
57 #define __io_pbr() __io_br()
61 #define __io_par(v) __io_ar(v)
66 * __raw_{read,write}{b,w,l,q}() access memory in native endianness.
68 * On some architectures memory mapped IO needs to be accessed differently.
69 * On the simple architectures, we just read/write the memory location
74 #define __raw_readb __raw_readb
75 static inline u8 __raw_readb(const volatile void __iomem *addr)
77 return *(const volatile u8 __force *)addr;
82 #define __raw_readw __raw_readw
83 static inline u16 __raw_readw(const volatile void __iomem *addr)
85 return *(const volatile u16 __force *)addr;
90 #define __raw_readl __raw_readl
91 static inline u32 __raw_readl(const volatile void __iomem *addr)
93 return *(const volatile u32 __force *)addr;
99 #define __raw_readq __raw_readq
100 static inline u64 __raw_readq(const volatile void __iomem *addr)
102 return *(const volatile u64 __force *)addr;
105 #endif /* CONFIG_64BIT */
108 #define __raw_writeb __raw_writeb
109 static inline void __raw_writeb(u8 value, volatile void __iomem *addr)
111 *(volatile u8 __force *)addr = value;
116 #define __raw_writew __raw_writew
117 static inline void __raw_writew(u16 value, volatile void __iomem *addr)
119 *(volatile u16 __force *)addr = value;
124 #define __raw_writel __raw_writel
125 static inline void __raw_writel(u32 value, volatile void __iomem *addr)
127 *(volatile u32 __force *)addr = value;
133 #define __raw_writeq __raw_writeq
134 static inline void __raw_writeq(u64 value, volatile void __iomem *addr)
136 *(volatile u64 __force *)addr = value;
139 #endif /* CONFIG_64BIT */
142 * {read,write}{b,w,l,q}() access little endian memory and return result in
148 static inline u8 readb(const volatile void __iomem *addr)
153 val = __raw_readb(addr);
161 static inline u16 readw(const volatile void __iomem *addr)
166 val = __le16_to_cpu(__raw_readw(addr));
174 static inline u32 readl(const volatile void __iomem *addr)
179 val = __le32_to_cpu(__raw_readl(addr));
188 static inline u64 readq(const volatile void __iomem *addr)
193 val = __le64_to_cpu(__raw_readq(addr));
198 #endif /* CONFIG_64BIT */
201 #define writeb writeb
202 static inline void writeb(u8 value, volatile void __iomem *addr)
205 __raw_writeb(value, addr);
211 #define writew writew
212 static inline void writew(u16 value, volatile void __iomem *addr)
215 __raw_writew(cpu_to_le16(value), addr);
221 #define writel writel
222 static inline void writel(u32 value, volatile void __iomem *addr)
225 __raw_writel(__cpu_to_le32(value), addr);
232 #define writeq writeq
233 static inline void writeq(u64 value, volatile void __iomem *addr)
236 __raw_writeq(__cpu_to_le64(value), addr);
240 #endif /* CONFIG_64BIT */
243 * {read,write}{b,w,l,q}_relaxed() are like the regular version, but
244 * are not guaranteed to provide ordering against spinlocks or memory
247 #ifndef readb_relaxed
248 #define readb_relaxed readb_relaxed
249 static inline u8 readb_relaxed(const volatile void __iomem *addr)
251 return __raw_readb(addr);
255 #ifndef readw_relaxed
256 #define readw_relaxed readw_relaxed
257 static inline u16 readw_relaxed(const volatile void __iomem *addr)
259 return __le16_to_cpu(__raw_readw(addr));
263 #ifndef readl_relaxed
264 #define readl_relaxed readl_relaxed
265 static inline u32 readl_relaxed(const volatile void __iomem *addr)
267 return __le32_to_cpu(__raw_readl(addr));
271 #if defined(readq) && !defined(readq_relaxed)
272 #define readq_relaxed readq_relaxed
273 static inline u64 readq_relaxed(const volatile void __iomem *addr)
275 return __le64_to_cpu(__raw_readq(addr));
279 #ifndef writeb_relaxed
280 #define writeb_relaxed writeb_relaxed
281 static inline void writeb_relaxed(u8 value, volatile void __iomem *addr)
283 __raw_writeb(value, addr);
287 #ifndef writew_relaxed
288 #define writew_relaxed writew_relaxed
289 static inline void writew_relaxed(u16 value, volatile void __iomem *addr)
291 __raw_writew(cpu_to_le16(value), addr);
295 #ifndef writel_relaxed
296 #define writel_relaxed writel_relaxed
297 static inline void writel_relaxed(u32 value, volatile void __iomem *addr)
299 __raw_writel(__cpu_to_le32(value), addr);
303 #if defined(writeq) && !defined(writeq_relaxed)
304 #define writeq_relaxed writeq_relaxed
305 static inline void writeq_relaxed(u64 value, volatile void __iomem *addr)
307 __raw_writeq(__cpu_to_le64(value), addr);
312 * {read,write}s{b,w,l,q}() repeatedly access the same memory address in
313 * native endianness in 8-, 16-, 32- or 64-bit chunks (@count times).
316 #define readsb readsb
317 static inline void readsb(const volatile void __iomem *addr, void *buffer,
324 u8 x = __raw_readb(addr);
332 #define readsw readsw
333 static inline void readsw(const volatile void __iomem *addr, void *buffer,
340 u16 x = __raw_readw(addr);
348 #define readsl readsl
349 static inline void readsl(const volatile void __iomem *addr, void *buffer,
356 u32 x = __raw_readl(addr);
365 #define readsq readsq
366 static inline void readsq(const volatile void __iomem *addr, void *buffer,
373 u64 x = __raw_readq(addr);
379 #endif /* CONFIG_64BIT */
382 #define writesb writesb
383 static inline void writesb(volatile void __iomem *addr, const void *buffer,
387 const u8 *buf = buffer;
390 __raw_writeb(*buf++, addr);
397 #define writesw writesw
398 static inline void writesw(volatile void __iomem *addr, const void *buffer,
402 const u16 *buf = buffer;
405 __raw_writew(*buf++, addr);
412 #define writesl writesl
413 static inline void writesl(volatile void __iomem *addr, const void *buffer,
417 const u32 *buf = buffer;
420 __raw_writel(*buf++, addr);
428 #define writesq writesq
429 static inline void writesq(volatile void __iomem *addr, const void *buffer,
433 const u64 *buf = buffer;
436 __raw_writeq(*buf++, addr);
441 #endif /* CONFIG_64BIT */
444 #define PCI_IOBASE ((void __iomem *)0)
447 #ifndef IO_SPACE_LIMIT
448 #define IO_SPACE_LIMIT 0xffff
451 #include <linux/logic_pio.h>
454 * {in,out}{b,w,l}() access little endian I/O. {in,out}{b,w,l}_p() can be
455 * implemented on hardware that needs an additional delay for I/O accesses to
461 static inline u8 inb(unsigned long addr)
466 val = __raw_readb(PCI_IOBASE + addr);
474 static inline u16 inw(unsigned long addr)
479 val = __le16_to_cpu(__raw_readw(PCI_IOBASE + addr));
487 static inline u32 inl(unsigned long addr)
492 val = __le32_to_cpu(__raw_readl(PCI_IOBASE + addr));
500 static inline void outb(u8 value, unsigned long addr)
503 __raw_writeb(value, PCI_IOBASE + addr);
510 static inline void outw(u16 value, unsigned long addr)
513 __raw_writew(cpu_to_le16(value), PCI_IOBASE + addr);
520 static inline void outl(u32 value, unsigned long addr)
523 __raw_writel(cpu_to_le32(value), PCI_IOBASE + addr);
530 static inline u8 inb_p(unsigned long addr)
538 static inline u16 inw_p(unsigned long addr)
546 static inline u32 inl_p(unsigned long addr)
553 #define outb_p outb_p
554 static inline void outb_p(u8 value, unsigned long addr)
561 #define outw_p outw_p
562 static inline void outw_p(u16 value, unsigned long addr)
569 #define outl_p outl_p
570 static inline void outl_p(u32 value, unsigned long addr)
577 * {in,out}s{b,w,l}{,_p}() are variants of the above that repeatedly access a
578 * single I/O port multiple times.
583 static inline void insb(unsigned long addr, void *buffer, unsigned int count)
585 readsb(PCI_IOBASE + addr, buffer, count);
591 static inline void insw(unsigned long addr, void *buffer, unsigned int count)
593 readsw(PCI_IOBASE + addr, buffer, count);
599 static inline void insl(unsigned long addr, void *buffer, unsigned int count)
601 readsl(PCI_IOBASE + addr, buffer, count);
607 static inline void outsb(unsigned long addr, const void *buffer,
610 writesb(PCI_IOBASE + addr, buffer, count);
616 static inline void outsw(unsigned long addr, const void *buffer,
619 writesw(PCI_IOBASE + addr, buffer, count);
625 static inline void outsl(unsigned long addr, const void *buffer,
628 writesl(PCI_IOBASE + addr, buffer, count);
633 #define insb_p insb_p
634 static inline void insb_p(unsigned long addr, void *buffer, unsigned int count)
636 insb(addr, buffer, count);
641 #define insw_p insw_p
642 static inline void insw_p(unsigned long addr, void *buffer, unsigned int count)
644 insw(addr, buffer, count);
649 #define insl_p insl_p
650 static inline void insl_p(unsigned long addr, void *buffer, unsigned int count)
652 insl(addr, buffer, count);
657 #define outsb_p outsb_p
658 static inline void outsb_p(unsigned long addr, const void *buffer,
661 outsb(addr, buffer, count);
666 #define outsw_p outsw_p
667 static inline void outsw_p(unsigned long addr, const void *buffer,
670 outsw(addr, buffer, count);
675 #define outsl_p outsl_p
676 static inline void outsl_p(unsigned long addr, const void *buffer,
679 outsl(addr, buffer, count);
683 #ifndef CONFIG_GENERIC_IOMAP
685 #define ioread8 ioread8
686 static inline u8 ioread8(const volatile void __iomem *addr)
693 #define ioread16 ioread16
694 static inline u16 ioread16(const volatile void __iomem *addr)
701 #define ioread32 ioread32
702 static inline u32 ioread32(const volatile void __iomem *addr)
710 #define ioread64 ioread64
711 static inline u64 ioread64(const volatile void __iomem *addr)
716 #endif /* CONFIG_64BIT */
719 #define iowrite8 iowrite8
720 static inline void iowrite8(u8 value, volatile void __iomem *addr)
727 #define iowrite16 iowrite16
728 static inline void iowrite16(u16 value, volatile void __iomem *addr)
735 #define iowrite32 iowrite32
736 static inline void iowrite32(u32 value, volatile void __iomem *addr)
744 #define iowrite64 iowrite64
745 static inline void iowrite64(u64 value, volatile void __iomem *addr)
750 #endif /* CONFIG_64BIT */
753 #define ioread16be ioread16be
754 static inline u16 ioread16be(const volatile void __iomem *addr)
756 return swab16(readw(addr));
761 #define ioread32be ioread32be
762 static inline u32 ioread32be(const volatile void __iomem *addr)
764 return swab32(readl(addr));
770 #define ioread64be ioread64be
771 static inline u64 ioread64be(const volatile void __iomem *addr)
773 return swab64(readq(addr));
776 #endif /* CONFIG_64BIT */
779 #define iowrite16be iowrite16be
780 static inline void iowrite16be(u16 value, void volatile __iomem *addr)
782 writew(swab16(value), addr);
787 #define iowrite32be iowrite32be
788 static inline void iowrite32be(u32 value, volatile void __iomem *addr)
790 writel(swab32(value), addr);
796 #define iowrite64be iowrite64be
797 static inline void iowrite64be(u64 value, volatile void __iomem *addr)
799 writeq(swab64(value), addr);
802 #endif /* CONFIG_64BIT */
805 #define ioread8_rep ioread8_rep
806 static inline void ioread8_rep(const volatile void __iomem *addr, void *buffer,
809 readsb(addr, buffer, count);
814 #define ioread16_rep ioread16_rep
815 static inline void ioread16_rep(const volatile void __iomem *addr,
816 void *buffer, unsigned int count)
818 readsw(addr, buffer, count);
823 #define ioread32_rep ioread32_rep
824 static inline void ioread32_rep(const volatile void __iomem *addr,
825 void *buffer, unsigned int count)
827 readsl(addr, buffer, count);
833 #define ioread64_rep ioread64_rep
834 static inline void ioread64_rep(const volatile void __iomem *addr,
835 void *buffer, unsigned int count)
837 readsq(addr, buffer, count);
840 #endif /* CONFIG_64BIT */
843 #define iowrite8_rep iowrite8_rep
844 static inline void iowrite8_rep(volatile void __iomem *addr,
848 writesb(addr, buffer, count);
852 #ifndef iowrite16_rep
853 #define iowrite16_rep iowrite16_rep
854 static inline void iowrite16_rep(volatile void __iomem *addr,
858 writesw(addr, buffer, count);
862 #ifndef iowrite32_rep
863 #define iowrite32_rep iowrite32_rep
864 static inline void iowrite32_rep(volatile void __iomem *addr,
868 writesl(addr, buffer, count);
873 #ifndef iowrite64_rep
874 #define iowrite64_rep iowrite64_rep
875 static inline void iowrite64_rep(volatile void __iomem *addr,
879 writesq(addr, buffer, count);
882 #endif /* CONFIG_64BIT */
883 #endif /* CONFIG_GENERIC_IOMAP */
887 #include <linux/vmalloc.h>
888 #define __io_virt(x) ((void __force *)(x))
890 #ifndef CONFIG_GENERIC_IOMAP
892 extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);
895 #define pci_iounmap pci_iounmap
896 static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
900 #endif /* CONFIG_GENERIC_IOMAP */
903 * Change virtual addresses to physical addresses and vv.
904 * These are pretty trivial
907 #define virt_to_phys virt_to_phys
908 static inline unsigned long virt_to_phys(volatile void *address)
910 return __pa((unsigned long)address);
915 #define phys_to_virt phys_to_virt
916 static inline void *phys_to_virt(unsigned long address)
918 return __va(address);
923 * DOC: ioremap() and ioremap_*() variants
925 * If you have an IOMMU your architecture is expected to have both ioremap()
926 * and iounmap() implemented otherwise the asm-generic helpers will provide a
929 * There are ioremap_*() call variants, if you have no IOMMU we naturally will
930 * default to direct mapping for all of them, you can override these defaults.
931 * If you have an IOMMU you are highly encouraged to provide your own
932 * ioremap variant implementation as there currently is no safe architecture
933 * agnostic default. To avoid possible improper behaviour default asm-generic
934 * ioremap_*() variants all return NULL when an IOMMU is available. If you've
935 * defined your own ioremap_*() variant you must then declare your own
936 * ioremap_*() variant as defined to itself to avoid the default NULL return.
942 #define ioremap_uc ioremap_uc
943 static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
949 #else /* !CONFIG_MMU */
952 * Change "struct page" to physical address.
954 * This implementation is for the no-MMU case only... if you have an MMU
955 * you'll need to provide your own definitions.
959 #define ioremap ioremap
960 static inline void __iomem *ioremap(phys_addr_t offset, size_t size)
962 return (void __iomem *)(unsigned long)offset;
967 #define iounmap iounmap
969 static inline void iounmap(void __iomem *addr)
973 #endif /* CONFIG_MMU */
974 #ifndef ioremap_nocache
975 void __iomem *ioremap(phys_addr_t phys_addr, size_t size);
976 #define ioremap_nocache ioremap_nocache
977 static inline void __iomem *ioremap_nocache(phys_addr_t offset, size_t size)
979 return ioremap(offset, size);
984 #define ioremap_uc ioremap_uc
985 static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
987 return ioremap_nocache(offset, size);
992 #define ioremap_wc ioremap_wc
993 static inline void __iomem *ioremap_wc(phys_addr_t offset, size_t size)
995 return ioremap_nocache(offset, size);
1000 #define ioremap_wt ioremap_wt
1001 static inline void __iomem *ioremap_wt(phys_addr_t offset, size_t size)
1003 return ioremap_nocache(offset, size);
1007 #ifdef CONFIG_HAS_IOPORT_MAP
1008 #ifndef CONFIG_GENERIC_IOMAP
1010 #define ioport_map ioport_map
1011 static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
1013 port &= IO_SPACE_LIMIT;
1014 return (port > MMIO_UPPER_LIMIT) ? NULL : PCI_IOBASE + port;
1018 #ifndef ioport_unmap
1019 #define ioport_unmap ioport_unmap
1020 static inline void ioport_unmap(void __iomem *p)
1024 #else /* CONFIG_GENERIC_IOMAP */
1025 extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
1026 extern void ioport_unmap(void __iomem *p);
1027 #endif /* CONFIG_GENERIC_IOMAP */
1028 #endif /* CONFIG_HAS_IOPORT_MAP */
1031 * Convert a virtual cached pointer to an uncached pointer
1033 #ifndef xlate_dev_kmem_ptr
1034 #define xlate_dev_kmem_ptr xlate_dev_kmem_ptr
1035 static inline void *xlate_dev_kmem_ptr(void *addr)
1041 #ifndef xlate_dev_mem_ptr
1042 #define xlate_dev_mem_ptr xlate_dev_mem_ptr
1043 static inline void *xlate_dev_mem_ptr(phys_addr_t addr)
1049 #ifndef unxlate_dev_mem_ptr
1050 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
1051 static inline void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
1056 #ifdef CONFIG_VIRT_TO_BUS
1058 static inline unsigned long virt_to_bus(void *address)
1060 return (unsigned long)address;
1063 static inline void *bus_to_virt(unsigned long address)
1065 return (void *)address;
1071 #define memset_io memset_io
1073 * memset_io Set a range of I/O memory to a constant value
1074 * @addr: The beginning of the I/O-memory range to set
1075 * @val: The value to set the memory to
1076 * @count: The number of bytes to set
1078 * Set a range of I/O memory to a given value.
1080 static inline void memset_io(volatile void __iomem *addr, int value,
1083 memset(__io_virt(addr), value, size);
1087 #ifndef memcpy_fromio
1088 #define memcpy_fromio memcpy_fromio
1090 * memcpy_fromio Copy a block of data from I/O memory
1091 * @dst: The (RAM) destination for the copy
1092 * @src: The (I/O memory) source for the data
1093 * @count: The number of bytes to copy
1095 * Copy a block of data from I/O memory.
1097 static inline void memcpy_fromio(void *buffer,
1098 const volatile void __iomem *addr,
1101 memcpy(buffer, __io_virt(addr), size);
1106 #define memcpy_toio memcpy_toio
1108 * memcpy_toio Copy a block of data into I/O memory
1109 * @dst: The (I/O memory) destination for the copy
1110 * @src: The (RAM) source for the data
1111 * @count: The number of bytes to copy
1113 * Copy a block of data to I/O memory.
1115 static inline void memcpy_toio(volatile void __iomem *addr, const void *buffer,
1118 memcpy(__io_virt(addr), buffer, size);
1122 #endif /* __KERNEL__ */
1124 #endif /* __ASM_GENERIC_IO_H */