109cfefb7SHuacai Chen // SPDX-License-Identifier: GPL-2.0 209cfefb7SHuacai Chen /* 309cfefb7SHuacai Chen * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 409cfefb7SHuacai Chen */ 509cfefb7SHuacai Chen #include <linux/init.h> 609cfefb7SHuacai Chen #include <linux/export.h> 709cfefb7SHuacai Chen #include <linux/mm.h> 809cfefb7SHuacai Chen #include <asm/pgalloc.h> 909cfefb7SHuacai Chen #include <asm/pgtable.h> 1009cfefb7SHuacai Chen #include <asm/tlbflush.h> 1109cfefb7SHuacai Chen 1209cfefb7SHuacai Chen pgd_t *pgd_alloc(struct mm_struct *mm) 1309cfefb7SHuacai Chen { 1438273979SVishal Moola (Oracle) pgd_t *init, *ret = NULL; 1538273979SVishal Moola (Oracle) struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0); 1609cfefb7SHuacai Chen 1738273979SVishal Moola (Oracle) if (ptdesc) { 1838273979SVishal Moola (Oracle) ret = (pgd_t *)ptdesc_address(ptdesc); 1909cfefb7SHuacai Chen init = pgd_offset(&init_mm, 0UL); 2022c4e804SFeiyang Chen pgd_init(ret); 2109cfefb7SHuacai Chen memcpy(ret + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD, 2209cfefb7SHuacai Chen (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); 2309cfefb7SHuacai Chen } 2409cfefb7SHuacai Chen 2509cfefb7SHuacai Chen return ret; 2609cfefb7SHuacai Chen } 2709cfefb7SHuacai Chen EXPORT_SYMBOL_GPL(pgd_alloc); 2809cfefb7SHuacai Chen 2922c4e804SFeiyang Chen void pgd_init(void *addr) 3009cfefb7SHuacai Chen { 3109cfefb7SHuacai Chen unsigned long *p, *end; 3209cfefb7SHuacai Chen unsigned long entry; 3309cfefb7SHuacai Chen 3409cfefb7SHuacai Chen #if !defined(__PAGETABLE_PUD_FOLDED) 3509cfefb7SHuacai Chen entry = (unsigned long)invalid_pud_table; 3609cfefb7SHuacai Chen #elif !defined(__PAGETABLE_PMD_FOLDED) 3709cfefb7SHuacai Chen entry = (unsigned long)invalid_pmd_table; 3809cfefb7SHuacai Chen #else 3909cfefb7SHuacai Chen entry = (unsigned long)invalid_pte_table; 4009cfefb7SHuacai Chen #endif 4109cfefb7SHuacai Chen 4222c4e804SFeiyang Chen p = (unsigned long *)addr; 4309cfefb7SHuacai Chen end = p + PTRS_PER_PGD; 4409cfefb7SHuacai Chen 4509cfefb7SHuacai Chen do { 4609cfefb7SHuacai Chen p[0] = entry; 4709cfefb7SHuacai Chen p[1] = entry; 4809cfefb7SHuacai Chen p[2] = entry; 4909cfefb7SHuacai Chen p[3] = entry; 5009cfefb7SHuacai Chen p[4] = entry; 5109cfefb7SHuacai Chen p += 8; 5209cfefb7SHuacai Chen p[-3] = entry; 5309cfefb7SHuacai Chen p[-2] = entry; 5409cfefb7SHuacai Chen p[-1] = entry; 5509cfefb7SHuacai Chen } while (p != end); 5609cfefb7SHuacai Chen } 5709cfefb7SHuacai Chen EXPORT_SYMBOL_GPL(pgd_init); 5809cfefb7SHuacai Chen 5909cfefb7SHuacai Chen #ifndef __PAGETABLE_PMD_FOLDED 6022c4e804SFeiyang Chen void pmd_init(void *addr) 6109cfefb7SHuacai Chen { 6209cfefb7SHuacai Chen unsigned long *p, *end; 6322c4e804SFeiyang Chen unsigned long pagetable = (unsigned long)invalid_pte_table; 6409cfefb7SHuacai Chen 6509cfefb7SHuacai Chen p = (unsigned long *)addr; 6609cfefb7SHuacai Chen end = p + PTRS_PER_PMD; 6709cfefb7SHuacai Chen 6809cfefb7SHuacai Chen do { 6909cfefb7SHuacai Chen p[0] = pagetable; 7009cfefb7SHuacai Chen p[1] = pagetable; 7109cfefb7SHuacai Chen p[2] = pagetable; 7209cfefb7SHuacai Chen p[3] = pagetable; 7309cfefb7SHuacai Chen p[4] = pagetable; 7409cfefb7SHuacai Chen p += 8; 7509cfefb7SHuacai Chen p[-3] = pagetable; 7609cfefb7SHuacai Chen p[-2] = pagetable; 7709cfefb7SHuacai Chen p[-1] = pagetable; 7809cfefb7SHuacai Chen } while (p != end); 7909cfefb7SHuacai Chen } 8009cfefb7SHuacai Chen EXPORT_SYMBOL_GPL(pmd_init); 8109cfefb7SHuacai Chen #endif 8209cfefb7SHuacai Chen 8309cfefb7SHuacai Chen #ifndef __PAGETABLE_PUD_FOLDED 8422c4e804SFeiyang Chen void pud_init(void *addr) 8509cfefb7SHuacai Chen { 8609cfefb7SHuacai Chen unsigned long *p, *end; 8722c4e804SFeiyang Chen unsigned long pagetable = (unsigned long)invalid_pmd_table; 8809cfefb7SHuacai Chen 8909cfefb7SHuacai Chen p = (unsigned long *)addr; 9009cfefb7SHuacai Chen end = p + PTRS_PER_PUD; 9109cfefb7SHuacai Chen 9209cfefb7SHuacai Chen do { 9309cfefb7SHuacai Chen p[0] = pagetable; 9409cfefb7SHuacai Chen p[1] = pagetable; 9509cfefb7SHuacai Chen p[2] = pagetable; 9609cfefb7SHuacai Chen p[3] = pagetable; 9709cfefb7SHuacai Chen p[4] = pagetable; 9809cfefb7SHuacai Chen p += 8; 9909cfefb7SHuacai Chen p[-3] = pagetable; 10009cfefb7SHuacai Chen p[-2] = pagetable; 10109cfefb7SHuacai Chen p[-1] = pagetable; 10209cfefb7SHuacai Chen } while (p != end); 10309cfefb7SHuacai Chen } 10422c4e804SFeiyang Chen EXPORT_SYMBOL_GPL(pud_init); 10509cfefb7SHuacai Chen #endif 10609cfefb7SHuacai Chen 10709cfefb7SHuacai Chen pmd_t mk_pmd(struct page *page, pgprot_t prot) 10809cfefb7SHuacai Chen { 10909cfefb7SHuacai Chen pmd_t pmd; 11009cfefb7SHuacai Chen 111*a6d01af0SMatthew Wilcox (Oracle) pmd_val(pmd) = (page_to_pfn(page) << PFN_PTE_SHIFT) | pgprot_val(prot); 11209cfefb7SHuacai Chen 11309cfefb7SHuacai Chen return pmd; 11409cfefb7SHuacai Chen } 11509cfefb7SHuacai Chen 11609cfefb7SHuacai Chen void set_pmd_at(struct mm_struct *mm, unsigned long addr, 11709cfefb7SHuacai Chen pmd_t *pmdp, pmd_t pmd) 11809cfefb7SHuacai Chen { 11909cfefb7SHuacai Chen *pmdp = pmd; 12009cfefb7SHuacai Chen flush_tlb_all(); 12109cfefb7SHuacai Chen } 12209cfefb7SHuacai Chen 12309cfefb7SHuacai Chen void __init pagetable_init(void) 12409cfefb7SHuacai Chen { 12509cfefb7SHuacai Chen /* Initialize the entire pgd. */ 12622c4e804SFeiyang Chen pgd_init(swapper_pg_dir); 12722c4e804SFeiyang Chen pgd_init(invalid_pg_dir); 12809cfefb7SHuacai Chen #ifndef __PAGETABLE_PUD_FOLDED 12922c4e804SFeiyang Chen pud_init(invalid_pud_table); 13009cfefb7SHuacai Chen #endif 13109cfefb7SHuacai Chen #ifndef __PAGETABLE_PMD_FOLDED 13222c4e804SFeiyang Chen pmd_init(invalid_pmd_table); 13309cfefb7SHuacai Chen #endif 13409cfefb7SHuacai Chen } 135