blob: a120e7f8d535a30c7f8d812710b310a60f578ecb [file] [log] [blame]
Aneesh Kumar K.V75a9b8a2016-04-29 23:26:14 +10001#ifndef _ASM_POWERPC_BOOK3S_32_PGALLOC_H
2#define _ASM_POWERPC_BOOK3S_32_PGALLOC_H
Aneesh Kumar K.V101ad5c2016-04-29 23:26:13 +10003
4#include <linux/threads.h>
Christophe Leroy9b081e12016-12-07 08:47:24 +01005#include <linux/slab.h>
Aneesh Kumar K.V101ad5c2016-04-29 23:26:13 +10006
Christophe Leroy9b081e12016-12-07 08:47:24 +01007/*
8 * Functions that deal with pagetables that could be at any level of
9 * the table need to be passed an "index_size" so they know how to
10 * handle allocation. For PTE pages (which are linked to a struct
11 * page for now, and drawn from the main get_free_pages() pool), the
12 * allocation size will be (2^index_size * sizeof(pointer)) and
13 * allocations are drawn from the kmem_cache in PGT_CACHE(index_size).
14 *
15 * The maximum index size needs to be big enough to allow any
16 * pagetable sizes we need, but small enough to fit in the low bits of
17 * any page table pointer. In other words all pagetables, even tiny
18 * ones, must be aligned to allow at least enough low 0 bits to
19 * contain this value. This value is also used as a mask, so it must
20 * be one less than a power of two.
21 */
22#define MAX_PGTABLE_INDEX_SIZE 0xf
Aneesh Kumar K.V101ad5c2016-04-29 23:26:13 +100023
24extern void __bad_pte(pmd_t *pmd);
25
Christophe Leroy9b081e12016-12-07 08:47:24 +010026extern struct kmem_cache *pgtable_cache[];
27#define PGT_CACHE(shift) ({ \
28 BUG_ON(!(shift)); \
29 pgtable_cache[(shift) - 1]; \
30 })
31
32static inline pgd_t *pgd_alloc(struct mm_struct *mm)
33{
Balbir Singhde3b8762017-05-02 15:17:04 +100034 return kmem_cache_alloc(PGT_CACHE(PGD_INDEX_SIZE),
35 pgtable_gfp_flags(mm, GFP_KERNEL));
Christophe Leroy9b081e12016-12-07 08:47:24 +010036}
37
38static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
39{
40 kmem_cache_free(PGT_CACHE(PGD_INDEX_SIZE), pgd);
41}
Aneesh Kumar K.V101ad5c2016-04-29 23:26:13 +100042
43/*
44 * We don't have any real pmd's, and this code never triggers because
45 * the pgd will always be present..
46 */
47/* #define pmd_alloc_one(mm,address) ({ BUG(); ((pmd_t *)2); }) */
48#define pmd_free(mm, x) do { } while (0)
49#define __pmd_free_tlb(tlb,x,a) do { } while (0)
50/* #define pgd_populate(mm, pmd, pte) BUG() */
51
52#ifndef CONFIG_BOOKE
53
54static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp,
55 pte_t *pte)
56{
57 *pmdp = __pmd(__pa(pte) | _PMD_PRESENT);
58}
59
60static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmdp,
61 pgtable_t pte_page)
62{
63 *pmdp = __pmd((page_to_pfn(pte_page) << PAGE_SHIFT) | _PMD_PRESENT);
64}
65
66#define pmd_pgtable(pmd) pmd_page(pmd)
67#else
68
69static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp,
70 pte_t *pte)
71{
72 *pmdp = __pmd((unsigned long)pte | _PMD_PRESENT);
73}
74
75static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmdp,
76 pgtable_t pte_page)
77{
78 *pmdp = __pmd((unsigned long)lowmem_page_address(pte_page) | _PMD_PRESENT);
79}
80
81#define pmd_pgtable(pmd) pmd_page(pmd)
82#endif
83
84extern pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long addr);
85extern pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long addr);
86
87static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
88{
89 free_page((unsigned long)pte);
90}
91
92static inline void pte_free(struct mm_struct *mm, pgtable_t ptepage)
93{
94 pgtable_page_dtor(ptepage);
95 __free_page(ptepage);
96}
97
98static inline void pgtable_free(void *table, unsigned index_size)
99{
Christophe Leroy9b081e12016-12-07 08:47:24 +0100100 if (!index_size) {
101 free_page((unsigned long)table);
102 } else {
103 BUG_ON(index_size > MAX_PGTABLE_INDEX_SIZE);
104 kmem_cache_free(PGT_CACHE(index_size), table);
105 }
Aneesh Kumar K.V101ad5c2016-04-29 23:26:13 +1000106}
107
108#define check_pgt_cache() do { } while (0)
109
110#ifdef CONFIG_SMP
111static inline void pgtable_free_tlb(struct mmu_gather *tlb,
112 void *table, int shift)
113{
114 unsigned long pgf = (unsigned long)table;
115 BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE);
116 pgf |= shift;
117 tlb_remove_table(tlb, (void *)pgf);
118}
119
120static inline void __tlb_remove_table(void *_table)
121{
122 void *table = (void *)((unsigned long)_table & ~MAX_PGTABLE_INDEX_SIZE);
123 unsigned shift = (unsigned long)_table & MAX_PGTABLE_INDEX_SIZE;
124
125 pgtable_free(table, shift);
126}
127#else
128static inline void pgtable_free_tlb(struct mmu_gather *tlb,
129 void *table, int shift)
130{
131 pgtable_free(table, shift);
132}
133#endif
134
135static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t table,
136 unsigned long address)
137{
Aneesh Kumar K.V101ad5c2016-04-29 23:26:13 +1000138 pgtable_page_dtor(table);
139 pgtable_free_tlb(tlb, page_address(table), 0);
140}
Aneesh Kumar K.V75a9b8a2016-04-29 23:26:14 +1000141#endif /* _ASM_POWERPC_BOOK3S_32_PGALLOC_H */