1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 SiFive
6 #include <linux/acpi.h>
8 #include <linux/prctl.h>
10 #include <asm/cacheflush.h>
16 static void ipi_remote_fence_i(void *info)
18 return local_flush_icache_all();
21 void flush_icache_all(void)
23 local_flush_icache_all();
25 if (num_online_cpus() < 2)
29 * Make sure all previous writes to the D$ are ordered before making
30 * the IPI. The RISC-V spec states that a hart must execute a data fence
31 * before triggering a remote fence.i in order to make the modification
32 * visable for remote harts.
34 * IPIs on RISC-V are triggered by MMIO writes to either CLINT or
35 * S-IMSIC, so the fence ensures previous data writes "happen before"
40 if (riscv_use_sbi_for_rfence())
41 sbi_remote_fence_i(NULL);
43 on_each_cpu(ipi_remote_fence_i, NULL, 1);
45 EXPORT_SYMBOL(flush_icache_all);
48 * Performs an icache flush for the given MM context. RISC-V has no direct
49 * mechanism for instruction cache shoot downs, so instead we send an IPI that
50 * informs the remote harts they need to flush their local instruction caches.
51 * To avoid pathologically slow behavior in a common case (a bunch of
52 * single-hart processes on a many-hart machine, ie 'make -j') we avoid the
53 * IPIs for harts that are not currently executing a MM context and instead
54 * schedule a deferred local instruction cache flush to be performed before
55 * execution resumes on each hart.
57 void flush_icache_mm(struct mm_struct *mm, bool local)
60 cpumask_t others, *mask;
64 /* Mark every hart's icache as needing a flush for this MM. */
65 mask = &mm->context.icache_stale_mask;
67 /* Flush this hart's I$ now, and mark it as flushed. */
68 cpu = smp_processor_id();
69 cpumask_clear_cpu(cpu, mask);
70 local_flush_icache_all();
73 * Flush the I$ of other harts concurrently executing, and mark them as
76 cpumask_andnot(&others, mm_cpumask(mm), cpumask_of(cpu));
77 local |= cpumask_empty(&others);
78 if (mm == current->active_mm && local) {
80 * It's assumed that at least one strongly ordered operation is
81 * performed on this hart between setting a hart's cpumask bit
82 * and scheduling this MM context on that hart. Sending an SBI
83 * remote message will do this, but in the case where no
84 * messages are sent we still need to order this hart's writes
85 * with flush_icache_deferred().
88 } else if (riscv_use_sbi_for_rfence()) {
89 sbi_remote_fence_i(&others);
91 on_each_cpu_mask(&others, ipi_remote_fence_i, NULL, 1);
97 #endif /* CONFIG_SMP */
100 void flush_icache_pte(struct mm_struct *mm, pte_t pte)
102 struct folio *folio = page_folio(pte_page(pte));
104 if (!test_bit(PG_dcache_clean, &folio->flags)) {
105 flush_icache_mm(mm, false);
106 set_bit(PG_dcache_clean, &folio->flags);
109 #endif /* CONFIG_MMU */
111 unsigned int riscv_cbom_block_size;
112 EXPORT_SYMBOL_GPL(riscv_cbom_block_size);
114 unsigned int riscv_cboz_block_size;
115 EXPORT_SYMBOL_GPL(riscv_cboz_block_size);
117 unsigned int riscv_cbop_block_size;
118 EXPORT_SYMBOL_GPL(riscv_cbop_block_size);
120 static void __init cbo_get_block_size(struct device_node *node,
121 const char *name, u32 *block_size,
122 unsigned long *first_hartid)
124 unsigned long hartid;
127 if (riscv_of_processor_hartid(node, &hartid))
130 if (of_property_read_u32(node, name, &val))
135 *first_hartid = hartid;
136 } else if (*block_size != val) {
137 pr_warn("%s mismatched between harts %lu and %lu\n",
138 name, *first_hartid, hartid);
142 void __init riscv_init_cbo_blocksizes(void)
144 unsigned long cbom_hartid, cboz_hartid, cbop_hartid;
145 u32 cbom_block_size = 0, cboz_block_size = 0, cbop_block_size = 0;
146 struct device_node *node;
147 struct acpi_table_header *rhct;
151 for_each_of_cpu_node(node) {
152 /* set block-size for cbom and/or cboz extension if available */
153 cbo_get_block_size(node, "riscv,cbom-block-size",
154 &cbom_block_size, &cbom_hartid);
155 cbo_get_block_size(node, "riscv,cboz-block-size",
156 &cboz_block_size, &cboz_hartid);
157 cbo_get_block_size(node, "riscv,cbop-block-size",
158 &cbop_block_size, &cbop_hartid);
161 status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
162 if (ACPI_FAILURE(status))
165 acpi_get_cbo_block_size(rhct, &cbom_block_size, &cboz_block_size, &cbop_block_size);
166 acpi_put_table((struct acpi_table_header *)rhct);
170 riscv_cbom_block_size = cbom_block_size;
173 riscv_cboz_block_size = cboz_block_size;
176 riscv_cbop_block_size = cbop_block_size;
180 static void set_icache_stale_mask(void)
187 * Mark every other hart's icache as needing a flush for
188 * this MM. Maintain the previous value of the current
189 * cpu to handle the case when this function is called
190 * concurrently on different harts.
192 mask = ¤t->mm->context.icache_stale_mask;
193 stale_cpu = cpumask_test_cpu(cpu, mask);
195 cpumask_setall(mask);
196 __assign_cpu(cpu, mask, stale_cpu);
202 * riscv_set_icache_flush_ctx() - Enable/disable icache flushing instructions in
204 * @ctx: Set the type of icache flushing instructions permitted/prohibited in
205 * userspace. Supported values described below.
207 * Supported values for ctx:
209 * * %PR_RISCV_CTX_SW_FENCEI_ON: Allow fence.i in user space.
211 * * %PR_RISCV_CTX_SW_FENCEI_OFF: Disallow fence.i in user space. All threads in
212 * a process will be affected when ``scope == PR_RISCV_SCOPE_PER_PROCESS``.
213 * Therefore, caution must be taken; use this flag only when you can guarantee
214 * that no thread in the process will emit fence.i from this point onward.
216 * @scope: Set scope of where icache flushing instructions are allowed to be
217 * emitted. Supported values described below.
219 * Supported values for scope:
221 * * %PR_RISCV_SCOPE_PER_PROCESS: Ensure the icache of any thread in this process
222 * is coherent with instruction storage upon
225 * * %PR_RISCV_SCOPE_PER_THREAD: Ensure the icache of the current thread is
226 * coherent with instruction storage upon
229 * When ``scope == PR_RISCV_SCOPE_PER_PROCESS``, all threads in the process are
230 * permitted to emit icache flushing instructions. Whenever any thread in the
231 * process is migrated, the corresponding hart's icache will be guaranteed to be
232 * consistent with instruction storage. This does not enforce any guarantees
233 * outside of migration. If a thread modifies an instruction that another thread
234 * may attempt to execute, the other thread must still emit an icache flushing
235 * instruction before attempting to execute the potentially modified
236 * instruction. This must be performed by the user-space program.
238 * In per-thread context (eg. ``scope == PR_RISCV_SCOPE_PER_THREAD``) only the
239 * thread calling this function is permitted to emit icache flushing
240 * instructions. When the thread is migrated, the corresponding hart's icache
241 * will be guaranteed to be consistent with instruction storage.
243 * On kernels configured without SMP, this function is a nop as migrations
244 * across harts will not occur.
246 int riscv_set_icache_flush_ctx(unsigned long ctx, unsigned long scope)
250 case PR_RISCV_CTX_SW_FENCEI_ON:
252 case PR_RISCV_SCOPE_PER_PROCESS:
253 current->mm->context.force_icache_flush = true;
255 case PR_RISCV_SCOPE_PER_THREAD:
256 current->thread.force_icache_flush = true;
262 case PR_RISCV_CTX_SW_FENCEI_OFF:
264 case PR_RISCV_SCOPE_PER_PROCESS:
265 set_icache_stale_mask();
266 current->mm->context.force_icache_flush = false;
268 case PR_RISCV_SCOPE_PER_THREAD:
269 set_icache_stale_mask();
270 current->thread.force_icache_flush = false;
282 case PR_RISCV_CTX_SW_FENCEI_ON:
283 case PR_RISCV_CTX_SW_FENCEI_OFF: