smp.c 27 KB

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