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, page) (pmd_val(*pmd) = \
32 	(unsigned long)(page_address(page)))
33 
34 #define pmd_populate_kernel(mm, pmd, pte) (pmd_val(*pmd) = (unsigned long)(pte))
35 
36 #define pmd_pgtable(pmd) pmd_page(pmd)
37 
38 static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t page,
39 				  unsigned long address)
40 {
41 	pgtable_pte_page_dtor(page);
42 	__free_page(page);
43 }
44 
45 static inline struct page *pte_alloc_one(struct mm_struct *mm)
46 {
47 	struct page *page = alloc_pages(GFP_DMA, 0);
48 	pte_t *pte;
49 
50 	if (!page)
51 		return NULL;
52 	if (!pgtable_pte_page_ctor(page)) {
53 		__free_page(page);
54 		return NULL;
55 	}
56 
57 	pte = kmap(page);
58 	if (pte) {
59 		clear_page(pte);
60 		__flush_page_to_ram(pte);
61 		flush_tlb_kernel_page(pte);
62 		nocache_page(pte);
63 	}
64 	kunmap(page);
65 
66 	return page;
67 }
68 
69 static inline void pte_free(struct mm_struct *mm, struct page *page)
70 {
71 	pgtable_pte_page_dtor(page);
72 	__free_page(page);
73 }
74 
75 /*
76  * In our implementation, each pgd entry contains 1 pmd that is never allocated
77  * or freed.  pgd_present is always 1, so this should never be called. -NL
78  */
79 #define pmd_free(mm, pmd) BUG()
80 
81 static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
82 {
83 	free_page((unsigned long) pgd);
84 }
85 
86 static inline pgd_t *pgd_alloc(struct mm_struct *mm)
87 {
88 	pgd_t *new_pgd;
89 
90 	new_pgd = (pgd_t *)__get_free_page(GFP_DMA | __GFP_NOWARN);
91 	if (!new_pgd)
92 		return NULL;
93 	memcpy(new_pgd, swapper_pg_dir, PAGE_SIZE);
94 	memset(new_pgd, 0, PAGE_OFFSET >> PGDIR_SHIFT);
95 	return new_pgd;
96 }
97 
98 #endif /* M68K_MCF_PGALLOC_H */
99