• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
tlbiel_radix_set_isa300(unsigned int set,unsigned int is,unsigned int pid,unsigned int ric,unsigned int prs)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 
tlbiel_all_isa300(unsigned int num_sets,unsigned int is)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 
radix__tlbiel_all(unsigned int action)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 
__tlbiel_pid(unsigned long pid,int set,unsigned long ric)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 
__tlbie_pid(unsigned long pid,unsigned long ric)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 
__tlbiel_lpid(unsigned long lpid,int set,unsigned long ric)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 
__tlbie_lpid(unsigned long lpid,unsigned long ric)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 
__tlbiel_lpid_guest(unsigned long lpid,int set,unsigned long ric)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 
__tlbiel_va(unsigned long va,unsigned long pid,unsigned long ap,unsigned long ric)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 
__tlbie_va(unsigned long va,unsigned long pid,unsigned long ap,unsigned long ric)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 
__tlbie_lpid_va(unsigned long va,unsigned long lpid,unsigned long ap,unsigned long ric)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 
fixup_tlbie_va(unsigned long va,unsigned long pid,unsigned long ap)219 static 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 
fixup_tlbie_va_range(unsigned long va,unsigned long pid,unsigned long ap)233 static 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 
fixup_tlbie_pid(unsigned long pid)247 static 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 
fixup_tlbie_lpid_va(unsigned long va,unsigned long lpid,unsigned long ap)267 static 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 
fixup_tlbie_lpid(unsigned long lpid)281 static 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  */
_tlbiel_pid(unsigned long pid,unsigned long ric)303 static 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 
_tlbie_pid(unsigned long pid,unsigned long ric)329 static 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 
_tlbiel_lpid(unsigned long lpid,unsigned long ric)354 static 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 
_tlbie_lpid(unsigned long lpid,unsigned long ric)382 static 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 
_tlbiel_lpid_guest(unsigned long lpid,unsigned long ric)407 static 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 
__tlbiel_va_range(unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize)436 static 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 
_tlbiel_va(unsigned long va,unsigned long pid,unsigned long psize,unsigned long ric)447 static 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 
_tlbiel_va_range(unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize,bool also_pwc)457 static 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 
__tlbie_va_range(unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize)468 static 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 
_tlbie_va(unsigned long va,unsigned long pid,unsigned long psize,unsigned long ric)481 static 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 
_tlbie_lpid_va(unsigned long va,unsigned long lpid,unsigned long psize,unsigned long ric)492 static 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 
_tlbie_va_range(unsigned long start,unsigned long end,unsigned long pid,unsigned long page_size,unsigned long psize,bool also_pwc)503 static 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  */
radix__local_flush_tlb_mm(struct mm_struct * mm)525 void 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 }
535 EXPORT_SYMBOL(radix__local_flush_tlb_mm);
536 
537 #ifndef CONFIG_SMP
radix__local_flush_all_mm(struct mm_struct * mm)538 void 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 }
548 EXPORT_SYMBOL(radix__local_flush_all_mm);
549 #endif /* CONFIG_SMP */
550 
radix__local_flush_tlb_page_psize(struct mm_struct * mm,unsigned long vmaddr,int psize)551 void 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 
radix__local_flush_tlb_page(struct vm_area_struct * vma,unsigned long vmaddr)563 void 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 }
572 EXPORT_SYMBOL(radix__local_flush_tlb_page);
573 
mm_is_singlethreaded(struct mm_struct * mm)574 static 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 
mm_needs_flush_escalation(struct mm_struct * mm)583 static 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
do_exit_flush_lazy_tlb(void * arg)596 static void do_exit_flush_lazy_tlb(void *arg)
597 {
598 	struct mm_struct *mm = arg;
599 	unsigned long pid = mm->context.id;
600 
601 	/*
602 	 * A kthread could have done a mmget_not_zero() after the flushing CPU
603 	 * checked mm_is_singlethreaded, and be in the process of
604 	 * kthread_use_mm when interrupted here. In that case, current->mm will
605 	 * be set to mm, because kthread_use_mm() setting ->mm and switching to
606 	 * the mm is done with interrupts off.
607 	 */
608 	if (current->mm == mm)
609 		goto out_flush;
610 
611 	if (current->active_mm == mm) {
612 		WARN_ON_ONCE(current->mm != NULL);
613 		/* Is a kernel thread and is using mm as the lazy tlb */
614 		mmgrab(&init_mm);
615 		current->active_mm = &init_mm;
616 		switch_mm_irqs_off(mm, &init_mm, current);
617 		mmdrop(mm);
618 	}
619 
620 	atomic_dec(&mm->context.active_cpus);
621 	cpumask_clear_cpu(smp_processor_id(), mm_cpumask(mm));
622 
623 out_flush:
624 	_tlbiel_pid(pid, RIC_FLUSH_ALL);
625 }
626 
exit_flush_lazy_tlbs(struct mm_struct * mm)627 static void exit_flush_lazy_tlbs(struct mm_struct *mm)
628 {
629 	/*
630 	 * Would be nice if this was async so it could be run in
631 	 * parallel with our local flush, but generic code does not
632 	 * give a good API for it. Could extend the generic code or
633 	 * make a special powerpc IPI for flushing TLBs.
634 	 * For now it's not too performance critical.
635 	 */
636 	smp_call_function_many(mm_cpumask(mm), do_exit_flush_lazy_tlb,
637 				(void *)mm, 1);
638 }
639 
radix__flush_tlb_mm(struct mm_struct * mm)640 void radix__flush_tlb_mm(struct mm_struct *mm)
641 {
642 	unsigned long pid;
643 
644 	pid = mm->context.id;
645 	if (unlikely(pid == MMU_NO_CONTEXT))
646 		return;
647 
648 	preempt_disable();
649 	/*
650 	 * Order loads of mm_cpumask vs previous stores to clear ptes before
651 	 * the invalidate. See barrier in switch_mm_irqs_off
652 	 */
653 	smp_mb();
654 	if (!mm_is_thread_local(mm)) {
655 		if (unlikely(mm_is_singlethreaded(mm))) {
656 			exit_flush_lazy_tlbs(mm);
657 			goto local;
658 		}
659 
660 		if (mm_needs_flush_escalation(mm))
661 			_tlbie_pid(pid, RIC_FLUSH_ALL);
662 		else
663 			_tlbie_pid(pid, RIC_FLUSH_TLB);
664 	} else {
665 local:
666 		_tlbiel_pid(pid, RIC_FLUSH_TLB);
667 	}
668 	preempt_enable();
669 }
670 EXPORT_SYMBOL(radix__flush_tlb_mm);
671 
__flush_all_mm(struct mm_struct * mm,bool fullmm)672 static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
673 {
674 	unsigned long pid;
675 
676 	pid = mm->context.id;
677 	if (unlikely(pid == MMU_NO_CONTEXT))
678 		return;
679 
680 	preempt_disable();
681 	smp_mb(); /* see radix__flush_tlb_mm */
682 	if (!mm_is_thread_local(mm)) {
683 		if (unlikely(mm_is_singlethreaded(mm))) {
684 			if (!fullmm) {
685 				exit_flush_lazy_tlbs(mm);
686 				goto local;
687 			}
688 		}
689 		_tlbie_pid(pid, RIC_FLUSH_ALL);
690 	} else {
691 local:
692 		_tlbiel_pid(pid, RIC_FLUSH_ALL);
693 	}
694 	preempt_enable();
695 }
radix__flush_all_mm(struct mm_struct * mm)696 void radix__flush_all_mm(struct mm_struct *mm)
697 {
698 	__flush_all_mm(mm, false);
699 }
700 EXPORT_SYMBOL(radix__flush_all_mm);
701 
radix__flush_tlb_pwc(struct mmu_gather * tlb,unsigned long addr)702 void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
703 {
704 	tlb->need_flush_all = 1;
705 }
706 EXPORT_SYMBOL(radix__flush_tlb_pwc);
707 
radix__flush_tlb_page_psize(struct mm_struct * mm,unsigned long vmaddr,int psize)708 void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
709 				 int psize)
710 {
711 	unsigned long pid;
712 
713 	pid = mm->context.id;
714 	if (unlikely(pid == MMU_NO_CONTEXT))
715 		return;
716 
717 	preempt_disable();
718 	smp_mb(); /* see radix__flush_tlb_mm */
719 	if (!mm_is_thread_local(mm)) {
720 		if (unlikely(mm_is_singlethreaded(mm))) {
721 			exit_flush_lazy_tlbs(mm);
722 			goto local;
723 		}
724 		_tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
725 	} else {
726 local:
727 		_tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
728 	}
729 	preempt_enable();
730 }
731 
radix__flush_tlb_page(struct vm_area_struct * vma,unsigned long vmaddr)732 void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
733 {
734 #ifdef CONFIG_HUGETLB_PAGE
735 	if (is_vm_hugetlb_page(vma))
736 		return radix__flush_hugetlb_page(vma, vmaddr);
737 #endif
738 	radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
739 }
740 EXPORT_SYMBOL(radix__flush_tlb_page);
741 
742 #else /* CONFIG_SMP */
743 #define radix__flush_all_mm radix__local_flush_all_mm
744 #endif /* CONFIG_SMP */
745 
radix__flush_tlb_kernel_range(unsigned long start,unsigned long end)746 void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
747 {
748 	_tlbie_pid(0, RIC_FLUSH_ALL);
749 }
750 EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
751 
752 #define TLB_FLUSH_ALL -1UL
753 
754 /*
755  * Number of pages above which we invalidate the entire PID rather than
756  * flush individual pages, for local and global flushes respectively.
757  *
758  * tlbie goes out to the interconnect and individual ops are more costly.
759  * It also does not iterate over sets like the local tlbiel variant when
760  * invalidating a full PID, so it has a far lower threshold to change from
761  * individual page flushes to full-pid flushes.
762  */
763 static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
764 static unsigned long tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
765 
__radix__flush_tlb_range(struct mm_struct * mm,unsigned long start,unsigned long end,bool flush_all_sizes)766 static inline void __radix__flush_tlb_range(struct mm_struct *mm,
767 					unsigned long start, unsigned long end,
768 					bool flush_all_sizes)
769 
770 {
771 	unsigned long pid;
772 	unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
773 	unsigned long page_size = 1UL << page_shift;
774 	unsigned long nr_pages = (end - start) >> page_shift;
775 	bool local, full;
776 
777 	pid = mm->context.id;
778 	if (unlikely(pid == MMU_NO_CONTEXT))
779 		return;
780 
781 	preempt_disable();
782 	smp_mb(); /* see radix__flush_tlb_mm */
783 	if (!mm_is_thread_local(mm)) {
784 		if (unlikely(mm_is_singlethreaded(mm))) {
785 			if (end != TLB_FLUSH_ALL) {
786 				exit_flush_lazy_tlbs(mm);
787 				goto is_local;
788 			}
789 		}
790 		local = false;
791 		full = (end == TLB_FLUSH_ALL ||
792 				nr_pages > tlb_single_page_flush_ceiling);
793 	} else {
794 is_local:
795 		local = true;
796 		full = (end == TLB_FLUSH_ALL ||
797 				nr_pages > tlb_local_single_page_flush_ceiling);
798 	}
799 
800 	if (full) {
801 		if (local) {
802 			_tlbiel_pid(pid, RIC_FLUSH_TLB);
803 		} else {
804 			if (mm_needs_flush_escalation(mm))
805 				_tlbie_pid(pid, RIC_FLUSH_ALL);
806 			else
807 				_tlbie_pid(pid, RIC_FLUSH_TLB);
808 		}
809 	} else {
810 		bool hflush = flush_all_sizes;
811 		bool gflush = flush_all_sizes;
812 		unsigned long hstart, hend;
813 		unsigned long gstart, gend;
814 
815 		if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
816 			hflush = true;
817 
818 		if (hflush) {
819 			hstart = (start + PMD_SIZE - 1) & PMD_MASK;
820 			hend = end & PMD_MASK;
821 			if (hstart == hend)
822 				hflush = false;
823 		}
824 
825 		if (gflush) {
826 			gstart = (start + PUD_SIZE - 1) & PUD_MASK;
827 			gend = end & PUD_MASK;
828 			if (gstart == gend)
829 				gflush = false;
830 		}
831 
832 		asm volatile("ptesync": : :"memory");
833 		if (local) {
834 			__tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
835 			if (hflush)
836 				__tlbiel_va_range(hstart, hend, pid,
837 						PMD_SIZE, MMU_PAGE_2M);
838 			if (gflush)
839 				__tlbiel_va_range(gstart, gend, pid,
840 						PUD_SIZE, MMU_PAGE_1G);
841 			asm volatile("ptesync": : :"memory");
842 		} else {
843 			__tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
844 			if (hflush)
845 				__tlbie_va_range(hstart, hend, pid,
846 						PMD_SIZE, MMU_PAGE_2M);
847 			if (gflush)
848 				__tlbie_va_range(gstart, gend, pid,
849 						PUD_SIZE, MMU_PAGE_1G);
850 
851 			asm volatile("eieio; tlbsync; ptesync": : :"memory");
852 		}
853 	}
854 	preempt_enable();
855 }
856 
radix__flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)857 void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
858 		     unsigned long end)
859 
860 {
861 #ifdef CONFIG_HUGETLB_PAGE
862 	if (is_vm_hugetlb_page(vma))
863 		return radix__flush_hugetlb_tlb_range(vma, start, end);
864 #endif
865 
866 	__radix__flush_tlb_range(vma->vm_mm, start, end, false);
867 }
868 EXPORT_SYMBOL(radix__flush_tlb_range);
869 
radix_get_mmu_psize(int page_size)870 static int radix_get_mmu_psize(int page_size)
871 {
872 	int psize;
873 
874 	if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
875 		psize = mmu_virtual_psize;
876 	else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
877 		psize = MMU_PAGE_2M;
878 	else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
879 		psize = MMU_PAGE_1G;
880 	else
881 		return -1;
882 	return psize;
883 }
884 
885 /*
886  * Flush partition scoped LPID address translation for all CPUs.
887  */
radix__flush_tlb_lpid_page(unsigned int lpid,unsigned long addr,unsigned long page_size)888 void radix__flush_tlb_lpid_page(unsigned int lpid,
889 					unsigned long addr,
890 					unsigned long page_size)
891 {
892 	int psize = radix_get_mmu_psize(page_size);
893 
894 	_tlbie_lpid_va(addr, lpid, psize, RIC_FLUSH_TLB);
895 }
896 EXPORT_SYMBOL_GPL(radix__flush_tlb_lpid_page);
897 
898 /*
899  * Flush partition scoped PWC from LPID for all CPUs.
900  */
radix__flush_pwc_lpid(unsigned int lpid)901 void radix__flush_pwc_lpid(unsigned int lpid)
902 {
903 	_tlbie_lpid(lpid, RIC_FLUSH_PWC);
904 }
905 EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);
906 
907 /*
908  * Flush partition scoped translations from LPID (=LPIDR)
909  */
radix__local_flush_tlb_lpid(unsigned int lpid)910 void radix__local_flush_tlb_lpid(unsigned int lpid)
911 {
912 	_tlbiel_lpid(lpid, RIC_FLUSH_ALL);
913 }
914 EXPORT_SYMBOL_GPL(radix__local_flush_tlb_lpid);
915 
916 /*
917  * Flush process scoped translations from LPID (=LPIDR).
918  * Important difference, the guest normally manages its own translations,
919  * but some cases e.g., vCPU CPU migration require KVM to flush.
920  */
radix__local_flush_tlb_lpid_guest(unsigned int lpid)921 void radix__local_flush_tlb_lpid_guest(unsigned int lpid)
922 {
923 	_tlbiel_lpid_guest(lpid, RIC_FLUSH_ALL);
924 }
925 EXPORT_SYMBOL_GPL(radix__local_flush_tlb_lpid_guest);
926 
927 
928 static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
929 				  unsigned long end, int psize);
930 
radix__tlb_flush(struct mmu_gather * tlb)931 void radix__tlb_flush(struct mmu_gather *tlb)
932 {
933 	int psize = 0;
934 	struct mm_struct *mm = tlb->mm;
935 	int page_size = tlb->page_size;
936 	unsigned long start = tlb->start;
937 	unsigned long end = tlb->end;
938 
939 	/*
940 	 * if page size is not something we understand, do a full mm flush
941 	 *
942 	 * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
943 	 * that flushes the process table entry cache upon process teardown.
944 	 * See the comment for radix in arch_exit_mmap().
945 	 */
946 	if (tlb->fullmm) {
947 		__flush_all_mm(mm, true);
948 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE)
949 	} else if (mm_tlb_flush_nested(mm)) {
950 		/*
951 		 * If there is a concurrent invalidation that is clearing ptes,
952 		 * then it's possible this invalidation will miss one of those
953 		 * cleared ptes and miss flushing the TLB. If this invalidate
954 		 * returns before the other one flushes TLBs, that can result
955 		 * in it returning while there are still valid TLBs inside the
956 		 * range to be invalidated.
957 		 *
958 		 * See mm/memory.c:tlb_finish_mmu() for more details.
959 		 *
960 		 * The solution to this is ensure the entire range is always
961 		 * flushed here. The problem for powerpc is that the flushes
962 		 * are page size specific, so this "forced flush" would not
963 		 * do the right thing if there are a mix of page sizes in
964 		 * the range to be invalidated. So use __flush_tlb_range
965 		 * which invalidates all possible page sizes in the range.
966 		 *
967 		 * PWC flush probably is not be required because the core code
968 		 * shouldn't free page tables in this path, but accounting
969 		 * for the possibility makes us a bit more robust.
970 		 *
971 		 * need_flush_all is an uncommon case because page table
972 		 * teardown should be done with exclusive locks held (but
973 		 * after locks are dropped another invalidate could come
974 		 * in), it could be optimized further if necessary.
975 		 */
976 		if (!tlb->need_flush_all)
977 			__radix__flush_tlb_range(mm, start, end, true);
978 		else
979 			radix__flush_all_mm(mm);
980 #endif
981 	} else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
982 		if (!tlb->need_flush_all)
983 			radix__flush_tlb_mm(mm);
984 		else
985 			radix__flush_all_mm(mm);
986 	} else {
987 		if (!tlb->need_flush_all)
988 			radix__flush_tlb_range_psize(mm, start, end, psize);
989 		else
990 			radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
991 	}
992 	tlb->need_flush_all = 0;
993 }
994 
__radix__flush_tlb_range_psize(struct mm_struct * mm,unsigned long start,unsigned long end,int psize,bool also_pwc)995 static inline void __radix__flush_tlb_range_psize(struct mm_struct *mm,
996 				unsigned long start, unsigned long end,
997 				int psize, bool also_pwc)
998 {
999 	unsigned long pid;
1000 	unsigned int page_shift = mmu_psize_defs[psize].shift;
1001 	unsigned long page_size = 1UL << page_shift;
1002 	unsigned long nr_pages = (end - start) >> page_shift;
1003 	bool local, full;
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 	if (!mm_is_thread_local(mm)) {
1012 		if (unlikely(mm_is_singlethreaded(mm))) {
1013 			if (end != TLB_FLUSH_ALL) {
1014 				exit_flush_lazy_tlbs(mm);
1015 				goto is_local;
1016 			}
1017 		}
1018 		local = false;
1019 		full = (end == TLB_FLUSH_ALL ||
1020 				nr_pages > tlb_single_page_flush_ceiling);
1021 	} else {
1022 is_local:
1023 		local = true;
1024 		full = (end == TLB_FLUSH_ALL ||
1025 				nr_pages > tlb_local_single_page_flush_ceiling);
1026 	}
1027 
1028 	if (full) {
1029 		if (local) {
1030 			_tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1031 		} else {
1032 			if (mm_needs_flush_escalation(mm))
1033 				also_pwc = true;
1034 
1035 			_tlbie_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
1036 		}
1037 	} else {
1038 		if (local)
1039 			_tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
1040 		else
1041 			_tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
1042 	}
1043 	preempt_enable();
1044 }
1045 
radix__flush_tlb_range_psize(struct mm_struct * mm,unsigned long start,unsigned long end,int psize)1046 void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
1047 				  unsigned long end, int psize)
1048 {
1049 	return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
1050 }
1051 
radix__flush_tlb_pwc_range_psize(struct mm_struct * mm,unsigned long start,unsigned long end,int psize)1052 static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
1053 				  unsigned long end, int psize)
1054 {
1055 	__radix__flush_tlb_range_psize(mm, start, end, psize, true);
1056 }
1057 
1058 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
radix__flush_tlb_collapsed_pmd(struct mm_struct * mm,unsigned long addr)1059 void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
1060 {
1061 	unsigned long pid, end;
1062 
1063 	pid = mm->context.id;
1064 	if (unlikely(pid == MMU_NO_CONTEXT))
1065 		return;
1066 
1067 	/* 4k page size, just blow the world */
1068 	if (PAGE_SIZE == 0x1000) {
1069 		radix__flush_all_mm(mm);
1070 		return;
1071 	}
1072 
1073 	end = addr + HPAGE_PMD_SIZE;
1074 
1075 	/* Otherwise first do the PWC, then iterate the pages. */
1076 	preempt_disable();
1077 	smp_mb(); /* see radix__flush_tlb_mm */
1078 	if (!mm_is_thread_local(mm)) {
1079 		if (unlikely(mm_is_singlethreaded(mm))) {
1080 			exit_flush_lazy_tlbs(mm);
1081 			goto local;
1082 		}
1083 		_tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1084 	} else {
1085 local:
1086 		_tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
1087 	}
1088 
1089 	preempt_enable();
1090 }
1091 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1092 
radix__flush_pmd_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)1093 void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
1094 				unsigned long start, unsigned long end)
1095 {
1096 	radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
1097 }
1098 EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
1099 
radix__flush_tlb_all(void)1100 void radix__flush_tlb_all(void)
1101 {
1102 	unsigned long rb,prs,r,rs;
1103 	unsigned long ric = RIC_FLUSH_ALL;
1104 
1105 	rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
1106 	prs = 0; /* partition scoped */
1107 	r = 1;   /* radix format */
1108 	rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
1109 
1110 	asm volatile("ptesync": : :"memory");
1111 	/*
1112 	 * now flush guest entries by passing PRS = 1 and LPID != 0
1113 	 */
1114 	asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1115 		     : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
1116 	/*
1117 	 * now flush host entires by passing PRS = 0 and LPID == 0
1118 	 */
1119 	asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
1120 		     : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
1121 	asm volatile("eieio; tlbsync; ptesync": : :"memory");
1122 }
1123 
1124 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
radix_kvm_prefetch_workaround(struct mm_struct * mm)1125 extern void radix_kvm_prefetch_workaround(struct mm_struct *mm)
1126 {
1127 	unsigned long pid = mm->context.id;
1128 
1129 	if (unlikely(pid == MMU_NO_CONTEXT))
1130 		return;
1131 
1132 	/*
1133 	 * If this context hasn't run on that CPU before and KVM is
1134 	 * around, there's a slim chance that the guest on another
1135 	 * CPU just brought in obsolete translation into the TLB of
1136 	 * this CPU due to a bad prefetch using the guest PID on
1137 	 * the way into the hypervisor.
1138 	 *
1139 	 * We work around this here. If KVM is possible, we check if
1140 	 * any sibling thread is in KVM. If it is, the window may exist
1141 	 * and thus we flush that PID from the core.
1142 	 *
1143 	 * A potential future improvement would be to mark which PIDs
1144 	 * have never been used on the system and avoid it if the PID
1145 	 * is new and the process has no other cpumask bit set.
1146 	 */
1147 	if (cpu_has_feature(CPU_FTR_HVMODE) && radix_enabled()) {
1148 		int cpu = smp_processor_id();
1149 		int sib = cpu_first_thread_sibling(cpu);
1150 		bool flush = false;
1151 
1152 		for (; sib <= cpu_last_thread_sibling(cpu) && !flush; sib++) {
1153 			if (sib == cpu)
1154 				continue;
1155 			if (!cpu_possible(sib))
1156 				continue;
1157 			if (paca_ptrs[sib]->kvm_hstate.kvm_vcpu)
1158 				flush = true;
1159 		}
1160 		if (flush)
1161 			_tlbiel_pid(pid, RIC_FLUSH_ALL);
1162 	}
1163 }
1164 EXPORT_SYMBOL_GPL(radix_kvm_prefetch_workaround);
1165 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1166