blob: 1230b7050d8e30dfbef786c4d28f0f8f532a589d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _ASM_M32R_MMU_CONTEXT_H
2#define _ASM_M32R_MMU_CONTEXT_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#ifdef __KERNEL__
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <asm/m32r.h>
6
7#define MMU_CONTEXT_ASID_MASK (0x000000FF)
8#define MMU_CONTEXT_VERSION_MASK (0xFFFFFF00)
9#define MMU_CONTEXT_FIRST_VERSION (0x00000100)
10#define NO_CONTEXT (0x00000000)
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#ifndef __ASSEMBLY__
13
Arun Sharma600634972011-07-26 16:09:06 -070014#include <linux/atomic.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010015#include <linux/mm_types.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/pgalloc.h>
18#include <asm/mmu.h>
19#include <asm/tlbflush.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020020#include <asm-generic/mm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
23 * Cache of MMU context last used.
24 */
25#ifndef CONFIG_SMP
26extern unsigned long mmu_context_cache_dat;
27#define mmu_context_cache mmu_context_cache_dat
28#define mm_context(mm) mm->context
29#else /* not CONFIG_SMP */
30extern unsigned long mmu_context_cache_dat[];
31#define mmu_context_cache mmu_context_cache_dat[smp_processor_id()]
32#define mm_context(mm) mm->context[smp_processor_id()]
33#endif /* not CONFIG_SMP */
34
35#define set_tlb_tag(entry, tag) (*entry = (tag & PAGE_MASK)|get_asid())
36#define set_tlb_data(entry, data) (*entry = (data | _PAGE_PRESENT))
37
38#ifdef CONFIG_MMU
39#define enter_lazy_tlb(mm, tsk) do { } while (0)
40
41static inline void get_new_mmu_context(struct mm_struct *mm)
42{
43 unsigned long mc = ++mmu_context_cache;
44
45 if (!(mc & MMU_CONTEXT_ASID_MASK)) {
46 /* We exhaust ASID of this version.
47 Flush all TLB and start new cycle. */
48 local_flush_tlb_all();
49 /* Fix version if needed.
Michael Opdenackeraa5e5dc2013-09-18 06:00:43 +020050 Note that we avoid version #0 to distinguish NO_CONTEXT. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (!mc)
52 mmu_context_cache = mc = MMU_CONTEXT_FIRST_VERSION;
53 }
54 mm_context(mm) = mc;
55}
56
57/*
58 * Get MMU context if needed.
59 */
60static inline void get_mmu_context(struct mm_struct *mm)
61{
62 if (mm) {
63 unsigned long mc = mmu_context_cache;
64
65 /* Check if we have old version of context.
66 If it's old, we need to get new context with new version. */
67 if ((mm_context(mm) ^ mc) & MMU_CONTEXT_VERSION_MASK)
68 get_new_mmu_context(mm);
69 }
70}
71
72/*
73 * Initialize the context related info for a new mm_struct
74 * instance.
75 */
76static inline int init_new_context(struct task_struct *tsk,
77 struct mm_struct *mm)
78{
79#ifndef CONFIG_SMP
80 mm->context = NO_CONTEXT;
81#else /* CONFIG_SMP */
82 int num_cpus = num_online_cpus();
83 int i;
84
85 for (i = 0 ; i < num_cpus ; i++)
86 mm->context[i] = NO_CONTEXT;
87#endif /* CONFIG_SMP */
88
89 return 0;
90}
91
92/*
93 * Destroy context related info for an mm_struct that is about
94 * to be put to rest.
95 */
96#define destroy_context(mm) do { } while (0)
97
98static inline void set_asid(unsigned long asid)
99{
100 *(volatile unsigned long *)MASID = (asid & MMU_CONTEXT_ASID_MASK);
101}
102
103static inline unsigned long get_asid(void)
104{
105 unsigned long asid;
106
107 asid = *(volatile long *)MASID;
108 asid &= MMU_CONTEXT_ASID_MASK;
109
110 return asid;
111}
112
113/*
114 * After we have set current->mm to a new value, this activates
115 * the context for the new mm so we see the new mappings.
116 */
117static inline void activate_context(struct mm_struct *mm)
118{
119 get_mmu_context(mm);
120 set_asid(mm_context(mm) & MMU_CONTEXT_ASID_MASK);
121}
122
123static inline void switch_mm(struct mm_struct *prev,
124 struct mm_struct *next, struct task_struct *tsk)
125{
126#ifdef CONFIG_SMP
127 int cpu = smp_processor_id();
128#endif /* CONFIG_SMP */
129
130 if (prev != next) {
131#ifdef CONFIG_SMP
Rusty Russell49b92052009-09-24 09:34:49 -0600132 cpumask_set_cpu(cpu, mm_cpumask(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif /* CONFIG_SMP */
134 /* Set MPTB = next->pgd */
135 *(volatile unsigned long *)MPTB = (unsigned long)next->pgd;
136 activate_context(next);
137 }
138#ifdef CONFIG_SMP
139 else
Rusty Russell49b92052009-09-24 09:34:49 -0600140 if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 activate_context(next);
142#endif /* CONFIG_SMP */
143}
144
145#define deactivate_mm(tsk, mm) do { } while (0)
146
147#define activate_mm(prev, next) \
148 switch_mm((prev), (next), NULL)
149
Hirokazu Takatafabb6262007-02-10 01:43:40 -0800150#else /* not CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#define get_mmu_context(mm) do { } while (0)
152#define init_new_context(tsk,mm) (0)
153#define destroy_context(mm) do { } while (0)
154#define set_asid(asid) do { } while (0)
155#define get_asid() (0)
156#define activate_context(mm) do { } while (0)
157#define switch_mm(prev,next,tsk) do { } while (0)
158#define deactivate_mm(mm,tsk) do { } while (0)
159#define activate_mm(prev,next) do { } while (0)
160#define enter_lazy_tlb(mm,tsk) do { } while (0)
Hirokazu Takatafabb6262007-02-10 01:43:40 -0800161#endif /* not CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163#endif /* not __ASSEMBLY__ */
164
165#endif /* __KERNEL__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif /* _ASM_M32R_MMU_CONTEXT_H */