smp.c 4.6 KB

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