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