smp.c 26 KB

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