xref: /openbmc/linux/arch/powerpc/kernel/dawr.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1a278e7eaSMichael Neuling // SPDX-License-Identifier: GPL-2.0+
2a278e7eaSMichael Neuling /*
3a278e7eaSMichael Neuling  * DAWR infrastructure
4a278e7eaSMichael Neuling  *
5a278e7eaSMichael Neuling  * Copyright 2019, Michael Neuling, IBM Corporation.
6a278e7eaSMichael Neuling  */
7a278e7eaSMichael Neuling 
8a278e7eaSMichael Neuling #include <linux/types.h>
9a278e7eaSMichael Neuling #include <linux/export.h>
10a278e7eaSMichael Neuling #include <linux/fs.h>
11a278e7eaSMichael Neuling #include <linux/debugfs.h>
12a278e7eaSMichael Neuling #include <asm/machdep.h>
13a278e7eaSMichael Neuling #include <asm/hvcall.h>
14*46d60bdbSChristophe Leroy #include <asm/firmware.h>
15a278e7eaSMichael Neuling 
16a278e7eaSMichael Neuling bool dawr_force_enable;
17a278e7eaSMichael Neuling EXPORT_SYMBOL_GPL(dawr_force_enable);
18a278e7eaSMichael Neuling 
set_dawr(int nr,struct arch_hw_breakpoint * brk)19a18b8346SRavi Bangoria int set_dawr(int nr, struct arch_hw_breakpoint *brk)
20a278e7eaSMichael Neuling {
21a278e7eaSMichael Neuling 	unsigned long dawr, dawrx, mrd;
22a278e7eaSMichael Neuling 
23a278e7eaSMichael Neuling 	dawr = brk->address;
24a278e7eaSMichael Neuling 
25a278e7eaSMichael Neuling 	dawrx  = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE))
26a278e7eaSMichael Neuling 		<< (63 - 58);
27a278e7eaSMichael Neuling 	dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) << (63 - 59);
28a278e7eaSMichael Neuling 	dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) >> 3;
29a278e7eaSMichael Neuling 	/*
30a278e7eaSMichael Neuling 	 * DAWR length is stored in field MDR bits 48:53.  Matches range in
311fd02f66SJulia Lawall 	 * doublewords (64 bits) biased by -1 eg. 0b000000=1DW and
32a278e7eaSMichael Neuling 	 * 0b111111=64DW.
33b57aeab8SRavi Bangoria 	 * brk->hw_len is in bytes.
34a278e7eaSMichael Neuling 	 * This aligns up to double word size, shifts and does the bias.
35a278e7eaSMichael Neuling 	 */
36b57aeab8SRavi Bangoria 	mrd = ((brk->hw_len + 7) >> 3) - 1;
37a278e7eaSMichael Neuling 	dawrx |= (mrd & 0x3f) << (63 - 53);
38a278e7eaSMichael Neuling 
39a278e7eaSMichael Neuling 	if (ppc_md.set_dawr)
4003f3e54aSRavi Bangoria 		return ppc_md.set_dawr(nr, dawr, dawrx);
41a278e7eaSMichael Neuling 
42a18b8346SRavi Bangoria 	if (nr == 0) {
4309f82b06SRavi Bangoria 		mtspr(SPRN_DAWR0, dawr);
4409f82b06SRavi Bangoria 		mtspr(SPRN_DAWRX0, dawrx);
45a18b8346SRavi Bangoria 	} else {
46a18b8346SRavi Bangoria 		mtspr(SPRN_DAWR1, dawr);
47a18b8346SRavi Bangoria 		mtspr(SPRN_DAWRX1, dawrx);
48a18b8346SRavi Bangoria 	}
49a278e7eaSMichael Neuling 
50a278e7eaSMichael Neuling 	return 0;
51a278e7eaSMichael Neuling }
52a278e7eaSMichael Neuling 
disable_dawrs_cb(void * info)5322a214e4SRavi Bangoria static void disable_dawrs_cb(void *info)
54a278e7eaSMichael Neuling {
5522a214e4SRavi Bangoria 	struct arch_hw_breakpoint null_brk = {0};
5622a214e4SRavi Bangoria 	int i;
5722a214e4SRavi Bangoria 
5822a214e4SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++)
5922a214e4SRavi Bangoria 		set_dawr(i, &null_brk);
60a278e7eaSMichael Neuling }
61a278e7eaSMichael Neuling 
dawr_write_file_bool(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)62a278e7eaSMichael Neuling static ssize_t dawr_write_file_bool(struct file *file,
63a278e7eaSMichael Neuling 				    const char __user *user_buf,
64a278e7eaSMichael Neuling 				    size_t count, loff_t *ppos)
65a278e7eaSMichael Neuling {
66b57aeab8SRavi Bangoria 	struct arch_hw_breakpoint null_brk = {0};
67a278e7eaSMichael Neuling 	size_t rc;
68a278e7eaSMichael Neuling 
69a278e7eaSMichael Neuling 	/* Send error to user if they hypervisor won't allow us to write DAWR */
70a278e7eaSMichael Neuling 	if (!dawr_force_enable &&
71a278e7eaSMichael Neuling 	    firmware_has_feature(FW_FEATURE_LPAR) &&
72a18b8346SRavi Bangoria 	    set_dawr(0, &null_brk) != H_SUCCESS)
73a278e7eaSMichael Neuling 		return -ENODEV;
74a278e7eaSMichael Neuling 
75a278e7eaSMichael Neuling 	rc = debugfs_write_file_bool(file, user_buf, count, ppos);
76a278e7eaSMichael Neuling 	if (rc)
77a278e7eaSMichael Neuling 		return rc;
78a278e7eaSMichael Neuling 
79a278e7eaSMichael Neuling 	/* If we are clearing, make sure all CPUs have the DAWR cleared */
80a278e7eaSMichael Neuling 	if (!dawr_force_enable)
8122a214e4SRavi Bangoria 		smp_call_function(disable_dawrs_cb, NULL, 0);
82a278e7eaSMichael Neuling 
83a278e7eaSMichael Neuling 	return rc;
84a278e7eaSMichael Neuling }
85a278e7eaSMichael Neuling 
86a278e7eaSMichael Neuling static const struct file_operations dawr_enable_fops = {
87a278e7eaSMichael Neuling 	.read =		debugfs_read_file_bool,
88a278e7eaSMichael Neuling 	.write =	dawr_write_file_bool,
89a278e7eaSMichael Neuling 	.open =		simple_open,
90a278e7eaSMichael Neuling 	.llseek =	default_llseek,
91a278e7eaSMichael Neuling };
92a278e7eaSMichael Neuling 
dawr_force_setup(void)93a278e7eaSMichael Neuling static int __init dawr_force_setup(void)
94a278e7eaSMichael Neuling {
95a278e7eaSMichael Neuling 	if (cpu_has_feature(CPU_FTR_DAWR)) {
96a278e7eaSMichael Neuling 		/* Don't setup sysfs file for user control on P8 */
97a278e7eaSMichael Neuling 		dawr_force_enable = true;
98a278e7eaSMichael Neuling 		return 0;
99a278e7eaSMichael Neuling 	}
100a278e7eaSMichael Neuling 
101a278e7eaSMichael Neuling 	if (PVR_VER(mfspr(SPRN_PVR)) == PVR_POWER9) {
102a278e7eaSMichael Neuling 		/* Turn DAWR off by default, but allow admin to turn it on */
103a278e7eaSMichael Neuling 		debugfs_create_file_unsafe("dawr_enable_dangerous", 0600,
104dbf77fedSAneesh Kumar K.V 					   arch_debugfs_dir,
105a278e7eaSMichael Neuling 					   &dawr_force_enable,
106a278e7eaSMichael Neuling 					   &dawr_enable_fops);
107a278e7eaSMichael Neuling 	}
108a278e7eaSMichael Neuling 	return 0;
109a278e7eaSMichael Neuling }
110a278e7eaSMichael Neuling arch_initcall(dawr_force_setup);
111