2 * exynos_ppmu.c - EXYNOS PPMU (Platform Performance Monitoring Unit) support
4 * Copyright (c) 2014 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/mutex.h>
19 #include <linux/of_address.h>
20 #include <linux/platform_device.h>
21 #include <linux/suspend.h>
22 #include <linux/devfreq-event.h>
24 #include "exynos-ppmu.h"
26 struct exynos_ppmu_data
{
32 struct devfreq_event_dev
**edev
;
33 struct devfreq_event_desc
*desc
;
34 unsigned int num_events
;
39 struct exynos_ppmu_data ppmu
;
42 #define PPMU_EVENT(name) \
43 { "ppmu-event0-"#name, PPMU_PMNCNT0 }, \
44 { "ppmu-event1-"#name, PPMU_PMNCNT1 }, \
45 { "ppmu-event2-"#name, PPMU_PMNCNT2 }, \
46 { "ppmu-event3-"#name, PPMU_PMNCNT3 }
48 struct __exynos_ppmu_events
{
52 /* For Exynos3250, Exynos4 and Exynos5260 */
56 /* For Exynos4 SoCs and Exynos3250 */
65 /* Only for Exynos3250 and Exynos5260 */
68 /* Only for Exynos4 SoCs */
70 PPMU_EVENT(mfc
-right
),
72 /* Only for Exynos5260 SoCs */
88 static int exynos_ppmu_find_ppmu_id(struct devfreq_event_dev
*edev
)
92 for (i
= 0; i
< ARRAY_SIZE(ppmu_events
); i
++)
93 if (!strcmp(edev
->desc
->name
, ppmu_events
[i
].name
))
94 return ppmu_events
[i
].id
;
99 static int exynos_ppmu_disable(struct devfreq_event_dev
*edev
)
101 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
104 /* Disable all counters */
105 __raw_writel(PPMU_CCNT_MASK
|
110 info
->ppmu
.base
+ PPMU_CNTENC
);
113 pmnc
= __raw_readl(info
->ppmu
.base
+ PPMU_PMNC
);
114 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
115 __raw_writel(pmnc
, info
->ppmu
.base
+ PPMU_PMNC
);
120 static int exynos_ppmu_set_event(struct devfreq_event_dev
*edev
)
122 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
123 int id
= exynos_ppmu_find_ppmu_id(edev
);
129 /* Enable specific counter */
130 cntens
= __raw_readl(info
->ppmu
.base
+ PPMU_CNTENS
);
131 cntens
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
132 __raw_writel(cntens
, info
->ppmu
.base
+ PPMU_CNTENS
);
134 /* Set the event of Read/Write data count */
135 __raw_writel(PPMU_RO_DATA_CNT
| PPMU_WO_DATA_CNT
,
136 info
->ppmu
.base
+ PPMU_BEVTxSEL(id
));
138 /* Reset cycle counter/performance counter and enable PPMU */
139 pmnc
= __raw_readl(info
->ppmu
.base
+ PPMU_PMNC
);
140 pmnc
&= ~(PPMU_PMNC_ENABLE_MASK
141 | PPMU_PMNC_COUNTER_RESET_MASK
142 | PPMU_PMNC_CC_RESET_MASK
);
143 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_ENABLE_SHIFT
);
144 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_COUNTER_RESET_SHIFT
);
145 pmnc
|= (PPMU_ENABLE
<< PPMU_PMNC_CC_RESET_SHIFT
);
146 __raw_writel(pmnc
, info
->ppmu
.base
+ PPMU_PMNC
);
151 static int exynos_ppmu_get_event(struct devfreq_event_dev
*edev
,
152 struct devfreq_event_data
*edata
)
154 struct exynos_ppmu
*info
= devfreq_event_get_drvdata(edev
);
155 int id
= exynos_ppmu_find_ppmu_id(edev
);
162 pmnc
= __raw_readl(info
->ppmu
.base
+ PPMU_PMNC
);
163 pmnc
&= ~PPMU_PMNC_ENABLE_MASK
;
164 __raw_writel(pmnc
, info
->ppmu
.base
+ PPMU_PMNC
);
166 /* Read cycle count */
167 edata
->total_count
= __raw_readl(info
->ppmu
.base
+ PPMU_CCNT
);
169 /* Read performance count */
175 = __raw_readl(info
->ppmu
.base
+ PPMU_PMNCT(id
));
179 ((__raw_readl(info
->ppmu
.base
+ PPMU_PMCNT3_HIGH
) << 8)
180 | __raw_readl(info
->ppmu
.base
+ PPMU_PMCNT3_LOW
));
186 /* Disable specific counter */
187 cntenc
= __raw_readl(info
->ppmu
.base
+ PPMU_CNTENC
);
188 cntenc
|= (PPMU_CCNT_MASK
| (PPMU_ENABLE
<< id
));
189 __raw_writel(cntenc
, info
->ppmu
.base
+ PPMU_CNTENC
);
191 dev_dbg(&edev
->dev
, "%s (event: %ld/%ld)\n", edev
->desc
->name
,
192 edata
->load_count
, edata
->total_count
);
197 static const struct devfreq_event_ops exynos_ppmu_ops
= {
198 .disable
= exynos_ppmu_disable
,
199 .set_event
= exynos_ppmu_set_event
,
200 .get_event
= exynos_ppmu_get_event
,
203 static int of_get_devfreq_events(struct device_node
*np
,
204 struct exynos_ppmu
*info
)
206 struct devfreq_event_desc
*desc
;
207 struct device
*dev
= info
->dev
;
208 struct device_node
*events_np
, *node
;
211 events_np
= of_get_child_by_name(np
, "events");
214 "failed to get child node of devfreq-event devices\n");
218 count
= of_get_child_count(events_np
);
219 desc
= devm_kzalloc(dev
, sizeof(*desc
) * count
, GFP_KERNEL
);
222 info
->num_events
= count
;
225 for_each_child_of_node(events_np
, node
) {
226 for (i
= 0; i
< ARRAY_SIZE(ppmu_events
); i
++) {
227 if (!ppmu_events
[i
].name
)
230 if (!of_node_cmp(node
->name
, ppmu_events
[i
].name
))
234 if (i
== ARRAY_SIZE(ppmu_events
)) {
236 "don't know how to configure events : %s\n",
241 desc
[j
].ops
= &exynos_ppmu_ops
;
242 desc
[j
].driver_data
= info
;
244 of_property_read_string(node
, "event-name", &desc
[j
].name
);
252 of_node_put(events_np
);
257 static int exynos_ppmu_parse_dt(struct exynos_ppmu
*info
)
259 struct device
*dev
= info
->dev
;
260 struct device_node
*np
= dev
->of_node
;
264 dev_err(dev
, "failed to find devicetree node\n");
268 /* Maps the memory mapped IO to control PPMU register */
269 info
->ppmu
.base
= of_iomap(np
, 0);
270 if (IS_ERR_OR_NULL(info
->ppmu
.base
)) {
271 dev_err(dev
, "failed to map memory region\n");
275 info
->ppmu
.clk
= devm_clk_get(dev
, "ppmu");
276 if (IS_ERR(info
->ppmu
.clk
)) {
277 info
->ppmu
.clk
= NULL
;
278 dev_warn(dev
, "cannot get PPMU clock\n");
281 ret
= of_get_devfreq_events(np
, info
);
283 dev_err(dev
, "failed to parse exynos ppmu dt node\n");
290 iounmap(info
->ppmu
.base
);
295 static int exynos_ppmu_probe(struct platform_device
*pdev
)
297 struct exynos_ppmu
*info
;
298 struct devfreq_event_dev
**edev
;
299 struct devfreq_event_desc
*desc
;
300 int i
, ret
= 0, size
;
302 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
306 mutex_init(&info
->lock
);
307 info
->dev
= &pdev
->dev
;
309 /* Parse dt data to get resource */
310 ret
= exynos_ppmu_parse_dt(info
);
313 "failed to parse devicetree for resource\n");
318 size
= sizeof(struct devfreq_event_dev
*) * info
->num_events
;
319 info
->edev
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
322 "failed to allocate memory devfreq-event devices\n");
326 platform_set_drvdata(pdev
, info
);
328 for (i
= 0; i
< info
->num_events
; i
++) {
329 edev
[i
] = devm_devfreq_event_add_edev(&pdev
->dev
, &desc
[i
]);
330 if (IS_ERR(edev
[i
])) {
331 ret
= PTR_ERR(edev
[i
]);
333 "failed to add devfreq-event device\n");
338 clk_prepare_enable(info
->ppmu
.clk
);
342 iounmap(info
->ppmu
.base
);
347 static int exynos_ppmu_remove(struct platform_device
*pdev
)
349 struct exynos_ppmu
*info
= platform_get_drvdata(pdev
);
351 clk_disable_unprepare(info
->ppmu
.clk
);
352 iounmap(info
->ppmu
.base
);
357 static struct of_device_id exynos_ppmu_id_match
[] = {
358 { .compatible
= "samsung,exynos-ppmu", },
362 static struct platform_driver exynos_ppmu_driver
= {
363 .probe
= exynos_ppmu_probe
,
364 .remove
= exynos_ppmu_remove
,
366 .name
= "exynos-ppmu",
367 .of_match_table
= exynos_ppmu_id_match
,
370 module_platform_driver(exynos_ppmu_driver
);
372 MODULE_DESCRIPTION("Exynos PPMU(Platform Performance Monitoring Unit) driver");
373 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
374 MODULE_LICENSE("GPL");