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