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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/* drivers/gud/sec-os-ctrl/secos_booster.c
*
* Secure OS booster driver for Samsung Exynos
*
* Copyright (c) 2014 Samsung Electronics
* http://www.samsungsemi.com/
*
* 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/kthread.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/time.h>
#include <linux/io.h>
#include <linux/fs.h>
#include <linux/of_gpio.h>
#include <linux/cpu.h>
#include <linux/cpufreq.h>
#include <linux/sched.h>
#include <linux/sched/rt.h>
#include <linux/pm_qos.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/suspend.h>
#include <mach/secos_booster.h>
#include <mach/cpufreq.h>
#include "platform.h"
#define MID_CPUFREQ 1700000
#define STB_CPUFREQ 1700000
#define MIN_CPUFREQ 800000
#define INT_PERFORMANCE 400000
#define INT_SAVE 0
#define BOOST_POLICY_OFFSET 0
#define BOOST_TIME_OFFSET 16
#define NS_DIV_MS (1000ull * 1000ull)
#define WAIT_TIME (10ull * NS_DIV_MS)
int mc_switch_core(uint32_t core_num);
void mc_set_schedule_policy(int core);
uint32_t mc_active_core(void);
int mc_boost_usage_count;
struct mutex boost_lock;
unsigned int current_core;
unsigned int is_suspend_prepared;
struct timer_work {
struct kthread_work work;
};
static struct pm_qos_request secos_booster_cluster_qos;
static struct pm_qos_request secos_booster_int_freq_qos;
static struct hrtimer timer;
static int max_cpu_freq;
static struct task_struct *mc_timer_thread; /* Timer Thread task structure */
static DEFINE_KTHREAD_WORKER(mc_timer_worker);
static struct hrtimer mc_hrtimer;
static enum hrtimer_restart mc_hrtimer_func(struct hrtimer *timer)
{
struct irq_desc *desc = irq_to_desc(MC_INTR_LOCAL_TIMER);
if (desc->depth != 0)
enable_irq(MC_INTR_LOCAL_TIMER);
return HRTIMER_NORESTART;
}
static void mc_timer_work_func(struct kthread_work *work)
{
hrtimer_start(&mc_hrtimer, ns_to_ktime((u64)LOCAL_TIMER_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
}
int secos_booster_request_pm_qos(struct pm_qos_request *req, s32 freq)
{
static ktime_t recent_qos_req_time;
ktime_t current_time;
unsigned long long ns;
current_time = ktime_get();
ns = ktime_to_ns(ktime_sub(current_time, recent_qos_req_time));
if (ns > 0 && WAIT_TIME > ns) {
pr_info("%s: recalling time is too short. wait %lldms\n", __func__, (WAIT_TIME - ns) / NS_DIV_MS + 1);
msleep((WAIT_TIME - ns) / NS_DIV_MS + 1);
}
pm_qos_update_request(req, freq);
if (freq == 0) {
pm_qos_update_request(&secos_booster_int_freq_qos, INT_SAVE);
} else if (freq == max_cpu_freq) {
pm_qos_update_request(&secos_booster_int_freq_qos, INT_PERFORMANCE);
}
recent_qos_req_time = ktime_get();
return 0;
}
int mc_timer(void)
{
struct timer_work t_work = {
KTHREAD_WORK_INIT(t_work.work, mc_timer_work_func),
};
if (!queue_kthread_work(&mc_timer_worker, &t_work.work))
return false;
flush_kthread_work(&t_work.work);
return true;
}
static int mc_timer_init(void)
{
cpumask_t cpu;
mc_timer_thread = kthread_create(kthread_worker_fn, &mc_timer_worker, "mc_timer");
if (IS_ERR(mc_timer_thread)) {
mc_timer_thread = NULL;
pr_err("%s: timer thread creation failed!", __func__);
return -EFAULT;
}
wake_up_process(mc_timer_thread);
cpumask_setall(&cpu);
cpumask_clear_cpu(DEFAULT_BIG_CORE, &cpu);
set_cpus_allowed(mc_timer_thread, cpu);
hrtimer_init(&mc_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mc_hrtimer.function = mc_hrtimer_func;
return 0;
}
static void stop_wq(struct work_struct *work)
{
int ret;
ret = secos_booster_stop();
if (ret)
pr_err("%s: secos_booster_stop failed. err:%d\n", __func__, ret);
return;
}
static DECLARE_WORK(stopwq, stop_wq);
static enum hrtimer_restart secos_booster_hrtimer_fn(struct hrtimer *timer)
{
schedule_work_on(0, &stopwq);
return HRTIMER_NORESTART;
}
int secos_booster_start(enum secos_boost_policy policy)
{
int ret = 0;
int freq;
uint32_t boost_time; /* milli second */
enum secos_boost_policy boost_policy;
mutex_lock(&boost_lock);
mc_boost_usage_count++;
if (mc_boost_usage_count > 1) {
goto out;
} else if (mc_boost_usage_count <= 0) {
pr_err("boost usage count sync error. count : %d\n", mc_boost_usage_count);
mc_boost_usage_count = 0;
ret = -EINVAL;
goto error;
}
current_core = mc_active_core();
boost_time = (((uint32_t)policy) >> BOOST_TIME_OFFSET) & 0xFFFF;
boost_policy = (((uint32_t)policy) >> BOOST_POLICY_OFFSET) & 0xFFFF;
/* migrate to big Core */
if (boost_policy >= PERFORMANCE_MAX_CNT || boost_policy < 0) {
pr_err("%s: wrong secos boost policy:%d\n", __func__, boost_policy);
ret = -EINVAL;
goto error;
}
/* cpufreq configuration */
if (boost_policy == MAX_PERFORMANCE)
freq = max_cpu_freq;
else if (boost_policy == MID_PERFORMANCE)
freq = MID_CPUFREQ;
else if (boost_policy == STB_PERFORMANCE)
freq = STB_CPUFREQ;
else
freq = MIN_CPUFREQ;
if (secos_booster_request_pm_qos(&secos_booster_cluster_qos, freq)) { /* KHz */
ret = -EPERM;
goto error;
}
if (!cpu_online(DEFAULT_BIG_CORE)) {
pr_debug("%s: %d core is offline\n", __func__, DEFAULT_BIG_CORE);
udelay(100);
if (!cpu_online(DEFAULT_BIG_CORE)) {
pr_debug("%s: %d core is offline\n", __func__, DEFAULT_BIG_CORE);
secos_booster_request_pm_qos(&secos_booster_cluster_qos, 0);
ret = -EPERM;
goto error;
}
pr_debug("%s: %d core is online\n", __func__, DEFAULT_BIG_CORE);
}
ret = mc_switch_core(DEFAULT_BIG_CORE);
if (ret) {
pr_err("%s: mc switch failed : err:%d\n", __func__, ret);
secos_booster_request_pm_qos(&secos_booster_cluster_qos, 0);
ret = -EPERM;
goto error;
}
if (boost_policy == STB_PERFORMANCE) {
/* Restore origin performance policy after default boost time */
if (boost_time == 0)
boost_time = DEFAULT_SECOS_BOOST_TIME;
else if (boost_time > MAX_SECOS_BOOST_TIME)
boost_time = MAX_SECOS_BOOST_TIME;
hrtimer_cancel(&timer);
hrtimer_start(&timer, ns_to_ktime((u64)boost_time * NSEC_PER_MSEC),
HRTIMER_MODE_REL);
} else {
/* Change schedule policy */
mc_set_schedule_policy(DEFAULT_BIG_CORE);
}
out:
mutex_unlock(&boost_lock);
return ret;
error:
mc_boost_usage_count--;
mutex_unlock(&boost_lock);
return ret;
}
int secos_booster_stop(void)
{
int ret = 0;
mutex_lock(&boost_lock);
mc_boost_usage_count--;
mc_set_schedule_policy(DEFAULT_LITTLE_CORE);
if (mc_boost_usage_count > 0) {
goto out;
} else if(mc_boost_usage_count == 0) {
hrtimer_cancel(&timer);
pr_debug("%s: mc switch to little core \n", __func__);
ret = mc_switch_core(current_core);
if (ret)
pr_err("%s: mc switch core failed. err:%d\n", __func__, ret);
secos_booster_request_pm_qos(&secos_booster_cluster_qos, 0);
} else {
/* mismatched usage count */
pr_warn("boost usage count sync mismatched. count : %d\n", mc_boost_usage_count);
mc_boost_usage_count = 0;
}
out:
mutex_unlock(&boost_lock);
return ret;
}
static int secos_booster_pm_notifier(struct notifier_block *notifier,
unsigned long pm_event, void *dummy)
{
mutex_lock(&boost_lock);
switch (pm_event) {
case PM_SUSPEND_PREPARE:
is_suspend_prepared = true;
break;
case PM_POST_SUSPEND:
is_suspend_prepared = false;
break;
}
mutex_unlock(&boost_lock);
return NOTIFY_OK;
}
static struct notifier_block secos_booster_pm_notifier_block = {
.notifier_call = secos_booster_pm_notifier,
};
static int __init secos_booster_init(void)
{
int ret;
mc_boost_usage_count = 0;
mutex_init(&boost_lock);
ret = mc_timer_init();
if (ret) {
pr_err("%s: mc timer init error :%d\n", __func__, ret);
return ret;
}
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
timer.function = secos_booster_hrtimer_fn;
max_cpu_freq = cpufreq_quick_get_max(DEFAULT_BIG_CORE);
pm_qos_add_request(&secos_booster_cluster_qos, PM_QOS_CLUSTER1_FREQ_MIN, 0);
pm_qos_add_request(&secos_booster_int_freq_qos, PM_QOS_DEVICE_THROUGHPUT, 0);
register_pm_notifier(&secos_booster_pm_notifier_block);
return ret;
}
late_initcall(secos_booster_init);
|