^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * governor.c - governor support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Shaohua Li <shaohua.li@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Adam Belay <abelay@novell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This code is licenced under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pm_qos.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "cpuidle.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) char param_governor[CPUIDLE_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) LIST_HEAD(cpuidle_governors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct cpuidle_governor *cpuidle_curr_governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct cpuidle_governor *cpuidle_prev_governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * cpuidle_find_governor - finds a governor of the specified name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @str: the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Must be called with cpuidle_lock acquired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct cpuidle_governor *cpuidle_find_governor(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct cpuidle_governor *gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) list_for_each_entry(gov, &cpuidle_governors, governor_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * cpuidle_switch_governor - changes the governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @gov: the new target governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Must be called with cpuidle_lock acquired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int cpuidle_switch_governor(struct cpuidle_governor *gov)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (!gov)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (gov == cpuidle_curr_governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) cpuidle_uninstall_idle_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (cpuidle_curr_governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) cpuidle_disable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) cpuidle_curr_governor = gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (gov) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) cpuidle_enable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) cpuidle_install_idle_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * cpuidle_register_governor - registers a governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @gov: the governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int cpuidle_register_governor(struct cpuidle_governor *gov)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!gov || !gov->select)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (cpuidle_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) mutex_lock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (cpuidle_find_governor(gov->name) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) list_add_tail(&gov->governor_list, &cpuidle_governors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!cpuidle_curr_governor ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) !strncasecmp(param_governor, gov->name, CPUIDLE_NAME_LEN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) (cpuidle_curr_governor->rating < gov->rating &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) strncasecmp(param_governor, cpuidle_curr_governor->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) CPUIDLE_NAME_LEN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) cpuidle_switch_governor(gov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) mutex_unlock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) EXPORT_SYMBOL_GPL(cpuidle_register_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * cpuidle_governor_latency_req - Compute a latency constraint for CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * @cpu: Target CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) s64 cpuidle_governor_latency_req(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct device *device = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int device_req = dev_pm_qos_raw_resume_latency(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int global_req = cpu_latency_qos_limit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (device_req > global_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) device_req = global_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return (s64)device_req * NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) EXPORT_SYMBOL_GPL(cpuidle_governor_latency_req);