19 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
114 #define WAITING_BITS 16
115 #define MAX_WAITING_CPUS (1 << WAITING_BITS)
116 #define WAITING_MASK (MAX_WAITING_CPUS - 1)
117 #define READY_MASK (~WAITING_MASK)
119 #define CPUIDLE_COUPLED_NOT_IDLE (-1)
130 static cpumask_t cpuidle_coupled_poked_mask;
152 int n = dev->coupled->online_count;
187 static inline void cpuidle_coupled_set_ready(
struct cpuidle_coupled *coupled)
207 inline int cpuidle_coupled_set_not_ready(
struct cpuidle_coupled *coupled)
225 static inline int cpuidle_coupled_no_cpus_ready(
struct cpuidle_coupled *coupled)
237 static inline bool cpuidle_coupled_cpus_ready(
struct cpuidle_coupled *coupled)
249 static inline bool cpuidle_coupled_cpus_waiting(
struct cpuidle_coupled *coupled)
261 static inline int cpuidle_coupled_no_cpus_waiting(
struct cpuidle_coupled *coupled)
288 if (
cpu_online(i) && coupled->requested_state[i] < state)
289 state = coupled->requested_state[i];
294 static
void cpuidle_coupled_poked(
void *
info)
297 cpumask_clear_cpu(cpu, &cpuidle_coupled_poked_mask);
312 static void cpuidle_coupled_poke(
int cpu)
316 if (!cpumask_test_and_set_cpu(cpu, &cpuidle_coupled_poked_mask))
317 __smp_call_function_single(cpu, csd, 0);
327 static void cpuidle_coupled_poke_others(
int this_cpu,
334 cpuidle_coupled_poke(cpu);
347 static
void cpuidle_coupled_set_waiting(
int cpu,
365 if (w == coupled->online_count)
366 cpuidle_coupled_poke_others(cpu, coupled);
376 static void cpuidle_coupled_set_not_waiting(
int cpu,
399 static void cpuidle_coupled_set_done(
int cpu,
struct cpuidle_coupled *coupled)
401 cpuidle_coupled_set_not_waiting(cpu, coupled);
418 static int cpuidle_coupled_clear_pokes(
int cpu)
425 return need_resched() ? -
EINTR : 0;
450 int entered_state = -1;
457 if (cpuidle_coupled_clear_pokes(dev->
cpu)) {
459 return entered_state;
462 dev->safe_state_index);
468 cpuidle_coupled_set_waiting(dev->
cpu, coupled, next_state);
475 while (!cpuidle_coupled_cpus_waiting(coupled)) {
476 if (cpuidle_coupled_clear_pokes(dev->
cpu)) {
477 cpuidle_coupled_set_not_waiting(dev->
cpu, coupled);
482 cpuidle_coupled_set_not_waiting(dev->
cpu, coupled);
487 dev->safe_state_index);
490 if (cpuidle_coupled_clear_pokes(dev->
cpu)) {
491 cpuidle_coupled_set_not_waiting(dev->
cpu, coupled);
504 cpuidle_coupled_set_ready(coupled);
505 while (!cpuidle_coupled_cpus_ready(coupled)) {
507 if (!cpuidle_coupled_cpus_waiting(coupled))
508 if (!cpuidle_coupled_set_not_ready(coupled))
515 next_state = cpuidle_coupled_get_state(dev, coupled);
519 cpuidle_coupled_set_done(dev->
cpu, coupled);
543 while (!cpuidle_coupled_no_cpus_ready(coupled))
546 return entered_state;
549 static void cpuidle_coupled_update_online_cpus(
struct cpuidle_coupled *coupled)
552 cpumask_and(&cpus, cpu_online_mask, &coupled->
coupled_cpus);
571 if (cpumask_empty(&dev->coupled_cpus))
575 other_dev =
per_cpu(cpuidle_devices, cpu);
576 if (other_dev && other_dev->coupled) {
577 coupled = other_dev->coupled;
590 dev->coupled = coupled;
594 cpuidle_coupled_update_online_cpus(coupled);
598 csd = &
per_cpu(cpuidle_coupled_poke_cb, dev->
cpu);
599 csd->
func = cpuidle_coupled_poked;
600 csd->
info = (
void *)(
unsigned long)dev->
cpu;
617 if (cpumask_empty(&dev->coupled_cpus))
632 static void cpuidle_coupled_prevent_idle(
struct cpuidle_coupled *coupled)
638 cpuidle_coupled_poke_others(cpu, coupled);
640 while (!cpuidle_coupled_no_cpus_waiting(coupled))
651 static void cpuidle_coupled_allow_idle(
struct cpuidle_coupled *coupled)
662 cpuidle_coupled_poke_others(cpu, coupled);
676 unsigned long action,
void *hcpu)
678 int cpu = (
unsigned long)hcpu;
695 dev =
per_cpu(cpuidle_devices, cpu);
696 if (!dev || !dev->coupled)
702 cpuidle_coupled_prevent_idle(dev->coupled);
706 cpuidle_coupled_update_online_cpus(dev->coupled);
710 cpuidle_coupled_allow_idle(dev->coupled);
720 .notifier_call = cpuidle_coupled_cpu_notify,
723 static int __init cpuidle_coupled_init(
void)
725 return register_cpu_notifier(&cpuidle_coupled_cpu_notifier);