Linux-2.6.12-rc2
[linux-3.10.git] / kernel / power / smp.c
1 /*
2  * drivers/power/smp.c - Functions for stopping other CPUs.
3  *
4  * Copyright 2004 Pavel Machek <pavel@suse.cz>
5  * Copyright (C) 2002-2003 Nigel Cunningham <ncunningham@clear.net.nz>
6  *
7  * This file is released under the GPLv2.
8  */
9
10 #undef DEBUG
11
12 #include <linux/smp_lock.h>
13 #include <linux/interrupt.h>
14 #include <linux/suspend.h>
15 #include <linux/module.h>
16 #include <asm/atomic.h>
17 #include <asm/tlbflush.h>
18
19 static atomic_t cpu_counter, freeze;
20
21
22 static void smp_pause(void * data)
23 {
24         struct saved_context ctxt;
25         __save_processor_state(&ctxt);
26         printk("Sleeping in:\n");
27         dump_stack();
28         atomic_inc(&cpu_counter);
29         while (atomic_read(&freeze)) {
30                 /* FIXME: restore takes place at random piece inside this.
31                    This should probably be written in assembly, and
32                    preserve general-purpose registers, too
33
34                    What about stack? We may need to move to new stack here.
35
36                    This should better be ran with interrupts disabled.
37                  */
38                 cpu_relax();
39                 barrier();
40         }
41         atomic_dec(&cpu_counter);
42         __restore_processor_state(&ctxt);
43 }
44
45 static cpumask_t oldmask;
46
47 void disable_nonboot_cpus(void)
48 {
49         printk("Freezing CPUs (at %d)", smp_processor_id());
50         oldmask = current->cpus_allowed;
51         set_cpus_allowed(current, cpumask_of_cpu(0));
52         current->state = TASK_INTERRUPTIBLE;
53         schedule_timeout(HZ);
54         printk("...");
55         BUG_ON(smp_processor_id() != 0);
56
57         /* FIXME: for this to work, all the CPUs must be running
58          * "idle" thread (or we deadlock). Is that guaranteed? */
59
60         atomic_set(&cpu_counter, 0);
61         atomic_set(&freeze, 1);
62         smp_call_function(smp_pause, NULL, 0, 0);
63         while (atomic_read(&cpu_counter) < (num_online_cpus() - 1)) {
64                 cpu_relax();
65                 barrier();
66         }
67         printk("ok\n");
68 }
69
70 void enable_nonboot_cpus(void)
71 {
72         printk("Restarting CPUs");
73         atomic_set(&freeze, 0);
74         while (atomic_read(&cpu_counter)) {
75                 cpu_relax();
76                 barrier();
77         }
78         printk("...");
79         set_cpus_allowed(current, oldmask);
80         schedule();
81         printk("ok\n");
82
83 }
84
85