2015-08-19 02:55:36 +07:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2015-08-19 02:55:38 +07:00
|
|
|
#ifdef CONFIG_ARCH_HAS_PMEM_API
|
2015-08-19 02:55:36 +07:00
|
|
|
/**
|
|
|
|
* 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
|
2016-06-02 13:07:43 +07:00
|
|
|
* a subsequent pmem driver flush operation will drain posted write queues.
|
2015-08-19 02:55:36 +07:00
|
|
|
*/
|
2016-06-04 08:06:47 +07:00
|
|
|
static inline void arch_memcpy_to_pmem(void *dst, const void *src, size_t n)
|
2015-08-19 02:55:36 +07:00
|
|
|
{
|
2016-06-04 08:06:47 +07:00
|
|
|
int rem;
|
2015-08-19 02:55:36 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-06-04 08:06:47 +07:00
|
|
|
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))
|
2015-08-19 02:55:36 +07:00
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2016-06-04 08:06:47 +07:00
|
|
|
static inline int arch_memcpy_from_pmem(void *dst, const void *src, size_t n)
|
2016-03-09 01:30:19 +07:00
|
|
|
{
|
2016-09-02 01:39:33 +07:00
|
|
|
return memcpy_mcsafe(dst, src, n);
|
2016-03-09 01:30:19 +07:00
|
|
|
}
|
|
|
|
|
2015-08-19 02:55:39 +07:00
|
|
|
/**
|
2016-01-23 06:10:37 +07:00
|
|
|
* arch_wb_cache_pmem - write back a cache range with CLWB
|
2015-08-19 02:55:39 +07:00
|
|
|
* @vaddr: virtual start address
|
|
|
|
* @size: number of bytes to write back
|
|
|
|
*
|
|
|
|
* Write back a cache range using the CLWB (cache line write back)
|
2016-06-02 13:07:43 +07:00
|
|
|
* instruction.
|
2015-08-19 02:55:39 +07:00
|
|
|
*/
|
2016-06-04 08:06:47 +07:00
|
|
|
static inline void arch_wb_cache_pmem(void *addr, size_t size)
|
2015-08-19 02:55:39 +07:00
|
|
|
{
|
|
|
|
u16 x86_clflush_size = boot_cpu_data.x86_clflush_size;
|
|
|
|
unsigned long clflush_mask = x86_clflush_size - 1;
|
2016-06-04 08:06:47 +07:00
|
|
|
void *vend = addr + size;
|
2015-08-19 02:55:39 +07:00
|
|
|
void *p;
|
|
|
|
|
2016-06-04 08:06:47 +07:00
|
|
|
for (p = (void *)((unsigned long)addr & ~clflush_mask);
|
2015-08-19 02:55:39 +07:00
|
|
|
p < vend; p += x86_clflush_size)
|
|
|
|
clwb(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy_from_iter_nocache() on x86 only uses non-temporal stores for iovec
|
|
|
|
* iterators, so for other types (bvec & kvec) we must do a cache write-back.
|
|
|
|
*/
|
|
|
|
static inline bool __iter_needs_pmem_wb(struct iov_iter *i)
|
|
|
|
{
|
|
|
|
return iter_is_iovec(i) == false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'.
|
|
|
|
*/
|
2016-06-04 08:06:47 +07:00
|
|
|
static inline size_t arch_copy_from_iter_pmem(void *addr, size_t bytes,
|
2015-08-19 02:55:39 +07:00
|
|
|
struct iov_iter *i)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* TODO: skip the write-back by always using non-temporal stores */
|
2016-06-04 08:06:47 +07:00
|
|
|
len = copy_from_iter_nocache(addr, bytes, i);
|
2015-08-19 02:55:39 +07:00
|
|
|
|
|
|
|
if (__iter_needs_pmem_wb(i))
|
2016-01-23 06:10:37 +07:00
|
|
|
arch_wb_cache_pmem(addr, bytes);
|
2015-08-19 02:55:39 +07:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-06-04 08:06:47 +07:00
|
|
|
static inline void arch_clear_pmem(void *addr, size_t size)
|
2015-08-19 02:55:39 +07:00
|
|
|
{
|
2016-06-04 08:06:47 +07:00
|
|
|
memset(addr, 0, size);
|
2016-01-23 06:10:37 +07:00
|
|
|
arch_wb_cache_pmem(addr, size);
|
2015-08-19 02:55:39 +07:00
|
|
|
}
|
|
|
|
|
2016-06-04 08:06:47 +07:00
|
|
|
static inline void arch_invalidate_pmem(void *addr, size_t size)
|
2016-03-08 22:16:07 +07:00
|
|
|
{
|
2016-06-04 08:06:47 +07:00
|
|
|
clflush_cache_range(addr, size);
|
2016-03-08 22:16:07 +07:00
|
|
|
}
|
2015-08-19 02:55:38 +07:00
|
|
|
#endif /* CONFIG_ARCH_HAS_PMEM_API */
|
2015-08-19 02:55:36 +07:00
|
|
|
#endif /* __ASM_X86_PMEM_H__ */
|