smp.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SMP initialisation and IPI support
  4. * Based on arch/arm/kernel/smp.c
  5. *
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/arm_sdei.h>
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/sched/mm.h>
  14. #include <linux/sched/hotplug.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/cache.h>
  18. #include <linux/profile.h>
  19. #include <linux/errno.h>
  20. #include <linux/mm.h>
  21. #include <linux/err.h>
  22. #include <linux/cpu.h>
  23. #include <linux/smp.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/irq.h>
  26. #include <linux/irqchip/arm-gic-v3.h>
  27. #include <linux/percpu.h>
  28. #include <linux/clockchips.h>
  29. #include <linux/completion.h>
  30. #include <linux/of.h>
  31. #include <linux/irq_work.h>
  32. #include <linux/kernel_stat.h>
  33. #include <linux/kexec.h>
  34. #include <linux/kvm_host.h>
  35. #include <asm/alternative.h>
  36. #include <asm/atomic.h>
  37. #include <asm/cacheflush.h>
  38. #include <asm/cpu.h>
  39. #include <asm/cputype.h>
  40. #include <asm/cpu_ops.h>
  41. #include <asm/daifflags.h>
  42. #include <asm/kvm_mmu.h>
  43. #include <asm/mmu_context.h>
  44. #include <asm/numa.h>
  45. #include <asm/processor.h>
  46. #include <asm/smp_plat.h>
  47. #include <asm/sections.h>
  48. #include <asm/tlbflush.h>
  49. #include <asm/ptrace.h>
  50. #include <asm/virt.h>
  51. #define CREATE_TRACE_POINTS
  52. #include <trace/events/ipi.h>
  53. #undef CREATE_TRACE_POINTS
  54. #include <trace/hooks/debug.h>
  55. DEFINE_PER_CPU_READ_MOSTLY(int, cpu_number);
  56. EXPORT_PER_CPU_SYMBOL(cpu_number);
  57. EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_raise);
  58. EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_entry);
  59. EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_exit);
  60. /*
  61. * as from 2.5, kernels no longer have an init_tasks structure
  62. * so we need some other way of telling a new secondary core
  63. * where to place its SVC stack
  64. */
  65. struct secondary_data secondary_data;
  66. /* Number of CPUs which aren't online, but looping in kernel text. */
  67. static int cpus_stuck_in_kernel;
  68. enum ipi_msg_type {
  69. IPI_RESCHEDULE,
  70. IPI_CALL_FUNC,
  71. IPI_CPU_STOP,
  72. IPI_CPU_CRASH_STOP,
  73. IPI_TIMER,
  74. IPI_IRQ_WORK,
  75. IPI_WAKEUP,
  76. NR_IPI
  77. };
  78. static int ipi_irq_base __read_mostly;
  79. static int nr_ipi __read_mostly = NR_IPI;
  80. static struct irq_desc *ipi_desc[NR_IPI] __read_mostly;
  81. static void ipi_setup(int cpu);
  82. #ifdef CONFIG_HOTPLUG_CPU
  83. static void ipi_teardown(int cpu);
  84. static int op_cpu_kill(unsigned int cpu);
  85. #else
  86. static inline int op_cpu_kill(unsigned int cpu)
  87. {
  88. return -ENOSYS;
  89. }
  90. #endif
  91. /*
  92. * Boot a secondary CPU, and assign it the specified idle task.
  93. * This also gives us the initial stack to use for this CPU.
  94. */
  95. static int boot_secondary(unsigned int cpu, struct task_struct *idle)
  96. {
  97. const struct cpu_operations *ops = get_cpu_ops(cpu);
  98. if (ops->cpu_boot)
  99. return ops->cpu_boot(cpu);
  100. return -EOPNOTSUPP;
  101. }
  102. static DECLARE_COMPLETION(cpu_running);
  103. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  104. {
  105. int ret;
  106. long status;
  107. /*
  108. * We need to tell the secondary core where to find its stack and the
  109. * page tables.
  110. */
  111. secondary_data.task = idle;
  112. update_cpu_boot_status(CPU_MMU_OFF);
  113. /* Now bring the CPU into our world */
  114. ret = boot_secondary(cpu, idle);
  115. if (ret) {
  116. pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
  117. return ret;
  118. }
  119. /*
  120. * CPU was successfully started, wait for it to come online or
  121. * time out.
  122. */
  123. wait_for_completion_timeout(&cpu_running,
  124. msecs_to_jiffies(5000));
  125. if (cpu_online(cpu))
  126. return 0;
  127. pr_crit("CPU%u: failed to come online\n", cpu);
  128. secondary_data.task = NULL;
  129. status = READ_ONCE(secondary_data.status);
  130. if (status == CPU_MMU_OFF)
  131. status = READ_ONCE(__early_cpu_boot_status);
  132. switch (status & CPU_BOOT_STATUS_MASK) {
  133. default:
  134. pr_err("CPU%u: failed in unknown state : 0x%lx\n",
  135. cpu, status);
  136. cpus_stuck_in_kernel++;
  137. break;
  138. case CPU_KILL_ME:
  139. if (!op_cpu_kill(cpu)) {
  140. pr_crit("CPU%u: died during early boot\n", cpu);
  141. break;
  142. }
  143. pr_crit("CPU%u: may not have shut down cleanly\n", cpu);
  144. fallthrough;
  145. case CPU_STUCK_IN_KERNEL:
  146. pr_crit("CPU%u: is stuck in kernel\n", cpu);
  147. if (status & CPU_STUCK_REASON_52_BIT_VA)
  148. pr_crit("CPU%u: does not support 52-bit VAs\n", cpu);
  149. if (status & CPU_STUCK_REASON_NO_GRAN) {
  150. pr_crit("CPU%u: does not support %luK granule\n",
  151. cpu, PAGE_SIZE / SZ_1K);
  152. }
  153. cpus_stuck_in_kernel++;
  154. break;
  155. case CPU_PANIC_KERNEL:
  156. panic("CPU%u detected unsupported configuration\n", cpu);
  157. }
  158. return -EIO;
  159. }
  160. static void init_gic_priority_masking(void)
  161. {
  162. u32 cpuflags;
  163. if (WARN_ON(!gic_enable_sre()))
  164. return;
  165. cpuflags = read_sysreg(daif);
  166. WARN_ON(!(cpuflags & PSR_I_BIT));
  167. WARN_ON(!(cpuflags & PSR_F_BIT));
  168. gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
  169. }
  170. /*
  171. * This is the secondary CPU boot entry. We're using this CPUs
  172. * idle thread stack, but a set of temporary page tables.
  173. */
  174. asmlinkage notrace void secondary_start_kernel(void)
  175. {
  176. u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  177. struct mm_struct *mm = &init_mm;
  178. const struct cpu_operations *ops;
  179. unsigned int cpu = smp_processor_id();
  180. /*
  181. * All kernel threads share the same mm context; grab a
  182. * reference and switch to it.
  183. */
  184. mmgrab(mm);
  185. current->active_mm = mm;
  186. /*
  187. * TTBR0 is only used for the identity mapping at this stage. Make it
  188. * point to zero page to avoid speculatively fetching new entries.
  189. */
  190. cpu_uninstall_idmap();
  191. if (system_uses_irq_prio_masking())
  192. init_gic_priority_masking();
  193. rcu_cpu_starting(cpu);
  194. trace_hardirqs_off();
  195. /*
  196. * If the system has established the capabilities, make sure
  197. * this CPU ticks all of those. If it doesn't, the CPU will
  198. * fail to come online.
  199. */
  200. check_local_cpu_capabilities();
  201. ops = get_cpu_ops(cpu);
  202. if (ops->cpu_postboot)
  203. ops->cpu_postboot();
  204. /*
  205. * Log the CPU info before it is marked online and might get read.
  206. */
  207. cpuinfo_store_cpu();
  208. store_cpu_topology(cpu);
  209. /*
  210. * Enable GIC and timers.
  211. */
  212. notify_cpu_starting(cpu);
  213. ipi_setup(cpu);
  214. numa_add_cpu(cpu);
  215. /*
  216. * OK, now it's safe to let the boot CPU continue. Wait for
  217. * the CPU migration code to notice that the CPU is online
  218. * before we continue.
  219. */
  220. pr_info("CPU%u: Booted secondary processor 0x%010lx [0x%08x]\n",
  221. cpu, (unsigned long)mpidr,
  222. read_cpuid_id());
  223. update_cpu_boot_status(CPU_BOOT_SUCCESS);
  224. set_cpu_online(cpu, true);
  225. complete(&cpu_running);
  226. local_daif_restore(DAIF_PROCCTX);
  227. /*
  228. * OK, it's off to the idle thread for us
  229. */
  230. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  231. }
  232. #ifdef CONFIG_HOTPLUG_CPU
  233. static int op_cpu_disable(unsigned int cpu)
  234. {
  235. const struct cpu_operations *ops = get_cpu_ops(cpu);
  236. /*
  237. * If we don't have a cpu_die method, abort before we reach the point
  238. * of no return. CPU0 may not have an cpu_ops, so test for it.
  239. */
  240. if (!ops || !ops->cpu_die)
  241. return -EOPNOTSUPP;
  242. /*
  243. * We may need to abort a hot unplug for some other mechanism-specific
  244. * reason.
  245. */
  246. if (ops->cpu_disable)
  247. return ops->cpu_disable(cpu);
  248. return 0;
  249. }
  250. /*
  251. * __cpu_disable runs on the processor to be shutdown.
  252. */
  253. int __cpu_disable(void)
  254. {
  255. unsigned int cpu = smp_processor_id();
  256. int ret;
  257. ret = op_cpu_disable(cpu);
  258. if (ret)
  259. return ret;
  260. remove_cpu_topology(cpu);
  261. numa_remove_cpu(cpu);
  262. /*
  263. * Take this CPU offline. Once we clear this, we can't return,
  264. * and we must not schedule until we're ready to give up the cpu.
  265. */
  266. set_cpu_online(cpu, false);
  267. ipi_teardown(cpu);
  268. /*
  269. * OK - migrate IRQs away from this CPU
  270. */
  271. irq_migrate_all_off_this_cpu();
  272. return 0;
  273. }
  274. static int op_cpu_kill(unsigned int cpu)
  275. {
  276. const struct cpu_operations *ops = get_cpu_ops(cpu);
  277. /*
  278. * If we have no means of synchronising with the dying CPU, then assume
  279. * that it is really dead. We can only wait for an arbitrary length of
  280. * time and hope that it's dead, so let's skip the wait and just hope.
  281. */
  282. if (!ops->cpu_kill)
  283. return 0;
  284. return ops->cpu_kill(cpu);
  285. }
  286. /*
  287. * called on the thread which is asking for a CPU to be shutdown -
  288. * waits until shutdown has completed, or it is timed out.
  289. */
  290. void __cpu_die(unsigned int cpu)
  291. {
  292. int err;
  293. if (!cpu_wait_death(cpu, 5)) {
  294. pr_crit("CPU%u: cpu didn't die\n", cpu);
  295. return;
  296. }
  297. pr_debug("CPU%u: shutdown\n", cpu);
  298. /*
  299. * Now that the dying CPU is beyond the point of no return w.r.t.
  300. * in-kernel synchronisation, try to get the firwmare to help us to
  301. * verify that it has really left the kernel before we consider
  302. * clobbering anything it might still be using.
  303. */
  304. err = op_cpu_kill(cpu);
  305. if (err)
  306. pr_warn("CPU%d may not have shut down cleanly: %d\n", cpu, err);
  307. }
  308. /*
  309. * Called from the idle thread for the CPU which has been shutdown.
  310. *
  311. */
  312. void cpu_die(void)
  313. {
  314. unsigned int cpu = smp_processor_id();
  315. const struct cpu_operations *ops = get_cpu_ops(cpu);
  316. idle_task_exit();
  317. local_daif_mask();
  318. /* Tell __cpu_die() that this CPU is now safe to dispose of */
  319. (void)cpu_report_death();
  320. /*
  321. * Actually shutdown the CPU. This must never fail. The specific hotplug
  322. * mechanism must perform all required cache maintenance to ensure that
  323. * no dirty lines are lost in the process of shutting down the CPU.
  324. */
  325. ops->cpu_die(cpu);
  326. BUG();
  327. }
  328. #endif
  329. static void __cpu_try_die(int cpu)
  330. {
  331. #ifdef CONFIG_HOTPLUG_CPU
  332. const struct cpu_operations *ops = get_cpu_ops(cpu);
  333. if (ops && ops->cpu_die)
  334. ops->cpu_die(cpu);
  335. #endif
  336. }
  337. /*
  338. * Kill the calling secondary CPU, early in bringup before it is turned
  339. * online.
  340. */
  341. void cpu_die_early(void)
  342. {
  343. int cpu = smp_processor_id();
  344. pr_crit("CPU%d: will not boot\n", cpu);
  345. /* Mark this CPU absent */
  346. set_cpu_present(cpu, 0);
  347. rcu_report_dead(cpu);
  348. if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
  349. update_cpu_boot_status(CPU_KILL_ME);
  350. __cpu_try_die(cpu);
  351. }
  352. update_cpu_boot_status(CPU_STUCK_IN_KERNEL);
  353. cpu_park_loop();
  354. }
  355. static void __init hyp_mode_check(void)
  356. {
  357. if (is_hyp_mode_available())
  358. pr_info("CPU: All CPU(s) started at EL2\n");
  359. else if (is_hyp_mode_mismatched())
  360. WARN_TAINT(1, TAINT_CPU_OUT_OF_SPEC,
  361. "CPU: CPUs started in inconsistent modes");
  362. else
  363. pr_info("CPU: All CPU(s) started at EL1\n");
  364. if (IS_ENABLED(CONFIG_KVM) && !is_kernel_in_hyp_mode()) {
  365. kvm_compute_layout();
  366. kvm_apply_hyp_relocations();
  367. }
  368. }
  369. void __init smp_cpus_done(unsigned int max_cpus)
  370. {
  371. pr_info("SMP: Total of %d processors activated.\n", num_online_cpus());
  372. setup_cpu_features();
  373. hyp_mode_check();
  374. apply_alternatives_all();
  375. mark_linear_text_alias_ro();
  376. }
  377. void __init smp_prepare_boot_cpu(void)
  378. {
  379. /*
  380. * The runtime per-cpu areas have been allocated by
  381. * setup_per_cpu_areas(), and CPU0's boot time per-cpu area will be
  382. * freed shortly, so we must move over to the runtime per-cpu area.
  383. */
  384. set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
  385. cpuinfo_store_boot_cpu();
  386. /*
  387. * We now know enough about the boot CPU to apply the
  388. * alternatives that cannot wait until interrupt handling
  389. * and/or scheduling is enabled.
  390. */
  391. apply_boot_alternatives();
  392. /* Conditionally switch to GIC PMR for interrupt masking */
  393. if (system_uses_irq_prio_masking())
  394. init_gic_priority_masking();
  395. kasan_init_hw_tags();
  396. }
  397. /*
  398. * Duplicate MPIDRs are a recipe for disaster. Scan all initialized
  399. * entries and check for duplicates. If any is found just ignore the
  400. * cpu. cpu_logical_map was initialized to INVALID_HWID to avoid
  401. * matching valid MPIDR values.
  402. */
  403. static bool __init is_mpidr_duplicate(unsigned int cpu, u64 hwid)
  404. {
  405. unsigned int i;
  406. for (i = 1; (i < cpu) && (i < NR_CPUS); i++)
  407. if (cpu_logical_map(i) == hwid)
  408. return true;
  409. return false;
  410. }
  411. /*
  412. * Initialize cpu operations for a logical cpu and
  413. * set it in the possible mask on success
  414. */
  415. static int __init smp_cpu_setup(int cpu)
  416. {
  417. const struct cpu_operations *ops;
  418. if (init_cpu_ops(cpu))
  419. return -ENODEV;
  420. ops = get_cpu_ops(cpu);
  421. if (ops->cpu_init(cpu))
  422. return -ENODEV;
  423. set_cpu_possible(cpu, true);
  424. return 0;
  425. }
  426. static bool bootcpu_valid __initdata;
  427. static unsigned int cpu_count = 1;
  428. #ifdef CONFIG_ACPI
  429. static struct acpi_madt_generic_interrupt cpu_madt_gicc[NR_CPUS];
  430. struct acpi_madt_generic_interrupt *acpi_cpu_get_madt_gicc(int cpu)
  431. {
  432. return &cpu_madt_gicc[cpu];
  433. }
  434. EXPORT_SYMBOL_GPL(acpi_cpu_get_madt_gicc);
  435. /*
  436. * acpi_map_gic_cpu_interface - parse processor MADT entry
  437. *
  438. * Carry out sanity checks on MADT processor entry and initialize
  439. * cpu_logical_map on success
  440. */
  441. static void __init
  442. acpi_map_gic_cpu_interface(struct acpi_madt_generic_interrupt *processor)
  443. {
  444. u64 hwid = processor->arm_mpidr;
  445. if (!(processor->flags & ACPI_MADT_ENABLED)) {
  446. pr_debug("skipping disabled CPU entry with 0x%llx MPIDR\n", hwid);
  447. return;
  448. }
  449. if (hwid & ~MPIDR_HWID_BITMASK || hwid == INVALID_HWID) {
  450. pr_err("skipping CPU entry with invalid MPIDR 0x%llx\n", hwid);
  451. return;
  452. }
  453. if (is_mpidr_duplicate(cpu_count, hwid)) {
  454. pr_err("duplicate CPU MPIDR 0x%llx in MADT\n", hwid);
  455. return;
  456. }
  457. /* Check if GICC structure of boot CPU is available in the MADT */
  458. if (cpu_logical_map(0) == hwid) {
  459. if (bootcpu_valid) {
  460. pr_err("duplicate boot CPU MPIDR: 0x%llx in MADT\n",
  461. hwid);
  462. return;
  463. }
  464. bootcpu_valid = true;
  465. cpu_madt_gicc[0] = *processor;
  466. return;
  467. }
  468. if (cpu_count >= NR_CPUS)
  469. return;
  470. /* map the logical cpu id to cpu MPIDR */
  471. set_cpu_logical_map(cpu_count, hwid);
  472. cpu_madt_gicc[cpu_count] = *processor;
  473. /*
  474. * Set-up the ACPI parking protocol cpu entries
  475. * while initializing the cpu_logical_map to
  476. * avoid parsing MADT entries multiple times for
  477. * nothing (ie a valid cpu_logical_map entry should
  478. * contain a valid parking protocol data set to
  479. * initialize the cpu if the parking protocol is
  480. * the only available enable method).
  481. */
  482. acpi_set_mailbox_entry(cpu_count, processor);
  483. cpu_count++;
  484. }
  485. static int __init
  486. acpi_parse_gic_cpu_interface(union acpi_subtable_headers *header,
  487. const unsigned long end)
  488. {
  489. struct acpi_madt_generic_interrupt *processor;
  490. processor = (struct acpi_madt_generic_interrupt *)header;
  491. if (BAD_MADT_GICC_ENTRY(processor, end))
  492. return -EINVAL;
  493. acpi_table_print_madt_entry(&header->common);
  494. acpi_map_gic_cpu_interface(processor);
  495. return 0;
  496. }
  497. static void __init acpi_parse_and_init_cpus(void)
  498. {
  499. int i;
  500. /*
  501. * do a walk of MADT to determine how many CPUs
  502. * we have including disabled CPUs, and get information
  503. * we need for SMP init.
  504. */
  505. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  506. acpi_parse_gic_cpu_interface, 0);
  507. /*
  508. * In ACPI, SMP and CPU NUMA information is provided in separate
  509. * static tables, namely the MADT and the SRAT.
  510. *
  511. * Thus, it is simpler to first create the cpu logical map through
  512. * an MADT walk and then map the logical cpus to their node ids
  513. * as separate steps.
  514. */
  515. acpi_map_cpus_to_nodes();
  516. for (i = 0; i < nr_cpu_ids; i++)
  517. early_map_cpu_to_node(i, acpi_numa_get_nid(i));
  518. }
  519. #else
  520. #define acpi_parse_and_init_cpus(...) do { } while (0)
  521. #endif
  522. /*
  523. * Enumerate the possible CPU set from the device tree and build the
  524. * cpu logical map array containing MPIDR values related to logical
  525. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  526. */
  527. static void __init of_parse_and_init_cpus(void)
  528. {
  529. struct device_node *dn;
  530. for_each_of_cpu_node(dn) {
  531. u64 hwid = of_get_cpu_hwid(dn, 0);
  532. if (hwid & ~MPIDR_HWID_BITMASK)
  533. goto next;
  534. if (is_mpidr_duplicate(cpu_count, hwid)) {
  535. pr_err("%pOF: duplicate cpu reg properties in the DT\n",
  536. dn);
  537. goto next;
  538. }
  539. /*
  540. * The numbering scheme requires that the boot CPU
  541. * must be assigned logical id 0. Record it so that
  542. * the logical map built from DT is validated and can
  543. * be used.
  544. */
  545. if (hwid == cpu_logical_map(0)) {
  546. if (bootcpu_valid) {
  547. pr_err("%pOF: duplicate boot cpu reg property in DT\n",
  548. dn);
  549. goto next;
  550. }
  551. bootcpu_valid = true;
  552. early_map_cpu_to_node(0, of_node_to_nid(dn));
  553. /*
  554. * cpu_logical_map has already been
  555. * initialized and the boot cpu doesn't need
  556. * the enable-method so continue without
  557. * incrementing cpu.
  558. */
  559. continue;
  560. }
  561. if (cpu_count >= NR_CPUS)
  562. goto next;
  563. pr_debug("cpu logical map 0x%llx\n", hwid);
  564. set_cpu_logical_map(cpu_count, hwid);
  565. early_map_cpu_to_node(cpu_count, of_node_to_nid(dn));
  566. next:
  567. cpu_count++;
  568. }
  569. }
  570. /*
  571. * Enumerate the possible CPU set from the device tree or ACPI and build the
  572. * cpu logical map array containing MPIDR values related to logical
  573. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  574. */
  575. void __init smp_init_cpus(void)
  576. {
  577. int i;
  578. if (acpi_disabled)
  579. of_parse_and_init_cpus();
  580. else
  581. acpi_parse_and_init_cpus();
  582. if (cpu_count > nr_cpu_ids)
  583. pr_warn("Number of cores (%d) exceeds configured maximum of %u - clipping\n",
  584. cpu_count, nr_cpu_ids);
  585. if (!bootcpu_valid) {
  586. pr_err("missing boot CPU MPIDR, not enabling secondaries\n");
  587. return;
  588. }
  589. /*
  590. * We need to set the cpu_logical_map entries before enabling
  591. * the cpus so that cpu processor description entries (DT cpu nodes
  592. * and ACPI MADT entries) can be retrieved by matching the cpu hwid
  593. * with entries in cpu_logical_map while initializing the cpus.
  594. * If the cpu set-up fails, invalidate the cpu_logical_map entry.
  595. */
  596. for (i = 1; i < nr_cpu_ids; i++) {
  597. if (cpu_logical_map(i) != INVALID_HWID) {
  598. if (smp_cpu_setup(i))
  599. set_cpu_logical_map(i, INVALID_HWID);
  600. }
  601. }
  602. }
  603. void __init smp_prepare_cpus(unsigned int max_cpus)
  604. {
  605. const struct cpu_operations *ops;
  606. int err;
  607. unsigned int cpu;
  608. unsigned int this_cpu;
  609. init_cpu_topology();
  610. this_cpu = smp_processor_id();
  611. store_cpu_topology(this_cpu);
  612. numa_store_cpu_info(this_cpu);
  613. numa_add_cpu(this_cpu);
  614. /*
  615. * If UP is mandated by "nosmp" (which implies "maxcpus=0"), don't set
  616. * secondary CPUs present.
  617. */
  618. if (max_cpus == 0)
  619. return;
  620. /*
  621. * Initialise the present map (which describes the set of CPUs
  622. * actually populated at the present time) and release the
  623. * secondaries from the bootloader.
  624. */
  625. for_each_possible_cpu(cpu) {
  626. per_cpu(cpu_number, cpu) = cpu;
  627. if (cpu == smp_processor_id())
  628. continue;
  629. ops = get_cpu_ops(cpu);
  630. if (!ops)
  631. continue;
  632. err = ops->cpu_prepare(cpu);
  633. if (err)
  634. continue;
  635. set_cpu_present(cpu, true);
  636. numa_store_cpu_info(cpu);
  637. }
  638. }
  639. static const char *ipi_types[NR_IPI] __tracepoint_string = {
  640. [IPI_RESCHEDULE] = "Rescheduling interrupts",
  641. [IPI_CALL_FUNC] = "Function call interrupts",
  642. [IPI_CPU_STOP] = "CPU stop interrupts",
  643. [IPI_CPU_CRASH_STOP] = "CPU stop (for crash dump) interrupts",
  644. [IPI_TIMER] = "Timer broadcast interrupts",
  645. [IPI_IRQ_WORK] = "IRQ work interrupts",
  646. [IPI_WAKEUP] = "CPU wake-up interrupts",
  647. };
  648. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr);
  649. unsigned long irq_err_count;
  650. int arch_show_interrupts(struct seq_file *p, int prec)
  651. {
  652. unsigned int cpu, i;
  653. for (i = 0; i < NR_IPI; i++) {
  654. seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
  655. prec >= 4 ? " " : "");
  656. for_each_online_cpu(cpu)
  657. seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i], cpu));
  658. seq_printf(p, " %s\n", ipi_types[i]);
  659. }
  660. seq_printf(p, "%*s: %10lu\n", prec, "Err", irq_err_count);
  661. return 0;
  662. }
  663. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  664. {
  665. smp_cross_call(mask, IPI_CALL_FUNC);
  666. }
  667. void arch_send_call_function_single_ipi(int cpu)
  668. {
  669. smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC);
  670. }
  671. #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL
  672. void arch_send_wakeup_ipi_mask(const struct cpumask *mask)
  673. {
  674. smp_cross_call(mask, IPI_WAKEUP);
  675. }
  676. #endif
  677. #ifdef CONFIG_IRQ_WORK
  678. void arch_irq_work_raise(void)
  679. {
  680. smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
  681. }
  682. #endif
  683. static void local_cpu_stop(void)
  684. {
  685. set_cpu_online(smp_processor_id(), false);
  686. local_daif_mask();
  687. sdei_mask_local_cpu();
  688. cpu_park_loop();
  689. }
  690. /*
  691. * We need to implement panic_smp_self_stop() for parallel panic() calls, so
  692. * that cpu_online_mask gets correctly updated and smp_send_stop() can skip
  693. * CPUs that have already stopped themselves.
  694. */
  695. void panic_smp_self_stop(void)
  696. {
  697. local_cpu_stop();
  698. }
  699. #ifdef CONFIG_KEXEC_CORE
  700. static atomic_t waiting_for_crash_ipi = ATOMIC_INIT(0);
  701. #endif
  702. static void ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs)
  703. {
  704. #ifdef CONFIG_KEXEC_CORE
  705. crash_save_cpu(regs, cpu);
  706. atomic_dec(&waiting_for_crash_ipi);
  707. local_irq_disable();
  708. sdei_mask_local_cpu();
  709. if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
  710. __cpu_try_die(cpu);
  711. /* just in case */
  712. cpu_park_loop();
  713. #endif
  714. }
  715. /*
  716. * Main handler for inter-processor interrupts
  717. */
  718. static void do_handle_IPI(int ipinr)
  719. {
  720. unsigned int cpu = smp_processor_id();
  721. if ((unsigned)ipinr < NR_IPI)
  722. trace_ipi_entry_rcuidle(ipi_types[ipinr]);
  723. switch (ipinr) {
  724. case IPI_RESCHEDULE:
  725. scheduler_ipi();
  726. break;
  727. case IPI_CALL_FUNC:
  728. generic_smp_call_function_interrupt();
  729. break;
  730. case IPI_CPU_STOP:
  731. trace_android_vh_ipi_stop(get_irq_regs());
  732. local_cpu_stop();
  733. break;
  734. case IPI_CPU_CRASH_STOP:
  735. if (IS_ENABLED(CONFIG_KEXEC_CORE)) {
  736. ipi_cpu_crash_stop(cpu, get_irq_regs());
  737. unreachable();
  738. }
  739. break;
  740. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  741. case IPI_TIMER:
  742. tick_receive_broadcast();
  743. break;
  744. #endif
  745. #ifdef CONFIG_IRQ_WORK
  746. case IPI_IRQ_WORK:
  747. irq_work_run();
  748. break;
  749. #endif
  750. #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL
  751. case IPI_WAKEUP:
  752. WARN_ONCE(!acpi_parking_protocol_valid(cpu),
  753. "CPU%u: Wake-up IPI outside the ACPI parking protocol\n",
  754. cpu);
  755. break;
  756. #endif
  757. default:
  758. pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
  759. break;
  760. }
  761. if ((unsigned)ipinr < NR_IPI)
  762. trace_ipi_exit_rcuidle(ipi_types[ipinr]);
  763. }
  764. static irqreturn_t ipi_handler(int irq, void *data)
  765. {
  766. do_handle_IPI(irq - ipi_irq_base);
  767. return IRQ_HANDLED;
  768. }
  769. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
  770. {
  771. trace_ipi_raise(target, ipi_types[ipinr]);
  772. __ipi_send_mask(ipi_desc[ipinr], target);
  773. }
  774. static void ipi_setup(int cpu)
  775. {
  776. int i;
  777. if (WARN_ON_ONCE(!ipi_irq_base))
  778. return;
  779. for (i = 0; i < nr_ipi; i++)
  780. enable_percpu_irq(ipi_irq_base + i, 0);
  781. }
  782. #ifdef CONFIG_HOTPLUG_CPU
  783. static void ipi_teardown(int cpu)
  784. {
  785. int i;
  786. if (WARN_ON_ONCE(!ipi_irq_base))
  787. return;
  788. for (i = 0; i < nr_ipi; i++)
  789. disable_percpu_irq(ipi_irq_base + i);
  790. }
  791. #endif
  792. void __init set_smp_ipi_range(int ipi_base, int n)
  793. {
  794. int i;
  795. WARN_ON(n < NR_IPI);
  796. nr_ipi = min(n, NR_IPI);
  797. for (i = 0; i < nr_ipi; i++) {
  798. int err;
  799. err = request_percpu_irq(ipi_base + i, ipi_handler,
  800. "IPI", &cpu_number);
  801. WARN_ON(err);
  802. ipi_desc[i] = irq_to_desc(ipi_base + i);
  803. irq_set_status_flags(ipi_base + i, IRQ_HIDDEN);
  804. }
  805. ipi_irq_base = ipi_base;
  806. /* Setup the boot CPU immediately */
  807. ipi_setup(smp_processor_id());
  808. }
  809. void smp_send_reschedule(int cpu)
  810. {
  811. smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
  812. }
  813. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  814. void tick_broadcast(const struct cpumask *mask)
  815. {
  816. smp_cross_call(mask, IPI_TIMER);
  817. }
  818. #endif
  819. /*
  820. * The number of CPUs online, not counting this CPU (which may not be
  821. * fully online and so not counted in num_online_cpus()).
  822. */
  823. static inline unsigned int num_other_online_cpus(void)
  824. {
  825. unsigned int this_cpu_online = cpu_online(smp_processor_id());
  826. return num_online_cpus() - this_cpu_online;
  827. }
  828. void smp_send_stop(void)
  829. {
  830. unsigned long timeout;
  831. if (num_other_online_cpus()) {
  832. cpumask_t mask;
  833. cpumask_copy(&mask, cpu_online_mask);
  834. cpumask_clear_cpu(smp_processor_id(), &mask);
  835. if (system_state <= SYSTEM_RUNNING)
  836. pr_crit("SMP: stopping secondary CPUs\n");
  837. smp_cross_call(&mask, IPI_CPU_STOP);
  838. }
  839. /* Wait up to one second for other CPUs to stop */
  840. timeout = USEC_PER_SEC;
  841. while (num_other_online_cpus() && timeout--)
  842. udelay(1);
  843. if (num_other_online_cpus())
  844. pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
  845. cpumask_pr_args(cpu_online_mask));
  846. sdei_mask_local_cpu();
  847. }
  848. #ifdef CONFIG_KEXEC_CORE
  849. void crash_smp_send_stop(void)
  850. {
  851. static int cpus_stopped;
  852. cpumask_t mask;
  853. unsigned long timeout;
  854. /*
  855. * This function can be called twice in panic path, but obviously
  856. * we execute this only once.
  857. */
  858. if (cpus_stopped)
  859. return;
  860. cpus_stopped = 1;
  861. /*
  862. * If this cpu is the only one alive at this point in time, online or
  863. * not, there are no stop messages to be sent around, so just back out.
  864. */
  865. if (num_other_online_cpus() == 0)
  866. goto skip_ipi;
  867. cpumask_copy(&mask, cpu_online_mask);
  868. cpumask_clear_cpu(smp_processor_id(), &mask);
  869. atomic_set(&waiting_for_crash_ipi, num_other_online_cpus());
  870. pr_crit("SMP: stopping secondary CPUs\n");
  871. smp_cross_call(&mask, IPI_CPU_CRASH_STOP);
  872. /* Wait up to one second for other CPUs to stop */
  873. timeout = USEC_PER_SEC;
  874. while ((atomic_read(&waiting_for_crash_ipi) > 0) && timeout--)
  875. udelay(1);
  876. if (atomic_read(&waiting_for_crash_ipi) > 0)
  877. pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
  878. cpumask_pr_args(&mask));
  879. skip_ipi:
  880. sdei_mask_local_cpu();
  881. sdei_handler_abort();
  882. }
  883. bool smp_crash_stop_failed(void)
  884. {
  885. return (atomic_read(&waiting_for_crash_ipi) > 0);
  886. }
  887. #endif
  888. static bool have_cpu_die(void)
  889. {
  890. #ifdef CONFIG_HOTPLUG_CPU
  891. int any_cpu = raw_smp_processor_id();
  892. const struct cpu_operations *ops = get_cpu_ops(any_cpu);
  893. if (ops && ops->cpu_die)
  894. return true;
  895. #endif
  896. return false;
  897. }
  898. bool cpus_are_stuck_in_kernel(void)
  899. {
  900. bool smp_spin_tables = (num_possible_cpus() > 1 && !have_cpu_die());
  901. return !!cpus_stuck_in_kernel || smp_spin_tables ||
  902. is_protected_kvm_enabled();
  903. }
  904. int nr_ipi_get(void)
  905. {
  906. return nr_ipi;
  907. }
  908. EXPORT_SYMBOL_GPL(nr_ipi_get);
  909. struct irq_desc **ipi_desc_get(void)
  910. {
  911. return ipi_desc;
  912. }
  913. EXPORT_SYMBOL_GPL(ipi_desc_get);