1 /* Generic I/O port emulation.
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
11 #ifndef __ASM_GENERIC_IO_H
12 #define __ASM_GENERIC_IO_H
14 #include <asm/page.h> /* I/O is all done through memory accesses */
15 #include <linux/string.h> /* for memset() and memcpy() */
16 #include <linux/types.h>
18 #ifdef CONFIG_GENERIC_IOMAP
19 #include <asm-generic/iomap.h>
22 #include <asm-generic/pci_iomap.h>
25 #define mmiowb() do {} while (0)
29 #define __io_br() barrier()
32 /* prevent prefetching of coherent DMA data ahead of a dma-complete */
35 #define __io_ar(v) rmb()
37 #define __io_ar(v) barrier()
41 /* flush writes to coherent DMA data before possibly triggering a DMA read */
44 #define __io_bw() wmb()
46 #define __io_bw() barrier()
50 /* serialize device access against a spin_unlock, usually handled there. */
52 #define __io_aw() barrier()
56 #define __io_pbw() __io_bw()
60 #define __io_paw() __io_aw()
64 #define __io_pbr() __io_br()
68 #define __io_par(v) __io_ar(v)
73 * __raw_{read,write}{b,w,l,q}() access memory in native endianness.
75 * On some architectures memory mapped IO needs to be accessed differently.
76 * On the simple architectures, we just read/write the memory location
81 #define __raw_readb __raw_readb
82 static inline u8
__raw_readb(const volatile void __iomem
*addr
)
84 return *(const volatile u8 __force
*)addr
;
89 #define __raw_readw __raw_readw
90 static inline u16
__raw_readw(const volatile void __iomem
*addr
)
92 return *(const volatile u16 __force
*)addr
;
97 #define __raw_readl __raw_readl
98 static inline u32
__raw_readl(const volatile void __iomem
*addr
)
100 return *(const volatile u32 __force
*)addr
;
106 #define __raw_readq __raw_readq
107 static inline u64
__raw_readq(const volatile void __iomem
*addr
)
109 return *(const volatile u64 __force
*)addr
;
112 #endif /* CONFIG_64BIT */
115 #define __raw_writeb __raw_writeb
116 static inline void __raw_writeb(u8 value
, volatile void __iomem
*addr
)
118 *(volatile u8 __force
*)addr
= value
;
123 #define __raw_writew __raw_writew
124 static inline void __raw_writew(u16 value
, volatile void __iomem
*addr
)
126 *(volatile u16 __force
*)addr
= value
;
131 #define __raw_writel __raw_writel
132 static inline void __raw_writel(u32 value
, volatile void __iomem
*addr
)
134 *(volatile u32 __force
*)addr
= value
;
140 #define __raw_writeq __raw_writeq
141 static inline void __raw_writeq(u64 value
, volatile void __iomem
*addr
)
143 *(volatile u64 __force
*)addr
= value
;
146 #endif /* CONFIG_64BIT */
149 * {read,write}{b,w,l,q}() access little endian memory and return result in
155 static inline u8
readb(const volatile void __iomem
*addr
)
160 val
= __raw_readb(addr
);
168 static inline u16
readw(const volatile void __iomem
*addr
)
173 val
= __le16_to_cpu(__raw_readw(addr
));
181 static inline u32
readl(const volatile void __iomem
*addr
)
186 val
= __le32_to_cpu(__raw_readl(addr
));
195 static inline u64
readq(const volatile void __iomem
*addr
)
200 val
= __le64_to_cpu(__raw_readq(addr
));
205 #endif /* CONFIG_64BIT */
208 #define writeb writeb
209 static inline void writeb(u8 value
, volatile void __iomem
*addr
)
212 __raw_writeb(value
, addr
);
218 #define writew writew
219 static inline void writew(u16 value
, volatile void __iomem
*addr
)
222 __raw_writew(cpu_to_le16(value
), addr
);
228 #define writel writel
229 static inline void writel(u32 value
, volatile void __iomem
*addr
)
232 __raw_writel(__cpu_to_le32(value
), addr
);
239 #define writeq writeq
240 static inline void writeq(u64 value
, volatile void __iomem
*addr
)
243 __raw_writeq(__cpu_to_le64(value
), addr
);
247 #endif /* CONFIG_64BIT */
250 * {read,write}{b,w,l,q}_relaxed() are like the regular version, but
251 * are not guaranteed to provide ordering against spinlocks or memory
254 #ifndef readb_relaxed
255 #define readb_relaxed readb_relaxed
256 static inline u8
readb_relaxed(const volatile void __iomem
*addr
)
258 return __raw_readb(addr
);
262 #ifndef readw_relaxed
263 #define readw_relaxed readw_relaxed
264 static inline u16
readw_relaxed(const volatile void __iomem
*addr
)
266 return __le16_to_cpu(__raw_readw(addr
));
270 #ifndef readl_relaxed
271 #define readl_relaxed readl_relaxed
272 static inline u32
readl_relaxed(const volatile void __iomem
*addr
)
274 return __le32_to_cpu(__raw_readl(addr
));
278 #if defined(readq) && !defined(readq_relaxed)
279 #define readq_relaxed readq_relaxed
280 static inline u64
readq_relaxed(const volatile void __iomem
*addr
)
282 return __le64_to_cpu(__raw_readq(addr
));
286 #ifndef writeb_relaxed
287 #define writeb_relaxed writeb_relaxed
288 static inline void writeb_relaxed(u8 value
, volatile void __iomem
*addr
)
290 __raw_writeb(value
, addr
);
294 #ifndef writew_relaxed
295 #define writew_relaxed writew_relaxed
296 static inline void writew_relaxed(u16 value
, volatile void __iomem
*addr
)
298 __raw_writew(cpu_to_le16(value
), addr
);
302 #ifndef writel_relaxed
303 #define writel_relaxed writel_relaxed
304 static inline void writel_relaxed(u32 value
, volatile void __iomem
*addr
)
306 __raw_writel(__cpu_to_le32(value
), addr
);
310 #if defined(writeq) && !defined(writeq_relaxed)
311 #define writeq_relaxed writeq_relaxed
312 static inline void writeq_relaxed(u64 value
, volatile void __iomem
*addr
)
314 __raw_writeq(__cpu_to_le64(value
), addr
);
319 * {read,write}s{b,w,l,q}() repeatedly access the same memory address in
320 * native endianness in 8-, 16-, 32- or 64-bit chunks (@count times).
323 #define readsb readsb
324 static inline void readsb(const volatile void __iomem
*addr
, void *buffer
,
331 u8 x
= __raw_readb(addr
);
339 #define readsw readsw
340 static inline void readsw(const volatile void __iomem
*addr
, void *buffer
,
347 u16 x
= __raw_readw(addr
);
355 #define readsl readsl
356 static inline void readsl(const volatile void __iomem
*addr
, void *buffer
,
363 u32 x
= __raw_readl(addr
);
372 #define readsq readsq
373 static inline void readsq(const volatile void __iomem
*addr
, void *buffer
,
380 u64 x
= __raw_readq(addr
);
386 #endif /* CONFIG_64BIT */
389 #define writesb writesb
390 static inline void writesb(volatile void __iomem
*addr
, const void *buffer
,
394 const u8
*buf
= buffer
;
397 __raw_writeb(*buf
++, addr
);
404 #define writesw writesw
405 static inline void writesw(volatile void __iomem
*addr
, const void *buffer
,
409 const u16
*buf
= buffer
;
412 __raw_writew(*buf
++, addr
);
419 #define writesl writesl
420 static inline void writesl(volatile void __iomem
*addr
, const void *buffer
,
424 const u32
*buf
= buffer
;
427 __raw_writel(*buf
++, addr
);
435 #define writesq writesq
436 static inline void writesq(volatile void __iomem
*addr
, const void *buffer
,
440 const u64
*buf
= buffer
;
443 __raw_writeq(*buf
++, addr
);
448 #endif /* CONFIG_64BIT */
451 #define PCI_IOBASE ((void __iomem *)0)
454 #ifndef IO_SPACE_LIMIT
455 #define IO_SPACE_LIMIT 0xffff
458 #include <linux/logic_pio.h>
461 * {in,out}{b,w,l}() access little endian I/O. {in,out}{b,w,l}_p() can be
462 * implemented on hardware that needs an additional delay for I/O accesses to
468 static inline u8
inb(unsigned long addr
)
473 val
= __raw_readb(PCI_IOBASE
+ addr
);
481 static inline u16
inw(unsigned long addr
)
486 val
= __le16_to_cpu(__raw_readw(PCI_IOBASE
+ addr
));
494 static inline u32
inl(unsigned long addr
)
499 val
= __le32_to_cpu(__raw_readl(PCI_IOBASE
+ addr
));
507 static inline void outb(u8 value
, unsigned long addr
)
510 __raw_writeb(value
, PCI_IOBASE
+ addr
);
517 static inline void outw(u16 value
, unsigned long addr
)
520 __raw_writew(cpu_to_le16(value
), PCI_IOBASE
+ addr
);
527 static inline void outl(u32 value
, unsigned long addr
)
530 __raw_writel(cpu_to_le32(value
), PCI_IOBASE
+ addr
);
537 static inline u8
inb_p(unsigned long addr
)
545 static inline u16
inw_p(unsigned long addr
)
553 static inline u32
inl_p(unsigned long addr
)
560 #define outb_p outb_p
561 static inline void outb_p(u8 value
, unsigned long addr
)
568 #define outw_p outw_p
569 static inline void outw_p(u16 value
, unsigned long addr
)
576 #define outl_p outl_p
577 static inline void outl_p(u32 value
, unsigned long addr
)
584 * {in,out}s{b,w,l}{,_p}() are variants of the above that repeatedly access a
585 * single I/O port multiple times.
590 static inline void insb(unsigned long addr
, void *buffer
, unsigned int count
)
592 readsb(PCI_IOBASE
+ addr
, buffer
, count
);
598 static inline void insw(unsigned long addr
, void *buffer
, unsigned int count
)
600 readsw(PCI_IOBASE
+ addr
, buffer
, count
);
606 static inline void insl(unsigned long addr
, void *buffer
, unsigned int count
)
608 readsl(PCI_IOBASE
+ addr
, buffer
, count
);
614 static inline void outsb(unsigned long addr
, const void *buffer
,
617 writesb(PCI_IOBASE
+ addr
, buffer
, count
);
623 static inline void outsw(unsigned long addr
, const void *buffer
,
626 writesw(PCI_IOBASE
+ addr
, buffer
, count
);
632 static inline void outsl(unsigned long addr
, const void *buffer
,
635 writesl(PCI_IOBASE
+ addr
, buffer
, count
);
640 #define insb_p insb_p
641 static inline void insb_p(unsigned long addr
, void *buffer
, unsigned int count
)
643 insb(addr
, buffer
, count
);
648 #define insw_p insw_p
649 static inline void insw_p(unsigned long addr
, void *buffer
, unsigned int count
)
651 insw(addr
, buffer
, count
);
656 #define insl_p insl_p
657 static inline void insl_p(unsigned long addr
, void *buffer
, unsigned int count
)
659 insl(addr
, buffer
, count
);
664 #define outsb_p outsb_p
665 static inline void outsb_p(unsigned long addr
, const void *buffer
,
668 outsb(addr
, buffer
, count
);
673 #define outsw_p outsw_p
674 static inline void outsw_p(unsigned long addr
, const void *buffer
,
677 outsw(addr
, buffer
, count
);
682 #define outsl_p outsl_p
683 static inline void outsl_p(unsigned long addr
, const void *buffer
,
686 outsl(addr
, buffer
, count
);
690 #ifndef CONFIG_GENERIC_IOMAP
692 #define ioread8 ioread8
693 static inline u8
ioread8(const volatile void __iomem
*addr
)
700 #define ioread16 ioread16
701 static inline u16
ioread16(const volatile void __iomem
*addr
)
708 #define ioread32 ioread32
709 static inline u32
ioread32(const volatile void __iomem
*addr
)
717 #define ioread64 ioread64
718 static inline u64
ioread64(const volatile void __iomem
*addr
)
723 #endif /* CONFIG_64BIT */
726 #define iowrite8 iowrite8
727 static inline void iowrite8(u8 value
, volatile void __iomem
*addr
)
734 #define iowrite16 iowrite16
735 static inline void iowrite16(u16 value
, volatile void __iomem
*addr
)
742 #define iowrite32 iowrite32
743 static inline void iowrite32(u32 value
, volatile void __iomem
*addr
)
751 #define iowrite64 iowrite64
752 static inline void iowrite64(u64 value
, volatile void __iomem
*addr
)
757 #endif /* CONFIG_64BIT */
760 #define ioread16be ioread16be
761 static inline u16
ioread16be(const volatile void __iomem
*addr
)
763 return swab16(readw(addr
));
768 #define ioread32be ioread32be
769 static inline u32
ioread32be(const volatile void __iomem
*addr
)
771 return swab32(readl(addr
));
777 #define ioread64be ioread64be
778 static inline u64
ioread64be(const volatile void __iomem
*addr
)
780 return swab64(readq(addr
));
783 #endif /* CONFIG_64BIT */
786 #define iowrite16be iowrite16be
787 static inline void iowrite16be(u16 value
, void volatile __iomem
*addr
)
789 writew(swab16(value
), addr
);
794 #define iowrite32be iowrite32be
795 static inline void iowrite32be(u32 value
, volatile void __iomem
*addr
)
797 writel(swab32(value
), addr
);
803 #define iowrite64be iowrite64be
804 static inline void iowrite64be(u64 value
, volatile void __iomem
*addr
)
806 writeq(swab64(value
), addr
);
809 #endif /* CONFIG_64BIT */
812 #define ioread8_rep ioread8_rep
813 static inline void ioread8_rep(const volatile void __iomem
*addr
, void *buffer
,
816 readsb(addr
, buffer
, count
);
821 #define ioread16_rep ioread16_rep
822 static inline void ioread16_rep(const volatile void __iomem
*addr
,
823 void *buffer
, unsigned int count
)
825 readsw(addr
, buffer
, count
);
830 #define ioread32_rep ioread32_rep
831 static inline void ioread32_rep(const volatile void __iomem
*addr
,
832 void *buffer
, unsigned int count
)
834 readsl(addr
, buffer
, count
);
840 #define ioread64_rep ioread64_rep
841 static inline void ioread64_rep(const volatile void __iomem
*addr
,
842 void *buffer
, unsigned int count
)
844 readsq(addr
, buffer
, count
);
847 #endif /* CONFIG_64BIT */
850 #define iowrite8_rep iowrite8_rep
851 static inline void iowrite8_rep(volatile void __iomem
*addr
,
855 writesb(addr
, buffer
, count
);
859 #ifndef iowrite16_rep
860 #define iowrite16_rep iowrite16_rep
861 static inline void iowrite16_rep(volatile void __iomem
*addr
,
865 writesw(addr
, buffer
, count
);
869 #ifndef iowrite32_rep
870 #define iowrite32_rep iowrite32_rep
871 static inline void iowrite32_rep(volatile void __iomem
*addr
,
875 writesl(addr
, buffer
, count
);
880 #ifndef iowrite64_rep
881 #define iowrite64_rep iowrite64_rep
882 static inline void iowrite64_rep(volatile void __iomem
*addr
,
886 writesq(addr
, buffer
, count
);
889 #endif /* CONFIG_64BIT */
890 #endif /* CONFIG_GENERIC_IOMAP */
894 #include <linux/vmalloc.h>
895 #define __io_virt(x) ((void __force *)(x))
897 #ifndef CONFIG_GENERIC_IOMAP
899 extern void __iomem
*pci_iomap(struct pci_dev
*dev
, int bar
, unsigned long max
);
902 #define pci_iounmap pci_iounmap
903 static inline void pci_iounmap(struct pci_dev
*dev
, void __iomem
*p
)
907 #endif /* CONFIG_GENERIC_IOMAP */
910 * Change virtual addresses to physical addresses and vv.
911 * These are pretty trivial
914 #define virt_to_phys virt_to_phys
915 static inline unsigned long virt_to_phys(volatile void *address
)
917 return __pa((unsigned long)address
);
922 #define phys_to_virt phys_to_virt
923 static inline void *phys_to_virt(unsigned long address
)
925 return __va(address
);
930 * DOC: ioremap() and ioremap_*() variants
932 * If you have an IOMMU your architecture is expected to have both ioremap()
933 * and iounmap() implemented otherwise the asm-generic helpers will provide a
936 * There are ioremap_*() call variants, if you have no IOMMU we naturally will
937 * default to direct mapping for all of them, you can override these defaults.
938 * If you have an IOMMU you are highly encouraged to provide your own
939 * ioremap variant implementation as there currently is no safe architecture
940 * agnostic default. To avoid possible improper behaviour default asm-generic
941 * ioremap_*() variants all return NULL when an IOMMU is available. If you've
942 * defined your own ioremap_*() variant you must then declare your own
943 * ioremap_*() variant as defined to itself to avoid the default NULL return.
949 #define ioremap_uc ioremap_uc
950 static inline void __iomem
*ioremap_uc(phys_addr_t offset
, size_t size
)
956 #else /* !CONFIG_MMU */
959 * Change "struct page" to physical address.
961 * This implementation is for the no-MMU case only... if you have an MMU
962 * you'll need to provide your own definitions.
966 #define ioremap ioremap
967 static inline void __iomem
*ioremap(phys_addr_t offset
, size_t size
)
969 return (void __iomem
*)(unsigned long)offset
;
974 #define __ioremap __ioremap
975 static inline void __iomem
*__ioremap(phys_addr_t offset
, size_t size
,
978 return ioremap(offset
, size
);
983 #define iounmap iounmap
985 static inline void iounmap(void __iomem
*addr
)
989 #endif /* CONFIG_MMU */
990 #ifndef ioremap_nocache
991 void __iomem
*ioremap(phys_addr_t phys_addr
, size_t size
);
992 #define ioremap_nocache ioremap_nocache
993 static inline void __iomem
*ioremap_nocache(phys_addr_t offset
, size_t size
)
995 return ioremap(offset
, size
);
1000 #define ioremap_uc ioremap_uc
1001 static inline void __iomem
*ioremap_uc(phys_addr_t offset
, size_t size
)
1003 return ioremap_nocache(offset
, size
);
1008 #define ioremap_wc ioremap_wc
1009 static inline void __iomem
*ioremap_wc(phys_addr_t offset
, size_t size
)
1011 return ioremap_nocache(offset
, size
);
1016 #define ioremap_wt ioremap_wt
1017 static inline void __iomem
*ioremap_wt(phys_addr_t offset
, size_t size
)
1019 return ioremap_nocache(offset
, size
);
1023 #ifdef CONFIG_HAS_IOPORT_MAP
1024 #ifndef CONFIG_GENERIC_IOMAP
1026 #define ioport_map ioport_map
1027 static inline void __iomem
*ioport_map(unsigned long port
, unsigned int nr
)
1029 port
&= IO_SPACE_LIMIT
;
1030 return (port
> MMIO_UPPER_LIMIT
) ? NULL
: PCI_IOBASE
+ port
;
1034 #ifndef ioport_unmap
1035 #define ioport_unmap ioport_unmap
1036 static inline void ioport_unmap(void __iomem
*p
)
1040 #else /* CONFIG_GENERIC_IOMAP */
1041 extern void __iomem
*ioport_map(unsigned long port
, unsigned int nr
);
1042 extern void ioport_unmap(void __iomem
*p
);
1043 #endif /* CONFIG_GENERIC_IOMAP */
1044 #endif /* CONFIG_HAS_IOPORT_MAP */
1047 * Convert a virtual cached pointer to an uncached pointer
1049 #ifndef xlate_dev_kmem_ptr
1050 #define xlate_dev_kmem_ptr xlate_dev_kmem_ptr
1051 static inline void *xlate_dev_kmem_ptr(void *addr
)
1057 #ifndef xlate_dev_mem_ptr
1058 #define xlate_dev_mem_ptr xlate_dev_mem_ptr
1059 static inline void *xlate_dev_mem_ptr(phys_addr_t addr
)
1065 #ifndef unxlate_dev_mem_ptr
1066 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
1067 static inline void unxlate_dev_mem_ptr(phys_addr_t phys
, void *addr
)
1072 #ifdef CONFIG_VIRT_TO_BUS
1074 static inline unsigned long virt_to_bus(void *address
)
1076 return (unsigned long)address
;
1079 static inline void *bus_to_virt(unsigned long address
)
1081 return (void *)address
;
1087 #define memset_io memset_io
1089 * memset_io Set a range of I/O memory to a constant value
1090 * @addr: The beginning of the I/O-memory range to set
1091 * @val: The value to set the memory to
1092 * @count: The number of bytes to set
1094 * Set a range of I/O memory to a given value.
1096 static inline void memset_io(volatile void __iomem
*addr
, int value
,
1099 memset(__io_virt(addr
), value
, size
);
1103 #ifndef memcpy_fromio
1104 #define memcpy_fromio memcpy_fromio
1106 * memcpy_fromio Copy a block of data from I/O memory
1107 * @dst: The (RAM) destination for the copy
1108 * @src: The (I/O memory) source for the data
1109 * @count: The number of bytes to copy
1111 * Copy a block of data from I/O memory.
1113 static inline void memcpy_fromio(void *buffer
,
1114 const volatile void __iomem
*addr
,
1117 memcpy(buffer
, __io_virt(addr
), size
);
1122 #define memcpy_toio memcpy_toio
1124 * memcpy_toio Copy a block of data into I/O memory
1125 * @dst: The (I/O memory) destination for the copy
1126 * @src: The (RAM) source for the data
1127 * @count: The number of bytes to copy
1129 * Copy a block of data to I/O memory.
1131 static inline void memcpy_toio(volatile void __iomem
*addr
, const void *buffer
,
1134 memcpy(__io_virt(addr
), buffer
, size
);
1138 #endif /* __KERNEL__ */
1140 #endif /* __ASM_GENERIC_IO_H */