2 * TLB flush routines for radix kernels.
4 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
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.
13 #include <linux/hugetlb.h>
14 #include <linux/memblock.h>
16 #include <asm/ppc-opcode.h>
18 #include <asm/tlbflush.h>
19 #include <asm/trace.h>
20 #include <asm/cputhreads.h>
22 #define RIC_FLUSH_TLB 0
23 #define RIC_FLUSH_PWC 1
24 #define RIC_FLUSH_ALL 2
26 static inline void __tlbiel_pid(unsigned long pid, int set,
29 unsigned long rb,rs,prs,r;
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 */
37 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
38 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
39 trace_tlbie(0, 1, rb, rs, ric, prs, r);
42 static inline void __tlbie_pid(unsigned long pid, unsigned long ric)
44 unsigned long rb,rs,prs,r;
46 rb = PPC_BIT(53); /* IS = 1 */
47 rs = pid << PPC_BITLSHIFT(31);
48 prs = 1; /* process scoped */
49 r = 1; /* raidx format */
51 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
52 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
53 trace_tlbie(0, 0, rb, rs, ric, prs, r);
57 * We use 128 set in radix mode and 256 set in hpt mode.
59 static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
63 asm volatile("ptesync": : :"memory");
66 * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
67 * also flush the entire Page Walk Cache.
69 __tlbiel_pid(pid, 0, ric);
71 /* For PWC, only one flush is needed */
72 if (ric == RIC_FLUSH_PWC) {
73 asm volatile("ptesync": : :"memory");
77 /* For the remaining sets, just flush the TLB */
78 for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
79 __tlbiel_pid(pid, set, RIC_FLUSH_TLB);
81 asm volatile("ptesync": : :"memory");
82 asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
85 static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
87 asm volatile("ptesync": : :"memory");
88 __tlbie_pid(pid, ric);
89 asm volatile("eieio; tlbsync; ptesync": : :"memory");
92 static inline void __tlbiel_va(unsigned long va, unsigned long pid,
93 unsigned long ap, unsigned long ric)
95 unsigned long rb,rs,prs,r;
97 rb = va & ~(PPC_BITMASK(52, 63));
98 rb |= ap << PPC_BITLSHIFT(58);
99 rs = pid << PPC_BITLSHIFT(31);
100 prs = 1; /* process scoped */
101 r = 1; /* raidx format */
103 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
104 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
105 trace_tlbie(0, 1, rb, rs, ric, prs, r);
108 static inline void __tlbiel_va_range(unsigned long start, unsigned long end,
109 unsigned long pid, unsigned long page_size,
113 unsigned long ap = mmu_get_ap(psize);
115 for (addr = start; addr < end; addr += page_size)
116 __tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
119 static inline void _tlbiel_va(unsigned long va, unsigned long pid,
120 unsigned long psize, unsigned long ric)
122 unsigned long ap = mmu_get_ap(psize);
124 asm volatile("ptesync": : :"memory");
125 __tlbiel_va(va, pid, ap, ric);
126 asm volatile("ptesync": : :"memory");
129 static inline void _tlbiel_va_range(unsigned long start, unsigned long end,
130 unsigned long pid, unsigned long page_size,
131 unsigned long psize, bool also_pwc)
133 asm volatile("ptesync": : :"memory");
135 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
136 __tlbiel_va_range(start, end, pid, page_size, psize);
137 asm volatile("ptesync": : :"memory");
140 static inline void __tlbie_va(unsigned long va, unsigned long pid,
141 unsigned long ap, unsigned long ric)
143 unsigned long rb,rs,prs,r;
145 rb = va & ~(PPC_BITMASK(52, 63));
146 rb |= ap << PPC_BITLSHIFT(58);
147 rs = pid << PPC_BITLSHIFT(31);
148 prs = 1; /* process scoped */
149 r = 1; /* raidx format */
151 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
152 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
153 trace_tlbie(0, 0, rb, rs, ric, prs, r);
156 static inline void __tlbie_va_range(unsigned long start, unsigned long end,
157 unsigned long pid, unsigned long page_size,
161 unsigned long ap = mmu_get_ap(psize);
163 for (addr = start; addr < end; addr += page_size)
164 __tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
167 static inline void _tlbie_va(unsigned long va, unsigned long pid,
168 unsigned long psize, unsigned long ric)
170 unsigned long ap = mmu_get_ap(psize);
172 asm volatile("ptesync": : :"memory");
173 __tlbie_va(va, pid, ap, ric);
174 asm volatile("eieio; tlbsync; ptesync": : :"memory");
177 static inline void _tlbie_va_range(unsigned long start, unsigned long end,
178 unsigned long pid, unsigned long page_size,
179 unsigned long psize, bool also_pwc)
181 asm volatile("ptesync": : :"memory");
183 __tlbie_pid(pid, RIC_FLUSH_PWC);
184 __tlbie_va_range(start, end, pid, page_size, psize);
185 asm volatile("eieio; tlbsync; ptesync": : :"memory");
189 * Base TLB flushing operations:
191 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
192 * - flush_tlb_page(vma, vmaddr) flushes one page
193 * - flush_tlb_range(vma, start, end) flushes a range of pages
194 * - flush_tlb_kernel_range(start, end) flushes kernel pages
196 * - local_* variants of page and mm only apply to the current
199 void radix__local_flush_tlb_mm(struct mm_struct *mm)
204 pid = mm->context.id;
205 if (pid != MMU_NO_CONTEXT)
206 _tlbiel_pid(pid, RIC_FLUSH_TLB);
209 EXPORT_SYMBOL(radix__local_flush_tlb_mm);
212 void radix__local_flush_all_mm(struct mm_struct *mm)
217 pid = mm->context.id;
218 if (pid != MMU_NO_CONTEXT)
219 _tlbiel_pid(pid, RIC_FLUSH_ALL);
222 EXPORT_SYMBOL(radix__local_flush_all_mm);
223 #endif /* CONFIG_SMP */
225 void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
231 pid = mm->context.id;
232 if (pid != MMU_NO_CONTEXT)
233 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
237 void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
239 #ifdef CONFIG_HUGETLB_PAGE
240 /* need the return fix for nohash.c */
241 if (is_vm_hugetlb_page(vma))
242 return radix__local_flush_hugetlb_page(vma, vmaddr);
244 radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
246 EXPORT_SYMBOL(radix__local_flush_tlb_page);
249 void radix__flush_tlb_mm(struct mm_struct *mm)
253 pid = mm->context.id;
254 if (unlikely(pid == MMU_NO_CONTEXT))
258 if (!mm_is_thread_local(mm))
259 _tlbie_pid(pid, RIC_FLUSH_TLB);
261 _tlbiel_pid(pid, RIC_FLUSH_TLB);
264 EXPORT_SYMBOL(radix__flush_tlb_mm);
266 void radix__flush_all_mm(struct mm_struct *mm)
270 pid = mm->context.id;
271 if (unlikely(pid == MMU_NO_CONTEXT))
275 if (!mm_is_thread_local(mm))
276 _tlbie_pid(pid, RIC_FLUSH_ALL);
278 _tlbiel_pid(pid, RIC_FLUSH_ALL);
281 EXPORT_SYMBOL(radix__flush_all_mm);
283 void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
285 tlb->need_flush_all = 1;
287 EXPORT_SYMBOL(radix__flush_tlb_pwc);
289 void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
294 pid = mm->context.id;
295 if (unlikely(pid == MMU_NO_CONTEXT))
299 if (!mm_is_thread_local(mm))
300 _tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
302 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
306 void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
308 #ifdef CONFIG_HUGETLB_PAGE
309 if (is_vm_hugetlb_page(vma))
310 return radix__flush_hugetlb_page(vma, vmaddr);
312 radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
314 EXPORT_SYMBOL(radix__flush_tlb_page);
316 #else /* CONFIG_SMP */
317 #define radix__flush_all_mm radix__local_flush_all_mm
318 #endif /* CONFIG_SMP */
320 void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
322 _tlbie_pid(0, RIC_FLUSH_ALL);
324 EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
326 #define TLB_FLUSH_ALL -1UL
329 * Number of pages above which we invalidate the entire PID rather than
330 * flush individual pages, for local and global flushes respectively.
332 * tlbie goes out to the interconnect and individual ops are more costly.
333 * It also does not iterate over sets like the local tlbiel variant when
334 * invalidating a full PID, so it has a far lower threshold to change from
335 * individual page flushes to full-pid flushes.
337 static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
338 static unsigned long tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
340 void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
344 struct mm_struct *mm = vma->vm_mm;
346 unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
347 unsigned long page_size = 1UL << page_shift;
348 unsigned long nr_pages = (end - start) >> page_shift;
351 #ifdef CONFIG_HUGETLB_PAGE
352 if (is_vm_hugetlb_page(vma))
353 return radix__flush_hugetlb_tlb_range(vma, start, end);
356 pid = mm->context.id;
357 if (unlikely(pid == MMU_NO_CONTEXT))
361 if (mm_is_thread_local(mm)) {
363 full = (end == TLB_FLUSH_ALL ||
364 nr_pages > tlb_local_single_page_flush_ceiling);
367 full = (end == TLB_FLUSH_ALL ||
368 nr_pages > tlb_single_page_flush_ceiling);
373 _tlbiel_pid(pid, RIC_FLUSH_TLB);
375 _tlbie_pid(pid, RIC_FLUSH_TLB);
378 unsigned long hstart, hend;
380 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
381 hstart = (start + HPAGE_PMD_SIZE - 1) >> HPAGE_PMD_SHIFT;
382 hend = end >> HPAGE_PMD_SHIFT;
384 hstart <<= HPAGE_PMD_SHIFT;
385 hend <<= HPAGE_PMD_SHIFT;
390 asm volatile("ptesync": : :"memory");
392 __tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
394 __tlbiel_va_range(hstart, hend, pid,
395 HPAGE_PMD_SIZE, MMU_PAGE_2M);
396 asm volatile("ptesync": : :"memory");
398 __tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
400 __tlbie_va_range(hstart, hend, pid,
401 HPAGE_PMD_SIZE, MMU_PAGE_2M);
402 asm volatile("eieio; tlbsync; ptesync": : :"memory");
407 EXPORT_SYMBOL(radix__flush_tlb_range);
409 static int radix_get_mmu_psize(int page_size)
413 if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
414 psize = mmu_virtual_psize;
415 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
417 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
424 static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
425 unsigned long end, int psize);
427 void radix__tlb_flush(struct mmu_gather *tlb)
430 struct mm_struct *mm = tlb->mm;
431 int page_size = tlb->page_size;
434 * if page size is not something we understand, do a full mm flush
436 * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
437 * that flushes the process table entry cache upon process teardown.
438 * See the comment for radix in arch_exit_mmap().
441 radix__flush_all_mm(mm);
442 } else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
443 if (!tlb->need_flush_all)
444 radix__flush_tlb_mm(mm);
446 radix__flush_all_mm(mm);
448 unsigned long start = tlb->start;
449 unsigned long end = tlb->end;
451 if (!tlb->need_flush_all)
452 radix__flush_tlb_range_psize(mm, start, end, psize);
454 radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
456 tlb->need_flush_all = 0;
459 static inline void __radix__flush_tlb_range_psize(struct mm_struct *mm,
460 unsigned long start, unsigned long end,
461 int psize, bool also_pwc)
464 unsigned int page_shift = mmu_psize_defs[psize].shift;
465 unsigned long page_size = 1UL << page_shift;
466 unsigned long nr_pages = (end - start) >> page_shift;
469 pid = mm->context.id;
470 if (unlikely(pid == MMU_NO_CONTEXT))
474 if (mm_is_thread_local(mm)) {
476 full = (end == TLB_FLUSH_ALL ||
477 nr_pages > tlb_local_single_page_flush_ceiling);
480 full = (end == TLB_FLUSH_ALL ||
481 nr_pages > tlb_single_page_flush_ceiling);
486 _tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
488 _tlbie_pid(pid, also_pwc ? RIC_FLUSH_ALL: RIC_FLUSH_TLB);
491 _tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
493 _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
498 void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
499 unsigned long end, int psize)
501 return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
504 static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
505 unsigned long end, int psize)
507 __radix__flush_tlb_range_psize(mm, start, end, psize, true);
510 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
511 void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
513 unsigned long pid, end;
515 pid = mm->context.id;
516 if (unlikely(pid == MMU_NO_CONTEXT))
519 /* 4k page size, just blow the world */
520 if (PAGE_SIZE == 0x1000) {
521 radix__flush_all_mm(mm);
525 end = addr + HPAGE_PMD_SIZE;
527 /* Otherwise first do the PWC, then iterate the pages. */
530 if (mm_is_thread_local(mm)) {
531 _tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
533 _tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
538 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
540 void radix__flush_tlb_lpid_va(unsigned long lpid, unsigned long gpa,
541 unsigned long page_size)
543 unsigned long rb,rs,prs,r;
545 unsigned long ric = RIC_FLUSH_TLB;
547 ap = mmu_get_ap(radix_get_mmu_psize(page_size));
548 rb = gpa & ~(PPC_BITMASK(52, 63));
549 rb |= ap << PPC_BITLSHIFT(58);
550 rs = lpid & ((1UL << 32) - 1);
551 prs = 0; /* process scoped */
552 r = 1; /* raidx format */
554 asm volatile("ptesync": : :"memory");
555 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
556 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
557 asm volatile("eieio; tlbsync; ptesync": : :"memory");
558 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
560 EXPORT_SYMBOL(radix__flush_tlb_lpid_va);
562 void radix__flush_tlb_lpid(unsigned long lpid)
564 unsigned long rb,rs,prs,r;
565 unsigned long ric = RIC_FLUSH_ALL;
567 rb = 0x2 << PPC_BITLSHIFT(53); /* IS = 2 */
568 rs = lpid & ((1UL << 32) - 1);
569 prs = 0; /* partition scoped */
570 r = 1; /* raidx format */
572 asm volatile("ptesync": : :"memory");
573 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
574 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
575 asm volatile("eieio; tlbsync; ptesync": : :"memory");
576 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
578 EXPORT_SYMBOL(radix__flush_tlb_lpid);
580 void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
581 unsigned long start, unsigned long end)
583 radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
585 EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
587 void radix__flush_tlb_all(void)
589 unsigned long rb,prs,r,rs;
590 unsigned long ric = RIC_FLUSH_ALL;
592 rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
593 prs = 0; /* partition scoped */
594 r = 1; /* raidx format */
595 rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
597 asm volatile("ptesync": : :"memory");
599 * now flush guest entries by passing PRS = 1 and LPID != 0
601 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
602 : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
603 trace_tlbie(0, 0, rb, rs, ric, prs, r);
605 * now flush host entires by passing PRS = 0 and LPID == 0
607 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
608 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
609 asm volatile("eieio; tlbsync; ptesync": : :"memory");
610 trace_tlbie(0, 0, rb, 0, ric, prs, r);
613 void radix__flush_tlb_pte_p9_dd1(unsigned long old_pte, struct mm_struct *mm,
614 unsigned long address)
617 * We track page size in pte only for DD1, So we can
618 * call this only on DD1.
620 if (!cpu_has_feature(CPU_FTR_POWER9_DD1)) {
625 if (old_pte & R_PAGE_LARGE)
626 radix__flush_tlb_page_psize(mm, address, MMU_PAGE_2M);
628 radix__flush_tlb_page_psize(mm, address, mmu_virtual_psize);
631 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
632 extern void radix_kvm_prefetch_workaround(struct mm_struct *mm)
634 unsigned int pid = mm->context.id;
636 if (unlikely(pid == MMU_NO_CONTEXT))
640 * If this context hasn't run on that CPU before and KVM is
641 * around, there's a slim chance that the guest on another
642 * CPU just brought in obsolete translation into the TLB of
643 * this CPU due to a bad prefetch using the guest PID on
644 * the way into the hypervisor.
646 * We work around this here. If KVM is possible, we check if
647 * any sibling thread is in KVM. If it is, the window may exist
648 * and thus we flush that PID from the core.
650 * A potential future improvement would be to mark which PIDs
651 * have never been used on the system and avoid it if the PID
652 * is new and the process has no other cpumask bit set.
654 if (cpu_has_feature(CPU_FTR_HVMODE) && radix_enabled()) {
655 int cpu = smp_processor_id();
656 int sib = cpu_first_thread_sibling(cpu);
659 for (; sib <= cpu_last_thread_sibling(cpu) && !flush; sib++) {
662 if (paca[sib].kvm_hstate.kvm_vcpu)
666 _tlbiel_pid(pid, RIC_FLUSH_ALL);
669 EXPORT_SYMBOL_GPL(radix_kvm_prefetch_workaround);
670 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */