io.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #include <linux/export.h>
  6. #include <linux/types.h>
  7. #include <linux/io.h>
  8. /*
  9. * Copy data from IO memory space to "real" memory space.
  10. */
  11. void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
  12. {
  13. while (count && !IS_ALIGNED((unsigned long)from, 8)) {
  14. *(u8 *)to = __raw_readb(from);
  15. from++;
  16. to++;
  17. count--;
  18. }
  19. while (count >= 8) {
  20. *(u64 *)to = __raw_readq(from);
  21. from += 8;
  22. to += 8;
  23. count -= 8;
  24. }
  25. while (count) {
  26. *(u8 *)to = __raw_readb(from);
  27. from++;
  28. to++;
  29. count--;
  30. }
  31. }
  32. EXPORT_SYMBOL(__memcpy_fromio);
  33. /*
  34. * Copy data from "real" memory space to IO memory space.
  35. */
  36. void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
  37. {
  38. while (count && !IS_ALIGNED((unsigned long)to, 8)) {
  39. __raw_writeb(*(u8 *)from, to);
  40. from++;
  41. to++;
  42. count--;
  43. }
  44. while (count >= 8) {
  45. __raw_writeq(*(u64 *)from, to);
  46. from += 8;
  47. to += 8;
  48. count -= 8;
  49. }
  50. while (count) {
  51. __raw_writeb(*(u8 *)from, to);
  52. from++;
  53. to++;
  54. count--;
  55. }
  56. }
  57. EXPORT_SYMBOL(__memcpy_toio);
  58. /*
  59. * "memset" on IO memory space.
  60. */
  61. void __memset_io(volatile void __iomem *dst, int c, size_t count)
  62. {
  63. u64 qc = (u8)c;
  64. qc |= qc << 8;
  65. qc |= qc << 16;
  66. qc |= qc << 32;
  67. while (count && !IS_ALIGNED((unsigned long)dst, 8)) {
  68. __raw_writeb(c, dst);
  69. dst++;
  70. count--;
  71. }
  72. while (count >= 8) {
  73. __raw_writeq(qc, dst);
  74. dst += 8;
  75. count -= 8;
  76. }
  77. while (count) {
  78. __raw_writeb(c, dst);
  79. dst++;
  80. count--;
  81. }
  82. }
  83. EXPORT_SYMBOL(__memset_io);