smp.c 21 KB

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