pmem.h 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright(c) 2015 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */
#ifndef __ASM_X86_PMEM_H__
#define __ASM_X86_PMEM_H__

#include <linux/uaccess.h>
#include <asm/cacheflush.h>
#include <asm/cpufeature.h>
#include <asm/special_insns.h>

21
#ifdef CONFIG_ARCH_HAS_PMEM_API
22 23 24 25 26 27 28
/**
 * arch_memcpy_to_pmem - copy data to persistent memory
 * @dst: destination buffer for the copy
 * @src: source buffer for the copy
 * @n: length of the copy in bytes
 *
 * Copy data to persistent memory media via non-temporal stores so that
D
Dan Williams 已提交
29
 * a subsequent pmem driver flush operation will drain posted write queues.
30
 */
D
Dan Williams 已提交
31
static inline void arch_memcpy_to_pmem(void *dst, const void *src, size_t n)
32
{
D
Dan Williams 已提交
33
	int rem;
34 35 36 37 38 39 40

	/*
	 * We are copying between two kernel buffers, if
	 * __copy_from_user_inatomic_nocache() returns an error (page
	 * fault) we would have already reported a general protection fault
	 * before the WARN+BUG.
	 */
D
Dan Williams 已提交
41 42 43
	rem = __copy_from_user_inatomic_nocache(dst, (void __user *) src, n);
	if (WARN(rem, "%s: fault copying %p <- %p unwritten: %d\n",
				__func__, dst, src, rem))
44 45 46
		BUG();
}

47
/**
48
 * arch_wb_cache_pmem - write back a cache range with CLWB
49 50 51 52
 * @vaddr:	virtual start address
 * @size:	number of bytes to write back
 *
 * Write back a cache range using the CLWB (cache line write back)
53 54
 * instruction. Note that @size is internally rounded up to be cache
 * line size aligned.
55
 */
D
Dan Williams 已提交
56
static inline void arch_wb_cache_pmem(void *addr, size_t size)
57 58 59
{
	u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
	unsigned long clflush_mask = x86_clflush_size - 1;
D
Dan Williams 已提交
60
	void *vend = addr + size;
61 62
	void *p;

D
Dan Williams 已提交
63
	for (p = (void *)((unsigned long)addr & ~clflush_mask);
64 65 66 67 68 69 70 71 72 73 74 75
	     p < vend; p += x86_clflush_size)
		clwb(p);
}

/**
 * arch_copy_from_iter_pmem - copy data from an iterator to PMEM
 * @addr:	PMEM destination address
 * @bytes:	number of bytes to copy
 * @i:		iterator with source data
 *
 * Copy data from the iterator 'i' to the PMEM buffer starting at 'addr'.
 */
D
Dan Williams 已提交
76
static inline size_t arch_copy_from_iter_pmem(void *addr, size_t bytes,
77 78 79 80 81
		struct iov_iter *i)
{
	size_t len;

	/* TODO: skip the write-back by always using non-temporal stores */
D
Dan Williams 已提交
82
	len = copy_from_iter_nocache(addr, bytes, i);
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	/*
	 * In the iovec case on x86_64 copy_from_iter_nocache() uses
	 * non-temporal stores for the bulk of the transfer, but we need
	 * to manually flush if the transfer is unaligned. A cached
	 * memory copy is used when destination or size is not naturally
	 * aligned. That is:
	 *   - Require 8-byte alignment when size is 8 bytes or larger.
	 *   - Require 4-byte alignment when size is 4 bytes.
	 *
	 * In the non-iovec case the entire destination needs to be
	 * flushed.
	 */
	if (iter_is_iovec(i)) {
		unsigned long flushed, dest = (unsigned long) addr;

		if (bytes < 8) {
			if (!IS_ALIGNED(dest, 4) || (bytes != 4))
				arch_wb_cache_pmem(addr, 1);
		} else {
			if (!IS_ALIGNED(dest, 8)) {
				dest = ALIGN(dest, boot_cpu_data.x86_clflush_size);
				arch_wb_cache_pmem(addr, 1);
			}

			flushed = dest - (unsigned long) addr;
			if (bytes > flushed && !IS_ALIGNED(bytes - flushed, 8))
				arch_wb_cache_pmem(addr + bytes - 1, 1);
		}
	} else
113
		arch_wb_cache_pmem(addr, bytes);
114 115 116 117 118 119 120 121 122 123 124

	return len;
}

/**
 * arch_clear_pmem - zero a PMEM memory range
 * @addr:	virtual start address
 * @size:	number of bytes to zero
 *
 * Write zeros into the memory range starting at 'addr' for 'size' bytes.
 */
D
Dan Williams 已提交
125
static inline void arch_clear_pmem(void *addr, size_t size)
126
{
D
Dan Williams 已提交
127
	memset(addr, 0, size);
128
	arch_wb_cache_pmem(addr, size);
129 130
}

D
Dan Williams 已提交
131
static inline void arch_invalidate_pmem(void *addr, size_t size)
132
{
D
Dan Williams 已提交
133
	clflush_cache_range(addr, size);
134
}
135
#endif /* CONFIG_ARCH_HAS_PMEM_API */
136
#endif /* __ASM_X86_PMEM_H__ */