cacheflush.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 SiFive
  4. */
  5. #include <linux/acpi.h>
  6. #include <linux/of.h>
  7. #include <linux/prctl.h>
  8. #include <asm/acpi.h>
  9. #include <asm/cacheflush.h>
  10. #ifdef CONFIG_SMP
  11. #include <asm/sbi.h>
  12. static void ipi_remote_fence_i(void *info)
  13. {
  14. return local_flush_icache_all();
  15. }
  16. void flush_icache_all(void)
  17. {
  18. local_flush_icache_all();
  19. if (num_online_cpus() < 2)
  20. return;
  21. /*
  22. * Make sure all previous writes to the D$ are ordered before making
  23. * the IPI. The RISC-V spec states that a hart must execute a data fence
  24. * before triggering a remote fence.i in order to make the modification
  25. * visable for remote harts.
  26. *
  27. * IPIs on RISC-V are triggered by MMIO writes to either CLINT or
  28. * S-IMSIC, so the fence ensures previous data writes "happen before"
  29. * the MMIO.
  30. */
  31. RISCV_FENCE(w, o);
  32. if (riscv_use_sbi_for_rfence())
  33. sbi_remote_fence_i(NULL);
  34. else
  35. on_each_cpu(ipi_remote_fence_i, NULL, 1);
  36. }
  37. EXPORT_SYMBOL(flush_icache_all);
  38. /*
  39. * Performs an icache flush for the given MM context. RISC-V has no direct
  40. * mechanism for instruction cache shoot downs, so instead we send an IPI that
  41. * informs the remote harts they need to flush their local instruction caches.
  42. * To avoid pathologically slow behavior in a common case (a bunch of
  43. * single-hart processes on a many-hart machine, ie 'make -j') we avoid the
  44. * IPIs for harts that are not currently executing a MM context and instead
  45. * schedule a deferred local instruction cache flush to be performed before
  46. * execution resumes on each hart.
  47. */
  48. void flush_icache_mm(struct mm_struct *mm, bool local)
  49. {
  50. unsigned int cpu;
  51. cpumask_t others, *mask;
  52. preempt_disable();
  53. /* Mark every hart's icache as needing a flush for this MM. */
  54. mask = &mm->context.icache_stale_mask;
  55. cpumask_setall(mask);
  56. /* Flush this hart's I$ now, and mark it as flushed. */
  57. cpu = smp_processor_id();
  58. cpumask_clear_cpu(cpu, mask);
  59. local_flush_icache_all();
  60. /*
  61. * Flush the I$ of other harts concurrently executing, and mark them as
  62. * flushed.
  63. */
  64. cpumask_andnot(&others, mm_cpumask(mm), cpumask_of(cpu));
  65. local |= cpumask_empty(&others);
  66. if (mm == current->active_mm && local) {
  67. /*
  68. * It's assumed that at least one strongly ordered operation is
  69. * performed on this hart between setting a hart's cpumask bit
  70. * and scheduling this MM context on that hart. Sending an SBI
  71. * remote message will do this, but in the case where no
  72. * messages are sent we still need to order this hart's writes
  73. * with flush_icache_deferred().
  74. */
  75. smp_mb();
  76. } else if (riscv_use_sbi_for_rfence()) {
  77. sbi_remote_fence_i(&others);
  78. } else {
  79. on_each_cpu_mask(&others, ipi_remote_fence_i, NULL, 1);
  80. }
  81. preempt_enable();
  82. }
  83. #endif /* CONFIG_SMP */
  84. #ifdef CONFIG_MMU
  85. void flush_icache_pte(struct mm_struct *mm, pte_t pte)
  86. {
  87. struct folio *folio = page_folio(pte_page(pte));
  88. if (!test_bit(PG_dcache_clean, &folio->flags)) {
  89. flush_icache_mm(mm, false);
  90. set_bit(PG_dcache_clean, &folio->flags);
  91. }
  92. }
  93. #endif /* CONFIG_MMU */
  94. unsigned int riscv_cbom_block_size;
  95. EXPORT_SYMBOL_GPL(riscv_cbom_block_size);
  96. unsigned int riscv_cboz_block_size;
  97. EXPORT_SYMBOL_GPL(riscv_cboz_block_size);
  98. static void __init cbo_get_block_size(struct device_node *node,
  99. const char *name, u32 *block_size,
  100. unsigned long *first_hartid)
  101. {
  102. unsigned long hartid;
  103. u32 val;
  104. if (riscv_of_processor_hartid(node, &hartid))
  105. return;
  106. if (of_property_read_u32(node, name, &val))
  107. return;
  108. if (!*block_size) {
  109. *block_size = val;
  110. *first_hartid = hartid;
  111. } else if (*block_size != val) {
  112. pr_warn("%s mismatched between harts %lu and %lu\n",
  113. name, *first_hartid, hartid);
  114. }
  115. }
  116. void __init riscv_init_cbo_blocksizes(void)
  117. {
  118. unsigned long cbom_hartid, cboz_hartid;
  119. u32 cbom_block_size = 0, cboz_block_size = 0;
  120. struct device_node *node;
  121. struct acpi_table_header *rhct;
  122. acpi_status status;
  123. if (acpi_disabled) {
  124. for_each_of_cpu_node(node) {
  125. /* set block-size for cbom and/or cboz extension if available */
  126. cbo_get_block_size(node, "riscv,cbom-block-size",
  127. &cbom_block_size, &cbom_hartid);
  128. cbo_get_block_size(node, "riscv,cboz-block-size",
  129. &cboz_block_size, &cboz_hartid);
  130. }
  131. } else {
  132. status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
  133. if (ACPI_FAILURE(status))
  134. return;
  135. acpi_get_cbo_block_size(rhct, &cbom_block_size, &cboz_block_size, NULL);
  136. acpi_put_table((struct acpi_table_header *)rhct);
  137. }
  138. if (cbom_block_size)
  139. riscv_cbom_block_size = cbom_block_size;
  140. if (cboz_block_size)
  141. riscv_cboz_block_size = cboz_block_size;
  142. }
  143. #ifdef CONFIG_SMP
  144. static void set_icache_stale_mask(void)
  145. {
  146. int cpu = get_cpu();
  147. cpumask_t *mask;
  148. bool stale_cpu;
  149. /*
  150. * Mark every other hart's icache as needing a flush for
  151. * this MM. Maintain the previous value of the current
  152. * cpu to handle the case when this function is called
  153. * concurrently on different harts.
  154. */
  155. mask = &current->mm->context.icache_stale_mask;
  156. stale_cpu = cpumask_test_cpu(cpu, mask);
  157. cpumask_setall(mask);
  158. cpumask_assign_cpu(cpu, mask, stale_cpu);
  159. put_cpu();
  160. }
  161. #endif
  162. /**
  163. * riscv_set_icache_flush_ctx() - Enable/disable icache flushing instructions in
  164. * userspace.
  165. * @ctx: Set the type of icache flushing instructions permitted/prohibited in
  166. * userspace. Supported values described below.
  167. *
  168. * Supported values for ctx:
  169. *
  170. * * %PR_RISCV_CTX_SW_FENCEI_ON: Allow fence.i in user space.
  171. *
  172. * * %PR_RISCV_CTX_SW_FENCEI_OFF: Disallow fence.i in user space. All threads in
  173. * a process will be affected when ``scope == PR_RISCV_SCOPE_PER_PROCESS``.
  174. * Therefore, caution must be taken; use this flag only when you can guarantee
  175. * that no thread in the process will emit fence.i from this point onward.
  176. *
  177. * @scope: Set scope of where icache flushing instructions are allowed to be
  178. * emitted. Supported values described below.
  179. *
  180. * Supported values for scope:
  181. *
  182. * * %PR_RISCV_SCOPE_PER_PROCESS: Ensure the icache of any thread in this process
  183. * is coherent with instruction storage upon
  184. * migration.
  185. *
  186. * * %PR_RISCV_SCOPE_PER_THREAD: Ensure the icache of the current thread is
  187. * coherent with instruction storage upon
  188. * migration.
  189. *
  190. * When ``scope == PR_RISCV_SCOPE_PER_PROCESS``, all threads in the process are
  191. * permitted to emit icache flushing instructions. Whenever any thread in the
  192. * process is migrated, the corresponding hart's icache will be guaranteed to be
  193. * consistent with instruction storage. This does not enforce any guarantees
  194. * outside of migration. If a thread modifies an instruction that another thread
  195. * may attempt to execute, the other thread must still emit an icache flushing
  196. * instruction before attempting to execute the potentially modified
  197. * instruction. This must be performed by the user-space program.
  198. *
  199. * In per-thread context (eg. ``scope == PR_RISCV_SCOPE_PER_THREAD``) only the
  200. * thread calling this function is permitted to emit icache flushing
  201. * instructions. When the thread is migrated, the corresponding hart's icache
  202. * will be guaranteed to be consistent with instruction storage.
  203. *
  204. * On kernels configured without SMP, this function is a nop as migrations
  205. * across harts will not occur.
  206. */
  207. int riscv_set_icache_flush_ctx(unsigned long ctx, unsigned long scope)
  208. {
  209. #ifdef CONFIG_SMP
  210. switch (ctx) {
  211. case PR_RISCV_CTX_SW_FENCEI_ON:
  212. switch (scope) {
  213. case PR_RISCV_SCOPE_PER_PROCESS:
  214. current->mm->context.force_icache_flush = true;
  215. break;
  216. case PR_RISCV_SCOPE_PER_THREAD:
  217. current->thread.force_icache_flush = true;
  218. break;
  219. default:
  220. return -EINVAL;
  221. }
  222. break;
  223. case PR_RISCV_CTX_SW_FENCEI_OFF:
  224. switch (scope) {
  225. case PR_RISCV_SCOPE_PER_PROCESS:
  226. set_icache_stale_mask();
  227. current->mm->context.force_icache_flush = false;
  228. break;
  229. case PR_RISCV_SCOPE_PER_THREAD:
  230. set_icache_stale_mask();
  231. current->thread.force_icache_flush = false;
  232. break;
  233. default:
  234. return -EINVAL;
  235. }
  236. break;
  237. default:
  238. return -EINVAL;
  239. }
  240. return 0;
  241. #else
  242. switch (ctx) {
  243. case PR_RISCV_CTX_SW_FENCEI_ON:
  244. case PR_RISCV_CTX_SW_FENCEI_OFF:
  245. return 0;
  246. default:
  247. return -EINVAL;
  248. }
  249. #endif
  250. }