summaryrefstree
path: root/kernel/drivers/devfreq/exynos/exynos-ppc.c
blob: 2b07e9afcbce057726a52c0b00797e423e5481a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * EXYNOS - PPMU support
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/io.h>
#include <linux/sched/clock.h>
#include <linux/math64.h>

#include "exynos-ppc.h"

#define PMNC			0x0004
#define CNTENS			0x0008
#define CNTENC			0x000C
#define CNTRESET		0x002C
#define CNTAUTO			0x0030
#define PMCNT			0x0034
#define CCNT			0x0048

#define BIT_CHVALUE		((0x1 << 31) | (0x1 << 0))
#define BIT_REGVALUE		(0x1 << 24)
#define BIT_RESETALL		(0x6)
#define BIT_GLBCNTEN		(0x1)

static u64 ppc_dvfs_total_ccnt;
static u64 ppc_dvfs_total_pmcnt;
static u64 ppc_dvfs_total_ccnt_alt;
static u64 ppc_dvfs_total_pmcnt_alt;
ktime_t last_updated_time;

DEFINE_MUTEX(ppc_dvfs_lock);

static void exynos_init_ppc(void __iomem *base)
{
	/* Initialize Q-CH, OP Mode */
	__raw_writel(BIT_REGVALUE | BIT_RESETALL, base + PMNC);

	/* Enable CCNT, PMCNTTx Counting*/
	__raw_writel(BIT_CHVALUE, base + CNTENS);
}

static void exynos_start_ppc(void __iomem *base)
{
	__raw_writel(BIT_REGVALUE | BIT_GLBCNTEN, base + PMNC);
}

static void exynos_stop_ppc(void __iomem *base)
{
	__raw_writel(BIT_REGVALUE, base + PMNC);
}

static void exynos_clear_ppc(void __iomem *base)
{
	__raw_writel(BIT_RESETALL, base + PMNC);
}

static void exynos_reset_ppc(void __iomem *base)
{
	__raw_writel(BIT_REGVALUE | BIT_RESETALL, base + PMNC);
}

static void exynos_read_ppc(struct exynos_devfreq_data *data)
{
	int i;
	struct ppc_data ppc = { 0, };
	int index = 0;

	data->um_data.val_ccnt = 0;
	data->um_data.val_pmcnt = 0;

	for (i = 0; i < data->um_data.um_count; i++) {
		ppc.ccnt = __raw_readl(data->um_data.va_base[i] + CCNT);
		ppc.pmcnt = __raw_readl(data->um_data.va_base[i] + PMCNT);

		if (data->um_data.val_pmcnt < ppc.pmcnt) {
			data->um_data.val_ccnt = ppc.ccnt;
			data->um_data.val_pmcnt = ppc.pmcnt;
			index = i;
		}
	}
}

int exynos_devfreq_um_init(struct exynos_devfreq_data *data)
{
	int i = 0;

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_init_ppc(data->um_data.va_base[i]);

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_start_ppc(data->um_data.va_base[i]);

	return 0;
}

void exynos_devfreq_um_exit(struct exynos_devfreq_data *data)
{
	int i = 0;

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_clear_ppc(data->um_data.va_base[i]);
}

ktime_t exynos_devfreq_get_ppc_status(struct exynos_devfreq_data *data)
{
	int i;
	unsigned int ccnt = 0, pmcnt = 0, max_ccnt = 0, max_pmcnt = 0;
	ktime_t active_time, cur_time;

	mutex_lock(&ppc_dvfs_lock);

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_stop_ppc(data->um_data.va_base[i]);

	/* Read current counter */
	for (i = 0 ; i < data->um_data.um_count; i++) {
		ccnt = __raw_readl(data->um_data.va_base[i] + CCNT);
		pmcnt = __raw_readl(data->um_data.va_base[i] + PMCNT);

		if (max_pmcnt < pmcnt) {
			max_ccnt = ccnt;
			max_pmcnt = pmcnt;
		}
	}

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_start_ppc(data->um_data.va_base[i]);

	cur_time = ktime_get();
	active_time = (cur_time - last_updated_time) * (max_pmcnt + ppc_dvfs_total_pmcnt_alt - ppc_dvfs_total_pmcnt) / (max_ccnt + ppc_dvfs_total_ccnt_alt - ppc_dvfs_total_ccnt);

	ppc_dvfs_total_pmcnt = max_pmcnt + ppc_dvfs_total_pmcnt_alt;
	ppc_dvfs_total_ccnt = max_ccnt + ppc_dvfs_total_ccnt_alt;
	last_updated_time = cur_time;

	mutex_unlock(&ppc_dvfs_lock);

	return active_time;

}

static int exynos_devfreq_get_dev_status(struct device *dev, struct devfreq_dev_status *stat)
{
	struct platform_device *pdev = container_of(dev, struct platform_device, dev);
	struct exynos_devfreq_data *data = platform_get_drvdata(pdev);
	u64 cur_time;
	int i;

	if (data->devfreq_disabled)
		return -EAGAIN;

	mutex_lock(&ppc_dvfs_lock);

	cur_time = sched_clock();
	data->last_monitor_period = (cur_time - data->last_monitor_time);
	data->last_monitor_time = cur_time;

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_stop_ppc(data->um_data.va_base[i]);

	/* Read current counter */
	exynos_read_ppc(data);

	stat->current_frequency = data->devfreq->previous_freq;
	stat->busy_time = data->um_data.val_pmcnt;
	stat->total_time = data->um_data.val_ccnt;
	stat->delta_time = data->last_monitor_period;

	data->last_um_usage_rate = div64_u64(stat->busy_time * 100, stat->total_time);

	ppc_dvfs_total_ccnt_alt += data->um_data.val_ccnt;
	ppc_dvfs_total_pmcnt_alt += data->um_data.val_pmcnt;

	for (i = 0; i < data->um_data.um_count; i++) {
		exynos_reset_ppc(data->um_data.va_base[i]);
		exynos_start_ppc(data->um_data.va_base[i]);
	}

	mutex_unlock(&ppc_dvfs_lock);

	return 0;
}

void register_get_dev_status(struct exynos_devfreq_data *data)
{
	data->devfreq_profile.get_dev_status = exynos_devfreq_get_dev_status;
	mutex_init(&ppc_dvfs_lock);
}