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 | |
Mark Rutland | 15670ef | 2014-11-19 17:44:12 +0000 | [diff] [blame] | 29 | #define PGALLOC_GFP (GFP_KERNEL | __GFP_NOTRACK | __GFP_REPEAT | __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 | |
| 39 | static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd) |
| 40 | { |
| 41 | BUG_ON((unsigned long)pmd & (PAGE_SIZE-1)); |
| 42 | free_page((unsigned long)pmd); |
| 43 | } |
| 44 | |
| 45 | static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) |
| 46 | { |
| 47 | set_pud(pud, __pud(__pa(pmd) | PMD_TYPE_TABLE)); |
| 48 | } |
| 49 | |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 50 | #endif /* CONFIG_PGTABLE_LEVELS > 2 */ |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 51 | |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 52 | #if CONFIG_PGTABLE_LEVELS > 3 |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 53 | |
| 54 | static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr) |
| 55 | { |
Mark Rutland | 15670ef | 2014-11-19 17:44:12 +0000 | [diff] [blame] | 56 | return (pud_t *)__get_free_page(PGALLOC_GFP); |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static inline void pud_free(struct mm_struct *mm, pud_t *pud) |
| 60 | { |
| 61 | BUG_ON((unsigned long)pud & (PAGE_SIZE-1)); |
| 62 | free_page((unsigned long)pud); |
| 63 | } |
| 64 | |
| 65 | static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pud_t *pud) |
| 66 | { |
| 67 | set_pgd(pgd, __pgd(__pa(pud) | PUD_TYPE_TABLE)); |
| 68 | } |
| 69 | |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 70 | #endif /* CONFIG_PGTABLE_LEVELS > 3 */ |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 71 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 72 | extern pgd_t *pgd_alloc(struct mm_struct *mm); |
| 73 | extern void pgd_free(struct mm_struct *mm, pgd_t *pgd); |
| 74 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 75 | static inline pte_t * |
| 76 | pte_alloc_one_kernel(struct mm_struct *mm, unsigned long addr) |
| 77 | { |
| 78 | return (pte_t *)__get_free_page(PGALLOC_GFP); |
| 79 | } |
| 80 | |
| 81 | static inline pgtable_t |
| 82 | pte_alloc_one(struct mm_struct *mm, unsigned long addr) |
| 83 | { |
| 84 | struct page *pte; |
| 85 | |
| 86 | pte = alloc_pages(PGALLOC_GFP, 0); |
Kirill A. Shutemov | d97a229 | 2013-11-14 14:31:27 -0800 | [diff] [blame] | 87 | if (!pte) |
| 88 | return NULL; |
| 89 | if (!pgtable_page_ctor(pte)) { |
| 90 | __free_page(pte); |
| 91 | return NULL; |
| 92 | } |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 93 | return pte; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Free a PTE table. |
| 98 | */ |
| 99 | static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte) |
| 100 | { |
| 101 | if (pte) |
| 102 | free_page((unsigned long)pte); |
| 103 | } |
| 104 | |
| 105 | static inline void pte_free(struct mm_struct *mm, pgtable_t pte) |
| 106 | { |
| 107 | pgtable_page_dtor(pte); |
| 108 | __free_page(pte); |
| 109 | } |
| 110 | |
| 111 | static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t pte, |
| 112 | pmdval_t prot) |
| 113 | { |
| 114 | set_pmd(pmdp, __pmd(pte | prot)); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Populate the pmdp entry with a pointer to the pte. This pmd is part |
| 119 | * of the mm address space. |
| 120 | */ |
| 121 | static inline void |
| 122 | pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep) |
| 123 | { |
| 124 | /* |
| 125 | * The pmd must be loaded with the physical address of the PTE table |
| 126 | */ |
| 127 | __pmd_populate(pmdp, __pa(ptep), PMD_TYPE_TABLE); |
| 128 | } |
| 129 | |
| 130 | static inline void |
| 131 | pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) |
| 132 | { |
| 133 | __pmd_populate(pmdp, page_to_phys(ptep), PMD_TYPE_TABLE); |
| 134 | } |
| 135 | #define pmd_pgtable(pmd) pmd_page(pmd) |
| 136 | |
| 137 | #endif |