2 * Copyright(c) 2015 Intel Corporation. All rights reserved.
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.
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.
18 #ifdef CONFIG_ARCH_HAS_PMEM_API
19 #include <asm/cacheflush.h>
21 static inline void arch_wmb_pmem(void)
26 static inline bool __arch_has_wmb_pmem(void)
31 static inline void __pmem *arch_memremap_pmem(resource_size_t offset,
37 static inline void arch_memcpy_to_pmem(void __pmem *dst, const void *src,
45 * Architectures that define ARCH_HAS_PMEM_API must provide
46 * implementations for arch_memremap_pmem(), arch_memcpy_to_pmem(),
47 * arch_wmb_pmem(), and __arch_has_wmb_pmem().
50 static inline void memcpy_from_pmem(void *dst, void __pmem const *src, size_t size)
52 memcpy(dst, (void __force const *) src, size);
55 static inline void memunmap_pmem(void __pmem *addr)
57 iounmap((void __force __iomem *) addr);
61 * arch_has_wmb_pmem - true if wmb_pmem() ensures durability
63 * For a given cpu implementation within an architecture it is possible
64 * that wmb_pmem() resolves to a nop. In the case this returns
65 * false, pmem api users are unable to ensure durability and may want to
66 * fall back to a different data consistency model, or otherwise notify
69 static inline bool arch_has_wmb_pmem(void)
71 if (IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API))
72 return __arch_has_wmb_pmem();
76 static inline bool arch_has_pmem_api(void)
78 return IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && arch_has_wmb_pmem();
82 * These defaults seek to offer decent performance and minimize the
83 * window between i/o completion and writes being durable on media.
84 * However, it is undefined / architecture specific whether
85 * default_memremap_pmem + default_memcpy_to_pmem is sufficient for
86 * making data durable relative to i/o completion.
88 static void default_memcpy_to_pmem(void __pmem *dst, const void *src,
91 memcpy((void __force *) dst, src, size);
94 static void __pmem *default_memremap_pmem(resource_size_t offset,
97 /* TODO: convert to ioremap_wt() */
98 return (void __pmem __force *)ioremap_nocache(offset, size);
102 * memremap_pmem - map physical persistent memory for pmem api
103 * @offset: physical address of persistent memory
104 * @size: size of the mapping
106 * Establish a mapping of the architecture specific memory type expected
107 * by memcpy_to_pmem() and wmb_pmem(). For example, it may be
108 * the case that an uncacheable or writethrough mapping is sufficient,
109 * or a writeback mapping provided memcpy_to_pmem() and
110 * wmb_pmem() arrange for the data to be written through the
111 * cache to persistent media.
113 static inline void __pmem *memremap_pmem(resource_size_t offset,
116 if (arch_has_pmem_api())
117 return arch_memremap_pmem(offset, size);
118 return default_memremap_pmem(offset, size);
122 * memcpy_to_pmem - copy data to persistent memory
123 * @dst: destination buffer for the copy
124 * @src: source buffer for the copy
125 * @n: length of the copy in bytes
127 * Perform a memory copy that results in the destination of the copy
128 * being effectively evicted from, or never written to, the processor
129 * cache hierarchy after the copy completes. After memcpy_to_pmem()
130 * data may still reside in cpu or platform buffers, so this operation
131 * must be followed by a wmb_pmem().
133 static inline void memcpy_to_pmem(void __pmem *dst, const void *src, size_t n)
135 if (arch_has_pmem_api())
136 arch_memcpy_to_pmem(dst, src, n);
138 default_memcpy_to_pmem(dst, src, n);
142 * wmb_pmem - synchronize writes to persistent memory
144 * After a series of memcpy_to_pmem() operations this drains data from
145 * cpu write buffers and any platform (memory controller) buffers to
146 * ensure that written data is durable on persistent memory media.
148 static inline void wmb_pmem(void)
150 if (arch_has_pmem_api())
153 #endif /* __PMEM_H__ */