2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * Copyright (C) 2014 ARM Limited
14 #include <linux/ctype.h>
15 #include <linux/hrtimer.h>
16 #include <linux/idr.h>
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/perf_event.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
25 #define CCN_NUM_XP_PORTS 2
27 #define CCN_NUM_REGIONS 256
28 #define CCN_REGION_SIZE 0x10000
30 #define CCN_ALL_OLY_ID 0xff00
31 #define CCN_ALL_OLY_ID__OLY_ID__SHIFT 0
32 #define CCN_ALL_OLY_ID__OLY_ID__MASK 0x1f
33 #define CCN_ALL_OLY_ID__NODE_ID__SHIFT 8
34 #define CCN_ALL_OLY_ID__NODE_ID__MASK 0x3f
36 #define CCN_MN_ERRINT_STATUS 0x0008
37 #define CCN_MN_ERRINT_STATUS__INTREQ__DESSERT 0x11
38 #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__ENABLE 0x02
39 #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLED 0x20
40 #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE 0x22
41 #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_ENABLE 0x04
42 #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLED 0x40
43 #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLE 0x44
44 #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE 0x08
45 #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED 0x80
46 #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE 0x88
47 #define CCN_MN_OLY_COMP_LIST_63_0 0x01e0
48 #define CCN_MN_ERR_SIG_VAL_63_0 0x0300
49 #define CCN_MN_ERR_SIG_VAL_63_0__DT (1 << 1)
51 #define CCN_DT_ACTIVE_DSM 0x0000
52 #define CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(n) ((n) * 8)
53 #define CCN_DT_ACTIVE_DSM__DSM_ID__MASK 0xff
54 #define CCN_DT_CTL 0x0028
55 #define CCN_DT_CTL__DT_EN (1 << 0)
56 #define CCN_DT_PMEVCNT(n) (0x0100 + (n) * 0x8)
57 #define CCN_DT_PMCCNTR 0x0140
58 #define CCN_DT_PMCCNTRSR 0x0190
59 #define CCN_DT_PMOVSR 0x0198
60 #define CCN_DT_PMOVSR_CLR 0x01a0
61 #define CCN_DT_PMOVSR_CLR__MASK 0x1f
62 #define CCN_DT_PMCR 0x01a8
63 #define CCN_DT_PMCR__OVFL_INTR_EN (1 << 6)
64 #define CCN_DT_PMCR__PMU_EN (1 << 0)
65 #define CCN_DT_PMSR 0x01b0
66 #define CCN_DT_PMSR_REQ 0x01b8
67 #define CCN_DT_PMSR_CLR 0x01c0
69 #define CCN_HNF_PMU_EVENT_SEL 0x0600
70 #define CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
71 #define CCN_HNF_PMU_EVENT_SEL__ID__MASK 0xf
73 #define CCN_XP_DT_CONFIG 0x0300
74 #define CCN_XP_DT_CONFIG__DT_CFG__SHIFT(n) ((n) * 4)
75 #define CCN_XP_DT_CONFIG__DT_CFG__MASK 0xf
76 #define CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH 0x0
77 #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT_0_OR_1 0x1
78 #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(n) (0x2 + (n))
79 #define CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(n) (0x4 + (n))
80 #define CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(d, n) (0x8 + (d) * 4 + (n))
81 #define CCN_XP_DT_INTERFACE_SEL 0x0308
82 #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(n) (0 + (n) * 8)
83 #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK 0x1
84 #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(n) (1 + (n) * 8)
85 #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK 0x1
86 #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(n) (2 + (n) * 8)
87 #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK 0x3
88 #define CCN_XP_DT_CMP_VAL_L(n) (0x0310 + (n) * 0x40)
89 #define CCN_XP_DT_CMP_VAL_H(n) (0x0318 + (n) * 0x40)
90 #define CCN_XP_DT_CMP_MASK_L(n) (0x0320 + (n) * 0x40)
91 #define CCN_XP_DT_CMP_MASK_H(n) (0x0328 + (n) * 0x40)
92 #define CCN_XP_DT_CONTROL 0x0370
93 #define CCN_XP_DT_CONTROL__DT_ENABLE (1 << 0)
94 #define CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(n) (12 + (n) * 4)
95 #define CCN_XP_DT_CONTROL__WP_ARM_SEL__MASK 0xf
96 #define CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS 0xf
97 #define CCN_XP_PMU_EVENT_SEL 0x0600
98 #define CCN_XP_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 7)
99 #define CCN_XP_PMU_EVENT_SEL__ID__MASK 0x3f
101 #define CCN_SBAS_PMU_EVENT_SEL 0x0600
102 #define CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
103 #define CCN_SBAS_PMU_EVENT_SEL__ID__MASK 0xf
105 #define CCN_RNI_PMU_EVENT_SEL 0x0600
106 #define CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
107 #define CCN_RNI_PMU_EVENT_SEL__ID__MASK 0xf
109 #define CCN_TYPE_MN 0x01
110 #define CCN_TYPE_DT 0x02
111 #define CCN_TYPE_HNF 0x04
112 #define CCN_TYPE_HNI 0x05
113 #define CCN_TYPE_XP 0x08
114 #define CCN_TYPE_SBSX 0x0c
115 #define CCN_TYPE_SBAS 0x10
116 #define CCN_TYPE_RNI_1P 0x14
117 #define CCN_TYPE_RNI_2P 0x15
118 #define CCN_TYPE_RNI_3P 0x16
119 #define CCN_TYPE_RND_1P 0x18 /* RN-D = RN-I + DVM */
120 #define CCN_TYPE_RND_2P 0x19
121 #define CCN_TYPE_RND_3P 0x1a
122 #define CCN_TYPE_CYCLES 0xff /* Pseudotype */
124 #define CCN_EVENT_WATCHPOINT 0xfe /* Pseudoevent */
126 #define CCN_NUM_PMU_EVENTS 4
127 #define CCN_NUM_XP_WATCHPOINTS 2 /* See DT.dbg_id.num_watchpoints */
128 #define CCN_NUM_PMU_EVENT_COUNTERS 8 /* See DT.dbg_id.num_pmucntr */
129 #define CCN_IDX_PMU_CYCLE_COUNTER CCN_NUM_PMU_EVENT_COUNTERS
131 #define CCN_NUM_PREDEFINED_MASKS 4
132 #define CCN_IDX_MASK_ANY (CCN_NUM_PMU_EVENT_COUNTERS + 0)
133 #define CCN_IDX_MASK_EXACT (CCN_NUM_PMU_EVENT_COUNTERS + 1)
134 #define CCN_IDX_MASK_ORDER (CCN_NUM_PMU_EVENT_COUNTERS + 2)
135 #define CCN_IDX_MASK_OPCODE (CCN_NUM_PMU_EVENT_COUNTERS + 3)
137 struct arm_ccn_component
{
141 DECLARE_BITMAP(pmu_events_mask
, CCN_NUM_PMU_EVENTS
);
144 DECLARE_BITMAP(dt_cmp_mask
, CCN_NUM_XP_WATCHPOINTS
);
149 #define pmu_to_arm_ccn(_pmu) container_of(container_of(_pmu, \
150 struct arm_ccn_dt, pmu), struct arm_ccn, dt)
156 spinlock_t config_lock
;
158 DECLARE_BITMAP(pmu_counters_mask
, CCN_NUM_PMU_EVENT_COUNTERS
+ 1);
160 struct arm_ccn_component
*source
;
161 struct perf_event
*event
;
162 } pmu_counters
[CCN_NUM_PMU_EVENT_COUNTERS
+ 1];
166 } cmp_mask
[CCN_NUM_PMU_EVENT_COUNTERS
+ CCN_NUM_PREDEFINED_MASKS
];
168 struct hrtimer hrtimer
;
171 struct hlist_node node
;
181 unsigned sbas_present
:1;
182 unsigned sbsx_present
:1;
185 struct arm_ccn_component
*node
;
188 struct arm_ccn_component
*xp
;
190 struct arm_ccn_dt dt
;
194 static int arm_ccn_node_to_xp(int node
)
196 return node
/ CCN_NUM_XP_PORTS
;
199 static int arm_ccn_node_to_xp_port(int node
)
201 return node
% CCN_NUM_XP_PORTS
;
206 * Bit shifts and masks in these defines must be kept in sync with
207 * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
209 #define CCN_CONFIG_NODE(_config) (((_config) >> 0) & 0xff)
210 #define CCN_CONFIG_XP(_config) (((_config) >> 0) & 0xff)
211 #define CCN_CONFIG_TYPE(_config) (((_config) >> 8) & 0xff)
212 #define CCN_CONFIG_EVENT(_config) (((_config) >> 16) & 0xff)
213 #define CCN_CONFIG_PORT(_config) (((_config) >> 24) & 0x3)
214 #define CCN_CONFIG_BUS(_config) (((_config) >> 24) & 0x3)
215 #define CCN_CONFIG_VC(_config) (((_config) >> 26) & 0x7)
216 #define CCN_CONFIG_DIR(_config) (((_config) >> 29) & 0x1)
217 #define CCN_CONFIG_MASK(_config) (((_config) >> 30) & 0xf)
219 static void arm_ccn_pmu_config_set(u64
*config
, u32 node_xp
, u32 type
, u32 port
)
221 *config
&= ~((0xff << 0) | (0xff << 8) | (0x3 << 24));
222 *config
|= (node_xp
<< 0) | (type
<< 8) | (port
<< 24);
225 static ssize_t
arm_ccn_pmu_format_show(struct device
*dev
,
226 struct device_attribute
*attr
, char *buf
)
228 struct dev_ext_attribute
*ea
= container_of(attr
,
229 struct dev_ext_attribute
, attr
);
231 return snprintf(buf
, PAGE_SIZE
, "%s\n", (char *)ea
->var
);
234 #define CCN_FORMAT_ATTR(_name, _config) \
235 struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
236 { __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
239 static CCN_FORMAT_ATTR(node
, "config:0-7");
240 static CCN_FORMAT_ATTR(xp
, "config:0-7");
241 static CCN_FORMAT_ATTR(type
, "config:8-15");
242 static CCN_FORMAT_ATTR(event
, "config:16-23");
243 static CCN_FORMAT_ATTR(port
, "config:24-25");
244 static CCN_FORMAT_ATTR(bus
, "config:24-25");
245 static CCN_FORMAT_ATTR(vc
, "config:26-28");
246 static CCN_FORMAT_ATTR(dir
, "config:29-29");
247 static CCN_FORMAT_ATTR(mask
, "config:30-33");
248 static CCN_FORMAT_ATTR(cmp_l
, "config1:0-62");
249 static CCN_FORMAT_ATTR(cmp_h
, "config2:0-59");
251 static struct attribute
*arm_ccn_pmu_format_attrs
[] = {
252 &arm_ccn_pmu_format_attr_node
.attr
.attr
,
253 &arm_ccn_pmu_format_attr_xp
.attr
.attr
,
254 &arm_ccn_pmu_format_attr_type
.attr
.attr
,
255 &arm_ccn_pmu_format_attr_event
.attr
.attr
,
256 &arm_ccn_pmu_format_attr_port
.attr
.attr
,
257 &arm_ccn_pmu_format_attr_bus
.attr
.attr
,
258 &arm_ccn_pmu_format_attr_vc
.attr
.attr
,
259 &arm_ccn_pmu_format_attr_dir
.attr
.attr
,
260 &arm_ccn_pmu_format_attr_mask
.attr
.attr
,
261 &arm_ccn_pmu_format_attr_cmp_l
.attr
.attr
,
262 &arm_ccn_pmu_format_attr_cmp_h
.attr
.attr
,
266 static const struct attribute_group arm_ccn_pmu_format_attr_group
= {
268 .attrs
= arm_ccn_pmu_format_attrs
,
272 struct arm_ccn_pmu_event
{
273 struct device_attribute attr
;
282 #define CCN_EVENT_ATTR(_name) \
283 __ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
286 * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
287 * their ports in XP they are connected to. For the sake of usability they are
288 * explicitly defined here (and translated into a relevant watchpoint in
289 * arm_ccn_pmu_event_init()) so the user can easily request them without deep
290 * knowledge of the flit format.
293 #define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
294 .type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
295 .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
296 .def = _def, .mask = _mask, }
298 #define CCN_EVENT_HNI(_name, _def, _mask) { \
299 .attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
300 .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
301 .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
303 #define CCN_EVENT_SBSX(_name, _def, _mask) { \
304 .attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
305 .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
306 .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
308 #define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
309 .type = CCN_TYPE_HNF, .event = _event, }
311 #define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
312 .type = CCN_TYPE_XP, .event = _event, \
313 .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
316 * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
317 * on configuration. One of them is picked to represent the whole group,
318 * as they all share the same event types.
320 #define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
321 .type = CCN_TYPE_RNI_3P, .event = _event, }
323 #define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
324 .type = CCN_TYPE_SBAS, .event = _event, }
326 #define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
327 .type = CCN_TYPE_CYCLES }
330 static ssize_t
arm_ccn_pmu_event_show(struct device
*dev
,
331 struct device_attribute
*attr
, char *buf
)
333 struct arm_ccn
*ccn
= pmu_to_arm_ccn(dev_get_drvdata(dev
));
334 struct arm_ccn_pmu_event
*event
= container_of(attr
,
335 struct arm_ccn_pmu_event
, attr
);
338 res
= snprintf(buf
, PAGE_SIZE
, "type=0x%x", event
->type
);
340 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
, ",event=0x%x",
343 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
, ",%s",
346 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
, ",mask=0x%x",
349 /* Arguments required by an event */
350 switch (event
->type
) {
351 case CCN_TYPE_CYCLES
:
354 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
,
356 if (event
->event
== CCN_EVENT_WATCHPOINT
)
357 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
,
358 ",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
360 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
,
365 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
, ",node=%d", ccn
->mn_id
);
368 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
, ",node=?");
372 res
+= snprintf(buf
+ res
, PAGE_SIZE
- res
, "\n");
377 static umode_t
arm_ccn_pmu_events_is_visible(struct kobject
*kobj
,
378 struct attribute
*attr
, int index
)
380 struct device
*dev
= kobj_to_dev(kobj
);
381 struct arm_ccn
*ccn
= pmu_to_arm_ccn(dev_get_drvdata(dev
));
382 struct device_attribute
*dev_attr
= container_of(attr
,
383 struct device_attribute
, attr
);
384 struct arm_ccn_pmu_event
*event
= container_of(dev_attr
,
385 struct arm_ccn_pmu_event
, attr
);
387 if (event
->type
== CCN_TYPE_SBAS
&& !ccn
->sbas_present
)
389 if (event
->type
== CCN_TYPE_SBSX
&& !ccn
->sbsx_present
)
395 static struct arm_ccn_pmu_event arm_ccn_pmu_events
[] = {
396 CCN_EVENT_MN(eobarrier
, "dir=1,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE
),
397 CCN_EVENT_MN(ecbarrier
, "dir=1,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE
),
398 CCN_EVENT_MN(dvmop
, "dir=1,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE
),
399 CCN_EVENT_HNI(txdatflits
, "dir=1,vc=3", CCN_IDX_MASK_ANY
),
400 CCN_EVENT_HNI(rxdatflits
, "dir=0,vc=3", CCN_IDX_MASK_ANY
),
401 CCN_EVENT_HNI(txreqflits
, "dir=1,vc=0", CCN_IDX_MASK_ANY
),
402 CCN_EVENT_HNI(rxreqflits
, "dir=0,vc=0", CCN_IDX_MASK_ANY
),
403 CCN_EVENT_HNI(rxreqflits_order
, "dir=0,vc=0,cmp_h=0x8000",
405 CCN_EVENT_SBSX(txdatflits
, "dir=1,vc=3", CCN_IDX_MASK_ANY
),
406 CCN_EVENT_SBSX(rxdatflits
, "dir=0,vc=3", CCN_IDX_MASK_ANY
),
407 CCN_EVENT_SBSX(txreqflits
, "dir=1,vc=0", CCN_IDX_MASK_ANY
),
408 CCN_EVENT_SBSX(rxreqflits
, "dir=0,vc=0", CCN_IDX_MASK_ANY
),
409 CCN_EVENT_SBSX(rxreqflits_order
, "dir=0,vc=0,cmp_h=0x8000",
411 CCN_EVENT_HNF(cache_miss
, 0x1),
412 CCN_EVENT_HNF(l3_sf_cache_access
, 0x02),
413 CCN_EVENT_HNF(cache_fill
, 0x3),
414 CCN_EVENT_HNF(pocq_retry
, 0x4),
415 CCN_EVENT_HNF(pocq_reqs_recvd
, 0x5),
416 CCN_EVENT_HNF(sf_hit
, 0x6),
417 CCN_EVENT_HNF(sf_evictions
, 0x7),
418 CCN_EVENT_HNF(snoops_sent
, 0x8),
419 CCN_EVENT_HNF(snoops_broadcast
, 0x9),
420 CCN_EVENT_HNF(l3_eviction
, 0xa),
421 CCN_EVENT_HNF(l3_fill_invalid_way
, 0xb),
422 CCN_EVENT_HNF(mc_retries
, 0xc),
423 CCN_EVENT_HNF(mc_reqs
, 0xd),
424 CCN_EVENT_HNF(qos_hh_retry
, 0xe),
425 CCN_EVENT_RNI(rdata_beats_p0
, 0x1),
426 CCN_EVENT_RNI(rdata_beats_p1
, 0x2),
427 CCN_EVENT_RNI(rdata_beats_p2
, 0x3),
428 CCN_EVENT_RNI(rxdat_flits
, 0x4),
429 CCN_EVENT_RNI(txdat_flits
, 0x5),
430 CCN_EVENT_RNI(txreq_flits
, 0x6),
431 CCN_EVENT_RNI(txreq_flits_retried
, 0x7),
432 CCN_EVENT_RNI(rrt_full
, 0x8),
433 CCN_EVENT_RNI(wrt_full
, 0x9),
434 CCN_EVENT_RNI(txreq_flits_replayed
, 0xa),
435 CCN_EVENT_XP(upload_starvation
, 0x1),
436 CCN_EVENT_XP(download_starvation
, 0x2),
437 CCN_EVENT_XP(respin
, 0x3),
438 CCN_EVENT_XP(valid_flit
, 0x4),
439 CCN_EVENT_XP(watchpoint
, CCN_EVENT_WATCHPOINT
),
440 CCN_EVENT_SBAS(rdata_beats_p0
, 0x1),
441 CCN_EVENT_SBAS(rxdat_flits
, 0x4),
442 CCN_EVENT_SBAS(txdat_flits
, 0x5),
443 CCN_EVENT_SBAS(txreq_flits
, 0x6),
444 CCN_EVENT_SBAS(txreq_flits_retried
, 0x7),
445 CCN_EVENT_SBAS(rrt_full
, 0x8),
446 CCN_EVENT_SBAS(wrt_full
, 0x9),
447 CCN_EVENT_SBAS(txreq_flits_replayed
, 0xa),
448 CCN_EVENT_CYCLES(cycles
),
451 /* Populated in arm_ccn_init() */
452 static struct attribute
453 *arm_ccn_pmu_events_attrs
[ARRAY_SIZE(arm_ccn_pmu_events
) + 1];
455 static const struct attribute_group arm_ccn_pmu_events_attr_group
= {
457 .is_visible
= arm_ccn_pmu_events_is_visible
,
458 .attrs
= arm_ccn_pmu_events_attrs
,
462 static u64
*arm_ccn_pmu_get_cmp_mask(struct arm_ccn
*ccn
, const char *name
)
466 if (WARN_ON(!name
|| !name
[0] || !isxdigit(name
[0]) || !name
[1]))
468 i
= isdigit(name
[0]) ? name
[0] - '0' : 0xa + tolower(name
[0]) - 'a';
472 return &ccn
->dt
.cmp_mask
[i
].l
;
474 return &ccn
->dt
.cmp_mask
[i
].h
;
480 static ssize_t
arm_ccn_pmu_cmp_mask_show(struct device
*dev
,
481 struct device_attribute
*attr
, char *buf
)
483 struct arm_ccn
*ccn
= pmu_to_arm_ccn(dev_get_drvdata(dev
));
484 u64
*mask
= arm_ccn_pmu_get_cmp_mask(ccn
, attr
->attr
.name
);
486 return mask
? snprintf(buf
, PAGE_SIZE
, "0x%016llx\n", *mask
) : -EINVAL
;
489 static ssize_t
arm_ccn_pmu_cmp_mask_store(struct device
*dev
,
490 struct device_attribute
*attr
, const char *buf
, size_t count
)
492 struct arm_ccn
*ccn
= pmu_to_arm_ccn(dev_get_drvdata(dev
));
493 u64
*mask
= arm_ccn_pmu_get_cmp_mask(ccn
, attr
->attr
.name
);
497 err
= kstrtoull(buf
, 0, mask
);
499 return err
? err
: count
;
502 #define CCN_CMP_MASK_ATTR(_name) \
503 struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
504 __ATTR(_name, S_IRUGO | S_IWUSR, \
505 arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
507 #define CCN_CMP_MASK_ATTR_RO(_name) \
508 struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
509 __ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
511 static CCN_CMP_MASK_ATTR(0l);
512 static CCN_CMP_MASK_ATTR(0h
);
513 static CCN_CMP_MASK_ATTR(1l);
514 static CCN_CMP_MASK_ATTR(1h
);
515 static CCN_CMP_MASK_ATTR(2l);
516 static CCN_CMP_MASK_ATTR(2h
);
517 static CCN_CMP_MASK_ATTR(3l);
518 static CCN_CMP_MASK_ATTR(3h
);
519 static CCN_CMP_MASK_ATTR(4l);
520 static CCN_CMP_MASK_ATTR(4h
);
521 static CCN_CMP_MASK_ATTR(5l);
522 static CCN_CMP_MASK_ATTR(5h
);
523 static CCN_CMP_MASK_ATTR(6l);
524 static CCN_CMP_MASK_ATTR(6h
);
525 static CCN_CMP_MASK_ATTR(7l);
526 static CCN_CMP_MASK_ATTR(7h
);
527 static CCN_CMP_MASK_ATTR_RO(8l);
528 static CCN_CMP_MASK_ATTR_RO(8h
);
529 static CCN_CMP_MASK_ATTR_RO(9l);
530 static CCN_CMP_MASK_ATTR_RO(9h
);
531 static CCN_CMP_MASK_ATTR_RO(al
);
532 static CCN_CMP_MASK_ATTR_RO(ah
);
533 static CCN_CMP_MASK_ATTR_RO(bl
);
534 static CCN_CMP_MASK_ATTR_RO(bh
);
536 static struct attribute
*arm_ccn_pmu_cmp_mask_attrs
[] = {
537 &arm_ccn_pmu_cmp_mask_attr_0l
.attr
, &arm_ccn_pmu_cmp_mask_attr_0h
.attr
,
538 &arm_ccn_pmu_cmp_mask_attr_1l
.attr
, &arm_ccn_pmu_cmp_mask_attr_1h
.attr
,
539 &arm_ccn_pmu_cmp_mask_attr_2l
.attr
, &arm_ccn_pmu_cmp_mask_attr_2h
.attr
,
540 &arm_ccn_pmu_cmp_mask_attr_3l
.attr
, &arm_ccn_pmu_cmp_mask_attr_3h
.attr
,
541 &arm_ccn_pmu_cmp_mask_attr_4l
.attr
, &arm_ccn_pmu_cmp_mask_attr_4h
.attr
,
542 &arm_ccn_pmu_cmp_mask_attr_5l
.attr
, &arm_ccn_pmu_cmp_mask_attr_5h
.attr
,
543 &arm_ccn_pmu_cmp_mask_attr_6l
.attr
, &arm_ccn_pmu_cmp_mask_attr_6h
.attr
,
544 &arm_ccn_pmu_cmp_mask_attr_7l
.attr
, &arm_ccn_pmu_cmp_mask_attr_7h
.attr
,
545 &arm_ccn_pmu_cmp_mask_attr_8l
.attr
, &arm_ccn_pmu_cmp_mask_attr_8h
.attr
,
546 &arm_ccn_pmu_cmp_mask_attr_9l
.attr
, &arm_ccn_pmu_cmp_mask_attr_9h
.attr
,
547 &arm_ccn_pmu_cmp_mask_attr_al
.attr
, &arm_ccn_pmu_cmp_mask_attr_ah
.attr
,
548 &arm_ccn_pmu_cmp_mask_attr_bl
.attr
, &arm_ccn_pmu_cmp_mask_attr_bh
.attr
,
552 static const struct attribute_group arm_ccn_pmu_cmp_mask_attr_group
= {
554 .attrs
= arm_ccn_pmu_cmp_mask_attrs
,
557 static ssize_t
arm_ccn_pmu_cpumask_show(struct device
*dev
,
558 struct device_attribute
*attr
, char *buf
)
560 struct arm_ccn
*ccn
= pmu_to_arm_ccn(dev_get_drvdata(dev
));
562 return cpumap_print_to_pagebuf(true, buf
, &ccn
->dt
.cpu
);
565 static struct device_attribute arm_ccn_pmu_cpumask_attr
=
566 __ATTR(cpumask
, S_IRUGO
, arm_ccn_pmu_cpumask_show
, NULL
);
568 static struct attribute
*arm_ccn_pmu_cpumask_attrs
[] = {
569 &arm_ccn_pmu_cpumask_attr
.attr
,
573 static const struct attribute_group arm_ccn_pmu_cpumask_attr_group
= {
574 .attrs
= arm_ccn_pmu_cpumask_attrs
,
578 * Default poll period is 10ms, which is way over the top anyway,
579 * as in the worst case scenario (an event every cycle), with 1GHz
580 * clocked bus, the smallest, 32 bit counter will overflow in
583 static unsigned int arm_ccn_pmu_poll_period_us
= 10000;
584 module_param_named(pmu_poll_period_us
, arm_ccn_pmu_poll_period_us
, uint
,
587 static ktime_t
arm_ccn_pmu_timer_period(void)
589 return ns_to_ktime((u64
)arm_ccn_pmu_poll_period_us
* 1000);
593 static const struct attribute_group
*arm_ccn_pmu_attr_groups
[] = {
594 &arm_ccn_pmu_events_attr_group
,
595 &arm_ccn_pmu_format_attr_group
,
596 &arm_ccn_pmu_cmp_mask_attr_group
,
597 &arm_ccn_pmu_cpumask_attr_group
,
602 static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap
, unsigned long size
)
607 bit
= find_first_zero_bit(bitmap
, size
);
610 } while (test_and_set_bit(bit
, bitmap
));
615 /* All RN-I and RN-D nodes have identical PMUs */
616 static int arm_ccn_pmu_type_eq(u32 a
, u32 b
)
622 case CCN_TYPE_RNI_1P
:
623 case CCN_TYPE_RNI_2P
:
624 case CCN_TYPE_RNI_3P
:
625 case CCN_TYPE_RND_1P
:
626 case CCN_TYPE_RND_2P
:
627 case CCN_TYPE_RND_3P
:
629 case CCN_TYPE_RNI_1P
:
630 case CCN_TYPE_RNI_2P
:
631 case CCN_TYPE_RNI_3P
:
632 case CCN_TYPE_RND_1P
:
633 case CCN_TYPE_RND_2P
:
634 case CCN_TYPE_RND_3P
:
643 static int arm_ccn_pmu_event_alloc(struct perf_event
*event
)
645 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
646 struct hw_perf_event
*hw
= &event
->hw
;
647 u32 node_xp
, type
, event_id
;
648 struct arm_ccn_component
*source
;
651 node_xp
= CCN_CONFIG_NODE(event
->attr
.config
);
652 type
= CCN_CONFIG_TYPE(event
->attr
.config
);
653 event_id
= CCN_CONFIG_EVENT(event
->attr
.config
);
655 /* Allocate the cycle counter */
656 if (type
== CCN_TYPE_CYCLES
) {
657 if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER
,
658 ccn
->dt
.pmu_counters_mask
))
661 hw
->idx
= CCN_IDX_PMU_CYCLE_COUNTER
;
662 ccn
->dt
.pmu_counters
[CCN_IDX_PMU_CYCLE_COUNTER
].event
= event
;
667 /* Allocate an event counter */
668 hw
->idx
= arm_ccn_pmu_alloc_bit(ccn
->dt
.pmu_counters_mask
,
669 CCN_NUM_PMU_EVENT_COUNTERS
);
671 dev_dbg(ccn
->dev
, "No more counters available!\n");
675 if (type
== CCN_TYPE_XP
)
676 source
= &ccn
->xp
[node_xp
];
678 source
= &ccn
->node
[node_xp
];
679 ccn
->dt
.pmu_counters
[hw
->idx
].source
= source
;
681 /* Allocate an event source or a watchpoint */
682 if (type
== CCN_TYPE_XP
&& event_id
== CCN_EVENT_WATCHPOINT
)
683 bit
= arm_ccn_pmu_alloc_bit(source
->xp
.dt_cmp_mask
,
684 CCN_NUM_XP_WATCHPOINTS
);
686 bit
= arm_ccn_pmu_alloc_bit(source
->pmu_events_mask
,
689 dev_dbg(ccn
->dev
, "No more event sources/watchpoints on node/XP %d!\n",
691 clear_bit(hw
->idx
, ccn
->dt
.pmu_counters_mask
);
694 hw
->config_base
= bit
;
696 ccn
->dt
.pmu_counters
[hw
->idx
].event
= event
;
701 static void arm_ccn_pmu_event_release(struct perf_event
*event
)
703 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
704 struct hw_perf_event
*hw
= &event
->hw
;
706 if (hw
->idx
== CCN_IDX_PMU_CYCLE_COUNTER
) {
707 clear_bit(CCN_IDX_PMU_CYCLE_COUNTER
, ccn
->dt
.pmu_counters_mask
);
709 struct arm_ccn_component
*source
=
710 ccn
->dt
.pmu_counters
[hw
->idx
].source
;
712 if (CCN_CONFIG_TYPE(event
->attr
.config
) == CCN_TYPE_XP
&&
713 CCN_CONFIG_EVENT(event
->attr
.config
) ==
714 CCN_EVENT_WATCHPOINT
)
715 clear_bit(hw
->config_base
, source
->xp
.dt_cmp_mask
);
717 clear_bit(hw
->config_base
, source
->pmu_events_mask
);
718 clear_bit(hw
->idx
, ccn
->dt
.pmu_counters_mask
);
721 ccn
->dt
.pmu_counters
[hw
->idx
].source
= NULL
;
722 ccn
->dt
.pmu_counters
[hw
->idx
].event
= NULL
;
725 static int arm_ccn_pmu_event_init(struct perf_event
*event
)
728 struct hw_perf_event
*hw
= &event
->hw
;
729 u32 node_xp
, type
, event_id
;
732 struct perf_event
*sibling
;
734 if (event
->attr
.type
!= event
->pmu
->type
)
737 ccn
= pmu_to_arm_ccn(event
->pmu
);
739 if (hw
->sample_period
) {
740 dev_dbg(ccn
->dev
, "Sampling not supported!\n");
744 if (has_branch_stack(event
)) {
745 dev_dbg(ccn
->dev
, "Can't exclude execution levels!\n");
749 if (event
->cpu
< 0) {
750 dev_dbg(ccn
->dev
, "Can't provide per-task data!\n");
754 * Many perf core operations (eg. events rotation) operate on a
755 * single CPU context. This is obvious for CPU PMUs, where one
756 * expects the same sets of events being observed on all CPUs,
757 * but can lead to issues for off-core PMUs, like CCN, where each
758 * event could be theoretically assigned to a different CPU. To
759 * mitigate this, we enforce CPU assignment to one, selected
760 * processor (the one described in the "cpumask" attribute).
762 event
->cpu
= cpumask_first(&ccn
->dt
.cpu
);
764 node_xp
= CCN_CONFIG_NODE(event
->attr
.config
);
765 type
= CCN_CONFIG_TYPE(event
->attr
.config
);
766 event_id
= CCN_CONFIG_EVENT(event
->attr
.config
);
768 /* Validate node/xp vs topology */
771 if (node_xp
!= ccn
->mn_id
) {
772 dev_dbg(ccn
->dev
, "Invalid MN ID %d!\n", node_xp
);
777 if (node_xp
>= ccn
->num_xps
) {
778 dev_dbg(ccn
->dev
, "Invalid XP ID %d!\n", node_xp
);
782 case CCN_TYPE_CYCLES
:
785 if (node_xp
>= ccn
->num_nodes
) {
786 dev_dbg(ccn
->dev
, "Invalid node ID %d!\n", node_xp
);
789 if (!arm_ccn_pmu_type_eq(type
, ccn
->node
[node_xp
].type
)) {
790 dev_dbg(ccn
->dev
, "Invalid type 0x%x for node %d!\n",
797 /* Validate event ID vs available for the type */
798 for (i
= 0, valid
= 0; i
< ARRAY_SIZE(arm_ccn_pmu_events
) && !valid
;
800 struct arm_ccn_pmu_event
*e
= &arm_ccn_pmu_events
[i
];
801 u32 port
= CCN_CONFIG_PORT(event
->attr
.config
);
802 u32 vc
= CCN_CONFIG_VC(event
->attr
.config
);
804 if (!arm_ccn_pmu_type_eq(type
, e
->type
))
806 if (event_id
!= e
->event
)
808 if (e
->num_ports
&& port
>= e
->num_ports
) {
809 dev_dbg(ccn
->dev
, "Invalid port %d for node/XP %d!\n",
813 if (e
->num_vcs
&& vc
>= e
->num_vcs
) {
814 dev_dbg(ccn
->dev
, "Invalid vc %d for node/XP %d!\n",
821 dev_dbg(ccn
->dev
, "Invalid event 0x%x for node/XP %d!\n",
826 /* Watchpoint-based event for a node is actually set on XP */
827 if (event_id
== CCN_EVENT_WATCHPOINT
&& type
!= CCN_TYPE_XP
) {
831 port
= arm_ccn_node_to_xp_port(node_xp
);
832 node_xp
= arm_ccn_node_to_xp(node_xp
);
834 arm_ccn_pmu_config_set(&event
->attr
.config
,
835 node_xp
, type
, port
);
839 * We must NOT create groups containing mixed PMUs, although software
840 * events are acceptable (for example to create a CCN group
841 * periodically read when a hrtimer aka cpu-clock leader triggers).
843 if (event
->group_leader
->pmu
!= event
->pmu
&&
844 !is_software_event(event
->group_leader
))
847 for_each_sibling_event(sibling
, event
->group_leader
) {
848 if (sibling
->pmu
!= event
->pmu
&&
849 !is_software_event(sibling
))
856 static u64
arm_ccn_pmu_read_counter(struct arm_ccn
*ccn
, int idx
)
860 if (idx
== CCN_IDX_PMU_CYCLE_COUNTER
) {
862 res
= readq(ccn
->dt
.base
+ CCN_DT_PMCCNTR
);
864 /* 40 bit counter, can do snapshot and read in two parts */
865 writel(0x1, ccn
->dt
.base
+ CCN_DT_PMSR_REQ
);
866 while (!(readl(ccn
->dt
.base
+ CCN_DT_PMSR
) & 0x1))
868 writel(0x1, ccn
->dt
.base
+ CCN_DT_PMSR_CLR
);
869 res
= readl(ccn
->dt
.base
+ CCN_DT_PMCCNTRSR
+ 4) & 0xff;
871 res
|= readl(ccn
->dt
.base
+ CCN_DT_PMCCNTRSR
);
874 res
= readl(ccn
->dt
.base
+ CCN_DT_PMEVCNT(idx
));
880 static void arm_ccn_pmu_event_update(struct perf_event
*event
)
882 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
883 struct hw_perf_event
*hw
= &event
->hw
;
884 u64 prev_count
, new_count
, mask
;
887 prev_count
= local64_read(&hw
->prev_count
);
888 new_count
= arm_ccn_pmu_read_counter(ccn
, hw
->idx
);
889 } while (local64_xchg(&hw
->prev_count
, new_count
) != prev_count
);
891 mask
= (1LLU << (hw
->idx
== CCN_IDX_PMU_CYCLE_COUNTER
? 40 : 32)) - 1;
893 local64_add((new_count
- prev_count
) & mask
, &event
->count
);
896 static void arm_ccn_pmu_xp_dt_config(struct perf_event
*event
, int enable
)
898 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
899 struct hw_perf_event
*hw
= &event
->hw
;
900 struct arm_ccn_component
*xp
;
903 /* Nothing to do for cycle counter */
904 if (hw
->idx
== CCN_IDX_PMU_CYCLE_COUNTER
)
907 if (CCN_CONFIG_TYPE(event
->attr
.config
) == CCN_TYPE_XP
)
908 xp
= &ccn
->xp
[CCN_CONFIG_XP(event
->attr
.config
)];
910 xp
= &ccn
->xp
[arm_ccn_node_to_xp(
911 CCN_CONFIG_NODE(event
->attr
.config
))];
914 dt_cfg
= hw
->event_base
;
916 dt_cfg
= CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH
;
918 spin_lock(&ccn
->dt
.config_lock
);
920 val
= readl(xp
->base
+ CCN_XP_DT_CONFIG
);
921 val
&= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK
<<
922 CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw
->idx
));
923 val
|= dt_cfg
<< CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw
->idx
);
924 writel(val
, xp
->base
+ CCN_XP_DT_CONFIG
);
926 spin_unlock(&ccn
->dt
.config_lock
);
929 static void arm_ccn_pmu_event_start(struct perf_event
*event
, int flags
)
931 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
932 struct hw_perf_event
*hw
= &event
->hw
;
934 local64_set(&event
->hw
.prev_count
,
935 arm_ccn_pmu_read_counter(ccn
, hw
->idx
));
938 /* Set the DT bus input, engaging the counter */
939 arm_ccn_pmu_xp_dt_config(event
, 1);
942 static void arm_ccn_pmu_event_stop(struct perf_event
*event
, int flags
)
944 struct hw_perf_event
*hw
= &event
->hw
;
946 /* Disable counting, setting the DT bus to pass-through mode */
947 arm_ccn_pmu_xp_dt_config(event
, 0);
949 if (flags
& PERF_EF_UPDATE
)
950 arm_ccn_pmu_event_update(event
);
952 hw
->state
|= PERF_HES_STOPPED
;
955 static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event
*event
)
957 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
958 struct hw_perf_event
*hw
= &event
->hw
;
959 struct arm_ccn_component
*source
=
960 ccn
->dt
.pmu_counters
[hw
->idx
].source
;
961 unsigned long wp
= hw
->config_base
;
963 u64 cmp_l
= event
->attr
.config1
;
964 u64 cmp_h
= event
->attr
.config2
;
965 u64 mask_l
= ccn
->dt
.cmp_mask
[CCN_CONFIG_MASK(event
->attr
.config
)].l
;
966 u64 mask_h
= ccn
->dt
.cmp_mask
[CCN_CONFIG_MASK(event
->attr
.config
)].h
;
968 hw
->event_base
= CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp
);
970 /* Direction (RX/TX), device (port) & virtual channel */
971 val
= readl(source
->base
+ CCN_XP_DT_INTERFACE_SEL
);
972 val
&= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK
<<
973 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp
));
974 val
|= CCN_CONFIG_DIR(event
->attr
.config
) <<
975 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp
);
976 val
&= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK
<<
977 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp
));
978 val
|= CCN_CONFIG_PORT(event
->attr
.config
) <<
979 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp
);
980 val
&= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK
<<
981 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp
));
982 val
|= CCN_CONFIG_VC(event
->attr
.config
) <<
983 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp
);
984 writel(val
, source
->base
+ CCN_XP_DT_INTERFACE_SEL
);
986 /* Comparison values */
987 writel(cmp_l
& 0xffffffff, source
->base
+ CCN_XP_DT_CMP_VAL_L(wp
));
988 writel((cmp_l
>> 32) & 0x7fffffff,
989 source
->base
+ CCN_XP_DT_CMP_VAL_L(wp
) + 4);
990 writel(cmp_h
& 0xffffffff, source
->base
+ CCN_XP_DT_CMP_VAL_H(wp
));
991 writel((cmp_h
>> 32) & 0x0fffffff,
992 source
->base
+ CCN_XP_DT_CMP_VAL_H(wp
) + 4);
995 writel(mask_l
& 0xffffffff, source
->base
+ CCN_XP_DT_CMP_MASK_L(wp
));
996 writel((mask_l
>> 32) & 0x7fffffff,
997 source
->base
+ CCN_XP_DT_CMP_MASK_L(wp
) + 4);
998 writel(mask_h
& 0xffffffff, source
->base
+ CCN_XP_DT_CMP_MASK_H(wp
));
999 writel((mask_h
>> 32) & 0x0fffffff,
1000 source
->base
+ CCN_XP_DT_CMP_MASK_H(wp
) + 4);
1003 static void arm_ccn_pmu_xp_event_config(struct perf_event
*event
)
1005 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
1006 struct hw_perf_event
*hw
= &event
->hw
;
1007 struct arm_ccn_component
*source
=
1008 ccn
->dt
.pmu_counters
[hw
->idx
].source
;
1011 hw
->event_base
= CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw
->config_base
);
1013 id
= (CCN_CONFIG_VC(event
->attr
.config
) << 4) |
1014 (CCN_CONFIG_BUS(event
->attr
.config
) << 3) |
1015 (CCN_CONFIG_EVENT(event
->attr
.config
) << 0);
1017 val
= readl(source
->base
+ CCN_XP_PMU_EVENT_SEL
);
1018 val
&= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK
<<
1019 CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw
->config_base
));
1020 val
|= id
<< CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw
->config_base
);
1021 writel(val
, source
->base
+ CCN_XP_PMU_EVENT_SEL
);
1024 static void arm_ccn_pmu_node_event_config(struct perf_event
*event
)
1026 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
1027 struct hw_perf_event
*hw
= &event
->hw
;
1028 struct arm_ccn_component
*source
=
1029 ccn
->dt
.pmu_counters
[hw
->idx
].source
;
1030 u32 type
= CCN_CONFIG_TYPE(event
->attr
.config
);
1033 port
= arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event
->attr
.config
));
1034 hw
->event_base
= CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port
,
1037 /* These *_event_sel regs should be identical, but let's make sure... */
1038 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL
!= CCN_SBAS_PMU_EVENT_SEL
);
1039 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL
!= CCN_RNI_PMU_EVENT_SEL
);
1040 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
1041 CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
1042 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
1043 CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
1044 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK
!=
1045 CCN_SBAS_PMU_EVENT_SEL__ID__MASK
);
1046 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK
!=
1047 CCN_RNI_PMU_EVENT_SEL__ID__MASK
);
1048 if (WARN_ON(type
!= CCN_TYPE_HNF
&& type
!= CCN_TYPE_SBAS
&&
1049 !arm_ccn_pmu_type_eq(type
, CCN_TYPE_RNI_3P
)))
1052 /* Set the event id for the pre-allocated counter */
1053 val
= readl(source
->base
+ CCN_HNF_PMU_EVENT_SEL
);
1054 val
&= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK
<<
1055 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw
->config_base
));
1056 val
|= CCN_CONFIG_EVENT(event
->attr
.config
) <<
1057 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw
->config_base
);
1058 writel(val
, source
->base
+ CCN_HNF_PMU_EVENT_SEL
);
1061 static void arm_ccn_pmu_event_config(struct perf_event
*event
)
1063 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
1064 struct hw_perf_event
*hw
= &event
->hw
;
1065 u32 xp
, offset
, val
;
1067 /* Cycle counter requires no setup */
1068 if (hw
->idx
== CCN_IDX_PMU_CYCLE_COUNTER
)
1071 if (CCN_CONFIG_TYPE(event
->attr
.config
) == CCN_TYPE_XP
)
1072 xp
= CCN_CONFIG_XP(event
->attr
.config
);
1074 xp
= arm_ccn_node_to_xp(CCN_CONFIG_NODE(event
->attr
.config
));
1076 spin_lock(&ccn
->dt
.config_lock
);
1078 /* Set the DT bus "distance" register */
1079 offset
= (hw
->idx
/ 4) * 4;
1080 val
= readl(ccn
->dt
.base
+ CCN_DT_ACTIVE_DSM
+ offset
);
1081 val
&= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK
<<
1082 CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw
->idx
% 4));
1083 val
|= xp
<< CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw
->idx
% 4);
1084 writel(val
, ccn
->dt
.base
+ CCN_DT_ACTIVE_DSM
+ offset
);
1086 if (CCN_CONFIG_TYPE(event
->attr
.config
) == CCN_TYPE_XP
) {
1087 if (CCN_CONFIG_EVENT(event
->attr
.config
) ==
1088 CCN_EVENT_WATCHPOINT
)
1089 arm_ccn_pmu_xp_watchpoint_config(event
);
1091 arm_ccn_pmu_xp_event_config(event
);
1093 arm_ccn_pmu_node_event_config(event
);
1096 spin_unlock(&ccn
->dt
.config_lock
);
1099 static int arm_ccn_pmu_active_counters(struct arm_ccn
*ccn
)
1101 return bitmap_weight(ccn
->dt
.pmu_counters_mask
,
1102 CCN_NUM_PMU_EVENT_COUNTERS
+ 1);
1105 static int arm_ccn_pmu_event_add(struct perf_event
*event
, int flags
)
1108 struct hw_perf_event
*hw
= &event
->hw
;
1109 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
1111 err
= arm_ccn_pmu_event_alloc(event
);
1116 * Pin the timer, so that the overflows are handled by the chosen
1117 * event->cpu (this is the same one as presented in "cpumask"
1120 if (!ccn
->irq
&& arm_ccn_pmu_active_counters(ccn
) == 1)
1121 hrtimer_start(&ccn
->dt
.hrtimer
, arm_ccn_pmu_timer_period(),
1122 HRTIMER_MODE_REL_PINNED
);
1124 arm_ccn_pmu_event_config(event
);
1126 hw
->state
= PERF_HES_STOPPED
;
1128 if (flags
& PERF_EF_START
)
1129 arm_ccn_pmu_event_start(event
, PERF_EF_UPDATE
);
1134 static void arm_ccn_pmu_event_del(struct perf_event
*event
, int flags
)
1136 struct arm_ccn
*ccn
= pmu_to_arm_ccn(event
->pmu
);
1138 arm_ccn_pmu_event_stop(event
, PERF_EF_UPDATE
);
1140 arm_ccn_pmu_event_release(event
);
1142 if (!ccn
->irq
&& arm_ccn_pmu_active_counters(ccn
) == 0)
1143 hrtimer_cancel(&ccn
->dt
.hrtimer
);
1146 static void arm_ccn_pmu_event_read(struct perf_event
*event
)
1148 arm_ccn_pmu_event_update(event
);
1151 static void arm_ccn_pmu_enable(struct pmu
*pmu
)
1153 struct arm_ccn
*ccn
= pmu_to_arm_ccn(pmu
);
1155 u32 val
= readl(ccn
->dt
.base
+ CCN_DT_PMCR
);
1156 val
|= CCN_DT_PMCR__PMU_EN
;
1157 writel(val
, ccn
->dt
.base
+ CCN_DT_PMCR
);
1160 static void arm_ccn_pmu_disable(struct pmu
*pmu
)
1162 struct arm_ccn
*ccn
= pmu_to_arm_ccn(pmu
);
1164 u32 val
= readl(ccn
->dt
.base
+ CCN_DT_PMCR
);
1165 val
&= ~CCN_DT_PMCR__PMU_EN
;
1166 writel(val
, ccn
->dt
.base
+ CCN_DT_PMCR
);
1169 static irqreturn_t
arm_ccn_pmu_overflow_handler(struct arm_ccn_dt
*dt
)
1171 u32 pmovsr
= readl(dt
->base
+ CCN_DT_PMOVSR
);
1177 writel(pmovsr
, dt
->base
+ CCN_DT_PMOVSR_CLR
);
1179 BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER
!= CCN_NUM_PMU_EVENT_COUNTERS
);
1181 for (idx
= 0; idx
< CCN_NUM_PMU_EVENT_COUNTERS
+ 1; idx
++) {
1182 struct perf_event
*event
= dt
->pmu_counters
[idx
].event
;
1183 int overflowed
= pmovsr
& BIT(idx
);
1185 WARN_ON_ONCE(overflowed
&& !event
&&
1186 idx
!= CCN_IDX_PMU_CYCLE_COUNTER
);
1188 if (!event
|| !overflowed
)
1191 arm_ccn_pmu_event_update(event
);
1197 static enum hrtimer_restart
arm_ccn_pmu_timer_handler(struct hrtimer
*hrtimer
)
1199 struct arm_ccn_dt
*dt
= container_of(hrtimer
, struct arm_ccn_dt
,
1201 unsigned long flags
;
1203 local_irq_save(flags
);
1204 arm_ccn_pmu_overflow_handler(dt
);
1205 local_irq_restore(flags
);
1207 hrtimer_forward_now(hrtimer
, arm_ccn_pmu_timer_period());
1208 return HRTIMER_RESTART
;
1212 static int arm_ccn_pmu_offline_cpu(unsigned int cpu
, struct hlist_node
*node
)
1214 struct arm_ccn_dt
*dt
= hlist_entry_safe(node
, struct arm_ccn_dt
, node
);
1215 struct arm_ccn
*ccn
= container_of(dt
, struct arm_ccn
, dt
);
1216 unsigned int target
;
1218 if (!cpumask_test_and_clear_cpu(cpu
, &dt
->cpu
))
1220 target
= cpumask_any_but(cpu_online_mask
, cpu
);
1221 if (target
>= nr_cpu_ids
)
1223 perf_pmu_migrate_context(&dt
->pmu
, cpu
, target
);
1224 cpumask_set_cpu(target
, &dt
->cpu
);
1226 WARN_ON(irq_set_affinity_hint(ccn
->irq
, &dt
->cpu
) != 0);
1230 static DEFINE_IDA(arm_ccn_pmu_ida
);
1232 static int arm_ccn_pmu_init(struct arm_ccn
*ccn
)
1238 /* Initialize DT subsystem */
1239 ccn
->dt
.base
= ccn
->base
+ CCN_REGION_SIZE
;
1240 spin_lock_init(&ccn
->dt
.config_lock
);
1241 writel(CCN_DT_PMOVSR_CLR__MASK
, ccn
->dt
.base
+ CCN_DT_PMOVSR_CLR
);
1242 writel(CCN_DT_CTL__DT_EN
, ccn
->dt
.base
+ CCN_DT_CTL
);
1243 writel(CCN_DT_PMCR__OVFL_INTR_EN
| CCN_DT_PMCR__PMU_EN
,
1244 ccn
->dt
.base
+ CCN_DT_PMCR
);
1245 writel(0x1, ccn
->dt
.base
+ CCN_DT_PMSR_CLR
);
1246 for (i
= 0; i
< ccn
->num_xps
; i
++) {
1247 writel(0, ccn
->xp
[i
].base
+ CCN_XP_DT_CONFIG
);
1248 writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS
<<
1249 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
1250 (CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS
<<
1251 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
1252 CCN_XP_DT_CONTROL__DT_ENABLE
,
1253 ccn
->xp
[i
].base
+ CCN_XP_DT_CONTROL
);
1255 ccn
->dt
.cmp_mask
[CCN_IDX_MASK_ANY
].l
= ~0;
1256 ccn
->dt
.cmp_mask
[CCN_IDX_MASK_ANY
].h
= ~0;
1257 ccn
->dt
.cmp_mask
[CCN_IDX_MASK_EXACT
].l
= 0;
1258 ccn
->dt
.cmp_mask
[CCN_IDX_MASK_EXACT
].h
= 0;
1259 ccn
->dt
.cmp_mask
[CCN_IDX_MASK_ORDER
].l
= ~0;
1260 ccn
->dt
.cmp_mask
[CCN_IDX_MASK_ORDER
].h
= ~(0x1 << 15);
1261 ccn
->dt
.cmp_mask
[CCN_IDX_MASK_OPCODE
].l
= ~0;
1262 ccn
->dt
.cmp_mask
[CCN_IDX_MASK_OPCODE
].h
= ~(0x1f << 9);
1264 /* Get a convenient /sys/event_source/devices/ name */
1265 ccn
->dt
.id
= ida_simple_get(&arm_ccn_pmu_ida
, 0, 0, GFP_KERNEL
);
1266 if (ccn
->dt
.id
== 0) {
1269 name
= devm_kasprintf(ccn
->dev
, GFP_KERNEL
, "ccn_%d",
1273 goto error_choose_name
;
1277 /* Perf driver registration */
1278 ccn
->dt
.pmu
= (struct pmu
) {
1279 .module
= THIS_MODULE
,
1280 .attr_groups
= arm_ccn_pmu_attr_groups
,
1281 .task_ctx_nr
= perf_invalid_context
,
1282 .event_init
= arm_ccn_pmu_event_init
,
1283 .add
= arm_ccn_pmu_event_add
,
1284 .del
= arm_ccn_pmu_event_del
,
1285 .start
= arm_ccn_pmu_event_start
,
1286 .stop
= arm_ccn_pmu_event_stop
,
1287 .read
= arm_ccn_pmu_event_read
,
1288 .pmu_enable
= arm_ccn_pmu_enable
,
1289 .pmu_disable
= arm_ccn_pmu_disable
,
1290 .capabilities
= PERF_PMU_CAP_NO_EXCLUDE
,
1293 /* No overflow interrupt? Have to use a timer instead. */
1295 dev_info(ccn
->dev
, "No access to interrupts, using timer.\n");
1296 hrtimer_init(&ccn
->dt
.hrtimer
, CLOCK_MONOTONIC
,
1298 ccn
->dt
.hrtimer
.function
= arm_ccn_pmu_timer_handler
;
1301 /* Pick one CPU which we will use to collect data from CCN... */
1302 cpumask_set_cpu(get_cpu(), &ccn
->dt
.cpu
);
1304 /* Also make sure that the overflow interrupt is handled by this CPU */
1306 err
= irq_set_affinity_hint(ccn
->irq
, &ccn
->dt
.cpu
);
1308 dev_err(ccn
->dev
, "Failed to set interrupt affinity!\n");
1309 goto error_set_affinity
;
1313 err
= perf_pmu_register(&ccn
->dt
.pmu
, name
, -1);
1315 goto error_pmu_register
;
1317 cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE
,
1326 ida_simple_remove(&arm_ccn_pmu_ida
, ccn
->dt
.id
);
1327 for (i
= 0; i
< ccn
->num_xps
; i
++)
1328 writel(0, ccn
->xp
[i
].base
+ CCN_XP_DT_CONTROL
);
1329 writel(0, ccn
->dt
.base
+ CCN_DT_PMCR
);
1333 static void arm_ccn_pmu_cleanup(struct arm_ccn
*ccn
)
1337 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE
,
1340 irq_set_affinity_hint(ccn
->irq
, NULL
);
1341 for (i
= 0; i
< ccn
->num_xps
; i
++)
1342 writel(0, ccn
->xp
[i
].base
+ CCN_XP_DT_CONTROL
);
1343 writel(0, ccn
->dt
.base
+ CCN_DT_PMCR
);
1344 perf_pmu_unregister(&ccn
->dt
.pmu
);
1345 ida_simple_remove(&arm_ccn_pmu_ida
, ccn
->dt
.id
);
1348 static int arm_ccn_for_each_valid_region(struct arm_ccn
*ccn
,
1349 int (*callback
)(struct arm_ccn
*ccn
, int region
,
1350 void __iomem
*base
, u32 type
, u32 id
))
1354 for (region
= 0; region
< CCN_NUM_REGIONS
; region
++) {
1359 val
= readl(ccn
->base
+ CCN_MN_OLY_COMP_LIST_63_0
+
1361 if (!(val
& (1 << (region
% 32))))
1364 base
= ccn
->base
+ region
* CCN_REGION_SIZE
;
1365 val
= readl(base
+ CCN_ALL_OLY_ID
);
1366 type
= (val
>> CCN_ALL_OLY_ID__OLY_ID__SHIFT
) &
1367 CCN_ALL_OLY_ID__OLY_ID__MASK
;
1368 id
= (val
>> CCN_ALL_OLY_ID__NODE_ID__SHIFT
) &
1369 CCN_ALL_OLY_ID__NODE_ID__MASK
;
1371 err
= callback(ccn
, region
, base
, type
, id
);
1379 static int arm_ccn_get_nodes_num(struct arm_ccn
*ccn
, int region
,
1380 void __iomem
*base
, u32 type
, u32 id
)
1383 if (type
== CCN_TYPE_XP
&& id
>= ccn
->num_xps
)
1384 ccn
->num_xps
= id
+ 1;
1385 else if (id
>= ccn
->num_nodes
)
1386 ccn
->num_nodes
= id
+ 1;
1391 static int arm_ccn_init_nodes(struct arm_ccn
*ccn
, int region
,
1392 void __iomem
*base
, u32 type
, u32 id
)
1394 struct arm_ccn_component
*component
;
1396 dev_dbg(ccn
->dev
, "Region %d: id=%u, type=0x%02x\n", region
, id
, type
);
1405 component
= &ccn
->xp
[id
];
1408 ccn
->sbsx_present
= 1;
1409 component
= &ccn
->node
[id
];
1412 ccn
->sbas_present
= 1;
1415 component
= &ccn
->node
[id
];
1419 component
->base
= base
;
1420 component
->type
= type
;
1426 static irqreturn_t
arm_ccn_error_handler(struct arm_ccn
*ccn
,
1427 const u32
*err_sig_val
)
1429 /* This should be really handled by firmware... */
1430 dev_err(ccn
->dev
, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
1431 err_sig_val
[5], err_sig_val
[4], err_sig_val
[3],
1432 err_sig_val
[2], err_sig_val
[1], err_sig_val
[0]);
1433 dev_err(ccn
->dev
, "Disabling interrupt generation for all errors.\n");
1434 writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE
,
1435 ccn
->base
+ CCN_MN_ERRINT_STATUS
);
1441 static irqreturn_t
arm_ccn_irq_handler(int irq
, void *dev_id
)
1443 irqreturn_t res
= IRQ_NONE
;
1444 struct arm_ccn
*ccn
= dev_id
;
1449 /* PMU overflow is a special case */
1450 err_or
= err_sig_val
[0] = readl(ccn
->base
+ CCN_MN_ERR_SIG_VAL_63_0
);
1451 if (err_or
& CCN_MN_ERR_SIG_VAL_63_0__DT
) {
1452 err_or
&= ~CCN_MN_ERR_SIG_VAL_63_0__DT
;
1453 res
= arm_ccn_pmu_overflow_handler(&ccn
->dt
);
1456 /* Have to read all err_sig_vals to clear them */
1457 for (i
= 1; i
< ARRAY_SIZE(err_sig_val
); i
++) {
1458 err_sig_val
[i
] = readl(ccn
->base
+
1459 CCN_MN_ERR_SIG_VAL_63_0
+ i
* 4);
1460 err_or
|= err_sig_val
[i
];
1463 res
|= arm_ccn_error_handler(ccn
, err_sig_val
);
1465 if (res
!= IRQ_NONE
)
1466 writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT
,
1467 ccn
->base
+ CCN_MN_ERRINT_STATUS
);
1473 static int arm_ccn_probe(struct platform_device
*pdev
)
1475 struct arm_ccn
*ccn
;
1476 struct resource
*res
;
1480 ccn
= devm_kzalloc(&pdev
->dev
, sizeof(*ccn
), GFP_KERNEL
);
1483 ccn
->dev
= &pdev
->dev
;
1484 platform_set_drvdata(pdev
, ccn
);
1486 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1487 ccn
->base
= devm_ioremap_resource(ccn
->dev
, res
);
1488 if (IS_ERR(ccn
->base
))
1489 return PTR_ERR(ccn
->base
);
1491 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1496 /* Check if we can use the interrupt */
1497 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE
,
1498 ccn
->base
+ CCN_MN_ERRINT_STATUS
);
1499 if (readl(ccn
->base
+ CCN_MN_ERRINT_STATUS
) &
1500 CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED
) {
1501 /* Can set 'disable' bits, so can acknowledge interrupts */
1502 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE
,
1503 ccn
->base
+ CCN_MN_ERRINT_STATUS
);
1504 err
= devm_request_irq(ccn
->dev
, irq
, arm_ccn_irq_handler
,
1505 IRQF_NOBALANCING
| IRQF_NO_THREAD
,
1506 dev_name(ccn
->dev
), ccn
);
1514 /* Build topology */
1516 err
= arm_ccn_for_each_valid_region(ccn
, arm_ccn_get_nodes_num
);
1520 ccn
->node
= devm_kcalloc(ccn
->dev
, ccn
->num_nodes
, sizeof(*ccn
->node
),
1522 ccn
->xp
= devm_kcalloc(ccn
->dev
, ccn
->num_xps
, sizeof(*ccn
->node
),
1524 if (!ccn
->node
|| !ccn
->xp
)
1527 err
= arm_ccn_for_each_valid_region(ccn
, arm_ccn_init_nodes
);
1531 return arm_ccn_pmu_init(ccn
);
1534 static int arm_ccn_remove(struct platform_device
*pdev
)
1536 struct arm_ccn
*ccn
= platform_get_drvdata(pdev
);
1538 arm_ccn_pmu_cleanup(ccn
);
1543 static const struct of_device_id arm_ccn_match
[] = {
1544 { .compatible
= "arm,ccn-502", },
1545 { .compatible
= "arm,ccn-504", },
1548 MODULE_DEVICE_TABLE(of
, arm_ccn_match
);
1550 static struct platform_driver arm_ccn_driver
= {
1553 .of_match_table
= arm_ccn_match
,
1555 .probe
= arm_ccn_probe
,
1556 .remove
= arm_ccn_remove
,
1559 static int __init
arm_ccn_init(void)
1563 ret
= cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE
,
1564 "perf/arm/ccn:online", NULL
,
1565 arm_ccn_pmu_offline_cpu
);
1569 for (i
= 0; i
< ARRAY_SIZE(arm_ccn_pmu_events
); i
++)
1570 arm_ccn_pmu_events_attrs
[i
] = &arm_ccn_pmu_events
[i
].attr
.attr
;
1572 ret
= platform_driver_register(&arm_ccn_driver
);
1574 cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE
);
1578 static void __exit
arm_ccn_exit(void)
1580 platform_driver_unregister(&arm_ccn_driver
);
1581 cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE
);
1584 module_init(arm_ccn_init
);
1585 module_exit(arm_ccn_exit
);
1587 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
1588 MODULE_LICENSE("GPL v2");