xref: /OK3568_Linux_fs/kernel/drivers/perf/arm_smmuv3_pmu.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun  * This driver adds support for perf events to use the Performance
5*4882a593Smuzhiyun  * Monitor Counter Groups (PMCG) associated with an SMMUv3 node
6*4882a593Smuzhiyun  * to monitor that node.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * SMMUv3 PMCG devices are named as smmuv3_pmcg_<phys_addr_page> where
9*4882a593Smuzhiyun  * <phys_addr_page> is the physical page address of the SMMU PMCG wrapped
10*4882a593Smuzhiyun  * to 4K boundary. For example, the PMCG at 0xff88840000 is named
11*4882a593Smuzhiyun  * smmuv3_pmcg_ff88840
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Filtering by stream id is done by specifying filtering parameters
14*4882a593Smuzhiyun  * with the event. options are:
15*4882a593Smuzhiyun  *   filter_enable    - 0 = no filtering, 1 = filtering enabled
16*4882a593Smuzhiyun  *   filter_span      - 0 = exact match, 1 = pattern match
17*4882a593Smuzhiyun  *   filter_stream_id - pattern to filter against
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * To match a partial StreamID where the X most-significant bits must match
20*4882a593Smuzhiyun  * but the Y least-significant bits might differ, STREAMID is programmed
21*4882a593Smuzhiyun  * with a value that contains:
22*4882a593Smuzhiyun  *  STREAMID[Y - 1] == 0.
23*4882a593Smuzhiyun  *  STREAMID[Y - 2:0] == 1 (where Y > 1).
24*4882a593Smuzhiyun  * The remainder of implemented bits of STREAMID (X bits, from bit Y upwards)
25*4882a593Smuzhiyun  * contain a value to match from the corresponding bits of event StreamID.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * Example: perf stat -e smmuv3_pmcg_ff88840/transaction,filter_enable=1,
28*4882a593Smuzhiyun  *                    filter_span=1,filter_stream_id=0x42/ -a netperf
29*4882a593Smuzhiyun  * Applies filter pattern 0x42 to transaction events, which means events
30*4882a593Smuzhiyun  * matching stream ids 0x42 and 0x43 are counted. Further filtering
31*4882a593Smuzhiyun  * information is available in the SMMU documentation.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * SMMU events are not attributable to a CPU, so task mode and sampling
34*4882a593Smuzhiyun  * are not supported.
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include <linux/acpi.h>
38*4882a593Smuzhiyun #include <linux/acpi_iort.h>
39*4882a593Smuzhiyun #include <linux/bitfield.h>
40*4882a593Smuzhiyun #include <linux/bitops.h>
41*4882a593Smuzhiyun #include <linux/cpuhotplug.h>
42*4882a593Smuzhiyun #include <linux/cpumask.h>
43*4882a593Smuzhiyun #include <linux/device.h>
44*4882a593Smuzhiyun #include <linux/errno.h>
45*4882a593Smuzhiyun #include <linux/interrupt.h>
46*4882a593Smuzhiyun #include <linux/irq.h>
47*4882a593Smuzhiyun #include <linux/kernel.h>
48*4882a593Smuzhiyun #include <linux/list.h>
49*4882a593Smuzhiyun #include <linux/msi.h>
50*4882a593Smuzhiyun #include <linux/perf_event.h>
51*4882a593Smuzhiyun #include <linux/platform_device.h>
52*4882a593Smuzhiyun #include <linux/smp.h>
53*4882a593Smuzhiyun #include <linux/sysfs.h>
54*4882a593Smuzhiyun #include <linux/types.h>
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define SMMU_PMCG_EVCNTR0               0x0
57*4882a593Smuzhiyun #define SMMU_PMCG_EVCNTR(n, stride)     (SMMU_PMCG_EVCNTR0 + (n) * (stride))
58*4882a593Smuzhiyun #define SMMU_PMCG_EVTYPER0              0x400
59*4882a593Smuzhiyun #define SMMU_PMCG_EVTYPER(n)            (SMMU_PMCG_EVTYPER0 + (n) * 4)
60*4882a593Smuzhiyun #define SMMU_PMCG_SID_SPAN_SHIFT        29
61*4882a593Smuzhiyun #define SMMU_PMCG_SMR0                  0xA00
62*4882a593Smuzhiyun #define SMMU_PMCG_SMR(n)                (SMMU_PMCG_SMR0 + (n) * 4)
63*4882a593Smuzhiyun #define SMMU_PMCG_CNTENSET0             0xC00
64*4882a593Smuzhiyun #define SMMU_PMCG_CNTENCLR0             0xC20
65*4882a593Smuzhiyun #define SMMU_PMCG_INTENSET0             0xC40
66*4882a593Smuzhiyun #define SMMU_PMCG_INTENCLR0             0xC60
67*4882a593Smuzhiyun #define SMMU_PMCG_OVSCLR0               0xC80
68*4882a593Smuzhiyun #define SMMU_PMCG_OVSSET0               0xCC0
69*4882a593Smuzhiyun #define SMMU_PMCG_CFGR                  0xE00
70*4882a593Smuzhiyun #define SMMU_PMCG_CFGR_SID_FILTER_TYPE  BIT(23)
71*4882a593Smuzhiyun #define SMMU_PMCG_CFGR_MSI              BIT(21)
72*4882a593Smuzhiyun #define SMMU_PMCG_CFGR_RELOC_CTRS       BIT(20)
73*4882a593Smuzhiyun #define SMMU_PMCG_CFGR_SIZE             GENMASK(13, 8)
74*4882a593Smuzhiyun #define SMMU_PMCG_CFGR_NCTR             GENMASK(5, 0)
75*4882a593Smuzhiyun #define SMMU_PMCG_CR                    0xE04
76*4882a593Smuzhiyun #define SMMU_PMCG_CR_ENABLE             BIT(0)
77*4882a593Smuzhiyun #define SMMU_PMCG_CEID0                 0xE20
78*4882a593Smuzhiyun #define SMMU_PMCG_CEID1                 0xE28
79*4882a593Smuzhiyun #define SMMU_PMCG_IRQ_CTRL              0xE50
80*4882a593Smuzhiyun #define SMMU_PMCG_IRQ_CTRL_IRQEN        BIT(0)
81*4882a593Smuzhiyun #define SMMU_PMCG_IRQ_CFG0              0xE58
82*4882a593Smuzhiyun #define SMMU_PMCG_IRQ_CFG1              0xE60
83*4882a593Smuzhiyun #define SMMU_PMCG_IRQ_CFG2              0xE64
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* MSI config fields */
86*4882a593Smuzhiyun #define MSI_CFG0_ADDR_MASK              GENMASK_ULL(51, 2)
87*4882a593Smuzhiyun #define MSI_CFG2_MEMATTR_DEVICE_nGnRE   0x1
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun #define SMMU_PMCG_DEFAULT_FILTER_SPAN   1
90*4882a593Smuzhiyun #define SMMU_PMCG_DEFAULT_FILTER_SID    GENMASK(31, 0)
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #define SMMU_PMCG_MAX_COUNTERS          64
93*4882a593Smuzhiyun #define SMMU_PMCG_ARCH_MAX_EVENTS       128
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #define SMMU_PMCG_PA_SHIFT              12
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define SMMU_PMCG_EVCNTR_RDONLY         BIT(0)
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun static int cpuhp_state_num;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun struct smmu_pmu {
102*4882a593Smuzhiyun 	struct hlist_node node;
103*4882a593Smuzhiyun 	struct perf_event *events[SMMU_PMCG_MAX_COUNTERS];
104*4882a593Smuzhiyun 	DECLARE_BITMAP(used_counters, SMMU_PMCG_MAX_COUNTERS);
105*4882a593Smuzhiyun 	DECLARE_BITMAP(supported_events, SMMU_PMCG_ARCH_MAX_EVENTS);
106*4882a593Smuzhiyun 	unsigned int irq;
107*4882a593Smuzhiyun 	unsigned int on_cpu;
108*4882a593Smuzhiyun 	struct pmu pmu;
109*4882a593Smuzhiyun 	unsigned int num_counters;
110*4882a593Smuzhiyun 	struct device *dev;
111*4882a593Smuzhiyun 	void __iomem *reg_base;
112*4882a593Smuzhiyun 	void __iomem *reloc_base;
113*4882a593Smuzhiyun 	u64 counter_mask;
114*4882a593Smuzhiyun 	u32 options;
115*4882a593Smuzhiyun 	bool global_filter;
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #define to_smmu_pmu(p) (container_of(p, struct smmu_pmu, pmu))
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun #define SMMU_PMU_EVENT_ATTR_EXTRACTOR(_name, _config, _start, _end)        \
121*4882a593Smuzhiyun 	static inline u32 get_##_name(struct perf_event *event)            \
122*4882a593Smuzhiyun 	{                                                                  \
123*4882a593Smuzhiyun 		return FIELD_GET(GENMASK_ULL(_end, _start),                \
124*4882a593Smuzhiyun 				 event->attr._config);                     \
125*4882a593Smuzhiyun 	}                                                                  \
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun SMMU_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 15);
128*4882a593Smuzhiyun SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_stream_id, config1, 0, 31);
129*4882a593Smuzhiyun SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_span, config1, 32, 32);
130*4882a593Smuzhiyun SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_enable, config1, 33, 33);
131*4882a593Smuzhiyun 
smmu_pmu_enable(struct pmu * pmu)132*4882a593Smuzhiyun static inline void smmu_pmu_enable(struct pmu *pmu)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	writel(SMMU_PMCG_IRQ_CTRL_IRQEN,
137*4882a593Smuzhiyun 	       smmu_pmu->reg_base + SMMU_PMCG_IRQ_CTRL);
138*4882a593Smuzhiyun 	writel(SMMU_PMCG_CR_ENABLE, smmu_pmu->reg_base + SMMU_PMCG_CR);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
smmu_pmu_disable(struct pmu * pmu)141*4882a593Smuzhiyun static inline void smmu_pmu_disable(struct pmu *pmu)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	writel(0, smmu_pmu->reg_base + SMMU_PMCG_CR);
146*4882a593Smuzhiyun 	writel(0, smmu_pmu->reg_base + SMMU_PMCG_IRQ_CTRL);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
smmu_pmu_counter_set_value(struct smmu_pmu * smmu_pmu,u32 idx,u64 value)149*4882a593Smuzhiyun static inline void smmu_pmu_counter_set_value(struct smmu_pmu *smmu_pmu,
150*4882a593Smuzhiyun 					      u32 idx, u64 value)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	if (smmu_pmu->counter_mask & BIT(32))
153*4882a593Smuzhiyun 		writeq(value, smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 8));
154*4882a593Smuzhiyun 	else
155*4882a593Smuzhiyun 		writel(value, smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 4));
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
smmu_pmu_counter_get_value(struct smmu_pmu * smmu_pmu,u32 idx)158*4882a593Smuzhiyun static inline u64 smmu_pmu_counter_get_value(struct smmu_pmu *smmu_pmu, u32 idx)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	u64 value;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (smmu_pmu->counter_mask & BIT(32))
163*4882a593Smuzhiyun 		value = readq(smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 8));
164*4882a593Smuzhiyun 	else
165*4882a593Smuzhiyun 		value = readl(smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 4));
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	return value;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
smmu_pmu_counter_enable(struct smmu_pmu * smmu_pmu,u32 idx)170*4882a593Smuzhiyun static inline void smmu_pmu_counter_enable(struct smmu_pmu *smmu_pmu, u32 idx)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_CNTENSET0);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
smmu_pmu_counter_disable(struct smmu_pmu * smmu_pmu,u32 idx)175*4882a593Smuzhiyun static inline void smmu_pmu_counter_disable(struct smmu_pmu *smmu_pmu, u32 idx)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
smmu_pmu_interrupt_enable(struct smmu_pmu * smmu_pmu,u32 idx)180*4882a593Smuzhiyun static inline void smmu_pmu_interrupt_enable(struct smmu_pmu *smmu_pmu, u32 idx)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_INTENSET0);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
smmu_pmu_interrupt_disable(struct smmu_pmu * smmu_pmu,u32 idx)185*4882a593Smuzhiyun static inline void smmu_pmu_interrupt_disable(struct smmu_pmu *smmu_pmu,
186*4882a593Smuzhiyun 					      u32 idx)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
smmu_pmu_set_evtyper(struct smmu_pmu * smmu_pmu,u32 idx,u32 val)191*4882a593Smuzhiyun static inline void smmu_pmu_set_evtyper(struct smmu_pmu *smmu_pmu, u32 idx,
192*4882a593Smuzhiyun 					u32 val)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	writel(val, smmu_pmu->reg_base + SMMU_PMCG_EVTYPER(idx));
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
smmu_pmu_set_smr(struct smmu_pmu * smmu_pmu,u32 idx,u32 val)197*4882a593Smuzhiyun static inline void smmu_pmu_set_smr(struct smmu_pmu *smmu_pmu, u32 idx, u32 val)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	writel(val, smmu_pmu->reg_base + SMMU_PMCG_SMR(idx));
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
smmu_pmu_event_update(struct perf_event * event)202*4882a593Smuzhiyun static void smmu_pmu_event_update(struct perf_event *event)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
205*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
206*4882a593Smuzhiyun 	u64 delta, prev, now;
207*4882a593Smuzhiyun 	u32 idx = hwc->idx;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	do {
210*4882a593Smuzhiyun 		prev = local64_read(&hwc->prev_count);
211*4882a593Smuzhiyun 		now = smmu_pmu_counter_get_value(smmu_pmu, idx);
212*4882a593Smuzhiyun 	} while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	/* handle overflow. */
215*4882a593Smuzhiyun 	delta = now - prev;
216*4882a593Smuzhiyun 	delta &= smmu_pmu->counter_mask;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	local64_add(delta, &event->count);
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
smmu_pmu_set_period(struct smmu_pmu * smmu_pmu,struct hw_perf_event * hwc)221*4882a593Smuzhiyun static void smmu_pmu_set_period(struct smmu_pmu *smmu_pmu,
222*4882a593Smuzhiyun 				struct hw_perf_event *hwc)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	u32 idx = hwc->idx;
225*4882a593Smuzhiyun 	u64 new;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	if (smmu_pmu->options & SMMU_PMCG_EVCNTR_RDONLY) {
228*4882a593Smuzhiyun 		/*
229*4882a593Smuzhiyun 		 * On platforms that require this quirk, if the counter starts
230*4882a593Smuzhiyun 		 * at < half_counter value and wraps, the current logic of
231*4882a593Smuzhiyun 		 * handling the overflow may not work. It is expected that,
232*4882a593Smuzhiyun 		 * those platforms will have full 64 counter bits implemented
233*4882a593Smuzhiyun 		 * so that such a possibility is remote(eg: HiSilicon HIP08).
234*4882a593Smuzhiyun 		 */
235*4882a593Smuzhiyun 		new = smmu_pmu_counter_get_value(smmu_pmu, idx);
236*4882a593Smuzhiyun 	} else {
237*4882a593Smuzhiyun 		/*
238*4882a593Smuzhiyun 		 * We limit the max period to half the max counter value
239*4882a593Smuzhiyun 		 * of the counter size, so that even in the case of extreme
240*4882a593Smuzhiyun 		 * interrupt latency the counter will (hopefully) not wrap
241*4882a593Smuzhiyun 		 * past its initial value.
242*4882a593Smuzhiyun 		 */
243*4882a593Smuzhiyun 		new = smmu_pmu->counter_mask >> 1;
244*4882a593Smuzhiyun 		smmu_pmu_counter_set_value(smmu_pmu, idx, new);
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	local64_set(&hwc->prev_count, new);
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
smmu_pmu_set_event_filter(struct perf_event * event,int idx,u32 span,u32 sid)250*4882a593Smuzhiyun static void smmu_pmu_set_event_filter(struct perf_event *event,
251*4882a593Smuzhiyun 				      int idx, u32 span, u32 sid)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
254*4882a593Smuzhiyun 	u32 evtyper;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	evtyper = get_event(event) | span << SMMU_PMCG_SID_SPAN_SHIFT;
257*4882a593Smuzhiyun 	smmu_pmu_set_evtyper(smmu_pmu, idx, evtyper);
258*4882a593Smuzhiyun 	smmu_pmu_set_smr(smmu_pmu, idx, sid);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
smmu_pmu_check_global_filter(struct perf_event * curr,struct perf_event * new)261*4882a593Smuzhiyun static bool smmu_pmu_check_global_filter(struct perf_event *curr,
262*4882a593Smuzhiyun 					 struct perf_event *new)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	if (get_filter_enable(new) != get_filter_enable(curr))
265*4882a593Smuzhiyun 		return false;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	if (!get_filter_enable(new))
268*4882a593Smuzhiyun 		return true;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	return get_filter_span(new) == get_filter_span(curr) &&
271*4882a593Smuzhiyun 	       get_filter_stream_id(new) == get_filter_stream_id(curr);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun 
smmu_pmu_apply_event_filter(struct smmu_pmu * smmu_pmu,struct perf_event * event,int idx)274*4882a593Smuzhiyun static int smmu_pmu_apply_event_filter(struct smmu_pmu *smmu_pmu,
275*4882a593Smuzhiyun 				       struct perf_event *event, int idx)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	u32 span, sid;
278*4882a593Smuzhiyun 	unsigned int cur_idx, num_ctrs = smmu_pmu->num_counters;
279*4882a593Smuzhiyun 	bool filter_en = !!get_filter_enable(event);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	span = filter_en ? get_filter_span(event) :
282*4882a593Smuzhiyun 			   SMMU_PMCG_DEFAULT_FILTER_SPAN;
283*4882a593Smuzhiyun 	sid = filter_en ? get_filter_stream_id(event) :
284*4882a593Smuzhiyun 			   SMMU_PMCG_DEFAULT_FILTER_SID;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	cur_idx = find_first_bit(smmu_pmu->used_counters, num_ctrs);
287*4882a593Smuzhiyun 	/*
288*4882a593Smuzhiyun 	 * Per-counter filtering, or scheduling the first globally-filtered
289*4882a593Smuzhiyun 	 * event into an empty PMU so idx == 0 and it works out equivalent.
290*4882a593Smuzhiyun 	 */
291*4882a593Smuzhiyun 	if (!smmu_pmu->global_filter || cur_idx == num_ctrs) {
292*4882a593Smuzhiyun 		smmu_pmu_set_event_filter(event, idx, span, sid);
293*4882a593Smuzhiyun 		return 0;
294*4882a593Smuzhiyun 	}
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/* Otherwise, must match whatever's currently scheduled */
297*4882a593Smuzhiyun 	if (smmu_pmu_check_global_filter(smmu_pmu->events[cur_idx], event)) {
298*4882a593Smuzhiyun 		smmu_pmu_set_evtyper(smmu_pmu, idx, get_event(event));
299*4882a593Smuzhiyun 		return 0;
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	return -EAGAIN;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
smmu_pmu_get_event_idx(struct smmu_pmu * smmu_pmu,struct perf_event * event)305*4882a593Smuzhiyun static int smmu_pmu_get_event_idx(struct smmu_pmu *smmu_pmu,
306*4882a593Smuzhiyun 				  struct perf_event *event)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	int idx, err;
309*4882a593Smuzhiyun 	unsigned int num_ctrs = smmu_pmu->num_counters;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	idx = find_first_zero_bit(smmu_pmu->used_counters, num_ctrs);
312*4882a593Smuzhiyun 	if (idx == num_ctrs)
313*4882a593Smuzhiyun 		/* The counters are all in use. */
314*4882a593Smuzhiyun 		return -EAGAIN;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	err = smmu_pmu_apply_event_filter(smmu_pmu, event, idx);
317*4882a593Smuzhiyun 	if (err)
318*4882a593Smuzhiyun 		return err;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	set_bit(idx, smmu_pmu->used_counters);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	return idx;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
smmu_pmu_events_compatible(struct perf_event * curr,struct perf_event * new)325*4882a593Smuzhiyun static bool smmu_pmu_events_compatible(struct perf_event *curr,
326*4882a593Smuzhiyun 				       struct perf_event *new)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun 	if (new->pmu != curr->pmu)
329*4882a593Smuzhiyun 		return false;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	if (to_smmu_pmu(new->pmu)->global_filter &&
332*4882a593Smuzhiyun 	    !smmu_pmu_check_global_filter(curr, new))
333*4882a593Smuzhiyun 		return false;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	return true;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /*
339*4882a593Smuzhiyun  * Implementation of abstract pmu functionality required by
340*4882a593Smuzhiyun  * the core perf events code.
341*4882a593Smuzhiyun  */
342*4882a593Smuzhiyun 
smmu_pmu_event_init(struct perf_event * event)343*4882a593Smuzhiyun static int smmu_pmu_event_init(struct perf_event *event)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
346*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
347*4882a593Smuzhiyun 	struct device *dev = smmu_pmu->dev;
348*4882a593Smuzhiyun 	struct perf_event *sibling;
349*4882a593Smuzhiyun 	int group_num_events = 1;
350*4882a593Smuzhiyun 	u16 event_id;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (event->attr.type != event->pmu->type)
353*4882a593Smuzhiyun 		return -ENOENT;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	if (hwc->sample_period) {
356*4882a593Smuzhiyun 		dev_dbg(dev, "Sampling not supported\n");
357*4882a593Smuzhiyun 		return -EOPNOTSUPP;
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	if (event->cpu < 0) {
361*4882a593Smuzhiyun 		dev_dbg(dev, "Per-task mode not supported\n");
362*4882a593Smuzhiyun 		return -EOPNOTSUPP;
363*4882a593Smuzhiyun 	}
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	/* Verify specified event is supported on this PMU */
366*4882a593Smuzhiyun 	event_id = get_event(event);
367*4882a593Smuzhiyun 	if (event_id < SMMU_PMCG_ARCH_MAX_EVENTS &&
368*4882a593Smuzhiyun 	    (!test_bit(event_id, smmu_pmu->supported_events))) {
369*4882a593Smuzhiyun 		dev_dbg(dev, "Invalid event %d for this PMU\n", event_id);
370*4882a593Smuzhiyun 		return -EINVAL;
371*4882a593Smuzhiyun 	}
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	/* Don't allow groups with mixed PMUs, except for s/w events */
374*4882a593Smuzhiyun 	if (!is_software_event(event->group_leader)) {
375*4882a593Smuzhiyun 		if (!smmu_pmu_events_compatible(event->group_leader, event))
376*4882a593Smuzhiyun 			return -EINVAL;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 		if (++group_num_events > smmu_pmu->num_counters)
379*4882a593Smuzhiyun 			return -EINVAL;
380*4882a593Smuzhiyun 	}
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	for_each_sibling_event(sibling, event->group_leader) {
383*4882a593Smuzhiyun 		if (is_software_event(sibling))
384*4882a593Smuzhiyun 			continue;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 		if (!smmu_pmu_events_compatible(sibling, event))
387*4882a593Smuzhiyun 			return -EINVAL;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 		if (++group_num_events > smmu_pmu->num_counters)
390*4882a593Smuzhiyun 			return -EINVAL;
391*4882a593Smuzhiyun 	}
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	hwc->idx = -1;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	/*
396*4882a593Smuzhiyun 	 * Ensure all events are on the same cpu so all events are in the
397*4882a593Smuzhiyun 	 * same cpu context, to avoid races on pmu_enable etc.
398*4882a593Smuzhiyun 	 */
399*4882a593Smuzhiyun 	event->cpu = smmu_pmu->on_cpu;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	return 0;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
smmu_pmu_event_start(struct perf_event * event,int flags)404*4882a593Smuzhiyun static void smmu_pmu_event_start(struct perf_event *event, int flags)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
407*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
408*4882a593Smuzhiyun 	int idx = hwc->idx;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	hwc->state = 0;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	smmu_pmu_set_period(smmu_pmu, hwc);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	smmu_pmu_counter_enable(smmu_pmu, idx);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
smmu_pmu_event_stop(struct perf_event * event,int flags)417*4882a593Smuzhiyun static void smmu_pmu_event_stop(struct perf_event *event, int flags)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
420*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
421*4882a593Smuzhiyun 	int idx = hwc->idx;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (hwc->state & PERF_HES_STOPPED)
424*4882a593Smuzhiyun 		return;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	smmu_pmu_counter_disable(smmu_pmu, idx);
427*4882a593Smuzhiyun 	/* As the counter gets updated on _start, ignore PERF_EF_UPDATE */
428*4882a593Smuzhiyun 	smmu_pmu_event_update(event);
429*4882a593Smuzhiyun 	hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun 
smmu_pmu_event_add(struct perf_event * event,int flags)432*4882a593Smuzhiyun static int smmu_pmu_event_add(struct perf_event *event, int flags)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
435*4882a593Smuzhiyun 	int idx;
436*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	idx = smmu_pmu_get_event_idx(smmu_pmu, event);
439*4882a593Smuzhiyun 	if (idx < 0)
440*4882a593Smuzhiyun 		return idx;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	hwc->idx = idx;
443*4882a593Smuzhiyun 	hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
444*4882a593Smuzhiyun 	smmu_pmu->events[idx] = event;
445*4882a593Smuzhiyun 	local64_set(&hwc->prev_count, 0);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	smmu_pmu_interrupt_enable(smmu_pmu, idx);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	if (flags & PERF_EF_START)
450*4882a593Smuzhiyun 		smmu_pmu_event_start(event, flags);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/* Propagate changes to the userspace mapping. */
453*4882a593Smuzhiyun 	perf_event_update_userpage(event);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	return 0;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun 
smmu_pmu_event_del(struct perf_event * event,int flags)458*4882a593Smuzhiyun static void smmu_pmu_event_del(struct perf_event *event, int flags)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	struct hw_perf_event *hwc = &event->hw;
461*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
462*4882a593Smuzhiyun 	int idx = hwc->idx;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	smmu_pmu_event_stop(event, flags | PERF_EF_UPDATE);
465*4882a593Smuzhiyun 	smmu_pmu_interrupt_disable(smmu_pmu, idx);
466*4882a593Smuzhiyun 	smmu_pmu->events[idx] = NULL;
467*4882a593Smuzhiyun 	clear_bit(idx, smmu_pmu->used_counters);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	perf_event_update_userpage(event);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
smmu_pmu_event_read(struct perf_event * event)472*4882a593Smuzhiyun static void smmu_pmu_event_read(struct perf_event *event)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun 	smmu_pmu_event_update(event);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun /* cpumask */
478*4882a593Smuzhiyun 
smmu_pmu_cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)479*4882a593Smuzhiyun static ssize_t smmu_pmu_cpumask_show(struct device *dev,
480*4882a593Smuzhiyun 				     struct device_attribute *attr,
481*4882a593Smuzhiyun 				     char *buf)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	return cpumap_print_to_pagebuf(true, buf, cpumask_of(smmu_pmu->on_cpu));
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun static struct device_attribute smmu_pmu_cpumask_attr =
489*4882a593Smuzhiyun 		__ATTR(cpumask, 0444, smmu_pmu_cpumask_show, NULL);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun static struct attribute *smmu_pmu_cpumask_attrs[] = {
492*4882a593Smuzhiyun 	&smmu_pmu_cpumask_attr.attr,
493*4882a593Smuzhiyun 	NULL
494*4882a593Smuzhiyun };
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun static struct attribute_group smmu_pmu_cpumask_group = {
497*4882a593Smuzhiyun 	.attrs = smmu_pmu_cpumask_attrs,
498*4882a593Smuzhiyun };
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /* Events */
501*4882a593Smuzhiyun 
smmu_pmu_event_show(struct device * dev,struct device_attribute * attr,char * page)502*4882a593Smuzhiyun static ssize_t smmu_pmu_event_show(struct device *dev,
503*4882a593Smuzhiyun 				   struct device_attribute *attr, char *page)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	struct perf_pmu_events_attr *pmu_attr;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun #define SMMU_EVENT_ATTR(name, config) \
513*4882a593Smuzhiyun 	PMU_EVENT_ATTR(name, smmu_event_attr_##name, \
514*4882a593Smuzhiyun 		       config, smmu_pmu_event_show)
515*4882a593Smuzhiyun SMMU_EVENT_ATTR(cycles, 0);
516*4882a593Smuzhiyun SMMU_EVENT_ATTR(transaction, 1);
517*4882a593Smuzhiyun SMMU_EVENT_ATTR(tlb_miss, 2);
518*4882a593Smuzhiyun SMMU_EVENT_ATTR(config_cache_miss, 3);
519*4882a593Smuzhiyun SMMU_EVENT_ATTR(trans_table_walk_access, 4);
520*4882a593Smuzhiyun SMMU_EVENT_ATTR(config_struct_access, 5);
521*4882a593Smuzhiyun SMMU_EVENT_ATTR(pcie_ats_trans_rq, 6);
522*4882a593Smuzhiyun SMMU_EVENT_ATTR(pcie_ats_trans_passed, 7);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun static struct attribute *smmu_pmu_events[] = {
525*4882a593Smuzhiyun 	&smmu_event_attr_cycles.attr.attr,
526*4882a593Smuzhiyun 	&smmu_event_attr_transaction.attr.attr,
527*4882a593Smuzhiyun 	&smmu_event_attr_tlb_miss.attr.attr,
528*4882a593Smuzhiyun 	&smmu_event_attr_config_cache_miss.attr.attr,
529*4882a593Smuzhiyun 	&smmu_event_attr_trans_table_walk_access.attr.attr,
530*4882a593Smuzhiyun 	&smmu_event_attr_config_struct_access.attr.attr,
531*4882a593Smuzhiyun 	&smmu_event_attr_pcie_ats_trans_rq.attr.attr,
532*4882a593Smuzhiyun 	&smmu_event_attr_pcie_ats_trans_passed.attr.attr,
533*4882a593Smuzhiyun 	NULL
534*4882a593Smuzhiyun };
535*4882a593Smuzhiyun 
smmu_pmu_event_is_visible(struct kobject * kobj,struct attribute * attr,int unused)536*4882a593Smuzhiyun static umode_t smmu_pmu_event_is_visible(struct kobject *kobj,
537*4882a593Smuzhiyun 					 struct attribute *attr, int unused)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	struct device *dev = kobj_to_dev(kobj);
540*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
541*4882a593Smuzhiyun 	struct perf_pmu_events_attr *pmu_attr;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (test_bit(pmu_attr->id, smmu_pmu->supported_events))
546*4882a593Smuzhiyun 		return attr->mode;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	return 0;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun static struct attribute_group smmu_pmu_events_group = {
552*4882a593Smuzhiyun 	.name = "events",
553*4882a593Smuzhiyun 	.attrs = smmu_pmu_events,
554*4882a593Smuzhiyun 	.is_visible = smmu_pmu_event_is_visible,
555*4882a593Smuzhiyun };
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /* Formats */
558*4882a593Smuzhiyun PMU_FORMAT_ATTR(event,		   "config:0-15");
559*4882a593Smuzhiyun PMU_FORMAT_ATTR(filter_stream_id,  "config1:0-31");
560*4882a593Smuzhiyun PMU_FORMAT_ATTR(filter_span,	   "config1:32");
561*4882a593Smuzhiyun PMU_FORMAT_ATTR(filter_enable,	   "config1:33");
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun static struct attribute *smmu_pmu_formats[] = {
564*4882a593Smuzhiyun 	&format_attr_event.attr,
565*4882a593Smuzhiyun 	&format_attr_filter_stream_id.attr,
566*4882a593Smuzhiyun 	&format_attr_filter_span.attr,
567*4882a593Smuzhiyun 	&format_attr_filter_enable.attr,
568*4882a593Smuzhiyun 	NULL
569*4882a593Smuzhiyun };
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun static struct attribute_group smmu_pmu_format_group = {
572*4882a593Smuzhiyun 	.name = "format",
573*4882a593Smuzhiyun 	.attrs = smmu_pmu_formats,
574*4882a593Smuzhiyun };
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun static const struct attribute_group *smmu_pmu_attr_grps[] = {
577*4882a593Smuzhiyun 	&smmu_pmu_cpumask_group,
578*4882a593Smuzhiyun 	&smmu_pmu_events_group,
579*4882a593Smuzhiyun 	&smmu_pmu_format_group,
580*4882a593Smuzhiyun 	NULL
581*4882a593Smuzhiyun };
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /*
584*4882a593Smuzhiyun  * Generic device handlers
585*4882a593Smuzhiyun  */
586*4882a593Smuzhiyun 
smmu_pmu_offline_cpu(unsigned int cpu,struct hlist_node * node)587*4882a593Smuzhiyun static int smmu_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu;
590*4882a593Smuzhiyun 	unsigned int target;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	smmu_pmu = hlist_entry_safe(node, struct smmu_pmu, node);
593*4882a593Smuzhiyun 	if (cpu != smmu_pmu->on_cpu)
594*4882a593Smuzhiyun 		return 0;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	target = cpumask_any_but(cpu_online_mask, cpu);
597*4882a593Smuzhiyun 	if (target >= nr_cpu_ids)
598*4882a593Smuzhiyun 		return 0;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	perf_pmu_migrate_context(&smmu_pmu->pmu, cpu, target);
601*4882a593Smuzhiyun 	smmu_pmu->on_cpu = target;
602*4882a593Smuzhiyun 	WARN_ON(irq_set_affinity_hint(smmu_pmu->irq, cpumask_of(target)));
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	return 0;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun 
smmu_pmu_handle_irq(int irq_num,void * data)607*4882a593Smuzhiyun static irqreturn_t smmu_pmu_handle_irq(int irq_num, void *data)
608*4882a593Smuzhiyun {
609*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = data;
610*4882a593Smuzhiyun 	u64 ovsr;
611*4882a593Smuzhiyun 	unsigned int idx;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	ovsr = readq(smmu_pmu->reloc_base + SMMU_PMCG_OVSSET0);
614*4882a593Smuzhiyun 	if (!ovsr)
615*4882a593Smuzhiyun 		return IRQ_NONE;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	writeq(ovsr, smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0);
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	for_each_set_bit(idx, (unsigned long *)&ovsr, smmu_pmu->num_counters) {
620*4882a593Smuzhiyun 		struct perf_event *event = smmu_pmu->events[idx];
621*4882a593Smuzhiyun 		struct hw_perf_event *hwc;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 		if (WARN_ON_ONCE(!event))
624*4882a593Smuzhiyun 			continue;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 		smmu_pmu_event_update(event);
627*4882a593Smuzhiyun 		hwc = &event->hw;
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 		smmu_pmu_set_period(smmu_pmu, hwc);
630*4882a593Smuzhiyun 	}
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	return IRQ_HANDLED;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun 
smmu_pmu_free_msis(void * data)635*4882a593Smuzhiyun static void smmu_pmu_free_msis(void *data)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	struct device *dev = data;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	platform_msi_domain_free_irqs(dev);
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun 
smmu_pmu_write_msi_msg(struct msi_desc * desc,struct msi_msg * msg)642*4882a593Smuzhiyun static void smmu_pmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun 	phys_addr_t doorbell;
645*4882a593Smuzhiyun 	struct device *dev = msi_desc_to_dev(desc);
646*4882a593Smuzhiyun 	struct smmu_pmu *pmu = dev_get_drvdata(dev);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
649*4882a593Smuzhiyun 	doorbell &= MSI_CFG0_ADDR_MASK;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	writeq_relaxed(doorbell, pmu->reg_base + SMMU_PMCG_IRQ_CFG0);
652*4882a593Smuzhiyun 	writel_relaxed(msg->data, pmu->reg_base + SMMU_PMCG_IRQ_CFG1);
653*4882a593Smuzhiyun 	writel_relaxed(MSI_CFG2_MEMATTR_DEVICE_nGnRE,
654*4882a593Smuzhiyun 		       pmu->reg_base + SMMU_PMCG_IRQ_CFG2);
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun 
smmu_pmu_setup_msi(struct smmu_pmu * pmu)657*4882a593Smuzhiyun static void smmu_pmu_setup_msi(struct smmu_pmu *pmu)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun 	struct msi_desc *desc;
660*4882a593Smuzhiyun 	struct device *dev = pmu->dev;
661*4882a593Smuzhiyun 	int ret;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	/* Clear MSI address reg */
664*4882a593Smuzhiyun 	writeq_relaxed(0, pmu->reg_base + SMMU_PMCG_IRQ_CFG0);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	/* MSI supported or not */
667*4882a593Smuzhiyun 	if (!(readl(pmu->reg_base + SMMU_PMCG_CFGR) & SMMU_PMCG_CFGR_MSI))
668*4882a593Smuzhiyun 		return;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	ret = platform_msi_domain_alloc_irqs(dev, 1, smmu_pmu_write_msi_msg);
671*4882a593Smuzhiyun 	if (ret) {
672*4882a593Smuzhiyun 		dev_warn(dev, "failed to allocate MSIs\n");
673*4882a593Smuzhiyun 		return;
674*4882a593Smuzhiyun 	}
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	desc = first_msi_entry(dev);
677*4882a593Smuzhiyun 	if (desc)
678*4882a593Smuzhiyun 		pmu->irq = desc->irq;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	/* Add callback to free MSIs on teardown */
681*4882a593Smuzhiyun 	devm_add_action(dev, smmu_pmu_free_msis, dev);
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
smmu_pmu_setup_irq(struct smmu_pmu * pmu)684*4882a593Smuzhiyun static int smmu_pmu_setup_irq(struct smmu_pmu *pmu)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	unsigned long flags = IRQF_NOBALANCING | IRQF_SHARED | IRQF_NO_THREAD;
687*4882a593Smuzhiyun 	int irq, ret = -ENXIO;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	smmu_pmu_setup_msi(pmu);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	irq = pmu->irq;
692*4882a593Smuzhiyun 	if (irq)
693*4882a593Smuzhiyun 		ret = devm_request_irq(pmu->dev, irq, smmu_pmu_handle_irq,
694*4882a593Smuzhiyun 				       flags, "smmuv3-pmu", pmu);
695*4882a593Smuzhiyun 	return ret;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun 
smmu_pmu_reset(struct smmu_pmu * smmu_pmu)698*4882a593Smuzhiyun static void smmu_pmu_reset(struct smmu_pmu *smmu_pmu)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun 	u64 counter_present_mask = GENMASK_ULL(smmu_pmu->num_counters - 1, 0);
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	smmu_pmu_disable(&smmu_pmu->pmu);
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	/* Disable counter and interrupt */
705*4882a593Smuzhiyun 	writeq_relaxed(counter_present_mask,
706*4882a593Smuzhiyun 		       smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0);
707*4882a593Smuzhiyun 	writeq_relaxed(counter_present_mask,
708*4882a593Smuzhiyun 		       smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0);
709*4882a593Smuzhiyun 	writeq_relaxed(counter_present_mask,
710*4882a593Smuzhiyun 		       smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0);
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun 
smmu_pmu_get_acpi_options(struct smmu_pmu * smmu_pmu)713*4882a593Smuzhiyun static void smmu_pmu_get_acpi_options(struct smmu_pmu *smmu_pmu)
714*4882a593Smuzhiyun {
715*4882a593Smuzhiyun 	u32 model;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	model = *(u32 *)dev_get_platdata(smmu_pmu->dev);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	switch (model) {
720*4882a593Smuzhiyun 	case IORT_SMMU_V3_PMCG_HISI_HIP08:
721*4882a593Smuzhiyun 		/* HiSilicon Erratum 162001800 */
722*4882a593Smuzhiyun 		smmu_pmu->options |= SMMU_PMCG_EVCNTR_RDONLY;
723*4882a593Smuzhiyun 		break;
724*4882a593Smuzhiyun 	}
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	dev_notice(smmu_pmu->dev, "option mask 0x%x\n", smmu_pmu->options);
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun 
smmu_pmu_probe(struct platform_device * pdev)729*4882a593Smuzhiyun static int smmu_pmu_probe(struct platform_device *pdev)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu;
732*4882a593Smuzhiyun 	struct resource *res_0;
733*4882a593Smuzhiyun 	u32 cfgr, reg_size;
734*4882a593Smuzhiyun 	u64 ceid_64[2];
735*4882a593Smuzhiyun 	int irq, err;
736*4882a593Smuzhiyun 	char *name;
737*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	smmu_pmu = devm_kzalloc(dev, sizeof(*smmu_pmu), GFP_KERNEL);
740*4882a593Smuzhiyun 	if (!smmu_pmu)
741*4882a593Smuzhiyun 		return -ENOMEM;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	smmu_pmu->dev = dev;
744*4882a593Smuzhiyun 	platform_set_drvdata(pdev, smmu_pmu);
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	smmu_pmu->pmu = (struct pmu) {
747*4882a593Smuzhiyun 		.module		= THIS_MODULE,
748*4882a593Smuzhiyun 		.task_ctx_nr    = perf_invalid_context,
749*4882a593Smuzhiyun 		.pmu_enable	= smmu_pmu_enable,
750*4882a593Smuzhiyun 		.pmu_disable	= smmu_pmu_disable,
751*4882a593Smuzhiyun 		.event_init	= smmu_pmu_event_init,
752*4882a593Smuzhiyun 		.add		= smmu_pmu_event_add,
753*4882a593Smuzhiyun 		.del		= smmu_pmu_event_del,
754*4882a593Smuzhiyun 		.start		= smmu_pmu_event_start,
755*4882a593Smuzhiyun 		.stop		= smmu_pmu_event_stop,
756*4882a593Smuzhiyun 		.read		= smmu_pmu_event_read,
757*4882a593Smuzhiyun 		.attr_groups	= smmu_pmu_attr_grps,
758*4882a593Smuzhiyun 		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
759*4882a593Smuzhiyun 	};
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	smmu_pmu->reg_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res_0);
762*4882a593Smuzhiyun 	if (IS_ERR(smmu_pmu->reg_base))
763*4882a593Smuzhiyun 		return PTR_ERR(smmu_pmu->reg_base);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	cfgr = readl_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CFGR);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	/* Determine if page 1 is present */
768*4882a593Smuzhiyun 	if (cfgr & SMMU_PMCG_CFGR_RELOC_CTRS) {
769*4882a593Smuzhiyun 		smmu_pmu->reloc_base = devm_platform_ioremap_resource(pdev, 1);
770*4882a593Smuzhiyun 		if (IS_ERR(smmu_pmu->reloc_base))
771*4882a593Smuzhiyun 			return PTR_ERR(smmu_pmu->reloc_base);
772*4882a593Smuzhiyun 	} else {
773*4882a593Smuzhiyun 		smmu_pmu->reloc_base = smmu_pmu->reg_base;
774*4882a593Smuzhiyun 	}
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	irq = platform_get_irq_optional(pdev, 0);
777*4882a593Smuzhiyun 	if (irq > 0)
778*4882a593Smuzhiyun 		smmu_pmu->irq = irq;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	ceid_64[0] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID0);
781*4882a593Smuzhiyun 	ceid_64[1] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID1);
782*4882a593Smuzhiyun 	bitmap_from_arr32(smmu_pmu->supported_events, (u32 *)ceid_64,
783*4882a593Smuzhiyun 			  SMMU_PMCG_ARCH_MAX_EVENTS);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	smmu_pmu->num_counters = FIELD_GET(SMMU_PMCG_CFGR_NCTR, cfgr) + 1;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	smmu_pmu->global_filter = !!(cfgr & SMMU_PMCG_CFGR_SID_FILTER_TYPE);
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	reg_size = FIELD_GET(SMMU_PMCG_CFGR_SIZE, cfgr);
790*4882a593Smuzhiyun 	smmu_pmu->counter_mask = GENMASK_ULL(reg_size, 0);
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	smmu_pmu_reset(smmu_pmu);
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	err = smmu_pmu_setup_irq(smmu_pmu);
795*4882a593Smuzhiyun 	if (err) {
796*4882a593Smuzhiyun 		dev_err(dev, "Setup irq failed, PMU @%pa\n", &res_0->start);
797*4882a593Smuzhiyun 		return err;
798*4882a593Smuzhiyun 	}
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "smmuv3_pmcg_%llx",
801*4882a593Smuzhiyun 			      (res_0->start) >> SMMU_PMCG_PA_SHIFT);
802*4882a593Smuzhiyun 	if (!name) {
803*4882a593Smuzhiyun 		dev_err(dev, "Create name failed, PMU @%pa\n", &res_0->start);
804*4882a593Smuzhiyun 		return -EINVAL;
805*4882a593Smuzhiyun 	}
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	smmu_pmu_get_acpi_options(smmu_pmu);
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	/* Pick one CPU to be the preferred one to use */
810*4882a593Smuzhiyun 	smmu_pmu->on_cpu = raw_smp_processor_id();
811*4882a593Smuzhiyun 	WARN_ON(irq_set_affinity_hint(smmu_pmu->irq,
812*4882a593Smuzhiyun 				      cpumask_of(smmu_pmu->on_cpu)));
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	err = cpuhp_state_add_instance_nocalls(cpuhp_state_num,
815*4882a593Smuzhiyun 					       &smmu_pmu->node);
816*4882a593Smuzhiyun 	if (err) {
817*4882a593Smuzhiyun 		dev_err(dev, "Error %d registering hotplug, PMU @%pa\n",
818*4882a593Smuzhiyun 			err, &res_0->start);
819*4882a593Smuzhiyun 		goto out_clear_affinity;
820*4882a593Smuzhiyun 	}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	err = perf_pmu_register(&smmu_pmu->pmu, name, -1);
823*4882a593Smuzhiyun 	if (err) {
824*4882a593Smuzhiyun 		dev_err(dev, "Error %d registering PMU @%pa\n",
825*4882a593Smuzhiyun 			err, &res_0->start);
826*4882a593Smuzhiyun 		goto out_unregister;
827*4882a593Smuzhiyun 	}
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	dev_info(dev, "Registered PMU @ %pa using %d counters with %s filter settings\n",
830*4882a593Smuzhiyun 		 &res_0->start, smmu_pmu->num_counters,
831*4882a593Smuzhiyun 		 smmu_pmu->global_filter ? "Global(Counter0)" :
832*4882a593Smuzhiyun 		 "Individual");
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	return 0;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun out_unregister:
837*4882a593Smuzhiyun 	cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node);
838*4882a593Smuzhiyun out_clear_affinity:
839*4882a593Smuzhiyun 	irq_set_affinity_hint(smmu_pmu->irq, NULL);
840*4882a593Smuzhiyun 	return err;
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun 
smmu_pmu_remove(struct platform_device * pdev)843*4882a593Smuzhiyun static int smmu_pmu_remove(struct platform_device *pdev)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev);
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	perf_pmu_unregister(&smmu_pmu->pmu);
848*4882a593Smuzhiyun 	cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node);
849*4882a593Smuzhiyun 	irq_set_affinity_hint(smmu_pmu->irq, NULL);
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	return 0;
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun 
smmu_pmu_shutdown(struct platform_device * pdev)854*4882a593Smuzhiyun static void smmu_pmu_shutdown(struct platform_device *pdev)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun 	struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev);
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	smmu_pmu_disable(&smmu_pmu->pmu);
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun static struct platform_driver smmu_pmu_driver = {
862*4882a593Smuzhiyun 	.driver = {
863*4882a593Smuzhiyun 		.name = "arm-smmu-v3-pmcg",
864*4882a593Smuzhiyun 		.suppress_bind_attrs = true,
865*4882a593Smuzhiyun 	},
866*4882a593Smuzhiyun 	.probe = smmu_pmu_probe,
867*4882a593Smuzhiyun 	.remove = smmu_pmu_remove,
868*4882a593Smuzhiyun 	.shutdown = smmu_pmu_shutdown,
869*4882a593Smuzhiyun };
870*4882a593Smuzhiyun 
arm_smmu_pmu_init(void)871*4882a593Smuzhiyun static int __init arm_smmu_pmu_init(void)
872*4882a593Smuzhiyun {
873*4882a593Smuzhiyun 	cpuhp_state_num = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
874*4882a593Smuzhiyun 						  "perf/arm/pmcg:online",
875*4882a593Smuzhiyun 						  NULL,
876*4882a593Smuzhiyun 						  smmu_pmu_offline_cpu);
877*4882a593Smuzhiyun 	if (cpuhp_state_num < 0)
878*4882a593Smuzhiyun 		return cpuhp_state_num;
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	return platform_driver_register(&smmu_pmu_driver);
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun module_init(arm_smmu_pmu_init);
883*4882a593Smuzhiyun 
arm_smmu_pmu_exit(void)884*4882a593Smuzhiyun static void __exit arm_smmu_pmu_exit(void)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun 	platform_driver_unregister(&smmu_pmu_driver);
887*4882a593Smuzhiyun 	cpuhp_remove_multi_state(cpuhp_state_num);
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun module_exit(arm_smmu_pmu_exit);
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun MODULE_DESCRIPTION("PMU driver for ARM SMMUv3 Performance Monitors Extension");
893*4882a593Smuzhiyun MODULE_AUTHOR("Neil Leeder <nleeder@codeaurora.org>");
894*4882a593Smuzhiyun MODULE_AUTHOR("Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>");
895*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
896