smp.c 9.93 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Smp support for ppc.
 *
 * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
 * deal of code from the sparc and intel versions.
 *
 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
 *
 */

#include <linux/config.h>
#include <linux/kernel.h>
13
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
14 15 16 17 18 19 20 21
#include <linux/sched.h>
#include <linux/smp.h>
#include <linux/smp_lock.h>
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/spinlock.h>
Linus Torvalds's avatar
Linus Torvalds committed
22
#include <linux/cache.h>
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26 27 28 29 30 31 32

#include <asm/ptrace.h>
#include <asm/atomic.h>
#include <asm/irq.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/hardirq.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/smp.h>
Linus Torvalds's avatar
Linus Torvalds committed
33
#include <asm/residual.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
#include <asm/time.h>
35
#include <asm/thread_info.h>
36
#include <asm/tlbflush.h>
37
#include <asm/xmon.h>
Linus Torvalds's avatar
Linus Torvalds committed
38

Linus Torvalds's avatar
Linus Torvalds committed
39 40
int smp_threads_ready;
volatile int smp_commenced;
Linus Torvalds's avatar
Linus Torvalds committed
41
int smp_tb_synchronized;
Linus Torvalds's avatar
Linus Torvalds committed
42 43
struct cpuinfo_PPC cpu_data[NR_CPUS];
struct klock_info_struct klock_info = { KLOCK_CLEAR, 0 };
Linus Torvalds's avatar
Linus Torvalds committed
44 45
atomic_t ipi_recv;
atomic_t ipi_sent;
46 47
DEFINE_PER_CPU(unsigned int, prof_multiplier);
DEFINE_PER_CPU(unsigned int, prof_counter);
48
unsigned long cache_decay_ticks = HZ/100;
49 50
cpumask_t cpu_online_map;
cpumask_t cpu_possible_map;
Linus Torvalds's avatar
Linus Torvalds committed
51
int smp_hw_index[NR_CPUS];
52
struct thread_info *secondary_ti;
Linus Torvalds's avatar
Linus Torvalds committed
53

54
EXPORT_SYMBOL(cpu_online_map);
55
EXPORT_SYMBOL(cpu_possible_map);
56

57 58 59
/* SMP operations for this machine */
static struct smp_ops_t *smp_ops;

Linus Torvalds's avatar
Linus Torvalds committed
60 61 62 63 64 65
/* all cpu mappings are 1-1 -- Cort */
volatile unsigned long cpu_callin_map[NR_CPUS];

int start_secondary(void *);
extern int cpu_idle(void *unused);
void smp_call_function_interrupt(void);
66 67
static int __smp_call_function(void (*func) (void *info), void *info,
			       int wait, int target);
Linus Torvalds's avatar
Linus Torvalds committed
68

69 70 71
/* Low level assembly function used to backup CPU 0 state */
extern void __save_cpu_setup(void);

Linus Torvalds's avatar
Linus Torvalds committed
72
/* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
73
 *
Linus Torvalds's avatar
Linus Torvalds committed
74 75
 * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
 * in /proc/interrupts will be wrong!!! --Troy */
Linus Torvalds's avatar
Linus Torvalds committed
76 77 78 79 80
#define PPC_MSG_CALL_FUNCTION	0
#define PPC_MSG_RESCHEDULE	1
#define PPC_MSG_INVALIDATE_TLB	2
#define PPC_MSG_XMON_BREAK	3

81 82 83 84 85 86 87 88
static inline void
smp_message_pass(int target, int msg, unsigned long data, int wait)
{
	if (smp_ops){
		atomic_inc(&ipi_sent);
		smp_ops->message_pass(target,msg,data,wait);
	}
}
Linus Torvalds's avatar
Linus Torvalds committed
89

90
/*
Linus Torvalds's avatar
Linus Torvalds committed
91 92
 * Common functions
 */
Linus Torvalds's avatar
Linus Torvalds committed
93 94 95 96
void smp_local_timer_interrupt(struct pt_regs * regs)
{
	int cpu = smp_processor_id();

97
	if (!--per_cpu(prof_counter, cpu)) {
Linus Torvalds's avatar
Linus Torvalds committed
98
		update_process_times(user_mode(regs));
99
		per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
Linus Torvalds's avatar
Linus Torvalds committed
100 101 102 103 104
	}
}

void smp_message_recv(int msg, struct pt_regs *regs)
{
Linus Torvalds's avatar
Linus Torvalds committed
105
	atomic_inc(&ipi_recv);
106

Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110
	switch( msg ) {
	case PPC_MSG_CALL_FUNCTION:
		smp_call_function_interrupt();
		break;
111
	case PPC_MSG_RESCHEDULE:
112
		set_need_resched();
Linus Torvalds's avatar
Linus Torvalds committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		break;
	case PPC_MSG_INVALIDATE_TLB:
		_tlbia();
		break;
#ifdef CONFIG_XMON
	case PPC_MSG_XMON_BREAK:
		xmon(regs);
		break;
#endif /* CONFIG_XMON */
	default:
		printk("SMP %d: smp_message_recv(): unknown msg %d\n",
		       smp_processor_id(), msg);
		break;
	}
}

