RISC-V: Flush I$ when making a dirty page executable
[linux-2.6-microblaze.git] / arch / riscv / include / asm / mmu_context.h
1 /*
2  * Copyright (C) 2012 Regents of the University of California
3  * Copyright (C) 2017 SiFive
4  *
5  *   This program is free software; you can redistribute it and/or
6  *   modify it under the terms of the GNU General Public License
7  *   as published by the Free Software Foundation, version 2.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  */
14
15 #ifndef _ASM_RISCV_MMU_CONTEXT_H
16 #define _ASM_RISCV_MMU_CONTEXT_H
17
18 #include <asm-generic/mm_hooks.h>
19
20 #include <linux/mm.h>
21 #include <linux/sched.h>
22 #include <asm/tlbflush.h>
23 #include <asm/cacheflush.h>
24
25 static inline void enter_lazy_tlb(struct mm_struct *mm,
26         struct task_struct *task)
27 {
28 }
29
30 /* Initialize context-related info for a new mm_struct */
31 static inline int init_new_context(struct task_struct *task,
32         struct mm_struct *mm)
33 {
34         return 0;
35 }
36
37 static inline void destroy_context(struct mm_struct *mm)
38 {
39 }
40
41 static inline pgd_t *current_pgdir(void)
42 {
43         return pfn_to_virt(csr_read(sptbr) & SPTBR_PPN);
44 }
45
46 static inline void set_pgdir(pgd_t *pgd)
47 {
48         csr_write(sptbr, virt_to_pfn(pgd) | SPTBR_MODE);
49 }
50
51 /*
52  * When necessary, performs a deferred icache flush for the given MM context,
53  * on the local CPU.  RISC-V has no direct mechanism for instruction cache
54  * shoot downs, so instead we send an IPI that informs the remote harts they
55  * need to flush their local instruction caches.  To avoid pathologically slow
56  * behavior in a common case (a bunch of single-hart processes on a many-hart
57  * machine, ie 'make -j') we avoid the IPIs for harts that are not currently
58  * executing a MM context and instead schedule a deferred local instruction
59  * cache flush to be performed before execution resumes on each hart.  This
60  * actually performs that local instruction cache flush, which implicitly only
61  * refers to the current hart.
62  */
63 static inline void flush_icache_deferred(struct mm_struct *mm)
64 {
65 #ifdef CONFIG_SMP
66         unsigned int cpu = smp_processor_id();
67         cpumask_t *mask = &mm->context.icache_stale_mask;
68
69         if (cpumask_test_cpu(cpu, mask)) {
70                 cpumask_clear_cpu(cpu, mask);
71                 /*
72                  * Ensure the remote hart's writes are visible to this hart.
73                  * This pairs with a barrier in flush_icache_mm.
74                  */
75                 smp_mb();
76                 local_flush_icache_all();
77         }
78 #endif
79 }
80
81 static inline void switch_mm(struct mm_struct *prev,
82         struct mm_struct *next, struct task_struct *task)
83 {
84         if (likely(prev != next)) {
85                 /*
86                  * Mark the current MM context as inactive, and the next as
87                  * active.  This is at least used by the icache flushing
88                  * routines in order to determine who should
89                  */
90                 unsigned int cpu = smp_processor_id();
91
92                 cpumask_clear_cpu(cpu, mm_cpumask(prev));
93                 cpumask_set_cpu(cpu, mm_cpumask(next));
94
95                 set_pgdir(next->pgd);
96                 local_flush_tlb_all();
97
98                 flush_icache_deferred(next);
99         }
100 }
101
102 static inline void activate_mm(struct mm_struct *prev,
103                                struct mm_struct *next)
104 {
105         switch_mm(prev, next, NULL);
106 }
107
108 static inline void deactivate_mm(struct task_struct *task,
109         struct mm_struct *mm)
110 {
111 }
112
113 #endif /* _ASM_RISCV_MMU_CONTEXT_H */