1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * TLB flush routines for radix kernels.
4 *
5 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
6 */
7
8 #include <linux/mm.h>
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>
14
15 #include <asm/ppc-opcode.h>
16 #include <asm/tlb.h>
17 #include <asm/tlbflush.h>
18 #include <asm/trace.h>
19 #include <asm/cputhreads.h>
20 #include <asm/plpar_wrappers.h>
21
22 #include "internal.h"
23
24 /*
25 * tlbiel instruction for radix, set invalidation
26 * i.e., r=1 and is=01 or is=10 or is=11
27 */
tlbiel_radix_set_isa300(unsigned int set,unsigned int is,unsigned int pid,unsigned int ric,unsigned int prs)28 static __always_inline void tlbiel_radix_set_isa300(unsigned int set, unsigned int is,
29 unsigned int pid,
30 unsigned int ric, unsigned int prs)
31 {
32 unsigned long rb;
33 unsigned long rs;
34
35 rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
36 rs = ((unsigned long)pid << PPC_BITLSHIFT(31));
37
38 asm volatile(PPC_TLBIEL(%0, %1, %2, %3, 1)
39 : : "r"(rb), "r"(rs), "i"(ric), "i"(prs)
40 : "memory");
41 }
42
tlbiel_all_isa300(unsigned int num_sets,unsigned int is)43 static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
44 {
45 unsigned int set;
46
47 asm volatile("ptesync": : :"memory");
48
49 /*
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
52 * TLB.
53 */
54
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);
58
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,
62 RIC_FLUSH_TLB, 0);
63 }
64 }
65
66 /* Flush process scoped entries. */
67 tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 1);
68
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);
72 }
73
74 ppc_after_tlbiel_barrier();
75 }
76
radix__tlbiel_all(unsigned int action)77 void radix__tlbiel_all(unsigned int action)
78 {
79 unsigned int is;
80
81 switch (action) {
82 case TLB_INVAL_SCOPE_GLOBAL:
83 is = 3;
84 break;
85 case TLB_INVAL_SCOPE_LPID:
86 is = 2;
87 break;
88 default:
89 BUG();
90 }
91
92 if (early_cpu_has_feature(CPU_FTR_ARCH_300))
93 tlbiel_all_isa300(POWER9_TLB_SETS_RADIX, is);
94 else
95 WARN(1, "%s called on pre-POWER9 CPU\n", __func__);
96
97 asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT "; isync" : : :"memory");
98 }
99
__tlbiel_pid(unsigned long pid,int set,unsigned long ric)100 static __always_inline void __tlbiel_pid(unsigned long pid, int set,
101 unsigned long ric)
102 {
103 unsigned long rb,rs,prs,r;
104
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 */
110
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);
114 }
115
__tlbie_pid(unsigned long pid,unsigned long ric)116 static __always_inline void __tlbie_pid(unsigned long pid, unsigned long ric)
117 {
118 unsigned long rb,rs,prs,r;
119
120 rb = PPC_BIT(53); /* IS = 1 */
121 rs = pid << PPC_BITLSHIFT(31);
122 prs = 1; /* process scoped */
123 r = 1; /* radix format */
124
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);
128 }
129
__tlbie_lpid(unsigned long lpid,unsigned long ric)130 static __always_inline void __tlbie_lpid(unsigned long lpid, unsigned long ric)
131 {
132 unsigned long rb,rs,prs,r;
133
134 rb = PPC_BIT(52); /* IS = 2 */
135 rs = lpid;
136 prs = 0; /* partition scoped */
137 r = 1; /* radix format */
138
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);
142 }
143
__tlbie_lpid_guest(unsigned long lpid,unsigned long ric)144 static __always_inline void __tlbie_lpid_guest(unsigned long lpid, unsigned long ric)
145 {
146 unsigned long rb,rs,prs,r;
147
148 rb = PPC_BIT(52); /* IS = 2 */
149 rs = lpid;
150 prs = 1; /* process scoped */
151 r = 1; /* radix format */
152
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);
156 }
157
__tlbiel_va(unsigned long va,unsigned long pid,unsigned long ap,unsigned long ric)158 static __always_inline void __tlbiel_va(unsigned long va, unsigned long pid,
159 unsigned long ap, unsigned long ric)
160 {
161 unsigned long rb,rs,prs,r;
162
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 */
168
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);
172 }
173
__tlbie_va(unsigned long va,unsigned long pid,unsigned long ap,unsigned long ric)174 static __always_inline void __tlbie_va(unsigned long va, unsigned long pid,
175 unsigned long ap, unsigned long ric)
176 {
177 unsigned long rb,rs,prs,r;
178
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 */
184
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);
188 }
189
__tlbie_lpid_va(unsigned long va,unsigned long lpid,unsigned long ap,unsigned long ric)190 static __always_inline void __tlbie_lpid_va(unsigned long va, unsigned long lpid,
191 unsigned long ap, unsigned long ric)
192 {
193 unsigned long rb,rs,prs,r;
194
195 rb = va & ~(PPC_BITMASK(52, 63));
196 rb |= ap << PPC_BITLSHIFT(58);
197 rs = lpid;
198 prs = 0; /* partition scoped */
199 r = 1; /* radix format */
200
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);
204 }
205
206
fixup_tlbie_va(unsigned long va,unsigned long pid,unsigned long ap)207 static inline void fixup_tlbie_va(unsigned long va, unsigned long pid,
208 unsigned long ap)
209 {
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);
213 }
214
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);
218 }
219 }
220
fixup_tlbie_va_range(unsigned long va,unsigned long pid,unsigned long ap)221 static inline void fixup_tlbie_va_range(unsigned long va, unsigned long pid,
222 unsigned long ap)
223 {
224 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
225 asm volatile("ptesync": : :"memory");
226 __tlbie_pid(0, RIC_FLUSH_TLB);
227 }
228
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);
232 }
233 }
234
fixup_tlbie_pid(unsigned long pid)235 static inline void fixup_tlbie_pid(unsigned long pid)
236 {
237 /*
238 * We can use any address for the invalidation, pick one which is
239 * probably unused as an optimisation.
240 */
241 unsigned long va = ((1UL << 52) - 1);
242
243 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
244 asm volatile("ptesync": : :"memory");
245 __tlbie_pid(0, RIC_FLUSH_TLB);
246 }
247
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);
251 }
252 }
253
fixup_tlbie_lpid_va(unsigned long va,unsigned long lpid,unsigned long ap)254 static inline void fixup_tlbie_lpid_va(unsigned long va, unsigned long lpid,
255 unsigned long ap)
256 {
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);
260 }
261
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);
265 }
266 }
267
fixup_tlbie_lpid(unsigned long lpid)268 static inline void fixup_tlbie_lpid(unsigned long lpid)
269 {
270 /*
271 * We can use any address for the invalidation, pick one which is
272 * probably unused as an optimisation.
273 */
274 unsigned long va = ((1UL << 52) - 1);
275
276 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
277 asm volatile("ptesync": : :"memory");
278 __tlbie_lpid(0, RIC_FLUSH_TLB);
279 }
280
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);
284 }
285 }
286
287 /*
288 * We use 128 set in radix mode and 256 set in hpt mode.
289 */
_tlbiel_pid(unsigned long pid,unsigned long ric)290 static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
291 {
292 int set;
293
294 asm volatile("ptesync": : :"memory");
295
296 switch (ric) {
297 case RIC_FLUSH_PWC:
298
299 /* For PWC, only one flush is needed */
300 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
301 ppc_after_tlbiel_barrier();
302 return;
303 case RIC_FLUSH_TLB:
304 __tlbiel_pid(pid, 0, RIC_FLUSH_TLB);
305 break;
306 case RIC_FLUSH_ALL:
307 default:
308 /*
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.
312 */
313 __tlbiel_pid(pid, 0, RIC_FLUSH_ALL);
314 }
315
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);
320 }
321
322 ppc_after_tlbiel_barrier();
323 asm volatile(PPC_RADIX_INVALIDATE_ERAT_USER "; isync" : : :"memory");
324 }
325
_tlbie_pid(unsigned long pid,unsigned long ric)326 static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
327 {
328 asm volatile("ptesync": : :"memory");
329
330 /*
331 * Workaround the fact that the "ric" argument to __tlbie_pid
332 * must be a compile-time contraint to match the "i" constraint
333 * in the asm statement.
334 */
335 switch (ric) {
336 case RIC_FLUSH_TLB:
337 __tlbie_pid(pid, RIC_FLUSH_TLB);
338 fixup_tlbie_pid(pid);
339 break;
340 case RIC_FLUSH_PWC:
341 __tlbie_pid(pid, RIC_FLUSH_PWC);
342 break;
343 case RIC_FLUSH_ALL:
344 default:
345 __tlbie_pid(pid, RIC_FLUSH_ALL);
346 fixup_tlbie_pid(pid);
347 }
348 asm volatile("eieio; tlbsync; ptesync": : :"memory");
349 }
350
351 struct tlbiel_pid {
352 unsigned long pid;
353 unsigned long ric;
354 };
355
do_tlbiel_pid(void * info)356 static void do_tlbiel_pid(void *info)
357 {
358 struct tlbiel_pid *t = info;
359
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);
364 else
365 _tlbiel_pid(t->pid, RIC_FLUSH_ALL);
366 }
367
_tlbiel_pid_multicast(struct mm_struct * mm,unsigned long pid,unsigned long ric)368 static inline void _tlbiel_pid_multicast(struct mm_struct *mm,
369 unsigned long pid, unsigned long ric)
370 {
371 struct cpumask *cpus = mm_cpumask(mm);
372 struct tlbiel_pid t = { .pid = pid, .ric = ric };
373
374 on_each_cpu_mask(cpus, do_tlbiel_pid, &t, 1);
375 /*
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.
379 */
380 if (atomic_read(&mm->context.copros) > 0)
381 _tlbie_pid(pid, RIC_FLUSH_ALL);
382 }
383
_tlbie_lpid(unsigned long lpid,unsigned long ric)384 static inline void _tlbie_lpid(unsigned long lpid, unsigned long ric)
385 {
386 asm volatile("ptesync": : :"memory");
387
388 /*
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.
392 */
393 switch (ric) {
394 case RIC_FLUSH_TLB:
395 __tlbie_lpid(lpid, RIC_FLUSH_TLB);
396 fixup_tlbie_lpid(lpid);
397 break;
398 case RIC_FLUSH_PWC:
399 __tlbie_lpid(lpid, RIC_FLUSH_PWC);
400 break;
401 case RIC_FLUSH_ALL:
402 default:
403 __tlbie_lpid(lpid, RIC_FLUSH_ALL);
404 fixup_tlbie_lpid(lpid);
405 }
406 asm volatile("eieio; tlbsync; ptesync": : :"memory");
407 }
408
_tlbie_lpid_guest(unsigned long lpid,unsigned long ric)409 static __always_inline void _tlbie_lpid_guest(unsigned long lpid, unsigned long ric)
410 {
411 /*
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.
415 */
416 switch (ric) {
417 case RIC_FLUSH_TLB:
418 __tlbie_lpid_guest(lpid, RIC_FLUSH_TLB);
419 break;
420 case RIC_FLUSH_PWC:
421 __tlbie_lpid_guest(lpid, RIC_FLUSH_PWC);
422 break;
423 case RIC_FLUSH_ALL:
424 default:
425 __tlbie_lpid_guest(lpid, RIC_FLUSH_ALL);
426 }
427 fixup_tlbie_lpid(lpid);
428 asm volatile("eieio; tlbsync; ptesync": : :"memory");
429 }
430
__tlbiel_va_range(unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize)431 static inline void __tlbiel_va_range(unsigned long start, unsigned long end,
432 unsigned long pid, unsigned long page_size,
433 unsigned long psize)
434 {
435 unsigned long addr;
436 unsigned long ap = mmu_get_ap(psize);
437
438 for (addr = start; addr < end; addr += page_size)
439 __tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
440 }
441
_tlbiel_va(unsigned long va,unsigned long pid,unsigned long psize,unsigned long ric)442 static __always_inline void _tlbiel_va(unsigned long va, unsigned long pid,
443 unsigned long psize, unsigned long ric)
444 {
445 unsigned long ap = mmu_get_ap(psize);
446
447 asm volatile("ptesync": : :"memory");
448 __tlbiel_va(va, pid, ap, ric);
449 ppc_after_tlbiel_barrier();
450 }
451
_tlbiel_va_range(unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize,bool also_pwc)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)
455 {
456 asm volatile("ptesync": : :"memory");
457 if (also_pwc)
458 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
459 __tlbiel_va_range(start, end, pid, page_size, psize);
460 ppc_after_tlbiel_barrier();
461 }
462
__tlbie_va_range(unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize)463 static inline void __tlbie_va_range(unsigned long start, unsigned long end,
464 unsigned long pid, unsigned long page_size,
465 unsigned long psize)
466 {
467 unsigned long addr;
468 unsigned long ap = mmu_get_ap(psize);
469
470 for (addr = start; addr < end; addr += page_size)
471 __tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
472
473 fixup_tlbie_va_range(addr - page_size, pid, ap);
474 }
475
_tlbie_va(unsigned long va,unsigned long pid,unsigned long psize,unsigned long ric)476 static __always_inline void _tlbie_va(unsigned long va, unsigned long pid,
477 unsigned long psize, unsigned long ric)
478 {
479 unsigned long ap = mmu_get_ap(psize);
480
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");
485 }
486
487 struct tlbiel_va {
488 unsigned long pid;
489 unsigned long va;
490 unsigned long psize;
491 unsigned long ric;
492 };
493
do_tlbiel_va(void * info)494 static void do_tlbiel_va(void *info)
495 {
496 struct tlbiel_va *t = info;
497
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);
502 else
503 _tlbiel_va(t->va, t->pid, t->psize, RIC_FLUSH_ALL);
504 }
505
_tlbiel_va_multicast(struct mm_struct * mm,unsigned long va,unsigned long pid,unsigned long psize,unsigned long ric)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)
509 {
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);
515 }
516
517 struct tlbiel_va_range {
518 unsigned long pid;
519 unsigned long start;
520 unsigned long end;
521 unsigned long page_size;
522 unsigned long psize;
523 bool also_pwc;
524 };
525
do_tlbiel_va_range(void * info)526 static void do_tlbiel_va_range(void *info)
527 {
528 struct tlbiel_va_range *t = info;
529
530 _tlbiel_va_range(t->start, t->end, t->pid, t->page_size,
531 t->psize, t->also_pwc);
532 }
533
_tlbie_lpid_va(unsigned long va,unsigned long lpid,unsigned long psize,unsigned long ric)534 static __always_inline void _tlbie_lpid_va(unsigned long va, unsigned long lpid,
535 unsigned long psize, unsigned long ric)
536 {
537 unsigned long ap = mmu_get_ap(psize);
538
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");
543 }
544
_tlbie_va_range(unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize,bool also_pwc)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)
548 {
549 asm volatile("ptesync": : :"memory");
550 if (also_pwc)
551 __tlbie_pid(pid, RIC_FLUSH_PWC);
552 __tlbie_va_range(start, end, pid, page_size, psize);
553 asm volatile("eieio; tlbsync; ptesync": : :"memory");
554 }
555
_tlbiel_va_range_multicast(struct mm_struct * mm,unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize,bool also_pwc)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)
560 {
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 };
565
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);
569 }
570
571 /*
572 * Base TLB flushing operations:
573 *
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
578 *
579 * - local_* variants of page and mm only apply to the current
580 * processor
581 */
radix__local_flush_tlb_mm(struct mm_struct * mm)582 void radix__local_flush_tlb_mm(struct mm_struct *mm)
583 {
584 unsigned long pid;
585
586 preempt_disable();
587 pid = mm->context.id;
588 if (pid != MMU_NO_CONTEXT)
589 _tlbiel_pid(pid, RIC_FLUSH_TLB);
590 preempt_enable();
591 }
592 EXPORT_SYMBOL(radix__local_flush_tlb_mm);
593
594 #ifndef CONFIG_SMP
radix__local_flush_all_mm(struct mm_struct * mm)595 void radix__local_flush_all_mm(struct mm_struct *mm)
596 {
597 unsigned long pid;
598
599 preempt_disable();
600 pid = mm->context.id;
601 if (pid != MMU_NO_CONTEXT)
602 _tlbiel_pid(pid, RIC_FLUSH_ALL);
603 preempt_enable();
604 }
605 EXPORT_SYMBOL(radix__local_flush_all_mm);
606
__flush_all_mm(struct mm_struct * mm,bool fullmm)607 static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
608 {
609 radix__local_flush_all_mm(mm);
610 }
611 #endif /* CONFIG_SMP */
612
radix__local_flush_tlb_page_psize(struct mm_struct * mm,unsigned long vmaddr,int psize)613 void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
614 int psize)
615 {
616 unsigned long pid;
617
618 preempt_disable();
619 pid = mm->context.id;
620 if (pid != MMU_NO_CONTEXT)
621 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
622 preempt_enable();
623 }
624
radix__local_flush_tlb_page(struct vm_area_struct * vma,unsigned long vmaddr)625 void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
626 {
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);
631 #endif
632 radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
633 }
634 EXPORT_SYMBOL(radix__local_flush_tlb_page);
635
mm_needs_flush_escalation(struct mm_struct * mm)636 static bool mm_needs_flush_escalation(struct mm_struct *mm)
637 {
638 /*
639 * P9 nest MMU has issues with the page walk cache
640 * caching PTEs and not flushing them properly when
641 * RIC = 0 for a PID/LPID invalidate
642 */
643 if (atomic_read(&mm->context.copros) > 0)
644 return true;
645 return false;
646 }
647
648 /*
649 * If always_flush is true, then flush even if this CPU can't be removed
650 * from mm_cpumask.
651 */
exit_lazy_flush_tlb(struct mm_struct * mm,bool always_flush)652 void exit_lazy_flush_tlb(struct mm_struct *mm, bool always_flush)
653 {
654 unsigned long pid = mm->context.id;
655 int cpu = smp_processor_id();
656
657 /*
658 * A kthread could have done a mmget_not_zero() after the flushing CPU
659 * checked mm_cpumask, and be in the process of kthread_use_mm when
660 * interrupted here. In that case, current->mm will be set to mm,
661 * because kthread_use_mm() setting ->mm and switching to the mm is
662 * done with interrupts off.
663 */
664 if (current->mm == mm)
665 goto out;
666
667 if (current->active_mm == mm) {
668 WARN_ON_ONCE(current->mm != NULL);
669 /* Is a kernel thread and is using mm as the lazy tlb */
670 mmgrab(&init_mm);
671 current->active_mm = &init_mm;
672 switch_mm_irqs_off(mm, &init_mm, current);
673 mmdrop(mm);
674 }
675
676 /*
677 * This IPI may be initiated from any source including those not
678 * running the mm, so there may be a racing IPI that comes after
679 * this one which finds the cpumask already clear. Check and avoid
680 * underflowing the active_cpus count in that case. The race should
681 * not otherwise be a problem, but the TLB must be flushed because
682 * that's what the caller expects.
683 */
684 if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
685 atomic_dec(&mm->context.active_cpus);
686 cpumask_clear_cpu(cpu, mm_cpumask(mm));
687 always_flush = true;
688 }
689
690 out:
691 if (always_flush)
692 _tlbiel_pid(pid, RIC_FLUSH_ALL);
693 }
694
695 #ifdef CONFIG_SMP
do_exit_flush_lazy_tlb(void * arg)696 static void do_exit_flush_lazy_tlb(void *arg)
697 {
698 struct mm_struct *mm = arg;
699 exit_lazy_flush_tlb(mm, true);
700 }
701
exit_flush_lazy_tlbs(struct mm_struct * mm)702 static void exit_flush_lazy_tlbs(struct mm_struct *mm)
703 {
704 /*
705 * Would be nice if this was async so it could be run in
706 * parallel with our local flush, but generic code does not
707 * give a good API for it. Could extend the generic code or
708 * make a special powerpc IPI for flushing TLBs.
709 * For now it's not too performance critical.
710 */
711 smp_call_function_many(mm_cpumask(mm), do_exit_flush_lazy_tlb,
712 (void *)mm, 1);
713 }
714
715 #else /* CONFIG_SMP */
exit_flush_lazy_tlbs(struct mm_struct * mm)716 static inline void exit_flush_lazy_tlbs(struct mm_struct *mm) { }
717 #endif /* CONFIG_SMP */
718
719 static DEFINE_PER_CPU(unsigned int, mm_cpumask_trim_clock);
720
721 /*
722 * Interval between flushes at which we send out IPIs to check whether the
723 * mm_cpumask can be trimmed for the case where it's not a single-threaded
724 * process flushing its own mm. The intent is to reduce the cost of later
725 * flushes. Don't want this to be so low that it adds noticable cost to TLB
726 * flushing, or so high that it doesn't help reduce global TLBIEs.
727 */
728 static unsigned long tlb_mm_cpumask_trim_timer = 1073;
729
tick_and_test_trim_clock(void)730 static bool tick_and_test_trim_clock(void)
731 {
732 if (__this_cpu_inc_return(mm_cpumask_trim_clock) ==
733 tlb_mm_cpumask_trim_timer) {
734 __this_cpu_write(mm_cpumask_trim_clock, 0);
735 return true;
736 }
737 return false;
738 }
739
740 enum tlb_flush_type {
741 FLUSH_TYPE_NONE,
742 FLUSH_TYPE_LOCAL,
743 FLUSH_TYPE_GLOBAL,
744 };
745
flush_type_needed(struct mm_struct * mm,bool fullmm)746 static enum tlb_flush_type flush_type_needed(struct mm_struct *mm, bool fullmm)
747 {
748 int active_cpus = atomic_read(&mm->context.active_cpus);
749 int cpu = smp_processor_id();
750
751 if (active_cpus == 0)
752 return FLUSH_TYPE_NONE;
753 if (active_cpus == 1 && cpumask_test_cpu(cpu, mm_cpumask(mm))) {
754 if (current->mm != mm) {
755 /*
756 * Asynchronous flush sources may trim down to nothing
757 * if the process is not running, so occasionally try
758 * to trim.
759 */
760 if (tick_and_test_trim_clock()) {
761 exit_lazy_flush_tlb(mm, true);
762 return FLUSH_TYPE_NONE;
763 }
764 }
765 return FLUSH_TYPE_LOCAL;
766 }
767
768 /* Coprocessors require TLBIE to invalidate nMMU. */
769 if (atomic_read(&mm->context.copros) > 0)
770 return FLUSH_TYPE_GLOBAL;
771
772 /*
773 * In the fullmm case there's no point doing the exit_flush_lazy_tlbs
774 * because the mm is being taken down anyway, and a TLBIE tends to
775 * be faster than an IPI+TLBIEL.
776 */
777 if (fullmm)
778 return FLUSH_TYPE_GLOBAL;
779
780 /*
781 * If we are running the only thread of a single-threaded process,
782 * then we should almost always be able to trim off the rest of the
783 * CPU mask (except in the case of use_mm() races), so always try
784 * trimming the mask.
785 */
786 if (atomic_read(&mm->mm_users) <= 1 && current->mm == mm) {
787 exit_flush_lazy_tlbs(mm);
788 /*
789 * use_mm() race could prevent IPIs from being able to clear
790 * the cpumask here, however those users are established
791 * after our first check (and so after the PTEs are removed),
792 * and the TLB still gets flushed by the IPI, so this CPU
793 * will only require a local flush.
794 */
795 return FLUSH_TYPE_LOCAL;
796 }
797
798 /*
799 * Occasionally try to trim down the cpumask. It's possible this can
800 * bring the mask to zero, which results in no flush.
801 */
802 if (tick_and_test_trim_clock()) {
803 exit_flush_lazy_tlbs(mm);
804 if (current->mm == mm)
805 return FLUSH_TYPE_LOCAL;
806 if (cpumask_test_cpu(cpu, mm_cpumask(mm)))
807 exit_lazy_flush_tlb(mm, true);
808 return FLUSH_TYPE_NONE;
809 }
810
811 return FLUSH_TYPE_GLOBAL;
812 }
813
814 #ifdef CONFIG_SMP
radix__flush_tlb_mm(struct mm_struct * mm)815 void radix__flush_tlb_mm(struct mm_struct *mm)
816 {
817 unsigned long pid;
818 enum tlb_flush_type type;
819
820 pid = mm->context.id;
821 if (unlikely(pid == MMU_NO_CONTEXT))
822 return;
823
824 preempt_disable();
825 /*
826 * Order loads of mm_cpumask (in flush_type_needed) vs previous
827 * stores to clear ptes before the invalidate. See barrier in
828 * switch_mm_irqs_off
829 */
830 smp_mb();
831 type = flush_type_needed(mm, false);
832 if (type == FLUSH_TYPE_LOCAL) {
833 _tlbiel_pid(pid, RIC_FLUSH_TLB);
834 } else if (type == FLUSH_TYPE_GLOBAL) {
835 if (!mmu_has_feature(MMU_FTR_GTSE)) {
836 unsigned long tgt = H_RPTI_TARGET_CMMU;
837
838 if (atomic_read(&mm->context.copros) > 0)
839 tgt |= H_RPTI_TARGET_NMMU;
840 pseries_rpt_invalidate(pid, tgt, H_RPTI_TYPE_TLB,
841 H_RPTI_PAGE_ALL, 0, -1UL);
842 } else if (cputlb_use_tlbie()) {
843 if (mm_needs_flush_escalation(mm))
844 _tlbie_pid(pid, RIC_FLUSH_ALL);
845 else
846 _tlbie_pid(pid, RIC_FLUSH_TLB);
847 } else {
848 _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_TLB);
849 }
850 }
851 preempt_enable();
852 }
853 EXPORT_SYMBOL(radix__flush_tlb_mm);
854
__flush_all_mm(struct mm_struct * mm,bool fullmm)855 static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
856 {
857 unsigned long pid;
858 enum tlb_flush_type type;
859
860 pid = mm->context.id;
861 if (unlikely(pid == MMU_NO_CONTEXT))
862 return;
863
864 preempt_disable();
865 smp_mb(); /* see radix__flush_tlb_mm */
866 type = flush_type_needed(mm, fullmm);
867 if (type == FLUSH_TYPE_LOCAL) {
868 _tlbiel_pid(pid, RIC_FLUSH_ALL);
869 } else if (type == FLUSH_TYPE_GLOBAL) {
870 if (!mmu_has_feature(MMU_FTR_GTSE)) {
871 unsigned long tgt = H_RPTI_TARGET_CMMU;
872 unsigned long type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
873 H_RPTI_TYPE_PRT;
874
875 if (atomic_read(&mm->context.copros) > 0)
876 tgt |= H_RPTI_TARGET_NMMU;
877 pseries_rpt_invalidate(pid, tgt, type,
878 H_RPTI_PAGE_ALL, 0, -1UL);
879 } else if (cputlb_use_tlbie())
880 _tlbie_pid(pid, RIC_FLUSH_ALL);
881 else
882 _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL);
883 }
884 preempt_enable();
885 }
886
radix__flush_all_mm(struct mm_struct * mm)887 void radix__flush_all_mm(struct mm_struct *mm)
888 {
889 __flush_all_mm(mm, false);
890 }
891 EXPORT_SYMBOL(radix__flush_all_mm);
892
radix__flush_tlb_page_psize(struct mm_struct * mm,unsigned long vmaddr,int psize)893 void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
894 int psize)
895 {
896 unsigned long pid;
897 enum tlb_flush_type type;
898
899 pid = mm->context.id;
900 if (unlikely(pid == MMU_NO_CONTEXT))
901 return;
902
903 preempt_disable();
904 smp_mb(); /* see radix__flush_tlb_mm */
905 type = flush_type_needed(mm, false);
906 if (type == FLUSH_TYPE_LOCAL) {
907 _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
908 } else if (type == FLUSH_TYPE_GLOBAL) {
909 if (!mmu_has_feature(MMU_FTR_GTSE)) {
910 unsigned long tgt, pg_sizes, size;
911
912 tgt = H_RPTI_TARGET_CMMU;
913 pg_sizes = psize_to_rpti_pgsize(psize);
914 size = 1UL << mmu_psize_to_shift(psize);
915
916 if (atomic_read(&mm->context.copros) > 0)
917 tgt |= H_RPTI_TARGET_NMMU;
918 pseries_rpt_invalidate(pid, tgt, H_RPTI_TYPE_TLB,
919 pg_sizes, vmaddr,
920 vmaddr + size);
921 } else if (cputlb_use_tlbie())
922 _tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
923 else
924 _tlbiel_va_multicast(mm, vmaddr, pid, psize, RIC_FLUSH_TLB);
925 }
926 preempt_enable();
927 }
928
radix__flush_tlb_page(struct vm_area_struct * vma,unsigned long vmaddr)929 void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
930 {
931 #ifdef CONFIG_HUGETLB_PAGE
932 if (is_vm_hugetlb_page(vma))
933 return radix__flush_hugetlb_page(vma, vmaddr);
934 #endif
935 radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
936 }
937 EXPORT_SYMBOL(radix__flush_tlb_page);
938
939 #endif /* CONFIG_SMP */
940
do_tlbiel_kernel(void * info)941 static void do_tlbiel_kernel(void *info)
942 {
943 _tlbiel_pid(0, RIC_FLUSH_ALL);
944 }
945
_tlbiel_kernel_broadcast(void)946 static inline void _tlbiel_kernel_broadcast(void)
947 {
948 on_each_cpu(do_tlbiel_kernel, NULL, 1);
949 if (tlbie_capable) {
950 /*
951 * Coherent accelerators don't refcount kernel memory mappings,
952 * so have to always issue a tlbie for them. This is quite a
953 * slow path anyway.
954 */
955 _tlbie_pid(0, RIC_FLUSH_ALL);
956 }
957 }
958
959 /*
960 * If kernel TLBIs ever become local rather than global, then
961 * drivers/misc/ocxl/link.c:ocxl_link_add_pe will need some work, as it
962 * assumes kernel TLBIs are global.
963 */
radix__flush_tlb_kernel_range(unsigned long start,unsigned long end)964 void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
965 {
966 if (!mmu_has_feature(MMU_FTR_GTSE)) {
967 unsigned long tgt = H_RPTI_TARGET_CMMU | H_RPTI_TARGET_NMMU;
968 unsigned long type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
969 H_RPTI_TYPE_PRT;
970
971 pseries_rpt_invalidate(0, tgt, type, H_RPTI_PAGE_ALL,
972 start, end);
973 } else if (cputlb_use_tlbie())
974 _tlbie_pid(0, RIC_FLUSH_ALL);
975 else
976 _tlbiel_kernel_broadcast();
977 }
978 EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
979
980 #define TLB_FLUSH_ALL -1UL
981
982 /*
983 * Number of pages above which we invalidate the entire PID rather than
984 * flush individual pages, for local and global flushes respectively.
985 *
986 * tlbie goes out to the interconnect and individual ops are more costly.
987 * It also does not iterate over sets like the local tlbiel variant when
988 * invalidating a full PID, so it has a far lower threshold to change from
989 * individual page flushes to full-pid flushes.
990 */
991 static u32 tlb_single_page_flush_ceiling __read_mostly = 33;
992 static u32 tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
993
__radix__flush_tlb_range(struct mm_struct * mm,unsigned long start,unsigned long end)994 static inline void __radix__flush_tlb_range(struct mm_struct *mm,
995 unsigned long start, unsigned long end)
996 {
997 unsigned long pid;
998 unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
999 unsigned long page_size = 1UL << page_shift;
1000 unsigned long nr_pages = (end - start) >> page_shift;
1001 bool fullmm = (end == TLB_FLUSH_ALL);
1002 bool flush_pid, flush_pwc = false;
1003 enum tlb_flush_type type;
1004
1005 pid = mm->context.id;
1006 if (unlikely(pid == MMU_NO_CONTEXT))
1007 return;
1008
1009 preempt_disable();
1010 smp_mb(); /* see radix__flush_tlb_mm */
1011 type = flush_type_needed(mm, fullmm);
1012 if (type == FLUSH_TYPE_NONE)
1013 goto out;
1014
1015 if (fullmm)
1016 flush_pid = true;
1017 else if (type == FLUSH_TYPE_GLOBAL)
1018 flush_pid = nr_pages > tlb_single_page_flush_ceiling;
1019 else
1020 flush_pid = nr_pages > tlb_local_single_page_flush_ceiling;
1021 /*
1022 * full pid flush already does the PWC flush. if it is not full pid
1023 * flush check the range is more than PMD and force a pwc flush
1024 * mremap() depends on this behaviour.
1025 */
1026 if (!flush_pid && (end - start) >= PMD_SIZE)
1027 flush_pwc = true;
1028
1029 if (!mmu_has_feature(MMU_FTR_GTSE) && type == FLUSH_TYPE_GLOBAL) {
1030 unsigned long type = H_RPTI_TYPE_TLB;
1031 unsigned long tgt = H_RPTI_TARGET_CMMU;
1032 unsigned long pg_sizes = psize_to_rpti_pgsize(mmu_virtual_psize);
1033
1034 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1035 pg_sizes |= psize_to_rpti_pgsize(MMU_PAGE_2M);
1036 if (atomic_read(&mm->context.copros) > 0)
1037 tgt |= H_RPTI_TARGET_NMMU;
1038 if (flush_pwc)
1039 type |= H_RPTI_TYPE_PWC;
1040 pseries_rpt_invalidate(pid, tgt, type, pg_sizes, start, end);
1041 } else if (flush_pid) {
1042 /*
1043 * We are now flushing a range larger than PMD size force a RIC_FLUSH_ALL
1044 */
1045 if (type == FLUSH_TYPE_LOCAL) {
1046 _tlbiel_pid(pid, RIC_FLUSH_ALL);
1047 } else {
1048 if (cputlb_use_tlbie()) {
1049 _tlbie_pid(pid, RIC_FLUSH_ALL);
1050 } else {
1051 _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL);
1052 }
1053 }
1054 } else {
1055 bool hflush;
1056 unsigned long hstart, hend;
1057
1058 hstart = (start + PMD_SIZE - 1) & PMD_MASK;
1059 hend = end & PMD_MASK;
1060 hflush = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hstart < hend;
1061
1062 if (type == FLUSH_TYPE_LOCAL) {
1063 asm volatile("ptesync": : :"memory");
1064 if (flush_pwc)
1065 /* For PWC, only one flush is needed */
1066 __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
1067 __tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
1068 if (hflush)
1069 __tlbiel_va_range(hstart, hend, pid,
1070 PMD_SIZE, MMU_PAGE_2M);
1071 ppc_after_tlbiel_barrier();
1072 } else if (cputlb_use_tlbie()) {
1073 asm volatile("ptesync": : :"memory");
1074 if (flush_pwc)
1075 __tlbie_pid(pid, RIC_FLUSH_PWC);
1076 __tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
1077 if (hflush)
1078 __tlbie_va_range(hstart, hend, pid,
1079 PMD_SIZE, MMU_PAGE_2M);
1080 asm volatile("eieio; tlbsync; ptesync": : :"memory");
1081 } else {
1082 _tlbiel_va_range_multicast(mm,
1083 start, end, pid, page_size, mmu_virtual_psize, flush_pwc);
1084 if (hflush)
1085 _tlbiel_va_range_multicast(mm,
1086 hstart, hend, pid, PMD_SIZE, MMU_PAGE_2M, flush_pwc);
1087 }
1088 }
1089 out:
1090 preempt_enable();
1091 }
1092
radix__flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)1093 void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
1094 unsigned long end)
1095
1096 {
1097 #ifdef CONFIG_HUGETLB_PAGE
1098 if (is_vm_hugetlb_page(vma))
1099 return radix__flush_hugetlb_tlb_range(vma, start, end);
1100 #endif
1101
1102 __radix__flush_tlb_range(vma->vm_mm, start, end);
1103 }
1104 EXPORT_SYMBOL(radix__flush_tlb_range);
1105
radix_get_mmu_psize(int page_size)1106 static int radix_get_mmu_psize(int page_size)
1107 {
1108 int psize;
1109
1110 if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
1111 psize = mmu_virtual_psize;
1112 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
1113 psize = MMU_PAGE_2M;
1114 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
1115 psize = MMU_PAGE_1G;
1116 else
1117 return -1;
1118 return psize;
1119 }
1120
1121 /*
1122 * Flush partition scoped LPID address translation for all CPUs.
1123 */
radix__flush_tlb_lpid_page(unsigned int lpid,unsigned long addr,unsigned long page_size)1124 void radix__flush_tlb_lpid_page(unsigned int lpid,
1125 unsigned long addr,
1126 unsigned long page_size)
1127 {
1128 int psize = radix_get_mmu_psize(page_size);
1129
1130 _tlbie_lpid_va(addr, lpid, psize, RIC_FLUSH_TLB);
1131 }
1132 EXPORT_SYMBOL_GPL(radix__flush_tlb_lpid_page);
1133
1134 /*
1135 * Flush partition scoped PWC from LPID for all CPUs.
1136 */
radix__flush_pwc_lpid(unsigned int lpid)1137 void radix__flush_pwc_lpid(unsigned int lpid)
1138 {
1139 _tlbie_lpid(lpid, RIC_FLUSH_PWC);
1140 }
1141 EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);
1142
1143 /*
1144 * Flush partition scoped translations from LPID (=LPIDR)
1145 */
radix__flush_all_lpid(unsigned int lpid)1146 void radix__flush_all_lpid(unsigned int lpid)
1147 {
1148 _tlbie_lpid(lpid, RIC_FLUSH_ALL);
1149 }
1150 EXPORT_SYMBOL_GPL(radix__flush_all_lpid);
1151
1152 /*
1153 * Flush process scoped translations from LPID (=LPIDR)
1154 */
radix__flush_all_lpid_guest(unsigned int lpid)1155 void radix__flush_all_lpid_guest(unsigned int lpid)
1156 {
1157 _tlbie_lpid_guest(lpid, RIC_FLUSH_ALL);
1158 }
1159
radix__tlb_flush(struct mmu_gather * tlb)1160 void radix__tlb_flush(struct mmu_gather *tlb)
1161 {
1162 int psize = 0;
1163 struct mm_struct *mm = tlb->mm;
1164 int page_size = tlb->page_size;
1165 unsigned long start = tlb->start;
1166 unsigned long end = tlb->end;
1167
1168 /*
1169 * if page size is not something we understand, do a full mm flush
1170 *
1171 * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
1172 * that flushes the process table entry cache upon process teardown.
1173 * See the comment for radix in arch_exit_mmap().
1174 */
1175 if (tlb->fullmm || tlb->need_flush_all) {
1176 __flush_all_mm(mm, true);
1177 } else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
1178 if (!tlb->freed_tables)
1179 radix__flush_tlb_mm(mm);
1180 else
1181 radix__flush_all_mm(mm);
1182 } else {
1183 if (!tlb->freed_tables)
1184 radix__flush_tlb_range_psize(mm, start, end, psize);
1185 else
1186 radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
1187 }
1188 }
1189
__radix__flush_tlb_range_psize(struct mm_struct * mm,unsigned long start,unsigned long end,int psize,bool also_pwc)1190 static void __radix__flush_tlb_range_psize(struct mm_struct *mm,
1191 unsigned long start, unsigned long end,
1192 int psize, bool also_pwc)
1193 {
1194 unsigned long pid;
1195 unsigned int page_shift = mmu_psize_defs[psize].shift;
1196 unsigned long page_size = 1UL << page_shift;
1197 unsigned long nr_pages = (end - start) >> page_shift;
1198 bool fullmm = (end == TLB_FLUSH_ALL);
1199 bool flush_pid;
1200 enum tlb_flush_type type;
1201
1202 pid = mm->context.id;
1203 if (unlikely(pid == MMU_NO_CONTEXT))
1204 return;
1205
1206 fullmm = (end == TLB_FLUSH_ALL);
1207
1208 preempt_disable();
1209 smp_mb(); /* see radix__flush_tlb_mm */
1210 type = flush_type_needed(mm, fullmm);
1211 if (type == FLUSH_TYPE_NONE)
1212 goto out;
1213
1214 if (fullmm)
1215 flush_pid = true;
1216 else if (type == FLUSH_TYPE_GLOBAL)
1217 flush_pid = nr_pages > tlb_single_page_flush_ceiling;
1218 else
1219 flush_pid = nr_pages > tlb_local_single_page_flush_ceiling;
1220
1221 if (!mmu_has_feature(MMU_FTR_GTSE) && type == FLUSH_TYPE_GLOBAL) {
1222 unsigned long tgt = H_RPTI_TARGET_CMMU;
1223 unsigned long type = H_RPTI_TYPE_TLB;
1224 unsigned long pg_sizes = psize_to_rpti_pgsize(psize);
1225
1226 if (also_pwc)
1227 type |= H_RPTI_TYPE_PWC;
1228 if (atomic_read(&mm->context.copros) > 0)
1229 tgt |= H_RPTI_TARGET_NMMU;
1230 pseries_rpt_invalidate(pid, tgt, type, pg_sizes, start, end);
1231 } else if (flush_pid) {
1232 if (type == FLUSH_TYPE_LOCAL) {
1233 _tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1234 } else {
1235 if (cputlb_use_tlbie()) {
1236 if (mm_needs_flush_escalation(mm))
1237 also_pwc = true;
1238
1239 _tlbie_pid(pid,
1240 also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1241 } else {
1242 _tlbiel_pid_multicast(mm, pid,
1243 also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1244 }
1245
1246 }
1247 } else {
1248 if (type == FLUSH_TYPE_LOCAL)
1249 _tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
1250 else if (cputlb_use_tlbie())
1251 _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
1252 else
1253 _tlbiel_va_range_multicast(mm,
1254 start, end, pid, page_size, psize, also_pwc);
1255 }
1256 out:
1257 preempt_enable();
1258 }
1259
radix__flush_tlb_range_psize(struct mm_struct * mm,unsigned long start,unsigned long end,int psize)1260 void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
1261 unsigned long end, int psize)
1262 {
1263 return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
1264 }
1265
radix__flush_tlb_pwc_range_psize(struct mm_struct * mm,unsigned long start,unsigned long end,int psize)1266 void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
1267 unsigned long end, int psize)
1268 {
1269 __radix__flush_tlb_range_psize(mm, start, end, psize, true);
1270 }
1271
1272 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
radix__flush_tlb_collapsed_pmd(struct mm_struct * mm,unsigned long addr)1273 void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
1274 {
1275 unsigned long pid, end;
1276 enum tlb_flush_type type;
1277
1278 pid = mm->context.id;
1279 if (unlikely(pid == MMU_NO_CONTEXT))
1280 return;
1281
1282 /* 4k page size, just blow the world */
1283 if (PAGE_SIZE == 0x1000) {
1284 radix__flush_all_mm(mm);
1285 return;
1286 }
1287
1288 end = addr + HPAGE_PMD_SIZE;
1289
1290 /* Otherwise first do the PWC, then iterate the pages. */
1291 preempt_disable();
1292 smp_mb(); /* see radix__flush_tlb_mm */
1293 type = flush_type_needed(mm, false);
1294 if (type == FLUSH_TYPE_LOCAL) {
1295 _tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1296 } else if (type == FLUSH_TYPE_GLOBAL) {
1297 if (!mmu_has_feature(MMU_FTR_GTSE)) {
1298 unsigned long tgt, type, pg_sizes;
1299
1300 tgt = H_RPTI_TARGET_CMMU;
1301 type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
1302 H_RPTI_TYPE_PRT;
1303 pg_sizes = psize_to_rpti_pgsize(mmu_virtual_psize);
1304
1305 if (atomic_read(&mm->context.copros) > 0)
1306 tgt |= H_RPTI_TARGET_NMMU;
1307 pseries_rpt_invalidate(pid, tgt, type, pg_sizes,
1308 addr, end);
1309 } else if (cputlb_use_tlbie())
1310 _tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1311 else
1312 _tlbiel_va_range_multicast(mm,
1313 addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1314 }
1315
1316 preempt_enable();
1317 }
1318 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1319
radix__flush_pmd_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)1320 void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
1321 unsigned long start, unsigned long end)
1322 {
1323 radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
1324 }
1325 EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
1326
radix__flush_tlb_all(void)1327 void radix__flush_tlb_all(void)
1328 {
1329 unsigned long rb,prs,r,rs;
1330 unsigned long ric = RIC_FLUSH_ALL;
1331
1332 rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
1333 prs = 0; /* partition scoped */
1334 r = 1; /* radix format */
1335 rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
1336
1337 asm volatile("ptesync": : :"memory");
1338 /*
1339 * now flush guest entries by passing PRS = 1 and LPID != 0
1340 */
1341 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1342 : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
1343 /*
1344 * now flush host entires by passing PRS = 0 and LPID == 0
1345 */
1346 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1347 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
1348 asm volatile("eieio; tlbsync; ptesync": : :"memory");
1349 }
1350
1351 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
__tlbie_pid_lpid(unsigned long pid,unsigned long lpid,unsigned long ric)1352 static __always_inline void __tlbie_pid_lpid(unsigned long pid,
1353 unsigned long lpid,
1354 unsigned long ric)
1355 {
1356 unsigned long rb, rs, prs, r;
1357
1358 rb = PPC_BIT(53); /* IS = 1 */
1359 rs = (pid << PPC_BITLSHIFT(31)) | (lpid & ~(PPC_BITMASK(0, 31)));
1360 prs = 1; /* process scoped */
1361 r = 1; /* radix format */
1362
1363 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1364 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
1365 trace_tlbie(0, 0, rb, rs, ric, prs, r);
1366 }
1367
__tlbie_va_lpid(unsigned long va,unsigned long pid,unsigned long lpid,unsigned long ap,unsigned long ric)1368 static __always_inline void __tlbie_va_lpid(unsigned long va, unsigned long pid,
1369 unsigned long lpid,
1370 unsigned long ap, unsigned long ric)
1371 {
1372 unsigned long rb, rs, prs, r;
1373
1374 rb = va & ~(PPC_BITMASK(52, 63));
1375 rb |= ap << PPC_BITLSHIFT(58);
1376 rs = (pid << PPC_BITLSHIFT(31)) | (lpid & ~(PPC_BITMASK(0, 31)));
1377 prs = 1; /* process scoped */
1378 r = 1; /* radix format */
1379
1380 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1381 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
1382 trace_tlbie(0, 0, rb, rs, ric, prs, r);
1383 }
1384
fixup_tlbie_pid_lpid(unsigned long pid,unsigned long lpid)1385 static inline void fixup_tlbie_pid_lpid(unsigned long pid, unsigned long lpid)
1386 {
1387 /*
1388 * We can use any address for the invalidation, pick one which is
1389 * probably unused as an optimisation.
1390 */
1391 unsigned long va = ((1UL << 52) - 1);
1392
1393 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
1394 asm volatile("ptesync" : : : "memory");
1395 __tlbie_pid_lpid(0, lpid, RIC_FLUSH_TLB);
1396 }
1397
1398 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
1399 asm volatile("ptesync" : : : "memory");
1400 __tlbie_va_lpid(va, pid, lpid, mmu_get_ap(MMU_PAGE_64K),
1401 RIC_FLUSH_TLB);
1402 }
1403 }
1404
_tlbie_pid_lpid(unsigned long pid,unsigned long lpid,unsigned long ric)1405 static inline void _tlbie_pid_lpid(unsigned long pid, unsigned long lpid,
1406 unsigned long ric)
1407 {
1408 asm volatile("ptesync" : : : "memory");
1409
1410 /*
1411 * Workaround the fact that the "ric" argument to __tlbie_pid
1412 * must be a compile-time contraint to match the "i" constraint
1413 * in the asm statement.
1414 */
1415 switch (ric) {
1416 case RIC_FLUSH_TLB:
1417 __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
1418 fixup_tlbie_pid_lpid(pid, lpid);
1419 break;
1420 case RIC_FLUSH_PWC:
1421 __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
1422 break;
1423 case RIC_FLUSH_ALL:
1424 default:
1425 __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_ALL);
1426 fixup_tlbie_pid_lpid(pid, lpid);
1427 }
1428 asm volatile("eieio; tlbsync; ptesync" : : : "memory");
1429 }
1430
fixup_tlbie_va_range_lpid(unsigned long va,unsigned long pid,unsigned long lpid,unsigned long ap)1431 static inline void fixup_tlbie_va_range_lpid(unsigned long va,
1432 unsigned long pid,
1433 unsigned long lpid,
1434 unsigned long ap)
1435 {
1436 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
1437 asm volatile("ptesync" : : : "memory");
1438 __tlbie_pid_lpid(0, lpid, RIC_FLUSH_TLB);
1439 }
1440
1441 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
1442 asm volatile("ptesync" : : : "memory");
1443 __tlbie_va_lpid(va, pid, lpid, ap, RIC_FLUSH_TLB);
1444 }
1445 }
1446
__tlbie_va_range_lpid(unsigned long start,unsigned long end,unsigned long pid,unsigned long lpid,unsigned long page_size,unsigned long psize)1447 static inline void __tlbie_va_range_lpid(unsigned long start, unsigned long end,
1448 unsigned long pid, unsigned long lpid,
1449 unsigned long page_size,
1450 unsigned long psize)
1451 {
1452 unsigned long addr;
1453 unsigned long ap = mmu_get_ap(psize);
1454
1455 for (addr = start; addr < end; addr += page_size)
1456 __tlbie_va_lpid(addr, pid, lpid, ap, RIC_FLUSH_TLB);
1457
1458 fixup_tlbie_va_range_lpid(addr - page_size, pid, lpid, ap);
1459 }
1460
_tlbie_va_range_lpid(unsigned long start,unsigned long end,unsigned long pid,unsigned long lpid,unsigned long page_size,unsigned long psize,bool also_pwc)1461 static inline void _tlbie_va_range_lpid(unsigned long start, unsigned long end,
1462 unsigned long pid, unsigned long lpid,
1463 unsigned long page_size,
1464 unsigned long psize, bool also_pwc)
1465 {
1466 asm volatile("ptesync" : : : "memory");
1467 if (also_pwc)
1468 __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
1469 __tlbie_va_range_lpid(start, end, pid, lpid, page_size, psize);
1470 asm volatile("eieio; tlbsync; ptesync" : : : "memory");
1471 }
1472
1473 /*
1474 * Performs process-scoped invalidations for a given LPID
1475 * as part of H_RPT_INVALIDATE hcall.
1476 */
do_h_rpt_invalidate_prt(unsigned long pid,unsigned long lpid,unsigned long type,unsigned long pg_sizes,unsigned long start,unsigned long end)1477 void do_h_rpt_invalidate_prt(unsigned long pid, unsigned long lpid,
1478 unsigned long type, unsigned long pg_sizes,
1479 unsigned long start, unsigned long end)
1480 {
1481 unsigned long psize, nr_pages;
1482 struct mmu_psize_def *def;
1483 bool flush_pid;
1484
1485 /*
1486 * A H_RPTI_TYPE_ALL request implies RIC=3, hence
1487 * do a single IS=1 based flush.
1488 */
1489 if ((type & H_RPTI_TYPE_ALL) == H_RPTI_TYPE_ALL) {
1490 _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_ALL);
1491 return;
1492 }
1493
1494 if (type & H_RPTI_TYPE_PWC)
1495 _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
1496
1497 /* Full PID flush */
1498 if (start == 0 && end == -1)
1499 return _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
1500
1501 /* Do range invalidation for all the valid page sizes */
1502 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1503 def = &mmu_psize_defs[psize];
1504 if (!(pg_sizes & def->h_rpt_pgsize))
1505 continue;
1506
1507 nr_pages = (end - start) >> def->shift;
1508 flush_pid = nr_pages > tlb_single_page_flush_ceiling;
1509
1510 /*
1511 * If the number of pages spanning the range is above
1512 * the ceiling, convert the request into a full PID flush.
1513 * And since PID flush takes out all the page sizes, there
1514 * is no need to consider remaining page sizes.
1515 */
1516 if (flush_pid) {
1517 _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
1518 return;
1519 }
1520 _tlbie_va_range_lpid(start, end, pid, lpid,
1521 (1UL << def->shift), psize, false);
1522 }
1523 }
1524 EXPORT_SYMBOL_GPL(do_h_rpt_invalidate_prt);
1525
1526 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1527
create_tlb_single_page_flush_ceiling(void)1528 static int __init create_tlb_single_page_flush_ceiling(void)
1529 {
1530 debugfs_create_u32("tlb_single_page_flush_ceiling", 0600,
1531 arch_debugfs_dir, &tlb_single_page_flush_ceiling);
1532 debugfs_create_u32("tlb_local_single_page_flush_ceiling", 0600,
1533 arch_debugfs_dir, &tlb_local_single_page_flush_ceiling);
1534 return 0;
1535 }
1536 late_initcall(create_tlb_single_page_flush_ceiling);
1537
1538