blob: afe64ab2c06bbcf02044a7cbfe34b850c6d19925 [file] [log] [blame]
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001/*
2 * CCI cache coherent interconnect driver
3 *
4 * Copyright (C) 2013 ARM Ltd.
5 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/arm-cci.h>
18#include <linux/io.h>
Mark Rutlandc6f85cb2014-06-30 12:20:21 +010019#include <linux/interrupt.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010020#include <linux/module.h>
21#include <linux/of_address.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010022#include <linux/of_irq.h>
23#include <linux/of_platform.h>
Mark Rutlandc6f85cb2014-06-30 12:20:21 +010024#include <linux/perf_event.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010025#include <linux/platform_device.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010026#include <linux/slab.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010027#include <linux/spinlock.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010028
29#include <asm/cacheflush.h>
30#include <asm/smp_plat.h>
31
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000032static void __iomem *cci_ctrl_base;
33static unsigned long cci_ctrl_phys;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010034
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +000035#ifdef CONFIG_ARM_CCI400_PORT_CTRL
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010036struct cci_nb_ports {
37 unsigned int nb_ace;
38 unsigned int nb_ace_lite;
39};
40
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000041static const struct cci_nb_ports cci400_ports = {
42 .nb_ace = 2,
43 .nb_ace_lite = 3
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010044};
45
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +000046#define CCI400_PORTS_DATA (&cci400_ports)
47#else
48#define CCI400_PORTS_DATA (NULL)
49#endif
50
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000051static const struct of_device_id arm_cci_matches[] = {
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +000052#ifdef CONFIG_ARM_CCI400_COMMON
53 {.compatible = "arm,cci-400", .data = CCI400_PORTS_DATA },
54#endif
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +000055#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +010056 { .compatible = "arm,cci-500", },
Suzuki K Poulosed7dd5fd2016-02-23 10:49:55 +000057 { .compatible = "arm,cci-550", },
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +010058#endif
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000059 {},
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010060};
61
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +010062#ifdef CONFIG_ARM_CCI_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +010063
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +010064#define DRIVER_NAME "ARM-CCI"
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000065#define DRIVER_NAME_PMU DRIVER_NAME " PMU"
66
Punit Agrawalb91c8f22013-08-22 14:41:51 +010067#define CCI_PMCR 0x0100
68#define CCI_PID2 0x0fe8
69
70#define CCI_PMCR_CEN 0x00000001
71#define CCI_PMCR_NCNT_MASK 0x0000f800
72#define CCI_PMCR_NCNT_SHIFT 11
73
74#define CCI_PID2_REV_MASK 0xf0
75#define CCI_PID2_REV_SHIFT 4
76
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000077#define CCI_PMU_EVT_SEL 0x000
78#define CCI_PMU_CNTR 0x004
79#define CCI_PMU_CNTR_CTRL 0x008
80#define CCI_PMU_OVRFLW 0x00c
81
82#define CCI_PMU_OVRFLW_FLAG 1
83
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +010084#define CCI_PMU_CNTR_SIZE(model) ((model)->cntr_size)
85#define CCI_PMU_CNTR_BASE(model, idx) ((idx) * CCI_PMU_CNTR_SIZE(model))
86#define CCI_PMU_CNTR_MASK ((1ULL << 32) -1)
87#define CCI_PMU_CNTR_LAST(cci_pmu) (cci_pmu->num_cntrs - 1)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000088
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +010089#define CCI_PMU_MAX_HW_CNTRS(model) \
90 ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000091
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +000092/* Types of interfaces that can generate events */
93enum {
94 CCI_IF_SLAVE,
95 CCI_IF_MASTER,
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +000096#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +010097 CCI_IF_GLOBAL,
98#endif
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +000099 CCI_IF_MAX,
100};
101
102struct event_range {
103 u32 min;
104 u32 max;
105};
106
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000107struct cci_pmu_hw_events {
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100108 struct perf_event **events;
109 unsigned long *used_mask;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000110 raw_spinlock_t pmu_lock;
111};
112
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100113struct cci_pmu;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100114/*
115 * struct cci_pmu_model:
116 * @fixed_hw_cntrs - Number of fixed event counters
117 * @num_hw_cntrs - Maximum number of programmable event counters
118 * @cntr_size - Size of an event counter mapping
119 */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000120struct cci_pmu_model {
121 char *name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100122 u32 fixed_hw_cntrs;
123 u32 num_hw_cntrs;
124 u32 cntr_size;
Mark Rutland5e442eb2016-02-23 10:49:43 +0000125 struct attribute **format_attrs;
126 struct attribute **event_attrs;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000127 struct event_range event_ranges[CCI_IF_MAX];
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100128 int (*validate_hw_event)(struct cci_pmu *, unsigned long);
129 int (*get_event_idx)(struct cci_pmu *, struct cci_pmu_hw_events *, unsigned long);
Suzuki K Poulosefff3f1a2016-02-23 10:49:52 +0000130 void (*write_counters)(struct cci_pmu *, unsigned long *);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000131};
132
133static struct cci_pmu_model cci_pmu_models[];
134
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000135struct cci_pmu {
136 void __iomem *base;
137 struct pmu pmu;
138 int nr_irqs;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100139 int *irqs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000140 unsigned long active_irqs;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000141 const struct cci_pmu_model *model;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000142 struct cci_pmu_hw_events hw_events;
143 struct platform_device *plat_device;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100144 int num_cntrs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000145 atomic_t active_events;
146 struct mutex reserve_mutex;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100147 struct notifier_block cpu_nb;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000148 cpumask_t cpus;
149};
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000150
151#define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
152
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100153enum cci_models {
154#ifdef CONFIG_ARM_CCI400_PMU
155 CCI400_R0,
156 CCI400_R1,
157#endif
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000158#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100159 CCI500_R0,
Suzuki K Poulosed7dd5fd2016-02-23 10:49:55 +0000160 CCI550_R0,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100161#endif
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100162 CCI_MODEL_MAX
163};
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100164
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000165static void pmu_write_counters(struct cci_pmu *cci_pmu,
166 unsigned long *mask);
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100167static ssize_t cci_pmu_format_show(struct device *dev,
168 struct device_attribute *attr, char *buf);
169static ssize_t cci_pmu_event_show(struct device *dev,
170 struct device_attribute *attr, char *buf);
171
Mark Rutland5e442eb2016-02-23 10:49:43 +0000172#define CCI_EXT_ATTR_ENTRY(_name, _func, _config) \
173 &((struct dev_ext_attribute[]) { \
174 { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config } \
175 })[0].attr.attr
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100176
177#define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
178 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_format_show, (char *)_config)
179#define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
180 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
181
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100182/* CCI400 PMU Specific definitions */
183
184#ifdef CONFIG_ARM_CCI400_PMU
185
186/* Port ids */
187#define CCI400_PORT_S0 0
188#define CCI400_PORT_S1 1
189#define CCI400_PORT_S2 2
190#define CCI400_PORT_S3 3
191#define CCI400_PORT_S4 4
192#define CCI400_PORT_M0 5
193#define CCI400_PORT_M1 6
194#define CCI400_PORT_M2 7
195
196#define CCI400_R1_PX 5
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100197
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100198/*
199 * Instead of an event id to monitor CCI cycles, a dedicated counter is
200 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
201 * make use of this event in hardware.
202 */
203enum cci400_perf_events {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100204 CCI400_PMU_CYCLES = 0xff
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100205};
206
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100207#define CCI400_PMU_CYCLE_CNTR_IDX 0
208#define CCI400_PMU_CNTR0_IDX 1
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100209
210/*
211 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
212 * ports and bits 4:0 are event codes. There are different event codes
213 * associated with each port type.
214 *
215 * Additionally, the range of events associated with the port types changed
216 * between Rev0 and Rev1.
217 *
218 * The constants below define the range of valid codes for each port type for
219 * the different revisions and are used to validate the event to be monitored.
220 */
221
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100222#define CCI400_PMU_EVENT_MASK 0xffUL
223#define CCI400_PMU_EVENT_SOURCE_SHIFT 5
224#define CCI400_PMU_EVENT_SOURCE_MASK 0x7
225#define CCI400_PMU_EVENT_CODE_SHIFT 0
226#define CCI400_PMU_EVENT_CODE_MASK 0x1f
227#define CCI400_PMU_EVENT_SOURCE(event) \
228 ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
229 CCI400_PMU_EVENT_SOURCE_MASK)
230#define CCI400_PMU_EVENT_CODE(event) \
231 ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100232
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100233#define CCI400_R0_SLAVE_PORT_MIN_EV 0x00
234#define CCI400_R0_SLAVE_PORT_MAX_EV 0x13
235#define CCI400_R0_MASTER_PORT_MIN_EV 0x14
236#define CCI400_R0_MASTER_PORT_MAX_EV 0x1a
237
238#define CCI400_R1_SLAVE_PORT_MIN_EV 0x00
239#define CCI400_R1_SLAVE_PORT_MAX_EV 0x14
240#define CCI400_R1_MASTER_PORT_MIN_EV 0x00
241#define CCI400_R1_MASTER_PORT_MAX_EV 0x11
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100242
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100243#define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
244 CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
245 (unsigned long)_config)
246
247static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
248 struct device_attribute *attr, char *buf);
249
Mark Rutland5e442eb2016-02-23 10:49:43 +0000250static struct attribute *cci400_pmu_format_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100251 CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
252 CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-7"),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000253 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100254};
255
Mark Rutland5e442eb2016-02-23 10:49:43 +0000256static struct attribute *cci400_r0_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100257 /* Slave events */
258 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
259 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
260 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
261 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
262 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
263 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
264 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
265 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
266 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
267 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
268 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
269 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
270 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
271 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
272 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
273 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
274 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
275 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
276 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
277 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
278 /* Master events */
279 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x14),
280 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard, 0x15),
281 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard, 0x16),
282 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full, 0x17),
283 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x18),
284 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x19),
285 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full, 0x1A),
286 /* Special event for cycles counter */
287 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000288 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100289};
290
Mark Rutland5e442eb2016-02-23 10:49:43 +0000291static struct attribute *cci400_r1_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100292 /* Slave events */
293 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
294 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
295 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
296 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
297 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
298 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
299 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
300 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
301 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
302 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
303 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
304 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
305 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
306 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
307 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
308 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
309 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
310 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
311 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
312 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
313 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard, 0x14),
314 /* Master events */
315 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x0),
316 CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard, 0x1),
317 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard, 0x2),
318 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full, 0x3),
319 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x4),
320 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x5),
321 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full, 0x6),
322 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full, 0x7),
323 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full, 0x8),
324 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0, 0x9),
325 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1, 0xA),
326 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2, 0xB),
327 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3, 0xC),
328 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0, 0xD),
329 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1, 0xE),
330 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2, 0xF),
331 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3, 0x10),
332 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard, 0x11),
333 /* Special event for cycles counter */
334 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000335 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100336};
337
338static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
339 struct device_attribute *attr, char *buf)
340{
341 struct dev_ext_attribute *eattr = container_of(attr,
342 struct dev_ext_attribute, attr);
343 return snprintf(buf, PAGE_SIZE, "config=0x%lx\n", (unsigned long)eattr->var);
344}
345
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100346static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
347 struct cci_pmu_hw_events *hw,
348 unsigned long cci_event)
349{
350 int idx;
351
352 /* cycles event idx is fixed */
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100353 if (cci_event == CCI400_PMU_CYCLES) {
354 if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100355 return -EAGAIN;
356
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100357 return CCI400_PMU_CYCLE_CNTR_IDX;
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100358 }
359
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100360 for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100361 if (!test_and_set_bit(idx, hw->used_mask))
362 return idx;
363
364 /* No counters available */
365 return -EAGAIN;
366}
367
368static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100369{
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100370 u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
371 u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000372 int if_type;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100373
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100374 if (hw_event & ~CCI400_PMU_EVENT_MASK)
Suzuki K. Poulose874c5712015-03-18 12:24:42 +0000375 return -ENOENT;
376
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100377 if (hw_event == CCI400_PMU_CYCLES)
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100378 return hw_event;
379
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100380 switch (ev_source) {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100381 case CCI400_PORT_S0:
382 case CCI400_PORT_S1:
383 case CCI400_PORT_S2:
384 case CCI400_PORT_S3:
385 case CCI400_PORT_S4:
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100386 /* Slave Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000387 if_type = CCI_IF_SLAVE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100388 break;
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100389 case CCI400_PORT_M0:
390 case CCI400_PORT_M1:
391 case CCI400_PORT_M2:
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100392 /* Master Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000393 if_type = CCI_IF_MASTER;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100394 break;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000395 default:
396 return -ENOENT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100397 }
398
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100399 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
400 ev_code <= cci_pmu->model->event_ranges[if_type].max)
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000401 return hw_event;
402
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100403 return -ENOENT;
404}
405
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100406static int probe_cci400_revision(void)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000407{
408 int rev;
409 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
410 rev >>= CCI_PID2_REV_SHIFT;
411
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100412 if (rev < CCI400_R1_PX)
413 return CCI400_R0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000414 else
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100415 return CCI400_R1;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000416}
417
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000418static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000419{
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000420 if (platform_has_secure_cci_access())
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100421 return &cci_pmu_models[probe_cci400_revision()];
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000422 return NULL;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000423}
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100424#else /* !CONFIG_ARM_CCI400_PMU */
425static inline struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
426{
427 return NULL;
428}
429#endif /* CONFIG_ARM_CCI400_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000430
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000431#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100432
433/*
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000434 * CCI5xx PMU event id is an 9-bit value made of two parts.
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100435 * bits [8:5] - Source for the event
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100436 * bits [4:0] - Event code (specific to type of interface)
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000437 *
438 *
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100439 */
440
441/* Port ids */
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000442#define CCI5xx_PORT_S0 0x0
443#define CCI5xx_PORT_S1 0x1
444#define CCI5xx_PORT_S2 0x2
445#define CCI5xx_PORT_S3 0x3
446#define CCI5xx_PORT_S4 0x4
447#define CCI5xx_PORT_S5 0x5
448#define CCI5xx_PORT_S6 0x6
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100449
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000450#define CCI5xx_PORT_M0 0x8
451#define CCI5xx_PORT_M1 0x9
452#define CCI5xx_PORT_M2 0xa
453#define CCI5xx_PORT_M3 0xb
454#define CCI5xx_PORT_M4 0xc
455#define CCI5xx_PORT_M5 0xd
Suzuki K Poulosed7dd5fd2016-02-23 10:49:55 +0000456#define CCI5xx_PORT_M6 0xe
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100457
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000458#define CCI5xx_PORT_GLOBAL 0xf
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100459
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000460#define CCI5xx_PMU_EVENT_MASK 0x1ffUL
461#define CCI5xx_PMU_EVENT_SOURCE_SHIFT 0x5
462#define CCI5xx_PMU_EVENT_SOURCE_MASK 0xf
463#define CCI5xx_PMU_EVENT_CODE_SHIFT 0x0
464#define CCI5xx_PMU_EVENT_CODE_MASK 0x1f
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100465
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000466#define CCI5xx_PMU_EVENT_SOURCE(event) \
467 ((event >> CCI5xx_PMU_EVENT_SOURCE_SHIFT) & CCI5xx_PMU_EVENT_SOURCE_MASK)
468#define CCI5xx_PMU_EVENT_CODE(event) \
469 ((event >> CCI5xx_PMU_EVENT_CODE_SHIFT) & CCI5xx_PMU_EVENT_CODE_MASK)
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100470
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000471#define CCI5xx_SLAVE_PORT_MIN_EV 0x00
472#define CCI5xx_SLAVE_PORT_MAX_EV 0x1f
473#define CCI5xx_MASTER_PORT_MIN_EV 0x00
474#define CCI5xx_MASTER_PORT_MAX_EV 0x06
475#define CCI5xx_GLOBAL_PORT_MIN_EV 0x00
476#define CCI5xx_GLOBAL_PORT_MAX_EV 0x0f
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100477
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100478
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000479#define CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
480 CCI_EXT_ATTR_ENTRY(_name, cci5xx_pmu_global_event_show, \
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100481 (unsigned long) _config)
482
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000483static ssize_t cci5xx_pmu_global_event_show(struct device *dev,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100484 struct device_attribute *attr, char *buf);
485
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000486static struct attribute *cci5xx_pmu_format_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100487 CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
488 CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-8"),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000489 NULL,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100490};
491
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000492static struct attribute *cci5xx_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100493 /* Slave events */
494 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_arvalid, 0x0),
495 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_dev, 0x1),
496 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_nonshareable, 0x2),
497 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_non_alloc, 0x3),
498 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_alloc, 0x4),
499 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_invalidate, 0x5),
500 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maint, 0x6),
501 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
502 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rval, 0x8),
503 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rlast_snoop, 0x9),
504 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_awalid, 0xA),
505 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_dev, 0xB),
506 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_non_shareable, 0xC),
507 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wb, 0xD),
508 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wlu, 0xE),
509 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wunique, 0xF),
510 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_evict, 0x10),
511 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_wrevict, 0x11),
512 CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_beat, 0x12),
513 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_acvalid, 0x13),
514 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_read, 0x14),
515 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_clean, 0x15),
516 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_data_transfer_low, 0x16),
517 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_arvalid, 0x17),
518 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall, 0x18),
519 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall, 0x19),
520 CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_stall, 0x1A),
521 CCI_EVENT_EXT_ATTR_ENTRY(si_w_resp_stall, 0x1B),
522 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_stall, 0x1C),
523 CCI_EVENT_EXT_ATTR_ENTRY(si_s_data_stall, 0x1D),
524 CCI_EVENT_EXT_ATTR_ENTRY(si_rq_stall_ot_limit, 0x1E),
525 CCI_EVENT_EXT_ATTR_ENTRY(si_r_stall_arbit, 0x1F),
526
527 /* Master events */
528 CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_beat_any, 0x0),
529 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_beat_any, 0x1),
530 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall, 0x2),
531 CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_stall, 0x3),
532 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall, 0x4),
533 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_stall, 0x5),
534 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_resp_stall, 0x6),
535
536 /* Global events */
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000537 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1, 0x0),
538 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3, 0x1),
539 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5, 0x2),
540 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7, 0x3),
541 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1, 0x4),
542 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3, 0x5),
543 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5, 0x6),
544 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7, 0x7),
545 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation, 0x8),
546 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy, 0x9),
547 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full, 0xA),
548 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq, 0xB),
549 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs, 0xC),
550 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard, 0xD),
551 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snopp_rq_stall_tt_full, 0xE),
552 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot, 0xF),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000553 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100554};
555
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000556static ssize_t cci5xx_pmu_global_event_show(struct device *dev,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100557 struct device_attribute *attr, char *buf)
558{
559 struct dev_ext_attribute *eattr = container_of(attr,
560 struct dev_ext_attribute, attr);
561 /* Global events have single fixed source code */
562 return snprintf(buf, PAGE_SIZE, "event=0x%lx,source=0x%x\n",
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000563 (unsigned long)eattr->var, CCI5xx_PORT_GLOBAL);
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100564}
565
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000566/*
567 * CCI500 provides 8 independent event counters that can count
568 * any of the events available.
569 * CCI500 PMU event source ids
570 * 0x0-0x6 - Slave interfaces
571 * 0x8-0xD - Master interfaces
572 * 0xf - Global Events
573 * 0x7,0xe - Reserved
574 */
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100575static int cci500_validate_hw_event(struct cci_pmu *cci_pmu,
576 unsigned long hw_event)
577{
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000578 u32 ev_source = CCI5xx_PMU_EVENT_SOURCE(hw_event);
579 u32 ev_code = CCI5xx_PMU_EVENT_CODE(hw_event);
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100580 int if_type;
581
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000582 if (hw_event & ~CCI5xx_PMU_EVENT_MASK)
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100583 return -ENOENT;
584
585 switch (ev_source) {
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000586 case CCI5xx_PORT_S0:
587 case CCI5xx_PORT_S1:
588 case CCI5xx_PORT_S2:
589 case CCI5xx_PORT_S3:
590 case CCI5xx_PORT_S4:
591 case CCI5xx_PORT_S5:
592 case CCI5xx_PORT_S6:
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100593 if_type = CCI_IF_SLAVE;
594 break;
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000595 case CCI5xx_PORT_M0:
596 case CCI5xx_PORT_M1:
597 case CCI5xx_PORT_M2:
598 case CCI5xx_PORT_M3:
599 case CCI5xx_PORT_M4:
600 case CCI5xx_PORT_M5:
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100601 if_type = CCI_IF_MASTER;
602 break;
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000603 case CCI5xx_PORT_GLOBAL:
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100604 if_type = CCI_IF_GLOBAL;
605 break;
606 default:
607 return -ENOENT;
608 }
609
610 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
611 ev_code <= cci_pmu->model->event_ranges[if_type].max)
612 return hw_event;
613
614 return -ENOENT;
615}
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000616
Suzuki K Poulosed7dd5fd2016-02-23 10:49:55 +0000617/*
618 * CCI550 provides 8 independent event counters that can count
619 * any of the events available.
620 * CCI550 PMU event source ids
621 * 0x0-0x6 - Slave interfaces
622 * 0x8-0xe - Master interfaces
623 * 0xf - Global Events
624 * 0x7 - Reserved
625 */
626static int cci550_validate_hw_event(struct cci_pmu *cci_pmu,
627 unsigned long hw_event)
628{
629 u32 ev_source = CCI5xx_PMU_EVENT_SOURCE(hw_event);
630 u32 ev_code = CCI5xx_PMU_EVENT_CODE(hw_event);
631 int if_type;
632
633 if (hw_event & ~CCI5xx_PMU_EVENT_MASK)
634 return -ENOENT;
635
636 switch (ev_source) {
637 case CCI5xx_PORT_S0:
638 case CCI5xx_PORT_S1:
639 case CCI5xx_PORT_S2:
640 case CCI5xx_PORT_S3:
641 case CCI5xx_PORT_S4:
642 case CCI5xx_PORT_S5:
643 case CCI5xx_PORT_S6:
644 if_type = CCI_IF_SLAVE;
645 break;
646 case CCI5xx_PORT_M0:
647 case CCI5xx_PORT_M1:
648 case CCI5xx_PORT_M2:
649 case CCI5xx_PORT_M3:
650 case CCI5xx_PORT_M4:
651 case CCI5xx_PORT_M5:
652 case CCI5xx_PORT_M6:
653 if_type = CCI_IF_MASTER;
654 break;
655 case CCI5xx_PORT_GLOBAL:
656 if_type = CCI_IF_GLOBAL;
657 break;
658 default:
659 return -ENOENT;
660 }
661
662 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
663 ev_code <= cci_pmu->model->event_ranges[if_type].max)
664 return hw_event;
665
666 return -ENOENT;
667}
668
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000669#endif /* CONFIG_ARM_CCI5xx_PMU */
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100670
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000671/*
672 * Program the CCI PMU counters which have PERF_HES_ARCH set
673 * with the event period and mark them ready before we enable
674 * PMU.
675 */
Will Deaconceb49512016-02-26 16:12:43 +0000676static void cci_pmu_sync_counters(struct cci_pmu *cci_pmu)
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000677{
678 int i;
679 struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
680
681 DECLARE_BITMAP(mask, cci_pmu->num_cntrs);
682
683 bitmap_zero(mask, cci_pmu->num_cntrs);
684 for_each_set_bit(i, cci_pmu->hw_events.used_mask, cci_pmu->num_cntrs) {
685 struct perf_event *event = cci_hw->events[i];
686
687 if (WARN_ON(!event))
688 continue;
689
690 /* Leave the events which are not counting */
691 if (event->hw.state & PERF_HES_STOPPED)
692 continue;
693 if (event->hw.state & PERF_HES_ARCH) {
694 set_bit(i, mask);
695 event->hw.state &= ~PERF_HES_ARCH;
696 }
697 }
698
699 pmu_write_counters(cci_pmu, mask);
700}
701
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000702/* Should be called with cci_pmu->hw_events->pmu_lock held */
Suzuki K Poulose11300022016-02-23 10:49:51 +0000703static void __cci_pmu_enable_nosync(struct cci_pmu *cci_pmu)
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000704{
705 u32 val;
706
707 /* Enable all the PMU counters. */
708 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
709 writel(val, cci_ctrl_base + CCI_PMCR);
710}
711
712/* Should be called with cci_pmu->hw_events->pmu_lock held */
Suzuki K Poulose11300022016-02-23 10:49:51 +0000713static void __cci_pmu_enable_sync(struct cci_pmu *cci_pmu)
714{
715 cci_pmu_sync_counters(cci_pmu);
716 __cci_pmu_enable_nosync(cci_pmu);
717}
718
719/* Should be called with cci_pmu->hw_events->pmu_lock held */
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000720static void __cci_pmu_disable(void)
721{
722 u32 val;
723
724 /* Disable all the PMU counters. */
725 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
726 writel(val, cci_ctrl_base + CCI_PMCR);
727}
728
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100729static ssize_t cci_pmu_format_show(struct device *dev,
730 struct device_attribute *attr, char *buf)
731{
732 struct dev_ext_attribute *eattr = container_of(attr,
733 struct dev_ext_attribute, attr);
734 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)eattr->var);
735}
736
737static ssize_t cci_pmu_event_show(struct device *dev,
738 struct device_attribute *attr, char *buf)
739{
740 struct dev_ext_attribute *eattr = container_of(attr,
741 struct dev_ext_attribute, attr);
742 /* source parameter is mandatory for normal PMU events */
743 return snprintf(buf, PAGE_SIZE, "source=?,event=0x%lx\n",
744 (unsigned long)eattr->var);
745}
746
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100747static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100748{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100749 return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100750}
751
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100752static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100753{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100754 return readl_relaxed(cci_pmu->base +
755 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100756}
757
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100758static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
759 int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100760{
Will Deacon6ec30702016-02-26 16:13:37 +0000761 writel_relaxed(value, cci_pmu->base +
762 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100763}
764
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100765static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100766{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100767 pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100768}
769
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100770static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100771{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100772 pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100773}
774
Suzuki K Poulose1ce63112016-02-23 10:49:49 +0000775static bool __maybe_unused
776pmu_counter_is_enabled(struct cci_pmu *cci_pmu, int idx)
777{
778 return (pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR_CTRL) & 0x1) != 0;
779}
780
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100781static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100782{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100783 pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100784}
785
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100786/*
Suzuki K Poulosecea16f82016-02-23 10:49:50 +0000787 * For all counters on the CCI-PMU, disable any 'enabled' counters,
788 * saving the changed counters in the mask, so that we can restore
789 * it later using pmu_restore_counters. The mask is private to the
790 * caller. We cannot rely on the used_mask maintained by the CCI_PMU
791 * as it only tells us if the counter is assigned to perf_event or not.
792 * The state of the perf_event cannot be locked by the PMU layer, hence
793 * we check the individual counter status (which can be locked by
794 * cci_pm->hw_events->pmu_lock).
795 *
796 * @mask should be initialised to empty by the caller.
797 */
798static void __maybe_unused
799pmu_save_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
800{
801 int i;
802
803 for (i = 0; i < cci_pmu->num_cntrs; i++) {
804 if (pmu_counter_is_enabled(cci_pmu, i)) {
805 set_bit(i, mask);
806 pmu_disable_counter(cci_pmu, i);
807 }
808 }
809}
810
811/*
812 * Restore the status of the counters. Reversal of the pmu_save_counters().
813 * For each counter set in the mask, enable the counter back.
814 */
815static void __maybe_unused
816pmu_restore_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
817{
818 int i;
819
820 for_each_set_bit(i, mask, cci_pmu->num_cntrs)
821 pmu_enable_counter(cci_pmu, i);
822}
823
824/*
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100825 * Returns the number of programmable counters actually implemented
826 * by the cci
827 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100828static u32 pmu_get_max_counters(void)
829{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100830 return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
831 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100832}
833
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100834static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100835{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100836 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100837 unsigned long cci_event = event->hw.config_base;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100838 int idx;
839
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100840 if (cci_pmu->model->get_event_idx)
841 return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100842
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100843 /* Generic code to find an unused idx from the mask */
844 for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100845 if (!test_and_set_bit(idx, hw->used_mask))
846 return idx;
847
848 /* No counters available */
849 return -EAGAIN;
850}
851
852static int pmu_map_event(struct perf_event *event)
853{
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100854 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100855
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100856 if (event->attr.type < PERF_TYPE_MAX ||
857 !cci_pmu->model->validate_hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100858 return -ENOENT;
859
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100860 return cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100861}
862
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100863static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100864{
865 int i;
866 struct platform_device *pmu_device = cci_pmu->plat_device;
867
868 if (unlikely(!pmu_device))
869 return -ENODEV;
870
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100871 if (cci_pmu->nr_irqs < 1) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100872 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
873 return -ENODEV;
874 }
875
876 /*
877 * Register all available CCI PMU interrupts. In the interrupt handler
878 * we iterate over the counters checking for interrupt source (the
879 * overflowing counter) and clear it.
880 *
881 * This should allow handling of non-unique interrupt for the counters.
882 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100883 for (i = 0; i < cci_pmu->nr_irqs; i++) {
884 int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100885 "arm-cci-pmu", cci_pmu);
886 if (err) {
887 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100888 cci_pmu->irqs[i]);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100889 return err;
890 }
891
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100892 set_bit(i, &cci_pmu->active_irqs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100893 }
894
895 return 0;
896}
897
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100898static void pmu_free_irq(struct cci_pmu *cci_pmu)
899{
900 int i;
901
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100902 for (i = 0; i < cci_pmu->nr_irqs; i++) {
903 if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100904 continue;
905
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100906 free_irq(cci_pmu->irqs[i], cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100907 }
908}
909
910static u32 pmu_read_counter(struct perf_event *event)
911{
912 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
913 struct hw_perf_event *hw_counter = &event->hw;
914 int idx = hw_counter->idx;
915 u32 value;
916
917 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
918 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
919 return 0;
920 }
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100921 value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100922
923 return value;
924}
925
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000926static void pmu_write_counter(struct cci_pmu *cci_pmu, u32 value, int idx)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100927{
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000928 pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100929}
930
Suzuki K Poulosefff3f1a2016-02-23 10:49:52 +0000931static void __pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
Suzuki K Poulosea53eb5c2016-02-23 10:49:45 +0000932{
933 int i;
934 struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
935
936 for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
937 struct perf_event *event = cci_hw->events[i];
938
939 if (WARN_ON(!event))
940 continue;
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000941 pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
Suzuki K Poulosea53eb5c2016-02-23 10:49:45 +0000942 }
943}
944
Suzuki K Poulosefff3f1a2016-02-23 10:49:52 +0000945static void pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
946{
947 if (cci_pmu->model->write_counters)
948 cci_pmu->model->write_counters(cci_pmu, mask);
949 else
950 __pmu_write_counters(cci_pmu, mask);
951}
952
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000953#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000954
955/*
Suzuki K Poulosed7dd5fd2016-02-23 10:49:55 +0000956 * CCI-500/CCI-550 has advanced power saving policies, which could gate the
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000957 * clocks to the PMU counters, which makes the writes to them ineffective.
958 * The only way to write to those counters is when the global counters
959 * are enabled and the particular counter is enabled.
960 *
961 * So we do the following :
962 *
963 * 1) Disable all the PMU counters, saving their current state
964 * 2) Enable the global PMU profiling, now that all counters are
965 * disabled.
966 *
967 * For each counter to be programmed, repeat steps 3-7:
968 *
969 * 3) Write an invalid event code to the event control register for the
970 counter, so that the counters are not modified.
971 * 4) Enable the counter control for the counter.
972 * 5) Set the counter value
973 * 6) Disable the counter
974 * 7) Restore the event in the target counter
975 *
976 * 8) Disable the global PMU.
977 * 9) Restore the status of the rest of the counters.
978 *
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000979 * We choose an event which for CCI-5xx is guaranteed not to count.
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000980 * We use the highest possible event code (0x1f) for the master interface 0.
981 */
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000982#define CCI5xx_INVALID_EVENT ((CCI5xx_PORT_M0 << CCI5xx_PMU_EVENT_SOURCE_SHIFT) | \
983 (CCI5xx_PMU_EVENT_CODE_MASK << CCI5xx_PMU_EVENT_CODE_SHIFT))
984static void cci5xx_pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000985{
986 int i;
987 DECLARE_BITMAP(saved_mask, cci_pmu->num_cntrs);
988
989 bitmap_zero(saved_mask, cci_pmu->num_cntrs);
990 pmu_save_counters(cci_pmu, saved_mask);
991
992 /*
993 * Now that all the counters are disabled, we can safely turn the PMU on,
994 * without syncing the status of the counters
995 */
996 __cci_pmu_enable_nosync(cci_pmu);
997
998 for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
999 struct perf_event *event = cci_pmu->hw_events.events[i];
1000
1001 if (WARN_ON(!event))
1002 continue;
1003
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001004 pmu_set_event(cci_pmu, i, CCI5xx_INVALID_EVENT);
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +00001005 pmu_enable_counter(cci_pmu, i);
1006 pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
1007 pmu_disable_counter(cci_pmu, i);
1008 pmu_set_event(cci_pmu, i, event->hw.config_base);
1009 }
1010
1011 __cci_pmu_disable();
1012
1013 pmu_restore_counters(cci_pmu, saved_mask);
1014}
1015
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001016#endif /* CONFIG_ARM_CCI5xx_PMU */
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +00001017
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001018static u64 pmu_event_update(struct perf_event *event)
1019{
1020 struct hw_perf_event *hwc = &event->hw;
1021 u64 delta, prev_raw_count, new_raw_count;
1022
1023 do {
1024 prev_raw_count = local64_read(&hwc->prev_count);
1025 new_raw_count = pmu_read_counter(event);
1026 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
1027 new_raw_count) != prev_raw_count);
1028
1029 delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
1030
1031 local64_add(delta, &event->count);
1032
1033 return new_raw_count;
1034}
1035
1036static void pmu_read(struct perf_event *event)
1037{
1038 pmu_event_update(event);
1039}
1040
Will Deaconceb49512016-02-26 16:12:43 +00001041static void pmu_event_set_period(struct perf_event *event)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001042{
1043 struct hw_perf_event *hwc = &event->hw;
1044 /*
1045 * The CCI PMU counters have a period of 2^32. To account for the
1046 * possiblity of extreme interrupt latency we program for a period of
1047 * half that. Hopefully we can handle the interrupt before another 2^31
1048 * events occur and the counter overtakes its previous value.
1049 */
1050 u64 val = 1ULL << 31;
1051 local64_set(&hwc->prev_count, val);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +00001052
1053 /*
1054 * CCI PMU uses PERF_HES_ARCH to keep track of the counters, whose
1055 * values needs to be sync-ed with the s/w state before the PMU is
1056 * enabled.
1057 * Mark this counter for sync.
1058 */
1059 hwc->state |= PERF_HES_ARCH;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001060}
1061
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001062static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
1063{
1064 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001065 struct cci_pmu *cci_pmu = dev;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001066 struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001067 int idx, handled = IRQ_NONE;
1068
1069 raw_spin_lock_irqsave(&events->pmu_lock, flags);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +00001070
1071 /* Disable the PMU while we walk through the counters */
1072 __cci_pmu_disable();
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001073 /*
1074 * Iterate over counters and update the corresponding perf events.
1075 * This should work regardless of whether we have per-counter overflow
1076 * interrupt or a combined overflow interrupt.
1077 */
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001078 for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001079 struct perf_event *event = events->events[idx];
1080 struct hw_perf_event *hw_counter;
1081
1082 if (!event)
1083 continue;
1084
1085 hw_counter = &event->hw;
1086
1087 /* Did this counter overflow? */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001088 if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
Himangi Saraogifc5130d2014-07-30 11:37:35 +01001089 CCI_PMU_OVRFLW_FLAG))
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001090 continue;
1091
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001092 pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
1093 CCI_PMU_OVRFLW);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001094
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001095 pmu_event_update(event);
1096 pmu_event_set_period(event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001097 handled = IRQ_HANDLED;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001098 }
Suzuki K Poulosec66eea52016-02-23 10:49:47 +00001099
1100 /* Enable the PMU and sync possibly overflowed counters */
Suzuki K Poulose11300022016-02-23 10:49:51 +00001101 __cci_pmu_enable_sync(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001102 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
1103
1104 return IRQ_RETVAL(handled);
1105}
1106
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001107static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001108{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001109 int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
1110 if (ret) {
1111 pmu_free_irq(cci_pmu);
1112 return ret;
1113 }
1114 return 0;
1115}
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001116
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001117static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
1118{
1119 pmu_free_irq(cci_pmu);
1120}
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001121
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001122static void hw_perf_event_destroy(struct perf_event *event)
1123{
1124 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1125 atomic_t *active_events = &cci_pmu->active_events;
1126 struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
1127
1128 if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
1129 cci_pmu_put_hw(cci_pmu);
1130 mutex_unlock(reserve_mutex);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001131 }
1132}
1133
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001134static void cci_pmu_enable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001135{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001136 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
1137 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001138 int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001139 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001140
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001141 if (!enabled)
1142 return;
1143
1144 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Suzuki K Poulose11300022016-02-23 10:49:51 +00001145 __cci_pmu_enable_sync(cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001146 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001147
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001148}
1149
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001150static void cci_pmu_disable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001151{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001152 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
1153 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001154 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001155
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001156 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Suzuki K Poulosea077c522016-02-23 10:49:46 +00001157 __cci_pmu_disable();
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001158 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001159}
1160
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001161/*
1162 * Check if the idx represents a non-programmable counter.
1163 * All the fixed event counters are mapped before the programmable
1164 * counters.
1165 */
1166static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
1167{
1168 return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
1169}
1170
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001171static void cci_pmu_start(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001172{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001173 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1174 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1175 struct hw_perf_event *hwc = &event->hw;
1176 int idx = hwc->idx;
1177 unsigned long flags;
1178
1179 /*
1180 * To handle interrupt latency, we always reprogram the period
1181 * regardlesss of PERF_EF_RELOAD.
1182 */
1183 if (pmu_flags & PERF_EF_RELOAD)
1184 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
1185
1186 hwc->state = 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001187
1188 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
1189 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001190 return;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001191 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001192
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001193 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
1194
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001195 /* Configure the counter unless you are counting a fixed event */
1196 if (!pmu_fixed_hw_idx(cci_pmu, idx))
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001197 pmu_set_event(cci_pmu, idx, hwc->config_base);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001198
1199 pmu_event_set_period(event);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001200 pmu_enable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001201
1202 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001203}
1204
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001205static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001206{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001207 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1208 struct hw_perf_event *hwc = &event->hw;
1209 int idx = hwc->idx;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001210
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001211 if (hwc->state & PERF_HES_STOPPED)
1212 return;
1213
1214 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001215 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001216 return;
1217 }
1218
1219 /*
1220 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
1221 * cci_pmu_start()
1222 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001223 pmu_disable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001224 pmu_event_update(event);
1225 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001226}
1227
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001228static int cci_pmu_add(struct perf_event *event, int flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001229{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001230 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1231 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1232 struct hw_perf_event *hwc = &event->hw;
1233 int idx;
1234 int err = 0;
1235
1236 perf_pmu_disable(event->pmu);
1237
1238 /* If we don't have a space for the counter then finish early. */
1239 idx = pmu_get_event_idx(hw_events, event);
1240 if (idx < 0) {
1241 err = idx;
1242 goto out;
1243 }
1244
1245 event->hw.idx = idx;
1246 hw_events->events[idx] = event;
1247
1248 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1249 if (flags & PERF_EF_START)
1250 cci_pmu_start(event, PERF_EF_RELOAD);
1251
1252 /* Propagate our changes to the userspace mapping. */
1253 perf_event_update_userpage(event);
1254
1255out:
1256 perf_pmu_enable(event->pmu);
1257 return err;
1258}
1259
1260static void cci_pmu_del(struct perf_event *event, int flags)
1261{
1262 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1263 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1264 struct hw_perf_event *hwc = &event->hw;
1265 int idx = hwc->idx;
1266
1267 cci_pmu_stop(event, PERF_EF_UPDATE);
1268 hw_events->events[idx] = NULL;
1269 clear_bit(idx, hw_events->used_mask);
1270
1271 perf_event_update_userpage(event);
1272}
1273
1274static int
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001275validate_event(struct pmu *cci_pmu,
1276 struct cci_pmu_hw_events *hw_events,
1277 struct perf_event *event)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001278{
1279 if (is_software_event(event))
1280 return 1;
1281
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001282 /*
1283 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
1284 * core perf code won't check that the pmu->ctx == leader->ctx
1285 * until after pmu->event_init(event).
1286 */
1287 if (event->pmu != cci_pmu)
1288 return 0;
1289
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001290 if (event->state < PERF_EVENT_STATE_OFF)
1291 return 1;
1292
1293 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
1294 return 1;
1295
1296 return pmu_get_event_idx(hw_events, event) >= 0;
1297}
1298
1299static int
1300validate_group(struct perf_event *event)
1301{
1302 struct perf_event *sibling, *leader = event->group_leader;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001303 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1304 unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001305 struct cci_pmu_hw_events fake_pmu = {
1306 /*
1307 * Initialise the fake PMU. We only need to populate the
1308 * used_mask for the purposes of validation.
1309 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001310 .used_mask = mask,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001311 };
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001312 memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001313
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001314 if (!validate_event(event->pmu, &fake_pmu, leader))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001315 return -EINVAL;
1316
1317 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001318 if (!validate_event(event->pmu, &fake_pmu, sibling))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001319 return -EINVAL;
1320 }
1321
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001322 if (!validate_event(event->pmu, &fake_pmu, event))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001323 return -EINVAL;
1324
1325 return 0;
1326}
1327
1328static int
1329__hw_perf_event_init(struct perf_event *event)
1330{
1331 struct hw_perf_event *hwc = &event->hw;
1332 int mapping;
1333
1334 mapping = pmu_map_event(event);
1335
1336 if (mapping < 0) {
1337 pr_debug("event %x:%llx not supported\n", event->attr.type,
1338 event->attr.config);
1339 return mapping;
1340 }
1341
1342 /*
1343 * We don't assign an index until we actually place the event onto
1344 * hardware. Use -1 to signify that we haven't decided where to put it
1345 * yet.
1346 */
1347 hwc->idx = -1;
1348 hwc->config_base = 0;
1349 hwc->config = 0;
1350 hwc->event_base = 0;
1351
1352 /*
1353 * Store the event encoding into the config_base field.
1354 */
1355 hwc->config_base |= (unsigned long)mapping;
1356
1357 /*
1358 * Limit the sample_period to half of the counter width. That way, the
1359 * new counter value is far less likely to overtake the previous one
1360 * unless you have some serious IRQ latency issues.
1361 */
1362 hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
1363 hwc->last_period = hwc->sample_period;
1364 local64_set(&hwc->period_left, hwc->sample_period);
1365
1366 if (event->group_leader != event) {
1367 if (validate_group(event) != 0)
1368 return -EINVAL;
1369 }
1370
1371 return 0;
1372}
1373
1374static int cci_pmu_event_init(struct perf_event *event)
1375{
1376 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1377 atomic_t *active_events = &cci_pmu->active_events;
1378 int err = 0;
1379 int cpu;
1380
1381 if (event->attr.type != event->pmu->type)
1382 return -ENOENT;
1383
1384 /* Shared by all CPUs, no meaningful state to sample */
1385 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
1386 return -EOPNOTSUPP;
1387
1388 /* We have no filtering of any kind */
1389 if (event->attr.exclude_user ||
1390 event->attr.exclude_kernel ||
1391 event->attr.exclude_hv ||
1392 event->attr.exclude_idle ||
1393 event->attr.exclude_host ||
1394 event->attr.exclude_guest)
1395 return -EINVAL;
1396
1397 /*
1398 * Following the example set by other "uncore" PMUs, we accept any CPU
1399 * and rewrite its affinity dynamically rather than having perf core
1400 * handle cpu == -1 and pid == -1 for this case.
1401 *
1402 * The perf core will pin online CPUs for the duration of this call and
1403 * the event being installed into its context, so the PMU's CPU can't
1404 * change under our feet.
1405 */
1406 cpu = cpumask_first(&cci_pmu->cpus);
1407 if (event->cpu < 0 || cpu < 0)
1408 return -EINVAL;
1409 event->cpu = cpu;
1410
1411 event->destroy = hw_perf_event_destroy;
1412 if (!atomic_inc_not_zero(active_events)) {
1413 mutex_lock(&cci_pmu->reserve_mutex);
1414 if (atomic_read(active_events) == 0)
1415 err = cci_pmu_get_hw(cci_pmu);
1416 if (!err)
1417 atomic_inc(active_events);
1418 mutex_unlock(&cci_pmu->reserve_mutex);
1419 }
1420 if (err)
1421 return err;
1422
1423 err = __hw_perf_event_init(event);
1424 if (err)
1425 hw_perf_event_destroy(event);
1426
1427 return err;
1428}
1429
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001430static ssize_t pmu_cpumask_attr_show(struct device *dev,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001431 struct device_attribute *attr, char *buf)
1432{
Mark Rutland5e442eb2016-02-23 10:49:43 +00001433 struct pmu *pmu = dev_get_drvdata(dev);
1434 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001435
Tejun Heo660e5ec2015-02-13 14:37:20 -08001436 int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001437 cpumask_pr_args(&cci_pmu->cpus));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001438 buf[n++] = '\n';
1439 buf[n] = '\0';
1440 return n;
1441}
1442
Mark Rutland5e442eb2016-02-23 10:49:43 +00001443static struct device_attribute pmu_cpumask_attr =
1444 __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001445
1446static struct attribute *pmu_attrs[] = {
Mark Rutland5e442eb2016-02-23 10:49:43 +00001447 &pmu_cpumask_attr.attr,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001448 NULL,
1449};
1450
1451static struct attribute_group pmu_attr_group = {
1452 .attrs = pmu_attrs,
1453};
1454
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001455static struct attribute_group pmu_format_attr_group = {
1456 .name = "format",
1457 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1458};
1459
1460static struct attribute_group pmu_event_attr_group = {
1461 .name = "events",
1462 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1463};
1464
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001465static const struct attribute_group *pmu_attr_groups[] = {
1466 &pmu_attr_group,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001467 &pmu_format_attr_group,
1468 &pmu_event_attr_group,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001469 NULL
1470};
1471
1472static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
1473{
Mark Rutland5e442eb2016-02-23 10:49:43 +00001474 const struct cci_pmu_model *model = cci_pmu->model;
1475 char *name = model->name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001476 u32 num_cntrs;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001477
Mark Rutland5e442eb2016-02-23 10:49:43 +00001478 pmu_event_attr_group.attrs = model->event_attrs;
1479 pmu_format_attr_group.attrs = model->format_attrs;
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001480
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001481 cci_pmu->pmu = (struct pmu) {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001482 .name = cci_pmu->model->name,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001483 .task_ctx_nr = perf_invalid_context,
1484 .pmu_enable = cci_pmu_enable,
1485 .pmu_disable = cci_pmu_disable,
1486 .event_init = cci_pmu_event_init,
1487 .add = cci_pmu_add,
1488 .del = cci_pmu_del,
1489 .start = cci_pmu_start,
1490 .stop = cci_pmu_stop,
1491 .read = pmu_read,
1492 .attr_groups = pmu_attr_groups,
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001493 };
1494
1495 cci_pmu->plat_device = pdev;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001496 num_cntrs = pmu_get_max_counters();
1497 if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
1498 dev_warn(&pdev->dev,
1499 "PMU implements more counters(%d) than supported by"
1500 " the model(%d), truncated.",
1501 num_cntrs, cci_pmu->model->num_hw_cntrs);
1502 num_cntrs = cci_pmu->model->num_hw_cntrs;
1503 }
1504 cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001505
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001506 return perf_pmu_register(&cci_pmu->pmu, name, -1);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001507}
1508
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001509static int cci_pmu_cpu_notifier(struct notifier_block *self,
1510 unsigned long action, void *hcpu)
1511{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001512 struct cci_pmu *cci_pmu = container_of(self,
1513 struct cci_pmu, cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001514 unsigned int cpu = (long)hcpu;
1515 unsigned int target;
1516
1517 switch (action & ~CPU_TASKS_FROZEN) {
1518 case CPU_DOWN_PREPARE:
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001519 if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001520 break;
1521 target = cpumask_any_but(cpu_online_mask, cpu);
Andrzej Hajda0f173802016-02-23 10:49:44 +00001522 if (target >= nr_cpu_ids) // UP, last CPU
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001523 break;
1524 /*
1525 * TODO: migrate context once core races on event->ctx have
1526 * been fixed.
1527 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001528 cpumask_set_cpu(target, &cci_pmu->cpus);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001529 default:
1530 break;
1531 }
1532
1533 return NOTIFY_OK;
1534}
1535
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001536static struct cci_pmu_model cci_pmu_models[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001537#ifdef CONFIG_ARM_CCI400_PMU
1538 [CCI400_R0] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001539 .name = "CCI_400",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001540 .fixed_hw_cntrs = 1, /* Cycle counter */
1541 .num_hw_cntrs = 4,
1542 .cntr_size = SZ_4K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001543 .format_attrs = cci400_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001544 .event_attrs = cci400_r0_pmu_event_attrs,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001545 .event_ranges = {
1546 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001547 CCI400_R0_SLAVE_PORT_MIN_EV,
1548 CCI400_R0_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001549 },
1550 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001551 CCI400_R0_MASTER_PORT_MIN_EV,
1552 CCI400_R0_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001553 },
1554 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001555 .validate_hw_event = cci400_validate_hw_event,
1556 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001557 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001558 [CCI400_R1] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001559 .name = "CCI_400_r1",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001560 .fixed_hw_cntrs = 1, /* Cycle counter */
1561 .num_hw_cntrs = 4,
1562 .cntr_size = SZ_4K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001563 .format_attrs = cci400_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001564 .event_attrs = cci400_r1_pmu_event_attrs,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001565 .event_ranges = {
1566 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001567 CCI400_R1_SLAVE_PORT_MIN_EV,
1568 CCI400_R1_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001569 },
1570 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001571 CCI400_R1_MASTER_PORT_MIN_EV,
1572 CCI400_R1_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001573 },
1574 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001575 .validate_hw_event = cci400_validate_hw_event,
1576 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001577 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001578#endif
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001579#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001580 [CCI500_R0] = {
1581 .name = "CCI_500",
1582 .fixed_hw_cntrs = 0,
1583 .num_hw_cntrs = 8,
1584 .cntr_size = SZ_64K,
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001585 .format_attrs = cci5xx_pmu_format_attrs,
1586 .event_attrs = cci5xx_pmu_event_attrs,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001587 .event_ranges = {
1588 [CCI_IF_SLAVE] = {
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001589 CCI5xx_SLAVE_PORT_MIN_EV,
1590 CCI5xx_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001591 },
1592 [CCI_IF_MASTER] = {
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001593 CCI5xx_MASTER_PORT_MIN_EV,
1594 CCI5xx_MASTER_PORT_MAX_EV,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001595 },
1596 [CCI_IF_GLOBAL] = {
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001597 CCI5xx_GLOBAL_PORT_MIN_EV,
1598 CCI5xx_GLOBAL_PORT_MAX_EV,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001599 },
1600 },
1601 .validate_hw_event = cci500_validate_hw_event,
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001602 .write_counters = cci5xx_pmu_write_counters,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001603 },
Suzuki K Poulosed7dd5fd2016-02-23 10:49:55 +00001604 [CCI550_R0] = {
1605 .name = "CCI_550",
1606 .fixed_hw_cntrs = 0,
1607 .num_hw_cntrs = 8,
1608 .cntr_size = SZ_64K,
1609 .format_attrs = cci5xx_pmu_format_attrs,
1610 .event_attrs = cci5xx_pmu_event_attrs,
1611 .event_ranges = {
1612 [CCI_IF_SLAVE] = {
1613 CCI5xx_SLAVE_PORT_MIN_EV,
1614 CCI5xx_SLAVE_PORT_MAX_EV,
1615 },
1616 [CCI_IF_MASTER] = {
1617 CCI5xx_MASTER_PORT_MIN_EV,
1618 CCI5xx_MASTER_PORT_MAX_EV,
1619 },
1620 [CCI_IF_GLOBAL] = {
1621 CCI5xx_GLOBAL_PORT_MIN_EV,
1622 CCI5xx_GLOBAL_PORT_MAX_EV,
1623 },
1624 },
1625 .validate_hw_event = cci550_validate_hw_event,
1626 .write_counters = cci5xx_pmu_write_counters,
1627 },
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001628#endif
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001629};
1630
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001631static const struct of_device_id arm_cci_pmu_matches[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001632#ifdef CONFIG_ARM_CCI400_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001633 {
1634 .compatible = "arm,cci-400-pmu",
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001635 .data = NULL,
1636 },
1637 {
1638 .compatible = "arm,cci-400-pmu,r0",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001639 .data = &cci_pmu_models[CCI400_R0],
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001640 },
1641 {
1642 .compatible = "arm,cci-400-pmu,r1",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001643 .data = &cci_pmu_models[CCI400_R1],
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001644 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001645#endif
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001646#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001647 {
1648 .compatible = "arm,cci-500-pmu,r0",
1649 .data = &cci_pmu_models[CCI500_R0],
1650 },
Suzuki K Poulosed7dd5fd2016-02-23 10:49:55 +00001651 {
1652 .compatible = "arm,cci-550-pmu,r0",
1653 .data = &cci_pmu_models[CCI550_R0],
1654 },
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001655#endif
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001656 {},
1657};
1658
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001659static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
1660{
1661 const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
1662 pdev->dev.of_node);
1663 if (!match)
1664 return NULL;
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001665 if (match->data)
1666 return match->data;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001667
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001668 dev_warn(&pdev->dev, "DEPRECATED compatible property,"
1669 "requires secure access to CCI registers");
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001670 return probe_cci_model(pdev);
1671}
1672
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001673static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
1674{
1675 int i;
1676
1677 for (i = 0; i < nr_irqs; i++)
1678 if (irq == irqs[i])
1679 return true;
1680
1681 return false;
1682}
1683
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001684static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
1685{
1686 struct cci_pmu *cci_pmu;
1687 const struct cci_pmu_model *model;
1688
1689 /*
1690 * All allocations are devm_* hence we don't have to free
1691 * them explicitly on an error, as it would end up in driver
1692 * detach.
1693 */
1694 model = get_cci_model(pdev);
1695 if (!model) {
1696 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
1697 return ERR_PTR(-ENODEV);
1698 }
1699
1700 cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
1701 if (!cci_pmu)
1702 return ERR_PTR(-ENOMEM);
1703
1704 cci_pmu->model = model;
1705 cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
1706 sizeof(*cci_pmu->irqs), GFP_KERNEL);
1707 if (!cci_pmu->irqs)
1708 return ERR_PTR(-ENOMEM);
1709 cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
1710 CCI_PMU_MAX_HW_CNTRS(model),
1711 sizeof(*cci_pmu->hw_events.events),
1712 GFP_KERNEL);
1713 if (!cci_pmu->hw_events.events)
1714 return ERR_PTR(-ENOMEM);
1715 cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
1716 BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
1717 sizeof(*cci_pmu->hw_events.used_mask),
1718 GFP_KERNEL);
1719 if (!cci_pmu->hw_events.used_mask)
1720 return ERR_PTR(-ENOMEM);
1721
1722 return cci_pmu;
1723}
1724
1725
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001726static int cci_pmu_probe(struct platform_device *pdev)
1727{
1728 struct resource *res;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001729 struct cci_pmu *cci_pmu;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001730 int i, ret, irq;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001731
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001732 cci_pmu = cci_pmu_alloc(pdev);
1733 if (IS_ERR(cci_pmu))
1734 return PTR_ERR(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001735
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001736 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001737 cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
1738 if (IS_ERR(cci_pmu->base))
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001739 return -ENOMEM;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001740
1741 /*
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001742 * CCI PMU has one overflow interrupt per counter; but some may be tied
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001743 * together to a common interrupt.
1744 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001745 cci_pmu->nr_irqs = 0;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001746 for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001747 irq = platform_get_irq(pdev, i);
1748 if (irq < 0)
1749 break;
1750
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001751 if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001752 continue;
1753
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001754 cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001755 }
1756
1757 /*
1758 * Ensure that the device tree has as many interrupts as the number
1759 * of counters.
1760 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001761 if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001762 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001763 i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001764 return -EINVAL;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001765 }
1766
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001767 raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
1768 mutex_init(&cci_pmu->reserve_mutex);
1769 atomic_set(&cci_pmu->active_events, 0);
1770 cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001771
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001772 cci_pmu->cpu_nb = (struct notifier_block) {
1773 .notifier_call = cci_pmu_cpu_notifier,
1774 /*
1775 * to migrate uncore events, our notifier should be executed
1776 * before perf core's notifier.
1777 */
1778 .priority = CPU_PRI_PERF + 1,
1779 };
1780
1781 ret = register_cpu_notifier(&cci_pmu->cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001782 if (ret)
1783 return ret;
1784
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001785 ret = cci_pmu_init(cci_pmu, pdev);
1786 if (ret) {
1787 unregister_cpu_notifier(&cci_pmu->cpu_nb);
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001788 return ret;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001789 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001790
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001791 pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001792 return 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001793}
1794
1795static int cci_platform_probe(struct platform_device *pdev)
1796{
1797 if (!cci_probed())
1798 return -ENODEV;
1799
1800 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1801}
1802
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001803static struct platform_driver cci_pmu_driver = {
1804 .driver = {
1805 .name = DRIVER_NAME_PMU,
1806 .of_match_table = arm_cci_pmu_matches,
1807 },
1808 .probe = cci_pmu_probe,
1809};
1810
1811static struct platform_driver cci_platform_driver = {
1812 .driver = {
1813 .name = DRIVER_NAME,
1814 .of_match_table = arm_cci_matches,
1815 },
1816 .probe = cci_platform_probe,
1817};
1818
1819static int __init cci_platform_init(void)
1820{
1821 int ret;
1822
1823 ret = platform_driver_register(&cci_pmu_driver);
1824 if (ret)
1825 return ret;
1826
1827 return platform_driver_register(&cci_platform_driver);
1828}
1829
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001830#else /* !CONFIG_ARM_CCI_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001831
1832static int __init cci_platform_init(void)
1833{
1834 return 0;
1835}
1836
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001837#endif /* CONFIG_ARM_CCI_PMU */
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001838
1839#ifdef CONFIG_ARM_CCI400_PORT_CTRL
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001840
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001841#define CCI_PORT_CTRL 0x0
1842#define CCI_CTRL_STATUS 0xc
1843
1844#define CCI_ENABLE_SNOOP_REQ 0x1
1845#define CCI_ENABLE_DVM_REQ 0x2
1846#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1847
1848enum cci_ace_port_type {
1849 ACE_INVALID_PORT = 0x0,
1850 ACE_PORT,
1851 ACE_LITE_PORT,
1852};
1853
1854struct cci_ace_port {
1855 void __iomem *base;
1856 unsigned long phys;
1857 enum cci_ace_port_type type;
1858 struct device_node *dn;
1859};
1860
1861static struct cci_ace_port *ports;
1862static unsigned int nb_cci_ports;
1863
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001864struct cpu_port {
1865 u64 mpidr;
1866 u32 port;
1867};
Nicolas Pitre62158f82013-05-21 23:34:41 -04001868
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001869/*
1870 * Use the port MSB as valid flag, shift can be made dynamic
1871 * by computing number of bits required for port indexes.
1872 * Code disabling CCI cpu ports runs with D-cache invalidated
1873 * and SCTLR bit clear so data accesses must be kept to a minimum
1874 * to improve performance; for now shift is left static to
1875 * avoid one more data access while disabling the CCI port.
1876 */
1877#define PORT_VALID_SHIFT 31
1878#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1879
1880static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
1881{
1882 port->port = PORT_VALID | index;
1883 port->mpidr = mpidr;
1884}
1885
1886static inline bool cpu_port_is_valid(struct cpu_port *port)
1887{
1888 return !!(port->port & PORT_VALID);
1889}
1890
1891static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
1892{
1893 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
1894}
1895
1896static struct cpu_port cpu_port[NR_CPUS];
1897
1898/**
1899 * __cci_ace_get_port - Function to retrieve the port index connected to
1900 * a cpu or device.
1901 *
1902 * @dn: device node of the device to look-up
1903 * @type: port type
1904 *
1905 * Return value:
1906 * - CCI port index if success
1907 * - -ENODEV if failure
1908 */
1909static int __cci_ace_get_port(struct device_node *dn, int type)
1910{
1911 int i;
1912 bool ace_match;
1913 struct device_node *cci_portn;
1914
1915 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
1916 for (i = 0; i < nb_cci_ports; i++) {
1917 ace_match = ports[i].type == type;
1918 if (ace_match && cci_portn == ports[i].dn)
1919 return i;
1920 }
1921 return -ENODEV;
1922}
1923
1924int cci_ace_get_port(struct device_node *dn)
1925{
1926 return __cci_ace_get_port(dn, ACE_LITE_PORT);
1927}
1928EXPORT_SYMBOL_GPL(cci_ace_get_port);
1929
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001930static void cci_ace_init_ports(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001931{
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001932 int port, cpu;
1933 struct device_node *cpun;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001934
1935 /*
1936 * Port index look-up speeds up the function disabling ports by CPU,
1937 * since the logical to port index mapping is done once and does
1938 * not change after system boot.
1939 * The stashed index array is initialized for all possible CPUs
1940 * at probe time.
1941 */
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001942 for_each_possible_cpu(cpu) {
1943 /* too early to use cpu->of_node */
1944 cpun = of_get_cpu_node(cpu, NULL);
1945
1946 if (WARN(!cpun, "Missing cpu device node\n"))
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001947 continue;
1948
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001949 port = __cci_ace_get_port(cpun, ACE_PORT);
1950 if (port < 0)
1951 continue;
1952
1953 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
1954 }
1955
1956 for_each_possible_cpu(cpu) {
1957 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
1958 "CPU %u does not have an associated CCI port\n",
1959 cpu);
1960 }
1961}
1962/*
1963 * Functions to enable/disable a CCI interconnect slave port
1964 *
1965 * They are called by low-level power management code to disable slave
1966 * interfaces snoops and DVM broadcast.
1967 * Since they may execute with cache data allocation disabled and
1968 * after the caches have been cleaned and invalidated the functions provide
1969 * no explicit locking since they may run with D-cache disabled, so normal
1970 * cacheable kernel locks based on ldrex/strex may not work.
1971 * Locking has to be provided by BSP implementations to ensure proper
1972 * operations.
1973 */
1974
1975/**
1976 * cci_port_control() - function to control a CCI port
1977 *
1978 * @port: index of the port to setup
1979 * @enable: if true enables the port, if false disables it
1980 */
1981static void notrace cci_port_control(unsigned int port, bool enable)
1982{
1983 void __iomem *base = ports[port].base;
1984
1985 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
1986 /*
1987 * This function is called from power down procedures
1988 * and must not execute any instruction that might
1989 * cause the processor to be put in a quiescent state
1990 * (eg wfi). Hence, cpu_relax() can not be added to this
1991 * read loop to optimize power, since it might hide possibly
1992 * disruptive operations.
1993 */
1994 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
1995 ;
1996}
1997
1998/**
1999 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
2000 * reference
2001 *
2002 * @mpidr: mpidr of the CPU whose CCI port should be disabled
2003 *
2004 * Disabling a CCI port for a CPU implies disabling the CCI port
2005 * controlling that CPU cluster. Code disabling CPU CCI ports
2006 * must make sure that the CPU running the code is the last active CPU
2007 * in the cluster ie all other CPUs are quiescent in a low power state.
2008 *
2009 * Return:
2010 * 0 on success
2011 * -ENODEV on port look-up failure
2012 */
2013int notrace cci_disable_port_by_cpu(u64 mpidr)
2014{
2015 int cpu;
2016 bool is_valid;
2017 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
2018 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
2019 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
2020 cci_port_control(cpu_port[cpu].port, false);
2021 return 0;
2022 }
2023 }
2024 return -ENODEV;
2025}
2026EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
2027
2028/**
Nicolas Pitre62158f82013-05-21 23:34:41 -04002029 * cci_enable_port_for_self() - enable a CCI port for calling CPU
2030 *
2031 * Enabling a CCI port for the calling CPU implies enabling the CCI
2032 * port controlling that CPU's cluster. Caller must make sure that the
2033 * CPU running the code is the first active CPU in the cluster and all
2034 * other CPUs are quiescent in a low power state or waiting for this CPU
2035 * to complete the CCI initialization.
2036 *
2037 * Because this is called when the MMU is still off and with no stack,
2038 * the code must be position independent and ideally rely on callee
2039 * clobbered registers only. To achieve this we must code this function
2040 * entirely in assembler.
2041 *
2042 * On success this returns with the proper CCI port enabled. In case of
2043 * any failure this never returns as the inability to enable the CCI is
2044 * fatal and there is no possible recovery at this stage.
2045 */
2046asmlinkage void __naked cci_enable_port_for_self(void)
2047{
2048 asm volatile ("\n"
Arnd Bergmannf4902492013-06-03 15:15:36 +02002049" .arch armv7-a\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04002050" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
2051" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
2052" adr r1, 5f \n"
2053" ldr r2, [r1] \n"
2054" add r1, r1, r2 @ &cpu_port \n"
2055" add ip, r1, %[sizeof_cpu_port] \n"
2056
2057 /* Loop over the cpu_port array looking for a matching MPIDR */
2058"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
2059" cmp r2, r0 @ compare MPIDR \n"
2060" bne 2f \n"
2061
2062 /* Found a match, now test port validity */
2063" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
2064" tst r3, #"__stringify(PORT_VALID)" \n"
2065" bne 3f \n"
2066
2067 /* no match, loop with the next cpu_port entry */
2068"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
2069" cmp r1, ip @ done? \n"
2070" blo 1b \n"
2071
2072 /* CCI port not found -- cheaply try to stall this CPU */
2073"cci_port_not_found: \n"
2074" wfi \n"
2075" wfe \n"
2076" b cci_port_not_found \n"
2077
2078 /* Use matched port index to look up the corresponding ports entry */
2079"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
2080" adr r0, 6f \n"
2081" ldmia r0, {r1, r2} \n"
2082" sub r1, r1, r0 @ virt - phys \n"
2083" ldr r0, [r0, r2] @ *(&ports) \n"
2084" mov r2, %[sizeof_struct_ace_port] \n"
2085" mla r0, r2, r3, r0 @ &ports[index] \n"
2086" sub r0, r0, r1 @ virt_to_phys() \n"
2087
2088 /* Enable the CCI port */
2089" ldr r0, [r0, %[offsetof_port_phys]] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07002090" mov r3, %[cci_enable_req]\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04002091" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
2092
2093 /* poll the status reg for completion */
2094" adr r1, 7f \n"
2095" ldr r0, [r1] \n"
2096" ldr r0, [r0, r1] @ cci_ctrl_base \n"
2097"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07002098" tst r1, %[cci_control_status_bits] \n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04002099" bne 4b \n"
2100
2101" mov r0, #0 \n"
2102" bx lr \n"
2103
2104" .align 2 \n"
2105"5: .word cpu_port - . \n"
2106"6: .word . \n"
2107" .word ports - 6b \n"
2108"7: .word cci_ctrl_phys - . \n"
2109 : :
2110 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07002111 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
2112 [cci_control_status_bits] "i" cpu_to_le32(1),
Nicolas Pitre62158f82013-05-21 23:34:41 -04002113#ifndef __ARMEB__
2114 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
2115#else
2116 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
2117#endif
2118 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
2119 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
2120 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
2121 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
2122
2123 unreachable();
2124}
2125
2126/**
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002127 * __cci_control_port_by_device() - function to control a CCI port by device
2128 * reference
2129 *
2130 * @dn: device node pointer of the device whose CCI port should be
2131 * controlled
2132 * @enable: if true enables the port, if false disables it
2133 *
2134 * Return:
2135 * 0 on success
2136 * -ENODEV on port look-up failure
2137 */
2138int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
2139{
2140 int port;
2141
2142 if (!dn)
2143 return -ENODEV;
2144
2145 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
2146 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
2147 dn->full_name))
2148 return -ENODEV;
2149 cci_port_control(port, enable);
2150 return 0;
2151}
2152EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
2153
2154/**
2155 * __cci_control_port_by_index() - function to control a CCI port by port index
2156 *
2157 * @port: port index previously retrieved with cci_ace_get_port()
2158 * @enable: if true enables the port, if false disables it
2159 *
2160 * Return:
2161 * 0 on success
2162 * -ENODEV on port index out of range
2163 * -EPERM if operation carried out on an ACE PORT
2164 */
2165int notrace __cci_control_port_by_index(u32 port, bool enable)
2166{
2167 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
2168 return -ENODEV;
2169 /*
2170 * CCI control for ports connected to CPUS is extremely fragile
2171 * and must be made to go through a specific and controlled
2172 * interface (ie cci_disable_port_by_cpu(); control by general purpose
2173 * indexing is therefore disabled for ACE ports.
2174 */
2175 if (ports[port].type == ACE_PORT)
2176 return -EPERM;
2177
2178 cci_port_control(port, enable);
2179 return 0;
2180}
2181EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
2182
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002183static const struct of_device_id arm_cci_ctrl_if_matches[] = {
2184 {.compatible = "arm,cci-400-ctrl-if", },
2185 {},
2186};
2187
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002188static int cci_probe_ports(struct device_node *np)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002189{
2190 struct cci_nb_ports const *cci_config;
2191 int ret, i, nb_ace = 0, nb_ace_lite = 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002192 struct device_node *cp;
Nicolas Pitre62158f82013-05-21 23:34:41 -04002193 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002194 const char *match_str;
2195 bool is_ace;
2196
Abhilash Kesavan896ddd62015-01-10 08:41:35 +05302197
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002198 cci_config = of_match_node(arm_cci_matches, np)->data;
2199 if (!cci_config)
2200 return -ENODEV;
2201
2202 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
2203
Lorenzo Pieralisi7c762032014-01-27 10:50:37 +00002204 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002205 if (!ports)
2206 return -ENOMEM;
2207
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002208 for_each_child_of_node(np, cp) {
2209 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
2210 continue;
2211
2212 i = nb_ace + nb_ace_lite;
2213
2214 if (i >= nb_cci_ports)
2215 break;
2216
2217 if (of_property_read_string(cp, "interface-type",
2218 &match_str)) {
2219 WARN(1, "node %s missing interface-type property\n",
2220 cp->full_name);
2221 continue;
2222 }
2223 is_ace = strcmp(match_str, "ace") == 0;
2224 if (!is_ace && strcmp(match_str, "ace-lite")) {
2225 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
2226 cp->full_name);
2227 continue;
2228 }
2229
Nicolas Pitre62158f82013-05-21 23:34:41 -04002230 ret = of_address_to_resource(cp, 0, &res);
2231 if (!ret) {
2232 ports[i].base = ioremap(res.start, resource_size(&res));
2233 ports[i].phys = res.start;
2234 }
2235 if (ret || !ports[i].base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002236 WARN(1, "unable to ioremap CCI port %d\n", i);
2237 continue;
2238 }
2239
2240 if (is_ace) {
2241 if (WARN_ON(nb_ace >= cci_config->nb_ace))
2242 continue;
2243 ports[i].type = ACE_PORT;
2244 ++nb_ace;
2245 } else {
2246 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
2247 continue;
2248 ports[i].type = ACE_LITE_PORT;
2249 ++nb_ace_lite;
2250 }
2251 ports[i].dn = cp;
2252 }
2253
2254 /* initialize a stashed array of ACE ports to speed-up look-up */
2255 cci_ace_init_ports();
2256
2257 /*
2258 * Multi-cluster systems may need this data when non-coherent, during
2259 * cluster power-up/power-down. Make sure it reaches main memory.
2260 */
2261 sync_cache_w(&cci_ctrl_base);
Nicolas Pitre62158f82013-05-21 23:34:41 -04002262 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002263 sync_cache_w(&ports);
2264 sync_cache_w(&cpu_port);
2265 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
2266 pr_info("ARM CCI driver probed\n");
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002267
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002268 return 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002269}
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00002270#else /* !CONFIG_ARM_CCI400_PORT_CTRL */
2271static inline int cci_probe_ports(struct device_node *np)
2272{
2273 return 0;
2274}
2275#endif /* CONFIG_ARM_CCI400_PORT_CTRL */
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002276
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002277static int cci_probe(void)
2278{
2279 int ret;
2280 struct device_node *np;
2281 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002282
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002283 np = of_find_matching_node(NULL, arm_cci_matches);
2284 if(!np || !of_device_is_available(np))
2285 return -ENODEV;
2286
2287 ret = of_address_to_resource(np, 0, &res);
2288 if (!ret) {
2289 cci_ctrl_base = ioremap(res.start, resource_size(&res));
2290 cci_ctrl_phys = res.start;
2291 }
2292 if (ret || !cci_ctrl_base) {
2293 WARN(1, "unable to ioremap CCI ctrl\n");
2294 return -ENXIO;
2295 }
2296
2297 return cci_probe_ports(np);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002298}
2299
2300static int cci_init_status = -EAGAIN;
2301static DEFINE_MUTEX(cci_probing);
2302
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002303static int cci_init(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002304{
2305 if (cci_init_status != -EAGAIN)
2306 return cci_init_status;
2307
2308 mutex_lock(&cci_probing);
2309 if (cci_init_status == -EAGAIN)
2310 cci_init_status = cci_probe();
2311 mutex_unlock(&cci_probing);
2312 return cci_init_status;
2313}
2314
2315/*
2316 * To sort out early init calls ordering a helper function is provided to
2317 * check if the CCI driver has beed initialized. Function check if the driver
2318 * has been initialized, if not it calls the init function that probes
2319 * the driver and updates the return value.
2320 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002321bool cci_probed(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002322{
2323 return cci_init() == 0;
2324}
2325EXPORT_SYMBOL_GPL(cci_probed);
2326
2327early_initcall(cci_init);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002328core_initcall(cci_platform_init);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002329MODULE_LICENSE("GPL");
2330MODULE_DESCRIPTION("ARM CCI support");