1
2#ifndef __ASM_POWERPC_MMU_CONTEXT_H
3#define __ASM_POWERPC_MMU_CONTEXT_H
4#ifdef __KERNEL__
5
6#include <linux/kernel.h>
7#include <linux/mm.h>
8#include <linux/sched.h>
9#include <linux/spinlock.h>
10#include <asm/mmu.h>
11#include <asm/cputable.h>
12#include <asm/cputhreads.h>
13
14
15
16
17#define init_new_context init_new_context
18extern int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
19#define destroy_context destroy_context
20extern void destroy_context(struct mm_struct *mm);
21#ifdef CONFIG_SPAPR_TCE_IOMMU
22struct mm_iommu_table_group_mem_t;
23
24extern int isolate_lru_page(struct page *page);
25extern bool mm_iommu_preregistered(struct mm_struct *mm);
26extern long mm_iommu_new(struct mm_struct *mm,
27 unsigned long ua, unsigned long entries,
28 struct mm_iommu_table_group_mem_t **pmem);
29extern long mm_iommu_newdev(struct mm_struct *mm, unsigned long ua,
30 unsigned long entries, unsigned long dev_hpa,
31 struct mm_iommu_table_group_mem_t **pmem);
32extern long mm_iommu_put(struct mm_struct *mm,
33 struct mm_iommu_table_group_mem_t *mem);
34extern void mm_iommu_init(struct mm_struct *mm);
35extern void mm_iommu_cleanup(struct mm_struct *mm);
36extern struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
37 unsigned long ua, unsigned long size);
38extern struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm(
39 struct mm_struct *mm, unsigned long ua, unsigned long size);
40extern struct mm_iommu_table_group_mem_t *mm_iommu_get(struct mm_struct *mm,
41 unsigned long ua, unsigned long entries);
42extern long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
43 unsigned long ua, unsigned int pageshift, unsigned long *hpa);
44extern long mm_iommu_ua_to_hpa_rm(struct mm_iommu_table_group_mem_t *mem,
45 unsigned long ua, unsigned int pageshift, unsigned long *hpa);
46extern void mm_iommu_ua_mark_dirty_rm(struct mm_struct *mm, unsigned long ua);
47extern bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
48 unsigned int pageshift, unsigned long *size);
49extern long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem);
50extern void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem);
51#else
52static inline bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
53 unsigned int pageshift, unsigned long *size)
54{
55 return false;
56}
57static inline void mm_iommu_init(struct mm_struct *mm) { }
58#endif
59extern void switch_slb(struct task_struct *tsk, struct mm_struct *mm);
60
61#ifdef CONFIG_PPC_BOOK3S_64
62extern void radix__switch_mmu_context(struct mm_struct *prev,
63 struct mm_struct *next);
64static inline void switch_mmu_context(struct mm_struct *prev,
65 struct mm_struct *next,
66 struct task_struct *tsk)
67{
68 if (radix_enabled())
69 return radix__switch_mmu_context(prev, next);
70 return switch_slb(tsk, next);
71}
72
73extern int hash__alloc_context_id(void);
74extern void hash__reserve_context_id(int id);
75extern void __destroy_context(int context_id);
76static inline void mmu_context_init(void) { }
77
78static inline int alloc_extended_context(struct mm_struct *mm,
79 unsigned long ea)
80{
81 int context_id;
82
83 int index = ea >> MAX_EA_BITS_PER_CONTEXT;
84
85 context_id = hash__alloc_context_id();
86 if (context_id < 0)
87 return context_id;
88
89 VM_WARN_ON(mm->context.extended_id[index]);
90 mm->context.extended_id[index] = context_id;
91 return context_id;
92}
93
94static inline bool need_extra_context(struct mm_struct *mm, unsigned long ea)
95{
96 int context_id;
97
98 context_id = get_user_context(&mm->context, ea);
99 if (!context_id)
100 return true;
101 return false;
102}
103
104#else
105extern void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next,
106 struct task_struct *tsk);
107extern unsigned long __init_new_context(void);
108extern void __destroy_context(unsigned long context_id);
109extern void mmu_context_init(void);
110static inline int alloc_extended_context(struct mm_struct *mm,
111 unsigned long ea)
112{
113
114 WARN_ON(1);
115 return -ENOMEM;
116}
117
118static inline bool need_extra_context(struct mm_struct *mm, unsigned long ea)
119{
120 return false;
121}
122#endif
123
124extern void switch_cop(struct mm_struct *next);
125extern int use_cop(unsigned long acop, struct mm_struct *mm);
126extern void drop_cop(unsigned long acop, struct mm_struct *mm);
127
128#ifdef CONFIG_PPC_BOOK3S_64
129static inline void inc_mm_active_cpus(struct mm_struct *mm)
130{
131 atomic_inc(&mm->context.active_cpus);
132}
133
134static inline void dec_mm_active_cpus(struct mm_struct *mm)
135{
136 atomic_dec(&mm->context.active_cpus);
137}
138
139static inline void mm_context_add_copro(struct mm_struct *mm)
140{
141
142
143
144
145
146 if (atomic_inc_return(&mm->context.copros) == 1)
147 inc_mm_active_cpus(mm);
148}
149
150static inline void mm_context_remove_copro(struct mm_struct *mm)
151{
152 int c;
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 if (radix_enabled()) {
173 flush_all_mm(mm);
174
175 c = atomic_dec_if_positive(&mm->context.copros);
176
177 WARN_ON(c < 0);
178
179 if (c == 0)
180 dec_mm_active_cpus(mm);
181 }
182}
183
184
185
186
187
188
189
190
191
192
193
194
195static inline void mm_context_add_vas_window(struct mm_struct *mm)
196{
197 atomic_inc(&mm->context.vas_windows);
198 mm_context_add_copro(mm);
199}
200
201static inline void mm_context_remove_vas_window(struct mm_struct *mm)
202{
203 int v;
204
205 mm_context_remove_copro(mm);
206 v = atomic_dec_if_positive(&mm->context.vas_windows);
207
208
209 WARN_ON(v < 0);
210}
211#else
212static inline void inc_mm_active_cpus(struct mm_struct *mm) { }
213static inline void dec_mm_active_cpus(struct mm_struct *mm) { }
214static inline void mm_context_add_copro(struct mm_struct *mm) { }
215static inline void mm_context_remove_copro(struct mm_struct *mm) { }
216#endif
217
218#if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE) && defined(CONFIG_PPC_RADIX_MMU)
219void do_h_rpt_invalidate_prt(unsigned long pid, unsigned long lpid,
220 unsigned long type, unsigned long pg_sizes,
221 unsigned long start, unsigned long end);
222#else
223static inline void do_h_rpt_invalidate_prt(unsigned long pid,
224 unsigned long lpid,
225 unsigned long type,
226 unsigned long pg_sizes,
227 unsigned long start,
228 unsigned long end) { }
229#endif
230
231extern void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
232 struct task_struct *tsk);
233
234static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
235 struct task_struct *tsk)
236{
237 unsigned long flags;
238
239 local_irq_save(flags);
240 switch_mm_irqs_off(prev, next, tsk);
241 local_irq_restore(flags);
242}
243#define switch_mm_irqs_off switch_mm_irqs_off
244
245
246
247
248
249#define activate_mm activate_mm
250static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next)
251{
252 switch_mm_irqs_off(prev, next, current);
253}
254
255
256#ifdef CONFIG_PPC_BOOK3E_64
257#define enter_lazy_tlb enter_lazy_tlb
258static inline void enter_lazy_tlb(struct mm_struct *mm,
259 struct task_struct *tsk)
260{
261
262 get_paca()->pgd = NULL;
263}
264#endif
265
266extern void arch_exit_mmap(struct mm_struct *mm);
267
268static inline void arch_unmap(struct mm_struct *mm,
269 unsigned long start, unsigned long end)
270{
271 unsigned long vdso_base = (unsigned long)mm->context.vdso;
272
273 if (start <= vdso_base && vdso_base < end)
274 mm->context.vdso = NULL;
275}
276
277#ifdef CONFIG_PPC_MEM_KEYS
278bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
279 bool execute, bool foreign);
280void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm);
281#else
282static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
283 bool write, bool execute, bool foreign)
284{
285
286 return true;
287}
288
289#define pkey_mm_init(mm)
290#define arch_dup_pkeys(oldmm, mm)
291
292static inline u64 pte_to_hpte_pkey_bits(u64 pteflags, unsigned long flags)
293{
294 return 0x0UL;
295}
296
297#endif
298
299static inline int arch_dup_mmap(struct mm_struct *oldmm,
300 struct mm_struct *mm)
301{
302 arch_dup_pkeys(oldmm, mm);
303 return 0;
304}
305
306#include <asm-generic/mmu_context.h>
307
308#endif
309#endif
310