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 454f86d3a8SLen Brown /** 461a022e3fSBoris Ostrovsky * cpuidle_play_dead - cpu off-lining 471a022e3fSBoris Ostrovsky * 48ee01e663SToshi Kani * Returns in case of an error or no driver 491a022e3fSBoris Ostrovsky */ 501a022e3fSBoris Ostrovsky int cpuidle_play_dead(void) 511a022e3fSBoris Ostrovsky { 521a022e3fSBoris Ostrovsky struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); 53bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 548aef33a7SDaniel Lezcano int i; 551a022e3fSBoris Ostrovsky 56ee01e663SToshi Kani if (!drv) 57ee01e663SToshi Kani return -ENODEV; 58ee01e663SToshi Kani 591a022e3fSBoris Ostrovsky /* Find lowest-power state that supports long-term idle */ 608aef33a7SDaniel Lezcano for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--) 618aef33a7SDaniel Lezcano if (drv->states[i].enter_dead) 628aef33a7SDaniel Lezcano return drv->states[i].enter_dead(dev, i); 631a022e3fSBoris Ostrovsky 641a022e3fSBoris Ostrovsky return -ENODEV; 651a022e3fSBoris Ostrovsky } 661a022e3fSBoris Ostrovsky 671a022e3fSBoris Ostrovsky /** 6856cfbf74SColin Cross * cpuidle_enter_state - enter the state and update stats 6956cfbf74SColin Cross * @dev: cpuidle device for this cpu 7056cfbf74SColin Cross * @drv: cpuidle driver for this cpu 7156cfbf74SColin Cross * @next_state: index into drv->states of the state to enter 7256cfbf74SColin Cross */ 7356cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, 74554c06baSDaniel Lezcano int index) 7556cfbf74SColin Cross { 7656cfbf74SColin Cross int entered_state; 7756cfbf74SColin Cross 78554c06baSDaniel Lezcano struct cpuidle_state *target_state = &drv->states[index]; 79554c06baSDaniel Lezcano ktime_t time_start, time_end; 80554c06baSDaniel Lezcano s64 diff; 81554c06baSDaniel Lezcano 82554c06baSDaniel Lezcano time_start = ktime_get(); 83554c06baSDaniel Lezcano 84554c06baSDaniel Lezcano entered_state = target_state->enter(dev, drv, index); 85554c06baSDaniel Lezcano 86554c06baSDaniel Lezcano time_end = ktime_get(); 87554c06baSDaniel Lezcano 88554c06baSDaniel Lezcano local_irq_enable(); 89554c06baSDaniel Lezcano 90554c06baSDaniel Lezcano diff = ktime_to_us(ktime_sub(time_end, time_start)); 91554c06baSDaniel Lezcano if (diff > INT_MAX) 92554c06baSDaniel Lezcano diff = INT_MAX; 93554c06baSDaniel Lezcano 94554c06baSDaniel Lezcano dev->last_residency = (int) diff; 9556cfbf74SColin Cross 9656cfbf74SColin Cross if (entered_state >= 0) { 9756cfbf74SColin Cross /* Update cpuidle counters */ 9856cfbf74SColin Cross /* This can be moved to within driver enter routine 9956cfbf74SColin Cross * but that results in multiple copies of same code. 10056cfbf74SColin Cross */ 101a474a515SJulius Werner dev->states_usage[entered_state].time += dev->last_residency; 10256cfbf74SColin Cross dev->states_usage[entered_state].usage++; 10356cfbf74SColin Cross } else { 10456cfbf74SColin Cross dev->last_residency = 0; 10556cfbf74SColin Cross } 10656cfbf74SColin Cross 10756cfbf74SColin Cross return entered_state; 10856cfbf74SColin Cross } 10956cfbf74SColin Cross 11056cfbf74SColin Cross /** 1114f86d3a8SLen Brown * cpuidle_idle_call - the main idle loop 1124f86d3a8SLen Brown * 1134f86d3a8SLen Brown * NOTE: no locks or semaphores should be used here 114a0bfa137SLen Brown * return non-zero on failure 1154f86d3a8SLen Brown */ 116a0bfa137SLen Brown int cpuidle_idle_call(void) 1174f86d3a8SLen Brown { 1184a6f4fe8SChristoph Lameter struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); 119bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv; 120e978aa7dSDeepthi Dharwar int next_state, entered_state; 1214f86d3a8SLen Brown 122a0bfa137SLen Brown if (off) 123a0bfa137SLen Brown return -ENODEV; 124a0bfa137SLen Brown 125a0bfa137SLen Brown if (!initialized) 126a0bfa137SLen Brown return -ENODEV; 127a0bfa137SLen Brown 1284f86d3a8SLen Brown /* check if the device is ready */ 129a0bfa137SLen Brown if (!dev || !dev->enabled) 130a0bfa137SLen Brown return -EBUSY; 1314f86d3a8SLen Brown 132bf4d1b5dSDaniel Lezcano drv = cpuidle_get_cpu_driver(dev); 133bf4d1b5dSDaniel Lezcano 1344f86d3a8SLen Brown /* ask the governor for the next state */ 13546bcfad7SDeepthi Dharwar next_state = cpuidle_curr_governor->select(drv, dev); 136246eb7f0SKevin Hilman if (need_resched()) { 137d73d68dcSYouquan Song dev->last_residency = 0; 138d73d68dcSYouquan Song /* give the governor an opportunity to reflect on the outcome */ 139d73d68dcSYouquan Song if (cpuidle_curr_governor->reflect) 140d73d68dcSYouquan Song cpuidle_curr_governor->reflect(dev, next_state); 141246eb7f0SKevin Hilman local_irq_enable(); 142a0bfa137SLen Brown return 0; 143246eb7f0SKevin Hilman } 144246eb7f0SKevin Hilman 14576027ea8SSteven Rostedt trace_cpu_idle_rcuidle(next_state, dev->cpu); 146f77cfe4eSThomas Renninger 147b60e6a0eSDaniel Lezcano if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP) 148b60e6a0eSDaniel Lezcano clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, 149b60e6a0eSDaniel Lezcano &dev->cpu); 150b60e6a0eSDaniel Lezcano 1514126c019SColin Cross if (cpuidle_state_is_coupled(dev, drv, next_state)) 1524126c019SColin Cross entered_state = cpuidle_enter_state_coupled(dev, drv, 1534126c019SColin Cross next_state); 1544126c019SColin Cross else 15556cfbf74SColin Cross entered_state = cpuidle_enter_state(dev, drv, next_state); 156f77cfe4eSThomas Renninger 157b60e6a0eSDaniel Lezcano if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP) 158b60e6a0eSDaniel Lezcano clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, 159b60e6a0eSDaniel Lezcano &dev->cpu); 160b60e6a0eSDaniel Lezcano 16176027ea8SSteven Rostedt trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu); 162f77cfe4eSThomas Renninger 1634f86d3a8SLen Brown /* give the governor an opportunity to reflect on the outcome */ 1644f86d3a8SLen Brown if (cpuidle_curr_governor->reflect) 165e978aa7dSDeepthi Dharwar cpuidle_curr_governor->reflect(dev, entered_state); 166a0bfa137SLen Brown 167a0bfa137SLen Brown return 0; 1684f86d3a8SLen Brown } 1694f86d3a8SLen Brown 1704f86d3a8SLen Brown /** 1714f86d3a8SLen Brown * cpuidle_install_idle_handler - installs the cpuidle idle loop handler 1724f86d3a8SLen Brown */ 1734f86d3a8SLen Brown void cpuidle_install_idle_handler(void) 1744f86d3a8SLen Brown { 175a0bfa137SLen Brown if (enabled_devices) { 1764f86d3a8SLen Brown /* Make sure all changes finished before we switch to new idle */ 1774f86d3a8SLen Brown smp_wmb(); 178a0bfa137SLen Brown initialized = 1; 1794f86d3a8SLen Brown } 1804f86d3a8SLen Brown } 1814f86d3a8SLen Brown 1824f86d3a8SLen Brown /** 1834f86d3a8SLen Brown * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler 1844f86d3a8SLen Brown */ 1854f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void) 1864f86d3a8SLen Brown { 187a0bfa137SLen Brown if (enabled_devices) { 188a0bfa137SLen Brown initialized = 0; 1894a162513SThomas Gleixner kick_all_cpus_sync(); 1904f86d3a8SLen Brown } 1914f86d3a8SLen Brown } 1924f86d3a8SLen Brown 1934f86d3a8SLen Brown /** 1944f86d3a8SLen Brown * cpuidle_pause_and_lock - temporarily disables CPUIDLE 1954f86d3a8SLen Brown */ 1964f86d3a8SLen Brown void cpuidle_pause_and_lock(void) 1974f86d3a8SLen Brown { 1984f86d3a8SLen Brown mutex_lock(&cpuidle_lock); 1994f86d3a8SLen Brown cpuidle_uninstall_idle_handler(); 2004f86d3a8SLen Brown } 2014f86d3a8SLen Brown 2024f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); 2034f86d3a8SLen Brown 2044f86d3a8SLen Brown /** 2054f86d3a8SLen Brown * cpuidle_resume_and_unlock - resumes CPUIDLE operation 2064f86d3a8SLen Brown */ 2074f86d3a8SLen Brown void cpuidle_resume_and_unlock(void) 2084f86d3a8SLen Brown { 2094f86d3a8SLen Brown cpuidle_install_idle_handler(); 2104f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 2114f86d3a8SLen Brown } 2124f86d3a8SLen Brown 2134f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); 2144f86d3a8SLen Brown 2158651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */ 2168651f97bSPreeti U Murthy void cpuidle_pause(void) 2178651f97bSPreeti U Murthy { 2188651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 2198651f97bSPreeti U Murthy cpuidle_uninstall_idle_handler(); 2208651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 2218651f97bSPreeti U Murthy } 2228651f97bSPreeti U Murthy 2238651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */ 2248651f97bSPreeti U Murthy void cpuidle_resume(void) 2258651f97bSPreeti U Murthy { 2268651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 2278651f97bSPreeti U Murthy cpuidle_install_idle_handler(); 2288651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 2298651f97bSPreeti U Murthy } 2308651f97bSPreeti U Murthy 231d8c216cfSRafael J. Wysocki #ifdef CONFIG_ARCH_HAS_CPU_RELAX 23246bcfad7SDeepthi Dharwar static int poll_idle(struct cpuidle_device *dev, 23346bcfad7SDeepthi Dharwar struct cpuidle_driver *drv, int index) 234d8c216cfSRafael J. Wysocki { 235d8c216cfSRafael J. Wysocki ktime_t t1, t2; 236d8c216cfSRafael J. Wysocki s64 diff; 237d8c216cfSRafael J. Wysocki 238d8c216cfSRafael J. Wysocki t1 = ktime_get(); 239d8c216cfSRafael J. Wysocki local_irq_enable(); 240d8c216cfSRafael J. Wysocki while (!need_resched()) 241d8c216cfSRafael J. Wysocki cpu_relax(); 242d8c216cfSRafael J. Wysocki 243d8c216cfSRafael J. Wysocki t2 = ktime_get(); 244d8c216cfSRafael J. Wysocki diff = ktime_to_us(ktime_sub(t2, t1)); 245d8c216cfSRafael J. Wysocki if (diff > INT_MAX) 246d8c216cfSRafael J. Wysocki diff = INT_MAX; 247d8c216cfSRafael J. Wysocki 248e978aa7dSDeepthi Dharwar dev->last_residency = (int) diff; 249e978aa7dSDeepthi Dharwar 250e978aa7dSDeepthi Dharwar return index; 251d8c216cfSRafael J. Wysocki } 252d8c216cfSRafael J. Wysocki 25346bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) 254d8c216cfSRafael J. Wysocki { 25546bcfad7SDeepthi Dharwar struct cpuidle_state *state = &drv->states[0]; 256d8c216cfSRafael J. Wysocki 257720f1c30SThomas Renninger snprintf(state->name, CPUIDLE_NAME_LEN, "POLL"); 258d8c216cfSRafael J. Wysocki snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE"); 259d8c216cfSRafael J. Wysocki state->exit_latency = 0; 260d8c216cfSRafael J. Wysocki state->target_residency = 0; 261d8c216cfSRafael J. Wysocki state->power_usage = -1; 262d247632cSLen Brown state->flags = 0; 263d8c216cfSRafael J. Wysocki state->enter = poll_idle; 264cbc9ef02SRafael J. Wysocki state->disabled = false; 265d8c216cfSRafael J. Wysocki } 266d8c216cfSRafael J. Wysocki #else 26746bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) {} 268d8c216cfSRafael J. Wysocki #endif /* CONFIG_ARCH_HAS_CPU_RELAX */ 269d8c216cfSRafael J. Wysocki 2704f86d3a8SLen Brown /** 2714f86d3a8SLen Brown * cpuidle_enable_device - enables idle PM for a CPU 2724f86d3a8SLen Brown * @dev: the CPU 2734f86d3a8SLen Brown * 2744f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 2754f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 2764f86d3a8SLen Brown */ 2774f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev) 2784f86d3a8SLen Brown { 2795df0aa73SDaniel Lezcano int ret; 280bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv; 2814f86d3a8SLen Brown 2821b0a0e9aSSrivatsa S. Bhat if (!dev) 2831b0a0e9aSSrivatsa S. Bhat return -EINVAL; 2841b0a0e9aSSrivatsa S. Bhat 2854f86d3a8SLen Brown if (dev->enabled) 2864f86d3a8SLen Brown return 0; 287bf4d1b5dSDaniel Lezcano 288bf4d1b5dSDaniel Lezcano drv = cpuidle_get_cpu_driver(dev); 289bf4d1b5dSDaniel Lezcano 290e1689795SRobert Lee if (!drv || !cpuidle_curr_governor) 2914f86d3a8SLen Brown return -EIO; 292bf4d1b5dSDaniel Lezcano 29310b9d3f8SDaniel Lezcano if (!dev->registered) 29410b9d3f8SDaniel Lezcano return -EINVAL; 29510b9d3f8SDaniel Lezcano 2964f86d3a8SLen Brown if (!dev->state_count) 297fc850f39SDaniel Lezcano dev->state_count = drv->state_count; 2984f86d3a8SLen Brown 299e1689795SRobert Lee poll_idle_init(drv); 300d8c216cfSRafael J. Wysocki 301bf4d1b5dSDaniel Lezcano ret = cpuidle_add_device_sysfs(dev); 302bf4d1b5dSDaniel Lezcano if (ret) 3034f86d3a8SLen Brown return ret; 3044f86d3a8SLen Brown 3054f86d3a8SLen Brown if (cpuidle_curr_governor->enable && 306e1689795SRobert Lee (ret = cpuidle_curr_governor->enable(drv, dev))) 3074f86d3a8SLen Brown goto fail_sysfs; 3084f86d3a8SLen Brown 3094f86d3a8SLen Brown smp_wmb(); 3104f86d3a8SLen Brown 3114f86d3a8SLen Brown dev->enabled = 1; 3124f86d3a8SLen Brown 3134f86d3a8SLen Brown enabled_devices++; 3144f86d3a8SLen Brown return 0; 3154f86d3a8SLen Brown 3164f86d3a8SLen Brown fail_sysfs: 317bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 3184f86d3a8SLen Brown 3194f86d3a8SLen Brown return ret; 3204f86d3a8SLen Brown } 3214f86d3a8SLen Brown 3224f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device); 3234f86d3a8SLen Brown 3244f86d3a8SLen Brown /** 3254f86d3a8SLen Brown * cpuidle_disable_device - disables idle PM for a CPU 3264f86d3a8SLen Brown * @dev: the CPU 3274f86d3a8SLen Brown * 3284f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 3294f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 3304f86d3a8SLen Brown */ 3314f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev) 3324f86d3a8SLen Brown { 333bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 334bf4d1b5dSDaniel Lezcano 335cf31cd1aSSrivatsa S. Bhat if (!dev || !dev->enabled) 3364f86d3a8SLen Brown return; 337bf4d1b5dSDaniel Lezcano 338bf4d1b5dSDaniel Lezcano if (!drv || !cpuidle_curr_governor) 3394f86d3a8SLen Brown return; 3404f86d3a8SLen Brown 3414f86d3a8SLen Brown dev->enabled = 0; 3424f86d3a8SLen Brown 3434f86d3a8SLen Brown if (cpuidle_curr_governor->disable) 344bf4d1b5dSDaniel Lezcano cpuidle_curr_governor->disable(drv, dev); 3454f86d3a8SLen Brown 346bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 3474f86d3a8SLen Brown enabled_devices--; 3484f86d3a8SLen Brown } 3494f86d3a8SLen Brown 3504f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device); 3514f86d3a8SLen Brown 352f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev) 353f6bb51a5SDaniel Lezcano { 354f6bb51a5SDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 355f6bb51a5SDaniel Lezcano 356f6bb51a5SDaniel Lezcano list_del(&dev->device_list); 357f6bb51a5SDaniel Lezcano per_cpu(cpuidle_devices, dev->cpu) = NULL; 358f6bb51a5SDaniel Lezcano module_put(drv->owner); 359f6bb51a5SDaniel Lezcano } 360f6bb51a5SDaniel Lezcano 3615df0aa73SDaniel Lezcano static int __cpuidle_device_init(struct cpuidle_device *dev) 3625df0aa73SDaniel Lezcano { 3635df0aa73SDaniel Lezcano memset(dev->states_usage, 0, sizeof(dev->states_usage)); 3645df0aa73SDaniel Lezcano dev->last_residency = 0; 3655df0aa73SDaniel Lezcano 3665df0aa73SDaniel Lezcano return 0; 3675df0aa73SDaniel Lezcano } 3685df0aa73SDaniel Lezcano 3694f86d3a8SLen Brown /** 370dcb84f33SVenkatesh Pallipadi * __cpuidle_register_device - internal register function called before register 371dcb84f33SVenkatesh Pallipadi * and enable routines 3724f86d3a8SLen Brown * @dev: the cpu 373dcb84f33SVenkatesh Pallipadi * 374dcb84f33SVenkatesh Pallipadi * cpuidle_lock mutex must be held before this is called 3754f86d3a8SLen Brown */ 376dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev) 3774f86d3a8SLen Brown { 3784f86d3a8SLen Brown int ret; 379bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 3804f86d3a8SLen Brown 381bf4d1b5dSDaniel Lezcano if (!try_module_get(drv->owner)) 3824f86d3a8SLen Brown return -EINVAL; 3834f86d3a8SLen Brown 3844f86d3a8SLen Brown per_cpu(cpuidle_devices, dev->cpu) = dev; 3854f86d3a8SLen Brown list_add(&dev->device_list, &cpuidle_detected_devices); 3864f86d3a8SLen Brown 3874126c019SColin Cross ret = cpuidle_coupled_register_device(dev); 388f6bb51a5SDaniel Lezcano if (ret) { 389f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 390f6bb51a5SDaniel Lezcano return ret; 391f6bb51a5SDaniel Lezcano } 3924f86d3a8SLen Brown 393dcb84f33SVenkatesh Pallipadi dev->registered = 1; 394dcb84f33SVenkatesh Pallipadi return 0; 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 { 403c878a52dSDaniel Lezcano int ret = -EBUSY; 404dcb84f33SVenkatesh Pallipadi 4051b0a0e9aSSrivatsa S. Bhat if (!dev) 4061b0a0e9aSSrivatsa S. Bhat return -EINVAL; 4071b0a0e9aSSrivatsa S. Bhat 408dcb84f33SVenkatesh Pallipadi mutex_lock(&cpuidle_lock); 409dcb84f33SVenkatesh Pallipadi 410c878a52dSDaniel Lezcano if (dev->registered) 411c878a52dSDaniel Lezcano goto out_unlock; 412c878a52dSDaniel Lezcano 4135df0aa73SDaniel Lezcano ret = __cpuidle_device_init(dev); 4145df0aa73SDaniel Lezcano if (ret) 4155df0aa73SDaniel Lezcano goto out_unlock; 4165df0aa73SDaniel Lezcano 417f6bb51a5SDaniel Lezcano ret = __cpuidle_register_device(dev); 418f6bb51a5SDaniel Lezcano if (ret) 419f6bb51a5SDaniel Lezcano goto out_unlock; 420f6bb51a5SDaniel Lezcano 421f6bb51a5SDaniel Lezcano ret = cpuidle_add_sysfs(dev); 422f6bb51a5SDaniel Lezcano if (ret) 423f6bb51a5SDaniel Lezcano goto out_unregister; 424dcb84f33SVenkatesh Pallipadi 42510b9d3f8SDaniel Lezcano ret = cpuidle_enable_device(dev); 426f6bb51a5SDaniel Lezcano if (ret) 427f6bb51a5SDaniel Lezcano goto out_sysfs; 42810b9d3f8SDaniel Lezcano 4294f86d3a8SLen Brown cpuidle_install_idle_handler(); 4304f86d3a8SLen Brown 431f6bb51a5SDaniel Lezcano out_unlock: 4324f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 4334f86d3a8SLen Brown 434f6bb51a5SDaniel Lezcano return ret; 435f6bb51a5SDaniel Lezcano 436f6bb51a5SDaniel Lezcano out_sysfs: 437f6bb51a5SDaniel Lezcano cpuidle_remove_sysfs(dev); 438f6bb51a5SDaniel Lezcano out_unregister: 439f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 440f6bb51a5SDaniel Lezcano goto out_unlock; 4414f86d3a8SLen Brown } 4424f86d3a8SLen Brown 4434f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device); 4444f86d3a8SLen Brown 4454f86d3a8SLen Brown /** 4464f86d3a8SLen Brown * cpuidle_unregister_device - unregisters a CPU's idle PM feature 4474f86d3a8SLen Brown * @dev: the cpu 4484f86d3a8SLen Brown */ 4494f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev) 4504f86d3a8SLen Brown { 451dcb84f33SVenkatesh Pallipadi if (dev->registered == 0) 452dcb84f33SVenkatesh Pallipadi return; 453dcb84f33SVenkatesh Pallipadi 4544f86d3a8SLen Brown cpuidle_pause_and_lock(); 4554f86d3a8SLen Brown 4564f86d3a8SLen Brown cpuidle_disable_device(dev); 4574f86d3a8SLen Brown 4581aef40e2SDaniel Lezcano cpuidle_remove_sysfs(dev); 459f6bb51a5SDaniel Lezcano 460f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 4614f86d3a8SLen Brown 4624126c019SColin Cross cpuidle_coupled_unregister_device(dev); 4634126c019SColin Cross 4644f86d3a8SLen Brown cpuidle_resume_and_unlock(); 4654f86d3a8SLen Brown } 4664f86d3a8SLen Brown 4674f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device); 4684f86d3a8SLen Brown 4691c192d04SDaniel Lezcano /** 4704c637b21SDaniel Lezcano * cpuidle_unregister: unregister a driver and the devices. This function 4714c637b21SDaniel Lezcano * can be used only if the driver has been previously registered through 4724c637b21SDaniel Lezcano * the cpuidle_register function. 4734c637b21SDaniel Lezcano * 4744c637b21SDaniel Lezcano * @drv: a valid pointer to a struct cpuidle_driver 4754c637b21SDaniel Lezcano */ 4764c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv) 4774c637b21SDaniel Lezcano { 4784c637b21SDaniel Lezcano int cpu; 4794c637b21SDaniel Lezcano struct cpuidle_device *device; 4804c637b21SDaniel Lezcano 48182467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 4824c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 4834c637b21SDaniel Lezcano cpuidle_unregister_device(device); 4844c637b21SDaniel Lezcano } 4854c637b21SDaniel Lezcano 4864c637b21SDaniel Lezcano cpuidle_unregister_driver(drv); 4874c637b21SDaniel Lezcano } 4884c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister); 4894c637b21SDaniel Lezcano 4904c637b21SDaniel Lezcano /** 4914c637b21SDaniel Lezcano * cpuidle_register: registers the driver and the cpu devices with the 4924c637b21SDaniel Lezcano * coupled_cpus passed as parameter. This function is used for all common 4934c637b21SDaniel Lezcano * initialization pattern there are in the arch specific drivers. The 4944c637b21SDaniel Lezcano * devices is globally defined in this file. 4954c637b21SDaniel Lezcano * 4964c637b21SDaniel Lezcano * @drv : a valid pointer to a struct cpuidle_driver 4974c637b21SDaniel Lezcano * @coupled_cpus: a cpumask for the coupled states 4984c637b21SDaniel Lezcano * 4994c637b21SDaniel Lezcano * Returns 0 on success, < 0 otherwise 5004c637b21SDaniel Lezcano */ 5014c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv, 5024c637b21SDaniel Lezcano const struct cpumask *const coupled_cpus) 5034c637b21SDaniel Lezcano { 5044c637b21SDaniel Lezcano int ret, cpu; 5054c637b21SDaniel Lezcano struct cpuidle_device *device; 5064c637b21SDaniel Lezcano 5074c637b21SDaniel Lezcano ret = cpuidle_register_driver(drv); 5084c637b21SDaniel Lezcano if (ret) { 5094c637b21SDaniel Lezcano pr_err("failed to register cpuidle driver\n"); 5104c637b21SDaniel Lezcano return ret; 5114c637b21SDaniel Lezcano } 5124c637b21SDaniel Lezcano 51382467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 5144c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 5154c637b21SDaniel Lezcano device->cpu = cpu; 5164c637b21SDaniel Lezcano 5174c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 5184c637b21SDaniel Lezcano /* 519*caf4a36eSViresh Kumar * On multiplatform for ARM, the coupled idle states could be 5204c637b21SDaniel Lezcano * enabled in the kernel even if the cpuidle driver does not 5214c637b21SDaniel Lezcano * use it. Note, coupled_cpus is a struct copy. 5224c637b21SDaniel Lezcano */ 5234c637b21SDaniel Lezcano if (coupled_cpus) 5244c637b21SDaniel Lezcano device->coupled_cpus = *coupled_cpus; 5254c637b21SDaniel Lezcano #endif 5264c637b21SDaniel Lezcano ret = cpuidle_register_device(device); 5274c637b21SDaniel Lezcano if (!ret) 5284c637b21SDaniel Lezcano continue; 5294c637b21SDaniel Lezcano 5304c637b21SDaniel Lezcano pr_err("Failed to register cpuidle device for cpu%d\n", cpu); 5314c637b21SDaniel Lezcano 5324c637b21SDaniel Lezcano cpuidle_unregister(drv); 5334c637b21SDaniel Lezcano break; 5344c637b21SDaniel Lezcano } 5354c637b21SDaniel Lezcano 5364c637b21SDaniel Lezcano return ret; 5374c637b21SDaniel Lezcano } 5384c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register); 5394c637b21SDaniel Lezcano 5404f86d3a8SLen Brown #ifdef CONFIG_SMP 5414f86d3a8SLen Brown 5424f86d3a8SLen Brown static void smp_callback(void *v) 5434f86d3a8SLen Brown { 5444f86d3a8SLen Brown /* we already woke the CPU up, nothing more to do */ 5454f86d3a8SLen Brown } 5464f86d3a8SLen Brown 5474f86d3a8SLen Brown /* 5484f86d3a8SLen Brown * This function gets called when a part of the kernel has a new latency 5494f86d3a8SLen Brown * requirement. This means we need to get all processors out of their C-state, 5504f86d3a8SLen Brown * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that 5514f86d3a8SLen Brown * wakes them all right up. 5524f86d3a8SLen Brown */ 5534f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b, 5544f86d3a8SLen Brown unsigned long l, void *v) 5554f86d3a8SLen Brown { 5568691e5a8SJens Axboe smp_call_function(smp_callback, NULL, 1); 5574f86d3a8SLen Brown return NOTIFY_OK; 5584f86d3a8SLen Brown } 5594f86d3a8SLen Brown 5604f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = { 5614f86d3a8SLen Brown .notifier_call = cpuidle_latency_notify, 5624f86d3a8SLen Brown }; 5634f86d3a8SLen Brown 564d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n) 565d82b3518SMark Gross { 566d82b3518SMark Gross pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); 567d82b3518SMark Gross } 5684f86d3a8SLen Brown 5694f86d3a8SLen Brown #else /* CONFIG_SMP */ 5704f86d3a8SLen Brown 5714f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0) 5724f86d3a8SLen Brown 5734f86d3a8SLen Brown #endif /* CONFIG_SMP */ 5744f86d3a8SLen Brown 5754f86d3a8SLen Brown /** 5764f86d3a8SLen Brown * cpuidle_init - core initializer 5774f86d3a8SLen Brown */ 5784f86d3a8SLen Brown static int __init cpuidle_init(void) 5794f86d3a8SLen Brown { 5804f86d3a8SLen Brown int ret; 5814f86d3a8SLen Brown 58262027aeaSLen Brown if (cpuidle_disabled()) 58362027aeaSLen Brown return -ENODEV; 58462027aeaSLen Brown 5858a25a2fdSKay Sievers ret = cpuidle_add_interface(cpu_subsys.dev_root); 5864f86d3a8SLen Brown if (ret) 5874f86d3a8SLen Brown return ret; 5884f86d3a8SLen Brown 5894f86d3a8SLen Brown latency_notifier_init(&cpuidle_latency_notifier); 5904f86d3a8SLen Brown 5914f86d3a8SLen Brown return 0; 5924f86d3a8SLen Brown } 5934f86d3a8SLen Brown 59462027aeaSLen Brown module_param(off, int, 0444); 5954f86d3a8SLen Brown core_initcall(cpuidle_init); 596