blob: 71ecf3d46aacb210edfed793d5ab0ece06671d72 [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 size_t arch_copy_from_iter_pmem(void *addr, size_t bytes,
Ross Zwisler5de490d2015-08-18 13:55:39 -060035 struct iov_iter *i)
36{
37 BUG();
38 return 0;
39}
40
Dan Williams7a9eb202016-06-03 18:06:47 -070041static inline void arch_clear_pmem(void *addr, size_t size)
Ross Zwisler5de490d2015-08-18 13:55:39 -060042{
43 BUG();
44}
Ross Zwisler3f4a2672016-01-22 15:10:37 -080045
Dan Williams7a9eb202016-06-03 18:06:47 -070046static inline void arch_wb_cache_pmem(void *addr, size_t size)
Ross Zwisler3f4a2672016-01-22 15:10:37 -080047{
48 BUG();
49}
Dan Williams59e64732016-03-08 07:16:07 -080050
Dan Williams7a9eb202016-06-03 18:06:47 -070051static inline void arch_invalidate_pmem(void *addr, size_t size)
Dan Williams59e64732016-03-08 07:16:07 -080052{
53 BUG();
54}
Ross Zwisler61031952015-06-25 03:08:39 -040055#endif
56
Toshi Kanicba2e472016-04-12 18:10:52 -060057static inline bool arch_has_pmem_api(void)
58{
59 return IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API);
60}
61
Ross Zwisler61031952015-06-25 03:08:39 -040062/**
Ross Zwisler61031952015-06-25 03:08:39 -040063 * 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 Williams7c8a6a72016-06-01 23:07:43 -070072 * must be followed by a blkdev_issue_flush() on the pmem block device.
Ross Zwisler61031952015-06-25 03:08:39 -040073 */
Dan Williams7a9eb202016-06-03 18:06:47 -070074static inline void memcpy_to_pmem(void *dst, const void *src, size_t n)
Ross Zwisler61031952015-06-25 03:08:39 -040075{
76 if (arch_has_pmem_api())
77 arch_memcpy_to_pmem(dst, src, n);
78 else
Dan Williams7a9eb202016-06-03 18:06:47 -070079 memcpy(dst, src, n);
Ross Zwisler61031952015-06-25 03:08:39 -040080}
81
82/**
Ross Zwisler5de490d2015-08-18 13:55:39 -060083 * 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 Williams7c8a6a72016-06-01 23:07:43 -070089 * See blkdev_issue_flush() note for memcpy_to_pmem().
Ross Zwisler5de490d2015-08-18 13:55:39 -060090 */
Dan Williams7a9eb202016-06-03 18:06:47 -070091static inline size_t copy_from_iter_pmem(void *addr, size_t bytes,
Ross Zwisler5de490d2015-08-18 13:55:39 -060092 struct iov_iter *i)
93{
94 if (arch_has_pmem_api())
95 return arch_copy_from_iter_pmem(addr, bytes, i);
Dan Williams7a9eb202016-06-03 18:06:47 -070096 return copy_from_iter_nocache(addr, bytes, i);
Ross Zwisler5de490d2015-08-18 13:55:39 -060097}
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 Williams7c8a6a72016-06-01 23:07:43 -0700105 * See blkdev_issue_flush() note for memcpy_to_pmem().
Ross Zwisler5de490d2015-08-18 13:55:39 -0600106 */
Dan Williams7a9eb202016-06-03 18:06:47 -0700107static inline void clear_pmem(void *addr, size_t size)
Ross Zwisler5de490d2015-08-18 13:55:39 -0600108{
109 if (arch_has_pmem_api())
110 arch_clear_pmem(addr, size);
111 else
Dan Williams7a9eb202016-06-03 18:06:47 -0700112 memset(addr, 0, size);
Ross Zwisler5de490d2015-08-18 13:55:39 -0600113}
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800114
115/**
Dan Williams59e64732016-03-08 07:16:07 -0800116 * 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 Williams7a9eb202016-06-03 18:06:47 -0700123static inline void invalidate_pmem(void *addr, size_t size)
Dan Williams59e64732016-03-08 07:16:07 -0800124{
125 if (arch_has_pmem_api())
126 arch_invalidate_pmem(addr, size);
127}
128
129/**
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800130 * 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 Williams7c8a6a72016-06-01 23:07:43 -0700135 * See blkdev_issue_flush() note for memcpy_to_pmem().
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800136 */
Dan Williams7a9eb202016-06-03 18:06:47 -0700137static inline void wb_cache_pmem(void *addr, size_t size)
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800138{
139 if (arch_has_pmem_api())
140 arch_wb_cache_pmem(addr, size);
141}
Ross Zwisler61031952015-06-25 03:08:39 -0400142#endif /* __PMEM_H__ */