xref: /linux-6.15/arch/powerpc/kernel/security.c (revision e7304597)
19a868f63SMichael Ellerman // SPDX-License-Identifier: GPL-2.0+
29a868f63SMichael Ellerman //
39a868f63SMichael Ellerman // Security related flags and so on.
49a868f63SMichael Ellerman //
59a868f63SMichael Ellerman // Copyright 2018, Michael Ellerman, IBM Corporation.
69a868f63SMichael Ellerman 
742e2acdeSBreno Leitao #include <linux/cpu.h>
89a868f63SMichael Ellerman #include <linux/kernel.h>
98ad33041SMichael Ellerman #include <linux/device.h>
10c6b4c914SMichael Ellerman #include <linux/memblock.h>
11d93e5e2dSMichael Ellerman #include <linux/nospec.h>
12d93e5e2dSMichael Ellerman #include <linux/prctl.h>
13ff348355SMichael Ellerman #include <linux/seq_buf.h>
148ad33041SMichael Ellerman 
15ee13cb24SMichael Ellerman #include <asm/asm-prototypes.h>
16ee13cb24SMichael Ellerman #include <asm/code-patching.h>
17a048a07dSNicholas Piggin #include <asm/debugfs.h>
189a868f63SMichael Ellerman #include <asm/security_features.h>
192eea7f06SMichal Suchanek #include <asm/setup.h>
2075346251SJordan Niethe #include <asm/inst.h>
219a868f63SMichael Ellerman 
22c6b4c914SMichael Ellerman #include "setup.h"
239a868f63SMichael Ellerman 
243b05a1e5SGeert Uytterhoeven u64 powerpc_security_features __read_mostly = SEC_FTR_DEFAULT;
258ad33041SMichael Ellerman 
261026798cSNicholas Piggin enum branch_cache_flush_type {
271026798cSNicholas Piggin 	BRANCH_CACHE_FLUSH_NONE	= 0x1,
281026798cSNicholas Piggin 	BRANCH_CACHE_FLUSH_SW	= 0x2,
291026798cSNicholas Piggin 	BRANCH_CACHE_FLUSH_HW	= 0x4,
30ee13cb24SMichael Ellerman };
311026798cSNicholas Piggin static enum branch_cache_flush_type count_cache_flush_type = BRANCH_CACHE_FLUSH_NONE;
32c06ac277SNicholas Piggin static enum branch_cache_flush_type link_stack_flush_type = BRANCH_CACHE_FLUSH_NONE;
33ee13cb24SMichael Ellerman 
34815069caSMichal Suchanek bool barrier_nospec_enabled;
35cf175dc3SDiana Craciun static bool no_nospec;
36f633a8adSDiana Craciun static bool btb_flush_enabled;
37d8f0e0b0SChristopher M. Riedl #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
38f633a8adSDiana Craciun static bool no_spectrev2;
39f633a8adSDiana Craciun #endif
402eea7f06SMichal Suchanek 
412eea7f06SMichal Suchanek static void enable_barrier_nospec(bool enable)
422eea7f06SMichal Suchanek {
432eea7f06SMichal Suchanek 	barrier_nospec_enabled = enable;
442eea7f06SMichal Suchanek 	do_barrier_nospec_fixups(enable);
452eea7f06SMichal Suchanek }
462eea7f06SMichal Suchanek 
47cb3d6759SMichal Suchanek void setup_barrier_nospec(void)
48cb3d6759SMichal Suchanek {
49cb3d6759SMichal Suchanek 	bool enable;
50cb3d6759SMichal Suchanek 
51cb3d6759SMichal Suchanek 	/*
52cb3d6759SMichal Suchanek 	 * It would make sense to check SEC_FTR_SPEC_BAR_ORI31 below as well.
53cb3d6759SMichal Suchanek 	 * But there's a good reason not to. The two flags we check below are
54cb3d6759SMichal Suchanek 	 * both are enabled by default in the kernel, so if the hcall is not
55cb3d6759SMichal Suchanek 	 * functional they will be enabled.
56cb3d6759SMichal Suchanek 	 * On a system where the host firmware has been updated (so the ori
57cb3d6759SMichal Suchanek 	 * functions as a barrier), but on which the hypervisor (KVM/Qemu) has
58cb3d6759SMichal Suchanek 	 * not been updated, we would like to enable the barrier. Dropping the
59cb3d6759SMichal Suchanek 	 * check for SEC_FTR_SPEC_BAR_ORI31 achieves that. The only downside is
60cb3d6759SMichal Suchanek 	 * we potentially enable the barrier on systems where the host firmware
61cb3d6759SMichal Suchanek 	 * is not updated, but that's harmless as it's a no-op.
62cb3d6759SMichal Suchanek 	 */
63cb3d6759SMichal Suchanek 	enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
64cb3d6759SMichal Suchanek 		 security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR);
65cb3d6759SMichal Suchanek 
66782e69efSJosh Poimboeuf 	if (!no_nospec && !cpu_mitigations_off())
67cb3d6759SMichal Suchanek 		enable_barrier_nospec(enable);
68cb3d6759SMichal Suchanek }
69cb3d6759SMichal Suchanek 
70cf175dc3SDiana Craciun static int __init handle_nospectre_v1(char *p)
71cf175dc3SDiana Craciun {
72cf175dc3SDiana Craciun 	no_nospec = true;
73cf175dc3SDiana Craciun 
74cf175dc3SDiana Craciun 	return 0;
75cf175dc3SDiana Craciun }
76cf175dc3SDiana Craciun early_param("nospectre_v1", handle_nospectre_v1);
77cf175dc3SDiana Craciun 
78cb3d6759SMichal Suchanek #ifdef CONFIG_DEBUG_FS
79cb3d6759SMichal Suchanek static int barrier_nospec_set(void *data, u64 val)
80cb3d6759SMichal Suchanek {
81cb3d6759SMichal Suchanek 	switch (val) {
82cb3d6759SMichal Suchanek 	case 0:
83cb3d6759SMichal Suchanek 	case 1:
84cb3d6759SMichal Suchanek 		break;
85cb3d6759SMichal Suchanek 	default:
86cb3d6759SMichal Suchanek 		return -EINVAL;
87cb3d6759SMichal Suchanek 	}
88cb3d6759SMichal Suchanek 
89cb3d6759SMichal Suchanek 	if (!!val == !!barrier_nospec_enabled)
90cb3d6759SMichal Suchanek 		return 0;
91cb3d6759SMichal Suchanek 
92cb3d6759SMichal Suchanek 	enable_barrier_nospec(!!val);
93cb3d6759SMichal Suchanek 
94cb3d6759SMichal Suchanek 	return 0;
95cb3d6759SMichal Suchanek }
96cb3d6759SMichal Suchanek 
97cb3d6759SMichal Suchanek static int barrier_nospec_get(void *data, u64 *val)
98cb3d6759SMichal Suchanek {
99cb3d6759SMichal Suchanek 	*val = barrier_nospec_enabled ? 1 : 0;
100cb3d6759SMichal Suchanek 	return 0;
101cb3d6759SMichal Suchanek }
102cb3d6759SMichal Suchanek 
103090d5ab9SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(fops_barrier_nospec, barrier_nospec_get,
104090d5ab9SYueHaibing 			 barrier_nospec_set, "%llu\n");
105cb3d6759SMichal Suchanek 
106cb3d6759SMichal Suchanek static __init int barrier_nospec_debugfs_init(void)
107cb3d6759SMichal Suchanek {
108090d5ab9SYueHaibing 	debugfs_create_file_unsafe("barrier_nospec", 0600,
109090d5ab9SYueHaibing 				   powerpc_debugfs_root, NULL,
110cb3d6759SMichal Suchanek 				   &fops_barrier_nospec);
111cb3d6759SMichal Suchanek 	return 0;
112cb3d6759SMichal Suchanek }
113cb3d6759SMichal Suchanek device_initcall(barrier_nospec_debugfs_init);
114398af571SMichael Ellerman 
115398af571SMichael Ellerman static __init int security_feature_debugfs_init(void)
116398af571SMichael Ellerman {
117398af571SMichael Ellerman 	debugfs_create_x64("security_features", 0400, powerpc_debugfs_root,
1183b05a1e5SGeert Uytterhoeven 			   &powerpc_security_features);
119398af571SMichael Ellerman 	return 0;
120398af571SMichael Ellerman }
121398af571SMichael Ellerman device_initcall(security_feature_debugfs_init);
122cb3d6759SMichal Suchanek #endif /* CONFIG_DEBUG_FS */
123cb3d6759SMichal Suchanek 
124d8f0e0b0SChristopher M. Riedl #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
125f633a8adSDiana Craciun static int __init handle_nospectre_v2(char *p)
126f633a8adSDiana Craciun {
127f633a8adSDiana Craciun 	no_spectrev2 = true;
128f633a8adSDiana Craciun 
129f633a8adSDiana Craciun 	return 0;
130f633a8adSDiana Craciun }
131f633a8adSDiana Craciun early_param("nospectre_v2", handle_nospectre_v2);
132d8f0e0b0SChristopher M. Riedl #endif /* CONFIG_PPC_FSL_BOOK3E || CONFIG_PPC_BOOK3S_64 */
133d8f0e0b0SChristopher M. Riedl 
134d8f0e0b0SChristopher M. Riedl #ifdef CONFIG_PPC_FSL_BOOK3E
135f633a8adSDiana Craciun void setup_spectre_v2(void)
136f633a8adSDiana Craciun {
137782e69efSJosh Poimboeuf 	if (no_spectrev2 || cpu_mitigations_off())
138f633a8adSDiana Craciun 		do_btb_flush_fixups();
139f633a8adSDiana Craciun 	else
140f633a8adSDiana Craciun 		btb_flush_enabled = true;
141f633a8adSDiana Craciun }
142f633a8adSDiana Craciun #endif /* CONFIG_PPC_FSL_BOOK3E */
143f633a8adSDiana Craciun 
144406d2b6aSDiana Craciun #ifdef CONFIG_PPC_BOOK3S_64
1458ad33041SMichael Ellerman ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1468ad33041SMichael Ellerman {
147ff348355SMichael Ellerman 	bool thread_priv;
148ff348355SMichael Ellerman 
149ff348355SMichael Ellerman 	thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV);
150ff348355SMichael Ellerman 
1514e706af3SGustavo L. F. Walbon 	if (rfi_flush) {
152ff348355SMichael Ellerman 		struct seq_buf s;
153ff348355SMichael Ellerman 		seq_buf_init(&s, buf, PAGE_SIZE - 1);
154ff348355SMichael Ellerman 
1554e706af3SGustavo L. F. Walbon 		seq_buf_printf(&s, "Mitigation: RFI Flush");
156ff348355SMichael Ellerman 		if (thread_priv)
1574e706af3SGustavo L. F. Walbon 			seq_buf_printf(&s, ", L1D private per thread");
158ff348355SMichael Ellerman 
159ff348355SMichael Ellerman 		seq_buf_printf(&s, "\n");
160ff348355SMichael Ellerman 
161ff348355SMichael Ellerman 		return s.len;
162ff348355SMichael Ellerman 	}
163ff348355SMichael Ellerman 
1644e706af3SGustavo L. F. Walbon 	if (thread_priv)
1654e706af3SGustavo L. F. Walbon 		return sprintf(buf, "Vulnerable: L1D private per thread\n");
1664e706af3SGustavo L. F. Walbon 
167ff348355SMichael Ellerman 	if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
168ff348355SMichael Ellerman 	    !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
169ff348355SMichael Ellerman 		return sprintf(buf, "Not affected\n");
1708ad33041SMichael Ellerman 
1718ad33041SMichael Ellerman 	return sprintf(buf, "Vulnerable\n");
1728ad33041SMichael Ellerman }
1738e6b6da9SAnthony Steinhauser 
1748e6b6da9SAnthony Steinhauser ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1758e6b6da9SAnthony Steinhauser {
1768e6b6da9SAnthony Steinhauser 	return cpu_show_meltdown(dev, attr, buf);
1778e6b6da9SAnthony Steinhauser }
178406d2b6aSDiana Craciun #endif
17956986016SMichael Ellerman 
18056986016SMichael Ellerman ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
18156986016SMichael Ellerman {
1826d44acaeSMichael Ellerman 	struct seq_buf s;
18356986016SMichael Ellerman 
1846d44acaeSMichael Ellerman 	seq_buf_init(&s, buf, PAGE_SIZE - 1);
1856d44acaeSMichael Ellerman 
1866d44acaeSMichael Ellerman 	if (security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) {
187a3775145SMichal Suchanek 		if (barrier_nospec_enabled)
1886d44acaeSMichael Ellerman 			seq_buf_printf(&s, "Mitigation: __user pointer sanitization");
1896d44acaeSMichael Ellerman 		else
1906d44acaeSMichael Ellerman 			seq_buf_printf(&s, "Vulnerable");
191a3775145SMichal Suchanek 
1926d44acaeSMichael Ellerman 		if (security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31))
1936d44acaeSMichael Ellerman 			seq_buf_printf(&s, ", ori31 speculation barrier enabled");
1946d44acaeSMichael Ellerman 
1956d44acaeSMichael Ellerman 		seq_buf_printf(&s, "\n");
1966d44acaeSMichael Ellerman 	} else
1976d44acaeSMichael Ellerman 		seq_buf_printf(&s, "Not affected\n");
1986d44acaeSMichael Ellerman 
1996d44acaeSMichael Ellerman 	return s.len;
20056986016SMichael Ellerman }
201d6fbe1c5SMichael Ellerman 
202d6fbe1c5SMichael Ellerman ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
203d6fbe1c5SMichael Ellerman {
204d6fbe1c5SMichael Ellerman 	struct seq_buf s;
2056d44acaeSMichael Ellerman 	bool bcs, ccd;
206d6fbe1c5SMichael Ellerman 
207d6fbe1c5SMichael Ellerman 	seq_buf_init(&s, buf, PAGE_SIZE - 1);
208d6fbe1c5SMichael Ellerman 
209d6fbe1c5SMichael Ellerman 	bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED);
210d6fbe1c5SMichael Ellerman 	ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED);
211d6fbe1c5SMichael Ellerman 
21292edf8dfSMichael Ellerman 	if (bcs || ccd) {
213d6fbe1c5SMichael Ellerman 		seq_buf_printf(&s, "Mitigation: ");
214d6fbe1c5SMichael Ellerman 
21592edf8dfSMichael Ellerman 		if (bcs)
216d6fbe1c5SMichael Ellerman 			seq_buf_printf(&s, "Indirect branch serialisation (kernel only)");
217d6fbe1c5SMichael Ellerman 
21892edf8dfSMichael Ellerman 		if (bcs && ccd)
219ee13cb24SMichael Ellerman 			seq_buf_printf(&s, ", ");
22092edf8dfSMichael Ellerman 
22192edf8dfSMichael Ellerman 		if (ccd)
222ee13cb24SMichael Ellerman 			seq_buf_printf(&s, "Indirect branch cache disabled");
22339e72bf9SMichael Ellerman 
2241026798cSNicholas Piggin 	} else if (count_cache_flush_type != BRANCH_CACHE_FLUSH_NONE) {
22592edf8dfSMichael Ellerman 		seq_buf_printf(&s, "Mitigation: Software count cache flush");
226ee13cb24SMichael Ellerman 
2271026798cSNicholas Piggin 		if (count_cache_flush_type == BRANCH_CACHE_FLUSH_HW)
228ee13cb24SMichael Ellerman 			seq_buf_printf(&s, " (hardware accelerated)");
22939e72bf9SMichael Ellerman 
230dfa88658SDiana Craciun 	} else if (btb_flush_enabled) {
231dfa88658SDiana Craciun 		seq_buf_printf(&s, "Mitigation: Branch predictor state flush");
232dfa88658SDiana Craciun 	} else {
233d6fbe1c5SMichael Ellerman 		seq_buf_printf(&s, "Vulnerable");
234dfa88658SDiana Craciun 	}
235d6fbe1c5SMichael Ellerman 
2364d24e21cSNicholas Piggin 	if (bcs || ccd || count_cache_flush_type != BRANCH_CACHE_FLUSH_NONE) {
2374d24e21cSNicholas Piggin 		if (link_stack_flush_type != BRANCH_CACHE_FLUSH_NONE)
2384d24e21cSNicholas Piggin 			seq_buf_printf(&s, ", Software link stack flush");
2394d24e21cSNicholas Piggin 		if (link_stack_flush_type == BRANCH_CACHE_FLUSH_HW)
2404d24e21cSNicholas Piggin 			seq_buf_printf(&s, " (hardware accelerated)");
2414d24e21cSNicholas Piggin 	}
2424d24e21cSNicholas Piggin 
243d6fbe1c5SMichael Ellerman 	seq_buf_printf(&s, "\n");
244d6fbe1c5SMichael Ellerman 
245d6fbe1c5SMichael Ellerman 	return s.len;
246d6fbe1c5SMichael Ellerman }
247a048a07dSNicholas Piggin 
2486453b532SDiana Craciun #ifdef CONFIG_PPC_BOOK3S_64
249a048a07dSNicholas Piggin /*
250a048a07dSNicholas Piggin  * Store-forwarding barrier support.
251a048a07dSNicholas Piggin  */
252a048a07dSNicholas Piggin 
253a048a07dSNicholas Piggin static enum stf_barrier_type stf_enabled_flush_types;
254a048a07dSNicholas Piggin static bool no_stf_barrier;
2557f262b4dSLi Huafei static bool stf_barrier;
256a048a07dSNicholas Piggin 
257a048a07dSNicholas Piggin static int __init handle_no_stf_barrier(char *p)
258a048a07dSNicholas Piggin {
259a048a07dSNicholas Piggin 	pr_info("stf-barrier: disabled on command line.");
260a048a07dSNicholas Piggin 	no_stf_barrier = true;
261a048a07dSNicholas Piggin 	return 0;
262a048a07dSNicholas Piggin }
263a048a07dSNicholas Piggin 
264a048a07dSNicholas Piggin early_param("no_stf_barrier", handle_no_stf_barrier);
265a048a07dSNicholas Piggin 
266a048a07dSNicholas Piggin /* This is the generic flag used by other architectures */
267a048a07dSNicholas Piggin static int __init handle_ssbd(char *p)
268a048a07dSNicholas Piggin {
269a048a07dSNicholas Piggin 	if (!p || strncmp(p, "auto", 5) == 0 || strncmp(p, "on", 2) == 0 ) {
270a048a07dSNicholas Piggin 		/* Until firmware tells us, we have the barrier with auto */
271a048a07dSNicholas Piggin 		return 0;
272a048a07dSNicholas Piggin 	} else if (strncmp(p, "off", 3) == 0) {
273a048a07dSNicholas Piggin 		handle_no_stf_barrier(NULL);
274a048a07dSNicholas Piggin 		return 0;
275a048a07dSNicholas Piggin 	} else
276a048a07dSNicholas Piggin 		return 1;
277a048a07dSNicholas Piggin 
278a048a07dSNicholas Piggin 	return 0;
279a048a07dSNicholas Piggin }
280a048a07dSNicholas Piggin early_param("spec_store_bypass_disable", handle_ssbd);
281a048a07dSNicholas Piggin 
282a048a07dSNicholas Piggin /* This is the generic flag used by other architectures */
283a048a07dSNicholas Piggin static int __init handle_no_ssbd(char *p)
284a048a07dSNicholas Piggin {
285a048a07dSNicholas Piggin 	handle_no_stf_barrier(NULL);
286a048a07dSNicholas Piggin 	return 0;
287a048a07dSNicholas Piggin }
288a048a07dSNicholas Piggin early_param("nospec_store_bypass_disable", handle_no_ssbd);
289a048a07dSNicholas Piggin 
290a048a07dSNicholas Piggin static void stf_barrier_enable(bool enable)
291a048a07dSNicholas Piggin {
292a048a07dSNicholas Piggin 	if (enable)
293a048a07dSNicholas Piggin 		do_stf_barrier_fixups(stf_enabled_flush_types);
294a048a07dSNicholas Piggin 	else
295a048a07dSNicholas Piggin 		do_stf_barrier_fixups(STF_BARRIER_NONE);
296a048a07dSNicholas Piggin 
297a048a07dSNicholas Piggin 	stf_barrier = enable;
298a048a07dSNicholas Piggin }
299a048a07dSNicholas Piggin 
300a048a07dSNicholas Piggin void setup_stf_barrier(void)
301a048a07dSNicholas Piggin {
302a048a07dSNicholas Piggin 	enum stf_barrier_type type;
303a048a07dSNicholas Piggin 	bool enable, hv;
304a048a07dSNicholas Piggin 
305a048a07dSNicholas Piggin 	hv = cpu_has_feature(CPU_FTR_HVMODE);
306a048a07dSNicholas Piggin 
307a048a07dSNicholas Piggin 	/* Default to fallback in case fw-features are not available */
308a048a07dSNicholas Piggin 	if (cpu_has_feature(CPU_FTR_ARCH_300))
309a048a07dSNicholas Piggin 		type = STF_BARRIER_EIEIO;
310a048a07dSNicholas Piggin 	else if (cpu_has_feature(CPU_FTR_ARCH_207S))
311a048a07dSNicholas Piggin 		type = STF_BARRIER_SYNC_ORI;
312a048a07dSNicholas Piggin 	else if (cpu_has_feature(CPU_FTR_ARCH_206))
313a048a07dSNicholas Piggin 		type = STF_BARRIER_FALLBACK;
314a048a07dSNicholas Piggin 	else
315a048a07dSNicholas Piggin 		type = STF_BARRIER_NONE;
316a048a07dSNicholas Piggin 
317a048a07dSNicholas Piggin 	enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
318a048a07dSNicholas Piggin 		(security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) ||
319a048a07dSNicholas Piggin 		 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && hv));
320a048a07dSNicholas Piggin 
321a048a07dSNicholas Piggin 	if (type == STF_BARRIER_FALLBACK) {
322a048a07dSNicholas Piggin 		pr_info("stf-barrier: fallback barrier available\n");
323a048a07dSNicholas Piggin 	} else if (type == STF_BARRIER_SYNC_ORI) {
324a048a07dSNicholas Piggin 		pr_info("stf-barrier: hwsync barrier available\n");
325a048a07dSNicholas Piggin 	} else if (type == STF_BARRIER_EIEIO) {
326a048a07dSNicholas Piggin 		pr_info("stf-barrier: eieio barrier available\n");
327a048a07dSNicholas Piggin 	}
328a048a07dSNicholas Piggin 
329a048a07dSNicholas Piggin 	stf_enabled_flush_types = type;
330a048a07dSNicholas Piggin 
331782e69efSJosh Poimboeuf 	if (!no_stf_barrier && !cpu_mitigations_off())
332a048a07dSNicholas Piggin 		stf_barrier_enable(enable);
333a048a07dSNicholas Piggin }
334a048a07dSNicholas Piggin 
335a048a07dSNicholas Piggin ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
336a048a07dSNicholas Piggin {
337a048a07dSNicholas Piggin 	if (stf_barrier && stf_enabled_flush_types != STF_BARRIER_NONE) {
338a048a07dSNicholas Piggin 		const char *type;
339a048a07dSNicholas Piggin 		switch (stf_enabled_flush_types) {
340a048a07dSNicholas Piggin 		case STF_BARRIER_EIEIO:
341a048a07dSNicholas Piggin 			type = "eieio";
342a048a07dSNicholas Piggin 			break;
343a048a07dSNicholas Piggin 		case STF_BARRIER_SYNC_ORI:
344a048a07dSNicholas Piggin 			type = "hwsync";
345a048a07dSNicholas Piggin 			break;
346a048a07dSNicholas Piggin 		case STF_BARRIER_FALLBACK:
347a048a07dSNicholas Piggin 			type = "fallback";
348a048a07dSNicholas Piggin 			break;
349a048a07dSNicholas Piggin 		default:
350a048a07dSNicholas Piggin 			type = "unknown";
351a048a07dSNicholas Piggin 		}
352a048a07dSNicholas Piggin 		return sprintf(buf, "Mitigation: Kernel entry/exit barrier (%s)\n", type);
353a048a07dSNicholas Piggin 	}
354a048a07dSNicholas Piggin 
355a048a07dSNicholas Piggin 	if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
356a048a07dSNicholas Piggin 	    !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
357a048a07dSNicholas Piggin 		return sprintf(buf, "Not affected\n");
358a048a07dSNicholas Piggin 
359a048a07dSNicholas Piggin 	return sprintf(buf, "Vulnerable\n");
360a048a07dSNicholas Piggin }
361a048a07dSNicholas Piggin 
362d93e5e2dSMichael Ellerman static int ssb_prctl_get(struct task_struct *task)
363d93e5e2dSMichael Ellerman {
364d93e5e2dSMichael Ellerman 	if (stf_enabled_flush_types == STF_BARRIER_NONE)
365d93e5e2dSMichael Ellerman 		/*
366d93e5e2dSMichael Ellerman 		 * We don't have an explicit signal from firmware that we're
367d93e5e2dSMichael Ellerman 		 * vulnerable or not, we only have certain CPU revisions that
368d93e5e2dSMichael Ellerman 		 * are known to be vulnerable.
369d93e5e2dSMichael Ellerman 		 *
370d93e5e2dSMichael Ellerman 		 * We assume that if we're on another CPU, where the barrier is
371d93e5e2dSMichael Ellerman 		 * NONE, then we are not vulnerable.
372d93e5e2dSMichael Ellerman 		 */
373d93e5e2dSMichael Ellerman 		return PR_SPEC_NOT_AFFECTED;
374d93e5e2dSMichael Ellerman 	else
375d93e5e2dSMichael Ellerman 		/*
376d93e5e2dSMichael Ellerman 		 * If we do have a barrier type then we are vulnerable. The
377d93e5e2dSMichael Ellerman 		 * barrier is not a global or per-process mitigation, so the
378d93e5e2dSMichael Ellerman 		 * only value we can report here is PR_SPEC_ENABLE, which
379d93e5e2dSMichael Ellerman 		 * appears as "vulnerable" in /proc.
380d93e5e2dSMichael Ellerman 		 */
381d93e5e2dSMichael Ellerman 		return PR_SPEC_ENABLE;
382d93e5e2dSMichael Ellerman 
383d93e5e2dSMichael Ellerman 	return -EINVAL;
384d93e5e2dSMichael Ellerman }
385d93e5e2dSMichael Ellerman 
386d93e5e2dSMichael Ellerman int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
387d93e5e2dSMichael Ellerman {
388d93e5e2dSMichael Ellerman 	switch (which) {
389d93e5e2dSMichael Ellerman 	case PR_SPEC_STORE_BYPASS:
390d93e5e2dSMichael Ellerman 		return ssb_prctl_get(task);
391d93e5e2dSMichael Ellerman 	default:
392d93e5e2dSMichael Ellerman 		return -ENODEV;
393d93e5e2dSMichael Ellerman 	}
394d93e5e2dSMichael Ellerman }
395d93e5e2dSMichael Ellerman 
396a048a07dSNicholas Piggin #ifdef CONFIG_DEBUG_FS
397a048a07dSNicholas Piggin static int stf_barrier_set(void *data, u64 val)
398a048a07dSNicholas Piggin {
399a048a07dSNicholas Piggin 	bool enable;
400a048a07dSNicholas Piggin 
401a048a07dSNicholas Piggin 	if (val == 1)
402a048a07dSNicholas Piggin 		enable = true;
403a048a07dSNicholas Piggin 	else if (val == 0)
404a048a07dSNicholas Piggin 		enable = false;
405a048a07dSNicholas Piggin 	else
406a048a07dSNicholas Piggin 		return -EINVAL;
407a048a07dSNicholas Piggin 
408a048a07dSNicholas Piggin 	/* Only do anything if we're changing state */
409a048a07dSNicholas Piggin 	if (enable != stf_barrier)
410a048a07dSNicholas Piggin 		stf_barrier_enable(enable);
411a048a07dSNicholas Piggin 
412a048a07dSNicholas Piggin 	return 0;
413a048a07dSNicholas Piggin }
414a048a07dSNicholas Piggin 
415a048a07dSNicholas Piggin static int stf_barrier_get(void *data, u64 *val)
416a048a07dSNicholas Piggin {
417a048a07dSNicholas Piggin 	*val = stf_barrier ? 1 : 0;
418a048a07dSNicholas Piggin 	return 0;
419a048a07dSNicholas Piggin }
420a048a07dSNicholas Piggin 
421090d5ab9SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(fops_stf_barrier, stf_barrier_get, stf_barrier_set,
422090d5ab9SYueHaibing 			 "%llu\n");
423a048a07dSNicholas Piggin 
424a048a07dSNicholas Piggin static __init int stf_barrier_debugfs_init(void)
425a048a07dSNicholas Piggin {
426090d5ab9SYueHaibing 	debugfs_create_file_unsafe("stf_barrier", 0600, powerpc_debugfs_root,
427090d5ab9SYueHaibing 				   NULL, &fops_stf_barrier);
428a048a07dSNicholas Piggin 	return 0;
429a048a07dSNicholas Piggin }
430a048a07dSNicholas Piggin device_initcall(stf_barrier_debugfs_init);
431a048a07dSNicholas Piggin #endif /* CONFIG_DEBUG_FS */
432ee13cb24SMichael Ellerman 
433c0036549SNicholas Piggin static void update_branch_cache_flush(void)
434ee13cb24SMichael Ellerman {
435792254a7SNicholas Piggin 	u32 *site;
436792254a7SNicholas Piggin 
437c0036549SNicholas Piggin #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
438792254a7SNicholas Piggin 	site = &patch__call_kvm_flush_link_stack;
439c0036549SNicholas Piggin 	// This controls the branch from guest_exit_cont to kvm_flush_link_stack
440c0036549SNicholas Piggin 	if (link_stack_flush_type == BRANCH_CACHE_FLUSH_NONE) {
441*e7304597SChristophe Leroy 		patch_instruction_site(site, ppc_inst(PPC_RAW_NOP()));
442c0036549SNicholas Piggin 	} else {
4434d24e21cSNicholas Piggin 		// Could use HW flush, but that could also flush count cache
444792254a7SNicholas Piggin 		patch_branch_site(site, (u64)&kvm_flush_link_stack, BRANCH_SET_LINK);
445c0036549SNicholas Piggin 	}
446c0036549SNicholas Piggin #endif
447c0036549SNicholas Piggin 
448792254a7SNicholas Piggin 	// Patch out the bcctr first, then nop the rest
449792254a7SNicholas Piggin 	site = &patch__call_flush_branch_caches3;
450*e7304597SChristophe Leroy 	patch_instruction_site(site, ppc_inst(PPC_RAW_NOP()));
451792254a7SNicholas Piggin 	site = &patch__call_flush_branch_caches2;
452*e7304597SChristophe Leroy 	patch_instruction_site(site, ppc_inst(PPC_RAW_NOP()));
453792254a7SNicholas Piggin 	site = &patch__call_flush_branch_caches1;
454*e7304597SChristophe Leroy 	patch_instruction_site(site, ppc_inst(PPC_RAW_NOP()));
455792254a7SNicholas Piggin 
456c0036549SNicholas Piggin 	// This controls the branch from _switch to flush_branch_caches
457c0036549SNicholas Piggin 	if (count_cache_flush_type == BRANCH_CACHE_FLUSH_NONE &&
458c0036549SNicholas Piggin 	    link_stack_flush_type == BRANCH_CACHE_FLUSH_NONE) {
459792254a7SNicholas Piggin 		// Nothing to be done
460792254a7SNicholas Piggin 
4614d24e21cSNicholas Piggin 	} else if (count_cache_flush_type == BRANCH_CACHE_FLUSH_HW &&
4624d24e21cSNicholas Piggin 		   link_stack_flush_type == BRANCH_CACHE_FLUSH_HW) {
463792254a7SNicholas Piggin 		// Patch in the bcctr last
464792254a7SNicholas Piggin 		site = &patch__call_flush_branch_caches1;
465792254a7SNicholas Piggin 		patch_instruction_site(site, ppc_inst(0x39207fff)); // li r9,0x7fff
466792254a7SNicholas Piggin 		site = &patch__call_flush_branch_caches2;
467792254a7SNicholas Piggin 		patch_instruction_site(site, ppc_inst(0x7d2903a6)); // mtctr r9
468792254a7SNicholas Piggin 		site = &patch__call_flush_branch_caches3;
469792254a7SNicholas Piggin 		patch_instruction_site(site, ppc_inst(PPC_INST_BCCTR_FLUSH));
470792254a7SNicholas Piggin 
471c0036549SNicholas Piggin 	} else {
472792254a7SNicholas Piggin 		patch_branch_site(site, (u64)&flush_branch_caches, BRANCH_SET_LINK);
473c0036549SNicholas Piggin 
474c0036549SNicholas Piggin 		// If we just need to flush the link stack, early return
475c0036549SNicholas Piggin 		if (count_cache_flush_type == BRANCH_CACHE_FLUSH_NONE) {
476c0036549SNicholas Piggin 			patch_instruction_site(&patch__flush_link_stack_return,
477*e7304597SChristophe Leroy 					       ppc_inst(PPC_RAW_BLR()));
478c0036549SNicholas Piggin 
479c0036549SNicholas Piggin 		// If we have flush instruction, early return
480c0036549SNicholas Piggin 		} else if (count_cache_flush_type == BRANCH_CACHE_FLUSH_HW) {
481c0036549SNicholas Piggin 			patch_instruction_site(&patch__flush_count_cache_return,
482*e7304597SChristophe Leroy 					       ppc_inst(PPC_RAW_BLR()));
483c0036549SNicholas Piggin 		}
484c0036549SNicholas Piggin 	}
48539e72bf9SMichael Ellerman }
48639e72bf9SMichael Ellerman 
4871026798cSNicholas Piggin static void toggle_branch_cache_flush(bool enable)
48839e72bf9SMichael Ellerman {
489c0036549SNicholas Piggin 	if (!enable || !security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE)) {
490c0036549SNicholas Piggin 		if (count_cache_flush_type != BRANCH_CACHE_FLUSH_NONE)
491c0036549SNicholas Piggin 			count_cache_flush_type = BRANCH_CACHE_FLUSH_NONE;
49239e72bf9SMichael Ellerman 
493c0036549SNicholas Piggin 		pr_info("count-cache-flush: flush disabled.\n");
494c0036549SNicholas Piggin 	} else {
495c0036549SNicholas Piggin 		if (security_ftr_enabled(SEC_FTR_BCCTR_FLUSH_ASSIST)) {
4961026798cSNicholas Piggin 			count_cache_flush_type = BRANCH_CACHE_FLUSH_HW;
4971afe00c7SNicholas Piggin 			pr_info("count-cache-flush: hardware flush enabled.\n");
498c0036549SNicholas Piggin 		} else {
499c0036549SNicholas Piggin 			count_cache_flush_type = BRANCH_CACHE_FLUSH_SW;
500c0036549SNicholas Piggin 			pr_info("count-cache-flush: software flush enabled.\n");
501c0036549SNicholas Piggin 		}
502c0036549SNicholas Piggin 	}
503c0036549SNicholas Piggin 
504c0036549SNicholas Piggin 	if (!enable || !security_ftr_enabled(SEC_FTR_FLUSH_LINK_STACK)) {
505c0036549SNicholas Piggin 		if (link_stack_flush_type != BRANCH_CACHE_FLUSH_NONE)
506c0036549SNicholas Piggin 			link_stack_flush_type = BRANCH_CACHE_FLUSH_NONE;
507c0036549SNicholas Piggin 
508c0036549SNicholas Piggin 		pr_info("link-stack-flush: flush disabled.\n");
509c0036549SNicholas Piggin 	} else {
5104d24e21cSNicholas Piggin 		if (security_ftr_enabled(SEC_FTR_BCCTR_LINK_FLUSH_ASSIST)) {
5114d24e21cSNicholas Piggin 			link_stack_flush_type = BRANCH_CACHE_FLUSH_HW;
5124d24e21cSNicholas Piggin 			pr_info("link-stack-flush: hardware flush enabled.\n");
5134d24e21cSNicholas Piggin 		} else {
514c0036549SNicholas Piggin 			link_stack_flush_type = BRANCH_CACHE_FLUSH_SW;
515c0036549SNicholas Piggin 			pr_info("link-stack-flush: software flush enabled.\n");
516c0036549SNicholas Piggin 		}
5174d24e21cSNicholas Piggin 	}
518c0036549SNicholas Piggin 
519c0036549SNicholas Piggin 	update_branch_cache_flush();
520ee13cb24SMichael Ellerman }
521ee13cb24SMichael Ellerman 
522ee13cb24SMichael Ellerman void setup_count_cache_flush(void)
523ee13cb24SMichael Ellerman {
524d8f0e0b0SChristopher M. Riedl 	bool enable = true;
525d8f0e0b0SChristopher M. Riedl 
526d8f0e0b0SChristopher M. Riedl 	if (no_spectrev2 || cpu_mitigations_off()) {
527d8f0e0b0SChristopher M. Riedl 		if (security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED) ||
528d8f0e0b0SChristopher M. Riedl 		    security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED))
52939e72bf9SMichael Ellerman 			pr_warn("Spectre v2 mitigations not fully under software control, can't disable\n");
530d8f0e0b0SChristopher M. Riedl 
531d8f0e0b0SChristopher M. Riedl 		enable = false;
532d8f0e0b0SChristopher M. Riedl 	}
533d8f0e0b0SChristopher M. Riedl 
53439e72bf9SMichael Ellerman 	/*
53539e72bf9SMichael Ellerman 	 * There's no firmware feature flag/hypervisor bit to tell us we need to
53639e72bf9SMichael Ellerman 	 * flush the link stack on context switch. So we set it here if we see
53739e72bf9SMichael Ellerman 	 * either of the Spectre v2 mitigations that aim to protect userspace.
53839e72bf9SMichael Ellerman 	 */
53939e72bf9SMichael Ellerman 	if (security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED) ||
54039e72bf9SMichael Ellerman 	    security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE))
54139e72bf9SMichael Ellerman 		security_ftr_set(SEC_FTR_FLUSH_LINK_STACK);
54239e72bf9SMichael Ellerman 
5431026798cSNicholas Piggin 	toggle_branch_cache_flush(enable);
544ee13cb24SMichael Ellerman }
545ee13cb24SMichael Ellerman 
546c6b4c914SMichael Ellerman static enum l1d_flush_type enabled_flush_types;
547c6b4c914SMichael Ellerman static void *l1d_flush_fallback_area;
548c6b4c914SMichael Ellerman static bool no_rfi_flush;
549c6b4c914SMichael Ellerman static bool no_entry_flush;
550c6b4c914SMichael Ellerman static bool no_uaccess_flush;
551c6b4c914SMichael Ellerman bool rfi_flush;
552c6b4c914SMichael Ellerman static bool entry_flush;
553c6b4c914SMichael Ellerman static bool uaccess_flush;
554c6b4c914SMichael Ellerman DEFINE_STATIC_KEY_FALSE(uaccess_flush_key);
555c6b4c914SMichael Ellerman EXPORT_SYMBOL(uaccess_flush_key);
556c6b4c914SMichael Ellerman 
557c6b4c914SMichael Ellerman static int __init handle_no_rfi_flush(char *p)
558c6b4c914SMichael Ellerman {
559c6b4c914SMichael Ellerman 	pr_info("rfi-flush: disabled on command line.");
560c6b4c914SMichael Ellerman 	no_rfi_flush = true;
561c6b4c914SMichael Ellerman 	return 0;
562c6b4c914SMichael Ellerman }
563c6b4c914SMichael Ellerman early_param("no_rfi_flush", handle_no_rfi_flush);
564c6b4c914SMichael Ellerman 
565c6b4c914SMichael Ellerman static int __init handle_no_entry_flush(char *p)
566c6b4c914SMichael Ellerman {
567c6b4c914SMichael Ellerman 	pr_info("entry-flush: disabled on command line.");
568c6b4c914SMichael Ellerman 	no_entry_flush = true;
569c6b4c914SMichael Ellerman 	return 0;
570c6b4c914SMichael Ellerman }
571c6b4c914SMichael Ellerman early_param("no_entry_flush", handle_no_entry_flush);
572c6b4c914SMichael Ellerman 
573c6b4c914SMichael Ellerman static int __init handle_no_uaccess_flush(char *p)
574c6b4c914SMichael Ellerman {
575c6b4c914SMichael Ellerman 	pr_info("uaccess-flush: disabled on command line.");
576c6b4c914SMichael Ellerman 	no_uaccess_flush = true;
577c6b4c914SMichael Ellerman 	return 0;
578c6b4c914SMichael Ellerman }
579c6b4c914SMichael Ellerman early_param("no_uaccess_flush", handle_no_uaccess_flush);
580c6b4c914SMichael Ellerman 
581c6b4c914SMichael Ellerman /*
582c6b4c914SMichael Ellerman  * The RFI flush is not KPTI, but because users will see doco that says to use
583c6b4c914SMichael Ellerman  * nopti we hijack that option here to also disable the RFI flush.
584c6b4c914SMichael Ellerman  */
585c6b4c914SMichael Ellerman static int __init handle_no_pti(char *p)
586c6b4c914SMichael Ellerman {
587c6b4c914SMichael Ellerman 	pr_info("rfi-flush: disabling due to 'nopti' on command line.\n");
588c6b4c914SMichael Ellerman 	handle_no_rfi_flush(NULL);
589c6b4c914SMichael Ellerman 	return 0;
590c6b4c914SMichael Ellerman }
591c6b4c914SMichael Ellerman early_param("nopti", handle_no_pti);
592c6b4c914SMichael Ellerman 
593c6b4c914SMichael Ellerman static void do_nothing(void *unused)
594c6b4c914SMichael Ellerman {
595c6b4c914SMichael Ellerman 	/*
596c6b4c914SMichael Ellerman 	 * We don't need to do the flush explicitly, just enter+exit kernel is
597c6b4c914SMichael Ellerman 	 * sufficient, the RFI exit handlers will do the right thing.
598c6b4c914SMichael Ellerman 	 */
599c6b4c914SMichael Ellerman }
600c6b4c914SMichael Ellerman 
601c6b4c914SMichael Ellerman void rfi_flush_enable(bool enable)
602c6b4c914SMichael Ellerman {
603c6b4c914SMichael Ellerman 	if (enable) {
604c6b4c914SMichael Ellerman 		do_rfi_flush_fixups(enabled_flush_types);
605c6b4c914SMichael Ellerman 		on_each_cpu(do_nothing, NULL, 1);
606c6b4c914SMichael Ellerman 	} else
607c6b4c914SMichael Ellerman 		do_rfi_flush_fixups(L1D_FLUSH_NONE);
608c6b4c914SMichael Ellerman 
609c6b4c914SMichael Ellerman 	rfi_flush = enable;
610c6b4c914SMichael Ellerman }
611c6b4c914SMichael Ellerman 
612c6b4c914SMichael Ellerman static void entry_flush_enable(bool enable)
613c6b4c914SMichael Ellerman {
614c6b4c914SMichael Ellerman 	if (enable) {
615c6b4c914SMichael Ellerman 		do_entry_flush_fixups(enabled_flush_types);
616c6b4c914SMichael Ellerman 		on_each_cpu(do_nothing, NULL, 1);
617c6b4c914SMichael Ellerman 	} else {
618c6b4c914SMichael Ellerman 		do_entry_flush_fixups(L1D_FLUSH_NONE);
619c6b4c914SMichael Ellerman 	}
620c6b4c914SMichael Ellerman 
621c6b4c914SMichael Ellerman 	entry_flush = enable;
622c6b4c914SMichael Ellerman }
623c6b4c914SMichael Ellerman 
624c6b4c914SMichael Ellerman static void uaccess_flush_enable(bool enable)
625c6b4c914SMichael Ellerman {
626c6b4c914SMichael Ellerman 	if (enable) {
627c6b4c914SMichael Ellerman 		do_uaccess_flush_fixups(enabled_flush_types);
628c6b4c914SMichael Ellerman 		static_branch_enable(&uaccess_flush_key);
629c6b4c914SMichael Ellerman 		on_each_cpu(do_nothing, NULL, 1);
630c6b4c914SMichael Ellerman 	} else {
631c6b4c914SMichael Ellerman 		static_branch_disable(&uaccess_flush_key);
632c6b4c914SMichael Ellerman 		do_uaccess_flush_fixups(L1D_FLUSH_NONE);
633c6b4c914SMichael Ellerman 	}
634c6b4c914SMichael Ellerman 
635c6b4c914SMichael Ellerman 	uaccess_flush = enable;
636c6b4c914SMichael Ellerman }
637c6b4c914SMichael Ellerman 
638c6b4c914SMichael Ellerman static void __ref init_fallback_flush(void)
639c6b4c914SMichael Ellerman {
640c6b4c914SMichael Ellerman 	u64 l1d_size, limit;
641c6b4c914SMichael Ellerman 	int cpu;
642c6b4c914SMichael Ellerman 
643c6b4c914SMichael Ellerman 	/* Only allocate the fallback flush area once (at boot time). */
644c6b4c914SMichael Ellerman 	if (l1d_flush_fallback_area)
645c6b4c914SMichael Ellerman 		return;
646c6b4c914SMichael Ellerman 
647c6b4c914SMichael Ellerman 	l1d_size = ppc64_caches.l1d.size;
648c6b4c914SMichael Ellerman 
649c6b4c914SMichael Ellerman 	/*
650c6b4c914SMichael Ellerman 	 * If there is no d-cache-size property in the device tree, l1d_size
651c6b4c914SMichael Ellerman 	 * could be zero. That leads to the loop in the asm wrapping around to
652c6b4c914SMichael Ellerman 	 * 2^64-1, and then walking off the end of the fallback area and
653c6b4c914SMichael Ellerman 	 * eventually causing a page fault which is fatal. Just default to
654c6b4c914SMichael Ellerman 	 * something vaguely sane.
655c6b4c914SMichael Ellerman 	 */
656c6b4c914SMichael Ellerman 	if (!l1d_size)
657c6b4c914SMichael Ellerman 		l1d_size = (64 * 1024);
658c6b4c914SMichael Ellerman 
659c6b4c914SMichael Ellerman 	limit = min(ppc64_bolted_size(), ppc64_rma_size);
660c6b4c914SMichael Ellerman 
661c6b4c914SMichael Ellerman 	/*
662c6b4c914SMichael Ellerman 	 * Align to L1d size, and size it at 2x L1d size, to catch possible
663c6b4c914SMichael Ellerman 	 * hardware prefetch runoff. We don't have a recipe for load patterns to
664c6b4c914SMichael Ellerman 	 * reliably avoid the prefetcher.
665c6b4c914SMichael Ellerman 	 */
666c6b4c914SMichael Ellerman 	l1d_flush_fallback_area = memblock_alloc_try_nid(l1d_size * 2,
667c6b4c914SMichael Ellerman 						l1d_size, MEMBLOCK_LOW_LIMIT,
668c6b4c914SMichael Ellerman 						limit, NUMA_NO_NODE);
669c6b4c914SMichael Ellerman 	if (!l1d_flush_fallback_area)
670c6b4c914SMichael Ellerman 		panic("%s: Failed to allocate %llu bytes align=0x%llx max_addr=%pa\n",
671c6b4c914SMichael Ellerman 		      __func__, l1d_size * 2, l1d_size, &limit);
672c6b4c914SMichael Ellerman 
673c6b4c914SMichael Ellerman 
674c6b4c914SMichael Ellerman 	for_each_possible_cpu(cpu) {
675c6b4c914SMichael Ellerman 		struct paca_struct *paca = paca_ptrs[cpu];
676c6b4c914SMichael Ellerman 		paca->rfi_flush_fallback_area = l1d_flush_fallback_area;
677c6b4c914SMichael Ellerman 		paca->l1d_flush_size = l1d_size;
678c6b4c914SMichael Ellerman 	}
679c6b4c914SMichael Ellerman }
680c6b4c914SMichael Ellerman 
681c6b4c914SMichael Ellerman void setup_rfi_flush(enum l1d_flush_type types, bool enable)
682c6b4c914SMichael Ellerman {
683c6b4c914SMichael Ellerman 	if (types & L1D_FLUSH_FALLBACK) {
684c6b4c914SMichael Ellerman 		pr_info("rfi-flush: fallback displacement flush available\n");
685c6b4c914SMichael Ellerman 		init_fallback_flush();
686c6b4c914SMichael Ellerman 	}
687c6b4c914SMichael Ellerman 
688c6b4c914SMichael Ellerman 	if (types & L1D_FLUSH_ORI)
689c6b4c914SMichael Ellerman 		pr_info("rfi-flush: ori type flush available\n");
690c6b4c914SMichael Ellerman 
691c6b4c914SMichael Ellerman 	if (types & L1D_FLUSH_MTTRIG)
692c6b4c914SMichael Ellerman 		pr_info("rfi-flush: mttrig type flush available\n");
693c6b4c914SMichael Ellerman 
694c6b4c914SMichael Ellerman 	enabled_flush_types = types;
695c6b4c914SMichael Ellerman 
696c6b4c914SMichael Ellerman 	if (!cpu_mitigations_off() && !no_rfi_flush)
697c6b4c914SMichael Ellerman 		rfi_flush_enable(enable);
698c6b4c914SMichael Ellerman }
699c6b4c914SMichael Ellerman 
700c6b4c914SMichael Ellerman void setup_entry_flush(bool enable)
701c6b4c914SMichael Ellerman {
702c6b4c914SMichael Ellerman 	if (cpu_mitigations_off())
703c6b4c914SMichael Ellerman 		return;
704c6b4c914SMichael Ellerman 
705c6b4c914SMichael Ellerman 	if (!no_entry_flush)
706c6b4c914SMichael Ellerman 		entry_flush_enable(enable);
707c6b4c914SMichael Ellerman }
708c6b4c914SMichael Ellerman 
709c6b4c914SMichael Ellerman void setup_uaccess_flush(bool enable)
710c6b4c914SMichael Ellerman {
711c6b4c914SMichael Ellerman 	if (cpu_mitigations_off())
712c6b4c914SMichael Ellerman 		return;
713c6b4c914SMichael Ellerman 
714c6b4c914SMichael Ellerman 	if (!no_uaccess_flush)
715c6b4c914SMichael Ellerman 		uaccess_flush_enable(enable);
716c6b4c914SMichael Ellerman }
717c6b4c914SMichael Ellerman 
718ee13cb24SMichael Ellerman #ifdef CONFIG_DEBUG_FS
719ee13cb24SMichael Ellerman static int count_cache_flush_set(void *data, u64 val)
720ee13cb24SMichael Ellerman {
721ee13cb24SMichael Ellerman 	bool enable;
722ee13cb24SMichael Ellerman 
723ee13cb24SMichael Ellerman 	if (val == 1)
724ee13cb24SMichael Ellerman 		enable = true;
725ee13cb24SMichael Ellerman 	else if (val == 0)
726ee13cb24SMichael Ellerman 		enable = false;
727ee13cb24SMichael Ellerman 	else
728ee13cb24SMichael Ellerman 		return -EINVAL;
729ee13cb24SMichael Ellerman 
7301026798cSNicholas Piggin 	toggle_branch_cache_flush(enable);
731ee13cb24SMichael Ellerman 
732ee13cb24SMichael Ellerman 	return 0;
733ee13cb24SMichael Ellerman }
734ee13cb24SMichael Ellerman 
735ee13cb24SMichael Ellerman static int count_cache_flush_get(void *data, u64 *val)
736ee13cb24SMichael Ellerman {
7371026798cSNicholas Piggin 	if (count_cache_flush_type == BRANCH_CACHE_FLUSH_NONE)
738ee13cb24SMichael Ellerman 		*val = 0;
739ee13cb24SMichael Ellerman 	else
740ee13cb24SMichael Ellerman 		*val = 1;
741ee13cb24SMichael Ellerman 
742ee13cb24SMichael Ellerman 	return 0;
743ee13cb24SMichael Ellerman }
744ee13cb24SMichael Ellerman 
745090d5ab9SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(fops_count_cache_flush, count_cache_flush_get,
746ee13cb24SMichael Ellerman 			 count_cache_flush_set, "%llu\n");
747ee13cb24SMichael Ellerman 
748ee13cb24SMichael Ellerman static __init int count_cache_flush_debugfs_init(void)
749ee13cb24SMichael Ellerman {
750090d5ab9SYueHaibing 	debugfs_create_file_unsafe("count_cache_flush", 0600,
751090d5ab9SYueHaibing 				   powerpc_debugfs_root, NULL,
752090d5ab9SYueHaibing 				   &fops_count_cache_flush);
753ee13cb24SMichael Ellerman 	return 0;
754ee13cb24SMichael Ellerman }
755ee13cb24SMichael Ellerman device_initcall(count_cache_flush_debugfs_init);
756c6b4c914SMichael Ellerman 
757c6b4c914SMichael Ellerman static int rfi_flush_set(void *data, u64 val)
758c6b4c914SMichael Ellerman {
759c6b4c914SMichael Ellerman 	bool enable;
760c6b4c914SMichael Ellerman 
761c6b4c914SMichael Ellerman 	if (val == 1)
762c6b4c914SMichael Ellerman 		enable = true;
763c6b4c914SMichael Ellerman 	else if (val == 0)
764c6b4c914SMichael Ellerman 		enable = false;
765c6b4c914SMichael Ellerman 	else
766c6b4c914SMichael Ellerman 		return -EINVAL;
767c6b4c914SMichael Ellerman 
768c6b4c914SMichael Ellerman 	/* Only do anything if we're changing state */
769c6b4c914SMichael Ellerman 	if (enable != rfi_flush)
770c6b4c914SMichael Ellerman 		rfi_flush_enable(enable);
771c6b4c914SMichael Ellerman 
772c6b4c914SMichael Ellerman 	return 0;
773c6b4c914SMichael Ellerman }
774c6b4c914SMichael Ellerman 
775c6b4c914SMichael Ellerman static int rfi_flush_get(void *data, u64 *val)
776c6b4c914SMichael Ellerman {
777c6b4c914SMichael Ellerman 	*val = rfi_flush ? 1 : 0;
778c6b4c914SMichael Ellerman 	return 0;
779c6b4c914SMichael Ellerman }
780c6b4c914SMichael Ellerman 
781c6b4c914SMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_rfi_flush, rfi_flush_get, rfi_flush_set, "%llu\n");
782c6b4c914SMichael Ellerman 
783c6b4c914SMichael Ellerman static int entry_flush_set(void *data, u64 val)
784c6b4c914SMichael Ellerman {
785c6b4c914SMichael Ellerman 	bool enable;
786c6b4c914SMichael Ellerman 
787c6b4c914SMichael Ellerman 	if (val == 1)
788c6b4c914SMichael Ellerman 		enable = true;
789c6b4c914SMichael Ellerman 	else if (val == 0)
790c6b4c914SMichael Ellerman 		enable = false;
791c6b4c914SMichael Ellerman 	else
792c6b4c914SMichael Ellerman 		return -EINVAL;
793c6b4c914SMichael Ellerman 
794c6b4c914SMichael Ellerman 	/* Only do anything if we're changing state */
795c6b4c914SMichael Ellerman 	if (enable != entry_flush)
796c6b4c914SMichael Ellerman 		entry_flush_enable(enable);
797c6b4c914SMichael Ellerman 
798c6b4c914SMichael Ellerman 	return 0;
799c6b4c914SMichael Ellerman }
800c6b4c914SMichael Ellerman 
801c6b4c914SMichael Ellerman static int entry_flush_get(void *data, u64 *val)
802c6b4c914SMichael Ellerman {
803c6b4c914SMichael Ellerman 	*val = entry_flush ? 1 : 0;
804c6b4c914SMichael Ellerman 	return 0;
805c6b4c914SMichael Ellerman }
806c6b4c914SMichael Ellerman 
807c6b4c914SMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_entry_flush, entry_flush_get, entry_flush_set, "%llu\n");
808c6b4c914SMichael Ellerman 
809c6b4c914SMichael Ellerman static int uaccess_flush_set(void *data, u64 val)
810c6b4c914SMichael Ellerman {
811c6b4c914SMichael Ellerman 	bool enable;
812c6b4c914SMichael Ellerman 
813c6b4c914SMichael Ellerman 	if (val == 1)
814c6b4c914SMichael Ellerman 		enable = true;
815c6b4c914SMichael Ellerman 	else if (val == 0)
816c6b4c914SMichael Ellerman 		enable = false;
817c6b4c914SMichael Ellerman 	else
818c6b4c914SMichael Ellerman 		return -EINVAL;
819c6b4c914SMichael Ellerman 
820c6b4c914SMichael Ellerman 	/* Only do anything if we're changing state */
821c6b4c914SMichael Ellerman 	if (enable != uaccess_flush)
822c6b4c914SMichael Ellerman 		uaccess_flush_enable(enable);
823c6b4c914SMichael Ellerman 
824c6b4c914SMichael Ellerman 	return 0;
825c6b4c914SMichael Ellerman }
826c6b4c914SMichael Ellerman 
827c6b4c914SMichael Ellerman static int uaccess_flush_get(void *data, u64 *val)
828c6b4c914SMichael Ellerman {
829c6b4c914SMichael Ellerman 	*val = uaccess_flush ? 1 : 0;
830c6b4c914SMichael Ellerman 	return 0;
831c6b4c914SMichael Ellerman }
832c6b4c914SMichael Ellerman 
833c6b4c914SMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_uaccess_flush, uaccess_flush_get, uaccess_flush_set, "%llu\n");
834c6b4c914SMichael Ellerman 
835c6b4c914SMichael Ellerman static __init int rfi_flush_debugfs_init(void)
836c6b4c914SMichael Ellerman {
837c6b4c914SMichael Ellerman 	debugfs_create_file("rfi_flush", 0600, powerpc_debugfs_root, NULL, &fops_rfi_flush);
838c6b4c914SMichael Ellerman 	debugfs_create_file("entry_flush", 0600, powerpc_debugfs_root, NULL, &fops_entry_flush);
839c6b4c914SMichael Ellerman 	debugfs_create_file("uaccess_flush", 0600, powerpc_debugfs_root, NULL, &fops_uaccess_flush);
840c6b4c914SMichael Ellerman 	return 0;
841c6b4c914SMichael Ellerman }
842c6b4c914SMichael Ellerman device_initcall(rfi_flush_debugfs_init);
843ee13cb24SMichael Ellerman #endif /* CONFIG_DEBUG_FS */
8446453b532SDiana Craciun #endif /* CONFIG_PPC_BOOK3S_64 */
845