^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * cpuidle.c - core cpuidle infrastructure
^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/clockchips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.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/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched/clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pm_qos.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) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/ktime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <trace/events/power.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <trace/hooks/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "cpuidle.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) DEFINE_MUTEX(cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) LIST_HEAD(cpuidle_detected_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int enabled_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int off __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int initialized __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int cpuidle_disabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void disable_cpuidle(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) off = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) bool cpuidle_not_available(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return off || !initialized || !drv || !dev || !dev->enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * cpuidle_play_dead - cpu off-lining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Returns in case of an error or no driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int cpuidle_play_dead(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Find lowest-power state that supports long-term idle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) for (i = drv->state_count - 1; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (drv->states[i].enter_dead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return drv->states[i].enter_dead(dev, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static int find_deepest_state(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) u64 max_latency_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned int forbidden_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bool s2idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) u64 latency_req = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (i = 1; i < drv->state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct cpuidle_state *s = &drv->states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (dev->states_usage[i].disable ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) s->exit_latency_ns <= latency_req ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) s->exit_latency_ns > max_latency_ns ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) (s->flags & forbidden_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) (s2idle && !s->enter_s2idle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) latency_req = s->exit_latency_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ret = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return ret;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * cpuidle_use_deepest_state - Set/unset governor override mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * state with exit latency within @latency_limit_ns (override governors going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * forward), or do not override governors if it is zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) void cpuidle_use_deepest_state(u64 latency_limit_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) dev = cpuidle_get_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) dev->forced_idle_latency_limit_ns = latency_limit_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * cpuidle_find_deepest_state - Find the deepest available idle state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * @drv: cpuidle driver for the given CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * @dev: cpuidle device for the given CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * @latency_limit_ns: Idle state exit latency limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Return: the index of the deepest available idle state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) u64 latency_limit_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #ifdef CONFIG_SUSPEND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static void enter_s2idle_proper(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct cpuidle_device *dev, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ktime_t time_start, time_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct cpuidle_state *target_state = &drv->states[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) time_start = ns_to_ktime(local_clock());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) tick_freeze();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * The state used here cannot be a "coupled" one, because the "coupled"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * cpuidle mechanism enables interrupts and doing that with timekeeping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * suspended is generally unsafe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) stop_critical_timings();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) rcu_idle_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) target_state->enter_s2idle(dev, drv, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (WARN_ON_ONCE(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) rcu_idle_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) tick_unfreeze();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) start_critical_timings();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) time_end = ns_to_ktime(local_clock());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) dev->states_usage[index].s2idle_usage++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^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) * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * @drv: cpuidle driver for the given CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * @dev: cpuidle device for the given CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * If there are states with the ->enter_s2idle callback, find the deepest of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * them and enter it with frozen tick.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * Find the deepest state with ->enter_s2idle present, which guarantees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * that interrupts won't be enabled when it exits and allows the tick to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * be frozen safely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) index = find_deepest_state(drv, dev, U64_MAX, 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (index > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) enter_s2idle_proper(drv, dev, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #endif /* CONFIG_SUSPEND */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * cpuidle_enter_state - enter the state and update stats
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @dev: cpuidle device for this cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @drv: cpuidle driver for this cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * @index: index into the states table in @drv of the state to enter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int entered_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct cpuidle_state *target_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) bool broadcast;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ktime_t time_start, time_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * The vendor hook may modify index, which means target_state and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * broadcast must be assigned after the vendor hook.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) trace_android_vh_cpu_idle_enter(&index, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) target_state = &drv->states[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * Tell the time framework to switch to a broadcast timer because our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * local timer will be shut down. If a local timer is used from another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * CPU as a broadcast timer, this call may fail if it is not available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (broadcast && tick_broadcast_enter()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) CPUIDLE_FLAG_TIMER_STOP, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (index < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) default_idle_call();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) target_state = &drv->states[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) broadcast = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) leave_mm(dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* Take note of the planned idle state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) sched_idle_set_state(target_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) trace_cpu_idle(index, dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) time_start = ns_to_ktime(local_clock());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) stop_critical_timings();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) rcu_idle_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) entered_state = target_state->enter(dev, drv, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) rcu_idle_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) start_critical_timings();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) sched_clock_idle_wakeup_event();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) time_end = ns_to_ktime(local_clock());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) trace_android_vh_cpu_idle_exit(entered_state, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* The cpu is no longer idle or about to enter idle. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) sched_idle_set_state(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (broadcast) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (WARN_ON_ONCE(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) tick_broadcast_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!cpuidle_state_is_coupled(drv, index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (entered_state >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) s64 diff, delay = drv->states[entered_state].exit_latency_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * Update cpuidle counters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * This can be moved to within driver enter routine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * but that results in multiple copies of same code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) diff = ktime_sub(time_end, time_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) dev->last_residency_ns = diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dev->states_usage[entered_state].time_ns += diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) dev->states_usage[entered_state].usage++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (diff < drv->states[entered_state].target_residency_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) for (i = entered_state - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (dev->states_usage[i].disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /* Shallower states are enabled, so update. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) dev->states_usage[entered_state].above++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) } else if (diff > delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) for (i = entered_state + 1; i < drv->state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (dev->states_usage[i].disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * Update if a deeper state would have been a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * better match for the observed idle duration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (diff - delay >= drv->states[i].target_residency_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) dev->states_usage[entered_state].below++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) dev->last_residency_ns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) dev->states_usage[index].rejected++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return entered_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * cpuidle_select - ask the cpuidle framework to choose an idle state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * @drv: the cpuidle driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * @dev: the cpuidle device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * @stop_tick: indication on whether or not to stop the tick
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * Returns the index of the idle state. The return value must not be negative.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * The memory location pointed to by @stop_tick is expected to be written the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * 'false' boolean value if the scheduler tick should not be stopped before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * entering the returned state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) bool *stop_tick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return cpuidle_curr_governor->select(drv, dev, stop_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * cpuidle_enter - enter into the specified idle state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * @drv: the cpuidle driver tied with the cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * @dev: the cpuidle device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * @index: the index in the idle state table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * Returns the index in the idle state, < 0 in case of error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * The error code depends on the backend driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * Store the next hrtimer, which becomes either next tick or the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * timer event, whatever expires first. Additionally, to make this data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * useful for consumers outside cpuidle, we rely on that the governor's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * ->select() callback have decided, whether to stop the tick or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (cpuidle_state_is_coupled(drv, index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ret = cpuidle_enter_state_coupled(dev, drv, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ret = cpuidle_enter_state(dev, drv, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) WRITE_ONCE(dev->next_hrtimer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * cpuidle_reflect - tell the underlying governor what was the state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * we were in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * @dev : the cpuidle device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * @index: the index in the idle state table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) void cpuidle_reflect(struct cpuidle_device *dev, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (cpuidle_curr_governor->reflect && index >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) cpuidle_curr_governor->reflect(dev, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * cpuidle_poll_time - return amount of time to poll for,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * governors can override dev->poll_limit_ns if necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * @drv: the cpuidle driver tied with the cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * @dev: the cpuidle device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) u64 cpuidle_poll_time(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) u64 limit_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (dev->poll_limit_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return dev->poll_limit_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) limit_ns = TICK_NSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) for (i = 1; i < drv->state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (dev->states_usage[i].disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) limit_ns = drv->states[i].target_residency_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) dev->poll_limit_ns = limit_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return dev->poll_limit_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) void cpuidle_install_idle_handler(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (enabled_devices) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* Make sure all changes finished before we switch to new idle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) initialized = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) void cpuidle_uninstall_idle_handler(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (enabled_devices) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) initialized = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) wake_up_all_online_idle_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * Make sure external observers (such as the scheduler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * are done looking at pointed idle states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * cpuidle_pause_and_lock - temporarily disables CPUIDLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) void cpuidle_pause_and_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) mutex_lock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) cpuidle_uninstall_idle_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * cpuidle_resume_and_unlock - resumes CPUIDLE operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) void cpuidle_resume_and_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) cpuidle_install_idle_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) mutex_unlock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /* Currently used in suspend/resume path to suspend cpuidle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) void cpuidle_pause(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) mutex_lock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) cpuidle_uninstall_idle_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) mutex_unlock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) /* Currently used in suspend/resume path to resume cpuidle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) void cpuidle_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) mutex_lock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) cpuidle_install_idle_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) mutex_unlock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * cpuidle_enable_device - enables idle PM for a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * @dev: the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * This function must be called between cpuidle_pause_and_lock and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * cpuidle_resume_and_unlock when used externally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int cpuidle_enable_device(struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct cpuidle_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (dev->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (!cpuidle_curr_governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) drv = cpuidle_get_cpu_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (!drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (!dev->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) ret = cpuidle_add_device_sysfs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (cpuidle_curr_governor->enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) ret = cpuidle_curr_governor->enable(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) goto fail_sysfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) dev->enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) enabled_devices++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) fail_sysfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) cpuidle_remove_device_sysfs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) EXPORT_SYMBOL_GPL(cpuidle_enable_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * cpuidle_disable_device - disables idle PM for a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * @dev: the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * This function must be called between cpuidle_pause_and_lock and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * cpuidle_resume_and_unlock when used externally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) void cpuidle_disable_device(struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (!dev || !dev->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (!drv || !cpuidle_curr_governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) dev->enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (cpuidle_curr_governor->disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) cpuidle_curr_governor->disable(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) cpuidle_remove_device_sysfs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) enabled_devices--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) EXPORT_SYMBOL_GPL(cpuidle_disable_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static void __cpuidle_unregister_device(struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) list_del(&dev->device_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) per_cpu(cpuidle_devices, dev->cpu) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) module_put(drv->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) dev->registered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static void __cpuidle_device_init(struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) memset(dev->states_usage, 0, sizeof(dev->states_usage));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) dev->last_residency_ns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) dev->next_hrtimer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * __cpuidle_register_device - internal register function called before register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * and enable routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * @dev: the cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * cpuidle_lock mutex must be held before this is called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static int __cpuidle_register_device(struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (!try_module_get(drv->owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) for (i = 0; i < drv->state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) per_cpu(cpuidle_devices, dev->cpu) = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) list_add(&dev->device_list, &cpuidle_detected_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ret = cpuidle_coupled_register_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) __cpuidle_unregister_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) dev->registered = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * cpuidle_register_device - registers a CPU's idle PM feature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * @dev: the cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) int cpuidle_register_device(struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) int ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) mutex_lock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (dev->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) __cpuidle_device_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) ret = __cpuidle_register_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) ret = cpuidle_add_sysfs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) goto out_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) ret = cpuidle_enable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) goto out_sysfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) cpuidle_install_idle_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) mutex_unlock(&cpuidle_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) out_sysfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) cpuidle_remove_sysfs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) out_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) __cpuidle_unregister_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) EXPORT_SYMBOL_GPL(cpuidle_register_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * cpuidle_unregister_device - unregisters a CPU's idle PM feature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * @dev: the cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) void cpuidle_unregister_device(struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) if (!dev || dev->registered == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) cpuidle_pause_and_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) cpuidle_disable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) cpuidle_remove_sysfs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) __cpuidle_unregister_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) cpuidle_coupled_unregister_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) cpuidle_resume_and_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * cpuidle_unregister: unregister a driver and the devices. This function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * can be used only if the driver has been previously registered through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * the cpuidle_register function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * @drv: a valid pointer to a struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) void cpuidle_unregister(struct cpuidle_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) struct cpuidle_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) for_each_cpu(cpu, drv->cpumask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) device = &per_cpu(cpuidle_dev, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) cpuidle_unregister_device(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) cpuidle_unregister_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) EXPORT_SYMBOL_GPL(cpuidle_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) * cpuidle_register: registers the driver and the cpu devices with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * coupled_cpus passed as parameter. This function is used for all common
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * initialization pattern there are in the arch specific drivers. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * devices is globally defined in this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * @drv : a valid pointer to a struct cpuidle_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * @coupled_cpus: a cpumask for the coupled states
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * Returns 0 on success, < 0 otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) int cpuidle_register(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) const struct cpumask *const coupled_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) int ret, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) struct cpuidle_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) ret = cpuidle_register_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) pr_err("failed to register cpuidle driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) for_each_cpu(cpu, drv->cpumask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) device = &per_cpu(cpuidle_dev, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) device->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * On multiplatform for ARM, the coupled idle states could be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * enabled in the kernel even if the cpuidle driver does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * use it. Note, coupled_cpus is a struct copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (coupled_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) device->coupled_cpus = *coupled_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) ret = cpuidle_register_device(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) cpuidle_unregister(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) EXPORT_SYMBOL_GPL(cpuidle_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * cpuidle_init - core initializer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static int __init cpuidle_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (cpuidle_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) return cpuidle_add_interface(cpu_subsys.dev_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) module_param(off, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) core_initcall(cpuidle_init);