smp.c 21 KB

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