blob: fb589d4dbaf6c143d6bdfb710854ffe883793052 [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;
Pawel Moll9ce1aa82015-04-17 12:15:56 +0100663 struct perf_event *sibling;
Pawel Molla33b0da2014-07-22 18:32:59 +0100664
665 if (event->attr.type != event->pmu->type)
666 return -ENOENT;
667
668 ccn = pmu_to_arm_ccn(event->pmu);
Pawel Moll8fb22262014-09-02 16:26:11 +0100669 event->destroy = arm_ccn_pmu_event_destroy;
Pawel Molla33b0da2014-07-22 18:32:59 +0100670
671 if (hw->sample_period) {
672 dev_warn(ccn->dev, "Sampling not supported!\n");
673 return -EOPNOTSUPP;
674 }
675
676 if (has_branch_stack(event) || event->attr.exclude_user ||
677 event->attr.exclude_kernel || event->attr.exclude_hv ||
678 event->attr.exclude_idle) {
679 dev_warn(ccn->dev, "Can't exclude execution levels!\n");
680 return -EOPNOTSUPP;
681 }
682
683 if (event->cpu < 0) {
684 dev_warn(ccn->dev, "Can't provide per-task data!\n");
685 return -EOPNOTSUPP;
686 }
Pawel Mollffa41522015-04-16 12:14:35 +0100687 /*
688 * Many perf core operations (eg. events rotation) operate on a
689 * single CPU context. This is obvious for CPU PMUs, where one
690 * expects the same sets of events being observed on all CPUs,
691 * but can lead to issues for off-core PMUs, like CCN, where each
692 * event could be theoretically assigned to a different CPU. To
693 * mitigate this, we enforce CPU assignment to one, selected
694 * processor (the one described in the "cpumask" attribute).
695 */
696 event->cpu = cpumask_first(&ccn->dt.cpu);
Pawel Molla33b0da2014-07-22 18:32:59 +0100697
698 node_xp = CCN_CONFIG_NODE(event->attr.config);
699 type = CCN_CONFIG_TYPE(event->attr.config);
700 event_id = CCN_CONFIG_EVENT(event->attr.config);
701
702 /* Validate node/xp vs topology */
703 switch (type) {
704 case CCN_TYPE_XP:
705 if (node_xp >= ccn->num_xps) {
706 dev_warn(ccn->dev, "Invalid XP ID %d!\n", node_xp);
707 return -EINVAL;
708 }
709 break;
710 case CCN_TYPE_CYCLES:
711 break;
712 default:
713 if (node_xp >= ccn->num_nodes) {
714 dev_warn(ccn->dev, "Invalid node ID %d!\n", node_xp);
715 return -EINVAL;
716 }
717 if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
718 dev_warn(ccn->dev, "Invalid type 0x%x for node %d!\n",
719 type, node_xp);
720 return -EINVAL;
721 }
722 break;
723 }
724
725 /* Validate event ID vs available for the type */
726 for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
727 i++) {
728 struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
729 u32 port = CCN_CONFIG_PORT(event->attr.config);
730 u32 vc = CCN_CONFIG_VC(event->attr.config);
731
732 if (!arm_ccn_pmu_type_eq(type, e->type))
733 continue;
734 if (event_id != e->event)
735 continue;
736 if (e->num_ports && port >= e->num_ports) {
737 dev_warn(ccn->dev, "Invalid port %d for node/XP %d!\n",
738 port, node_xp);
739 return -EINVAL;
740 }
741 if (e->num_vcs && vc >= e->num_vcs) {
742 dev_warn(ccn->dev, "Invalid vc %d for node/XP %d!\n",
Pawel Mollbf87bb12014-08-18 18:20:49 +0100743 vc, node_xp);
Pawel Molla33b0da2014-07-22 18:32:59 +0100744 return -EINVAL;
745 }
746 valid = 1;
747 }
748 if (!valid) {
749 dev_warn(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
750 event_id, node_xp);
751 return -EINVAL;
752 }
753
754 /* Watchpoint-based event for a node is actually set on XP */
755 if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
756 u32 port;
757
758 type = CCN_TYPE_XP;
759 port = arm_ccn_node_to_xp_port(node_xp);
760 node_xp = arm_ccn_node_to_xp(node_xp);
761
762 arm_ccn_pmu_config_set(&event->attr.config,
763 node_xp, type, port);
764 }
765
Pawel Moll9ce1aa82015-04-17 12:15:56 +0100766 /*
767 * We must NOT create groups containing mixed PMUs, although software
768 * events are acceptable (for example to create a CCN group
769 * periodically read when a hrtimer aka cpu-clock leader triggers).
770 */
771 if (event->group_leader->pmu != event->pmu &&
772 !is_software_event(event->group_leader))
773 return -EINVAL;
774
775 list_for_each_entry(sibling, &event->group_leader->sibling_list,
776 group_entry)
777 if (sibling->pmu != event->pmu &&
778 !is_software_event(sibling))
779 return -EINVAL;
780
Pawel Molla33b0da2014-07-22 18:32:59 +0100781 /* Allocate the cycle counter */
782 if (type == CCN_TYPE_CYCLES) {
783 if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
784 ccn->dt.pmu_counters_mask))
785 return -EAGAIN;
786
787 hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
788 ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
789
790 return 0;
791 }
792
793 /* Allocate an event counter */
794 hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
795 CCN_NUM_PMU_EVENT_COUNTERS);
796 if (hw->idx < 0) {
797 dev_warn(ccn->dev, "No more counters available!\n");
798 return -EAGAIN;
799 }
800
801 if (type == CCN_TYPE_XP)
802 source = &ccn->xp[node_xp];
803 else
804 source = &ccn->node[node_xp];
805 ccn->dt.pmu_counters[hw->idx].source = source;
806
807 /* Allocate an event source or a watchpoint */
808 if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
Pawel Moll3e528cb2014-07-31 16:16:37 +0100809 bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
Pawel Molla33b0da2014-07-22 18:32:59 +0100810 CCN_NUM_XP_WATCHPOINTS);
811 else
Pawel Moll3e528cb2014-07-31 16:16:37 +0100812 bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
Pawel Molla33b0da2014-07-22 18:32:59 +0100813 CCN_NUM_PMU_EVENTS);
Pawel Moll3e528cb2014-07-31 16:16:37 +0100814 if (bit < 0) {
Pawel Molla33b0da2014-07-22 18:32:59 +0100815 dev_warn(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
816 node_xp);
817 clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
818 return -EAGAIN;
819 }
Pawel Moll3e528cb2014-07-31 16:16:37 +0100820 hw->config_base = bit;
Pawel Molla33b0da2014-07-22 18:32:59 +0100821
822 ccn->dt.pmu_counters[hw->idx].event = event;
823
824 return 0;
825}
826
Pawel Molla33b0da2014-07-22 18:32:59 +0100827static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
828{
829 u64 res;
830
831 if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
832#ifdef readq
833 res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
834#else
835 /* 40 bit counter, can do snapshot and read in two parts */
836 writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
837 while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
838 ;
839 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
840 res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
841 res <<= 32;
842 res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
843#endif
844 } else {
845 res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
846 }
847
848 return res;
849}
850
851static void arm_ccn_pmu_event_update(struct perf_event *event)
852{
853 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
854 struct hw_perf_event *hw = &event->hw;
855 u64 prev_count, new_count, mask;
856
857 do {
858 prev_count = local64_read(&hw->prev_count);
859 new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
860 } while (local64_xchg(&hw->prev_count, new_count) != prev_count);
861
862 mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
863
864 local64_add((new_count - prev_count) & mask, &event->count);
865}
866
867static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
868{
869 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
870 struct hw_perf_event *hw = &event->hw;
871 struct arm_ccn_component *xp;
872 u32 val, dt_cfg;
873
874 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
875 xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
876 else
877 xp = &ccn->xp[arm_ccn_node_to_xp(
878 CCN_CONFIG_NODE(event->attr.config))];
879
880 if (enable)
881 dt_cfg = hw->event_base;
882 else
883 dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
884
885 spin_lock(&ccn->dt.config_lock);
886
887 val = readl(xp->base + CCN_XP_DT_CONFIG);
888 val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
889 CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
890 val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
891 writel(val, xp->base + CCN_XP_DT_CONFIG);
892
893 spin_unlock(&ccn->dt.config_lock);
894}
895
896static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
897{
898 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
899 struct hw_perf_event *hw = &event->hw;
900
901 local64_set(&event->hw.prev_count,
902 arm_ccn_pmu_read_counter(ccn, hw->idx));
903 hw->state = 0;
904
Pawel Mollffa41522015-04-16 12:14:35 +0100905 /*
906 * Pin the timer, so that the overflows are handled by the chosen
907 * event->cpu (this is the same one as presented in "cpumask"
908 * attribute).
909 */
910 if (!ccn->irq)
911 __hrtimer_start_range_ns(&ccn->dt.hrtimer,
912 arm_ccn_pmu_timer_period(), 0,
913 HRTIMER_MODE_REL_PINNED, 0);
Pawel Molla33b0da2014-07-22 18:32:59 +0100914
915 /* Set the DT bus input, engaging the counter */
916 arm_ccn_pmu_xp_dt_config(event, 1);
917}
918
919static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
920{
921 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
922 struct hw_perf_event *hw = &event->hw;
923 u64 timeout;
924
925 /* Disable counting, setting the DT bus to pass-through mode */
926 arm_ccn_pmu_xp_dt_config(event, 0);
927
Pawel Mollffa41522015-04-16 12:14:35 +0100928 if (!ccn->irq)
Pawel Molla33b0da2014-07-22 18:32:59 +0100929 hrtimer_cancel(&ccn->dt.hrtimer);
930
931 /* Let the DT bus drain */
932 timeout = arm_ccn_pmu_read_counter(ccn, CCN_IDX_PMU_CYCLE_COUNTER) +
933 ccn->num_xps;
934 while (arm_ccn_pmu_read_counter(ccn, CCN_IDX_PMU_CYCLE_COUNTER) <
935 timeout)
936 cpu_relax();
937
938 if (flags & PERF_EF_UPDATE)
939 arm_ccn_pmu_event_update(event);
940
941 hw->state |= PERF_HES_STOPPED;
942}
943
944static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
945{
946 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
947 struct hw_perf_event *hw = &event->hw;
948 struct arm_ccn_component *source =
949 ccn->dt.pmu_counters[hw->idx].source;
950 unsigned long wp = hw->config_base;
951 u32 val;
952 u64 cmp_l = event->attr.config1;
953 u64 cmp_h = event->attr.config2;
954 u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
955 u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
956
957 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
958
959 /* Direction (RX/TX), device (port) & virtual channel */
960 val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
961 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
962 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
963 val |= CCN_CONFIG_DIR(event->attr.config) <<
964 CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
965 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
966 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
967 val |= CCN_CONFIG_PORT(event->attr.config) <<
968 CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
969 val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
970 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
971 val |= CCN_CONFIG_VC(event->attr.config) <<
972 CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
973 writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
974
975 /* Comparison values */
976 writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
977 writel((cmp_l >> 32) & 0xefffffff,
978 source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
979 writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
980 writel((cmp_h >> 32) & 0x0fffffff,
981 source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
982
983 /* Mask */
984 writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
985 writel((mask_l >> 32) & 0xefffffff,
986 source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
987 writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
988 writel((mask_h >> 32) & 0x0fffffff,
989 source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
990}
991
992static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
993{
994 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
995 struct hw_perf_event *hw = &event->hw;
996 struct arm_ccn_component *source =
997 ccn->dt.pmu_counters[hw->idx].source;
998 u32 val, id;
999
1000 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
1001
1002 id = (CCN_CONFIG_VC(event->attr.config) << 4) |
1003 (CCN_CONFIG_PORT(event->attr.config) << 3) |
1004 (CCN_CONFIG_EVENT(event->attr.config) << 0);
1005
1006 val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
1007 val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
1008 CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
1009 val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
1010 writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
1011}
1012
1013static void arm_ccn_pmu_node_event_config(struct perf_event *event)
1014{
1015 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1016 struct hw_perf_event *hw = &event->hw;
1017 struct arm_ccn_component *source =
1018 ccn->dt.pmu_counters[hw->idx].source;
1019 u32 type = CCN_CONFIG_TYPE(event->attr.config);
1020 u32 val, port;
1021
1022 port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
1023 hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
1024 hw->config_base);
1025
1026 /* These *_event_sel regs should be identical, but let's make sure... */
1027 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
1028 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
1029 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
1030 CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
1031 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
1032 CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
1033 BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
1034 CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
1035 BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
1036 CCN_RNI_PMU_EVENT_SEL__ID__MASK);
1037 if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
1038 !arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
1039 return;
1040
1041 /* Set the event id for the pre-allocated counter */
1042 val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
1043 val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
1044 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
1045 val |= CCN_CONFIG_EVENT(event->attr.config) <<
1046 CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
1047 writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
1048}
1049
1050static void arm_ccn_pmu_event_config(struct perf_event *event)
1051{
1052 struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
1053 struct hw_perf_event *hw = &event->hw;
1054 u32 xp, offset, val;
1055
1056 /* Cycle counter requires no setup */
1057 if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
1058 return;
1059
1060 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
1061 xp = CCN_CONFIG_XP(event->attr.config);
1062 else
1063 xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
1064
1065 spin_lock(&ccn->dt.config_lock);
1066
1067 /* Set the DT bus "distance" register */
1068 offset = (hw->idx / 4) * 4;
1069 val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1070 val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
1071 CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
1072 val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
1073 writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
1074
1075 if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
1076 if (CCN_CONFIG_EVENT(event->attr.config) ==
1077 CCN_EVENT_WATCHPOINT)
1078 arm_ccn_pmu_xp_watchpoint_config(event);
1079 else
1080 arm_ccn_pmu_xp_event_config(event);
1081 } else {
1082 arm_ccn_pmu_node_event_config(event);
1083 }
1084
1085 spin_unlock(&ccn->dt.config_lock);
1086}
1087
1088static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
1089{
1090 struct hw_perf_event *hw = &event->hw;
1091
1092 arm_ccn_pmu_event_config(event);
1093
1094 hw->state = PERF_HES_STOPPED;
1095
1096 if (flags & PERF_EF_START)
1097 arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
1098
1099 return 0;
1100}
1101
1102static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
1103{
1104 arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
Pawel Molla33b0da2014-07-22 18:32:59 +01001105}
1106
1107static void arm_ccn_pmu_event_read(struct perf_event *event)
1108{
1109 arm_ccn_pmu_event_update(event);
1110}
1111
1112static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
1113{
1114 u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
1115 int idx;
1116
1117 if (!pmovsr)
1118 return IRQ_NONE;
1119
1120 writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
1121
1122 BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
1123
1124 for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
1125 struct perf_event *event = dt->pmu_counters[idx].event;
1126 int overflowed = pmovsr & BIT(idx);
1127
Pawel Mollfa637bf2014-09-15 15:33:48 +01001128 WARN_ON_ONCE(overflowed && !event &&
1129 idx != CCN_IDX_PMU_CYCLE_COUNTER);
Pawel Molla33b0da2014-07-22 18:32:59 +01001130
1131 if (!event || !overflowed)
1132 continue;
1133
1134 arm_ccn_pmu_event_update(event);
1135 }
1136
1137 return IRQ_HANDLED;
1138}
1139
1140static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
1141{
1142 struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
1143 hrtimer);
1144 unsigned long flags;
1145
1146 local_irq_save(flags);
1147 arm_ccn_pmu_overflow_handler(dt);
1148 local_irq_restore(flags);
1149
1150 hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
1151 return HRTIMER_RESTART;
1152}
1153
1154
Pawel Mollffa41522015-04-16 12:14:35 +01001155static int arm_ccn_pmu_cpu_notifier(struct notifier_block *nb,
1156 unsigned long action, void *hcpu)
1157{
1158 struct arm_ccn_dt *dt = container_of(nb, struct arm_ccn_dt, cpu_nb);
1159 struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
1160 unsigned int cpu = (long)hcpu; /* for (long) see kernel/cpu.c */
1161 unsigned int target;
1162
1163 switch (action & ~CPU_TASKS_FROZEN) {
1164 case CPU_DOWN_PREPARE:
1165 if (!cpumask_test_and_clear_cpu(cpu, &dt->cpu))
1166 break;
1167 target = cpumask_any_but(cpu_online_mask, cpu);
1168 if (target < 0)
1169 break;
1170 perf_pmu_migrate_context(&dt->pmu, cpu, target);
1171 cpumask_set_cpu(target, &dt->cpu);
1172 WARN_ON(irq_set_affinity(ccn->irq, &dt->cpu) != 0);
1173 default:
1174 break;
1175 }
1176
1177 return NOTIFY_OK;
1178}
1179
1180
Pawel Molla33b0da2014-07-22 18:32:59 +01001181static DEFINE_IDA(arm_ccn_pmu_ida);
1182
1183static int arm_ccn_pmu_init(struct arm_ccn *ccn)
1184{
1185 int i;
1186 char *name;
Pawel Mollffa41522015-04-16 12:14:35 +01001187 int err;
Pawel Molla33b0da2014-07-22 18:32:59 +01001188
1189 /* Initialize DT subsystem */
1190 ccn->dt.base = ccn->base + CCN_REGION_SIZE;
1191 spin_lock_init(&ccn->dt.config_lock);
Pawel Mollfa637bf2014-09-15 15:33:48 +01001192 writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
Pawel Molla33b0da2014-07-22 18:32:59 +01001193 writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
1194 writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
1195 ccn->dt.base + CCN_DT_PMCR);
1196 writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
1197 for (i = 0; i < ccn->num_xps; i++) {
1198 writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
1199 writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1200 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
1201 (CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
1202 CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
1203 CCN_XP_DT_CONTROL__DT_ENABLE,
1204 ccn->xp[i].base + CCN_XP_DT_CONTROL);
1205 }
1206 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
1207 ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
1208 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
1209 ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
1210 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
1211 ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
1212 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
1213 ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
1214
1215 /* Get a convenient /sys/event_source/devices/ name */
1216 ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
1217 if (ccn->dt.id == 0) {
1218 name = "ccn";
1219 } else {
1220 int len = snprintf(NULL, 0, "ccn_%d", ccn->dt.id);
1221
1222 name = devm_kzalloc(ccn->dev, len + 1, GFP_KERNEL);
1223 snprintf(name, len + 1, "ccn_%d", ccn->dt.id);
1224 }
1225
1226 /* Perf driver registration */
1227 ccn->dt.pmu = (struct pmu) {
1228 .attr_groups = arm_ccn_pmu_attr_groups,
1229 .task_ctx_nr = perf_invalid_context,
1230 .event_init = arm_ccn_pmu_event_init,
1231 .add = arm_ccn_pmu_event_add,
1232 .del = arm_ccn_pmu_event_del,
1233 .start = arm_ccn_pmu_event_start,
1234 .stop = arm_ccn_pmu_event_stop,
1235 .read = arm_ccn_pmu_event_read,
1236 };
1237
1238 /* No overflow interrupt? Have to use a timer instead. */
Pawel Mollffa41522015-04-16 12:14:35 +01001239 if (!ccn->irq) {
Pawel Molla33b0da2014-07-22 18:32:59 +01001240 dev_info(ccn->dev, "No access to interrupts, using timer.\n");
1241 hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
1242 HRTIMER_MODE_REL);
1243 ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
1244 }
1245
Pawel Mollffa41522015-04-16 12:14:35 +01001246 /* Pick one CPU which we will use to collect data from CCN... */
1247 cpumask_set_cpu(smp_processor_id(), &ccn->dt.cpu);
1248
1249 /*
1250 * ... and change the selection when it goes offline. Priority is
1251 * picked to have a chance to migrate events before perf is notified.
1252 */
1253 ccn->dt.cpu_nb.notifier_call = arm_ccn_pmu_cpu_notifier;
1254 ccn->dt.cpu_nb.priority = CPU_PRI_PERF + 1,
1255 err = register_cpu_notifier(&ccn->dt.cpu_nb);
1256 if (err)
1257 goto error_cpu_notifier;
1258
1259 /* Also make sure that the overflow interrupt is handled by this CPU */
1260 if (ccn->irq) {
1261 err = irq_set_affinity(ccn->irq, &ccn->dt.cpu);
1262 if (err) {
1263 dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
1264 goto error_set_affinity;
1265 }
1266 }
1267
1268 err = perf_pmu_register(&ccn->dt.pmu, name, -1);
1269 if (err)
1270 goto error_pmu_register;
1271
1272 return 0;
1273
1274error_pmu_register:
1275error_set_affinity:
1276 unregister_cpu_notifier(&ccn->dt.cpu_nb);
1277error_cpu_notifier:
1278 ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1279 for (i = 0; i < ccn->num_xps; i++)
1280 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1281 writel(0, ccn->dt.base + CCN_DT_PMCR);
1282 return err;
Pawel Molla33b0da2014-07-22 18:32:59 +01001283}
1284
1285static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
1286{
1287 int i;
1288
Pawel Mollffa41522015-04-16 12:14:35 +01001289 irq_set_affinity(ccn->irq, cpu_possible_mask);
1290 unregister_cpu_notifier(&ccn->dt.cpu_nb);
Pawel Molla33b0da2014-07-22 18:32:59 +01001291 for (i = 0; i < ccn->num_xps; i++)
1292 writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
1293 writel(0, ccn->dt.base + CCN_DT_PMCR);
1294 perf_pmu_unregister(&ccn->dt.pmu);
1295 ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
1296}
1297
1298
1299static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
1300 int (*callback)(struct arm_ccn *ccn, int region,
1301 void __iomem *base, u32 type, u32 id))
1302{
1303 int region;
1304
1305 for (region = 0; region < CCN_NUM_REGIONS; region++) {
1306 u32 val, type, id;
1307 void __iomem *base;
1308 int err;
1309
1310 val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
1311 4 * (region / 32));
1312 if (!(val & (1 << (region % 32))))
1313 continue;
1314
1315 base = ccn->base + region * CCN_REGION_SIZE;
1316 val = readl(base + CCN_ALL_OLY_ID);
1317 type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
1318 CCN_ALL_OLY_ID__OLY_ID__MASK;
1319 id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
1320 CCN_ALL_OLY_ID__NODE_ID__MASK;
1321
1322 err = callback(ccn, region, base, type, id);
1323 if (err)
1324 return err;
1325 }
1326
1327 return 0;
1328}
1329
1330static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
1331 void __iomem *base, u32 type, u32 id)
1332{
1333
1334 if (type == CCN_TYPE_XP && id >= ccn->num_xps)
1335 ccn->num_xps = id + 1;
1336 else if (id >= ccn->num_nodes)
1337 ccn->num_nodes = id + 1;
1338
1339 return 0;
1340}
1341
1342static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
1343 void __iomem *base, u32 type, u32 id)
1344{
1345 struct arm_ccn_component *component;
1346
1347 dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
1348
1349 switch (type) {
1350 case CCN_TYPE_MN:
1351 case CCN_TYPE_DT:
1352 return 0;
1353 case CCN_TYPE_XP:
1354 component = &ccn->xp[id];
1355 break;
1356 case CCN_TYPE_SBSX:
1357 ccn->sbsx_present = 1;
1358 component = &ccn->node[id];
1359 break;
1360 case CCN_TYPE_SBAS:
1361 ccn->sbas_present = 1;
1362 /* Fall-through */
1363 default:
1364 component = &ccn->node[id];
1365 break;
1366 }
1367
1368 component->base = base;
1369 component->type = type;
1370
1371 return 0;
1372}
1373
1374
1375static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
1376 const u32 *err_sig_val)
1377{
1378 /* This should be really handled by firmware... */
1379 dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
1380 err_sig_val[5], err_sig_val[4], err_sig_val[3],
1381 err_sig_val[2], err_sig_val[1], err_sig_val[0]);
1382 dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
1383 writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
1384 ccn->base + CCN_MN_ERRINT_STATUS);
1385
1386 return IRQ_HANDLED;
1387}
1388
1389
1390static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
1391{
1392 irqreturn_t res = IRQ_NONE;
1393 struct arm_ccn *ccn = dev_id;
1394 u32 err_sig_val[6];
1395 u32 err_or;
1396 int i;
1397
1398 /* PMU overflow is a special case */
1399 err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
1400 if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
1401 err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
1402 res = arm_ccn_pmu_overflow_handler(&ccn->dt);
1403 }
1404
1405 /* Have to read all err_sig_vals to clear them */
1406 for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
1407 err_sig_val[i] = readl(ccn->base +
1408 CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
1409 err_or |= err_sig_val[i];
1410 }
1411 if (err_or)
1412 res |= arm_ccn_error_handler(ccn, err_sig_val);
1413
1414 if (res != IRQ_NONE)
1415 writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
1416 ccn->base + CCN_MN_ERRINT_STATUS);
1417
1418 return res;
1419}
1420
1421
1422static int arm_ccn_probe(struct platform_device *pdev)
1423{
1424 struct arm_ccn *ccn;
1425 struct resource *res;
Pawel Mollffa41522015-04-16 12:14:35 +01001426 unsigned int irq;
Pawel Molla33b0da2014-07-22 18:32:59 +01001427 int err;
1428
1429 ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
1430 if (!ccn)
1431 return -ENOMEM;
1432 ccn->dev = &pdev->dev;
1433 platform_set_drvdata(pdev, ccn);
1434
1435 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1436 if (!res)
1437 return -EINVAL;
1438
1439 if (!devm_request_mem_region(ccn->dev, res->start,
1440 resource_size(res), pdev->name))
1441 return -EBUSY;
1442
1443 ccn->base = devm_ioremap(ccn->dev, res->start,
1444 resource_size(res));
1445 if (!ccn->base)
1446 return -EFAULT;
1447
1448 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1449 if (!res)
1450 return -EINVAL;
Pawel Mollffa41522015-04-16 12:14:35 +01001451 irq = res->start;
Pawel Molla33b0da2014-07-22 18:32:59 +01001452
1453 /* Check if we can use the interrupt */
1454 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
1455 ccn->base + CCN_MN_ERRINT_STATUS);
1456 if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
1457 CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
1458 /* Can set 'disable' bits, so can acknowledge interrupts */
1459 writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
1460 ccn->base + CCN_MN_ERRINT_STATUS);
Pawel Mollffa41522015-04-16 12:14:35 +01001461 err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler, 0,
1462 dev_name(ccn->dev), ccn);
Pawel Molla33b0da2014-07-22 18:32:59 +01001463 if (err)
1464 return err;
1465
Pawel Mollffa41522015-04-16 12:14:35 +01001466 ccn->irq = irq;
Pawel Molla33b0da2014-07-22 18:32:59 +01001467 }
1468
1469
1470 /* Build topology */
1471
1472 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
1473 if (err)
1474 return err;
1475
1476 ccn->node = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_nodes,
1477 GFP_KERNEL);
1478 ccn->xp = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_xps,
1479 GFP_KERNEL);
1480 if (!ccn->node || !ccn->xp)
1481 return -ENOMEM;
1482
1483 err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
1484 if (err)
1485 return err;
1486
1487 return arm_ccn_pmu_init(ccn);
1488}
1489
1490static int arm_ccn_remove(struct platform_device *pdev)
1491{
1492 struct arm_ccn *ccn = platform_get_drvdata(pdev);
1493
1494 arm_ccn_pmu_cleanup(ccn);
1495
1496 return 0;
1497}
1498
1499static const struct of_device_id arm_ccn_match[] = {
1500 { .compatible = "arm,ccn-504", },
1501 {},
1502};
1503
1504static struct platform_driver arm_ccn_driver = {
1505 .driver = {
1506 .name = "arm-ccn",
1507 .of_match_table = arm_ccn_match,
1508 },
1509 .probe = arm_ccn_probe,
1510 .remove = arm_ccn_remove,
1511};
1512
1513static int __init arm_ccn_init(void)
1514{
1515 int i;
1516
1517 for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
1518 arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
1519
1520 return platform_driver_register(&arm_ccn_driver);
1521}
1522
1523static void __exit arm_ccn_exit(void)
1524{
1525 platform_driver_unregister(&arm_ccn_driver);
1526}
1527
1528module_init(arm_ccn_init);
1529module_exit(arm_ccn_exit);
1530
1531MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
1532MODULE_LICENSE("GPL");