xref: /linux/drivers/perf/arm-ccn.c (revision 84fca8ba620581067c16f2b578f277b1c72fb74b)
11802d0beSThomas 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 
224700a9cf0SZihao Tang 	return sysfs_emit(buf, "%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);
3299ec9f9cfSQi Liu 	int res;
3301888d3ddSRobin Murphy 
3319ec9f9cfSQi Liu 	res = sysfs_emit(buf, "type=0x%x", event->type);
3321888d3ddSRobin Murphy 	if (event->event)
3339ec9f9cfSQi Liu 		res += sysfs_emit_at(buf, res, ",event=0x%x", event->event);
3341888d3ddSRobin Murphy 	if (event->def)
3359ec9f9cfSQi Liu 		res += sysfs_emit_at(buf, res, ",%s", event->def);
3361888d3ddSRobin Murphy 	if (event->mask)
3379ec9f9cfSQi Liu 		res += sysfs_emit_at(buf, res, ",mask=0x%x", event->mask);
3381888d3ddSRobin Murphy 
3391888d3ddSRobin Murphy 	/* Arguments required by an event */
3401888d3ddSRobin Murphy 	switch (event->type) {
3411888d3ddSRobin Murphy 	case CCN_TYPE_CYCLES:
3421888d3ddSRobin Murphy 		break;
3431888d3ddSRobin Murphy 	case CCN_TYPE_XP:
3449ec9f9cfSQi Liu 		res += sysfs_emit_at(buf, res, ",xp=?,vc=?");
3451888d3ddSRobin Murphy 		if (event->event == CCN_EVENT_WATCHPOINT)
3469ec9f9cfSQi Liu 			res += sysfs_emit_at(buf, res,
3471888d3ddSRobin Murphy 					",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
3481888d3ddSRobin Murphy 		else
3499ec9f9cfSQi Liu 			res += sysfs_emit_at(buf, res, ",bus=?");
3501888d3ddSRobin Murphy 
3511888d3ddSRobin Murphy 		break;
3521888d3ddSRobin Murphy 	case CCN_TYPE_MN:
3539ec9f9cfSQi Liu 		res += sysfs_emit_at(buf, res, ",node=%d", ccn->mn_id);
3541888d3ddSRobin Murphy 		break;
3551888d3ddSRobin Murphy 	default:
3569ec9f9cfSQi Liu 		res += sysfs_emit_at(buf, res, ",node=?");
3571888d3ddSRobin Murphy 		break;
3581888d3ddSRobin Murphy 	}
3591888d3ddSRobin Murphy 
3609ec9f9cfSQi Liu 	res += sysfs_emit_at(buf, res, "\n");
3611888d3ddSRobin Murphy 
3621888d3ddSRobin Murphy 	return res;
3631888d3ddSRobin Murphy }
3641888d3ddSRobin Murphy 
3651888d3ddSRobin Murphy static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
3661888d3ddSRobin Murphy 				     struct attribute *attr, int index)
3671888d3ddSRobin Murphy {
3681888d3ddSRobin Murphy 	struct device *dev = kobj_to_dev(kobj);
3691888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
3701888d3ddSRobin Murphy 	struct device_attribute *dev_attr = container_of(attr,
3711888d3ddSRobin Murphy 			struct device_attribute, attr);
3721888d3ddSRobin Murphy 	struct arm_ccn_pmu_event *event = container_of(dev_attr,
3731888d3ddSRobin Murphy 			struct arm_ccn_pmu_event, attr);
3741888d3ddSRobin Murphy 
3751888d3ddSRobin Murphy 	if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
3761888d3ddSRobin Murphy 		return 0;
3771888d3ddSRobin Murphy 	if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
3781888d3ddSRobin Murphy 		return 0;
3791888d3ddSRobin Murphy 
3801888d3ddSRobin Murphy 	return attr->mode;
3811888d3ddSRobin Murphy }
3821888d3ddSRobin Murphy 
3831888d3ddSRobin Murphy static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
3841888d3ddSRobin Murphy 	CCN_EVENT_MN(eobarrier, "dir=1,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
3851888d3ddSRobin Murphy 	CCN_EVENT_MN(ecbarrier, "dir=1,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
3861888d3ddSRobin Murphy 	CCN_EVENT_MN(dvmop, "dir=1,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
3871888d3ddSRobin Murphy 	CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
3881888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
3891888d3ddSRobin Murphy 	CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
3901888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
3911888d3ddSRobin Murphy 	CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
3921888d3ddSRobin Murphy 			CCN_IDX_MASK_ORDER),
3931888d3ddSRobin Murphy 	CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
3941888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
3951888d3ddSRobin Murphy 	CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
3961888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
3971888d3ddSRobin Murphy 	CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
3981888d3ddSRobin Murphy 			CCN_IDX_MASK_ORDER),
3991888d3ddSRobin Murphy 	CCN_EVENT_HNF(cache_miss, 0x1),
4001888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
4011888d3ddSRobin Murphy 	CCN_EVENT_HNF(cache_fill, 0x3),
4021888d3ddSRobin Murphy 	CCN_EVENT_HNF(pocq_retry, 0x4),
4031888d3ddSRobin Murphy 	CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
4041888d3ddSRobin Murphy 	CCN_EVENT_HNF(sf_hit, 0x6),
4051888d3ddSRobin Murphy 	CCN_EVENT_HNF(sf_evictions, 0x7),
4061888d3ddSRobin Murphy 	CCN_EVENT_HNF(snoops_sent, 0x8),
4071888d3ddSRobin Murphy 	CCN_EVENT_HNF(snoops_broadcast, 0x9),
4081888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_eviction, 0xa),
4091888d3ddSRobin Murphy 	CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
4101888d3ddSRobin Murphy 	CCN_EVENT_HNF(mc_retries, 0xc),
4111888d3ddSRobin Murphy 	CCN_EVENT_HNF(mc_reqs, 0xd),
4121888d3ddSRobin Murphy 	CCN_EVENT_HNF(qos_hh_retry, 0xe),
4131888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p0, 0x1),
4141888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p1, 0x2),
4151888d3ddSRobin Murphy 	CCN_EVENT_RNI(rdata_beats_p2, 0x3),
4161888d3ddSRobin Murphy 	CCN_EVENT_RNI(rxdat_flits, 0x4),
4171888d3ddSRobin Murphy 	CCN_EVENT_RNI(txdat_flits, 0x5),
4181888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits, 0x6),
4191888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits_retried, 0x7),
4201888d3ddSRobin Murphy 	CCN_EVENT_RNI(rrt_full, 0x8),
4211888d3ddSRobin Murphy 	CCN_EVENT_RNI(wrt_full, 0x9),
4221888d3ddSRobin Murphy 	CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
4231888d3ddSRobin Murphy 	CCN_EVENT_XP(upload_starvation, 0x1),
4241888d3ddSRobin Murphy 	CCN_EVENT_XP(download_starvation, 0x2),
4251888d3ddSRobin Murphy 	CCN_EVENT_XP(respin, 0x3),
4261888d3ddSRobin Murphy 	CCN_EVENT_XP(valid_flit, 0x4),
4271888d3ddSRobin Murphy 	CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
4281888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
4291888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rxdat_flits, 0x4),
4301888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txdat_flits, 0x5),
4311888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits, 0x6),
4321888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
4331888d3ddSRobin Murphy 	CCN_EVENT_SBAS(rrt_full, 0x8),
4341888d3ddSRobin Murphy 	CCN_EVENT_SBAS(wrt_full, 0x9),
4351888d3ddSRobin Murphy 	CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
4361888d3ddSRobin Murphy 	CCN_EVENT_CYCLES(cycles),
4371888d3ddSRobin Murphy };
4381888d3ddSRobin Murphy 
4391888d3ddSRobin Murphy /* Populated in arm_ccn_init() */
4401888d3ddSRobin Murphy static struct attribute
4411888d3ddSRobin Murphy 		*arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
4421888d3ddSRobin Murphy 
4431888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_events_attr_group = {
4441888d3ddSRobin Murphy 	.name = "events",
4451888d3ddSRobin Murphy 	.is_visible = arm_ccn_pmu_events_is_visible,
4461888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_events_attrs,
4471888d3ddSRobin Murphy };
4481888d3ddSRobin Murphy 
4491888d3ddSRobin Murphy 
4501888d3ddSRobin Murphy static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
4511888d3ddSRobin Murphy {
4521888d3ddSRobin Murphy 	unsigned long i;
4531888d3ddSRobin Murphy 
4541888d3ddSRobin Murphy 	if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
4551888d3ddSRobin Murphy 		return NULL;
4561888d3ddSRobin Murphy 	i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
4571888d3ddSRobin Murphy 
4581888d3ddSRobin Murphy 	switch (name[1]) {
4591888d3ddSRobin Murphy 	case 'l':
4601888d3ddSRobin Murphy 		return &ccn->dt.cmp_mask[i].l;
4611888d3ddSRobin Murphy 	case 'h':
4621888d3ddSRobin Murphy 		return &ccn->dt.cmp_mask[i].h;
4631888d3ddSRobin Murphy 	default:
4641888d3ddSRobin Murphy 		return NULL;
4651888d3ddSRobin Murphy 	}
4661888d3ddSRobin Murphy }
4671888d3ddSRobin Murphy 
4681888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
4691888d3ddSRobin Murphy 		struct device_attribute *attr, char *buf)
4701888d3ddSRobin Murphy {
4711888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
4721888d3ddSRobin Murphy 	u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
4731888d3ddSRobin Murphy 
474700a9cf0SZihao Tang 	return mask ? sysfs_emit(buf, "0x%016llx\n", *mask) : -EINVAL;
4751888d3ddSRobin Murphy }
4761888d3ddSRobin Murphy 
4771888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
4781888d3ddSRobin Murphy 		struct device_attribute *attr, const char *buf, size_t count)
4791888d3ddSRobin Murphy {
4801888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
4811888d3ddSRobin Murphy 	u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
4821888d3ddSRobin Murphy 	int err = -EINVAL;
4831888d3ddSRobin Murphy 
4841888d3ddSRobin Murphy 	if (mask)
4851888d3ddSRobin Murphy 		err = kstrtoull(buf, 0, mask);
4861888d3ddSRobin Murphy 
4871888d3ddSRobin Murphy 	return err ? err : count;
4881888d3ddSRobin Murphy }
4891888d3ddSRobin Murphy 
4901888d3ddSRobin Murphy #define CCN_CMP_MASK_ATTR(_name) \
4911888d3ddSRobin Murphy 	struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
4921888d3ddSRobin Murphy 			__ATTR(_name, S_IRUGO | S_IWUSR, \
4931888d3ddSRobin Murphy 			arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
4941888d3ddSRobin Murphy 
4951888d3ddSRobin Murphy #define CCN_CMP_MASK_ATTR_RO(_name) \
4961888d3ddSRobin Murphy 	struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
4971888d3ddSRobin Murphy 			__ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
4981888d3ddSRobin Murphy 
4991888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(0l);
5001888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(0h);
5011888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(1l);
5021888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(1h);
5031888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(2l);
5041888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(2h);
5051888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(3l);
5061888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(3h);
5071888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(4l);
5081888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(4h);
5091888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(5l);
5101888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(5h);
5111888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(6l);
5121888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(6h);
5131888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(7l);
5141888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR(7h);
5151888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(8l);
5161888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(8h);
5171888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(9l);
5181888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(9h);
5191888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(al);
5201888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(ah);
5211888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(bl);
5221888d3ddSRobin Murphy static CCN_CMP_MASK_ATTR_RO(bh);
5231888d3ddSRobin Murphy 
5241888d3ddSRobin Murphy static struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
5251888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
5261888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
5271888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
5281888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
5291888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
5301888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
5311888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
5321888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
5331888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
5341888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
5351888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
5361888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
5371888d3ddSRobin Murphy 	NULL
5381888d3ddSRobin Murphy };
5391888d3ddSRobin Murphy 
5401888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
5411888d3ddSRobin Murphy 	.name = "cmp_mask",
5421888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_cmp_mask_attrs,
5431888d3ddSRobin Murphy };
5441888d3ddSRobin Murphy 
5451888d3ddSRobin Murphy static ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
5461888d3ddSRobin Murphy 				     struct device_attribute *attr, char *buf)
5471888d3ddSRobin Murphy {
5481888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
5491888d3ddSRobin Murphy 
5509bcb929fSRobin Murphy 	return cpumap_print_to_pagebuf(true, buf, cpumask_of(ccn->dt.cpu));
5511888d3ddSRobin Murphy }
5521888d3ddSRobin Murphy 
5531888d3ddSRobin Murphy static struct device_attribute arm_ccn_pmu_cpumask_attr =
5541888d3ddSRobin Murphy 		__ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
5551888d3ddSRobin Murphy 
5561888d3ddSRobin Murphy static struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
5571888d3ddSRobin Murphy 	&arm_ccn_pmu_cpumask_attr.attr,
5581888d3ddSRobin Murphy 	NULL,
5591888d3ddSRobin Murphy };
5601888d3ddSRobin Murphy 
5611888d3ddSRobin Murphy static const struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
5621888d3ddSRobin Murphy 	.attrs = arm_ccn_pmu_cpumask_attrs,
5631888d3ddSRobin Murphy };
5641888d3ddSRobin Murphy 
5651888d3ddSRobin Murphy /*
5661888d3ddSRobin Murphy  * Default poll period is 10ms, which is way over the top anyway,
5671888d3ddSRobin Murphy  * as in the worst case scenario (an event every cycle), with 1GHz
5681888d3ddSRobin Murphy  * clocked bus, the smallest, 32 bit counter will overflow in
5691888d3ddSRobin Murphy  * more than 4s.
5701888d3ddSRobin Murphy  */
5711888d3ddSRobin Murphy static unsigned int arm_ccn_pmu_poll_period_us = 10000;
5721888d3ddSRobin Murphy module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
5731888d3ddSRobin Murphy 		S_IRUGO | S_IWUSR);
5741888d3ddSRobin Murphy 
5751888d3ddSRobin Murphy static ktime_t arm_ccn_pmu_timer_period(void)
5761888d3ddSRobin Murphy {
5771888d3ddSRobin Murphy 	return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
5781888d3ddSRobin Murphy }
5791888d3ddSRobin Murphy 
5801888d3ddSRobin Murphy 
5811888d3ddSRobin Murphy static const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
5821888d3ddSRobin Murphy 	&arm_ccn_pmu_events_attr_group,
5831888d3ddSRobin Murphy 	&arm_ccn_pmu_format_attr_group,
5841888d3ddSRobin Murphy 	&arm_ccn_pmu_cmp_mask_attr_group,
5851888d3ddSRobin Murphy 	&arm_ccn_pmu_cpumask_attr_group,
5861888d3ddSRobin Murphy 	NULL
5871888d3ddSRobin Murphy };
5881888d3ddSRobin Murphy 
5891888d3ddSRobin Murphy 
5901888d3ddSRobin Murphy static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
5911888d3ddSRobin Murphy {
5921888d3ddSRobin Murphy 	int bit;
5931888d3ddSRobin Murphy 
5941888d3ddSRobin Murphy 	do {
5951888d3ddSRobin Murphy 		bit = find_first_zero_bit(bitmap, size);
5961888d3ddSRobin Murphy 		if (bit >= size)
5971888d3ddSRobin Murphy 			return -EAGAIN;
5981888d3ddSRobin Murphy 	} while (test_and_set_bit(bit, bitmap));
5991888d3ddSRobin Murphy 
6001888d3ddSRobin Murphy 	return bit;
6011888d3ddSRobin Murphy }
6021888d3ddSRobin Murphy 
6031888d3ddSRobin Murphy /* All RN-I and RN-D nodes have identical PMUs */
6041888d3ddSRobin Murphy static int arm_ccn_pmu_type_eq(u32 a, u32 b)
6051888d3ddSRobin Murphy {
6061888d3ddSRobin Murphy 	if (a == b)
6071888d3ddSRobin Murphy 		return 1;
6081888d3ddSRobin Murphy 
6091888d3ddSRobin Murphy 	switch (a) {
6101888d3ddSRobin Murphy 	case CCN_TYPE_RNI_1P:
6111888d3ddSRobin Murphy 	case CCN_TYPE_RNI_2P:
6121888d3ddSRobin Murphy 	case CCN_TYPE_RNI_3P:
6131888d3ddSRobin Murphy 	case CCN_TYPE_RND_1P:
6141888d3ddSRobin Murphy 	case CCN_TYPE_RND_2P:
6151888d3ddSRobin Murphy 	case CCN_TYPE_RND_3P:
6161888d3ddSRobin Murphy 		switch (b) {
6171888d3ddSRobin Murphy 		case CCN_TYPE_RNI_1P:
6181888d3ddSRobin Murphy 		case CCN_TYPE_RNI_2P:
6191888d3ddSRobin Murphy 		case CCN_TYPE_RNI_3P:
6201888d3ddSRobin Murphy 		case CCN_TYPE_RND_1P:
6211888d3ddSRobin Murphy 		case CCN_TYPE_RND_2P:
6221888d3ddSRobin Murphy 		case CCN_TYPE_RND_3P:
6231888d3ddSRobin Murphy 			return 1;
6241888d3ddSRobin Murphy 		}
6251888d3ddSRobin Murphy 		break;
6261888d3ddSRobin Murphy 	}
6271888d3ddSRobin Murphy 
6281888d3ddSRobin Murphy 	return 0;
6291888d3ddSRobin Murphy }
6301888d3ddSRobin Murphy 
6311888d3ddSRobin Murphy static int arm_ccn_pmu_event_alloc(struct perf_event *event)
6321888d3ddSRobin Murphy {
6331888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
6341888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
6351888d3ddSRobin Murphy 	u32 node_xp, type, event_id;
6361888d3ddSRobin Murphy 	struct arm_ccn_component *source;
6371888d3ddSRobin Murphy 	int bit;
6381888d3ddSRobin Murphy 
6391888d3ddSRobin Murphy 	node_xp = CCN_CONFIG_NODE(event->attr.config);
6401888d3ddSRobin Murphy 	type = CCN_CONFIG_TYPE(event->attr.config);
6411888d3ddSRobin Murphy 	event_id = CCN_CONFIG_EVENT(event->attr.config);
6421888d3ddSRobin Murphy 
6431888d3ddSRobin Murphy 	/* Allocate the cycle counter */
6441888d3ddSRobin Murphy 	if (type == CCN_TYPE_CYCLES) {
6451888d3ddSRobin Murphy 		if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
6461888d3ddSRobin Murphy 				ccn->dt.pmu_counters_mask))
6471888d3ddSRobin Murphy 			return -EAGAIN;
6481888d3ddSRobin Murphy 
6491888d3ddSRobin Murphy 		hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
6501888d3ddSRobin Murphy 		ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
6511888d3ddSRobin Murphy 
6521888d3ddSRobin Murphy 		return 0;
6531888d3ddSRobin Murphy 	}
6541888d3ddSRobin Murphy 
6551888d3ddSRobin Murphy 	/* Allocate an event counter */
6561888d3ddSRobin Murphy 	hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
6571888d3ddSRobin Murphy 			CCN_NUM_PMU_EVENT_COUNTERS);
6581888d3ddSRobin Murphy 	if (hw->idx < 0) {
6591888d3ddSRobin Murphy 		dev_dbg(ccn->dev, "No more counters available!\n");
6601888d3ddSRobin Murphy 		return -EAGAIN;
6611888d3ddSRobin Murphy 	}
6621888d3ddSRobin Murphy 
6631888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP)
6641888d3ddSRobin Murphy 		source = &ccn->xp[node_xp];
6651888d3ddSRobin Murphy 	else
6661888d3ddSRobin Murphy 		source = &ccn->node[node_xp];
6671888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].source = source;
6681888d3ddSRobin Murphy 
6691888d3ddSRobin Murphy 	/* Allocate an event source or a watchpoint */
6701888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
6711888d3ddSRobin Murphy 		bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
6721888d3ddSRobin Murphy 				CCN_NUM_XP_WATCHPOINTS);
6731888d3ddSRobin Murphy 	else
6741888d3ddSRobin Murphy 		bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
6751888d3ddSRobin Murphy 				CCN_NUM_PMU_EVENTS);
6761888d3ddSRobin Murphy 	if (bit < 0) {
6771888d3ddSRobin Murphy 		dev_dbg(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
6781888d3ddSRobin Murphy 				node_xp);
6791888d3ddSRobin Murphy 		clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
6801888d3ddSRobin Murphy 		return -EAGAIN;
6811888d3ddSRobin Murphy 	}
6821888d3ddSRobin Murphy 	hw->config_base = bit;
6831888d3ddSRobin Murphy 
6841888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].event = event;
6851888d3ddSRobin Murphy 
6861888d3ddSRobin Murphy 	return 0;
6871888d3ddSRobin Murphy }
6881888d3ddSRobin Murphy 
6891888d3ddSRobin Murphy static void arm_ccn_pmu_event_release(struct perf_event *event)
6901888d3ddSRobin Murphy {
6911888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
6921888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
6931888d3ddSRobin Murphy 
6941888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
6951888d3ddSRobin Murphy 		clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
6961888d3ddSRobin Murphy 	} else {
6971888d3ddSRobin Murphy 		struct arm_ccn_component *source =
6981888d3ddSRobin Murphy 				ccn->dt.pmu_counters[hw->idx].source;
6991888d3ddSRobin Murphy 
7001888d3ddSRobin Murphy 		if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
7011888d3ddSRobin Murphy 				CCN_CONFIG_EVENT(event->attr.config) ==
7021888d3ddSRobin Murphy 				CCN_EVENT_WATCHPOINT)
7031888d3ddSRobin Murphy 			clear_bit(hw->config_base, source->xp.dt_cmp_mask);
7041888d3ddSRobin Murphy 		else
7051888d3ddSRobin Murphy 			clear_bit(hw->config_base, source->pmu_events_mask);
7061888d3ddSRobin Murphy 		clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
7071888d3ddSRobin Murphy 	}
7081888d3ddSRobin Murphy 
7091888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].source = NULL;
7101888d3ddSRobin Murphy 	ccn->dt.pmu_counters[hw->idx].event = NULL;
7111888d3ddSRobin Murphy }
7121888d3ddSRobin Murphy 
7131888d3ddSRobin Murphy static int arm_ccn_pmu_event_init(struct perf_event *event)
7141888d3ddSRobin Murphy {
7151888d3ddSRobin Murphy 	struct arm_ccn *ccn;
7161888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
7171888d3ddSRobin Murphy 	u32 node_xp, type, event_id;
7181888d3ddSRobin Murphy 	int valid;
7191888d3ddSRobin Murphy 	int i;
7201888d3ddSRobin Murphy 	struct perf_event *sibling;
7211888d3ddSRobin Murphy 
7221888d3ddSRobin Murphy 	if (event->attr.type != event->pmu->type)
7231888d3ddSRobin Murphy 		return -ENOENT;
7241888d3ddSRobin Murphy 
7251888d3ddSRobin Murphy 	ccn = pmu_to_arm_ccn(event->pmu);
7261888d3ddSRobin Murphy 
7271888d3ddSRobin Murphy 	if (hw->sample_period) {
7281898eb61SMark Rutland 		dev_dbg(ccn->dev, "Sampling not supported!\n");
7291888d3ddSRobin Murphy 		return -EOPNOTSUPP;
7301888d3ddSRobin Murphy 	}
7311888d3ddSRobin Murphy 
73230656398SAndrew Murray 	if (has_branch_stack(event)) {
7331898eb61SMark Rutland 		dev_dbg(ccn->dev, "Can't exclude execution levels!\n");
7341888d3ddSRobin Murphy 		return -EINVAL;
7351888d3ddSRobin Murphy 	}
7361888d3ddSRobin Murphy 
7371888d3ddSRobin Murphy 	if (event->cpu < 0) {
7381898eb61SMark Rutland 		dev_dbg(ccn->dev, "Can't provide per-task data!\n");
7391888d3ddSRobin Murphy 		return -EOPNOTSUPP;
7401888d3ddSRobin Murphy 	}
7411888d3ddSRobin Murphy 	/*
7421888d3ddSRobin Murphy 	 * Many perf core operations (eg. events rotation) operate on a
7431888d3ddSRobin Murphy 	 * single CPU context. This is obvious for CPU PMUs, where one
7441888d3ddSRobin Murphy 	 * expects the same sets of events being observed on all CPUs,
7451888d3ddSRobin Murphy 	 * but can lead to issues for off-core PMUs, like CCN, where each
7461888d3ddSRobin Murphy 	 * event could be theoretically assigned to a different CPU. To
7471888d3ddSRobin Murphy 	 * mitigate this, we enforce CPU assignment to one, selected
7481888d3ddSRobin Murphy 	 * processor (the one described in the "cpumask" attribute).
7491888d3ddSRobin Murphy 	 */
7509bcb929fSRobin Murphy 	event->cpu = ccn->dt.cpu;
7511888d3ddSRobin Murphy 
7521888d3ddSRobin Murphy 	node_xp = CCN_CONFIG_NODE(event->attr.config);
7531888d3ddSRobin Murphy 	type = CCN_CONFIG_TYPE(event->attr.config);
7541888d3ddSRobin Murphy 	event_id = CCN_CONFIG_EVENT(event->attr.config);
7551888d3ddSRobin Murphy 
7561888d3ddSRobin Murphy 	/* Validate node/xp vs topology */
7571888d3ddSRobin Murphy 	switch (type) {
7581888d3ddSRobin Murphy 	case CCN_TYPE_MN:
7591888d3ddSRobin Murphy 		if (node_xp != ccn->mn_id) {
7601898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid MN ID %d!\n", node_xp);
7611888d3ddSRobin Murphy 			return -EINVAL;
7621888d3ddSRobin Murphy 		}
7631888d3ddSRobin Murphy 		break;
7641888d3ddSRobin Murphy 	case CCN_TYPE_XP:
7651888d3ddSRobin Murphy 		if (node_xp >= ccn->num_xps) {
7661898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid XP ID %d!\n", node_xp);
7671888d3ddSRobin Murphy 			return -EINVAL;
7681888d3ddSRobin Murphy 		}
7691888d3ddSRobin Murphy 		break;
7701888d3ddSRobin Murphy 	case CCN_TYPE_CYCLES:
7711888d3ddSRobin Murphy 		break;
7721888d3ddSRobin Murphy 	default:
7731888d3ddSRobin Murphy 		if (node_xp >= ccn->num_nodes) {
7741898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid node ID %d!\n", node_xp);
7751888d3ddSRobin Murphy 			return -EINVAL;
7761888d3ddSRobin Murphy 		}
7771888d3ddSRobin Murphy 		if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
7781898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid type 0x%x for node %d!\n",
7791888d3ddSRobin Murphy 					type, node_xp);
7801888d3ddSRobin Murphy 			return -EINVAL;
7811888d3ddSRobin Murphy 		}
7821888d3ddSRobin Murphy 		break;
7831888d3ddSRobin Murphy 	}
7841888d3ddSRobin Murphy 
7851888d3ddSRobin Murphy 	/* Validate event ID vs available for the type */
7861888d3ddSRobin Murphy 	for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
7871888d3ddSRobin Murphy 			i++) {
7881888d3ddSRobin Murphy 		struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
7891888d3ddSRobin Murphy 		u32 port = CCN_CONFIG_PORT(event->attr.config);
7901888d3ddSRobin Murphy 		u32 vc = CCN_CONFIG_VC(event->attr.config);
7911888d3ddSRobin Murphy 
7921888d3ddSRobin Murphy 		if (!arm_ccn_pmu_type_eq(type, e->type))
7931888d3ddSRobin Murphy 			continue;
7941888d3ddSRobin Murphy 		if (event_id != e->event)
7951888d3ddSRobin Murphy 			continue;
7961888d3ddSRobin Murphy 		if (e->num_ports && port >= e->num_ports) {
7971898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid port %d for node/XP %d!\n",
7981888d3ddSRobin Murphy 					port, node_xp);
7991888d3ddSRobin Murphy 			return -EINVAL;
8001888d3ddSRobin Murphy 		}
8011888d3ddSRobin Murphy 		if (e->num_vcs && vc >= e->num_vcs) {
8021898eb61SMark Rutland 			dev_dbg(ccn->dev, "Invalid vc %d for node/XP %d!\n",
8031888d3ddSRobin Murphy 					vc, node_xp);
8041888d3ddSRobin Murphy 			return -EINVAL;
8051888d3ddSRobin Murphy 		}
8061888d3ddSRobin Murphy 		valid = 1;
8071888d3ddSRobin Murphy 	}
8081888d3ddSRobin Murphy 	if (!valid) {
8091898eb61SMark Rutland 		dev_dbg(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
8101888d3ddSRobin Murphy 				event_id, node_xp);
8111888d3ddSRobin Murphy 		return -EINVAL;
8121888d3ddSRobin Murphy 	}
8131888d3ddSRobin Murphy 
8141888d3ddSRobin Murphy 	/* Watchpoint-based event for a node is actually set on XP */
8151888d3ddSRobin Murphy 	if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
8161888d3ddSRobin Murphy 		u32 port;
8171888d3ddSRobin Murphy 
8181888d3ddSRobin Murphy 		type = CCN_TYPE_XP;
8191888d3ddSRobin Murphy 		port = arm_ccn_node_to_xp_port(node_xp);
8201888d3ddSRobin Murphy 		node_xp = arm_ccn_node_to_xp(node_xp);
8211888d3ddSRobin Murphy 
8221888d3ddSRobin Murphy 		arm_ccn_pmu_config_set(&event->attr.config,
8231888d3ddSRobin Murphy 				node_xp, type, port);
8241888d3ddSRobin Murphy 	}
8251888d3ddSRobin Murphy 
8261888d3ddSRobin Murphy 	/*
8271888d3ddSRobin Murphy 	 * We must NOT create groups containing mixed PMUs, although software
8281888d3ddSRobin Murphy 	 * events are acceptable (for example to create a CCN group
8291888d3ddSRobin Murphy 	 * periodically read when a hrtimer aka cpu-clock leader triggers).
8301888d3ddSRobin Murphy 	 */
8311888d3ddSRobin Murphy 	if (event->group_leader->pmu != event->pmu &&
8321888d3ddSRobin Murphy 			!is_software_event(event->group_leader))
8331888d3ddSRobin Murphy 		return -EINVAL;
8341888d3ddSRobin Murphy 
83538c23685SLinus Torvalds 	for_each_sibling_event(sibling, event->group_leader) {
8361888d3ddSRobin Murphy 		if (sibling->pmu != event->pmu &&
8371888d3ddSRobin Murphy 				!is_software_event(sibling))
8381888d3ddSRobin Murphy 			return -EINVAL;
83938c23685SLinus Torvalds 	}
8401888d3ddSRobin Murphy 
8411888d3ddSRobin Murphy 	return 0;
8421888d3ddSRobin Murphy }
8431888d3ddSRobin Murphy 
8441888d3ddSRobin Murphy static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
8451888d3ddSRobin Murphy {
8461888d3ddSRobin Murphy 	u64 res;
8471888d3ddSRobin Murphy 
8481888d3ddSRobin Murphy 	if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
8491888d3ddSRobin Murphy #ifdef readq
8501888d3ddSRobin Murphy 		res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
8511888d3ddSRobin Murphy #else
8521888d3ddSRobin Murphy 		/* 40 bit counter, can do snapshot and read in two parts */
8531888d3ddSRobin Murphy 		writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
8541888d3ddSRobin Murphy 		while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
8551888d3ddSRobin Murphy 			;
8561888d3ddSRobin Murphy 		writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
8571888d3ddSRobin Murphy 		res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
8581888d3ddSRobin Murphy 		res <<= 32;
8591888d3ddSRobin Murphy 		res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
8601888d3ddSRobin Murphy #endif
8611888d3ddSRobin Murphy 	} else {
8621888d3ddSRobin Murphy 		res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
8631888d3ddSRobin Murphy 	}
8641888d3ddSRobin Murphy 
8651888d3ddSRobin Murphy 	return res;
8661888d3ddSRobin Murphy }
8671888d3ddSRobin Murphy 
8681888d3ddSRobin Murphy static void arm_ccn_pmu_event_update(struct perf_event *event)
8691888d3ddSRobin Murphy {
8701888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
8711888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
8721888d3ddSRobin Murphy 	u64 prev_count, new_count, mask;
8731888d3ddSRobin Murphy 
8741888d3ddSRobin Murphy 	do {
8751888d3ddSRobin Murphy 		prev_count = local64_read(&hw->prev_count);
8761888d3ddSRobin Murphy 		new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
8771888d3ddSRobin Murphy 	} while (local64_xchg(&hw->prev_count, new_count) != prev_count);
8781888d3ddSRobin Murphy 
8791888d3ddSRobin Murphy 	mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
8801888d3ddSRobin Murphy 
8811888d3ddSRobin Murphy 	local64_add((new_count - prev_count) & mask, &event->count);
8821888d3ddSRobin Murphy }
8831888d3ddSRobin Murphy 
8841888d3ddSRobin Murphy static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
8851888d3ddSRobin Murphy {
8861888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
8871888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
8881888d3ddSRobin Murphy 	struct arm_ccn_component *xp;
8891888d3ddSRobin Murphy 	u32 val, dt_cfg;
8901888d3ddSRobin Murphy 
8911888d3ddSRobin Murphy 	/* Nothing to do for cycle counter */
8921888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
8931888d3ddSRobin Murphy 		return;
8941888d3ddSRobin Murphy 
8951888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
8961888d3ddSRobin Murphy 		xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
8971888d3ddSRobin Murphy 	else
8981888d3ddSRobin Murphy 		xp = &ccn->xp[arm_ccn_node_to_xp(
8991888d3ddSRobin Murphy 				CCN_CONFIG_NODE(event->attr.config))];
9001888d3ddSRobin Murphy 
9011888d3ddSRobin Murphy 	if (enable)
9021888d3ddSRobin Murphy 		dt_cfg = hw->event_base;
9031888d3ddSRobin Murphy 	else
9041888d3ddSRobin Murphy 		dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
9051888d3ddSRobin Murphy 
9061888d3ddSRobin Murphy 	spin_lock(&ccn->dt.config_lock);
9071888d3ddSRobin Murphy 
9081888d3ddSRobin Murphy 	val = readl(xp->base + CCN_XP_DT_CONFIG);
9091888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
9101888d3ddSRobin Murphy 			CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
9111888d3ddSRobin Murphy 	val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
9121888d3ddSRobin Murphy 	writel(val, xp->base + CCN_XP_DT_CONFIG);
9131888d3ddSRobin Murphy 
9141888d3ddSRobin Murphy 	spin_unlock(&ccn->dt.config_lock);
9151888d3ddSRobin Murphy }
9161888d3ddSRobin Murphy 
9171888d3ddSRobin Murphy static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
9181888d3ddSRobin Murphy {
9191888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9201888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
9211888d3ddSRobin Murphy 
9221888d3ddSRobin Murphy 	local64_set(&event->hw.prev_count,
9231888d3ddSRobin Murphy 			arm_ccn_pmu_read_counter(ccn, hw->idx));
9241888d3ddSRobin Murphy 	hw->state = 0;
9251888d3ddSRobin Murphy 
9261888d3ddSRobin Murphy 	/* Set the DT bus input, engaging the counter */
9271888d3ddSRobin Murphy 	arm_ccn_pmu_xp_dt_config(event, 1);
9281888d3ddSRobin Murphy }
9291888d3ddSRobin Murphy 
9301888d3ddSRobin Murphy static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
9311888d3ddSRobin Murphy {
9321888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
9331888d3ddSRobin Murphy 
9341888d3ddSRobin Murphy 	/* Disable counting, setting the DT bus to pass-through mode */
9351888d3ddSRobin Murphy 	arm_ccn_pmu_xp_dt_config(event, 0);
9361888d3ddSRobin Murphy 
9371888d3ddSRobin Murphy 	if (flags & PERF_EF_UPDATE)
9381888d3ddSRobin Murphy 		arm_ccn_pmu_event_update(event);
9391888d3ddSRobin Murphy 
9401888d3ddSRobin Murphy 	hw->state |= PERF_HES_STOPPED;
9411888d3ddSRobin Murphy }
9421888d3ddSRobin Murphy 
9431888d3ddSRobin Murphy static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
9441888d3ddSRobin Murphy {
9451888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9461888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
9471888d3ddSRobin Murphy 	struct arm_ccn_component *source =
9481888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
9491888d3ddSRobin Murphy 	unsigned long wp = hw->config_base;
9501888d3ddSRobin Murphy 	u32 val;
9511888d3ddSRobin Murphy 	u64 cmp_l = event->attr.config1;
9521888d3ddSRobin Murphy 	u64 cmp_h = event->attr.config2;
9531888d3ddSRobin Murphy 	u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
9541888d3ddSRobin Murphy 	u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
9551888d3ddSRobin Murphy 
9561888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
9571888d3ddSRobin Murphy 
9581888d3ddSRobin Murphy 	/* Direction (RX/TX), device (port) & virtual channel */
9591888d3ddSRobin Murphy 	val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
9601888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
9611888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
9621888d3ddSRobin Murphy 	val |= CCN_CONFIG_DIR(event->attr.config) <<
9631888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
9641888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
9651888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
9661888d3ddSRobin Murphy 	val |= CCN_CONFIG_PORT(event->attr.config) <<
9671888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
9681888d3ddSRobin Murphy 	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
9691888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
9701888d3ddSRobin Murphy 	val |= CCN_CONFIG_VC(event->attr.config) <<
9711888d3ddSRobin Murphy 			CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
9721888d3ddSRobin Murphy 	writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
9731888d3ddSRobin Murphy 
9741888d3ddSRobin Murphy 	/* Comparison values */
9751888d3ddSRobin Murphy 	writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
9761888d3ddSRobin Murphy 	writel((cmp_l >> 32) & 0x7fffffff,
9771888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
9781888d3ddSRobin Murphy 	writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
9791888d3ddSRobin Murphy 	writel((cmp_h >> 32) & 0x0fffffff,
9801888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
9811888d3ddSRobin Murphy 
9821888d3ddSRobin Murphy 	/* Mask */
9831888d3ddSRobin Murphy 	writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
9841888d3ddSRobin Murphy 	writel((mask_l >> 32) & 0x7fffffff,
9851888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
9861888d3ddSRobin Murphy 	writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
9871888d3ddSRobin Murphy 	writel((mask_h >> 32) & 0x0fffffff,
9881888d3ddSRobin Murphy 			source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
9891888d3ddSRobin Murphy }
9901888d3ddSRobin Murphy 
9911888d3ddSRobin Murphy static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
9921888d3ddSRobin Murphy {
9931888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9941888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
9951888d3ddSRobin Murphy 	struct arm_ccn_component *source =
9961888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
9971888d3ddSRobin Murphy 	u32 val, id;
9981888d3ddSRobin Murphy 
9991888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
10001888d3ddSRobin Murphy 
10011888d3ddSRobin Murphy 	id = (CCN_CONFIG_VC(event->attr.config) << 4) |
10021888d3ddSRobin Murphy 			(CCN_CONFIG_BUS(event->attr.config) << 3) |
10031888d3ddSRobin Murphy 			(CCN_CONFIG_EVENT(event->attr.config) << 0);
10041888d3ddSRobin Murphy 
10051888d3ddSRobin Murphy 	val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
10061888d3ddSRobin Murphy 	val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
10071888d3ddSRobin Murphy 			CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
10081888d3ddSRobin Murphy 	val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
10091888d3ddSRobin Murphy 	writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
10101888d3ddSRobin Murphy }
10111888d3ddSRobin Murphy 
10121888d3ddSRobin Murphy static void arm_ccn_pmu_node_event_config(struct perf_event *event)
10131888d3ddSRobin Murphy {
10141888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
10151888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
10161888d3ddSRobin Murphy 	struct arm_ccn_component *source =
10171888d3ddSRobin Murphy 			ccn->dt.pmu_counters[hw->idx].source;
10181888d3ddSRobin Murphy 	u32 type = CCN_CONFIG_TYPE(event->attr.config);
10191888d3ddSRobin Murphy 	u32 val, port;
10201888d3ddSRobin Murphy 
10211888d3ddSRobin Murphy 	port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
10221888d3ddSRobin Murphy 	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
10231888d3ddSRobin Murphy 			hw->config_base);
10241888d3ddSRobin Murphy 
10251888d3ddSRobin Murphy 	/* These *_event_sel regs should be identical, but let's make sure... */
10261888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
10271888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
10281888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
10291888d3ddSRobin Murphy 			CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
10301888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
10311888d3ddSRobin Murphy 			CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
10321888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
10331888d3ddSRobin Murphy 			CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
10341888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
10351888d3ddSRobin Murphy 			CCN_RNI_PMU_EVENT_SEL__ID__MASK);
10361888d3ddSRobin Murphy 	if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
10371888d3ddSRobin Murphy 			!arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
10381888d3ddSRobin Murphy 		return;
10391888d3ddSRobin Murphy 
10401888d3ddSRobin Murphy 	/* Set the event id for the pre-allocated counter */
10411888d3ddSRobin Murphy 	val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
10421888d3ddSRobin Murphy 	val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
10431888d3ddSRobin Murphy 		CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
10441888d3ddSRobin Murphy 	val |= CCN_CONFIG_EVENT(event->attr.config) <<
10451888d3ddSRobin Murphy 		CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
10461888d3ddSRobin Murphy 	writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
10471888d3ddSRobin Murphy }
10481888d3ddSRobin Murphy 
10491888d3ddSRobin Murphy static void arm_ccn_pmu_event_config(struct perf_event *event)
10501888d3ddSRobin Murphy {
10511888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
10521888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
10531888d3ddSRobin Murphy 	u32 xp, offset, val;
10541888d3ddSRobin Murphy 
10551888d3ddSRobin Murphy 	/* Cycle counter requires no setup */
10561888d3ddSRobin Murphy 	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
10571888d3ddSRobin Murphy 		return;
10581888d3ddSRobin Murphy 
10591888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
10601888d3ddSRobin Murphy 		xp = CCN_CONFIG_XP(event->attr.config);
10611888d3ddSRobin Murphy 	else
10621888d3ddSRobin Murphy 		xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
10631888d3ddSRobin Murphy 
10641888d3ddSRobin Murphy 	spin_lock(&ccn->dt.config_lock);
10651888d3ddSRobin Murphy 
10661888d3ddSRobin Murphy 	/* Set the DT bus "distance" register */
10671888d3ddSRobin Murphy 	offset = (hw->idx / 4) * 4;
10681888d3ddSRobin Murphy 	val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
10691888d3ddSRobin Murphy 	val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
10701888d3ddSRobin Murphy 			CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
10711888d3ddSRobin Murphy 	val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
10721888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
10731888d3ddSRobin Murphy 
10741888d3ddSRobin Murphy 	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
10751888d3ddSRobin Murphy 		if (CCN_CONFIG_EVENT(event->attr.config) ==
10761888d3ddSRobin Murphy 				CCN_EVENT_WATCHPOINT)
10771888d3ddSRobin Murphy 			arm_ccn_pmu_xp_watchpoint_config(event);
10781888d3ddSRobin Murphy 		else
10791888d3ddSRobin Murphy 			arm_ccn_pmu_xp_event_config(event);
10801888d3ddSRobin Murphy 	} else {
10811888d3ddSRobin Murphy 		arm_ccn_pmu_node_event_config(event);
10821888d3ddSRobin Murphy 	}
10831888d3ddSRobin Murphy 
10841888d3ddSRobin Murphy 	spin_unlock(&ccn->dt.config_lock);
10851888d3ddSRobin Murphy }
10861888d3ddSRobin Murphy 
10871888d3ddSRobin Murphy static int arm_ccn_pmu_active_counters(struct arm_ccn *ccn)
10881888d3ddSRobin Murphy {
10891888d3ddSRobin Murphy 	return bitmap_weight(ccn->dt.pmu_counters_mask,
10901888d3ddSRobin Murphy 			     CCN_NUM_PMU_EVENT_COUNTERS + 1);
10911888d3ddSRobin Murphy }
10921888d3ddSRobin Murphy 
10931888d3ddSRobin Murphy static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
10941888d3ddSRobin Murphy {
10951888d3ddSRobin Murphy 	int err;
10961888d3ddSRobin Murphy 	struct hw_perf_event *hw = &event->hw;
10971888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
10981888d3ddSRobin Murphy 
10991888d3ddSRobin Murphy 	err = arm_ccn_pmu_event_alloc(event);
11001888d3ddSRobin Murphy 	if (err)
11011888d3ddSRobin Murphy 		return err;
11021888d3ddSRobin Murphy 
11031888d3ddSRobin Murphy 	/*
11041888d3ddSRobin Murphy 	 * Pin the timer, so that the overflows are handled by the chosen
11051888d3ddSRobin Murphy 	 * event->cpu (this is the same one as presented in "cpumask"
11061888d3ddSRobin Murphy 	 * attribute).
11071888d3ddSRobin Murphy 	 */
11081888d3ddSRobin Murphy 	if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 1)
11091888d3ddSRobin Murphy 		hrtimer_start(&ccn->dt.hrtimer, arm_ccn_pmu_timer_period(),
11101888d3ddSRobin Murphy 			      HRTIMER_MODE_REL_PINNED);
11111888d3ddSRobin Murphy 
11121888d3ddSRobin Murphy 	arm_ccn_pmu_event_config(event);
11131888d3ddSRobin Murphy 
11141888d3ddSRobin Murphy 	hw->state = PERF_HES_STOPPED;
11151888d3ddSRobin Murphy 
11161888d3ddSRobin Murphy 	if (flags & PERF_EF_START)
11171888d3ddSRobin Murphy 		arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
11181888d3ddSRobin Murphy 
11191888d3ddSRobin Murphy 	return 0;
11201888d3ddSRobin Murphy }
11211888d3ddSRobin Murphy 
11221888d3ddSRobin Murphy static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
11231888d3ddSRobin Murphy {
11241888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
11251888d3ddSRobin Murphy 
11261888d3ddSRobin Murphy 	arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
11271888d3ddSRobin Murphy 
11281888d3ddSRobin Murphy 	arm_ccn_pmu_event_release(event);
11291888d3ddSRobin Murphy 
11301888d3ddSRobin Murphy 	if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 0)
11311888d3ddSRobin Murphy 		hrtimer_cancel(&ccn->dt.hrtimer);
11321888d3ddSRobin Murphy }
11331888d3ddSRobin Murphy 
11341888d3ddSRobin Murphy static void arm_ccn_pmu_event_read(struct perf_event *event)
11351888d3ddSRobin Murphy {
11361888d3ddSRobin Murphy 	arm_ccn_pmu_event_update(event);
11371888d3ddSRobin Murphy }
11381888d3ddSRobin Murphy 
11391888d3ddSRobin Murphy static void arm_ccn_pmu_enable(struct pmu *pmu)
11401888d3ddSRobin Murphy {
11411888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
11421888d3ddSRobin Murphy 
11431888d3ddSRobin Murphy 	u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
11441888d3ddSRobin Murphy 	val |= CCN_DT_PMCR__PMU_EN;
11451888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_PMCR);
11461888d3ddSRobin Murphy }
11471888d3ddSRobin Murphy 
11481888d3ddSRobin Murphy static void arm_ccn_pmu_disable(struct pmu *pmu)
11491888d3ddSRobin Murphy {
11501888d3ddSRobin Murphy 	struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
11511888d3ddSRobin Murphy 
11521888d3ddSRobin Murphy 	u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
11531888d3ddSRobin Murphy 	val &= ~CCN_DT_PMCR__PMU_EN;
11541888d3ddSRobin Murphy 	writel(val, ccn->dt.base + CCN_DT_PMCR);
11551888d3ddSRobin Murphy }
11561888d3ddSRobin Murphy 
11571888d3ddSRobin Murphy static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
11581888d3ddSRobin Murphy {
11591888d3ddSRobin Murphy 	u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
11601888d3ddSRobin Murphy 	int idx;
11611888d3ddSRobin Murphy 
11621888d3ddSRobin Murphy 	if (!pmovsr)
11631888d3ddSRobin Murphy 		return IRQ_NONE;
11641888d3ddSRobin Murphy 
11651888d3ddSRobin Murphy 	writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
11661888d3ddSRobin Murphy 
11671888d3ddSRobin Murphy 	BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
11681888d3ddSRobin Murphy 
11691888d3ddSRobin Murphy 	for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
11701888d3ddSRobin Murphy 		struct perf_event *event = dt->pmu_counters[idx].event;
11711888d3ddSRobin Murphy 		int overflowed = pmovsr & BIT(idx);
11721888d3ddSRobin Murphy 
11731888d3ddSRobin Murphy 		WARN_ON_ONCE(overflowed && !event &&
11741888d3ddSRobin Murphy 				idx != CCN_IDX_PMU_CYCLE_COUNTER);
11751888d3ddSRobin Murphy 
11761888d3ddSRobin Murphy 		if (!event || !overflowed)
11771888d3ddSRobin Murphy 			continue;
11781888d3ddSRobin Murphy 
11791888d3ddSRobin Murphy 		arm_ccn_pmu_event_update(event);
11801888d3ddSRobin Murphy 	}
11811888d3ddSRobin Murphy 
11821888d3ddSRobin Murphy 	return IRQ_HANDLED;
11831888d3ddSRobin Murphy }
11841888d3ddSRobin Murphy 
11851888d3ddSRobin Murphy static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
11861888d3ddSRobin Murphy {
11871888d3ddSRobin Murphy 	struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
11881888d3ddSRobin Murphy 			hrtimer);
11891888d3ddSRobin Murphy 	unsigned long flags;
11901888d3ddSRobin Murphy 
11911888d3ddSRobin Murphy 	local_irq_save(flags);
11921888d3ddSRobin Murphy 	arm_ccn_pmu_overflow_handler(dt);
11931888d3ddSRobin Murphy 	local_irq_restore(flags);
11941888d3ddSRobin Murphy 
11951888d3ddSRobin Murphy 	hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
11961888d3ddSRobin Murphy 	return HRTIMER_RESTART;
11971888d3ddSRobin Murphy }
11981888d3ddSRobin Murphy 
11991888d3ddSRobin Murphy 
12001888d3ddSRobin Murphy static int arm_ccn_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
12011888d3ddSRobin Murphy {
12021888d3ddSRobin Murphy 	struct arm_ccn_dt *dt = hlist_entry_safe(node, struct arm_ccn_dt, node);
12031888d3ddSRobin Murphy 	struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
12041888d3ddSRobin Murphy 	unsigned int target;
12051888d3ddSRobin Murphy 
12069bcb929fSRobin Murphy 	if (cpu != dt->cpu)
12071888d3ddSRobin Murphy 		return 0;
12081888d3ddSRobin Murphy 	target = cpumask_any_but(cpu_online_mask, cpu);
12091888d3ddSRobin Murphy 	if (target >= nr_cpu_ids)
12101888d3ddSRobin Murphy 		return 0;
12111888d3ddSRobin Murphy 	perf_pmu_migrate_context(&dt->pmu, cpu, target);
12129bcb929fSRobin Murphy 	dt->cpu = target;
12131888d3ddSRobin Murphy 	if (ccn->irq)
1214*84fca8baSThomas Gleixner 		WARN_ON(irq_set_affinity(ccn->irq, cpumask_of(dt->cpu)));
12151888d3ddSRobin Murphy 	return 0;
12161888d3ddSRobin Murphy }
12171888d3ddSRobin Murphy 
12181888d3ddSRobin Murphy static DEFINE_IDA(arm_ccn_pmu_ida);
12191888d3ddSRobin Murphy 
12201888d3ddSRobin Murphy static int arm_ccn_pmu_init(struct arm_ccn *ccn)
12211888d3ddSRobin Murphy {
12221888d3ddSRobin Murphy 	int i;
12231888d3ddSRobin Murphy 	char *name;
12241888d3ddSRobin Murphy 	int err;
12251888d3ddSRobin Murphy 
12261888d3ddSRobin Murphy 	/* Initialize DT subsystem */
12271888d3ddSRobin Murphy 	ccn->dt.base = ccn->base + CCN_REGION_SIZE;
12281888d3ddSRobin Murphy 	spin_lock_init(&ccn->dt.config_lock);
12291888d3ddSRobin Murphy 	writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
12301888d3ddSRobin Murphy 	writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
12311888d3ddSRobin Murphy 	writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
12321888d3ddSRobin Murphy 			ccn->dt.base + CCN_DT_PMCR);
12331888d3ddSRobin Murphy 	writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
12341888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++) {
12351888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
12361888d3ddSRobin Murphy 		writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
12371888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
12381888d3ddSRobin Murphy 				(CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
12391888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
12401888d3ddSRobin Murphy 				CCN_XP_DT_CONTROL__DT_ENABLE,
12411888d3ddSRobin Murphy 				ccn->xp[i].base + CCN_XP_DT_CONTROL);
12421888d3ddSRobin Murphy 	}
12431888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
12441888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
12451888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
12461888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
12471888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
12481888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
12491888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
12501888d3ddSRobin Murphy 	ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
12511888d3ddSRobin Murphy 
12521888d3ddSRobin Murphy 	/* Get a convenient /sys/event_source/devices/ name */
12531888d3ddSRobin Murphy 	ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
12541888d3ddSRobin Murphy 	if (ccn->dt.id == 0) {
12551888d3ddSRobin Murphy 		name = "ccn";
12561888d3ddSRobin Murphy 	} else {
12571888d3ddSRobin Murphy 		name = devm_kasprintf(ccn->dev, GFP_KERNEL, "ccn_%d",
12581888d3ddSRobin Murphy 				      ccn->dt.id);
12591888d3ddSRobin Murphy 		if (!name) {
12601888d3ddSRobin Murphy 			err = -ENOMEM;
12611888d3ddSRobin Murphy 			goto error_choose_name;
12621888d3ddSRobin Murphy 		}
12631888d3ddSRobin Murphy 	}
12641888d3ddSRobin Murphy 
12651888d3ddSRobin Murphy 	/* Perf driver registration */
12661888d3ddSRobin Murphy 	ccn->dt.pmu = (struct pmu) {
12671888d3ddSRobin Murphy 		.module = THIS_MODULE,
12681888d3ddSRobin Murphy 		.attr_groups = arm_ccn_pmu_attr_groups,
12691888d3ddSRobin Murphy 		.task_ctx_nr = perf_invalid_context,
12701888d3ddSRobin Murphy 		.event_init = arm_ccn_pmu_event_init,
12711888d3ddSRobin Murphy 		.add = arm_ccn_pmu_event_add,
12721888d3ddSRobin Murphy 		.del = arm_ccn_pmu_event_del,
12731888d3ddSRobin Murphy 		.start = arm_ccn_pmu_event_start,
12741888d3ddSRobin Murphy 		.stop = arm_ccn_pmu_event_stop,
12751888d3ddSRobin Murphy 		.read = arm_ccn_pmu_event_read,
12761888d3ddSRobin Murphy 		.pmu_enable = arm_ccn_pmu_enable,
12771888d3ddSRobin Murphy 		.pmu_disable = arm_ccn_pmu_disable,
127830656398SAndrew Murray 		.capabilities = PERF_PMU_CAP_NO_EXCLUDE,
12791888d3ddSRobin Murphy 	};
12801888d3ddSRobin Murphy 
12811888d3ddSRobin Murphy 	/* No overflow interrupt? Have to use a timer instead. */
12821888d3ddSRobin Murphy 	if (!ccn->irq) {
12831888d3ddSRobin Murphy 		dev_info(ccn->dev, "No access to interrupts, using timer.\n");
12841888d3ddSRobin Murphy 		hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
12851888d3ddSRobin Murphy 				HRTIMER_MODE_REL);
12861888d3ddSRobin Murphy 		ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
12871888d3ddSRobin Murphy 	}
12881888d3ddSRobin Murphy 
12891888d3ddSRobin Murphy 	/* Pick one CPU which we will use to collect data from CCN... */
12909bcb929fSRobin Murphy 	ccn->dt.cpu = raw_smp_processor_id();
12911888d3ddSRobin Murphy 
12921888d3ddSRobin Murphy 	/* Also make sure that the overflow interrupt is handled by this CPU */
12931888d3ddSRobin Murphy 	if (ccn->irq) {
1294*84fca8baSThomas Gleixner 		err = irq_set_affinity(ccn->irq, cpumask_of(ccn->dt.cpu));
12951888d3ddSRobin Murphy 		if (err) {
12961888d3ddSRobin Murphy 			dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
12971888d3ddSRobin Murphy 			goto error_set_affinity;
12981888d3ddSRobin Murphy 		}
12991888d3ddSRobin Murphy 	}
13001888d3ddSRobin Murphy 
13019bcb929fSRobin Murphy 	cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13029bcb929fSRobin Murphy 					 &ccn->dt.node);
13039bcb929fSRobin Murphy 
13041888d3ddSRobin Murphy 	err = perf_pmu_register(&ccn->dt.pmu, name, -1);
13051888d3ddSRobin Murphy 	if (err)
13061888d3ddSRobin Murphy 		goto error_pmu_register;
13071888d3ddSRobin Murphy 
13081888d3ddSRobin Murphy 	return 0;
13091888d3ddSRobin Murphy 
13101888d3ddSRobin Murphy error_pmu_register:
13119bcb929fSRobin Murphy 	cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13129bcb929fSRobin Murphy 					    &ccn->dt.node);
13131888d3ddSRobin Murphy error_set_affinity:
13141888d3ddSRobin Murphy error_choose_name:
13151888d3ddSRobin Murphy 	ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
13161888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++)
13171888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
13181888d3ddSRobin Murphy 	writel(0, ccn->dt.base + CCN_DT_PMCR);
13191888d3ddSRobin Murphy 	return err;
13201888d3ddSRobin Murphy }
13211888d3ddSRobin Murphy 
13221888d3ddSRobin Murphy static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
13231888d3ddSRobin Murphy {
13241888d3ddSRobin Murphy 	int i;
13251888d3ddSRobin Murphy 
13261888d3ddSRobin Murphy 	cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13271888d3ddSRobin Murphy 					    &ccn->dt.node);
13281888d3ddSRobin Murphy 	for (i = 0; i < ccn->num_xps; i++)
13291888d3ddSRobin Murphy 		writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
13301888d3ddSRobin Murphy 	writel(0, ccn->dt.base + CCN_DT_PMCR);
13311888d3ddSRobin Murphy 	perf_pmu_unregister(&ccn->dt.pmu);
13321888d3ddSRobin Murphy 	ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
13331888d3ddSRobin Murphy }
13341888d3ddSRobin Murphy 
13351888d3ddSRobin Murphy static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
13361888d3ddSRobin Murphy 		int (*callback)(struct arm_ccn *ccn, int region,
13371888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id))
13381888d3ddSRobin Murphy {
13391888d3ddSRobin Murphy 	int region;
13401888d3ddSRobin Murphy 
13411888d3ddSRobin Murphy 	for (region = 0; region < CCN_NUM_REGIONS; region++) {
13421888d3ddSRobin Murphy 		u32 val, type, id;
13431888d3ddSRobin Murphy 		void __iomem *base;
13441888d3ddSRobin Murphy 		int err;
13451888d3ddSRobin Murphy 
13461888d3ddSRobin Murphy 		val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
13471888d3ddSRobin Murphy 				4 * (region / 32));
13481888d3ddSRobin Murphy 		if (!(val & (1 << (region % 32))))
13491888d3ddSRobin Murphy 			continue;
13501888d3ddSRobin Murphy 
13511888d3ddSRobin Murphy 		base = ccn->base + region * CCN_REGION_SIZE;
13521888d3ddSRobin Murphy 		val = readl(base + CCN_ALL_OLY_ID);
13531888d3ddSRobin Murphy 		type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
13541888d3ddSRobin Murphy 				CCN_ALL_OLY_ID__OLY_ID__MASK;
13551888d3ddSRobin Murphy 		id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
13561888d3ddSRobin Murphy 				CCN_ALL_OLY_ID__NODE_ID__MASK;
13571888d3ddSRobin Murphy 
13581888d3ddSRobin Murphy 		err = callback(ccn, region, base, type, id);
13591888d3ddSRobin Murphy 		if (err)
13601888d3ddSRobin Murphy 			return err;
13611888d3ddSRobin Murphy 	}
13621888d3ddSRobin Murphy 
13631888d3ddSRobin Murphy 	return 0;
13641888d3ddSRobin Murphy }
13651888d3ddSRobin Murphy 
13661888d3ddSRobin Murphy static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
13671888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id)
13681888d3ddSRobin Murphy {
13691888d3ddSRobin Murphy 
13701888d3ddSRobin Murphy 	if (type == CCN_TYPE_XP && id >= ccn->num_xps)
13711888d3ddSRobin Murphy 		ccn->num_xps = id + 1;
13721888d3ddSRobin Murphy 	else if (id >= ccn->num_nodes)
13731888d3ddSRobin Murphy 		ccn->num_nodes = id + 1;
13741888d3ddSRobin Murphy 
13751888d3ddSRobin Murphy 	return 0;
13761888d3ddSRobin Murphy }
13771888d3ddSRobin Murphy 
13781888d3ddSRobin Murphy static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
13791888d3ddSRobin Murphy 		void __iomem *base, u32 type, u32 id)
13801888d3ddSRobin Murphy {
13811888d3ddSRobin Murphy 	struct arm_ccn_component *component;
13821888d3ddSRobin Murphy 
13831888d3ddSRobin Murphy 	dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
13841888d3ddSRobin Murphy 
13851888d3ddSRobin Murphy 	switch (type) {
13861888d3ddSRobin Murphy 	case CCN_TYPE_MN:
13871888d3ddSRobin Murphy 		ccn->mn_id = id;
13881888d3ddSRobin Murphy 		return 0;
13891888d3ddSRobin Murphy 	case CCN_TYPE_DT:
13901888d3ddSRobin Murphy 		return 0;
13911888d3ddSRobin Murphy 	case CCN_TYPE_XP:
13921888d3ddSRobin Murphy 		component = &ccn->xp[id];
13931888d3ddSRobin Murphy 		break;
13941888d3ddSRobin Murphy 	case CCN_TYPE_SBSX:
13951888d3ddSRobin Murphy 		ccn->sbsx_present = 1;
13961888d3ddSRobin Murphy 		component = &ccn->node[id];
13971888d3ddSRobin Murphy 		break;
13981888d3ddSRobin Murphy 	case CCN_TYPE_SBAS:
13991888d3ddSRobin Murphy 		ccn->sbas_present = 1;
1400df561f66SGustavo A. R. Silva 		fallthrough;
14011888d3ddSRobin Murphy 	default:
14021888d3ddSRobin Murphy 		component = &ccn->node[id];
14031888d3ddSRobin Murphy 		break;
14041888d3ddSRobin Murphy 	}
14051888d3ddSRobin Murphy 
14061888d3ddSRobin Murphy 	component->base = base;
14071888d3ddSRobin Murphy 	component->type = type;
14081888d3ddSRobin Murphy 
14091888d3ddSRobin Murphy 	return 0;
14101888d3ddSRobin Murphy }
14111888d3ddSRobin Murphy 
14121888d3ddSRobin Murphy 
14131888d3ddSRobin Murphy static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
14141888d3ddSRobin Murphy 		const u32 *err_sig_val)
14151888d3ddSRobin Murphy {
14161888d3ddSRobin Murphy 	/* This should be really handled by firmware... */
14171888d3ddSRobin Murphy 	dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
14181888d3ddSRobin Murphy 			err_sig_val[5], err_sig_val[4], err_sig_val[3],
14191888d3ddSRobin Murphy 			err_sig_val[2], err_sig_val[1], err_sig_val[0]);
14201888d3ddSRobin Murphy 	dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
14211888d3ddSRobin Murphy 	writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
14221888d3ddSRobin Murphy 			ccn->base + CCN_MN_ERRINT_STATUS);
14231888d3ddSRobin Murphy 
14241888d3ddSRobin Murphy 	return IRQ_HANDLED;
14251888d3ddSRobin Murphy }
14261888d3ddSRobin Murphy 
14271888d3ddSRobin Murphy 
14281888d3ddSRobin Murphy static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
14291888d3ddSRobin Murphy {
14301888d3ddSRobin Murphy 	irqreturn_t res = IRQ_NONE;
14311888d3ddSRobin Murphy 	struct arm_ccn *ccn = dev_id;
14321888d3ddSRobin Murphy 	u32 err_sig_val[6];
14331888d3ddSRobin Murphy 	u32 err_or;
14341888d3ddSRobin Murphy 	int i;
14351888d3ddSRobin Murphy 
14361888d3ddSRobin Murphy 	/* PMU overflow is a special case */
14371888d3ddSRobin Murphy 	err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
14381888d3ddSRobin Murphy 	if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
14391888d3ddSRobin Murphy 		err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
14401888d3ddSRobin Murphy 		res = arm_ccn_pmu_overflow_handler(&ccn->dt);
14411888d3ddSRobin Murphy 	}
14421888d3ddSRobin Murphy 
14431888d3ddSRobin Murphy 	/* Have to read all err_sig_vals to clear them */
14441888d3ddSRobin Murphy 	for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
14451888d3ddSRobin Murphy 		err_sig_val[i] = readl(ccn->base +
14461888d3ddSRobin Murphy 				CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
14471888d3ddSRobin Murphy 		err_or |= err_sig_val[i];
14481888d3ddSRobin Murphy 	}
14491888d3ddSRobin Murphy 	if (err_or)
14501888d3ddSRobin Murphy 		res |= arm_ccn_error_handler(ccn, err_sig_val);
14511888d3ddSRobin Murphy 
14521888d3ddSRobin Murphy 	if (res != IRQ_NONE)
14531888d3ddSRobin Murphy 		writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
14541888d3ddSRobin Murphy 				ccn->base + CCN_MN_ERRINT_STATUS);
14551888d3ddSRobin Murphy 
14561888d3ddSRobin Murphy 	return res;
14571888d3ddSRobin Murphy }
14581888d3ddSRobin Murphy 
14591888d3ddSRobin Murphy 
14601888d3ddSRobin Murphy static int arm_ccn_probe(struct platform_device *pdev)
14611888d3ddSRobin Murphy {
14621888d3ddSRobin Murphy 	struct arm_ccn *ccn;
14631888d3ddSRobin Murphy 	struct resource *res;
14641888d3ddSRobin Murphy 	unsigned int irq;
14651888d3ddSRobin Murphy 	int err;
14661888d3ddSRobin Murphy 
14671888d3ddSRobin Murphy 	ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
14681888d3ddSRobin Murphy 	if (!ccn)
14691888d3ddSRobin Murphy 		return -ENOMEM;
14701888d3ddSRobin Murphy 	ccn->dev = &pdev->dev;
14711888d3ddSRobin Murphy 	platform_set_drvdata(pdev, ccn);
14721888d3ddSRobin Murphy 
14731c8d96b4SYueHaibing 	ccn->base = devm_platform_ioremap_resource(pdev, 0);
1474809092dcSSudeep Holla 	if (IS_ERR(ccn->base))
1475809092dcSSudeep Holla 		return PTR_ERR(ccn->base);
14761888d3ddSRobin Murphy 
14771888d3ddSRobin Murphy 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
14781888d3ddSRobin Murphy 	if (!res)
14791888d3ddSRobin Murphy 		return -EINVAL;
14801888d3ddSRobin Murphy 	irq = res->start;
14811888d3ddSRobin Murphy 
14821888d3ddSRobin Murphy 	/* Check if we can use the interrupt */
14831888d3ddSRobin Murphy 	writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
14841888d3ddSRobin Murphy 			ccn->base + CCN_MN_ERRINT_STATUS);
14851888d3ddSRobin Murphy 	if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
14861888d3ddSRobin Murphy 			CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
14871888d3ddSRobin Murphy 		/* Can set 'disable' bits, so can acknowledge interrupts */
14881888d3ddSRobin Murphy 		writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
14891888d3ddSRobin Murphy 				ccn->base + CCN_MN_ERRINT_STATUS);
14901888d3ddSRobin Murphy 		err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler,
14911888d3ddSRobin Murphy 				       IRQF_NOBALANCING | IRQF_NO_THREAD,
14921888d3ddSRobin Murphy 				       dev_name(ccn->dev), ccn);
14931888d3ddSRobin Murphy 		if (err)
14941888d3ddSRobin Murphy 			return err;
14951888d3ddSRobin Murphy 
14961888d3ddSRobin Murphy 		ccn->irq = irq;
14971888d3ddSRobin Murphy 	}
14981888d3ddSRobin Murphy 
14991888d3ddSRobin Murphy 
15001888d3ddSRobin Murphy 	/* Build topology */
15011888d3ddSRobin Murphy 
15021888d3ddSRobin Murphy 	err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
15031888d3ddSRobin Murphy 	if (err)
15041888d3ddSRobin Murphy 		return err;
15051888d3ddSRobin Murphy 
15061888d3ddSRobin Murphy 	ccn->node = devm_kcalloc(ccn->dev, ccn->num_nodes, sizeof(*ccn->node),
15071888d3ddSRobin Murphy 				 GFP_KERNEL);
15081888d3ddSRobin Murphy 	ccn->xp = devm_kcalloc(ccn->dev, ccn->num_xps, sizeof(*ccn->node),
15091888d3ddSRobin Murphy 			       GFP_KERNEL);
15101888d3ddSRobin Murphy 	if (!ccn->node || !ccn->xp)
15111888d3ddSRobin Murphy 		return -ENOMEM;
15121888d3ddSRobin Murphy 
15131888d3ddSRobin Murphy 	err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
15141888d3ddSRobin Murphy 	if (err)
15151888d3ddSRobin Murphy 		return err;
15161888d3ddSRobin Murphy 
15171888d3ddSRobin Murphy 	return arm_ccn_pmu_init(ccn);
15181888d3ddSRobin Murphy }
15191888d3ddSRobin Murphy 
15201888d3ddSRobin Murphy static int arm_ccn_remove(struct platform_device *pdev)
15211888d3ddSRobin Murphy {
15221888d3ddSRobin Murphy 	struct arm_ccn *ccn = platform_get_drvdata(pdev);
15231888d3ddSRobin Murphy 
15241888d3ddSRobin Murphy 	arm_ccn_pmu_cleanup(ccn);
15251888d3ddSRobin Murphy 
15261888d3ddSRobin Murphy 	return 0;
15271888d3ddSRobin Murphy }
15281888d3ddSRobin Murphy 
15291888d3ddSRobin Murphy static const struct of_device_id arm_ccn_match[] = {
15301888d3ddSRobin Murphy 	{ .compatible = "arm,ccn-502", },
15311888d3ddSRobin Murphy 	{ .compatible = "arm,ccn-504", },
1532126b0a17SMarek Bykowski 	{ .compatible = "arm,ccn-512", },
15331888d3ddSRobin Murphy 	{},
15341888d3ddSRobin Murphy };
15351888d3ddSRobin Murphy MODULE_DEVICE_TABLE(of, arm_ccn_match);
15361888d3ddSRobin Murphy 
15371888d3ddSRobin Murphy static struct platform_driver arm_ccn_driver = {
15381888d3ddSRobin Murphy 	.driver = {
15391888d3ddSRobin Murphy 		.name = "arm-ccn",
15401888d3ddSRobin Murphy 		.of_match_table = arm_ccn_match,
1541f32ed8ebSQi Liu 		.suppress_bind_attrs = true,
15421888d3ddSRobin Murphy 	},
15431888d3ddSRobin Murphy 	.probe = arm_ccn_probe,
15441888d3ddSRobin Murphy 	.remove = arm_ccn_remove,
15451888d3ddSRobin Murphy };
15461888d3ddSRobin Murphy 
15471888d3ddSRobin Murphy static int __init arm_ccn_init(void)
15481888d3ddSRobin Murphy {
15491888d3ddSRobin Murphy 	int i, ret;
15501888d3ddSRobin Murphy 
15511888d3ddSRobin Murphy 	ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE,
15521888d3ddSRobin Murphy 				      "perf/arm/ccn:online", NULL,
15531888d3ddSRobin Murphy 				      arm_ccn_pmu_offline_cpu);
15541888d3ddSRobin Murphy 	if (ret)
15551888d3ddSRobin Murphy 		return ret;
15561888d3ddSRobin Murphy 
15571888d3ddSRobin Murphy 	for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
15581888d3ddSRobin Murphy 		arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
15591888d3ddSRobin Murphy 
15601888d3ddSRobin Murphy 	ret = platform_driver_register(&arm_ccn_driver);
15611888d3ddSRobin Murphy 	if (ret)
15621888d3ddSRobin Murphy 		cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
15631888d3ddSRobin Murphy 	return ret;
15641888d3ddSRobin Murphy }
15651888d3ddSRobin Murphy 
15661888d3ddSRobin Murphy static void __exit arm_ccn_exit(void)
15671888d3ddSRobin Murphy {
15681888d3ddSRobin Murphy 	platform_driver_unregister(&arm_ccn_driver);
15691888d3ddSRobin Murphy 	cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
15701888d3ddSRobin Murphy }
15711888d3ddSRobin Murphy 
15721888d3ddSRobin Murphy module_init(arm_ccn_init);
15731888d3ddSRobin Murphy module_exit(arm_ccn_exit);
15741888d3ddSRobin Murphy 
15751888d3ddSRobin Murphy MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
157675dc3441SRobin Murphy MODULE_LICENSE("GPL v2");
1577