pmem.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright(c) 2015 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #ifndef __PMEM_H__
  14. #define __PMEM_H__
  15. #include <linux/io.h>
  16. #include <linux/uio.h>
  17. #ifdef CONFIG_ARCH_HAS_PMEM_API
  18. #define ARCH_MEMREMAP_PMEM MEMREMAP_WB
  19. #include <asm/pmem.h>
  20. #else
  21. #define ARCH_MEMREMAP_PMEM MEMREMAP_WT
  22. /*
  23. * These are simply here to enable compilation, all call sites gate
  24. * calling these symbols with arch_has_pmem_api() and redirect to the
  25. * implementation in asm/pmem.h.
  26. */
  27. static inline void arch_memcpy_to_pmem(void *dst, const void *src, size_t n)
  28. {
  29. BUG();
  30. }
  31. static inline int arch_memcpy_from_pmem(void *dst, const void *src, size_t n)
  32. {
  33. BUG();
  34. return -EFAULT;
  35. }
  36. static inline size_t arch_copy_from_iter_pmem(void *addr, size_t bytes,
  37. struct iov_iter *i)
  38. {
  39. BUG();
  40. return 0;
  41. }
  42. static inline void arch_clear_pmem(void *addr, size_t size)
  43. {
  44. BUG();
  45. }
  46. static inline void arch_wb_cache_pmem(void *addr, size_t size)
  47. {
  48. BUG();
  49. }
  50. static inline void arch_invalidate_pmem(void *addr, size_t size)
  51. {
  52. BUG();
  53. }
  54. #endif
  55. static inline bool arch_has_pmem_api(void)
  56. {
  57. return IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API);
  58. }
  59. /*
  60. * memcpy_from_pmem - read from persistent memory with error handling
  61. * @dst: destination buffer
  62. * @src: source buffer
  63. * @size: transfer length
  64. *
  65. * Returns 0 on success negative error code on failure.
  66. */
  67. static inline int memcpy_from_pmem(void *dst, void const *src, size_t size)
  68. {
  69. if (arch_has_pmem_api())
  70. return arch_memcpy_from_pmem(dst, src, size);
  71. else
  72. memcpy(dst, src, size);
  73. return 0;
  74. }
  75. /**
  76. * memcpy_to_pmem - copy data to persistent memory
  77. * @dst: destination buffer for the copy
  78. * @src: source buffer for the copy
  79. * @n: length of the copy in bytes
  80. *
  81. * Perform a memory copy that results in the destination of the copy
  82. * being effectively evicted from, or never written to, the processor
  83. * cache hierarchy after the copy completes. After memcpy_to_pmem()
  84. * data may still reside in cpu or platform buffers, so this operation
  85. * must be followed by a blkdev_issue_flush() on the pmem block device.
  86. */
  87. static inline void memcpy_to_pmem(void *dst, const void *src, size_t n)
  88. {
  89. if (arch_has_pmem_api())
  90. arch_memcpy_to_pmem(dst, src, n);
  91. else
  92. memcpy(dst, src, n);
  93. }
  94. /**
  95. * copy_from_iter_pmem - copy data from an iterator to PMEM
  96. * @addr: PMEM destination address
  97. * @bytes: number of bytes to copy
  98. * @i: iterator with source data
  99. *
  100. * Copy data from the iterator 'i' to the PMEM buffer starting at 'addr'.
  101. * See blkdev_issue_flush() note for memcpy_to_pmem().
  102. */
  103. static inline size_t copy_from_iter_pmem(void *addr, size_t bytes,
  104. struct iov_iter *i)
  105. {
  106. if (arch_has_pmem_api())
  107. return arch_copy_from_iter_pmem(addr, bytes, i);
  108. return copy_from_iter_nocache(addr, bytes, i);
  109. }
  110. /**
  111. * clear_pmem - zero a PMEM memory range
  112. * @addr: virtual start address
  113. * @size: number of bytes to zero
  114. *
  115. * Write zeros into the memory range starting at 'addr' for 'size' bytes.
  116. * See blkdev_issue_flush() note for memcpy_to_pmem().
  117. */
  118. static inline void clear_pmem(void *addr, size_t size)
  119. {
  120. if (arch_has_pmem_api())
  121. arch_clear_pmem(addr, size);
  122. else
  123. memset(addr, 0, size);
  124. }
  125. /**
  126. * invalidate_pmem - flush a pmem range from the cache hierarchy
  127. * @addr: virtual start address
  128. * @size: bytes to invalidate (internally aligned to cache line size)
  129. *
  130. * For platforms that support clearing poison this flushes any poisoned
  131. * ranges out of the cache
  132. */
  133. static inline void invalidate_pmem(void *addr, size_t size)
  134. {
  135. if (arch_has_pmem_api())
  136. arch_invalidate_pmem(addr, size);
  137. }
  138. /**
  139. * wb_cache_pmem - write back processor cache for PMEM memory range
  140. * @addr: virtual start address
  141. * @size: number of bytes to write back
  142. *
  143. * Write back the processor cache range starting at 'addr' for 'size' bytes.
  144. * See blkdev_issue_flush() note for memcpy_to_pmem().
  145. */
  146. static inline void wb_cache_pmem(void *addr, size_t size)
  147. {
  148. if (arch_has_pmem_api())
  149. arch_wb_cache_pmem(addr, size);
  150. }
  151. #endif /* __PMEM_H__ */