18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2016 Anju T, IBM Corporation.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/errno.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/sched.h>
98c2ecf20Sopenharmony_ci#include <linux/sched/task_stack.h>
108c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
118c2ecf20Sopenharmony_ci#include <linux/bug.h>
128c2ecf20Sopenharmony_ci#include <linux/stddef.h>
138c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
148c2ecf20Sopenharmony_ci#include <asm/perf_regs.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciu64 PERF_REG_EXTENDED_MASK;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define PT_REGS_OFFSET(id, r) [id] = offsetof(struct pt_regs, r)
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define REG_RESERVED (~(PERF_REG_EXTENDED_MASK | PERF_REG_PMU_MASK))
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic unsigned int pt_regs_offset[PERF_REG_POWERPC_MAX] = {
238c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R0,  gpr[0]),
248c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R1,  gpr[1]),
258c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R2,  gpr[2]),
268c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R3,  gpr[3]),
278c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R4,  gpr[4]),
288c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R5,  gpr[5]),
298c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R6,  gpr[6]),
308c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R7,  gpr[7]),
318c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R8,  gpr[8]),
328c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R9,  gpr[9]),
338c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R10, gpr[10]),
348c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R11, gpr[11]),
358c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R12, gpr[12]),
368c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R13, gpr[13]),
378c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R14, gpr[14]),
388c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R15, gpr[15]),
398c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R16, gpr[16]),
408c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R17, gpr[17]),
418c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R18, gpr[18]),
428c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R19, gpr[19]),
438c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R20, gpr[20]),
448c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R21, gpr[21]),
458c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R22, gpr[22]),
468c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R23, gpr[23]),
478c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R24, gpr[24]),
488c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R25, gpr[25]),
498c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R26, gpr[26]),
508c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R27, gpr[27]),
518c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R28, gpr[28]),
528c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R29, gpr[29]),
538c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R30, gpr[30]),
548c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_R31, gpr[31]),
558c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_NIP, nip),
568c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_MSR, msr),
578c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_ORIG_R3, orig_gpr3),
588c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_CTR, ctr),
598c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_LINK, link),
608c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_XER, xer),
618c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_CCR, ccr),
628c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
638c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_SOFTE, softe),
648c2ecf20Sopenharmony_ci#else
658c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_SOFTE, mq),
668c2ecf20Sopenharmony_ci#endif
678c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_TRAP, trap),
688c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_DAR, dar),
698c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_DSISR, dsisr),
708c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_SIER, dar),
718c2ecf20Sopenharmony_ci	PT_REGS_OFFSET(PERF_REG_POWERPC_MMCRA, dsisr),
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* Function to return the extended register values */
758c2ecf20Sopenharmony_cistatic u64 get_ext_regs_value(int idx)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	switch (idx) {
788c2ecf20Sopenharmony_ci	case PERF_REG_POWERPC_MMCR0:
798c2ecf20Sopenharmony_ci		return mfspr(SPRN_MMCR0);
808c2ecf20Sopenharmony_ci	case PERF_REG_POWERPC_MMCR1:
818c2ecf20Sopenharmony_ci		return mfspr(SPRN_MMCR1);
828c2ecf20Sopenharmony_ci	case PERF_REG_POWERPC_MMCR2:
838c2ecf20Sopenharmony_ci		return mfspr(SPRN_MMCR2);
848c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
858c2ecf20Sopenharmony_ci	case PERF_REG_POWERPC_MMCR3:
868c2ecf20Sopenharmony_ci		return mfspr(SPRN_MMCR3);
878c2ecf20Sopenharmony_ci	case PERF_REG_POWERPC_SIER2:
888c2ecf20Sopenharmony_ci		return mfspr(SPRN_SIER2);
898c2ecf20Sopenharmony_ci	case PERF_REG_POWERPC_SIER3:
908c2ecf20Sopenharmony_ci		return mfspr(SPRN_SIER3);
918c2ecf20Sopenharmony_ci#endif
928c2ecf20Sopenharmony_ci	default: return 0;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ciu64 perf_reg_value(struct pt_regs *regs, int idx)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	u64 perf_reg_extended_max = PERF_REG_POWERPC_MAX;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_ARCH_31))
1018c2ecf20Sopenharmony_ci		perf_reg_extended_max = PERF_REG_MAX_ISA_31;
1028c2ecf20Sopenharmony_ci	else if (cpu_has_feature(CPU_FTR_ARCH_300))
1038c2ecf20Sopenharmony_ci		perf_reg_extended_max = PERF_REG_MAX_ISA_300;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (idx == PERF_REG_POWERPC_SIER &&
1068c2ecf20Sopenharmony_ci	   (IS_ENABLED(CONFIG_FSL_EMB_PERF_EVENT) ||
1078c2ecf20Sopenharmony_ci	    IS_ENABLED(CONFIG_PPC32) ||
1088c2ecf20Sopenharmony_ci	    !is_sier_available()))
1098c2ecf20Sopenharmony_ci		return 0;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (idx == PERF_REG_POWERPC_MMCRA &&
1128c2ecf20Sopenharmony_ci	   (IS_ENABLED(CONFIG_FSL_EMB_PERF_EVENT) ||
1138c2ecf20Sopenharmony_ci	    IS_ENABLED(CONFIG_PPC32)))
1148c2ecf20Sopenharmony_ci		return 0;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (idx >= PERF_REG_POWERPC_MAX && idx < perf_reg_extended_max)
1178c2ecf20Sopenharmony_ci		return get_ext_regs_value(idx);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/*
1208c2ecf20Sopenharmony_ci	 * If the idx is referring to value beyond the
1218c2ecf20Sopenharmony_ci	 * supported registers, return 0 with a warning
1228c2ecf20Sopenharmony_ci	 */
1238c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(idx >= perf_reg_extended_max))
1248c2ecf20Sopenharmony_ci		return 0;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return regs_get_register(regs, pt_regs_offset[idx]);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciint perf_reg_validate(u64 mask)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	if (!mask || mask & REG_RESERVED)
1328c2ecf20Sopenharmony_ci		return -EINVAL;
1338c2ecf20Sopenharmony_ci	return 0;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciu64 perf_reg_abi(struct task_struct *task)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
1398c2ecf20Sopenharmony_ci	if (!test_tsk_thread_flag(task, TIF_32BIT))
1408c2ecf20Sopenharmony_ci		return PERF_SAMPLE_REGS_ABI_64;
1418c2ecf20Sopenharmony_ci	else
1428c2ecf20Sopenharmony_ci#endif
1438c2ecf20Sopenharmony_ci	return PERF_SAMPLE_REGS_ABI_32;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_civoid perf_get_regs_user(struct perf_regs *regs_user,
1478c2ecf20Sopenharmony_ci			struct pt_regs *regs)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	regs_user->regs = task_pt_regs(current);
1508c2ecf20Sopenharmony_ci	regs_user->abi = (regs_user->regs) ? perf_reg_abi(current) :
1518c2ecf20Sopenharmony_ci			 PERF_SAMPLE_REGS_ABI_NONE;
1528c2ecf20Sopenharmony_ci}
153