blob: 2c1ebeb4d7376db6350b7266048a1d37d800ac86 [file] [log] [blame]
Ross Zwisler40603522015-08-18 13:55:36 -06001/*
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
Ross Zwisler4a370df2015-08-18 13:55:38 -060021#ifdef CONFIG_ARCH_HAS_PMEM_API
Ross Zwisler40603522015-08-18 13:55:36 -060022/**
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
Dan Williams7c8a6a72016-06-01 23:07:43 -070029 * a subsequent pmem driver flush operation will drain posted write queues.
Ross Zwisler40603522015-08-18 13:55:36 -060030 */
Dan Williams7a9eb202016-06-03 18:06:47 -070031static inline void arch_memcpy_to_pmem(void *dst, const void *src, size_t n)
Ross Zwisler40603522015-08-18 13:55:36 -060032{
Dan Williams7a9eb202016-06-03 18:06:47 -070033 int rem;
Ross Zwisler40603522015-08-18 13:55:36 -060034
35 /*
36 * We are copying between two kernel buffers, if
37 * __copy_from_user_inatomic_nocache() returns an error (page
38 * fault) we would have already reported a general protection fault
39 * before the WARN+BUG.
40 */
Dan Williams7a9eb202016-06-03 18:06:47 -070041 rem = __copy_from_user_inatomic_nocache(dst, (void __user *) src, n);
42 if (WARN(rem, "%s: fault copying %p <- %p unwritten: %d\n",
43 __func__, dst, src, rem))
Ross Zwisler40603522015-08-18 13:55:36 -060044 BUG();
45}
46
Dan Williams7a9eb202016-06-03 18:06:47 -070047static inline int arch_memcpy_from_pmem(void *dst, const void *src, size_t n)
Dan Williamsfc0c2022016-03-08 10:30:19 -080048{
Tony Luck9a6fb282016-09-01 11:39:33 -070049 return memcpy_mcsafe(dst, src, n);
Dan Williamsfc0c2022016-03-08 10:30:19 -080050}
51
Ross Zwisler40603522015-08-18 13:55:36 -060052/**
Ross Zwisler3f4a2672016-01-22 15:10:37 -080053 * arch_wb_cache_pmem - write back a cache range with CLWB
Ross Zwisler5de490d2015-08-18 13:55:39 -060054 * @vaddr: virtual start address
55 * @size: number of bytes to write back
56 *
57 * Write back a cache range using the CLWB (cache line write back)
Dan Williams7c8a6a72016-06-01 23:07:43 -070058 * instruction.
Ross Zwisler5de490d2015-08-18 13:55:39 -060059 */
Dan Williams7a9eb202016-06-03 18:06:47 -070060static inline void arch_wb_cache_pmem(void *addr, size_t size)
Ross Zwisler5de490d2015-08-18 13:55:39 -060061{
62 u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
63 unsigned long clflush_mask = x86_clflush_size - 1;
Dan Williams7a9eb202016-06-03 18:06:47 -070064 void *vend = addr + size;
Ross Zwisler5de490d2015-08-18 13:55:39 -060065 void *p;
66
Dan Williams7a9eb202016-06-03 18:06:47 -070067 for (p = (void *)((unsigned long)addr & ~clflush_mask);
Ross Zwisler5de490d2015-08-18 13:55:39 -060068 p < vend; p += x86_clflush_size)
69 clwb(p);
70}
71
72/*
73 * copy_from_iter_nocache() on x86 only uses non-temporal stores for iovec
74 * iterators, so for other types (bvec & kvec) we must do a cache write-back.
75 */
76static inline bool __iter_needs_pmem_wb(struct iov_iter *i)
77{
78 return iter_is_iovec(i) == false;
79}
80
81/**
82 * arch_copy_from_iter_pmem - copy data from an iterator to PMEM
83 * @addr: PMEM destination address
84 * @bytes: number of bytes to copy
85 * @i: iterator with source data
86 *
87 * Copy data from the iterator 'i' to the PMEM buffer starting at 'addr'.
Ross Zwisler5de490d2015-08-18 13:55:39 -060088 */
Dan Williams7a9eb202016-06-03 18:06:47 -070089static inline size_t arch_copy_from_iter_pmem(void *addr, size_t bytes,
Ross Zwisler5de490d2015-08-18 13:55:39 -060090 struct iov_iter *i)
91{
Ross Zwisler5de490d2015-08-18 13:55:39 -060092 size_t len;
93
94 /* TODO: skip the write-back by always using non-temporal stores */
Dan Williams7a9eb202016-06-03 18:06:47 -070095 len = copy_from_iter_nocache(addr, bytes, i);
Ross Zwisler5de490d2015-08-18 13:55:39 -060096
97 if (__iter_needs_pmem_wb(i))
Ross Zwisler3f4a2672016-01-22 15:10:37 -080098 arch_wb_cache_pmem(addr, bytes);
Ross Zwisler5de490d2015-08-18 13:55:39 -060099
100 return len;
101}
102
103/**
104 * arch_clear_pmem - zero a PMEM memory range
105 * @addr: virtual start address
106 * @size: number of bytes to zero
107 *
108 * Write zeros into the memory range starting at 'addr' for 'size' bytes.
Ross Zwisler5de490d2015-08-18 13:55:39 -0600109 */
Dan Williams7a9eb202016-06-03 18:06:47 -0700110static inline void arch_clear_pmem(void *addr, size_t size)
Ross Zwisler5de490d2015-08-18 13:55:39 -0600111{
Dan Williams7a9eb202016-06-03 18:06:47 -0700112 memset(addr, 0, size);
Ross Zwisler3f4a2672016-01-22 15:10:37 -0800113 arch_wb_cache_pmem(addr, size);
Ross Zwisler5de490d2015-08-18 13:55:39 -0600114}
115
Dan Williams7a9eb202016-06-03 18:06:47 -0700116static inline void arch_invalidate_pmem(void *addr, size_t size)
Dan Williams59e64732016-03-08 07:16:07 -0800117{
Dan Williams7a9eb202016-06-03 18:06:47 -0700118 clflush_cache_range(addr, size);
Dan Williams59e64732016-03-08 07:16:07 -0800119}
Ross Zwisler4a370df2015-08-18 13:55:38 -0600120#endif /* CONFIG_ARCH_HAS_PMEM_API */
Ross Zwisler40603522015-08-18 13:55:36 -0600121#endif /* __ASM_X86_PMEM_H__ */