1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TLB flush routines for radix kernels.
5 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
9 #include <linux/hugetlb.h>
10 #include <linux/memblock.h>
11 #include <linux/mmu_context.h>
12 #include <linux/sched/mm.h>
13 #include <linux/debugfs.h>
15 #include <asm/ppc-opcode.h>
17 #include <asm/tlbflush.h>
18 #include <asm/trace.h>
19 #include <asm/cputhreads.h>
20 #include <asm/plpar_wrappers.h>
25 * tlbiel instruction for radix, set invalidation
26 * i.e., r=1 and is=01 or is=10 or is=11
28 static __always_inline void tlbiel_radix_set_isa300(unsigned int set, unsigned int is,
30 unsigned int ric, unsigned int prs)
35 rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
36 rs = ((unsigned long)pid << PPC_BITLSHIFT(31));
38 asm volatile(PPC_TLBIEL(%0, %1, %2, %3, 1)
39 : : "r"(rb), "r"(rs), "i"(ric), "i"(prs)
43 static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
47 asm volatile("ptesync": : :"memory");
50 * Flush the first set of the TLB, and the entire Page Walk Cache
51 * and partition table entries. Then flush the remaining sets of the
55 if (early_cpu_has_feature(CPU_FTR_HVMODE)) {
56 /* MSR[HV] should flush partition scope translations first. */
57 tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 0);
59 if (!early_cpu_has_feature(CPU_FTR_ARCH_31)) {
60 for (set = 1; set < num_sets; set++)
61 tlbiel_radix_set_isa300(set, is, 0,
66 /* Flush process scoped entries. */
67 tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 1);
69 if (!early_cpu_has_feature(CPU_FTR_ARCH_31)) {
70 for (set = 1; set < num_sets; set++)
71 tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 1);
74 ppc_after_tlbiel_barrier();
77 void radix__tlbiel_all(unsigned int action)
82 case TLB_INVAL_SCOPE_GLOBAL:
85 case TLB_INVAL_SCOPE_LPID:
92 if (early_cpu_has_feature(CPU_FTR_ARCH_300))
93 tlbiel_all_isa300(POWER9_TLB_SETS_RADIX, is);
95 WARN(1, "%s called on pre-POWER9 CPU\n", __func__);
97 asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT "; isync" : : :"memory");
100 static __always_inline void __tlbiel_pid(unsigned long pid, int set,
103 unsigned long rb,rs,prs,r;
105 rb = PPC_BIT(53); /* IS = 1 */
106 rb |= set << PPC_BITLSHIFT(51);
107 rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
108 prs = 1; /* process scoped */
109 r = 1; /* radix format */
111 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
112 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
113 trace_tlbie(0, 1, rb, rs, ric, prs, r);
116 static __always_inline void __tlbie_pid(unsigned long pid, unsigned long ric)
118 unsigned long rb,rs,prs,r;
120 rb = PPC_BIT(53); /* IS = 1 */
121 rs = pid << PPC_BITLSHIFT(31);
122 prs = 1; /* process scoped */
123 r = 1; /* radix format */
125 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
126 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
127 trace_tlbie(0, 0, rb, rs, ric, prs, r);
130 static __always_inline void __tlbie_lpid(unsigned long lpid, unsigned long ric)
132 unsigned long rb,rs,prs,r;
134 rb = PPC_BIT(52); /* IS = 2 */
136 prs = 0; /* partition scoped */
137 r = 1; /* radix format */
139 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
140 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
141 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
144 static __always_inline void __tlbie_lpid_guest(unsigned long lpid, unsigned long ric)
146 unsigned long rb,rs,prs,r;
148 rb = PPC_BIT(52); /* IS = 2 */
150 prs = 1; /* process scoped */
151 r = 1; /* radix format */
153 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
154 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
155 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
158 static __always_inline void __tlbiel_va(unsigned long va, unsigned long pid,
159 unsigned long ap, unsigned long ric)
161 unsigned long rb,rs,prs,r;
163 rb = va & ~(PPC_BITMASK(52, 63));
164 rb |= ap << PPC_BITLSHIFT(58);
165 rs = pid << PPC_BITLSHIFT(31);
166 prs = 1; /* process scoped */
167 r = 1; /* radix format */
169 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
170 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
171 trace_tlbie(0, 1, rb, rs, ric, prs, r);
174 static __always_inline void __tlbie_va(unsigned long va, unsigned long pid,
175 unsigned long ap, unsigned long ric)
177 unsigned long rb,rs,prs,r;
179 rb = va & ~(PPC_BITMASK(52, 63));
180 rb |= ap << PPC_BITLSHIFT(58);
181 rs = pid << PPC_BITLSHIFT(31);
182 prs = 1; /* process scoped */
183 r = 1; /* radix format */
185 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
186 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
187 trace_tlbie(0, 0, rb, rs, ric, prs, r);
190 static __always_inline void __tlbie_lpid_va(unsigned long va, unsigned long lpid,
191 unsigned long ap, unsigned long ric)
193 unsigned long rb,rs,prs,r;
195 rb = va & ~(PPC_BITMASK(52, 63));
196 rb |= ap << PPC_BITLSHIFT(58);
198 prs = 0; /* partition scoped */
199 r = 1; /* radix format */
201 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
202 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
203 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
207 static inline void fixup_tlbie_va(unsigned long va, unsigned long pid,
210 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
211 asm volatile("ptesync": : :"memory");
212 __tlbie_va(va, 0, ap, RIC_FLUSH_TLB);
215 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
216 asm volatile("ptesync": : :"memory");
217 __tlbie_va(va, pid, ap, RIC_FLUSH_TLB);
221 static inline void fixup_tlbie_va_range(unsigned long va, unsigned long pid,
224 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
225 asm volatile("ptesync": : :"memory");
226 __tlbie_pid(0, RIC_FLUSH_TLB);
229 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
230 asm volatile("ptesync": : :"memory");
231 __tlbie_va(va, pid, ap, RIC_FLUSH_TLB);
235 static inline void fixup_tlbie_pid(unsigned long pid)
238 * We can use any address for the invalidation, pick one which is
239 * probably unused as an optimisation.
241 unsigned long va = ((1UL << 52) - 1);
243 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
244 asm volatile("ptesync": : :"memory");
245 __tlbie_pid(0, RIC_FLUSH_TLB);
248 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
249 asm volatile("ptesync": : :"memory");
250 __tlbie_va(va, pid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
254 static inline void fixup_tlbie_lpid_va(unsigned long va, unsigned long lpid,
257 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
258 asm volatile("ptesync": : :"memory");
259 __tlbie_lpid_va(va, 0, ap, RIC_FLUSH_TLB);
262 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
263 asm volatile("ptesync": : :"memory");
264 __tlbie_lpid_va(va, lpid, ap, RIC_FLUSH_TLB);
268 static inline void fixup_tlbie_lpid(unsigned long lpid)
271 * We can use any address for the invalidation, pick one which is
272 * probably unused as an optimisation.
274 unsigned long va = ((1UL << 52) - 1);
276 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
277 asm volatile("ptesync": : :"memory");
278 __tlbie_lpid(0, RIC_FLUSH_TLB);
281 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
282 asm volatile("ptesync": : :"memory");
283 __tlbie_lpid_va(va, lpid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
288 * We use 128 set in radix mode and 256 set in hpt mode.
290 static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
294 asm volatile("ptesync": : :"memory");
299 /* For PWC, only one flush is needed */
300 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
301 ppc_after_tlbiel_barrier();
304 __tlbiel_pid(pid, 0, RIC_FLUSH_TLB);
309 * Flush the first set of the TLB, and if
310 * we're doing a RIC_FLUSH_ALL, also flush
311 * the entire Page Walk Cache.
313 __tlbiel_pid(pid, 0, RIC_FLUSH_ALL);
316 if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
317 /* For the remaining sets, just flush the TLB */
318 for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
319 __tlbiel_pid(pid, set, RIC_FLUSH_TLB);
322 ppc_after_tlbiel_barrier();
323 asm volatile(PPC_RADIX_INVALIDATE_ERAT_USER "; isync" : : :"memory");
326 static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
328 asm volatile("ptesync": : :"memory");
331 * Workaround the fact that the "ric" argument to __tlbie_pid
332 * must be a compile-time constraint to match the "i" constraint
333 * in the asm statement.
337 __tlbie_pid(pid, RIC_FLUSH_TLB);
338 fixup_tlbie_pid(pid);
341 __tlbie_pid(pid, RIC_FLUSH_PWC);
345 __tlbie_pid(pid, RIC_FLUSH_ALL);
346 fixup_tlbie_pid(pid);
348 asm volatile("eieio; tlbsync; ptesync": : :"memory");
356 static void do_tlbiel_pid(void *info)
358 struct tlbiel_pid *t = info;
360 if (t->ric == RIC_FLUSH_TLB)
361 _tlbiel_pid(t->pid, RIC_FLUSH_TLB);
362 else if (t->ric == RIC_FLUSH_PWC)
363 _tlbiel_pid(t->pid, RIC_FLUSH_PWC);
365 _tlbiel_pid(t->pid, RIC_FLUSH_ALL);
368 static inline void _tlbiel_pid_multicast(struct mm_struct *mm,
369 unsigned long pid, unsigned long ric)
371 struct cpumask *cpus = mm_cpumask(mm);
372 struct tlbiel_pid t = { .pid = pid, .ric = ric };
374 on_each_cpu_mask(cpus, do_tlbiel_pid, &t, 1);
376 * Always want the CPU translations to be invalidated with tlbiel in
377 * these paths, so while coprocessors must use tlbie, we can not
378 * optimise away the tlbiel component.
380 if (atomic_read(&mm->context.copros) > 0)
381 _tlbie_pid(pid, RIC_FLUSH_ALL);
384 static inline void _tlbie_lpid(unsigned long lpid, unsigned long ric)
386 asm volatile("ptesync": : :"memory");
389 * Workaround the fact that the "ric" argument to __tlbie_pid
390 * must be a compile-time contraint to match the "i" constraint
391 * in the asm statement.
395 __tlbie_lpid(lpid, RIC_FLUSH_TLB);
396 fixup_tlbie_lpid(lpid);
399 __tlbie_lpid(lpid, RIC_FLUSH_PWC);
403 __tlbie_lpid(lpid, RIC_FLUSH_ALL);
404 fixup_tlbie_lpid(lpid);
406 asm volatile("eieio; tlbsync; ptesync": : :"memory");
409 static __always_inline void _tlbie_lpid_guest(unsigned long lpid, unsigned long ric)
412 * Workaround the fact that the "ric" argument to __tlbie_pid
413 * must be a compile-time contraint to match the "i" constraint
414 * in the asm statement.
418 __tlbie_lpid_guest(lpid, RIC_FLUSH_TLB);
421 __tlbie_lpid_guest(lpid, RIC_FLUSH_PWC);
425 __tlbie_lpid_guest(lpid, RIC_FLUSH_ALL);
427 fixup_tlbie_lpid(lpid);
428 asm volatile("eieio; tlbsync; ptesync": : :"memory");
431 static inline void __tlbiel_va_range(unsigned long start, unsigned long end,
432 unsigned long pid, unsigned long page_size,
436 unsigned long ap = mmu_get_ap(psize);
438 for (addr = start; addr < end; addr += page_size)
439 __tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
442 static __always_inline void _tlbiel_va(unsigned long va, unsigned long pid,
443 unsigned long psize, unsigned long ric)
445 unsigned long ap = mmu_get_ap(psize);
447 asm volatile("ptesync": : :"memory");
448 __tlbiel_va(va, pid, ap, ric);
449 ppc_after_tlbiel_barrier();
452 static inline void _tlbiel_va_range(unsigned long start, unsigned long end,
453 unsigned long pid, unsigned long page_size,
454 unsigned long psize, bool also_pwc)
456 asm volatile("ptesync": : :"memory");
458 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
459 __tlbiel_va_range(start, end, pid, page_size, psize);
460 ppc_after_tlbiel_barrier();
463 static inline void __tlbie_va_range(unsigned long start, unsigned long end,
464 unsigned long pid, unsigned long page_size,
468 unsigned long ap = mmu_get_ap(psize);
470 for (addr = start; addr < end; addr += page_size)
471 __tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
473 fixup_tlbie_va_range(addr - page_size, pid, ap);
476 static __always_inline void _tlbie_va(unsigned long va, unsigned long pid,
477 unsigned long psize, unsigned long ric)
479 unsigned long ap = mmu_get_ap(psize);
481 asm volatile("ptesync": : :"memory");
482 __tlbie_va(va, pid, ap, ric);
483 fixup_tlbie_va(va, pid, ap);
484 asm volatile("eieio; tlbsync; ptesync": : :"memory");
494 static void do_tlbiel_va(void *info)
496 struct tlbiel_va *t = info;
498 if (t->ric == RIC_FLUSH_TLB)
499 _tlbiel_va(t->va, t->pid, t->psize, RIC_FLUSH_TLB);
500 else if (t->ric == RIC_FLUSH_PWC)
501 _tlbiel_va(t->va, t->pid, t->psize, RIC_FLUSH_PWC);
503 _tlbiel_va(t->va, t->pid, t->psize, RIC_FLUSH_ALL);
506 static inline void _tlbiel_va_multicast(struct mm_struct *mm,
507 unsigned long va, unsigned long pid,
508 unsigned long psize, unsigned long ric)
510 struct cpumask *cpus = mm_cpumask(mm);
511 struct tlbiel_va t = { .va = va, .pid = pid, .psize = psize, .ric = ric };
512 on_each_cpu_mask(cpus, do_tlbiel_va, &t, 1);
513 if (atomic_read(&mm->context.copros) > 0)
514 _tlbie_va(va, pid, psize, RIC_FLUSH_TLB);
517 struct tlbiel_va_range {
521 unsigned long page_size;
526 static void do_tlbiel_va_range(void *info)
528 struct tlbiel_va_range *t = info;
530 _tlbiel_va_range(t->start, t->end, t->pid, t->page_size,
531 t->psize, t->also_pwc);
534 static __always_inline void _tlbie_lpid_va(unsigned long va, unsigned long lpid,
535 unsigned long psize, unsigned long ric)
537 unsigned long ap = mmu_get_ap(psize);
539 asm volatile("ptesync": : :"memory");
540 __tlbie_lpid_va(va, lpid, ap, ric);
541 fixup_tlbie_lpid_va(va, lpid, ap);
542 asm volatile("eieio; tlbsync; ptesync": : :"memory");
545 static inline void _tlbie_va_range(unsigned long start, unsigned long end,
546 unsigned long pid, unsigned long page_size,
547 unsigned long psize, bool also_pwc)
549 asm volatile("ptesync": : :"memory");
551 __tlbie_pid(pid, RIC_FLUSH_PWC);
552 __tlbie_va_range(start, end, pid, page_size, psize);
553 asm volatile("eieio; tlbsync; ptesync": : :"memory");
556 static inline void _tlbiel_va_range_multicast(struct mm_struct *mm,
557 unsigned long start, unsigned long end,
558 unsigned long pid, unsigned long page_size,
559 unsigned long psize, bool also_pwc)
561 struct cpumask *cpus = mm_cpumask(mm);
562 struct tlbiel_va_range t = { .start = start, .end = end,
563 .pid = pid, .page_size = page_size,
564 .psize = psize, .also_pwc = also_pwc };
566 on_each_cpu_mask(cpus, do_tlbiel_va_range, &t, 1);
567 if (atomic_read(&mm->context.copros) > 0)
568 _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
572 * Base TLB flushing operations:
574 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
575 * - flush_tlb_page(vma, vmaddr) flushes one page
576 * - flush_tlb_range(vma, start, end) flushes a range of pages
577 * - flush_tlb_kernel_range(start, end) flushes kernel pages
579 * - local_* variants of page and mm only apply to the current
582 void radix__local_flush_tlb_mm(struct mm_struct *mm)
587 pid = mm->context.id;
588 if (pid != MMU_NO_CONTEXT)
589 _tlbiel_pid(pid, RIC_FLUSH_TLB);
592 EXPORT_SYMBOL(radix__local_flush_tlb_mm);
595 void radix__local_flush_all_mm(struct mm_struct *mm)
600 pid = mm->context.id;
601 if (pid != MMU_NO_CONTEXT)
602 _tlbiel_pid(pid, RIC_FLUSH_ALL);
605 EXPORT_SYMBOL(radix__local_flush_all_mm);
607 static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
609 radix__local_flush_all_mm(mm);
611 #endif /* CONFIG_SMP */
613 void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
619 pid = mm->context.id;
620 if (pid != MMU_NO_CONTEXT)
621 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
625 void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
627 #ifdef CONFIG_HUGETLB_PAGE
628 /* need the return fix for nohash.c */
629 if (is_vm_hugetlb_page(vma))
630 return radix__local_flush_hugetlb_page(vma, vmaddr);
632 radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
634 EXPORT_SYMBOL(radix__local_flush_tlb_page);
636 static bool mm_needs_flush_escalation(struct mm_struct *mm)
639 * The P9 nest MMU has issues with the page walk cache caching PTEs
640 * and not flushing them when RIC = 0 for a PID/LPID invalidate.
642 * This may have been fixed in shipping firmware (by disabling PWC
643 * or preventing it from caching PTEs), but until that is confirmed,
644 * this workaround is required - escalate all RIC=0 IS=1/2/3 flushes
647 * POWER10 (and P9P) does not have this problem.
649 if (cpu_has_feature(CPU_FTR_ARCH_31))
651 if (atomic_read(&mm->context.copros) > 0)
657 * If always_flush is true, then flush even if this CPU can't be removed
660 void exit_lazy_flush_tlb(struct mm_struct *mm, bool always_flush)
662 unsigned long pid = mm->context.id;
663 int cpu = smp_processor_id();
666 * A kthread could have done a mmget_not_zero() after the flushing CPU
667 * checked mm_cpumask, and be in the process of kthread_use_mm when
668 * interrupted here. In that case, current->mm will be set to mm,
669 * because kthread_use_mm() setting ->mm and switching to the mm is
670 * done with interrupts off.
672 if (current->mm == mm)
675 if (current->active_mm == mm) {
676 WARN_ON_ONCE(current->mm != NULL);
677 /* Is a kernel thread and is using mm as the lazy tlb */
679 current->active_mm = &init_mm;
680 switch_mm_irqs_off(mm, &init_mm, current);
685 * This IPI may be initiated from any source including those not
686 * running the mm, so there may be a racing IPI that comes after
687 * this one which finds the cpumask already clear. Check and avoid
688 * underflowing the active_cpus count in that case. The race should
689 * not otherwise be a problem, but the TLB must be flushed because
690 * that's what the caller expects.
692 if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
693 atomic_dec(&mm->context.active_cpus);
694 cpumask_clear_cpu(cpu, mm_cpumask(mm));
700 _tlbiel_pid(pid, RIC_FLUSH_ALL);
704 static void do_exit_flush_lazy_tlb(void *arg)
706 struct mm_struct *mm = arg;
707 exit_lazy_flush_tlb(mm, true);
710 static void exit_flush_lazy_tlbs(struct mm_struct *mm)
713 * Would be nice if this was async so it could be run in
714 * parallel with our local flush, but generic code does not
715 * give a good API for it. Could extend the generic code or
716 * make a special powerpc IPI for flushing TLBs.
717 * For now it's not too performance critical.
719 smp_call_function_many(mm_cpumask(mm), do_exit_flush_lazy_tlb,
723 #else /* CONFIG_SMP */
724 static inline void exit_flush_lazy_tlbs(struct mm_struct *mm) { }
725 #endif /* CONFIG_SMP */
727 static DEFINE_PER_CPU(unsigned int, mm_cpumask_trim_clock);
730 * Interval between flushes at which we send out IPIs to check whether the
731 * mm_cpumask can be trimmed for the case where it's not a single-threaded
732 * process flushing its own mm. The intent is to reduce the cost of later
733 * flushes. Don't want this to be so low that it adds noticable cost to TLB
734 * flushing, or so high that it doesn't help reduce global TLBIEs.
736 static unsigned long tlb_mm_cpumask_trim_timer = 1073;
738 static bool tick_and_test_trim_clock(void)
740 if (__this_cpu_inc_return(mm_cpumask_trim_clock) ==
741 tlb_mm_cpumask_trim_timer) {
742 __this_cpu_write(mm_cpumask_trim_clock, 0);
748 enum tlb_flush_type {
754 static enum tlb_flush_type flush_type_needed(struct mm_struct *mm, bool fullmm)
756 int active_cpus = atomic_read(&mm->context.active_cpus);
757 int cpu = smp_processor_id();
759 if (active_cpus == 0)
760 return FLUSH_TYPE_NONE;
761 if (active_cpus == 1 && cpumask_test_cpu(cpu, mm_cpumask(mm))) {
762 if (current->mm != mm) {
764 * Asynchronous flush sources may trim down to nothing
765 * if the process is not running, so occasionally try
768 if (tick_and_test_trim_clock()) {
769 exit_lazy_flush_tlb(mm, true);
770 return FLUSH_TYPE_NONE;
773 return FLUSH_TYPE_LOCAL;
776 /* Coprocessors require TLBIE to invalidate nMMU. */
777 if (atomic_read(&mm->context.copros) > 0)
778 return FLUSH_TYPE_GLOBAL;
781 * In the fullmm case there's no point doing the exit_flush_lazy_tlbs
782 * because the mm is being taken down anyway, and a TLBIE tends to
783 * be faster than an IPI+TLBIEL.
786 return FLUSH_TYPE_GLOBAL;
789 * If we are running the only thread of a single-threaded process,
790 * then we should almost always be able to trim off the rest of the
791 * CPU mask (except in the case of use_mm() races), so always try
794 if (atomic_read(&mm->mm_users) <= 1 && current->mm == mm) {
795 exit_flush_lazy_tlbs(mm);
797 * use_mm() race could prevent IPIs from being able to clear
798 * the cpumask here, however those users are established
799 * after our first check (and so after the PTEs are removed),
800 * and the TLB still gets flushed by the IPI, so this CPU
801 * will only require a local flush.
803 return FLUSH_TYPE_LOCAL;
807 * Occasionally try to trim down the cpumask. It's possible this can
808 * bring the mask to zero, which results in no flush.
810 if (tick_and_test_trim_clock()) {
811 exit_flush_lazy_tlbs(mm);
812 if (current->mm == mm)
813 return FLUSH_TYPE_LOCAL;
814 if (cpumask_test_cpu(cpu, mm_cpumask(mm)))
815 exit_lazy_flush_tlb(mm, true);
816 return FLUSH_TYPE_NONE;
819 return FLUSH_TYPE_GLOBAL;
823 void radix__flush_tlb_mm(struct mm_struct *mm)
826 enum tlb_flush_type type;
828 pid = mm->context.id;
829 if (unlikely(pid == MMU_NO_CONTEXT))
834 * Order loads of mm_cpumask (in flush_type_needed) vs previous
835 * stores to clear ptes before the invalidate. See barrier in
839 type = flush_type_needed(mm, false);
840 if (type == FLUSH_TYPE_LOCAL) {
841 _tlbiel_pid(pid, RIC_FLUSH_TLB);
842 } else if (type == FLUSH_TYPE_GLOBAL) {
843 if (!mmu_has_feature(MMU_FTR_GTSE)) {
844 unsigned long tgt = H_RPTI_TARGET_CMMU;
846 if (atomic_read(&mm->context.copros) > 0)
847 tgt |= H_RPTI_TARGET_NMMU;
848 pseries_rpt_invalidate(pid, tgt, H_RPTI_TYPE_TLB,
849 H_RPTI_PAGE_ALL, 0, -1UL);
850 } else if (cputlb_use_tlbie()) {
851 if (mm_needs_flush_escalation(mm))
852 _tlbie_pid(pid, RIC_FLUSH_ALL);
854 _tlbie_pid(pid, RIC_FLUSH_TLB);
856 _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_TLB);
861 EXPORT_SYMBOL(radix__flush_tlb_mm);
863 static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
866 enum tlb_flush_type type;
868 pid = mm->context.id;
869 if (unlikely(pid == MMU_NO_CONTEXT))
873 smp_mb(); /* see radix__flush_tlb_mm */
874 type = flush_type_needed(mm, fullmm);
875 if (type == FLUSH_TYPE_LOCAL) {
876 _tlbiel_pid(pid, RIC_FLUSH_ALL);
877 } else if (type == FLUSH_TYPE_GLOBAL) {
878 if (!mmu_has_feature(MMU_FTR_GTSE)) {
879 unsigned long tgt = H_RPTI_TARGET_CMMU;
880 unsigned long type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
883 if (atomic_read(&mm->context.copros) > 0)
884 tgt |= H_RPTI_TARGET_NMMU;
885 pseries_rpt_invalidate(pid, tgt, type,
886 H_RPTI_PAGE_ALL, 0, -1UL);
887 } else if (cputlb_use_tlbie())
888 _tlbie_pid(pid, RIC_FLUSH_ALL);
890 _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL);
895 void radix__flush_all_mm(struct mm_struct *mm)
897 __flush_all_mm(mm, false);
899 EXPORT_SYMBOL(radix__flush_all_mm);
901 void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
905 enum tlb_flush_type type;
907 pid = mm->context.id;
908 if (unlikely(pid == MMU_NO_CONTEXT))
912 smp_mb(); /* see radix__flush_tlb_mm */
913 type = flush_type_needed(mm, false);
914 if (type == FLUSH_TYPE_LOCAL) {
915 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
916 } else if (type == FLUSH_TYPE_GLOBAL) {
917 if (!mmu_has_feature(MMU_FTR_GTSE)) {
918 unsigned long tgt, pg_sizes, size;
920 tgt = H_RPTI_TARGET_CMMU;
921 pg_sizes = psize_to_rpti_pgsize(psize);
922 size = 1UL << mmu_psize_to_shift(psize);
924 if (atomic_read(&mm->context.copros) > 0)
925 tgt |= H_RPTI_TARGET_NMMU;
926 pseries_rpt_invalidate(pid, tgt, H_RPTI_TYPE_TLB,
929 } else if (cputlb_use_tlbie())
930 _tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
932 _tlbiel_va_multicast(mm, vmaddr, pid, psize, RIC_FLUSH_TLB);
937 void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
939 #ifdef CONFIG_HUGETLB_PAGE
940 if (is_vm_hugetlb_page(vma))
941 return radix__flush_hugetlb_page(vma, vmaddr);
943 radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
945 EXPORT_SYMBOL(radix__flush_tlb_page);
947 #endif /* CONFIG_SMP */
949 static void do_tlbiel_kernel(void *info)
951 _tlbiel_pid(0, RIC_FLUSH_ALL);
954 static inline void _tlbiel_kernel_broadcast(void)
956 on_each_cpu(do_tlbiel_kernel, NULL, 1);
959 * Coherent accelerators don't refcount kernel memory mappings,
960 * so have to always issue a tlbie for them. This is quite a
963 _tlbie_pid(0, RIC_FLUSH_ALL);
968 * If kernel TLBIs ever become local rather than global, then
969 * drivers/misc/ocxl/link.c:ocxl_link_add_pe will need some work, as it
970 * assumes kernel TLBIs are global.
972 void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
974 if (!mmu_has_feature(MMU_FTR_GTSE)) {
975 unsigned long tgt = H_RPTI_TARGET_CMMU | H_RPTI_TARGET_NMMU;
976 unsigned long type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
979 pseries_rpt_invalidate(0, tgt, type, H_RPTI_PAGE_ALL,
981 } else if (cputlb_use_tlbie())
982 _tlbie_pid(0, RIC_FLUSH_ALL);
984 _tlbiel_kernel_broadcast();
986 EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
988 #define TLB_FLUSH_ALL -1UL
991 * Number of pages above which we invalidate the entire PID rather than
992 * flush individual pages, for local and global flushes respectively.
994 * tlbie goes out to the interconnect and individual ops are more costly.
995 * It also does not iterate over sets like the local tlbiel variant when
996 * invalidating a full PID, so it has a far lower threshold to change from
997 * individual page flushes to full-pid flushes.
999 static u32 tlb_single_page_flush_ceiling __read_mostly = 33;
1000 static u32 tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
1002 static inline void __radix__flush_tlb_range(struct mm_struct *mm,
1003 unsigned long start, unsigned long end)
1006 unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
1007 unsigned long page_size = 1UL << page_shift;
1008 unsigned long nr_pages = (end - start) >> page_shift;
1009 bool fullmm = (end == TLB_FLUSH_ALL);
1010 bool flush_pid, flush_pwc = false;
1011 enum tlb_flush_type type;
1013 pid = mm->context.id;
1014 if (unlikely(pid == MMU_NO_CONTEXT))
1018 smp_mb(); /* see radix__flush_tlb_mm */
1019 type = flush_type_needed(mm, fullmm);
1020 if (type == FLUSH_TYPE_NONE)
1025 else if (type == FLUSH_TYPE_GLOBAL)
1026 flush_pid = nr_pages > tlb_single_page_flush_ceiling;
1028 flush_pid = nr_pages > tlb_local_single_page_flush_ceiling;
1030 * full pid flush already does the PWC flush. if it is not full pid
1031 * flush check the range is more than PMD and force a pwc flush
1032 * mremap() depends on this behaviour.
1034 if (!flush_pid && (end - start) >= PMD_SIZE)
1037 if (!mmu_has_feature(MMU_FTR_GTSE) && type == FLUSH_TYPE_GLOBAL) {
1038 unsigned long type = H_RPTI_TYPE_TLB;
1039 unsigned long tgt = H_RPTI_TARGET_CMMU;
1040 unsigned long pg_sizes = psize_to_rpti_pgsize(mmu_virtual_psize);
1042 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1043 pg_sizes |= psize_to_rpti_pgsize(MMU_PAGE_2M);
1044 if (atomic_read(&mm->context.copros) > 0)
1045 tgt |= H_RPTI_TARGET_NMMU;
1047 type |= H_RPTI_TYPE_PWC;
1048 pseries_rpt_invalidate(pid, tgt, type, pg_sizes, start, end);
1049 } else if (flush_pid) {
1051 * We are now flushing a range larger than PMD size force a RIC_FLUSH_ALL
1053 if (type == FLUSH_TYPE_LOCAL) {
1054 _tlbiel_pid(pid, RIC_FLUSH_ALL);
1056 if (cputlb_use_tlbie()) {
1057 _tlbie_pid(pid, RIC_FLUSH_ALL);
1059 _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL);
1064 unsigned long hstart, hend;
1066 hstart = (start + PMD_SIZE - 1) & PMD_MASK;
1067 hend = end & PMD_MASK;
1068 hflush = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hstart < hend;
1070 if (type == FLUSH_TYPE_LOCAL) {
1071 asm volatile("ptesync": : :"memory");
1073 /* For PWC, only one flush is needed */
1074 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
1075 __tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
1077 __tlbiel_va_range(hstart, hend, pid,
1078 PMD_SIZE, MMU_PAGE_2M);
1079 ppc_after_tlbiel_barrier();
1080 } else if (cputlb_use_tlbie()) {
1081 asm volatile("ptesync": : :"memory");
1083 __tlbie_pid(pid, RIC_FLUSH_PWC);
1084 __tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
1086 __tlbie_va_range(hstart, hend, pid,
1087 PMD_SIZE, MMU_PAGE_2M);
1088 asm volatile("eieio; tlbsync; ptesync": : :"memory");
1090 _tlbiel_va_range_multicast(mm,
1091 start, end, pid, page_size, mmu_virtual_psize, flush_pwc);
1093 _tlbiel_va_range_multicast(mm,
1094 hstart, hend, pid, PMD_SIZE, MMU_PAGE_2M, flush_pwc);
1101 void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
1105 #ifdef CONFIG_HUGETLB_PAGE
1106 if (is_vm_hugetlb_page(vma))
1107 return radix__flush_hugetlb_tlb_range(vma, start, end);
1110 __radix__flush_tlb_range(vma->vm_mm, start, end);
1112 EXPORT_SYMBOL(radix__flush_tlb_range);
1114 static int radix_get_mmu_psize(int page_size)
1118 if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
1119 psize = mmu_virtual_psize;
1120 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
1121 psize = MMU_PAGE_2M;
1122 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
1123 psize = MMU_PAGE_1G;
1130 * Flush partition scoped LPID address translation for all CPUs.
1132 void radix__flush_tlb_lpid_page(unsigned int lpid,
1134 unsigned long page_size)
1136 int psize = radix_get_mmu_psize(page_size);
1138 _tlbie_lpid_va(addr, lpid, psize, RIC_FLUSH_TLB);
1140 EXPORT_SYMBOL_GPL(radix__flush_tlb_lpid_page);
1143 * Flush partition scoped PWC from LPID for all CPUs.
1145 void radix__flush_pwc_lpid(unsigned int lpid)
1147 _tlbie_lpid(lpid, RIC_FLUSH_PWC);
1149 EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);
1152 * Flush partition scoped translations from LPID (=LPIDR)
1154 void radix__flush_all_lpid(unsigned int lpid)
1156 _tlbie_lpid(lpid, RIC_FLUSH_ALL);
1158 EXPORT_SYMBOL_GPL(radix__flush_all_lpid);
1161 * Flush process scoped translations from LPID (=LPIDR)
1163 void radix__flush_all_lpid_guest(unsigned int lpid)
1165 _tlbie_lpid_guest(lpid, RIC_FLUSH_ALL);
1168 void radix__tlb_flush(struct mmu_gather *tlb)
1171 struct mm_struct *mm = tlb->mm;
1172 int page_size = tlb->page_size;
1173 unsigned long start = tlb->start;
1174 unsigned long end = tlb->end;
1177 * if page size is not something we understand, do a full mm flush
1179 * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
1180 * that flushes the process table entry cache upon process teardown.
1181 * See the comment for radix in arch_exit_mmap().
1183 if (tlb->fullmm || tlb->need_flush_all) {
1184 __flush_all_mm(mm, true);
1185 } else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
1186 if (!tlb->freed_tables)
1187 radix__flush_tlb_mm(mm);
1189 radix__flush_all_mm(mm);
1191 if (!tlb->freed_tables)
1192 radix__flush_tlb_range_psize(mm, start, end, psize);
1194 radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
1198 static void __radix__flush_tlb_range_psize(struct mm_struct *mm,
1199 unsigned long start, unsigned long end,
1200 int psize, bool also_pwc)
1203 unsigned int page_shift = mmu_psize_defs[psize].shift;
1204 unsigned long page_size = 1UL << page_shift;
1205 unsigned long nr_pages = (end - start) >> page_shift;
1206 bool fullmm = (end == TLB_FLUSH_ALL);
1208 enum tlb_flush_type type;
1210 pid = mm->context.id;
1211 if (unlikely(pid == MMU_NO_CONTEXT))
1214 fullmm = (end == TLB_FLUSH_ALL);
1217 smp_mb(); /* see radix__flush_tlb_mm */
1218 type = flush_type_needed(mm, fullmm);
1219 if (type == FLUSH_TYPE_NONE)
1224 else if (type == FLUSH_TYPE_GLOBAL)
1225 flush_pid = nr_pages > tlb_single_page_flush_ceiling;
1227 flush_pid = nr_pages > tlb_local_single_page_flush_ceiling;
1229 if (!mmu_has_feature(MMU_FTR_GTSE) && type == FLUSH_TYPE_GLOBAL) {
1230 unsigned long tgt = H_RPTI_TARGET_CMMU;
1231 unsigned long type = H_RPTI_TYPE_TLB;
1232 unsigned long pg_sizes = psize_to_rpti_pgsize(psize);
1235 type |= H_RPTI_TYPE_PWC;
1236 if (atomic_read(&mm->context.copros) > 0)
1237 tgt |= H_RPTI_TARGET_NMMU;
1238 pseries_rpt_invalidate(pid, tgt, type, pg_sizes, start, end);
1239 } else if (flush_pid) {
1240 if (type == FLUSH_TYPE_LOCAL) {
1241 _tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1243 if (cputlb_use_tlbie()) {
1244 if (mm_needs_flush_escalation(mm))
1248 also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1250 _tlbiel_pid_multicast(mm, pid,
1251 also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1256 if (type == FLUSH_TYPE_LOCAL)
1257 _tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
1258 else if (cputlb_use_tlbie())
1259 _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
1261 _tlbiel_va_range_multicast(mm,
1262 start, end, pid, page_size, psize, also_pwc);
1268 void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
1269 unsigned long end, int psize)
1271 return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
1274 void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
1275 unsigned long end, int psize)
1277 __radix__flush_tlb_range_psize(mm, start, end, psize, true);
1280 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1281 void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
1283 unsigned long pid, end;
1284 enum tlb_flush_type type;
1286 pid = mm->context.id;
1287 if (unlikely(pid == MMU_NO_CONTEXT))
1290 /* 4k page size, just blow the world */
1291 if (PAGE_SIZE == 0x1000) {
1292 radix__flush_all_mm(mm);
1296 end = addr + HPAGE_PMD_SIZE;
1298 /* Otherwise first do the PWC, then iterate the pages. */
1300 smp_mb(); /* see radix__flush_tlb_mm */
1301 type = flush_type_needed(mm, false);
1302 if (type == FLUSH_TYPE_LOCAL) {
1303 _tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1304 } else if (type == FLUSH_TYPE_GLOBAL) {
1305 if (!mmu_has_feature(MMU_FTR_GTSE)) {
1306 unsigned long tgt, type, pg_sizes;
1308 tgt = H_RPTI_TARGET_CMMU;
1309 type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
1311 pg_sizes = psize_to_rpti_pgsize(mmu_virtual_psize);
1313 if (atomic_read(&mm->context.copros) > 0)
1314 tgt |= H_RPTI_TARGET_NMMU;
1315 pseries_rpt_invalidate(pid, tgt, type, pg_sizes,
1317 } else if (cputlb_use_tlbie())
1318 _tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1320 _tlbiel_va_range_multicast(mm,
1321 addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1326 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1328 void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
1329 unsigned long start, unsigned long end)
1331 radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
1333 EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
1335 void radix__flush_tlb_all(void)
1337 unsigned long rb,prs,r,rs;
1338 unsigned long ric = RIC_FLUSH_ALL;
1340 rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
1341 prs = 0; /* partition scoped */
1342 r = 1; /* radix format */
1343 rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
1345 asm volatile("ptesync": : :"memory");
1347 * now flush guest entries by passing PRS = 1 and LPID != 0
1349 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1350 : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
1352 * now flush host entires by passing PRS = 0 and LPID == 0
1354 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1355 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
1356 asm volatile("eieio; tlbsync; ptesync": : :"memory");
1359 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1360 static __always_inline void __tlbie_pid_lpid(unsigned long pid,
1364 unsigned long rb, rs, prs, r;
1366 rb = PPC_BIT(53); /* IS = 1 */
1367 rs = (pid << PPC_BITLSHIFT(31)) | (lpid & ~(PPC_BITMASK(0, 31)));
1368 prs = 1; /* process scoped */
1369 r = 1; /* radix format */
1371 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1372 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
1373 trace_tlbie(0, 0, rb, rs, ric, prs, r);
1376 static __always_inline void __tlbie_va_lpid(unsigned long va, unsigned long pid,
1378 unsigned long ap, unsigned long ric)
1380 unsigned long rb, rs, prs, r;
1382 rb = va & ~(PPC_BITMASK(52, 63));
1383 rb |= ap << PPC_BITLSHIFT(58);
1384 rs = (pid << PPC_BITLSHIFT(31)) | (lpid & ~(PPC_BITMASK(0, 31)));
1385 prs = 1; /* process scoped */
1386 r = 1; /* radix format */
1388 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1389 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
1390 trace_tlbie(0, 0, rb, rs, ric, prs, r);
1393 static inline void fixup_tlbie_pid_lpid(unsigned long pid, unsigned long lpid)
1396 * We can use any address for the invalidation, pick one which is
1397 * probably unused as an optimisation.
1399 unsigned long va = ((1UL << 52) - 1);
1401 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
1402 asm volatile("ptesync" : : : "memory");
1403 __tlbie_pid_lpid(0, lpid, RIC_FLUSH_TLB);
1406 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
1407 asm volatile("ptesync" : : : "memory");
1408 __tlbie_va_lpid(va, pid, lpid, mmu_get_ap(MMU_PAGE_64K),
1413 static inline void _tlbie_pid_lpid(unsigned long pid, unsigned long lpid,
1416 asm volatile("ptesync" : : : "memory");
1419 * Workaround the fact that the "ric" argument to __tlbie_pid
1420 * must be a compile-time contraint to match the "i" constraint
1421 * in the asm statement.
1425 __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
1426 fixup_tlbie_pid_lpid(pid, lpid);
1429 __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
1433 __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_ALL);
1434 fixup_tlbie_pid_lpid(pid, lpid);
1436 asm volatile("eieio; tlbsync; ptesync" : : : "memory");
1439 static inline void fixup_tlbie_va_range_lpid(unsigned long va,
1444 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
1445 asm volatile("ptesync" : : : "memory");
1446 __tlbie_pid_lpid(0, lpid, RIC_FLUSH_TLB);
1449 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
1450 asm volatile("ptesync" : : : "memory");
1451 __tlbie_va_lpid(va, pid, lpid, ap, RIC_FLUSH_TLB);
1455 static inline void __tlbie_va_range_lpid(unsigned long start, unsigned long end,
1456 unsigned long pid, unsigned long lpid,
1457 unsigned long page_size,
1458 unsigned long psize)
1461 unsigned long ap = mmu_get_ap(psize);
1463 for (addr = start; addr < end; addr += page_size)
1464 __tlbie_va_lpid(addr, pid, lpid, ap, RIC_FLUSH_TLB);
1466 fixup_tlbie_va_range_lpid(addr - page_size, pid, lpid, ap);
1469 static inline void _tlbie_va_range_lpid(unsigned long start, unsigned long end,
1470 unsigned long pid, unsigned long lpid,
1471 unsigned long page_size,
1472 unsigned long psize, bool also_pwc)
1474 asm volatile("ptesync" : : : "memory");
1476 __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
1477 __tlbie_va_range_lpid(start, end, pid, lpid, page_size, psize);
1478 asm volatile("eieio; tlbsync; ptesync" : : : "memory");
1482 * Performs process-scoped invalidations for a given LPID
1483 * as part of H_RPT_INVALIDATE hcall.
1485 void do_h_rpt_invalidate_prt(unsigned long pid, unsigned long lpid,
1486 unsigned long type, unsigned long pg_sizes,
1487 unsigned long start, unsigned long end)
1489 unsigned long psize, nr_pages;
1490 struct mmu_psize_def *def;
1494 * A H_RPTI_TYPE_ALL request implies RIC=3, hence
1495 * do a single IS=1 based flush.
1497 if ((type & H_RPTI_TYPE_ALL) == H_RPTI_TYPE_ALL) {
1498 _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_ALL);
1502 if (type & H_RPTI_TYPE_PWC)
1503 _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
1505 /* Full PID flush */
1506 if (start == 0 && end == -1)
1507 return _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
1509 /* Do range invalidation for all the valid page sizes */
1510 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1511 def = &mmu_psize_defs[psize];
1512 if (!(pg_sizes & def->h_rpt_pgsize))
1515 nr_pages = (end - start) >> def->shift;
1516 flush_pid = nr_pages > tlb_single_page_flush_ceiling;
1519 * If the number of pages spanning the range is above
1520 * the ceiling, convert the request into a full PID flush.
1521 * And since PID flush takes out all the page sizes, there
1522 * is no need to consider remaining page sizes.
1525 _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
1528 _tlbie_va_range_lpid(start, end, pid, lpid,
1529 (1UL << def->shift), psize, false);
1532 EXPORT_SYMBOL_GPL(do_h_rpt_invalidate_prt);
1534 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1536 static int __init create_tlb_single_page_flush_ceiling(void)
1538 debugfs_create_u32("tlb_single_page_flush_ceiling", 0600,
1539 arch_debugfs_dir, &tlb_single_page_flush_ceiling);
1540 debugfs_create_u32("tlb_local_single_page_flush_ceiling", 0600,
1541 arch_debugfs_dir, &tlb_local_single_page_flush_ceiling);
1544 late_initcall(create_tlb_single_page_flush_ceiling);