xref: /openbmc/linux/arch/arm/kernel/hw_breakpoint.c (revision 9b377e217f0bd07f972d89ed0963df92818beffd)
1f81ef4a9SWill Deacon /*
2f81ef4a9SWill Deacon  * This program is free software; you can redistribute it and/or modify
3f81ef4a9SWill Deacon  * it under the terms of the GNU General Public License version 2 as
4f81ef4a9SWill Deacon  * published by the Free Software Foundation.
5f81ef4a9SWill Deacon  *
6f81ef4a9SWill Deacon  * This program is distributed in the hope that it will be useful,
7f81ef4a9SWill Deacon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8f81ef4a9SWill Deacon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9f81ef4a9SWill Deacon  * GNU General Public License for more details.
10f81ef4a9SWill Deacon  *
11f81ef4a9SWill Deacon  * You should have received a copy of the GNU General Public License
12f81ef4a9SWill Deacon  * along with this program; if not, write to the Free Software
13f81ef4a9SWill Deacon  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14f81ef4a9SWill Deacon  *
15f81ef4a9SWill Deacon  * Copyright (C) 2009, 2010 ARM Limited
16f81ef4a9SWill Deacon  *
17f81ef4a9SWill Deacon  * Author: Will Deacon <will.deacon@arm.com>
18f81ef4a9SWill Deacon  */
19f81ef4a9SWill Deacon 
20f81ef4a9SWill Deacon /*
21f81ef4a9SWill Deacon  * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
22f81ef4a9SWill Deacon  * using the CPU's debug registers.
23f81ef4a9SWill Deacon  */
24f81ef4a9SWill Deacon #define pr_fmt(fmt) "hw-breakpoint: " fmt
25f81ef4a9SWill Deacon 
26f81ef4a9SWill Deacon #include <linux/errno.h>
277e202696SWill Deacon #include <linux/hardirq.h>
28f81ef4a9SWill Deacon #include <linux/perf_event.h>
29f81ef4a9SWill Deacon #include <linux/hw_breakpoint.h>
30f81ef4a9SWill Deacon #include <linux/smp.h>
319a6eb310SDietmar Eggemann #include <linux/cpu_pm.h>
32184901a0SMathieu Poirier #include <linux/coresight.h>
33f81ef4a9SWill Deacon 
34f81ef4a9SWill Deacon #include <asm/cacheflush.h>
35f81ef4a9SWill Deacon #include <asm/cputype.h>
36f81ef4a9SWill Deacon #include <asm/current.h>
37f81ef4a9SWill Deacon #include <asm/hw_breakpoint.h>
38f81ef4a9SWill Deacon #include <asm/traps.h>
39f81ef4a9SWill Deacon 
40f81ef4a9SWill Deacon /* Breakpoint currently in use for each BRP. */
41f81ef4a9SWill Deacon static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[ARM_MAX_BRP]);
42f81ef4a9SWill Deacon 
43f81ef4a9SWill Deacon /* Watchpoint currently in use for each WRP. */
44f81ef4a9SWill Deacon static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[ARM_MAX_WRP]);
45f81ef4a9SWill Deacon 
46f81ef4a9SWill Deacon /* Number of BRP/WRP registers on this CPU. */
47f81ef4a9SWill Deacon static int core_num_brps;
48f81ef4a9SWill Deacon static int core_num_wrps;
49f81ef4a9SWill Deacon 
50f81ef4a9SWill Deacon /* Debug architecture version. */
51f81ef4a9SWill Deacon static u8 debug_arch;
52f81ef4a9SWill Deacon 
5357ba8997SDietmar Eggemann /* Does debug architecture support OS Save and Restore? */
5457ba8997SDietmar Eggemann static bool has_ossr;
5557ba8997SDietmar Eggemann 
56f81ef4a9SWill Deacon /* Maximum supported watchpoint length. */
57f81ef4a9SWill Deacon static u8 max_watchpoint_len;
58f81ef4a9SWill Deacon 
59f81ef4a9SWill Deacon #define READ_WB_REG_CASE(OP2, M, VAL)			\
60f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):				\
619e962f76SDietmar Eggemann 		ARM_DBG_READ(c0, c ## M, OP2, VAL);	\
62f81ef4a9SWill Deacon 		break
63f81ef4a9SWill Deacon 
64f81ef4a9SWill Deacon #define WRITE_WB_REG_CASE(OP2, M, VAL)			\
65f81ef4a9SWill Deacon 	case ((OP2 << 4) + M):				\
669e962f76SDietmar Eggemann 		ARM_DBG_WRITE(c0, c ## M, OP2, VAL);	\
67f81ef4a9SWill Deacon 		break
68f81ef4a9SWill Deacon 
69f81ef4a9SWill Deacon #define GEN_READ_WB_REG_CASES(OP2, VAL)		\
70f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 0, VAL);		\
71f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 1, VAL);		\
72f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 2, VAL);		\
73f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 3, VAL);		\
74f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 4, VAL);		\
75f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 5, VAL);		\
76f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 6, VAL);		\
77f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 7, VAL);		\
78f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 8, VAL);		\
79f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 9, VAL);		\
80f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 10, VAL);		\
81f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 11, VAL);		\
82f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 12, VAL);		\
83f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 13, VAL);		\
84f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 14, VAL);		\
85f81ef4a9SWill Deacon 	READ_WB_REG_CASE(OP2, 15, VAL)
86f81ef4a9SWill Deacon 
87f81ef4a9SWill Deacon #define GEN_WRITE_WB_REG_CASES(OP2, VAL)	\
88f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 0, VAL);		\
89f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 1, VAL);		\
90f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 2, VAL);		\
91f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 3, VAL);		\
92f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 4, VAL);		\
93f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 5, VAL);		\
94f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 6, VAL);		\
95f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 7, VAL);		\
96f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 8, VAL);		\
97f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 9, VAL);		\
98f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 10, VAL);	\
99f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 11, VAL);	\
100f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 12, VAL);	\
101f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 13, VAL);	\
102f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 14, VAL);	\
103f81ef4a9SWill Deacon 	WRITE_WB_REG_CASE(OP2, 15, VAL)
104f81ef4a9SWill Deacon 
105f81ef4a9SWill Deacon static u32 read_wb_reg(int n)
106f81ef4a9SWill Deacon {
107f81ef4a9SWill Deacon 	u32 val = 0;
108f81ef4a9SWill Deacon 
109f81ef4a9SWill Deacon 	switch (n) {
110f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BVR, val);
111f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_BCR, val);
112f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WVR, val);
113f81ef4a9SWill Deacon 	GEN_READ_WB_REG_CASES(ARM_OP2_WCR, val);
114f81ef4a9SWill Deacon 	default:
1158b521cb2SJoe Perches 		pr_warn("attempt to read from unknown breakpoint register %d\n",
1168b521cb2SJoe Perches 			n);
117f81ef4a9SWill Deacon 	}
118f81ef4a9SWill Deacon 
119f81ef4a9SWill Deacon 	return val;
120f81ef4a9SWill Deacon }
121f81ef4a9SWill Deacon 
122f81ef4a9SWill Deacon static void write_wb_reg(int n, u32 val)
123f81ef4a9SWill Deacon {
124f81ef4a9SWill Deacon 	switch (n) {
125f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BVR, val);
126f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_BCR, val);
127f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WVR, val);
128f81ef4a9SWill Deacon 	GEN_WRITE_WB_REG_CASES(ARM_OP2_WCR, val);
129f81ef4a9SWill Deacon 	default:
1308b521cb2SJoe Perches 		pr_warn("attempt to write to unknown breakpoint register %d\n",
1318b521cb2SJoe Perches 			n);
132f81ef4a9SWill Deacon 	}
133f81ef4a9SWill Deacon 	isb();
134f81ef4a9SWill Deacon }
135f81ef4a9SWill Deacon 
1360017ff42SWill Deacon /* Determine debug architecture. */
1370017ff42SWill Deacon static u8 get_debug_arch(void)
1380017ff42SWill Deacon {
1390017ff42SWill Deacon 	u32 didr;
1400017ff42SWill Deacon 
1410017ff42SWill Deacon 	/* Do we implement the extended CPUID interface? */
142d1244336SWill Deacon 	if (((read_cpuid_id() >> 16) & 0xf) != 0xf) {
1435ad29ea2SWill Deacon 		pr_warn_once("CPUID feature registers not supported. "
144d1244336SWill Deacon 			     "Assuming v6 debug is present.\n");
1450017ff42SWill Deacon 		return ARM_DEBUG_ARCH_V6;
146d1244336SWill Deacon 	}
1470017ff42SWill Deacon 
1489e962f76SDietmar Eggemann 	ARM_DBG_READ(c0, c0, 0, didr);
1490017ff42SWill Deacon 	return (didr >> 16) & 0xf;
1500017ff42SWill Deacon }
1510017ff42SWill Deacon 
1520017ff42SWill Deacon u8 arch_get_debug_arch(void)
1530017ff42SWill Deacon {
1540017ff42SWill Deacon 	return debug_arch;
1550017ff42SWill Deacon }
1560017ff42SWill Deacon 
15766e1cfe6SWill Deacon static int debug_arch_supported(void)
15866e1cfe6SWill Deacon {
15966e1cfe6SWill Deacon 	u8 arch = get_debug_arch();
160b5d5b8f9SWill Deacon 
161b5d5b8f9SWill Deacon 	/* We don't support the memory-mapped interface. */
162b5d5b8f9SWill Deacon 	return (arch >= ARM_DEBUG_ARCH_V6 && arch <= ARM_DEBUG_ARCH_V7_ECP14) ||
163b5d5b8f9SWill Deacon 		arch >= ARM_DEBUG_ARCH_V7_1;
16466e1cfe6SWill Deacon }
16566e1cfe6SWill Deacon 
166bf880114SWill Deacon /* Can we determine the watchpoint access type from the fsr? */
167bf880114SWill Deacon static int debug_exception_updates_fsr(void)
168bf880114SWill Deacon {
1695b61d4a5SChristopher Covington 	return get_debug_arch() >= ARM_DEBUG_ARCH_V8;
170bf880114SWill Deacon }
171bf880114SWill Deacon 
172c512de95SWill Deacon /* Determine number of WRP registers available. */
173c512de95SWill Deacon static int get_num_wrp_resources(void)
174c512de95SWill Deacon {
175c512de95SWill Deacon 	u32 didr;
1769e962f76SDietmar Eggemann 	ARM_DBG_READ(c0, c0, 0, didr);
177c512de95SWill Deacon 	return ((didr >> 28) & 0xf) + 1;
178c512de95SWill Deacon }
179c512de95SWill Deacon 
180c512de95SWill Deacon /* Determine number of BRP registers available. */
1810017ff42SWill Deacon static int get_num_brp_resources(void)
1820017ff42SWill Deacon {
1830017ff42SWill Deacon 	u32 didr;
1849e962f76SDietmar Eggemann 	ARM_DBG_READ(c0, c0, 0, didr);
1850017ff42SWill Deacon 	return ((didr >> 24) & 0xf) + 1;
1860017ff42SWill Deacon }
1870017ff42SWill Deacon 
1880017ff42SWill Deacon /* Does this core support mismatch breakpoints? */
1890017ff42SWill Deacon static int core_has_mismatch_brps(void)
1900017ff42SWill Deacon {
1910017ff42SWill Deacon 	return (get_debug_arch() >= ARM_DEBUG_ARCH_V7_ECP14 &&
1920017ff42SWill Deacon 		get_num_brp_resources() > 1);
1930017ff42SWill Deacon }
1940017ff42SWill Deacon 
1950017ff42SWill Deacon /* Determine number of usable WRPs available. */
1960017ff42SWill Deacon static int get_num_wrps(void)
1970017ff42SWill Deacon {
1980017ff42SWill Deacon 	/*
199c512de95SWill Deacon 	 * On debug architectures prior to 7.1, when a watchpoint fires, the
200c512de95SWill Deacon 	 * only way to work out which watchpoint it was is by disassembling
201c512de95SWill Deacon 	 * the faulting instruction and working out the address of the memory
202c512de95SWill Deacon 	 * access.
2030017ff42SWill Deacon 	 *
2040017ff42SWill Deacon 	 * Furthermore, we can only do this if the watchpoint was precise
2050017ff42SWill Deacon 	 * since imprecise watchpoints prevent us from calculating register
2060017ff42SWill Deacon 	 * based addresses.
2070017ff42SWill Deacon 	 *
2080017ff42SWill Deacon 	 * Providing we have more than 1 breakpoint register, we only report
2090017ff42SWill Deacon 	 * a single watchpoint register for the time being. This way, we always
2100017ff42SWill Deacon 	 * know which watchpoint fired. In the future we can either add a
2110017ff42SWill Deacon 	 * disassembler and address generation emulator, or we can insert a
2120017ff42SWill Deacon 	 * check to see if the DFAR is set on watchpoint exception entry
2130017ff42SWill Deacon 	 * [the ARM ARM states that the DFAR is UNKNOWN, but experience shows
2140017ff42SWill Deacon 	 * that it is set on some implementations].
2150017ff42SWill Deacon 	 */
216c512de95SWill Deacon 	if (get_debug_arch() < ARM_DEBUG_ARCH_V7_1)
217c512de95SWill Deacon 		return 1;
2180017ff42SWill Deacon 
219c512de95SWill Deacon 	return get_num_wrp_resources();
2200017ff42SWill Deacon }
2210017ff42SWill Deacon 
2220017ff42SWill Deacon /* Determine number of usable BRPs available. */
2230017ff42SWill Deacon static int get_num_brps(void)
2240017ff42SWill Deacon {
2250017ff42SWill Deacon 	int brps = get_num_brp_resources();
226c512de95SWill Deacon 	return core_has_mismatch_brps() ? brps - 1 : brps;
2270017ff42SWill Deacon }
2280017ff42SWill Deacon 
229f81ef4a9SWill Deacon /*
230f81ef4a9SWill Deacon  * In order to access the breakpoint/watchpoint control registers,
231f81ef4a9SWill Deacon  * we must be running in debug monitor mode. Unfortunately, we can
232f81ef4a9SWill Deacon  * be put into halting debug mode at any time by an external debugger
233f81ef4a9SWill Deacon  * but there is nothing we can do to prevent that.
234f81ef4a9SWill Deacon  */
2350daa034eSWill Deacon static int monitor_mode_enabled(void)
2360daa034eSWill Deacon {
2370daa034eSWill Deacon 	u32 dscr;
2389e962f76SDietmar Eggemann 	ARM_DBG_READ(c0, c1, 0, dscr);
2390daa034eSWill Deacon 	return !!(dscr & ARM_DSCR_MDBGEN);
2400daa034eSWill Deacon }
2410daa034eSWill Deacon 
242f81ef4a9SWill Deacon static int enable_monitor_mode(void)
243f81ef4a9SWill Deacon {
244f81ef4a9SWill Deacon 	u32 dscr;
2459e962f76SDietmar Eggemann 	ARM_DBG_READ(c0, c1, 0, dscr);
246f81ef4a9SWill Deacon 
2478fbf397cSWill Deacon 	/* If monitor mode is already enabled, just return. */
2488fbf397cSWill Deacon 	if (dscr & ARM_DSCR_MDBGEN)
2498fbf397cSWill Deacon 		goto out;
2508fbf397cSWill Deacon 
251f81ef4a9SWill Deacon 	/* Write to the corresponding DSCR. */
2528fbf397cSWill Deacon 	switch (get_debug_arch()) {
253f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6:
254f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V6_1:
2559e962f76SDietmar Eggemann 		ARM_DBG_WRITE(c0, c1, 0, (dscr | ARM_DSCR_MDBGEN));
256f81ef4a9SWill Deacon 		break;
257f81ef4a9SWill Deacon 	case ARM_DEBUG_ARCH_V7_ECP14:
258b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_1:
2595b61d4a5SChristopher Covington 	case ARM_DEBUG_ARCH_V8:
2609e962f76SDietmar Eggemann 		ARM_DBG_WRITE(c0, c2, 2, (dscr | ARM_DSCR_MDBGEN));
261b59a540cSWill Deacon 		isb();
262f81ef4a9SWill Deacon 		break;
263f81ef4a9SWill Deacon 	default:
264614bea50SWill Deacon 		return -ENODEV;
265f81ef4a9SWill Deacon 	}
266f81ef4a9SWill Deacon 
267f81ef4a9SWill Deacon 	/* Check that the write made it through. */
2689e962f76SDietmar Eggemann 	ARM_DBG_READ(c0, c1, 0, dscr);
269f435ab79SWill Deacon 	if (!(dscr & ARM_DSCR_MDBGEN)) {
270f435ab79SWill Deacon 		pr_warn_once("Failed to enable monitor mode on CPU %d.\n",
271f435ab79SWill Deacon 				smp_processor_id());
272614bea50SWill Deacon 		return -EPERM;
273f435ab79SWill Deacon 	}
274f81ef4a9SWill Deacon 
275f81ef4a9SWill Deacon out:
276614bea50SWill Deacon 	return 0;
277f81ef4a9SWill Deacon }
278f81ef4a9SWill Deacon 
2798fbf397cSWill Deacon int hw_breakpoint_slots(int type)
2808fbf397cSWill Deacon {
28166e1cfe6SWill Deacon 	if (!debug_arch_supported())
28266e1cfe6SWill Deacon 		return 0;
28366e1cfe6SWill Deacon 
2848fbf397cSWill Deacon 	/*
2858fbf397cSWill Deacon 	 * We can be called early, so don't rely on
2868fbf397cSWill Deacon 	 * our static variables being initialised.
2878fbf397cSWill Deacon 	 */
2888fbf397cSWill Deacon 	switch (type) {
2898fbf397cSWill Deacon 	case TYPE_INST:
2908fbf397cSWill Deacon 		return get_num_brps();
2918fbf397cSWill Deacon 	case TYPE_DATA:
2928fbf397cSWill Deacon 		return get_num_wrps();
2938fbf397cSWill Deacon 	default:
2948b521cb2SJoe Perches 		pr_warn("unknown slot type: %d\n", type);
2958fbf397cSWill Deacon 		return 0;
2968fbf397cSWill Deacon 	}
2978fbf397cSWill Deacon }
2988fbf397cSWill Deacon 
299f81ef4a9SWill Deacon /*
300f81ef4a9SWill Deacon  * Check if 8-bit byte-address select is available.
301f81ef4a9SWill Deacon  * This clobbers WRP 0.
302f81ef4a9SWill Deacon  */
303f81ef4a9SWill Deacon static u8 get_max_wp_len(void)
304f81ef4a9SWill Deacon {
305f81ef4a9SWill Deacon 	u32 ctrl_reg;
306f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
307f81ef4a9SWill Deacon 	u8 size = 4;
308f81ef4a9SWill Deacon 
309f81ef4a9SWill Deacon 	if (debug_arch < ARM_DEBUG_ARCH_V7_ECP14)
310f81ef4a9SWill Deacon 		goto out;
311f81ef4a9SWill Deacon 
312f81ef4a9SWill Deacon 	memset(&ctrl, 0, sizeof(ctrl));
313f81ef4a9SWill Deacon 	ctrl.len = ARM_BREAKPOINT_LEN_8;
314f81ef4a9SWill Deacon 	ctrl_reg = encode_ctrl_reg(ctrl);
315f81ef4a9SWill Deacon 
316f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WVR, 0);
317f81ef4a9SWill Deacon 	write_wb_reg(ARM_BASE_WCR, ctrl_reg);
318f81ef4a9SWill Deacon 	if ((read_wb_reg(ARM_BASE_WCR) & ctrl_reg) == ctrl_reg)
319f81ef4a9SWill Deacon 		size = 8;
320f81ef4a9SWill Deacon 
321f81ef4a9SWill Deacon out:
322f81ef4a9SWill Deacon 	return size;
323f81ef4a9SWill Deacon }
324f81ef4a9SWill Deacon 
325f81ef4a9SWill Deacon u8 arch_get_max_wp_len(void)
326f81ef4a9SWill Deacon {
327f81ef4a9SWill Deacon 	return max_watchpoint_len;
328f81ef4a9SWill Deacon }
329f81ef4a9SWill Deacon 
330f81ef4a9SWill Deacon /*
331f81ef4a9SWill Deacon  * Install a perf counter breakpoint.
332f81ef4a9SWill Deacon  */
333f81ef4a9SWill Deacon int arch_install_hw_breakpoint(struct perf_event *bp)
334f81ef4a9SWill Deacon {
335f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
336f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
3370daa034eSWill Deacon 	int i, max_slots, ctrl_base, val_base;
33893a04a34SWill Deacon 	u32 addr, ctrl;
339f81ef4a9SWill Deacon 
34093a04a34SWill Deacon 	addr = info->address;
34193a04a34SWill Deacon 	ctrl = encode_ctrl_reg(info->ctrl) | 0x1;
34293a04a34SWill Deacon 
343f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
344f81ef4a9SWill Deacon 		/* Breakpoint */
345f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_BCR;
346f81ef4a9SWill Deacon 		val_base = ARM_BASE_BVR;
3471436c1aaSChristoph Lameter 		slots = this_cpu_ptr(bp_on_reg);
3480017ff42SWill Deacon 		max_slots = core_num_brps;
349f81ef4a9SWill Deacon 	} else {
350f81ef4a9SWill Deacon 		/* Watchpoint */
351f81ef4a9SWill Deacon 		ctrl_base = ARM_BASE_WCR;
352f81ef4a9SWill Deacon 		val_base = ARM_BASE_WVR;
3531436c1aaSChristoph Lameter 		slots = this_cpu_ptr(wp_on_reg);
354f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
355f81ef4a9SWill Deacon 	}
356f81ef4a9SWill Deacon 
357f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
358f81ef4a9SWill Deacon 		slot = &slots[i];
359f81ef4a9SWill Deacon 
360f81ef4a9SWill Deacon 		if (!*slot) {
361f81ef4a9SWill Deacon 			*slot = bp;
362f81ef4a9SWill Deacon 			break;
363f81ef4a9SWill Deacon 		}
364f81ef4a9SWill Deacon 	}
365f81ef4a9SWill Deacon 
366f435ab79SWill Deacon 	if (i == max_slots) {
3678b521cb2SJoe Perches 		pr_warn("Can't find any breakpoint slot\n");
3680daa034eSWill Deacon 		return -EBUSY;
369f435ab79SWill Deacon 	}
370f81ef4a9SWill Deacon 
3716f26aa05SWill Deacon 	/* Override the breakpoint data with the step data. */
3726f26aa05SWill Deacon 	if (info->step_ctrl.enabled) {
3736f26aa05SWill Deacon 		addr = info->trigger & ~0x3;
3746f26aa05SWill Deacon 		ctrl = encode_ctrl_reg(info->step_ctrl);
3756f26aa05SWill Deacon 		if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE) {
3766f26aa05SWill Deacon 			i = 0;
3776f26aa05SWill Deacon 			ctrl_base = ARM_BASE_BCR + core_num_brps;
3786f26aa05SWill Deacon 			val_base = ARM_BASE_BVR + core_num_brps;
3796f26aa05SWill Deacon 		}
3806f26aa05SWill Deacon 	}
3816f26aa05SWill Deacon 
382f81ef4a9SWill Deacon 	/* Setup the address register. */
38393a04a34SWill Deacon 	write_wb_reg(val_base + i, addr);
384f81ef4a9SWill Deacon 
385f81ef4a9SWill Deacon 	/* Setup the control register. */
38693a04a34SWill Deacon 	write_wb_reg(ctrl_base + i, ctrl);
3870daa034eSWill Deacon 	return 0;
388f81ef4a9SWill Deacon }
389f81ef4a9SWill Deacon 
390f81ef4a9SWill Deacon void arch_uninstall_hw_breakpoint(struct perf_event *bp)
391f81ef4a9SWill Deacon {
392f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
393f81ef4a9SWill Deacon 	struct perf_event **slot, **slots;
394f81ef4a9SWill Deacon 	int i, max_slots, base;
395f81ef4a9SWill Deacon 
396f81ef4a9SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE) {
397f81ef4a9SWill Deacon 		/* Breakpoint */
398f81ef4a9SWill Deacon 		base = ARM_BASE_BCR;
3991436c1aaSChristoph Lameter 		slots = this_cpu_ptr(bp_on_reg);
4000017ff42SWill Deacon 		max_slots = core_num_brps;
401f81ef4a9SWill Deacon 	} else {
402f81ef4a9SWill Deacon 		/* Watchpoint */
403f81ef4a9SWill Deacon 		base = ARM_BASE_WCR;
4041436c1aaSChristoph Lameter 		slots = this_cpu_ptr(wp_on_reg);
405f81ef4a9SWill Deacon 		max_slots = core_num_wrps;
406f81ef4a9SWill Deacon 	}
407f81ef4a9SWill Deacon 
408f81ef4a9SWill Deacon 	/* Remove the breakpoint. */
409f81ef4a9SWill Deacon 	for (i = 0; i < max_slots; ++i) {
410f81ef4a9SWill Deacon 		slot = &slots[i];
411f81ef4a9SWill Deacon 
412f81ef4a9SWill Deacon 		if (*slot == bp) {
413f81ef4a9SWill Deacon 			*slot = NULL;
414f81ef4a9SWill Deacon 			break;
415f81ef4a9SWill Deacon 		}
416f81ef4a9SWill Deacon 	}
417f81ef4a9SWill Deacon 
418f435ab79SWill Deacon 	if (i == max_slots) {
4198b521cb2SJoe Perches 		pr_warn("Can't find any breakpoint slot\n");
420f81ef4a9SWill Deacon 		return;
421f435ab79SWill Deacon 	}
422f81ef4a9SWill Deacon 
4236f26aa05SWill Deacon 	/* Ensure that we disable the mismatch breakpoint. */
4246f26aa05SWill Deacon 	if (info->ctrl.type != ARM_BREAKPOINT_EXECUTE &&
4256f26aa05SWill Deacon 	    info->step_ctrl.enabled) {
4266f26aa05SWill Deacon 		i = 0;
4276f26aa05SWill Deacon 		base = ARM_BASE_BCR + core_num_brps;
4286f26aa05SWill Deacon 	}
4296f26aa05SWill Deacon 
430f81ef4a9SWill Deacon 	/* Reset the control register. */
431f81ef4a9SWill Deacon 	write_wb_reg(base + i, 0);
432f81ef4a9SWill Deacon }
433f81ef4a9SWill Deacon 
434f81ef4a9SWill Deacon static int get_hbp_len(u8 hbp_len)
435f81ef4a9SWill Deacon {
436f81ef4a9SWill Deacon 	unsigned int len_in_bytes = 0;
437f81ef4a9SWill Deacon 
438f81ef4a9SWill Deacon 	switch (hbp_len) {
439f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
440f81ef4a9SWill Deacon 		len_in_bytes = 1;
441f81ef4a9SWill Deacon 		break;
442f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
443f81ef4a9SWill Deacon 		len_in_bytes = 2;
444f81ef4a9SWill Deacon 		break;
445f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
446f81ef4a9SWill Deacon 		len_in_bytes = 4;
447f81ef4a9SWill Deacon 		break;
448f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
449f81ef4a9SWill Deacon 		len_in_bytes = 8;
450f81ef4a9SWill Deacon 		break;
451f81ef4a9SWill Deacon 	}
452f81ef4a9SWill Deacon 
453f81ef4a9SWill Deacon 	return len_in_bytes;
454f81ef4a9SWill Deacon }
455f81ef4a9SWill Deacon 
456f81ef4a9SWill Deacon /*
457f81ef4a9SWill Deacon  * Check whether bp virtual address is in kernel space.
458f81ef4a9SWill Deacon  */
459f81ef4a9SWill Deacon int arch_check_bp_in_kernelspace(struct perf_event *bp)
460f81ef4a9SWill Deacon {
461f81ef4a9SWill Deacon 	unsigned int len;
462f81ef4a9SWill Deacon 	unsigned long va;
463f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
464f81ef4a9SWill Deacon 
465f81ef4a9SWill Deacon 	va = info->address;
466f81ef4a9SWill Deacon 	len = get_hbp_len(info->ctrl.len);
467f81ef4a9SWill Deacon 
468f81ef4a9SWill Deacon 	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
469f81ef4a9SWill Deacon }
470f81ef4a9SWill Deacon 
471f81ef4a9SWill Deacon /*
472f81ef4a9SWill Deacon  * Extract generic type and length encodings from an arch_hw_breakpoint_ctrl.
473f81ef4a9SWill Deacon  * Hopefully this will disappear when ptrace can bypass the conversion
474f81ef4a9SWill Deacon  * to generic breakpoint descriptions.
475f81ef4a9SWill Deacon  */
476f81ef4a9SWill Deacon int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
477f81ef4a9SWill Deacon 			   int *gen_len, int *gen_type)
478f81ef4a9SWill Deacon {
479f81ef4a9SWill Deacon 	/* Type */
480f81ef4a9SWill Deacon 	switch (ctrl.type) {
481f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_EXECUTE:
482f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_X;
483f81ef4a9SWill Deacon 		break;
484f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD:
485f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_R;
486f81ef4a9SWill Deacon 		break;
487f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_STORE:
488f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_W;
489f81ef4a9SWill Deacon 		break;
490f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE:
491f81ef4a9SWill Deacon 		*gen_type = HW_BREAKPOINT_RW;
492f81ef4a9SWill Deacon 		break;
493f81ef4a9SWill Deacon 	default:
494f81ef4a9SWill Deacon 		return -EINVAL;
495f81ef4a9SWill Deacon 	}
496f81ef4a9SWill Deacon 
497f81ef4a9SWill Deacon 	/* Len */
498f81ef4a9SWill Deacon 	switch (ctrl.len) {
499f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_1:
500f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_1;
501f81ef4a9SWill Deacon 		break;
502f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_2:
503f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_2;
504f81ef4a9SWill Deacon 		break;
505f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_4:
506f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_4;
507f81ef4a9SWill Deacon 		break;
508f81ef4a9SWill Deacon 	case ARM_BREAKPOINT_LEN_8:
509f81ef4a9SWill Deacon 		*gen_len = HW_BREAKPOINT_LEN_8;
510f81ef4a9SWill Deacon 		break;
511f81ef4a9SWill Deacon 	default:
512f81ef4a9SWill Deacon 		return -EINVAL;
513f81ef4a9SWill Deacon 	}
514f81ef4a9SWill Deacon 
515f81ef4a9SWill Deacon 	return 0;
516f81ef4a9SWill Deacon }
517f81ef4a9SWill Deacon 
518f81ef4a9SWill Deacon /*
519f81ef4a9SWill Deacon  * Construct an arch_hw_breakpoint from a perf_event.
520f81ef4a9SWill Deacon  */
521f81ef4a9SWill Deacon static int arch_build_bp_info(struct perf_event *bp)
522f81ef4a9SWill Deacon {
523f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
524f81ef4a9SWill Deacon 
525f81ef4a9SWill Deacon 	/* Type */
526f81ef4a9SWill Deacon 	switch (bp->attr.bp_type) {
527f81ef4a9SWill Deacon 	case HW_BREAKPOINT_X:
528f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_EXECUTE;
529f81ef4a9SWill Deacon 		break;
530f81ef4a9SWill Deacon 	case HW_BREAKPOINT_R:
531f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD;
532f81ef4a9SWill Deacon 		break;
533f81ef4a9SWill Deacon 	case HW_BREAKPOINT_W:
534f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_STORE;
535f81ef4a9SWill Deacon 		break;
536f81ef4a9SWill Deacon 	case HW_BREAKPOINT_RW:
537f81ef4a9SWill Deacon 		info->ctrl.type = ARM_BREAKPOINT_LOAD | ARM_BREAKPOINT_STORE;
538f81ef4a9SWill Deacon 		break;
539f81ef4a9SWill Deacon 	default:
540f81ef4a9SWill Deacon 		return -EINVAL;
541f81ef4a9SWill Deacon 	}
542f81ef4a9SWill Deacon 
543f81ef4a9SWill Deacon 	/* Len */
544f81ef4a9SWill Deacon 	switch (bp->attr.bp_len) {
545f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_1:
546f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_1;
547f81ef4a9SWill Deacon 		break;
548f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_2:
549f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_2;
550f81ef4a9SWill Deacon 		break;
551f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_4:
552f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_4;
553f81ef4a9SWill Deacon 		break;
554f81ef4a9SWill Deacon 	case HW_BREAKPOINT_LEN_8:
555f81ef4a9SWill Deacon 		info->ctrl.len = ARM_BREAKPOINT_LEN_8;
556f81ef4a9SWill Deacon 		if ((info->ctrl.type != ARM_BREAKPOINT_EXECUTE)
557f81ef4a9SWill Deacon 			&& max_watchpoint_len >= 8)
558f81ef4a9SWill Deacon 			break;
559f81ef4a9SWill Deacon 	default:
560f81ef4a9SWill Deacon 		return -EINVAL;
561f81ef4a9SWill Deacon 	}
562f81ef4a9SWill Deacon 
5636ee33c27SWill Deacon 	/*
5646ee33c27SWill Deacon 	 * Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
5656ee33c27SWill Deacon 	 * Watchpoints can be of length 1, 2, 4 or 8 bytes if supported
5666ee33c27SWill Deacon 	 * by the hardware and must be aligned to the appropriate number of
5676ee33c27SWill Deacon 	 * bytes.
5686ee33c27SWill Deacon 	 */
5696ee33c27SWill Deacon 	if (info->ctrl.type == ARM_BREAKPOINT_EXECUTE &&
5706ee33c27SWill Deacon 	    info->ctrl.len != ARM_BREAKPOINT_LEN_2 &&
5716ee33c27SWill Deacon 	    info->ctrl.len != ARM_BREAKPOINT_LEN_4)
5726ee33c27SWill Deacon 		return -EINVAL;
5736ee33c27SWill Deacon 
574f81ef4a9SWill Deacon 	/* Address */
575f81ef4a9SWill Deacon 	info->address = bp->attr.bp_addr;
576f81ef4a9SWill Deacon 
577f81ef4a9SWill Deacon 	/* Privilege */
578f81ef4a9SWill Deacon 	info->ctrl.privilege = ARM_BREAKPOINT_USER;
57993a04a34SWill Deacon 	if (arch_check_bp_in_kernelspace(bp))
580f81ef4a9SWill Deacon 		info->ctrl.privilege |= ARM_BREAKPOINT_PRIV;
581f81ef4a9SWill Deacon 
582f81ef4a9SWill Deacon 	/* Enabled? */
583f81ef4a9SWill Deacon 	info->ctrl.enabled = !bp->attr.disabled;
584f81ef4a9SWill Deacon 
585f81ef4a9SWill Deacon 	/* Mismatch */
586f81ef4a9SWill Deacon 	info->ctrl.mismatch = 0;
587f81ef4a9SWill Deacon 
588f81ef4a9SWill Deacon 	return 0;
589f81ef4a9SWill Deacon }
590f81ef4a9SWill Deacon 
591f81ef4a9SWill Deacon /*
592f81ef4a9SWill Deacon  * Validate the arch-specific HW Breakpoint register settings.
593f81ef4a9SWill Deacon  */
594f81ef4a9SWill Deacon int arch_validate_hwbkpt_settings(struct perf_event *bp)
595f81ef4a9SWill Deacon {
596f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
597f81ef4a9SWill Deacon 	int ret = 0;
5986ee33c27SWill Deacon 	u32 offset, alignment_mask = 0x3;
599f81ef4a9SWill Deacon 
6000daa034eSWill Deacon 	/* Ensure that we are in monitor debug mode. */
6010daa034eSWill Deacon 	if (!monitor_mode_enabled())
6020daa034eSWill Deacon 		return -ENODEV;
6030daa034eSWill Deacon 
604f81ef4a9SWill Deacon 	/* Build the arch_hw_breakpoint. */
605f81ef4a9SWill Deacon 	ret = arch_build_bp_info(bp);
606f81ef4a9SWill Deacon 	if (ret)
607f81ef4a9SWill Deacon 		goto out;
608f81ef4a9SWill Deacon 
609f81ef4a9SWill Deacon 	/* Check address alignment. */
610f81ef4a9SWill Deacon 	if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
611f81ef4a9SWill Deacon 		alignment_mask = 0x7;
6126ee33c27SWill Deacon 	offset = info->address & alignment_mask;
6136ee33c27SWill Deacon 	switch (offset) {
6146ee33c27SWill Deacon 	case 0:
6156ee33c27SWill Deacon 		/* Aligned */
6166ee33c27SWill Deacon 		break;
6176ee33c27SWill Deacon 	case 1:
6186ee33c27SWill Deacon 	case 2:
6196ee33c27SWill Deacon 		/* Allow halfword watchpoints and breakpoints. */
6206ee33c27SWill Deacon 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_2)
6216ee33c27SWill Deacon 			break;
622d968d2b8SWill Deacon 	case 3:
623d968d2b8SWill Deacon 		/* Allow single byte watchpoint. */
624d968d2b8SWill Deacon 		if (info->ctrl.len == ARM_BREAKPOINT_LEN_1)
625d968d2b8SWill Deacon 			break;
6266ee33c27SWill Deacon 	default:
6276ee33c27SWill Deacon 		ret = -EINVAL;
628f81ef4a9SWill Deacon 		goto out;
629f81ef4a9SWill Deacon 	}
630f81ef4a9SWill Deacon 
6316ee33c27SWill Deacon 	info->address &= ~alignment_mask;
632f81ef4a9SWill Deacon 	info->ctrl.len <<= offset;
633f81ef4a9SWill Deacon 
6341879445dSWang Nan 	if (is_default_overflow_handler(bp)) {
635f81ef4a9SWill Deacon 		/*
636bf880114SWill Deacon 		 * Mismatch breakpoints are required for single-stepping
637bf880114SWill Deacon 		 * breakpoints.
638f81ef4a9SWill Deacon 		 */
639bf880114SWill Deacon 		if (!core_has_mismatch_brps())
640bf880114SWill Deacon 			return -EINVAL;
641bf880114SWill Deacon 
642bf880114SWill Deacon 		/* We don't allow mismatch breakpoints in kernel space. */
643bf880114SWill Deacon 		if (arch_check_bp_in_kernelspace(bp))
644bf880114SWill Deacon 			return -EPERM;
645bf880114SWill Deacon 
646bf880114SWill Deacon 		/*
647bf880114SWill Deacon 		 * Per-cpu breakpoints are not supported by our stepping
648bf880114SWill Deacon 		 * mechanism.
649bf880114SWill Deacon 		 */
65050f16a8bSPeter Zijlstra 		if (!bp->hw.target)
651bf880114SWill Deacon 			return -EINVAL;
652bf880114SWill Deacon 
653bf880114SWill Deacon 		/*
654bf880114SWill Deacon 		 * We only support specific access types if the fsr
655bf880114SWill Deacon 		 * reports them.
656bf880114SWill Deacon 		 */
657bf880114SWill Deacon 		if (!debug_exception_updates_fsr() &&
658bf880114SWill Deacon 		    (info->ctrl.type == ARM_BREAKPOINT_LOAD ||
659bf880114SWill Deacon 		     info->ctrl.type == ARM_BREAKPOINT_STORE))
660bf880114SWill Deacon 			return -EINVAL;
661f81ef4a9SWill Deacon 	}
662bf880114SWill Deacon 
663f81ef4a9SWill Deacon out:
664f81ef4a9SWill Deacon 	return ret;
665f81ef4a9SWill Deacon }
666f81ef4a9SWill Deacon 
6679ebb3cbcSWill Deacon /*
6689ebb3cbcSWill Deacon  * Enable/disable single-stepping over the breakpoint bp at address addr.
6699ebb3cbcSWill Deacon  */
6709ebb3cbcSWill Deacon static void enable_single_step(struct perf_event *bp, u32 addr)
671f81ef4a9SWill Deacon {
6729ebb3cbcSWill Deacon 	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
673f81ef4a9SWill Deacon 
6749ebb3cbcSWill Deacon 	arch_uninstall_hw_breakpoint(bp);
6759ebb3cbcSWill Deacon 	info->step_ctrl.mismatch  = 1;
6769ebb3cbcSWill Deacon 	info->step_ctrl.len	  = ARM_BREAKPOINT_LEN_4;
6779ebb3cbcSWill Deacon 	info->step_ctrl.type	  = ARM_BREAKPOINT_EXECUTE;
6789ebb3cbcSWill Deacon 	info->step_ctrl.privilege = info->ctrl.privilege;
6799ebb3cbcSWill Deacon 	info->step_ctrl.enabled	  = 1;
6809ebb3cbcSWill Deacon 	info->trigger		  = addr;
6819ebb3cbcSWill Deacon 	arch_install_hw_breakpoint(bp);
682f81ef4a9SWill Deacon }
6839ebb3cbcSWill Deacon 
6849ebb3cbcSWill Deacon static void disable_single_step(struct perf_event *bp)
6859ebb3cbcSWill Deacon {
6869ebb3cbcSWill Deacon 	arch_uninstall_hw_breakpoint(bp);
6879ebb3cbcSWill Deacon 	counter_arch_bp(bp)->step_ctrl.enabled = 0;
6889ebb3cbcSWill Deacon 	arch_install_hw_breakpoint(bp);
689f81ef4a9SWill Deacon }
690f81ef4a9SWill Deacon 
6916f26aa05SWill Deacon static void watchpoint_handler(unsigned long addr, unsigned int fsr,
6926f26aa05SWill Deacon 			       struct pt_regs *regs)
693f81ef4a9SWill Deacon {
6946f26aa05SWill Deacon 	int i, access;
6956f26aa05SWill Deacon 	u32 val, ctrl_reg, alignment_mask;
6964a55c18eSWill Deacon 	struct perf_event *wp, **slots;
697f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
6986f26aa05SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
699f81ef4a9SWill Deacon 
7001436c1aaSChristoph Lameter 	slots = this_cpu_ptr(wp_on_reg);
7014a55c18eSWill Deacon 
702f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
703f81ef4a9SWill Deacon 		rcu_read_lock();
704f81ef4a9SWill Deacon 
70593a04a34SWill Deacon 		wp = slots[i];
70693a04a34SWill Deacon 
7076f26aa05SWill Deacon 		if (wp == NULL)
7086f26aa05SWill Deacon 			goto unlock;
7096f26aa05SWill Deacon 
7106f26aa05SWill Deacon 		info = counter_arch_bp(wp);
7116f26aa05SWill Deacon 		/*
7126f26aa05SWill Deacon 		 * The DFAR is an unknown value on debug architectures prior
7136f26aa05SWill Deacon 		 * to 7.1. Since we only allow a single watchpoint on these
7146f26aa05SWill Deacon 		 * older CPUs, we can set the trigger to the lowest possible
7156f26aa05SWill Deacon 		 * faulting address.
7166f26aa05SWill Deacon 		 */
7176f26aa05SWill Deacon 		if (debug_arch < ARM_DEBUG_ARCH_V7_1) {
7186f26aa05SWill Deacon 			BUG_ON(i > 0);
7196f26aa05SWill Deacon 			info->trigger = wp->attr.bp_addr;
7206f26aa05SWill Deacon 		} else {
7216f26aa05SWill Deacon 			if (info->ctrl.len == ARM_BREAKPOINT_LEN_8)
7226f26aa05SWill Deacon 				alignment_mask = 0x7;
7236f26aa05SWill Deacon 			else
7246f26aa05SWill Deacon 				alignment_mask = 0x3;
7256f26aa05SWill Deacon 
7266f26aa05SWill Deacon 			/* Check if the watchpoint value matches. */
7276f26aa05SWill Deacon 			val = read_wb_reg(ARM_BASE_WVR + i);
7286f26aa05SWill Deacon 			if (val != (addr & ~alignment_mask))
7296f26aa05SWill Deacon 				goto unlock;
7306f26aa05SWill Deacon 
7316f26aa05SWill Deacon 			/* Possible match, check the byte address select. */
7326f26aa05SWill Deacon 			ctrl_reg = read_wb_reg(ARM_BASE_WCR + i);
7336f26aa05SWill Deacon 			decode_ctrl_reg(ctrl_reg, &ctrl);
7346f26aa05SWill Deacon 			if (!((1 << (addr & alignment_mask)) & ctrl.len))
7356f26aa05SWill Deacon 				goto unlock;
7366f26aa05SWill Deacon 
7376f26aa05SWill Deacon 			/* Check that the access type matches. */
738bf880114SWill Deacon 			if (debug_exception_updates_fsr()) {
739bf880114SWill Deacon 				access = (fsr & ARM_FSR_ACCESS_MASK) ?
740bf880114SWill Deacon 					  HW_BREAKPOINT_W : HW_BREAKPOINT_R;
7416f26aa05SWill Deacon 				if (!(access & hw_breakpoint_type(wp)))
7426f26aa05SWill Deacon 					goto unlock;
743bf880114SWill Deacon 			}
7446f26aa05SWill Deacon 
7456f26aa05SWill Deacon 			/* We have a winner. */
7466f26aa05SWill Deacon 			info->trigger = addr;
747f81ef4a9SWill Deacon 		}
748f81ef4a9SWill Deacon 
749f81ef4a9SWill Deacon 		pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
75093a04a34SWill Deacon 		perf_bp_event(wp, regs);
751f81ef4a9SWill Deacon 
752f81ef4a9SWill Deacon 		/*
753f81ef4a9SWill Deacon 		 * If no overflow handler is present, insert a temporary
754f81ef4a9SWill Deacon 		 * mismatch breakpoint so we can single-step over the
755f81ef4a9SWill Deacon 		 * watchpoint trigger.
756f81ef4a9SWill Deacon 		 */
7571879445dSWang Nan 		if (is_default_overflow_handler(wp))
7589ebb3cbcSWill Deacon 			enable_single_step(wp, instruction_pointer(regs));
759f81ef4a9SWill Deacon 
7606f26aa05SWill Deacon unlock:
761f81ef4a9SWill Deacon 		rcu_read_unlock();
762f81ef4a9SWill Deacon 	}
763f81ef4a9SWill Deacon }
764f81ef4a9SWill Deacon 
76593a04a34SWill Deacon static void watchpoint_single_step_handler(unsigned long pc)
76693a04a34SWill Deacon {
76793a04a34SWill Deacon 	int i;
7684a55c18eSWill Deacon 	struct perf_event *wp, **slots;
76993a04a34SWill Deacon 	struct arch_hw_breakpoint *info;
77093a04a34SWill Deacon 
7711436c1aaSChristoph Lameter 	slots = this_cpu_ptr(wp_on_reg);
7724a55c18eSWill Deacon 
773c512de95SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
77493a04a34SWill Deacon 		rcu_read_lock();
77593a04a34SWill Deacon 
77693a04a34SWill Deacon 		wp = slots[i];
77793a04a34SWill Deacon 
77893a04a34SWill Deacon 		if (wp == NULL)
77993a04a34SWill Deacon 			goto unlock;
78093a04a34SWill Deacon 
78193a04a34SWill Deacon 		info = counter_arch_bp(wp);
78293a04a34SWill Deacon 		if (!info->step_ctrl.enabled)
78393a04a34SWill Deacon 			goto unlock;
78493a04a34SWill Deacon 
78593a04a34SWill Deacon 		/*
78693a04a34SWill Deacon 		 * Restore the original watchpoint if we've completed the
78793a04a34SWill Deacon 		 * single-step.
78893a04a34SWill Deacon 		 */
7899ebb3cbcSWill Deacon 		if (info->trigger != pc)
7909ebb3cbcSWill Deacon 			disable_single_step(wp);
79193a04a34SWill Deacon 
79293a04a34SWill Deacon unlock:
79393a04a34SWill Deacon 		rcu_read_unlock();
79493a04a34SWill Deacon 	}
79593a04a34SWill Deacon }
79693a04a34SWill Deacon 
797f81ef4a9SWill Deacon static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
798f81ef4a9SWill Deacon {
799f81ef4a9SWill Deacon 	int i;
800f81ef4a9SWill Deacon 	u32 ctrl_reg, val, addr;
8014a55c18eSWill Deacon 	struct perf_event *bp, **slots;
802f81ef4a9SWill Deacon 	struct arch_hw_breakpoint *info;
803f81ef4a9SWill Deacon 	struct arch_hw_breakpoint_ctrl ctrl;
804f81ef4a9SWill Deacon 
8051436c1aaSChristoph Lameter 	slots = this_cpu_ptr(bp_on_reg);
8064a55c18eSWill Deacon 
807f81ef4a9SWill Deacon 	/* The exception entry code places the amended lr in the PC. */
808f81ef4a9SWill Deacon 	addr = regs->ARM_pc;
809f81ef4a9SWill Deacon 
81093a04a34SWill Deacon 	/* Check the currently installed breakpoints first. */
81193a04a34SWill Deacon 	for (i = 0; i < core_num_brps; ++i) {
812f81ef4a9SWill Deacon 		rcu_read_lock();
813f81ef4a9SWill Deacon 
814f81ef4a9SWill Deacon 		bp = slots[i];
815f81ef4a9SWill Deacon 
8169ebb3cbcSWill Deacon 		if (bp == NULL)
8179ebb3cbcSWill Deacon 			goto unlock;
818f81ef4a9SWill Deacon 
8199ebb3cbcSWill Deacon 		info = counter_arch_bp(bp);
820f81ef4a9SWill Deacon 
821f81ef4a9SWill Deacon 		/* Check if the breakpoint value matches. */
822f81ef4a9SWill Deacon 		val = read_wb_reg(ARM_BASE_BVR + i);
823f81ef4a9SWill Deacon 		if (val != (addr & ~0x3))
8249ebb3cbcSWill Deacon 			goto mismatch;
825f81ef4a9SWill Deacon 
826f81ef4a9SWill Deacon 		/* Possible match, check the byte address select to confirm. */
827f81ef4a9SWill Deacon 		ctrl_reg = read_wb_reg(ARM_BASE_BCR + i);
828f81ef4a9SWill Deacon 		decode_ctrl_reg(ctrl_reg, &ctrl);
829f81ef4a9SWill Deacon 		if ((1 << (addr & 0x3)) & ctrl.len) {
830f81ef4a9SWill Deacon 			info->trigger = addr;
831f81ef4a9SWill Deacon 			pr_debug("breakpoint fired: address = 0x%x\n", addr);
832f81ef4a9SWill Deacon 			perf_bp_event(bp, regs);
8339ebb3cbcSWill Deacon 			if (!bp->overflow_handler)
8349ebb3cbcSWill Deacon 				enable_single_step(bp, addr);
8359ebb3cbcSWill Deacon 			goto unlock;
836f81ef4a9SWill Deacon 		}
837f81ef4a9SWill Deacon 
8389ebb3cbcSWill Deacon mismatch:
8399ebb3cbcSWill Deacon 		/* If we're stepping a breakpoint, it can now be restored. */
8409ebb3cbcSWill Deacon 		if (info->step_ctrl.enabled)
8419ebb3cbcSWill Deacon 			disable_single_step(bp);
8429ebb3cbcSWill Deacon unlock:
843f81ef4a9SWill Deacon 		rcu_read_unlock();
844f81ef4a9SWill Deacon 	}
84593a04a34SWill Deacon 
84693a04a34SWill Deacon 	/* Handle any pending watchpoint single-step breakpoints. */
84793a04a34SWill Deacon 	watchpoint_single_step_handler(addr);
848f81ef4a9SWill Deacon }
849f81ef4a9SWill Deacon 
850f81ef4a9SWill Deacon /*
851f81ef4a9SWill Deacon  * Called from either the Data Abort Handler [watchpoint] or the
85202fe2845SRussell King  * Prefetch Abort Handler [breakpoint] with interrupts disabled.
853f81ef4a9SWill Deacon  */
854f81ef4a9SWill Deacon static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
855f81ef4a9SWill Deacon 				 struct pt_regs *regs)
856f81ef4a9SWill Deacon {
8577e202696SWill Deacon 	int ret = 0;
858f81ef4a9SWill Deacon 	u32 dscr;
859f81ef4a9SWill Deacon 
86002fe2845SRussell King 	preempt_disable();
86102fe2845SRussell King 
86202fe2845SRussell King 	if (interrupts_enabled(regs))
86302fe2845SRussell King 		local_irq_enable();
8647e202696SWill Deacon 
865f81ef4a9SWill Deacon 	/* We only handle watchpoints and hardware breakpoints. */
8669e962f76SDietmar Eggemann 	ARM_DBG_READ(c0, c1, 0, dscr);
867f81ef4a9SWill Deacon 
868f81ef4a9SWill Deacon 	/* Perform perf callbacks. */
869f81ef4a9SWill Deacon 	switch (ARM_DSCR_MOE(dscr)) {
870f81ef4a9SWill Deacon 	case ARM_ENTRY_BREAKPOINT:
871f81ef4a9SWill Deacon 		breakpoint_handler(addr, regs);
872f81ef4a9SWill Deacon 		break;
873f81ef4a9SWill Deacon 	case ARM_ENTRY_ASYNC_WATCHPOINT:
874235584b6SJoe Perches 		WARN(1, "Asynchronous watchpoint exception taken. Debugging results may be unreliable\n");
875f81ef4a9SWill Deacon 	case ARM_ENTRY_SYNC_WATCHPOINT:
8766f26aa05SWill Deacon 		watchpoint_handler(addr, fsr, regs);
877f81ef4a9SWill Deacon 		break;
878f81ef4a9SWill Deacon 	default:
8797e202696SWill Deacon 		ret = 1; /* Unhandled fault. */
880f81ef4a9SWill Deacon 	}
881f81ef4a9SWill Deacon 
8827e202696SWill Deacon 	preempt_enable();
8837e202696SWill Deacon 
884f81ef4a9SWill Deacon 	return ret;
885f81ef4a9SWill Deacon }
886f81ef4a9SWill Deacon 
887f81ef4a9SWill Deacon /*
888f81ef4a9SWill Deacon  * One-time initialisation.
889f81ef4a9SWill Deacon  */
8900d352e3dSWill Deacon static cpumask_t debug_err_mask;
8910d352e3dSWill Deacon 
8920d352e3dSWill Deacon static int debug_reg_trap(struct pt_regs *regs, unsigned int instr)
8930d352e3dSWill Deacon {
8940d352e3dSWill Deacon 	int cpu = smp_processor_id();
8950d352e3dSWill Deacon 
8968b521cb2SJoe Perches 	pr_warn("Debug register access (0x%x) caused undefined instruction on CPU %d\n",
8970d352e3dSWill Deacon 		instr, cpu);
8980d352e3dSWill Deacon 
8990d352e3dSWill Deacon 	/* Set the error flag for this CPU and skip the faulting instruction. */
9000d352e3dSWill Deacon 	cpumask_set_cpu(cpu, &debug_err_mask);
9010d352e3dSWill Deacon 	instruction_pointer(regs) += 4;
9020d352e3dSWill Deacon 	return 0;
9030d352e3dSWill Deacon }
9040d352e3dSWill Deacon 
9050d352e3dSWill Deacon static struct undef_hook debug_reg_hook = {
9060d352e3dSWill Deacon 	.instr_mask	= 0x0fe80f10,
9070d352e3dSWill Deacon 	.instr_val	= 0x0e000e10,
9080d352e3dSWill Deacon 	.fn		= debug_reg_trap,
9090d352e3dSWill Deacon };
9100d352e3dSWill Deacon 
91157ba8997SDietmar Eggemann /* Does this core support OS Save and Restore? */
91257ba8997SDietmar Eggemann static bool core_has_os_save_restore(void)
91357ba8997SDietmar Eggemann {
91457ba8997SDietmar Eggemann 	u32 oslsr;
91557ba8997SDietmar Eggemann 
91657ba8997SDietmar Eggemann 	switch (get_debug_arch()) {
91757ba8997SDietmar Eggemann 	case ARM_DEBUG_ARCH_V7_1:
91857ba8997SDietmar Eggemann 		return true;
91957ba8997SDietmar Eggemann 	case ARM_DEBUG_ARCH_V7_ECP14:
92057ba8997SDietmar Eggemann 		ARM_DBG_READ(c1, c1, 4, oslsr);
92157ba8997SDietmar Eggemann 		if (oslsr & ARM_OSLSR_OSLM0)
92257ba8997SDietmar Eggemann 			return true;
92357ba8997SDietmar Eggemann 	default:
92457ba8997SDietmar Eggemann 		return false;
92557ba8997SDietmar Eggemann 	}
92657ba8997SDietmar Eggemann }
92757ba8997SDietmar Eggemann 
928*9b377e21SSebastian Andrzej Siewior static void reset_ctrl_regs(unsigned int cpu)
929f81ef4a9SWill Deacon {
930*9b377e21SSebastian Andrzej Siewior 	int i, raw_num_brps, err = 0;
931e64877dcSWill Deacon 	u32 val;
932f81ef4a9SWill Deacon 
933ac88e071SWill Deacon 	/*
934ac88e071SWill Deacon 	 * v7 debug contains save and restore registers so that debug state
935ed19b739SWill Deacon 	 * can be maintained across low-power modes without leaving the debug
936ed19b739SWill Deacon 	 * logic powered up. It is IMPLEMENTATION DEFINED whether we can access
937ed19b739SWill Deacon 	 * the debug registers out of reset, so we must unlock the OS Lock
938ed19b739SWill Deacon 	 * Access Register to avoid taking undefined instruction exceptions
939ed19b739SWill Deacon 	 * later on.
940ac88e071SWill Deacon 	 */
941b5d5b8f9SWill Deacon 	switch (debug_arch) {
942a26bce12SWill Deacon 	case ARM_DEBUG_ARCH_V6:
943a26bce12SWill Deacon 	case ARM_DEBUG_ARCH_V6_1:
9447f4050a0SWill Deacon 		/* ARMv6 cores clear the registers out of reset. */
9457f4050a0SWill Deacon 		goto out_mdbgen;
946b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_ECP14:
947ac88e071SWill Deacon 		/*
948c09bae70SWill Deacon 		 * Ensure sticky power-down is clear (i.e. debug logic is
949c09bae70SWill Deacon 		 * powered up).
950c09bae70SWill Deacon 		 */
9519e962f76SDietmar Eggemann 		ARM_DBG_READ(c1, c5, 4, val);
952e64877dcSWill Deacon 		if ((val & 0x1) == 0)
953b5d5b8f9SWill Deacon 			err = -EPERM;
954e64877dcSWill Deacon 
95557ba8997SDietmar Eggemann 		if (!has_ossr)
956e64877dcSWill Deacon 			goto clear_vcr;
957b5d5b8f9SWill Deacon 		break;
958b5d5b8f9SWill Deacon 	case ARM_DEBUG_ARCH_V7_1:
959b5d5b8f9SWill Deacon 		/*
960b5d5b8f9SWill Deacon 		 * Ensure the OS double lock is clear.
961b5d5b8f9SWill Deacon 		 */
9629e962f76SDietmar Eggemann 		ARM_DBG_READ(c1, c3, 4, val);
963e64877dcSWill Deacon 		if ((val & 0x1) == 1)
964b5d5b8f9SWill Deacon 			err = -EPERM;
965b5d5b8f9SWill Deacon 		break;
966b5d5b8f9SWill Deacon 	}
967b5d5b8f9SWill Deacon 
968b5d5b8f9SWill Deacon 	if (err) {
96968a154fcSSantosh Shilimkar 		pr_warn_once("CPU %d debug is powered down!\n", cpu);
9700d352e3dSWill Deacon 		cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
971c09bae70SWill Deacon 		return;
972c09bae70SWill Deacon 	}
973c09bae70SWill Deacon 
974c09bae70SWill Deacon 	/*
975e64877dcSWill Deacon 	 * Unconditionally clear the OS lock by writing a value
97602051eadSDietmar Eggemann 	 * other than CS_LAR_KEY to the access register.
977ac88e071SWill Deacon 	 */
978184901a0SMathieu Poirier 	ARM_DBG_WRITE(c1, c0, 4, ~CORESIGHT_UNLOCK);
979ac88e071SWill Deacon 	isb();
980e89c0d70SWill Deacon 
981e89c0d70SWill Deacon 	/*
982e89c0d70SWill Deacon 	 * Clear any configured vector-catch events before
983e89c0d70SWill Deacon 	 * enabling monitor mode.
984e89c0d70SWill Deacon 	 */
985e64877dcSWill Deacon clear_vcr:
9869e962f76SDietmar Eggemann 	ARM_DBG_WRITE(c0, c7, 0, 0);
987e89c0d70SWill Deacon 	isb();
988ac88e071SWill Deacon 
989614bea50SWill Deacon 	if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) {
99068a154fcSSantosh Shilimkar 		pr_warn_once("CPU %d failed to disable vector catch\n", cpu);
991f81ef4a9SWill Deacon 		return;
992614bea50SWill Deacon 	}
993f81ef4a9SWill Deacon 
994614bea50SWill Deacon 	/*
995614bea50SWill Deacon 	 * The control/value register pairs are UNKNOWN out of reset so
996614bea50SWill Deacon 	 * clear them to avoid spurious debug events.
997614bea50SWill Deacon 	 */
998c512de95SWill Deacon 	raw_num_brps = get_num_brp_resources();
999c512de95SWill Deacon 	for (i = 0; i < raw_num_brps; ++i) {
1000f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BCR + i, 0UL);
1001f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_BVR + i, 0UL);
1002f81ef4a9SWill Deacon 	}
1003f81ef4a9SWill Deacon 
1004f81ef4a9SWill Deacon 	for (i = 0; i < core_num_wrps; ++i) {
1005f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WCR + i, 0UL);
1006f81ef4a9SWill Deacon 		write_wb_reg(ARM_BASE_WVR + i, 0UL);
1007f81ef4a9SWill Deacon 	}
1008614bea50SWill Deacon 
1009614bea50SWill Deacon 	if (cpumask_intersects(&debug_err_mask, cpumask_of(cpu))) {
101068a154fcSSantosh Shilimkar 		pr_warn_once("CPU %d failed to clear debug register pairs\n", cpu);
1011614bea50SWill Deacon 		return;
1012614bea50SWill Deacon 	}
1013614bea50SWill Deacon 
1014614bea50SWill Deacon 	/*
1015614bea50SWill Deacon 	 * Have a crack at enabling monitor mode. We don't actually need
1016614bea50SWill Deacon 	 * it yet, but reporting an error early is useful if it fails.
1017614bea50SWill Deacon 	 */
10187f4050a0SWill Deacon out_mdbgen:
1019614bea50SWill Deacon 	if (enable_monitor_mode())
1020614bea50SWill Deacon 		cpumask_or(&debug_err_mask, &debug_err_mask, cpumask_of(cpu));
1021f81ef4a9SWill Deacon }
1022f81ef4a9SWill Deacon 
1023*9b377e21SSebastian Andrzej Siewior static int dbg_reset_online(unsigned int cpu)
10247d99331eSWill Deacon {
1025*9b377e21SSebastian Andrzej Siewior 	local_irq_disable();
1026*9b377e21SSebastian Andrzej Siewior 	reset_ctrl_regs(cpu);
1027*9b377e21SSebastian Andrzej Siewior 	local_irq_enable();
1028*9b377e21SSebastian Andrzej Siewior 	return 0;
10297d99331eSWill Deacon }
10307d99331eSWill Deacon 
10319a6eb310SDietmar Eggemann #ifdef CONFIG_CPU_PM
10329a6eb310SDietmar Eggemann static int dbg_cpu_pm_notify(struct notifier_block *self, unsigned long action,
10339a6eb310SDietmar Eggemann 			     void *v)
10349a6eb310SDietmar Eggemann {
10359a6eb310SDietmar Eggemann 	if (action == CPU_PM_EXIT)
1036*9b377e21SSebastian Andrzej Siewior 		reset_ctrl_regs(smp_processor_id());
10379a6eb310SDietmar Eggemann 
10389a6eb310SDietmar Eggemann 	return NOTIFY_OK;
10399a6eb310SDietmar Eggemann }
10409a6eb310SDietmar Eggemann 
104150acff3cSBastian Hecht static struct notifier_block dbg_cpu_pm_nb = {
10429a6eb310SDietmar Eggemann 	.notifier_call = dbg_cpu_pm_notify,
10439a6eb310SDietmar Eggemann };
10449a6eb310SDietmar Eggemann 
10459a6eb310SDietmar Eggemann static void __init pm_init(void)
10469a6eb310SDietmar Eggemann {
10479a6eb310SDietmar Eggemann 	cpu_pm_register_notifier(&dbg_cpu_pm_nb);
10489a6eb310SDietmar Eggemann }
10499a6eb310SDietmar Eggemann #else
10509a6eb310SDietmar Eggemann static inline void pm_init(void)
10519a6eb310SDietmar Eggemann {
10529a6eb310SDietmar Eggemann }
10539a6eb310SDietmar Eggemann #endif
10549a6eb310SDietmar Eggemann 
1055f81ef4a9SWill Deacon static int __init arch_hw_breakpoint_init(void)
1056f81ef4a9SWill Deacon {
1057*9b377e21SSebastian Andrzej Siewior 	int ret;
1058*9b377e21SSebastian Andrzej Siewior 
1059f81ef4a9SWill Deacon 	debug_arch = get_debug_arch();
1060f81ef4a9SWill Deacon 
106166e1cfe6SWill Deacon 	if (!debug_arch_supported()) {
1062f81ef4a9SWill Deacon 		pr_info("debug architecture 0x%x unsupported.\n", debug_arch);
10638fbf397cSWill Deacon 		return 0;
1064f81ef4a9SWill Deacon 	}
1065f81ef4a9SWill Deacon 
106657ba8997SDietmar Eggemann 	has_ossr = core_has_os_save_restore();
106757ba8997SDietmar Eggemann 
1068f81ef4a9SWill Deacon 	/* Determine how many BRPs/WRPs are available. */
1069f81ef4a9SWill Deacon 	core_num_brps = get_num_brps();
1070f81ef4a9SWill Deacon 	core_num_wrps = get_num_wrps();
1071f81ef4a9SWill Deacon 
10720d352e3dSWill Deacon 	/*
10730d352e3dSWill Deacon 	 * We need to tread carefully here because DBGSWENABLE may be
10740d352e3dSWill Deacon 	 * driven low on this core and there isn't an architected way to
10750d352e3dSWill Deacon 	 * determine that.
10760d352e3dSWill Deacon 	 */
1077*9b377e21SSebastian Andrzej Siewior 	get_online_cpus();
10780d352e3dSWill Deacon 	register_undef_hook(&debug_reg_hook);
1079f81ef4a9SWill Deacon 
1080f81ef4a9SWill Deacon 	/*
1081*9b377e21SSebastian Andrzej Siewior 	 * Register CPU notifier which resets the breakpoint resources. We
1082*9b377e21SSebastian Andrzej Siewior 	 * assume that a halting debugger will leave the world in a nice state
1083*9b377e21SSebastian Andrzej Siewior 	 * for us.
1084f81ef4a9SWill Deacon 	 */
1085*9b377e21SSebastian Andrzej Siewior 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "arm/hw_breakpoint:online",
1086*9b377e21SSebastian Andrzej Siewior 				dbg_reset_online, NULL);
10870d352e3dSWill Deacon 	unregister_undef_hook(&debug_reg_hook);
1088*9b377e21SSebastian Andrzej Siewior 	if (WARN_ON(ret < 0) || !cpumask_empty(&debug_err_mask)) {
1089c09bae70SWill Deacon 		core_num_brps = 0;
1090c09bae70SWill Deacon 		core_num_wrps = 0;
1091*9b377e21SSebastian Andrzej Siewior 		if (ret > 0)
1092*9b377e21SSebastian Andrzej Siewior 			cpuhp_remove_state_nocalls(ret);
1093*9b377e21SSebastian Andrzej Siewior 		put_online_cpus();
1094c09bae70SWill Deacon 		return 0;
1095c09bae70SWill Deacon 	}
1096ac88e071SWill Deacon 
10970d352e3dSWill Deacon 	pr_info("found %d " "%s" "breakpoint and %d watchpoint registers.\n",
10980d352e3dSWill Deacon 		core_num_brps, core_has_mismatch_brps() ? "(+1 reserved) " :
10990d352e3dSWill Deacon 		"", core_num_wrps);
11000d352e3dSWill Deacon 
1101ac88e071SWill Deacon 	/* Work out the maximum supported watchpoint length. */
1102ac88e071SWill Deacon 	max_watchpoint_len = get_max_wp_len();
1103ac88e071SWill Deacon 	pr_info("maximum watchpoint size is %u bytes.\n",
1104ac88e071SWill Deacon 			max_watchpoint_len);
1105f81ef4a9SWill Deacon 
1106f81ef4a9SWill Deacon 	/* Register debug fault handler. */
1107f7b8156dSCatalin Marinas 	hook_fault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
1108f7b8156dSCatalin Marinas 			TRAP_HWBKPT, "watchpoint debug exception");
1109f7b8156dSCatalin Marinas 	hook_ifault_code(FAULT_CODE_DEBUG, hw_breakpoint_pending, SIGTRAP,
1110f7b8156dSCatalin Marinas 			TRAP_HWBKPT, "breakpoint debug exception");
1111*9b377e21SSebastian Andrzej Siewior 	put_online_cpus();
1112f81ef4a9SWill Deacon 
1113*9b377e21SSebastian Andrzej Siewior 	/* Register PM notifiers. */
11149a6eb310SDietmar Eggemann 	pm_init();
11158fbf397cSWill Deacon 	return 0;
1116f81ef4a9SWill Deacon }
1117f81ef4a9SWill Deacon arch_initcall(arch_hw_breakpoint_init);
1118f81ef4a9SWill Deacon 
1119f81ef4a9SWill Deacon void hw_breakpoint_pmu_read(struct perf_event *bp)
1120f81ef4a9SWill Deacon {
1121f81ef4a9SWill Deacon }
1122f81ef4a9SWill Deacon 
1123f81ef4a9SWill Deacon /*
1124f81ef4a9SWill Deacon  * Dummy function to register with die_notifier.
1125f81ef4a9SWill Deacon  */
1126f81ef4a9SWill Deacon int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
1127f81ef4a9SWill Deacon 					unsigned long val, void *data)
1128f81ef4a9SWill Deacon {
1129f81ef4a9SWill Deacon 	return NOTIFY_DONE;
1130f81ef4a9SWill Deacon }
1131