xref: /openbmc/linux/drivers/perf/arm-cci.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
13de6be7aSRobin Murphy // SPDX-License-Identifier: GPL-2.0
23de6be7aSRobin Murphy // CCI Cache Coherent Interconnect PMU driver
33de6be7aSRobin Murphy // Copyright (C) 2013-2018 Arm Ltd.
43de6be7aSRobin Murphy // Author: Punit Agrawal <punit.agrawal@arm.com>, Suzuki Poulose <suzuki.poulose@arm.com>
53de6be7aSRobin Murphy 
63de6be7aSRobin Murphy #include <linux/arm-cci.h>
73de6be7aSRobin Murphy #include <linux/io.h>
83de6be7aSRobin Murphy #include <linux/interrupt.h>
93de6be7aSRobin Murphy #include <linux/module.h>
10*918dc87bSRob Herring #include <linux/of.h>
113de6be7aSRobin Murphy #include <linux/perf_event.h>
123de6be7aSRobin Murphy #include <linux/platform_device.h>
133de6be7aSRobin Murphy #include <linux/slab.h>
143de6be7aSRobin Murphy #include <linux/spinlock.h>
153de6be7aSRobin Murphy 
163de6be7aSRobin Murphy #define DRIVER_NAME		"ARM-CCI PMU"
173de6be7aSRobin Murphy 
183de6be7aSRobin Murphy #define CCI_PMCR		0x0100
193de6be7aSRobin Murphy #define CCI_PID2		0x0fe8
203de6be7aSRobin Murphy 
213de6be7aSRobin Murphy #define CCI_PMCR_CEN		0x00000001
223de6be7aSRobin Murphy #define CCI_PMCR_NCNT_MASK	0x0000f800
233de6be7aSRobin Murphy #define CCI_PMCR_NCNT_SHIFT	11
243de6be7aSRobin Murphy 
253de6be7aSRobin Murphy #define CCI_PID2_REV_MASK	0xf0
263de6be7aSRobin Murphy #define CCI_PID2_REV_SHIFT	4
273de6be7aSRobin Murphy 
283de6be7aSRobin Murphy #define CCI_PMU_EVT_SEL		0x000
293de6be7aSRobin Murphy #define CCI_PMU_CNTR		0x004
303de6be7aSRobin Murphy #define CCI_PMU_CNTR_CTRL	0x008
313de6be7aSRobin Murphy #define CCI_PMU_OVRFLW		0x00c
323de6be7aSRobin Murphy 
333de6be7aSRobin Murphy #define CCI_PMU_OVRFLW_FLAG	1
343de6be7aSRobin Murphy 
353de6be7aSRobin Murphy #define CCI_PMU_CNTR_SIZE(model)	((model)->cntr_size)
363de6be7aSRobin Murphy #define CCI_PMU_CNTR_BASE(model, idx)	((idx) * CCI_PMU_CNTR_SIZE(model))
373de6be7aSRobin Murphy #define CCI_PMU_CNTR_MASK		((1ULL << 32) - 1)
383de6be7aSRobin Murphy #define CCI_PMU_CNTR_LAST(cci_pmu)	(cci_pmu->num_cntrs - 1)
393de6be7aSRobin Murphy 
403de6be7aSRobin Murphy #define CCI_PMU_MAX_HW_CNTRS(model) \
413de6be7aSRobin Murphy 	((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
423de6be7aSRobin Murphy 
433de6be7aSRobin Murphy /* Types of interfaces that can generate events */
443de6be7aSRobin Murphy enum {
453de6be7aSRobin Murphy 	CCI_IF_SLAVE,
463de6be7aSRobin Murphy 	CCI_IF_MASTER,
473de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI5xx_PMU
483de6be7aSRobin Murphy 	CCI_IF_GLOBAL,
493de6be7aSRobin Murphy #endif
503de6be7aSRobin Murphy 	CCI_IF_MAX,
513de6be7aSRobin Murphy };
523de6be7aSRobin Murphy 
531201a5a2SKees Cook #define NUM_HW_CNTRS_CII_4XX	4
541201a5a2SKees Cook #define NUM_HW_CNTRS_CII_5XX	8
551201a5a2SKees Cook #define NUM_HW_CNTRS_MAX	NUM_HW_CNTRS_CII_5XX
561201a5a2SKees Cook 
571201a5a2SKees Cook #define FIXED_HW_CNTRS_CII_4XX	1
581201a5a2SKees Cook #define FIXED_HW_CNTRS_CII_5XX	0
591201a5a2SKees Cook #define FIXED_HW_CNTRS_MAX	FIXED_HW_CNTRS_CII_4XX
601201a5a2SKees Cook 
611201a5a2SKees Cook #define HW_CNTRS_MAX		(NUM_HW_CNTRS_MAX + FIXED_HW_CNTRS_MAX)
621201a5a2SKees Cook 
633de6be7aSRobin Murphy struct event_range {
643de6be7aSRobin Murphy 	u32 min;
653de6be7aSRobin Murphy 	u32 max;
663de6be7aSRobin Murphy };
673de6be7aSRobin Murphy 
683de6be7aSRobin Murphy struct cci_pmu_hw_events {
693de6be7aSRobin Murphy 	struct perf_event **events;
703de6be7aSRobin Murphy 	unsigned long *used_mask;
713de6be7aSRobin Murphy 	raw_spinlock_t pmu_lock;
723de6be7aSRobin Murphy };
733de6be7aSRobin Murphy 
743de6be7aSRobin Murphy struct cci_pmu;
753de6be7aSRobin Murphy /*
763de6be7aSRobin Murphy  * struct cci_pmu_model:
773de6be7aSRobin Murphy  * @fixed_hw_cntrs - Number of fixed event counters
783de6be7aSRobin Murphy  * @num_hw_cntrs - Maximum number of programmable event counters
793de6be7aSRobin Murphy  * @cntr_size - Size of an event counter mapping
803de6be7aSRobin Murphy  */
813de6be7aSRobin Murphy struct cci_pmu_model {
823de6be7aSRobin Murphy 	char *name;
833de6be7aSRobin Murphy 	u32 fixed_hw_cntrs;
843de6be7aSRobin Murphy 	u32 num_hw_cntrs;
853de6be7aSRobin Murphy 	u32 cntr_size;
863de6be7aSRobin Murphy 	struct attribute **format_attrs;
873de6be7aSRobin Murphy 	struct attribute **event_attrs;
883de6be7aSRobin Murphy 	struct event_range event_ranges[CCI_IF_MAX];
893de6be7aSRobin Murphy 	int (*validate_hw_event)(struct cci_pmu *, unsigned long);
903de6be7aSRobin Murphy 	int (*get_event_idx)(struct cci_pmu *, struct cci_pmu_hw_events *, unsigned long);
913de6be7aSRobin Murphy 	void (*write_counters)(struct cci_pmu *, unsigned long *);
923de6be7aSRobin Murphy };
933de6be7aSRobin Murphy 
943de6be7aSRobin Murphy static struct cci_pmu_model cci_pmu_models[];
953de6be7aSRobin Murphy 
963de6be7aSRobin Murphy struct cci_pmu {
973de6be7aSRobin Murphy 	void __iomem *base;
98e9c112c9SRobin Murphy 	void __iomem *ctrl_base;
993de6be7aSRobin Murphy 	struct pmu pmu;
10003057f26SRobin Murphy 	int cpu;
1013de6be7aSRobin Murphy 	int nr_irqs;
1023de6be7aSRobin Murphy 	int *irqs;
1033de6be7aSRobin Murphy 	unsigned long active_irqs;
1043de6be7aSRobin Murphy 	const struct cci_pmu_model *model;
1053de6be7aSRobin Murphy 	struct cci_pmu_hw_events hw_events;
1063de6be7aSRobin Murphy 	struct platform_device *plat_device;
1073de6be7aSRobin Murphy 	int num_cntrs;
1083de6be7aSRobin Murphy 	atomic_t active_events;
1093de6be7aSRobin Murphy 	struct mutex reserve_mutex;
1103de6be7aSRobin Murphy };
1113de6be7aSRobin Murphy 
1123de6be7aSRobin Murphy #define to_cci_pmu(c)	(container_of(c, struct cci_pmu, pmu))
1133de6be7aSRobin Murphy 
11403057f26SRobin Murphy static struct cci_pmu *g_cci_pmu;
11503057f26SRobin Murphy 
1163de6be7aSRobin Murphy enum cci_models {
1173de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI400_PMU
1183de6be7aSRobin Murphy 	CCI400_R0,
1193de6be7aSRobin Murphy 	CCI400_R1,
1203de6be7aSRobin Murphy #endif
1213de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI5xx_PMU
1223de6be7aSRobin Murphy 	CCI500_R0,
1233de6be7aSRobin Murphy 	CCI550_R0,
1243de6be7aSRobin Murphy #endif
1253de6be7aSRobin Murphy 	CCI_MODEL_MAX
1263de6be7aSRobin Murphy };
1273de6be7aSRobin Murphy 
1283de6be7aSRobin Murphy static void pmu_write_counters(struct cci_pmu *cci_pmu,
1293de6be7aSRobin Murphy 				 unsigned long *mask);
130984e9cf1SArnd Bergmann static ssize_t __maybe_unused cci_pmu_format_show(struct device *dev,
1313de6be7aSRobin Murphy 			struct device_attribute *attr, char *buf);
132984e9cf1SArnd Bergmann static ssize_t __maybe_unused cci_pmu_event_show(struct device *dev,
1333de6be7aSRobin Murphy 			struct device_attribute *attr, char *buf);
1343de6be7aSRobin Murphy 
1353de6be7aSRobin Murphy #define CCI_EXT_ATTR_ENTRY(_name, _func, _config) 				\
1363de6be7aSRobin Murphy 	&((struct dev_ext_attribute[]) {					\
1373de6be7aSRobin Murphy 		{ __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config }	\
1383de6be7aSRobin Murphy 	})[0].attr.attr
1393de6be7aSRobin Murphy 
1403de6be7aSRobin Murphy #define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
1413de6be7aSRobin Murphy 	CCI_EXT_ATTR_ENTRY(_name, cci_pmu_format_show, (char *)_config)
1423de6be7aSRobin Murphy #define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
1433de6be7aSRobin Murphy 	CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
1443de6be7aSRobin Murphy 
1453de6be7aSRobin Murphy /* CCI400 PMU Specific definitions */
1463de6be7aSRobin Murphy 
1473de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI400_PMU
1483de6be7aSRobin Murphy 
1493de6be7aSRobin Murphy /* Port ids */
1503de6be7aSRobin Murphy #define CCI400_PORT_S0		0
1513de6be7aSRobin Murphy #define CCI400_PORT_S1		1
1523de6be7aSRobin Murphy #define CCI400_PORT_S2		2
1533de6be7aSRobin Murphy #define CCI400_PORT_S3		3
1543de6be7aSRobin Murphy #define CCI400_PORT_S4		4
1553de6be7aSRobin Murphy #define CCI400_PORT_M0		5
1563de6be7aSRobin Murphy #define CCI400_PORT_M1		6
1573de6be7aSRobin Murphy #define CCI400_PORT_M2		7
1583de6be7aSRobin Murphy 
1593de6be7aSRobin Murphy #define CCI400_R1_PX		5
1603de6be7aSRobin Murphy 
1613de6be7aSRobin Murphy /*
1623de6be7aSRobin Murphy  * Instead of an event id to monitor CCI cycles, a dedicated counter is
1633de6be7aSRobin Murphy  * provided. Use 0xff to represent CCI cycles and hope that no future revisions
1643de6be7aSRobin Murphy  * make use of this event in hardware.
1653de6be7aSRobin Murphy  */
1663de6be7aSRobin Murphy enum cci400_perf_events {
1673de6be7aSRobin Murphy 	CCI400_PMU_CYCLES = 0xff
1683de6be7aSRobin Murphy };
1693de6be7aSRobin Murphy 
1703de6be7aSRobin Murphy #define CCI400_PMU_CYCLE_CNTR_IDX	0
1713de6be7aSRobin Murphy #define CCI400_PMU_CNTR0_IDX		1
1723de6be7aSRobin Murphy 
1733de6be7aSRobin Murphy /*
1743de6be7aSRobin Murphy  * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
1753de6be7aSRobin Murphy  * ports and bits 4:0 are event codes. There are different event codes
1763de6be7aSRobin Murphy  * associated with each port type.
1773de6be7aSRobin Murphy  *
1783de6be7aSRobin Murphy  * Additionally, the range of events associated with the port types changed
1793de6be7aSRobin Murphy  * between Rev0 and Rev1.
1803de6be7aSRobin Murphy  *
1813de6be7aSRobin Murphy  * The constants below define the range of valid codes for each port type for
1823de6be7aSRobin Murphy  * the different revisions and are used to validate the event to be monitored.
1833de6be7aSRobin Murphy  */
1843de6be7aSRobin Murphy 
1853de6be7aSRobin Murphy #define CCI400_PMU_EVENT_MASK		0xffUL
1863de6be7aSRobin Murphy #define CCI400_PMU_EVENT_SOURCE_SHIFT	5
1873de6be7aSRobin Murphy #define CCI400_PMU_EVENT_SOURCE_MASK	0x7
1883de6be7aSRobin Murphy #define CCI400_PMU_EVENT_CODE_SHIFT	0
1893de6be7aSRobin Murphy #define CCI400_PMU_EVENT_CODE_MASK	0x1f
1903de6be7aSRobin Murphy #define CCI400_PMU_EVENT_SOURCE(event) \
1913de6be7aSRobin Murphy 	((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
1923de6be7aSRobin Murphy 			CCI400_PMU_EVENT_SOURCE_MASK)
1933de6be7aSRobin Murphy #define CCI400_PMU_EVENT_CODE(event) \
1943de6be7aSRobin Murphy 	((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
1953de6be7aSRobin Murphy 
1963de6be7aSRobin Murphy #define CCI400_R0_SLAVE_PORT_MIN_EV	0x00
1973de6be7aSRobin Murphy #define CCI400_R0_SLAVE_PORT_MAX_EV	0x13
1983de6be7aSRobin Murphy #define CCI400_R0_MASTER_PORT_MIN_EV	0x14
1993de6be7aSRobin Murphy #define CCI400_R0_MASTER_PORT_MAX_EV	0x1a
2003de6be7aSRobin Murphy 
2013de6be7aSRobin Murphy #define CCI400_R1_SLAVE_PORT_MIN_EV	0x00
2023de6be7aSRobin Murphy #define CCI400_R1_SLAVE_PORT_MAX_EV	0x14
2033de6be7aSRobin Murphy #define CCI400_R1_MASTER_PORT_MIN_EV	0x00
2043de6be7aSRobin Murphy #define CCI400_R1_MASTER_PORT_MAX_EV	0x11
2053de6be7aSRobin Murphy 
2063de6be7aSRobin Murphy #define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
2073de6be7aSRobin Murphy 	CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
2083de6be7aSRobin Murphy 					(unsigned long)_config)
2093de6be7aSRobin Murphy 
2103de6be7aSRobin Murphy static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
2113de6be7aSRobin Murphy 			struct device_attribute *attr, char *buf);
2123de6be7aSRobin Murphy 
2133de6be7aSRobin Murphy static struct attribute *cci400_pmu_format_attrs[] = {
2143de6be7aSRobin Murphy 	CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
2153de6be7aSRobin Murphy 	CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-7"),
2163de6be7aSRobin Murphy 	NULL
2173de6be7aSRobin Murphy };
2183de6be7aSRobin Murphy 
2193de6be7aSRobin Murphy static struct attribute *cci400_r0_pmu_event_attrs[] = {
2203de6be7aSRobin Murphy 	/* Slave events */
2213de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
2223de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
2233de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
2243de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
2253de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
2263de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
2273de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
2283de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
2293de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
2303de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
2313de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
2323de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
2333de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
2343de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
2353de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
2363de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
2373de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
2383de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
2393de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
2403de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
2413de6be7aSRobin Murphy 	/* Master events */
2423de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x14),
2433de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard, 0x15),
2443de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard, 0x16),
2453de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full, 0x17),
2463de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x18),
2473de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x19),
2483de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full, 0x1A),
2493de6be7aSRobin Murphy 	/* Special event for cycles counter */
2503de6be7aSRobin Murphy 	CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
2513de6be7aSRobin Murphy 	NULL
2523de6be7aSRobin Murphy };
2533de6be7aSRobin Murphy 
2543de6be7aSRobin Murphy static struct attribute *cci400_r1_pmu_event_attrs[] = {
2553de6be7aSRobin Murphy 	/* Slave events */
2563de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
2573de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
2583de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
2593de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
2603de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
2613de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
2623de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
2633de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
2643de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
2653de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
2663de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
2673de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
2683de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
2693de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
2703de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
2713de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
2723de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
2733de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
2743de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
2753de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
2763de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard, 0x14),
2773de6be7aSRobin Murphy 	/* Master events */
2783de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x0),
2793de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard, 0x1),
2803de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard, 0x2),
2813de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full, 0x3),
2823de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x4),
2833de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x5),
2843de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full, 0x6),
2853de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full, 0x7),
2863de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full, 0x8),
2873de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0, 0x9),
2883de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1, 0xA),
2893de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2, 0xB),
2903de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3, 0xC),
2913de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0, 0xD),
2923de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1, 0xE),
2933de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2, 0xF),
2943de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3, 0x10),
2953de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard, 0x11),
2963de6be7aSRobin Murphy 	/* Special event for cycles counter */
2973de6be7aSRobin Murphy 	CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
2983de6be7aSRobin Murphy 	NULL
2993de6be7aSRobin Murphy };
3003de6be7aSRobin Murphy 
cci400_pmu_cycle_event_show(struct device * dev,struct device_attribute * attr,char * buf)3013de6be7aSRobin Murphy static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
3023de6be7aSRobin Murphy 			struct device_attribute *attr, char *buf)
3033de6be7aSRobin Murphy {
3043de6be7aSRobin Murphy 	struct dev_ext_attribute *eattr = container_of(attr,
3053de6be7aSRobin Murphy 				struct dev_ext_attribute, attr);
306700a9cf0SZihao Tang 	return sysfs_emit(buf, "config=0x%lx\n", (unsigned long)eattr->var);
3073de6be7aSRobin Murphy }
3083de6be7aSRobin Murphy 
cci400_get_event_idx(struct cci_pmu * cci_pmu,struct cci_pmu_hw_events * hw,unsigned long cci_event)3093de6be7aSRobin Murphy static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
3103de6be7aSRobin Murphy 				struct cci_pmu_hw_events *hw,
3113de6be7aSRobin Murphy 				unsigned long cci_event)
3123de6be7aSRobin Murphy {
3133de6be7aSRobin Murphy 	int idx;
3143de6be7aSRobin Murphy 
3153de6be7aSRobin Murphy 	/* cycles event idx is fixed */
3163de6be7aSRobin Murphy 	if (cci_event == CCI400_PMU_CYCLES) {
3173de6be7aSRobin Murphy 		if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
3183de6be7aSRobin Murphy 			return -EAGAIN;
3193de6be7aSRobin Murphy 
3203de6be7aSRobin Murphy 		return CCI400_PMU_CYCLE_CNTR_IDX;
3213de6be7aSRobin Murphy 	}
3223de6be7aSRobin Murphy 
3233de6be7aSRobin Murphy 	for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
3243de6be7aSRobin Murphy 		if (!test_and_set_bit(idx, hw->used_mask))
3253de6be7aSRobin Murphy 			return idx;
3263de6be7aSRobin Murphy 
3273de6be7aSRobin Murphy 	/* No counters available */
3283de6be7aSRobin Murphy 	return -EAGAIN;
3293de6be7aSRobin Murphy }
3303de6be7aSRobin Murphy 
cci400_validate_hw_event(struct cci_pmu * cci_pmu,unsigned long hw_event)3313de6be7aSRobin Murphy static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
3323de6be7aSRobin Murphy {
3333de6be7aSRobin Murphy 	u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
3343de6be7aSRobin Murphy 	u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
3353de6be7aSRobin Murphy 	int if_type;
3363de6be7aSRobin Murphy 
3373de6be7aSRobin Murphy 	if (hw_event & ~CCI400_PMU_EVENT_MASK)
3383de6be7aSRobin Murphy 		return -ENOENT;
3393de6be7aSRobin Murphy 
3403de6be7aSRobin Murphy 	if (hw_event == CCI400_PMU_CYCLES)
3413de6be7aSRobin Murphy 		return hw_event;
3423de6be7aSRobin Murphy 
3433de6be7aSRobin Murphy 	switch (ev_source) {
3443de6be7aSRobin Murphy 	case CCI400_PORT_S0:
3453de6be7aSRobin Murphy 	case CCI400_PORT_S1:
3463de6be7aSRobin Murphy 	case CCI400_PORT_S2:
3473de6be7aSRobin Murphy 	case CCI400_PORT_S3:
3483de6be7aSRobin Murphy 	case CCI400_PORT_S4:
3493de6be7aSRobin Murphy 		/* Slave Interface */
3503de6be7aSRobin Murphy 		if_type = CCI_IF_SLAVE;
3513de6be7aSRobin Murphy 		break;
3523de6be7aSRobin Murphy 	case CCI400_PORT_M0:
3533de6be7aSRobin Murphy 	case CCI400_PORT_M1:
3543de6be7aSRobin Murphy 	case CCI400_PORT_M2:
3553de6be7aSRobin Murphy 		/* Master Interface */
3563de6be7aSRobin Murphy 		if_type = CCI_IF_MASTER;
3573de6be7aSRobin Murphy 		break;
3583de6be7aSRobin Murphy 	default:
3593de6be7aSRobin Murphy 		return -ENOENT;
3603de6be7aSRobin Murphy 	}
3613de6be7aSRobin Murphy 
3623de6be7aSRobin Murphy 	if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
3633de6be7aSRobin Murphy 		ev_code <= cci_pmu->model->event_ranges[if_type].max)
3643de6be7aSRobin Murphy 		return hw_event;
3653de6be7aSRobin Murphy 
3663de6be7aSRobin Murphy 	return -ENOENT;
3673de6be7aSRobin Murphy }
3683de6be7aSRobin Murphy 
probe_cci400_revision(struct cci_pmu * cci_pmu)369e9c112c9SRobin Murphy static int probe_cci400_revision(struct cci_pmu *cci_pmu)
3703de6be7aSRobin Murphy {
3713de6be7aSRobin Murphy 	int rev;
372e9c112c9SRobin Murphy 	rev = readl_relaxed(cci_pmu->ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
3733de6be7aSRobin Murphy 	rev >>= CCI_PID2_REV_SHIFT;
3743de6be7aSRobin Murphy 
3753de6be7aSRobin Murphy 	if (rev < CCI400_R1_PX)
3763de6be7aSRobin Murphy 		return CCI400_R0;
3773de6be7aSRobin Murphy 	else
3783de6be7aSRobin Murphy 		return CCI400_R1;
3793de6be7aSRobin Murphy }
3803de6be7aSRobin Murphy 
probe_cci_model(struct cci_pmu * cci_pmu)381e9c112c9SRobin Murphy static const struct cci_pmu_model *probe_cci_model(struct cci_pmu *cci_pmu)
3823de6be7aSRobin Murphy {
3833de6be7aSRobin Murphy 	if (platform_has_secure_cci_access())
384e9c112c9SRobin Murphy 		return &cci_pmu_models[probe_cci400_revision(cci_pmu)];
3853de6be7aSRobin Murphy 	return NULL;
3863de6be7aSRobin Murphy }
3873de6be7aSRobin Murphy #else	/* !CONFIG_ARM_CCI400_PMU */
probe_cci_model(struct cci_pmu * cci_pmu)388e9c112c9SRobin Murphy static inline struct cci_pmu_model *probe_cci_model(struct cci_pmu *cci_pmu)
3893de6be7aSRobin Murphy {
3903de6be7aSRobin Murphy 	return NULL;
3913de6be7aSRobin Murphy }
3923de6be7aSRobin Murphy #endif	/* CONFIG_ARM_CCI400_PMU */
3933de6be7aSRobin Murphy 
3943de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI5xx_PMU
3953de6be7aSRobin Murphy 
3963de6be7aSRobin Murphy /*
3973de6be7aSRobin Murphy  * CCI5xx PMU event id is an 9-bit value made of two parts.
3983de6be7aSRobin Murphy  *	 bits [8:5] - Source for the event
3993de6be7aSRobin Murphy  *	 bits [4:0] - Event code (specific to type of interface)
4003de6be7aSRobin Murphy  *
4013de6be7aSRobin Murphy  *
4023de6be7aSRobin Murphy  */
4033de6be7aSRobin Murphy 
4043de6be7aSRobin Murphy /* Port ids */
4053de6be7aSRobin Murphy #define CCI5xx_PORT_S0			0x0
4063de6be7aSRobin Murphy #define CCI5xx_PORT_S1			0x1
4073de6be7aSRobin Murphy #define CCI5xx_PORT_S2			0x2
4083de6be7aSRobin Murphy #define CCI5xx_PORT_S3			0x3
4093de6be7aSRobin Murphy #define CCI5xx_PORT_S4			0x4
4103de6be7aSRobin Murphy #define CCI5xx_PORT_S5			0x5
4113de6be7aSRobin Murphy #define CCI5xx_PORT_S6			0x6
4123de6be7aSRobin Murphy 
4133de6be7aSRobin Murphy #define CCI5xx_PORT_M0			0x8
4143de6be7aSRobin Murphy #define CCI5xx_PORT_M1			0x9
4153de6be7aSRobin Murphy #define CCI5xx_PORT_M2			0xa
4163de6be7aSRobin Murphy #define CCI5xx_PORT_M3			0xb
4173de6be7aSRobin Murphy #define CCI5xx_PORT_M4			0xc
4183de6be7aSRobin Murphy #define CCI5xx_PORT_M5			0xd
4193de6be7aSRobin Murphy #define CCI5xx_PORT_M6			0xe
4203de6be7aSRobin Murphy 
4213de6be7aSRobin Murphy #define CCI5xx_PORT_GLOBAL		0xf
4223de6be7aSRobin Murphy 
4233de6be7aSRobin Murphy #define CCI5xx_PMU_EVENT_MASK		0x1ffUL
4243de6be7aSRobin Murphy #define CCI5xx_PMU_EVENT_SOURCE_SHIFT	0x5
4253de6be7aSRobin Murphy #define CCI5xx_PMU_EVENT_SOURCE_MASK	0xf
4263de6be7aSRobin Murphy #define CCI5xx_PMU_EVENT_CODE_SHIFT	0x0
4273de6be7aSRobin Murphy #define CCI5xx_PMU_EVENT_CODE_MASK	0x1f
4283de6be7aSRobin Murphy 
4293de6be7aSRobin Murphy #define CCI5xx_PMU_EVENT_SOURCE(event)	\
4303de6be7aSRobin Murphy 	((event >> CCI5xx_PMU_EVENT_SOURCE_SHIFT) & CCI5xx_PMU_EVENT_SOURCE_MASK)
4313de6be7aSRobin Murphy #define CCI5xx_PMU_EVENT_CODE(event)	\
4323de6be7aSRobin Murphy 	((event >> CCI5xx_PMU_EVENT_CODE_SHIFT) & CCI5xx_PMU_EVENT_CODE_MASK)
4333de6be7aSRobin Murphy 
4343de6be7aSRobin Murphy #define CCI5xx_SLAVE_PORT_MIN_EV	0x00
4353de6be7aSRobin Murphy #define CCI5xx_SLAVE_PORT_MAX_EV	0x1f
4363de6be7aSRobin Murphy #define CCI5xx_MASTER_PORT_MIN_EV	0x00
4373de6be7aSRobin Murphy #define CCI5xx_MASTER_PORT_MAX_EV	0x06
4383de6be7aSRobin Murphy #define CCI5xx_GLOBAL_PORT_MIN_EV	0x00
4393de6be7aSRobin Murphy #define CCI5xx_GLOBAL_PORT_MAX_EV	0x0f
4403de6be7aSRobin Murphy 
4413de6be7aSRobin Murphy 
4423de6be7aSRobin Murphy #define CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
4433de6be7aSRobin Murphy 	CCI_EXT_ATTR_ENTRY(_name, cci5xx_pmu_global_event_show, \
4443de6be7aSRobin Murphy 					(unsigned long) _config)
4453de6be7aSRobin Murphy 
4463de6be7aSRobin Murphy static ssize_t cci5xx_pmu_global_event_show(struct device *dev,
4473de6be7aSRobin Murphy 				struct device_attribute *attr, char *buf);
4483de6be7aSRobin Murphy 
4493de6be7aSRobin Murphy static struct attribute *cci5xx_pmu_format_attrs[] = {
4503de6be7aSRobin Murphy 	CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
4513de6be7aSRobin Murphy 	CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-8"),
4523de6be7aSRobin Murphy 	NULL,
4533de6be7aSRobin Murphy };
4543de6be7aSRobin Murphy 
4553de6be7aSRobin Murphy static struct attribute *cci5xx_pmu_event_attrs[] = {
4563de6be7aSRobin Murphy 	/* Slave events */
4573de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_arvalid, 0x0),
4583de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_dev, 0x1),
4593de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_nonshareable, 0x2),
4603de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_non_alloc, 0x3),
4613de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_alloc, 0x4),
4623de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_invalidate, 0x5),
4633de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maint, 0x6),
4643de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
4653de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rval, 0x8),
4663de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rlast_snoop, 0x9),
4673de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_awalid, 0xA),
4683de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_dev, 0xB),
4693de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_non_shareable, 0xC),
4703de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wb, 0xD),
4713de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wlu, 0xE),
4723de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wunique, 0xF),
4733de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_evict, 0x10),
4743de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_wrevict, 0x11),
4753de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_beat, 0x12),
4763de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_srq_acvalid, 0x13),
4773de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_srq_read, 0x14),
4783de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_srq_clean, 0x15),
4793de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_srq_data_transfer_low, 0x16),
4803de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_arvalid, 0x17),
4813de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall, 0x18),
4823de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall, 0x19),
4833de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_stall, 0x1A),
4843de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_w_resp_stall, 0x1B),
4853de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_srq_stall, 0x1C),
4863de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_s_data_stall, 0x1D),
4873de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_rq_stall_ot_limit, 0x1E),
4883de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(si_r_stall_arbit, 0x1F),
4893de6be7aSRobin Murphy 
4903de6be7aSRobin Murphy 	/* Master events */
4913de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_beat_any, 0x0),
4923de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_beat_any, 0x1),
4933de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall, 0x2),
4943de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_stall, 0x3),
4953de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall, 0x4),
4963de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_stall, 0x5),
4973de6be7aSRobin Murphy 	CCI_EVENT_EXT_ATTR_ENTRY(mi_w_resp_stall, 0x6),
4983de6be7aSRobin Murphy 
4993de6be7aSRobin Murphy 	/* Global events */
5003de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1, 0x0),
5013de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3, 0x1),
5023de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5, 0x2),
5033de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7, 0x3),
5043de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1, 0x4),
5053de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3, 0x5),
5063de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5, 0x6),
5073de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7, 0x7),
5083de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation, 0x8),
5093de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy, 0x9),
5103de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full, 0xA),
5113de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq, 0xB),
5123de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs, 0xC),
5133de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard, 0xD),
5143de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_stall_tt_full, 0xE),
5153de6be7aSRobin Murphy 	CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot, 0xF),
5163de6be7aSRobin Murphy 	NULL
5173de6be7aSRobin Murphy };
5183de6be7aSRobin Murphy 
cci5xx_pmu_global_event_show(struct device * dev,struct device_attribute * attr,char * buf)5193de6be7aSRobin Murphy static ssize_t cci5xx_pmu_global_event_show(struct device *dev,
5203de6be7aSRobin Murphy 				struct device_attribute *attr, char *buf)
5213de6be7aSRobin Murphy {
5223de6be7aSRobin Murphy 	struct dev_ext_attribute *eattr = container_of(attr,
5233de6be7aSRobin Murphy 					struct dev_ext_attribute, attr);
5243de6be7aSRobin Murphy 	/* Global events have single fixed source code */
525700a9cf0SZihao Tang 	return sysfs_emit(buf, "event=0x%lx,source=0x%x\n",
5263de6be7aSRobin Murphy 			  (unsigned long)eattr->var, CCI5xx_PORT_GLOBAL);
5273de6be7aSRobin Murphy }
5283de6be7aSRobin Murphy 
5293de6be7aSRobin Murphy /*
5303de6be7aSRobin Murphy  * CCI500 provides 8 independent event counters that can count
5313de6be7aSRobin Murphy  * any of the events available.
5323de6be7aSRobin Murphy  * CCI500 PMU event source ids
5333de6be7aSRobin Murphy  *	0x0-0x6 - Slave interfaces
5343de6be7aSRobin Murphy  *	0x8-0xD - Master interfaces
5353de6be7aSRobin Murphy  *	0xf     - Global Events
5363de6be7aSRobin Murphy  *	0x7,0xe - Reserved
5373de6be7aSRobin Murphy  */
cci500_validate_hw_event(struct cci_pmu * cci_pmu,unsigned long hw_event)5383de6be7aSRobin Murphy static int cci500_validate_hw_event(struct cci_pmu *cci_pmu,
5393de6be7aSRobin Murphy 					unsigned long hw_event)
5403de6be7aSRobin Murphy {
5413de6be7aSRobin Murphy 	u32 ev_source = CCI5xx_PMU_EVENT_SOURCE(hw_event);
5423de6be7aSRobin Murphy 	u32 ev_code = CCI5xx_PMU_EVENT_CODE(hw_event);
5433de6be7aSRobin Murphy 	int if_type;
5443de6be7aSRobin Murphy 
5453de6be7aSRobin Murphy 	if (hw_event & ~CCI5xx_PMU_EVENT_MASK)
5463de6be7aSRobin Murphy 		return -ENOENT;
5473de6be7aSRobin Murphy 
5483de6be7aSRobin Murphy 	switch (ev_source) {
5493de6be7aSRobin Murphy 	case CCI5xx_PORT_S0:
5503de6be7aSRobin Murphy 	case CCI5xx_PORT_S1:
5513de6be7aSRobin Murphy 	case CCI5xx_PORT_S2:
5523de6be7aSRobin Murphy 	case CCI5xx_PORT_S3:
5533de6be7aSRobin Murphy 	case CCI5xx_PORT_S4:
5543de6be7aSRobin Murphy 	case CCI5xx_PORT_S5:
5553de6be7aSRobin Murphy 	case CCI5xx_PORT_S6:
5563de6be7aSRobin Murphy 		if_type = CCI_IF_SLAVE;
5573de6be7aSRobin Murphy 		break;
5583de6be7aSRobin Murphy 	case CCI5xx_PORT_M0:
5593de6be7aSRobin Murphy 	case CCI5xx_PORT_M1:
5603de6be7aSRobin Murphy 	case CCI5xx_PORT_M2:
5613de6be7aSRobin Murphy 	case CCI5xx_PORT_M3:
5623de6be7aSRobin Murphy 	case CCI5xx_PORT_M4:
5633de6be7aSRobin Murphy 	case CCI5xx_PORT_M5:
5643de6be7aSRobin Murphy 		if_type = CCI_IF_MASTER;
5653de6be7aSRobin Murphy 		break;
5663de6be7aSRobin Murphy 	case CCI5xx_PORT_GLOBAL:
5673de6be7aSRobin Murphy 		if_type = CCI_IF_GLOBAL;
5683de6be7aSRobin Murphy 		break;
5693de6be7aSRobin Murphy 	default:
5703de6be7aSRobin Murphy 		return -ENOENT;
5713de6be7aSRobin Murphy 	}
5723de6be7aSRobin Murphy 
5733de6be7aSRobin Murphy 	if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
5743de6be7aSRobin Murphy 		ev_code <= cci_pmu->model->event_ranges[if_type].max)
5753de6be7aSRobin Murphy 		return hw_event;
5763de6be7aSRobin Murphy 
5773de6be7aSRobin Murphy 	return -ENOENT;
5783de6be7aSRobin Murphy }
5793de6be7aSRobin Murphy 
5803de6be7aSRobin Murphy /*
5813de6be7aSRobin Murphy  * CCI550 provides 8 independent event counters that can count
5823de6be7aSRobin Murphy  * any of the events available.
5833de6be7aSRobin Murphy  * CCI550 PMU event source ids
5843de6be7aSRobin Murphy  *	0x0-0x6 - Slave interfaces
5853de6be7aSRobin Murphy  *	0x8-0xe - Master interfaces
5863de6be7aSRobin Murphy  *	0xf     - Global Events
5873de6be7aSRobin Murphy  *	0x7	- Reserved
5883de6be7aSRobin Murphy  */
cci550_validate_hw_event(struct cci_pmu * cci_pmu,unsigned long hw_event)5893de6be7aSRobin Murphy static int cci550_validate_hw_event(struct cci_pmu *cci_pmu,
5903de6be7aSRobin Murphy 					unsigned long hw_event)
5913de6be7aSRobin Murphy {
5923de6be7aSRobin Murphy 	u32 ev_source = CCI5xx_PMU_EVENT_SOURCE(hw_event);
5933de6be7aSRobin Murphy 	u32 ev_code = CCI5xx_PMU_EVENT_CODE(hw_event);
5943de6be7aSRobin Murphy 	int if_type;
5953de6be7aSRobin Murphy 
5963de6be7aSRobin Murphy 	if (hw_event & ~CCI5xx_PMU_EVENT_MASK)
5973de6be7aSRobin Murphy 		return -ENOENT;
5983de6be7aSRobin Murphy 
5993de6be7aSRobin Murphy 	switch (ev_source) {
6003de6be7aSRobin Murphy 	case CCI5xx_PORT_S0:
6013de6be7aSRobin Murphy 	case CCI5xx_PORT_S1:
6023de6be7aSRobin Murphy 	case CCI5xx_PORT_S2:
6033de6be7aSRobin Murphy 	case CCI5xx_PORT_S3:
6043de6be7aSRobin Murphy 	case CCI5xx_PORT_S4:
6053de6be7aSRobin Murphy 	case CCI5xx_PORT_S5:
6063de6be7aSRobin Murphy 	case CCI5xx_PORT_S6:
6073de6be7aSRobin Murphy 		if_type = CCI_IF_SLAVE;
6083de6be7aSRobin Murphy 		break;
6093de6be7aSRobin Murphy 	case CCI5xx_PORT_M0:
6103de6be7aSRobin Murphy 	case CCI5xx_PORT_M1:
6113de6be7aSRobin Murphy 	case CCI5xx_PORT_M2:
6123de6be7aSRobin Murphy 	case CCI5xx_PORT_M3:
6133de6be7aSRobin Murphy 	case CCI5xx_PORT_M4:
6143de6be7aSRobin Murphy 	case CCI5xx_PORT_M5:
6153de6be7aSRobin Murphy 	case CCI5xx_PORT_M6:
6163de6be7aSRobin Murphy 		if_type = CCI_IF_MASTER;
6173de6be7aSRobin Murphy 		break;
6183de6be7aSRobin Murphy 	case CCI5xx_PORT_GLOBAL:
6193de6be7aSRobin Murphy 		if_type = CCI_IF_GLOBAL;
6203de6be7aSRobin Murphy 		break;
6213de6be7aSRobin Murphy 	default:
6223de6be7aSRobin Murphy 		return -ENOENT;
6233de6be7aSRobin Murphy 	}
6243de6be7aSRobin Murphy 
6253de6be7aSRobin Murphy 	if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
6263de6be7aSRobin Murphy 		ev_code <= cci_pmu->model->event_ranges[if_type].max)
6273de6be7aSRobin Murphy 		return hw_event;
6283de6be7aSRobin Murphy 
6293de6be7aSRobin Murphy 	return -ENOENT;
6303de6be7aSRobin Murphy }
6313de6be7aSRobin Murphy 
6323de6be7aSRobin Murphy #endif	/* CONFIG_ARM_CCI5xx_PMU */
6333de6be7aSRobin Murphy 
6343de6be7aSRobin Murphy /*
6353de6be7aSRobin Murphy  * Program the CCI PMU counters which have PERF_HES_ARCH set
6363de6be7aSRobin Murphy  * with the event period and mark them ready before we enable
6373de6be7aSRobin Murphy  * PMU.
6383de6be7aSRobin Murphy  */
cci_pmu_sync_counters(struct cci_pmu * cci_pmu)6393de6be7aSRobin Murphy static void cci_pmu_sync_counters(struct cci_pmu *cci_pmu)
6403de6be7aSRobin Murphy {
6413de6be7aSRobin Murphy 	int i;
6423de6be7aSRobin Murphy 	struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
6431201a5a2SKees Cook 	DECLARE_BITMAP(mask, HW_CNTRS_MAX);
6443de6be7aSRobin Murphy 
645f818947aSChristophe JAILLET 	bitmap_zero(mask, HW_CNTRS_MAX);
6463de6be7aSRobin Murphy 	for_each_set_bit(i, cci_pmu->hw_events.used_mask, cci_pmu->num_cntrs) {
6473de6be7aSRobin Murphy 		struct perf_event *event = cci_hw->events[i];
6483de6be7aSRobin Murphy 
6493de6be7aSRobin Murphy 		if (WARN_ON(!event))
6503de6be7aSRobin Murphy 			continue;
6513de6be7aSRobin Murphy 
6523de6be7aSRobin Murphy 		/* Leave the events which are not counting */
6533de6be7aSRobin Murphy 		if (event->hw.state & PERF_HES_STOPPED)
6543de6be7aSRobin Murphy 			continue;
6553de6be7aSRobin Murphy 		if (event->hw.state & PERF_HES_ARCH) {
656f818947aSChristophe JAILLET 			__set_bit(i, mask);
6573de6be7aSRobin Murphy 			event->hw.state &= ~PERF_HES_ARCH;
6583de6be7aSRobin Murphy 		}
6593de6be7aSRobin Murphy 	}
6603de6be7aSRobin Murphy 
6613de6be7aSRobin Murphy 	pmu_write_counters(cci_pmu, mask);
6623de6be7aSRobin Murphy }
6633de6be7aSRobin Murphy 
6643de6be7aSRobin Murphy /* Should be called with cci_pmu->hw_events->pmu_lock held */
__cci_pmu_enable_nosync(struct cci_pmu * cci_pmu)6653de6be7aSRobin Murphy static void __cci_pmu_enable_nosync(struct cci_pmu *cci_pmu)
6663de6be7aSRobin Murphy {
6673de6be7aSRobin Murphy 	u32 val;
6683de6be7aSRobin Murphy 
6693de6be7aSRobin Murphy 	/* Enable all the PMU counters. */
670e9c112c9SRobin Murphy 	val = readl_relaxed(cci_pmu->ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
671e9c112c9SRobin Murphy 	writel(val, cci_pmu->ctrl_base + CCI_PMCR);
6723de6be7aSRobin Murphy }
6733de6be7aSRobin Murphy 
6743de6be7aSRobin Murphy /* Should be called with cci_pmu->hw_events->pmu_lock held */
__cci_pmu_enable_sync(struct cci_pmu * cci_pmu)6753de6be7aSRobin Murphy static void __cci_pmu_enable_sync(struct cci_pmu *cci_pmu)
6763de6be7aSRobin Murphy {
6773de6be7aSRobin Murphy 	cci_pmu_sync_counters(cci_pmu);
6783de6be7aSRobin Murphy 	__cci_pmu_enable_nosync(cci_pmu);
6793de6be7aSRobin Murphy }
6803de6be7aSRobin Murphy 
6813de6be7aSRobin Murphy /* Should be called with cci_pmu->hw_events->pmu_lock held */
__cci_pmu_disable(struct cci_pmu * cci_pmu)682e9c112c9SRobin Murphy static void __cci_pmu_disable(struct cci_pmu *cci_pmu)
6833de6be7aSRobin Murphy {
6843de6be7aSRobin Murphy 	u32 val;
6853de6be7aSRobin Murphy 
6863de6be7aSRobin Murphy 	/* Disable all the PMU counters. */
687e9c112c9SRobin Murphy 	val = readl_relaxed(cci_pmu->ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
688e9c112c9SRobin Murphy 	writel(val, cci_pmu->ctrl_base + CCI_PMCR);
6893de6be7aSRobin Murphy }
6903de6be7aSRobin Murphy 
cci_pmu_format_show(struct device * dev,struct device_attribute * attr,char * buf)6913de6be7aSRobin Murphy static ssize_t cci_pmu_format_show(struct device *dev,
6923de6be7aSRobin Murphy 			struct device_attribute *attr, char *buf)
6933de6be7aSRobin Murphy {
6943de6be7aSRobin Murphy 	struct dev_ext_attribute *eattr = container_of(attr,
6953de6be7aSRobin Murphy 				struct dev_ext_attribute, attr);
696700a9cf0SZihao Tang 	return sysfs_emit(buf, "%s\n", (char *)eattr->var);
6973de6be7aSRobin Murphy }
6983de6be7aSRobin Murphy 
cci_pmu_event_show(struct device * dev,struct device_attribute * attr,char * buf)6993de6be7aSRobin Murphy static ssize_t cci_pmu_event_show(struct device *dev,
7003de6be7aSRobin Murphy 			struct device_attribute *attr, char *buf)
7013de6be7aSRobin Murphy {
7023de6be7aSRobin Murphy 	struct dev_ext_attribute *eattr = container_of(attr,
7033de6be7aSRobin Murphy 				struct dev_ext_attribute, attr);
7043de6be7aSRobin Murphy 	/* source parameter is mandatory for normal PMU events */
705700a9cf0SZihao Tang 	return sysfs_emit(buf, "source=?,event=0x%lx\n",
7063de6be7aSRobin Murphy 			  (unsigned long)eattr->var);
7073de6be7aSRobin Murphy }
7083de6be7aSRobin Murphy 
pmu_is_valid_counter(struct cci_pmu * cci_pmu,int idx)7093de6be7aSRobin Murphy static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
7103de6be7aSRobin Murphy {
7113de6be7aSRobin Murphy 	return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
7123de6be7aSRobin Murphy }
7133de6be7aSRobin Murphy 
pmu_read_register(struct cci_pmu * cci_pmu,int idx,unsigned int offset)7143de6be7aSRobin Murphy static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
7153de6be7aSRobin Murphy {
7163de6be7aSRobin Murphy 	return readl_relaxed(cci_pmu->base +
7173de6be7aSRobin Murphy 			     CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
7183de6be7aSRobin Murphy }
7193de6be7aSRobin Murphy 
pmu_write_register(struct cci_pmu * cci_pmu,u32 value,int idx,unsigned int offset)7203de6be7aSRobin Murphy static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
7213de6be7aSRobin Murphy 			       int idx, unsigned int offset)
7223de6be7aSRobin Murphy {
7233de6be7aSRobin Murphy 	writel_relaxed(value, cci_pmu->base +
7243de6be7aSRobin Murphy 		       CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
7253de6be7aSRobin Murphy }
7263de6be7aSRobin Murphy 
pmu_disable_counter(struct cci_pmu * cci_pmu,int idx)7273de6be7aSRobin Murphy static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
7283de6be7aSRobin Murphy {
7293de6be7aSRobin Murphy 	pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
7303de6be7aSRobin Murphy }
7313de6be7aSRobin Murphy 
pmu_enable_counter(struct cci_pmu * cci_pmu,int idx)7323de6be7aSRobin Murphy static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
7333de6be7aSRobin Murphy {
7343de6be7aSRobin Murphy 	pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
7353de6be7aSRobin Murphy }
7363de6be7aSRobin Murphy 
7373de6be7aSRobin Murphy static bool __maybe_unused
pmu_counter_is_enabled(struct cci_pmu * cci_pmu,int idx)7383de6be7aSRobin Murphy pmu_counter_is_enabled(struct cci_pmu *cci_pmu, int idx)
7393de6be7aSRobin Murphy {
7403de6be7aSRobin Murphy 	return (pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR_CTRL) & 0x1) != 0;
7413de6be7aSRobin Murphy }
7423de6be7aSRobin Murphy 
pmu_set_event(struct cci_pmu * cci_pmu,int idx,unsigned long event)7433de6be7aSRobin Murphy static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
7443de6be7aSRobin Murphy {
7453de6be7aSRobin Murphy 	pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
7463de6be7aSRobin Murphy }
7473de6be7aSRobin Murphy 
7483de6be7aSRobin Murphy /*
7493de6be7aSRobin Murphy  * For all counters on the CCI-PMU, disable any 'enabled' counters,
7503de6be7aSRobin Murphy  * saving the changed counters in the mask, so that we can restore
7513de6be7aSRobin Murphy  * it later using pmu_restore_counters. The mask is private to the
7523de6be7aSRobin Murphy  * caller. We cannot rely on the used_mask maintained by the CCI_PMU
7533de6be7aSRobin Murphy  * as it only tells us if the counter is assigned to perf_event or not.
7543de6be7aSRobin Murphy  * The state of the perf_event cannot be locked by the PMU layer, hence
7553de6be7aSRobin Murphy  * we check the individual counter status (which can be locked by
7563de6be7aSRobin Murphy  * cci_pm->hw_events->pmu_lock).
7573de6be7aSRobin Murphy  *
7583de6be7aSRobin Murphy  * @mask should be initialised to empty by the caller.
7593de6be7aSRobin Murphy  */
7603de6be7aSRobin Murphy static void __maybe_unused
pmu_save_counters(struct cci_pmu * cci_pmu,unsigned long * mask)7613de6be7aSRobin Murphy pmu_save_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
7623de6be7aSRobin Murphy {
7633de6be7aSRobin Murphy 	int i;
7643de6be7aSRobin Murphy 
7653de6be7aSRobin Murphy 	for (i = 0; i < cci_pmu->num_cntrs; i++) {
7663de6be7aSRobin Murphy 		if (pmu_counter_is_enabled(cci_pmu, i)) {
7673de6be7aSRobin Murphy 			set_bit(i, mask);
7683de6be7aSRobin Murphy 			pmu_disable_counter(cci_pmu, i);
7693de6be7aSRobin Murphy 		}
7703de6be7aSRobin Murphy 	}
7713de6be7aSRobin Murphy }
7723de6be7aSRobin Murphy 
7733de6be7aSRobin Murphy /*
7743de6be7aSRobin Murphy  * Restore the status of the counters. Reversal of the pmu_save_counters().
7753de6be7aSRobin Murphy  * For each counter set in the mask, enable the counter back.
7763de6be7aSRobin Murphy  */
7773de6be7aSRobin Murphy static void __maybe_unused
pmu_restore_counters(struct cci_pmu * cci_pmu,unsigned long * mask)7783de6be7aSRobin Murphy pmu_restore_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
7793de6be7aSRobin Murphy {
7803de6be7aSRobin Murphy 	int i;
7813de6be7aSRobin Murphy 
7823de6be7aSRobin Murphy 	for_each_set_bit(i, mask, cci_pmu->num_cntrs)
7833de6be7aSRobin Murphy 		pmu_enable_counter(cci_pmu, i);
7843de6be7aSRobin Murphy }
7853de6be7aSRobin Murphy 
7863de6be7aSRobin Murphy /*
7873de6be7aSRobin Murphy  * Returns the number of programmable counters actually implemented
7883de6be7aSRobin Murphy  * by the cci
7893de6be7aSRobin Murphy  */
pmu_get_max_counters(struct cci_pmu * cci_pmu)790e9c112c9SRobin Murphy static u32 pmu_get_max_counters(struct cci_pmu *cci_pmu)
7913de6be7aSRobin Murphy {
792e9c112c9SRobin Murphy 	return (readl_relaxed(cci_pmu->ctrl_base + CCI_PMCR) &
7933de6be7aSRobin Murphy 		CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
7943de6be7aSRobin Murphy }
7953de6be7aSRobin Murphy 
pmu_get_event_idx(struct cci_pmu_hw_events * hw,struct perf_event * event)7963de6be7aSRobin Murphy static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
7973de6be7aSRobin Murphy {
7983de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
7993de6be7aSRobin Murphy 	unsigned long cci_event = event->hw.config_base;
8003de6be7aSRobin Murphy 	int idx;
8013de6be7aSRobin Murphy 
8023de6be7aSRobin Murphy 	if (cci_pmu->model->get_event_idx)
8033de6be7aSRobin Murphy 		return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
8043de6be7aSRobin Murphy 
8053de6be7aSRobin Murphy 	/* Generic code to find an unused idx from the mask */
8063de6be7aSRobin Murphy 	for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
8073de6be7aSRobin Murphy 		if (!test_and_set_bit(idx, hw->used_mask))
8083de6be7aSRobin Murphy 			return idx;
8093de6be7aSRobin Murphy 
8103de6be7aSRobin Murphy 	/* No counters available */
8113de6be7aSRobin Murphy 	return -EAGAIN;
8123de6be7aSRobin Murphy }
8133de6be7aSRobin Murphy 
pmu_map_event(struct perf_event * event)8143de6be7aSRobin Murphy static int pmu_map_event(struct perf_event *event)
8153de6be7aSRobin Murphy {
8163de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
8173de6be7aSRobin Murphy 
8183de6be7aSRobin Murphy 	if (event->attr.type < PERF_TYPE_MAX ||
8193de6be7aSRobin Murphy 			!cci_pmu->model->validate_hw_event)
8203de6be7aSRobin Murphy 		return -ENOENT;
8213de6be7aSRobin Murphy 
8223de6be7aSRobin Murphy 	return	cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
8233de6be7aSRobin Murphy }
8243de6be7aSRobin Murphy 
pmu_request_irq(struct cci_pmu * cci_pmu,irq_handler_t handler)8253de6be7aSRobin Murphy static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
8263de6be7aSRobin Murphy {
8273de6be7aSRobin Murphy 	int i;
8283de6be7aSRobin Murphy 	struct platform_device *pmu_device = cci_pmu->plat_device;
8293de6be7aSRobin Murphy 
8303de6be7aSRobin Murphy 	if (unlikely(!pmu_device))
8313de6be7aSRobin Murphy 		return -ENODEV;
8323de6be7aSRobin Murphy 
8333de6be7aSRobin Murphy 	if (cci_pmu->nr_irqs < 1) {
8343de6be7aSRobin Murphy 		dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
8353de6be7aSRobin Murphy 		return -ENODEV;
8363de6be7aSRobin Murphy 	}
8373de6be7aSRobin Murphy 
8383de6be7aSRobin Murphy 	/*
8393de6be7aSRobin Murphy 	 * Register all available CCI PMU interrupts. In the interrupt handler
8403de6be7aSRobin Murphy 	 * we iterate over the counters checking for interrupt source (the
8413de6be7aSRobin Murphy 	 * overflowing counter) and clear it.
8423de6be7aSRobin Murphy 	 *
8433de6be7aSRobin Murphy 	 * This should allow handling of non-unique interrupt for the counters.
8443de6be7aSRobin Murphy 	 */
8453de6be7aSRobin Murphy 	for (i = 0; i < cci_pmu->nr_irqs; i++) {
8463de6be7aSRobin Murphy 		int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
8473de6be7aSRobin Murphy 				"arm-cci-pmu", cci_pmu);
8483de6be7aSRobin Murphy 		if (err) {
8493de6be7aSRobin Murphy 			dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
8503de6be7aSRobin Murphy 				cci_pmu->irqs[i]);
8513de6be7aSRobin Murphy 			return err;
8523de6be7aSRobin Murphy 		}
8533de6be7aSRobin Murphy 
8543de6be7aSRobin Murphy 		set_bit(i, &cci_pmu->active_irqs);
8553de6be7aSRobin Murphy 	}
8563de6be7aSRobin Murphy 
8573de6be7aSRobin Murphy 	return 0;
8583de6be7aSRobin Murphy }
8593de6be7aSRobin Murphy 
pmu_free_irq(struct cci_pmu * cci_pmu)8603de6be7aSRobin Murphy static void pmu_free_irq(struct cci_pmu *cci_pmu)
8613de6be7aSRobin Murphy {
8623de6be7aSRobin Murphy 	int i;
8633de6be7aSRobin Murphy 
8643de6be7aSRobin Murphy 	for (i = 0; i < cci_pmu->nr_irqs; i++) {
8653de6be7aSRobin Murphy 		if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
8663de6be7aSRobin Murphy 			continue;
8673de6be7aSRobin Murphy 
8683de6be7aSRobin Murphy 		free_irq(cci_pmu->irqs[i], cci_pmu);
8693de6be7aSRobin Murphy 	}
8703de6be7aSRobin Murphy }
8713de6be7aSRobin Murphy 
pmu_read_counter(struct perf_event * event)8723de6be7aSRobin Murphy static u32 pmu_read_counter(struct perf_event *event)
8733de6be7aSRobin Murphy {
8743de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
8753de6be7aSRobin Murphy 	struct hw_perf_event *hw_counter = &event->hw;
8763de6be7aSRobin Murphy 	int idx = hw_counter->idx;
8773de6be7aSRobin Murphy 	u32 value;
8783de6be7aSRobin Murphy 
8793de6be7aSRobin Murphy 	if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
8803de6be7aSRobin Murphy 		dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
8813de6be7aSRobin Murphy 		return 0;
8823de6be7aSRobin Murphy 	}
8833de6be7aSRobin Murphy 	value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
8843de6be7aSRobin Murphy 
8853de6be7aSRobin Murphy 	return value;
8863de6be7aSRobin Murphy }
8873de6be7aSRobin Murphy 
pmu_write_counter(struct cci_pmu * cci_pmu,u32 value,int idx)8883de6be7aSRobin Murphy static void pmu_write_counter(struct cci_pmu *cci_pmu, u32 value, int idx)
8893de6be7aSRobin Murphy {
8903de6be7aSRobin Murphy 	pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
8913de6be7aSRobin Murphy }
8923de6be7aSRobin Murphy 
__pmu_write_counters(struct cci_pmu * cci_pmu,unsigned long * mask)8933de6be7aSRobin Murphy static void __pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
8943de6be7aSRobin Murphy {
8953de6be7aSRobin Murphy 	int i;
8963de6be7aSRobin Murphy 	struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
8973de6be7aSRobin Murphy 
8983de6be7aSRobin Murphy 	for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
8993de6be7aSRobin Murphy 		struct perf_event *event = cci_hw->events[i];
9003de6be7aSRobin Murphy 
9013de6be7aSRobin Murphy 		if (WARN_ON(!event))
9023de6be7aSRobin Murphy 			continue;
9033de6be7aSRobin Murphy 		pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
9043de6be7aSRobin Murphy 	}
9053de6be7aSRobin Murphy }
9063de6be7aSRobin Murphy 
pmu_write_counters(struct cci_pmu * cci_pmu,unsigned long * mask)9073de6be7aSRobin Murphy static void pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
9083de6be7aSRobin Murphy {
9093de6be7aSRobin Murphy 	if (cci_pmu->model->write_counters)
9103de6be7aSRobin Murphy 		cci_pmu->model->write_counters(cci_pmu, mask);
9113de6be7aSRobin Murphy 	else
9123de6be7aSRobin Murphy 		__pmu_write_counters(cci_pmu, mask);
9133de6be7aSRobin Murphy }
9143de6be7aSRobin Murphy 
9153de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI5xx_PMU
9163de6be7aSRobin Murphy 
9173de6be7aSRobin Murphy /*
9183de6be7aSRobin Murphy  * CCI-500/CCI-550 has advanced power saving policies, which could gate the
9193de6be7aSRobin Murphy  * clocks to the PMU counters, which makes the writes to them ineffective.
9203de6be7aSRobin Murphy  * The only way to write to those counters is when the global counters
9213de6be7aSRobin Murphy  * are enabled and the particular counter is enabled.
9223de6be7aSRobin Murphy  *
9233de6be7aSRobin Murphy  * So we do the following :
9243de6be7aSRobin Murphy  *
9253de6be7aSRobin Murphy  * 1) Disable all the PMU counters, saving their current state
9263de6be7aSRobin Murphy  * 2) Enable the global PMU profiling, now that all counters are
9273de6be7aSRobin Murphy  *    disabled.
9283de6be7aSRobin Murphy  *
9293de6be7aSRobin Murphy  * For each counter to be programmed, repeat steps 3-7:
9303de6be7aSRobin Murphy  *
9313de6be7aSRobin Murphy  * 3) Write an invalid event code to the event control register for the
9323de6be7aSRobin Murphy       counter, so that the counters are not modified.
9333de6be7aSRobin Murphy  * 4) Enable the counter control for the counter.
9343de6be7aSRobin Murphy  * 5) Set the counter value
9353de6be7aSRobin Murphy  * 6) Disable the counter
9363de6be7aSRobin Murphy  * 7) Restore the event in the target counter
9373de6be7aSRobin Murphy  *
9383de6be7aSRobin Murphy  * 8) Disable the global PMU.
9393de6be7aSRobin Murphy  * 9) Restore the status of the rest of the counters.
9403de6be7aSRobin Murphy  *
9413de6be7aSRobin Murphy  * We choose an event which for CCI-5xx is guaranteed not to count.
9423de6be7aSRobin Murphy  * We use the highest possible event code (0x1f) for the master interface 0.
9433de6be7aSRobin Murphy  */
9443de6be7aSRobin Murphy #define CCI5xx_INVALID_EVENT	((CCI5xx_PORT_M0 << CCI5xx_PMU_EVENT_SOURCE_SHIFT) | \
9453de6be7aSRobin Murphy 				 (CCI5xx_PMU_EVENT_CODE_MASK << CCI5xx_PMU_EVENT_CODE_SHIFT))
cci5xx_pmu_write_counters(struct cci_pmu * cci_pmu,unsigned long * mask)9463de6be7aSRobin Murphy static void cci5xx_pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
9473de6be7aSRobin Murphy {
9483de6be7aSRobin Murphy 	int i;
9491201a5a2SKees Cook 	DECLARE_BITMAP(saved_mask, HW_CNTRS_MAX);
9503de6be7aSRobin Murphy 
9513de6be7aSRobin Murphy 	bitmap_zero(saved_mask, cci_pmu->num_cntrs);
9523de6be7aSRobin Murphy 	pmu_save_counters(cci_pmu, saved_mask);
9533de6be7aSRobin Murphy 
9543de6be7aSRobin Murphy 	/*
9553de6be7aSRobin Murphy 	 * Now that all the counters are disabled, we can safely turn the PMU on,
9563de6be7aSRobin Murphy 	 * without syncing the status of the counters
9573de6be7aSRobin Murphy 	 */
9583de6be7aSRobin Murphy 	__cci_pmu_enable_nosync(cci_pmu);
9593de6be7aSRobin Murphy 
9603de6be7aSRobin Murphy 	for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
9613de6be7aSRobin Murphy 		struct perf_event *event = cci_pmu->hw_events.events[i];
9623de6be7aSRobin Murphy 
9633de6be7aSRobin Murphy 		if (WARN_ON(!event))
9643de6be7aSRobin Murphy 			continue;
9653de6be7aSRobin Murphy 
9663de6be7aSRobin Murphy 		pmu_set_event(cci_pmu, i, CCI5xx_INVALID_EVENT);
9673de6be7aSRobin Murphy 		pmu_enable_counter(cci_pmu, i);
9683de6be7aSRobin Murphy 		pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
9693de6be7aSRobin Murphy 		pmu_disable_counter(cci_pmu, i);
9703de6be7aSRobin Murphy 		pmu_set_event(cci_pmu, i, event->hw.config_base);
9713de6be7aSRobin Murphy 	}
9723de6be7aSRobin Murphy 
973e9c112c9SRobin Murphy 	__cci_pmu_disable(cci_pmu);
9743de6be7aSRobin Murphy 
9753de6be7aSRobin Murphy 	pmu_restore_counters(cci_pmu, saved_mask);
9763de6be7aSRobin Murphy }
9773de6be7aSRobin Murphy 
9783de6be7aSRobin Murphy #endif	/* CONFIG_ARM_CCI5xx_PMU */
9793de6be7aSRobin Murphy 
pmu_event_update(struct perf_event * event)9803de6be7aSRobin Murphy static u64 pmu_event_update(struct perf_event *event)
9813de6be7aSRobin Murphy {
9823de6be7aSRobin Murphy 	struct hw_perf_event *hwc = &event->hw;
9833de6be7aSRobin Murphy 	u64 delta, prev_raw_count, new_raw_count;
9843de6be7aSRobin Murphy 
9853de6be7aSRobin Murphy 	do {
9863de6be7aSRobin Murphy 		prev_raw_count = local64_read(&hwc->prev_count);
9873de6be7aSRobin Murphy 		new_raw_count = pmu_read_counter(event);
9883de6be7aSRobin Murphy 	} while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
9893de6be7aSRobin Murphy 		 new_raw_count) != prev_raw_count);
9903de6be7aSRobin Murphy 
9913de6be7aSRobin Murphy 	delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
9923de6be7aSRobin Murphy 
9933de6be7aSRobin Murphy 	local64_add(delta, &event->count);
9943de6be7aSRobin Murphy 
9953de6be7aSRobin Murphy 	return new_raw_count;
9963de6be7aSRobin Murphy }
9973de6be7aSRobin Murphy 
pmu_read(struct perf_event * event)9983de6be7aSRobin Murphy static void pmu_read(struct perf_event *event)
9993de6be7aSRobin Murphy {
10003de6be7aSRobin Murphy 	pmu_event_update(event);
10013de6be7aSRobin Murphy }
10023de6be7aSRobin Murphy 
pmu_event_set_period(struct perf_event * event)10033de6be7aSRobin Murphy static void pmu_event_set_period(struct perf_event *event)
10043de6be7aSRobin Murphy {
10053de6be7aSRobin Murphy 	struct hw_perf_event *hwc = &event->hw;
10063de6be7aSRobin Murphy 	/*
10073de6be7aSRobin Murphy 	 * The CCI PMU counters have a period of 2^32. To account for the
10083de6be7aSRobin Murphy 	 * possiblity of extreme interrupt latency we program for a period of
10093de6be7aSRobin Murphy 	 * half that. Hopefully we can handle the interrupt before another 2^31
10103de6be7aSRobin Murphy 	 * events occur and the counter overtakes its previous value.
10113de6be7aSRobin Murphy 	 */
10123de6be7aSRobin Murphy 	u64 val = 1ULL << 31;
10133de6be7aSRobin Murphy 	local64_set(&hwc->prev_count, val);
10143de6be7aSRobin Murphy 
10153de6be7aSRobin Murphy 	/*
10163de6be7aSRobin Murphy 	 * CCI PMU uses PERF_HES_ARCH to keep track of the counters, whose
10173de6be7aSRobin Murphy 	 * values needs to be sync-ed with the s/w state before the PMU is
10183de6be7aSRobin Murphy 	 * enabled.
10193de6be7aSRobin Murphy 	 * Mark this counter for sync.
10203de6be7aSRobin Murphy 	 */
10213de6be7aSRobin Murphy 	hwc->state |= PERF_HES_ARCH;
10223de6be7aSRobin Murphy }
10233de6be7aSRobin Murphy 
pmu_handle_irq(int irq_num,void * dev)10243de6be7aSRobin Murphy static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
10253de6be7aSRobin Murphy {
10263de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = dev;
10273de6be7aSRobin Murphy 	struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
10283de6be7aSRobin Murphy 	int idx, handled = IRQ_NONE;
10293de6be7aSRobin Murphy 
10308ee37e0fSQi Liu 	raw_spin_lock(&events->pmu_lock);
10313de6be7aSRobin Murphy 
10323de6be7aSRobin Murphy 	/* Disable the PMU while we walk through the counters */
1033e9c112c9SRobin Murphy 	__cci_pmu_disable(cci_pmu);
10343de6be7aSRobin Murphy 	/*
10353de6be7aSRobin Murphy 	 * Iterate over counters and update the corresponding perf events.
10363de6be7aSRobin Murphy 	 * This should work regardless of whether we have per-counter overflow
10373de6be7aSRobin Murphy 	 * interrupt or a combined overflow interrupt.
10383de6be7aSRobin Murphy 	 */
10393de6be7aSRobin Murphy 	for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
10403de6be7aSRobin Murphy 		struct perf_event *event = events->events[idx];
10413de6be7aSRobin Murphy 
10423de6be7aSRobin Murphy 		if (!event)
10433de6be7aSRobin Murphy 			continue;
10443de6be7aSRobin Murphy 
10453de6be7aSRobin Murphy 		/* Did this counter overflow? */
10463de6be7aSRobin Murphy 		if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
10473de6be7aSRobin Murphy 		      CCI_PMU_OVRFLW_FLAG))
10483de6be7aSRobin Murphy 			continue;
10493de6be7aSRobin Murphy 
10503de6be7aSRobin Murphy 		pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
10513de6be7aSRobin Murphy 							CCI_PMU_OVRFLW);
10523de6be7aSRobin Murphy 
10533de6be7aSRobin Murphy 		pmu_event_update(event);
10543de6be7aSRobin Murphy 		pmu_event_set_period(event);
10553de6be7aSRobin Murphy 		handled = IRQ_HANDLED;
10563de6be7aSRobin Murphy 	}
10573de6be7aSRobin Murphy 
10583de6be7aSRobin Murphy 	/* Enable the PMU and sync possibly overflowed counters */
10593de6be7aSRobin Murphy 	__cci_pmu_enable_sync(cci_pmu);
10608ee37e0fSQi Liu 	raw_spin_unlock(&events->pmu_lock);
10613de6be7aSRobin Murphy 
10623de6be7aSRobin Murphy 	return IRQ_RETVAL(handled);
10633de6be7aSRobin Murphy }
10643de6be7aSRobin Murphy 
cci_pmu_get_hw(struct cci_pmu * cci_pmu)10653de6be7aSRobin Murphy static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
10663de6be7aSRobin Murphy {
10673de6be7aSRobin Murphy 	int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
10683de6be7aSRobin Murphy 	if (ret) {
10693de6be7aSRobin Murphy 		pmu_free_irq(cci_pmu);
10703de6be7aSRobin Murphy 		return ret;
10713de6be7aSRobin Murphy 	}
10723de6be7aSRobin Murphy 	return 0;
10733de6be7aSRobin Murphy }
10743de6be7aSRobin Murphy 
cci_pmu_put_hw(struct cci_pmu * cci_pmu)10753de6be7aSRobin Murphy static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
10763de6be7aSRobin Murphy {
10773de6be7aSRobin Murphy 	pmu_free_irq(cci_pmu);
10783de6be7aSRobin Murphy }
10793de6be7aSRobin Murphy 
hw_perf_event_destroy(struct perf_event * event)10803de6be7aSRobin Murphy static void hw_perf_event_destroy(struct perf_event *event)
10813de6be7aSRobin Murphy {
10823de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
10833de6be7aSRobin Murphy 	atomic_t *active_events = &cci_pmu->active_events;
10843de6be7aSRobin Murphy 	struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
10853de6be7aSRobin Murphy 
10863de6be7aSRobin Murphy 	if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
10873de6be7aSRobin Murphy 		cci_pmu_put_hw(cci_pmu);
10883de6be7aSRobin Murphy 		mutex_unlock(reserve_mutex);
10893de6be7aSRobin Murphy 	}
10903de6be7aSRobin Murphy }
10913de6be7aSRobin Murphy 
cci_pmu_enable(struct pmu * pmu)10923de6be7aSRobin Murphy static void cci_pmu_enable(struct pmu *pmu)
10933de6be7aSRobin Murphy {
10943de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
10953de6be7aSRobin Murphy 	struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
109695ed57c7SYury Norov 	bool enabled = !bitmap_empty(hw_events->used_mask, cci_pmu->num_cntrs);
10973de6be7aSRobin Murphy 	unsigned long flags;
10983de6be7aSRobin Murphy 
10993de6be7aSRobin Murphy 	if (!enabled)
11003de6be7aSRobin Murphy 		return;
11013de6be7aSRobin Murphy 
11023de6be7aSRobin Murphy 	raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
11033de6be7aSRobin Murphy 	__cci_pmu_enable_sync(cci_pmu);
11043de6be7aSRobin Murphy 	raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
11053de6be7aSRobin Murphy 
11063de6be7aSRobin Murphy }
11073de6be7aSRobin Murphy 
cci_pmu_disable(struct pmu * pmu)11083de6be7aSRobin Murphy static void cci_pmu_disable(struct pmu *pmu)
11093de6be7aSRobin Murphy {
11103de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
11113de6be7aSRobin Murphy 	struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
11123de6be7aSRobin Murphy 	unsigned long flags;
11133de6be7aSRobin Murphy 
11143de6be7aSRobin Murphy 	raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
1115e9c112c9SRobin Murphy 	__cci_pmu_disable(cci_pmu);
11163de6be7aSRobin Murphy 	raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
11173de6be7aSRobin Murphy }
11183de6be7aSRobin Murphy 
11193de6be7aSRobin Murphy /*
11203de6be7aSRobin Murphy  * Check if the idx represents a non-programmable counter.
11213de6be7aSRobin Murphy  * All the fixed event counters are mapped before the programmable
11223de6be7aSRobin Murphy  * counters.
11233de6be7aSRobin Murphy  */
pmu_fixed_hw_idx(struct cci_pmu * cci_pmu,int idx)11243de6be7aSRobin Murphy static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
11253de6be7aSRobin Murphy {
11263de6be7aSRobin Murphy 	return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
11273de6be7aSRobin Murphy }
11283de6be7aSRobin Murphy 
cci_pmu_start(struct perf_event * event,int pmu_flags)11293de6be7aSRobin Murphy static void cci_pmu_start(struct perf_event *event, int pmu_flags)
11303de6be7aSRobin Murphy {
11313de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
11323de6be7aSRobin Murphy 	struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
11333de6be7aSRobin Murphy 	struct hw_perf_event *hwc = &event->hw;
11343de6be7aSRobin Murphy 	int idx = hwc->idx;
11353de6be7aSRobin Murphy 	unsigned long flags;
11363de6be7aSRobin Murphy 
11373de6be7aSRobin Murphy 	/*
11383de6be7aSRobin Murphy 	 * To handle interrupt latency, we always reprogram the period
11399ba86a47SJulia Lawall 	 * regardless of PERF_EF_RELOAD.
11403de6be7aSRobin Murphy 	 */
11413de6be7aSRobin Murphy 	if (pmu_flags & PERF_EF_RELOAD)
11423de6be7aSRobin Murphy 		WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
11433de6be7aSRobin Murphy 
11443de6be7aSRobin Murphy 	hwc->state = 0;
11453de6be7aSRobin Murphy 
11463de6be7aSRobin Murphy 	if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
11473de6be7aSRobin Murphy 		dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
11483de6be7aSRobin Murphy 		return;
11493de6be7aSRobin Murphy 	}
11503de6be7aSRobin Murphy 
11513de6be7aSRobin Murphy 	raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
11523de6be7aSRobin Murphy 
11533de6be7aSRobin Murphy 	/* Configure the counter unless you are counting a fixed event */
11543de6be7aSRobin Murphy 	if (!pmu_fixed_hw_idx(cci_pmu, idx))
11553de6be7aSRobin Murphy 		pmu_set_event(cci_pmu, idx, hwc->config_base);
11563de6be7aSRobin Murphy 
11573de6be7aSRobin Murphy 	pmu_event_set_period(event);
11583de6be7aSRobin Murphy 	pmu_enable_counter(cci_pmu, idx);
11593de6be7aSRobin Murphy 
11603de6be7aSRobin Murphy 	raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
11613de6be7aSRobin Murphy }
11623de6be7aSRobin Murphy 
cci_pmu_stop(struct perf_event * event,int pmu_flags)11633de6be7aSRobin Murphy static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
11643de6be7aSRobin Murphy {
11653de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
11663de6be7aSRobin Murphy 	struct hw_perf_event *hwc = &event->hw;
11673de6be7aSRobin Murphy 	int idx = hwc->idx;
11683de6be7aSRobin Murphy 
11693de6be7aSRobin Murphy 	if (hwc->state & PERF_HES_STOPPED)
11703de6be7aSRobin Murphy 		return;
11713de6be7aSRobin Murphy 
11723de6be7aSRobin Murphy 	if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
11733de6be7aSRobin Murphy 		dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
11743de6be7aSRobin Murphy 		return;
11753de6be7aSRobin Murphy 	}
11763de6be7aSRobin Murphy 
11773de6be7aSRobin Murphy 	/*
11783de6be7aSRobin Murphy 	 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
11793de6be7aSRobin Murphy 	 * cci_pmu_start()
11803de6be7aSRobin Murphy 	 */
11813de6be7aSRobin Murphy 	pmu_disable_counter(cci_pmu, idx);
11823de6be7aSRobin Murphy 	pmu_event_update(event);
11833de6be7aSRobin Murphy 	hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
11843de6be7aSRobin Murphy }
11853de6be7aSRobin Murphy 
cci_pmu_add(struct perf_event * event,int flags)11863de6be7aSRobin Murphy static int cci_pmu_add(struct perf_event *event, int flags)
11873de6be7aSRobin Murphy {
11883de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
11893de6be7aSRobin Murphy 	struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
11903de6be7aSRobin Murphy 	struct hw_perf_event *hwc = &event->hw;
11913de6be7aSRobin Murphy 	int idx;
11923de6be7aSRobin Murphy 
11933de6be7aSRobin Murphy 	/* If we don't have a space for the counter then finish early. */
11943de6be7aSRobin Murphy 	idx = pmu_get_event_idx(hw_events, event);
119528c01dc9SRobin Murphy 	if (idx < 0)
119628c01dc9SRobin Murphy 		return idx;
11973de6be7aSRobin Murphy 
11983de6be7aSRobin Murphy 	event->hw.idx = idx;
11993de6be7aSRobin Murphy 	hw_events->events[idx] = event;
12003de6be7aSRobin Murphy 
12013de6be7aSRobin Murphy 	hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
12023de6be7aSRobin Murphy 	if (flags & PERF_EF_START)
12033de6be7aSRobin Murphy 		cci_pmu_start(event, PERF_EF_RELOAD);
12043de6be7aSRobin Murphy 
12053de6be7aSRobin Murphy 	/* Propagate our changes to the userspace mapping. */
12063de6be7aSRobin Murphy 	perf_event_update_userpage(event);
12073de6be7aSRobin Murphy 
120828c01dc9SRobin Murphy 	return 0;
12093de6be7aSRobin Murphy }
12103de6be7aSRobin Murphy 
cci_pmu_del(struct perf_event * event,int flags)12113de6be7aSRobin Murphy static void cci_pmu_del(struct perf_event *event, int flags)
12123de6be7aSRobin Murphy {
12133de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
12143de6be7aSRobin Murphy 	struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
12153de6be7aSRobin Murphy 	struct hw_perf_event *hwc = &event->hw;
12163de6be7aSRobin Murphy 	int idx = hwc->idx;
12173de6be7aSRobin Murphy 
12183de6be7aSRobin Murphy 	cci_pmu_stop(event, PERF_EF_UPDATE);
12193de6be7aSRobin Murphy 	hw_events->events[idx] = NULL;
12203de6be7aSRobin Murphy 	clear_bit(idx, hw_events->used_mask);
12213de6be7aSRobin Murphy 
12223de6be7aSRobin Murphy 	perf_event_update_userpage(event);
12233de6be7aSRobin Murphy }
12243de6be7aSRobin Murphy 
validate_event(struct pmu * cci_pmu,struct cci_pmu_hw_events * hw_events,struct perf_event * event)12253de6be7aSRobin Murphy static int validate_event(struct pmu *cci_pmu,
12263de6be7aSRobin Murphy 			  struct cci_pmu_hw_events *hw_events,
12273de6be7aSRobin Murphy 			  struct perf_event *event)
12283de6be7aSRobin Murphy {
12293de6be7aSRobin Murphy 	if (is_software_event(event))
12303de6be7aSRobin Murphy 		return 1;
12313de6be7aSRobin Murphy 
12323de6be7aSRobin Murphy 	/*
12333de6be7aSRobin Murphy 	 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
12343de6be7aSRobin Murphy 	 * core perf code won't check that the pmu->ctx == leader->ctx
12353de6be7aSRobin Murphy 	 * until after pmu->event_init(event).
12363de6be7aSRobin Murphy 	 */
12373de6be7aSRobin Murphy 	if (event->pmu != cci_pmu)
12383de6be7aSRobin Murphy 		return 0;
12393de6be7aSRobin Murphy 
12403de6be7aSRobin Murphy 	if (event->state < PERF_EVENT_STATE_OFF)
12413de6be7aSRobin Murphy 		return 1;
12423de6be7aSRobin Murphy 
12433de6be7aSRobin Murphy 	if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
12443de6be7aSRobin Murphy 		return 1;
12453de6be7aSRobin Murphy 
12463de6be7aSRobin Murphy 	return pmu_get_event_idx(hw_events, event) >= 0;
12473de6be7aSRobin Murphy }
12483de6be7aSRobin Murphy 
validate_group(struct perf_event * event)12493de6be7aSRobin Murphy static int validate_group(struct perf_event *event)
12503de6be7aSRobin Murphy {
12513de6be7aSRobin Murphy 	struct perf_event *sibling, *leader = event->group_leader;
12523de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
12531201a5a2SKees Cook 	unsigned long mask[BITS_TO_LONGS(HW_CNTRS_MAX)];
12543de6be7aSRobin Murphy 	struct cci_pmu_hw_events fake_pmu = {
12553de6be7aSRobin Murphy 		/*
12563de6be7aSRobin Murphy 		 * Initialise the fake PMU. We only need to populate the
12573de6be7aSRobin Murphy 		 * used_mask for the purposes of validation.
12583de6be7aSRobin Murphy 		 */
12593de6be7aSRobin Murphy 		.used_mask = mask,
12603de6be7aSRobin Murphy 	};
12610e35850bSChristophe JAILLET 	bitmap_zero(mask, cci_pmu->num_cntrs);
12623de6be7aSRobin Murphy 
12633de6be7aSRobin Murphy 	if (!validate_event(event->pmu, &fake_pmu, leader))
12643de6be7aSRobin Murphy 		return -EINVAL;
12653de6be7aSRobin Murphy 
126638c23685SLinus Torvalds 	for_each_sibling_event(sibling, leader) {
12673de6be7aSRobin Murphy 		if (!validate_event(event->pmu, &fake_pmu, sibling))
12683de6be7aSRobin Murphy 			return -EINVAL;
12693de6be7aSRobin Murphy 	}
12703de6be7aSRobin Murphy 
12713de6be7aSRobin Murphy 	if (!validate_event(event->pmu, &fake_pmu, event))
12723de6be7aSRobin Murphy 		return -EINVAL;
12733de6be7aSRobin Murphy 
12743de6be7aSRobin Murphy 	return 0;
12753de6be7aSRobin Murphy }
12763de6be7aSRobin Murphy 
__hw_perf_event_init(struct perf_event * event)12773de6be7aSRobin Murphy static int __hw_perf_event_init(struct perf_event *event)
12783de6be7aSRobin Murphy {
12793de6be7aSRobin Murphy 	struct hw_perf_event *hwc = &event->hw;
12803de6be7aSRobin Murphy 	int mapping;
12813de6be7aSRobin Murphy 
12823de6be7aSRobin Murphy 	mapping = pmu_map_event(event);
12833de6be7aSRobin Murphy 
12843de6be7aSRobin Murphy 	if (mapping < 0) {
12853de6be7aSRobin Murphy 		pr_debug("event %x:%llx not supported\n", event->attr.type,
12863de6be7aSRobin Murphy 			 event->attr.config);
12873de6be7aSRobin Murphy 		return mapping;
12883de6be7aSRobin Murphy 	}
12893de6be7aSRobin Murphy 
12903de6be7aSRobin Murphy 	/*
12913de6be7aSRobin Murphy 	 * We don't assign an index until we actually place the event onto
12923de6be7aSRobin Murphy 	 * hardware. Use -1 to signify that we haven't decided where to put it
12933de6be7aSRobin Murphy 	 * yet.
12943de6be7aSRobin Murphy 	 */
12953de6be7aSRobin Murphy 	hwc->idx		= -1;
12963de6be7aSRobin Murphy 	hwc->config_base	= 0;
12973de6be7aSRobin Murphy 	hwc->config		= 0;
12983de6be7aSRobin Murphy 	hwc->event_base		= 0;
12993de6be7aSRobin Murphy 
13003de6be7aSRobin Murphy 	/*
13013de6be7aSRobin Murphy 	 * Store the event encoding into the config_base field.
13023de6be7aSRobin Murphy 	 */
13033de6be7aSRobin Murphy 	hwc->config_base	    |= (unsigned long)mapping;
13043de6be7aSRobin Murphy 
13053de6be7aSRobin Murphy 	if (event->group_leader != event) {
13063de6be7aSRobin Murphy 		if (validate_group(event) != 0)
13073de6be7aSRobin Murphy 			return -EINVAL;
13083de6be7aSRobin Murphy 	}
13093de6be7aSRobin Murphy 
13103de6be7aSRobin Murphy 	return 0;
13113de6be7aSRobin Murphy }
13123de6be7aSRobin Murphy 
cci_pmu_event_init(struct perf_event * event)13133de6be7aSRobin Murphy static int cci_pmu_event_init(struct perf_event *event)
13143de6be7aSRobin Murphy {
13153de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
13163de6be7aSRobin Murphy 	atomic_t *active_events = &cci_pmu->active_events;
13173de6be7aSRobin Murphy 	int err = 0;
13183de6be7aSRobin Murphy 
13193de6be7aSRobin Murphy 	if (event->attr.type != event->pmu->type)
13203de6be7aSRobin Murphy 		return -ENOENT;
13213de6be7aSRobin Murphy 
13223de6be7aSRobin Murphy 	/* Shared by all CPUs, no meaningful state to sample */
13233de6be7aSRobin Murphy 	if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
13243de6be7aSRobin Murphy 		return -EOPNOTSUPP;
13253de6be7aSRobin Murphy 
13263de6be7aSRobin Murphy 	/*
13273de6be7aSRobin Murphy 	 * Following the example set by other "uncore" PMUs, we accept any CPU
13283de6be7aSRobin Murphy 	 * and rewrite its affinity dynamically rather than having perf core
13293de6be7aSRobin Murphy 	 * handle cpu == -1 and pid == -1 for this case.
13303de6be7aSRobin Murphy 	 *
13313de6be7aSRobin Murphy 	 * The perf core will pin online CPUs for the duration of this call and
13323de6be7aSRobin Murphy 	 * the event being installed into its context, so the PMU's CPU can't
13333de6be7aSRobin Murphy 	 * change under our feet.
13343de6be7aSRobin Murphy 	 */
133503057f26SRobin Murphy 	if (event->cpu < 0)
13363de6be7aSRobin Murphy 		return -EINVAL;
133703057f26SRobin Murphy 	event->cpu = cci_pmu->cpu;
13383de6be7aSRobin Murphy 
13393de6be7aSRobin Murphy 	event->destroy = hw_perf_event_destroy;
13403de6be7aSRobin Murphy 	if (!atomic_inc_not_zero(active_events)) {
13413de6be7aSRobin Murphy 		mutex_lock(&cci_pmu->reserve_mutex);
13423de6be7aSRobin Murphy 		if (atomic_read(active_events) == 0)
13433de6be7aSRobin Murphy 			err = cci_pmu_get_hw(cci_pmu);
13443de6be7aSRobin Murphy 		if (!err)
13453de6be7aSRobin Murphy 			atomic_inc(active_events);
13463de6be7aSRobin Murphy 		mutex_unlock(&cci_pmu->reserve_mutex);
13473de6be7aSRobin Murphy 	}
13483de6be7aSRobin Murphy 	if (err)
13493de6be7aSRobin Murphy 		return err;
13503de6be7aSRobin Murphy 
13513de6be7aSRobin Murphy 	err = __hw_perf_event_init(event);
13523de6be7aSRobin Murphy 	if (err)
13533de6be7aSRobin Murphy 		hw_perf_event_destroy(event);
13543de6be7aSRobin Murphy 
13553de6be7aSRobin Murphy 	return err;
13563de6be7aSRobin Murphy }
13573de6be7aSRobin Murphy 
pmu_cpumask_attr_show(struct device * dev,struct device_attribute * attr,char * buf)13583de6be7aSRobin Murphy static ssize_t pmu_cpumask_attr_show(struct device *dev,
13593de6be7aSRobin Murphy 				     struct device_attribute *attr, char *buf)
13603de6be7aSRobin Murphy {
13613de6be7aSRobin Murphy 	struct pmu *pmu = dev_get_drvdata(dev);
13623de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
13633de6be7aSRobin Murphy 
136403057f26SRobin Murphy 	return cpumap_print_to_pagebuf(true, buf, cpumask_of(cci_pmu->cpu));
13653de6be7aSRobin Murphy }
13663de6be7aSRobin Murphy 
13673de6be7aSRobin Murphy static struct device_attribute pmu_cpumask_attr =
13683de6be7aSRobin Murphy 	__ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL);
13693de6be7aSRobin Murphy 
13703de6be7aSRobin Murphy static struct attribute *pmu_attrs[] = {
13713de6be7aSRobin Murphy 	&pmu_cpumask_attr.attr,
13723de6be7aSRobin Murphy 	NULL,
13733de6be7aSRobin Murphy };
13743de6be7aSRobin Murphy 
1375f0c14048SRikard Falkeborn static const struct attribute_group pmu_attr_group = {
13763de6be7aSRobin Murphy 	.attrs = pmu_attrs,
13773de6be7aSRobin Murphy };
13783de6be7aSRobin Murphy 
13793de6be7aSRobin Murphy static struct attribute_group pmu_format_attr_group = {
13803de6be7aSRobin Murphy 	.name = "format",
13813de6be7aSRobin Murphy 	.attrs = NULL,		/* Filled in cci_pmu_init_attrs */
13823de6be7aSRobin Murphy };
13833de6be7aSRobin Murphy 
13843de6be7aSRobin Murphy static struct attribute_group pmu_event_attr_group = {
13853de6be7aSRobin Murphy 	.name = "events",
13863de6be7aSRobin Murphy 	.attrs = NULL,		/* Filled in cci_pmu_init_attrs */
13873de6be7aSRobin Murphy };
13883de6be7aSRobin Murphy 
13893de6be7aSRobin Murphy static const struct attribute_group *pmu_attr_groups[] = {
13903de6be7aSRobin Murphy 	&pmu_attr_group,
13913de6be7aSRobin Murphy 	&pmu_format_attr_group,
13923de6be7aSRobin Murphy 	&pmu_event_attr_group,
13933de6be7aSRobin Murphy 	NULL
13943de6be7aSRobin Murphy };
13953de6be7aSRobin Murphy 
cci_pmu_init(struct cci_pmu * cci_pmu,struct platform_device * pdev)13963de6be7aSRobin Murphy static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
13973de6be7aSRobin Murphy {
13983de6be7aSRobin Murphy 	const struct cci_pmu_model *model = cci_pmu->model;
13993de6be7aSRobin Murphy 	char *name = model->name;
14003de6be7aSRobin Murphy 	u32 num_cntrs;
14013de6be7aSRobin Murphy 
14021201a5a2SKees Cook 	if (WARN_ON(model->num_hw_cntrs > NUM_HW_CNTRS_MAX))
14031201a5a2SKees Cook 		return -EINVAL;
14041201a5a2SKees Cook 	if (WARN_ON(model->fixed_hw_cntrs > FIXED_HW_CNTRS_MAX))
14051201a5a2SKees Cook 		return -EINVAL;
14061201a5a2SKees Cook 
14073de6be7aSRobin Murphy 	pmu_event_attr_group.attrs = model->event_attrs;
14083de6be7aSRobin Murphy 	pmu_format_attr_group.attrs = model->format_attrs;
14093de6be7aSRobin Murphy 
14103de6be7aSRobin Murphy 	cci_pmu->pmu = (struct pmu) {
14118b0c93c2SRobin Murphy 		.module		= THIS_MODULE,
14123de6be7aSRobin Murphy 		.name		= cci_pmu->model->name,
14133de6be7aSRobin Murphy 		.task_ctx_nr	= perf_invalid_context,
14143de6be7aSRobin Murphy 		.pmu_enable	= cci_pmu_enable,
14153de6be7aSRobin Murphy 		.pmu_disable	= cci_pmu_disable,
14163de6be7aSRobin Murphy 		.event_init	= cci_pmu_event_init,
14173de6be7aSRobin Murphy 		.add		= cci_pmu_add,
14183de6be7aSRobin Murphy 		.del		= cci_pmu_del,
14193de6be7aSRobin Murphy 		.start		= cci_pmu_start,
14203de6be7aSRobin Murphy 		.stop		= cci_pmu_stop,
14213de6be7aSRobin Murphy 		.read		= pmu_read,
14223de6be7aSRobin Murphy 		.attr_groups	= pmu_attr_groups,
142330656398SAndrew Murray 		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
14243de6be7aSRobin Murphy 	};
14253de6be7aSRobin Murphy 
14263de6be7aSRobin Murphy 	cci_pmu->plat_device = pdev;
1427e9c112c9SRobin Murphy 	num_cntrs = pmu_get_max_counters(cci_pmu);
14283de6be7aSRobin Murphy 	if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
14293de6be7aSRobin Murphy 		dev_warn(&pdev->dev,
14303de6be7aSRobin Murphy 			"PMU implements more counters(%d) than supported by"
14313de6be7aSRobin Murphy 			" the model(%d), truncated.",
14323de6be7aSRobin Murphy 			num_cntrs, cci_pmu->model->num_hw_cntrs);
14333de6be7aSRobin Murphy 		num_cntrs = cci_pmu->model->num_hw_cntrs;
14343de6be7aSRobin Murphy 	}
14353de6be7aSRobin Murphy 	cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
14363de6be7aSRobin Murphy 
14373de6be7aSRobin Murphy 	return perf_pmu_register(&cci_pmu->pmu, name, -1);
14383de6be7aSRobin Murphy }
14393de6be7aSRobin Murphy 
cci_pmu_offline_cpu(unsigned int cpu)144003057f26SRobin Murphy static int cci_pmu_offline_cpu(unsigned int cpu)
14413de6be7aSRobin Murphy {
144203057f26SRobin Murphy 	int target;
14433de6be7aSRobin Murphy 
144403057f26SRobin Murphy 	if (!g_cci_pmu || cpu != g_cci_pmu->cpu)
14453de6be7aSRobin Murphy 		return 0;
144603057f26SRobin Murphy 
14473de6be7aSRobin Murphy 	target = cpumask_any_but(cpu_online_mask, cpu);
14483de6be7aSRobin Murphy 	if (target >= nr_cpu_ids)
14493de6be7aSRobin Murphy 		return 0;
145003057f26SRobin Murphy 
145103057f26SRobin Murphy 	perf_pmu_migrate_context(&g_cci_pmu->pmu, cpu, target);
145203057f26SRobin Murphy 	g_cci_pmu->cpu = target;
14533de6be7aSRobin Murphy 	return 0;
14543de6be7aSRobin Murphy }
14553de6be7aSRobin Murphy 
1456984e9cf1SArnd Bergmann static __maybe_unused struct cci_pmu_model cci_pmu_models[] = {
14573de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI400_PMU
14583de6be7aSRobin Murphy 	[CCI400_R0] = {
14593de6be7aSRobin Murphy 		.name = "CCI_400",
14601201a5a2SKees Cook 		.fixed_hw_cntrs = FIXED_HW_CNTRS_CII_4XX, /* Cycle counter */
14611201a5a2SKees Cook 		.num_hw_cntrs = NUM_HW_CNTRS_CII_4XX,
14623de6be7aSRobin Murphy 		.cntr_size = SZ_4K,
14633de6be7aSRobin Murphy 		.format_attrs = cci400_pmu_format_attrs,
14643de6be7aSRobin Murphy 		.event_attrs = cci400_r0_pmu_event_attrs,
14653de6be7aSRobin Murphy 		.event_ranges = {
14663de6be7aSRobin Murphy 			[CCI_IF_SLAVE] = {
14673de6be7aSRobin Murphy 				CCI400_R0_SLAVE_PORT_MIN_EV,
14683de6be7aSRobin Murphy 				CCI400_R0_SLAVE_PORT_MAX_EV,
14693de6be7aSRobin Murphy 			},
14703de6be7aSRobin Murphy 			[CCI_IF_MASTER] = {
14713de6be7aSRobin Murphy 				CCI400_R0_MASTER_PORT_MIN_EV,
14723de6be7aSRobin Murphy 				CCI400_R0_MASTER_PORT_MAX_EV,
14733de6be7aSRobin Murphy 			},
14743de6be7aSRobin Murphy 		},
14753de6be7aSRobin Murphy 		.validate_hw_event = cci400_validate_hw_event,
14763de6be7aSRobin Murphy 		.get_event_idx = cci400_get_event_idx,
14773de6be7aSRobin Murphy 	},
14783de6be7aSRobin Murphy 	[CCI400_R1] = {
14793de6be7aSRobin Murphy 		.name = "CCI_400_r1",
14801201a5a2SKees Cook 		.fixed_hw_cntrs = FIXED_HW_CNTRS_CII_4XX, /* Cycle counter */
14811201a5a2SKees Cook 		.num_hw_cntrs = NUM_HW_CNTRS_CII_4XX,
14823de6be7aSRobin Murphy 		.cntr_size = SZ_4K,
14833de6be7aSRobin Murphy 		.format_attrs = cci400_pmu_format_attrs,
14843de6be7aSRobin Murphy 		.event_attrs = cci400_r1_pmu_event_attrs,
14853de6be7aSRobin Murphy 		.event_ranges = {
14863de6be7aSRobin Murphy 			[CCI_IF_SLAVE] = {
14873de6be7aSRobin Murphy 				CCI400_R1_SLAVE_PORT_MIN_EV,
14883de6be7aSRobin Murphy 				CCI400_R1_SLAVE_PORT_MAX_EV,
14893de6be7aSRobin Murphy 			},
14903de6be7aSRobin Murphy 			[CCI_IF_MASTER] = {
14913de6be7aSRobin Murphy 				CCI400_R1_MASTER_PORT_MIN_EV,
14923de6be7aSRobin Murphy 				CCI400_R1_MASTER_PORT_MAX_EV,
14933de6be7aSRobin Murphy 			},
14943de6be7aSRobin Murphy 		},
14953de6be7aSRobin Murphy 		.validate_hw_event = cci400_validate_hw_event,
14963de6be7aSRobin Murphy 		.get_event_idx = cci400_get_event_idx,
14973de6be7aSRobin Murphy 	},
14983de6be7aSRobin Murphy #endif
14993de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI5xx_PMU
15003de6be7aSRobin Murphy 	[CCI500_R0] = {
15013de6be7aSRobin Murphy 		.name = "CCI_500",
15021201a5a2SKees Cook 		.fixed_hw_cntrs = FIXED_HW_CNTRS_CII_5XX,
15031201a5a2SKees Cook 		.num_hw_cntrs = NUM_HW_CNTRS_CII_5XX,
15043de6be7aSRobin Murphy 		.cntr_size = SZ_64K,
15053de6be7aSRobin Murphy 		.format_attrs = cci5xx_pmu_format_attrs,
15063de6be7aSRobin Murphy 		.event_attrs = cci5xx_pmu_event_attrs,
15073de6be7aSRobin Murphy 		.event_ranges = {
15083de6be7aSRobin Murphy 			[CCI_IF_SLAVE] = {
15093de6be7aSRobin Murphy 				CCI5xx_SLAVE_PORT_MIN_EV,
15103de6be7aSRobin Murphy 				CCI5xx_SLAVE_PORT_MAX_EV,
15113de6be7aSRobin Murphy 			},
15123de6be7aSRobin Murphy 			[CCI_IF_MASTER] = {
15133de6be7aSRobin Murphy 				CCI5xx_MASTER_PORT_MIN_EV,
15143de6be7aSRobin Murphy 				CCI5xx_MASTER_PORT_MAX_EV,
15153de6be7aSRobin Murphy 			},
15163de6be7aSRobin Murphy 			[CCI_IF_GLOBAL] = {
15173de6be7aSRobin Murphy 				CCI5xx_GLOBAL_PORT_MIN_EV,
15183de6be7aSRobin Murphy 				CCI5xx_GLOBAL_PORT_MAX_EV,
15193de6be7aSRobin Murphy 			},
15203de6be7aSRobin Murphy 		},
15213de6be7aSRobin Murphy 		.validate_hw_event = cci500_validate_hw_event,
15223de6be7aSRobin Murphy 		.write_counters	= cci5xx_pmu_write_counters,
15233de6be7aSRobin Murphy 	},
15243de6be7aSRobin Murphy 	[CCI550_R0] = {
15253de6be7aSRobin Murphy 		.name = "CCI_550",
15261201a5a2SKees Cook 		.fixed_hw_cntrs = FIXED_HW_CNTRS_CII_5XX,
15271201a5a2SKees Cook 		.num_hw_cntrs = NUM_HW_CNTRS_CII_5XX,
15283de6be7aSRobin Murphy 		.cntr_size = SZ_64K,
15293de6be7aSRobin Murphy 		.format_attrs = cci5xx_pmu_format_attrs,
15303de6be7aSRobin Murphy 		.event_attrs = cci5xx_pmu_event_attrs,
15313de6be7aSRobin Murphy 		.event_ranges = {
15323de6be7aSRobin Murphy 			[CCI_IF_SLAVE] = {
15333de6be7aSRobin Murphy 				CCI5xx_SLAVE_PORT_MIN_EV,
15343de6be7aSRobin Murphy 				CCI5xx_SLAVE_PORT_MAX_EV,
15353de6be7aSRobin Murphy 			},
15363de6be7aSRobin Murphy 			[CCI_IF_MASTER] = {
15373de6be7aSRobin Murphy 				CCI5xx_MASTER_PORT_MIN_EV,
15383de6be7aSRobin Murphy 				CCI5xx_MASTER_PORT_MAX_EV,
15393de6be7aSRobin Murphy 			},
15403de6be7aSRobin Murphy 			[CCI_IF_GLOBAL] = {
15413de6be7aSRobin Murphy 				CCI5xx_GLOBAL_PORT_MIN_EV,
15423de6be7aSRobin Murphy 				CCI5xx_GLOBAL_PORT_MAX_EV,
15433de6be7aSRobin Murphy 			},
15443de6be7aSRobin Murphy 		},
15453de6be7aSRobin Murphy 		.validate_hw_event = cci550_validate_hw_event,
15463de6be7aSRobin Murphy 		.write_counters	= cci5xx_pmu_write_counters,
15473de6be7aSRobin Murphy 	},
15483de6be7aSRobin Murphy #endif
15493de6be7aSRobin Murphy };
15503de6be7aSRobin Murphy 
15513de6be7aSRobin Murphy static const struct of_device_id arm_cci_pmu_matches[] = {
15523de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI400_PMU
15533de6be7aSRobin Murphy 	{
15543de6be7aSRobin Murphy 		.compatible = "arm,cci-400-pmu",
15553de6be7aSRobin Murphy 		.data	= NULL,
15563de6be7aSRobin Murphy 	},
15573de6be7aSRobin Murphy 	{
15583de6be7aSRobin Murphy 		.compatible = "arm,cci-400-pmu,r0",
15593de6be7aSRobin Murphy 		.data	= &cci_pmu_models[CCI400_R0],
15603de6be7aSRobin Murphy 	},
15613de6be7aSRobin Murphy 	{
15623de6be7aSRobin Murphy 		.compatible = "arm,cci-400-pmu,r1",
15633de6be7aSRobin Murphy 		.data	= &cci_pmu_models[CCI400_R1],
15643de6be7aSRobin Murphy 	},
15653de6be7aSRobin Murphy #endif
15663de6be7aSRobin Murphy #ifdef CONFIG_ARM_CCI5xx_PMU
15673de6be7aSRobin Murphy 	{
15683de6be7aSRobin Murphy 		.compatible = "arm,cci-500-pmu,r0",
15693de6be7aSRobin Murphy 		.data = &cci_pmu_models[CCI500_R0],
15703de6be7aSRobin Murphy 	},
15713de6be7aSRobin Murphy 	{
15723de6be7aSRobin Murphy 		.compatible = "arm,cci-550-pmu,r0",
15733de6be7aSRobin Murphy 		.data = &cci_pmu_models[CCI550_R0],
15743de6be7aSRobin Murphy 	},
15753de6be7aSRobin Murphy #endif
15763de6be7aSRobin Murphy 	{},
15773de6be7aSRobin Murphy };
15788b0c93c2SRobin Murphy MODULE_DEVICE_TABLE(of, arm_cci_pmu_matches);
15793de6be7aSRobin Murphy 
is_duplicate_irq(int irq,int * irqs,int nr_irqs)15803de6be7aSRobin Murphy static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
15813de6be7aSRobin Murphy {
15823de6be7aSRobin Murphy 	int i;
15833de6be7aSRobin Murphy 
15843de6be7aSRobin Murphy 	for (i = 0; i < nr_irqs; i++)
15853de6be7aSRobin Murphy 		if (irq == irqs[i])
15863de6be7aSRobin Murphy 			return true;
15873de6be7aSRobin Murphy 
15883de6be7aSRobin Murphy 	return false;
15893de6be7aSRobin Murphy }
15903de6be7aSRobin Murphy 
cci_pmu_alloc(struct device * dev)159132837954SRobin Murphy static struct cci_pmu *cci_pmu_alloc(struct device *dev)
15923de6be7aSRobin Murphy {
15933de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu;
15943de6be7aSRobin Murphy 	const struct cci_pmu_model *model;
15953de6be7aSRobin Murphy 
15963de6be7aSRobin Murphy 	/*
15973de6be7aSRobin Murphy 	 * All allocations are devm_* hence we don't have to free
15983de6be7aSRobin Murphy 	 * them explicitly on an error, as it would end up in driver
15993de6be7aSRobin Murphy 	 * detach.
16003de6be7aSRobin Murphy 	 */
1601e9c112c9SRobin Murphy 	cci_pmu = devm_kzalloc(dev, sizeof(*cci_pmu), GFP_KERNEL);
1602e9c112c9SRobin Murphy 	if (!cci_pmu)
1603e9c112c9SRobin Murphy 		return ERR_PTR(-ENOMEM);
1604e9c112c9SRobin Murphy 
1605e9c112c9SRobin Murphy 	cci_pmu->ctrl_base = *(void __iomem **)dev->platform_data;
1606e9c112c9SRobin Murphy 
160732837954SRobin Murphy 	model = of_device_get_match_data(dev);
16083de6be7aSRobin Murphy 	if (!model) {
160932837954SRobin Murphy 		dev_warn(dev,
161032837954SRobin Murphy 			 "DEPRECATED compatible property, requires secure access to CCI registers");
1611e9c112c9SRobin Murphy 		model = probe_cci_model(cci_pmu);
161232837954SRobin Murphy 	}
161332837954SRobin Murphy 	if (!model) {
161432837954SRobin Murphy 		dev_warn(dev, "CCI PMU version not supported\n");
16153de6be7aSRobin Murphy 		return ERR_PTR(-ENODEV);
16163de6be7aSRobin Murphy 	}
16173de6be7aSRobin Murphy 
16183de6be7aSRobin Murphy 	cci_pmu->model = model;
161932837954SRobin Murphy 	cci_pmu->irqs = devm_kcalloc(dev, CCI_PMU_MAX_HW_CNTRS(model),
16203de6be7aSRobin Murphy 					sizeof(*cci_pmu->irqs), GFP_KERNEL);
16213de6be7aSRobin Murphy 	if (!cci_pmu->irqs)
16223de6be7aSRobin Murphy 		return ERR_PTR(-ENOMEM);
162332837954SRobin Murphy 	cci_pmu->hw_events.events = devm_kcalloc(dev,
16243de6be7aSRobin Murphy 					     CCI_PMU_MAX_HW_CNTRS(model),
16253de6be7aSRobin Murphy 					     sizeof(*cci_pmu->hw_events.events),
16263de6be7aSRobin Murphy 					     GFP_KERNEL);
16273de6be7aSRobin Murphy 	if (!cci_pmu->hw_events.events)
16283de6be7aSRobin Murphy 		return ERR_PTR(-ENOMEM);
16290e35850bSChristophe JAILLET 	cci_pmu->hw_events.used_mask = devm_bitmap_zalloc(dev,
16300e35850bSChristophe JAILLET 							  CCI_PMU_MAX_HW_CNTRS(model),
16313de6be7aSRobin Murphy 							  GFP_KERNEL);
16323de6be7aSRobin Murphy 	if (!cci_pmu->hw_events.used_mask)
16333de6be7aSRobin Murphy 		return ERR_PTR(-ENOMEM);
16343de6be7aSRobin Murphy 
16353de6be7aSRobin Murphy 	return cci_pmu;
16363de6be7aSRobin Murphy }
16373de6be7aSRobin Murphy 
cci_pmu_probe(struct platform_device * pdev)16383de6be7aSRobin Murphy static int cci_pmu_probe(struct platform_device *pdev)
16393de6be7aSRobin Murphy {
16403de6be7aSRobin Murphy 	struct cci_pmu *cci_pmu;
16413de6be7aSRobin Murphy 	int i, ret, irq;
16423de6be7aSRobin Murphy 
164332837954SRobin Murphy 	cci_pmu = cci_pmu_alloc(&pdev->dev);
16443de6be7aSRobin Murphy 	if (IS_ERR(cci_pmu))
16453de6be7aSRobin Murphy 		return PTR_ERR(cci_pmu);
16463de6be7aSRobin Murphy 
1647504db0f8SYueHaibing 	cci_pmu->base = devm_platform_ioremap_resource(pdev, 0);
16483de6be7aSRobin Murphy 	if (IS_ERR(cci_pmu->base))
16493de6be7aSRobin Murphy 		return -ENOMEM;
16503de6be7aSRobin Murphy 
16513de6be7aSRobin Murphy 	/*
16523de6be7aSRobin Murphy 	 * CCI PMU has one overflow interrupt per counter; but some may be tied
16533de6be7aSRobin Murphy 	 * together to a common interrupt.
16543de6be7aSRobin Murphy 	 */
16553de6be7aSRobin Murphy 	cci_pmu->nr_irqs = 0;
16563de6be7aSRobin Murphy 	for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
16573de6be7aSRobin Murphy 		irq = platform_get_irq(pdev, i);
16583de6be7aSRobin Murphy 		if (irq < 0)
16593de6be7aSRobin Murphy 			break;
16603de6be7aSRobin Murphy 
16613de6be7aSRobin Murphy 		if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
16623de6be7aSRobin Murphy 			continue;
16633de6be7aSRobin Murphy 
16643de6be7aSRobin Murphy 		cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
16653de6be7aSRobin Murphy 	}
16663de6be7aSRobin Murphy 
16673de6be7aSRobin Murphy 	/*
16683de6be7aSRobin Murphy 	 * Ensure that the device tree has as many interrupts as the number
16693de6be7aSRobin Murphy 	 * of counters.
16703de6be7aSRobin Murphy 	 */
16713de6be7aSRobin Murphy 	if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
16723de6be7aSRobin Murphy 		dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
16733de6be7aSRobin Murphy 			i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
16743de6be7aSRobin Murphy 		return -EINVAL;
16753de6be7aSRobin Murphy 	}
16763de6be7aSRobin Murphy 
16773de6be7aSRobin Murphy 	raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
16783de6be7aSRobin Murphy 	mutex_init(&cci_pmu->reserve_mutex);
16793de6be7aSRobin Murphy 	atomic_set(&cci_pmu->active_events, 0);
16803de6be7aSRobin Murphy 
16810d2e2a82SRobin Murphy 	cci_pmu->cpu = raw_smp_processor_id();
16820d2e2a82SRobin Murphy 	g_cci_pmu = cci_pmu;
168303057f26SRobin Murphy 	cpuhp_setup_state_nocalls(CPUHP_AP_PERF_ARM_CCI_ONLINE,
168403057f26SRobin Murphy 				  "perf/arm/cci:online", NULL,
168503057f26SRobin Murphy 				  cci_pmu_offline_cpu);
16860d2e2a82SRobin Murphy 
16870d2e2a82SRobin Murphy 	ret = cci_pmu_init(cci_pmu, pdev);
16880d2e2a82SRobin Murphy 	if (ret)
16890d2e2a82SRobin Murphy 		goto error_pmu_init;
16900d2e2a82SRobin Murphy 
16913de6be7aSRobin Murphy 	pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
16923de6be7aSRobin Murphy 	return 0;
16930d2e2a82SRobin Murphy 
16940d2e2a82SRobin Murphy error_pmu_init:
16950d2e2a82SRobin Murphy 	cpuhp_remove_state(CPUHP_AP_PERF_ARM_CCI_ONLINE);
16960d2e2a82SRobin Murphy 	g_cci_pmu = NULL;
16970d2e2a82SRobin Murphy 	return ret;
16983de6be7aSRobin Murphy }
16993de6be7aSRobin Murphy 
cci_pmu_remove(struct platform_device * pdev)17008b0c93c2SRobin Murphy static int cci_pmu_remove(struct platform_device *pdev)
17018b0c93c2SRobin Murphy {
17028b0c93c2SRobin Murphy 	if (!g_cci_pmu)
17038b0c93c2SRobin Murphy 		return 0;
17048b0c93c2SRobin Murphy 
17058b0c93c2SRobin Murphy 	cpuhp_remove_state(CPUHP_AP_PERF_ARM_CCI_ONLINE);
17068b0c93c2SRobin Murphy 	perf_pmu_unregister(&g_cci_pmu->pmu);
17078b0c93c2SRobin Murphy 	g_cci_pmu = NULL;
17088b0c93c2SRobin Murphy 
17098b0c93c2SRobin Murphy 	return 0;
17108b0c93c2SRobin Murphy }
17118b0c93c2SRobin Murphy 
17123de6be7aSRobin Murphy static struct platform_driver cci_pmu_driver = {
17133de6be7aSRobin Murphy 	.driver = {
17143de6be7aSRobin Murphy 		   .name = DRIVER_NAME,
17153de6be7aSRobin Murphy 		   .of_match_table = arm_cci_pmu_matches,
1716f32ed8ebSQi Liu 		   .suppress_bind_attrs = true,
17173de6be7aSRobin Murphy 		  },
17183de6be7aSRobin Murphy 	.probe = cci_pmu_probe,
17198b0c93c2SRobin Murphy 	.remove = cci_pmu_remove,
17203de6be7aSRobin Murphy };
17213de6be7aSRobin Murphy 
17228b0c93c2SRobin Murphy module_platform_driver(cci_pmu_driver);
172375dc3441SRobin Murphy MODULE_LICENSE("GPL v2");
17243de6be7aSRobin Murphy MODULE_DESCRIPTION("ARM CCI PMU support");
1725