Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [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 __PMEM_H__ |
| 14 | #define __PMEM_H__ |
| 15 | |
| 16 | #include <linux/io.h> |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 17 | #include <linux/uio.h> |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 18 | |
| 19 | #ifdef CONFIG_ARCH_HAS_PMEM_API |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 20 | #define ARCH_MEMREMAP_PMEM MEMREMAP_WB |
Ross Zwisler | 4060352 | 2015-08-18 13:55:36 -0600 | [diff] [blame] | 21 | #include <asm/pmem.h> |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 22 | #else |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 23 | #define ARCH_MEMREMAP_PMEM MEMREMAP_WT |
| 24 | /* |
| 25 | * These are simply here to enable compilation, all call sites gate |
| 26 | * calling these symbols with arch_has_pmem_api() and redirect to the |
| 27 | * implementation in asm/pmem.h. |
| 28 | */ |
| 29 | static inline bool __arch_has_wmb_pmem(void) |
| 30 | { |
| 31 | return false; |
| 32 | } |
| 33 | |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 34 | static inline void arch_wmb_pmem(void) |
| 35 | { |
| 36 | BUG(); |
| 37 | } |
| 38 | |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 39 | static inline void arch_memcpy_to_pmem(void __pmem *dst, const void *src, |
| 40 | size_t n) |
| 41 | { |
| 42 | BUG(); |
| 43 | } |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 44 | |
Dan Williams | fc0c202 | 2016-03-08 10:30:19 -0800 | [diff] [blame] | 45 | static inline int arch_memcpy_from_pmem(void *dst, const void __pmem *src, |
| 46 | size_t n) |
| 47 | { |
| 48 | BUG(); |
| 49 | return -EFAULT; |
| 50 | } |
| 51 | |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 52 | static inline size_t arch_copy_from_iter_pmem(void __pmem *addr, size_t bytes, |
| 53 | struct iov_iter *i) |
| 54 | { |
| 55 | BUG(); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static inline void arch_clear_pmem(void __pmem *addr, size_t size) |
| 60 | { |
| 61 | BUG(); |
| 62 | } |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 63 | |
| 64 | static inline void arch_wb_cache_pmem(void __pmem *addr, size_t size) |
| 65 | { |
| 66 | BUG(); |
| 67 | } |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 68 | |
| 69 | static inline void arch_invalidate_pmem(void __pmem *addr, size_t size) |
| 70 | { |
| 71 | BUG(); |
| 72 | } |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | /* |
Dan Williams | fc0c202 | 2016-03-08 10:30:19 -0800 | [diff] [blame] | 76 | * memcpy_from_pmem - read from persistent memory with error handling |
| 77 | * @dst: destination buffer |
| 78 | * @src: source buffer |
| 79 | * @size: transfer length |
| 80 | * |
| 81 | * Returns 0 on success negative error code on failure. |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 82 | */ |
Dan Williams | fc0c202 | 2016-03-08 10:30:19 -0800 | [diff] [blame] | 83 | static inline int memcpy_from_pmem(void *dst, void __pmem const *src, |
| 84 | size_t size) |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 85 | { |
Dan Williams | fc0c202 | 2016-03-08 10:30:19 -0800 | [diff] [blame] | 86 | return arch_memcpy_from_pmem(dst, src, size); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 89 | static inline bool arch_has_pmem_api(void) |
| 90 | { |
| 91 | return IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API); |
| 92 | } |
| 93 | |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 94 | /** |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 95 | * arch_has_wmb_pmem - true if wmb_pmem() ensures durability |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 96 | * |
| 97 | * For a given cpu implementation within an architecture it is possible |
| 98 | * that wmb_pmem() resolves to a nop. In the case this returns |
| 99 | * false, pmem api users are unable to ensure durability and may want to |
| 100 | * fall back to a different data consistency model, or otherwise notify |
| 101 | * the user. |
| 102 | */ |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 103 | static inline bool arch_has_wmb_pmem(void) |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 104 | { |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 105 | return arch_has_pmem_api() && __arch_has_wmb_pmem(); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | * These defaults seek to offer decent performance and minimize the |
| 110 | * window between i/o completion and writes being durable on media. |
| 111 | * However, it is undefined / architecture specific whether |
Dan Williams | a639315 | 2015-09-15 02:14:03 -0400 | [diff] [blame] | 112 | * ARCH_MEMREMAP_PMEM + default_memcpy_to_pmem is sufficient for |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 113 | * making data durable relative to i/o completion. |
| 114 | */ |
Dan Williams | e836a25 | 2015-08-12 18:42:56 -0400 | [diff] [blame] | 115 | static inline void default_memcpy_to_pmem(void __pmem *dst, const void *src, |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 116 | size_t size) |
| 117 | { |
| 118 | memcpy((void __force *) dst, src, size); |
| 119 | } |
| 120 | |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 121 | static inline size_t default_copy_from_iter_pmem(void __pmem *addr, |
| 122 | size_t bytes, struct iov_iter *i) |
| 123 | { |
| 124 | return copy_from_iter_nocache((void __force *)addr, bytes, i); |
| 125 | } |
| 126 | |
| 127 | static inline void default_clear_pmem(void __pmem *addr, size_t size) |
| 128 | { |
| 129 | if (size == PAGE_SIZE && ((unsigned long)addr & ~PAGE_MASK) == 0) |
| 130 | clear_page((void __force *)addr); |
| 131 | else |
| 132 | memset((void __force *)addr, 0, size); |
| 133 | } |
| 134 | |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 135 | /** |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 136 | * memcpy_to_pmem - copy data to persistent memory |
| 137 | * @dst: destination buffer for the copy |
| 138 | * @src: source buffer for the copy |
| 139 | * @n: length of the copy in bytes |
| 140 | * |
| 141 | * Perform a memory copy that results in the destination of the copy |
| 142 | * being effectively evicted from, or never written to, the processor |
| 143 | * cache hierarchy after the copy completes. After memcpy_to_pmem() |
| 144 | * data may still reside in cpu or platform buffers, so this operation |
| 145 | * must be followed by a wmb_pmem(). |
| 146 | */ |
| 147 | static inline void memcpy_to_pmem(void __pmem *dst, const void *src, size_t n) |
| 148 | { |
| 149 | if (arch_has_pmem_api()) |
| 150 | arch_memcpy_to_pmem(dst, src, n); |
| 151 | else |
| 152 | default_memcpy_to_pmem(dst, src, n); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * wmb_pmem - synchronize writes to persistent memory |
| 157 | * |
| 158 | * After a series of memcpy_to_pmem() operations this drains data from |
| 159 | * cpu write buffers and any platform (memory controller) buffers to |
| 160 | * ensure that written data is durable on persistent memory media. |
| 161 | */ |
| 162 | static inline void wmb_pmem(void) |
| 163 | { |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 164 | if (arch_has_wmb_pmem()) |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 165 | arch_wmb_pmem(); |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 166 | else |
| 167 | wmb(); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 168 | } |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * copy_from_iter_pmem - copy data from an iterator to PMEM |
| 172 | * @addr: PMEM destination address |
| 173 | * @bytes: number of bytes to copy |
| 174 | * @i: iterator with source data |
| 175 | * |
| 176 | * Copy data from the iterator 'i' to the PMEM buffer starting at 'addr'. |
| 177 | * This function requires explicit ordering with a wmb_pmem() call. |
| 178 | */ |
| 179 | static inline size_t copy_from_iter_pmem(void __pmem *addr, size_t bytes, |
| 180 | struct iov_iter *i) |
| 181 | { |
| 182 | if (arch_has_pmem_api()) |
| 183 | return arch_copy_from_iter_pmem(addr, bytes, i); |
| 184 | return default_copy_from_iter_pmem(addr, bytes, i); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * clear_pmem - zero a PMEM memory range |
| 189 | * @addr: virtual start address |
| 190 | * @size: number of bytes to zero |
| 191 | * |
| 192 | * Write zeros into the memory range starting at 'addr' for 'size' bytes. |
| 193 | * This function requires explicit ordering with a wmb_pmem() call. |
| 194 | */ |
| 195 | static inline void clear_pmem(void __pmem *addr, size_t size) |
| 196 | { |
| 197 | if (arch_has_pmem_api()) |
| 198 | arch_clear_pmem(addr, size); |
| 199 | else |
| 200 | default_clear_pmem(addr, size); |
| 201 | } |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 202 | |
| 203 | /** |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 204 | * invalidate_pmem - flush a pmem range from the cache hierarchy |
| 205 | * @addr: virtual start address |
| 206 | * @size: bytes to invalidate (internally aligned to cache line size) |
| 207 | * |
| 208 | * For platforms that support clearing poison this flushes any poisoned |
| 209 | * ranges out of the cache |
| 210 | */ |
| 211 | static inline void invalidate_pmem(void __pmem *addr, size_t size) |
| 212 | { |
| 213 | if (arch_has_pmem_api()) |
| 214 | arch_invalidate_pmem(addr, size); |
| 215 | } |
| 216 | |
| 217 | /** |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 218 | * wb_cache_pmem - write back processor cache for PMEM memory range |
| 219 | * @addr: virtual start address |
| 220 | * @size: number of bytes to write back |
| 221 | * |
| 222 | * Write back the processor cache range starting at 'addr' for 'size' bytes. |
| 223 | * This function requires explicit ordering with a wmb_pmem() call. |
| 224 | */ |
| 225 | static inline void wb_cache_pmem(void __pmem *addr, size_t size) |
| 226 | { |
| 227 | if (arch_has_pmem_api()) |
| 228 | arch_wb_cache_pmem(addr, size); |
| 229 | } |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 230 | #endif /* __PMEM_H__ */ |