blob: 3374dcb12f1e3d1c3c286ddbfa688e591a61e346 [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);
343 res += snprintf(buf + res, PAGE_SIZE - res, "\n");
344
345 return res;
346}
347
348static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
349 struct attribute *attr, int index)
350{
351 struct device *dev = kobj_to_dev(kobj);
352 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
353 struct device_attribute *dev_attr = container_of(attr,
354 struct device_attribute, attr);
355 struct arm_ccn_pmu_event *event = container_of(dev_attr,
356 struct arm_ccn_pmu_event, attr);
357
358 if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
359 return 0;
360 if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
361 return 0;
362
363 return attr->mode;
364}
365
366static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
367 CCN_EVENT_MN(eobarrier, "dir=0,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
368 CCN_EVENT_MN(ecbarrier, "dir=0,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
369 CCN_EVENT_MN(dvmop, "dir=0,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
370 CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
371 CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
372 CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
373 CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
374 CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
375 CCN_IDX_MASK_ORDER),
376 CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
377 CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
378 CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
379 CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
380 CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
381 CCN_IDX_MASK_ORDER),
382 CCN_EVENT_HNF(cache_miss, 0x1),
383 CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
384 CCN_EVENT_HNF(cache_fill, 0x3),
385 CCN_EVENT_HNF(pocq_retry, 0x4),
386 CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
387 CCN_EVENT_HNF(sf_hit, 0x6),
388 CCN_EVENT_HNF(sf_evictions, 0x7),
389 CCN_EVENT_HNF(snoops_sent, 0x8),
390 CCN_EVENT_HNF(snoops_broadcast, 0x9),
391 CCN_EVENT_HNF(l3_eviction, 0xa),
392 CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
393 CCN_EVENT_HNF(mc_retries, 0xc),
394 CCN_EVENT_HNF(mc_reqs, 0xd),
395 CCN_EVENT_HNF(qos_hh_retry, 0xe),
396 CCN_EVENT_RNI(rdata_beats_p0, 0x1),
397 CCN_EVENT_RNI(rdata_beats_p1, 0x2),
398 CCN_EVENT_RNI(rdata_beats_p2, 0x3),
399 CCN_EVENT_RNI(rxdat_flits, 0x4),
400 CCN_EVENT_RNI(txdat_flits, 0x5),
401 CCN_EVENT_RNI(txreq_flits, 0x6),
402 CCN_EVENT_RNI(txreq_flits_retried, 0x7),
403 CCN_EVENT_RNI(rrt_full, 0x8),
404 CCN_EVENT_RNI(wrt_full, 0x9),
405 CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
406 CCN_EVENT_XP(upload_starvation, 0x1),
407 CCN_EVENT_XP(download_starvation, 0x2),
408 CCN_EVENT_XP(respin, 0x3),
409 CCN_EVENT_XP(valid_flit, 0x4),
410 CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
411 CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
412 CCN_EVENT_SBAS(rxdat_flits, 0x4),
413 CCN_EVENT_SBAS(txdat_flits, 0x5),
414 CCN_EVENT_SBAS(txreq_flits, 0x6),
415 CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
416 CCN_EVENT_SBAS(rrt_full, 0x8),
417 CCN_EVENT_SBAS(wrt_full, 0x9),
418 CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
419 CCN_EVENT_CYCLES(cycles),
420};
421
422/* Populated in arm_ccn_init() */
423static struct attribute
424 *arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
425
426static struct attribute_group arm_ccn_pmu_events_attr_group = {
427 .name = "events",
428 .is_visible = arm_ccn_pmu_events_is_visible,
429 .attrs = arm_ccn_pmu_events_attrs,
430};
431
432
433static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
434{
435 unsigned long i;
436
437 if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
438 return NULL;
439 i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
440
441 switch (name[1]) {
442 case 'l':
443 return &ccn->dt.cmp_mask[i].l;
444 case 'h':
445 return &ccn->dt.cmp_mask[i].h;
446 default:
447 return NULL;
448 }
449}
450
451static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
454 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
455 u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
456
457 return mask ? snprintf(buf, PAGE_SIZE, "0x%016llx\n", *mask) : -EINVAL;
458}
459
460static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
461 struct device_attribute *attr, const char *buf, size_t count)
462{
463 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
464 u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
465 int err = -EINVAL;
466
467 if (mask)
468 err = kstrtoull(buf, 0, mask);
469
470 return err ? err : count;
471}
472
473#define CCN_CMP_MASK_ATTR(_name) \
474 struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
475 __ATTR(_name, S_IRUGO | S_IWUSR, \
476 arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
477
478#define CCN_CMP_MASK_ATTR_RO(_name) \
479 struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
480 __ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
481
482static CCN_CMP_MASK_ATTR(0l);
483static CCN_CMP_MASK_ATTR(0h);
484static CCN_CMP_MASK_ATTR(1l);
485static CCN_CMP_MASK_ATTR(1h);
486static CCN_CMP_MASK_ATTR(2l);
487static CCN_CMP_MASK_ATTR(2h);
488static CCN_CMP_MASK_ATTR(3l);
489static CCN_CMP_MASK_ATTR(3h);
490static CCN_CMP_MASK_ATTR(4l);
491static CCN_CMP_MASK_ATTR(4h);
492static CCN_CMP_MASK_ATTR(5l);
493static CCN_CMP_MASK_ATTR(5h);
494static CCN_CMP_MASK_ATTR(6l);
495static CCN_CMP_MASK_ATTR(6h);
496static CCN_CMP_MASK_ATTR(7l);
497static CCN_CMP_MASK_ATTR(7h);
498static CCN_CMP_MASK_ATTR_RO(8l);
499static CCN_CMP_MASK_ATTR_RO(8h);
500static CCN_CMP_MASK_ATTR_RO(9l);
501static CCN_CMP_MASK_ATTR_RO(9h);
502static CCN_CMP_MASK_ATTR_RO(al);
503static CCN_CMP_MASK_ATTR_RO(ah);
504static CCN_CMP_MASK_ATTR_RO(bl);
505static CCN_CMP_MASK_ATTR_RO(bh);
506
507static struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
508 &arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
509 &arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
510 &arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
511 &arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
512 &arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
513 &arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
514 &arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
515 &arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
516 &arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
517 &arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
518 &arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
519 &arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
520 NULL
521};
522
523static struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
524 .name = "cmp_mask",
525 .attrs = arm_ccn_pmu_cmp_mask_attrs,
526};
527
Pawel Mollffa41522015-04-16 12:14:35 +0100528static ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
529 struct device_attribute *attr, char *buf)
530{
531 struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
532
533 return cpumap_print_to_pagebuf(true, buf, &ccn->dt.cpu);
534}
535
536static struct device_attribute arm_ccn_pmu_cpumask_attr =
537 __ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
538
539static struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
540 &arm_ccn_pmu_cpumask_attr.attr,
541 NULL,
542};
543
544static struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
545 .attrs = arm_ccn_pmu_cpumask_attrs,
546};
Pawel Molla33b0da2014-07-22 18:32:59 +0100547
548/*
549 * Default poll period is 10ms, which is way over the top anyway,
550 * as in the worst case scenario (an event every cycle), with 1GHz
551 * clocked bus, the smallest, 32 bit counter will overflow in
552 * more than 4s.
553 */
554static unsigned int arm_ccn_pmu_poll_period_us = 10000;
555module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
556 S_IRUGO | S_IWUSR);
557
558static ktime_t arm_ccn_pmu_timer_period(void)
559{
560 return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
561}
562
563
564static const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
565 &arm_ccn_pmu_events_attr_group,
566 &arm_ccn_pmu_format_attr_group,
567 &arm_ccn_pmu_cmp_mask_attr_group,
Pawel Mollffa41522015-04-16 12:14:35 +0100568 &arm_ccn_pmu_cpumask_attr_group,
Pawel Molla33b0da2014-07-22 18:32:59 +0100569 NULL
570};
571
572
573static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
574{
575 int bit;
576
577 do {
578 bit = find_first_zero_bit(bitmap, size);
579 if (bit >= size)
580 return -EAGAIN;
581 } while (test_and_set_bit(bit, bitmap));
582
583 return bit;
584}
585
586/* All RN-I and RN-D nodes have identical PMUs */
587static int arm_ccn_pmu_type_eq(u32 a, u32 b)
588{
589 if (a == b)
590 return 1;
591
592 switch (a) {
593 case CCN_TYPE_RNI_1P:
594 case CCN_TYPE_RNI_2P:
595 case CCN_TYPE_RNI_3P:
596 case CCN_TYPE_RND_1P:
597 case CCN_TYPE_RND_2P:
598 case CCN_TYPE_RND_3P:
599 switch (b) {
600 case CCN_TYPE_RNI_1P:
601 case CCN_TYPE_RNI_2P:
602 case CCN_TYPE_RNI_3P:
603 case CCN_TYPE_RND_1P:
604 case CCN_TYPE_RND_2P:
605 case CCN_TYPE_RND_3P:
606 return 1;
607 }
608 break;
609 }
610
611 return 0;
612}
613
Pawel Moll8fb22262014-09-02 16:26:11 +0100614static void arm_ccn_pmu_event_destroy(struct perf_event *event)
615{
616 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
617 struct hw_perf_event *hw = &event->hw;
618
619 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
620 clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
621 } else {
622 struct arm_ccn_component *source =
623 ccn->dt.pmu_counters[hw->idx].source;
624
625 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
626 CCN_CONFIG_EVENT(event->attr.config) ==
627 CCN_EVENT_WATCHPOINT)
628 clear_bit(hw->config_base, source->xp.dt_cmp_mask);
629 else
630 clear_bit(hw->config_base, source->pmu_events_mask);
631 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
632 }
633
634 ccn->dt.pmu_counters[hw->idx].source = NULL;
635 ccn->dt.pmu_counters[hw->idx].event = NULL;
636}
637
Pawel Molla33b0da2014-07-22 18:32:59 +0100638static int arm_ccn_pmu_event_init(struct perf_event *event)
639{
640 struct arm_ccn *ccn;
641 struct hw_perf_event *hw = &event->hw;
642 u32 node_xp, type, event_id;
Pawel Moll3e528cb2014-07-31 16:16:37 +0100643 int valid, bit;
Pawel Molla33b0da2014-07-22 18:32:59 +0100644 struct arm_ccn_component *source;
645 int i;
646
647 if (event->attr.type != event->pmu->type)
648 return -ENOENT;
649
650 ccn = pmu_to_arm_ccn(event->pmu);
Pawel Moll8fb22262014-09-02 16:26:11 +0100651 event->destroy = arm_ccn_pmu_event_destroy;
Pawel Molla33b0da2014-07-22 18:32:59 +0100652
653 if (hw->sample_period) {
654 dev_warn(ccn->dev, "Sampling not supported!\n");
655 return -EOPNOTSUPP;
656 }
657
658 if (has_branch_stack(event) || event->attr.exclude_user ||
659 event->attr.exclude_kernel || event->attr.exclude_hv ||
660 event->attr.exclude_idle) {
661 dev_warn(ccn->dev, "Can't exclude execution levels!\n");
662 return -EOPNOTSUPP;
663 }
664
665 if (event->cpu < 0) {
666 dev_warn(ccn->dev, "Can't provide per-task data!\n");
667 return -EOPNOTSUPP;
668 }
Pawel Mollffa41522015-04-16 12:14:35 +0100669 /*
670 * Many perf core operations (eg. events rotation) operate on a
671 * single CPU context. This is obvious for CPU PMUs, where one
672 * expects the same sets of events being observed on all CPUs,
673 * but can lead to issues for off-core PMUs, like CCN, where each
674 * event could be theoretically assigned to a different CPU. To
675 * mitigate this, we enforce CPU assignment to one, selected
676 * processor (the one described in the "cpumask" attribute).
677 */
678 event->cpu = cpumask_first(&ccn->dt.cpu);
Pawel Molla33b0da2014-07-22 18:32:59 +0100679
680 node_xp = CCN_CONFIG_NODE(event->attr.config);
681 type = CCN_CONFIG_TYPE(event->attr.config);
682 event_id = CCN_CONFIG_EVENT(event->attr.config);
683
684 /* Validate node/xp vs topology */
685 switch (type) {
686 case CCN_TYPE_XP:
687 if (node_xp >= ccn->num_xps) {
688 dev_warn(ccn->dev, "Invalid XP ID %d!\n", node_xp);
689 return -EINVAL;
690 }
691 break;
692 case CCN_TYPE_CYCLES:
693 break;
694 default:
695 if (node_xp >= ccn->num_nodes) {
696 dev_warn(ccn->dev, "Invalid node ID %d!\n", node_xp);
697 return -EINVAL;
698 }
699 if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
700 dev_warn(ccn->dev, "Invalid type 0x%x for node %d!\n",
701 type, node_xp);
702 return -EINVAL;
703 }
704 break;
705 }
706
707 /* Validate event ID vs available for the type */
708 for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
709 i++) {
710 struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
711 u32 port = CCN_CONFIG_PORT(event->attr.config);
712 u32 vc = CCN_CONFIG_VC(event->attr.config);
713
714 if (!arm_ccn_pmu_type_eq(type, e->type))
715 continue;
716 if (event_id != e->event)
717 continue;
718 if (e->num_ports && port >= e->num_ports) {
719 dev_warn(ccn->dev, "Invalid port %d for node/XP %d!\n",
720 port, node_xp);
721 return -EINVAL;
722 }
723 if (e->num_vcs && vc >= e->num_vcs) {
724 dev_warn(ccn->dev, "Invalid vc %d for node/XP %d!\n",
Pawel Mollbf87bb12014-08-18 18:20:49 +0100725 vc, node_xp);
Pawel Molla33b0da2014-07-22 18:32:59 +0100726 return -EINVAL;
727 }
728 valid = 1;
729 }
730 if (!valid) {
731 dev_warn(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
732 event_id, node_xp);
733 return -EINVAL;
734 }
735
736 /* Watchpoint-based event for a node is actually set on XP */
737 if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
738 u32 port;
739
740 type = CCN_TYPE_XP;
741 port = arm_ccn_node_to_xp_port(node_xp);
742 node_xp = arm_ccn_node_to_xp(node_xp);
743
744 arm_ccn_pmu_config_set(&event->attr.config,
745 node_xp, type, port);
746 }
747
748 /* Allocate the cycle counter */
749 if (type == CCN_TYPE_CYCLES) {
750 if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
751 ccn->dt.pmu_counters_mask))
752 return -EAGAIN;
753
754 hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
755 ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
756
757 return 0;
758 }
759
760 /* Allocate an event counter */
761 hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
762 CCN_NUM_PMU_EVENT_COUNTERS);
763 if (hw->idx < 0) {
764 dev_warn(ccn->dev, "No more counters available!\n");
765 return -EAGAIN;
766 }
767
768 if (type == CCN_TYPE_XP)
769 source = &ccn->xp[node_xp];
770 else
771 source = &ccn->node[node_xp];
772 ccn->dt.pmu_counters[hw->idx].source = source;
773
774 /* Allocate an event source or a watchpoint */
775 if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
Pawel Moll3e528cb2014-07-31 16:16:37 +0100776 bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
Pawel Molla33b0da2014-07-22 18:32:59 +0100777 CCN_NUM_XP_WATCHPOINTS);
778 else
Pawel Moll3e528cb2014-07-31 16:16:37 +0100779 bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
Pawel Molla33b0da2014-07-22 18:32:59 +0100780 CCN_NUM_PMU_EVENTS);
Pawel Moll3e528cb2014-07-31 16:16:37 +0100781 if (bit < 0) {
Pawel Molla33b0da2014-07-22 18:32:59 +0100782 dev_warn(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
783 node_xp);
784 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
785 return -EAGAIN;
786 }
Pawel Moll3e528cb2014-07-31 16:16:37 +0100787 hw->config_base = bit;
Pawel Molla33b0da2014-07-22 18:32:59 +0100788
789 ccn->dt.pmu_counters[hw->idx].event = event;
790
791 return 0;
792}
793
Pawel Molla33b0da2014-07-22 18:32:59 +0100794static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
795{
796 u64 res;
797
798 if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
799#ifdef readq
800 res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
801#else
802 /* 40 bit counter, can do snapshot and read in two parts */
803 writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
804 while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
805 ;
806 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
807 res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
808 res <<= 32;
809 res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
810#endif
811 } else {
812 res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
813 }
814
815 return res;
816}
817
818static void arm_ccn_pmu_event_update(struct perf_event *event)
819{
820 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
821 struct hw_perf_event *hw = &event->hw;
822 u64 prev_count, new_count, mask;
823
824 do {
825 prev_count = local64_read(&hw->prev_count);
826 new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
827 } while (local64_xchg(&hw->prev_count, new_count) != prev_count);
828
829 mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
830
831 local64_add((new_count - prev_count) & mask, &event->count);
832}
833
834static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
835{
836 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
837 struct hw_perf_event *hw = &event->hw;
838 struct arm_ccn_component *xp;
839 u32 val, dt_cfg;
840
841 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
842 xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
843 else
844 xp = &ccn->xp[arm_ccn_node_to_xp(
845 CCN_CONFIG_NODE(event->attr.config))];
846
847 if (enable)
848 dt_cfg = hw->event_base;
849 else
850 dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
851
852 spin_lock(&ccn->dt.config_lock);
853
854 val = readl(xp->base + CCN_XP_DT_CONFIG);
855 val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
856 CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
857 val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
858 writel(val, xp->base + CCN_XP_DT_CONFIG);
859
860 spin_unlock(&ccn->dt.config_lock);
861}
862
863static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
864{
865 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
866 struct hw_perf_event *hw = &event->hw;
867
868 local64_set(&event->hw.prev_count,
869 arm_ccn_pmu_read_counter(ccn, hw->idx));
870 hw->state = 0;
871
Pawel Mollffa41522015-04-16 12:14:35 +0100872 /*
873 * Pin the timer, so that the overflows are handled by the chosen
874 * event->cpu (this is the same one as presented in "cpumask"
875 * attribute).
876 */
877 if (!ccn->irq)
878 __hrtimer_start_range_ns(&ccn->dt.hrtimer,
879 arm_ccn_pmu_timer_period(), 0,
880 HRTIMER_MODE_REL_PINNED, 0);
Pawel Molla33b0da2014-07-22 18:32:59 +0100881
882 /* Set the DT bus input, engaging the counter */
883 arm_ccn_pmu_xp_dt_config(event, 1);
884}
885
886static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
887{
888 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
889 struct hw_perf_event *hw = &event->hw;
890 u64 timeout;
891
892 /* Disable counting, setting the DT bus to pass-through mode */
893 arm_ccn_pmu_xp_dt_config(event, 0);
894
Pawel Mollffa41522015-04-16 12:14:35 +0100895 if (!ccn->irq)
Pawel Molla33b0da2014-07-22 18:32:59 +0100896 hrtimer_cancel(&ccn->dt.hrtimer);
897
898 /* Let the DT bus drain */
899 timeout = arm_ccn_pmu_read_counter(ccn, CCN_IDX_PMU_CYCLE_COUNTER) +
900 ccn->num_xps;
901 while (arm_ccn_pmu_read_counter(ccn, CCN_IDX_PMU_CYCLE_COUNTER) <
902 timeout)
903 cpu_relax();
904
905 if (flags & PERF_EF_UPDATE)
906 arm_ccn_pmu_event_update(event);
907
908 hw->state |= PERF_HES_STOPPED;
909}
910
911static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
912{
913 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
914 struct hw_perf_event *hw = &event->hw;
915 struct arm_ccn_component *source =
916 ccn->dt.pmu_counters[hw->idx].source;
917 unsigned long wp = hw->config_base;
918 u32 val;
919 u64 cmp_l = event->attr.config1;
920 u64 cmp_h = event->attr.config2;
921 u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
922 u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
923
924 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
925
926 /* Direction (RX/TX), device (port) & virtual channel */
927 val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
928 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
929 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
930 val |= CCN_CONFIG_DIR(event->attr.config) <<
931 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
932 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
933 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
934 val |= CCN_CONFIG_PORT(event->attr.config) <<
935 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
936 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
937 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
938 val |= CCN_CONFIG_VC(event->attr.config) <<
939 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
940 writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
941
942 /* Comparison values */
943 writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
944 writel((cmp_l >> 32) & 0xefffffff,
945 source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
946 writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
947 writel((cmp_h >> 32) & 0x0fffffff,
948 source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
949
950 /* Mask */
951 writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
952 writel((mask_l >> 32) & 0xefffffff,
953 source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
954 writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
955 writel((mask_h >> 32) & 0x0fffffff,
956 source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
957}
958
959static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
960{
961 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
962 struct hw_perf_event *hw = &event->hw;
963 struct arm_ccn_component *source =
964 ccn->dt.pmu_counters[hw->idx].source;
965 u32 val, id;
966
967 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
968
969 id = (CCN_CONFIG_VC(event->attr.config) << 4) |
970 (CCN_CONFIG_PORT(event->attr.config) << 3) |
971 (CCN_CONFIG_EVENT(event->attr.config) << 0);
972
973 val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
974 val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
975 CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
976 val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
977 writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
978}
979
980static void arm_ccn_pmu_node_event_config(struct perf_event *event)
981{
982 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
983 struct hw_perf_event *hw = &event->hw;
984 struct arm_ccn_component *source =
985 ccn->dt.pmu_counters[hw->idx].source;
986 u32 type = CCN_CONFIG_TYPE(event->attr.config);
987 u32 val, port;
988
989 port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
990 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
991 hw->config_base);
992
993 /* These *_event_sel regs should be identical, but let's make sure... */
994 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
995 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
996 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
997 CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
998 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
999 CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
1000 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
1001 CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
1002 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
1003 CCN_RNI_PMU_EVENT_SEL__ID__MASK);
1004 if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
1005 !arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
1006 return;
1007
1008 /* Set the event id for the pre-allocated counter */
1009 val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
1010 val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
1011 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
1012 val |= CCN_CONFIG_EVENT(event->attr.config) <<
1013 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
1014 writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
1015}
1016
1017static void arm_ccn_pmu_event_config(struct perf_event *event)
1018{
1019 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1020 struct hw_perf_event *hw = &event->hw;
1021 u32 xp, offset, val;
1022
1023 /* Cycle counter requires no setup */
1024 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
1025 return;
1026
1027 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
1028 xp = CCN_CONFIG_XP(event->attr.config);
1029 else
1030 xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
1031
1032 spin_lock(&ccn->dt.config_lock);
1033
1034 /* Set the DT bus "distance" register */
1035 offset = (hw->idx / 4) * 4;
1036 val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1037 val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
1038 CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
1039 val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
1040 writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1041
1042 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
1043 if (CCN_CONFIG_EVENT(event->attr.config) ==
1044 CCN_EVENT_WATCHPOINT)
1045 arm_ccn_pmu_xp_watchpoint_config(event);
1046 else
1047 arm_ccn_pmu_xp_event_config(event);
1048 } else {
1049 arm_ccn_pmu_node_event_config(event);
1050 }
1051
1052 spin_unlock(&ccn->dt.config_lock);
1053}
1054
1055static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
1056{
1057 struct hw_perf_event *hw = &event->hw;
1058
1059 arm_ccn_pmu_event_config(event);
1060
1061 hw->state = PERF_HES_STOPPED;
1062
1063 if (flags & PERF_EF_START)
1064 arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
1065
1066 return 0;
1067}
1068
1069static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
1070{
1071 arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
Pawel Molla33b0da2014-07-22 18:32:59 +01001072}
1073
1074static void arm_ccn_pmu_event_read(struct perf_event *event)
1075{
1076 arm_ccn_pmu_event_update(event);
1077}
1078
1079static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
1080{
1081 u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
1082 int idx;
1083
1084 if (!pmovsr)
1085 return IRQ_NONE;
1086
1087 writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
1088
1089 BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
1090
1091 for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
1092 struct perf_event *event = dt->pmu_counters[idx].event;
1093 int overflowed = pmovsr & BIT(idx);
1094
Pawel Mollfa637bf2014-09-15 15:33:48 +01001095 WARN_ON_ONCE(overflowed && !event &&
1096 idx != CCN_IDX_PMU_CYCLE_COUNTER);
Pawel Molla33b0da2014-07-22 18:32:59 +01001097
1098 if (!event || !overflowed)
1099 continue;
1100
1101 arm_ccn_pmu_event_update(event);
1102 }
1103
1104 return IRQ_HANDLED;
1105}
1106
1107static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
1108{
1109 struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
1110 hrtimer);
1111 unsigned long flags;
1112
1113 local_irq_save(flags);
1114 arm_ccn_pmu_overflow_handler(dt);
1115 local_irq_restore(flags);
1116
1117 hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
1118 return HRTIMER_RESTART;
1119}
1120
1121
Pawel Mollffa41522015-04-16 12:14:35 +01001122static int arm_ccn_pmu_cpu_notifier(struct notifier_block *nb,
1123 unsigned long action, void *hcpu)
1124{
1125 struct arm_ccn_dt *dt = container_of(nb, struct arm_ccn_dt, cpu_nb);
1126 struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
1127 unsigned int cpu = (long)hcpu; /* for (long) see kernel/cpu.c */
1128 unsigned int target;
1129
1130 switch (action & ~CPU_TASKS_FROZEN) {
1131 case CPU_DOWN_PREPARE:
1132 if (!cpumask_test_and_clear_cpu(cpu, &dt->cpu))
1133 break;
1134 target = cpumask_any_but(cpu_online_mask, cpu);
1135 if (target < 0)
1136 break;
1137 perf_pmu_migrate_context(&dt->pmu, cpu, target);
1138 cpumask_set_cpu(target, &dt->cpu);
1139 WARN_ON(irq_set_affinity(ccn->irq, &dt->cpu) != 0);
1140 default:
1141 break;
1142 }
1143
1144 return NOTIFY_OK;
1145}
1146
1147
Pawel Molla33b0da2014-07-22 18:32:59 +01001148static DEFINE_IDA(arm_ccn_pmu_ida);
1149
1150static int arm_ccn_pmu_init(struct arm_ccn *ccn)
1151{
1152 int i;
1153 char *name;
Pawel Mollffa41522015-04-16 12:14:35 +01001154 int err;
Pawel Molla33b0da2014-07-22 18:32:59 +01001155
1156 /* Initialize DT subsystem */
1157 ccn->dt.base = ccn->base + CCN_REGION_SIZE;
1158 spin_lock_init(&ccn->dt.config_lock);
Pawel Mollfa637bf2014-09-15 15:33:48 +01001159 writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
Pawel Molla33b0da2014-07-22 18:32:59 +01001160 writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
1161 writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
1162 ccn->dt.base + CCN_DT_PMCR);
1163 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
1164 for (i = 0; i < ccn->num_xps; i++) {
1165 writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
1166 writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1167 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
1168 (CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1169 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
1170 CCN_XP_DT_CONTROL__DT_ENABLE,
1171 ccn->xp[i].base + CCN_XP_DT_CONTROL);
1172 }
1173 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
1174 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
1175 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
1176 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
1177 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
1178 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
1179 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
1180 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
1181
1182 /* Get a convenient /sys/event_source/devices/ name */
1183 ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
1184 if (ccn->dt.id == 0) {
1185 name = "ccn";
1186 } else {
1187 int len = snprintf(NULL, 0, "ccn_%d", ccn->dt.id);
1188
1189 name = devm_kzalloc(ccn->dev, len + 1, GFP_KERNEL);
1190 snprintf(name, len + 1, "ccn_%d", ccn->dt.id);
1191 }
1192
1193 /* Perf driver registration */
1194 ccn->dt.pmu = (struct pmu) {
1195 .attr_groups = arm_ccn_pmu_attr_groups,
1196 .task_ctx_nr = perf_invalid_context,
1197 .event_init = arm_ccn_pmu_event_init,
1198 .add = arm_ccn_pmu_event_add,
1199 .del = arm_ccn_pmu_event_del,
1200 .start = arm_ccn_pmu_event_start,
1201 .stop = arm_ccn_pmu_event_stop,
1202 .read = arm_ccn_pmu_event_read,
1203 };
1204
1205 /* No overflow interrupt? Have to use a timer instead. */
Pawel Mollffa41522015-04-16 12:14:35 +01001206 if (!ccn->irq) {
Pawel Molla33b0da2014-07-22 18:32:59 +01001207 dev_info(ccn->dev, "No access to interrupts, using timer.\n");
1208 hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
1209 HRTIMER_MODE_REL);
1210 ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
1211 }
1212
Pawel Mollffa41522015-04-16 12:14:35 +01001213 /* Pick one CPU which we will use to collect data from CCN... */
1214 cpumask_set_cpu(smp_processor_id(), &ccn->dt.cpu);
1215
1216 /*
1217 * ... and change the selection when it goes offline. Priority is
1218 * picked to have a chance to migrate events before perf is notified.
1219 */
1220 ccn->dt.cpu_nb.notifier_call = arm_ccn_pmu_cpu_notifier;
1221 ccn->dt.cpu_nb.priority = CPU_PRI_PERF + 1,
1222 err = register_cpu_notifier(&ccn->dt.cpu_nb);
1223 if (err)
1224 goto error_cpu_notifier;
1225
1226 /* Also make sure that the overflow interrupt is handled by this CPU */
1227 if (ccn->irq) {
1228 err = irq_set_affinity(ccn->irq, &ccn->dt.cpu);
1229 if (err) {
1230 dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
1231 goto error_set_affinity;
1232 }
1233 }
1234
1235 err = perf_pmu_register(&ccn->dt.pmu, name, -1);
1236 if (err)
1237 goto error_pmu_register;
1238
1239 return 0;
1240
1241error_pmu_register:
1242error_set_affinity:
1243 unregister_cpu_notifier(&ccn->dt.cpu_nb);
1244error_cpu_notifier:
1245 ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1246 for (i = 0; i < ccn->num_xps; i++)
1247 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1248 writel(0, ccn->dt.base + CCN_DT_PMCR);
1249 return err;
Pawel Molla33b0da2014-07-22 18:32:59 +01001250}
1251
1252static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
1253{
1254 int i;
1255
Pawel Mollffa41522015-04-16 12:14:35 +01001256 irq_set_affinity(ccn->irq, cpu_possible_mask);
1257 unregister_cpu_notifier(&ccn->dt.cpu_nb);
Pawel Molla33b0da2014-07-22 18:32:59 +01001258 for (i = 0; i < ccn->num_xps; i++)
1259 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1260 writel(0, ccn->dt.base + CCN_DT_PMCR);
1261 perf_pmu_unregister(&ccn->dt.pmu);
1262 ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1263}
1264
1265
1266static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
1267 int (*callback)(struct arm_ccn *ccn, int region,
1268 void __iomem *base, u32 type, u32 id))
1269{
1270 int region;
1271
1272 for (region = 0; region < CCN_NUM_REGIONS; region++) {
1273 u32 val, type, id;
1274 void __iomem *base;
1275 int err;
1276
1277 val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
1278 4 * (region / 32));
1279 if (!(val & (1 << (region % 32))))
1280 continue;
1281
1282 base = ccn->base + region * CCN_REGION_SIZE;
1283 val = readl(base + CCN_ALL_OLY_ID);
1284 type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
1285 CCN_ALL_OLY_ID__OLY_ID__MASK;
1286 id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
1287 CCN_ALL_OLY_ID__NODE_ID__MASK;
1288
1289 err = callback(ccn, region, base, type, id);
1290 if (err)
1291 return err;
1292 }
1293
1294 return 0;
1295}
1296
1297static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
1298 void __iomem *base, u32 type, u32 id)
1299{
1300
1301 if (type == CCN_TYPE_XP && id >= ccn->num_xps)
1302 ccn->num_xps = id + 1;
1303 else if (id >= ccn->num_nodes)
1304 ccn->num_nodes = id + 1;
1305
1306 return 0;
1307}
1308
1309static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
1310 void __iomem *base, u32 type, u32 id)
1311{
1312 struct arm_ccn_component *component;
1313
1314 dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
1315
1316 switch (type) {
1317 case CCN_TYPE_MN:
1318 case CCN_TYPE_DT:
1319 return 0;
1320 case CCN_TYPE_XP:
1321 component = &ccn->xp[id];
1322 break;
1323 case CCN_TYPE_SBSX:
1324 ccn->sbsx_present = 1;
1325 component = &ccn->node[id];
1326 break;
1327 case CCN_TYPE_SBAS:
1328 ccn->sbas_present = 1;
1329 /* Fall-through */
1330 default:
1331 component = &ccn->node[id];
1332 break;
1333 }
1334
1335 component->base = base;
1336 component->type = type;
1337
1338 return 0;
1339}
1340
1341
1342static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
1343 const u32 *err_sig_val)
1344{
1345 /* This should be really handled by firmware... */
1346 dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
1347 err_sig_val[5], err_sig_val[4], err_sig_val[3],
1348 err_sig_val[2], err_sig_val[1], err_sig_val[0]);
1349 dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
1350 writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
1351 ccn->base + CCN_MN_ERRINT_STATUS);
1352
1353 return IRQ_HANDLED;
1354}
1355
1356
1357static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
1358{
1359 irqreturn_t res = IRQ_NONE;
1360 struct arm_ccn *ccn = dev_id;
1361 u32 err_sig_val[6];
1362 u32 err_or;
1363 int i;
1364
1365 /* PMU overflow is a special case */
1366 err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
1367 if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
1368 err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
1369 res = arm_ccn_pmu_overflow_handler(&ccn->dt);
1370 }
1371
1372 /* Have to read all err_sig_vals to clear them */
1373 for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
1374 err_sig_val[i] = readl(ccn->base +
1375 CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
1376 err_or |= err_sig_val[i];
1377 }
1378 if (err_or)
1379 res |= arm_ccn_error_handler(ccn, err_sig_val);
1380
1381 if (res != IRQ_NONE)
1382 writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
1383 ccn->base + CCN_MN_ERRINT_STATUS);
1384
1385 return res;
1386}
1387
1388
1389static int arm_ccn_probe(struct platform_device *pdev)
1390{
1391 struct arm_ccn *ccn;
1392 struct resource *res;
Pawel Mollffa41522015-04-16 12:14:35 +01001393 unsigned int irq;
Pawel Molla33b0da2014-07-22 18:32:59 +01001394 int err;
1395
1396 ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
1397 if (!ccn)
1398 return -ENOMEM;
1399 ccn->dev = &pdev->dev;
1400 platform_set_drvdata(pdev, ccn);
1401
1402 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1403 if (!res)
1404 return -EINVAL;
1405
1406 if (!devm_request_mem_region(ccn->dev, res->start,
1407 resource_size(res), pdev->name))
1408 return -EBUSY;
1409
1410 ccn->base = devm_ioremap(ccn->dev, res->start,
1411 resource_size(res));
1412 if (!ccn->base)
1413 return -EFAULT;
1414
1415 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1416 if (!res)
1417 return -EINVAL;
Pawel Mollffa41522015-04-16 12:14:35 +01001418 irq = res->start;
Pawel Molla33b0da2014-07-22 18:32:59 +01001419
1420 /* Check if we can use the interrupt */
1421 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
1422 ccn->base + CCN_MN_ERRINT_STATUS);
1423 if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
1424 CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
1425 /* Can set 'disable' bits, so can acknowledge interrupts */
1426 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
1427 ccn->base + CCN_MN_ERRINT_STATUS);
Pawel Mollffa41522015-04-16 12:14:35 +01001428 err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler, 0,
1429 dev_name(ccn->dev), ccn);
Pawel Molla33b0da2014-07-22 18:32:59 +01001430 if (err)
1431 return err;
1432
Pawel Mollffa41522015-04-16 12:14:35 +01001433 ccn->irq = irq;
Pawel Molla33b0da2014-07-22 18:32:59 +01001434 }
1435
1436
1437 /* Build topology */
1438
1439 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
1440 if (err)
1441 return err;
1442
1443 ccn->node = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_nodes,
1444 GFP_KERNEL);
1445 ccn->xp = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_xps,
1446 GFP_KERNEL);
1447 if (!ccn->node || !ccn->xp)
1448 return -ENOMEM;
1449
1450 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
1451 if (err)
1452 return err;
1453
1454 return arm_ccn_pmu_init(ccn);
1455}
1456
1457static int arm_ccn_remove(struct platform_device *pdev)
1458{
1459 struct arm_ccn *ccn = platform_get_drvdata(pdev);
1460
1461 arm_ccn_pmu_cleanup(ccn);
1462
1463 return 0;
1464}
1465
1466static const struct of_device_id arm_ccn_match[] = {
1467 { .compatible = "arm,ccn-504", },
1468 {},
1469};
1470
1471static struct platform_driver arm_ccn_driver = {
1472 .driver = {
1473 .name = "arm-ccn",
1474 .of_match_table = arm_ccn_match,
1475 },
1476 .probe = arm_ccn_probe,
1477 .remove = arm_ccn_remove,
1478};
1479
1480static int __init arm_ccn_init(void)
1481{
1482 int i;
1483
1484 for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
1485 arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
1486
1487 return platform_driver_register(&arm_ccn_driver);
1488}
1489
1490static void __exit arm_ccn_exit(void)
1491{
1492 platform_driver_unregister(&arm_ccn_driver);
1493}
1494
1495module_init(arm_ccn_init);
1496module_exit(arm_ccn_exit);
1497
1498MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
1499MODULE_LICENSE("GPL");