smp.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SMP support for ppc.
  4. *
  5. * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
  6. * deal of code from the sparc and intel versions.
  7. *
  8. * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  9. *
  10. * PowerPC-64 Support added by Dave Engebretsen, Peter Bergner, and
  11. * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
  12. */
  13. #undef DEBUG
  14. #include <linux/kernel.h>
  15. #include <linux/export.h>
  16. #include <linux/sched/mm.h>
  17. #include <linux/sched/task_stack.h>
  18. #include <linux/sched/topology.h>
  19. #include <linux/smp.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/cache.h>
  25. #include <linux/err.h>
  26. #include <linux/device.h>
  27. #include <linux/cpu.h>
  28. #include <linux/notifier.h>
  29. #include <linux/topology.h>
  30. #include <linux/profile.h>
  31. #include <linux/processor.h>
  32. #include <linux/random.h>
  33. #include <linux/stackprotector.h>
  34. #include <linux/pgtable.h>
  35. #include <linux/clockchips.h>
  36. #include <linux/kexec.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/smp.h>
  45. #include <asm/time.h>
  46. #include <asm/machdep.h>
  47. #include <asm/mmu_context.h>
  48. #include <asm/cputhreads.h>
  49. #include <asm/cputable.h>
  50. #include <asm/mpic.h>
  51. #include <asm/vdso_datapage.h>
  52. #ifdef CONFIG_PPC64
  53. #include <asm/paca.h>
  54. #endif
  55. #include <asm/vdso.h>
  56. #include <asm/debug.h>
  57. #include <asm/cpu_has_feature.h>
  58. #include <asm/ftrace.h>
  59. #include <asm/kup.h>
  60. #include <asm/fadump.h>
  61. #include <trace/events/ipi.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 task_struct *secondary_current;
  73. bool has_big_cores __ro_after_init;
  74. bool coregroup_enabled __ro_after_init;
  75. bool thread_group_shares_l2 __ro_after_init;
  76. bool thread_group_shares_l3 __ro_after_init;
  77. DEFINE_PER_CPU(cpumask_var_t, cpu_sibling_map);
  78. DEFINE_PER_CPU(cpumask_var_t, cpu_smallcore_map);
  79. DEFINE_PER_CPU(cpumask_var_t, cpu_l2_cache_map);
  80. DEFINE_PER_CPU(cpumask_var_t, cpu_core_map);
  81. static DEFINE_PER_CPU(cpumask_var_t, cpu_coregroup_map);
  82. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  83. EXPORT_PER_CPU_SYMBOL(cpu_l2_cache_map);
  84. EXPORT_PER_CPU_SYMBOL(cpu_core_map);
  85. EXPORT_SYMBOL_GPL(has_big_cores);
  86. #define MAX_THREAD_LIST_SIZE 8
  87. #define THREAD_GROUP_SHARE_L1 1
  88. #define THREAD_GROUP_SHARE_L2_L3 2
  89. struct thread_groups {
  90. unsigned int property;
  91. unsigned int nr_groups;
  92. unsigned int threads_per_group;
  93. unsigned int thread_list[MAX_THREAD_LIST_SIZE];
  94. };
  95. /* Maximum number of properties that groups of threads within a core can share */
  96. #define MAX_THREAD_GROUP_PROPERTIES 2
  97. struct thread_groups_list {
  98. unsigned int nr_properties;
  99. struct thread_groups property_tgs[MAX_THREAD_GROUP_PROPERTIES];
  100. };
  101. static struct thread_groups_list tgl[NR_CPUS] __initdata;
  102. /*
  103. * On big-cores system, thread_group_l1_cache_map for each CPU corresponds to
  104. * the set its siblings that share the L1-cache.
  105. */
  106. DEFINE_PER_CPU(cpumask_var_t, thread_group_l1_cache_map);
  107. /*
  108. * On some big-cores system, thread_group_l2_cache_map for each CPU
  109. * corresponds to the set its siblings within the core that share the
  110. * L2-cache.
  111. */
  112. DEFINE_PER_CPU(cpumask_var_t, thread_group_l2_cache_map);
  113. /*
  114. * On P10, thread_group_l3_cache_map for each CPU is equal to the
  115. * thread_group_l2_cache_map
  116. */
  117. DEFINE_PER_CPU(cpumask_var_t, thread_group_l3_cache_map);
  118. /* SMP operations for this machine */
  119. struct smp_ops_t *smp_ops;
  120. /* Can't be static due to PowerMac hackery */
  121. volatile unsigned int cpu_callin_map[NR_CPUS];
  122. int smt_enabled_at_boot = 1;
  123. /*
  124. * Returns 1 if the specified cpu should be brought up during boot.
  125. * Used to inhibit booting threads if they've been disabled or
  126. * limited on the command line
  127. */
  128. int smp_generic_cpu_bootable(unsigned int nr)
  129. {
  130. /* Special case - we inhibit secondary thread startup
  131. * during boot if the user requests it.
  132. */
  133. if (system_state < SYSTEM_RUNNING && cpu_has_feature(CPU_FTR_SMT)) {
  134. if (!smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
  135. return 0;
  136. if (smt_enabled_at_boot
  137. && cpu_thread_in_core(nr) >= smt_enabled_at_boot)
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. #ifdef CONFIG_PPC64
  143. int smp_generic_kick_cpu(int nr)
  144. {
  145. if (nr < 0 || nr >= nr_cpu_ids)
  146. return -EINVAL;
  147. /*
  148. * The processor is currently spinning, waiting for the
  149. * cpu_start field to become non-zero After we set cpu_start,
  150. * the processor will continue on to secondary_start
  151. */
  152. if (!paca_ptrs[nr]->cpu_start) {
  153. paca_ptrs[nr]->cpu_start = 1;
  154. smp_mb();
  155. return 0;
  156. }
  157. #ifdef CONFIG_HOTPLUG_CPU
  158. /*
  159. * Ok it's not there, so it might be soft-unplugged, let's
  160. * try to bring it back
  161. */
  162. generic_set_cpu_up(nr);
  163. smp_wmb();
  164. smp_send_reschedule(nr);
  165. #endif /* CONFIG_HOTPLUG_CPU */
  166. return 0;
  167. }
  168. #endif /* CONFIG_PPC64 */
  169. static irqreturn_t call_function_action(int irq, void *data)
  170. {
  171. generic_smp_call_function_interrupt();
  172. return IRQ_HANDLED;
  173. }
  174. static irqreturn_t reschedule_action(int irq, void *data)
  175. {
  176. scheduler_ipi();
  177. return IRQ_HANDLED;
  178. }
  179. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  180. static irqreturn_t tick_broadcast_ipi_action(int irq, void *data)
  181. {
  182. timer_broadcast_interrupt();
  183. return IRQ_HANDLED;
  184. }
  185. #endif
  186. #ifdef CONFIG_NMI_IPI
  187. static irqreturn_t nmi_ipi_action(int irq, void *data)
  188. {
  189. smp_handle_nmi_ipi(get_irq_regs());
  190. return IRQ_HANDLED;
  191. }
  192. #endif
  193. static irq_handler_t smp_ipi_action[] = {
  194. [PPC_MSG_CALL_FUNCTION] = call_function_action,
  195. [PPC_MSG_RESCHEDULE] = reschedule_action,
  196. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  197. [PPC_MSG_TICK_BROADCAST] = tick_broadcast_ipi_action,
  198. #endif
  199. #ifdef CONFIG_NMI_IPI
  200. [PPC_MSG_NMI_IPI] = nmi_ipi_action,
  201. #endif
  202. };
  203. /*
  204. * The NMI IPI is a fallback and not truly non-maskable. It is simpler
  205. * than going through the call function infrastructure, and strongly
  206. * serialized, so it is more appropriate for debugging.
  207. */
  208. const char *smp_ipi_name[] = {
  209. [PPC_MSG_CALL_FUNCTION] = "ipi call function",
  210. [PPC_MSG_RESCHEDULE] = "ipi reschedule",
  211. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  212. [PPC_MSG_TICK_BROADCAST] = "ipi tick-broadcast",
  213. #endif
  214. #ifdef CONFIG_NMI_IPI
  215. [PPC_MSG_NMI_IPI] = "nmi ipi",
  216. #endif
  217. };
  218. /* optional function to request ipi, for controllers with >= 4 ipis */
  219. int smp_request_message_ipi(int virq, int msg)
  220. {
  221. int err;
  222. if (msg < 0 || msg > PPC_MSG_NMI_IPI)
  223. return -EINVAL;
  224. #ifndef CONFIG_NMI_IPI
  225. if (msg == PPC_MSG_NMI_IPI)
  226. return 1;
  227. #endif
  228. err = request_irq(virq, smp_ipi_action[msg],
  229. IRQF_PERCPU | IRQF_NO_THREAD | IRQF_NO_SUSPEND,
  230. smp_ipi_name[msg], NULL);
  231. WARN(err < 0, "unable to request_irq %d for %s (rc %d)\n",
  232. virq, smp_ipi_name[msg], err);
  233. return err;
  234. }
  235. #ifdef CONFIG_PPC_SMP_MUXED_IPI
  236. struct cpu_messages {
  237. long messages; /* current messages */
  238. };
  239. static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpu_messages, ipi_message);
  240. void smp_muxed_ipi_set_message(int cpu, int msg)
  241. {
  242. struct cpu_messages *info = &per_cpu(ipi_message, cpu);
  243. char *message = (char *)&info->messages;
  244. /*
  245. * Order previous accesses before accesses in the IPI handler.
  246. */
  247. smp_mb();
  248. WRITE_ONCE(message[msg], 1);
  249. }
  250. void smp_muxed_ipi_message_pass(int cpu, int msg)
  251. {
  252. smp_muxed_ipi_set_message(cpu, msg);
  253. /*
  254. * cause_ipi functions are required to include a full barrier
  255. * before doing whatever causes the IPI.
  256. */
  257. smp_ops->cause_ipi(cpu);
  258. }
  259. #ifdef __BIG_ENDIAN__
  260. #define IPI_MESSAGE(A) (1uL << ((BITS_PER_LONG - 8) - 8 * (A)))
  261. #else
  262. #define IPI_MESSAGE(A) (1uL << (8 * (A)))
  263. #endif
  264. irqreturn_t smp_ipi_demux(void)
  265. {
  266. mb(); /* order any irq clear */
  267. return smp_ipi_demux_relaxed();
  268. }
  269. /* sync-free variant. Callers should ensure synchronization */
  270. irqreturn_t smp_ipi_demux_relaxed(void)
  271. {
  272. struct cpu_messages *info;
  273. unsigned long all;
  274. info = this_cpu_ptr(&ipi_message);
  275. do {
  276. all = xchg(&info->messages, 0);
  277. #if defined(CONFIG_KVM_XICS) && defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
  278. /*
  279. * Must check for PPC_MSG_RM_HOST_ACTION messages
  280. * before PPC_MSG_CALL_FUNCTION messages because when
  281. * a VM is destroyed, we call kick_all_cpus_sync()
  282. * to ensure that any pending PPC_MSG_RM_HOST_ACTION
  283. * messages have completed before we free any VCPUs.
  284. */
  285. if (all & IPI_MESSAGE(PPC_MSG_RM_HOST_ACTION))
  286. kvmppc_xics_ipi_action();
  287. #endif
  288. if (all & IPI_MESSAGE(PPC_MSG_CALL_FUNCTION))
  289. generic_smp_call_function_interrupt();
  290. if (all & IPI_MESSAGE(PPC_MSG_RESCHEDULE))
  291. scheduler_ipi();
  292. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  293. if (all & IPI_MESSAGE(PPC_MSG_TICK_BROADCAST))
  294. timer_broadcast_interrupt();
  295. #endif
  296. #ifdef CONFIG_NMI_IPI
  297. if (all & IPI_MESSAGE(PPC_MSG_NMI_IPI))
  298. nmi_ipi_action(0, NULL);
  299. #endif
  300. } while (READ_ONCE(info->messages));
  301. return IRQ_HANDLED;
  302. }
  303. #endif /* CONFIG_PPC_SMP_MUXED_IPI */
  304. static inline void do_message_pass(int cpu, int msg)
  305. {
  306. if (smp_ops->message_pass)
  307. smp_ops->message_pass(cpu, msg);
  308. #ifdef CONFIG_PPC_SMP_MUXED_IPI
  309. else
  310. smp_muxed_ipi_message_pass(cpu, msg);
  311. #endif
  312. }
  313. void arch_smp_send_reschedule(int cpu)
  314. {
  315. if (likely(smp_ops))
  316. do_message_pass(cpu, PPC_MSG_RESCHEDULE);
  317. }
  318. EXPORT_SYMBOL_GPL(arch_smp_send_reschedule);
  319. void arch_send_call_function_single_ipi(int cpu)
  320. {
  321. do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
  322. }
  323. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  324. {
  325. unsigned int cpu;
  326. for_each_cpu(cpu, mask)
  327. do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
  328. }
  329. #ifdef CONFIG_NMI_IPI
  330. /*
  331. * "NMI IPI" system.
  332. *
  333. * NMI IPIs may not be recoverable, so should not be used as ongoing part of
  334. * a running system. They can be used for crash, debug, halt/reboot, etc.
  335. *
  336. * The IPI call waits with interrupts disabled until all targets enter the
  337. * NMI handler, then returns. Subsequent IPIs can be issued before targets
  338. * have returned from their handlers, so there is no guarantee about
  339. * concurrency or re-entrancy.
  340. *
  341. * A new NMI can be issued before all targets exit the handler.
  342. *
  343. * The IPI call may time out without all targets entering the NMI handler.
  344. * In that case, there is some logic to recover (and ignore subsequent
  345. * NMI interrupts that may eventually be raised), but the platform interrupt
  346. * handler may not be able to distinguish this from other exception causes,
  347. * which may cause a crash.
  348. */
  349. static atomic_t __nmi_ipi_lock = ATOMIC_INIT(0);
  350. static struct cpumask nmi_ipi_pending_mask;
  351. static bool nmi_ipi_busy = false;
  352. static void (*nmi_ipi_function)(struct pt_regs *) = NULL;
  353. noinstr static void nmi_ipi_lock_start(unsigned long *flags)
  354. {
  355. raw_local_irq_save(*flags);
  356. hard_irq_disable();
  357. while (raw_atomic_cmpxchg(&__nmi_ipi_lock, 0, 1) == 1) {
  358. raw_local_irq_restore(*flags);
  359. spin_until_cond(raw_atomic_read(&__nmi_ipi_lock) == 0);
  360. raw_local_irq_save(*flags);
  361. hard_irq_disable();
  362. }
  363. }
  364. noinstr static void nmi_ipi_lock(void)
  365. {
  366. while (raw_atomic_cmpxchg(&__nmi_ipi_lock, 0, 1) == 1)
  367. spin_until_cond(raw_atomic_read(&__nmi_ipi_lock) == 0);
  368. }
  369. noinstr static void nmi_ipi_unlock(void)
  370. {
  371. smp_mb();
  372. WARN_ON(raw_atomic_read(&__nmi_ipi_lock) != 1);
  373. raw_atomic_set(&__nmi_ipi_lock, 0);
  374. }
  375. noinstr static void nmi_ipi_unlock_end(unsigned long *flags)
  376. {
  377. nmi_ipi_unlock();
  378. raw_local_irq_restore(*flags);
  379. }
  380. /*
  381. * Platform NMI handler calls this to ack
  382. */
  383. noinstr int smp_handle_nmi_ipi(struct pt_regs *regs)
  384. {
  385. void (*fn)(struct pt_regs *) = NULL;
  386. unsigned long flags;
  387. int me = raw_smp_processor_id();
  388. int ret = 0;
  389. /*
  390. * Unexpected NMIs are possible here because the interrupt may not
  391. * be able to distinguish NMI IPIs from other types of NMIs, or
  392. * because the caller may have timed out.
  393. */
  394. nmi_ipi_lock_start(&flags);
  395. if (cpumask_test_cpu(me, &nmi_ipi_pending_mask)) {
  396. cpumask_clear_cpu(me, &nmi_ipi_pending_mask);
  397. fn = READ_ONCE(nmi_ipi_function);
  398. WARN_ON_ONCE(!fn);
  399. ret = 1;
  400. }
  401. nmi_ipi_unlock_end(&flags);
  402. if (fn)
  403. fn(regs);
  404. return ret;
  405. }
  406. static void do_smp_send_nmi_ipi(int cpu, bool safe)
  407. {
  408. if (!safe && smp_ops->cause_nmi_ipi && smp_ops->cause_nmi_ipi(cpu))
  409. return;
  410. if (cpu >= 0) {
  411. do_message_pass(cpu, PPC_MSG_NMI_IPI);
  412. } else {
  413. int c;
  414. for_each_online_cpu(c) {
  415. if (c == raw_smp_processor_id())
  416. continue;
  417. do_message_pass(c, PPC_MSG_NMI_IPI);
  418. }
  419. }
  420. }
  421. /*
  422. * - cpu is the target CPU (must not be this CPU), or NMI_IPI_ALL_OTHERS.
  423. * - fn is the target callback function.
  424. * - delay_us > 0 is the delay before giving up waiting for targets to
  425. * begin executing the handler, == 0 specifies indefinite delay.
  426. */
  427. static int __smp_send_nmi_ipi(int cpu, void (*fn)(struct pt_regs *),
  428. u64 delay_us, bool safe)
  429. {
  430. unsigned long flags;
  431. int me = raw_smp_processor_id();
  432. int ret = 1;
  433. BUG_ON(cpu == me);
  434. BUG_ON(cpu < 0 && cpu != NMI_IPI_ALL_OTHERS);
  435. if (unlikely(!smp_ops))
  436. return 0;
  437. nmi_ipi_lock_start(&flags);
  438. while (nmi_ipi_busy) {
  439. nmi_ipi_unlock_end(&flags);
  440. spin_until_cond(!nmi_ipi_busy);
  441. nmi_ipi_lock_start(&flags);
  442. }
  443. nmi_ipi_busy = true;
  444. nmi_ipi_function = fn;
  445. WARN_ON_ONCE(!cpumask_empty(&nmi_ipi_pending_mask));
  446. if (cpu < 0) {
  447. /* ALL_OTHERS */
  448. cpumask_copy(&nmi_ipi_pending_mask, cpu_online_mask);
  449. cpumask_clear_cpu(me, &nmi_ipi_pending_mask);
  450. } else {
  451. cpumask_set_cpu(cpu, &nmi_ipi_pending_mask);
  452. }
  453. nmi_ipi_unlock();
  454. /* Interrupts remain hard disabled */
  455. do_smp_send_nmi_ipi(cpu, safe);
  456. nmi_ipi_lock();
  457. /* nmi_ipi_busy is set here, so unlock/lock is okay */
  458. while (!cpumask_empty(&nmi_ipi_pending_mask)) {
  459. nmi_ipi_unlock();
  460. udelay(1);
  461. nmi_ipi_lock();
  462. if (delay_us) {
  463. delay_us--;
  464. if (!delay_us)
  465. break;
  466. }
  467. }
  468. if (!cpumask_empty(&nmi_ipi_pending_mask)) {
  469. /* Timeout waiting for CPUs to call smp_handle_nmi_ipi */
  470. ret = 0;
  471. cpumask_clear(&nmi_ipi_pending_mask);
  472. }
  473. nmi_ipi_function = NULL;
  474. nmi_ipi_busy = false;
  475. nmi_ipi_unlock_end(&flags);
  476. return ret;
  477. }
  478. int smp_send_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us)
  479. {
  480. return __smp_send_nmi_ipi(cpu, fn, delay_us, false);
  481. }
  482. int smp_send_safe_nmi_ipi(int cpu, void (*fn)(struct pt_regs *), u64 delay_us)
  483. {
  484. return __smp_send_nmi_ipi(cpu, fn, delay_us, true);
  485. }
  486. #endif /* CONFIG_NMI_IPI */
  487. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  488. void tick_broadcast(const struct cpumask *mask)
  489. {
  490. unsigned int cpu;
  491. for_each_cpu(cpu, mask)
  492. do_message_pass(cpu, PPC_MSG_TICK_BROADCAST);
  493. }
  494. #endif
  495. #ifdef CONFIG_DEBUGGER
  496. static void debugger_ipi_callback(struct pt_regs *regs)
  497. {
  498. debugger_ipi(regs);
  499. }
  500. void smp_send_debugger_break(void)
  501. {
  502. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, debugger_ipi_callback, 1000000);
  503. }
  504. #endif
  505. #ifdef CONFIG_CRASH_DUMP
  506. void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
  507. {
  508. int cpu;
  509. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, crash_ipi_callback, 1000000);
  510. if (kdump_in_progress() && crash_wake_offline) {
  511. for_each_present_cpu(cpu) {
  512. if (cpu_online(cpu))
  513. continue;
  514. /*
  515. * crash_ipi_callback will wait for
  516. * all cpus, including offline CPUs.
  517. * We don't care about nmi_ipi_function.
  518. * Offline cpus will jump straight into
  519. * crash_ipi_callback, we can skip the
  520. * entire NMI dance and waiting for
  521. * cpus to clear pending mask, etc.
  522. */
  523. do_smp_send_nmi_ipi(cpu, false);
  524. }
  525. }
  526. }
  527. #endif
  528. void crash_smp_send_stop(void)
  529. {
  530. static bool stopped = false;
  531. /*
  532. * In case of fadump, register data for all CPUs is captured by f/w
  533. * on ibm,os-term rtas call. Skip IPI callbacks to other CPUs before
  534. * this rtas call to avoid tricky post processing of those CPUs'
  535. * backtraces.
  536. */
  537. if (should_fadump_crash())
  538. return;
  539. if (stopped)
  540. return;
  541. stopped = true;
  542. #ifdef CONFIG_CRASH_DUMP
  543. if (kexec_crash_image) {
  544. crash_kexec_prepare();
  545. return;
  546. }
  547. #endif
  548. smp_send_stop();
  549. }
  550. #ifdef CONFIG_NMI_IPI
  551. static void nmi_stop_this_cpu(struct pt_regs *regs)
  552. {
  553. /*
  554. * IRQs are already hard disabled by the smp_handle_nmi_ipi.
  555. */
  556. set_cpu_online(smp_processor_id(), false);
  557. spin_begin();
  558. while (1)
  559. spin_cpu_relax();
  560. }
  561. void smp_send_stop(void)
  562. {
  563. smp_send_nmi_ipi(NMI_IPI_ALL_OTHERS, nmi_stop_this_cpu, 1000000);
  564. }
  565. #else /* CONFIG_NMI_IPI */
  566. static void stop_this_cpu(void *dummy)
  567. {
  568. hard_irq_disable();
  569. /*
  570. * Offlining CPUs in stop_this_cpu can result in scheduler warnings,
  571. * (see commit de6e5d38417e), but printk_safe_flush_on_panic() wants
  572. * to know other CPUs are offline before it breaks locks to flush
  573. * printk buffers, in case we panic()ed while holding the lock.
  574. */
  575. set_cpu_online(smp_processor_id(), false);
  576. spin_begin();
  577. while (1)
  578. spin_cpu_relax();
  579. }
  580. void smp_send_stop(void)
  581. {
  582. static bool stopped = false;
  583. /*
  584. * Prevent waiting on csd lock from a previous smp_send_stop.
  585. * This is racy, but in general callers try to do the right
  586. * thing and only fire off one smp_send_stop (e.g., see
  587. * kernel/panic.c)
  588. */
  589. if (stopped)
  590. return;
  591. stopped = true;
  592. smp_call_function(stop_this_cpu, NULL, 0);
  593. }
  594. #endif /* CONFIG_NMI_IPI */
  595. static struct task_struct *current_set[NR_CPUS];
  596. static void smp_store_cpu_info(int id)
  597. {
  598. per_cpu(cpu_pvr, id) = mfspr(SPRN_PVR);
  599. #ifdef CONFIG_PPC_E500
  600. per_cpu(next_tlbcam_idx, id)
  601. = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
  602. #endif
  603. }
  604. /*
  605. * Relationships between CPUs are maintained in a set of per-cpu cpumasks so
  606. * rather than just passing around the cpumask we pass around a function that
  607. * returns the that cpumask for the given CPU.
  608. */
  609. static void set_cpus_related(int i, int j, struct cpumask *(*get_cpumask)(int))
  610. {
  611. cpumask_set_cpu(i, get_cpumask(j));
  612. cpumask_set_cpu(j, get_cpumask(i));
  613. }
  614. #ifdef CONFIG_HOTPLUG_CPU
  615. static void set_cpus_unrelated(int i, int j,
  616. struct cpumask *(*get_cpumask)(int))
  617. {
  618. cpumask_clear_cpu(i, get_cpumask(j));
  619. cpumask_clear_cpu(j, get_cpumask(i));
  620. }
  621. #endif
  622. /*
  623. * Extends set_cpus_related. Instead of setting one CPU at a time in
  624. * dstmask, set srcmask at oneshot. dstmask should be super set of srcmask.
  625. */
  626. static void or_cpumasks_related(int i, int j, struct cpumask *(*srcmask)(int),
  627. struct cpumask *(*dstmask)(int))
  628. {
  629. struct cpumask *mask;
  630. int k;
  631. mask = srcmask(j);
  632. for_each_cpu(k, srcmask(i))
  633. cpumask_or(dstmask(k), dstmask(k), mask);
  634. if (i == j)
  635. return;
  636. mask = srcmask(i);
  637. for_each_cpu(k, srcmask(j))
  638. cpumask_or(dstmask(k), dstmask(k), mask);
  639. }
  640. /*
  641. * parse_thread_groups: Parses the "ibm,thread-groups" device tree
  642. * property for the CPU device node @dn and stores
  643. * the parsed output in the thread_groups_list
  644. * structure @tglp.
  645. *
  646. * @dn: The device node of the CPU device.
  647. * @tglp: Pointer to a thread group list structure into which the parsed
  648. * output of "ibm,thread-groups" is stored.
  649. *
  650. * ibm,thread-groups[0..N-1] array defines which group of threads in
  651. * the CPU-device node can be grouped together based on the property.
  652. *
  653. * This array can represent thread groupings for multiple properties.
  654. *
  655. * ibm,thread-groups[i + 0] tells us the property based on which the
  656. * threads are being grouped together. If this value is 1, it implies
  657. * that the threads in the same group share L1, translation cache. If
  658. * the value is 2, it implies that the threads in the same group share
  659. * the same L2 cache.
  660. *
  661. * ibm,thread-groups[i+1] tells us how many such thread groups exist for the
  662. * property ibm,thread-groups[i]
  663. *
  664. * ibm,thread-groups[i+2] tells us the number of threads in each such
  665. * group.
  666. * Suppose k = (ibm,thread-groups[i+1] * ibm,thread-groups[i+2]), then,
  667. *
  668. * ibm,thread-groups[i+3..i+k+2] (is the list of threads identified by
  669. * "ibm,ppc-interrupt-server#s" arranged as per their membership in
  670. * the grouping.
  671. *
  672. * Example:
  673. * If "ibm,thread-groups" = [1,2,4,8,10,12,14,9,11,13,15,2,2,4,8,10,12,14,9,11,13,15]
  674. * This can be decomposed up into two consecutive arrays:
  675. * a) [1,2,4,8,10,12,14,9,11,13,15]
  676. * b) [2,2,4,8,10,12,14,9,11,13,15]
  677. *
  678. * where in,
  679. *
  680. * a) provides information of Property "1" being shared by "2" groups,
  681. * each with "4" threads each. The "ibm,ppc-interrupt-server#s" of
  682. * the first group is {8,10,12,14} and the
  683. * "ibm,ppc-interrupt-server#s" of the second group is
  684. * {9,11,13,15}. Property "1" is indicative of the thread in the
  685. * group sharing L1 cache, translation cache and Instruction Data
  686. * flow.
  687. *
  688. * b) provides information of Property "2" being shared by "2" groups,
  689. * each group with "4" threads. The "ibm,ppc-interrupt-server#s" of
  690. * the first group is {8,10,12,14} and the
  691. * "ibm,ppc-interrupt-server#s" of the second group is
  692. * {9,11,13,15}. Property "2" indicates that the threads in each
  693. * group share the L2-cache.
  694. *
  695. * Returns 0 on success, -EINVAL if the property does not exist,
  696. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  697. * property data isn't large enough.
  698. */
  699. static int parse_thread_groups(struct device_node *dn,
  700. struct thread_groups_list *tglp)
  701. {
  702. unsigned int property_idx = 0;
  703. u32 *thread_group_array;
  704. size_t total_threads;
  705. int ret = 0, count;
  706. u32 *thread_list;
  707. int i = 0;
  708. count = of_property_count_u32_elems(dn, "ibm,thread-groups");
  709. thread_group_array = kcalloc(count, sizeof(u32), GFP_KERNEL);
  710. ret = of_property_read_u32_array(dn, "ibm,thread-groups",
  711. thread_group_array, count);
  712. if (ret)
  713. goto out_free;
  714. while (i < count && property_idx < MAX_THREAD_GROUP_PROPERTIES) {
  715. int j;
  716. struct thread_groups *tg = &tglp->property_tgs[property_idx++];
  717. tg->property = thread_group_array[i];
  718. tg->nr_groups = thread_group_array[i + 1];
  719. tg->threads_per_group = thread_group_array[i + 2];
  720. total_threads = tg->nr_groups * tg->threads_per_group;
  721. thread_list = &thread_group_array[i + 3];
  722. for (j = 0; j < total_threads; j++)
  723. tg->thread_list[j] = thread_list[j];
  724. i = i + 3 + total_threads;
  725. }
  726. tglp->nr_properties = property_idx;
  727. out_free:
  728. kfree(thread_group_array);
  729. return ret;
  730. }
  731. /*
  732. * get_cpu_thread_group_start : Searches the thread group in tg->thread_list
  733. * that @cpu belongs to.
  734. *
  735. * @cpu : The logical CPU whose thread group is being searched.
  736. * @tg : The thread-group structure of the CPU node which @cpu belongs
  737. * to.
  738. *
  739. * Returns the index to tg->thread_list that points to the start
  740. * of the thread_group that @cpu belongs to.
  741. *
  742. * Returns -1 if cpu doesn't belong to any of the groups pointed to by
  743. * tg->thread_list.
  744. */
  745. static int get_cpu_thread_group_start(int cpu, struct thread_groups *tg)
  746. {
  747. int hw_cpu_id = get_hard_smp_processor_id(cpu);
  748. int i, j;
  749. for (i = 0; i < tg->nr_groups; i++) {
  750. int group_start = i * tg->threads_per_group;
  751. for (j = 0; j < tg->threads_per_group; j++) {
  752. int idx = group_start + j;
  753. if (tg->thread_list[idx] == hw_cpu_id)
  754. return group_start;
  755. }
  756. }
  757. return -1;
  758. }
  759. static struct thread_groups *__init get_thread_groups(int cpu,
  760. int group_property,
  761. int *err)
  762. {
  763. struct device_node *dn = of_get_cpu_node(cpu, NULL);
  764. struct thread_groups_list *cpu_tgl = &tgl[cpu];
  765. struct thread_groups *tg = NULL;
  766. int i;
  767. *err = 0;
  768. if (!dn) {
  769. *err = -ENODATA;
  770. return NULL;
  771. }
  772. if (!cpu_tgl->nr_properties) {
  773. *err = parse_thread_groups(dn, cpu_tgl);
  774. if (*err)
  775. goto out;
  776. }
  777. for (i = 0; i < cpu_tgl->nr_properties; i++) {
  778. if (cpu_tgl->property_tgs[i].property == group_property) {
  779. tg = &cpu_tgl->property_tgs[i];
  780. break;
  781. }
  782. }
  783. if (!tg)
  784. *err = -EINVAL;
  785. out:
  786. of_node_put(dn);
  787. return tg;
  788. }
  789. static int __init update_mask_from_threadgroup(cpumask_var_t *mask, struct thread_groups *tg,
  790. int cpu, int cpu_group_start)
  791. {
  792. int first_thread = cpu_first_thread_sibling(cpu);
  793. int i;
  794. zalloc_cpumask_var_node(mask, GFP_KERNEL, cpu_to_node(cpu));
  795. for (i = first_thread; i < first_thread + threads_per_core; i++) {
  796. int i_group_start = get_cpu_thread_group_start(i, tg);
  797. if (unlikely(i_group_start == -1)) {
  798. WARN_ON_ONCE(1);
  799. return -ENODATA;
  800. }
  801. if (i_group_start == cpu_group_start)
  802. cpumask_set_cpu(i, *mask);
  803. }
  804. return 0;
  805. }
  806. static int __init init_thread_group_cache_map(int cpu, int cache_property)
  807. {
  808. int cpu_group_start = -1, err = 0;
  809. struct thread_groups *tg = NULL;
  810. cpumask_var_t *mask = NULL;
  811. if (cache_property != THREAD_GROUP_SHARE_L1 &&
  812. cache_property != THREAD_GROUP_SHARE_L2_L3)
  813. return -EINVAL;
  814. tg = get_thread_groups(cpu, cache_property, &err);
  815. if (!tg)
  816. return err;
  817. cpu_group_start = get_cpu_thread_group_start(cpu, tg);
  818. if (unlikely(cpu_group_start == -1)) {
  819. WARN_ON_ONCE(1);
  820. return -ENODATA;
  821. }
  822. if (cache_property == THREAD_GROUP_SHARE_L1) {
  823. mask = &per_cpu(thread_group_l1_cache_map, cpu);
  824. update_mask_from_threadgroup(mask, tg, cpu, cpu_group_start);
  825. }
  826. else if (cache_property == THREAD_GROUP_SHARE_L2_L3) {
  827. mask = &per_cpu(thread_group_l2_cache_map, cpu);
  828. update_mask_from_threadgroup(mask, tg, cpu, cpu_group_start);
  829. mask = &per_cpu(thread_group_l3_cache_map, cpu);
  830. update_mask_from_threadgroup(mask, tg, cpu, cpu_group_start);
  831. }
  832. return 0;
  833. }
  834. static bool shared_caches __ro_after_init;
  835. #ifdef CONFIG_SCHED_SMT
  836. /* cpumask of CPUs with asymmetric SMT dependency */
  837. static int powerpc_smt_flags(void)
  838. {
  839. int flags = SD_SHARE_CPUCAPACITY | SD_SHARE_LLC;
  840. if (cpu_has_feature(CPU_FTR_ASYM_SMT)) {
  841. printk_once(KERN_INFO "Enabling Asymmetric SMT scheduling\n");
  842. flags |= SD_ASYM_PACKING;
  843. }
  844. return flags;
  845. }
  846. #endif
  847. /*
  848. * On shared processor LPARs scheduled on a big core (which has two or more
  849. * independent thread groups per core), prefer lower numbered CPUs, so
  850. * that workload consolidates to lesser number of cores.
  851. */
  852. static __ro_after_init DEFINE_STATIC_KEY_FALSE(splpar_asym_pack);
  853. /*
  854. * P9 has a slightly odd architecture where pairs of cores share an L2 cache.
  855. * This topology makes it *much* cheaper to migrate tasks between adjacent cores
  856. * since the migrated task remains cache hot. We want to take advantage of this
  857. * at the scheduler level so an extra topology level is required.
  858. */
  859. static int powerpc_shared_cache_flags(void)
  860. {
  861. if (static_branch_unlikely(&splpar_asym_pack))
  862. return SD_SHARE_LLC | SD_ASYM_PACKING;
  863. return SD_SHARE_LLC;
  864. }
  865. static int powerpc_shared_proc_flags(void)
  866. {
  867. if (static_branch_unlikely(&splpar_asym_pack))
  868. return SD_ASYM_PACKING;
  869. return 0;
  870. }
  871. /*
  872. * We can't just pass cpu_l2_cache_mask() directly because
  873. * returns a non-const pointer and the compiler barfs on that.
  874. */
  875. static const struct cpumask *shared_cache_mask(int cpu)
  876. {
  877. return per_cpu(cpu_l2_cache_map, cpu);
  878. }
  879. #ifdef CONFIG_SCHED_SMT
  880. static const struct cpumask *smallcore_smt_mask(int cpu)
  881. {
  882. return cpu_smallcore_mask(cpu);
  883. }
  884. #endif
  885. static struct cpumask *cpu_coregroup_mask(int cpu)
  886. {
  887. return per_cpu(cpu_coregroup_map, cpu);
  888. }
  889. static bool has_coregroup_support(void)
  890. {
  891. /* Coregroup identification not available on shared systems */
  892. if (is_shared_processor())
  893. return 0;
  894. return coregroup_enabled;
  895. }
  896. static const struct cpumask *cpu_mc_mask(int cpu)
  897. {
  898. return cpu_coregroup_mask(cpu);
  899. }
  900. static int __init init_big_cores(void)
  901. {
  902. int cpu;
  903. for_each_possible_cpu(cpu) {
  904. int err = init_thread_group_cache_map(cpu, THREAD_GROUP_SHARE_L1);
  905. if (err)
  906. return err;
  907. zalloc_cpumask_var_node(&per_cpu(cpu_smallcore_map, cpu),
  908. GFP_KERNEL,
  909. cpu_to_node(cpu));
  910. }
  911. has_big_cores = true;
  912. for_each_possible_cpu(cpu) {
  913. int err = init_thread_group_cache_map(cpu, THREAD_GROUP_SHARE_L2_L3);
  914. if (err)
  915. return err;
  916. }
  917. thread_group_shares_l2 = true;
  918. thread_group_shares_l3 = true;
  919. pr_debug("L2/L3 cache only shared by the threads in the small core\n");
  920. return 0;
  921. }
  922. void __init smp_prepare_cpus(unsigned int max_cpus)
  923. {
  924. unsigned int cpu, num_threads;
  925. DBG("smp_prepare_cpus\n");
  926. /*
  927. * setup_cpu may need to be called on the boot cpu. We haven't
  928. * spun any cpus up but lets be paranoid.
  929. */
  930. BUG_ON(boot_cpuid != smp_processor_id());
  931. /* Fixup boot cpu */
  932. smp_store_cpu_info(boot_cpuid);
  933. cpu_callin_map[boot_cpuid] = 1;
  934. for_each_possible_cpu(cpu) {
  935. zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu),
  936. GFP_KERNEL, cpu_to_node(cpu));
  937. zalloc_cpumask_var_node(&per_cpu(cpu_l2_cache_map, cpu),
  938. GFP_KERNEL, cpu_to_node(cpu));
  939. zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu),
  940. GFP_KERNEL, cpu_to_node(cpu));
  941. if (has_coregroup_support())
  942. zalloc_cpumask_var_node(&per_cpu(cpu_coregroup_map, cpu),
  943. GFP_KERNEL, cpu_to_node(cpu));
  944. #ifdef CONFIG_NUMA
  945. /*
  946. * numa_node_id() works after this.
  947. */
  948. if (cpu_present(cpu)) {
  949. set_cpu_numa_node(cpu, numa_cpu_lookup_table[cpu]);
  950. set_cpu_numa_mem(cpu,
  951. local_memory_node(numa_cpu_lookup_table[cpu]));
  952. }
  953. #endif
  954. }
  955. /* Init the cpumasks so the boot CPU is related to itself */
  956. cpumask_set_cpu(boot_cpuid, cpu_sibling_mask(boot_cpuid));
  957. cpumask_set_cpu(boot_cpuid, cpu_l2_cache_mask(boot_cpuid));
  958. cpumask_set_cpu(boot_cpuid, cpu_core_mask(boot_cpuid));
  959. if (has_coregroup_support())
  960. cpumask_set_cpu(boot_cpuid, cpu_coregroup_mask(boot_cpuid));
  961. init_big_cores();
  962. if (has_big_cores) {
  963. cpumask_set_cpu(boot_cpuid,
  964. cpu_smallcore_mask(boot_cpuid));
  965. }
  966. if (cpu_to_chip_id(boot_cpuid) != -1) {
  967. int idx = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
  968. /*
  969. * All threads of a core will all belong to the same core,
  970. * chip_id_lookup_table will have one entry per core.
  971. * Assumption: if boot_cpuid doesn't have a chip-id, then no
  972. * other CPUs, will also not have chip-id.
  973. */
  974. chip_id_lookup_table = kcalloc(idx, sizeof(int), GFP_KERNEL);
  975. if (chip_id_lookup_table)
  976. memset(chip_id_lookup_table, -1, sizeof(int) * idx);
  977. }
  978. if (smp_ops && smp_ops->probe)
  979. smp_ops->probe();
  980. // Initalise the generic SMT topology support
  981. num_threads = 1;
  982. if (smt_enabled_at_boot)
  983. num_threads = smt_enabled_at_boot;
  984. cpu_smt_set_num_threads(num_threads, threads_per_core);
  985. }
  986. void __init smp_prepare_boot_cpu(void)
  987. {
  988. BUG_ON(smp_processor_id() != boot_cpuid);
  989. #ifdef CONFIG_PPC64
  990. paca_ptrs[boot_cpuid]->__current = current;
  991. #endif
  992. set_numa_node(numa_cpu_lookup_table[boot_cpuid]);
  993. current_set[boot_cpuid] = current;
  994. }
  995. #ifdef CONFIG_HOTPLUG_CPU
  996. int generic_cpu_disable(void)
  997. {
  998. unsigned int cpu = smp_processor_id();
  999. if (cpu == boot_cpuid)
  1000. return -EBUSY;
  1001. set_cpu_online(cpu, false);
  1002. #ifdef CONFIG_PPC64
  1003. vdso_data->processorCount--;
  1004. #endif
  1005. /* Update affinity of all IRQs previously aimed at this CPU */
  1006. irq_migrate_all_off_this_cpu();
  1007. /*
  1008. * Depending on the details of the interrupt controller, it's possible
  1009. * that one of the interrupts we just migrated away from this CPU is
  1010. * actually already pending on this CPU. If we leave it in that state
  1011. * the interrupt will never be EOI'ed, and will never fire again. So
  1012. * temporarily enable interrupts here, to allow any pending interrupt to
  1013. * be received (and EOI'ed), before we take this CPU offline.
  1014. */
  1015. local_irq_enable();
  1016. mdelay(1);
  1017. local_irq_disable();
  1018. return 0;
  1019. }
  1020. void generic_cpu_die(unsigned int cpu)
  1021. {
  1022. int i;
  1023. for (i = 0; i < 100; i++) {
  1024. smp_rmb();
  1025. if (is_cpu_dead(cpu))
  1026. return;
  1027. msleep(100);
  1028. }
  1029. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  1030. }
  1031. void generic_set_cpu_dead(unsigned int cpu)
  1032. {
  1033. per_cpu(cpu_state, cpu) = CPU_DEAD;
  1034. }
  1035. /*
  1036. * The cpu_state should be set to CPU_UP_PREPARE in kick_cpu(), otherwise
  1037. * the cpu_state is always CPU_DEAD after calling generic_set_cpu_dead(),
  1038. * which makes the delay in generic_cpu_die() not happen.
  1039. */
  1040. void generic_set_cpu_up(unsigned int cpu)
  1041. {
  1042. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  1043. }
  1044. int generic_check_cpu_restart(unsigned int cpu)
  1045. {
  1046. return per_cpu(cpu_state, cpu) == CPU_UP_PREPARE;
  1047. }
  1048. int is_cpu_dead(unsigned int cpu)
  1049. {
  1050. return per_cpu(cpu_state, cpu) == CPU_DEAD;
  1051. }
  1052. static bool secondaries_inhibited(void)
  1053. {
  1054. return kvm_hv_mode_active();
  1055. }
  1056. #else /* HOTPLUG_CPU */
  1057. #define secondaries_inhibited() 0
  1058. #endif
  1059. static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
  1060. {
  1061. #ifdef CONFIG_PPC64
  1062. paca_ptrs[cpu]->__current = idle;
  1063. paca_ptrs[cpu]->kstack = (unsigned long)task_stack_page(idle) +
  1064. THREAD_SIZE - STACK_FRAME_MIN_SIZE;
  1065. #endif
  1066. task_thread_info(idle)->cpu = cpu;
  1067. secondary_current = current_set[cpu] = idle;
  1068. }
  1069. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  1070. {
  1071. const unsigned long boot_spin_ms = 5 * MSEC_PER_SEC;
  1072. const bool booting = system_state < SYSTEM_RUNNING;
  1073. const unsigned long hp_spin_ms = 1;
  1074. unsigned long deadline;
  1075. int rc;
  1076. const unsigned long spin_wait_ms = booting ? boot_spin_ms : hp_spin_ms;
  1077. /*
  1078. * Don't allow secondary threads to come online if inhibited
  1079. */
  1080. if (threads_per_core > 1 && secondaries_inhibited() &&
  1081. cpu_thread_in_subcore(cpu))
  1082. return -EBUSY;
  1083. if (smp_ops == NULL ||
  1084. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  1085. return -EINVAL;
  1086. cpu_idle_thread_init(cpu, tidle);
  1087. /*
  1088. * The platform might need to allocate resources prior to bringing
  1089. * up the CPU
  1090. */
  1091. if (smp_ops->prepare_cpu) {
  1092. rc = smp_ops->prepare_cpu(cpu);
  1093. if (rc)
  1094. return rc;
  1095. }
  1096. /* Make sure callin-map entry is 0 (can be leftover a CPU
  1097. * hotplug
  1098. */
  1099. cpu_callin_map[cpu] = 0;
  1100. /* The information for processor bringup must
  1101. * be written out to main store before we release
  1102. * the processor.
  1103. */
  1104. smp_mb();
  1105. /* wake up cpus */
  1106. DBG("smp: kicking cpu %d\n", cpu);
  1107. rc = smp_ops->kick_cpu(cpu);
  1108. if (rc) {
  1109. pr_err("smp: failed starting cpu %d (rc %d)\n", cpu, rc);
  1110. return rc;
  1111. }
  1112. /*
  1113. * At boot time, simply spin on the callin word until the
  1114. * deadline passes.
  1115. *
  1116. * At run time, spin for an optimistic amount of time to avoid
  1117. * sleeping in the common case.
  1118. */
  1119. deadline = jiffies + msecs_to_jiffies(spin_wait_ms);
  1120. spin_until_cond(cpu_callin_map[cpu] || time_is_before_jiffies(deadline));
  1121. if (!cpu_callin_map[cpu] && system_state >= SYSTEM_RUNNING) {
  1122. const unsigned long sleep_interval_us = 10 * USEC_PER_MSEC;
  1123. const unsigned long sleep_wait_ms = 100 * MSEC_PER_SEC;
  1124. deadline = jiffies + msecs_to_jiffies(sleep_wait_ms);
  1125. while (!cpu_callin_map[cpu] && time_is_after_jiffies(deadline))
  1126. fsleep(sleep_interval_us);
  1127. }
  1128. if (!cpu_callin_map[cpu]) {
  1129. printk(KERN_ERR "Processor %u is stuck.\n", cpu);
  1130. return -ENOENT;
  1131. }
  1132. DBG("Processor %u found.\n", cpu);
  1133. if (smp_ops->give_timebase)
  1134. smp_ops->give_timebase();
  1135. /* Wait until cpu puts itself in the online & active maps */
  1136. spin_until_cond(cpu_online(cpu));
  1137. return 0;
  1138. }
  1139. /* Return the value of the reg property corresponding to the given
  1140. * logical cpu.
  1141. */
  1142. int cpu_to_core_id(int cpu)
  1143. {
  1144. struct device_node *np;
  1145. int id = -1;
  1146. np = of_get_cpu_node(cpu, NULL);
  1147. if (!np)
  1148. goto out;
  1149. id = of_get_cpu_hwid(np, 0);
  1150. out:
  1151. of_node_put(np);
  1152. return id;
  1153. }
  1154. EXPORT_SYMBOL_GPL(cpu_to_core_id);
  1155. /* Helper routines for cpu to core mapping */
  1156. int cpu_core_index_of_thread(int cpu)
  1157. {
  1158. return cpu >> threads_shift;
  1159. }
  1160. EXPORT_SYMBOL_GPL(cpu_core_index_of_thread);
  1161. int cpu_first_thread_of_core(int core)
  1162. {
  1163. return core << threads_shift;
  1164. }
  1165. EXPORT_SYMBOL_GPL(cpu_first_thread_of_core);
  1166. /* Must be called when no change can occur to cpu_present_mask,
  1167. * i.e. during cpu online or offline.
  1168. */
  1169. static struct device_node *cpu_to_l2cache(int cpu)
  1170. {
  1171. struct device_node *np;
  1172. struct device_node *cache;
  1173. if (!cpu_present(cpu))
  1174. return NULL;
  1175. np = of_get_cpu_node(cpu, NULL);
  1176. if (np == NULL)
  1177. return NULL;
  1178. cache = of_find_next_cache_node(np);
  1179. of_node_put(np);
  1180. return cache;
  1181. }
  1182. static bool update_mask_by_l2(int cpu, cpumask_var_t *mask)
  1183. {
  1184. struct cpumask *(*submask_fn)(int) = cpu_sibling_mask;
  1185. struct device_node *l2_cache, *np;
  1186. int i;
  1187. if (has_big_cores)
  1188. submask_fn = cpu_smallcore_mask;
  1189. /*
  1190. * If the threads in a thread-group share L2 cache, then the
  1191. * L2-mask can be obtained from thread_group_l2_cache_map.
  1192. */
  1193. if (thread_group_shares_l2) {
  1194. cpumask_set_cpu(cpu, cpu_l2_cache_mask(cpu));
  1195. for_each_cpu(i, per_cpu(thread_group_l2_cache_map, cpu)) {
  1196. if (cpu_online(i))
  1197. set_cpus_related(i, cpu, cpu_l2_cache_mask);
  1198. }
  1199. /* Verify that L1-cache siblings are a subset of L2 cache-siblings */
  1200. if (!cpumask_equal(submask_fn(cpu), cpu_l2_cache_mask(cpu)) &&
  1201. !cpumask_subset(submask_fn(cpu), cpu_l2_cache_mask(cpu))) {
  1202. pr_warn_once("CPU %d : Inconsistent L1 and L2 cache siblings\n",
  1203. cpu);
  1204. }
  1205. return true;
  1206. }
  1207. l2_cache = cpu_to_l2cache(cpu);
  1208. if (!l2_cache || !*mask) {
  1209. /* Assume only core siblings share cache with this CPU */
  1210. for_each_cpu(i, cpu_sibling_mask(cpu))
  1211. set_cpus_related(cpu, i, cpu_l2_cache_mask);
  1212. return false;
  1213. }
  1214. cpumask_and(*mask, cpu_online_mask, cpu_cpu_mask(cpu));
  1215. /* Update l2-cache mask with all the CPUs that are part of submask */
  1216. or_cpumasks_related(cpu, cpu, submask_fn, cpu_l2_cache_mask);
  1217. /* Skip all CPUs already part of current CPU l2-cache mask */
  1218. cpumask_andnot(*mask, *mask, cpu_l2_cache_mask(cpu));
  1219. for_each_cpu(i, *mask) {
  1220. /*
  1221. * when updating the marks the current CPU has not been marked
  1222. * online, but we need to update the cache masks
  1223. */
  1224. np = cpu_to_l2cache(i);
  1225. /* Skip all CPUs already part of current CPU l2-cache */
  1226. if (np == l2_cache) {
  1227. or_cpumasks_related(cpu, i, submask_fn, cpu_l2_cache_mask);
  1228. cpumask_andnot(*mask, *mask, submask_fn(i));
  1229. } else {
  1230. cpumask_andnot(*mask, *mask, cpu_l2_cache_mask(i));
  1231. }
  1232. of_node_put(np);
  1233. }
  1234. of_node_put(l2_cache);
  1235. return true;
  1236. }
  1237. #ifdef CONFIG_HOTPLUG_CPU
  1238. static void remove_cpu_from_masks(int cpu)
  1239. {
  1240. struct cpumask *(*mask_fn)(int) = cpu_sibling_mask;
  1241. int i;
  1242. unmap_cpu_from_node(cpu);
  1243. if (shared_caches)
  1244. mask_fn = cpu_l2_cache_mask;
  1245. for_each_cpu(i, mask_fn(cpu)) {
  1246. set_cpus_unrelated(cpu, i, cpu_l2_cache_mask);
  1247. set_cpus_unrelated(cpu, i, cpu_sibling_mask);
  1248. if (has_big_cores)
  1249. set_cpus_unrelated(cpu, i, cpu_smallcore_mask);
  1250. }
  1251. for_each_cpu(i, cpu_core_mask(cpu))
  1252. set_cpus_unrelated(cpu, i, cpu_core_mask);
  1253. if (has_coregroup_support()) {
  1254. for_each_cpu(i, cpu_coregroup_mask(cpu))
  1255. set_cpus_unrelated(cpu, i, cpu_coregroup_mask);
  1256. }
  1257. }
  1258. #endif
  1259. static inline void add_cpu_to_smallcore_masks(int cpu)
  1260. {
  1261. int i;
  1262. if (!has_big_cores)
  1263. return;
  1264. cpumask_set_cpu(cpu, cpu_smallcore_mask(cpu));
  1265. for_each_cpu(i, per_cpu(thread_group_l1_cache_map, cpu)) {
  1266. if (cpu_online(i))
  1267. set_cpus_related(i, cpu, cpu_smallcore_mask);
  1268. }
  1269. }
  1270. static void update_coregroup_mask(int cpu, cpumask_var_t *mask)
  1271. {
  1272. struct cpumask *(*submask_fn)(int) = cpu_sibling_mask;
  1273. int coregroup_id = cpu_to_coregroup_id(cpu);
  1274. int i;
  1275. if (shared_caches)
  1276. submask_fn = cpu_l2_cache_mask;
  1277. if (!*mask) {
  1278. /* Assume only siblings are part of this CPU's coregroup */
  1279. for_each_cpu(i, submask_fn(cpu))
  1280. set_cpus_related(cpu, i, cpu_coregroup_mask);
  1281. return;
  1282. }
  1283. cpumask_and(*mask, cpu_online_mask, cpu_cpu_mask(cpu));
  1284. /* Update coregroup mask with all the CPUs that are part of submask */
  1285. or_cpumasks_related(cpu, cpu, submask_fn, cpu_coregroup_mask);
  1286. /* Skip all CPUs already part of coregroup mask */
  1287. cpumask_andnot(*mask, *mask, cpu_coregroup_mask(cpu));
  1288. for_each_cpu(i, *mask) {
  1289. /* Skip all CPUs not part of this coregroup */
  1290. if (coregroup_id == cpu_to_coregroup_id(i)) {
  1291. or_cpumasks_related(cpu, i, submask_fn, cpu_coregroup_mask);
  1292. cpumask_andnot(*mask, *mask, submask_fn(i));
  1293. } else {
  1294. cpumask_andnot(*mask, *mask, cpu_coregroup_mask(i));
  1295. }
  1296. }
  1297. }
  1298. static void add_cpu_to_masks(int cpu)
  1299. {
  1300. struct cpumask *(*submask_fn)(int) = cpu_sibling_mask;
  1301. int first_thread = cpu_first_thread_sibling(cpu);
  1302. cpumask_var_t mask;
  1303. int chip_id = -1;
  1304. bool ret;
  1305. int i;
  1306. /*
  1307. * This CPU will not be in the online mask yet so we need to manually
  1308. * add it to its own thread sibling mask.
  1309. */
  1310. map_cpu_to_node(cpu, cpu_to_node(cpu));
  1311. cpumask_set_cpu(cpu, cpu_sibling_mask(cpu));
  1312. cpumask_set_cpu(cpu, cpu_core_mask(cpu));
  1313. for (i = first_thread; i < first_thread + threads_per_core; i++)
  1314. if (cpu_online(i))
  1315. set_cpus_related(i, cpu, cpu_sibling_mask);
  1316. add_cpu_to_smallcore_masks(cpu);
  1317. /* In CPU-hotplug path, hence use GFP_ATOMIC */
  1318. ret = alloc_cpumask_var_node(&mask, GFP_ATOMIC, cpu_to_node(cpu));
  1319. update_mask_by_l2(cpu, &mask);
  1320. if (has_coregroup_support())
  1321. update_coregroup_mask(cpu, &mask);
  1322. if (chip_id_lookup_table && ret)
  1323. chip_id = cpu_to_chip_id(cpu);
  1324. if (shared_caches)
  1325. submask_fn = cpu_l2_cache_mask;
  1326. /* Update core_mask with all the CPUs that are part of submask */
  1327. or_cpumasks_related(cpu, cpu, submask_fn, cpu_core_mask);
  1328. /* Skip all CPUs already part of current CPU core mask */
  1329. cpumask_andnot(mask, cpu_online_mask, cpu_core_mask(cpu));
  1330. /* If chip_id is -1; limit the cpu_core_mask to within PKG */
  1331. if (chip_id == -1)
  1332. cpumask_and(mask, mask, cpu_cpu_mask(cpu));
  1333. for_each_cpu(i, mask) {
  1334. if (chip_id == cpu_to_chip_id(i)) {
  1335. or_cpumasks_related(cpu, i, submask_fn, cpu_core_mask);
  1336. cpumask_andnot(mask, mask, submask_fn(i));
  1337. } else {
  1338. cpumask_andnot(mask, mask, cpu_core_mask(i));
  1339. }
  1340. }
  1341. free_cpumask_var(mask);
  1342. }
  1343. /* Activate a secondary processor. */
  1344. __no_stack_protector
  1345. void start_secondary(void *unused)
  1346. {
  1347. unsigned int cpu = raw_smp_processor_id();
  1348. /* PPC64 calls setup_kup() in early_setup_secondary() */
  1349. if (IS_ENABLED(CONFIG_PPC32))
  1350. setup_kup();
  1351. mmgrab_lazy_tlb(&init_mm);
  1352. current->active_mm = &init_mm;
  1353. VM_WARN_ON(cpumask_test_cpu(smp_processor_id(), mm_cpumask(&init_mm)));
  1354. cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
  1355. inc_mm_active_cpus(&init_mm);
  1356. smp_store_cpu_info(cpu);
  1357. set_dec(tb_ticks_per_jiffy);
  1358. rcutree_report_cpu_starting(cpu);
  1359. cpu_callin_map[cpu] = 1;
  1360. if (smp_ops->setup_cpu)
  1361. smp_ops->setup_cpu(cpu);
  1362. if (smp_ops->take_timebase)
  1363. smp_ops->take_timebase();
  1364. secondary_cpu_time_init();
  1365. #ifdef CONFIG_PPC64
  1366. if (system_state == SYSTEM_RUNNING)
  1367. vdso_data->processorCount++;
  1368. vdso_getcpu_init();
  1369. #endif
  1370. set_numa_node(numa_cpu_lookup_table[cpu]);
  1371. set_numa_mem(local_memory_node(numa_cpu_lookup_table[cpu]));
  1372. /* Update topology CPU masks */
  1373. add_cpu_to_masks(cpu);
  1374. /*
  1375. * Check for any shared caches. Note that this must be done on a
  1376. * per-core basis because one core in the pair might be disabled.
  1377. */
  1378. if (!shared_caches) {
  1379. struct cpumask *(*sibling_mask)(int) = cpu_sibling_mask;
  1380. struct cpumask *mask = cpu_l2_cache_mask(cpu);
  1381. if (has_big_cores)
  1382. sibling_mask = cpu_smallcore_mask;
  1383. if (cpumask_weight(mask) > cpumask_weight(sibling_mask(cpu)))
  1384. shared_caches = true;
  1385. }
  1386. smp_wmb();
  1387. notify_cpu_starting(cpu);
  1388. set_cpu_online(cpu, true);
  1389. boot_init_stack_canary();
  1390. local_irq_enable();
  1391. /* We can enable ftrace for secondary cpus now */
  1392. this_cpu_enable_ftrace();
  1393. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  1394. BUG();
  1395. }
  1396. static struct sched_domain_topology_level powerpc_topology[6];
  1397. static void __init build_sched_topology(void)
  1398. {
  1399. int i = 0;
  1400. if (is_shared_processor() && has_big_cores)
  1401. static_branch_enable(&splpar_asym_pack);
  1402. #ifdef CONFIG_SCHED_SMT
  1403. if (has_big_cores) {
  1404. pr_info("Big cores detected but using small core scheduling\n");
  1405. powerpc_topology[i++] = (struct sched_domain_topology_level){
  1406. smallcore_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT)
  1407. };
  1408. } else {
  1409. powerpc_topology[i++] = (struct sched_domain_topology_level){
  1410. cpu_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT)
  1411. };
  1412. }
  1413. #endif
  1414. if (shared_caches) {
  1415. powerpc_topology[i++] = (struct sched_domain_topology_level){
  1416. shared_cache_mask, powerpc_shared_cache_flags, SD_INIT_NAME(CACHE)
  1417. };
  1418. }
  1419. if (has_coregroup_support()) {
  1420. powerpc_topology[i++] = (struct sched_domain_topology_level){
  1421. cpu_mc_mask, powerpc_shared_proc_flags, SD_INIT_NAME(MC)
  1422. };
  1423. }
  1424. powerpc_topology[i++] = (struct sched_domain_topology_level){
  1425. cpu_cpu_mask, powerpc_shared_proc_flags, SD_INIT_NAME(PKG)
  1426. };
  1427. /* There must be one trailing NULL entry left. */
  1428. BUG_ON(i >= ARRAY_SIZE(powerpc_topology) - 1);
  1429. set_sched_topology(powerpc_topology);
  1430. }
  1431. void __init smp_cpus_done(unsigned int max_cpus)
  1432. {
  1433. /*
  1434. * We are running pinned to the boot CPU, see rest_init().
  1435. */
  1436. if (smp_ops && smp_ops->setup_cpu)
  1437. smp_ops->setup_cpu(boot_cpuid);
  1438. if (smp_ops && smp_ops->bringup_done)
  1439. smp_ops->bringup_done();
  1440. dump_numa_cpu_topology();
  1441. build_sched_topology();
  1442. }
  1443. /*
  1444. * For asym packing, by default lower numbered CPU has higher priority.
  1445. * On shared processors, pack to lower numbered core. However avoid moving
  1446. * between thread_groups within the same core.
  1447. */
  1448. int arch_asym_cpu_priority(int cpu)
  1449. {
  1450. if (static_branch_unlikely(&splpar_asym_pack))
  1451. return -cpu / threads_per_core;
  1452. return -cpu;
  1453. }
  1454. #ifdef CONFIG_HOTPLUG_CPU
  1455. int __cpu_disable(void)
  1456. {
  1457. int cpu = smp_processor_id();
  1458. int err;
  1459. if (!smp_ops->cpu_disable)
  1460. return -ENOSYS;
  1461. this_cpu_disable_ftrace();
  1462. err = smp_ops->cpu_disable();
  1463. if (err)
  1464. return err;
  1465. /* Update sibling maps */
  1466. remove_cpu_from_masks(cpu);
  1467. return 0;
  1468. }
  1469. void __cpu_die(unsigned int cpu)
  1470. {
  1471. /*
  1472. * This could perhaps be a generic call in idlea_task_dead(), but
  1473. * that requires testing from all archs, so first put it here to
  1474. */
  1475. VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(&init_mm)));
  1476. dec_mm_active_cpus(&init_mm);
  1477. cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
  1478. if (smp_ops->cpu_die)
  1479. smp_ops->cpu_die(cpu);
  1480. }
  1481. void __noreturn arch_cpu_idle_dead(void)
  1482. {
  1483. /*
  1484. * Disable on the down path. This will be re-enabled by
  1485. * start_secondary() via start_secondary_resume() below
  1486. */
  1487. this_cpu_disable_ftrace();
  1488. if (smp_ops->cpu_offline_self)
  1489. smp_ops->cpu_offline_self();
  1490. /* If we return, we re-enter start_secondary */
  1491. start_secondary_resume();
  1492. }
  1493. #endif