^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #define movs(type,to,from) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) asm volatile("movs" type:"=&D" (to), "=&S" (from):"0" (to), "1" (from):"memory")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /* Originally from i386/string.h */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) static __always_inline void rep_movs(void *to, const void *from, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) unsigned long d0, d1, d2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) asm volatile("rep ; movsl\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) "testb $2,%b4\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) "je 1f\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) "movsw\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) "1:\ttestb $1,%b4\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) "je 2f\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) "movsb\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) "2:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) : "=&c" (d0), "=&D" (d1), "=&S" (d2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) : "0" (n / 4), "q" (n), "1" ((long)to), "2" ((long)from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) : "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) void memcpy_fromio(void *to, const volatile void __iomem *from, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (unlikely(!n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Align any unaligned source IO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (unlikely(1 & (unsigned long)from)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) movs("b", to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) n--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (n > 1 && unlikely(2 & (unsigned long)from)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) movs("w", to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) n-=2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) rep_movs(to, (const void *)from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) EXPORT_SYMBOL(memcpy_fromio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void memcpy_toio(volatile void __iomem *to, const void *from, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (unlikely(!n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* Align any unaligned destination IO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (unlikely(1 & (unsigned long)to)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) movs("b", to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) n--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (n > 1 && unlikely(2 & (unsigned long)to)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) movs("w", to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) n-=2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) rep_movs((void *)to, (const void *) from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) EXPORT_SYMBOL(memcpy_toio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) void memset_io(volatile void __iomem *a, int b, size_t c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * TODO: memset can mangle the IO patterns quite a bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * perhaps it would be better to use a dumb one:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) memset((void *)a, b, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) EXPORT_SYMBOL(memset_io);