1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2009 Chen Liqin <liqin.chen@sunplusct.com>
4 * Copyright (C) 2012 Regents of the University of California
7 #ifndef _ASM_RISCV_PGALLOC_H
8 #define _ASM_RISCV_PGALLOC_H
14 #include <asm-generic/pgalloc.h>
16 static inline void pmd_populate_kernel(struct mm_struct *mm,
17 pmd_t *pmd, pte_t *pte)
19 unsigned long pfn = virt_to_pfn(pte);
21 set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
24 static inline void pmd_populate(struct mm_struct *mm,
25 pmd_t *pmd, pgtable_t pte)
27 unsigned long pfn = virt_to_pfn(page_address(pte));
29 set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
32 #ifndef __PAGETABLE_PMD_FOLDED
33 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
35 unsigned long pfn = virt_to_pfn(pmd);
37 set_pud(pud, __pud((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
39 #endif /* __PAGETABLE_PMD_FOLDED */
41 static inline pgd_t *pgd_alloc(struct mm_struct *mm)
45 pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
46 if (likely(pgd != NULL)) {
47 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
48 /* Copy kernel mappings */
49 memcpy(pgd + USER_PTRS_PER_PGD,
50 init_mm.pgd + USER_PTRS_PER_PGD,
51 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
56 #ifndef __PAGETABLE_PMD_FOLDED
58 #define __pmd_free_tlb(tlb, pmd, addr) pmd_free((tlb)->mm, pmd)
60 #endif /* __PAGETABLE_PMD_FOLDED */
62 #define __pte_free_tlb(tlb, pte, buf) \
64 pgtable_pte_page_dtor(pte); \
65 tlb_remove_page((tlb), pte); \
67 #endif /* CONFIG_MMU */
69 #endif /* _ASM_RISCV_PGALLOC_H */