blob: 5d359aff3cc538005f6ebeb3b32a2ded2dd5605b [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
2 * governor.c - governor support
3 *
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
7 *
8 * This code is licenced under the GPL.
9 */
10
11#include <linux/mutex.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040012#include <linux/cpuidle.h>
13
14#include "cpuidle.h"
15
16LIST_HEAD(cpuidle_governors);
17struct cpuidle_governor *cpuidle_curr_governor;
18
19/**
20 * __cpuidle_find_governor - finds a governor of the specified name
21 * @str: the name
22 *
Uwe Kleine-König21ae2952009-10-07 15:21:09 +020023 * Must be called with cpuidle_lock acquired.
Len Brown4f86d3a2007-10-03 18:58:00 -040024 */
25static struct cpuidle_governor * __cpuidle_find_governor(const char *str)
26{
27 struct cpuidle_governor *gov;
28
29 list_for_each_entry(gov, &cpuidle_governors, governor_list)
Rasmus Villemoes91336642014-09-16 22:51:26 +020030 if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN))
Len Brown4f86d3a2007-10-03 18:58:00 -040031 return gov;
32
33 return NULL;
34}
35
36/**
37 * cpuidle_switch_governor - changes the governor
38 * @gov: the new target governor
Uwe Kleine-König21ae2952009-10-07 15:21:09 +020039 * Must be called with cpuidle_lock acquired.
Len Brown4f86d3a2007-10-03 18:58:00 -040040 */
41int cpuidle_switch_governor(struct cpuidle_governor *gov)
42{
43 struct cpuidle_device *dev;
44
gaurav jindal3cfd68b2018-01-05 14:01:30 +010045 if (!gov)
46 return -EINVAL;
47
Len Brown4f86d3a2007-10-03 18:58:00 -040048 if (gov == cpuidle_curr_governor)
49 return 0;
50
51 cpuidle_uninstall_idle_handler();
52
53 if (cpuidle_curr_governor) {
54 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
55 cpuidle_disable_device(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -040056 }
57
58 cpuidle_curr_governor = gov;
59
60 if (gov) {
Len Brown4f86d3a2007-10-03 18:58:00 -040061 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
62 cpuidle_enable_device(dev);
63 cpuidle_install_idle_handler();
64 printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
65 }
66
67 return 0;
68}
69
70/**
71 * cpuidle_register_governor - registers a governor
72 * @gov: the governor
73 */
74int cpuidle_register_governor(struct cpuidle_governor *gov)
75{
76 int ret = -EEXIST;
77
78 if (!gov || !gov->select)
79 return -EINVAL;
80
Len Brown62027ae2011-04-01 18:13:10 -040081 if (cpuidle_disabled())
82 return -ENODEV;
83
Len Brown4f86d3a2007-10-03 18:58:00 -040084 mutex_lock(&cpuidle_lock);
85 if (__cpuidle_find_governor(gov->name) == NULL) {
86 ret = 0;
87 list_add_tail(&gov->governor_list, &cpuidle_governors);
88 if (!cpuidle_curr_governor ||
89 cpuidle_curr_governor->rating < gov->rating)
90 cpuidle_switch_governor(gov);
91 }
92 mutex_unlock(&cpuidle_lock);
93
94 return ret;
95}