smp.c 6.3 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
 *
 * 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.
 */


#include <linux/kernel.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>
A
Arun Sharma 已提交
29
#include <linux/atomic.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38
#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/machdep.h>
#include <asm/cputable.h>
39
#include <asm/firmware.h>
L
Linus Torvalds 已提交
40 41 42
#include <asm/system.h>
#include <asm/rtas.h>
#include <asm/pSeries_reconfig.h>
43
#include <asm/mpic.h>
44
#include <asm/vdso_datapage.h>
45
#include <asm/cputhreads.h>
46
#include <asm/xics.h>
L
Linus Torvalds 已提交
47

48
#include "plpar_wrappers.h"
49
#include "pseries.h"
50
#include "offline_states.h"
51

L
Linus Torvalds 已提交
52 53

/*
54 55
 * The Primary thread of each non-boot processor was started from the OF client
 * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
L
Linus Torvalds 已提交
56
 */
57
static cpumask_var_t of_spin_mask;
L
Linus Torvalds 已提交
58

59 60 61 62 63 64 65
/* Query where a cpu is now.  Return codes #defined in plpar_wrappers.h */
int smp_query_cpu_stopped(unsigned int pcpu)
{
	int cpu_status, status;
	int qcss_tok = rtas_token("query-cpu-stopped-state");

	if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
66 67
		printk_once(KERN_INFO
			"Firmware doesn't support query-cpu-stopped-state\n");
68 69 70 71 72 73 74 75 76 77 78 79 80
		return QCSS_HARDWARE_ERROR;
	}

	status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
	if (status != 0) {
		printk(KERN_ERR
		       "RTAS query-cpu-stopped-state failed: %i\n", status);
		return status;
	}

	return cpu_status;
}

L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/**
 * 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 已提交
96
					       generic_secondary_smp_init));
L
Linus Torvalds 已提交
97
	unsigned int pcpu;
98
	int start_cpu;
L
Linus Torvalds 已提交
99

100
	if (cpumask_test_cpu(lcpu, of_spin_mask))
L
Linus Torvalds 已提交
101 102 103 104 105
		/* Already started by OF and sitting in spin loop */
		return 1;

	pcpu = get_hard_smp_processor_id(lcpu);

106 107
	/* Check to see if the CPU out of FW already for kexec */
	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
108
		cpumask_set_cpu(lcpu, of_spin_mask);
109 110 111
		return 1;
	}

L
Linus Torvalds 已提交
112
	/* Fixup atomic count: it exited inside IRQ handler. */
A
Al Viro 已提交
113
	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;
114
#ifdef CONFIG_HOTPLUG_CPU
115 116
	if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
		goto out;
117
#endif
118 119 120 121 122 123 124 125
	/* 
	 * 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;

126
	status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
L
Linus Torvalds 已提交
127 128 129 130
	if (status != 0) {
		printk(KERN_ERR "start-cpu failed: %i\n", status);
		return 0;
	}
131

132
#ifdef CONFIG_HOTPLUG_CPU
133
out:
134
#endif
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142
	return 1;
}

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

143
	if (firmware_has_feature(FW_FEATURE_SPLPAR))
L
Linus Torvalds 已提交
144 145
		vpa_init(cpu);

146
	cpumask_clear_cpu(cpu, of_spin_mask);
147
#ifdef CONFIG_HOTPLUG_CPU
148 149
	set_cpu_current_state(cpu, CPU_STATE_ONLINE);
	set_default_offline_state(cpu);
150
#endif
151
	pseries_notify_cpuidle_add_cpu(cpu);
L
Linus Torvalds 已提交
152 153
}

154
static int __devinit smp_pSeries_kick_cpu(int nr)
L
Linus Torvalds 已提交
155 156 157 158
{
	BUG_ON(nr < 0 || nr >= NR_CPUS);

	if (!smp_startup_cpu(nr))
159
		return -ENOENT;
L
Linus Torvalds 已提交
160 161 162 163 164 165 166

	/*
	 * 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;
167
#ifdef CONFIG_HOTPLUG_CPU
168 169 170
	set_preferred_offline_state(nr, CPU_STATE_ONLINE);

	if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
171 172 173
		long rc;
		unsigned long hcpuid;

174 175 176
		hcpuid = get_hard_smp_processor_id(nr);
		rc = plpar_hcall_norets(H_PROD, hcpuid);
		if (rc != H_SUCCESS)
177 178
			printk(KERN_ERR "Error: Prod to wake up processor %d "
						"Ret= %ld\n", nr, rc);
179
	}
180
#endif
181 182

	return 0;
L
Linus Torvalds 已提交
183 184 185 186 187
}

static int smp_pSeries_cpu_bootable(unsigned int nr)
{
	/* Special case - we inhibit secondary thread startup
188
	 * during boot if the user requests it.
L
Linus Torvalds 已提交
189
	 */
190 191 192 193 194 195 196
	if (system_state < SYSTEM_RUNNING && cpu_has_feature(CPU_FTR_SMT)) {
		if (!smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
			return 0;
		if (smt_enabled_at_boot
		    && cpu_thread_in_core(nr) >= smt_enabled_at_boot)
			return 0;
	}
L
Linus Torvalds 已提交
197 198 199

	return 1;
}
200

L
Linus Torvalds 已提交
201 202 203 204 205 206
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,
};
207

L
Linus Torvalds 已提交
208
static struct smp_ops_t pSeries_xics_smp_ops = {
209
	.message_pass	= NULL,	/* Use smp_muxed_ipi_message_pass */
210
	.cause_ipi	= NULL,	/* Filled at runtime by xics_smp_probe() */
211
	.probe		= xics_smp_probe,
L
Linus Torvalds 已提交
212 213 214 215 216 217
	.kick_cpu	= smp_pSeries_kick_cpu,
	.setup_cpu	= smp_xics_setup_cpu,
	.cpu_bootable	= smp_pSeries_cpu_bootable,
};

/* This is called very early */
218
static void __init smp_init_pseries(void)
L
Linus Torvalds 已提交
219 220 221
{
	int i;

222
	pr_debug(" -> smp_init_pSeries()\n");
L
Linus Torvalds 已提交
223

224 225
	alloc_bootmem_cpumask_var(&of_spin_mask);

L
Linus Torvalds 已提交
226
	/* Mark threads which are still spinning in hold loops. */
227 228
	if (cpu_has_feature(CPU_FTR_SMT)) {
		for_each_present_cpu(i) { 
229
			if (cpu_thread_in_core(i) == 0)
230
				cpumask_set_cpu(i, of_spin_mask);
L
Linus Torvalds 已提交
231
		}
232
	} else {
233
		cpumask_copy(of_spin_mask, cpu_present_mask);
234
	}
L
Linus Torvalds 已提交
235

236
	cpumask_clear_cpu(boot_cpuid, of_spin_mask);
L
Linus Torvalds 已提交
237 238 239

	/* Non-lpar has additional take/give timebase */
	if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
240 241
		smp_ops->give_timebase = rtas_give_timebase;
		smp_ops->take_timebase = rtas_take_timebase;
L
Linus Torvalds 已提交
242 243
	}

244
	pr_debug(" <- smp_init_pSeries()\n");
L
Linus Torvalds 已提交
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258 259
void __init smp_init_pseries_mpic(void)
{
	smp_ops = &pSeries_mpic_smp_ops;

	smp_init_pseries();
}

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

	smp_init_pseries();
}