smp.c 21 KB

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