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