io.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/kernel/io.c
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. */
  7. #include <linux/export.h>
  8. #include <linux/types.h>
  9. #include <linux/io.h>
  10. /*
  11. * Copy data from IO memory space to "real" memory space.
  12. */
  13. void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
  14. {
  15. while (count && !IS_ALIGNED((unsigned long)from, 8)) {
  16. *(u8 *)to = __raw_readb(from);
  17. from++;
  18. to++;
  19. count--;
  20. }
  21. while (count >= 8) {
  22. *(u64 *)to = __raw_readq(from);
  23. from += 8;
  24. to += 8;
  25. count -= 8;
  26. }
  27. while (count) {
  28. *(u8 *)to = __raw_readb(from);
  29. from++;
  30. to++;
  31. count--;
  32. }
  33. }
  34. EXPORT_SYMBOL(__memcpy_fromio);
  35. /*
  36. * This generates a memcpy that works on a from/to address which is aligned to
  37. * bits. Count is in terms of the number of bits sized quantities to copy. It
  38. * optimizes to use the STR groupings when possible so that it is WC friendly.
  39. */
  40. #define memcpy_toio_aligned(to, from, count, bits) \
  41. ({ \
  42. volatile u##bits __iomem *_to = to; \
  43. const u##bits *_from = from; \
  44. size_t _count = count; \
  45. const u##bits *_end_from = _from + ALIGN_DOWN(_count, 8); \
  46. \
  47. for (; _from < _end_from; _from += 8, _to += 8) \
  48. __const_memcpy_toio_aligned##bits(_to, _from, 8); \
  49. if ((_count % 8) >= 4) { \
  50. __const_memcpy_toio_aligned##bits(_to, _from, 4); \
  51. _from += 4; \
  52. _to += 4; \
  53. } \
  54. if ((_count % 4) >= 2) { \
  55. __const_memcpy_toio_aligned##bits(_to, _from, 2); \
  56. _from += 2; \
  57. _to += 2; \
  58. } \
  59. if (_count % 2) \
  60. __const_memcpy_toio_aligned##bits(_to, _from, 1); \
  61. })
  62. void __iowrite64_copy_full(void __iomem *to, const void *from, size_t count)
  63. {
  64. memcpy_toio_aligned(to, from, count, 64);
  65. dgh();
  66. }
  67. EXPORT_SYMBOL(__iowrite64_copy_full);
  68. void __iowrite32_copy_full(void __iomem *to, const void *from, size_t count)
  69. {
  70. memcpy_toio_aligned(to, from, count, 32);
  71. dgh();
  72. }
  73. EXPORT_SYMBOL(__iowrite32_copy_full);
  74. /*
  75. * Copy data from "real" memory space to IO memory space.
  76. */
  77. void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
  78. {
  79. while (count && !IS_ALIGNED((unsigned long)to, 8)) {
  80. __raw_writeb(*(u8 *)from, to);
  81. from++;
  82. to++;
  83. count--;
  84. }
  85. while (count >= 8) {
  86. __raw_writeq(*(u64 *)from, to);
  87. from += 8;
  88. to += 8;
  89. count -= 8;
  90. }
  91. while (count) {
  92. __raw_writeb(*(u8 *)from, to);
  93. from++;
  94. to++;
  95. count--;
  96. }
  97. }
  98. EXPORT_SYMBOL(__memcpy_toio);
  99. /*
  100. * "memset" on IO memory space.
  101. */
  102. void __memset_io(volatile void __iomem *dst, int c, size_t count)
  103. {
  104. u64 qc = (u8)c;
  105. qc |= qc << 8;
  106. qc |= qc << 16;
  107. qc |= qc << 32;
  108. while (count && !IS_ALIGNED((unsigned long)dst, 8)) {
  109. __raw_writeb(c, dst);
  110. dst++;
  111. count--;
  112. }
  113. while (count >= 8) {
  114. __raw_writeq(qc, dst);
  115. dst += 8;
  116. count -= 8;
  117. }
  118. while (count) {
  119. __raw_writeb(c, dst);
  120. dst++;
  121. count--;
  122. }
  123. }
  124. EXPORT_SYMBOL(__memset_io);