]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/bus/arm-cci.c
arm-cci: Group writes to counter
[mirror_ubuntu-bionic-kernel.git] / drivers / bus / arm-cci.c
CommitLineData
ed69bdd8
LP
1/*
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>
c6f85cb4 19#include <linux/interrupt.h>
ed69bdd8
LP
20#include <linux/module.h>
21#include <linux/of_address.h>
b91c8f28
PA
22#include <linux/of_irq.h>
23#include <linux/of_platform.h>
c6f85cb4 24#include <linux/perf_event.h>
b91c8f28 25#include <linux/platform_device.h>
ed69bdd8 26#include <linux/slab.h>
b91c8f28 27#include <linux/spinlock.h>
ed69bdd8
LP
28
29#include <asm/cacheflush.h>
30#include <asm/smp_plat.h>
31
f6b9e83c
SP
32static void __iomem *cci_ctrl_base;
33static unsigned long cci_ctrl_phys;
ed69bdd8 34
ee8e5d5f 35#ifdef CONFIG_ARM_CCI400_PORT_CTRL
ed69bdd8
LP
36struct cci_nb_ports {
37 unsigned int nb_ace;
38 unsigned int nb_ace_lite;
39};
40
f6b9e83c
SP
41static const struct cci_nb_ports cci400_ports = {
42 .nb_ace = 2,
43 .nb_ace_lite = 3
ed69bdd8
LP
44};
45
ee8e5d5f
SP
46#define CCI400_PORTS_DATA (&cci400_ports)
47#else
48#define CCI400_PORTS_DATA (NULL)
49#endif
50
f6b9e83c 51static const struct of_device_id arm_cci_matches[] = {
ee8e5d5f
SP
52#ifdef CONFIG_ARM_CCI400_COMMON
53 {.compatible = "arm,cci-400", .data = CCI400_PORTS_DATA },
a95791ef
SP
54#endif
55#ifdef CONFIG_ARM_CCI500_PMU
56 { .compatible = "arm,cci-500", },
ee8e5d5f 57#endif
f6b9e83c 58 {},
ed69bdd8
LP
59};
60
f4d58938 61#ifdef CONFIG_ARM_CCI_PMU
b91c8f28 62
f4d58938 63#define DRIVER_NAME "ARM-CCI"
f6b9e83c
SP
64#define DRIVER_NAME_PMU DRIVER_NAME " PMU"
65
b91c8f28
PA
66#define CCI_PMCR 0x0100
67#define CCI_PID2 0x0fe8
68
69#define CCI_PMCR_CEN 0x00000001
70#define CCI_PMCR_NCNT_MASK 0x0000f800
71#define CCI_PMCR_NCNT_SHIFT 11
72
73#define CCI_PID2_REV_MASK 0xf0
74#define CCI_PID2_REV_SHIFT 4
75
f6b9e83c
SP
76#define CCI_PMU_EVT_SEL 0x000
77#define CCI_PMU_CNTR 0x004
78#define CCI_PMU_CNTR_CTRL 0x008
79#define CCI_PMU_OVRFLW 0x00c
80
81#define CCI_PMU_OVRFLW_FLAG 1
82
ab5b316d
SP
83#define CCI_PMU_CNTR_SIZE(model) ((model)->cntr_size)
84#define CCI_PMU_CNTR_BASE(model, idx) ((idx) * CCI_PMU_CNTR_SIZE(model))
85#define CCI_PMU_CNTR_MASK ((1ULL << 32) -1)
86#define CCI_PMU_CNTR_LAST(cci_pmu) (cci_pmu->num_cntrs - 1)
f6b9e83c 87
ab5b316d
SP
88#define CCI_PMU_MAX_HW_CNTRS(model) \
89 ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
f6b9e83c 90
fc17c839
SP
91/* Types of interfaces that can generate events */
92enum {
93 CCI_IF_SLAVE,
94 CCI_IF_MASTER,
a95791ef
SP
95#ifdef CONFIG_ARM_CCI500_PMU
96 CCI_IF_GLOBAL,
97#endif
fc17c839
SP
98 CCI_IF_MAX,
99};
100
101struct event_range {
102 u32 min;
103 u32 max;
104};
105
f6b9e83c 106struct cci_pmu_hw_events {
ab5b316d
SP
107 struct perf_event **events;
108 unsigned long *used_mask;
f6b9e83c
SP
109 raw_spinlock_t pmu_lock;
110};
111
31216290 112struct cci_pmu;
ab5b316d
SP
113/*
114 * struct cci_pmu_model:
115 * @fixed_hw_cntrs - Number of fixed event counters
116 * @num_hw_cntrs - Maximum number of programmable event counters
117 * @cntr_size - Size of an event counter mapping
118 */
fc17c839
SP
119struct cci_pmu_model {
120 char *name;
ab5b316d
SP
121 u32 fixed_hw_cntrs;
122 u32 num_hw_cntrs;
123 u32 cntr_size;
5e442eba
MR
124 struct attribute **format_attrs;
125 struct attribute **event_attrs;
fc17c839 126 struct event_range event_ranges[CCI_IF_MAX];
31216290
SP
127 int (*validate_hw_event)(struct cci_pmu *, unsigned long);
128 int (*get_event_idx)(struct cci_pmu *, struct cci_pmu_hw_events *, unsigned long);
fc17c839
SP
129};
130
131static struct cci_pmu_model cci_pmu_models[];
132
f6b9e83c
SP
133struct cci_pmu {
134 void __iomem *base;
135 struct pmu pmu;
136 int nr_irqs;
ab5b316d 137 int *irqs;
f6b9e83c 138 unsigned long active_irqs;
fc17c839 139 const struct cci_pmu_model *model;
f6b9e83c
SP
140 struct cci_pmu_hw_events hw_events;
141 struct platform_device *plat_device;
ab5b316d 142 int num_cntrs;
f6b9e83c
SP
143 atomic_t active_events;
144 struct mutex reserve_mutex;
a1a076d7 145 struct notifier_block cpu_nb;
f6b9e83c
SP
146 cpumask_t cpus;
147};
f6b9e83c
SP
148
149#define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
150
f4d58938
SP
151enum cci_models {
152#ifdef CONFIG_ARM_CCI400_PMU
153 CCI400_R0,
154 CCI400_R1,
a95791ef
SP
155#endif
156#ifdef CONFIG_ARM_CCI500_PMU
157 CCI500_R0,
f4d58938
SP
158#endif
159 CCI_MODEL_MAX
160};
161
e14cfad3
SP
162static ssize_t cci_pmu_format_show(struct device *dev,
163 struct device_attribute *attr, char *buf);
164static ssize_t cci_pmu_event_show(struct device *dev,
165 struct device_attribute *attr, char *buf);
166
5e442eba
MR
167#define CCI_EXT_ATTR_ENTRY(_name, _func, _config) \
168 &((struct dev_ext_attribute[]) { \
169 { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config } \
170 })[0].attr.attr
e14cfad3
SP
171
172#define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
173 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_format_show, (char *)_config)
174#define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
175 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
176
f4d58938
SP
177/* CCI400 PMU Specific definitions */
178
179#ifdef CONFIG_ARM_CCI400_PMU
180
b91c8f28 181/* Port ids */
f4d58938
SP
182#define CCI400_PORT_S0 0
183#define CCI400_PORT_S1 1
184#define CCI400_PORT_S2 2
185#define CCI400_PORT_S3 3
186#define CCI400_PORT_S4 4
187#define CCI400_PORT_M0 5
188#define CCI400_PORT_M1 6
189#define CCI400_PORT_M2 7
190
191#define CCI400_R1_PX 5
b91c8f28 192
b91c8f28
PA
193/*
194 * Instead of an event id to monitor CCI cycles, a dedicated counter is
195 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
196 * make use of this event in hardware.
197 */
198enum cci400_perf_events {
f4d58938 199 CCI400_PMU_CYCLES = 0xff
b91c8f28
PA
200};
201
f4d58938
SP
202#define CCI400_PMU_CYCLE_CNTR_IDX 0
203#define CCI400_PMU_CNTR0_IDX 1
b91c8f28
PA
204
205/*
206 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
207 * ports and bits 4:0 are event codes. There are different event codes
208 * associated with each port type.
209 *
210 * Additionally, the range of events associated with the port types changed
211 * between Rev0 and Rev1.
212 *
213 * The constants below define the range of valid codes for each port type for
214 * the different revisions and are used to validate the event to be monitored.
215 */
216
f4d58938
SP
217#define CCI400_PMU_EVENT_MASK 0xffUL
218#define CCI400_PMU_EVENT_SOURCE_SHIFT 5
219#define CCI400_PMU_EVENT_SOURCE_MASK 0x7
220#define CCI400_PMU_EVENT_CODE_SHIFT 0
221#define CCI400_PMU_EVENT_CODE_MASK 0x1f
222#define CCI400_PMU_EVENT_SOURCE(event) \
223 ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
224 CCI400_PMU_EVENT_SOURCE_MASK)
225#define CCI400_PMU_EVENT_CODE(event) \
226 ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
227
228#define CCI400_R0_SLAVE_PORT_MIN_EV 0x00
229#define CCI400_R0_SLAVE_PORT_MAX_EV 0x13
230#define CCI400_R0_MASTER_PORT_MIN_EV 0x14
231#define CCI400_R0_MASTER_PORT_MAX_EV 0x1a
232
233#define CCI400_R1_SLAVE_PORT_MIN_EV 0x00
234#define CCI400_R1_SLAVE_PORT_MAX_EV 0x14
235#define CCI400_R1_MASTER_PORT_MIN_EV 0x00
236#define CCI400_R1_MASTER_PORT_MAX_EV 0x11
b91c8f28 237
e14cfad3
SP
238#define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
239 CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
240 (unsigned long)_config)
241
242static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
243 struct device_attribute *attr, char *buf);
244
5e442eba 245static struct attribute *cci400_pmu_format_attrs[] = {
e14cfad3
SP
246 CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
247 CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-7"),
5e442eba 248 NULL
e14cfad3
SP
249};
250
5e442eba 251static struct attribute *cci400_r0_pmu_event_attrs[] = {
e14cfad3
SP
252 /* Slave events */
253 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
254 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
255 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
256 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
257 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
258 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
259 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
260 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
261 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
262 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
263 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
264 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
265 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
266 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
267 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
268 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
269 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
270 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
271 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
272 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
273 /* Master events */
274 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x14),
275 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard, 0x15),
276 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard, 0x16),
277 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full, 0x17),
278 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x18),
279 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x19),
280 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full, 0x1A),
281 /* Special event for cycles counter */
282 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
5e442eba 283 NULL
e14cfad3
SP
284};
285
5e442eba 286static struct attribute *cci400_r1_pmu_event_attrs[] = {
e14cfad3
SP
287 /* Slave events */
288 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
289 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
290 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
291 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
292 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
293 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
294 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
295 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
296 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
297 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
298 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
299 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
300 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
301 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
302 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
303 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
304 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
305 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
306 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
307 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
308 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard, 0x14),
309 /* Master events */
310 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x0),
311 CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard, 0x1),
312 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard, 0x2),
313 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full, 0x3),
314 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x4),
315 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x5),
316 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full, 0x6),
317 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full, 0x7),
318 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full, 0x8),
319 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0, 0x9),
320 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1, 0xA),
321 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2, 0xB),
322 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3, 0xC),
323 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0, 0xD),
324 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1, 0xE),
325 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2, 0xF),
326 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3, 0x10),
327 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard, 0x11),
328 /* Special event for cycles counter */
329 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
5e442eba 330 NULL
e14cfad3
SP
331};
332
333static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
334 struct device_attribute *attr, char *buf)
335{
336 struct dev_ext_attribute *eattr = container_of(attr,
337 struct dev_ext_attribute, attr);
338 return snprintf(buf, PAGE_SIZE, "config=0x%lx\n", (unsigned long)eattr->var);
339}
340
31216290
SP
341static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
342 struct cci_pmu_hw_events *hw,
343 unsigned long cci_event)
344{
345 int idx;
346
347 /* cycles event idx is fixed */
f4d58938
SP
348 if (cci_event == CCI400_PMU_CYCLES) {
349 if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
31216290
SP
350 return -EAGAIN;
351
f4d58938 352 return CCI400_PMU_CYCLE_CNTR_IDX;
31216290
SP
353 }
354
f4d58938 355 for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
31216290
SP
356 if (!test_and_set_bit(idx, hw->used_mask))
357 return idx;
358
359 /* No counters available */
360 return -EAGAIN;
361}
362
363static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
b91c8f28 364{
f4d58938
SP
365 u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
366 u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
fc17c839 367 int if_type;
b91c8f28 368
f4d58938 369 if (hw_event & ~CCI400_PMU_EVENT_MASK)
874c5714
SP
370 return -ENOENT;
371
f4d58938 372 if (hw_event == CCI400_PMU_CYCLES)
31216290
SP
373 return hw_event;
374
b91c8f28 375 switch (ev_source) {
f4d58938
SP
376 case CCI400_PORT_S0:
377 case CCI400_PORT_S1:
378 case CCI400_PORT_S2:
379 case CCI400_PORT_S3:
380 case CCI400_PORT_S4:
b91c8f28 381 /* Slave Interface */
fc17c839 382 if_type = CCI_IF_SLAVE;
b91c8f28 383 break;
f4d58938
SP
384 case CCI400_PORT_M0:
385 case CCI400_PORT_M1:
386 case CCI400_PORT_M2:
b91c8f28 387 /* Master Interface */
fc17c839 388 if_type = CCI_IF_MASTER;
b91c8f28 389 break;
fc17c839
SP
390 default:
391 return -ENOENT;
b91c8f28
PA
392 }
393
a1a076d7
SP
394 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
395 ev_code <= cci_pmu->model->event_ranges[if_type].max)
fc17c839
SP
396 return hw_event;
397
b91c8f28
PA
398 return -ENOENT;
399}
400
f4d58938 401static int probe_cci400_revision(void)
f6b9e83c
SP
402{
403 int rev;
404 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
405 rev >>= CCI_PID2_REV_SHIFT;
406
f4d58938
SP
407 if (rev < CCI400_R1_PX)
408 return CCI400_R0;
f6b9e83c 409 else
f4d58938 410 return CCI400_R1;
f6b9e83c
SP
411}
412
fc17c839 413static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
f6b9e83c 414{
772742a6 415 if (platform_has_secure_cci_access())
f4d58938
SP
416 return &cci_pmu_models[probe_cci400_revision()];
417 return NULL;
418}
419#else /* !CONFIG_ARM_CCI400_PMU */
420static inline struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
421{
772742a6 422 return NULL;
f6b9e83c 423}
f4d58938 424#endif /* CONFIG_ARM_CCI400_PMU */
f6b9e83c 425
a95791ef
SP
426#ifdef CONFIG_ARM_CCI500_PMU
427
428/*
429 * CCI500 provides 8 independent event counters that can count
430 * any of the events available.
431 *
432 * CCI500 PMU event id is an 9-bit value made of two parts.
433 * bits [8:5] - Source for the event
434 * 0x0-0x6 - Slave interfaces
435 * 0x8-0xD - Master interfaces
436 * 0xf - Global Events
437 * 0x7,0xe - Reserved
438 *
439 * bits [4:0] - Event code (specific to type of interface)
440 */
441
442/* Port ids */
443#define CCI500_PORT_S0 0x0
444#define CCI500_PORT_S1 0x1
445#define CCI500_PORT_S2 0x2
446#define CCI500_PORT_S3 0x3
447#define CCI500_PORT_S4 0x4
448#define CCI500_PORT_S5 0x5
449#define CCI500_PORT_S6 0x6
450
451#define CCI500_PORT_M0 0x8
452#define CCI500_PORT_M1 0x9
453#define CCI500_PORT_M2 0xa
454#define CCI500_PORT_M3 0xb
455#define CCI500_PORT_M4 0xc
456#define CCI500_PORT_M5 0xd
457
458#define CCI500_PORT_GLOBAL 0xf
459
460#define CCI500_PMU_EVENT_MASK 0x1ffUL
461#define CCI500_PMU_EVENT_SOURCE_SHIFT 0x5
462#define CCI500_PMU_EVENT_SOURCE_MASK 0xf
463#define CCI500_PMU_EVENT_CODE_SHIFT 0x0
464#define CCI500_PMU_EVENT_CODE_MASK 0x1f
465
466#define CCI500_PMU_EVENT_SOURCE(event) \
467 ((event >> CCI500_PMU_EVENT_SOURCE_SHIFT) & CCI500_PMU_EVENT_SOURCE_MASK)
468#define CCI500_PMU_EVENT_CODE(event) \
469 ((event >> CCI500_PMU_EVENT_CODE_SHIFT) & CCI500_PMU_EVENT_CODE_MASK)
470
471#define CCI500_SLAVE_PORT_MIN_EV 0x00
472#define CCI500_SLAVE_PORT_MAX_EV 0x1f
473#define CCI500_MASTER_PORT_MIN_EV 0x00
474#define CCI500_MASTER_PORT_MAX_EV 0x06
475#define CCI500_GLOBAL_PORT_MIN_EV 0x00
476#define CCI500_GLOBAL_PORT_MAX_EV 0x0f
477
e14cfad3
SP
478
479#define CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
480 CCI_EXT_ATTR_ENTRY(_name, cci500_pmu_global_event_show, \
481 (unsigned long) _config)
482
483static ssize_t cci500_pmu_global_event_show(struct device *dev,
484 struct device_attribute *attr, char *buf);
485
5e442eba 486static struct attribute *cci500_pmu_format_attrs[] = {
e14cfad3
SP
487 CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
488 CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-8"),
5e442eba 489 NULL,
e14cfad3
SP
490};
491
5e442eba 492static struct attribute *cci500_pmu_event_attrs[] = {
e14cfad3
SP
493 /* 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 */
537 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1, 0x0),
538 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3, 0x1),
539 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5, 0x2),
540 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7, 0x3),
541 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1, 0x4),
542 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3, 0x5),
543 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5, 0x6),
544 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7, 0x7),
545 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation, 0x8),
546 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy, 0x9),
547 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full, 0xA),
548 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq, 0xB),
549 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs, 0xC),
550 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard, 0xD),
551 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snopp_rq_stall_tt_full, 0xE),
552 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot, 0xF),
5e442eba 553 NULL
e14cfad3
SP
554};
555
556static ssize_t cci500_pmu_global_event_show(struct device *dev,
557 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",
563 (unsigned long)eattr->var, CCI500_PORT_GLOBAL);
564}
565
a95791ef
SP
566static int cci500_validate_hw_event(struct cci_pmu *cci_pmu,
567 unsigned long hw_event)
568{
569 u32 ev_source = CCI500_PMU_EVENT_SOURCE(hw_event);
570 u32 ev_code = CCI500_PMU_EVENT_CODE(hw_event);
571 int if_type;
572
573 if (hw_event & ~CCI500_PMU_EVENT_MASK)
574 return -ENOENT;
575
576 switch (ev_source) {
577 case CCI500_PORT_S0:
578 case CCI500_PORT_S1:
579 case CCI500_PORT_S2:
580 case CCI500_PORT_S3:
581 case CCI500_PORT_S4:
582 case CCI500_PORT_S5:
583 case CCI500_PORT_S6:
584 if_type = CCI_IF_SLAVE;
585 break;
586 case CCI500_PORT_M0:
587 case CCI500_PORT_M1:
588 case CCI500_PORT_M2:
589 case CCI500_PORT_M3:
590 case CCI500_PORT_M4:
591 case CCI500_PORT_M5:
592 if_type = CCI_IF_MASTER;
593 break;
594 case CCI500_PORT_GLOBAL:
595 if_type = CCI_IF_GLOBAL;
596 break;
597 default:
598 return -ENOENT;
599 }
600
601 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
602 ev_code <= cci_pmu->model->event_ranges[if_type].max)
603 return hw_event;
604
605 return -ENOENT;
606}
607#endif /* CONFIG_ARM_CCI500_PMU */
608
e14cfad3
SP
609static ssize_t cci_pmu_format_show(struct device *dev,
610 struct device_attribute *attr, char *buf)
611{
612 struct dev_ext_attribute *eattr = container_of(attr,
613 struct dev_ext_attribute, attr);
614 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)eattr->var);
615}
616
617static ssize_t cci_pmu_event_show(struct device *dev,
618 struct device_attribute *attr, char *buf)
619{
620 struct dev_ext_attribute *eattr = container_of(attr,
621 struct dev_ext_attribute, attr);
622 /* source parameter is mandatory for normal PMU events */
623 return snprintf(buf, PAGE_SIZE, "source=?,event=0x%lx\n",
624 (unsigned long)eattr->var);
625}
626
c6f85cb4 627static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
b91c8f28 628{
ab5b316d 629 return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
b91c8f28
PA
630}
631
a1a076d7 632static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
b91c8f28 633{
ab5b316d
SP
634 return readl_relaxed(cci_pmu->base +
635 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
b91c8f28
PA
636}
637
a1a076d7
SP
638static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
639 int idx, unsigned int offset)
b91c8f28 640{
a1a076d7 641 return writel_relaxed(value, cci_pmu->base +
ab5b316d 642 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
b91c8f28
PA
643}
644
a1a076d7 645static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
b91c8f28 646{
a1a076d7 647 pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
b91c8f28
PA
648}
649
a1a076d7 650static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
b91c8f28 651{
a1a076d7 652 pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
b91c8f28
PA
653}
654
a1a076d7 655static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
b91c8f28 656{
a1a076d7 657 pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
b91c8f28
PA
658}
659
ab5b316d
SP
660/*
661 * Returns the number of programmable counters actually implemented
662 * by the cci
663 */
b91c8f28
PA
664static u32 pmu_get_max_counters(void)
665{
ab5b316d
SP
666 return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
667 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
b91c8f28
PA
668}
669
c6f85cb4 670static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
b91c8f28 671{
c6f85cb4 672 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
31216290 673 unsigned long cci_event = event->hw.config_base;
b91c8f28
PA
674 int idx;
675
31216290
SP
676 if (cci_pmu->model->get_event_idx)
677 return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
b91c8f28 678
31216290
SP
679 /* Generic code to find an unused idx from the mask */
680 for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
b91c8f28
PA
681 if (!test_and_set_bit(idx, hw->used_mask))
682 return idx;
683
684 /* No counters available */
685 return -EAGAIN;
686}
687
688static int pmu_map_event(struct perf_event *event)
689{
31216290 690 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
b91c8f28 691
31216290
SP
692 if (event->attr.type < PERF_TYPE_MAX ||
693 !cci_pmu->model->validate_hw_event)
b91c8f28
PA
694 return -ENOENT;
695
31216290 696 return cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
b91c8f28
PA
697}
698
c6f85cb4 699static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
b91c8f28
PA
700{
701 int i;
702 struct platform_device *pmu_device = cci_pmu->plat_device;
703
704 if (unlikely(!pmu_device))
705 return -ENODEV;
706
a1a076d7 707 if (cci_pmu->nr_irqs < 1) {
b91c8f28
PA
708 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
709 return -ENODEV;
710 }
711
712 /*
713 * Register all available CCI PMU interrupts. In the interrupt handler
714 * we iterate over the counters checking for interrupt source (the
715 * overflowing counter) and clear it.
716 *
717 * This should allow handling of non-unique interrupt for the counters.
718 */
a1a076d7
SP
719 for (i = 0; i < cci_pmu->nr_irqs; i++) {
720 int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
b91c8f28
PA
721 "arm-cci-pmu", cci_pmu);
722 if (err) {
723 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
a1a076d7 724 cci_pmu->irqs[i]);
b91c8f28
PA
725 return err;
726 }
727
a1a076d7 728 set_bit(i, &cci_pmu->active_irqs);
b91c8f28
PA
729 }
730
731 return 0;
732}
733
c6f85cb4
MR
734static void pmu_free_irq(struct cci_pmu *cci_pmu)
735{
736 int i;
737
a1a076d7
SP
738 for (i = 0; i < cci_pmu->nr_irqs; i++) {
739 if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
c6f85cb4
MR
740 continue;
741
a1a076d7 742 free_irq(cci_pmu->irqs[i], cci_pmu);
c6f85cb4
MR
743 }
744}
745
746static u32 pmu_read_counter(struct perf_event *event)
747{
748 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
749 struct hw_perf_event *hw_counter = &event->hw;
750 int idx = hw_counter->idx;
751 u32 value;
752
753 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
754 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
755 return 0;
756 }
a1a076d7 757 value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
c6f85cb4
MR
758
759 return value;
760}
761
762static void pmu_write_counter(struct perf_event *event, u32 value)
763{
764 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
765 struct hw_perf_event *hw_counter = &event->hw;
766 int idx = hw_counter->idx;
767
768 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
769 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
770 else
a1a076d7 771 pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
c6f85cb4
MR
772}
773
a53eb5c6
SP
774static void __maybe_unused
775pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
776{
777 int i;
778 struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
779
780 for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
781 struct perf_event *event = cci_hw->events[i];
782
783 if (WARN_ON(!event))
784 continue;
785 pmu_write_counter(event, local64_read(&event->hw.prev_count));
786 }
787}
788
c6f85cb4
MR
789static u64 pmu_event_update(struct perf_event *event)
790{
791 struct hw_perf_event *hwc = &event->hw;
792 u64 delta, prev_raw_count, new_raw_count;
793
794 do {
795 prev_raw_count = local64_read(&hwc->prev_count);
796 new_raw_count = pmu_read_counter(event);
797 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
798 new_raw_count) != prev_raw_count);
799
800 delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
801
802 local64_add(delta, &event->count);
803
804 return new_raw_count;
805}
806
807static void pmu_read(struct perf_event *event)
808{
809 pmu_event_update(event);
810}
811
812void pmu_event_set_period(struct perf_event *event)
813{
814 struct hw_perf_event *hwc = &event->hw;
815 /*
816 * The CCI PMU counters have a period of 2^32. To account for the
817 * possiblity of extreme interrupt latency we program for a period of
818 * half that. Hopefully we can handle the interrupt before another 2^31
819 * events occur and the counter overtakes its previous value.
820 */
821 u64 val = 1ULL << 31;
822 local64_set(&hwc->prev_count, val);
823 pmu_write_counter(event, val);
824}
825
b91c8f28
PA
826static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
827{
828 unsigned long flags;
c6f85cb4 829 struct cci_pmu *cci_pmu = dev;
a1a076d7 830 struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
b91c8f28
PA
831 int idx, handled = IRQ_NONE;
832
833 raw_spin_lock_irqsave(&events->pmu_lock, flags);
b91c8f28
PA
834 /*
835 * Iterate over counters and update the corresponding perf events.
836 * This should work regardless of whether we have per-counter overflow
837 * interrupt or a combined overflow interrupt.
838 */
31216290 839 for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
b91c8f28
PA
840 struct perf_event *event = events->events[idx];
841 struct hw_perf_event *hw_counter;
842
843 if (!event)
844 continue;
845
846 hw_counter = &event->hw;
847
848 /* Did this counter overflow? */
a1a076d7 849 if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
fc5130de 850 CCI_PMU_OVRFLW_FLAG))
b91c8f28
PA
851 continue;
852
a1a076d7
SP
853 pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
854 CCI_PMU_OVRFLW);
b91c8f28 855
c6f85cb4
MR
856 pmu_event_update(event);
857 pmu_event_set_period(event);
b91c8f28 858 handled = IRQ_HANDLED;
b91c8f28
PA
859 }
860 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
861
862 return IRQ_RETVAL(handled);
863}
864
c6f85cb4 865static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
b91c8f28 866{
c6f85cb4
MR
867 int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
868 if (ret) {
869 pmu_free_irq(cci_pmu);
870 return ret;
871 }
872 return 0;
873}
b91c8f28 874
c6f85cb4
MR
875static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
876{
877 pmu_free_irq(cci_pmu);
878}
b91c8f28 879
c6f85cb4
MR
880static void hw_perf_event_destroy(struct perf_event *event)
881{
882 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
883 atomic_t *active_events = &cci_pmu->active_events;
884 struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
885
886 if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
887 cci_pmu_put_hw(cci_pmu);
888 mutex_unlock(reserve_mutex);
b91c8f28
PA
889 }
890}
891
c6f85cb4 892static void cci_pmu_enable(struct pmu *pmu)
b91c8f28 893{
c6f85cb4
MR
894 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
895 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
ab5b316d 896 int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
b91c8f28 897 unsigned long flags;
c6f85cb4
MR
898 u32 val;
899
900 if (!enabled)
901 return;
902
903 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
904
905 /* Enable all the PMU counters. */
906 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
907 writel(val, cci_ctrl_base + CCI_PMCR);
908 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
909
910}
911
912static void cci_pmu_disable(struct pmu *pmu)
913{
914 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
915 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
916 unsigned long flags;
917 u32 val;
918
919 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
920
921 /* Disable all the PMU counters. */
922 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
923 writel(val, cci_ctrl_base + CCI_PMCR);
924 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
925}
926
31216290
SP
927/*
928 * Check if the idx represents a non-programmable counter.
929 * All the fixed event counters are mapped before the programmable
930 * counters.
931 */
932static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
933{
934 return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
935}
936
c6f85cb4
MR
937static void cci_pmu_start(struct perf_event *event, int pmu_flags)
938{
939 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
940 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
941 struct hw_perf_event *hwc = &event->hw;
942 int idx = hwc->idx;
943 unsigned long flags;
944
945 /*
946 * To handle interrupt latency, we always reprogram the period
947 * regardlesss of PERF_EF_RELOAD.
948 */
949 if (pmu_flags & PERF_EF_RELOAD)
950 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
951
952 hwc->state = 0;
b91c8f28
PA
953
954 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
955 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
956 return;
957 }
958
c6f85cb4 959 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
b91c8f28 960
31216290
SP
961 /* Configure the counter unless you are counting a fixed event */
962 if (!pmu_fixed_hw_idx(cci_pmu, idx))
a1a076d7 963 pmu_set_event(cci_pmu, idx, hwc->config_base);
b91c8f28 964
c6f85cb4 965 pmu_event_set_period(event);
a1a076d7 966 pmu_enable_counter(cci_pmu, idx);
b91c8f28 967
c6f85cb4 968 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
b91c8f28
PA
969}
970
c6f85cb4 971static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
b91c8f28 972{
c6f85cb4
MR
973 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
974 struct hw_perf_event *hwc = &event->hw;
975 int idx = hwc->idx;
976
977 if (hwc->state & PERF_HES_STOPPED)
978 return;
b91c8f28
PA
979
980 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
981 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
982 return;
983 }
984
c6f85cb4
MR
985 /*
986 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
987 * cci_pmu_start()
988 */
a1a076d7 989 pmu_disable_counter(cci_pmu, idx);
c6f85cb4
MR
990 pmu_event_update(event);
991 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
b91c8f28
PA
992}
993
c6f85cb4 994static int cci_pmu_add(struct perf_event *event, int flags)
b91c8f28 995{
c6f85cb4
MR
996 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
997 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
998 struct hw_perf_event *hwc = &event->hw;
999 int idx;
1000 int err = 0;
b91c8f28 1001
c6f85cb4 1002 perf_pmu_disable(event->pmu);
b91c8f28 1003
c6f85cb4
MR
1004 /* If we don't have a space for the counter then finish early. */
1005 idx = pmu_get_event_idx(hw_events, event);
1006 if (idx < 0) {
1007 err = idx;
1008 goto out;
1009 }
b91c8f28 1010
c6f85cb4
MR
1011 event->hw.idx = idx;
1012 hw_events->events[idx] = event;
1013
1014 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1015 if (flags & PERF_EF_START)
1016 cci_pmu_start(event, PERF_EF_RELOAD);
1017
1018 /* Propagate our changes to the userspace mapping. */
1019 perf_event_update_userpage(event);
1020
1021out:
1022 perf_pmu_enable(event->pmu);
1023 return err;
b91c8f28
PA
1024}
1025
c6f85cb4 1026static void cci_pmu_del(struct perf_event *event, int flags)
b91c8f28 1027{
c6f85cb4
MR
1028 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1029 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1030 struct hw_perf_event *hwc = &event->hw;
1031 int idx = hwc->idx;
b91c8f28 1032
c6f85cb4
MR
1033 cci_pmu_stop(event, PERF_EF_UPDATE);
1034 hw_events->events[idx] = NULL;
1035 clear_bit(idx, hw_events->used_mask);
b91c8f28 1036
c6f85cb4
MR
1037 perf_event_update_userpage(event);
1038}
b91c8f28 1039
c6f85cb4 1040static int
b1862199
SP
1041validate_event(struct pmu *cci_pmu,
1042 struct cci_pmu_hw_events *hw_events,
1043 struct perf_event *event)
c6f85cb4
MR
1044{
1045 if (is_software_event(event))
1046 return 1;
1047
b1862199
SP
1048 /*
1049 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
1050 * core perf code won't check that the pmu->ctx == leader->ctx
1051 * until after pmu->event_init(event).
1052 */
1053 if (event->pmu != cci_pmu)
1054 return 0;
1055
c6f85cb4
MR
1056 if (event->state < PERF_EVENT_STATE_OFF)
1057 return 1;
1058
1059 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
1060 return 1;
1061
1062 return pmu_get_event_idx(hw_events, event) >= 0;
b91c8f28
PA
1063}
1064
c6f85cb4
MR
1065static int
1066validate_group(struct perf_event *event)
b91c8f28 1067{
c6f85cb4 1068 struct perf_event *sibling, *leader = event->group_leader;
ab5b316d
SP
1069 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1070 unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
c6f85cb4
MR
1071 struct cci_pmu_hw_events fake_pmu = {
1072 /*
1073 * Initialise the fake PMU. We only need to populate the
1074 * used_mask for the purposes of validation.
1075 */
ab5b316d 1076 .used_mask = mask,
c6f85cb4 1077 };
ab5b316d 1078 memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
b91c8f28 1079
b1862199 1080 if (!validate_event(event->pmu, &fake_pmu, leader))
c6f85cb4
MR
1081 return -EINVAL;
1082
1083 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
b1862199 1084 if (!validate_event(event->pmu, &fake_pmu, sibling))
c6f85cb4 1085 return -EINVAL;
b91c8f28 1086 }
b91c8f28 1087
b1862199 1088 if (!validate_event(event->pmu, &fake_pmu, event))
c6f85cb4
MR
1089 return -EINVAL;
1090
1091 return 0;
b91c8f28
PA
1092}
1093
c6f85cb4
MR
1094static int
1095__hw_perf_event_init(struct perf_event *event)
b91c8f28 1096{
c6f85cb4
MR
1097 struct hw_perf_event *hwc = &event->hw;
1098 int mapping;
b91c8f28 1099
c6f85cb4
MR
1100 mapping = pmu_map_event(event);
1101
1102 if (mapping < 0) {
1103 pr_debug("event %x:%llx not supported\n", event->attr.type,
1104 event->attr.config);
1105 return mapping;
1106 }
1107
1108 /*
1109 * We don't assign an index until we actually place the event onto
1110 * hardware. Use -1 to signify that we haven't decided where to put it
1111 * yet.
1112 */
1113 hwc->idx = -1;
1114 hwc->config_base = 0;
1115 hwc->config = 0;
1116 hwc->event_base = 0;
1117
1118 /*
1119 * Store the event encoding into the config_base field.
1120 */
1121 hwc->config_base |= (unsigned long)mapping;
1122
1123 /*
1124 * Limit the sample_period to half of the counter width. That way, the
1125 * new counter value is far less likely to overtake the previous one
1126 * unless you have some serious IRQ latency issues.
1127 */
1128 hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
1129 hwc->last_period = hwc->sample_period;
1130 local64_set(&hwc->period_left, hwc->sample_period);
1131
1132 if (event->group_leader != event) {
1133 if (validate_group(event) != 0)
1134 return -EINVAL;
1135 }
1136
1137 return 0;
1138}
1139
1140static int cci_pmu_event_init(struct perf_event *event)
1141{
1142 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1143 atomic_t *active_events = &cci_pmu->active_events;
1144 int err = 0;
1145 int cpu;
1146
1147 if (event->attr.type != event->pmu->type)
1148 return -ENOENT;
1149
1150 /* Shared by all CPUs, no meaningful state to sample */
1151 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
1152 return -EOPNOTSUPP;
1153
1154 /* We have no filtering of any kind */
1155 if (event->attr.exclude_user ||
1156 event->attr.exclude_kernel ||
1157 event->attr.exclude_hv ||
1158 event->attr.exclude_idle ||
1159 event->attr.exclude_host ||
1160 event->attr.exclude_guest)
1161 return -EINVAL;
1162
1163 /*
1164 * Following the example set by other "uncore" PMUs, we accept any CPU
1165 * and rewrite its affinity dynamically rather than having perf core
1166 * handle cpu == -1 and pid == -1 for this case.
1167 *
1168 * The perf core will pin online CPUs for the duration of this call and
1169 * the event being installed into its context, so the PMU's CPU can't
1170 * change under our feet.
1171 */
1172 cpu = cpumask_first(&cci_pmu->cpus);
1173 if (event->cpu < 0 || cpu < 0)
1174 return -EINVAL;
1175 event->cpu = cpu;
1176
1177 event->destroy = hw_perf_event_destroy;
1178 if (!atomic_inc_not_zero(active_events)) {
1179 mutex_lock(&cci_pmu->reserve_mutex);
1180 if (atomic_read(active_events) == 0)
1181 err = cci_pmu_get_hw(cci_pmu);
1182 if (!err)
1183 atomic_inc(active_events);
1184 mutex_unlock(&cci_pmu->reserve_mutex);
1185 }
1186 if (err)
1187 return err;
1188
1189 err = __hw_perf_event_init(event);
1190 if (err)
1191 hw_perf_event_destroy(event);
1192
1193 return err;
b91c8f28
PA
1194}
1195
a1a076d7 1196static ssize_t pmu_cpumask_attr_show(struct device *dev,
c6f85cb4
MR
1197 struct device_attribute *attr, char *buf)
1198{
5e442eba
MR
1199 struct pmu *pmu = dev_get_drvdata(dev);
1200 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
a1a076d7 1201
660e5ec0 1202 int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
a1a076d7 1203 cpumask_pr_args(&cci_pmu->cpus));
c6f85cb4
MR
1204 buf[n++] = '\n';
1205 buf[n] = '\0';
1206 return n;
1207}
1208
5e442eba
MR
1209static struct device_attribute pmu_cpumask_attr =
1210 __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL);
c6f85cb4
MR
1211
1212static struct attribute *pmu_attrs[] = {
5e442eba 1213 &pmu_cpumask_attr.attr,
c6f85cb4
MR
1214 NULL,
1215};
1216
1217static struct attribute_group pmu_attr_group = {
1218 .attrs = pmu_attrs,
1219};
1220
e14cfad3
SP
1221static struct attribute_group pmu_format_attr_group = {
1222 .name = "format",
1223 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1224};
1225
1226static struct attribute_group pmu_event_attr_group = {
1227 .name = "events",
1228 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1229};
1230
c6f85cb4
MR
1231static const struct attribute_group *pmu_attr_groups[] = {
1232 &pmu_attr_group,
e14cfad3
SP
1233 &pmu_format_attr_group,
1234 &pmu_event_attr_group,
c6f85cb4
MR
1235 NULL
1236};
1237
1238static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
1239{
5e442eba
MR
1240 const struct cci_pmu_model *model = cci_pmu->model;
1241 char *name = model->name;
ab5b316d 1242 u32 num_cntrs;
e14cfad3 1243
5e442eba
MR
1244 pmu_event_attr_group.attrs = model->event_attrs;
1245 pmu_format_attr_group.attrs = model->format_attrs;
a1a076d7 1246
c6f85cb4 1247 cci_pmu->pmu = (struct pmu) {
fc17c839 1248 .name = cci_pmu->model->name,
c6f85cb4
MR
1249 .task_ctx_nr = perf_invalid_context,
1250 .pmu_enable = cci_pmu_enable,
1251 .pmu_disable = cci_pmu_disable,
1252 .event_init = cci_pmu_event_init,
1253 .add = cci_pmu_add,
1254 .del = cci_pmu_del,
1255 .start = cci_pmu_start,
1256 .stop = cci_pmu_stop,
1257 .read = pmu_read,
1258 .attr_groups = pmu_attr_groups,
b91c8f28
PA
1259 };
1260
1261 cci_pmu->plat_device = pdev;
ab5b316d
SP
1262 num_cntrs = pmu_get_max_counters();
1263 if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
1264 dev_warn(&pdev->dev,
1265 "PMU implements more counters(%d) than supported by"
1266 " the model(%d), truncated.",
1267 num_cntrs, cci_pmu->model->num_hw_cntrs);
1268 num_cntrs = cci_pmu->model->num_hw_cntrs;
1269 }
1270 cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
b91c8f28 1271
c6f85cb4 1272 return perf_pmu_register(&cci_pmu->pmu, name, -1);
b91c8f28
PA
1273}
1274
c6f85cb4
MR
1275static int cci_pmu_cpu_notifier(struct notifier_block *self,
1276 unsigned long action, void *hcpu)
1277{
a1a076d7
SP
1278 struct cci_pmu *cci_pmu = container_of(self,
1279 struct cci_pmu, cpu_nb);
c6f85cb4
MR
1280 unsigned int cpu = (long)hcpu;
1281 unsigned int target;
1282
1283 switch (action & ~CPU_TASKS_FROZEN) {
1284 case CPU_DOWN_PREPARE:
a1a076d7 1285 if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
c6f85cb4
MR
1286 break;
1287 target = cpumask_any_but(cpu_online_mask, cpu);
0f17380c 1288 if (target >= nr_cpu_ids) // UP, last CPU
c6f85cb4
MR
1289 break;
1290 /*
1291 * TODO: migrate context once core races on event->ctx have
1292 * been fixed.
1293 */
a1a076d7 1294 cpumask_set_cpu(target, &cci_pmu->cpus);
c6f85cb4
MR
1295 default:
1296 break;
1297 }
1298
1299 return NOTIFY_OK;
1300}
1301
fc17c839 1302static struct cci_pmu_model cci_pmu_models[] = {
f4d58938
SP
1303#ifdef CONFIG_ARM_CCI400_PMU
1304 [CCI400_R0] = {
fc17c839 1305 .name = "CCI_400",
ab5b316d
SP
1306 .fixed_hw_cntrs = 1, /* Cycle counter */
1307 .num_hw_cntrs = 4,
1308 .cntr_size = SZ_4K,
e14cfad3 1309 .format_attrs = cci400_pmu_format_attrs,
e14cfad3 1310 .event_attrs = cci400_r0_pmu_event_attrs,
fc17c839
SP
1311 .event_ranges = {
1312 [CCI_IF_SLAVE] = {
f4d58938
SP
1313 CCI400_R0_SLAVE_PORT_MIN_EV,
1314 CCI400_R0_SLAVE_PORT_MAX_EV,
fc17c839
SP
1315 },
1316 [CCI_IF_MASTER] = {
f4d58938
SP
1317 CCI400_R0_MASTER_PORT_MIN_EV,
1318 CCI400_R0_MASTER_PORT_MAX_EV,
fc17c839
SP
1319 },
1320 },
31216290
SP
1321 .validate_hw_event = cci400_validate_hw_event,
1322 .get_event_idx = cci400_get_event_idx,
fc17c839 1323 },
f4d58938 1324 [CCI400_R1] = {
fc17c839 1325 .name = "CCI_400_r1",
ab5b316d
SP
1326 .fixed_hw_cntrs = 1, /* Cycle counter */
1327 .num_hw_cntrs = 4,
1328 .cntr_size = SZ_4K,
e14cfad3 1329 .format_attrs = cci400_pmu_format_attrs,
e14cfad3 1330 .event_attrs = cci400_r1_pmu_event_attrs,
fc17c839
SP
1331 .event_ranges = {
1332 [CCI_IF_SLAVE] = {
f4d58938
SP
1333 CCI400_R1_SLAVE_PORT_MIN_EV,
1334 CCI400_R1_SLAVE_PORT_MAX_EV,
fc17c839
SP
1335 },
1336 [CCI_IF_MASTER] = {
f4d58938
SP
1337 CCI400_R1_MASTER_PORT_MIN_EV,
1338 CCI400_R1_MASTER_PORT_MAX_EV,
fc17c839
SP
1339 },
1340 },
31216290
SP
1341 .validate_hw_event = cci400_validate_hw_event,
1342 .get_event_idx = cci400_get_event_idx,
fc17c839 1343 },
f4d58938 1344#endif
a95791ef
SP
1345#ifdef CONFIG_ARM_CCI500_PMU
1346 [CCI500_R0] = {
1347 .name = "CCI_500",
1348 .fixed_hw_cntrs = 0,
1349 .num_hw_cntrs = 8,
1350 .cntr_size = SZ_64K,
e14cfad3 1351 .format_attrs = cci500_pmu_format_attrs,
e14cfad3 1352 .event_attrs = cci500_pmu_event_attrs,
a95791ef
SP
1353 .event_ranges = {
1354 [CCI_IF_SLAVE] = {
1355 CCI500_SLAVE_PORT_MIN_EV,
1356 CCI500_SLAVE_PORT_MAX_EV,
1357 },
1358 [CCI_IF_MASTER] = {
1359 CCI500_MASTER_PORT_MIN_EV,
1360 CCI500_MASTER_PORT_MAX_EV,
1361 },
1362 [CCI_IF_GLOBAL] = {
1363 CCI500_GLOBAL_PORT_MIN_EV,
1364 CCI500_GLOBAL_PORT_MAX_EV,
1365 },
1366 },
1367 .validate_hw_event = cci500_validate_hw_event,
1368 },
1369#endif
fc17c839
SP
1370};
1371
b91c8f28 1372static const struct of_device_id arm_cci_pmu_matches[] = {
f4d58938 1373#ifdef CONFIG_ARM_CCI400_PMU
b91c8f28
PA
1374 {
1375 .compatible = "arm,cci-400-pmu",
772742a6
SP
1376 .data = NULL,
1377 },
1378 {
1379 .compatible = "arm,cci-400-pmu,r0",
f4d58938 1380 .data = &cci_pmu_models[CCI400_R0],
772742a6
SP
1381 },
1382 {
1383 .compatible = "arm,cci-400-pmu,r1",
f4d58938 1384 .data = &cci_pmu_models[CCI400_R1],
b91c8f28 1385 },
a95791ef
SP
1386#endif
1387#ifdef CONFIG_ARM_CCI500_PMU
1388 {
1389 .compatible = "arm,cci-500-pmu,r0",
1390 .data = &cci_pmu_models[CCI500_R0],
1391 },
f4d58938 1392#endif
b91c8f28
PA
1393 {},
1394};
1395
fc17c839
SP
1396static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
1397{
1398 const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
1399 pdev->dev.of_node);
1400 if (!match)
1401 return NULL;
772742a6
SP
1402 if (match->data)
1403 return match->data;
fc17c839 1404
772742a6
SP
1405 dev_warn(&pdev->dev, "DEPRECATED compatible property,"
1406 "requires secure access to CCI registers");
fc17c839
SP
1407 return probe_cci_model(pdev);
1408}
1409
f6b9e83c
SP
1410static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
1411{
1412 int i;
1413
1414 for (i = 0; i < nr_irqs; i++)
1415 if (irq == irqs[i])
1416 return true;
1417
1418 return false;
1419}
1420
ab5b316d 1421static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
b91c8f28 1422{
a1a076d7 1423 struct cci_pmu *cci_pmu;
fc17c839
SP
1424 const struct cci_pmu_model *model;
1425
ab5b316d
SP
1426 /*
1427 * All allocations are devm_* hence we don't have to free
1428 * them explicitly on an error, as it would end up in driver
1429 * detach.
1430 */
fc17c839
SP
1431 model = get_cci_model(pdev);
1432 if (!model) {
1433 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
ab5b316d 1434 return ERR_PTR(-ENODEV);
fc17c839 1435 }
b91c8f28 1436
a1a076d7
SP
1437 cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
1438 if (!cci_pmu)
ab5b316d 1439 return ERR_PTR(-ENOMEM);
b91c8f28 1440
a1a076d7 1441 cci_pmu->model = model;
ab5b316d
SP
1442 cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
1443 sizeof(*cci_pmu->irqs), GFP_KERNEL);
1444 if (!cci_pmu->irqs)
1445 return ERR_PTR(-ENOMEM);
1446 cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
1447 CCI_PMU_MAX_HW_CNTRS(model),
1448 sizeof(*cci_pmu->hw_events.events),
1449 GFP_KERNEL);
1450 if (!cci_pmu->hw_events.events)
1451 return ERR_PTR(-ENOMEM);
1452 cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
1453 BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
1454 sizeof(*cci_pmu->hw_events.used_mask),
1455 GFP_KERNEL);
1456 if (!cci_pmu->hw_events.used_mask)
1457 return ERR_PTR(-ENOMEM);
1458
1459 return cci_pmu;
1460}
1461
1462
1463static int cci_pmu_probe(struct platform_device *pdev)
1464{
1465 struct resource *res;
1466 struct cci_pmu *cci_pmu;
1467 int i, ret, irq;
1468
1469 cci_pmu = cci_pmu_alloc(pdev);
1470 if (IS_ERR(cci_pmu))
1471 return PTR_ERR(cci_pmu);
1472
b91c8f28 1473 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
a1a076d7
SP
1474 cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
1475 if (IS_ERR(cci_pmu->base))
fee4f2c6 1476 return -ENOMEM;
b91c8f28
PA
1477
1478 /*
ab5b316d 1479 * CCI PMU has one overflow interrupt per counter; but some may be tied
b91c8f28
PA
1480 * together to a common interrupt.
1481 */
a1a076d7 1482 cci_pmu->nr_irqs = 0;
ab5b316d 1483 for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
b91c8f28
PA
1484 irq = platform_get_irq(pdev, i);
1485 if (irq < 0)
1486 break;
1487
a1a076d7 1488 if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
b91c8f28
PA
1489 continue;
1490
a1a076d7 1491 cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
b91c8f28
PA
1492 }
1493
1494 /*
1495 * Ensure that the device tree has as many interrupts as the number
1496 * of counters.
1497 */
ab5b316d 1498 if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
b91c8f28 1499 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
ab5b316d 1500 i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
fee4f2c6 1501 return -EINVAL;
b91c8f28
PA
1502 }
1503
a1a076d7
SP
1504 raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
1505 mutex_init(&cci_pmu->reserve_mutex);
1506 atomic_set(&cci_pmu->active_events, 0);
1507 cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
c6f85cb4 1508
a1a076d7
SP
1509 cci_pmu->cpu_nb = (struct notifier_block) {
1510 .notifier_call = cci_pmu_cpu_notifier,
1511 /*
1512 * to migrate uncore events, our notifier should be executed
1513 * before perf core's notifier.
1514 */
1515 .priority = CPU_PRI_PERF + 1,
1516 };
1517
1518 ret = register_cpu_notifier(&cci_pmu->cpu_nb);
c6f85cb4
MR
1519 if (ret)
1520 return ret;
b91c8f28 1521
a1a076d7
SP
1522 ret = cci_pmu_init(cci_pmu, pdev);
1523 if (ret) {
1524 unregister_cpu_notifier(&cci_pmu->cpu_nb);
fee4f2c6 1525 return ret;
a1a076d7 1526 }
b91c8f28 1527
a1a076d7 1528 pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
b91c8f28 1529 return 0;
b91c8f28
PA
1530}
1531
1532static int cci_platform_probe(struct platform_device *pdev)
1533{
1534 if (!cci_probed())
1535 return -ENODEV;
1536
1537 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1538}
1539
f6b9e83c
SP
1540static struct platform_driver cci_pmu_driver = {
1541 .driver = {
1542 .name = DRIVER_NAME_PMU,
1543 .of_match_table = arm_cci_pmu_matches,
1544 },
1545 .probe = cci_pmu_probe,
1546};
1547
1548static struct platform_driver cci_platform_driver = {
1549 .driver = {
1550 .name = DRIVER_NAME,
1551 .of_match_table = arm_cci_matches,
1552 },
1553 .probe = cci_platform_probe,
1554};
1555
1556static int __init cci_platform_init(void)
1557{
1558 int ret;
1559
1560 ret = platform_driver_register(&cci_pmu_driver);
1561 if (ret)
1562 return ret;
1563
1564 return platform_driver_register(&cci_platform_driver);
1565}
1566
f4d58938 1567#else /* !CONFIG_ARM_CCI_PMU */
f6b9e83c
SP
1568
1569static int __init cci_platform_init(void)
1570{
1571 return 0;
1572}
1573
f4d58938 1574#endif /* CONFIG_ARM_CCI_PMU */
ee8e5d5f
SP
1575
1576#ifdef CONFIG_ARM_CCI400_PORT_CTRL
b91c8f28 1577
f6b9e83c
SP
1578#define CCI_PORT_CTRL 0x0
1579#define CCI_CTRL_STATUS 0xc
1580
1581#define CCI_ENABLE_SNOOP_REQ 0x1
1582#define CCI_ENABLE_DVM_REQ 0x2
1583#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1584
1585enum cci_ace_port_type {
1586 ACE_INVALID_PORT = 0x0,
1587 ACE_PORT,
1588 ACE_LITE_PORT,
1589};
1590
1591struct cci_ace_port {
1592 void __iomem *base;
1593 unsigned long phys;
1594 enum cci_ace_port_type type;
1595 struct device_node *dn;
1596};
1597
1598static struct cci_ace_port *ports;
1599static unsigned int nb_cci_ports;
1600
ed69bdd8
LP
1601struct cpu_port {
1602 u64 mpidr;
1603 u32 port;
1604};
62158f81 1605
ed69bdd8
LP
1606/*
1607 * Use the port MSB as valid flag, shift can be made dynamic
1608 * by computing number of bits required for port indexes.
1609 * Code disabling CCI cpu ports runs with D-cache invalidated
1610 * and SCTLR bit clear so data accesses must be kept to a minimum
1611 * to improve performance; for now shift is left static to
1612 * avoid one more data access while disabling the CCI port.
1613 */
1614#define PORT_VALID_SHIFT 31
1615#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1616
1617static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
1618{
1619 port->port = PORT_VALID | index;
1620 port->mpidr = mpidr;
1621}
1622
1623static inline bool cpu_port_is_valid(struct cpu_port *port)
1624{
1625 return !!(port->port & PORT_VALID);
1626}
1627
1628static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
1629{
1630 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
1631}
1632
1633static struct cpu_port cpu_port[NR_CPUS];
1634
1635/**
1636 * __cci_ace_get_port - Function to retrieve the port index connected to
1637 * a cpu or device.
1638 *
1639 * @dn: device node of the device to look-up
1640 * @type: port type
1641 *
1642 * Return value:
1643 * - CCI port index if success
1644 * - -ENODEV if failure
1645 */
1646static int __cci_ace_get_port(struct device_node *dn, int type)
1647{
1648 int i;
1649 bool ace_match;
1650 struct device_node *cci_portn;
1651
1652 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
1653 for (i = 0; i < nb_cci_ports; i++) {
1654 ace_match = ports[i].type == type;
1655 if (ace_match && cci_portn == ports[i].dn)
1656 return i;
1657 }
1658 return -ENODEV;
1659}
1660
1661int cci_ace_get_port(struct device_node *dn)
1662{
1663 return __cci_ace_get_port(dn, ACE_LITE_PORT);
1664}
1665EXPORT_SYMBOL_GPL(cci_ace_get_port);
1666
b91c8f28 1667static void cci_ace_init_ports(void)
ed69bdd8 1668{
78b4d6e0
SH
1669 int port, cpu;
1670 struct device_node *cpun;
ed69bdd8
LP
1671
1672 /*
1673 * Port index look-up speeds up the function disabling ports by CPU,
1674 * since the logical to port index mapping is done once and does
1675 * not change after system boot.
1676 * The stashed index array is initialized for all possible CPUs
1677 * at probe time.
1678 */
78b4d6e0
SH
1679 for_each_possible_cpu(cpu) {
1680 /* too early to use cpu->of_node */
1681 cpun = of_get_cpu_node(cpu, NULL);
ed69bdd8 1682
78b4d6e0 1683 if (WARN(!cpun, "Missing cpu device node\n"))
ed69bdd8 1684 continue;
78b4d6e0 1685
ed69bdd8
LP
1686 port = __cci_ace_get_port(cpun, ACE_PORT);
1687 if (port < 0)
1688 continue;
1689
1690 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
1691 }
1692
1693 for_each_possible_cpu(cpu) {
1694 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
1695 "CPU %u does not have an associated CCI port\n",
1696 cpu);
1697 }
1698}
1699/*
1700 * Functions to enable/disable a CCI interconnect slave port
1701 *
1702 * They are called by low-level power management code to disable slave
1703 * interfaces snoops and DVM broadcast.
1704 * Since they may execute with cache data allocation disabled and
1705 * after the caches have been cleaned and invalidated the functions provide
1706 * no explicit locking since they may run with D-cache disabled, so normal
1707 * cacheable kernel locks based on ldrex/strex may not work.
1708 * Locking has to be provided by BSP implementations to ensure proper
1709 * operations.
1710 */
1711
1712/**
1713 * cci_port_control() - function to control a CCI port
1714 *
1715 * @port: index of the port to setup
1716 * @enable: if true enables the port, if false disables it
1717 */
1718static void notrace cci_port_control(unsigned int port, bool enable)
1719{
1720 void __iomem *base = ports[port].base;
1721
1722 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
1723 /*
1724 * This function is called from power down procedures
1725 * and must not execute any instruction that might
1726 * cause the processor to be put in a quiescent state
1727 * (eg wfi). Hence, cpu_relax() can not be added to this
1728 * read loop to optimize power, since it might hide possibly
1729 * disruptive operations.
1730 */
1731 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
1732 ;
1733}
1734
1735/**
1736 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
1737 * reference
1738 *
1739 * @mpidr: mpidr of the CPU whose CCI port should be disabled
1740 *
1741 * Disabling a CCI port for a CPU implies disabling the CCI port
1742 * controlling that CPU cluster. Code disabling CPU CCI ports
1743 * must make sure that the CPU running the code is the last active CPU
1744 * in the cluster ie all other CPUs are quiescent in a low power state.
1745 *
1746 * Return:
1747 * 0 on success
1748 * -ENODEV on port look-up failure
1749 */
1750int notrace cci_disable_port_by_cpu(u64 mpidr)
1751{
1752 int cpu;
1753 bool is_valid;
1754 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
1755 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
1756 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
1757 cci_port_control(cpu_port[cpu].port, false);
1758 return 0;
1759 }
1760 }
1761 return -ENODEV;
1762}
1763EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
1764
62158f81
NP
1765/**
1766 * cci_enable_port_for_self() - enable a CCI port for calling CPU
1767 *
1768 * Enabling a CCI port for the calling CPU implies enabling the CCI
1769 * port controlling that CPU's cluster. Caller must make sure that the
1770 * CPU running the code is the first active CPU in the cluster and all
1771 * other CPUs are quiescent in a low power state or waiting for this CPU
1772 * to complete the CCI initialization.
1773 *
1774 * Because this is called when the MMU is still off and with no stack,
1775 * the code must be position independent and ideally rely on callee
1776 * clobbered registers only. To achieve this we must code this function
1777 * entirely in assembler.
1778 *
1779 * On success this returns with the proper CCI port enabled. In case of
1780 * any failure this never returns as the inability to enable the CCI is
1781 * fatal and there is no possible recovery at this stage.
1782 */
1783asmlinkage void __naked cci_enable_port_for_self(void)
1784{
1785 asm volatile ("\n"
f4902492 1786" .arch armv7-a\n"
62158f81
NP
1787" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
1788" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
1789" adr r1, 5f \n"
1790" ldr r2, [r1] \n"
1791" add r1, r1, r2 @ &cpu_port \n"
1792" add ip, r1, %[sizeof_cpu_port] \n"
1793
1794 /* Loop over the cpu_port array looking for a matching MPIDR */
1795"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
1796" cmp r2, r0 @ compare MPIDR \n"
1797" bne 2f \n"
1798
1799 /* Found a match, now test port validity */
1800" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
1801" tst r3, #"__stringify(PORT_VALID)" \n"
1802" bne 3f \n"
1803
1804 /* no match, loop with the next cpu_port entry */
1805"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
1806" cmp r1, ip @ done? \n"
1807" blo 1b \n"
1808
1809 /* CCI port not found -- cheaply try to stall this CPU */
1810"cci_port_not_found: \n"
1811" wfi \n"
1812" wfe \n"
1813" b cci_port_not_found \n"
1814
1815 /* Use matched port index to look up the corresponding ports entry */
1816"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
1817" adr r0, 6f \n"
1818" ldmia r0, {r1, r2} \n"
1819" sub r1, r1, r0 @ virt - phys \n"
1820" ldr r0, [r0, r2] @ *(&ports) \n"
1821" mov r2, %[sizeof_struct_ace_port] \n"
1822" mla r0, r2, r3, r0 @ &ports[index] \n"
1823" sub r0, r0, r1 @ virt_to_phys() \n"
1824
1825 /* Enable the CCI port */
1826" ldr r0, [r0, %[offsetof_port_phys]] \n"
fdb07aee 1827" mov r3, %[cci_enable_req]\n"
62158f81
NP
1828" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
1829
1830 /* poll the status reg for completion */
1831" adr r1, 7f \n"
1832" ldr r0, [r1] \n"
1833" ldr r0, [r0, r1] @ cci_ctrl_base \n"
1834"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
fdb07aee 1835" tst r1, %[cci_control_status_bits] \n"
62158f81
NP
1836" bne 4b \n"
1837
1838" mov r0, #0 \n"
1839" bx lr \n"
1840
1841" .align 2 \n"
1842"5: .word cpu_port - . \n"
1843"6: .word . \n"
1844" .word ports - 6b \n"
1845"7: .word cci_ctrl_phys - . \n"
1846 : :
1847 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
fdb07aee
VK
1848 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
1849 [cci_control_status_bits] "i" cpu_to_le32(1),
62158f81
NP
1850#ifndef __ARMEB__
1851 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
1852#else
1853 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
1854#endif
1855 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
1856 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
1857 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
1858 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
1859
1860 unreachable();
1861}
1862
ed69bdd8
LP
1863/**
1864 * __cci_control_port_by_device() - function to control a CCI port by device
1865 * reference
1866 *
1867 * @dn: device node pointer of the device whose CCI port should be
1868 * controlled
1869 * @enable: if true enables the port, if false disables it
1870 *
1871 * Return:
1872 * 0 on success
1873 * -ENODEV on port look-up failure
1874 */
1875int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
1876{
1877 int port;
1878
1879 if (!dn)
1880 return -ENODEV;
1881
1882 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
1883 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
1884 dn->full_name))
1885 return -ENODEV;
1886 cci_port_control(port, enable);
1887 return 0;
1888}
1889EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
1890
1891/**
1892 * __cci_control_port_by_index() - function to control a CCI port by port index
1893 *
1894 * @port: port index previously retrieved with cci_ace_get_port()
1895 * @enable: if true enables the port, if false disables it
1896 *
1897 * Return:
1898 * 0 on success
1899 * -ENODEV on port index out of range
1900 * -EPERM if operation carried out on an ACE PORT
1901 */
1902int notrace __cci_control_port_by_index(u32 port, bool enable)
1903{
1904 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
1905 return -ENODEV;
1906 /*
1907 * CCI control for ports connected to CPUS is extremely fragile
1908 * and must be made to go through a specific and controlled
1909 * interface (ie cci_disable_port_by_cpu(); control by general purpose
1910 * indexing is therefore disabled for ACE ports.
1911 */
1912 if (ports[port].type == ACE_PORT)
1913 return -EPERM;
1914
1915 cci_port_control(port, enable);
1916 return 0;
1917}
1918EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
1919
ed69bdd8
LP
1920static const struct of_device_id arm_cci_ctrl_if_matches[] = {
1921 {.compatible = "arm,cci-400-ctrl-if", },
1922 {},
1923};
1924
f6b9e83c 1925static int cci_probe_ports(struct device_node *np)
ed69bdd8
LP
1926{
1927 struct cci_nb_ports const *cci_config;
1928 int ret, i, nb_ace = 0, nb_ace_lite = 0;
f6b9e83c 1929 struct device_node *cp;
62158f81 1930 struct resource res;
ed69bdd8
LP
1931 const char *match_str;
1932 bool is_ace;
1933
896ddd60 1934
ed69bdd8
LP
1935 cci_config = of_match_node(arm_cci_matches, np)->data;
1936 if (!cci_config)
1937 return -ENODEV;
1938
1939 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
1940
7c762036 1941 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
ed69bdd8
LP
1942 if (!ports)
1943 return -ENOMEM;
1944
ed69bdd8
LP
1945 for_each_child_of_node(np, cp) {
1946 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
1947 continue;
1948
1949 i = nb_ace + nb_ace_lite;
1950
1951 if (i >= nb_cci_ports)
1952 break;
1953
1954 if (of_property_read_string(cp, "interface-type",
1955 &match_str)) {
1956 WARN(1, "node %s missing interface-type property\n",
1957 cp->full_name);
1958 continue;
1959 }
1960 is_ace = strcmp(match_str, "ace") == 0;
1961 if (!is_ace && strcmp(match_str, "ace-lite")) {
1962 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
1963 cp->full_name);
1964 continue;
1965 }
1966
62158f81
NP
1967 ret = of_address_to_resource(cp, 0, &res);
1968 if (!ret) {
1969 ports[i].base = ioremap(res.start, resource_size(&res));
1970 ports[i].phys = res.start;
1971 }
1972 if (ret || !ports[i].base) {
ed69bdd8
LP
1973 WARN(1, "unable to ioremap CCI port %d\n", i);
1974 continue;
1975 }
1976
1977 if (is_ace) {
1978 if (WARN_ON(nb_ace >= cci_config->nb_ace))
1979 continue;
1980 ports[i].type = ACE_PORT;
1981 ++nb_ace;
1982 } else {
1983 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
1984 continue;
1985 ports[i].type = ACE_LITE_PORT;
1986 ++nb_ace_lite;
1987 }
1988 ports[i].dn = cp;
1989 }
1990
1991 /* initialize a stashed array of ACE ports to speed-up look-up */
1992 cci_ace_init_ports();
1993
1994 /*
1995 * Multi-cluster systems may need this data when non-coherent, during
1996 * cluster power-up/power-down. Make sure it reaches main memory.
1997 */
1998 sync_cache_w(&cci_ctrl_base);
62158f81 1999 sync_cache_w(&cci_ctrl_phys);
ed69bdd8
LP
2000 sync_cache_w(&ports);
2001 sync_cache_w(&cpu_port);
2002 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
2003 pr_info("ARM CCI driver probed\n");
f6b9e83c 2004
ed69bdd8 2005 return 0;
f6b9e83c 2006}
ee8e5d5f
SP
2007#else /* !CONFIG_ARM_CCI400_PORT_CTRL */
2008static inline int cci_probe_ports(struct device_node *np)
2009{
2010 return 0;
2011}
2012#endif /* CONFIG_ARM_CCI400_PORT_CTRL */
ed69bdd8 2013
f6b9e83c
SP
2014static int cci_probe(void)
2015{
2016 int ret;
2017 struct device_node *np;
2018 struct resource res;
ed69bdd8 2019
f6b9e83c
SP
2020 np = of_find_matching_node(NULL, arm_cci_matches);
2021 if(!np || !of_device_is_available(np))
2022 return -ENODEV;
2023
2024 ret = of_address_to_resource(np, 0, &res);
2025 if (!ret) {
2026 cci_ctrl_base = ioremap(res.start, resource_size(&res));
2027 cci_ctrl_phys = res.start;
2028 }
2029 if (ret || !cci_ctrl_base) {
2030 WARN(1, "unable to ioremap CCI ctrl\n");
2031 return -ENXIO;
2032 }
2033
2034 return cci_probe_ports(np);
ed69bdd8
LP
2035}
2036
2037static int cci_init_status = -EAGAIN;
2038static DEFINE_MUTEX(cci_probing);
2039
b91c8f28 2040static int cci_init(void)
ed69bdd8
LP
2041{
2042 if (cci_init_status != -EAGAIN)
2043 return cci_init_status;
2044
2045 mutex_lock(&cci_probing);
2046 if (cci_init_status == -EAGAIN)
2047 cci_init_status = cci_probe();
2048 mutex_unlock(&cci_probing);
2049 return cci_init_status;
2050}
2051
2052/*
2053 * To sort out early init calls ordering a helper function is provided to
2054 * check if the CCI driver has beed initialized. Function check if the driver
2055 * has been initialized, if not it calls the init function that probes
2056 * the driver and updates the return value.
2057 */
b91c8f28 2058bool cci_probed(void)
ed69bdd8
LP
2059{
2060 return cci_init() == 0;
2061}
2062EXPORT_SYMBOL_GPL(cci_probed);
2063
2064early_initcall(cci_init);
b91c8f28 2065core_initcall(cci_platform_init);
ed69bdd8
LP
2066MODULE_LICENSE("GPL");
2067MODULE_DESCRIPTION("ARM CCI support");