xref: /linux/arch/arm64/include/asm/tlbflush.h (revision c43267e6794a36013fd495a4d81bf7f748fe4615)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Based on arch/arm/include/asm/tlbflush.h
4  *
5  * Copyright (C) 1999-2003 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  */
8 #ifndef __ASM_TLBFLUSH_H
9 #define __ASM_TLBFLUSH_H
10 
11 #ifndef __ASSEMBLER__
12 
13 #include <linux/bitfield.h>
14 #include <linux/mm_types.h>
15 #include <linux/sched.h>
16 #include <linux/mmu_notifier.h>
17 #include <asm/cputype.h>
18 #include <asm/mmu.h>
19 
20 /*
21  * Raw TLBI operations.
22  *
23  * Where necessary, use the __tlbi() macro to avoid asm()
24  * boilerplate. Drivers and most kernel code should use the TLB
25  * management routines in preference to the macro below.
26  *
27  * The macro can be used as __tlbi(op) or __tlbi(op, arg), depending
28  * on whether a particular TLBI operation takes an argument or
29  * not. The macros handles invoking the asm with or without the
30  * register argument as appropriate.
31  */
32 #define __TLBI_0(op, arg) asm (ARM64_ASM_PREAMBLE			       \
33 			       "tlbi " #op "\n"				       \
34 			    : : )
35 
36 #define __TLBI_1(op, arg) asm (ARM64_ASM_PREAMBLE			       \
37 			       "tlbi " #op ", %x0\n"			       \
38 			    : : "rZ" (arg))
39 
40 #define __TLBI_N(op, arg, n, ...) __TLBI_##n(op, arg)
41 
42 #define __tlbi(op, ...)		__TLBI_N(op, ##__VA_ARGS__, 1, 0)
43 
44 #define __tlbi_user(op, arg) do {						\
45 	if (arm64_kernel_unmapped_at_el0())					\
46 		__tlbi(op, (arg) | USER_ASID_FLAG);				\
47 } while (0)
48 
49 /* This macro creates a properly formatted VA operand for the TLBI */
50 #define __TLBI_VADDR(addr, asid)				\
51 	({							\
52 		unsigned long __ta = (addr) >> 12;		\
53 		__ta &= GENMASK_ULL(43, 0);			\
54 		__ta |= (unsigned long)(asid) << 48;		\
55 		__ta;						\
56 	})
57 
58 /*
59  * Get translation granule of the system, which is decided by
60  * PAGE_SIZE.  Used by TTL.
61  *  - 4KB	: 1
62  *  - 16KB	: 2
63  *  - 64KB	: 3
64  */
65 #define TLBI_TTL_TG_4K		1
66 #define TLBI_TTL_TG_16K		2
67 #define TLBI_TTL_TG_64K		3
68 
get_trans_granule(void)69 static inline unsigned long get_trans_granule(void)
70 {
71 	switch (PAGE_SIZE) {
72 	case SZ_4K:
73 		return TLBI_TTL_TG_4K;
74 	case SZ_16K:
75 		return TLBI_TTL_TG_16K;
76 	case SZ_64K:
77 		return TLBI_TTL_TG_64K;
78 	default:
79 		return 0;
80 	}
81 }
82 
83 /*
84  * Level-based TLBI operations.
85  *
86  * When ARMv8.4-TTL exists, TLBI operations take an additional hint for
87  * the level at which the invalidation must take place. If the level is
88  * wrong, no invalidation may take place. In the case where the level
89  * cannot be easily determined, the value TLBI_TTL_UNKNOWN will perform
90  * a non-hinted invalidation. Any provided level outside the hint range
91  * will also cause fall-back to non-hinted invalidation.
92  *
93  * For Stage-2 invalidation, use the level values provided to that effect
94  * in asm/stage2_pgtable.h.
95  */
96 #define TLBI_TTL_MASK		GENMASK_ULL(47, 44)
97 
98 #define TLBI_TTL_UNKNOWN	INT_MAX
99 
100 typedef void (*tlbi_op)(u64 arg);
101 
vae1is(u64 arg)102 static __always_inline void vae1is(u64 arg)
103 {
104 	__tlbi(vae1is, arg);
105 	__tlbi_user(vae1is, arg);
106 }
107 
vae2is(u64 arg)108 static __always_inline void vae2is(u64 arg)
109 {
110 	__tlbi(vae2is, arg);
111 }
112 
vale1(u64 arg)113 static __always_inline void vale1(u64 arg)
114 {
115 	__tlbi(vale1, arg);
116 	__tlbi_user(vale1, arg);
117 }
118 
vale1is(u64 arg)119 static __always_inline void vale1is(u64 arg)
120 {
121 	__tlbi(vale1is, arg);
122 	__tlbi_user(vale1is, arg);
123 }
124 
vale2is(u64 arg)125 static __always_inline void vale2is(u64 arg)
126 {
127 	__tlbi(vale2is, arg);
128 }
129 
vaale1is(u64 arg)130 static __always_inline void vaale1is(u64 arg)
131 {
132 	__tlbi(vaale1is, arg);
133 }
134 
ipas2e1(u64 arg)135 static __always_inline void ipas2e1(u64 arg)
136 {
137 	__tlbi(ipas2e1, arg);
138 }
139 
ipas2e1is(u64 arg)140 static __always_inline void ipas2e1is(u64 arg)
141 {
142 	__tlbi(ipas2e1is, arg);
143 }
144 
__tlbi_level_asid(tlbi_op op,u64 addr,u32 level,u16 asid)145 static __always_inline void __tlbi_level_asid(tlbi_op op, u64 addr, u32 level,
146 					      u16 asid)
147 {
148 	u64 arg = __TLBI_VADDR(addr, asid);
149 
150 	if (alternative_has_cap_unlikely(ARM64_HAS_ARMv8_4_TTL) && level <= 3) {
151 		u64 ttl = level | (get_trans_granule() << 2);
152 
153 		FIELD_MODIFY(TLBI_TTL_MASK, &arg, ttl);
154 	}
155 
156 	op(arg);
157 }
158 
__tlbi_level(tlbi_op op,u64 addr,u32 level)159 static inline void __tlbi_level(tlbi_op op, u64 addr, u32 level)
160 {
161 	__tlbi_level_asid(op, addr, level, 0);
162 }
163 
164 /*
165  * This macro creates a properly formatted VA operand for the TLB RANGE. The
166  * value bit assignments are:
167  *
168  * +----------+------+-------+-------+-------+----------------------+
169  * |   ASID   |  TG  | SCALE |  NUM  |  TTL  |        BADDR         |
170  * +-----------------+-------+-------+-------+----------------------+
171  * |63      48|47  46|45   44|43   39|38   37|36                   0|
172  *
173  * The address range is determined by below formula: [BADDR, BADDR + (NUM + 1) *
174  * 2^(5*SCALE + 1) * PAGESIZE)
175  *
176  * Note that the first argument, baddr, is pre-shifted; If LPA2 is in use, BADDR
177  * holds addr[52:16]. Else BADDR holds page number. See for example ARM DDI
178  * 0487J.a section C5.5.60 "TLBI VAE1IS, TLBI VAE1ISNXS, TLB Invalidate by VA,
179  * EL1, Inner Shareable".
180  *
181  */
182 #define TLBIR_ASID_MASK		GENMASK_ULL(63, 48)
183 #define TLBIR_TG_MASK		GENMASK_ULL(47, 46)
184 #define TLBIR_SCALE_MASK	GENMASK_ULL(45, 44)
185 #define TLBIR_NUM_MASK		GENMASK_ULL(43, 39)
186 #define TLBIR_TTL_MASK		GENMASK_ULL(38, 37)
187 #define TLBIR_BADDR_MASK	GENMASK_ULL(36,  0)
188 
189 /* These macros are used by the TLBI RANGE feature. */
190 #define __TLBI_RANGE_PAGES(num, scale)	\
191 	((unsigned long)((num) + 1) << (5 * (scale) + 1))
192 #define MAX_TLBI_RANGE_PAGES		__TLBI_RANGE_PAGES(31, 3)
193 
194 /*
195  * Generate 'num' values from -1 to 31 with -1 rejected by the
196  * __flush_tlb_range() loop below. Its return value is only
197  * significant for a maximum of MAX_TLBI_RANGE_PAGES pages. If
198  * 'pages' is more than that, you must iterate over the overall
199  * range.
200  */
201 #define __TLBI_RANGE_NUM(pages, scale)					\
202 	(((pages) >> (5 * (scale) + 1)) - 1)
203 
204 #define __repeat_tlbi_sync(op, arg...)						\
205 do {										\
206 	if (!alternative_has_cap_unlikely(ARM64_WORKAROUND_REPEAT_TLBI))	\
207 		break;								\
208 	__tlbi(op, ##arg);							\
209 	dsb(ish);								\
210 } while (0)
211 
212 /*
213  * Complete broadcast TLB maintenance issued by the host which invalidates
214  * stage 1 information in the host's own translation regime.
215  */
__tlbi_sync_s1ish(void)216 static inline void __tlbi_sync_s1ish(void)
217 {
218 	dsb(ish);
219 	__repeat_tlbi_sync(vale1is, 0);
220 }
221 
222 /*
223  * Complete broadcast TLB maintenance issued by hyp code which invalidates
224  * stage 1 translation information in any translation regime.
225  */
__tlbi_sync_s1ish_hyp(void)226 static inline void __tlbi_sync_s1ish_hyp(void)
227 {
228 	dsb(ish);
229 	__repeat_tlbi_sync(vale2is, 0);
230 }
231 
232 /*
233  *	TLB Invalidation
234  *	================
235  *
236  * 	This header file implements the low-level TLB invalidation routines
237  *	(sometimes referred to as "flushing" in the kernel) for arm64.
238  *
239  *	Every invalidation operation uses the following template:
240  *
241  *	DSB ISHST	// Ensure prior page-table updates have completed
242  *	TLBI ...	// Invalidate the TLB
243  *	DSB ISH		// Ensure the TLB invalidation has completed
244  *      if (invalidated kernel mappings)
245  *		ISB	// Discard any instructions fetched from the old mapping
246  *
247  *
248  *	The following functions form part of the "core" TLB invalidation API,
249  *	as documented in Documentation/core-api/cachetlb.rst:
250  *
251  *	flush_tlb_all()
252  *		Invalidate the entire TLB (kernel + user) on all CPUs
253  *
254  *	flush_tlb_mm(mm)
255  *		Invalidate an entire user address space on all CPUs.
256  *		The 'mm' argument identifies the ASID to invalidate.
257  *
258  *	flush_tlb_range(vma, start, end)
259  *		Invalidate the virtual-address range '[start, end)' on all
260  *		CPUs for the user address space corresponding to 'vma->mm'.
261  *		Note that this operation also invalidates any walk-cache
262  *		entries associated with translations for the specified address
263  *		range.
264  *
265  *	flush_tlb_kernel_range(start, end)
266  *		Same as flush_tlb_range(..., start, end), but applies to
267  * 		kernel mappings rather than a particular user address space.
268  *		Whilst not explicitly documented, this function is used when
269  *		unmapping pages from vmalloc/io space.
270  *
271  *	flush_tlb_page(vma, addr)
272  *		Equivalent to __flush_tlb_page(..., flags=TLBF_NONE)
273  *
274  *
275  *	Next, we have some undocumented invalidation routines that you probably
276  *	don't want to call unless you know what you're doing:
277  *
278  *	local_flush_tlb_all()
279  *		Same as flush_tlb_all(), but only applies to the calling CPU.
280  *
281  *	__flush_tlb_kernel_pgtable(addr)
282  *		Invalidate a single kernel mapping for address 'addr' on all
283  *		CPUs, ensuring that any walk-cache entries associated with the
284  *		translation are also invalidated.
285  *
286  *	__flush_tlb_range(vma, start, end, stride, tlb_level, flags)
287  *		Invalidate the virtual-address range '[start, end)' on all
288  *		CPUs for the user address space corresponding to 'vma->mm'.
289  *		The invalidation operations are issued at a granularity
290  *		determined by 'stride'. tlb_level is the level at
291  *		which the invalidation must take place. If the level is wrong,
292  *		no invalidation may take place. In the case where the level
293  *		cannot be easily determined, the value TLBI_TTL_UNKNOWN will
294  *		perform a non-hinted invalidation. flags may be TLBF_NONE (0) or
295  *		any combination of TLBF_NOWALKCACHE (elide eviction of walk
296  *		cache entries), TLBF_NONOTIFY (don't call mmu notifiers),
297  *		TLBF_NOSYNC (don't issue trailing dsb) and TLBF_NOBROADCAST
298  *		(only perform the invalidation for the local cpu).
299  *
300  *	__flush_tlb_page(vma, addr, flags)
301  *		Invalidate a single user mapping for address 'addr' in the
302  *		address space corresponding to 'vma->mm'.  Note that this
303  *		operation only invalidates a single level 3 page-table entry
304  *		and therefore does not affect any walk-caches. flags may contain
305  *		any combination of TLBF_NONOTIFY (don't call mmu notifiers),
306  *		TLBF_NOSYNC (don't issue trailing dsb) and TLBF_NOBROADCAST
307  *		(only perform the invalidation for the local cpu).
308  *
309  *	Finally, take a look at asm/tlb.h to see how tlb_flush() is implemented
310  *	on top of these routines, since that is our interface to the mmu_gather
311  *	API as used by munmap() and friends.
312  */
local_flush_tlb_all(void)313 static inline void local_flush_tlb_all(void)
314 {
315 	dsb(nshst);
316 	__tlbi(vmalle1);
317 	dsb(nsh);
318 	isb();
319 }
320 
flush_tlb_all(void)321 static inline void flush_tlb_all(void)
322 {
323 	dsb(ishst);
324 	__tlbi(vmalle1is);
325 	__tlbi_sync_s1ish();
326 	isb();
327 }
328 
flush_tlb_mm(struct mm_struct * mm)329 static inline void flush_tlb_mm(struct mm_struct *mm)
330 {
331 	unsigned long asid;
332 
333 	dsb(ishst);
334 	asid = __TLBI_VADDR(0, ASID(mm));
335 	__tlbi(aside1is, asid);
336 	__tlbi_user(aside1is, asid);
337 	__tlbi_sync_s1ish();
338 	mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
339 }
340 
arch_tlbbatch_should_defer(struct mm_struct * mm)341 static inline bool arch_tlbbatch_should_defer(struct mm_struct *mm)
342 {
343 	return true;
344 }
345 
346 /*
347  * To support TLB batched flush for multiple pages unmapping, we only send
348  * the TLBI for each page in arch_tlbbatch_add_pending() and wait for the
349  * completion at the end in arch_tlbbatch_flush(). Since we've already issued
350  * TLBI for each page so only a DSB is needed to synchronise its effect on the
351  * other CPUs.
352  *
353  * This will save the time waiting on DSB comparing issuing a TLBI;DSB sequence
354  * for each page.
355  */
arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch * batch)356 static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
357 {
358 	__tlbi_sync_s1ish();
359 }
360 
361 /*
362  * This is meant to avoid soft lock-ups on large TLB flushing ranges and not
363  * necessarily a performance improvement.
364  */
365 #define MAX_DVM_OPS	PTRS_PER_PTE
366 
367 /*
368  * __flush_tlb_range_op - Perform TLBI operation upon a range
369  *
370  * @lop:	TLBI level operation to perform
371  * @rop:	TLBI range operation to perform
372  * @start:	The start address of the range
373  * @pages:	Range as the number of pages from 'start'
374  * @stride:	Flush granularity
375  * @asid:	The ASID of the task (0 for IPA instructions)
376  * @level:	Translation Table level hint, if known
377  * @lpa2:	If 'true', the lpa2 scheme is used as set out below
378  *
379  * When the CPU does not support TLB range operations, flush the TLB
380  * entries one by one at the granularity of 'stride'. If the TLB
381  * range ops are supported, then:
382  *
383  * 1. If FEAT_LPA2 is in use, the start address of a range operation must be
384  *    64KB aligned, so flush pages one by one until the alignment is reached
385  *    using the non-range operations. This step is skipped if LPA2 is not in
386  *    use.
387  *
388  * 2. The minimum range granularity is decided by 'scale', so multiple range
389  *    TLBI operations may be required. Start from scale = 3, flush the largest
390  *    possible number of pages ((num+1)*2^(5*scale+1)) that fit into the
391  *    requested range, then decrement scale and continue until one or zero pages
392  *    are left. We must start from highest scale to ensure 64KB start alignment
393  *    is maintained in the LPA2 case.
394  *
395  * 3. If there is 1 page remaining, flush it through non-range operations. Range
396  *    operations can only span an even number of pages. We save this for last to
397  *    ensure 64KB start alignment is maintained for the LPA2 case.
398  */
rvae1is(u64 arg)399 static __always_inline void rvae1is(u64 arg)
400 {
401 	__tlbi(rvae1is, arg);
402 	__tlbi_user(rvae1is, arg);
403 }
404 
rvale1(u64 arg)405 static __always_inline void rvale1(u64 arg)
406 {
407 	__tlbi(rvale1, arg);
408 	__tlbi_user(rvale1, arg);
409 }
410 
rvale1is(u64 arg)411 static __always_inline void rvale1is(u64 arg)
412 {
413 	__tlbi(rvale1is, arg);
414 	__tlbi_user(rvale1is, arg);
415 }
416 
rvaale1is(u64 arg)417 static __always_inline void rvaale1is(u64 arg)
418 {
419 	__tlbi(rvaale1is, arg);
420 }
421 
ripas2e1is(u64 arg)422 static __always_inline void ripas2e1is(u64 arg)
423 {
424 	__tlbi(ripas2e1is, arg);
425 }
426 
__tlbi_range(tlbi_op op,u64 addr,u16 asid,int scale,int num,u32 level,bool lpa2)427 static __always_inline void __tlbi_range(tlbi_op op, u64 addr,
428 					 u16 asid, int scale, int num,
429 					 u32 level, bool lpa2)
430 {
431 	u64 arg = 0;
432 
433 	arg |= FIELD_PREP(TLBIR_BADDR_MASK, addr >> (lpa2 ? 16 : PAGE_SHIFT));
434 	arg |= FIELD_PREP(TLBIR_TTL_MASK, level > 3 ? 0 : level);
435 	arg |= FIELD_PREP(TLBIR_NUM_MASK, num);
436 	arg |= FIELD_PREP(TLBIR_SCALE_MASK, scale);
437 	arg |= FIELD_PREP(TLBIR_TG_MASK, get_trans_granule());
438 	arg |= FIELD_PREP(TLBIR_ASID_MASK, asid);
439 
440 	op(arg);
441 }
442 
__flush_tlb_range_op(tlbi_op lop,tlbi_op rop,u64 start,size_t pages,u64 stride,u16 asid,u32 level,bool lpa2)443 static __always_inline void __flush_tlb_range_op(tlbi_op lop, tlbi_op rop,
444 						 u64 start, size_t pages,
445 						 u64 stride, u16 asid,
446 						 u32 level, bool lpa2)
447 {
448 	u64 addr = start, end = start + pages * PAGE_SIZE;
449 	int scale = 3;
450 
451 	while (addr != end) {
452 		int num;
453 
454 		pages = (end - addr) >> PAGE_SHIFT;
455 
456 		if (!system_supports_tlb_range() || pages == 1)
457 			goto invalidate_one;
458 
459 		if (lpa2 && !IS_ALIGNED(addr, SZ_64K))
460 			goto invalidate_one;
461 
462 		num = __TLBI_RANGE_NUM(pages, scale);
463 		if (num >= 0) {
464 			__tlbi_range(rop, addr, asid, scale, num, level, lpa2);
465 			addr += __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT;
466 		}
467 
468 		scale--;
469 		continue;
470 invalidate_one:
471 		__tlbi_level_asid(lop, addr, level, asid);
472 		addr += stride;
473 	}
474 }
475 
476 #define __flush_s1_tlb_range_op(op, start, pages, stride, asid, tlb_level) \
477 	__flush_tlb_range_op(op, r##op, start, pages, stride, asid, tlb_level, lpa2_is_enabled())
478 
479 #define __flush_s2_tlb_range_op(op, start, pages, stride, tlb_level) \
480 	__flush_tlb_range_op(op, r##op, start, pages, stride, 0, tlb_level, kvm_lpa2_is_enabled())
481 
__flush_tlb_range_limit_excess(unsigned long pages,unsigned long stride)482 static inline bool __flush_tlb_range_limit_excess(unsigned long pages,
483 						  unsigned long stride)
484 {
485 	/*
486 	 * Assume that the worst case number of DVM ops required to flush a
487 	 * given range on a system that supports tlb-range is 20 (4 scales, 1
488 	 * final page, 15 for alignment on LPA2 systems), which is much smaller
489 	 * than MAX_DVM_OPS.
490 	 */
491 	if (system_supports_tlb_range())
492 		return pages > MAX_TLBI_RANGE_PAGES;
493 
494 	return pages >= (MAX_DVM_OPS * stride) >> PAGE_SHIFT;
495 }
496 
497 typedef unsigned __bitwise tlbf_t;
498 
499 /* No special behaviour. */
500 #define TLBF_NONE		((__force tlbf_t)0)
501 
502 /* Invalidate tlb entries only, leaving the page table walk cache intact. */
503 #define TLBF_NOWALKCACHE	((__force tlbf_t)BIT(0))
504 
505 /* Skip the trailing dsb after issuing tlbi. */
506 #define TLBF_NOSYNC		((__force tlbf_t)BIT(1))
507 
508 /* Suppress tlb notifier callbacks for this flush operation. */
509 #define TLBF_NONOTIFY		((__force tlbf_t)BIT(2))
510 
511 /* Perform the tlbi locally without broadcasting to other CPUs. */
512 #define TLBF_NOBROADCAST	((__force tlbf_t)BIT(3))
513 
__do_flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long stride,int tlb_level,tlbf_t flags)514 static __always_inline void __do_flush_tlb_range(struct vm_area_struct *vma,
515 					unsigned long start, unsigned long end,
516 					unsigned long stride, int tlb_level,
517 					tlbf_t flags)
518 {
519 	struct mm_struct *mm = vma->vm_mm;
520 	unsigned long asid, pages;
521 
522 	pages = (end - start) >> PAGE_SHIFT;
523 
524 	if (__flush_tlb_range_limit_excess(pages, stride)) {
525 		flush_tlb_mm(mm);
526 		return;
527 	}
528 
529 	if (!(flags & TLBF_NOBROADCAST))
530 		dsb(ishst);
531 	else
532 		dsb(nshst);
533 
534 	asid = ASID(mm);
535 
536 	switch (flags & (TLBF_NOWALKCACHE | TLBF_NOBROADCAST)) {
537 	case TLBF_NONE:
538 		__flush_s1_tlb_range_op(vae1is, start, pages, stride,
539 					asid, tlb_level);
540 		break;
541 	case TLBF_NOWALKCACHE:
542 		__flush_s1_tlb_range_op(vale1is, start, pages, stride,
543 					asid, tlb_level);
544 		break;
545 	case TLBF_NOBROADCAST:
546 		/* Combination unused */
547 		BUG();
548 		break;
549 	case TLBF_NOWALKCACHE | TLBF_NOBROADCAST:
550 		__flush_s1_tlb_range_op(vale1, start, pages, stride,
551 					asid, tlb_level);
552 		break;
553 	}
554 
555 	if (!(flags & TLBF_NONOTIFY))
556 		mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end);
557 
558 	if (!(flags & TLBF_NOSYNC)) {
559 		if (!(flags & TLBF_NOBROADCAST))
560 			__tlbi_sync_s1ish();
561 		else
562 			dsb(nsh);
563 	}
564 }
565 
__flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long stride,int tlb_level,tlbf_t flags)566 static inline void __flush_tlb_range(struct vm_area_struct *vma,
567 				     unsigned long start, unsigned long end,
568 				     unsigned long stride, int tlb_level,
569 				     tlbf_t flags)
570 {
571 	start = round_down(start, stride);
572 	end = round_up(end, stride);
573 	__do_flush_tlb_range(vma, start, end, stride, tlb_level, flags);
574 }
575 
flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)576 static inline void flush_tlb_range(struct vm_area_struct *vma,
577 				   unsigned long start, unsigned long end)
578 {
579 	/*
580 	 * We cannot use leaf-only invalidation here, since we may be invalidating
581 	 * table entries as part of collapsing hugepages or moving page tables.
582 	 * Set the tlb_level to TLBI_TTL_UNKNOWN because we can not get enough
583 	 * information here.
584 	 */
585 	__flush_tlb_range(vma, start, end, PAGE_SIZE, TLBI_TTL_UNKNOWN, TLBF_NONE);
586 }
587 
__flush_tlb_page(struct vm_area_struct * vma,unsigned long uaddr,tlbf_t flags)588 static inline void __flush_tlb_page(struct vm_area_struct *vma,
589 				    unsigned long uaddr, tlbf_t flags)
590 {
591 	unsigned long start = round_down(uaddr, PAGE_SIZE);
592 	unsigned long end = start + PAGE_SIZE;
593 
594 	__do_flush_tlb_range(vma, start, end, PAGE_SIZE, 3,
595 			     TLBF_NOWALKCACHE | flags);
596 }
597 
flush_tlb_page(struct vm_area_struct * vma,unsigned long uaddr)598 static inline void flush_tlb_page(struct vm_area_struct *vma,
599 				  unsigned long uaddr)
600 {
601 	__flush_tlb_page(vma, uaddr, TLBF_NONE);
602 }
603 
flush_tlb_kernel_range(unsigned long start,unsigned long end)604 static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end)
605 {
606 	const unsigned long stride = PAGE_SIZE;
607 	unsigned long pages;
608 
609 	start = round_down(start, stride);
610 	end = round_up(end, stride);
611 	pages = (end - start) >> PAGE_SHIFT;
612 
613 	if (__flush_tlb_range_limit_excess(pages, stride)) {
614 		flush_tlb_all();
615 		return;
616 	}
617 
618 	dsb(ishst);
619 	__flush_s1_tlb_range_op(vaale1is, start, pages, stride, 0,
620 				TLBI_TTL_UNKNOWN);
621 	__tlbi_sync_s1ish();
622 	isb();
623 }
624 
625 /*
626  * Used to invalidate the TLB (walk caches) corresponding to intermediate page
627  * table levels (pgd/pud/pmd).
628  */
__flush_tlb_kernel_pgtable(unsigned long kaddr)629 static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr)
630 {
631 	unsigned long addr = __TLBI_VADDR(kaddr, 0);
632 
633 	dsb(ishst);
634 	__tlbi(vaae1is, addr);
635 	__tlbi_sync_s1ish();
636 	isb();
637 }
638 
arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch * batch,struct mm_struct * mm,unsigned long start,unsigned long end)639 static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch,
640 		struct mm_struct *mm, unsigned long start, unsigned long end)
641 {
642 	struct vm_area_struct vma = { .vm_mm = mm, .vm_flags = 0 };
643 
644 	__flush_tlb_range(&vma, start, end, PAGE_SIZE, 3,
645 			  TLBF_NOWALKCACHE | TLBF_NOSYNC);
646 }
647 
__pte_flags_need_flush(ptdesc_t oldval,ptdesc_t newval)648 static inline bool __pte_flags_need_flush(ptdesc_t oldval, ptdesc_t newval)
649 {
650 	ptdesc_t diff = oldval ^ newval;
651 
652 	/* invalid to valid transition requires no flush */
653 	if (!(oldval & PTE_VALID))
654 		return false;
655 
656 	/* Transition in the SW bits requires no flush */
657 	diff &= ~PTE_SWBITS_MASK;
658 
659 	return diff;
660 }
661 
pte_needs_flush(pte_t oldpte,pte_t newpte)662 static inline bool pte_needs_flush(pte_t oldpte, pte_t newpte)
663 {
664 	return __pte_flags_need_flush(pte_val(oldpte), pte_val(newpte));
665 }
666 #define pte_needs_flush pte_needs_flush
667 
huge_pmd_needs_flush(pmd_t oldpmd,pmd_t newpmd)668 static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd)
669 {
670 	return __pte_flags_need_flush(pmd_val(oldpmd), pmd_val(newpmd));
671 }
672 #define huge_pmd_needs_flush huge_pmd_needs_flush
673 
674 #undef __tlbi_user
675 #undef __TLBI_VADDR
676 #endif
677 
678 #endif
679