2 * exynos_ppmu.c - EXYNOS PPMU (Platform Performance Monitoring Unit) support
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
5 * Author : Chanwoo Choi <cw00.choi@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This driver is based on drivers/devfreq/exynos/exynos_ppmu.c
14 #include <linux/clk.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/suspend.h>
22 #include <linux/devfreq-event.h>
24 #include "exynos-ppmu.h"
26 struct exynos_ppmu_data
{
31 struct devfreq_event_dev
**edev
;
32 struct devfreq_event_desc
*desc
;
33 unsigned int num_events
;
36 struct regmap
*regmap
;
38 struct exynos_ppmu_data ppmu
;
41 #define PPMU_EVENT(name) \
42 { "ppmu-event0-"#name, PPMU_PMNCNT0 }, \
43 { "ppmu-event1-"#name, PPMU_PMNCNT1 }, \
44 { "ppmu-event2-"#name, PPMU_PMNCNT2 }, \
45 { "ppmu-event3-"#name, PPMU_PMNCNT3 }
47 static struct __exynos_ppmu_events
{
51 /* For Exynos3250, Exynos4 and Exynos5260 */
55 /* For Exynos4 SoCs and Exynos3250 */
64 /* Only for Exynos3250 and Exynos5260 */
67 /* Only for Exynos4 SoCs */
69 PPMU_EVENT(mfc
-right
),
71 /* Only for Exynos5260 SoCs */
85 /* Only for Exynos5433 SoCs */
87 PPMU_EVENT(d0
-general
),
90 PPMU_EVENT(d1
-general
),
94 static int exynos_ppmu_find_ppmu_id(struct devfreq_event_dev
*edev
)
98 for (i
= 0; i
< ARRAY_SIZE(ppmu_events
); i
++)
99 if (!strcmp(edev
->desc
->name
, ppmu_events
[i
].name
))
100 return ppmu_events
[i
].id
;
106 * The devfreq-event ops structure for PPMU v1.1
108 static int exynos_ppmu_disable(struct devfreq_event_dev
*edev
)
110 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
114 /* Disable all counters */
115 ret
= regmap_write(info
->regmap
, PPMU_CNTENC
,
125 ret
= regmap_read(info
->regmap
, PPMU_PMNC
, &pmnc
);
129 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
130 ret
= regmap_write(info
->regmap
, PPMU_PMNC
, pmnc
);
137 static int exynos_ppmu_set_event(struct devfreq_event_dev
*edev
)
139 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
140 int id
= exynos_ppmu_find_ppmu_id(edev
);
147 /* Enable specific counter */
148 ret
= regmap_read(info
->regmap
, PPMU_CNTENS
, &cntens
);
152 cntens
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
153 ret
= regmap_write(info
->regmap
, PPMU_CNTENS
, cntens
);
157 /* Set the event of Read/Write data count */
158 ret
= regmap_write(info
->regmap
, PPMU_BEVTxSEL(id
),
159 PPMU_RO_DATA_CNT
| PPMU_WO_DATA_CNT
);
163 /* Reset cycle counter/performance counter and enable PPMU */
164 ret
= regmap_read(info
->regmap
, PPMU_PMNC
, &pmnc
);
168 pmnc
&= ~(PPMU_PMNC_ENABLE_MASK
169 | PPMU_PMNC_COUNTER_RESET_MASK
170 | PPMU_PMNC_CC_RESET_MASK
);
171 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_ENABLE_SHIFT
);
172 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_COUNTER_RESET_SHIFT
);
173 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_CC_RESET_SHIFT
);
174 ret
= regmap_write(info
->regmap
, PPMU_PMNC
, pmnc
);
181 static int exynos_ppmu_get_event(struct devfreq_event_dev
*edev
,
182 struct devfreq_event_data
*edata
)
184 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
185 int id
= exynos_ppmu_find_ppmu_id(edev
);
186 unsigned int total_count
, load_count
;
187 unsigned int pmcnt3_high
, pmcnt3_low
;
188 unsigned int pmnc
, cntenc
;
195 ret
= regmap_read(info
->regmap
, PPMU_PMNC
, &pmnc
);
199 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
200 ret
= regmap_write(info
->regmap
, PPMU_PMNC
, pmnc
);
204 /* Read cycle count */
205 ret
= regmap_read(info
->regmap
, PPMU_CCNT
, &total_count
);
208 edata
->total_count
= total_count
;
210 /* Read performance count */
215 ret
= regmap_read(info
->regmap
, PPMU_PMNCT(id
), &load_count
);
218 edata
->load_count
= load_count
;
221 ret
= regmap_read(info
->regmap
, PPMU_PMCNT3_HIGH
, &pmcnt3_high
);
225 ret
= regmap_read(info
->regmap
, PPMU_PMCNT3_LOW
, &pmcnt3_low
);
229 edata
->load_count
= ((pmcnt3_high
<< 8) | pmcnt3_low
);
235 /* Disable specific counter */
236 ret
= regmap_read(info
->regmap
, PPMU_CNTENC
, &cntenc
);
240 cntenc
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
241 ret
= regmap_write(info
->regmap
, PPMU_CNTENC
, cntenc
);
245 dev_dbg(&edev
->dev
, "%s (event: %ld/%ld)\n", edev
->desc
->name
,
246 edata
->load_count
, edata
->total_count
);
251 static const struct devfreq_event_ops exynos_ppmu_ops
= {
252 .disable
= exynos_ppmu_disable
,
253 .set_event
= exynos_ppmu_set_event
,
254 .get_event
= exynos_ppmu_get_event
,
258 * The devfreq-event ops structure for PPMU v2.0
260 static int exynos_ppmu_v2_disable(struct devfreq_event_dev
*edev
)
262 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
266 /* Disable all counters */
267 clear
= (PPMU_CCNT_MASK
| PPMU_PMCNT0_MASK
| PPMU_PMCNT1_MASK
268 | PPMU_PMCNT2_MASK
| PPMU_PMCNT3_MASK
);
269 ret
= regmap_write(info
->regmap
, PPMU_V2_FLAG
, clear
);
273 ret
= regmap_write(info
->regmap
, PPMU_V2_INTENC
, clear
);
277 ret
= regmap_write(info
->regmap
, PPMU_V2_CNTENC
, clear
);
281 ret
= regmap_write(info
->regmap
, PPMU_V2_CNT_RESET
, clear
);
285 ret
= regmap_write(info
->regmap
, PPMU_V2_CIG_CFG0
, 0x0);
289 ret
= regmap_write(info
->regmap
, PPMU_V2_CIG_CFG1
, 0x0);
293 ret
= regmap_write(info
->regmap
, PPMU_V2_CIG_CFG2
, 0x0);
297 ret
= regmap_write(info
->regmap
, PPMU_V2_CIG_RESULT
, 0x0);
301 ret
= regmap_write(info
->regmap
, PPMU_V2_CNT_AUTO
, 0x0);
305 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EV0_TYPE
, 0x0);
309 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EV1_TYPE
, 0x0);
313 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EV2_TYPE
, 0x0);
317 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EV3_TYPE
, 0x0);
321 ret
= regmap_write(info
->regmap
, PPMU_V2_SM_ID_V
, 0x0);
325 ret
= regmap_write(info
->regmap
, PPMU_V2_SM_ID_A
, 0x0);
329 ret
= regmap_write(info
->regmap
, PPMU_V2_SM_OTHERS_V
, 0x0);
333 ret
= regmap_write(info
->regmap
, PPMU_V2_SM_OTHERS_A
, 0x0);
337 ret
= regmap_write(info
->regmap
, PPMU_V2_INTERRUPT_RESET
, 0x0);
342 ret
= regmap_read(info
->regmap
, PPMU_V2_PMNC
, &pmnc
);
346 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
347 ret
= regmap_write(info
->regmap
, PPMU_V2_PMNC
, pmnc
);
354 static int exynos_ppmu_v2_set_event(struct devfreq_event_dev
*edev
)
356 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
357 unsigned int pmnc
, cntens
;
358 int id
= exynos_ppmu_find_ppmu_id(edev
);
361 /* Enable all counters */
362 ret
= regmap_read(info
->regmap
, PPMU_V2_CNTENS
, &cntens
);
366 cntens
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
367 ret
= regmap_write(info
->regmap
, PPMU_V2_CNTENS
, cntens
);
371 /* Set the event of Read/Write data count */
376 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EVx_TYPE(id
),
377 PPMU_V2_RO_DATA_CNT
| PPMU_V2_WO_DATA_CNT
);
382 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EVx_TYPE(id
),
383 PPMU_V2_EVT3_RW_DATA_CNT
);
389 /* Reset cycle counter/performance counter and enable PPMU */
390 ret
= regmap_read(info
->regmap
, PPMU_V2_PMNC
, &pmnc
);
394 pmnc
&= ~(PPMU_PMNC_ENABLE_MASK
395 | PPMU_PMNC_COUNTER_RESET_MASK
396 | PPMU_PMNC_CC_RESET_MASK
397 | PPMU_PMNC_CC_DIVIDER_MASK
398 | PPMU_V2_PMNC_START_MODE_MASK
);
399 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_ENABLE_SHIFT
);
400 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_COUNTER_RESET_SHIFT
);
401 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_CC_RESET_SHIFT
);
402 pmnc
|= (PPMU_V2_MODE_MANUAL
<< PPMU_V2_PMNC_START_MODE_SHIFT
);
404 ret
= regmap_write(info
->regmap
, PPMU_V2_PMNC
, pmnc
);
411 static int exynos_ppmu_v2_get_event(struct devfreq_event_dev
*edev
,
412 struct devfreq_event_data
*edata
)
414 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
415 int id
= exynos_ppmu_find_ppmu_id(edev
);
417 unsigned int pmnc
, cntenc
;
418 unsigned int pmcnt_high
, pmcnt_low
;
419 unsigned int total_count
, count
;
420 unsigned long load_count
= 0;
423 ret
= regmap_read(info
->regmap
, PPMU_V2_PMNC
, &pmnc
);
427 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
428 ret
= regmap_write(info
->regmap
, PPMU_V2_PMNC
, pmnc
);
432 /* Read cycle count and performance count */
433 ret
= regmap_read(info
->regmap
, PPMU_V2_CCNT
, &total_count
);
436 edata
->total_count
= total_count
;
442 ret
= regmap_read(info
->regmap
, PPMU_V2_PMNCT(id
), &count
);
448 ret
= regmap_read(info
->regmap
, PPMU_V2_PMCNT3_HIGH
,
453 ret
= regmap_read(info
->regmap
, PPMU_V2_PMCNT3_LOW
, &pmcnt_low
);
457 load_count
= ((u64
)((pmcnt_high
& 0xff)) << 32)+ (u64
)pmcnt_low
;
460 edata
->load_count
= load_count
;
462 /* Disable all counters */
463 ret
= regmap_read(info
->regmap
, PPMU_V2_CNTENC
, &cntenc
);
467 cntenc
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
468 ret
= regmap_write(info
->regmap
, PPMU_V2_CNTENC
, cntenc
);
472 dev_dbg(&edev
->dev
, "%25s (load: %ld / %ld)\n", edev
->desc
->name
,
473 edata
->load_count
, edata
->total_count
);
477 static const struct devfreq_event_ops exynos_ppmu_v2_ops
= {
478 .disable
= exynos_ppmu_v2_disable
,
479 .set_event
= exynos_ppmu_v2_set_event
,
480 .get_event
= exynos_ppmu_v2_get_event
,
483 static const struct of_device_id exynos_ppmu_id_match
[] = {
485 .compatible
= "samsung,exynos-ppmu",
486 .data
= (void *)&exynos_ppmu_ops
,
488 .compatible
= "samsung,exynos-ppmu-v2",
489 .data
= (void *)&exynos_ppmu_v2_ops
,
493 MODULE_DEVICE_TABLE(of
, exynos_ppmu_id_match
);
495 static struct devfreq_event_ops
*exynos_bus_get_ops(struct device_node
*np
)
497 const struct of_device_id
*match
;
499 match
= of_match_node(exynos_ppmu_id_match
, np
);
500 return (struct devfreq_event_ops
*)match
->data
;
503 static int of_get_devfreq_events(struct device_node
*np
,
504 struct exynos_ppmu
*info
)
506 struct devfreq_event_desc
*desc
;
507 struct devfreq_event_ops
*event_ops
;
508 struct device
*dev
= info
->dev
;
509 struct device_node
*events_np
, *node
;
512 events_np
= of_get_child_by_name(np
, "events");
515 "failed to get child node of devfreq-event devices\n");
518 event_ops
= exynos_bus_get_ops(np
);
520 count
= of_get_child_count(events_np
);
521 desc
= devm_kcalloc(dev
, count
, sizeof(*desc
), GFP_KERNEL
);
524 info
->num_events
= count
;
527 for_each_child_of_node(events_np
, node
) {
528 for (i
= 0; i
< ARRAY_SIZE(ppmu_events
); i
++) {
529 if (!ppmu_events
[i
].name
)
532 if (!of_node_cmp(node
->name
, ppmu_events
[i
].name
))
536 if (i
== ARRAY_SIZE(ppmu_events
)) {
538 "don't know how to configure events : %s\n",
543 desc
[j
].ops
= event_ops
;
544 desc
[j
].driver_data
= info
;
546 of_property_read_string(node
, "event-name", &desc
[j
].name
);
552 of_node_put(events_np
);
557 static struct regmap_config exynos_ppmu_regmap_config
= {
563 static int exynos_ppmu_parse_dt(struct platform_device
*pdev
,
564 struct exynos_ppmu
*info
)
566 struct device
*dev
= info
->dev
;
567 struct device_node
*np
= dev
->of_node
;
568 struct resource
*res
;
573 dev_err(dev
, "failed to find devicetree node\n");
577 /* Maps the memory mapped IO to control PPMU register */
578 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
579 base
= devm_ioremap_resource(dev
, res
);
581 return PTR_ERR(base
);
583 exynos_ppmu_regmap_config
.max_register
= resource_size(res
) - 4;
584 info
->regmap
= devm_regmap_init_mmio(dev
, base
,
585 &exynos_ppmu_regmap_config
);
586 if (IS_ERR(info
->regmap
)) {
587 dev_err(dev
, "failed to initialize regmap\n");
588 return PTR_ERR(info
->regmap
);
591 info
->ppmu
.clk
= devm_clk_get(dev
, "ppmu");
592 if (IS_ERR(info
->ppmu
.clk
)) {
593 info
->ppmu
.clk
= NULL
;
594 dev_warn(dev
, "cannot get PPMU clock\n");
597 ret
= of_get_devfreq_events(np
, info
);
599 dev_err(dev
, "failed to parse exynos ppmu dt node\n");
606 static int exynos_ppmu_probe(struct platform_device
*pdev
)
608 struct exynos_ppmu
*info
;
609 struct devfreq_event_dev
**edev
;
610 struct devfreq_event_desc
*desc
;
611 int i
, ret
= 0, size
;
613 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
617 info
->dev
= &pdev
->dev
;
619 /* Parse dt data to get resource */
620 ret
= exynos_ppmu_parse_dt(pdev
, info
);
623 "failed to parse devicetree for resource\n");
628 size
= sizeof(struct devfreq_event_dev
*) * info
->num_events
;
629 info
->edev
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
634 platform_set_drvdata(pdev
, info
);
636 for (i
= 0; i
< info
->num_events
; i
++) {
637 edev
[i
] = devm_devfreq_event_add_edev(&pdev
->dev
, &desc
[i
]);
638 if (IS_ERR(edev
[i
])) {
639 ret
= PTR_ERR(edev
[i
]);
641 "failed to add devfreq-event device\n");
642 return PTR_ERR(edev
[i
]);
645 pr_info("exynos-ppmu: new PPMU device registered %s (%s)\n",
646 dev_name(&pdev
->dev
), desc
[i
].name
);
649 ret
= clk_prepare_enable(info
->ppmu
.clk
);
651 dev_err(&pdev
->dev
, "failed to prepare ppmu clock\n");
658 static int exynos_ppmu_remove(struct platform_device
*pdev
)
660 struct exynos_ppmu
*info
= platform_get_drvdata(pdev
);
662 clk_disable_unprepare(info
->ppmu
.clk
);
667 static struct platform_driver exynos_ppmu_driver
= {
668 .probe
= exynos_ppmu_probe
,
669 .remove
= exynos_ppmu_remove
,
671 .name
= "exynos-ppmu",
672 .of_match_table
= exynos_ppmu_id_match
,
675 module_platform_driver(exynos_ppmu_driver
);
677 MODULE_DESCRIPTION("Exynos PPMU(Platform Performance Monitoring Unit) driver");
678 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
679 MODULE_LICENSE("GPL");