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
22unsigned long *crst_table_alloc(struct mm_struct *);
23void crst_table_free(struct mm_struct *, unsigned long *);
24
25unsigned long *page_table_alloc(struct mm_struct *);
26struct ptdesc *page_table_alloc_pgste(struct mm_struct *mm);
27void page_table_free(struct mm_struct *, unsigned long *);
28void page_table_free_pgste(struct ptdesc *ptdesc);
29extern int page_table_allocate_pgste;
30
31static inline void crst_table_init(unsigned long *crst, unsigned long entry)
32{
33	memset64((u64 *)crst, entry, _CRST_ENTRIES);
34}
35
36int crst_table_upgrade(struct mm_struct *mm, unsigned long limit);
37
38static inline unsigned long check_asce_limit(struct mm_struct *mm, unsigned long addr,
39					     unsigned long len)
40{
41	int rc;
42
43	if (addr + len > mm->context.asce_limit &&
44	    addr + len <= TASK_SIZE) {
45		rc = crst_table_upgrade(mm, addr + len);
46		if (rc)
47			return (unsigned long) rc;
48	}
49	return addr;
50}
51
52static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long address)
53{
54	unsigned long *table = crst_table_alloc(mm);
55
56	if (table)
57		crst_table_init(table, _REGION2_ENTRY_EMPTY);
58	return (p4d_t *) table;
59}
60
61static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
62{
63	if (!mm_p4d_folded(mm))
64		crst_table_free(mm, (unsigned long *) p4d);
65}
66
67static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long address)
68{
69	unsigned long *table = crst_table_alloc(mm);
70	if (table)
71		crst_table_init(table, _REGION3_ENTRY_EMPTY);
72	return (pud_t *) table;
73}
74
75static inline void pud_free(struct mm_struct *mm, pud_t *pud)
76{
77	if (!mm_pud_folded(mm))
78		crst_table_free(mm, (unsigned long *) pud);
79}
80
81static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long vmaddr)
82{
83	unsigned long *table = crst_table_alloc(mm);
84
85	if (!table)
86		return NULL;
87	crst_table_init(table, _SEGMENT_ENTRY_EMPTY);
88	if (!pagetable_pmd_ctor(virt_to_ptdesc(table))) {
89		crst_table_free(mm, table);
90		return NULL;
91	}
92	return (pmd_t *) table;
93}
94
95static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
96{
97	if (mm_pmd_folded(mm))
98		return;
99	pagetable_pmd_dtor(virt_to_ptdesc(pmd));
100	crst_table_free(mm, (unsigned long *) pmd);
101}
102
103static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d)
104{
105	set_pgd(pgd, __pgd(_REGION1_ENTRY | __pa(p4d)));
106}
107
108static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud)
109{
110	set_p4d(p4d, __p4d(_REGION2_ENTRY | __pa(pud)));
111}
112
113static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
114{
115	set_pud(pud, __pud(_REGION3_ENTRY | __pa(pmd)));
116}
117
118static inline pgd_t *pgd_alloc(struct mm_struct *mm)
119{
120	return (pgd_t *) crst_table_alloc(mm);
121}
122
123static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
124{
125	crst_table_free(mm, (unsigned long *) pgd);
126}
127
128static inline void pmd_populate(struct mm_struct *mm,
129				pmd_t *pmd, pgtable_t pte)
130{
131	set_pmd(pmd, __pmd(_SEGMENT_ENTRY | __pa(pte)));
132}
133
134#define pmd_populate_kernel(mm, pmd, pte) pmd_populate(mm, pmd, pte)
135
136/*
137 * page table entry allocation/free routines.
138 */
139#define pte_alloc_one_kernel(mm) ((pte_t *)page_table_alloc(mm))
140#define pte_alloc_one(mm) ((pte_t *)page_table_alloc(mm))
141
142#define pte_free_kernel(mm, pte) page_table_free(mm, (unsigned long *) pte)
143#define pte_free(mm, pte) page_table_free(mm, (unsigned long *) pte)
144
145/* arch use pte_free_defer() implementation in arch/s390/mm/pgalloc.c */
146#define pte_free_defer pte_free_defer
147void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable);
148
149void vmem_map_init(void);
150void *vmem_crst_alloc(unsigned long val);
151pte_t *vmem_pte_alloc(void);
152
153unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages);
154void base_asce_free(unsigned long asce);
155
156#endif /* _S390_PGALLOC_H */
157