blob: 572360f1f493edff8ad0572aa2d45ee17e0693ea [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
Pawel Mollffa41522015-04-16 12:14:35 +0100169 cpumask_t cpu;
170 struct notifier_block cpu_nb;
171
Pawel Molla33b0da2014-07-22 18:32:59 +0100172 struct pmu pmu;
173};
174
175struct arm_ccn {
176 struct device *dev;
177 void __iomem *base;
Pawel Mollffa41522015-04-16 12:14:35 +0100178 unsigned int irq;
179
Pawel Molla33b0da2014-07-22 18:32:59 +0100180 unsigned sbas_present:1;
181 unsigned sbsx_present:1;
182
183 int num_nodes;
184 struct arm_ccn_component *node;
185
186 int num_xps;
187 struct arm_ccn_component *xp;
188
189 struct arm_ccn_dt dt;
190};
191
192
193static int arm_ccn_node_to_xp(int node)
194{
195 return node / CCN_NUM_XP_PORTS;
196}
197
198static int arm_ccn_node_to_xp_port(int node)
199{
200 return node % CCN_NUM_XP_PORTS;
201}
202
203
204/*
205 * Bit shifts and masks in these defines must be kept in sync with
206 * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
207 */
208#define CCN_CONFIG_NODE(_config) (((_config) >> 0) & 0xff)
209#define CCN_CONFIG_XP(_config) (((_config) >> 0) & 0xff)
210#define CCN_CONFIG_TYPE(_config) (((_config) >> 8) & 0xff)
211#define CCN_CONFIG_EVENT(_config) (((_config) >> 16) & 0xff)
212#define CCN_CONFIG_PORT(_config) (((_config) >> 24) & 0x3)
213#define CCN_CONFIG_VC(_config) (((_config) >> 26) & 0x7)
214#define CCN_CONFIG_DIR(_config) (((_config) >> 29) & 0x1)
215#define CCN_CONFIG_MASK(_config) (((_config) >> 30) & 0xf)
216
217static void arm_ccn_pmu_config_set(u64 *config, u32 node_xp, u32 type, u32 port)
218{
Pawel Molla18f8e92015-04-02 18:50:32 +0100219 *config &= ~((0xff << 0) | (0xff << 8) | (0x3 << 24));
Pawel Molla33b0da2014-07-22 18:32:59 +0100220 *config |= (node_xp << 0) | (type << 8) | (port << 24);
221}
222
223static ssize_t arm_ccn_pmu_format_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 struct dev_ext_attribute *ea = container_of(attr,
227 struct dev_ext_attribute, attr);
228
229 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)ea->var);
230}
231
232#define CCN_FORMAT_ATTR(_name, _config) \
233 struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
234 { __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
235 NULL), _config }
236
237static CCN_FORMAT_ATTR(node, "config:0-7");
238static CCN_FORMAT_ATTR(xp, "config:0-7");
239static CCN_FORMAT_ATTR(type, "config:8-15");
240static CCN_FORMAT_ATTR(event, "config:16-23");
241static CCN_FORMAT_ATTR(port, "config:24-25");
242static CCN_FORMAT_ATTR(vc, "config:26-28");
243static CCN_FORMAT_ATTR(dir, "config:29-29");
244static CCN_FORMAT_ATTR(mask, "config:30-33");
245static CCN_FORMAT_ATTR(cmp_l, "config1:0-62");
246static CCN_FORMAT_ATTR(cmp_h, "config2:0-59");
247
248static struct attribute *arm_ccn_pmu_format_attrs[] = {
249 &arm_ccn_pmu_format_attr_node.attr.attr,
250 &arm_ccn_pmu_format_attr_xp.attr.attr,
251 &arm_ccn_pmu_format_attr_type.attr.attr,
252 &arm_ccn_pmu_format_attr_event.attr.attr,
253 &arm_ccn_pmu_format_attr_port.attr.attr,
254 &arm_ccn_pmu_format_attr_vc.attr.attr,
255 &arm_ccn_pmu_format_attr_dir.attr.attr,
256 &arm_ccn_pmu_format_attr_mask.attr.attr,
257 &arm_ccn_pmu_format_attr_cmp_l.attr.attr,
258 &arm_ccn_pmu_format_attr_cmp_h.attr.attr,
259 NULL
260};
261
262static struct attribute_group arm_ccn_pmu_format_attr_group = {
263 .name = "format",
264 .attrs = arm_ccn_pmu_format_attrs,
265};
266
267
268struct arm_ccn_pmu_event {
269 struct device_attribute attr;
270 u32 type;
271 u32 event;
272 int num_ports;
273 int num_vcs;
274 const char *def;
275 int mask;
276};
277
278#define CCN_EVENT_ATTR(_name) \
279 __ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
280
281/*
282 * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
283 * their ports in XP they are connected to. For the sake of usability they are
284 * explicitly defined here (and translated into a relevant watchpoint in
285 * arm_ccn_pmu_event_init()) so the user can easily request them without deep
286 * knowledge of the flit format.
287 */
288
289#define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
290 .type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
291 .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
292 .def = _def, .mask = _mask, }
293
294#define CCN_EVENT_HNI(_name, _def, _mask) { \
295 .attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
296 .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
297 .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
298
299#define CCN_EVENT_SBSX(_name, _def, _mask) { \
300 .attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
301 .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
302 .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
303
304#define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
305 .type = CCN_TYPE_HNF, .event = _event, }
306
307#define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
308 .type = CCN_TYPE_XP, .event = _event, \
309 .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
310
311/*
312 * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
313 * on configuration. One of them is picked to represent the whole group,
314 * as they all share the same event types.
315 */
316#define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
317 .type = CCN_TYPE_RNI_3P, .event = _event, }
318
319#define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
320 .type = CCN_TYPE_SBAS, .event = _event, }
321
322#define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
323 .type = CCN_TYPE_CYCLES }
324
325
326static ssize_t arm_ccn_pmu_event_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329 struct arm_ccn_pmu_event *event = container_of(attr,
330 struct arm_ccn_pmu_event, attr);
331 ssize_t res;
332
333 res = snprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
334 if (event->event)
335 res += snprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
336 event->event);
337 if (event->def)
338 res += snprintf(buf + res, PAGE_SIZE - res, ",%s",
339 event->def);
340 if (event->mask)
341 res += snprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
342 event->mask);
Pawel Moll8f06c512015-04-02 14:01:06 +0100343
344 /* Arguments required by an event */
345 switch (event->type) {
346 case CCN_TYPE_CYCLES:
347 break;
348 case CCN_TYPE_XP:
349 res += snprintf(buf + res, PAGE_SIZE - res,
350 ",xp=?,port=?,vc=?,dir=?");
351 if (event->event == CCN_EVENT_WATCHPOINT)
352 res += snprintf(buf + res, PAGE_SIZE - res,
353 ",cmp_l=?,cmp_h=?,mask=?");
354 break;
355 default:
356 res += snprintf(buf + res, PAGE_SIZE - res, ",node=?");
357 break;
358 }
359
Pawel Molla33b0da2014-07-22 18:32:59 +0100360 res += snprintf(buf + res, PAGE_SIZE - res, "\n");
361
362 return res;
363}
364
365static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
366 struct attribute *attr, int index)
367{
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);
374
375 if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
376 return 0;
377 if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
378 return 0;
379
380 return attr->mode;
381}
382
383static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
384 CCN_EVENT_MN(eobarrier, "dir=0,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
385 CCN_EVENT_MN(ecbarrier, "dir=0,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
386 CCN_EVENT_MN(dvmop, "dir=0,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",
392 CCN_IDX_MASK_ORDER),
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",
398 CCN_IDX_MASK_ORDER),
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),
437};
438
439/* Populated in arm_ccn_init() */
440static struct attribute
441 *arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
442
443static struct attribute_group arm_ccn_pmu_events_attr_group = {
444 .name = "events",
445 .is_visible = arm_ccn_pmu_events_is_visible,
446 .attrs = arm_ccn_pmu_events_attrs,
447};
448
449
450static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
451{
452 unsigned long i;
453
454 if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
455 return NULL;
456 i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
457
458 switch (name[1]) {
459 case 'l':
460 return &ccn->dt.cmp_mask[i].l;
461 case 'h':
462 return &ccn->dt.cmp_mask[i].h;
463 default:
464 return NULL;
465 }
466}
467
468static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
470{
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);
473
474 return mask ? snprintf(buf, PAGE_SIZE, "0x%016llx\n", *mask) : -EINVAL;
475}
476
477static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
478 struct device_attribute *attr, const char *buf, size_t count)
479{
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);
482 int err = -EINVAL;
483
484 if (mask)
485 err = kstrtoull(buf, 0, mask);
486
487 return err ? err : count;
488}
489
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)
494
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)
498
499static CCN_CMP_MASK_ATTR(0l);
500static CCN_CMP_MASK_ATTR(0h);
501static CCN_CMP_MASK_ATTR(1l);
502static CCN_CMP_MASK_ATTR(1h);
503static CCN_CMP_MASK_ATTR(2l);
504static CCN_CMP_MASK_ATTR(2h);
505static CCN_CMP_MASK_ATTR(3l);
506static CCN_CMP_MASK_ATTR(3h);
507static CCN_CMP_MASK_ATTR(4l);
508static CCN_CMP_MASK_ATTR(4h);
509static CCN_CMP_MASK_ATTR(5l);
510static CCN_CMP_MASK_ATTR(5h);
511static CCN_CMP_MASK_ATTR(6l);
512static CCN_CMP_MASK_ATTR(6h);
513static CCN_CMP_MASK_ATTR(7l);
514static CCN_CMP_MASK_ATTR(7h);
515static CCN_CMP_MASK_ATTR_RO(8l);
516static CCN_CMP_MASK_ATTR_RO(8h);
517static CCN_CMP_MASK_ATTR_RO(9l);
518static CCN_CMP_MASK_ATTR_RO(9h);
519static CCN_CMP_MASK_ATTR_RO(al);
520static CCN_CMP_MASK_ATTR_RO(ah);
521static CCN_CMP_MASK_ATTR_RO(bl);
522static CCN_CMP_MASK_ATTR_RO(bh);
523
524static 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,
537 NULL
538};
539
540static struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
541 .name = "cmp_mask",
542 .attrs = arm_ccn_pmu_cmp_mask_attrs,
543};
544
Pawel Mollffa41522015-04-16 12:14:35 +0100545static ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
547{
548 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
549
550 return cpumap_print_to_pagebuf(true, buf, &ccn->dt.cpu);
551}
552
553static struct device_attribute arm_ccn_pmu_cpumask_attr =
554 __ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
555
556static struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
557 &arm_ccn_pmu_cpumask_attr.attr,
558 NULL,
559};
560
561static struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
562 .attrs = arm_ccn_pmu_cpumask_attrs,
563};
Pawel Molla33b0da2014-07-22 18:32:59 +0100564
565/*
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
569 * more than 4s.
570 */
571static unsigned int arm_ccn_pmu_poll_period_us = 10000;
572module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
573 S_IRUGO | S_IWUSR);
574
575static ktime_t arm_ccn_pmu_timer_period(void)
576{
577 return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
578}
579
580
581static 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,
Pawel Mollffa41522015-04-16 12:14:35 +0100585 &arm_ccn_pmu_cpumask_attr_group,
Pawel Molla33b0da2014-07-22 18:32:59 +0100586 NULL
587};
588
589
590static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
591{
592 int bit;
593
594 do {
595 bit = find_first_zero_bit(bitmap, size);
596 if (bit >= size)
597 return -EAGAIN;
598 } while (test_and_set_bit(bit, bitmap));
599
600 return bit;
601}
602
603/* All RN-I and RN-D nodes have identical PMUs */
604static int arm_ccn_pmu_type_eq(u32 a, u32 b)
605{
606 if (a == b)
607 return 1;
608
609 switch (a) {
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:
616 switch (b) {
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:
623 return 1;
624 }
625 break;
626 }
627
628 return 0;
629}
630
Pawel Moll8fb22262014-09-02 16:26:11 +0100631static void arm_ccn_pmu_event_destroy(struct perf_event *event)
632{
633 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
634 struct hw_perf_event *hw = &event->hw;
635
636 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
637 clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
638 } else {
639 struct arm_ccn_component *source =
640 ccn->dt.pmu_counters[hw->idx].source;
641
642 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
643 CCN_CONFIG_EVENT(event->attr.config) ==
644 CCN_EVENT_WATCHPOINT)
645 clear_bit(hw->config_base, source->xp.dt_cmp_mask);
646 else
647 clear_bit(hw->config_base, source->pmu_events_mask);
648 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
649 }
650
651 ccn->dt.pmu_counters[hw->idx].source = NULL;
652 ccn->dt.pmu_counters[hw->idx].event = NULL;
653}
654
Pawel Molla33b0da2014-07-22 18:32:59 +0100655static int arm_ccn_pmu_event_init(struct perf_event *event)
656{
657 struct arm_ccn *ccn;
658 struct hw_perf_event *hw = &event->hw;
659 u32 node_xp, type, event_id;
Pawel Moll3e528cb2014-07-31 16:16:37 +0100660 int valid, bit;
Pawel Molla33b0da2014-07-22 18:32:59 +0100661 struct arm_ccn_component *source;
662 int i;
663
664 if (event->attr.type != event->pmu->type)
665 return -ENOENT;
666
667 ccn = pmu_to_arm_ccn(event->pmu);
Pawel Moll8fb22262014-09-02 16:26:11 +0100668 event->destroy = arm_ccn_pmu_event_destroy;
Pawel Molla33b0da2014-07-22 18:32:59 +0100669
670 if (hw->sample_period) {
671 dev_warn(ccn->dev, "Sampling not supported!\n");
672 return -EOPNOTSUPP;
673 }
674
675 if (has_branch_stack(event) || event->attr.exclude_user ||
676 event->attr.exclude_kernel || event->attr.exclude_hv ||
677 event->attr.exclude_idle) {
678 dev_warn(ccn->dev, "Can't exclude execution levels!\n");
679 return -EOPNOTSUPP;
680 }
681
682 if (event->cpu < 0) {
683 dev_warn(ccn->dev, "Can't provide per-task data!\n");
684 return -EOPNOTSUPP;
685 }
Pawel Mollffa41522015-04-16 12:14:35 +0100686 /*
687 * Many perf core operations (eg. events rotation) operate on a
688 * single CPU context. This is obvious for CPU PMUs, where one
689 * expects the same sets of events being observed on all CPUs,
690 * but can lead to issues for off-core PMUs, like CCN, where each
691 * event could be theoretically assigned to a different CPU. To
692 * mitigate this, we enforce CPU assignment to one, selected
693 * processor (the one described in the "cpumask" attribute).
694 */
695 event->cpu = cpumask_first(&ccn->dt.cpu);
Pawel Molla33b0da2014-07-22 18:32:59 +0100696
697 node_xp = CCN_CONFIG_NODE(event->attr.config);
698 type = CCN_CONFIG_TYPE(event->attr.config);
699 event_id = CCN_CONFIG_EVENT(event->attr.config);
700
701 /* Validate node/xp vs topology */
702 switch (type) {
703 case CCN_TYPE_XP:
704 if (node_xp >= ccn->num_xps) {
705 dev_warn(ccn->dev, "Invalid XP ID %d!\n", node_xp);
706 return -EINVAL;
707 }
708 break;
709 case CCN_TYPE_CYCLES:
710 break;
711 default:
712 if (node_xp >= ccn->num_nodes) {
713 dev_warn(ccn->dev, "Invalid node ID %d!\n", node_xp);
714 return -EINVAL;
715 }
716 if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
717 dev_warn(ccn->dev, "Invalid type 0x%x for node %d!\n",
718 type, node_xp);
719 return -EINVAL;
720 }
721 break;
722 }
723
724 /* Validate event ID vs available for the type */
725 for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
726 i++) {
727 struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
728 u32 port = CCN_CONFIG_PORT(event->attr.config);
729 u32 vc = CCN_CONFIG_VC(event->attr.config);
730
731 if (!arm_ccn_pmu_type_eq(type, e->type))
732 continue;
733 if (event_id != e->event)
734 continue;
735 if (e->num_ports && port >= e->num_ports) {
736 dev_warn(ccn->dev, "Invalid port %d for node/XP %d!\n",
737 port, node_xp);
738 return -EINVAL;
739 }
740 if (e->num_vcs && vc >= e->num_vcs) {
741 dev_warn(ccn->dev, "Invalid vc %d for node/XP %d!\n",
Pawel Mollbf87bb12014-08-18 18:20:49 +0100742 vc, node_xp);
Pawel Molla33b0da2014-07-22 18:32:59 +0100743 return -EINVAL;
744 }
745 valid = 1;
746 }
747 if (!valid) {
748 dev_warn(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
749 event_id, node_xp);
750 return -EINVAL;
751 }
752
753 /* Watchpoint-based event for a node is actually set on XP */
754 if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
755 u32 port;
756
757 type = CCN_TYPE_XP;
758 port = arm_ccn_node_to_xp_port(node_xp);
759 node_xp = arm_ccn_node_to_xp(node_xp);
760
761 arm_ccn_pmu_config_set(&event->attr.config,
762 node_xp, type, port);
763 }
764
765 /* Allocate the cycle counter */
766 if (type == CCN_TYPE_CYCLES) {
767 if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
768 ccn->dt.pmu_counters_mask))
769 return -EAGAIN;
770
771 hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
772 ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
773
774 return 0;
775 }
776
777 /* Allocate an event counter */
778 hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
779 CCN_NUM_PMU_EVENT_COUNTERS);
780 if (hw->idx < 0) {
781 dev_warn(ccn->dev, "No more counters available!\n");
782 return -EAGAIN;
783 }
784
785 if (type == CCN_TYPE_XP)
786 source = &ccn->xp[node_xp];
787 else
788 source = &ccn->node[node_xp];
789 ccn->dt.pmu_counters[hw->idx].source = source;
790
791 /* Allocate an event source or a watchpoint */
792 if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
Pawel Moll3e528cb2014-07-31 16:16:37 +0100793 bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
Pawel Molla33b0da2014-07-22 18:32:59 +0100794 CCN_NUM_XP_WATCHPOINTS);
795 else
Pawel Moll3e528cb2014-07-31 16:16:37 +0100796 bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
Pawel Molla33b0da2014-07-22 18:32:59 +0100797 CCN_NUM_PMU_EVENTS);
Pawel Moll3e528cb2014-07-31 16:16:37 +0100798 if (bit < 0) {
Pawel Molla33b0da2014-07-22 18:32:59 +0100799 dev_warn(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
800 node_xp);
801 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
802 return -EAGAIN;
803 }
Pawel Moll3e528cb2014-07-31 16:16:37 +0100804 hw->config_base = bit;
Pawel Molla33b0da2014-07-22 18:32:59 +0100805
806 ccn->dt.pmu_counters[hw->idx].event = event;
807
808 return 0;
809}
810
Pawel Molla33b0da2014-07-22 18:32:59 +0100811static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
812{
813 u64 res;
814
815 if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
816#ifdef readq
817 res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
818#else
819 /* 40 bit counter, can do snapshot and read in two parts */
820 writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
821 while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
822 ;
823 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
824 res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
825 res <<= 32;
826 res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
827#endif
828 } else {
829 res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
830 }
831
832 return res;
833}
834
835static void arm_ccn_pmu_event_update(struct perf_event *event)
836{
837 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
838 struct hw_perf_event *hw = &event->hw;
839 u64 prev_count, new_count, mask;
840
841 do {
842 prev_count = local64_read(&hw->prev_count);
843 new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
844 } while (local64_xchg(&hw->prev_count, new_count) != prev_count);
845
846 mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
847
848 local64_add((new_count - prev_count) & mask, &event->count);
849}
850
851static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
852{
853 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
854 struct hw_perf_event *hw = &event->hw;
855 struct arm_ccn_component *xp;
856 u32 val, dt_cfg;
857
858 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
859 xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
860 else
861 xp = &ccn->xp[arm_ccn_node_to_xp(
862 CCN_CONFIG_NODE(event->attr.config))];
863
864 if (enable)
865 dt_cfg = hw->event_base;
866 else
867 dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
868
869 spin_lock(&ccn->dt.config_lock);
870
871 val = readl(xp->base + CCN_XP_DT_CONFIG);
872 val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
873 CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
874 val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
875 writel(val, xp->base + CCN_XP_DT_CONFIG);
876
877 spin_unlock(&ccn->dt.config_lock);
878}
879
880static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
881{
882 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
883 struct hw_perf_event *hw = &event->hw;
884
885 local64_set(&event->hw.prev_count,
886 arm_ccn_pmu_read_counter(ccn, hw->idx));
887 hw->state = 0;
888
Pawel Mollffa41522015-04-16 12:14:35 +0100889 /*
890 * Pin the timer, so that the overflows are handled by the chosen
891 * event->cpu (this is the same one as presented in "cpumask"
892 * attribute).
893 */
894 if (!ccn->irq)
895 __hrtimer_start_range_ns(&ccn->dt.hrtimer,
896 arm_ccn_pmu_timer_period(), 0,
897 HRTIMER_MODE_REL_PINNED, 0);
Pawel Molla33b0da2014-07-22 18:32:59 +0100898
899 /* Set the DT bus input, engaging the counter */
900 arm_ccn_pmu_xp_dt_config(event, 1);
901}
902
903static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
904{
905 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
906 struct hw_perf_event *hw = &event->hw;
907 u64 timeout;
908
909 /* Disable counting, setting the DT bus to pass-through mode */
910 arm_ccn_pmu_xp_dt_config(event, 0);
911
Pawel Mollffa41522015-04-16 12:14:35 +0100912 if (!ccn->irq)
Pawel Molla33b0da2014-07-22 18:32:59 +0100913 hrtimer_cancel(&ccn->dt.hrtimer);
914
915 /* Let the DT bus drain */
916 timeout = arm_ccn_pmu_read_counter(ccn, CCN_IDX_PMU_CYCLE_COUNTER) +
917 ccn->num_xps;
918 while (arm_ccn_pmu_read_counter(ccn, CCN_IDX_PMU_CYCLE_COUNTER) <
919 timeout)
920 cpu_relax();
921
922 if (flags & PERF_EF_UPDATE)
923 arm_ccn_pmu_event_update(event);
924
925 hw->state |= PERF_HES_STOPPED;
926}
927
928static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
929{
930 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
931 struct hw_perf_event *hw = &event->hw;
932 struct arm_ccn_component *source =
933 ccn->dt.pmu_counters[hw->idx].source;
934 unsigned long wp = hw->config_base;
935 u32 val;
936 u64 cmp_l = event->attr.config1;
937 u64 cmp_h = event->attr.config2;
938 u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
939 u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
940
941 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
942
943 /* Direction (RX/TX), device (port) & virtual channel */
944 val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
945 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
946 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
947 val |= CCN_CONFIG_DIR(event->attr.config) <<
948 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
949 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
950 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
951 val |= CCN_CONFIG_PORT(event->attr.config) <<
952 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
953 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
954 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
955 val |= CCN_CONFIG_VC(event->attr.config) <<
956 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
957 writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
958
959 /* Comparison values */
960 writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
961 writel((cmp_l >> 32) & 0xefffffff,
962 source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
963 writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
964 writel((cmp_h >> 32) & 0x0fffffff,
965 source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
966
967 /* Mask */
968 writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
969 writel((mask_l >> 32) & 0xefffffff,
970 source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
971 writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
972 writel((mask_h >> 32) & 0x0fffffff,
973 source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
974}
975
976static void arm_ccn_pmu_xp_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 struct arm_ccn_component *source =
981 ccn->dt.pmu_counters[hw->idx].source;
982 u32 val, id;
983
984 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
985
986 id = (CCN_CONFIG_VC(event->attr.config) << 4) |
987 (CCN_CONFIG_PORT(event->attr.config) << 3) |
988 (CCN_CONFIG_EVENT(event->attr.config) << 0);
989
990 val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
991 val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
992 CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
993 val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
994 writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
995}
996
997static void arm_ccn_pmu_node_event_config(struct perf_event *event)
998{
999 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1000 struct hw_perf_event *hw = &event->hw;
1001 struct arm_ccn_component *source =
1002 ccn->dt.pmu_counters[hw->idx].source;
1003 u32 type = CCN_CONFIG_TYPE(event->attr.config);
1004 u32 val, port;
1005
1006 port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
1007 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
1008 hw->config_base);
1009
1010 /* These *_event_sel regs should be identical, but let's make sure... */
1011 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
1012 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
1013 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
1014 CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
1015 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
1016 CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
1017 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
1018 CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
1019 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
1020 CCN_RNI_PMU_EVENT_SEL__ID__MASK);
1021 if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
1022 !arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
1023 return;
1024
1025 /* Set the event id for the pre-allocated counter */
1026 val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
1027 val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
1028 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
1029 val |= CCN_CONFIG_EVENT(event->attr.config) <<
1030 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
1031 writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
1032}
1033
1034static void arm_ccn_pmu_event_config(struct perf_event *event)
1035{
1036 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1037 struct hw_perf_event *hw = &event->hw;
1038 u32 xp, offset, val;
1039
1040 /* Cycle counter requires no setup */
1041 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
1042 return;
1043
1044 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
1045 xp = CCN_CONFIG_XP(event->attr.config);
1046 else
1047 xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
1048
1049 spin_lock(&ccn->dt.config_lock);
1050
1051 /* Set the DT bus "distance" register */
1052 offset = (hw->idx / 4) * 4;
1053 val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1054 val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
1055 CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
1056 val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
1057 writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1058
1059 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
1060 if (CCN_CONFIG_EVENT(event->attr.config) ==
1061 CCN_EVENT_WATCHPOINT)
1062 arm_ccn_pmu_xp_watchpoint_config(event);
1063 else
1064 arm_ccn_pmu_xp_event_config(event);
1065 } else {
1066 arm_ccn_pmu_node_event_config(event);
1067 }
1068
1069 spin_unlock(&ccn->dt.config_lock);
1070}
1071
1072static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
1073{
1074 struct hw_perf_event *hw = &event->hw;
1075
1076 arm_ccn_pmu_event_config(event);
1077
1078 hw->state = PERF_HES_STOPPED;
1079
1080 if (flags & PERF_EF_START)
1081 arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
1082
1083 return 0;
1084}
1085
1086static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
1087{
1088 arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
Pawel Molla33b0da2014-07-22 18:32:59 +01001089}
1090
1091static void arm_ccn_pmu_event_read(struct perf_event *event)
1092{
1093 arm_ccn_pmu_event_update(event);
1094}
1095
1096static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
1097{
1098 u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
1099 int idx;
1100
1101 if (!pmovsr)
1102 return IRQ_NONE;
1103
1104 writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
1105
1106 BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
1107
1108 for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
1109 struct perf_event *event = dt->pmu_counters[idx].event;
1110 int overflowed = pmovsr & BIT(idx);
1111
Pawel Mollfa637bf2014-09-15 15:33:48 +01001112 WARN_ON_ONCE(overflowed && !event &&
1113 idx != CCN_IDX_PMU_CYCLE_COUNTER);
Pawel Molla33b0da2014-07-22 18:32:59 +01001114
1115 if (!event || !overflowed)
1116 continue;
1117
1118 arm_ccn_pmu_event_update(event);
1119 }
1120
1121 return IRQ_HANDLED;
1122}
1123
1124static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
1125{
1126 struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
1127 hrtimer);
1128 unsigned long flags;
1129
1130 local_irq_save(flags);
1131 arm_ccn_pmu_overflow_handler(dt);
1132 local_irq_restore(flags);
1133
1134 hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
1135 return HRTIMER_RESTART;
1136}
1137
1138
Pawel Mollffa41522015-04-16 12:14:35 +01001139static int arm_ccn_pmu_cpu_notifier(struct notifier_block *nb,
1140 unsigned long action, void *hcpu)
1141{
1142 struct arm_ccn_dt *dt = container_of(nb, struct arm_ccn_dt, cpu_nb);
1143 struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
1144 unsigned int cpu = (long)hcpu; /* for (long) see kernel/cpu.c */
1145 unsigned int target;
1146
1147 switch (action & ~CPU_TASKS_FROZEN) {
1148 case CPU_DOWN_PREPARE:
1149 if (!cpumask_test_and_clear_cpu(cpu, &dt->cpu))
1150 break;
1151 target = cpumask_any_but(cpu_online_mask, cpu);
1152 if (target < 0)
1153 break;
1154 perf_pmu_migrate_context(&dt->pmu, cpu, target);
1155 cpumask_set_cpu(target, &dt->cpu);
1156 WARN_ON(irq_set_affinity(ccn->irq, &dt->cpu) != 0);
1157 default:
1158 break;
1159 }
1160
1161 return NOTIFY_OK;
1162}
1163
1164
Pawel Molla33b0da2014-07-22 18:32:59 +01001165static DEFINE_IDA(arm_ccn_pmu_ida);
1166
1167static int arm_ccn_pmu_init(struct arm_ccn *ccn)
1168{
1169 int i;
1170 char *name;
Pawel Mollffa41522015-04-16 12:14:35 +01001171 int err;
Pawel Molla33b0da2014-07-22 18:32:59 +01001172
1173 /* Initialize DT subsystem */
1174 ccn->dt.base = ccn->base + CCN_REGION_SIZE;
1175 spin_lock_init(&ccn->dt.config_lock);
Pawel Mollfa637bf2014-09-15 15:33:48 +01001176 writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
Pawel Molla33b0da2014-07-22 18:32:59 +01001177 writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
1178 writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
1179 ccn->dt.base + CCN_DT_PMCR);
1180 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
1181 for (i = 0; i < ccn->num_xps; i++) {
1182 writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
1183 writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1184 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
1185 (CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1186 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
1187 CCN_XP_DT_CONTROL__DT_ENABLE,
1188 ccn->xp[i].base + CCN_XP_DT_CONTROL);
1189 }
1190 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
1191 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
1192 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
1193 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
1194 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
1195 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
1196 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
1197 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
1198
1199 /* Get a convenient /sys/event_source/devices/ name */
1200 ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
1201 if (ccn->dt.id == 0) {
1202 name = "ccn";
1203 } else {
1204 int len = snprintf(NULL, 0, "ccn_%d", ccn->dt.id);
1205
1206 name = devm_kzalloc(ccn->dev, len + 1, GFP_KERNEL);
1207 snprintf(name, len + 1, "ccn_%d", ccn->dt.id);
1208 }
1209
1210 /* Perf driver registration */
1211 ccn->dt.pmu = (struct pmu) {
1212 .attr_groups = arm_ccn_pmu_attr_groups,
1213 .task_ctx_nr = perf_invalid_context,
1214 .event_init = arm_ccn_pmu_event_init,
1215 .add = arm_ccn_pmu_event_add,
1216 .del = arm_ccn_pmu_event_del,
1217 .start = arm_ccn_pmu_event_start,
1218 .stop = arm_ccn_pmu_event_stop,
1219 .read = arm_ccn_pmu_event_read,
1220 };
1221
1222 /* No overflow interrupt? Have to use a timer instead. */
Pawel Mollffa41522015-04-16 12:14:35 +01001223 if (!ccn->irq) {
Pawel Molla33b0da2014-07-22 18:32:59 +01001224 dev_info(ccn->dev, "No access to interrupts, using timer.\n");
1225 hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
1226 HRTIMER_MODE_REL);
1227 ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
1228 }
1229
Pawel Mollffa41522015-04-16 12:14:35 +01001230 /* Pick one CPU which we will use to collect data from CCN... */
1231 cpumask_set_cpu(smp_processor_id(), &ccn->dt.cpu);
1232
1233 /*
1234 * ... and change the selection when it goes offline. Priority is
1235 * picked to have a chance to migrate events before perf is notified.
1236 */
1237 ccn->dt.cpu_nb.notifier_call = arm_ccn_pmu_cpu_notifier;
1238 ccn->dt.cpu_nb.priority = CPU_PRI_PERF + 1,
1239 err = register_cpu_notifier(&ccn->dt.cpu_nb);
1240 if (err)
1241 goto error_cpu_notifier;
1242
1243 /* Also make sure that the overflow interrupt is handled by this CPU */
1244 if (ccn->irq) {
1245 err = irq_set_affinity(ccn->irq, &ccn->dt.cpu);
1246 if (err) {
1247 dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
1248 goto error_set_affinity;
1249 }
1250 }
1251
1252 err = perf_pmu_register(&ccn->dt.pmu, name, -1);
1253 if (err)
1254 goto error_pmu_register;
1255
1256 return 0;
1257
1258error_pmu_register:
1259error_set_affinity:
1260 unregister_cpu_notifier(&ccn->dt.cpu_nb);
1261error_cpu_notifier:
1262 ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1263 for (i = 0; i < ccn->num_xps; i++)
1264 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1265 writel(0, ccn->dt.base + CCN_DT_PMCR);
1266 return err;
Pawel Molla33b0da2014-07-22 18:32:59 +01001267}
1268
1269static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
1270{
1271 int i;
1272
Pawel Mollffa41522015-04-16 12:14:35 +01001273 irq_set_affinity(ccn->irq, cpu_possible_mask);
1274 unregister_cpu_notifier(&ccn->dt.cpu_nb);
Pawel Molla33b0da2014-07-22 18:32:59 +01001275 for (i = 0; i < ccn->num_xps; i++)
1276 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1277 writel(0, ccn->dt.base + CCN_DT_PMCR);
1278 perf_pmu_unregister(&ccn->dt.pmu);
1279 ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1280}
1281
1282
1283static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
1284 int (*callback)(struct arm_ccn *ccn, int region,
1285 void __iomem *base, u32 type, u32 id))
1286{
1287 int region;
1288
1289 for (region = 0; region < CCN_NUM_REGIONS; region++) {
1290 u32 val, type, id;
1291 void __iomem *base;
1292 int err;
1293
1294 val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
1295 4 * (region / 32));
1296 if (!(val & (1 << (region % 32))))
1297 continue;
1298
1299 base = ccn->base + region * CCN_REGION_SIZE;
1300 val = readl(base + CCN_ALL_OLY_ID);
1301 type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
1302 CCN_ALL_OLY_ID__OLY_ID__MASK;
1303 id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
1304 CCN_ALL_OLY_ID__NODE_ID__MASK;
1305
1306 err = callback(ccn, region, base, type, id);
1307 if (err)
1308 return err;
1309 }
1310
1311 return 0;
1312}
1313
1314static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
1315 void __iomem *base, u32 type, u32 id)
1316{
1317
1318 if (type == CCN_TYPE_XP && id >= ccn->num_xps)
1319 ccn->num_xps = id + 1;
1320 else if (id >= ccn->num_nodes)
1321 ccn->num_nodes = id + 1;
1322
1323 return 0;
1324}
1325
1326static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
1327 void __iomem *base, u32 type, u32 id)
1328{
1329 struct arm_ccn_component *component;
1330
1331 dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
1332
1333 switch (type) {
1334 case CCN_TYPE_MN:
1335 case CCN_TYPE_DT:
1336 return 0;
1337 case CCN_TYPE_XP:
1338 component = &ccn->xp[id];
1339 break;
1340 case CCN_TYPE_SBSX:
1341 ccn->sbsx_present = 1;
1342 component = &ccn->node[id];
1343 break;
1344 case CCN_TYPE_SBAS:
1345 ccn->sbas_present = 1;
1346 /* Fall-through */
1347 default:
1348 component = &ccn->node[id];
1349 break;
1350 }
1351
1352 component->base = base;
1353 component->type = type;
1354
1355 return 0;
1356}
1357
1358
1359static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
1360 const u32 *err_sig_val)
1361{
1362 /* This should be really handled by firmware... */
1363 dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
1364 err_sig_val[5], err_sig_val[4], err_sig_val[3],
1365 err_sig_val[2], err_sig_val[1], err_sig_val[0]);
1366 dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
1367 writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
1368 ccn->base + CCN_MN_ERRINT_STATUS);
1369
1370 return IRQ_HANDLED;
1371}
1372
1373
1374static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
1375{
1376 irqreturn_t res = IRQ_NONE;
1377 struct arm_ccn *ccn = dev_id;
1378 u32 err_sig_val[6];
1379 u32 err_or;
1380 int i;
1381
1382 /* PMU overflow is a special case */
1383 err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
1384 if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
1385 err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
1386 res = arm_ccn_pmu_overflow_handler(&ccn->dt);
1387 }
1388
1389 /* Have to read all err_sig_vals to clear them */
1390 for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
1391 err_sig_val[i] = readl(ccn->base +
1392 CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
1393 err_or |= err_sig_val[i];
1394 }
1395 if (err_or)
1396 res |= arm_ccn_error_handler(ccn, err_sig_val);
1397
1398 if (res != IRQ_NONE)
1399 writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
1400 ccn->base + CCN_MN_ERRINT_STATUS);
1401
1402 return res;
1403}
1404
1405
1406static int arm_ccn_probe(struct platform_device *pdev)
1407{
1408 struct arm_ccn *ccn;
1409 struct resource *res;
Pawel Mollffa41522015-04-16 12:14:35 +01001410 unsigned int irq;
Pawel Molla33b0da2014-07-22 18:32:59 +01001411 int err;
1412
1413 ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
1414 if (!ccn)
1415 return -ENOMEM;
1416 ccn->dev = &pdev->dev;
1417 platform_set_drvdata(pdev, ccn);
1418
1419 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1420 if (!res)
1421 return -EINVAL;
1422
1423 if (!devm_request_mem_region(ccn->dev, res->start,
1424 resource_size(res), pdev->name))
1425 return -EBUSY;
1426
1427 ccn->base = devm_ioremap(ccn->dev, res->start,
1428 resource_size(res));
1429 if (!ccn->base)
1430 return -EFAULT;
1431
1432 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1433 if (!res)
1434 return -EINVAL;
Pawel Mollffa41522015-04-16 12:14:35 +01001435 irq = res->start;
Pawel Molla33b0da2014-07-22 18:32:59 +01001436
1437 /* Check if we can use the interrupt */
1438 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
1439 ccn->base + CCN_MN_ERRINT_STATUS);
1440 if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
1441 CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
1442 /* Can set 'disable' bits, so can acknowledge interrupts */
1443 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
1444 ccn->base + CCN_MN_ERRINT_STATUS);
Pawel Mollffa41522015-04-16 12:14:35 +01001445 err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler, 0,
1446 dev_name(ccn->dev), ccn);
Pawel Molla33b0da2014-07-22 18:32:59 +01001447 if (err)
1448 return err;
1449
Pawel Mollffa41522015-04-16 12:14:35 +01001450 ccn->irq = irq;
Pawel Molla33b0da2014-07-22 18:32:59 +01001451 }
1452
1453
1454 /* Build topology */
1455
1456 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
1457 if (err)
1458 return err;
1459
1460 ccn->node = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_nodes,
1461 GFP_KERNEL);
1462 ccn->xp = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_xps,
1463 GFP_KERNEL);
1464 if (!ccn->node || !ccn->xp)
1465 return -ENOMEM;
1466
1467 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
1468 if (err)
1469 return err;
1470
1471 return arm_ccn_pmu_init(ccn);
1472}
1473
1474static int arm_ccn_remove(struct platform_device *pdev)
1475{
1476 struct arm_ccn *ccn = platform_get_drvdata(pdev);
1477
1478 arm_ccn_pmu_cleanup(ccn);
1479
1480 return 0;
1481}
1482
1483static const struct of_device_id arm_ccn_match[] = {
1484 { .compatible = "arm,ccn-504", },
1485 {},
1486};
1487
1488static struct platform_driver arm_ccn_driver = {
1489 .driver = {
1490 .name = "arm-ccn",
1491 .of_match_table = arm_ccn_match,
1492 },
1493 .probe = arm_ccn_probe,
1494 .remove = arm_ccn_remove,
1495};
1496
1497static int __init arm_ccn_init(void)
1498{
1499 int i;
1500
1501 for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
1502 arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
1503
1504 return platform_driver_register(&arm_ccn_driver);
1505}
1506
1507static void __exit arm_ccn_exit(void)
1508{
1509 platform_driver_unregister(&arm_ccn_driver);
1510}
1511
1512module_init(arm_ccn_init);
1513module_exit(arm_ccn_exit);
1514
1515MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
1516MODULE_LICENSE("GPL");