smp.c 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * SMP support for PowerNV machines.
 *
 * Copyright 2011 IBM Corp.
 *
 * 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/module.h>
#include <linux/sched.h>
15
#include <linux/sched/hotplug.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <linux/smp.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/cpu.h>

#include <asm/irq.h>
#include <asm/smp.h>
#include <asm/paca.h>
#include <asm/machdep.h>
#include <asm/cputable.h>
#include <asm/firmware.h>
#include <asm/vdso_datapage.h>
#include <asm/cputhreads.h>
#include <asm/xics.h>
32
#include <asm/opal.h>
33
#include <asm/runlatch.h>
34
#include <asm/code-patching.h>
35
#include <asm/dbell.h>
36 37
#include <asm/kvm_ppc.h>
#include <asm/ppc-opcode.h>
38 39 40

#include "powernv.h"

41 42 43 44 45 46 47
#ifdef DEBUG
#include <asm/udbg.h>
#define DBG(fmt...) udbg_printf(fmt)
#else
#define DBG(fmt...)
#endif

48
static void pnv_smp_setup_cpu(int cpu)
49 50 51
{
	if (cpu != boot_cpuid)
		xics_setup_cpu();
52 53 54 55 56

#ifdef CONFIG_PPC_DOORBELL
	if (cpu_has_feature(CPU_FTR_DBELL))
		doorbell_setup_this_cpu();
#endif
57 58
}

59
static int pnv_smp_kick_cpu(int nr)
60 61
{
	unsigned int pcpu = get_hard_smp_processor_id(nr);
62 63
	unsigned long start_here =
			__pa(ppc_function_entry(generic_secondary_smp_init));
64
	long rc;
65
	uint8_t status;
66 67 68

	BUG_ON(nr < 0 || nr >= NR_CPUS);

69
	/*
70
	 * If we already started or OPAL is not supported, we just
71
	 * kick the CPU via the PACA
72
	 */
73
	if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPAL))
74 75 76 77 78 79 80 81
		goto kick;

	/*
	 * At this point, the CPU can either be spinning on the way in
	 * from kexec or be inside OPAL waiting to be started for the
	 * first time. OPAL v3 allows us to query OPAL to know if it
	 * has the CPUs, so we do that
	 */
82 83 84 85 86
	rc = opal_query_cpu_status(pcpu, &status);
	if (rc != OPAL_SUCCESS) {
		pr_warn("OPAL Error %ld querying CPU %d state\n", rc, nr);
		return -ENODEV;
	}
87

88 89 90 91 92 93
	/*
	 * Already started, just kick it, probably coming from
	 * kexec and spinning
	 */
	if (status == OPAL_THREAD_STARTED)
		goto kick;
94

95 96 97 98 99 100 101 102
	/*
	 * Available/inactive, let's kick it
	 */
	if (status == OPAL_THREAD_INACTIVE) {
		pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n", nr, pcpu);
		rc = opal_start_cpu(pcpu, start_here);
		if (rc != OPAL_SUCCESS) {
			pr_warn("OPAL Error %ld starting CPU %d\n", rc, nr);
103 104 105 106
			return -ENODEV;
		}
	} else {
		/*
107 108 109 110
		 * An unavailable CPU (or any other unknown status)
		 * shouldn't be started. It should also
		 * not be in the possible map but currently it can
		 * happen
111
		 */
112 113 114
		pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
			 " (status %d)...\n", nr, pcpu, status);
		return -ENODEV;
115
	}
116 117

kick:
118 119 120
	return smp_generic_kick_cpu(nr);
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#ifdef CONFIG_HOTPLUG_CPU

static int pnv_smp_cpu_disable(void)
{
	int cpu = smp_processor_id();

	/* This is identical to pSeries... might consolidate by
	 * moving migrate_irqs_away to a ppc_md with default to
	 * the generic fixup_irqs. --BenH.
	 */
	set_cpu_online(cpu, false);
	vdso_data->processorCount--;
	if (cpu == boot_cpuid)
		boot_cpuid = cpumask_any(cpu_online_mask);
	xics_migrate_irqs_away();
	return 0;
}

