1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 4 */ 5 6 #include <linux/fs.h> 7 #include <linux/mm.h> 8 #include <linux/hugetlb.h> 9 #include <linux/pagemap.h> 10 #include <linux/err.h> 11 #include <linux/sysctl.h> 12 #include <asm/mman.h> 13 #include <asm/tlb.h> 14 #include <asm/tlbflush.h> 15 16 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 17 unsigned long addr, unsigned long sz) 18 { 19 pgd_t *pgd; 20 p4d_t *p4d; 21 pud_t *pud; 22 pte_t *pte = NULL; 23 24 pgd = pgd_offset(mm, addr); 25 p4d = p4d_alloc(mm, pgd, addr); 26 pud = pud_alloc(mm, p4d, addr); 27 if (pud) 28 pte = (pte_t *)pmd_alloc(mm, pud, addr); 29 30 return pte; 31 } 32 33 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, 34 unsigned long sz) 35 { 36 pgd_t *pgd; 37 p4d_t *p4d; 38 pud_t *pud; 39 pmd_t *pmd = NULL; 40 41 pgd = pgd_offset(mm, addr); 42 if (pgd_present(*pgd)) { 43 p4d = p4d_offset(pgd, addr); 44 if (p4d_present(*p4d)) { 45 pud = pud_offset(p4d, addr); 46 if (pud_present(*pud)) 47 pmd = pmd_offset(pud, addr); 48 } 49 } 50 return (pte_t *) pmd; 51 } 52 53 uint64_t pmd_to_entrylo(unsigned long pmd_val) 54 { 55 uint64_t val; 56 /* PMD as PTE. Must be huge page */ 57 if (!pmd_leaf(__pmd(pmd_val))) 58 panic("%s", __func__); 59 60 val = pmd_val ^ _PAGE_HUGE; 61 val |= ((val & _PAGE_HGLOBAL) >> 62 (_PAGE_HGLOBAL_SHIFT - _PAGE_GLOBAL_SHIFT)); 63 64 return val; 65 } 66