blob: e856c2cb0fe86da91d55e2766b687b66ad02cc1e [file] [log] [blame]
Ross Zwisler61031952015-06-25 03:08:39 -04001/*
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 Zwisler5de490d2015-08-18 13:55:39 -060017#include <linux/uio.h>
Ross Zwisler61031952015-06-25 03:08:39 -040018
19#ifdef CONFIG_ARCH_HAS_PMEM_API
Dan Williams96601ad2015-08-24 18:29:38 -040020#define ARCH_MEMREMAP_PMEM MEMREMAP_WB
Ross Zwisler40603522015-08-18 13:55:36 -060021#include <asm/pmem.h>
Ross Zwisler61031952015-06-25 03:08:39 -040022#else
Dan Williams96601ad2015-08-24 18:29:38 -040023#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 Williams7a9eb202016-06-03 18:06:47 -070029static inline void arch_memcpy_to_pmem(void *dst, const void *src, size_t n)
Ross Zwisler61031952015-06-25 03:08:39 -040030{
31 BUG();
32}
Ross Zwisler5de490d2015-08-18 13:55:39 -060033
Dan Williams7a9eb202016-06-03 18:06:47 -070034static inline int arch_memcpy_from_pmem(void *dst, const void *src, size_t n)
Dan Williamsfc0c2022016-03-08 10:30:19 -080035{
36 BUG();
37 return -EFAULT;
38}
39
Dan Williams7a9eb202016-06-03 18:06:47 -070040static inline size_t arch_copy_from_iter_pmem(void *addr, size_t bytes,
Ross Zwisler5de490d2015-08-18 13:55:39 -060041 struct iov_iter *i)
42{
43 BUG();
44 return 0;
45}
46
Dan Williams7a9eb202016-06-03 18:06:47 -070047static inline void arch_clear_pmem(void *addr, size_t size)
Ross Zwisler5de490d2015-08-18 13:55:39 -060048{
49 BUG();
50}
Ross Zwisler3f4a2672016-01-22 15:10:37 -080051
Dan Williams7a9eb202016-06-03 18:06:47 -070052static inline void arch_wb_cache_pmem(void *addr, size_t size)
Ross Zwisler3f4a2672016-01-22 15:10:37 -080053{
54 BUG();
55}
Dan Williams59e64732016-03-08 07:16:07 -080056
Dan Williams7a9eb202016-06-03 18:06:47 -070057static inline void arch_invalidate_pmem(void *addr, size_t size)
Dan Williams59e64732016-03-08 07:16:07 -080058{
59 BUG();
60}
Ross Zwisler61031952015-06-25 03:08:39 -040061#endif
62
Toshi Kanicba2e472016-04-12 18:10:52 -060063static inline bool arch_has_pmem_api(void)
64{
65 return IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API);
66}
67
Ross Zwisler61031952015-06-25 03:08:39 -040068/*
Dan Williamsfc0c2022016-03-08 10:30:19 -080069 * 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 Zwisler61031952015-06-25 03:08:39 -040075 */
Dan Williams7a9eb202016-06-03 18:06:47 -070076static inline int memcpy_from_pmem(void *dst, void const *src, size_t size)
Ross Zwisler61031952015-06-25 03:08:39 -040077{
Toshi Kanicba2e472016-04-12 18:10:52 -060078 if (arch_has_pmem_api())
79 return arch_memcpy_from_pmem(dst, src, size);
80 else
Dan Williams7a9eb202016-06-03 18:06:47 -070081 memcpy(dst, src, size);
82 return 0;
Ross Zwisler5de490d2015-08-18 13:55:39 -060083}
84
Ross Zwisler61031952015-06-25 03:08:39 -040085/**
Ross Zwisler61031952015-06-25 03:08:39 -040086 * 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 Williams7c8a6a72016-06-01 23:07:43 -070095 * must be followed by a blkdev_issue_flush() on the pmem block device.
Ross Zwisler61031952015-06-25 03:08:39 -040096 */
Dan Williams7a9eb202016-06-03 18:06:47 -070097static inline void memcpy_to_pmem(void *dst, const void *src, size_t n)
Ross Zwisler61031952015-06-25 03:08:39 -040098{
99 if (arch_has_pmem_api())
100 arch_memcpy_to_pmem(dst, src, n);
101 else
Dan Williams7a9eb202016-06-03 18:06:47 -0700102 memcpy(dst, src, n);
Ross Zwisler61031952015-06-25 03:08:39 -0400103}
104
105/**
Ross Zwisler5de490d2015-08-18 13:55:39 -0600106 * 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 Williams7c8a6a72016-06-01 23:07:43 -0700112 * See blkdev_issue_flush() note for memcpy_to_pmem().
Ross Zwisler5de490d2015-08-18 13:55:39 -0600113 */
Dan Williams7a9eb202016-06-03 18:06:47 -0700114static inline size_t copy_from_iter_pmem(void *addr, size_t bytes,
Ross Zwisler5de490d2015-08-18 13:55:39 -0600115 struct iov_iter *i)
116{
117 if (arch_has_pmem_api())
118 return arch_copy_from_iter_pmem(addr, bytes, i);
Dan Williams7a9eb202016-06-03 18:06:47 -0700119 return copy_from_iter_nocache(addr, bytes, i);
Ross Zwisler5de490d2015-08-18 13:55:39 -0600120}
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 Williams7c8a6a72016-06-01 23:07:43 -0700128 * See blkdev_issue_flush() note for memcpy_to_pmem().
Ross Zwisler5de490d2015-08-18 13:55:39 -0600129 */
Dan Williams7a9eb202016-06-03 18:06:47 -0700130static inline void clear_pmem(void *addr, size_t size)
Ross Zwisler5de490d2015-08-18 13:55:39 -0600131{
132 if (arch_has_pmem_api())
133 arch_clear_pmem(addr, size);
134 else
Dan Williams7a9eb202016-06-03 18:06:47 -0700135 memset(addr, 0, size);
Ross Zwisler5de490d2015-08-18 13:55:39 -0600136}
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800137
138/**
Dan Williams59e64732016-03-08 07:16:07 -0800139 * 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 Williams7a9eb202016-06-03 18:06:47 -0700146static inline void invalidate_pmem(void *addr, size_t size)
Dan Williams59e64732016-03-08 07:16:07 -0800147{
148 if (arch_has_pmem_api())
149 arch_invalidate_pmem(addr, size);
150}
151
152/**
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800153 * 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 Williams7c8a6a72016-06-01 23:07:43 -0700158 * See blkdev_issue_flush() note for memcpy_to_pmem().
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800159 */
Dan Williams7a9eb202016-06-03 18:06:47 -0700160static inline void wb_cache_pmem(void *addr, size_t size)
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800161{
162 if (arch_has_pmem_api())
163 arch_wb_cache_pmem(addr, size);
164}
Ross Zwisler61031952015-06-25 03:08:39 -0400165#endif /* __PMEM_H__ */