smp.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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/kgdb.h>
  35. #include <linux/kvm_host.h>
  36. #include <linux/nmi.h>
  37. #include <asm/alternative.h>
  38. #include <asm/atomic.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/cpu.h>
  41. #include <asm/cputype.h>
  42. #include <asm/cpu_ops.h>
  43. #include <asm/daifflags.h>
  44. #include <asm/kvm_mmu.h>
  45. #include <asm/mmu_context.h>
  46. #include <asm/numa.h>
  47. #include <asm/processor.h>
  48. #include <asm/smp_plat.h>
  49. #include <asm/sections.h>
  50. #include <asm/tlbflush.h>
  51. #include <asm/ptrace.h>
  52. #include <asm/virt.h>
  53. #include <trace/events/ipi.h>
  54. /*
  55. * as from 2.5, kernels no longer have an init_tasks structure
  56. * so we need some other way of telling a new secondary core
  57. * where to place its SVC stack
  58. */
  59. struct secondary_data secondary_data;
  60. /* Number of CPUs which aren't online, but looping in kernel text. */
  61. static int cpus_stuck_in_kernel;
  62. enum ipi_msg_type {
  63. IPI_RESCHEDULE,
  64. IPI_CALL_FUNC,
  65. IPI_CPU_STOP,
  66. IPI_CPU_STOP_NMI,
  67. IPI_TIMER,
  68. IPI_IRQ_WORK,
  69. NR_IPI,
  70. /*
  71. * Any enum >= NR_IPI and < MAX_IPI is special and not tracable
  72. * with trace_ipi_*
  73. */
  74. IPI_CPU_BACKTRACE = NR_IPI,
  75. IPI_KGDB_ROUNDUP,
  76. MAX_IPI
  77. };
  78. static int ipi_irq_base __ro_after_init;
  79. static int nr_ipi __ro_after_init = NR_IPI;
  80. static struct irq_desc *ipi_desc[MAX_IPI] __ro_after_init;
  81. static bool crash_stop;
  82. static void ipi_setup(int cpu);
  83. #ifdef CONFIG_HOTPLUG_CPU
  84. static void ipi_teardown(int cpu);
  85. static int op_cpu_kill(unsigned int cpu);
  86. #else
  87. static inline int op_cpu_kill(unsigned int cpu)
  88. {
  89. return -ENOSYS;
  90. }
  91. #endif
  92. /*
  93. * Boot a secondary CPU, and assign it the specified idle task.
  94. * This also gives us the initial stack to use for this CPU.
  95. */
  96. static int boot_secondary(unsigned int cpu, struct task_struct *idle)
  97. {
  98. const struct cpu_operations *ops = get_cpu_ops(cpu);
  99. if (ops->cpu_boot)
  100. return ops->cpu_boot(cpu);
  101. return -EOPNOTSUPP;
  102. }
  103. static DECLARE_COMPLETION(cpu_running);
  104. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  105. {
  106. int ret;
  107. long status;
  108. /*
  109. * We need to tell the secondary core where to find its stack and the
  110. * page tables.
  111. */
  112. secondary_data.task = idle;
  113. update_cpu_boot_status(CPU_MMU_OFF);
  114. /* Now bring the CPU into our world */
  115. ret = boot_secondary(cpu, idle);
  116. if (ret) {
  117. if (ret != -EPERM)
  118. pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
  119. return ret;
  120. }
  121. /*
  122. * CPU was successfully started, wait for it to come online or
  123. * time out.
  124. */
  125. wait_for_completion_timeout(&cpu_running,
  126. msecs_to_jiffies(5000));
  127. if (cpu_online(cpu))
  128. return 0;
  129. pr_crit("CPU%u: failed to come online\n", cpu);
  130. secondary_data.task = NULL;
  131. status = READ_ONCE(secondary_data.status);
  132. if (status == CPU_MMU_OFF)
  133. status = READ_ONCE(__early_cpu_boot_status);
  134. switch (status & CPU_BOOT_STATUS_MASK) {
  135. default:
  136. pr_err("CPU%u: failed in unknown state : 0x%lx\n",
  137. cpu, status);
  138. cpus_stuck_in_kernel++;
  139. break;
  140. case CPU_KILL_ME:
  141. if (!op_cpu_kill(cpu)) {
  142. pr_crit("CPU%u: died during early boot\n", cpu);
  143. break;
  144. }
  145. pr_crit("CPU%u: may not have shut down cleanly\n", cpu);
  146. fallthrough;
  147. case CPU_STUCK_IN_KERNEL:
  148. pr_crit("CPU%u: is stuck in kernel\n", cpu);
  149. if (status & CPU_STUCK_REASON_52_BIT_VA)
  150. pr_crit("CPU%u: does not support 52-bit VAs\n", cpu);
  151. if (status & CPU_STUCK_REASON_NO_GRAN) {
  152. pr_crit("CPU%u: does not support %luK granule\n",
  153. cpu, PAGE_SIZE / SZ_1K);
  154. }
  155. cpus_stuck_in_kernel++;
  156. break;
  157. case CPU_PANIC_KERNEL:
  158. panic("CPU%u detected unsupported configuration\n", cpu);
  159. }
  160. return -EIO;
  161. }
  162. static void init_gic_priority_masking(void)
  163. {
  164. u32 cpuflags;
  165. if (WARN_ON(!gic_enable_sre()))
  166. return;
  167. cpuflags = read_sysreg(daif);
  168. WARN_ON(!(cpuflags & PSR_I_BIT));
  169. WARN_ON(!(cpuflags & PSR_F_BIT));
  170. gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
  171. }
  172. /*
  173. * This is the secondary CPU boot entry. We're using this CPUs
  174. * idle thread stack, but a set of temporary page tables.
  175. */
  176. asmlinkage notrace void secondary_start_kernel(void)
  177. {
  178. u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  179. struct mm_struct *mm = &init_mm;
  180. const struct cpu_operations *ops;
  181. unsigned int cpu = smp_processor_id();
  182. /*
  183. * All kernel threads share the same mm context; grab a
  184. * reference and switch to it.
  185. */
  186. mmgrab(mm);
  187. current->active_mm = mm;
  188. /*
  189. * TTBR0 is only used for the identity mapping at this stage. Make it
  190. * point to zero page to avoid speculatively fetching new entries.
  191. */
  192. cpu_uninstall_idmap();
  193. if (system_uses_irq_prio_masking())
  194. init_gic_priority_masking();
  195. rcutree_report_cpu_starting(cpu);
  196. trace_hardirqs_off();
  197. /*
  198. * If the system has established the capabilities, make sure
  199. * this CPU ticks all of those. If it doesn't, the CPU will
  200. * fail to come online.
  201. */
  202. check_local_cpu_capabilities();
  203. ops = get_cpu_ops(cpu);
  204. if (ops->cpu_postboot)
  205. ops->cpu_postboot();
  206. /*
  207. * Log the CPU info before it is marked online and might get read.
  208. */
  209. cpuinfo_store_cpu();
  210. store_cpu_topology(cpu);
  211. /*
  212. * Enable GIC and timers.
  213. */
  214. notify_cpu_starting(cpu);
  215. ipi_setup(cpu);
  216. numa_add_cpu(cpu);
  217. /*
  218. * OK, now it's safe to let the boot CPU continue. Wait for
  219. * the CPU migration code to notice that the CPU is online
  220. * before we continue.
  221. */
  222. pr_info("CPU%u: Booted secondary processor 0x%010lx [0x%08x]\n",
  223. cpu, (unsigned long)mpidr,
  224. read_cpuid_id());
  225. update_cpu_boot_status(CPU_BOOT_SUCCESS);
  226. set_cpu_online(cpu, true);
  227. complete(&cpu_running);
  228. /*
  229. * Secondary CPUs enter the kernel with all DAIF exceptions masked.
  230. *
  231. * As with setup_arch() we must unmask Debug and SError exceptions, and
  232. * as the root irqchip has already been detected and initialized we can
  233. * unmask IRQ and FIQ at the same time.
  234. */
  235. local_daif_restore(DAIF_PROCCTX);
  236. /*
  237. * OK, it's off to the idle thread for us
  238. */
  239. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  240. }
  241. #ifdef CONFIG_HOTPLUG_CPU
  242. static int op_cpu_disable(unsigned int cpu)
  243. {
  244. const struct cpu_operations *ops = get_cpu_ops(cpu);
  245. /*
  246. * If we don't have a cpu_die method, abort before we reach the point
  247. * of no return. CPU0 may not have an cpu_ops, so test for it.
  248. */
  249. if (!ops || !ops->cpu_die)
  250. return -EOPNOTSUPP;
  251. /*
  252. * We may need to abort a hot unplug for some other mechanism-specific
  253. * reason.
  254. */
  255. if (ops->cpu_disable)
  256. return ops->cpu_disable(cpu);
  257. return 0;
  258. }
  259. /*
  260. * __cpu_disable runs on the processor to be shutdown.
  261. */
  262. int __cpu_disable(void)
  263. {
  264. unsigned int cpu = smp_processor_id();
  265. int ret;
  266. ret = op_cpu_disable(cpu);
  267. if (ret)
  268. return ret;
  269. remove_cpu_topology(cpu);
  270. numa_remove_cpu(cpu);
  271. /*
  272. * Take this CPU offline. Once we clear this, we can't return,
  273. * and we must not schedule until we're ready to give up the cpu.
  274. */
  275. set_cpu_online(cpu, false);
  276. ipi_teardown(cpu);
  277. /*
  278. * OK - migrate IRQs away from this CPU
  279. */
  280. irq_migrate_all_off_this_cpu();
  281. return 0;
  282. }
  283. static int op_cpu_kill(unsigned int cpu)
  284. {
  285. const struct cpu_operations *ops = get_cpu_ops(cpu);
  286. /*
  287. * If we have no means of synchronising with the dying CPU, then assume
  288. * that it is really dead. We can only wait for an arbitrary length of
  289. * time and hope that it's dead, so let's skip the wait and just hope.
  290. */
  291. if (!ops->cpu_kill)
  292. return 0;
  293. return ops->cpu_kill(cpu);
  294. }
  295. /*
  296. * Called on the thread which is asking for a CPU to be shutdown after the
  297. * shutdown completed.
  298. */
  299. void arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)
  300. {
  301. int err;
  302. pr_debug("CPU%u: shutdown\n", cpu);
  303. /*
  304. * Now that the dying CPU is beyond the point of no return w.r.t.
  305. * in-kernel synchronisation, try to get the firwmare to help us to
  306. * verify that it has really left the kernel before we consider
  307. * clobbering anything it might still be using.
  308. */
  309. err = op_cpu_kill(cpu);
  310. if (err)
  311. pr_warn("CPU%d may not have shut down cleanly: %d\n", cpu, err);
  312. }
  313. /*
  314. * Called from the idle thread for the CPU which has been shutdown.
  315. *
  316. */
  317. void __noreturn cpu_die(void)
  318. {
  319. unsigned int cpu = smp_processor_id();
  320. const struct cpu_operations *ops = get_cpu_ops(cpu);
  321. idle_task_exit();
  322. local_daif_mask();
  323. /* Tell cpuhp_bp_sync_dead() that this CPU is now safe to dispose of */
  324. cpuhp_ap_report_dead();
  325. /*
  326. * Actually shutdown the CPU. This must never fail. The specific hotplug
  327. * mechanism must perform all required cache maintenance to ensure that
  328. * no dirty lines are lost in the process of shutting down the CPU.
  329. */
  330. ops->cpu_die(cpu);
  331. BUG();
  332. }
  333. #endif
  334. static void __cpu_try_die(int cpu)
  335. {
  336. #ifdef CONFIG_HOTPLUG_CPU
  337. const struct cpu_operations *ops = get_cpu_ops(cpu);
  338. if (ops && ops->cpu_die)
  339. ops->cpu_die(cpu);
  340. #endif
  341. }
  342. /*
  343. * Kill the calling secondary CPU, early in bringup before it is turned
  344. * online.
  345. */
  346. void __noreturn cpu_die_early(void)
  347. {
  348. int cpu = smp_processor_id();
  349. pr_crit("CPU%d: will not boot\n", cpu);
  350. /* Mark this CPU absent */
  351. set_cpu_present(cpu, 0);
  352. rcutree_report_cpu_dead();
  353. if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
  354. update_cpu_boot_status(CPU_KILL_ME);
  355. __cpu_try_die(cpu);
  356. }
  357. update_cpu_boot_status(CPU_STUCK_IN_KERNEL);
  358. cpu_park_loop();
  359. }
  360. static void __init hyp_mode_check(void)
  361. {
  362. if (is_hyp_mode_available())
  363. pr_info("CPU: All CPU(s) started at EL2\n");
  364. else if (is_hyp_mode_mismatched())
  365. WARN_TAINT(1, TAINT_CPU_OUT_OF_SPEC,
  366. "CPU: CPUs started in inconsistent modes");
  367. else
  368. pr_info("CPU: All CPU(s) started at EL1\n");
  369. if (IS_ENABLED(CONFIG_KVM) && !is_kernel_in_hyp_mode()) {
  370. kvm_compute_layout();
  371. kvm_apply_hyp_relocations();
  372. }
  373. }
  374. void __init smp_cpus_done(unsigned int max_cpus)
  375. {
  376. pr_info("SMP: Total of %d processors activated.\n", num_online_cpus());
  377. hyp_mode_check();
  378. setup_system_features();
  379. setup_user_features();
  380. mark_linear_text_alias_ro();
  381. }
  382. void __init smp_prepare_boot_cpu(void)
  383. {
  384. /*
  385. * The runtime per-cpu areas have been allocated by
  386. * setup_per_cpu_areas(), and CPU0's boot time per-cpu area will be
  387. * freed shortly, so we must move over to the runtime per-cpu area.
  388. */
  389. set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
  390. cpuinfo_store_boot_cpu();
  391. setup_boot_cpu_features();
  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. /* Init percpu seeds for random tags after cpus are set up. */
  397. kasan_init_sw_tags();
  398. }
  399. /*
  400. * Duplicate MPIDRs are a recipe for disaster. Scan all initialized
  401. * entries and check for duplicates. If any is found just ignore the
  402. * cpu. cpu_logical_map was initialized to INVALID_HWID to avoid
  403. * matching valid MPIDR values.
  404. */
  405. static bool __init is_mpidr_duplicate(unsigned int cpu, u64 hwid)
  406. {
  407. unsigned int i;
  408. for (i = 1; (i < cpu) && (i < NR_CPUS); i++)
  409. if (cpu_logical_map(i) == hwid)
  410. return true;
  411. return false;
  412. }
  413. /*
  414. * Initialize cpu operations for a logical cpu and
  415. * set it in the possible mask on success
  416. */
  417. static int __init smp_cpu_setup(int cpu)
  418. {
  419. const struct cpu_operations *ops;
  420. if (init_cpu_ops(cpu))
  421. return -ENODEV;
  422. ops = get_cpu_ops(cpu);
  423. if (ops->cpu_init(cpu))
  424. return -ENODEV;
  425. set_cpu_possible(cpu, true);
  426. return 0;
  427. }
  428. static bool bootcpu_valid __initdata;
  429. static unsigned int cpu_count = 1;
  430. int arch_register_cpu(int cpu)
  431. {
  432. acpi_handle acpi_handle = acpi_get_processor_handle(cpu);
  433. struct cpu *c = &per_cpu(cpu_devices, cpu);
  434. if (!acpi_disabled && !acpi_handle &&
  435. IS_ENABLED(CONFIG_ACPI_HOTPLUG_CPU))
  436. return -EPROBE_DEFER;
  437. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  438. /* For now block anything that looks like physical CPU Hotplug */
  439. if (invalid_logical_cpuid(cpu) || !cpu_present(cpu)) {
  440. pr_err_once("Changing CPU present bit is not supported\n");
  441. return -ENODEV;
  442. }
  443. #endif
  444. /*
  445. * Availability of the acpi handle is sufficient to establish
  446. * that _STA has aleady been checked. No need to recheck here.
  447. */
  448. c->hotpluggable = arch_cpu_is_hotpluggable(cpu);
  449. return register_cpu(c, cpu);
  450. }
  451. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  452. void arch_unregister_cpu(int cpu)
  453. {
  454. acpi_handle acpi_handle = acpi_get_processor_handle(cpu);
  455. struct cpu *c = &per_cpu(cpu_devices, cpu);
  456. acpi_status status;
  457. unsigned long long sta;
  458. if (!acpi_handle) {
  459. pr_err_once("Removing a CPU without associated ACPI handle\n");
  460. return;
  461. }
  462. status = acpi_evaluate_integer(acpi_handle, "_STA", NULL, &sta);
  463. if (ACPI_FAILURE(status))
  464. return;
  465. /* For now do not allow anything that looks like physical CPU HP */
  466. if (cpu_present(cpu) && !(sta & ACPI_STA_DEVICE_PRESENT)) {
  467. pr_err_once("Changing CPU present bit is not supported\n");
  468. return;
  469. }
  470. unregister_cpu(c);
  471. }
  472. #endif /* CONFIG_ACPI_HOTPLUG_CPU */
  473. #ifdef CONFIG_ACPI
  474. static struct acpi_madt_generic_interrupt cpu_madt_gicc[NR_CPUS];
  475. struct acpi_madt_generic_interrupt *acpi_cpu_get_madt_gicc(int cpu)
  476. {
  477. return &cpu_madt_gicc[cpu];
  478. }
  479. EXPORT_SYMBOL_GPL(acpi_cpu_get_madt_gicc);
  480. /*
  481. * acpi_map_gic_cpu_interface - parse processor MADT entry
  482. *
  483. * Carry out sanity checks on MADT processor entry and initialize
  484. * cpu_logical_map on success
  485. */
  486. static void __init
  487. acpi_map_gic_cpu_interface(struct acpi_madt_generic_interrupt *processor)
  488. {
  489. u64 hwid = processor->arm_mpidr;
  490. if (!(processor->flags &
  491. (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE))) {
  492. pr_debug("skipping disabled CPU entry with 0x%llx MPIDR\n", hwid);
  493. return;
  494. }
  495. if (hwid & ~MPIDR_HWID_BITMASK || hwid == INVALID_HWID) {
  496. pr_err("skipping CPU entry with invalid MPIDR 0x%llx\n", hwid);
  497. return;
  498. }
  499. if (is_mpidr_duplicate(cpu_count, hwid)) {
  500. pr_err("duplicate CPU MPIDR 0x%llx in MADT\n", hwid);
  501. return;
  502. }
  503. /* Check if GICC structure of boot CPU is available in the MADT */
  504. if (cpu_logical_map(0) == hwid) {
  505. if (bootcpu_valid) {
  506. pr_err("duplicate boot CPU MPIDR: 0x%llx in MADT\n",
  507. hwid);
  508. return;
  509. }
  510. bootcpu_valid = true;
  511. cpu_madt_gicc[0] = *processor;
  512. return;
  513. }
  514. if (cpu_count >= NR_CPUS)
  515. return;
  516. /* map the logical cpu id to cpu MPIDR */
  517. set_cpu_logical_map(cpu_count, hwid);
  518. cpu_madt_gicc[cpu_count] = *processor;
  519. /*
  520. * Set-up the ACPI parking protocol cpu entries
  521. * while initializing the cpu_logical_map to
  522. * avoid parsing MADT entries multiple times for
  523. * nothing (ie a valid cpu_logical_map entry should
  524. * contain a valid parking protocol data set to
  525. * initialize the cpu if the parking protocol is
  526. * the only available enable method).
  527. */
  528. acpi_set_mailbox_entry(cpu_count, processor);
  529. cpu_count++;
  530. }
  531. static int __init
  532. acpi_parse_gic_cpu_interface(union acpi_subtable_headers *header,
  533. const unsigned long end)
  534. {
  535. struct acpi_madt_generic_interrupt *processor;
  536. processor = (struct acpi_madt_generic_interrupt *)header;
  537. if (BAD_MADT_GICC_ENTRY(processor, end))
  538. return -EINVAL;
  539. acpi_table_print_madt_entry(&header->common);
  540. acpi_map_gic_cpu_interface(processor);
  541. return 0;
  542. }
  543. static void __init acpi_parse_and_init_cpus(void)
  544. {
  545. int i;
  546. /*
  547. * do a walk of MADT to determine how many CPUs
  548. * we have including disabled CPUs, and get information
  549. * we need for SMP init.
  550. */
  551. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  552. acpi_parse_gic_cpu_interface, 0);
  553. /*
  554. * In ACPI, SMP and CPU NUMA information is provided in separate
  555. * static tables, namely the MADT and the SRAT.
  556. *
  557. * Thus, it is simpler to first create the cpu logical map through
  558. * an MADT walk and then map the logical cpus to their node ids
  559. * as separate steps.
  560. */
  561. acpi_map_cpus_to_nodes();
  562. for (i = 0; i < nr_cpu_ids; i++)
  563. early_map_cpu_to_node(i, acpi_numa_get_nid(i));
  564. }
  565. #else
  566. #define acpi_parse_and_init_cpus(...) do { } while (0)
  567. #endif
  568. /*
  569. * Enumerate the possible CPU set from the device tree and build the
  570. * cpu logical map array containing MPIDR values related to logical
  571. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  572. */
  573. static void __init of_parse_and_init_cpus(void)
  574. {
  575. struct device_node *dn;
  576. for_each_of_cpu_node(dn) {
  577. u64 hwid = of_get_cpu_hwid(dn, 0);
  578. if (hwid & ~MPIDR_HWID_BITMASK)
  579. goto next;
  580. if (is_mpidr_duplicate(cpu_count, hwid)) {
  581. pr_err("%pOF: duplicate cpu reg properties in the DT\n",
  582. dn);
  583. goto next;
  584. }
  585. /*
  586. * The numbering scheme requires that the boot CPU
  587. * must be assigned logical id 0. Record it so that
  588. * the logical map built from DT is validated and can
  589. * be used.
  590. */
  591. if (hwid == cpu_logical_map(0)) {
  592. if (bootcpu_valid) {
  593. pr_err("%pOF: duplicate boot cpu reg property in DT\n",
  594. dn);
  595. goto next;
  596. }
  597. bootcpu_valid = true;
  598. early_map_cpu_to_node(0, of_node_to_nid(dn));
  599. /*
  600. * cpu_logical_map has already been
  601. * initialized and the boot cpu doesn't need
  602. * the enable-method so continue without
  603. * incrementing cpu.
  604. */
  605. continue;
  606. }
  607. if (cpu_count >= NR_CPUS)
  608. goto next;
  609. pr_debug("cpu logical map 0x%llx\n", hwid);
  610. set_cpu_logical_map(cpu_count, hwid);
  611. early_map_cpu_to_node(cpu_count, of_node_to_nid(dn));
  612. next:
  613. cpu_count++;
  614. }
  615. }
  616. /*
  617. * Enumerate the possible CPU set from the device tree or ACPI and build the
  618. * cpu logical map array containing MPIDR values related to logical
  619. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  620. */
  621. void __init smp_init_cpus(void)
  622. {
  623. int i;
  624. if (acpi_disabled)
  625. of_parse_and_init_cpus();
  626. else
  627. acpi_parse_and_init_cpus();
  628. if (cpu_count > nr_cpu_ids)
  629. pr_warn("Number of cores (%d) exceeds configured maximum of %u - clipping\n",
  630. cpu_count, nr_cpu_ids);
  631. if (!bootcpu_valid) {
  632. pr_err("missing boot CPU MPIDR, not enabling secondaries\n");
  633. return;
  634. }
  635. /*
  636. * We need to set the cpu_logical_map entries before enabling
  637. * the cpus so that cpu processor description entries (DT cpu nodes
  638. * and ACPI MADT entries) can be retrieved by matching the cpu hwid
  639. * with entries in cpu_logical_map while initializing the cpus.
  640. * If the cpu set-up fails, invalidate the cpu_logical_map entry.
  641. */
  642. for (i = 1; i < nr_cpu_ids; i++) {
  643. if (cpu_logical_map(i) != INVALID_HWID) {
  644. if (smp_cpu_setup(i))
  645. set_cpu_logical_map(i, INVALID_HWID);
  646. }
  647. }
  648. }
  649. void __init smp_prepare_cpus(unsigned int max_cpus)
  650. {
  651. const struct cpu_operations *ops;
  652. int err;
  653. unsigned int cpu;
  654. unsigned int this_cpu;
  655. init_cpu_topology();
  656. this_cpu = smp_processor_id();
  657. store_cpu_topology(this_cpu);
  658. numa_store_cpu_info(this_cpu);
  659. numa_add_cpu(this_cpu);
  660. /*
  661. * If UP is mandated by "nosmp" (which implies "maxcpus=0"), don't set
  662. * secondary CPUs present.
  663. */
  664. if (max_cpus == 0)
  665. return;
  666. /*
  667. * Initialise the present map (which describes the set of CPUs
  668. * actually populated at the present time) and release the
  669. * secondaries from the bootloader.
  670. */
  671. for_each_possible_cpu(cpu) {
  672. if (cpu == smp_processor_id())
  673. continue;
  674. ops = get_cpu_ops(cpu);
  675. if (!ops)
  676. continue;
  677. err = ops->cpu_prepare(cpu);
  678. if (err)
  679. continue;
  680. set_cpu_present(cpu, true);
  681. numa_store_cpu_info(cpu);
  682. }
  683. }
  684. static const char *ipi_types[MAX_IPI] __tracepoint_string = {
  685. [IPI_RESCHEDULE] = "Rescheduling interrupts",
  686. [IPI_CALL_FUNC] = "Function call interrupts",
  687. [IPI_CPU_STOP] = "CPU stop interrupts",
  688. [IPI_CPU_STOP_NMI] = "CPU stop NMIs",
  689. [IPI_TIMER] = "Timer broadcast interrupts",
  690. [IPI_IRQ_WORK] = "IRQ work interrupts",
  691. [IPI_CPU_BACKTRACE] = "CPU backtrace interrupts",
  692. [IPI_KGDB_ROUNDUP] = "KGDB roundup interrupts",
  693. };
  694. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr);
  695. unsigned long irq_err_count;
  696. int arch_show_interrupts(struct seq_file *p, int prec)
  697. {
  698. unsigned int cpu, i;
  699. for (i = 0; i < MAX_IPI; i++) {
  700. seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
  701. prec >= 4 ? " " : "");
  702. for_each_online_cpu(cpu)
  703. seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i], cpu));
  704. seq_printf(p, " %s\n", ipi_types[i]);
  705. }
  706. seq_printf(p, "%*s: %10lu\n", prec, "Err", irq_err_count);
  707. return 0;
  708. }
  709. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  710. {
  711. smp_cross_call(mask, IPI_CALL_FUNC);
  712. }
  713. void arch_send_call_function_single_ipi(int cpu)
  714. {
  715. smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC);
  716. }
  717. #ifdef CONFIG_IRQ_WORK
  718. void arch_irq_work_raise(void)
  719. {
  720. smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
  721. }
  722. #endif
  723. static void __noreturn local_cpu_stop(unsigned int cpu)
  724. {
  725. set_cpu_online(cpu, false);
  726. local_daif_mask();
  727. sdei_mask_local_cpu();
  728. cpu_park_loop();
  729. }
  730. /*
  731. * We need to implement panic_smp_self_stop() for parallel panic() calls, so
  732. * that cpu_online_mask gets correctly updated and smp_send_stop() can skip
  733. * CPUs that have already stopped themselves.
  734. */
  735. void __noreturn panic_smp_self_stop(void)
  736. {
  737. local_cpu_stop(smp_processor_id());
  738. }
  739. static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs)
  740. {
  741. #ifdef CONFIG_KEXEC_CORE
  742. /*
  743. * Use local_daif_mask() instead of local_irq_disable() to make sure
  744. * that pseudo-NMIs are disabled. The "crash stop" code starts with
  745. * an IRQ and falls back to NMI (which might be pseudo). If the IRQ
  746. * finally goes through right as we're timing out then the NMI could
  747. * interrupt us. It's better to prevent the NMI and let the IRQ
  748. * finish since the pt_regs will be better.
  749. */
  750. local_daif_mask();
  751. crash_save_cpu(regs, cpu);
  752. set_cpu_online(cpu, false);
  753. sdei_mask_local_cpu();
  754. if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
  755. __cpu_try_die(cpu);
  756. /* just in case */
  757. cpu_park_loop();
  758. #else
  759. BUG();
  760. #endif
  761. }
  762. static void arm64_backtrace_ipi(cpumask_t *mask)
  763. {
  764. __ipi_send_mask(ipi_desc[IPI_CPU_BACKTRACE], mask);
  765. }
  766. void arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu)
  767. {
  768. /*
  769. * NOTE: though nmi_trigger_cpumask_backtrace() has "nmi_" in the name,
  770. * nothing about it truly needs to be implemented using an NMI, it's
  771. * just that it's _allowed_ to work with NMIs. If ipi_should_be_nmi()
  772. * returned false our backtrace attempt will just use a regular IPI.
  773. */
  774. nmi_trigger_cpumask_backtrace(mask, exclude_cpu, arm64_backtrace_ipi);
  775. }
  776. #ifdef CONFIG_KGDB
  777. void kgdb_roundup_cpus(void)
  778. {
  779. int this_cpu = raw_smp_processor_id();
  780. int cpu;
  781. for_each_online_cpu(cpu) {
  782. /* No need to roundup ourselves */
  783. if (cpu == this_cpu)
  784. continue;
  785. __ipi_send_single(ipi_desc[IPI_KGDB_ROUNDUP], cpu);
  786. }
  787. }
  788. #endif
  789. /*
  790. * Main handler for inter-processor interrupts
  791. */
  792. static void do_handle_IPI(int ipinr)
  793. {
  794. unsigned int cpu = smp_processor_id();
  795. if ((unsigned)ipinr < NR_IPI)
  796. trace_ipi_entry(ipi_types[ipinr]);
  797. switch (ipinr) {
  798. case IPI_RESCHEDULE:
  799. scheduler_ipi();
  800. break;
  801. case IPI_CALL_FUNC:
  802. generic_smp_call_function_interrupt();
  803. break;
  804. case IPI_CPU_STOP:
  805. case IPI_CPU_STOP_NMI:
  806. if (IS_ENABLED(CONFIG_KEXEC_CORE) && crash_stop) {
  807. ipi_cpu_crash_stop(cpu, get_irq_regs());
  808. unreachable();
  809. } else {
  810. local_cpu_stop(cpu);
  811. }
  812. break;
  813. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  814. case IPI_TIMER:
  815. tick_receive_broadcast();
  816. break;
  817. #endif
  818. #ifdef CONFIG_IRQ_WORK
  819. case IPI_IRQ_WORK:
  820. irq_work_run();
  821. break;
  822. #endif
  823. case IPI_CPU_BACKTRACE:
  824. /*
  825. * NOTE: in some cases this _won't_ be NMI context. See the
  826. * comment in arch_trigger_cpumask_backtrace().
  827. */
  828. nmi_cpu_backtrace(get_irq_regs());
  829. break;
  830. case IPI_KGDB_ROUNDUP:
  831. kgdb_nmicallback(cpu, get_irq_regs());
  832. break;
  833. default:
  834. pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
  835. break;
  836. }
  837. if ((unsigned)ipinr < NR_IPI)
  838. trace_ipi_exit(ipi_types[ipinr]);
  839. }
  840. static irqreturn_t ipi_handler(int irq, void *data)
  841. {
  842. do_handle_IPI(irq - ipi_irq_base);
  843. return IRQ_HANDLED;
  844. }
  845. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
  846. {
  847. trace_ipi_raise(target, ipi_types[ipinr]);
  848. __ipi_send_mask(ipi_desc[ipinr], target);
  849. }
  850. static bool ipi_should_be_nmi(enum ipi_msg_type ipi)
  851. {
  852. if (!system_uses_irq_prio_masking())
  853. return false;
  854. switch (ipi) {
  855. case IPI_CPU_STOP_NMI:
  856. case IPI_CPU_BACKTRACE:
  857. case IPI_KGDB_ROUNDUP:
  858. return true;
  859. default:
  860. return false;
  861. }
  862. }
  863. static void ipi_setup(int cpu)
  864. {
  865. int i;
  866. if (WARN_ON_ONCE(!ipi_irq_base))
  867. return;
  868. for (i = 0; i < nr_ipi; i++) {
  869. if (ipi_should_be_nmi(i)) {
  870. prepare_percpu_nmi(ipi_irq_base + i);
  871. enable_percpu_nmi(ipi_irq_base + i, 0);
  872. } else {
  873. enable_percpu_irq(ipi_irq_base + i, 0);
  874. }
  875. }
  876. }
  877. #ifdef CONFIG_HOTPLUG_CPU
  878. static void ipi_teardown(int cpu)
  879. {
  880. int i;
  881. if (WARN_ON_ONCE(!ipi_irq_base))
  882. return;
  883. for (i = 0; i < nr_ipi; i++) {
  884. if (ipi_should_be_nmi(i)) {
  885. disable_percpu_nmi(ipi_irq_base + i);
  886. teardown_percpu_nmi(ipi_irq_base + i);
  887. } else {
  888. disable_percpu_irq(ipi_irq_base + i);
  889. }
  890. }
  891. }
  892. #endif
  893. void __init set_smp_ipi_range(int ipi_base, int n)
  894. {
  895. int i;
  896. WARN_ON(n < MAX_IPI);
  897. nr_ipi = min(n, MAX_IPI);
  898. for (i = 0; i < nr_ipi; i++) {
  899. int err;
  900. if (ipi_should_be_nmi(i)) {
  901. err = request_percpu_nmi(ipi_base + i, ipi_handler,
  902. "IPI", &irq_stat);
  903. WARN(err, "Could not request IPI %d as NMI, err=%d\n",
  904. i, err);
  905. } else {
  906. err = request_percpu_irq(ipi_base + i, ipi_handler,
  907. "IPI", &irq_stat);
  908. WARN(err, "Could not request IPI %d as IRQ, err=%d\n",
  909. i, err);
  910. }
  911. ipi_desc[i] = irq_to_desc(ipi_base + i);
  912. irq_set_status_flags(ipi_base + i, IRQ_HIDDEN);
  913. }
  914. ipi_irq_base = ipi_base;
  915. /* Setup the boot CPU immediately */
  916. ipi_setup(smp_processor_id());
  917. }
  918. void arch_smp_send_reschedule(int cpu)
  919. {
  920. smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
  921. }
  922. #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL
  923. void arch_send_wakeup_ipi(unsigned int cpu)
  924. {
  925. /*
  926. * We use a scheduler IPI to wake the CPU as this avoids the need for a
  927. * dedicated IPI and we can safely handle spurious scheduler IPIs.
  928. */
  929. smp_send_reschedule(cpu);
  930. }
  931. #endif
  932. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  933. void tick_broadcast(const struct cpumask *mask)
  934. {
  935. smp_cross_call(mask, IPI_TIMER);
  936. }
  937. #endif
  938. /*
  939. * The number of CPUs online, not counting this CPU (which may not be
  940. * fully online and so not counted in num_online_cpus()).
  941. */
  942. static inline unsigned int num_other_online_cpus(void)
  943. {
  944. unsigned int this_cpu_online = cpu_online(smp_processor_id());
  945. return num_online_cpus() - this_cpu_online;
  946. }
  947. void smp_send_stop(void)
  948. {
  949. static unsigned long stop_in_progress;
  950. cpumask_t mask;
  951. unsigned long timeout;
  952. /*
  953. * If this cpu is the only one alive at this point in time, online or
  954. * not, there are no stop messages to be sent around, so just back out.
  955. */
  956. if (num_other_online_cpus() == 0)
  957. goto skip_ipi;
  958. /* Only proceed if this is the first CPU to reach this code */
  959. if (test_and_set_bit(0, &stop_in_progress))
  960. return;
  961. /*
  962. * Send an IPI to all currently online CPUs except the CPU running
  963. * this code.
  964. *
  965. * NOTE: we don't do anything here to prevent other CPUs from coming
  966. * online after we snapshot `cpu_online_mask`. Ideally, the calling code
  967. * should do something to prevent other CPUs from coming up. This code
  968. * can be called in the panic path and thus it doesn't seem wise to
  969. * grab the CPU hotplug mutex ourselves. Worst case:
  970. * - If a CPU comes online as we're running, we'll likely notice it
  971. * during the 1 second wait below and then we'll catch it when we try
  972. * with an NMI (assuming NMIs are enabled) since we re-snapshot the
  973. * mask before sending an NMI.
  974. * - If we leave the function and see that CPUs are still online we'll
  975. * at least print a warning. Especially without NMIs this function
  976. * isn't foolproof anyway so calling code will just have to accept
  977. * the fact that there could be cases where a CPU can't be stopped.
  978. */
  979. cpumask_copy(&mask, cpu_online_mask);
  980. cpumask_clear_cpu(smp_processor_id(), &mask);
  981. if (system_state <= SYSTEM_RUNNING)
  982. pr_crit("SMP: stopping secondary CPUs\n");
  983. /*
  984. * Start with a normal IPI and wait up to one second for other CPUs to
  985. * stop. We do this first because it gives other processors a chance
  986. * to exit critical sections / drop locks and makes the rest of the
  987. * stop process (especially console flush) more robust.
  988. */
  989. smp_cross_call(&mask, IPI_CPU_STOP);
  990. timeout = USEC_PER_SEC;
  991. while (num_other_online_cpus() && timeout--)
  992. udelay(1);
  993. /*
  994. * If CPUs are still online, try an NMI. There's no excuse for this to
  995. * be slow, so we only give them an extra 10 ms to respond.
  996. */
  997. if (num_other_online_cpus() && ipi_should_be_nmi(IPI_CPU_STOP_NMI)) {
  998. smp_rmb();
  999. cpumask_copy(&mask, cpu_online_mask);
  1000. cpumask_clear_cpu(smp_processor_id(), &mask);
  1001. pr_info("SMP: retry stop with NMI for CPUs %*pbl\n",
  1002. cpumask_pr_args(&mask));
  1003. smp_cross_call(&mask, IPI_CPU_STOP_NMI);
  1004. timeout = USEC_PER_MSEC * 10;
  1005. while (num_other_online_cpus() && timeout--)
  1006. udelay(1);
  1007. }
  1008. if (num_other_online_cpus()) {
  1009. smp_rmb();
  1010. cpumask_copy(&mask, cpu_online_mask);
  1011. cpumask_clear_cpu(smp_processor_id(), &mask);
  1012. pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
  1013. cpumask_pr_args(&mask));
  1014. }
  1015. skip_ipi:
  1016. sdei_mask_local_cpu();
  1017. }
  1018. #ifdef CONFIG_KEXEC_CORE
  1019. void crash_smp_send_stop(void)
  1020. {
  1021. /*
  1022. * This function can be called twice in panic path, but obviously
  1023. * we execute this only once.
  1024. *
  1025. * We use this same boolean to tell whether the IPI we send was a
  1026. * stop or a "crash stop".
  1027. */
  1028. if (crash_stop)
  1029. return;
  1030. crash_stop = 1;
  1031. smp_send_stop();
  1032. sdei_handler_abort();
  1033. }
  1034. bool smp_crash_stop_failed(void)
  1035. {
  1036. return num_other_online_cpus() != 0;
  1037. }
  1038. #endif
  1039. static bool have_cpu_die(void)
  1040. {
  1041. #ifdef CONFIG_HOTPLUG_CPU
  1042. int any_cpu = raw_smp_processor_id();
  1043. const struct cpu_operations *ops = get_cpu_ops(any_cpu);
  1044. if (ops && ops->cpu_die)
  1045. return true;
  1046. #endif
  1047. return false;
  1048. }
  1049. bool cpus_are_stuck_in_kernel(void)
  1050. {
  1051. bool smp_spin_tables = (num_possible_cpus() > 1 && !have_cpu_die());
  1052. return !!cpus_stuck_in_kernel || smp_spin_tables ||
  1053. is_protected_kvm_enabled();
  1054. }