smp.c 27 KB

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