/*
 * 750's don't broadcast tlb invalidates so
 * we have to emulate that behavior.
 *   -- Cort
 */
void smp_send_tlb_invalidate(int cpu)
{
Linus Torvalds's avatar
Linus Torvalds committed
136
	if ( PVR_VER(mfspr(PVR)) == 8 )
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB, 0, 0);
}

void smp_send_reschedule(int cpu)
{
	/*
	 * This is only used if `cpu' is running an idle task,
	 * so it will reschedule itself anyway...
	 *
	 * This isn't the case anymore since the other CPU could be
	 * sleeping and won't reschedule until the next interrupt (such
	 * as the timer).
	 *  -- Cort
	 */
	/* This is only used if `cpu' is running an idle task,
	   so it will reschedule itself anyway... */
	smp_message_pass(cpu, PPC_MSG_RESCHEDULE, 0, 0);
}

#ifdef CONFIG_XMON
void smp_send_xmon_break(int cpu)
{
	smp_message_pass(cpu, PPC_MSG_XMON_BREAK, 0, 0);
}
#endif /* CONFIG_XMON */

static void stop_this_cpu(void *dummy)
{
165
	local_irq_disable();
Linus Torvalds's avatar
Linus Torvalds committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	while (1)
		;
}

void smp_send_stop(void)
{
	smp_call_function(stop_this_cpu, NULL, 1, 0);
}

/*
 * Structure and data for smp_call_function(). This is designed to minimise
 * static memory requirements. It also looks cleaner.
 * Stolen from the i386 version.
 */
static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;

Linus Torvalds's avatar
Linus Torvalds committed
182
static struct call_data_struct {
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186 187 188 189 190 191 192 193 194
	void (*func) (void *info);
	void *info;
	atomic_t started;
	atomic_t finished;
	int wait;
} *call_data;

/*
 * this function sends a 'generic call function' IPI to all other CPUs
 * in the system.
 */

195 196
int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
		      int wait)
Linus Torvalds's avatar
Linus Torvalds committed
197 198 199 200 201 202 203 204 205 206
/*
 * [SUMMARY] Run a function on all other CPUs.
 * <func> The function to run. This must be fast and non-blocking.
 * <info> An arbitrary pointer to pass to the function.
 * <nonatomic> currently unused.
 * <wait> If true, wait (atomically) until function has completed on other CPUs.
 * [RETURNS] 0 on success, else a negative status code. Does not return until
 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
 *
 * You must not call this function with disabled interrupts or from a
207
 * hardware interrupt handler or from a bottom half handler.
Linus Torvalds's avatar
Linus Torvalds committed
208
 */
209
{
210 211 212
	/* FIXME: get cpu lock with hotplug cpus, or change this to
           bitmask. --RR */
	if (num_online_cpus() <= 1)
213 214 215 216 217 218
		return 0;
	return __smp_call_function(func, info, wait, MSG_ALL_BUT_SELF);
}

static int __smp_call_function(void (*func) (void *info), void *info,
			       int wait, int target)
Linus Torvalds's avatar
Linus Torvalds committed
219 220
{
	struct call_data_struct data;
221
	int ret = -1;
Linus Torvalds's avatar
Linus Torvalds committed
222
	int timeout;
223
	int ncpus = 1;
Linus Torvalds's avatar
Linus Torvalds committed
224

225
	if (target == MSG_ALL_BUT_SELF)
226
		ncpus = num_online_cpus() - 1;
227
	else if (target == MSG_ALL)
228
		ncpus = num_online_cpus();
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231 232 233 234 235 236

	data.func = func;
	data.info = info;
	atomic_set(&data.started, 0);
	data.wait = wait;
	if (wait)
		atomic_set(&data.finished, 0);

237
	spin_lock(&call_lock);
Linus Torvalds's avatar
Linus Torvalds committed
238 239
	call_data = &data;
	/* Send a message to all other CPUs and wait for them to respond */
240
	smp_message_pass(target, PPC_MSG_CALL_FUNCTION, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
241 242 243

	/* Wait for response */
	timeout = 1000000;
244
	while (atomic_read(&data.started) != ncpus) {
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248 249 250 251 252 253 254 255
		if (--timeout == 0) {
			printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
			       smp_processor_id(), atomic_read(&data.started));
			goto out;
		}
		barrier();
		udelay(1);
	}

	if (wait) {
		timeout = 1000000;
256
		while (atomic_read(&data.finished) != ncpus) {
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260 261 262 263 264 265 266 267 268
			if (--timeout == 0) {
				printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
				       smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started));
				goto out;
			}
			barrier();
			udelay(1);
		}
	}
	ret = 0;

 out:
