Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 0fc784f | 2018-05-30 13:43:01 +0200 | [diff] [blame] | 11 | #include <linux/cpu.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 12 | #include <linux/cpuidle.h> |
Rafael J. Wysocki | 0fc784f | 2018-05-30 13:43:01 +0200 | [diff] [blame] | 13 | #include <linux/mutex.h> |
Rafael J. Wysocki | 61cb575 | 2018-12-05 23:45:34 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
Rafael J. Wysocki | 0fc784f | 2018-05-30 13:43:01 +0200 | [diff] [blame] | 15 | #include <linux/pm_qos.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 16 | |
| 17 | #include "cpuidle.h" |
| 18 | |
Rafael J. Wysocki | 61cb575 | 2018-12-05 23:45:34 +0100 | [diff] [blame] | 19 | char param_governor[CPUIDLE_NAME_LEN]; |
| 20 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 21 | LIST_HEAD(cpuidle_governors); |
| 22 | struct cpuidle_governor *cpuidle_curr_governor; |
| 23 | |
| 24 | /** |
| 25 | * __cpuidle_find_governor - finds a governor of the specified name |
| 26 | * @str: the name |
| 27 | * |
Uwe Kleine-König | 21ae295 | 2009-10-07 15:21:09 +0200 | [diff] [blame] | 28 | * Must be called with cpuidle_lock acquired. |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 29 | */ |
| 30 | static struct cpuidle_governor * __cpuidle_find_governor(const char *str) |
| 31 | { |
| 32 | struct cpuidle_governor *gov; |
| 33 | |
| 34 | list_for_each_entry(gov, &cpuidle_governors, governor_list) |
Rasmus Villemoes | 9133664 | 2014-09-16 22:51:26 +0200 | [diff] [blame] | 35 | if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN)) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 36 | return gov; |
| 37 | |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * cpuidle_switch_governor - changes the governor |
| 43 | * @gov: the new target governor |
Uwe Kleine-König | 21ae295 | 2009-10-07 15:21:09 +0200 | [diff] [blame] | 44 | * Must be called with cpuidle_lock acquired. |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 45 | */ |
| 46 | int cpuidle_switch_governor(struct cpuidle_governor *gov) |
| 47 | { |
| 48 | struct cpuidle_device *dev; |
| 49 | |
gaurav jindal | 3cfd68b | 2018-01-05 14:01:30 +0100 | [diff] [blame] | 50 | if (!gov) |
| 51 | return -EINVAL; |
| 52 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 53 | if (gov == cpuidle_curr_governor) |
| 54 | return 0; |
| 55 | |
| 56 | cpuidle_uninstall_idle_handler(); |
| 57 | |
| 58 | if (cpuidle_curr_governor) { |
| 59 | list_for_each_entry(dev, &cpuidle_detected_devices, device_list) |
| 60 | cpuidle_disable_device(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | cpuidle_curr_governor = gov; |
| 64 | |
| 65 | if (gov) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 66 | list_for_each_entry(dev, &cpuidle_detected_devices, device_list) |
| 67 | cpuidle_enable_device(dev); |
| 68 | cpuidle_install_idle_handler(); |
| 69 | printk(KERN_INFO "cpuidle: using governor %s\n", gov->name); |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * cpuidle_register_governor - registers a governor |
| 77 | * @gov: the governor |
| 78 | */ |
| 79 | int cpuidle_register_governor(struct cpuidle_governor *gov) |
| 80 | { |
| 81 | int ret = -EEXIST; |
| 82 | |
| 83 | if (!gov || !gov->select) |
| 84 | return -EINVAL; |
| 85 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 86 | if (cpuidle_disabled()) |
| 87 | return -ENODEV; |
| 88 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 89 | mutex_lock(&cpuidle_lock); |
| 90 | if (__cpuidle_find_governor(gov->name) == NULL) { |
| 91 | ret = 0; |
Rafael J. Wysocki | 22782b3 | 2019-03-12 19:13:13 +0100 | [diff] [blame] | 92 | list_add_tail(&gov->governor_list, &cpuidle_governors); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 93 | if (!cpuidle_curr_governor || |
Rafael J. Wysocki | 61cb575 | 2018-12-05 23:45:34 +0100 | [diff] [blame] | 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))) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 98 | cpuidle_switch_governor(gov); |
| 99 | } |
| 100 | mutex_unlock(&cpuidle_lock); |
| 101 | |
| 102 | return ret; |
| 103 | } |
Rafael J. Wysocki | 0fc784f | 2018-05-30 13:43:01 +0200 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * cpuidle_governor_latency_req - Compute a latency constraint for CPU |
| 107 | * @cpu: Target CPU |
| 108 | */ |
| 109 | int cpuidle_governor_latency_req(unsigned int cpu) |
| 110 | { |
| 111 | int global_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
| 112 | struct device *device = get_cpu_device(cpu); |
| 113 | int device_req = dev_pm_qos_raw_read_value(device); |
| 114 | |
| 115 | return device_req < global_req ? device_req : global_req; |
| 116 | } |