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