1
/******************************************************************************
2
* common/stop_machine.c
4
* Facilities to put whole machine in a safe 'stop' state
6
* Copyright 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation
7
* Copyright 2008 Kevin Tian <kevin.tian@intel.com>, Intel Corporation.
9
* This program is free software; you can redistribute it and/or modify it
10
* under the terms and conditions of the GNU General Public License,
11
* version 2, as published by the Free Software Foundation.
13
* This program is distributed in the hope 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
18
* You should have received a copy of the GNU General Public License along with
19
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20
* Place - Suite 330, Boston, MA 02111-1307 USA.
23
#include <xen/config.h>
25
#include <xen/sched.h>
26
#include <xen/spinlock.h>
27
#include <xen/softirq.h>
28
#include <xen/stop_machine.h>
29
#include <xen/errno.h>
31
#include <asm/current.h>
32
#include <asm/processor.h>
34
enum stopmachine_state {
37
STOPMACHINE_DISABLE_IRQ,
42
struct stopmachine_data {
45
enum stopmachine_state state;
54
static struct stopmachine_data stopmachine_data;
55
static DEFINE_SPINLOCK(stopmachine_lock);
57
static void stopmachine_set_state(enum stopmachine_state state)
59
atomic_set(&stopmachine_data.done, 0);
61
stopmachine_data.state = state;
62
while ( atomic_read(&stopmachine_data.done) != stopmachine_data.nr_cpus )
66
int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
69
unsigned int i, nr_cpus;
72
BUG_ON(!local_irq_is_enabled());
74
allbutself = cpu_online_map;
75
cpu_clear(smp_processor_id(), allbutself);
76
nr_cpus = cpus_weight(allbutself);
80
BUG_ON(cpu != smp_processor_id());
84
/* Note: We shouldn't spin on lock when it's held by others since others
85
* is expecting this cpus to enter softirq context. Or else deadlock
88
if ( !spin_trylock(&stopmachine_lock) )
91
stopmachine_data.fn = fn;
92
stopmachine_data.fn_data = data;
93
stopmachine_data.nr_cpus = nr_cpus;
94
stopmachine_data.fn_cpu = cpu;
95
atomic_set(&stopmachine_data.done, 0);
96
stopmachine_data.state = STOPMACHINE_START;
100
for_each_cpu_mask ( i, allbutself )
101
cpu_raise_softirq(i, STOPMACHINE_SOFTIRQ);
103
stopmachine_set_state(STOPMACHINE_PREPARE);
106
stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
108
if ( cpu == smp_processor_id() )
109
stopmachine_data.fn_result = (*fn)(data);
110
stopmachine_set_state(STOPMACHINE_INVOKE);
111
ret = stopmachine_data.fn_result;
113
stopmachine_set_state(STOPMACHINE_EXIT);
116
spin_unlock(&stopmachine_lock);
121
static void stopmachine_softirq(void)
123
enum stopmachine_state state = STOPMACHINE_START;
127
while ( state != STOPMACHINE_EXIT )
129
while ( stopmachine_data.state == state )
132
state = stopmachine_data.state;
135
case STOPMACHINE_DISABLE_IRQ:
138
case STOPMACHINE_INVOKE:
139
if ( stopmachine_data.fn_cpu == smp_processor_id() )
140
stopmachine_data.fn_result =
141
stopmachine_data.fn(stopmachine_data.fn_data);
148
atomic_inc(&stopmachine_data.done);
154
static int __init cpu_stopmachine_init(void)
156
open_softirq(STOPMACHINE_SOFTIRQ, stopmachine_softirq);
159
__initcall(cpu_stopmachine_init);