tlb-radix.c 19.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * TLB flush routines for radix kernels.
 *
 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/mm.h>
#include <linux/hugetlb.h>
#include <linux/memblock.h>

16
#include <asm/ppc-opcode.h>
17 18
#include <asm/tlb.h>
#include <asm/tlbflush.h>
19
#include <asm/trace.h>
20
#include <asm/cputhreads.h>
21

22 23 24 25
#define RIC_FLUSH_TLB 0
#define RIC_FLUSH_PWC 1
#define RIC_FLUSH_ALL 2

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/*
 * tlbiel instruction for radix, set invalidation
 * i.e., r=1 and is=01 or is=10 or is=11
 */
static inline void tlbiel_radix_set_isa300(unsigned int set, unsigned int is,
					unsigned int pid,
					unsigned int ric, unsigned int prs)
{
	unsigned long rb;
	unsigned long rs;
	unsigned int r = 1; /* radix format */

	rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
	rs = ((unsigned long)pid << PPC_BITLSHIFT(31));

	asm volatile(PPC_TLBIEL(%0, %1, %2, %3, %4)
		     : : "r"(rb), "r"(rs), "i"(ric), "i"(prs), "r"(r)
		     : "memory");
}

static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
{
	unsigned int set;

	asm volatile("ptesync": : :"memory");

	/*
	 * Flush the first set of the TLB, and the entire Page Walk Cache
	 * and partition table entries. Then flush the remaining sets of the
	 * TLB.
	 */
	tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 0);
	for (set = 1; set < num_sets; set++)
		tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 0);

	/* Do the same for process scoped entries. */
	tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 1);
	for (set = 1; set < num_sets; set++)
		tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 1);

	asm volatile("ptesync": : :"memory");
}

void radix__tlbiel_all(unsigned int action)
{
	unsigned int is;

	switch (action) {
	case TLB_INVAL_SCOPE_GLOBAL:
		is = 3;
		break;
	case TLB_INVAL_SCOPE_LPID:
		is = 2;
		break;
	default:
		BUG();
	}

	if (early_cpu_has_feature(CPU_FTR_ARCH_300))
		tlbiel_all_isa300(POWER9_TLB_SETS_RADIX, is);
	else
		WARN(1, "%s called on pre-POWER9 CPU\n", __func__);

	asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
}

92 93
static inline void __tlbiel_pid(unsigned long pid, int set,
				unsigned long ric)
94
{
95
	unsigned long rb,rs,prs,r;
96 97 98 99 100 101 102

	rb = PPC_BIT(53); /* IS = 1 */
	rb |= set << PPC_BITLSHIFT(51);
	rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
	prs = 1; /* process scoped */
	r = 1;   /* raidx format */

103
	asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
104
		     : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
105
	trace_tlbie(0, 1, rb, rs, ric, prs, r);
106 107
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121
static inline void __tlbie_pid(unsigned long pid, unsigned long ric)
{
	unsigned long rb,rs,prs,r;

	rb = PPC_BIT(53); /* IS = 1 */
	rs = pid << PPC_BITLSHIFT(31);
	prs = 1; /* process scoped */
	r = 1;   /* raidx format */

	asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
		     : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
	trace_tlbie(0, 0, rb, rs, ric, prs, r);
}

122 123 124
/*
 * We use 128 set in radix mode and 256 set in hpt mode.
 */
125
static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
126 127 128
{
	int set;

129
	asm volatile("ptesync": : :"memory");
130 131 132 133 134 135 136

	/*
	 * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
	 * also flush the entire Page Walk Cache.
	 */
	__tlbiel_pid(pid, 0, ric);

137 138 139 140 141
	/* For PWC, only one flush is needed */
	if (ric == RIC_FLUSH_PWC) {
		asm volatile("ptesync": : :"memory");
		return;
	}
142

143
	/* For the remaining sets, just flush the TLB */
144
	for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
145
		__tlbiel_pid(pid, set, RIC_FLUSH_TLB);
146

147
	asm volatile("ptesync": : :"memory");
148
	asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
149 150
}

151
static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
152 153
{
	asm volatile("ptesync": : :"memory");
154
	__tlbie_pid(pid, ric);
155 156 157
	asm volatile("eieio; tlbsync; ptesync": : :"memory");
}

158
static 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 164 165 166 167 168

	rb = va & ~(PPC_BITMASK(52, 63));
	rb |= ap << PPC_BITLSHIFT(58);
	rs = pid << PPC_BITLSHIFT(31);
	prs = 1; /* process scoped */
	r = 1;   /* raidx format */

169
	asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
170
		     : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
171
	trace_tlbie(0, 1, rb, rs, ric, prs, r);
172 173
}

