1 // SPDX-License-Identifier: GPL-2.0
2 // CCI Cache Coherent Interconnect PMU driver
3 // Copyright (C) 2013-2018 Arm Ltd.
4 // Author: Punit Agrawal <punit.agrawal@arm.com>, Suzuki Poulose <suzuki.poulose@arm.com>
6 #include <linux/arm-cci.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
11 #include <linux/perf_event.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
16 #define DRIVER_NAME "ARM-CCI PMU"
18 #define CCI_PMCR 0x0100
19 #define CCI_PID2 0x0fe8
21 #define CCI_PMCR_CEN 0x00000001
22 #define CCI_PMCR_NCNT_MASK 0x0000f800
23 #define CCI_PMCR_NCNT_SHIFT 11
25 #define CCI_PID2_REV_MASK 0xf0
26 #define CCI_PID2_REV_SHIFT 4
28 #define CCI_PMU_EVT_SEL 0x000
29 #define CCI_PMU_CNTR 0x004
30 #define CCI_PMU_CNTR_CTRL 0x008
31 #define CCI_PMU_OVRFLW 0x00c
33 #define CCI_PMU_OVRFLW_FLAG 1
35 #define CCI_PMU_CNTR_SIZE(model) ((model)->cntr_size)
36 #define CCI_PMU_CNTR_BASE(model, idx) ((idx) * CCI_PMU_CNTR_SIZE(model))
37 #define CCI_PMU_CNTR_MASK ((1ULL << 32) - 1)
38 #define CCI_PMU_CNTR_LAST(cci_pmu) (cci_pmu->num_cntrs - 1)
40 #define CCI_PMU_MAX_HW_CNTRS(model) \
41 ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
43 /* Types of interfaces that can generate events */
47 #ifdef CONFIG_ARM_CCI5xx_PMU
53 #define NUM_HW_CNTRS_CII_4XX 4
54 #define NUM_HW_CNTRS_CII_5XX 8
55 #define NUM_HW_CNTRS_MAX NUM_HW_CNTRS_CII_5XX
57 #define FIXED_HW_CNTRS_CII_4XX 1
58 #define FIXED_HW_CNTRS_CII_5XX 0
59 #define FIXED_HW_CNTRS_MAX FIXED_HW_CNTRS_CII_4XX
61 #define HW_CNTRS_MAX (NUM_HW_CNTRS_MAX + FIXED_HW_CNTRS_MAX)
68 struct cci_pmu_hw_events
{
69 struct perf_event
**events
;
70 unsigned long *used_mask
;
71 raw_spinlock_t pmu_lock
;
76 * struct cci_pmu_model:
77 * @fixed_hw_cntrs - Number of fixed event counters
78 * @num_hw_cntrs - Maximum number of programmable event counters
79 * @cntr_size - Size of an event counter mapping
81 struct cci_pmu_model
{
86 struct attribute
**format_attrs
;
87 struct attribute
**event_attrs
;
88 struct event_range event_ranges
[CCI_IF_MAX
];
89 int (*validate_hw_event
)(struct cci_pmu
*, unsigned long);
90 int (*get_event_idx
)(struct cci_pmu
*, struct cci_pmu_hw_events
*, unsigned long);
91 void (*write_counters
)(struct cci_pmu
*, unsigned long *);
94 static struct cci_pmu_model cci_pmu_models
[];
98 void __iomem
*ctrl_base
;
103 unsigned long active_irqs
;
104 const struct cci_pmu_model
*model
;
105 struct cci_pmu_hw_events hw_events
;
106 struct platform_device
*plat_device
;
108 atomic_t active_events
;
109 struct mutex reserve_mutex
;
112 #define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
114 static struct cci_pmu
*g_cci_pmu
;
117 #ifdef CONFIG_ARM_CCI400_PMU
121 #ifdef CONFIG_ARM_CCI5xx_PMU
128 static void pmu_write_counters(struct cci_pmu
*cci_pmu
,
129 unsigned long *mask
);
130 static ssize_t __maybe_unused
cci_pmu_event_show(struct device
*dev
,
131 struct device_attribute
*attr
, char *buf
);
133 #define CCI_EXT_ATTR_ENTRY(_name, _func, _config) \
134 &((struct dev_ext_attribute[]) { \
135 { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config } \
138 #define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
139 CCI_EXT_ATTR_ENTRY(_name, device_show_string, _config)
140 #define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
141 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
143 /* CCI400 PMU Specific definitions */
145 #ifdef CONFIG_ARM_CCI400_PMU
148 #define CCI400_PORT_S0 0
149 #define CCI400_PORT_S1 1
150 #define CCI400_PORT_S2 2
151 #define CCI400_PORT_S3 3
152 #define CCI400_PORT_S4 4
153 #define CCI400_PORT_M0 5
154 #define CCI400_PORT_M1 6
155 #define CCI400_PORT_M2 7
157 #define CCI400_R1_PX 5
160 * Instead of an event id to monitor CCI cycles, a dedicated counter is
161 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
162 * make use of this event in hardware.
164 enum cci400_perf_events
{
165 CCI400_PMU_CYCLES
= 0xff
168 #define CCI400_PMU_CYCLE_CNTR_IDX 0
169 #define CCI400_PMU_CNTR0_IDX 1
172 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
173 * ports and bits 4:0 are event codes. There are different event codes
174 * associated with each port type.
176 * Additionally, the range of events associated with the port types changed
177 * between Rev0 and Rev1.
179 * The constants below define the range of valid codes for each port type for
180 * the different revisions and are used to validate the event to be monitored.
183 #define CCI400_PMU_EVENT_MASK 0xffUL
184 #define CCI400_PMU_EVENT_SOURCE_SHIFT 5
185 #define CCI400_PMU_EVENT_SOURCE_MASK 0x7
186 #define CCI400_PMU_EVENT_CODE_SHIFT 0
187 #define CCI400_PMU_EVENT_CODE_MASK 0x1f
188 #define CCI400_PMU_EVENT_SOURCE(event) \
189 ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
190 CCI400_PMU_EVENT_SOURCE_MASK)
191 #define CCI400_PMU_EVENT_CODE(event) \
192 ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
194 #define CCI400_R0_SLAVE_PORT_MIN_EV 0x00
195 #define CCI400_R0_SLAVE_PORT_MAX_EV 0x13
196 #define CCI400_R0_MASTER_PORT_MIN_EV 0x14
197 #define CCI400_R0_MASTER_PORT_MAX_EV 0x1a
199 #define CCI400_R1_SLAVE_PORT_MIN_EV 0x00
200 #define CCI400_R1_SLAVE_PORT_MAX_EV 0x14
201 #define CCI400_R1_MASTER_PORT_MIN_EV 0x00
202 #define CCI400_R1_MASTER_PORT_MAX_EV 0x11
204 #define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
205 CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
206 (unsigned long)_config)
208 static ssize_t
cci400_pmu_cycle_event_show(struct device
*dev
,
209 struct device_attribute
*attr
, char *buf
);
211 static struct attribute
*cci400_pmu_format_attrs
[] = {
212 CCI_FORMAT_EXT_ATTR_ENTRY(event
, "config:0-4"),
213 CCI_FORMAT_EXT_ATTR_ENTRY(source
, "config:5-7"),
217 static struct attribute
*cci400_r0_pmu_event_attrs
[] = {
219 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any
, 0x0),
220 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device
, 0x01),
221 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable
, 0x2),
222 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable
, 0x3),
223 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance
, 0x4),
224 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier
, 0x5),
225 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier
, 0x6),
226 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg
, 0x7),
227 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync
, 0x8),
228 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full
, 0x9),
229 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop
, 0xA),
230 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l
, 0xB),
231 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any
, 0xC),
232 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device
, 0xD),
233 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable
, 0xE),
234 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean
, 0xF),
235 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique
, 0x10),
236 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique
, 0x11),
237 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict
, 0x12),
238 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full
, 0x13),
240 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch
, 0x14),
241 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard
, 0x15),
242 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard
, 0x16),
243 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full
, 0x17),
244 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard
, 0x18),
245 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard
, 0x19),
246 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full
, 0x1A),
247 /* Special event for cycles counter */
248 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles
, 0xff),
252 static struct attribute
*cci400_r1_pmu_event_attrs
[] = {
254 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any
, 0x0),
255 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device
, 0x01),
256 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable
, 0x2),
257 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable
, 0x3),
258 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance
, 0x4),
259 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier
, 0x5),
260 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier
, 0x6),
261 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg
, 0x7),
262 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync
, 0x8),
263 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full
, 0x9),
264 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop
, 0xA),
265 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l
, 0xB),
266 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any
, 0xC),
267 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device
, 0xD),
268 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable
, 0xE),
269 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean
, 0xF),
270 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique
, 0x10),
271 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique
, 0x11),
272 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict
, 0x12),
273 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full
, 0x13),
274 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard
, 0x14),
276 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch
, 0x0),
277 CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard
, 0x1),
278 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard
, 0x2),
279 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full
, 0x3),
280 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard
, 0x4),
281 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard
, 0x5),
282 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full
, 0x6),
283 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full
, 0x7),
284 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full
, 0x8),
285 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0
, 0x9),
286 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1
, 0xA),
287 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2
, 0xB),
288 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3
, 0xC),
289 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0
, 0xD),
290 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1
, 0xE),
291 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2
, 0xF),
292 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3
, 0x10),
293 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard
, 0x11),
294 /* Special event for cycles counter */
295 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles
, 0xff),
299 static ssize_t
cci400_pmu_cycle_event_show(struct device
*dev
,
300 struct device_attribute
*attr
, char *buf
)
302 struct dev_ext_attribute
*eattr
= container_of(attr
,
303 struct dev_ext_attribute
, attr
);
304 return sysfs_emit(buf
, "config=0x%lx\n", (unsigned long)eattr
->var
);
307 static int cci400_get_event_idx(struct cci_pmu
*cci_pmu
,
308 struct cci_pmu_hw_events
*hw
,
309 unsigned long cci_event
)
313 /* cycles event idx is fixed */
314 if (cci_event
== CCI400_PMU_CYCLES
) {
315 if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX
, hw
->used_mask
))
318 return CCI400_PMU_CYCLE_CNTR_IDX
;
321 for (idx
= CCI400_PMU_CNTR0_IDX
; idx
<= CCI_PMU_CNTR_LAST(cci_pmu
); ++idx
)
322 if (!test_and_set_bit(idx
, hw
->used_mask
))
325 /* No counters available */
329 static int cci400_validate_hw_event(struct cci_pmu
*cci_pmu
, unsigned long hw_event
)
331 u8 ev_source
= CCI400_PMU_EVENT_SOURCE(hw_event
);
332 u8 ev_code
= CCI400_PMU_EVENT_CODE(hw_event
);
335 if (hw_event
& ~CCI400_PMU_EVENT_MASK
)
338 if (hw_event
== CCI400_PMU_CYCLES
)
347 /* Slave Interface */
348 if_type
= CCI_IF_SLAVE
;
353 /* Master Interface */
354 if_type
= CCI_IF_MASTER
;
360 if (ev_code
>= cci_pmu
->model
->event_ranges
[if_type
].min
&&
361 ev_code
<= cci_pmu
->model
->event_ranges
[if_type
].max
)
367 static int probe_cci400_revision(struct cci_pmu
*cci_pmu
)
370 rev
= readl_relaxed(cci_pmu
->ctrl_base
+ CCI_PID2
) & CCI_PID2_REV_MASK
;
371 rev
>>= CCI_PID2_REV_SHIFT
;
373 if (rev
< CCI400_R1_PX
)
379 static const struct cci_pmu_model
*probe_cci_model(struct cci_pmu
*cci_pmu
)
381 if (platform_has_secure_cci_access())
382 return &cci_pmu_models
[probe_cci400_revision(cci_pmu
)];
385 #else /* !CONFIG_ARM_CCI400_PMU */
386 static inline struct cci_pmu_model
*probe_cci_model(struct cci_pmu
*cci_pmu
)
390 #endif /* CONFIG_ARM_CCI400_PMU */
392 #ifdef CONFIG_ARM_CCI5xx_PMU
395 * CCI5xx PMU event id is an 9-bit value made of two parts.
396 * bits [8:5] - Source for the event
397 * bits [4:0] - Event code (specific to type of interface)
403 #define CCI5xx_PORT_S0 0x0
404 #define CCI5xx_PORT_S1 0x1
405 #define CCI5xx_PORT_S2 0x2
406 #define CCI5xx_PORT_S3 0x3
407 #define CCI5xx_PORT_S4 0x4
408 #define CCI5xx_PORT_S5 0x5
409 #define CCI5xx_PORT_S6 0x6
411 #define CCI5xx_PORT_M0 0x8
412 #define CCI5xx_PORT_M1 0x9
413 #define CCI5xx_PORT_M2 0xa
414 #define CCI5xx_PORT_M3 0xb
415 #define CCI5xx_PORT_M4 0xc
416 #define CCI5xx_PORT_M5 0xd
417 #define CCI5xx_PORT_M6 0xe
419 #define CCI5xx_PORT_GLOBAL 0xf
421 #define CCI5xx_PMU_EVENT_MASK 0x1ffUL
422 #define CCI5xx_PMU_EVENT_SOURCE_SHIFT 0x5
423 #define CCI5xx_PMU_EVENT_SOURCE_MASK 0xf
424 #define CCI5xx_PMU_EVENT_CODE_SHIFT 0x0
425 #define CCI5xx_PMU_EVENT_CODE_MASK 0x1f
427 #define CCI5xx_PMU_EVENT_SOURCE(event) \
428 ((event >> CCI5xx_PMU_EVENT_SOURCE_SHIFT) & CCI5xx_PMU_EVENT_SOURCE_MASK)
429 #define CCI5xx_PMU_EVENT_CODE(event) \
430 ((event >> CCI5xx_PMU_EVENT_CODE_SHIFT) & CCI5xx_PMU_EVENT_CODE_MASK)
432 #define CCI5xx_SLAVE_PORT_MIN_EV 0x00
433 #define CCI5xx_SLAVE_PORT_MAX_EV 0x1f
434 #define CCI5xx_MASTER_PORT_MIN_EV 0x00
435 #define CCI5xx_MASTER_PORT_MAX_EV 0x06
436 #define CCI5xx_GLOBAL_PORT_MIN_EV 0x00
437 #define CCI5xx_GLOBAL_PORT_MAX_EV 0x0f
440 #define CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
441 CCI_EXT_ATTR_ENTRY(_name, cci5xx_pmu_global_event_show, \
442 (unsigned long) _config)
444 static ssize_t
cci5xx_pmu_global_event_show(struct device
*dev
,
445 struct device_attribute
*attr
, char *buf
);
447 static struct attribute
*cci5xx_pmu_format_attrs
[] = {
448 CCI_FORMAT_EXT_ATTR_ENTRY(event
, "config:0-4"),
449 CCI_FORMAT_EXT_ATTR_ENTRY(source
, "config:5-8"),
453 static struct attribute
*cci5xx_pmu_event_attrs
[] = {
455 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_arvalid
, 0x0),
456 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_dev
, 0x1),
457 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_nonshareable
, 0x2),
458 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_non_alloc
, 0x3),
459 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_alloc
, 0x4),
460 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_invalidate
, 0x5),
461 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maint
, 0x6),
462 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg
, 0x7),
463 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rval
, 0x8),
464 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rlast_snoop
, 0x9),
465 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_awalid
, 0xA),
466 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_dev
, 0xB),
467 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_non_shareable
, 0xC),
468 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wb
, 0xD),
469 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wlu
, 0xE),
470 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wunique
, 0xF),
471 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_evict
, 0x10),
472 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_wrevict
, 0x11),
473 CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_beat
, 0x12),
474 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_acvalid
, 0x13),
475 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_read
, 0x14),
476 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_clean
, 0x15),
477 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_data_transfer_low
, 0x16),
478 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_arvalid
, 0x17),
479 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall
, 0x18),
480 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall
, 0x19),
481 CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_stall
, 0x1A),
482 CCI_EVENT_EXT_ATTR_ENTRY(si_w_resp_stall
, 0x1B),
483 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_stall
, 0x1C),
484 CCI_EVENT_EXT_ATTR_ENTRY(si_s_data_stall
, 0x1D),
485 CCI_EVENT_EXT_ATTR_ENTRY(si_rq_stall_ot_limit
, 0x1E),
486 CCI_EVENT_EXT_ATTR_ENTRY(si_r_stall_arbit
, 0x1F),
489 CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_beat_any
, 0x0),
490 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_beat_any
, 0x1),
491 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall
, 0x2),
492 CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_stall
, 0x3),
493 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall
, 0x4),
494 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_stall
, 0x5),
495 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_resp_stall
, 0x6),
498 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1
, 0x0),
499 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3
, 0x1),
500 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5
, 0x2),
501 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7
, 0x3),
502 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1
, 0x4),
503 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3
, 0x5),
504 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5
, 0x6),
505 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7
, 0x7),
506 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation
, 0x8),
507 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy
, 0x9),
508 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full
, 0xA),
509 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq
, 0xB),
510 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs
, 0xC),
511 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard
, 0xD),
512 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_stall_tt_full
, 0xE),
513 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot
, 0xF),
517 static ssize_t
cci5xx_pmu_global_event_show(struct device
*dev
,
518 struct device_attribute
*attr
, char *buf
)
520 struct dev_ext_attribute
*eattr
= container_of(attr
,
521 struct dev_ext_attribute
, attr
);
522 /* Global events have single fixed source code */
523 return sysfs_emit(buf
, "event=0x%lx,source=0x%x\n",
524 (unsigned long)eattr
->var
, CCI5xx_PORT_GLOBAL
);
528 * CCI500 provides 8 independent event counters that can count
529 * any of the events available.
530 * CCI500 PMU event source ids
531 * 0x0-0x6 - Slave interfaces
532 * 0x8-0xD - Master interfaces
533 * 0xf - Global Events
536 static int cci500_validate_hw_event(struct cci_pmu
*cci_pmu
,
537 unsigned long hw_event
)
539 u32 ev_source
= CCI5xx_PMU_EVENT_SOURCE(hw_event
);
540 u32 ev_code
= CCI5xx_PMU_EVENT_CODE(hw_event
);
543 if (hw_event
& ~CCI5xx_PMU_EVENT_MASK
)
554 if_type
= CCI_IF_SLAVE
;
562 if_type
= CCI_IF_MASTER
;
564 case CCI5xx_PORT_GLOBAL
:
565 if_type
= CCI_IF_GLOBAL
;
571 if (ev_code
>= cci_pmu
->model
->event_ranges
[if_type
].min
&&
572 ev_code
<= cci_pmu
->model
->event_ranges
[if_type
].max
)
579 * CCI550 provides 8 independent event counters that can count
580 * any of the events available.
581 * CCI550 PMU event source ids
582 * 0x0-0x6 - Slave interfaces
583 * 0x8-0xe - Master interfaces
584 * 0xf - Global Events
587 static int cci550_validate_hw_event(struct cci_pmu
*cci_pmu
,
588 unsigned long hw_event
)
590 u32 ev_source
= CCI5xx_PMU_EVENT_SOURCE(hw_event
);
591 u32 ev_code
= CCI5xx_PMU_EVENT_CODE(hw_event
);
594 if (hw_event
& ~CCI5xx_PMU_EVENT_MASK
)
605 if_type
= CCI_IF_SLAVE
;
614 if_type
= CCI_IF_MASTER
;
616 case CCI5xx_PORT_GLOBAL
:
617 if_type
= CCI_IF_GLOBAL
;
623 if (ev_code
>= cci_pmu
->model
->event_ranges
[if_type
].min
&&
624 ev_code
<= cci_pmu
->model
->event_ranges
[if_type
].max
)
630 #endif /* CONFIG_ARM_CCI5xx_PMU */
633 * Program the CCI PMU counters which have PERF_HES_ARCH set
634 * with the event period and mark them ready before we enable
637 static void cci_pmu_sync_counters(struct cci_pmu
*cci_pmu
)
640 struct cci_pmu_hw_events
*cci_hw
= &cci_pmu
->hw_events
;
641 DECLARE_BITMAP(mask
, HW_CNTRS_MAX
);
643 bitmap_zero(mask
, HW_CNTRS_MAX
);
644 for_each_set_bit(i
, cci_pmu
->hw_events
.used_mask
, cci_pmu
->num_cntrs
) {
645 struct perf_event
*event
= cci_hw
->events
[i
];
650 /* Leave the events which are not counting */
651 if (event
->hw
.state
& PERF_HES_STOPPED
)
653 if (event
->hw
.state
& PERF_HES_ARCH
) {
655 event
->hw
.state
&= ~PERF_HES_ARCH
;
659 pmu_write_counters(cci_pmu
, mask
);
662 /* Should be called with cci_pmu->hw_events->pmu_lock held */
663 static void __cci_pmu_enable_nosync(struct cci_pmu
*cci_pmu
)
667 /* Enable all the PMU counters. */
668 val
= readl_relaxed(cci_pmu
->ctrl_base
+ CCI_PMCR
) | CCI_PMCR_CEN
;
669 writel(val
, cci_pmu
->ctrl_base
+ CCI_PMCR
);
672 /* Should be called with cci_pmu->hw_events->pmu_lock held */
673 static void __cci_pmu_enable_sync(struct cci_pmu
*cci_pmu
)
675 cci_pmu_sync_counters(cci_pmu
);
676 __cci_pmu_enable_nosync(cci_pmu
);
679 /* Should be called with cci_pmu->hw_events->pmu_lock held */
680 static void __cci_pmu_disable(struct cci_pmu
*cci_pmu
)
684 /* Disable all the PMU counters. */
685 val
= readl_relaxed(cci_pmu
->ctrl_base
+ CCI_PMCR
) & ~CCI_PMCR_CEN
;
686 writel(val
, cci_pmu
->ctrl_base
+ CCI_PMCR
);
689 static ssize_t
cci_pmu_event_show(struct device
*dev
,
690 struct device_attribute
*attr
, char *buf
)
692 struct dev_ext_attribute
*eattr
= container_of(attr
,
693 struct dev_ext_attribute
, attr
);
694 /* source parameter is mandatory for normal PMU events */
695 return sysfs_emit(buf
, "source=?,event=0x%lx\n",
696 (unsigned long)eattr
->var
);
699 static int pmu_is_valid_counter(struct cci_pmu
*cci_pmu
, int idx
)
701 return 0 <= idx
&& idx
<= CCI_PMU_CNTR_LAST(cci_pmu
);
704 static u32
pmu_read_register(struct cci_pmu
*cci_pmu
, int idx
, unsigned int offset
)
706 return readl_relaxed(cci_pmu
->base
+
707 CCI_PMU_CNTR_BASE(cci_pmu
->model
, idx
) + offset
);
710 static void pmu_write_register(struct cci_pmu
*cci_pmu
, u32 value
,
711 int idx
, unsigned int offset
)
713 writel_relaxed(value
, cci_pmu
->base
+
714 CCI_PMU_CNTR_BASE(cci_pmu
->model
, idx
) + offset
);
717 static void pmu_disable_counter(struct cci_pmu
*cci_pmu
, int idx
)
719 pmu_write_register(cci_pmu
, 0, idx
, CCI_PMU_CNTR_CTRL
);
722 static void pmu_enable_counter(struct cci_pmu
*cci_pmu
, int idx
)
724 pmu_write_register(cci_pmu
, 1, idx
, CCI_PMU_CNTR_CTRL
);
727 static bool __maybe_unused
728 pmu_counter_is_enabled(struct cci_pmu
*cci_pmu
, int idx
)
730 return (pmu_read_register(cci_pmu
, idx
, CCI_PMU_CNTR_CTRL
) & 0x1) != 0;
733 static void pmu_set_event(struct cci_pmu
*cci_pmu
, int idx
, unsigned long event
)
735 pmu_write_register(cci_pmu
, event
, idx
, CCI_PMU_EVT_SEL
);
739 * For all counters on the CCI-PMU, disable any 'enabled' counters,
740 * saving the changed counters in the mask, so that we can restore
741 * it later using pmu_restore_counters. The mask is private to the
742 * caller. We cannot rely on the used_mask maintained by the CCI_PMU
743 * as it only tells us if the counter is assigned to perf_event or not.
744 * The state of the perf_event cannot be locked by the PMU layer, hence
745 * we check the individual counter status (which can be locked by
746 * cci_pm->hw_events->pmu_lock).
748 * @mask should be initialised to empty by the caller.
750 static void __maybe_unused
751 pmu_save_counters(struct cci_pmu
*cci_pmu
, unsigned long *mask
)
755 for (i
= 0; i
< cci_pmu
->num_cntrs
; i
++) {
756 if (pmu_counter_is_enabled(cci_pmu
, i
)) {
758 pmu_disable_counter(cci_pmu
, i
);
764 * Restore the status of the counters. Reversal of the pmu_save_counters().
765 * For each counter set in the mask, enable the counter back.
767 static void __maybe_unused
768 pmu_restore_counters(struct cci_pmu
*cci_pmu
, unsigned long *mask
)
772 for_each_set_bit(i
, mask
, cci_pmu
->num_cntrs
)
773 pmu_enable_counter(cci_pmu
, i
);
777 * Returns the number of programmable counters actually implemented
780 static u32
pmu_get_max_counters(struct cci_pmu
*cci_pmu
)
782 return (readl_relaxed(cci_pmu
->ctrl_base
+ CCI_PMCR
) &
783 CCI_PMCR_NCNT_MASK
) >> CCI_PMCR_NCNT_SHIFT
;
786 static int pmu_get_event_idx(struct cci_pmu_hw_events
*hw
, struct perf_event
*event
)
788 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
789 unsigned long cci_event
= event
->hw
.config_base
;
792 if (cci_pmu
->model
->get_event_idx
)
793 return cci_pmu
->model
->get_event_idx(cci_pmu
, hw
, cci_event
);
795 /* Generic code to find an unused idx from the mask */
796 for (idx
= 0; idx
<= CCI_PMU_CNTR_LAST(cci_pmu
); idx
++)
797 if (!test_and_set_bit(idx
, hw
->used_mask
))
800 /* No counters available */
804 static int pmu_map_event(struct perf_event
*event
)
806 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
808 if (event
->attr
.type
< PERF_TYPE_MAX
||
809 !cci_pmu
->model
->validate_hw_event
)
812 return cci_pmu
->model
->validate_hw_event(cci_pmu
, event
->attr
.config
);
815 static int pmu_request_irq(struct cci_pmu
*cci_pmu
, irq_handler_t handler
)
818 struct platform_device
*pmu_device
= cci_pmu
->plat_device
;
820 if (unlikely(!pmu_device
))
823 if (cci_pmu
->nr_irqs
< 1) {
824 dev_err(&pmu_device
->dev
, "no irqs for CCI PMUs defined\n");
829 * Register all available CCI PMU interrupts. In the interrupt handler
830 * we iterate over the counters checking for interrupt source (the
831 * overflowing counter) and clear it.
833 * This should allow handling of non-unique interrupt for the counters.
835 for (i
= 0; i
< cci_pmu
->nr_irqs
; i
++) {
836 int err
= request_irq(cci_pmu
->irqs
[i
], handler
, IRQF_SHARED
,
837 "arm-cci-pmu", cci_pmu
);
839 dev_err(&pmu_device
->dev
, "unable to request IRQ%d for ARM CCI PMU counters\n",
844 set_bit(i
, &cci_pmu
->active_irqs
);
850 static void pmu_free_irq(struct cci_pmu
*cci_pmu
)
854 for (i
= 0; i
< cci_pmu
->nr_irqs
; i
++) {
855 if (!test_and_clear_bit(i
, &cci_pmu
->active_irqs
))
858 free_irq(cci_pmu
->irqs
[i
], cci_pmu
);
862 static u32
pmu_read_counter(struct perf_event
*event
)
864 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
865 struct hw_perf_event
*hw_counter
= &event
->hw
;
866 int idx
= hw_counter
->idx
;
869 if (unlikely(!pmu_is_valid_counter(cci_pmu
, idx
))) {
870 dev_err(&cci_pmu
->plat_device
->dev
, "Invalid CCI PMU counter %d\n", idx
);
873 value
= pmu_read_register(cci_pmu
, idx
, CCI_PMU_CNTR
);
878 static void pmu_write_counter(struct cci_pmu
*cci_pmu
, u32 value
, int idx
)
880 pmu_write_register(cci_pmu
, value
, idx
, CCI_PMU_CNTR
);
883 static void __pmu_write_counters(struct cci_pmu
*cci_pmu
, unsigned long *mask
)
886 struct cci_pmu_hw_events
*cci_hw
= &cci_pmu
->hw_events
;
888 for_each_set_bit(i
, mask
, cci_pmu
->num_cntrs
) {
889 struct perf_event
*event
= cci_hw
->events
[i
];
893 pmu_write_counter(cci_pmu
, local64_read(&event
->hw
.prev_count
), i
);
897 static void pmu_write_counters(struct cci_pmu
*cci_pmu
, unsigned long *mask
)
899 if (cci_pmu
->model
->write_counters
)
900 cci_pmu
->model
->write_counters(cci_pmu
, mask
);
902 __pmu_write_counters(cci_pmu
, mask
);
905 #ifdef CONFIG_ARM_CCI5xx_PMU
908 * CCI-500/CCI-550 has advanced power saving policies, which could gate the
909 * clocks to the PMU counters, which makes the writes to them ineffective.
910 * The only way to write to those counters is when the global counters
911 * are enabled and the particular counter is enabled.
913 * So we do the following :
915 * 1) Disable all the PMU counters, saving their current state
916 * 2) Enable the global PMU profiling, now that all counters are
919 * For each counter to be programmed, repeat steps 3-7:
921 * 3) Write an invalid event code to the event control register for the
922 counter, so that the counters are not modified.
923 * 4) Enable the counter control for the counter.
924 * 5) Set the counter value
925 * 6) Disable the counter
926 * 7) Restore the event in the target counter
928 * 8) Disable the global PMU.
929 * 9) Restore the status of the rest of the counters.
931 * We choose an event which for CCI-5xx is guaranteed not to count.
932 * We use the highest possible event code (0x1f) for the master interface 0.
934 #define CCI5xx_INVALID_EVENT ((CCI5xx_PORT_M0 << CCI5xx_PMU_EVENT_SOURCE_SHIFT) | \
935 (CCI5xx_PMU_EVENT_CODE_MASK << CCI5xx_PMU_EVENT_CODE_SHIFT))
936 static void cci5xx_pmu_write_counters(struct cci_pmu
*cci_pmu
, unsigned long *mask
)
939 DECLARE_BITMAP(saved_mask
, HW_CNTRS_MAX
);
941 bitmap_zero(saved_mask
, cci_pmu
->num_cntrs
);
942 pmu_save_counters(cci_pmu
, saved_mask
);
945 * Now that all the counters are disabled, we can safely turn the PMU on,
946 * without syncing the status of the counters
948 __cci_pmu_enable_nosync(cci_pmu
);
950 for_each_set_bit(i
, mask
, cci_pmu
->num_cntrs
) {
951 struct perf_event
*event
= cci_pmu
->hw_events
.events
[i
];
956 pmu_set_event(cci_pmu
, i
, CCI5xx_INVALID_EVENT
);
957 pmu_enable_counter(cci_pmu
, i
);
958 pmu_write_counter(cci_pmu
, local64_read(&event
->hw
.prev_count
), i
);
959 pmu_disable_counter(cci_pmu
, i
);
960 pmu_set_event(cci_pmu
, i
, event
->hw
.config_base
);
963 __cci_pmu_disable(cci_pmu
);
965 pmu_restore_counters(cci_pmu
, saved_mask
);
968 #endif /* CONFIG_ARM_CCI5xx_PMU */
970 static u64
pmu_event_update(struct perf_event
*event
)
972 struct hw_perf_event
*hwc
= &event
->hw
;
973 u64 delta
, prev_raw_count
, new_raw_count
;
976 prev_raw_count
= local64_read(&hwc
->prev_count
);
977 new_raw_count
= pmu_read_counter(event
);
978 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
979 new_raw_count
) != prev_raw_count
);
981 delta
= (new_raw_count
- prev_raw_count
) & CCI_PMU_CNTR_MASK
;
983 local64_add(delta
, &event
->count
);
985 return new_raw_count
;
988 static void pmu_read(struct perf_event
*event
)
990 pmu_event_update(event
);
993 static void pmu_event_set_period(struct perf_event
*event
)
995 struct hw_perf_event
*hwc
= &event
->hw
;
997 * The CCI PMU counters have a period of 2^32. To account for the
998 * possiblity of extreme interrupt latency we program for a period of
999 * half that. Hopefully we can handle the interrupt before another 2^31
1000 * events occur and the counter overtakes its previous value.
1002 u64 val
= 1ULL << 31;
1003 local64_set(&hwc
->prev_count
, val
);
1006 * CCI PMU uses PERF_HES_ARCH to keep track of the counters, whose
1007 * values needs to be sync-ed with the s/w state before the PMU is
1009 * Mark this counter for sync.
1011 hwc
->state
|= PERF_HES_ARCH
;
1014 static irqreturn_t
pmu_handle_irq(int irq_num
, void *dev
)
1016 struct cci_pmu
*cci_pmu
= dev
;
1017 struct cci_pmu_hw_events
*events
= &cci_pmu
->hw_events
;
1018 int idx
, handled
= IRQ_NONE
;
1020 raw_spin_lock(&events
->pmu_lock
);
1022 /* Disable the PMU while we walk through the counters */
1023 __cci_pmu_disable(cci_pmu
);
1025 * Iterate over counters and update the corresponding perf events.
1026 * This should work regardless of whether we have per-counter overflow
1027 * interrupt or a combined overflow interrupt.
1029 for (idx
= 0; idx
<= CCI_PMU_CNTR_LAST(cci_pmu
); idx
++) {
1030 struct perf_event
*event
= events
->events
[idx
];
1035 /* Did this counter overflow? */
1036 if (!(pmu_read_register(cci_pmu
, idx
, CCI_PMU_OVRFLW
) &
1037 CCI_PMU_OVRFLW_FLAG
))
1040 pmu_write_register(cci_pmu
, CCI_PMU_OVRFLW_FLAG
, idx
,
1043 pmu_event_update(event
);
1044 pmu_event_set_period(event
);
1045 handled
= IRQ_HANDLED
;
1048 /* Enable the PMU and sync possibly overflowed counters */
1049 __cci_pmu_enable_sync(cci_pmu
);
1050 raw_spin_unlock(&events
->pmu_lock
);
1052 return IRQ_RETVAL(handled
);
1055 static int cci_pmu_get_hw(struct cci_pmu
*cci_pmu
)
1057 int ret
= pmu_request_irq(cci_pmu
, pmu_handle_irq
);
1059 pmu_free_irq(cci_pmu
);
1065 static void cci_pmu_put_hw(struct cci_pmu
*cci_pmu
)
1067 pmu_free_irq(cci_pmu
);
1070 static void hw_perf_event_destroy(struct perf_event
*event
)
1072 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
1073 atomic_t
*active_events
= &cci_pmu
->active_events
;
1074 struct mutex
*reserve_mutex
= &cci_pmu
->reserve_mutex
;
1076 if (atomic_dec_and_mutex_lock(active_events
, reserve_mutex
)) {
1077 cci_pmu_put_hw(cci_pmu
);
1078 mutex_unlock(reserve_mutex
);
1082 static void cci_pmu_enable(struct pmu
*pmu
)
1084 struct cci_pmu
*cci_pmu
= to_cci_pmu(pmu
);
1085 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
1086 bool enabled
= !bitmap_empty(hw_events
->used_mask
, cci_pmu
->num_cntrs
);
1087 unsigned long flags
;
1092 raw_spin_lock_irqsave(&hw_events
->pmu_lock
, flags
);
1093 __cci_pmu_enable_sync(cci_pmu
);
1094 raw_spin_unlock_irqrestore(&hw_events
->pmu_lock
, flags
);
1098 static void cci_pmu_disable(struct pmu
*pmu
)
1100 struct cci_pmu
*cci_pmu
= to_cci_pmu(pmu
);
1101 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
1102 unsigned long flags
;
1104 raw_spin_lock_irqsave(&hw_events
->pmu_lock
, flags
);
1105 __cci_pmu_disable(cci_pmu
);
1106 raw_spin_unlock_irqrestore(&hw_events
->pmu_lock
, flags
);
1110 * Check if the idx represents a non-programmable counter.
1111 * All the fixed event counters are mapped before the programmable
1114 static bool pmu_fixed_hw_idx(struct cci_pmu
*cci_pmu
, int idx
)
1116 return (idx
>= 0) && (idx
< cci_pmu
->model
->fixed_hw_cntrs
);
1119 static void cci_pmu_start(struct perf_event
*event
, int pmu_flags
)
1121 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
1122 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
1123 struct hw_perf_event
*hwc
= &event
->hw
;
1125 unsigned long flags
;
1128 * To handle interrupt latency, we always reprogram the period
1129 * regardless of PERF_EF_RELOAD.
1131 if (pmu_flags
& PERF_EF_RELOAD
)
1132 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
1136 if (unlikely(!pmu_is_valid_counter(cci_pmu
, idx
))) {
1137 dev_err(&cci_pmu
->plat_device
->dev
, "Invalid CCI PMU counter %d\n", idx
);
1141 raw_spin_lock_irqsave(&hw_events
->pmu_lock
, flags
);
1143 /* Configure the counter unless you are counting a fixed event */
1144 if (!pmu_fixed_hw_idx(cci_pmu
, idx
))
1145 pmu_set_event(cci_pmu
, idx
, hwc
->config_base
);
1147 pmu_event_set_period(event
);
1148 pmu_enable_counter(cci_pmu
, idx
);
1150 raw_spin_unlock_irqrestore(&hw_events
->pmu_lock
, flags
);
1153 static void cci_pmu_stop(struct perf_event
*event
, int pmu_flags
)
1155 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
1156 struct hw_perf_event
*hwc
= &event
->hw
;
1159 if (hwc
->state
& PERF_HES_STOPPED
)
1162 if (unlikely(!pmu_is_valid_counter(cci_pmu
, idx
))) {
1163 dev_err(&cci_pmu
->plat_device
->dev
, "Invalid CCI PMU counter %d\n", idx
);
1168 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
1171 pmu_disable_counter(cci_pmu
, idx
);
1172 pmu_event_update(event
);
1173 hwc
->state
|= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
1176 static int cci_pmu_add(struct perf_event
*event
, int flags
)
1178 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
1179 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
1180 struct hw_perf_event
*hwc
= &event
->hw
;
1183 /* If we don't have a space for the counter then finish early. */
1184 idx
= pmu_get_event_idx(hw_events
, event
);
1188 event
->hw
.idx
= idx
;
1189 hw_events
->events
[idx
] = event
;
1191 hwc
->state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
1192 if (flags
& PERF_EF_START
)
1193 cci_pmu_start(event
, PERF_EF_RELOAD
);
1195 /* Propagate our changes to the userspace mapping. */
1196 perf_event_update_userpage(event
);
1201 static void cci_pmu_del(struct perf_event
*event
, int flags
)
1203 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
1204 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
1205 struct hw_perf_event
*hwc
= &event
->hw
;
1208 cci_pmu_stop(event
, PERF_EF_UPDATE
);
1209 hw_events
->events
[idx
] = NULL
;
1210 clear_bit(idx
, hw_events
->used_mask
);
1212 perf_event_update_userpage(event
);
1215 static int validate_event(struct pmu
*cci_pmu
,
1216 struct cci_pmu_hw_events
*hw_events
,
1217 struct perf_event
*event
)
1219 if (is_software_event(event
))
1223 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
1224 * core perf code won't check that the pmu->ctx == leader->ctx
1225 * until after pmu->event_init(event).
1227 if (event
->pmu
!= cci_pmu
)
1230 if (event
->state
< PERF_EVENT_STATE_OFF
)
1233 if (event
->state
== PERF_EVENT_STATE_OFF
&& !event
->attr
.enable_on_exec
)
1236 return pmu_get_event_idx(hw_events
, event
) >= 0;
1239 static int validate_group(struct perf_event
*event
)
1241 struct perf_event
*sibling
, *leader
= event
->group_leader
;
1242 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
1243 unsigned long mask
[BITS_TO_LONGS(HW_CNTRS_MAX
)];
1244 struct cci_pmu_hw_events fake_pmu
= {
1246 * Initialise the fake PMU. We only need to populate the
1247 * used_mask for the purposes of validation.
1251 bitmap_zero(mask
, cci_pmu
->num_cntrs
);
1253 if (!validate_event(event
->pmu
, &fake_pmu
, leader
))
1256 for_each_sibling_event(sibling
, leader
) {
1257 if (!validate_event(event
->pmu
, &fake_pmu
, sibling
))
1261 if (!validate_event(event
->pmu
, &fake_pmu
, event
))
1267 static int __hw_perf_event_init(struct perf_event
*event
)
1269 struct hw_perf_event
*hwc
= &event
->hw
;
1272 mapping
= pmu_map_event(event
);
1275 pr_debug("event %x:%llx not supported\n", event
->attr
.type
,
1276 event
->attr
.config
);
1281 * We don't assign an index until we actually place the event onto
1282 * hardware. Use -1 to signify that we haven't decided where to put it
1286 hwc
->config_base
= 0;
1288 hwc
->event_base
= 0;
1291 * Store the event encoding into the config_base field.
1293 hwc
->config_base
|= (unsigned long)mapping
;
1295 if (event
->group_leader
!= event
) {
1296 if (validate_group(event
) != 0)
1303 static int cci_pmu_event_init(struct perf_event
*event
)
1305 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
1306 atomic_t
*active_events
= &cci_pmu
->active_events
;
1309 if (event
->attr
.type
!= event
->pmu
->type
)
1312 /* Shared by all CPUs, no meaningful state to sample */
1313 if (is_sampling_event(event
) || event
->attach_state
& PERF_ATTACH_TASK
)
1317 * Following the example set by other "uncore" PMUs, we accept any CPU
1318 * and rewrite its affinity dynamically rather than having perf core
1319 * handle cpu == -1 and pid == -1 for this case.
1321 * The perf core will pin online CPUs for the duration of this call and
1322 * the event being installed into its context, so the PMU's CPU can't
1323 * change under our feet.
1327 event
->cpu
= cci_pmu
->cpu
;
1329 event
->destroy
= hw_perf_event_destroy
;
1330 if (!atomic_inc_not_zero(active_events
)) {
1331 mutex_lock(&cci_pmu
->reserve_mutex
);
1332 if (atomic_read(active_events
) == 0)
1333 err
= cci_pmu_get_hw(cci_pmu
);
1335 atomic_inc(active_events
);
1336 mutex_unlock(&cci_pmu
->reserve_mutex
);
1341 err
= __hw_perf_event_init(event
);
1343 hw_perf_event_destroy(event
);
1348 static ssize_t
pmu_cpumask_attr_show(struct device
*dev
,
1349 struct device_attribute
*attr
, char *buf
)
1351 struct pmu
*pmu
= dev_get_drvdata(dev
);
1352 struct cci_pmu
*cci_pmu
= to_cci_pmu(pmu
);
1354 return cpumap_print_to_pagebuf(true, buf
, cpumask_of(cci_pmu
->cpu
));
1357 static struct device_attribute pmu_cpumask_attr
=
1358 __ATTR(cpumask
, S_IRUGO
, pmu_cpumask_attr_show
, NULL
);
1360 static struct attribute
*pmu_attrs
[] = {
1361 &pmu_cpumask_attr
.attr
,
1365 static const struct attribute_group pmu_attr_group
= {
1369 static struct attribute_group pmu_format_attr_group
= {
1371 .attrs
= NULL
, /* Filled in cci_pmu_init_attrs */
1374 static struct attribute_group pmu_event_attr_group
= {
1376 .attrs
= NULL
, /* Filled in cci_pmu_init_attrs */
1379 static const struct attribute_group
*pmu_attr_groups
[] = {
1381 &pmu_format_attr_group
,
1382 &pmu_event_attr_group
,
1386 static int cci_pmu_init(struct cci_pmu
*cci_pmu
, struct platform_device
*pdev
)
1388 const struct cci_pmu_model
*model
= cci_pmu
->model
;
1389 char *name
= model
->name
;
1392 if (WARN_ON(model
->num_hw_cntrs
> NUM_HW_CNTRS_MAX
))
1394 if (WARN_ON(model
->fixed_hw_cntrs
> FIXED_HW_CNTRS_MAX
))
1397 pmu_event_attr_group
.attrs
= model
->event_attrs
;
1398 pmu_format_attr_group
.attrs
= model
->format_attrs
;
1400 cci_pmu
->pmu
= (struct pmu
) {
1401 .module
= THIS_MODULE
,
1402 .parent
= &pdev
->dev
,
1403 .name
= cci_pmu
->model
->name
,
1404 .task_ctx_nr
= perf_invalid_context
,
1405 .pmu_enable
= cci_pmu_enable
,
1406 .pmu_disable
= cci_pmu_disable
,
1407 .event_init
= cci_pmu_event_init
,
1410 .start
= cci_pmu_start
,
1411 .stop
= cci_pmu_stop
,
1413 .attr_groups
= pmu_attr_groups
,
1414 .capabilities
= PERF_PMU_CAP_NO_EXCLUDE
,
1417 cci_pmu
->plat_device
= pdev
;
1418 num_cntrs
= pmu_get_max_counters(cci_pmu
);
1419 if (num_cntrs
> cci_pmu
->model
->num_hw_cntrs
) {
1420 dev_warn(&pdev
->dev
,
1421 "PMU implements more counters(%d) than supported by"
1422 " the model(%d), truncated.",
1423 num_cntrs
, cci_pmu
->model
->num_hw_cntrs
);
1424 num_cntrs
= cci_pmu
->model
->num_hw_cntrs
;
1426 cci_pmu
->num_cntrs
= num_cntrs
+ cci_pmu
->model
->fixed_hw_cntrs
;
1428 return perf_pmu_register(&cci_pmu
->pmu
, name
, -1);
1431 static int cci_pmu_offline_cpu(unsigned int cpu
)
1435 if (!g_cci_pmu
|| cpu
!= g_cci_pmu
->cpu
)
1438 target
= cpumask_any_but(cpu_online_mask
, cpu
);
1439 if (target
>= nr_cpu_ids
)
1442 perf_pmu_migrate_context(&g_cci_pmu
->pmu
, cpu
, target
);
1443 g_cci_pmu
->cpu
= target
;
1447 static __maybe_unused
struct cci_pmu_model cci_pmu_models
[] = {
1448 #ifdef CONFIG_ARM_CCI400_PMU
1451 .fixed_hw_cntrs
= FIXED_HW_CNTRS_CII_4XX
, /* Cycle counter */
1452 .num_hw_cntrs
= NUM_HW_CNTRS_CII_4XX
,
1454 .format_attrs
= cci400_pmu_format_attrs
,
1455 .event_attrs
= cci400_r0_pmu_event_attrs
,
1458 CCI400_R0_SLAVE_PORT_MIN_EV
,
1459 CCI400_R0_SLAVE_PORT_MAX_EV
,
1462 CCI400_R0_MASTER_PORT_MIN_EV
,
1463 CCI400_R0_MASTER_PORT_MAX_EV
,
1466 .validate_hw_event
= cci400_validate_hw_event
,
1467 .get_event_idx
= cci400_get_event_idx
,
1470 .name
= "CCI_400_r1",
1471 .fixed_hw_cntrs
= FIXED_HW_CNTRS_CII_4XX
, /* Cycle counter */
1472 .num_hw_cntrs
= NUM_HW_CNTRS_CII_4XX
,
1474 .format_attrs
= cci400_pmu_format_attrs
,
1475 .event_attrs
= cci400_r1_pmu_event_attrs
,
1478 CCI400_R1_SLAVE_PORT_MIN_EV
,
1479 CCI400_R1_SLAVE_PORT_MAX_EV
,
1482 CCI400_R1_MASTER_PORT_MIN_EV
,
1483 CCI400_R1_MASTER_PORT_MAX_EV
,
1486 .validate_hw_event
= cci400_validate_hw_event
,
1487 .get_event_idx
= cci400_get_event_idx
,
1490 #ifdef CONFIG_ARM_CCI5xx_PMU
1493 .fixed_hw_cntrs
= FIXED_HW_CNTRS_CII_5XX
,
1494 .num_hw_cntrs
= NUM_HW_CNTRS_CII_5XX
,
1495 .cntr_size
= SZ_64K
,
1496 .format_attrs
= cci5xx_pmu_format_attrs
,
1497 .event_attrs
= cci5xx_pmu_event_attrs
,
1500 CCI5xx_SLAVE_PORT_MIN_EV
,
1501 CCI5xx_SLAVE_PORT_MAX_EV
,
1504 CCI5xx_MASTER_PORT_MIN_EV
,
1505 CCI5xx_MASTER_PORT_MAX_EV
,
1508 CCI5xx_GLOBAL_PORT_MIN_EV
,
1509 CCI5xx_GLOBAL_PORT_MAX_EV
,
1512 .validate_hw_event
= cci500_validate_hw_event
,
1513 .write_counters
= cci5xx_pmu_write_counters
,
1517 .fixed_hw_cntrs
= FIXED_HW_CNTRS_CII_5XX
,
1518 .num_hw_cntrs
= NUM_HW_CNTRS_CII_5XX
,
1519 .cntr_size
= SZ_64K
,
1520 .format_attrs
= cci5xx_pmu_format_attrs
,
1521 .event_attrs
= cci5xx_pmu_event_attrs
,
1524 CCI5xx_SLAVE_PORT_MIN_EV
,
1525 CCI5xx_SLAVE_PORT_MAX_EV
,
1528 CCI5xx_MASTER_PORT_MIN_EV
,
1529 CCI5xx_MASTER_PORT_MAX_EV
,
1532 CCI5xx_GLOBAL_PORT_MIN_EV
,
1533 CCI5xx_GLOBAL_PORT_MAX_EV
,
1536 .validate_hw_event
= cci550_validate_hw_event
,
1537 .write_counters
= cci5xx_pmu_write_counters
,
1542 static const struct of_device_id arm_cci_pmu_matches
[] = {
1543 #ifdef CONFIG_ARM_CCI400_PMU
1545 .compatible
= "arm,cci-400-pmu",
1549 .compatible
= "arm,cci-400-pmu,r0",
1550 .data
= &cci_pmu_models
[CCI400_R0
],
1553 .compatible
= "arm,cci-400-pmu,r1",
1554 .data
= &cci_pmu_models
[CCI400_R1
],
1557 #ifdef CONFIG_ARM_CCI5xx_PMU
1559 .compatible
= "arm,cci-500-pmu,r0",
1560 .data
= &cci_pmu_models
[CCI500_R0
],
1563 .compatible
= "arm,cci-550-pmu,r0",
1564 .data
= &cci_pmu_models
[CCI550_R0
],
1569 MODULE_DEVICE_TABLE(of
, arm_cci_pmu_matches
);
1571 static bool is_duplicate_irq(int irq
, int *irqs
, int nr_irqs
)
1575 for (i
= 0; i
< nr_irqs
; i
++)
1582 static struct cci_pmu
*cci_pmu_alloc(struct device
*dev
)
1584 struct cci_pmu
*cci_pmu
;
1585 const struct cci_pmu_model
*model
;
1588 * All allocations are devm_* hence we don't have to free
1589 * them explicitly on an error, as it would end up in driver
1592 cci_pmu
= devm_kzalloc(dev
, sizeof(*cci_pmu
), GFP_KERNEL
);
1594 return ERR_PTR(-ENOMEM
);
1596 cci_pmu
->ctrl_base
= *(void __iomem
**)dev
->platform_data
;
1598 model
= of_device_get_match_data(dev
);
1601 "DEPRECATED compatible property, requires secure access to CCI registers");
1602 model
= probe_cci_model(cci_pmu
);
1605 dev_warn(dev
, "CCI PMU version not supported\n");
1606 return ERR_PTR(-ENODEV
);
1609 cci_pmu
->model
= model
;
1610 cci_pmu
->irqs
= devm_kcalloc(dev
, CCI_PMU_MAX_HW_CNTRS(model
),
1611 sizeof(*cci_pmu
->irqs
), GFP_KERNEL
);
1613 return ERR_PTR(-ENOMEM
);
1614 cci_pmu
->hw_events
.events
= devm_kcalloc(dev
,
1615 CCI_PMU_MAX_HW_CNTRS(model
),
1616 sizeof(*cci_pmu
->hw_events
.events
),
1618 if (!cci_pmu
->hw_events
.events
)
1619 return ERR_PTR(-ENOMEM
);
1620 cci_pmu
->hw_events
.used_mask
= devm_bitmap_zalloc(dev
,
1621 CCI_PMU_MAX_HW_CNTRS(model
),
1623 if (!cci_pmu
->hw_events
.used_mask
)
1624 return ERR_PTR(-ENOMEM
);
1629 static int cci_pmu_probe(struct platform_device
*pdev
)
1631 struct cci_pmu
*cci_pmu
;
1634 cci_pmu
= cci_pmu_alloc(&pdev
->dev
);
1635 if (IS_ERR(cci_pmu
))
1636 return PTR_ERR(cci_pmu
);
1638 cci_pmu
->base
= devm_platform_ioremap_resource(pdev
, 0);
1639 if (IS_ERR(cci_pmu
->base
))
1643 * CCI PMU has one overflow interrupt per counter; but some may be tied
1644 * together to a common interrupt.
1646 cci_pmu
->nr_irqs
= 0;
1647 for (i
= 0; i
< CCI_PMU_MAX_HW_CNTRS(cci_pmu
->model
); i
++) {
1648 irq
= platform_get_irq(pdev
, i
);
1652 if (is_duplicate_irq(irq
, cci_pmu
->irqs
, cci_pmu
->nr_irqs
))
1655 cci_pmu
->irqs
[cci_pmu
->nr_irqs
++] = irq
;
1659 * Ensure that the device tree has as many interrupts as the number
1662 if (i
< CCI_PMU_MAX_HW_CNTRS(cci_pmu
->model
)) {
1663 dev_warn(&pdev
->dev
, "In-correct number of interrupts: %d, should be %d\n",
1664 i
, CCI_PMU_MAX_HW_CNTRS(cci_pmu
->model
));
1668 raw_spin_lock_init(&cci_pmu
->hw_events
.pmu_lock
);
1669 mutex_init(&cci_pmu
->reserve_mutex
);
1670 atomic_set(&cci_pmu
->active_events
, 0);
1672 cci_pmu
->cpu
= raw_smp_processor_id();
1673 g_cci_pmu
= cci_pmu
;
1674 cpuhp_setup_state_nocalls(CPUHP_AP_PERF_ARM_CCI_ONLINE
,
1675 "perf/arm/cci:online", NULL
,
1676 cci_pmu_offline_cpu
);
1678 ret
= cci_pmu_init(cci_pmu
, pdev
);
1680 goto error_pmu_init
;
1682 pr_info("ARM %s PMU driver probed", cci_pmu
->model
->name
);
1686 cpuhp_remove_state(CPUHP_AP_PERF_ARM_CCI_ONLINE
);
1691 static void cci_pmu_remove(struct platform_device
*pdev
)
1696 cpuhp_remove_state(CPUHP_AP_PERF_ARM_CCI_ONLINE
);
1697 perf_pmu_unregister(&g_cci_pmu
->pmu
);
1701 static struct platform_driver cci_pmu_driver
= {
1703 .name
= DRIVER_NAME
,
1704 .of_match_table
= arm_cci_pmu_matches
,
1705 .suppress_bind_attrs
= true,
1707 .probe
= cci_pmu_probe
,
1708 .remove
= cci_pmu_remove
,
1711 module_platform_driver(cci_pmu_driver
);
1712 MODULE_LICENSE("GPL v2");
1713 MODULE_DESCRIPTION("ARM CCI PMU support");