smp.c 34 KB

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