1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2009 Chen Liqin <liqin.chen@sunplusct.com> 4 * Copyright (C) 2012 Regents of the University of California 5 */ 6 7 #ifndef _ASM_RISCV_PGALLOC_H 8 #define _ASM_RISCV_PGALLOC_H 9 10 #include <linux/mm.h> 11 #include <asm/tlb.h> 12 13 static inline void pmd_populate_kernel(struct mm_struct *mm, 14 pmd_t *pmd, pte_t *pte) 15 { 16 unsigned long pfn = virt_to_pfn(pte); 17 18 set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); 19 } 20 21 static inline void pmd_populate(struct mm_struct *mm, 22 pmd_t *pmd, pgtable_t pte) 23 { 24 unsigned long pfn = virt_to_pfn(page_address(pte)); 25 26 set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); 27 } 28 29 #ifndef __PAGETABLE_PMD_FOLDED 30 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) 31 { 32 unsigned long pfn = virt_to_pfn(pmd); 33 34 set_pud(pud, __pud((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); 35 } 36 #endif /* __PAGETABLE_PMD_FOLDED */ 37 38 #define pmd_pgtable(pmd) pmd_page(pmd) 39 40 static inline pgd_t *pgd_alloc(struct mm_struct *mm) 41 { 42 pgd_t *pgd; 43 44 pgd = (pgd_t *)__get_free_page(GFP_KERNEL); 45 if (likely(pgd != NULL)) { 46 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); 47 /* Copy kernel mappings */ 48 memcpy(pgd + USER_PTRS_PER_PGD, 49 init_mm.pgd + USER_PTRS_PER_PGD, 50 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); 51 } 52 return pgd; 53 } 54 55 static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd) 56 { 57 free_page((unsigned long)pgd); 58 } 59 60 #ifndef __PAGETABLE_PMD_FOLDED 61 62 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr) 63 { 64 return (pmd_t *)__get_free_page( 65 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_ZERO); 66 } 67 68 static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd) 69 { 70 free_page((unsigned long)pmd); 71 } 72 73 #define __pmd_free_tlb(tlb, pmd, addr) pmd_free((tlb)->mm, pmd) 74 75 #endif /* __PAGETABLE_PMD_FOLDED */ 76 77 static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm) 78 { 79 return (pte_t *)__get_free_page( 80 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_ZERO); 81 } 82 83 static inline struct page *pte_alloc_one(struct mm_struct *mm) 84 { 85 struct page *pte; 86 87 pte = alloc_page(GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_ZERO); 88 if (likely(pte != NULL)) 89 pgtable_page_ctor(pte); 90 return pte; 91 } 92 93 static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte) 94 { 95 free_page((unsigned long)pte); 96 } 97 98 static inline void pte_free(struct mm_struct *mm, pgtable_t pte) 99 { 100 pgtable_page_dtor(pte); 101 __free_page(pte); 102 } 103 104 #define __pte_free_tlb(tlb, pte, buf) \ 105 do { \ 106 pgtable_page_dtor(pte); \ 107 tlb_remove_page((tlb), pte); \ 108 } while (0) 109 110 static inline void check_pgt_cache(void) 111 { 112 } 113 114 #endif /* _ASM_RISCV_PGALLOC_H */ 115