3 * Copyright 2011,2016 Freescale Semiconductor, Inc.
4 * Copyright 2011 Linaro Ltd.
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
14 #include <linux/hrtimer.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_device.h>
22 #include <linux/perf_event.h>
23 #include <linux/slab.h>
27 #define MMDC_MAPSR 0x404
28 #define BP_MMDC_MAPSR_PSD 0
29 #define BP_MMDC_MAPSR_PSS 4
31 #define MMDC_MDMISC 0x18
32 #define BM_MMDC_MDMISC_DDR_TYPE 0x18
33 #define BP_MMDC_MDMISC_DDR_TYPE 0x3
35 #define TOTAL_CYCLES 0x0
36 #define BUSY_CYCLES 0x1
37 #define READ_ACCESSES 0x2
38 #define WRITE_ACCESSES 0x3
39 #define READ_BYTES 0x4
40 #define WRITE_BYTES 0x5
42 /* Enables, resets, freezes, overflow profiling*/
48 #define PROFILE_SEL 0x10
50 #define MMDC_MADPCR0 0x410
51 #define MMDC_MADPCR1 0x414
52 #define MMDC_MADPSR0 0x418
53 #define MMDC_MADPSR1 0x41C
54 #define MMDC_MADPSR2 0x420
55 #define MMDC_MADPSR3 0x424
56 #define MMDC_MADPSR4 0x428
57 #define MMDC_MADPSR5 0x42C
59 #define MMDC_NUM_COUNTERS 6
61 #define MMDC_FLAG_PROFILE_SEL 0x1
62 #define MMDC_PRF_AXI_ID_CLEAR 0x0
64 #define to_mmdc_pmu(p) container_of(p, struct mmdc_pmu, pmu)
68 struct fsl_mmdc_devtype_data
{
72 static const struct fsl_mmdc_devtype_data imx6q_data
= {
75 static const struct fsl_mmdc_devtype_data imx6qp_data
= {
76 .flags
= MMDC_FLAG_PROFILE_SEL
,
79 static const struct of_device_id imx_mmdc_dt_ids
[] = {
80 { .compatible
= "fsl,imx6q-mmdc", .data
= (void *)&imx6q_data
},
81 { .compatible
= "fsl,imx6qp-mmdc", .data
= (void *)&imx6qp_data
},
85 #ifdef CONFIG_PERF_EVENTS
87 static enum cpuhp_state cpuhp_mmdc_state
;
88 static DEFINE_IDA(mmdc_ida
);
90 PMU_EVENT_ATTR_STRING(total
-cycles
, mmdc_pmu_total_cycles
, "event=0x00")
91 PMU_EVENT_ATTR_STRING(busy
-cycles
, mmdc_pmu_busy_cycles
, "event=0x01")
92 PMU_EVENT_ATTR_STRING(read
-accesses
, mmdc_pmu_read_accesses
, "event=0x02")
93 PMU_EVENT_ATTR_STRING(write
-accesses
, mmdc_pmu_write_accesses
, "event=0x03")
94 PMU_EVENT_ATTR_STRING(read
-bytes
, mmdc_pmu_read_bytes
, "event=0x04")
95 PMU_EVENT_ATTR_STRING(read
-bytes
.unit
, mmdc_pmu_read_bytes_unit
, "MB");
96 PMU_EVENT_ATTR_STRING(read
-bytes
.scale
, mmdc_pmu_read_bytes_scale
, "0.000001");
97 PMU_EVENT_ATTR_STRING(write
-bytes
, mmdc_pmu_write_bytes
, "event=0x05")
98 PMU_EVENT_ATTR_STRING(write
-bytes
.unit
, mmdc_pmu_write_bytes_unit
, "MB");
99 PMU_EVENT_ATTR_STRING(write
-bytes
.scale
, mmdc_pmu_write_bytes_scale
, "0.000001");
103 void __iomem
*mmdc_base
;
105 struct hrtimer hrtimer
;
106 unsigned int active_events
;
108 struct perf_event
*mmdc_events
[MMDC_NUM_COUNTERS
];
109 struct hlist_node node
;
110 struct fsl_mmdc_devtype_data
*devtype_data
;
114 * Polling period is set to one second, overflow of total-cycles (the fastest
115 * increasing counter) takes ten seconds so one second is safe
117 static unsigned int mmdc_pmu_poll_period_us
= 1000000;
119 module_param_named(pmu_pmu_poll_period_us
, mmdc_pmu_poll_period_us
, uint
,
122 static ktime_t
mmdc_pmu_timer_period(void)
124 return ns_to_ktime((u64
)mmdc_pmu_poll_period_us
* 1000);
127 static ssize_t
mmdc_pmu_cpumask_show(struct device
*dev
,
128 struct device_attribute
*attr
, char *buf
)
130 struct mmdc_pmu
*pmu_mmdc
= dev_get_drvdata(dev
);
132 return cpumap_print_to_pagebuf(true, buf
, &pmu_mmdc
->cpu
);
135 static struct device_attribute mmdc_pmu_cpumask_attr
=
136 __ATTR(cpumask
, S_IRUGO
, mmdc_pmu_cpumask_show
, NULL
);
138 static struct attribute
*mmdc_pmu_cpumask_attrs
[] = {
139 &mmdc_pmu_cpumask_attr
.attr
,
143 static struct attribute_group mmdc_pmu_cpumask_attr_group
= {
144 .attrs
= mmdc_pmu_cpumask_attrs
,
147 static struct attribute
*mmdc_pmu_events_attrs
[] = {
148 &mmdc_pmu_total_cycles
.attr
.attr
,
149 &mmdc_pmu_busy_cycles
.attr
.attr
,
150 &mmdc_pmu_read_accesses
.attr
.attr
,
151 &mmdc_pmu_write_accesses
.attr
.attr
,
152 &mmdc_pmu_read_bytes
.attr
.attr
,
153 &mmdc_pmu_read_bytes_unit
.attr
.attr
,
154 &mmdc_pmu_read_bytes_scale
.attr
.attr
,
155 &mmdc_pmu_write_bytes
.attr
.attr
,
156 &mmdc_pmu_write_bytes_unit
.attr
.attr
,
157 &mmdc_pmu_write_bytes_scale
.attr
.attr
,
161 static struct attribute_group mmdc_pmu_events_attr_group
= {
163 .attrs
= mmdc_pmu_events_attrs
,
166 PMU_FORMAT_ATTR(event
, "config:0-63");
167 PMU_FORMAT_ATTR(axi_id
, "config1:0-63");
169 static struct attribute
*mmdc_pmu_format_attrs
[] = {
170 &format_attr_event
.attr
,
171 &format_attr_axi_id
.attr
,
175 static struct attribute_group mmdc_pmu_format_attr_group
= {
177 .attrs
= mmdc_pmu_format_attrs
,
180 static const struct attribute_group
*attr_groups
[] = {
181 &mmdc_pmu_events_attr_group
,
182 &mmdc_pmu_format_attr_group
,
183 &mmdc_pmu_cpumask_attr_group
,
187 static u32
mmdc_pmu_read_counter(struct mmdc_pmu
*pmu_mmdc
, int cfg
)
189 void __iomem
*mmdc_base
, *reg
;
191 mmdc_base
= pmu_mmdc
->mmdc_base
;
195 reg
= mmdc_base
+ MMDC_MADPSR0
;
198 reg
= mmdc_base
+ MMDC_MADPSR1
;
201 reg
= mmdc_base
+ MMDC_MADPSR2
;
204 reg
= mmdc_base
+ MMDC_MADPSR3
;
207 reg
= mmdc_base
+ MMDC_MADPSR4
;
210 reg
= mmdc_base
+ MMDC_MADPSR5
;
214 "invalid configuration %d for mmdc counter", cfg
);
219 static int mmdc_pmu_offline_cpu(unsigned int cpu
, struct hlist_node
*node
)
221 struct mmdc_pmu
*pmu_mmdc
= hlist_entry_safe(node
, struct mmdc_pmu
, node
);
224 if (!cpumask_test_and_clear_cpu(cpu
, &pmu_mmdc
->cpu
))
227 target
= cpumask_any_but(cpu_online_mask
, cpu
);
228 if (target
>= nr_cpu_ids
)
231 perf_pmu_migrate_context(&pmu_mmdc
->pmu
, cpu
, target
);
232 cpumask_set_cpu(target
, &pmu_mmdc
->cpu
);
237 static bool mmdc_pmu_group_event_is_valid(struct perf_event
*event
,
239 unsigned long *used_counters
)
241 int cfg
= event
->attr
.config
;
243 if (is_software_event(event
))
246 if (event
->pmu
!= pmu
)
249 return !test_and_set_bit(cfg
, used_counters
);
253 * Each event has a single fixed-purpose counter, so we can only have a
254 * single active event for each at any point in time. Here we just check
255 * for duplicates, and rely on mmdc_pmu_event_init to verify that the HW
256 * event numbers are valid.
258 static bool mmdc_pmu_group_is_valid(struct perf_event
*event
)
260 struct pmu
*pmu
= event
->pmu
;
261 struct perf_event
*leader
= event
->group_leader
;
262 struct perf_event
*sibling
;
263 unsigned long counter_mask
= 0;
265 set_bit(leader
->attr
.config
, &counter_mask
);
267 if (event
!= leader
) {
268 if (!mmdc_pmu_group_event_is_valid(event
, pmu
, &counter_mask
))
272 list_for_each_entry(sibling
, &leader
->sibling_list
, group_entry
) {
273 if (!mmdc_pmu_group_event_is_valid(sibling
, pmu
, &counter_mask
))
280 static int mmdc_pmu_event_init(struct perf_event
*event
)
282 struct mmdc_pmu
*pmu_mmdc
= to_mmdc_pmu(event
->pmu
);
283 int cfg
= event
->attr
.config
;
285 if (event
->attr
.type
!= event
->pmu
->type
)
288 if (is_sampling_event(event
) || event
->attach_state
& PERF_ATTACH_TASK
)
291 if (event
->cpu
< 0) {
292 dev_warn(pmu_mmdc
->dev
, "Can't provide per-task data!\n");
296 if (event
->attr
.exclude_user
||
297 event
->attr
.exclude_kernel
||
298 event
->attr
.exclude_hv
||
299 event
->attr
.exclude_idle
||
300 event
->attr
.exclude_host
||
301 event
->attr
.exclude_guest
||
302 event
->attr
.sample_period
)
305 if (cfg
< 0 || cfg
>= MMDC_NUM_COUNTERS
)
308 if (!mmdc_pmu_group_is_valid(event
))
311 event
->cpu
= cpumask_first(&pmu_mmdc
->cpu
);
315 static void mmdc_pmu_event_update(struct perf_event
*event
)
317 struct mmdc_pmu
*pmu_mmdc
= to_mmdc_pmu(event
->pmu
);
318 struct hw_perf_event
*hwc
= &event
->hw
;
319 u64 delta
, prev_raw_count
, new_raw_count
;
322 prev_raw_count
= local64_read(&hwc
->prev_count
);
323 new_raw_count
= mmdc_pmu_read_counter(pmu_mmdc
,
325 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
326 new_raw_count
) != prev_raw_count
);
328 delta
= (new_raw_count
- prev_raw_count
) & 0xFFFFFFFF;
330 local64_add(delta
, &event
->count
);
333 static void mmdc_pmu_event_start(struct perf_event
*event
, int flags
)
335 struct mmdc_pmu
*pmu_mmdc
= to_mmdc_pmu(event
->pmu
);
336 struct hw_perf_event
*hwc
= &event
->hw
;
337 void __iomem
*mmdc_base
, *reg
;
340 mmdc_base
= pmu_mmdc
->mmdc_base
;
341 reg
= mmdc_base
+ MMDC_MADPCR0
;
344 * hrtimer is required because mmdc does not provide an interrupt so
345 * polling is necessary
347 hrtimer_start(&pmu_mmdc
->hrtimer
, mmdc_pmu_timer_period(),
348 HRTIMER_MODE_REL_PINNED
);
350 local64_set(&hwc
->prev_count
, 0);
352 writel(DBG_RST
, reg
);
355 * Write the AXI id parameter to MADPCR1.
357 val
= event
->attr
.config1
;
358 reg
= mmdc_base
+ MMDC_MADPCR1
;
361 reg
= mmdc_base
+ MMDC_MADPCR0
;
363 if (pmu_mmdc
->devtype_data
->flags
& MMDC_FLAG_PROFILE_SEL
)
369 static int mmdc_pmu_event_add(struct perf_event
*event
, int flags
)
371 struct mmdc_pmu
*pmu_mmdc
= to_mmdc_pmu(event
->pmu
);
372 struct hw_perf_event
*hwc
= &event
->hw
;
374 int cfg
= event
->attr
.config
;
376 if (flags
& PERF_EF_START
)
377 mmdc_pmu_event_start(event
, flags
);
379 if (pmu_mmdc
->mmdc_events
[cfg
] != NULL
)
382 pmu_mmdc
->mmdc_events
[cfg
] = event
;
383 pmu_mmdc
->active_events
++;
385 local64_set(&hwc
->prev_count
, mmdc_pmu_read_counter(pmu_mmdc
, cfg
));
390 static void mmdc_pmu_event_stop(struct perf_event
*event
, int flags
)
392 struct mmdc_pmu
*pmu_mmdc
= to_mmdc_pmu(event
->pmu
);
393 void __iomem
*mmdc_base
, *reg
;
395 mmdc_base
= pmu_mmdc
->mmdc_base
;
396 reg
= mmdc_base
+ MMDC_MADPCR0
;
398 writel(PRF_FRZ
, reg
);
400 reg
= mmdc_base
+ MMDC_MADPCR1
;
401 writel(MMDC_PRF_AXI_ID_CLEAR
, reg
);
403 mmdc_pmu_event_update(event
);
406 static void mmdc_pmu_event_del(struct perf_event
*event
, int flags
)
408 struct mmdc_pmu
*pmu_mmdc
= to_mmdc_pmu(event
->pmu
);
409 int cfg
= event
->attr
.config
;
411 pmu_mmdc
->mmdc_events
[cfg
] = NULL
;
412 pmu_mmdc
->active_events
--;
414 if (pmu_mmdc
->active_events
== 0)
415 hrtimer_cancel(&pmu_mmdc
->hrtimer
);
417 mmdc_pmu_event_stop(event
, PERF_EF_UPDATE
);
420 static void mmdc_pmu_overflow_handler(struct mmdc_pmu
*pmu_mmdc
)
424 for (i
= 0; i
< MMDC_NUM_COUNTERS
; i
++) {
425 struct perf_event
*event
= pmu_mmdc
->mmdc_events
[i
];
428 mmdc_pmu_event_update(event
);
432 static enum hrtimer_restart
mmdc_pmu_timer_handler(struct hrtimer
*hrtimer
)
434 struct mmdc_pmu
*pmu_mmdc
= container_of(hrtimer
, struct mmdc_pmu
,
437 mmdc_pmu_overflow_handler(pmu_mmdc
);
438 hrtimer_forward_now(hrtimer
, mmdc_pmu_timer_period());
440 return HRTIMER_RESTART
;
443 static int mmdc_pmu_init(struct mmdc_pmu
*pmu_mmdc
,
444 void __iomem
*mmdc_base
, struct device
*dev
)
448 *pmu_mmdc
= (struct mmdc_pmu
) {
449 .pmu
= (struct pmu
) {
450 .task_ctx_nr
= perf_invalid_context
,
451 .attr_groups
= attr_groups
,
452 .event_init
= mmdc_pmu_event_init
,
453 .add
= mmdc_pmu_event_add
,
454 .del
= mmdc_pmu_event_del
,
455 .start
= mmdc_pmu_event_start
,
456 .stop
= mmdc_pmu_event_stop
,
457 .read
= mmdc_pmu_event_update
,
459 .mmdc_base
= mmdc_base
,
464 mmdc_num
= ida_simple_get(&mmdc_ida
, 0, 0, GFP_KERNEL
);
469 static int imx_mmdc_remove(struct platform_device
*pdev
)
471 struct mmdc_pmu
*pmu_mmdc
= platform_get_drvdata(pdev
);
473 cpuhp_state_remove_instance_nocalls(cpuhp_mmdc_state
, &pmu_mmdc
->node
);
474 perf_pmu_unregister(&pmu_mmdc
->pmu
);
479 static int imx_mmdc_perf_init(struct platform_device
*pdev
, void __iomem
*mmdc_base
)
481 struct mmdc_pmu
*pmu_mmdc
;
485 const struct of_device_id
*of_id
=
486 of_match_device(imx_mmdc_dt_ids
, &pdev
->dev
);
488 pmu_mmdc
= kzalloc(sizeof(*pmu_mmdc
), GFP_KERNEL
);
490 pr_err("failed to allocate PMU device!\n");
494 /* The first instance registers the hotplug state */
495 if (!cpuhp_mmdc_state
) {
496 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
,
497 "perf/arm/mmdc:online", NULL
,
498 mmdc_pmu_offline_cpu
);
500 pr_err("cpuhp_setup_state_multi failed\n");
503 cpuhp_mmdc_state
= ret
;
506 mmdc_num
= mmdc_pmu_init(pmu_mmdc
, mmdc_base
, &pdev
->dev
);
510 name
= devm_kasprintf(&pdev
->dev
,
511 GFP_KERNEL
, "mmdc%d", mmdc_num
);
513 pmu_mmdc
->devtype_data
= (struct fsl_mmdc_devtype_data
*)of_id
->data
;
515 hrtimer_init(&pmu_mmdc
->hrtimer
, CLOCK_MONOTONIC
,
517 pmu_mmdc
->hrtimer
.function
= mmdc_pmu_timer_handler
;
519 cpumask_set_cpu(raw_smp_processor_id(), &pmu_mmdc
->cpu
);
521 /* Register the pmu instance for cpu hotplug */
522 cpuhp_state_add_instance_nocalls(cpuhp_mmdc_state
, &pmu_mmdc
->node
);
524 ret
= perf_pmu_register(&(pmu_mmdc
->pmu
), name
, -1);
526 goto pmu_register_err
;
528 platform_set_drvdata(pdev
, pmu_mmdc
);
532 pr_warn("MMDC Perf PMU failed (%d), disabled\n", ret
);
533 cpuhp_state_remove_instance_nocalls(cpuhp_mmdc_state
, &pmu_mmdc
->node
);
534 hrtimer_cancel(&pmu_mmdc
->hrtimer
);
541 #define imx_mmdc_remove NULL
542 #define imx_mmdc_perf_init(pdev, mmdc_base) 0
545 static int imx_mmdc_probe(struct platform_device
*pdev
)
547 struct device_node
*np
= pdev
->dev
.of_node
;
548 void __iomem
*mmdc_base
, *reg
;
551 mmdc_base
= of_iomap(np
, 0);
554 reg
= mmdc_base
+ MMDC_MDMISC
;
556 val
= readl_relaxed(reg
);
557 ddr_type
= (val
& BM_MMDC_MDMISC_DDR_TYPE
) >>
558 BP_MMDC_MDMISC_DDR_TYPE
;
560 reg
= mmdc_base
+ MMDC_MAPSR
;
562 /* Enable automatic power saving */
563 val
= readl_relaxed(reg
);
564 val
&= ~(1 << BP_MMDC_MAPSR_PSD
);
565 writel_relaxed(val
, reg
);
567 return imx_mmdc_perf_init(pdev
, mmdc_base
);
570 int imx_mmdc_get_ddr_type(void)
575 static struct platform_driver imx_mmdc_driver
= {
578 .of_match_table
= imx_mmdc_dt_ids
,
580 .probe
= imx_mmdc_probe
,
581 .remove
= imx_mmdc_remove
,
584 static int __init
imx_mmdc_init(void)
586 return platform_driver_register(&imx_mmdc_driver
);
588 postcore_initcall(imx_mmdc_init
);