blob: da2221a54c953e6e0f6c9153cfeb4590625c3505 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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 <linux/mmu_context.h>
16#include <linux/sched/mm.h>
17
18#include <asm/ppc-opcode.h>
19#include <asm/tlb.h>
20#include <asm/tlbflush.h>
21#include <asm/trace.h>
22#include <asm/cputhreads.h>
23
24#define RIC_FLUSH_TLB 0
25#define RIC_FLUSH_PWC 1
26#define RIC_FLUSH_ALL 2
27
28/*
29 * tlbiel instruction for radix, set invalidation
30 * i.e., r=1 and is=01 or is=10 or is=11
31 */
32static inline void tlbiel_radix_set_isa300(unsigned int set, unsigned int is,
33 unsigned int pid,
34 unsigned int ric, unsigned int prs)
35{
36 unsigned long rb;
37 unsigned long rs;
38
39 rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
40 rs = ((unsigned long)pid << PPC_BITLSHIFT(31));
41
42 asm volatile(PPC_TLBIEL(%0, %1, %2, %3, 1)
43 : : "r"(rb), "r"(rs), "i"(ric), "i"(prs)
44 : "memory");
45}
46
47static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
48{
49 unsigned int set;
50
51 asm volatile("ptesync": : :"memory");
52
53 /*
54 * Flush the first set of the TLB, and the entire Page Walk Cache
55 * and partition table entries. Then flush the remaining sets of the
56 * TLB.
57 */
58 tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 0);
59 for (set = 1; set < num_sets; set++)
60 tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 0);
61
62 /* Do the same for process scoped entries. */
63 tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 1);
64 for (set = 1; set < num_sets; set++)
65 tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 1);
66
67 asm volatile("ptesync": : :"memory");
68}
69
70void radix__tlbiel_all(unsigned int action)
71{
72 unsigned int is;
73
74 switch (action) {
75 case TLB_INVAL_SCOPE_GLOBAL:
76 is = 3;
77 break;
78 case TLB_INVAL_SCOPE_LPID:
79 is = 2;
80 break;
81 default:
82 BUG();
83 }
84
85 if (early_cpu_has_feature(CPU_FTR_ARCH_300))
86 tlbiel_all_isa300(POWER9_TLB_SETS_RADIX, is);
87 else
88 WARN(1, "%s called on pre-POWER9 CPU\n", __func__);
89
90 asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
91}
92
93static __always_inline void __tlbiel_pid(unsigned long pid, int set,
94 unsigned long ric)
95{
96 unsigned long rb,rs,prs,r;
97
98 rb = PPC_BIT(53); /* IS = 1 */
99 rb |= set << PPC_BITLSHIFT(51);
100 rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
101 prs = 1; /* process scoped */
102 r = 1; /* radix format */
103
104 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
105 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
106 trace_tlbie(0, 1, rb, rs, ric, prs, r);
107}
108
109static __always_inline void __tlbie_pid(unsigned long pid, unsigned long ric)
110{
111 unsigned long rb,rs,prs,r;
112
113 rb = PPC_BIT(53); /* IS = 1 */
114 rs = pid << PPC_BITLSHIFT(31);
115 prs = 1; /* process scoped */
116 r = 1; /* radix format */
117
118 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
119 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
120 trace_tlbie(0, 0, rb, rs, ric, prs, r);
121}
122
123static inline void __tlbiel_lpid(unsigned long lpid, int set,
124 unsigned long ric)
125{
126 unsigned long rb,rs,prs,r;
127
128 rb = PPC_BIT(52); /* IS = 2 */
129 rb |= set << PPC_BITLSHIFT(51);
130 rs = 0; /* LPID comes from LPIDR */
131 prs = 0; /* partition scoped */
132 r = 1; /* radix format */
133
134 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
135 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
136 trace_tlbie(lpid, 1, rb, rs, ric, prs, r);
137}
138
139static __always_inline void __tlbie_lpid(unsigned long lpid, unsigned long ric)
140{
141 unsigned long rb,rs,prs,r;
142
143 rb = PPC_BIT(52); /* IS = 2 */
144 rs = lpid;
145 prs = 0; /* partition scoped */
146 r = 1; /* radix format */
147
148 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
149 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
150 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
151}
152
153static inline void __tlbiel_lpid_guest(unsigned long lpid, int set,
154 unsigned long ric)
155{
156 unsigned long rb,rs,prs,r;
157
158 rb = PPC_BIT(52); /* IS = 2 */
159 rb |= set << PPC_BITLSHIFT(51);
160 rs = 0; /* LPID comes from LPIDR */
161 prs = 1; /* process scoped */
162 r = 1; /* radix format */
163
164 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
165 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
166 trace_tlbie(lpid, 1, rb, rs, ric, prs, r);
167}
168
169
170static inline void __tlbiel_va(unsigned long va, unsigned long pid,
171 unsigned long ap, unsigned long ric)
172{
173 unsigned long rb,rs,prs,r;
174
175 rb = va & ~(PPC_BITMASK(52, 63));
176 rb |= ap << PPC_BITLSHIFT(58);
177 rs = pid << PPC_BITLSHIFT(31);
178 prs = 1; /* process scoped */
179 r = 1; /* radix format */
180
181 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
182 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
183 trace_tlbie(0, 1, rb, rs, ric, prs, r);
184}
185
186static inline void __tlbie_va(unsigned long va, unsigned long pid,
187 unsigned long ap, unsigned long ric)
188{
189 unsigned long rb,rs,prs,r;
190
191 rb = va & ~(PPC_BITMASK(52, 63));
192 rb |= ap << PPC_BITLSHIFT(58);
193 rs = pid << PPC_BITLSHIFT(31);
194 prs = 1; /* process scoped */
195 r = 1; /* radix format */
196
197 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
198 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
199 trace_tlbie(0, 0, rb, rs, ric, prs, r);
200}
201
202static inline void __tlbie_lpid_va(unsigned long va, unsigned long lpid,
203 unsigned long ap, unsigned long ric)
204{
205 unsigned long rb,rs,prs,r;
206
207 rb = va & ~(PPC_BITMASK(52, 63));
208 rb |= ap << PPC_BITLSHIFT(58);
209 rs = lpid;
210 prs = 0; /* partition scoped */
211 r = 1; /* radix format */
212
213 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
214 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
215 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
216}
217
218
219static inline void fixup_tlbie_va(unsigned long va, unsigned long pid,
220 unsigned long ap)
221{
222 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
223 asm volatile("ptesync": : :"memory");
224 __tlbie_va(va, 0, ap, RIC_FLUSH_TLB);
225 }
226
227 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
228 asm volatile("ptesync": : :"memory");
229 __tlbie_va(va, pid, ap, RIC_FLUSH_TLB);
230 }
231}
232
233static inline void fixup_tlbie_va_range(unsigned long va, unsigned long pid,
234 unsigned long ap)
235{
236 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
237 asm volatile("ptesync": : :"memory");
238 __tlbie_pid(0, RIC_FLUSH_TLB);
239 }
240
241 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
242 asm volatile("ptesync": : :"memory");
243 __tlbie_va(va, pid, ap, RIC_FLUSH_TLB);
244 }
245}
246
247static inline void fixup_tlbie_pid(unsigned long pid)
248{
249 /*
250 * We can use any address for the invalidation, pick one which is
251 * probably unused as an optimisation.
252 */
253 unsigned long va = ((1UL << 52) - 1);
254
255 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
256 asm volatile("ptesync": : :"memory");
257 __tlbie_pid(0, RIC_FLUSH_TLB);
258 }
259
260 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
261 asm volatile("ptesync": : :"memory");
262 __tlbie_va(va, pid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
263 }
264}
265
266
267static inline void fixup_tlbie_lpid_va(unsigned long va, unsigned long lpid,
268 unsigned long ap)
269{
270 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
271 asm volatile("ptesync": : :"memory");
272 __tlbie_lpid_va(va, 0, ap, RIC_FLUSH_TLB);
273 }
274
275 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
276 asm volatile("ptesync": : :"memory");
277 __tlbie_lpid_va(va, lpid, ap, RIC_FLUSH_TLB);
278 }
279}
280
281static inline void fixup_tlbie_lpid(unsigned long lpid)
282{
283 /*
284 * We can use any address for the invalidation, pick one which is
285 * probably unused as an optimisation.
286 */
287 unsigned long va = ((1UL << 52) - 1);
288
289 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
290 asm volatile("ptesync": : :"memory");
291 __tlbie_lpid(0, RIC_FLUSH_TLB);
292 }
293
294 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
295 asm volatile("ptesync": : :"memory");
296 __tlbie_lpid_va(va, lpid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
297 }
298}
299
300/*
301 * We use 128 set in radix mode and 256 set in hpt mode.
302 */
303static __always_inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
304{
305 int set;
306
307 asm volatile("ptesync": : :"memory");
308
309 /*
310 * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
311 * also flush the entire Page Walk Cache.
312 */
313 __tlbiel_pid(pid, 0, ric);
314
315 /* For PWC, only one flush is needed */
316 if (ric == RIC_FLUSH_PWC) {
317 asm volatile("ptesync": : :"memory");
318 return;
319 }
320
321 /* For the remaining sets, just flush the TLB */
322 for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
323 __tlbiel_pid(pid, set, RIC_FLUSH_TLB);
324
325 asm volatile("ptesync": : :"memory");
326 asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
327}
328
329static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
330{
331 asm volatile("ptesync": : :"memory");
332
333 /*
334 * Workaround the fact that the "ric" argument to __tlbie_pid
335 * must be a compile-time contraint to match the "i" constraint
336 * in the asm statement.
337 */
338 switch (ric) {
339 case RIC_FLUSH_TLB:
340 __tlbie_pid(pid, RIC_FLUSH_TLB);
341 fixup_tlbie_pid(pid);
342 break;
343 case RIC_FLUSH_PWC:
344 __tlbie_pid(pid, RIC_FLUSH_PWC);
345 break;
346 case RIC_FLUSH_ALL:
347 default:
348 __tlbie_pid(pid, RIC_FLUSH_ALL);
349 fixup_tlbie_pid(pid);
350 }
351 asm volatile("eieio; tlbsync; ptesync": : :"memory");
352}
353
354static inline void _tlbiel_lpid(unsigned long lpid, unsigned long ric)
355{
356 int set;
357
358 VM_BUG_ON(mfspr(SPRN_LPID) != lpid);
359
360 asm volatile("ptesync": : :"memory");
361
362 /*
363 * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
364 * also flush the entire Page Walk Cache.
365 */
366 __tlbiel_lpid(lpid, 0, ric);
367
368 /* For PWC, only one flush is needed */
369 if (ric == RIC_FLUSH_PWC) {
370 asm volatile("ptesync": : :"memory");
371 return;
372 }
373
374 /* For the remaining sets, just flush the TLB */
375 for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
376 __tlbiel_lpid(lpid, set, RIC_FLUSH_TLB);
377
378 asm volatile("ptesync": : :"memory");
379 asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
380}
381
382static inline void _tlbie_lpid(unsigned long lpid, unsigned long ric)
383{
384 asm volatile("ptesync": : :"memory");
385
386 /*
387 * Workaround the fact that the "ric" argument to __tlbie_pid
388 * must be a compile-time contraint to match the "i" constraint
389 * in the asm statement.
390 */
391 switch (ric) {
392 case RIC_FLUSH_TLB:
393 __tlbie_lpid(lpid, RIC_FLUSH_TLB);
394 fixup_tlbie_lpid(lpid);
395 break;
396 case RIC_FLUSH_PWC:
397 __tlbie_lpid(lpid, RIC_FLUSH_PWC);
398 break;
399 case RIC_FLUSH_ALL:
400 default:
401 __tlbie_lpid(lpid, RIC_FLUSH_ALL);
402 fixup_tlbie_lpid(lpid);
403 }
404 asm volatile("eieio; tlbsync; ptesync": : :"memory");
405}
406
407static inline void _tlbiel_lpid_guest(unsigned long lpid, unsigned long ric)
408{
409 int set;
410
411 VM_BUG_ON(mfspr(SPRN_LPID) != lpid);
412
413 asm volatile("ptesync": : :"memory");
414
415 /*
416 * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
417 * also flush the entire Page Walk Cache.
418 */
419 __tlbiel_lpid_guest(lpid, 0, ric);
420
421 /* For PWC, only one flush is needed */
422 if (ric == RIC_FLUSH_PWC) {
423 asm volatile("ptesync": : :"memory");
424 return;
425 }
426
427 /* For the remaining sets, just flush the TLB */
428 for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
429 __tlbiel_lpid_guest(lpid, set, RIC_FLUSH_TLB);
430
431 asm volatile("ptesync": : :"memory");
432 asm volatile(PPC_INVALIDATE_ERAT : : :"memory");
433}
434
435
436static inline void __tlbiel_va_range(unsigned long start, unsigned long end,
437 unsigned long pid, unsigned long page_size,
438 unsigned long psize)
439{
440 unsigned long addr;
441 unsigned long ap = mmu_get_ap(psize);
442
443 for (addr = start; addr < end; addr += page_size)
444 __tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
445}
446
447static inline void _tlbiel_va(unsigned long va, unsigned long pid,
448 unsigned long psize, unsigned long ric)
449{
450 unsigned long ap = mmu_get_ap(psize);
451
452 asm volatile("ptesync": : :"memory");
453 __tlbiel_va(va, pid, ap, ric);
454 asm volatile("ptesync": : :"memory");
455}
456
457static inline void _tlbiel_va_range(unsigned long start, unsigned long end,
458 unsigned long pid, unsigned long page_size,
459 unsigned long psize, bool also_pwc)
460{
461 asm volatile("ptesync": : :"memory");
462 if (also_pwc)
463 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
464 __tlbiel_va_range(start, end, pid, page_size, psize);
465 asm volatile("ptesync": : :"memory");
466}
467
468static inline void __tlbie_va_range(unsigned long start, unsigned long end,
469 unsigned long pid, unsigned long page_size,
470 unsigned long psize)
471{
472 unsigned long addr;
473 unsigned long ap = mmu_get_ap(psize);
474
475 for (addr = start; addr < end; addr += page_size)
476 __tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
477
478 fixup_tlbie_va_range(addr - page_size, pid, ap);
479}
480
481static inline void _tlbie_va(unsigned long va, unsigned long pid,
482 unsigned long psize, unsigned long ric)
483{
484 unsigned long ap = mmu_get_ap(psize);
485
486 asm volatile("ptesync": : :"memory");
487 __tlbie_va(va, pid, ap, ric);
488 fixup_tlbie_va(va, pid, ap);
489 asm volatile("eieio; tlbsync; ptesync": : :"memory");
490}
491
492static inline void _tlbie_lpid_va(unsigned long va, unsigned long lpid,
493 unsigned long psize, unsigned long ric)
494{
495 unsigned long ap = mmu_get_ap(psize);
496
497 asm volatile("ptesync": : :"memory");
498 __tlbie_lpid_va(va, lpid, ap, ric);
499 fixup_tlbie_lpid_va(va, lpid, ap);
500 asm volatile("eieio; tlbsync; ptesync": : :"memory");
501}
502
503static inline void _tlbie_va_range(unsigned long start, unsigned long end,
504 unsigned long pid, unsigned long page_size,
505 unsigned long psize, bool also_pwc)
506{
507 asm volatile("ptesync": : :"memory");
508 if (also_pwc)
509 __tlbie_pid(pid, RIC_FLUSH_PWC);
510 __tlbie_va_range(start, end, pid, page_size, psize);
511 asm volatile("eieio; tlbsync; ptesync": : :"memory");
512}
513
514/*
515 * Base TLB flushing operations:
516 *
517 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
518 * - flush_tlb_page(vma, vmaddr) flushes one page
519 * - flush_tlb_range(vma, start, end) flushes a range of pages
520 * - flush_tlb_kernel_range(start, end) flushes kernel pages
521 *
522 * - local_* variants of page and mm only apply to the current
523 * processor
524 */
525void radix__local_flush_tlb_mm(struct mm_struct *mm)
526{
527 unsigned long pid;
528
529 preempt_disable();
530 pid = mm->context.id;
531 if (pid != MMU_NO_CONTEXT)
532 _tlbiel_pid(pid, RIC_FLUSH_TLB);
533 preempt_enable();
534}
535EXPORT_SYMBOL(radix__local_flush_tlb_mm);
536
537#ifndef CONFIG_SMP
538void radix__local_flush_all_mm(struct mm_struct *mm)
539{
540 unsigned long pid;
541
542 preempt_disable();
543 pid = mm->context.id;
544 if (pid != MMU_NO_CONTEXT)
545 _tlbiel_pid(pid, RIC_FLUSH_ALL);
546 preempt_enable();
547}
548EXPORT_SYMBOL(radix__local_flush_all_mm);
549#endif /* CONFIG_SMP */
550
551void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
552 int psize)
553{
554 unsigned long pid;
555
556 preempt_disable();
557 pid = mm->context.id;
558 if (pid != MMU_NO_CONTEXT)
559 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
560 preempt_enable();
561}
562
563void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
564{
565#ifdef CONFIG_HUGETLB_PAGE
566 /* need the return fix for nohash.c */
567 if (is_vm_hugetlb_page(vma))
568 return radix__local_flush_hugetlb_page(vma, vmaddr);
569#endif
570 radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
571}
572EXPORT_SYMBOL(radix__local_flush_tlb_page);
573
574static bool mm_is_singlethreaded(struct mm_struct *mm)
575{
576 if (atomic_read(&mm->context.copros) > 0)
577 return false;
578 if (atomic_read(&mm->mm_users) <= 1 && current->mm == mm)
579 return true;
580 return false;
581}
582
583static bool mm_needs_flush_escalation(struct mm_struct *mm)
584{
585 /*
586 * P9 nest MMU has issues with the page walk cache
587 * caching PTEs and not flushing them properly when
588 * RIC = 0 for a PID/LPID invalidate
589 */
590 if (atomic_read(&mm->context.copros) > 0)
591 return true;
592 return false;
593}
594
595#ifdef CONFIG_SMP
596static void do_exit_flush_lazy_tlb(void *arg)
597{
598 struct mm_struct *mm = arg;
599 unsigned long pid = mm->context.id;
600
601 if (current->mm == mm)
602 return; /* Local CPU */
603
604 if (current->active_mm == mm) {
605 /*
606 * Must be a kernel thread because sender is single-threaded.
607 */
608 BUG_ON(current->mm);
609 mmgrab(&init_mm);
610 switch_mm(mm, &init_mm, current);
611 current->active_mm = &init_mm;
612 mmdrop(mm);
613 }
614 _tlbiel_pid(pid, RIC_FLUSH_ALL);
615}
616
617static void exit_flush_lazy_tlbs(struct mm_struct *mm)
618{
619 /*
620 * Would be nice if this was async so it could be run in
621 * parallel with our local flush, but generic code does not
622 * give a good API for it. Could extend the generic code or
623 * make a special powerpc IPI for flushing TLBs.
624 * For now it's not too performance critical.
625 */
626 smp_call_function_many(mm_cpumask(mm), do_exit_flush_lazy_tlb,
627 (void *)mm, 1);
628 mm_reset_thread_local(mm);
629}
630
631void radix__flush_tlb_mm(struct mm_struct *mm)
632{
633 unsigned long pid;
634
635 pid = mm->context.id;
636 if (unlikely(pid == MMU_NO_CONTEXT))
637 return;
638
639 preempt_disable();
640 /*
641 * Order loads of mm_cpumask vs previous stores to clear ptes before
642 * the invalidate. See barrier in switch_mm_irqs_off
643 */
644 smp_mb();
645 if (!mm_is_thread_local(mm)) {
646 if (unlikely(mm_is_singlethreaded(mm))) {
647 exit_flush_lazy_tlbs(mm);
648 goto local;
649 }
650
651 if (mm_needs_flush_escalation(mm))
652 _tlbie_pid(pid, RIC_FLUSH_ALL);
653 else
654 _tlbie_pid(pid, RIC_FLUSH_TLB);
655 } else {
656local:
657 _tlbiel_pid(pid, RIC_FLUSH_TLB);
658 }
659 preempt_enable();
660}
661EXPORT_SYMBOL(radix__flush_tlb_mm);
662
663static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
664{
665 unsigned long pid;
666
667 pid = mm->context.id;
668 if (unlikely(pid == MMU_NO_CONTEXT))
669 return;
670
671 preempt_disable();
672 smp_mb(); /* see radix__flush_tlb_mm */
673 if (!mm_is_thread_local(mm)) {
674 if (unlikely(mm_is_singlethreaded(mm))) {
675 if (!fullmm) {
676 exit_flush_lazy_tlbs(mm);
677 goto local;
678 }
679 }
680 _tlbie_pid(pid, RIC_FLUSH_ALL);
681 } else {
682local:
683 _tlbiel_pid(pid, RIC_FLUSH_ALL);
684 }
685 preempt_enable();
686}
687void radix__flush_all_mm(struct mm_struct *mm)
688{
689 __flush_all_mm(mm, false);
690}
691EXPORT_SYMBOL(radix__flush_all_mm);
692
693void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
694{
695 tlb->need_flush_all = 1;
696}
697EXPORT_SYMBOL(radix__flush_tlb_pwc);
698
699void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
700 int psize)
701{
702 unsigned long pid;
703
704 pid = mm->context.id;
705 if (unlikely(pid == MMU_NO_CONTEXT))
706 return;
707
708 preempt_disable();
709 smp_mb(); /* see radix__flush_tlb_mm */
710 if (!mm_is_thread_local(mm)) {
711 if (unlikely(mm_is_singlethreaded(mm))) {
712 exit_flush_lazy_tlbs(mm);
713 goto local;
714 }
715 _tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
716 } else {
717local:
718 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
719 }
720 preempt_enable();
721}
722
723void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
724{
725#ifdef CONFIG_HUGETLB_PAGE
726 if (is_vm_hugetlb_page(vma))
727 return radix__flush_hugetlb_page(vma, vmaddr);
728#endif
729 radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
730}
731EXPORT_SYMBOL(radix__flush_tlb_page);
732
733#else /* CONFIG_SMP */
734#define radix__flush_all_mm radix__local_flush_all_mm
735#endif /* CONFIG_SMP */
736
737void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
738{
739 _tlbie_pid(0, RIC_FLUSH_ALL);
740}
741EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
742
743#define TLB_FLUSH_ALL -1UL
744
745/*
746 * Number of pages above which we invalidate the entire PID rather than
747 * flush individual pages, for local and global flushes respectively.
748 *
749 * tlbie goes out to the interconnect and individual ops are more costly.
750 * It also does not iterate over sets like the local tlbiel variant when
751 * invalidating a full PID, so it has a far lower threshold to change from
752 * individual page flushes to full-pid flushes.
753 */
754static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
755static unsigned long tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
756
757static inline void __radix__flush_tlb_range(struct mm_struct *mm,
758 unsigned long start, unsigned long end,
759 bool flush_all_sizes)
760
761{
762 unsigned long pid;
763 unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
764 unsigned long page_size = 1UL << page_shift;
765 unsigned long nr_pages = (end - start) >> page_shift;
766 bool local, full;
767
768 pid = mm->context.id;
769 if (unlikely(pid == MMU_NO_CONTEXT))
770 return;
771
772 preempt_disable();
773 smp_mb(); /* see radix__flush_tlb_mm */
774 if (!mm_is_thread_local(mm)) {
775 if (unlikely(mm_is_singlethreaded(mm))) {
776 if (end != TLB_FLUSH_ALL) {
777 exit_flush_lazy_tlbs(mm);
778 goto is_local;
779 }
780 }
781 local = false;
782 full = (end == TLB_FLUSH_ALL ||
783 nr_pages > tlb_single_page_flush_ceiling);
784 } else {
785is_local:
786 local = true;
787 full = (end == TLB_FLUSH_ALL ||
788 nr_pages > tlb_local_single_page_flush_ceiling);
789 }
790
791 if (full) {
792 if (local) {
793 _tlbiel_pid(pid, RIC_FLUSH_TLB);
794 } else {
795 if (mm_needs_flush_escalation(mm))
796 _tlbie_pid(pid, RIC_FLUSH_ALL);
797 else
798 _tlbie_pid(pid, RIC_FLUSH_TLB);
799 }
800 } else {
801 bool hflush = flush_all_sizes;
802 bool gflush = flush_all_sizes;
803 unsigned long hstart, hend;
804 unsigned long gstart, gend;
805
806 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
807 hflush = true;
808
809 if (hflush) {
810 hstart = (start + PMD_SIZE - 1) & PMD_MASK;
811 hend = end & PMD_MASK;
812 if (hstart == hend)
813 hflush = false;
814 }
815
816 if (gflush) {
817 gstart = (start + PUD_SIZE - 1) & PUD_MASK;
818 gend = end & PUD_MASK;
819 if (gstart == gend)
820 gflush = false;
821 }
822
823 asm volatile("ptesync": : :"memory");
824 if (local) {
825 __tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
826 if (hflush)
827 __tlbiel_va_range(hstart, hend, pid,
828 PMD_SIZE, MMU_PAGE_2M);
829 if (gflush)
830 __tlbiel_va_range(gstart, gend, pid,
831 PUD_SIZE, MMU_PAGE_1G);
832 asm volatile("ptesync": : :"memory");
833 } else {
834 __tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
835 if (hflush)
836 __tlbie_va_range(hstart, hend, pid,
837 PMD_SIZE, MMU_PAGE_2M);
838 if (gflush)
839 __tlbie_va_range(gstart, gend, pid,
840 PUD_SIZE, MMU_PAGE_1G);
841
842 asm volatile("eieio; tlbsync; ptesync": : :"memory");
843 }
844 }
845 preempt_enable();
846}
847
848void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
849 unsigned long end)
850
851{
852#ifdef CONFIG_HUGETLB_PAGE
853 if (is_vm_hugetlb_page(vma))
854 return radix__flush_hugetlb_tlb_range(vma, start, end);
855#endif
856
857 __radix__flush_tlb_range(vma->vm_mm, start, end, false);
858}
859EXPORT_SYMBOL(radix__flush_tlb_range);
860
861static int radix_get_mmu_psize(int page_size)
862{
863 int psize;
864
865 if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
866 psize = mmu_virtual_psize;
867 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
868 psize = MMU_PAGE_2M;
869 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
870 psize = MMU_PAGE_1G;
871 else
872 return -1;
873 return psize;
874}
875
876/*
877 * Flush partition scoped LPID address translation for all CPUs.
878 */
879void radix__flush_tlb_lpid_page(unsigned int lpid,
880 unsigned long addr,
881 unsigned long page_size)
882{
883 int psize = radix_get_mmu_psize(page_size);
884
885 _tlbie_lpid_va(addr, lpid, psize, RIC_FLUSH_TLB);
886}
887EXPORT_SYMBOL_GPL(radix__flush_tlb_lpid_page);
888
889/*
890 * Flush partition scoped PWC from LPID for all CPUs.
891 */
892void radix__flush_pwc_lpid(unsigned int lpid)
893{
894 _tlbie_lpid(lpid, RIC_FLUSH_PWC);
895}
896EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);
897
898/*
899 * Flush partition scoped translations from LPID (=LPIDR)
900 */
901void radix__local_flush_tlb_lpid(unsigned int lpid)
902{
903 _tlbiel_lpid(lpid, RIC_FLUSH_ALL);
904}
905EXPORT_SYMBOL_GPL(radix__local_flush_tlb_lpid);
906
907/*
908 * Flush process scoped translations from LPID (=LPIDR).
909 * Important difference, the guest normally manages its own translations,
910 * but some cases e.g., vCPU CPU migration require KVM to flush.
911 */
912void radix__local_flush_tlb_lpid_guest(unsigned int lpid)
913{
914 _tlbiel_lpid_guest(lpid, RIC_FLUSH_ALL);
915}
916EXPORT_SYMBOL_GPL(radix__local_flush_tlb_lpid_guest);
917
918
919static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
920 unsigned long end, int psize);
921
922void radix__tlb_flush(struct mmu_gather *tlb)
923{
924 int psize = 0;
925 struct mm_struct *mm = tlb->mm;
926 int page_size = tlb->page_size;
927 unsigned long start = tlb->start;
928 unsigned long end = tlb->end;
929
930 /*
931 * if page size is not something we understand, do a full mm flush
932 *
933 * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
934 * that flushes the process table entry cache upon process teardown.
935 * See the comment for radix in arch_exit_mmap().
936 */
937 if (tlb->fullmm) {
938 __flush_all_mm(mm, true);
939#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE)
940 } else if (mm_tlb_flush_nested(mm)) {
941 /*
942 * If there is a concurrent invalidation that is clearing ptes,
943 * then it's possible this invalidation will miss one of those
944 * cleared ptes and miss flushing the TLB. If this invalidate
945 * returns before the other one flushes TLBs, that can result
946 * in it returning while there are still valid TLBs inside the
947 * range to be invalidated.
948 *
949 * See mm/memory.c:tlb_finish_mmu() for more details.
950 *
951 * The solution to this is ensure the entire range is always
952 * flushed here. The problem for powerpc is that the flushes
953 * are page size specific, so this "forced flush" would not
954 * do the right thing if there are a mix of page sizes in
955 * the range to be invalidated. So use __flush_tlb_range
956 * which invalidates all possible page sizes in the range.
957 *
958 * PWC flush probably is not be required because the core code
959 * shouldn't free page tables in this path, but accounting
960 * for the possibility makes us a bit more robust.
961 *
962 * need_flush_all is an uncommon case because page table
963 * teardown should be done with exclusive locks held (but
964 * after locks are dropped another invalidate could come
965 * in), it could be optimized further if necessary.
966 */
967 if (!tlb->need_flush_all)
968 __radix__flush_tlb_range(mm, start, end, true);
969 else
970 radix__flush_all_mm(mm);
971#endif
972 } else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
973 if (!tlb->need_flush_all)
974 radix__flush_tlb_mm(mm);
975 else
976 radix__flush_all_mm(mm);
977 } else {
978 if (!tlb->need_flush_all)
979 radix__flush_tlb_range_psize(mm, start, end, psize);
980 else
981 radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
982 }
983 tlb->need_flush_all = 0;
984}
985
986static __always_inline void __radix__flush_tlb_range_psize(struct mm_struct *mm,
987 unsigned long start, unsigned long end,
988 int psize, bool also_pwc)
989{
990 unsigned long pid;
991 unsigned int page_shift = mmu_psize_defs[psize].shift;
992 unsigned long page_size = 1UL << page_shift;
993 unsigned long nr_pages = (end - start) >> page_shift;
994 bool local, full;
995
996 pid = mm->context.id;
997 if (unlikely(pid == MMU_NO_CONTEXT))
998 return;
999
1000 preempt_disable();
1001 smp_mb(); /* see radix__flush_tlb_mm */
1002 if (!mm_is_thread_local(mm)) {
1003 if (unlikely(mm_is_singlethreaded(mm))) {
1004 if (end != TLB_FLUSH_ALL) {
1005 exit_flush_lazy_tlbs(mm);
1006 goto is_local;
1007 }
1008 }
1009 local = false;
1010 full = (end == TLB_FLUSH_ALL ||
1011 nr_pages > tlb_single_page_flush_ceiling);
1012 } else {
1013is_local:
1014 local = true;
1015 full = (end == TLB_FLUSH_ALL ||
1016 nr_pages > tlb_local_single_page_flush_ceiling);
1017 }
1018
1019 if (full) {
1020 if (local) {
1021 _tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1022 } else {
1023 if (mm_needs_flush_escalation(mm))
1024 also_pwc = true;
1025
1026 _tlbie_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1027 }
1028 } else {
1029 if (local)
1030 _tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
1031 else
1032 _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
1033 }
1034 preempt_enable();
1035}
1036
1037void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
1038 unsigned long end, int psize)
1039{
1040 return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
1041}
1042
1043static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
1044 unsigned long end, int psize)
1045{
1046 __radix__flush_tlb_range_psize(mm, start, end, psize, true);
1047}
1048
1049#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1050void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
1051{
1052 unsigned long pid, end;
1053
1054 pid = mm->context.id;
1055 if (unlikely(pid == MMU_NO_CONTEXT))
1056 return;
1057
1058 /* 4k page size, just blow the world */
1059 if (PAGE_SIZE == 0x1000) {
1060 radix__flush_all_mm(mm);
1061 return;
1062 }
1063
1064 end = addr + HPAGE_PMD_SIZE;
1065
1066 /* Otherwise first do the PWC, then iterate the pages. */
1067 preempt_disable();
1068 smp_mb(); /* see radix__flush_tlb_mm */
1069 if (!mm_is_thread_local(mm)) {
1070 if (unlikely(mm_is_singlethreaded(mm))) {
1071 exit_flush_lazy_tlbs(mm);
1072 goto local;
1073 }
1074 _tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1075 } else {
1076local:
1077 _tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1078 }
1079
1080 preempt_enable();
1081}
1082#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1083
1084void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
1085 unsigned long start, unsigned long end)
1086{
1087 radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
1088}
1089EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
1090
1091void radix__flush_tlb_all(void)
1092{
1093 unsigned long rb,prs,r,rs;
1094 unsigned long ric = RIC_FLUSH_ALL;
1095
1096 rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
1097 prs = 0; /* partition scoped */
1098 r = 1; /* radix format */
1099 rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
1100
1101 asm volatile("ptesync": : :"memory");
1102 /*
1103 * now flush guest entries by passing PRS = 1 and LPID != 0
1104 */
1105 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1106 : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
1107 /*
1108 * now flush host entires by passing PRS = 0 and LPID == 0
1109 */
1110 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1111 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
1112 asm volatile("eieio; tlbsync; ptesync": : :"memory");
1113}
1114
1115#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1116extern void radix_kvm_prefetch_workaround(struct mm_struct *mm)
1117{
1118 unsigned long pid = mm->context.id;
1119
1120 if (unlikely(pid == MMU_NO_CONTEXT))
1121 return;
1122
1123 /*
1124 * If this context hasn't run on that CPU before and KVM is
1125 * around, there's a slim chance that the guest on another
1126 * CPU just brought in obsolete translation into the TLB of
1127 * this CPU due to a bad prefetch using the guest PID on
1128 * the way into the hypervisor.
1129 *
1130 * We work around this here. If KVM is possible, we check if
1131 * any sibling thread is in KVM. If it is, the window may exist
1132 * and thus we flush that PID from the core.
1133 *
1134 * A potential future improvement would be to mark which PIDs
1135 * have never been used on the system and avoid it if the PID
1136 * is new and the process has no other cpumask bit set.
1137 */
1138 if (cpu_has_feature(CPU_FTR_HVMODE) && radix_enabled()) {
1139 int cpu = smp_processor_id();
1140 int sib = cpu_first_thread_sibling(cpu);
1141 bool flush = false;
1142
1143 for (; sib <= cpu_last_thread_sibling(cpu) && !flush; sib++) {
1144 if (sib == cpu)
1145 continue;
1146 if (!cpu_possible(sib))
1147 continue;
1148 if (paca_ptrs[sib]->kvm_hstate.kvm_vcpu)
1149 flush = true;
1150 }
1151 if (flush)
1152 _tlbiel_pid(pid, RIC_FLUSH_ALL);
1153 }
1154}
1155EXPORT_SYMBOL_GPL(radix_kvm_prefetch_workaround);
1156#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */