pmem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 bool __arch_has_wmb_pmem(void)
  28. {
  29. return false;
  30. }
  31. static inline void arch_wmb_pmem(void)
  32. {
  33. BUG();
  34. }
  35. static inline void arch_memcpy_to_pmem(void __pmem *dst, const void *src,
  36. size_t n)
  37. {
  38. BUG();
  39. }
  40. static inline size_t arch_copy_from_iter_pmem(void __pmem *addr, size_t bytes,
  41. struct iov_iter *i)
  42. {
  43. BUG();
  44. return 0;
  45. }
  46. static inline void arch_clear_pmem(void __pmem *addr, size_t size)
  47. {
  48. BUG();
  49. }
  50. #endif
  51. /*
  52. * Architectures that define ARCH_HAS_PMEM_API must provide
  53. * implementations for arch_memcpy_to_pmem(), arch_wmb_pmem(),
  54. * arch_copy_from_iter_pmem(), arch_clear_pmem() and arch_has_wmb_pmem().
  55. */
  56. static inline void memcpy_from_pmem(void *dst, void __pmem const *src, size_t size)
  57. {
  58. memcpy(dst, (void __force const *) src, size);
  59. }
  60. static inline bool arch_has_pmem_api(void)
  61. {
  62. return IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API);
  63. }
  64. /**
  65. * arch_has_wmb_pmem - true if wmb_pmem() ensures durability
  66. *
  67. * For a given cpu implementation within an architecture it is possible
  68. * that wmb_pmem() resolves to a nop. In the case this returns
  69. * false, pmem api users are unable to ensure durability and may want to
  70. * fall back to a different data consistency model, or otherwise notify
  71. * the user.
  72. */
  73. static inline bool arch_has_wmb_pmem(void)
  74. {
  75. return arch_has_pmem_api() && __arch_has_wmb_pmem();
  76. }
  77. /*
  78. * These defaults seek to offer decent performance and minimize the
  79. * window between i/o completion and writes being durable on media.
  80. * However, it is undefined / architecture specific whether
  81. * ARCH_MEMREMAP_PMEM + default_memcpy_to_pmem is sufficient for
  82. * making data durable relative to i/o completion.
  83. */
  84. static inline void default_memcpy_to_pmem(void __pmem *dst, const void *src,
  85. size_t size)
  86. {
  87. memcpy((void __force *) dst, src, size);
  88. }
  89. static inline size_t default_copy_from_iter_pmem(void __pmem *addr,
  90. size_t bytes, struct iov_iter *i)
  91. {
  92. return copy_from_iter_nocache((void __force *)addr, bytes, i);
  93. }
  94. static inline void default_clear_pmem(void __pmem *addr, size_t size)
  95. {
  96. if (size == PAGE_SIZE && ((unsigned long)addr & ~PAGE_MASK) == 0)
  97. clear_page((void __force *)addr);
  98. else
  99. memset((void __force *)addr, 0, size);
  100. }
  101. /**
  102. * memcpy_to_pmem - copy data to persistent memory
  103. * @dst: destination buffer for the copy
  104. * @src: source buffer for the copy
  105. * @n: length of the copy in bytes
  106. *
  107. * Perform a memory copy that results in the destination of the copy
  108. * being effectively evicted from, or never written to, the processor
  109. * cache hierarchy after the copy completes. After memcpy_to_pmem()
  110. * data may still reside in cpu or platform buffers, so this operation
  111. * must be followed by a wmb_pmem().
  112. */
  113. static inline void memcpy_to_pmem(void __pmem *dst, const void *src, size_t n)
  114. {
  115. if (arch_has_pmem_api())
  116. arch_memcpy_to_pmem(dst, src, n);
  117. else
  118. default_memcpy_to_pmem(dst, src, n);
  119. }
  120. /**
  121. * wmb_pmem - synchronize writes to persistent memory
  122. *
  123. * After a series of memcpy_to_pmem() operations this drains data from
  124. * cpu write buffers and any platform (memory controller) buffers to
  125. * ensure that written data is durable on persistent memory media.
  126. */
  127. static inline void wmb_pmem(void)
  128. {
  129. if (arch_has_wmb_pmem())
  130. arch_wmb_pmem();
  131. else
  132. wmb();
  133. }
  134. /**
  135. * copy_from_iter_pmem - copy data from an iterator to PMEM
  136. * @addr: PMEM destination address
  137. * @bytes: number of bytes to copy
  138. * @i: iterator with source data
  139. *
  140. * Copy data from the iterator 'i' to the PMEM buffer starting at 'addr'.
  141. * This function requires explicit ordering with a wmb_pmem() call.
  142. */
  143. static inline size_t copy_from_iter_pmem(void __pmem *addr, size_t bytes,
  144. struct iov_iter *i)
  145. {
  146. if (arch_has_pmem_api())
  147. return arch_copy_from_iter_pmem(addr, bytes, i);
  148. return default_copy_from_iter_pmem(addr, bytes, i);
  149. }
  150. /**
  151. * clear_pmem - zero a PMEM memory range
  152. * @addr: virtual start address
  153. * @size: number of bytes to zero
  154. *
  155. * Write zeros into the memory range starting at 'addr' for 'size' bytes.
  156. * This function requires explicit ordering with a wmb_pmem() call.
  157. */
  158. static inline void clear_pmem(void __pmem *addr, size_t size)
  159. {
  160. if (arch_has_pmem_api())
  161. arch_clear_pmem(addr, size);
  162. else
  163. default_clear_pmem(addr, size);
  164. }
  165. #endif /* __PMEM_H__ */