162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Performance event support - PowerPC classic/server specific definitions.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/types.h>
962306a36Sopenharmony_ci#include <asm/hw_irq.h>
1062306a36Sopenharmony_ci#include <linux/device.h>
1162306a36Sopenharmony_ci#include <uapi/asm/perf_event.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/* Update perf_event_print_debug() if this changes */
1462306a36Sopenharmony_ci#define MAX_HWEVENTS		8
1562306a36Sopenharmony_ci#define MAX_EVENT_ALTERNATIVES	8
1662306a36Sopenharmony_ci#define MAX_LIMITED_HWCOUNTERS	2
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistruct perf_event;
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cistruct mmcr_regs {
2162306a36Sopenharmony_ci	unsigned long mmcr0;
2262306a36Sopenharmony_ci	unsigned long mmcr1;
2362306a36Sopenharmony_ci	unsigned long mmcr2;
2462306a36Sopenharmony_ci	unsigned long mmcra;
2562306a36Sopenharmony_ci	unsigned long mmcr3;
2662306a36Sopenharmony_ci};
2762306a36Sopenharmony_ci/*
2862306a36Sopenharmony_ci * This struct provides the constants and functions needed to
2962306a36Sopenharmony_ci * describe the PMU on a particular POWER-family CPU.
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_cistruct power_pmu {
3262306a36Sopenharmony_ci	const char	*name;
3362306a36Sopenharmony_ci	int		n_counter;
3462306a36Sopenharmony_ci	int		max_alternatives;
3562306a36Sopenharmony_ci	unsigned long	add_fields;
3662306a36Sopenharmony_ci	unsigned long	test_adder;
3762306a36Sopenharmony_ci	int		(*compute_mmcr)(u64 events[], int n_ev,
3862306a36Sopenharmony_ci				unsigned int hwc[], struct mmcr_regs *mmcr,
3962306a36Sopenharmony_ci				struct perf_event *pevents[], u32 flags);
4062306a36Sopenharmony_ci	int		(*get_constraint)(u64 event_id, unsigned long *mskp,
4162306a36Sopenharmony_ci				unsigned long *valp, u64 event_config1);
4262306a36Sopenharmony_ci	int		(*get_alternatives)(u64 event_id, unsigned int flags,
4362306a36Sopenharmony_ci				u64 alt[]);
4462306a36Sopenharmony_ci	void		(*get_mem_data_src)(union perf_mem_data_src *dsrc,
4562306a36Sopenharmony_ci				u32 flags, struct pt_regs *regs);
4662306a36Sopenharmony_ci	void		(*get_mem_weight)(u64 *weight, u64 type);
4762306a36Sopenharmony_ci	unsigned long	group_constraint_mask;
4862306a36Sopenharmony_ci	unsigned long	group_constraint_val;
4962306a36Sopenharmony_ci	u64             (*bhrb_filter_map)(u64 branch_sample_type);
5062306a36Sopenharmony_ci	void            (*config_bhrb)(u64 pmu_bhrb_filter);
5162306a36Sopenharmony_ci	void		(*disable_pmc)(unsigned int pmc, struct mmcr_regs *mmcr);
5262306a36Sopenharmony_ci	int		(*limited_pmc_event)(u64 event_id);
5362306a36Sopenharmony_ci	u32		flags;
5462306a36Sopenharmony_ci	const struct attribute_group	**attr_groups;
5562306a36Sopenharmony_ci	int		n_generic;
5662306a36Sopenharmony_ci	int		*generic_events;
5762306a36Sopenharmony_ci	u64		(*cache_events)[PERF_COUNT_HW_CACHE_MAX]
5862306a36Sopenharmony_ci			       [PERF_COUNT_HW_CACHE_OP_MAX]
5962306a36Sopenharmony_ci			       [PERF_COUNT_HW_CACHE_RESULT_MAX];
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	int		n_blacklist_ev;
6262306a36Sopenharmony_ci	int 		*blacklist_ev;
6362306a36Sopenharmony_ci	/* BHRB entries in the PMU */
6462306a36Sopenharmony_ci	int		bhrb_nr;
6562306a36Sopenharmony_ci	/*
6662306a36Sopenharmony_ci	 * set this flag with `PERF_PMU_CAP_EXTENDED_REGS` if
6762306a36Sopenharmony_ci	 * the pmu supports extended perf regs capability
6862306a36Sopenharmony_ci	 */
6962306a36Sopenharmony_ci	int		capabilities;
7062306a36Sopenharmony_ci	/*
7162306a36Sopenharmony_ci	 * Function to check event code for values which are
7262306a36Sopenharmony_ci	 * reserved. Function takes struct perf_event as input,
7362306a36Sopenharmony_ci	 * since event code could be spread in attr.config*
7462306a36Sopenharmony_ci	 */
7562306a36Sopenharmony_ci	int		(*check_attr_config)(struct perf_event *ev);
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci/*
7962306a36Sopenharmony_ci * Values for power_pmu.flags
8062306a36Sopenharmony_ci */
8162306a36Sopenharmony_ci#define PPMU_LIMITED_PMC5_6	0x00000001 /* PMC5/6 have limited function */
8262306a36Sopenharmony_ci#define PPMU_ALT_SIPR		0x00000002 /* uses alternate posn for SIPR/HV */
8362306a36Sopenharmony_ci#define PPMU_NO_SIPR		0x00000004 /* no SIPR/HV in MMCRA at all */
8462306a36Sopenharmony_ci#define PPMU_NO_CONT_SAMPLING	0x00000008 /* no continuous sampling */
8562306a36Sopenharmony_ci#define PPMU_SIAR_VALID		0x00000010 /* Processor has SIAR Valid bit */
8662306a36Sopenharmony_ci#define PPMU_HAS_SSLOT		0x00000020 /* Has sampled slot in MMCRA */
8762306a36Sopenharmony_ci#define PPMU_HAS_SIER		0x00000040 /* Has SIER */
8862306a36Sopenharmony_ci#define PPMU_ARCH_207S		0x00000080 /* PMC is architecture v2.07S */
8962306a36Sopenharmony_ci#define PPMU_NO_SIAR		0x00000100 /* Do not use SIAR */
9062306a36Sopenharmony_ci#define PPMU_ARCH_31		0x00000200 /* Has MMCR3, SIER2 and SIER3 */
9162306a36Sopenharmony_ci#define PPMU_P10_DD1		0x00000400 /* Is power10 DD1 processor version */
9262306a36Sopenharmony_ci#define PPMU_HAS_ATTR_CONFIG1	0x00000800 /* Using config1 attribute */
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * Values for flags to get_alternatives()
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ci#define PPMU_LIMITED_PMC_OK	1	/* can put this on a limited PMC */
9862306a36Sopenharmony_ci#define PPMU_LIMITED_PMC_REQD	2	/* have to put this on a limited PMC */
9962306a36Sopenharmony_ci#define PPMU_ONLY_COUNT_RUN	4	/* only counting in run state */
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciint __init register_power_pmu(struct power_pmu *pmu);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistruct pt_regs;
10462306a36Sopenharmony_ciextern unsigned long perf_misc_flags(struct pt_regs *regs);
10562306a36Sopenharmony_ciextern unsigned long perf_instruction_pointer(struct pt_regs *regs);
10662306a36Sopenharmony_ciextern unsigned long int read_bhrb(int n);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/*
10962306a36Sopenharmony_ci * Only override the default definitions in include/linux/perf_event.h
11062306a36Sopenharmony_ci * if we have hardware PMU support.
11162306a36Sopenharmony_ci */
11262306a36Sopenharmony_ci#ifdef CONFIG_PPC_PERF_CTRS
11362306a36Sopenharmony_ci#define perf_misc_flags(regs)	perf_misc_flags(regs)
11462306a36Sopenharmony_ci#endif
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci/*
11762306a36Sopenharmony_ci * The power_pmu.get_constraint function returns a 32/64-bit value and
11862306a36Sopenharmony_ci * a 32/64-bit mask that express the constraints between this event_id and
11962306a36Sopenharmony_ci * other events.
12062306a36Sopenharmony_ci *
12162306a36Sopenharmony_ci * The value and mask are divided up into (non-overlapping) bitfields
12262306a36Sopenharmony_ci * of three different types:
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * Select field: this expresses the constraint that some set of bits
12562306a36Sopenharmony_ci * in MMCR* needs to be set to a specific value for this event_id.  For a
12662306a36Sopenharmony_ci * select field, the mask contains 1s in every bit of the field, and
12762306a36Sopenharmony_ci * the value contains a unique value for each possible setting of the
12862306a36Sopenharmony_ci * MMCR* bits.  The constraint checking code will ensure that two events
12962306a36Sopenharmony_ci * that set the same field in their masks have the same value in their
13062306a36Sopenharmony_ci * value dwords.
13162306a36Sopenharmony_ci *
13262306a36Sopenharmony_ci * Add field: this expresses the constraint that there can be at most
13362306a36Sopenharmony_ci * N events in a particular class.  A field of k bits can be used for
13462306a36Sopenharmony_ci * N <= 2^(k-1) - 1.  The mask has the most significant bit of the field
13562306a36Sopenharmony_ci * set (and the other bits 0), and the value has only the least significant
13662306a36Sopenharmony_ci * bit of the field set.  In addition, the 'add_fields' and 'test_adder'
13762306a36Sopenharmony_ci * in the struct power_pmu for this processor come into play.  The
13862306a36Sopenharmony_ci * add_fields value contains 1 in the LSB of the field, and the
13962306a36Sopenharmony_ci * test_adder contains 2^(k-1) - 1 - N in the field.
14062306a36Sopenharmony_ci *
14162306a36Sopenharmony_ci * NAND field: this expresses the constraint that you may not have events
14262306a36Sopenharmony_ci * in all of a set of classes.  (For example, on PPC970, you can't select
14362306a36Sopenharmony_ci * events from the FPU, ISU and IDU simultaneously, although any two are
14462306a36Sopenharmony_ci * possible.)  For N classes, the field is N+1 bits wide, and each class
14562306a36Sopenharmony_ci * is assigned one bit from the least-significant N bits.  The mask has
14662306a36Sopenharmony_ci * only the most-significant bit set, and the value has only the bit
14762306a36Sopenharmony_ci * for the event_id's class set.  The test_adder has the least significant
14862306a36Sopenharmony_ci * bit set in the field.
14962306a36Sopenharmony_ci *
15062306a36Sopenharmony_ci * If an event_id is not subject to the constraint expressed by a particular
15162306a36Sopenharmony_ci * field, then it will have 0 in both the mask and value for that field.
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ciextern ssize_t power_events_sysfs_show(struct device *dev,
15562306a36Sopenharmony_ci				struct device_attribute *attr, char *page);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci/*
15862306a36Sopenharmony_ci * EVENT_VAR() is same as PMU_EVENT_VAR with a suffix.
15962306a36Sopenharmony_ci *
16062306a36Sopenharmony_ci * Having a suffix allows us to have aliases in sysfs - eg: the generic
16162306a36Sopenharmony_ci * event 'cpu-cycles' can have two entries in sysfs: 'cpu-cycles' and
16262306a36Sopenharmony_ci * 'PM_CYC' where the latter is the name by which the event is known in
16362306a36Sopenharmony_ci * POWER CPU specification.
16462306a36Sopenharmony_ci *
16562306a36Sopenharmony_ci * Similarly, some hardware and cache events use the same event code. Eg.
16662306a36Sopenharmony_ci * on POWER8, both "cache-references" and "L1-dcache-loads" events refer
16762306a36Sopenharmony_ci * to the same event, PM_LD_REF_L1.  The suffix, allows us to have two
16862306a36Sopenharmony_ci * sysfs objects for the same event and thus two entries/aliases in sysfs.
16962306a36Sopenharmony_ci */
17062306a36Sopenharmony_ci#define	EVENT_VAR(_id, _suffix)		event_attr_##_id##_suffix
17162306a36Sopenharmony_ci#define	EVENT_PTR(_id, _suffix)		&EVENT_VAR(_id, _suffix).attr.attr
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci#define	EVENT_ATTR(_name, _id, _suffix)					\
17462306a36Sopenharmony_ci	PMU_EVENT_ATTR(_name, EVENT_VAR(_id, _suffix), _id,		\
17562306a36Sopenharmony_ci			power_events_sysfs_show)
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci#define	GENERIC_EVENT_ATTR(_name, _id)	EVENT_ATTR(_name, _id, _g)
17862306a36Sopenharmony_ci#define	GENERIC_EVENT_PTR(_id)		EVENT_PTR(_id, _g)
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci#define	CACHE_EVENT_ATTR(_name, _id)	EVENT_ATTR(_name, _id, _c)
18162306a36Sopenharmony_ci#define	CACHE_EVENT_PTR(_id)		EVENT_PTR(_id, _c)
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci#define	POWER_EVENT_ATTR(_name, _id)	EVENT_ATTR(_name, _id, _p)
18462306a36Sopenharmony_ci#define	POWER_EVENT_PTR(_id)		EVENT_PTR(_id, _p)
185