174 175 176 177 178 179 180 181 182 183 184
static inline void __tlbiel_va_range(unsigned long start, unsigned long end,
				    unsigned long pid, unsigned long page_size,
				    unsigned long psize)
{
	unsigned long addr;
	unsigned long ap = mmu_get_ap(psize);

	for (addr = start; addr < end; addr += page_size)
		__tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
}

185
static inline void _tlbiel_va(unsigned long va, unsigned long pid,
186
			      unsigned long psize, unsigned long ric)
187
{
188 189
	unsigned long ap = mmu_get_ap(psize);

190 191 192 193 194
	asm volatile("ptesync": : :"memory");
	__tlbiel_va(va, pid, ap, ric);
	asm volatile("ptesync": : :"memory");
}

195 196
static inline void _tlbiel_va_range(unsigned long start, unsigned long end,
				    unsigned long pid, unsigned long page_size,
197
				    unsigned long psize, bool also_pwc)
198 199
{
	asm volatile("ptesync": : :"memory");
200 201
	if (also_pwc)
		__tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
202
	__tlbiel_va_range(start, end, pid, page_size, psize);
203 204 205
	asm volatile("ptesync": : :"memory");
}

206
static inline void __tlbie_va(unsigned long va, unsigned long pid,
207
			     unsigned long ap, unsigned long ric)
208
{
209
	unsigned long rb,rs,prs,r;
210 211 212 213 214 215 216

	rb = va & ~(PPC_BITMASK(52, 63));
	rb |= ap << PPC_BITLSHIFT(58);
	rs = pid << PPC_BITLSHIFT(31);
	prs = 1; /* process scoped */
	r = 1;   /* raidx format */

217
	asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
218
		     : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
219
	trace_tlbie(0, 0, rb, rs, ric, prs, r);
220 221
}

222 223 224 225 226 227 228 229 230 231 232
static inline void __tlbie_va_range(unsigned long start, unsigned long end,
				    unsigned long pid, unsigned long page_size,
				    unsigned long psize)
{
	unsigned long addr;
	unsigned long ap = mmu_get_ap(psize);

	for (addr = start; addr < end; addr += page_size)
		__tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
}

233
static inline void _tlbie_va(unsigned long va, unsigned long pid,
234
			      unsigned long psize, unsigned long ric)
235
{
236 237
	unsigned long ap = mmu_get_ap(psize);

238 239 240 241 242
	asm volatile("ptesync": : :"memory");
	__tlbie_va(va, pid, ap, ric);
	asm volatile("eieio; tlbsync; ptesync": : :"memory");
}

243 244
static inline void _tlbie_va_range(unsigned long start, unsigned long end,
				    unsigned long pid, unsigned long page_size,
245
				    unsigned long psize, bool also_pwc)
246 247
{
	asm volatile("ptesync": : :"memory");
248 249
	if (also_pwc)
		__tlbie_pid(pid, RIC_FLUSH_PWC);
250
	__tlbie_va_range(start, end, pid, page_size, psize);
251 252
	asm volatile("eieio; tlbsync; ptesync": : :"memory");
}
253

254 255 256 257 258 259 260 261 262 263 264 265 266
/*
 * Base TLB flushing operations:
 *
 *  - flush_tlb_mm(mm) flushes the specified mm context TLB's
 *  - flush_tlb_page(vma, vmaddr) flushes one page
 *  - flush_tlb_range(vma, start, end) flushes a range of pages
 *  - flush_tlb_kernel_range(start, end) flushes kernel pages
 *
 *  - local_* variants of page and mm only apply to the current
 *    processor
 */
void radix__local_flush_tlb_mm(struct mm_struct *mm)
{
267
	unsigned long pid;
268 269 270 271

	preempt_disable();
	pid = mm->context.id;
	if (pid != MMU_NO_CONTEXT)
272
		_tlbiel_pid(pid, RIC_FLUSH_TLB);
273 274 275 276
	preempt_enable();
}
EXPORT_SYMBOL(radix__local_flush_tlb_mm);

277
#ifndef CONFIG_SMP
278
void radix__local_flush_all_mm(struct mm_struct *mm)
279 280 281 282 283 284
{
	unsigned long pid;

	preempt_disable();
	pid = mm->context.id;
	if (pid != MMU_NO_CONTEXT)
285
		_tlbiel_pid(pid, RIC_FLUSH_ALL);
286 287
	preempt_enable();
}
288
EXPORT_SYMBOL(radix__local_flush_all_mm);
289
#endif /* CONFIG_SMP */
290

291
void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
292
				       int psize)
