pmem.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 __ASM_X86_PMEM_H__
  14. #define __ASM_X86_PMEM_H__
  15. #include <linux/uaccess.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/cpufeature.h>
  18. #include <asm/special_insns.h>
  19. #ifdef CONFIG_ARCH_HAS_PMEM_API
  20. /**
  21. * arch_memcpy_to_pmem - copy data to persistent memory
  22. * @dst: destination buffer for the copy
  23. * @src: source buffer for the copy
  24. * @n: length of the copy in bytes
  25. *
  26. * Copy data to persistent memory media via non-temporal stores so that
  27. * a subsequent pmem driver flush operation will drain posted write queues.
  28. */
  29. static inline void arch_memcpy_to_pmem(void *dst, const void *src, size_t n)
  30. {
  31. int rem;
  32. /*
  33. * We are copying between two kernel buffers, if
  34. * __copy_from_user_inatomic_nocache() returns an error (page
  35. * fault) we would have already reported a general protection fault
  36. * before the WARN+BUG.
  37. */
  38. rem = __copy_from_user_inatomic_nocache(dst, (void __user *) src, n);
  39. if (WARN(rem, "%s: fault copying %p <- %p unwritten: %d\n",
  40. __func__, dst, src, rem))
  41. BUG();
  42. }
  43. static inline int arch_memcpy_from_pmem(void *dst, const void *src, size_t n)
  44. {
  45. if (static_cpu_has(X86_FEATURE_MCE_RECOVERY))
  46. return memcpy_mcsafe(dst, src, n);
  47. memcpy(dst, src, n);
  48. return 0;
  49. }
  50. /**
  51. * arch_wb_cache_pmem - write back a cache range with CLWB
  52. * @vaddr: virtual start address
  53. * @size: number of bytes to write back
  54. *
  55. * Write back a cache range using the CLWB (cache line write back)
  56. * instruction.
  57. */
  58. static inline void arch_wb_cache_pmem(void *addr, size_t size)
  59. {
  60. u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
  61. unsigned long clflush_mask = x86_clflush_size - 1;
  62. void *vend = addr + size;
  63. void *p;
  64. for (p = (void *)((unsigned long)addr & ~clflush_mask);
  65. p < vend; p += x86_clflush_size)
  66. clwb(p);
  67. }
  68. /*
  69. * copy_from_iter_nocache() on x86 only uses non-temporal stores for iovec
  70. * iterators, so for other types (bvec & kvec) we must do a cache write-back.
  71. */
  72. static inline bool __iter_needs_pmem_wb(struct iov_iter *i)
  73. {
  74. return iter_is_iovec(i) == false;
  75. }
  76. /**
  77. * arch_copy_from_iter_pmem - copy data from an iterator to PMEM
  78. * @addr: PMEM destination address
  79. * @bytes: number of bytes to copy
  80. * @i: iterator with source data
  81. *
  82. * Copy data from the iterator 'i' to the PMEM buffer starting at 'addr'.
  83. */
  84. static inline size_t arch_copy_from_iter_pmem(void *addr, size_t bytes,
  85. struct iov_iter *i)
  86. {
  87. size_t len;
  88. /* TODO: skip the write-back by always using non-temporal stores */
  89. len = copy_from_iter_nocache(addr, bytes, i);
  90. if (__iter_needs_pmem_wb(i))
  91. arch_wb_cache_pmem(addr, bytes);
  92. return len;
  93. }
  94. /**
  95. * arch_clear_pmem - zero a PMEM memory range
  96. * @addr: virtual start address
  97. * @size: number of bytes to zero
  98. *
  99. * Write zeros into the memory range starting at 'addr' for 'size' bytes.
  100. */
  101. static inline void arch_clear_pmem(void *addr, size_t size)
  102. {
  103. memset(addr, 0, size);
  104. arch_wb_cache_pmem(addr, size);
  105. }
  106. static inline void arch_invalidate_pmem(void *addr, size_t size)
  107. {
  108. clflush_cache_range(addr, size);
  109. }
  110. #endif /* CONFIG_ARCH_HAS_PMEM_API */
  111. #endif /* __ASM_X86_PMEM_H__ */