blob: 896efa55999694cdad22f92615d073468d7cfe3b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Paul Mackerras047ea782005-11-19 20:17:32 +11002#ifndef __ASM_POWERPC_MMU_CONTEXT_H
3#define __ASM_POWERPC_MMU_CONTEXT_H
Arnd Bergmann88ced032005-12-16 22:43:46 +01004#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +00006#include <linux/kernel.h>
7#include <linux/mm.h>
8#include <linux/sched.h>
9#include <linux/spinlock.h>
Kumar Gala80a7cc62007-07-03 03:22:05 -050010#include <asm/mmu.h>
11#include <asm/cputable.h>
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +000012#include <asm/cputhreads.h>
Kumar Gala80a7cc62007-07-03 03:22:05 -050013
14/*
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +000015 * Most if the context management is out of line
Kumar Gala80a7cc62007-07-03 03:22:05 -050016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017extern int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
18extern void destroy_context(struct mm_struct *mm);
Alexey Kardashevskiy15b244a2015-06-05 16:35:24 +100019#ifdef CONFIG_SPAPR_TCE_IOMMU
20struct mm_iommu_table_group_mem_t;
21
Balbir Singh2e5bbb52016-09-06 16:27:31 +100022extern int isolate_lru_page(struct page *page); /* from internal.h */
Alexey Kardashevskiyd7baee62016-11-30 17:52:00 +110023extern bool mm_iommu_preregistered(struct mm_struct *mm);
24extern long mm_iommu_get(struct mm_struct *mm,
25 unsigned long ua, unsigned long entries,
Alexey Kardashevskiy15b244a2015-06-05 16:35:24 +100026 struct mm_iommu_table_group_mem_t **pmem);
Alexey Kardashevskiyd7baee62016-11-30 17:52:00 +110027extern long mm_iommu_put(struct mm_struct *mm,
28 struct mm_iommu_table_group_mem_t *mem);
Alexey Kardashevskiy88f54a32016-11-30 17:51:59 +110029extern void mm_iommu_init(struct mm_struct *mm);
30extern void mm_iommu_cleanup(struct mm_struct *mm);
Alexey Kardashevskiyd7baee62016-11-30 17:52:00 +110031extern struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
32 unsigned long ua, unsigned long size);
Alexey Kardashevskiy6b5c19c2017-03-22 15:21:47 +110033extern struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm(
34 struct mm_struct *mm, unsigned long ua, unsigned long size);
Alexey Kardashevskiyd7baee62016-11-30 17:52:00 +110035extern struct mm_iommu_table_group_mem_t *mm_iommu_find(struct mm_struct *mm,
36 unsigned long ua, unsigned long entries);
Alexey Kardashevskiy15b244a2015-06-05 16:35:24 +100037extern long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
38 unsigned long ua, unsigned long *hpa);
Alexey Kardashevskiy6b5c19c2017-03-22 15:21:47 +110039extern long mm_iommu_ua_to_hpa_rm(struct mm_iommu_table_group_mem_t *mem,
40 unsigned long ua, unsigned long *hpa);
Alexey Kardashevskiy15b244a2015-06-05 16:35:24 +100041extern long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem);
42extern void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem);
43#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070044extern void switch_slb(struct task_struct *tsk, struct mm_struct *mm);
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +000045extern void set_context(unsigned long id, pgd_t *pgd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Benjamin Herrenschmidt6f0ef0f2009-07-23 23:15:26 +000047#ifdef CONFIG_PPC_BOOK3S_64
Aneesh Kumar K.V7e381c02016-04-29 23:26:02 +100048extern void radix__switch_mmu_context(struct mm_struct *prev,
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +100049 struct mm_struct *next);
Aneesh Kumar K.Vd2adba32016-04-29 23:26:01 +100050static inline void switch_mmu_context(struct mm_struct *prev,
51 struct mm_struct *next,
52 struct task_struct *tsk)
53{
Aneesh Kumar K.V7e381c02016-04-29 23:26:02 +100054 if (radix_enabled())
55 return radix__switch_mmu_context(prev, next);
Aneesh Kumar K.Vd2adba32016-04-29 23:26:01 +100056 return switch_slb(tsk, next);
57}
58
Michael Ellermana336f2f2017-03-29 22:00:46 +110059extern int hash__alloc_context_id(void);
Aneesh Kumar K.V82228e32017-03-22 09:07:00 +053060extern void hash__reserve_context_id(int id);
Alexander Grafe85a4712009-11-02 12:02:30 +000061extern void __destroy_context(int context_id);
Benjamin Herrenschmidt6f0ef0f2009-07-23 23:15:26 +000062static inline void mmu_context_init(void) { }
Aneesh Kumar K.Vf384796c2018-03-26 15:34:48 +053063
64static inline int alloc_extended_context(struct mm_struct *mm,
65 unsigned long ea)
66{
67 int context_id;
68
69 int index = ea >> MAX_EA_BITS_PER_CONTEXT;
70
71 context_id = hash__alloc_context_id();
72 if (context_id < 0)
73 return context_id;
74
75 VM_WARN_ON(mm->context.extended_id[index]);
76 mm->context.extended_id[index] = context_id;
77 return context_id;
78}
79
80static inline bool need_extra_context(struct mm_struct *mm, unsigned long ea)
81{
82 int context_id;
83
84 context_id = get_ea_context(&mm->context, ea);
85 if (!context_id)
86 return true;
87 return false;
88}
89
Benjamin Herrenschmidt6f0ef0f2009-07-23 23:15:26 +000090#else
Aneesh Kumar K.Vd2adba32016-04-29 23:26:01 +100091extern void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next,
92 struct task_struct *tsk);
Alexander Grafc83ec262010-04-16 00:11:36 +020093extern unsigned long __init_new_context(void);
94extern void __destroy_context(unsigned long context_id);
Benjamin Herrenschmidt6f0ef0f2009-07-23 23:15:26 +000095extern void mmu_context_init(void);
Aneesh Kumar K.Vf384796c2018-03-26 15:34:48 +053096static inline int alloc_extended_context(struct mm_struct *mm,
97 unsigned long ea)
98{
99 /* non book3s_64 should never find this called */
100 WARN_ON(1);
101 return -ENOMEM;
102}
103
104static inline bool need_extra_context(struct mm_struct *mm, unsigned long ea)
105{
106 return false;
107}
Benjamin Herrenschmidt6f0ef0f2009-07-23 23:15:26 +0000108#endif
109
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +1000110#if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE) && defined(CONFIG_PPC_RADIX_MMU)
111extern void radix_kvm_prefetch_workaround(struct mm_struct *mm);
112#else
113static inline void radix_kvm_prefetch_workaround(struct mm_struct *mm) { }
114#endif
115
Tseng-Hui (Frank) Lin851d2e22011-05-02 20:43:04 +0000116extern void switch_cop(struct mm_struct *next);
117extern int use_cop(unsigned long acop, struct mm_struct *mm);
118extern void drop_cop(unsigned long acop, struct mm_struct *mm);
119
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200120#ifdef CONFIG_PPC_BOOK3S_64
121static inline void inc_mm_active_cpus(struct mm_struct *mm)
122{
123 atomic_inc(&mm->context.active_cpus);
124}
125
126static inline void dec_mm_active_cpus(struct mm_struct *mm)
127{
128 atomic_dec(&mm->context.active_cpus);
129}
130
131static inline void mm_context_add_copro(struct mm_struct *mm)
132{
133 /*
Benjamin Herrenschmidtaff6f8c2018-03-23 09:29:05 +1100134 * If any copro is in use, increment the active CPU count
135 * in order to force TLB invalidations to be global as to
136 * propagate to the Nest MMU.
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200137 */
Benjamin Herrenschmidtaff6f8c2018-03-23 09:29:05 +1100138 if (atomic_inc_return(&mm->context.copros) == 1)
139 inc_mm_active_cpus(mm);
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200140}
141
142static inline void mm_context_remove_copro(struct mm_struct *mm)
143{
Benjamin Herrenschmidtaff6f8c2018-03-23 09:29:05 +1100144 int c;
145
146 c = atomic_dec_if_positive(&mm->context.copros);
147
148 /* Detect imbalance between add and remove */
149 WARN_ON(c < 0);
150
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200151 /*
152 * Need to broadcast a global flush of the full mm before
153 * decrementing active_cpus count, as the next TLBI may be
154 * local and the nMMU and/or PSL need to be cleaned up.
155 * Should be rare enough so that it's acceptable.
156 *
157 * Skip on hash, as we don't know how to do the proper flush
158 * for the time being. Invalidations will remain global if
159 * used on hash.
160 */
Benjamin Herrenschmidtaff6f8c2018-03-23 09:29:05 +1100161 if (c == 0 && radix_enabled()) {
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200162 flush_all_mm(mm);
163 dec_mm_active_cpus(mm);
164 }
165}
166#else
167static inline void inc_mm_active_cpus(struct mm_struct *mm) { }
168static inline void dec_mm_active_cpus(struct mm_struct *mm) { }
169static inline void mm_context_add_copro(struct mm_struct *mm) { }
170static inline void mm_context_remove_copro(struct mm_struct *mm) { }
171#endif
172
173
Benjamin Herrenschmidt3a2df372017-07-24 14:28:03 +1000174extern void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
175 struct task_struct *tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
David Gibson9765ad12017-04-19 16:38:26 +1000177static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
178 struct task_struct *tsk)
179{
180 unsigned long flags;
181
182 local_irq_save(flags);
183 switch_mm_irqs_off(prev, next, tsk);
184 local_irq_restore(flags);
185}
186#define switch_mm_irqs_off switch_mm_irqs_off
187
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define deactivate_mm(tsk,mm) do { } while (0)
190
191/*
192 * After we have set current->mm to a new value, this activates
193 * the context for the new mm so we see the new mappings.
194 */
195static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next)
196{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 switch_mm(prev, next, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +0000200/* We don't currently use enter_lazy_tlb() for anything */
201static inline void enter_lazy_tlb(struct mm_struct *mm,
202 struct task_struct *tsk)
203{
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000204 /* 64-bit Book3E keeps track of current PGD in the PACA */
205#ifdef CONFIG_PPC_BOOK3E_64
206 get_paca()->pgd = NULL;
207#endif
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +0000208}
209
Thomas Gleixnerc10e83f2017-12-14 12:27:29 +0100210static inline int arch_dup_mmap(struct mm_struct *oldmm,
211 struct mm_struct *mm)
Laurent Dufour83d3f0e2015-06-24 16:56:22 -0700212{
Thomas Gleixnerc10e83f2017-12-14 12:27:29 +0100213 return 0;
Laurent Dufour83d3f0e2015-06-24 16:56:22 -0700214}
215
Nicholas Piggin30b49ec2017-10-24 23:06:54 +1000216#ifndef CONFIG_PPC_BOOK3S_64
Laurent Dufour83d3f0e2015-06-24 16:56:22 -0700217static inline void arch_exit_mmap(struct mm_struct *mm)
218{
219}
Nicholas Piggin30b49ec2017-10-24 23:06:54 +1000220#else
221extern void arch_exit_mmap(struct mm_struct *mm);
222#endif
Laurent Dufour83d3f0e2015-06-24 16:56:22 -0700223
224static inline void arch_unmap(struct mm_struct *mm,
225 struct vm_area_struct *vma,
226 unsigned long start, unsigned long end)
227{
228 if (start <= mm->context.vdso_base && mm->context.vdso_base < end)
229 mm->context.vdso_base = 0;
230}
231
232static inline void arch_bprm_mm_init(struct mm_struct *mm,
233 struct vm_area_struct *vma)
234{
235}
236
Ram Pai11375732018-01-18 17:50:39 -0800237#ifdef CONFIG_PPC_MEM_KEYS
238bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
239 bool execute, bool foreign);
240#else /* CONFIG_PPC_MEM_KEYS */
Dave Hansen1b2ee122016-02-12 13:02:21 -0800241static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
Dave Hansend61172b2016-02-12 13:02:24 -0800242 bool write, bool execute, bool foreign)
Dave Hansen33a709b2016-02-12 13:02:19 -0800243{
244 /* by default, allow everything */
245 return true;
246}
Ram Pai92e3da32018-01-18 17:50:24 -0800247
Ram Pai4fb158f2018-01-18 17:50:25 -0800248#define pkey_mm_init(mm)
Ram Pai06bb53b2018-01-18 17:50:31 -0800249#define thread_pkey_regs_save(thread)
250#define thread_pkey_regs_restore(new_thread, old_thread)
251#define thread_pkey_regs_init(thread)
Ram Pai87bbabb2018-01-18 17:50:34 -0800252
Ram Paia6590ca2018-01-18 17:50:36 -0800253static inline u64 pte_to_hpte_pkey_bits(u64 pteflags)
254{
255 return 0x0UL;
256}
257
Ram Pai4fb158f2018-01-18 17:50:25 -0800258#endif /* CONFIG_PPC_MEM_KEYS */
259
Arnd Bergmann88ced032005-12-16 22:43:46 +0100260#endif /* __KERNEL__ */
Paul Mackerras047ea782005-11-19 20:17:32 +1100261#endif /* __ASM_POWERPC_MMU_CONTEXT_H */