aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-exynos/dynamic-dvfs-nr_running-hotplug.c
blob: 7c380ae43000b901d69ed00b4b30c2c65a8315d4 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* linux/arch/arm/mach-exynos/dynamic-dvfs-nr_running-hotplug.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * EXYNOS4 - Integrated DVFS CPU hotplug
 *
 * 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/sched.h>
#include <linux/cpufreq.h>
#include <linux/cpu.h>
#include <linux/err.h>
#include <linux/notifier.h>
#include <linux/reboot.h>
#include <linux/suspend.h>
#include <linux/io.h>

#ifdef CONFIG_SLP
#include <linux/platform_device.h>
#endif

#include <plat/cpu.h>

static unsigned int total_num_target_freq;
static unsigned int ctn_freq_in_trg_cnt;	/* continuous frequency hotplug in trigger count */
static unsigned int ctn_freq_out_trg_cnt;	/* continuous frequency hotplug out trigger count */
static unsigned int ctn_nr_running_over2;
static unsigned int ctn_nr_running_over3;
static unsigned int ctn_nr_running_over4;
static unsigned int ctn_nr_running_under2;
static unsigned int ctn_nr_running_under3;
static unsigned int ctn_nr_running_under4;
static unsigned int freq_max;			/* max frequency of the dedicated dvfs table */
static unsigned int freq_min = -1UL;		/* min frequency of the dedicated dvfs table */
static unsigned int freq_in_trg;		/* frequency hotplug in trigger */
static unsigned int freq_out_trg;		/* frequency hotplug out trigger */
static unsigned int can_hotplug;
#ifdef CONFIG_SLP
static unsigned int user_lock;			/* Enable/Disable hotplug */
#endif

