blob: 184ec612f078444c38b6845fc8eaf1b012aef12d [file] [log] [blame]
Abhijeet Dharmapurikara599de42012-08-23 13:49:45 -07001 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "mpd %s: " fmt, __func__
14
15#include <linux/cpumask.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/mutex.h>
20#include <linux/kthread.h>
21#include <linux/kobject.h>
22#include <linux/ktime.h>
23#include <linux/hrtimer.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <linux/cpu.h>
27#include <linux/stringify.h>
28#include <linux/sched.h>
29#include <linux/platform_device.h>
30#include <linux/debugfs.h>
31#include <linux/cpu_pm.h>
32#include <linux/cpu.h>
33#include <linux/cpufreq.h>
34#include <linux/sched.h>
35#include <linux/rq_stats.h>
36#include <asm/atomic.h>
37#include <asm/page.h>
38#include <mach/msm_dcvs.h>
39#include <mach/msm_dcvs_scm.h>
Abhijeet Dharmapurikarf17f2d32012-09-13 19:05:13 -070040#define CREATE_TRACE_POINTS
41#include <trace/events/mpdcvs_trace.h>
Abhijeet Dharmapurikara599de42012-08-23 13:49:45 -070042
43#define DEFAULT_RQ_AVG_POLL_MS (1)
44
45struct mpd_attrib {
46 struct kobj_attribute enabled;
47 struct kobj_attribute rq_avg_poll_ms;
48 struct kobj_attribute iowait_threshold_pct;
49
50 struct kobj_attribute em_win_size_min_us;
51 struct kobj_attribute em_win_size_max_us;
52 struct kobj_attribute em_max_util_pct;
53 struct kobj_attribute mp_em_rounding_point_min;
54 struct kobj_attribute mp_em_rounding_point_max;
55 struct kobj_attribute online_util_pct_min;
56 struct kobj_attribute online_util_pct_max;
57 struct kobj_attribute slack_time_min_us;
58 struct kobj_attribute slack_time_max_us;
59 struct kobj_attribute hp_up_max_ms;
60 struct kobj_attribute hp_up_ms;
61 struct kobj_attribute hp_up_count;
62 struct kobj_attribute hp_dw_max_ms;
63 struct kobj_attribute hp_dw_ms;
64 struct kobj_attribute hp_dw_count;
65 struct attribute_group attrib_group;
66};
67
68struct msm_mpd_scm_data {
69 enum msm_dcvs_scm_event event;
70 int nr;
71};
72
73struct mpdecision {
74 uint32_t enabled;
75 atomic_t algo_cpu_mask;
76 uint32_t rq_avg_poll_ms;
77 uint32_t iowait_threshold_pct;
78 ktime_t next_update;
79 uint32_t slack_us;
80 struct msm_mpd_algo_param mp_param;
81 struct mpd_attrib attrib;
82 struct mutex lock;
83 struct task_struct *task;
84 struct task_struct *hptask;
85 struct hrtimer slack_timer;
86 struct msm_mpd_scm_data data;
87 int hpupdate;
88 wait_queue_head_t wait_q;
89 wait_queue_head_t wait_hpq;
90};
91
92struct hp_latency {
93 int hp_up_max_ms;
94 int hp_up_ms;
95 int hp_up_count;
96 int hp_dw_max_ms;
97 int hp_dw_ms;
98 int hp_dw_count;
99};
100
101static DEFINE_PER_CPU(struct hrtimer, rq_avg_poll_timer);
102static DEFINE_SPINLOCK(rq_avg_lock);
103
104enum {
105 MSM_MPD_DEBUG_NOTIFIER = BIT(0),
106 MSM_MPD_CORE_STATUS = BIT(1),
107 MSM_MPD_SLACK_TIMER = BIT(2),
108};
109
110enum {
111 HPUPDATE_WAITING = 0, /* we are waiting for cpumask update */
112 HPUPDATE_SCHEDULED = 1, /* we are in the process of hotplugging */
113 HPUPDATE_IN_PROGRESS = 2, /* we are in the process of hotplugging */
114};
115
116static int msm_mpd_enabled = 1;
117module_param_named(enabled, msm_mpd_enabled, int, S_IRUGO | S_IWUSR | S_IWGRP);
118
119static struct dentry *debugfs_base;
120static struct mpdecision msm_mpd;
121
122static struct hp_latency hp_latencies;
123
124static unsigned long last_nr;
125static int num_present_hundreds;
126
127#define RQ_AVG_INSIGNIFICANT_BITS 3
128static bool ok_to_update_tz(int nr, int last_nr)
129{
130 /*
131 * Exclude unnecessary TZ reports if run queue haven't changed much from
132 * the last reported value. The left shift by INSIGNIFICANT_BITS is to
133 * filter out small changes in the run queue average which won't cause
134 * a online cpu mask change. Also if the cpu online count does not match
135 * the count requested by TZ and we are not in the process of bringing
136 * cpus online as indicated by a HPUPDATE_IN_PROGRESS in msm_mpd.hpdata
137 */
138 return
139 (((nr >> RQ_AVG_INSIGNIFICANT_BITS)
140 != (last_nr >> RQ_AVG_INSIGNIFICANT_BITS))
141 || ((hweight32(atomic_read(&msm_mpd.algo_cpu_mask))
142 != num_online_cpus())
143 && (msm_mpd.hpupdate != HPUPDATE_IN_PROGRESS)));
144}
145
146static enum hrtimer_restart msm_mpd_rq_avg_poll_timer(struct hrtimer *timer)
147{
148 int nr, nr_iowait;
149 ktime_t curr_time = ktime_get();
150 unsigned long flags;
151 int cpu = smp_processor_id();
152 enum hrtimer_restart restart = HRTIMER_RESTART;
153
154 spin_lock_irqsave(&rq_avg_lock, flags);
155 /* If running on the wrong cpu, don't restart */
156 if (&per_cpu(rq_avg_poll_timer, cpu) != timer)
157 restart = HRTIMER_NORESTART;
158
159 if (ktime_to_ns(ktime_sub(curr_time, msm_mpd.next_update)) < 0)
160 goto out;
161
162 msm_mpd.next_update = ktime_add_ns(curr_time,
163 (msm_mpd.rq_avg_poll_ms * NSEC_PER_MSEC));
164
165 sched_get_nr_running_avg(&nr, &nr_iowait);
166
167 if ((nr_iowait >= msm_mpd.iowait_threshold_pct) && (nr < last_nr))
168 nr = last_nr;
169
170 if (nr > num_present_hundreds)
171 nr = num_present_hundreds;
172
Abhijeet Dharmapurikarf17f2d32012-09-13 19:05:13 -0700173 trace_msm_mp_runq("nr_running", nr);
174
Abhijeet Dharmapurikara599de42012-08-23 13:49:45 -0700175 if (ok_to_update_tz(nr, last_nr)) {
176 hrtimer_try_to_cancel(&msm_mpd.slack_timer);
177 msm_mpd.data.nr = nr;
178 msm_mpd.data.event = MSM_DCVS_SCM_RUNQ_UPDATE;
179 wake_up(&msm_mpd.wait_q);
180 last_nr = nr;
181 }
182
183out:
184 hrtimer_set_expires(timer, msm_mpd.next_update);
185 spin_unlock_irqrestore(&rq_avg_lock, flags);
186 /* set next expiration */
187 return restart;
188}
189
190static void bring_up_cpu(int cpu)
191{
192 int cpu_action_time_ms;
193 int time_taken_ms;
194 int ret, ret1, ret2;
195
196 cpu_action_time_ms = ktime_to_ms(ktime_get());
197 ret = cpu_up(cpu);
198 if (ret) {
199 pr_debug("Error %d online core %d\n", ret, cpu);
200 } else {
201 time_taken_ms = ktime_to_ms(ktime_get()) - cpu_action_time_ms;
202 if (time_taken_ms > hp_latencies.hp_up_max_ms)
203 hp_latencies.hp_up_max_ms = time_taken_ms;
204 if (time_taken_ms > 5)
205 pr_warn("cpu_up for cpu%d exceeded 5ms (%d)\n",
206 cpu, time_taken_ms);
207 hp_latencies.hp_up_ms += time_taken_ms;
208 hp_latencies.hp_up_count++;
209 ret = msm_dcvs_scm_event(
210 CPU_OFFSET + cpu,
211 MSM_DCVS_SCM_CORE_ONLINE,
212 cpufreq_get(cpu),
213 (uint32_t) time_taken_ms * USEC_PER_MSEC,
214 &ret1, &ret2);
215 if (ret)
216 pr_err("Error sending hotplug scm event err=%d\n", ret);
217 }
218}
219
220static void bring_down_cpu(int cpu)
221{
222 int cpu_action_time_ms;
223 int time_taken_ms;
224 int ret, ret1, ret2;
225
226 BUG_ON(cpu == 0);
227 cpu_action_time_ms = ktime_to_ms(ktime_get());
228 ret = cpu_down(cpu);
229 if (ret) {
230 pr_debug("Error %d offline" "core %d\n", ret, cpu);
231 } else {
232 time_taken_ms = ktime_to_ms(ktime_get()) - cpu_action_time_ms;
233 if (time_taken_ms > hp_latencies.hp_dw_max_ms)
234 hp_latencies.hp_dw_max_ms = time_taken_ms;
235 if (time_taken_ms > 5)
236 pr_warn("cpu_down for cpu%d exceeded 5ms (%d)\n",
237 cpu, time_taken_ms);
238 hp_latencies.hp_dw_ms += time_taken_ms;
239 hp_latencies.hp_dw_count++;
240 ret = msm_dcvs_scm_event(
241 CPU_OFFSET + cpu,
242 MSM_DCVS_SCM_CORE_OFFLINE,
243 (uint32_t) time_taken_ms * USEC_PER_MSEC,
244 0,
245 &ret1, &ret2);
246 if (ret)
247 pr_err("Error sending hotplug scm event err=%d\n", ret);
248 }
249}
250
251static int __ref msm_mpd_update_scm(enum msm_dcvs_scm_event event, int nr)
252{
253 int ret = 0;
254 uint32_t req_cpu_mask = 0;
255 uint32_t slack_us = 0;
256 uint32_t param0 = 0;
257
258 if (event == MSM_DCVS_SCM_RUNQ_UPDATE)
259 param0 = nr;
260
261 ret = msm_dcvs_scm_event(0, event, param0, 0,
262 &req_cpu_mask, &slack_us);
263
264 if (ret) {
265 pr_err("Error (%d) sending event %d, param %d\n", ret, event,
266 param0);
267 return ret;
268 }
269
Abhijeet Dharmapurikarf17f2d32012-09-13 19:05:13 -0700270 trace_msm_mp_cpusonline("cpu_online_mp", req_cpu_mask);
271 trace_msm_mp_slacktime("slack_time_mp", slack_us);
Abhijeet Dharmapurikara599de42012-08-23 13:49:45 -0700272 msm_mpd.slack_us = slack_us;
273 atomic_set(&msm_mpd.algo_cpu_mask, req_cpu_mask);
274 msm_mpd.hpupdate = HPUPDATE_SCHEDULED;
275 wake_up(&msm_mpd.wait_hpq);
276
277 /* Start MP Decision slack timer */
278 if (slack_us) {
279 hrtimer_cancel(&msm_mpd.slack_timer);
280 ret = hrtimer_start(&msm_mpd.slack_timer,
281 ktime_set(0, slack_us * NSEC_PER_USEC),
282 HRTIMER_MODE_REL_PINNED);
283 if (ret)
284 pr_err("Failed to register slack timer (%d) %d\n",
285 slack_us, ret);
286 }
287
288 return ret;
289}
290
291static enum hrtimer_restart msm_mpd_slack_timer(struct hrtimer *timer)
292{
293 unsigned long flags;
294
Abhijeet Dharmapurikarf17f2d32012-09-13 19:05:13 -0700295 trace_printk("mpd:slack_timer_fired!\n");
296
Abhijeet Dharmapurikara599de42012-08-23 13:49:45 -0700297 spin_lock_irqsave(&rq_avg_lock, flags);
298 if (msm_mpd.data.event == MSM_DCVS_SCM_RUNQ_UPDATE)
299 goto out;
300
301 msm_mpd.data.nr = 0;
302 msm_mpd.data.event = MSM_DCVS_SCM_MPD_QOS_TIMER_EXPIRED;
303 wake_up(&msm_mpd.wait_q);
304out:
305 spin_unlock_irqrestore(&rq_avg_lock, flags);
306 return HRTIMER_NORESTART;
307}
308
309static int msm_mpd_idle_notifier(struct notifier_block *self,
310 unsigned long cmd, void *v)
311{
312 int cpu = smp_processor_id();
313 unsigned long flags;
314
315 switch (cmd) {
316 case CPU_PM_EXIT:
317 spin_lock_irqsave(&rq_avg_lock, flags);
318 hrtimer_start(&per_cpu(rq_avg_poll_timer, cpu),
319 msm_mpd.next_update,
320 HRTIMER_MODE_ABS_PINNED);
321 spin_unlock_irqrestore(&rq_avg_lock, flags);
322 break;
323 case CPU_PM_ENTER:
324 hrtimer_cancel(&per_cpu(rq_avg_poll_timer, cpu));
325 break;
326 default:
327 break;
328 }
329
330 return NOTIFY_OK;
331}
332
333static int msm_mpd_hotplug_notifier(struct notifier_block *self,
334 unsigned long action, void *hcpu)
335{
336 int cpu = (int)hcpu;
337 unsigned long flags;
338
339 switch (action & (~CPU_TASKS_FROZEN)) {
340 case CPU_STARTING:
341 spin_lock_irqsave(&rq_avg_lock, flags);
342 hrtimer_start(&per_cpu(rq_avg_poll_timer, cpu),
343 msm_mpd.next_update,
344 HRTIMER_MODE_ABS_PINNED);
345 spin_unlock_irqrestore(&rq_avg_lock, flags);
346 break;
347 default:
348 break;
349 }
350
351 return NOTIFY_OK;
352}
353
354static struct notifier_block msm_mpd_idle_nb = {
355 .notifier_call = msm_mpd_idle_notifier,
356};
357
358static struct notifier_block msm_mpd_hotplug_nb = {
359 .notifier_call = msm_mpd_hotplug_notifier,
360};
361
362static int __cpuinit msm_mpd_do_hotplug(void *data)
363{
364 int *event = (int *)data;
365 static struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1};
366 int cpu;
367
368 sched_setscheduler(current, SCHED_FIFO, &param);
369
370 while (1) {
371 wait_event(msm_mpd.wait_hpq, *event || kthread_should_stop());
372 if (kthread_should_stop())
373 break;
374
375 msm_mpd.hpupdate = HPUPDATE_IN_PROGRESS;
376 /*
377 * Bring online any offline cores, then offline any online
378 * cores. Whenever a core is off/onlined restart the procedure
379 * in case a new core is desired to be brought online in the
380 * mean time.
381 */
382restart:
383 for_each_possible_cpu(cpu) {
384 if ((atomic_read(&msm_mpd.algo_cpu_mask) & (1 << cpu))
385 && !cpu_online(cpu)) {
386 bring_up_cpu(cpu);
387 if (cpu_online(cpu))
388 goto restart;
389 }
390 }
391
392 for_each_possible_cpu(cpu) {
393 if (!(atomic_read(&msm_mpd.algo_cpu_mask) & (1 << cpu))
394 && cpu_online(cpu)) {
395 bring_down_cpu(cpu);
396 if (!cpu_online(cpu))
397 goto restart;
398 }
399 }
400 msm_mpd.hpupdate = HPUPDATE_WAITING;
401 }
402
403 return 0;
404}
405
406static int msm_mpd_do_update_scm(void *data)
407{
408 struct msm_mpd_scm_data *scm_data = (struct msm_mpd_scm_data *)data;
409 static struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1};
410 unsigned long flags;
411 enum msm_dcvs_scm_event event;
412 int nr;
413
414 sched_setscheduler(current, SCHED_FIFO, &param);
415
416 while (1) {
417 wait_event(msm_mpd.wait_q,
418 msm_mpd.data.event == MSM_DCVS_SCM_MPD_QOS_TIMER_EXPIRED
419 || msm_mpd.data.event == MSM_DCVS_SCM_RUNQ_UPDATE
420 || kthread_should_stop());
421
422 if (kthread_should_stop())
423 break;
424
425 spin_lock_irqsave(&rq_avg_lock, flags);
426 event = scm_data->event;
427 nr = scm_data->nr;
428 scm_data->event = 0;
429 scm_data->nr = 0;
430 spin_unlock_irqrestore(&rq_avg_lock, flags);
431
432 msm_mpd_update_scm(event, nr);
433 }
434 return 0;
435}
436
437static int __ref msm_mpd_set_enabled(uint32_t enable)
438{
439 int ret = 0;
440 int ret0 = 0;
441 int ret1 = 0;
442 int cpu;
443 static uint32_t last_enable;
444
445 enable = (enable > 0) ? 1 : 0;
446 if (last_enable == enable)
447 return ret;
448
449 if (enable) {
450 ret = msm_mpd_scm_set_algo_params(&msm_mpd.mp_param);
451 if (ret) {
452 pr_err("Error(%d): msm_mpd_scm_set_algo_params failed\n",
453 ret);
454 return ret;
455 }
456 }
457
458 ret = msm_dcvs_scm_event(0, MSM_DCVS_SCM_MPD_ENABLE, enable, 0,
459 &ret0, &ret1);
460 if (ret) {
461 pr_err("Error(%d) %s MP Decision\n",
462 ret, (enable ? "enabling" : "disabling"));
463 } else {
464 last_enable = enable;
465 last_nr = 0;
466 }
467 if (enable) {
468 msm_mpd.next_update = ktime_add_ns(ktime_get(),
469 (msm_mpd.rq_avg_poll_ms * NSEC_PER_MSEC));
470 msm_mpd.task = kthread_run(msm_mpd_do_update_scm,
471 &msm_mpd.data, "msm_mpdecision");
472 if (IS_ERR(msm_mpd.task))
473 return -EFAULT;
474
475 msm_mpd.hptask = kthread_run(msm_mpd_do_hotplug,
476 &msm_mpd.hpupdate, "msm_hp");
477 if (IS_ERR(msm_mpd.hptask))
478 return -EFAULT;
479
480 for_each_online_cpu(cpu)
481 hrtimer_start(&per_cpu(rq_avg_poll_timer, cpu),
482 msm_mpd.next_update,
483 HRTIMER_MODE_ABS_PINNED);
484 cpu_pm_register_notifier(&msm_mpd_idle_nb);
485 register_cpu_notifier(&msm_mpd_hotplug_nb);
486 msm_mpd.enabled = 1;
487 } else {
488 for_each_online_cpu(cpu)
489 hrtimer_cancel(&per_cpu(rq_avg_poll_timer, cpu));
490 kthread_stop(msm_mpd.hptask);
491 kthread_stop(msm_mpd.task);
492 cpu_pm_unregister_notifier(&msm_mpd_idle_nb);
493 unregister_cpu_notifier(&msm_mpd_hotplug_nb);
494 msm_mpd.enabled = 0;
495 }
496
497 return ret;
498}
499
500static int msm_mpd_set_rq_avg_poll_ms(uint32_t val)
501{
502 /*
503 * No need to do anything. Just let the timer set its own next poll
504 * interval when it next fires.
505 */
506 msm_mpd.rq_avg_poll_ms = val;
507 return 0;
508}
509
510static int msm_mpd_set_iowait_threshold_pct(uint32_t val)
511{
512 /*
513 * No need to do anything. Just let the timer set its own next poll
514 * interval when it next fires.
515 */
516 msm_mpd.iowait_threshold_pct = val;
517 return 0;
518}
519
520#define MPD_ALGO_PARAM(_name, _param) \
521static ssize_t msm_mpd_attr_##_name##_show(struct kobject *kobj, \
522 struct kobj_attribute *attr, char *buf) \
523{ \
524 return snprintf(buf, PAGE_SIZE, "%d\n", _param); \
525} \
526static ssize_t msm_mpd_attr_##_name##_store(struct kobject *kobj, \
527 struct kobj_attribute *attr, const char *buf, size_t count) \
528{ \
529 int ret = 0; \
530 uint32_t val; \
531 uint32_t old_val; \
532 mutex_lock(&msm_mpd.lock); \
533 ret = kstrtouint(buf, 10, &val); \
534 if (ret) { \
535 pr_err("Invalid input %s for %s %d\n", \
536 buf, __stringify(_name), ret);\
537 return 0; \
538 } \
539 old_val = _param; \
540 _param = val; \
541 ret = msm_mpd_scm_set_algo_params(&msm_mpd.mp_param); \
542 if (ret) { \
543 pr_err("Error %d returned when setting algo param %s to %d\n",\
544 ret, __stringify(_name), val); \
545 _param = old_val; \
546 } \
547 mutex_unlock(&msm_mpd.lock); \
548 return count; \
549}
550
551#define MPD_PARAM(_name, _param) \
552static ssize_t msm_mpd_attr_##_name##_show(struct kobject *kobj, \
553 struct kobj_attribute *attr, char *buf) \
554{ \
555 return snprintf(buf, PAGE_SIZE, "%d\n", _param); \
556} \
557static ssize_t msm_mpd_attr_##_name##_store(struct kobject *kobj, \
558 struct kobj_attribute *attr, const char *buf, size_t count) \
559{ \
560 int ret = 0; \
561 uint32_t val; \
562 uint32_t old_val; \
563 mutex_lock(&msm_mpd.lock); \
564 ret = kstrtouint(buf, 10, &val); \
565 if (ret) { \
566 pr_err("Invalid input %s for %s %d\n", \
567 buf, __stringify(_name), ret);\
568 return 0; \
569 } \
570 old_val = _param; \
571 ret = msm_mpd_set_##_name(val); \
572 if (ret) { \
573 pr_err("Error %d returned when setting algo param %s to %d\n",\
574 ret, __stringify(_name), val); \
575 _param = old_val; \
576 } \
577 mutex_unlock(&msm_mpd.lock); \
578 return count; \
579}
580
581#define MPD_RW_ATTRIB(i, _name) \
582 msm_mpd.attrib._name.attr.name = __stringify(_name); \
583 msm_mpd.attrib._name.attr.mode = S_IRUGO | S_IWUSR; \
584 msm_mpd.attrib._name.show = msm_mpd_attr_##_name##_show; \
585 msm_mpd.attrib._name.store = msm_mpd_attr_##_name##_store; \
586 msm_mpd.attrib.attrib_group.attrs[i] = &msm_mpd.attrib._name.attr;
587
588MPD_PARAM(enabled, msm_mpd.enabled);
589MPD_PARAM(rq_avg_poll_ms, msm_mpd.rq_avg_poll_ms);
590MPD_PARAM(iowait_threshold_pct, msm_mpd.iowait_threshold_pct);
591MPD_ALGO_PARAM(em_win_size_min_us, msm_mpd.mp_param.em_win_size_min_us);
592MPD_ALGO_PARAM(em_win_size_max_us, msm_mpd.mp_param.em_win_size_max_us);
593MPD_ALGO_PARAM(em_max_util_pct, msm_mpd.mp_param.em_max_util_pct);
594MPD_ALGO_PARAM(mp_em_rounding_point_min,
595 msm_mpd.mp_param.mp_em_rounding_point_min);
596MPD_ALGO_PARAM(mp_em_rounding_point_max,
597 msm_mpd.mp_param.mp_em_rounding_point_max);
598MPD_ALGO_PARAM(online_util_pct_min, msm_mpd.mp_param.online_util_pct_min);
599MPD_ALGO_PARAM(online_util_pct_max, msm_mpd.mp_param.online_util_pct_max);
600MPD_ALGO_PARAM(slack_time_min_us, msm_mpd.mp_param.slack_time_min_us);
601MPD_ALGO_PARAM(slack_time_max_us, msm_mpd.mp_param.slack_time_max_us);
602MPD_ALGO_PARAM(hp_up_max_ms, hp_latencies.hp_up_max_ms);
603MPD_ALGO_PARAM(hp_up_ms, hp_latencies.hp_up_ms);
604MPD_ALGO_PARAM(hp_up_count, hp_latencies.hp_up_count);
605MPD_ALGO_PARAM(hp_dw_max_ms, hp_latencies.hp_dw_max_ms);
606MPD_ALGO_PARAM(hp_dw_ms, hp_latencies.hp_dw_ms);
607MPD_ALGO_PARAM(hp_dw_count, hp_latencies.hp_dw_count);
608
609static int __devinit msm_mpd_probe(struct platform_device *pdev)
610{
611 struct kobject *module_kobj = NULL;
612 int ret = 0;
613 const int attr_count = 19;
614 struct msm_mpd_algo_param *param = NULL;
615
616 param = pdev->dev.platform_data;
617
618 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
619 if (!module_kobj) {
620 pr_err("Cannot find kobject for module %s\n", KBUILD_MODNAME);
621 ret = -ENOENT;
622 goto done;
623 }
624
625 msm_mpd.attrib.attrib_group.attrs =
626 kzalloc(attr_count * sizeof(struct attribute *), GFP_KERNEL);
627 if (!msm_mpd.attrib.attrib_group.attrs) {
628 ret = -ENOMEM;
629 goto done;
630 }
631
632 MPD_RW_ATTRIB(0, enabled);
633 MPD_RW_ATTRIB(1, rq_avg_poll_ms);
634 MPD_RW_ATTRIB(2, iowait_threshold_pct);
635 MPD_RW_ATTRIB(3, em_win_size_min_us);
636 MPD_RW_ATTRIB(4, em_win_size_max_us);
637 MPD_RW_ATTRIB(5, em_max_util_pct);
638 MPD_RW_ATTRIB(6, mp_em_rounding_point_min);
639 MPD_RW_ATTRIB(7, mp_em_rounding_point_max);
640 MPD_RW_ATTRIB(8, online_util_pct_min);
641 MPD_RW_ATTRIB(9, online_util_pct_max);
642 MPD_RW_ATTRIB(10, slack_time_min_us);
643 MPD_RW_ATTRIB(11, slack_time_max_us);
644 MPD_RW_ATTRIB(12, hp_up_max_ms);
645 MPD_RW_ATTRIB(13, hp_up_ms);
646 MPD_RW_ATTRIB(14, hp_up_count);
647 MPD_RW_ATTRIB(15, hp_dw_max_ms);
648 MPD_RW_ATTRIB(16, hp_dw_ms);
649 MPD_RW_ATTRIB(17, hp_dw_count);
650
651 msm_mpd.attrib.attrib_group.attrs[18] = NULL;
652 ret = sysfs_create_group(module_kobj, &msm_mpd.attrib.attrib_group);
653 if (ret)
654 pr_err("Unable to create sysfs objects :%d\n", ret);
655
656 msm_mpd.rq_avg_poll_ms = DEFAULT_RQ_AVG_POLL_MS;
657
658 memcpy(&msm_mpd.mp_param, param, sizeof(struct msm_mpd_algo_param));
659
660 debugfs_base = debugfs_create_dir("msm_mpdecision", NULL);
661 if (!debugfs_base) {
662 pr_err("Cannot create debugfs base msm_mpdecision\n");
663 ret = -ENOENT;
664 goto done;
665 }
666
667done:
668 if (ret && debugfs_base)
669 debugfs_remove(debugfs_base);
670
671 return ret;
672}
673
674static int __devexit msm_mpd_remove(struct platform_device *pdev)
675{
676 platform_set_drvdata(pdev, NULL);
677
678 return 0;
679}
680
681static struct platform_driver msm_mpd_driver = {
682 .probe = msm_mpd_probe,
683 .remove = __devexit_p(msm_mpd_remove),
684 .driver = {
685 .name = "msm_mpdecision",
686 .owner = THIS_MODULE,
687 },
688};
689
690static int __init msm_mpdecision_init(void)
691{
692 int cpu;
693 if (!msm_mpd_enabled) {
694 pr_info("Not enabled\n");
695 return 0;
696 }
697
698 num_present_hundreds = 100 * num_present_cpus();
699
700 hrtimer_init(&msm_mpd.slack_timer, CLOCK_MONOTONIC,
701 HRTIMER_MODE_REL_PINNED);
702 msm_mpd.slack_timer.function = msm_mpd_slack_timer;
703
704 for_each_possible_cpu(cpu) {
705 hrtimer_init(&per_cpu(rq_avg_poll_timer, cpu),
706 CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
707 per_cpu(rq_avg_poll_timer, cpu).function
708 = msm_mpd_rq_avg_poll_timer;
709 }
710 mutex_init(&msm_mpd.lock);
711 init_waitqueue_head(&msm_mpd.wait_q);
712 init_waitqueue_head(&msm_mpd.wait_hpq);
713 return platform_driver_register(&msm_mpd_driver);
714}
715late_initcall(msm_mpdecision_init);