nds32: fix build error "relocation truncated to fit: R_NDS32_25_PCREL_RELA" when
[linux-2.6-microblaze.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 <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  */
32 static 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
47 static 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
70 void 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
93 static 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
109 static 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
123 static 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
139 static 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
153 static 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
170 static 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
186 static 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
202 static 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 static inline void fixup_tlbie(void)
219 {
220         unsigned long pid = 0;
221         unsigned long va = ((1UL << 52) - 1);
222
223         if (cpu_has_feature(CPU_FTR_P9_TLBIE_BUG)) {
224                 asm volatile("ptesync": : :"memory");
225                 __tlbie_va(va, pid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
226         }
227 }
228
229 static inline void fixup_tlbie_lpid(unsigned long lpid)
230 {
231         unsigned long va = ((1UL << 52) - 1);
232
233         if (cpu_has_feature(CPU_FTR_P9_TLBIE_BUG)) {
234                 asm volatile("ptesync": : :"memory");
235                 __tlbie_lpid_va(va, lpid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
236         }
237 }
238
239 /*
240  * We use 128 set in radix mode and 256 set in hpt mode.
241  */
242 static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
243 {
244         int set;
245
246         asm volatile("ptesync": : :"memory");
247
248         /*
249          * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
250          * also flush the entire Page Walk Cache.
251          */
252         __tlbiel_pid(pid, 0, ric);
253
254         /* For PWC, only one flush is needed */
255         if (ric == RIC_FLUSH_PWC) {
256                 asm volatile("ptesync": : :"memory");
257                 return;
258         }
259
260         /* For the remaining sets, just flush the TLB */
261         for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
262                 __tlbiel_pid(pid, set, RIC_FLUSH_TLB);
263
264         asm volatile("ptesync": : :"memory");
265         asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
266 }
267
268 static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
269 {
270         asm volatile("ptesync": : :"memory");
271
272         /*
273          * Workaround the fact that the "ric" argument to __tlbie_pid
274          * must be a compile-time contraint to match the "i" constraint
275          * in the asm statement.
276          */
277         switch (ric) {
278         case RIC_FLUSH_TLB:
279                 __tlbie_pid(pid, RIC_FLUSH_TLB);
280                 break;
281         case RIC_FLUSH_PWC:
282                 __tlbie_pid(pid, RIC_FLUSH_PWC);
283                 break;
284         case RIC_FLUSH_ALL:
285         default:
286                 __tlbie_pid(pid, RIC_FLUSH_ALL);
287         }
288         fixup_tlbie();
289         asm volatile("eieio; tlbsync; ptesync": : :"memory");
290 }
291
292 static inline void _tlbiel_lpid(unsigned long lpid, unsigned long ric)
293 {
294         int set;
295
296         VM_BUG_ON(mfspr(SPRN_LPID) != lpid);
297
298         asm volatile("ptesync": : :"memory");
299
300         /*
301          * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
302          * also flush the entire Page Walk Cache.
303          */
304         __tlbiel_lpid(lpid, 0, ric);
305
306         /* For PWC, only one flush is needed */
307         if (ric == RIC_FLUSH_PWC) {
308                 asm volatile("ptesync": : :"memory");
309                 return;
310         }
311
312         /* For the remaining sets, just flush the TLB */
313         for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
314                 __tlbiel_lpid(lpid, set, RIC_FLUSH_TLB);
315
316         asm volatile("ptesync": : :"memory");
317         asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
318 }
319
320 static inline void _tlbie_lpid(unsigned long lpid, unsigned long ric)
321 {
322         asm volatile("ptesync": : :"memory");
323
324         /*
325          * Workaround the fact that the "ric" argument to __tlbie_pid
326          * must be a compile-time contraint to match the "i" constraint
327          * in the asm statement.
328          */
329         switch (ric) {
330         case RIC_FLUSH_TLB:
331                 __tlbie_lpid(lpid, RIC_FLUSH_TLB);
332                 break;
333         case RIC_FLUSH_PWC:
334                 __tlbie_lpid(lpid, RIC_FLUSH_PWC);
335                 break;
336         case RIC_FLUSH_ALL:
337         default:
338                 __tlbie_lpid(lpid, RIC_FLUSH_ALL);
339         }
340         fixup_tlbie_lpid(lpid);
341         asm volatile("eieio; tlbsync; ptesync": : :"memory");
342 }
343
344 static inline void _tlbiel_lpid_guest(unsigned long lpid, unsigned long ric)
345 {
346         int set;
347
348         VM_BUG_ON(mfspr(SPRN_LPID) != lpid);
349
350         asm volatile("ptesync": : :"memory");
351
352         /*
353          * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
354          * also flush the entire Page Walk Cache.
355          */
356         __tlbiel_lpid_guest(lpid, 0, ric);
357
358         /* For PWC, only one flush is needed */
359         if (ric == RIC_FLUSH_PWC) {
360                 asm volatile("ptesync": : :"memory");
361                 return;
362         }
363
364         /* For the remaining sets, just flush the TLB */
365         for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
366                 __tlbiel_lpid_guest(lpid, set, RIC_FLUSH_TLB);
367
368         asm volatile("ptesync": : :"memory");
369 }
370
371
372 static inline void __tlbiel_va_range(unsigned long start, unsigned long end,
373                                     unsigned long pid, unsigned long page_size,
374                                     unsigned long psize)
375 {
376         unsigned long addr;
377         unsigned long ap = mmu_get_ap(psize);
378
379         for (addr = start; addr < end; addr += page_size)
380                 __tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
381 }
382
383 static inline void _tlbiel_va(unsigned long va, unsigned long pid,
384                               unsigned long psize, unsigned long ric)
385 {
386         unsigned long ap = mmu_get_ap(psize);
387
388         asm volatile("ptesync": : :"memory");
389         __tlbiel_va(va, pid, ap, ric);
390         asm volatile("ptesync": : :"memory");
391 }
392
393 static inline void _tlbiel_va_range(unsigned long start, unsigned long end,
394                                     unsigned long pid, unsigned long page_size,
395                                     unsigned long psize, bool also_pwc)
396 {
397         asm volatile("ptesync": : :"memory");
398         if (also_pwc)
399                 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
400         __tlbiel_va_range(start, end, pid, page_size, psize);
401         asm volatile("ptesync": : :"memory");
402 }
403
404 static inline void __tlbie_va_range(unsigned long start, unsigned long end,
405                                     unsigned long pid, unsigned long page_size,
406                                     unsigned long psize)
407 {
408         unsigned long addr;
409         unsigned long ap = mmu_get_ap(psize);
410
411         for (addr = start; addr < end; addr += page_size)
412                 __tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
413 }
414
415 static inline void _tlbie_va(unsigned long va, unsigned long pid,
416                               unsigned long psize, unsigned long ric)
417 {
418         unsigned long ap = mmu_get_ap(psize);
419
420         asm volatile("ptesync": : :"memory");
421         __tlbie_va(va, pid, ap, ric);
422         fixup_tlbie();
423         asm volatile("eieio; tlbsync; ptesync": : :"memory");
424 }
425
426 static inline void _tlbie_lpid_va(unsigned long va, unsigned long lpid,
427                               unsigned long psize, unsigned long ric)
428 {
429         unsigned long ap = mmu_get_ap(psize);
430
431         asm volatile("ptesync": : :"memory");
432         __tlbie_lpid_va(va, lpid, ap, ric);
433         fixup_tlbie_lpid(lpid);
434         asm volatile("eieio; tlbsync; ptesync": : :"memory");
435 }
436
437 static inline void _tlbie_va_range(unsigned long start, unsigned long end,
438                                     unsigned long pid, unsigned long page_size,
439                                     unsigned long psize, bool also_pwc)
440 {
441         asm volatile("ptesync": : :"memory");
442         if (also_pwc)
443                 __tlbie_pid(pid, RIC_FLUSH_PWC);
444         __tlbie_va_range(start, end, pid, page_size, psize);
445         fixup_tlbie();
446         asm volatile("eieio; tlbsync; ptesync": : :"memory");
447 }
448
449 /*
450  * Base TLB flushing operations:
451  *
452  *  - flush_tlb_mm(mm) flushes the specified mm context TLB's
453  *  - flush_tlb_page(vma, vmaddr) flushes one page
454  *  - flush_tlb_range(vma, start, end) flushes a range of pages
455  *  - flush_tlb_kernel_range(start, end) flushes kernel pages
456  *
457  *  - local_* variants of page and mm only apply to the current
458  *    processor
459  */
460 void radix__local_flush_tlb_mm(struct mm_struct *mm)
461 {
462         unsigned long pid;
463
464         preempt_disable();
465         pid = mm->context.id;
466         if (pid != MMU_NO_CONTEXT)
467                 _tlbiel_pid(pid, RIC_FLUSH_TLB);
468         preempt_enable();
469 }
470 EXPORT_SYMBOL(radix__local_flush_tlb_mm);
471
472 #ifndef CONFIG_SMP
473 void radix__local_flush_all_mm(struct mm_struct *mm)
474 {
475         unsigned long pid;
476
477         preempt_disable();
478         pid = mm->context.id;
479         if (pid != MMU_NO_CONTEXT)
480                 _tlbiel_pid(pid, RIC_FLUSH_ALL);
481         preempt_enable();
482 }
483 EXPORT_SYMBOL(radix__local_flush_all_mm);
484 #endif /* CONFIG_SMP */
485
486 void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
487                                        int psize)
488 {
489         unsigned long pid;
490
491         preempt_disable();
492         pid = mm->context.id;
493         if (pid != MMU_NO_CONTEXT)
494                 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
495         preempt_enable();
496 }
497
498 void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
499 {
500 #ifdef CONFIG_HUGETLB_PAGE
501         /* need the return fix for nohash.c */
502         if (is_vm_hugetlb_page(vma))
503                 return radix__local_flush_hugetlb_page(vma, vmaddr);
504 #endif
505         radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
506 }
507 EXPORT_SYMBOL(radix__local_flush_tlb_page);
508
509 static bool mm_is_singlethreaded(struct mm_struct *mm)
510 {
511         if (atomic_read(&mm->context.copros) > 0)
512                 return false;
513         if (atomic_read(&mm->mm_users) <= 1 && current->mm == mm)
514                 return true;
515         return false;
516 }
517
518 static bool mm_needs_flush_escalation(struct mm_struct *mm)
519 {
520         /*
521          * P9 nest MMU has issues with the page walk cache
522          * caching PTEs and not flushing them properly when
523          * RIC = 0 for a PID/LPID invalidate
524          */
525         if (atomic_read(&mm->context.copros) > 0)
526                 return true;
527         return false;
528 }
529
530 #ifdef CONFIG_SMP
531 static void do_exit_flush_lazy_tlb(void *arg)
532 {
533         struct mm_struct *mm = arg;
534         unsigned long pid = mm->context.id;
535
536         if (current->mm == mm)
537                 return; /* Local CPU */
538
539         if (current->active_mm == mm) {
540                 /*
541                  * Must be a kernel thread because sender is single-threaded.
542                  */
543                 BUG_ON(current->mm);
544                 mmgrab(&init_mm);
545                 switch_mm(mm, &init_mm, current);
546                 current->active_mm = &init_mm;
547                 mmdrop(mm);
548         }
549         _tlbiel_pid(pid, RIC_FLUSH_ALL);
550 }
551
552 static void exit_flush_lazy_tlbs(struct mm_struct *mm)
553 {
554         /*
555          * Would be nice if this was async so it could be run in
556          * parallel with our local flush, but generic code does not
557          * give a good API for it. Could extend the generic code or
558          * make a special powerpc IPI for flushing TLBs.
559          * For now it's not too performance critical.
560          */
561         smp_call_function_many(mm_cpumask(mm), do_exit_flush_lazy_tlb,
562                                 (void *)mm, 1);
563         mm_reset_thread_local(mm);
564 }
565
566 void radix__flush_tlb_mm(struct mm_struct *mm)
567 {
568         unsigned long pid;
569
570         pid = mm->context.id;
571         if (unlikely(pid == MMU_NO_CONTEXT))
572                 return;
573
574         preempt_disable();
575         /*
576          * Order loads of mm_cpumask vs previous stores to clear ptes before
577          * the invalidate. See barrier in switch_mm_irqs_off
578          */
579         smp_mb();
580         if (!mm_is_thread_local(mm)) {
581                 if (unlikely(mm_is_singlethreaded(mm))) {
582                         exit_flush_lazy_tlbs(mm);
583                         goto local;
584                 }
585
586                 if (mm_needs_flush_escalation(mm))
587                         _tlbie_pid(pid, RIC_FLUSH_ALL);
588                 else
589                         _tlbie_pid(pid, RIC_FLUSH_TLB);
590         } else {
591 local:
592                 _tlbiel_pid(pid, RIC_FLUSH_TLB);
593         }
594         preempt_enable();
595 }
596 EXPORT_SYMBOL(radix__flush_tlb_mm);
597
598 static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
599 {
600         unsigned long pid;
601
602         pid = mm->context.id;
603         if (unlikely(pid == MMU_NO_CONTEXT))
604                 return;
605
606         preempt_disable();
607         smp_mb(); /* see radix__flush_tlb_mm */
608         if (!mm_is_thread_local(mm)) {
609                 if (unlikely(mm_is_singlethreaded(mm))) {
610                         if (!fullmm) {
611                                 exit_flush_lazy_tlbs(mm);
612                                 goto local;
613                         }
614                 }
615                 _tlbie_pid(pid, RIC_FLUSH_ALL);
616         } else {
617 local:
618                 _tlbiel_pid(pid, RIC_FLUSH_ALL);
619         }
620         preempt_enable();
621 }
622 void radix__flush_all_mm(struct mm_struct *mm)
623 {
624         __flush_all_mm(mm, false);
625 }
626 EXPORT_SYMBOL(radix__flush_all_mm);
627
628 void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
629 {
630         tlb->need_flush_all = 1;
631 }
632 EXPORT_SYMBOL(radix__flush_tlb_pwc);
633
634 void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
635                                  int psize)
636 {
637         unsigned long pid;
638
639         pid = mm->context.id;
640         if (unlikely(pid == MMU_NO_CONTEXT))
641                 return;
642
643         preempt_disable();
644         smp_mb(); /* see radix__flush_tlb_mm */
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                 _tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
651         } else {
652 local:
653                 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
654         }
655         preempt_enable();
656 }
657
658 void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
659 {
660 #ifdef CONFIG_HUGETLB_PAGE
661         if (is_vm_hugetlb_page(vma))
662                 return radix__flush_hugetlb_page(vma, vmaddr);
663 #endif
664         radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
665 }
666 EXPORT_SYMBOL(radix__flush_tlb_page);
667
668 #else /* CONFIG_SMP */
669 #define radix__flush_all_mm radix__local_flush_all_mm
670 #endif /* CONFIG_SMP */
671
672 void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
673 {
674         _tlbie_pid(0, RIC_FLUSH_ALL);
675 }
676 EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
677
678 #define TLB_FLUSH_ALL -1UL
679
680 /*
681  * Number of pages above which we invalidate the entire PID rather than
682  * flush individual pages, for local and global flushes respectively.
683  *
684  * tlbie goes out to the interconnect and individual ops are more costly.
685  * It also does not iterate over sets like the local tlbiel variant when
686  * invalidating a full PID, so it has a far lower threshold to change from
687  * individual page flushes to full-pid flushes.
688  */
689 static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
690 static unsigned long tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
691
692 void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
693                      unsigned long end)
694
695 {
696         struct mm_struct *mm = vma->vm_mm;
697         unsigned long pid;
698         unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
699         unsigned long page_size = 1UL << page_shift;
700         unsigned long nr_pages = (end - start) >> page_shift;
701         bool local, full;
702
703 #ifdef CONFIG_HUGETLB_PAGE
704         if (is_vm_hugetlb_page(vma))
705                 return radix__flush_hugetlb_tlb_range(vma, start, end);
706 #endif
707
708         pid = mm->context.id;
709         if (unlikely(pid == MMU_NO_CONTEXT))
710                 return;
711
712         preempt_disable();
713         smp_mb(); /* see radix__flush_tlb_mm */
714         if (!mm_is_thread_local(mm)) {
715                 if (unlikely(mm_is_singlethreaded(mm))) {
716                         if (end != TLB_FLUSH_ALL) {
717                                 exit_flush_lazy_tlbs(mm);
718                                 goto is_local;
719                         }
720                 }
721                 local = false;
722                 full = (end == TLB_FLUSH_ALL ||
723                                 nr_pages > tlb_single_page_flush_ceiling);
724         } else {
725 is_local:
726                 local = true;
727                 full = (end == TLB_FLUSH_ALL ||
728                                 nr_pages > tlb_local_single_page_flush_ceiling);
729         }
730
731         if (full) {
732                 if (local) {
733                         _tlbiel_pid(pid, RIC_FLUSH_TLB);
734                 } else {
735                         if (mm_needs_flush_escalation(mm))
736                                 _tlbie_pid(pid, RIC_FLUSH_ALL);
737                         else
738                                 _tlbie_pid(pid, RIC_FLUSH_TLB);
739                 }
740         } else {
741                 bool hflush = false;
742                 unsigned long hstart, hend;
743
744 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
745                 hstart = (start + HPAGE_PMD_SIZE - 1) >> HPAGE_PMD_SHIFT;
746                 hend = end >> HPAGE_PMD_SHIFT;
747                 if (hstart < hend) {
748                         hstart <<= HPAGE_PMD_SHIFT;
749                         hend <<= HPAGE_PMD_SHIFT;
750                         hflush = true;
751                 }
752 #endif
753
754                 asm volatile("ptesync": : :"memory");
755                 if (local) {
756                         __tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
757                         if (hflush)
758                                 __tlbiel_va_range(hstart, hend, pid,
759                                                 HPAGE_PMD_SIZE, MMU_PAGE_2M);
760                         asm volatile("ptesync": : :"memory");
761                 } else {
762                         __tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
763                         if (hflush)
764                                 __tlbie_va_range(hstart, hend, pid,
765                                                 HPAGE_PMD_SIZE, MMU_PAGE_2M);
766                         fixup_tlbie();
767                         asm volatile("eieio; tlbsync; ptesync": : :"memory");
768                 }
769         }
770         preempt_enable();
771 }
772 EXPORT_SYMBOL(radix__flush_tlb_range);
773
774 static int radix_get_mmu_psize(int page_size)
775 {
776         int psize;
777
778         if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
779                 psize = mmu_virtual_psize;
780         else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
781                 psize = MMU_PAGE_2M;
782         else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
783                 psize = MMU_PAGE_1G;
784         else
785                 return -1;
786         return psize;
787 }
788
789 /*
790  * Flush partition scoped LPID address translation for all CPUs.
791  */
792 void radix__flush_tlb_lpid_page(unsigned int lpid,
793                                         unsigned long addr,
794                                         unsigned long page_size)
795 {
796         int psize = radix_get_mmu_psize(page_size);
797
798         _tlbie_lpid_va(addr, lpid, psize, RIC_FLUSH_TLB);
799 }
800 EXPORT_SYMBOL_GPL(radix__flush_tlb_lpid_page);
801
802 /*
803  * Flush partition scoped PWC from LPID for all CPUs.
804  */
805 void radix__flush_pwc_lpid(unsigned int lpid)
806 {
807         _tlbie_lpid(lpid, RIC_FLUSH_PWC);
808 }
809 EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);
810
811 /*
812  * Flush partition scoped translations from LPID (=LPIDR)
813  */
814 void radix__local_flush_tlb_lpid(unsigned int lpid)
815 {
816         _tlbiel_lpid(lpid, RIC_FLUSH_ALL);
817 }
818 EXPORT_SYMBOL_GPL(radix__local_flush_tlb_lpid);
819
820 /*
821  * Flush process scoped translations from LPID (=LPIDR).
822  * Important difference, the guest normally manages its own translations,
823  * but some cases e.g., vCPU CPU migration require KVM to flush.
824  */
825 void radix__local_flush_tlb_lpid_guest(unsigned int lpid)
826 {
827         _tlbiel_lpid_guest(lpid, RIC_FLUSH_ALL);
828 }
829 EXPORT_SYMBOL_GPL(radix__local_flush_tlb_lpid_guest);
830
831
832 static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
833                                   unsigned long end, int psize);
834
835 void radix__tlb_flush(struct mmu_gather *tlb)
836 {
837         int psize = 0;
838         struct mm_struct *mm = tlb->mm;
839         int page_size = tlb->page_size;
840
841         /*
842          * if page size is not something we understand, do a full mm flush
843          *
844          * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
845          * that flushes the process table entry cache upon process teardown.
846          * See the comment for radix in arch_exit_mmap().
847          */
848         if (tlb->fullmm) {
849                 __flush_all_mm(mm, true);
850         } else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
851                 if (!tlb->need_flush_all)
852                         radix__flush_tlb_mm(mm);
853                 else
854                         radix__flush_all_mm(mm);
855         } else {
856                 unsigned long start = tlb->start;
857                 unsigned long end = tlb->end;
858
859                 if (!tlb->need_flush_all)
860                         radix__flush_tlb_range_psize(mm, start, end, psize);
861                 else
862                         radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
863         }
864         tlb->need_flush_all = 0;
865 }
866
867 static inline void __radix__flush_tlb_range_psize(struct mm_struct *mm,
868                                 unsigned long start, unsigned long end,
869                                 int psize, bool also_pwc)
870 {
871         unsigned long pid;
872         unsigned int page_shift = mmu_psize_defs[psize].shift;
873         unsigned long page_size = 1UL << page_shift;
874         unsigned long nr_pages = (end - start) >> page_shift;
875         bool local, full;
876
877         pid = mm->context.id;
878         if (unlikely(pid == MMU_NO_CONTEXT))
879                 return;
880
881         preempt_disable();
882         smp_mb(); /* see radix__flush_tlb_mm */
883         if (!mm_is_thread_local(mm)) {
884                 if (unlikely(mm_is_singlethreaded(mm))) {
885                         if (end != TLB_FLUSH_ALL) {
886                                 exit_flush_lazy_tlbs(mm);
887                                 goto is_local;
888                         }
889                 }
890                 local = false;
891                 full = (end == TLB_FLUSH_ALL ||
892                                 nr_pages > tlb_single_page_flush_ceiling);
893         } else {
894 is_local:
895                 local = true;
896                 full = (end == TLB_FLUSH_ALL ||
897                                 nr_pages > tlb_local_single_page_flush_ceiling);
898         }
899
900         if (full) {
901                 if (local) {
902                         _tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
903                 } else {
904                         if (mm_needs_flush_escalation(mm))
905                                 also_pwc = true;
906
907                         _tlbie_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
908                 }
909         } else {
910                 if (local)
911                         _tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
912                 else
913                         _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
914         }
915         preempt_enable();
916 }
917
918 void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
919                                   unsigned long end, int psize)
920 {
921         return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
922 }
923
924 static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
925                                   unsigned long end, int psize)
926 {
927         __radix__flush_tlb_range_psize(mm, start, end, psize, true);
928 }
929
930 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
931 void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
932 {
933         unsigned long pid, end;
934
935         pid = mm->context.id;
936         if (unlikely(pid == MMU_NO_CONTEXT))
937                 return;
938
939         /* 4k page size, just blow the world */
940         if (PAGE_SIZE == 0x1000) {
941                 radix__flush_all_mm(mm);
942                 return;
943         }
944
945         end = addr + HPAGE_PMD_SIZE;
946
947         /* Otherwise first do the PWC, then iterate the pages. */
948         preempt_disable();
949         smp_mb(); /* see radix__flush_tlb_mm */
950         if (!mm_is_thread_local(mm)) {
951                 if (unlikely(mm_is_singlethreaded(mm))) {
952                         exit_flush_lazy_tlbs(mm);
953                         goto local;
954                 }
955                 _tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
956                 goto local;
957         } else {
958 local:
959                 _tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
960         }
961
962         preempt_enable();
963 }
964 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
965
966 void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
967                                 unsigned long start, unsigned long end)
968 {
969         radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
970 }
971 EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
972
973 void radix__flush_tlb_all(void)
974 {
975         unsigned long rb,prs,r,rs;
976         unsigned long ric = RIC_FLUSH_ALL;
977
978         rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
979         prs = 0; /* partition scoped */
980         r = 1;   /* radix format */
981         rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
982
983         asm volatile("ptesync": : :"memory");
984         /*
985          * now flush guest entries by passing PRS = 1 and LPID != 0
986          */
987         asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
988                      : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
989         /*
990          * now flush host entires by passing PRS = 0 and LPID == 0
991          */
992         asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
993                      : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
994         asm volatile("eieio; tlbsync; ptesync": : :"memory");
995 }
996
997 void radix__flush_tlb_pte_p9_dd1(unsigned long old_pte, struct mm_struct *mm,
998                                  unsigned long address)
999 {
1000         /*
1001          * We track page size in pte only for DD1, So we can
1002          * call this only on DD1.
1003          */
1004         if (!cpu_has_feature(CPU_FTR_POWER9_DD1)) {
1005                 VM_WARN_ON(1);
1006                 return;
1007         }
1008
1009         if (old_pte & R_PAGE_LARGE)
1010                 radix__flush_tlb_page_psize(mm, address, MMU_PAGE_2M);
1011         else
1012                 radix__flush_tlb_page_psize(mm, address, mmu_virtual_psize);
1013 }
1014
1015 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1016 extern void radix_kvm_prefetch_workaround(struct mm_struct *mm)
1017 {
1018         unsigned long pid = mm->context.id;
1019
1020         if (unlikely(pid == MMU_NO_CONTEXT))
1021                 return;
1022
1023         /*
1024          * If this context hasn't run on that CPU before and KVM is
1025          * around, there's a slim chance that the guest on another
1026          * CPU just brought in obsolete translation into the TLB of
1027          * this CPU due to a bad prefetch using the guest PID on
1028          * the way into the hypervisor.
1029          *
1030          * We work around this here. If KVM is possible, we check if
1031          * any sibling thread is in KVM. If it is, the window may exist
1032          * and thus we flush that PID from the core.
1033          *
1034          * A potential future improvement would be to mark which PIDs
1035          * have never been used on the system and avoid it if the PID
1036          * is new and the process has no other cpumask bit set.
1037          */
1038         if (cpu_has_feature(CPU_FTR_HVMODE) && radix_enabled()) {
1039                 int cpu = smp_processor_id();
1040                 int sib = cpu_first_thread_sibling(cpu);
1041                 bool flush = false;
1042
1043                 for (; sib <= cpu_last_thread_sibling(cpu) && !flush; sib++) {
1044                         if (sib == cpu)
1045                                 continue;
1046                         if (paca_ptrs[sib]->kvm_hstate.kvm_vcpu)
1047                                 flush = true;
1048                 }
1049                 if (flush)
1050                         _tlbiel_pid(pid, RIC_FLUSH_ALL);
1051         }
1052 }
1053 EXPORT_SYMBOL_GPL(radix_kvm_prefetch_workaround);
1054 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */