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 | */ |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 29 | static inline void arch_memcpy_to_pmem(void *dst, const void *src, size_t n) |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 30 | { |
| 31 | BUG(); |
| 32 | } |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 33 | |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 34 | 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] | 35 | { |
| 36 | BUG(); |
| 37 | return -EFAULT; |
| 38 | } |
| 39 | |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 40 | 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] | 41 | struct iov_iter *i) |
| 42 | { |
| 43 | BUG(); |
| 44 | return 0; |
| 45 | } |
| 46 | |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 47 | static inline void arch_clear_pmem(void *addr, size_t size) |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 48 | { |
| 49 | BUG(); |
| 50 | } |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 51 | |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 52 | static inline void arch_wb_cache_pmem(void *addr, size_t size) |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 53 | { |
| 54 | BUG(); |
| 55 | } |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 56 | |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 57 | static inline void arch_invalidate_pmem(void *addr, size_t size) |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 58 | { |
| 59 | BUG(); |
| 60 | } |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 61 | #endif |
| 62 | |
Toshi Kani | cba2e47 | 2016-04-12 18:10:52 -0600 | [diff] [blame] | 63 | static inline bool arch_has_pmem_api(void) |
| 64 | { |
| 65 | return IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API); |
| 66 | } |
| 67 | |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 68 | /* |
Dan Williams | fc0c202 | 2016-03-08 10:30:19 -0800 | [diff] [blame] | 69 | * memcpy_from_pmem - read from persistent memory with error handling |
| 70 | * @dst: destination buffer |
| 71 | * @src: source buffer |
| 72 | * @size: transfer length |
| 73 | * |
| 74 | * Returns 0 on success negative error code on failure. |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 75 | */ |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 76 | static inline int memcpy_from_pmem(void *dst, void const *src, size_t size) |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 77 | { |
Toshi Kani | cba2e47 | 2016-04-12 18:10:52 -0600 | [diff] [blame] | 78 | if (arch_has_pmem_api()) |
| 79 | return arch_memcpy_from_pmem(dst, src, size); |
| 80 | else |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 81 | memcpy(dst, src, size); |
| 82 | return 0; |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 83 | } |
| 84 | |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 85 | /** |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 86 | * memcpy_to_pmem - copy data to persistent memory |
| 87 | * @dst: destination buffer for the copy |
| 88 | * @src: source buffer for the copy |
| 89 | * @n: length of the copy in bytes |
| 90 | * |
| 91 | * Perform a memory copy that results in the destination of the copy |
| 92 | * being effectively evicted from, or never written to, the processor |
| 93 | * cache hierarchy after the copy completes. After memcpy_to_pmem() |
| 94 | * data may still reside in cpu or platform buffers, so this operation |
Dan Williams | 7c8a6a7 | 2016-06-01 23:07:43 -0700 | [diff] [blame] | 95 | * must be followed by a blkdev_issue_flush() on the pmem block device. |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 96 | */ |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 97 | static inline void memcpy_to_pmem(void *dst, const void *src, size_t n) |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 98 | { |
| 99 | if (arch_has_pmem_api()) |
| 100 | arch_memcpy_to_pmem(dst, src, n); |
| 101 | else |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 102 | memcpy(dst, src, n); |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 106 | * copy_from_iter_pmem - copy data from an iterator to PMEM |
| 107 | * @addr: PMEM destination address |
| 108 | * @bytes: number of bytes to copy |
| 109 | * @i: iterator with source data |
| 110 | * |
| 111 | * Copy data from the iterator 'i' to the PMEM buffer starting at 'addr'. |
Dan Williams | 7c8a6a7 | 2016-06-01 23:07:43 -0700 | [diff] [blame] | 112 | * See blkdev_issue_flush() note for memcpy_to_pmem(). |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 113 | */ |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 114 | static inline size_t copy_from_iter_pmem(void *addr, size_t bytes, |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 115 | struct iov_iter *i) |
| 116 | { |
| 117 | if (arch_has_pmem_api()) |
| 118 | return arch_copy_from_iter_pmem(addr, bytes, i); |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 119 | return copy_from_iter_nocache(addr, bytes, i); |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * clear_pmem - zero a PMEM memory range |
| 124 | * @addr: virtual start address |
| 125 | * @size: number of bytes to zero |
| 126 | * |
| 127 | * Write zeros into the memory range starting at 'addr' for 'size' bytes. |
Dan Williams | 7c8a6a7 | 2016-06-01 23:07:43 -0700 | [diff] [blame] | 128 | * See blkdev_issue_flush() note for memcpy_to_pmem(). |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 129 | */ |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 130 | static inline void clear_pmem(void *addr, size_t size) |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 131 | { |
| 132 | if (arch_has_pmem_api()) |
| 133 | arch_clear_pmem(addr, size); |
| 134 | else |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 135 | memset(addr, 0, size); |
Ross Zwisler | 5de490d | 2015-08-18 13:55:39 -0600 | [diff] [blame] | 136 | } |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 137 | |
| 138 | /** |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 139 | * invalidate_pmem - flush a pmem range from the cache hierarchy |
| 140 | * @addr: virtual start address |
| 141 | * @size: bytes to invalidate (internally aligned to cache line size) |
| 142 | * |
| 143 | * For platforms that support clearing poison this flushes any poisoned |
| 144 | * ranges out of the cache |
| 145 | */ |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 146 | static inline void invalidate_pmem(void *addr, size_t size) |
Dan Williams | 59e6473 | 2016-03-08 07:16:07 -0800 | [diff] [blame] | 147 | { |
| 148 | if (arch_has_pmem_api()) |
| 149 | arch_invalidate_pmem(addr, size); |
| 150 | } |
| 151 | |
| 152 | /** |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 153 | * wb_cache_pmem - write back processor cache for PMEM memory range |
| 154 | * @addr: virtual start address |
| 155 | * @size: number of bytes to write back |
| 156 | * |
| 157 | * Write back the processor cache range starting at 'addr' for 'size' bytes. |
Dan Williams | 7c8a6a7 | 2016-06-01 23:07:43 -0700 | [diff] [blame] | 158 | * See blkdev_issue_flush() note for memcpy_to_pmem(). |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 159 | */ |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 160 | static inline void wb_cache_pmem(void *addr, size_t size) |
Ross Zwisler | 3f4a267 | 2016-01-22 15:10:37 -0800 | [diff] [blame] | 161 | { |
| 162 | if (arch_has_pmem_api()) |
| 163 | arch_wb_cache_pmem(addr, size); |
| 164 | } |
Ross Zwisler | 6103195 | 2015-06-25 03:08:39 -0400 | [diff] [blame] | 165 | #endif /* __PMEM_H__ */ |