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 116ab4b199SKajetan Puchalski #include "linux/percpu-defs.h" 12b60e6a0eSDaniel Lezcano #include <linux/clockchips.h> 134f86d3a8SLen Brown #include <linux/kernel.h> 144f86d3a8SLen Brown #include <linux/mutex.h> 154f86d3a8SLen Brown #include <linux/sched.h> 16e6017571SIngo Molnar #include <linux/sched/clock.h> 17a01353cfSPeter Zijlstra #include <linux/sched/idle.h> 184f86d3a8SLen Brown #include <linux/notifier.h> 19e8db0be1SJean Pihet #include <linux/pm_qos.h> 204f86d3a8SLen Brown #include <linux/cpu.h> 214f86d3a8SLen Brown #include <linux/cpuidle.h> 229a0b8415Svenkatesh.pallipadi@intel.com #include <linux/ktime.h> 232e94d1f7SArjan van de Ven #include <linux/hrtimer.h> 24884b17e1SPaul Gortmaker #include <linux/module.h> 2538106313SRafael J. Wysocki #include <linux/suspend.h> 26124cf911SRafael J. Wysocki #include <linux/tick.h> 27bf9282dcSPeter Zijlstra #include <linux/mmu_context.h> 28e67198ccSFrederic Weisbecker #include <linux/context_tracking.h> 29288f023eSArjan van de Ven #include <trace/events/power.h> 304f86d3a8SLen Brown 314f86d3a8SLen Brown #include "cpuidle.h" 324f86d3a8SLen Brown 334f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); 344c637b21SDaniel Lezcano DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev); 354f86d3a8SLen Brown 364f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock); 374f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices); 384f86d3a8SLen Brown 394f86d3a8SLen Brown static int enabled_devices; 4062027aeaSLen Brown static int off __read_mostly; 41a0bfa137SLen Brown static int initialized __read_mostly; 4262027aeaSLen Brown 4362027aeaSLen Brown int cpuidle_disabled(void) 4462027aeaSLen Brown { 4562027aeaSLen Brown return off; 4662027aeaSLen Brown } 47d91ee586SLen Brown void disable_cpuidle(void) 48d91ee586SLen Brown { 49d91ee586SLen Brown off = 1; 50d91ee586SLen Brown } 514f86d3a8SLen Brown 52ef2b22acSRafael J. Wysocki bool cpuidle_not_available(struct cpuidle_driver *drv, 5331a34090SRafael J. Wysocki struct cpuidle_device *dev) 5431a34090SRafael J. Wysocki { 5531a34090SRafael J. Wysocki return off || !initialized || !drv || !dev || !dev->enabled; 5631a34090SRafael J. Wysocki } 5731a34090SRafael J. Wysocki 584f86d3a8SLen Brown /** 591a022e3fSBoris Ostrovsky * cpuidle_play_dead - cpu off-lining 601a022e3fSBoris Ostrovsky * 61ee01e663SToshi Kani * Returns in case of an error or no driver 621a022e3fSBoris Ostrovsky */ 631a022e3fSBoris Ostrovsky int cpuidle_play_dead(void) 641a022e3fSBoris Ostrovsky { 651a022e3fSBoris Ostrovsky struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); 66bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 678aef33a7SDaniel Lezcano int i; 681a022e3fSBoris Ostrovsky 69ee01e663SToshi Kani if (!drv) 70ee01e663SToshi Kani return -ENODEV; 71ee01e663SToshi Kani 721a022e3fSBoris Ostrovsky /* Find lowest-power state that supports long-term idle */ 737d51d979SRafael J. Wysocki for (i = drv->state_count - 1; i >= 0; i--) 748aef33a7SDaniel Lezcano if (drv->states[i].enter_dead) 758aef33a7SDaniel Lezcano return drv->states[i].enter_dead(dev, i); 761a022e3fSBoris Ostrovsky 771a022e3fSBoris Ostrovsky return -ENODEV; 781a022e3fSBoris Ostrovsky } 791a022e3fSBoris Ostrovsky 80ef2b22acSRafael J. Wysocki static int find_deepest_state(struct cpuidle_driver *drv, 810d94039fSRafael J. Wysocki struct cpuidle_device *dev, 82c1d51f68SRafael J. Wysocki u64 max_latency_ns, 830d94039fSRafael J. Wysocki unsigned int forbidden_flags, 8428ba086eSRafael J. Wysocki bool s2idle) 85a6220fc1SRafael J. Wysocki { 86c1d51f68SRafael J. Wysocki u64 latency_req = 0; 8751164251SRafael J. Wysocki int i, ret = 0; 88a6220fc1SRafael J. Wysocki 8951164251SRafael J. Wysocki for (i = 1; i < drv->state_count; i++) { 90a6220fc1SRafael J. Wysocki struct cpuidle_state *s = &drv->states[i]; 91a6220fc1SRafael J. Wysocki 9299e98d3fSRafael J. Wysocki if (dev->states_usage[i].disable || 93c1d51f68SRafael J. Wysocki s->exit_latency_ns <= latency_req || 94c1d51f68SRafael J. Wysocki s->exit_latency_ns > max_latency_ns || 9599e98d3fSRafael J. Wysocki (s->flags & forbidden_flags) || 9699e98d3fSRafael J. Wysocki (s2idle && !s->enter_s2idle)) 97a6220fc1SRafael J. Wysocki continue; 98a6220fc1SRafael J. Wysocki 99c1d51f68SRafael J. Wysocki latency_req = s->exit_latency_ns; 100a6220fc1SRafael J. Wysocki ret = i; 101a6220fc1SRafael J. Wysocki } 102a6220fc1SRafael J. Wysocki return ret; 103a6220fc1SRafael J. Wysocki } 104a6220fc1SRafael J. Wysocki 1050e7414b7SRafael J. Wysocki /** 106c55b51a0SDaniel Lezcano * cpuidle_use_deepest_state - Set/unset governor override mode. 107c55b51a0SDaniel Lezcano * @latency_limit_ns: Idle state exit latency limit (or no override if 0). 1080e7414b7SRafael J. Wysocki * 109c55b51a0SDaniel Lezcano * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle 110c55b51a0SDaniel Lezcano * state with exit latency within @latency_limit_ns (override governors going 111c55b51a0SDaniel Lezcano * forward), or do not override governors if it is zero. 1120e7414b7SRafael J. Wysocki */ 113c55b51a0SDaniel Lezcano void cpuidle_use_deepest_state(u64 latency_limit_ns) 114bb8313b6SJacob Pan { 115bb8313b6SJacob Pan struct cpuidle_device *dev; 116bb8313b6SJacob Pan 117bb8313b6SJacob Pan preempt_disable(); 118bb8313b6SJacob Pan dev = cpuidle_get_device(); 11941dc750eSLi, Fei if (dev) 120c55b51a0SDaniel Lezcano dev->forced_idle_latency_limit_ns = latency_limit_ns; 121bb8313b6SJacob Pan preempt_enable(); 122bb8313b6SJacob Pan } 123bb8313b6SJacob Pan 124ef2b22acSRafael J. Wysocki /** 125ef2b22acSRafael J. Wysocki * cpuidle_find_deepest_state - Find the deepest available idle state. 126ef2b22acSRafael J. Wysocki * @drv: cpuidle driver for the given CPU. 127ef2b22acSRafael J. Wysocki * @dev: cpuidle device for the given CPU. 128cefb9409SBenjamin Gaignard * @latency_limit_ns: Idle state exit latency limit 129cefb9409SBenjamin Gaignard * 130cefb9409SBenjamin Gaignard * Return: the index of the deepest available idle state. 131ef2b22acSRafael J. Wysocki */ 132ef2b22acSRafael J. Wysocki int cpuidle_find_deepest_state(struct cpuidle_driver *drv, 1335aa9ba63SDaniel Lezcano struct cpuidle_device *dev, 1345aa9ba63SDaniel Lezcano u64 latency_limit_ns) 135ef2b22acSRafael J. Wysocki { 1365aa9ba63SDaniel Lezcano return find_deepest_state(drv, dev, latency_limit_ns, 0, false); 137ef2b22acSRafael J. Wysocki } 138ef2b22acSRafael J. Wysocki 139bb8313b6SJacob Pan #ifdef CONFIG_SUSPEND 14017cc2b55SPeter Zijlstra static noinstr void enter_s2idle_proper(struct cpuidle_driver *drv, 141124cf911SRafael J. Wysocki struct cpuidle_device *dev, int index) 142124cf911SRafael J. Wysocki { 1438747f202SPeter Zijlstra struct cpuidle_state *target_state = &drv->states[index]; 14417cc2b55SPeter Zijlstra ktime_t time_start, time_end; 14517cc2b55SPeter Zijlstra 14617cc2b55SPeter Zijlstra instrumentation_begin(); 14764bdff69SRafael J. Wysocki 148e6a15fa9SPeter Zijlstra time_start = ns_to_ktime(local_clock_noinstr()); 14964bdff69SRafael J. Wysocki 1501098582aSPeter Zijlstra tick_freeze(); 151124cf911SRafael J. Wysocki /* 152124cf911SRafael J. Wysocki * The state used here cannot be a "coupled" one, because the "coupled" 153124cf911SRafael J. Wysocki * cpuidle mechanism enables interrupts and doing that with timekeeping 154124cf911SRafael J. Wysocki * suspended is generally unsafe. 155124cf911SRafael J. Wysocki */ 15663caae84SLucas Stach stop_critical_timings(); 15717cc2b55SPeter Zijlstra if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) { 158a01353cfSPeter Zijlstra ct_cpuidle_enter(); 15917cc2b55SPeter Zijlstra /* Annotate away the indirect call */ 16017cc2b55SPeter Zijlstra instrumentation_begin(); 16117cc2b55SPeter Zijlstra } 1628747f202SPeter Zijlstra target_state->enter_s2idle(dev, drv, index); 16349d9c593SPeter Zijlstra if (WARN_ON_ONCE(!irqs_disabled())) 164a01353cfSPeter Zijlstra raw_local_irq_disable(); 16517cc2b55SPeter Zijlstra if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) { 16617cc2b55SPeter Zijlstra instrumentation_end(); 167a01353cfSPeter Zijlstra ct_cpuidle_exit(); 16817cc2b55SPeter Zijlstra } 1691098582aSPeter Zijlstra tick_unfreeze(); 17063caae84SLucas Stach start_critical_timings(); 17164bdff69SRafael J. Wysocki 172e6a15fa9SPeter Zijlstra time_end = ns_to_ktime(local_clock_noinstr()); 17364bdff69SRafael J. Wysocki 17464bdff69SRafael J. Wysocki dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start); 17564bdff69SRafael J. Wysocki dev->states_usage[index].s2idle_usage++; 17617cc2b55SPeter Zijlstra instrumentation_end(); 177124cf911SRafael J. Wysocki } 178124cf911SRafael J. Wysocki 179a6220fc1SRafael J. Wysocki /** 18028ba086eSRafael J. Wysocki * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle. 181ef2b22acSRafael J. Wysocki * @drv: cpuidle driver for the given CPU. 182ef2b22acSRafael J. Wysocki * @dev: cpuidle device for the given CPU. 18338106313SRafael J. Wysocki * 18428ba086eSRafael J. Wysocki * If there are states with the ->enter_s2idle callback, find the deepest of 185ef2b22acSRafael J. Wysocki * them and enter it with frozen tick. 18638106313SRafael J. Wysocki */ 18728ba086eSRafael J. Wysocki int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev) 18838106313SRafael J. Wysocki { 18938106313SRafael J. Wysocki int index; 19038106313SRafael J. Wysocki 191124cf911SRafael J. Wysocki /* 19228ba086eSRafael J. Wysocki * Find the deepest state with ->enter_s2idle present, which guarantees 193124cf911SRafael J. Wysocki * that interrupts won't be enabled when it exits and allows the tick to 194124cf911SRafael J. Wysocki * be frozen safely. 195124cf911SRafael J. Wysocki */ 196c1d51f68SRafael J. Wysocki index = find_deepest_state(drv, dev, U64_MAX, 0, true); 19710e8b11eSRafael J. Wysocki if (index > 0) { 19828ba086eSRafael J. Wysocki enter_s2idle_proper(drv, dev, index); 19910e8b11eSRafael J. Wysocki local_irq_enable(); 20010e8b11eSRafael J. Wysocki } 201ef2b22acSRafael J. Wysocki return index; 20238106313SRafael J. Wysocki } 20387e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */ 20438106313SRafael J. Wysocki 20538106313SRafael J. Wysocki /** 20656cfbf74SColin Cross * cpuidle_enter_state - enter the state and update stats 20756cfbf74SColin Cross * @dev: cpuidle device for this cpu 20856cfbf74SColin Cross * @drv: cpuidle driver for this cpu 2097312280bSRafael J. Wysocki * @index: index into the states table in @drv of the state to enter 21056cfbf74SColin Cross */ 21117cc2b55SPeter Zijlstra noinstr int cpuidle_enter_state(struct cpuidle_device *dev, 21217cc2b55SPeter Zijlstra struct cpuidle_driver *drv, 213554c06baSDaniel Lezcano int index) 21456cfbf74SColin Cross { 21556cfbf74SColin Cross int entered_state; 21656cfbf74SColin Cross 217554c06baSDaniel Lezcano struct cpuidle_state *target_state = &drv->states[index]; 218df8d9eeaSRafael J. Wysocki bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP); 219dbd1b8eaSShreyas B. Prabhu ktime_t time_start, time_end; 220554c06baSDaniel Lezcano 22117cc2b55SPeter Zijlstra instrumentation_begin(); 22217cc2b55SPeter Zijlstra 223df8d9eeaSRafael J. Wysocki /* 224df8d9eeaSRafael J. Wysocki * Tell the time framework to switch to a broadcast timer because our 225df8d9eeaSRafael J. Wysocki * local timer will be shut down. If a local timer is used from another 226df8d9eeaSRafael J. Wysocki * CPU as a broadcast timer, this call may fail if it is not available. 227df8d9eeaSRafael J. Wysocki */ 228827a5aefSRafael J. Wysocki if (broadcast && tick_broadcast_enter()) { 229c1d51f68SRafael J. Wysocki index = find_deepest_state(drv, dev, target_state->exit_latency_ns, 2300d94039fSRafael J. Wysocki CPUIDLE_FLAG_TIMER_STOP, false); 2310d94039fSRafael J. Wysocki if (index < 0) { 232827a5aefSRafael J. Wysocki default_idle_call(); 233df8d9eeaSRafael J. Wysocki return -EBUSY; 234827a5aefSRafael J. Wysocki } 2350d94039fSRafael J. Wysocki target_state = &drv->states[index]; 236f187851bSNicholas Piggin broadcast = false; 2370d94039fSRafael J. Wysocki } 238df8d9eeaSRafael J. Wysocki 239bf9282dcSPeter Zijlstra if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED) 240*7dbbc8f5SYosry Ahmed leave_mm(); 241bf9282dcSPeter Zijlstra 242faad3849SRafael J. Wysocki /* Take note of the planned idle state. */ 243faad3849SRafael J. Wysocki sched_idle_set_state(target_state); 244faad3849SRafael J. Wysocki 2451098582aSPeter Zijlstra trace_cpu_idle(index, dev->cpu); 246e6a15fa9SPeter Zijlstra time_start = ns_to_ktime(local_clock_noinstr()); 247554c06baSDaniel Lezcano 24863caae84SLucas Stach stop_critical_timings(); 24917cc2b55SPeter Zijlstra if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) { 250a01353cfSPeter Zijlstra ct_cpuidle_enter(); 25117cc2b55SPeter Zijlstra /* Annotate away the indirect call */ 25217cc2b55SPeter Zijlstra instrumentation_begin(); 25317cc2b55SPeter Zijlstra } 254bb7b1125SPeter Zijlstra 2550e985e9dSPeter Zijlstra /* 2560e985e9dSPeter Zijlstra * NOTE!! 2570e985e9dSPeter Zijlstra * 2580e985e9dSPeter Zijlstra * For cpuidle_state::enter() methods that do *NOT* set 2590e985e9dSPeter Zijlstra * CPUIDLE_FLAG_RCU_IDLE RCU will be disabled here and these functions 2600e985e9dSPeter Zijlstra * must be marked either noinstr or __cpuidle. 2610e985e9dSPeter Zijlstra * 2620e985e9dSPeter Zijlstra * For cpuidle_state::enter() methods that *DO* set 2630e985e9dSPeter Zijlstra * CPUIDLE_FLAG_RCU_IDLE this isn't required, but they must mark the 2640e985e9dSPeter Zijlstra * function calling ct_cpuidle_enter() as noinstr/__cpuidle and all 2650e985e9dSPeter Zijlstra * functions called within the RCU-idle region. 2660e985e9dSPeter Zijlstra */ 267554c06baSDaniel Lezcano entered_state = target_state->enter(dev, drv, index); 26817cc2b55SPeter Zijlstra 269bb7b1125SPeter Zijlstra if (WARN_ONCE(!irqs_disabled(), "%ps leaked IRQ state", target_state->enter)) 270bb7b1125SPeter Zijlstra raw_local_irq_disable(); 271bb7b1125SPeter Zijlstra 27217cc2b55SPeter Zijlstra if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) { 27317cc2b55SPeter Zijlstra instrumentation_end(); 274a01353cfSPeter Zijlstra ct_cpuidle_exit(); 27517cc2b55SPeter Zijlstra } 27663caae84SLucas Stach start_critical_timings(); 277554c06baSDaniel Lezcano 278f9fccdb9SPeter Zijlstra sched_clock_idle_wakeup_event(); 279e6a15fa9SPeter Zijlstra time_end = ns_to_ktime(local_clock_noinstr()); 2801098582aSPeter Zijlstra trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu); 281554c06baSDaniel Lezcano 282faad3849SRafael J. Wysocki /* The cpu is no longer idle or about to enter idle. */ 283faad3849SRafael J. Wysocki sched_idle_set_state(NULL); 284faad3849SRafael J. Wysocki 285bb7b1125SPeter Zijlstra if (broadcast) 286df8d9eeaSRafael J. Wysocki tick_broadcast_exit(); 287df8d9eeaSRafael J. Wysocki 288e7387da5SDaniel Lezcano if (!cpuidle_state_is_coupled(drv, index)) 289554c06baSDaniel Lezcano local_irq_enable(); 290554c06baSDaniel Lezcano 2917037b43eSFieah Lim if (entered_state >= 0) { 292c1d51f68SRafael J. Wysocki s64 diff, delay = drv->states[entered_state].exit_latency_ns; 29304dab58aSRafael J. Wysocki int i; 29404dab58aSRafael J. Wysocki 2957037b43eSFieah Lim /* 2967037b43eSFieah Lim * Update cpuidle counters 2977037b43eSFieah Lim * This can be moved to within driver enter routine, 2987037b43eSFieah Lim * but that results in multiple copies of same code. 2997037b43eSFieah Lim */ 300c1d51f68SRafael J. Wysocki diff = ktime_sub(time_end, time_start); 301554c06baSDaniel Lezcano 302c1d51f68SRafael J. Wysocki dev->last_residency_ns = diff; 303c1d51f68SRafael J. Wysocki dev->states_usage[entered_state].time_ns += diff; 30456cfbf74SColin Cross dev->states_usage[entered_state].usage++; 30504dab58aSRafael J. Wysocki 306c1d51f68SRafael J. Wysocki if (diff < drv->states[entered_state].target_residency_ns) { 30704dab58aSRafael J. Wysocki for (i = entered_state - 1; i >= 0; i--) { 30899e98d3fSRafael J. Wysocki if (dev->states_usage[i].disable) 30904dab58aSRafael J. Wysocki continue; 31004dab58aSRafael J. Wysocki 31104dab58aSRafael J. Wysocki /* Shallower states are enabled, so update. */ 31204dab58aSRafael J. Wysocki dev->states_usage[entered_state].above++; 3136ab4b199SKajetan Puchalski trace_cpu_idle_miss(dev->cpu, entered_state, false); 31404dab58aSRafael J. Wysocki break; 31504dab58aSRafael J. Wysocki } 31604dab58aSRafael J. Wysocki } else if (diff > delay) { 31704dab58aSRafael J. Wysocki for (i = entered_state + 1; i < drv->state_count; i++) { 31899e98d3fSRafael J. Wysocki if (dev->states_usage[i].disable) 31904dab58aSRafael J. Wysocki continue; 32004dab58aSRafael J. Wysocki 32104dab58aSRafael J. Wysocki /* 32204dab58aSRafael J. Wysocki * Update if a deeper state would have been a 32304dab58aSRafael J. Wysocki * better match for the observed idle duration. 32404dab58aSRafael J. Wysocki */ 3256ab4b199SKajetan Puchalski if (diff - delay >= drv->states[i].target_residency_ns) { 32604dab58aSRafael J. Wysocki dev->states_usage[entered_state].below++; 3276ab4b199SKajetan Puchalski trace_cpu_idle_miss(dev->cpu, entered_state, true); 3286ab4b199SKajetan Puchalski } 32904dab58aSRafael J. Wysocki 33004dab58aSRafael J. Wysocki break; 33104dab58aSRafael J. Wysocki } 33204dab58aSRafael J. Wysocki } 33356cfbf74SColin Cross } else { 334c1d51f68SRafael J. Wysocki dev->last_residency_ns = 0; 335f49735f4SLina Iyer dev->states_usage[index].rejected++; 33656cfbf74SColin Cross } 33756cfbf74SColin Cross 33817cc2b55SPeter Zijlstra instrumentation_end(); 33917cc2b55SPeter Zijlstra 34056cfbf74SColin Cross return entered_state; 34156cfbf74SColin Cross } 34256cfbf74SColin Cross 34356cfbf74SColin Cross /** 344907e30f1SDaniel Lezcano * cpuidle_select - ask the cpuidle framework to choose an idle state 3454f86d3a8SLen Brown * 346907e30f1SDaniel Lezcano * @drv: the cpuidle driver 347907e30f1SDaniel Lezcano * @dev: the cpuidle device 34845f1ff59SRafael J. Wysocki * @stop_tick: indication on whether or not to stop the tick 349907e30f1SDaniel Lezcano * 35051164251SRafael J. Wysocki * Returns the index of the idle state. The return value must not be negative. 35145f1ff59SRafael J. Wysocki * 35245f1ff59SRafael J. Wysocki * The memory location pointed to by @stop_tick is expected to be written the 35345f1ff59SRafael J. Wysocki * 'false' boolean value if the scheduler tick should not be stopped before 35445f1ff59SRafael J. Wysocki * entering the returned state. 3554f86d3a8SLen Brown */ 35645f1ff59SRafael J. Wysocki int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, 35745f1ff59SRafael J. Wysocki bool *stop_tick) 3584f86d3a8SLen Brown { 35945f1ff59SRafael J. Wysocki return cpuidle_curr_governor->select(drv, dev, stop_tick); 360246eb7f0SKevin Hilman } 361246eb7f0SKevin Hilman 362907e30f1SDaniel Lezcano /** 363907e30f1SDaniel Lezcano * cpuidle_enter - enter into the specified idle state 364907e30f1SDaniel Lezcano * 365907e30f1SDaniel Lezcano * @drv: the cpuidle driver tied with the cpu 366907e30f1SDaniel Lezcano * @dev: the cpuidle device 367907e30f1SDaniel Lezcano * @index: the index in the idle state table 368907e30f1SDaniel Lezcano * 369907e30f1SDaniel Lezcano * Returns the index in the idle state, < 0 in case of error. 370907e30f1SDaniel Lezcano * The error code depends on the backend driver 371907e30f1SDaniel Lezcano */ 372907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev, 373907e30f1SDaniel Lezcano int index) 374907e30f1SDaniel Lezcano { 3756f9b83acSUlf Hansson int ret = 0; 3766f9b83acSUlf Hansson 3776f9b83acSUlf Hansson /* 3786f9b83acSUlf Hansson * Store the next hrtimer, which becomes either next tick or the next 3796f9b83acSUlf Hansson * timer event, whatever expires first. Additionally, to make this data 3806f9b83acSUlf Hansson * useful for consumers outside cpuidle, we rely on that the governor's 3816f9b83acSUlf Hansson * ->select() callback have decided, whether to stop the tick or not. 3826f9b83acSUlf Hansson */ 3836f9b83acSUlf Hansson WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer()); 3846f9b83acSUlf Hansson 3854c1ed5a6SXunlei Pang if (cpuidle_state_is_coupled(drv, index)) 3866f9b83acSUlf Hansson ret = cpuidle_enter_state_coupled(dev, drv, index); 3876f9b83acSUlf Hansson else 3886f9b83acSUlf Hansson ret = cpuidle_enter_state(dev, drv, index); 3896f9b83acSUlf Hansson 3906f9b83acSUlf Hansson WRITE_ONCE(dev->next_hrtimer, 0); 3916f9b83acSUlf Hansson return ret; 392907e30f1SDaniel Lezcano } 393fb11c9c6SViresh Kumar 394907e30f1SDaniel Lezcano /** 395907e30f1SDaniel Lezcano * cpuidle_reflect - tell the underlying governor what was the state 396907e30f1SDaniel Lezcano * we were in 397907e30f1SDaniel Lezcano * 398907e30f1SDaniel Lezcano * @dev : the cpuidle device 399907e30f1SDaniel Lezcano * @index: the index in the idle state table 400907e30f1SDaniel Lezcano * 401907e30f1SDaniel Lezcano */ 402907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index) 403907e30f1SDaniel Lezcano { 404a802ea96SRafael J. Wysocki if (cpuidle_curr_governor->reflect && index >= 0) 405907e30f1SDaniel Lezcano cpuidle_curr_governor->reflect(dev, index); 4064f86d3a8SLen Brown } 4074f86d3a8SLen Brown 4087a25759eSMel Gorman /* 4097a25759eSMel Gorman * Min polling interval of 10usec is a guess. It is assuming that 4107a25759eSMel Gorman * for most users, the time for a single ping-pong workload like 4117a25759eSMel Gorman * perf bench pipe would generally complete within 10usec but 4127a25759eSMel Gorman * this is hardware dependant. Actual time can be estimated with 4137a25759eSMel Gorman * 4147a25759eSMel Gorman * perf bench sched pipe -l 10000 4157a25759eSMel Gorman * 4167a25759eSMel Gorman * Run multiple times to avoid cpufreq effects. 4177a25759eSMel Gorman */ 4187a25759eSMel Gorman #define CPUIDLE_POLL_MIN 10000 4197a25759eSMel Gorman #define CPUIDLE_POLL_MAX (TICK_NSEC / 16) 4207a25759eSMel Gorman 4214f86d3a8SLen Brown /** 422259231a0SMarcelo Tosatti * cpuidle_poll_time - return amount of time to poll for, 423259231a0SMarcelo Tosatti * governors can override dev->poll_limit_ns if necessary 424259231a0SMarcelo Tosatti * 425259231a0SMarcelo Tosatti * @drv: the cpuidle driver tied with the cpu 426259231a0SMarcelo Tosatti * @dev: the cpuidle device 427259231a0SMarcelo Tosatti * 428259231a0SMarcelo Tosatti */ 4294d627628SPeter Zijlstra __cpuidle u64 cpuidle_poll_time(struct cpuidle_driver *drv, 430259231a0SMarcelo Tosatti struct cpuidle_device *dev) 431259231a0SMarcelo Tosatti { 432259231a0SMarcelo Tosatti int i; 433259231a0SMarcelo Tosatti u64 limit_ns; 434259231a0SMarcelo Tosatti 4357a25759eSMel Gorman BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX); 4367a25759eSMel Gorman 437259231a0SMarcelo Tosatti if (dev->poll_limit_ns) 438259231a0SMarcelo Tosatti return dev->poll_limit_ns; 439259231a0SMarcelo Tosatti 4407a25759eSMel Gorman limit_ns = CPUIDLE_POLL_MAX; 441259231a0SMarcelo Tosatti for (i = 1; i < drv->state_count; i++) { 4427a25759eSMel Gorman u64 state_limit; 4437a25759eSMel Gorman 44499e98d3fSRafael J. Wysocki if (dev->states_usage[i].disable) 445259231a0SMarcelo Tosatti continue; 446259231a0SMarcelo Tosatti 4477a25759eSMel Gorman state_limit = drv->states[i].target_residency_ns; 4487a25759eSMel Gorman if (state_limit < CPUIDLE_POLL_MIN) 4497a25759eSMel Gorman continue; 4507a25759eSMel Gorman 4517a25759eSMel Gorman limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX); 45236fcb429SMarcelo Tosatti break; 453259231a0SMarcelo Tosatti } 454259231a0SMarcelo Tosatti 455259231a0SMarcelo Tosatti dev->poll_limit_ns = limit_ns; 456259231a0SMarcelo Tosatti 457259231a0SMarcelo Tosatti return dev->poll_limit_ns; 458259231a0SMarcelo Tosatti } 459259231a0SMarcelo Tosatti 460259231a0SMarcelo Tosatti /** 4614f86d3a8SLen Brown * cpuidle_install_idle_handler - installs the cpuidle idle loop handler 4624f86d3a8SLen Brown */ 4634f86d3a8SLen Brown void cpuidle_install_idle_handler(void) 4644f86d3a8SLen Brown { 465a0bfa137SLen Brown if (enabled_devices) { 4664f86d3a8SLen Brown /* Make sure all changes finished before we switch to new idle */ 4674f86d3a8SLen Brown smp_wmb(); 468a0bfa137SLen Brown initialized = 1; 4694f86d3a8SLen Brown } 4704f86d3a8SLen Brown } 4714f86d3a8SLen Brown 4724f86d3a8SLen Brown /** 4734f86d3a8SLen Brown * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler 4744f86d3a8SLen Brown */ 4754f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void) 4764f86d3a8SLen Brown { 477a0bfa137SLen Brown if (enabled_devices) { 478a0bfa137SLen Brown initialized = 0; 4792ed903c5SChuansheng Liu wake_up_all_idle_cpus(); 4804f86d3a8SLen Brown } 481442bf3aaSDaniel Lezcano 482442bf3aaSDaniel Lezcano /* 483442bf3aaSDaniel Lezcano * Make sure external observers (such as the scheduler) 484442bf3aaSDaniel Lezcano * are done looking at pointed idle states. 485442bf3aaSDaniel Lezcano */ 486442bf3aaSDaniel Lezcano synchronize_rcu(); 4874f86d3a8SLen Brown } 4884f86d3a8SLen Brown 4894f86d3a8SLen Brown /** 4904f86d3a8SLen Brown * cpuidle_pause_and_lock - temporarily disables CPUIDLE 4914f86d3a8SLen Brown */ 4924f86d3a8SLen Brown void cpuidle_pause_and_lock(void) 4934f86d3a8SLen Brown { 4944f86d3a8SLen Brown mutex_lock(&cpuidle_lock); 4954f86d3a8SLen Brown cpuidle_uninstall_idle_handler(); 4964f86d3a8SLen Brown } 4974f86d3a8SLen Brown 4984f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); 4994f86d3a8SLen Brown 5004f86d3a8SLen Brown /** 5014f86d3a8SLen Brown * cpuidle_resume_and_unlock - resumes CPUIDLE operation 5024f86d3a8SLen Brown */ 5034f86d3a8SLen Brown void cpuidle_resume_and_unlock(void) 5044f86d3a8SLen Brown { 5054f86d3a8SLen Brown cpuidle_install_idle_handler(); 5064f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 5074f86d3a8SLen Brown } 5084f86d3a8SLen Brown 5094f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); 5104f86d3a8SLen Brown 5118651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */ 5128651f97bSPreeti U Murthy void cpuidle_pause(void) 5138651f97bSPreeti U Murthy { 5148651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 5158651f97bSPreeti U Murthy cpuidle_uninstall_idle_handler(); 5168651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 5178651f97bSPreeti U Murthy } 5188651f97bSPreeti U Murthy 5198651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */ 5208651f97bSPreeti U Murthy void cpuidle_resume(void) 5218651f97bSPreeti U Murthy { 5228651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 5238651f97bSPreeti U Murthy cpuidle_install_idle_handler(); 5248651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 5258651f97bSPreeti U Murthy } 5268651f97bSPreeti U Murthy 5274f86d3a8SLen Brown /** 5284f86d3a8SLen Brown * cpuidle_enable_device - enables idle PM for a CPU 5294f86d3a8SLen Brown * @dev: the CPU 5304f86d3a8SLen Brown * 5314f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 5324f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 5334f86d3a8SLen Brown */ 5344f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev) 5354f86d3a8SLen Brown { 5365df0aa73SDaniel Lezcano int ret; 537bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv; 5384f86d3a8SLen Brown 5391b0a0e9aSSrivatsa S. Bhat if (!dev) 5401b0a0e9aSSrivatsa S. Bhat return -EINVAL; 5411b0a0e9aSSrivatsa S. Bhat 5424f86d3a8SLen Brown if (dev->enabled) 5434f86d3a8SLen Brown return 0; 544bf4d1b5dSDaniel Lezcano 545e7b06a09SGaurav Jindal if (!cpuidle_curr_governor) 546e7b06a09SGaurav Jindal return -EIO; 547e7b06a09SGaurav Jindal 548bf4d1b5dSDaniel Lezcano drv = cpuidle_get_cpu_driver(dev); 549bf4d1b5dSDaniel Lezcano 550e7b06a09SGaurav Jindal if (!drv) 5514f86d3a8SLen Brown return -EIO; 552bf4d1b5dSDaniel Lezcano 55310b9d3f8SDaniel Lezcano if (!dev->registered) 55410b9d3f8SDaniel Lezcano return -EINVAL; 55510b9d3f8SDaniel Lezcano 556bf4d1b5dSDaniel Lezcano ret = cpuidle_add_device_sysfs(dev); 557bf4d1b5dSDaniel Lezcano if (ret) 5584f86d3a8SLen Brown return ret; 5594f86d3a8SLen Brown 5603fc74bd8SGaurav Jindal if (cpuidle_curr_governor->enable) { 5613fc74bd8SGaurav Jindal ret = cpuidle_curr_governor->enable(drv, dev); 5623fc74bd8SGaurav Jindal if (ret) 5634f86d3a8SLen Brown goto fail_sysfs; 5643fc74bd8SGaurav Jindal } 5654f86d3a8SLen Brown 5664f86d3a8SLen Brown smp_wmb(); 5674f86d3a8SLen Brown 5684f86d3a8SLen Brown dev->enabled = 1; 5694f86d3a8SLen Brown 5704f86d3a8SLen Brown enabled_devices++; 5714f86d3a8SLen Brown return 0; 5724f86d3a8SLen Brown 5734f86d3a8SLen Brown fail_sysfs: 574bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 5754f86d3a8SLen Brown 5764f86d3a8SLen Brown return ret; 5774f86d3a8SLen Brown } 5784f86d3a8SLen Brown 5794f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device); 5804f86d3a8SLen Brown 5814f86d3a8SLen Brown /** 5824f86d3a8SLen Brown * cpuidle_disable_device - disables idle PM for a CPU 5834f86d3a8SLen Brown * @dev: the CPU 5844f86d3a8SLen Brown * 5854f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 5864f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 5874f86d3a8SLen Brown */ 5884f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev) 5894f86d3a8SLen Brown { 590bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 591bf4d1b5dSDaniel Lezcano 592cf31cd1aSSrivatsa S. Bhat if (!dev || !dev->enabled) 5934f86d3a8SLen Brown return; 594bf4d1b5dSDaniel Lezcano 595bf4d1b5dSDaniel Lezcano if (!drv || !cpuidle_curr_governor) 5964f86d3a8SLen Brown return; 5974f86d3a8SLen Brown 5984f86d3a8SLen Brown dev->enabled = 0; 5994f86d3a8SLen Brown 6004f86d3a8SLen Brown if (cpuidle_curr_governor->disable) 601bf4d1b5dSDaniel Lezcano cpuidle_curr_governor->disable(drv, dev); 6024f86d3a8SLen Brown 603bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 6044f86d3a8SLen Brown enabled_devices--; 6054f86d3a8SLen Brown } 6064f86d3a8SLen Brown 6074f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device); 6084f86d3a8SLen Brown 609f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev) 610f6bb51a5SDaniel Lezcano { 611f6bb51a5SDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 612f6bb51a5SDaniel Lezcano 613f6bb51a5SDaniel Lezcano list_del(&dev->device_list); 614f6bb51a5SDaniel Lezcano per_cpu(cpuidle_devices, dev->cpu) = NULL; 615f6bb51a5SDaniel Lezcano module_put(drv->owner); 616c998c078SDave Gerlach 617c998c078SDave Gerlach dev->registered = 0; 618f6bb51a5SDaniel Lezcano } 619f6bb51a5SDaniel Lezcano 620267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev) 6215df0aa73SDaniel Lezcano { 6225df0aa73SDaniel Lezcano memset(dev->states_usage, 0, sizeof(dev->states_usage)); 623c1d51f68SRafael J. Wysocki dev->last_residency_ns = 0; 6246f9b83acSUlf Hansson dev->next_hrtimer = 0; 6255df0aa73SDaniel Lezcano } 6265df0aa73SDaniel Lezcano 6274f86d3a8SLen Brown /** 628dcb84f33SVenkatesh Pallipadi * __cpuidle_register_device - internal register function called before register 629dcb84f33SVenkatesh Pallipadi * and enable routines 6304f86d3a8SLen Brown * @dev: the cpu 631dcb84f33SVenkatesh Pallipadi * 632dcb84f33SVenkatesh Pallipadi * cpuidle_lock mutex must be held before this is called 6334f86d3a8SLen Brown */ 634dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev) 6354f86d3a8SLen Brown { 636bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 63799e98d3fSRafael J. Wysocki int i, ret; 6384f86d3a8SLen Brown 639bf4d1b5dSDaniel Lezcano if (!try_module_get(drv->owner)) 6404f86d3a8SLen Brown return -EINVAL; 6414f86d3a8SLen Brown 64275a80267SRafael J. Wysocki for (i = 0; i < drv->state_count; i++) { 643ba1e78a1SRafael J. Wysocki if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE) 64499e98d3fSRafael J. Wysocki dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER; 64599e98d3fSRafael J. Wysocki 64675a80267SRafael J. Wysocki if (drv->states[i].flags & CPUIDLE_FLAG_OFF) 64775a80267SRafael J. Wysocki dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER; 64875a80267SRafael J. Wysocki } 64975a80267SRafael J. Wysocki 6504f86d3a8SLen Brown per_cpu(cpuidle_devices, dev->cpu) = dev; 6514f86d3a8SLen Brown list_add(&dev->device_list, &cpuidle_detected_devices); 6524f86d3a8SLen Brown 6534126c019SColin Cross ret = cpuidle_coupled_register_device(dev); 65447182668SViresh Kumar if (ret) 655f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 65647182668SViresh Kumar else 657dcb84f33SVenkatesh Pallipadi dev->registered = 1; 65847182668SViresh Kumar 65947182668SViresh Kumar return ret; 660dcb84f33SVenkatesh Pallipadi } 661dcb84f33SVenkatesh Pallipadi 662dcb84f33SVenkatesh Pallipadi /** 663dcb84f33SVenkatesh Pallipadi * cpuidle_register_device - registers a CPU's idle PM feature 664dcb84f33SVenkatesh Pallipadi * @dev: the cpu 665dcb84f33SVenkatesh Pallipadi */ 666dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev) 667dcb84f33SVenkatesh Pallipadi { 668c878a52dSDaniel Lezcano int ret = -EBUSY; 669dcb84f33SVenkatesh Pallipadi 6701b0a0e9aSSrivatsa S. Bhat if (!dev) 6711b0a0e9aSSrivatsa S. Bhat return -EINVAL; 6721b0a0e9aSSrivatsa S. Bhat 673dcb84f33SVenkatesh Pallipadi mutex_lock(&cpuidle_lock); 674dcb84f33SVenkatesh Pallipadi 675c878a52dSDaniel Lezcano if (dev->registered) 676c878a52dSDaniel Lezcano goto out_unlock; 677c878a52dSDaniel Lezcano 678267d4bf8SViresh Kumar __cpuidle_device_init(dev); 6795df0aa73SDaniel Lezcano 680f6bb51a5SDaniel Lezcano ret = __cpuidle_register_device(dev); 681f6bb51a5SDaniel Lezcano if (ret) 682f6bb51a5SDaniel Lezcano goto out_unlock; 683f6bb51a5SDaniel Lezcano 684f6bb51a5SDaniel Lezcano ret = cpuidle_add_sysfs(dev); 685f6bb51a5SDaniel Lezcano if (ret) 686f6bb51a5SDaniel Lezcano goto out_unregister; 687dcb84f33SVenkatesh Pallipadi 68810b9d3f8SDaniel Lezcano ret = cpuidle_enable_device(dev); 689f6bb51a5SDaniel Lezcano if (ret) 690f6bb51a5SDaniel Lezcano goto out_sysfs; 69110b9d3f8SDaniel Lezcano 6924f86d3a8SLen Brown cpuidle_install_idle_handler(); 6934f86d3a8SLen Brown 694f6bb51a5SDaniel Lezcano out_unlock: 6954f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 6964f86d3a8SLen Brown 697f6bb51a5SDaniel Lezcano return ret; 698f6bb51a5SDaniel Lezcano 699f6bb51a5SDaniel Lezcano out_sysfs: 700f6bb51a5SDaniel Lezcano cpuidle_remove_sysfs(dev); 701f6bb51a5SDaniel Lezcano out_unregister: 702f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 703f6bb51a5SDaniel Lezcano goto out_unlock; 7044f86d3a8SLen Brown } 7054f86d3a8SLen Brown 7064f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device); 7074f86d3a8SLen Brown 7084f86d3a8SLen Brown /** 7094f86d3a8SLen Brown * cpuidle_unregister_device - unregisters a CPU's idle PM feature 7104f86d3a8SLen Brown * @dev: the cpu 7114f86d3a8SLen Brown */ 7124f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev) 7134f86d3a8SLen Brown { 714813e8e3dSKonrad Rzeszutek Wilk if (!dev || dev->registered == 0) 715dcb84f33SVenkatesh Pallipadi return; 716dcb84f33SVenkatesh Pallipadi 7174f86d3a8SLen Brown cpuidle_pause_and_lock(); 7184f86d3a8SLen Brown 7194f86d3a8SLen Brown cpuidle_disable_device(dev); 7204f86d3a8SLen Brown 7211aef40e2SDaniel Lezcano cpuidle_remove_sysfs(dev); 722f6bb51a5SDaniel Lezcano 723f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 7244f86d3a8SLen Brown 7254126c019SColin Cross cpuidle_coupled_unregister_device(dev); 7264126c019SColin Cross 7274f86d3a8SLen Brown cpuidle_resume_and_unlock(); 7284f86d3a8SLen Brown } 7294f86d3a8SLen Brown 7304f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device); 7314f86d3a8SLen Brown 7321c192d04SDaniel Lezcano /** 7334c637b21SDaniel Lezcano * cpuidle_unregister: unregister a driver and the devices. This function 7344c637b21SDaniel Lezcano * can be used only if the driver has been previously registered through 7354c637b21SDaniel Lezcano * the cpuidle_register function. 7364c637b21SDaniel Lezcano * 7374c637b21SDaniel Lezcano * @drv: a valid pointer to a struct cpuidle_driver 7384c637b21SDaniel Lezcano */ 7394c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv) 7404c637b21SDaniel Lezcano { 7414c637b21SDaniel Lezcano int cpu; 7424c637b21SDaniel Lezcano struct cpuidle_device *device; 7434c637b21SDaniel Lezcano 74482467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 7454c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 7464c637b21SDaniel Lezcano cpuidle_unregister_device(device); 7474c637b21SDaniel Lezcano } 7484c637b21SDaniel Lezcano 7494c637b21SDaniel Lezcano cpuidle_unregister_driver(drv); 7504c637b21SDaniel Lezcano } 7514c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister); 7524c637b21SDaniel Lezcano 7534c637b21SDaniel Lezcano /** 7544c637b21SDaniel Lezcano * cpuidle_register: registers the driver and the cpu devices with the 7554c637b21SDaniel Lezcano * coupled_cpus passed as parameter. This function is used for all common 7564c637b21SDaniel Lezcano * initialization pattern there are in the arch specific drivers. The 7574c637b21SDaniel Lezcano * devices is globally defined in this file. 7584c637b21SDaniel Lezcano * 7594c637b21SDaniel Lezcano * @drv : a valid pointer to a struct cpuidle_driver 7604c637b21SDaniel Lezcano * @coupled_cpus: a cpumask for the coupled states 7614c637b21SDaniel Lezcano * 7624c637b21SDaniel Lezcano * Returns 0 on success, < 0 otherwise 7634c637b21SDaniel Lezcano */ 7644c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv, 7654c637b21SDaniel Lezcano const struct cpumask *const coupled_cpus) 7664c637b21SDaniel Lezcano { 7674c637b21SDaniel Lezcano int ret, cpu; 7684c637b21SDaniel Lezcano struct cpuidle_device *device; 7694c637b21SDaniel Lezcano 7704c637b21SDaniel Lezcano ret = cpuidle_register_driver(drv); 7714c637b21SDaniel Lezcano if (ret) { 7724c637b21SDaniel Lezcano pr_err("failed to register cpuidle driver\n"); 7734c637b21SDaniel Lezcano return ret; 7744c637b21SDaniel Lezcano } 7754c637b21SDaniel Lezcano 77682467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 7774c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 7784c637b21SDaniel Lezcano device->cpu = cpu; 7794c637b21SDaniel Lezcano 7804c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 7814c637b21SDaniel Lezcano /* 782caf4a36eSViresh Kumar * On multiplatform for ARM, the coupled idle states could be 7834c637b21SDaniel Lezcano * enabled in the kernel even if the cpuidle driver does not 7844c637b21SDaniel Lezcano * use it. Note, coupled_cpus is a struct copy. 7854c637b21SDaniel Lezcano */ 7864c637b21SDaniel Lezcano if (coupled_cpus) 7874c637b21SDaniel Lezcano device->coupled_cpus = *coupled_cpus; 7884c637b21SDaniel Lezcano #endif 7894c637b21SDaniel Lezcano ret = cpuidle_register_device(device); 7904c637b21SDaniel Lezcano if (!ret) 7914c637b21SDaniel Lezcano continue; 7924c637b21SDaniel Lezcano 7934c637b21SDaniel Lezcano pr_err("Failed to register cpuidle device for cpu%d\n", cpu); 7944c637b21SDaniel Lezcano 7954c637b21SDaniel Lezcano cpuidle_unregister(drv); 7964c637b21SDaniel Lezcano break; 7974c637b21SDaniel Lezcano } 7984c637b21SDaniel Lezcano 7994c637b21SDaniel Lezcano return ret; 8004c637b21SDaniel Lezcano } 8014c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register); 8024c637b21SDaniel Lezcano 8034f86d3a8SLen Brown /** 8044f86d3a8SLen Brown * cpuidle_init - core initializer 8054f86d3a8SLen Brown */ 8064f86d3a8SLen Brown static int __init cpuidle_init(void) 8074f86d3a8SLen Brown { 80862027aeaSLen Brown if (cpuidle_disabled()) 80962027aeaSLen Brown return -ENODEV; 81062027aeaSLen Brown 811bf6479dbSGreg Kroah-Hartman return cpuidle_add_interface(); 8124f86d3a8SLen Brown } 8134f86d3a8SLen Brown 81462027aeaSLen Brown module_param(off, int, 0444); 81561cb5758SRafael J. Wysocki module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444); 8164f86d3a8SLen Brown core_initcall(cpuidle_init); 817