smp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /* Unsupported */
  37. int setup_profiling_timer(unsigned int multiplier)
  38. {
  39. return -EINVAL;
  40. }
  41. irqreturn_t handle_ipi(void)
  42. {
  43. unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
  44. /* Clear pending IPI */
  45. csr_clear(sip, SIE_SSIE);
  46. while (true) {
  47. unsigned long ops;
  48. /* Order bit clearing and data access. */
  49. mb();
  50. ops = xchg(pending_ipis, 0);
  51. if (ops == 0)
  52. return IRQ_HANDLED;
  53. if (ops & (1 << IPI_RESCHEDULE))
  54. scheduler_ipi();
  55. if (ops & (1 << IPI_CALL_FUNC))
  56. generic_smp_call_function_interrupt();
  57. BUG_ON((ops >> IPI_MAX) != 0);
  58. /* Order data access and bit testing. */
  59. mb();
  60. }
  61. return IRQ_HANDLED;
  62. }
  63. static void
  64. send_ipi_message(const struct cpumask *to_whom, enum ipi_message_type operation)
  65. {
  66. int i;
  67. mb();
  68. for_each_cpu(i, to_whom)
  69. set_bit(operation, &ipi_data[i].bits);
  70. mb();
  71. sbi_send_ipi(cpumask_bits(to_whom));
  72. }
  73. void arch_send_call_function_ipi_mask(struct cpumask *mask)
  74. {
  75. send_ipi_message(mask, IPI_CALL_FUNC);
  76. }
  77. void arch_send_call_function_single_ipi(int cpu)
  78. {
  79. send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
  80. }
  81. static void ipi_stop(void *unused)
  82. {
  83. while (1)
  84. wait_for_interrupt();
  85. }
  86. void smp_send_stop(void)
  87. {
  88. on_each_cpu(ipi_stop, NULL, 1);
  89. }
  90. void smp_send_reschedule(int cpu)
  91. {
  92. send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
  93. }
  94. /*
  95. * Performs an icache flush for the given MM context. RISC-V has no direct
  96. * mechanism for instruction cache shoot downs, so instead we send an IPI that
  97. * informs the remote harts they need to flush their local instruction caches.
  98. * To avoid pathologically slow behavior in a common case (a bunch of
  99. * single-hart processes on a many-hart machine, ie 'make -j') we avoid the
  100. * IPIs for harts that are not currently executing a MM context and instead
  101. * schedule a deferred local instruction cache flush to be performed before
  102. * execution resumes on each hart.
  103. */
  104. void flush_icache_mm(struct mm_struct *mm, bool local)
  105. {
  106. unsigned int cpu;
  107. cpumask_t others, *mask;
  108. preempt_disable();
  109. /* Mark every hart's icache as needing a flush for this MM. */
  110. mask = &mm->context.icache_stale_mask;
  111. cpumask_setall(mask);
  112. /* Flush this hart's I$ now, and mark it as flushed. */
  113. cpu = smp_processor_id();
  114. cpumask_clear_cpu(cpu, mask);
  115. local_flush_icache_all();
  116. /*
  117. * Flush the I$ of other harts concurrently executing, and mark them as
  118. * flushed.
  119. */
  120. cpumask_andnot(&others, mm_cpumask(mm), cpumask_of(cpu));
  121. local |= cpumask_empty(&others);
  122. if (mm != current->active_mm || !local)
  123. sbi_remote_fence_i(others.bits);
  124. else {
  125. /*
  126. * It's assumed that at least one strongly ordered operation is
  127. * performed on this hart between setting a hart's cpumask bit
  128. * and scheduling this MM context on that hart. Sending an SBI
  129. * remote message will do this, but in the case where no
  130. * messages are sent we still need to order this hart's writes
  131. * with flush_icache_deferred().
  132. */
  133. smp_mb();
  134. }
  135. preempt_enable();
  136. }