blob: 63e9c709760ca5bcc4968e3b6d75d3e2b798c0b8 [file] [log] [blame]
Abhijeet Dharmapurikar7b933c52012-08-23 15:51:58 -07001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -07002 *
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
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/mutex.h>
18#include <linux/kthread.h>
19#include <linux/kobject.h>
20#include <linux/ktime.h>
21#include <linux/hrtimer.h>
22#include <linux/slab.h>
23#include <linux/spinlock.h>
24#include <linux/stringify.h>
25#include <linux/debugfs.h>
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -070026#include <linux/msm_tsens.h>
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070027#include <asm/atomic.h>
28#include <asm/page.h>
29#include <mach/msm_dcvs.h>
30
31#define CORE_HANDLE_OFFSET (0xA0)
32#define __err(f, ...) pr_err("MSM_DCVS: %s: " f, __func__, __VA_ARGS__)
33#define __info(f, ...) pr_info("MSM_DCVS: %s: " f, __func__, __VA_ARGS__)
34#define MAX_PENDING (5)
35
36enum {
37 MSM_DCVS_DEBUG_NOTIFIER = BIT(0),
38 MSM_DCVS_DEBUG_IDLE_PULSE = BIT(1),
39 MSM_DCVS_DEBUG_FREQ_CHANGE = BIT(2),
40};
41
42struct core_attribs {
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -070043 struct kobj_attribute core_id;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070044 struct kobj_attribute idle_enabled;
45 struct kobj_attribute freq_change_enabled;
46 struct kobj_attribute actual_freq;
47 struct kobj_attribute freq_change_us;
48
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070049 struct kobj_attribute disable_pc_threshold;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -070050 struct kobj_attribute em_win_size_min_us;
51 struct kobj_attribute em_win_size_max_us;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070052 struct kobj_attribute em_max_util_pct;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -070053 struct kobj_attribute group_id;
54 struct kobj_attribute max_freq_chg_time_us;
55 struct kobj_attribute slack_mode_dynamic;
56 struct kobj_attribute slack_time_min_us;
57 struct kobj_attribute slack_time_max_us;
58 struct kobj_attribute slack_weight_thresh_pct;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070059 struct kobj_attribute ss_iobusy_conv;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -070060 struct kobj_attribute ss_win_size_min_us;
61 struct kobj_attribute ss_win_size_max_us;
62 struct kobj_attribute ss_util_pct;
63
64 struct kobj_attribute active_coeff_a;
65 struct kobj_attribute active_coeff_b;
66 struct kobj_attribute active_coeff_c;
67 struct kobj_attribute leakage_coeff_a;
68 struct kobj_attribute leakage_coeff_b;
69 struct kobj_attribute leakage_coeff_c;
70 struct kobj_attribute leakage_coeff_d;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070071
72 struct attribute_group attrib_group;
73};
74
75struct dcvs_core {
76 char core_name[CORE_NAME_MAX];
77 uint32_t new_freq[MAX_PENDING];
78 uint32_t actual_freq;
79 uint32_t freq_change_us;
80
81 uint32_t max_time_us; /* core param */
82
83 struct msm_dcvs_algo_param algo_param;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -070084 struct msm_dcvs_energy_curve_coeffs coeffs;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070085 struct msm_dcvs_idle *idle_driver;
86 struct msm_dcvs_freq *freq_driver;
87
88 /* private */
89 int64_t time_start;
90 struct mutex lock;
91 spinlock_t cpu_lock;
92 struct task_struct *task;
93 struct core_attribs attrib;
94 uint32_t handle;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070095 uint32_t freq_pending;
96 struct hrtimer timer;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -070097 int32_t timer_disabled;
Eugene Seah76af9832012-03-28 18:43:53 -060098 /* track if kthread for change_freq is active */
99 int32_t change_freq_activated;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700100 struct msm_dcvs_core_info *info;
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700101 int sensor;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700102};
103
104static int msm_dcvs_debug;
105static int msm_dcvs_enabled = 1;
106module_param_named(enable, msm_dcvs_enabled, int, S_IRUGO | S_IWUSR | S_IWGRP);
107
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700108static struct dentry *debugfs_base;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700109
110static struct dcvs_core core_list[CORES_MAX];
111static DEFINE_MUTEX(core_list_lock);
112
113static struct kobject *cores_kobj;
114static struct dcvs_core *core_handles[CORES_MAX];
115
116/* Change core frequency, called with core mutex locked */
117static int __msm_dcvs_change_freq(struct dcvs_core *core)
118{
119 int ret = 0;
120 unsigned long flags = 0;
121 unsigned int requested_freq = 0;
122 unsigned int prev_freq = 0;
123 int64_t time_start = 0;
124 int64_t time_end = 0;
125 uint32_t slack_us = 0;
126 uint32_t ret1 = 0;
127
128 if (!core->freq_driver || !core->freq_driver->set_frequency) {
129 /* Core may have unregistered or hotplugged */
130 return -ENODEV;
131 }
132repeat:
133 spin_lock_irqsave(&core->cpu_lock, flags);
134 if (unlikely(!core->freq_pending)) {
135 spin_unlock_irqrestore(&core->cpu_lock, flags);
136 return ret;
137 }
138 requested_freq = core->new_freq[core->freq_pending - 1];
139 if (unlikely(core->freq_pending > 1) &&
140 (msm_dcvs_debug & MSM_DCVS_DEBUG_FREQ_CHANGE)) {
141 int i;
142 for (i = 0; i < core->freq_pending - 1; i++) {
143 __info("Core %s missing freq %u\n",
144 core->core_name, core->new_freq[i]);
145 }
146 }
147 time_start = core->time_start;
148 core->time_start = 0;
149 core->freq_pending = 0;
150 /**
151 * Cancel the timers, we dont want the timer firing as we are
152 * changing the clock rate. Dont let idle_exit and others setup
153 * timers as well.
154 */
155 hrtimer_cancel(&core->timer);
156 core->timer_disabled = 1;
157 spin_unlock_irqrestore(&core->cpu_lock, flags);
158
159 if (requested_freq == core->actual_freq)
160 return ret;
161
162 /**
163 * Call the frequency sink driver to change the frequency
164 * We will need to get back the actual frequency in KHz and
165 * the record the time taken to change it.
166 */
167 ret = core->freq_driver->set_frequency(core->freq_driver,
168 requested_freq);
169 if (ret <= 0) {
170 __err("Core %s failed to set freq %u\n",
171 core->core_name, requested_freq);
Eugene Seah76af9832012-03-28 18:43:53 -0600172 /* continue to call TZ to get updated slack timer */
173 } else {
174 prev_freq = core->actual_freq;
175 core->actual_freq = ret;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700176 }
177
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700178 time_end = ktime_to_ns(ktime_get());
179 if (msm_dcvs_debug & MSM_DCVS_DEBUG_FREQ_CHANGE)
180 __info("Core %s Time end %llu Time start: %llu\n",
181 core->core_name, time_end, time_start);
182 time_end -= time_start;
183 do_div(time_end, NSEC_PER_USEC);
184 core->freq_change_us = (uint32_t)time_end;
185
186 /**
187 * Disable low power modes if the actual frequency is >
188 * disable_pc_threshold.
189 */
190 if (core->actual_freq >
191 core->algo_param.disable_pc_threshold) {
192 core->idle_driver->enable(core->idle_driver,
193 MSM_DCVS_DISABLE_HIGH_LATENCY_MODES);
194 if (msm_dcvs_debug & MSM_DCVS_DEBUG_IDLE_PULSE)
195 __info("Disabling LPM for %s\n", core->core_name);
196 } else if (core->actual_freq <=
197 core->algo_param.disable_pc_threshold) {
198 core->idle_driver->enable(core->idle_driver,
199 MSM_DCVS_ENABLE_HIGH_LATENCY_MODES);
200 if (msm_dcvs_debug & MSM_DCVS_DEBUG_IDLE_PULSE)
201 __info("Enabling LPM for %s\n", core->core_name);
202 }
203
204 /**
205 * Update algorithm with new freq and time taken to change
206 * to this frequency and that will get us the new slack
207 * timer
208 */
209 ret = msm_dcvs_scm_event(core->handle, MSM_DCVS_SCM_CLOCK_FREQ_UPDATE,
210 core->actual_freq, (uint32_t)time_end, &slack_us, &ret1);
211 if (!ret) {
212 /* Reset the slack timer */
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700213 if (slack_us) {
214 core->timer_disabled = 0;
215 ret = hrtimer_start(&core->timer,
216 ktime_set(0, slack_us * 1000),
217 HRTIMER_MODE_REL_PINNED);
218 if (ret)
219 __err("Failed to register timer for core %s\n",
220 core->core_name);
221 }
222 } else {
223 __err("Error sending core (%s) freq change (%u)\n",
224 core->core_name, core->actual_freq);
225 }
226
227 if (msm_dcvs_debug & MSM_DCVS_DEBUG_FREQ_CHANGE)
228 __info("Freq %u requested for core %s (actual %u prev %u) "
229 "change time %u us slack time %u us\n",
230 requested_freq, core->core_name,
231 core->actual_freq, prev_freq,
Eugene Seah76af9832012-03-28 18:43:53 -0600232 core->freq_change_us, slack_us);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700233
234 /**
235 * By the time we are done with freq changes, we could be asked to
236 * change again. Check before exiting.
237 */
238 if (core->freq_pending)
239 goto repeat;
240
Eugene Seah76af9832012-03-28 18:43:53 -0600241 core->change_freq_activated = 0;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700242 return ret;
243}
244
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700245static int __msm_dcvs_report_temp(struct dcvs_core *core)
246{
247 struct msm_dcvs_core_info *info = core->info;
248 struct tsens_device tsens_dev;
249 int ret;
250 unsigned long temp = 0;
251
252 tsens_dev.sensor_num = core->sensor;
253 ret = tsens_get_temp(&tsens_dev, &temp);
254 if (!ret) {
255 tsens_dev.sensor_num = 0;
256 ret = tsens_get_temp(&tsens_dev, &temp);
257 if (!ret)
258 return -ENODEV;
259 }
260
261 ret = msm_dcvs_scm_set_power_params(core->handle, &info->power_param,
262 &info->freq_tbl[0], &core->coeffs);
263 return ret;
264}
265
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700266static int msm_dcvs_do_freq(void *data)
267{
268 struct dcvs_core *core = (struct dcvs_core *)data;
269 static struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1};
270
271 sched_setscheduler(current, SCHED_FIFO, &param);
272 set_current_state(TASK_UNINTERRUPTIBLE);
273
274 while (!kthread_should_stop()) {
275 mutex_lock(&core->lock);
276 __msm_dcvs_change_freq(core);
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700277 __msm_dcvs_report_temp(core);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700278 mutex_unlock(&core->lock);
279
280 schedule();
281
282 if (kthread_should_stop())
283 break;
284
285 set_current_state(TASK_UNINTERRUPTIBLE);
286 }
287
288 __set_current_state(TASK_RUNNING);
289
290 return 0;
291}
292
293static int msm_dcvs_update_freq(struct dcvs_core *core,
Eugene Seah76af9832012-03-28 18:43:53 -0600294 enum msm_dcvs_scm_event event, uint32_t param0,
295 uint32_t *ret1, int *freq_changed)
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700296{
297 int ret = 0;
298 unsigned long flags = 0;
299 uint32_t new_freq = 0;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700300
301 spin_lock_irqsave(&core->cpu_lock, flags);
302 ret = msm_dcvs_scm_event(core->handle, event, param0,
Eugene Seah76af9832012-03-28 18:43:53 -0600303 core->actual_freq, &new_freq, ret1);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700304 if (ret) {
Jeff Ohlstein4aa4a2b2012-06-28 19:03:49 -0700305 if (ret == -13)
306 ret = 0;
307 else
308 __err("Error (%d) sending SCM event %d for core %s\n",
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700309 ret, event, core->core_name);
310 goto freq_done;
311 }
312
313 if ((core->actual_freq != new_freq) &&
314 (core->new_freq[core->freq_pending] != new_freq)) {
315 if (core->freq_pending >= MAX_PENDING - 1)
316 core->freq_pending = MAX_PENDING - 1;
317 core->new_freq[core->freq_pending++] = new_freq;
318 core->time_start = ktime_to_ns(ktime_get());
319
320 /* Schedule the frequency change */
321 if (!core->task)
322 __err("Uninitialized task for core %s\n",
323 core->core_name);
Eugene Seah76af9832012-03-28 18:43:53 -0600324 else {
325 if (freq_changed)
326 *freq_changed = 1;
327 core->change_freq_activated = 1;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700328 wake_up_process(core->task);
Eugene Seah76af9832012-03-28 18:43:53 -0600329 }
330 } else {
331 if (freq_changed)
332 *freq_changed = 0;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700333 }
334freq_done:
335 spin_unlock_irqrestore(&core->cpu_lock, flags);
336
337 return ret;
338}
339
340static enum hrtimer_restart msm_dcvs_core_slack_timer(struct hrtimer *timer)
341{
342 int ret = 0;
343 struct dcvs_core *core = container_of(timer, struct dcvs_core, timer);
Eugene Seah76af9832012-03-28 18:43:53 -0600344 uint32_t ret1;
345 uint32_t ret2;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700346
347 if (msm_dcvs_debug & MSM_DCVS_DEBUG_FREQ_CHANGE)
348 __info("Slack timer fired for core %s\n", core->core_name);
349
350 /**
351 * Timer expired, notify TZ
352 * Dont care about the third arg.
353 */
Eugene Seah76af9832012-03-28 18:43:53 -0600354 ret = msm_dcvs_update_freq(core, MSM_DCVS_SCM_QOS_TIMER_EXPIRED, 0,
355 &ret1, &ret2);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700356 if (ret)
357 __err("Timer expired for core %s but failed to notify.\n",
358 core->core_name);
359
360 return HRTIMER_NORESTART;
361}
362
363/* Helper functions and macros for sysfs nodes for a core */
364#define CORE_FROM_ATTRIBS(attr, name) \
365 container_of(container_of(attr, struct core_attribs, name), \
366 struct dcvs_core, attrib);
367
368#define DCVS_PARAM_SHOW(_name, v) \
369static ssize_t msm_dcvs_attr_##_name##_show(struct kobject *kobj, \
370 struct kobj_attribute *attr, char *buf) \
371{ \
372 struct dcvs_core *core = CORE_FROM_ATTRIBS(attr, _name); \
373 return snprintf(buf, PAGE_SIZE, "%d\n", v); \
374}
375
376#define DCVS_ALGO_PARAM(_name) \
377static ssize_t msm_dcvs_attr_##_name##_show(struct kobject *kobj,\
378 struct kobj_attribute *attr, char *buf) \
379{ \
380 struct dcvs_core *core = CORE_FROM_ATTRIBS(attr, _name); \
381 return snprintf(buf, PAGE_SIZE, "%d\n", core->algo_param._name); \
382} \
383static ssize_t msm_dcvs_attr_##_name##_store(struct kobject *kobj, \
384 struct kobj_attribute *attr, const char *buf, size_t count) \
385{ \
386 int ret = 0; \
387 uint32_t val = 0; \
388 struct dcvs_core *core = CORE_FROM_ATTRIBS(attr, _name); \
389 mutex_lock(&core->lock); \
390 ret = kstrtouint(buf, 10, &val); \
391 if (ret) { \
392 __err("Invalid input %s for %s\n", buf, __stringify(_name));\
393 } else { \
394 uint32_t old_val = core->algo_param._name; \
395 core->algo_param._name = val; \
396 ret = msm_dcvs_scm_set_algo_params(core->handle, \
397 &core->algo_param); \
398 if (ret) { \
399 core->algo_param._name = old_val; \
400 __err("Error(%d) in setting %d for algo param %s\n",\
401 ret, val, __stringify(_name)); \
402 } \
403 } \
404 mutex_unlock(&core->lock); \
405 return count; \
406}
407
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700408#define DCVS_ENERGY_PARAM(_name) \
409static ssize_t msm_dcvs_attr_##_name##_show(struct kobject *kobj,\
410 struct kobj_attribute *attr, char *buf) \
411{ \
412 struct dcvs_core *core = CORE_FROM_ATTRIBS(attr, _name); \
413 return snprintf(buf, PAGE_SIZE, "%d\n", core->coeffs._name); \
414} \
415static ssize_t msm_dcvs_attr_##_name##_store(struct kobject *kobj, \
416 struct kobj_attribute *attr, const char *buf, size_t count) \
417{ \
418 int ret = 0; \
419 int32_t val = 0; \
420 struct dcvs_core *core = CORE_FROM_ATTRIBS(attr, _name); \
421 mutex_lock(&core->lock); \
422 ret = kstrtoint(buf, 10, &val); \
423 if (ret) { \
424 __err("Invalid input %s for %s\n", buf, __stringify(_name));\
425 } else { \
426 int32_t old_val = core->coeffs._name; \
427 core->coeffs._name = val; \
428 ret = msm_dcvs_scm_set_power_params(core->handle, \
429 &core->info->power_param, &core->info->freq_tbl[0], \
430 &core->coeffs); \
431 if (ret) { \
432 core->coeffs._name = old_val; \
433 __err("Error(%d) in setting %d for coeffs param %s\n",\
434 ret, val, __stringify(_name)); \
435 } \
436 } \
437 mutex_unlock(&core->lock); \
438 return count; \
439}
440
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700441#define DCVS_RO_ATTRIB(i, _name) \
442 core->attrib._name.attr.name = __stringify(_name); \
443 core->attrib._name.attr.mode = S_IRUGO; \
444 core->attrib._name.show = msm_dcvs_attr_##_name##_show; \
445 core->attrib._name.store = NULL; \
446 core->attrib.attrib_group.attrs[i] = &core->attrib._name.attr;
447
448#define DCVS_RW_ATTRIB(i, _name) \
449 core->attrib._name.attr.name = __stringify(_name); \
450 core->attrib._name.attr.mode = S_IRUGO | S_IWUSR; \
451 core->attrib._name.show = msm_dcvs_attr_##_name##_show; \
452 core->attrib._name.store = msm_dcvs_attr_##_name##_store; \
453 core->attrib.attrib_group.attrs[i] = &core->attrib._name.attr;
454
455/**
456 * Function declarations for different attributes.
457 * Gets used when setting the attribute show and store parameters.
458 */
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700459DCVS_PARAM_SHOW(core_id, core->handle)
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700460DCVS_PARAM_SHOW(idle_enabled, (core->idle_driver != NULL))
461DCVS_PARAM_SHOW(freq_change_enabled, (core->freq_driver != NULL))
462DCVS_PARAM_SHOW(actual_freq, (core->actual_freq))
463DCVS_PARAM_SHOW(freq_change_us, (core->freq_change_us))
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700464
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700465DCVS_ALGO_PARAM(disable_pc_threshold)
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700466DCVS_ALGO_PARAM(em_win_size_min_us)
467DCVS_ALGO_PARAM(em_win_size_max_us)
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700468DCVS_ALGO_PARAM(em_max_util_pct)
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700469DCVS_ALGO_PARAM(group_id)
470DCVS_ALGO_PARAM(max_freq_chg_time_us)
471DCVS_ALGO_PARAM(slack_mode_dynamic)
472DCVS_ALGO_PARAM(slack_time_min_us)
473DCVS_ALGO_PARAM(slack_time_max_us)
474DCVS_ALGO_PARAM(slack_weight_thresh_pct)
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700475DCVS_ALGO_PARAM(ss_iobusy_conv)
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700476DCVS_ALGO_PARAM(ss_win_size_min_us)
477DCVS_ALGO_PARAM(ss_win_size_max_us)
478DCVS_ALGO_PARAM(ss_util_pct)
479
480DCVS_ENERGY_PARAM(active_coeff_a)
481DCVS_ENERGY_PARAM(active_coeff_b)
482DCVS_ENERGY_PARAM(active_coeff_c)
483DCVS_ENERGY_PARAM(leakage_coeff_a)
484DCVS_ENERGY_PARAM(leakage_coeff_b)
485DCVS_ENERGY_PARAM(leakage_coeff_c)
486DCVS_ENERGY_PARAM(leakage_coeff_d)
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700487
488static int msm_dcvs_setup_core_sysfs(struct dcvs_core *core)
489{
490 int ret = 0;
491 struct kobject *core_kobj = NULL;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700492 const int attr_count = 27;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700493
494 BUG_ON(!cores_kobj);
495
496 core->attrib.attrib_group.attrs =
497 kzalloc(attr_count * sizeof(struct attribute *), GFP_KERNEL);
498
499 if (!core->attrib.attrib_group.attrs) {
500 ret = -ENOMEM;
501 goto done;
502 }
503
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700504
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700505 DCVS_RO_ATTRIB(0, core_id);
506 DCVS_RO_ATTRIB(1, idle_enabled);
507 DCVS_RO_ATTRIB(2, freq_change_enabled);
508 DCVS_RO_ATTRIB(3, actual_freq);
509 DCVS_RO_ATTRIB(4, freq_change_us);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700510
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700511 DCVS_RW_ATTRIB(5, disable_pc_threshold);
512 DCVS_RW_ATTRIB(6, em_win_size_min_us);
513 DCVS_RW_ATTRIB(7, em_win_size_max_us);
514 DCVS_RW_ATTRIB(8, em_max_util_pct);
515 DCVS_RW_ATTRIB(9, group_id);
516 DCVS_RW_ATTRIB(10, max_freq_chg_time_us);
517 DCVS_RW_ATTRIB(11, slack_mode_dynamic);
518 DCVS_RW_ATTRIB(12, slack_time_min_us);
519 DCVS_RW_ATTRIB(13, slack_time_max_us);
520 DCVS_RW_ATTRIB(14, slack_weight_thresh_pct);
521 DCVS_RW_ATTRIB(15, ss_iobusy_conv);
522 DCVS_RW_ATTRIB(16, ss_win_size_min_us);
523 DCVS_RW_ATTRIB(17, ss_win_size_max_us);
524 DCVS_RW_ATTRIB(18, ss_util_pct);
525
526 DCVS_RW_ATTRIB(19, active_coeff_a);
527 DCVS_RW_ATTRIB(20, active_coeff_b);
528 DCVS_RW_ATTRIB(21, active_coeff_c);
529 DCVS_RW_ATTRIB(22, leakage_coeff_a);
530 DCVS_RW_ATTRIB(23, leakage_coeff_b);
531 DCVS_RW_ATTRIB(24, leakage_coeff_c);
532 DCVS_RW_ATTRIB(25, leakage_coeff_d);
533
534 core->attrib.attrib_group.attrs[26] = NULL;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700535
536 core_kobj = kobject_create_and_add(core->core_name, cores_kobj);
537 if (!core_kobj) {
538 ret = -ENOMEM;
539 goto done;
540 }
541
542 ret = sysfs_create_group(core_kobj, &core->attrib.attrib_group);
543 if (ret)
544 __err("Cannot create core %s attr group\n", core->core_name);
545 else if (msm_dcvs_debug & MSM_DCVS_DEBUG_NOTIFIER)
546 __info("Setting up attributes for core %s\n", core->core_name);
547
548done:
549 if (ret) {
550 kfree(core->attrib.attrib_group.attrs);
551 kobject_del(core_kobj);
552 }
553
554 return ret;
555}
556
557/* Return the core if found or add to list if @add_to_list is true */
558static struct dcvs_core *msm_dcvs_get_core(const char *name, int add_to_list)
559{
560 struct dcvs_core *core = NULL;
561 int i;
562 int empty = -1;
563
564 if (!name[0] ||
565 (strnlen(name, CORE_NAME_MAX - 1) == CORE_NAME_MAX - 1))
566 return core;
567
568 mutex_lock(&core_list_lock);
569 for (i = 0; i < CORES_MAX; i++) {
570 core = &core_list[i];
571 if ((empty < 0) && !core->core_name[0]) {
572 empty = i;
573 continue;
574 }
575 if (!strncmp(name, core->core_name, CORE_NAME_MAX))
576 break;
577 }
578
579 /* Check for core_list full */
580 if ((i == CORES_MAX) && (empty < 0)) {
581 mutex_unlock(&core_list_lock);
582 return NULL;
583 }
584
585 if (i == CORES_MAX && add_to_list) {
586 core = &core_list[empty];
587 strlcpy(core->core_name, name, CORE_NAME_MAX);
588 mutex_init(&core->lock);
589 spin_lock_init(&core->cpu_lock);
590 core->handle = empty + CORE_HANDLE_OFFSET;
591 hrtimer_init(&core->timer,
592 CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
593 core->timer.function = msm_dcvs_core_slack_timer;
594 }
595 mutex_unlock(&core_list_lock);
596
597 return core;
598}
599
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700600int msm_dcvs_register_core(const char *core_name,
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700601 struct msm_dcvs_core_info *info, int sensor)
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700602{
603 int ret = -EINVAL;
604 struct dcvs_core *core = NULL;
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700605 uint32_t ret1;
606 uint32_t ret2;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700607
608 if (!core_name || !core_name[0])
609 return ret;
610
611 core = msm_dcvs_get_core(core_name, true);
612 if (!core)
613 return ret;
614
615 mutex_lock(&core->lock);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700616
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700617 core->info = info;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700618 memcpy(&core->algo_param, &info->algo_param,
619 sizeof(struct msm_dcvs_algo_param));
620
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700621 memcpy(&core->coeffs, &info->energy_coeffs,
622 sizeof(struct msm_dcvs_energy_curve_coeffs));
623
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700624 pr_debug("registering core with sensor %d\n", sensor);
625 core->sensor = sensor;
626 ret = msm_dcvs_scm_register_core(core->handle,
627 &info->core_param);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700628 if (ret)
629 goto bail;
630
631 ret = msm_dcvs_scm_set_algo_params(core->handle, &info->algo_param);
632 if (ret)
633 goto bail;
634
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -0700635 ret = msm_dcvs_scm_set_power_params(core->handle, &info->power_param,
636 &info->freq_tbl[0], &core->coeffs);
637 if (ret)
638 goto bail;
639
640 ret = msm_dcvs_scm_event(core->handle, MSM_DCVS_SCM_CORE_ONLINE,
641 core->actual_freq, 0, &ret1, &ret2);
642 if (ret)
643 goto bail;
644
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700645 ret = msm_dcvs_setup_core_sysfs(core);
646 if (ret) {
647 __err("Unable to setup core %s sysfs\n", core->core_name);
648 core_handles[core->handle - CORE_HANDLE_OFFSET] = NULL;
649 goto bail;
650 }
651
Abhijeet Dharmapurikar74f10832012-08-26 22:40:28 -0700652 core->task = kthread_create(msm_dcvs_do_freq, (void *)core,
653 "msm_dcvs/%d", core->handle);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700654bail:
655 mutex_unlock(&core->lock);
656 return ret;
657}
658EXPORT_SYMBOL(msm_dcvs_register_core);
659
660int msm_dcvs_freq_sink_register(struct msm_dcvs_freq *drv)
661{
662 int ret = -EINVAL;
663 struct dcvs_core *core = NULL;
Eugene Seah76af9832012-03-28 18:43:53 -0600664 uint32_t ret1;
665 uint32_t ret2;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700666
667 if (!drv || !drv->core_name)
668 return ret;
669
670 core = msm_dcvs_get_core(drv->core_name, true);
671 if (!core)
672 return ret;
673
674 mutex_lock(&core->lock);
675 if (core->freq_driver && (msm_dcvs_debug & MSM_DCVS_DEBUG_NOTIFIER))
676 __info("Frequency notifier for %s being replaced\n",
677 core->core_name);
678 core->freq_driver = drv;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700679 if (IS_ERR(core->task)) {
680 mutex_unlock(&core->lock);
681 return -EFAULT;
682 }
683
684 if (msm_dcvs_debug & MSM_DCVS_DEBUG_IDLE_PULSE)
685 __info("Enabling idle pulse for %s\n", core->core_name);
686
687 if (core->idle_driver) {
688 core->actual_freq = core->freq_driver->get_frequency(drv);
689 /* Notify TZ to start receiving idle info for the core */
Abhijeet Dharmapurikar7b933c52012-08-23 15:51:58 -0700690 ret = msm_dcvs_update_freq(core, MSM_DCVS_SCM_DCVS_ENABLE, 1,
Eugene Seah76af9832012-03-28 18:43:53 -0600691 &ret1, &ret2);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700692 core->idle_driver->enable(core->idle_driver,
693 MSM_DCVS_ENABLE_IDLE_PULSE);
694 }
695
696 mutex_unlock(&core->lock);
697
698 return core->handle;
699}
700EXPORT_SYMBOL(msm_dcvs_freq_sink_register);
701
702int msm_dcvs_freq_sink_unregister(struct msm_dcvs_freq *drv)
703{
704 int ret = -EINVAL;
705 struct dcvs_core *core = NULL;
Eugene Seah76af9832012-03-28 18:43:53 -0600706 uint32_t ret1;
707 uint32_t ret2;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700708
709 if (!drv || !drv->core_name)
710 return ret;
711
712 core = msm_dcvs_get_core(drv->core_name, false);
713 if (!core)
714 return ret;
715
716 mutex_lock(&core->lock);
717 if (msm_dcvs_debug & MSM_DCVS_DEBUG_IDLE_PULSE)
718 __info("Disabling idle pulse for %s\n", core->core_name);
719 if (core->idle_driver) {
720 core->idle_driver->enable(core->idle_driver,
721 MSM_DCVS_DISABLE_IDLE_PULSE);
722 /* Notify TZ to stop receiving idle info for the core */
Abhijeet Dharmapurikar7b933c52012-08-23 15:51:58 -0700723 ret = msm_dcvs_update_freq(core, MSM_DCVS_SCM_DCVS_ENABLE, 0,
Eugene Seah76af9832012-03-28 18:43:53 -0600724 &ret1, &ret2);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700725 hrtimer_cancel(&core->timer);
726 core->idle_driver->enable(core->idle_driver,
727 MSM_DCVS_ENABLE_HIGH_LATENCY_MODES);
728 if (msm_dcvs_debug & MSM_DCVS_DEBUG_IDLE_PULSE)
729 __info("Enabling LPM for %s\n", core->core_name);
730 }
731 core->freq_pending = 0;
732 core->freq_driver = NULL;
733 mutex_unlock(&core->lock);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700734
735 return 0;
736}
737EXPORT_SYMBOL(msm_dcvs_freq_sink_unregister);
738
739int msm_dcvs_idle_source_register(struct msm_dcvs_idle *drv)
740{
741 int ret = -EINVAL;
742 struct dcvs_core *core = NULL;
743
744 if (!drv || !drv->core_name)
745 return ret;
746
747 core = msm_dcvs_get_core(drv->core_name, true);
748 if (!core)
749 return ret;
750
751 mutex_lock(&core->lock);
752 if (core->idle_driver && (msm_dcvs_debug & MSM_DCVS_DEBUG_NOTIFIER))
753 __info("Idle notifier for %s being replaced\n",
754 core->core_name);
755 core->idle_driver = drv;
756 mutex_unlock(&core->lock);
757
758 return core->handle;
759}
760EXPORT_SYMBOL(msm_dcvs_idle_source_register);
761
762int msm_dcvs_idle_source_unregister(struct msm_dcvs_idle *drv)
763{
764 int ret = -EINVAL;
765 struct dcvs_core *core = NULL;
766
767 if (!drv || !drv->core_name)
768 return ret;
769
770 core = msm_dcvs_get_core(drv->core_name, false);
771 if (!core)
772 return ret;
773
774 mutex_lock(&core->lock);
775 core->idle_driver = NULL;
776 mutex_unlock(&core->lock);
777
778 return 0;
779}
780EXPORT_SYMBOL(msm_dcvs_idle_source_unregister);
781
782int msm_dcvs_idle(int handle, enum msm_core_idle_state state, uint32_t iowaited)
783{
784 int ret = 0;
785 struct dcvs_core *core = NULL;
Eugene Seah76af9832012-03-28 18:43:53 -0600786 uint32_t timer_interval_us = 0;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700787 uint32_t r0, r1;
Eugene Seah76af9832012-03-28 18:43:53 -0600788 uint32_t freq_changed = 0;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700789
790 if (handle >= CORE_HANDLE_OFFSET &&
791 (handle - CORE_HANDLE_OFFSET) < CORES_MAX)
792 core = &core_list[handle - CORE_HANDLE_OFFSET];
793
794 BUG_ON(!core);
795
796 if (msm_dcvs_debug & MSM_DCVS_DEBUG_IDLE_PULSE)
797 __info("Core %s idle state %d\n", core->core_name, state);
798
799 switch (state) {
800 case MSM_DCVS_IDLE_ENTER:
801 hrtimer_cancel(&core->timer);
802 ret = msm_dcvs_scm_event(core->handle,
803 MSM_DCVS_SCM_IDLE_ENTER, 0, 0, &r0, &r1);
804 if (ret)
805 __err("Error (%d) sending idle enter for %s\n",
806 ret, core->core_name);
807 break;
808
809 case MSM_DCVS_IDLE_EXIT:
810 hrtimer_cancel(&core->timer);
811 ret = msm_dcvs_update_freq(core, MSM_DCVS_SCM_IDLE_EXIT,
Eugene Seah76af9832012-03-28 18:43:53 -0600812 iowaited, &timer_interval_us, &freq_changed);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700813 if (ret)
814 __err("Error (%d) sending idle exit for %s\n",
815 ret, core->core_name);
Eugene Seah76af9832012-03-28 18:43:53 -0600816 /* only start slack timer if change_freq won't */
817 if (freq_changed || core->change_freq_activated)
818 break;
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700819 if (timer_interval_us && !core->timer_disabled) {
820 ret = hrtimer_start(&core->timer,
821 ktime_set(0, timer_interval_us * 1000),
822 HRTIMER_MODE_REL_PINNED);
Eugene Seah76af9832012-03-28 18:43:53 -0600823
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700824 if (ret)
825 __err("Failed to register timer for core %s\n",
Eugene Seah76af9832012-03-28 18:43:53 -0600826 core->core_name);
Praveen Chidambaramf53ef1b2011-12-06 08:27:49 -0700827 }
828 break;
829 }
830
831 return ret;
832}
833EXPORT_SYMBOL(msm_dcvs_idle);
834
835static int __init msm_dcvs_late_init(void)
836{
837 struct kobject *module_kobj = NULL;
838 int ret = 0;
839
840 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
841 if (!module_kobj) {
842 pr_err("%s: cannot find kobject for module %s\n",
843 __func__, KBUILD_MODNAME);
844 ret = -ENOENT;
845 goto err;
846 }
847
848 cores_kobj = kobject_create_and_add("cores", module_kobj);
849 if (!cores_kobj) {
850 __err("Cannot create %s kobject\n", "cores");
851 ret = -ENOMEM;
852 goto err;
853 }
854
855 debugfs_base = debugfs_create_dir("msm_dcvs", NULL);
856 if (!debugfs_base) {
857 __err("Cannot create debugfs base %s\n", "msm_dcvs");
858 ret = -ENOENT;
859 goto err;
860 }
861
862 if (!debugfs_create_u32("debug_mask", S_IRUGO | S_IWUSR,
863 debugfs_base, &msm_dcvs_debug)) {
864 __err("Cannot create debugfs entry %s\n", "debug_mask");
865 ret = -ENOMEM;
866 goto err;
867 }
868
869err:
870 if (ret) {
871 kobject_del(cores_kobj);
872 cores_kobj = NULL;
873 debugfs_remove(debugfs_base);
874 }
875
876 return ret;
877}
878late_initcall(msm_dcvs_late_init);
879
880static int __init msm_dcvs_early_init(void)
881{
882 int ret = 0;
883
884 if (!msm_dcvs_enabled) {
885 __info("Not enabled (%d)\n", msm_dcvs_enabled);
886 return 0;
887 }
888
889 ret = msm_dcvs_scm_init(10 * 1024);
890 if (ret)
891 __err("Unable to initialize DCVS err=%d\n", ret);
892
893 return ret;
894}
895postcore_initcall(msm_dcvs_early_init);