smp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. numa_add_cpu(cpu);
  200. /*
  201. * OK, now it's safe to let the boot CPU continue. Wait for
  202. * the CPU migration code to notice that the CPU is online
  203. * before we continue.
  204. */
  205. pr_info("CPU%u: Booted secondary processor 0x%010lx [0x%08x]\n",
  206. cpu, (unsigned long)mpidr,
  207. read_cpuid_id());
  208. update_cpu_boot_status(CPU_BOOT_SUCCESS);
  209. set_cpu_online(cpu, true);
  210. complete(&cpu_running);
  211. local_daif_restore(DAIF_PROCCTX);
  212. /*
  213. * OK, it's off to the idle thread for us
  214. */
  215. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  216. }
  217. #ifdef CONFIG_HOTPLUG_CPU
  218. static int op_cpu_disable(unsigned int cpu)
  219. {
  220. /*
  221. * If we don't have a cpu_die method, abort before we reach the point
  222. * of no return. CPU0 may not have an cpu_ops, so test for it.
  223. */
  224. if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_die)
  225. return -EOPNOTSUPP;
  226. /*
  227. * We may need to abort a hot unplug for some other mechanism-specific
  228. * reason.
  229. */
  230. if (cpu_ops[cpu]->cpu_disable)
  231. return cpu_ops[cpu]->cpu_disable(cpu);
  232. return 0;
  233. }
  234. /*
  235. * __cpu_disable runs on the processor to be shutdown.
  236. */
  237. int __cpu_disable(void)
  238. {
  239. unsigned int cpu = smp_processor_id();
  240. int ret;
  241. ret = op_cpu_disable(cpu);
  242. if (ret)
  243. return ret;
  244. remove_cpu_topology(cpu);
  245. numa_remove_cpu(cpu);
  246. /*
  247. * Take this CPU offline. Once we clear this, we can't return,
  248. * and we must not schedule until we're ready to give up the cpu.
  249. */
  250. set_cpu_online(cpu, false);
  251. /*
  252. * OK - migrate IRQs away from this CPU
  253. */
  254. irq_migrate_all_off_this_cpu();
  255. return 0;
  256. }
  257. static int op_cpu_kill(unsigned int cpu)
  258. {
  259. /*
  260. * If we have no means of synchronising with the dying CPU, then assume
  261. * that it is really dead. We can only wait for an arbitrary length of
  262. * time and hope that it's dead, so let's skip the wait and just hope.
  263. */
  264. if (!cpu_ops[cpu]->cpu_kill)
  265. return 0;
  266. return cpu_ops[cpu]->cpu_kill(cpu);
  267. }
  268. /*
  269. * called on the thread which is asking for a CPU to be shutdown -
  270. * waits until shutdown has completed, or it is timed out.
  271. */
  272. void __cpu_die(unsigned int cpu)
  273. {
  274. int err;
  275. if (!cpu_wait_death(cpu, 5)) {
  276. pr_crit("CPU%u: cpu didn't die\n", cpu);
  277. return;
  278. }
  279. pr_notice("CPU%u: shutdown\n", cpu);
  280. /*
  281. * Now that the dying CPU is beyond the point of no return w.r.t.
  282. * in-kernel synchronisation, try to get the firwmare to help us to
  283. * verify that it has really left the kernel before we consider
  284. * clobbering anything it might still be using.
  285. */
  286. err = op_cpu_kill(cpu);
  287. if (err)
  288. pr_warn("CPU%d may not have shut down cleanly: %d\n",
  289. cpu, err);
  290. }
  291. /*
  292. * Called from the idle thread for the CPU which has been shutdown.
  293. *
  294. */
  295. void cpu_die(void)
  296. {
  297. unsigned int cpu = smp_processor_id();
  298. idle_task_exit();
  299. local_daif_mask();
  300. /* Tell __cpu_die() that this CPU is now safe to dispose of */
  301. (void)cpu_report_death();
  302. /*
  303. * Actually shutdown the CPU. This must never fail. The specific hotplug
  304. * mechanism must perform all required cache maintenance to ensure that
  305. * no dirty lines are lost in the process of shutting down the CPU.
  306. */
  307. cpu_ops[cpu]->cpu_die(cpu);
  308. BUG();
  309. }
  310. #endif
  311. /*
  312. * Kill the calling secondary CPU, early in bringup before it is turned
  313. * online.
  314. */
  315. void cpu_die_early(void)
  316. {
  317. int cpu = smp_processor_id();
  318. pr_crit("CPU%d: will not boot\n", cpu);
  319. /* Mark this CPU absent */
  320. set_cpu_present(cpu, 0);
  321. #ifdef CONFIG_HOTPLUG_CPU
  322. update_cpu_boot_status(CPU_KILL_ME);
  323. /* Check if we can park ourselves */
  324. if (cpu_ops[cpu] && cpu_ops[cpu]->cpu_die)
  325. cpu_ops[cpu]->cpu_die(cpu);
  326. #endif
  327. update_cpu_boot_status(CPU_STUCK_IN_KERNEL);
  328. cpu_park_loop();
  329. }
  330. static void __init hyp_mode_check(void)
  331. {
  332. if (is_hyp_mode_available())
  333. pr_info("CPU: All CPU(s) started at EL2\n");
  334. else if (is_hyp_mode_mismatched())
  335. WARN_TAINT(1, TAINT_CPU_OUT_OF_SPEC,
  336. "CPU: CPUs started in inconsistent modes");
  337. else
  338. pr_info("CPU: All CPU(s) started at EL1\n");
  339. }
  340. void __init smp_cpus_done(unsigned int max_cpus)
  341. {
  342. pr_info("SMP: Total of %d processors activated.\n", num_online_cpus());
  343. setup_cpu_features();
  344. hyp_mode_check();
  345. apply_alternatives_all();
  346. mark_linear_text_alias_ro();
  347. }
  348. void __init smp_prepare_boot_cpu(void)
  349. {
  350. set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
  351. /*
  352. * Initialise the static keys early as they may be enabled by the
  353. * cpufeature code.
  354. */
  355. jump_label_init();
  356. cpuinfo_store_boot_cpu();
  357. }
  358. static u64 __init of_get_cpu_mpidr(struct device_node *dn)
  359. {
  360. const __be32 *cell;
  361. u64 hwid;
  362. /*
  363. * A cpu node with missing "reg" property is
  364. * considered invalid to build a cpu_logical_map
  365. * entry.
  366. */
  367. cell = of_get_property(dn, "reg", NULL);
  368. if (!cell) {
  369. pr_err("%pOF: missing reg property\n", dn);
  370. return INVALID_HWID;
  371. }
  372. hwid = of_read_number(cell, of_n_addr_cells(dn));
  373. /*
  374. * Non affinity bits must be set to 0 in the DT
  375. */
  376. if (hwid & ~MPIDR_HWID_BITMASK) {
  377. pr_err("%pOF: invalid reg property\n", dn);
  378. return INVALID_HWID;
  379. }
  380. return hwid;
  381. }
  382. /*
  383. * Duplicate MPIDRs are a recipe for disaster. Scan all initialized
  384. * entries and check for duplicates. If any is found just ignore the
  385. * cpu. cpu_logical_map was initialized to INVALID_HWID to avoid
  386. * matching valid MPIDR values.
  387. */
  388. static bool __init is_mpidr_duplicate(unsigned int cpu, u64 hwid)
  389. {
  390. unsigned int i;
  391. for (i = 1; (i < cpu) && (i < NR_CPUS); i++)
  392. if (cpu_logical_map(i) == hwid)
  393. return true;
  394. return false;
  395. }
  396. /*
  397. * Initialize cpu operations for a logical cpu and
  398. * set it in the possible mask on success
  399. */
  400. static int __init smp_cpu_setup(int cpu)
  401. {
  402. if (cpu_read_ops(cpu))
  403. return -ENODEV;
  404. if (cpu_ops[cpu]->cpu_init(cpu))
  405. return -ENODEV;
  406. set_cpu_possible(cpu, true);
  407. return 0;
  408. }
  409. static bool bootcpu_valid __initdata;
  410. static unsigned int cpu_count = 1;
  411. #ifdef CONFIG_ACPI
  412. static struct acpi_madt_generic_interrupt cpu_madt_gicc[NR_CPUS];
  413. struct acpi_madt_generic_interrupt *acpi_cpu_get_madt_gicc(int cpu)
  414. {
  415. return &cpu_madt_gicc[cpu];
  416. }
  417. /*
  418. * acpi_map_gic_cpu_interface - parse processor MADT entry
  419. *
  420. * Carry out sanity checks on MADT processor entry and initialize
  421. * cpu_logical_map on success
  422. */
  423. static void __init
  424. acpi_map_gic_cpu_interface(struct acpi_madt_generic_interrupt *processor)
  425. {
  426. u64 hwid = processor->arm_mpidr;
  427. if (!(processor->flags & ACPI_MADT_ENABLED)) {
  428. pr_debug("skipping disabled CPU entry with 0x%llx MPIDR\n", hwid);
  429. return;
  430. }
  431. if (hwid & ~MPIDR_HWID_BITMASK || hwid == INVALID_HWID) {
  432. pr_err("skipping CPU entry with invalid MPIDR 0x%llx\n", hwid);
  433. return;
  434. }
  435. if (is_mpidr_duplicate(cpu_count, hwid)) {
  436. pr_err("duplicate CPU MPIDR 0x%llx in MADT\n", hwid);
  437. return;
  438. }
  439. /* Check if GICC structure of boot CPU is available in the MADT */
  440. if (cpu_logical_map(0) == hwid) {
  441. if (bootcpu_valid) {
  442. pr_err("duplicate boot CPU MPIDR: 0x%llx in MADT\n",
  443. hwid);
  444. return;
  445. }
  446. bootcpu_valid = true;
  447. cpu_madt_gicc[0] = *processor;
  448. early_map_cpu_to_node(0, acpi_numa_get_nid(0, hwid));
  449. return;
  450. }
  451. if (cpu_count >= NR_CPUS)
  452. return;
  453. /* map the logical cpu id to cpu MPIDR */
  454. cpu_logical_map(cpu_count) = hwid;
  455. cpu_madt_gicc[cpu_count] = *processor;
  456. /*
  457. * Set-up the ACPI parking protocol cpu entries
  458. * while initializing the cpu_logical_map to
  459. * avoid parsing MADT entries multiple times for
  460. * nothing (ie a valid cpu_logical_map entry should
  461. * contain a valid parking protocol data set to
  462. * initialize the cpu if the parking protocol is
  463. * the only available enable method).
  464. */
  465. acpi_set_mailbox_entry(cpu_count, processor);
  466. early_map_cpu_to_node(cpu_count, acpi_numa_get_nid(cpu_count, hwid));
  467. cpu_count++;
  468. }
  469. static int __init
  470. acpi_parse_gic_cpu_interface(struct acpi_subtable_header *header,
  471. const unsigned long end)
  472. {
  473. struct acpi_madt_generic_interrupt *processor;
  474. processor = (struct acpi_madt_generic_interrupt *)header;
  475. if (BAD_MADT_GICC_ENTRY(processor, end))
  476. return -EINVAL;
  477. acpi_table_print_madt_entry(header);
  478. acpi_map_gic_cpu_interface(processor);
  479. return 0;
  480. }
  481. #else
  482. #define acpi_table_parse_madt(...) do { } while (0)
  483. #endif
  484. /*
  485. * Enumerate the possible CPU set from the device tree and build the
  486. * cpu logical map array containing MPIDR values related to logical
  487. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  488. */
  489. static void __init of_parse_and_init_cpus(void)
  490. {
  491. struct device_node *dn;
  492. for_each_node_by_type(dn, "cpu") {
  493. u64 hwid = of_get_cpu_mpidr(dn);
  494. if (hwid == INVALID_HWID)
  495. goto next;
  496. if (is_mpidr_duplicate(cpu_count, hwid)) {
  497. pr_err("%pOF: duplicate cpu reg properties in the DT\n",
  498. dn);
  499. goto next;
  500. }
  501. /*
  502. * The numbering scheme requires that the boot CPU
  503. * must be assigned logical id 0. Record it so that
  504. * the logical map built from DT is validated and can
  505. * be used.
  506. */
  507. if (hwid == cpu_logical_map(0)) {
  508. if (bootcpu_valid) {
  509. pr_err("%pOF: duplicate boot cpu reg property in DT\n",
  510. dn);
  511. goto next;
  512. }
  513. bootcpu_valid = true;
  514. early_map_cpu_to_node(0, of_node_to_nid(dn));
  515. /*
  516. * cpu_logical_map has already been
  517. * initialized and the boot cpu doesn't need
  518. * the enable-method so continue without
  519. * incrementing cpu.
  520. */
  521. continue;
  522. }
  523. if (cpu_count >= NR_CPUS)
  524. goto next;
  525. pr_debug("cpu logical map 0x%llx\n", hwid);
  526. cpu_logical_map(cpu_count) = hwid;
  527. early_map_cpu_to_node(cpu_count, of_node_to_nid(dn));
  528. next:
  529. cpu_count++;
  530. }
  531. }
  532. /*
  533. * Enumerate the possible CPU set from the device tree or ACPI and build the
  534. * cpu logical map array containing MPIDR values related to logical
  535. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  536. */
  537. void __init smp_init_cpus(void)
  538. {
  539. int i;
  540. if (acpi_disabled)
  541. of_parse_and_init_cpus();
  542. else
  543. /*
  544. * do a walk of MADT to determine how many CPUs
  545. * we have including disabled CPUs, and get information
  546. * we need for SMP init
  547. */
  548. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  549. acpi_parse_gic_cpu_interface, 0);
  550. if (cpu_count > nr_cpu_ids)
  551. pr_warn("Number of cores (%d) exceeds configured maximum of %u - clipping\n",
  552. cpu_count, nr_cpu_ids);
  553. if (!bootcpu_valid) {
  554. pr_err("missing boot CPU MPIDR, not enabling secondaries\n");
  555. return;
  556. }
  557. /*
  558. * We need to set the cpu_logical_map entries before enabling
  559. * the cpus so that cpu processor description entries (DT cpu nodes
  560. * and ACPI MADT entries) can be retrieved by matching the cpu hwid
  561. * with entries in cpu_logical_map while initializing the cpus.
  562. * If the cpu set-up fails, invalidate the cpu_logical_map entry.
  563. */
  564. for (i = 1; i < nr_cpu_ids; i++) {
  565. if (cpu_logical_map(i) != INVALID_HWID) {
  566. if (smp_cpu_setup(i))
  567. cpu_logical_map(i) = INVALID_HWID;
  568. }
  569. }
  570. }
  571. void __init smp_prepare_cpus(unsigned int max_cpus)
  572. {
  573. int err;
  574. unsigned int cpu;
  575. unsigned int this_cpu;
  576. init_cpu_topology();
  577. this_cpu = smp_processor_id();
  578. store_cpu_topology(this_cpu);
  579. numa_store_cpu_info(this_cpu);
  580. numa_add_cpu(this_cpu);
  581. /*
  582. * If UP is mandated by "nosmp" (which implies "maxcpus=0"), don't set
  583. * secondary CPUs present.
  584. */
  585. if (max_cpus == 0)
  586. return;
  587. /*
  588. * Initialise the present map (which describes the set of CPUs
  589. * actually populated at the present time) and release the
  590. * secondaries from the bootloader.
  591. */
  592. for_each_possible_cpu(cpu) {
  593. per_cpu(cpu_number, cpu) = cpu;
  594. if (cpu == smp_processor_id())
  595. continue;
  596. if (!cpu_ops[cpu])
  597. continue;
  598. err = cpu_ops[cpu]->cpu_prepare(cpu);
  599. if (err)
  600. continue;
  601. set_cpu_present(cpu, true);
  602. numa_store_cpu_info(cpu);
  603. }
  604. }
  605. void (*__smp_cross_call)(const struct cpumask *, unsigned int);
  606. void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
  607. {
  608. __smp_cross_call = fn;
  609. }
  610. static const char *ipi_types[NR_IPI] __tracepoint_string = {
  611. #define S(x,s) [x] = s
  612. S(IPI_RESCHEDULE, "Rescheduling interrupts"),
  613. S(IPI_CALL_FUNC, "Function call interrupts"),
  614. S(IPI_CPU_STOP, "CPU stop interrupts"),
  615. S(IPI_CPU_CRASH_STOP, "CPU stop (for crash dump) interrupts"),
  616. S(IPI_TIMER, "Timer broadcast interrupts"),
  617. S(IPI_IRQ_WORK, "IRQ work interrupts"),
  618. S(IPI_WAKEUP, "CPU wake-up interrupts"),
  619. };
  620. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
  621. {
  622. trace_ipi_raise(target, ipi_types[ipinr]);
  623. __smp_cross_call(target, ipinr);
  624. }
  625. void show_ipi_list(struct seq_file *p, int prec)
  626. {
  627. unsigned int cpu, i;
  628. for (i = 0; i < NR_IPI; i++) {
  629. seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
  630. prec >= 4 ? " " : "");
  631. for_each_online_cpu(cpu)
  632. seq_printf(p, "%10u ",
  633. __get_irq_stat(cpu, ipi_irqs[i]));
  634. seq_printf(p, " %s\n", ipi_types[i]);
  635. }
  636. }
  637. u64 smp_irq_stat_cpu(unsigned int cpu)
  638. {
  639. u64 sum = 0;
  640. int i;
  641. for (i = 0; i < NR_IPI; i++)
  642. sum += __get_irq_stat(cpu, ipi_irqs[i]);
  643. return sum;
  644. }
  645. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  646. {
  647. smp_cross_call(mask, IPI_CALL_FUNC);
  648. }
  649. void arch_send_call_function_single_ipi(int cpu)
  650. {
  651. smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC);
  652. }
  653. #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL
  654. void arch_send_wakeup_ipi_mask(const struct cpumask *mask)
  655. {
  656. smp_cross_call(mask, IPI_WAKEUP);
  657. }
  658. #endif
  659. #ifdef CONFIG_IRQ_WORK
  660. void arch_irq_work_raise(void)
  661. {
  662. if (__smp_cross_call)
  663. smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
  664. }
  665. #endif
  666. /*
  667. * ipi_cpu_stop - handle IPI from smp_send_stop()
  668. */
  669. static void ipi_cpu_stop(unsigned int cpu)
  670. {
  671. set_cpu_online(cpu, false);
  672. local_daif_mask();
  673. sdei_mask_local_cpu();
  674. while (1)
  675. cpu_relax();
  676. }
  677. #ifdef CONFIG_KEXEC_CORE
  678. static atomic_t waiting_for_crash_ipi = ATOMIC_INIT(0);
  679. #endif
  680. static void ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs)
  681. {
  682. #ifdef CONFIG_KEXEC_CORE
  683. crash_save_cpu(regs, cpu);
  684. atomic_dec(&waiting_for_crash_ipi);
  685. local_irq_disable();
  686. sdei_mask_local_cpu();
  687. #ifdef CONFIG_HOTPLUG_CPU
  688. if (cpu_ops[cpu]->cpu_die)
  689. cpu_ops[cpu]->cpu_die(cpu);
  690. #endif
  691. /* just in case */
  692. cpu_park_loop();
  693. #endif
  694. }
  695. /*
  696. * Main handler for inter-processor interrupts
  697. */
  698. void handle_IPI(int ipinr, struct pt_regs *regs)
  699. {
  700. unsigned int cpu = smp_processor_id();
  701. struct pt_regs *old_regs = set_irq_regs(regs);
  702. if ((unsigned)ipinr < NR_IPI) {
  703. trace_ipi_entry_rcuidle(ipi_types[ipinr]);
  704. __inc_irq_stat(cpu, ipi_irqs[ipinr]);
  705. }
  706. switch (ipinr) {
  707. case IPI_RESCHEDULE:
  708. scheduler_ipi();
  709. break;
  710. case IPI_CALL_FUNC:
  711. irq_enter();
  712. generic_smp_call_function_interrupt();
  713. irq_exit();
  714. break;
  715. case IPI_CPU_STOP:
  716. irq_enter();
  717. ipi_cpu_stop(cpu);
  718. irq_exit();
  719. break;
  720. case IPI_CPU_CRASH_STOP:
  721. if (IS_ENABLED(CONFIG_KEXEC_CORE)) {
  722. irq_enter();
  723. ipi_cpu_crash_stop(cpu, regs);
  724. unreachable();
  725. }
  726. break;
  727. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  728. case IPI_TIMER:
  729. irq_enter();
  730. tick_receive_broadcast();
  731. irq_exit();
  732. break;
  733. #endif
  734. #ifdef CONFIG_IRQ_WORK
  735. case IPI_IRQ_WORK:
  736. irq_enter();
  737. irq_work_run();
  738. irq_exit();
  739. break;
  740. #endif
  741. #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL
  742. case IPI_WAKEUP:
  743. WARN_ONCE(!acpi_parking_protocol_valid(cpu),
  744. "CPU%u: Wake-up IPI outside the ACPI parking protocol\n",
  745. cpu);
  746. break;
  747. #endif
  748. default:
  749. pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
  750. break;
  751. }
  752. if ((unsigned)ipinr < NR_IPI)
  753. trace_ipi_exit_rcuidle(ipi_types[ipinr]);
  754. set_irq_regs(old_regs);
  755. }
  756. void smp_send_reschedule(int cpu)
  757. {
  758. smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
  759. }
  760. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  761. void tick_broadcast(const struct cpumask *mask)
  762. {
  763. smp_cross_call(mask, IPI_TIMER);
  764. }
  765. #endif
  766. void smp_send_stop(void)
  767. {
  768. unsigned long timeout;
  769. if (num_online_cpus() > 1) {
  770. cpumask_t mask;
  771. cpumask_copy(&mask, cpu_online_mask);
  772. cpumask_clear_cpu(smp_processor_id(), &mask);
  773. if (system_state <= SYSTEM_RUNNING)
  774. pr_crit("SMP: stopping secondary CPUs\n");
  775. smp_cross_call(&mask, IPI_CPU_STOP);
  776. }
  777. /* Wait up to one second for other CPUs to stop */
  778. timeout = USEC_PER_SEC;
  779. while (num_online_cpus() > 1 && timeout--)
  780. udelay(1);
  781. if (num_online_cpus() > 1)
  782. pr_warning("SMP: failed to stop secondary CPUs %*pbl\n",
  783. cpumask_pr_args(cpu_online_mask));
  784. sdei_mask_local_cpu();
  785. }
  786. #ifdef CONFIG_KEXEC_CORE
  787. void crash_smp_send_stop(void)
  788. {
  789. static int cpus_stopped;
  790. cpumask_t mask;
  791. unsigned long timeout;
  792. /*
  793. * This function can be called twice in panic path, but obviously
  794. * we execute this only once.
  795. */
  796. if (cpus_stopped)
  797. return;
  798. cpus_stopped = 1;
  799. if (num_online_cpus() == 1) {
  800. sdei_mask_local_cpu();
  801. return;
  802. }
  803. cpumask_copy(&mask, cpu_online_mask);
  804. cpumask_clear_cpu(smp_processor_id(), &mask);
  805. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  806. pr_crit("SMP: stopping secondary CPUs\n");
  807. smp_cross_call(&mask, IPI_CPU_CRASH_STOP);
  808. /* Wait up to one second for other CPUs to stop */
  809. timeout = USEC_PER_SEC;
  810. while ((atomic_read(&waiting_for_crash_ipi) > 0) && timeout--)
  811. udelay(1);
  812. if (atomic_read(&waiting_for_crash_ipi) > 0)
  813. pr_warning("SMP: failed to stop secondary CPUs %*pbl\n",
  814. cpumask_pr_args(&mask));
  815. sdei_mask_local_cpu();
  816. }
  817. bool smp_crash_stop_failed(void)
  818. {
  819. return (atomic_read(&waiting_for_crash_ipi) > 0);
  820. }
  821. #endif
  822. /*
  823. * not supported here
  824. */
  825. int setup_profiling_timer(unsigned int multiplier)
  826. {
  827. return -EINVAL;
  828. }
  829. static bool have_cpu_die(void)
  830. {
  831. #ifdef CONFIG_HOTPLUG_CPU
  832. int any_cpu = raw_smp_processor_id();
  833. if (cpu_ops[any_cpu] && cpu_ops[any_cpu]->cpu_die)
  834. return true;
  835. #endif
  836. return false;
  837. }
  838. bool cpus_are_stuck_in_kernel(void)
  839. {
  840. bool smp_spin_tables = (num_possible_cpus() > 1 && !have_cpu_die());
  841. return !!cpus_stuck_in_kernel || smp_spin_tables;
  842. }