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> 22288f023eSArjan van de Ven #include <trace/events/power.h> 234f86d3a8SLen Brown 244f86d3a8SLen Brown #include "cpuidle.h" 254f86d3a8SLen Brown 264f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); 274c637b21SDaniel Lezcano DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev); 284f86d3a8SLen Brown 294f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock); 304f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices); 314f86d3a8SLen Brown 324f86d3a8SLen Brown static int enabled_devices; 3362027aeaSLen Brown static int off __read_mostly; 34a0bfa137SLen Brown static int initialized __read_mostly; 3562027aeaSLen Brown 3662027aeaSLen Brown int cpuidle_disabled(void) 3762027aeaSLen Brown { 3862027aeaSLen Brown return off; 3962027aeaSLen Brown } 40d91ee586SLen Brown void disable_cpuidle(void) 41d91ee586SLen Brown { 42d91ee586SLen Brown off = 1; 43d91ee586SLen Brown } 444f86d3a8SLen Brown 45dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev); 46dcb84f33SVenkatesh Pallipadi 474f86d3a8SLen Brown /** 481a022e3fSBoris Ostrovsky * cpuidle_play_dead - cpu off-lining 491a022e3fSBoris Ostrovsky * 50ee01e663SToshi Kani * Returns in case of an error or no driver 511a022e3fSBoris Ostrovsky */ 521a022e3fSBoris Ostrovsky int cpuidle_play_dead(void) 531a022e3fSBoris Ostrovsky { 541a022e3fSBoris Ostrovsky struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); 55bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 568aef33a7SDaniel Lezcano int i; 571a022e3fSBoris Ostrovsky 58ee01e663SToshi Kani if (!drv) 59ee01e663SToshi Kani return -ENODEV; 60ee01e663SToshi Kani 611a022e3fSBoris Ostrovsky /* Find lowest-power state that supports long-term idle */ 628aef33a7SDaniel Lezcano for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--) 638aef33a7SDaniel Lezcano if (drv->states[i].enter_dead) 648aef33a7SDaniel Lezcano return drv->states[i].enter_dead(dev, i); 651a022e3fSBoris Ostrovsky 661a022e3fSBoris Ostrovsky return -ENODEV; 671a022e3fSBoris Ostrovsky } 681a022e3fSBoris Ostrovsky 691a022e3fSBoris Ostrovsky /** 7056cfbf74SColin Cross * cpuidle_enter_state - enter the state and update stats 7156cfbf74SColin Cross * @dev: cpuidle device for this cpu 7256cfbf74SColin Cross * @drv: cpuidle driver for this cpu 7356cfbf74SColin Cross * @next_state: index into drv->states of the state to enter 7456cfbf74SColin Cross */ 7556cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, 76554c06baSDaniel Lezcano int index) 7756cfbf74SColin Cross { 7856cfbf74SColin Cross int entered_state; 7956cfbf74SColin Cross 80554c06baSDaniel Lezcano struct cpuidle_state *target_state = &drv->states[index]; 81554c06baSDaniel Lezcano ktime_t time_start, time_end; 82554c06baSDaniel Lezcano s64 diff; 83554c06baSDaniel Lezcano 84554c06baSDaniel Lezcano time_start = ktime_get(); 85554c06baSDaniel Lezcano 86554c06baSDaniel Lezcano entered_state = target_state->enter(dev, drv, index); 87554c06baSDaniel Lezcano 88554c06baSDaniel Lezcano time_end = ktime_get(); 89554c06baSDaniel Lezcano 90554c06baSDaniel Lezcano local_irq_enable(); 91554c06baSDaniel Lezcano 92554c06baSDaniel Lezcano diff = ktime_to_us(ktime_sub(time_end, time_start)); 93554c06baSDaniel Lezcano if (diff > INT_MAX) 94554c06baSDaniel Lezcano diff = INT_MAX; 95554c06baSDaniel Lezcano 96554c06baSDaniel Lezcano dev->last_residency = (int) diff; 9756cfbf74SColin Cross 9856cfbf74SColin Cross if (entered_state >= 0) { 9956cfbf74SColin Cross /* Update cpuidle counters */ 10056cfbf74SColin Cross /* This can be moved to within driver enter routine 10156cfbf74SColin Cross * but that results in multiple copies of same code. 10256cfbf74SColin Cross */ 103a474a515SJulius Werner dev->states_usage[entered_state].time += dev->last_residency; 10456cfbf74SColin Cross dev->states_usage[entered_state].usage++; 10556cfbf74SColin Cross } else { 10656cfbf74SColin Cross dev->last_residency = 0; 10756cfbf74SColin Cross } 10856cfbf74SColin Cross 10956cfbf74SColin Cross return entered_state; 11056cfbf74SColin Cross } 11156cfbf74SColin Cross 11256cfbf74SColin Cross /** 1134f86d3a8SLen Brown * cpuidle_idle_call - the main idle loop 1144f86d3a8SLen Brown * 1154f86d3a8SLen Brown * NOTE: no locks or semaphores should be used here 116a0bfa137SLen Brown * return non-zero on failure 1174f86d3a8SLen Brown */ 118a0bfa137SLen Brown int cpuidle_idle_call(void) 1194f86d3a8SLen Brown { 1204a6f4fe8SChristoph Lameter struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); 121bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv; 122e978aa7dSDeepthi Dharwar int next_state, entered_state; 1234f86d3a8SLen Brown 124a0bfa137SLen Brown if (off) 125a0bfa137SLen Brown return -ENODEV; 126a0bfa137SLen Brown 127a0bfa137SLen Brown if (!initialized) 128a0bfa137SLen Brown return -ENODEV; 129a0bfa137SLen Brown 1304f86d3a8SLen Brown /* check if the device is ready */ 131a0bfa137SLen Brown if (!dev || !dev->enabled) 132a0bfa137SLen Brown return -EBUSY; 1334f86d3a8SLen Brown 134bf4d1b5dSDaniel Lezcano drv = cpuidle_get_cpu_driver(dev); 135bf4d1b5dSDaniel Lezcano 1364f86d3a8SLen Brown /* ask the governor for the next state */ 13746bcfad7SDeepthi Dharwar next_state = cpuidle_curr_governor->select(drv, dev); 138246eb7f0SKevin Hilman if (need_resched()) { 139d73d68dcSYouquan Song dev->last_residency = 0; 140d73d68dcSYouquan Song /* give the governor an opportunity to reflect on the outcome */ 141d73d68dcSYouquan Song if (cpuidle_curr_governor->reflect) 142d73d68dcSYouquan Song cpuidle_curr_governor->reflect(dev, next_state); 143246eb7f0SKevin Hilman local_irq_enable(); 144a0bfa137SLen Brown return 0; 145246eb7f0SKevin Hilman } 146246eb7f0SKevin Hilman 14776027ea8SSteven Rostedt trace_cpu_idle_rcuidle(next_state, dev->cpu); 148f77cfe4eSThomas Renninger 149b60e6a0eSDaniel Lezcano if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP) 150b60e6a0eSDaniel Lezcano clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, 151b60e6a0eSDaniel Lezcano &dev->cpu); 152b60e6a0eSDaniel Lezcano 1534126c019SColin Cross if (cpuidle_state_is_coupled(dev, drv, next_state)) 1544126c019SColin Cross entered_state = cpuidle_enter_state_coupled(dev, drv, 1554126c019SColin Cross next_state); 1564126c019SColin Cross else 15756cfbf74SColin Cross entered_state = cpuidle_enter_state(dev, drv, next_state); 158f77cfe4eSThomas Renninger 159b60e6a0eSDaniel Lezcano if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP) 160b60e6a0eSDaniel Lezcano clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, 161b60e6a0eSDaniel Lezcano &dev->cpu); 162b60e6a0eSDaniel Lezcano 16376027ea8SSteven Rostedt trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu); 164f77cfe4eSThomas Renninger 1654f86d3a8SLen Brown /* give the governor an opportunity to reflect on the outcome */ 1664f86d3a8SLen Brown if (cpuidle_curr_governor->reflect) 167e978aa7dSDeepthi Dharwar cpuidle_curr_governor->reflect(dev, entered_state); 168a0bfa137SLen Brown 169a0bfa137SLen Brown return 0; 1704f86d3a8SLen Brown } 1714f86d3a8SLen Brown 1724f86d3a8SLen Brown /** 1734f86d3a8SLen Brown * cpuidle_install_idle_handler - installs the cpuidle idle loop handler 1744f86d3a8SLen Brown */ 1754f86d3a8SLen Brown void cpuidle_install_idle_handler(void) 1764f86d3a8SLen Brown { 177a0bfa137SLen Brown if (enabled_devices) { 1784f86d3a8SLen Brown /* Make sure all changes finished before we switch to new idle */ 1794f86d3a8SLen Brown smp_wmb(); 180a0bfa137SLen Brown initialized = 1; 1814f86d3a8SLen Brown } 1824f86d3a8SLen Brown } 1834f86d3a8SLen Brown 1844f86d3a8SLen Brown /** 1854f86d3a8SLen Brown * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler 1864f86d3a8SLen Brown */ 1874f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void) 1884f86d3a8SLen Brown { 189a0bfa137SLen Brown if (enabled_devices) { 190a0bfa137SLen Brown initialized = 0; 1914a162513SThomas Gleixner kick_all_cpus_sync(); 1924f86d3a8SLen Brown } 1934f86d3a8SLen Brown } 1944f86d3a8SLen Brown 1954f86d3a8SLen Brown /** 1964f86d3a8SLen Brown * cpuidle_pause_and_lock - temporarily disables CPUIDLE 1974f86d3a8SLen Brown */ 1984f86d3a8SLen Brown void cpuidle_pause_and_lock(void) 1994f86d3a8SLen Brown { 2004f86d3a8SLen Brown mutex_lock(&cpuidle_lock); 2014f86d3a8SLen Brown cpuidle_uninstall_idle_handler(); 2024f86d3a8SLen Brown } 2034f86d3a8SLen Brown 2044f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); 2054f86d3a8SLen Brown 2064f86d3a8SLen Brown /** 2074f86d3a8SLen Brown * cpuidle_resume_and_unlock - resumes CPUIDLE operation 2084f86d3a8SLen Brown */ 2094f86d3a8SLen Brown void cpuidle_resume_and_unlock(void) 2104f86d3a8SLen Brown { 2114f86d3a8SLen Brown cpuidle_install_idle_handler(); 2124f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 2134f86d3a8SLen Brown } 2144f86d3a8SLen Brown 2154f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); 2164f86d3a8SLen Brown 2178651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */ 2188651f97bSPreeti U Murthy void cpuidle_pause(void) 2198651f97bSPreeti U Murthy { 2208651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 2218651f97bSPreeti U Murthy cpuidle_uninstall_idle_handler(); 2228651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 2238651f97bSPreeti U Murthy } 2248651f97bSPreeti U Murthy 2258651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */ 2268651f97bSPreeti U Murthy void cpuidle_resume(void) 2278651f97bSPreeti U Murthy { 2288651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 2298651f97bSPreeti U Murthy cpuidle_install_idle_handler(); 2308651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 2318651f97bSPreeti U Murthy } 2328651f97bSPreeti U Murthy 233d8c216cfSRafael J. Wysocki #ifdef CONFIG_ARCH_HAS_CPU_RELAX 23446bcfad7SDeepthi Dharwar static int poll_idle(struct cpuidle_device *dev, 23546bcfad7SDeepthi Dharwar struct cpuidle_driver *drv, int index) 236d8c216cfSRafael J. Wysocki { 237d8c216cfSRafael J. Wysocki ktime_t t1, t2; 238d8c216cfSRafael J. Wysocki s64 diff; 239d8c216cfSRafael J. Wysocki 240d8c216cfSRafael J. Wysocki t1 = ktime_get(); 241d8c216cfSRafael J. Wysocki local_irq_enable(); 242d8c216cfSRafael J. Wysocki while (!need_resched()) 243d8c216cfSRafael J. Wysocki cpu_relax(); 244d8c216cfSRafael J. Wysocki 245d8c216cfSRafael J. Wysocki t2 = ktime_get(); 246d8c216cfSRafael J. Wysocki diff = ktime_to_us(ktime_sub(t2, t1)); 247d8c216cfSRafael J. Wysocki if (diff > INT_MAX) 248d8c216cfSRafael J. Wysocki diff = INT_MAX; 249d8c216cfSRafael J. Wysocki 250e978aa7dSDeepthi Dharwar dev->last_residency = (int) diff; 251e978aa7dSDeepthi Dharwar 252e978aa7dSDeepthi Dharwar return index; 253d8c216cfSRafael J. Wysocki } 254d8c216cfSRafael J. Wysocki 25546bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) 256d8c216cfSRafael J. Wysocki { 25746bcfad7SDeepthi Dharwar struct cpuidle_state *state = &drv->states[0]; 258d8c216cfSRafael J. Wysocki 259720f1c30SThomas Renninger snprintf(state->name, CPUIDLE_NAME_LEN, "POLL"); 260d8c216cfSRafael J. Wysocki snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE"); 261d8c216cfSRafael J. Wysocki state->exit_latency = 0; 262d8c216cfSRafael J. Wysocki state->target_residency = 0; 263d8c216cfSRafael J. Wysocki state->power_usage = -1; 264d247632cSLen Brown state->flags = 0; 265d8c216cfSRafael J. Wysocki state->enter = poll_idle; 266cbc9ef02SRafael J. Wysocki state->disabled = false; 267d8c216cfSRafael J. Wysocki } 268d8c216cfSRafael J. Wysocki #else 26946bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) {} 270d8c216cfSRafael J. Wysocki #endif /* CONFIG_ARCH_HAS_CPU_RELAX */ 271d8c216cfSRafael J. Wysocki 2724f86d3a8SLen Brown /** 2734f86d3a8SLen Brown * cpuidle_enable_device - enables idle PM for a CPU 2744f86d3a8SLen Brown * @dev: the CPU 2754f86d3a8SLen Brown * 2764f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 2774f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 2784f86d3a8SLen Brown */ 2794f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev) 2804f86d3a8SLen Brown { 2814f86d3a8SLen Brown int ret, i; 282bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv; 2834f86d3a8SLen Brown 2841b0a0e9aSSrivatsa S. Bhat if (!dev) 2851b0a0e9aSSrivatsa S. Bhat return -EINVAL; 2861b0a0e9aSSrivatsa S. Bhat 2874f86d3a8SLen Brown if (dev->enabled) 2884f86d3a8SLen Brown return 0; 289bf4d1b5dSDaniel Lezcano 290bf4d1b5dSDaniel Lezcano drv = cpuidle_get_cpu_driver(dev); 291bf4d1b5dSDaniel Lezcano 292e1689795SRobert Lee if (!drv || !cpuidle_curr_governor) 2934f86d3a8SLen Brown return -EIO; 294bf4d1b5dSDaniel Lezcano 295*10b9d3f8SDaniel Lezcano if (!dev->registered) 296*10b9d3f8SDaniel Lezcano return -EINVAL; 297*10b9d3f8SDaniel Lezcano 2984f86d3a8SLen Brown if (!dev->state_count) 299fc850f39SDaniel Lezcano dev->state_count = drv->state_count; 3004f86d3a8SLen Brown 301e1689795SRobert Lee poll_idle_init(drv); 302d8c216cfSRafael J. Wysocki 303bf4d1b5dSDaniel Lezcano ret = cpuidle_add_device_sysfs(dev); 304bf4d1b5dSDaniel Lezcano if (ret) 3054f86d3a8SLen Brown return ret; 3064f86d3a8SLen Brown 3074f86d3a8SLen Brown if (cpuidle_curr_governor->enable && 308e1689795SRobert Lee (ret = cpuidle_curr_governor->enable(drv, dev))) 3094f86d3a8SLen Brown goto fail_sysfs; 3104f86d3a8SLen Brown 3114f86d3a8SLen Brown for (i = 0; i < dev->state_count; i++) { 3124202735eSDeepthi Dharwar dev->states_usage[i].usage = 0; 3134202735eSDeepthi Dharwar dev->states_usage[i].time = 0; 3144f86d3a8SLen Brown } 3154f86d3a8SLen Brown dev->last_residency = 0; 3164f86d3a8SLen Brown 3174f86d3a8SLen Brown smp_wmb(); 3184f86d3a8SLen Brown 3194f86d3a8SLen Brown dev->enabled = 1; 3204f86d3a8SLen Brown 3214f86d3a8SLen Brown enabled_devices++; 3224f86d3a8SLen Brown return 0; 3234f86d3a8SLen Brown 3244f86d3a8SLen Brown fail_sysfs: 325bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 3264f86d3a8SLen Brown 3274f86d3a8SLen Brown return ret; 3284f86d3a8SLen Brown } 3294f86d3a8SLen Brown 3304f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device); 3314f86d3a8SLen Brown 3324f86d3a8SLen Brown /** 3334f86d3a8SLen Brown * cpuidle_disable_device - disables idle PM for a CPU 3344f86d3a8SLen Brown * @dev: the CPU 3354f86d3a8SLen Brown * 3364f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 3374f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 3384f86d3a8SLen Brown */ 3394f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev) 3404f86d3a8SLen Brown { 341bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 342bf4d1b5dSDaniel Lezcano 343cf31cd1aSSrivatsa S. Bhat if (!dev || !dev->enabled) 3444f86d3a8SLen Brown return; 345bf4d1b5dSDaniel Lezcano 346bf4d1b5dSDaniel Lezcano if (!drv || !cpuidle_curr_governor) 3474f86d3a8SLen Brown return; 3484f86d3a8SLen Brown 3494f86d3a8SLen Brown dev->enabled = 0; 3504f86d3a8SLen Brown 3514f86d3a8SLen Brown if (cpuidle_curr_governor->disable) 352bf4d1b5dSDaniel Lezcano cpuidle_curr_governor->disable(drv, dev); 3534f86d3a8SLen Brown 354bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 3554f86d3a8SLen Brown enabled_devices--; 3564f86d3a8SLen Brown } 3574f86d3a8SLen Brown 3584f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device); 3594f86d3a8SLen Brown 3604f86d3a8SLen Brown /** 361dcb84f33SVenkatesh Pallipadi * __cpuidle_register_device - internal register function called before register 362dcb84f33SVenkatesh Pallipadi * and enable routines 3634f86d3a8SLen Brown * @dev: the cpu 364dcb84f33SVenkatesh Pallipadi * 365dcb84f33SVenkatesh Pallipadi * cpuidle_lock mutex must be held before this is called 3664f86d3a8SLen Brown */ 367dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev) 3684f86d3a8SLen Brown { 3694f86d3a8SLen Brown int ret; 370bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 3714f86d3a8SLen Brown 372bf4d1b5dSDaniel Lezcano if (!try_module_get(drv->owner)) 3734f86d3a8SLen Brown return -EINVAL; 3744f86d3a8SLen Brown 3754f86d3a8SLen Brown per_cpu(cpuidle_devices, dev->cpu) = dev; 3764f86d3a8SLen Brown list_add(&dev->device_list, &cpuidle_detected_devices); 3771aef40e2SDaniel Lezcano ret = cpuidle_add_sysfs(dev); 3783af272abSColin Cross if (ret) 3793af272abSColin Cross goto err_sysfs; 3804f86d3a8SLen Brown 3814126c019SColin Cross ret = cpuidle_coupled_register_device(dev); 3824126c019SColin Cross if (ret) 3834126c019SColin Cross goto err_coupled; 3844f86d3a8SLen Brown 385dcb84f33SVenkatesh Pallipadi dev->registered = 1; 386dcb84f33SVenkatesh Pallipadi return 0; 3873af272abSColin Cross 3884126c019SColin Cross err_coupled: 3891aef40e2SDaniel Lezcano cpuidle_remove_sysfs(dev); 3903af272abSColin Cross err_sysfs: 3913af272abSColin Cross list_del(&dev->device_list); 3923af272abSColin Cross per_cpu(cpuidle_devices, dev->cpu) = NULL; 393bf4d1b5dSDaniel Lezcano module_put(drv->owner); 3943af272abSColin Cross return ret; 395dcb84f33SVenkatesh Pallipadi } 396dcb84f33SVenkatesh Pallipadi 397dcb84f33SVenkatesh Pallipadi /** 398dcb84f33SVenkatesh Pallipadi * cpuidle_register_device - registers a CPU's idle PM feature 399dcb84f33SVenkatesh Pallipadi * @dev: the cpu 400dcb84f33SVenkatesh Pallipadi */ 401dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev) 402dcb84f33SVenkatesh Pallipadi { 403dcb84f33SVenkatesh Pallipadi int ret; 404dcb84f33SVenkatesh Pallipadi 4051b0a0e9aSSrivatsa S. Bhat if (!dev) 4061b0a0e9aSSrivatsa S. Bhat return -EINVAL; 4071b0a0e9aSSrivatsa S. Bhat 408dcb84f33SVenkatesh Pallipadi mutex_lock(&cpuidle_lock); 409dcb84f33SVenkatesh Pallipadi 410dcb84f33SVenkatesh Pallipadi if ((ret = __cpuidle_register_device(dev))) { 411dcb84f33SVenkatesh Pallipadi mutex_unlock(&cpuidle_lock); 412dcb84f33SVenkatesh Pallipadi return ret; 413dcb84f33SVenkatesh Pallipadi } 414dcb84f33SVenkatesh Pallipadi 415*10b9d3f8SDaniel Lezcano ret = cpuidle_enable_device(dev); 416*10b9d3f8SDaniel Lezcano if (ret) { 417*10b9d3f8SDaniel Lezcano mutex_unlock(&cpuidle_lock); 418*10b9d3f8SDaniel Lezcano return ret; 419*10b9d3f8SDaniel Lezcano } 420*10b9d3f8SDaniel Lezcano 4214f86d3a8SLen Brown cpuidle_install_idle_handler(); 4224f86d3a8SLen Brown 4234f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 4244f86d3a8SLen Brown 4254f86d3a8SLen Brown return 0; 4264f86d3a8SLen Brown } 4274f86d3a8SLen Brown 4284f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device); 4294f86d3a8SLen Brown 4304f86d3a8SLen Brown /** 4314f86d3a8SLen Brown * cpuidle_unregister_device - unregisters a CPU's idle PM feature 4324f86d3a8SLen Brown * @dev: the cpu 4334f86d3a8SLen Brown */ 4344f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev) 4354f86d3a8SLen Brown { 436bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 4374f86d3a8SLen Brown 438dcb84f33SVenkatesh Pallipadi if (dev->registered == 0) 439dcb84f33SVenkatesh Pallipadi return; 440dcb84f33SVenkatesh Pallipadi 4414f86d3a8SLen Brown cpuidle_pause_and_lock(); 4424f86d3a8SLen Brown 4434f86d3a8SLen Brown cpuidle_disable_device(dev); 4444f86d3a8SLen Brown 4451aef40e2SDaniel Lezcano cpuidle_remove_sysfs(dev); 4464f86d3a8SLen Brown list_del(&dev->device_list); 4474f86d3a8SLen Brown per_cpu(cpuidle_devices, dev->cpu) = NULL; 4484f86d3a8SLen Brown 4494126c019SColin Cross cpuidle_coupled_unregister_device(dev); 4504126c019SColin Cross 4514f86d3a8SLen Brown cpuidle_resume_and_unlock(); 4524f86d3a8SLen Brown 453bf4d1b5dSDaniel Lezcano module_put(drv->owner); 4544f86d3a8SLen Brown } 4554f86d3a8SLen Brown 4564f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device); 4574f86d3a8SLen Brown 4581c192d04SDaniel Lezcano /** 4594c637b21SDaniel Lezcano * cpuidle_unregister: unregister a driver and the devices. This function 4604c637b21SDaniel Lezcano * can be used only if the driver has been previously registered through 4614c637b21SDaniel Lezcano * the cpuidle_register function. 4624c637b21SDaniel Lezcano * 4634c637b21SDaniel Lezcano * @drv: a valid pointer to a struct cpuidle_driver 4644c637b21SDaniel Lezcano */ 4654c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv) 4664c637b21SDaniel Lezcano { 4674c637b21SDaniel Lezcano int cpu; 4684c637b21SDaniel Lezcano struct cpuidle_device *device; 4694c637b21SDaniel Lezcano 47082467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 4714c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 4724c637b21SDaniel Lezcano cpuidle_unregister_device(device); 4734c637b21SDaniel Lezcano } 4744c637b21SDaniel Lezcano 4754c637b21SDaniel Lezcano cpuidle_unregister_driver(drv); 4764c637b21SDaniel Lezcano } 4774c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister); 4784c637b21SDaniel Lezcano 4794c637b21SDaniel Lezcano /** 4804c637b21SDaniel Lezcano * cpuidle_register: registers the driver and the cpu devices with the 4814c637b21SDaniel Lezcano * coupled_cpus passed as parameter. This function is used for all common 4824c637b21SDaniel Lezcano * initialization pattern there are in the arch specific drivers. The 4834c637b21SDaniel Lezcano * devices is globally defined in this file. 4844c637b21SDaniel Lezcano * 4854c637b21SDaniel Lezcano * @drv : a valid pointer to a struct cpuidle_driver 4864c637b21SDaniel Lezcano * @coupled_cpus: a cpumask for the coupled states 4874c637b21SDaniel Lezcano * 4884c637b21SDaniel Lezcano * Returns 0 on success, < 0 otherwise 4894c637b21SDaniel Lezcano */ 4904c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv, 4914c637b21SDaniel Lezcano const struct cpumask *const coupled_cpus) 4924c637b21SDaniel Lezcano { 4934c637b21SDaniel Lezcano int ret, cpu; 4944c637b21SDaniel Lezcano struct cpuidle_device *device; 4954c637b21SDaniel Lezcano 4964c637b21SDaniel Lezcano ret = cpuidle_register_driver(drv); 4974c637b21SDaniel Lezcano if (ret) { 4984c637b21SDaniel Lezcano pr_err("failed to register cpuidle driver\n"); 4994c637b21SDaniel Lezcano return ret; 5004c637b21SDaniel Lezcano } 5014c637b21SDaniel Lezcano 50282467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 5034c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 5044c637b21SDaniel Lezcano device->cpu = cpu; 5054c637b21SDaniel Lezcano 5064c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 5074c637b21SDaniel Lezcano /* 5084c637b21SDaniel Lezcano * On multiplatform for ARM, the coupled idle states could 5094c637b21SDaniel Lezcano * enabled in the kernel even if the cpuidle driver does not 5104c637b21SDaniel Lezcano * use it. Note, coupled_cpus is a struct copy. 5114c637b21SDaniel Lezcano */ 5124c637b21SDaniel Lezcano if (coupled_cpus) 5134c637b21SDaniel Lezcano device->coupled_cpus = *coupled_cpus; 5144c637b21SDaniel Lezcano #endif 5154c637b21SDaniel Lezcano ret = cpuidle_register_device(device); 5164c637b21SDaniel Lezcano if (!ret) 5174c637b21SDaniel Lezcano continue; 5184c637b21SDaniel Lezcano 5194c637b21SDaniel Lezcano pr_err("Failed to register cpuidle device for cpu%d\n", cpu); 5204c637b21SDaniel Lezcano 5214c637b21SDaniel Lezcano cpuidle_unregister(drv); 5224c637b21SDaniel Lezcano break; 5234c637b21SDaniel Lezcano } 5244c637b21SDaniel Lezcano 5254c637b21SDaniel Lezcano return ret; 5264c637b21SDaniel Lezcano } 5274c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register); 5284c637b21SDaniel Lezcano 5294f86d3a8SLen Brown #ifdef CONFIG_SMP 5304f86d3a8SLen Brown 5314f86d3a8SLen Brown static void smp_callback(void *v) 5324f86d3a8SLen Brown { 5334f86d3a8SLen Brown /* we already woke the CPU up, nothing more to do */ 5344f86d3a8SLen Brown } 5354f86d3a8SLen Brown 5364f86d3a8SLen Brown /* 5374f86d3a8SLen Brown * This function gets called when a part of the kernel has a new latency 5384f86d3a8SLen Brown * requirement. This means we need to get all processors out of their C-state, 5394f86d3a8SLen Brown * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that 5404f86d3a8SLen Brown * wakes them all right up. 5414f86d3a8SLen Brown */ 5424f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b, 5434f86d3a8SLen Brown unsigned long l, void *v) 5444f86d3a8SLen Brown { 5458691e5a8SJens Axboe smp_call_function(smp_callback, NULL, 1); 5464f86d3a8SLen Brown return NOTIFY_OK; 5474f86d3a8SLen Brown } 5484f86d3a8SLen Brown 5494f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = { 5504f86d3a8SLen Brown .notifier_call = cpuidle_latency_notify, 5514f86d3a8SLen Brown }; 5524f86d3a8SLen Brown 553d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n) 554d82b3518SMark Gross { 555d82b3518SMark Gross pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); 556d82b3518SMark Gross } 5574f86d3a8SLen Brown 5584f86d3a8SLen Brown #else /* CONFIG_SMP */ 5594f86d3a8SLen Brown 5604f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0) 5614f86d3a8SLen Brown 5624f86d3a8SLen Brown #endif /* CONFIG_SMP */ 5634f86d3a8SLen Brown 5644f86d3a8SLen Brown /** 5654f86d3a8SLen Brown * cpuidle_init - core initializer 5664f86d3a8SLen Brown */ 5674f86d3a8SLen Brown static int __init cpuidle_init(void) 5684f86d3a8SLen Brown { 5694f86d3a8SLen Brown int ret; 5704f86d3a8SLen Brown 57162027aeaSLen Brown if (cpuidle_disabled()) 57262027aeaSLen Brown return -ENODEV; 57362027aeaSLen Brown 5748a25a2fdSKay Sievers ret = cpuidle_add_interface(cpu_subsys.dev_root); 5754f86d3a8SLen Brown if (ret) 5764f86d3a8SLen Brown return ret; 5774f86d3a8SLen Brown 5784f86d3a8SLen Brown latency_notifier_init(&cpuidle_latency_notifier); 5794f86d3a8SLen Brown 5804f86d3a8SLen Brown return 0; 5814f86d3a8SLen Brown } 5824f86d3a8SLen Brown 58362027aeaSLen Brown module_param(off, int, 0444); 5844f86d3a8SLen Brown core_initcall(cpuidle_init); 585