1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * S390 version 4 * Copyright IBM Corp. 1999, 2000 5 * Author(s): Hartmut Penner (hp@de.ibm.com) 6 * Martin Schwidefsky (schwidefsky@de.ibm.com) 7 * 8 * Derived from "include/asm-i386/pgalloc.h" 9 * Copyright (C) 1994 Linus Torvalds 10 */ 11 12 #ifndef _S390_PGALLOC_H 13 #define _S390_PGALLOC_H 14 15 #include <linux/threads.h> 16 #include <linux/string.h> 17 #include <linux/gfp.h> 18 #include <linux/mm.h> 19 20 #define CRST_ALLOC_ORDER 2 21 22 unsigned long *crst_table_alloc_noprof(struct mm_struct *); 23 #define crst_table_alloc(...) alloc_hooks(crst_table_alloc_noprof(__VA_ARGS__)) 24 void crst_table_free(struct mm_struct *, unsigned long *); 25 26 unsigned long *page_table_alloc_noprof(struct mm_struct *); 27 #define page_table_alloc(...) alloc_hooks(page_table_alloc_noprof(__VA_ARGS__)) 28 void page_table_free(struct mm_struct *, unsigned long *); 29 30 static inline void crst_table_init(unsigned long *crst, unsigned long entry) 31 { 32 memset64((u64 *)crst, entry, _CRST_ENTRIES); 33 } 34 35 int crst_table_upgrade(struct mm_struct *mm, unsigned long limit); 36 37 static inline unsigned long check_asce_limit(struct mm_struct *mm, unsigned long addr, 38 unsigned long len) 39 { 40 int rc; 41 42 if (addr + len > mm->context.asce_limit && 43 addr + len <= TASK_SIZE) { 44 rc = crst_table_upgrade(mm, addr + len); 45 if (rc) 46 return (unsigned long) rc; 47 } 48 return addr; 49 } 50 51 static inline p4d_t *p4d_alloc_one_noprof(struct mm_struct *mm, unsigned long address) 52 { 53 unsigned long *table = crst_table_alloc_noprof(mm); 54 55 if (!table) 56 return NULL; 57 crst_table_init(table, _REGION2_ENTRY_EMPTY); 58 pagetable_p4d_ctor(virt_to_ptdesc(table)); 59 60 return (p4d_t *) table; 61 } 62 #define p4d_alloc_one(...) alloc_hooks(p4d_alloc_one_noprof(__VA_ARGS__)) 63 64 static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) 65 { 66 if (mm_p4d_folded(mm)) 67 return; 68 69 pagetable_dtor(virt_to_ptdesc(p4d)); 70 crst_table_free(mm, (unsigned long *) p4d); 71 } 72 73 static inline pud_t *pud_alloc_one_noprof(struct mm_struct *mm, unsigned long address) 74 { 75 unsigned long *table = crst_table_alloc_noprof(mm); 76 77 if (!table) 78 return NULL; 79 crst_table_init(table, _REGION3_ENTRY_EMPTY); 80 pagetable_pud_ctor(virt_to_ptdesc(table)); 81 82 return (pud_t *) table; 83 } 84 #define pud_alloc_one(...) alloc_hooks(pud_alloc_one_noprof(__VA_ARGS__)) 85 86 static inline void pud_free(struct mm_struct *mm, pud_t *pud) 87 { 88 if (mm_pud_folded(mm)) 89 return; 90 91 pagetable_dtor(virt_to_ptdesc(pud)); 92 crst_table_free(mm, (unsigned long *) pud); 93 } 94 95 static inline pmd_t *pmd_alloc_one_noprof(struct mm_struct *mm, unsigned long vmaddr) 96 { 97 unsigned long *table = crst_table_alloc_noprof(mm); 98 99 if (!table) 100 return NULL; 101 crst_table_init(table, _SEGMENT_ENTRY_EMPTY); 102 if (!pagetable_pmd_ctor(mm, virt_to_ptdesc(table))) { 103 crst_table_free(mm, table); 104 return NULL; 105 } 106 return (pmd_t *) table; 107 } 108 #define pmd_alloc_one(...) alloc_hooks(pmd_alloc_one_noprof(__VA_ARGS__)) 109 110 static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd) 111 { 112 if (mm_pmd_folded(mm)) 113 return; 114 pagetable_dtor(virt_to_ptdesc(pmd)); 115 crst_table_free(mm, (unsigned long *) pmd); 116 } 117 118 static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d) 119 { 120 set_pgd(pgd, __pgd(_REGION1_ENTRY | __pa(p4d))); 121 } 122 123 static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud) 124 { 125 set_p4d(p4d, __p4d(_REGION2_ENTRY | __pa(pud))); 126 } 127 128 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) 129 { 130 set_pud(pud, __pud(_REGION3_ENTRY | __pa(pmd))); 131 } 132 133 static inline pgd_t *pgd_alloc_noprof(struct mm_struct *mm) 134 { 135 unsigned long *table = crst_table_alloc_noprof(mm); 136 137 if (!table) 138 return NULL; 139 pagetable_pgd_ctor(virt_to_ptdesc(table)); 140 141 return (pgd_t *) table; 142 } 143 #define pgd_alloc(...) alloc_hooks(pgd_alloc_noprof(__VA_ARGS__)) 144 145 static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd) 146 { 147 pagetable_dtor(virt_to_ptdesc(pgd)); 148 crst_table_free(mm, (unsigned long *) pgd); 149 } 150 151 static inline void pmd_populate(struct mm_struct *mm, 152 pmd_t *pmd, pgtable_t pte) 153 { 154 set_pmd(pmd, __pmd(_SEGMENT_ENTRY | __pa(pte))); 155 } 156 157 #define pmd_populate_kernel(mm, pmd, pte) pmd_populate(mm, pmd, pte) 158 159 /* 160 * page table entry allocation/free routines. 161 */ 162 #define pte_alloc_one_kernel(mm) ((pte_t *)page_table_alloc(mm)) 163 #define pte_alloc_one(mm) ((pte_t *)page_table_alloc(mm)) 164 165 #define pte_free_kernel(mm, pte) page_table_free(mm, (unsigned long *) pte) 166 #define pte_free(mm, pte) page_table_free(mm, (unsigned long *) pte) 167 168 /* arch use pte_free_defer() implementation in arch/s390/mm/pgalloc.c */ 169 #define pte_free_defer pte_free_defer 170 void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable); 171 172 void vmem_map_init(void); 173 void *vmem_crst_alloc(unsigned long val); 174 pte_t *vmem_pte_alloc(void); 175 176 unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages); 177 void base_asce_free(unsigned long asce); 178 179 #endif /* _S390_PGALLOC_H */ 180