xref: /openbmc/linux/arch/arc/include/asm/pgalloc.h (revision d9820ff76f95fa26d33e412254a89cd65b23142d)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4  *
5  * vineetg: June 2011
6  *  -"/proc/meminfo | grep PageTables" kept on increasing
7  *   Recently added pgtable dtor was not getting called.
8  *
9  * vineetg: May 2011
10  *  -Variable pg-sz means that Page Tables could be variable sized themselves
11  *    So calculate it based on addr traversal split [pgd-bits:pte-bits:xxx]
12  *  -Page Table size capped to max 1 to save memory - hence verified.
13  *  -Since these deal with constants, gcc compile-time optimizes them.
14  *
15  * vineetg: Nov 2010
16  *  -Added pgtable ctor/dtor used for pgtable mem accounting
17  *
18  * vineetg: April 2010
19  *  -Switched pgtable_t from being struct page * to unsigned long
20  *      =Needed so that Page Table allocator (pte_alloc_one) is not forced to
21  *       deal with struct page. That way in future we can make it allocate
22  *       multiple PG Tbls in one Page Frame
23  *      =sweet side effect is avoiding calls to ugly page_address( ) from the
24  *       pg-tlb allocator sub-sys (pte_alloc_one, ptr_free, pmd_populate)
25  *
26  *  Amit Bhor, Sameer Dhavale: Codito Technologies 2004
27  */
28 
29 #ifndef _ASM_ARC_PGALLOC_H
30 #define _ASM_ARC_PGALLOC_H
31 
32 #include <linux/mm.h>
33 #include <linux/log2.h>
34 
35 static inline void
36 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte)
37 {
38 	/*
39 	 * The cast to long below is OK in 32-bit PAE40 regime with long long pte
40 	 * Despite "wider" pte, the pte table needs to be in non-PAE low memory
41 	 * as all higher levels can only hold long pointers.
42 	 *
43 	 * The cast itself is needed given simplistic definition of set_pmd()
44 	 */
45 	set_pmd(pmd, __pmd((unsigned long)pte));
46 }
47 
48 static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, pgtable_t pte_page)
49 {
50 	set_pmd(pmd, __pmd((unsigned long)page_address(pte_page)));
51 }
52 
53 static inline pgd_t *pgd_alloc(struct mm_struct *mm)
54 {
55 	pgd_t *ret = (pgd_t *) __get_free_page(GFP_KERNEL);
56 
57 	if (ret) {
58 		int num, num2;
59 		num = USER_PTRS_PER_PGD + USER_KERNEL_GUTTER / PGDIR_SIZE;
60 		memzero(ret, num * sizeof(pgd_t));
61 
62 		num2 = VMALLOC_SIZE / PGDIR_SIZE;
63 		memcpy(ret + num, swapper_pg_dir + num, num2 * sizeof(pgd_t));
64 
65 		memzero(ret + num + num2,
66 			       (PTRS_PER_PGD - num - num2) * sizeof(pgd_t));
67 
68 	}
69 	return ret;
70 }
71 
72 static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
73 {
74 	free_page((unsigned long)pgd);
75 }
76 
77 static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
78 {
79 	pte_t *pte;
80 
81 	pte = (pte_t *) __get_free_page(GFP_KERNEL | __GFP_ZERO);
82 
83 	return pte;
84 }
85 
86 static inline pgtable_t pte_alloc_one(struct mm_struct *mm)
87 {
88 	struct page *page;
89 
90 	page = (pgtable_t)alloc_page(GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT);
91 	if (!page)
92 		return NULL;
93 
94 	if (!pgtable_pte_page_ctor(page)) {
95 		__free_page(page);
96 		return NULL;
97 	}
98 
99 	return page;
100 }
101 
102 static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
103 {
104 	free_page((unsigned long)pte);
105 }
106 
107 static inline void pte_free(struct mm_struct *mm, pgtable_t pte_page)
108 {
109 	pgtable_pte_page_dtor(pte_page);
110 	__free_page(pte_page);
111 }
112 
113 #define __pte_free_tlb(tlb, pte, addr)  pte_free((tlb)->mm, pte)
114 
115 #endif /* _ASM_ARC_PGALLOC_H */
116