2 * TLB flush routines for radix kernels.
4 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
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.
13 #include <linux/hugetlb.h>
14 #include <linux/memblock.h>
15 #include <asm/ppc-opcode.h>
18 #include <asm/tlbflush.h>
20 static DEFINE_RAW_SPINLOCK(native_tlbie_lock
);
22 #define RIC_FLUSH_TLB 0
23 #define RIC_FLUSH_PWC 1
24 #define RIC_FLUSH_ALL 2
26 static inline void __tlbiel_pid(unsigned long pid
, int set
,
29 unsigned long rb
,rs
,prs
,r
;
31 rb
= PPC_BIT(53); /* IS = 1 */
32 rb
|= set
<< PPC_BITLSHIFT(51);
33 rs
= ((unsigned long)pid
) << PPC_BITLSHIFT(31);
34 prs
= 1; /* process scoped */
35 r
= 1; /* raidx format */
37 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
38 : : "r"(rb
), "i"(r
), "i"(prs
), "i"(ric
), "r"(rs
) : "memory");
42 * We use 128 set in radix mode and 256 set in hpt mode.
44 static inline void _tlbiel_pid(unsigned long pid
, unsigned long ric
)
48 asm volatile("ptesync": : :"memory");
51 * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
52 * also flush the entire Page Walk Cache.
54 __tlbiel_pid(pid
, 0, ric
);
56 /* For PWC, only one flush is needed */
57 if (ric
== RIC_FLUSH_PWC
) {
58 asm volatile("ptesync": : :"memory");
62 /* For the remaining sets, just flush the TLB */
63 for (set
= 1; set
< POWER9_TLB_SETS_RADIX
; set
++)
64 __tlbiel_pid(pid
, set
, RIC_FLUSH_TLB
);
66 asm volatile("ptesync": : :"memory");
67 asm volatile(PPC_INVALIDATE_ERAT
"; isync" : : :"memory");
70 static inline void _tlbie_pid(unsigned long pid
, unsigned long ric
)
72 unsigned long rb
,rs
,prs
,r
;
74 rb
= PPC_BIT(53); /* IS = 1 */
75 rs
= pid
<< PPC_BITLSHIFT(31);
76 prs
= 1; /* process scoped */
77 r
= 1; /* raidx format */
79 asm volatile("ptesync": : :"memory");
80 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
81 : : "r"(rb
), "i"(r
), "i"(prs
), "i"(ric
), "r"(rs
) : "memory");
82 asm volatile("eieio; tlbsync; ptesync": : :"memory");
85 static inline void _tlbiel_va(unsigned long va
, unsigned long pid
,
86 unsigned long ap
, unsigned long ric
)
88 unsigned long rb
,rs
,prs
,r
;
90 rb
= va
& ~(PPC_BITMASK(52, 63));
91 rb
|= ap
<< PPC_BITLSHIFT(58);
92 rs
= pid
<< PPC_BITLSHIFT(31);
93 prs
= 1; /* process scoped */
94 r
= 1; /* raidx format */
96 asm volatile("ptesync": : :"memory");
97 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
98 : : "r"(rb
), "i"(r
), "i"(prs
), "i"(ric
), "r"(rs
) : "memory");
99 asm volatile("ptesync": : :"memory");
102 static inline void _tlbie_va(unsigned long va
, unsigned long pid
,
103 unsigned long ap
, unsigned long ric
)
105 unsigned long rb
,rs
,prs
,r
;
107 rb
= va
& ~(PPC_BITMASK(52, 63));
108 rb
|= ap
<< PPC_BITLSHIFT(58);
109 rs
= pid
<< PPC_BITLSHIFT(31);
110 prs
= 1; /* process scoped */
111 r
= 1; /* raidx format */
113 asm volatile("ptesync": : :"memory");
114 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
115 : : "r"(rb
), "i"(r
), "i"(prs
), "i"(ric
), "r"(rs
) : "memory");
116 asm volatile("eieio; tlbsync; ptesync": : :"memory");
120 * Base TLB flushing operations:
122 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
123 * - flush_tlb_page(vma, vmaddr) flushes one page
124 * - flush_tlb_range(vma, start, end) flushes a range of pages
125 * - flush_tlb_kernel_range(start, end) flushes kernel pages
127 * - local_* variants of page and mm only apply to the current
130 void radix__local_flush_tlb_mm(struct mm_struct
*mm
)
135 pid
= mm
->context
.id
;
136 if (pid
!= MMU_NO_CONTEXT
)
137 _tlbiel_pid(pid
, RIC_FLUSH_ALL
);
140 EXPORT_SYMBOL(radix__local_flush_tlb_mm
);
142 void radix__local_flush_tlb_pwc(struct mmu_gather
*tlb
, unsigned long addr
)
145 struct mm_struct
*mm
= tlb
->mm
;
147 * If we are doing a full mm flush, we will do a tlb flush
148 * with RIC_FLUSH_ALL later.
155 pid
= mm
->context
.id
;
156 if (pid
!= MMU_NO_CONTEXT
)
157 _tlbiel_pid(pid
, RIC_FLUSH_PWC
);
161 EXPORT_SYMBOL(radix__local_flush_tlb_pwc
);
163 void radix__local_flush_tlb_page_psize(struct mm_struct
*mm
, unsigned long vmaddr
,
167 unsigned long ap
= mmu_get_ap(psize
);
170 pid
= mm
? mm
->context
.id
: 0;
171 if (pid
!= MMU_NO_CONTEXT
)
172 _tlbiel_va(vmaddr
, pid
, ap
, RIC_FLUSH_TLB
);
176 void radix__local_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long vmaddr
)
178 #ifdef CONFIG_HUGETLB_PAGE
179 /* need the return fix for nohash.c */
180 if (vma
&& is_vm_hugetlb_page(vma
))
181 return __local_flush_hugetlb_page(vma
, vmaddr
);
183 radix__local_flush_tlb_page_psize(vma
? vma
->vm_mm
: NULL
, vmaddr
,
186 EXPORT_SYMBOL(radix__local_flush_tlb_page
);
189 void radix__flush_tlb_mm(struct mm_struct
*mm
)
194 pid
= mm
->context
.id
;
195 if (unlikely(pid
== MMU_NO_CONTEXT
))
198 if (!mm_is_thread_local(mm
)) {
199 int lock_tlbie
= !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE
);
202 raw_spin_lock(&native_tlbie_lock
);
203 _tlbie_pid(pid
, RIC_FLUSH_ALL
);
205 raw_spin_unlock(&native_tlbie_lock
);
207 _tlbiel_pid(pid
, RIC_FLUSH_ALL
);
211 EXPORT_SYMBOL(radix__flush_tlb_mm
);
213 void radix__flush_tlb_pwc(struct mmu_gather
*tlb
, unsigned long addr
)
216 struct mm_struct
*mm
= tlb
->mm
;
219 * If we are doing a full mm flush, we will do a tlb flush
220 * with RIC_FLUSH_ALL later.
226 pid
= mm
->context
.id
;
227 if (unlikely(pid
== MMU_NO_CONTEXT
))
230 if (!mm_is_thread_local(mm
)) {
231 int lock_tlbie
= !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE
);
234 raw_spin_lock(&native_tlbie_lock
);
235 _tlbie_pid(pid
, RIC_FLUSH_PWC
);
237 raw_spin_unlock(&native_tlbie_lock
);
239 _tlbiel_pid(pid
, RIC_FLUSH_PWC
);
243 EXPORT_SYMBOL(radix__flush_tlb_pwc
);
245 void radix__flush_tlb_page_psize(struct mm_struct
*mm
, unsigned long vmaddr
,
249 unsigned long ap
= mmu_get_ap(psize
);
252 pid
= mm
? mm
->context
.id
: 0;
253 if (unlikely(pid
== MMU_NO_CONTEXT
))
255 if (!mm_is_thread_local(mm
)) {
256 int lock_tlbie
= !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE
);
259 raw_spin_lock(&native_tlbie_lock
);
260 _tlbie_va(vmaddr
, pid
, ap
, RIC_FLUSH_TLB
);
262 raw_spin_unlock(&native_tlbie_lock
);
264 _tlbiel_va(vmaddr
, pid
, ap
, RIC_FLUSH_TLB
);
269 void radix__flush_tlb_page(struct vm_area_struct
*vma
, unsigned long vmaddr
)
271 #ifdef CONFIG_HUGETLB_PAGE
272 if (vma
&& is_vm_hugetlb_page(vma
))
273 return flush_hugetlb_page(vma
, vmaddr
);
275 radix__flush_tlb_page_psize(vma
? vma
->vm_mm
: NULL
, vmaddr
,
278 EXPORT_SYMBOL(radix__flush_tlb_page
);
280 #endif /* CONFIG_SMP */
282 void radix__flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
284 int lock_tlbie
= !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE
);
287 raw_spin_lock(&native_tlbie_lock
);
288 _tlbie_pid(0, RIC_FLUSH_ALL
);
290 raw_spin_unlock(&native_tlbie_lock
);
292 EXPORT_SYMBOL(radix__flush_tlb_kernel_range
);
295 * Currently, for range flushing, we just do a full mm flush. Because
296 * we use this in code path where we don' track the page size.
298 void radix__flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
302 struct mm_struct
*mm
= vma
->vm_mm
;
303 radix__flush_tlb_mm(mm
);
305 EXPORT_SYMBOL(radix__flush_tlb_range
);
307 static int radix_get_mmu_psize(int page_size
)
311 if (page_size
== (1UL << mmu_psize_defs
[mmu_virtual_psize
].shift
))
312 psize
= mmu_virtual_psize
;
313 else if (page_size
== (1UL << mmu_psize_defs
[MMU_PAGE_2M
].shift
))
315 else if (page_size
== (1UL << mmu_psize_defs
[MMU_PAGE_1G
].shift
))
322 void radix__tlb_flush(struct mmu_gather
*tlb
)
325 struct mm_struct
*mm
= tlb
->mm
;
326 int page_size
= tlb
->page_size
;
328 psize
= radix_get_mmu_psize(page_size
);
330 * if page size is not something we understand, do a full mm flush
332 if (psize
!= -1 && !tlb
->fullmm
&& !tlb
->need_flush_all
)
333 radix__flush_tlb_range_psize(mm
, tlb
->start
, tlb
->end
, psize
);
335 radix__flush_tlb_mm(mm
);
338 #define TLB_FLUSH_ALL -1UL
340 * Number of pages above which we will do a bcast tlbie. Just a
341 * number at this point copied from x86
343 static unsigned long tlb_single_page_flush_ceiling __read_mostly
= 33;
345 void radix__flush_tlb_range_psize(struct mm_struct
*mm
, unsigned long start
,
346 unsigned long end
, int psize
)
350 int local
= mm_is_thread_local(mm
);
351 unsigned long ap
= mmu_get_ap(psize
);
352 int lock_tlbie
= !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE
);
353 unsigned long page_size
= 1UL << mmu_psize_defs
[psize
].shift
;
357 pid
= mm
? mm
->context
.id
: 0;
358 if (unlikely(pid
== MMU_NO_CONTEXT
))
361 if (end
== TLB_FLUSH_ALL
||
362 (end
- start
) > tlb_single_page_flush_ceiling
* page_size
) {
364 _tlbiel_pid(pid
, RIC_FLUSH_TLB
);
366 _tlbie_pid(pid
, RIC_FLUSH_TLB
);
369 for (addr
= start
; addr
< end
; addr
+= page_size
) {
372 _tlbiel_va(addr
, pid
, ap
, RIC_FLUSH_TLB
);
375 raw_spin_lock(&native_tlbie_lock
);
376 _tlbie_va(addr
, pid
, ap
, RIC_FLUSH_TLB
);
378 raw_spin_unlock(&native_tlbie_lock
);
385 void radix__flush_tlb_lpid_va(unsigned long lpid
, unsigned long gpa
,
386 unsigned long page_size
)
388 unsigned long rb
,rs
,prs
,r
;
390 unsigned long ric
= RIC_FLUSH_TLB
;
392 ap
= mmu_get_ap(radix_get_mmu_psize(page_size
));
393 rb
= gpa
& ~(PPC_BITMASK(52, 63));
394 rb
|= ap
<< PPC_BITLSHIFT(58);
395 rs
= lpid
& ((1UL << 32) - 1);
396 prs
= 0; /* process scoped */
397 r
= 1; /* raidx format */
399 asm volatile("ptesync": : :"memory");
400 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
401 : : "r"(rb
), "i"(r
), "i"(prs
), "i"(ric
), "r"(rs
) : "memory");
402 asm volatile("eieio; tlbsync; ptesync": : :"memory");
404 EXPORT_SYMBOL(radix__flush_tlb_lpid_va
);
406 void radix__flush_tlb_lpid(unsigned long lpid
)
408 unsigned long rb
,rs
,prs
,r
;
409 unsigned long ric
= RIC_FLUSH_ALL
;
411 rb
= 0x2 << PPC_BITLSHIFT(53); /* IS = 2 */
412 rs
= lpid
& ((1UL << 32) - 1);
413 prs
= 0; /* partition scoped */
414 r
= 1; /* raidx format */
416 asm volatile("ptesync": : :"memory");
417 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
418 : : "r"(rb
), "i"(r
), "i"(prs
), "i"(ric
), "r"(rs
) : "memory");
419 asm volatile("eieio; tlbsync; ptesync": : :"memory");
421 EXPORT_SYMBOL(radix__flush_tlb_lpid
);
423 void radix__flush_pmd_tlb_range(struct vm_area_struct
*vma
,
424 unsigned long start
, unsigned long end
)
426 radix__flush_tlb_range_psize(vma
->vm_mm
, start
, end
, MMU_PAGE_2M
);
428 EXPORT_SYMBOL(radix__flush_pmd_tlb_range
);
430 void radix__flush_tlb_all(void)
432 unsigned long rb
,prs
,r
,rs
;
433 unsigned long ric
= RIC_FLUSH_ALL
;
435 rb
= 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
436 prs
= 0; /* partition scoped */
437 r
= 1; /* raidx format */
438 rs
= 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
440 asm volatile("ptesync": : :"memory");
442 * now flush guest entries by passing PRS = 1 and LPID != 0
444 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
445 : : "r"(rb
), "i"(r
), "i"(1), "i"(ric
), "r"(rs
) : "memory");
447 * now flush host entires by passing PRS = 0 and LPID == 0
449 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
450 : : "r"(rb
), "i"(r
), "i"(prs
), "i"(ric
), "r"(0) : "memory");
451 asm volatile("eieio; tlbsync; ptesync": : :"memory");
454 void radix__flush_tlb_pte_p9_dd1(unsigned long old_pte
, struct mm_struct
*mm
,
455 unsigned long address
)
458 * We track page size in pte only for DD1, So we can
459 * call this only on DD1.
461 if (!cpu_has_feature(CPU_FTR_POWER9_DD1
)) {
466 if (old_pte
& _PAGE_LARGE
)
467 radix__flush_tlb_page_psize(mm
, address
, MMU_PAGE_2M
);
469 radix__flush_tlb_page_psize(mm
, address
, mmu_virtual_psize
);