1 // SPDX-License-Identifier: GPL-2.0-only
3 * exynos_ppmu.c - Exynos PPMU (Platform Performance Monitoring Unit) support
5 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
6 * Author : Chanwoo Choi <cw00.choi@samsung.com>
8 * This driver is based on drivers/devfreq/exynos/exynos_ppmu.c
11 #include <linux/clk.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/regmap.h>
19 #include <linux/suspend.h>
20 #include <linux/devfreq-event.h>
22 #include "exynos-ppmu.h"
24 enum exynos_ppmu_type
{
29 struct exynos_ppmu_data
{
34 struct devfreq_event_dev
**edev
;
35 struct devfreq_event_desc
*desc
;
36 unsigned int num_events
;
39 struct regmap
*regmap
;
41 struct exynos_ppmu_data ppmu
;
42 enum exynos_ppmu_type ppmu_type
;
45 #define PPMU_EVENT(name) \
46 { "ppmu-event0-"#name, PPMU_PMNCNT0 }, \
47 { "ppmu-event1-"#name, PPMU_PMNCNT1 }, \
48 { "ppmu-event2-"#name, PPMU_PMNCNT2 }, \
49 { "ppmu-event3-"#name, PPMU_PMNCNT3 }
51 static struct __exynos_ppmu_events
{
55 /* For Exynos3250, Exynos4 and Exynos5260 */
59 /* For Exynos4 SoCs and Exynos3250 */
68 /* Only for Exynos3250 and Exynos5260 */
71 /* Only for Exynos4 SoCs */
73 PPMU_EVENT(mfc
-right
),
75 /* Only for Exynos5260 SoCs */
89 /* Only for Exynos5433 SoCs */
91 PPMU_EVENT(d0
-general
),
94 PPMU_EVENT(d1
-general
),
97 /* For Exynos5422 SoC, deprecated (backwards compatible) */
102 /* For Exynos5422 SoC */
109 static int __exynos_ppmu_find_ppmu_id(const char *edev_name
)
113 for (i
= 0; i
< ARRAY_SIZE(ppmu_events
); i
++)
114 if (!strcmp(edev_name
, ppmu_events
[i
].name
))
115 return ppmu_events
[i
].id
;
120 static int exynos_ppmu_find_ppmu_id(struct devfreq_event_dev
*edev
)
122 return __exynos_ppmu_find_ppmu_id(edev
->desc
->name
);
126 * The devfreq-event ops structure for PPMU v1.1
128 static int exynos_ppmu_disable(struct devfreq_event_dev
*edev
)
130 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
134 /* Disable all counters */
135 ret
= regmap_write(info
->regmap
, PPMU_CNTENC
,
145 ret
= regmap_read(info
->regmap
, PPMU_PMNC
, &pmnc
);
149 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
150 ret
= regmap_write(info
->regmap
, PPMU_PMNC
, pmnc
);
157 static int exynos_ppmu_set_event(struct devfreq_event_dev
*edev
)
159 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
160 int id
= exynos_ppmu_find_ppmu_id(edev
);
167 /* Enable specific counter */
168 ret
= regmap_read(info
->regmap
, PPMU_CNTENS
, &cntens
);
172 cntens
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
173 ret
= regmap_write(info
->regmap
, PPMU_CNTENS
, cntens
);
177 /* Set the event of proper data type monitoring */
178 ret
= regmap_write(info
->regmap
, PPMU_BEVTxSEL(id
),
179 edev
->desc
->event_type
);
183 /* Reset cycle counter/performance counter and enable PPMU */
184 ret
= regmap_read(info
->regmap
, PPMU_PMNC
, &pmnc
);
188 pmnc
&= ~(PPMU_PMNC_ENABLE_MASK
189 | PPMU_PMNC_COUNTER_RESET_MASK
190 | PPMU_PMNC_CC_RESET_MASK
);
191 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_ENABLE_SHIFT
);
192 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_COUNTER_RESET_SHIFT
);
193 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_CC_RESET_SHIFT
);
194 ret
= regmap_write(info
->regmap
, PPMU_PMNC
, pmnc
);
201 static int exynos_ppmu_get_event(struct devfreq_event_dev
*edev
,
202 struct devfreq_event_data
*edata
)
204 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
205 int id
= exynos_ppmu_find_ppmu_id(edev
);
206 unsigned int total_count
, load_count
;
207 unsigned int pmcnt3_high
, pmcnt3_low
;
208 unsigned int pmnc
, cntenc
;
215 ret
= regmap_read(info
->regmap
, PPMU_PMNC
, &pmnc
);
219 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
220 ret
= regmap_write(info
->regmap
, PPMU_PMNC
, pmnc
);
224 /* Read cycle count */
225 ret
= regmap_read(info
->regmap
, PPMU_CCNT
, &total_count
);
228 edata
->total_count
= total_count
;
230 /* Read performance count */
235 ret
= regmap_read(info
->regmap
, PPMU_PMNCT(id
), &load_count
);
238 edata
->load_count
= load_count
;
241 ret
= regmap_read(info
->regmap
, PPMU_PMCNT3_HIGH
, &pmcnt3_high
);
245 ret
= regmap_read(info
->regmap
, PPMU_PMCNT3_LOW
, &pmcnt3_low
);
249 edata
->load_count
= ((pmcnt3_high
<< 8) | pmcnt3_low
);
255 /* Disable specific counter */
256 ret
= regmap_read(info
->regmap
, PPMU_CNTENC
, &cntenc
);
260 cntenc
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
261 ret
= regmap_write(info
->regmap
, PPMU_CNTENC
, cntenc
);
265 dev_dbg(&edev
->dev
, "%s (event: %ld/%ld)\n", edev
->desc
->name
,
266 edata
->load_count
, edata
->total_count
);
271 static const struct devfreq_event_ops exynos_ppmu_ops
= {
272 .disable
= exynos_ppmu_disable
,
273 .set_event
= exynos_ppmu_set_event
,
274 .get_event
= exynos_ppmu_get_event
,
278 * The devfreq-event ops structure for PPMU v2.0
280 static int exynos_ppmu_v2_disable(struct devfreq_event_dev
*edev
)
282 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
286 /* Disable all counters */
287 clear
= (PPMU_CCNT_MASK
| PPMU_PMCNT0_MASK
| PPMU_PMCNT1_MASK
288 | PPMU_PMCNT2_MASK
| PPMU_PMCNT3_MASK
);
289 ret
= regmap_write(info
->regmap
, PPMU_V2_FLAG
, clear
);
293 ret
= regmap_write(info
->regmap
, PPMU_V2_INTENC
, clear
);
297 ret
= regmap_write(info
->regmap
, PPMU_V2_CNTENC
, clear
);
301 ret
= regmap_write(info
->regmap
, PPMU_V2_CNT_RESET
, clear
);
305 ret
= regmap_write(info
->regmap
, PPMU_V2_CIG_CFG0
, 0x0);
309 ret
= regmap_write(info
->regmap
, PPMU_V2_CIG_CFG1
, 0x0);
313 ret
= regmap_write(info
->regmap
, PPMU_V2_CIG_CFG2
, 0x0);
317 ret
= regmap_write(info
->regmap
, PPMU_V2_CIG_RESULT
, 0x0);
321 ret
= regmap_write(info
->regmap
, PPMU_V2_CNT_AUTO
, 0x0);
325 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EV0_TYPE
, 0x0);
329 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EV1_TYPE
, 0x0);
333 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EV2_TYPE
, 0x0);
337 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EV3_TYPE
, 0x0);
341 ret
= regmap_write(info
->regmap
, PPMU_V2_SM_ID_V
, 0x0);
345 ret
= regmap_write(info
->regmap
, PPMU_V2_SM_ID_A
, 0x0);
349 ret
= regmap_write(info
->regmap
, PPMU_V2_SM_OTHERS_V
, 0x0);
353 ret
= regmap_write(info
->regmap
, PPMU_V2_SM_OTHERS_A
, 0x0);
357 ret
= regmap_write(info
->regmap
, PPMU_V2_INTERRUPT_RESET
, 0x0);
362 ret
= regmap_read(info
->regmap
, PPMU_V2_PMNC
, &pmnc
);
366 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
367 ret
= regmap_write(info
->regmap
, PPMU_V2_PMNC
, pmnc
);
374 static int exynos_ppmu_v2_set_event(struct devfreq_event_dev
*edev
)
376 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
377 unsigned int pmnc
, cntens
;
378 int id
= exynos_ppmu_find_ppmu_id(edev
);
381 /* Enable all counters */
382 ret
= regmap_read(info
->regmap
, PPMU_V2_CNTENS
, &cntens
);
386 cntens
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
387 ret
= regmap_write(info
->regmap
, PPMU_V2_CNTENS
, cntens
);
391 /* Set the event of proper data type monitoring */
392 ret
= regmap_write(info
->regmap
, PPMU_V2_CH_EVx_TYPE(id
),
393 edev
->desc
->event_type
);
397 /* Reset cycle counter/performance counter and enable PPMU */
398 ret
= regmap_read(info
->regmap
, PPMU_V2_PMNC
, &pmnc
);
402 pmnc
&= ~(PPMU_PMNC_ENABLE_MASK
403 | PPMU_PMNC_COUNTER_RESET_MASK
404 | PPMU_PMNC_CC_RESET_MASK
405 | PPMU_PMNC_CC_DIVIDER_MASK
406 | PPMU_V2_PMNC_START_MODE_MASK
);
407 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_ENABLE_SHIFT
);
408 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_COUNTER_RESET_SHIFT
);
409 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_CC_RESET_SHIFT
);
410 pmnc
|= (PPMU_V2_MODE_MANUAL
<< PPMU_V2_PMNC_START_MODE_SHIFT
);
412 ret
= regmap_write(info
->regmap
, PPMU_V2_PMNC
, pmnc
);
419 static int exynos_ppmu_v2_get_event(struct devfreq_event_dev
*edev
,
420 struct devfreq_event_data
*edata
)
422 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
423 int id
= exynos_ppmu_find_ppmu_id(edev
);
425 unsigned int pmnc
, cntenc
;
426 unsigned int pmcnt_high
, pmcnt_low
;
427 unsigned int total_count
, count
;
428 unsigned long load_count
= 0;
431 ret
= regmap_read(info
->regmap
, PPMU_V2_PMNC
, &pmnc
);
435 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
436 ret
= regmap_write(info
->regmap
, PPMU_V2_PMNC
, pmnc
);
440 /* Read cycle count and performance count */
441 ret
= regmap_read(info
->regmap
, PPMU_V2_CCNT
, &total_count
);
444 edata
->total_count
= total_count
;
450 ret
= regmap_read(info
->regmap
, PPMU_V2_PMNCT(id
), &count
);
456 ret
= regmap_read(info
->regmap
, PPMU_V2_PMCNT3_HIGH
,
461 ret
= regmap_read(info
->regmap
, PPMU_V2_PMCNT3_LOW
, &pmcnt_low
);
465 load_count
= ((u64
)((pmcnt_high
& 0xff)) << 32)+ (u64
)pmcnt_low
;
468 edata
->load_count
= load_count
;
470 /* Disable all counters */
471 ret
= regmap_read(info
->regmap
, PPMU_V2_CNTENC
, &cntenc
);
475 cntenc
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
476 ret
= regmap_write(info
->regmap
, PPMU_V2_CNTENC
, cntenc
);
480 dev_dbg(&edev
->dev
, "%25s (load: %ld / %ld)\n", edev
->desc
->name
,
481 edata
->load_count
, edata
->total_count
);
485 static const struct devfreq_event_ops exynos_ppmu_v2_ops
= {
486 .disable
= exynos_ppmu_v2_disable
,
487 .set_event
= exynos_ppmu_v2_set_event
,
488 .get_event
= exynos_ppmu_v2_get_event
,
491 static const struct of_device_id exynos_ppmu_id_match
[] = {
493 .compatible
= "samsung,exynos-ppmu",
494 .data
= (void *)EXYNOS_TYPE_PPMU
,
496 .compatible
= "samsung,exynos-ppmu-v2",
497 .data
= (void *)EXYNOS_TYPE_PPMU_V2
,
501 MODULE_DEVICE_TABLE(of
, exynos_ppmu_id_match
);
503 static int of_get_devfreq_events(struct device_node
*np
,
504 struct exynos_ppmu
*info
)
506 struct devfreq_event_desc
*desc
;
507 struct device
*dev
= info
->dev
;
508 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");
519 count
= of_get_child_count(events_np
);
520 desc
= devm_kcalloc(dev
, count
, sizeof(*desc
), GFP_KERNEL
);
522 of_node_put(events_np
);
525 info
->num_events
= count
;
527 info
->ppmu_type
= (enum exynos_ppmu_type
)device_get_match_data(dev
);
530 for_each_child_of_node(events_np
, node
) {
531 for (i
= 0; i
< ARRAY_SIZE(ppmu_events
); i
++) {
532 if (!ppmu_events
[i
].name
)
535 if (of_node_name_eq(node
, ppmu_events
[i
].name
))
539 if (i
== ARRAY_SIZE(ppmu_events
)) {
541 "don't know how to configure events : %pOFn\n",
546 switch (info
->ppmu_type
) {
547 case EXYNOS_TYPE_PPMU
:
548 desc
[j
].ops
= &exynos_ppmu_ops
;
550 case EXYNOS_TYPE_PPMU_V2
:
551 desc
[j
].ops
= &exynos_ppmu_v2_ops
;
555 desc
[j
].driver_data
= info
;
557 of_property_read_string(node
, "event-name", &desc
[j
].name
);
558 ret
= of_property_read_u32(node
, "event-data-type",
559 &desc
[j
].event_type
);
561 /* Set the event of proper data type counting.
562 * Check if the data type has been defined in DT,
563 * use default if not.
565 if (info
->ppmu_type
== EXYNOS_TYPE_PPMU_V2
) {
566 /* Not all registers take the same value for
567 * read+write data count.
569 switch (ppmu_events
[i
].id
) {
573 desc
[j
].event_type
= PPMU_V2_RO_DATA_CNT
574 | PPMU_V2_WO_DATA_CNT
;
578 PPMU_V2_EVT3_RW_DATA_CNT
;
582 desc
[j
].event_type
= PPMU_RO_DATA_CNT
|
591 of_node_put(events_np
);
596 static struct regmap_config exynos_ppmu_regmap_config
= {
602 static int exynos_ppmu_parse_dt(struct platform_device
*pdev
,
603 struct exynos_ppmu
*info
)
605 struct device
*dev
= info
->dev
;
606 struct device_node
*np
= dev
->of_node
;
607 struct resource
*res
;
612 dev_err(dev
, "failed to find devicetree node\n");
616 /* Maps the memory mapped IO to control PPMU register */
617 base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
619 return PTR_ERR(base
);
621 exynos_ppmu_regmap_config
.max_register
= resource_size(res
) - 4;
622 info
->regmap
= devm_regmap_init_mmio(dev
, base
,
623 &exynos_ppmu_regmap_config
);
624 if (IS_ERR(info
->regmap
)) {
625 dev_err(dev
, "failed to initialize regmap\n");
626 return PTR_ERR(info
->regmap
);
629 info
->ppmu
.clk
= devm_clk_get(dev
, "ppmu");
630 if (IS_ERR(info
->ppmu
.clk
)) {
631 info
->ppmu
.clk
= NULL
;
632 dev_warn(dev
, "cannot get PPMU clock\n");
635 ret
= of_get_devfreq_events(np
, info
);
637 dev_err(dev
, "failed to parse exynos ppmu dt node\n");
644 static int exynos_ppmu_probe(struct platform_device
*pdev
)
646 struct exynos_ppmu
*info
;
647 struct devfreq_event_dev
**edev
;
648 struct devfreq_event_desc
*desc
;
649 int i
, ret
= 0, size
;
651 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
655 info
->dev
= &pdev
->dev
;
657 /* Parse dt data to get resource */
658 ret
= exynos_ppmu_parse_dt(pdev
, info
);
661 "failed to parse devicetree for resource\n");
666 size
= sizeof(struct devfreq_event_dev
*) * info
->num_events
;
667 info
->edev
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
672 platform_set_drvdata(pdev
, info
);
674 for (i
= 0; i
< info
->num_events
; i
++) {
675 edev
[i
] = devm_devfreq_event_add_edev(&pdev
->dev
, &desc
[i
]);
676 if (IS_ERR(edev
[i
])) {
678 "failed to add devfreq-event device\n");
679 return PTR_ERR(edev
[i
]);
682 pr_info("exynos-ppmu: new PPMU device registered %s (%s)\n",
683 dev_name(&pdev
->dev
), desc
[i
].name
);
686 ret
= clk_prepare_enable(info
->ppmu
.clk
);
688 dev_err(&pdev
->dev
, "failed to prepare ppmu clock\n");
695 static void exynos_ppmu_remove(struct platform_device
*pdev
)
697 struct exynos_ppmu
*info
= platform_get_drvdata(pdev
);
699 clk_disable_unprepare(info
->ppmu
.clk
);
702 static struct platform_driver exynos_ppmu_driver
= {
703 .probe
= exynos_ppmu_probe
,
704 .remove
= exynos_ppmu_remove
,
706 .name
= "exynos-ppmu",
707 .of_match_table
= exynos_ppmu_id_match
,
710 module_platform_driver(exynos_ppmu_driver
);
712 MODULE_DESCRIPTION("Exynos PPMU(Platform Performance Monitoring Unit) driver");
713 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
714 MODULE_LICENSE("GPL");