smp.c 4.8 KB
Newer Older
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5
 * Licensed under the GPL
 */

6 7 8
#include <linux/percpu.h>
#include <asm/pgalloc.h>
#include <asm/tlb.h>
L
Linus Torvalds 已提交
9 10 11

#ifdef CONFIG_SMP

12 13 14 15 16 17 18 19 20 21 22 23
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/threads.h>
#include <linux/interrupt.h>
#include <linux/err.h>
#include <linux/hardirq.h>
#include <asm/smp.h>
#include <asm/processor.h>
#include <asm/spinlock.h>
#include <kern.h>
#include <irq_user.h>
#include <os.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

/* Per CPU bogomips and other parameters
 * The only piece used here is the ipi pipe, which is set before SMP is
 * started and never changed.
 */
struct cpuinfo_um cpu_data[NR_CPUS];

/* A statistic, can be a little off */
int num_reschedules_sent = 0;

/* Not changed after boot */
struct task_struct *idle_threads[NR_CPUS];

void smp_send_reschedule(int cpu)
{
39
	os_write_file(cpu_data[cpu].ipi_pipe[1], "R", 1);
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47
	num_reschedules_sent++;
}

void smp_send_stop(void)
{
	int i;

	printk(KERN_INFO "Stopping all CPUs...");
48
	for_each_online_cpu(i) {
J
Jeff Dike 已提交
49
		if (i == current_thread->cpu)
L
Linus Torvalds 已提交
50
			continue;
51
		os_write_file(cpu_data[i].ipi_pipe[1], "S", 1);
L
Linus Torvalds 已提交
52
	}
J
Jeff Dike 已提交
53
	printk(KERN_CONT "done\n");
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62 63
}

static cpumask_t smp_commenced_mask = CPU_MASK_NONE;
static cpumask_t cpu_callin_map = CPU_MASK_NONE;

static int idle_proc(void *cpup)
{
	int cpu = (int) cpup, err;

	err = os_pipe(cpu_data[cpu].ipi_pipe, 1, 1);
J
Jeff Dike 已提交
64
	if (err < 0)
L
Linus Torvalds 已提交
65 66
		panic("CPU#%d failed to create IPI pipe, err = %d", cpu, -err);

J
Jeff Dike 已提交
67
	os_set_fd_async(cpu_data[cpu].ipi_pipe[0]);
68

L
Linus Torvalds 已提交
69
	wmb();
70
	if (cpumask_test_and_set_cpu(cpu, &cpu_callin_map)) {
J
Jeff Dike 已提交
71
		printk(KERN_ERR "huh, CPU#%d already present??\n", cpu);
L
Linus Torvalds 已提交
72 73 74
		BUG();
	}

75
	while (!cpumask_test_cpu(cpu, &smp_commenced_mask))
L
Linus Torvalds 已提交
76 77
		cpu_relax();

78
	notify_cpu_starting(cpu);
79
	set_cpu_online(cpu, true);
L
Linus Torvalds 已提交
80
	default_idle();
81
	return 0;
L
Linus Torvalds 已提交
82 83 84 85 86 87
}

static struct task_struct *idle_thread(int cpu)
{
	struct task_struct *new_task;

88 89
	current->thread.request.u.thread.proc = idle_proc;
	current->thread.request.u.thread.arg = (void *) cpu;
L
Linus Torvalds 已提交
90
	new_task = fork_idle(cpu);
J
Jeff Dike 已提交
91
	if (IS_ERR(new_task))
L
Linus Torvalds 已提交
92 93 94
		panic("copy_process failed in idle_thread, error = %ld",
		      PTR_ERR(new_task));

95
	cpu_tasks[cpu] = ((struct cpu_task)
L
Linus Torvalds 已提交
96 97 98
		          { .pid = 	new_task->thread.mode.tt.extern_pid,
			    .task = 	new_task } );
	idle_threads[cpu] = new_task;
J
Jeff Dike 已提交
99
	panic("skas mode doesn't support SMP");
100
	return new_task;
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110
}

