summaryrefslogtreecommitdiffstats
path: root/kernel/power/smp.c
blob: cba3584b80fefa88b3fdcb8d938800b08d44abeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * drivers/power/smp.c - Functions for stopping other CPUs.
 *
 * Copyright 2004 Pavel Machek <pavel@suse.cz>
 * Copyright (C) 2002-2003 Nigel Cunningham <ncunningham@clear.net.nz>
 *
 * This file is released under the GPLv2.
 */

#undef DEBUG

#include <linux/smp_lock.h>
#include <linux/interrupt.h>
#include <linux/suspend.h>
#include <linux/module.h>
#include <asm/atomic.h>
#include <asm/tlbflush.h>

static atomic_t cpu_counter, freeze;


static void smp_pause(void * data)
{
	struct saved_context ctxt;
	__save_processor_state(&ctxt);
	printk("Sleeping in:\n");
	dump_stack();
	atomic_inc(&cpu_counter);
	while (atomic_read(&freeze)) {
		/* FIXME: restore takes place at random piece inside this.
		   This should probably be written in assembly, and
		   preserve general-purpose registers, too

		   What about stack? We may need to move to new stack here.

		   This should better be ran with interrupts disabled.
		 */
		cpu_relax();
		barrier();
	}
	atomic_dec(&cpu_counter);
	__restore_processor_state(&ctxt);
}

static cpumask_t oldmask;

void disable_nonboot_cpus(void)
{
	oldmask = current->cpus_allowed;
	set_cpus_allowed(current, cpumask_of_cpu(0));
	printk("Freezing CPUs (at %d)", _smp_processor_id());
	current->state = TASK_INTERRUPTIBLE;
	schedule_timeout(HZ);
	printk("...");
	BUG_ON(_smp_processor_id() != 0);

	/* FIXME: for this to work, all the CPUs must be running
	 * "idle" thread (or we deadlock). Is that guaranteed? */

	atomic_set(&cpu_counter, 0);
	atomic_set(&freeze, 1);
	smp_call_function(smp_pause, NULL, 0, 0);
	while (atomic_read(&cpu_counter) < (num_online_cpus() - 1)) {
		cpu_relax();
		barrier();
	}
	printk("ok\n");
}

void enable_nonboot_cpus(void)
{
	printk("Restarting CPUs");
	atomic_set(&freeze, 0);
	while (atomic_read(&cpu_counter)) {
		cpu_relax();
		barrier();
	}
	printk("...");
	set_cpus_allowed(current, oldmask);
	schedule();
	printk("ok\n");

}