293
{
294
	unsigned long pid;
295 296

	preempt_disable();
297
	pid = mm->context.id;
298
	if (pid != MMU_NO_CONTEXT)
299
		_tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
300 301 302 303 304
	preempt_enable();
}

void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
{
305 306
#ifdef CONFIG_HUGETLB_PAGE
	/* need the return fix for nohash.c */
307 308
	if (is_vm_hugetlb_page(vma))
		return radix__local_flush_hugetlb_page(vma, vmaddr);
309
#endif
310
	radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
311 312 313 314 315 316
}
EXPORT_SYMBOL(radix__local_flush_tlb_page);

#ifdef CONFIG_SMP
void radix__flush_tlb_mm(struct mm_struct *mm)
{
317
	unsigned long pid;
318 319 320

	pid = mm->context.id;
	if (unlikely(pid == MMU_NO_CONTEXT))
321
		return;
322

323
	preempt_disable();
324
	if (!mm_is_thread_local(mm))
325
		_tlbie_pid(pid, RIC_FLUSH_TLB);
326
	else
327
		_tlbiel_pid(pid, RIC_FLUSH_TLB);
328 329 330 331
	preempt_enable();
}
EXPORT_SYMBOL(radix__flush_tlb_mm);

332
void radix__flush_all_mm(struct mm_struct *mm)
333 334 335 336 337
{
	unsigned long pid;

	pid = mm->context.id;
	if (unlikely(pid == MMU_NO_CONTEXT))
338
		return;
339

340
	preempt_disable();
341
	if (!mm_is_thread_local(mm))
342
		_tlbie_pid(pid, RIC_FLUSH_ALL);
343
	else
344
		_tlbiel_pid(pid, RIC_FLUSH_ALL);
345 346
	preempt_enable();
}
347
EXPORT_SYMBOL(radix__flush_all_mm);
348 349 350 351 352

void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
{
	tlb->need_flush_all = 1;
}
353 354
EXPORT_SYMBOL(radix__flush_tlb_pwc);

355
void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
356
				 int psize)
357
{
358
	unsigned long pid;
359

360
	pid = mm->context.id;
361
	if (unlikely(pid == MMU_NO_CONTEXT))
362 363 364
		return;

	preempt_disable();
365
	if (!mm_is_thread_local(mm))
366
		_tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
367
	else
368
		_tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
369 370 371 372 373
	preempt_enable();
}

void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
{
374
#ifdef CONFIG_HUGETLB_PAGE
375 376
	if (is_vm_hugetlb_page(vma))
		return radix__flush_hugetlb_page(vma, vmaddr);
377
#endif
378
	radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
379 380 381
}
EXPORT_SYMBOL(radix__flush_tlb_page);

382 383
#else /* CONFIG_SMP */
#define radix__flush_all_mm radix__local_flush_all_mm
384 385 386 387
#endif /* CONFIG_SMP */

void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
{
388
	_tlbie_pid(0, RIC_FLUSH_ALL);
389 390 391
}
EXPORT_SYMBOL(radix__flush_tlb_kernel_range);

392 393
#define TLB_FLUSH_ALL -1UL

394
/*
395 396 397 398 399 400 401
 * Number of pages above which we invalidate the entire PID rather than
 * flush individual pages, for local and global flushes respectively.
 *
 * tlbie goes out to the interconnect and individual ops are more costly.
 * It also does not iterate over sets like the local tlbiel variant when
 * invalidating a full PID, so it has a far lower threshold to change from
 * individual page flushes to full-pid flushes.
402
 */
403
static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
404
static unsigned long tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
405

406 407 408 409 410
void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
		     unsigned long end)

