1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * arch/arm/include/asm/tlbflush.h
5 * Copyright (C) 1999-2003 Russell King
7 #ifndef _ASMARM_TLBFLUSH_H
8 #define _ASMARM_TLBFLUSH_H
11 # include <linux/mm_types.h>
18 #define TLB_V4_U_PAGE (1 << 1)
19 #define TLB_V4_D_PAGE (1 << 2)
20 #define TLB_V4_I_PAGE (1 << 3)
21 #define TLB_V6_U_PAGE (1 << 4)
22 #define TLB_V6_D_PAGE (1 << 5)
23 #define TLB_V6_I_PAGE (1 << 6)
25 #define TLB_V4_U_FULL (1 << 9)
26 #define TLB_V4_D_FULL (1 << 10)
27 #define TLB_V4_I_FULL (1 << 11)
28 #define TLB_V6_U_FULL (1 << 12)
29 #define TLB_V6_D_FULL (1 << 13)
30 #define TLB_V6_I_FULL (1 << 14)
32 #define TLB_V6_U_ASID (1 << 16)
33 #define TLB_V6_D_ASID (1 << 17)
34 #define TLB_V6_I_ASID (1 << 18)
36 #define TLB_V6_BP (1 << 19)
38 /* Unified Inner Shareable TLB operations (ARMv7 MP extensions) */
39 #define TLB_V7_UIS_PAGE (1 << 20)
40 #define TLB_V7_UIS_FULL (1 << 21)
41 #define TLB_V7_UIS_ASID (1 << 22)
42 #define TLB_V7_UIS_BP (1 << 23)
44 #define TLB_BARRIER (1 << 28)
45 #define TLB_L2CLEAN_FR (1 << 29) /* Feroceon */
46 #define TLB_DCLEAN (1 << 30)
47 #define TLB_WB (1 << 31)
53 * We have the following to choose from:
54 * v4 - ARMv4 without write buffer
55 * v4wb - ARMv4 with write buffer without I TLB flush entry instruction
56 * v4wbi - ARMv4 with write buffer with I TLB flush entry instruction
57 * fr - Feroceon (v4wbi with non-outer-cacheable page table walks)
58 * fa - Faraday (v4 with write buffer with UTLB)
59 * v6wbi - ARMv6 with write buffer with I TLB flush entry instruction
60 * v7wbi - identical to v6wbi
65 #ifdef CONFIG_SMP_ON_UP
69 #define v4_tlb_flags (TLB_V4_U_FULL | TLB_V4_U_PAGE)
71 #ifdef CONFIG_CPU_TLB_V4WT
72 # define v4_possible_flags v4_tlb_flags
73 # define v4_always_flags v4_tlb_flags
80 # define v4_possible_flags 0
81 # define v4_always_flags (-1UL)
84 #define fa_tlb_flags (TLB_WB | TLB_DCLEAN | TLB_BARRIER | \
85 TLB_V4_U_FULL | TLB_V4_U_PAGE)
87 #ifdef CONFIG_CPU_TLB_FA
88 # define fa_possible_flags fa_tlb_flags
89 # define fa_always_flags fa_tlb_flags
96 # define fa_possible_flags 0
97 # define fa_always_flags (-1UL)
100 #define v4wbi_tlb_flags (TLB_WB | TLB_DCLEAN | \
101 TLB_V4_I_FULL | TLB_V4_D_FULL | \
102 TLB_V4_I_PAGE | TLB_V4_D_PAGE)
104 #ifdef CONFIG_CPU_TLB_V4WBI
105 # define v4wbi_possible_flags v4wbi_tlb_flags
106 # define v4wbi_always_flags v4wbi_tlb_flags
113 # define v4wbi_possible_flags 0
114 # define v4wbi_always_flags (-1UL)
117 #define fr_tlb_flags (TLB_WB | TLB_DCLEAN | TLB_L2CLEAN_FR | \
118 TLB_V4_I_FULL | TLB_V4_D_FULL | \
119 TLB_V4_I_PAGE | TLB_V4_D_PAGE)
121 #ifdef CONFIG_CPU_TLB_FEROCEON
122 # define fr_possible_flags fr_tlb_flags
123 # define fr_always_flags fr_tlb_flags
130 # define fr_possible_flags 0
131 # define fr_always_flags (-1UL)
134 #define v4wb_tlb_flags (TLB_WB | TLB_DCLEAN | \
135 TLB_V4_I_FULL | TLB_V4_D_FULL | \
138 #ifdef CONFIG_CPU_TLB_V4WB
139 # define v4wb_possible_flags v4wb_tlb_flags
140 # define v4wb_always_flags v4wb_tlb_flags
147 # define v4wb_possible_flags 0
148 # define v4wb_always_flags (-1UL)
151 #define v6wbi_tlb_flags (TLB_WB | TLB_DCLEAN | TLB_BARRIER | \
152 TLB_V6_I_FULL | TLB_V6_D_FULL | \
153 TLB_V6_I_PAGE | TLB_V6_D_PAGE | \
154 TLB_V6_I_ASID | TLB_V6_D_ASID | \
157 #ifdef CONFIG_CPU_TLB_V6
158 # define v6wbi_possible_flags v6wbi_tlb_flags
159 # define v6wbi_always_flags v6wbi_tlb_flags
166 # define v6wbi_possible_flags 0
167 # define v6wbi_always_flags (-1UL)
170 #define v7wbi_tlb_flags_smp (TLB_WB | TLB_BARRIER | \
171 TLB_V7_UIS_FULL | TLB_V7_UIS_PAGE | \
172 TLB_V7_UIS_ASID | TLB_V7_UIS_BP)
173 #define v7wbi_tlb_flags_up (TLB_WB | TLB_DCLEAN | TLB_BARRIER | \
174 TLB_V6_U_FULL | TLB_V6_U_PAGE | \
175 TLB_V6_U_ASID | TLB_V6_BP)
177 #ifdef CONFIG_CPU_TLB_V7
179 # ifdef CONFIG_SMP_ON_UP
180 # define v7wbi_possible_flags (v7wbi_tlb_flags_smp | v7wbi_tlb_flags_up)
181 # define v7wbi_always_flags (v7wbi_tlb_flags_smp & v7wbi_tlb_flags_up)
182 # elif defined(CONFIG_SMP)
183 # define v7wbi_possible_flags v7wbi_tlb_flags_smp
184 # define v7wbi_always_flags v7wbi_tlb_flags_smp
186 # define v7wbi_possible_flags v7wbi_tlb_flags_up
187 # define v7wbi_always_flags v7wbi_tlb_flags_up
195 # define v7wbi_possible_flags 0
196 # define v7wbi_always_flags (-1UL)
200 #error Unknown TLB model
205 #include <linux/sched.h>
208 void (*flush_user_range
)(unsigned long, unsigned long, struct vm_area_struct
*);
209 void (*flush_kern_range
)(unsigned long, unsigned long);
210 unsigned long tlb_flags
;
214 * Select the calling method
218 #define __cpu_flush_user_tlb_range cpu_tlb.flush_user_range
219 #define __cpu_flush_kern_tlb_range cpu_tlb.flush_kern_range
223 #define __cpu_flush_user_tlb_range __glue(_TLB,_flush_user_tlb_range)
224 #define __cpu_flush_kern_tlb_range __glue(_TLB,_flush_kern_tlb_range)
226 extern void __cpu_flush_user_tlb_range(unsigned long, unsigned long, struct vm_area_struct
*);
227 extern void __cpu_flush_kern_tlb_range(unsigned long, unsigned long);
231 extern struct cpu_tlb_fns cpu_tlb
;
233 #define __cpu_tlb_flags cpu_tlb.tlb_flags
239 * The arch/arm/mm/tlb-*.S files implement these methods.
241 * The TLB specific code is expected to perform whatever tests it
242 * needs to determine if it should invalidate the TLB for each
243 * call. Start addresses are inclusive and end addresses are
244 * exclusive; it is safe to round these addresses down.
248 * Invalidate the entire TLB.
252 * Invalidate all TLB entries in a particular address
254 * - mm - mm_struct describing address space
256 * flush_tlb_range(mm,start,end)
258 * Invalidate a range of TLB entries in the specified
260 * - mm - mm_struct describing address space
261 * - start - start address (may not be aligned)
262 * - end - end address (exclusive, may not be aligned)
264 * flush_tlb_page(vaddr,vma)
266 * Invalidate the specified page in the specified address range.
267 * - vaddr - virtual address (may not be aligned)
268 * - vma - vma_struct describing address range
270 * flush_kern_tlb_page(kaddr)
272 * Invalidate the TLB entry for the specified page. The address
273 * will be in the kernels virtual memory space. Current uses
274 * only require the D-TLB to be invalidated.
275 * - kaddr - Kernel virtual memory address
279 * We optimise the code below by:
280 * - building a set of TLB flags that might be set in __cpu_tlb_flags
281 * - building a set of TLB flags that will always be set in __cpu_tlb_flags
282 * - if we're going to need __cpu_tlb_flags, access it once and only once
284 * This allows us to build optimal assembly for the single-CPU type case,
285 * and as close to optimal given the compiler constrants for multi-CPU
286 * case. We could do better for the multi-CPU case if the compiler
287 * implemented the "%?" method, but this has been discontinued due to too
288 * many people getting it wrong.
290 #define possible_tlb_flags (v4_possible_flags | \
291 v4wbi_possible_flags | \
292 fr_possible_flags | \
293 v4wb_possible_flags | \
294 fa_possible_flags | \
295 v6wbi_possible_flags | \
296 v7wbi_possible_flags)
298 #define always_tlb_flags (v4_always_flags & \
299 v4wbi_always_flags & \
301 v4wb_always_flags & \
303 v6wbi_always_flags & \
306 #define tlb_flag(f) ((always_tlb_flags & (f)) || (__tlb_flag & possible_tlb_flags & (f)))
308 #define __tlb_op(f, insnarg, arg) \
310 if (always_tlb_flags & (f)) \
312 : : "r" (arg) : "cc"); \
313 else if (possible_tlb_flags & (f)) \
314 asm("tst %1, %2\n\t" \
316 : : "r" (arg), "r" (__tlb_flag), "Ir" (f) \
320 #define tlb_op(f, regs, arg) __tlb_op(f, "p15, 0, %0, " regs, arg)
321 #define tlb_l2_op(f, regs, arg) __tlb_op(f, "p15, 1, %0, " regs, arg)
323 static inline void __local_flush_tlb_all(void)
326 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
328 tlb_op(TLB_V4_U_FULL
| TLB_V6_U_FULL
, "c8, c7, 0", zero
);
329 tlb_op(TLB_V4_D_FULL
| TLB_V6_D_FULL
, "c8, c6, 0", zero
);
330 tlb_op(TLB_V4_I_FULL
| TLB_V6_I_FULL
, "c8, c5, 0", zero
);
333 static inline void local_flush_tlb_all(void)
336 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
338 if (tlb_flag(TLB_WB
))
341 __local_flush_tlb_all();
342 tlb_op(TLB_V7_UIS_FULL
, "c8, c7, 0", zero
);
344 if (tlb_flag(TLB_BARRIER
)) {
350 static inline void __flush_tlb_all(void)
353 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
355 if (tlb_flag(TLB_WB
))
358 __local_flush_tlb_all();
359 tlb_op(TLB_V7_UIS_FULL
, "c8, c3, 0", zero
);
361 if (tlb_flag(TLB_BARRIER
)) {
367 static inline void __local_flush_tlb_mm(struct mm_struct
*mm
)
370 const int asid
= ASID(mm
);
371 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
373 if (possible_tlb_flags
& (TLB_V4_U_FULL
|TLB_V4_D_FULL
|TLB_V4_I_FULL
)) {
374 if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm
))) {
375 tlb_op(TLB_V4_U_FULL
, "c8, c7, 0", zero
);
376 tlb_op(TLB_V4_D_FULL
, "c8, c6, 0", zero
);
377 tlb_op(TLB_V4_I_FULL
, "c8, c5, 0", zero
);
381 tlb_op(TLB_V6_U_ASID
, "c8, c7, 2", asid
);
382 tlb_op(TLB_V6_D_ASID
, "c8, c6, 2", asid
);
383 tlb_op(TLB_V6_I_ASID
, "c8, c5, 2", asid
);
386 static inline void local_flush_tlb_mm(struct mm_struct
*mm
)
388 const int asid
= ASID(mm
);
389 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
391 if (tlb_flag(TLB_WB
))
394 __local_flush_tlb_mm(mm
);
395 tlb_op(TLB_V7_UIS_ASID
, "c8, c7, 2", asid
);
397 if (tlb_flag(TLB_BARRIER
))
401 static inline void __flush_tlb_mm(struct mm_struct
*mm
)
403 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
405 if (tlb_flag(TLB_WB
))
408 __local_flush_tlb_mm(mm
);
409 #ifdef CONFIG_ARM_ERRATA_720789
410 tlb_op(TLB_V7_UIS_ASID
, "c8, c3, 0", 0);
412 tlb_op(TLB_V7_UIS_ASID
, "c8, c3, 2", ASID(mm
));
415 if (tlb_flag(TLB_BARRIER
))
420 __local_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long uaddr
)
423 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
425 uaddr
= (uaddr
& PAGE_MASK
) | ASID(vma
->vm_mm
);
427 if (possible_tlb_flags
& (TLB_V4_U_PAGE
|TLB_V4_D_PAGE
|TLB_V4_I_PAGE
|TLB_V4_I_FULL
) &&
428 cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma
->vm_mm
))) {
429 tlb_op(TLB_V4_U_PAGE
, "c8, c7, 1", uaddr
);
430 tlb_op(TLB_V4_D_PAGE
, "c8, c6, 1", uaddr
);
431 tlb_op(TLB_V4_I_PAGE
, "c8, c5, 1", uaddr
);
432 if (!tlb_flag(TLB_V4_I_PAGE
) && tlb_flag(TLB_V4_I_FULL
))
433 asm("mcr p15, 0, %0, c8, c5, 0" : : "r" (zero
) : "cc");
436 tlb_op(TLB_V6_U_PAGE
, "c8, c7, 1", uaddr
);
437 tlb_op(TLB_V6_D_PAGE
, "c8, c6, 1", uaddr
);
438 tlb_op(TLB_V6_I_PAGE
, "c8, c5, 1", uaddr
);
442 local_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long uaddr
)
444 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
446 uaddr
= (uaddr
& PAGE_MASK
) | ASID(vma
->vm_mm
);
448 if (tlb_flag(TLB_WB
))
451 __local_flush_tlb_page(vma
, uaddr
);
452 tlb_op(TLB_V7_UIS_PAGE
, "c8, c7, 1", uaddr
);
454 if (tlb_flag(TLB_BARRIER
))
459 __flush_tlb_page(struct vm_area_struct
*vma
, unsigned long uaddr
)
461 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
463 uaddr
= (uaddr
& PAGE_MASK
) | ASID(vma
->vm_mm
);
465 if (tlb_flag(TLB_WB
))
468 __local_flush_tlb_page(vma
, uaddr
);
469 #ifdef CONFIG_ARM_ERRATA_720789
470 tlb_op(TLB_V7_UIS_PAGE
, "c8, c3, 3", uaddr
& PAGE_MASK
);
472 tlb_op(TLB_V7_UIS_PAGE
, "c8, c3, 1", uaddr
);
475 if (tlb_flag(TLB_BARRIER
))
479 static inline void __local_flush_tlb_kernel_page(unsigned long kaddr
)
482 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
484 tlb_op(TLB_V4_U_PAGE
, "c8, c7, 1", kaddr
);
485 tlb_op(TLB_V4_D_PAGE
, "c8, c6, 1", kaddr
);
486 tlb_op(TLB_V4_I_PAGE
, "c8, c5, 1", kaddr
);
487 if (!tlb_flag(TLB_V4_I_PAGE
) && tlb_flag(TLB_V4_I_FULL
))
488 asm("mcr p15, 0, %0, c8, c5, 0" : : "r" (zero
) : "cc");
490 tlb_op(TLB_V6_U_PAGE
, "c8, c7, 1", kaddr
);
491 tlb_op(TLB_V6_D_PAGE
, "c8, c6, 1", kaddr
);
492 tlb_op(TLB_V6_I_PAGE
, "c8, c5, 1", kaddr
);
495 static inline void local_flush_tlb_kernel_page(unsigned long kaddr
)
497 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
501 if (tlb_flag(TLB_WB
))
504 __local_flush_tlb_kernel_page(kaddr
);
505 tlb_op(TLB_V7_UIS_PAGE
, "c8, c7, 1", kaddr
);
507 if (tlb_flag(TLB_BARRIER
)) {
513 static inline void __flush_tlb_kernel_page(unsigned long kaddr
)
515 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
519 if (tlb_flag(TLB_WB
))
522 __local_flush_tlb_kernel_page(kaddr
);
523 tlb_op(TLB_V7_UIS_PAGE
, "c8, c3, 1", kaddr
);
525 if (tlb_flag(TLB_BARRIER
)) {
532 * Branch predictor maintenance is paired with full TLB invalidation, so
533 * there is no need for any barriers here.
535 static inline void __local_flush_bp_all(void)
538 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
540 if (tlb_flag(TLB_V6_BP
))
541 asm("mcr p15, 0, %0, c7, c5, 6" : : "r" (zero
));
544 static inline void local_flush_bp_all(void)
547 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
549 __local_flush_bp_all();
550 if (tlb_flag(TLB_V7_UIS_BP
))
551 asm("mcr p15, 0, %0, c7, c5, 6" : : "r" (zero
));
554 static inline void __flush_bp_all(void)
557 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
559 __local_flush_bp_all();
560 if (tlb_flag(TLB_V7_UIS_BP
))
561 asm("mcr p15, 0, %0, c7, c1, 6" : : "r" (zero
));
567 * Flush a PMD entry (word aligned, or double-word aligned) to
568 * RAM if the TLB for the CPU we are running on requires this.
569 * This is typically used when we are creating PMD entries.
573 * Clean (but don't drain the write buffer) if the CPU requires
574 * these operations. This is typically used when we are removing
577 static inline void flush_pmd_entry(void *pmd
)
579 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
581 tlb_op(TLB_DCLEAN
, "c7, c10, 1 @ flush_pmd", pmd
);
582 tlb_l2_op(TLB_L2CLEAN_FR
, "c15, c9, 1 @ L2 flush_pmd", pmd
);
584 if (tlb_flag(TLB_WB
))
588 static inline void clean_pmd_entry(void *pmd
)
590 const unsigned int __tlb_flag
= __cpu_tlb_flags
;
592 tlb_op(TLB_DCLEAN
, "c7, c10, 1 @ flush_pmd", pmd
);
593 tlb_l2_op(TLB_L2CLEAN_FR
, "c15, c9, 1 @ L2 flush_pmd", pmd
);
598 #undef always_tlb_flags
599 #undef possible_tlb_flags
602 * Convert calls to our calling convention.
604 #define local_flush_tlb_range(vma,start,end) __cpu_flush_user_tlb_range(start,end,vma)
605 #define local_flush_tlb_kernel_range(s,e) __cpu_flush_kern_tlb_range(s,e)
608 #define flush_tlb_all local_flush_tlb_all
609 #define flush_tlb_mm local_flush_tlb_mm
610 #define flush_tlb_page local_flush_tlb_page
611 #define flush_tlb_kernel_page local_flush_tlb_kernel_page
612 #define flush_tlb_range local_flush_tlb_range
613 #define flush_tlb_kernel_range local_flush_tlb_kernel_range
614 #define flush_bp_all local_flush_bp_all
616 extern void flush_tlb_all(void);
617 extern void flush_tlb_mm(struct mm_struct
*mm
);
618 extern void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long uaddr
);
619 extern void flush_tlb_kernel_page(unsigned long kaddr
);
620 extern void flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
, unsigned long end
);
621 extern void flush_tlb_kernel_range(unsigned long start
, unsigned long end
);
622 extern void flush_bp_all(void);
626 * If PG_dcache_clean is not set for the page, we need to ensure that any
627 * cache entries for the kernels virtual memory range are written
628 * back to the page. On ARMv6 and later, the cache coherency is handled via
629 * the set_pte_at() function.
631 #if __LINUX_ARM_ARCH__ < 6
632 extern void update_mmu_cache(struct vm_area_struct
*vma
, unsigned long addr
,
635 static inline void update_mmu_cache(struct vm_area_struct
*vma
,
636 unsigned long addr
, pte_t
*ptep
)
641 #define update_mmu_cache_pmd(vma, address, pmd) do { } while (0)
645 #elif defined(CONFIG_SMP) /* !CONFIG_MMU */
648 static inline void local_flush_tlb_all(void) { }
649 static inline void local_flush_tlb_mm(struct mm_struct
*mm
) { }
650 static inline void local_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long uaddr
) { }
651 static inline void local_flush_tlb_kernel_page(unsigned long kaddr
) { }
652 static inline void local_flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
, unsigned long end
) { }
653 static inline void local_flush_tlb_kernel_range(unsigned long start
, unsigned long end
) { }
654 static inline void local_flush_bp_all(void) { }
656 extern void flush_tlb_all(void);
657 extern void flush_tlb_mm(struct mm_struct
*mm
);
658 extern void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long uaddr
);
659 extern void flush_tlb_kernel_page(unsigned long kaddr
);
660 extern void flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
, unsigned long end
);
661 extern void flush_tlb_kernel_range(unsigned long start
, unsigned long end
);
662 extern void flush_bp_all(void);
663 #endif /* __ASSEMBLY__ */
668 #ifdef CONFIG_ARM_ERRATA_798181
669 extern void erratum_a15_798181_init(void);
671 static inline void erratum_a15_798181_init(void) {}
673 extern bool (*erratum_a15_798181_handler
)(void);
675 static inline bool erratum_a15_798181(void)
677 if (unlikely(IS_ENABLED(CONFIG_ARM_ERRATA_798181
) &&
678 erratum_a15_798181_handler
))
679 return erratum_a15_798181_handler();