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