GNU Linux-libre 4.14.265-gnu1
[releases.git] / arch / mn10300 / include / asm / mmu_context.h
1 /* MN10300 MMU context management
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Modified by David Howells (dhowells@redhat.com)
5  * - Derived from include/asm-m32r/mmu_context.h
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public Licence
9  * as published by the Free Software Foundation; either version
10  * 2 of the Licence, or (at your option) any later version.
11  *
12  *
13  * This implements an algorithm to provide TLB PID mappings to provide
14  * selective access to the TLB for processes, thus reducing the number of TLB
15  * flushes required.
16  *
17  * Note, however, that the M32R algorithm is technically broken as it does not
18  * handle version wrap-around, and could, theoretically, have a problem with a
19  * very long lived program that sleeps long enough for the version number to
20  * wrap all the way around so that its TLB mappings appear valid once again.
21  */
22 #ifndef _ASM_MMU_CONTEXT_H
23 #define _ASM_MMU_CONTEXT_H
24
25 #include <linux/atomic.h>
26 #include <linux/mm_types.h>
27
28 #include <asm/pgalloc.h>
29 #include <asm/tlbflush.h>
30 #include <asm-generic/mm_hooks.h>
31
32 #define MMU_CONTEXT_TLBPID_NR           256
33 #define MMU_CONTEXT_TLBPID_MASK         0x000000ffUL
34 #define MMU_CONTEXT_VERSION_MASK        0xffffff00UL
35 #define MMU_CONTEXT_FIRST_VERSION       0x00000100UL
36 #define MMU_NO_CONTEXT                  0x00000000UL
37 #define MMU_CONTEXT_TLBPID_LOCK_NR      0
38
39 #define enter_lazy_tlb(mm, tsk) do {} while (0)
40
41 static inline void cpu_ran_vm(int cpu, struct mm_struct *mm)
42 {
43 #ifdef CONFIG_SMP
44         cpumask_set_cpu(cpu, mm_cpumask(mm));
45 #endif
46 }
47
48 static inline bool cpu_maybe_ran_vm(int cpu, struct mm_struct *mm)
49 {
50 #ifdef CONFIG_SMP
51         return cpumask_test_and_set_cpu(cpu, mm_cpumask(mm));
52 #else
53         return true;
54 #endif
55 }
56
57 #ifdef CONFIG_MN10300_TLB_USE_PIDR
58 extern unsigned long mmu_context_cache[NR_CPUS];
59 #define mm_context(mm)  (mm->context.tlbpid[smp_processor_id()])
60
61 /**
62  * allocate_mmu_context - Allocate storage for the arch-specific MMU data
63  * @mm: The userspace VM context being set up
64  */
65 static inline unsigned long allocate_mmu_context(struct mm_struct *mm)
66 {
67         unsigned long *pmc = &mmu_context_cache[smp_processor_id()];
68         unsigned long mc = ++(*pmc);
69
70         if (!(mc & MMU_CONTEXT_TLBPID_MASK)) {
71                 /* we exhausted the TLB PIDs of this version on this CPU, so we
72                  * flush this CPU's TLB in its entirety and start new cycle */
73                 local_flush_tlb_all();
74
75                 /* fix the TLB version if needed (we avoid version #0 so as to
76                  * distinguish MMU_NO_CONTEXT) */
77                 if (!mc)
78                         *pmc = mc = MMU_CONTEXT_FIRST_VERSION;
79         }
80         mm_context(mm) = mc;
81         return mc;
82 }
83
84 /*
85  * get an MMU context if one is needed
86  */
87 static inline unsigned long get_mmu_context(struct mm_struct *mm)
88 {
89         unsigned long mc = MMU_NO_CONTEXT, cache;
90
91         if (mm) {
92                 cache = mmu_context_cache[smp_processor_id()];
93                 mc = mm_context(mm);
94
95                 /* if we have an old version of the context, replace it */
96                 if ((mc ^ cache) & MMU_CONTEXT_VERSION_MASK)
97                         mc = allocate_mmu_context(mm);
98         }
99         return mc;
100 }
101
102 /*
103  * initialise the context related info for a new mm_struct instance
104  */
105 static inline int init_new_context(struct task_struct *tsk,
106                                    struct mm_struct *mm)
107 {
108         int num_cpus = NR_CPUS, i;
109
110         for (i = 0; i < num_cpus; i++)
111                 mm->context.tlbpid[i] = MMU_NO_CONTEXT;
112         return 0;
113 }
114
115 /*
116  * after we have set current->mm to a new value, this activates the context for
117  * the new mm so we see the new mappings.
118  */
119 static inline void activate_context(struct mm_struct *mm)
120 {
121         PIDR = get_mmu_context(mm) & MMU_CONTEXT_TLBPID_MASK;
122 }
123 #else  /* CONFIG_MN10300_TLB_USE_PIDR */
124
125 #define init_new_context(tsk, mm)       (0)
126 #define activate_context(mm)            local_flush_tlb()
127
128 #endif /* CONFIG_MN10300_TLB_USE_PIDR */
129
130 /**
131  * destroy_context - Destroy mm context information
132  * @mm: The MM being destroyed.
133  *
134  * Destroy context related info for an mm_struct that is about to be put to
135  * rest
136  */
137 #define destroy_context(mm)     do {} while (0)
138
139 /**
140  * switch_mm - Change between userspace virtual memory contexts
141  * @prev: The outgoing MM context.
142  * @next: The incoming MM context.
143  * @tsk: The incoming task.
144  */
145 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
146                              struct task_struct *tsk)
147 {
148         int cpu = smp_processor_id();
149
150         if (prev != next) {
151 #ifdef CONFIG_SMP
152                 per_cpu(cpu_tlbstate, cpu).active_mm = next;
153 #endif
154                 cpu_ran_vm(cpu, next);
155                 PTBR = (unsigned long) next->pgd;
156                 activate_context(next);
157         }
158 }
159
160 #define deactivate_mm(tsk, mm)  do {} while (0)
161 #define activate_mm(prev, next) switch_mm((prev), (next), NULL)
162
163 #endif /* _ASM_MMU_CONTEXT_H */