Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Based on arch/arm/include/asm/pgalloc.h |
| 3 | * |
| 4 | * Copyright (C) 2000-2001 Russell King |
| 5 | * Copyright (C) 2012 ARM Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | #ifndef __ASM_PGALLOC_H |
| 20 | #define __ASM_PGALLOC_H |
| 21 | |
| 22 | #include <asm/pgtable-hwdef.h> |
| 23 | #include <asm/processor.h> |
| 24 | #include <asm/cacheflush.h> |
| 25 | #include <asm/tlbflush.h> |
| 26 | |
| 27 | #define check_pgt_cache() do { } while (0) |
| 28 | |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 29 | #define PGALLOC_GFP (GFP_KERNEL | __GFP_ZERO) |
Andrey Ryabinin | fd2203d | 2015-10-12 18:52:57 +0300 | [diff] [blame] | 30 | #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t)) |
Mark Rutland | 15670ef | 2014-11-19 17:44:12 +0000 | [diff] [blame] | 31 | |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 32 | #if CONFIG_PGTABLE_LEVELS > 2 |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 33 | |
| 34 | static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr) |
| 35 | { |
Mark Rutland | 15670ef | 2014-11-19 17:44:12 +0000 | [diff] [blame] | 36 | return (pmd_t *)__get_free_page(PGALLOC_GFP); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 39 | static inline void pmd_free(struct mm_struct *mm, pmd_t *pmdp) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 40 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 41 | BUG_ON((unsigned long)pmdp & (PAGE_SIZE-1)); |
| 42 | free_page((unsigned long)pmdp); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 45 | static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 46 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 47 | set_pud(pudp, __pud(__phys_to_pud_val(pmdp) | prot)); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 50 | static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp) |
Mark Rutland | 1e531cc | 2016-01-25 11:45:06 +0000 | [diff] [blame] | 51 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 52 | __pud_populate(pudp, __pa(pmdp), PMD_TYPE_TABLE); |
Mark Rutland | 1e531cc | 2016-01-25 11:45:06 +0000 | [diff] [blame] | 53 | } |
| 54 | #else |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 55 | static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot) |
Mark Rutland | 1e531cc | 2016-01-25 11:45:06 +0000 | [diff] [blame] | 56 | { |
| 57 | BUILD_BUG(); |
| 58 | } |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 59 | #endif /* CONFIG_PGTABLE_LEVELS > 2 */ |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 60 | |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 61 | #if CONFIG_PGTABLE_LEVELS > 3 |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 62 | |
| 63 | static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr) |
| 64 | { |
Mark Rutland | 15670ef | 2014-11-19 17:44:12 +0000 | [diff] [blame] | 65 | return (pud_t *)__get_free_page(PGALLOC_GFP); |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 66 | } |
| 67 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 68 | static inline void pud_free(struct mm_struct *mm, pud_t *pudp) |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 69 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 70 | BUG_ON((unsigned long)pudp & (PAGE_SIZE-1)); |
| 71 | free_page((unsigned long)pudp); |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 74 | static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t pudp, pgdval_t prot) |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 75 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 76 | set_pgd(pgdp, __pgd(__phys_to_pgd_val(pudp) | prot)); |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 77 | } |
| 78 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 79 | static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, pud_t *pudp) |
Mark Rutland | 1e531cc | 2016-01-25 11:45:06 +0000 | [diff] [blame] | 80 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 81 | __pgd_populate(pgdp, __pa(pudp), PUD_TYPE_TABLE); |
Mark Rutland | 1e531cc | 2016-01-25 11:45:06 +0000 | [diff] [blame] | 82 | } |
| 83 | #else |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 84 | static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t pudp, pgdval_t prot) |
Mark Rutland | 1e531cc | 2016-01-25 11:45:06 +0000 | [diff] [blame] | 85 | { |
| 86 | BUILD_BUG(); |
| 87 | } |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 88 | #endif /* CONFIG_PGTABLE_LEVELS > 3 */ |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 89 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 90 | extern pgd_t *pgd_alloc(struct mm_struct *mm); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 91 | extern void pgd_free(struct mm_struct *mm, pgd_t *pgdp); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 92 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 93 | static inline pte_t * |
| 94 | pte_alloc_one_kernel(struct mm_struct *mm, unsigned long addr) |
| 95 | { |
| 96 | return (pte_t *)__get_free_page(PGALLOC_GFP); |
| 97 | } |
| 98 | |
| 99 | static inline pgtable_t |
| 100 | pte_alloc_one(struct mm_struct *mm, unsigned long addr) |
| 101 | { |
| 102 | struct page *pte; |
| 103 | |
| 104 | pte = alloc_pages(PGALLOC_GFP, 0); |
Kirill A. Shutemov | d97a229 | 2013-11-14 14:31:27 -0800 | [diff] [blame] | 105 | if (!pte) |
| 106 | return NULL; |
| 107 | if (!pgtable_page_ctor(pte)) { |
| 108 | __free_page(pte); |
| 109 | return NULL; |
| 110 | } |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 111 | return pte; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Free a PTE table. |
| 116 | */ |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 117 | static inline void pte_free_kernel(struct mm_struct *mm, pte_t *ptep) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 118 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 119 | if (ptep) |
| 120 | free_page((unsigned long)ptep); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static inline void pte_free(struct mm_struct *mm, pgtable_t pte) |
| 124 | { |
| 125 | pgtable_page_dtor(pte); |
| 126 | __free_page(pte); |
| 127 | } |
| 128 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 129 | static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t ptep, |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 130 | pmdval_t prot) |
| 131 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 132 | set_pmd(pmdp, __pmd(__phys_to_pmd_val(ptep) | prot)); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Populate the pmdp entry with a pointer to the pte. This pmd is part |
| 137 | * of the mm address space. |
| 138 | */ |
| 139 | static inline void |
| 140 | pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep) |
| 141 | { |
| 142 | /* |
| 143 | * The pmd must be loaded with the physical address of the PTE table |
| 144 | */ |
| 145 | __pmd_populate(pmdp, __pa(ptep), PMD_TYPE_TABLE); |
| 146 | } |
| 147 | |
| 148 | static inline void |
| 149 | pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) |
| 150 | { |
| 151 | __pmd_populate(pmdp, page_to_phys(ptep), PMD_TYPE_TABLE); |
| 152 | } |
| 153 | #define pmd_pgtable(pmd) pmd_page(pmd) |
| 154 | |
| 155 | #endif |