static void exynos4_integrated_dvfs_hotplug(unsigned int freq_old,
					unsigned int freq_new)
{

	total_num_target_freq++;
	freq_in_trg = 800000;			/* tunnable */
	freq_out_trg = freq_min;		/* tunnable */

	if (nr_running() <= 1) {
		ctn_nr_running_over2 = 0;
		ctn_nr_running_over3 = 0;
		ctn_nr_running_over4 = 0;
		ctn_nr_running_under2++;
		ctn_nr_running_under3++;
		ctn_nr_running_under4++;
	} else if ((nr_running() > 1) && (nr_running() <= 2)) {
		ctn_nr_running_over2++;
		ctn_nr_running_over3 = 0;
		ctn_nr_running_over4 = 0;
		ctn_nr_running_under2 = 0;
		ctn_nr_running_under3++;
		ctn_nr_running_under4++;
	} else if ((nr_running() > 2) && (nr_running() <= 3)) {
		ctn_nr_running_over2++;
		ctn_nr_running_over3++;
		ctn_nr_running_over4 = 0;
		ctn_nr_running_under2 = 0;
		ctn_nr_running_under3 = 0;
		ctn_nr_running_under4++;
	} else if (nr_running() > 3) {
		ctn_nr_running_over2++;
		ctn_nr_running_over3++;
		ctn_nr_running_over4++;
		ctn_nr_running_under2 = 0;
		ctn_nr_running_under3 = 0;
		ctn_nr_running_under4 = 0;
	}

	if ((freq_old >= freq_in_trg) && (freq_new >= freq_in_trg))
		ctn_freq_in_trg_cnt++;
	else
		ctn_freq_in_trg_cnt = 0;
	if ((freq_old <= freq_out_trg) && (freq_new <= freq_out_trg))
		ctn_freq_out_trg_cnt++;
	else
		ctn_freq_out_trg_cnt = 0;

	if (soc_is_exynos4412()) {
		if ((cpu_online(3) == 0) && (nr_running() >= 2) &&
		   ((freq_old >= freq_in_trg) && (freq_new >= freq_in_trg))) {
			if ((ctn_nr_running_over2 >= 4) &&
			   (ctn_freq_in_trg_cnt >= 5)) {
				/* over 400ms for nr_running(), over 500ms for frequency, tunnable */
				cpu_up(3);
				ctn_freq_in_trg_cnt = 0;
			}
		} else if ((cpu_online(2) == 0) && (nr_running() >= 3) &&
			  ((freq_old >= freq_in_trg) && (freq_new >= freq_in_trg))) {
			if ((ctn_nr_running_over3 >= 4) &&
			   (ctn_freq_in_trg_cnt >= 5)) {
				/* over 400ms for nr_running(), over 500ms for frequency, tunnable */
				cpu_up(2);
				ctn_freq_in_trg_cnt = 0;
			}
		} else if ((cpu_online(1) == 0) && (nr_running() >= 4) &&
			  ((freq_old >= freq_in_trg) && (freq_new >= freq_in_trg))) {
			if ((ctn_nr_running_over4 >= 8) &&
			   (ctn_freq_in_trg_cnt >= 5)) {
				/* over 800ms for nr_running(), over 500ms for frequency, tunnable */
				cpu_up(1);
				ctn_freq_in_trg_cnt = 0;
			}
		}
	} else {
		if ((cpu_online(1) == 0) && ((freq_old >= freq_in_trg) &&
		   (freq_new >= freq_in_trg))) {
			if ((ctn_nr_running_over2 >= 8) &&
			   (ctn_freq_in_trg_cnt >= 5)) {
				/* over 800ms  for nr_running(), over 500ms for frequency, tunnable */
				cpu_up(1);
				ctn_nr_running_over2 = 0;
				ctn_freq_in_trg_cnt = 0;
			}
		}
	}

	if (soc_is_exynos4412()) {
		if ((cpu_online(1) == 1) && (nr_running() < 4) &&
		   ((freq_old <= freq_out_trg) && (freq_new <= freq_out_trg))) {
			if ((ctn_nr_running_under4 >= 8) &&
			   (ctn_freq_out_trg_cnt >= 5)) {
				/* over 800ms for nr_running(), over 500ms for frequency, tunnable */
				cpu_down(1);
				ctn_freq_out_trg_cnt = 0;
			}
		} else if ((cpu_online(2) == 1) && (nr_running() < 3) &&
			  ((freq_old <= freq_out_trg) && (freq_new <= freq_out_trg))) {
			if ((ctn_nr_running_under3 >= 8) &&
			   (ctn_freq_out_trg_cnt >= 5)) {
				/* over 800ms for nr_running(), over 500ms for frequency, tunnable */
				cpu_down(2);
				ctn_freq_out_trg_cnt = 0;
			}
		} else if ((cpu_online(3) == 1) && (nr_running() < 2) &&
			  ((freq_old <= freq_out_trg) && (freq_new <= freq_out_trg))) {
			if ((ctn_nr_running_under2 >= 8) &&
			   (ctn_freq_out_trg_cnt >= 5)) {
				/* over 800ms for nr_running(), over 500ms for frequency, tunnable */
				cpu_down(3);
				ctn_freq_out_trg_cnt = 0;
			}
		}
	} else {
		if ((cpu_online(1) == 1) &&
		   ((freq_old <= freq_out_trg) && (freq_new <= freq_out_trg))) {
			if ((ctn_nr_running_under2 >= 8) &&
			   (ctn_freq_out_trg_cnt >= 5)) {
				/* over 800ms for nr_running(), over 500ms for frequency, tunnable */
				cpu_down(1);
				ctn_nr_running_under2 = 0;
				ctn_freq_out_trg_cnt = 0;
			}
		}
	}
}

static int hotplug_cpufreq_transition(struct notifier_block *nb,
					unsigned long val, void *data)
{
	struct cpufreq_freqs *freqs = (struct cpufreq_freqs *)data;

	if ((val == CPUFREQ_POSTCHANGE) && can_hotplug)
		exynos4_integrated_dvfs_hotplug(freqs->old, freqs->new);

	return 0;
}

