smp.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. /* Remove this CPU */
  520. set_cpu_online(smp_processor_id(), false);
  521. spin_begin();
  522. while (1)
  523. spin_cpu_relax();
  524. }
  525. void smp_send_stop(void)
  526. {
  527. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, nmi_stop_this_cpu, 1000000);
  528. }
  529. #else /* CONFIG_NMI_IPI */
  530. static void stop_this_cpu(void *dummy)
  531. {
  532. /* Remove this CPU */
  533. set_cpu_online(smp_processor_id(), false);
  534. hard_irq_disable();
  535. spin_begin();
  536. while (1)
  537. spin_cpu_relax();
  538. }
  539. void smp_send_stop(void)
  540. {
  541. static bool stopped = false;
  542. /*
  543. * Prevent waiting on csd lock from a previous smp_send_stop.
  544. * This is racy, but in general callers try to do the right
  545. * thing and only fire off one smp_send_stop (e.g., see
  546. * kernel/panic.c)
  547. */
  548. if (stopped)
  549. return;
  550. stopped = true;
  551. smp_call_function(stop_this_cpu, NULL, 0);
  552. }
  553. #endif /* CONFIG_NMI_IPI */
  554. struct thread_info *current_set[NR_CPUS];
  555. static void smp_store_cpu_info(int id)
  556. {
  557. per_cpu(cpu_pvr, id) = mfspr(SPRN_PVR);
  558. #ifdef CONFIG_PPC_FSL_BOOK3E
  559. per_cpu(next_tlbcam_idx, id)
  560. = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
  561. #endif
  562. }
  563. /*
  564. * Relationships between CPUs are maintained in a set of per-cpu cpumasks so
  565. * rather than just passing around the cpumask we pass around a function that
  566. * returns the that cpumask for the given CPU.
  567. */
  568. static void set_cpus_related(int i, int j, struct cpumask *(*get_cpumask)(int))
  569. {
  570. cpumask_set_cpu(i, get_cpumask(j));
  571. cpumask_set_cpu(j, get_cpumask(i));
  572. }
  573. #ifdef CONFIG_HOTPLUG_CPU
  574. static void set_cpus_unrelated(int i, int j,
  575. struct cpumask *(*get_cpumask)(int))
  576. {
  577. cpumask_clear_cpu(i, get_cpumask(j));
  578. cpumask_clear_cpu(j, get_cpumask(i));
  579. }
  580. #endif
  581. void __init smp_prepare_cpus(unsigned int max_cpus)
  582. {
  583. unsigned int cpu;
  584. DBG("smp_prepare_cpus\n");
  585. /*
  586. * setup_cpu may need to be called on the boot cpu. We havent
  587. * spun any cpus up but lets be paranoid.
  588. */
  589. BUG_ON(boot_cpuid != smp_processor_id());
  590. /* Fixup boot cpu */
  591. smp_store_cpu_info(boot_cpuid);
  592. cpu_callin_map[boot_cpuid] = 1;
  593. for_each_possible_cpu(cpu) {
  594. zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu),
  595. GFP_KERNEL, cpu_to_node(cpu));
  596. zalloc_cpumask_var_node(&per_cpu(cpu_l2_cache_map, cpu),
  597. GFP_KERNEL, cpu_to_node(cpu));
  598. zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu),
  599. GFP_KERNEL, cpu_to_node(cpu));
  600. /*
  601. * numa_node_id() works after this.
  602. */
  603. if (cpu_present(cpu)) {
  604. set_cpu_numa_node(cpu, numa_cpu_lookup_table[cpu]);
  605. set_cpu_numa_mem(cpu,
  606. local_memory_node(numa_cpu_lookup_table[cpu]));
  607. }
  608. }
  609. /* Init the cpumasks so the boot CPU is related to itself */
  610. cpumask_set_cpu(boot_cpuid, cpu_sibling_mask(boot_cpuid));
  611. cpumask_set_cpu(boot_cpuid, cpu_l2_cache_mask(boot_cpuid));
  612. cpumask_set_cpu(boot_cpuid, cpu_core_mask(boot_cpuid));
  613. if (smp_ops && smp_ops->probe)
  614. smp_ops->probe();
  615. }
  616. void smp_prepare_boot_cpu(void)
  617. {
  618. BUG_ON(smp_processor_id() != boot_cpuid);
  619. #ifdef CONFIG_PPC64
  620. paca_ptrs[boot_cpuid]->__current = current;
  621. #endif
  622. set_numa_node(numa_cpu_lookup_table[boot_cpuid]);
  623. current_set[boot_cpuid] = task_thread_info(current);
  624. }
  625. #ifdef CONFIG_HOTPLUG_CPU
  626. int generic_cpu_disable(void)
  627. {
  628. unsigned int cpu = smp_processor_id();
  629. if (cpu == boot_cpuid)
  630. return -EBUSY;
  631. set_cpu_online(cpu, false);
  632. #ifdef CONFIG_PPC64
  633. vdso_data->processorCount--;
  634. #endif
  635. /* Update affinity of all IRQs previously aimed at this CPU */
  636. irq_migrate_all_off_this_cpu();
  637. /*
  638. * Depending on the details of the interrupt controller, it's possible
  639. * that one of the interrupts we just migrated away from this CPU is
  640. * actually already pending on this CPU. If we leave it in that state
  641. * the interrupt will never be EOI'ed, and will never fire again. So
  642. * temporarily enable interrupts here, to allow any pending interrupt to
  643. * be received (and EOI'ed), before we take this CPU offline.
  644. */
  645. local_irq_enable();
  646. mdelay(1);
  647. local_irq_disable();
  648. return 0;
  649. }
  650. void generic_cpu_die(unsigned int cpu)
  651. {
  652. int i;
  653. for (i = 0; i < 100; i++) {
  654. smp_rmb();
  655. if (is_cpu_dead(cpu))
  656. return;
  657. msleep(100);
  658. }
  659. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  660. }
  661. void generic_set_cpu_dead(unsigned int cpu)
  662. {
  663. per_cpu(cpu_state, cpu) = CPU_DEAD;
  664. }
  665. /*
  666. * The cpu_state should be set to CPU_UP_PREPARE in kick_cpu(), otherwise
  667. * the cpu_state is always CPU_DEAD after calling generic_set_cpu_dead(),
  668. * which makes the delay in generic_cpu_die() not happen.
  669. */
  670. void generic_set_cpu_up(unsigned int cpu)
  671. {
  672. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  673. }
  674. int generic_check_cpu_restart(unsigned int cpu)
  675. {
  676. return per_cpu(cpu_state, cpu) == CPU_UP_PREPARE;
  677. }
  678. int is_cpu_dead(unsigned int cpu)
  679. {
  680. return per_cpu(cpu_state, cpu) == CPU_DEAD;
  681. }
  682. static bool secondaries_inhibited(void)
  683. {
  684. return kvm_hv_mode_active();
  685. }
  686. #else /* HOTPLUG_CPU */
  687. #define secondaries_inhibited() 0
  688. #endif
  689. static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
  690. {
  691. struct thread_info *ti = task_thread_info(idle);
  692. #ifdef CONFIG_PPC64
  693. paca_ptrs[cpu]->__current = idle;
  694. paca_ptrs[cpu]->kstack = (unsigned long)ti + THREAD_SIZE - STACK_FRAME_OVERHEAD;
  695. #endif
  696. ti->cpu = cpu;
  697. secondary_ti = current_set[cpu] = ti;
  698. }
  699. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  700. {
  701. int rc, c;
  702. /*
  703. * Don't allow secondary threads to come online if inhibited
  704. */
  705. if (threads_per_core > 1 && secondaries_inhibited() &&
  706. cpu_thread_in_subcore(cpu))
  707. return -EBUSY;
  708. if (smp_ops == NULL ||
  709. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  710. return -EINVAL;
  711. cpu_idle_thread_init(cpu, tidle);
  712. /*
  713. * The platform might need to allocate resources prior to bringing
  714. * up the CPU
  715. */
  716. if (smp_ops->prepare_cpu) {
  717. rc = smp_ops->prepare_cpu(cpu);
  718. if (rc)
  719. return rc;
  720. }
  721. /* Make sure callin-map entry is 0 (can be leftover a CPU
  722. * hotplug
  723. */
  724. cpu_callin_map[cpu] = 0;
  725. /* The information for processor bringup must
  726. * be written out to main store before we release
  727. * the processor.
  728. */
  729. smp_mb();
  730. /* wake up cpus */
  731. DBG("smp: kicking cpu %d\n", cpu);
  732. rc = smp_ops->kick_cpu(cpu);
  733. if (rc) {
  734. pr_err("smp: failed starting cpu %d (rc %d)\n", cpu, rc);
  735. return rc;
  736. }
  737. /*
  738. * wait to see if the cpu made a callin (is actually up).
  739. * use this value that I found through experimentation.
  740. * -- Cort
  741. */
  742. if (system_state < SYSTEM_RUNNING)
  743. for (c = 50000; c && !cpu_callin_map[cpu]; c--)
  744. udelay(100);
  745. #ifdef CONFIG_HOTPLUG_CPU
  746. else
  747. /*
  748. * CPUs can take much longer to come up in the
  749. * hotplug case. Wait five seconds.
  750. */
  751. for (c = 5000; c && !cpu_callin_map[cpu]; c--)
  752. msleep(1);
  753. #endif
  754. if (!cpu_callin_map[cpu]) {
  755. printk(KERN_ERR "Processor %u is stuck.\n", cpu);
  756. return -ENOENT;
  757. }
  758. DBG("Processor %u found.\n", cpu);
  759. if (smp_ops->give_timebase)
  760. smp_ops->give_timebase();
  761. /* Wait until cpu puts itself in the online & active maps */
  762. spin_until_cond(cpu_online(cpu));
  763. return 0;
  764. }
  765. /* Return the value of the reg property corresponding to the given
  766. * logical cpu.
  767. */
  768. int cpu_to_core_id(int cpu)
  769. {
  770. struct device_node *np;
  771. const __be32 *reg;
  772. int id = -1;
  773. np = of_get_cpu_node(cpu, NULL);
  774. if (!np)
  775. goto out;
  776. reg = of_get_property(np, "reg", NULL);
  777. if (!reg)
  778. goto out;
  779. id = be32_to_cpup(reg);
  780. out:
  781. of_node_put(np);
  782. return id;
  783. }
  784. EXPORT_SYMBOL_GPL(cpu_to_core_id);
  785. /* Helper routines for cpu to core mapping */
  786. int cpu_core_index_of_thread(int cpu)
  787. {
  788. return cpu >> threads_shift;
  789. }
  790. EXPORT_SYMBOL_GPL(cpu_core_index_of_thread);
  791. int cpu_first_thread_of_core(int core)
  792. {
  793. return core << threads_shift;
  794. }
  795. EXPORT_SYMBOL_GPL(cpu_first_thread_of_core);
  796. /* Must be called when no change can occur to cpu_present_mask,
  797. * i.e. during cpu online or offline.
  798. */
  799. static struct device_node *cpu_to_l2cache(int cpu)
  800. {
  801. struct device_node *np;
  802. struct device_node *cache;
  803. if (!cpu_present(cpu))
  804. return NULL;
  805. np = of_get_cpu_node(cpu, NULL);
  806. if (np == NULL)
  807. return NULL;
  808. cache = of_find_next_cache_node(np);
  809. of_node_put(np);
  810. return cache;
  811. }
  812. static bool update_mask_by_l2(int cpu, struct cpumask *(*mask_fn)(int))
  813. {
  814. struct device_node *l2_cache, *np;
  815. int i;
  816. l2_cache = cpu_to_l2cache(cpu);
  817. if (!l2_cache)
  818. return false;
  819. for_each_cpu(i, cpu_online_mask) {
  820. /*
  821. * when updating the marks the current CPU has not been marked
  822. * online, but we need to update the cache masks
  823. */
  824. np = cpu_to_l2cache(i);
  825. if (!np)
  826. continue;
  827. if (np == l2_cache)
  828. set_cpus_related(cpu, i, mask_fn);
  829. of_node_put(np);
  830. }
  831. of_node_put(l2_cache);
  832. return true;
  833. }
  834. #ifdef CONFIG_HOTPLUG_CPU
  835. static void remove_cpu_from_masks(int cpu)
  836. {
  837. int i;
  838. /* NB: cpu_core_mask is a superset of the others */
  839. for_each_cpu(i, cpu_core_mask(cpu)) {
  840. set_cpus_unrelated(cpu, i, cpu_core_mask);
  841. set_cpus_unrelated(cpu, i, cpu_l2_cache_mask);
  842. set_cpus_unrelated(cpu, i, cpu_sibling_mask);
  843. }
  844. }
  845. #endif
  846. static void add_cpu_to_masks(int cpu)
  847. {
  848. int first_thread = cpu_first_thread_sibling(cpu);
  849. int chipid = cpu_to_chip_id(cpu);
  850. int i;
  851. /*
  852. * This CPU will not be in the online mask yet so we need to manually
  853. * add it to it's own thread sibling mask.
  854. */
  855. cpumask_set_cpu(cpu, cpu_sibling_mask(cpu));
  856. for (i = first_thread; i < first_thread + threads_per_core; i++)
  857. if (cpu_online(i))
  858. set_cpus_related(i, cpu, cpu_sibling_mask);
  859. /*
  860. * Copy the thread sibling mask into the cache sibling mask
  861. * and mark any CPUs that share an L2 with this CPU.
  862. */
  863. for_each_cpu(i, cpu_sibling_mask(cpu))
  864. set_cpus_related(cpu, i, cpu_l2_cache_mask);
  865. update_mask_by_l2(cpu, cpu_l2_cache_mask);
  866. /*
  867. * Copy the cache sibling mask into core sibling mask and mark
  868. * any CPUs on the same chip as this CPU.
  869. */
  870. for_each_cpu(i, cpu_l2_cache_mask(cpu))
  871. set_cpus_related(cpu, i, cpu_core_mask);
  872. if (chipid == -1)
  873. return;
  874. for_each_cpu(i, cpu_online_mask)
  875. if (cpu_to_chip_id(i) == chipid)
  876. set_cpus_related(cpu, i, cpu_core_mask);
  877. }
  878. static bool shared_caches;
  879. /* Activate a secondary processor. */
  880. void start_secondary(void *unused)
  881. {
  882. unsigned int cpu = smp_processor_id();
  883. mmgrab(&init_mm);
  884. current->active_mm = &init_mm;
  885. smp_store_cpu_info(cpu);
  886. set_dec(tb_ticks_per_jiffy);
  887. preempt_disable();
  888. cpu_callin_map[cpu] = 1;
  889. if (smp_ops->setup_cpu)
  890. smp_ops->setup_cpu(cpu);
  891. if (smp_ops->take_timebase)
  892. smp_ops->take_timebase();
  893. secondary_cpu_time_init();
  894. #ifdef CONFIG_PPC64
  895. if (system_state == SYSTEM_RUNNING)
  896. vdso_data->processorCount++;
  897. vdso_getcpu_init();
  898. #endif
  899. /* Update topology CPU masks */
  900. add_cpu_to_masks(cpu);
  901. /*
  902. * Check for any shared caches. Note that this must be done on a
  903. * per-core basis because one core in the pair might be disabled.
  904. */
  905. if (!cpumask_equal(cpu_l2_cache_mask(cpu), cpu_sibling_mask(cpu)))
  906. shared_caches = true;
  907. set_numa_node(numa_cpu_lookup_table[cpu]);
  908. set_numa_mem(local_memory_node(numa_cpu_lookup_table[cpu]));
  909. smp_wmb();
  910. notify_cpu_starting(cpu);
  911. set_cpu_online(cpu, true);
  912. local_irq_enable();
  913. /* We can enable ftrace for secondary cpus now */
  914. this_cpu_enable_ftrace();
  915. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  916. BUG();
  917. }
  918. int setup_profiling_timer(unsigned int multiplier)
  919. {
  920. return 0;
  921. }
  922. #ifdef CONFIG_SCHED_SMT
  923. /* cpumask of CPUs with asymetric SMT dependancy */
  924. static int powerpc_smt_flags(void)
  925. {
  926. int flags = SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
  927. if (cpu_has_feature(CPU_FTR_ASYM_SMT)) {
  928. printk_once(KERN_INFO "Enabling Asymmetric SMT scheduling\n");
  929. flags |= SD_ASYM_PACKING;
  930. }
  931. return flags;
  932. }
  933. #endif
  934. static struct sched_domain_topology_level powerpc_topology[] = {
  935. #ifdef CONFIG_SCHED_SMT
  936. { cpu_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT) },
  937. #endif
  938. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  939. { NULL, },
  940. };
  941. /*
  942. * P9 has a slightly odd architecture where pairs of cores share an L2 cache.
  943. * This topology makes it *much* cheaper to migrate tasks between adjacent cores
  944. * since the migrated task remains cache hot. We want to take advantage of this
  945. * at the scheduler level so an extra topology level is required.
  946. */
  947. static int powerpc_shared_cache_flags(void)
  948. {
  949. return SD_SHARE_PKG_RESOURCES;
  950. }
  951. /*
  952. * We can't just pass cpu_l2_cache_mask() directly because
  953. * returns a non-const pointer and the compiler barfs on that.
  954. */
  955. static const struct cpumask *shared_cache_mask(int cpu)
  956. {
  957. return cpu_l2_cache_mask(cpu);
  958. }
  959. static struct sched_domain_topology_level power9_topology[] = {
  960. #ifdef CONFIG_SCHED_SMT
  961. { cpu_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT) },
  962. #endif
  963. { shared_cache_mask, powerpc_shared_cache_flags, SD_INIT_NAME(CACHE) },
  964. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  965. { NULL, },
  966. };
  967. void __init smp_cpus_done(unsigned int max_cpus)
  968. {
  969. /*
  970. * We are running pinned to the boot CPU, see rest_init().
  971. */
  972. if (smp_ops && smp_ops->setup_cpu)
  973. smp_ops->setup_cpu(boot_cpuid);
  974. if (smp_ops && smp_ops->bringup_done)
  975. smp_ops->bringup_done();
  976. dump_numa_cpu_topology();
  977. /*
  978. * If any CPU detects that it's sharing a cache with another CPU then
  979. * use the deeper topology that is aware of this sharing.
  980. */
  981. if (shared_caches) {
  982. pr_info("Using shared cache scheduler topology\n");
  983. set_sched_topology(power9_topology);
  984. } else {
  985. pr_info("Using standard scheduler topology\n");
  986. set_sched_topology(powerpc_topology);
  987. }
  988. }
  989. #ifdef CONFIG_HOTPLUG_CPU
  990. int __cpu_disable(void)
  991. {
  992. int cpu = smp_processor_id();
  993. int err;
  994. if (!smp_ops->cpu_disable)
  995. return -ENOSYS;
  996. this_cpu_disable_ftrace();
  997. err = smp_ops->cpu_disable();
  998. if (err)
  999. return err;
  1000. /* Update sibling maps */
  1001. remove_cpu_from_masks(cpu);
  1002. return 0;
  1003. }
  1004. void __cpu_die(unsigned int cpu)
  1005. {
  1006. if (smp_ops->cpu_die)
  1007. smp_ops->cpu_die(cpu);
  1008. }
  1009. void cpu_die(void)
  1010. {
  1011. /*
  1012. * Disable on the down path. This will be re-enabled by
  1013. * start_secondary() via start_secondary_resume() below
  1014. */
  1015. this_cpu_disable_ftrace();
  1016. if (ppc_md.cpu_die)
  1017. ppc_md.cpu_die();
  1018. /* If we return, we re-enter start_secondary */
  1019. start_secondary_resume();
  1020. }
  1021. #endif