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> 154f86d3a8SLen Brown #include <linux/notifier.h> 16e8db0be1SJean Pihet #include <linux/pm_qos.h> 174f86d3a8SLen Brown #include <linux/cpu.h> 184f86d3a8SLen Brown #include <linux/cpuidle.h> 199a0b8415Svenkatesh.pallipadi@intel.com #include <linux/ktime.h> 202e94d1f7SArjan van de Ven #include <linux/hrtimer.h> 21884b17e1SPaul Gortmaker #include <linux/module.h> 2238106313SRafael J. Wysocki #include <linux/suspend.h> 23124cf911SRafael J. Wysocki #include <linux/tick.h> 24288f023eSArjan van de Ven #include <trace/events/power.h> 254f86d3a8SLen Brown 264f86d3a8SLen Brown #include "cpuidle.h" 274f86d3a8SLen Brown 284f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); 294c637b21SDaniel Lezcano DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev); 304f86d3a8SLen Brown 314f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock); 324f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices); 334f86d3a8SLen Brown 344f86d3a8SLen Brown static int enabled_devices; 3562027aeaSLen Brown static int off __read_mostly; 36a0bfa137SLen Brown static int initialized __read_mostly; 3762027aeaSLen Brown 3862027aeaSLen Brown int cpuidle_disabled(void) 3962027aeaSLen Brown { 4062027aeaSLen Brown return off; 4162027aeaSLen Brown } 42d91ee586SLen Brown void disable_cpuidle(void) 43d91ee586SLen Brown { 44d91ee586SLen Brown off = 1; 45d91ee586SLen Brown } 464f86d3a8SLen Brown 47ef2b22acSRafael J. Wysocki bool cpuidle_not_available(struct cpuidle_driver *drv, 4831a34090SRafael J. Wysocki struct cpuidle_device *dev) 4931a34090SRafael J. Wysocki { 5031a34090SRafael J. Wysocki return off || !initialized || !drv || !dev || !dev->enabled; 5131a34090SRafael J. Wysocki } 5231a34090SRafael J. Wysocki 534f86d3a8SLen Brown /** 541a022e3fSBoris Ostrovsky * cpuidle_play_dead - cpu off-lining 551a022e3fSBoris Ostrovsky * 56ee01e663SToshi Kani * Returns in case of an error or no driver 571a022e3fSBoris Ostrovsky */ 581a022e3fSBoris Ostrovsky int cpuidle_play_dead(void) 591a022e3fSBoris Ostrovsky { 601a022e3fSBoris Ostrovsky struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); 61bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 628aef33a7SDaniel Lezcano int i; 631a022e3fSBoris Ostrovsky 64ee01e663SToshi Kani if (!drv) 65ee01e663SToshi Kani return -ENODEV; 66ee01e663SToshi Kani 671a022e3fSBoris Ostrovsky /* Find lowest-power state that supports long-term idle */ 688aef33a7SDaniel Lezcano for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--) 698aef33a7SDaniel Lezcano if (drv->states[i].enter_dead) 708aef33a7SDaniel Lezcano return drv->states[i].enter_dead(dev, i); 711a022e3fSBoris Ostrovsky 721a022e3fSBoris Ostrovsky return -ENODEV; 731a022e3fSBoris Ostrovsky } 741a022e3fSBoris Ostrovsky 75ef2b22acSRafael J. Wysocki static int find_deepest_state(struct cpuidle_driver *drv, 76124cf911SRafael J. Wysocki struct cpuidle_device *dev, bool freeze) 77a6220fc1SRafael J. Wysocki { 78a6220fc1SRafael J. Wysocki unsigned int latency_req = 0; 79124cf911SRafael J. Wysocki int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1; 80a6220fc1SRafael J. Wysocki 81a6220fc1SRafael J. Wysocki for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) { 82a6220fc1SRafael J. Wysocki struct cpuidle_state *s = &drv->states[i]; 83a6220fc1SRafael J. Wysocki struct cpuidle_state_usage *su = &dev->states_usage[i]; 84a6220fc1SRafael J. Wysocki 85124cf911SRafael J. Wysocki if (s->disabled || su->disable || s->exit_latency <= latency_req 86124cf911SRafael J. Wysocki || (freeze && !s->enter_freeze)) 87a6220fc1SRafael J. Wysocki continue; 88a6220fc1SRafael J. Wysocki 89a6220fc1SRafael J. Wysocki latency_req = s->exit_latency; 90a6220fc1SRafael J. Wysocki ret = i; 91a6220fc1SRafael J. Wysocki } 92a6220fc1SRafael J. Wysocki return ret; 93a6220fc1SRafael J. Wysocki } 94a6220fc1SRafael J. Wysocki 95ef2b22acSRafael J. Wysocki /** 96ef2b22acSRafael J. Wysocki * cpuidle_find_deepest_state - Find the deepest available idle state. 97ef2b22acSRafael J. Wysocki * @drv: cpuidle driver for the given CPU. 98ef2b22acSRafael J. Wysocki * @dev: cpuidle device for the given CPU. 99ef2b22acSRafael J. Wysocki */ 100ef2b22acSRafael J. Wysocki int cpuidle_find_deepest_state(struct cpuidle_driver *drv, 101ef2b22acSRafael J. Wysocki struct cpuidle_device *dev) 102ef2b22acSRafael J. Wysocki { 103ef2b22acSRafael J. Wysocki return find_deepest_state(drv, dev, false); 104ef2b22acSRafael J. Wysocki } 105ef2b22acSRafael J. Wysocki 106124cf911SRafael J. Wysocki static void enter_freeze_proper(struct cpuidle_driver *drv, 107124cf911SRafael J. Wysocki struct cpuidle_device *dev, int index) 108124cf911SRafael J. Wysocki { 109124cf911SRafael J. Wysocki tick_freeze(); 110124cf911SRafael J. Wysocki /* 111124cf911SRafael J. Wysocki * The state used here cannot be a "coupled" one, because the "coupled" 112124cf911SRafael J. Wysocki * cpuidle mechanism enables interrupts and doing that with timekeeping 113124cf911SRafael J. Wysocki * suspended is generally unsafe. 114124cf911SRafael J. Wysocki */ 115124cf911SRafael J. Wysocki drv->states[index].enter_freeze(dev, drv, index); 116124cf911SRafael J. Wysocki WARN_ON(!irqs_disabled()); 117124cf911SRafael J. Wysocki /* 118124cf911SRafael J. Wysocki * timekeeping_resume() that will be called by tick_unfreeze() for the 119124cf911SRafael J. Wysocki * last CPU executing it calls functions containing RCU read-side 120124cf911SRafael J. Wysocki * critical sections, so tell RCU about that. 121124cf911SRafael J. Wysocki */ 122124cf911SRafael J. Wysocki RCU_NONIDLE(tick_unfreeze()); 123124cf911SRafael J. Wysocki } 124124cf911SRafael J. Wysocki 125a6220fc1SRafael J. Wysocki /** 12638106313SRafael J. Wysocki * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle. 127ef2b22acSRafael J. Wysocki * @drv: cpuidle driver for the given CPU. 128ef2b22acSRafael J. Wysocki * @dev: cpuidle device for the given CPU. 12938106313SRafael J. Wysocki * 130124cf911SRafael J. Wysocki * If there are states with the ->enter_freeze callback, find the deepest of 131ef2b22acSRafael J. Wysocki * them and enter it with frozen tick. 13238106313SRafael J. Wysocki */ 133ef2b22acSRafael J. Wysocki int cpuidle_enter_freeze(struct cpuidle_driver *drv, struct cpuidle_device *dev) 13438106313SRafael J. Wysocki { 13538106313SRafael J. Wysocki int index; 13638106313SRafael J. Wysocki 137124cf911SRafael J. Wysocki /* 138124cf911SRafael J. Wysocki * Find the deepest state with ->enter_freeze present, which guarantees 139124cf911SRafael J. Wysocki * that interrupts won't be enabled when it exits and allows the tick to 140124cf911SRafael J. Wysocki * be frozen safely. 141124cf911SRafael J. Wysocki */ 142ef2b22acSRafael J. Wysocki index = find_deepest_state(drv, dev, true); 143ef2b22acSRafael J. Wysocki if (index >= 0) 144124cf911SRafael J. Wysocki enter_freeze_proper(drv, dev, index); 145124cf911SRafael J. Wysocki 146ef2b22acSRafael J. Wysocki return index; 14738106313SRafael J. Wysocki } 14838106313SRafael J. Wysocki 14938106313SRafael J. Wysocki /** 15056cfbf74SColin Cross * cpuidle_enter_state - enter the state and update stats 15156cfbf74SColin Cross * @dev: cpuidle device for this cpu 15256cfbf74SColin Cross * @drv: cpuidle driver for this cpu 1537312280bSRafael J. Wysocki * @index: index into the states table in @drv of the state to enter 15456cfbf74SColin Cross */ 15556cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, 156554c06baSDaniel Lezcano int index) 15756cfbf74SColin Cross { 15856cfbf74SColin Cross int entered_state; 15956cfbf74SColin Cross 160554c06baSDaniel Lezcano struct cpuidle_state *target_state = &drv->states[index]; 161df8d9eeaSRafael J. Wysocki bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP); 162554c06baSDaniel Lezcano ktime_t time_start, time_end; 163554c06baSDaniel Lezcano s64 diff; 164554c06baSDaniel Lezcano 165df8d9eeaSRafael J. Wysocki /* 166df8d9eeaSRafael J. Wysocki * Tell the time framework to switch to a broadcast timer because our 167df8d9eeaSRafael J. Wysocki * local timer will be shut down. If a local timer is used from another 168df8d9eeaSRafael J. Wysocki * CPU as a broadcast timer, this call may fail if it is not available. 169df8d9eeaSRafael J. Wysocki */ 170*827a5aefSRafael J. Wysocki if (broadcast && tick_broadcast_enter()) { 171*827a5aefSRafael J. Wysocki default_idle_call(); 172df8d9eeaSRafael J. Wysocki return -EBUSY; 173*827a5aefSRafael J. Wysocki } 174df8d9eeaSRafael J. Wysocki 175faad3849SRafael J. Wysocki /* Take note of the planned idle state. */ 176faad3849SRafael J. Wysocki sched_idle_set_state(target_state); 177faad3849SRafael J. Wysocki 17830fe6884SSandeep Tripathy trace_cpu_idle_rcuidle(index, dev->cpu); 179554c06baSDaniel Lezcano time_start = ktime_get(); 180554c06baSDaniel Lezcano 181554c06baSDaniel Lezcano entered_state = target_state->enter(dev, drv, index); 182554c06baSDaniel Lezcano 183554c06baSDaniel Lezcano time_end = ktime_get(); 18430fe6884SSandeep Tripathy trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu); 185554c06baSDaniel Lezcano 186faad3849SRafael J. Wysocki /* The cpu is no longer idle or about to enter idle. */ 187faad3849SRafael J. Wysocki sched_idle_set_state(NULL); 188faad3849SRafael J. Wysocki 189df8d9eeaSRafael J. Wysocki if (broadcast) { 190df8d9eeaSRafael J. Wysocki if (WARN_ON_ONCE(!irqs_disabled())) 191df8d9eeaSRafael J. Wysocki local_irq_disable(); 192df8d9eeaSRafael J. Wysocki 193df8d9eeaSRafael J. Wysocki tick_broadcast_exit(); 194df8d9eeaSRafael J. Wysocki } 195df8d9eeaSRafael J. Wysocki 1960b89e9aaSPaul Burton if (!cpuidle_state_is_coupled(dev, drv, entered_state)) 197554c06baSDaniel Lezcano local_irq_enable(); 198554c06baSDaniel Lezcano 199554c06baSDaniel Lezcano diff = ktime_to_us(ktime_sub(time_end, time_start)); 200554c06baSDaniel Lezcano if (diff > INT_MAX) 201554c06baSDaniel Lezcano diff = INT_MAX; 202554c06baSDaniel Lezcano 203554c06baSDaniel Lezcano dev->last_residency = (int) diff; 20456cfbf74SColin Cross 20556cfbf74SColin Cross if (entered_state >= 0) { 20656cfbf74SColin Cross /* Update cpuidle counters */ 20756cfbf74SColin Cross /* This can be moved to within driver enter routine 20856cfbf74SColin Cross * but that results in multiple copies of same code. 20956cfbf74SColin Cross */ 210a474a515SJulius Werner dev->states_usage[entered_state].time += dev->last_residency; 21156cfbf74SColin Cross dev->states_usage[entered_state].usage++; 21256cfbf74SColin Cross } else { 21356cfbf74SColin Cross dev->last_residency = 0; 21456cfbf74SColin Cross } 21556cfbf74SColin Cross 21656cfbf74SColin Cross return entered_state; 21756cfbf74SColin Cross } 21856cfbf74SColin Cross 21956cfbf74SColin Cross /** 220907e30f1SDaniel Lezcano * cpuidle_select - ask the cpuidle framework to choose an idle state 2214f86d3a8SLen Brown * 222907e30f1SDaniel Lezcano * @drv: the cpuidle driver 223907e30f1SDaniel Lezcano * @dev: the cpuidle device 224907e30f1SDaniel Lezcano * 225907e30f1SDaniel Lezcano * Returns the index of the idle state. 2264f86d3a8SLen Brown */ 227907e30f1SDaniel Lezcano int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev) 2284f86d3a8SLen Brown { 229907e30f1SDaniel Lezcano return cpuidle_curr_governor->select(drv, dev); 230246eb7f0SKevin Hilman } 231246eb7f0SKevin Hilman 232907e30f1SDaniel Lezcano /** 233907e30f1SDaniel Lezcano * cpuidle_enter - enter into the specified idle state 234907e30f1SDaniel Lezcano * 235907e30f1SDaniel Lezcano * @drv: the cpuidle driver tied with the cpu 236907e30f1SDaniel Lezcano * @dev: the cpuidle device 237907e30f1SDaniel Lezcano * @index: the index in the idle state table 238907e30f1SDaniel Lezcano * 239907e30f1SDaniel Lezcano * Returns the index in the idle state, < 0 in case of error. 240907e30f1SDaniel Lezcano * The error code depends on the backend driver 241907e30f1SDaniel Lezcano */ 242907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev, 243907e30f1SDaniel Lezcano int index) 244907e30f1SDaniel Lezcano { 245907e30f1SDaniel Lezcano if (cpuidle_state_is_coupled(dev, drv, index)) 246907e30f1SDaniel Lezcano return cpuidle_enter_state_coupled(dev, drv, index); 247907e30f1SDaniel Lezcano return cpuidle_enter_state(dev, drv, index); 248907e30f1SDaniel Lezcano } 249fb11c9c6SViresh Kumar 250907e30f1SDaniel Lezcano /** 251907e30f1SDaniel Lezcano * cpuidle_reflect - tell the underlying governor what was the state 252907e30f1SDaniel Lezcano * we were in 253907e30f1SDaniel Lezcano * 254907e30f1SDaniel Lezcano * @dev : the cpuidle device 255907e30f1SDaniel Lezcano * @index: the index in the idle state table 256907e30f1SDaniel Lezcano * 257907e30f1SDaniel Lezcano */ 258907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index) 259907e30f1SDaniel Lezcano { 260a802ea96SRafael J. Wysocki if (cpuidle_curr_governor->reflect && index >= 0) 261907e30f1SDaniel Lezcano cpuidle_curr_governor->reflect(dev, index); 2624f86d3a8SLen Brown } 2634f86d3a8SLen Brown 2644f86d3a8SLen Brown /** 2654f86d3a8SLen Brown * cpuidle_install_idle_handler - installs the cpuidle idle loop handler 2664f86d3a8SLen Brown */ 2674f86d3a8SLen Brown void cpuidle_install_idle_handler(void) 2684f86d3a8SLen Brown { 269a0bfa137SLen Brown if (enabled_devices) { 2704f86d3a8SLen Brown /* Make sure all changes finished before we switch to new idle */ 2714f86d3a8SLen Brown smp_wmb(); 272a0bfa137SLen Brown initialized = 1; 2734f86d3a8SLen Brown } 2744f86d3a8SLen Brown } 2754f86d3a8SLen Brown 2764f86d3a8SLen Brown /** 2774f86d3a8SLen Brown * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler 2784f86d3a8SLen Brown */ 2794f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void) 2804f86d3a8SLen Brown { 281a0bfa137SLen Brown if (enabled_devices) { 282a0bfa137SLen Brown initialized = 0; 2832ed903c5SChuansheng Liu wake_up_all_idle_cpus(); 2844f86d3a8SLen Brown } 285442bf3aaSDaniel Lezcano 286442bf3aaSDaniel Lezcano /* 287442bf3aaSDaniel Lezcano * Make sure external observers (such as the scheduler) 288442bf3aaSDaniel Lezcano * are done looking at pointed idle states. 289442bf3aaSDaniel Lezcano */ 290442bf3aaSDaniel Lezcano synchronize_rcu(); 2914f86d3a8SLen Brown } 2924f86d3a8SLen Brown 2934f86d3a8SLen Brown /** 2944f86d3a8SLen Brown * cpuidle_pause_and_lock - temporarily disables CPUIDLE 2954f86d3a8SLen Brown */ 2964f86d3a8SLen Brown void cpuidle_pause_and_lock(void) 2974f86d3a8SLen Brown { 2984f86d3a8SLen Brown mutex_lock(&cpuidle_lock); 2994f86d3a8SLen Brown cpuidle_uninstall_idle_handler(); 3004f86d3a8SLen Brown } 3014f86d3a8SLen Brown 3024f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); 3034f86d3a8SLen Brown 3044f86d3a8SLen Brown /** 3054f86d3a8SLen Brown * cpuidle_resume_and_unlock - resumes CPUIDLE operation 3064f86d3a8SLen Brown */ 3074f86d3a8SLen Brown void cpuidle_resume_and_unlock(void) 3084f86d3a8SLen Brown { 3094f86d3a8SLen Brown cpuidle_install_idle_handler(); 3104f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 3114f86d3a8SLen Brown } 3124f86d3a8SLen Brown 3134f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); 3144f86d3a8SLen Brown 3158651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */ 3168651f97bSPreeti U Murthy void cpuidle_pause(void) 3178651f97bSPreeti U Murthy { 3188651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 3198651f97bSPreeti U Murthy cpuidle_uninstall_idle_handler(); 3208651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 3218651f97bSPreeti U Murthy } 3228651f97bSPreeti U Murthy 3238651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */ 3248651f97bSPreeti U Murthy void cpuidle_resume(void) 3258651f97bSPreeti U Murthy { 3268651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 3278651f97bSPreeti U Murthy cpuidle_install_idle_handler(); 3288651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 3298651f97bSPreeti U Murthy } 3308651f97bSPreeti U Murthy 3314f86d3a8SLen Brown /** 3324f86d3a8SLen Brown * cpuidle_enable_device - enables idle PM for a CPU 3334f86d3a8SLen Brown * @dev: the CPU 3344f86d3a8SLen Brown * 3354f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 3364f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 3374f86d3a8SLen Brown */ 3384f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev) 3394f86d3a8SLen Brown { 3405df0aa73SDaniel Lezcano int ret; 341bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv; 3424f86d3a8SLen Brown 3431b0a0e9aSSrivatsa S. Bhat if (!dev) 3441b0a0e9aSSrivatsa S. Bhat return -EINVAL; 3451b0a0e9aSSrivatsa S. Bhat 3464f86d3a8SLen Brown if (dev->enabled) 3474f86d3a8SLen Brown return 0; 348bf4d1b5dSDaniel Lezcano 349bf4d1b5dSDaniel Lezcano drv = cpuidle_get_cpu_driver(dev); 350bf4d1b5dSDaniel Lezcano 351e1689795SRobert Lee if (!drv || !cpuidle_curr_governor) 3524f86d3a8SLen Brown return -EIO; 353bf4d1b5dSDaniel Lezcano 35410b9d3f8SDaniel Lezcano if (!dev->registered) 35510b9d3f8SDaniel Lezcano return -EINVAL; 35610b9d3f8SDaniel Lezcano 357bf4d1b5dSDaniel Lezcano ret = cpuidle_add_device_sysfs(dev); 358bf4d1b5dSDaniel Lezcano if (ret) 3594f86d3a8SLen Brown return ret; 3604f86d3a8SLen Brown 3614f86d3a8SLen Brown if (cpuidle_curr_governor->enable && 362e1689795SRobert Lee (ret = cpuidle_curr_governor->enable(drv, dev))) 3634f86d3a8SLen Brown goto fail_sysfs; 3644f86d3a8SLen Brown 3654f86d3a8SLen Brown smp_wmb(); 3664f86d3a8SLen Brown 3674f86d3a8SLen Brown dev->enabled = 1; 3684f86d3a8SLen Brown 3694f86d3a8SLen Brown enabled_devices++; 3704f86d3a8SLen Brown return 0; 3714f86d3a8SLen Brown 3724f86d3a8SLen Brown fail_sysfs: 373bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 3744f86d3a8SLen Brown 3754f86d3a8SLen Brown return ret; 3764f86d3a8SLen Brown } 3774f86d3a8SLen Brown 3784f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device); 3794f86d3a8SLen Brown 3804f86d3a8SLen Brown /** 3814f86d3a8SLen Brown * cpuidle_disable_device - disables idle PM for a CPU 3824f86d3a8SLen Brown * @dev: the CPU 3834f86d3a8SLen Brown * 3844f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 3854f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 3864f86d3a8SLen Brown */ 3874f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev) 3884f86d3a8SLen Brown { 389bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 390bf4d1b5dSDaniel Lezcano 391cf31cd1aSSrivatsa S. Bhat if (!dev || !dev->enabled) 3924f86d3a8SLen Brown return; 393bf4d1b5dSDaniel Lezcano 394bf4d1b5dSDaniel Lezcano if (!drv || !cpuidle_curr_governor) 3954f86d3a8SLen Brown return; 3964f86d3a8SLen Brown 3974f86d3a8SLen Brown dev->enabled = 0; 3984f86d3a8SLen Brown 3994f86d3a8SLen Brown if (cpuidle_curr_governor->disable) 400bf4d1b5dSDaniel Lezcano cpuidle_curr_governor->disable(drv, dev); 4014f86d3a8SLen Brown 402bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 4034f86d3a8SLen Brown enabled_devices--; 4044f86d3a8SLen Brown } 4054f86d3a8SLen Brown 4064f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device); 4074f86d3a8SLen Brown 408f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev) 409f6bb51a5SDaniel Lezcano { 410f6bb51a5SDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 411f6bb51a5SDaniel Lezcano 412f6bb51a5SDaniel Lezcano list_del(&dev->device_list); 413f6bb51a5SDaniel Lezcano per_cpu(cpuidle_devices, dev->cpu) = NULL; 414f6bb51a5SDaniel Lezcano module_put(drv->owner); 415f6bb51a5SDaniel Lezcano } 416f6bb51a5SDaniel Lezcano 417267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev) 4185df0aa73SDaniel Lezcano { 4195df0aa73SDaniel Lezcano memset(dev->states_usage, 0, sizeof(dev->states_usage)); 4205df0aa73SDaniel Lezcano dev->last_residency = 0; 4215df0aa73SDaniel Lezcano } 4225df0aa73SDaniel Lezcano 4234f86d3a8SLen Brown /** 424dcb84f33SVenkatesh Pallipadi * __cpuidle_register_device - internal register function called before register 425dcb84f33SVenkatesh Pallipadi * and enable routines 4264f86d3a8SLen Brown * @dev: the cpu 427dcb84f33SVenkatesh Pallipadi * 428dcb84f33SVenkatesh Pallipadi * cpuidle_lock mutex must be held before this is called 4294f86d3a8SLen Brown */ 430dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev) 4314f86d3a8SLen Brown { 4324f86d3a8SLen Brown int ret; 433bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 4344f86d3a8SLen Brown 435bf4d1b5dSDaniel Lezcano if (!try_module_get(drv->owner)) 4364f86d3a8SLen Brown return -EINVAL; 4374f86d3a8SLen Brown 4384f86d3a8SLen Brown per_cpu(cpuidle_devices, dev->cpu) = dev; 4394f86d3a8SLen Brown list_add(&dev->device_list, &cpuidle_detected_devices); 4404f86d3a8SLen Brown 4414126c019SColin Cross ret = cpuidle_coupled_register_device(dev); 44247182668SViresh Kumar if (ret) 443f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 44447182668SViresh Kumar else 445dcb84f33SVenkatesh Pallipadi dev->registered = 1; 44647182668SViresh Kumar 44747182668SViresh Kumar return ret; 448dcb84f33SVenkatesh Pallipadi } 449dcb84f33SVenkatesh Pallipadi 450dcb84f33SVenkatesh Pallipadi /** 451dcb84f33SVenkatesh Pallipadi * cpuidle_register_device - registers a CPU's idle PM feature 452dcb84f33SVenkatesh Pallipadi * @dev: the cpu 453dcb84f33SVenkatesh Pallipadi */ 454dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev) 455dcb84f33SVenkatesh Pallipadi { 456c878a52dSDaniel Lezcano int ret = -EBUSY; 457dcb84f33SVenkatesh Pallipadi 4581b0a0e9aSSrivatsa S. Bhat if (!dev) 4591b0a0e9aSSrivatsa S. Bhat return -EINVAL; 4601b0a0e9aSSrivatsa S. Bhat 461dcb84f33SVenkatesh Pallipadi mutex_lock(&cpuidle_lock); 462dcb84f33SVenkatesh Pallipadi 463c878a52dSDaniel Lezcano if (dev->registered) 464c878a52dSDaniel Lezcano goto out_unlock; 465c878a52dSDaniel Lezcano 466267d4bf8SViresh Kumar __cpuidle_device_init(dev); 4675df0aa73SDaniel Lezcano 468f6bb51a5SDaniel Lezcano ret = __cpuidle_register_device(dev); 469f6bb51a5SDaniel Lezcano if (ret) 470f6bb51a5SDaniel Lezcano goto out_unlock; 471f6bb51a5SDaniel Lezcano 472f6bb51a5SDaniel Lezcano ret = cpuidle_add_sysfs(dev); 473f6bb51a5SDaniel Lezcano if (ret) 474f6bb51a5SDaniel Lezcano goto out_unregister; 475dcb84f33SVenkatesh Pallipadi 47610b9d3f8SDaniel Lezcano ret = cpuidle_enable_device(dev); 477f6bb51a5SDaniel Lezcano if (ret) 478f6bb51a5SDaniel Lezcano goto out_sysfs; 47910b9d3f8SDaniel Lezcano 4804f86d3a8SLen Brown cpuidle_install_idle_handler(); 4814f86d3a8SLen Brown 482f6bb51a5SDaniel Lezcano out_unlock: 4834f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 4844f86d3a8SLen Brown 485f6bb51a5SDaniel Lezcano return ret; 486f6bb51a5SDaniel Lezcano 487f6bb51a5SDaniel Lezcano out_sysfs: 488f6bb51a5SDaniel Lezcano cpuidle_remove_sysfs(dev); 489f6bb51a5SDaniel Lezcano out_unregister: 490f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 491f6bb51a5SDaniel Lezcano goto out_unlock; 4924f86d3a8SLen Brown } 4934f86d3a8SLen Brown 4944f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device); 4954f86d3a8SLen Brown 4964f86d3a8SLen Brown /** 4974f86d3a8SLen Brown * cpuidle_unregister_device - unregisters a CPU's idle PM feature 4984f86d3a8SLen Brown * @dev: the cpu 4994f86d3a8SLen Brown */ 5004f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev) 5014f86d3a8SLen Brown { 502813e8e3dSKonrad Rzeszutek Wilk if (!dev || dev->registered == 0) 503dcb84f33SVenkatesh Pallipadi return; 504dcb84f33SVenkatesh Pallipadi 5054f86d3a8SLen Brown cpuidle_pause_and_lock(); 5064f86d3a8SLen Brown 5074f86d3a8SLen Brown cpuidle_disable_device(dev); 5084f86d3a8SLen Brown 5091aef40e2SDaniel Lezcano cpuidle_remove_sysfs(dev); 510f6bb51a5SDaniel Lezcano 511f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 5124f86d3a8SLen Brown 5134126c019SColin Cross cpuidle_coupled_unregister_device(dev); 5144126c019SColin Cross 5154f86d3a8SLen Brown cpuidle_resume_and_unlock(); 5164f86d3a8SLen Brown } 5174f86d3a8SLen Brown 5184f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device); 5194f86d3a8SLen Brown 5201c192d04SDaniel Lezcano /** 5214c637b21SDaniel Lezcano * cpuidle_unregister: unregister a driver and the devices. This function 5224c637b21SDaniel Lezcano * can be used only if the driver has been previously registered through 5234c637b21SDaniel Lezcano * the cpuidle_register function. 5244c637b21SDaniel Lezcano * 5254c637b21SDaniel Lezcano * @drv: a valid pointer to a struct cpuidle_driver 5264c637b21SDaniel Lezcano */ 5274c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv) 5284c637b21SDaniel Lezcano { 5294c637b21SDaniel Lezcano int cpu; 5304c637b21SDaniel Lezcano struct cpuidle_device *device; 5314c637b21SDaniel Lezcano 53282467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 5334c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 5344c637b21SDaniel Lezcano cpuidle_unregister_device(device); 5354c637b21SDaniel Lezcano } 5364c637b21SDaniel Lezcano 5374c637b21SDaniel Lezcano cpuidle_unregister_driver(drv); 5384c637b21SDaniel Lezcano } 5394c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister); 5404c637b21SDaniel Lezcano 5414c637b21SDaniel Lezcano /** 5424c637b21SDaniel Lezcano * cpuidle_register: registers the driver and the cpu devices with the 5434c637b21SDaniel Lezcano * coupled_cpus passed as parameter. This function is used for all common 5444c637b21SDaniel Lezcano * initialization pattern there are in the arch specific drivers. The 5454c637b21SDaniel Lezcano * devices is globally defined in this file. 5464c637b21SDaniel Lezcano * 5474c637b21SDaniel Lezcano * @drv : a valid pointer to a struct cpuidle_driver 5484c637b21SDaniel Lezcano * @coupled_cpus: a cpumask for the coupled states 5494c637b21SDaniel Lezcano * 5504c637b21SDaniel Lezcano * Returns 0 on success, < 0 otherwise 5514c637b21SDaniel Lezcano */ 5524c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv, 5534c637b21SDaniel Lezcano const struct cpumask *const coupled_cpus) 5544c637b21SDaniel Lezcano { 5554c637b21SDaniel Lezcano int ret, cpu; 5564c637b21SDaniel Lezcano struct cpuidle_device *device; 5574c637b21SDaniel Lezcano 5584c637b21SDaniel Lezcano ret = cpuidle_register_driver(drv); 5594c637b21SDaniel Lezcano if (ret) { 5604c637b21SDaniel Lezcano pr_err("failed to register cpuidle driver\n"); 5614c637b21SDaniel Lezcano return ret; 5624c637b21SDaniel Lezcano } 5634c637b21SDaniel Lezcano 56482467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 5654c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 5664c637b21SDaniel Lezcano device->cpu = cpu; 5674c637b21SDaniel Lezcano 5684c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 5694c637b21SDaniel Lezcano /* 570caf4a36eSViresh Kumar * On multiplatform for ARM, the coupled idle states could be 5714c637b21SDaniel Lezcano * enabled in the kernel even if the cpuidle driver does not 5724c637b21SDaniel Lezcano * use it. Note, coupled_cpus is a struct copy. 5734c637b21SDaniel Lezcano */ 5744c637b21SDaniel Lezcano if (coupled_cpus) 5754c637b21SDaniel Lezcano device->coupled_cpus = *coupled_cpus; 5764c637b21SDaniel Lezcano #endif 5774c637b21SDaniel Lezcano ret = cpuidle_register_device(device); 5784c637b21SDaniel Lezcano if (!ret) 5794c637b21SDaniel Lezcano continue; 5804c637b21SDaniel Lezcano 5814c637b21SDaniel Lezcano pr_err("Failed to register cpuidle device for cpu%d\n", cpu); 5824c637b21SDaniel Lezcano 5834c637b21SDaniel Lezcano cpuidle_unregister(drv); 5844c637b21SDaniel Lezcano break; 5854c637b21SDaniel Lezcano } 5864c637b21SDaniel Lezcano 5874c637b21SDaniel Lezcano return ret; 5884c637b21SDaniel Lezcano } 5894c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register); 5904c637b21SDaniel Lezcano 5914f86d3a8SLen Brown #ifdef CONFIG_SMP 5924f86d3a8SLen Brown 5934f86d3a8SLen Brown /* 5944f86d3a8SLen Brown * This function gets called when a part of the kernel has a new latency 5954f86d3a8SLen Brown * requirement. This means we need to get all processors out of their C-state, 5964f86d3a8SLen Brown * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that 5974f86d3a8SLen Brown * wakes them all right up. 5984f86d3a8SLen Brown */ 5994f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b, 6004f86d3a8SLen Brown unsigned long l, void *v) 6014f86d3a8SLen Brown { 6022ed903c5SChuansheng Liu wake_up_all_idle_cpus(); 6034f86d3a8SLen Brown return NOTIFY_OK; 6044f86d3a8SLen Brown } 6054f86d3a8SLen Brown 6064f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = { 6074f86d3a8SLen Brown .notifier_call = cpuidle_latency_notify, 6084f86d3a8SLen Brown }; 6094f86d3a8SLen Brown 610d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n) 611d82b3518SMark Gross { 612d82b3518SMark Gross pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); 613d82b3518SMark Gross } 6144f86d3a8SLen Brown 6154f86d3a8SLen Brown #else /* CONFIG_SMP */ 6164f86d3a8SLen Brown 6174f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0) 6184f86d3a8SLen Brown 6194f86d3a8SLen Brown #endif /* CONFIG_SMP */ 6204f86d3a8SLen Brown 6214f86d3a8SLen Brown /** 6224f86d3a8SLen Brown * cpuidle_init - core initializer 6234f86d3a8SLen Brown */ 6244f86d3a8SLen Brown static int __init cpuidle_init(void) 6254f86d3a8SLen Brown { 6264f86d3a8SLen Brown int ret; 6274f86d3a8SLen Brown 62862027aeaSLen Brown if (cpuidle_disabled()) 62962027aeaSLen Brown return -ENODEV; 63062027aeaSLen Brown 6318a25a2fdSKay Sievers ret = cpuidle_add_interface(cpu_subsys.dev_root); 6324f86d3a8SLen Brown if (ret) 6334f86d3a8SLen Brown return ret; 6344f86d3a8SLen Brown 6354f86d3a8SLen Brown latency_notifier_init(&cpuidle_latency_notifier); 6364f86d3a8SLen Brown 6374f86d3a8SLen Brown return 0; 6384f86d3a8SLen Brown } 6394f86d3a8SLen Brown 64062027aeaSLen Brown module_param(off, int, 0444); 6414f86d3a8SLen Brown core_initcall(cpuidle_init); 642