smp.c 22 KB

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