1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2014 ARM Limited
7 #include <linux/ctype.h>
8 #include <linux/hrtimer.h>
10 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/perf_event.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 #define CCN_NUM_XP_PORTS 2
20 #define CCN_NUM_REGIONS 256
21 #define CCN_REGION_SIZE 0x10000
23 #define CCN_ALL_OLY_ID 0xff00
24 #define CCN_ALL_OLY_ID__OLY_ID__SHIFT 0
25 #define CCN_ALL_OLY_ID__OLY_ID__MASK 0x1f
26 #define CCN_ALL_OLY_ID__NODE_ID__SHIFT 8
27 #define CCN_ALL_OLY_ID__NODE_ID__MASK 0x3f
29 #define CCN_MN_ERRINT_STATUS 0x0008
30 #define CCN_MN_ERRINT_STATUS__INTREQ__DESSERT 0x11
31 #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__ENABLE 0x02
32 #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLED 0x20
33 #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE 0x22
34 #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_ENABLE 0x04
35 #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLED 0x40
36 #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLE 0x44
37 #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE 0x08
38 #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED 0x80
39 #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE 0x88
40 #define CCN_MN_OLY_COMP_LIST_63_0 0x01e0
41 #define CCN_MN_ERR_SIG_VAL_63_0 0x0300
42 #define CCN_MN_ERR_SIG_VAL_63_0__DT (1 << 1)
44 #define CCN_DT_ACTIVE_DSM 0x0000
45 #define CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(n) ((n) * 8)
46 #define CCN_DT_ACTIVE_DSM__DSM_ID__MASK 0xff
47 #define CCN_DT_CTL 0x0028
48 #define CCN_DT_CTL__DT_EN (1 << 0)
49 #define CCN_DT_PMEVCNT(n) (0x0100 + (n) * 0x8)
50 #define CCN_DT_PMCCNTR 0x0140
51 #define CCN_DT_PMCCNTRSR 0x0190
52 #define CCN_DT_PMOVSR 0x0198
53 #define CCN_DT_PMOVSR_CLR 0x01a0
54 #define CCN_DT_PMOVSR_CLR__MASK 0x1f
55 #define CCN_DT_PMCR 0x01a8
56 #define CCN_DT_PMCR__OVFL_INTR_EN (1 << 6)
57 #define CCN_DT_PMCR__PMU_EN (1 << 0)
58 #define CCN_DT_PMSR 0x01b0
59 #define CCN_DT_PMSR_REQ 0x01b8
60 #define CCN_DT_PMSR_CLR 0x01c0
62 #define CCN_HNF_PMU_EVENT_SEL 0x0600
63 #define CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
64 #define CCN_HNF_PMU_EVENT_SEL__ID__MASK 0xf
66 #define CCN_XP_DT_CONFIG 0x0300
67 #define CCN_XP_DT_CONFIG__DT_CFG__SHIFT(n) ((n) * 4)
68 #define CCN_XP_DT_CONFIG__DT_CFG__MASK 0xf
69 #define CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH 0x0
70 #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT_0_OR_1 0x1
71 #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(n) (0x2 + (n))
72 #define CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(n) (0x4 + (n))
73 #define CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(d, n) (0x8 + (d) * 4 + (n))
74 #define CCN_XP_DT_INTERFACE_SEL 0x0308
75 #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(n) (0 + (n) * 8)
76 #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK 0x1
77 #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(n) (1 + (n) * 8)
78 #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK 0x1
79 #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(n) (2 + (n) * 8)
80 #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK 0x3
81 #define CCN_XP_DT_CMP_VAL_L(n) (0x0310 + (n) * 0x40)
82 #define CCN_XP_DT_CMP_VAL_H(n) (0x0318 + (n) * 0x40)
83 #define CCN_XP_DT_CMP_MASK_L(n) (0x0320 + (n) * 0x40)
84 #define CCN_XP_DT_CMP_MASK_H(n) (0x0328 + (n) * 0x40)
85 #define CCN_XP_DT_CONTROL 0x0370
86 #define CCN_XP_DT_CONTROL__DT_ENABLE (1 << 0)
87 #define CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(n) (12 + (n) * 4)
88 #define CCN_XP_DT_CONTROL__WP_ARM_SEL__MASK 0xf
89 #define CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS 0xf
90 #define CCN_XP_PMU_EVENT_SEL 0x0600
91 #define CCN_XP_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 7)
92 #define CCN_XP_PMU_EVENT_SEL__ID__MASK 0x3f
94 #define CCN_SBAS_PMU_EVENT_SEL 0x0600
95 #define CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
96 #define CCN_SBAS_PMU_EVENT_SEL__ID__MASK 0xf
98 #define CCN_RNI_PMU_EVENT_SEL 0x0600
99 #define CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
100 #define CCN_RNI_PMU_EVENT_SEL__ID__MASK 0xf
102 #define CCN_TYPE_MN 0x01
103 #define CCN_TYPE_DT 0x02
104 #define CCN_TYPE_HNF 0x04
105 #define CCN_TYPE_HNI 0x05
106 #define CCN_TYPE_XP 0x08
107 #define CCN_TYPE_SBSX 0x0c
108 #define CCN_TYPE_SBAS 0x10
109 #define CCN_TYPE_RNI_1P 0x14
110 #define CCN_TYPE_RNI_2P 0x15
111 #define CCN_TYPE_RNI_3P 0x16
112 #define CCN_TYPE_RND_1P 0x18 /* RN-D = RN-I + DVM */
113 #define CCN_TYPE_RND_2P 0x19
114 #define CCN_TYPE_RND_3P 0x1a
115 #define CCN_TYPE_CYCLES 0xff /* Pseudotype */
117 #define CCN_EVENT_WATCHPOINT 0xfe /* Pseudoevent */
119 #define CCN_NUM_PMU_EVENTS 4
120 #define CCN_NUM_XP_WATCHPOINTS 2 /* See DT.dbg_id.num_watchpoints */
121 #define CCN_NUM_PMU_EVENT_COUNTERS 8 /* See DT.dbg_id.num_pmucntr */
122 #define CCN_IDX_PMU_CYCLE_COUNTER CCN_NUM_PMU_EVENT_COUNTERS
124 #define CCN_NUM_PREDEFINED_MASKS 4
125 #define CCN_IDX_MASK_ANY (CCN_NUM_PMU_EVENT_COUNTERS + 0)
126 #define CCN_IDX_MASK_EXACT (CCN_NUM_PMU_EVENT_COUNTERS + 1)
127 #define CCN_IDX_MASK_ORDER (CCN_NUM_PMU_EVENT_COUNTERS + 2)
128 #define CCN_IDX_MASK_OPCODE (CCN_NUM_PMU_EVENT_COUNTERS + 3)
130 struct arm_ccn_component {
134 DECLARE_BITMAP(pmu_events_mask, CCN_NUM_PMU_EVENTS);
137 DECLARE_BITMAP(dt_cmp_mask, CCN_NUM_XP_WATCHPOINTS);
142 #define pmu_to_arm_ccn(_pmu) container_of(container_of(_pmu, \
143 struct arm_ccn_dt, pmu), struct arm_ccn, dt)
149 spinlock_t config_lock;
151 DECLARE_BITMAP(pmu_counters_mask, CCN_NUM_PMU_EVENT_COUNTERS + 1);
153 struct arm_ccn_component *source;
154 struct perf_event *event;
155 } pmu_counters[CCN_NUM_PMU_EVENT_COUNTERS + 1];
159 } cmp_mask[CCN_NUM_PMU_EVENT_COUNTERS + CCN_NUM_PREDEFINED_MASKS];
161 struct hrtimer hrtimer;
164 struct hlist_node node;
174 unsigned sbas_present:1;
175 unsigned sbsx_present:1;
178 struct arm_ccn_component *node;
181 struct arm_ccn_component *xp;
183 struct arm_ccn_dt dt;
187 static int arm_ccn_node_to_xp(int node)
189 return node / CCN_NUM_XP_PORTS;
192 static int arm_ccn_node_to_xp_port(int node)
194 return node % CCN_NUM_XP_PORTS;
199 * Bit shifts and masks in these defines must be kept in sync with
200 * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
202 #define CCN_CONFIG_NODE(_config) (((_config) >> 0) & 0xff)
203 #define CCN_CONFIG_XP(_config) (((_config) >> 0) & 0xff)
204 #define CCN_CONFIG_TYPE(_config) (((_config) >> 8) & 0xff)
205 #define CCN_CONFIG_EVENT(_config) (((_config) >> 16) & 0xff)
206 #define CCN_CONFIG_PORT(_config) (((_config) >> 24) & 0x3)
207 #define CCN_CONFIG_BUS(_config) (((_config) >> 24) & 0x3)
208 #define CCN_CONFIG_VC(_config) (((_config) >> 26) & 0x7)
209 #define CCN_CONFIG_DIR(_config) (((_config) >> 29) & 0x1)
210 #define CCN_CONFIG_MASK(_config) (((_config) >> 30) & 0xf)
212 static void arm_ccn_pmu_config_set(u64 *config, u32 node_xp, u32 type, u32 port)
214 *config &= ~((0xff << 0) | (0xff << 8) | (0x3 << 24));
215 *config |= (node_xp << 0) | (type << 8) | (port << 24);
218 static ssize_t arm_ccn_pmu_format_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
221 struct dev_ext_attribute *ea = container_of(attr,
222 struct dev_ext_attribute, attr);
224 return sysfs_emit(buf, "%s\n", (char *)ea->var);
227 #define CCN_FORMAT_ATTR(_name, _config) \
228 struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
229 { __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
232 static CCN_FORMAT_ATTR(node, "config:0-7");
233 static CCN_FORMAT_ATTR(xp, "config:0-7");
234 static CCN_FORMAT_ATTR(type, "config:8-15");
235 static CCN_FORMAT_ATTR(event, "config:16-23");
236 static CCN_FORMAT_ATTR(port, "config:24-25");
237 static CCN_FORMAT_ATTR(bus, "config:24-25");
238 static CCN_FORMAT_ATTR(vc, "config:26-28");
239 static CCN_FORMAT_ATTR(dir, "config:29-29");
240 static CCN_FORMAT_ATTR(mask, "config:30-33");
241 static CCN_FORMAT_ATTR(cmp_l, "config1:0-62");
242 static CCN_FORMAT_ATTR(cmp_h, "config2:0-59");
244 static struct attribute *arm_ccn_pmu_format_attrs[] = {
245 &arm_ccn_pmu_format_attr_node.attr.attr,
246 &arm_ccn_pmu_format_attr_xp.attr.attr,
247 &arm_ccn_pmu_format_attr_type.attr.attr,
248 &arm_ccn_pmu_format_attr_event.attr.attr,
249 &arm_ccn_pmu_format_attr_port.attr.attr,
250 &arm_ccn_pmu_format_attr_bus.attr.attr,
251 &arm_ccn_pmu_format_attr_vc.attr.attr,
252 &arm_ccn_pmu_format_attr_dir.attr.attr,
253 &arm_ccn_pmu_format_attr_mask.attr.attr,
254 &arm_ccn_pmu_format_attr_cmp_l.attr.attr,
255 &arm_ccn_pmu_format_attr_cmp_h.attr.attr,
259 static const struct attribute_group arm_ccn_pmu_format_attr_group = {
261 .attrs = arm_ccn_pmu_format_attrs,
265 struct arm_ccn_pmu_event {
266 struct device_attribute attr;
275 #define CCN_EVENT_ATTR(_name) \
276 __ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
279 * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
280 * their ports in XP they are connected to. For the sake of usability they are
281 * explicitly defined here (and translated into a relevant watchpoint in
282 * arm_ccn_pmu_event_init()) so the user can easily request them without deep
283 * knowledge of the flit format.
286 #define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
287 .type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
288 .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
289 .def = _def, .mask = _mask, }
291 #define CCN_EVENT_HNI(_name, _def, _mask) { \
292 .attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
293 .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
294 .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
296 #define CCN_EVENT_SBSX(_name, _def, _mask) { \
297 .attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
298 .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
299 .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
301 #define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
302 .type = CCN_TYPE_HNF, .event = _event, }
304 #define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
305 .type = CCN_TYPE_XP, .event = _event, \
306 .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
309 * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
310 * on configuration. One of them is picked to represent the whole group,
311 * as they all share the same event types.
313 #define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
314 .type = CCN_TYPE_RNI_3P, .event = _event, }
316 #define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
317 .type = CCN_TYPE_SBAS, .event = _event, }
319 #define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
320 .type = CCN_TYPE_CYCLES }
323 static ssize_t arm_ccn_pmu_event_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
327 struct arm_ccn_pmu_event *event = container_of(attr,
328 struct arm_ccn_pmu_event, attr);
331 res = sysfs_emit(buf, "type=0x%x", event->type);
333 res += sysfs_emit_at(buf, res, ",event=0x%x", event->event);
335 res += sysfs_emit_at(buf, res, ",%s", event->def);
337 res += sysfs_emit_at(buf, res, ",mask=0x%x", event->mask);
339 /* Arguments required by an event */
340 switch (event->type) {
341 case CCN_TYPE_CYCLES:
344 res += sysfs_emit_at(buf, res, ",xp=?,vc=?");
345 if (event->event == CCN_EVENT_WATCHPOINT)
346 res += sysfs_emit_at(buf, res,
347 ",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
349 res += sysfs_emit_at(buf, res, ",bus=?");
353 res += sysfs_emit_at(buf, res, ",node=%d", ccn->mn_id);
356 res += sysfs_emit_at(buf, res, ",node=?");
360 res += sysfs_emit_at(buf, res, "\n");
365 static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
366 struct attribute *attr, int index)
368 struct device *dev = kobj_to_dev(kobj);
369 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
370 struct device_attribute *dev_attr = container_of(attr,
371 struct device_attribute, attr);
372 struct arm_ccn_pmu_event *event = container_of(dev_attr,
373 struct arm_ccn_pmu_event, attr);
375 if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
377 if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
383 static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
384 CCN_EVENT_MN(eobarrier, "dir=1,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
385 CCN_EVENT_MN(ecbarrier, "dir=1,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
386 CCN_EVENT_MN(dvmop, "dir=1,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
387 CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
388 CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
389 CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
390 CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
391 CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
393 CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
394 CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
395 CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
396 CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
397 CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
399 CCN_EVENT_HNF(cache_miss, 0x1),
400 CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
401 CCN_EVENT_HNF(cache_fill, 0x3),
402 CCN_EVENT_HNF(pocq_retry, 0x4),
403 CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
404 CCN_EVENT_HNF(sf_hit, 0x6),
405 CCN_EVENT_HNF(sf_evictions, 0x7),
406 CCN_EVENT_HNF(snoops_sent, 0x8),
407 CCN_EVENT_HNF(snoops_broadcast, 0x9),
408 CCN_EVENT_HNF(l3_eviction, 0xa),
409 CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
410 CCN_EVENT_HNF(mc_retries, 0xc),
411 CCN_EVENT_HNF(mc_reqs, 0xd),
412 CCN_EVENT_HNF(qos_hh_retry, 0xe),
413 CCN_EVENT_RNI(rdata_beats_p0, 0x1),
414 CCN_EVENT_RNI(rdata_beats_p1, 0x2),
415 CCN_EVENT_RNI(rdata_beats_p2, 0x3),
416 CCN_EVENT_RNI(rxdat_flits, 0x4),
417 CCN_EVENT_RNI(txdat_flits, 0x5),
418 CCN_EVENT_RNI(txreq_flits, 0x6),
419 CCN_EVENT_RNI(txreq_flits_retried, 0x7),
420 CCN_EVENT_RNI(rrt_full, 0x8),
421 CCN_EVENT_RNI(wrt_full, 0x9),
422 CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
423 CCN_EVENT_XP(upload_starvation, 0x1),
424 CCN_EVENT_XP(download_starvation, 0x2),
425 CCN_EVENT_XP(respin, 0x3),
426 CCN_EVENT_XP(valid_flit, 0x4),
427 CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
428 CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
429 CCN_EVENT_SBAS(rxdat_flits, 0x4),
430 CCN_EVENT_SBAS(txdat_flits, 0x5),
431 CCN_EVENT_SBAS(txreq_flits, 0x6),
432 CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
433 CCN_EVENT_SBAS(rrt_full, 0x8),
434 CCN_EVENT_SBAS(wrt_full, 0x9),
435 CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
436 CCN_EVENT_CYCLES(cycles),
439 /* Populated in arm_ccn_init() */
440 static struct attribute
441 *arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
443 static const struct attribute_group arm_ccn_pmu_events_attr_group = {
445 .is_visible = arm_ccn_pmu_events_is_visible,
446 .attrs = arm_ccn_pmu_events_attrs,
450 static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
454 if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
456 i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
460 return &ccn->dt.cmp_mask[i].l;
462 return &ccn->dt.cmp_mask[i].h;
468 static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
472 u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
474 return mask ? sysfs_emit(buf, "0x%016llx\n", *mask) : -EINVAL;
477 static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
478 struct device_attribute *attr, const char *buf, size_t count)
480 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
481 u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
485 err = kstrtoull(buf, 0, mask);
487 return err ? err : count;
490 #define CCN_CMP_MASK_ATTR(_name) \
491 struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
492 __ATTR(_name, S_IRUGO | S_IWUSR, \
493 arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
495 #define CCN_CMP_MASK_ATTR_RO(_name) \
496 struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
497 __ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
499 static CCN_CMP_MASK_ATTR(0l);
500 static CCN_CMP_MASK_ATTR(0h);
501 static CCN_CMP_MASK_ATTR(1l);
502 static CCN_CMP_MASK_ATTR(1h);
503 static CCN_CMP_MASK_ATTR(2l);
504 static CCN_CMP_MASK_ATTR(2h);
505 static CCN_CMP_MASK_ATTR(3l);
506 static CCN_CMP_MASK_ATTR(3h);
507 static CCN_CMP_MASK_ATTR(4l);
508 static CCN_CMP_MASK_ATTR(4h);
509 static CCN_CMP_MASK_ATTR(5l);
510 static CCN_CMP_MASK_ATTR(5h);
511 static CCN_CMP_MASK_ATTR(6l);
512 static CCN_CMP_MASK_ATTR(6h);
513 static CCN_CMP_MASK_ATTR(7l);
514 static CCN_CMP_MASK_ATTR(7h);
515 static CCN_CMP_MASK_ATTR_RO(8l);
516 static CCN_CMP_MASK_ATTR_RO(8h);
517 static CCN_CMP_MASK_ATTR_RO(9l);
518 static CCN_CMP_MASK_ATTR_RO(9h);
519 static CCN_CMP_MASK_ATTR_RO(al);
520 static CCN_CMP_MASK_ATTR_RO(ah);
521 static CCN_CMP_MASK_ATTR_RO(bl);
522 static CCN_CMP_MASK_ATTR_RO(bh);
524 static struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
525 &arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
526 &arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
527 &arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
528 &arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
529 &arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
530 &arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
531 &arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
532 &arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
533 &arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
534 &arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
535 &arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
536 &arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
540 static const struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
542 .attrs = arm_ccn_pmu_cmp_mask_attrs,
545 static ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
548 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
550 return cpumap_print_to_pagebuf(true, buf, cpumask_of(ccn->dt.cpu));
553 static struct device_attribute arm_ccn_pmu_cpumask_attr =
554 __ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
556 static struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
557 &arm_ccn_pmu_cpumask_attr.attr,
561 static const struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
562 .attrs = arm_ccn_pmu_cpumask_attrs,
566 * Default poll period is 10ms, which is way over the top anyway,
567 * as in the worst case scenario (an event every cycle), with 1GHz
568 * clocked bus, the smallest, 32 bit counter will overflow in
571 static unsigned int arm_ccn_pmu_poll_period_us = 10000;
572 module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
575 static ktime_t arm_ccn_pmu_timer_period(void)
577 return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
581 static const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
582 &arm_ccn_pmu_events_attr_group,
583 &arm_ccn_pmu_format_attr_group,
584 &arm_ccn_pmu_cmp_mask_attr_group,
585 &arm_ccn_pmu_cpumask_attr_group,
590 static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
595 bit = find_first_zero_bit(bitmap, size);
598 } while (test_and_set_bit(bit, bitmap));
603 /* All RN-I and RN-D nodes have identical PMUs */
604 static int arm_ccn_pmu_type_eq(u32 a, u32 b)
610 case CCN_TYPE_RNI_1P:
611 case CCN_TYPE_RNI_2P:
612 case CCN_TYPE_RNI_3P:
613 case CCN_TYPE_RND_1P:
614 case CCN_TYPE_RND_2P:
615 case CCN_TYPE_RND_3P:
617 case CCN_TYPE_RNI_1P:
618 case CCN_TYPE_RNI_2P:
619 case CCN_TYPE_RNI_3P:
620 case CCN_TYPE_RND_1P:
621 case CCN_TYPE_RND_2P:
622 case CCN_TYPE_RND_3P:
631 static int arm_ccn_pmu_event_alloc(struct perf_event *event)
633 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
634 struct hw_perf_event *hw = &event->hw;
635 u32 node_xp, type, event_id;
636 struct arm_ccn_component *source;
639 node_xp = CCN_CONFIG_NODE(event->attr.config);
640 type = CCN_CONFIG_TYPE(event->attr.config);
641 event_id = CCN_CONFIG_EVENT(event->attr.config);
643 /* Allocate the cycle counter */
644 if (type == CCN_TYPE_CYCLES) {
645 if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
646 ccn->dt.pmu_counters_mask))
649 hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
650 ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
655 /* Allocate an event counter */
656 hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
657 CCN_NUM_PMU_EVENT_COUNTERS);
659 dev_dbg(ccn->dev, "No more counters available!\n");
663 if (type == CCN_TYPE_XP)
664 source = &ccn->xp[node_xp];
666 source = &ccn->node[node_xp];
667 ccn->dt.pmu_counters[hw->idx].source = source;
669 /* Allocate an event source or a watchpoint */
670 if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
671 bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
672 CCN_NUM_XP_WATCHPOINTS);
674 bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
677 dev_dbg(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
679 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
682 hw->config_base = bit;
684 ccn->dt.pmu_counters[hw->idx].event = event;
689 static void arm_ccn_pmu_event_release(struct perf_event *event)
691 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
692 struct hw_perf_event *hw = &event->hw;
694 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
695 clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
697 struct arm_ccn_component *source =
698 ccn->dt.pmu_counters[hw->idx].source;
700 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
701 CCN_CONFIG_EVENT(event->attr.config) ==
702 CCN_EVENT_WATCHPOINT)
703 clear_bit(hw->config_base, source->xp.dt_cmp_mask);
705 clear_bit(hw->config_base, source->pmu_events_mask);
706 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
709 ccn->dt.pmu_counters[hw->idx].source = NULL;
710 ccn->dt.pmu_counters[hw->idx].event = NULL;
713 static int arm_ccn_pmu_event_init(struct perf_event *event)
716 struct hw_perf_event *hw = &event->hw;
717 u32 node_xp, type, event_id;
720 struct perf_event *sibling;
722 if (event->attr.type != event->pmu->type)
725 ccn = pmu_to_arm_ccn(event->pmu);
727 if (hw->sample_period) {
728 dev_dbg(ccn->dev, "Sampling not supported!\n");
732 if (has_branch_stack(event)) {
733 dev_dbg(ccn->dev, "Can't exclude execution levels!\n");
737 if (event->cpu < 0) {
738 dev_dbg(ccn->dev, "Can't provide per-task data!\n");
742 * Many perf core operations (eg. events rotation) operate on a
743 * single CPU context. This is obvious for CPU PMUs, where one
744 * expects the same sets of events being observed on all CPUs,
745 * but can lead to issues for off-core PMUs, like CCN, where each
746 * event could be theoretically assigned to a different CPU. To
747 * mitigate this, we enforce CPU assignment to one, selected
748 * processor (the one described in the "cpumask" attribute).
750 event->cpu = ccn->dt.cpu;
752 node_xp = CCN_CONFIG_NODE(event->attr.config);
753 type = CCN_CONFIG_TYPE(event->attr.config);
754 event_id = CCN_CONFIG_EVENT(event->attr.config);
756 /* Validate node/xp vs topology */
759 if (node_xp != ccn->mn_id) {
760 dev_dbg(ccn->dev, "Invalid MN ID %d!\n", node_xp);
765 if (node_xp >= ccn->num_xps) {
766 dev_dbg(ccn->dev, "Invalid XP ID %d!\n", node_xp);
770 case CCN_TYPE_CYCLES:
773 if (node_xp >= ccn->num_nodes) {
774 dev_dbg(ccn->dev, "Invalid node ID %d!\n", node_xp);
777 if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
778 dev_dbg(ccn->dev, "Invalid type 0x%x for node %d!\n",
785 /* Validate event ID vs available for the type */
786 for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
788 struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
789 u32 port = CCN_CONFIG_PORT(event->attr.config);
790 u32 vc = CCN_CONFIG_VC(event->attr.config);
792 if (!arm_ccn_pmu_type_eq(type, e->type))
794 if (event_id != e->event)
796 if (e->num_ports && port >= e->num_ports) {
797 dev_dbg(ccn->dev, "Invalid port %d for node/XP %d!\n",
801 if (e->num_vcs && vc >= e->num_vcs) {
802 dev_dbg(ccn->dev, "Invalid vc %d for node/XP %d!\n",
809 dev_dbg(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
814 /* Watchpoint-based event for a node is actually set on XP */
815 if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
819 port = arm_ccn_node_to_xp_port(node_xp);
820 node_xp = arm_ccn_node_to_xp(node_xp);
822 arm_ccn_pmu_config_set(&event->attr.config,
823 node_xp, type, port);
827 * We must NOT create groups containing mixed PMUs, although software
828 * events are acceptable (for example to create a CCN group
829 * periodically read when a hrtimer aka cpu-clock leader triggers).
831 if (event->group_leader->pmu != event->pmu &&
832 !is_software_event(event->group_leader))
835 for_each_sibling_event(sibling, event->group_leader) {
836 if (sibling->pmu != event->pmu &&
837 !is_software_event(sibling))
844 static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
848 if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
850 res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
852 /* 40 bit counter, can do snapshot and read in two parts */
853 writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
854 while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
856 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
857 res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
859 res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
862 res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
868 static void arm_ccn_pmu_event_update(struct perf_event *event)
870 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
871 struct hw_perf_event *hw = &event->hw;
872 u64 prev_count, new_count, mask;
875 prev_count = local64_read(&hw->prev_count);
876 new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
877 } while (local64_xchg(&hw->prev_count, new_count) != prev_count);
879 mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
881 local64_add((new_count - prev_count) & mask, &event->count);
884 static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
886 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
887 struct hw_perf_event *hw = &event->hw;
888 struct arm_ccn_component *xp;
891 /* Nothing to do for cycle counter */
892 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
895 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
896 xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
898 xp = &ccn->xp[arm_ccn_node_to_xp(
899 CCN_CONFIG_NODE(event->attr.config))];
902 dt_cfg = hw->event_base;
904 dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
906 spin_lock(&ccn->dt.config_lock);
908 val = readl(xp->base + CCN_XP_DT_CONFIG);
909 val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
910 CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
911 val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
912 writel(val, xp->base + CCN_XP_DT_CONFIG);
914 spin_unlock(&ccn->dt.config_lock);
917 static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
919 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
920 struct hw_perf_event *hw = &event->hw;
922 local64_set(&event->hw.prev_count,
923 arm_ccn_pmu_read_counter(ccn, hw->idx));
926 /* Set the DT bus input, engaging the counter */
927 arm_ccn_pmu_xp_dt_config(event, 1);
930 static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
932 struct hw_perf_event *hw = &event->hw;
934 /* Disable counting, setting the DT bus to pass-through mode */
935 arm_ccn_pmu_xp_dt_config(event, 0);
937 if (flags & PERF_EF_UPDATE)
938 arm_ccn_pmu_event_update(event);
940 hw->state |= PERF_HES_STOPPED;
943 static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
945 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
946 struct hw_perf_event *hw = &event->hw;
947 struct arm_ccn_component *source =
948 ccn->dt.pmu_counters[hw->idx].source;
949 unsigned long wp = hw->config_base;
951 u64 cmp_l = event->attr.config1;
952 u64 cmp_h = event->attr.config2;
953 u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
954 u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
956 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
958 /* Direction (RX/TX), device (port) & virtual channel */
959 val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
960 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
961 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
962 val |= CCN_CONFIG_DIR(event->attr.config) <<
963 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
964 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
965 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
966 val |= CCN_CONFIG_PORT(event->attr.config) <<
967 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
968 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
969 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
970 val |= CCN_CONFIG_VC(event->attr.config) <<
971 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
972 writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
974 /* Comparison values */
975 writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
976 writel((cmp_l >> 32) & 0x7fffffff,
977 source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
978 writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
979 writel((cmp_h >> 32) & 0x0fffffff,
980 source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
983 writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
984 writel((mask_l >> 32) & 0x7fffffff,
985 source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
986 writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
987 writel((mask_h >> 32) & 0x0fffffff,
988 source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
991 static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
993 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
994 struct hw_perf_event *hw = &event->hw;
995 struct arm_ccn_component *source =
996 ccn->dt.pmu_counters[hw->idx].source;
999 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
1001 id = (CCN_CONFIG_VC(event->attr.config) << 4) |
1002 (CCN_CONFIG_BUS(event->attr.config) << 3) |
1003 (CCN_CONFIG_EVENT(event->attr.config) << 0);
1005 val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
1006 val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
1007 CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
1008 val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
1009 writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
1012 static void arm_ccn_pmu_node_event_config(struct perf_event *event)
1014 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1015 struct hw_perf_event *hw = &event->hw;
1016 struct arm_ccn_component *source =
1017 ccn->dt.pmu_counters[hw->idx].source;
1018 u32 type = CCN_CONFIG_TYPE(event->attr.config);
1021 port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
1022 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
1025 /* These *_event_sel regs should be identical, but let's make sure... */
1026 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
1027 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
1028 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
1029 CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
1030 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
1031 CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
1032 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
1033 CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
1034 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
1035 CCN_RNI_PMU_EVENT_SEL__ID__MASK);
1036 if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
1037 !arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
1040 /* Set the event id for the pre-allocated counter */
1041 val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
1042 val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
1043 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
1044 val |= CCN_CONFIG_EVENT(event->attr.config) <<
1045 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
1046 writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
1049 static void arm_ccn_pmu_event_config(struct perf_event *event)
1051 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1052 struct hw_perf_event *hw = &event->hw;
1053 u32 xp, offset, val;
1055 /* Cycle counter requires no setup */
1056 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
1059 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
1060 xp = CCN_CONFIG_XP(event->attr.config);
1062 xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
1064 spin_lock(&ccn->dt.config_lock);
1066 /* Set the DT bus "distance" register */
1067 offset = (hw->idx / 4) * 4;
1068 val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1069 val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
1070 CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
1071 val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
1072 writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1074 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
1075 if (CCN_CONFIG_EVENT(event->attr.config) ==
1076 CCN_EVENT_WATCHPOINT)
1077 arm_ccn_pmu_xp_watchpoint_config(event);
1079 arm_ccn_pmu_xp_event_config(event);
1081 arm_ccn_pmu_node_event_config(event);
1084 spin_unlock(&ccn->dt.config_lock);
1087 static int arm_ccn_pmu_active_counters(struct arm_ccn *ccn)
1089 return bitmap_weight(ccn->dt.pmu_counters_mask,
1090 CCN_NUM_PMU_EVENT_COUNTERS + 1);
1093 static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
1096 struct hw_perf_event *hw = &event->hw;
1097 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1099 err = arm_ccn_pmu_event_alloc(event);
1104 * Pin the timer, so that the overflows are handled by the chosen
1105 * event->cpu (this is the same one as presented in "cpumask"
1108 if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 1)
1109 hrtimer_start(&ccn->dt.hrtimer, arm_ccn_pmu_timer_period(),
1110 HRTIMER_MODE_REL_PINNED);
1112 arm_ccn_pmu_event_config(event);
1114 hw->state = PERF_HES_STOPPED;
1116 if (flags & PERF_EF_START)
1117 arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
1122 static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
1124 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1126 arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
1128 arm_ccn_pmu_event_release(event);
1130 if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 0)
1131 hrtimer_cancel(&ccn->dt.hrtimer);
1134 static void arm_ccn_pmu_event_read(struct perf_event *event)
1136 arm_ccn_pmu_event_update(event);
1139 static void arm_ccn_pmu_enable(struct pmu *pmu)
1141 struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
1143 u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
1144 val |= CCN_DT_PMCR__PMU_EN;
1145 writel(val, ccn->dt.base + CCN_DT_PMCR);
1148 static void arm_ccn_pmu_disable(struct pmu *pmu)
1150 struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
1152 u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
1153 val &= ~CCN_DT_PMCR__PMU_EN;
1154 writel(val, ccn->dt.base + CCN_DT_PMCR);
1157 static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
1159 u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
1165 writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
1167 BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
1169 for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
1170 struct perf_event *event = dt->pmu_counters[idx].event;
1171 int overflowed = pmovsr & BIT(idx);
1173 WARN_ON_ONCE(overflowed && !event &&
1174 idx != CCN_IDX_PMU_CYCLE_COUNTER);
1176 if (!event || !overflowed)
1179 arm_ccn_pmu_event_update(event);
1185 static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
1187 struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
1189 unsigned long flags;
1191 local_irq_save(flags);
1192 arm_ccn_pmu_overflow_handler(dt);
1193 local_irq_restore(flags);
1195 hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
1196 return HRTIMER_RESTART;
1200 static int arm_ccn_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
1202 struct arm_ccn_dt *dt = hlist_entry_safe(node, struct arm_ccn_dt, node);
1203 struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
1204 unsigned int target;
1208 target = cpumask_any_but(cpu_online_mask, cpu);
1209 if (target >= nr_cpu_ids)
1211 perf_pmu_migrate_context(&dt->pmu, cpu, target);
1214 WARN_ON(irq_set_affinity(ccn->irq, cpumask_of(dt->cpu)));
1218 static DEFINE_IDA(arm_ccn_pmu_ida);
1220 static int arm_ccn_pmu_init(struct arm_ccn *ccn)
1226 /* Initialize DT subsystem */
1227 ccn->dt.base = ccn->base + CCN_REGION_SIZE;
1228 spin_lock_init(&ccn->dt.config_lock);
1229 writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
1230 writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
1231 writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
1232 ccn->dt.base + CCN_DT_PMCR);
1233 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
1234 for (i = 0; i < ccn->num_xps; i++) {
1235 writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
1236 writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1237 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
1238 (CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1239 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
1240 CCN_XP_DT_CONTROL__DT_ENABLE,
1241 ccn->xp[i].base + CCN_XP_DT_CONTROL);
1243 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
1244 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
1245 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
1246 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
1247 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
1248 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
1249 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
1250 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
1252 /* Get a convenient /sys/event_source/devices/ name */
1253 ccn->dt.id = ida_alloc(&arm_ccn_pmu_ida, GFP_KERNEL);
1254 if (ccn->dt.id == 0) {
1257 name = devm_kasprintf(ccn->dev, GFP_KERNEL, "ccn_%d",
1261 goto error_choose_name;
1265 /* Perf driver registration */
1266 ccn->dt.pmu = (struct pmu) {
1267 .module = THIS_MODULE,
1268 .attr_groups = arm_ccn_pmu_attr_groups,
1269 .task_ctx_nr = perf_invalid_context,
1270 .event_init = arm_ccn_pmu_event_init,
1271 .add = arm_ccn_pmu_event_add,
1272 .del = arm_ccn_pmu_event_del,
1273 .start = arm_ccn_pmu_event_start,
1274 .stop = arm_ccn_pmu_event_stop,
1275 .read = arm_ccn_pmu_event_read,
1276 .pmu_enable = arm_ccn_pmu_enable,
1277 .pmu_disable = arm_ccn_pmu_disable,
1278 .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
1281 /* No overflow interrupt? Have to use a timer instead. */
1283 dev_info(ccn->dev, "No access to interrupts, using timer.\n");
1284 hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
1286 ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
1289 /* Pick one CPU which we will use to collect data from CCN... */
1290 ccn->dt.cpu = raw_smp_processor_id();
1292 /* Also make sure that the overflow interrupt is handled by this CPU */
1294 err = irq_set_affinity(ccn->irq, cpumask_of(ccn->dt.cpu));
1296 dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
1297 goto error_set_affinity;
1301 cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
1304 err = perf_pmu_register(&ccn->dt.pmu, name, -1);
1306 goto error_pmu_register;
1311 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
1315 ida_free(&arm_ccn_pmu_ida, ccn->dt.id);
1316 for (i = 0; i < ccn->num_xps; i++)
1317 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1318 writel(0, ccn->dt.base + CCN_DT_PMCR);
1322 static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
1326 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
1328 for (i = 0; i < ccn->num_xps; i++)
1329 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1330 writel(0, ccn->dt.base + CCN_DT_PMCR);
1331 perf_pmu_unregister(&ccn->dt.pmu);
1332 ida_free(&arm_ccn_pmu_ida, ccn->dt.id);
1335 static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
1336 int (*callback)(struct arm_ccn *ccn, int region,
1337 void __iomem *base, u32 type, u32 id))
1341 for (region = 0; region < CCN_NUM_REGIONS; region++) {
1346 val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
1348 if (!(val & (1 << (region % 32))))
1351 base = ccn->base + region * CCN_REGION_SIZE;
1352 val = readl(base + CCN_ALL_OLY_ID);
1353 type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
1354 CCN_ALL_OLY_ID__OLY_ID__MASK;
1355 id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
1356 CCN_ALL_OLY_ID__NODE_ID__MASK;
1358 err = callback(ccn, region, base, type, id);
1366 static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
1367 void __iomem *base, u32 type, u32 id)
1370 if (type == CCN_TYPE_XP && id >= ccn->num_xps)
1371 ccn->num_xps = id + 1;
1372 else if (id >= ccn->num_nodes)
1373 ccn->num_nodes = id + 1;
1378 static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
1379 void __iomem *base, u32 type, u32 id)
1381 struct arm_ccn_component *component;
1383 dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
1392 component = &ccn->xp[id];
1395 ccn->sbsx_present = 1;
1396 component = &ccn->node[id];
1399 ccn->sbas_present = 1;
1402 component = &ccn->node[id];
1406 component->base = base;
1407 component->type = type;
1413 static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
1414 const u32 *err_sig_val)
1416 /* This should be really handled by firmware... */
1417 dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
1418 err_sig_val[5], err_sig_val[4], err_sig_val[3],
1419 err_sig_val[2], err_sig_val[1], err_sig_val[0]);
1420 dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
1421 writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
1422 ccn->base + CCN_MN_ERRINT_STATUS);
1428 static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
1430 irqreturn_t res = IRQ_NONE;
1431 struct arm_ccn *ccn = dev_id;
1436 /* PMU overflow is a special case */
1437 err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
1438 if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
1439 err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
1440 res = arm_ccn_pmu_overflow_handler(&ccn->dt);
1443 /* Have to read all err_sig_vals to clear them */
1444 for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
1445 err_sig_val[i] = readl(ccn->base +
1446 CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
1447 err_or |= err_sig_val[i];
1450 res |= arm_ccn_error_handler(ccn, err_sig_val);
1452 if (res != IRQ_NONE)
1453 writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
1454 ccn->base + CCN_MN_ERRINT_STATUS);
1460 static int arm_ccn_probe(struct platform_device *pdev)
1462 struct arm_ccn *ccn;
1466 ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
1469 ccn->dev = &pdev->dev;
1470 platform_set_drvdata(pdev, ccn);
1472 ccn->base = devm_platform_ioremap_resource(pdev, 0);
1473 if (IS_ERR(ccn->base))
1474 return PTR_ERR(ccn->base);
1476 irq = platform_get_irq(pdev, 0);
1480 /* Check if we can use the interrupt */
1481 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
1482 ccn->base + CCN_MN_ERRINT_STATUS);
1483 if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
1484 CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
1485 /* Can set 'disable' bits, so can acknowledge interrupts */
1486 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
1487 ccn->base + CCN_MN_ERRINT_STATUS);
1488 err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler,
1489 IRQF_NOBALANCING | IRQF_NO_THREAD,
1490 dev_name(ccn->dev), ccn);
1498 /* Build topology */
1500 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
1504 ccn->node = devm_kcalloc(ccn->dev, ccn->num_nodes, sizeof(*ccn->node),
1506 ccn->xp = devm_kcalloc(ccn->dev, ccn->num_xps, sizeof(*ccn->node),
1508 if (!ccn->node || !ccn->xp)
1511 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
1515 return arm_ccn_pmu_init(ccn);
1518 static int arm_ccn_remove(struct platform_device *pdev)
1520 struct arm_ccn *ccn = platform_get_drvdata(pdev);
1522 arm_ccn_pmu_cleanup(ccn);
1527 static const struct of_device_id arm_ccn_match[] = {
1528 { .compatible = "arm,ccn-502", },
1529 { .compatible = "arm,ccn-504", },
1530 { .compatible = "arm,ccn-512", },
1533 MODULE_DEVICE_TABLE(of, arm_ccn_match);
1535 static struct platform_driver arm_ccn_driver = {
1538 .of_match_table = arm_ccn_match,
1539 .suppress_bind_attrs = true,
1541 .probe = arm_ccn_probe,
1542 .remove = arm_ccn_remove,
1545 static int __init arm_ccn_init(void)
1549 ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE,
1550 "perf/arm/ccn:online", NULL,
1551 arm_ccn_pmu_offline_cpu);
1555 for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
1556 arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
1558 ret = platform_driver_register(&arm_ccn_driver);
1560 cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
1564 static void __exit arm_ccn_exit(void)
1566 platform_driver_unregister(&arm_ccn_driver);
1567 cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
1570 module_init(arm_ccn_init);
1571 module_exit(arm_ccn_exit);
1573 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
1574 MODULE_LICENSE("GPL v2");