smp.c 23 KB

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