18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2014 ARM Limited
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/ctype.h>
88c2ecf20Sopenharmony_ci#include <linux/hrtimer.h>
98c2ecf20Sopenharmony_ci#include <linux/idr.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
148c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define CCN_NUM_XP_PORTS 2
198c2ecf20Sopenharmony_ci#define CCN_NUM_VCS 4
208c2ecf20Sopenharmony_ci#define CCN_NUM_REGIONS	256
218c2ecf20Sopenharmony_ci#define CCN_REGION_SIZE	0x10000
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define CCN_ALL_OLY_ID			0xff00
248c2ecf20Sopenharmony_ci#define CCN_ALL_OLY_ID__OLY_ID__SHIFT			0
258c2ecf20Sopenharmony_ci#define CCN_ALL_OLY_ID__OLY_ID__MASK			0x1f
268c2ecf20Sopenharmony_ci#define CCN_ALL_OLY_ID__NODE_ID__SHIFT			8
278c2ecf20Sopenharmony_ci#define CCN_ALL_OLY_ID__NODE_ID__MASK			0x3f
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS		0x0008
308c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__INTREQ__DESSERT		0x11
318c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__ALL_ERRORS__ENABLE	0x02
328c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLED	0x20
338c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE	0x22
348c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_ENABLE	0x04
358c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLED	0x40
368c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLE	0x44
378c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE	0x08
388c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED	0x80
398c2ecf20Sopenharmony_ci#define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE	0x88
408c2ecf20Sopenharmony_ci#define CCN_MN_OLY_COMP_LIST_63_0	0x01e0
418c2ecf20Sopenharmony_ci#define CCN_MN_ERR_SIG_VAL_63_0		0x0300
428c2ecf20Sopenharmony_ci#define CCN_MN_ERR_SIG_VAL_63_0__DT			(1 << 1)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define CCN_DT_ACTIVE_DSM		0x0000
458c2ecf20Sopenharmony_ci#define CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(n)		((n) * 8)
468c2ecf20Sopenharmony_ci#define CCN_DT_ACTIVE_DSM__DSM_ID__MASK			0xff
478c2ecf20Sopenharmony_ci#define CCN_DT_CTL			0x0028
488c2ecf20Sopenharmony_ci#define CCN_DT_CTL__DT_EN				(1 << 0)
498c2ecf20Sopenharmony_ci#define CCN_DT_PMEVCNT(n)		(0x0100 + (n) * 0x8)
508c2ecf20Sopenharmony_ci#define CCN_DT_PMCCNTR			0x0140
518c2ecf20Sopenharmony_ci#define CCN_DT_PMCCNTRSR		0x0190
528c2ecf20Sopenharmony_ci#define CCN_DT_PMOVSR			0x0198
538c2ecf20Sopenharmony_ci#define CCN_DT_PMOVSR_CLR		0x01a0
548c2ecf20Sopenharmony_ci#define CCN_DT_PMOVSR_CLR__MASK				0x1f
558c2ecf20Sopenharmony_ci#define CCN_DT_PMCR			0x01a8
568c2ecf20Sopenharmony_ci#define CCN_DT_PMCR__OVFL_INTR_EN			(1 << 6)
578c2ecf20Sopenharmony_ci#define CCN_DT_PMCR__PMU_EN				(1 << 0)
588c2ecf20Sopenharmony_ci#define CCN_DT_PMSR			0x01b0
598c2ecf20Sopenharmony_ci#define CCN_DT_PMSR_REQ			0x01b8
608c2ecf20Sopenharmony_ci#define CCN_DT_PMSR_CLR			0x01c0
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define CCN_HNF_PMU_EVENT_SEL		0x0600
638c2ecf20Sopenharmony_ci#define CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
648c2ecf20Sopenharmony_ci#define CCN_HNF_PMU_EVENT_SEL__ID__MASK			0xf
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONFIG		0x0300
678c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONFIG__DT_CFG__SHIFT(n)		((n) * 4)
688c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONFIG__DT_CFG__MASK			0xf
698c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH		0x0
708c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT_0_OR_1	0x1
718c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(n)		(0x2 + (n))
728c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(n)	(0x4 + (n))
738c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(d, n) (0x8 + (d) * 4 + (n))
748c2ecf20Sopenharmony_ci#define CCN_XP_DT_INTERFACE_SEL		0x0308
758c2ecf20Sopenharmony_ci#define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(n)	(0 + (n) * 8)
768c2ecf20Sopenharmony_ci#define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK	0x1
778c2ecf20Sopenharmony_ci#define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(n)	(1 + (n) * 8)
788c2ecf20Sopenharmony_ci#define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK	0x1
798c2ecf20Sopenharmony_ci#define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(n)	(2 + (n) * 8)
808c2ecf20Sopenharmony_ci#define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK	0x3
818c2ecf20Sopenharmony_ci#define CCN_XP_DT_CMP_VAL_L(n)		(0x0310 + (n) * 0x40)
828c2ecf20Sopenharmony_ci#define CCN_XP_DT_CMP_VAL_H(n)		(0x0318 + (n) * 0x40)
838c2ecf20Sopenharmony_ci#define CCN_XP_DT_CMP_MASK_L(n)		(0x0320 + (n) * 0x40)
848c2ecf20Sopenharmony_ci#define CCN_XP_DT_CMP_MASK_H(n)		(0x0328 + (n) * 0x40)
858c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONTROL		0x0370
868c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONTROL__DT_ENABLE			(1 << 0)
878c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(n)		(12 + (n) * 4)
888c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONTROL__WP_ARM_SEL__MASK		0xf
898c2ecf20Sopenharmony_ci#define CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS		0xf
908c2ecf20Sopenharmony_ci#define CCN_XP_PMU_EVENT_SEL		0x0600
918c2ecf20Sopenharmony_ci#define CCN_XP_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 7)
928c2ecf20Sopenharmony_ci#define CCN_XP_PMU_EVENT_SEL__ID__MASK			0x3f
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define CCN_SBAS_PMU_EVENT_SEL		0x0600
958c2ecf20Sopenharmony_ci#define CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
968c2ecf20Sopenharmony_ci#define CCN_SBAS_PMU_EVENT_SEL__ID__MASK		0xf
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define CCN_RNI_PMU_EVENT_SEL		0x0600
998c2ecf20Sopenharmony_ci#define CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(n)		((n) * 4)
1008c2ecf20Sopenharmony_ci#define CCN_RNI_PMU_EVENT_SEL__ID__MASK			0xf
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define CCN_TYPE_MN	0x01
1038c2ecf20Sopenharmony_ci#define CCN_TYPE_DT	0x02
1048c2ecf20Sopenharmony_ci#define CCN_TYPE_HNF	0x04
1058c2ecf20Sopenharmony_ci#define CCN_TYPE_HNI	0x05
1068c2ecf20Sopenharmony_ci#define CCN_TYPE_XP	0x08
1078c2ecf20Sopenharmony_ci#define CCN_TYPE_SBSX	0x0c
1088c2ecf20Sopenharmony_ci#define CCN_TYPE_SBAS	0x10
1098c2ecf20Sopenharmony_ci#define CCN_TYPE_RNI_1P	0x14
1108c2ecf20Sopenharmony_ci#define CCN_TYPE_RNI_2P	0x15
1118c2ecf20Sopenharmony_ci#define CCN_TYPE_RNI_3P	0x16
1128c2ecf20Sopenharmony_ci#define CCN_TYPE_RND_1P	0x18 /* RN-D = RN-I + DVM */
1138c2ecf20Sopenharmony_ci#define CCN_TYPE_RND_2P	0x19
1148c2ecf20Sopenharmony_ci#define CCN_TYPE_RND_3P	0x1a
1158c2ecf20Sopenharmony_ci#define CCN_TYPE_CYCLES	0xff /* Pseudotype */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define CCN_EVENT_WATCHPOINT 0xfe /* Pseudoevent */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#define CCN_NUM_PMU_EVENTS		4
1208c2ecf20Sopenharmony_ci#define CCN_NUM_XP_WATCHPOINTS		2 /* See DT.dbg_id.num_watchpoints */
1218c2ecf20Sopenharmony_ci#define CCN_NUM_PMU_EVENT_COUNTERS	8 /* See DT.dbg_id.num_pmucntr */
1228c2ecf20Sopenharmony_ci#define CCN_IDX_PMU_CYCLE_COUNTER	CCN_NUM_PMU_EVENT_COUNTERS
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define CCN_NUM_PREDEFINED_MASKS	4
1258c2ecf20Sopenharmony_ci#define CCN_IDX_MASK_ANY		(CCN_NUM_PMU_EVENT_COUNTERS + 0)
1268c2ecf20Sopenharmony_ci#define CCN_IDX_MASK_EXACT		(CCN_NUM_PMU_EVENT_COUNTERS + 1)
1278c2ecf20Sopenharmony_ci#define CCN_IDX_MASK_ORDER		(CCN_NUM_PMU_EVENT_COUNTERS + 2)
1288c2ecf20Sopenharmony_ci#define CCN_IDX_MASK_OPCODE		(CCN_NUM_PMU_EVENT_COUNTERS + 3)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistruct arm_ccn_component {
1318c2ecf20Sopenharmony_ci	void __iomem *base;
1328c2ecf20Sopenharmony_ci	u32 type;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	DECLARE_BITMAP(pmu_events_mask, CCN_NUM_PMU_EVENTS);
1358c2ecf20Sopenharmony_ci	union {
1368c2ecf20Sopenharmony_ci		struct {
1378c2ecf20Sopenharmony_ci			DECLARE_BITMAP(dt_cmp_mask, CCN_NUM_XP_WATCHPOINTS);
1388c2ecf20Sopenharmony_ci		} xp;
1398c2ecf20Sopenharmony_ci	};
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#define pmu_to_arm_ccn(_pmu) container_of(container_of(_pmu, \
1438c2ecf20Sopenharmony_ci	struct arm_ccn_dt, pmu), struct arm_ccn, dt)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistruct arm_ccn_dt {
1468c2ecf20Sopenharmony_ci	int id;
1478c2ecf20Sopenharmony_ci	void __iomem *base;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	spinlock_t config_lock;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	DECLARE_BITMAP(pmu_counters_mask, CCN_NUM_PMU_EVENT_COUNTERS + 1);
1528c2ecf20Sopenharmony_ci	struct {
1538c2ecf20Sopenharmony_ci		struct arm_ccn_component *source;
1548c2ecf20Sopenharmony_ci		struct perf_event *event;
1558c2ecf20Sopenharmony_ci	} pmu_counters[CCN_NUM_PMU_EVENT_COUNTERS + 1];
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	struct {
1588c2ecf20Sopenharmony_ci	       u64 l, h;
1598c2ecf20Sopenharmony_ci	} cmp_mask[CCN_NUM_PMU_EVENT_COUNTERS + CCN_NUM_PREDEFINED_MASKS];
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	struct hrtimer hrtimer;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	unsigned int cpu;
1648c2ecf20Sopenharmony_ci	struct hlist_node node;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	struct pmu pmu;
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistruct arm_ccn {
1708c2ecf20Sopenharmony_ci	struct device *dev;
1718c2ecf20Sopenharmony_ci	void __iomem *base;
1728c2ecf20Sopenharmony_ci	unsigned int irq;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	unsigned sbas_present:1;
1758c2ecf20Sopenharmony_ci	unsigned sbsx_present:1;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	int num_nodes;
1788c2ecf20Sopenharmony_ci	struct arm_ccn_component *node;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	int num_xps;
1818c2ecf20Sopenharmony_ci	struct arm_ccn_component *xp;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	struct arm_ccn_dt dt;
1848c2ecf20Sopenharmony_ci	int mn_id;
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int arm_ccn_node_to_xp(int node)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	return node / CCN_NUM_XP_PORTS;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic int arm_ccn_node_to_xp_port(int node)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	return node % CCN_NUM_XP_PORTS;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/*
1998c2ecf20Sopenharmony_ci * Bit shifts and masks in these defines must be kept in sync with
2008c2ecf20Sopenharmony_ci * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
2018c2ecf20Sopenharmony_ci */
2028c2ecf20Sopenharmony_ci#define CCN_CONFIG_NODE(_config)	(((_config) >> 0) & 0xff)
2038c2ecf20Sopenharmony_ci#define CCN_CONFIG_XP(_config)		(((_config) >> 0) & 0xff)
2048c2ecf20Sopenharmony_ci#define CCN_CONFIG_TYPE(_config)	(((_config) >> 8) & 0xff)
2058c2ecf20Sopenharmony_ci#define CCN_CONFIG_EVENT(_config)	(((_config) >> 16) & 0xff)
2068c2ecf20Sopenharmony_ci#define CCN_CONFIG_PORT(_config)	(((_config) >> 24) & 0x3)
2078c2ecf20Sopenharmony_ci#define CCN_CONFIG_BUS(_config)		(((_config) >> 24) & 0x3)
2088c2ecf20Sopenharmony_ci#define CCN_CONFIG_VC(_config)		(((_config) >> 26) & 0x7)
2098c2ecf20Sopenharmony_ci#define CCN_CONFIG_DIR(_config)		(((_config) >> 29) & 0x1)
2108c2ecf20Sopenharmony_ci#define CCN_CONFIG_MASK(_config)	(((_config) >> 30) & 0xf)
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_config_set(u64 *config, u32 node_xp, u32 type, u32 port)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	*config &= ~((0xff << 0) | (0xff << 8) | (0x3 << 24));
2158c2ecf20Sopenharmony_ci	*config |= (node_xp << 0) | (type << 8) | (port << 24);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic ssize_t arm_ccn_pmu_format_show(struct device *dev,
2198c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct dev_ext_attribute *ea = container_of(attr,
2228c2ecf20Sopenharmony_ci			struct dev_ext_attribute, attr);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%s\n", (char *)ea->var);
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#define CCN_FORMAT_ATTR(_name, _config) \
2288c2ecf20Sopenharmony_ci	struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
2298c2ecf20Sopenharmony_ci			{ __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
2308c2ecf20Sopenharmony_ci			NULL), _config }
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(node, "config:0-7");
2338c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(xp, "config:0-7");
2348c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(type, "config:8-15");
2358c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(event, "config:16-23");
2368c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(port, "config:24-25");
2378c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(bus, "config:24-25");
2388c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(vc, "config:26-28");
2398c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(dir, "config:29-29");
2408c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(mask, "config:30-33");
2418c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(cmp_l, "config1:0-62");
2428c2ecf20Sopenharmony_cistatic CCN_FORMAT_ATTR(cmp_h, "config2:0-59");
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic struct attribute *arm_ccn_pmu_format_attrs[] = {
2458c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_node.attr.attr,
2468c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_xp.attr.attr,
2478c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_type.attr.attr,
2488c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_event.attr.attr,
2498c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_port.attr.attr,
2508c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_bus.attr.attr,
2518c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_vc.attr.attr,
2528c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_dir.attr.attr,
2538c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_mask.attr.attr,
2548c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_cmp_l.attr.attr,
2558c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_cmp_h.attr.attr,
2568c2ecf20Sopenharmony_ci	NULL
2578c2ecf20Sopenharmony_ci};
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic const struct attribute_group arm_ccn_pmu_format_attr_group = {
2608c2ecf20Sopenharmony_ci	.name = "format",
2618c2ecf20Sopenharmony_ci	.attrs = arm_ccn_pmu_format_attrs,
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistruct arm_ccn_pmu_event {
2668c2ecf20Sopenharmony_ci	struct device_attribute attr;
2678c2ecf20Sopenharmony_ci	u32 type;
2688c2ecf20Sopenharmony_ci	u32 event;
2698c2ecf20Sopenharmony_ci	int num_ports;
2708c2ecf20Sopenharmony_ci	int num_vcs;
2718c2ecf20Sopenharmony_ci	const char *def;
2728c2ecf20Sopenharmony_ci	int mask;
2738c2ecf20Sopenharmony_ci};
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci#define CCN_EVENT_ATTR(_name) \
2768c2ecf20Sopenharmony_ci	__ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci/*
2798c2ecf20Sopenharmony_ci * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
2808c2ecf20Sopenharmony_ci * their ports in XP they are connected to. For the sake of usability they are
2818c2ecf20Sopenharmony_ci * explicitly defined here (and translated into a relevant watchpoint in
2828c2ecf20Sopenharmony_ci * arm_ccn_pmu_event_init()) so the user can easily request them without deep
2838c2ecf20Sopenharmony_ci * knowledge of the flit format.
2848c2ecf20Sopenharmony_ci */
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci#define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
2878c2ecf20Sopenharmony_ci		.type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
2888c2ecf20Sopenharmony_ci		.num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
2898c2ecf20Sopenharmony_ci		.def = _def, .mask = _mask, }
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci#define CCN_EVENT_HNI(_name, _def, _mask) { \
2928c2ecf20Sopenharmony_ci		.attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
2938c2ecf20Sopenharmony_ci		.event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
2948c2ecf20Sopenharmony_ci		.num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci#define CCN_EVENT_SBSX(_name, _def, _mask) { \
2978c2ecf20Sopenharmony_ci		.attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
2988c2ecf20Sopenharmony_ci		.event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
2998c2ecf20Sopenharmony_ci		.num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci#define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
3028c2ecf20Sopenharmony_ci		.type = CCN_TYPE_HNF, .event = _event, }
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci#define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
3058c2ecf20Sopenharmony_ci		.type = CCN_TYPE_XP, .event = _event, \
3068c2ecf20Sopenharmony_ci		.num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/*
3098c2ecf20Sopenharmony_ci * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
3108c2ecf20Sopenharmony_ci * on configuration. One of them is picked to represent the whole group,
3118c2ecf20Sopenharmony_ci * as they all share the same event types.
3128c2ecf20Sopenharmony_ci */
3138c2ecf20Sopenharmony_ci#define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
3148c2ecf20Sopenharmony_ci		.type = CCN_TYPE_RNI_3P, .event = _event, }
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci#define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
3178c2ecf20Sopenharmony_ci		.type = CCN_TYPE_SBAS, .event = _event, }
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci#define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
3208c2ecf20Sopenharmony_ci		.type = CCN_TYPE_CYCLES }
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic ssize_t arm_ccn_pmu_event_show(struct device *dev,
3248c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
3278c2ecf20Sopenharmony_ci	struct arm_ccn_pmu_event *event = container_of(attr,
3288c2ecf20Sopenharmony_ci			struct arm_ccn_pmu_event, attr);
3298c2ecf20Sopenharmony_ci	ssize_t res;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	res = scnprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
3328c2ecf20Sopenharmony_ci	if (event->event)
3338c2ecf20Sopenharmony_ci		res += scnprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
3348c2ecf20Sopenharmony_ci				event->event);
3358c2ecf20Sopenharmony_ci	if (event->def)
3368c2ecf20Sopenharmony_ci		res += scnprintf(buf + res, PAGE_SIZE - res, ",%s",
3378c2ecf20Sopenharmony_ci				event->def);
3388c2ecf20Sopenharmony_ci	if (event->mask)
3398c2ecf20Sopenharmony_ci		res += scnprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
3408c2ecf20Sopenharmony_ci				event->mask);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	/* Arguments required by an event */
3438c2ecf20Sopenharmony_ci	switch (event->type) {
3448c2ecf20Sopenharmony_ci	case CCN_TYPE_CYCLES:
3458c2ecf20Sopenharmony_ci		break;
3468c2ecf20Sopenharmony_ci	case CCN_TYPE_XP:
3478c2ecf20Sopenharmony_ci		res += scnprintf(buf + res, PAGE_SIZE - res,
3488c2ecf20Sopenharmony_ci				",xp=?,vc=?");
3498c2ecf20Sopenharmony_ci		if (event->event == CCN_EVENT_WATCHPOINT)
3508c2ecf20Sopenharmony_ci			res += scnprintf(buf + res, PAGE_SIZE - res,
3518c2ecf20Sopenharmony_ci					",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
3528c2ecf20Sopenharmony_ci		else
3538c2ecf20Sopenharmony_ci			res += scnprintf(buf + res, PAGE_SIZE - res,
3548c2ecf20Sopenharmony_ci					",bus=?");
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci		break;
3578c2ecf20Sopenharmony_ci	case CCN_TYPE_MN:
3588c2ecf20Sopenharmony_ci		res += scnprintf(buf + res, PAGE_SIZE - res, ",node=%d", ccn->mn_id);
3598c2ecf20Sopenharmony_ci		break;
3608c2ecf20Sopenharmony_ci	default:
3618c2ecf20Sopenharmony_ci		res += scnprintf(buf + res, PAGE_SIZE - res, ",node=?");
3628c2ecf20Sopenharmony_ci		break;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	res += scnprintf(buf + res, PAGE_SIZE - res, "\n");
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	return res;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
3718c2ecf20Sopenharmony_ci				     struct attribute *attr, int index)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
3748c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
3758c2ecf20Sopenharmony_ci	struct device_attribute *dev_attr = container_of(attr,
3768c2ecf20Sopenharmony_ci			struct device_attribute, attr);
3778c2ecf20Sopenharmony_ci	struct arm_ccn_pmu_event *event = container_of(dev_attr,
3788c2ecf20Sopenharmony_ci			struct arm_ccn_pmu_event, attr);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
3818c2ecf20Sopenharmony_ci		return 0;
3828c2ecf20Sopenharmony_ci	if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
3838c2ecf20Sopenharmony_ci		return 0;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	return attr->mode;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
3898c2ecf20Sopenharmony_ci	CCN_EVENT_MN(eobarrier, "dir=1,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
3908c2ecf20Sopenharmony_ci	CCN_EVENT_MN(ecbarrier, "dir=1,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
3918c2ecf20Sopenharmony_ci	CCN_EVENT_MN(dvmop, "dir=1,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
3928c2ecf20Sopenharmony_ci	CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
3938c2ecf20Sopenharmony_ci	CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
3948c2ecf20Sopenharmony_ci	CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
3958c2ecf20Sopenharmony_ci	CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
3968c2ecf20Sopenharmony_ci	CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
3978c2ecf20Sopenharmony_ci			CCN_IDX_MASK_ORDER),
3988c2ecf20Sopenharmony_ci	CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
3998c2ecf20Sopenharmony_ci	CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
4008c2ecf20Sopenharmony_ci	CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
4018c2ecf20Sopenharmony_ci	CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
4028c2ecf20Sopenharmony_ci	CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
4038c2ecf20Sopenharmony_ci			CCN_IDX_MASK_ORDER),
4048c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(cache_miss, 0x1),
4058c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
4068c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(cache_fill, 0x3),
4078c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(pocq_retry, 0x4),
4088c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
4098c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(sf_hit, 0x6),
4108c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(sf_evictions, 0x7),
4118c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(snoops_sent, 0x8),
4128c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(snoops_broadcast, 0x9),
4138c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(l3_eviction, 0xa),
4148c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
4158c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(mc_retries, 0xc),
4168c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(mc_reqs, 0xd),
4178c2ecf20Sopenharmony_ci	CCN_EVENT_HNF(qos_hh_retry, 0xe),
4188c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(rdata_beats_p0, 0x1),
4198c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(rdata_beats_p1, 0x2),
4208c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(rdata_beats_p2, 0x3),
4218c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(rxdat_flits, 0x4),
4228c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(txdat_flits, 0x5),
4238c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(txreq_flits, 0x6),
4248c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(txreq_flits_retried, 0x7),
4258c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(rrt_full, 0x8),
4268c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(wrt_full, 0x9),
4278c2ecf20Sopenharmony_ci	CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
4288c2ecf20Sopenharmony_ci	CCN_EVENT_XP(upload_starvation, 0x1),
4298c2ecf20Sopenharmony_ci	CCN_EVENT_XP(download_starvation, 0x2),
4308c2ecf20Sopenharmony_ci	CCN_EVENT_XP(respin, 0x3),
4318c2ecf20Sopenharmony_ci	CCN_EVENT_XP(valid_flit, 0x4),
4328c2ecf20Sopenharmony_ci	CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
4338c2ecf20Sopenharmony_ci	CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
4348c2ecf20Sopenharmony_ci	CCN_EVENT_SBAS(rxdat_flits, 0x4),
4358c2ecf20Sopenharmony_ci	CCN_EVENT_SBAS(txdat_flits, 0x5),
4368c2ecf20Sopenharmony_ci	CCN_EVENT_SBAS(txreq_flits, 0x6),
4378c2ecf20Sopenharmony_ci	CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
4388c2ecf20Sopenharmony_ci	CCN_EVENT_SBAS(rrt_full, 0x8),
4398c2ecf20Sopenharmony_ci	CCN_EVENT_SBAS(wrt_full, 0x9),
4408c2ecf20Sopenharmony_ci	CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
4418c2ecf20Sopenharmony_ci	CCN_EVENT_CYCLES(cycles),
4428c2ecf20Sopenharmony_ci};
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci/* Populated in arm_ccn_init() */
4458c2ecf20Sopenharmony_cistatic struct attribute
4468c2ecf20Sopenharmony_ci		*arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic const struct attribute_group arm_ccn_pmu_events_attr_group = {
4498c2ecf20Sopenharmony_ci	.name = "events",
4508c2ecf20Sopenharmony_ci	.is_visible = arm_ccn_pmu_events_is_visible,
4518c2ecf20Sopenharmony_ci	.attrs = arm_ccn_pmu_events_attrs,
4528c2ecf20Sopenharmony_ci};
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	unsigned long i;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
4608c2ecf20Sopenharmony_ci		return NULL;
4618c2ecf20Sopenharmony_ci	i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	switch (name[1]) {
4648c2ecf20Sopenharmony_ci	case 'l':
4658c2ecf20Sopenharmony_ci		return &ccn->dt.cmp_mask[i].l;
4668c2ecf20Sopenharmony_ci	case 'h':
4678c2ecf20Sopenharmony_ci		return &ccn->dt.cmp_mask[i].h;
4688c2ecf20Sopenharmony_ci	default:
4698c2ecf20Sopenharmony_ci		return NULL;
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
4748c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
4778c2ecf20Sopenharmony_ci	u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	return mask ? snprintf(buf, PAGE_SIZE, "0x%016llx\n", *mask) : -EINVAL;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
4838c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t count)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
4868c2ecf20Sopenharmony_ci	u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
4878c2ecf20Sopenharmony_ci	int err = -EINVAL;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	if (mask)
4908c2ecf20Sopenharmony_ci		err = kstrtoull(buf, 0, mask);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	return err ? err : count;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci#define CCN_CMP_MASK_ATTR(_name) \
4968c2ecf20Sopenharmony_ci	struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
4978c2ecf20Sopenharmony_ci			__ATTR(_name, S_IRUGO | S_IWUSR, \
4988c2ecf20Sopenharmony_ci			arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci#define CCN_CMP_MASK_ATTR_RO(_name) \
5018c2ecf20Sopenharmony_ci	struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
5028c2ecf20Sopenharmony_ci			__ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(0l);
5058c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(0h);
5068c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(1l);
5078c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(1h);
5088c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(2l);
5098c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(2h);
5108c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(3l);
5118c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(3h);
5128c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(4l);
5138c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(4h);
5148c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(5l);
5158c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(5h);
5168c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(6l);
5178c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(6h);
5188c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(7l);
5198c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR(7h);
5208c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR_RO(8l);
5218c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR_RO(8h);
5228c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR_RO(9l);
5238c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR_RO(9h);
5248c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR_RO(al);
5258c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR_RO(ah);
5268c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR_RO(bl);
5278c2ecf20Sopenharmony_cistatic CCN_CMP_MASK_ATTR_RO(bh);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_cistatic struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
5308c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
5318c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
5328c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
5338c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
5348c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
5358c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
5368c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
5378c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
5388c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
5398c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
5408c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
5418c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
5428c2ecf20Sopenharmony_ci	NULL
5438c2ecf20Sopenharmony_ci};
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_cistatic const struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
5468c2ecf20Sopenharmony_ci	.name = "cmp_mask",
5478c2ecf20Sopenharmony_ci	.attrs = arm_ccn_pmu_cmp_mask_attrs,
5488c2ecf20Sopenharmony_ci};
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
5518c2ecf20Sopenharmony_ci				     struct device_attribute *attr, char *buf)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	return cpumap_print_to_pagebuf(true, buf, cpumask_of(ccn->dt.cpu));
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic struct device_attribute arm_ccn_pmu_cpumask_attr =
5598c2ecf20Sopenharmony_ci		__ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
5628c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cpumask_attr.attr,
5638c2ecf20Sopenharmony_ci	NULL,
5648c2ecf20Sopenharmony_ci};
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_cistatic const struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
5678c2ecf20Sopenharmony_ci	.attrs = arm_ccn_pmu_cpumask_attrs,
5688c2ecf20Sopenharmony_ci};
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci/*
5718c2ecf20Sopenharmony_ci * Default poll period is 10ms, which is way over the top anyway,
5728c2ecf20Sopenharmony_ci * as in the worst case scenario (an event every cycle), with 1GHz
5738c2ecf20Sopenharmony_ci * clocked bus, the smallest, 32 bit counter will overflow in
5748c2ecf20Sopenharmony_ci * more than 4s.
5758c2ecf20Sopenharmony_ci */
5768c2ecf20Sopenharmony_cistatic unsigned int arm_ccn_pmu_poll_period_us = 10000;
5778c2ecf20Sopenharmony_cimodule_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
5788c2ecf20Sopenharmony_ci		S_IRUGO | S_IWUSR);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_cistatic ktime_t arm_ccn_pmu_timer_period(void)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_cistatic const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
5878c2ecf20Sopenharmony_ci	&arm_ccn_pmu_events_attr_group,
5888c2ecf20Sopenharmony_ci	&arm_ccn_pmu_format_attr_group,
5898c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cmp_mask_attr_group,
5908c2ecf20Sopenharmony_ci	&arm_ccn_pmu_cpumask_attr_group,
5918c2ecf20Sopenharmony_ci	NULL
5928c2ecf20Sopenharmony_ci};
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_cistatic int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
5968c2ecf20Sopenharmony_ci{
5978c2ecf20Sopenharmony_ci	int bit;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	do {
6008c2ecf20Sopenharmony_ci		bit = find_first_zero_bit(bitmap, size);
6018c2ecf20Sopenharmony_ci		if (bit >= size)
6028c2ecf20Sopenharmony_ci			return -EAGAIN;
6038c2ecf20Sopenharmony_ci	} while (test_and_set_bit(bit, bitmap));
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	return bit;
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci/* All RN-I and RN-D nodes have identical PMUs */
6098c2ecf20Sopenharmony_cistatic int arm_ccn_pmu_type_eq(u32 a, u32 b)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	if (a == b)
6128c2ecf20Sopenharmony_ci		return 1;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	switch (a) {
6158c2ecf20Sopenharmony_ci	case CCN_TYPE_RNI_1P:
6168c2ecf20Sopenharmony_ci	case CCN_TYPE_RNI_2P:
6178c2ecf20Sopenharmony_ci	case CCN_TYPE_RNI_3P:
6188c2ecf20Sopenharmony_ci	case CCN_TYPE_RND_1P:
6198c2ecf20Sopenharmony_ci	case CCN_TYPE_RND_2P:
6208c2ecf20Sopenharmony_ci	case CCN_TYPE_RND_3P:
6218c2ecf20Sopenharmony_ci		switch (b) {
6228c2ecf20Sopenharmony_ci		case CCN_TYPE_RNI_1P:
6238c2ecf20Sopenharmony_ci		case CCN_TYPE_RNI_2P:
6248c2ecf20Sopenharmony_ci		case CCN_TYPE_RNI_3P:
6258c2ecf20Sopenharmony_ci		case CCN_TYPE_RND_1P:
6268c2ecf20Sopenharmony_ci		case CCN_TYPE_RND_2P:
6278c2ecf20Sopenharmony_ci		case CCN_TYPE_RND_3P:
6288c2ecf20Sopenharmony_ci			return 1;
6298c2ecf20Sopenharmony_ci		}
6308c2ecf20Sopenharmony_ci		break;
6318c2ecf20Sopenharmony_ci	}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	return 0;
6348c2ecf20Sopenharmony_ci}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_cistatic int arm_ccn_pmu_event_alloc(struct perf_event *event)
6378c2ecf20Sopenharmony_ci{
6388c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
6398c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
6408c2ecf20Sopenharmony_ci	u32 node_xp, type, event_id;
6418c2ecf20Sopenharmony_ci	struct arm_ccn_component *source;
6428c2ecf20Sopenharmony_ci	int bit;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	node_xp = CCN_CONFIG_NODE(event->attr.config);
6458c2ecf20Sopenharmony_ci	type = CCN_CONFIG_TYPE(event->attr.config);
6468c2ecf20Sopenharmony_ci	event_id = CCN_CONFIG_EVENT(event->attr.config);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	/* Allocate the cycle counter */
6498c2ecf20Sopenharmony_ci	if (type == CCN_TYPE_CYCLES) {
6508c2ecf20Sopenharmony_ci		if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
6518c2ecf20Sopenharmony_ci				ccn->dt.pmu_counters_mask))
6528c2ecf20Sopenharmony_ci			return -EAGAIN;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci		hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
6558c2ecf20Sopenharmony_ci		ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci		return 0;
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	/* Allocate an event counter */
6618c2ecf20Sopenharmony_ci	hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
6628c2ecf20Sopenharmony_ci			CCN_NUM_PMU_EVENT_COUNTERS);
6638c2ecf20Sopenharmony_ci	if (hw->idx < 0) {
6648c2ecf20Sopenharmony_ci		dev_dbg(ccn->dev, "No more counters available!\n");
6658c2ecf20Sopenharmony_ci		return -EAGAIN;
6668c2ecf20Sopenharmony_ci	}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	if (type == CCN_TYPE_XP)
6698c2ecf20Sopenharmony_ci		source = &ccn->xp[node_xp];
6708c2ecf20Sopenharmony_ci	else
6718c2ecf20Sopenharmony_ci		source = &ccn->node[node_xp];
6728c2ecf20Sopenharmony_ci	ccn->dt.pmu_counters[hw->idx].source = source;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/* Allocate an event source or a watchpoint */
6758c2ecf20Sopenharmony_ci	if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
6768c2ecf20Sopenharmony_ci		bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
6778c2ecf20Sopenharmony_ci				CCN_NUM_XP_WATCHPOINTS);
6788c2ecf20Sopenharmony_ci	else
6798c2ecf20Sopenharmony_ci		bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
6808c2ecf20Sopenharmony_ci				CCN_NUM_PMU_EVENTS);
6818c2ecf20Sopenharmony_ci	if (bit < 0) {
6828c2ecf20Sopenharmony_ci		dev_dbg(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
6838c2ecf20Sopenharmony_ci				node_xp);
6848c2ecf20Sopenharmony_ci		clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
6858c2ecf20Sopenharmony_ci		return -EAGAIN;
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci	hw->config_base = bit;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	ccn->dt.pmu_counters[hw->idx].event = event;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	return 0;
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_event_release(struct perf_event *event)
6958c2ecf20Sopenharmony_ci{
6968c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
6978c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
7008c2ecf20Sopenharmony_ci		clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
7018c2ecf20Sopenharmony_ci	} else {
7028c2ecf20Sopenharmony_ci		struct arm_ccn_component *source =
7038c2ecf20Sopenharmony_ci				ccn->dt.pmu_counters[hw->idx].source;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci		if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
7068c2ecf20Sopenharmony_ci				CCN_CONFIG_EVENT(event->attr.config) ==
7078c2ecf20Sopenharmony_ci				CCN_EVENT_WATCHPOINT)
7088c2ecf20Sopenharmony_ci			clear_bit(hw->config_base, source->xp.dt_cmp_mask);
7098c2ecf20Sopenharmony_ci		else
7108c2ecf20Sopenharmony_ci			clear_bit(hw->config_base, source->pmu_events_mask);
7118c2ecf20Sopenharmony_ci		clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	ccn->dt.pmu_counters[hw->idx].source = NULL;
7158c2ecf20Sopenharmony_ci	ccn->dt.pmu_counters[hw->idx].event = NULL;
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_cistatic int arm_ccn_pmu_event_init(struct perf_event *event)
7198c2ecf20Sopenharmony_ci{
7208c2ecf20Sopenharmony_ci	struct arm_ccn *ccn;
7218c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
7228c2ecf20Sopenharmony_ci	u32 node_xp, type, event_id;
7238c2ecf20Sopenharmony_ci	int valid;
7248c2ecf20Sopenharmony_ci	int i;
7258c2ecf20Sopenharmony_ci	struct perf_event *sibling;
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	if (event->attr.type != event->pmu->type)
7288c2ecf20Sopenharmony_ci		return -ENOENT;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	ccn = pmu_to_arm_ccn(event->pmu);
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	if (hw->sample_period) {
7338c2ecf20Sopenharmony_ci		dev_dbg(ccn->dev, "Sampling not supported!\n");
7348c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	if (has_branch_stack(event)) {
7388c2ecf20Sopenharmony_ci		dev_dbg(ccn->dev, "Can't exclude execution levels!\n");
7398c2ecf20Sopenharmony_ci		return -EINVAL;
7408c2ecf20Sopenharmony_ci	}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	if (event->cpu < 0) {
7438c2ecf20Sopenharmony_ci		dev_dbg(ccn->dev, "Can't provide per-task data!\n");
7448c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci	/*
7478c2ecf20Sopenharmony_ci	 * Many perf core operations (eg. events rotation) operate on a
7488c2ecf20Sopenharmony_ci	 * single CPU context. This is obvious for CPU PMUs, where one
7498c2ecf20Sopenharmony_ci	 * expects the same sets of events being observed on all CPUs,
7508c2ecf20Sopenharmony_ci	 * but can lead to issues for off-core PMUs, like CCN, where each
7518c2ecf20Sopenharmony_ci	 * event could be theoretically assigned to a different CPU. To
7528c2ecf20Sopenharmony_ci	 * mitigate this, we enforce CPU assignment to one, selected
7538c2ecf20Sopenharmony_ci	 * processor (the one described in the "cpumask" attribute).
7548c2ecf20Sopenharmony_ci	 */
7558c2ecf20Sopenharmony_ci	event->cpu = ccn->dt.cpu;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	node_xp = CCN_CONFIG_NODE(event->attr.config);
7588c2ecf20Sopenharmony_ci	type = CCN_CONFIG_TYPE(event->attr.config);
7598c2ecf20Sopenharmony_ci	event_id = CCN_CONFIG_EVENT(event->attr.config);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* Validate node/xp vs topology */
7628c2ecf20Sopenharmony_ci	switch (type) {
7638c2ecf20Sopenharmony_ci	case CCN_TYPE_MN:
7648c2ecf20Sopenharmony_ci		if (node_xp != ccn->mn_id) {
7658c2ecf20Sopenharmony_ci			dev_dbg(ccn->dev, "Invalid MN ID %d!\n", node_xp);
7668c2ecf20Sopenharmony_ci			return -EINVAL;
7678c2ecf20Sopenharmony_ci		}
7688c2ecf20Sopenharmony_ci		break;
7698c2ecf20Sopenharmony_ci	case CCN_TYPE_XP:
7708c2ecf20Sopenharmony_ci		if (node_xp >= ccn->num_xps) {
7718c2ecf20Sopenharmony_ci			dev_dbg(ccn->dev, "Invalid XP ID %d!\n", node_xp);
7728c2ecf20Sopenharmony_ci			return -EINVAL;
7738c2ecf20Sopenharmony_ci		}
7748c2ecf20Sopenharmony_ci		break;
7758c2ecf20Sopenharmony_ci	case CCN_TYPE_CYCLES:
7768c2ecf20Sopenharmony_ci		break;
7778c2ecf20Sopenharmony_ci	default:
7788c2ecf20Sopenharmony_ci		if (node_xp >= ccn->num_nodes) {
7798c2ecf20Sopenharmony_ci			dev_dbg(ccn->dev, "Invalid node ID %d!\n", node_xp);
7808c2ecf20Sopenharmony_ci			return -EINVAL;
7818c2ecf20Sopenharmony_ci		}
7828c2ecf20Sopenharmony_ci		if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
7838c2ecf20Sopenharmony_ci			dev_dbg(ccn->dev, "Invalid type 0x%x for node %d!\n",
7848c2ecf20Sopenharmony_ci					type, node_xp);
7858c2ecf20Sopenharmony_ci			return -EINVAL;
7868c2ecf20Sopenharmony_ci		}
7878c2ecf20Sopenharmony_ci		break;
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	/* Validate event ID vs available for the type */
7918c2ecf20Sopenharmony_ci	for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
7928c2ecf20Sopenharmony_ci			i++) {
7938c2ecf20Sopenharmony_ci		struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
7948c2ecf20Sopenharmony_ci		u32 port = CCN_CONFIG_PORT(event->attr.config);
7958c2ecf20Sopenharmony_ci		u32 vc = CCN_CONFIG_VC(event->attr.config);
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci		if (!arm_ccn_pmu_type_eq(type, e->type))
7988c2ecf20Sopenharmony_ci			continue;
7998c2ecf20Sopenharmony_ci		if (event_id != e->event)
8008c2ecf20Sopenharmony_ci			continue;
8018c2ecf20Sopenharmony_ci		if (e->num_ports && port >= e->num_ports) {
8028c2ecf20Sopenharmony_ci			dev_dbg(ccn->dev, "Invalid port %d for node/XP %d!\n",
8038c2ecf20Sopenharmony_ci					port, node_xp);
8048c2ecf20Sopenharmony_ci			return -EINVAL;
8058c2ecf20Sopenharmony_ci		}
8068c2ecf20Sopenharmony_ci		if (e->num_vcs && vc >= e->num_vcs) {
8078c2ecf20Sopenharmony_ci			dev_dbg(ccn->dev, "Invalid vc %d for node/XP %d!\n",
8088c2ecf20Sopenharmony_ci					vc, node_xp);
8098c2ecf20Sopenharmony_ci			return -EINVAL;
8108c2ecf20Sopenharmony_ci		}
8118c2ecf20Sopenharmony_ci		valid = 1;
8128c2ecf20Sopenharmony_ci	}
8138c2ecf20Sopenharmony_ci	if (!valid) {
8148c2ecf20Sopenharmony_ci		dev_dbg(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
8158c2ecf20Sopenharmony_ci				event_id, node_xp);
8168c2ecf20Sopenharmony_ci		return -EINVAL;
8178c2ecf20Sopenharmony_ci	}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	/* Watchpoint-based event for a node is actually set on XP */
8208c2ecf20Sopenharmony_ci	if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
8218c2ecf20Sopenharmony_ci		u32 port;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci		type = CCN_TYPE_XP;
8248c2ecf20Sopenharmony_ci		port = arm_ccn_node_to_xp_port(node_xp);
8258c2ecf20Sopenharmony_ci		node_xp = arm_ccn_node_to_xp(node_xp);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci		arm_ccn_pmu_config_set(&event->attr.config,
8288c2ecf20Sopenharmony_ci				node_xp, type, port);
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	/*
8328c2ecf20Sopenharmony_ci	 * We must NOT create groups containing mixed PMUs, although software
8338c2ecf20Sopenharmony_ci	 * events are acceptable (for example to create a CCN group
8348c2ecf20Sopenharmony_ci	 * periodically read when a hrtimer aka cpu-clock leader triggers).
8358c2ecf20Sopenharmony_ci	 */
8368c2ecf20Sopenharmony_ci	if (event->group_leader->pmu != event->pmu &&
8378c2ecf20Sopenharmony_ci			!is_software_event(event->group_leader))
8388c2ecf20Sopenharmony_ci		return -EINVAL;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	for_each_sibling_event(sibling, event->group_leader) {
8418c2ecf20Sopenharmony_ci		if (sibling->pmu != event->pmu &&
8428c2ecf20Sopenharmony_ci				!is_software_event(sibling))
8438c2ecf20Sopenharmony_ci			return -EINVAL;
8448c2ecf20Sopenharmony_ci	}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	return 0;
8478c2ecf20Sopenharmony_ci}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_cistatic u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
8508c2ecf20Sopenharmony_ci{
8518c2ecf20Sopenharmony_ci	u64 res;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
8548c2ecf20Sopenharmony_ci#ifdef readq
8558c2ecf20Sopenharmony_ci		res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
8568c2ecf20Sopenharmony_ci#else
8578c2ecf20Sopenharmony_ci		/* 40 bit counter, can do snapshot and read in two parts */
8588c2ecf20Sopenharmony_ci		writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
8598c2ecf20Sopenharmony_ci		while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
8608c2ecf20Sopenharmony_ci			;
8618c2ecf20Sopenharmony_ci		writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
8628c2ecf20Sopenharmony_ci		res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
8638c2ecf20Sopenharmony_ci		res <<= 32;
8648c2ecf20Sopenharmony_ci		res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
8658c2ecf20Sopenharmony_ci#endif
8668c2ecf20Sopenharmony_ci	} else {
8678c2ecf20Sopenharmony_ci		res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	return res;
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_event_update(struct perf_event *event)
8748c2ecf20Sopenharmony_ci{
8758c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
8768c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
8778c2ecf20Sopenharmony_ci	u64 prev_count, new_count, mask;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	do {
8808c2ecf20Sopenharmony_ci		prev_count = local64_read(&hw->prev_count);
8818c2ecf20Sopenharmony_ci		new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
8828c2ecf20Sopenharmony_ci	} while (local64_xchg(&hw->prev_count, new_count) != prev_count);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	local64_add((new_count - prev_count) & mask, &event->count);
8878c2ecf20Sopenharmony_ci}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
8908c2ecf20Sopenharmony_ci{
8918c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
8928c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
8938c2ecf20Sopenharmony_ci	struct arm_ccn_component *xp;
8948c2ecf20Sopenharmony_ci	u32 val, dt_cfg;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	/* Nothing to do for cycle counter */
8978c2ecf20Sopenharmony_ci	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
8988c2ecf20Sopenharmony_ci		return;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
9018c2ecf20Sopenharmony_ci		xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
9028c2ecf20Sopenharmony_ci	else
9038c2ecf20Sopenharmony_ci		xp = &ccn->xp[arm_ccn_node_to_xp(
9048c2ecf20Sopenharmony_ci				CCN_CONFIG_NODE(event->attr.config))];
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	if (enable)
9078c2ecf20Sopenharmony_ci		dt_cfg = hw->event_base;
9088c2ecf20Sopenharmony_ci	else
9098c2ecf20Sopenharmony_ci		dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	spin_lock(&ccn->dt.config_lock);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	val = readl(xp->base + CCN_XP_DT_CONFIG);
9148c2ecf20Sopenharmony_ci	val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
9158c2ecf20Sopenharmony_ci			CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
9168c2ecf20Sopenharmony_ci	val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
9178c2ecf20Sopenharmony_ci	writel(val, xp->base + CCN_XP_DT_CONFIG);
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	spin_unlock(&ccn->dt.config_lock);
9208c2ecf20Sopenharmony_ci}
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
9238c2ecf20Sopenharmony_ci{
9248c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9258c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	local64_set(&event->hw.prev_count,
9288c2ecf20Sopenharmony_ci			arm_ccn_pmu_read_counter(ccn, hw->idx));
9298c2ecf20Sopenharmony_ci	hw->state = 0;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	/* Set the DT bus input, engaging the counter */
9328c2ecf20Sopenharmony_ci	arm_ccn_pmu_xp_dt_config(event, 1);
9338c2ecf20Sopenharmony_ci}
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
9368c2ecf20Sopenharmony_ci{
9378c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	/* Disable counting, setting the DT bus to pass-through mode */
9408c2ecf20Sopenharmony_ci	arm_ccn_pmu_xp_dt_config(event, 0);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	if (flags & PERF_EF_UPDATE)
9438c2ecf20Sopenharmony_ci		arm_ccn_pmu_event_update(event);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	hw->state |= PERF_HES_STOPPED;
9468c2ecf20Sopenharmony_ci}
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
9498c2ecf20Sopenharmony_ci{
9508c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9518c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
9528c2ecf20Sopenharmony_ci	struct arm_ccn_component *source =
9538c2ecf20Sopenharmony_ci			ccn->dt.pmu_counters[hw->idx].source;
9548c2ecf20Sopenharmony_ci	unsigned long wp = hw->config_base;
9558c2ecf20Sopenharmony_ci	u32 val;
9568c2ecf20Sopenharmony_ci	u64 cmp_l = event->attr.config1;
9578c2ecf20Sopenharmony_ci	u64 cmp_h = event->attr.config2;
9588c2ecf20Sopenharmony_ci	u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
9598c2ecf20Sopenharmony_ci	u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	/* Direction (RX/TX), device (port) & virtual channel */
9648c2ecf20Sopenharmony_ci	val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
9658c2ecf20Sopenharmony_ci	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
9668c2ecf20Sopenharmony_ci			CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
9678c2ecf20Sopenharmony_ci	val |= CCN_CONFIG_DIR(event->attr.config) <<
9688c2ecf20Sopenharmony_ci			CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
9698c2ecf20Sopenharmony_ci	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
9708c2ecf20Sopenharmony_ci			CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
9718c2ecf20Sopenharmony_ci	val |= CCN_CONFIG_PORT(event->attr.config) <<
9728c2ecf20Sopenharmony_ci			CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
9738c2ecf20Sopenharmony_ci	val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
9748c2ecf20Sopenharmony_ci			CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
9758c2ecf20Sopenharmony_ci	val |= CCN_CONFIG_VC(event->attr.config) <<
9768c2ecf20Sopenharmony_ci			CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
9778c2ecf20Sopenharmony_ci	writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	/* Comparison values */
9808c2ecf20Sopenharmony_ci	writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
9818c2ecf20Sopenharmony_ci	writel((cmp_l >> 32) & 0x7fffffff,
9828c2ecf20Sopenharmony_ci			source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
9838c2ecf20Sopenharmony_ci	writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
9848c2ecf20Sopenharmony_ci	writel((cmp_h >> 32) & 0x0fffffff,
9858c2ecf20Sopenharmony_ci			source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	/* Mask */
9888c2ecf20Sopenharmony_ci	writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
9898c2ecf20Sopenharmony_ci	writel((mask_l >> 32) & 0x7fffffff,
9908c2ecf20Sopenharmony_ci			source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
9918c2ecf20Sopenharmony_ci	writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
9928c2ecf20Sopenharmony_ci	writel((mask_h >> 32) & 0x0fffffff,
9938c2ecf20Sopenharmony_ci			source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
9948c2ecf20Sopenharmony_ci}
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_xp_event_config(struct perf_event *event)
9978c2ecf20Sopenharmony_ci{
9988c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
9998c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
10008c2ecf20Sopenharmony_ci	struct arm_ccn_component *source =
10018c2ecf20Sopenharmony_ci			ccn->dt.pmu_counters[hw->idx].source;
10028c2ecf20Sopenharmony_ci	u32 val, id;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	id = (CCN_CONFIG_VC(event->attr.config) << 4) |
10078c2ecf20Sopenharmony_ci			(CCN_CONFIG_BUS(event->attr.config) << 3) |
10088c2ecf20Sopenharmony_ci			(CCN_CONFIG_EVENT(event->attr.config) << 0);
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
10118c2ecf20Sopenharmony_ci	val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
10128c2ecf20Sopenharmony_ci			CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
10138c2ecf20Sopenharmony_ci	val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
10148c2ecf20Sopenharmony_ci	writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
10158c2ecf20Sopenharmony_ci}
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_node_event_config(struct perf_event *event)
10188c2ecf20Sopenharmony_ci{
10198c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
10208c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
10218c2ecf20Sopenharmony_ci	struct arm_ccn_component *source =
10228c2ecf20Sopenharmony_ci			ccn->dt.pmu_counters[hw->idx].source;
10238c2ecf20Sopenharmony_ci	u32 type = CCN_CONFIG_TYPE(event->attr.config);
10248c2ecf20Sopenharmony_ci	u32 val, port;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
10278c2ecf20Sopenharmony_ci	hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
10288c2ecf20Sopenharmony_ci			hw->config_base);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	/* These *_event_sel regs should be identical, but let's make sure... */
10318c2ecf20Sopenharmony_ci	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
10328c2ecf20Sopenharmony_ci	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
10338c2ecf20Sopenharmony_ci	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
10348c2ecf20Sopenharmony_ci			CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
10358c2ecf20Sopenharmony_ci	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
10368c2ecf20Sopenharmony_ci			CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
10378c2ecf20Sopenharmony_ci	BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
10388c2ecf20Sopenharmony_ci			CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
10398c2ecf20Sopenharmony_ci	BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
10408c2ecf20Sopenharmony_ci			CCN_RNI_PMU_EVENT_SEL__ID__MASK);
10418c2ecf20Sopenharmony_ci	if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
10428c2ecf20Sopenharmony_ci			!arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
10438c2ecf20Sopenharmony_ci		return;
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	/* Set the event id for the pre-allocated counter */
10468c2ecf20Sopenharmony_ci	val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
10478c2ecf20Sopenharmony_ci	val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
10488c2ecf20Sopenharmony_ci		CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
10498c2ecf20Sopenharmony_ci	val |= CCN_CONFIG_EVENT(event->attr.config) <<
10508c2ecf20Sopenharmony_ci		CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
10518c2ecf20Sopenharmony_ci	writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
10528c2ecf20Sopenharmony_ci}
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_event_config(struct perf_event *event)
10558c2ecf20Sopenharmony_ci{
10568c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
10578c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
10588c2ecf20Sopenharmony_ci	u32 xp, offset, val;
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	/* Cycle counter requires no setup */
10618c2ecf20Sopenharmony_ci	if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
10628c2ecf20Sopenharmony_ci		return;
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
10658c2ecf20Sopenharmony_ci		xp = CCN_CONFIG_XP(event->attr.config);
10668c2ecf20Sopenharmony_ci	else
10678c2ecf20Sopenharmony_ci		xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	spin_lock(&ccn->dt.config_lock);
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	/* Set the DT bus "distance" register */
10728c2ecf20Sopenharmony_ci	offset = (hw->idx / 4) * 4;
10738c2ecf20Sopenharmony_ci	val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
10748c2ecf20Sopenharmony_ci	val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
10758c2ecf20Sopenharmony_ci			CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
10768c2ecf20Sopenharmony_ci	val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
10778c2ecf20Sopenharmony_ci	writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
10808c2ecf20Sopenharmony_ci		if (CCN_CONFIG_EVENT(event->attr.config) ==
10818c2ecf20Sopenharmony_ci				CCN_EVENT_WATCHPOINT)
10828c2ecf20Sopenharmony_ci			arm_ccn_pmu_xp_watchpoint_config(event);
10838c2ecf20Sopenharmony_ci		else
10848c2ecf20Sopenharmony_ci			arm_ccn_pmu_xp_event_config(event);
10858c2ecf20Sopenharmony_ci	} else {
10868c2ecf20Sopenharmony_ci		arm_ccn_pmu_node_event_config(event);
10878c2ecf20Sopenharmony_ci	}
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	spin_unlock(&ccn->dt.config_lock);
10908c2ecf20Sopenharmony_ci}
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_cistatic int arm_ccn_pmu_active_counters(struct arm_ccn *ccn)
10938c2ecf20Sopenharmony_ci{
10948c2ecf20Sopenharmony_ci	return bitmap_weight(ccn->dt.pmu_counters_mask,
10958c2ecf20Sopenharmony_ci			     CCN_NUM_PMU_EVENT_COUNTERS + 1);
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_cistatic int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
10998c2ecf20Sopenharmony_ci{
11008c2ecf20Sopenharmony_ci	int err;
11018c2ecf20Sopenharmony_ci	struct hw_perf_event *hw = &event->hw;
11028c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	err = arm_ccn_pmu_event_alloc(event);
11058c2ecf20Sopenharmony_ci	if (err)
11068c2ecf20Sopenharmony_ci		return err;
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	/*
11098c2ecf20Sopenharmony_ci	 * Pin the timer, so that the overflows are handled by the chosen
11108c2ecf20Sopenharmony_ci	 * event->cpu (this is the same one as presented in "cpumask"
11118c2ecf20Sopenharmony_ci	 * attribute).
11128c2ecf20Sopenharmony_ci	 */
11138c2ecf20Sopenharmony_ci	if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 1)
11148c2ecf20Sopenharmony_ci		hrtimer_start(&ccn->dt.hrtimer, arm_ccn_pmu_timer_period(),
11158c2ecf20Sopenharmony_ci			      HRTIMER_MODE_REL_PINNED);
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	arm_ccn_pmu_event_config(event);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	hw->state = PERF_HES_STOPPED;
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	if (flags & PERF_EF_START)
11228c2ecf20Sopenharmony_ci		arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	return 0;
11258c2ecf20Sopenharmony_ci}
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
11288c2ecf20Sopenharmony_ci{
11298c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci	arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	arm_ccn_pmu_event_release(event);
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 0)
11368c2ecf20Sopenharmony_ci		hrtimer_cancel(&ccn->dt.hrtimer);
11378c2ecf20Sopenharmony_ci}
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_event_read(struct perf_event *event)
11408c2ecf20Sopenharmony_ci{
11418c2ecf20Sopenharmony_ci	arm_ccn_pmu_event_update(event);
11428c2ecf20Sopenharmony_ci}
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_enable(struct pmu *pmu)
11458c2ecf20Sopenharmony_ci{
11468c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
11498c2ecf20Sopenharmony_ci	val |= CCN_DT_PMCR__PMU_EN;
11508c2ecf20Sopenharmony_ci	writel(val, ccn->dt.base + CCN_DT_PMCR);
11518c2ecf20Sopenharmony_ci}
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_disable(struct pmu *pmu)
11548c2ecf20Sopenharmony_ci{
11558c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
11588c2ecf20Sopenharmony_ci	val &= ~CCN_DT_PMCR__PMU_EN;
11598c2ecf20Sopenharmony_ci	writel(val, ccn->dt.base + CCN_DT_PMCR);
11608c2ecf20Sopenharmony_ci}
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_cistatic irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
11638c2ecf20Sopenharmony_ci{
11648c2ecf20Sopenharmony_ci	u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
11658c2ecf20Sopenharmony_ci	int idx;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	if (!pmovsr)
11688c2ecf20Sopenharmony_ci		return IRQ_NONE;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
11758c2ecf20Sopenharmony_ci		struct perf_event *event = dt->pmu_counters[idx].event;
11768c2ecf20Sopenharmony_ci		int overflowed = pmovsr & BIT(idx);
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci		WARN_ON_ONCE(overflowed && !event &&
11798c2ecf20Sopenharmony_ci				idx != CCN_IDX_PMU_CYCLE_COUNTER);
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci		if (!event || !overflowed)
11828c2ecf20Sopenharmony_ci			continue;
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci		arm_ccn_pmu_event_update(event);
11858c2ecf20Sopenharmony_ci	}
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
11888c2ecf20Sopenharmony_ci}
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_cistatic enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
11918c2ecf20Sopenharmony_ci{
11928c2ecf20Sopenharmony_ci	struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
11938c2ecf20Sopenharmony_ci			hrtimer);
11948c2ecf20Sopenharmony_ci	unsigned long flags;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	local_irq_save(flags);
11978c2ecf20Sopenharmony_ci	arm_ccn_pmu_overflow_handler(dt);
11988c2ecf20Sopenharmony_ci	local_irq_restore(flags);
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
12018c2ecf20Sopenharmony_ci	return HRTIMER_RESTART;
12028c2ecf20Sopenharmony_ci}
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_cistatic int arm_ccn_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	struct arm_ccn_dt *dt = hlist_entry_safe(node, struct arm_ccn_dt, node);
12088c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
12098c2ecf20Sopenharmony_ci	unsigned int target;
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	if (cpu != dt->cpu)
12128c2ecf20Sopenharmony_ci		return 0;
12138c2ecf20Sopenharmony_ci	target = cpumask_any_but(cpu_online_mask, cpu);
12148c2ecf20Sopenharmony_ci	if (target >= nr_cpu_ids)
12158c2ecf20Sopenharmony_ci		return 0;
12168c2ecf20Sopenharmony_ci	perf_pmu_migrate_context(&dt->pmu, cpu, target);
12178c2ecf20Sopenharmony_ci	dt->cpu = target;
12188c2ecf20Sopenharmony_ci	if (ccn->irq)
12198c2ecf20Sopenharmony_ci		WARN_ON(irq_set_affinity_hint(ccn->irq, cpumask_of(dt->cpu)));
12208c2ecf20Sopenharmony_ci	return 0;
12218c2ecf20Sopenharmony_ci}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_cistatic DEFINE_IDA(arm_ccn_pmu_ida);
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_cistatic int arm_ccn_pmu_init(struct arm_ccn *ccn)
12268c2ecf20Sopenharmony_ci{
12278c2ecf20Sopenharmony_ci	int i;
12288c2ecf20Sopenharmony_ci	char *name;
12298c2ecf20Sopenharmony_ci	int err;
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	/* Initialize DT subsystem */
12328c2ecf20Sopenharmony_ci	ccn->dt.base = ccn->base + CCN_REGION_SIZE;
12338c2ecf20Sopenharmony_ci	spin_lock_init(&ccn->dt.config_lock);
12348c2ecf20Sopenharmony_ci	writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
12358c2ecf20Sopenharmony_ci	writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
12368c2ecf20Sopenharmony_ci	writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
12378c2ecf20Sopenharmony_ci			ccn->dt.base + CCN_DT_PMCR);
12388c2ecf20Sopenharmony_ci	writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
12398c2ecf20Sopenharmony_ci	for (i = 0; i < ccn->num_xps; i++) {
12408c2ecf20Sopenharmony_ci		writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
12418c2ecf20Sopenharmony_ci		writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
12428c2ecf20Sopenharmony_ci				CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
12438c2ecf20Sopenharmony_ci				(CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
12448c2ecf20Sopenharmony_ci				CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
12458c2ecf20Sopenharmony_ci				CCN_XP_DT_CONTROL__DT_ENABLE,
12468c2ecf20Sopenharmony_ci				ccn->xp[i].base + CCN_XP_DT_CONTROL);
12478c2ecf20Sopenharmony_ci	}
12488c2ecf20Sopenharmony_ci	ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
12498c2ecf20Sopenharmony_ci	ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
12508c2ecf20Sopenharmony_ci	ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
12518c2ecf20Sopenharmony_ci	ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
12528c2ecf20Sopenharmony_ci	ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
12538c2ecf20Sopenharmony_ci	ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
12548c2ecf20Sopenharmony_ci	ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
12558c2ecf20Sopenharmony_ci	ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	/* Get a convenient /sys/event_source/devices/ name */
12588c2ecf20Sopenharmony_ci	ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
12598c2ecf20Sopenharmony_ci	if (ccn->dt.id == 0) {
12608c2ecf20Sopenharmony_ci		name = "ccn";
12618c2ecf20Sopenharmony_ci	} else {
12628c2ecf20Sopenharmony_ci		name = devm_kasprintf(ccn->dev, GFP_KERNEL, "ccn_%d",
12638c2ecf20Sopenharmony_ci				      ccn->dt.id);
12648c2ecf20Sopenharmony_ci		if (!name) {
12658c2ecf20Sopenharmony_ci			err = -ENOMEM;
12668c2ecf20Sopenharmony_ci			goto error_choose_name;
12678c2ecf20Sopenharmony_ci		}
12688c2ecf20Sopenharmony_ci	}
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	/* Perf driver registration */
12718c2ecf20Sopenharmony_ci	ccn->dt.pmu = (struct pmu) {
12728c2ecf20Sopenharmony_ci		.module = THIS_MODULE,
12738c2ecf20Sopenharmony_ci		.attr_groups = arm_ccn_pmu_attr_groups,
12748c2ecf20Sopenharmony_ci		.task_ctx_nr = perf_invalid_context,
12758c2ecf20Sopenharmony_ci		.event_init = arm_ccn_pmu_event_init,
12768c2ecf20Sopenharmony_ci		.add = arm_ccn_pmu_event_add,
12778c2ecf20Sopenharmony_ci		.del = arm_ccn_pmu_event_del,
12788c2ecf20Sopenharmony_ci		.start = arm_ccn_pmu_event_start,
12798c2ecf20Sopenharmony_ci		.stop = arm_ccn_pmu_event_stop,
12808c2ecf20Sopenharmony_ci		.read = arm_ccn_pmu_event_read,
12818c2ecf20Sopenharmony_ci		.pmu_enable = arm_ccn_pmu_enable,
12828c2ecf20Sopenharmony_ci		.pmu_disable = arm_ccn_pmu_disable,
12838c2ecf20Sopenharmony_ci		.capabilities = PERF_PMU_CAP_NO_EXCLUDE,
12848c2ecf20Sopenharmony_ci	};
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	/* No overflow interrupt? Have to use a timer instead. */
12878c2ecf20Sopenharmony_ci	if (!ccn->irq) {
12888c2ecf20Sopenharmony_ci		dev_info(ccn->dev, "No access to interrupts, using timer.\n");
12898c2ecf20Sopenharmony_ci		hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
12908c2ecf20Sopenharmony_ci				HRTIMER_MODE_REL);
12918c2ecf20Sopenharmony_ci		ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
12928c2ecf20Sopenharmony_ci	}
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci	/* Pick one CPU which we will use to collect data from CCN... */
12958c2ecf20Sopenharmony_ci	ccn->dt.cpu = raw_smp_processor_id();
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci	/* Also make sure that the overflow interrupt is handled by this CPU */
12988c2ecf20Sopenharmony_ci	if (ccn->irq) {
12998c2ecf20Sopenharmony_ci		err = irq_set_affinity_hint(ccn->irq, cpumask_of(ccn->dt.cpu));
13008c2ecf20Sopenharmony_ci		if (err) {
13018c2ecf20Sopenharmony_ci			dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
13028c2ecf20Sopenharmony_ci			goto error_set_affinity;
13038c2ecf20Sopenharmony_ci		}
13048c2ecf20Sopenharmony_ci	}
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13078c2ecf20Sopenharmony_ci					 &ccn->dt.node);
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	err = perf_pmu_register(&ccn->dt.pmu, name, -1);
13108c2ecf20Sopenharmony_ci	if (err)
13118c2ecf20Sopenharmony_ci		goto error_pmu_register;
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	return 0;
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_cierror_pmu_register:
13168c2ecf20Sopenharmony_ci	cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13178c2ecf20Sopenharmony_ci					    &ccn->dt.node);
13188c2ecf20Sopenharmony_cierror_set_affinity:
13198c2ecf20Sopenharmony_cierror_choose_name:
13208c2ecf20Sopenharmony_ci	ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
13218c2ecf20Sopenharmony_ci	for (i = 0; i < ccn->num_xps; i++)
13228c2ecf20Sopenharmony_ci		writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
13238c2ecf20Sopenharmony_ci	writel(0, ccn->dt.base + CCN_DT_PMCR);
13248c2ecf20Sopenharmony_ci	return err;
13258c2ecf20Sopenharmony_ci}
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_cistatic void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
13288c2ecf20Sopenharmony_ci{
13298c2ecf20Sopenharmony_ci	int i;
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
13328c2ecf20Sopenharmony_ci					    &ccn->dt.node);
13338c2ecf20Sopenharmony_ci	if (ccn->irq)
13348c2ecf20Sopenharmony_ci		irq_set_affinity_hint(ccn->irq, NULL);
13358c2ecf20Sopenharmony_ci	for (i = 0; i < ccn->num_xps; i++)
13368c2ecf20Sopenharmony_ci		writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
13378c2ecf20Sopenharmony_ci	writel(0, ccn->dt.base + CCN_DT_PMCR);
13388c2ecf20Sopenharmony_ci	perf_pmu_unregister(&ccn->dt.pmu);
13398c2ecf20Sopenharmony_ci	ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
13408c2ecf20Sopenharmony_ci}
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_cistatic int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
13438c2ecf20Sopenharmony_ci		int (*callback)(struct arm_ccn *ccn, int region,
13448c2ecf20Sopenharmony_ci		void __iomem *base, u32 type, u32 id))
13458c2ecf20Sopenharmony_ci{
13468c2ecf20Sopenharmony_ci	int region;
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	for (region = 0; region < CCN_NUM_REGIONS; region++) {
13498c2ecf20Sopenharmony_ci		u32 val, type, id;
13508c2ecf20Sopenharmony_ci		void __iomem *base;
13518c2ecf20Sopenharmony_ci		int err;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci		val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
13548c2ecf20Sopenharmony_ci				4 * (region / 32));
13558c2ecf20Sopenharmony_ci		if (!(val & (1 << (region % 32))))
13568c2ecf20Sopenharmony_ci			continue;
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci		base = ccn->base + region * CCN_REGION_SIZE;
13598c2ecf20Sopenharmony_ci		val = readl(base + CCN_ALL_OLY_ID);
13608c2ecf20Sopenharmony_ci		type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
13618c2ecf20Sopenharmony_ci				CCN_ALL_OLY_ID__OLY_ID__MASK;
13628c2ecf20Sopenharmony_ci		id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
13638c2ecf20Sopenharmony_ci				CCN_ALL_OLY_ID__NODE_ID__MASK;
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci		err = callback(ccn, region, base, type, id);
13668c2ecf20Sopenharmony_ci		if (err)
13678c2ecf20Sopenharmony_ci			return err;
13688c2ecf20Sopenharmony_ci	}
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	return 0;
13718c2ecf20Sopenharmony_ci}
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_cistatic int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
13748c2ecf20Sopenharmony_ci		void __iomem *base, u32 type, u32 id)
13758c2ecf20Sopenharmony_ci{
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	if (type == CCN_TYPE_XP && id >= ccn->num_xps)
13788c2ecf20Sopenharmony_ci		ccn->num_xps = id + 1;
13798c2ecf20Sopenharmony_ci	else if (id >= ccn->num_nodes)
13808c2ecf20Sopenharmony_ci		ccn->num_nodes = id + 1;
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	return 0;
13838c2ecf20Sopenharmony_ci}
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_cistatic int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
13868c2ecf20Sopenharmony_ci		void __iomem *base, u32 type, u32 id)
13878c2ecf20Sopenharmony_ci{
13888c2ecf20Sopenharmony_ci	struct arm_ccn_component *component;
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci	dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_ci	switch (type) {
13938c2ecf20Sopenharmony_ci	case CCN_TYPE_MN:
13948c2ecf20Sopenharmony_ci		ccn->mn_id = id;
13958c2ecf20Sopenharmony_ci		return 0;
13968c2ecf20Sopenharmony_ci	case CCN_TYPE_DT:
13978c2ecf20Sopenharmony_ci		return 0;
13988c2ecf20Sopenharmony_ci	case CCN_TYPE_XP:
13998c2ecf20Sopenharmony_ci		component = &ccn->xp[id];
14008c2ecf20Sopenharmony_ci		break;
14018c2ecf20Sopenharmony_ci	case CCN_TYPE_SBSX:
14028c2ecf20Sopenharmony_ci		ccn->sbsx_present = 1;
14038c2ecf20Sopenharmony_ci		component = &ccn->node[id];
14048c2ecf20Sopenharmony_ci		break;
14058c2ecf20Sopenharmony_ci	case CCN_TYPE_SBAS:
14068c2ecf20Sopenharmony_ci		ccn->sbas_present = 1;
14078c2ecf20Sopenharmony_ci		fallthrough;
14088c2ecf20Sopenharmony_ci	default:
14098c2ecf20Sopenharmony_ci		component = &ccn->node[id];
14108c2ecf20Sopenharmony_ci		break;
14118c2ecf20Sopenharmony_ci	}
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	component->base = base;
14148c2ecf20Sopenharmony_ci	component->type = type;
14158c2ecf20Sopenharmony_ci
14168c2ecf20Sopenharmony_ci	return 0;
14178c2ecf20Sopenharmony_ci}
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_cistatic irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
14218c2ecf20Sopenharmony_ci		const u32 *err_sig_val)
14228c2ecf20Sopenharmony_ci{
14238c2ecf20Sopenharmony_ci	/* This should be really handled by firmware... */
14248c2ecf20Sopenharmony_ci	dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
14258c2ecf20Sopenharmony_ci			err_sig_val[5], err_sig_val[4], err_sig_val[3],
14268c2ecf20Sopenharmony_ci			err_sig_val[2], err_sig_val[1], err_sig_val[0]);
14278c2ecf20Sopenharmony_ci	dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
14288c2ecf20Sopenharmony_ci	writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
14298c2ecf20Sopenharmony_ci			ccn->base + CCN_MN_ERRINT_STATUS);
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
14328c2ecf20Sopenharmony_ci}
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_cistatic irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
14368c2ecf20Sopenharmony_ci{
14378c2ecf20Sopenharmony_ci	irqreturn_t res = IRQ_NONE;
14388c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = dev_id;
14398c2ecf20Sopenharmony_ci	u32 err_sig_val[6];
14408c2ecf20Sopenharmony_ci	u32 err_or;
14418c2ecf20Sopenharmony_ci	int i;
14428c2ecf20Sopenharmony_ci
14438c2ecf20Sopenharmony_ci	/* PMU overflow is a special case */
14448c2ecf20Sopenharmony_ci	err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
14458c2ecf20Sopenharmony_ci	if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
14468c2ecf20Sopenharmony_ci		err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
14478c2ecf20Sopenharmony_ci		res = arm_ccn_pmu_overflow_handler(&ccn->dt);
14488c2ecf20Sopenharmony_ci	}
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	/* Have to read all err_sig_vals to clear them */
14518c2ecf20Sopenharmony_ci	for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
14528c2ecf20Sopenharmony_ci		err_sig_val[i] = readl(ccn->base +
14538c2ecf20Sopenharmony_ci				CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
14548c2ecf20Sopenharmony_ci		err_or |= err_sig_val[i];
14558c2ecf20Sopenharmony_ci	}
14568c2ecf20Sopenharmony_ci	if (err_or)
14578c2ecf20Sopenharmony_ci		res |= arm_ccn_error_handler(ccn, err_sig_val);
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	if (res != IRQ_NONE)
14608c2ecf20Sopenharmony_ci		writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
14618c2ecf20Sopenharmony_ci				ccn->base + CCN_MN_ERRINT_STATUS);
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	return res;
14648c2ecf20Sopenharmony_ci}
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_cistatic int arm_ccn_probe(struct platform_device *pdev)
14688c2ecf20Sopenharmony_ci{
14698c2ecf20Sopenharmony_ci	struct arm_ccn *ccn;
14708c2ecf20Sopenharmony_ci	struct resource *res;
14718c2ecf20Sopenharmony_ci	unsigned int irq;
14728c2ecf20Sopenharmony_ci	int err;
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci	ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
14758c2ecf20Sopenharmony_ci	if (!ccn)
14768c2ecf20Sopenharmony_ci		return -ENOMEM;
14778c2ecf20Sopenharmony_ci	ccn->dev = &pdev->dev;
14788c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ccn);
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	ccn->base = devm_platform_ioremap_resource(pdev, 0);
14818c2ecf20Sopenharmony_ci	if (IS_ERR(ccn->base))
14828c2ecf20Sopenharmony_ci		return PTR_ERR(ccn->base);
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
14858c2ecf20Sopenharmony_ci	if (!res)
14868c2ecf20Sopenharmony_ci		return -EINVAL;
14878c2ecf20Sopenharmony_ci	irq = res->start;
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	/* Check if we can use the interrupt */
14908c2ecf20Sopenharmony_ci	writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
14918c2ecf20Sopenharmony_ci			ccn->base + CCN_MN_ERRINT_STATUS);
14928c2ecf20Sopenharmony_ci	if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
14938c2ecf20Sopenharmony_ci			CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
14948c2ecf20Sopenharmony_ci		/* Can set 'disable' bits, so can acknowledge interrupts */
14958c2ecf20Sopenharmony_ci		writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
14968c2ecf20Sopenharmony_ci				ccn->base + CCN_MN_ERRINT_STATUS);
14978c2ecf20Sopenharmony_ci		err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler,
14988c2ecf20Sopenharmony_ci				       IRQF_NOBALANCING | IRQF_NO_THREAD,
14998c2ecf20Sopenharmony_ci				       dev_name(ccn->dev), ccn);
15008c2ecf20Sopenharmony_ci		if (err)
15018c2ecf20Sopenharmony_ci			return err;
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci		ccn->irq = irq;
15048c2ecf20Sopenharmony_ci	}
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci	/* Build topology */
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci	err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
15108c2ecf20Sopenharmony_ci	if (err)
15118c2ecf20Sopenharmony_ci		return err;
15128c2ecf20Sopenharmony_ci
15138c2ecf20Sopenharmony_ci	ccn->node = devm_kcalloc(ccn->dev, ccn->num_nodes, sizeof(*ccn->node),
15148c2ecf20Sopenharmony_ci				 GFP_KERNEL);
15158c2ecf20Sopenharmony_ci	ccn->xp = devm_kcalloc(ccn->dev, ccn->num_xps, sizeof(*ccn->node),
15168c2ecf20Sopenharmony_ci			       GFP_KERNEL);
15178c2ecf20Sopenharmony_ci	if (!ccn->node || !ccn->xp)
15188c2ecf20Sopenharmony_ci		return -ENOMEM;
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_ci	err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
15218c2ecf20Sopenharmony_ci	if (err)
15228c2ecf20Sopenharmony_ci		return err;
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	return arm_ccn_pmu_init(ccn);
15258c2ecf20Sopenharmony_ci}
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_cistatic int arm_ccn_remove(struct platform_device *pdev)
15288c2ecf20Sopenharmony_ci{
15298c2ecf20Sopenharmony_ci	struct arm_ccn *ccn = platform_get_drvdata(pdev);
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci	arm_ccn_pmu_cleanup(ccn);
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci	return 0;
15348c2ecf20Sopenharmony_ci}
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_cistatic const struct of_device_id arm_ccn_match[] = {
15378c2ecf20Sopenharmony_ci	{ .compatible = "arm,ccn-502", },
15388c2ecf20Sopenharmony_ci	{ .compatible = "arm,ccn-504", },
15398c2ecf20Sopenharmony_ci	{ .compatible = "arm,ccn-512", },
15408c2ecf20Sopenharmony_ci	{},
15418c2ecf20Sopenharmony_ci};
15428c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, arm_ccn_match);
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_cistatic struct platform_driver arm_ccn_driver = {
15458c2ecf20Sopenharmony_ci	.driver = {
15468c2ecf20Sopenharmony_ci		.name = "arm-ccn",
15478c2ecf20Sopenharmony_ci		.of_match_table = arm_ccn_match,
15488c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
15498c2ecf20Sopenharmony_ci	},
15508c2ecf20Sopenharmony_ci	.probe = arm_ccn_probe,
15518c2ecf20Sopenharmony_ci	.remove = arm_ccn_remove,
15528c2ecf20Sopenharmony_ci};
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_cistatic int __init arm_ccn_init(void)
15558c2ecf20Sopenharmony_ci{
15568c2ecf20Sopenharmony_ci	int i, ret;
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE,
15598c2ecf20Sopenharmony_ci				      "perf/arm/ccn:online", NULL,
15608c2ecf20Sopenharmony_ci				      arm_ccn_pmu_offline_cpu);
15618c2ecf20Sopenharmony_ci	if (ret)
15628c2ecf20Sopenharmony_ci		return ret;
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
15658c2ecf20Sopenharmony_ci		arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	ret = platform_driver_register(&arm_ccn_driver);
15688c2ecf20Sopenharmony_ci	if (ret)
15698c2ecf20Sopenharmony_ci		cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
15708c2ecf20Sopenharmony_ci	return ret;
15718c2ecf20Sopenharmony_ci}
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_cistatic void __exit arm_ccn_exit(void)
15748c2ecf20Sopenharmony_ci{
15758c2ecf20Sopenharmony_ci	platform_driver_unregister(&arm_ccn_driver);
15768c2ecf20Sopenharmony_ci	cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
15778c2ecf20Sopenharmony_ci}
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_cimodule_init(arm_ccn_init);
15808c2ecf20Sopenharmony_cimodule_exit(arm_ccn_exit);
15818c2ecf20Sopenharmony_ci
15828c2ecf20Sopenharmony_ciMODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
15838c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1584