blob: cf0e1245b8cc1c78948a4004be2d20c5b5ac0b78 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * SMP support for ppc.
3 *
4 * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
5 * deal of code from the sparc and intel versions.
6 *
7 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
8 *
9 * PowerPC-64 Support added by Dave Engebretsen, Peter Bergner, and
10 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18#undef DEBUG
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040021#include <linux/export.h>
Ingo Molnar68e21be2017-02-01 19:08:20 +010022#include <linux/sched/mm.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010023#include <linux/sched/topology.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/smp.h>
25#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/init.h>
28#include <linux/spinlock.h>
29#include <linux/cache.h>
30#include <linux/err.h>
Kay Sievers8a25a2f2011-12-21 14:29:42 -080031#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/cpu.h>
33#include <linux/notifier.h>
Anton Blanchard4b703a22005-12-13 06:56:47 +110034#include <linux/topology.h>
Daniel Axtens665e87f2016-05-18 11:16:51 +100035#include <linux/profile.h>
Nicholas Piggin4e287e62017-06-06 23:08:32 +100036#include <linux/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/ptrace.h>
Arun Sharma600634972011-07-26 16:09:06 -070039#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/irq.h>
Srivatsa S. Bhat1b67bee2014-02-26 05:37:43 +053041#include <asm/hw_irq.h>
Michael Ellerman441c19c2014-05-23 18:15:25 +100042#include <asm/kvm_ppc.h>
Nicholas Pigginb866cc22017-04-13 20:16:21 +100043#include <asm/dbell.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/page.h>
45#include <asm/pgtable.h>
46#include <asm/prom.h>
47#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/time.h>
49#include <asm/machdep.h>
Nathan Lynche2075f72008-07-27 15:24:52 +100050#include <asm/cputhreads.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/cputable.h>
Stephen Rothwellbbeb3f42005-09-27 13:51:59 +100052#include <asm/mpic.h>
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +110053#include <asm/vdso_datapage.h>
Paul Mackerras5ad57072005-11-05 10:33:55 +110054#ifdef CONFIG_PPC64
55#include <asm/paca.h>
56#endif
Anton Blanchard18ad51d2012-07-04 20:37:11 +000057#include <asm/vdso.h>
David Howellsae3a1972012-03-28 18:30:02 +010058#include <asm/debug.h>
Anton Blanchard1217d342014-08-20 08:55:19 +100059#include <asm/kexec.h>
Daniel Axtens42f5b4c2016-05-18 11:16:50 +100060#include <asm/asm-prototypes.h>
Kevin Haob92a2262016-07-23 14:42:40 +053061#include <asm/cpu_has_feature.h>
Paul Mackerras5ad57072005-11-05 10:33:55 +110062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef DEBUG
Michael Ellermanf9e4ec52005-11-15 15:16:38 +110064#include <asm/udbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define DBG(fmt...) udbg_printf(fmt)
66#else
67#define DBG(fmt...)
68#endif
69
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +110070#ifdef CONFIG_HOTPLUG_CPU
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +000071/* State of each CPU during hotplug phases */
72static DEFINE_PER_CPU(int, cpu_state) = { 0 };
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +110073#endif
74
Michael Ellermanf9e4ec52005-11-15 15:16:38 +110075struct thread_info *secondary_ti;
76
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +000077DEFINE_PER_CPU(cpumask_var_t, cpu_sibling_map);
78DEFINE_PER_CPU(cpumask_var_t, cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Mike Travisd5a74302007-10-16 01:24:05 -070080EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
Nathan Lynch440a0852008-07-27 15:24:53 +100081EXPORT_PER_CPU_SYMBOL(cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Paul Mackerras5ad57072005-11-05 10:33:55 +110083/* SMP operations for this machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084struct smp_ops_t *smp_ops;
85
Benjamin Herrenschmidt7ccbe502009-06-18 23:30:07 +000086/* Can't be static due to PowerMac hackery */
87volatile unsigned int cpu_callin_map[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089int smt_enabled_at_boot = 1;
90
Andy Fleming3cd85252013-08-05 14:58:34 -050091/*
92 * Returns 1 if the specified cpu should be brought up during boot.
93 * Used to inhibit booting threads if they've been disabled or
94 * limited on the command line
95 */
96int smp_generic_cpu_bootable(unsigned int nr)
97{
98 /* Special case - we inhibit secondary thread startup
99 * during boot if the user requests it.
100 */
Thomas Gleixnera8fcfc12017-05-16 20:42:37 +0200101 if (system_state < SYSTEM_RUNNING && cpu_has_feature(CPU_FTR_SMT)) {
Andy Fleming3cd85252013-08-05 14:58:34 -0500102 if (!smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
103 return 0;
104 if (smt_enabled_at_boot
105 && cpu_thread_in_core(nr) >= smt_enabled_at_boot)
106 return 0;
107 }
108
109 return 1;
110}
111
112
Paul Mackerras5ad57072005-11-05 10:33:55 +1100113#ifdef CONFIG_PPC64
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800114int smp_generic_kick_cpu(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Santosh Sivarajc642af92017-06-27 12:30:06 +0530116 if (nr < 0 || nr >= nr_cpu_ids)
Santosh Sivarajf8d0d5d2017-06-27 12:30:05 +0530117 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /*
120 * The processor is currently spinning, waiting for the
121 * cpu_start field to become non-zero After we set cpu_start,
122 * the processor will continue on to secondary_start
123 */
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000124 if (!paca[nr].cpu_start) {
125 paca[nr].cpu_start = 1;
126 smp_mb();
127 return 0;
128 }
129
130#ifdef CONFIG_HOTPLUG_CPU
131 /*
132 * Ok it's not there, so it might be soft-unplugged, let's
133 * try to bring it back
134 */
Zhao Chenhuiae5cab42012-07-20 20:42:34 +0800135 generic_set_cpu_up(nr);
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000136 smp_wmb();
137 smp_send_reschedule(nr);
138#endif /* CONFIG_HOTPLUG_CPU */
Michael Ellermande300972011-04-11 21:46:19 +0000139
140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000142#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Milton Miller25ddd732008-11-14 20:11:49 +0000144static irqreturn_t call_function_action(int irq, void *data)
145{
146 generic_smp_call_function_interrupt();
147 return IRQ_HANDLED;
148}
149
150static irqreturn_t reschedule_action(int irq, void *data)
151{
Peter Zijlstra184748c2011-04-05 17:23:39 +0200152 scheduler_ipi();
Milton Miller25ddd732008-11-14 20:11:49 +0000153 return IRQ_HANDLED;
154}
155
Srivatsa S. Bhat1b67bee2014-02-26 05:37:43 +0530156static irqreturn_t tick_broadcast_ipi_action(int irq, void *data)
Milton Miller25ddd732008-11-14 20:11:49 +0000157{
Srivatsa S. Bhat1b67bee2014-02-26 05:37:43 +0530158 tick_broadcast_ipi_handler();
Milton Miller25ddd732008-11-14 20:11:49 +0000159 return IRQ_HANDLED;
160}
161
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000162#ifdef CONFIG_NMI_IPI
163static irqreturn_t nmi_ipi_action(int irq, void *data)
Milton Miller25ddd732008-11-14 20:11:49 +0000164{
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000165 smp_handle_nmi_ipi(get_irq_regs());
Milton Miller25ddd732008-11-14 20:11:49 +0000166 return IRQ_HANDLED;
167}
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000168#endif
Milton Miller25ddd732008-11-14 20:11:49 +0000169
170static irq_handler_t smp_ipi_action[] = {
171 [PPC_MSG_CALL_FUNCTION] = call_function_action,
172 [PPC_MSG_RESCHEDULE] = reschedule_action,
Srivatsa S. Bhat1b67bee2014-02-26 05:37:43 +0530173 [PPC_MSG_TICK_BROADCAST] = tick_broadcast_ipi_action,
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000174#ifdef CONFIG_NMI_IPI
175 [PPC_MSG_NMI_IPI] = nmi_ipi_action,
176#endif
Milton Miller25ddd732008-11-14 20:11:49 +0000177};
178
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000179/*
180 * The NMI IPI is a fallback and not truly non-maskable. It is simpler
181 * than going through the call function infrastructure, and strongly
182 * serialized, so it is more appropriate for debugging.
183 */
Milton Miller25ddd732008-11-14 20:11:49 +0000184const char *smp_ipi_name[] = {
185 [PPC_MSG_CALL_FUNCTION] = "ipi call function",
186 [PPC_MSG_RESCHEDULE] = "ipi reschedule",
Srivatsa S. Bhat1b67bee2014-02-26 05:37:43 +0530187 [PPC_MSG_TICK_BROADCAST] = "ipi tick-broadcast",
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000188 [PPC_MSG_NMI_IPI] = "nmi ipi",
Milton Miller25ddd732008-11-14 20:11:49 +0000189};
190
191/* optional function to request ipi, for controllers with >= 4 ipis */
192int smp_request_message_ipi(int virq, int msg)
193{
194 int err;
195
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000196 if (msg < 0 || msg > PPC_MSG_NMI_IPI)
Milton Miller25ddd732008-11-14 20:11:49 +0000197 return -EINVAL;
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000198#ifndef CONFIG_NMI_IPI
199 if (msg == PPC_MSG_NMI_IPI)
Milton Miller25ddd732008-11-14 20:11:49 +0000200 return 1;
Milton Miller25ddd732008-11-14 20:11:49 +0000201#endif
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000202
Thomas Gleixner3b5e16d2011-10-05 02:30:50 +0000203 err = request_irq(virq, smp_ipi_action[msg],
Zhao Chenhuie6651de2012-07-20 20:47:01 +0800204 IRQF_PERCPU | IRQF_NO_THREAD | IRQF_NO_SUSPEND,
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000205 smp_ipi_name[msg], NULL);
Milton Miller25ddd732008-11-14 20:11:49 +0000206 WARN(err < 0, "unable to request_irq %d for %s (rc %d)\n",
207 virq, smp_ipi_name[msg], err);
208
209 return err;
210}
211
Milton Miller1ece3552011-05-10 19:29:42 +0000212#ifdef CONFIG_PPC_SMP_MUXED_IPI
Milton Miller23d72bf2011-05-10 19:29:39 +0000213struct cpu_messages {
Suresh Warrierbd7f5612015-12-17 14:59:03 -0600214 long messages; /* current messages */
Milton Miller23d72bf2011-05-10 19:29:39 +0000215};
216static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpu_messages, ipi_message);
217
Suresh Warrier31639c772015-12-17 14:59:04 -0600218void smp_muxed_ipi_set_message(int cpu, int msg)
Milton Miller23d72bf2011-05-10 19:29:39 +0000219{
220 struct cpu_messages *info = &per_cpu(ipi_message, cpu);
Milton Miller71454272011-05-10 19:29:46 +0000221 char *message = (char *)&info->messages;
Milton Miller23d72bf2011-05-10 19:29:39 +0000222
Paul Mackerras9fb1b362012-09-04 18:33:08 +0000223 /*
224 * Order previous accesses before accesses in the IPI handler.
225 */
226 smp_mb();
Milton Miller71454272011-05-10 19:29:46 +0000227 message[msg] = 1;
Suresh Warrier31639c772015-12-17 14:59:04 -0600228}
229
230void smp_muxed_ipi_message_pass(int cpu, int msg)
231{
Suresh Warrier31639c772015-12-17 14:59:04 -0600232 smp_muxed_ipi_set_message(cpu, msg);
Nicholas Pigginb866cc22017-04-13 20:16:21 +1000233
Paul Mackerras9fb1b362012-09-04 18:33:08 +0000234 /*
235 * cause_ipi functions are required to include a full barrier
236 * before doing whatever causes the IPI.
237 */
Nicholas Pigginb866cc22017-04-13 20:16:21 +1000238 smp_ops->cause_ipi(cpu);
Milton Miller23d72bf2011-05-10 19:29:39 +0000239}
240
Anton Blanchard0654de12013-08-07 02:01:48 +1000241#ifdef __BIG_ENDIAN__
Suresh Warrierbd7f5612015-12-17 14:59:03 -0600242#define IPI_MESSAGE(A) (1uL << ((BITS_PER_LONG - 8) - 8 * (A)))
Anton Blanchard0654de12013-08-07 02:01:48 +1000243#else
Suresh Warrierbd7f5612015-12-17 14:59:03 -0600244#define IPI_MESSAGE(A) (1uL << (8 * (A)))
Anton Blanchard0654de12013-08-07 02:01:48 +1000245#endif
246
Milton Miller23d72bf2011-05-10 19:29:39 +0000247irqreturn_t smp_ipi_demux(void)
248{
Milton Miller23d72bf2011-05-10 19:29:39 +0000249 mb(); /* order any irq clear */
Milton Miller71454272011-05-10 19:29:46 +0000250
Nicholas Pigginb87ac022017-04-13 20:16:22 +1000251 return smp_ipi_demux_relaxed();
252}
253
254/* sync-free variant. Callers should ensure synchronization */
255irqreturn_t smp_ipi_demux_relaxed(void)
256{
Nicholas Pigginb866cc22017-04-13 20:16:21 +1000257 struct cpu_messages *info;
Milton Miller23d72bf2011-05-10 19:29:39 +0000258 unsigned long all;
259
Nicholas Pigginb866cc22017-04-13 20:16:21 +1000260 info = this_cpu_ptr(&ipi_message);
Milton Miller71454272011-05-10 19:29:46 +0000261 do {
Paul Mackerras9fb1b362012-09-04 18:33:08 +0000262 all = xchg(&info->messages, 0);
Suresh E. Warriere17769e2015-12-21 16:22:51 -0600263#if defined(CONFIG_KVM_XICS) && defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
264 /*
265 * Must check for PPC_MSG_RM_HOST_ACTION messages
266 * before PPC_MSG_CALL_FUNCTION messages because when
267 * a VM is destroyed, we call kick_all_cpus_sync()
268 * to ensure that any pending PPC_MSG_RM_HOST_ACTION
269 * messages have completed before we free any VCPUs.
270 */
271 if (all & IPI_MESSAGE(PPC_MSG_RM_HOST_ACTION))
272 kvmppc_xics_ipi_action();
273#endif
Anton Blanchard0654de12013-08-07 02:01:48 +1000274 if (all & IPI_MESSAGE(PPC_MSG_CALL_FUNCTION))
Milton Miller23d72bf2011-05-10 19:29:39 +0000275 generic_smp_call_function_interrupt();
Anton Blanchard0654de12013-08-07 02:01:48 +1000276 if (all & IPI_MESSAGE(PPC_MSG_RESCHEDULE))
Benjamin Herrenschmidt880102e2011-05-20 15:36:52 +1000277 scheduler_ipi();
Srivatsa S. Bhat1b67bee2014-02-26 05:37:43 +0530278 if (all & IPI_MESSAGE(PPC_MSG_TICK_BROADCAST))
279 tick_broadcast_ipi_handler();
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000280#ifdef CONFIG_NMI_IPI
281 if (all & IPI_MESSAGE(PPC_MSG_NMI_IPI))
282 nmi_ipi_action(0, NULL);
283#endif
Milton Miller71454272011-05-10 19:29:46 +0000284 } while (info->messages);
285
Milton Miller23d72bf2011-05-10 19:29:39 +0000286 return IRQ_HANDLED;
287}
Milton Miller1ece3552011-05-10 19:29:42 +0000288#endif /* CONFIG_PPC_SMP_MUXED_IPI */
Milton Miller23d72bf2011-05-10 19:29:39 +0000289
Paul Mackerras9ca980d2011-05-25 23:34:12 +0000290static inline void do_message_pass(int cpu, int msg)
291{
292 if (smp_ops->message_pass)
293 smp_ops->message_pass(cpu, msg);
294#ifdef CONFIG_PPC_SMP_MUXED_IPI
295 else
296 smp_muxed_ipi_message_pass(cpu, msg);
297#endif
298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300void smp_send_reschedule(int cpu)
301{
Benjamin Herrenschmidt8cffc6a2006-07-04 14:09:36 +1000302 if (likely(smp_ops))
Paul Mackerras9ca980d2011-05-25 23:34:12 +0000303 do_message_pass(cpu, PPC_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000305EXPORT_SYMBOL_GPL(smp_send_reschedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Jens Axboeb7d7a242008-06-26 11:22:13 +0200307void arch_send_call_function_single_ipi(int cpu)
308{
Srivatsa S. Bhat402d9a12014-02-26 05:37:29 +0530309 do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
Jens Axboeb7d7a242008-06-26 11:22:13 +0200310}
311
Rusty Russellf063ea02009-09-24 09:34:45 -0600312void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Jens Axboeb7d7a242008-06-26 11:22:13 +0200313{
314 unsigned int cpu;
315
Rusty Russellf063ea02009-09-24 09:34:45 -0600316 for_each_cpu(cpu, mask)
Paul Mackerras9ca980d2011-05-25 23:34:12 +0000317 do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
Jens Axboeb7d7a242008-06-26 11:22:13 +0200318}
319
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000320#ifdef CONFIG_NMI_IPI
321
322/*
323 * "NMI IPI" system.
324 *
325 * NMI IPIs may not be recoverable, so should not be used as ongoing part of
326 * a running system. They can be used for crash, debug, halt/reboot, etc.
327 *
328 * NMI IPIs are globally single threaded. No more than one in progress at
329 * any time.
330 *
331 * The IPI call waits with interrupts disabled until all targets enter the
332 * NMI handler, then the call returns.
333 *
334 * No new NMI can be initiated until targets exit the handler.
335 *
336 * The IPI call may time out without all targets entering the NMI handler.
337 * In that case, there is some logic to recover (and ignore subsequent
338 * NMI interrupts that may eventually be raised), but the platform interrupt
339 * handler may not be able to distinguish this from other exception causes,
340 * which may cause a crash.
341 */
342
343static atomic_t __nmi_ipi_lock = ATOMIC_INIT(0);
344static struct cpumask nmi_ipi_pending_mask;
345static int nmi_ipi_busy_count = 0;
346static void (*nmi_ipi_function)(struct pt_regs *) = NULL;
347
348static void nmi_ipi_lock_start(unsigned long *flags)
349{
350 raw_local_irq_save(*flags);
351 hard_irq_disable();
352 while (atomic_cmpxchg(&__nmi_ipi_lock, 0, 1) == 1) {
353 raw_local_irq_restore(*flags);
354 cpu_relax();
355 raw_local_irq_save(*flags);
356 hard_irq_disable();
357 }
358}
359
360static void nmi_ipi_lock(void)
361{
362 while (atomic_cmpxchg(&__nmi_ipi_lock, 0, 1) == 1)
363 cpu_relax();
364}
365
366static void nmi_ipi_unlock(void)
367{
368 smp_mb();
369 WARN_ON(atomic_read(&__nmi_ipi_lock) != 1);
370 atomic_set(&__nmi_ipi_lock, 0);
371}
372
373static void nmi_ipi_unlock_end(unsigned long *flags)
374{
375 nmi_ipi_unlock();
376 raw_local_irq_restore(*flags);
377}
378
379/*
380 * Platform NMI handler calls this to ack
381 */
382int smp_handle_nmi_ipi(struct pt_regs *regs)
383{
384 void (*fn)(struct pt_regs *);
385 unsigned long flags;
386 int me = raw_smp_processor_id();
387 int ret = 0;
388
389 /*
390 * Unexpected NMIs are possible here because the interrupt may not
391 * be able to distinguish NMI IPIs from other types of NMIs, or
392 * because the caller may have timed out.
393 */
394 nmi_ipi_lock_start(&flags);
395 if (!nmi_ipi_busy_count)
396 goto out;
397 if (!cpumask_test_cpu(me, &nmi_ipi_pending_mask))
398 goto out;
399
400 fn = nmi_ipi_function;
401 if (!fn)
402 goto out;
403
404 cpumask_clear_cpu(me, &nmi_ipi_pending_mask);
405 nmi_ipi_busy_count++;
406 nmi_ipi_unlock();
407
408 ret = 1;
409
410 fn(regs);
411
412 nmi_ipi_lock();
413 nmi_ipi_busy_count--;
414out:
415 nmi_ipi_unlock_end(&flags);
416
417 return ret;
418}
419
420static void do_smp_send_nmi_ipi(int cpu)
421{
Nicholas Pigginc64af642016-12-20 04:30:09 +1000422 if (smp_ops->cause_nmi_ipi && smp_ops->cause_nmi_ipi(cpu))
423 return;
424
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000425 if (cpu >= 0) {
426 do_message_pass(cpu, PPC_MSG_NMI_IPI);
427 } else {
428 int c;
429
430 for_each_online_cpu(c) {
431 if (c == raw_smp_processor_id())
432 continue;
433 do_message_pass(c, PPC_MSG_NMI_IPI);
434 }
435 }
436}
437
Nicholas Piggin21041802017-07-12 14:35:52 -0700438void smp_flush_nmi_ipi(u64 delay_us)
439{
440 unsigned long flags;
441
442 nmi_ipi_lock_start(&flags);
443 while (nmi_ipi_busy_count) {
444 nmi_ipi_unlock_end(&flags);
445 udelay(1);
446 if (delay_us) {
447 delay_us--;
448 if (!delay_us)
449 return;
450 }
451 nmi_ipi_lock_start(&flags);
452 }
453 nmi_ipi_unlock_end(&flags);
454}
455
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000456/*
457 * - cpu is the target CPU (must not be this CPU), or NMI_IPI_ALL_OTHERS.
458 * - fn is the target callback function.
459 * - delay_us > 0 is the delay before giving up waiting for targets to
460 * enter the handler, == 0 specifies indefinite delay.
461 */
Nicholas Piggin21041802017-07-12 14:35:52 -0700462int smp_send_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us)
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000463{
464 unsigned long flags;
465 int me = raw_smp_processor_id();
466 int ret = 1;
467
468 BUG_ON(cpu == me);
469 BUG_ON(cpu < 0 && cpu != NMI_IPI_ALL_OTHERS);
470
471 if (unlikely(!smp_ops))
472 return 0;
473
474 /* Take the nmi_ipi_busy count/lock with interrupts hard disabled */
475 nmi_ipi_lock_start(&flags);
476 while (nmi_ipi_busy_count) {
477 nmi_ipi_unlock_end(&flags);
478 cpu_relax();
479 nmi_ipi_lock_start(&flags);
480 }
481
482 nmi_ipi_function = fn;
483
484 if (cpu < 0) {
485 /* ALL_OTHERS */
486 cpumask_copy(&nmi_ipi_pending_mask, cpu_online_mask);
487 cpumask_clear_cpu(me, &nmi_ipi_pending_mask);
488 } else {
489 /* cpumask starts clear */
490 cpumask_set_cpu(cpu, &nmi_ipi_pending_mask);
491 }
492 nmi_ipi_busy_count++;
493 nmi_ipi_unlock();
494
495 do_smp_send_nmi_ipi(cpu);
496
497 while (!cpumask_empty(&nmi_ipi_pending_mask)) {
498 udelay(1);
499 if (delay_us) {
500 delay_us--;
501 if (!delay_us)
502 break;
503 }
504 }
505
506 nmi_ipi_lock();
507 if (!cpumask_empty(&nmi_ipi_pending_mask)) {
508 /* Could not gather all CPUs */
509 ret = 0;
510 cpumask_clear(&nmi_ipi_pending_mask);
511 }
512 nmi_ipi_busy_count--;
513 nmi_ipi_unlock_end(&flags);
514
515 return ret;
516}
517#endif /* CONFIG_NMI_IPI */
518
Srivatsa S. Bhat1b67bee2014-02-26 05:37:43 +0530519#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
520void tick_broadcast(const struct cpumask *mask)
521{
522 unsigned int cpu;
523
524 for_each_cpu(cpu, mask)
525 do_message_pass(cpu, PPC_MSG_TICK_BROADCAST);
526}
527#endif
528
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000529#ifdef CONFIG_DEBUGGER
530void debugger_ipi_callback(struct pt_regs *regs)
531{
532 debugger_ipi(regs);
533}
534
Milton Millere0476372011-05-10 19:29:06 +0000535void smp_send_debugger_break(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000537 smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, debugger_ipi_callback, 1000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539#endif
540
Thiago Jung Bauermannda665882016-11-29 23:45:50 +1100541#ifdef CONFIG_KEXEC_CORE
Michael Ellermancc532912005-12-04 18:39:43 +1100542void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
543{
Nicholas Pigginddd703c2016-12-20 04:30:08 +1000544 smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, crash_ipi_callback, 1000000);
Michael Ellermancc532912005-12-04 18:39:43 +1100545}
546#endif
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static void stop_this_cpu(void *dummy)
549{
Valentine Barshak8389b372009-11-25 11:48:52 +0000550 /* Remove this CPU */
551 set_cpu_online(smp_processor_id(), false);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 local_irq_disable();
554 while (1)
555 ;
556}
557
Satyam Sharma8fd7675c2007-09-18 09:43:40 +1000558void smp_send_stop(void)
559{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200560 smp_call_function(stop_this_cpu, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563struct thread_info *current_set[NR_CPUS];
564
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800565static void smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Tejun Heo6b7487f2009-10-29 22:34:14 +0900567 per_cpu(cpu_pvr, id) = mfspr(SPRN_PVR);
Becky Bruce3160b092011-06-28 14:54:47 -0500568#ifdef CONFIG_PPC_FSL_BOOK3E
569 per_cpu(next_tlbcam_idx, id)
570 = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574void __init smp_prepare_cpus(unsigned int max_cpus)
575{
576 unsigned int cpu;
577
578 DBG("smp_prepare_cpus\n");
579
580 /*
581 * setup_cpu may need to be called on the boot cpu. We havent
582 * spun any cpus up but lets be paranoid.
583 */
584 BUG_ON(boot_cpuid != smp_processor_id());
585
586 /* Fixup boot cpu */
587 smp_store_cpu_info(boot_cpuid);
588 cpu_callin_map[boot_cpuid] = 1;
589
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +0000590 for_each_possible_cpu(cpu) {
591 zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu),
592 GFP_KERNEL, cpu_to_node(cpu));
593 zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu),
594 GFP_KERNEL, cpu_to_node(cpu));
Nishanth Aravamudan2fabf082014-07-17 16:15:12 -0700595 /*
596 * numa_node_id() works after this.
597 */
Li Zhongbc3c4322014-08-27 17:34:00 +0800598 if (cpu_present(cpu)) {
599 set_cpu_numa_node(cpu, numa_cpu_lookup_table[cpu]);
600 set_cpu_numa_mem(cpu,
601 local_memory_node(numa_cpu_lookup_table[cpu]));
602 }
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +0000603 }
604
605 cpumask_set_cpu(boot_cpuid, cpu_sibling_mask(boot_cpuid));
606 cpumask_set_cpu(boot_cpuid, cpu_core_mask(boot_cpuid));
607
Chen Gangdfee0ef2013-07-22 14:40:20 +0800608 if (smp_ops && smp_ops->probe)
609 smp_ops->probe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800612void smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 BUG_ON(smp_processor_id() != boot_cpuid);
Paul Mackerras5ad57072005-11-05 10:33:55 +1100615#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 paca[boot_cpuid].__current = current;
Paul Mackerras5ad57072005-11-05 10:33:55 +1100617#endif
Nishanth Aravamudan8c272262014-05-19 11:14:23 -0700618 set_numa_node(numa_cpu_lookup_table[boot_cpuid]);
Al Virob5e2fc12006-01-12 01:06:01 -0800619 current_set[boot_cpuid] = task_thread_info(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624int generic_cpu_disable(void)
625{
626 unsigned int cpu = smp_processor_id();
627
628 if (cpu == boot_cpuid)
629 return -EBUSY;
630
Rusty Russellea0f1ca2009-09-24 09:34:48 -0600631 set_cpu_online(cpu, false);
Paul Mackerras799d6042005-11-10 13:37:51 +1100632#ifdef CONFIG_PPC64
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +1100633 vdso_data->processorCount--;
Paul Mackerras094fe2e2005-11-10 14:26:12 +1100634#endif
Benjamin Herrenschmidta978e132017-04-05 17:54:49 +1000635 /* Update affinity of all IRQs previously aimed at this CPU */
636 irq_migrate_all_off_this_cpu();
637
Michael Ellerman687b8f22017-02-15 20:49:54 +1100638 /*
639 * Depending on the details of the interrupt controller, it's possible
640 * that one of the interrupts we just migrated away from this CPU is
641 * actually already pending on this CPU. If we leave it in that state
642 * the interrupt will never be EOI'ed, and will never fire again. So
643 * temporarily enable interrupts here, to allow any pending interrupt to
644 * be received (and EOI'ed), before we take this CPU offline.
645 */
Benjamin Herrenschmidta978e132017-04-05 17:54:49 +1000646 local_irq_enable();
647 mdelay(1);
648 local_irq_disable();
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653void generic_cpu_die(unsigned int cpu)
654{
655 int i;
656
657 for (i = 0; i < 100; i++) {
Anton Blanchard0d8d4d42005-05-01 08:58:47 -0700658 smp_rmb();
chenhui zhao2f4f1f82015-11-20 17:14:01 +0800659 if (is_cpu_dead(cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return;
661 msleep(100);
662 }
663 printk(KERN_ERR "CPU%d didn't die...\n", cpu);
664}
665
Benjamin Herrenschmidt105765f2011-04-01 09:23:37 +1100666void generic_set_cpu_dead(unsigned int cpu)
667{
668 per_cpu(cpu_state, cpu) = CPU_DEAD;
669}
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000670
Zhao Chenhuiae5cab42012-07-20 20:42:34 +0800671/*
672 * The cpu_state should be set to CPU_UP_PREPARE in kick_cpu(), otherwise
673 * the cpu_state is always CPU_DEAD after calling generic_set_cpu_dead(),
674 * which makes the delay in generic_cpu_die() not happen.
675 */
676void generic_set_cpu_up(unsigned int cpu)
677{
678 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
679}
680
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000681int generic_check_cpu_restart(unsigned int cpu)
682{
683 return per_cpu(cpu_state, cpu) == CPU_UP_PREPARE;
684}
Paul Mackerras512691d2012-10-15 01:15:41 +0000685
chenhui zhao2f4f1f82015-11-20 17:14:01 +0800686int is_cpu_dead(unsigned int cpu)
687{
688 return per_cpu(cpu_state, cpu) == CPU_DEAD;
689}
690
Michael Ellerman441c19c2014-05-23 18:15:25 +1000691static bool secondaries_inhibited(void)
Paul Mackerras512691d2012-10-15 01:15:41 +0000692{
Michael Ellerman441c19c2014-05-23 18:15:25 +1000693 return kvm_hv_mode_active();
Paul Mackerras512691d2012-10-15 01:15:41 +0000694}
695
696#else /* HOTPLUG_CPU */
697
698#define secondaries_inhibited() 0
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700#endif
701
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000702static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100703{
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000704 struct thread_info *ti = task_thread_info(idle);
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100705
706#ifdef CONFIG_PPC64
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000707 paca[cpu].__current = idle;
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100708 paca[cpu].kstack = (unsigned long)ti + THREAD_SIZE - STACK_FRAME_OVERHEAD;
709#endif
710 ti->cpu = cpu;
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000711 secondary_ti = current_set[cpu] = ti;
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100712}
713
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400714int __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100716 int rc, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Paul Mackerras512691d2012-10-15 01:15:41 +0000718 /*
719 * Don't allow secondary threads to come online if inhibited
720 */
721 if (threads_per_core > 1 && secondaries_inhibited() &&
Michael Ellerman6f5e40a2014-05-23 18:15:28 +1000722 cpu_thread_in_subcore(cpu))
Paul Mackerras512691d2012-10-15 01:15:41 +0000723 return -EBUSY;
724
Benjamin Herrenschmidt8cffc6a2006-07-04 14:09:36 +1000725 if (smp_ops == NULL ||
726 (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -EINVAL;
728
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000729 cpu_idle_thread_init(cpu, tidle);
kerstin jonssonc560bbc2011-05-17 23:57:11 +0000730
Benjamin Herrenschmidt14d4ae52017-04-05 17:54:48 +1000731 /*
732 * The platform might need to allocate resources prior to bringing
733 * up the CPU
734 */
735 if (smp_ops->prepare_cpu) {
736 rc = smp_ops->prepare_cpu(cpu);
737 if (rc)
738 return rc;
739 }
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* Make sure callin-map entry is 0 (can be leftover a CPU
742 * hotplug
743 */
744 cpu_callin_map[cpu] = 0;
745
746 /* The information for processor bringup must
747 * be written out to main store before we release
748 * the processor.
749 */
Anton Blanchard0d8d4d42005-05-01 08:58:47 -0700750 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* wake up cpus */
753 DBG("smp: kicking cpu %d\n", cpu);
Michael Ellermande300972011-04-11 21:46:19 +0000754 rc = smp_ops->kick_cpu(cpu);
755 if (rc) {
756 pr_err("smp: failed starting cpu %d (rc %d)\n", cpu, rc);
757 return rc;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
761 * wait to see if the cpu made a callin (is actually up).
762 * use this value that I found through experimentation.
763 * -- Cort
764 */
765 if (system_state < SYSTEM_RUNNING)
Jon Loeligeree0339f2006-06-17 17:52:44 -0500766 for (c = 50000; c && !cpu_callin_map[cpu]; c--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 udelay(100);
768#ifdef CONFIG_HOTPLUG_CPU
769 else
770 /*
771 * CPUs can take much longer to come up in the
772 * hotplug case. Wait five seconds.
773 */
Gautham R Shenoy67764262009-06-23 23:26:37 +0000774 for (c = 5000; c && !cpu_callin_map[cpu]; c--)
775 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#endif
777
778 if (!cpu_callin_map[cpu]) {
Signed-off-by: Darren Hart6685a472010-08-04 18:28:34 +0000779 printk(KERN_ERR "Processor %u is stuck.\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -ENOENT;
781 }
782
Signed-off-by: Darren Hart6685a472010-08-04 18:28:34 +0000783 DBG("Processor %u found.\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (smp_ops->give_timebase)
786 smp_ops->give_timebase();
787
Michael Ellerman875ebe942015-02-24 17:58:02 +1100788 /* Wait until cpu puts itself in the online & active maps */
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000789 spin_until_cond(cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 return 0;
792}
793
Nathan Lynche9efed32008-07-27 15:24:54 +1000794/* Return the value of the reg property corresponding to the given
795 * logical cpu.
796 */
797int cpu_to_core_id(int cpu)
798{
799 struct device_node *np;
Anton Blanchardf8a18832013-12-12 15:59:36 +1100800 const __be32 *reg;
Nathan Lynche9efed32008-07-27 15:24:54 +1000801 int id = -1;
802
803 np = of_get_cpu_node(cpu, NULL);
804 if (!np)
805 goto out;
806
807 reg = of_get_property(np, "reg", NULL);
808 if (!reg)
809 goto out;
810
Anton Blanchardf8a18832013-12-12 15:59:36 +1100811 id = be32_to_cpup(reg);
Nathan Lynche9efed32008-07-27 15:24:54 +1000812out:
813 of_node_put(np);
814 return id;
815}
Mauricio Faria de Oliveiraf8ab4812016-06-02 08:45:14 -0300816EXPORT_SYMBOL_GPL(cpu_to_core_id);
Nathan Lynche9efed32008-07-27 15:24:54 +1000817
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +0000818/* Helper routines for cpu to core mapping */
819int cpu_core_index_of_thread(int cpu)
820{
821 return cpu >> threads_shift;
822}
823EXPORT_SYMBOL_GPL(cpu_core_index_of_thread);
824
825int cpu_first_thread_of_core(int core)
826{
827 return core << threads_shift;
828}
829EXPORT_SYMBOL_GPL(cpu_first_thread_of_core);
830
Paul Mackerras256f2d42013-08-12 16:29:33 +1000831static void traverse_siblings_chip_id(int cpu, bool add, int chipid)
832{
833 const struct cpumask *mask;
834 struct device_node *np;
835 int i, plen;
836 const __be32 *prop;
837
838 mask = add ? cpu_online_mask : cpu_present_mask;
839 for_each_cpu(i, mask) {
840 np = of_get_cpu_node(i, NULL);
841 if (!np)
842 continue;
843 prop = of_get_property(np, "ibm,chip-id", &plen);
844 if (prop && plen == sizeof(int) &&
845 of_read_number(prop, 1) == chipid) {
846 if (add) {
847 cpumask_set_cpu(cpu, cpu_core_mask(i));
848 cpumask_set_cpu(i, cpu_core_mask(cpu));
849 } else {
850 cpumask_clear_cpu(cpu, cpu_core_mask(i));
851 cpumask_clear_cpu(i, cpu_core_mask(cpu));
852 }
853 }
854 of_node_put(np);
855 }
856}
857
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000858/* Must be called when no change can occur to cpu_present_mask,
Nathan Lynch440a0852008-07-27 15:24:53 +1000859 * i.e. during cpu online or offline.
860 */
861static struct device_node *cpu_to_l2cache(int cpu)
862{
863 struct device_node *np;
Nathan Lynchb2ea25b2008-12-10 20:16:07 +0000864 struct device_node *cache;
Nathan Lynch440a0852008-07-27 15:24:53 +1000865
866 if (!cpu_present(cpu))
867 return NULL;
868
869 np = of_get_cpu_node(cpu, NULL);
870 if (np == NULL)
871 return NULL;
872
Nathan Lynchb2ea25b2008-12-10 20:16:07 +0000873 cache = of_find_next_cache_node(np);
874
Nathan Lynch440a0852008-07-27 15:24:53 +1000875 of_node_put(np);
876
Nathan Lynchb2ea25b2008-12-10 20:16:07 +0000877 return cache;
Nathan Lynch440a0852008-07-27 15:24:53 +1000878}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000880static void traverse_core_siblings(int cpu, bool add)
881{
Paul Mackerras256f2d42013-08-12 16:29:33 +1000882 struct device_node *l2_cache, *np;
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000883 const struct cpumask *mask;
Paul Mackerras256f2d42013-08-12 16:29:33 +1000884 int i, chip, plen;
885 const __be32 *prop;
886
887 /* First see if we have ibm,chip-id properties in cpu nodes */
888 np = of_get_cpu_node(cpu, NULL);
889 if (np) {
890 chip = -1;
891 prop = of_get_property(np, "ibm,chip-id", &plen);
892 if (prop && plen == sizeof(int))
893 chip = of_read_number(prop, 1);
894 of_node_put(np);
895 if (chip >= 0) {
896 traverse_siblings_chip_id(cpu, add, chip);
897 return;
898 }
899 }
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000900
901 l2_cache = cpu_to_l2cache(cpu);
902 mask = add ? cpu_online_mask : cpu_present_mask;
903 for_each_cpu(i, mask) {
Paul Mackerras256f2d42013-08-12 16:29:33 +1000904 np = cpu_to_l2cache(i);
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000905 if (!np)
906 continue;
907 if (np == l2_cache) {
908 if (add) {
909 cpumask_set_cpu(cpu, cpu_core_mask(i));
910 cpumask_set_cpu(i, cpu_core_mask(cpu));
911 } else {
912 cpumask_clear_cpu(cpu, cpu_core_mask(i));
913 cpumask_clear_cpu(i, cpu_core_mask(cpu));
914 }
915 }
916 of_node_put(np);
917 }
918 of_node_put(l2_cache);
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/* Activate a secondary processor. */
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400922void start_secondary(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 unsigned int cpu = smp_processor_id();
Nathan Lynche2075f72008-07-27 15:24:52 +1000925 int i, base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Vegard Nossumf1f10072017-02-27 14:30:07 -0800927 mmgrab(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 current->active_mm = &init_mm;
929
930 smp_store_cpu_info(cpu);
Paul Mackerras5ad57072005-11-05 10:33:55 +1100931 set_dec(tb_ticks_per_jiffy);
Andrew Mortone4d76e12005-11-09 15:45:30 -0800932 preempt_disable();
Michael Ellerman1be6f102014-12-29 15:47:05 +1100933 cpu_callin_map[cpu] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Kumar Gala757cbd42009-09-08 17:38:52 +0000935 if (smp_ops->setup_cpu)
936 smp_ops->setup_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (smp_ops->take_timebase)
938 smp_ops->take_timebase();
939
Tony Breedsd831d0b2007-09-21 13:26:03 +1000940 secondary_cpu_time_init();
941
Benjamin Herrenschmidtaeeafbf2011-03-08 14:49:33 +1100942#ifdef CONFIG_PPC64
943 if (system_state == SYSTEM_RUNNING)
944 vdso_data->processorCount++;
Anton Blanchard18ad51d2012-07-04 20:37:11 +0000945
946 vdso_getcpu_init();
Benjamin Herrenschmidtaeeafbf2011-03-08 14:49:33 +1100947#endif
Nathan Lynche2075f72008-07-27 15:24:52 +1000948 /* Update sibling maps */
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +0000949 base = cpu_first_thread_sibling(cpu);
Nathan Lynche2075f72008-07-27 15:24:52 +1000950 for (i = 0; i < threads_per_core; i++) {
Li Zhongcce606f2013-05-16 18:20:26 +0800951 if (cpu_is_offline(base + i) && (cpu != base + i))
Nathan Lynche2075f72008-07-27 15:24:52 +1000952 continue;
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +0000953 cpumask_set_cpu(cpu, cpu_sibling_mask(base + i));
954 cpumask_set_cpu(base + i, cpu_sibling_mask(cpu));
Nathan Lynch440a0852008-07-27 15:24:53 +1000955
956 /* cpu_core_map should be a superset of
957 * cpu_sibling_map even if we don't have cache
958 * information, so update the former here, too.
959 */
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +0000960 cpumask_set_cpu(cpu, cpu_core_mask(base + i));
961 cpumask_set_cpu(base + i, cpu_core_mask(cpu));
Nathan Lynche2075f72008-07-27 15:24:52 +1000962 }
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000963 traverse_core_siblings(cpu, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Li Zhongbc3c4322014-08-27 17:34:00 +0800965 set_numa_node(numa_cpu_lookup_table[cpu]);
966 set_numa_mem(local_memory_node(numa_cpu_lookup_table[cpu]));
967
Li Zhongcce606f2013-05-16 18:20:26 +0800968 smp_wmb();
969 notify_cpu_starting(cpu);
970 set_cpu_online(cpu, true);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 local_irq_enable();
973
Thomas Gleixnerfc6d73d2016-02-26 18:43:40 +0000974 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
Benjamin Herrenschmidtfa3f82c2011-02-10 18:45:24 +1100975
976 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979int setup_profiling_timer(unsigned int multiplier)
980{
981 return 0;
982}
983
Vincent Guittot607b45e2014-04-11 11:44:39 +0200984#ifdef CONFIG_SCHED_SMT
985/* cpumask of CPUs with asymetric SMT dependancy */
Guenter Roeckb6220ad2014-06-24 18:05:29 -0700986static int powerpc_smt_flags(void)
Vincent Guittot607b45e2014-04-11 11:44:39 +0200987{
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -0400988 int flags = SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
Vincent Guittot607b45e2014-04-11 11:44:39 +0200989
990 if (cpu_has_feature(CPU_FTR_ASYM_SMT)) {
991 printk_once(KERN_INFO "Enabling Asymmetric SMT scheduling\n");
992 flags |= SD_ASYM_PACKING;
993 }
994 return flags;
995}
996#endif
997
998static struct sched_domain_topology_level powerpc_topology[] = {
999#ifdef CONFIG_SCHED_SMT
1000 { cpu_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT) },
1001#endif
1002 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
1003 { NULL, },
1004};
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006void __init smp_cpus_done(unsigned int max_cpus)
1007{
Thomas Gleixner6d11b872017-04-12 22:07:31 +02001008 /*
Michael Ellerman7b7622b2017-07-27 23:23:37 +10001009 * We are running pinned to the boot CPU, see rest_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Kumar Gala757cbd42009-09-08 17:38:52 +00001011 if (smp_ops && smp_ops->setup_cpu)
Michael Ellerman7b7622b2017-07-27 23:23:37 +10001012 smp_ops->setup_cpu(boot_cpuid);
Anton Blanchard4b703a22005-12-13 06:56:47 +11001013
Benjamin Herrenschmidtd7294442011-03-08 13:50:37 +11001014 if (smp_ops && smp_ops->bringup_done)
1015 smp_ops->bringup_done();
1016
Anton Blanchard4b703a22005-12-13 06:56:47 +11001017 dump_numa_cpu_topology();
Benjamin Herrenschmidtd7294442011-03-08 13:50:37 +11001018
Vincent Guittot607b45e2014-04-11 11:44:39 +02001019 set_sched_topology(powerpc_topology);
Michael Neulinge1f0ece2010-08-10 20:02:05 +00001020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022#ifdef CONFIG_HOTPLUG_CPU
1023int __cpu_disable(void)
1024{
Nathan Lynche2075f72008-07-27 15:24:52 +10001025 int cpu = smp_processor_id();
1026 int base, i;
1027 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Nathan Lynche2075f72008-07-27 15:24:52 +10001029 if (!smp_ops->cpu_disable)
1030 return -ENOSYS;
1031
1032 err = smp_ops->cpu_disable();
1033 if (err)
1034 return err;
1035
1036 /* Update sibling maps */
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +00001037 base = cpu_first_thread_sibling(cpu);
Boqun Feng19ab58d2016-08-16 19:50:12 +08001038 for (i = 0; i < threads_per_core && base + i < nr_cpu_ids; i++) {
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +00001039 cpumask_clear_cpu(cpu, cpu_sibling_mask(base + i));
1040 cpumask_clear_cpu(base + i, cpu_sibling_mask(cpu));
1041 cpumask_clear_cpu(cpu, cpu_core_mask(base + i));
1042 cpumask_clear_cpu(base + i, cpu_core_mask(cpu));
Nathan Lynche2075f72008-07-27 15:24:52 +10001043 }
Paul Mackerrasa8a53562013-08-12 16:28:47 +10001044 traverse_core_siblings(cpu, false);
Nathan Lynch440a0852008-07-27 15:24:53 +10001045
Nathan Lynche2075f72008-07-27 15:24:52 +10001046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049void __cpu_die(unsigned int cpu)
1050{
1051 if (smp_ops->cpu_die)
1052 smp_ops->cpu_die(cpu);
1053}
Nathan Fontenotd0174c72010-01-14 09:52:44 +00001054
Milton Millerabb17f92010-05-19 02:56:29 +00001055void cpu_die(void)
1056{
1057 if (ppc_md.cpu_die)
1058 ppc_md.cpu_die();
Benjamin Herrenschmidtfa3f82c2011-02-10 18:45:24 +11001059
1060 /* If we return, we re-enter start_secondary */
1061 start_secondary_resume();
Milton Millerabb17f92010-05-19 02:56:29 +00001062}
Benjamin Herrenschmidtfa3f82c2011-02-10 18:45:24 +11001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064#endif