xref: /OK3568_Linux_fs/kernel/arch/powerpc/include/asm/perf_event_server.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Performance event support - PowerPC classic/server specific definitions.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/types.h>
9*4882a593Smuzhiyun #include <asm/hw_irq.h>
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <uapi/asm/perf_event.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /* Update perf_event_print_debug() if this changes */
14*4882a593Smuzhiyun #define MAX_HWEVENTS		8
15*4882a593Smuzhiyun #define MAX_EVENT_ALTERNATIVES	8
16*4882a593Smuzhiyun #define MAX_LIMITED_HWCOUNTERS	2
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct perf_event;
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct mmcr_regs {
21*4882a593Smuzhiyun 	unsigned long mmcr0;
22*4882a593Smuzhiyun 	unsigned long mmcr1;
23*4882a593Smuzhiyun 	unsigned long mmcr2;
24*4882a593Smuzhiyun 	unsigned long mmcra;
25*4882a593Smuzhiyun 	unsigned long mmcr3;
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun  * This struct provides the constants and functions needed to
29*4882a593Smuzhiyun  * describe the PMU on a particular POWER-family CPU.
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun struct power_pmu {
32*4882a593Smuzhiyun 	const char	*name;
33*4882a593Smuzhiyun 	int		n_counter;
34*4882a593Smuzhiyun 	int		max_alternatives;
35*4882a593Smuzhiyun 	unsigned long	add_fields;
36*4882a593Smuzhiyun 	unsigned long	test_adder;
37*4882a593Smuzhiyun 	int		(*compute_mmcr)(u64 events[], int n_ev,
38*4882a593Smuzhiyun 				unsigned int hwc[], struct mmcr_regs *mmcr,
39*4882a593Smuzhiyun 				struct perf_event *pevents[]);
40*4882a593Smuzhiyun 	int		(*get_constraint)(u64 event_id, unsigned long *mskp,
41*4882a593Smuzhiyun 				unsigned long *valp);
42*4882a593Smuzhiyun 	int		(*get_alternatives)(u64 event_id, unsigned int flags,
43*4882a593Smuzhiyun 				u64 alt[]);
44*4882a593Smuzhiyun 	void		(*get_mem_data_src)(union perf_mem_data_src *dsrc,
45*4882a593Smuzhiyun 				u32 flags, struct pt_regs *regs);
46*4882a593Smuzhiyun 	void		(*get_mem_weight)(u64 *weight);
47*4882a593Smuzhiyun 	unsigned long	group_constraint_mask;
48*4882a593Smuzhiyun 	unsigned long	group_constraint_val;
49*4882a593Smuzhiyun 	u64             (*bhrb_filter_map)(u64 branch_sample_type);
50*4882a593Smuzhiyun 	void            (*config_bhrb)(u64 pmu_bhrb_filter);
51*4882a593Smuzhiyun 	void		(*disable_pmc)(unsigned int pmc, struct mmcr_regs *mmcr);
52*4882a593Smuzhiyun 	int		(*limited_pmc_event)(u64 event_id);
53*4882a593Smuzhiyun 	u32		flags;
54*4882a593Smuzhiyun 	const struct attribute_group	**attr_groups;
55*4882a593Smuzhiyun 	int		n_generic;
56*4882a593Smuzhiyun 	int		*generic_events;
57*4882a593Smuzhiyun 	u64		(*cache_events)[PERF_COUNT_HW_CACHE_MAX]
58*4882a593Smuzhiyun 			       [PERF_COUNT_HW_CACHE_OP_MAX]
59*4882a593Smuzhiyun 			       [PERF_COUNT_HW_CACHE_RESULT_MAX];
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	int		n_blacklist_ev;
62*4882a593Smuzhiyun 	int 		*blacklist_ev;
63*4882a593Smuzhiyun 	/* BHRB entries in the PMU */
64*4882a593Smuzhiyun 	int		bhrb_nr;
65*4882a593Smuzhiyun 	/*
66*4882a593Smuzhiyun 	 * set this flag with `PERF_PMU_CAP_EXTENDED_REGS` if
67*4882a593Smuzhiyun 	 * the pmu supports extended perf regs capability
68*4882a593Smuzhiyun 	 */
69*4882a593Smuzhiyun 	int		capabilities;
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun  * Values for power_pmu.flags
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun #define PPMU_LIMITED_PMC5_6	0x00000001 /* PMC5/6 have limited function */
76*4882a593Smuzhiyun #define PPMU_ALT_SIPR		0x00000002 /* uses alternate posn for SIPR/HV */
77*4882a593Smuzhiyun #define PPMU_NO_SIPR		0x00000004 /* no SIPR/HV in MMCRA at all */
78*4882a593Smuzhiyun #define PPMU_NO_CONT_SAMPLING	0x00000008 /* no continuous sampling */
79*4882a593Smuzhiyun #define PPMU_SIAR_VALID		0x00000010 /* Processor has SIAR Valid bit */
80*4882a593Smuzhiyun #define PPMU_HAS_SSLOT		0x00000020 /* Has sampled slot in MMCRA */
81*4882a593Smuzhiyun #define PPMU_HAS_SIER		0x00000040 /* Has SIER */
82*4882a593Smuzhiyun #define PPMU_ARCH_207S		0x00000080 /* PMC is architecture v2.07S */
83*4882a593Smuzhiyun #define PPMU_NO_SIAR		0x00000100 /* Do not use SIAR */
84*4882a593Smuzhiyun #define PPMU_ARCH_31		0x00000200 /* Has MMCR3, SIER2 and SIER3 */
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun  * Values for flags to get_alternatives()
88*4882a593Smuzhiyun  */
89*4882a593Smuzhiyun #define PPMU_LIMITED_PMC_OK	1	/* can put this on a limited PMC */
90*4882a593Smuzhiyun #define PPMU_LIMITED_PMC_REQD	2	/* have to put this on a limited PMC */
91*4882a593Smuzhiyun #define PPMU_ONLY_COUNT_RUN	4	/* only counting in run state */
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun extern int register_power_pmu(struct power_pmu *);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun struct pt_regs;
96*4882a593Smuzhiyun extern unsigned long perf_misc_flags(struct pt_regs *regs);
97*4882a593Smuzhiyun extern unsigned long perf_instruction_pointer(struct pt_regs *regs);
98*4882a593Smuzhiyun extern unsigned long int read_bhrb(int n);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun  * Only override the default definitions in include/linux/perf_event.h
102*4882a593Smuzhiyun  * if we have hardware PMU support.
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun #ifdef CONFIG_PPC_PERF_CTRS
105*4882a593Smuzhiyun #define perf_misc_flags(regs)	perf_misc_flags(regs)
106*4882a593Smuzhiyun #endif
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun  * The power_pmu.get_constraint function returns a 32/64-bit value and
110*4882a593Smuzhiyun  * a 32/64-bit mask that express the constraints between this event_id and
111*4882a593Smuzhiyun  * other events.
112*4882a593Smuzhiyun  *
113*4882a593Smuzhiyun  * The value and mask are divided up into (non-overlapping) bitfields
114*4882a593Smuzhiyun  * of three different types:
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * Select field: this expresses the constraint that some set of bits
117*4882a593Smuzhiyun  * in MMCR* needs to be set to a specific value for this event_id.  For a
118*4882a593Smuzhiyun  * select field, the mask contains 1s in every bit of the field, and
119*4882a593Smuzhiyun  * the value contains a unique value for each possible setting of the
120*4882a593Smuzhiyun  * MMCR* bits.  The constraint checking code will ensure that two events
121*4882a593Smuzhiyun  * that set the same field in their masks have the same value in their
122*4882a593Smuzhiyun  * value dwords.
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * Add field: this expresses the constraint that there can be at most
125*4882a593Smuzhiyun  * N events in a particular class.  A field of k bits can be used for
126*4882a593Smuzhiyun  * N <= 2^(k-1) - 1.  The mask has the most significant bit of the field
127*4882a593Smuzhiyun  * set (and the other bits 0), and the value has only the least significant
128*4882a593Smuzhiyun  * bit of the field set.  In addition, the 'add_fields' and 'test_adder'
129*4882a593Smuzhiyun  * in the struct power_pmu for this processor come into play.  The
130*4882a593Smuzhiyun  * add_fields value contains 1 in the LSB of the field, and the
131*4882a593Smuzhiyun  * test_adder contains 2^(k-1) - 1 - N in the field.
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * NAND field: this expresses the constraint that you may not have events
134*4882a593Smuzhiyun  * in all of a set of classes.  (For example, on PPC970, you can't select
135*4882a593Smuzhiyun  * events from the FPU, ISU and IDU simultaneously, although any two are
136*4882a593Smuzhiyun  * possible.)  For N classes, the field is N+1 bits wide, and each class
137*4882a593Smuzhiyun  * is assigned one bit from the least-significant N bits.  The mask has
138*4882a593Smuzhiyun  * only the most-significant bit set, and the value has only the bit
139*4882a593Smuzhiyun  * for the event_id's class set.  The test_adder has the least significant
140*4882a593Smuzhiyun  * bit set in the field.
141*4882a593Smuzhiyun  *
142*4882a593Smuzhiyun  * If an event_id is not subject to the constraint expressed by a particular
143*4882a593Smuzhiyun  * field, then it will have 0 in both the mask and value for that field.
144*4882a593Smuzhiyun  */
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun extern ssize_t power_events_sysfs_show(struct device *dev,
147*4882a593Smuzhiyun 				struct device_attribute *attr, char *page);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun /*
150*4882a593Smuzhiyun  * EVENT_VAR() is same as PMU_EVENT_VAR with a suffix.
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * Having a suffix allows us to have aliases in sysfs - eg: the generic
153*4882a593Smuzhiyun  * event 'cpu-cycles' can have two entries in sysfs: 'cpu-cycles' and
154*4882a593Smuzhiyun  * 'PM_CYC' where the latter is the name by which the event is known in
155*4882a593Smuzhiyun  * POWER CPU specification.
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * Similarly, some hardware and cache events use the same event code. Eg.
158*4882a593Smuzhiyun  * on POWER8, both "cache-references" and "L1-dcache-loads" events refer
159*4882a593Smuzhiyun  * to the same event, PM_LD_REF_L1.  The suffix, allows us to have two
160*4882a593Smuzhiyun  * sysfs objects for the same event and thus two entries/aliases in sysfs.
161*4882a593Smuzhiyun  */
162*4882a593Smuzhiyun #define	EVENT_VAR(_id, _suffix)		event_attr_##_id##_suffix
163*4882a593Smuzhiyun #define	EVENT_PTR(_id, _suffix)		&EVENT_VAR(_id, _suffix).attr.attr
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun #define	EVENT_ATTR(_name, _id, _suffix)					\
166*4882a593Smuzhiyun 	PMU_EVENT_ATTR(_name, EVENT_VAR(_id, _suffix), _id,		\
167*4882a593Smuzhiyun 			power_events_sysfs_show)
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun #define	GENERIC_EVENT_ATTR(_name, _id)	EVENT_ATTR(_name, _id, _g)
170*4882a593Smuzhiyun #define	GENERIC_EVENT_PTR(_id)		EVENT_PTR(_id, _g)
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun #define	CACHE_EVENT_ATTR(_name, _id)	EVENT_ATTR(_name, _id, _c)
173*4882a593Smuzhiyun #define	CACHE_EVENT_PTR(_id)		EVENT_PTR(_id, _c)
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun #define	POWER_EVENT_ATTR(_name, _id)	EVENT_ATTR(_name, _id, _p)
176*4882a593Smuzhiyun #define	POWER_EVENT_PTR(_id)		EVENT_PTR(_id, _p)
177