]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/mm/tlb-radix.c
powerpc/mm/radix: Add tlb flush of THP ptes
[karo-tx-linux.git] / arch / powerpc / mm / tlb-radix.c
1 /*
2  * TLB flush routines for radix kernels.
3  *
4  * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/mm.h>
13 #include <linux/hugetlb.h>
14 #include <linux/memblock.h>
15 #include <asm/ppc-opcode.h>
16
17 #include <asm/tlb.h>
18 #include <asm/tlbflush.h>
19
20 static DEFINE_RAW_SPINLOCK(native_tlbie_lock);
21
22 #define RIC_FLUSH_TLB 0
23 #define RIC_FLUSH_PWC 1
24 #define RIC_FLUSH_ALL 2
25
26 static inline void __tlbiel_pid(unsigned long pid, int set,
27                                 unsigned long ric)
28 {
29         unsigned long rb,rs,prs,r;
30
31         rb = PPC_BIT(53); /* IS = 1 */
32         rb |= set << PPC_BITLSHIFT(51);
33         rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
34         prs = 1; /* process scoped */
35         r = 1;   /* raidx format */
36
37         asm volatile("ptesync": : :"memory");
38         asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
39                      : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
40         asm volatile("ptesync": : :"memory");
41 }
42
43 /*
44  * We use 128 set in radix mode and 256 set in hpt mode.
45  */
46 static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
47 {
48         int set;
49
50         for (set = 0; set < POWER9_TLB_SETS_RADIX ; set++) {
51                 __tlbiel_pid(pid, set, ric);
52         }
53         return;
54 }
55
56 static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
57 {
58         unsigned long rb,rs,prs,r;
59
60         rb = PPC_BIT(53); /* IS = 1 */
61         rs = pid << PPC_BITLSHIFT(31);
62         prs = 1; /* process scoped */
63         r = 1;   /* raidx format */
64
65         asm volatile("ptesync": : :"memory");
66         asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
67                      : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
68         asm volatile("eieio; tlbsync; ptesync": : :"memory");
69 }
70
71 static inline void _tlbiel_va(unsigned long va, unsigned long pid,
72                               unsigned long ap, unsigned long ric)
73 {
74         unsigned long rb,rs,prs,r;
75
76         rb = va & ~(PPC_BITMASK(52, 63));
77         rb |= ap << PPC_BITLSHIFT(58);
78         rs = pid << PPC_BITLSHIFT(31);
79         prs = 1; /* process scoped */
80         r = 1;   /* raidx format */
81
82         asm volatile("ptesync": : :"memory");
83         asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
84                      : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
85         asm volatile("ptesync": : :"memory");
86 }
87
88 static inline void _tlbie_va(unsigned long va, unsigned long pid,
89                              unsigned long ap, unsigned long ric)
90 {
91         unsigned long rb,rs,prs,r;
92
93         rb = va & ~(PPC_BITMASK(52, 63));
94         rb |= ap << PPC_BITLSHIFT(58);
95         rs = pid << PPC_BITLSHIFT(31);
96         prs = 1; /* process scoped */
97         r = 1;   /* raidx format */
98
99         asm volatile("ptesync": : :"memory");
100         asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
101                      : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
102         asm volatile("eieio; tlbsync; ptesync": : :"memory");
103 }
104
105 /*
106  * Base TLB flushing operations:
107  *
108  *  - flush_tlb_mm(mm) flushes the specified mm context TLB's
109  *  - flush_tlb_page(vma, vmaddr) flushes one page
110  *  - flush_tlb_range(vma, start, end) flushes a range of pages
111  *  - flush_tlb_kernel_range(start, end) flushes kernel pages
112  *
113  *  - local_* variants of page and mm only apply to the current
114  *    processor
115  */
116 void radix__local_flush_tlb_mm(struct mm_struct *mm)
117 {
118         unsigned long pid;
119
120         preempt_disable();
121         pid = mm->context.id;
122         if (pid != MMU_NO_CONTEXT)
123                 _tlbiel_pid(pid, RIC_FLUSH_ALL);
124         preempt_enable();
125 }
126 EXPORT_SYMBOL(radix__local_flush_tlb_mm);
127
128 void radix__local_flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
129 {
130         unsigned long pid;
131         struct mm_struct *mm = tlb->mm;
132
133         preempt_disable();
134
135         pid = mm->context.id;
136         if (pid != MMU_NO_CONTEXT)
137                 _tlbiel_pid(pid, RIC_FLUSH_PWC);
138
139         preempt_enable();
140 }
141 EXPORT_SYMBOL(radix__local_flush_tlb_pwc);
142
143 void radix___local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
144                             unsigned long ap, int nid)
145 {
146         unsigned long pid;
147
148         preempt_disable();
149         pid = mm ? mm->context.id : 0;
150         if (pid != MMU_NO_CONTEXT)
151                 _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
152         preempt_enable();
153 }
154
155 void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
156 {
157 #ifdef CONFIG_HUGETLB_PAGE
158         /* need the return fix for nohash.c */
159         if (vma && is_vm_hugetlb_page(vma))
160                 return __local_flush_hugetlb_page(vma, vmaddr);
161 #endif
162         radix___local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
163                                mmu_get_ap(mmu_virtual_psize), 0);
164 }
165 EXPORT_SYMBOL(radix__local_flush_tlb_page);
166
167 #ifdef CONFIG_SMP
168 void radix__flush_tlb_mm(struct mm_struct *mm)
169 {
170         unsigned long pid;
171
172         preempt_disable();
173         pid = mm->context.id;
174         if (unlikely(pid == MMU_NO_CONTEXT))
175                 goto no_context;
176
177         if (!mm_is_core_local(mm)) {
178                 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
179
180                 if (lock_tlbie)
181                         raw_spin_lock(&native_tlbie_lock);
182                 _tlbie_pid(pid, RIC_FLUSH_ALL);
183                 if (lock_tlbie)
184                         raw_spin_unlock(&native_tlbie_lock);
185         } else
186                 _tlbiel_pid(pid, RIC_FLUSH_ALL);
187 no_context:
188         preempt_enable();
189 }
190 EXPORT_SYMBOL(radix__flush_tlb_mm);
191
192 void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
193 {
194         unsigned long pid;
195         struct mm_struct *mm = tlb->mm;
196
197         preempt_disable();
198
199         pid = mm->context.id;
200         if (unlikely(pid == MMU_NO_CONTEXT))
201                 goto no_context;
202
203         if (!mm_is_core_local(mm)) {
204                 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
205
206                 if (lock_tlbie)
207                         raw_spin_lock(&native_tlbie_lock);
208                 _tlbie_pid(pid, RIC_FLUSH_PWC);
209                 if (lock_tlbie)
210                         raw_spin_unlock(&native_tlbie_lock);
211         } else
212                 _tlbiel_pid(pid, RIC_FLUSH_PWC);
213 no_context:
214         preempt_enable();
215 }
216 EXPORT_SYMBOL(radix__flush_tlb_pwc);
217
218 void radix___flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
219                        unsigned long ap, int nid)
220 {
221         unsigned long pid;
222
223         preempt_disable();
224         pid = mm ? mm->context.id : 0;
225         if (unlikely(pid == MMU_NO_CONTEXT))
226                 goto bail;
227         if (!mm_is_core_local(mm)) {
228                 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
229
230                 if (lock_tlbie)
231                         raw_spin_lock(&native_tlbie_lock);
232                 _tlbie_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
233                 if (lock_tlbie)
234                         raw_spin_unlock(&native_tlbie_lock);
235         } else
236                 _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
237 bail:
238         preempt_enable();
239 }
240
241 void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
242 {
243 #ifdef CONFIG_HUGETLB_PAGE
244         if (vma && is_vm_hugetlb_page(vma))
245                 return flush_hugetlb_page(vma, vmaddr);
246 #endif
247         radix___flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
248                          mmu_get_ap(mmu_virtual_psize), 0);
249 }
250 EXPORT_SYMBOL(radix__flush_tlb_page);
251
252 #endif /* CONFIG_SMP */
253
254 void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
255 {
256         int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
257
258         if (lock_tlbie)
259                 raw_spin_lock(&native_tlbie_lock);
260         _tlbie_pid(0, RIC_FLUSH_ALL);
261         if (lock_tlbie)
262                 raw_spin_unlock(&native_tlbie_lock);
263 }
264 EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
265
266 /*
267  * Currently, for range flushing, we just do a full mm flush. Because
268  * we use this in code path where we don' track the page size.
269  */
270 void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
271                      unsigned long end)
272
273 {
274         struct mm_struct *mm = vma->vm_mm;
275         radix__flush_tlb_mm(mm);
276 }
277 EXPORT_SYMBOL(radix__flush_tlb_range);
278
279 static int radix_get_mmu_psize(int page_size)
280 {
281         int psize;
282
283         if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
284                 psize = mmu_virtual_psize;
285         else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
286                 psize = MMU_PAGE_2M;
287         else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
288                 psize = MMU_PAGE_1G;
289         else
290                 return -1;
291         return psize;
292 }
293
294 void radix__tlb_flush(struct mmu_gather *tlb)
295 {
296         int psize = 0;
297         struct mm_struct *mm = tlb->mm;
298         int page_size = tlb->page_size;
299
300         psize = radix_get_mmu_psize(page_size);
301         /*
302          * if page size is not something we understand, do a full mm flush
303          */
304         if (psize != -1 && !tlb->fullmm && !tlb->need_flush_all)
305                 radix__flush_tlb_range_psize(mm, tlb->start, tlb->end, psize);
306         else
307                 radix__flush_tlb_mm(mm);
308 }
309
310 #define TLB_FLUSH_ALL -1UL
311 /*
312  * Number of pages above which we will do a bcast tlbie. Just a
313  * number at this point copied from x86
314  */
315 static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
316
317 void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
318                                   unsigned long end, int psize)
319 {
320         unsigned long pid;
321         unsigned long addr;
322         int local = mm_is_core_local(mm);
323         unsigned long ap = mmu_get_ap(psize);
324         int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
325         unsigned long page_size = 1UL << mmu_psize_defs[psize].shift;
326
327
328         preempt_disable();
329         pid = mm ? mm->context.id : 0;
330         if (unlikely(pid == MMU_NO_CONTEXT))
331                 goto err_out;
332
333         if (end == TLB_FLUSH_ALL ||
334             (end - start) > tlb_single_page_flush_ceiling * page_size) {
335                 if (local)
336                         _tlbiel_pid(pid, RIC_FLUSH_TLB);
337                 else
338                         _tlbie_pid(pid, RIC_FLUSH_TLB);
339                 goto err_out;
340         }
341         for (addr = start; addr < end; addr += page_size) {
342
343                 if (local)
344                         _tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
345                 else {
346                         if (lock_tlbie)
347                                 raw_spin_lock(&native_tlbie_lock);
348                         _tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
349                         if (lock_tlbie)
350                                 raw_spin_unlock(&native_tlbie_lock);
351                 }
352         }
353 err_out:
354         preempt_enable();
355 }
356
357 void radix__flush_tlb_lpid_va(unsigned long lpid, unsigned long gpa,
358                               unsigned long page_size)
359 {
360         unsigned long rb,rs,prs,r;
361         unsigned long ap;
362         unsigned long ric = RIC_FLUSH_TLB;
363
364         ap = mmu_get_ap(radix_get_mmu_psize(page_size));
365         rb = gpa & ~(PPC_BITMASK(52, 63));
366         rb |= ap << PPC_BITLSHIFT(58);
367         rs = lpid & ((1UL << 32) - 1);
368         prs = 0; /* process scoped */
369         r = 1;   /* raidx format */
370
371         asm volatile("ptesync": : :"memory");
372         asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
373                      : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
374         asm volatile("eieio; tlbsync; ptesync": : :"memory");
375 }
376 EXPORT_SYMBOL(radix__flush_tlb_lpid_va);
377
378 void radix__flush_tlb_lpid(unsigned long lpid)
379 {
380         unsigned long rb,rs,prs,r;
381         unsigned long ric = RIC_FLUSH_ALL;
382
383         rb = 0x2 << PPC_BITLSHIFT(53); /* IS = 2 */
384         rs = lpid & ((1UL << 32) - 1);
385         prs = 0; /* partition scoped */
386         r = 1;   /* raidx format */
387
388         asm volatile("ptesync": : :"memory");
389         asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
390                      : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
391         asm volatile("eieio; tlbsync; ptesync": : :"memory");
392 }
393 EXPORT_SYMBOL(radix__flush_tlb_lpid);
394
395 void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
396                                 unsigned long start, unsigned long end)
397 {
398         radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
399 }
400 EXPORT_SYMBOL(radix__flush_pmd_tlb_range);