smp.c 6.0 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * SMP support for pSeries machines.
L
Linus Torvalds 已提交
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
 *
 * Dave Engebretsen, Peter Bergner, and
 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
 *
 * Plus various changes from other IBM teams...
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

#undef DEBUG

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/smp.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/cache.h>
#include <linux/err.h>
#include <linux/sysdev.h>
#include <linux/cpu.h>

#include <asm/ptrace.h>
#include <asm/atomic.h>
#include <asm/irq.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/smp.h>
#include <asm/paca.h>
#include <asm/time.h>
#include <asm/machdep.h>
41
#include "xics.h"
L
Linus Torvalds 已提交
42
#include <asm/cputable.h>
43
#include <asm/firmware.h>
L
Linus Torvalds 已提交
44 45 46
#include <asm/system.h>
#include <asm/rtas.h>
#include <asm/pSeries_reconfig.h>
47
#include <asm/mpic.h>
48
#include <asm/vdso_datapage.h>
L
Linus Torvalds 已提交
49

50
#include "plpar_wrappers.h"
51
#include "pseries.h"
52

L
Linus Torvalds 已提交
53
#ifdef DEBUG
54
#include <asm/udbg.h>
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64 65
#define DBG(fmt...) udbg_printf(fmt)
#else
#define DBG(fmt...)
#endif

/*
 * The primary thread of each non-boot processor is recorded here before
 * smp init.
 */
static cpumask_t of_spin_map;

O
Olof Johansson 已提交
66
extern void generic_secondary_smp_init(unsigned long);
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

/**
 * smp_startup_cpu() - start the given cpu
 *
 * At boot time, there is nothing to do for primary threads which were
 * started from Open Firmware.  For anything else, call RTAS with the
 * appropriate start location.
 *
 * Returns:
 *	0	- failure
 *	1	- success
 */
static inline int __devinit smp_startup_cpu(unsigned int lcpu)
{
	int status;
	unsigned long start_here = __pa((u32)*((unsigned long *)
O
Olof Johansson 已提交
83
					       generic_secondary_smp_init));
L
Linus Torvalds 已提交
84
	unsigned int pcpu;
85
	int start_cpu;
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93

	if (cpu_isset(lcpu, of_spin_map))
		/* Already started by OF and sitting in spin loop */
		return 1;

	pcpu = get_hard_smp_processor_id(lcpu);

	/* Fixup atomic count: it exited inside IRQ handler. */
A
Al Viro 已提交
94
	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;
L
Linus Torvalds 已提交
95

96 97 98 99 100 101 102 103
	/* 
	 * If the RTAS start-cpu token does not exist then presume the
	 * cpu is already spinning.
	 */
	start_cpu = rtas_token("start-cpu");
	if (start_cpu == RTAS_UNKNOWN_SERVICE)
		return 1;

104
	status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
L
Linus Torvalds 已提交
105 106 107 108
	if (status != 0) {
		printk(KERN_ERR "start-cpu failed: %i\n", status);
		return 0;
	}
109

L
Linus Torvalds 已提交
110 111 112
	return 1;
}

113
#ifdef CONFIG_XICS
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static inline void smp_xics_do_message(int cpu, int msg)
{
	set_bit(msg, &xics_ipi_message[cpu].value);
	mb();
	xics_cause_IPI(cpu);
}

static void smp_xics_message_pass(int target, int msg)
{
	unsigned int i;

	if (target < NR_CPUS) {
		smp_xics_do_message(target, msg);
	} else {
		for_each_online_cpu(i) {
			if (target == MSG_ALL_BUT_SELF
			    && i == smp_processor_id())
				continue;
			smp_xics_do_message(i, msg);
		}
	}
}

static int __init smp_xics_probe(void)
{
	xics_request_IPIs();

	return cpus_weight(cpu_possible_map);
}

static void __devinit smp_xics_setup_cpu(int cpu)
{
	if (cpu != boot_cpuid)
		xics_setup_cpu();

149
	if (firmware_has_feature(FW_FEATURE_SPLPAR))
L
Linus Torvalds 已提交
150 151 152 153 154
		vpa_init(cpu);

	cpu_clear(cpu, of_spin_map);

}
155
#endif /* CONFIG_XICS */
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

static DEFINE_SPINLOCK(timebase_lock);
static unsigned long timebase = 0;

static void __devinit pSeries_give_timebase(void)
{
	spin_lock(&timebase_lock);
	rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
	timebase = get_tb();
	spin_unlock(&timebase_lock);

	while (timebase)
		barrier();
	rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
}

static void __devinit pSeries_take_timebase(void)
{
	while (!timebase)
		barrier();
	spin_lock(&timebase_lock);
	set_tb(timebase >> 32, timebase & 0xffffffff);
	timebase = 0;
	spin_unlock(&timebase_lock);
}

static void __devinit smp_pSeries_kick_cpu(int nr)
{
	BUG_ON(nr < 0 || nr >= NR_CPUS);

	if (!smp_startup_cpu(nr))
		return;

	/*
	 * The processor is currently spinning, waiting for the
	 * cpu_start field to become non-zero After we set cpu_start,
	 * the processor will continue on to secondary_start
	 */
	paca[nr].cpu_start = 1;
}

static int smp_pSeries_cpu_bootable(unsigned int nr)
{
	/* Special case - we inhibit secondary thread startup
	 * during boot if the user requests it.  Odd-numbered
	 * cpus are assumed to be secondary threads.
	 */
	if (system_state < SYSTEM_RUNNING &&
204
	    cpu_has_feature(CPU_FTR_SMT) &&
L
Linus Torvalds 已提交
205 206 207 208 209
	    !smt_enabled_at_boot && nr % 2 != 0)
		return 0;

	return 1;
}
210
#ifdef CONFIG_MPIC
L
Linus Torvalds 已提交
211 212 213 214 215 216
static struct smp_ops_t pSeries_mpic_smp_ops = {
	.message_pass	= smp_mpic_message_pass,
	.probe		= smp_mpic_probe,
	.kick_cpu	= smp_pSeries_kick_cpu,
	.setup_cpu	= smp_mpic_setup_cpu,
};
217 218
#endif
#ifdef CONFIG_XICS
L
Linus Torvalds 已提交
219 220 221 222 223 224 225
static struct smp_ops_t pSeries_xics_smp_ops = {
	.message_pass	= smp_xics_message_pass,
	.probe		= smp_xics_probe,
	.kick_cpu	= smp_pSeries_kick_cpu,
	.setup_cpu	= smp_xics_setup_cpu,
	.cpu_bootable	= smp_pSeries_cpu_bootable,
};
226
#endif
L
Linus Torvalds 已提交
227 228

/* This is called very early */
229
static void __init smp_init_pseries(void)
L
Linus Torvalds 已提交
230 231 232 233 234 235
{
	int i;

	DBG(" -> smp_init_pSeries()\n");

	/* Mark threads which are still spinning in hold loops. */
236 237
	if (cpu_has_feature(CPU_FTR_SMT)) {
		for_each_present_cpu(i) { 
L
Linus Torvalds 已提交
238 239 240 241 242 243 244
			if (i % 2 == 0)
				/*
				 * Even-numbered logical cpus correspond to
				 * primary threads.
				 */
				cpu_set(i, of_spin_map);
		}
245
	} else {
L
Linus Torvalds 已提交
246
		of_spin_map = cpu_present_map;
247
	}
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259

	cpu_clear(boot_cpuid, of_spin_map);

	/* Non-lpar has additional take/give timebase */
	if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
		smp_ops->give_timebase = pSeries_give_timebase;
		smp_ops->take_timebase = pSeries_take_timebase;
	}

	DBG(" <- smp_init_pSeries()\n");
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
#ifdef CONFIG_MPIC
void __init smp_init_pseries_mpic(void)
{
	smp_ops = &pSeries_mpic_smp_ops;

	smp_init_pseries();
}
#endif

void __init smp_init_pseries_xics(void)
{
	smp_ops = &pSeries_xics_smp_ops;

	smp_init_pseries();
}