blob: f5bca3113bfc74f758ab0d45071c99e079048de7 [file] [log] [blame]
Pawel Molla33b0da2014-07-22 18:32:59 +01001/*
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.
5 *
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.
10 *
11 * Copyright (C) 2014 ARM Limited
12 */
13
14#include <linux/ctype.h>
15#include <linux/hrtimer.h>
16#include <linux/idr.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/module.h>
20#include <linux/perf_event.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23
24#define CCN_NUM_XP_PORTS 2
25#define CCN_NUM_VCS 4
26#define CCN_NUM_REGIONS 256
27#define CCN_REGION_SIZE 0x10000
28
29#define CCN_ALL_OLY_ID 0xff00
30#define CCN_ALL_OLY_ID__OLY_ID__SHIFT 0
31#define CCN_ALL_OLY_ID__OLY_ID__MASK 0x1f
32#define CCN_ALL_OLY_ID__NODE_ID__SHIFT 8
33#define CCN_ALL_OLY_ID__NODE_ID__MASK 0x3f
34
35#define CCN_MN_ERRINT_STATUS 0x0008
36#define CCN_MN_ERRINT_STATUS__INTREQ__DESSERT 0x11
37#define CCN_MN_ERRINT_STATUS__ALL_ERRORS__ENABLE 0x02
38#define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLED 0x20
39#define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE 0x22
40#define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_ENABLE 0x04
41#define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLED 0x40
42#define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLE 0x44
43#define CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE 0x08
44#define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED 0x80
45#define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE 0x88
46#define CCN_MN_OLY_COMP_LIST_63_0 0x01e0
47#define CCN_MN_ERR_SIG_VAL_63_0 0x0300
48#define CCN_MN_ERR_SIG_VAL_63_0__DT (1 << 1)
49
50#define CCN_DT_ACTIVE_DSM 0x0000
51#define CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(n) ((n) * 8)
52#define CCN_DT_ACTIVE_DSM__DSM_ID__MASK 0xff
53#define CCN_DT_CTL 0x0028
54#define CCN_DT_CTL__DT_EN (1 << 0)
55#define CCN_DT_PMEVCNT(n) (0x0100 + (n) * 0x8)
56#define CCN_DT_PMCCNTR 0x0140
57#define CCN_DT_PMCCNTRSR 0x0190
58#define CCN_DT_PMOVSR 0x0198
59#define CCN_DT_PMOVSR_CLR 0x01a0
Pawel Mollfa637bf2014-09-15 15:33:48 +010060#define CCN_DT_PMOVSR_CLR__MASK 0x1f
Pawel Molla33b0da2014-07-22 18:32:59 +010061#define CCN_DT_PMCR 0x01a8
62#define CCN_DT_PMCR__OVFL_INTR_EN (1 << 6)
63#define CCN_DT_PMCR__PMU_EN (1 << 0)
64#define CCN_DT_PMSR 0x01b0
65#define CCN_DT_PMSR_REQ 0x01b8
66#define CCN_DT_PMSR_CLR 0x01c0
67
68#define CCN_HNF_PMU_EVENT_SEL 0x0600
69#define CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
70#define CCN_HNF_PMU_EVENT_SEL__ID__MASK 0xf
71
72#define CCN_XP_DT_CONFIG 0x0300
73#define CCN_XP_DT_CONFIG__DT_CFG__SHIFT(n) ((n) * 4)
74#define CCN_XP_DT_CONFIG__DT_CFG__MASK 0xf
75#define CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH 0x0
76#define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT_0_OR_1 0x1
77#define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(n) (0x2 + (n))
78#define CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(n) (0x4 + (n))
79#define CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(d, n) (0x8 + (d) * 4 + (n))
80#define CCN_XP_DT_INTERFACE_SEL 0x0308
81#define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(n) (0 + (n) * 8)
82#define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK 0x1
83#define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(n) (1 + (n) * 8)
84#define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK 0x1
85#define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(n) (2 + (n) * 8)
86#define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK 0x3
87#define CCN_XP_DT_CMP_VAL_L(n) (0x0310 + (n) * 0x40)
88#define CCN_XP_DT_CMP_VAL_H(n) (0x0318 + (n) * 0x40)
89#define CCN_XP_DT_CMP_MASK_L(n) (0x0320 + (n) * 0x40)
90#define CCN_XP_DT_CMP_MASK_H(n) (0x0328 + (n) * 0x40)
91#define CCN_XP_DT_CONTROL 0x0370
92#define CCN_XP_DT_CONTROL__DT_ENABLE (1 << 0)
93#define CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(n) (12 + (n) * 4)
94#define CCN_XP_DT_CONTROL__WP_ARM_SEL__MASK 0xf
95#define CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS 0xf
96#define CCN_XP_PMU_EVENT_SEL 0x0600
97#define CCN_XP_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 7)
98#define CCN_XP_PMU_EVENT_SEL__ID__MASK 0x3f
99
100#define CCN_SBAS_PMU_EVENT_SEL 0x0600
101#define CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
102#define CCN_SBAS_PMU_EVENT_SEL__ID__MASK 0xf
103
104#define CCN_RNI_PMU_EVENT_SEL 0x0600
105#define CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
106#define CCN_RNI_PMU_EVENT_SEL__ID__MASK 0xf
107
108#define CCN_TYPE_MN 0x01
109#define CCN_TYPE_DT 0x02
110#define CCN_TYPE_HNF 0x04
111#define CCN_TYPE_HNI 0x05
112#define CCN_TYPE_XP 0x08
113#define CCN_TYPE_SBSX 0x0c
114#define CCN_TYPE_SBAS 0x10
115#define CCN_TYPE_RNI_1P 0x14
116#define CCN_TYPE_RNI_2P 0x15
117#define CCN_TYPE_RNI_3P 0x16
118#define CCN_TYPE_RND_1P 0x18 /* RN-D = RN-I + DVM */
119#define CCN_TYPE_RND_2P 0x19
120#define CCN_TYPE_RND_3P 0x1a
121#define CCN_TYPE_CYCLES 0xff /* Pseudotype */
122
123#define CCN_EVENT_WATCHPOINT 0xfe /* Pseudoevent */
124
125#define CCN_NUM_PMU_EVENTS 4
126#define CCN_NUM_XP_WATCHPOINTS 2 /* See DT.dbg_id.num_watchpoints */
127#define CCN_NUM_PMU_EVENT_COUNTERS 8 /* See DT.dbg_id.num_pmucntr */
128#define CCN_IDX_PMU_CYCLE_COUNTER CCN_NUM_PMU_EVENT_COUNTERS
129
130#define CCN_NUM_PREDEFINED_MASKS 4
131#define CCN_IDX_MASK_ANY (CCN_NUM_PMU_EVENT_COUNTERS + 0)
132#define CCN_IDX_MASK_EXACT (CCN_NUM_PMU_EVENT_COUNTERS + 1)
133#define CCN_IDX_MASK_ORDER (CCN_NUM_PMU_EVENT_COUNTERS + 2)
134#define CCN_IDX_MASK_OPCODE (CCN_NUM_PMU_EVENT_COUNTERS + 3)
135
136struct arm_ccn_component {
137 void __iomem *base;
138 u32 type;
139
140 DECLARE_BITMAP(pmu_events_mask, CCN_NUM_PMU_EVENTS);
141 union {
142 struct {
143 DECLARE_BITMAP(dt_cmp_mask, CCN_NUM_XP_WATCHPOINTS);
144 } xp;
145 };
146};
147
148#define pmu_to_arm_ccn(_pmu) container_of(container_of(_pmu, \
149 struct arm_ccn_dt, pmu), struct arm_ccn, dt)
150
151struct arm_ccn_dt {
152 int id;
153 void __iomem *base;
154
155 spinlock_t config_lock;
156
157 DECLARE_BITMAP(pmu_counters_mask, CCN_NUM_PMU_EVENT_COUNTERS + 1);
158 struct {
159 struct arm_ccn_component *source;
160 struct perf_event *event;
161 } pmu_counters[CCN_NUM_PMU_EVENT_COUNTERS + 1];
162
163 struct {
164 u64 l, h;
165 } cmp_mask[CCN_NUM_PMU_EVENT_COUNTERS + CCN_NUM_PREDEFINED_MASKS];
166
167 struct hrtimer hrtimer;
168
169 struct pmu pmu;
170};
171
172struct arm_ccn {
173 struct device *dev;
174 void __iomem *base;
175 unsigned irq_used:1;
176 unsigned sbas_present:1;
177 unsigned sbsx_present:1;
178
179 int num_nodes;
180 struct arm_ccn_component *node;
181
182 int num_xps;
183 struct arm_ccn_component *xp;
184
185 struct arm_ccn_dt dt;
186};
187
188
189static int arm_ccn_node_to_xp(int node)
190{
191 return node / CCN_NUM_XP_PORTS;
192}
193
194static int arm_ccn_node_to_xp_port(int node)
195{
196 return node % CCN_NUM_XP_PORTS;
197}
198
199
200/*
201 * Bit shifts and masks in these defines must be kept in sync with
202 * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
203 */
204#define CCN_CONFIG_NODE(_config) (((_config) >> 0) & 0xff)
205#define CCN_CONFIG_XP(_config) (((_config) >> 0) & 0xff)
206#define CCN_CONFIG_TYPE(_config) (((_config) >> 8) & 0xff)
207#define CCN_CONFIG_EVENT(_config) (((_config) >> 16) & 0xff)
208#define CCN_CONFIG_PORT(_config) (((_config) >> 24) & 0x3)
209#define CCN_CONFIG_VC(_config) (((_config) >> 26) & 0x7)
210#define CCN_CONFIG_DIR(_config) (((_config) >> 29) & 0x1)
211#define CCN_CONFIG_MASK(_config) (((_config) >> 30) & 0xf)
212
213static void arm_ccn_pmu_config_set(u64 *config, u32 node_xp, u32 type, u32 port)
214{
215 *config &= ~((0xff << 0) | (0xff << 8) | (0xff << 24));
216 *config |= (node_xp << 0) | (type << 8) | (port << 24);
217}
218
219static ssize_t arm_ccn_pmu_format_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 struct dev_ext_attribute *ea = container_of(attr,
223 struct dev_ext_attribute, attr);
224
225 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)ea->var);
226}
227
228#define CCN_FORMAT_ATTR(_name, _config) \
229 struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
230 { __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
231 NULL), _config }
232
233static CCN_FORMAT_ATTR(node, "config:0-7");
234static CCN_FORMAT_ATTR(xp, "config:0-7");
235static CCN_FORMAT_ATTR(type, "config:8-15");
236static CCN_FORMAT_ATTR(event, "config:16-23");
237static CCN_FORMAT_ATTR(port, "config:24-25");
238static CCN_FORMAT_ATTR(vc, "config:26-28");
239static CCN_FORMAT_ATTR(dir, "config:29-29");
240static CCN_FORMAT_ATTR(mask, "config:30-33");
241static CCN_FORMAT_ATTR(cmp_l, "config1:0-62");
242static CCN_FORMAT_ATTR(cmp_h, "config2:0-59");
243
244static 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_vc.attr.attr,
251 &arm_ccn_pmu_format_attr_dir.attr.attr,
252 &arm_ccn_pmu_format_attr_mask.attr.attr,
253 &arm_ccn_pmu_format_attr_cmp_l.attr.attr,
254 &arm_ccn_pmu_format_attr_cmp_h.attr.attr,
255 NULL
256};
257
258static struct attribute_group arm_ccn_pmu_format_attr_group = {
259 .name = "format",
260 .attrs = arm_ccn_pmu_format_attrs,
261};
262
263
264struct arm_ccn_pmu_event {
265 struct device_attribute attr;
266 u32 type;
267 u32 event;
268 int num_ports;
269 int num_vcs;
270 const char *def;
271 int mask;
272};
273
274#define CCN_EVENT_ATTR(_name) \
275 __ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
276
277/*
278 * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
279 * their ports in XP they are connected to. For the sake of usability they are
280 * explicitly defined here (and translated into a relevant watchpoint in
281 * arm_ccn_pmu_event_init()) so the user can easily request them without deep
282 * knowledge of the flit format.
283 */
284
285#define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
286 .type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
287 .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
288 .def = _def, .mask = _mask, }
289
290#define CCN_EVENT_HNI(_name, _def, _mask) { \
291 .attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
292 .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
293 .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
294
295#define CCN_EVENT_SBSX(_name, _def, _mask) { \
296 .attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
297 .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
298 .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
299
300#define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
301 .type = CCN_TYPE_HNF, .event = _event, }
302
303#define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
304 .type = CCN_TYPE_XP, .event = _event, \
305 .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
306
307/*
308 * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
309 * on configuration. One of them is picked to represent the whole group,
310 * as they all share the same event types.
311 */
312#define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
313 .type = CCN_TYPE_RNI_3P, .event = _event, }
314
315#define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
316 .type = CCN_TYPE_SBAS, .event = _event, }
317
318#define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
319 .type = CCN_TYPE_CYCLES }
320
321
322static ssize_t arm_ccn_pmu_event_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 struct arm_ccn_pmu_event *event = container_of(attr,
326 struct arm_ccn_pmu_event, attr);
327 ssize_t res;
328
329 res = snprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
330 if (event->event)
331 res += snprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
332 event->event);
333 if (event->def)
334 res += snprintf(buf + res, PAGE_SIZE - res, ",%s",
335 event->def);
336 if (event->mask)
337 res += snprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
338 event->mask);
339 res += snprintf(buf + res, PAGE_SIZE - res, "\n");
340
341 return res;
342}
343
344static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
345 struct attribute *attr, int index)
346{
347 struct device *dev = kobj_to_dev(kobj);
348 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
349 struct device_attribute *dev_attr = container_of(attr,
350 struct device_attribute, attr);
351 struct arm_ccn_pmu_event *event = container_of(dev_attr,
352 struct arm_ccn_pmu_event, attr);
353
354 if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
355 return 0;
356 if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
357 return 0;
358
359 return attr->mode;
360}
361
362static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
363 CCN_EVENT_MN(eobarrier, "dir=0,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
364 CCN_EVENT_MN(ecbarrier, "dir=0,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
365 CCN_EVENT_MN(dvmop, "dir=0,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
366 CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
367 CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
368 CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
369 CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
370 CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
371 CCN_IDX_MASK_ORDER),
372 CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
373 CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
374 CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
375 CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
376 CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
377 CCN_IDX_MASK_ORDER),
378 CCN_EVENT_HNF(cache_miss, 0x1),
379 CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
380 CCN_EVENT_HNF(cache_fill, 0x3),
381 CCN_EVENT_HNF(pocq_retry, 0x4),
382 CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
383 CCN_EVENT_HNF(sf_hit, 0x6),
384 CCN_EVENT_HNF(sf_evictions, 0x7),
385 CCN_EVENT_HNF(snoops_sent, 0x8),
386 CCN_EVENT_HNF(snoops_broadcast, 0x9),
387 CCN_EVENT_HNF(l3_eviction, 0xa),
388 CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
389 CCN_EVENT_HNF(mc_retries, 0xc),
390 CCN_EVENT_HNF(mc_reqs, 0xd),
391 CCN_EVENT_HNF(qos_hh_retry, 0xe),
392 CCN_EVENT_RNI(rdata_beats_p0, 0x1),
393 CCN_EVENT_RNI(rdata_beats_p1, 0x2),
394 CCN_EVENT_RNI(rdata_beats_p2, 0x3),
395 CCN_EVENT_RNI(rxdat_flits, 0x4),
396 CCN_EVENT_RNI(txdat_flits, 0x5),
397 CCN_EVENT_RNI(txreq_flits, 0x6),
398 CCN_EVENT_RNI(txreq_flits_retried, 0x7),
399 CCN_EVENT_RNI(rrt_full, 0x8),
400 CCN_EVENT_RNI(wrt_full, 0x9),
401 CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
402 CCN_EVENT_XP(upload_starvation, 0x1),
403 CCN_EVENT_XP(download_starvation, 0x2),
404 CCN_EVENT_XP(respin, 0x3),
405 CCN_EVENT_XP(valid_flit, 0x4),
406 CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
407 CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
408 CCN_EVENT_SBAS(rxdat_flits, 0x4),
409 CCN_EVENT_SBAS(txdat_flits, 0x5),
410 CCN_EVENT_SBAS(txreq_flits, 0x6),
411 CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
412 CCN_EVENT_SBAS(rrt_full, 0x8),
413 CCN_EVENT_SBAS(wrt_full, 0x9),
414 CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
415 CCN_EVENT_CYCLES(cycles),
416};
417
418/* Populated in arm_ccn_init() */
419static struct attribute
420 *arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
421
422static struct attribute_group arm_ccn_pmu_events_attr_group = {
423 .name = "events",
424 .is_visible = arm_ccn_pmu_events_is_visible,
425 .attrs = arm_ccn_pmu_events_attrs,
426};
427
428
429static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
430{
431 unsigned long i;
432
433 if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
434 return NULL;
435 i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
436
437 switch (name[1]) {
438 case 'l':
439 return &ccn->dt.cmp_mask[i].l;
440 case 'h':
441 return &ccn->dt.cmp_mask[i].h;
442 default:
443 return NULL;
444 }
445}
446
447static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
448 struct device_attribute *attr, char *buf)
449{
450 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
451 u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
452
453 return mask ? snprintf(buf, PAGE_SIZE, "0x%016llx\n", *mask) : -EINVAL;
454}
455
456static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
457 struct device_attribute *attr, const char *buf, size_t count)
458{
459 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
460 u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
461 int err = -EINVAL;
462
463 if (mask)
464 err = kstrtoull(buf, 0, mask);
465
466 return err ? err : count;
467}
468
469#define CCN_CMP_MASK_ATTR(_name) \
470 struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
471 __ATTR(_name, S_IRUGO | S_IWUSR, \
472 arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
473
474#define CCN_CMP_MASK_ATTR_RO(_name) \
475 struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
476 __ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
477
478static CCN_CMP_MASK_ATTR(0l);
479static CCN_CMP_MASK_ATTR(0h);
480static CCN_CMP_MASK_ATTR(1l);
481static CCN_CMP_MASK_ATTR(1h);
482static CCN_CMP_MASK_ATTR(2l);
483static CCN_CMP_MASK_ATTR(2h);
484static CCN_CMP_MASK_ATTR(3l);
485static CCN_CMP_MASK_ATTR(3h);
486static CCN_CMP_MASK_ATTR(4l);
487static CCN_CMP_MASK_ATTR(4h);
488static CCN_CMP_MASK_ATTR(5l);
489static CCN_CMP_MASK_ATTR(5h);
490static CCN_CMP_MASK_ATTR(6l);
491static CCN_CMP_MASK_ATTR(6h);
492static CCN_CMP_MASK_ATTR(7l);
493static CCN_CMP_MASK_ATTR(7h);
494static CCN_CMP_MASK_ATTR_RO(8l);
495static CCN_CMP_MASK_ATTR_RO(8h);
496static CCN_CMP_MASK_ATTR_RO(9l);
497static CCN_CMP_MASK_ATTR_RO(9h);
498static CCN_CMP_MASK_ATTR_RO(al);
499static CCN_CMP_MASK_ATTR_RO(ah);
500static CCN_CMP_MASK_ATTR_RO(bl);
501static CCN_CMP_MASK_ATTR_RO(bh);
502
503static struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
504 &arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
505 &arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
506 &arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
507 &arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
508 &arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
509 &arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
510 &arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
511 &arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
512 &arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
513 &arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
514 &arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
515 &arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
516 NULL
517};
518
519static struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
520 .name = "cmp_mask",
521 .attrs = arm_ccn_pmu_cmp_mask_attrs,
522};
523
524
525/*
526 * Default poll period is 10ms, which is way over the top anyway,
527 * as in the worst case scenario (an event every cycle), with 1GHz
528 * clocked bus, the smallest, 32 bit counter will overflow in
529 * more than 4s.
530 */
531static unsigned int arm_ccn_pmu_poll_period_us = 10000;
532module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
533 S_IRUGO | S_IWUSR);
534
535static ktime_t arm_ccn_pmu_timer_period(void)
536{
537 return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
538}
539
540
541static const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
542 &arm_ccn_pmu_events_attr_group,
543 &arm_ccn_pmu_format_attr_group,
544 &arm_ccn_pmu_cmp_mask_attr_group,
545 NULL
546};
547
548
549static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
550{
551 int bit;
552
553 do {
554 bit = find_first_zero_bit(bitmap, size);
555 if (bit >= size)
556 return -EAGAIN;
557 } while (test_and_set_bit(bit, bitmap));
558
559 return bit;
560}
561
562/* All RN-I and RN-D nodes have identical PMUs */
563static int arm_ccn_pmu_type_eq(u32 a, u32 b)
564{
565 if (a == b)
566 return 1;
567
568 switch (a) {
569 case CCN_TYPE_RNI_1P:
570 case CCN_TYPE_RNI_2P:
571 case CCN_TYPE_RNI_3P:
572 case CCN_TYPE_RND_1P:
573 case CCN_TYPE_RND_2P:
574 case CCN_TYPE_RND_3P:
575 switch (b) {
576 case CCN_TYPE_RNI_1P:
577 case CCN_TYPE_RNI_2P:
578 case CCN_TYPE_RNI_3P:
579 case CCN_TYPE_RND_1P:
580 case CCN_TYPE_RND_2P:
581 case CCN_TYPE_RND_3P:
582 return 1;
583 }
584 break;
585 }
586
587 return 0;
588}
589
590static int arm_ccn_pmu_event_init(struct perf_event *event)
591{
592 struct arm_ccn *ccn;
593 struct hw_perf_event *hw = &event->hw;
594 u32 node_xp, type, event_id;
Pawel Moll3e528cb2014-07-31 16:16:37 +0100595 int valid, bit;
Pawel Molla33b0da2014-07-22 18:32:59 +0100596 struct arm_ccn_component *source;
597 int i;
598
599 if (event->attr.type != event->pmu->type)
600 return -ENOENT;
601
602 ccn = pmu_to_arm_ccn(event->pmu);
603
604 if (hw->sample_period) {
605 dev_warn(ccn->dev, "Sampling not supported!\n");
606 return -EOPNOTSUPP;
607 }
608
609 if (has_branch_stack(event) || event->attr.exclude_user ||
610 event->attr.exclude_kernel || event->attr.exclude_hv ||
611 event->attr.exclude_idle) {
612 dev_warn(ccn->dev, "Can't exclude execution levels!\n");
613 return -EOPNOTSUPP;
614 }
615
616 if (event->cpu < 0) {
617 dev_warn(ccn->dev, "Can't provide per-task data!\n");
618 return -EOPNOTSUPP;
619 }
620
621 node_xp = CCN_CONFIG_NODE(event->attr.config);
622 type = CCN_CONFIG_TYPE(event->attr.config);
623 event_id = CCN_CONFIG_EVENT(event->attr.config);
624
625 /* Validate node/xp vs topology */
626 switch (type) {
627 case CCN_TYPE_XP:
628 if (node_xp >= ccn->num_xps) {
629 dev_warn(ccn->dev, "Invalid XP ID %d!\n", node_xp);
630 return -EINVAL;
631 }
632 break;
633 case CCN_TYPE_CYCLES:
634 break;
635 default:
636 if (node_xp >= ccn->num_nodes) {
637 dev_warn(ccn->dev, "Invalid node ID %d!\n", node_xp);
638 return -EINVAL;
639 }
640 if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
641 dev_warn(ccn->dev, "Invalid type 0x%x for node %d!\n",
642 type, node_xp);
643 return -EINVAL;
644 }
645 break;
646 }
647
648 /* Validate event ID vs available for the type */
649 for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
650 i++) {
651 struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
652 u32 port = CCN_CONFIG_PORT(event->attr.config);
653 u32 vc = CCN_CONFIG_VC(event->attr.config);
654
655 if (!arm_ccn_pmu_type_eq(type, e->type))
656 continue;
657 if (event_id != e->event)
658 continue;
659 if (e->num_ports && port >= e->num_ports) {
660 dev_warn(ccn->dev, "Invalid port %d for node/XP %d!\n",
661 port, node_xp);
662 return -EINVAL;
663 }
664 if (e->num_vcs && vc >= e->num_vcs) {
665 dev_warn(ccn->dev, "Invalid vc %d for node/XP %d!\n",
Pawel Mollbf87bb12014-08-18 18:20:49 +0100666 vc, node_xp);
Pawel Molla33b0da2014-07-22 18:32:59 +0100667 return -EINVAL;
668 }
669 valid = 1;
670 }
671 if (!valid) {
672 dev_warn(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
673 event_id, node_xp);
674 return -EINVAL;
675 }
676
677 /* Watchpoint-based event for a node is actually set on XP */
678 if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
679 u32 port;
680
681 type = CCN_TYPE_XP;
682 port = arm_ccn_node_to_xp_port(node_xp);
683 node_xp = arm_ccn_node_to_xp(node_xp);
684
685 arm_ccn_pmu_config_set(&event->attr.config,
686 node_xp, type, port);
687 }
688
689 /* Allocate the cycle counter */
690 if (type == CCN_TYPE_CYCLES) {
691 if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
692 ccn->dt.pmu_counters_mask))
693 return -EAGAIN;
694
695 hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
696 ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
697
698 return 0;
699 }
700
701 /* Allocate an event counter */
702 hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
703 CCN_NUM_PMU_EVENT_COUNTERS);
704 if (hw->idx < 0) {
705 dev_warn(ccn->dev, "No more counters available!\n");
706 return -EAGAIN;
707 }
708
709 if (type == CCN_TYPE_XP)
710 source = &ccn->xp[node_xp];
711 else
712 source = &ccn->node[node_xp];
713 ccn->dt.pmu_counters[hw->idx].source = source;
714
715 /* Allocate an event source or a watchpoint */
716 if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
Pawel Moll3e528cb2014-07-31 16:16:37 +0100717 bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
Pawel Molla33b0da2014-07-22 18:32:59 +0100718 CCN_NUM_XP_WATCHPOINTS);
719 else
Pawel Moll3e528cb2014-07-31 16:16:37 +0100720 bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
Pawel Molla33b0da2014-07-22 18:32:59 +0100721 CCN_NUM_PMU_EVENTS);
Pawel Moll3e528cb2014-07-31 16:16:37 +0100722 if (bit < 0) {
Pawel Molla33b0da2014-07-22 18:32:59 +0100723 dev_warn(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
724 node_xp);
725 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
726 return -EAGAIN;
727 }
Pawel Moll3e528cb2014-07-31 16:16:37 +0100728 hw->config_base = bit;
Pawel Molla33b0da2014-07-22 18:32:59 +0100729
730 ccn->dt.pmu_counters[hw->idx].event = event;
731
732 return 0;
733}
734
735static void arm_ccn_pmu_event_free(struct perf_event *event)
736{
737 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
738 struct hw_perf_event *hw = &event->hw;
739
740 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
741 clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
742 } else {
743 struct arm_ccn_component *source =
744 ccn->dt.pmu_counters[hw->idx].source;
745
746 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
747 CCN_CONFIG_EVENT(event->attr.config) ==
748 CCN_EVENT_WATCHPOINT)
749 clear_bit(hw->config_base, source->xp.dt_cmp_mask);
750 else
751 clear_bit(hw->config_base, source->pmu_events_mask);
752 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
753 }
754
755 ccn->dt.pmu_counters[hw->idx].source = NULL;
756 ccn->dt.pmu_counters[hw->idx].event = NULL;
757}
758
759static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
760{
761 u64 res;
762
763 if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
764#ifdef readq
765 res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
766#else
767 /* 40 bit counter, can do snapshot and read in two parts */
768 writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
769 while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
770 ;
771 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
772 res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
773 res <<= 32;
774 res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
775#endif
776 } else {
777 res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
778 }
779
780 return res;
781}
782
783static void arm_ccn_pmu_event_update(struct perf_event *event)
784{
785 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
786 struct hw_perf_event *hw = &event->hw;
787 u64 prev_count, new_count, mask;
788
789 do {
790 prev_count = local64_read(&hw->prev_count);
791 new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
792 } while (local64_xchg(&hw->prev_count, new_count) != prev_count);
793
794 mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
795
796 local64_add((new_count - prev_count) & mask, &event->count);
797}
798
799static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
800{
801 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
802 struct hw_perf_event *hw = &event->hw;
803 struct arm_ccn_component *xp;
804 u32 val, dt_cfg;
805
806 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
807 xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
808 else
809 xp = &ccn->xp[arm_ccn_node_to_xp(
810 CCN_CONFIG_NODE(event->attr.config))];
811
812 if (enable)
813 dt_cfg = hw->event_base;
814 else
815 dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
816
817 spin_lock(&ccn->dt.config_lock);
818
819 val = readl(xp->base + CCN_XP_DT_CONFIG);
820 val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
821 CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
822 val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
823 writel(val, xp->base + CCN_XP_DT_CONFIG);
824
825 spin_unlock(&ccn->dt.config_lock);
826}
827
828static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
829{
830 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
831 struct hw_perf_event *hw = &event->hw;
832
833 local64_set(&event->hw.prev_count,
834 arm_ccn_pmu_read_counter(ccn, hw->idx));
835 hw->state = 0;
836
837 if (!ccn->irq_used)
838 hrtimer_start(&ccn->dt.hrtimer, arm_ccn_pmu_timer_period(),
839 HRTIMER_MODE_REL);
840
841 /* Set the DT bus input, engaging the counter */
842 arm_ccn_pmu_xp_dt_config(event, 1);
843}
844
845static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
846{
847 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
848 struct hw_perf_event *hw = &event->hw;
849 u64 timeout;
850
851 /* Disable counting, setting the DT bus to pass-through mode */
852 arm_ccn_pmu_xp_dt_config(event, 0);
853
854 if (!ccn->irq_used)
855 hrtimer_cancel(&ccn->dt.hrtimer);
856
857 /* Let the DT bus drain */
858 timeout = arm_ccn_pmu_read_counter(ccn, CCN_IDX_PMU_CYCLE_COUNTER) +
859 ccn->num_xps;
860 while (arm_ccn_pmu_read_counter(ccn, CCN_IDX_PMU_CYCLE_COUNTER) <
861 timeout)
862 cpu_relax();
863
864 if (flags & PERF_EF_UPDATE)
865 arm_ccn_pmu_event_update(event);
866
867 hw->state |= PERF_HES_STOPPED;
868}
869
870static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
871{
872 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
873 struct hw_perf_event *hw = &event->hw;
874 struct arm_ccn_component *source =
875 ccn->dt.pmu_counters[hw->idx].source;
876 unsigned long wp = hw->config_base;
877 u32 val;
878 u64 cmp_l = event->attr.config1;
879 u64 cmp_h = event->attr.config2;
880 u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
881 u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
882
883 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
884
885 /* Direction (RX/TX), device (port) & virtual channel */
886 val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
887 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
888 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
889 val |= CCN_CONFIG_DIR(event->attr.config) <<
890 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
891 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
892 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
893 val |= CCN_CONFIG_PORT(event->attr.config) <<
894 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
895 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
896 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
897 val |= CCN_CONFIG_VC(event->attr.config) <<
898 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
899 writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
900
901 /* Comparison values */
902 writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
903 writel((cmp_l >> 32) & 0xefffffff,
904 source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
905 writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
906 writel((cmp_h >> 32) & 0x0fffffff,
907 source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
908
909 /* Mask */
910 writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
911 writel((mask_l >> 32) & 0xefffffff,
912 source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
913 writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
914 writel((mask_h >> 32) & 0x0fffffff,
915 source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
916}
917
918static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
919{
920 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
921 struct hw_perf_event *hw = &event->hw;
922 struct arm_ccn_component *source =
923 ccn->dt.pmu_counters[hw->idx].source;
924 u32 val, id;
925
926 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
927
928 id = (CCN_CONFIG_VC(event->attr.config) << 4) |
929 (CCN_CONFIG_PORT(event->attr.config) << 3) |
930 (CCN_CONFIG_EVENT(event->attr.config) << 0);
931
932 val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
933 val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
934 CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
935 val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
936 writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
937}
938
939static void arm_ccn_pmu_node_event_config(struct perf_event *event)
940{
941 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
942 struct hw_perf_event *hw = &event->hw;
943 struct arm_ccn_component *source =
944 ccn->dt.pmu_counters[hw->idx].source;
945 u32 type = CCN_CONFIG_TYPE(event->attr.config);
946 u32 val, port;
947
948 port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
949 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
950 hw->config_base);
951
952 /* These *_event_sel regs should be identical, but let's make sure... */
953 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
954 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
955 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
956 CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
957 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
958 CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
959 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
960 CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
961 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
962 CCN_RNI_PMU_EVENT_SEL__ID__MASK);
963 if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
964 !arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
965 return;
966
967 /* Set the event id for the pre-allocated counter */
968 val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
969 val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
970 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
971 val |= CCN_CONFIG_EVENT(event->attr.config) <<
972 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
973 writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
974}
975
976static void arm_ccn_pmu_event_config(struct perf_event *event)
977{
978 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
979 struct hw_perf_event *hw = &event->hw;
980 u32 xp, offset, val;
981
982 /* Cycle counter requires no setup */
983 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
984 return;
985
986 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
987 xp = CCN_CONFIG_XP(event->attr.config);
988 else
989 xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
990
991 spin_lock(&ccn->dt.config_lock);
992
993 /* Set the DT bus "distance" register */
994 offset = (hw->idx / 4) * 4;
995 val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
996 val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
997 CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
998 val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
999 writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1000
1001 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
1002 if (CCN_CONFIG_EVENT(event->attr.config) ==
1003 CCN_EVENT_WATCHPOINT)
1004 arm_ccn_pmu_xp_watchpoint_config(event);
1005 else
1006 arm_ccn_pmu_xp_event_config(event);
1007 } else {
1008 arm_ccn_pmu_node_event_config(event);
1009 }
1010
1011 spin_unlock(&ccn->dt.config_lock);
1012}
1013
1014static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
1015{
1016 struct hw_perf_event *hw = &event->hw;
1017
1018 arm_ccn_pmu_event_config(event);
1019
1020 hw->state = PERF_HES_STOPPED;
1021
1022 if (flags & PERF_EF_START)
1023 arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
1024
1025 return 0;
1026}
1027
1028static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
1029{
1030 arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
1031
1032 arm_ccn_pmu_event_free(event);
1033}
1034
1035static void arm_ccn_pmu_event_read(struct perf_event *event)
1036{
1037 arm_ccn_pmu_event_update(event);
1038}
1039
1040static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
1041{
1042 u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
1043 int idx;
1044
1045 if (!pmovsr)
1046 return IRQ_NONE;
1047
1048 writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
1049
1050 BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
1051
1052 for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
1053 struct perf_event *event = dt->pmu_counters[idx].event;
1054 int overflowed = pmovsr & BIT(idx);
1055
Pawel Mollfa637bf2014-09-15 15:33:48 +01001056 WARN_ON_ONCE(overflowed && !event &&
1057 idx != CCN_IDX_PMU_CYCLE_COUNTER);
Pawel Molla33b0da2014-07-22 18:32:59 +01001058
1059 if (!event || !overflowed)
1060 continue;
1061
1062 arm_ccn_pmu_event_update(event);
1063 }
1064
1065 return IRQ_HANDLED;
1066}
1067
1068static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
1069{
1070 struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
1071 hrtimer);
1072 unsigned long flags;
1073
1074 local_irq_save(flags);
1075 arm_ccn_pmu_overflow_handler(dt);
1076 local_irq_restore(flags);
1077
1078 hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
1079 return HRTIMER_RESTART;
1080}
1081
1082
1083static DEFINE_IDA(arm_ccn_pmu_ida);
1084
1085static int arm_ccn_pmu_init(struct arm_ccn *ccn)
1086{
1087 int i;
1088 char *name;
1089
1090 /* Initialize DT subsystem */
1091 ccn->dt.base = ccn->base + CCN_REGION_SIZE;
1092 spin_lock_init(&ccn->dt.config_lock);
Pawel Mollfa637bf2014-09-15 15:33:48 +01001093 writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
Pawel Molla33b0da2014-07-22 18:32:59 +01001094 writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
1095 writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
1096 ccn->dt.base + CCN_DT_PMCR);
1097 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
1098 for (i = 0; i < ccn->num_xps; i++) {
1099 writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
1100 writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1101 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
1102 (CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1103 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
1104 CCN_XP_DT_CONTROL__DT_ENABLE,
1105 ccn->xp[i].base + CCN_XP_DT_CONTROL);
1106 }
1107 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
1108 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
1109 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
1110 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
1111 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
1112 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
1113 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
1114 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
1115
1116 /* Get a convenient /sys/event_source/devices/ name */
1117 ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
1118 if (ccn->dt.id == 0) {
1119 name = "ccn";
1120 } else {
1121 int len = snprintf(NULL, 0, "ccn_%d", ccn->dt.id);
1122
1123 name = devm_kzalloc(ccn->dev, len + 1, GFP_KERNEL);
1124 snprintf(name, len + 1, "ccn_%d", ccn->dt.id);
1125 }
1126
1127 /* Perf driver registration */
1128 ccn->dt.pmu = (struct pmu) {
1129 .attr_groups = arm_ccn_pmu_attr_groups,
1130 .task_ctx_nr = perf_invalid_context,
1131 .event_init = arm_ccn_pmu_event_init,
1132 .add = arm_ccn_pmu_event_add,
1133 .del = arm_ccn_pmu_event_del,
1134 .start = arm_ccn_pmu_event_start,
1135 .stop = arm_ccn_pmu_event_stop,
1136 .read = arm_ccn_pmu_event_read,
1137 };
1138
1139 /* No overflow interrupt? Have to use a timer instead. */
1140 if (!ccn->irq_used) {
1141 dev_info(ccn->dev, "No access to interrupts, using timer.\n");
1142 hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
1143 HRTIMER_MODE_REL);
1144 ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
1145 }
1146
1147 return perf_pmu_register(&ccn->dt.pmu, name, -1);
1148}
1149
1150static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
1151{
1152 int i;
1153
1154 for (i = 0; i < ccn->num_xps; i++)
1155 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1156 writel(0, ccn->dt.base + CCN_DT_PMCR);
1157 perf_pmu_unregister(&ccn->dt.pmu);
1158 ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1159}
1160
1161
1162static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
1163 int (*callback)(struct arm_ccn *ccn, int region,
1164 void __iomem *base, u32 type, u32 id))
1165{
1166 int region;
1167
1168 for (region = 0; region < CCN_NUM_REGIONS; region++) {
1169 u32 val, type, id;
1170 void __iomem *base;
1171 int err;
1172
1173 val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
1174 4 * (region / 32));
1175 if (!(val & (1 << (region % 32))))
1176 continue;
1177
1178 base = ccn->base + region * CCN_REGION_SIZE;
1179 val = readl(base + CCN_ALL_OLY_ID);
1180 type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
1181 CCN_ALL_OLY_ID__OLY_ID__MASK;
1182 id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
1183 CCN_ALL_OLY_ID__NODE_ID__MASK;
1184
1185 err = callback(ccn, region, base, type, id);
1186 if (err)
1187 return err;
1188 }
1189
1190 return 0;
1191}
1192
1193static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
1194 void __iomem *base, u32 type, u32 id)
1195{
1196
1197 if (type == CCN_TYPE_XP && id >= ccn->num_xps)
1198 ccn->num_xps = id + 1;
1199 else if (id >= ccn->num_nodes)
1200 ccn->num_nodes = id + 1;
1201
1202 return 0;
1203}
1204
1205static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
1206 void __iomem *base, u32 type, u32 id)
1207{
1208 struct arm_ccn_component *component;
1209
1210 dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
1211
1212 switch (type) {
1213 case CCN_TYPE_MN:
1214 case CCN_TYPE_DT:
1215 return 0;
1216 case CCN_TYPE_XP:
1217 component = &ccn->xp[id];
1218 break;
1219 case CCN_TYPE_SBSX:
1220 ccn->sbsx_present = 1;
1221 component = &ccn->node[id];
1222 break;
1223 case CCN_TYPE_SBAS:
1224 ccn->sbas_present = 1;
1225 /* Fall-through */
1226 default:
1227 component = &ccn->node[id];
1228 break;
1229 }
1230
1231 component->base = base;
1232 component->type = type;
1233
1234 return 0;
1235}
1236
1237
1238static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
1239 const u32 *err_sig_val)
1240{
1241 /* This should be really handled by firmware... */
1242 dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
1243 err_sig_val[5], err_sig_val[4], err_sig_val[3],
1244 err_sig_val[2], err_sig_val[1], err_sig_val[0]);
1245 dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
1246 writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
1247 ccn->base + CCN_MN_ERRINT_STATUS);
1248
1249 return IRQ_HANDLED;
1250}
1251
1252
1253static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
1254{
1255 irqreturn_t res = IRQ_NONE;
1256 struct arm_ccn *ccn = dev_id;
1257 u32 err_sig_val[6];
1258 u32 err_or;
1259 int i;
1260
1261 /* PMU overflow is a special case */
1262 err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
1263 if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
1264 err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
1265 res = arm_ccn_pmu_overflow_handler(&ccn->dt);
1266 }
1267
1268 /* Have to read all err_sig_vals to clear them */
1269 for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
1270 err_sig_val[i] = readl(ccn->base +
1271 CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
1272 err_or |= err_sig_val[i];
1273 }
1274 if (err_or)
1275 res |= arm_ccn_error_handler(ccn, err_sig_val);
1276
1277 if (res != IRQ_NONE)
1278 writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
1279 ccn->base + CCN_MN_ERRINT_STATUS);
1280
1281 return res;
1282}
1283
1284
1285static int arm_ccn_probe(struct platform_device *pdev)
1286{
1287 struct arm_ccn *ccn;
1288 struct resource *res;
1289 int err;
1290
1291 ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
1292 if (!ccn)
1293 return -ENOMEM;
1294 ccn->dev = &pdev->dev;
1295 platform_set_drvdata(pdev, ccn);
1296
1297 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1298 if (!res)
1299 return -EINVAL;
1300
1301 if (!devm_request_mem_region(ccn->dev, res->start,
1302 resource_size(res), pdev->name))
1303 return -EBUSY;
1304
1305 ccn->base = devm_ioremap(ccn->dev, res->start,
1306 resource_size(res));
1307 if (!ccn->base)
1308 return -EFAULT;
1309
1310 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1311 if (!res)
1312 return -EINVAL;
1313
1314 /* Check if we can use the interrupt */
1315 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
1316 ccn->base + CCN_MN_ERRINT_STATUS);
1317 if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
1318 CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
1319 /* Can set 'disable' bits, so can acknowledge interrupts */
1320 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
1321 ccn->base + CCN_MN_ERRINT_STATUS);
1322 err = devm_request_irq(ccn->dev, res->start,
1323 arm_ccn_irq_handler, 0, dev_name(ccn->dev),
1324 ccn);
1325 if (err)
1326 return err;
1327
1328 ccn->irq_used = 1;
1329 }
1330
1331
1332 /* Build topology */
1333
1334 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
1335 if (err)
1336 return err;
1337
1338 ccn->node = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_nodes,
1339 GFP_KERNEL);
1340 ccn->xp = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_xps,
1341 GFP_KERNEL);
1342 if (!ccn->node || !ccn->xp)
1343 return -ENOMEM;
1344
1345 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
1346 if (err)
1347 return err;
1348
1349 return arm_ccn_pmu_init(ccn);
1350}
1351
1352static int arm_ccn_remove(struct platform_device *pdev)
1353{
1354 struct arm_ccn *ccn = platform_get_drvdata(pdev);
1355
1356 arm_ccn_pmu_cleanup(ccn);
1357
1358 return 0;
1359}
1360
1361static const struct of_device_id arm_ccn_match[] = {
1362 { .compatible = "arm,ccn-504", },
1363 {},
1364};
1365
1366static struct platform_driver arm_ccn_driver = {
1367 .driver = {
1368 .name = "arm-ccn",
1369 .of_match_table = arm_ccn_match,
1370 },
1371 .probe = arm_ccn_probe,
1372 .remove = arm_ccn_remove,
1373};
1374
1375static int __init arm_ccn_init(void)
1376{
1377 int i;
1378
1379 for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
1380 arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
1381
1382 return platform_driver_register(&arm_ccn_driver);
1383}
1384
1385static void __exit arm_ccn_exit(void)
1386{
1387 platform_driver_unregister(&arm_ccn_driver);
1388}
1389
1390module_init(arm_ccn_init);
1391module_exit(arm_ccn_exit);
1392
1393MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
1394MODULE_LICENSE("GPL");