void smp_prepare_cpus(unsigned int maxcpus)
{
	struct task_struct *idle;
	unsigned long waittime;
	int err, cpu, me = smp_processor_id();
	int i;

	for (i = 0; i < ncpus; ++i)
111
		set_cpu_possible(i, true);
L
Linus Torvalds 已提交
112

113
	set_cpu_online(me, true);
114
	cpumask_set_cpu(me, &cpu_callin_map);
L
Linus Torvalds 已提交
115 116

	err = os_pipe(cpu_data[me].ipi_pipe, 1, 1);
J
Jeff Dike 已提交
117
	if (err < 0)
L
Linus Torvalds 已提交
118 119
		panic("CPU#0 failed to create IPI pipe, errno = %d", -err);

J
Jeff Dike 已提交
120
	os_set_fd_async(cpu_data[me].ipi_pipe[0]);
L
Linus Torvalds 已提交
121

J
Jeff Dike 已提交
122 123
	for (cpu = 1; cpu < ncpus; cpu++) {
		printk(KERN_INFO "Booting processor %d...\n", cpu);
124

L
Linus Torvalds 已提交
125 126 127 128 129
		idle = idle_thread(cpu);

		init_idle(idle, cpu);

		waittime = 200000000;
130
		while (waittime-- && !cpumask_test_cpu(cpu, &cpu_callin_map))
L
Linus Torvalds 已提交
131 132
			cpu_relax();

J
Jeff Dike 已提交
133
		printk(KERN_INFO "%s\n",
134
		       cpumask_test_cpu(cpu, &cpu_calling_map) ? "done" : "failed");
L
Linus Torvalds 已提交
135 136 137 138 139
	}
}

void smp_prepare_boot_cpu(void)
{
140
	set_cpu_online(smp_processor_id(), true);
L
Linus Torvalds 已提交
141 142
}

143
int __cpu_up(unsigned int cpu, struct task_struct *tidle)
L
Linus Torvalds 已提交
144
{
145
	cpumask_set_cpu(cpu, &smp_commenced_mask);
146
	while (!cpu_online(cpu))
L
Linus Torvalds 已提交
147
		mb();
148
	return 0;
L
Linus Torvalds 已提交
149 150 151 152 153
}

int setup_profiling_timer(unsigned int multiplier)
{
	printk(KERN_INFO "setup_profiling_timer\n");
154
	return 0;
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164
}

void smp_call_function_slave(int cpu);

void IPI_handler(int cpu)
{
	unsigned char c;
	int fd;

	fd = cpu_data[cpu].ipi_pipe[0];
165
	while (os_read_file(fd, &c, 1) == 1) {
L
Linus Torvalds 已提交
166 167 168 169 170 171
		switch (c) {
		case 'C':
			smp_call_function_slave(cpu);
			break;

		case 'R':
172
			scheduler_ipi();
L
Linus Torvalds 已提交
173 174 175
			break;

		case 'S':
J
Jeff Dike 已提交
176 177
			printk(KERN_INFO "CPU#%d stopping\n", cpu);
			while (1)
L
Linus Torvalds 已提交
178 179 180 181
				pause();
			break;

		default:
J
Jeff Dike 已提交
182 183
			printk(KERN_ERR "CPU#%d received unknown IPI [%c]!\n",
			       cpu, c);
L
Linus Torvalds 已提交
184 185 186 187 188 189 190
			break;
		}
	}
}

int hard_smp_processor_id(void)
{
191
	return pid_to_processor_id(os_getpid());
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

static DEFINE_SPINLOCK(call_lock);
static atomic_t scf_started;
static atomic_t scf_finished;
static void (*func)(void *info);
static void *info;

void smp_call_function_slave(int cpu)
{
	atomic_inc(&scf_started);
	(*func)(info);
	atomic_inc(&scf_finished);
}

207
int smp_call_function(void (*_func)(void *info), void *_info, int wait)
L
Linus Torvalds 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
	int cpus = num_online_cpus() - 1;
	int i;

	if (!cpus)
		return 0;

	/* Can deadlock when called with interrupts disabled */
	WARN_ON(irqs_disabled());

	spin_lock_bh(&call_lock);
	atomic_set(&scf_started, 0);
	atomic_set(&scf_finished, 0);
	func = _func;
	info = _info;

	for_each_online_cpu(i)
225
		os_write_file(cpu_data[i].ipi_pipe[1], "C", 1);
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238

	while (atomic_read(&scf_started) != cpus)
		barrier();

	if (wait)
		while (atomic_read(&scf_finished) != cpus)
			barrier();

	spin_unlock_bh(&call_lock);
	return 0;
}

#endif