smp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  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. #undef DEBUG
  18. #include <linux/kernel.h>
  19. #include <linux/export.h>
  20. #include <linux/sched/mm.h>
  21. #include <linux/sched/topology.h>
  22. #include <linux/smp.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/cache.h>
  28. #include <linux/err.h>
  29. #include <linux/device.h>
  30. #include <linux/cpu.h>
  31. #include <linux/notifier.h>
  32. #include <linux/topology.h>
  33. #include <linux/profile.h>
  34. #include <asm/ptrace.h>
  35. #include <linux/atomic.h>
  36. #include <asm/irq.h>
  37. #include <asm/hw_irq.h>
  38. #include <asm/kvm_ppc.h>
  39. #include <asm/dbell.h>
  40. #include <asm/page.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/prom.h>
  43. #include <asm/smp.h>
  44. #include <asm/time.h>
  45. #include <asm/machdep.h>
  46. #include <asm/cputhreads.h>
  47. #include <asm/cputable.h>
  48. #include <asm/mpic.h>
  49. #include <asm/vdso_datapage.h>
  50. #ifdef CONFIG_PPC64
  51. #include <asm/paca.h>
  52. #endif
  53. #include <asm/vdso.h>
  54. #include <asm/debug.h>
  55. #include <asm/kexec.h>
  56. #include <asm/asm-prototypes.h>
  57. #include <asm/cpu_has_feature.h>
  58. #ifdef DEBUG
  59. #include <asm/udbg.h>
  60. #define DBG(fmt...) udbg_printf(fmt)
  61. #else
  62. #define DBG(fmt...)
  63. #endif
  64. #ifdef CONFIG_HOTPLUG_CPU
  65. /* State of each CPU during hotplug phases */
  66. static DEFINE_PER_CPU(int, cpu_state) = { 0 };
  67. #endif
  68. struct thread_info *secondary_ti;
  69. DEFINE_PER_CPU(cpumask_var_t, cpu_sibling_map);
  70. DEFINE_PER_CPU(cpumask_var_t, cpu_core_map);
  71. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  72. EXPORT_PER_CPU_SYMBOL(cpu_core_map);
  73. /* SMP operations for this machine */
  74. struct smp_ops_t *smp_ops;
  75. /* Can't be static due to PowerMac hackery */
  76. volatile unsigned int cpu_callin_map[NR_CPUS];
  77. int smt_enabled_at_boot = 1;
  78. /*
  79. * Returns 1 if the specified cpu should be brought up during boot.
  80. * Used to inhibit booting threads if they've been disabled or
  81. * limited on the command line
  82. */
  83. int smp_generic_cpu_bootable(unsigned int nr)
  84. {
  85. /* Special case - we inhibit secondary thread startup
  86. * during boot if the user requests it.
  87. */
  88. if (system_state == SYSTEM_BOOTING && cpu_has_feature(CPU_FTR_SMT)) {
  89. if (!smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
  90. return 0;
  91. if (smt_enabled_at_boot
  92. && cpu_thread_in_core(nr) >= smt_enabled_at_boot)
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. #ifdef CONFIG_PPC64
  98. int smp_generic_kick_cpu(int nr)
  99. {
  100. BUG_ON(nr < 0 || nr >= NR_CPUS);
  101. /*
  102. * The processor is currently spinning, waiting for the
  103. * cpu_start field to become non-zero After we set cpu_start,
  104. * the processor will continue on to secondary_start
  105. */
  106. if (!paca[nr].cpu_start) {
  107. paca[nr].cpu_start = 1;
  108. smp_mb();
  109. return 0;
  110. }
  111. #ifdef CONFIG_HOTPLUG_CPU
  112. /*
  113. * Ok it's not there, so it might be soft-unplugged, let's
  114. * try to bring it back
  115. */
  116. generic_set_cpu_up(nr);
  117. smp_wmb();
  118. smp_send_reschedule(nr);
  119. #endif /* CONFIG_HOTPLUG_CPU */
  120. return 0;
  121. }
  122. #endif /* CONFIG_PPC64 */
  123. static irqreturn_t call_function_action(int irq, void *data)
  124. {
  125. generic_smp_call_function_interrupt();
  126. return IRQ_HANDLED;
  127. }
  128. static irqreturn_t reschedule_action(int irq, void *data)
  129. {
  130. scheduler_ipi();
  131. return IRQ_HANDLED;
  132. }
  133. static irqreturn_t tick_broadcast_ipi_action(int irq, void *data)
  134. {
  135. tick_broadcast_ipi_handler();
  136. return IRQ_HANDLED;
  137. }
  138. #ifdef CONFIG_NMI_IPI
  139. static irqreturn_t nmi_ipi_action(int irq, void *data)
  140. {
  141. smp_handle_nmi_ipi(get_irq_regs());
  142. return IRQ_HANDLED;
  143. }
  144. #endif
  145. static irq_handler_t smp_ipi_action[] = {
  146. [PPC_MSG_CALL_FUNCTION] = call_function_action,
  147. [PPC_MSG_RESCHEDULE] = reschedule_action,
  148. [PPC_MSG_TICK_BROADCAST] = tick_broadcast_ipi_action,
  149. #ifdef CONFIG_NMI_IPI
  150. [PPC_MSG_NMI_IPI] = nmi_ipi_action,
  151. #endif
  152. };
  153. /*
  154. * The NMI IPI is a fallback and not truly non-maskable. It is simpler
  155. * than going through the call function infrastructure, and strongly
  156. * serialized, so it is more appropriate for debugging.
  157. */
  158. const char *smp_ipi_name[] = {
  159. [PPC_MSG_CALL_FUNCTION] = "ipi call function",
  160. [PPC_MSG_RESCHEDULE] = "ipi reschedule",
  161. [PPC_MSG_TICK_BROADCAST] = "ipi tick-broadcast",
  162. [PPC_MSG_NMI_IPI] = "nmi ipi",
  163. };
  164. /* optional function to request ipi, for controllers with >= 4 ipis */
  165. int smp_request_message_ipi(int virq, int msg)
  166. {
  167. int err;
  168. if (msg < 0 || msg > PPC_MSG_NMI_IPI)
  169. return -EINVAL;
  170. #ifndef CONFIG_NMI_IPI
  171. if (msg == PPC_MSG_NMI_IPI)
  172. return 1;
  173. #endif
  174. err = request_irq(virq, smp_ipi_action[msg],
  175. IRQF_PERCPU | IRQF_NO_THREAD | IRQF_NO_SUSPEND,
  176. smp_ipi_name[msg], NULL);
  177. WARN(err < 0, "unable to request_irq %d for %s (rc %d)\n",
  178. virq, smp_ipi_name[msg], err);
  179. return err;
  180. }
  181. #ifdef CONFIG_PPC_SMP_MUXED_IPI
  182. struct cpu_messages {
  183. long messages; /* current messages */
  184. };
  185. static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpu_messages, ipi_message);
  186. void smp_muxed_ipi_set_message(int cpu, int msg)
  187. {
  188. struct cpu_messages *info = &per_cpu(ipi_message, cpu);
  189. char *message = (char *)&info->messages;
  190. /*
  191. * Order previous accesses before accesses in the IPI handler.
  192. */
  193. smp_mb();
  194. message[msg] = 1;
  195. }
  196. void smp_muxed_ipi_message_pass(int cpu, int msg)
  197. {
  198. smp_muxed_ipi_set_message(cpu, msg);
  199. /*
  200. * cause_ipi functions are required to include a full barrier
  201. * before doing whatever causes the IPI.
  202. */
  203. smp_ops->cause_ipi(cpu);
  204. }
  205. #ifdef __BIG_ENDIAN__
  206. #define IPI_MESSAGE(A) (1uL << ((BITS_PER_LONG - 8) - 8 * (A)))
  207. #else
  208. #define IPI_MESSAGE(A) (1uL << (8 * (A)))
  209. #endif
  210. irqreturn_t smp_ipi_demux(void)
  211. {
  212. mb(); /* order any irq clear */
  213. return smp_ipi_demux_relaxed();
  214. }
  215. /* sync-free variant. Callers should ensure synchronization */
  216. irqreturn_t smp_ipi_demux_relaxed(void)
  217. {
  218. struct cpu_messages *info;
  219. unsigned long all;
  220. info = this_cpu_ptr(&ipi_message);
  221. do {
  222. all = xchg(&info->messages, 0);
  223. #if defined(CONFIG_KVM_XICS) && defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
  224. /*
  225. * Must check for PPC_MSG_RM_HOST_ACTION messages
  226. * before PPC_MSG_CALL_FUNCTION messages because when
  227. * a VM is destroyed, we call kick_all_cpus_sync()
  228. * to ensure that any pending PPC_MSG_RM_HOST_ACTION
  229. * messages have completed before we free any VCPUs.
  230. */
  231. if (all & IPI_MESSAGE(PPC_MSG_RM_HOST_ACTION))
  232. kvmppc_xics_ipi_action();
  233. #endif
  234. if (all & IPI_MESSAGE(PPC_MSG_CALL_FUNCTION))
  235. generic_smp_call_function_interrupt();
  236. if (all & IPI_MESSAGE(PPC_MSG_RESCHEDULE))
  237. scheduler_ipi();
  238. if (all & IPI_MESSAGE(PPC_MSG_TICK_BROADCAST))
  239. tick_broadcast_ipi_handler();
  240. #ifdef CONFIG_NMI_IPI
  241. if (all & IPI_MESSAGE(PPC_MSG_NMI_IPI))
  242. nmi_ipi_action(0, NULL);
  243. #endif
  244. } while (info->messages);
  245. return IRQ_HANDLED;
  246. }
  247. #endif /* CONFIG_PPC_SMP_MUXED_IPI */
  248. static inline void do_message_pass(int cpu, int msg)
  249. {
  250. if (smp_ops->message_pass)
  251. smp_ops->message_pass(cpu, msg);
  252. #ifdef CONFIG_PPC_SMP_MUXED_IPI
  253. else
  254. smp_muxed_ipi_message_pass(cpu, msg);
  255. #endif
  256. }
  257. void smp_send_reschedule(int cpu)
  258. {
  259. if (likely(smp_ops))
  260. do_message_pass(cpu, PPC_MSG_RESCHEDULE);
  261. }
  262. EXPORT_SYMBOL_GPL(smp_send_reschedule);
  263. void arch_send_call_function_single_ipi(int cpu)
  264. {
  265. do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
  266. }
  267. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  268. {
  269. unsigned int cpu;
  270. for_each_cpu(cpu, mask)
  271. do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
  272. }
  273. #ifdef CONFIG_NMI_IPI
  274. /*
  275. * "NMI IPI" system.
  276. *
  277. * NMI IPIs may not be recoverable, so should not be used as ongoing part of
  278. * a running system. They can be used for crash, debug, halt/reboot, etc.
  279. *
  280. * NMI IPIs are globally single threaded. No more than one in progress at
  281. * any time.
  282. *
  283. * The IPI call waits with interrupts disabled until all targets enter the
  284. * NMI handler, then the call returns.
  285. *
  286. * No new NMI can be initiated until targets exit the handler.
  287. *
  288. * The IPI call may time out without all targets entering the NMI handler.
  289. * In that case, there is some logic to recover (and ignore subsequent
  290. * NMI interrupts that may eventually be raised), but the platform interrupt
  291. * handler may not be able to distinguish this from other exception causes,
  292. * which may cause a crash.
  293. */
  294. static atomic_t __nmi_ipi_lock = ATOMIC_INIT(0);
  295. static struct cpumask nmi_ipi_pending_mask;
  296. static int nmi_ipi_busy_count = 0;
  297. static void (*nmi_ipi_function)(struct pt_regs *) = NULL;
  298. static void nmi_ipi_lock_start(unsigned long *flags)
  299. {
  300. raw_local_irq_save(*flags);
  301. hard_irq_disable();
  302. while (atomic_cmpxchg(&__nmi_ipi_lock, 0, 1) == 1) {
  303. raw_local_irq_restore(*flags);
  304. cpu_relax();
  305. raw_local_irq_save(*flags);
  306. hard_irq_disable();
  307. }
  308. }
  309. static void nmi_ipi_lock(void)
  310. {
  311. while (atomic_cmpxchg(&__nmi_ipi_lock, 0, 1) == 1)
  312. cpu_relax();
  313. }
  314. static void nmi_ipi_unlock(void)
  315. {
  316. smp_mb();
  317. WARN_ON(atomic_read(&__nmi_ipi_lock) != 1);
  318. atomic_set(&__nmi_ipi_lock, 0);
  319. }
  320. static void nmi_ipi_unlock_end(unsigned long *flags)
  321. {
  322. nmi_ipi_unlock();
  323. raw_local_irq_restore(*flags);
  324. }
  325. /*
  326. * Platform NMI handler calls this to ack
  327. */
  328. int smp_handle_nmi_ipi(struct pt_regs *regs)
  329. {
  330. void (*fn)(struct pt_regs *);
  331. unsigned long flags;
  332. int me = raw_smp_processor_id();
  333. int ret = 0;
  334. /*
  335. * Unexpected NMIs are possible here because the interrupt may not
  336. * be able to distinguish NMI IPIs from other types of NMIs, or
  337. * because the caller may have timed out.
  338. */
  339. nmi_ipi_lock_start(&flags);
  340. if (!nmi_ipi_busy_count)
  341. goto out;
  342. if (!cpumask_test_cpu(me, &nmi_ipi_pending_mask))
  343. goto out;
  344. fn = nmi_ipi_function;
  345. if (!fn)
  346. goto out;
  347. cpumask_clear_cpu(me, &nmi_ipi_pending_mask);
  348. nmi_ipi_busy_count++;
  349. nmi_ipi_unlock();
  350. ret = 1;
  351. fn(regs);
  352. nmi_ipi_lock();
  353. nmi_ipi_busy_count--;
  354. out:
  355. nmi_ipi_unlock_end(&flags);
  356. return ret;
  357. }
  358. static void do_smp_send_nmi_ipi(int cpu)
  359. {
  360. if (smp_ops->cause_nmi_ipi && smp_ops->cause_nmi_ipi(cpu))
  361. return;
  362. if (cpu >= 0) {
  363. do_message_pass(cpu, PPC_MSG_NMI_IPI);
  364. } else {
  365. int c;
  366. for_each_online_cpu(c) {
  367. if (c == raw_smp_processor_id())
  368. continue;
  369. do_message_pass(c, PPC_MSG_NMI_IPI);
  370. }
  371. }
  372. }
  373. /*
  374. * - cpu is the target CPU (must not be this CPU), or NMI_IPI_ALL_OTHERS.
  375. * - fn is the target callback function.
  376. * - delay_us > 0 is the delay before giving up waiting for targets to
  377. * enter the handler, == 0 specifies indefinite delay.
  378. */
  379. static int smp_send_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us)
  380. {
  381. unsigned long flags;
  382. int me = raw_smp_processor_id();
  383. int ret = 1;
  384. BUG_ON(cpu == me);
  385. BUG_ON(cpu < 0 && cpu != NMI_IPI_ALL_OTHERS);
  386. if (unlikely(!smp_ops))
  387. return 0;
  388. /* Take the nmi_ipi_busy count/lock with interrupts hard disabled */
  389. nmi_ipi_lock_start(&flags);
  390. while (nmi_ipi_busy_count) {
  391. nmi_ipi_unlock_end(&flags);
  392. cpu_relax();
  393. nmi_ipi_lock_start(&flags);
  394. }
  395. nmi_ipi_function = fn;
  396. if (cpu < 0) {
  397. /* ALL_OTHERS */
  398. cpumask_copy(&nmi_ipi_pending_mask, cpu_online_mask);
  399. cpumask_clear_cpu(me, &nmi_ipi_pending_mask);
  400. } else {
  401. /* cpumask starts clear */
  402. cpumask_set_cpu(cpu, &nmi_ipi_pending_mask);
  403. }
  404. nmi_ipi_busy_count++;
  405. nmi_ipi_unlock();
  406. do_smp_send_nmi_ipi(cpu);
  407. while (!cpumask_empty(&nmi_ipi_pending_mask)) {
  408. udelay(1);
  409. if (delay_us) {
  410. delay_us--;
  411. if (!delay_us)
  412. break;
  413. }
  414. }
  415. nmi_ipi_lock();
  416. if (!cpumask_empty(&nmi_ipi_pending_mask)) {
  417. /* Could not gather all CPUs */
  418. ret = 0;
  419. cpumask_clear(&nmi_ipi_pending_mask);
  420. }
  421. nmi_ipi_busy_count--;
  422. nmi_ipi_unlock_end(&flags);
  423. return ret;
  424. }
  425. #endif /* CONFIG_NMI_IPI */
  426. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  427. void tick_broadcast(const struct cpumask *mask)
  428. {
  429. unsigned int cpu;
  430. for_each_cpu(cpu, mask)
  431. do_message_pass(cpu, PPC_MSG_TICK_BROADCAST);
  432. }
  433. #endif
  434. #ifdef CONFIG_DEBUGGER
  435. void debugger_ipi_callback(struct pt_regs *regs)
  436. {
  437. debugger_ipi(regs);
  438. }
  439. void smp_send_debugger_break(void)
  440. {
  441. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, debugger_ipi_callback, 1000000);
  442. }
  443. #endif
  444. #ifdef CONFIG_KEXEC_CORE
  445. void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
  446. {
  447. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, crash_ipi_callback, 1000000);
  448. }
  449. #endif
  450. static void stop_this_cpu(void *dummy)
  451. {
  452. /* Remove this CPU */
  453. set_cpu_online(smp_processor_id(), false);
  454. local_irq_disable();
  455. while (1)
  456. ;
  457. }
  458. void smp_send_stop(void)
  459. {
  460. smp_call_function(stop_this_cpu, NULL, 0);
  461. }
  462. struct thread_info *current_set[NR_CPUS];
  463. static void smp_store_cpu_info(int id)
  464. {
  465. per_cpu(cpu_pvr, id) = mfspr(SPRN_PVR);
  466. #ifdef CONFIG_PPC_FSL_BOOK3E
  467. per_cpu(next_tlbcam_idx, id)
  468. = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
  469. #endif
  470. }
  471. void __init smp_prepare_cpus(unsigned int max_cpus)
  472. {
  473. unsigned int cpu;
  474. DBG("smp_prepare_cpus\n");
  475. /*
  476. * setup_cpu may need to be called on the boot cpu. We havent
  477. * spun any cpus up but lets be paranoid.
  478. */
  479. BUG_ON(boot_cpuid != smp_processor_id());
  480. /* Fixup boot cpu */
  481. smp_store_cpu_info(boot_cpuid);
  482. cpu_callin_map[boot_cpuid] = 1;
  483. for_each_possible_cpu(cpu) {
  484. zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu),
  485. GFP_KERNEL, cpu_to_node(cpu));
  486. zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu),
  487. GFP_KERNEL, cpu_to_node(cpu));
  488. /*
  489. * numa_node_id() works after this.
  490. */
  491. if (cpu_present(cpu)) {
  492. set_cpu_numa_node(cpu, numa_cpu_lookup_table[cpu]);
  493. set_cpu_numa_mem(cpu,
  494. local_memory_node(numa_cpu_lookup_table[cpu]));
  495. }
  496. }
  497. cpumask_set_cpu(boot_cpuid, cpu_sibling_mask(boot_cpuid));
  498. cpumask_set_cpu(boot_cpuid, cpu_core_mask(boot_cpuid));
  499. if (smp_ops && smp_ops->probe)
  500. smp_ops->probe();
  501. }
  502. void smp_prepare_boot_cpu(void)
  503. {
  504. BUG_ON(smp_processor_id() != boot_cpuid);
  505. #ifdef CONFIG_PPC64
  506. paca[boot_cpuid].__current = current;
  507. #endif
  508. set_numa_node(numa_cpu_lookup_table[boot_cpuid]);
  509. current_set[boot_cpuid] = task_thread_info(current);
  510. }
  511. #ifdef CONFIG_HOTPLUG_CPU
  512. int generic_cpu_disable(void)
  513. {
  514. unsigned int cpu = smp_processor_id();
  515. if (cpu == boot_cpuid)
  516. return -EBUSY;
  517. set_cpu_online(cpu, false);
  518. #ifdef CONFIG_PPC64
  519. vdso_data->processorCount--;
  520. #endif
  521. /* Update affinity of all IRQs previously aimed at this CPU */
  522. irq_migrate_all_off_this_cpu();
  523. /*
  524. * Depending on the details of the interrupt controller, it's possible
  525. * that one of the interrupts we just migrated away from this CPU is
  526. * actually already pending on this CPU. If we leave it in that state
  527. * the interrupt will never be EOI'ed, and will never fire again. So
  528. * temporarily enable interrupts here, to allow any pending interrupt to
  529. * be received (and EOI'ed), before we take this CPU offline.
  530. */
  531. local_irq_enable();
  532. mdelay(1);
  533. local_irq_disable();
  534. return 0;
  535. }
  536. void generic_cpu_die(unsigned int cpu)
  537. {
  538. int i;
  539. for (i = 0; i < 100; i++) {
  540. smp_rmb();
  541. if (is_cpu_dead(cpu))
  542. return;
  543. msleep(100);
  544. }
  545. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  546. }
  547. void generic_set_cpu_dead(unsigned int cpu)
  548. {
  549. per_cpu(cpu_state, cpu) = CPU_DEAD;
  550. }
  551. /*
  552. * The cpu_state should be set to CPU_UP_PREPARE in kick_cpu(), otherwise
  553. * the cpu_state is always CPU_DEAD after calling generic_set_cpu_dead(),
  554. * which makes the delay in generic_cpu_die() not happen.
  555. */
  556. void generic_set_cpu_up(unsigned int cpu)
  557. {
  558. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  559. }
  560. int generic_check_cpu_restart(unsigned int cpu)
  561. {
  562. return per_cpu(cpu_state, cpu) == CPU_UP_PREPARE;
  563. }
  564. int is_cpu_dead(unsigned int cpu)
  565. {
  566. return per_cpu(cpu_state, cpu) == CPU_DEAD;
  567. }
  568. static bool secondaries_inhibited(void)
  569. {
  570. return kvm_hv_mode_active();
  571. }
  572. #else /* HOTPLUG_CPU */
  573. #define secondaries_inhibited() 0
  574. #endif
  575. static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
  576. {
  577. struct thread_info *ti = task_thread_info(idle);
  578. #ifdef CONFIG_PPC64
  579. paca[cpu].__current = idle;
  580. paca[cpu].kstack = (unsigned long)ti + THREAD_SIZE - STACK_FRAME_OVERHEAD;
  581. #endif
  582. ti->cpu = cpu;
  583. secondary_ti = current_set[cpu] = ti;
  584. }
  585. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  586. {
  587. int rc, c;
  588. /*
  589. * Don't allow secondary threads to come online if inhibited
  590. */
  591. if (threads_per_core > 1 && secondaries_inhibited() &&
  592. cpu_thread_in_subcore(cpu))
  593. return -EBUSY;
  594. if (smp_ops == NULL ||
  595. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  596. return -EINVAL;
  597. cpu_idle_thread_init(cpu, tidle);
  598. /*
  599. * The platform might need to allocate resources prior to bringing
  600. * up the CPU
  601. */
  602. if (smp_ops->prepare_cpu) {
  603. rc = smp_ops->prepare_cpu(cpu);
  604. if (rc)
  605. return rc;
  606. }
  607. /* Make sure callin-map entry is 0 (can be leftover a CPU
  608. * hotplug
  609. */
  610. cpu_callin_map[cpu] = 0;
  611. /* The information for processor bringup must
  612. * be written out to main store before we release
  613. * the processor.
  614. */
  615. smp_mb();
  616. /* wake up cpus */
  617. DBG("smp: kicking cpu %d\n", cpu);
  618. rc = smp_ops->kick_cpu(cpu);
  619. if (rc) {
  620. pr_err("smp: failed starting cpu %d (rc %d)\n", cpu, rc);
  621. return rc;
  622. }
  623. /*
  624. * wait to see if the cpu made a callin (is actually up).
  625. * use this value that I found through experimentation.
  626. * -- Cort
  627. */
  628. if (system_state < SYSTEM_RUNNING)
  629. for (c = 50000; c && !cpu_callin_map[cpu]; c--)
  630. udelay(100);
  631. #ifdef CONFIG_HOTPLUG_CPU
  632. else
  633. /*
  634. * CPUs can take much longer to come up in the
  635. * hotplug case. Wait five seconds.
  636. */
  637. for (c = 5000; c && !cpu_callin_map[cpu]; c--)
  638. msleep(1);
  639. #endif
  640. if (!cpu_callin_map[cpu]) {
  641. printk(KERN_ERR "Processor %u is stuck.\n", cpu);
  642. return -ENOENT;
  643. }
  644. DBG("Processor %u found.\n", cpu);
  645. if (smp_ops->give_timebase)
  646. smp_ops->give_timebase();
  647. /* Wait until cpu puts itself in the online & active maps */
  648. while (!cpu_online(cpu))
  649. cpu_relax();
  650. return 0;
  651. }
  652. /* Return the value of the reg property corresponding to the given
  653. * logical cpu.
  654. */
  655. int cpu_to_core_id(int cpu)
  656. {
  657. struct device_node *np;
  658. const __be32 *reg;
  659. int id = -1;
  660. np = of_get_cpu_node(cpu, NULL);
  661. if (!np)
  662. goto out;
  663. reg = of_get_property(np, "reg", NULL);
  664. if (!reg)
  665. goto out;
  666. id = be32_to_cpup(reg);
  667. out:
  668. of_node_put(np);
  669. return id;
  670. }
  671. EXPORT_SYMBOL_GPL(cpu_to_core_id);
  672. /* Helper routines for cpu to core mapping */
  673. int cpu_core_index_of_thread(int cpu)
  674. {
  675. return cpu >> threads_shift;
  676. }
  677. EXPORT_SYMBOL_GPL(cpu_core_index_of_thread);
  678. int cpu_first_thread_of_core(int core)
  679. {
  680. return core << threads_shift;
  681. }
  682. EXPORT_SYMBOL_GPL(cpu_first_thread_of_core);
  683. static void traverse_siblings_chip_id(int cpu, bool add, int chipid)
  684. {
  685. const struct cpumask *mask;
  686. struct device_node *np;
  687. int i, plen;
  688. const __be32 *prop;
  689. mask = add ? cpu_online_mask : cpu_present_mask;
  690. for_each_cpu(i, mask) {
  691. np = of_get_cpu_node(i, NULL);
  692. if (!np)
  693. continue;
  694. prop = of_get_property(np, "ibm,chip-id", &plen);
  695. if (prop && plen == sizeof(int) &&
  696. of_read_number(prop, 1) == chipid) {
  697. if (add) {
  698. cpumask_set_cpu(cpu, cpu_core_mask(i));
  699. cpumask_set_cpu(i, cpu_core_mask(cpu));
  700. } else {
  701. cpumask_clear_cpu(cpu, cpu_core_mask(i));
  702. cpumask_clear_cpu(i, cpu_core_mask(cpu));
  703. }
  704. }
  705. of_node_put(np);
  706. }
  707. }
  708. /* Must be called when no change can occur to cpu_present_mask,
  709. * i.e. during cpu online or offline.
  710. */
  711. static struct device_node *cpu_to_l2cache(int cpu)
  712. {
  713. struct device_node *np;
  714. struct device_node *cache;
  715. if (!cpu_present(cpu))
  716. return NULL;
  717. np = of_get_cpu_node(cpu, NULL);
  718. if (np == NULL)
  719. return NULL;
  720. cache = of_find_next_cache_node(np);
  721. of_node_put(np);
  722. return cache;
  723. }
  724. static void traverse_core_siblings(int cpu, bool add)
  725. {
  726. struct device_node *l2_cache, *np;
  727. const struct cpumask *mask;
  728. int i, chip, plen;
  729. const __be32 *prop;
  730. /* First see if we have ibm,chip-id properties in cpu nodes */
  731. np = of_get_cpu_node(cpu, NULL);
  732. if (np) {
  733. chip = -1;
  734. prop = of_get_property(np, "ibm,chip-id", &plen);
  735. if (prop && plen == sizeof(int))
  736. chip = of_read_number(prop, 1);
  737. of_node_put(np);
  738. if (chip >= 0) {
  739. traverse_siblings_chip_id(cpu, add, chip);
  740. return;
  741. }
  742. }
  743. l2_cache = cpu_to_l2cache(cpu);
  744. mask = add ? cpu_online_mask : cpu_present_mask;
  745. for_each_cpu(i, mask) {
  746. np = cpu_to_l2cache(i);
  747. if (!np)
  748. continue;
  749. if (np == l2_cache) {
  750. if (add) {
  751. cpumask_set_cpu(cpu, cpu_core_mask(i));
  752. cpumask_set_cpu(i, cpu_core_mask(cpu));
  753. } else {
  754. cpumask_clear_cpu(cpu, cpu_core_mask(i));
  755. cpumask_clear_cpu(i, cpu_core_mask(cpu));
  756. }
  757. }
  758. of_node_put(np);
  759. }
  760. of_node_put(l2_cache);
  761. }
  762. /* Activate a secondary processor. */
  763. void start_secondary(void *unused)
  764. {
  765. unsigned int cpu = smp_processor_id();
  766. int i, base;
  767. mmgrab(&init_mm);
  768. current->active_mm = &init_mm;
  769. smp_store_cpu_info(cpu);
  770. set_dec(tb_ticks_per_jiffy);
  771. preempt_disable();
  772. cpu_callin_map[cpu] = 1;
  773. if (smp_ops->setup_cpu)
  774. smp_ops->setup_cpu(cpu);
  775. if (smp_ops->take_timebase)
  776. smp_ops->take_timebase();
  777. secondary_cpu_time_init();
  778. #ifdef CONFIG_PPC64
  779. if (system_state == SYSTEM_RUNNING)
  780. vdso_data->processorCount++;
  781. vdso_getcpu_init();
  782. #endif
  783. /* Update sibling maps */
  784. base = cpu_first_thread_sibling(cpu);
  785. for (i = 0; i < threads_per_core; i++) {
  786. if (cpu_is_offline(base + i) && (cpu != base + i))
  787. continue;
  788. cpumask_set_cpu(cpu, cpu_sibling_mask(base + i));
  789. cpumask_set_cpu(base + i, cpu_sibling_mask(cpu));
  790. /* cpu_core_map should be a superset of
  791. * cpu_sibling_map even if we don't have cache
  792. * information, so update the former here, too.
  793. */
  794. cpumask_set_cpu(cpu, cpu_core_mask(base + i));
  795. cpumask_set_cpu(base + i, cpu_core_mask(cpu));
  796. }
  797. traverse_core_siblings(cpu, true);
  798. set_numa_node(numa_cpu_lookup_table[cpu]);
  799. set_numa_mem(local_memory_node(numa_cpu_lookup_table[cpu]));
  800. smp_wmb();
  801. notify_cpu_starting(cpu);
  802. set_cpu_online(cpu, true);
  803. local_irq_enable();
  804. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  805. BUG();
  806. }
  807. int setup_profiling_timer(unsigned int multiplier)
  808. {
  809. return 0;
  810. }
  811. #ifdef CONFIG_SCHED_SMT
  812. /* cpumask of CPUs with asymetric SMT dependancy */
  813. static int powerpc_smt_flags(void)
  814. {
  815. int flags = SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
  816. if (cpu_has_feature(CPU_FTR_ASYM_SMT)) {
  817. printk_once(KERN_INFO "Enabling Asymmetric SMT scheduling\n");
  818. flags |= SD_ASYM_PACKING;
  819. }
  820. return flags;
  821. }
  822. #endif
  823. static struct sched_domain_topology_level powerpc_topology[] = {
  824. #ifdef CONFIG_SCHED_SMT
  825. { cpu_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT) },
  826. #endif
  827. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  828. { NULL, },
  829. };
  830. static __init long smp_setup_cpu_workfn(void *data __always_unused)
  831. {
  832. smp_ops->setup_cpu(boot_cpuid);
  833. return 0;
  834. }
  835. void __init smp_cpus_done(unsigned int max_cpus)
  836. {
  837. /*
  838. * We want the setup_cpu() here to be called on the boot CPU, but
  839. * init might run on any CPU, so make sure it's invoked on the boot
  840. * CPU.
  841. */
  842. if (smp_ops && smp_ops->setup_cpu)
  843. work_on_cpu_safe(boot_cpuid, smp_setup_cpu_workfn, NULL);
  844. if (smp_ops && smp_ops->bringup_done)
  845. smp_ops->bringup_done();
  846. dump_numa_cpu_topology();
  847. set_sched_topology(powerpc_topology);
  848. }
  849. #ifdef CONFIG_HOTPLUG_CPU
  850. int __cpu_disable(void)
  851. {
  852. int cpu = smp_processor_id();
  853. int base, i;
  854. int err;
  855. if (!smp_ops->cpu_disable)
  856. return -ENOSYS;
  857. err = smp_ops->cpu_disable();
  858. if (err)
  859. return err;
  860. /* Update sibling maps */
  861. base = cpu_first_thread_sibling(cpu);
  862. for (i = 0; i < threads_per_core && base + i < nr_cpu_ids; i++) {
  863. cpumask_clear_cpu(cpu, cpu_sibling_mask(base + i));
  864. cpumask_clear_cpu(base + i, cpu_sibling_mask(cpu));
  865. cpumask_clear_cpu(cpu, cpu_core_mask(base + i));
  866. cpumask_clear_cpu(base + i, cpu_core_mask(cpu));
  867. }
  868. traverse_core_siblings(cpu, false);
  869. return 0;
  870. }
  871. void __cpu_die(unsigned int cpu)
  872. {
  873. if (smp_ops->cpu_die)
  874. smp_ops->cpu_die(cpu);
  875. }
  876. void cpu_die(void)
  877. {
  878. if (ppc_md.cpu_die)
  879. ppc_md.cpu_die();
  880. /* If we return, we re-enter start_secondary */
  881. start_secondary_resume();
  882. }
  883. #endif