{
	struct mm_struct *mm = vma->vm_mm;
411 412 413 414 415
	unsigned long pid;
	unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
	unsigned long page_size = 1UL << page_shift;
	unsigned long nr_pages = (end - start) >> page_shift;
	bool local, full;
416

417 418 419 420 421 422 423 424 425 426
#ifdef CONFIG_HUGETLB_PAGE
	if (is_vm_hugetlb_page(vma))
		return radix__flush_hugetlb_tlb_range(vma, start, end);
#endif

	pid = mm->context.id;
	if (unlikely(pid == MMU_NO_CONTEXT))
		return;

	preempt_disable();
427 428 429 430 431 432 433 434 435
	if (mm_is_thread_local(mm)) {
		local = true;
		full = (end == TLB_FLUSH_ALL ||
				nr_pages > tlb_local_single_page_flush_ceiling);
	} else {
		local = false;
		full = (end == TLB_FLUSH_ALL ||
				nr_pages > tlb_single_page_flush_ceiling);
	}
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

	if (full) {
		if (local)
			_tlbiel_pid(pid, RIC_FLUSH_TLB);
		else
			_tlbie_pid(pid, RIC_FLUSH_TLB);
	} else {
		bool hflush = false;
		unsigned long hstart, hend;

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
		hstart = (start + HPAGE_PMD_SIZE - 1) >> HPAGE_PMD_SHIFT;
		hend = end >> HPAGE_PMD_SHIFT;
		if (hstart < hend) {
			hstart <<= HPAGE_PMD_SHIFT;
			hend <<= HPAGE_PMD_SHIFT;
			hflush = true;
		}
#endif

		asm volatile("ptesync": : :"memory");
		if (local) {
			__tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
			if (hflush)
				__tlbiel_va_range(hstart, hend, pid,
						HPAGE_PMD_SIZE, MMU_PAGE_2M);
			asm volatile("ptesync": : :"memory");
		} else {
			__tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
			if (hflush)
				__tlbie_va_range(hstart, hend, pid,
						HPAGE_PMD_SIZE, MMU_PAGE_2M);
			asm volatile("eieio; tlbsync; ptesync": : :"memory");
		}
	}
	preempt_enable();
472 473 474
}
EXPORT_SYMBOL(radix__flush_tlb_range);

475 476 477 478 479 480 481 482 483 484 485 486 487 488
static int radix_get_mmu_psize(int page_size)
{
	int psize;

	if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
		psize = mmu_virtual_psize;
	else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
		psize = MMU_PAGE_2M;
	else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
		psize = MMU_PAGE_1G;
	else
		return -1;
	return psize;
}
489

490 491 492
static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
				  unsigned long end, int psize);

493 494
void radix__tlb_flush(struct mmu_gather *tlb)
{
495
	int psize = 0;
496
	struct mm_struct *mm = tlb->mm;
497 498 499 500
	int page_size = tlb->page_size;

	/*
	 * if page size is not something we understand, do a full mm flush
501 502 503 504
	 *
	 * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
	 * that flushes the process table entry cache upon process teardown.
	 * See the comment for radix in arch_exit_mmap().
505
	 */
506
	if (tlb->fullmm) {
507
		radix__flush_all_mm(mm);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	} else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
		if (!tlb->need_flush_all)
			radix__flush_tlb_mm(mm);
		else
			radix__flush_all_mm(mm);
	} else {
		unsigned long start = tlb->start;
		unsigned long end = tlb->end;

		if (!tlb->need_flush_all)
			radix__flush_tlb_range_psize(mm, start, end, psize);
		else
			radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
	}
	tlb->need_flush_all = 0;
523 524
}

525 526 527
static inline void __radix__flush_tlb_range_psize(struct mm_struct *mm,
				unsigned long start, unsigned long end,
				int psize, bool also_pwc)
528 529
{
	unsigned long pid;
530 531 532 533
	unsigned int page_shift = mmu_psize_defs[psize].shift;
	unsigned long page_size = 1UL << page_shift;
	unsigned long nr_pages = (end - start) >> page_shift;
	bool local, full;
534

535
	pid = mm->context.id;
536
	if (unlikely(pid == MMU_NO_CONTEXT))
537
		return;
538

539
	preempt_disable();
540 541 542 543 544 545 546 547 548
	if (mm_is_thread_local(mm)) {
		local = true;
		full = (end == TLB_FLUSH_ALL ||
				nr_pages > tlb_local_single_page_flush_ceiling);
	} else {
		local = false;
		full = (end == TLB_FLUSH_ALL ||
				nr_pages > tlb_single_page_flush_ceiling);
	}
549 550

	if (full) {
551
		if (local)
552
			_tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
553
		else
554
			_tlbie_pid(pid, also_pwc ? RIC_FLUSH_ALL: RIC_FLUSH_TLB);
555
	} else {
556
		if (local)
557
			_tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
558
		else
559
			_tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
560 561
	}
	preempt_enable();
562
}
563

564 565 566 567 568 569 570 571 572 573 574 575
void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
				  unsigned long end, int psize)
{
	return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
}

static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
				  unsigned long end, int psize)
{
	__radix__flush_tlb_range_psize(mm, start, end, psize, true);
}

