smp.c 4.0 KB

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