smp.c 23 KB

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