1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_POWERPC_MMU_CONTEXT_H
3 #define __ASM_POWERPC_MMU_CONTEXT_H
6 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/spinlock.h>
11 #include <asm/cputable.h>
12 #include <asm/cputhreads.h>
15 * Most if the context management is out of line
17 #define init_new_context init_new_context
18 extern int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
19 #define destroy_context destroy_context
20 extern void destroy_context(struct mm_struct *mm);
21 #ifdef CONFIG_SPAPR_TCE_IOMMU
22 struct mm_iommu_table_group_mem_t;
24 extern int isolate_lru_page(struct page *page); /* from internal.h */
25 extern bool mm_iommu_preregistered(struct mm_struct *mm);
26 extern long mm_iommu_new(struct mm_struct *mm,
27 unsigned long ua, unsigned long entries,
28 struct mm_iommu_table_group_mem_t **pmem);
29 extern 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);
32 extern long mm_iommu_put(struct mm_struct *mm,
33 struct mm_iommu_table_group_mem_t *mem);
34 extern void mm_iommu_init(struct mm_struct *mm);
35 extern void mm_iommu_cleanup(struct mm_struct *mm);
36 extern struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
37 unsigned long ua, unsigned long size);
38 extern struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm(
39 struct mm_struct *mm, unsigned long ua, unsigned long size);
40 extern struct mm_iommu_table_group_mem_t *mm_iommu_get(struct mm_struct *mm,
41 unsigned long ua, unsigned long entries);
42 extern long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
43 unsigned long ua, unsigned int pageshift, unsigned long *hpa);
44 extern 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);
46 extern void mm_iommu_ua_mark_dirty_rm(struct mm_struct *mm, unsigned long ua);
47 extern bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
48 unsigned int pageshift, unsigned long *size);
49 extern long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem);
50 extern void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem);
52 static inline bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
53 unsigned int pageshift, unsigned long *size)
57 static inline void mm_iommu_init(struct mm_struct *mm) { }
59 extern void switch_slb(struct task_struct *tsk, struct mm_struct *mm);
60 extern void set_context(unsigned long id, pgd_t *pgd);
62 #ifdef CONFIG_PPC_BOOK3S_64
63 extern void radix__switch_mmu_context(struct mm_struct *prev,
64 struct mm_struct *next);
65 static inline void switch_mmu_context(struct mm_struct *prev,
66 struct mm_struct *next,
67 struct task_struct *tsk)
70 return radix__switch_mmu_context(prev, next);
71 return switch_slb(tsk, next);
74 extern int hash__alloc_context_id(void);
75 extern void hash__reserve_context_id(int id);
76 extern void __destroy_context(int context_id);
77 static inline void mmu_context_init(void) { }
79 static inline int alloc_extended_context(struct mm_struct *mm,
84 int index = ea >> MAX_EA_BITS_PER_CONTEXT;
86 context_id = hash__alloc_context_id();
90 VM_WARN_ON(mm->context.extended_id[index]);
91 mm->context.extended_id[index] = context_id;
95 static inline bool need_extra_context(struct mm_struct *mm, unsigned long ea)
99 context_id = get_user_context(&mm->context, ea);
106 extern void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next,
107 struct task_struct *tsk);
108 extern unsigned long __init_new_context(void);
109 extern void __destroy_context(unsigned long context_id);
110 extern void mmu_context_init(void);
111 static inline int alloc_extended_context(struct mm_struct *mm,
114 /* non book3s_64 should never find this called */
119 static inline bool need_extra_context(struct mm_struct *mm, unsigned long ea)
125 #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE) && defined(CONFIG_PPC_RADIX_MMU)
126 extern void radix_kvm_prefetch_workaround(struct mm_struct *mm);
128 static inline void radix_kvm_prefetch_workaround(struct mm_struct *mm) { }
131 extern void switch_cop(struct mm_struct *next);
132 extern int use_cop(unsigned long acop, struct mm_struct *mm);
133 extern void drop_cop(unsigned long acop, struct mm_struct *mm);
135 #ifdef CONFIG_PPC_BOOK3S_64
136 static inline void inc_mm_active_cpus(struct mm_struct *mm)
138 atomic_inc(&mm->context.active_cpus);
141 static inline void dec_mm_active_cpus(struct mm_struct *mm)
143 atomic_dec(&mm->context.active_cpus);
146 static inline void mm_context_add_copro(struct mm_struct *mm)
149 * If any copro is in use, increment the active CPU count
150 * in order to force TLB invalidations to be global as to
151 * propagate to the Nest MMU.
153 if (atomic_inc_return(&mm->context.copros) == 1)
154 inc_mm_active_cpus(mm);
157 static inline void mm_context_remove_copro(struct mm_struct *mm)
162 * When removing the last copro, we need to broadcast a global
163 * flush of the full mm, as the next TLBI may be local and the
164 * nMMU and/or PSL need to be cleaned up.
166 * Both the 'copros' and 'active_cpus' counts are looked at in
167 * flush_all_mm() to determine the scope (local/global) of the
168 * TLBIs, so we need to flush first before decrementing
169 * 'copros'. If this API is used by several callers for the
170 * same context, it can lead to over-flushing. It's hopefully
171 * not common enough to be a problem.
173 * Skip on hash, as we don't know how to do the proper flush
174 * for the time being. Invalidations will remain global if
175 * used on hash. Note that we can't drop 'copros' either, as
176 * it could make some invalidations local with no flush
179 if (radix_enabled()) {
182 c = atomic_dec_if_positive(&mm->context.copros);
183 /* Detect imbalance between add and remove */
187 dec_mm_active_cpus(mm);
192 * vas_windows counter shows number of open windows in the mm
193 * context. During context switch, use this counter to clear the
194 * foreign real address mapping (CP_ABORT) for the thread / process
195 * that intend to use COPY/PASTE. When a process closes all windows,
196 * disable CP_ABORT which is expensive to run.
198 * For user context, register a copro so that TLBIs are seen by the
199 * nest MMU. mm_context_add/remove_vas_window() are used only for user
202 static inline void mm_context_add_vas_window(struct mm_struct *mm)
204 atomic_inc(&mm->context.vas_windows);
205 mm_context_add_copro(mm);
208 static inline void mm_context_remove_vas_window(struct mm_struct *mm)
212 mm_context_remove_copro(mm);
213 v = atomic_dec_if_positive(&mm->context.vas_windows);
215 /* Detect imbalance between add and remove */
219 static inline void inc_mm_active_cpus(struct mm_struct *mm) { }
220 static inline void dec_mm_active_cpus(struct mm_struct *mm) { }
221 static inline void mm_context_add_copro(struct mm_struct *mm) { }
222 static inline void mm_context_remove_copro(struct mm_struct *mm) { }
226 extern void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
227 struct task_struct *tsk);
229 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
230 struct task_struct *tsk)
234 local_irq_save(flags);
235 switch_mm_irqs_off(prev, next, tsk);
236 local_irq_restore(flags);
238 #define switch_mm_irqs_off switch_mm_irqs_off
241 * After we have set current->mm to a new value, this activates
242 * the context for the new mm so we see the new mappings.
244 #define activate_mm activate_mm
245 static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next)
247 switch_mm_irqs_off(prev, next, current);
250 /* We don't currently use enter_lazy_tlb() for anything */
251 #ifdef CONFIG_PPC_BOOK3E_64
252 #define enter_lazy_tlb enter_lazy_tlb
253 static inline void enter_lazy_tlb(struct mm_struct *mm,
254 struct task_struct *tsk)
256 /* 64-bit Book3E keeps track of current PGD in the PACA */
257 get_paca()->pgd = NULL;
261 extern void arch_exit_mmap(struct mm_struct *mm);
263 static inline void arch_unmap(struct mm_struct *mm,
264 unsigned long start, unsigned long end)
266 unsigned long vdso_base = (unsigned long)mm->context.vdso - PAGE_SIZE;
268 if (start <= vdso_base && vdso_base < end)
269 mm->context.vdso = NULL;
272 #ifdef CONFIG_PPC_MEM_KEYS
273 bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
274 bool execute, bool foreign);
275 void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm);
276 #else /* CONFIG_PPC_MEM_KEYS */
277 static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
278 bool write, bool execute, bool foreign)
280 /* by default, allow everything */
284 #define pkey_mm_init(mm)
285 #define arch_dup_pkeys(oldmm, mm)
287 static inline u64 pte_to_hpte_pkey_bits(u64 pteflags, unsigned long flags)
292 #endif /* CONFIG_PPC_MEM_KEYS */
294 static inline int arch_dup_mmap(struct mm_struct *oldmm,
295 struct mm_struct *mm)
297 arch_dup_pkeys(oldmm, mm);
301 #include <asm-generic/mmu_context.h>
303 #endif /* __KERNEL__ */
304 #endif /* __ASM_POWERPC_MMU_CONTEXT_H */