1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
4 * for Non-CPU Devices.
5 *
6 * Copyright (C) 2011 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/kmod.h>
12 #include <linux/sched.h>
13 #include <linux/debugfs.h>
14 #include <linux/devfreq_cooling.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/slab.h>
20 #include <linux/stat.h>
21 #include <linux/pm_opp.h>
22 #include <linux/devfreq.h>
23 #include <linux/workqueue.h>
24 #include <linux/platform_device.h>
25 #include <linux/list.h>
26 #include <linux/printk.h>
27 #include <linux/hrtimer.h>
28 #include <linux/of.h>
29 #include <linux/pm_qos.h>
30 #include <linux/units.h>
31 #include "governor.h"
32
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/devfreq.h>
35
36 #define IS_SUPPORTED_FLAG(f, name) ((f & DEVFREQ_GOV_FLAG_##name) ? true : false)
37 #define IS_SUPPORTED_ATTR(f, name) ((f & DEVFREQ_GOV_ATTR_##name) ? true : false)
38
39 static struct class *devfreq_class;
40 static struct dentry *devfreq_debugfs;
41
42 /*
43 * devfreq core provides delayed work based load monitoring helper
44 * functions. Governors can use these or can implement their own
45 * monitoring mechanism.
46 */
47 static struct workqueue_struct *devfreq_wq;
48
49 /* The list of all device-devfreq governors */
50 static LIST_HEAD(devfreq_governor_list);
51 /* The list of all device-devfreq */
52 static LIST_HEAD(devfreq_list);
53 static DEFINE_MUTEX(devfreq_list_lock);
54
55 static const char timer_name[][DEVFREQ_NAME_LEN] = {
56 [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" },
57 [DEVFREQ_TIMER_DELAYED] = { "delayed" },
58 };
59
60 /**
61 * find_device_devfreq() - find devfreq struct using device pointer
62 * @dev: device pointer used to lookup device devfreq.
63 *
64 * Search the list of device devfreqs and return the matched device's
65 * devfreq info. devfreq_list_lock should be held by the caller.
66 */
find_device_devfreq(struct device * dev)67 static struct devfreq *find_device_devfreq(struct device *dev)
68 {
69 struct devfreq *tmp_devfreq;
70
71 lockdep_assert_held(&devfreq_list_lock);
72
73 if (IS_ERR_OR_NULL(dev)) {
74 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
75 return ERR_PTR(-EINVAL);
76 }
77
78 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
79 if (tmp_devfreq->dev.parent == dev)
80 return tmp_devfreq;
81 }
82
83 return ERR_PTR(-ENODEV);
84 }
85
find_available_min_freq(struct devfreq * devfreq)86 static unsigned long find_available_min_freq(struct devfreq *devfreq)
87 {
88 struct dev_pm_opp *opp;
89 unsigned long min_freq = 0;
90
91 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
92 if (IS_ERR(opp))
93 min_freq = 0;
94 else
95 dev_pm_opp_put(opp);
96
97 return min_freq;
98 }
99
find_available_max_freq(struct devfreq * devfreq)100 static unsigned long find_available_max_freq(struct devfreq *devfreq)
101 {
102 struct dev_pm_opp *opp;
103 unsigned long max_freq = ULONG_MAX;
104
105 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
106 if (IS_ERR(opp))
107 max_freq = 0;
108 else
109 dev_pm_opp_put(opp);
110
111 return max_freq;
112 }
113
114 /**
115 * get_freq_range() - Get the current freq range
116 * @devfreq: the devfreq instance
117 * @min_freq: the min frequency
118 * @max_freq: the max frequency
119 *
120 * This takes into consideration all constraints.
121 */
get_freq_range(struct devfreq * devfreq,unsigned long * min_freq,unsigned long * max_freq)122 static void get_freq_range(struct devfreq *devfreq,
123 unsigned long *min_freq,
124 unsigned long *max_freq)
125 {
126 unsigned long *freq_table = devfreq->profile->freq_table;
127 s32 qos_min_freq, qos_max_freq;
128
129 lockdep_assert_held(&devfreq->lock);
130
131 /*
132 * Initialize minimum/maximum frequency from freq table.
133 * The devfreq drivers can initialize this in either ascending or
134 * descending order and devfreq core supports both.
135 */
136 if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) {
137 *min_freq = freq_table[0];
138 *max_freq = freq_table[devfreq->profile->max_state - 1];
139 } else {
140 *min_freq = freq_table[devfreq->profile->max_state - 1];
141 *max_freq = freq_table[0];
142 }
143
144 /* Apply constraints from PM QoS */
145 qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
146 DEV_PM_QOS_MIN_FREQUENCY);
147 qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
148 DEV_PM_QOS_MAX_FREQUENCY);
149 *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq);
150 if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
151 *max_freq = min(*max_freq,
152 (unsigned long)HZ_PER_KHZ * qos_max_freq);
153
154 /* Apply constraints from OPP interface */
155 *min_freq = max(*min_freq, devfreq->scaling_min_freq);
156 *max_freq = min(*max_freq, devfreq->scaling_max_freq);
157
158 if (*min_freq > *max_freq)
159 *min_freq = *max_freq;
160 }
161
162 /**
163 * devfreq_get_freq_level() - Lookup freq_table for the frequency
164 * @devfreq: the devfreq instance
165 * @freq: the target frequency
166 */
devfreq_get_freq_level(struct devfreq * devfreq,unsigned long freq)167 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
168 {
169 int lev;
170
171 for (lev = 0; lev < devfreq->profile->max_state; lev++)
172 if (freq == devfreq->profile->freq_table[lev])
173 return lev;
174
175 return -EINVAL;
176 }
177
set_freq_table(struct devfreq * devfreq)178 static int set_freq_table(struct devfreq *devfreq)
179 {
180 struct devfreq_dev_profile *profile = devfreq->profile;
181 struct dev_pm_opp *opp;
182 unsigned long freq;
183 int i, count;
184
185 /* Initialize the freq_table from OPP table */
186 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
187 if (count <= 0)
188 return -EINVAL;
189
190 profile->max_state = count;
191 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
192 profile->max_state,
193 sizeof(*profile->freq_table),
194 GFP_KERNEL);
195 if (!profile->freq_table) {
196 profile->max_state = 0;
197 return -ENOMEM;
198 }
199
200 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
201 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
202 if (IS_ERR(opp)) {
203 devm_kfree(devfreq->dev.parent, profile->freq_table);
204 profile->max_state = 0;
205 return PTR_ERR(opp);
206 }
207 dev_pm_opp_put(opp);
208 profile->freq_table[i] = freq;
209 }
210
211 return 0;
212 }
213
214 /**
215 * devfreq_update_status() - Update statistics of devfreq behavior
216 * @devfreq: the devfreq instance
217 * @freq: the update target frequency
218 */
devfreq_update_status(struct devfreq * devfreq,unsigned long freq)219 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
220 {
221 int lev, prev_lev, ret = 0;
222 u64 cur_time;
223
224 lockdep_assert_held(&devfreq->lock);
225 cur_time = get_jiffies_64();
226
227 /* Immediately exit if previous_freq is not initialized yet. */
228 if (!devfreq->previous_freq)
229 goto out;
230
231 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
232 if (prev_lev < 0) {
233 ret = prev_lev;
234 goto out;
235 }
236
237 devfreq->stats.time_in_state[prev_lev] +=
238 cur_time - devfreq->stats.last_update;
239
240 lev = devfreq_get_freq_level(devfreq, freq);
241 if (lev < 0) {
242 ret = lev;
243 goto out;
244 }
245
246 if (lev != prev_lev) {
247 devfreq->stats.trans_table[
248 (prev_lev * devfreq->profile->max_state) + lev]++;
249 devfreq->stats.total_trans++;
250 }
251
252 out:
253 devfreq->stats.last_update = cur_time;
254 return ret;
255 }
256 EXPORT_SYMBOL(devfreq_update_status);
257
258 /**
259 * find_devfreq_governor() - find devfreq governor from name
260 * @name: name of the governor
261 *
262 * Search the list of devfreq governors and return the matched
263 * governor's pointer. devfreq_list_lock should be held by the caller.
264 */
find_devfreq_governor(const char * name)265 static struct devfreq_governor *find_devfreq_governor(const char *name)
266 {
267 struct devfreq_governor *tmp_governor;
268
269 lockdep_assert_held(&devfreq_list_lock);
270
271 if (IS_ERR_OR_NULL(name)) {
272 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
273 return ERR_PTR(-EINVAL);
274 }
275
276 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
277 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
278 return tmp_governor;
279 }
280
281 return ERR_PTR(-ENODEV);
282 }
283
284 /**
285 * try_then_request_governor() - Try to find the governor and request the
286 * module if is not found.
287 * @name: name of the governor
288 *
289 * Search the list of devfreq governors and request the module and try again
290 * if is not found. This can happen when both drivers (the governor driver
291 * and the driver that call devfreq_add_device) are built as modules.
292 * devfreq_list_lock should be held by the caller. Returns the matched
293 * governor's pointer or an error pointer.
294 */
try_then_request_governor(const char * name)295 static struct devfreq_governor *try_then_request_governor(const char *name)
296 {
297 struct devfreq_governor *governor;
298 int err = 0;
299
300 lockdep_assert_held(&devfreq_list_lock);
301
302 if (IS_ERR_OR_NULL(name)) {
303 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
304 return ERR_PTR(-EINVAL);
305 }
306
307 governor = find_devfreq_governor(name);
308 if (IS_ERR(governor)) {
309 mutex_unlock(&devfreq_list_lock);
310
311 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
312 DEVFREQ_NAME_LEN))
313 err = request_module("governor_%s", "simpleondemand");
314 else
315 err = request_module("governor_%s", name);
316 /* Restore previous state before return */
317 mutex_lock(&devfreq_list_lock);
318 if (err)
319 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
320
321 governor = find_devfreq_governor(name);
322 }
323
324 return governor;
325 }
326
devfreq_notify_transition(struct devfreq * devfreq,struct devfreq_freqs * freqs,unsigned int state)327 static int devfreq_notify_transition(struct devfreq *devfreq,
328 struct devfreq_freqs *freqs, unsigned int state)
329 {
330 if (!devfreq)
331 return -EINVAL;
332
333 switch (state) {
334 case DEVFREQ_PRECHANGE:
335 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
336 DEVFREQ_PRECHANGE, freqs);
337 break;
338
339 case DEVFREQ_POSTCHANGE:
340 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
341 DEVFREQ_POSTCHANGE, freqs);
342 break;
343 default:
344 return -EINVAL;
345 }
346
347 return 0;
348 }
349
devfreq_set_target(struct devfreq * devfreq,unsigned long new_freq,u32 flags)350 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
351 u32 flags)
352 {
353 struct devfreq_freqs freqs;
354 unsigned long cur_freq;
355 int err = 0;
356
357 if (devfreq->profile->get_cur_freq)
358 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
359 else
360 cur_freq = devfreq->previous_freq;
361
362 freqs.old = cur_freq;
363 freqs.new = new_freq;
364 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
365
366 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
367 if (err) {
368 freqs.new = cur_freq;
369 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
370 return err;
371 }
372
373 /*
374 * Print devfreq_frequency trace information between DEVFREQ_PRECHANGE
375 * and DEVFREQ_POSTCHANGE because for showing the correct frequency
376 * change order of between devfreq device and passive devfreq device.
377 */
378 if (trace_devfreq_frequency_enabled() && new_freq != cur_freq)
379 trace_devfreq_frequency(devfreq, new_freq, cur_freq);
380
381 freqs.new = new_freq;
382 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
383
384 if (devfreq_update_status(devfreq, new_freq))
385 dev_err(&devfreq->dev,
386 "Couldn't update frequency transition information.\n");
387
388 devfreq->previous_freq = new_freq;
389
390 if (devfreq->suspend_freq)
391 devfreq->resume_freq = new_freq;
392
393 return err;
394 }
395
396 /**
397 * devfreq_update_target() - Reevaluate the device and configure frequency
398 * on the final stage.
399 * @devfreq: the devfreq instance.
400 * @freq: the new frequency of parent device. This argument
401 * is only used for devfreq device using passive governor.
402 *
403 * Note: Lock devfreq->lock before calling devfreq_update_target. This function
404 * should be only used by both update_devfreq() and devfreq governors.
405 */
devfreq_update_target(struct devfreq * devfreq,unsigned long freq)406 int devfreq_update_target(struct devfreq *devfreq, unsigned long freq)
407 {
408 unsigned long min_freq, max_freq;
409 int err = 0;
410 u32 flags = 0;
411
412 lockdep_assert_held(&devfreq->lock);
413
414 if (!devfreq->governor)
415 return -EINVAL;
416
417 /* Reevaluate the proper frequency */
418 err = devfreq->governor->get_target_freq(devfreq, &freq);
419 if (err)
420 return err;
421 get_freq_range(devfreq, &min_freq, &max_freq);
422
423 if (freq < min_freq) {
424 freq = min_freq;
425 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
426 }
427 if (freq > max_freq) {
428 freq = max_freq;
429 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
430 }
431
432 return devfreq_set_target(devfreq, freq, flags);
433 }
434 EXPORT_SYMBOL(devfreq_update_target);
435
436 /* Load monitoring helper functions for governors use */
437
438 /**
439 * update_devfreq() - Reevaluate the device and configure frequency.
440 * @devfreq: the devfreq instance.
441 *
442 * Note: Lock devfreq->lock before calling update_devfreq
443 * This function is exported for governors.
444 */
update_devfreq(struct devfreq * devfreq)445 int update_devfreq(struct devfreq *devfreq)
446 {
447 return devfreq_update_target(devfreq, 0L);
448 }
449 EXPORT_SYMBOL(update_devfreq);
450
451 /**
452 * devfreq_monitor() - Periodically poll devfreq objects.
453 * @work: the work struct used to run devfreq_monitor periodically.
454 *
455 */
devfreq_monitor(struct work_struct * work)456 static void devfreq_monitor(struct work_struct *work)
457 {
458 int err;
459 struct devfreq *devfreq = container_of(work,
460 struct devfreq, work.work);
461
462 mutex_lock(&devfreq->lock);
463 err = update_devfreq(devfreq);
464 if (err)
465 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
466
467 if (devfreq->stop_polling)
468 goto out;
469
470 queue_delayed_work(devfreq_wq, &devfreq->work,
471 msecs_to_jiffies(devfreq->profile->polling_ms));
472
473 out:
474 mutex_unlock(&devfreq->lock);
475 trace_devfreq_monitor(devfreq);
476 }
477
478 /**
479 * devfreq_monitor_start() - Start load monitoring of devfreq instance
480 * @devfreq: the devfreq instance.
481 *
482 * Helper function for starting devfreq device load monitoring. By
483 * default delayed work based monitoring is supported. Function
484 * to be called from governor in response to DEVFREQ_GOV_START
485 * event when device is added to devfreq framework.
486 */
devfreq_monitor_start(struct devfreq * devfreq)487 void devfreq_monitor_start(struct devfreq *devfreq)
488 {
489 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
490 return;
491
492 mutex_lock(&devfreq->lock);
493 if (delayed_work_pending(&devfreq->work))
494 goto out;
495
496 switch (devfreq->profile->timer) {
497 case DEVFREQ_TIMER_DEFERRABLE:
498 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
499 break;
500 case DEVFREQ_TIMER_DELAYED:
501 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
502 break;
503 default:
504 goto out;
505 }
506
507 if (devfreq->profile->polling_ms)
508 queue_delayed_work(devfreq_wq, &devfreq->work,
509 msecs_to_jiffies(devfreq->profile->polling_ms));
510
511 out:
512 devfreq->stop_polling = false;
513 mutex_unlock(&devfreq->lock);
514 }
515 EXPORT_SYMBOL(devfreq_monitor_start);
516
517 /**
518 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
519 * @devfreq: the devfreq instance.
520 *
521 * Helper function to stop devfreq device load monitoring. Function
522 * to be called from governor in response to DEVFREQ_GOV_STOP
523 * event when device is removed from devfreq framework.
524 */
devfreq_monitor_stop(struct devfreq * devfreq)525 void devfreq_monitor_stop(struct devfreq *devfreq)
526 {
527 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
528 return;
529
530 mutex_lock(&devfreq->lock);
531 if (devfreq->stop_polling) {
532 mutex_unlock(&devfreq->lock);
533 return;
534 }
535
536 devfreq->stop_polling = true;
537 mutex_unlock(&devfreq->lock);
538 cancel_delayed_work_sync(&devfreq->work);
539 }
540 EXPORT_SYMBOL(devfreq_monitor_stop);
541
542 /**
543 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
544 * @devfreq: the devfreq instance.
545 *
546 * Helper function to suspend devfreq device load monitoring. Function
547 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
548 * event or when polling interval is set to zero.
549 *
550 * Note: Though this function is same as devfreq_monitor_stop(),
551 * intentionally kept separate to provide hooks for collecting
552 * transition statistics.
553 */
devfreq_monitor_suspend(struct devfreq * devfreq)554 void devfreq_monitor_suspend(struct devfreq *devfreq)
555 {
556 mutex_lock(&devfreq->lock);
557 if (devfreq->stop_polling) {
558 mutex_unlock(&devfreq->lock);
559 return;
560 }
561
562 devfreq_update_status(devfreq, devfreq->previous_freq);
563 devfreq->stop_polling = true;
564 mutex_unlock(&devfreq->lock);
565
566 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
567 return;
568
569 cancel_delayed_work_sync(&devfreq->work);
570 }
571 EXPORT_SYMBOL(devfreq_monitor_suspend);
572
573 /**
574 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
575 * @devfreq: the devfreq instance.
576 *
577 * Helper function to resume devfreq device load monitoring. Function
578 * to be called from governor in response to DEVFREQ_GOV_RESUME
579 * event or when polling interval is set to non-zero.
580 */
devfreq_monitor_resume(struct devfreq * devfreq)581 void devfreq_monitor_resume(struct devfreq *devfreq)
582 {
583 unsigned long freq;
584
585 mutex_lock(&devfreq->lock);
586
587 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
588 goto out_update;
589
590 if (!devfreq->stop_polling)
591 goto out;
592
593 if (!delayed_work_pending(&devfreq->work) &&
594 devfreq->profile->polling_ms)
595 queue_delayed_work(devfreq_wq, &devfreq->work,
596 msecs_to_jiffies(devfreq->profile->polling_ms));
597
598 out_update:
599 devfreq->stats.last_update = get_jiffies_64();
600 devfreq->stop_polling = false;
601
602 if (devfreq->profile->get_cur_freq &&
603 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
604 devfreq->previous_freq = freq;
605
606 out:
607 mutex_unlock(&devfreq->lock);
608 }
609 EXPORT_SYMBOL(devfreq_monitor_resume);
610
611 /**
612 * devfreq_update_interval() - Update device devfreq monitoring interval
613 * @devfreq: the devfreq instance.
614 * @delay: new polling interval to be set.
615 *
616 * Helper function to set new load monitoring polling interval. Function
617 * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
618 */
devfreq_update_interval(struct devfreq * devfreq,unsigned int * delay)619 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
620 {
621 unsigned int cur_delay = devfreq->profile->polling_ms;
622 unsigned int new_delay = *delay;
623
624 mutex_lock(&devfreq->lock);
625 devfreq->profile->polling_ms = new_delay;
626
627 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
628 goto out;
629
630 if (devfreq->stop_polling)
631 goto out;
632
633 /* if new delay is zero, stop polling */
634 if (!new_delay) {
635 mutex_unlock(&devfreq->lock);
636 cancel_delayed_work_sync(&devfreq->work);
637 return;
638 }
639
640 /* if current delay is zero, start polling with new delay */
641 if (!cur_delay) {
642 queue_delayed_work(devfreq_wq, &devfreq->work,
643 msecs_to_jiffies(devfreq->profile->polling_ms));
644 goto out;
645 }
646
647 /* if current delay is greater than new delay, restart polling */
648 if (cur_delay > new_delay) {
649 mutex_unlock(&devfreq->lock);
650 cancel_delayed_work_sync(&devfreq->work);
651 mutex_lock(&devfreq->lock);
652 if (!devfreq->stop_polling)
653 queue_delayed_work(devfreq_wq, &devfreq->work,
654 msecs_to_jiffies(devfreq->profile->polling_ms));
655 }
656 out:
657 mutex_unlock(&devfreq->lock);
658 }
659 EXPORT_SYMBOL(devfreq_update_interval);
660
661 /**
662 * devfreq_notifier_call() - Notify that the device frequency requirements
663 * has been changed out of devfreq framework.
664 * @nb: the notifier_block (supposed to be devfreq->nb)
665 * @type: not used
666 * @devp: not used
667 *
668 * Called by a notifier that uses devfreq->nb.
669 */
devfreq_notifier_call(struct notifier_block * nb,unsigned long type,void * devp)670 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
671 void *devp)
672 {
673 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
674 int err = -EINVAL;
675
676 mutex_lock(&devfreq->lock);
677
678 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
679 if (!devfreq->scaling_min_freq)
680 goto out;
681
682 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
683 if (!devfreq->scaling_max_freq) {
684 devfreq->scaling_max_freq = ULONG_MAX;
685 goto out;
686 }
687
688 err = update_devfreq(devfreq);
689
690 out:
691 mutex_unlock(&devfreq->lock);
692 if (err)
693 dev_err(devfreq->dev.parent,
694 "failed to update frequency from OPP notifier (%d)\n",
695 err);
696
697 return NOTIFY_OK;
698 }
699
700 /**
701 * qos_notifier_call() - Common handler for QoS constraints.
702 * @devfreq: the devfreq instance.
703 */
qos_notifier_call(struct devfreq * devfreq)704 static int qos_notifier_call(struct devfreq *devfreq)
705 {
706 int err;
707
708 mutex_lock(&devfreq->lock);
709 err = update_devfreq(devfreq);
710 mutex_unlock(&devfreq->lock);
711 if (err)
712 dev_err(devfreq->dev.parent,
713 "failed to update frequency from PM QoS (%d)\n",
714 err);
715
716 return NOTIFY_OK;
717 }
718
719 /**
720 * qos_min_notifier_call() - Callback for QoS min_freq changes.
721 * @nb: Should be devfreq->nb_min
722 */
qos_min_notifier_call(struct notifier_block * nb,unsigned long val,void * ptr)723 static int qos_min_notifier_call(struct notifier_block *nb,
724 unsigned long val, void *ptr)
725 {
726 return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
727 }
728
729 /**
730 * qos_max_notifier_call() - Callback for QoS max_freq changes.
731 * @nb: Should be devfreq->nb_max
732 */
qos_max_notifier_call(struct notifier_block * nb,unsigned long val,void * ptr)733 static int qos_max_notifier_call(struct notifier_block *nb,
734 unsigned long val, void *ptr)
735 {
736 return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
737 }
738
739 /**
740 * devfreq_dev_release() - Callback for struct device to release the device.
741 * @dev: the devfreq device
742 *
743 * Remove devfreq from the list and release its resources.
744 */
devfreq_dev_release(struct device * dev)745 static void devfreq_dev_release(struct device *dev)
746 {
747 struct devfreq *devfreq = to_devfreq(dev);
748 int err;
749
750 mutex_lock(&devfreq_list_lock);
751 list_del(&devfreq->node);
752 mutex_unlock(&devfreq_list_lock);
753
754 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
755 DEV_PM_QOS_MAX_FREQUENCY);
756 if (err && err != -ENOENT)
757 dev_warn(dev->parent,
758 "Failed to remove max_freq notifier: %d\n", err);
759 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
760 DEV_PM_QOS_MIN_FREQUENCY);
761 if (err && err != -ENOENT)
762 dev_warn(dev->parent,
763 "Failed to remove min_freq notifier: %d\n", err);
764
765 if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
766 err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
767 if (err < 0)
768 dev_warn(dev->parent,
769 "Failed to remove max_freq request: %d\n", err);
770 }
771 if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
772 err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
773 if (err < 0)
774 dev_warn(dev->parent,
775 "Failed to remove min_freq request: %d\n", err);
776 }
777
778 if (devfreq->profile->exit)
779 devfreq->profile->exit(devfreq->dev.parent);
780
781 if (devfreq->opp_table)
782 dev_pm_opp_put_opp_table(devfreq->opp_table);
783
784 mutex_destroy(&devfreq->lock);
785 srcu_cleanup_notifier_head(&devfreq->transition_notifier_list);
786 kfree(devfreq);
787 }
788
789 static void create_sysfs_files(struct devfreq *devfreq,
790 const struct devfreq_governor *gov);
791 static void remove_sysfs_files(struct devfreq *devfreq,
792 const struct devfreq_governor *gov);
793
794 /**
795 * devfreq_add_device() - Add devfreq feature to the device
796 * @dev: the device to add devfreq feature.
797 * @profile: device-specific profile to run devfreq.
798 * @governor_name: name of the policy to choose frequency.
799 * @data: private data for the governor. The devfreq framework does not
800 * touch this value.
801 */
devfreq_add_device(struct device * dev,struct devfreq_dev_profile * profile,const char * governor_name,void * data)802 struct devfreq *devfreq_add_device(struct device *dev,
803 struct devfreq_dev_profile *profile,
804 const char *governor_name,
805 void *data)
806 {
807 struct devfreq *devfreq;
808 struct devfreq_governor *governor;
809 int err = 0;
810
811 if (!dev || !profile || !governor_name) {
812 dev_err(dev, "%s: Invalid parameters.\n", __func__);
813 return ERR_PTR(-EINVAL);
814 }
815
816 mutex_lock(&devfreq_list_lock);
817 devfreq = find_device_devfreq(dev);
818 mutex_unlock(&devfreq_list_lock);
819 if (!IS_ERR(devfreq)) {
820 dev_err(dev, "%s: devfreq device already exists!\n",
821 __func__);
822 err = -EINVAL;
823 goto err_out;
824 }
825
826 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
827 if (!devfreq) {
828 err = -ENOMEM;
829 goto err_out;
830 }
831
832 mutex_init(&devfreq->lock);
833 mutex_lock(&devfreq->lock);
834 devfreq->dev.parent = dev;
835 devfreq->dev.class = devfreq_class;
836 devfreq->dev.release = devfreq_dev_release;
837 INIT_LIST_HEAD(&devfreq->node);
838 devfreq->profile = profile;
839 devfreq->previous_freq = profile->initial_freq;
840 devfreq->last_status.current_frequency = profile->initial_freq;
841 devfreq->data = data;
842 devfreq->nb.notifier_call = devfreq_notifier_call;
843
844 if (devfreq->profile->timer < 0
845 || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
846 mutex_unlock(&devfreq->lock);
847 err = -EINVAL;
848 goto err_dev;
849 }
850
851 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
852 mutex_unlock(&devfreq->lock);
853 err = set_freq_table(devfreq);
854 if (err < 0)
855 goto err_dev;
856 mutex_lock(&devfreq->lock);
857 }
858
859 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
860 if (!devfreq->scaling_min_freq) {
861 mutex_unlock(&devfreq->lock);
862 err = -EINVAL;
863 goto err_dev;
864 }
865
866 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
867 if (!devfreq->scaling_max_freq) {
868 mutex_unlock(&devfreq->lock);
869 err = -EINVAL;
870 goto err_dev;
871 }
872
873 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
874 devfreq->opp_table = dev_pm_opp_get_opp_table(dev);
875 if (IS_ERR(devfreq->opp_table))
876 devfreq->opp_table = NULL;
877
878 atomic_set(&devfreq->suspend_count, 0);
879
880 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
881 err = device_register(&devfreq->dev);
882 if (err) {
883 mutex_unlock(&devfreq->lock);
884 put_device(&devfreq->dev);
885 goto err_out;
886 }
887
888 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
889 array3_size(sizeof(unsigned int),
890 devfreq->profile->max_state,
891 devfreq->profile->max_state),
892 GFP_KERNEL);
893 if (!devfreq->stats.trans_table) {
894 mutex_unlock(&devfreq->lock);
895 err = -ENOMEM;
896 goto err_devfreq;
897 }
898
899 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
900 devfreq->profile->max_state,
901 sizeof(*devfreq->stats.time_in_state),
902 GFP_KERNEL);
903 if (!devfreq->stats.time_in_state) {
904 mutex_unlock(&devfreq->lock);
905 err = -ENOMEM;
906 goto err_devfreq;
907 }
908
909 devfreq->stats.total_trans = 0;
910 devfreq->stats.last_update = get_jiffies_64();
911
912 srcu_init_notifier_head(&devfreq->transition_notifier_list);
913
914 mutex_unlock(&devfreq->lock);
915
916 err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
917 DEV_PM_QOS_MIN_FREQUENCY, 0);
918 if (err < 0)
919 goto err_devfreq;
920 err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
921 DEV_PM_QOS_MAX_FREQUENCY,
922 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
923 if (err < 0)
924 goto err_devfreq;
925
926 devfreq->nb_min.notifier_call = qos_min_notifier_call;
927 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_min,
928 DEV_PM_QOS_MIN_FREQUENCY);
929 if (err)
930 goto err_devfreq;
931
932 devfreq->nb_max.notifier_call = qos_max_notifier_call;
933 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_max,
934 DEV_PM_QOS_MAX_FREQUENCY);
935 if (err)
936 goto err_devfreq;
937
938 mutex_lock(&devfreq_list_lock);
939
940 governor = try_then_request_governor(governor_name);
941 if (IS_ERR(governor)) {
942 dev_err(dev, "%s: Unable to find governor for the device\n",
943 __func__);
944 err = PTR_ERR(governor);
945 goto err_init;
946 }
947
948 devfreq->governor = governor;
949 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
950 NULL);
951 if (err) {
952 dev_err(dev, "%s: Unable to start governor for the device\n",
953 __func__);
954 goto err_init;
955 }
956 create_sysfs_files(devfreq, devfreq->governor);
957
958 list_add(&devfreq->node, &devfreq_list);
959
960 mutex_unlock(&devfreq_list_lock);
961
962 if (devfreq->profile->is_cooling_device) {
963 devfreq->cdev = devfreq_cooling_em_register(devfreq, NULL);
964 if (IS_ERR(devfreq->cdev))
965 devfreq->cdev = NULL;
966 }
967
968 return devfreq;
969
970 err_init:
971 mutex_unlock(&devfreq_list_lock);
972 err_devfreq:
973 devfreq_remove_device(devfreq);
974 devfreq = NULL;
975 err_dev:
976 kfree(devfreq);
977 err_out:
978 return ERR_PTR(err);
979 }
980 EXPORT_SYMBOL(devfreq_add_device);
981
982 /**
983 * devfreq_remove_device() - Remove devfreq feature from a device.
984 * @devfreq: the devfreq instance to be removed
985 *
986 * The opposite of devfreq_add_device().
987 */
devfreq_remove_device(struct devfreq * devfreq)988 int devfreq_remove_device(struct devfreq *devfreq)
989 {
990 if (!devfreq)
991 return -EINVAL;
992
993 devfreq_cooling_unregister(devfreq->cdev);
994
995 if (devfreq->governor) {
996 devfreq->governor->event_handler(devfreq,
997 DEVFREQ_GOV_STOP, NULL);
998 remove_sysfs_files(devfreq, devfreq->governor);
999 }
1000
1001 device_unregister(&devfreq->dev);
1002
1003 return 0;
1004 }
1005 EXPORT_SYMBOL(devfreq_remove_device);
1006
devm_devfreq_dev_match(struct device * dev,void * res,void * data)1007 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
1008 {
1009 struct devfreq **r = res;
1010
1011 if (WARN_ON(!r || !*r))
1012 return 0;
1013
1014 return *r == data;
1015 }
1016
devm_devfreq_dev_release(struct device * dev,void * res)1017 static void devm_devfreq_dev_release(struct device *dev, void *res)
1018 {
1019 devfreq_remove_device(*(struct devfreq **)res);
1020 }
1021
1022 /**
1023 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
1024 * @dev: the device to add devfreq feature.
1025 * @profile: device-specific profile to run devfreq.
1026 * @governor_name: name of the policy to choose frequency.
1027 * @data: private data for the governor. The devfreq framework does not
1028 * touch this value.
1029 *
1030 * This function manages automatically the memory of devfreq device using device
1031 * resource management and simplify the free operation for memory of devfreq
1032 * device.
1033 */
devm_devfreq_add_device(struct device * dev,struct devfreq_dev_profile * profile,const char * governor_name,void * data)1034 struct devfreq *devm_devfreq_add_device(struct device *dev,
1035 struct devfreq_dev_profile *profile,
1036 const char *governor_name,
1037 void *data)
1038 {
1039 struct devfreq **ptr, *devfreq;
1040
1041 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
1042 if (!ptr)
1043 return ERR_PTR(-ENOMEM);
1044
1045 devfreq = devfreq_add_device(dev, profile, governor_name, data);
1046 if (IS_ERR(devfreq)) {
1047 devres_free(ptr);
1048 return devfreq;
1049 }
1050
1051 *ptr = devfreq;
1052 devres_add(dev, ptr);
1053
1054 return devfreq;
1055 }
1056 EXPORT_SYMBOL(devm_devfreq_add_device);
1057
1058 #ifdef CONFIG_OF
1059 /*
1060 * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
1061 * @node - pointer to device_node
1062 *
1063 * return the instance of devfreq device
1064 */
devfreq_get_devfreq_by_node(struct device_node * node)1065 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1066 {
1067 struct devfreq *devfreq;
1068
1069 if (!node)
1070 return ERR_PTR(-EINVAL);
1071
1072 mutex_lock(&devfreq_list_lock);
1073 list_for_each_entry(devfreq, &devfreq_list, node) {
1074 if (devfreq->dev.parent
1075 && devfreq->dev.parent->of_node == node) {
1076 mutex_unlock(&devfreq_list_lock);
1077 return devfreq;
1078 }
1079 }
1080 mutex_unlock(&devfreq_list_lock);
1081
1082 return ERR_PTR(-ENODEV);
1083 }
1084
1085 /*
1086 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1087 * @dev - instance to the given device
1088 * @phandle_name - name of property holding a phandle value
1089 * @index - index into list of devfreq
1090 *
1091 * return the instance of devfreq device
1092 */
devfreq_get_devfreq_by_phandle(struct device * dev,const char * phandle_name,int index)1093 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1094 const char *phandle_name, int index)
1095 {
1096 struct device_node *node;
1097 struct devfreq *devfreq;
1098
1099 if (!dev || !phandle_name)
1100 return ERR_PTR(-EINVAL);
1101
1102 if (!dev->of_node)
1103 return ERR_PTR(-EINVAL);
1104
1105 node = of_parse_phandle(dev->of_node, phandle_name, index);
1106 if (!node)
1107 return ERR_PTR(-ENODEV);
1108
1109 devfreq = devfreq_get_devfreq_by_node(node);
1110 of_node_put(node);
1111
1112 return devfreq;
1113 }
1114
1115 #else
devfreq_get_devfreq_by_node(struct device_node * node)1116 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1117 {
1118 return ERR_PTR(-ENODEV);
1119 }
1120
devfreq_get_devfreq_by_phandle(struct device * dev,const char * phandle_name,int index)1121 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1122 const char *phandle_name, int index)
1123 {
1124 return ERR_PTR(-ENODEV);
1125 }
1126 #endif /* CONFIG_OF */
1127 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1128 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1129
1130 /**
1131 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1132 * @dev: the device from which to remove devfreq feature.
1133 * @devfreq: the devfreq instance to be removed
1134 */
devm_devfreq_remove_device(struct device * dev,struct devfreq * devfreq)1135 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1136 {
1137 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1138 devm_devfreq_dev_match, devfreq));
1139 }
1140 EXPORT_SYMBOL(devm_devfreq_remove_device);
1141
1142 /**
1143 * devfreq_suspend_device() - Suspend devfreq of a device.
1144 * @devfreq: the devfreq instance to be suspended
1145 *
1146 * This function is intended to be called by the pm callbacks
1147 * (e.g., runtime_suspend, suspend) of the device driver that
1148 * holds the devfreq.
1149 */
devfreq_suspend_device(struct devfreq * devfreq)1150 int devfreq_suspend_device(struct devfreq *devfreq)
1151 {
1152 int ret;
1153
1154 if (!devfreq)
1155 return -EINVAL;
1156
1157 if (atomic_inc_return(&devfreq->suspend_count) > 1)
1158 return 0;
1159
1160 if (devfreq->governor) {
1161 ret = devfreq->governor->event_handler(devfreq,
1162 DEVFREQ_GOV_SUSPEND, NULL);
1163 if (ret)
1164 return ret;
1165 }
1166
1167 if (devfreq->suspend_freq) {
1168 mutex_lock(&devfreq->lock);
1169 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1170 mutex_unlock(&devfreq->lock);
1171 if (ret)
1172 return ret;
1173 }
1174
1175 return 0;
1176 }
1177 EXPORT_SYMBOL(devfreq_suspend_device);
1178
1179 /**
1180 * devfreq_resume_device() - Resume devfreq of a device.
1181 * @devfreq: the devfreq instance to be resumed
1182 *
1183 * This function is intended to be called by the pm callbacks
1184 * (e.g., runtime_resume, resume) of the device driver that
1185 * holds the devfreq.
1186 */
devfreq_resume_device(struct devfreq * devfreq)1187 int devfreq_resume_device(struct devfreq *devfreq)
1188 {
1189 int ret;
1190
1191 if (!devfreq)
1192 return -EINVAL;
1193
1194 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1195 return 0;
1196
1197 if (devfreq->resume_freq) {
1198 mutex_lock(&devfreq->lock);
1199 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1200 mutex_unlock(&devfreq->lock);
1201 if (ret)
1202 return ret;
1203 }
1204
1205 if (devfreq->governor) {
1206 ret = devfreq->governor->event_handler(devfreq,
1207 DEVFREQ_GOV_RESUME, NULL);
1208 if (ret)
1209 return ret;
1210 }
1211
1212 return 0;
1213 }
1214 EXPORT_SYMBOL(devfreq_resume_device);
1215
1216 /**
1217 * devfreq_suspend() - Suspend devfreq governors and devices
1218 *
1219 * Called during system wide Suspend/Hibernate cycles for suspending governors
1220 * and devices preserving the state for resume. On some platforms the devfreq
1221 * device must have precise state (frequency) after resume in order to provide
1222 * fully operating setup.
1223 */
devfreq_suspend(void)1224 void devfreq_suspend(void)
1225 {
1226 struct devfreq *devfreq;
1227 int ret;
1228
1229 mutex_lock(&devfreq_list_lock);
1230 list_for_each_entry(devfreq, &devfreq_list, node) {
1231 ret = devfreq_suspend_device(devfreq);
1232 if (ret)
1233 dev_err(&devfreq->dev,
1234 "failed to suspend devfreq device\n");
1235 }
1236 mutex_unlock(&devfreq_list_lock);
1237 }
1238
1239 /**
1240 * devfreq_resume() - Resume devfreq governors and devices
1241 *
1242 * Called during system wide Suspend/Hibernate cycle for resuming governors and
1243 * devices that are suspended with devfreq_suspend().
1244 */
devfreq_resume(void)1245 void devfreq_resume(void)
1246 {
1247 struct devfreq *devfreq;
1248 int ret;
1249
1250 mutex_lock(&devfreq_list_lock);
1251 list_for_each_entry(devfreq, &devfreq_list, node) {
1252 ret = devfreq_resume_device(devfreq);
1253 if (ret)
1254 dev_warn(&devfreq->dev,
1255 "failed to resume devfreq device\n");
1256 }
1257 mutex_unlock(&devfreq_list_lock);
1258 }
1259
1260 /**
1261 * devfreq_add_governor() - Add devfreq governor
1262 * @governor: the devfreq governor to be added
1263 */
devfreq_add_governor(struct devfreq_governor * governor)1264 int devfreq_add_governor(struct devfreq_governor *governor)
1265 {
1266 struct devfreq_governor *g;
1267 struct devfreq *devfreq;
1268 int err = 0;
1269
1270 if (!governor) {
1271 pr_err("%s: Invalid parameters.\n", __func__);
1272 return -EINVAL;
1273 }
1274
1275 mutex_lock(&devfreq_list_lock);
1276 g = find_devfreq_governor(governor->name);
1277 if (!IS_ERR(g)) {
1278 pr_err("%s: governor %s already registered\n", __func__,
1279 g->name);
1280 err = -EINVAL;
1281 goto err_out;
1282 }
1283
1284 list_add(&governor->node, &devfreq_governor_list);
1285
1286 list_for_each_entry(devfreq, &devfreq_list, node) {
1287 int ret = 0;
1288 struct device *dev = devfreq->dev.parent;
1289
1290 if (!strncmp(devfreq->governor->name, governor->name,
1291 DEVFREQ_NAME_LEN)) {
1292 /* The following should never occur */
1293 if (devfreq->governor) {
1294 dev_warn(dev,
1295 "%s: Governor %s already present\n",
1296 __func__, devfreq->governor->name);
1297 ret = devfreq->governor->event_handler(devfreq,
1298 DEVFREQ_GOV_STOP, NULL);
1299 if (ret) {
1300 dev_warn(dev,
1301 "%s: Governor %s stop = %d\n",
1302 __func__,
1303 devfreq->governor->name, ret);
1304 }
1305 /* Fall through */
1306 }
1307 devfreq->governor = governor;
1308 ret = devfreq->governor->event_handler(devfreq,
1309 DEVFREQ_GOV_START, NULL);
1310 if (ret) {
1311 dev_warn(dev, "%s: Governor %s start=%d\n",
1312 __func__, devfreq->governor->name,
1313 ret);
1314 }
1315 }
1316 }
1317
1318 err_out:
1319 mutex_unlock(&devfreq_list_lock);
1320
1321 return err;
1322 }
1323 EXPORT_SYMBOL(devfreq_add_governor);
1324
1325 /**
1326 * devfreq_remove_governor() - Remove devfreq feature from a device.
1327 * @governor: the devfreq governor to be removed
1328 */
devfreq_remove_governor(struct devfreq_governor * governor)1329 int devfreq_remove_governor(struct devfreq_governor *governor)
1330 {
1331 struct devfreq_governor *g;
1332 struct devfreq *devfreq;
1333 int err = 0;
1334
1335 if (!governor) {
1336 pr_err("%s: Invalid parameters.\n", __func__);
1337 return -EINVAL;
1338 }
1339
1340 mutex_lock(&devfreq_list_lock);
1341 g = find_devfreq_governor(governor->name);
1342 if (IS_ERR(g)) {
1343 pr_err("%s: governor %s not registered\n", __func__,
1344 governor->name);
1345 err = PTR_ERR(g);
1346 goto err_out;
1347 }
1348 list_for_each_entry(devfreq, &devfreq_list, node) {
1349 int ret;
1350 struct device *dev = devfreq->dev.parent;
1351
1352 if (!strncmp(devfreq->governor->name, governor->name,
1353 DEVFREQ_NAME_LEN)) {
1354 /* we should have a devfreq governor! */
1355 if (!devfreq->governor) {
1356 dev_warn(dev, "%s: Governor %s NOT present\n",
1357 __func__, governor->name);
1358 continue;
1359 /* Fall through */
1360 }
1361 ret = devfreq->governor->event_handler(devfreq,
1362 DEVFREQ_GOV_STOP, NULL);
1363 if (ret) {
1364 dev_warn(dev, "%s: Governor %s stop=%d\n",
1365 __func__, devfreq->governor->name,
1366 ret);
1367 }
1368 devfreq->governor = NULL;
1369 }
1370 }
1371
1372 list_del(&governor->node);
1373 err_out:
1374 mutex_unlock(&devfreq_list_lock);
1375
1376 return err;
1377 }
1378 EXPORT_SYMBOL(devfreq_remove_governor);
1379
name_show(struct device * dev,struct device_attribute * attr,char * buf)1380 static ssize_t name_show(struct device *dev,
1381 struct device_attribute *attr, char *buf)
1382 {
1383 struct devfreq *df = to_devfreq(dev);
1384 return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1385 }
1386 static DEVICE_ATTR_RO(name);
1387
governor_show(struct device * dev,struct device_attribute * attr,char * buf)1388 static ssize_t governor_show(struct device *dev,
1389 struct device_attribute *attr, char *buf)
1390 {
1391 struct devfreq *df = to_devfreq(dev);
1392
1393 if (!df->governor)
1394 return -EINVAL;
1395
1396 return sprintf(buf, "%s\n", df->governor->name);
1397 }
1398
governor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1399 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1400 const char *buf, size_t count)
1401 {
1402 struct devfreq *df = to_devfreq(dev);
1403 int ret;
1404 char str_governor[DEVFREQ_NAME_LEN + 1];
1405 const struct devfreq_governor *governor, *prev_governor;
1406
1407 if (!df->governor)
1408 return -EINVAL;
1409
1410 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1411 if (ret != 1)
1412 return -EINVAL;
1413
1414 mutex_lock(&devfreq_list_lock);
1415 governor = try_then_request_governor(str_governor);
1416 if (IS_ERR(governor)) {
1417 ret = PTR_ERR(governor);
1418 goto out;
1419 }
1420 if (df->governor == governor) {
1421 ret = 0;
1422 goto out;
1423 } else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)
1424 || IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) {
1425 ret = -EINVAL;
1426 goto out;
1427 }
1428
1429 /*
1430 * Stop the current governor and remove the specific sysfs files
1431 * which depend on current governor.
1432 */
1433 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1434 if (ret) {
1435 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1436 __func__, df->governor->name, ret);
1437 goto out;
1438 }
1439 remove_sysfs_files(df, df->governor);
1440
1441 /*
1442 * Start the new governor and create the specific sysfs files
1443 * which depend on the new governor.
1444 */
1445 prev_governor = df->governor;
1446 df->governor = governor;
1447 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1448 if (ret) {
1449 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1450 __func__, df->governor->name, ret);
1451
1452 /* Restore previous governor */
1453 df->governor = prev_governor;
1454 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1455 if (ret) {
1456 dev_err(dev,
1457 "%s: reverting to Governor %s failed (%d)\n",
1458 __func__, prev_governor->name, ret);
1459 df->governor = NULL;
1460 goto out;
1461 }
1462 }
1463
1464 /*
1465 * Create the sysfs files for the new governor. But if failed to start
1466 * the new governor, restore the sysfs files of previous governor.
1467 */
1468 create_sysfs_files(df, df->governor);
1469
1470 out:
1471 mutex_unlock(&devfreq_list_lock);
1472
1473 if (!ret)
1474 ret = count;
1475 return ret;
1476 }
1477 static DEVICE_ATTR_RW(governor);
1478
available_governors_show(struct device * d,struct device_attribute * attr,char * buf)1479 static ssize_t available_governors_show(struct device *d,
1480 struct device_attribute *attr,
1481 char *buf)
1482 {
1483 struct devfreq *df = to_devfreq(d);
1484 ssize_t count = 0;
1485
1486 if (!df->governor)
1487 return -EINVAL;
1488
1489 mutex_lock(&devfreq_list_lock);
1490
1491 /*
1492 * The devfreq with immutable governor (e.g., passive) shows
1493 * only own governor.
1494 */
1495 if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) {
1496 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1497 "%s ", df->governor->name);
1498 /*
1499 * The devfreq device shows the registered governor except for
1500 * immutable governors such as passive governor .
1501 */
1502 } else {
1503 struct devfreq_governor *governor;
1504
1505 list_for_each_entry(governor, &devfreq_governor_list, node) {
1506 if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE))
1507 continue;
1508 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1509 "%s ", governor->name);
1510 }
1511 }
1512
1513 mutex_unlock(&devfreq_list_lock);
1514
1515 /* Truncate the trailing space */
1516 if (count)
1517 count--;
1518
1519 count += sprintf(&buf[count], "\n");
1520
1521 return count;
1522 }
1523 static DEVICE_ATTR_RO(available_governors);
1524
cur_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1525 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1526 char *buf)
1527 {
1528 unsigned long freq;
1529 struct devfreq *df = to_devfreq(dev);
1530
1531 if (!df->profile)
1532 return -EINVAL;
1533
1534 if (df->profile->get_cur_freq &&
1535 !df->profile->get_cur_freq(df->dev.parent, &freq))
1536 return sprintf(buf, "%lu\n", freq);
1537
1538 return sprintf(buf, "%lu\n", df->previous_freq);
1539 }
1540 static DEVICE_ATTR_RO(cur_freq);
1541
target_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1542 static ssize_t target_freq_show(struct device *dev,
1543 struct device_attribute *attr, char *buf)
1544 {
1545 struct devfreq *df = to_devfreq(dev);
1546
1547 return sprintf(buf, "%lu\n", df->previous_freq);
1548 }
1549 static DEVICE_ATTR_RO(target_freq);
1550
min_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1551 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1552 const char *buf, size_t count)
1553 {
1554 struct devfreq *df = to_devfreq(dev);
1555 unsigned long value;
1556 int ret;
1557
1558 /*
1559 * Protect against theoretical sysfs writes between
1560 * device_add and dev_pm_qos_add_request
1561 */
1562 if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1563 return -EAGAIN;
1564
1565 ret = sscanf(buf, "%lu", &value);
1566 if (ret != 1)
1567 return -EINVAL;
1568
1569 /* Round down to kHz for PM QoS */
1570 ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1571 value / HZ_PER_KHZ);
1572 if (ret < 0)
1573 return ret;
1574
1575 return count;
1576 }
1577
min_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1578 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1579 char *buf)
1580 {
1581 struct devfreq *df = to_devfreq(dev);
1582 unsigned long min_freq, max_freq;
1583
1584 mutex_lock(&df->lock);
1585 get_freq_range(df, &min_freq, &max_freq);
1586 mutex_unlock(&df->lock);
1587
1588 return sprintf(buf, "%lu\n", min_freq);
1589 }
1590 static DEVICE_ATTR_RW(min_freq);
1591
max_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1592 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1593 const char *buf, size_t count)
1594 {
1595 struct devfreq *df = to_devfreq(dev);
1596 unsigned long value;
1597 int ret;
1598
1599 /*
1600 * Protect against theoretical sysfs writes between
1601 * device_add and dev_pm_qos_add_request
1602 */
1603 if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1604 return -EINVAL;
1605
1606 ret = sscanf(buf, "%lu", &value);
1607 if (ret != 1)
1608 return -EINVAL;
1609
1610 /*
1611 * PM QoS frequencies are in kHz so we need to convert. Convert by
1612 * rounding upwards so that the acceptable interval never shrinks.
1613 *
1614 * For example if the user writes "666666666" to sysfs this value will
1615 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1616 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1617 *
1618 * A value of zero means "no limit".
1619 */
1620 if (value)
1621 value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1622 else
1623 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1624
1625 ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1626 if (ret < 0)
1627 return ret;
1628
1629 return count;
1630 }
1631
max_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1632 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1633 char *buf)
1634 {
1635 struct devfreq *df = to_devfreq(dev);
1636 unsigned long min_freq, max_freq;
1637
1638 mutex_lock(&df->lock);
1639 get_freq_range(df, &min_freq, &max_freq);
1640 mutex_unlock(&df->lock);
1641
1642 return sprintf(buf, "%lu\n", max_freq);
1643 }
1644 static DEVICE_ATTR_RW(max_freq);
1645
available_frequencies_show(struct device * d,struct device_attribute * attr,char * buf)1646 static ssize_t available_frequencies_show(struct device *d,
1647 struct device_attribute *attr,
1648 char *buf)
1649 {
1650 struct devfreq *df = to_devfreq(d);
1651 ssize_t count = 0;
1652 int i;
1653
1654 if (!df->profile)
1655 return -EINVAL;
1656
1657 mutex_lock(&df->lock);
1658
1659 for (i = 0; i < df->profile->max_state; i++)
1660 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1661 "%lu ", df->profile->freq_table[i]);
1662
1663 mutex_unlock(&df->lock);
1664 /* Truncate the trailing space */
1665 if (count)
1666 count--;
1667
1668 count += sprintf(&buf[count], "\n");
1669
1670 return count;
1671 }
1672 static DEVICE_ATTR_RO(available_frequencies);
1673
trans_stat_show(struct device * dev,struct device_attribute * attr,char * buf)1674 static ssize_t trans_stat_show(struct device *dev,
1675 struct device_attribute *attr, char *buf)
1676 {
1677 struct devfreq *df = to_devfreq(dev);
1678 ssize_t len = 0;
1679 int i, j;
1680 unsigned int max_state;
1681
1682 if (!df->profile)
1683 return -EINVAL;
1684 max_state = df->profile->max_state;
1685
1686 if (max_state == 0)
1687 return scnprintf(buf, PAGE_SIZE, "Not Supported.\n");
1688
1689 mutex_lock(&df->lock);
1690 if (!df->stop_polling &&
1691 devfreq_update_status(df, df->previous_freq)) {
1692 mutex_unlock(&df->lock);
1693 return 0;
1694 }
1695 mutex_unlock(&df->lock);
1696
1697 len += scnprintf(buf + len, PAGE_SIZE - len, " From : To\n");
1698 len += scnprintf(buf + len, PAGE_SIZE - len, " :");
1699 for (i = 0; i < max_state; i++) {
1700 if (len >= PAGE_SIZE - 1)
1701 break;
1702 len += scnprintf(buf + len, PAGE_SIZE - len, "%10lu",
1703 df->profile->freq_table[i]);
1704 }
1705 if (len >= PAGE_SIZE - 1)
1706 return PAGE_SIZE - 1;
1707
1708 len += scnprintf(buf + len, PAGE_SIZE - len, " time(ms)\n");
1709
1710 for (i = 0; i < max_state; i++) {
1711 if (len >= PAGE_SIZE - 1)
1712 break;
1713 if (df->profile->freq_table[i]
1714 == df->previous_freq) {
1715 len += scnprintf(buf + len, PAGE_SIZE - len, "*");
1716 } else {
1717 len += scnprintf(buf + len, PAGE_SIZE - len, " ");
1718 }
1719 if (len >= PAGE_SIZE - 1)
1720 break;
1721
1722 len += scnprintf(buf + len, PAGE_SIZE - len, "%10lu:",
1723 df->profile->freq_table[i]);
1724 for (j = 0; j < max_state; j++) {
1725 if (len >= PAGE_SIZE - 1)
1726 break;
1727 len += scnprintf(buf + len, PAGE_SIZE - len, "%10u",
1728 df->stats.trans_table[(i * max_state) + j]);
1729 }
1730 if (len >= PAGE_SIZE - 1)
1731 break;
1732 len += scnprintf(buf + len, PAGE_SIZE - len, "%10llu\n", (u64)
1733 jiffies64_to_msecs(df->stats.time_in_state[i]));
1734 }
1735
1736 if (len < PAGE_SIZE - 1)
1737 len += scnprintf(buf + len, PAGE_SIZE - len, "Total transition : %u\n",
1738 df->stats.total_trans);
1739
1740 if (len >= PAGE_SIZE - 1) {
1741 pr_warn_once("devfreq transition table exceeds PAGE_SIZE. Disabling\n");
1742 return -EFBIG;
1743 }
1744
1745 return len;
1746 }
1747
trans_stat_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1748 static ssize_t trans_stat_store(struct device *dev,
1749 struct device_attribute *attr,
1750 const char *buf, size_t count)
1751 {
1752 struct devfreq *df = to_devfreq(dev);
1753 int err, value;
1754
1755 if (!df->profile)
1756 return -EINVAL;
1757
1758 if (df->profile->max_state == 0)
1759 return count;
1760
1761 err = kstrtoint(buf, 10, &value);
1762 if (err || value != 0)
1763 return -EINVAL;
1764
1765 mutex_lock(&df->lock);
1766 memset(df->stats.time_in_state, 0, (df->profile->max_state *
1767 sizeof(*df->stats.time_in_state)));
1768 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1769 df->profile->max_state,
1770 df->profile->max_state));
1771 df->stats.total_trans = 0;
1772 df->stats.last_update = get_jiffies_64();
1773 mutex_unlock(&df->lock);
1774
1775 return count;
1776 }
1777 static DEVICE_ATTR_RW(trans_stat);
1778
1779 static struct attribute *devfreq_attrs[] = {
1780 &dev_attr_name.attr,
1781 &dev_attr_governor.attr,
1782 &dev_attr_available_governors.attr,
1783 &dev_attr_cur_freq.attr,
1784 &dev_attr_available_frequencies.attr,
1785 &dev_attr_target_freq.attr,
1786 &dev_attr_min_freq.attr,
1787 &dev_attr_max_freq.attr,
1788 &dev_attr_trans_stat.attr,
1789 NULL,
1790 };
1791 ATTRIBUTE_GROUPS(devfreq);
1792
polling_interval_show(struct device * dev,struct device_attribute * attr,char * buf)1793 static ssize_t polling_interval_show(struct device *dev,
1794 struct device_attribute *attr, char *buf)
1795 {
1796 struct devfreq *df = to_devfreq(dev);
1797
1798 if (!df->profile)
1799 return -EINVAL;
1800
1801 return sprintf(buf, "%d\n", df->profile->polling_ms);
1802 }
1803
polling_interval_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1804 static ssize_t polling_interval_store(struct device *dev,
1805 struct device_attribute *attr,
1806 const char *buf, size_t count)
1807 {
1808 struct devfreq *df = to_devfreq(dev);
1809 unsigned int value;
1810 int ret;
1811
1812 if (!df->governor)
1813 return -EINVAL;
1814
1815 ret = sscanf(buf, "%u", &value);
1816 if (ret != 1)
1817 return -EINVAL;
1818
1819 df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1820 ret = count;
1821
1822 return ret;
1823 }
1824 static DEVICE_ATTR_RW(polling_interval);
1825
timer_show(struct device * dev,struct device_attribute * attr,char * buf)1826 static ssize_t timer_show(struct device *dev,
1827 struct device_attribute *attr, char *buf)
1828 {
1829 struct devfreq *df = to_devfreq(dev);
1830
1831 if (!df->profile)
1832 return -EINVAL;
1833
1834 return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1835 }
1836
timer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1837 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1838 const char *buf, size_t count)
1839 {
1840 struct devfreq *df = to_devfreq(dev);
1841 char str_timer[DEVFREQ_NAME_LEN + 1];
1842 int timer = -1;
1843 int ret = 0, i;
1844
1845 if (!df->governor || !df->profile)
1846 return -EINVAL;
1847
1848 ret = sscanf(buf, "%16s", str_timer);
1849 if (ret != 1)
1850 return -EINVAL;
1851
1852 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1853 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1854 timer = i;
1855 break;
1856 }
1857 }
1858
1859 if (timer < 0) {
1860 ret = -EINVAL;
1861 goto out;
1862 }
1863
1864 if (df->profile->timer == timer) {
1865 ret = 0;
1866 goto out;
1867 }
1868
1869 mutex_lock(&df->lock);
1870 df->profile->timer = timer;
1871 mutex_unlock(&df->lock);
1872
1873 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1874 if (ret) {
1875 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1876 __func__, df->governor->name, ret);
1877 goto out;
1878 }
1879
1880 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1881 if (ret)
1882 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1883 __func__, df->governor->name, ret);
1884 out:
1885 return ret ? ret : count;
1886 }
1887 static DEVICE_ATTR_RW(timer);
1888
1889 #define CREATE_SYSFS_FILE(df, name) \
1890 { \
1891 int ret; \
1892 ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr); \
1893 if (ret < 0) { \
1894 dev_warn(&df->dev, \
1895 "Unable to create attr(%s)\n", "##name"); \
1896 } \
1897 } \
1898
1899 /* Create the specific sysfs files which depend on each governor. */
create_sysfs_files(struct devfreq * devfreq,const struct devfreq_governor * gov)1900 static void create_sysfs_files(struct devfreq *devfreq,
1901 const struct devfreq_governor *gov)
1902 {
1903 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1904 CREATE_SYSFS_FILE(devfreq, polling_interval);
1905 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1906 CREATE_SYSFS_FILE(devfreq, timer);
1907 }
1908
1909 /* Remove the specific sysfs files which depend on each governor. */
remove_sysfs_files(struct devfreq * devfreq,const struct devfreq_governor * gov)1910 static void remove_sysfs_files(struct devfreq *devfreq,
1911 const struct devfreq_governor *gov)
1912 {
1913 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1914 sysfs_remove_file(&devfreq->dev.kobj,
1915 &dev_attr_polling_interval.attr);
1916 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1917 sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
1918 }
1919
1920 /**
1921 * devfreq_summary_show() - Show the summary of the devfreq devices
1922 * @s: seq_file instance to show the summary of devfreq devices
1923 * @data: not used
1924 *
1925 * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1926 * It helps that user can know the detailed information of the devfreq devices.
1927 *
1928 * Return 0 always because it shows the information without any data change.
1929 */
devfreq_summary_show(struct seq_file * s,void * data)1930 static int devfreq_summary_show(struct seq_file *s, void *data)
1931 {
1932 struct devfreq *devfreq;
1933 struct devfreq *p_devfreq = NULL;
1934 unsigned long cur_freq, min_freq, max_freq;
1935 unsigned int polling_ms;
1936 unsigned int timer;
1937
1938 seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1939 "dev",
1940 "parent_dev",
1941 "governor",
1942 "timer",
1943 "polling_ms",
1944 "cur_freq_Hz",
1945 "min_freq_Hz",
1946 "max_freq_Hz");
1947 seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1948 "------------------------------",
1949 "------------------------------",
1950 "---------------",
1951 "----------",
1952 "----------",
1953 "------------",
1954 "------------",
1955 "------------");
1956
1957 mutex_lock(&devfreq_list_lock);
1958
1959 list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1960 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1961 if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE,
1962 DEVFREQ_NAME_LEN)) {
1963 struct devfreq_passive_data *data = devfreq->data;
1964
1965 if (data)
1966 p_devfreq = data->parent;
1967 } else {
1968 p_devfreq = NULL;
1969 }
1970 #endif
1971
1972 mutex_lock(&devfreq->lock);
1973 cur_freq = devfreq->previous_freq;
1974 get_freq_range(devfreq, &min_freq, &max_freq);
1975 timer = devfreq->profile->timer;
1976
1977 if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL))
1978 polling_ms = devfreq->profile->polling_ms;
1979 else
1980 polling_ms = 0;
1981 mutex_unlock(&devfreq->lock);
1982
1983 seq_printf(s,
1984 "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
1985 dev_name(&devfreq->dev),
1986 p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1987 devfreq->governor->name,
1988 polling_ms ? timer_name[timer] : "null",
1989 polling_ms,
1990 cur_freq,
1991 min_freq,
1992 max_freq);
1993 }
1994
1995 mutex_unlock(&devfreq_list_lock);
1996
1997 return 0;
1998 }
1999 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
2000
devfreq_init(void)2001 static int __init devfreq_init(void)
2002 {
2003 devfreq_class = class_create(THIS_MODULE, "devfreq");
2004 if (IS_ERR(devfreq_class)) {
2005 pr_err("%s: couldn't create class\n", __FILE__);
2006 return PTR_ERR(devfreq_class);
2007 }
2008
2009 devfreq_wq = create_freezable_workqueue("devfreq_wq");
2010 if (!devfreq_wq) {
2011 class_destroy(devfreq_class);
2012 pr_err("%s: couldn't create workqueue\n", __FILE__);
2013 return -ENOMEM;
2014 }
2015 devfreq_class->dev_groups = devfreq_groups;
2016
2017 devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
2018 debugfs_create_file("devfreq_summary", 0444,
2019 devfreq_debugfs, NULL,
2020 &devfreq_summary_fops);
2021
2022 return 0;
2023 }
2024 subsys_initcall(devfreq_init);
2025
2026 /*
2027 * The following are helper functions for devfreq user device drivers with
2028 * OPP framework.
2029 */
2030
2031 /**
2032 * devfreq_recommended_opp() - Helper function to get proper OPP for the
2033 * freq value given to target callback.
2034 * @dev: The devfreq user device. (parent of devfreq)
2035 * @freq: The frequency given to target function
2036 * @flags: Flags handed from devfreq framework.
2037 *
2038 * The callers are required to call dev_pm_opp_put() for the returned OPP after
2039 * use.
2040 */
devfreq_recommended_opp(struct device * dev,unsigned long * freq,u32 flags)2041 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
2042 unsigned long *freq,
2043 u32 flags)
2044 {
2045 struct dev_pm_opp *opp;
2046
2047 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
2048 /* The freq is an upper bound. opp should be lower */
2049 opp = dev_pm_opp_find_freq_floor(dev, freq);
2050
2051 /* If not available, use the closest opp */
2052 if (opp == ERR_PTR(-ERANGE))
2053 opp = dev_pm_opp_find_freq_ceil(dev, freq);
2054 } else {
2055 /* The freq is an lower bound. opp should be higher */
2056 opp = dev_pm_opp_find_freq_ceil(dev, freq);
2057
2058 /* If not available, use the closest opp */
2059 if (opp == ERR_PTR(-ERANGE))
2060 opp = dev_pm_opp_find_freq_floor(dev, freq);
2061 }
2062
2063 return opp;
2064 }
2065 EXPORT_SYMBOL(devfreq_recommended_opp);
2066
2067 /**
2068 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
2069 * for any changes in the OPP availability
2070 * changes
2071 * @dev: The devfreq user device. (parent of devfreq)
2072 * @devfreq: The devfreq object.
2073 */
devfreq_register_opp_notifier(struct device * dev,struct devfreq * devfreq)2074 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
2075 {
2076 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
2077 }
2078 EXPORT_SYMBOL(devfreq_register_opp_notifier);
2079
2080 /**
2081 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
2082 * notified for any changes in the OPP
2083 * availability changes anymore.
2084 * @dev: The devfreq user device. (parent of devfreq)
2085 * @devfreq: The devfreq object.
2086 *
2087 * At exit() callback of devfreq_dev_profile, this must be included if
2088 * devfreq_recommended_opp is used.
2089 */
devfreq_unregister_opp_notifier(struct device * dev,struct devfreq * devfreq)2090 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
2091 {
2092 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
2093 }
2094 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
2095
devm_devfreq_opp_release(struct device * dev,void * res)2096 static void devm_devfreq_opp_release(struct device *dev, void *res)
2097 {
2098 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
2099 }
2100
2101 /**
2102 * devm_devfreq_register_opp_notifier() - Resource-managed
2103 * devfreq_register_opp_notifier()
2104 * @dev: The devfreq user device. (parent of devfreq)
2105 * @devfreq: The devfreq object.
2106 */
devm_devfreq_register_opp_notifier(struct device * dev,struct devfreq * devfreq)2107 int devm_devfreq_register_opp_notifier(struct device *dev,
2108 struct devfreq *devfreq)
2109 {
2110 struct devfreq **ptr;
2111 int ret;
2112
2113 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
2114 if (!ptr)
2115 return -ENOMEM;
2116
2117 ret = devfreq_register_opp_notifier(dev, devfreq);
2118 if (ret) {
2119 devres_free(ptr);
2120 return ret;
2121 }
2122
2123 *ptr = devfreq;
2124 devres_add(dev, ptr);
2125
2126 return 0;
2127 }
2128 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
2129
2130 /**
2131 * devm_devfreq_unregister_opp_notifier() - Resource-managed
2132 * devfreq_unregister_opp_notifier()
2133 * @dev: The devfreq user device. (parent of devfreq)
2134 * @devfreq: The devfreq object.
2135 */
devm_devfreq_unregister_opp_notifier(struct device * dev,struct devfreq * devfreq)2136 void devm_devfreq_unregister_opp_notifier(struct device *dev,
2137 struct devfreq *devfreq)
2138 {
2139 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
2140 devm_devfreq_dev_match, devfreq));
2141 }
2142 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2143
2144 /**
2145 * devfreq_register_notifier() - Register a driver with devfreq
2146 * @devfreq: The devfreq object.
2147 * @nb: The notifier block to register.
2148 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2149 */
devfreq_register_notifier(struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)2150 int devfreq_register_notifier(struct devfreq *devfreq,
2151 struct notifier_block *nb,
2152 unsigned int list)
2153 {
2154 int ret = 0;
2155
2156 if (!devfreq)
2157 return -EINVAL;
2158
2159 switch (list) {
2160 case DEVFREQ_TRANSITION_NOTIFIER:
2161 ret = srcu_notifier_chain_register(
2162 &devfreq->transition_notifier_list, nb);
2163 break;
2164 default:
2165 ret = -EINVAL;
2166 }
2167
2168 return ret;
2169 }
2170 EXPORT_SYMBOL(devfreq_register_notifier);
2171
2172 /*
2173 * devfreq_unregister_notifier() - Unregister a driver with devfreq
2174 * @devfreq: The devfreq object.
2175 * @nb: The notifier block to be unregistered.
2176 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2177 */
devfreq_unregister_notifier(struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)2178 int devfreq_unregister_notifier(struct devfreq *devfreq,
2179 struct notifier_block *nb,
2180 unsigned int list)
2181 {
2182 int ret = 0;
2183
2184 if (!devfreq)
2185 return -EINVAL;
2186
2187 switch (list) {
2188 case DEVFREQ_TRANSITION_NOTIFIER:
2189 ret = srcu_notifier_chain_unregister(
2190 &devfreq->transition_notifier_list, nb);
2191 break;
2192 default:
2193 ret = -EINVAL;
2194 }
2195
2196 return ret;
2197 }
2198 EXPORT_SYMBOL(devfreq_unregister_notifier);
2199
2200 struct devfreq_notifier_devres {
2201 struct devfreq *devfreq;
2202 struct notifier_block *nb;
2203 unsigned int list;
2204 };
2205
devm_devfreq_notifier_release(struct device * dev,void * res)2206 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2207 {
2208 struct devfreq_notifier_devres *this = res;
2209
2210 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2211 }
2212
2213 /**
2214 * devm_devfreq_register_notifier()
2215 * - Resource-managed devfreq_register_notifier()
2216 * @dev: The devfreq user device. (parent of devfreq)
2217 * @devfreq: The devfreq object.
2218 * @nb: The notifier block to be unregistered.
2219 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2220 */
devm_devfreq_register_notifier(struct device * dev,struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)2221 int devm_devfreq_register_notifier(struct device *dev,
2222 struct devfreq *devfreq,
2223 struct notifier_block *nb,
2224 unsigned int list)
2225 {
2226 struct devfreq_notifier_devres *ptr;
2227 int ret;
2228
2229 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2230 GFP_KERNEL);
2231 if (!ptr)
2232 return -ENOMEM;
2233
2234 ret = devfreq_register_notifier(devfreq, nb, list);
2235 if (ret) {
2236 devres_free(ptr);
2237 return ret;
2238 }
2239
2240 ptr->devfreq = devfreq;
2241 ptr->nb = nb;
2242 ptr->list = list;
2243 devres_add(dev, ptr);
2244
2245 return 0;
2246 }
2247 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2248
2249 /**
2250 * devm_devfreq_unregister_notifier()
2251 * - Resource-managed devfreq_unregister_notifier()
2252 * @dev: The devfreq user device. (parent of devfreq)
2253 * @devfreq: The devfreq object.
2254 * @nb: The notifier block to be unregistered.
2255 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2256 */
devm_devfreq_unregister_notifier(struct device * dev,struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)2257 void devm_devfreq_unregister_notifier(struct device *dev,
2258 struct devfreq *devfreq,
2259 struct notifier_block *nb,
2260 unsigned int list)
2261 {
2262 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2263 devm_devfreq_dev_match, devfreq));
2264 }
2265 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);
2266