memory.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. * Misc memory accessors
  6. */
  7. #include <linux/export.h>
  8. #include <linux/io.h>
  9. #include <linux/uaccess.h>
  10. #include <sound/core.h>
  11. #include <sound/pcm.h>
  12. /**
  13. * copy_to_user_fromio - copy data from mmio-space to user-space
  14. * @dst: the destination pointer on user-space
  15. * @src: the source pointer on mmio
  16. * @count: the data size to copy in bytes
  17. *
  18. * Copies the data from mmio-space to user-space.
  19. *
  20. * Return: Zero if successful, or non-zero on failure.
  21. */
  22. int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
  23. {
  24. struct iov_iter iter;
  25. if (import_ubuf(ITER_DEST, dst, count, &iter))
  26. return -EFAULT;
  27. return copy_to_iter_fromio(&iter, (const void __iomem *)src, count);
  28. }
  29. EXPORT_SYMBOL(copy_to_user_fromio);
  30. /**
  31. * copy_to_iter_fromio - copy data from mmio-space to iov_iter
  32. * @dst: the destination iov_iter
  33. * @src: the source pointer on mmio
  34. * @count: the data size to copy in bytes
  35. *
  36. * Copies the data from mmio-space to iov_iter.
  37. *
  38. * Return: Zero if successful, or non-zero on failure.
  39. */
  40. int copy_to_iter_fromio(struct iov_iter *dst, const void __iomem *src,
  41. size_t count)
  42. {
  43. #if defined(__i386__) || defined(CONFIG_SPARC32)
  44. return copy_to_iter((const void __force *)src, count, dst) == count ? 0 : -EFAULT;
  45. #else
  46. char buf[256];
  47. while (count) {
  48. size_t c = count;
  49. if (c > sizeof(buf))
  50. c = sizeof(buf);
  51. memcpy_fromio(buf, (void __iomem *)src, c);
  52. if (copy_to_iter(buf, c, dst) != c)
  53. return -EFAULT;
  54. count -= c;
  55. src += c;
  56. }
  57. return 0;
  58. #endif
  59. }
  60. EXPORT_SYMBOL(copy_to_iter_fromio);
  61. /**
  62. * copy_from_user_toio - copy data from user-space to mmio-space
  63. * @dst: the destination pointer on mmio-space
  64. * @src: the source pointer on user-space
  65. * @count: the data size to copy in bytes
  66. *
  67. * Copies the data from user-space to mmio-space.
  68. *
  69. * Return: Zero if successful, or non-zero on failure.
  70. */
  71. int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
  72. {
  73. struct iov_iter iter;
  74. if (import_ubuf(ITER_SOURCE, (void __user *)src, count, &iter))
  75. return -EFAULT;
  76. return copy_from_iter_toio((void __iomem *)dst, &iter, count);
  77. }
  78. EXPORT_SYMBOL(copy_from_user_toio);
  79. /**
  80. * copy_from_iter_toio - copy data from iov_iter to mmio-space
  81. * @dst: the destination pointer on mmio-space
  82. * @src: the source iov_iter
  83. * @count: the data size to copy in bytes
  84. *
  85. * Copies the data from iov_iter to mmio-space.
  86. *
  87. * Return: Zero if successful, or non-zero on failure.
  88. */
  89. int copy_from_iter_toio(void __iomem *dst, struct iov_iter *src, size_t count)
  90. {
  91. #if defined(__i386__) || defined(CONFIG_SPARC32)
  92. return copy_from_iter((void __force *)dst, count, src) == count ? 0 : -EFAULT;
  93. #else
  94. char buf[256];
  95. while (count) {
  96. size_t c = count;
  97. if (c > sizeof(buf))
  98. c = sizeof(buf);
  99. if (copy_from_iter(buf, c, src) != c)
  100. return -EFAULT;
  101. memcpy_toio(dst, buf, c);
  102. count -= c;
  103. dst += c;
  104. }
  105. return 0;
  106. #endif
  107. }
  108. EXPORT_SYMBOL(copy_from_iter_toio);