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 140*17cc2b55SPeter 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]; 144*17cc2b55SPeter Zijlstra ktime_t time_start, time_end; 145*17cc2b55SPeter Zijlstra 146*17cc2b55SPeter Zijlstra instrumentation_begin(); 14764bdff69SRafael J. Wysocki 14864bdff69SRafael J. Wysocki time_start = ns_to_ktime(local_clock()); 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(); 157*17cc2b55SPeter Zijlstra if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) { 158a01353cfSPeter Zijlstra ct_cpuidle_enter(); 159*17cc2b55SPeter Zijlstra /* Annotate away the indirect call */ 160*17cc2b55SPeter Zijlstra instrumentation_begin(); 161*17cc2b55SPeter Zijlstra } 1628747f202SPeter Zijlstra target_state->enter_s2idle(dev, drv, index); 16349d9c593SPeter Zijlstra if (WARN_ON_ONCE(!irqs_disabled())) 164a01353cfSPeter Zijlstra raw_local_irq_disable(); 165*17cc2b55SPeter Zijlstra if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) { 166*17cc2b55SPeter Zijlstra instrumentation_end(); 167a01353cfSPeter Zijlstra ct_cpuidle_exit(); 168*17cc2b55SPeter Zijlstra } 1691098582aSPeter Zijlstra tick_unfreeze(); 17063caae84SLucas Stach start_critical_timings(); 17164bdff69SRafael J. Wysocki 17264bdff69SRafael J. Wysocki time_end = ns_to_ktime(local_clock()); 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++; 176*17cc2b55SPeter 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 */ 211*17cc2b55SPeter Zijlstra noinstr int cpuidle_enter_state(struct cpuidle_device *dev, 212*17cc2b55SPeter 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 221*17cc2b55SPeter Zijlstra instrumentation_begin(); 222*17cc2b55SPeter 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) 240bf9282dcSPeter Zijlstra leave_mm(dev->cpu); 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); 246dbd1b8eaSShreyas B. Prabhu time_start = ns_to_ktime(local_clock()); 247554c06baSDaniel Lezcano 24863caae84SLucas Stach stop_critical_timings(); 249*17cc2b55SPeter Zijlstra if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) { 250a01353cfSPeter Zijlstra ct_cpuidle_enter(); 251*17cc2b55SPeter Zijlstra /* Annotate away the indirect call */ 252*17cc2b55SPeter Zijlstra instrumentation_begin(); 253*17cc2b55SPeter Zijlstra } 254bb7b1125SPeter Zijlstra 255554c06baSDaniel Lezcano entered_state = target_state->enter(dev, drv, index); 256*17cc2b55SPeter Zijlstra 257bb7b1125SPeter Zijlstra if (WARN_ONCE(!irqs_disabled(), "%ps leaked IRQ state", target_state->enter)) 258bb7b1125SPeter Zijlstra raw_local_irq_disable(); 259bb7b1125SPeter Zijlstra 260*17cc2b55SPeter Zijlstra if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) { 261*17cc2b55SPeter Zijlstra instrumentation_end(); 262a01353cfSPeter Zijlstra ct_cpuidle_exit(); 263*17cc2b55SPeter Zijlstra } 26463caae84SLucas Stach start_critical_timings(); 265554c06baSDaniel Lezcano 266f9fccdb9SPeter Zijlstra sched_clock_idle_wakeup_event(); 267dbd1b8eaSShreyas B. Prabhu time_end = ns_to_ktime(local_clock()); 2681098582aSPeter Zijlstra trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu); 269554c06baSDaniel Lezcano 270faad3849SRafael J. Wysocki /* The cpu is no longer idle or about to enter idle. */ 271faad3849SRafael J. Wysocki sched_idle_set_state(NULL); 272faad3849SRafael J. Wysocki 273bb7b1125SPeter Zijlstra if (broadcast) 274df8d9eeaSRafael J. Wysocki tick_broadcast_exit(); 275df8d9eeaSRafael J. Wysocki 276e7387da5SDaniel Lezcano if (!cpuidle_state_is_coupled(drv, index)) 277554c06baSDaniel Lezcano local_irq_enable(); 278554c06baSDaniel Lezcano 2797037b43eSFieah Lim if (entered_state >= 0) { 280c1d51f68SRafael J. Wysocki s64 diff, delay = drv->states[entered_state].exit_latency_ns; 28104dab58aSRafael J. Wysocki int i; 28204dab58aSRafael J. Wysocki 2837037b43eSFieah Lim /* 2847037b43eSFieah Lim * Update cpuidle counters 2857037b43eSFieah Lim * This can be moved to within driver enter routine, 2867037b43eSFieah Lim * but that results in multiple copies of same code. 2877037b43eSFieah Lim */ 288c1d51f68SRafael J. Wysocki diff = ktime_sub(time_end, time_start); 289554c06baSDaniel Lezcano 290c1d51f68SRafael J. Wysocki dev->last_residency_ns = diff; 291c1d51f68SRafael J. Wysocki dev->states_usage[entered_state].time_ns += diff; 29256cfbf74SColin Cross dev->states_usage[entered_state].usage++; 29304dab58aSRafael J. Wysocki 294c1d51f68SRafael J. Wysocki if (diff < drv->states[entered_state].target_residency_ns) { 29504dab58aSRafael J. Wysocki for (i = entered_state - 1; i >= 0; i--) { 29699e98d3fSRafael J. Wysocki if (dev->states_usage[i].disable) 29704dab58aSRafael J. Wysocki continue; 29804dab58aSRafael J. Wysocki 29904dab58aSRafael J. Wysocki /* Shallower states are enabled, so update. */ 30004dab58aSRafael J. Wysocki dev->states_usage[entered_state].above++; 3016ab4b199SKajetan Puchalski trace_cpu_idle_miss(dev->cpu, entered_state, false); 30204dab58aSRafael J. Wysocki break; 30304dab58aSRafael J. Wysocki } 30404dab58aSRafael J. Wysocki } else if (diff > delay) { 30504dab58aSRafael J. Wysocki for (i = entered_state + 1; i < drv->state_count; i++) { 30699e98d3fSRafael J. Wysocki if (dev->states_usage[i].disable) 30704dab58aSRafael J. Wysocki continue; 30804dab58aSRafael J. Wysocki 30904dab58aSRafael J. Wysocki /* 31004dab58aSRafael J. Wysocki * Update if a deeper state would have been a 31104dab58aSRafael J. Wysocki * better match for the observed idle duration. 31204dab58aSRafael J. Wysocki */ 3136ab4b199SKajetan Puchalski if (diff - delay >= drv->states[i].target_residency_ns) { 31404dab58aSRafael J. Wysocki dev->states_usage[entered_state].below++; 3156ab4b199SKajetan Puchalski trace_cpu_idle_miss(dev->cpu, entered_state, true); 3166ab4b199SKajetan Puchalski } 31704dab58aSRafael J. Wysocki 31804dab58aSRafael J. Wysocki break; 31904dab58aSRafael J. Wysocki } 32004dab58aSRafael J. Wysocki } 32156cfbf74SColin Cross } else { 322c1d51f68SRafael J. Wysocki dev->last_residency_ns = 0; 323f49735f4SLina Iyer dev->states_usage[index].rejected++; 32456cfbf74SColin Cross } 32556cfbf74SColin Cross 326*17cc2b55SPeter Zijlstra instrumentation_end(); 327*17cc2b55SPeter Zijlstra 32856cfbf74SColin Cross return entered_state; 32956cfbf74SColin Cross } 33056cfbf74SColin Cross 33156cfbf74SColin Cross /** 332907e30f1SDaniel Lezcano * cpuidle_select - ask the cpuidle framework to choose an idle state 3334f86d3a8SLen Brown * 334907e30f1SDaniel Lezcano * @drv: the cpuidle driver 335907e30f1SDaniel Lezcano * @dev: the cpuidle device 33645f1ff59SRafael J. Wysocki * @stop_tick: indication on whether or not to stop the tick 337907e30f1SDaniel Lezcano * 33851164251SRafael J. Wysocki * Returns the index of the idle state. The return value must not be negative. 33945f1ff59SRafael J. Wysocki * 34045f1ff59SRafael J. Wysocki * The memory location pointed to by @stop_tick is expected to be written the 34145f1ff59SRafael J. Wysocki * 'false' boolean value if the scheduler tick should not be stopped before 34245f1ff59SRafael J. Wysocki * entering the returned state. 3434f86d3a8SLen Brown */ 34445f1ff59SRafael J. Wysocki int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, 34545f1ff59SRafael J. Wysocki bool *stop_tick) 3464f86d3a8SLen Brown { 34745f1ff59SRafael J. Wysocki return cpuidle_curr_governor->select(drv, dev, stop_tick); 348246eb7f0SKevin Hilman } 349246eb7f0SKevin Hilman 350907e30f1SDaniel Lezcano /** 351907e30f1SDaniel Lezcano * cpuidle_enter - enter into the specified idle state 352907e30f1SDaniel Lezcano * 353907e30f1SDaniel Lezcano * @drv: the cpuidle driver tied with the cpu 354907e30f1SDaniel Lezcano * @dev: the cpuidle device 355907e30f1SDaniel Lezcano * @index: the index in the idle state table 356907e30f1SDaniel Lezcano * 357907e30f1SDaniel Lezcano * Returns the index in the idle state, < 0 in case of error. 358907e30f1SDaniel Lezcano * The error code depends on the backend driver 359907e30f1SDaniel Lezcano */ 360907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev, 361907e30f1SDaniel Lezcano int index) 362907e30f1SDaniel Lezcano { 3636f9b83acSUlf Hansson int ret = 0; 3646f9b83acSUlf Hansson 3656f9b83acSUlf Hansson /* 3666f9b83acSUlf Hansson * Store the next hrtimer, which becomes either next tick or the next 3676f9b83acSUlf Hansson * timer event, whatever expires first. Additionally, to make this data 3686f9b83acSUlf Hansson * useful for consumers outside cpuidle, we rely on that the governor's 3696f9b83acSUlf Hansson * ->select() callback have decided, whether to stop the tick or not. 3706f9b83acSUlf Hansson */ 3716f9b83acSUlf Hansson WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer()); 3726f9b83acSUlf Hansson 3734c1ed5a6SXunlei Pang if (cpuidle_state_is_coupled(drv, index)) 3746f9b83acSUlf Hansson ret = cpuidle_enter_state_coupled(dev, drv, index); 3756f9b83acSUlf Hansson else 3766f9b83acSUlf Hansson ret = cpuidle_enter_state(dev, drv, index); 3776f9b83acSUlf Hansson 3786f9b83acSUlf Hansson WRITE_ONCE(dev->next_hrtimer, 0); 3796f9b83acSUlf Hansson return ret; 380907e30f1SDaniel Lezcano } 381fb11c9c6SViresh Kumar 382907e30f1SDaniel Lezcano /** 383907e30f1SDaniel Lezcano * cpuidle_reflect - tell the underlying governor what was the state 384907e30f1SDaniel Lezcano * we were in 385907e30f1SDaniel Lezcano * 386907e30f1SDaniel Lezcano * @dev : the cpuidle device 387907e30f1SDaniel Lezcano * @index: the index in the idle state table 388907e30f1SDaniel Lezcano * 389907e30f1SDaniel Lezcano */ 390907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index) 391907e30f1SDaniel Lezcano { 392a802ea96SRafael J. Wysocki if (cpuidle_curr_governor->reflect && index >= 0) 393907e30f1SDaniel Lezcano cpuidle_curr_governor->reflect(dev, index); 3944f86d3a8SLen Brown } 3954f86d3a8SLen Brown 3967a25759eSMel Gorman /* 3977a25759eSMel Gorman * Min polling interval of 10usec is a guess. It is assuming that 3987a25759eSMel Gorman * for most users, the time for a single ping-pong workload like 3997a25759eSMel Gorman * perf bench pipe would generally complete within 10usec but 4007a25759eSMel Gorman * this is hardware dependant. Actual time can be estimated with 4017a25759eSMel Gorman * 4027a25759eSMel Gorman * perf bench sched pipe -l 10000 4037a25759eSMel Gorman * 4047a25759eSMel Gorman * Run multiple times to avoid cpufreq effects. 4057a25759eSMel Gorman */ 4067a25759eSMel Gorman #define CPUIDLE_POLL_MIN 10000 4077a25759eSMel Gorman #define CPUIDLE_POLL_MAX (TICK_NSEC / 16) 4087a25759eSMel Gorman 4094f86d3a8SLen Brown /** 410259231a0SMarcelo Tosatti * cpuidle_poll_time - return amount of time to poll for, 411259231a0SMarcelo Tosatti * governors can override dev->poll_limit_ns if necessary 412259231a0SMarcelo Tosatti * 413259231a0SMarcelo Tosatti * @drv: the cpuidle driver tied with the cpu 414259231a0SMarcelo Tosatti * @dev: the cpuidle device 415259231a0SMarcelo Tosatti * 416259231a0SMarcelo Tosatti */ 417259231a0SMarcelo Tosatti u64 cpuidle_poll_time(struct cpuidle_driver *drv, 418259231a0SMarcelo Tosatti struct cpuidle_device *dev) 419259231a0SMarcelo Tosatti { 420259231a0SMarcelo Tosatti int i; 421259231a0SMarcelo Tosatti u64 limit_ns; 422259231a0SMarcelo Tosatti 4237a25759eSMel Gorman BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX); 4247a25759eSMel Gorman 425259231a0SMarcelo Tosatti if (dev->poll_limit_ns) 426259231a0SMarcelo Tosatti return dev->poll_limit_ns; 427259231a0SMarcelo Tosatti 4287a25759eSMel Gorman limit_ns = CPUIDLE_POLL_MAX; 429259231a0SMarcelo Tosatti for (i = 1; i < drv->state_count; i++) { 4307a25759eSMel Gorman u64 state_limit; 4317a25759eSMel Gorman 43299e98d3fSRafael J. Wysocki if (dev->states_usage[i].disable) 433259231a0SMarcelo Tosatti continue; 434259231a0SMarcelo Tosatti 4357a25759eSMel Gorman state_limit = drv->states[i].target_residency_ns; 4367a25759eSMel Gorman if (state_limit < CPUIDLE_POLL_MIN) 4377a25759eSMel Gorman continue; 4387a25759eSMel Gorman 4397a25759eSMel Gorman limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX); 44036fcb429SMarcelo Tosatti break; 441259231a0SMarcelo Tosatti } 442259231a0SMarcelo Tosatti 443259231a0SMarcelo Tosatti dev->poll_limit_ns = limit_ns; 444259231a0SMarcelo Tosatti 445259231a0SMarcelo Tosatti return dev->poll_limit_ns; 446259231a0SMarcelo Tosatti } 447259231a0SMarcelo Tosatti 448259231a0SMarcelo Tosatti /** 4494f86d3a8SLen Brown * cpuidle_install_idle_handler - installs the cpuidle idle loop handler 4504f86d3a8SLen Brown */ 4514f86d3a8SLen Brown void cpuidle_install_idle_handler(void) 4524f86d3a8SLen Brown { 453a0bfa137SLen Brown if (enabled_devices) { 4544f86d3a8SLen Brown /* Make sure all changes finished before we switch to new idle */ 4554f86d3a8SLen Brown smp_wmb(); 456a0bfa137SLen Brown initialized = 1; 4574f86d3a8SLen Brown } 4584f86d3a8SLen Brown } 4594f86d3a8SLen Brown 4604f86d3a8SLen Brown /** 4614f86d3a8SLen Brown * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler 4624f86d3a8SLen Brown */ 4634f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void) 4644f86d3a8SLen Brown { 465a0bfa137SLen Brown if (enabled_devices) { 466a0bfa137SLen Brown initialized = 0; 4672ed903c5SChuansheng Liu wake_up_all_idle_cpus(); 4684f86d3a8SLen Brown } 469442bf3aaSDaniel Lezcano 470442bf3aaSDaniel Lezcano /* 471442bf3aaSDaniel Lezcano * Make sure external observers (such as the scheduler) 472442bf3aaSDaniel Lezcano * are done looking at pointed idle states. 473442bf3aaSDaniel Lezcano */ 474442bf3aaSDaniel Lezcano synchronize_rcu(); 4754f86d3a8SLen Brown } 4764f86d3a8SLen Brown 4774f86d3a8SLen Brown /** 4784f86d3a8SLen Brown * cpuidle_pause_and_lock - temporarily disables CPUIDLE 4794f86d3a8SLen Brown */ 4804f86d3a8SLen Brown void cpuidle_pause_and_lock(void) 4814f86d3a8SLen Brown { 4824f86d3a8SLen Brown mutex_lock(&cpuidle_lock); 4834f86d3a8SLen Brown cpuidle_uninstall_idle_handler(); 4844f86d3a8SLen Brown } 4854f86d3a8SLen Brown 4864f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); 4874f86d3a8SLen Brown 4884f86d3a8SLen Brown /** 4894f86d3a8SLen Brown * cpuidle_resume_and_unlock - resumes CPUIDLE operation 4904f86d3a8SLen Brown */ 4914f86d3a8SLen Brown void cpuidle_resume_and_unlock(void) 4924f86d3a8SLen Brown { 4934f86d3a8SLen Brown cpuidle_install_idle_handler(); 4944f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 4954f86d3a8SLen Brown } 4964f86d3a8SLen Brown 4974f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); 4984f86d3a8SLen Brown 4998651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */ 5008651f97bSPreeti U Murthy void cpuidle_pause(void) 5018651f97bSPreeti U Murthy { 5028651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 5038651f97bSPreeti U Murthy cpuidle_uninstall_idle_handler(); 5048651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 5058651f97bSPreeti U Murthy } 5068651f97bSPreeti U Murthy 5078651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */ 5088651f97bSPreeti U Murthy void cpuidle_resume(void) 5098651f97bSPreeti U Murthy { 5108651f97bSPreeti U Murthy mutex_lock(&cpuidle_lock); 5118651f97bSPreeti U Murthy cpuidle_install_idle_handler(); 5128651f97bSPreeti U Murthy mutex_unlock(&cpuidle_lock); 5138651f97bSPreeti U Murthy } 5148651f97bSPreeti U Murthy 5154f86d3a8SLen Brown /** 5164f86d3a8SLen Brown * cpuidle_enable_device - enables idle PM for a CPU 5174f86d3a8SLen Brown * @dev: the CPU 5184f86d3a8SLen Brown * 5194f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 5204f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 5214f86d3a8SLen Brown */ 5224f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev) 5234f86d3a8SLen Brown { 5245df0aa73SDaniel Lezcano int ret; 525bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv; 5264f86d3a8SLen Brown 5271b0a0e9aSSrivatsa S. Bhat if (!dev) 5281b0a0e9aSSrivatsa S. Bhat return -EINVAL; 5291b0a0e9aSSrivatsa S. Bhat 5304f86d3a8SLen Brown if (dev->enabled) 5314f86d3a8SLen Brown return 0; 532bf4d1b5dSDaniel Lezcano 533e7b06a09SGaurav Jindal if (!cpuidle_curr_governor) 534e7b06a09SGaurav Jindal return -EIO; 535e7b06a09SGaurav Jindal 536bf4d1b5dSDaniel Lezcano drv = cpuidle_get_cpu_driver(dev); 537bf4d1b5dSDaniel Lezcano 538e7b06a09SGaurav Jindal if (!drv) 5394f86d3a8SLen Brown return -EIO; 540bf4d1b5dSDaniel Lezcano 54110b9d3f8SDaniel Lezcano if (!dev->registered) 54210b9d3f8SDaniel Lezcano return -EINVAL; 54310b9d3f8SDaniel Lezcano 544bf4d1b5dSDaniel Lezcano ret = cpuidle_add_device_sysfs(dev); 545bf4d1b5dSDaniel Lezcano if (ret) 5464f86d3a8SLen Brown return ret; 5474f86d3a8SLen Brown 5483fc74bd8SGaurav Jindal if (cpuidle_curr_governor->enable) { 5493fc74bd8SGaurav Jindal ret = cpuidle_curr_governor->enable(drv, dev); 5503fc74bd8SGaurav Jindal if (ret) 5514f86d3a8SLen Brown goto fail_sysfs; 5523fc74bd8SGaurav Jindal } 5534f86d3a8SLen Brown 5544f86d3a8SLen Brown smp_wmb(); 5554f86d3a8SLen Brown 5564f86d3a8SLen Brown dev->enabled = 1; 5574f86d3a8SLen Brown 5584f86d3a8SLen Brown enabled_devices++; 5594f86d3a8SLen Brown return 0; 5604f86d3a8SLen Brown 5614f86d3a8SLen Brown fail_sysfs: 562bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 5634f86d3a8SLen Brown 5644f86d3a8SLen Brown return ret; 5654f86d3a8SLen Brown } 5664f86d3a8SLen Brown 5674f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device); 5684f86d3a8SLen Brown 5694f86d3a8SLen Brown /** 5704f86d3a8SLen Brown * cpuidle_disable_device - disables idle PM for a CPU 5714f86d3a8SLen Brown * @dev: the CPU 5724f86d3a8SLen Brown * 5734f86d3a8SLen Brown * This function must be called between cpuidle_pause_and_lock and 5744f86d3a8SLen Brown * cpuidle_resume_and_unlock when used externally. 5754f86d3a8SLen Brown */ 5764f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev) 5774f86d3a8SLen Brown { 578bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 579bf4d1b5dSDaniel Lezcano 580cf31cd1aSSrivatsa S. Bhat if (!dev || !dev->enabled) 5814f86d3a8SLen Brown return; 582bf4d1b5dSDaniel Lezcano 583bf4d1b5dSDaniel Lezcano if (!drv || !cpuidle_curr_governor) 5844f86d3a8SLen Brown return; 5854f86d3a8SLen Brown 5864f86d3a8SLen Brown dev->enabled = 0; 5874f86d3a8SLen Brown 5884f86d3a8SLen Brown if (cpuidle_curr_governor->disable) 589bf4d1b5dSDaniel Lezcano cpuidle_curr_governor->disable(drv, dev); 5904f86d3a8SLen Brown 591bf4d1b5dSDaniel Lezcano cpuidle_remove_device_sysfs(dev); 5924f86d3a8SLen Brown enabled_devices--; 5934f86d3a8SLen Brown } 5944f86d3a8SLen Brown 5954f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device); 5964f86d3a8SLen Brown 597f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev) 598f6bb51a5SDaniel Lezcano { 599f6bb51a5SDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 600f6bb51a5SDaniel Lezcano 601f6bb51a5SDaniel Lezcano list_del(&dev->device_list); 602f6bb51a5SDaniel Lezcano per_cpu(cpuidle_devices, dev->cpu) = NULL; 603f6bb51a5SDaniel Lezcano module_put(drv->owner); 604c998c078SDave Gerlach 605c998c078SDave Gerlach dev->registered = 0; 606f6bb51a5SDaniel Lezcano } 607f6bb51a5SDaniel Lezcano 608267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev) 6095df0aa73SDaniel Lezcano { 6105df0aa73SDaniel Lezcano memset(dev->states_usage, 0, sizeof(dev->states_usage)); 611c1d51f68SRafael J. Wysocki dev->last_residency_ns = 0; 6126f9b83acSUlf Hansson dev->next_hrtimer = 0; 6135df0aa73SDaniel Lezcano } 6145df0aa73SDaniel Lezcano 6154f86d3a8SLen Brown /** 616dcb84f33SVenkatesh Pallipadi * __cpuidle_register_device - internal register function called before register 617dcb84f33SVenkatesh Pallipadi * and enable routines 6184f86d3a8SLen Brown * @dev: the cpu 619dcb84f33SVenkatesh Pallipadi * 620dcb84f33SVenkatesh Pallipadi * cpuidle_lock mutex must be held before this is called 6214f86d3a8SLen Brown */ 622dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev) 6234f86d3a8SLen Brown { 624bf4d1b5dSDaniel Lezcano struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 62599e98d3fSRafael J. Wysocki int i, ret; 6264f86d3a8SLen Brown 627bf4d1b5dSDaniel Lezcano if (!try_module_get(drv->owner)) 6284f86d3a8SLen Brown return -EINVAL; 6294f86d3a8SLen Brown 63075a80267SRafael J. Wysocki for (i = 0; i < drv->state_count; i++) { 631ba1e78a1SRafael J. Wysocki if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE) 63299e98d3fSRafael J. Wysocki dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER; 63399e98d3fSRafael J. Wysocki 63475a80267SRafael J. Wysocki if (drv->states[i].flags & CPUIDLE_FLAG_OFF) 63575a80267SRafael J. Wysocki dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER; 63675a80267SRafael J. Wysocki } 63775a80267SRafael J. Wysocki 6384f86d3a8SLen Brown per_cpu(cpuidle_devices, dev->cpu) = dev; 6394f86d3a8SLen Brown list_add(&dev->device_list, &cpuidle_detected_devices); 6404f86d3a8SLen Brown 6414126c019SColin Cross ret = cpuidle_coupled_register_device(dev); 64247182668SViresh Kumar if (ret) 643f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 64447182668SViresh Kumar else 645dcb84f33SVenkatesh Pallipadi dev->registered = 1; 64647182668SViresh Kumar 64747182668SViresh Kumar return ret; 648dcb84f33SVenkatesh Pallipadi } 649dcb84f33SVenkatesh Pallipadi 650dcb84f33SVenkatesh Pallipadi /** 651dcb84f33SVenkatesh Pallipadi * cpuidle_register_device - registers a CPU's idle PM feature 652dcb84f33SVenkatesh Pallipadi * @dev: the cpu 653dcb84f33SVenkatesh Pallipadi */ 654dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev) 655dcb84f33SVenkatesh Pallipadi { 656c878a52dSDaniel Lezcano int ret = -EBUSY; 657dcb84f33SVenkatesh Pallipadi 6581b0a0e9aSSrivatsa S. Bhat if (!dev) 6591b0a0e9aSSrivatsa S. Bhat return -EINVAL; 6601b0a0e9aSSrivatsa S. Bhat 661dcb84f33SVenkatesh Pallipadi mutex_lock(&cpuidle_lock); 662dcb84f33SVenkatesh Pallipadi 663c878a52dSDaniel Lezcano if (dev->registered) 664c878a52dSDaniel Lezcano goto out_unlock; 665c878a52dSDaniel Lezcano 666267d4bf8SViresh Kumar __cpuidle_device_init(dev); 6675df0aa73SDaniel Lezcano 668f6bb51a5SDaniel Lezcano ret = __cpuidle_register_device(dev); 669f6bb51a5SDaniel Lezcano if (ret) 670f6bb51a5SDaniel Lezcano goto out_unlock; 671f6bb51a5SDaniel Lezcano 672f6bb51a5SDaniel Lezcano ret = cpuidle_add_sysfs(dev); 673f6bb51a5SDaniel Lezcano if (ret) 674f6bb51a5SDaniel Lezcano goto out_unregister; 675dcb84f33SVenkatesh Pallipadi 67610b9d3f8SDaniel Lezcano ret = cpuidle_enable_device(dev); 677f6bb51a5SDaniel Lezcano if (ret) 678f6bb51a5SDaniel Lezcano goto out_sysfs; 67910b9d3f8SDaniel Lezcano 6804f86d3a8SLen Brown cpuidle_install_idle_handler(); 6814f86d3a8SLen Brown 682f6bb51a5SDaniel Lezcano out_unlock: 6834f86d3a8SLen Brown mutex_unlock(&cpuidle_lock); 6844f86d3a8SLen Brown 685f6bb51a5SDaniel Lezcano return ret; 686f6bb51a5SDaniel Lezcano 687f6bb51a5SDaniel Lezcano out_sysfs: 688f6bb51a5SDaniel Lezcano cpuidle_remove_sysfs(dev); 689f6bb51a5SDaniel Lezcano out_unregister: 690f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 691f6bb51a5SDaniel Lezcano goto out_unlock; 6924f86d3a8SLen Brown } 6934f86d3a8SLen Brown 6944f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device); 6954f86d3a8SLen Brown 6964f86d3a8SLen Brown /** 6974f86d3a8SLen Brown * cpuidle_unregister_device - unregisters a CPU's idle PM feature 6984f86d3a8SLen Brown * @dev: the cpu 6994f86d3a8SLen Brown */ 7004f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev) 7014f86d3a8SLen Brown { 702813e8e3dSKonrad Rzeszutek Wilk if (!dev || dev->registered == 0) 703dcb84f33SVenkatesh Pallipadi return; 704dcb84f33SVenkatesh Pallipadi 7054f86d3a8SLen Brown cpuidle_pause_and_lock(); 7064f86d3a8SLen Brown 7074f86d3a8SLen Brown cpuidle_disable_device(dev); 7084f86d3a8SLen Brown 7091aef40e2SDaniel Lezcano cpuidle_remove_sysfs(dev); 710f6bb51a5SDaniel Lezcano 711f6bb51a5SDaniel Lezcano __cpuidle_unregister_device(dev); 7124f86d3a8SLen Brown 7134126c019SColin Cross cpuidle_coupled_unregister_device(dev); 7144126c019SColin Cross 7154f86d3a8SLen Brown cpuidle_resume_and_unlock(); 7164f86d3a8SLen Brown } 7174f86d3a8SLen Brown 7184f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device); 7194f86d3a8SLen Brown 7201c192d04SDaniel Lezcano /** 7214c637b21SDaniel Lezcano * cpuidle_unregister: unregister a driver and the devices. This function 7224c637b21SDaniel Lezcano * can be used only if the driver has been previously registered through 7234c637b21SDaniel Lezcano * the cpuidle_register function. 7244c637b21SDaniel Lezcano * 7254c637b21SDaniel Lezcano * @drv: a valid pointer to a struct cpuidle_driver 7264c637b21SDaniel Lezcano */ 7274c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv) 7284c637b21SDaniel Lezcano { 7294c637b21SDaniel Lezcano int cpu; 7304c637b21SDaniel Lezcano struct cpuidle_device *device; 7314c637b21SDaniel Lezcano 73282467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 7334c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 7344c637b21SDaniel Lezcano cpuidle_unregister_device(device); 7354c637b21SDaniel Lezcano } 7364c637b21SDaniel Lezcano 7374c637b21SDaniel Lezcano cpuidle_unregister_driver(drv); 7384c637b21SDaniel Lezcano } 7394c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister); 7404c637b21SDaniel Lezcano 7414c637b21SDaniel Lezcano /** 7424c637b21SDaniel Lezcano * cpuidle_register: registers the driver and the cpu devices with the 7434c637b21SDaniel Lezcano * coupled_cpus passed as parameter. This function is used for all common 7444c637b21SDaniel Lezcano * initialization pattern there are in the arch specific drivers. The 7454c637b21SDaniel Lezcano * devices is globally defined in this file. 7464c637b21SDaniel Lezcano * 7474c637b21SDaniel Lezcano * @drv : a valid pointer to a struct cpuidle_driver 7484c637b21SDaniel Lezcano * @coupled_cpus: a cpumask for the coupled states 7494c637b21SDaniel Lezcano * 7504c637b21SDaniel Lezcano * Returns 0 on success, < 0 otherwise 7514c637b21SDaniel Lezcano */ 7524c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv, 7534c637b21SDaniel Lezcano const struct cpumask *const coupled_cpus) 7544c637b21SDaniel Lezcano { 7554c637b21SDaniel Lezcano int ret, cpu; 7564c637b21SDaniel Lezcano struct cpuidle_device *device; 7574c637b21SDaniel Lezcano 7584c637b21SDaniel Lezcano ret = cpuidle_register_driver(drv); 7594c637b21SDaniel Lezcano if (ret) { 7604c637b21SDaniel Lezcano pr_err("failed to register cpuidle driver\n"); 7614c637b21SDaniel Lezcano return ret; 7624c637b21SDaniel Lezcano } 7634c637b21SDaniel Lezcano 76482467a5aSDaniel Lezcano for_each_cpu(cpu, drv->cpumask) { 7654c637b21SDaniel Lezcano device = &per_cpu(cpuidle_dev, cpu); 7664c637b21SDaniel Lezcano device->cpu = cpu; 7674c637b21SDaniel Lezcano 7684c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED 7694c637b21SDaniel Lezcano /* 770caf4a36eSViresh Kumar * On multiplatform for ARM, the coupled idle states could be 7714c637b21SDaniel Lezcano * enabled in the kernel even if the cpuidle driver does not 7724c637b21SDaniel Lezcano * use it. Note, coupled_cpus is a struct copy. 7734c637b21SDaniel Lezcano */ 7744c637b21SDaniel Lezcano if (coupled_cpus) 7754c637b21SDaniel Lezcano device->coupled_cpus = *coupled_cpus; 7764c637b21SDaniel Lezcano #endif 7774c637b21SDaniel Lezcano ret = cpuidle_register_device(device); 7784c637b21SDaniel Lezcano if (!ret) 7794c637b21SDaniel Lezcano continue; 7804c637b21SDaniel Lezcano 7814c637b21SDaniel Lezcano pr_err("Failed to register cpuidle device for cpu%d\n", cpu); 7824c637b21SDaniel Lezcano 7834c637b21SDaniel Lezcano cpuidle_unregister(drv); 7844c637b21SDaniel Lezcano break; 7854c637b21SDaniel Lezcano } 7864c637b21SDaniel Lezcano 7874c637b21SDaniel Lezcano return ret; 7884c637b21SDaniel Lezcano } 7894c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register); 7904c637b21SDaniel Lezcano 7914f86d3a8SLen Brown /** 7924f86d3a8SLen Brown * cpuidle_init - core initializer 7934f86d3a8SLen Brown */ 7944f86d3a8SLen Brown static int __init cpuidle_init(void) 7954f86d3a8SLen Brown { 79662027aeaSLen Brown if (cpuidle_disabled()) 79762027aeaSLen Brown return -ENODEV; 79862027aeaSLen Brown 7993a4a0042SRafael J. Wysocki return cpuidle_add_interface(cpu_subsys.dev_root); 8004f86d3a8SLen Brown } 8014f86d3a8SLen Brown 80262027aeaSLen Brown module_param(off, int, 0444); 80361cb5758SRafael J. Wysocki module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444); 8044f86d3a8SLen Brown core_initcall(cpuidle_init); 805