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 extern cpumask_t sme_active_cpus;
86
87 void sme_do_dvmsync(const struct cpumask *mask);
88
sme_dvmsync(struct mm_struct * mm)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
sme_dvmsync_batch(void)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
sme_dvmsync(struct mm_struct * mm)107 static inline void sme_dvmsync(struct mm_struct *mm)
108 {
109 }
sme_dvmsync_batch(void)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
vae1is(u64 arg)135 static __always_inline void vae1is(u64 arg)
136 {
137 __tlbi(vae1is, arg);
138 __tlbi_user(vae1is, arg);
139 }
140
vae2is(u64 arg)141 static __always_inline void vae2is(u64 arg)
142 {
143 __tlbi(vae2is, arg);
144 }
145
vale1(u64 arg)146 static __always_inline void vale1(u64 arg)
147 {
148 __tlbi(vale1, arg);
149 __tlbi_user(vale1, arg);
150 }
151
vale1is(u64 arg)152 static __always_inline void vale1is(u64 arg)
153 {
154 __tlbi(vale1is, arg);
155 __tlbi_user(vale1is, arg);
156 }
157
vale2is(u64 arg)158 static __always_inline void vale2is(u64 arg)
159 {
160 __tlbi(vale2is, arg);
161 }
162
vaale1is(u64 arg)163 static __always_inline void vaale1is(u64 arg)
164 {
165 __tlbi(vaale1is, arg);
166 }
167
ipas2e1(u64 arg)168 static __always_inline void ipas2e1(u64 arg)
169 {
170 __tlbi(ipas2e1, arg);
171 }
172
ipas2e1is(u64 arg)173 static __always_inline void ipas2e1is(u64 arg)
174 {
175 __tlbi(ipas2e1is, arg);
176 }
177
__tlbi_level_asid(tlbi_op op,u64 addr,u32 level,u16 asid)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
__tlbi_level(tlbi_op op,u64 addr,u32 level)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_SYNC)) \
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 */
__tlbi_sync_s1ish(struct mm_struct * mm)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
__tlbi_sync_s1ish_batch(void)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
__tlbi_sync_s1ish_kernel(void)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 */
__tlbi_sync_s1ish_hyp(void)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 */
local_flush_tlb_all(void)360 static inline void local_flush_tlb_all(void)
361 {
362 dsb(nshst);
363 __tlbi(vmalle1);
364 dsb(nsh);
365 isb();
366 }
367
flush_tlb_all(void)368 static inline void flush_tlb_all(void)
369 {
370 dsb(ishst);
371 __tlbi(vmalle1is);
372 __tlbi_sync_s1ish_kernel();
373 isb();
374 }
375
flush_tlb_mm(struct mm_struct * mm)376 static inline void flush_tlb_mm(struct mm_struct *mm)
377 {
378 unsigned long asid;
379
380 dsb(ishst);
381 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_NXP_ERR050104)) {
382 __tlbi(vmalle1is);
383 } else {
384 asid = __TLBI_VADDR(0, ASID(mm));
385 __tlbi(aside1is, asid);
386 __tlbi_user(aside1is, asid);
387 }
388 __tlbi_sync_s1ish(mm);
389 mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
390 }
391
arch_tlbbatch_should_defer(struct mm_struct * mm)392 static inline bool arch_tlbbatch_should_defer(struct mm_struct *mm)
393 {
394 return true;
395 }
396
397 /*
398 * To support TLB batched flush for multiple pages unmapping, we only send
399 * the TLBI for each page in arch_tlbbatch_add_pending() and wait for the
400 * completion at the end in arch_tlbbatch_flush(). Since we've already issued
401 * TLBI for each page so only a DSB is needed to synchronise its effect on the
402 * other CPUs.
403 *
404 * This will save the time waiting on DSB comparing issuing a TLBI;DSB sequence
405 * for each page.
406 */
arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch * batch)407 static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
408 {
409 __tlbi_sync_s1ish_batch();
410 }
411
412 /*
413 * This is meant to avoid soft lock-ups on large TLB flushing ranges and not
414 * necessarily a performance improvement.
415 */
416 #define MAX_DVM_OPS PTRS_PER_PTE
417
418 /*
419 * __flush_tlb_range_op - Perform TLBI operation upon a range
420 *
421 * @lop: TLBI level operation to perform
422 * @rop: TLBI range operation to perform
423 * @start: The start address of the range
424 * @pages: Range as the number of pages from 'start'
425 * @stride: Flush granularity
426 * @asid: The ASID of the task (0 for IPA instructions)
427 * @level: Translation Table level hint, if known
428 * @lpa2: If 'true', the lpa2 scheme is used as set out below
429 *
430 * When the CPU does not support TLB range operations, flush the TLB
431 * entries one by one at the granularity of 'stride'. If the TLB
432 * range ops are supported, then:
433 *
434 * 1. If FEAT_LPA2 is in use, the start address of a range operation must be
435 * 64KB aligned, so flush pages one by one until the alignment is reached
436 * using the non-range operations. This step is skipped if LPA2 is not in
437 * use.
438 *
439 * 2. The minimum range granularity is decided by 'scale', so multiple range
440 * TLBI operations may be required. Start from scale = 3, flush the largest
441 * possible number of pages ((num+1)*2^(5*scale+1)) that fit into the
442 * requested range, then decrement scale and continue until one or zero pages
443 * are left. We must start from highest scale to ensure 64KB start alignment
444 * is maintained in the LPA2 case.
445 *
446 * 3. If there is 1 page remaining, flush it through non-range operations. Range
447 * operations can only span an even number of pages. We save this for last to
448 * ensure 64KB start alignment is maintained for the LPA2 case.
449 */
rvae1is(u64 arg)450 static __always_inline void rvae1is(u64 arg)
451 {
452 __tlbi(rvae1is, arg);
453 __tlbi_user(rvae1is, arg);
454 }
455
rvale1(u64 arg)456 static __always_inline void rvale1(u64 arg)
457 {
458 __tlbi(rvale1, arg);
459 __tlbi_user(rvale1, arg);
460 }
461
rvale1is(u64 arg)462 static __always_inline void rvale1is(u64 arg)
463 {
464 __tlbi(rvale1is, arg);
465 __tlbi_user(rvale1is, arg);
466 }
467
rvaale1is(u64 arg)468 static __always_inline void rvaale1is(u64 arg)
469 {
470 __tlbi(rvaale1is, arg);
471 }
472
ripas2e1is(u64 arg)473 static __always_inline void ripas2e1is(u64 arg)
474 {
475 __tlbi(ripas2e1is, arg);
476 }
477
__tlbi_range(tlbi_op op,u64 addr,u16 asid,int scale,int num,u32 level,bool lpa2)478 static __always_inline void __tlbi_range(tlbi_op op, u64 addr,
479 u16 asid, int scale, int num,
480 u32 level, bool lpa2)
481 {
482 u64 arg = 0;
483
484 arg |= FIELD_PREP(TLBIR_BADDR_MASK, addr >> (lpa2 ? 16 : PAGE_SHIFT));
485 arg |= FIELD_PREP(TLBIR_TTL_MASK, level > 3 ? 0 : level);
486 arg |= FIELD_PREP(TLBIR_NUM_MASK, num);
487 arg |= FIELD_PREP(TLBIR_SCALE_MASK, scale);
488 arg |= FIELD_PREP(TLBIR_TG_MASK, get_trans_granule());
489 arg |= FIELD_PREP(TLBIR_ASID_MASK, asid);
490
491 op(arg);
492 }
493
__flush_tlb_range_op(tlbi_op lop,tlbi_op rop,u64 start,size_t pages,u64 stride,u16 asid,u32 level,bool lpa2)494 static __always_inline void __flush_tlb_range_op(tlbi_op lop, tlbi_op rop,
495 u64 start, size_t pages,
496 u64 stride, u16 asid,
497 u32 level, bool lpa2)
498 {
499 u64 addr = start, end = start + pages * PAGE_SIZE;
500 int scale = 3;
501
502 while (addr != end) {
503 int num;
504
505 pages = (end - addr) >> PAGE_SHIFT;
506
507 if (!system_supports_tlb_range() || pages == 1)
508 goto invalidate_one;
509
510 if (lpa2 && !IS_ALIGNED(addr, SZ_64K))
511 goto invalidate_one;
512
513 num = __TLBI_RANGE_NUM(pages, scale);
514 if (num >= 0) {
515 __tlbi_range(rop, addr, asid, scale, num, level, lpa2);
516 addr += __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT;
517 }
518
519 scale--;
520 continue;
521 invalidate_one:
522 __tlbi_level_asid(lop, addr, level, asid);
523 addr += stride;
524 }
525 }
526
527 #define __flush_s1_tlb_range_op(op, start, pages, stride, asid, tlb_level) \
528 __flush_tlb_range_op(op, r##op, start, pages, stride, asid, tlb_level, lpa2_is_enabled())
529
530 #define __flush_s2_tlb_range_op(op, start, pages, stride, tlb_level) \
531 __flush_tlb_range_op(op, r##op, start, pages, stride, 0, tlb_level, kvm_lpa2_is_enabled())
532
__flush_tlb_range_limit_excess(unsigned long pages,unsigned long stride)533 static inline bool __flush_tlb_range_limit_excess(unsigned long pages,
534 unsigned long stride)
535 {
536 /*
537 * Assume that the worst case number of DVM ops required to flush a
538 * given range on a system that supports tlb-range is 20 (4 scales, 1
539 * final page, 15 for alignment on LPA2 systems), which is much smaller
540 * than MAX_DVM_OPS.
541 */
542 if (system_supports_tlb_range())
543 return pages > MAX_TLBI_RANGE_PAGES;
544
545 return pages >= (MAX_DVM_OPS * stride) >> PAGE_SHIFT;
546 }
547
548 typedef unsigned __bitwise tlbf_t;
549
550 /* No special behaviour. */
551 #define TLBF_NONE ((__force tlbf_t)0)
552
553 /* Invalidate tlb entries only, leaving the page table walk cache intact. */
554 #define TLBF_NOWALKCACHE ((__force tlbf_t)BIT(0))
555
556 /* Skip the trailing dsb after issuing tlbi. */
557 #define TLBF_NOSYNC ((__force tlbf_t)BIT(1))
558
559 /* Suppress tlb notifier callbacks for this flush operation. */
560 #define TLBF_NONOTIFY ((__force tlbf_t)BIT(2))
561
562 /* Perform the tlbi locally without broadcasting to other CPUs. */
563 #define TLBF_NOBROADCAST ((__force tlbf_t)BIT(3))
564
__do_flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long stride,int tlb_level,tlbf_t flags)565 static __always_inline void __do_flush_tlb_range(struct vm_area_struct *vma,
566 unsigned long start, unsigned long end,
567 unsigned long stride, int tlb_level,
568 tlbf_t flags)
569 {
570 struct mm_struct *mm = vma->vm_mm;
571 unsigned long asid, pages;
572
573 pages = (end - start) >> PAGE_SHIFT;
574
575 if (__flush_tlb_range_limit_excess(pages, stride)) {
576 flush_tlb_mm(mm);
577 return;
578 }
579
580 if (!(flags & TLBF_NOBROADCAST))
581 dsb(ishst);
582 else
583 dsb(nshst);
584
585 asid = ASID(mm);
586
587 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_NXP_ERR050104) &&
588 !(flags & TLBF_NOBROADCAST)) {
589 __tlbi(vmalle1is);
590 } else {
591 switch (flags & (TLBF_NOWALKCACHE | TLBF_NOBROADCAST)) {
592 case TLBF_NONE:
593 __flush_s1_tlb_range_op(vae1is, start, pages, stride,
594 asid, tlb_level);
595 break;
596 case TLBF_NOWALKCACHE:
597 __flush_s1_tlb_range_op(vale1is, start, pages, stride,
598 asid, tlb_level);
599 break;
600 case TLBF_NOBROADCAST:
601 /* Combination unused */
602 BUG();
603 break;
604 case TLBF_NOWALKCACHE | TLBF_NOBROADCAST:
605 __flush_s1_tlb_range_op(vale1, start, pages, stride,
606 asid, tlb_level);
607 break;
608 }
609 }
610
611 if (!(flags & TLBF_NONOTIFY))
612 mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end);
613
614 if (!(flags & TLBF_NOSYNC)) {
615 if (!(flags & TLBF_NOBROADCAST))
616 __tlbi_sync_s1ish(mm);
617 else
618 dsb(nsh);
619 }
620 }
621
__flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long stride,int tlb_level,tlbf_t flags)622 static inline void __flush_tlb_range(struct vm_area_struct *vma,
623 unsigned long start, unsigned long end,
624 unsigned long stride, int tlb_level,
625 tlbf_t flags)
626 {
627 start = round_down(start, stride);
628 end = round_up(end, stride);
629 __do_flush_tlb_range(vma, start, end, stride, tlb_level, flags);
630 }
631
flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)632 static inline void flush_tlb_range(struct vm_area_struct *vma,
633 unsigned long start, unsigned long end)
634 {
635 /*
636 * We cannot use leaf-only invalidation here, since we may be invalidating
637 * table entries as part of collapsing hugepages or moving page tables.
638 * Set the tlb_level to TLBI_TTL_UNKNOWN because we can not get enough
639 * information here.
640 */
641 __flush_tlb_range(vma, start, end, PAGE_SIZE, TLBI_TTL_UNKNOWN, TLBF_NONE);
642 }
643
__flush_tlb_page(struct vm_area_struct * vma,unsigned long uaddr,tlbf_t flags)644 static inline void __flush_tlb_page(struct vm_area_struct *vma,
645 unsigned long uaddr, tlbf_t flags)
646 {
647 unsigned long start = round_down(uaddr, PAGE_SIZE);
648 unsigned long end = start + PAGE_SIZE;
649
650 __do_flush_tlb_range(vma, start, end, PAGE_SIZE, 3,
651 TLBF_NOWALKCACHE | flags);
652 }
653
flush_tlb_page(struct vm_area_struct * vma,unsigned long uaddr)654 static inline void flush_tlb_page(struct vm_area_struct *vma,
655 unsigned long uaddr)
656 {
657 __flush_tlb_page(vma, uaddr, TLBF_NONE);
658 }
659
flush_tlb_kernel_range(unsigned long start,unsigned long end)660 static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end)
661 {
662 const unsigned long stride = PAGE_SIZE;
663 unsigned long pages;
664
665 start = round_down(start, stride);
666 end = round_up(end, stride);
667 pages = (end - start) >> PAGE_SHIFT;
668
669 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_NXP_ERR050104) ||
670 __flush_tlb_range_limit_excess(pages, stride)) {
671 flush_tlb_all();
672 return;
673 }
674
675 dsb(ishst);
676 __flush_s1_tlb_range_op(vaale1is, start, pages, stride, 0,
677 TLBI_TTL_UNKNOWN);
678 __tlbi_sync_s1ish_kernel();
679 isb();
680 }
681
682 /*
683 * Used to invalidate the TLB (walk caches) corresponding to intermediate page
684 * table levels (pgd/pud/pmd).
685 */
__flush_tlb_kernel_pgtable(unsigned long kaddr)686 static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr)
687 {
688 dsb(ishst);
689 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_NXP_ERR050104)) {
690 __tlbi(vmalle1is);
691 } else {
692 unsigned long addr = __TLBI_VADDR(kaddr, 0);
693
694 __tlbi(vaae1is, addr);
695 }
696 __tlbi_sync_s1ish_kernel();
697 isb();
698 }
699
arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch * batch,struct mm_struct * mm,unsigned long start,unsigned long end)700 static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch,
701 struct mm_struct *mm, unsigned long start, unsigned long end)
702 {
703 struct vm_area_struct vma = { .vm_mm = mm, .vm_flags = 0 };
704
705 __flush_tlb_range(&vma, start, end, PAGE_SIZE, 3,
706 TLBF_NOWALKCACHE | TLBF_NOSYNC);
707 }
708
__pte_flags_need_flush(ptval_t oldval,ptval_t newval)709 static inline bool __pte_flags_need_flush(ptval_t oldval, ptval_t newval)
710 {
711 ptval_t diff = oldval ^ newval;
712
713 /* invalid to valid transition requires no flush */
714 if (!(oldval & PTE_VALID))
715 return false;
716
717 /* Transition in the SW bits requires no flush */
718 diff &= ~PTE_SWBITS_MASK;
719
720 return diff;
721 }
722
pte_needs_flush(pte_t oldpte,pte_t newpte)723 static inline bool pte_needs_flush(pte_t oldpte, pte_t newpte)
724 {
725 return __pte_flags_need_flush(pte_val(oldpte), pte_val(newpte));
726 }
727 #define pte_needs_flush pte_needs_flush
728
huge_pmd_needs_flush(pmd_t oldpmd,pmd_t newpmd)729 static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd)
730 {
731 return __pte_flags_need_flush(pmd_val(oldpmd), pmd_val(newpmd));
732 }
733 #define huge_pmd_needs_flush huge_pmd_needs_flush
734
735 #undef __tlbi_user
736 #undef __TLBI_VADDR
737 #endif
738
739 #endif
740