Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / cpuidle / governor.c
blob29acaf48e575818933c0176f576e18adefc0e5d3
1 /*
2 * governor.c - governor support
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
8 * This code is licenced under the GPL.
9 */
11 #include <linux/cpu.h>
12 #include <linux/cpuidle.h>
13 #include <linux/mutex.h>
14 #include <linux/module.h>
15 #include <linux/pm_qos.h>
17 #include "cpuidle.h"
19 char param_governor[CPUIDLE_NAME_LEN];
21 LIST_HEAD(cpuidle_governors);
22 struct cpuidle_governor *cpuidle_curr_governor;
23 struct cpuidle_governor *cpuidle_prev_governor;
25 /**
26 * cpuidle_find_governor - finds a governor of the specified name
27 * @str: the name
29 * Must be called with cpuidle_lock acquired.
31 struct cpuidle_governor *cpuidle_find_governor(const char *str)
33 struct cpuidle_governor *gov;
35 list_for_each_entry(gov, &cpuidle_governors, governor_list)
36 if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN))
37 return gov;
39 return NULL;
42 /**
43 * cpuidle_switch_governor - changes the governor
44 * @gov: the new target governor
45 * Must be called with cpuidle_lock acquired.
47 int cpuidle_switch_governor(struct cpuidle_governor *gov)
49 struct cpuidle_device *dev;
51 if (!gov)
52 return -EINVAL;
54 if (gov == cpuidle_curr_governor)
55 return 0;
57 cpuidle_uninstall_idle_handler();
59 if (cpuidle_curr_governor) {
60 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
61 cpuidle_disable_device(dev);
64 cpuidle_curr_governor = gov;
66 if (gov) {
67 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
68 cpuidle_enable_device(dev);
69 cpuidle_install_idle_handler();
70 printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
73 return 0;
76 /**
77 * cpuidle_register_governor - registers a governor
78 * @gov: the governor
80 int cpuidle_register_governor(struct cpuidle_governor *gov)
82 int ret = -EEXIST;
84 if (!gov || !gov->select)
85 return -EINVAL;
87 if (cpuidle_disabled())
88 return -ENODEV;
90 mutex_lock(&cpuidle_lock);
91 if (cpuidle_find_governor(gov->name) == NULL) {
92 ret = 0;
93 list_add_tail(&gov->governor_list, &cpuidle_governors);
94 if (!cpuidle_curr_governor ||
95 !strncasecmp(param_governor, gov->name, CPUIDLE_NAME_LEN) ||
96 (cpuidle_curr_governor->rating < gov->rating &&
97 strncasecmp(param_governor, cpuidle_curr_governor->name,
98 CPUIDLE_NAME_LEN)))
99 cpuidle_switch_governor(gov);
101 mutex_unlock(&cpuidle_lock);
103 return ret;
107 * cpuidle_governor_latency_req - Compute a latency constraint for CPU
108 * @cpu: Target CPU
110 s64 cpuidle_governor_latency_req(unsigned int cpu)
112 struct device *device = get_cpu_device(cpu);
113 int device_req = dev_pm_qos_raw_resume_latency(device);
114 int global_req = cpu_latency_qos_limit();
116 if (device_req > global_req)
117 device_req = global_req;
119 return (s64)device_req * NSEC_PER_USEC;