smp.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. * NMI IPIs are globally single threaded. No more than one in progress at
  296. * any time.
  297. *
  298. * The IPI call waits with interrupts disabled until all targets enter the
  299. * NMI handler, then the call returns.
  300. *
  301. * No new NMI can be initiated until targets exit the handler.
  302. *
  303. * The IPI call may time out without all targets entering the NMI handler.
  304. * In that case, there is some logic to recover (and ignore subsequent
  305. * NMI interrupts that may eventually be raised), but the platform interrupt
  306. * handler may not be able to distinguish this from other exception causes,
  307. * which may cause a crash.
  308. */
  309. static atomic_t __nmi_ipi_lock = ATOMIC_INIT(0);
  310. static struct cpumask nmi_ipi_pending_mask;
  311. static int nmi_ipi_busy_count = 0;
  312. static void (*nmi_ipi_function)(struct pt_regs *) = NULL;
  313. static void nmi_ipi_lock_start(unsigned long *flags)
  314. {
  315. raw_local_irq_save(*flags);
  316. hard_irq_disable();
  317. while (atomic_cmpxchg(&__nmi_ipi_lock, 0, 1) == 1) {
  318. raw_local_irq_restore(*flags);
  319. spin_until_cond(atomic_read(&__nmi_ipi_lock) == 0);
  320. raw_local_irq_save(*flags);
  321. hard_irq_disable();
  322. }
  323. }
  324. static void nmi_ipi_lock(void)
  325. {
  326. while (atomic_cmpxchg(&__nmi_ipi_lock, 0, 1) == 1)
  327. spin_until_cond(atomic_read(&__nmi_ipi_lock) == 0);
  328. }
  329. static void nmi_ipi_unlock(void)
  330. {
  331. smp_mb();
  332. WARN_ON(atomic_read(&__nmi_ipi_lock) != 1);
  333. atomic_set(&__nmi_ipi_lock, 0);
  334. }
  335. static void nmi_ipi_unlock_end(unsigned long *flags)
  336. {
  337. nmi_ipi_unlock();
  338. raw_local_irq_restore(*flags);
  339. }
  340. /*
  341. * Platform NMI handler calls this to ack
  342. */
  343. int smp_handle_nmi_ipi(struct pt_regs *regs)
  344. {
  345. void (*fn)(struct pt_regs *);
  346. unsigned long flags;
  347. int me = raw_smp_processor_id();
  348. int ret = 0;
  349. /*
  350. * Unexpected NMIs are possible here because the interrupt may not
  351. * be able to distinguish NMI IPIs from other types of NMIs, or
  352. * because the caller may have timed out.
  353. */
  354. nmi_ipi_lock_start(&flags);
  355. if (!nmi_ipi_busy_count)
  356. goto out;
  357. if (!cpumask_test_cpu(me, &nmi_ipi_pending_mask))
  358. goto out;
  359. fn = nmi_ipi_function;
  360. if (!fn)
  361. goto out;
  362. cpumask_clear_cpu(me, &nmi_ipi_pending_mask);
  363. nmi_ipi_busy_count++;
  364. nmi_ipi_unlock();
  365. ret = 1;
  366. fn(regs);
  367. nmi_ipi_lock();
  368. nmi_ipi_busy_count--;
  369. out:
  370. nmi_ipi_unlock_end(&flags);
  371. return ret;
  372. }
  373. static void do_smp_send_nmi_ipi(int cpu, bool safe)
  374. {
  375. if (!safe && smp_ops->cause_nmi_ipi && smp_ops->cause_nmi_ipi(cpu))
  376. return;
  377. if (cpu >= 0) {
  378. do_message_pass(cpu, PPC_MSG_NMI_IPI);
  379. } else {
  380. int c;
  381. for_each_online_cpu(c) {
  382. if (c == raw_smp_processor_id())
  383. continue;
  384. do_message_pass(c, PPC_MSG_NMI_IPI);
  385. }
  386. }
  387. }
  388. void smp_flush_nmi_ipi(u64 delay_us)
  389. {
  390. unsigned long flags;
  391. nmi_ipi_lock_start(&flags);
  392. while (nmi_ipi_busy_count) {
  393. nmi_ipi_unlock_end(&flags);
  394. udelay(1);
  395. if (delay_us) {
  396. delay_us--;
  397. if (!delay_us)
  398. return;
  399. }
  400. nmi_ipi_lock_start(&flags);
  401. }
  402. nmi_ipi_unlock_end(&flags);
  403. }
  404. /*
  405. * - cpu is the target CPU (must not be this CPU), or NMI_IPI_ALL_OTHERS.
  406. * - fn is the target callback function.
  407. * - delay_us > 0 is the delay before giving up waiting for targets to
  408. * enter the handler, == 0 specifies indefinite delay.
  409. */
  410. int __smp_send_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us, bool safe)
  411. {
  412. unsigned long flags;
  413. int me = raw_smp_processor_id();
  414. int ret = 1;
  415. BUG_ON(cpu == me);
  416. BUG_ON(cpu < 0 && cpu != NMI_IPI_ALL_OTHERS);
  417. if (unlikely(!smp_ops))
  418. return 0;
  419. /* Take the nmi_ipi_busy count/lock with interrupts hard disabled */
  420. nmi_ipi_lock_start(&flags);
  421. while (nmi_ipi_busy_count) {
  422. nmi_ipi_unlock_end(&flags);
  423. spin_until_cond(nmi_ipi_busy_count == 0);
  424. nmi_ipi_lock_start(&flags);
  425. }
  426. nmi_ipi_function = fn;
  427. if (cpu < 0) {
  428. /* ALL_OTHERS */
  429. cpumask_copy(&nmi_ipi_pending_mask, cpu_online_mask);
  430. cpumask_clear_cpu(me, &nmi_ipi_pending_mask);
  431. } else {
  432. /* cpumask starts clear */
  433. cpumask_set_cpu(cpu, &nmi_ipi_pending_mask);
  434. }
  435. nmi_ipi_busy_count++;
  436. nmi_ipi_unlock();
  437. do_smp_send_nmi_ipi(cpu, safe);
  438. while (!cpumask_empty(&nmi_ipi_pending_mask)) {
  439. udelay(1);
  440. if (delay_us) {
  441. delay_us--;
  442. if (!delay_us)
  443. break;
  444. }
  445. }
  446. nmi_ipi_lock();
  447. if (!cpumask_empty(&nmi_ipi_pending_mask)) {
  448. /* Could not gather all CPUs */
  449. ret = 0;
  450. cpumask_clear(&nmi_ipi_pending_mask);
  451. }
  452. nmi_ipi_busy_count--;
  453. nmi_ipi_unlock_end(&flags);
  454. return ret;
  455. }
  456. int smp_send_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us)
  457. {
  458. return __smp_send_nmi_ipi(cpu, fn, delay_us, false);
  459. }
  460. int smp_send_safe_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us)
  461. {
  462. return __smp_send_nmi_ipi(cpu, fn, delay_us, true);
  463. }
  464. #endif /* CONFIG_NMI_IPI */
  465. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  466. void tick_broadcast(const struct cpumask *mask)
  467. {
  468. unsigned int cpu;
  469. for_each_cpu(cpu, mask)
  470. do_message_pass(cpu, PPC_MSG_TICK_BROADCAST);
  471. }
  472. #endif
  473. #ifdef CONFIG_DEBUGGER
  474. void debugger_ipi_callback(struct pt_regs *regs)
  475. {
  476. debugger_ipi(regs);
  477. }
  478. void smp_send_debugger_break(void)
  479. {
  480. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, debugger_ipi_callback, 1000000);
  481. }
  482. #endif
  483. #ifdef CONFIG_KEXEC_CORE
  484. void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
  485. {
  486. int cpu;
  487. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, crash_ipi_callback, 1000000);
  488. if (kdump_in_progress() && crash_wake_offline) {
  489. for_each_present_cpu(cpu) {
  490. if (cpu_online(cpu))
  491. continue;
  492. /*
  493. * crash_ipi_callback will wait for
  494. * all cpus, including offline CPUs.
  495. * We don't care about nmi_ipi_function.
  496. * Offline cpus will jump straight into
  497. * crash_ipi_callback, we can skip the
  498. * entire NMI dance and waiting for
  499. * cpus to clear pending mask, etc.
  500. */
  501. do_smp_send_nmi_ipi(cpu, false);
  502. }
  503. }
  504. }
  505. #endif
  506. #ifdef CONFIG_NMI_IPI
  507. static void nmi_stop_this_cpu(struct pt_regs *regs)
  508. {
  509. /*
  510. * This is a special case because it never returns, so the NMI IPI
  511. * handling would never mark it as done, which makes any later
  512. * smp_send_nmi_ipi() call spin forever. Mark it done now.
  513. *
  514. * IRQs are already hard disabled by the smp_handle_nmi_ipi.
  515. */
  516. nmi_ipi_lock();
  517. nmi_ipi_busy_count--;
  518. nmi_ipi_unlock();
  519. spin_begin();
  520. while (1)
  521. spin_cpu_relax();
  522. }
  523. void smp_send_stop(void)
  524. {
  525. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, nmi_stop_this_cpu, 1000000);
  526. }
  527. #else /* CONFIG_NMI_IPI */
  528. static void stop_this_cpu(void *dummy)
  529. {
  530. hard_irq_disable();
  531. spin_begin();
  532. while (1)
  533. spin_cpu_relax();
  534. }
  535. void smp_send_stop(void)
  536. {
  537. static bool stopped = false;
  538. /*
  539. * Prevent waiting on csd lock from a previous smp_send_stop.
  540. * This is racy, but in general callers try to do the right
  541. * thing and only fire off one smp_send_stop (e.g., see
  542. * kernel/panic.c)
  543. */
  544. if (stopped)
  545. return;
  546. stopped = true;
  547. smp_call_function(stop_this_cpu, NULL, 0);
  548. }
  549. #endif /* CONFIG_NMI_IPI */
  550. struct thread_info *current_set[NR_CPUS];
  551. static void smp_store_cpu_info(int id)
  552. {
  553. per_cpu(cpu_pvr, id) = mfspr(SPRN_PVR);
  554. #ifdef CONFIG_PPC_FSL_BOOK3E
  555. per_cpu(next_tlbcam_idx, id)
  556. = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
  557. #endif
  558. }
  559. /*
  560. * Relationships between CPUs are maintained in a set of per-cpu cpumasks so
  561. * rather than just passing around the cpumask we pass around a function that
  562. * returns the that cpumask for the given CPU.
  563. */
  564. static void set_cpus_related(int i, int j, struct cpumask *(*get_cpumask)(int))
  565. {
  566. cpumask_set_cpu(i, get_cpumask(j));
  567. cpumask_set_cpu(j, get_cpumask(i));
  568. }
  569. #ifdef CONFIG_HOTPLUG_CPU
  570. static void set_cpus_unrelated(int i, int j,
  571. struct cpumask *(*get_cpumask)(int))
  572. {
  573. cpumask_clear_cpu(i, get_cpumask(j));
  574. cpumask_clear_cpu(j, get_cpumask(i));
  575. }
  576. #endif
  577. void __init smp_prepare_cpus(unsigned int max_cpus)
  578. {
  579. unsigned int cpu;
  580. DBG("smp_prepare_cpus\n");
  581. /*
  582. * setup_cpu may need to be called on the boot cpu. We havent
  583. * spun any cpus up but lets be paranoid.
  584. */
  585. BUG_ON(boot_cpuid != smp_processor_id());
  586. /* Fixup boot cpu */
  587. smp_store_cpu_info(boot_cpuid);
  588. cpu_callin_map[boot_cpuid] = 1;
  589. for_each_possible_cpu(cpu) {
  590. zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu),
  591. GFP_KERNEL, cpu_to_node(cpu));
  592. zalloc_cpumask_var_node(&per_cpu(cpu_l2_cache_map, cpu),
  593. GFP_KERNEL, cpu_to_node(cpu));
  594. zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu),
  595. GFP_KERNEL, cpu_to_node(cpu));
  596. /*
  597. * numa_node_id() works after this.
  598. */
  599. if (cpu_present(cpu)) {
  600. set_cpu_numa_node(cpu, numa_cpu_lookup_table[cpu]);
  601. set_cpu_numa_mem(cpu,
  602. local_memory_node(numa_cpu_lookup_table[cpu]));
  603. }
  604. }
  605. /* Init the cpumasks so the boot CPU is related to itself */
  606. cpumask_set_cpu(boot_cpuid, cpu_sibling_mask(boot_cpuid));
  607. cpumask_set_cpu(boot_cpuid, cpu_l2_cache_mask(boot_cpuid));
  608. cpumask_set_cpu(boot_cpuid, cpu_core_mask(boot_cpuid));
  609. if (smp_ops && smp_ops->probe)
  610. smp_ops->probe();
  611. }
  612. void smp_prepare_boot_cpu(void)
  613. {
  614. BUG_ON(smp_processor_id() != boot_cpuid);
  615. #ifdef CONFIG_PPC64
  616. paca_ptrs[boot_cpuid]->__current = current;
  617. #endif
  618. set_numa_node(numa_cpu_lookup_table[boot_cpuid]);
  619. current_set[boot_cpuid] = task_thread_info(current);
  620. }
  621. #ifdef CONFIG_HOTPLUG_CPU
  622. int generic_cpu_disable(void)
  623. {
  624. unsigned int cpu = smp_processor_id();
  625. if (cpu == boot_cpuid)
  626. return -EBUSY;
  627. set_cpu_online(cpu, false);
  628. #ifdef CONFIG_PPC64
  629. vdso_data->processorCount--;
  630. #endif
  631. /* Update affinity of all IRQs previously aimed at this CPU */
  632. irq_migrate_all_off_this_cpu();
  633. /*
  634. * Depending on the details of the interrupt controller, it's possible
  635. * that one of the interrupts we just migrated away from this CPU is
  636. * actually already pending on this CPU. If we leave it in that state
  637. * the interrupt will never be EOI'ed, and will never fire again. So
  638. * temporarily enable interrupts here, to allow any pending interrupt to
  639. * be received (and EOI'ed), before we take this CPU offline.
  640. */
  641. local_irq_enable();
  642. mdelay(1);
  643. local_irq_disable();
  644. return 0;
  645. }
  646. void generic_cpu_die(unsigned int cpu)
  647. {
  648. int i;
  649. for (i = 0; i < 100; i++) {
  650. smp_rmb();
  651. if (is_cpu_dead(cpu))
  652. return;
  653. msleep(100);
  654. }
  655. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  656. }
  657. void generic_set_cpu_dead(unsigned int cpu)
  658. {
  659. per_cpu(cpu_state, cpu) = CPU_DEAD;
  660. }
  661. /*
  662. * The cpu_state should be set to CPU_UP_PREPARE in kick_cpu(), otherwise
  663. * the cpu_state is always CPU_DEAD after calling generic_set_cpu_dead(),
  664. * which makes the delay in generic_cpu_die() not happen.
  665. */
  666. void generic_set_cpu_up(unsigned int cpu)
  667. {
  668. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  669. }
  670. int generic_check_cpu_restart(unsigned int cpu)
  671. {
  672. return per_cpu(cpu_state, cpu) == CPU_UP_PREPARE;
  673. }
  674. int is_cpu_dead(unsigned int cpu)
  675. {
  676. return per_cpu(cpu_state, cpu) == CPU_DEAD;
  677. }
  678. static bool secondaries_inhibited(void)
  679. {
  680. return kvm_hv_mode_active();
  681. }
  682. #else /* HOTPLUG_CPU */
  683. #define secondaries_inhibited() 0
  684. #endif
  685. static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
  686. {
  687. struct thread_info *ti = task_thread_info(idle);
  688. #ifdef CONFIG_PPC64
  689. paca_ptrs[cpu]->__current = idle;
  690. paca_ptrs[cpu]->kstack = (unsigned long)ti + THREAD_SIZE - STACK_FRAME_OVERHEAD;
  691. #endif
  692. ti->cpu = cpu;
  693. secondary_ti = current_set[cpu] = ti;
  694. }
  695. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  696. {
  697. int rc, c;
  698. /*
  699. * Don't allow secondary threads to come online if inhibited
  700. */
  701. if (threads_per_core > 1 && secondaries_inhibited() &&
  702. cpu_thread_in_subcore(cpu))
  703. return -EBUSY;
  704. if (smp_ops == NULL ||
  705. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  706. return -EINVAL;
  707. cpu_idle_thread_init(cpu, tidle);
  708. /*
  709. * The platform might need to allocate resources prior to bringing
  710. * up the CPU
  711. */
  712. if (smp_ops->prepare_cpu) {
  713. rc = smp_ops->prepare_cpu(cpu);
  714. if (rc)
  715. return rc;
  716. }
  717. /* Make sure callin-map entry is 0 (can be leftover a CPU
  718. * hotplug
  719. */
  720. cpu_callin_map[cpu] = 0;
  721. /* The information for processor bringup must
  722. * be written out to main store before we release
  723. * the processor.
  724. */
  725. smp_mb();
  726. /* wake up cpus */
  727. DBG("smp: kicking cpu %d\n", cpu);
  728. rc = smp_ops->kick_cpu(cpu);
  729. if (rc) {
  730. pr_err("smp: failed starting cpu %d (rc %d)\n", cpu, rc);
  731. return rc;
  732. }
  733. /*
  734. * wait to see if the cpu made a callin (is actually up).
  735. * use this value that I found through experimentation.
  736. * -- Cort
  737. */
  738. if (system_state < SYSTEM_RUNNING)
  739. for (c = 50000; c && !cpu_callin_map[cpu]; c--)
  740. udelay(100);
  741. #ifdef CONFIG_HOTPLUG_CPU
  742. else
  743. /*
  744. * CPUs can take much longer to come up in the
  745. * hotplug case. Wait five seconds.
  746. */
  747. for (c = 5000; c && !cpu_callin_map[cpu]; c--)
  748. msleep(1);
  749. #endif
  750. if (!cpu_callin_map[cpu]) {
  751. printk(KERN_ERR "Processor %u is stuck.\n", cpu);
  752. return -ENOENT;
  753. }
  754. DBG("Processor %u found.\n", cpu);
  755. if (smp_ops->give_timebase)
  756. smp_ops->give_timebase();
  757. /* Wait until cpu puts itself in the online & active maps */
  758. spin_until_cond(cpu_online(cpu));
  759. return 0;
  760. }
  761. /* Return the value of the reg property corresponding to the given
  762. * logical cpu.
  763. */
  764. int cpu_to_core_id(int cpu)
  765. {
  766. struct device_node *np;
  767. const __be32 *reg;
  768. int id = -1;
  769. np = of_get_cpu_node(cpu, NULL);
  770. if (!np)
  771. goto out;
  772. reg = of_get_property(np, "reg", NULL);
  773. if (!reg)
  774. goto out;
  775. id = be32_to_cpup(reg);
  776. out:
  777. of_node_put(np);
  778. return id;
  779. }
  780. EXPORT_SYMBOL_GPL(cpu_to_core_id);
  781. /* Helper routines for cpu to core mapping */
  782. int cpu_core_index_of_thread(int cpu)
  783. {
  784. return cpu >> threads_shift;
  785. }
  786. EXPORT_SYMBOL_GPL(cpu_core_index_of_thread);
  787. int cpu_first_thread_of_core(int core)
  788. {
  789. return core << threads_shift;
  790. }
  791. EXPORT_SYMBOL_GPL(cpu_first_thread_of_core);
  792. /* Must be called when no change can occur to cpu_present_mask,
  793. * i.e. during cpu online or offline.
  794. */
  795. static struct device_node *cpu_to_l2cache(int cpu)
  796. {
  797. struct device_node *np;
  798. struct device_node *cache;
  799. if (!cpu_present(cpu))
  800. return NULL;
  801. np = of_get_cpu_node(cpu, NULL);
  802. if (np == NULL)
  803. return NULL;
  804. cache = of_find_next_cache_node(np);
  805. of_node_put(np);
  806. return cache;
  807. }
  808. static bool update_mask_by_l2(int cpu, struct cpumask *(*mask_fn)(int))
  809. {
  810. struct device_node *l2_cache, *np;
  811. int i;
  812. l2_cache = cpu_to_l2cache(cpu);
  813. if (!l2_cache)
  814. return false;
  815. for_each_cpu(i, cpu_online_mask) {
  816. /*
  817. * when updating the marks the current CPU has not been marked
  818. * online, but we need to update the cache masks
  819. */
  820. np = cpu_to_l2cache(i);
  821. if (!np)
  822. continue;
  823. if (np == l2_cache)
  824. set_cpus_related(cpu, i, mask_fn);
  825. of_node_put(np);
  826. }
  827. of_node_put(l2_cache);
  828. return true;
  829. }
  830. #ifdef CONFIG_HOTPLUG_CPU
  831. static void remove_cpu_from_masks(int cpu)
  832. {
  833. int i;
  834. /* NB: cpu_core_mask is a superset of the others */
  835. for_each_cpu(i, cpu_core_mask(cpu)) {
  836. set_cpus_unrelated(cpu, i, cpu_core_mask);
  837. set_cpus_unrelated(cpu, i, cpu_l2_cache_mask);
  838. set_cpus_unrelated(cpu, i, cpu_sibling_mask);
  839. }
  840. }
  841. #endif
  842. static void add_cpu_to_masks(int cpu)
  843. {
  844. int first_thread = cpu_first_thread_sibling(cpu);
  845. int chipid = cpu_to_chip_id(cpu);
  846. int i;
  847. /*
  848. * This CPU will not be in the online mask yet so we need to manually
  849. * add it to it's own thread sibling mask.
  850. */
  851. cpumask_set_cpu(cpu, cpu_sibling_mask(cpu));
  852. for (i = first_thread; i < first_thread + threads_per_core; i++)
  853. if (cpu_online(i))
  854. set_cpus_related(i, cpu, cpu_sibling_mask);
  855. /*
  856. * Copy the thread sibling mask into the cache sibling mask
  857. * and mark any CPUs that share an L2 with this CPU.
  858. */
  859. for_each_cpu(i, cpu_sibling_mask(cpu))
  860. set_cpus_related(cpu, i, cpu_l2_cache_mask);
  861. update_mask_by_l2(cpu, cpu_l2_cache_mask);
  862. /*
  863. * Copy the cache sibling mask into core sibling mask and mark
  864. * any CPUs on the same chip as this CPU.
  865. */
  866. for_each_cpu(i, cpu_l2_cache_mask(cpu))
  867. set_cpus_related(cpu, i, cpu_core_mask);
  868. if (chipid == -1)
  869. return;
  870. for_each_cpu(i, cpu_online_mask)
  871. if (cpu_to_chip_id(i) == chipid)
  872. set_cpus_related(cpu, i, cpu_core_mask);
  873. }
  874. static bool shared_caches;
  875. /* Activate a secondary processor. */
  876. void start_secondary(void *unused)
  877. {
  878. unsigned int cpu = smp_processor_id();
  879. mmgrab(&init_mm);
  880. current->active_mm = &init_mm;
  881. smp_store_cpu_info(cpu);
  882. set_dec(tb_ticks_per_jiffy);
  883. preempt_disable();
  884. cpu_callin_map[cpu] = 1;
  885. if (smp_ops->setup_cpu)
  886. smp_ops->setup_cpu(cpu);
  887. if (smp_ops->take_timebase)
  888. smp_ops->take_timebase();
  889. secondary_cpu_time_init();
  890. #ifdef CONFIG_PPC64
  891. if (system_state == SYSTEM_RUNNING)
  892. vdso_data->processorCount++;
  893. vdso_getcpu_init();
  894. #endif
  895. /* Update topology CPU masks */
  896. add_cpu_to_masks(cpu);
  897. /*
  898. * Check for any shared caches. Note that this must be done on a
  899. * per-core basis because one core in the pair might be disabled.
  900. */
  901. if (!cpumask_equal(cpu_l2_cache_mask(cpu), cpu_sibling_mask(cpu)))
  902. shared_caches = true;
  903. set_numa_node(numa_cpu_lookup_table[cpu]);
  904. set_numa_mem(local_memory_node(numa_cpu_lookup_table[cpu]));
  905. smp_wmb();
  906. notify_cpu_starting(cpu);
  907. set_cpu_online(cpu, true);
  908. local_irq_enable();
  909. /* We can enable ftrace for secondary cpus now */
  910. this_cpu_enable_ftrace();
  911. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  912. BUG();
  913. }
  914. int setup_profiling_timer(unsigned int multiplier)
  915. {
  916. return 0;
  917. }
  918. #ifdef CONFIG_SCHED_SMT
  919. /* cpumask of CPUs with asymetric SMT dependancy */
  920. static int powerpc_smt_flags(void)
  921. {
  922. int flags = SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
  923. if (cpu_has_feature(CPU_FTR_ASYM_SMT)) {
  924. printk_once(KERN_INFO "Enabling Asymmetric SMT scheduling\n");
  925. flags |= SD_ASYM_PACKING;
  926. }
  927. return flags;
  928. }
  929. #endif
  930. static struct sched_domain_topology_level powerpc_topology[] = {
  931. #ifdef CONFIG_SCHED_SMT
  932. { cpu_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT) },
  933. #endif
  934. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  935. { NULL, },
  936. };
  937. /*
  938. * P9 has a slightly odd architecture where pairs of cores share an L2 cache.
  939. * This topology makes it *much* cheaper to migrate tasks between adjacent cores
  940. * since the migrated task remains cache hot. We want to take advantage of this
  941. * at the scheduler level so an extra topology level is required.
  942. */
  943. static int powerpc_shared_cache_flags(void)
  944. {
  945. return SD_SHARE_PKG_RESOURCES;
  946. }
  947. /*
  948. * We can't just pass cpu_l2_cache_mask() directly because
  949. * returns a non-const pointer and the compiler barfs on that.
  950. */
  951. static const struct cpumask *shared_cache_mask(int cpu)
  952. {
  953. return cpu_l2_cache_mask(cpu);
  954. }
  955. static struct sched_domain_topology_level power9_topology[] = {
  956. #ifdef CONFIG_SCHED_SMT
  957. { cpu_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT) },
  958. #endif
  959. { shared_cache_mask, powerpc_shared_cache_flags, SD_INIT_NAME(CACHE) },
  960. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  961. { NULL, },
  962. };
  963. void __init smp_cpus_done(unsigned int max_cpus)
  964. {
  965. /*
  966. * We are running pinned to the boot CPU, see rest_init().
  967. */
  968. if (smp_ops && smp_ops->setup_cpu)
  969. smp_ops->setup_cpu(boot_cpuid);
  970. if (smp_ops && smp_ops->bringup_done)
  971. smp_ops->bringup_done();
  972. dump_numa_cpu_topology();
  973. /*
  974. * If any CPU detects that it's sharing a cache with another CPU then
  975. * use the deeper topology that is aware of this sharing.
  976. */
  977. if (shared_caches) {
  978. pr_info("Using shared cache scheduler topology\n");
  979. set_sched_topology(power9_topology);
  980. } else {
  981. pr_info("Using standard scheduler topology\n");
  982. set_sched_topology(powerpc_topology);
  983. }
  984. }
  985. #ifdef CONFIG_HOTPLUG_CPU
  986. int __cpu_disable(void)
  987. {
  988. int cpu = smp_processor_id();
  989. int err;
  990. if (!smp_ops->cpu_disable)
  991. return -ENOSYS;
  992. this_cpu_disable_ftrace();
  993. err = smp_ops->cpu_disable();
  994. if (err)
  995. return err;
  996. /* Update sibling maps */
  997. remove_cpu_from_masks(cpu);
  998. return 0;
  999. }
  1000. void __cpu_die(unsigned int cpu)
  1001. {
  1002. if (smp_ops->cpu_die)
  1003. smp_ops->cpu_die(cpu);
  1004. }
  1005. void cpu_die(void)
  1006. {
  1007. /*
  1008. * Disable on the down path. This will be re-enabled by
  1009. * start_secondary() via start_secondary_resume() below
  1010. */
  1011. this_cpu_disable_ftrace();
  1012. if (ppc_md.cpu_die)
  1013. ppc_md.cpu_die();
  1014. /* If we return, we re-enter start_secondary */
  1015. start_secondary_resume();
  1016. }
  1017. #endif