1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
8 #include <linux/hugetlb.h>
9 #include <linux/pagemap.h>
10 #include <linux/err.h>
11 #include <linux/sysctl.h>
14 #include <asm/tlbflush.h>
16 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
17 unsigned long addr, unsigned long sz)
24 pgd = pgd_offset(mm, addr);
25 p4d = p4d_alloc(mm, pgd, addr);
26 pud = pud_alloc(mm, p4d, addr);
28 pte = (pte_t *)pmd_alloc(mm, pud, addr);
33 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr,
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);
53 int pmd_huge(pmd_t pmd)
55 return (pmd_val(pmd) & _PAGE_HUGE) != 0;
58 int pud_huge(pud_t pud)
60 return (pud_val(pud) & _PAGE_HUGE) != 0;
63 uint64_t pmd_to_entrylo(unsigned long pmd_val)
66 /* PMD as PTE. Must be huge page */
67 if (!pmd_huge(__pmd(pmd_val)))
68 panic("%s", __func__);
70 val = pmd_val ^ _PAGE_HUGE;
71 val |= ((val & _PAGE_HGLOBAL) >>
72 (_PAGE_HGLOBAL_SHIFT - _PAGE_GLOBAL_SHIFT));