blob: 9fed1b82929278d08d3aa8ef26c77ebd705fbd1f [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
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +020011#include <linux/cpu.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040012#include <linux/cpuidle.h>
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +020013#include <linux/mutex.h>
14#include <linux/pm_qos.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040015
16#include "cpuidle.h"
17
18LIST_HEAD(cpuidle_governors);
19struct cpuidle_governor *cpuidle_curr_governor;
20
21/**
22 * __cpuidle_find_governor - finds a governor of the specified name
23 * @str: the name
24 *
Uwe Kleine-König21ae2952009-10-07 15:21:09 +020025 * Must be called with cpuidle_lock acquired.
Len Brown4f86d3a2007-10-03 18:58:00 -040026 */
27static struct cpuidle_governor * __cpuidle_find_governor(const char *str)
28{
29 struct cpuidle_governor *gov;
30
31 list_for_each_entry(gov, &cpuidle_governors, governor_list)
Rasmus Villemoes91336642014-09-16 22:51:26 +020032 if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN))
Len Brown4f86d3a2007-10-03 18:58:00 -040033 return gov;
34
35 return NULL;
36}
37
38/**
39 * cpuidle_switch_governor - changes the governor
40 * @gov: the new target governor
Uwe Kleine-König21ae2952009-10-07 15:21:09 +020041 * Must be called with cpuidle_lock acquired.
Len Brown4f86d3a2007-10-03 18:58:00 -040042 */
43int cpuidle_switch_governor(struct cpuidle_governor *gov)
44{
45 struct cpuidle_device *dev;
46
gaurav jindal3cfd68b2018-01-05 14:01:30 +010047 if (!gov)
48 return -EINVAL;
49
Len Brown4f86d3a2007-10-03 18:58:00 -040050 if (gov == cpuidle_curr_governor)
51 return 0;
52
53 cpuidle_uninstall_idle_handler();
54
55 if (cpuidle_curr_governor) {
56 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
57 cpuidle_disable_device(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -040058 }
59
60 cpuidle_curr_governor = gov;
61
62 if (gov) {
Len Brown4f86d3a2007-10-03 18:58:00 -040063 list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
64 cpuidle_enable_device(dev);
65 cpuidle_install_idle_handler();
66 printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
67 }
68
69 return 0;
70}
71
72/**
73 * cpuidle_register_governor - registers a governor
74 * @gov: the governor
75 */
76int cpuidle_register_governor(struct cpuidle_governor *gov)
77{
78 int ret = -EEXIST;
79
80 if (!gov || !gov->select)
81 return -EINVAL;
82
Len Brown62027ae2011-04-01 18:13:10 -040083 if (cpuidle_disabled())
84 return -ENODEV;
85
Len Brown4f86d3a2007-10-03 18:58:00 -040086 mutex_lock(&cpuidle_lock);
87 if (__cpuidle_find_governor(gov->name) == NULL) {
88 ret = 0;
89 list_add_tail(&gov->governor_list, &cpuidle_governors);
90 if (!cpuidle_curr_governor ||
91 cpuidle_curr_governor->rating < gov->rating)
92 cpuidle_switch_governor(gov);
93 }
94 mutex_unlock(&cpuidle_lock);
95
96 return ret;
97}
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +020098
99/**
100 * cpuidle_governor_latency_req - Compute a latency constraint for CPU
101 * @cpu: Target CPU
102 */
103int cpuidle_governor_latency_req(unsigned int cpu)
104{
105 int global_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
106 struct device *device = get_cpu_device(cpu);
107 int device_req = dev_pm_qos_raw_read_value(device);
108
109 return device_req < global_req ? device_req : global_req;
110}