flush.h 3.9 KB
Newer Older
Y
Yu Kuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (C) 2021. Huawei Technologies Co., Ltd. All rights reserved.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 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 EUFS_FLUSH_H
#define EUFS_FLUSH_H

#ifdef CONFIG_X86_64
static __always_inline bool arch_has_clwb(void)
{
	return static_cpu_has(X86_FEATURE_CLWB);
}

static __always_inline bool arch_has_clflushopt(void)
{
	return static_cpu_has(X86_FEATURE_CLFLUSHOPT);
}

static __always_inline bool arch_has_clflush(void)
{
	return static_cpu_has(X86_FEATURE_CLFLUSH);
}

static __always_inline bool arch_has_rtm(void)
{
	return static_cpu_has(X86_FEATURE_RTM);
}

static __always_inline void __sfence(void)
{
	asm volatile("sfence\n" : : : "memory");
}

static inline void _mm_clflush(const void *addr)
{
	asm volatile("clflush %0" : "+m"(*(volatile char *)(addr)));
}

static inline void _mm_clflushopt(const void *addr)
{
	asm volatile(".byte 0x66; clflush %0" : "+m"(*(volatile char *)(addr)));
}

static inline void _mm_clwb(const void *addr)
{
	asm volatile(".byte 0x66; xsaveopt %0" : "+m"(*(volatile char *)(addr)));
}

#else
static __always_inline bool arch_has_clwb(void)
{
	return false;
}

static __always_inline bool arch_has_clflushopt(void)
{
	return false;
}

static __always_inline bool arch_has_clflush(void)
{
	return false;
}

static __always_inline bool arch_has_rtm(void)
{
	return false;
}

static __always_inline void __sfence(void)
{
	/* arm64 doesn't support sfence */
	smp_mb();
}

#define _mm_clflush(addr) do {} while (0)
#define _mm_clflushopt(addr) do {} while (0)
#define _mm_clwb(addr) do {} while (0)
#endif

extern int support_rtm;
extern int support_clwb;
extern int support_clflushopt;
extern int support_clflush;
extern int clflush_delay;
extern int force_nocache_write;
extern int max_dirty_inodes;
extern int max_dep_nodes;

static __always_inline void eufs_sfence(void)
{
	__sfence();
}

static __always_inline void eufs_pbarrier(void)
{
	if (support_clwb || support_clflushopt)
		eufs_sfence();
}

static __always_inline void eufs_flush_cacheline(const void *ptr)
{
	if (support_clwb)
		_mm_clwb(ptr);
	else if (support_clflushopt)
		_mm_clflushopt(ptr);
	else if (support_clflush)
		_mm_clflush(ptr);
}

static __always_inline void eufs_flush_page(const void *ptr)
{
	uint32_t i;

	if (support_clwb) {
		for (i = 0; i < PAGE_SIZE; i += CACHELINE_SIZE)
			_mm_clwb(ptr + i);
	} else if (support_clflushopt) {
		for (i = 0; i < PAGE_SIZE; i += CACHELINE_SIZE)
			_mm_clflushopt(ptr + i);
	} else if (support_clflush) {
		for (i = 0; i < PAGE_SIZE; i += CACHELINE_SIZE)
			_mm_clflush(ptr + i);
	}
}

static __always_inline void eufs_flush_buffer(const void *buf, uint32_t len,
					       bool fence)
{
	uint32_t i;
	uint32_t aligned_len =
		len + ((unsigned long)(buf) & (CACHELINE_SIZE - 1));

	if (support_clwb) {
		for (i = 0; i < aligned_len; i += CACHELINE_SIZE)
			_mm_clwb(buf + i);
	} else if (support_clflushopt) {
		for (i = 0; i < aligned_len; i += CACHELINE_SIZE)
			_mm_clflushopt(buf + i);
	} else if (support_clflush) {
		for (i = 0; i < aligned_len; i += CACHELINE_SIZE) {
			/* flush the cache line that contains the address (buf + i) */
			_mm_clflush(buf + i);
		}
	}

	/*
	 * Do a fence only if asked. We often don't need to do a fence
	 * immediately after clflush because even if we get context switched
	 * between clflush and subsequent fence, the context switch operation
	 * provides implicit fence.
	 */
	if (fence)
		eufs_sfence();
}

static __always_inline void eufs_flush_range(const void *ptr, uint32_t len)
{
	eufs_flush_buffer(ptr, len, false);
}

#endif /* EUFS_FLUSH_H */