14f86d3a8SLen Brown /* 24f86d3a8SLen Brown * cpuidle.c - core cpuidle infrastructure 34f86d3a8SLen Brown * 44f86d3a8SLen Brown * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> 54f86d3a8SLen Brown * Shaohua Li <shaohua.li@intel.com> 64f86d3a8SLen Brown * Adam Belay <abelay@novell.com> 74f86d3a8SLen Brown * 84f86d3a8SLen Brown * This code is licenced under the GPL. 94f86d3a8SLen Brown */ 104f86d3a8SLen Brown 11b60e6a0eSDaniel Lezcano #include <linux/clockchips.h> 124f86d3a8SLen Brown #include <linux/kernel.h> 134f86d3a8SLen Brown #include <linux/mutex.h> 144f86d3a8SLen Brown #include <linux/sched.h> 15e6017571SIngo Molnar #include <linux/sched/clock.h> 164f86d3a8SLen Brown #include <linux/notifier.h> 17e8db0be1SJean Pihet #include <linux/pm_qos.h> 184f86d3a8SLen Brown #include <linux/cpu.h> 194f86d3a8SLen Brown #include <linux/cpuidle.h> 209a0b8415Svenkatesh.pallipadi@intel.com #include <linux/ktime.h> 212e94d1f7SArjan van de Ven #include <linux/hrtimer.h> 22884b17e1SPaul Gortmaker #include <linux/module.h> 2338106313SRafael J. Wysocki #include <linux/suspend.h> 24124cf911SRafael J. Wysocki #include <linux/tick.h> 25288f023eSArjan van de Ven #include <trace/events/power.h> 264f86d3a8SLen Brown 274f86d3a8SLen Brown #include "cpuidle.h" 284f86d3a8SLen Brown 294f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); 304c637b21SDaniel Lezcano DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev); 314f86d3a8SLen Brown 324f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock); 334f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices); 344f86d3a8SLen Brown 354f86d3a8SLen Brown static int enabled_devices; 3662027aeaSLen Brown static int off __read_mostly; 37a0bfa137SLen Brown static int initialized __read_mostly; 3862027aeaSLen Brown 3962027aeaSLen Brown int cpuidle_disabled(void) 4062027aeaSLen Brown { 4162027aeaSLen Brown return off; 4262027aeaSLen Brown } 43d91ee586SLen Brown void disable_cpuidle(void) 44d91ee586SLen Brown { 45d91ee586SLen Brown off = 1; 46d91ee586SLen Brown } 474f86d3a8SLen Brown 48ef2b22acSRafael J. Wysocki bool cpuidle_not_available(struct cpuidle_driver *drv, 4931a34090SRafael J. Wysocki struct cpuidle_device *dev) 5031a34090SRafael J. Wysocki { 5131a34090SRafael J. Wysocki return off || !initialized || !drv || !dev || !dev->enabled; 5231a34090SRafael J. Wysocki } 5331a34090SRafael J. Wysocki 544f86d3a8SLen Brown /** 551a022e3fSBoris Ostrovsky * cpuidle_play_dead - cpu off-lining 561a022e3fSBoris Ostrovsky * 57ee01e663SToshi Kani * Returns in case of an error or no driver 581a022e3fSBoris Ostrovsky */ 591a022e3fSBoris Ostrovsky int cpuidle_play_dead(void) 601a022e3fSBoris Ostrovsky { 611a022e3fSBoris Ostrovsky struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); 62bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 638aef33a7SDaniel Lezcano int i; 641a022e3fSBoris Ostrovsky 65ee01e663SToshi Kani if (!drv) 66ee01e663SToshi Kani return -ENODEV; 67ee01e663SToshi Kani 681a022e3fSBoris Ostrovsky /* Find lowest-power state that supports long-term idle */ 697d51d979SRafael J. Wysocki for (i = drv->state_count - 1; i >= 0; i--) 708aef33a7SDaniel Lezcano if (drv->states[i].enter_dead) 718aef33a7SDaniel Lezcano return drv->states[i].enter_dead(dev, i); 721a022e3fSBoris Ostrovsky 731a022e3fSBoris Ostrovsky return -ENODEV; 741a022e3fSBoris Ostrovsky } 751a022e3fSBoris Ostrovsky 76ef2b22acSRafael J. Wysocki static int find_deepest_state(struct cpuidle_driver *drv, 770d94039fSRafael J. Wysocki struct cpuidle_device *dev, 780d94039fSRafael J. Wysocki unsigned int max_latency, 790d94039fSRafael J. Wysocki unsigned int forbidden_flags, 8028ba086eSRafael J. Wysocki bool s2idle) 81a6220fc1SRafael J. Wysocki { 82a6220fc1SRafael J. Wysocki unsigned int latency_req = 0; 8351164251SRafael J. Wysocki int i, ret = 0; 84a6220fc1SRafael J. Wysocki 8551164251SRafael J. Wysocki for (i = 1; i < drv->state_count; i++) { 86a6220fc1SRafael J. Wysocki struct cpuidle_state *s = &drv->states[i]; 87a6220fc1SRafael J. Wysocki struct cpuidle_state_usage *su = &dev->states_usage[i]; 88a6220fc1SRafael J. Wysocki 89124cf911SRafael J. Wysocki if (s->disabled || su->disable || s->exit_latency <= latency_req 900d94039fSRafael J. Wysocki || s->exit_latency > max_latency 910d94039fSRafael J. Wysocki || (s->flags & forbidden_flags) 9228ba086eSRafael J. Wysocki || (s2idle && !s->enter_s2idle)) 93a6220fc1SRafael J. Wysocki continue; 94a6220fc1SRafael J. Wysocki 95a6220fc1SRafael J. Wysocki latency_req = s->exit_latency; 96a6220fc1SRafael J. Wysocki ret = i; 97a6220fc1SRafael J. Wysocki } 98a6220fc1SRafael J. Wysocki return ret; 99a6220fc1SRafael J. Wysocki } 100a6220fc1SRafael J. Wysocki 1010e7414b7SRafael J. Wysocki /** 1020e7414b7SRafael J. Wysocki * cpuidle_use_deepest_state - Set/clear governor override flag. 1030e7414b7SRafael J. Wysocki * @enable: New value of the flag. 1040e7414b7SRafael J. Wysocki * 1050e7414b7SRafael J. Wysocki * Set/unset the current CPU to use the deepest idle state (override governors 1060e7414b7SRafael J. Wysocki * going forward if set). 1070e7414b7SRafael J. Wysocki */ 108bb8313b6SJacob Pan void cpuidle_use_deepest_state(bool enable) 109bb8313b6SJacob Pan { 110bb8313b6SJacob Pan struct cpuidle_device *dev; 111bb8313b6SJacob Pan 112bb8313b6SJacob Pan preempt_disable(); 113bb8313b6SJacob Pan dev = cpuidle_get_device(); 11441dc750eSLi, Fei if (dev) 115bb8313b6SJacob Pan dev->use_deepest_state = enable; 116bb8313b6SJacob Pan preempt_enable(); 117bb8313b6SJacob Pan } 118bb8313b6SJacob Pan 119ef2b22acSRafael J. Wysocki /** 120ef2b22acSRafael J. Wysocki * cpuidle_find_deepest_state - Find the deepest available idle state. 121ef2b22acSRafael J. Wysocki * @drv: cpuidle driver for the given CPU. 122ef2b22acSRafael J. Wysocki * @dev: cpuidle device for the given CPU. 123ef2b22acSRafael J. Wysocki */ 124ef2b22acSRafael J. Wysocki int cpuidle_find_deepest_state(struct cpuidle_driver *drv, 125ef2b22acSRafael J. Wysocki struct cpuidle_device *dev) 126ef2b22acSRafael J. Wysocki { 1270d94039fSRafael J. Wysocki return find_deepest_state(drv, dev, UINT_MAX, 0, false); 128ef2b22acSRafael J. Wysocki } 129ef2b22acSRafael J. Wysocki 130bb8313b6SJacob Pan #ifdef CONFIG_SUSPEND 13128ba086eSRafael J. Wysocki static void enter_s2idle_proper(struct cpuidle_driver *drv, 132124cf911SRafael J. Wysocki struct cpuidle_device *dev, int index) 133124cf911SRafael J. Wysocki { 134ae0afb4fSRafael J. Wysocki /* 135ae0afb4fSRafael J. Wysocki * trace_suspend_resume() called by tick_freeze() for the last CPU 136ae0afb4fSRafael J. Wysocki * executing it contains RCU usage regarded as invalid in the idle 137ae0afb4fSRafael J. Wysocki * context, so tell RCU about that. 138ae0afb4fSRafael J. Wysocki */ 139ae0afb4fSRafael J. Wysocki RCU_NONIDLE(tick_freeze()); 140124cf911SRafael J. Wysocki /* 141124cf911SRafael J. Wysocki * The state used here cannot be a "coupled" one, because the "coupled" 142124cf911SRafael J. Wysocki * cpuidle mechanism enables interrupts and doing that with timekeeping 143124cf911SRafael J. Wysocki * suspended is generally unsafe. 144124cf911SRafael J. Wysocki */ 14563caae84SLucas Stach stop_critical_timings(); 14628ba086eSRafael J. Wysocki drv->states[index].enter_s2idle(dev, drv, index); 147124cf911SRafael J. Wysocki WARN_ON(!irqs_disabled()); 148124cf911SRafael J. Wysocki /* 149124cf911SRafael J. Wysocki * timekeeping_resume() that will be called by tick_unfreeze() for the 150ae0afb4fSRafael J. Wysocki * first CPU executing it calls functions containing RCU read-side 151124cf911SRafael J. Wysocki * critical sections, so tell RCU about that. 152124cf911SRafael J. Wysocki */ 153124cf911SRafael J. Wysocki RCU_NONIDLE(tick_unfreeze()); 15463caae84SLucas Stach start_critical_timings(); 155124cf911SRafael J. Wysocki } 156124cf911SRafael J. Wysocki 157a6220fc1SRafael J. Wysocki /** 15828ba086eSRafael J. Wysocki * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle. 159ef2b22acSRafael J. Wysocki * @drv: cpuidle driver for the given CPU. 160ef2b22acSRafael J. Wysocki * @dev: cpuidle device for the given CPU. 16138106313SRafael J. Wysocki * 16228ba086eSRafael J. Wysocki * If there are states with the ->enter_s2idle callback, find the deepest of 163ef2b22acSRafael J. Wysocki * them and enter it with frozen tick. 16438106313SRafael J. Wysocki */ 16528ba086eSRafael J. Wysocki int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev) 16638106313SRafael J. Wysocki { 16738106313SRafael J. Wysocki int index; 16838106313SRafael J. Wysocki 169124cf911SRafael J. Wysocki /* 17028ba086eSRafael J. Wysocki * Find the deepest state with ->enter_s2idle present, which guarantees 171124cf911SRafael J. Wysocki * that interrupts won't be enabled when it exits and allows the tick to 172124cf911SRafael J. Wysocki * be frozen safely. 173124cf911SRafael J. Wysocki */ 1740d94039fSRafael J. Wysocki index = find_deepest_state(drv, dev, UINT_MAX, 0, true); 1756f16886bSSudeep Holla if (index > 0) 17628ba086eSRafael J. Wysocki enter_s2idle_proper(drv, dev, index); 177124cf911SRafael J. Wysocki 178ef2b22acSRafael J. Wysocki return index; 17938106313SRafael J. Wysocki } 18087e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */ 18138106313SRafael J. Wysocki 18238106313SRafael J. Wysocki /** 18356cfbf74SColin Cross * cpuidle_enter_state - enter the state and update stats 18456cfbf74SColin Cross * @dev: cpuidle device for this cpu 18556cfbf74SColin Cross * @drv: cpuidle driver for this cpu 1867312280bSRafael J. Wysocki * @index: index into the states table in @drv of the state to enter 18756cfbf74SColin Cross */ 18856cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, 189554c06baSDaniel Lezcano int index) 19056cfbf74SColin Cross { 19156cfbf74SColin Cross int entered_state; 19256cfbf74SColin Cross 193554c06baSDaniel Lezcano struct cpuidle_state *target_state = &drv->states[index]; 194df8d9eeaSRafael J. Wysocki bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP); 195dbd1b8eaSShreyas B. Prabhu ktime_t time_start, time_end; 196554c06baSDaniel Lezcano s64 diff; 197554c06baSDaniel Lezcano 198df8d9eeaSRafael J. Wysocki /* 199df8d9eeaSRafael J. Wysocki * Tell the time framework to switch to a broadcast timer because our 200df8d9eeaSRafael J. Wysocki * local timer will be shut down. If a local timer is used from another 201df8d9eeaSRafael J. Wysocki * CPU as a broadcast timer, this call may fail if it is not available. 202df8d9eeaSRafael J. Wysocki */ 203827a5aefSRafael J. Wysocki if (broadcast && tick_broadcast_enter()) { 2040d94039fSRafael J. Wysocki index = find_deepest_state(drv, dev, target_state->exit_latency, 2050d94039fSRafael J. Wysocki CPUIDLE_FLAG_TIMER_STOP, false); 2060d94039fSRafael J. Wysocki if (index < 0) { 207827a5aefSRafael J. Wysocki default_idle_call(); 208df8d9eeaSRafael J. Wysocki return -EBUSY; 209827a5aefSRafael J. Wysocki } 2100d94039fSRafael J. Wysocki target_state = &drv->states[index]; 211*f187851bSNicholas Piggin broadcast = false; 2120d94039fSRafael J. Wysocki } 213df8d9eeaSRafael J. Wysocki 214faad3849SRafael J. Wysocki /* Take note of the planned idle state. */ 215faad3849SRafael J. Wysocki sched_idle_set_state(target_state); 216faad3849SRafael J. Wysocki 21730fe6884SSandeep Tripathy trace_cpu_idle_rcuidle(index, dev->cpu); 218dbd1b8eaSShreyas B. Prabhu time_start = ns_to_ktime(local_clock()); 219554c06baSDaniel Lezcano 22063caae84SLucas Stach stop_critical_timings(); 221554c06baSDaniel Lezcano entered_state = target_state->enter(dev, drv, index); 22263caae84SLucas Stach start_critical_timings(); 223554c06baSDaniel Lezcano 224f9fccdb9SPeter Zijlstra sched_clock_idle_wakeup_event(); 225dbd1b8eaSShreyas B. Prabhu time_end = ns_to_ktime(local_clock()); 22630fe6884SSandeep Tripathy trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu); 227554c06baSDaniel Lezcano 228faad3849SRafael J. Wysocki /* The cpu is no longer idle or about to enter idle. */ 229faad3849SRafael J. Wysocki sched_idle_set_state(NULL); 230faad3849SRafael J. Wysocki 231df8d9eeaSRafael J. Wysocki if (broadcast) { 232df8d9eeaSRafael J. Wysocki if (WARN_ON_ONCE(!irqs_disabled())) 233df8d9eeaSRafael J. Wysocki local_irq_disable(); 234df8d9eeaSRafael J. Wysocki 235df8d9eeaSRafael J. Wysocki tick_broadcast_exit(); 236df8d9eeaSRafael J. Wysocki } 237df8d9eeaSRafael J. Wysocki 238e7387da5SDaniel Lezcano if (!cpuidle_state_is_coupled(drv, index)) 239554c06baSDaniel Lezcano local_irq_enable(); 240554c06baSDaniel Lezcano 241dbd1b8eaSShreyas B. Prabhu diff = ktime_us_delta(time_end, time_start); 242554c06baSDaniel Lezcano if (diff > INT_MAX) 243554c06baSDaniel Lezcano diff = INT_MAX; 244554c06baSDaniel Lezcano 245554c06baSDaniel Lezcano dev->last_residency = (int) diff; 24656cfbf74SColin Cross 24756cfbf74SColin Cross if (entered_state >= 0) { 24856cfbf74SColin Cross /* Update cpuidle counters */ 24956cfbf74SColin Cross /* This can be moved to within driver enter routine 25056cfbf74SColin Cross * but that results in multiple copies of same code. 25156cfbf74SColin Cross */ 252a474a515SJulius Werner dev->states_usage[entered_state].time += dev->last_residency; 25356cfbf74SColin Cross dev->states_usage[entered_state].usage++; 25456cfbf74SColin Cross } else { 25556cfbf74SColin Cross dev->last_residency = 0; 25656cfbf74SColin Cross } 25756cfbf74SColin Cross 25856cfbf74SColin Cross return entered_state; 25956cfbf74SColin Cross } 26056cfbf74SColin Cross 26156cfbf74SColin Cross /** 262907e30f1SDaniel Lezcano * cpuidle_select - ask the cpuidle framework to choose an idle state 2634f86d3a8SLen Brown * 264907e30f1SDaniel Lezcano * @drv: the cpuidle driver 265907e30f1SDaniel Lezcano * @dev: the cpuidle device 266907e30f1SDaniel Lezcano * 26751164251SRafael J. Wysocki * Returns the index of the idle state. The return value must not be negative. 2684f86d3a8SLen Brown */ 269907e30f1SDaniel Lezcano int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev) 2704f86d3a8SLen Brown { 271907e30f1SDaniel Lezcano return cpuidle_curr_governor->select(drv, dev); 272246eb7f0SKevin Hilman } 273246eb7f0SKevin Hilman 274907e30f1SDaniel Lezcano /** 275907e30f1SDaniel Lezcano * cpuidle_enter - enter into the specified idle state 276907e30f1SDaniel Lezcano * 277907e30f1SDaniel Lezcano * @drv: the cpuidle driver tied with the cpu 278907e30f1SDaniel Lezcano * @dev: the cpuidle device 279907e30f1SDaniel Lezcano * @index: the index in the idle state table 280907e30f1SDaniel Lezcano * 281907e30f1SDaniel Lezcano * Returns the index in the idle state, < 0 in case of error. 282907e30f1SDaniel Lezcano * The error code depends on the backend driver 283907e30f1SDaniel Lezcano */ 284907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev, 285907e30f1SDaniel Lezcano int index) 286907e30f1SDaniel Lezcano { 2874c1ed5a6SXunlei Pang if (cpuidle_state_is_coupled(drv, index)) 288907e30f1SDaniel Lezcano return cpuidle_enter_state_coupled(dev, drv, index); 289907e30f1SDaniel Lezcano return cpuidle_enter_state(dev, drv, index); 290907e30f1SDaniel Lezcano } 291fb11c9c6SViresh Kumar 292907e30f1SDaniel Lezcano /** 293907e30f1SDaniel Lezcano * cpuidle_reflect - tell the underlying governor what was the state 294907e30f1SDaniel Lezcano * we were in 295907e30f1SDaniel Lezcano * 296907e30f1SDaniel Lezcano * @dev : the cpuidle device 297907e30f1SDaniel Lezcano * @index: the index in the idle state table 298907e30f1SDaniel Lezcano * 299907e30f1SDaniel Lezcano */ 300907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index) 301907e30f1SDaniel Lezcano { 302a802ea96SRafael J. Wysocki if (cpuidle_curr_governor->reflect && index >= 0) 303907e30f1SDaniel Lezcano cpuidle_curr_governor->reflect(dev, index); 3044f86d3a8SLen Brown } 3054f86d3a8SLen Brown 3064f86d3a8SLen Brown /** 3074f86d3a8SLen Brown * cpuidle_install_idle_handler - installs the cpuidle idle loop handler 3084f86d3a8SLen Brown */ 3094f86d3a8SLen Brown void cpuidle_install_idle_handler(void) 3104f86d3a8SLen Brown { 311a0bfa137SLen Brown if (enabled_devices) { 3124f86d3a8SLen Brown /* Make sure all changes finished before we switch to new idle */ 3134f86d3a8SLen Brown smp_wmb(); 314a0bfa137SLen Brown initialized = 1; 3154f86d3a8SLen Brown } 3164f86d3a8SLen Brown } 3174f86d3a8SLen Brown 3184f86d3a8SLen Brown /** 3194f86d3a8SLen Brown * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler 3204f86d3a8SLen Brown */ 3214f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void) 3224f86d3a8SLen Brown { 323a0bfa137SLen Brown if (enabled_devices) { 324a0bfa137SLen Brown initialized = 0; 3252ed903c5SChuansheng Liu wake_up_all_idle_cpus(); 3264f86d3a8SLen Brown } 327442bf3aaSDaniel Lezcano 328442bf3aaSDaniel Lezcano /* 329442bf3aaSDaniel Lezcano * Make sure external observers (such as the scheduler) 330442bf3aaSDaniel Lezcano * are done looking at pointed idle states. 331442bf3aaSDaniel Lezcano */ 332442bf3aaSDaniel Lezcano synchronize_rcu(); 3334f86d3a8SLen Brown } 3344f86d3a8SLen Brown 3354f86d3a8SLen Brown /** 3364f86d3a8SLen Brown * cpuidle_pause_and_lock - temporarily disables CPUIDLE 3374f86d3a8SLen Brown */ 3384f86d3a8SLen Brown void cpuidle_pause_and_lock(void) 3394f86d3a8SLen Brown { 3404f86d3a8SLen Brown mutex_lock(&cpuidle_lock); 3414f86d3a8SLen Brown cpuidle_uninstall_idle_handler(); 3424f86d3a8SLen Brown } 3434f86d3a8SLen Brown 3444f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); 3454f86d3a8SLen Brown 3464f86d3a8SLen Brown /** 3474f86d3a8SLen Brown * cpuidle_resume_and_unlock - resumes CPUIDLE operation 3484f86d3a8SLen Brown */ 3494f86d3a8SLen Brown void cpuidle_resume_and_unlock(void) 3504f86d3a8SLen Brown { 3514f86d3a8SLen Brown cpuidle_install_idle_handler(); 3524f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 3534f86d3a8SLen Brown } 3544f86d3a8SLen Brown 3554f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); 3564f86d3a8SLen Brown 3578651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */ 3588651f97bSPreeti U Murthy void cpuidle_pause(void) 3598651f97bSPreeti U Murthy { 3608651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 3618651f97bSPreeti U Murthy cpuidle_uninstall_idle_handler(); 3628651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 3638651f97bSPreeti U Murthy } 3648651f97bSPreeti U Murthy 3658651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */ 3668651f97bSPreeti U Murthy void cpuidle_resume(void) 3678651f97bSPreeti U Murthy { 3688651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 3698651f97bSPreeti U Murthy cpuidle_install_idle_handler(); 3708651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 3718651f97bSPreeti U Murthy } 3728651f97bSPreeti U Murthy 3734f86d3a8SLen Brown /** 3744f86d3a8SLen Brown * cpuidle_enable_device - enables idle PM for a CPU 3754f86d3a8SLen Brown * @dev: the CPU 3764f86d3a8SLen Brown * 3774f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 3784f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 3794f86d3a8SLen Brown */ 3804f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev) 3814f86d3a8SLen Brown { 3825df0aa73SDaniel Lezcano int ret; 383bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv; 3844f86d3a8SLen Brown 3851b0a0e9aSSrivatsa S. Bhat if (!dev) 3861b0a0e9aSSrivatsa S. Bhat return -EINVAL; 3871b0a0e9aSSrivatsa S. Bhat 3884f86d3a8SLen Brown if (dev->enabled) 3894f86d3a8SLen Brown return 0; 390bf4d1b5dSDaniel Lezcano 391bf4d1b5dSDaniel Lezcano drv = cpuidle_get_cpu_driver(dev); 392bf4d1b5dSDaniel Lezcano 393e1689795SRobert Lee if (!drv || !cpuidle_curr_governor) 3944f86d3a8SLen Brown return -EIO; 395bf4d1b5dSDaniel Lezcano 39610b9d3f8SDaniel Lezcano if (!dev->registered) 39710b9d3f8SDaniel Lezcano return -EINVAL; 39810b9d3f8SDaniel Lezcano 399bf4d1b5dSDaniel Lezcano ret = cpuidle_add_device_sysfs(dev); 400bf4d1b5dSDaniel Lezcano if (ret) 4014f86d3a8SLen Brown return ret; 4024f86d3a8SLen Brown 4034f86d3a8SLen Brown if (cpuidle_curr_governor->enable && 404e1689795SRobert Lee (ret = cpuidle_curr_governor->enable(drv, dev))) 4054f86d3a8SLen Brown goto fail_sysfs; 4064f86d3a8SLen Brown 4074f86d3a8SLen Brown smp_wmb(); 4084f86d3a8SLen Brown 4094f86d3a8SLen Brown dev->enabled = 1; 4104f86d3a8SLen Brown 4114f86d3a8SLen Brown enabled_devices++; 4124f86d3a8SLen Brown return 0; 4134f86d3a8SLen Brown 4144f86d3a8SLen Brown fail_sysfs: 415bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 4164f86d3a8SLen Brown 4174f86d3a8SLen Brown return ret; 4184f86d3a8SLen Brown } 4194f86d3a8SLen Brown 4204f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device); 4214f86d3a8SLen Brown 4224f86d3a8SLen Brown /** 4234f86d3a8SLen Brown * cpuidle_disable_device - disables idle PM for a CPU 4244f86d3a8SLen Brown * @dev: the CPU 4254f86d3a8SLen Brown * 4264f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 4274f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 4284f86d3a8SLen Brown */ 4294f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev) 4304f86d3a8SLen Brown { 431bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 432bf4d1b5dSDaniel Lezcano 433cf31cd1aSSrivatsa S. Bhat if (!dev || !dev->enabled) 4344f86d3a8SLen Brown return; 435bf4d1b5dSDaniel Lezcano 436bf4d1b5dSDaniel Lezcano if (!drv || !cpuidle_curr_governor) 4374f86d3a8SLen Brown return; 4384f86d3a8SLen Brown 4394f86d3a8SLen Brown dev->enabled = 0; 4404f86d3a8SLen Brown 4414f86d3a8SLen Brown if (cpuidle_curr_governor->disable) 442bf4d1b5dSDaniel Lezcano cpuidle_curr_governor->disable(drv, dev); 4434f86d3a8SLen Brown 444bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 4454f86d3a8SLen Brown enabled_devices--; 4464f86d3a8SLen Brown } 4474f86d3a8SLen Brown 4484f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device); 4494f86d3a8SLen Brown 450f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev) 451f6bb51a5SDaniel Lezcano { 452f6bb51a5SDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 453f6bb51a5SDaniel Lezcano 454f6bb51a5SDaniel Lezcano list_del(&dev->device_list); 455f6bb51a5SDaniel Lezcano per_cpu(cpuidle_devices, dev->cpu) = NULL; 456f6bb51a5SDaniel Lezcano module_put(drv->owner); 457c998c078SDave Gerlach 458c998c078SDave Gerlach dev->registered = 0; 459f6bb51a5SDaniel Lezcano } 460f6bb51a5SDaniel Lezcano 461267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev) 4625df0aa73SDaniel Lezcano { 4635df0aa73SDaniel Lezcano memset(dev->states_usage, 0, sizeof(dev->states_usage)); 4645df0aa73SDaniel Lezcano dev->last_residency = 0; 4655df0aa73SDaniel Lezcano } 4665df0aa73SDaniel Lezcano 4674f86d3a8SLen Brown /** 468dcb84f33SVenkatesh Pallipadi * __cpuidle_register_device - internal register function called before register 469dcb84f33SVenkatesh Pallipadi * and enable routines 4704f86d3a8SLen Brown * @dev: the cpu 471dcb84f33SVenkatesh Pallipadi * 472dcb84f33SVenkatesh Pallipadi * cpuidle_lock mutex must be held before this is called 4734f86d3a8SLen Brown */ 474dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev) 4754f86d3a8SLen Brown { 4764f86d3a8SLen Brown int ret; 477bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 4784f86d3a8SLen Brown 479bf4d1b5dSDaniel Lezcano if (!try_module_get(drv->owner)) 4804f86d3a8SLen Brown return -EINVAL; 4814f86d3a8SLen Brown 4824f86d3a8SLen Brown per_cpu(cpuidle_devices, dev->cpu) = dev; 4834f86d3a8SLen Brown list_add(&dev->device_list, &cpuidle_detected_devices); 4844f86d3a8SLen Brown 4854126c019SColin Cross ret = cpuidle_coupled_register_device(dev); 48647182668SViresh Kumar if (ret) 487f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 48847182668SViresh Kumar else 489dcb84f33SVenkatesh Pallipadi dev->registered = 1; 49047182668SViresh Kumar 49147182668SViresh Kumar return ret; 492dcb84f33SVenkatesh Pallipadi } 493dcb84f33SVenkatesh Pallipadi 494dcb84f33SVenkatesh Pallipadi /** 495dcb84f33SVenkatesh Pallipadi * cpuidle_register_device - registers a CPU's idle PM feature 496dcb84f33SVenkatesh Pallipadi * @dev: the cpu 497dcb84f33SVenkatesh Pallipadi */ 498dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev) 499dcb84f33SVenkatesh Pallipadi { 500c878a52dSDaniel Lezcano int ret = -EBUSY; 501dcb84f33SVenkatesh Pallipadi 5021b0a0e9aSSrivatsa S. Bhat if (!dev) 5031b0a0e9aSSrivatsa S. Bhat return -EINVAL; 5041b0a0e9aSSrivatsa S. Bhat 505dcb84f33SVenkatesh Pallipadi mutex_lock(&cpuidle_lock); 506dcb84f33SVenkatesh Pallipadi 507c878a52dSDaniel Lezcano if (dev->registered) 508c878a52dSDaniel Lezcano goto out_unlock; 509c878a52dSDaniel Lezcano 510267d4bf8SViresh Kumar __cpuidle_device_init(dev); 5115df0aa73SDaniel Lezcano 512f6bb51a5SDaniel Lezcano ret = __cpuidle_register_device(dev); 513f6bb51a5SDaniel Lezcano if (ret) 514f6bb51a5SDaniel Lezcano goto out_unlock; 515f6bb51a5SDaniel Lezcano 516f6bb51a5SDaniel Lezcano ret = cpuidle_add_sysfs(dev); 517f6bb51a5SDaniel Lezcano if (ret) 518f6bb51a5SDaniel Lezcano goto out_unregister; 519dcb84f33SVenkatesh Pallipadi 52010b9d3f8SDaniel Lezcano ret = cpuidle_enable_device(dev); 521f6bb51a5SDaniel Lezcano if (ret) 522f6bb51a5SDaniel Lezcano goto out_sysfs; 52310b9d3f8SDaniel Lezcano 5244f86d3a8SLen Brown cpuidle_install_idle_handler(); 5254f86d3a8SLen Brown 526f6bb51a5SDaniel Lezcano out_unlock: 5274f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 5284f86d3a8SLen Brown 529f6bb51a5SDaniel Lezcano return ret; 530f6bb51a5SDaniel Lezcano 531f6bb51a5SDaniel Lezcano out_sysfs: 532f6bb51a5SDaniel Lezcano cpuidle_remove_sysfs(dev); 533f6bb51a5SDaniel Lezcano out_unregister: 534f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 535f6bb51a5SDaniel Lezcano goto out_unlock; 5364f86d3a8SLen Brown } 5374f86d3a8SLen Brown 5384f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device); 5394f86d3a8SLen Brown 5404f86d3a8SLen Brown /** 5414f86d3a8SLen Brown * cpuidle_unregister_device - unregisters a CPU's idle PM feature 5424f86d3a8SLen Brown * @dev: the cpu 5434f86d3a8SLen Brown */ 5444f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev) 5454f86d3a8SLen Brown { 546813e8e3dSKonrad Rzeszutek Wilk if (!dev || dev->registered == 0) 547dcb84f33SVenkatesh Pallipadi return; 548dcb84f33SVenkatesh Pallipadi 5494f86d3a8SLen Brown cpuidle_pause_and_lock(); 5504f86d3a8SLen Brown 5514f86d3a8SLen Brown cpuidle_disable_device(dev); 5524f86d3a8SLen Brown 5531aef40e2SDaniel Lezcano cpuidle_remove_sysfs(dev); 554f6bb51a5SDaniel Lezcano 555f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 5564f86d3a8SLen Brown 5574126c019SColin Cross cpuidle_coupled_unregister_device(dev); 5584126c019SColin Cross 5594f86d3a8SLen Brown cpuidle_resume_and_unlock(); 5604f86d3a8SLen Brown } 5614f86d3a8SLen Brown 5624f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device); 5634f86d3a8SLen Brown 5641c192d04SDaniel Lezcano /** 5654c637b21SDaniel Lezcano * cpuidle_unregister: unregister a driver and the devices. This function 5664c637b21SDaniel Lezcano * can be used only if the driver has been previously registered through 5674c637b21SDaniel Lezcano * the cpuidle_register function. 5684c637b21SDaniel Lezcano * 5694c637b21SDaniel Lezcano * @drv: a valid pointer to a struct cpuidle_driver 5704c637b21SDaniel Lezcano */ 5714c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv) 5724c637b21SDaniel Lezcano { 5734c637b21SDaniel Lezcano int cpu; 5744c637b21SDaniel Lezcano struct cpuidle_device *device; 5754c637b21SDaniel Lezcano 57682467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 5774c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 5784c637b21SDaniel Lezcano cpuidle_unregister_device(device); 5794c637b21SDaniel Lezcano } 5804c637b21SDaniel Lezcano 5814c637b21SDaniel Lezcano cpuidle_unregister_driver(drv); 5824c637b21SDaniel Lezcano } 5834c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister); 5844c637b21SDaniel Lezcano 5854c637b21SDaniel Lezcano /** 5864c637b21SDaniel Lezcano * cpuidle_register: registers the driver and the cpu devices with the 5874c637b21SDaniel Lezcano * coupled_cpus passed as parameter. This function is used for all common 5884c637b21SDaniel Lezcano * initialization pattern there are in the arch specific drivers. The 5894c637b21SDaniel Lezcano * devices is globally defined in this file. 5904c637b21SDaniel Lezcano * 5914c637b21SDaniel Lezcano * @drv : a valid pointer to a struct cpuidle_driver 5924c637b21SDaniel Lezcano * @coupled_cpus: a cpumask for the coupled states 5934c637b21SDaniel Lezcano * 5944c637b21SDaniel Lezcano * Returns 0 on success, < 0 otherwise 5954c637b21SDaniel Lezcano */ 5964c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv, 5974c637b21SDaniel Lezcano const struct cpumask *const coupled_cpus) 5984c637b21SDaniel Lezcano { 5994c637b21SDaniel Lezcano int ret, cpu; 6004c637b21SDaniel Lezcano struct cpuidle_device *device; 6014c637b21SDaniel Lezcano 6024c637b21SDaniel Lezcano ret = cpuidle_register_driver(drv); 6034c637b21SDaniel Lezcano if (ret) { 6044c637b21SDaniel Lezcano pr_err("failed to register cpuidle driver\n"); 6054c637b21SDaniel Lezcano return ret; 6064c637b21SDaniel Lezcano } 6074c637b21SDaniel Lezcano 60882467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 6094c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 6104c637b21SDaniel Lezcano device->cpu = cpu; 6114c637b21SDaniel Lezcano 6124c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 6134c637b21SDaniel Lezcano /* 614caf4a36eSViresh Kumar * On multiplatform for ARM, the coupled idle states could be 6154c637b21SDaniel Lezcano * enabled in the kernel even if the cpuidle driver does not 6164c637b21SDaniel Lezcano * use it. Note, coupled_cpus is a struct copy. 6174c637b21SDaniel Lezcano */ 6184c637b21SDaniel Lezcano if (coupled_cpus) 6194c637b21SDaniel Lezcano device->coupled_cpus = *coupled_cpus; 6204c637b21SDaniel Lezcano #endif 6214c637b21SDaniel Lezcano ret = cpuidle_register_device(device); 6224c637b21SDaniel Lezcano if (!ret) 6234c637b21SDaniel Lezcano continue; 6244c637b21SDaniel Lezcano 6254c637b21SDaniel Lezcano pr_err("Failed to register cpuidle device for cpu%d\n", cpu); 6264c637b21SDaniel Lezcano 6274c637b21SDaniel Lezcano cpuidle_unregister(drv); 6284c637b21SDaniel Lezcano break; 6294c637b21SDaniel Lezcano } 6304c637b21SDaniel Lezcano 6314c637b21SDaniel Lezcano return ret; 6324c637b21SDaniel Lezcano } 6334c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register); 6344c637b21SDaniel Lezcano 6354f86d3a8SLen Brown #ifdef CONFIG_SMP 6364f86d3a8SLen Brown 6374f86d3a8SLen Brown /* 6384f86d3a8SLen Brown * This function gets called when a part of the kernel has a new latency 6394f86d3a8SLen Brown * requirement. This means we need to get all processors out of their C-state, 6404f86d3a8SLen Brown * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that 6414f86d3a8SLen Brown * wakes them all right up. 6424f86d3a8SLen Brown */ 6434f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b, 6444f86d3a8SLen Brown unsigned long l, void *v) 6454f86d3a8SLen Brown { 6462ed903c5SChuansheng Liu wake_up_all_idle_cpus(); 6474f86d3a8SLen Brown return NOTIFY_OK; 6484f86d3a8SLen Brown } 6494f86d3a8SLen Brown 6504f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = { 6514f86d3a8SLen Brown .notifier_call = cpuidle_latency_notify, 6524f86d3a8SLen Brown }; 6534f86d3a8SLen Brown 654d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n) 655d82b3518SMark Gross { 656d82b3518SMark Gross pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); 657d82b3518SMark Gross } 6584f86d3a8SLen Brown 6594f86d3a8SLen Brown #else /* CONFIG_SMP */ 6604f86d3a8SLen Brown 6614f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0) 6624f86d3a8SLen Brown 6634f86d3a8SLen Brown #endif /* CONFIG_SMP */ 6644f86d3a8SLen Brown 6654f86d3a8SLen Brown /** 6664f86d3a8SLen Brown * cpuidle_init - core initializer 6674f86d3a8SLen Brown */ 6684f86d3a8SLen Brown static int __init cpuidle_init(void) 6694f86d3a8SLen Brown { 6704f86d3a8SLen Brown int ret; 6714f86d3a8SLen Brown 67262027aeaSLen Brown if (cpuidle_disabled()) 67362027aeaSLen Brown return -ENODEV; 67462027aeaSLen Brown 6758a25a2fdSKay Sievers ret = cpuidle_add_interface(cpu_subsys.dev_root); 6764f86d3a8SLen Brown if (ret) 6774f86d3a8SLen Brown return ret; 6784f86d3a8SLen Brown 6794f86d3a8SLen Brown latency_notifier_init(&cpuidle_latency_notifier); 6804f86d3a8SLen Brown 6814f86d3a8SLen Brown return 0; 6824f86d3a8SLen Brown } 6834f86d3a8SLen Brown 68462027aeaSLen Brown module_param(off, int, 0444); 6854f86d3a8SLen Brown core_initcall(cpuidle_init); 686