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