blob: 29acaf48e575818933c0176f576e18adefc0e5d3 [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>
Rafael J. Wysocki61cb5752018-12-05 23:45:34 +010014#include <linux/module.h>
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +020015#include <linux/pm_qos.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040016
17#include "cpuidle.h"
18
Rafael J. Wysocki61cb5752018-12-05 23:45:34 +010019char param_governor[CPUIDLE_NAME_LEN];
20
Len Brown4f86d3a2007-10-03 18:58:00 -040021LIST_HEAD(cpuidle_governors);
22struct cpuidle_governor *cpuidle_curr_governor;
Joao Martinscb5d8c42019-09-08 00:45:21 +010023struct cpuidle_governor *cpuidle_prev_governor;
Len Brown4f86d3a2007-10-03 18:58:00 -040024
25/**
Joao Martinscb5d8c42019-09-08 00:45:21 +010026 * cpuidle_find_governor - finds a governor of the specified name
Len Brown4f86d3a2007-10-03 18:58:00 -040027 * @str: the name
28 *
Uwe Kleine-König21ae2952009-10-07 15:21:09 +020029 * Must be called with cpuidle_lock acquired.
Len Brown4f86d3a2007-10-03 18:58:00 -040030 */
Joao Martinscb5d8c42019-09-08 00:45:21 +010031struct cpuidle_governor *cpuidle_find_governor(const char *str)
Len Brown4f86d3a2007-10-03 18:58:00 -040032{
33 struct cpuidle_governor *gov;
34
35 list_for_each_entry(gov, &cpuidle_governors, governor_list)
Rasmus Villemoes91336642014-09-16 22:51:26 +020036 if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN))
Len Brown4f86d3a2007-10-03 18:58:00 -040037 return gov;
38
39 return NULL;
40}
41
42/**
43 * cpuidle_switch_governor - changes the governor
44 * @gov: the new target governor
Uwe Kleine-König21ae2952009-10-07 15:21:09 +020045 * Must be called with cpuidle_lock acquired.
Len Brown4f86d3a2007-10-03 18:58:00 -040046 */
47int cpuidle_switch_governor(struct cpuidle_governor *gov)
48{
49 struct cpuidle_device *dev;
50
gaurav jindal3cfd68b2018-01-05 14:01:30 +010051 if (!gov)
52 return -EINVAL;
53
Len Brown4f86d3a2007-10-03 18:58:00 -040054 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);
Len Brown4f86d3a2007-10-03 18:58:00 -040062 }
63
64 cpuidle_curr_governor = gov;
65
66 if (gov) {
Len Brown4f86d3a2007-10-03 18:58:00 -040067 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);
71 }
72
73 return 0;
74}
75
76/**
77 * cpuidle_register_governor - registers a governor
78 * @gov: the governor
79 */
80int cpuidle_register_governor(struct cpuidle_governor *gov)
81{
82 int ret = -EEXIST;
83
84 if (!gov || !gov->select)
85 return -EINVAL;
86
Len Brown62027ae2011-04-01 18:13:10 -040087 if (cpuidle_disabled())
88 return -ENODEV;
89
Len Brown4f86d3a2007-10-03 18:58:00 -040090 mutex_lock(&cpuidle_lock);
Joao Martinscb5d8c42019-09-08 00:45:21 +010091 if (cpuidle_find_governor(gov->name) == NULL) {
Len Brown4f86d3a2007-10-03 18:58:00 -040092 ret = 0;
Rafael J. Wysocki22782b32019-03-12 19:13:13 +010093 list_add_tail(&gov->governor_list, &cpuidle_governors);
Len Brown4f86d3a2007-10-03 18:58:00 -040094 if (!cpuidle_curr_governor ||
Rafael J. Wysocki61cb5752018-12-05 23:45:34 +010095 !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)))
Len Brown4f86d3a2007-10-03 18:58:00 -040099 cpuidle_switch_governor(gov);
100 }
101 mutex_unlock(&cpuidle_lock);
102
103 return ret;
104}
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +0200105
106/**
107 * cpuidle_governor_latency_req - Compute a latency constraint for CPU
108 * @cpu: Target CPU
109 */
Rafael J. Wysockic1d51f62019-11-07 15:25:12 +0100110s64 cpuidle_governor_latency_req(unsigned int cpu)
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +0200111{
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +0200112 struct device *device = get_cpu_device(cpu);
Viresh Kumar82623312019-07-04 13:06:18 +0530113 int device_req = dev_pm_qos_raw_resume_latency(device);
Rafael J. Wysockif60ccc32020-02-12 00:08:42 +0100114 int global_req = cpu_latency_qos_limit();
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +0200115
Rafael J. Wysockic1d51f62019-11-07 15:25:12 +0100116 if (device_req > global_req)
117 device_req = global_req;
118
119 return (s64)device_req * NSEC_PER_USEC;
Rafael J. Wysocki0fc784f2018-05-30 13:43:01 +0200120}