2 * SMP support for PowerNV machines.
4 * Copyright 2011 IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/cpu.h>
25 #include <asm/machdep.h>
26 #include <asm/cputable.h>
27 #include <asm/firmware.h>
29 #include <asm/vdso_datapage.h>
30 #include <asm/cputhreads.h>
33 #include <asm/runlatch.h>
34 #include <asm/code-patching.h>
40 #define DBG(fmt...) udbg_printf(fmt)
45 static void pnv_smp_setup_cpu(int cpu)
47 if (cpu != boot_cpuid)
51 int pnv_smp_kick_cpu(int nr)
53 unsigned int pcpu = get_hard_smp_processor_id(nr);
54 unsigned long start_here =
55 __pa(ppc_function_entry(generic_secondary_smp_init));
58 BUG_ON(nr < 0 || nr >= NR_CPUS);
61 * If we already started or OPALv2 is not supported, we just
62 * kick the CPU via the PACA
64 if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPALv2))
68 * At this point, the CPU can either be spinning on the way in
69 * from kexec or be inside OPAL waiting to be started for the
70 * first time. OPAL v3 allows us to query OPAL to know if it
71 * has the CPUs, so we do that
73 if (firmware_has_feature(FW_FEATURE_OPALv3)) {
76 rc = opal_query_cpu_status(pcpu, &status);
77 if (rc != OPAL_SUCCESS) {
78 pr_warn("OPAL Error %ld querying CPU %d state\n",
84 * Already started, just kick it, probably coming from
87 if (status == OPAL_THREAD_STARTED)
91 * Available/inactive, let's kick it
93 if (status == OPAL_THREAD_INACTIVE) {
94 pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n",
96 rc = opal_start_cpu(pcpu, start_here);
97 if (rc != OPAL_SUCCESS) {
98 pr_warn("OPAL Error %ld starting CPU %d\n",
104 * An unavailable CPU (or any other unknown status)
105 * shouldn't be started. It should also
106 * not be in the possible map but currently it can
109 pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
110 " (status %d)...\n", nr, pcpu, status);
115 * On OPAL v2, we just kick it and hope for the best,
116 * we must not test the error from opal_start_cpu() or
117 * we would fail to get CPUs from kexec.
119 opal_start_cpu(pcpu, start_here);
122 return smp_generic_kick_cpu(nr);
125 #ifdef CONFIG_HOTPLUG_CPU
127 static int pnv_smp_cpu_disable(void)
129 int cpu = smp_processor_id();
131 /* This is identical to pSeries... might consolidate by
132 * moving migrate_irqs_away to a ppc_md with default to
133 * the generic fixup_irqs. --BenH.
135 set_cpu_online(cpu, false);
136 vdso_data->processorCount--;
137 if (cpu == boot_cpuid)
138 boot_cpuid = cpumask_any(cpu_online_mask);
139 xics_migrate_irqs_away();
143 static void pnv_smp_cpu_kill_self(void)
147 /* Standard hot unplug procedure */
150 current->active_mm = NULL; /* for sanity */
151 cpu = smp_processor_id();
152 DBG("CPU%d offline\n", cpu);
153 generic_set_cpu_dead(cpu);
156 /* We don't want to take decrementer interrupts while we are offline,
157 * so clear LPCR:PECE1. We keep PECE2 enabled.
159 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
160 while (!generic_check_cpu_restart(cpu)) {
161 ppc64_runlatch_off();
164 if (!generic_check_cpu_restart(cpu)) {
165 DBG("CPU%d Unexpected exit while offline !\n", cpu);
166 /* We may be getting an IPI, so we re-enable
167 * interrupts to process it, it will be ignored
168 * since we aren't online (hopefully)
174 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1);
175 DBG("CPU%d coming online...\n", cpu);
178 #endif /* CONFIG_HOTPLUG_CPU */
180 static struct smp_ops_t pnv_smp_ops = {
181 .message_pass = smp_muxed_ipi_message_pass,
182 .cause_ipi = NULL, /* Filled at runtime by xics_smp_probe() */
183 .probe = xics_smp_probe,
184 .kick_cpu = pnv_smp_kick_cpu,
185 .setup_cpu = pnv_smp_setup_cpu,
186 .cpu_bootable = smp_generic_cpu_bootable,
187 #ifdef CONFIG_HOTPLUG_CPU
188 .cpu_disable = pnv_smp_cpu_disable,
189 .cpu_die = generic_cpu_die,
190 #endif /* CONFIG_HOTPLUG_CPU */
193 /* This is called very early during platform setup_arch */
194 void __init pnv_smp_init(void)
196 smp_ops = &pnv_smp_ops;
198 /* XXX We don't yet have a proper entry point from HAL, for
199 * now we rely on kexec-style entry from BML
202 #ifdef CONFIG_PPC_RTAS
203 /* Non-lpar has additional take/give timebase */
204 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
205 smp_ops->give_timebase = rtas_give_timebase;
206 smp_ops->take_timebase = rtas_take_timebase;
208 #endif /* CONFIG_PPC_RTAS */
210 #ifdef CONFIG_HOTPLUG_CPU
211 ppc_md.cpu_die = pnv_smp_cpu_kill_self;