blob: db92bcbe69461e404ea036025663ecfa49d5b6ce [file] [log] [blame]
Colin Cross4126c012012-05-07 17:57:41 -07001/*
2 * coupled.c - helper functions to enter the same idle state on multiple cpus
3 *
4 * Copyright (c) 2011 Google, Inc.
5 *
6 * Author: Colin Cross <ccross@android.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 */
18
19#include <linux/kernel.h>
20#include <linux/cpu.h>
21#include <linux/cpuidle.h>
22#include <linux/mutex.h>
23#include <linux/sched.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26
27#include "cpuidle.h"
28
29/**
30 * DOC: Coupled cpuidle states
31 *
32 * On some ARM SMP SoCs (OMAP4460, Tegra 2, and probably more), the
33 * cpus cannot be independently powered down, either due to
34 * sequencing restrictions (on Tegra 2, cpu 0 must be the last to
35 * power down), or due to HW bugs (on OMAP4460, a cpu powering up
36 * will corrupt the gic state unless the other cpu runs a work
37 * around). Each cpu has a power state that it can enter without
38 * coordinating with the other cpu (usually Wait For Interrupt, or
39 * WFI), and one or more "coupled" power states that affect blocks
40 * shared between the cpus (L2 cache, interrupt controller, and
41 * sometimes the whole SoC). Entering a coupled power state must
42 * be tightly controlled on both cpus.
43 *
44 * This file implements a solution, where each cpu will wait in the
45 * WFI state until all cpus are ready to enter a coupled state, at
46 * which point the coupled state function will be called on all
47 * cpus at approximately the same time.
48 *
49 * Once all cpus are ready to enter idle, they are woken by an smp
50 * cross call. At this point, there is a chance that one of the
51 * cpus will find work to do, and choose not to enter idle. A
52 * final pass is needed to guarantee that all cpus will call the
53 * power state enter function at the same time. During this pass,
54 * each cpu will increment the ready counter, and continue once the
55 * ready counter matches the number of online coupled cpus. If any
56 * cpu exits idle, the other cpus will decrement their counter and
57 * retry.
58 *
59 * requested_state stores the deepest coupled idle state each cpu
60 * is ready for. It is assumed that the states are indexed from
61 * shallowest (highest power, lowest exit latency) to deepest
62 * (lowest power, highest exit latency). The requested_state
63 * variable is not locked. It is only written from the cpu that
64 * it stores (or by the on/offlining cpu if that cpu is offline),
65 * and only read after all the cpus are ready for the coupled idle
66 * state are are no longer updating it.
67 *
68 * Three atomic counters are used. alive_count tracks the number
69 * of cpus in the coupled set that are currently or soon will be
70 * online. waiting_count tracks the number of cpus that are in
71 * the waiting loop, in the ready loop, or in the coupled idle state.
72 * ready_count tracks the number of cpus that are in the ready loop
73 * or in the coupled idle state.
74 *
75 * To use coupled cpuidle states, a cpuidle driver must:
76 *
77 * Set struct cpuidle_device.coupled_cpus to the mask of all
78 * coupled cpus, usually the same as cpu_possible_mask if all cpus
79 * are part of the same cluster. The coupled_cpus mask must be
80 * set in the struct cpuidle_device for each cpu.
81 *
82 * Set struct cpuidle_device.safe_state to a state that is not a
83 * coupled state. This is usually WFI.
84 *
85 * Set CPUIDLE_FLAG_COUPLED in struct cpuidle_state.flags for each
86 * state that affects multiple cpus.
87 *
88 * Provide a struct cpuidle_state.enter function for each state
89 * that affects multiple cpus. This function is guaranteed to be
90 * called on all cpus at approximately the same time. The driver
91 * should ensure that the cpus all abort together if any cpu tries
92 * to abort once the function is called. The function should return
93 * with interrupts still disabled.
94 */
95
96/**
97 * struct cpuidle_coupled - data for set of cpus that share a coupled idle state
98 * @coupled_cpus: mask of cpus that are part of the coupled set
99 * @requested_state: array of requested states for cpus in the coupled set
100 * @ready_waiting_counts: combined count of cpus in ready or waiting loops
101 * @online_count: count of cpus that are online
102 * @refcnt: reference count of cpuidle devices that are using this struct
103 * @prevent: flag to prevent coupled idle while a cpu is hotplugging
104 */
105struct cpuidle_coupled {
106 cpumask_t coupled_cpus;
107 int requested_state[NR_CPUS];
108 atomic_t ready_waiting_counts;
109 int online_count;
110 int refcnt;
111 int prevent;
112};
113
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)
118
119#define CPUIDLE_COUPLED_NOT_IDLE (-1)
120
121static DEFINE_MUTEX(cpuidle_coupled_lock);
122static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb);
123
124/*
125 * The cpuidle_coupled_poked_mask mask is used to avoid calling
126 * __smp_call_function_single with the per cpu call_single_data struct already
127 * in use. This prevents a deadlock where two cpus are waiting for each others
128 * call_single_data struct to be available
129 */
130static cpumask_t cpuidle_coupled_poked_mask;
131
132/**
Colin Cross20ff51a2012-05-07 17:57:42 -0700133 * cpuidle_coupled_parallel_barrier - synchronize all online coupled cpus
134 * @dev: cpuidle_device of the calling cpu
135 * @a: atomic variable to hold the barrier
136 *
137 * No caller to this function will return from this function until all online
138 * cpus in the same coupled group have called this function. Once any caller
139 * has returned from this function, the barrier is immediately available for
140 * reuse.
141 *
142 * The atomic variable a must be initialized to 0 before any cpu calls
143 * this function, will be reset to 0 before any cpu returns from this function.
144 *
145 * Must only be called from within a coupled idle state handler
146 * (state.enter when state.flags has CPUIDLE_FLAG_COUPLED set).
147 *
148 * Provides full smp barrier semantics before and after calling.
149 */
150void cpuidle_coupled_parallel_barrier(struct cpuidle_device *dev, atomic_t *a)
151{
152 int n = dev->coupled->online_count;
153
154 smp_mb__before_atomic_inc();
155 atomic_inc(a);
156
157 while (atomic_read(a) < n)
158 cpu_relax();
159
160 if (atomic_inc_return(a) == n * 2) {
161 atomic_set(a, 0);
162 return;
163 }
164
165 while (atomic_read(a) > n)
166 cpu_relax();
167}
168
169/**
Colin Cross4126c012012-05-07 17:57:41 -0700170 * cpuidle_state_is_coupled - check if a state is part of a coupled set
171 * @dev: struct cpuidle_device for the current cpu
172 * @drv: struct cpuidle_driver for the platform
173 * @state: index of the target state in drv->states
174 *
175 * Returns true if the target state is coupled with cpus besides this one
176 */
177bool cpuidle_state_is_coupled(struct cpuidle_device *dev,
178 struct cpuidle_driver *drv, int state)
179{
180 return drv->states[state].flags & CPUIDLE_FLAG_COUPLED;
181}
182
183/**
184 * cpuidle_coupled_set_ready - mark a cpu as ready
185 * @coupled: the struct coupled that contains the current cpu
186 */
187static inline void cpuidle_coupled_set_ready(struct cpuidle_coupled *coupled)
188{
189 atomic_add(MAX_WAITING_CPUS, &coupled->ready_waiting_counts);
190}
191
192/**
193 * cpuidle_coupled_set_not_ready - mark a cpu as not ready
194 * @coupled: the struct coupled that contains the current cpu
195 *
196 * Decrements the ready counter, unless the ready (and thus the waiting) counter
197 * is equal to the number of online cpus. Prevents a race where one cpu
198 * decrements the waiting counter and then re-increments it just before another
199 * cpu has decremented its ready counter, leading to the ready counter going
200 * down from the number of online cpus without going through the coupled idle
201 * state.
202 *
203 * Returns 0 if the counter was decremented successfully, -EINVAL if the ready
204 * counter was equal to the number of online cpus.
205 */
206static
207inline int cpuidle_coupled_set_not_ready(struct cpuidle_coupled *coupled)
208{
209 int all;
210 int ret;
211
Sivaram Nair92638e22012-12-18 13:52:54 +0100212 all = coupled->online_count | (coupled->online_count << WAITING_BITS);
Colin Cross4126c012012-05-07 17:57:41 -0700213 ret = atomic_add_unless(&coupled->ready_waiting_counts,
214 -MAX_WAITING_CPUS, all);
215
216 return ret ? 0 : -EINVAL;
217}
218
219/**
220 * cpuidle_coupled_no_cpus_ready - check if no cpus in a coupled set are ready
221 * @coupled: the struct coupled that contains the current cpu
222 *
223 * Returns true if all of the cpus in a coupled set are out of the ready loop.
224 */
225static inline int cpuidle_coupled_no_cpus_ready(struct cpuidle_coupled *coupled)
226{
227 int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS;
228 return r == 0;
229}
230
231/**
232 * cpuidle_coupled_cpus_ready - check if all cpus in a coupled set are ready
233 * @coupled: the struct coupled that contains the current cpu
234 *
235 * Returns true if all cpus coupled to this target state are in the ready loop
236 */
237static inline bool cpuidle_coupled_cpus_ready(struct cpuidle_coupled *coupled)
238{
239 int r = atomic_read(&coupled->ready_waiting_counts) >> WAITING_BITS;
240 return r == coupled->online_count;
241}
242
243/**
244 * cpuidle_coupled_cpus_waiting - check if all cpus in a coupled set are waiting
245 * @coupled: the struct coupled that contains the current cpu
246 *
247 * Returns true if all cpus coupled to this target state are in the wait loop
248 */
249static inline bool cpuidle_coupled_cpus_waiting(struct cpuidle_coupled *coupled)
250{
251 int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK;
252 return w == coupled->online_count;
253}
254
255/**
256 * cpuidle_coupled_no_cpus_waiting - check if no cpus in coupled set are waiting
257 * @coupled: the struct coupled that contains the current cpu
258 *
259 * Returns true if all of the cpus in a coupled set are out of the waiting loop.
260 */
261static inline int cpuidle_coupled_no_cpus_waiting(struct cpuidle_coupled *coupled)
262{
263 int w = atomic_read(&coupled->ready_waiting_counts) & WAITING_MASK;
264 return w == 0;
265}
266
267/**
268 * cpuidle_coupled_get_state - determine the deepest idle state
269 * @dev: struct cpuidle_device for this cpu
270 * @coupled: the struct coupled that contains the current cpu
271 *
272 * Returns the deepest idle state that all coupled cpus can enter
273 */
274static inline int cpuidle_coupled_get_state(struct cpuidle_device *dev,
275 struct cpuidle_coupled *coupled)
276{
277 int i;
278 int state = INT_MAX;
279
280 /*
281 * Read barrier ensures that read of requested_state is ordered after
282 * reads of ready_count. Matches the write barriers
283 * cpuidle_set_state_waiting.
284 */
285 smp_rmb();
286
287 for_each_cpu_mask(i, coupled->coupled_cpus)
288 if (cpu_online(i) && coupled->requested_state[i] < state)
289 state = coupled->requested_state[i];
290
291 return state;
292}
293
294static void cpuidle_coupled_poked(void *info)
295{
296 int cpu = (unsigned long)info;
297 cpumask_clear_cpu(cpu, &cpuidle_coupled_poked_mask);
298}
299
300/**
301 * cpuidle_coupled_poke - wake up a cpu that may be waiting
302 * @cpu: target cpu
303 *
304 * Ensures that the target cpu exits it's waiting idle state (if it is in it)
305 * and will see updates to waiting_count before it re-enters it's waiting idle
306 * state.
307 *
308 * If cpuidle_coupled_poked_mask is already set for the target cpu, that cpu
309 * either has or will soon have a pending IPI that will wake it out of idle,
310 * or it is currently processing the IPI and is not in idle.
311 */
312static void cpuidle_coupled_poke(int cpu)
313{
314 struct call_single_data *csd = &per_cpu(cpuidle_coupled_poke_cb, cpu);
315
316 if (!cpumask_test_and_set_cpu(cpu, &cpuidle_coupled_poked_mask))
317 __smp_call_function_single(cpu, csd, 0);
318}
319
320/**
321 * cpuidle_coupled_poke_others - wake up all other cpus that may be waiting
322 * @dev: struct cpuidle_device for this cpu
323 * @coupled: the struct coupled that contains the current cpu
324 *
325 * Calls cpuidle_coupled_poke on all other online cpus.
326 */
327static void cpuidle_coupled_poke_others(int this_cpu,
328 struct cpuidle_coupled *coupled)
329{
330 int cpu;
331
332 for_each_cpu_mask(cpu, coupled->coupled_cpus)
333 if (cpu != this_cpu && cpu_online(cpu))
334 cpuidle_coupled_poke(cpu);
335}
336
337/**
338 * cpuidle_coupled_set_waiting - mark this cpu as in the wait loop
339 * @dev: struct cpuidle_device for this cpu
340 * @coupled: the struct coupled that contains the current cpu
341 * @next_state: the index in drv->states of the requested state for this cpu
342 *
343 * Updates the requested idle state for the specified cpuidle device,
344 * poking all coupled cpus out of idle if necessary to let them see the new
345 * state.
346 */
347static void cpuidle_coupled_set_waiting(int cpu,
348 struct cpuidle_coupled *coupled, int next_state)
349{
350 int w;
351
352 coupled->requested_state[cpu] = next_state;
353
354 /*
355 * If this is the last cpu to enter the waiting state, poke
356 * all the other cpus out of their waiting state so they can
357 * enter a deeper state. This can race with one of the cpus
358 * exiting the waiting state due to an interrupt and
359 * decrementing waiting_count, see comment below.
360 *
361 * The atomic_inc_return provides a write barrier to order the write
362 * to requested_state with the later write that increments ready_count.
363 */
364 w = atomic_inc_return(&coupled->ready_waiting_counts) & WAITING_MASK;
365 if (w == coupled->online_count)
366 cpuidle_coupled_poke_others(cpu, coupled);
367}
368
369/**
370 * cpuidle_coupled_set_not_waiting - mark this cpu as leaving the wait loop
371 * @dev: struct cpuidle_device for this cpu
372 * @coupled: the struct coupled that contains the current cpu
373 *
374 * Removes the requested idle state for the specified cpuidle device.
375 */
376static void cpuidle_coupled_set_not_waiting(int cpu,
377 struct cpuidle_coupled *coupled)
378{
379 /*
380 * Decrementing waiting count can race with incrementing it in
381 * cpuidle_coupled_set_waiting, but that's OK. Worst case, some
382 * cpus will increment ready_count and then spin until they
383 * notice that this cpu has cleared it's requested_state.
384 */
385 atomic_dec(&coupled->ready_waiting_counts);
386
387 coupled->requested_state[cpu] = CPUIDLE_COUPLED_NOT_IDLE;
388}
389
390/**
391 * cpuidle_coupled_set_done - mark this cpu as leaving the ready loop
392 * @cpu: the current cpu
393 * @coupled: the struct coupled that contains the current cpu
394 *
395 * Marks this cpu as no longer in the ready and waiting loops. Decrements
396 * the waiting count first to prevent another cpu looping back in and seeing
397 * this cpu as waiting just before it exits idle.
398 */
399static void cpuidle_coupled_set_done(int cpu, struct cpuidle_coupled *coupled)
400{
401 cpuidle_coupled_set_not_waiting(cpu, coupled);
402 atomic_sub(MAX_WAITING_CPUS, &coupled->ready_waiting_counts);
403}
404
405/**
406 * cpuidle_coupled_clear_pokes - spin until the poke interrupt is processed
407 * @cpu - this cpu
408 *
409 * Turns on interrupts and spins until any outstanding poke interrupts have
410 * been processed and the poke bit has been cleared.
411 *
412 * Other interrupts may also be processed while interrupts are enabled, so
413 * need_resched() must be tested after turning interrupts off again to make sure
414 * the interrupt didn't schedule work that should take the cpu out of idle.
415 *
416 * Returns 0 if need_resched was false, -EINTR if need_resched was true.
417 */
418static int cpuidle_coupled_clear_pokes(int cpu)
419{
420 local_irq_enable();
421 while (cpumask_test_cpu(cpu, &cpuidle_coupled_poked_mask))
422 cpu_relax();
423 local_irq_disable();
424
425 return need_resched() ? -EINTR : 0;
426}
427
428/**
429 * cpuidle_enter_state_coupled - attempt to enter a state with coupled cpus
430 * @dev: struct cpuidle_device for the current cpu
431 * @drv: struct cpuidle_driver for the platform
432 * @next_state: index of the requested state in drv->states
433 *
434 * Coordinate with coupled cpus to enter the target state. This is a two
435 * stage process. In the first stage, the cpus are operating independently,
436 * and may call into cpuidle_enter_state_coupled at completely different times.
437 * To save as much power as possible, the first cpus to call this function will
438 * go to an intermediate state (the cpuidle_device's safe state), and wait for
439 * all the other cpus to call this function. Once all coupled cpus are idle,
440 * the second stage will start. Each coupled cpu will spin until all cpus have
441 * guaranteed that they will call the target_state.
442 *
443 * This function must be called with interrupts disabled. It may enable
444 * interrupts while preparing for idle, and it will always return with
445 * interrupts enabled.
446 */
447int cpuidle_enter_state_coupled(struct cpuidle_device *dev,
448 struct cpuidle_driver *drv, int next_state)
449{
450 int entered_state = -1;
451 struct cpuidle_coupled *coupled = dev->coupled;
452
453 if (!coupled)
454 return -EINVAL;
455
456 while (coupled->prevent) {
457 if (cpuidle_coupled_clear_pokes(dev->cpu)) {
458 local_irq_enable();
459 return entered_state;
460 }
461 entered_state = cpuidle_enter_state(dev, drv,
462 dev->safe_state_index);
Colin Cross59e99852013-08-23 12:45:10 -0700463 local_irq_disable();
Colin Cross4126c012012-05-07 17:57:41 -0700464 }
465
466 /* Read barrier ensures online_count is read after prevent is cleared */
467 smp_rmb();
468
469 cpuidle_coupled_set_waiting(dev->cpu, coupled, next_state);
470
471retry:
472 /*
473 * Wait for all coupled cpus to be idle, using the deepest state
474 * allowed for a single cpu.
475 */
476 while (!cpuidle_coupled_cpus_waiting(coupled)) {
477 if (cpuidle_coupled_clear_pokes(dev->cpu)) {
478 cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
479 goto out;
480 }
481
482 if (coupled->prevent) {
483 cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
484 goto out;
485 }
486
487 entered_state = cpuidle_enter_state(dev, drv,
488 dev->safe_state_index);
Colin Cross59e99852013-08-23 12:45:10 -0700489 local_irq_disable();
Colin Cross4126c012012-05-07 17:57:41 -0700490 }
491
492 if (cpuidle_coupled_clear_pokes(dev->cpu)) {
493 cpuidle_coupled_set_not_waiting(dev->cpu, coupled);
494 goto out;
495 }
496
497 /*
498 * All coupled cpus are probably idle. There is a small chance that
499 * one of the other cpus just became active. Increment the ready count,
500 * and spin until all coupled cpus have incremented the counter. Once a
501 * cpu has incremented the ready counter, it cannot abort idle and must
502 * spin until either all cpus have incremented the ready counter, or
503 * another cpu leaves idle and decrements the waiting counter.
504 */
505
506 cpuidle_coupled_set_ready(coupled);
507 while (!cpuidle_coupled_cpus_ready(coupled)) {
508 /* Check if any other cpus bailed out of idle. */
509 if (!cpuidle_coupled_cpus_waiting(coupled))
510 if (!cpuidle_coupled_set_not_ready(coupled))
511 goto retry;
512
513 cpu_relax();
514 }
515
516 /* all cpus have acked the coupled state */
517 next_state = cpuidle_coupled_get_state(dev, coupled);
518
519 entered_state = cpuidle_enter_state(dev, drv, next_state);
520
521 cpuidle_coupled_set_done(dev->cpu, coupled);
522
523out:
524 /*
525 * Normal cpuidle states are expected to return with irqs enabled.
526 * That leads to an inefficiency where a cpu receiving an interrupt
527 * that brings it out of idle will process that interrupt before
528 * exiting the idle enter function and decrementing ready_count. All
529 * other cpus will need to spin waiting for the cpu that is processing
530 * the interrupt. If the driver returns with interrupts disabled,
531 * all other cpus will loop back into the safe idle state instead of
532 * spinning, saving power.
533 *
534 * Calling local_irq_enable here allows coupled states to return with
535 * interrupts disabled, but won't cause problems for drivers that
536 * exit with interrupts enabled.
537 */
538 local_irq_enable();
539
540 /*
541 * Wait until all coupled cpus have exited idle. There is no risk that
542 * a cpu exits and re-enters the ready state because this cpu has
543 * already decremented its waiting_count.
544 */
545 while (!cpuidle_coupled_no_cpus_ready(coupled))
546 cpu_relax();
547
548 return entered_state;
549}
550
551static void cpuidle_coupled_update_online_cpus(struct cpuidle_coupled *coupled)
552{
553 cpumask_t cpus;
554 cpumask_and(&cpus, cpu_online_mask, &coupled->coupled_cpus);
555 coupled->online_count = cpumask_weight(&cpus);
556}
557
558/**
559 * cpuidle_coupled_register_device - register a coupled cpuidle device
560 * @dev: struct cpuidle_device for the current cpu
561 *
562 * Called from cpuidle_register_device to handle coupled idle init. Finds the
563 * cpuidle_coupled struct for this set of coupled cpus, or creates one if none
564 * exists yet.
565 */
566int cpuidle_coupled_register_device(struct cpuidle_device *dev)
567{
568 int cpu;
569 struct cpuidle_device *other_dev;
570 struct call_single_data *csd;
571 struct cpuidle_coupled *coupled;
572
573 if (cpumask_empty(&dev->coupled_cpus))
574 return 0;
575
576 for_each_cpu_mask(cpu, dev->coupled_cpus) {
577 other_dev = per_cpu(cpuidle_devices, cpu);
578 if (other_dev && other_dev->coupled) {
579 coupled = other_dev->coupled;
580 goto have_coupled;
581 }
582 }
583
584 /* No existing coupled info found, create a new one */
585 coupled = kzalloc(sizeof(struct cpuidle_coupled), GFP_KERNEL);
586 if (!coupled)
587 return -ENOMEM;
588
589 coupled->coupled_cpus = dev->coupled_cpus;
590
591have_coupled:
592 dev->coupled = coupled;
593 if (WARN_ON(!cpumask_equal(&dev->coupled_cpus, &coupled->coupled_cpus)))
594 coupled->prevent++;
595
596 cpuidle_coupled_update_online_cpus(coupled);
597
598 coupled->refcnt++;
599
600 csd = &per_cpu(cpuidle_coupled_poke_cb, dev->cpu);
601 csd->func = cpuidle_coupled_poked;
602 csd->info = (void *)(unsigned long)dev->cpu;
603
604 return 0;
605}
606
607/**
608 * cpuidle_coupled_unregister_device - unregister a coupled cpuidle device
609 * @dev: struct cpuidle_device for the current cpu
610 *
611 * Called from cpuidle_unregister_device to tear down coupled idle. Removes the
612 * cpu from the coupled idle set, and frees the cpuidle_coupled_info struct if
613 * this was the last cpu in the set.
614 */
615void cpuidle_coupled_unregister_device(struct cpuidle_device *dev)
616{
617 struct cpuidle_coupled *coupled = dev->coupled;
618
619 if (cpumask_empty(&dev->coupled_cpus))
620 return;
621
622 if (--coupled->refcnt)
623 kfree(coupled);
624 dev->coupled = NULL;
625}
626
627/**
628 * cpuidle_coupled_prevent_idle - prevent cpus from entering a coupled state
629 * @coupled: the struct coupled that contains the cpu that is changing state
630 *
631 * Disables coupled cpuidle on a coupled set of cpus. Used to ensure that
632 * cpu_online_mask doesn't change while cpus are coordinating coupled idle.
633 */
634static void cpuidle_coupled_prevent_idle(struct cpuidle_coupled *coupled)
635{
636 int cpu = get_cpu();
637
638 /* Force all cpus out of the waiting loop. */
639 coupled->prevent++;
640 cpuidle_coupled_poke_others(cpu, coupled);
641 put_cpu();
642 while (!cpuidle_coupled_no_cpus_waiting(coupled))
643 cpu_relax();
644}
645
646/**
647 * cpuidle_coupled_allow_idle - allows cpus to enter a coupled state
648 * @coupled: the struct coupled that contains the cpu that is changing state
649 *
650 * Enables coupled cpuidle on a coupled set of cpus. Used to ensure that
651 * cpu_online_mask doesn't change while cpus are coordinating coupled idle.
652 */
653static void cpuidle_coupled_allow_idle(struct cpuidle_coupled *coupled)
654{
655 int cpu = get_cpu();
656
657 /*
658 * Write barrier ensures readers see the new online_count when they
659 * see prevent == 0.
660 */
661 smp_wmb();
662 coupled->prevent--;
663 /* Force cpus out of the prevent loop. */
664 cpuidle_coupled_poke_others(cpu, coupled);
665 put_cpu();
666}
667
668/**
669 * cpuidle_coupled_cpu_notify - notifier called during hotplug transitions
670 * @nb: notifier block
671 * @action: hotplug transition
672 * @hcpu: target cpu number
673 *
674 * Called when a cpu is brought on or offline using hotplug. Updates the
675 * coupled cpu set appropriately
676 */
677static int cpuidle_coupled_cpu_notify(struct notifier_block *nb,
678 unsigned long action, void *hcpu)
679{
680 int cpu = (unsigned long)hcpu;
681 struct cpuidle_device *dev;
682
Colin Cross63c6ba42012-08-15 22:10:50 +0200683 switch (action & ~CPU_TASKS_FROZEN) {
684 case CPU_UP_PREPARE:
685 case CPU_DOWN_PREPARE:
686 case CPU_ONLINE:
687 case CPU_DEAD:
688 case CPU_UP_CANCELED:
689 case CPU_DOWN_FAILED:
690 break;
691 default:
692 return NOTIFY_OK;
693 }
694
Colin Cross4126c012012-05-07 17:57:41 -0700695 mutex_lock(&cpuidle_lock);
696
697 dev = per_cpu(cpuidle_devices, cpu);
Jon Medhurst (Tixy)5fbbb902012-08-15 22:11:00 +0200698 if (!dev || !dev->coupled)
Colin Cross4126c012012-05-07 17:57:41 -0700699 goto out;
700
701 switch (action & ~CPU_TASKS_FROZEN) {
702 case CPU_UP_PREPARE:
703 case CPU_DOWN_PREPARE:
704 cpuidle_coupled_prevent_idle(dev->coupled);
705 break;
706 case CPU_ONLINE:
707 case CPU_DEAD:
708 cpuidle_coupled_update_online_cpus(dev->coupled);
709 /* Fall through */
710 case CPU_UP_CANCELED:
711 case CPU_DOWN_FAILED:
712 cpuidle_coupled_allow_idle(dev->coupled);
713 break;
714 }
715
716out:
717 mutex_unlock(&cpuidle_lock);
718 return NOTIFY_OK;
719}
720
721static struct notifier_block cpuidle_coupled_cpu_notifier = {
722 .notifier_call = cpuidle_coupled_cpu_notify,
723};
724
725static int __init cpuidle_coupled_init(void)
726{
727 return register_cpu_notifier(&cpuidle_coupled_cpu_notifier);
728}
729core_initcall(cpuidle_coupled_init);