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