576 577 578 579 580
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
{
	unsigned long pid, end;

581
	pid = mm->context.id;
582
	if (unlikely(pid == MMU_NO_CONTEXT))
583
		return;
584 585 586 587 588 589 590

	/* 4k page size, just blow the world */
	if (PAGE_SIZE == 0x1000) {
		radix__flush_all_mm(mm);
		return;
	}

591 592 593
	end = addr + HPAGE_PMD_SIZE;

	/* Otherwise first do the PWC, then iterate the pages. */
594
	preempt_disable();
595

596
	if (mm_is_thread_local(mm)) {
597
		_tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
598
	} else {
599
		_tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
600
	}
601

602 603 604 605
	preempt_enable();
}
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
void radix__flush_tlb_lpid_va(unsigned long lpid, unsigned long gpa,
			      unsigned long page_size)
{
	unsigned long rb,rs,prs,r;
	unsigned long ap;
	unsigned long ric = RIC_FLUSH_TLB;

	ap = mmu_get_ap(radix_get_mmu_psize(page_size));
	rb = gpa & ~(PPC_BITMASK(52, 63));
	rb |= ap << PPC_BITLSHIFT(58);
	rs = lpid & ((1UL << 32) - 1);
	prs = 0; /* process scoped */
	r = 1;   /* raidx format */

	asm volatile("ptesync": : :"memory");
	asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
		     : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
	asm volatile("eieio; tlbsync; ptesync": : :"memory");
624
	trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
}
EXPORT_SYMBOL(radix__flush_tlb_lpid_va);

void radix__flush_tlb_lpid(unsigned long lpid)
{
	unsigned long rb,rs,prs,r;
	unsigned long ric = RIC_FLUSH_ALL;

	rb = 0x2 << PPC_BITLSHIFT(53); /* IS = 2 */
	rs = lpid & ((1UL << 32) - 1);
	prs = 0; /* partition scoped */
	r = 1;   /* raidx format */

	asm volatile("ptesync": : :"memory");
	asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
		     : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
	asm volatile("eieio; tlbsync; ptesync": : :"memory");
642
	trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
643 644
}
EXPORT_SYMBOL(radix__flush_tlb_lpid);
645 646 647 648 649 650 651

void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
				unsigned long start, unsigned long end)
{
	radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
}
EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

void radix__flush_tlb_all(void)
{
	unsigned long rb,prs,r,rs;
	unsigned long ric = RIC_FLUSH_ALL;

	rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
	prs = 0; /* partition scoped */
	r = 1;   /* raidx format */
	rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */

	asm volatile("ptesync": : :"memory");
	/*
	 * now flush guest entries by passing PRS = 1 and LPID != 0
	 */
	asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
		     : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
	/*
	 * now flush host entires by passing PRS = 0 and LPID == 0
	 */
	asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
		     : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
	asm volatile("eieio; tlbsync; ptesync": : :"memory");
}
676 677 678 679 680 681 682 683 684 685 686 687 688

void radix__flush_tlb_pte_p9_dd1(unsigned long old_pte, struct mm_struct *mm,
				 unsigned long address)
{
	/*
	 * We track page size in pte only for DD1, So we can
	 * call this only on DD1.
	 */
	if (!cpu_has_feature(CPU_FTR_POWER9_DD1)) {
		VM_WARN_ON(1);
		return;
	}

689
	if (old_pte & R_PAGE_LARGE)
690 691 692 693
		radix__flush_tlb_page_psize(mm, address, MMU_PAGE_2M);
	else
		radix__flush_tlb_page_psize(mm, address, mmu_virtual_psize);
}
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
extern void radix_kvm_prefetch_workaround(struct mm_struct *mm)
{
	unsigned int pid = mm->context.id;

	if (unlikely(pid == MMU_NO_CONTEXT))
		return;

	/*
	 * If this context hasn't run on that CPU before and KVM is
	 * around, there's a slim chance that the guest on another
	 * CPU just brought in obsolete translation into the TLB of
	 * this CPU due to a bad prefetch using the guest PID on
	 * the way into the hypervisor.
	 *
	 * We work around this here. If KVM is possible, we check if
	 * any sibling thread is in KVM. If it is, the window may exist
	 * and thus we flush that PID from the core.
	 *
	 * A potential future improvement would be to mark which PIDs
	 * have never been used on the system and avoid it if the PID
	 * is new and the process has no other cpumask bit set.
	 */
	if (cpu_has_feature(CPU_FTR_HVMODE) && radix_enabled()) {
		int cpu = smp_processor_id();
		int sib = cpu_first_thread_sibling(cpu);
		bool flush = false;

		for (; sib <= cpu_last_thread_sibling(cpu) && !flush; sib++) {
			if (sib == cpu)
				continue;
			if (paca[sib].kvm_hstate.kvm_vcpu)
				flush = true;
		}
		if (flush)
			_tlbiel_pid(pid, RIC_FLUSH_ALL);
	}
}
EXPORT_SYMBOL_GPL(radix_kvm_prefetch_workaround);
#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */