1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * This file contains the routines for TLB flushing.
4 * On machines where the MMU does not use a hash table to store virtual to
5 * physical translations (ie, SW loaded TLBs or Book3E compilant processors,
6 * this does -not- include 603 however which shares the implementation with
7 * hash based processors)
14 * Derived from arch/ppc/mm/init.c:
19 * Copyright (C) 1996 Paul Mackerras
21 * Derived from "arch/i386/mm/init.c"
22 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
25 #include <linux/kernel.h>
26 #include <linux/export.h>
28 #include <linux/init.h>
29 #include <linux/highmem.h>
30 #include <linux/pagemap.h>
31 #include <linux/preempt.h>
32 #include <linux/spinlock.h>
33 #include <linux/memblock.h>
34 #include <linux/of_fdt.h>
35 #include <linux/hugetlb.h>
37 #include <asm/pgalloc.h>
38 #include <asm/tlbflush.h>
40 #include <asm/code-patching.h>
41 #include <asm/cputhreads.h>
42 #include <asm/hugetlb.h>
45 #include <mm/mmu_decl.h>
48 * This struct lists the sw-supported page sizes. The hardawre MMU may support
49 * other sizes not listed here. The .ind field is only used on MMUs that have
50 * indirect page table entries.
52 #ifdef CONFIG_PPC_E500
53 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
56 .enc = BOOK3E_PAGESZ_4K,
60 .enc = BOOK3E_PAGESZ_2M,
64 .enc = BOOK3E_PAGESZ_4M,
68 .enc = BOOK3E_PAGESZ_16M,
72 .enc = BOOK3E_PAGESZ_64M,
76 .enc = BOOK3E_PAGESZ_256M,
80 .enc = BOOK3E_PAGESZ_1GB,
84 static inline int mmu_get_tsize(int psize)
86 return mmu_psize_defs[psize].enc;
89 static inline int mmu_get_tsize(int psize)
91 /* This isn't used on !Book3E for now */
97 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
113 /* The variables below are currently only used on 64-bit Book3E
114 * though this will probably be made common with other nohash
115 * implementations at some point
119 int mmu_pte_psize; /* Page size used for PTE pages */
120 int mmu_vmemmap_psize; /* Page size used for the virtual mem map */
121 int book3e_htw_mode; /* HW tablewalk? Value is PPC_HTW_* */
122 unsigned long linear_map_top; /* Top of linear mapping */
126 * Number of bytes to add to SPRN_SPRG_TLB_EXFRAME on crit/mcheck/debug
127 * exceptions. This is used for bolted and e6500 TLB miss handlers which
128 * do not modify this SPRG in the TLB miss code; for other TLB miss handlers,
129 * this is set to zero.
133 #endif /* CONFIG_PPC64 */
135 #ifdef CONFIG_PPC_E500
136 /* next_tlbcam_idx is used to round-robin tlbcam entry assignment */
137 DEFINE_PER_CPU(int, next_tlbcam_idx);
138 EXPORT_PER_CPU_SYMBOL(next_tlbcam_idx);
142 * Base TLB flushing operations:
144 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
145 * - flush_tlb_page(vma, vmaddr) flushes one page
146 * - flush_tlb_range(vma, start, end) flushes a range of pages
147 * - flush_tlb_kernel_range(start, end) flushes kernel pages
149 * - local_* variants of page and mm only apply to the current
153 #ifndef CONFIG_PPC_8xx
155 * These are the base non-SMP variants of page and mm flushing
157 void local_flush_tlb_mm(struct mm_struct *mm)
162 pid = mm->context.id;
163 if (pid != MMU_NO_CONTEXT)
167 EXPORT_SYMBOL(local_flush_tlb_mm);
169 void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
175 pid = mm ? mm->context.id : 0;
176 if (pid != MMU_NO_CONTEXT)
177 _tlbil_va(vmaddr, pid, tsize, ind);
181 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
183 __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
184 mmu_get_tsize(mmu_virtual_psize), 0);
186 EXPORT_SYMBOL(local_flush_tlb_page);
188 void local_flush_tlb_page_psize(struct mm_struct *mm,
189 unsigned long vmaddr, int psize)
191 __local_flush_tlb_page(mm, vmaddr, mmu_get_tsize(psize), 0);
193 EXPORT_SYMBOL(local_flush_tlb_page_psize);
198 * And here are the SMP non-local implementations
202 static DEFINE_RAW_SPINLOCK(tlbivax_lock);
204 struct tlb_flush_param {
211 static void do_flush_tlb_mm_ipi(void *param)
213 struct tlb_flush_param *p = param;
215 _tlbil_pid(p ? p->pid : 0);
218 static void do_flush_tlb_page_ipi(void *param)
220 struct tlb_flush_param *p = param;
222 _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
226 /* Note on invalidations and PID:
228 * We snapshot the PID with preempt disabled. At this point, it can still
229 * change either because:
230 * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
231 * - we are invaliating some target that isn't currently running here
232 * and is concurrently acquiring a new PID on another CPU
233 * - some other CPU is re-acquiring a lost PID for this mm
236 * However, this shouldn't be a problem as we only guarantee
237 * invalidation of TLB entries present prior to this call, so we
238 * don't care about the PID changing, and invalidating a stale PID
239 * is generally harmless.
242 void flush_tlb_mm(struct mm_struct *mm)
247 pid = mm->context.id;
248 if (unlikely(pid == MMU_NO_CONTEXT))
250 if (!mm_is_core_local(mm)) {
251 struct tlb_flush_param p = { .pid = pid };
252 /* Ignores smp_processor_id() even if set. */
253 smp_call_function_many(mm_cpumask(mm),
254 do_flush_tlb_mm_ipi, &p, 1);
260 EXPORT_SYMBOL(flush_tlb_mm);
262 void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
265 struct cpumask *cpu_mask;
269 * This function as well as __local_flush_tlb_page() must only be called
276 pid = mm->context.id;
277 if (unlikely(pid == MMU_NO_CONTEXT))
279 cpu_mask = mm_cpumask(mm);
280 if (!mm_is_core_local(mm)) {
281 /* If broadcast tlbivax is supported, use it */
282 if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
283 int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
285 raw_spin_lock(&tlbivax_lock);
286 _tlbivax_bcast(vmaddr, pid, tsize, ind);
288 raw_spin_unlock(&tlbivax_lock);
291 struct tlb_flush_param p = {
297 /* Ignores smp_processor_id() even if set in cpu_mask */
298 smp_call_function_many(cpu_mask,
299 do_flush_tlb_page_ipi, &p, 1);
302 _tlbil_va(vmaddr, pid, tsize, ind);
307 void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
309 #ifdef CONFIG_HUGETLB_PAGE
310 if (vma && is_vm_hugetlb_page(vma))
311 flush_hugetlb_page(vma, vmaddr);
314 __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
315 mmu_get_tsize(mmu_virtual_psize), 0);
317 EXPORT_SYMBOL(flush_tlb_page);
319 #endif /* CONFIG_SMP */
321 #ifdef CONFIG_PPC_47x
322 void __init early_init_mmu_47x(void)
325 unsigned long root = of_get_flat_dt_root();
326 if (of_get_flat_dt_prop(root, "cooperative-partition", NULL))
327 mmu_clear_feature(MMU_FTR_USE_TLBIVAX_BCAST);
328 #endif /* CONFIG_SMP */
330 #endif /* CONFIG_PPC_47x */
333 * Flush kernel TLB entries in the given range
335 #ifndef CONFIG_PPC_8xx
336 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
340 smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
347 EXPORT_SYMBOL(flush_tlb_kernel_range);
351 * Currently, for range flushing, we just do a full mm flush. This should
352 * be optimized based on a threshold on the size of the range, since
353 * some implementation can stack multiple tlbivax before a tlbsync but
354 * for now, we keep it that way
356 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
360 if (end - start == PAGE_SIZE && !(start & ~PAGE_MASK))
361 flush_tlb_page(vma, start);
363 flush_tlb_mm(vma->vm_mm);
365 EXPORT_SYMBOL(flush_tlb_range);
367 void tlb_flush(struct mmu_gather *tlb)
369 flush_tlb_mm(tlb->mm);
373 * Below are functions specific to the 64-bit variant of Book3E though that
374 * may change in the future
380 * Handling of virtual linear page tables or indirect TLB entries
381 * flushing when PTE pages are freed
383 void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
385 int tsize = mmu_psize_defs[mmu_pte_psize].enc;
387 if (book3e_htw_mode != PPC_HTW_NONE) {
388 unsigned long start = address & PMD_MASK;
389 unsigned long end = address + PMD_SIZE;
390 unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
392 /* This isn't the most optimal, ideally we would factor out the
393 * while preempt & CPU mask mucking around, or even the IPI but
396 while (start < end) {
397 __flush_tlb_page(tlb->mm, start, tsize, 1);
401 unsigned long rmask = 0xf000000000000000ul;
402 unsigned long rid = (address & rmask) | 0x1000000000000000ul;
403 unsigned long vpte = address & ~rmask;
405 vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
407 __flush_tlb_page(tlb->mm, vpte, tsize, 0);
411 static void __init setup_page_sizes(void)
413 unsigned int tlb0cfg;
418 #ifdef CONFIG_PPC_E500
419 unsigned int mmucfg = mfspr(SPRN_MMUCFG);
420 int fsl_mmu = mmu_has_feature(MMU_FTR_TYPE_FSL_E);
422 if (fsl_mmu && (mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
423 unsigned int tlb1cfg = mfspr(SPRN_TLB1CFG);
424 unsigned int min_pg, max_pg;
426 min_pg = (tlb1cfg & TLBnCFG_MINSIZE) >> TLBnCFG_MINSIZE_SHIFT;
427 max_pg = (tlb1cfg & TLBnCFG_MAXSIZE) >> TLBnCFG_MAXSIZE_SHIFT;
429 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
430 struct mmu_psize_def *def;
433 def = &mmu_psize_defs[psize];
436 if (shift == 0 || shift & 1)
439 /* adjust to be in terms of 4^shift Kb */
440 shift = (shift - 10) >> 1;
442 if ((shift >= min_pg) && (shift <= max_pg))
443 def->flags |= MMU_PAGE_SIZE_DIRECT;
449 if (fsl_mmu && (mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V2) {
452 tlb0cfg = mfspr(SPRN_TLB0CFG);
453 tlb1cfg = mfspr(SPRN_TLB1CFG);
454 tlb1ps = mfspr(SPRN_TLB1PS);
455 eptcfg = mfspr(SPRN_EPTCFG);
457 if ((tlb1cfg & TLBnCFG_IND) && (tlb0cfg & TLBnCFG_PT))
458 book3e_htw_mode = PPC_HTW_E6500;
461 * We expect 4K subpage size and unrestricted indirect size.
462 * The lack of a restriction on indirect size is a Freescale
463 * extension, indicated by PSn = 0 but SPSn != 0.
466 book3e_htw_mode = PPC_HTW_NONE;
468 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
469 struct mmu_psize_def *def = &mmu_psize_defs[psize];
474 if (tlb1ps & (1U << (def->shift - 10))) {
475 def->flags |= MMU_PAGE_SIZE_DIRECT;
477 if (book3e_htw_mode && psize == MMU_PAGE_2M)
478 def->flags |= MMU_PAGE_SIZE_INDIRECT;
486 tlb0cfg = mfspr(SPRN_TLB0CFG);
487 tlb0ps = mfspr(SPRN_TLB0PS);
488 eptcfg = mfspr(SPRN_EPTCFG);
490 /* Look for supported direct sizes */
491 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
492 struct mmu_psize_def *def = &mmu_psize_defs[psize];
494 if (tlb0ps & (1U << (def->shift - 10)))
495 def->flags |= MMU_PAGE_SIZE_DIRECT;
498 /* Indirect page sizes supported ? */
499 if ((tlb0cfg & TLBnCFG_IND) == 0 ||
500 (tlb0cfg & TLBnCFG_PT) == 0)
503 book3e_htw_mode = PPC_HTW_IBM;
505 /* Now, we only deal with one IND page size for each
506 * direct size. Hopefully all implementations today are
507 * unambiguous, but we might want to be careful in the
510 for (i = 0; i < 3; i++) {
511 unsigned int ps, sps;
519 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
520 struct mmu_psize_def *def = &mmu_psize_defs[psize];
522 if (ps == (def->shift - 10))
523 def->flags |= MMU_PAGE_SIZE_INDIRECT;
524 if (sps == (def->shift - 10))
530 /* Cleanup array and print summary */
531 pr_info("MMU: Supported page sizes\n");
532 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
533 struct mmu_psize_def *def = &mmu_psize_defs[psize];
534 const char *__page_type_names[] = {
540 if (def->flags == 0) {
544 pr_info(" %8ld KB as %s\n", 1ul << (def->shift - 10),
545 __page_type_names[def->flags & 0x3]);
549 static void __init setup_mmu_htw(void)
552 * If we want to use HW tablewalk, enable it by patching the TLB miss
553 * handlers to branch to the one dedicated to it.
556 switch (book3e_htw_mode) {
558 patch_exception(0x1c0, exc_data_tlb_miss_htw_book3e);
559 patch_exception(0x1e0, exc_instruction_tlb_miss_htw_book3e);
561 #ifdef CONFIG_PPC_E500
563 extlb_level_exc = EX_TLB_SIZE;
564 patch_exception(0x1c0, exc_data_tlb_miss_e6500_book3e);
565 patch_exception(0x1e0, exc_instruction_tlb_miss_e6500_book3e);
569 pr_info("MMU: Book3E HW tablewalk %s\n",
570 book3e_htw_mode != PPC_HTW_NONE ? "enabled" : "not supported");
574 * Early initialization of the MMU TLB code
576 static void early_init_this_mmu(void)
580 /* Set MAS4 based on page table setting */
582 mas4 = 0x4 << MAS4_WIMGED_SHIFT;
583 switch (book3e_htw_mode) {
586 mas4 |= BOOK3E_PAGESZ_2M << MAS4_TSIZED_SHIFT;
587 mas4 |= MAS4_TLBSELD(1);
588 mmu_pte_psize = MMU_PAGE_2M;
593 mas4 |= BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
594 mmu_pte_psize = MMU_PAGE_1M;
598 mas4 |= BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
599 mmu_pte_psize = mmu_virtual_psize;
602 mtspr(SPRN_MAS4, mas4);
604 #ifdef CONFIG_PPC_E500
605 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
606 unsigned int num_cams;
609 /* use a quarter of the TLBCAM for bolted linear map */
610 num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
613 * Only do the mapping once per core, or else the
614 * transient mapping would cause problems.
617 if (hweight32(get_tensr()) > 1)
622 linear_map_top = map_mem_in_cams(linear_map_top,
623 num_cams, false, true);
627 /* A sync won't hurt us after mucking around with
628 * the MMU configuration
633 static void __init early_init_mmu_global(void)
635 /* XXX This should be decided at runtime based on supported
636 * page sizes in the TLB, but for now let's assume 16M is
637 * always there and a good fit (which it probably is)
639 * Freescale booke only supports 4K pages in TLB0, so use that.
641 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E))
642 mmu_vmemmap_psize = MMU_PAGE_4K;
644 mmu_vmemmap_psize = MMU_PAGE_16M;
646 /* XXX This code only checks for TLB 0 capabilities and doesn't
647 * check what page size combos are supported by the HW. It
648 * also doesn't handle the case where a separate array holds
649 * the IND entries from the array loaded by the PT.
651 /* Look for supported page sizes */
654 /* Look for HW tablewalk support */
657 #ifdef CONFIG_PPC_E500
658 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
659 if (book3e_htw_mode == PPC_HTW_NONE) {
660 extlb_level_exc = EX_TLB_SIZE;
661 patch_exception(0x1c0, exc_data_tlb_miss_bolted_book3e);
662 patch_exception(0x1e0,
663 exc_instruction_tlb_miss_bolted_book3e);
668 /* Set the global containing the top of the linear mapping
669 * for use by the TLB miss code
671 linear_map_top = memblock_end_of_DRAM();
673 ioremap_bot = IOREMAP_BASE;
676 static void __init early_mmu_set_memory_limit(void)
678 #ifdef CONFIG_PPC_E500
679 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
681 * Limit memory so we dont have linear faults.
682 * Unlike memblock_set_current_limit, which limits
683 * memory available during early boot, this permanently
684 * reduces the memory available to Linux. We need to
685 * do this because highmem is not supported on 64-bit.
687 memblock_enforce_memory_limit(linear_map_top);
691 memblock_set_current_limit(linear_map_top);
695 void __init early_init_mmu(void)
697 early_init_mmu_global();
698 early_init_this_mmu();
699 early_mmu_set_memory_limit();
702 void early_init_mmu_secondary(void)
704 early_init_this_mmu();
707 void setup_initial_memory_limit(phys_addr_t first_memblock_base,
708 phys_addr_t first_memblock_size)
710 /* On non-FSL Embedded 64-bit, we adjust the RMA size to match
711 * the bolted TLB entry. We know for now that only 1G
712 * entries are supported though that may eventually
715 * on FSL Embedded 64-bit, usually all RAM is bolted, but with
716 * unusual memory sizes it's possible for some RAM to not be mapped
717 * (such RAM is not used at all by Linux, since we don't support
718 * highmem on 64-bit). We limit ppc64_rma_size to what would be
719 * mappable if this memblock is the only one. Additional memblocks
720 * can only increase, not decrease, the amount that ends up getting
721 * mapped. We still limit max to 1G even if we'll eventually map
722 * more. This is due to what the early init code is set up to do.
724 * We crop it to the size of the first MEMBLOCK to
725 * avoid going over total available memory just in case...
727 #ifdef CONFIG_PPC_E500
728 if (early_mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
729 unsigned long linear_sz;
730 unsigned int num_cams;
732 /* use a quarter of the TLBCAM for bolted linear map */
733 num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
735 linear_sz = map_mem_in_cams(first_memblock_size, num_cams,
738 ppc64_rma_size = min_t(u64, linear_sz, 0x40000000);
741 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
743 /* Finally limit subsequent allocations */
744 memblock_set_current_limit(first_memblock_base + ppc64_rma_size);
746 #else /* ! CONFIG_PPC64 */
747 void __init early_init_mmu(void)
749 #ifdef CONFIG_PPC_47x
750 early_init_mmu_47x();
753 #endif /* CONFIG_PPC64 */