269
	spin_unlock(&call_lock);
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	return ret;
}

void smp_call_function_interrupt(void)
{
	void (*func) (void *info) = call_data->func;
	void *info = call_data->info;
	int wait = call_data->wait;

	/*
	 * Notify initiating CPU that I've grabbed the data and am
	 * about to execute the function
	 */
	atomic_inc(&call_data->started);
	/*
	 * At this point the info structure may be out of scope unless wait==1
	 */
	(*func)(info);
	if (wait)
		atomic_inc(&call_data->finished);
}

292 293 294 295 296 297 298
static void __devinit smp_store_cpu_info(int id)
{
        struct cpuinfo_PPC *c = &cpu_data[id];

	/* assume bogomips are same for everything */
        c->loops_per_jiffy = loops_per_jiffy;
        c->pvr = mfspr(PVR);
299 300
	per_cpu(prof_counter, id) = 1;
	per_cpu(prof_multiplier, id) = 1;
301 302 303 304
}

void __init smp_prepare_cpus(unsigned int max_cpus)
{
305
	int num_cpus, i;
306 307 308 309 310 311 312 313 314 315 316 317 318

	/* Fixup boot cpu */
        smp_store_cpu_info(smp_processor_id());
	cpu_callin_map[smp_processor_id()] = 1;

	smp_ops = ppc_md.smp_ops;
	if (smp_ops == NULL) {
		printk("SMP not supported on this machine.\n");
		return;
	}

	/* Probe platform for CPUs: always linear. */
	num_cpus = smp_ops->probe();
319 320
	for (i = 0; i < num_cpus; ++i)
		cpu_set(i, cpu_possible_map);
321

322 323 324
	/* Backup CPU 0 state */
	__save_cpu_setup();

325 326 327 328
	if (smp_ops->space_timers)
		smp_ops->space_timers(num_cpus);
}

329 330
void __devinit smp_prepare_boot_cpu(void)
{
331 332
	cpu_set(smp_processor_id(), cpu_online_map);
	cpu_set(smp_processor_id(), cpu_possible_map);
333 334
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
int __init setup_profiling_timer(unsigned int multiplier)
{
	return 0;
}

/* Processor coming up starts here */
int __devinit start_secondary(void *unused)
{
	int cpu;

	atomic_inc(&init_mm.mm_count);
	current->active_mm = &init_mm;

	cpu = smp_processor_id();
        smp_store_cpu_info(cpu);
	set_dec(tb_ticks_per_jiffy);
	cpu_callin_map[cpu] = 1;

	printk("CPU %i done callin...\n", cpu);
	smp_ops->setup_cpu(cpu);
	printk("CPU %i done setup...\n", cpu);
356
	local_irq_enable();
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	smp_ops->take_timebase();
	printk("CPU %i done timebase take...\n", cpu);

	return cpu_idle(NULL);
}

int __cpu_up(unsigned int cpu)
{
	struct pt_regs regs;
	struct task_struct *p;
	char buf[32];
	int c;

	/* create a process for the processor */
	/* only regs.msr is actually used, and 0 is OK for it */
	memset(&regs, 0, sizeof(struct pt_regs));
373
	p = copy_process(CLONE_VM|CLONE_IDLETASK, 0, &regs, 0, NULL, NULL);
374 375
	if (IS_ERR(p))
		panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
376
	wake_up_forked_process(p);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

	init_idle(p, cpu);
	unhash_process(p);

	secondary_ti = p->thread_info;
	p->thread_info->cpu = cpu;

	/*
	 * There was a cache flush loop here to flush the cache
	 * to memory for the first 8MB of RAM.  The cache flush
	 * has been pushed into the kick_cpu function for those
	 * platforms that need it.
	 */

	/* wake up cpu */
	smp_ops->kick_cpu(cpu);
393
	
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	/*
	 * wait to see if the cpu made a callin (is actually up).
	 * use this value that I found through experimentation.
	 * -- Cort
	 */
	for (c = 1000; c && !cpu_callin_map[cpu]; c--)
		udelay(100);

	if (!cpu_callin_map[cpu]) {
		sprintf(buf, "didn't find cpu %u", cpu);
		if (ppc_md.progress) ppc_md.progress(buf, 0x360+cpu);
		printk("Processor %u is stuck.\n", cpu);
		return -ENOENT;
	}

	sprintf(buf, "found cpu %u", cpu);
	if (ppc_md.progress) ppc_md.progress(buf, 0x350+cpu);
	printk("Processor %d found.\n", cpu);

	smp_ops->give_timebase();
414
	cpu_set(cpu, cpu_online_map);
415 416 417 418 419 420 421
	return 0;
}

void smp_cpus_done(unsigned int max_cpus)
{
	smp_ops->setup_cpu(0);
}