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