smp.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. return;
  449. }
  450. if (cpu_count >= NR_CPUS)
  451. return;
  452. /* map the logical cpu id to cpu MPIDR */
  453. cpu_logical_map(cpu_count) = hwid;
  454. cpu_madt_gicc[cpu_count] = *processor;
  455. /*
  456. * Set-up the ACPI parking protocol cpu entries
  457. * while initializing the cpu_logical_map to
  458. * avoid parsing MADT entries multiple times for
  459. * nothing (ie a valid cpu_logical_map entry should
  460. * contain a valid parking protocol data set to
  461. * initialize the cpu if the parking protocol is
  462. * the only available enable method).
  463. */
  464. acpi_set_mailbox_entry(cpu_count, processor);
  465. cpu_count++;
  466. }
  467. static int __init
  468. acpi_parse_gic_cpu_interface(struct acpi_subtable_header *header,
  469. const unsigned long end)
  470. {
  471. struct acpi_madt_generic_interrupt *processor;
  472. processor = (struct acpi_madt_generic_interrupt *)header;
  473. if (BAD_MADT_GICC_ENTRY(processor, end))
  474. return -EINVAL;
  475. acpi_table_print_madt_entry(header);
  476. acpi_map_gic_cpu_interface(processor);
  477. return 0;
  478. }
  479. static void __init acpi_parse_and_init_cpus(void)
  480. {
  481. int i;
  482. /*
  483. * do a walk of MADT to determine how many CPUs
  484. * we have including disabled CPUs, and get information
  485. * we need for SMP init.
  486. */
  487. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  488. acpi_parse_gic_cpu_interface, 0);
  489. /*
  490. * In ACPI, SMP and CPU NUMA information is provided in separate
  491. * static tables, namely the MADT and the SRAT.
  492. *
  493. * Thus, it is simpler to first create the cpu logical map through
  494. * an MADT walk and then map the logical cpus to their node ids
  495. * as separate steps.
  496. */
  497. acpi_map_cpus_to_nodes();
  498. for (i = 0; i < nr_cpu_ids; i++)
  499. early_map_cpu_to_node(i, acpi_numa_get_nid(i));
  500. }
  501. #else
  502. #define acpi_parse_and_init_cpus(...) do { } while (0)
  503. #endif
  504. /*
  505. * Enumerate the possible CPU set from the device tree and build the
  506. * cpu logical map array containing MPIDR values related to logical
  507. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  508. */
  509. static void __init of_parse_and_init_cpus(void)
  510. {
  511. struct device_node *dn;
  512. for_each_of_cpu_node(dn) {
  513. u64 hwid = of_get_cpu_mpidr(dn);
  514. if (hwid == INVALID_HWID)
  515. goto next;
  516. if (is_mpidr_duplicate(cpu_count, hwid)) {
  517. pr_err("%pOF: duplicate cpu reg properties in the DT\n",
  518. dn);
  519. goto next;
  520. }
  521. /*
  522. * The numbering scheme requires that the boot CPU
  523. * must be assigned logical id 0. Record it so that
  524. * the logical map built from DT is validated and can
  525. * be used.
  526. */
  527. if (hwid == cpu_logical_map(0)) {
  528. if (bootcpu_valid) {
  529. pr_err("%pOF: duplicate boot cpu reg property in DT\n",
  530. dn);
  531. goto next;
  532. }
  533. bootcpu_valid = true;
  534. early_map_cpu_to_node(0, of_node_to_nid(dn));
  535. /*
  536. * cpu_logical_map has already been
  537. * initialized and the boot cpu doesn't need
  538. * the enable-method so continue without
  539. * incrementing cpu.
  540. */
  541. continue;
  542. }
  543. if (cpu_count >= NR_CPUS)
  544. goto next;
  545. pr_debug("cpu logical map 0x%llx\n", hwid);
  546. cpu_logical_map(cpu_count) = hwid;
  547. early_map_cpu_to_node(cpu_count, of_node_to_nid(dn));
  548. next:
  549. cpu_count++;
  550. }
  551. }
  552. /*
  553. * Enumerate the possible CPU set from the device tree or ACPI and build the
  554. * cpu logical map array containing MPIDR values related to logical
  555. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  556. */
  557. void __init smp_init_cpus(void)
  558. {
  559. int i;
  560. if (acpi_disabled)
  561. of_parse_and_init_cpus();
  562. else
  563. acpi_parse_and_init_cpus();
  564. if (cpu_count > nr_cpu_ids)
  565. pr_warn("Number of cores (%d) exceeds configured maximum of %u - clipping\n",
  566. cpu_count, nr_cpu_ids);
  567. if (!bootcpu_valid) {
  568. pr_err("missing boot CPU MPIDR, not enabling secondaries\n");
  569. return;
  570. }
  571. /*
  572. * We need to set the cpu_logical_map entries before enabling
  573. * the cpus so that cpu processor description entries (DT cpu nodes
  574. * and ACPI MADT entries) can be retrieved by matching the cpu hwid
  575. * with entries in cpu_logical_map while initializing the cpus.
  576. * If the cpu set-up fails, invalidate the cpu_logical_map entry.
  577. */
  578. for (i = 1; i < nr_cpu_ids; i++) {
  579. if (cpu_logical_map(i) != INVALID_HWID) {
  580. if (smp_cpu_setup(i))
  581. cpu_logical_map(i) = INVALID_HWID;
  582. }
  583. }
  584. }
  585. void __init smp_prepare_cpus(unsigned int max_cpus)
  586. {
  587. int err;
  588. unsigned int cpu;
  589. unsigned int this_cpu;
  590. init_cpu_topology();
  591. this_cpu = smp_processor_id();
  592. store_cpu_topology(this_cpu);
  593. numa_store_cpu_info(this_cpu);
  594. numa_add_cpu(this_cpu);
  595. /*
  596. * If UP is mandated by "nosmp" (which implies "maxcpus=0"), don't set
  597. * secondary CPUs present.
  598. */
  599. if (max_cpus == 0)
  600. return;
  601. /*
  602. * Initialise the present map (which describes the set of CPUs
  603. * actually populated at the present time) and release the
  604. * secondaries from the bootloader.
  605. */
  606. for_each_possible_cpu(cpu) {
  607. per_cpu(cpu_number, cpu) = cpu;
  608. if (cpu == smp_processor_id())
  609. continue;
  610. if (!cpu_ops[cpu])
  611. continue;
  612. err = cpu_ops[cpu]->cpu_prepare(cpu);
  613. if (err)
  614. continue;
  615. set_cpu_present(cpu, true);
  616. numa_store_cpu_info(cpu);
  617. }
  618. }
  619. void (*__smp_cross_call)(const struct cpumask *, unsigned int);
  620. void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
  621. {
  622. __smp_cross_call = fn;
  623. }
  624. static const char *ipi_types[NR_IPI] __tracepoint_string = {
  625. #define S(x,s) [x] = s
  626. S(IPI_RESCHEDULE, "Rescheduling interrupts"),
  627. S(IPI_CALL_FUNC, "Function call interrupts"),
  628. S(IPI_CPU_STOP, "CPU stop interrupts"),
  629. S(IPI_CPU_CRASH_STOP, "CPU stop (for crash dump) interrupts"),
  630. S(IPI_TIMER, "Timer broadcast interrupts"),
  631. S(IPI_IRQ_WORK, "IRQ work interrupts"),
  632. S(IPI_WAKEUP, "CPU wake-up interrupts"),
  633. };
  634. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
  635. {
  636. trace_ipi_raise(target, ipi_types[ipinr]);
  637. __smp_cross_call(target, ipinr);
  638. }
  639. void show_ipi_list(struct seq_file *p, int prec)
  640. {
  641. unsigned int cpu, i;
  642. for (i = 0; i < NR_IPI; i++) {
  643. seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
  644. prec >= 4 ? " " : "");
  645. for_each_online_cpu(cpu)
  646. seq_printf(p, "%10u ",
  647. __get_irq_stat(cpu, ipi_irqs[i]));
  648. seq_printf(p, " %s\n", ipi_types[i]);
  649. }
  650. }
  651. u64 smp_irq_stat_cpu(unsigned int cpu)
  652. {
  653. u64 sum = 0;
  654. int i;
  655. for (i = 0; i < NR_IPI; i++)
  656. sum += __get_irq_stat(cpu, ipi_irqs[i]);
  657. return sum;
  658. }
  659. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  660. {
  661. smp_cross_call(mask, IPI_CALL_FUNC);
  662. }
  663. void arch_send_call_function_single_ipi(int cpu)
  664. {
  665. smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC);
  666. }
  667. #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL
  668. void arch_send_wakeup_ipi_mask(const struct cpumask *mask)
  669. {
  670. smp_cross_call(mask, IPI_WAKEUP);
  671. }
  672. #endif
  673. #ifdef CONFIG_IRQ_WORK
  674. void arch_irq_work_raise(void)
  675. {
  676. if (__smp_cross_call)
  677. smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
  678. }
  679. #endif
  680. /*
  681. * ipi_cpu_stop - handle IPI from smp_send_stop()
  682. */
  683. static void ipi_cpu_stop(unsigned int cpu)
  684. {
  685. set_cpu_online(cpu, false);
  686. local_daif_mask();
  687. sdei_mask_local_cpu();
  688. while (1)
  689. cpu_relax();
  690. }
  691. #ifdef CONFIG_KEXEC_CORE
  692. static atomic_t waiting_for_crash_ipi = ATOMIC_INIT(0);
  693. #endif
  694. static void ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs)
  695. {
  696. #ifdef CONFIG_KEXEC_CORE
  697. crash_save_cpu(regs, cpu);
  698. atomic_dec(&waiting_for_crash_ipi);
  699. local_irq_disable();
  700. sdei_mask_local_cpu();
  701. #ifdef CONFIG_HOTPLUG_CPU
  702. if (cpu_ops[cpu]->cpu_die)
  703. cpu_ops[cpu]->cpu_die(cpu);
  704. #endif
  705. /* just in case */
  706. cpu_park_loop();
  707. #endif
  708. }
  709. /*
  710. * Main handler for inter-processor interrupts
  711. */
  712. void handle_IPI(int ipinr, struct pt_regs *regs)
  713. {
  714. unsigned int cpu = smp_processor_id();
  715. struct pt_regs *old_regs = set_irq_regs(regs);
  716. if ((unsigned)ipinr < NR_IPI) {
  717. trace_ipi_entry_rcuidle(ipi_types[ipinr]);
  718. __inc_irq_stat(cpu, ipi_irqs[ipinr]);
  719. }
  720. switch (ipinr) {
  721. case IPI_RESCHEDULE:
  722. scheduler_ipi();
  723. break;
  724. case IPI_CALL_FUNC:
  725. irq_enter();
  726. generic_smp_call_function_interrupt();
  727. irq_exit();
  728. break;
  729. case IPI_CPU_STOP:
  730. irq_enter();
  731. ipi_cpu_stop(cpu);
  732. irq_exit();
  733. break;
  734. case IPI_CPU_CRASH_STOP:
  735. if (IS_ENABLED(CONFIG_KEXEC_CORE)) {
  736. irq_enter();
  737. ipi_cpu_crash_stop(cpu, regs);
  738. unreachable();
  739. }
  740. break;
  741. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  742. case IPI_TIMER:
  743. irq_enter();
  744. tick_receive_broadcast();
  745. irq_exit();
  746. break;
  747. #endif
  748. #ifdef CONFIG_IRQ_WORK
  749. case IPI_IRQ_WORK:
  750. irq_enter();
  751. irq_work_run();
  752. irq_exit();
  753. break;
  754. #endif
  755. #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL
  756. case IPI_WAKEUP:
  757. WARN_ONCE(!acpi_parking_protocol_valid(cpu),
  758. "CPU%u: Wake-up IPI outside the ACPI parking protocol\n",
  759. cpu);
  760. break;
  761. #endif
  762. default:
  763. pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
  764. break;
  765. }
  766. if ((unsigned)ipinr < NR_IPI)
  767. trace_ipi_exit_rcuidle(ipi_types[ipinr]);
  768. set_irq_regs(old_regs);
  769. }
  770. void smp_send_reschedule(int cpu)
  771. {
  772. smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
  773. }
  774. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  775. void tick_broadcast(const struct cpumask *mask)
  776. {
  777. smp_cross_call(mask, IPI_TIMER);
  778. }
  779. #endif
  780. void smp_send_stop(void)
  781. {
  782. unsigned long timeout;
  783. if (num_online_cpus() > 1) {
  784. cpumask_t mask;
  785. cpumask_copy(&mask, cpu_online_mask);
  786. cpumask_clear_cpu(smp_processor_id(), &mask);
  787. if (system_state <= SYSTEM_RUNNING)
  788. pr_crit("SMP: stopping secondary CPUs\n");
  789. smp_cross_call(&mask, IPI_CPU_STOP);
  790. }
  791. /* Wait up to one second for other CPUs to stop */
  792. timeout = USEC_PER_SEC;
  793. while (num_online_cpus() > 1 && timeout--)
  794. udelay(1);
  795. if (num_online_cpus() > 1)
  796. pr_warning("SMP: failed to stop secondary CPUs %*pbl\n",
  797. cpumask_pr_args(cpu_online_mask));
  798. sdei_mask_local_cpu();
  799. }
  800. #ifdef CONFIG_KEXEC_CORE
  801. void crash_smp_send_stop(void)
  802. {
  803. static int cpus_stopped;
  804. cpumask_t mask;
  805. unsigned long timeout;
  806. /*
  807. * This function can be called twice in panic path, but obviously
  808. * we execute this only once.
  809. */
  810. if (cpus_stopped)
  811. return;
  812. cpus_stopped = 1;
  813. if (num_online_cpus() == 1) {
  814. sdei_mask_local_cpu();
  815. return;
  816. }
  817. cpumask_copy(&mask, cpu_online_mask);
  818. cpumask_clear_cpu(smp_processor_id(), &mask);
  819. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  820. pr_crit("SMP: stopping secondary CPUs\n");
  821. smp_cross_call(&mask, IPI_CPU_CRASH_STOP);
  822. /* Wait up to one second for other CPUs to stop */
  823. timeout = USEC_PER_SEC;
  824. while ((atomic_read(&waiting_for_crash_ipi) > 0) && timeout--)
  825. udelay(1);
  826. if (atomic_read(&waiting_for_crash_ipi) > 0)
  827. pr_warning("SMP: failed to stop secondary CPUs %*pbl\n",
  828. cpumask_pr_args(&mask));
  829. sdei_mask_local_cpu();
  830. }
  831. bool smp_crash_stop_failed(void)
  832. {
  833. return (atomic_read(&waiting_for_crash_ipi) > 0);
  834. }
  835. #endif
  836. /*
  837. * not supported here
  838. */
  839. int setup_profiling_timer(unsigned int multiplier)
  840. {
  841. return -EINVAL;
  842. }
  843. static bool have_cpu_die(void)
  844. {
  845. #ifdef CONFIG_HOTPLUG_CPU
  846. int any_cpu = raw_smp_processor_id();
  847. if (cpu_ops[any_cpu] && cpu_ops[any_cpu]->cpu_die)
  848. return true;
  849. #endif
  850. return false;
  851. }
  852. bool cpus_are_stuck_in_kernel(void)
  853. {
  854. bool smp_spin_tables = (num_possible_cpus() > 1 && !have_cpu_die());
  855. return !!cpus_stuck_in_kernel || smp_spin_tables;
  856. }