smp.c 23 KB

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