static struct notifier_block dvfs_hotplug = {
	.notifier_call = hotplug_cpufreq_transition,
};

static int hotplug_pm_transition(struct notifier_block *nb,
					unsigned long val, void *data)
{
	switch (val) {
	case PM_SUSPEND_PREPARE:
		can_hotplug = 0;
		ctn_freq_in_trg_cnt = 0;
		ctn_freq_out_trg_cnt = 0;
		break;
	case PM_POST_RESTORE:
	case PM_POST_SUSPEND:
#ifdef CONFIG_SLP
		if (!user_lock)
#endif
			can_hotplug = 1;
		break;
	}

	return 0;
}

static struct notifier_block pm_hotplug = {
	.notifier_call = hotplug_pm_transition,
};

#ifdef CONFIG_SLP
static ssize_t user_lock_show(struct device *dev,
	 struct device_attribute *attr,	char *buf)
{
	return sprintf(buf, "%u\n", user_lock);
}

static ssize_t user_lock_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	int ret;

	ret = sscanf(buf, "%u", &user_lock);
	if (ret != 1)
		return -EINVAL;

	if (user_lock)
		can_hotplug = 0;
	else
		can_hotplug = 1;

	return count;
}
static DEVICE_ATTR(user_lock, 0644, user_lock_show, user_lock_store);

static int sysfs_pm_hotplug_create(struct device *dev)
{
	int ret;

	ret = device_create_file(dev, &dev_attr_user_lock);

	if (ret)
		device_remove_file(dev, &dev_attr_user_lock);

	return ret;
}

static void sysfs_pm_hotplug_remove(struct device *dev)
{
	device_remove_file(dev, &dev_attr_user_lock);
}

static struct platform_device exynos_pm_hotplug_device = {
	.name   = "exynos-dynamic-cpu-hotplug",
	.id     = -1,
};
#endif

/*
 * Note : This function should be called after intialization of CPUFreq
 * driver for exynos4. The cpufreq_frequency_table for exynos4 should be
 * established before calling this function.
 */
static int __init exynos4_integrated_dvfs_hotplug_init(void)
{
	int i;
	struct cpufreq_frequency_table *table;
	unsigned int freq;

#ifdef CONFIG_SLP
	int ret;
#endif

	total_num_target_freq = 0;
	ctn_freq_in_trg_cnt = 0;
	ctn_freq_out_trg_cnt = 0;
	ctn_nr_running_over2 = 0;
	ctn_nr_running_over3 = 0;
	ctn_nr_running_over4 = 0;
	ctn_nr_running_under2 = 0;
	ctn_nr_running_under3 = 0;
	ctn_nr_running_under4 = 0;

	can_hotplug = 1;

	table = cpufreq_frequency_get_table(0);
	if (IS_ERR(table)) {
		printk(KERN_ERR "%s: Check loading cpufreq before\n", __func__);
		return PTR_ERR(table);
	}

	for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
		freq = table[i].frequency;

		if (freq != CPUFREQ_ENTRY_INVALID && freq > freq_max)
			freq_max = freq;
		else if (freq != CPUFREQ_ENTRY_INVALID && freq_min > freq)
			freq_min = freq;
	}

	printk(KERN_INFO "%s, max(%d),min(%d)\n", __func__, freq_max, freq_min);

#ifdef CONFIG_SLP
	ret = platform_device_register(&exynos_pm_hotplug_device);
	if (ret) {
		printk(KERN_ERR "failed register pd\n");
		return ret;
	}

	ret = sysfs_pm_hotplug_create(&exynos_pm_hotplug_device.dev);
	if (ret)
		printk(KERN_ERR "failed at(%d)\n", __LINE__);
#endif

	register_pm_notifier(&pm_hotplug);

	return cpufreq_register_notifier(&dvfs_hotplug,
					 CPUFREQ_TRANSITION_NOTIFIER);
}

late_initcall(exynos4_integrated_dvfs_hotplug_init);