Loading...
1/*
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>
12#include <linux/module.h>
13#include <linux/cpuidle.h>
14
15#include "cpuidle.h"
16
17LIST_HEAD(cpuidle_governors);
18struct cpuidle_governor *cpuidle_curr_governor;
19
20/**
21 * __cpuidle_find_governor - finds a governor of the specified name
22 * @str: the name
23 *
24 * Must be called with cpuidle_lock acquired.
25 */
26static struct cpuidle_governor * __cpuidle_find_governor(const char *str)
27{
28 struct cpuidle_governor *gov;
29
30 list_for_each_entry(gov, &cpuidle_governors, governor_list)
31 if (!strnicmp(str, gov->name, CPUIDLE_NAME_LEN))
32 return gov;
33
34 return NULL;
35}
36
37/**
38 * cpuidle_switch_governor - changes the governor
39 * @gov: the new target governor
40 *
41 * NOTE: "gov" can be NULL to specify disabled
42 * Must be called with cpuidle_lock acquired.
43 */
44int cpuidle_switch_governor(struct cpuidle_governor *gov)
45{
46 struct cpuidle_device *dev;
47
48 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);
56 module_put(cpuidle_curr_governor->owner);
57 }
58
59 cpuidle_curr_governor = gov;
60
61 if (gov) {
62 if (!try_module_get(cpuidle_curr_governor->owner))
63 return -EINVAL;
64 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
65 cpuidle_enable_device(dev);
66 cpuidle_install_idle_handler();
67 printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
68 }
69
70 return 0;
71}
72
73/**
74 * cpuidle_register_governor - registers a governor
75 * @gov: the governor
76 */
77int cpuidle_register_governor(struct cpuidle_governor *gov)
78{
79 int ret = -EEXIST;
80
81 if (!gov || !gov->select)
82 return -EINVAL;
83
84 if (cpuidle_disabled())
85 return -ENODEV;
86
87 mutex_lock(&cpuidle_lock);
88 if (__cpuidle_find_governor(gov->name) == NULL) {
89 ret = 0;
90 list_add_tail(&gov->governor_list, &cpuidle_governors);
91 if (!cpuidle_curr_governor ||
92 cpuidle_curr_governor->rating < gov->rating)
93 cpuidle_switch_governor(gov);
94 }
95 mutex_unlock(&cpuidle_lock);
96
97 return ret;
98}
99
100/**
101 * cpuidle_replace_governor - find a replacement governor
102 * @exclude_rating: the rating that will be skipped while looking for
103 * new governor.
104 */
105static struct cpuidle_governor *cpuidle_replace_governor(int exclude_rating)
106{
107 struct cpuidle_governor *gov;
108 struct cpuidle_governor *ret_gov = NULL;
109 unsigned int max_rating = 0;
110
111 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
112 if (gov->rating == exclude_rating)
113 continue;
114 if (gov->rating > max_rating) {
115 max_rating = gov->rating;
116 ret_gov = gov;
117 }
118 }
119
120 return ret_gov;
121}
122
123/**
124 * cpuidle_unregister_governor - unregisters a governor
125 * @gov: the governor
126 */
127void cpuidle_unregister_governor(struct cpuidle_governor *gov)
128{
129 if (!gov)
130 return;
131
132 mutex_lock(&cpuidle_lock);
133 if (gov == cpuidle_curr_governor) {
134 struct cpuidle_governor *new_gov;
135 new_gov = cpuidle_replace_governor(gov->rating);
136 cpuidle_switch_governor(new_gov);
137 }
138 list_del(&gov->governor_list);
139 mutex_unlock(&cpuidle_lock);
140}
141
1/*
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/cpu.h>
12#include <linux/cpuidle.h>
13#include <linux/mutex.h>
14#include <linux/module.h>
15#include <linux/pm_qos.h>
16
17#include "cpuidle.h"
18
19char param_governor[CPUIDLE_NAME_LEN];
20
21LIST_HEAD(cpuidle_governors);
22struct cpuidle_governor *cpuidle_curr_governor;
23struct cpuidle_governor *cpuidle_prev_governor;
24
25/**
26 * cpuidle_find_governor - finds a governor of the specified name
27 * @str: the name
28 *
29 * Must be called with cpuidle_lock acquired.
30 */
31struct cpuidle_governor *cpuidle_find_governor(const char *str)
32{
33 struct cpuidle_governor *gov;
34
35 list_for_each_entry(gov, &cpuidle_governors, governor_list)
36 if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN))
37 return gov;
38
39 return NULL;
40}
41
42/**
43 * cpuidle_switch_governor - changes the governor
44 * @gov: the new target governor
45 * Must be called with cpuidle_lock acquired.
46 */
47int cpuidle_switch_governor(struct cpuidle_governor *gov)
48{
49 struct cpuidle_device *dev;
50
51 if (!gov)
52 return -EINVAL;
53
54 if (gov == cpuidle_curr_governor)
55 return 0;
56
57 cpuidle_uninstall_idle_handler();
58
59 if (cpuidle_curr_governor) {
60 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
61 cpuidle_disable_device(dev);
62 }
63
64 cpuidle_curr_governor = gov;
65
66 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
67 cpuidle_enable_device(dev);
68
69 cpuidle_install_idle_handler();
70 pr_info("cpuidle: using governor %s\n", gov->name);
71
72 return 0;
73}
74
75/**
76 * cpuidle_register_governor - registers a governor
77 * @gov: the governor
78 */
79int cpuidle_register_governor(struct cpuidle_governor *gov)
80{
81 int ret = -EEXIST;
82
83 if (!gov || !gov->select)
84 return -EINVAL;
85
86 if (cpuidle_disabled())
87 return -ENODEV;
88
89 mutex_lock(&cpuidle_lock);
90 if (cpuidle_find_governor(gov->name) == NULL) {
91 ret = 0;
92 list_add_tail(&gov->governor_list, &cpuidle_governors);
93 if (!cpuidle_curr_governor ||
94 !strncasecmp(param_governor, gov->name, CPUIDLE_NAME_LEN) ||
95 (cpuidle_curr_governor->rating < gov->rating &&
96 strncasecmp(param_governor, cpuidle_curr_governor->name,
97 CPUIDLE_NAME_LEN)))
98 cpuidle_switch_governor(gov);
99 }
100 mutex_unlock(&cpuidle_lock);
101
102 return ret;
103}
104
105/**
106 * cpuidle_governor_latency_req - Compute a latency constraint for CPU
107 * @cpu: Target CPU
108 */
109s64 cpuidle_governor_latency_req(unsigned int cpu)
110{
111 struct device *device = get_cpu_device(cpu);
112 int device_req = dev_pm_qos_raw_resume_latency(device);
113 int global_req = cpu_latency_qos_limit();
114
115 if (device_req > global_req)
116 device_req = global_req;
117
118 return (s64)device_req * NSEC_PER_USEC;
119}