blob: d9d8bb0f44546a5de5d089ffc2178beaa7704abf [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/sched.h>
23#include <linux/smp.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/spinlock.h>
28#include <linux/cache.h>
29#include <linux/err.h>
Kay Sievers8a25a2f2011-12-21 14:29:42 -080030#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/cpu.h>
32#include <linux/notifier.h>
Anton Blanchard4b703a22005-12-13 06:56:47 +110033#include <linux/topology.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <asm/ptrace.h>
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/irq.h>
38#include <asm/page.h>
39#include <asm/pgtable.h>
40#include <asm/prom.h>
41#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/time.h>
43#include <asm/machdep.h>
Nathan Lynche2075f72008-07-27 15:24:52 +100044#include <asm/cputhreads.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/cputable.h>
Stephen Rothwellbbeb3f42005-09-27 13:51:59 +100046#include <asm/mpic.h>
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +110047#include <asm/vdso_datapage.h>
Paul Mackerras5ad57072005-11-05 10:33:55 +110048#ifdef CONFIG_PPC64
49#include <asm/paca.h>
50#endif
Anton Blanchard18ad51d2012-07-04 20:37:11 +000051#include <asm/vdso.h>
David Howellsae3a1972012-03-28 18:30:02 +010052#include <asm/debug.h>
Paul Mackerras5ad57072005-11-05 10:33:55 +110053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#ifdef DEBUG
Michael Ellermanf9e4ec52005-11-15 15:16:38 +110055#include <asm/udbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define DBG(fmt...) udbg_printf(fmt)
57#else
58#define DBG(fmt...)
59#endif
60
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +110061#ifdef CONFIG_HOTPLUG_CPU
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +000062/* State of each CPU during hotplug phases */
63static DEFINE_PER_CPU(int, cpu_state) = { 0 };
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +110064#endif
65
Michael Ellermanf9e4ec52005-11-15 15:16:38 +110066struct thread_info *secondary_ti;
67
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +000068DEFINE_PER_CPU(cpumask_var_t, cpu_sibling_map);
69DEFINE_PER_CPU(cpumask_var_t, cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Mike Travisd5a74302007-10-16 01:24:05 -070071EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
Nathan Lynch440a0852008-07-27 15:24:53 +100072EXPORT_PER_CPU_SYMBOL(cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Paul Mackerras5ad57072005-11-05 10:33:55 +110074/* SMP operations for this machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct smp_ops_t *smp_ops;
76
Benjamin Herrenschmidt7ccbe502009-06-18 23:30:07 +000077/* Can't be static due to PowerMac hackery */
78volatile unsigned int cpu_callin_map[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080int smt_enabled_at_boot = 1;
81
Michael Ellermancc532912005-12-04 18:39:43 +110082static void (*crash_ipi_function_ptr)(struct pt_regs *) = NULL;
83
Andy Fleming3cd85252013-08-05 14:58:34 -050084/*
85 * Returns 1 if the specified cpu should be brought up during boot.
86 * Used to inhibit booting threads if they've been disabled or
87 * limited on the command line
88 */
89int smp_generic_cpu_bootable(unsigned int nr)
90{
91 /* Special case - we inhibit secondary thread startup
92 * during boot if the user requests it.
93 */
94 if (system_state == SYSTEM_BOOTING && cpu_has_feature(CPU_FTR_SMT)) {
95 if (!smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
96 return 0;
97 if (smt_enabled_at_boot
98 && cpu_thread_in_core(nr) >= smt_enabled_at_boot)
99 return 0;
100 }
101
102 return 1;
103}
104
105
Paul Mackerras5ad57072005-11-05 10:33:55 +1100106#ifdef CONFIG_PPC64
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800107int smp_generic_kick_cpu(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 BUG_ON(nr < 0 || nr >= NR_CPUS);
110
111 /*
112 * The processor is currently spinning, waiting for the
113 * cpu_start field to become non-zero After we set cpu_start,
114 * the processor will continue on to secondary_start
115 */
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000116 if (!paca[nr].cpu_start) {
117 paca[nr].cpu_start = 1;
118 smp_mb();
119 return 0;
120 }
121
122#ifdef CONFIG_HOTPLUG_CPU
123 /*
124 * Ok it's not there, so it might be soft-unplugged, let's
125 * try to bring it back
126 */
Zhao Chenhuiae5cab42012-07-20 20:42:34 +0800127 generic_set_cpu_up(nr);
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000128 smp_wmb();
129 smp_send_reschedule(nr);
130#endif /* CONFIG_HOTPLUG_CPU */
Michael Ellermande300972011-04-11 21:46:19 +0000131
132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000134#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Milton Miller25ddd732008-11-14 20:11:49 +0000136static irqreturn_t call_function_action(int irq, void *data)
137{
138 generic_smp_call_function_interrupt();
139 return IRQ_HANDLED;
140}
141
142static irqreturn_t reschedule_action(int irq, void *data)
143{
Peter Zijlstra184748c2011-04-05 17:23:39 +0200144 scheduler_ipi();
Milton Miller25ddd732008-11-14 20:11:49 +0000145 return IRQ_HANDLED;
146}
147
148static irqreturn_t call_function_single_action(int irq, void *data)
149{
150 generic_smp_call_function_single_interrupt();
151 return IRQ_HANDLED;
152}
153
Milton Miller7ef71d72011-05-24 20:34:18 +0000154static irqreturn_t debug_ipi_action(int irq, void *data)
Milton Miller25ddd732008-11-14 20:11:49 +0000155{
Milton Miller23d72bf2011-05-10 19:29:39 +0000156 if (crash_ipi_function_ptr) {
157 crash_ipi_function_ptr(get_irq_regs());
158 return IRQ_HANDLED;
159 }
160
161#ifdef CONFIG_DEBUGGER
162 debugger_ipi(get_irq_regs());
163#endif /* CONFIG_DEBUGGER */
164
Milton Miller25ddd732008-11-14 20:11:49 +0000165 return IRQ_HANDLED;
166}
167
168static irq_handler_t smp_ipi_action[] = {
169 [PPC_MSG_CALL_FUNCTION] = call_function_action,
170 [PPC_MSG_RESCHEDULE] = reschedule_action,
171 [PPC_MSG_CALL_FUNC_SINGLE] = call_function_single_action,
172 [PPC_MSG_DEBUGGER_BREAK] = debug_ipi_action,
173};
174
175const char *smp_ipi_name[] = {
176 [PPC_MSG_CALL_FUNCTION] = "ipi call function",
177 [PPC_MSG_RESCHEDULE] = "ipi reschedule",
178 [PPC_MSG_CALL_FUNC_SINGLE] = "ipi call function single",
179 [PPC_MSG_DEBUGGER_BREAK] = "ipi debugger",
180};
181
182/* optional function to request ipi, for controllers with >= 4 ipis */
183int smp_request_message_ipi(int virq, int msg)
184{
185 int err;
186
187 if (msg < 0 || msg > PPC_MSG_DEBUGGER_BREAK) {
188 return -EINVAL;
189 }
190#if !defined(CONFIG_DEBUGGER) && !defined(CONFIG_KEXEC)
191 if (msg == PPC_MSG_DEBUGGER_BREAK) {
192 return 1;
193 }
194#endif
Thomas Gleixner3b5e16d2011-10-05 02:30:50 +0000195 err = request_irq(virq, smp_ipi_action[msg],
Zhao Chenhuie6651de2012-07-20 20:47:01 +0800196 IRQF_PERCPU | IRQF_NO_THREAD | IRQF_NO_SUSPEND,
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000197 smp_ipi_name[msg], NULL);
Milton Miller25ddd732008-11-14 20:11:49 +0000198 WARN(err < 0, "unable to request_irq %d for %s (rc %d)\n",
199 virq, smp_ipi_name[msg], err);
200
201 return err;
202}
203
Milton Miller1ece3552011-05-10 19:29:42 +0000204#ifdef CONFIG_PPC_SMP_MUXED_IPI
Milton Miller23d72bf2011-05-10 19:29:39 +0000205struct cpu_messages {
Milton Miller71454272011-05-10 19:29:46 +0000206 int messages; /* current messages */
Milton Miller23d72bf2011-05-10 19:29:39 +0000207 unsigned long data; /* data for cause ipi */
208};
209static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpu_messages, ipi_message);
210
211void smp_muxed_ipi_set_data(int cpu, unsigned long data)
212{
213 struct cpu_messages *info = &per_cpu(ipi_message, cpu);
214
215 info->data = data;
216}
217
218void smp_muxed_ipi_message_pass(int cpu, int msg)
219{
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;
Paul Mackerras9fb1b362012-09-04 18:33:08 +0000228 /*
229 * cause_ipi functions are required to include a full barrier
230 * before doing whatever causes the IPI.
231 */
Milton Miller23d72bf2011-05-10 19:29:39 +0000232 smp_ops->cause_ipi(cpu, info->data);
233}
234
Milton Miller23d72bf2011-05-10 19:29:39 +0000235irqreturn_t smp_ipi_demux(void)
236{
237 struct cpu_messages *info = &__get_cpu_var(ipi_message);
Milton Miller71454272011-05-10 19:29:46 +0000238 unsigned int all;
Milton Miller23d72bf2011-05-10 19:29:39 +0000239
240 mb(); /* order any irq clear */
Milton Miller71454272011-05-10 19:29:46 +0000241
242 do {
Paul Mackerras9fb1b362012-09-04 18:33:08 +0000243 all = xchg(&info->messages, 0);
Milton Miller71454272011-05-10 19:29:46 +0000244
245#ifdef __BIG_ENDIAN
246 if (all & (1 << (24 - 8 * PPC_MSG_CALL_FUNCTION)))
Milton Miller23d72bf2011-05-10 19:29:39 +0000247 generic_smp_call_function_interrupt();
Milton Miller71454272011-05-10 19:29:46 +0000248 if (all & (1 << (24 - 8 * PPC_MSG_RESCHEDULE)))
Benjamin Herrenschmidt880102e2011-05-20 15:36:52 +1000249 scheduler_ipi();
Milton Miller71454272011-05-10 19:29:46 +0000250 if (all & (1 << (24 - 8 * PPC_MSG_CALL_FUNC_SINGLE)))
Milton Miller23d72bf2011-05-10 19:29:39 +0000251 generic_smp_call_function_single_interrupt();
Milton Miller71454272011-05-10 19:29:46 +0000252 if (all & (1 << (24 - 8 * PPC_MSG_DEBUGGER_BREAK)))
Milton Miller23d72bf2011-05-10 19:29:39 +0000253 debug_ipi_action(0, NULL);
Milton Miller71454272011-05-10 19:29:46 +0000254#else
255#error Unsupported ENDIAN
Milton Miller23d72bf2011-05-10 19:29:39 +0000256#endif
Milton Miller71454272011-05-10 19:29:46 +0000257 } while (info->messages);
258
Milton Miller23d72bf2011-05-10 19:29:39 +0000259 return IRQ_HANDLED;
260}
Milton Miller1ece3552011-05-10 19:29:42 +0000261#endif /* CONFIG_PPC_SMP_MUXED_IPI */
Milton Miller23d72bf2011-05-10 19:29:39 +0000262
Paul Mackerras9ca980d2011-05-25 23:34:12 +0000263static inline void do_message_pass(int cpu, int msg)
264{
265 if (smp_ops->message_pass)
266 smp_ops->message_pass(cpu, msg);
267#ifdef CONFIG_PPC_SMP_MUXED_IPI
268 else
269 smp_muxed_ipi_message_pass(cpu, msg);
270#endif
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273void smp_send_reschedule(int cpu)
274{
Benjamin Herrenschmidt8cffc6a2006-07-04 14:09:36 +1000275 if (likely(smp_ops))
Paul Mackerras9ca980d2011-05-25 23:34:12 +0000276 do_message_pass(cpu, PPC_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000278EXPORT_SYMBOL_GPL(smp_send_reschedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jens Axboeb7d7a242008-06-26 11:22:13 +0200280void arch_send_call_function_single_ipi(int cpu)
281{
Paul Mackerras9ca980d2011-05-25 23:34:12 +0000282 do_message_pass(cpu, PPC_MSG_CALL_FUNC_SINGLE);
Jens Axboeb7d7a242008-06-26 11:22:13 +0200283}
284
Rusty Russellf063ea02009-09-24 09:34:45 -0600285void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Jens Axboeb7d7a242008-06-26 11:22:13 +0200286{
287 unsigned int cpu;
288
Rusty Russellf063ea02009-09-24 09:34:45 -0600289 for_each_cpu(cpu, mask)
Paul Mackerras9ca980d2011-05-25 23:34:12 +0000290 do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
Jens Axboeb7d7a242008-06-26 11:22:13 +0200291}
292
Milton Millere0476372011-05-10 19:29:06 +0000293#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
294void smp_send_debugger_break(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Milton Millere0476372011-05-10 19:29:06 +0000296 int cpu;
297 int me = raw_smp_processor_id();
298
299 if (unlikely(!smp_ops))
300 return;
301
302 for_each_online_cpu(cpu)
303 if (cpu != me)
Paul Mackerras9ca980d2011-05-25 23:34:12 +0000304 do_message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306#endif
307
Michael Ellermancc532912005-12-04 18:39:43 +1100308#ifdef CONFIG_KEXEC
309void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
310{
311 crash_ipi_function_ptr = crash_ipi_callback;
Milton Millere0476372011-05-10 19:29:06 +0000312 if (crash_ipi_callback) {
Michael Ellermancc532912005-12-04 18:39:43 +1100313 mb();
Milton Millere0476372011-05-10 19:29:06 +0000314 smp_send_debugger_break();
Michael Ellermancc532912005-12-04 18:39:43 +1100315 }
316}
317#endif
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static void stop_this_cpu(void *dummy)
320{
Valentine Barshak8389b372009-11-25 11:48:52 +0000321 /* Remove this CPU */
322 set_cpu_online(smp_processor_id(), false);
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 local_irq_disable();
325 while (1)
326 ;
327}
328
Satyam Sharma8fd76752007-09-18 09:43:40 +1000329void smp_send_stop(void)
330{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200331 smp_call_function(stop_this_cpu, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334struct thread_info *current_set[NR_CPUS];
335
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800336static void smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Tejun Heo6b7487f2009-10-29 22:34:14 +0900338 per_cpu(cpu_pvr, id) = mfspr(SPRN_PVR);
Becky Bruce3160b092011-06-28 14:54:47 -0500339#ifdef CONFIG_PPC_FSL_BOOK3E
340 per_cpu(next_tlbcam_idx, id)
341 = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
342#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345void __init smp_prepare_cpus(unsigned int max_cpus)
346{
347 unsigned int cpu;
348
349 DBG("smp_prepare_cpus\n");
350
351 /*
352 * setup_cpu may need to be called on the boot cpu. We havent
353 * spun any cpus up but lets be paranoid.
354 */
355 BUG_ON(boot_cpuid != smp_processor_id());
356
357 /* Fixup boot cpu */
358 smp_store_cpu_info(boot_cpuid);
359 cpu_callin_map[boot_cpuid] = 1;
360
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +0000361 for_each_possible_cpu(cpu) {
362 zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu),
363 GFP_KERNEL, cpu_to_node(cpu));
364 zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu),
365 GFP_KERNEL, cpu_to_node(cpu));
366 }
367
368 cpumask_set_cpu(boot_cpuid, cpu_sibling_mask(boot_cpuid));
369 cpumask_set_cpu(boot_cpuid, cpu_core_mask(boot_cpuid));
370
Benjamin Herrenschmidt8cffc6a2006-07-04 14:09:36 +1000371 if (smp_ops)
Kumar Gala757cbd42009-09-08 17:38:52 +0000372 if (smp_ops->probe)
373 max_cpus = smp_ops->probe();
374 else
375 max_cpus = NR_CPUS;
Benjamin Herrenschmidt8cffc6a2006-07-04 14:09:36 +1000376 else
377 max_cpus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800380void smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 BUG_ON(smp_processor_id() != boot_cpuid);
Paul Mackerras5ad57072005-11-05 10:33:55 +1100383#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 paca[boot_cpuid].__current = current;
Paul Mackerras5ad57072005-11-05 10:33:55 +1100385#endif
Al Virob5e2fc12006-01-12 01:06:01 -0800386 current_set[boot_cpuid] = task_thread_info(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391int generic_cpu_disable(void)
392{
393 unsigned int cpu = smp_processor_id();
394
395 if (cpu == boot_cpuid)
396 return -EBUSY;
397
Rusty Russellea0f1ca2009-09-24 09:34:48 -0600398 set_cpu_online(cpu, false);
Paul Mackerras799d6042005-11-10 13:37:51 +1100399#ifdef CONFIG_PPC64
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +1100400 vdso_data->processorCount--;
Paul Mackerras094fe2e2005-11-10 14:26:12 +1100401#endif
Benjamin Herrenschmidt1c91cc52011-02-11 13:05:17 +1100402 migrate_irqs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406void generic_cpu_die(unsigned int cpu)
407{
408 int i;
409
410 for (i = 0; i < 100; i++) {
Anton Blanchard0d8d4d42005-05-01 08:58:47 -0700411 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (per_cpu(cpu_state, cpu) == CPU_DEAD)
413 return;
414 msleep(100);
415 }
416 printk(KERN_ERR "CPU%d didn't die...\n", cpu);
417}
418
419void generic_mach_cpu_die(void)
420{
421 unsigned int cpu;
422
423 local_irq_disable();
Benjamin Herrenschmidt4fcb8832011-02-10 18:46:50 +1100424 idle_task_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 cpu = smp_processor_id();
426 printk(KERN_DEBUG "CPU%d offline\n", cpu);
427 __get_cpu_var(cpu_state) = CPU_DEAD;
Anton Blanchard0d8d4d42005-05-01 08:58:47 -0700428 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
430 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
Benjamin Herrenschmidt105765f2011-04-01 09:23:37 +1100432
433void generic_set_cpu_dead(unsigned int cpu)
434{
435 per_cpu(cpu_state, cpu) = CPU_DEAD;
436}
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000437
Zhao Chenhuiae5cab42012-07-20 20:42:34 +0800438/*
439 * The cpu_state should be set to CPU_UP_PREPARE in kick_cpu(), otherwise
440 * the cpu_state is always CPU_DEAD after calling generic_set_cpu_dead(),
441 * which makes the delay in generic_cpu_die() not happen.
442 */
443void generic_set_cpu_up(unsigned int cpu)
444{
445 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
446}
447
Benjamin Herrenschmidtfb82b832011-09-19 17:44:49 +0000448int generic_check_cpu_restart(unsigned int cpu)
449{
450 return per_cpu(cpu_state, cpu) == CPU_UP_PREPARE;
451}
Paul Mackerras512691d2012-10-15 01:15:41 +0000452
453static atomic_t secondary_inhibit_count;
454
455/*
456 * Don't allow secondary CPU threads to come online
457 */
458void inhibit_secondary_onlining(void)
459{
460 /*
461 * This makes secondary_inhibit_count stable during cpu
462 * online/offline operations.
463 */
464 get_online_cpus();
465
466 atomic_inc(&secondary_inhibit_count);
467 put_online_cpus();
468}
469EXPORT_SYMBOL_GPL(inhibit_secondary_onlining);
470
471/*
472 * Allow secondary CPU threads to come online again
473 */
474void uninhibit_secondary_onlining(void)
475{
476 get_online_cpus();
477 atomic_dec(&secondary_inhibit_count);
478 put_online_cpus();
479}
480EXPORT_SYMBOL_GPL(uninhibit_secondary_onlining);
481
482static int secondaries_inhibited(void)
483{
484 return atomic_read(&secondary_inhibit_count);
485}
486
487#else /* HOTPLUG_CPU */
488
489#define secondaries_inhibited() 0
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#endif
492
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000493static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100494{
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000495 struct thread_info *ti = task_thread_info(idle);
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100496
497#ifdef CONFIG_PPC64
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000498 paca[cpu].__current = idle;
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100499 paca[cpu].kstack = (unsigned long)ti + THREAD_SIZE - STACK_FRAME_OVERHEAD;
500#endif
501 ti->cpu = cpu;
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000502 secondary_ti = current_set[cpu] = ti;
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100503}
504
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400505int __cpu_up(unsigned int cpu, struct task_struct *tidle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Benjamin Herrenschmidtc56e5852011-03-08 14:40:04 +1100507 int rc, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Paul Mackerras512691d2012-10-15 01:15:41 +0000509 /*
510 * Don't allow secondary threads to come online if inhibited
511 */
512 if (threads_per_core > 1 && secondaries_inhibited() &&
513 cpu % threads_per_core != 0)
514 return -EBUSY;
515
Benjamin Herrenschmidt8cffc6a2006-07-04 14:09:36 +1000516 if (smp_ops == NULL ||
517 (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -EINVAL;
519
Thomas Gleixner17e32ea2012-04-20 13:05:48 +0000520 cpu_idle_thread_init(cpu, tidle);
kerstin jonssonc560bbc2011-05-17 23:57:11 +0000521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Make sure callin-map entry is 0 (can be leftover a CPU
523 * hotplug
524 */
525 cpu_callin_map[cpu] = 0;
526
527 /* The information for processor bringup must
528 * be written out to main store before we release
529 * the processor.
530 */
Anton Blanchard0d8d4d42005-05-01 08:58:47 -0700531 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* wake up cpus */
534 DBG("smp: kicking cpu %d\n", cpu);
Michael Ellermande300972011-04-11 21:46:19 +0000535 rc = smp_ops->kick_cpu(cpu);
536 if (rc) {
537 pr_err("smp: failed starting cpu %d (rc %d)\n", cpu, rc);
538 return rc;
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /*
542 * wait to see if the cpu made a callin (is actually up).
543 * use this value that I found through experimentation.
544 * -- Cort
545 */
546 if (system_state < SYSTEM_RUNNING)
Jon Loeligeree0339f2006-06-17 17:52:44 -0500547 for (c = 50000; c && !cpu_callin_map[cpu]; c--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 udelay(100);
549#ifdef CONFIG_HOTPLUG_CPU
550 else
551 /*
552 * CPUs can take much longer to come up in the
553 * hotplug case. Wait five seconds.
554 */
Gautham R Shenoy67764262009-06-23 23:26:37 +0000555 for (c = 5000; c && !cpu_callin_map[cpu]; c--)
556 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
558
559 if (!cpu_callin_map[cpu]) {
Signed-off-by: Darren Hart6685a472010-08-04 18:28:34 +0000560 printk(KERN_ERR "Processor %u is stuck.\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -ENOENT;
562 }
563
Signed-off-by: Darren Hart6685a472010-08-04 18:28:34 +0000564 DBG("Processor %u found.\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (smp_ops->give_timebase)
567 smp_ops->give_timebase();
568
569 /* Wait until cpu puts itself in the online map */
570 while (!cpu_online(cpu))
571 cpu_relax();
572
573 return 0;
574}
575
Nathan Lynche9efed32008-07-27 15:24:54 +1000576/* Return the value of the reg property corresponding to the given
577 * logical cpu.
578 */
579int cpu_to_core_id(int cpu)
580{
581 struct device_node *np;
582 const int *reg;
583 int id = -1;
584
585 np = of_get_cpu_node(cpu, NULL);
586 if (!np)
587 goto out;
588
589 reg = of_get_property(np, "reg", NULL);
590 if (!reg)
591 goto out;
592
593 id = *reg;
594out:
595 of_node_put(np);
596 return id;
597}
598
Vasant Hegde15863ff2013-08-12 17:35:57 +0530599/* Return the value of the chip-id property corresponding
600 * to the given logical cpu.
601 */
602int cpu_to_chip_id(int cpu)
603{
604 struct device_node *np;
605
606 np = of_get_cpu_node(cpu, NULL);
607 if (!np)
608 return -1;
609
610 of_node_put(np);
611 return of_get_ibm_chip_id(np);
612}
613
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +0000614/* Helper routines for cpu to core mapping */
615int cpu_core_index_of_thread(int cpu)
616{
617 return cpu >> threads_shift;
618}
619EXPORT_SYMBOL_GPL(cpu_core_index_of_thread);
620
621int cpu_first_thread_of_core(int core)
622{
623 return core << threads_shift;
624}
625EXPORT_SYMBOL_GPL(cpu_first_thread_of_core);
626
Paul Mackerras256f2d42013-08-12 16:29:33 +1000627static void traverse_siblings_chip_id(int cpu, bool add, int chipid)
628{
629 const struct cpumask *mask;
630 struct device_node *np;
631 int i, plen;
632 const __be32 *prop;
633
634 mask = add ? cpu_online_mask : cpu_present_mask;
635 for_each_cpu(i, mask) {
636 np = of_get_cpu_node(i, NULL);
637 if (!np)
638 continue;
639 prop = of_get_property(np, "ibm,chip-id", &plen);
640 if (prop && plen == sizeof(int) &&
641 of_read_number(prop, 1) == chipid) {
642 if (add) {
643 cpumask_set_cpu(cpu, cpu_core_mask(i));
644 cpumask_set_cpu(i, cpu_core_mask(cpu));
645 } else {
646 cpumask_clear_cpu(cpu, cpu_core_mask(i));
647 cpumask_clear_cpu(i, cpu_core_mask(cpu));
648 }
649 }
650 of_node_put(np);
651 }
652}
653
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000654/* Must be called when no change can occur to cpu_present_mask,
Nathan Lynch440a0852008-07-27 15:24:53 +1000655 * i.e. during cpu online or offline.
656 */
657static struct device_node *cpu_to_l2cache(int cpu)
658{
659 struct device_node *np;
Nathan Lynchb2ea25b2008-12-10 20:16:07 +0000660 struct device_node *cache;
Nathan Lynch440a0852008-07-27 15:24:53 +1000661
662 if (!cpu_present(cpu))
663 return NULL;
664
665 np = of_get_cpu_node(cpu, NULL);
666 if (np == NULL)
667 return NULL;
668
Nathan Lynchb2ea25b2008-12-10 20:16:07 +0000669 cache = of_find_next_cache_node(np);
670
Nathan Lynch440a0852008-07-27 15:24:53 +1000671 of_node_put(np);
672
Nathan Lynchb2ea25b2008-12-10 20:16:07 +0000673 return cache;
Nathan Lynch440a0852008-07-27 15:24:53 +1000674}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000676static void traverse_core_siblings(int cpu, bool add)
677{
Paul Mackerras256f2d42013-08-12 16:29:33 +1000678 struct device_node *l2_cache, *np;
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000679 const struct cpumask *mask;
Paul Mackerras256f2d42013-08-12 16:29:33 +1000680 int i, chip, plen;
681 const __be32 *prop;
682
683 /* First see if we have ibm,chip-id properties in cpu nodes */
684 np = of_get_cpu_node(cpu, NULL);
685 if (np) {
686 chip = -1;
687 prop = of_get_property(np, "ibm,chip-id", &plen);
688 if (prop && plen == sizeof(int))
689 chip = of_read_number(prop, 1);
690 of_node_put(np);
691 if (chip >= 0) {
692 traverse_siblings_chip_id(cpu, add, chip);
693 return;
694 }
695 }
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000696
697 l2_cache = cpu_to_l2cache(cpu);
698 mask = add ? cpu_online_mask : cpu_present_mask;
699 for_each_cpu(i, mask) {
Paul Mackerras256f2d42013-08-12 16:29:33 +1000700 np = cpu_to_l2cache(i);
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000701 if (!np)
702 continue;
703 if (np == l2_cache) {
704 if (add) {
705 cpumask_set_cpu(cpu, cpu_core_mask(i));
706 cpumask_set_cpu(i, cpu_core_mask(cpu));
707 } else {
708 cpumask_clear_cpu(cpu, cpu_core_mask(i));
709 cpumask_clear_cpu(i, cpu_core_mask(cpu));
710 }
711 }
712 of_node_put(np);
713 }
714 of_node_put(l2_cache);
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/* Activate a secondary processor. */
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400718void start_secondary(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 unsigned int cpu = smp_processor_id();
Nathan Lynche2075f72008-07-27 15:24:52 +1000721 int i, base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 atomic_inc(&init_mm.mm_count);
724 current->active_mm = &init_mm;
725
726 smp_store_cpu_info(cpu);
Paul Mackerras5ad57072005-11-05 10:33:55 +1100727 set_dec(tb_ticks_per_jiffy);
Andrew Mortone4d76e12005-11-09 15:45:30 -0800728 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 cpu_callin_map[cpu] = 1;
730
Kumar Gala757cbd42009-09-08 17:38:52 +0000731 if (smp_ops->setup_cpu)
732 smp_ops->setup_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (smp_ops->take_timebase)
734 smp_ops->take_timebase();
735
Tony Breedsd831d0b2007-09-21 13:26:03 +1000736 secondary_cpu_time_init();
737
Benjamin Herrenschmidtaeeafbf2011-03-08 14:49:33 +1100738#ifdef CONFIG_PPC64
739 if (system_state == SYSTEM_RUNNING)
740 vdso_data->processorCount++;
Anton Blanchard18ad51d2012-07-04 20:37:11 +0000741
742 vdso_getcpu_init();
Benjamin Herrenschmidtaeeafbf2011-03-08 14:49:33 +1100743#endif
Nathan Lynche2075f72008-07-27 15:24:52 +1000744 /* Update sibling maps */
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +0000745 base = cpu_first_thread_sibling(cpu);
Nathan Lynche2075f72008-07-27 15:24:52 +1000746 for (i = 0; i < threads_per_core; i++) {
Li Zhongcce606f2013-05-16 18:20:26 +0800747 if (cpu_is_offline(base + i) && (cpu != base + i))
Nathan Lynche2075f72008-07-27 15:24:52 +1000748 continue;
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +0000749 cpumask_set_cpu(cpu, cpu_sibling_mask(base + i));
750 cpumask_set_cpu(base + i, cpu_sibling_mask(cpu));
Nathan Lynch440a0852008-07-27 15:24:53 +1000751
752 /* cpu_core_map should be a superset of
753 * cpu_sibling_map even if we don't have cache
754 * information, so update the former here, too.
755 */
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +0000756 cpumask_set_cpu(cpu, cpu_core_mask(base + i));
757 cpumask_set_cpu(base + i, cpu_core_mask(cpu));
Nathan Lynche2075f72008-07-27 15:24:52 +1000758 }
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000759 traverse_core_siblings(cpu, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Li Zhongcce606f2013-05-16 18:20:26 +0800761 smp_wmb();
762 notify_cpu_starting(cpu);
763 set_cpu_online(cpu, true);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 local_irq_enable();
766
Thomas Gleixner799fef02013-03-21 22:49:56 +0100767 cpu_startup_entry(CPUHP_ONLINE);
Benjamin Herrenschmidtfa3f82c2011-02-10 18:45:24 +1100768
769 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772int setup_profiling_timer(unsigned int multiplier)
773{
774 return 0;
775}
776
777void __init smp_cpus_done(unsigned int max_cpus)
778{
Anton Blanchardbfb91262010-04-26 15:32:34 +0000779 cpumask_var_t old_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* We want the setup_cpu() here to be called from CPU 0, but our
782 * init thread may have been "borrowed" by another CPU in the meantime
783 * se we pin us down to CPU 0 for a short while
784 */
Anton Blanchardbfb91262010-04-26 15:32:34 +0000785 alloc_cpumask_var(&old_mask, GFP_NOWAIT);
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000786 cpumask_copy(old_mask, tsk_cpus_allowed(current));
Julia Lawall21dbeb92010-03-26 12:03:29 +0000787 set_cpus_allowed_ptr(current, cpumask_of(boot_cpuid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Kumar Gala757cbd42009-09-08 17:38:52 +0000789 if (smp_ops && smp_ops->setup_cpu)
Benjamin Herrenschmidt8cffc6a2006-07-04 14:09:36 +1000790 smp_ops->setup_cpu(boot_cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Anton Blanchardbfb91262010-04-26 15:32:34 +0000792 set_cpus_allowed_ptr(current, old_mask);
793
794 free_cpumask_var(old_mask);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100795
Benjamin Herrenschmidtd7294442011-03-08 13:50:37 +1100796 if (smp_ops && smp_ops->bringup_done)
797 smp_ops->bringup_done();
798
Anton Blanchard4b703a22005-12-13 06:56:47 +1100799 dump_numa_cpu_topology();
Benjamin Herrenschmidtd7294442011-03-08 13:50:37 +1100800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Michael Neulinge1f0ece2010-08-10 20:02:05 +0000803int arch_sd_sibling_asym_packing(void)
804{
805 if (cpu_has_feature(CPU_FTR_ASYM_SMT)) {
806 printk_once(KERN_INFO "Enabling Asymmetric SMT scheduling\n");
807 return SD_ASYM_PACKING;
808 }
809 return 0;
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#ifdef CONFIG_HOTPLUG_CPU
813int __cpu_disable(void)
814{
Nathan Lynche2075f72008-07-27 15:24:52 +1000815 int cpu = smp_processor_id();
816 int base, i;
817 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Nathan Lynche2075f72008-07-27 15:24:52 +1000819 if (!smp_ops->cpu_disable)
820 return -ENOSYS;
821
822 err = smp_ops->cpu_disable();
823 if (err)
824 return err;
825
826 /* Update sibling maps */
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +0000827 base = cpu_first_thread_sibling(cpu);
Nathan Lynche2075f72008-07-27 15:24:52 +1000828 for (i = 0; i < threads_per_core; i++) {
Anton Blanchardcc1ba8e2010-04-26 15:32:41 +0000829 cpumask_clear_cpu(cpu, cpu_sibling_mask(base + i));
830 cpumask_clear_cpu(base + i, cpu_sibling_mask(cpu));
831 cpumask_clear_cpu(cpu, cpu_core_mask(base + i));
832 cpumask_clear_cpu(base + i, cpu_core_mask(cpu));
Nathan Lynche2075f72008-07-27 15:24:52 +1000833 }
Paul Mackerrasa8a53562013-08-12 16:28:47 +1000834 traverse_core_siblings(cpu, false);
Nathan Lynch440a0852008-07-27 15:24:53 +1000835
Nathan Lynche2075f72008-07-27 15:24:52 +1000836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839void __cpu_die(unsigned int cpu)
840{
841 if (smp_ops->cpu_die)
842 smp_ops->cpu_die(cpu);
843}
Nathan Fontenotd0174c72010-01-14 09:52:44 +0000844
845static DEFINE_MUTEX(powerpc_cpu_hotplug_driver_mutex);
846
847void cpu_hotplug_driver_lock()
848{
849 mutex_lock(&powerpc_cpu_hotplug_driver_mutex);
850}
851
852void cpu_hotplug_driver_unlock()
853{
854 mutex_unlock(&powerpc_cpu_hotplug_driver_mutex);
855}
Milton Millerabb17f92010-05-19 02:56:29 +0000856
857void cpu_die(void)
858{
859 if (ppc_md.cpu_die)
860 ppc_md.cpu_die();
Benjamin Herrenschmidtfa3f82c2011-02-10 18:45:24 +1100861
862 /* If we return, we re-enter start_secondary */
863 start_secondary_resume();
Milton Millerabb17f92010-05-19 02:56:29 +0000864}
Benjamin Herrenschmidtfa3f82c2011-02-10 18:45:24 +1100865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866#endif