smp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/kernel/smp.c
  4. *
  5. * Copyright (C) 2002 ARM Limited, All Rights Reserved.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/delay.h>
  9. #include <linux/init.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/sched/mm.h>
  12. #include <linux/sched/hotplug.h>
  13. #include <linux/sched/task_stack.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/cache.h>
  16. #include <linux/profile.h>
  17. #include <linux/errno.h>
  18. #include <linux/mm.h>
  19. #include <linux/err.h>
  20. #include <linux/cpu.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/irq.h>
  23. #include <linux/nmi.h>
  24. #include <linux/percpu.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/completion.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/irq_work.h>
  29. #include <linux/kernel_stat.h>
  30. #include <linux/atomic.h>
  31. #include <asm/bugs.h>
  32. #include <asm/smp.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/cpu.h>
  35. #include <asm/cputype.h>
  36. #include <asm/exception.h>
  37. #include <asm/idmap.h>
  38. #include <asm/topology.h>
  39. #include <asm/mmu_context.h>
  40. #include <asm/procinfo.h>
  41. #include <asm/processor.h>
  42. #include <asm/sections.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/ptrace.h>
  45. #include <asm/smp_plat.h>
  46. #include <asm/virt.h>
  47. #include <asm/mach/arch.h>
  48. #include <asm/mpu.h>
  49. #define CREATE_TRACE_POINTS
  50. #include <trace/events/ipi.h>
  51. EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_raise);
  52. EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_entry);
  53. EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_exit);
  54. /*
  55. * as from 2.5, kernels no longer have an init_tasks structure
  56. * so we need some other way of telling a new secondary core
  57. * where to place its SVC stack
  58. */
  59. struct secondary_data secondary_data;
  60. enum ipi_msg_type {
  61. IPI_WAKEUP,
  62. IPI_TIMER,
  63. IPI_RESCHEDULE,
  64. IPI_CALL_FUNC,
  65. IPI_CPU_STOP,
  66. IPI_IRQ_WORK,
  67. IPI_COMPLETION,
  68. NR_IPI,
  69. /*
  70. * CPU_BACKTRACE is special and not included in NR_IPI
  71. * or tracable with trace_ipi_*
  72. */
  73. IPI_CPU_BACKTRACE = NR_IPI,
  74. /*
  75. * SGI8-15 can be reserved by secure firmware, and thus may
  76. * not be usable by the kernel. Please keep the above limited
  77. * to at most 8 entries.
  78. */
  79. MAX_IPI
  80. };
  81. static int ipi_irq_base __read_mostly;
  82. static int nr_ipi __read_mostly = NR_IPI;
  83. static struct irq_desc *ipi_desc[MAX_IPI] __read_mostly;
  84. static void ipi_setup(int cpu);
  85. static DECLARE_COMPLETION(cpu_running);
  86. static struct smp_operations smp_ops __ro_after_init;
  87. void __init smp_set_ops(const struct smp_operations *ops)
  88. {
  89. if (ops)
  90. smp_ops = *ops;
  91. };
  92. static unsigned long get_arch_pgd(pgd_t *pgd)
  93. {
  94. #ifdef CONFIG_ARM_LPAE
  95. return __phys_to_pfn(virt_to_phys(pgd));
  96. #else
  97. return virt_to_phys(pgd);
  98. #endif
  99. }
  100. #if defined(CONFIG_BIG_LITTLE) && defined(CONFIG_HARDEN_BRANCH_PREDICTOR)
  101. static int secondary_biglittle_prepare(unsigned int cpu)
  102. {
  103. if (!cpu_vtable[cpu])
  104. cpu_vtable[cpu] = kzalloc(sizeof(*cpu_vtable[cpu]), GFP_KERNEL);
  105. return cpu_vtable[cpu] ? 0 : -ENOMEM;
  106. }
  107. static void secondary_biglittle_init(void)
  108. {
  109. init_proc_vtable(lookup_processor(read_cpuid_id())->proc);
  110. }
  111. #else
  112. static int secondary_biglittle_prepare(unsigned int cpu)
  113. {
  114. return 0;
  115. }
  116. static void secondary_biglittle_init(void)
  117. {
  118. }
  119. #endif
  120. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  121. {
  122. int ret;
  123. if (!smp_ops.smp_boot_secondary)
  124. return -ENOSYS;
  125. ret = secondary_biglittle_prepare(cpu);
  126. if (ret)
  127. return ret;
  128. /*
  129. * We need to tell the secondary core where to find
  130. * its stack and the page tables.
  131. */
  132. secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
  133. #ifdef CONFIG_ARM_MPU
  134. secondary_data.mpu_rgn_info = &mpu_rgn_info;
  135. #endif
  136. #ifdef CONFIG_MMU
  137. secondary_data.pgdir = virt_to_phys(idmap_pgd);
  138. secondary_data.swapper_pg_dir = get_arch_pgd(swapper_pg_dir);
  139. #endif
  140. secondary_data.task = idle;
  141. sync_cache_w(&secondary_data);
  142. /*
  143. * Now bring the CPU into our world.
  144. */
  145. ret = smp_ops.smp_boot_secondary(cpu, idle);
  146. if (ret == 0) {
  147. /*
  148. * CPU was successfully started, wait for it
  149. * to come online or time out.
  150. */
  151. wait_for_completion_timeout(&cpu_running,
  152. msecs_to_jiffies(1000));
  153. if (!cpu_online(cpu)) {
  154. pr_crit("CPU%u: failed to come online\n", cpu);
  155. ret = -EIO;
  156. }
  157. } else {
  158. pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
  159. }
  160. memset(&secondary_data, 0, sizeof(secondary_data));
  161. return ret;
  162. }
  163. /* platform specific SMP operations */
  164. void __init smp_init_cpus(void)
  165. {
  166. if (smp_ops.smp_init_cpus)
  167. smp_ops.smp_init_cpus();
  168. }
  169. int platform_can_secondary_boot(void)
  170. {
  171. return !!smp_ops.smp_boot_secondary;
  172. }
  173. int platform_can_cpu_hotplug(void)
  174. {
  175. #ifdef CONFIG_HOTPLUG_CPU
  176. if (smp_ops.cpu_kill)
  177. return 1;
  178. #endif
  179. return 0;
  180. }
  181. #ifdef CONFIG_HOTPLUG_CPU
  182. static int platform_cpu_kill(unsigned int cpu)
  183. {
  184. if (smp_ops.cpu_kill)
  185. return smp_ops.cpu_kill(cpu);
  186. return 1;
  187. }
  188. static int platform_cpu_disable(unsigned int cpu)
  189. {
  190. if (smp_ops.cpu_disable)
  191. return smp_ops.cpu_disable(cpu);
  192. return 0;
  193. }
  194. int platform_can_hotplug_cpu(unsigned int cpu)
  195. {
  196. /* cpu_die must be specified to support hotplug */
  197. if (!smp_ops.cpu_die)
  198. return 0;
  199. if (smp_ops.cpu_can_disable)
  200. return smp_ops.cpu_can_disable(cpu);
  201. /*
  202. * By default, allow disabling all CPUs except the first one,
  203. * since this is special on a lot of platforms, e.g. because
  204. * of clock tick interrupts.
  205. */
  206. return cpu != 0;
  207. }
  208. static void ipi_teardown(int cpu)
  209. {
  210. int i;
  211. if (WARN_ON_ONCE(!ipi_irq_base))
  212. return;
  213. for (i = 0; i < nr_ipi; i++)
  214. disable_percpu_irq(ipi_irq_base + i);
  215. }
  216. /*
  217. * __cpu_disable runs on the processor to be shutdown.
  218. */
  219. int __cpu_disable(void)
  220. {
  221. unsigned int cpu = smp_processor_id();
  222. int ret;
  223. ret = platform_cpu_disable(cpu);
  224. if (ret)
  225. return ret;
  226. #ifdef CONFIG_GENERIC_ARCH_TOPOLOGY
  227. remove_cpu_topology(cpu);
  228. #endif
  229. /*
  230. * Take this CPU offline. Once we clear this, we can't return,
  231. * and we must not schedule until we're ready to give up the cpu.
  232. */
  233. set_cpu_online(cpu, false);
  234. ipi_teardown(cpu);
  235. /*
  236. * OK - migrate IRQs away from this CPU
  237. */
  238. irq_migrate_all_off_this_cpu();
  239. /*
  240. * Flush user cache and TLB mappings, and then remove this CPU
  241. * from the vm mask set of all processes.
  242. *
  243. * Caches are flushed to the Level of Unification Inner Shareable
  244. * to write-back dirty lines to unified caches shared by all CPUs.
  245. */
  246. flush_cache_louis();
  247. local_flush_tlb_all();
  248. return 0;
  249. }
  250. /*
  251. * called on the thread which is asking for a CPU to be shutdown -
  252. * waits until shutdown has completed, or it is timed out.
  253. */
  254. void __cpu_die(unsigned int cpu)
  255. {
  256. if (!cpu_wait_death(cpu, 5)) {
  257. pr_err("CPU%u: cpu didn't die\n", cpu);
  258. return;
  259. }
  260. pr_debug("CPU%u: shutdown\n", cpu);
  261. clear_tasks_mm_cpumask(cpu);
  262. /*
  263. * platform_cpu_kill() is generally expected to do the powering off
  264. * and/or cutting of clocks to the dying CPU. Optionally, this may
  265. * be done by the CPU which is dying in preference to supporting
  266. * this call, but that means there is _no_ synchronisation between
  267. * the requesting CPU and the dying CPU actually losing power.
  268. */
  269. if (!platform_cpu_kill(cpu))
  270. pr_err("CPU%u: unable to kill\n", cpu);
  271. }
  272. /*
  273. * Called from the idle thread for the CPU which has been shutdown.
  274. *
  275. * Note that we disable IRQs here, but do not re-enable them
  276. * before returning to the caller. This is also the behaviour
  277. * of the other hotplug-cpu capable cores, so presumably coming
  278. * out of idle fixes this.
  279. */
  280. void arch_cpu_idle_dead(void)
  281. {
  282. unsigned int cpu = smp_processor_id();
  283. idle_task_exit();
  284. local_irq_disable();
  285. /*
  286. * Flush the data out of the L1 cache for this CPU. This must be
  287. * before the completion to ensure that data is safely written out
  288. * before platform_cpu_kill() gets called - which may disable
  289. * *this* CPU and power down its cache.
  290. */
  291. flush_cache_louis();
  292. /*
  293. * Tell __cpu_die() that this CPU is now safe to dispose of. Once
  294. * this returns, power and/or clocks can be removed at any point
  295. * from this CPU and its cache by platform_cpu_kill().
  296. */
  297. (void)cpu_report_death();
  298. /*
  299. * Ensure that the cache lines associated with that completion are
  300. * written out. This covers the case where _this_ CPU is doing the
  301. * powering down, to ensure that the completion is visible to the
  302. * CPU waiting for this one.
  303. */
  304. flush_cache_louis();
  305. /*
  306. * The actual CPU shutdown procedure is at least platform (if not
  307. * CPU) specific. This may remove power, or it may simply spin.
  308. *
  309. * Platforms are generally expected *NOT* to return from this call,
  310. * although there are some which do because they have no way to
  311. * power down the CPU. These platforms are the _only_ reason we
  312. * have a return path which uses the fragment of assembly below.
  313. *
  314. * The return path should not be used for platforms which can
  315. * power off the CPU.
  316. */
  317. if (smp_ops.cpu_die)
  318. smp_ops.cpu_die(cpu);
  319. pr_warn("CPU%u: smp_ops.cpu_die() returned, trying to resuscitate\n",
  320. cpu);
  321. /*
  322. * Do not return to the idle loop - jump back to the secondary
  323. * cpu initialisation. There's some initialisation which needs
  324. * to be repeated to undo the effects of taking the CPU offline.
  325. */
  326. __asm__("mov sp, %0\n"
  327. " mov fp, #0\n"
  328. " mov r0, %1\n"
  329. " b secondary_start_kernel"
  330. :
  331. : "r" (task_stack_page(current) + THREAD_SIZE - 8),
  332. "r" (current)
  333. : "r0");
  334. }
  335. #endif /* CONFIG_HOTPLUG_CPU */
  336. /*
  337. * Called by both boot and secondaries to move global data into
  338. * per-processor storage.
  339. */
  340. static void smp_store_cpu_info(unsigned int cpuid)
  341. {
  342. struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
  343. cpu_info->loops_per_jiffy = loops_per_jiffy;
  344. cpu_info->cpuid = read_cpuid_id();
  345. store_cpu_topology(cpuid);
  346. check_cpu_icache_size(cpuid);
  347. }
  348. static void set_current(struct task_struct *cur)
  349. {
  350. /* Set TPIDRURO */
  351. asm("mcr p15, 0, %0, c13, c0, 3" :: "r"(cur) : "memory");
  352. }
  353. /*
  354. * This is the secondary CPU boot entry. We're using this CPUs
  355. * idle thread stack, but a set of temporary page tables.
  356. */
  357. asmlinkage void secondary_start_kernel(struct task_struct *task)
  358. {
  359. struct mm_struct *mm = &init_mm;
  360. unsigned int cpu;
  361. set_current(task);
  362. secondary_biglittle_init();
  363. /*
  364. * The identity mapping is uncached (strongly ordered), so
  365. * switch away from it before attempting any exclusive accesses.
  366. */
  367. cpu_switch_mm(mm->pgd, mm);
  368. local_flush_bp_all();
  369. enter_lazy_tlb(mm, current);
  370. local_flush_tlb_all();
  371. /*
  372. * All kernel threads share the same mm context; grab a
  373. * reference and switch to it.
  374. */
  375. cpu = smp_processor_id();
  376. mmgrab(mm);
  377. current->active_mm = mm;
  378. cpumask_set_cpu(cpu, mm_cpumask(mm));
  379. cpu_init();
  380. #ifndef CONFIG_MMU
  381. setup_vectors_base();
  382. #endif
  383. pr_debug("CPU%u: Booted secondary processor\n", cpu);
  384. trace_hardirqs_off();
  385. /*
  386. * Give the platform a chance to do its own initialisation.
  387. */
  388. if (smp_ops.smp_secondary_init)
  389. smp_ops.smp_secondary_init(cpu);
  390. notify_cpu_starting(cpu);
  391. ipi_setup(cpu);
  392. calibrate_delay();
  393. smp_store_cpu_info(cpu);
  394. /*
  395. * OK, now it's safe to let the boot CPU continue. Wait for
  396. * the CPU migration code to notice that the CPU is online
  397. * before we continue - which happens after __cpu_up returns.
  398. */
  399. set_cpu_online(cpu, true);
  400. check_other_bugs();
  401. complete(&cpu_running);
  402. local_irq_enable();
  403. local_fiq_enable();
  404. local_abt_enable();
  405. /*
  406. * OK, it's off to the idle thread for us
  407. */
  408. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  409. }
  410. void __init smp_cpus_done(unsigned int max_cpus)
  411. {
  412. int cpu;
  413. unsigned long bogosum = 0;
  414. for_each_online_cpu(cpu)
  415. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  416. printk(KERN_INFO "SMP: Total of %d processors activated "
  417. "(%lu.%02lu BogoMIPS).\n",
  418. num_online_cpus(),
  419. bogosum / (500000/HZ),
  420. (bogosum / (5000/HZ)) % 100);
  421. hyp_mode_check();
  422. }
  423. void __init smp_prepare_boot_cpu(void)
  424. {
  425. set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
  426. }
  427. void __init smp_prepare_cpus(unsigned int max_cpus)
  428. {
  429. unsigned int ncores = num_possible_cpus();
  430. init_cpu_topology();
  431. smp_store_cpu_info(smp_processor_id());
  432. /*
  433. * are we trying to boot more cores than exist?
  434. */
  435. if (max_cpus > ncores)
  436. max_cpus = ncores;
  437. if (ncores > 1 && max_cpus) {
  438. /*
  439. * Initialise the present map, which describes the set of CPUs
  440. * actually populated at the present time. A platform should
  441. * re-initialize the map in the platforms smp_prepare_cpus()
  442. * if present != possible (e.g. physical hotplug).
  443. */
  444. init_cpu_present(cpu_possible_mask);
  445. /*
  446. * Initialise the SCU if there are more than one CPU
  447. * and let them know where to start.
  448. */
  449. if (smp_ops.smp_prepare_cpus)
  450. smp_ops.smp_prepare_cpus(max_cpus);
  451. }
  452. }
  453. static const char *ipi_types[NR_IPI] __tracepoint_string = {
  454. [IPI_WAKEUP] = "CPU wakeup interrupts",
  455. [IPI_TIMER] = "Timer broadcast interrupts",
  456. [IPI_RESCHEDULE] = "Rescheduling interrupts",
  457. [IPI_CALL_FUNC] = "Function call interrupts",
  458. [IPI_CPU_STOP] = "CPU stop interrupts",
  459. [IPI_IRQ_WORK] = "IRQ work interrupts",
  460. [IPI_COMPLETION] = "completion interrupts",
  461. };
  462. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr);
  463. void show_ipi_list(struct seq_file *p, int prec)
  464. {
  465. unsigned int cpu, i;
  466. for (i = 0; i < NR_IPI; i++) {
  467. if (!ipi_desc[i])
  468. continue;
  469. seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
  470. for_each_online_cpu(cpu)
  471. seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i], cpu));
  472. seq_printf(p, " %s\n", ipi_types[i]);
  473. }
  474. }
  475. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  476. {
  477. smp_cross_call(mask, IPI_CALL_FUNC);
  478. }
  479. void arch_send_wakeup_ipi_mask(const struct cpumask *mask)
  480. {
  481. smp_cross_call(mask, IPI_WAKEUP);
  482. }
  483. void arch_send_call_function_single_ipi(int cpu)
  484. {
  485. smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC);
  486. }
  487. #ifdef CONFIG_IRQ_WORK
  488. void arch_irq_work_raise(void)
  489. {
  490. if (arch_irq_work_has_interrupt())
  491. smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
  492. }
  493. #endif
  494. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  495. void tick_broadcast(const struct cpumask *mask)
  496. {
  497. smp_cross_call(mask, IPI_TIMER);
  498. }
  499. #endif
  500. static DEFINE_RAW_SPINLOCK(stop_lock);
  501. /*
  502. * ipi_cpu_stop - handle IPI from smp_send_stop()
  503. */
  504. static void ipi_cpu_stop(unsigned int cpu)
  505. {
  506. if (system_state <= SYSTEM_RUNNING) {
  507. raw_spin_lock(&stop_lock);
  508. pr_crit("CPU%u: stopping\n", cpu);
  509. dump_stack();
  510. raw_spin_unlock(&stop_lock);
  511. }
  512. set_cpu_online(cpu, false);
  513. local_fiq_disable();
  514. local_irq_disable();
  515. while (1) {
  516. cpu_relax();
  517. wfe();
  518. }
  519. }
  520. static DEFINE_PER_CPU(struct completion *, cpu_completion);
  521. int register_ipi_completion(struct completion *completion, int cpu)
  522. {
  523. per_cpu(cpu_completion, cpu) = completion;
  524. return IPI_COMPLETION;
  525. }
  526. static void ipi_complete(unsigned int cpu)
  527. {
  528. complete(per_cpu(cpu_completion, cpu));
  529. }
  530. /*
  531. * Main handler for inter-processor interrupts
  532. */
  533. static void do_handle_IPI(int ipinr)
  534. {
  535. unsigned int cpu = smp_processor_id();
  536. if ((unsigned)ipinr < NR_IPI)
  537. trace_ipi_entry_rcuidle(ipi_types[ipinr]);
  538. switch (ipinr) {
  539. case IPI_WAKEUP:
  540. break;
  541. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  542. case IPI_TIMER:
  543. tick_receive_broadcast();
  544. break;
  545. #endif
  546. case IPI_RESCHEDULE:
  547. scheduler_ipi();
  548. break;
  549. case IPI_CALL_FUNC:
  550. generic_smp_call_function_interrupt();
  551. break;
  552. case IPI_CPU_STOP:
  553. ipi_cpu_stop(cpu);
  554. break;
  555. #ifdef CONFIG_IRQ_WORK
  556. case IPI_IRQ_WORK:
  557. irq_work_run();
  558. break;
  559. #endif
  560. case IPI_COMPLETION:
  561. ipi_complete(cpu);
  562. break;
  563. case IPI_CPU_BACKTRACE:
  564. printk_deferred_enter();
  565. nmi_cpu_backtrace(get_irq_regs());
  566. printk_deferred_exit();
  567. break;
  568. default:
  569. pr_crit("CPU%u: Unknown IPI message 0x%x\n",
  570. cpu, ipinr);
  571. break;
  572. }
  573. if ((unsigned)ipinr < NR_IPI)
  574. trace_ipi_exit_rcuidle(ipi_types[ipinr]);
  575. }
  576. /* Legacy version, should go away once all irqchips have been converted */
  577. void handle_IPI(int ipinr, struct pt_regs *regs)
  578. {
  579. struct pt_regs *old_regs = set_irq_regs(regs);
  580. irq_enter();
  581. do_handle_IPI(ipinr);
  582. irq_exit();
  583. set_irq_regs(old_regs);
  584. }
  585. static irqreturn_t ipi_handler(int irq, void *data)
  586. {
  587. do_handle_IPI(irq - ipi_irq_base);
  588. return IRQ_HANDLED;
  589. }
  590. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
  591. {
  592. trace_ipi_raise_rcuidle(target, ipi_types[ipinr]);
  593. __ipi_send_mask(ipi_desc[ipinr], target);
  594. }
  595. static void ipi_setup(int cpu)
  596. {
  597. int i;
  598. if (WARN_ON_ONCE(!ipi_irq_base))
  599. return;
  600. for (i = 0; i < nr_ipi; i++)
  601. enable_percpu_irq(ipi_irq_base + i, 0);
  602. }
  603. void __init set_smp_ipi_range(int ipi_base, int n)
  604. {
  605. int i;
  606. WARN_ON(n < MAX_IPI);
  607. nr_ipi = min(n, MAX_IPI);
  608. for (i = 0; i < nr_ipi; i++) {
  609. int err;
  610. err = request_percpu_irq(ipi_base + i, ipi_handler,
  611. "IPI", &irq_stat);
  612. WARN_ON(err);
  613. ipi_desc[i] = irq_to_desc(ipi_base + i);
  614. irq_set_status_flags(ipi_base + i, IRQ_HIDDEN);
  615. }
  616. ipi_irq_base = ipi_base;
  617. /* Setup the boot CPU immediately */
  618. ipi_setup(smp_processor_id());
  619. }
  620. void smp_send_reschedule(int cpu)
  621. {
  622. smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
  623. }
  624. void smp_send_stop(void)
  625. {
  626. unsigned long timeout;
  627. struct cpumask mask;
  628. cpumask_copy(&mask, cpu_online_mask);
  629. cpumask_clear_cpu(smp_processor_id(), &mask);
  630. if (!cpumask_empty(&mask))
  631. smp_cross_call(&mask, IPI_CPU_STOP);
  632. /* Wait up to one second for other CPUs to stop */
  633. timeout = USEC_PER_SEC;
  634. while (num_online_cpus() > 1 && timeout--)
  635. udelay(1);
  636. if (num_online_cpus() > 1)
  637. pr_warn("SMP: failed to stop secondary CPUs\n");
  638. }
  639. /* In case panic() and panic() called at the same time on CPU1 and CPU2,
  640. * and CPU 1 calls panic_smp_self_stop() before crash_smp_send_stop()
  641. * CPU1 can't receive the ipi irqs from CPU2, CPU1 will be always online,
  642. * kdump fails. So split out the panic_smp_self_stop() and add
  643. * set_cpu_online(smp_processor_id(), false).
  644. */
  645. void panic_smp_self_stop(void)
  646. {
  647. pr_debug("CPU %u will stop doing anything useful since another CPU has paniced\n",
  648. smp_processor_id());
  649. set_cpu_online(smp_processor_id(), false);
  650. while (1)
  651. cpu_relax();
  652. }
  653. #ifdef CONFIG_CPU_FREQ
  654. static DEFINE_PER_CPU(unsigned long, l_p_j_ref);
  655. static DEFINE_PER_CPU(unsigned long, l_p_j_ref_freq);
  656. static unsigned long global_l_p_j_ref;
  657. static unsigned long global_l_p_j_ref_freq;
  658. static int cpufreq_callback(struct notifier_block *nb,
  659. unsigned long val, void *data)
  660. {
  661. struct cpufreq_freqs *freq = data;
  662. struct cpumask *cpus = freq->policy->cpus;
  663. int cpu, first = cpumask_first(cpus);
  664. unsigned int lpj;
  665. if (freq->flags & CPUFREQ_CONST_LOOPS)
  666. return NOTIFY_OK;
  667. if (!per_cpu(l_p_j_ref, first)) {
  668. for_each_cpu(cpu, cpus) {
  669. per_cpu(l_p_j_ref, cpu) =
  670. per_cpu(cpu_data, cpu).loops_per_jiffy;
  671. per_cpu(l_p_j_ref_freq, cpu) = freq->old;
  672. }
  673. if (!global_l_p_j_ref) {
  674. global_l_p_j_ref = loops_per_jiffy;
  675. global_l_p_j_ref_freq = freq->old;
  676. }
  677. }
  678. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  679. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
  680. loops_per_jiffy = cpufreq_scale(global_l_p_j_ref,
  681. global_l_p_j_ref_freq,
  682. freq->new);
  683. lpj = cpufreq_scale(per_cpu(l_p_j_ref, first),
  684. per_cpu(l_p_j_ref_freq, first), freq->new);
  685. for_each_cpu(cpu, cpus)
  686. per_cpu(cpu_data, cpu).loops_per_jiffy = lpj;
  687. }
  688. return NOTIFY_OK;
  689. }
  690. static struct notifier_block cpufreq_notifier = {
  691. .notifier_call = cpufreq_callback,
  692. };
  693. static int __init register_cpufreq_notifier(void)
  694. {
  695. return cpufreq_register_notifier(&cpufreq_notifier,
  696. CPUFREQ_TRANSITION_NOTIFIER);
  697. }
  698. core_initcall(register_cpufreq_notifier);
  699. #endif
  700. static void raise_nmi(cpumask_t *mask)
  701. {
  702. __ipi_send_mask(ipi_desc[IPI_CPU_BACKTRACE], mask);
  703. }
  704. void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
  705. {
  706. nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_nmi);
  707. }