blob: d4ec04868d598c73ffff3c0c35c1f09db4b2d9d5 [file] [log] [blame]
Tai Nguyen832c9272016-07-15 10:38:04 -07001/*
2 * APM X-Gene SoC PMU (Performance Monitor Unit)
3 *
4 * Copyright (c) 2016, Applied Micro Circuits Corporation
5 * Author: Hoan Tran <hotran@apm.com>
6 * Tai Nguyen <ttnguyen@apm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <linux/acpi.h>
23#include <linux/clk.h>
Hoan Trancbb72a32018-11-07 19:40:58 +000024#include <linux/cpuhotplug.h>
Tai Nguyen832c9272016-07-15 10:38:04 -070025#include <linux/cpumask.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/mfd/syscon.h>
Stephen Boydc0bfc542017-01-25 15:46:58 -080029#include <linux/module.h>
Tai Nguyen832c9272016-07-15 10:38:04 -070030#include <linux/of_address.h>
31#include <linux/of_fdt.h>
32#include <linux/of_irq.h>
33#include <linux/of_platform.h>
34#include <linux/perf_event.h>
35#include <linux/platform_device.h>
36#include <linux/regmap.h>
37#include <linux/slab.h>
38
39#define CSW_CSWCR 0x0000
40#define CSW_CSWCR_DUALMCB_MASK BIT(0)
Hoan Tranc0f7f7a2017-06-22 19:26:05 +010041#define CSW_CSWCR_MCB0_ROUTING(x) (((x) & 0x0C) >> 2)
42#define CSW_CSWCR_MCB1_ROUTING(x) (((x) & 0x30) >> 4)
Tai Nguyen832c9272016-07-15 10:38:04 -070043#define MCBADDRMR 0x0000
44#define MCBADDRMR_DUALMCU_MODE_MASK BIT(2)
45
46#define PCPPMU_INTSTATUS_REG 0x000
47#define PCPPMU_INTMASK_REG 0x004
48#define PCPPMU_INTMASK 0x0000000F
49#define PCPPMU_INTENMASK 0xFFFFFFFF
50#define PCPPMU_INTCLRMASK 0xFFFFFFF0
51#define PCPPMU_INT_MCU BIT(0)
52#define PCPPMU_INT_MCB BIT(1)
53#define PCPPMU_INT_L3C BIT(2)
54#define PCPPMU_INT_IOB BIT(3)
55
Hoan Tranc0f7f7a2017-06-22 19:26:05 +010056#define PCPPMU_V3_INTMASK 0x00FF33FF
57#define PCPPMU_V3_INTENMASK 0xFFFFFFFF
58#define PCPPMU_V3_INTCLRMASK 0xFF00CC00
59#define PCPPMU_V3_INT_MCU 0x000000FF
60#define PCPPMU_V3_INT_MCB 0x00000300
61#define PCPPMU_V3_INT_L3C 0x00FF0000
62#define PCPPMU_V3_INT_IOB 0x00003000
63
Tai Nguyen832c9272016-07-15 10:38:04 -070064#define PMU_MAX_COUNTERS 4
Hoan Tranc0f7f7a2017-06-22 19:26:05 +010065#define PMU_CNT_MAX_PERIOD 0xFFFFFFFFULL
66#define PMU_V3_CNT_MAX_PERIOD 0xFFFFFFFFFFFFFFFFULL
Tai Nguyen832c9272016-07-15 10:38:04 -070067#define PMU_OVERFLOW_MASK 0xF
68#define PMU_PMCR_E BIT(0)
69#define PMU_PMCR_P BIT(1)
70
71#define PMU_PMEVCNTR0 0x000
72#define PMU_PMEVCNTR1 0x004
73#define PMU_PMEVCNTR2 0x008
74#define PMU_PMEVCNTR3 0x00C
75#define PMU_PMEVTYPER0 0x400
76#define PMU_PMEVTYPER1 0x404
77#define PMU_PMEVTYPER2 0x408
78#define PMU_PMEVTYPER3 0x40C
79#define PMU_PMAMR0 0xA00
80#define PMU_PMAMR1 0xA04
81#define PMU_PMCNTENSET 0xC00
82#define PMU_PMCNTENCLR 0xC20
83#define PMU_PMINTENSET 0xC40
84#define PMU_PMINTENCLR 0xC60
85#define PMU_PMOVSR 0xC80
86#define PMU_PMCR 0xE04
87
Hoan Tranc0f7f7a2017-06-22 19:26:05 +010088/* PMU registers for V3 */
89#define PMU_PMOVSCLR 0xC80
90#define PMU_PMOVSSET 0xCC0
91
Tai Nguyen832c9272016-07-15 10:38:04 -070092#define to_pmu_dev(p) container_of(p, struct xgene_pmu_dev, pmu)
93#define GET_CNTR(ev) (ev->hw.idx)
94#define GET_EVENTID(ev) (ev->hw.config & 0xFFULL)
95#define GET_AGENTID(ev) (ev->hw.config_base & 0xFFFFFFFFUL)
96#define GET_AGENT1ID(ev) ((ev->hw.config_base >> 32) & 0xFFFFFFFFUL)
97
98struct hw_pmu_info {
99 u32 type;
100 u32 enable_mask;
101 void __iomem *csr;
102};
103
104struct xgene_pmu_dev {
105 struct hw_pmu_info *inf;
106 struct xgene_pmu *parent;
107 struct pmu pmu;
108 u8 max_counters;
109 DECLARE_BITMAP(cntr_assign_mask, PMU_MAX_COUNTERS);
110 u64 max_period;
111 const struct attribute_group **attr_groups;
112 struct perf_event *pmu_counter_event[PMU_MAX_COUNTERS];
113};
114
Hoan Trane35e0a02017-06-22 19:26:04 +0100115struct xgene_pmu_ops {
116 void (*mask_int)(struct xgene_pmu *pmu);
117 void (*unmask_int)(struct xgene_pmu *pmu);
118 u64 (*read_counter)(struct xgene_pmu_dev *pmu, int idx);
119 void (*write_counter)(struct xgene_pmu_dev *pmu, int idx, u64 val);
120 void (*write_evttype)(struct xgene_pmu_dev *pmu_dev, int idx, u32 val);
121 void (*write_agentmsk)(struct xgene_pmu_dev *pmu_dev, u32 val);
122 void (*write_agent1msk)(struct xgene_pmu_dev *pmu_dev, u32 val);
123 void (*enable_counter)(struct xgene_pmu_dev *pmu_dev, int idx);
124 void (*disable_counter)(struct xgene_pmu_dev *pmu_dev, int idx);
125 void (*enable_counter_int)(struct xgene_pmu_dev *pmu_dev, int idx);
126 void (*disable_counter_int)(struct xgene_pmu_dev *pmu_dev, int idx);
127 void (*reset_counters)(struct xgene_pmu_dev *pmu_dev);
128 void (*start_counters)(struct xgene_pmu_dev *pmu_dev);
129 void (*stop_counters)(struct xgene_pmu_dev *pmu_dev);
130};
131
Tai Nguyen832c9272016-07-15 10:38:04 -0700132struct xgene_pmu {
133 struct device *dev;
Hoan Trancbb72a32018-11-07 19:40:58 +0000134 struct hlist_node node;
Tai Nguyen832c9272016-07-15 10:38:04 -0700135 int version;
136 void __iomem *pcppmu_csr;
137 u32 mcb_active_mask;
138 u32 mc_active_mask;
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100139 u32 l3c_active_mask;
Tai Nguyen832c9272016-07-15 10:38:04 -0700140 cpumask_t cpu;
Hoan Trancbb72a32018-11-07 19:40:58 +0000141 int irq;
Tai Nguyen832c9272016-07-15 10:38:04 -0700142 raw_spinlock_t lock;
Hoan Trane35e0a02017-06-22 19:26:04 +0100143 const struct xgene_pmu_ops *ops;
Tai Nguyen832c9272016-07-15 10:38:04 -0700144 struct list_head l3cpmus;
145 struct list_head iobpmus;
146 struct list_head mcbpmus;
147 struct list_head mcpmus;
148};
149
150struct xgene_pmu_dev_ctx {
151 char *name;
152 struct list_head next;
153 struct xgene_pmu_dev *pmu_dev;
154 struct hw_pmu_info inf;
155};
156
157struct xgene_pmu_data {
158 int id;
159 u32 data;
160};
161
162enum xgene_pmu_version {
163 PCP_PMU_V1 = 1,
164 PCP_PMU_V2,
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100165 PCP_PMU_V3,
Tai Nguyen832c9272016-07-15 10:38:04 -0700166};
167
168enum xgene_pmu_dev_type {
169 PMU_TYPE_L3C = 0,
170 PMU_TYPE_IOB,
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100171 PMU_TYPE_IOB_SLOW,
Tai Nguyen832c9272016-07-15 10:38:04 -0700172 PMU_TYPE_MCB,
173 PMU_TYPE_MC,
174};
175
176/*
177 * sysfs format attributes
178 */
179static ssize_t xgene_pmu_format_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
182 struct dev_ext_attribute *eattr;
183
184 eattr = container_of(attr, struct dev_ext_attribute, attr);
185 return sprintf(buf, "%s\n", (char *) eattr->var);
186}
187
188#define XGENE_PMU_FORMAT_ATTR(_name, _config) \
189 (&((struct dev_ext_attribute[]) { \
190 { .attr = __ATTR(_name, S_IRUGO, xgene_pmu_format_show, NULL), \
191 .var = (void *) _config, } \
192 })[0].attr.attr)
193
194static struct attribute *l3c_pmu_format_attrs[] = {
195 XGENE_PMU_FORMAT_ATTR(l3c_eventid, "config:0-7"),
196 XGENE_PMU_FORMAT_ATTR(l3c_agentid, "config1:0-9"),
197 NULL,
198};
199
200static struct attribute *iob_pmu_format_attrs[] = {
201 XGENE_PMU_FORMAT_ATTR(iob_eventid, "config:0-7"),
202 XGENE_PMU_FORMAT_ATTR(iob_agentid, "config1:0-63"),
203 NULL,
204};
205
206static struct attribute *mcb_pmu_format_attrs[] = {
207 XGENE_PMU_FORMAT_ATTR(mcb_eventid, "config:0-5"),
208 XGENE_PMU_FORMAT_ATTR(mcb_agentid, "config1:0-9"),
209 NULL,
210};
211
212static struct attribute *mc_pmu_format_attrs[] = {
213 XGENE_PMU_FORMAT_ATTR(mc_eventid, "config:0-28"),
214 NULL,
215};
216
217static const struct attribute_group l3c_pmu_format_attr_group = {
218 .name = "format",
219 .attrs = l3c_pmu_format_attrs,
220};
221
222static const struct attribute_group iob_pmu_format_attr_group = {
223 .name = "format",
224 .attrs = iob_pmu_format_attrs,
225};
226
227static const struct attribute_group mcb_pmu_format_attr_group = {
228 .name = "format",
229 .attrs = mcb_pmu_format_attrs,
230};
231
232static const struct attribute_group mc_pmu_format_attr_group = {
233 .name = "format",
234 .attrs = mc_pmu_format_attrs,
235};
236
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100237static struct attribute *l3c_pmu_v3_format_attrs[] = {
238 XGENE_PMU_FORMAT_ATTR(l3c_eventid, "config:0-39"),
239 NULL,
240};
241
242static struct attribute *iob_pmu_v3_format_attrs[] = {
243 XGENE_PMU_FORMAT_ATTR(iob_eventid, "config:0-47"),
244 NULL,
245};
246
247static struct attribute *iob_slow_pmu_v3_format_attrs[] = {
248 XGENE_PMU_FORMAT_ATTR(iob_slow_eventid, "config:0-16"),
249 NULL,
250};
251
252static struct attribute *mcb_pmu_v3_format_attrs[] = {
253 XGENE_PMU_FORMAT_ATTR(mcb_eventid, "config:0-35"),
254 NULL,
255};
256
257static struct attribute *mc_pmu_v3_format_attrs[] = {
258 XGENE_PMU_FORMAT_ATTR(mc_eventid, "config:0-44"),
259 NULL,
260};
261
262static const struct attribute_group l3c_pmu_v3_format_attr_group = {
263 .name = "format",
264 .attrs = l3c_pmu_v3_format_attrs,
265};
266
267static const struct attribute_group iob_pmu_v3_format_attr_group = {
268 .name = "format",
269 .attrs = iob_pmu_v3_format_attrs,
270};
271
272static const struct attribute_group iob_slow_pmu_v3_format_attr_group = {
273 .name = "format",
274 .attrs = iob_slow_pmu_v3_format_attrs,
275};
276
277static const struct attribute_group mcb_pmu_v3_format_attr_group = {
278 .name = "format",
279 .attrs = mcb_pmu_v3_format_attrs,
280};
281
282static const struct attribute_group mc_pmu_v3_format_attr_group = {
283 .name = "format",
284 .attrs = mc_pmu_v3_format_attrs,
285};
286
Tai Nguyen832c9272016-07-15 10:38:04 -0700287/*
288 * sysfs event attributes
289 */
290static ssize_t xgene_pmu_event_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
292{
293 struct dev_ext_attribute *eattr;
294
295 eattr = container_of(attr, struct dev_ext_attribute, attr);
296 return sprintf(buf, "config=0x%lx\n", (unsigned long) eattr->var);
297}
298
299#define XGENE_PMU_EVENT_ATTR(_name, _config) \
300 (&((struct dev_ext_attribute[]) { \
301 { .attr = __ATTR(_name, S_IRUGO, xgene_pmu_event_show, NULL), \
302 .var = (void *) _config, } \
303 })[0].attr.attr)
304
305static struct attribute *l3c_pmu_events_attrs[] = {
306 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
307 XGENE_PMU_EVENT_ATTR(cycle-count-div-64, 0x01),
308 XGENE_PMU_EVENT_ATTR(read-hit, 0x02),
309 XGENE_PMU_EVENT_ATTR(read-miss, 0x03),
310 XGENE_PMU_EVENT_ATTR(write-need-replacement, 0x06),
311 XGENE_PMU_EVENT_ATTR(write-not-need-replacement, 0x07),
312 XGENE_PMU_EVENT_ATTR(tq-full, 0x08),
313 XGENE_PMU_EVENT_ATTR(ackq-full, 0x09),
314 XGENE_PMU_EVENT_ATTR(wdb-full, 0x0a),
315 XGENE_PMU_EVENT_ATTR(bank-fifo-full, 0x0b),
316 XGENE_PMU_EVENT_ATTR(odb-full, 0x0c),
317 XGENE_PMU_EVENT_ATTR(wbq-full, 0x0d),
318 XGENE_PMU_EVENT_ATTR(bank-conflict-fifo-issue, 0x0e),
319 XGENE_PMU_EVENT_ATTR(bank-fifo-issue, 0x0f),
320 NULL,
321};
322
323static struct attribute *iob_pmu_events_attrs[] = {
324 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
325 XGENE_PMU_EVENT_ATTR(cycle-count-div-64, 0x01),
326 XGENE_PMU_EVENT_ATTR(axi0-read, 0x02),
327 XGENE_PMU_EVENT_ATTR(axi0-read-partial, 0x03),
328 XGENE_PMU_EVENT_ATTR(axi1-read, 0x04),
329 XGENE_PMU_EVENT_ATTR(axi1-read-partial, 0x05),
330 XGENE_PMU_EVENT_ATTR(csw-read-block, 0x06),
331 XGENE_PMU_EVENT_ATTR(csw-read-partial, 0x07),
332 XGENE_PMU_EVENT_ATTR(axi0-write, 0x10),
333 XGENE_PMU_EVENT_ATTR(axi0-write-partial, 0x11),
334 XGENE_PMU_EVENT_ATTR(axi1-write, 0x13),
335 XGENE_PMU_EVENT_ATTR(axi1-write-partial, 0x14),
336 XGENE_PMU_EVENT_ATTR(csw-inbound-dirty, 0x16),
337 NULL,
338};
339
340static struct attribute *mcb_pmu_events_attrs[] = {
341 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
342 XGENE_PMU_EVENT_ATTR(cycle-count-div-64, 0x01),
343 XGENE_PMU_EVENT_ATTR(csw-read, 0x02),
344 XGENE_PMU_EVENT_ATTR(csw-write-request, 0x03),
345 XGENE_PMU_EVENT_ATTR(mcb-csw-stall, 0x04),
346 XGENE_PMU_EVENT_ATTR(cancel-read-gack, 0x05),
347 NULL,
348};
349
350static struct attribute *mc_pmu_events_attrs[] = {
351 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
352 XGENE_PMU_EVENT_ATTR(cycle-count-div-64, 0x01),
353 XGENE_PMU_EVENT_ATTR(act-cmd-sent, 0x02),
354 XGENE_PMU_EVENT_ATTR(pre-cmd-sent, 0x03),
355 XGENE_PMU_EVENT_ATTR(rd-cmd-sent, 0x04),
356 XGENE_PMU_EVENT_ATTR(rda-cmd-sent, 0x05),
357 XGENE_PMU_EVENT_ATTR(wr-cmd-sent, 0x06),
358 XGENE_PMU_EVENT_ATTR(wra-cmd-sent, 0x07),
359 XGENE_PMU_EVENT_ATTR(pde-cmd-sent, 0x08),
360 XGENE_PMU_EVENT_ATTR(sre-cmd-sent, 0x09),
361 XGENE_PMU_EVENT_ATTR(prea-cmd-sent, 0x0a),
362 XGENE_PMU_EVENT_ATTR(ref-cmd-sent, 0x0b),
363 XGENE_PMU_EVENT_ATTR(rd-rda-cmd-sent, 0x0c),
364 XGENE_PMU_EVENT_ATTR(wr-wra-cmd-sent, 0x0d),
365 XGENE_PMU_EVENT_ATTR(in-rd-collision, 0x0e),
366 XGENE_PMU_EVENT_ATTR(in-wr-collision, 0x0f),
367 XGENE_PMU_EVENT_ATTR(collision-queue-not-empty, 0x10),
368 XGENE_PMU_EVENT_ATTR(collision-queue-full, 0x11),
369 XGENE_PMU_EVENT_ATTR(mcu-request, 0x12),
370 XGENE_PMU_EVENT_ATTR(mcu-rd-request, 0x13),
371 XGENE_PMU_EVENT_ATTR(mcu-hp-rd-request, 0x14),
372 XGENE_PMU_EVENT_ATTR(mcu-wr-request, 0x15),
373 XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-all, 0x16),
374 XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-cancel, 0x17),
375 XGENE_PMU_EVENT_ATTR(mcu-rd-response, 0x18),
376 XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-speculative-all, 0x19),
377 XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-speculative-cancel, 0x1a),
378 XGENE_PMU_EVENT_ATTR(mcu-wr-proceed-all, 0x1b),
379 XGENE_PMU_EVENT_ATTR(mcu-wr-proceed-cancel, 0x1c),
380 NULL,
381};
382
383static const struct attribute_group l3c_pmu_events_attr_group = {
384 .name = "events",
385 .attrs = l3c_pmu_events_attrs,
386};
387
388static const struct attribute_group iob_pmu_events_attr_group = {
389 .name = "events",
390 .attrs = iob_pmu_events_attrs,
391};
392
393static const struct attribute_group mcb_pmu_events_attr_group = {
394 .name = "events",
395 .attrs = mcb_pmu_events_attrs,
396};
397
398static const struct attribute_group mc_pmu_events_attr_group = {
399 .name = "events",
400 .attrs = mc_pmu_events_attrs,
401};
402
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100403static struct attribute *l3c_pmu_v3_events_attrs[] = {
404 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
405 XGENE_PMU_EVENT_ATTR(read-hit, 0x01),
406 XGENE_PMU_EVENT_ATTR(read-miss, 0x02),
407 XGENE_PMU_EVENT_ATTR(index-flush-eviction, 0x03),
408 XGENE_PMU_EVENT_ATTR(write-caused-replacement, 0x04),
409 XGENE_PMU_EVENT_ATTR(write-not-caused-replacement, 0x05),
410 XGENE_PMU_EVENT_ATTR(clean-eviction, 0x06),
411 XGENE_PMU_EVENT_ATTR(dirty-eviction, 0x07),
412 XGENE_PMU_EVENT_ATTR(read, 0x08),
413 XGENE_PMU_EVENT_ATTR(write, 0x09),
414 XGENE_PMU_EVENT_ATTR(request, 0x0a),
415 XGENE_PMU_EVENT_ATTR(tq-bank-conflict-issue-stall, 0x0b),
416 XGENE_PMU_EVENT_ATTR(tq-full, 0x0c),
417 XGENE_PMU_EVENT_ATTR(ackq-full, 0x0d),
418 XGENE_PMU_EVENT_ATTR(wdb-full, 0x0e),
419 XGENE_PMU_EVENT_ATTR(odb-full, 0x10),
420 XGENE_PMU_EVENT_ATTR(wbq-full, 0x11),
421 XGENE_PMU_EVENT_ATTR(input-req-async-fifo-stall, 0x12),
422 XGENE_PMU_EVENT_ATTR(output-req-async-fifo-stall, 0x13),
423 XGENE_PMU_EVENT_ATTR(output-data-async-fifo-stall, 0x14),
424 XGENE_PMU_EVENT_ATTR(total-insertion, 0x15),
425 XGENE_PMU_EVENT_ATTR(sip-insertions-r-set, 0x16),
426 XGENE_PMU_EVENT_ATTR(sip-insertions-r-clear, 0x17),
427 XGENE_PMU_EVENT_ATTR(dip-insertions-r-set, 0x18),
428 XGENE_PMU_EVENT_ATTR(dip-insertions-r-clear, 0x19),
429 XGENE_PMU_EVENT_ATTR(dip-insertions-force-r-set, 0x1a),
430 XGENE_PMU_EVENT_ATTR(egression, 0x1b),
431 XGENE_PMU_EVENT_ATTR(replacement, 0x1c),
432 XGENE_PMU_EVENT_ATTR(old-replacement, 0x1d),
433 XGENE_PMU_EVENT_ATTR(young-replacement, 0x1e),
434 XGENE_PMU_EVENT_ATTR(r-set-replacement, 0x1f),
435 XGENE_PMU_EVENT_ATTR(r-clear-replacement, 0x20),
436 XGENE_PMU_EVENT_ATTR(old-r-replacement, 0x21),
437 XGENE_PMU_EVENT_ATTR(old-nr-replacement, 0x22),
438 XGENE_PMU_EVENT_ATTR(young-r-replacement, 0x23),
439 XGENE_PMU_EVENT_ATTR(young-nr-replacement, 0x24),
440 XGENE_PMU_EVENT_ATTR(bloomfilter-clearing, 0x25),
441 XGENE_PMU_EVENT_ATTR(generation-flip, 0x26),
442 XGENE_PMU_EVENT_ATTR(vcc-droop-detected, 0x27),
443 NULL,
444};
445
446static struct attribute *iob_fast_pmu_v3_events_attrs[] = {
447 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
448 XGENE_PMU_EVENT_ATTR(pa-req-buf-alloc-all, 0x01),
449 XGENE_PMU_EVENT_ATTR(pa-req-buf-alloc-rd, 0x02),
450 XGENE_PMU_EVENT_ATTR(pa-req-buf-alloc-wr, 0x03),
451 XGENE_PMU_EVENT_ATTR(pa-all-cp-req, 0x04),
452 XGENE_PMU_EVENT_ATTR(pa-cp-blk-req, 0x05),
453 XGENE_PMU_EVENT_ATTR(pa-cp-ptl-req, 0x06),
454 XGENE_PMU_EVENT_ATTR(pa-cp-rd-req, 0x07),
455 XGENE_PMU_EVENT_ATTR(pa-cp-wr-req, 0x08),
456 XGENE_PMU_EVENT_ATTR(ba-all-req, 0x09),
457 XGENE_PMU_EVENT_ATTR(ba-rd-req, 0x0a),
458 XGENE_PMU_EVENT_ATTR(ba-wr-req, 0x0b),
459 XGENE_PMU_EVENT_ATTR(pa-rd-shared-req-issued, 0x10),
460 XGENE_PMU_EVENT_ATTR(pa-rd-exclusive-req-issued, 0x11),
461 XGENE_PMU_EVENT_ATTR(pa-wr-invalidate-req-issued-stashable, 0x12),
462 XGENE_PMU_EVENT_ATTR(pa-wr-invalidate-req-issued-nonstashable, 0x13),
463 XGENE_PMU_EVENT_ATTR(pa-wr-back-req-issued-stashable, 0x14),
464 XGENE_PMU_EVENT_ATTR(pa-wr-back-req-issued-nonstashable, 0x15),
465 XGENE_PMU_EVENT_ATTR(pa-ptl-wr-req, 0x16),
466 XGENE_PMU_EVENT_ATTR(pa-ptl-rd-req, 0x17),
467 XGENE_PMU_EVENT_ATTR(pa-wr-back-clean-data, 0x18),
468 XGENE_PMU_EVENT_ATTR(pa-wr-back-cancelled-on-SS, 0x1b),
469 XGENE_PMU_EVENT_ATTR(pa-barrier-occurrence, 0x1c),
470 XGENE_PMU_EVENT_ATTR(pa-barrier-cycles, 0x1d),
471 XGENE_PMU_EVENT_ATTR(pa-total-cp-snoops, 0x20),
472 XGENE_PMU_EVENT_ATTR(pa-rd-shared-snoop, 0x21),
473 XGENE_PMU_EVENT_ATTR(pa-rd-shared-snoop-hit, 0x22),
474 XGENE_PMU_EVENT_ATTR(pa-rd-exclusive-snoop, 0x23),
475 XGENE_PMU_EVENT_ATTR(pa-rd-exclusive-snoop-hit, 0x24),
476 XGENE_PMU_EVENT_ATTR(pa-rd-wr-invalid-snoop, 0x25),
477 XGENE_PMU_EVENT_ATTR(pa-rd-wr-invalid-snoop-hit, 0x26),
478 XGENE_PMU_EVENT_ATTR(pa-req-buffer-full, 0x28),
479 XGENE_PMU_EVENT_ATTR(cswlf-outbound-req-fifo-full, 0x29),
480 XGENE_PMU_EVENT_ATTR(cswlf-inbound-snoop-fifo-backpressure, 0x2a),
481 XGENE_PMU_EVENT_ATTR(cswlf-outbound-lack-fifo-full, 0x2b),
482 XGENE_PMU_EVENT_ATTR(cswlf-inbound-gack-fifo-backpressure, 0x2c),
483 XGENE_PMU_EVENT_ATTR(cswlf-outbound-data-fifo-full, 0x2d),
484 XGENE_PMU_EVENT_ATTR(cswlf-inbound-data-fifo-backpressure, 0x2e),
485 XGENE_PMU_EVENT_ATTR(cswlf-inbound-req-backpressure, 0x2f),
486 NULL,
487};
488
489static struct attribute *iob_slow_pmu_v3_events_attrs[] = {
490 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
491 XGENE_PMU_EVENT_ATTR(pa-axi0-rd-req, 0x01),
492 XGENE_PMU_EVENT_ATTR(pa-axi0-wr-req, 0x02),
493 XGENE_PMU_EVENT_ATTR(pa-axi1-rd-req, 0x03),
494 XGENE_PMU_EVENT_ATTR(pa-axi1-wr-req, 0x04),
495 XGENE_PMU_EVENT_ATTR(ba-all-axi-req, 0x07),
496 XGENE_PMU_EVENT_ATTR(ba-axi-rd-req, 0x08),
497 XGENE_PMU_EVENT_ATTR(ba-axi-wr-req, 0x09),
498 XGENE_PMU_EVENT_ATTR(ba-free-list-empty, 0x10),
499 NULL,
500};
501
502static struct attribute *mcb_pmu_v3_events_attrs[] = {
503 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
504 XGENE_PMU_EVENT_ATTR(req-receive, 0x01),
505 XGENE_PMU_EVENT_ATTR(rd-req-recv, 0x02),
506 XGENE_PMU_EVENT_ATTR(rd-req-recv-2, 0x03),
507 XGENE_PMU_EVENT_ATTR(wr-req-recv, 0x04),
508 XGENE_PMU_EVENT_ATTR(wr-req-recv-2, 0x05),
509 XGENE_PMU_EVENT_ATTR(rd-req-sent-to-mcu, 0x06),
510 XGENE_PMU_EVENT_ATTR(rd-req-sent-to-mcu-2, 0x07),
511 XGENE_PMU_EVENT_ATTR(rd-req-sent-to-spec-mcu, 0x08),
512 XGENE_PMU_EVENT_ATTR(rd-req-sent-to-spec-mcu-2, 0x09),
513 XGENE_PMU_EVENT_ATTR(glbl-ack-recv-for-rd-sent-to-spec-mcu, 0x0a),
514 XGENE_PMU_EVENT_ATTR(glbl-ack-go-recv-for-rd-sent-to-spec-mcu, 0x0b),
515 XGENE_PMU_EVENT_ATTR(glbl-ack-nogo-recv-for-rd-sent-to-spec-mcu, 0x0c),
516 XGENE_PMU_EVENT_ATTR(glbl-ack-go-recv-any-rd-req, 0x0d),
517 XGENE_PMU_EVENT_ATTR(glbl-ack-go-recv-any-rd-req-2, 0x0e),
518 XGENE_PMU_EVENT_ATTR(wr-req-sent-to-mcu, 0x0f),
519 XGENE_PMU_EVENT_ATTR(gack-recv, 0x10),
520 XGENE_PMU_EVENT_ATTR(rd-gack-recv, 0x11),
521 XGENE_PMU_EVENT_ATTR(wr-gack-recv, 0x12),
522 XGENE_PMU_EVENT_ATTR(cancel-rd-gack, 0x13),
523 XGENE_PMU_EVENT_ATTR(cancel-wr-gack, 0x14),
524 XGENE_PMU_EVENT_ATTR(mcb-csw-req-stall, 0x15),
525 XGENE_PMU_EVENT_ATTR(mcu-req-intf-blocked, 0x16),
526 XGENE_PMU_EVENT_ATTR(mcb-mcu-rd-intf-stall, 0x17),
527 XGENE_PMU_EVENT_ATTR(csw-rd-intf-blocked, 0x18),
528 XGENE_PMU_EVENT_ATTR(csw-local-ack-intf-blocked, 0x19),
529 XGENE_PMU_EVENT_ATTR(mcu-req-table-full, 0x1a),
530 XGENE_PMU_EVENT_ATTR(mcu-stat-table-full, 0x1b),
531 XGENE_PMU_EVENT_ATTR(mcu-wr-table-full, 0x1c),
532 XGENE_PMU_EVENT_ATTR(mcu-rdreceipt-resp, 0x1d),
533 XGENE_PMU_EVENT_ATTR(mcu-wrcomplete-resp, 0x1e),
534 XGENE_PMU_EVENT_ATTR(mcu-retryack-resp, 0x1f),
535 XGENE_PMU_EVENT_ATTR(mcu-pcrdgrant-resp, 0x20),
536 XGENE_PMU_EVENT_ATTR(mcu-req-from-lastload, 0x21),
537 XGENE_PMU_EVENT_ATTR(mcu-req-from-bypass, 0x22),
538 XGENE_PMU_EVENT_ATTR(volt-droop-detect, 0x23),
539 NULL,
540};
541
542static struct attribute *mc_pmu_v3_events_attrs[] = {
543 XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
544 XGENE_PMU_EVENT_ATTR(act-sent, 0x01),
545 XGENE_PMU_EVENT_ATTR(pre-sent, 0x02),
546 XGENE_PMU_EVENT_ATTR(rd-sent, 0x03),
547 XGENE_PMU_EVENT_ATTR(rda-sent, 0x04),
548 XGENE_PMU_EVENT_ATTR(wr-sent, 0x05),
549 XGENE_PMU_EVENT_ATTR(wra-sent, 0x06),
550 XGENE_PMU_EVENT_ATTR(pd-entry-vld, 0x07),
551 XGENE_PMU_EVENT_ATTR(sref-entry-vld, 0x08),
552 XGENE_PMU_EVENT_ATTR(prea-sent, 0x09),
553 XGENE_PMU_EVENT_ATTR(ref-sent, 0x0a),
554 XGENE_PMU_EVENT_ATTR(rd-rda-sent, 0x0b),
555 XGENE_PMU_EVENT_ATTR(wr-wra-sent, 0x0c),
556 XGENE_PMU_EVENT_ATTR(raw-hazard, 0x0d),
557 XGENE_PMU_EVENT_ATTR(war-hazard, 0x0e),
558 XGENE_PMU_EVENT_ATTR(waw-hazard, 0x0f),
559 XGENE_PMU_EVENT_ATTR(rar-hazard, 0x10),
560 XGENE_PMU_EVENT_ATTR(raw-war-waw-hazard, 0x11),
561 XGENE_PMU_EVENT_ATTR(hprd-lprd-wr-req-vld, 0x12),
562 XGENE_PMU_EVENT_ATTR(lprd-req-vld, 0x13),
563 XGENE_PMU_EVENT_ATTR(hprd-req-vld, 0x14),
564 XGENE_PMU_EVENT_ATTR(hprd-lprd-req-vld, 0x15),
565 XGENE_PMU_EVENT_ATTR(wr-req-vld, 0x16),
566 XGENE_PMU_EVENT_ATTR(partial-wr-req-vld, 0x17),
567 XGENE_PMU_EVENT_ATTR(rd-retry, 0x18),
568 XGENE_PMU_EVENT_ATTR(wr-retry, 0x19),
569 XGENE_PMU_EVENT_ATTR(retry-gnt, 0x1a),
570 XGENE_PMU_EVENT_ATTR(rank-change, 0x1b),
571 XGENE_PMU_EVENT_ATTR(dir-change, 0x1c),
572 XGENE_PMU_EVENT_ATTR(rank-dir-change, 0x1d),
573 XGENE_PMU_EVENT_ATTR(rank-active, 0x1e),
574 XGENE_PMU_EVENT_ATTR(rank-idle, 0x1f),
575 XGENE_PMU_EVENT_ATTR(rank-pd, 0x20),
576 XGENE_PMU_EVENT_ATTR(rank-sref, 0x21),
577 XGENE_PMU_EVENT_ATTR(queue-fill-gt-thresh, 0x22),
578 XGENE_PMU_EVENT_ATTR(queue-rds-gt-thresh, 0x23),
579 XGENE_PMU_EVENT_ATTR(queue-wrs-gt-thresh, 0x24),
580 XGENE_PMU_EVENT_ATTR(phy-updt-complt, 0x25),
581 XGENE_PMU_EVENT_ATTR(tz-fail, 0x26),
582 XGENE_PMU_EVENT_ATTR(dram-errc, 0x27),
583 XGENE_PMU_EVENT_ATTR(dram-errd, 0x28),
584 XGENE_PMU_EVENT_ATTR(rd-enq, 0x29),
585 XGENE_PMU_EVENT_ATTR(wr-enq, 0x2a),
586 XGENE_PMU_EVENT_ATTR(tmac-limit-reached, 0x2b),
587 XGENE_PMU_EVENT_ATTR(tmaw-tracker-full, 0x2c),
588 NULL,
589};
590
591static const struct attribute_group l3c_pmu_v3_events_attr_group = {
592 .name = "events",
593 .attrs = l3c_pmu_v3_events_attrs,
594};
595
596static const struct attribute_group iob_fast_pmu_v3_events_attr_group = {
597 .name = "events",
598 .attrs = iob_fast_pmu_v3_events_attrs,
599};
600
601static const struct attribute_group iob_slow_pmu_v3_events_attr_group = {
602 .name = "events",
603 .attrs = iob_slow_pmu_v3_events_attrs,
604};
605
606static const struct attribute_group mcb_pmu_v3_events_attr_group = {
607 .name = "events",
608 .attrs = mcb_pmu_v3_events_attrs,
609};
610
611static const struct attribute_group mc_pmu_v3_events_attr_group = {
612 .name = "events",
613 .attrs = mc_pmu_v3_events_attrs,
614};
615
Tai Nguyen832c9272016-07-15 10:38:04 -0700616/*
617 * sysfs cpumask attributes
618 */
619static ssize_t xgene_pmu_cpumask_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
621{
622 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(dev_get_drvdata(dev));
623
624 return cpumap_print_to_pagebuf(true, buf, &pmu_dev->parent->cpu);
625}
626
627static DEVICE_ATTR(cpumask, S_IRUGO, xgene_pmu_cpumask_show, NULL);
628
629static struct attribute *xgene_pmu_cpumask_attrs[] = {
630 &dev_attr_cpumask.attr,
631 NULL,
632};
633
634static const struct attribute_group pmu_cpumask_attr_group = {
635 .attrs = xgene_pmu_cpumask_attrs,
636};
637
638/*
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100639 * Per PMU device attribute groups of PMU v1 and v2
Tai Nguyen832c9272016-07-15 10:38:04 -0700640 */
641static const struct attribute_group *l3c_pmu_attr_groups[] = {
642 &l3c_pmu_format_attr_group,
643 &pmu_cpumask_attr_group,
644 &l3c_pmu_events_attr_group,
645 NULL
646};
647
648static const struct attribute_group *iob_pmu_attr_groups[] = {
649 &iob_pmu_format_attr_group,
650 &pmu_cpumask_attr_group,
651 &iob_pmu_events_attr_group,
652 NULL
653};
654
655static const struct attribute_group *mcb_pmu_attr_groups[] = {
656 &mcb_pmu_format_attr_group,
657 &pmu_cpumask_attr_group,
658 &mcb_pmu_events_attr_group,
659 NULL
660};
661
662static const struct attribute_group *mc_pmu_attr_groups[] = {
663 &mc_pmu_format_attr_group,
664 &pmu_cpumask_attr_group,
665 &mc_pmu_events_attr_group,
666 NULL
667};
668
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100669/*
670 * Per PMU device attribute groups of PMU v3
671 */
672static const struct attribute_group *l3c_pmu_v3_attr_groups[] = {
673 &l3c_pmu_v3_format_attr_group,
674 &pmu_cpumask_attr_group,
675 &l3c_pmu_v3_events_attr_group,
676 NULL
677};
678
679static const struct attribute_group *iob_fast_pmu_v3_attr_groups[] = {
680 &iob_pmu_v3_format_attr_group,
681 &pmu_cpumask_attr_group,
682 &iob_fast_pmu_v3_events_attr_group,
683 NULL
684};
685
686static const struct attribute_group *iob_slow_pmu_v3_attr_groups[] = {
687 &iob_slow_pmu_v3_format_attr_group,
688 &pmu_cpumask_attr_group,
689 &iob_slow_pmu_v3_events_attr_group,
690 NULL
691};
692
693static const struct attribute_group *mcb_pmu_v3_attr_groups[] = {
694 &mcb_pmu_v3_format_attr_group,
695 &pmu_cpumask_attr_group,
696 &mcb_pmu_v3_events_attr_group,
697 NULL
698};
699
700static const struct attribute_group *mc_pmu_v3_attr_groups[] = {
701 &mc_pmu_v3_format_attr_group,
702 &pmu_cpumask_attr_group,
703 &mc_pmu_v3_events_attr_group,
704 NULL
705};
706
Tai Nguyen832c9272016-07-15 10:38:04 -0700707static int get_next_avail_cntr(struct xgene_pmu_dev *pmu_dev)
708{
709 int cntr;
710
711 cntr = find_first_zero_bit(pmu_dev->cntr_assign_mask,
712 pmu_dev->max_counters);
713 if (cntr == pmu_dev->max_counters)
714 return -ENOSPC;
715 set_bit(cntr, pmu_dev->cntr_assign_mask);
716
717 return cntr;
718}
719
720static void clear_avail_cntr(struct xgene_pmu_dev *pmu_dev, int cntr)
721{
722 clear_bit(cntr, pmu_dev->cntr_assign_mask);
723}
724
725static inline void xgene_pmu_mask_int(struct xgene_pmu *xgene_pmu)
726{
727 writel(PCPPMU_INTENMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
728}
729
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100730static inline void xgene_pmu_v3_mask_int(struct xgene_pmu *xgene_pmu)
731{
732 writel(PCPPMU_V3_INTENMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
733}
734
Tai Nguyen832c9272016-07-15 10:38:04 -0700735static inline void xgene_pmu_unmask_int(struct xgene_pmu *xgene_pmu)
736{
737 writel(PCPPMU_INTCLRMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
738}
739
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100740static inline void xgene_pmu_v3_unmask_int(struct xgene_pmu *xgene_pmu)
741{
742 writel(PCPPMU_V3_INTCLRMASK,
743 xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
744}
745
Hoan Trane35e0a02017-06-22 19:26:04 +0100746static inline u64 xgene_pmu_read_counter32(struct xgene_pmu_dev *pmu_dev,
747 int idx)
Tai Nguyen832c9272016-07-15 10:38:04 -0700748{
749 return readl(pmu_dev->inf->csr + PMU_PMEVCNTR0 + (4 * idx));
750}
751
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100752static inline u64 xgene_pmu_read_counter64(struct xgene_pmu_dev *pmu_dev,
753 int idx)
754{
755 u32 lo, hi;
756
757 /*
758 * v3 has 64-bit counter registers composed by 2 32-bit registers
759 * This can be a problem if the counter increases and carries
760 * out of bit [31] between 2 reads. The extra reads would help
761 * to prevent this issue.
762 */
763 do {
764 hi = xgene_pmu_read_counter32(pmu_dev, 2 * idx + 1);
765 lo = xgene_pmu_read_counter32(pmu_dev, 2 * idx);
766 } while (hi != xgene_pmu_read_counter32(pmu_dev, 2 * idx + 1));
767
768 return (((u64)hi << 32) | lo);
769}
770
Tai Nguyen832c9272016-07-15 10:38:04 -0700771static inline void
Hoan Trane35e0a02017-06-22 19:26:04 +0100772xgene_pmu_write_counter32(struct xgene_pmu_dev *pmu_dev, int idx, u64 val)
Tai Nguyen832c9272016-07-15 10:38:04 -0700773{
774 writel(val, pmu_dev->inf->csr + PMU_PMEVCNTR0 + (4 * idx));
775}
776
777static inline void
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100778xgene_pmu_write_counter64(struct xgene_pmu_dev *pmu_dev, int idx, u64 val)
779{
780 u32 cnt_lo, cnt_hi;
781
782 cnt_hi = upper_32_bits(val);
783 cnt_lo = lower_32_bits(val);
784
785 /* v3 has 64-bit counter registers composed by 2 32-bit registers */
786 xgene_pmu_write_counter32(pmu_dev, 2 * idx, cnt_lo);
787 xgene_pmu_write_counter32(pmu_dev, 2 * idx + 1, cnt_hi);
788}
789
790static inline void
Tai Nguyen832c9272016-07-15 10:38:04 -0700791xgene_pmu_write_evttype(struct xgene_pmu_dev *pmu_dev, int idx, u32 val)
792{
793 writel(val, pmu_dev->inf->csr + PMU_PMEVTYPER0 + (4 * idx));
794}
795
796static inline void
797xgene_pmu_write_agentmsk(struct xgene_pmu_dev *pmu_dev, u32 val)
798{
799 writel(val, pmu_dev->inf->csr + PMU_PMAMR0);
800}
801
802static inline void
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100803xgene_pmu_v3_write_agentmsk(struct xgene_pmu_dev *pmu_dev, u32 val) { }
804
805static inline void
Tai Nguyen832c9272016-07-15 10:38:04 -0700806xgene_pmu_write_agent1msk(struct xgene_pmu_dev *pmu_dev, u32 val)
807{
808 writel(val, pmu_dev->inf->csr + PMU_PMAMR1);
809}
810
811static inline void
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100812xgene_pmu_v3_write_agent1msk(struct xgene_pmu_dev *pmu_dev, u32 val) { }
813
814static inline void
Tai Nguyen832c9272016-07-15 10:38:04 -0700815xgene_pmu_enable_counter(struct xgene_pmu_dev *pmu_dev, int idx)
816{
817 u32 val;
818
819 val = readl(pmu_dev->inf->csr + PMU_PMCNTENSET);
820 val |= 1 << idx;
821 writel(val, pmu_dev->inf->csr + PMU_PMCNTENSET);
822}
823
824static inline void
825xgene_pmu_disable_counter(struct xgene_pmu_dev *pmu_dev, int idx)
826{
827 u32 val;
828
829 val = readl(pmu_dev->inf->csr + PMU_PMCNTENCLR);
830 val |= 1 << idx;
831 writel(val, pmu_dev->inf->csr + PMU_PMCNTENCLR);
832}
833
834static inline void
835xgene_pmu_enable_counter_int(struct xgene_pmu_dev *pmu_dev, int idx)
836{
837 u32 val;
838
839 val = readl(pmu_dev->inf->csr + PMU_PMINTENSET);
840 val |= 1 << idx;
841 writel(val, pmu_dev->inf->csr + PMU_PMINTENSET);
842}
843
844static inline void
845xgene_pmu_disable_counter_int(struct xgene_pmu_dev *pmu_dev, int idx)
846{
847 u32 val;
848
849 val = readl(pmu_dev->inf->csr + PMU_PMINTENCLR);
850 val |= 1 << idx;
851 writel(val, pmu_dev->inf->csr + PMU_PMINTENCLR);
852}
853
854static inline void xgene_pmu_reset_counters(struct xgene_pmu_dev *pmu_dev)
855{
856 u32 val;
857
858 val = readl(pmu_dev->inf->csr + PMU_PMCR);
859 val |= PMU_PMCR_P;
860 writel(val, pmu_dev->inf->csr + PMU_PMCR);
861}
862
863static inline void xgene_pmu_start_counters(struct xgene_pmu_dev *pmu_dev)
864{
865 u32 val;
866
867 val = readl(pmu_dev->inf->csr + PMU_PMCR);
868 val |= PMU_PMCR_E;
869 writel(val, pmu_dev->inf->csr + PMU_PMCR);
870}
871
872static inline void xgene_pmu_stop_counters(struct xgene_pmu_dev *pmu_dev)
873{
874 u32 val;
875
876 val = readl(pmu_dev->inf->csr + PMU_PMCR);
877 val &= ~PMU_PMCR_E;
878 writel(val, pmu_dev->inf->csr + PMU_PMCR);
879}
880
881static void xgene_perf_pmu_enable(struct pmu *pmu)
882{
883 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(pmu);
Hoan Trane35e0a02017-06-22 19:26:04 +0100884 struct xgene_pmu *xgene_pmu = pmu_dev->parent;
Tai Nguyen832c9272016-07-15 10:38:04 -0700885 int enabled = bitmap_weight(pmu_dev->cntr_assign_mask,
886 pmu_dev->max_counters);
887
888 if (!enabled)
889 return;
890
Hoan Trane35e0a02017-06-22 19:26:04 +0100891 xgene_pmu->ops->start_counters(pmu_dev);
Tai Nguyen832c9272016-07-15 10:38:04 -0700892}
893
894static void xgene_perf_pmu_disable(struct pmu *pmu)
895{
896 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(pmu);
Hoan Trane35e0a02017-06-22 19:26:04 +0100897 struct xgene_pmu *xgene_pmu = pmu_dev->parent;
Tai Nguyen832c9272016-07-15 10:38:04 -0700898
Hoan Trane35e0a02017-06-22 19:26:04 +0100899 xgene_pmu->ops->stop_counters(pmu_dev);
Tai Nguyen832c9272016-07-15 10:38:04 -0700900}
901
902static int xgene_perf_event_init(struct perf_event *event)
903{
904 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
905 struct hw_perf_event *hw = &event->hw;
906 struct perf_event *sibling;
907
908 /* Test the event attr type check for PMU enumeration */
909 if (event->attr.type != event->pmu->type)
910 return -ENOENT;
911
912 /*
913 * SOC PMU counters are shared across all cores.
914 * Therefore, it does not support per-process mode.
915 * Also, it does not support event sampling mode.
916 */
917 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
918 return -EINVAL;
919
Tai Nguyen832c9272016-07-15 10:38:04 -0700920 if (event->cpu < 0)
921 return -EINVAL;
922 /*
923 * Many perf core operations (eg. events rotation) operate on a
924 * single CPU context. This is obvious for CPU PMUs, where one
925 * expects the same sets of events being observed on all CPUs,
926 * but can lead to issues for off-core PMUs, where each
927 * event could be theoretically assigned to a different CPU. To
928 * mitigate this, we enforce CPU assignment to one, selected
929 * processor (the one described in the "cpumask" attribute).
930 */
931 event->cpu = cpumask_first(&pmu_dev->parent->cpu);
932
933 hw->config = event->attr.config;
934 /*
935 * Each bit of the config1 field represents an agent from which the
936 * request of the event come. The event is counted only if it's caused
937 * by a request of an agent has the bit cleared.
938 * By default, the event is counted for all agents.
939 */
940 hw->config_base = event->attr.config1;
941
942 /*
943 * We must NOT create groups containing mixed PMUs, although software
944 * events are acceptable
945 */
946 if (event->group_leader->pmu != event->pmu &&
947 !is_software_event(event->group_leader))
948 return -EINVAL;
949
Peter Zijlstraedb39592018-03-15 17:36:56 +0100950 for_each_sibling_event(sibling, event->group_leader) {
Tai Nguyen832c9272016-07-15 10:38:04 -0700951 if (sibling->pmu != event->pmu &&
952 !is_software_event(sibling))
953 return -EINVAL;
Peter Zijlstraedb39592018-03-15 17:36:56 +0100954 }
Tai Nguyen832c9272016-07-15 10:38:04 -0700955
956 return 0;
957}
958
959static void xgene_perf_enable_event(struct perf_event *event)
960{
961 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
Hoan Trane35e0a02017-06-22 19:26:04 +0100962 struct xgene_pmu *xgene_pmu = pmu_dev->parent;
Tai Nguyen832c9272016-07-15 10:38:04 -0700963
Hoan Trane35e0a02017-06-22 19:26:04 +0100964 xgene_pmu->ops->write_evttype(pmu_dev, GET_CNTR(event),
965 GET_EVENTID(event));
966 xgene_pmu->ops->write_agentmsk(pmu_dev, ~((u32)GET_AGENTID(event)));
Tai Nguyen832c9272016-07-15 10:38:04 -0700967 if (pmu_dev->inf->type == PMU_TYPE_IOB)
Hoan Trane35e0a02017-06-22 19:26:04 +0100968 xgene_pmu->ops->write_agent1msk(pmu_dev,
969 ~((u32)GET_AGENT1ID(event)));
Tai Nguyen832c9272016-07-15 10:38:04 -0700970
Hoan Trane35e0a02017-06-22 19:26:04 +0100971 xgene_pmu->ops->enable_counter(pmu_dev, GET_CNTR(event));
972 xgene_pmu->ops->enable_counter_int(pmu_dev, GET_CNTR(event));
Tai Nguyen832c9272016-07-15 10:38:04 -0700973}
974
975static void xgene_perf_disable_event(struct perf_event *event)
976{
977 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
Hoan Trane35e0a02017-06-22 19:26:04 +0100978 struct xgene_pmu *xgene_pmu = pmu_dev->parent;
Tai Nguyen832c9272016-07-15 10:38:04 -0700979
Hoan Trane35e0a02017-06-22 19:26:04 +0100980 xgene_pmu->ops->disable_counter(pmu_dev, GET_CNTR(event));
981 xgene_pmu->ops->disable_counter_int(pmu_dev, GET_CNTR(event));
Tai Nguyen832c9272016-07-15 10:38:04 -0700982}
983
984static void xgene_perf_event_set_period(struct perf_event *event)
985{
986 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
Hoan Trane35e0a02017-06-22 19:26:04 +0100987 struct xgene_pmu *xgene_pmu = pmu_dev->parent;
Tai Nguyen832c9272016-07-15 10:38:04 -0700988 struct hw_perf_event *hw = &event->hw;
989 /*
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100990 * For 32 bit counter, it has a period of 2^32. To account for the
991 * possibility of extreme interrupt latency we program for a period of
992 * half that. Hopefully, we can handle the interrupt before another 2^31
Tai Nguyen832c9272016-07-15 10:38:04 -0700993 * events occur and the counter overtakes its previous value.
Hoan Tranc0f7f7a2017-06-22 19:26:05 +0100994 * For 64 bit counter, we don't expect it overflow.
Tai Nguyen832c9272016-07-15 10:38:04 -0700995 */
996 u64 val = 1ULL << 31;
997
998 local64_set(&hw->prev_count, val);
Hoan Trane35e0a02017-06-22 19:26:04 +0100999 xgene_pmu->ops->write_counter(pmu_dev, hw->idx, val);
Tai Nguyen832c9272016-07-15 10:38:04 -07001000}
1001
1002static void xgene_perf_event_update(struct perf_event *event)
1003{
1004 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
Hoan Trane35e0a02017-06-22 19:26:04 +01001005 struct xgene_pmu *xgene_pmu = pmu_dev->parent;
Tai Nguyen832c9272016-07-15 10:38:04 -07001006 struct hw_perf_event *hw = &event->hw;
1007 u64 delta, prev_raw_count, new_raw_count;
1008
1009again:
1010 prev_raw_count = local64_read(&hw->prev_count);
Hoan Trane35e0a02017-06-22 19:26:04 +01001011 new_raw_count = xgene_pmu->ops->read_counter(pmu_dev, GET_CNTR(event));
Tai Nguyen832c9272016-07-15 10:38:04 -07001012
1013 if (local64_cmpxchg(&hw->prev_count, prev_raw_count,
1014 new_raw_count) != prev_raw_count)
1015 goto again;
1016
1017 delta = (new_raw_count - prev_raw_count) & pmu_dev->max_period;
1018
1019 local64_add(delta, &event->count);
1020}
1021
1022static void xgene_perf_read(struct perf_event *event)
1023{
1024 xgene_perf_event_update(event);
1025}
1026
1027static void xgene_perf_start(struct perf_event *event, int flags)
1028{
1029 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
Hoan Trane35e0a02017-06-22 19:26:04 +01001030 struct xgene_pmu *xgene_pmu = pmu_dev->parent;
Tai Nguyen832c9272016-07-15 10:38:04 -07001031 struct hw_perf_event *hw = &event->hw;
1032
1033 if (WARN_ON_ONCE(!(hw->state & PERF_HES_STOPPED)))
1034 return;
1035
1036 WARN_ON_ONCE(!(hw->state & PERF_HES_UPTODATE));
1037 hw->state = 0;
1038
1039 xgene_perf_event_set_period(event);
1040
1041 if (flags & PERF_EF_RELOAD) {
1042 u64 prev_raw_count = local64_read(&hw->prev_count);
1043
Hoan Trane35e0a02017-06-22 19:26:04 +01001044 xgene_pmu->ops->write_counter(pmu_dev, GET_CNTR(event),
1045 prev_raw_count);
Tai Nguyen832c9272016-07-15 10:38:04 -07001046 }
1047
1048 xgene_perf_enable_event(event);
1049 perf_event_update_userpage(event);
1050}
1051
1052static void xgene_perf_stop(struct perf_event *event, int flags)
1053{
1054 struct hw_perf_event *hw = &event->hw;
1055 u64 config;
1056
1057 if (hw->state & PERF_HES_UPTODATE)
1058 return;
1059
1060 xgene_perf_disable_event(event);
1061 WARN_ON_ONCE(hw->state & PERF_HES_STOPPED);
1062 hw->state |= PERF_HES_STOPPED;
1063
1064 if (hw->state & PERF_HES_UPTODATE)
1065 return;
1066
1067 config = hw->config;
1068 xgene_perf_read(event);
1069 hw->state |= PERF_HES_UPTODATE;
1070}
1071
1072static int xgene_perf_add(struct perf_event *event, int flags)
1073{
1074 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
1075 struct hw_perf_event *hw = &event->hw;
1076
1077 hw->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1078
1079 /* Allocate an event counter */
1080 hw->idx = get_next_avail_cntr(pmu_dev);
1081 if (hw->idx < 0)
1082 return -EAGAIN;
1083
1084 /* Update counter event pointer for Interrupt handler */
1085 pmu_dev->pmu_counter_event[hw->idx] = event;
1086
1087 if (flags & PERF_EF_START)
1088 xgene_perf_start(event, PERF_EF_RELOAD);
1089
1090 return 0;
1091}
1092
1093static void xgene_perf_del(struct perf_event *event, int flags)
1094{
1095 struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
1096 struct hw_perf_event *hw = &event->hw;
1097
1098 xgene_perf_stop(event, PERF_EF_UPDATE);
1099
1100 /* clear the assigned counter */
1101 clear_avail_cntr(pmu_dev, GET_CNTR(event));
1102
1103 perf_event_update_userpage(event);
1104 pmu_dev->pmu_counter_event[hw->idx] = NULL;
1105}
1106
1107static int xgene_init_perf(struct xgene_pmu_dev *pmu_dev, char *name)
1108{
1109 struct xgene_pmu *xgene_pmu;
1110
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001111 if (pmu_dev->parent->version == PCP_PMU_V3)
1112 pmu_dev->max_period = PMU_V3_CNT_MAX_PERIOD;
1113 else
1114 pmu_dev->max_period = PMU_CNT_MAX_PERIOD;
Tai Nguyen832c9272016-07-15 10:38:04 -07001115 /* First version PMU supports only single event counter */
1116 xgene_pmu = pmu_dev->parent;
1117 if (xgene_pmu->version == PCP_PMU_V1)
1118 pmu_dev->max_counters = 1;
1119 else
1120 pmu_dev->max_counters = PMU_MAX_COUNTERS;
1121
1122 /* Perf driver registration */
1123 pmu_dev->pmu = (struct pmu) {
1124 .attr_groups = pmu_dev->attr_groups,
1125 .task_ctx_nr = perf_invalid_context,
1126 .pmu_enable = xgene_perf_pmu_enable,
1127 .pmu_disable = xgene_perf_pmu_disable,
1128 .event_init = xgene_perf_event_init,
1129 .add = xgene_perf_add,
1130 .del = xgene_perf_del,
1131 .start = xgene_perf_start,
1132 .stop = xgene_perf_stop,
1133 .read = xgene_perf_read,
Andrew Murraya66b0012019-01-10 13:53:30 +00001134 .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
Tai Nguyen832c9272016-07-15 10:38:04 -07001135 };
1136
1137 /* Hardware counter init */
Hoan Trane35e0a02017-06-22 19:26:04 +01001138 xgene_pmu->ops->stop_counters(pmu_dev);
1139 xgene_pmu->ops->reset_counters(pmu_dev);
Tai Nguyen832c9272016-07-15 10:38:04 -07001140
1141 return perf_pmu_register(&pmu_dev->pmu, name, -1);
1142}
1143
1144static int
1145xgene_pmu_dev_add(struct xgene_pmu *xgene_pmu, struct xgene_pmu_dev_ctx *ctx)
1146{
1147 struct device *dev = xgene_pmu->dev;
1148 struct xgene_pmu_dev *pmu;
Tai Nguyen832c9272016-07-15 10:38:04 -07001149
1150 pmu = devm_kzalloc(dev, sizeof(*pmu), GFP_KERNEL);
1151 if (!pmu)
1152 return -ENOMEM;
1153 pmu->parent = xgene_pmu;
1154 pmu->inf = &ctx->inf;
1155 ctx->pmu_dev = pmu;
1156
1157 switch (pmu->inf->type) {
1158 case PMU_TYPE_L3C:
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001159 if (!(xgene_pmu->l3c_active_mask & pmu->inf->enable_mask))
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001160 return -ENODEV;
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001161 if (xgene_pmu->version == PCP_PMU_V3)
1162 pmu->attr_groups = l3c_pmu_v3_attr_groups;
1163 else
1164 pmu->attr_groups = l3c_pmu_attr_groups;
Tai Nguyen832c9272016-07-15 10:38:04 -07001165 break;
1166 case PMU_TYPE_IOB:
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001167 if (xgene_pmu->version == PCP_PMU_V3)
1168 pmu->attr_groups = iob_fast_pmu_v3_attr_groups;
1169 else
1170 pmu->attr_groups = iob_pmu_attr_groups;
1171 break;
1172 case PMU_TYPE_IOB_SLOW:
1173 if (xgene_pmu->version == PCP_PMU_V3)
1174 pmu->attr_groups = iob_slow_pmu_v3_attr_groups;
Tai Nguyen832c9272016-07-15 10:38:04 -07001175 break;
1176 case PMU_TYPE_MCB:
1177 if (!(xgene_pmu->mcb_active_mask & pmu->inf->enable_mask))
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001178 return -ENODEV;
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001179 if (xgene_pmu->version == PCP_PMU_V3)
1180 pmu->attr_groups = mcb_pmu_v3_attr_groups;
1181 else
1182 pmu->attr_groups = mcb_pmu_attr_groups;
Tai Nguyen832c9272016-07-15 10:38:04 -07001183 break;
1184 case PMU_TYPE_MC:
1185 if (!(xgene_pmu->mc_active_mask & pmu->inf->enable_mask))
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001186 return -ENODEV;
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001187 if (xgene_pmu->version == PCP_PMU_V3)
1188 pmu->attr_groups = mc_pmu_v3_attr_groups;
1189 else
1190 pmu->attr_groups = mc_pmu_attr_groups;
Tai Nguyen832c9272016-07-15 10:38:04 -07001191 break;
1192 default:
1193 return -EINVAL;
1194 }
1195
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001196 if (xgene_init_perf(pmu, ctx->name)) {
Tai Nguyen832c9272016-07-15 10:38:04 -07001197 dev_err(dev, "%s PMU: Failed to init perf driver\n", ctx->name);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001198 return -ENODEV;
Tai Nguyen832c9272016-07-15 10:38:04 -07001199 }
1200
1201 dev_info(dev, "%s PMU registered\n", ctx->name);
1202
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001203 return 0;
Tai Nguyen832c9272016-07-15 10:38:04 -07001204}
1205
1206static void _xgene_pmu_isr(int irq, struct xgene_pmu_dev *pmu_dev)
1207{
1208 struct xgene_pmu *xgene_pmu = pmu_dev->parent;
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001209 void __iomem *csr = pmu_dev->inf->csr;
Tai Nguyen832c9272016-07-15 10:38:04 -07001210 u32 pmovsr;
1211 int idx;
1212
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001213 xgene_pmu->ops->stop_counters(pmu_dev);
1214
1215 if (xgene_pmu->version == PCP_PMU_V3)
1216 pmovsr = readl(csr + PMU_PMOVSSET) & PMU_OVERFLOW_MASK;
1217 else
1218 pmovsr = readl(csr + PMU_PMOVSR) & PMU_OVERFLOW_MASK;
1219
Tai Nguyen832c9272016-07-15 10:38:04 -07001220 if (!pmovsr)
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001221 goto out;
Tai Nguyen832c9272016-07-15 10:38:04 -07001222
1223 /* Clear interrupt flag */
1224 if (xgene_pmu->version == PCP_PMU_V1)
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001225 writel(0x0, csr + PMU_PMOVSR);
1226 else if (xgene_pmu->version == PCP_PMU_V2)
1227 writel(pmovsr, csr + PMU_PMOVSR);
Tai Nguyen832c9272016-07-15 10:38:04 -07001228 else
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001229 writel(pmovsr, csr + PMU_PMOVSCLR);
Tai Nguyen832c9272016-07-15 10:38:04 -07001230
1231 for (idx = 0; idx < PMU_MAX_COUNTERS; idx++) {
1232 struct perf_event *event = pmu_dev->pmu_counter_event[idx];
1233 int overflowed = pmovsr & BIT(idx);
1234
1235 /* Ignore if we don't have an event. */
1236 if (!event || !overflowed)
1237 continue;
1238 xgene_perf_event_update(event);
1239 xgene_perf_event_set_period(event);
1240 }
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001241
1242out:
1243 xgene_pmu->ops->start_counters(pmu_dev);
Tai Nguyen832c9272016-07-15 10:38:04 -07001244}
1245
1246static irqreturn_t xgene_pmu_isr(int irq, void *dev_id)
1247{
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001248 u32 intr_mcu, intr_mcb, intr_l3c, intr_iob;
Tai Nguyen832c9272016-07-15 10:38:04 -07001249 struct xgene_pmu_dev_ctx *ctx;
1250 struct xgene_pmu *xgene_pmu = dev_id;
1251 unsigned long flags;
1252 u32 val;
1253
1254 raw_spin_lock_irqsave(&xgene_pmu->lock, flags);
1255
1256 /* Get Interrupt PMU source */
1257 val = readl(xgene_pmu->pcppmu_csr + PCPPMU_INTSTATUS_REG);
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001258 if (xgene_pmu->version == PCP_PMU_V3) {
1259 intr_mcu = PCPPMU_V3_INT_MCU;
1260 intr_mcb = PCPPMU_V3_INT_MCB;
1261 intr_l3c = PCPPMU_V3_INT_L3C;
1262 intr_iob = PCPPMU_V3_INT_IOB;
1263 } else {
1264 intr_mcu = PCPPMU_INT_MCU;
1265 intr_mcb = PCPPMU_INT_MCB;
1266 intr_l3c = PCPPMU_INT_L3C;
1267 intr_iob = PCPPMU_INT_IOB;
1268 }
1269 if (val & intr_mcu) {
Tai Nguyen832c9272016-07-15 10:38:04 -07001270 list_for_each_entry(ctx, &xgene_pmu->mcpmus, next) {
1271 _xgene_pmu_isr(irq, ctx->pmu_dev);
1272 }
1273 }
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001274 if (val & intr_mcb) {
Tai Nguyen832c9272016-07-15 10:38:04 -07001275 list_for_each_entry(ctx, &xgene_pmu->mcbpmus, next) {
1276 _xgene_pmu_isr(irq, ctx->pmu_dev);
1277 }
1278 }
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001279 if (val & intr_l3c) {
Tai Nguyen832c9272016-07-15 10:38:04 -07001280 list_for_each_entry(ctx, &xgene_pmu->l3cpmus, next) {
1281 _xgene_pmu_isr(irq, ctx->pmu_dev);
1282 }
1283 }
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001284 if (val & intr_iob) {
Tai Nguyen832c9272016-07-15 10:38:04 -07001285 list_for_each_entry(ctx, &xgene_pmu->iobpmus, next) {
1286 _xgene_pmu_isr(irq, ctx->pmu_dev);
1287 }
1288 }
1289
1290 raw_spin_unlock_irqrestore(&xgene_pmu->lock, flags);
1291
1292 return IRQ_HANDLED;
1293}
1294
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001295static int acpi_pmu_probe_active_mcb_mcu_l3c(struct xgene_pmu *xgene_pmu,
1296 struct platform_device *pdev)
Tai Nguyen832c9272016-07-15 10:38:04 -07001297{
1298 void __iomem *csw_csr, *mcba_csr, *mcbb_csr;
1299 struct resource *res;
1300 unsigned int reg;
1301
1302 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1303 csw_csr = devm_ioremap_resource(&pdev->dev, res);
1304 if (IS_ERR(csw_csr)) {
1305 dev_err(&pdev->dev, "ioremap failed for CSW CSR resource\n");
1306 return PTR_ERR(csw_csr);
1307 }
1308
1309 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1310 mcba_csr = devm_ioremap_resource(&pdev->dev, res);
1311 if (IS_ERR(mcba_csr)) {
1312 dev_err(&pdev->dev, "ioremap failed for MCBA CSR resource\n");
1313 return PTR_ERR(mcba_csr);
1314 }
1315
1316 res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1317 mcbb_csr = devm_ioremap_resource(&pdev->dev, res);
1318 if (IS_ERR(mcbb_csr)) {
1319 dev_err(&pdev->dev, "ioremap failed for MCBB CSR resource\n");
1320 return PTR_ERR(mcbb_csr);
1321 }
1322
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001323 xgene_pmu->l3c_active_mask = 0x1;
1324
Tai Nguyen832c9272016-07-15 10:38:04 -07001325 reg = readl(csw_csr + CSW_CSWCR);
1326 if (reg & CSW_CSWCR_DUALMCB_MASK) {
1327 /* Dual MCB active */
1328 xgene_pmu->mcb_active_mask = 0x3;
1329 /* Probe all active MC(s) */
1330 reg = readl(mcbb_csr + CSW_CSWCR);
1331 xgene_pmu->mc_active_mask =
1332 (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
1333 } else {
1334 /* Single MCB active */
1335 xgene_pmu->mcb_active_mask = 0x1;
1336 /* Probe all active MC(s) */
1337 reg = readl(mcba_csr + CSW_CSWCR);
1338 xgene_pmu->mc_active_mask =
1339 (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
1340 }
1341
1342 return 0;
1343}
1344
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001345static int acpi_pmu_v3_probe_active_mcb_mcu_l3c(struct xgene_pmu *xgene_pmu,
1346 struct platform_device *pdev)
1347{
1348 void __iomem *csw_csr;
1349 struct resource *res;
1350 unsigned int reg;
1351 u32 mcb0routing;
1352 u32 mcb1routing;
1353
1354 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1355 csw_csr = devm_ioremap_resource(&pdev->dev, res);
1356 if (IS_ERR(csw_csr)) {
1357 dev_err(&pdev->dev, "ioremap failed for CSW CSR resource\n");
1358 return PTR_ERR(csw_csr);
1359 }
1360
1361 reg = readl(csw_csr + CSW_CSWCR);
1362 mcb0routing = CSW_CSWCR_MCB0_ROUTING(reg);
1363 mcb1routing = CSW_CSWCR_MCB1_ROUTING(reg);
1364 if (reg & CSW_CSWCR_DUALMCB_MASK) {
1365 /* Dual MCB active */
1366 xgene_pmu->mcb_active_mask = 0x3;
1367 /* Probe all active L3C(s), maximum is 8 */
1368 xgene_pmu->l3c_active_mask = 0xFF;
1369 /* Probe all active MC(s), maximum is 8 */
1370 if ((mcb0routing == 0x2) && (mcb1routing == 0x2))
1371 xgene_pmu->mc_active_mask = 0xFF;
1372 else if ((mcb0routing == 0x1) && (mcb1routing == 0x1))
1373 xgene_pmu->mc_active_mask = 0x33;
1374 else
1375 xgene_pmu->mc_active_mask = 0x11;
1376 } else {
1377 /* Single MCB active */
1378 xgene_pmu->mcb_active_mask = 0x1;
1379 /* Probe all active L3C(s), maximum is 4 */
1380 xgene_pmu->l3c_active_mask = 0x0F;
1381 /* Probe all active MC(s), maximum is 4 */
1382 if (mcb0routing == 0x2)
1383 xgene_pmu->mc_active_mask = 0x0F;
1384 else if (mcb0routing == 0x1)
1385 xgene_pmu->mc_active_mask = 0x03;
1386 else
1387 xgene_pmu->mc_active_mask = 0x01;
1388 }
1389
1390 return 0;
1391}
1392
1393static int fdt_pmu_probe_active_mcb_mcu_l3c(struct xgene_pmu *xgene_pmu,
1394 struct platform_device *pdev)
Tai Nguyen832c9272016-07-15 10:38:04 -07001395{
1396 struct regmap *csw_map, *mcba_map, *mcbb_map;
1397 struct device_node *np = pdev->dev.of_node;
1398 unsigned int reg;
1399
1400 csw_map = syscon_regmap_lookup_by_phandle(np, "regmap-csw");
1401 if (IS_ERR(csw_map)) {
1402 dev_err(&pdev->dev, "unable to get syscon regmap csw\n");
1403 return PTR_ERR(csw_map);
1404 }
1405
1406 mcba_map = syscon_regmap_lookup_by_phandle(np, "regmap-mcba");
1407 if (IS_ERR(mcba_map)) {
1408 dev_err(&pdev->dev, "unable to get syscon regmap mcba\n");
1409 return PTR_ERR(mcba_map);
1410 }
1411
1412 mcbb_map = syscon_regmap_lookup_by_phandle(np, "regmap-mcbb");
1413 if (IS_ERR(mcbb_map)) {
1414 dev_err(&pdev->dev, "unable to get syscon regmap mcbb\n");
1415 return PTR_ERR(mcbb_map);
1416 }
1417
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001418 xgene_pmu->l3c_active_mask = 0x1;
Tai Nguyen832c9272016-07-15 10:38:04 -07001419 if (regmap_read(csw_map, CSW_CSWCR, &reg))
1420 return -EINVAL;
1421
1422 if (reg & CSW_CSWCR_DUALMCB_MASK) {
1423 /* Dual MCB active */
1424 xgene_pmu->mcb_active_mask = 0x3;
1425 /* Probe all active MC(s) */
1426 if (regmap_read(mcbb_map, MCBADDRMR, &reg))
1427 return 0;
1428 xgene_pmu->mc_active_mask =
1429 (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
1430 } else {
1431 /* Single MCB active */
1432 xgene_pmu->mcb_active_mask = 0x1;
1433 /* Probe all active MC(s) */
1434 if (regmap_read(mcba_map, MCBADDRMR, &reg))
1435 return 0;
1436 xgene_pmu->mc_active_mask =
1437 (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
1438 }
1439
1440 return 0;
1441}
1442
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001443static int xgene_pmu_probe_active_mcb_mcu_l3c(struct xgene_pmu *xgene_pmu,
1444 struct platform_device *pdev)
Tai Nguyen832c9272016-07-15 10:38:04 -07001445{
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001446 if (has_acpi_companion(&pdev->dev)) {
1447 if (xgene_pmu->version == PCP_PMU_V3)
1448 return acpi_pmu_v3_probe_active_mcb_mcu_l3c(xgene_pmu,
1449 pdev);
1450 else
1451 return acpi_pmu_probe_active_mcb_mcu_l3c(xgene_pmu,
1452 pdev);
1453 }
1454 return fdt_pmu_probe_active_mcb_mcu_l3c(xgene_pmu, pdev);
Tai Nguyen832c9272016-07-15 10:38:04 -07001455}
1456
1457static char *xgene_pmu_dev_name(struct device *dev, u32 type, int id)
1458{
1459 switch (type) {
1460 case PMU_TYPE_L3C:
1461 return devm_kasprintf(dev, GFP_KERNEL, "l3c%d", id);
1462 case PMU_TYPE_IOB:
1463 return devm_kasprintf(dev, GFP_KERNEL, "iob%d", id);
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001464 case PMU_TYPE_IOB_SLOW:
Hoan Trana45fc262018-06-07 14:35:01 +01001465 return devm_kasprintf(dev, GFP_KERNEL, "iob_slow%d", id);
Tai Nguyen832c9272016-07-15 10:38:04 -07001466 case PMU_TYPE_MCB:
1467 return devm_kasprintf(dev, GFP_KERNEL, "mcb%d", id);
1468 case PMU_TYPE_MC:
1469 return devm_kasprintf(dev, GFP_KERNEL, "mc%d", id);
1470 default:
1471 return devm_kasprintf(dev, GFP_KERNEL, "unknown");
1472 }
1473}
1474
1475#if defined(CONFIG_ACPI)
1476static int acpi_pmu_dev_add_resource(struct acpi_resource *ares, void *data)
1477{
1478 struct resource *res = data;
1479
1480 if (ares->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32)
1481 acpi_dev_resource_memory(ares, res);
1482
1483 /* Always tell the ACPI core to skip this resource */
1484 return 1;
1485}
1486
1487static struct
1488xgene_pmu_dev_ctx *acpi_get_pmu_hw_inf(struct xgene_pmu *xgene_pmu,
1489 struct acpi_device *adev, u32 type)
1490{
1491 struct device *dev = xgene_pmu->dev;
1492 struct list_head resource_list;
1493 struct xgene_pmu_dev_ctx *ctx;
1494 const union acpi_object *obj;
1495 struct hw_pmu_info *inf;
1496 void __iomem *dev_csr;
1497 struct resource res;
1498 int enable_bit;
1499 int rc;
1500
1501 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1502 if (!ctx)
1503 return NULL;
1504
1505 INIT_LIST_HEAD(&resource_list);
1506 rc = acpi_dev_get_resources(adev, &resource_list,
1507 acpi_pmu_dev_add_resource, &res);
1508 acpi_dev_free_resource_list(&resource_list);
Tai Nguyen9a1a1f42016-10-13 11:09:16 -07001509 if (rc < 0) {
Tai Nguyen832c9272016-07-15 10:38:04 -07001510 dev_err(dev, "PMU type %d: No resource address found\n", type);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001511 return NULL;
Tai Nguyen832c9272016-07-15 10:38:04 -07001512 }
1513
1514 dev_csr = devm_ioremap_resource(dev, &res);
1515 if (IS_ERR(dev_csr)) {
1516 dev_err(dev, "PMU type %d: Fail to map resource\n", type);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001517 return NULL;
Tai Nguyen832c9272016-07-15 10:38:04 -07001518 }
1519
1520 /* A PMU device node without enable-bit-index is always enabled */
1521 rc = acpi_dev_get_property(adev, "enable-bit-index",
1522 ACPI_TYPE_INTEGER, &obj);
1523 if (rc < 0)
1524 enable_bit = 0;
1525 else
1526 enable_bit = (int) obj->integer.value;
1527
1528 ctx->name = xgene_pmu_dev_name(dev, type, enable_bit);
1529 if (!ctx->name) {
1530 dev_err(dev, "PMU type %d: Fail to get device name\n", type);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001531 return NULL;
Tai Nguyen832c9272016-07-15 10:38:04 -07001532 }
1533 inf = &ctx->inf;
1534 inf->type = type;
1535 inf->csr = dev_csr;
1536 inf->enable_mask = 1 << enable_bit;
1537
1538 return ctx;
Tai Nguyen832c9272016-07-15 10:38:04 -07001539}
1540
Hoan Tran838955e2017-06-22 19:26:03 +01001541static const struct acpi_device_id xgene_pmu_acpi_type_match[] = {
1542 {"APMC0D5D", PMU_TYPE_L3C},
1543 {"APMC0D5E", PMU_TYPE_IOB},
1544 {"APMC0D5F", PMU_TYPE_MCB},
1545 {"APMC0D60", PMU_TYPE_MC},
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001546 {"APMC0D84", PMU_TYPE_L3C},
1547 {"APMC0D85", PMU_TYPE_IOB},
1548 {"APMC0D86", PMU_TYPE_IOB_SLOW},
1549 {"APMC0D87", PMU_TYPE_MCB},
1550 {"APMC0D88", PMU_TYPE_MC},
Hoan Tran838955e2017-06-22 19:26:03 +01001551 {},
1552};
1553
1554static const struct acpi_device_id *xgene_pmu_acpi_match_type(
1555 const struct acpi_device_id *ids,
1556 struct acpi_device *adev)
1557{
1558 const struct acpi_device_id *match_id = NULL;
1559 const struct acpi_device_id *id;
1560
1561 for (id = ids; id->id[0] || id->cls; id++) {
1562 if (!acpi_match_device_ids(adev, id))
1563 match_id = id;
1564 else if (match_id)
1565 break;
1566 }
1567
1568 return match_id;
1569}
1570
Tai Nguyen832c9272016-07-15 10:38:04 -07001571static acpi_status acpi_pmu_dev_add(acpi_handle handle, u32 level,
1572 void *data, void **return_value)
1573{
Hoan Tran838955e2017-06-22 19:26:03 +01001574 const struct acpi_device_id *acpi_id;
Tai Nguyen832c9272016-07-15 10:38:04 -07001575 struct xgene_pmu *xgene_pmu = data;
1576 struct xgene_pmu_dev_ctx *ctx;
1577 struct acpi_device *adev;
1578
1579 if (acpi_bus_get_device(handle, &adev))
1580 return AE_OK;
1581 if (acpi_bus_get_status(adev) || !adev->status.present)
1582 return AE_OK;
1583
Hoan Tran838955e2017-06-22 19:26:03 +01001584 acpi_id = xgene_pmu_acpi_match_type(xgene_pmu_acpi_type_match, adev);
1585 if (!acpi_id)
1586 return AE_OK;
Tai Nguyen832c9272016-07-15 10:38:04 -07001587
Hoan Tran838955e2017-06-22 19:26:03 +01001588 ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, (u32)acpi_id->driver_data);
Tai Nguyen832c9272016-07-15 10:38:04 -07001589 if (!ctx)
1590 return AE_OK;
1591
1592 if (xgene_pmu_dev_add(xgene_pmu, ctx)) {
1593 /* Can't add the PMU device, skip it */
1594 devm_kfree(xgene_pmu->dev, ctx);
1595 return AE_OK;
1596 }
1597
1598 switch (ctx->inf.type) {
1599 case PMU_TYPE_L3C:
1600 list_add(&ctx->next, &xgene_pmu->l3cpmus);
1601 break;
1602 case PMU_TYPE_IOB:
1603 list_add(&ctx->next, &xgene_pmu->iobpmus);
1604 break;
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001605 case PMU_TYPE_IOB_SLOW:
1606 list_add(&ctx->next, &xgene_pmu->iobpmus);
1607 break;
Tai Nguyen832c9272016-07-15 10:38:04 -07001608 case PMU_TYPE_MCB:
1609 list_add(&ctx->next, &xgene_pmu->mcbpmus);
1610 break;
1611 case PMU_TYPE_MC:
1612 list_add(&ctx->next, &xgene_pmu->mcpmus);
1613 break;
1614 }
1615 return AE_OK;
1616}
1617
1618static int acpi_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1619 struct platform_device *pdev)
1620{
1621 struct device *dev = xgene_pmu->dev;
1622 acpi_handle handle;
1623 acpi_status status;
1624
1625 handle = ACPI_HANDLE(dev);
1626 if (!handle)
1627 return -EINVAL;
1628
1629 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1630 acpi_pmu_dev_add, NULL, xgene_pmu, NULL);
1631 if (ACPI_FAILURE(status)) {
1632 dev_err(dev, "failed to probe PMU devices\n");
1633 return -ENODEV;
1634 }
1635
1636 return 0;
1637}
1638#else
1639static int acpi_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1640 struct platform_device *pdev)
1641{
1642 return 0;
1643}
1644#endif
1645
1646static struct
1647xgene_pmu_dev_ctx *fdt_get_pmu_hw_inf(struct xgene_pmu *xgene_pmu,
1648 struct device_node *np, u32 type)
1649{
1650 struct device *dev = xgene_pmu->dev;
1651 struct xgene_pmu_dev_ctx *ctx;
1652 struct hw_pmu_info *inf;
1653 void __iomem *dev_csr;
1654 struct resource res;
1655 int enable_bit;
Tai Nguyen832c9272016-07-15 10:38:04 -07001656
1657 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1658 if (!ctx)
1659 return NULL;
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001660
1661 if (of_address_to_resource(np, 0, &res) < 0) {
Tai Nguyen832c9272016-07-15 10:38:04 -07001662 dev_err(dev, "PMU type %d: No resource address found\n", type);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001663 return NULL;
Tai Nguyen832c9272016-07-15 10:38:04 -07001664 }
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001665
Tai Nguyen832c9272016-07-15 10:38:04 -07001666 dev_csr = devm_ioremap_resource(dev, &res);
1667 if (IS_ERR(dev_csr)) {
1668 dev_err(dev, "PMU type %d: Fail to map resource\n", type);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001669 return NULL;
Tai Nguyen832c9272016-07-15 10:38:04 -07001670 }
1671
1672 /* A PMU device node without enable-bit-index is always enabled */
1673 if (of_property_read_u32(np, "enable-bit-index", &enable_bit))
1674 enable_bit = 0;
1675
1676 ctx->name = xgene_pmu_dev_name(dev, type, enable_bit);
1677 if (!ctx->name) {
1678 dev_err(dev, "PMU type %d: Fail to get device name\n", type);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001679 return NULL;
Tai Nguyen832c9272016-07-15 10:38:04 -07001680 }
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001681
Tai Nguyen832c9272016-07-15 10:38:04 -07001682 inf = &ctx->inf;
1683 inf->type = type;
1684 inf->csr = dev_csr;
1685 inf->enable_mask = 1 << enable_bit;
1686
1687 return ctx;
Tai Nguyen832c9272016-07-15 10:38:04 -07001688}
1689
1690static int fdt_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1691 struct platform_device *pdev)
1692{
1693 struct xgene_pmu_dev_ctx *ctx;
1694 struct device_node *np;
1695
1696 for_each_child_of_node(pdev->dev.of_node, np) {
1697 if (!of_device_is_available(np))
1698 continue;
1699
1700 if (of_device_is_compatible(np, "apm,xgene-pmu-l3c"))
1701 ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_L3C);
1702 else if (of_device_is_compatible(np, "apm,xgene-pmu-iob"))
1703 ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_IOB);
1704 else if (of_device_is_compatible(np, "apm,xgene-pmu-mcb"))
1705 ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_MCB);
1706 else if (of_device_is_compatible(np, "apm,xgene-pmu-mc"))
1707 ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_MC);
1708 else
1709 ctx = NULL;
1710
1711 if (!ctx)
1712 continue;
1713
1714 if (xgene_pmu_dev_add(xgene_pmu, ctx)) {
1715 /* Can't add the PMU device, skip it */
1716 devm_kfree(xgene_pmu->dev, ctx);
1717 continue;
1718 }
1719
1720 switch (ctx->inf.type) {
1721 case PMU_TYPE_L3C:
1722 list_add(&ctx->next, &xgene_pmu->l3cpmus);
1723 break;
1724 case PMU_TYPE_IOB:
1725 list_add(&ctx->next, &xgene_pmu->iobpmus);
1726 break;
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001727 case PMU_TYPE_IOB_SLOW:
1728 list_add(&ctx->next, &xgene_pmu->iobpmus);
1729 break;
Tai Nguyen832c9272016-07-15 10:38:04 -07001730 case PMU_TYPE_MCB:
1731 list_add(&ctx->next, &xgene_pmu->mcbpmus);
1732 break;
1733 case PMU_TYPE_MC:
1734 list_add(&ctx->next, &xgene_pmu->mcpmus);
1735 break;
1736 }
1737 }
1738
1739 return 0;
1740}
1741
1742static int xgene_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1743 struct platform_device *pdev)
1744{
1745 if (has_acpi_companion(&pdev->dev))
1746 return acpi_pmu_probe_pmu_dev(xgene_pmu, pdev);
1747 return fdt_pmu_probe_pmu_dev(xgene_pmu, pdev);
1748}
1749
1750static const struct xgene_pmu_data xgene_pmu_data = {
1751 .id = PCP_PMU_V1,
1752};
1753
1754static const struct xgene_pmu_data xgene_pmu_v2_data = {
1755 .id = PCP_PMU_V2,
1756};
1757
Hoan Trane35e0a02017-06-22 19:26:04 +01001758static const struct xgene_pmu_ops xgene_pmu_ops = {
1759 .mask_int = xgene_pmu_mask_int,
1760 .unmask_int = xgene_pmu_unmask_int,
1761 .read_counter = xgene_pmu_read_counter32,
1762 .write_counter = xgene_pmu_write_counter32,
1763 .write_evttype = xgene_pmu_write_evttype,
1764 .write_agentmsk = xgene_pmu_write_agentmsk,
1765 .write_agent1msk = xgene_pmu_write_agent1msk,
1766 .enable_counter = xgene_pmu_enable_counter,
1767 .disable_counter = xgene_pmu_disable_counter,
1768 .enable_counter_int = xgene_pmu_enable_counter_int,
1769 .disable_counter_int = xgene_pmu_disable_counter_int,
1770 .reset_counters = xgene_pmu_reset_counters,
1771 .start_counters = xgene_pmu_start_counters,
1772 .stop_counters = xgene_pmu_stop_counters,
1773};
1774
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001775static const struct xgene_pmu_ops xgene_pmu_v3_ops = {
1776 .mask_int = xgene_pmu_v3_mask_int,
1777 .unmask_int = xgene_pmu_v3_unmask_int,
1778 .read_counter = xgene_pmu_read_counter64,
1779 .write_counter = xgene_pmu_write_counter64,
1780 .write_evttype = xgene_pmu_write_evttype,
1781 .write_agentmsk = xgene_pmu_v3_write_agentmsk,
1782 .write_agent1msk = xgene_pmu_v3_write_agent1msk,
1783 .enable_counter = xgene_pmu_enable_counter,
1784 .disable_counter = xgene_pmu_disable_counter,
1785 .enable_counter_int = xgene_pmu_enable_counter_int,
1786 .disable_counter_int = xgene_pmu_disable_counter_int,
1787 .reset_counters = xgene_pmu_reset_counters,
1788 .start_counters = xgene_pmu_start_counters,
1789 .stop_counters = xgene_pmu_stop_counters,
1790};
1791
Tai Nguyen832c9272016-07-15 10:38:04 -07001792static const struct of_device_id xgene_pmu_of_match[] = {
1793 { .compatible = "apm,xgene-pmu", .data = &xgene_pmu_data },
1794 { .compatible = "apm,xgene-pmu-v2", .data = &xgene_pmu_v2_data },
1795 {},
1796};
1797MODULE_DEVICE_TABLE(of, xgene_pmu_of_match);
1798#ifdef CONFIG_ACPI
1799static const struct acpi_device_id xgene_pmu_acpi_match[] = {
1800 {"APMC0D5B", PCP_PMU_V1},
1801 {"APMC0D5C", PCP_PMU_V2},
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001802 {"APMC0D83", PCP_PMU_V3},
Tai Nguyen832c9272016-07-15 10:38:04 -07001803 {},
1804};
1805MODULE_DEVICE_TABLE(acpi, xgene_pmu_acpi_match);
1806#endif
1807
Hoan Trancbb72a32018-11-07 19:40:58 +00001808static int xgene_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
1809{
1810 struct xgene_pmu *xgene_pmu = hlist_entry_safe(node, struct xgene_pmu,
1811 node);
1812
1813 if (cpumask_empty(&xgene_pmu->cpu))
1814 cpumask_set_cpu(cpu, &xgene_pmu->cpu);
1815
1816 /* Overflow interrupt also should use the same CPU */
1817 WARN_ON(irq_set_affinity(xgene_pmu->irq, &xgene_pmu->cpu));
1818
1819 return 0;
1820}
1821
1822static int xgene_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
1823{
1824 struct xgene_pmu *xgene_pmu = hlist_entry_safe(node, struct xgene_pmu,
1825 node);
1826 struct xgene_pmu_dev_ctx *ctx;
1827 unsigned int target;
1828
1829 if (!cpumask_test_and_clear_cpu(cpu, &xgene_pmu->cpu))
1830 return 0;
1831 target = cpumask_any_but(cpu_online_mask, cpu);
1832 if (target >= nr_cpu_ids)
1833 return 0;
1834
1835 list_for_each_entry(ctx, &xgene_pmu->mcpmus, next) {
1836 perf_pmu_migrate_context(&ctx->pmu_dev->pmu, cpu, target);
1837 }
1838 list_for_each_entry(ctx, &xgene_pmu->mcbpmus, next) {
1839 perf_pmu_migrate_context(&ctx->pmu_dev->pmu, cpu, target);
1840 }
1841 list_for_each_entry(ctx, &xgene_pmu->l3cpmus, next) {
1842 perf_pmu_migrate_context(&ctx->pmu_dev->pmu, cpu, target);
1843 }
1844 list_for_each_entry(ctx, &xgene_pmu->iobpmus, next) {
1845 perf_pmu_migrate_context(&ctx->pmu_dev->pmu, cpu, target);
1846 }
1847
1848 cpumask_set_cpu(target, &xgene_pmu->cpu);
1849 /* Overflow interrupt also should use the same CPU */
1850 WARN_ON(irq_set_affinity(xgene_pmu->irq, &xgene_pmu->cpu));
1851
1852 return 0;
1853}
1854
Tai Nguyen832c9272016-07-15 10:38:04 -07001855static int xgene_pmu_probe(struct platform_device *pdev)
1856{
1857 const struct xgene_pmu_data *dev_data;
1858 const struct of_device_id *of_id;
1859 struct xgene_pmu *xgene_pmu;
1860 struct resource *res;
1861 int irq, rc;
1862 int version;
1863
Hoan Trancbb72a32018-11-07 19:40:58 +00001864 /* Install a hook to update the reader CPU in case it goes offline */
1865 rc = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE,
1866 "CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE",
1867 xgene_pmu_online_cpu,
1868 xgene_pmu_offline_cpu);
1869 if (rc)
1870 return rc;
1871
Tai Nguyen832c9272016-07-15 10:38:04 -07001872 xgene_pmu = devm_kzalloc(&pdev->dev, sizeof(*xgene_pmu), GFP_KERNEL);
1873 if (!xgene_pmu)
1874 return -ENOMEM;
1875 xgene_pmu->dev = &pdev->dev;
1876 platform_set_drvdata(pdev, xgene_pmu);
1877
1878 version = -EINVAL;
1879 of_id = of_match_device(xgene_pmu_of_match, &pdev->dev);
1880 if (of_id) {
1881 dev_data = (const struct xgene_pmu_data *) of_id->data;
1882 version = dev_data->id;
1883 }
1884
1885#ifdef CONFIG_ACPI
1886 if (ACPI_COMPANION(&pdev->dev)) {
1887 const struct acpi_device_id *acpi_id;
1888
1889 acpi_id = acpi_match_device(xgene_pmu_acpi_match, &pdev->dev);
1890 if (acpi_id)
1891 version = (int) acpi_id->driver_data;
1892 }
1893#endif
1894 if (version < 0)
1895 return -ENODEV;
1896
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001897 if (version == PCP_PMU_V3)
1898 xgene_pmu->ops = &xgene_pmu_v3_ops;
1899 else
1900 xgene_pmu->ops = &xgene_pmu_ops;
Hoan Trane35e0a02017-06-22 19:26:04 +01001901
Tai Nguyen832c9272016-07-15 10:38:04 -07001902 INIT_LIST_HEAD(&xgene_pmu->l3cpmus);
1903 INIT_LIST_HEAD(&xgene_pmu->iobpmus);
1904 INIT_LIST_HEAD(&xgene_pmu->mcbpmus);
1905 INIT_LIST_HEAD(&xgene_pmu->mcpmus);
1906
1907 xgene_pmu->version = version;
1908 dev_info(&pdev->dev, "X-Gene PMU version %d\n", xgene_pmu->version);
1909
1910 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1911 xgene_pmu->pcppmu_csr = devm_ioremap_resource(&pdev->dev, res);
1912 if (IS_ERR(xgene_pmu->pcppmu_csr)) {
1913 dev_err(&pdev->dev, "ioremap failed for PCP PMU resource\n");
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001914 return PTR_ERR(xgene_pmu->pcppmu_csr);
Tai Nguyen832c9272016-07-15 10:38:04 -07001915 }
1916
1917 irq = platform_get_irq(pdev, 0);
1918 if (irq < 0) {
1919 dev_err(&pdev->dev, "No IRQ resource\n");
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001920 return -EINVAL;
Tai Nguyen832c9272016-07-15 10:38:04 -07001921 }
Hoan Trancbb72a32018-11-07 19:40:58 +00001922
Tai Nguyen832c9272016-07-15 10:38:04 -07001923 rc = devm_request_irq(&pdev->dev, irq, xgene_pmu_isr,
1924 IRQF_NOBALANCING | IRQF_NO_THREAD,
1925 dev_name(&pdev->dev), xgene_pmu);
1926 if (rc) {
1927 dev_err(&pdev->dev, "Could not request IRQ %d\n", irq);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001928 return rc;
Tai Nguyen832c9272016-07-15 10:38:04 -07001929 }
1930
Hoan Trancbb72a32018-11-07 19:40:58 +00001931 xgene_pmu->irq = irq;
1932
Tai Nguyen832c9272016-07-15 10:38:04 -07001933 raw_spin_lock_init(&xgene_pmu->lock);
1934
1935 /* Check for active MCBs and MCUs */
Hoan Tranc0f7f7a2017-06-22 19:26:05 +01001936 rc = xgene_pmu_probe_active_mcb_mcu_l3c(xgene_pmu, pdev);
Tai Nguyen832c9272016-07-15 10:38:04 -07001937 if (rc) {
1938 dev_warn(&pdev->dev, "Unknown MCB/MCU active status\n");
1939 xgene_pmu->mcb_active_mask = 0x1;
1940 xgene_pmu->mc_active_mask = 0x1;
1941 }
1942
Hoan Trancbb72a32018-11-07 19:40:58 +00001943 /* Add this instance to the list used by the hotplug callback */
1944 rc = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE,
1945 &xgene_pmu->node);
Tai Nguyen832c9272016-07-15 10:38:04 -07001946 if (rc) {
Hoan Trancbb72a32018-11-07 19:40:58 +00001947 dev_err(&pdev->dev, "Error %d registering hotplug", rc);
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001948 return rc;
Tai Nguyen832c9272016-07-15 10:38:04 -07001949 }
1950
1951 /* Walk through the tree for all PMU perf devices */
1952 rc = xgene_pmu_probe_pmu_dev(xgene_pmu, pdev);
1953 if (rc) {
1954 dev_err(&pdev->dev, "No PMU perf devices found!\n");
Hoan Trancbb72a32018-11-07 19:40:58 +00001955 goto out_unregister;
Tai Nguyen832c9272016-07-15 10:38:04 -07001956 }
1957
1958 /* Enable interrupt */
Hoan Trane35e0a02017-06-22 19:26:04 +01001959 xgene_pmu->ops->unmask_int(xgene_pmu);
Tai Nguyen832c9272016-07-15 10:38:04 -07001960
1961 return 0;
Hoan Trancbb72a32018-11-07 19:40:58 +00001962
1963out_unregister:
1964 cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE,
1965 &xgene_pmu->node);
1966 return rc;
Tai Nguyen832c9272016-07-15 10:38:04 -07001967}
1968
1969static void
1970xgene_pmu_dev_cleanup(struct xgene_pmu *xgene_pmu, struct list_head *pmus)
1971{
1972 struct xgene_pmu_dev_ctx *ctx;
Tai Nguyen832c9272016-07-15 10:38:04 -07001973
1974 list_for_each_entry(ctx, pmus, next) {
Tai Nguyenc1be2ddb2017-07-13 11:19:08 -07001975 perf_pmu_unregister(&ctx->pmu_dev->pmu);
Tai Nguyen832c9272016-07-15 10:38:04 -07001976 }
1977}
1978
1979static int xgene_pmu_remove(struct platform_device *pdev)
1980{
1981 struct xgene_pmu *xgene_pmu = dev_get_drvdata(&pdev->dev);
1982
1983 xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->l3cpmus);
1984 xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->iobpmus);
1985 xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->mcbpmus);
1986 xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->mcpmus);
Hoan Trancbb72a32018-11-07 19:40:58 +00001987 cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE,
1988 &xgene_pmu->node);
Tai Nguyen832c9272016-07-15 10:38:04 -07001989
Tai Nguyen832c9272016-07-15 10:38:04 -07001990 return 0;
1991}
1992
1993static struct platform_driver xgene_pmu_driver = {
1994 .probe = xgene_pmu_probe,
1995 .remove = xgene_pmu_remove,
1996 .driver = {
1997 .name = "xgene-pmu",
1998 .of_match_table = xgene_pmu_of_match,
1999 .acpi_match_table = ACPI_PTR(xgene_pmu_acpi_match),
2000 },
2001};
2002
2003builtin_platform_driver(xgene_pmu_driver);