xref: /linux/drivers/perf/arm-ccn.c (revision 1888d3ddc3d6a2511be86045cfb2e7ea5fc67c44)
1*1888d3ddSRobin Murphy /*
2*1888d3ddSRobin Murphy  * This program is free software; you can redistribute it and/or modify
3*1888d3ddSRobin Murphy  * it under the terms of the GNU General Public License version 2 as
4*1888d3ddSRobin Murphy  * published by the Free Software Foundation.
5*1888d3ddSRobin Murphy  *
6*1888d3ddSRobin Murphy  * This program is distributed in the hope that it will be useful,
7*1888d3ddSRobin Murphy  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8*1888d3ddSRobin Murphy  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9*1888d3ddSRobin Murphy  * GNU General Public License for more details.
10*1888d3ddSRobin Murphy  *
11*1888d3ddSRobin Murphy  * Copyright (C) 2014 ARM Limited
12*1888d3ddSRobin Murphy  */
13*1888d3ddSRobin Murphy 
14*1888d3ddSRobin Murphy #include <linux/ctype.h>
15*1888d3ddSRobin Murphy #include <linux/hrtimer.h>
16*1888d3ddSRobin Murphy #include <linux/idr.h>
17*1888d3ddSRobin Murphy #include <linux/interrupt.h>
18*1888d3ddSRobin Murphy #include <linux/io.h>
19*1888d3ddSRobin Murphy #include <linux/module.h>
20*1888d3ddSRobin Murphy #include <linux/perf_event.h>
21*1888d3ddSRobin Murphy #include <linux/platform_device.h>
22*1888d3ddSRobin Murphy #include <linux/slab.h>
23*1888d3ddSRobin Murphy 
24*1888d3ddSRobin Murphy #define CCN_NUM_XP_PORTS 2
25*1888d3ddSRobin Murphy #define CCN_NUM_VCS 4
26*1888d3ddSRobin Murphy #define CCN_NUM_REGIONS	256
27*1888d3ddSRobin Murphy #define CCN_REGION_SIZE	0x10000
28*1888d3ddSRobin Murphy 
29*1888d3ddSRobin Murphy #define CCN_ALL_OLY_ID			0xff00
30*1888d3ddSRobin Murphy #define CCN_ALL_OLY_ID__OLY_ID__SHIFT			0
31*1888d3ddSRobin Murphy #define CCN_ALL_OLY_ID__OLY_ID__MASK			0x1f
32*1888d3ddSRobin Murphy #define CCN_ALL_OLY_ID__NODE_ID__SHIFT			8
33*1888d3ddSRobin Murphy #define CCN_ALL_OLY_ID__NODE_ID__MASK			0x3f
34*1888d3ddSRobin Murphy 
35*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS		0x0008
36*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__INTREQ__DESSERT		0x11
37*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__ENABLE	0x02
38*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLED	0x20
39*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE	0x22
40*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_ENABLE	0x04
41*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLED	0x40
42*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLE	0x44
43*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE	0x08
44*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED	0x80
45*1888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE	0x88
46*1888d3ddSRobin Murphy #define CCN_MN_OLY_COMP_LIST_63_0	0x01e0
47*1888d3ddSRobin Murphy #define CCN_MN_ERR_SIG_VAL_63_0		0x0300
48*1888d3ddSRobin Murphy #define CCN_MN_ERR_SIG_VAL_63_0__DT			(1 << 1)
49*1888d3ddSRobin Murphy 
50*1888d3ddSRobin Murphy #define CCN_DT_ACTIVE_DSM		0x0000
51*1888d3ddSRobin Murphy #define CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(n)		((n) * 8)
52*1888d3ddSRobin Murphy #define CCN_DT_ACTIVE_DSM__DSM_ID__MASK			0xff
53*1888d3ddSRobin Murphy #define CCN_DT_CTL			0x0028
54*1888d3ddSRobin Murphy #define CCN_DT_CTL__DT_EN				(1 << 0)
55*1888d3ddSRobin Murphy #define CCN_DT_PMEVCNT(n)		(0x0100 + (n) * 0x8)
56*1888d3ddSRobin Murphy #define CCN_DT_PMCCNTR			0x0140
57*1888d3ddSRobin Murphy #define CCN_DT_PMCCNTRSR		0x0190
58*1888d3ddSRobin Murphy #define CCN_DT_PMOVSR			0x0198
59*1888d3ddSRobin Murphy #define CCN_DT_PMOVSR_CLR		0x01a0
60*1888d3ddSRobin Murphy #define CCN_DT_PMOVSR_CLR__MASK				0x1f
61*1888d3ddSRobin Murphy #define CCN_DT_PMCR			0x01a8
62*1888d3ddSRobin Murphy #define CCN_DT_PMCR__OVFL_INTR_EN			(1 << 6)
63*1888d3ddSRobin Murphy #define CCN_DT_PMCR__PMU_EN				(1 << 0)
64*1888d3ddSRobin Murphy #define CCN_DT_PMSR			0x01b0
65*1888d3ddSRobin Murphy #define CCN_DT_PMSR_REQ			0x01b8
66*1888d3ddSRobin Murphy #define CCN_DT_PMSR_CLR			0x01c0
67*1888d3ddSRobin Murphy 
68*1888d3ddSRobin Murphy #define CCN_HNF_PMU_EVENT_SEL		0x0600
69*1888d3ddSRobin Murphy #define CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
70*1888d3ddSRobin Murphy #define CCN_HNF_PMU_EVENT_SEL__ID__MASK			0xf
71*1888d3ddSRobin Murphy 
72*1888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG		0x0300
73*1888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__SHIFT(n)		((n) * 4)
74*1888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__MASK			0xf
75*1888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH		0x0
76*1888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT_0_OR_1	0x1
77*1888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(n)		(0x2 + (n))
78*1888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(n)	(0x4 + (n))
79*1888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(d, n) (0x8 + (d) * 4 + (n))
80*1888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL		0x0308
81*1888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(n)	(0 + (n) * 8)
82*1888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK	0x1
83*1888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(n)	(1 + (n) * 8)
84*1888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK	0x1
85*1888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(n)	(2 + (n) * 8)
86*1888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK	0x3
87*1888d3ddSRobin Murphy #define CCN_XP_DT_CMP_VAL_L(n)		(0x0310 + (n) * 0x40)
88*1888d3ddSRobin Murphy #define CCN_XP_DT_CMP_VAL_H(n)		(0x0318 + (n) * 0x40)
89*1888d3ddSRobin Murphy #define CCN_XP_DT_CMP_MASK_L(n)		(0x0320 + (n) * 0x40)
90*1888d3ddSRobin Murphy #define CCN_XP_DT_CMP_MASK_H(n)		(0x0328 + (n) * 0x40)
91*1888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL		0x0370
92*1888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL__DT_ENABLE			(1 << 0)
93*1888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(n)		(12 + (n) * 4)
94*1888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL__WP_ARM_SEL__MASK		0xf
95*1888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS		0xf
96*1888d3ddSRobin Murphy #define CCN_XP_PMU_EVENT_SEL		0x0600
97*1888d3ddSRobin Murphy #define CCN_XP_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 7)
98*1888d3ddSRobin Murphy #define CCN_XP_PMU_EVENT_SEL__ID__MASK			0x3f
99*1888d3ddSRobin Murphy 
100*1888d3ddSRobin Murphy #define CCN_SBAS_PMU_EVENT_SEL		0x0600
101*1888d3ddSRobin Murphy #define CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
102*1888d3ddSRobin Murphy #define CCN_SBAS_PMU_EVENT_SEL__ID__MASK		0xf
103*1888d3ddSRobin Murphy 
104*1888d3ddSRobin Murphy #define CCN_RNI_PMU_EVENT_SEL		0x0600
105*1888d3ddSRobin Murphy #define CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
106*1888d3ddSRobin Murphy #define CCN_RNI_PMU_EVENT_SEL__ID__MASK			0xf
107*1888d3ddSRobin Murphy 
108*1888d3ddSRobin Murphy #define CCN_TYPE_MN	0x01
109*1888d3ddSRobin Murphy #define CCN_TYPE_DT	0x02
110*1888d3ddSRobin Murphy #define CCN_TYPE_HNF	0x04
111*1888d3ddSRobin Murphy #define CCN_TYPE_HNI	0x05
112*1888d3ddSRobin Murphy #define CCN_TYPE_XP	0x08
113*1888d3ddSRobin Murphy #define CCN_TYPE_SBSX	0x0c
114*1888d3ddSRobin Murphy #define CCN_TYPE_SBAS	0x10
115*1888d3ddSRobin Murphy #define CCN_TYPE_RNI_1P	0x14
116*1888d3ddSRobin Murphy #define CCN_TYPE_RNI_2P	0x15
117*1888d3ddSRobin Murphy #define CCN_TYPE_RNI_3P	0x16
118*1888d3ddSRobin Murphy #define CCN_TYPE_RND_1P	0x18 /* RN-D = RN-I + DVM */
119*1888d3ddSRobin Murphy #define CCN_TYPE_RND_2P	0x19
120*1888d3ddSRobin Murphy #define CCN_TYPE_RND_3P	0x1a
121*1888d3ddSRobin Murphy #define CCN_TYPE_CYCLES	0xff /* Pseudotype */
122*1888d3ddSRobin Murphy 
123*1888d3ddSRobin Murphy #define CCN_EVENT_WATCHPOINT 0xfe /* Pseudoevent */
124*1888d3ddSRobin Murphy 
125*1888d3ddSRobin Murphy #define CCN_NUM_PMU_EVENTS		4
126*1888d3ddSRobin Murphy #define CCN_NUM_XP_WATCHPOINTS		2 /* See DT.dbg_id.num_watchpoints */
127*1888d3ddSRobin Murphy #define CCN_NUM_PMU_EVENT_COUNTERS	8 /* See DT.dbg_id.num_pmucntr */
128*1888d3ddSRobin Murphy #define CCN_IDX_PMU_CYCLE_COUNTER	CCN_NUM_PMU_EVENT_COUNTERS
129*1888d3ddSRobin Murphy 
130*1888d3ddSRobin Murphy #define CCN_NUM_PREDEFINED_MASKS	4
131*1888d3ddSRobin Murphy #define CCN_IDX_MASK_ANY		(CCN_NUM_PMU_EVENT_COUNTERS + 0)
132*1888d3ddSRobin Murphy #define CCN_IDX_MASK_EXACT		(CCN_NUM_PMU_EVENT_COUNTERS + 1)
133*1888d3ddSRobin Murphy #define CCN_IDX_MASK_ORDER		(CCN_NUM_PMU_EVENT_COUNTERS + 2)
134*1888d3ddSRobin Murphy #define CCN_IDX_MASK_OPCODE		(CCN_NUM_PMU_EVENT_COUNTERS + 3)
135*1888d3ddSRobin Murphy 
136*1888d3ddSRobin Murphy struct arm_ccn_component {
137*1888d3ddSRobin Murphy 	void __iomem *base;
138*1888d3ddSRobin Murphy 	u32 type;
139*1888d3ddSRobin Murphy 
140*1888d3ddSRobin Murphy 	DECLARE_BITMAP(pmu_events_mask, CCN_NUM_PMU_EVENTS);
141*1888d3ddSRobin Murphy 	union {
142*1888d3ddSRobin Murphy 		struct {
143*1888d3ddSRobin Murphy 			DECLARE_BITMAP(dt_cmp_mask, CCN_NUM_XP_WATCHPOINTS);
144*1888d3ddSRobin Murphy 		} xp;
145*1888d3ddSRobin Murphy 	};
146*1888d3ddSRobin Murphy };
147*1888d3ddSRobin Murphy 
148*1888d3ddSRobin Murphy #define pmu_to_arm_ccn(_pmu) container_of(container_of(_pmu, \
149*1888d3ddSRobin Murphy 	struct arm_ccn_dt, pmu), struct arm_ccn, dt)
150*1888d3ddSRobin Murphy 
151*1888d3ddSRobin Murphy struct arm_ccn_dt {
152*1888d3ddSRobin Murphy 	int id;
153*1888d3ddSRobin Murphy 	void __iomem *base;
154*1888d3ddSRobin Murphy 
155*1888d3ddSRobin Murphy 	spinlock_t config_lock;
156*1888d3ddSRobin Murphy 
157*1888d3ddSRobin Murphy 	DECLARE_BITMAP(pmu_counters_mask, CCN_NUM_PMU_EVENT_COUNTERS + 1);
158*1888d3ddSRobin Murphy 	struct {
159*1888d3ddSRobin Murphy 		struct arm_ccn_component *source;
160*1888d3ddSRobin Murphy 		struct perf_event *event;
161*1888d3ddSRobin Murphy 	} pmu_counters[CCN_NUM_PMU_EVENT_COUNTERS + 1];
162*1888d3ddSRobin Murphy 
163*1888d3ddSRobin Murphy 	struct {
164*1888d3ddSRobin Murphy 	       u64 l, h;
165*1888d3ddSRobin Murphy 	} cmp_mask[CCN_NUM_PMU_EVENT_COUNTERS + CCN_NUM_PREDEFINED_MASKS];
166*1888d3ddSRobin Murphy 
167*1888d3ddSRobin Murphy 	struct hrtimer hrtimer;
168*1888d3ddSRobin Murphy 
169*1888d3ddSRobin Murphy 	cpumask_t cpu;
170*1888d3ddSRobin Murphy 	struct hlist_node node;
171*1888d3ddSRobin Murphy 
172*1888d3ddSRobin Murphy 	struct pmu pmu;
173*1888d3ddSRobin Murphy };
174*1888d3ddSRobin Murphy 
175*1888d3ddSRobin Murphy struct arm_ccn {
176*1888d3ddSRobin Murphy 	struct device *dev;
177*1888d3ddSRobin Murphy 	void __iomem *base;
178*1888d3ddSRobin Murphy 	unsigned int irq;
179*1888d3ddSRobin Murphy 
180*1888d3ddSRobin Murphy 	unsigned sbas_present:1;
181*1888d3ddSRobin Murphy 	unsigned sbsx_present:1;
182*1888d3ddSRobin Murphy 
183*1888d3ddSRobin Murphy 	int num_nodes;
184*1888d3ddSRobin Murphy 	struct arm_ccn_component *node;
185*1888d3ddSRobin Murphy 
186*1888d3ddSRobin Murphy 	int num_xps;
187*1888d3ddSRobin Murphy 	struct arm_ccn_component *xp;
188*1888d3ddSRobin Murphy 
189*1888d3ddSRobin Murphy 	struct arm_ccn_dt dt;
190*1888d3ddSRobin Murphy 	int mn_id;
191*1888d3ddSRobin Murphy };
192*1888d3ddSRobin Murphy 
193*1888d3ddSRobin Murphy static int arm_ccn_node_to_xp(int node)
194*1888d3ddSRobin Murphy {
195*1888d3ddSRobin Murphy 	return node / CCN_NUM_XP_PORTS;
196*1888d3ddSRobin Murphy }
197*1888d3ddSRobin Murphy 
198*1888d3ddSRobin Murphy static int arm_ccn_node_to_xp_port(int node)
199*1888d3ddSRobin Murphy {
200*1888d3ddSRobin Murphy 	return node % CCN_NUM_XP_PORTS;
201*1888d3ddSRobin Murphy }
202*1888d3ddSRobin Murphy 
203*1888d3ddSRobin Murphy 
204*1888d3ddSRobin Murphy /*
205*1888d3ddSRobin Murphy  * Bit shifts and masks in these defines must be kept in sync with
206*1888d3ddSRobin Murphy  * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
207*1888d3ddSRobin Murphy  */
208*1888d3ddSRobin Murphy #define CCN_CONFIG_NODE(_config)	(((_config) >> 0) & 0xff)
209*1888d3ddSRobin Murphy #define CCN_CONFIG_XP(_config)		(((_config) >> 0) & 0xff)
210*1888d3ddSRobin Murphy #define CCN_CONFIG_TYPE(_config)	(((_config) >> 8) & 0xff)
211*1888d3ddSRobin Murphy #define CCN_CONFIG_EVENT(_config)	(((_config) >> 16) & 0xff)
212*1888d3ddSRobin Murphy #define CCN_CONFIG_PORT(_config)	(((_config) >> 24) & 0x3)
213*1888d3ddSRobin Murphy #define CCN_CONFIG_BUS(_config)		(((_config) >> 24) & 0x3)
214*1888d3ddSRobin Murphy #define CCN_CONFIG_VC(_config)		(((_config) >> 26) & 0x7)
215*1888d3ddSRobin Murphy #define CCN_CONFIG_DIR(_config)		(((_config) >> 29) & 0x1)
216*1888d3ddSRobin Murphy #define CCN_CONFIG_MASK(_config)	(((_config) >> 30) & 0xf)
217*1888d3ddSRobin Murphy 
218*1888d3ddSRobin Murphy static void arm_ccn_pmu_config_set(u64 *config, u32 node_xp, u32 type, u32 port)
219*1888d3ddSRobin Murphy {
220*1888d3ddSRobin Murphy 	*config &= ~((0xff << 0) | (0xff << 8) | (0x3 << 24));
221*1888d3ddSRobin Murphy 	*config |= (node_xp << 0) | (type << 8) | (port << 24);
222*1888d3ddSRobin Murphy }
223*1888d3ddSRobin Murphy 
224*1888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_format_show(struct device *dev,
225*1888d3ddSRobin Murphy 		struct device_attribute *attr, char *buf)
226*1888d3ddSRobin Murphy {
227*1888d3ddSRobin Murphy 	struct dev_ext_attribute *ea = container_of(attr,
228*1888d3ddSRobin Murphy 			struct dev_ext_attribute, attr);
229*1888d3ddSRobin Murphy 
230*1888d3ddSRobin Murphy 	return snprintf(buf, PAGE_SIZE, "%s\n", (char *)ea->var);
231*1888d3ddSRobin Murphy }
232*1888d3ddSRobin Murphy 
233*1888d3ddSRobin Murphy #define CCN_FORMAT_ATTR(_name, _config) \
234*1888d3ddSRobin Murphy 	struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
235*1888d3ddSRobin Murphy 			{ __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
236*1888d3ddSRobin Murphy 			NULL), _config }
237*1888d3ddSRobin Murphy 
238*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(node, "config:0-7");
239*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(xp, "config:0-7");
240*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(type, "config:8-15");
241*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(event, "config:16-23");
242*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(port, "config:24-25");
243*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(bus, "config:24-25");
244*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(vc, "config:26-28");
245*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(dir, "config:29-29");
246*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(mask, "config:30-33");
247*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(cmp_l, "config1:0-62");
248*1888d3ddSRobin Murphy static CCN_FORMAT_ATTR(cmp_h, "config2:0-59");
249*1888d3ddSRobin Murphy 
250*1888d3ddSRobin Murphy static struct attribute *arm_ccn_pmu_format_attrs[] = {
251*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_node.attr.attr,
252*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_xp.attr.attr,
253*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_type.attr.attr,
254*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_event.attr.attr,
255*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_port.attr.attr,
256*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_bus.attr.attr,
257*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_vc.attr.attr,
258*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_dir.attr.attr,
259*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_mask.attr.attr,
260*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_cmp_l.attr.attr,
261*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_cmp_h.attr.attr,
262*1888d3ddSRobin Murphy 	NULL
263*1888d3ddSRobin Murphy };
264*1888d3ddSRobin Murphy 
265*1888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_format_attr_group = {
266*1888d3ddSRobin Murphy 	.name = "format",
267*1888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_format_attrs,
268*1888d3ddSRobin Murphy };
269*1888d3ddSRobin Murphy 
270*1888d3ddSRobin Murphy 
271*1888d3ddSRobin Murphy struct arm_ccn_pmu_event {
272*1888d3ddSRobin Murphy 	struct device_attribute attr;
273*1888d3ddSRobin Murphy 	u32 type;
274*1888d3ddSRobin Murphy 	u32 event;
275*1888d3ddSRobin Murphy 	int num_ports;
276*1888d3ddSRobin Murphy 	int num_vcs;
277*1888d3ddSRobin Murphy 	const char *def;
278*1888d3ddSRobin Murphy 	int mask;
279*1888d3ddSRobin Murphy };
280*1888d3ddSRobin Murphy 
281*1888d3ddSRobin Murphy #define CCN_EVENT_ATTR(_name) \
282*1888d3ddSRobin Murphy 	__ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
283*1888d3ddSRobin Murphy 
284*1888d3ddSRobin Murphy /*
285*1888d3ddSRobin Murphy  * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
286*1888d3ddSRobin Murphy  * their ports in XP they are connected to. For the sake of usability they are
287*1888d3ddSRobin Murphy  * explicitly defined here (and translated into a relevant watchpoint in
288*1888d3ddSRobin Murphy  * arm_ccn_pmu_event_init()) so the user can easily request them without deep
289*1888d3ddSRobin Murphy  * knowledge of the flit format.
290*1888d3ddSRobin Murphy  */
291*1888d3ddSRobin Murphy 
292*1888d3ddSRobin Murphy #define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
293*1888d3ddSRobin Murphy 		.type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
294*1888d3ddSRobin Murphy 		.num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
295*1888d3ddSRobin Murphy 		.def = _def, .mask = _mask, }
296*1888d3ddSRobin Murphy 
297*1888d3ddSRobin Murphy #define CCN_EVENT_HNI(_name, _def, _mask) { \
298*1888d3ddSRobin Murphy 		.attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
299*1888d3ddSRobin Murphy 		.event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
300*1888d3ddSRobin Murphy 		.num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
301*1888d3ddSRobin Murphy 
302*1888d3ddSRobin Murphy #define CCN_EVENT_SBSX(_name, _def, _mask) { \
303*1888d3ddSRobin Murphy 		.attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
304*1888d3ddSRobin Murphy 		.event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
305*1888d3ddSRobin Murphy 		.num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
306*1888d3ddSRobin Murphy 
307*1888d3ddSRobin Murphy #define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
308*1888d3ddSRobin Murphy 		.type = CCN_TYPE_HNF, .event = _event, }
309*1888d3ddSRobin Murphy 
310*1888d3ddSRobin Murphy #define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
311*1888d3ddSRobin Murphy 		.type = CCN_TYPE_XP, .event = _event, \
312*1888d3ddSRobin Murphy 		.num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
313*1888d3ddSRobin Murphy 
314*1888d3ddSRobin Murphy /*
315*1888d3ddSRobin Murphy  * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
316*1888d3ddSRobin Murphy  * on configuration. One of them is picked to represent the whole group,
317*1888d3ddSRobin Murphy  * as they all share the same event types.
318*1888d3ddSRobin Murphy  */
319*1888d3ddSRobin Murphy #define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
320*1888d3ddSRobin Murphy 		.type = CCN_TYPE_RNI_3P, .event = _event, }
321*1888d3ddSRobin Murphy 
322*1888d3ddSRobin Murphy #define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
323*1888d3ddSRobin Murphy 		.type = CCN_TYPE_SBAS, .event = _event, }
324*1888d3ddSRobin Murphy 
325*1888d3ddSRobin Murphy #define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
326*1888d3ddSRobin Murphy 		.type = CCN_TYPE_CYCLES }
327*1888d3ddSRobin Murphy 
328*1888d3ddSRobin Murphy 
329*1888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_event_show(struct device *dev,
330*1888d3ddSRobin Murphy 		struct device_attribute *attr, char *buf)
331*1888d3ddSRobin Murphy {
332*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
333*1888d3ddSRobin Murphy 	struct arm_ccn_pmu_event *event = container_of(attr,
334*1888d3ddSRobin Murphy 			struct arm_ccn_pmu_event, attr);
335*1888d3ddSRobin Murphy 	ssize_t res;
336*1888d3ddSRobin Murphy 
337*1888d3ddSRobin Murphy 	res = snprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
338*1888d3ddSRobin Murphy 	if (event->event)
339*1888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
340*1888d3ddSRobin Murphy 				event->event);
341*1888d3ddSRobin Murphy 	if (event->def)
342*1888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",%s",
343*1888d3ddSRobin Murphy 				event->def);
344*1888d3ddSRobin Murphy 	if (event->mask)
345*1888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
346*1888d3ddSRobin Murphy 				event->mask);
347*1888d3ddSRobin Murphy 
348*1888d3ddSRobin Murphy 	/* Arguments required by an event */
349*1888d3ddSRobin Murphy 	switch (event->type) {
350*1888d3ddSRobin Murphy 	case CCN_TYPE_CYCLES:
351*1888d3ddSRobin Murphy 		break;
352*1888d3ddSRobin Murphy 	case CCN_TYPE_XP:
353*1888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res,
354*1888d3ddSRobin Murphy 				",xp=?,vc=?");
355*1888d3ddSRobin Murphy 		if (event->event == CCN_EVENT_WATCHPOINT)
356*1888d3ddSRobin Murphy 			res += snprintf(buf + res, PAGE_SIZE - res,
357*1888d3ddSRobin Murphy 					",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
358*1888d3ddSRobin Murphy 		else
359*1888d3ddSRobin Murphy 			res += snprintf(buf + res, PAGE_SIZE - res,
360*1888d3ddSRobin Murphy 					",bus=?");
361*1888d3ddSRobin Murphy 
362*1888d3ddSRobin Murphy 		break;
363*1888d3ddSRobin Murphy 	case CCN_TYPE_MN:
364*1888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",node=%d", ccn->mn_id);
365*1888d3ddSRobin Murphy 		break;
366*1888d3ddSRobin Murphy 	default:
367*1888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",node=?");
368*1888d3ddSRobin Murphy 		break;
369*1888d3ddSRobin Murphy 	}
370*1888d3ddSRobin Murphy 
371*1888d3ddSRobin Murphy 	res += snprintf(buf + res, PAGE_SIZE - res, "\n");
372*1888d3ddSRobin Murphy 
373*1888d3ddSRobin Murphy 	return res;
374*1888d3ddSRobin Murphy }
375*1888d3ddSRobin Murphy 
376*1888d3ddSRobin Murphy static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
377*1888d3ddSRobin Murphy 				     struct attribute *attr, int index)
378*1888d3ddSRobin Murphy {
379*1888d3ddSRobin Murphy 	struct device *dev = kobj_to_dev(kobj);
380*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
381*1888d3ddSRobin Murphy 	struct device_attribute *dev_attr = container_of(attr,
382*1888d3ddSRobin Murphy 			struct device_attribute, attr);
383*1888d3ddSRobin Murphy 	struct arm_ccn_pmu_event *event = container_of(dev_attr,
384*1888d3ddSRobin Murphy 			struct arm_ccn_pmu_event, attr);
385*1888d3ddSRobin Murphy 
386*1888d3ddSRobin Murphy 	if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
387*1888d3ddSRobin Murphy 		return 0;
388*1888d3ddSRobin Murphy 	if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
389*1888d3ddSRobin Murphy 		return 0;
390*1888d3ddSRobin Murphy 
391*1888d3ddSRobin Murphy 	return attr->mode;
392*1888d3ddSRobin Murphy }
393*1888d3ddSRobin Murphy 
394*1888d3ddSRobin Murphy static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
395*1888d3ddSRobin Murphy 	CCN_EVENT_MN(eobarrier, "dir=1,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
396*1888d3ddSRobin Murphy 	CCN_EVENT_MN(ecbarrier, "dir=1,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
397*1888d3ddSRobin Murphy 	CCN_EVENT_MN(dvmop, "dir=1,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
398*1888d3ddSRobin Murphy 	CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
399*1888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
400*1888d3ddSRobin Murphy 	CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
401*1888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
402*1888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
403*1888d3ddSRobin Murphy 			CCN_IDX_MASK_ORDER),
404*1888d3ddSRobin Murphy 	CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
405*1888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
406*1888d3ddSRobin Murphy 	CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
407*1888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
408*1888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
409*1888d3ddSRobin Murphy 			CCN_IDX_MASK_ORDER),
410*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(cache_miss, 0x1),
411*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
412*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(cache_fill, 0x3),
413*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(pocq_retry, 0x4),
414*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
415*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(sf_hit, 0x6),
416*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(sf_evictions, 0x7),
417*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(snoops_sent, 0x8),
418*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(snoops_broadcast, 0x9),
419*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_eviction, 0xa),
420*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
421*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(mc_retries, 0xc),
422*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(mc_reqs, 0xd),
423*1888d3ddSRobin Murphy 	CCN_EVENT_HNF(qos_hh_retry, 0xe),
424*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p0, 0x1),
425*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p1, 0x2),
426*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p2, 0x3),
427*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(rxdat_flits, 0x4),
428*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(txdat_flits, 0x5),
429*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits, 0x6),
430*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits_retried, 0x7),
431*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(rrt_full, 0x8),
432*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(wrt_full, 0x9),
433*1888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
434*1888d3ddSRobin Murphy 	CCN_EVENT_XP(upload_starvation, 0x1),
435*1888d3ddSRobin Murphy 	CCN_EVENT_XP(download_starvation, 0x2),
436*1888d3ddSRobin Murphy 	CCN_EVENT_XP(respin, 0x3),
437*1888d3ddSRobin Murphy 	CCN_EVENT_XP(valid_flit, 0x4),
438*1888d3ddSRobin Murphy 	CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
439*1888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
440*1888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rxdat_flits, 0x4),
441*1888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txdat_flits, 0x5),
442*1888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits, 0x6),
443*1888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
444*1888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rrt_full, 0x8),
445*1888d3ddSRobin Murphy 	CCN_EVENT_SBAS(wrt_full, 0x9),
446*1888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
447*1888d3ddSRobin Murphy 	CCN_EVENT_CYCLES(cycles),
448*1888d3ddSRobin Murphy };
449*1888d3ddSRobin Murphy 
450*1888d3ddSRobin Murphy /* Populated in arm_ccn_init() */
451*1888d3ddSRobin Murphy static struct attribute
452*1888d3ddSRobin Murphy 		*arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
453*1888d3ddSRobin Murphy 
454*1888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_events_attr_group = {
455*1888d3ddSRobin Murphy 	.name = "events",
456*1888d3ddSRobin Murphy 	.is_visible = arm_ccn_pmu_events_is_visible,
457*1888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_events_attrs,
458*1888d3ddSRobin Murphy };
459*1888d3ddSRobin Murphy 
460*1888d3ddSRobin Murphy 
461*1888d3ddSRobin Murphy static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
462*1888d3ddSRobin Murphy {
463*1888d3ddSRobin Murphy 	unsigned long i;
464*1888d3ddSRobin Murphy 
465*1888d3ddSRobin Murphy 	if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
466*1888d3ddSRobin Murphy 		return NULL;
467*1888d3ddSRobin Murphy 	i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
468*1888d3ddSRobin Murphy 
469*1888d3ddSRobin Murphy 	switch (name[1]) {
470*1888d3ddSRobin Murphy 	case 'l':
471*1888d3ddSRobin Murphy 		return &ccn->dt.cmp_mask[i].l;
472*1888d3ddSRobin Murphy 	case 'h':
473*1888d3ddSRobin Murphy 		return &ccn->dt.cmp_mask[i].h;
474*1888d3ddSRobin Murphy 	default:
475*1888d3ddSRobin Murphy 		return NULL;
476*1888d3ddSRobin Murphy 	}
477*1888d3ddSRobin Murphy }
478*1888d3ddSRobin Murphy 
479*1888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
480*1888d3ddSRobin Murphy 		struct device_attribute *attr, char *buf)
481*1888d3ddSRobin Murphy {
482*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
483*1888d3ddSRobin Murphy 	u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
484*1888d3ddSRobin Murphy 
485*1888d3ddSRobin Murphy 	return mask ? snprintf(buf, PAGE_SIZE, "0x%016llx\n", *mask) : -EINVAL;
486*1888d3ddSRobin Murphy }
487*1888d3ddSRobin Murphy 
488*1888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
489*1888d3ddSRobin Murphy 		struct device_attribute *attr, const char *buf, size_t count)
490*1888d3ddSRobin Murphy {
491*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
492*1888d3ddSRobin Murphy 	u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
493*1888d3ddSRobin Murphy 	int err = -EINVAL;
494*1888d3ddSRobin Murphy 
495*1888d3ddSRobin Murphy 	if (mask)
496*1888d3ddSRobin Murphy 		err = kstrtoull(buf, 0, mask);
497*1888d3ddSRobin Murphy 
498*1888d3ddSRobin Murphy 	return err ? err : count;
499*1888d3ddSRobin Murphy }
500*1888d3ddSRobin Murphy 
501*1888d3ddSRobin Murphy #define CCN_CMP_MASK_ATTR(_name) \
502*1888d3ddSRobin Murphy 	struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
503*1888d3ddSRobin Murphy 			__ATTR(_name, S_IRUGO | S_IWUSR, \
504*1888d3ddSRobin Murphy 			arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
505*1888d3ddSRobin Murphy 
506*1888d3ddSRobin Murphy #define CCN_CMP_MASK_ATTR_RO(_name) \
507*1888d3ddSRobin Murphy 	struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
508*1888d3ddSRobin Murphy 			__ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
509*1888d3ddSRobin Murphy 
510*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(0l);
511*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(0h);
512*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(1l);
513*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(1h);
514*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(2l);
515*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(2h);
516*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(3l);
517*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(3h);
518*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(4l);
519*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(4h);
520*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(5l);
521*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(5h);
522*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(6l);
523*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(6h);
524*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(7l);
525*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(7h);
526*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(8l);
527*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(8h);
528*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(9l);
529*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(9h);
530*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(al);
531*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(ah);
532*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(bl);
533*1888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(bh);
534*1888d3ddSRobin Murphy 
535*1888d3ddSRobin Murphy static struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
536*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
537*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
538*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
539*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
540*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
541*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
542*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
543*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
544*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
545*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
546*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
547*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
548*1888d3ddSRobin Murphy 	NULL
549*1888d3ddSRobin Murphy };
550*1888d3ddSRobin Murphy 
551*1888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
552*1888d3ddSRobin Murphy 	.name = "cmp_mask",
553*1888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_cmp_mask_attrs,
554*1888d3ddSRobin Murphy };
555*1888d3ddSRobin Murphy 
556*1888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
557*1888d3ddSRobin Murphy 				     struct device_attribute *attr, char *buf)
558*1888d3ddSRobin Murphy {
559*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
560*1888d3ddSRobin Murphy 
561*1888d3ddSRobin Murphy 	return cpumap_print_to_pagebuf(true, buf, &ccn->dt.cpu);
562*1888d3ddSRobin Murphy }
563*1888d3ddSRobin Murphy 
564*1888d3ddSRobin Murphy static struct device_attribute arm_ccn_pmu_cpumask_attr =
565*1888d3ddSRobin Murphy 		__ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
566*1888d3ddSRobin Murphy 
567*1888d3ddSRobin Murphy static struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
568*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cpumask_attr.attr,
569*1888d3ddSRobin Murphy 	NULL,
570*1888d3ddSRobin Murphy };
571*1888d3ddSRobin Murphy 
572*1888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
573*1888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_cpumask_attrs,
574*1888d3ddSRobin Murphy };
575*1888d3ddSRobin Murphy 
576*1888d3ddSRobin Murphy /*
577*1888d3ddSRobin Murphy  * Default poll period is 10ms, which is way over the top anyway,
578*1888d3ddSRobin Murphy  * as in the worst case scenario (an event every cycle), with 1GHz
579*1888d3ddSRobin Murphy  * clocked bus, the smallest, 32 bit counter will overflow in
580*1888d3ddSRobin Murphy  * more than 4s.
581*1888d3ddSRobin Murphy  */
582*1888d3ddSRobin Murphy static unsigned int arm_ccn_pmu_poll_period_us = 10000;
583*1888d3ddSRobin Murphy module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
584*1888d3ddSRobin Murphy 		S_IRUGO | S_IWUSR);
585*1888d3ddSRobin Murphy 
586*1888d3ddSRobin Murphy static ktime_t arm_ccn_pmu_timer_period(void)
587*1888d3ddSRobin Murphy {
588*1888d3ddSRobin Murphy 	return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
589*1888d3ddSRobin Murphy }
590*1888d3ddSRobin Murphy 
591*1888d3ddSRobin Murphy 
592*1888d3ddSRobin Murphy static const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
593*1888d3ddSRobin Murphy 	&arm_ccn_pmu_events_attr_group,
594*1888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_group,
595*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_group,
596*1888d3ddSRobin Murphy 	&arm_ccn_pmu_cpumask_attr_group,
597*1888d3ddSRobin Murphy 	NULL
598*1888d3ddSRobin Murphy };
599*1888d3ddSRobin Murphy 
600*1888d3ddSRobin Murphy 
601*1888d3ddSRobin Murphy static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
602*1888d3ddSRobin Murphy {
603*1888d3ddSRobin Murphy 	int bit;
604*1888d3ddSRobin Murphy 
605*1888d3ddSRobin Murphy 	do {
606*1888d3ddSRobin Murphy 		bit = find_first_zero_bit(bitmap, size);
607*1888d3ddSRobin Murphy 		if (bit >= size)
608*1888d3ddSRobin Murphy 			return -EAGAIN;
609*1888d3ddSRobin Murphy 	} while (test_and_set_bit(bit, bitmap));
610*1888d3ddSRobin Murphy 
611*1888d3ddSRobin Murphy 	return bit;
612*1888d3ddSRobin Murphy }
613*1888d3ddSRobin Murphy 
614*1888d3ddSRobin Murphy /* All RN-I and RN-D nodes have identical PMUs */
615*1888d3ddSRobin Murphy static int arm_ccn_pmu_type_eq(u32 a, u32 b)
616*1888d3ddSRobin Murphy {
617*1888d3ddSRobin Murphy 	if (a == b)
618*1888d3ddSRobin Murphy 		return 1;
619*1888d3ddSRobin Murphy 
620*1888d3ddSRobin Murphy 	switch (a) {
621*1888d3ddSRobin Murphy 	case CCN_TYPE_RNI_1P:
622*1888d3ddSRobin Murphy 	case CCN_TYPE_RNI_2P:
623*1888d3ddSRobin Murphy 	case CCN_TYPE_RNI_3P:
624*1888d3ddSRobin Murphy 	case CCN_TYPE_RND_1P:
625*1888d3ddSRobin Murphy 	case CCN_TYPE_RND_2P:
626*1888d3ddSRobin Murphy 	case CCN_TYPE_RND_3P:
627*1888d3ddSRobin Murphy 		switch (b) {
628*1888d3ddSRobin Murphy 		case CCN_TYPE_RNI_1P:
629*1888d3ddSRobin Murphy 		case CCN_TYPE_RNI_2P:
630*1888d3ddSRobin Murphy 		case CCN_TYPE_RNI_3P:
631*1888d3ddSRobin Murphy 		case CCN_TYPE_RND_1P:
632*1888d3ddSRobin Murphy 		case CCN_TYPE_RND_2P:
633*1888d3ddSRobin Murphy 		case CCN_TYPE_RND_3P:
634*1888d3ddSRobin Murphy 			return 1;
635*1888d3ddSRobin Murphy 		}
636*1888d3ddSRobin Murphy 		break;
637*1888d3ddSRobin Murphy 	}
638*1888d3ddSRobin Murphy 
639*1888d3ddSRobin Murphy 	return 0;
640*1888d3ddSRobin Murphy }
641*1888d3ddSRobin Murphy 
642*1888d3ddSRobin Murphy static int arm_ccn_pmu_event_alloc(struct perf_event *event)
643*1888d3ddSRobin Murphy {
644*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
645*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
646*1888d3ddSRobin Murphy 	u32 node_xp, type, event_id;
647*1888d3ddSRobin Murphy 	struct arm_ccn_component *source;
648*1888d3ddSRobin Murphy 	int bit;
649*1888d3ddSRobin Murphy 
650*1888d3ddSRobin Murphy 	node_xp = CCN_CONFIG_NODE(event->attr.config);
651*1888d3ddSRobin Murphy 	type = CCN_CONFIG_TYPE(event->attr.config);
652*1888d3ddSRobin Murphy 	event_id = CCN_CONFIG_EVENT(event->attr.config);
653*1888d3ddSRobin Murphy 
654*1888d3ddSRobin Murphy 	/* Allocate the cycle counter */
655*1888d3ddSRobin Murphy 	if (type == CCN_TYPE_CYCLES) {
656*1888d3ddSRobin Murphy 		if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
657*1888d3ddSRobin Murphy 				ccn->dt.pmu_counters_mask))
658*1888d3ddSRobin Murphy 			return -EAGAIN;
659*1888d3ddSRobin Murphy 
660*1888d3ddSRobin Murphy 		hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
661*1888d3ddSRobin Murphy 		ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
662*1888d3ddSRobin Murphy 
663*1888d3ddSRobin Murphy 		return 0;
664*1888d3ddSRobin Murphy 	}
665*1888d3ddSRobin Murphy 
666*1888d3ddSRobin Murphy 	/* Allocate an event counter */
667*1888d3ddSRobin Murphy 	hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
668*1888d3ddSRobin Murphy 			CCN_NUM_PMU_EVENT_COUNTERS);
669*1888d3ddSRobin Murphy 	if (hw->idx < 0) {
670*1888d3ddSRobin Murphy 		dev_dbg(ccn->dev, "No more counters available!\n");
671*1888d3ddSRobin Murphy 		return -EAGAIN;
672*1888d3ddSRobin Murphy 	}
673*1888d3ddSRobin Murphy 
674*1888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP)
675*1888d3ddSRobin Murphy 		source = &ccn->xp[node_xp];
676*1888d3ddSRobin Murphy 	else
677*1888d3ddSRobin Murphy 		source = &ccn->node[node_xp];
678*1888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].source = source;
679*1888d3ddSRobin Murphy 
680*1888d3ddSRobin Murphy 	/* Allocate an event source or a watchpoint */
681*1888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
682*1888d3ddSRobin Murphy 		bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
683*1888d3ddSRobin Murphy 				CCN_NUM_XP_WATCHPOINTS);
684*1888d3ddSRobin Murphy 	else
685*1888d3ddSRobin Murphy 		bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
686*1888d3ddSRobin Murphy 				CCN_NUM_PMU_EVENTS);
687*1888d3ddSRobin Murphy 	if (bit < 0) {
688*1888d3ddSRobin Murphy 		dev_dbg(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
689*1888d3ddSRobin Murphy 				node_xp);
690*1888d3ddSRobin Murphy 		clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
691*1888d3ddSRobin Murphy 		return -EAGAIN;
692*1888d3ddSRobin Murphy 	}
693*1888d3ddSRobin Murphy 	hw->config_base = bit;
694*1888d3ddSRobin Murphy 
695*1888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].event = event;
696*1888d3ddSRobin Murphy 
697*1888d3ddSRobin Murphy 	return 0;
698*1888d3ddSRobin Murphy }
699*1888d3ddSRobin Murphy 
700*1888d3ddSRobin Murphy static void arm_ccn_pmu_event_release(struct perf_event *event)
701*1888d3ddSRobin Murphy {
702*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
703*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
704*1888d3ddSRobin Murphy 
705*1888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
706*1888d3ddSRobin Murphy 		clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
707*1888d3ddSRobin Murphy 	} else {
708*1888d3ddSRobin Murphy 		struct arm_ccn_component *source =
709*1888d3ddSRobin Murphy 				ccn->dt.pmu_counters[hw->idx].source;
710*1888d3ddSRobin Murphy 
711*1888d3ddSRobin Murphy 		if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
712*1888d3ddSRobin Murphy 				CCN_CONFIG_EVENT(event->attr.config) ==
713*1888d3ddSRobin Murphy 				CCN_EVENT_WATCHPOINT)
714*1888d3ddSRobin Murphy 			clear_bit(hw->config_base, source->xp.dt_cmp_mask);
715*1888d3ddSRobin Murphy 		else
716*1888d3ddSRobin Murphy 			clear_bit(hw->config_base, source->pmu_events_mask);
717*1888d3ddSRobin Murphy 		clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
718*1888d3ddSRobin Murphy 	}
719*1888d3ddSRobin Murphy 
720*1888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].source = NULL;
721*1888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].event = NULL;
722*1888d3ddSRobin Murphy }
723*1888d3ddSRobin Murphy 
724*1888d3ddSRobin Murphy static int arm_ccn_pmu_event_init(struct perf_event *event)
725*1888d3ddSRobin Murphy {
726*1888d3ddSRobin Murphy 	struct arm_ccn *ccn;
727*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
728*1888d3ddSRobin Murphy 	u32 node_xp, type, event_id;
729*1888d3ddSRobin Murphy 	int valid;
730*1888d3ddSRobin Murphy 	int i;
731*1888d3ddSRobin Murphy 	struct perf_event *sibling;
732*1888d3ddSRobin Murphy 
733*1888d3ddSRobin Murphy 	if (event->attr.type != event->pmu->type)
734*1888d3ddSRobin Murphy 		return -ENOENT;
735*1888d3ddSRobin Murphy 
736*1888d3ddSRobin Murphy 	ccn = pmu_to_arm_ccn(event->pmu);
737*1888d3ddSRobin Murphy 
738*1888d3ddSRobin Murphy 	if (hw->sample_period) {
739*1888d3ddSRobin Murphy 		dev_warn(ccn->dev, "Sampling not supported!\n");
740*1888d3ddSRobin Murphy 		return -EOPNOTSUPP;
741*1888d3ddSRobin Murphy 	}
742*1888d3ddSRobin Murphy 
743*1888d3ddSRobin Murphy 	if (has_branch_stack(event) || event->attr.exclude_user ||
744*1888d3ddSRobin Murphy 			event->attr.exclude_kernel || event->attr.exclude_hv ||
745*1888d3ddSRobin Murphy 			event->attr.exclude_idle || event->attr.exclude_host ||
746*1888d3ddSRobin Murphy 			event->attr.exclude_guest) {
747*1888d3ddSRobin Murphy 		dev_warn(ccn->dev, "Can't exclude execution levels!\n");
748*1888d3ddSRobin Murphy 		return -EINVAL;
749*1888d3ddSRobin Murphy 	}
750*1888d3ddSRobin Murphy 
751*1888d3ddSRobin Murphy 	if (event->cpu < 0) {
752*1888d3ddSRobin Murphy 		dev_warn(ccn->dev, "Can't provide per-task data!\n");
753*1888d3ddSRobin Murphy 		return -EOPNOTSUPP;
754*1888d3ddSRobin Murphy 	}
755*1888d3ddSRobin Murphy 	/*
756*1888d3ddSRobin Murphy 	 * Many perf core operations (eg. events rotation) operate on a
757*1888d3ddSRobin Murphy 	 * single CPU context. This is obvious for CPU PMUs, where one
758*1888d3ddSRobin Murphy 	 * expects the same sets of events being observed on all CPUs,
759*1888d3ddSRobin Murphy 	 * but can lead to issues for off-core PMUs, like CCN, where each
760*1888d3ddSRobin Murphy 	 * event could be theoretically assigned to a different CPU. To
761*1888d3ddSRobin Murphy 	 * mitigate this, we enforce CPU assignment to one, selected
762*1888d3ddSRobin Murphy 	 * processor (the one described in the "cpumask" attribute).
763*1888d3ddSRobin Murphy 	 */
764*1888d3ddSRobin Murphy 	event->cpu = cpumask_first(&ccn->dt.cpu);
765*1888d3ddSRobin Murphy 
766*1888d3ddSRobin Murphy 	node_xp = CCN_CONFIG_NODE(event->attr.config);
767*1888d3ddSRobin Murphy 	type = CCN_CONFIG_TYPE(event->attr.config);
768*1888d3ddSRobin Murphy 	event_id = CCN_CONFIG_EVENT(event->attr.config);
769*1888d3ddSRobin Murphy 
770*1888d3ddSRobin Murphy 	/* Validate node/xp vs topology */
771*1888d3ddSRobin Murphy 	switch (type) {
772*1888d3ddSRobin Murphy 	case CCN_TYPE_MN:
773*1888d3ddSRobin Murphy 		if (node_xp != ccn->mn_id) {
774*1888d3ddSRobin Murphy 			dev_warn(ccn->dev, "Invalid MN ID %d!\n", node_xp);
775*1888d3ddSRobin Murphy 			return -EINVAL;
776*1888d3ddSRobin Murphy 		}
777*1888d3ddSRobin Murphy 		break;
778*1888d3ddSRobin Murphy 	case CCN_TYPE_XP:
779*1888d3ddSRobin Murphy 		if (node_xp >= ccn->num_xps) {
780*1888d3ddSRobin Murphy 			dev_warn(ccn->dev, "Invalid XP ID %d!\n", node_xp);
781*1888d3ddSRobin Murphy 			return -EINVAL;
782*1888d3ddSRobin Murphy 		}
783*1888d3ddSRobin Murphy 		break;
784*1888d3ddSRobin Murphy 	case CCN_TYPE_CYCLES:
785*1888d3ddSRobin Murphy 		break;
786*1888d3ddSRobin Murphy 	default:
787*1888d3ddSRobin Murphy 		if (node_xp >= ccn->num_nodes) {
788*1888d3ddSRobin Murphy 			dev_warn(ccn->dev, "Invalid node ID %d!\n", node_xp);
789*1888d3ddSRobin Murphy 			return -EINVAL;
790*1888d3ddSRobin Murphy 		}
791*1888d3ddSRobin Murphy 		if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
792*1888d3ddSRobin Murphy 			dev_warn(ccn->dev, "Invalid type 0x%x for node %d!\n",
793*1888d3ddSRobin Murphy 					type, node_xp);
794*1888d3ddSRobin Murphy 			return -EINVAL;
795*1888d3ddSRobin Murphy 		}
796*1888d3ddSRobin Murphy 		break;
797*1888d3ddSRobin Murphy 	}
798*1888d3ddSRobin Murphy 
799*1888d3ddSRobin Murphy 	/* Validate event ID vs available for the type */
800*1888d3ddSRobin Murphy 	for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
801*1888d3ddSRobin Murphy 			i++) {
802*1888d3ddSRobin Murphy 		struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
803*1888d3ddSRobin Murphy 		u32 port = CCN_CONFIG_PORT(event->attr.config);
804*1888d3ddSRobin Murphy 		u32 vc = CCN_CONFIG_VC(event->attr.config);
805*1888d3ddSRobin Murphy 
806*1888d3ddSRobin Murphy 		if (!arm_ccn_pmu_type_eq(type, e->type))
807*1888d3ddSRobin Murphy 			continue;
808*1888d3ddSRobin Murphy 		if (event_id != e->event)
809*1888d3ddSRobin Murphy 			continue;
810*1888d3ddSRobin Murphy 		if (e->num_ports && port >= e->num_ports) {
811*1888d3ddSRobin Murphy 			dev_warn(ccn->dev, "Invalid port %d for node/XP %d!\n",
812*1888d3ddSRobin Murphy 					port, node_xp);
813*1888d3ddSRobin Murphy 			return -EINVAL;
814*1888d3ddSRobin Murphy 		}
815*1888d3ddSRobin Murphy 		if (e->num_vcs && vc >= e->num_vcs) {
816*1888d3ddSRobin Murphy 			dev_warn(ccn->dev, "Invalid vc %d for node/XP %d!\n",
817*1888d3ddSRobin Murphy 					vc, node_xp);
818*1888d3ddSRobin Murphy 			return -EINVAL;
819*1888d3ddSRobin Murphy 		}
820*1888d3ddSRobin Murphy 		valid = 1;
821*1888d3ddSRobin Murphy 	}
822*1888d3ddSRobin Murphy 	if (!valid) {
823*1888d3ddSRobin Murphy 		dev_warn(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
824*1888d3ddSRobin Murphy 				event_id, node_xp);
825*1888d3ddSRobin Murphy 		return -EINVAL;
826*1888d3ddSRobin Murphy 	}
827*1888d3ddSRobin Murphy 
828*1888d3ddSRobin Murphy 	/* Watchpoint-based event for a node is actually set on XP */
829*1888d3ddSRobin Murphy 	if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
830*1888d3ddSRobin Murphy 		u32 port;
831*1888d3ddSRobin Murphy 
832*1888d3ddSRobin Murphy 		type = CCN_TYPE_XP;
833*1888d3ddSRobin Murphy 		port = arm_ccn_node_to_xp_port(node_xp);
834*1888d3ddSRobin Murphy 		node_xp = arm_ccn_node_to_xp(node_xp);
835*1888d3ddSRobin Murphy 
836*1888d3ddSRobin Murphy 		arm_ccn_pmu_config_set(&event->attr.config,
837*1888d3ddSRobin Murphy 				node_xp, type, port);
838*1888d3ddSRobin Murphy 	}
839*1888d3ddSRobin Murphy 
840*1888d3ddSRobin Murphy 	/*
841*1888d3ddSRobin Murphy 	 * We must NOT create groups containing mixed PMUs, although software
842*1888d3ddSRobin Murphy 	 * events are acceptable (for example to create a CCN group
843*1888d3ddSRobin Murphy 	 * periodically read when a hrtimer aka cpu-clock leader triggers).
844*1888d3ddSRobin Murphy 	 */
845*1888d3ddSRobin Murphy 	if (event->group_leader->pmu != event->pmu &&
846*1888d3ddSRobin Murphy 			!is_software_event(event->group_leader))
847*1888d3ddSRobin Murphy 		return -EINVAL;
848*1888d3ddSRobin Murphy 
849*1888d3ddSRobin Murphy 	list_for_each_entry(sibling, &event->group_leader->sibling_list,
850*1888d3ddSRobin Murphy 			group_entry)
851*1888d3ddSRobin Murphy 		if (sibling->pmu != event->pmu &&
852*1888d3ddSRobin Murphy 				!is_software_event(sibling))
853*1888d3ddSRobin Murphy 			return -EINVAL;
854*1888d3ddSRobin Murphy 
855*1888d3ddSRobin Murphy 	return 0;
856*1888d3ddSRobin Murphy }
857*1888d3ddSRobin Murphy 
858*1888d3ddSRobin Murphy static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
859*1888d3ddSRobin Murphy {
860*1888d3ddSRobin Murphy 	u64 res;
861*1888d3ddSRobin Murphy 
862*1888d3ddSRobin Murphy 	if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
863*1888d3ddSRobin Murphy #ifdef readq
864*1888d3ddSRobin Murphy 		res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
865*1888d3ddSRobin Murphy #else
866*1888d3ddSRobin Murphy 		/* 40 bit counter, can do snapshot and read in two parts */
867*1888d3ddSRobin Murphy 		writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
868*1888d3ddSRobin Murphy 		while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
869*1888d3ddSRobin Murphy 			;
870*1888d3ddSRobin Murphy 		writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
871*1888d3ddSRobin Murphy 		res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
872*1888d3ddSRobin Murphy 		res <<= 32;
873*1888d3ddSRobin Murphy 		res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
874*1888d3ddSRobin Murphy #endif
875*1888d3ddSRobin Murphy 	} else {
876*1888d3ddSRobin Murphy 		res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
877*1888d3ddSRobin Murphy 	}
878*1888d3ddSRobin Murphy 
879*1888d3ddSRobin Murphy 	return res;
880*1888d3ddSRobin Murphy }
881*1888d3ddSRobin Murphy 
882*1888d3ddSRobin Murphy static void arm_ccn_pmu_event_update(struct perf_event *event)
883*1888d3ddSRobin Murphy {
884*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
885*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
886*1888d3ddSRobin Murphy 	u64 prev_count, new_count, mask;
887*1888d3ddSRobin Murphy 
888*1888d3ddSRobin Murphy 	do {
889*1888d3ddSRobin Murphy 		prev_count = local64_read(&hw->prev_count);
890*1888d3ddSRobin Murphy 		new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
891*1888d3ddSRobin Murphy 	} while (local64_xchg(&hw->prev_count, new_count) != prev_count);
892*1888d3ddSRobin Murphy 
893*1888d3ddSRobin Murphy 	mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
894*1888d3ddSRobin Murphy 
895*1888d3ddSRobin Murphy 	local64_add((new_count - prev_count) & mask, &event->count);
896*1888d3ddSRobin Murphy }
897*1888d3ddSRobin Murphy 
898*1888d3ddSRobin Murphy static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
899*1888d3ddSRobin Murphy {
900*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
901*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
902*1888d3ddSRobin Murphy 	struct arm_ccn_component *xp;
903*1888d3ddSRobin Murphy 	u32 val, dt_cfg;
904*1888d3ddSRobin Murphy 
905*1888d3ddSRobin Murphy 	/* Nothing to do for cycle counter */
906*1888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
907*1888d3ddSRobin Murphy 		return;
908*1888d3ddSRobin Murphy 
909*1888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
910*1888d3ddSRobin Murphy 		xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
911*1888d3ddSRobin Murphy 	else
912*1888d3ddSRobin Murphy 		xp = &ccn->xp[arm_ccn_node_to_xp(
913*1888d3ddSRobin Murphy 				CCN_CONFIG_NODE(event->attr.config))];
914*1888d3ddSRobin Murphy 
915*1888d3ddSRobin Murphy 	if (enable)
916*1888d3ddSRobin Murphy 		dt_cfg = hw->event_base;
917*1888d3ddSRobin Murphy 	else
918*1888d3ddSRobin Murphy 		dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
919*1888d3ddSRobin Murphy 
920*1888d3ddSRobin Murphy 	spin_lock(&ccn->dt.config_lock);
921*1888d3ddSRobin Murphy 
922*1888d3ddSRobin Murphy 	val = readl(xp->base + CCN_XP_DT_CONFIG);
923*1888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
924*1888d3ddSRobin Murphy 			CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
925*1888d3ddSRobin Murphy 	val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
926*1888d3ddSRobin Murphy 	writel(val, xp->base + CCN_XP_DT_CONFIG);
927*1888d3ddSRobin Murphy 
928*1888d3ddSRobin Murphy 	spin_unlock(&ccn->dt.config_lock);
929*1888d3ddSRobin Murphy }
930*1888d3ddSRobin Murphy 
931*1888d3ddSRobin Murphy static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
932*1888d3ddSRobin Murphy {
933*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
934*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
935*1888d3ddSRobin Murphy 
936*1888d3ddSRobin Murphy 	local64_set(&event->hw.prev_count,
937*1888d3ddSRobin Murphy 			arm_ccn_pmu_read_counter(ccn, hw->idx));
938*1888d3ddSRobin Murphy 	hw->state = 0;
939*1888d3ddSRobin Murphy 
940*1888d3ddSRobin Murphy 	/* Set the DT bus input, engaging the counter */
941*1888d3ddSRobin Murphy 	arm_ccn_pmu_xp_dt_config(event, 1);
942*1888d3ddSRobin Murphy }
943*1888d3ddSRobin Murphy 
944*1888d3ddSRobin Murphy static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
945*1888d3ddSRobin Murphy {
946*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
947*1888d3ddSRobin Murphy 
948*1888d3ddSRobin Murphy 	/* Disable counting, setting the DT bus to pass-through mode */
949*1888d3ddSRobin Murphy 	arm_ccn_pmu_xp_dt_config(event, 0);
950*1888d3ddSRobin Murphy 
951*1888d3ddSRobin Murphy 	if (flags & PERF_EF_UPDATE)
952*1888d3ddSRobin Murphy 		arm_ccn_pmu_event_update(event);
953*1888d3ddSRobin Murphy 
954*1888d3ddSRobin Murphy 	hw->state |= PERF_HES_STOPPED;
955*1888d3ddSRobin Murphy }
956*1888d3ddSRobin Murphy 
957*1888d3ddSRobin Murphy static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
958*1888d3ddSRobin Murphy {
959*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
960*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
961*1888d3ddSRobin Murphy 	struct arm_ccn_component *source =
962*1888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
963*1888d3ddSRobin Murphy 	unsigned long wp = hw->config_base;
964*1888d3ddSRobin Murphy 	u32 val;
965*1888d3ddSRobin Murphy 	u64 cmp_l = event->attr.config1;
966*1888d3ddSRobin Murphy 	u64 cmp_h = event->attr.config2;
967*1888d3ddSRobin Murphy 	u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
968*1888d3ddSRobin Murphy 	u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
969*1888d3ddSRobin Murphy 
970*1888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
971*1888d3ddSRobin Murphy 
972*1888d3ddSRobin Murphy 	/* Direction (RX/TX), device (port) & virtual channel */
973*1888d3ddSRobin Murphy 	val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
974*1888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
975*1888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
976*1888d3ddSRobin Murphy 	val |= CCN_CONFIG_DIR(event->attr.config) <<
977*1888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
978*1888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
979*1888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
980*1888d3ddSRobin Murphy 	val |= CCN_CONFIG_PORT(event->attr.config) <<
981*1888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
982*1888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
983*1888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
984*1888d3ddSRobin Murphy 	val |= CCN_CONFIG_VC(event->attr.config) <<
985*1888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
986*1888d3ddSRobin Murphy 	writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
987*1888d3ddSRobin Murphy 
988*1888d3ddSRobin Murphy 	/* Comparison values */
989*1888d3ddSRobin Murphy 	writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
990*1888d3ddSRobin Murphy 	writel((cmp_l >> 32) & 0x7fffffff,
991*1888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
992*1888d3ddSRobin Murphy 	writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
993*1888d3ddSRobin Murphy 	writel((cmp_h >> 32) & 0x0fffffff,
994*1888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
995*1888d3ddSRobin Murphy 
996*1888d3ddSRobin Murphy 	/* Mask */
997*1888d3ddSRobin Murphy 	writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
998*1888d3ddSRobin Murphy 	writel((mask_l >> 32) & 0x7fffffff,
999*1888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
1000*1888d3ddSRobin Murphy 	writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
1001*1888d3ddSRobin Murphy 	writel((mask_h >> 32) & 0x0fffffff,
1002*1888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
1003*1888d3ddSRobin Murphy }
1004*1888d3ddSRobin Murphy 
1005*1888d3ddSRobin Murphy static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
1006*1888d3ddSRobin Murphy {
1007*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1008*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
1009*1888d3ddSRobin Murphy 	struct arm_ccn_component *source =
1010*1888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
1011*1888d3ddSRobin Murphy 	u32 val, id;
1012*1888d3ddSRobin Murphy 
1013*1888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
1014*1888d3ddSRobin Murphy 
1015*1888d3ddSRobin Murphy 	id = (CCN_CONFIG_VC(event->attr.config) << 4) |
1016*1888d3ddSRobin Murphy 			(CCN_CONFIG_BUS(event->attr.config) << 3) |
1017*1888d3ddSRobin Murphy 			(CCN_CONFIG_EVENT(event->attr.config) << 0);
1018*1888d3ddSRobin Murphy 
1019*1888d3ddSRobin Murphy 	val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
1020*1888d3ddSRobin Murphy 	val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
1021*1888d3ddSRobin Murphy 			CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
1022*1888d3ddSRobin Murphy 	val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
1023*1888d3ddSRobin Murphy 	writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
1024*1888d3ddSRobin Murphy }
1025*1888d3ddSRobin Murphy 
1026*1888d3ddSRobin Murphy static void arm_ccn_pmu_node_event_config(struct perf_event *event)
1027*1888d3ddSRobin Murphy {
1028*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1029*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
1030*1888d3ddSRobin Murphy 	struct arm_ccn_component *source =
1031*1888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
1032*1888d3ddSRobin Murphy 	u32 type = CCN_CONFIG_TYPE(event->attr.config);
1033*1888d3ddSRobin Murphy 	u32 val, port;
1034*1888d3ddSRobin Murphy 
1035*1888d3ddSRobin Murphy 	port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
1036*1888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
1037*1888d3ddSRobin Murphy 			hw->config_base);
1038*1888d3ddSRobin Murphy 
1039*1888d3ddSRobin Murphy 	/* These *_event_sel regs should be identical, but let's make sure... */
1040*1888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
1041*1888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
1042*1888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
1043*1888d3ddSRobin Murphy 			CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
1044*1888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
1045*1888d3ddSRobin Murphy 			CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
1046*1888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
1047*1888d3ddSRobin Murphy 			CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
1048*1888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
1049*1888d3ddSRobin Murphy 			CCN_RNI_PMU_EVENT_SEL__ID__MASK);
1050*1888d3ddSRobin Murphy 	if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
1051*1888d3ddSRobin Murphy 			!arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
1052*1888d3ddSRobin Murphy 		return;
1053*1888d3ddSRobin Murphy 
1054*1888d3ddSRobin Murphy 	/* Set the event id for the pre-allocated counter */
1055*1888d3ddSRobin Murphy 	val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
1056*1888d3ddSRobin Murphy 	val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
1057*1888d3ddSRobin Murphy 		CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
1058*1888d3ddSRobin Murphy 	val |= CCN_CONFIG_EVENT(event->attr.config) <<
1059*1888d3ddSRobin Murphy 		CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
1060*1888d3ddSRobin Murphy 	writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
1061*1888d3ddSRobin Murphy }
1062*1888d3ddSRobin Murphy 
1063*1888d3ddSRobin Murphy static void arm_ccn_pmu_event_config(struct perf_event *event)
1064*1888d3ddSRobin Murphy {
1065*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1066*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
1067*1888d3ddSRobin Murphy 	u32 xp, offset, val;
1068*1888d3ddSRobin Murphy 
1069*1888d3ddSRobin Murphy 	/* Cycle counter requires no setup */
1070*1888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
1071*1888d3ddSRobin Murphy 		return;
1072*1888d3ddSRobin Murphy 
1073*1888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
1074*1888d3ddSRobin Murphy 		xp = CCN_CONFIG_XP(event->attr.config);
1075*1888d3ddSRobin Murphy 	else
1076*1888d3ddSRobin Murphy 		xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
1077*1888d3ddSRobin Murphy 
1078*1888d3ddSRobin Murphy 	spin_lock(&ccn->dt.config_lock);
1079*1888d3ddSRobin Murphy 
1080*1888d3ddSRobin Murphy 	/* Set the DT bus "distance" register */
1081*1888d3ddSRobin Murphy 	offset = (hw->idx / 4) * 4;
1082*1888d3ddSRobin Murphy 	val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1083*1888d3ddSRobin Murphy 	val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
1084*1888d3ddSRobin Murphy 			CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
1085*1888d3ddSRobin Murphy 	val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
1086*1888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1087*1888d3ddSRobin Murphy 
1088*1888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
1089*1888d3ddSRobin Murphy 		if (CCN_CONFIG_EVENT(event->attr.config) ==
1090*1888d3ddSRobin Murphy 				CCN_EVENT_WATCHPOINT)
1091*1888d3ddSRobin Murphy 			arm_ccn_pmu_xp_watchpoint_config(event);
1092*1888d3ddSRobin Murphy 		else
1093*1888d3ddSRobin Murphy 			arm_ccn_pmu_xp_event_config(event);
1094*1888d3ddSRobin Murphy 	} else {
1095*1888d3ddSRobin Murphy 		arm_ccn_pmu_node_event_config(event);
1096*1888d3ddSRobin Murphy 	}
1097*1888d3ddSRobin Murphy 
1098*1888d3ddSRobin Murphy 	spin_unlock(&ccn->dt.config_lock);
1099*1888d3ddSRobin Murphy }
1100*1888d3ddSRobin Murphy 
1101*1888d3ddSRobin Murphy static int arm_ccn_pmu_active_counters(struct arm_ccn *ccn)
1102*1888d3ddSRobin Murphy {
1103*1888d3ddSRobin Murphy 	return bitmap_weight(ccn->dt.pmu_counters_mask,
1104*1888d3ddSRobin Murphy 			     CCN_NUM_PMU_EVENT_COUNTERS + 1);
1105*1888d3ddSRobin Murphy }
1106*1888d3ddSRobin Murphy 
1107*1888d3ddSRobin Murphy static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
1108*1888d3ddSRobin Murphy {
1109*1888d3ddSRobin Murphy 	int err;
1110*1888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
1111*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1112*1888d3ddSRobin Murphy 
1113*1888d3ddSRobin Murphy 	err = arm_ccn_pmu_event_alloc(event);
1114*1888d3ddSRobin Murphy 	if (err)
1115*1888d3ddSRobin Murphy 		return err;
1116*1888d3ddSRobin Murphy 
1117*1888d3ddSRobin Murphy 	/*
1118*1888d3ddSRobin Murphy 	 * Pin the timer, so that the overflows are handled by the chosen
1119*1888d3ddSRobin Murphy 	 * event->cpu (this is the same one as presented in "cpumask"
1120*1888d3ddSRobin Murphy 	 * attribute).
1121*1888d3ddSRobin Murphy 	 */
1122*1888d3ddSRobin Murphy 	if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 1)
1123*1888d3ddSRobin Murphy 		hrtimer_start(&ccn->dt.hrtimer, arm_ccn_pmu_timer_period(),
1124*1888d3ddSRobin Murphy 			      HRTIMER_MODE_REL_PINNED);
1125*1888d3ddSRobin Murphy 
1126*1888d3ddSRobin Murphy 	arm_ccn_pmu_event_config(event);
1127*1888d3ddSRobin Murphy 
1128*1888d3ddSRobin Murphy 	hw->state = PERF_HES_STOPPED;
1129*1888d3ddSRobin Murphy 
1130*1888d3ddSRobin Murphy 	if (flags & PERF_EF_START)
1131*1888d3ddSRobin Murphy 		arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
1132*1888d3ddSRobin Murphy 
1133*1888d3ddSRobin Murphy 	return 0;
1134*1888d3ddSRobin Murphy }
1135*1888d3ddSRobin Murphy 
1136*1888d3ddSRobin Murphy static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
1137*1888d3ddSRobin Murphy {
1138*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1139*1888d3ddSRobin Murphy 
1140*1888d3ddSRobin Murphy 	arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
1141*1888d3ddSRobin Murphy 
1142*1888d3ddSRobin Murphy 	arm_ccn_pmu_event_release(event);
1143*1888d3ddSRobin Murphy 
1144*1888d3ddSRobin Murphy 	if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 0)
1145*1888d3ddSRobin Murphy 		hrtimer_cancel(&ccn->dt.hrtimer);
1146*1888d3ddSRobin Murphy }
1147*1888d3ddSRobin Murphy 
1148*1888d3ddSRobin Murphy static void arm_ccn_pmu_event_read(struct perf_event *event)
1149*1888d3ddSRobin Murphy {
1150*1888d3ddSRobin Murphy 	arm_ccn_pmu_event_update(event);
1151*1888d3ddSRobin Murphy }
1152*1888d3ddSRobin Murphy 
1153*1888d3ddSRobin Murphy static void arm_ccn_pmu_enable(struct pmu *pmu)
1154*1888d3ddSRobin Murphy {
1155*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
1156*1888d3ddSRobin Murphy 
1157*1888d3ddSRobin Murphy 	u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
1158*1888d3ddSRobin Murphy 	val |= CCN_DT_PMCR__PMU_EN;
1159*1888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_PMCR);
1160*1888d3ddSRobin Murphy }
1161*1888d3ddSRobin Murphy 
1162*1888d3ddSRobin Murphy static void arm_ccn_pmu_disable(struct pmu *pmu)
1163*1888d3ddSRobin Murphy {
1164*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
1165*1888d3ddSRobin Murphy 
1166*1888d3ddSRobin Murphy 	u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
1167*1888d3ddSRobin Murphy 	val &= ~CCN_DT_PMCR__PMU_EN;
1168*1888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_PMCR);
1169*1888d3ddSRobin Murphy }
1170*1888d3ddSRobin Murphy 
1171*1888d3ddSRobin Murphy static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
1172*1888d3ddSRobin Murphy {
1173*1888d3ddSRobin Murphy 	u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
1174*1888d3ddSRobin Murphy 	int idx;
1175*1888d3ddSRobin Murphy 
1176*1888d3ddSRobin Murphy 	if (!pmovsr)
1177*1888d3ddSRobin Murphy 		return IRQ_NONE;
1178*1888d3ddSRobin Murphy 
1179*1888d3ddSRobin Murphy 	writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
1180*1888d3ddSRobin Murphy 
1181*1888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
1182*1888d3ddSRobin Murphy 
1183*1888d3ddSRobin Murphy 	for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
1184*1888d3ddSRobin Murphy 		struct perf_event *event = dt->pmu_counters[idx].event;
1185*1888d3ddSRobin Murphy 		int overflowed = pmovsr & BIT(idx);
1186*1888d3ddSRobin Murphy 
1187*1888d3ddSRobin Murphy 		WARN_ON_ONCE(overflowed && !event &&
1188*1888d3ddSRobin Murphy 				idx != CCN_IDX_PMU_CYCLE_COUNTER);
1189*1888d3ddSRobin Murphy 
1190*1888d3ddSRobin Murphy 		if (!event || !overflowed)
1191*1888d3ddSRobin Murphy 			continue;
1192*1888d3ddSRobin Murphy 
1193*1888d3ddSRobin Murphy 		arm_ccn_pmu_event_update(event);
1194*1888d3ddSRobin Murphy 	}
1195*1888d3ddSRobin Murphy 
1196*1888d3ddSRobin Murphy 	return IRQ_HANDLED;
1197*1888d3ddSRobin Murphy }
1198*1888d3ddSRobin Murphy 
1199*1888d3ddSRobin Murphy static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
1200*1888d3ddSRobin Murphy {
1201*1888d3ddSRobin Murphy 	struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
1202*1888d3ddSRobin Murphy 			hrtimer);
1203*1888d3ddSRobin Murphy 	unsigned long flags;
1204*1888d3ddSRobin Murphy 
1205*1888d3ddSRobin Murphy 	local_irq_save(flags);
1206*1888d3ddSRobin Murphy 	arm_ccn_pmu_overflow_handler(dt);
1207*1888d3ddSRobin Murphy 	local_irq_restore(flags);
1208*1888d3ddSRobin Murphy 
1209*1888d3ddSRobin Murphy 	hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
1210*1888d3ddSRobin Murphy 	return HRTIMER_RESTART;
1211*1888d3ddSRobin Murphy }
1212*1888d3ddSRobin Murphy 
1213*1888d3ddSRobin Murphy 
1214*1888d3ddSRobin Murphy static int arm_ccn_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
1215*1888d3ddSRobin Murphy {
1216*1888d3ddSRobin Murphy 	struct arm_ccn_dt *dt = hlist_entry_safe(node, struct arm_ccn_dt, node);
1217*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
1218*1888d3ddSRobin Murphy 	unsigned int target;
1219*1888d3ddSRobin Murphy 
1220*1888d3ddSRobin Murphy 	if (!cpumask_test_and_clear_cpu(cpu, &dt->cpu))
1221*1888d3ddSRobin Murphy 		return 0;
1222*1888d3ddSRobin Murphy 	target = cpumask_any_but(cpu_online_mask, cpu);
1223*1888d3ddSRobin Murphy 	if (target >= nr_cpu_ids)
1224*1888d3ddSRobin Murphy 		return 0;
1225*1888d3ddSRobin Murphy 	perf_pmu_migrate_context(&dt->pmu, cpu, target);
1226*1888d3ddSRobin Murphy 	cpumask_set_cpu(target, &dt->cpu);
1227*1888d3ddSRobin Murphy 	if (ccn->irq)
1228*1888d3ddSRobin Murphy 		WARN_ON(irq_set_affinity_hint(ccn->irq, &dt->cpu) != 0);
1229*1888d3ddSRobin Murphy 	return 0;
1230*1888d3ddSRobin Murphy }
1231*1888d3ddSRobin Murphy 
1232*1888d3ddSRobin Murphy static DEFINE_IDA(arm_ccn_pmu_ida);
1233*1888d3ddSRobin Murphy 
1234*1888d3ddSRobin Murphy static int arm_ccn_pmu_init(struct arm_ccn *ccn)
1235*1888d3ddSRobin Murphy {
1236*1888d3ddSRobin Murphy 	int i;
1237*1888d3ddSRobin Murphy 	char *name;
1238*1888d3ddSRobin Murphy 	int err;
1239*1888d3ddSRobin Murphy 
1240*1888d3ddSRobin Murphy 	/* Initialize DT subsystem */
1241*1888d3ddSRobin Murphy 	ccn->dt.base = ccn->base + CCN_REGION_SIZE;
1242*1888d3ddSRobin Murphy 	spin_lock_init(&ccn->dt.config_lock);
1243*1888d3ddSRobin Murphy 	writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
1244*1888d3ddSRobin Murphy 	writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
1245*1888d3ddSRobin Murphy 	writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
1246*1888d3ddSRobin Murphy 			ccn->dt.base + CCN_DT_PMCR);
1247*1888d3ddSRobin Murphy 	writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
1248*1888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++) {
1249*1888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
1250*1888d3ddSRobin Murphy 		writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1251*1888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
1252*1888d3ddSRobin Murphy 				(CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1253*1888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
1254*1888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__DT_ENABLE,
1255*1888d3ddSRobin Murphy 				ccn->xp[i].base + CCN_XP_DT_CONTROL);
1256*1888d3ddSRobin Murphy 	}
1257*1888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
1258*1888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
1259*1888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
1260*1888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
1261*1888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
1262*1888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
1263*1888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
1264*1888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
1265*1888d3ddSRobin Murphy 
1266*1888d3ddSRobin Murphy 	/* Get a convenient /sys/event_source/devices/ name */
1267*1888d3ddSRobin Murphy 	ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
1268*1888d3ddSRobin Murphy 	if (ccn->dt.id == 0) {
1269*1888d3ddSRobin Murphy 		name = "ccn";
1270*1888d3ddSRobin Murphy 	} else {
1271*1888d3ddSRobin Murphy 		name = devm_kasprintf(ccn->dev, GFP_KERNEL, "ccn_%d",
1272*1888d3ddSRobin Murphy 				      ccn->dt.id);
1273*1888d3ddSRobin Murphy 		if (!name) {
1274*1888d3ddSRobin Murphy 			err = -ENOMEM;
1275*1888d3ddSRobin Murphy 			goto error_choose_name;
1276*1888d3ddSRobin Murphy 		}
1277*1888d3ddSRobin Murphy 	}
1278*1888d3ddSRobin Murphy 
1279*1888d3ddSRobin Murphy 	/* Perf driver registration */
1280*1888d3ddSRobin Murphy 	ccn->dt.pmu = (struct pmu) {
1281*1888d3ddSRobin Murphy 		.module = THIS_MODULE,
1282*1888d3ddSRobin Murphy 		.attr_groups = arm_ccn_pmu_attr_groups,
1283*1888d3ddSRobin Murphy 		.task_ctx_nr = perf_invalid_context,
1284*1888d3ddSRobin Murphy 		.event_init = arm_ccn_pmu_event_init,
1285*1888d3ddSRobin Murphy 		.add = arm_ccn_pmu_event_add,
1286*1888d3ddSRobin Murphy 		.del = arm_ccn_pmu_event_del,
1287*1888d3ddSRobin Murphy 		.start = arm_ccn_pmu_event_start,
1288*1888d3ddSRobin Murphy 		.stop = arm_ccn_pmu_event_stop,
1289*1888d3ddSRobin Murphy 		.read = arm_ccn_pmu_event_read,
1290*1888d3ddSRobin Murphy 		.pmu_enable = arm_ccn_pmu_enable,
1291*1888d3ddSRobin Murphy 		.pmu_disable = arm_ccn_pmu_disable,
1292*1888d3ddSRobin Murphy 	};
1293*1888d3ddSRobin Murphy 
1294*1888d3ddSRobin Murphy 	/* No overflow interrupt? Have to use a timer instead. */
1295*1888d3ddSRobin Murphy 	if (!ccn->irq) {
1296*1888d3ddSRobin Murphy 		dev_info(ccn->dev, "No access to interrupts, using timer.\n");
1297*1888d3ddSRobin Murphy 		hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
1298*1888d3ddSRobin Murphy 				HRTIMER_MODE_REL);
1299*1888d3ddSRobin Murphy 		ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
1300*1888d3ddSRobin Murphy 	}
1301*1888d3ddSRobin Murphy 
1302*1888d3ddSRobin Murphy 	/* Pick one CPU which we will use to collect data from CCN... */
1303*1888d3ddSRobin Murphy 	cpumask_set_cpu(get_cpu(), &ccn->dt.cpu);
1304*1888d3ddSRobin Murphy 
1305*1888d3ddSRobin Murphy 	/* Also make sure that the overflow interrupt is handled by this CPU */
1306*1888d3ddSRobin Murphy 	if (ccn->irq) {
1307*1888d3ddSRobin Murphy 		err = irq_set_affinity_hint(ccn->irq, &ccn->dt.cpu);
1308*1888d3ddSRobin Murphy 		if (err) {
1309*1888d3ddSRobin Murphy 			dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
1310*1888d3ddSRobin Murphy 			goto error_set_affinity;
1311*1888d3ddSRobin Murphy 		}
1312*1888d3ddSRobin Murphy 	}
1313*1888d3ddSRobin Murphy 
1314*1888d3ddSRobin Murphy 	err = perf_pmu_register(&ccn->dt.pmu, name, -1);
1315*1888d3ddSRobin Murphy 	if (err)
1316*1888d3ddSRobin Murphy 		goto error_pmu_register;
1317*1888d3ddSRobin Murphy 
1318*1888d3ddSRobin Murphy 	cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
1319*1888d3ddSRobin Murphy 					 &ccn->dt.node);
1320*1888d3ddSRobin Murphy 	put_cpu();
1321*1888d3ddSRobin Murphy 	return 0;
1322*1888d3ddSRobin Murphy 
1323*1888d3ddSRobin Murphy error_pmu_register:
1324*1888d3ddSRobin Murphy error_set_affinity:
1325*1888d3ddSRobin Murphy 	put_cpu();
1326*1888d3ddSRobin Murphy error_choose_name:
1327*1888d3ddSRobin Murphy 	ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1328*1888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++)
1329*1888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1330*1888d3ddSRobin Murphy 	writel(0, ccn->dt.base + CCN_DT_PMCR);
1331*1888d3ddSRobin Murphy 	return err;
1332*1888d3ddSRobin Murphy }
1333*1888d3ddSRobin Murphy 
1334*1888d3ddSRobin Murphy static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
1335*1888d3ddSRobin Murphy {
1336*1888d3ddSRobin Murphy 	int i;
1337*1888d3ddSRobin Murphy 
1338*1888d3ddSRobin Murphy 	cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
1339*1888d3ddSRobin Murphy 					    &ccn->dt.node);
1340*1888d3ddSRobin Murphy 	if (ccn->irq)
1341*1888d3ddSRobin Murphy 		irq_set_affinity_hint(ccn->irq, NULL);
1342*1888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++)
1343*1888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1344*1888d3ddSRobin Murphy 	writel(0, ccn->dt.base + CCN_DT_PMCR);
1345*1888d3ddSRobin Murphy 	perf_pmu_unregister(&ccn->dt.pmu);
1346*1888d3ddSRobin Murphy 	ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1347*1888d3ddSRobin Murphy }
1348*1888d3ddSRobin Murphy 
1349*1888d3ddSRobin Murphy static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
1350*1888d3ddSRobin Murphy 		int (*callback)(struct arm_ccn *ccn, int region,
1351*1888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id))
1352*1888d3ddSRobin Murphy {
1353*1888d3ddSRobin Murphy 	int region;
1354*1888d3ddSRobin Murphy 
1355*1888d3ddSRobin Murphy 	for (region = 0; region < CCN_NUM_REGIONS; region++) {
1356*1888d3ddSRobin Murphy 		u32 val, type, id;
1357*1888d3ddSRobin Murphy 		void __iomem *base;
1358*1888d3ddSRobin Murphy 		int err;
1359*1888d3ddSRobin Murphy 
1360*1888d3ddSRobin Murphy 		val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
1361*1888d3ddSRobin Murphy 				4 * (region / 32));
1362*1888d3ddSRobin Murphy 		if (!(val & (1 << (region % 32))))
1363*1888d3ddSRobin Murphy 			continue;
1364*1888d3ddSRobin Murphy 
1365*1888d3ddSRobin Murphy 		base = ccn->base + region * CCN_REGION_SIZE;
1366*1888d3ddSRobin Murphy 		val = readl(base + CCN_ALL_OLY_ID);
1367*1888d3ddSRobin Murphy 		type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
1368*1888d3ddSRobin Murphy 				CCN_ALL_OLY_ID__OLY_ID__MASK;
1369*1888d3ddSRobin Murphy 		id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
1370*1888d3ddSRobin Murphy 				CCN_ALL_OLY_ID__NODE_ID__MASK;
1371*1888d3ddSRobin Murphy 
1372*1888d3ddSRobin Murphy 		err = callback(ccn, region, base, type, id);
1373*1888d3ddSRobin Murphy 		if (err)
1374*1888d3ddSRobin Murphy 			return err;
1375*1888d3ddSRobin Murphy 	}
1376*1888d3ddSRobin Murphy 
1377*1888d3ddSRobin Murphy 	return 0;
1378*1888d3ddSRobin Murphy }
1379*1888d3ddSRobin Murphy 
1380*1888d3ddSRobin Murphy static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
1381*1888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id)
1382*1888d3ddSRobin Murphy {
1383*1888d3ddSRobin Murphy 
1384*1888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP && id >= ccn->num_xps)
1385*1888d3ddSRobin Murphy 		ccn->num_xps = id + 1;
1386*1888d3ddSRobin Murphy 	else if (id >= ccn->num_nodes)
1387*1888d3ddSRobin Murphy 		ccn->num_nodes = id + 1;
1388*1888d3ddSRobin Murphy 
1389*1888d3ddSRobin Murphy 	return 0;
1390*1888d3ddSRobin Murphy }
1391*1888d3ddSRobin Murphy 
1392*1888d3ddSRobin Murphy static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
1393*1888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id)
1394*1888d3ddSRobin Murphy {
1395*1888d3ddSRobin Murphy 	struct arm_ccn_component *component;
1396*1888d3ddSRobin Murphy 
1397*1888d3ddSRobin Murphy 	dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
1398*1888d3ddSRobin Murphy 
1399*1888d3ddSRobin Murphy 	switch (type) {
1400*1888d3ddSRobin Murphy 	case CCN_TYPE_MN:
1401*1888d3ddSRobin Murphy 		ccn->mn_id = id;
1402*1888d3ddSRobin Murphy 		return 0;
1403*1888d3ddSRobin Murphy 	case CCN_TYPE_DT:
1404*1888d3ddSRobin Murphy 		return 0;
1405*1888d3ddSRobin Murphy 	case CCN_TYPE_XP:
1406*1888d3ddSRobin Murphy 		component = &ccn->xp[id];
1407*1888d3ddSRobin Murphy 		break;
1408*1888d3ddSRobin Murphy 	case CCN_TYPE_SBSX:
1409*1888d3ddSRobin Murphy 		ccn->sbsx_present = 1;
1410*1888d3ddSRobin Murphy 		component = &ccn->node[id];
1411*1888d3ddSRobin Murphy 		break;
1412*1888d3ddSRobin Murphy 	case CCN_TYPE_SBAS:
1413*1888d3ddSRobin Murphy 		ccn->sbas_present = 1;
1414*1888d3ddSRobin Murphy 		/* Fall-through */
1415*1888d3ddSRobin Murphy 	default:
1416*1888d3ddSRobin Murphy 		component = &ccn->node[id];
1417*1888d3ddSRobin Murphy 		break;
1418*1888d3ddSRobin Murphy 	}
1419*1888d3ddSRobin Murphy 
1420*1888d3ddSRobin Murphy 	component->base = base;
1421*1888d3ddSRobin Murphy 	component->type = type;
1422*1888d3ddSRobin Murphy 
1423*1888d3ddSRobin Murphy 	return 0;
1424*1888d3ddSRobin Murphy }
1425*1888d3ddSRobin Murphy 
1426*1888d3ddSRobin Murphy 
1427*1888d3ddSRobin Murphy static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
1428*1888d3ddSRobin Murphy 		const u32 *err_sig_val)
1429*1888d3ddSRobin Murphy {
1430*1888d3ddSRobin Murphy 	/* This should be really handled by firmware... */
1431*1888d3ddSRobin Murphy 	dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
1432*1888d3ddSRobin Murphy 			err_sig_val[5], err_sig_val[4], err_sig_val[3],
1433*1888d3ddSRobin Murphy 			err_sig_val[2], err_sig_val[1], err_sig_val[0]);
1434*1888d3ddSRobin Murphy 	dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
1435*1888d3ddSRobin Murphy 	writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
1436*1888d3ddSRobin Murphy 			ccn->base + CCN_MN_ERRINT_STATUS);
1437*1888d3ddSRobin Murphy 
1438*1888d3ddSRobin Murphy 	return IRQ_HANDLED;
1439*1888d3ddSRobin Murphy }
1440*1888d3ddSRobin Murphy 
1441*1888d3ddSRobin Murphy 
1442*1888d3ddSRobin Murphy static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
1443*1888d3ddSRobin Murphy {
1444*1888d3ddSRobin Murphy 	irqreturn_t res = IRQ_NONE;
1445*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = dev_id;
1446*1888d3ddSRobin Murphy 	u32 err_sig_val[6];
1447*1888d3ddSRobin Murphy 	u32 err_or;
1448*1888d3ddSRobin Murphy 	int i;
1449*1888d3ddSRobin Murphy 
1450*1888d3ddSRobin Murphy 	/* PMU overflow is a special case */
1451*1888d3ddSRobin Murphy 	err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
1452*1888d3ddSRobin Murphy 	if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
1453*1888d3ddSRobin Murphy 		err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
1454*1888d3ddSRobin Murphy 		res = arm_ccn_pmu_overflow_handler(&ccn->dt);
1455*1888d3ddSRobin Murphy 	}
1456*1888d3ddSRobin Murphy 
1457*1888d3ddSRobin Murphy 	/* Have to read all err_sig_vals to clear them */
1458*1888d3ddSRobin Murphy 	for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
1459*1888d3ddSRobin Murphy 		err_sig_val[i] = readl(ccn->base +
1460*1888d3ddSRobin Murphy 				CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
1461*1888d3ddSRobin Murphy 		err_or |= err_sig_val[i];
1462*1888d3ddSRobin Murphy 	}
1463*1888d3ddSRobin Murphy 	if (err_or)
1464*1888d3ddSRobin Murphy 		res |= arm_ccn_error_handler(ccn, err_sig_val);
1465*1888d3ddSRobin Murphy 
1466*1888d3ddSRobin Murphy 	if (res != IRQ_NONE)
1467*1888d3ddSRobin Murphy 		writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
1468*1888d3ddSRobin Murphy 				ccn->base + CCN_MN_ERRINT_STATUS);
1469*1888d3ddSRobin Murphy 
1470*1888d3ddSRobin Murphy 	return res;
1471*1888d3ddSRobin Murphy }
1472*1888d3ddSRobin Murphy 
1473*1888d3ddSRobin Murphy 
1474*1888d3ddSRobin Murphy static int arm_ccn_probe(struct platform_device *pdev)
1475*1888d3ddSRobin Murphy {
1476*1888d3ddSRobin Murphy 	struct arm_ccn *ccn;
1477*1888d3ddSRobin Murphy 	struct resource *res;
1478*1888d3ddSRobin Murphy 	unsigned int irq;
1479*1888d3ddSRobin Murphy 	int err;
1480*1888d3ddSRobin Murphy 
1481*1888d3ddSRobin Murphy 	ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
1482*1888d3ddSRobin Murphy 	if (!ccn)
1483*1888d3ddSRobin Murphy 		return -ENOMEM;
1484*1888d3ddSRobin Murphy 	ccn->dev = &pdev->dev;
1485*1888d3ddSRobin Murphy 	platform_set_drvdata(pdev, ccn);
1486*1888d3ddSRobin Murphy 
1487*1888d3ddSRobin Murphy 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1488*1888d3ddSRobin Murphy 	if (!res)
1489*1888d3ddSRobin Murphy 		return -EINVAL;
1490*1888d3ddSRobin Murphy 
1491*1888d3ddSRobin Murphy 	if (!devm_request_mem_region(ccn->dev, res->start,
1492*1888d3ddSRobin Murphy 			resource_size(res), pdev->name))
1493*1888d3ddSRobin Murphy 		return -EBUSY;
1494*1888d3ddSRobin Murphy 
1495*1888d3ddSRobin Murphy 	ccn->base = devm_ioremap(ccn->dev, res->start,
1496*1888d3ddSRobin Murphy 				resource_size(res));
1497*1888d3ddSRobin Murphy 	if (!ccn->base)
1498*1888d3ddSRobin Murphy 		return -EFAULT;
1499*1888d3ddSRobin Murphy 
1500*1888d3ddSRobin Murphy 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1501*1888d3ddSRobin Murphy 	if (!res)
1502*1888d3ddSRobin Murphy 		return -EINVAL;
1503*1888d3ddSRobin Murphy 	irq = res->start;
1504*1888d3ddSRobin Murphy 
1505*1888d3ddSRobin Murphy 	/* Check if we can use the interrupt */
1506*1888d3ddSRobin Murphy 	writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
1507*1888d3ddSRobin Murphy 			ccn->base + CCN_MN_ERRINT_STATUS);
1508*1888d3ddSRobin Murphy 	if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
1509*1888d3ddSRobin Murphy 			CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
1510*1888d3ddSRobin Murphy 		/* Can set 'disable' bits, so can acknowledge interrupts */
1511*1888d3ddSRobin Murphy 		writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
1512*1888d3ddSRobin Murphy 				ccn->base + CCN_MN_ERRINT_STATUS);
1513*1888d3ddSRobin Murphy 		err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler,
1514*1888d3ddSRobin Murphy 				       IRQF_NOBALANCING | IRQF_NO_THREAD,
1515*1888d3ddSRobin Murphy 				       dev_name(ccn->dev), ccn);
1516*1888d3ddSRobin Murphy 		if (err)
1517*1888d3ddSRobin Murphy 			return err;
1518*1888d3ddSRobin Murphy 
1519*1888d3ddSRobin Murphy 		ccn->irq = irq;
1520*1888d3ddSRobin Murphy 	}
1521*1888d3ddSRobin Murphy 
1522*1888d3ddSRobin Murphy 
1523*1888d3ddSRobin Murphy 	/* Build topology */
1524*1888d3ddSRobin Murphy 
1525*1888d3ddSRobin Murphy 	err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
1526*1888d3ddSRobin Murphy 	if (err)
1527*1888d3ddSRobin Murphy 		return err;
1528*1888d3ddSRobin Murphy 
1529*1888d3ddSRobin Murphy 	ccn->node = devm_kcalloc(ccn->dev, ccn->num_nodes, sizeof(*ccn->node),
1530*1888d3ddSRobin Murphy 				 GFP_KERNEL);
1531*1888d3ddSRobin Murphy 	ccn->xp = devm_kcalloc(ccn->dev, ccn->num_xps, sizeof(*ccn->node),
1532*1888d3ddSRobin Murphy 			       GFP_KERNEL);
1533*1888d3ddSRobin Murphy 	if (!ccn->node || !ccn->xp)
1534*1888d3ddSRobin Murphy 		return -ENOMEM;
1535*1888d3ddSRobin Murphy 
1536*1888d3ddSRobin Murphy 	err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
1537*1888d3ddSRobin Murphy 	if (err)
1538*1888d3ddSRobin Murphy 		return err;
1539*1888d3ddSRobin Murphy 
1540*1888d3ddSRobin Murphy 	return arm_ccn_pmu_init(ccn);
1541*1888d3ddSRobin Murphy }
1542*1888d3ddSRobin Murphy 
1543*1888d3ddSRobin Murphy static int arm_ccn_remove(struct platform_device *pdev)
1544*1888d3ddSRobin Murphy {
1545*1888d3ddSRobin Murphy 	struct arm_ccn *ccn = platform_get_drvdata(pdev);
1546*1888d3ddSRobin Murphy 
1547*1888d3ddSRobin Murphy 	arm_ccn_pmu_cleanup(ccn);
1548*1888d3ddSRobin Murphy 
1549*1888d3ddSRobin Murphy 	return 0;
1550*1888d3ddSRobin Murphy }
1551*1888d3ddSRobin Murphy 
1552*1888d3ddSRobin Murphy static const struct of_device_id arm_ccn_match[] = {
1553*1888d3ddSRobin Murphy 	{ .compatible = "arm,ccn-502", },
1554*1888d3ddSRobin Murphy 	{ .compatible = "arm,ccn-504", },
1555*1888d3ddSRobin Murphy 	{},
1556*1888d3ddSRobin Murphy };
1557*1888d3ddSRobin Murphy MODULE_DEVICE_TABLE(of, arm_ccn_match);
1558*1888d3ddSRobin Murphy 
1559*1888d3ddSRobin Murphy static struct platform_driver arm_ccn_driver = {
1560*1888d3ddSRobin Murphy 	.driver = {
1561*1888d3ddSRobin Murphy 		.name = "arm-ccn",
1562*1888d3ddSRobin Murphy 		.of_match_table = arm_ccn_match,
1563*1888d3ddSRobin Murphy 	},
1564*1888d3ddSRobin Murphy 	.probe = arm_ccn_probe,
1565*1888d3ddSRobin Murphy 	.remove = arm_ccn_remove,
1566*1888d3ddSRobin Murphy };
1567*1888d3ddSRobin Murphy 
1568*1888d3ddSRobin Murphy static int __init arm_ccn_init(void)
1569*1888d3ddSRobin Murphy {
1570*1888d3ddSRobin Murphy 	int i, ret;
1571*1888d3ddSRobin Murphy 
1572*1888d3ddSRobin Murphy 	ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE,
1573*1888d3ddSRobin Murphy 				      "perf/arm/ccn:online", NULL,
1574*1888d3ddSRobin Murphy 				      arm_ccn_pmu_offline_cpu);
1575*1888d3ddSRobin Murphy 	if (ret)
1576*1888d3ddSRobin Murphy 		return ret;
1577*1888d3ddSRobin Murphy 
1578*1888d3ddSRobin Murphy 	for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
1579*1888d3ddSRobin Murphy 		arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
1580*1888d3ddSRobin Murphy 
1581*1888d3ddSRobin Murphy 	ret = platform_driver_register(&arm_ccn_driver);
1582*1888d3ddSRobin Murphy 	if (ret)
1583*1888d3ddSRobin Murphy 		cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
1584*1888d3ddSRobin Murphy 	return ret;
1585*1888d3ddSRobin Murphy }
1586*1888d3ddSRobin Murphy 
1587*1888d3ddSRobin Murphy static void __exit arm_ccn_exit(void)
1588*1888d3ddSRobin Murphy {
1589*1888d3ddSRobin Murphy 	platform_driver_unregister(&arm_ccn_driver);
1590*1888d3ddSRobin Murphy 	cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
1591*1888d3ddSRobin Murphy }
1592*1888d3ddSRobin Murphy 
1593*1888d3ddSRobin Murphy module_init(arm_ccn_init);
1594*1888d3ddSRobin Murphy module_exit(arm_ccn_exit);
1595*1888d3ddSRobin Murphy 
1596*1888d3ddSRobin Murphy MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
1597*1888d3ddSRobin Murphy MODULE_LICENSE("GPL");
1598