static void pnv_smp_cpu_kill_self(void)
{
	unsigned int cpu;
142
	unsigned long srr1, wmask;
143
	u32 idle_states;
144 145 146 147 148 149 150 151 152 153

	/* Standard hot unplug procedure */
	local_irq_disable();
	idle_task_exit();
	current->active_mm = NULL; /* for sanity */
	cpu = smp_processor_id();
	DBG("CPU%d offline\n", cpu);
	generic_set_cpu_dead(cpu);
	smp_wmb();

154 155 156 157
	wmask = SRR1_WAKEMASK;
	if (cpu_has_feature(CPU_FTR_ARCH_207S))
		wmask = SRR1_WAKEMASK_P8;

158
	idle_states = pnv_get_supported_cpuidle_states();
159

160
	/* We don't want to take decrementer interrupts while we are offline,
161 162
	 * so clear LPCR:PECE1. We keep PECE2 (and LPCR_PECE_HVEE on P9)
	 * enabled as to let IPIs in.
163 164
	 */
	mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
165 166 167 168 169 170 171 172 173 174 175

	/*
	 * Hard-disable interrupts, and then clear irq_happened flags
	 * that we can safely ignore while off-line, since they
	 * are for things for which we do no processing when off-line
	 * (or in the case of HMI, all the processing we need to do
	 * is done in lower-level real-mode code).
	 */
	hard_irq_disable();
	local_paca->irq_happened &= ~(PACA_IRQ_DEC | PACA_IRQ_HMI);

176
	while (!generic_check_cpu_restart(cpu)) {
177 178 179 180 181 182 183 184
		/*
		 * Clear IPI flag, since we don't handle IPIs while
		 * offline, except for those when changing micro-threading
		 * mode, which are handled explicitly below, and those
		 * for coming online, which are handled via
		 * generic_check_cpu_restart() calls.
		 */
		kvmppc_set_host_ipi(cpu, 0);
185

186
		ppc64_runlatch_off();
187

188 189 190 191
		if (cpu_has_feature(CPU_FTR_ARCH_300)) {
			srr1 = power9_idle_stop(pnv_deepest_stop_psscr_val,
						pnv_deepest_stop_psscr_mask);
		} else if (idle_states & OPAL_PM_WINKLE_ENABLED) {
192
			srr1 = power7_winkle();
193 194
		} else if ((idle_states & OPAL_PM_SLEEP_ENABLED) ||
			   (idle_states & OPAL_PM_SLEEP_ENABLED_ER1)) {
195
			srr1 = power7_sleep();
196
		} else {
197
			srr1 = power7_nap(1);
198
		}
199

200
		ppc64_runlatch_on();
201

202 203 204 205 206 207 208 209 210 211 212
		/*
		 * If the SRR1 value indicates that we woke up due to
		 * an external interrupt, then clear the interrupt.
		 * We clear the interrupt before checking for the
		 * reason, so as to avoid a race where we wake up for
		 * some other reason, find nothing and clear the interrupt
		 * just as some other cpu is sending us an interrupt.
		 * If we returned from power7_nap as a result of
		 * having finished executing in a KVM guest, then srr1
		 * contains 0.
		 */
213
		if (((srr1 & wmask) == SRR1_WAKEEE) ||
214
		    ((srr1 & wmask) == SRR1_WAKEHVI) ||
215
		    (local_paca->irq_happened & PACA_IRQ_EE)) {
216 217 218 219
			if (cpu_has_feature(CPU_FTR_ARCH_300))
				icp_opal_flush_interrupt();
			else
				icp_native_flush_interrupt();
220 221 222
		} else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
			unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
			asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
223
		}
224 225
		local_paca->irq_happened &= ~(PACA_IRQ_EE | PACA_IRQ_DBELL);
		smp_mb();
226 227 228 229

		if (cpu_core_split_required())
			continue;

230
		if (srr1 && !generic_check_cpu_restart(cpu))
231 232
			DBG("CPU%d Unexpected exit while offline !\n", cpu);
	}
233 234

	/* Re-enable decrementer interrupts */
235 236 237 238 239 240
	mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1);
	DBG("CPU%d coming online...\n", cpu);
}

#endif /* CONFIG_HOTPLUG_CPU */

241 242 243 244 245 246 247 248 249 250 251 252 253 254
static int pnv_cpu_bootable(unsigned int nr)
{
	/*
	 * Starting with POWER8, the subcore logic relies on all threads of a
	 * core being booted so that they can participate in split mode
	 * switches. So on those machines we ignore the smt_enabled_at_boot
	 * setting (smt-enabled on the kernel command line).
	 */
	if (cpu_has_feature(CPU_FTR_ARCH_207S))
		return 1;

	return smp_generic_cpu_bootable(nr);
}

255 256 257 258
static struct smp_ops_t pnv_smp_ops = {
	.message_pass	= smp_muxed_ipi_message_pass,
	.cause_ipi	= NULL,	/* Filled at runtime by xics_smp_probe() */
	.probe		= xics_smp_probe,
259
	.kick_cpu	= pnv_smp_kick_cpu,
260
	.setup_cpu	= pnv_smp_setup_cpu,
261
	.cpu_bootable	= pnv_cpu_bootable,
262 263 264 265
#ifdef CONFIG_HOTPLUG_CPU
	.cpu_disable	= pnv_smp_cpu_disable,
	.cpu_die	= generic_cpu_die,
#endif /* CONFIG_HOTPLUG_CPU */
266 267 268 269 270 271 272
};

/* This is called very early during platform setup_arch */
void __init pnv_smp_init(void)
{
	smp_ops = &pnv_smp_ops;

273 274 275
#ifdef CONFIG_HOTPLUG_CPU
	ppc_md.cpu_die	= pnv_smp_cpu_kill_self;
#endif
276
}