smp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * SMP initialisation and IPI support
  3. * Based on arch/arm64/kernel/smp.c
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Copyright (C) 2015 Regents of the University of California
  7. * Copyright (C) 2017 SiFive
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/interrupt.h>
  22. #include <linux/smp.h>
  23. #include <linux/sched.h>
  24. #include <asm/sbi.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/cacheflush.h>
  27. /* A collection of single bit ipi messages. */
  28. static struct {
  29. unsigned long bits ____cacheline_aligned;
  30. } ipi_data[NR_CPUS] __cacheline_aligned;
  31. enum ipi_message_type {
  32. IPI_RESCHEDULE,
  33. IPI_CALL_FUNC,
  34. IPI_MAX
  35. };
  36. irqreturn_t handle_ipi(void)
  37. {
  38. unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
  39. /* Clear pending IPI */
  40. csr_clear(sip, SIE_SSIE);
  41. while (true) {
  42. unsigned long ops;
  43. /* Order bit clearing and data access. */
  44. mb();
  45. ops = xchg(pending_ipis, 0);
  46. if (ops == 0)
  47. return IRQ_HANDLED;
  48. if (ops & (1 << IPI_RESCHEDULE))
  49. scheduler_ipi();
  50. if (ops & (1 << IPI_CALL_FUNC))
  51. generic_smp_call_function_interrupt();
  52. BUG_ON((ops >> IPI_MAX) != 0);
  53. /* Order data access and bit testing. */
  54. mb();
  55. }
  56. return IRQ_HANDLED;
  57. }
  58. static void
  59. send_ipi_message(const struct cpumask *to_whom, enum ipi_message_type operation)
  60. {
  61. int i;
  62. mb();
  63. for_each_cpu(i, to_whom)
  64. set_bit(operation, &ipi_data[i].bits);
  65. mb();
  66. sbi_send_ipi(cpumask_bits(to_whom));
  67. }
  68. void arch_send_call_function_ipi_mask(struct cpumask *mask)
  69. {
  70. send_ipi_message(mask, IPI_CALL_FUNC);
  71. }
  72. void arch_send_call_function_single_ipi(int cpu)
  73. {
  74. send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
  75. }
  76. static void ipi_stop(void *unused)
  77. {
  78. while (1)
  79. wait_for_interrupt();
  80. }
  81. void smp_send_stop(void)
  82. {
  83. on_each_cpu(ipi_stop, NULL, 1);
  84. }
  85. void smp_send_reschedule(int cpu)
  86. {
  87. send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
  88. }
  89. /*
  90. * Performs an icache flush for the given MM context. RISC-V has no direct
  91. * mechanism for instruction cache shoot downs, so instead we send an IPI that
  92. * informs the remote harts they need to flush their local instruction caches.
  93. * To avoid pathologically slow behavior in a common case (a bunch of
  94. * single-hart processes on a many-hart machine, ie 'make -j') we avoid the
  95. * IPIs for harts that are not currently executing a MM context and instead
  96. * schedule a deferred local instruction cache flush to be performed before
  97. * execution resumes on each hart.
  98. */
  99. void flush_icache_mm(struct mm_struct *mm, bool local)
  100. {
  101. unsigned int cpu;
  102. cpumask_t others, *mask;
  103. preempt_disable();
  104. /* Mark every hart's icache as needing a flush for this MM. */
  105. mask = &mm->context.icache_stale_mask;
  106. cpumask_setall(mask);
  107. /* Flush this hart's I$ now, and mark it as flushed. */
  108. cpu = smp_processor_id();
  109. cpumask_clear_cpu(cpu, mask);
  110. local_flush_icache_all();
  111. /*
  112. * Flush the I$ of other harts concurrently executing, and mark them as
  113. * flushed.
  114. */
  115. cpumask_andnot(&others, mm_cpumask(mm), cpumask_of(cpu));
  116. local |= cpumask_empty(&others);
  117. if (mm != current->active_mm || !local)
  118. sbi_remote_fence_i(others.bits);
  119. else {
  120. /*
  121. * It's assumed that at least one strongly ordered operation is
  122. * performed on this hart between setting a hart's cpumask bit
  123. * and scheduling this MM context on that hart. Sending an SBI
  124. * remote message will do this, but in the case where no
  125. * messages are sent we still need to order this hart's writes
  126. * with flush_icache_deferred().
  127. */
  128. smp_mb();
  129. }
  130. preempt_enable();
  131. }