Skip to content

Commit 840125a

Browse files
AlexGhitipalmer-dabbelt
authored andcommitted
riscv: Introduce functions to switch pt_ops
This simply gathers the different pt_ops initialization in functions where a comment was added to explain why the page table operations must be changed along the boot process. Signed-off-by: Alexandre Ghiti <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 2efad17 commit 840125a

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

arch/riscv/mm/init.c

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,52 @@ static void __init create_fdt_early_page_table(pgd_t *pgdir, uintptr_t dtb_pa)
581581
dtb_early_pa = dtb_pa;
582582
}
583583

584+
/*
585+
* MMU is not enabled, the page tables are allocated directly using
586+
* early_pmd/pud/p4d and the address returned is the physical one.
587+
*/
588+
void pt_ops_set_early(void)
589+
{
590+
pt_ops.alloc_pte = alloc_pte_early;
591+
pt_ops.get_pte_virt = get_pte_virt_early;
592+
#ifndef __PAGETABLE_PMD_FOLDED
593+
pt_ops.alloc_pmd = alloc_pmd_early;
594+
pt_ops.get_pmd_virt = get_pmd_virt_early;
595+
#endif
596+
}
597+
598+
/*
599+
* MMU is enabled but page table setup is not complete yet.
600+
* fixmap page table alloc functions must be used as a means to temporarily
601+
* map the allocated physical pages since the linear mapping does not exist yet.
602+
*
603+
* Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va,
604+
* but it will be used as described above.
605+
*/
606+
void pt_ops_set_fixmap(void)
607+
{
608+
pt_ops.alloc_pte = kernel_mapping_pa_to_va((uintptr_t)alloc_pte_fixmap);
609+
pt_ops.get_pte_virt = kernel_mapping_pa_to_va((uintptr_t)get_pte_virt_fixmap);
610+
#ifndef __PAGETABLE_PMD_FOLDED
611+
pt_ops.alloc_pmd = kernel_mapping_pa_to_va((uintptr_t)alloc_pmd_fixmap);
612+
pt_ops.get_pmd_virt = kernel_mapping_pa_to_va((uintptr_t)get_pmd_virt_fixmap);
613+
#endif
614+
}
615+
616+
/*
617+
* MMU is enabled and page table setup is complete, so from now, we can use
618+
* generic page allocation functions to setup page table.
619+
*/
620+
void pt_ops_set_late(void)
621+
{
622+
pt_ops.alloc_pte = alloc_pte_late;
623+
pt_ops.get_pte_virt = get_pte_virt_late;
624+
#ifndef __PAGETABLE_PMD_FOLDED
625+
pt_ops.alloc_pmd = alloc_pmd_late;
626+
pt_ops.get_pmd_virt = get_pmd_virt_late;
627+
#endif
628+
}
629+
584630
asmlinkage void __init setup_vm(uintptr_t dtb_pa)
585631
{
586632
pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd;
@@ -625,12 +671,8 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
625671
BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K);
626672
#endif
627673

628-
pt_ops.alloc_pte = alloc_pte_early;
629-
pt_ops.get_pte_virt = get_pte_virt_early;
630-
#ifndef __PAGETABLE_PMD_FOLDED
631-
pt_ops.alloc_pmd = alloc_pmd_early;
632-
pt_ops.get_pmd_virt = get_pmd_virt_early;
633-
#endif
674+
pt_ops_set_early();
675+
634676
/* Setup early PGD for fixmap */
635677
create_pgd_mapping(early_pg_dir, FIXADDR_START,
636678
(uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
@@ -694,6 +736,8 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
694736
pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
695737
}
696738
#endif
739+
740+
pt_ops_set_fixmap();
697741
}
698742

699743
static void __init setup_vm_final(void)
@@ -702,16 +746,6 @@ static void __init setup_vm_final(void)
702746
phys_addr_t pa, start, end;
703747
u64 i;
704748

705-
/**
706-
* MMU is enabled at this point. But page table setup is not complete yet.
707-
* fixmap page table alloc functions should be used at this point
708-
*/
709-
pt_ops.alloc_pte = alloc_pte_fixmap;
710-
pt_ops.get_pte_virt = get_pte_virt_fixmap;
711-
#ifndef __PAGETABLE_PMD_FOLDED
712-
pt_ops.alloc_pmd = alloc_pmd_fixmap;
713-
pt_ops.get_pmd_virt = get_pmd_virt_fixmap;
714-
#endif
715749
/* Setup swapper PGD for fixmap */
716750
create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
717751
__pa_symbol(fixmap_pgd_next),
@@ -753,13 +787,7 @@ static void __init setup_vm_final(void)
753787
csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
754788
local_flush_tlb_all();
755789

756-
/* generic page allocation functions must be used to setup page table */
757-
pt_ops.alloc_pte = alloc_pte_late;
758-
pt_ops.get_pte_virt = get_pte_virt_late;
759-
#ifndef __PAGETABLE_PMD_FOLDED
760-
pt_ops.alloc_pmd = alloc_pmd_late;
761-
pt_ops.get_pmd_virt = get_pmd_virt_late;
762-
#endif
790+
pt_ops_set_late();
763791
}
764792
#else
765793
asmlinkage void __init setup_vm(uintptr_t dtb_pa)

0 commit comments

Comments
 (0)