David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2009 Chen Liqin <liqin.chen@sunplusct.com> |
| 4 | * Copyright (C) 2012 Regents of the University of California |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 13 | #ifdef CONFIG_MMU |
| 14 | #include <asm-generic/pgalloc.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 15 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 16 | static inline void pmd_populate_kernel(struct mm_struct *mm, |
| 17 | pmd_t *pmd, pte_t *pte) |
| 18 | { |
| 19 | unsigned long pfn = virt_to_pfn(pte); |
| 20 | |
| 21 | set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); |
| 22 | } |
| 23 | |
| 24 | static inline void pmd_populate(struct mm_struct *mm, |
| 25 | pmd_t *pmd, pgtable_t pte) |
| 26 | { |
| 27 | unsigned long pfn = virt_to_pfn(page_address(pte)); |
| 28 | |
| 29 | set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); |
| 30 | } |
| 31 | |
| 32 | #ifndef __PAGETABLE_PMD_FOLDED |
| 33 | static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) |
| 34 | { |
| 35 | unsigned long pfn = virt_to_pfn(pmd); |
| 36 | |
| 37 | set_pud(pud, __pud((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); |
| 38 | } |
| 39 | #endif /* __PAGETABLE_PMD_FOLDED */ |
| 40 | |
| 41 | #define pmd_pgtable(pmd) pmd_page(pmd) |
| 42 | |
| 43 | static inline pgd_t *pgd_alloc(struct mm_struct *mm) |
| 44 | { |
| 45 | pgd_t *pgd; |
| 46 | |
| 47 | pgd = (pgd_t *)__get_free_page(GFP_KERNEL); |
| 48 | if (likely(pgd != NULL)) { |
| 49 | memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); |
| 50 | /* Copy kernel mappings */ |
| 51 | memcpy(pgd + USER_PTRS_PER_PGD, |
| 52 | init_mm.pgd + USER_PTRS_PER_PGD, |
| 53 | (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); |
| 54 | } |
| 55 | return pgd; |
| 56 | } |
| 57 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | #ifndef __PAGETABLE_PMD_FOLDED |
| 59 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | #define __pmd_free_tlb(tlb, pmd, addr) pmd_free((tlb)->mm, pmd) |
| 61 | |
| 62 | #endif /* __PAGETABLE_PMD_FOLDED */ |
| 63 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | #define __pte_free_tlb(tlb, pte, buf) \ |
| 65 | do { \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 66 | pgtable_pte_page_dtor(pte); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 67 | tlb_remove_page((tlb), pte); \ |
| 68 | } while (0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 69 | #endif /* CONFIG_MMU */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 70 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 71 | #endif /* _ASM_RISCV_PGALLOC_H */ |