^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * driver.c - driver 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/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sched/idle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "cpuidle.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) DEFINE_SPINLOCK(cpuidle_driver_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * __cpuidle_get_cpu_driver - return the cpuidle driver tied to a CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @cpu: the CPU handled by the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Returns a pointer to struct cpuidle_driver or NULL if no driver has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * registered for @cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return per_cpu(cpuidle_drivers, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * __cpuidle_unset_driver - unset per CPU driver variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @drv: a valid pointer to a struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * For each CPU in the driver's CPU mask, unset the registered driver per CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * variable. If @drv is different from the registered driver, the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * variable is not cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for_each_cpu(cpu, drv->cpumask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (drv != __cpuidle_get_cpu_driver(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) per_cpu(cpuidle_drivers, cpu) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * __cpuidle_set_driver - set per CPU driver variables for the given driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @drv: a valid pointer to a struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Returns 0 on success, -EBUSY if any CPU in the cpumask have a driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * different from drv already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) for_each_cpu(cpu, drv->cpumask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct cpuidle_driver *old_drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) old_drv = __cpuidle_get_cpu_driver(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (old_drv && old_drv != drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) for_each_cpu(cpu, drv->cpumask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) per_cpu(cpuidle_drivers, cpu) = drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static struct cpuidle_driver *cpuidle_curr_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * __cpuidle_get_cpu_driver - return the global cpuidle driver pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * @cpu: ignored without the multiple driver support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * Return a pointer to a struct cpuidle_driver object or NULL if no driver was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * previously registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return cpuidle_curr_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * __cpuidle_set_driver - assign the global cpuidle driver variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * @drv: pointer to a struct cpuidle_driver object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * Returns 0 on success, -EBUSY if the driver is already registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (cpuidle_curr_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) cpuidle_curr_driver = drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * __cpuidle_unset_driver - unset the global cpuidle driver variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @drv: a pointer to a struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Reset the global cpuidle variable to NULL. If @drv does not match the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * registered driver, do nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (drv == cpuidle_curr_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) cpuidle_curr_driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * cpuidle_setup_broadcast_timer - enable/disable the broadcast timer on a cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @arg: a void pointer used to match the SMP cross call API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * If @arg is NULL broadcast is disabled otherwise enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * This function is executed per CPU by an SMP cross call. It's not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * supposed to be called directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void cpuidle_setup_broadcast_timer(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) tick_broadcast_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) tick_broadcast_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * __cpuidle_driver_init - initialize the driver's internal data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * @drv: a valid pointer to a struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static void __cpuidle_driver_init(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * Use all possible CPUs as the default, because if the kernel boots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * with some CPUs offline and then we online one of them, the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * notifier has to know which driver to assign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!drv->cpumask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) drv->cpumask = (struct cpumask *)cpu_possible_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) for (i = 0; i < drv->state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct cpuidle_state *s = &drv->states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * Look for the timer stop flag in the different states and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * it is found, indicate that the broadcast timer has to be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (s->flags & CPUIDLE_FLAG_TIMER_STOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) drv->bctimer = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * The core will use the target residency and exit latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * values in nanoseconds, but allow drivers to provide them in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * microseconds too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (s->target_residency > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) s->target_residency_ns = s->target_residency * NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (s->exit_latency > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * __cpuidle_register_driver: register the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * @drv: a valid pointer to a struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * Do some sanity checks, initialize the driver, assign the driver to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * global cpuidle driver variable(s) and set up the broadcast timer if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * cpuidle driver has some states that shut down the local timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * Returns 0 on success, a negative error code otherwise:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * * -EINVAL if the driver pointer is NULL or no idle states are available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * * -ENODEV if the cpuidle framework is disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * * -EBUSY if the driver is already assigned to the global variable(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int __cpuidle_register_driver(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (!drv || !drv->state_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ret = cpuidle_coupled_state_verify(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (cpuidle_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) __cpuidle_driver_init(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ret = __cpuidle_set_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (drv->bctimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) (void *)1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * __cpuidle_unregister_driver - unregister the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * @drv: a valid pointer to a struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * Check if the driver is no longer in use, reset the global cpuidle driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * variable(s) and disable the timer broadcast notification mechanism if it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (drv->bctimer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) drv->bctimer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) __cpuidle_unset_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * cpuidle_register_driver - registers a driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * @drv: a pointer to a valid struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * Register the driver under a lock to prevent concurrent attempts to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * [un]register the driver from occuring at the same time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * Returns 0 on success, a negative error code (returned by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * __cpuidle_register_driver()) otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int cpuidle_register_driver(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct cpuidle_governor *gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) spin_lock(&cpuidle_driver_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ret = __cpuidle_register_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) spin_unlock(&cpuidle_driver_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!ret && !strlen(param_governor) && drv->governor &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) (cpuidle_get_driver() == drv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) mutex_lock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) gov = cpuidle_find_governor(drv->governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (gov) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) cpuidle_prev_governor = cpuidle_curr_governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (cpuidle_switch_governor(gov) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) cpuidle_prev_governor = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) mutex_unlock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) EXPORT_SYMBOL_GPL(cpuidle_register_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * cpuidle_unregister_driver - unregisters a driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * @drv: a pointer to a valid struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * Unregisters the cpuidle driver under a lock to prevent concurrent attempts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * to [un]register the driver from occuring at the same time. @drv has to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * match the currently registered driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) void cpuidle_unregister_driver(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) bool enabled = (cpuidle_get_driver() == drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) spin_lock(&cpuidle_driver_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) __cpuidle_unregister_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) spin_unlock(&cpuidle_driver_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) mutex_lock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (cpuidle_prev_governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (!cpuidle_switch_governor(cpuidle_prev_governor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) cpuidle_prev_governor = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) mutex_unlock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * cpuidle_get_driver - return the driver tied to the current CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct cpuidle_driver *cpuidle_get_driver(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct cpuidle_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) cpu = get_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) drv = __cpuidle_get_cpu_driver(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) put_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) EXPORT_SYMBOL_GPL(cpuidle_get_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * cpuidle_get_cpu_driver - return the driver registered for a CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * @dev: a valid pointer to a struct cpuidle_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * for the CPU associated with @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return __cpuidle_get_cpu_driver(dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * cpuidle_driver_state_disabled - Disable or enable an idle state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * @drv: cpuidle driver owning the state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * @idx: State index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * @disable: Whether or not to disable the state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) bool disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) mutex_lock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) spin_lock(&cpuidle_driver_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (!drv->cpumask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) drv->states[idx].flags |= CPUIDLE_FLAG_UNUSABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) for_each_cpu(cpu, drv->cpumask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) dev->states_usage[idx].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) dev->states_usage[idx].disable &= ~CPUIDLE_STATE_DISABLED_BY_DRIVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) spin_unlock(&cpuidle_driver_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) mutex_unlock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) EXPORT_SYMBOL_GPL(cpuidle_driver_state_disabled);