xref: /linux/drivers/perf/arm-ccn.c (revision 1802d0beecafe581ad584634ba92f8a471d8a63a)
1*1802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21888d3ddSRobin Murphy /*
31888d3ddSRobin Murphy  *
41888d3ddSRobin Murphy  * Copyright (C) 2014 ARM Limited
51888d3ddSRobin Murphy  */
61888d3ddSRobin Murphy 
71888d3ddSRobin Murphy #include <linux/ctype.h>
81888d3ddSRobin Murphy #include <linux/hrtimer.h>
91888d3ddSRobin Murphy #include <linux/idr.h>
101888d3ddSRobin Murphy #include <linux/interrupt.h>
111888d3ddSRobin Murphy #include <linux/io.h>
121888d3ddSRobin Murphy #include <linux/module.h>
13ac316725SRandy Dunlap #include <linux/mod_devicetable.h>
141888d3ddSRobin Murphy #include <linux/perf_event.h>
151888d3ddSRobin Murphy #include <linux/platform_device.h>
161888d3ddSRobin Murphy #include <linux/slab.h>
171888d3ddSRobin Murphy 
181888d3ddSRobin Murphy #define CCN_NUM_XP_PORTS 2
191888d3ddSRobin Murphy #define CCN_NUM_VCS 4
201888d3ddSRobin Murphy #define CCN_NUM_REGIONS	256
211888d3ddSRobin Murphy #define CCN_REGION_SIZE	0x10000
221888d3ddSRobin Murphy 
231888d3ddSRobin Murphy #define CCN_ALL_OLY_ID			0xff00
241888d3ddSRobin Murphy #define CCN_ALL_OLY_ID__OLY_ID__SHIFT			0
251888d3ddSRobin Murphy #define CCN_ALL_OLY_ID__OLY_ID__MASK			0x1f
261888d3ddSRobin Murphy #define CCN_ALL_OLY_ID__NODE_ID__SHIFT			8
271888d3ddSRobin Murphy #define CCN_ALL_OLY_ID__NODE_ID__MASK			0x3f
281888d3ddSRobin Murphy 
291888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS		0x0008
301888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__INTREQ__DESSERT		0x11
311888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__ENABLE	0x02
321888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLED	0x20
331888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE	0x22
341888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_ENABLE	0x04
351888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLED	0x40
361888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLE	0x44
371888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE	0x08
381888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED	0x80
391888d3ddSRobin Murphy #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE	0x88
401888d3ddSRobin Murphy #define CCN_MN_OLY_COMP_LIST_63_0	0x01e0
411888d3ddSRobin Murphy #define CCN_MN_ERR_SIG_VAL_63_0		0x0300
421888d3ddSRobin Murphy #define CCN_MN_ERR_SIG_VAL_63_0__DT			(1 << 1)
431888d3ddSRobin Murphy 
441888d3ddSRobin Murphy #define CCN_DT_ACTIVE_DSM		0x0000
451888d3ddSRobin Murphy #define CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(n)		((n) * 8)
461888d3ddSRobin Murphy #define CCN_DT_ACTIVE_DSM__DSM_ID__MASK			0xff
471888d3ddSRobin Murphy #define CCN_DT_CTL			0x0028
481888d3ddSRobin Murphy #define CCN_DT_CTL__DT_EN				(1 << 0)
491888d3ddSRobin Murphy #define CCN_DT_PMEVCNT(n)		(0x0100 + (n) * 0x8)
501888d3ddSRobin Murphy #define CCN_DT_PMCCNTR			0x0140
511888d3ddSRobin Murphy #define CCN_DT_PMCCNTRSR		0x0190
521888d3ddSRobin Murphy #define CCN_DT_PMOVSR			0x0198
531888d3ddSRobin Murphy #define CCN_DT_PMOVSR_CLR		0x01a0
541888d3ddSRobin Murphy #define CCN_DT_PMOVSR_CLR__MASK				0x1f
551888d3ddSRobin Murphy #define CCN_DT_PMCR			0x01a8
561888d3ddSRobin Murphy #define CCN_DT_PMCR__OVFL_INTR_EN			(1 << 6)
571888d3ddSRobin Murphy #define CCN_DT_PMCR__PMU_EN				(1 << 0)
581888d3ddSRobin Murphy #define CCN_DT_PMSR			0x01b0
591888d3ddSRobin Murphy #define CCN_DT_PMSR_REQ			0x01b8
601888d3ddSRobin Murphy #define CCN_DT_PMSR_CLR			0x01c0
611888d3ddSRobin Murphy 
621888d3ddSRobin Murphy #define CCN_HNF_PMU_EVENT_SEL		0x0600
631888d3ddSRobin Murphy #define CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
641888d3ddSRobin Murphy #define CCN_HNF_PMU_EVENT_SEL__ID__MASK			0xf
651888d3ddSRobin Murphy 
661888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG		0x0300
671888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__SHIFT(n)		((n) * 4)
681888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__MASK			0xf
691888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH		0x0
701888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT_0_OR_1	0x1
711888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(n)		(0x2 + (n))
721888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(n)	(0x4 + (n))
731888d3ddSRobin Murphy #define CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(d, n) (0x8 + (d) * 4 + (n))
741888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL		0x0308
751888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(n)	(0 + (n) * 8)
761888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK	0x1
771888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(n)	(1 + (n) * 8)
781888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK	0x1
791888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(n)	(2 + (n) * 8)
801888d3ddSRobin Murphy #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK	0x3
811888d3ddSRobin Murphy #define CCN_XP_DT_CMP_VAL_L(n)		(0x0310 + (n) * 0x40)
821888d3ddSRobin Murphy #define CCN_XP_DT_CMP_VAL_H(n)		(0x0318 + (n) * 0x40)
831888d3ddSRobin Murphy #define CCN_XP_DT_CMP_MASK_L(n)		(0x0320 + (n) * 0x40)
841888d3ddSRobin Murphy #define CCN_XP_DT_CMP_MASK_H(n)		(0x0328 + (n) * 0x40)
851888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL		0x0370
861888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL__DT_ENABLE			(1 << 0)
871888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(n)		(12 + (n) * 4)
881888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL__WP_ARM_SEL__MASK		0xf
891888d3ddSRobin Murphy #define CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS		0xf
901888d3ddSRobin Murphy #define CCN_XP_PMU_EVENT_SEL		0x0600
911888d3ddSRobin Murphy #define CCN_XP_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 7)
921888d3ddSRobin Murphy #define CCN_XP_PMU_EVENT_SEL__ID__MASK			0x3f
931888d3ddSRobin Murphy 
941888d3ddSRobin Murphy #define CCN_SBAS_PMU_EVENT_SEL		0x0600
951888d3ddSRobin Murphy #define CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
961888d3ddSRobin Murphy #define CCN_SBAS_PMU_EVENT_SEL__ID__MASK		0xf
971888d3ddSRobin Murphy 
981888d3ddSRobin Murphy #define CCN_RNI_PMU_EVENT_SEL		0x0600
991888d3ddSRobin Murphy #define CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
1001888d3ddSRobin Murphy #define CCN_RNI_PMU_EVENT_SEL__ID__MASK			0xf
1011888d3ddSRobin Murphy 
1021888d3ddSRobin Murphy #define CCN_TYPE_MN	0x01
1031888d3ddSRobin Murphy #define CCN_TYPE_DT	0x02
1041888d3ddSRobin Murphy #define CCN_TYPE_HNF	0x04
1051888d3ddSRobin Murphy #define CCN_TYPE_HNI	0x05
1061888d3ddSRobin Murphy #define CCN_TYPE_XP	0x08
1071888d3ddSRobin Murphy #define CCN_TYPE_SBSX	0x0c
1081888d3ddSRobin Murphy #define CCN_TYPE_SBAS	0x10
1091888d3ddSRobin Murphy #define CCN_TYPE_RNI_1P	0x14
1101888d3ddSRobin Murphy #define CCN_TYPE_RNI_2P	0x15
1111888d3ddSRobin Murphy #define CCN_TYPE_RNI_3P	0x16
1121888d3ddSRobin Murphy #define CCN_TYPE_RND_1P	0x18 /* RN-D = RN-I + DVM */
1131888d3ddSRobin Murphy #define CCN_TYPE_RND_2P	0x19
1141888d3ddSRobin Murphy #define CCN_TYPE_RND_3P	0x1a
1151888d3ddSRobin Murphy #define CCN_TYPE_CYCLES	0xff /* Pseudotype */
1161888d3ddSRobin Murphy 
1171888d3ddSRobin Murphy #define CCN_EVENT_WATCHPOINT 0xfe /* Pseudoevent */
1181888d3ddSRobin Murphy 
1191888d3ddSRobin Murphy #define CCN_NUM_PMU_EVENTS		4
1201888d3ddSRobin Murphy #define CCN_NUM_XP_WATCHPOINTS		2 /* See DT.dbg_id.num_watchpoints */
1211888d3ddSRobin Murphy #define CCN_NUM_PMU_EVENT_COUNTERS	8 /* See DT.dbg_id.num_pmucntr */
1221888d3ddSRobin Murphy #define CCN_IDX_PMU_CYCLE_COUNTER	CCN_NUM_PMU_EVENT_COUNTERS
1231888d3ddSRobin Murphy 
1241888d3ddSRobin Murphy #define CCN_NUM_PREDEFINED_MASKS	4
1251888d3ddSRobin Murphy #define CCN_IDX_MASK_ANY		(CCN_NUM_PMU_EVENT_COUNTERS + 0)
1261888d3ddSRobin Murphy #define CCN_IDX_MASK_EXACT		(CCN_NUM_PMU_EVENT_COUNTERS + 1)
1271888d3ddSRobin Murphy #define CCN_IDX_MASK_ORDER		(CCN_NUM_PMU_EVENT_COUNTERS + 2)
1281888d3ddSRobin Murphy #define CCN_IDX_MASK_OPCODE		(CCN_NUM_PMU_EVENT_COUNTERS + 3)
1291888d3ddSRobin Murphy 
1301888d3ddSRobin Murphy struct arm_ccn_component {
1311888d3ddSRobin Murphy 	void __iomem *base;
1321888d3ddSRobin Murphy 	u32 type;
1331888d3ddSRobin Murphy 
1341888d3ddSRobin Murphy 	DECLARE_BITMAP(pmu_events_mask, CCN_NUM_PMU_EVENTS);
1351888d3ddSRobin Murphy 	union {
1361888d3ddSRobin Murphy 		struct {
1371888d3ddSRobin Murphy 			DECLARE_BITMAP(dt_cmp_mask, CCN_NUM_XP_WATCHPOINTS);
1381888d3ddSRobin Murphy 		} xp;
1391888d3ddSRobin Murphy 	};
1401888d3ddSRobin Murphy };
1411888d3ddSRobin Murphy 
1421888d3ddSRobin Murphy #define pmu_to_arm_ccn(_pmu) container_of(container_of(_pmu, \
1431888d3ddSRobin Murphy 	struct arm_ccn_dt, pmu), struct arm_ccn, dt)
1441888d3ddSRobin Murphy 
1451888d3ddSRobin Murphy struct arm_ccn_dt {
1461888d3ddSRobin Murphy 	int id;
1471888d3ddSRobin Murphy 	void __iomem *base;
1481888d3ddSRobin Murphy 
1491888d3ddSRobin Murphy 	spinlock_t config_lock;
1501888d3ddSRobin Murphy 
1511888d3ddSRobin Murphy 	DECLARE_BITMAP(pmu_counters_mask, CCN_NUM_PMU_EVENT_COUNTERS + 1);
1521888d3ddSRobin Murphy 	struct {
1531888d3ddSRobin Murphy 		struct arm_ccn_component *source;
1541888d3ddSRobin Murphy 		struct perf_event *event;
1551888d3ddSRobin Murphy 	} pmu_counters[CCN_NUM_PMU_EVENT_COUNTERS + 1];
1561888d3ddSRobin Murphy 
1571888d3ddSRobin Murphy 	struct {
1581888d3ddSRobin Murphy 	       u64 l, h;
1591888d3ddSRobin Murphy 	} cmp_mask[CCN_NUM_PMU_EVENT_COUNTERS + CCN_NUM_PREDEFINED_MASKS];
1601888d3ddSRobin Murphy 
1611888d3ddSRobin Murphy 	struct hrtimer hrtimer;
1621888d3ddSRobin Murphy 
1639bcb929fSRobin Murphy 	unsigned int cpu;
1641888d3ddSRobin Murphy 	struct hlist_node node;
1651888d3ddSRobin Murphy 
1661888d3ddSRobin Murphy 	struct pmu pmu;
1671888d3ddSRobin Murphy };
1681888d3ddSRobin Murphy 
1691888d3ddSRobin Murphy struct arm_ccn {
1701888d3ddSRobin Murphy 	struct device *dev;
1711888d3ddSRobin Murphy 	void __iomem *base;
1721888d3ddSRobin Murphy 	unsigned int irq;
1731888d3ddSRobin Murphy 
1741888d3ddSRobin Murphy 	unsigned sbas_present:1;
1751888d3ddSRobin Murphy 	unsigned sbsx_present:1;
1761888d3ddSRobin Murphy 
1771888d3ddSRobin Murphy 	int num_nodes;
1781888d3ddSRobin Murphy 	struct arm_ccn_component *node;
1791888d3ddSRobin Murphy 
1801888d3ddSRobin Murphy 	int num_xps;
1811888d3ddSRobin Murphy 	struct arm_ccn_component *xp;
1821888d3ddSRobin Murphy 
1831888d3ddSRobin Murphy 	struct arm_ccn_dt dt;
1841888d3ddSRobin Murphy 	int mn_id;
1851888d3ddSRobin Murphy };
1861888d3ddSRobin Murphy 
1871888d3ddSRobin Murphy static int arm_ccn_node_to_xp(int node)
1881888d3ddSRobin Murphy {
1891888d3ddSRobin Murphy 	return node / CCN_NUM_XP_PORTS;
1901888d3ddSRobin Murphy }
1911888d3ddSRobin Murphy 
1921888d3ddSRobin Murphy static int arm_ccn_node_to_xp_port(int node)
1931888d3ddSRobin Murphy {
1941888d3ddSRobin Murphy 	return node % CCN_NUM_XP_PORTS;
1951888d3ddSRobin Murphy }
1961888d3ddSRobin Murphy 
1971888d3ddSRobin Murphy 
1981888d3ddSRobin Murphy /*
1991888d3ddSRobin Murphy  * Bit shifts and masks in these defines must be kept in sync with
2001888d3ddSRobin Murphy  * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
2011888d3ddSRobin Murphy  */
2021888d3ddSRobin Murphy #define CCN_CONFIG_NODE(_config)	(((_config) >> 0) & 0xff)
2031888d3ddSRobin Murphy #define CCN_CONFIG_XP(_config)		(((_config) >> 0) & 0xff)
2041888d3ddSRobin Murphy #define CCN_CONFIG_TYPE(_config)	(((_config) >> 8) & 0xff)
2051888d3ddSRobin Murphy #define CCN_CONFIG_EVENT(_config)	(((_config) >> 16) & 0xff)
2061888d3ddSRobin Murphy #define CCN_CONFIG_PORT(_config)	(((_config) >> 24) & 0x3)
2071888d3ddSRobin Murphy #define CCN_CONFIG_BUS(_config)		(((_config) >> 24) & 0x3)
2081888d3ddSRobin Murphy #define CCN_CONFIG_VC(_config)		(((_config) >> 26) & 0x7)
2091888d3ddSRobin Murphy #define CCN_CONFIG_DIR(_config)		(((_config) >> 29) & 0x1)
2101888d3ddSRobin Murphy #define CCN_CONFIG_MASK(_config)	(((_config) >> 30) & 0xf)
2111888d3ddSRobin Murphy 
2121888d3ddSRobin Murphy static void arm_ccn_pmu_config_set(u64 *config, u32 node_xp, u32 type, u32 port)
2131888d3ddSRobin Murphy {
2141888d3ddSRobin Murphy 	*config &= ~((0xff << 0) | (0xff << 8) | (0x3 << 24));
2151888d3ddSRobin Murphy 	*config |= (node_xp << 0) | (type << 8) | (port << 24);
2161888d3ddSRobin Murphy }
2171888d3ddSRobin Murphy 
2181888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_format_show(struct device *dev,
2191888d3ddSRobin Murphy 		struct device_attribute *attr, char *buf)
2201888d3ddSRobin Murphy {
2211888d3ddSRobin Murphy 	struct dev_ext_attribute *ea = container_of(attr,
2221888d3ddSRobin Murphy 			struct dev_ext_attribute, attr);
2231888d3ddSRobin Murphy 
2241888d3ddSRobin Murphy 	return snprintf(buf, PAGE_SIZE, "%s\n", (char *)ea->var);
2251888d3ddSRobin Murphy }
2261888d3ddSRobin Murphy 
2271888d3ddSRobin Murphy #define CCN_FORMAT_ATTR(_name, _config) \
2281888d3ddSRobin Murphy 	struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
2291888d3ddSRobin Murphy 			{ __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
2301888d3ddSRobin Murphy 			NULL), _config }
2311888d3ddSRobin Murphy 
2321888d3ddSRobin Murphy static CCN_FORMAT_ATTR(node, "config:0-7");
2331888d3ddSRobin Murphy static CCN_FORMAT_ATTR(xp, "config:0-7");
2341888d3ddSRobin Murphy static CCN_FORMAT_ATTR(type, "config:8-15");
2351888d3ddSRobin Murphy static CCN_FORMAT_ATTR(event, "config:16-23");
2361888d3ddSRobin Murphy static CCN_FORMAT_ATTR(port, "config:24-25");
2371888d3ddSRobin Murphy static CCN_FORMAT_ATTR(bus, "config:24-25");
2381888d3ddSRobin Murphy static CCN_FORMAT_ATTR(vc, "config:26-28");
2391888d3ddSRobin Murphy static CCN_FORMAT_ATTR(dir, "config:29-29");
2401888d3ddSRobin Murphy static CCN_FORMAT_ATTR(mask, "config:30-33");
2411888d3ddSRobin Murphy static CCN_FORMAT_ATTR(cmp_l, "config1:0-62");
2421888d3ddSRobin Murphy static CCN_FORMAT_ATTR(cmp_h, "config2:0-59");
2431888d3ddSRobin Murphy 
2441888d3ddSRobin Murphy static struct attribute *arm_ccn_pmu_format_attrs[] = {
2451888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_node.attr.attr,
2461888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_xp.attr.attr,
2471888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_type.attr.attr,
2481888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_event.attr.attr,
2491888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_port.attr.attr,
2501888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_bus.attr.attr,
2511888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_vc.attr.attr,
2521888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_dir.attr.attr,
2531888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_mask.attr.attr,
2541888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_cmp_l.attr.attr,
2551888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_cmp_h.attr.attr,
2561888d3ddSRobin Murphy 	NULL
2571888d3ddSRobin Murphy };
2581888d3ddSRobin Murphy 
2591888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_format_attr_group = {
2601888d3ddSRobin Murphy 	.name = "format",
2611888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_format_attrs,
2621888d3ddSRobin Murphy };
2631888d3ddSRobin Murphy 
2641888d3ddSRobin Murphy 
2651888d3ddSRobin Murphy struct arm_ccn_pmu_event {
2661888d3ddSRobin Murphy 	struct device_attribute attr;
2671888d3ddSRobin Murphy 	u32 type;
2681888d3ddSRobin Murphy 	u32 event;
2691888d3ddSRobin Murphy 	int num_ports;
2701888d3ddSRobin Murphy 	int num_vcs;
2711888d3ddSRobin Murphy 	const char *def;
2721888d3ddSRobin Murphy 	int mask;
2731888d3ddSRobin Murphy };
2741888d3ddSRobin Murphy 
2751888d3ddSRobin Murphy #define CCN_EVENT_ATTR(_name) \
2761888d3ddSRobin Murphy 	__ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
2771888d3ddSRobin Murphy 
2781888d3ddSRobin Murphy /*
2791888d3ddSRobin Murphy  * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
2801888d3ddSRobin Murphy  * their ports in XP they are connected to. For the sake of usability they are
2811888d3ddSRobin Murphy  * explicitly defined here (and translated into a relevant watchpoint in
2821888d3ddSRobin Murphy  * arm_ccn_pmu_event_init()) so the user can easily request them without deep
2831888d3ddSRobin Murphy  * knowledge of the flit format.
2841888d3ddSRobin Murphy  */
2851888d3ddSRobin Murphy 
2861888d3ddSRobin Murphy #define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
2871888d3ddSRobin Murphy 		.type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
2881888d3ddSRobin Murphy 		.num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
2891888d3ddSRobin Murphy 		.def = _def, .mask = _mask, }
2901888d3ddSRobin Murphy 
2911888d3ddSRobin Murphy #define CCN_EVENT_HNI(_name, _def, _mask) { \
2921888d3ddSRobin Murphy 		.attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
2931888d3ddSRobin Murphy 		.event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
2941888d3ddSRobin Murphy 		.num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
2951888d3ddSRobin Murphy 
2961888d3ddSRobin Murphy #define CCN_EVENT_SBSX(_name, _def, _mask) { \
2971888d3ddSRobin Murphy 		.attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
2981888d3ddSRobin Murphy 		.event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
2991888d3ddSRobin Murphy 		.num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
3001888d3ddSRobin Murphy 
3011888d3ddSRobin Murphy #define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
3021888d3ddSRobin Murphy 		.type = CCN_TYPE_HNF, .event = _event, }
3031888d3ddSRobin Murphy 
3041888d3ddSRobin Murphy #define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
3051888d3ddSRobin Murphy 		.type = CCN_TYPE_XP, .event = _event, \
3061888d3ddSRobin Murphy 		.num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
3071888d3ddSRobin Murphy 
3081888d3ddSRobin Murphy /*
3091888d3ddSRobin Murphy  * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
3101888d3ddSRobin Murphy  * on configuration. One of them is picked to represent the whole group,
3111888d3ddSRobin Murphy  * as they all share the same event types.
3121888d3ddSRobin Murphy  */
3131888d3ddSRobin Murphy #define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
3141888d3ddSRobin Murphy 		.type = CCN_TYPE_RNI_3P, .event = _event, }
3151888d3ddSRobin Murphy 
3161888d3ddSRobin Murphy #define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
3171888d3ddSRobin Murphy 		.type = CCN_TYPE_SBAS, .event = _event, }
3181888d3ddSRobin Murphy 
3191888d3ddSRobin Murphy #define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
3201888d3ddSRobin Murphy 		.type = CCN_TYPE_CYCLES }
3211888d3ddSRobin Murphy 
3221888d3ddSRobin Murphy 
3231888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_event_show(struct device *dev,
3241888d3ddSRobin Murphy 		struct device_attribute *attr, char *buf)
3251888d3ddSRobin Murphy {
3261888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
3271888d3ddSRobin Murphy 	struct arm_ccn_pmu_event *event = container_of(attr,
3281888d3ddSRobin Murphy 			struct arm_ccn_pmu_event, attr);
3291888d3ddSRobin Murphy 	ssize_t res;
3301888d3ddSRobin Murphy 
3311888d3ddSRobin Murphy 	res = snprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
3321888d3ddSRobin Murphy 	if (event->event)
3331888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
3341888d3ddSRobin Murphy 				event->event);
3351888d3ddSRobin Murphy 	if (event->def)
3361888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",%s",
3371888d3ddSRobin Murphy 				event->def);
3381888d3ddSRobin Murphy 	if (event->mask)
3391888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
3401888d3ddSRobin Murphy 				event->mask);
3411888d3ddSRobin Murphy 
3421888d3ddSRobin Murphy 	/* Arguments required by an event */
3431888d3ddSRobin Murphy 	switch (event->type) {
3441888d3ddSRobin Murphy 	case CCN_TYPE_CYCLES:
3451888d3ddSRobin Murphy 		break;
3461888d3ddSRobin Murphy 	case CCN_TYPE_XP:
3471888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res,
3481888d3ddSRobin Murphy 				",xp=?,vc=?");
3491888d3ddSRobin Murphy 		if (event->event == CCN_EVENT_WATCHPOINT)
3501888d3ddSRobin Murphy 			res += snprintf(buf + res, PAGE_SIZE - res,
3511888d3ddSRobin Murphy 					",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
3521888d3ddSRobin Murphy 		else
3531888d3ddSRobin Murphy 			res += snprintf(buf + res, PAGE_SIZE - res,
3541888d3ddSRobin Murphy 					",bus=?");
3551888d3ddSRobin Murphy 
3561888d3ddSRobin Murphy 		break;
3571888d3ddSRobin Murphy 	case CCN_TYPE_MN:
3581888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",node=%d", ccn->mn_id);
3591888d3ddSRobin Murphy 		break;
3601888d3ddSRobin Murphy 	default:
3611888d3ddSRobin Murphy 		res += snprintf(buf + res, PAGE_SIZE - res, ",node=?");
3621888d3ddSRobin Murphy 		break;
3631888d3ddSRobin Murphy 	}
3641888d3ddSRobin Murphy 
3651888d3ddSRobin Murphy 	res += snprintf(buf + res, PAGE_SIZE - res, "\n");
3661888d3ddSRobin Murphy 
3671888d3ddSRobin Murphy 	return res;
3681888d3ddSRobin Murphy }
3691888d3ddSRobin Murphy 
3701888d3ddSRobin Murphy static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
3711888d3ddSRobin Murphy 				     struct attribute *attr, int index)
3721888d3ddSRobin Murphy {
3731888d3ddSRobin Murphy 	struct device *dev = kobj_to_dev(kobj);
3741888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
3751888d3ddSRobin Murphy 	struct device_attribute *dev_attr = container_of(attr,
3761888d3ddSRobin Murphy 			struct device_attribute, attr);
3771888d3ddSRobin Murphy 	struct arm_ccn_pmu_event *event = container_of(dev_attr,
3781888d3ddSRobin Murphy 			struct arm_ccn_pmu_event, attr);
3791888d3ddSRobin Murphy 
3801888d3ddSRobin Murphy 	if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
3811888d3ddSRobin Murphy 		return 0;
3821888d3ddSRobin Murphy 	if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
3831888d3ddSRobin Murphy 		return 0;
3841888d3ddSRobin Murphy 
3851888d3ddSRobin Murphy 	return attr->mode;
3861888d3ddSRobin Murphy }
3871888d3ddSRobin Murphy 
3881888d3ddSRobin Murphy static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
3891888d3ddSRobin Murphy 	CCN_EVENT_MN(eobarrier, "dir=1,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
3901888d3ddSRobin Murphy 	CCN_EVENT_MN(ecbarrier, "dir=1,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
3911888d3ddSRobin Murphy 	CCN_EVENT_MN(dvmop, "dir=1,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
3921888d3ddSRobin Murphy 	CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
3931888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
3941888d3ddSRobin Murphy 	CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
3951888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
3961888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
3971888d3ddSRobin Murphy 			CCN_IDX_MASK_ORDER),
3981888d3ddSRobin Murphy 	CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
3991888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
4001888d3ddSRobin Murphy 	CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
4011888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
4021888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
4031888d3ddSRobin Murphy 			CCN_IDX_MASK_ORDER),
4041888d3ddSRobin Murphy 	CCN_EVENT_HNF(cache_miss, 0x1),
4051888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
4061888d3ddSRobin Murphy 	CCN_EVENT_HNF(cache_fill, 0x3),
4071888d3ddSRobin Murphy 	CCN_EVENT_HNF(pocq_retry, 0x4),
4081888d3ddSRobin Murphy 	CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
4091888d3ddSRobin Murphy 	CCN_EVENT_HNF(sf_hit, 0x6),
4101888d3ddSRobin Murphy 	CCN_EVENT_HNF(sf_evictions, 0x7),
4111888d3ddSRobin Murphy 	CCN_EVENT_HNF(snoops_sent, 0x8),
4121888d3ddSRobin Murphy 	CCN_EVENT_HNF(snoops_broadcast, 0x9),
4131888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_eviction, 0xa),
4141888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
4151888d3ddSRobin Murphy 	CCN_EVENT_HNF(mc_retries, 0xc),
4161888d3ddSRobin Murphy 	CCN_EVENT_HNF(mc_reqs, 0xd),
4171888d3ddSRobin Murphy 	CCN_EVENT_HNF(qos_hh_retry, 0xe),
4181888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p0, 0x1),
4191888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p1, 0x2),
4201888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p2, 0x3),
4211888d3ddSRobin Murphy 	CCN_EVENT_RNI(rxdat_flits, 0x4),
4221888d3ddSRobin Murphy 	CCN_EVENT_RNI(txdat_flits, 0x5),
4231888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits, 0x6),
4241888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits_retried, 0x7),
4251888d3ddSRobin Murphy 	CCN_EVENT_RNI(rrt_full, 0x8),
4261888d3ddSRobin Murphy 	CCN_EVENT_RNI(wrt_full, 0x9),
4271888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
4281888d3ddSRobin Murphy 	CCN_EVENT_XP(upload_starvation, 0x1),
4291888d3ddSRobin Murphy 	CCN_EVENT_XP(download_starvation, 0x2),
4301888d3ddSRobin Murphy 	CCN_EVENT_XP(respin, 0x3),
4311888d3ddSRobin Murphy 	CCN_EVENT_XP(valid_flit, 0x4),
4321888d3ddSRobin Murphy 	CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
4331888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
4341888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rxdat_flits, 0x4),
4351888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txdat_flits, 0x5),
4361888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits, 0x6),
4371888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
4381888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rrt_full, 0x8),
4391888d3ddSRobin Murphy 	CCN_EVENT_SBAS(wrt_full, 0x9),
4401888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
4411888d3ddSRobin Murphy 	CCN_EVENT_CYCLES(cycles),
4421888d3ddSRobin Murphy };
4431888d3ddSRobin Murphy 
4441888d3ddSRobin Murphy /* Populated in arm_ccn_init() */
4451888d3ddSRobin Murphy static struct attribute
4461888d3ddSRobin Murphy 		*arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
4471888d3ddSRobin Murphy 
4481888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_events_attr_group = {
4491888d3ddSRobin Murphy 	.name = "events",
4501888d3ddSRobin Murphy 	.is_visible = arm_ccn_pmu_events_is_visible,
4511888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_events_attrs,
4521888d3ddSRobin Murphy };
4531888d3ddSRobin Murphy 
4541888d3ddSRobin Murphy 
4551888d3ddSRobin Murphy static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
4561888d3ddSRobin Murphy {
4571888d3ddSRobin Murphy 	unsigned long i;
4581888d3ddSRobin Murphy 
4591888d3ddSRobin Murphy 	if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
4601888d3ddSRobin Murphy 		return NULL;
4611888d3ddSRobin Murphy 	i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
4621888d3ddSRobin Murphy 
4631888d3ddSRobin Murphy 	switch (name[1]) {
4641888d3ddSRobin Murphy 	case 'l':
4651888d3ddSRobin Murphy 		return &ccn->dt.cmp_mask[i].l;
4661888d3ddSRobin Murphy 	case 'h':
4671888d3ddSRobin Murphy 		return &ccn->dt.cmp_mask[i].h;
4681888d3ddSRobin Murphy 	default:
4691888d3ddSRobin Murphy 		return NULL;
4701888d3ddSRobin Murphy 	}
4711888d3ddSRobin Murphy }
4721888d3ddSRobin Murphy 
4731888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
4741888d3ddSRobin Murphy 		struct device_attribute *attr, char *buf)
4751888d3ddSRobin Murphy {
4761888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
4771888d3ddSRobin Murphy 	u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
4781888d3ddSRobin Murphy 
4791888d3ddSRobin Murphy 	return mask ? snprintf(buf, PAGE_SIZE, "0x%016llx\n", *mask) : -EINVAL;
4801888d3ddSRobin Murphy }
4811888d3ddSRobin Murphy 
4821888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
4831888d3ddSRobin Murphy 		struct device_attribute *attr, const char *buf, size_t count)
4841888d3ddSRobin Murphy {
4851888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
4861888d3ddSRobin Murphy 	u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
4871888d3ddSRobin Murphy 	int err = -EINVAL;
4881888d3ddSRobin Murphy 
4891888d3ddSRobin Murphy 	if (mask)
4901888d3ddSRobin Murphy 		err = kstrtoull(buf, 0, mask);
4911888d3ddSRobin Murphy 
4921888d3ddSRobin Murphy 	return err ? err : count;
4931888d3ddSRobin Murphy }
4941888d3ddSRobin Murphy 
4951888d3ddSRobin Murphy #define CCN_CMP_MASK_ATTR(_name) \
4961888d3ddSRobin Murphy 	struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
4971888d3ddSRobin Murphy 			__ATTR(_name, S_IRUGO | S_IWUSR, \
4981888d3ddSRobin Murphy 			arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
4991888d3ddSRobin Murphy 
5001888d3ddSRobin Murphy #define CCN_CMP_MASK_ATTR_RO(_name) \
5011888d3ddSRobin Murphy 	struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
5021888d3ddSRobin Murphy 			__ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
5031888d3ddSRobin Murphy 
5041888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(0l);
5051888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(0h);
5061888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(1l);
5071888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(1h);
5081888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(2l);
5091888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(2h);
5101888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(3l);
5111888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(3h);
5121888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(4l);
5131888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(4h);
5141888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(5l);
5151888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(5h);
5161888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(6l);
5171888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(6h);
5181888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(7l);
5191888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(7h);
5201888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(8l);
5211888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(8h);
5221888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(9l);
5231888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(9h);
5241888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(al);
5251888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(ah);
5261888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(bl);
5271888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(bh);
5281888d3ddSRobin Murphy 
5291888d3ddSRobin Murphy static struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
5301888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
5311888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
5321888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
5331888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
5341888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
5351888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
5361888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
5371888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
5381888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
5391888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
5401888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
5411888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
5421888d3ddSRobin Murphy 	NULL
5431888d3ddSRobin Murphy };
5441888d3ddSRobin Murphy 
5451888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
5461888d3ddSRobin Murphy 	.name = "cmp_mask",
5471888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_cmp_mask_attrs,
5481888d3ddSRobin Murphy };
5491888d3ddSRobin Murphy 
5501888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
5511888d3ddSRobin Murphy 				     struct device_attribute *attr, char *buf)
5521888d3ddSRobin Murphy {
5531888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
5541888d3ddSRobin Murphy 
5559bcb929fSRobin Murphy 	return cpumap_print_to_pagebuf(true, buf, cpumask_of(ccn->dt.cpu));
5561888d3ddSRobin Murphy }
5571888d3ddSRobin Murphy 
5581888d3ddSRobin Murphy static struct device_attribute arm_ccn_pmu_cpumask_attr =
5591888d3ddSRobin Murphy 		__ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
5601888d3ddSRobin Murphy 
5611888d3ddSRobin Murphy static struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
5621888d3ddSRobin Murphy 	&arm_ccn_pmu_cpumask_attr.attr,
5631888d3ddSRobin Murphy 	NULL,
5641888d3ddSRobin Murphy };
5651888d3ddSRobin Murphy 
5661888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
5671888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_cpumask_attrs,
5681888d3ddSRobin Murphy };
5691888d3ddSRobin Murphy 
5701888d3ddSRobin Murphy /*
5711888d3ddSRobin Murphy  * Default poll period is 10ms, which is way over the top anyway,
5721888d3ddSRobin Murphy  * as in the worst case scenario (an event every cycle), with 1GHz
5731888d3ddSRobin Murphy  * clocked bus, the smallest, 32 bit counter will overflow in
5741888d3ddSRobin Murphy  * more than 4s.
5751888d3ddSRobin Murphy  */
5761888d3ddSRobin Murphy static unsigned int arm_ccn_pmu_poll_period_us = 10000;
5771888d3ddSRobin Murphy module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
5781888d3ddSRobin Murphy 		S_IRUGO | S_IWUSR);
5791888d3ddSRobin Murphy 
5801888d3ddSRobin Murphy static ktime_t arm_ccn_pmu_timer_period(void)
5811888d3ddSRobin Murphy {
5821888d3ddSRobin Murphy 	return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
5831888d3ddSRobin Murphy }
5841888d3ddSRobin Murphy 
5851888d3ddSRobin Murphy 
5861888d3ddSRobin Murphy static const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
5871888d3ddSRobin Murphy 	&arm_ccn_pmu_events_attr_group,
5881888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_group,
5891888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_group,
5901888d3ddSRobin Murphy 	&arm_ccn_pmu_cpumask_attr_group,
5911888d3ddSRobin Murphy 	NULL
5921888d3ddSRobin Murphy };
5931888d3ddSRobin Murphy 
5941888d3ddSRobin Murphy 
5951888d3ddSRobin Murphy static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
5961888d3ddSRobin Murphy {
5971888d3ddSRobin Murphy 	int bit;
5981888d3ddSRobin Murphy 
5991888d3ddSRobin Murphy 	do {
6001888d3ddSRobin Murphy 		bit = find_first_zero_bit(bitmap, size);
6011888d3ddSRobin Murphy 		if (bit >= size)
6021888d3ddSRobin Murphy 			return -EAGAIN;
6031888d3ddSRobin Murphy 	} while (test_and_set_bit(bit, bitmap));
6041888d3ddSRobin Murphy 
6051888d3ddSRobin Murphy 	return bit;
6061888d3ddSRobin Murphy }
6071888d3ddSRobin Murphy 
6081888d3ddSRobin Murphy /* All RN-I and RN-D nodes have identical PMUs */
6091888d3ddSRobin Murphy static int arm_ccn_pmu_type_eq(u32 a, u32 b)
6101888d3ddSRobin Murphy {
6111888d3ddSRobin Murphy 	if (a == b)
6121888d3ddSRobin Murphy 		return 1;
6131888d3ddSRobin Murphy 
6141888d3ddSRobin Murphy 	switch (a) {
6151888d3ddSRobin Murphy 	case CCN_TYPE_RNI_1P:
6161888d3ddSRobin Murphy 	case CCN_TYPE_RNI_2P:
6171888d3ddSRobin Murphy 	case CCN_TYPE_RNI_3P:
6181888d3ddSRobin Murphy 	case CCN_TYPE_RND_1P:
6191888d3ddSRobin Murphy 	case CCN_TYPE_RND_2P:
6201888d3ddSRobin Murphy 	case CCN_TYPE_RND_3P:
6211888d3ddSRobin Murphy 		switch (b) {
6221888d3ddSRobin Murphy 		case CCN_TYPE_RNI_1P:
6231888d3ddSRobin Murphy 		case CCN_TYPE_RNI_2P:
6241888d3ddSRobin Murphy 		case CCN_TYPE_RNI_3P:
6251888d3ddSRobin Murphy 		case CCN_TYPE_RND_1P:
6261888d3ddSRobin Murphy 		case CCN_TYPE_RND_2P:
6271888d3ddSRobin Murphy 		case CCN_TYPE_RND_3P:
6281888d3ddSRobin Murphy 			return 1;
6291888d3ddSRobin Murphy 		}
6301888d3ddSRobin Murphy 		break;
6311888d3ddSRobin Murphy 	}
6321888d3ddSRobin Murphy 
6331888d3ddSRobin Murphy 	return 0;
6341888d3ddSRobin Murphy }
6351888d3ddSRobin Murphy 
6361888d3ddSRobin Murphy static int arm_ccn_pmu_event_alloc(struct perf_event *event)
6371888d3ddSRobin Murphy {
6381888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
6391888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
6401888d3ddSRobin Murphy 	u32 node_xp, type, event_id;
6411888d3ddSRobin Murphy 	struct arm_ccn_component *source;
6421888d3ddSRobin Murphy 	int bit;
6431888d3ddSRobin Murphy 
6441888d3ddSRobin Murphy 	node_xp = CCN_CONFIG_NODE(event->attr.config);
6451888d3ddSRobin Murphy 	type = CCN_CONFIG_TYPE(event->attr.config);
6461888d3ddSRobin Murphy 	event_id = CCN_CONFIG_EVENT(event->attr.config);
6471888d3ddSRobin Murphy 
6481888d3ddSRobin Murphy 	/* Allocate the cycle counter */
6491888d3ddSRobin Murphy 	if (type == CCN_TYPE_CYCLES) {
6501888d3ddSRobin Murphy 		if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
6511888d3ddSRobin Murphy 				ccn->dt.pmu_counters_mask))
6521888d3ddSRobin Murphy 			return -EAGAIN;
6531888d3ddSRobin Murphy 
6541888d3ddSRobin Murphy 		hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
6551888d3ddSRobin Murphy 		ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
6561888d3ddSRobin Murphy 
6571888d3ddSRobin Murphy 		return 0;
6581888d3ddSRobin Murphy 	}
6591888d3ddSRobin Murphy 
6601888d3ddSRobin Murphy 	/* Allocate an event counter */
6611888d3ddSRobin Murphy 	hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
6621888d3ddSRobin Murphy 			CCN_NUM_PMU_EVENT_COUNTERS);
6631888d3ddSRobin Murphy 	if (hw->idx < 0) {
6641888d3ddSRobin Murphy 		dev_dbg(ccn->dev, "No more counters available!\n");
6651888d3ddSRobin Murphy 		return -EAGAIN;
6661888d3ddSRobin Murphy 	}
6671888d3ddSRobin Murphy 
6681888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP)
6691888d3ddSRobin Murphy 		source = &ccn->xp[node_xp];
6701888d3ddSRobin Murphy 	else
6711888d3ddSRobin Murphy 		source = &ccn->node[node_xp];
6721888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].source = source;
6731888d3ddSRobin Murphy 
6741888d3ddSRobin Murphy 	/* Allocate an event source or a watchpoint */
6751888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
6761888d3ddSRobin Murphy 		bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
6771888d3ddSRobin Murphy 				CCN_NUM_XP_WATCHPOINTS);
6781888d3ddSRobin Murphy 	else
6791888d3ddSRobin Murphy 		bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
6801888d3ddSRobin Murphy 				CCN_NUM_PMU_EVENTS);
6811888d3ddSRobin Murphy 	if (bit < 0) {
6821888d3ddSRobin Murphy 		dev_dbg(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
6831888d3ddSRobin Murphy 				node_xp);
6841888d3ddSRobin Murphy 		clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
6851888d3ddSRobin Murphy 		return -EAGAIN;
6861888d3ddSRobin Murphy 	}
6871888d3ddSRobin Murphy 	hw->config_base = bit;
6881888d3ddSRobin Murphy 
6891888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].event = event;
6901888d3ddSRobin Murphy 
6911888d3ddSRobin Murphy 	return 0;
6921888d3ddSRobin Murphy }
6931888d3ddSRobin Murphy 
6941888d3ddSRobin Murphy static void arm_ccn_pmu_event_release(struct perf_event *event)
6951888d3ddSRobin Murphy {
6961888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
6971888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
6981888d3ddSRobin Murphy 
6991888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
7001888d3ddSRobin Murphy 		clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
7011888d3ddSRobin Murphy 	} else {
7021888d3ddSRobin Murphy 		struct arm_ccn_component *source =
7031888d3ddSRobin Murphy 				ccn->dt.pmu_counters[hw->idx].source;
7041888d3ddSRobin Murphy 
7051888d3ddSRobin Murphy 		if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
7061888d3ddSRobin Murphy 				CCN_CONFIG_EVENT(event->attr.config) ==
7071888d3ddSRobin Murphy 				CCN_EVENT_WATCHPOINT)
7081888d3ddSRobin Murphy 			clear_bit(hw->config_base, source->xp.dt_cmp_mask);
7091888d3ddSRobin Murphy 		else
7101888d3ddSRobin Murphy 			clear_bit(hw->config_base, source->pmu_events_mask);
7111888d3ddSRobin Murphy 		clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
7121888d3ddSRobin Murphy 	}
7131888d3ddSRobin Murphy 
7141888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].source = NULL;
7151888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].event = NULL;
7161888d3ddSRobin Murphy }
7171888d3ddSRobin Murphy 
7181888d3ddSRobin Murphy static int arm_ccn_pmu_event_init(struct perf_event *event)
7191888d3ddSRobin Murphy {
7201888d3ddSRobin Murphy 	struct arm_ccn *ccn;
7211888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
7221888d3ddSRobin Murphy 	u32 node_xp, type, event_id;
7231888d3ddSRobin Murphy 	int valid;
7241888d3ddSRobin Murphy 	int i;
7251888d3ddSRobin Murphy 	struct perf_event *sibling;
7261888d3ddSRobin Murphy 
7271888d3ddSRobin Murphy 	if (event->attr.type != event->pmu->type)
7281888d3ddSRobin Murphy 		return -ENOENT;
7291888d3ddSRobin Murphy 
7301888d3ddSRobin Murphy 	ccn = pmu_to_arm_ccn(event->pmu);
7311888d3ddSRobin Murphy 
7321888d3ddSRobin Murphy 	if (hw->sample_period) {
7331898eb61SMark Rutland 		dev_dbg(ccn->dev, "Sampling not supported!\n");
7341888d3ddSRobin Murphy 		return -EOPNOTSUPP;
7351888d3ddSRobin Murphy 	}
7361888d3ddSRobin Murphy 
73730656398SAndrew Murray 	if (has_branch_stack(event)) {
7381898eb61SMark Rutland 		dev_dbg(ccn->dev, "Can't exclude execution levels!\n");
7391888d3ddSRobin Murphy 		return -EINVAL;
7401888d3ddSRobin Murphy 	}
7411888d3ddSRobin Murphy 
7421888d3ddSRobin Murphy 	if (event->cpu < 0) {
7431898eb61SMark Rutland 		dev_dbg(ccn->dev, "Can't provide per-task data!\n");
7441888d3ddSRobin Murphy 		return -EOPNOTSUPP;
7451888d3ddSRobin Murphy 	}
7461888d3ddSRobin Murphy 	/*
7471888d3ddSRobin Murphy 	 * Many perf core operations (eg. events rotation) operate on a
7481888d3ddSRobin Murphy 	 * single CPU context. This is obvious for CPU PMUs, where one
7491888d3ddSRobin Murphy 	 * expects the same sets of events being observed on all CPUs,
7501888d3ddSRobin Murphy 	 * but can lead to issues for off-core PMUs, like CCN, where each
7511888d3ddSRobin Murphy 	 * event could be theoretically assigned to a different CPU. To
7521888d3ddSRobin Murphy 	 * mitigate this, we enforce CPU assignment to one, selected
7531888d3ddSRobin Murphy 	 * processor (the one described in the "cpumask" attribute).
7541888d3ddSRobin Murphy 	 */
7559bcb929fSRobin Murphy 	event->cpu = ccn->dt.cpu;
7561888d3ddSRobin Murphy 
7571888d3ddSRobin Murphy 	node_xp = CCN_CONFIG_NODE(event->attr.config);
7581888d3ddSRobin Murphy 	type = CCN_CONFIG_TYPE(event->attr.config);
7591888d3ddSRobin Murphy 	event_id = CCN_CONFIG_EVENT(event->attr.config);
7601888d3ddSRobin Murphy 
7611888d3ddSRobin Murphy 	/* Validate node/xp vs topology */
7621888d3ddSRobin Murphy 	switch (type) {
7631888d3ddSRobin Murphy 	case CCN_TYPE_MN:
7641888d3ddSRobin Murphy 		if (node_xp != ccn->mn_id) {
7651898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid MN ID %d!\n", node_xp);
7661888d3ddSRobin Murphy 			return -EINVAL;
7671888d3ddSRobin Murphy 		}
7681888d3ddSRobin Murphy 		break;
7691888d3ddSRobin Murphy 	case CCN_TYPE_XP:
7701888d3ddSRobin Murphy 		if (node_xp >= ccn->num_xps) {
7711898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid XP ID %d!\n", node_xp);
7721888d3ddSRobin Murphy 			return -EINVAL;
7731888d3ddSRobin Murphy 		}
7741888d3ddSRobin Murphy 		break;
7751888d3ddSRobin Murphy 	case CCN_TYPE_CYCLES:
7761888d3ddSRobin Murphy 		break;
7771888d3ddSRobin Murphy 	default:
7781888d3ddSRobin Murphy 		if (node_xp >= ccn->num_nodes) {
7791898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid node ID %d!\n", node_xp);
7801888d3ddSRobin Murphy 			return -EINVAL;
7811888d3ddSRobin Murphy 		}
7821888d3ddSRobin Murphy 		if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
7831898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid type 0x%x for node %d!\n",
7841888d3ddSRobin Murphy 					type, node_xp);
7851888d3ddSRobin Murphy 			return -EINVAL;
7861888d3ddSRobin Murphy 		}
7871888d3ddSRobin Murphy 		break;
7881888d3ddSRobin Murphy 	}
7891888d3ddSRobin Murphy 
7901888d3ddSRobin Murphy 	/* Validate event ID vs available for the type */
7911888d3ddSRobin Murphy 	for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
7921888d3ddSRobin Murphy 			i++) {
7931888d3ddSRobin Murphy 		struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
7941888d3ddSRobin Murphy 		u32 port = CCN_CONFIG_PORT(event->attr.config);
7951888d3ddSRobin Murphy 		u32 vc = CCN_CONFIG_VC(event->attr.config);
7961888d3ddSRobin Murphy 
7971888d3ddSRobin Murphy 		if (!arm_ccn_pmu_type_eq(type, e->type))
7981888d3ddSRobin Murphy 			continue;
7991888d3ddSRobin Murphy 		if (event_id != e->event)
8001888d3ddSRobin Murphy 			continue;
8011888d3ddSRobin Murphy 		if (e->num_ports && port >= e->num_ports) {
8021898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid port %d for node/XP %d!\n",
8031888d3ddSRobin Murphy 					port, node_xp);
8041888d3ddSRobin Murphy 			return -EINVAL;
8051888d3ddSRobin Murphy 		}
8061888d3ddSRobin Murphy 		if (e->num_vcs && vc >= e->num_vcs) {
8071898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid vc %d for node/XP %d!\n",
8081888d3ddSRobin Murphy 					vc, node_xp);
8091888d3ddSRobin Murphy 			return -EINVAL;
8101888d3ddSRobin Murphy 		}
8111888d3ddSRobin Murphy 		valid = 1;
8121888d3ddSRobin Murphy 	}
8131888d3ddSRobin Murphy 	if (!valid) {
8141898eb61SMark Rutland 		dev_dbg(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
8151888d3ddSRobin Murphy 				event_id, node_xp);
8161888d3ddSRobin Murphy 		return -EINVAL;
8171888d3ddSRobin Murphy 	}
8181888d3ddSRobin Murphy 
8191888d3ddSRobin Murphy 	/* Watchpoint-based event for a node is actually set on XP */
8201888d3ddSRobin Murphy 	if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
8211888d3ddSRobin Murphy 		u32 port;
8221888d3ddSRobin Murphy 
8231888d3ddSRobin Murphy 		type = CCN_TYPE_XP;
8241888d3ddSRobin Murphy 		port = arm_ccn_node_to_xp_port(node_xp);
8251888d3ddSRobin Murphy 		node_xp = arm_ccn_node_to_xp(node_xp);
8261888d3ddSRobin Murphy 
8271888d3ddSRobin Murphy 		arm_ccn_pmu_config_set(&event->attr.config,
8281888d3ddSRobin Murphy 				node_xp, type, port);
8291888d3ddSRobin Murphy 	}
8301888d3ddSRobin Murphy 
8311888d3ddSRobin Murphy 	/*
8321888d3ddSRobin Murphy 	 * We must NOT create groups containing mixed PMUs, although software
8331888d3ddSRobin Murphy 	 * events are acceptable (for example to create a CCN group
8341888d3ddSRobin Murphy 	 * periodically read when a hrtimer aka cpu-clock leader triggers).
8351888d3ddSRobin Murphy 	 */
8361888d3ddSRobin Murphy 	if (event->group_leader->pmu != event->pmu &&
8371888d3ddSRobin Murphy 			!is_software_event(event->group_leader))
8381888d3ddSRobin Murphy 		return -EINVAL;
8391888d3ddSRobin Murphy 
84038c23685SLinus Torvalds 	for_each_sibling_event(sibling, event->group_leader) {
8411888d3ddSRobin Murphy 		if (sibling->pmu != event->pmu &&
8421888d3ddSRobin Murphy 				!is_software_event(sibling))
8431888d3ddSRobin Murphy 			return -EINVAL;
84438c23685SLinus Torvalds 	}
8451888d3ddSRobin Murphy 
8461888d3ddSRobin Murphy 	return 0;
8471888d3ddSRobin Murphy }
8481888d3ddSRobin Murphy 
8491888d3ddSRobin Murphy static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
8501888d3ddSRobin Murphy {
8511888d3ddSRobin Murphy 	u64 res;
8521888d3ddSRobin Murphy 
8531888d3ddSRobin Murphy 	if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
8541888d3ddSRobin Murphy #ifdef readq
8551888d3ddSRobin Murphy 		res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
8561888d3ddSRobin Murphy #else
8571888d3ddSRobin Murphy 		/* 40 bit counter, can do snapshot and read in two parts */
8581888d3ddSRobin Murphy 		writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
8591888d3ddSRobin Murphy 		while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
8601888d3ddSRobin Murphy 			;
8611888d3ddSRobin Murphy 		writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
8621888d3ddSRobin Murphy 		res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
8631888d3ddSRobin Murphy 		res <<= 32;
8641888d3ddSRobin Murphy 		res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
8651888d3ddSRobin Murphy #endif
8661888d3ddSRobin Murphy 	} else {
8671888d3ddSRobin Murphy 		res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
8681888d3ddSRobin Murphy 	}
8691888d3ddSRobin Murphy 
8701888d3ddSRobin Murphy 	return res;
8711888d3ddSRobin Murphy }
8721888d3ddSRobin Murphy 
8731888d3ddSRobin Murphy static void arm_ccn_pmu_event_update(struct perf_event *event)
8741888d3ddSRobin Murphy {
8751888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
8761888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
8771888d3ddSRobin Murphy 	u64 prev_count, new_count, mask;
8781888d3ddSRobin Murphy 
8791888d3ddSRobin Murphy 	do {
8801888d3ddSRobin Murphy 		prev_count = local64_read(&hw->prev_count);
8811888d3ddSRobin Murphy 		new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
8821888d3ddSRobin Murphy 	} while (local64_xchg(&hw->prev_count, new_count) != prev_count);
8831888d3ddSRobin Murphy 
8841888d3ddSRobin Murphy 	mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
8851888d3ddSRobin Murphy 
8861888d3ddSRobin Murphy 	local64_add((new_count - prev_count) & mask, &event->count);
8871888d3ddSRobin Murphy }
8881888d3ddSRobin Murphy 
8891888d3ddSRobin Murphy static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
8901888d3ddSRobin Murphy {
8911888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
8921888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
8931888d3ddSRobin Murphy 	struct arm_ccn_component *xp;
8941888d3ddSRobin Murphy 	u32 val, dt_cfg;
8951888d3ddSRobin Murphy 
8961888d3ddSRobin Murphy 	/* Nothing to do for cycle counter */
8971888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
8981888d3ddSRobin Murphy 		return;
8991888d3ddSRobin Murphy 
9001888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
9011888d3ddSRobin Murphy 		xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
9021888d3ddSRobin Murphy 	else
9031888d3ddSRobin Murphy 		xp = &ccn->xp[arm_ccn_node_to_xp(
9041888d3ddSRobin Murphy 				CCN_CONFIG_NODE(event->attr.config))];
9051888d3ddSRobin Murphy 
9061888d3ddSRobin Murphy 	if (enable)
9071888d3ddSRobin Murphy 		dt_cfg = hw->event_base;
9081888d3ddSRobin Murphy 	else
9091888d3ddSRobin Murphy 		dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
9101888d3ddSRobin Murphy 
9111888d3ddSRobin Murphy 	spin_lock(&ccn->dt.config_lock);
9121888d3ddSRobin Murphy 
9131888d3ddSRobin Murphy 	val = readl(xp->base + CCN_XP_DT_CONFIG);
9141888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
9151888d3ddSRobin Murphy 			CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
9161888d3ddSRobin Murphy 	val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
9171888d3ddSRobin Murphy 	writel(val, xp->base + CCN_XP_DT_CONFIG);
9181888d3ddSRobin Murphy 
9191888d3ddSRobin Murphy 	spin_unlock(&ccn->dt.config_lock);
9201888d3ddSRobin Murphy }
9211888d3ddSRobin Murphy 
9221888d3ddSRobin Murphy static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
9231888d3ddSRobin Murphy {
9241888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9251888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
9261888d3ddSRobin Murphy 
9271888d3ddSRobin Murphy 	local64_set(&event->hw.prev_count,
9281888d3ddSRobin Murphy 			arm_ccn_pmu_read_counter(ccn, hw->idx));
9291888d3ddSRobin Murphy 	hw->state = 0;
9301888d3ddSRobin Murphy 
9311888d3ddSRobin Murphy 	/* Set the DT bus input, engaging the counter */
9321888d3ddSRobin Murphy 	arm_ccn_pmu_xp_dt_config(event, 1);
9331888d3ddSRobin Murphy }
9341888d3ddSRobin Murphy 
9351888d3ddSRobin Murphy static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
9361888d3ddSRobin Murphy {
9371888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
9381888d3ddSRobin Murphy 
9391888d3ddSRobin Murphy 	/* Disable counting, setting the DT bus to pass-through mode */
9401888d3ddSRobin Murphy 	arm_ccn_pmu_xp_dt_config(event, 0);
9411888d3ddSRobin Murphy 
9421888d3ddSRobin Murphy 	if (flags & PERF_EF_UPDATE)
9431888d3ddSRobin Murphy 		arm_ccn_pmu_event_update(event);
9441888d3ddSRobin Murphy 
9451888d3ddSRobin Murphy 	hw->state |= PERF_HES_STOPPED;
9461888d3ddSRobin Murphy }
9471888d3ddSRobin Murphy 
9481888d3ddSRobin Murphy static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
9491888d3ddSRobin Murphy {
9501888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9511888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
9521888d3ddSRobin Murphy 	struct arm_ccn_component *source =
9531888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
9541888d3ddSRobin Murphy 	unsigned long wp = hw->config_base;
9551888d3ddSRobin Murphy 	u32 val;
9561888d3ddSRobin Murphy 	u64 cmp_l = event->attr.config1;
9571888d3ddSRobin Murphy 	u64 cmp_h = event->attr.config2;
9581888d3ddSRobin Murphy 	u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
9591888d3ddSRobin Murphy 	u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
9601888d3ddSRobin Murphy 
9611888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
9621888d3ddSRobin Murphy 
9631888d3ddSRobin Murphy 	/* Direction (RX/TX), device (port) & virtual channel */
9641888d3ddSRobin Murphy 	val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
9651888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
9661888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
9671888d3ddSRobin Murphy 	val |= CCN_CONFIG_DIR(event->attr.config) <<
9681888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
9691888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
9701888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
9711888d3ddSRobin Murphy 	val |= CCN_CONFIG_PORT(event->attr.config) <<
9721888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
9731888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
9741888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
9751888d3ddSRobin Murphy 	val |= CCN_CONFIG_VC(event->attr.config) <<
9761888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
9771888d3ddSRobin Murphy 	writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
9781888d3ddSRobin Murphy 
9791888d3ddSRobin Murphy 	/* Comparison values */
9801888d3ddSRobin Murphy 	writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
9811888d3ddSRobin Murphy 	writel((cmp_l >> 32) & 0x7fffffff,
9821888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
9831888d3ddSRobin Murphy 	writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
9841888d3ddSRobin Murphy 	writel((cmp_h >> 32) & 0x0fffffff,
9851888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
9861888d3ddSRobin Murphy 
9871888d3ddSRobin Murphy 	/* Mask */
9881888d3ddSRobin Murphy 	writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
9891888d3ddSRobin Murphy 	writel((mask_l >> 32) & 0x7fffffff,
9901888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
9911888d3ddSRobin Murphy 	writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
9921888d3ddSRobin Murphy 	writel((mask_h >> 32) & 0x0fffffff,
9931888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
9941888d3ddSRobin Murphy }
9951888d3ddSRobin Murphy 
9961888d3ddSRobin Murphy static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
9971888d3ddSRobin Murphy {
9981888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9991888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
10001888d3ddSRobin Murphy 	struct arm_ccn_component *source =
10011888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
10021888d3ddSRobin Murphy 	u32 val, id;
10031888d3ddSRobin Murphy 
10041888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
10051888d3ddSRobin Murphy 
10061888d3ddSRobin Murphy 	id = (CCN_CONFIG_VC(event->attr.config) << 4) |
10071888d3ddSRobin Murphy 			(CCN_CONFIG_BUS(event->attr.config) << 3) |
10081888d3ddSRobin Murphy 			(CCN_CONFIG_EVENT(event->attr.config) << 0);
10091888d3ddSRobin Murphy 
10101888d3ddSRobin Murphy 	val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
10111888d3ddSRobin Murphy 	val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
10121888d3ddSRobin Murphy 			CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
10131888d3ddSRobin Murphy 	val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
10141888d3ddSRobin Murphy 	writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
10151888d3ddSRobin Murphy }
10161888d3ddSRobin Murphy 
10171888d3ddSRobin Murphy static void arm_ccn_pmu_node_event_config(struct perf_event *event)
10181888d3ddSRobin Murphy {
10191888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
10201888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
10211888d3ddSRobin Murphy 	struct arm_ccn_component *source =
10221888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
10231888d3ddSRobin Murphy 	u32 type = CCN_CONFIG_TYPE(event->attr.config);
10241888d3ddSRobin Murphy 	u32 val, port;
10251888d3ddSRobin Murphy 
10261888d3ddSRobin Murphy 	port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
10271888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
10281888d3ddSRobin Murphy 			hw->config_base);
10291888d3ddSRobin Murphy 
10301888d3ddSRobin Murphy 	/* These *_event_sel regs should be identical, but let's make sure... */
10311888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
10321888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
10331888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
10341888d3ddSRobin Murphy 			CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
10351888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
10361888d3ddSRobin Murphy 			CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
10371888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
10381888d3ddSRobin Murphy 			CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
10391888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
10401888d3ddSRobin Murphy 			CCN_RNI_PMU_EVENT_SEL__ID__MASK);
10411888d3ddSRobin Murphy 	if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
10421888d3ddSRobin Murphy 			!arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
10431888d3ddSRobin Murphy 		return;
10441888d3ddSRobin Murphy 
10451888d3ddSRobin Murphy 	/* Set the event id for the pre-allocated counter */
10461888d3ddSRobin Murphy 	val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
10471888d3ddSRobin Murphy 	val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
10481888d3ddSRobin Murphy 		CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
10491888d3ddSRobin Murphy 	val |= CCN_CONFIG_EVENT(event->attr.config) <<
10501888d3ddSRobin Murphy 		CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
10511888d3ddSRobin Murphy 	writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
10521888d3ddSRobin Murphy }
10531888d3ddSRobin Murphy 
10541888d3ddSRobin Murphy static void arm_ccn_pmu_event_config(struct perf_event *event)
10551888d3ddSRobin Murphy {
10561888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
10571888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
10581888d3ddSRobin Murphy 	u32 xp, offset, val;
10591888d3ddSRobin Murphy 
10601888d3ddSRobin Murphy 	/* Cycle counter requires no setup */
10611888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
10621888d3ddSRobin Murphy 		return;
10631888d3ddSRobin Murphy 
10641888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
10651888d3ddSRobin Murphy 		xp = CCN_CONFIG_XP(event->attr.config);
10661888d3ddSRobin Murphy 	else
10671888d3ddSRobin Murphy 		xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
10681888d3ddSRobin Murphy 
10691888d3ddSRobin Murphy 	spin_lock(&ccn->dt.config_lock);
10701888d3ddSRobin Murphy 
10711888d3ddSRobin Murphy 	/* Set the DT bus "distance" register */
10721888d3ddSRobin Murphy 	offset = (hw->idx / 4) * 4;
10731888d3ddSRobin Murphy 	val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
10741888d3ddSRobin Murphy 	val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
10751888d3ddSRobin Murphy 			CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
10761888d3ddSRobin Murphy 	val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
10771888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
10781888d3ddSRobin Murphy 
10791888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
10801888d3ddSRobin Murphy 		if (CCN_CONFIG_EVENT(event->attr.config) ==
10811888d3ddSRobin Murphy 				CCN_EVENT_WATCHPOINT)
10821888d3ddSRobin Murphy 			arm_ccn_pmu_xp_watchpoint_config(event);
10831888d3ddSRobin Murphy 		else
10841888d3ddSRobin Murphy 			arm_ccn_pmu_xp_event_config(event);
10851888d3ddSRobin Murphy 	} else {
10861888d3ddSRobin Murphy 		arm_ccn_pmu_node_event_config(event);
10871888d3ddSRobin Murphy 	}
10881888d3ddSRobin Murphy 
10891888d3ddSRobin Murphy 	spin_unlock(&ccn->dt.config_lock);
10901888d3ddSRobin Murphy }
10911888d3ddSRobin Murphy 
10921888d3ddSRobin Murphy static int arm_ccn_pmu_active_counters(struct arm_ccn *ccn)
10931888d3ddSRobin Murphy {
10941888d3ddSRobin Murphy 	return bitmap_weight(ccn->dt.pmu_counters_mask,
10951888d3ddSRobin Murphy 			     CCN_NUM_PMU_EVENT_COUNTERS + 1);
10961888d3ddSRobin Murphy }
10971888d3ddSRobin Murphy 
10981888d3ddSRobin Murphy static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
10991888d3ddSRobin Murphy {
11001888d3ddSRobin Murphy 	int err;
11011888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
11021888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
11031888d3ddSRobin Murphy 
11041888d3ddSRobin Murphy 	err = arm_ccn_pmu_event_alloc(event);
11051888d3ddSRobin Murphy 	if (err)
11061888d3ddSRobin Murphy 		return err;
11071888d3ddSRobin Murphy 
11081888d3ddSRobin Murphy 	/*
11091888d3ddSRobin Murphy 	 * Pin the timer, so that the overflows are handled by the chosen
11101888d3ddSRobin Murphy 	 * event->cpu (this is the same one as presented in "cpumask"
11111888d3ddSRobin Murphy 	 * attribute).
11121888d3ddSRobin Murphy 	 */
11131888d3ddSRobin Murphy 	if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 1)
11141888d3ddSRobin Murphy 		hrtimer_start(&ccn->dt.hrtimer, arm_ccn_pmu_timer_period(),
11151888d3ddSRobin Murphy 			      HRTIMER_MODE_REL_PINNED);
11161888d3ddSRobin Murphy 
11171888d3ddSRobin Murphy 	arm_ccn_pmu_event_config(event);
11181888d3ddSRobin Murphy 
11191888d3ddSRobin Murphy 	hw->state = PERF_HES_STOPPED;
11201888d3ddSRobin Murphy 
11211888d3ddSRobin Murphy 	if (flags & PERF_EF_START)
11221888d3ddSRobin Murphy 		arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
11231888d3ddSRobin Murphy 
11241888d3ddSRobin Murphy 	return 0;
11251888d3ddSRobin Murphy }
11261888d3ddSRobin Murphy 
11271888d3ddSRobin Murphy static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
11281888d3ddSRobin Murphy {
11291888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
11301888d3ddSRobin Murphy 
11311888d3ddSRobin Murphy 	arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
11321888d3ddSRobin Murphy 
11331888d3ddSRobin Murphy 	arm_ccn_pmu_event_release(event);
11341888d3ddSRobin Murphy 
11351888d3ddSRobin Murphy 	if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 0)
11361888d3ddSRobin Murphy 		hrtimer_cancel(&ccn->dt.hrtimer);
11371888d3ddSRobin Murphy }
11381888d3ddSRobin Murphy 
11391888d3ddSRobin Murphy static void arm_ccn_pmu_event_read(struct perf_event *event)
11401888d3ddSRobin Murphy {
11411888d3ddSRobin Murphy 	arm_ccn_pmu_event_update(event);
11421888d3ddSRobin Murphy }
11431888d3ddSRobin Murphy 
11441888d3ddSRobin Murphy static void arm_ccn_pmu_enable(struct pmu *pmu)
11451888d3ddSRobin Murphy {
11461888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
11471888d3ddSRobin Murphy 
11481888d3ddSRobin Murphy 	u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
11491888d3ddSRobin Murphy 	val |= CCN_DT_PMCR__PMU_EN;
11501888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_PMCR);
11511888d3ddSRobin Murphy }
11521888d3ddSRobin Murphy 
11531888d3ddSRobin Murphy static void arm_ccn_pmu_disable(struct pmu *pmu)
11541888d3ddSRobin Murphy {
11551888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
11561888d3ddSRobin Murphy 
11571888d3ddSRobin Murphy 	u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
11581888d3ddSRobin Murphy 	val &= ~CCN_DT_PMCR__PMU_EN;
11591888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_PMCR);
11601888d3ddSRobin Murphy }
11611888d3ddSRobin Murphy 
11621888d3ddSRobin Murphy static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
11631888d3ddSRobin Murphy {
11641888d3ddSRobin Murphy 	u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
11651888d3ddSRobin Murphy 	int idx;
11661888d3ddSRobin Murphy 
11671888d3ddSRobin Murphy 	if (!pmovsr)
11681888d3ddSRobin Murphy 		return IRQ_NONE;
11691888d3ddSRobin Murphy 
11701888d3ddSRobin Murphy 	writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
11711888d3ddSRobin Murphy 
11721888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
11731888d3ddSRobin Murphy 
11741888d3ddSRobin Murphy 	for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
11751888d3ddSRobin Murphy 		struct perf_event *event = dt->pmu_counters[idx].event;
11761888d3ddSRobin Murphy 		int overflowed = pmovsr & BIT(idx);
11771888d3ddSRobin Murphy 
11781888d3ddSRobin Murphy 		WARN_ON_ONCE(overflowed && !event &&
11791888d3ddSRobin Murphy 				idx != CCN_IDX_PMU_CYCLE_COUNTER);
11801888d3ddSRobin Murphy 
11811888d3ddSRobin Murphy 		if (!event || !overflowed)
11821888d3ddSRobin Murphy 			continue;
11831888d3ddSRobin Murphy 
11841888d3ddSRobin Murphy 		arm_ccn_pmu_event_update(event);
11851888d3ddSRobin Murphy 	}
11861888d3ddSRobin Murphy 
11871888d3ddSRobin Murphy 	return IRQ_HANDLED;
11881888d3ddSRobin Murphy }
11891888d3ddSRobin Murphy 
11901888d3ddSRobin Murphy static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
11911888d3ddSRobin Murphy {
11921888d3ddSRobin Murphy 	struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
11931888d3ddSRobin Murphy 			hrtimer);
11941888d3ddSRobin Murphy 	unsigned long flags;
11951888d3ddSRobin Murphy 
11961888d3ddSRobin Murphy 	local_irq_save(flags);
11971888d3ddSRobin Murphy 	arm_ccn_pmu_overflow_handler(dt);
11981888d3ddSRobin Murphy 	local_irq_restore(flags);
11991888d3ddSRobin Murphy 
12001888d3ddSRobin Murphy 	hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
12011888d3ddSRobin Murphy 	return HRTIMER_RESTART;
12021888d3ddSRobin Murphy }
12031888d3ddSRobin Murphy 
12041888d3ddSRobin Murphy 
12051888d3ddSRobin Murphy static int arm_ccn_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
12061888d3ddSRobin Murphy {
12071888d3ddSRobin Murphy 	struct arm_ccn_dt *dt = hlist_entry_safe(node, struct arm_ccn_dt, node);
12081888d3ddSRobin Murphy 	struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
12091888d3ddSRobin Murphy 	unsigned int target;
12101888d3ddSRobin Murphy 
12119bcb929fSRobin Murphy 	if (cpu != dt->cpu)
12121888d3ddSRobin Murphy 		return 0;
12131888d3ddSRobin Murphy 	target = cpumask_any_but(cpu_online_mask, cpu);
12141888d3ddSRobin Murphy 	if (target >= nr_cpu_ids)
12151888d3ddSRobin Murphy 		return 0;
12161888d3ddSRobin Murphy 	perf_pmu_migrate_context(&dt->pmu, cpu, target);
12179bcb929fSRobin Murphy 	dt->cpu = target;
12181888d3ddSRobin Murphy 	if (ccn->irq)
12199bcb929fSRobin Murphy 		WARN_ON(irq_set_affinity_hint(ccn->irq, cpumask_of(dt->cpu)));
12201888d3ddSRobin Murphy 	return 0;
12211888d3ddSRobin Murphy }
12221888d3ddSRobin Murphy 
12231888d3ddSRobin Murphy static DEFINE_IDA(arm_ccn_pmu_ida);
12241888d3ddSRobin Murphy 
12251888d3ddSRobin Murphy static int arm_ccn_pmu_init(struct arm_ccn *ccn)
12261888d3ddSRobin Murphy {
12271888d3ddSRobin Murphy 	int i;
12281888d3ddSRobin Murphy 	char *name;
12291888d3ddSRobin Murphy 	int err;
12301888d3ddSRobin Murphy 
12311888d3ddSRobin Murphy 	/* Initialize DT subsystem */
12321888d3ddSRobin Murphy 	ccn->dt.base = ccn->base + CCN_REGION_SIZE;
12331888d3ddSRobin Murphy 	spin_lock_init(&ccn->dt.config_lock);
12341888d3ddSRobin Murphy 	writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
12351888d3ddSRobin Murphy 	writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
12361888d3ddSRobin Murphy 	writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
12371888d3ddSRobin Murphy 			ccn->dt.base + CCN_DT_PMCR);
12381888d3ddSRobin Murphy 	writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
12391888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++) {
12401888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
12411888d3ddSRobin Murphy 		writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
12421888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
12431888d3ddSRobin Murphy 				(CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
12441888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
12451888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__DT_ENABLE,
12461888d3ddSRobin Murphy 				ccn->xp[i].base + CCN_XP_DT_CONTROL);
12471888d3ddSRobin Murphy 	}
12481888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
12491888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
12501888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
12511888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
12521888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
12531888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
12541888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
12551888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
12561888d3ddSRobin Murphy 
12571888d3ddSRobin Murphy 	/* Get a convenient /sys/event_source/devices/ name */
12581888d3ddSRobin Murphy 	ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
12591888d3ddSRobin Murphy 	if (ccn->dt.id == 0) {
12601888d3ddSRobin Murphy 		name = "ccn";
12611888d3ddSRobin Murphy 	} else {
12621888d3ddSRobin Murphy 		name = devm_kasprintf(ccn->dev, GFP_KERNEL, "ccn_%d",
12631888d3ddSRobin Murphy 				      ccn->dt.id);
12641888d3ddSRobin Murphy 		if (!name) {
12651888d3ddSRobin Murphy 			err = -ENOMEM;
12661888d3ddSRobin Murphy 			goto error_choose_name;
12671888d3ddSRobin Murphy 		}
12681888d3ddSRobin Murphy 	}
12691888d3ddSRobin Murphy 
12701888d3ddSRobin Murphy 	/* Perf driver registration */
12711888d3ddSRobin Murphy 	ccn->dt.pmu = (struct pmu) {
12721888d3ddSRobin Murphy 		.module = THIS_MODULE,
12731888d3ddSRobin Murphy 		.attr_groups = arm_ccn_pmu_attr_groups,
12741888d3ddSRobin Murphy 		.task_ctx_nr = perf_invalid_context,
12751888d3ddSRobin Murphy 		.event_init = arm_ccn_pmu_event_init,
12761888d3ddSRobin Murphy 		.add = arm_ccn_pmu_event_add,
12771888d3ddSRobin Murphy 		.del = arm_ccn_pmu_event_del,
12781888d3ddSRobin Murphy 		.start = arm_ccn_pmu_event_start,
12791888d3ddSRobin Murphy 		.stop = arm_ccn_pmu_event_stop,
12801888d3ddSRobin Murphy 		.read = arm_ccn_pmu_event_read,
12811888d3ddSRobin Murphy 		.pmu_enable = arm_ccn_pmu_enable,
12821888d3ddSRobin Murphy 		.pmu_disable = arm_ccn_pmu_disable,
128330656398SAndrew Murray 		.capabilities = PERF_PMU_CAP_NO_EXCLUDE,
12841888d3ddSRobin Murphy 	};
12851888d3ddSRobin Murphy 
12861888d3ddSRobin Murphy 	/* No overflow interrupt? Have to use a timer instead. */
12871888d3ddSRobin Murphy 	if (!ccn->irq) {
12881888d3ddSRobin Murphy 		dev_info(ccn->dev, "No access to interrupts, using timer.\n");
12891888d3ddSRobin Murphy 		hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
12901888d3ddSRobin Murphy 				HRTIMER_MODE_REL);
12911888d3ddSRobin Murphy 		ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
12921888d3ddSRobin Murphy 	}
12931888d3ddSRobin Murphy 
12941888d3ddSRobin Murphy 	/* Pick one CPU which we will use to collect data from CCN... */
12959bcb929fSRobin Murphy 	ccn->dt.cpu = raw_smp_processor_id();
12961888d3ddSRobin Murphy 
12971888d3ddSRobin Murphy 	/* Also make sure that the overflow interrupt is handled by this CPU */
12981888d3ddSRobin Murphy 	if (ccn->irq) {
12999bcb929fSRobin Murphy 		err = irq_set_affinity_hint(ccn->irq, cpumask_of(ccn->dt.cpu));
13001888d3ddSRobin Murphy 		if (err) {
13011888d3ddSRobin Murphy 			dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
13021888d3ddSRobin Murphy 			goto error_set_affinity;
13031888d3ddSRobin Murphy 		}
13041888d3ddSRobin Murphy 	}
13051888d3ddSRobin Murphy 
13069bcb929fSRobin Murphy 	cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13079bcb929fSRobin Murphy 					 &ccn->dt.node);
13089bcb929fSRobin Murphy 
13091888d3ddSRobin Murphy 	err = perf_pmu_register(&ccn->dt.pmu, name, -1);
13101888d3ddSRobin Murphy 	if (err)
13111888d3ddSRobin Murphy 		goto error_pmu_register;
13121888d3ddSRobin Murphy 
13131888d3ddSRobin Murphy 	return 0;
13141888d3ddSRobin Murphy 
13151888d3ddSRobin Murphy error_pmu_register:
13169bcb929fSRobin Murphy 	cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13179bcb929fSRobin Murphy 					    &ccn->dt.node);
13181888d3ddSRobin Murphy error_set_affinity:
13191888d3ddSRobin Murphy error_choose_name:
13201888d3ddSRobin Murphy 	ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
13211888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++)
13221888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
13231888d3ddSRobin Murphy 	writel(0, ccn->dt.base + CCN_DT_PMCR);
13241888d3ddSRobin Murphy 	return err;
13251888d3ddSRobin Murphy }
13261888d3ddSRobin Murphy 
13271888d3ddSRobin Murphy static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
13281888d3ddSRobin Murphy {
13291888d3ddSRobin Murphy 	int i;
13301888d3ddSRobin Murphy 
13311888d3ddSRobin Murphy 	cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13321888d3ddSRobin Murphy 					    &ccn->dt.node);
13331888d3ddSRobin Murphy 	if (ccn->irq)
13341888d3ddSRobin Murphy 		irq_set_affinity_hint(ccn->irq, NULL);
13351888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++)
13361888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
13371888d3ddSRobin Murphy 	writel(0, ccn->dt.base + CCN_DT_PMCR);
13381888d3ddSRobin Murphy 	perf_pmu_unregister(&ccn->dt.pmu);
13391888d3ddSRobin Murphy 	ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
13401888d3ddSRobin Murphy }
13411888d3ddSRobin Murphy 
13421888d3ddSRobin Murphy static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
13431888d3ddSRobin Murphy 		int (*callback)(struct arm_ccn *ccn, int region,
13441888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id))
13451888d3ddSRobin Murphy {
13461888d3ddSRobin Murphy 	int region;
13471888d3ddSRobin Murphy 
13481888d3ddSRobin Murphy 	for (region = 0; region < CCN_NUM_REGIONS; region++) {
13491888d3ddSRobin Murphy 		u32 val, type, id;
13501888d3ddSRobin Murphy 		void __iomem *base;
13511888d3ddSRobin Murphy 		int err;
13521888d3ddSRobin Murphy 
13531888d3ddSRobin Murphy 		val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
13541888d3ddSRobin Murphy 				4 * (region / 32));
13551888d3ddSRobin Murphy 		if (!(val & (1 << (region % 32))))
13561888d3ddSRobin Murphy 			continue;
13571888d3ddSRobin Murphy 
13581888d3ddSRobin Murphy 		base = ccn->base + region * CCN_REGION_SIZE;
13591888d3ddSRobin Murphy 		val = readl(base + CCN_ALL_OLY_ID);
13601888d3ddSRobin Murphy 		type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
13611888d3ddSRobin Murphy 				CCN_ALL_OLY_ID__OLY_ID__MASK;
13621888d3ddSRobin Murphy 		id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
13631888d3ddSRobin Murphy 				CCN_ALL_OLY_ID__NODE_ID__MASK;
13641888d3ddSRobin Murphy 
13651888d3ddSRobin Murphy 		err = callback(ccn, region, base, type, id);
13661888d3ddSRobin Murphy 		if (err)
13671888d3ddSRobin Murphy 			return err;
13681888d3ddSRobin Murphy 	}
13691888d3ddSRobin Murphy 
13701888d3ddSRobin Murphy 	return 0;
13711888d3ddSRobin Murphy }
13721888d3ddSRobin Murphy 
13731888d3ddSRobin Murphy static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
13741888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id)
13751888d3ddSRobin Murphy {
13761888d3ddSRobin Murphy 
13771888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP && id >= ccn->num_xps)
13781888d3ddSRobin Murphy 		ccn->num_xps = id + 1;
13791888d3ddSRobin Murphy 	else if (id >= ccn->num_nodes)
13801888d3ddSRobin Murphy 		ccn->num_nodes = id + 1;
13811888d3ddSRobin Murphy 
13821888d3ddSRobin Murphy 	return 0;
13831888d3ddSRobin Murphy }
13841888d3ddSRobin Murphy 
13851888d3ddSRobin Murphy static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
13861888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id)
13871888d3ddSRobin Murphy {
13881888d3ddSRobin Murphy 	struct arm_ccn_component *component;
13891888d3ddSRobin Murphy 
13901888d3ddSRobin Murphy 	dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
13911888d3ddSRobin Murphy 
13921888d3ddSRobin Murphy 	switch (type) {
13931888d3ddSRobin Murphy 	case CCN_TYPE_MN:
13941888d3ddSRobin Murphy 		ccn->mn_id = id;
13951888d3ddSRobin Murphy 		return 0;
13961888d3ddSRobin Murphy 	case CCN_TYPE_DT:
13971888d3ddSRobin Murphy 		return 0;
13981888d3ddSRobin Murphy 	case CCN_TYPE_XP:
13991888d3ddSRobin Murphy 		component = &ccn->xp[id];
14001888d3ddSRobin Murphy 		break;
14011888d3ddSRobin Murphy 	case CCN_TYPE_SBSX:
14021888d3ddSRobin Murphy 		ccn->sbsx_present = 1;
14031888d3ddSRobin Murphy 		component = &ccn->node[id];
14041888d3ddSRobin Murphy 		break;
14051888d3ddSRobin Murphy 	case CCN_TYPE_SBAS:
14061888d3ddSRobin Murphy 		ccn->sbas_present = 1;
14071888d3ddSRobin Murphy 		/* Fall-through */
14081888d3ddSRobin Murphy 	default:
14091888d3ddSRobin Murphy 		component = &ccn->node[id];
14101888d3ddSRobin Murphy 		break;
14111888d3ddSRobin Murphy 	}
14121888d3ddSRobin Murphy 
14131888d3ddSRobin Murphy 	component->base = base;
14141888d3ddSRobin Murphy 	component->type = type;
14151888d3ddSRobin Murphy 
14161888d3ddSRobin Murphy 	return 0;
14171888d3ddSRobin Murphy }
14181888d3ddSRobin Murphy 
14191888d3ddSRobin Murphy 
14201888d3ddSRobin Murphy static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
14211888d3ddSRobin Murphy 		const u32 *err_sig_val)
14221888d3ddSRobin Murphy {
14231888d3ddSRobin Murphy 	/* This should be really handled by firmware... */
14241888d3ddSRobin Murphy 	dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
14251888d3ddSRobin Murphy 			err_sig_val[5], err_sig_val[4], err_sig_val[3],
14261888d3ddSRobin Murphy 			err_sig_val[2], err_sig_val[1], err_sig_val[0]);
14271888d3ddSRobin Murphy 	dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
14281888d3ddSRobin Murphy 	writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
14291888d3ddSRobin Murphy 			ccn->base + CCN_MN_ERRINT_STATUS);
14301888d3ddSRobin Murphy 
14311888d3ddSRobin Murphy 	return IRQ_HANDLED;
14321888d3ddSRobin Murphy }
14331888d3ddSRobin Murphy 
14341888d3ddSRobin Murphy 
14351888d3ddSRobin Murphy static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
14361888d3ddSRobin Murphy {
14371888d3ddSRobin Murphy 	irqreturn_t res = IRQ_NONE;
14381888d3ddSRobin Murphy 	struct arm_ccn *ccn = dev_id;
14391888d3ddSRobin Murphy 	u32 err_sig_val[6];
14401888d3ddSRobin Murphy 	u32 err_or;
14411888d3ddSRobin Murphy 	int i;
14421888d3ddSRobin Murphy 
14431888d3ddSRobin Murphy 	/* PMU overflow is a special case */
14441888d3ddSRobin Murphy 	err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
14451888d3ddSRobin Murphy 	if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
14461888d3ddSRobin Murphy 		err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
14471888d3ddSRobin Murphy 		res = arm_ccn_pmu_overflow_handler(&ccn->dt);
14481888d3ddSRobin Murphy 	}
14491888d3ddSRobin Murphy 
14501888d3ddSRobin Murphy 	/* Have to read all err_sig_vals to clear them */
14511888d3ddSRobin Murphy 	for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
14521888d3ddSRobin Murphy 		err_sig_val[i] = readl(ccn->base +
14531888d3ddSRobin Murphy 				CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
14541888d3ddSRobin Murphy 		err_or |= err_sig_val[i];
14551888d3ddSRobin Murphy 	}
14561888d3ddSRobin Murphy 	if (err_or)
14571888d3ddSRobin Murphy 		res |= arm_ccn_error_handler(ccn, err_sig_val);
14581888d3ddSRobin Murphy 
14591888d3ddSRobin Murphy 	if (res != IRQ_NONE)
14601888d3ddSRobin Murphy 		writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
14611888d3ddSRobin Murphy 				ccn->base + CCN_MN_ERRINT_STATUS);
14621888d3ddSRobin Murphy 
14631888d3ddSRobin Murphy 	return res;
14641888d3ddSRobin Murphy }
14651888d3ddSRobin Murphy 
14661888d3ddSRobin Murphy 
14671888d3ddSRobin Murphy static int arm_ccn_probe(struct platform_device *pdev)
14681888d3ddSRobin Murphy {
14691888d3ddSRobin Murphy 	struct arm_ccn *ccn;
14701888d3ddSRobin Murphy 	struct resource *res;
14711888d3ddSRobin Murphy 	unsigned int irq;
14721888d3ddSRobin Murphy 	int err;
14731888d3ddSRobin Murphy 
14741888d3ddSRobin Murphy 	ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
14751888d3ddSRobin Murphy 	if (!ccn)
14761888d3ddSRobin Murphy 		return -ENOMEM;
14771888d3ddSRobin Murphy 	ccn->dev = &pdev->dev;
14781888d3ddSRobin Murphy 	platform_set_drvdata(pdev, ccn);
14791888d3ddSRobin Murphy 
14801888d3ddSRobin Murphy 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1481809092dcSSudeep Holla 	ccn->base = devm_ioremap_resource(ccn->dev, res);
1482809092dcSSudeep Holla 	if (IS_ERR(ccn->base))
1483809092dcSSudeep Holla 		return PTR_ERR(ccn->base);
14841888d3ddSRobin Murphy 
14851888d3ddSRobin Murphy 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
14861888d3ddSRobin Murphy 	if (!res)
14871888d3ddSRobin Murphy 		return -EINVAL;
14881888d3ddSRobin Murphy 	irq = res->start;
14891888d3ddSRobin Murphy 
14901888d3ddSRobin Murphy 	/* Check if we can use the interrupt */
14911888d3ddSRobin Murphy 	writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
14921888d3ddSRobin Murphy 			ccn->base + CCN_MN_ERRINT_STATUS);
14931888d3ddSRobin Murphy 	if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
14941888d3ddSRobin Murphy 			CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
14951888d3ddSRobin Murphy 		/* Can set 'disable' bits, so can acknowledge interrupts */
14961888d3ddSRobin Murphy 		writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
14971888d3ddSRobin Murphy 				ccn->base + CCN_MN_ERRINT_STATUS);
14981888d3ddSRobin Murphy 		err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler,
14991888d3ddSRobin Murphy 				       IRQF_NOBALANCING | IRQF_NO_THREAD,
15001888d3ddSRobin Murphy 				       dev_name(ccn->dev), ccn);
15011888d3ddSRobin Murphy 		if (err)
15021888d3ddSRobin Murphy 			return err;
15031888d3ddSRobin Murphy 
15041888d3ddSRobin Murphy 		ccn->irq = irq;
15051888d3ddSRobin Murphy 	}
15061888d3ddSRobin Murphy 
15071888d3ddSRobin Murphy 
15081888d3ddSRobin Murphy 	/* Build topology */
15091888d3ddSRobin Murphy 
15101888d3ddSRobin Murphy 	err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
15111888d3ddSRobin Murphy 	if (err)
15121888d3ddSRobin Murphy 		return err;
15131888d3ddSRobin Murphy 
15141888d3ddSRobin Murphy 	ccn->node = devm_kcalloc(ccn->dev, ccn->num_nodes, sizeof(*ccn->node),
15151888d3ddSRobin Murphy 				 GFP_KERNEL);
15161888d3ddSRobin Murphy 	ccn->xp = devm_kcalloc(ccn->dev, ccn->num_xps, sizeof(*ccn->node),
15171888d3ddSRobin Murphy 			       GFP_KERNEL);
15181888d3ddSRobin Murphy 	if (!ccn->node || !ccn->xp)
15191888d3ddSRobin Murphy 		return -ENOMEM;
15201888d3ddSRobin Murphy 
15211888d3ddSRobin Murphy 	err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
15221888d3ddSRobin Murphy 	if (err)
15231888d3ddSRobin Murphy 		return err;
15241888d3ddSRobin Murphy 
15251888d3ddSRobin Murphy 	return arm_ccn_pmu_init(ccn);
15261888d3ddSRobin Murphy }
15271888d3ddSRobin Murphy 
15281888d3ddSRobin Murphy static int arm_ccn_remove(struct platform_device *pdev)
15291888d3ddSRobin Murphy {
15301888d3ddSRobin Murphy 	struct arm_ccn *ccn = platform_get_drvdata(pdev);
15311888d3ddSRobin Murphy 
15321888d3ddSRobin Murphy 	arm_ccn_pmu_cleanup(ccn);
15331888d3ddSRobin Murphy 
15341888d3ddSRobin Murphy 	return 0;
15351888d3ddSRobin Murphy }
15361888d3ddSRobin Murphy 
15371888d3ddSRobin Murphy static const struct of_device_id arm_ccn_match[] = {
15381888d3ddSRobin Murphy 	{ .compatible = "arm,ccn-502", },
15391888d3ddSRobin Murphy 	{ .compatible = "arm,ccn-504", },
15401888d3ddSRobin Murphy 	{},
15411888d3ddSRobin Murphy };
15421888d3ddSRobin Murphy MODULE_DEVICE_TABLE(of, arm_ccn_match);
15431888d3ddSRobin Murphy 
15441888d3ddSRobin Murphy static struct platform_driver arm_ccn_driver = {
15451888d3ddSRobin Murphy 	.driver = {
15461888d3ddSRobin Murphy 		.name = "arm-ccn",
15471888d3ddSRobin Murphy 		.of_match_table = arm_ccn_match,
15481888d3ddSRobin Murphy 	},
15491888d3ddSRobin Murphy 	.probe = arm_ccn_probe,
15501888d3ddSRobin Murphy 	.remove = arm_ccn_remove,
15511888d3ddSRobin Murphy };
15521888d3ddSRobin Murphy 
15531888d3ddSRobin Murphy static int __init arm_ccn_init(void)
15541888d3ddSRobin Murphy {
15551888d3ddSRobin Murphy 	int i, ret;
15561888d3ddSRobin Murphy 
15571888d3ddSRobin Murphy 	ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE,
15581888d3ddSRobin Murphy 				      "perf/arm/ccn:online", NULL,
15591888d3ddSRobin Murphy 				      arm_ccn_pmu_offline_cpu);
15601888d3ddSRobin Murphy 	if (ret)
15611888d3ddSRobin Murphy 		return ret;
15621888d3ddSRobin Murphy 
15631888d3ddSRobin Murphy 	for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
15641888d3ddSRobin Murphy 		arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
15651888d3ddSRobin Murphy 
15661888d3ddSRobin Murphy 	ret = platform_driver_register(&arm_ccn_driver);
15671888d3ddSRobin Murphy 	if (ret)
15681888d3ddSRobin Murphy 		cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
15691888d3ddSRobin Murphy 	return ret;
15701888d3ddSRobin Murphy }
15711888d3ddSRobin Murphy 
15721888d3ddSRobin Murphy static void __exit arm_ccn_exit(void)
15731888d3ddSRobin Murphy {
15741888d3ddSRobin Murphy 	platform_driver_unregister(&arm_ccn_driver);
15751888d3ddSRobin Murphy 	cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
15761888d3ddSRobin Murphy }
15771888d3ddSRobin Murphy 
15781888d3ddSRobin Murphy module_init(arm_ccn_init);
15791888d3ddSRobin Murphy module_exit(arm_ccn_exit);
15801888d3ddSRobin Murphy 
15811888d3ddSRobin Murphy MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
158275dc3441SRobin Murphy MODULE_LICENSE("GPL v2");
1583