1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Alpha IO and memory functions. 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds #include <linux/kernel.h> 71da177e4SLinus Torvalds #include <linux/types.h> 81da177e4SLinus Torvalds #include <linux/string.h> 91da177e4SLinus Torvalds #include <linux/module.h> 101da177e4SLinus Torvalds #include <asm/io.h> 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* Out-of-line versions of the i/o routines that redirect into the 131da177e4SLinus Torvalds platform-specific version. Note that "platform-specific" may mean 141da177e4SLinus Torvalds "generic", which bumps through the machine vector. */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds unsigned int 178f28ca6bSKrzysztof Kozlowski ioread8(const void __iomem *addr) 181da177e4SLinus Torvalds { 1954505a1eSMikulas Patocka unsigned int ret; 2054505a1eSMikulas Patocka mb(); 2154505a1eSMikulas Patocka ret = IO_CONCAT(__IO_PREFIX,ioread8)(addr); 221da177e4SLinus Torvalds mb(); 231da177e4SLinus Torvalds return ret; 241da177e4SLinus Torvalds } 251da177e4SLinus Torvalds 268f28ca6bSKrzysztof Kozlowski unsigned int ioread16(const void __iomem *addr) 271da177e4SLinus Torvalds { 2854505a1eSMikulas Patocka unsigned int ret; 2954505a1eSMikulas Patocka mb(); 3054505a1eSMikulas Patocka ret = IO_CONCAT(__IO_PREFIX,ioread16)(addr); 311da177e4SLinus Torvalds mb(); 321da177e4SLinus Torvalds return ret; 331da177e4SLinus Torvalds } 341da177e4SLinus Torvalds 358f28ca6bSKrzysztof Kozlowski unsigned int ioread32(const void __iomem *addr) 361da177e4SLinus Torvalds { 3754505a1eSMikulas Patocka unsigned int ret; 3854505a1eSMikulas Patocka mb(); 3954505a1eSMikulas Patocka ret = IO_CONCAT(__IO_PREFIX,ioread32)(addr); 401da177e4SLinus Torvalds mb(); 411da177e4SLinus Torvalds return ret; 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds 44*e19d4ebcSArnd Bergmann u64 ioread64(const void __iomem *addr) 45*e19d4ebcSArnd Bergmann { 46*e19d4ebcSArnd Bergmann unsigned int ret; 47*e19d4ebcSArnd Bergmann mb(); 48*e19d4ebcSArnd Bergmann ret = IO_CONCAT(__IO_PREFIX,ioread64)(addr); 49*e19d4ebcSArnd Bergmann mb(); 50*e19d4ebcSArnd Bergmann return ret; 51*e19d4ebcSArnd Bergmann } 52*e19d4ebcSArnd Bergmann 531da177e4SLinus Torvalds void iowrite8(u8 b, void __iomem *addr) 541da177e4SLinus Torvalds { 551da177e4SLinus Torvalds mb(); 5692d7223aSSinan Kaya IO_CONCAT(__IO_PREFIX,iowrite8)(b, addr); 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds void iowrite16(u16 b, void __iomem *addr) 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds mb(); 6292d7223aSSinan Kaya IO_CONCAT(__IO_PREFIX,iowrite16)(b, addr); 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds void iowrite32(u32 b, void __iomem *addr) 661da177e4SLinus Torvalds { 671da177e4SLinus Torvalds mb(); 6892d7223aSSinan Kaya IO_CONCAT(__IO_PREFIX,iowrite32)(b, addr); 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 71*e19d4ebcSArnd Bergmann void iowrite64(u64 b, void __iomem *addr) 72*e19d4ebcSArnd Bergmann { 73*e19d4ebcSArnd Bergmann mb(); 74*e19d4ebcSArnd Bergmann IO_CONCAT(__IO_PREFIX,iowrite64)(b, addr); 75*e19d4ebcSArnd Bergmann } 76*e19d4ebcSArnd Bergmann 771da177e4SLinus Torvalds EXPORT_SYMBOL(ioread8); 781da177e4SLinus Torvalds EXPORT_SYMBOL(ioread16); 791da177e4SLinus Torvalds EXPORT_SYMBOL(ioread32); 80*e19d4ebcSArnd Bergmann EXPORT_SYMBOL(ioread64); 811da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite8); 821da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite16); 831da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite32); 84*e19d4ebcSArnd Bergmann EXPORT_SYMBOL(iowrite64); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds u8 inb(unsigned long port) 871da177e4SLinus Torvalds { 881da177e4SLinus Torvalds return ioread8(ioport_map(port, 1)); 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds u16 inw(unsigned long port) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds return ioread16(ioport_map(port, 2)); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds u32 inl(unsigned long port) 971da177e4SLinus Torvalds { 981da177e4SLinus Torvalds return ioread32(ioport_map(port, 4)); 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds void outb(u8 b, unsigned long port) 1021da177e4SLinus Torvalds { 1031da177e4SLinus Torvalds iowrite8(b, ioport_map(port, 1)); 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds void outw(u16 b, unsigned long port) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds iowrite16(b, ioport_map(port, 2)); 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds void outl(u32 b, unsigned long port) 1121da177e4SLinus Torvalds { 1131da177e4SLinus Torvalds iowrite32(b, ioport_map(port, 4)); 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds EXPORT_SYMBOL(inb); 1171da177e4SLinus Torvalds EXPORT_SYMBOL(inw); 1181da177e4SLinus Torvalds EXPORT_SYMBOL(inl); 1191da177e4SLinus Torvalds EXPORT_SYMBOL(outb); 1201da177e4SLinus Torvalds EXPORT_SYMBOL(outw); 1211da177e4SLinus Torvalds EXPORT_SYMBOL(outl); 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds u8 __raw_readb(const volatile void __iomem *addr) 1241da177e4SLinus Torvalds { 1251da177e4SLinus Torvalds return IO_CONCAT(__IO_PREFIX,readb)(addr); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds u16 __raw_readw(const volatile void __iomem *addr) 1291da177e4SLinus Torvalds { 1301da177e4SLinus Torvalds return IO_CONCAT(__IO_PREFIX,readw)(addr); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds u32 __raw_readl(const volatile void __iomem *addr) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds return IO_CONCAT(__IO_PREFIX,readl)(addr); 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds u64 __raw_readq(const volatile void __iomem *addr) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds return IO_CONCAT(__IO_PREFIX,readq)(addr); 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds void __raw_writeb(u8 b, volatile void __iomem *addr) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds IO_CONCAT(__IO_PREFIX,writeb)(b, addr); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds void __raw_writew(u16 b, volatile void __iomem *addr) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds IO_CONCAT(__IO_PREFIX,writew)(b, addr); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds void __raw_writel(u32 b, volatile void __iomem *addr) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds IO_CONCAT(__IO_PREFIX,writel)(b, addr); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds void __raw_writeq(u64 b, volatile void __iomem *addr) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds IO_CONCAT(__IO_PREFIX,writeq)(b, addr); 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_readb); 1641da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_readw); 1651da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_readl); 1661da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_readq); 1671da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_writeb); 1681da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_writew); 1691da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_writel); 1701da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_writeq); 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds u8 readb(const volatile void __iomem *addr) 1731da177e4SLinus Torvalds { 17454505a1eSMikulas Patocka u8 ret; 17554505a1eSMikulas Patocka mb(); 17654505a1eSMikulas Patocka ret = __raw_readb(addr); 1771da177e4SLinus Torvalds mb(); 1781da177e4SLinus Torvalds return ret; 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds u16 readw(const volatile void __iomem *addr) 1821da177e4SLinus Torvalds { 18354505a1eSMikulas Patocka u16 ret; 18454505a1eSMikulas Patocka mb(); 18554505a1eSMikulas Patocka ret = __raw_readw(addr); 1861da177e4SLinus Torvalds mb(); 1871da177e4SLinus Torvalds return ret; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds u32 readl(const volatile void __iomem *addr) 1911da177e4SLinus Torvalds { 19254505a1eSMikulas Patocka u32 ret; 19354505a1eSMikulas Patocka mb(); 19454505a1eSMikulas Patocka ret = __raw_readl(addr); 1951da177e4SLinus Torvalds mb(); 1961da177e4SLinus Torvalds return ret; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds u64 readq(const volatile void __iomem *addr) 2001da177e4SLinus Torvalds { 20154505a1eSMikulas Patocka u64 ret; 20254505a1eSMikulas Patocka mb(); 20354505a1eSMikulas Patocka ret = __raw_readq(addr); 2041da177e4SLinus Torvalds mb(); 2051da177e4SLinus Torvalds return ret; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds void writeb(u8 b, volatile void __iomem *addr) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds mb(); 21192d7223aSSinan Kaya __raw_writeb(b, addr); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds void writew(u16 b, volatile void __iomem *addr) 2151da177e4SLinus Torvalds { 2161da177e4SLinus Torvalds mb(); 21792d7223aSSinan Kaya __raw_writew(b, addr); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds void writel(u32 b, volatile void __iomem *addr) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds mb(); 22392d7223aSSinan Kaya __raw_writel(b, addr); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds void writeq(u64 b, volatile void __iomem *addr) 2271da177e4SLinus Torvalds { 2281da177e4SLinus Torvalds mb(); 22992d7223aSSinan Kaya __raw_writeq(b, addr); 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds EXPORT_SYMBOL(readb); 2331da177e4SLinus Torvalds EXPORT_SYMBOL(readw); 2341da177e4SLinus Torvalds EXPORT_SYMBOL(readl); 2351da177e4SLinus Torvalds EXPORT_SYMBOL(readq); 2361da177e4SLinus Torvalds EXPORT_SYMBOL(writeb); 2371da177e4SLinus Torvalds EXPORT_SYMBOL(writew); 2381da177e4SLinus Torvalds EXPORT_SYMBOL(writel); 2391da177e4SLinus Torvalds EXPORT_SYMBOL(writeq); 2401da177e4SLinus Torvalds 24154505a1eSMikulas Patocka /* 24254505a1eSMikulas Patocka * The _relaxed functions must be ordered w.r.t. each other, but they don't 24354505a1eSMikulas Patocka * have to be ordered w.r.t. other memory accesses. 24454505a1eSMikulas Patocka */ 24554505a1eSMikulas Patocka u8 readb_relaxed(const volatile void __iomem *addr) 24654505a1eSMikulas Patocka { 24754505a1eSMikulas Patocka mb(); 24854505a1eSMikulas Patocka return __raw_readb(addr); 24954505a1eSMikulas Patocka } 25054505a1eSMikulas Patocka 25154505a1eSMikulas Patocka u16 readw_relaxed(const volatile void __iomem *addr) 25254505a1eSMikulas Patocka { 25354505a1eSMikulas Patocka mb(); 25454505a1eSMikulas Patocka return __raw_readw(addr); 25554505a1eSMikulas Patocka } 25654505a1eSMikulas Patocka 25754505a1eSMikulas Patocka u32 readl_relaxed(const volatile void __iomem *addr) 25854505a1eSMikulas Patocka { 25954505a1eSMikulas Patocka mb(); 26054505a1eSMikulas Patocka return __raw_readl(addr); 26154505a1eSMikulas Patocka } 26254505a1eSMikulas Patocka 26354505a1eSMikulas Patocka u64 readq_relaxed(const volatile void __iomem *addr) 26454505a1eSMikulas Patocka { 26554505a1eSMikulas Patocka mb(); 26654505a1eSMikulas Patocka return __raw_readq(addr); 26754505a1eSMikulas Patocka } 26854505a1eSMikulas Patocka 26954505a1eSMikulas Patocka EXPORT_SYMBOL(readb_relaxed); 27054505a1eSMikulas Patocka EXPORT_SYMBOL(readw_relaxed); 27154505a1eSMikulas Patocka EXPORT_SYMBOL(readl_relaxed); 27254505a1eSMikulas Patocka EXPORT_SYMBOL(readq_relaxed); 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* 2751da177e4SLinus Torvalds * Read COUNT 8-bit bytes from port PORT into memory starting at SRC. 2761da177e4SLinus Torvalds */ 2778f28ca6bSKrzysztof Kozlowski void ioread8_rep(const void __iomem *port, void *dst, unsigned long count) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds while ((unsigned long)dst & 0x3) { 2801da177e4SLinus Torvalds if (!count) 2811da177e4SLinus Torvalds return; 2821da177e4SLinus Torvalds count--; 2831da177e4SLinus Torvalds *(unsigned char *)dst = ioread8(port); 2841da177e4SLinus Torvalds dst += 1; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds while (count >= 4) { 2881da177e4SLinus Torvalds unsigned int w; 2891da177e4SLinus Torvalds count -= 4; 2901da177e4SLinus Torvalds w = ioread8(port); 2911da177e4SLinus Torvalds w |= ioread8(port) << 8; 2921da177e4SLinus Torvalds w |= ioread8(port) << 16; 2931da177e4SLinus Torvalds w |= ioread8(port) << 24; 2941da177e4SLinus Torvalds *(unsigned int *)dst = w; 2951da177e4SLinus Torvalds dst += 4; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds while (count) { 2991da177e4SLinus Torvalds --count; 3001da177e4SLinus Torvalds *(unsigned char *)dst = ioread8(port); 3011da177e4SLinus Torvalds dst += 1; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds void insb(unsigned long port, void *dst, unsigned long count) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds ioread8_rep(ioport_map(port, 1), dst, count); 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds EXPORT_SYMBOL(ioread8_rep); 3111da177e4SLinus Torvalds EXPORT_SYMBOL(insb); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds /* 3141da177e4SLinus Torvalds * Read COUNT 16-bit words from port PORT into memory starting at 3151da177e4SLinus Torvalds * SRC. SRC must be at least short aligned. This is used by the 3161da177e4SLinus Torvalds * IDE driver to read disk sectors. Performance is important, but 3171da177e4SLinus Torvalds * the interfaces seems to be slow: just using the inlined version 3181da177e4SLinus Torvalds * of the inw() breaks things. 3191da177e4SLinus Torvalds */ 3208f28ca6bSKrzysztof Kozlowski void ioread16_rep(const void __iomem *port, void *dst, unsigned long count) 3211da177e4SLinus Torvalds { 3221da177e4SLinus Torvalds if (unlikely((unsigned long)dst & 0x3)) { 3231da177e4SLinus Torvalds if (!count) 3241da177e4SLinus Torvalds return; 3251da177e4SLinus Torvalds BUG_ON((unsigned long)dst & 0x1); 3261da177e4SLinus Torvalds count--; 3271da177e4SLinus Torvalds *(unsigned short *)dst = ioread16(port); 3281da177e4SLinus Torvalds dst += 2; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds while (count >= 2) { 3321da177e4SLinus Torvalds unsigned int w; 3331da177e4SLinus Torvalds count -= 2; 3341da177e4SLinus Torvalds w = ioread16(port); 3351da177e4SLinus Torvalds w |= ioread16(port) << 16; 3361da177e4SLinus Torvalds *(unsigned int *)dst = w; 3371da177e4SLinus Torvalds dst += 4; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds if (count) { 3411da177e4SLinus Torvalds *(unsigned short*)dst = ioread16(port); 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds void insw(unsigned long port, void *dst, unsigned long count) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds ioread16_rep(ioport_map(port, 2), dst, count); 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds EXPORT_SYMBOL(ioread16_rep); 3511da177e4SLinus Torvalds EXPORT_SYMBOL(insw); 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* 3551da177e4SLinus Torvalds * Read COUNT 32-bit words from port PORT into memory starting at 3561da177e4SLinus Torvalds * SRC. Now works with any alignment in SRC. Performance is important, 3571da177e4SLinus Torvalds * but the interfaces seems to be slow: just using the inlined version 3581da177e4SLinus Torvalds * of the inl() breaks things. 3591da177e4SLinus Torvalds */ 3608f28ca6bSKrzysztof Kozlowski void ioread32_rep(const void __iomem *port, void *dst, unsigned long count) 3611da177e4SLinus Torvalds { 3621da177e4SLinus Torvalds if (unlikely((unsigned long)dst & 0x3)) { 3631da177e4SLinus Torvalds while (count--) { 3641da177e4SLinus Torvalds struct S { int x __attribute__((packed)); }; 3651da177e4SLinus Torvalds ((struct S *)dst)->x = ioread32(port); 3661da177e4SLinus Torvalds dst += 4; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds } else { 3691da177e4SLinus Torvalds /* Buffer 32-bit aligned. */ 3701da177e4SLinus Torvalds while (count--) { 3711da177e4SLinus Torvalds *(unsigned int *)dst = ioread32(port); 3721da177e4SLinus Torvalds dst += 4; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds void insl(unsigned long port, void *dst, unsigned long count) 3781da177e4SLinus Torvalds { 3791da177e4SLinus Torvalds ioread32_rep(ioport_map(port, 4), dst, count); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds EXPORT_SYMBOL(ioread32_rep); 3831da177e4SLinus Torvalds EXPORT_SYMBOL(insl); 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds /* 3871da177e4SLinus Torvalds * Like insb but in the opposite direction. 3881da177e4SLinus Torvalds * Don't worry as much about doing aligned memory transfers: 3891da177e4SLinus Torvalds * doing byte reads the "slow" way isn't nearly as slow as 3901da177e4SLinus Torvalds * doing byte writes the slow way (no r-m-w cycle). 3911da177e4SLinus Torvalds */ 3921da177e4SLinus Torvalds void iowrite8_rep(void __iomem *port, const void *xsrc, unsigned long count) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds const unsigned char *src = xsrc; 3951da177e4SLinus Torvalds while (count--) 3961da177e4SLinus Torvalds iowrite8(*src++, port); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds void outsb(unsigned long port, const void *src, unsigned long count) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds iowrite8_rep(ioport_map(port, 1), src, count); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite8_rep); 4051da177e4SLinus Torvalds EXPORT_SYMBOL(outsb); 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* 4091da177e4SLinus Torvalds * Like insw but in the opposite direction. This is used by the IDE 4101da177e4SLinus Torvalds * driver to write disk sectors. Performance is important, but the 4111da177e4SLinus Torvalds * interfaces seems to be slow: just using the inlined version of the 4121da177e4SLinus Torvalds * outw() breaks things. 4131da177e4SLinus Torvalds */ 4141da177e4SLinus Torvalds void iowrite16_rep(void __iomem *port, const void *src, unsigned long count) 4151da177e4SLinus Torvalds { 4161da177e4SLinus Torvalds if (unlikely((unsigned long)src & 0x3)) { 4171da177e4SLinus Torvalds if (!count) 4181da177e4SLinus Torvalds return; 4191da177e4SLinus Torvalds BUG_ON((unsigned long)src & 0x1); 4201da177e4SLinus Torvalds iowrite16(*(unsigned short *)src, port); 4211da177e4SLinus Torvalds src += 2; 4221da177e4SLinus Torvalds --count; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds while (count >= 2) { 4261da177e4SLinus Torvalds unsigned int w; 4271da177e4SLinus Torvalds count -= 2; 4281da177e4SLinus Torvalds w = *(unsigned int *)src; 4291da177e4SLinus Torvalds src += 4; 4301da177e4SLinus Torvalds iowrite16(w >> 0, port); 4311da177e4SLinus Torvalds iowrite16(w >> 16, port); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds if (count) { 4351da177e4SLinus Torvalds iowrite16(*(unsigned short *)src, port); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds void outsw(unsigned long port, const void *src, unsigned long count) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds iowrite16_rep(ioport_map(port, 2), src, count); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite16_rep); 4451da177e4SLinus Torvalds EXPORT_SYMBOL(outsw); 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* 4491da177e4SLinus Torvalds * Like insl but in the opposite direction. This is used by the IDE 4501da177e4SLinus Torvalds * driver to write disk sectors. Works with any alignment in SRC. 4511da177e4SLinus Torvalds * Performance is important, but the interfaces seems to be slow: 4521da177e4SLinus Torvalds * just using the inlined version of the outl() breaks things. 4531da177e4SLinus Torvalds */ 4541da177e4SLinus Torvalds void iowrite32_rep(void __iomem *port, const void *src, unsigned long count) 4551da177e4SLinus Torvalds { 4561da177e4SLinus Torvalds if (unlikely((unsigned long)src & 0x3)) { 4571da177e4SLinus Torvalds while (count--) { 4581da177e4SLinus Torvalds struct S { int x __attribute__((packed)); }; 4591da177e4SLinus Torvalds iowrite32(((struct S *)src)->x, port); 4601da177e4SLinus Torvalds src += 4; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds } else { 4631da177e4SLinus Torvalds /* Buffer 32-bit aligned. */ 4641da177e4SLinus Torvalds while (count--) { 4651da177e4SLinus Torvalds iowrite32(*(unsigned int *)src, port); 4661da177e4SLinus Torvalds src += 4; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds void outsl(unsigned long port, const void *src, unsigned long count) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds iowrite32_rep(ioport_map(port, 4), src, count); 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite32_rep); 4771da177e4SLinus Torvalds EXPORT_SYMBOL(outsl); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds /* 4811da177e4SLinus Torvalds * Copy data from IO memory space to "real" memory space. 4821da177e4SLinus Torvalds * This needs to be optimized. 4831da177e4SLinus Torvalds */ 4841da177e4SLinus Torvalds void memcpy_fromio(void *to, const volatile void __iomem *from, long count) 4851da177e4SLinus Torvalds { 4861da177e4SLinus Torvalds /* Optimize co-aligned transfers. Everything else gets handled 4871da177e4SLinus Torvalds a byte at a time. */ 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) { 4901da177e4SLinus Torvalds count -= 8; 4911da177e4SLinus Torvalds do { 4921da177e4SLinus Torvalds *(u64 *)to = __raw_readq(from); 4931da177e4SLinus Torvalds count -= 8; 4941da177e4SLinus Torvalds to += 8; 4951da177e4SLinus Torvalds from += 8; 4961da177e4SLinus Torvalds } while (count >= 0); 4971da177e4SLinus Torvalds count += 8; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) { 5011da177e4SLinus Torvalds count -= 4; 5021da177e4SLinus Torvalds do { 5031da177e4SLinus Torvalds *(u32 *)to = __raw_readl(from); 5041da177e4SLinus Torvalds count -= 4; 5051da177e4SLinus Torvalds to += 4; 5061da177e4SLinus Torvalds from += 4; 5071da177e4SLinus Torvalds } while (count >= 0); 5081da177e4SLinus Torvalds count += 4; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) { 5121da177e4SLinus Torvalds count -= 2; 5131da177e4SLinus Torvalds do { 5141da177e4SLinus Torvalds *(u16 *)to = __raw_readw(from); 5151da177e4SLinus Torvalds count -= 2; 5161da177e4SLinus Torvalds to += 2; 5171da177e4SLinus Torvalds from += 2; 5181da177e4SLinus Torvalds } while (count >= 0); 5191da177e4SLinus Torvalds count += 2; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds while (count > 0) { 5231da177e4SLinus Torvalds *(u8 *) to = __raw_readb(from); 5241da177e4SLinus Torvalds count--; 5251da177e4SLinus Torvalds to++; 5261da177e4SLinus Torvalds from++; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds mb(); 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds EXPORT_SYMBOL(memcpy_fromio); 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds /* 5351da177e4SLinus Torvalds * Copy data from "real" memory space to IO memory space. 5361da177e4SLinus Torvalds * This needs to be optimized. 5371da177e4SLinus Torvalds */ 5381da177e4SLinus Torvalds void memcpy_toio(volatile void __iomem *to, const void *from, long count) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds /* Optimize co-aligned transfers. Everything else gets handled 5411da177e4SLinus Torvalds a byte at a time. */ 5421da177e4SLinus Torvalds /* FIXME -- align FROM. */ 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) { 5451da177e4SLinus Torvalds count -= 8; 5461da177e4SLinus Torvalds do { 5471da177e4SLinus Torvalds __raw_writeq(*(const u64 *)from, to); 5481da177e4SLinus Torvalds count -= 8; 5491da177e4SLinus Torvalds to += 8; 5501da177e4SLinus Torvalds from += 8; 5511da177e4SLinus Torvalds } while (count >= 0); 5521da177e4SLinus Torvalds count += 8; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) { 5561da177e4SLinus Torvalds count -= 4; 5571da177e4SLinus Torvalds do { 5581da177e4SLinus Torvalds __raw_writel(*(const u32 *)from, to); 5591da177e4SLinus Torvalds count -= 4; 5601da177e4SLinus Torvalds to += 4; 5611da177e4SLinus Torvalds from += 4; 5621da177e4SLinus Torvalds } while (count >= 0); 5631da177e4SLinus Torvalds count += 4; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) { 5671da177e4SLinus Torvalds count -= 2; 5681da177e4SLinus Torvalds do { 5691da177e4SLinus Torvalds __raw_writew(*(const u16 *)from, to); 5701da177e4SLinus Torvalds count -= 2; 5711da177e4SLinus Torvalds to += 2; 5721da177e4SLinus Torvalds from += 2; 5731da177e4SLinus Torvalds } while (count >= 0); 5741da177e4SLinus Torvalds count += 2; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds while (count > 0) { 5781da177e4SLinus Torvalds __raw_writeb(*(const u8 *) from, to); 5791da177e4SLinus Torvalds count--; 5801da177e4SLinus Torvalds to++; 5811da177e4SLinus Torvalds from++; 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds mb(); 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds EXPORT_SYMBOL(memcpy_toio); 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* 5901da177e4SLinus Torvalds * "memset" on IO memory space. 5911da177e4SLinus Torvalds */ 5921da177e4SLinus Torvalds void _memset_c_io(volatile void __iomem *to, unsigned long c, long count) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds /* Handle any initial odd byte */ 5951da177e4SLinus Torvalds if (count > 0 && ((u64)to & 1)) { 5961da177e4SLinus Torvalds __raw_writeb(c, to); 5971da177e4SLinus Torvalds to++; 5981da177e4SLinus Torvalds count--; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds /* Handle any initial odd halfword */ 6021da177e4SLinus Torvalds if (count >= 2 && ((u64)to & 2)) { 6031da177e4SLinus Torvalds __raw_writew(c, to); 6041da177e4SLinus Torvalds to += 2; 6051da177e4SLinus Torvalds count -= 2; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /* Handle any initial odd word */ 6091da177e4SLinus Torvalds if (count >= 4 && ((u64)to & 4)) { 6101da177e4SLinus Torvalds __raw_writel(c, to); 6111da177e4SLinus Torvalds to += 4; 6121da177e4SLinus Torvalds count -= 4; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds /* Handle all full-sized quadwords: we're aligned 6161da177e4SLinus Torvalds (or have a small count) */ 6171da177e4SLinus Torvalds count -= 8; 6181da177e4SLinus Torvalds if (count >= 0) { 6191da177e4SLinus Torvalds do { 6201da177e4SLinus Torvalds __raw_writeq(c, to); 6211da177e4SLinus Torvalds to += 8; 6221da177e4SLinus Torvalds count -= 8; 6231da177e4SLinus Torvalds } while (count >= 0); 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds count += 8; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds /* The tail is word-aligned if we still have count >= 4 */ 6281da177e4SLinus Torvalds if (count >= 4) { 6291da177e4SLinus Torvalds __raw_writel(c, to); 6301da177e4SLinus Torvalds to += 4; 6311da177e4SLinus Torvalds count -= 4; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /* The tail is half-word aligned if we have count >= 2 */ 6351da177e4SLinus Torvalds if (count >= 2) { 6361da177e4SLinus Torvalds __raw_writew(c, to); 6371da177e4SLinus Torvalds to += 2; 6381da177e4SLinus Torvalds count -= 2; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds /* And finally, one last byte.. */ 6421da177e4SLinus Torvalds if (count) { 6431da177e4SLinus Torvalds __raw_writeb(c, to); 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds mb(); 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds EXPORT_SYMBOL(_memset_c_io); 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /* A version of memcpy used by the vga console routines to move data around 6511da177e4SLinus Torvalds arbitrarily between screen and main memory. */ 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds void 6541da177e4SLinus Torvalds scr_memcpyw(u16 *d, const u16 *s, unsigned int count) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds const u16 __iomem *ios = (const u16 __iomem *) s; 6571da177e4SLinus Torvalds u16 __iomem *iod = (u16 __iomem *) d; 6581da177e4SLinus Torvalds int s_isio = __is_ioaddr(s); 6591da177e4SLinus Torvalds int d_isio = __is_ioaddr(d); 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds if (s_isio) { 6621da177e4SLinus Torvalds if (d_isio) { 6631da177e4SLinus Torvalds /* FIXME: Should handle unaligned ops and 6641da177e4SLinus Torvalds operation widening. */ 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds count /= 2; 6671da177e4SLinus Torvalds while (count--) { 6681da177e4SLinus Torvalds u16 tmp = __raw_readw(ios++); 6691da177e4SLinus Torvalds __raw_writew(tmp, iod++); 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds else 6731da177e4SLinus Torvalds memcpy_fromio(d, ios, count); 6741da177e4SLinus Torvalds } else { 6751da177e4SLinus Torvalds if (d_isio) 6761da177e4SLinus Torvalds memcpy_toio(iod, s, count); 6771da177e4SLinus Torvalds else 6781da177e4SLinus Torvalds memcpy(d, s, count); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds EXPORT_SYMBOL(scr_memcpyw); 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds void __iomem *ioport_map(unsigned long port, unsigned int size) 6851da177e4SLinus Torvalds { 6861da177e4SLinus Torvalds return IO_CONCAT(__IO_PREFIX,ioportmap) (port); 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds void ioport_unmap(void __iomem *addr) 6901da177e4SLinus Torvalds { 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds EXPORT_SYMBOL(ioport_map); 6941da177e4SLinus Torvalds EXPORT_SYMBOL(ioport_unmap); 695