1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef M68K_MCF_PGALLOC_H
3 #define M68K_MCF_PGALLOC_H
4 
5 #include <asm/tlb.h>
6 #include <asm/tlbflush.h>
7 
8 extern inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
9 {
10 	free_page((unsigned long) pte);
11 }
12 
13 extern const char bad_pmd_string[];
14 
15 extern inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
16 {
17 	unsigned long page = __get_free_page(GFP_DMA);
18 
19 	if (!page)
20 		return NULL;
21 
22 	memset((void *)page, 0, PAGE_SIZE);
23 	return (pte_t *) (page);
24 }
25 
26 extern inline pmd_t *pmd_alloc_kernel(pgd_t *pgd, unsigned long address)
27 {
28 	return (pmd_t *) pgd;
29 }
30 
31 #define pmd_populate(mm, pmd, pte) (pmd_val(*pmd) = (unsigned long)(pte))
32 
33 #define pmd_populate_kernel pmd_populate
34 
35 static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pgtable,
36 				  unsigned long address)
37 {
38 	struct page *page = virt_to_page(pgtable);
39 
40 	pgtable_pte_page_dtor(page);
41 	__free_page(page);
42 }
43 
44 static inline pgtable_t pte_alloc_one(struct mm_struct *mm)
45 {
46 	struct page *page = alloc_pages(GFP_DMA, 0);
47 	pte_t *pte;
48 
49 	if (!page)
50 		return NULL;
51 	if (!pgtable_pte_page_ctor(page)) {
52 		__free_page(page);
53 		return NULL;
54 	}
55 
56 	pte = page_address(page);
57 	clear_page(pte);
58 
59 	return pte;
60 }
61 
62 static inline void pte_free(struct mm_struct *mm, pgtable_t pgtable)
63 {
64 	struct page *page = virt_to_page(pgtable);
65 
66 	pgtable_pte_page_dtor(page);
67 	__free_page(page);
68 }
69 
70 /*
71  * In our implementation, each pgd entry contains 1 pmd that is never allocated
72  * or freed.  pgd_present is always 1, so this should never be called. -NL
73  */
74 #define pmd_free(mm, pmd) BUG()
75 
76 static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
77 {
78 	free_page((unsigned long) pgd);
79 }
80 
81 static inline pgd_t *pgd_alloc(struct mm_struct *mm)
82 {
83 	pgd_t *new_pgd;
84 
85 	new_pgd = (pgd_t *)__get_free_page(GFP_DMA | __GFP_NOWARN);
86 	if (!new_pgd)
87 		return NULL;
88 	memcpy(new_pgd, swapper_pg_dir, PTRS_PER_PGD * sizeof(pgd_t));
89 	memset(new_pgd, 0, PAGE_OFFSET >> PGDIR_SHIFT);
90 	return new_pgd;
91 }
92 
93 #endif /* M68K_MCF_PGALLOC_H */
94