1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
5 #include <linux/init.h>
6 #include <linux/export.h>
8 #include <asm/pgalloc.h>
9 #include <asm/pgtable.h>
10 #include <asm/tlbflush.h>
12 pgd_t *pgd_alloc(struct mm_struct *mm)
14 pgd_t *init, *ret = NULL;
15 struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
18 ret = (pgd_t *)ptdesc_address(ptdesc);
19 init = pgd_offset(&init_mm, 0UL);
21 memcpy(ret + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD,
22 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
27 EXPORT_SYMBOL_GPL(pgd_alloc);
29 void pgd_init(void *addr)
31 unsigned long *p, *end;
34 #if !defined(__PAGETABLE_PUD_FOLDED)
35 entry = (unsigned long)invalid_pud_table;
36 #elif !defined(__PAGETABLE_PMD_FOLDED)
37 entry = (unsigned long)invalid_pmd_table;
39 entry = (unsigned long)invalid_pte_table;
42 p = (unsigned long *)addr;
43 end = p + PTRS_PER_PGD;
57 EXPORT_SYMBOL_GPL(pgd_init);
59 #ifndef __PAGETABLE_PMD_FOLDED
60 void pmd_init(void *addr)
62 unsigned long *p, *end;
63 unsigned long pagetable = (unsigned long)invalid_pte_table;
65 p = (unsigned long *)addr;
66 end = p + PTRS_PER_PMD;
80 EXPORT_SYMBOL_GPL(pmd_init);
83 #ifndef __PAGETABLE_PUD_FOLDED
84 void pud_init(void *addr)
86 unsigned long *p, *end;
87 unsigned long pagetable = (unsigned long)invalid_pmd_table;
89 p = (unsigned long *)addr;
90 end = p + PTRS_PER_PUD;
104 EXPORT_SYMBOL_GPL(pud_init);
107 pmd_t mk_pmd(struct page *page, pgprot_t prot)
111 pmd_val(pmd) = (page_to_pfn(page) << PFN_PTE_SHIFT) | pgprot_val(prot);
116 void set_pmd_at(struct mm_struct *mm, unsigned long addr,
117 pmd_t *pmdp, pmd_t pmd)
123 void __init pagetable_init(void)
125 /* Initialize the entire pgd. */
126 pgd_init(swapper_pg_dir);
127 pgd_init(invalid_pg_dir);
128 #ifndef __PAGETABLE_PUD_FOLDED
129 pud_init(invalid_pud_table);
131 #ifndef __PAGETABLE_PMD_FOLDED
132 pmd_init(invalid_pmd_table);