18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 ARM Ltd.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#ifndef __ASM_HW_BREAKPOINT_H
68c2ecf20Sopenharmony_ci#define __ASM_HW_BREAKPOINT_H
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <asm/cputype.h>
98c2ecf20Sopenharmony_ci#include <asm/cpufeature.h>
108c2ecf20Sopenharmony_ci#include <asm/sysreg.h>
118c2ecf20Sopenharmony_ci#include <asm/virt.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistruct arch_hw_breakpoint_ctrl {
148c2ecf20Sopenharmony_ci	u32 __reserved	: 19,
158c2ecf20Sopenharmony_ci	len		: 8,
168c2ecf20Sopenharmony_ci	type		: 2,
178c2ecf20Sopenharmony_ci	privilege	: 2,
188c2ecf20Sopenharmony_ci	enabled		: 1;
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct arch_hw_breakpoint {
228c2ecf20Sopenharmony_ci	u64 address;
238c2ecf20Sopenharmony_ci	u64 trigger;
248c2ecf20Sopenharmony_ci	struct arch_hw_breakpoint_ctrl ctrl;
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* Privilege Levels */
288c2ecf20Sopenharmony_ci#define AARCH64_BREAKPOINT_EL1	1
298c2ecf20Sopenharmony_ci#define AARCH64_BREAKPOINT_EL0	2
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define DBG_HMC_HYP		(1 << 13)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic inline u32 encode_ctrl_reg(struct arch_hw_breakpoint_ctrl ctrl)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	u32 val = (ctrl.len << 5) | (ctrl.type << 3) | (ctrl.privilege << 1) |
368c2ecf20Sopenharmony_ci		ctrl.enabled;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	if (is_kernel_in_hyp_mode() && ctrl.privilege == AARCH64_BREAKPOINT_EL1)
398c2ecf20Sopenharmony_ci		val |= DBG_HMC_HYP;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	return val;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic inline void decode_ctrl_reg(u32 reg,
458c2ecf20Sopenharmony_ci				   struct arch_hw_breakpoint_ctrl *ctrl)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	ctrl->enabled	= reg & 0x1;
488c2ecf20Sopenharmony_ci	reg >>= 1;
498c2ecf20Sopenharmony_ci	ctrl->privilege	= reg & 0x3;
508c2ecf20Sopenharmony_ci	reg >>= 2;
518c2ecf20Sopenharmony_ci	ctrl->type	= reg & 0x3;
528c2ecf20Sopenharmony_ci	reg >>= 2;
538c2ecf20Sopenharmony_ci	ctrl->len	= reg & 0xff;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Breakpoint */
578c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_EXECUTE	0
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* Watchpoints */
608c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LOAD	1
618c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_STORE	2
628c2ecf20Sopenharmony_ci#define AARCH64_ESR_ACCESS_MASK	(1 << 6)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* Lengths */
658c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LEN_1	0x1
668c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LEN_2	0x3
678c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LEN_3	0x7
688c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LEN_4	0xf
698c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LEN_5	0x1f
708c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LEN_6	0x3f
718c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LEN_7	0x7f
728c2ecf20Sopenharmony_ci#define ARM_BREAKPOINT_LEN_8	0xff
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* Kernel stepping */
758c2ecf20Sopenharmony_ci#define ARM_KERNEL_STEP_NONE	0
768c2ecf20Sopenharmony_ci#define ARM_KERNEL_STEP_ACTIVE	1
778c2ecf20Sopenharmony_ci#define ARM_KERNEL_STEP_SUSPEND	2
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * Limits.
818c2ecf20Sopenharmony_ci * Changing these will require modifications to the register accessors.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_ci#define ARM_MAX_BRP		16
848c2ecf20Sopenharmony_ci#define ARM_MAX_WRP		16
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* Virtual debug register bases. */
878c2ecf20Sopenharmony_ci#define AARCH64_DBG_REG_BVR	0
888c2ecf20Sopenharmony_ci#define AARCH64_DBG_REG_BCR	(AARCH64_DBG_REG_BVR + ARM_MAX_BRP)
898c2ecf20Sopenharmony_ci#define AARCH64_DBG_REG_WVR	(AARCH64_DBG_REG_BCR + ARM_MAX_BRP)
908c2ecf20Sopenharmony_ci#define AARCH64_DBG_REG_WCR	(AARCH64_DBG_REG_WVR + ARM_MAX_WRP)
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/* Debug register names. */
938c2ecf20Sopenharmony_ci#define AARCH64_DBG_REG_NAME_BVR	bvr
948c2ecf20Sopenharmony_ci#define AARCH64_DBG_REG_NAME_BCR	bcr
958c2ecf20Sopenharmony_ci#define AARCH64_DBG_REG_NAME_WVR	wvr
968c2ecf20Sopenharmony_ci#define AARCH64_DBG_REG_NAME_WCR	wcr
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* Accessor macros for the debug registers. */
998c2ecf20Sopenharmony_ci#define AARCH64_DBG_READ(N, REG, VAL) do {\
1008c2ecf20Sopenharmony_ci	VAL = read_sysreg(dbg##REG##N##_el1);\
1018c2ecf20Sopenharmony_ci} while (0)
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define AARCH64_DBG_WRITE(N, REG, VAL) do {\
1048c2ecf20Sopenharmony_ci	write_sysreg(VAL, dbg##REG##N##_el1);\
1058c2ecf20Sopenharmony_ci} while (0)
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistruct task_struct;
1088c2ecf20Sopenharmony_cistruct notifier_block;
1098c2ecf20Sopenharmony_cistruct perf_event_attr;
1108c2ecf20Sopenharmony_cistruct perf_event;
1118c2ecf20Sopenharmony_cistruct pmu;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciextern int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,
1148c2ecf20Sopenharmony_ci				  int *gen_len, int *gen_type, int *offset);
1158c2ecf20Sopenharmony_ciextern int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw);
1168c2ecf20Sopenharmony_ciextern int hw_breakpoint_arch_parse(struct perf_event *bp,
1178c2ecf20Sopenharmony_ci				    const struct perf_event_attr *attr,
1188c2ecf20Sopenharmony_ci				    struct arch_hw_breakpoint *hw);
1198c2ecf20Sopenharmony_ciextern int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
1208c2ecf20Sopenharmony_ci					   unsigned long val, void *data);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciextern int arch_install_hw_breakpoint(struct perf_event *bp);
1238c2ecf20Sopenharmony_ciextern void arch_uninstall_hw_breakpoint(struct perf_event *bp);
1248c2ecf20Sopenharmony_ciextern void hw_breakpoint_pmu_read(struct perf_event *bp);
1258c2ecf20Sopenharmony_ciextern int hw_breakpoint_slots(int type);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_HW_BREAKPOINT
1288c2ecf20Sopenharmony_ciextern void hw_breakpoint_thread_switch(struct task_struct *next);
1298c2ecf20Sopenharmony_ciextern void ptrace_hw_copy_thread(struct task_struct *task);
1308c2ecf20Sopenharmony_ci#else
1318c2ecf20Sopenharmony_cistatic inline void hw_breakpoint_thread_switch(struct task_struct *next)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_cistatic inline void ptrace_hw_copy_thread(struct task_struct *task)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci#endif
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/* Determine number of BRP registers available. */
1408c2ecf20Sopenharmony_cistatic inline int get_num_brps(void)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	u64 dfr0 = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
1438c2ecf20Sopenharmony_ci	return 1 +
1448c2ecf20Sopenharmony_ci		cpuid_feature_extract_unsigned_field(dfr0,
1458c2ecf20Sopenharmony_ci						ID_AA64DFR0_BRPS_SHIFT);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/* Determine number of WRP registers available. */
1498c2ecf20Sopenharmony_cistatic inline int get_num_wrps(void)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	u64 dfr0 = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
1528c2ecf20Sopenharmony_ci	return 1 +
1538c2ecf20Sopenharmony_ci		cpuid_feature_extract_unsigned_field(dfr0,
1548c2ecf20Sopenharmony_ci						ID_AA64DFR0_WRPS_SHIFT);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#endif	/* __ASM_BREAKPOINT_H */
158