GNU Linux-libre 4.4.285-gnu1
[releases.git] / arch / x86 / include / asm / pmem.h
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 __ASM_X86_PMEM_H__
14 #define __ASM_X86_PMEM_H__
15
16 #include <linux/uaccess.h>
17 #include <asm/cacheflush.h>
18 #include <asm/cpufeature.h>
19 #include <asm/special_insns.h>
20
21 #ifdef CONFIG_ARCH_HAS_PMEM_API
22 /**
23  * arch_memcpy_to_pmem - copy data to persistent memory
24  * @dst: destination buffer for the copy
25  * @src: source buffer for the copy
26  * @n: length of the copy in bytes
27  *
28  * Copy data to persistent memory media via non-temporal stores so that
29  * a subsequent arch_wmb_pmem() can flush cpu and memory controller
30  * write buffers to guarantee durability.
31  */
32 static inline void arch_memcpy_to_pmem(void __pmem *dst, const void *src,
33                 size_t n)
34 {
35         int unwritten;
36
37         /*
38          * We are copying between two kernel buffers, if
39          * __copy_from_user_inatomic_nocache() returns an error (page
40          * fault) we would have already reported a general protection fault
41          * before the WARN+BUG.
42          */
43         unwritten = __copy_from_user_inatomic_nocache((void __force *) dst,
44                         (void __user *) src, n);
45         if (WARN(unwritten, "%s: fault copying %p <- %p unwritten: %d\n",
46                                 __func__, dst, src, unwritten))
47                 BUG();
48 }
49
50 /**
51  * arch_wmb_pmem - synchronize writes to persistent memory
52  *
53  * After a series of arch_memcpy_to_pmem() operations this drains data
54  * from cpu write buffers and any platform (memory controller) buffers
55  * to ensure that written data is durable on persistent memory media.
56  */
57 static inline void arch_wmb_pmem(void)
58 {
59         /*
60          * wmb() to 'sfence' all previous writes such that they are
61          * architecturally visible to 'pcommit'.  Note, that we've
62          * already arranged for pmem writes to avoid the cache via
63          * arch_memcpy_to_pmem().
64          */
65         wmb();
66         pcommit_sfence();
67 }
68
69 /**
70  * __arch_wb_cache_pmem - write back a cache range with CLWB
71  * @vaddr:      virtual start address
72  * @size:       number of bytes to write back
73  *
74  * Write back a cache range using the CLWB (cache line write back)
75  * instruction. Note that @size is internally rounded up to be cache
76  * line size aligned.
77  */
78 static inline void __arch_wb_cache_pmem(void *vaddr, size_t size)
79 {
80         u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
81         unsigned long clflush_mask = x86_clflush_size - 1;
82         void *vend = vaddr + size;
83         void *p;
84
85         for (p = (void *)((unsigned long)vaddr & ~clflush_mask);
86              p < vend; p += x86_clflush_size)
87                 clwb(p);
88 }
89
90 /**
91  * arch_copy_from_iter_pmem - copy data from an iterator to PMEM
92  * @addr:       PMEM destination address
93  * @bytes:      number of bytes to copy
94  * @i:          iterator with source data
95  *
96  * Copy data from the iterator 'i' to the PMEM buffer starting at 'addr'.
97  * This function requires explicit ordering with an arch_wmb_pmem() call.
98  */
99 static inline size_t arch_copy_from_iter_pmem(void __pmem *addr, size_t bytes,
100                 struct iov_iter *i)
101 {
102         void *vaddr = (void __force *)addr;
103         size_t len;
104
105         /* TODO: skip the write-back by always using non-temporal stores */
106         len = copy_from_iter_nocache(vaddr, bytes, i);
107
108         /*
109          * In the iovec case on x86_64 copy_from_iter_nocache() uses
110          * non-temporal stores for the bulk of the transfer, but we need
111          * to manually flush if the transfer is unaligned. A cached
112          * memory copy is used when destination or size is not naturally
113          * aligned. That is:
114          *   - Require 8-byte alignment when size is 8 bytes or larger.
115          *   - Require 4-byte alignment when size is 4 bytes.
116          *
117          * In the non-iovec case the entire destination needs to be
118          * flushed.
119          */
120         if (iter_is_iovec(i)) {
121                 unsigned long flushed, dest = (unsigned long) addr;
122
123                 if (bytes < 8) {
124                         if (!IS_ALIGNED(dest, 4) || (bytes != 4))
125                                 __arch_wb_cache_pmem(addr, bytes);
126                 } else {
127                         if (!IS_ALIGNED(dest, 8)) {
128                                 dest = ALIGN(dest, boot_cpu_data.x86_clflush_size);
129                                 __arch_wb_cache_pmem(addr, 1);
130                         }
131
132                         flushed = dest - (unsigned long) addr;
133                         if (bytes > flushed && !IS_ALIGNED(bytes - flushed, 8))
134                                 __arch_wb_cache_pmem(addr + bytes - 1, 1);
135                 }
136         } else
137                 __arch_wb_cache_pmem(addr, bytes);
138
139         return len;
140 }
141
142 /**
143  * arch_clear_pmem - zero a PMEM memory range
144  * @addr:       virtual start address
145  * @size:       number of bytes to zero
146  *
147  * Write zeros into the memory range starting at 'addr' for 'size' bytes.
148  * This function requires explicit ordering with an arch_wmb_pmem() call.
149  */
150 static inline void arch_clear_pmem(void __pmem *addr, size_t size)
151 {
152         void *vaddr = (void __force *)addr;
153
154         /* TODO: implement the zeroing via non-temporal writes */
155         if (size == PAGE_SIZE && ((unsigned long)vaddr & ~PAGE_MASK) == 0)
156                 clear_page(vaddr);
157         else
158                 memset(vaddr, 0, size);
159
160         __arch_wb_cache_pmem(vaddr, size);
161 }
162
163 static inline bool __arch_has_wmb_pmem(void)
164 {
165         /*
166          * We require that wmb() be an 'sfence', that is only guaranteed on
167          * 64-bit builds
168          */
169         return static_cpu_has(X86_FEATURE_PCOMMIT);
170 }
171 #endif /* CONFIG_ARCH_HAS_PMEM_API */
172 #endif /* __ASM_X86_PMEM_H__ */