1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016 Freescale Semiconductor, Inc.
7 #include <linux/bitfield.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
11 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/of_device.h>
15 #include <linux/of_irq.h>
16 #include <linux/perf_event.h>
17 #include <linux/slab.h>
19 #define COUNTER_CNTL 0x0
20 #define COUNTER_READ 0x20
22 #define COUNTER_DPCR1 0x30
25 #define CNTL_CLEAR 0x2
27 #define CNTL_EN_MASK 0xFFFFFFFB
28 #define CNTL_CLEAR_MASK 0xFFFFFFFD
29 #define CNTL_OVER_MASK 0xFFFFFFFE
31 #define CNTL_CSV_SHIFT 24
32 #define CNTL_CSV_MASK (0xFF << CNTL_CSV_SHIFT)
34 #define EVENT_CYCLES_ID 0
35 #define EVENT_CYCLES_COUNTER 0
36 #define NUM_COUNTERS 4
38 #define AXI_MASKING_REVERT 0xffff0000 /* AXI_MASKING(MSB 16bits) + AXI_ID(LSB 16bits) */
40 #define to_ddr_pmu(p) container_of(p, struct ddr_pmu, pmu)
42 #define DDR_PERF_DEV_NAME "imx8_ddr"
43 #define DDR_CPUHP_CB_NAME DDR_PERF_DEV_NAME "_perf_pmu"
45 static DEFINE_IDA(ddr_ida
);
47 /* DDR Perf hardware feature */
48 #define DDR_CAP_AXI_ID_FILTER 0x1 /* support AXI ID filter */
49 #define DDR_CAP_AXI_ID_FILTER_ENHANCED 0x3 /* support enhanced AXI ID filter */
51 struct fsl_ddr_devtype_data
{
52 unsigned int quirks
; /* quirks needed for different DDR Perf core */
55 static const struct fsl_ddr_devtype_data imx8_devtype_data
;
57 static const struct fsl_ddr_devtype_data imx8m_devtype_data
= {
58 .quirks
= DDR_CAP_AXI_ID_FILTER
,
61 static const struct of_device_id imx_ddr_pmu_dt_ids
[] = {
62 { .compatible
= "fsl,imx8-ddr-pmu", .data
= &imx8_devtype_data
},
63 { .compatible
= "fsl,imx8m-ddr-pmu", .data
= &imx8m_devtype_data
},
66 MODULE_DEVICE_TABLE(of
, imx_ddr_pmu_dt_ids
);
72 struct hlist_node node
;
74 struct perf_event
*events
[NUM_COUNTERS
];
76 enum cpuhp_state cpuhp_state
;
77 const struct fsl_ddr_devtype_data
*devtype_data
;
82 static ssize_t
ddr_perf_cpumask_show(struct device
*dev
,
83 struct device_attribute
*attr
, char *buf
)
85 struct ddr_pmu
*pmu
= dev_get_drvdata(dev
);
87 return cpumap_print_to_pagebuf(true, buf
, cpumask_of(pmu
->cpu
));
90 static struct device_attribute ddr_perf_cpumask_attr
=
91 __ATTR(cpumask
, 0444, ddr_perf_cpumask_show
, NULL
);
93 static struct attribute
*ddr_perf_cpumask_attrs
[] = {
94 &ddr_perf_cpumask_attr
.attr
,
98 static struct attribute_group ddr_perf_cpumask_attr_group
= {
99 .attrs
= ddr_perf_cpumask_attrs
,
103 ddr_pmu_event_show(struct device
*dev
, struct device_attribute
*attr
,
106 struct perf_pmu_events_attr
*pmu_attr
;
108 pmu_attr
= container_of(attr
, struct perf_pmu_events_attr
, attr
);
109 return sprintf(page
, "event=0x%02llx\n", pmu_attr
->id
);
112 #define IMX8_DDR_PMU_EVENT_ATTR(_name, _id) \
113 (&((struct perf_pmu_events_attr[]) { \
114 { .attr = __ATTR(_name, 0444, ddr_pmu_event_show, NULL),\
118 static struct attribute
*ddr_perf_events_attrs
[] = {
119 IMX8_DDR_PMU_EVENT_ATTR(cycles
, EVENT_CYCLES_ID
),
120 IMX8_DDR_PMU_EVENT_ATTR(selfresh
, 0x01),
121 IMX8_DDR_PMU_EVENT_ATTR(read
-accesses
, 0x04),
122 IMX8_DDR_PMU_EVENT_ATTR(write
-accesses
, 0x05),
123 IMX8_DDR_PMU_EVENT_ATTR(read
-queue
-depth
, 0x08),
124 IMX8_DDR_PMU_EVENT_ATTR(write
-queue
-depth
, 0x09),
125 IMX8_DDR_PMU_EVENT_ATTR(lp
-read
-credit
-cnt
, 0x10),
126 IMX8_DDR_PMU_EVENT_ATTR(hp
-read
-credit
-cnt
, 0x11),
127 IMX8_DDR_PMU_EVENT_ATTR(write
-credit
-cnt
, 0x12),
128 IMX8_DDR_PMU_EVENT_ATTR(read
-command
, 0x20),
129 IMX8_DDR_PMU_EVENT_ATTR(write
-command
, 0x21),
130 IMX8_DDR_PMU_EVENT_ATTR(read
-modify
-write
-command
, 0x22),
131 IMX8_DDR_PMU_EVENT_ATTR(hp
-read
, 0x23),
132 IMX8_DDR_PMU_EVENT_ATTR(hp
-req
-nocredit
, 0x24),
133 IMX8_DDR_PMU_EVENT_ATTR(hp
-xact
-credit
, 0x25),
134 IMX8_DDR_PMU_EVENT_ATTR(lp
-req
-nocredit
, 0x26),
135 IMX8_DDR_PMU_EVENT_ATTR(lp
-xact
-credit
, 0x27),
136 IMX8_DDR_PMU_EVENT_ATTR(wr
-xact
-credit
, 0x29),
137 IMX8_DDR_PMU_EVENT_ATTR(read
-cycles
, 0x2a),
138 IMX8_DDR_PMU_EVENT_ATTR(write
-cycles
, 0x2b),
139 IMX8_DDR_PMU_EVENT_ATTR(read
-write
-transition
, 0x30),
140 IMX8_DDR_PMU_EVENT_ATTR(precharge
, 0x31),
141 IMX8_DDR_PMU_EVENT_ATTR(activate
, 0x32),
142 IMX8_DDR_PMU_EVENT_ATTR(load
-mode
, 0x33),
143 IMX8_DDR_PMU_EVENT_ATTR(perf
-mwr
, 0x34),
144 IMX8_DDR_PMU_EVENT_ATTR(read
, 0x35),
145 IMX8_DDR_PMU_EVENT_ATTR(read
-activate
, 0x36),
146 IMX8_DDR_PMU_EVENT_ATTR(refresh
, 0x37),
147 IMX8_DDR_PMU_EVENT_ATTR(write
, 0x38),
148 IMX8_DDR_PMU_EVENT_ATTR(raw
-hazard
, 0x39),
149 IMX8_DDR_PMU_EVENT_ATTR(axid
-read
, 0x41),
150 IMX8_DDR_PMU_EVENT_ATTR(axid
-write
, 0x42),
154 static struct attribute_group ddr_perf_events_attr_group
= {
156 .attrs
= ddr_perf_events_attrs
,
159 PMU_FORMAT_ATTR(event
, "config:0-7");
160 PMU_FORMAT_ATTR(axi_id
, "config1:0-15");
161 PMU_FORMAT_ATTR(axi_mask
, "config1:16-31");
163 static struct attribute
*ddr_perf_format_attrs
[] = {
164 &format_attr_event
.attr
,
165 &format_attr_axi_id
.attr
,
166 &format_attr_axi_mask
.attr
,
170 static struct attribute_group ddr_perf_format_attr_group
= {
172 .attrs
= ddr_perf_format_attrs
,
175 static const struct attribute_group
*attr_groups
[] = {
176 &ddr_perf_events_attr_group
,
177 &ddr_perf_format_attr_group
,
178 &ddr_perf_cpumask_attr_group
,
182 static bool ddr_perf_is_filtered(struct perf_event
*event
)
184 return event
->attr
.config
== 0x41 || event
->attr
.config
== 0x42;
187 static u32
ddr_perf_filter_val(struct perf_event
*event
)
189 return event
->attr
.config1
;
192 static bool ddr_perf_filters_compatible(struct perf_event
*a
,
193 struct perf_event
*b
)
195 if (!ddr_perf_is_filtered(a
))
197 if (!ddr_perf_is_filtered(b
))
199 return ddr_perf_filter_val(a
) == ddr_perf_filter_val(b
);
202 static bool ddr_perf_is_enhanced_filtered(struct perf_event
*event
)
205 struct ddr_pmu
*pmu
= to_ddr_pmu(event
->pmu
);
207 filt
= pmu
->devtype_data
->quirks
& DDR_CAP_AXI_ID_FILTER_ENHANCED
;
208 return (filt
== DDR_CAP_AXI_ID_FILTER_ENHANCED
) &&
209 ddr_perf_is_filtered(event
);
212 static u32
ddr_perf_alloc_counter(struct ddr_pmu
*pmu
, int event
)
217 * Always map cycle event to counter 0
218 * Cycles counter is dedicated for cycle event
219 * can't used for the other events
221 if (event
== EVENT_CYCLES_ID
) {
222 if (pmu
->events
[EVENT_CYCLES_COUNTER
] == NULL
)
223 return EVENT_CYCLES_COUNTER
;
228 for (i
= 1; i
< NUM_COUNTERS
; i
++) {
229 if (pmu
->events
[i
] == NULL
)
236 static void ddr_perf_free_counter(struct ddr_pmu
*pmu
, int counter
)
238 pmu
->events
[counter
] = NULL
;
241 static u32
ddr_perf_read_counter(struct ddr_pmu
*pmu
, int counter
)
243 struct perf_event
*event
= pmu
->events
[counter
];
244 void __iomem
*base
= pmu
->base
;
247 * return bytes instead of bursts from ddr transaction for
248 * axid-read and axid-write event if PMU core supports enhanced
251 base
+= ddr_perf_is_enhanced_filtered(event
) ? COUNTER_DPCR1
:
253 return readl_relaxed(base
+ counter
* 4);
256 static int ddr_perf_event_init(struct perf_event
*event
)
258 struct ddr_pmu
*pmu
= to_ddr_pmu(event
->pmu
);
259 struct hw_perf_event
*hwc
= &event
->hw
;
260 struct perf_event
*sibling
;
262 if (event
->attr
.type
!= event
->pmu
->type
)
265 if (is_sampling_event(event
) || event
->attach_state
& PERF_ATTACH_TASK
)
268 if (event
->cpu
< 0) {
269 dev_warn(pmu
->dev
, "Can't provide per-task data!\n");
274 * We must NOT create groups containing mixed PMUs, although software
275 * events are acceptable (for example to create a CCN group
276 * periodically read when a hrtimer aka cpu-clock leader triggers).
278 if (event
->group_leader
->pmu
!= event
->pmu
&&
279 !is_software_event(event
->group_leader
))
282 if (pmu
->devtype_data
->quirks
& DDR_CAP_AXI_ID_FILTER
) {
283 if (!ddr_perf_filters_compatible(event
, event
->group_leader
))
285 for_each_sibling_event(sibling
, event
->group_leader
) {
286 if (!ddr_perf_filters_compatible(event
, sibling
))
291 for_each_sibling_event(sibling
, event
->group_leader
) {
292 if (sibling
->pmu
!= event
->pmu
&&
293 !is_software_event(sibling
))
297 event
->cpu
= pmu
->cpu
;
304 static void ddr_perf_event_update(struct perf_event
*event
)
306 struct ddr_pmu
*pmu
= to_ddr_pmu(event
->pmu
);
307 struct hw_perf_event
*hwc
= &event
->hw
;
308 u64 delta
, prev_raw_count
, new_raw_count
;
309 int counter
= hwc
->idx
;
312 prev_raw_count
= local64_read(&hwc
->prev_count
);
313 new_raw_count
= ddr_perf_read_counter(pmu
, counter
);
314 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
315 new_raw_count
) != prev_raw_count
);
317 delta
= (new_raw_count
- prev_raw_count
) & 0xFFFFFFFF;
319 local64_add(delta
, &event
->count
);
322 static void ddr_perf_counter_enable(struct ddr_pmu
*pmu
, int config
,
323 int counter
, bool enable
)
325 u8 reg
= counter
* 4 + COUNTER_CNTL
;
330 * cycle counter is special which should firstly write 0 then
331 * write 1 into CLEAR bit to clear it. Other counters only
332 * need write 0 into CLEAR bit and it turns out to be 1 by
333 * hardware. Below enable flow is harmless for all counters.
335 writel(0, pmu
->base
+ reg
);
336 val
= CNTL_EN
| CNTL_CLEAR
;
337 val
|= FIELD_PREP(CNTL_CSV_MASK
, config
);
338 writel(val
, pmu
->base
+ reg
);
340 /* Disable counter */
341 val
= readl_relaxed(pmu
->base
+ reg
) & CNTL_EN_MASK
;
342 writel(val
, pmu
->base
+ reg
);
346 static void ddr_perf_event_start(struct perf_event
*event
, int flags
)
348 struct ddr_pmu
*pmu
= to_ddr_pmu(event
->pmu
);
349 struct hw_perf_event
*hwc
= &event
->hw
;
350 int counter
= hwc
->idx
;
352 local64_set(&hwc
->prev_count
, 0);
354 ddr_perf_counter_enable(pmu
, event
->attr
.config
, counter
, true);
359 static int ddr_perf_event_add(struct perf_event
*event
, int flags
)
361 struct ddr_pmu
*pmu
= to_ddr_pmu(event
->pmu
);
362 struct hw_perf_event
*hwc
= &event
->hw
;
364 int cfg
= event
->attr
.config
;
365 int cfg1
= event
->attr
.config1
;
367 if (pmu
->devtype_data
->quirks
& DDR_CAP_AXI_ID_FILTER
) {
370 for (i
= 1; i
< NUM_COUNTERS
; i
++) {
371 if (pmu
->events
[i
] &&
372 !ddr_perf_filters_compatible(event
, pmu
->events
[i
]))
376 if (ddr_perf_is_filtered(event
)) {
377 /* revert axi id masking(axi_mask) value */
378 cfg1
^= AXI_MASKING_REVERT
;
379 writel(cfg1
, pmu
->base
+ COUNTER_DPCR1
);
383 counter
= ddr_perf_alloc_counter(pmu
, cfg
);
385 dev_dbg(pmu
->dev
, "There are not enough counters\n");
389 pmu
->events
[counter
] = event
;
390 pmu
->active_events
++;
393 hwc
->state
|= PERF_HES_STOPPED
;
395 if (flags
& PERF_EF_START
)
396 ddr_perf_event_start(event
, flags
);
401 static void ddr_perf_event_stop(struct perf_event
*event
, int flags
)
403 struct ddr_pmu
*pmu
= to_ddr_pmu(event
->pmu
);
404 struct hw_perf_event
*hwc
= &event
->hw
;
405 int counter
= hwc
->idx
;
407 ddr_perf_counter_enable(pmu
, event
->attr
.config
, counter
, false);
408 ddr_perf_event_update(event
);
410 hwc
->state
|= PERF_HES_STOPPED
;
413 static void ddr_perf_event_del(struct perf_event
*event
, int flags
)
415 struct ddr_pmu
*pmu
= to_ddr_pmu(event
->pmu
);
416 struct hw_perf_event
*hwc
= &event
->hw
;
417 int counter
= hwc
->idx
;
419 ddr_perf_event_stop(event
, PERF_EF_UPDATE
);
421 ddr_perf_free_counter(pmu
, counter
);
422 pmu
->active_events
--;
426 static void ddr_perf_pmu_enable(struct pmu
*pmu
)
428 struct ddr_pmu
*ddr_pmu
= to_ddr_pmu(pmu
);
430 /* enable cycle counter if cycle is not active event list */
431 if (ddr_pmu
->events
[EVENT_CYCLES_COUNTER
] == NULL
)
432 ddr_perf_counter_enable(ddr_pmu
,
434 EVENT_CYCLES_COUNTER
,
438 static void ddr_perf_pmu_disable(struct pmu
*pmu
)
440 struct ddr_pmu
*ddr_pmu
= to_ddr_pmu(pmu
);
442 if (ddr_pmu
->events
[EVENT_CYCLES_COUNTER
] == NULL
)
443 ddr_perf_counter_enable(ddr_pmu
,
445 EVENT_CYCLES_COUNTER
,
449 static int ddr_perf_init(struct ddr_pmu
*pmu
, void __iomem
*base
,
452 *pmu
= (struct ddr_pmu
) {
453 .pmu
= (struct pmu
) {
454 .module
= THIS_MODULE
,
455 .capabilities
= PERF_PMU_CAP_NO_EXCLUDE
,
456 .task_ctx_nr
= perf_invalid_context
,
457 .attr_groups
= attr_groups
,
458 .event_init
= ddr_perf_event_init
,
459 .add
= ddr_perf_event_add
,
460 .del
= ddr_perf_event_del
,
461 .start
= ddr_perf_event_start
,
462 .stop
= ddr_perf_event_stop
,
463 .read
= ddr_perf_event_update
,
464 .pmu_enable
= ddr_perf_pmu_enable
,
465 .pmu_disable
= ddr_perf_pmu_disable
,
471 pmu
->id
= ida_simple_get(&ddr_ida
, 0, 0, GFP_KERNEL
);
475 static irqreturn_t
ddr_perf_irq_handler(int irq
, void *p
)
478 struct ddr_pmu
*pmu
= (struct ddr_pmu
*) p
;
479 struct perf_event
*event
, *cycle_event
= NULL
;
481 /* all counter will stop if cycle counter disabled */
482 ddr_perf_counter_enable(pmu
,
484 EVENT_CYCLES_COUNTER
,
487 * When the cycle counter overflows, all counters are stopped,
488 * and an IRQ is raised. If any other counter overflows, it
489 * continues counting, and no IRQ is raised.
491 * Cycles occur at least 4 times as often as other events, so we
492 * can update all events on a cycle counter overflow and not
496 for (i
= 0; i
< NUM_COUNTERS
; i
++) {
501 event
= pmu
->events
[i
];
503 ddr_perf_event_update(event
);
505 if (event
->hw
.idx
== EVENT_CYCLES_COUNTER
)
509 ddr_perf_counter_enable(pmu
,
511 EVENT_CYCLES_COUNTER
,
514 ddr_perf_event_update(cycle_event
);
519 static int ddr_perf_offline_cpu(unsigned int cpu
, struct hlist_node
*node
)
521 struct ddr_pmu
*pmu
= hlist_entry_safe(node
, struct ddr_pmu
, node
);
527 target
= cpumask_any_but(cpu_online_mask
, cpu
);
528 if (target
>= nr_cpu_ids
)
531 perf_pmu_migrate_context(&pmu
->pmu
, cpu
, target
);
534 WARN_ON(irq_set_affinity_hint(pmu
->irq
, cpumask_of(pmu
->cpu
)));
539 static int ddr_perf_probe(struct platform_device
*pdev
)
542 struct device_node
*np
;
549 base
= devm_platform_ioremap_resource(pdev
, 0);
551 return PTR_ERR(base
);
553 np
= pdev
->dev
.of_node
;
555 pmu
= devm_kzalloc(&pdev
->dev
, sizeof(*pmu
), GFP_KERNEL
);
559 num
= ddr_perf_init(pmu
, base
, &pdev
->dev
);
561 platform_set_drvdata(pdev
, pmu
);
563 name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
, DDR_PERF_DEV_NAME
"%d",
568 pmu
->devtype_data
= of_device_get_match_data(&pdev
->dev
);
570 pmu
->cpu
= raw_smp_processor_id();
571 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
,
574 ddr_perf_offline_cpu
);
577 dev_err(&pdev
->dev
, "cpuhp_setup_state_multi failed\n");
578 goto cpuhp_state_err
;
581 pmu
->cpuhp_state
= ret
;
583 /* Register the pmu instance for cpu hotplug */
584 ret
= cpuhp_state_add_instance_nocalls(pmu
->cpuhp_state
, &pmu
->node
);
586 dev_err(&pdev
->dev
, "Error %d registering hotplug\n", ret
);
587 goto cpuhp_instance_err
;
591 irq
= of_irq_get(np
, 0);
593 dev_err(&pdev
->dev
, "Failed to get irq: %d", irq
);
598 ret
= devm_request_irq(&pdev
->dev
, irq
,
599 ddr_perf_irq_handler
,
600 IRQF_NOBALANCING
| IRQF_NO_THREAD
,
604 dev_err(&pdev
->dev
, "Request irq failed: %d", ret
);
609 ret
= irq_set_affinity_hint(pmu
->irq
, cpumask_of(pmu
->cpu
));
611 dev_err(pmu
->dev
, "Failed to set interrupt affinity!\n");
615 ret
= perf_pmu_register(&pmu
->pmu
, name
, -1);
622 cpuhp_state_remove_instance_nocalls(pmu
->cpuhp_state
, &pmu
->node
);
624 cpuhp_remove_multi_state(pmu
->cpuhp_state
);
626 ida_simple_remove(&ddr_ida
, pmu
->id
);
627 dev_warn(&pdev
->dev
, "i.MX8 DDR Perf PMU failed (%d), disabled\n", ret
);
631 static int ddr_perf_remove(struct platform_device
*pdev
)
633 struct ddr_pmu
*pmu
= platform_get_drvdata(pdev
);
635 cpuhp_state_remove_instance_nocalls(pmu
->cpuhp_state
, &pmu
->node
);
636 cpuhp_remove_multi_state(pmu
->cpuhp_state
);
637 irq_set_affinity_hint(pmu
->irq
, NULL
);
639 perf_pmu_unregister(&pmu
->pmu
);
641 ida_simple_remove(&ddr_ida
, pmu
->id
);
645 static struct platform_driver imx_ddr_pmu_driver
= {
647 .name
= "imx-ddr-pmu",
648 .of_match_table
= imx_ddr_pmu_dt_ids
,
649 .suppress_bind_attrs
= true,
651 .probe
= ddr_perf_probe
,
652 .remove
= ddr_perf_remove
,
655 module_platform_driver(imx_ddr_pmu_driver
);
656 MODULE_LICENSE("GPL v2");