blob: 605f5c5398d1d9e5acaecc32fd1cb2c27643acc4 [file] [log] [blame]
David Gibsonf88df142007-04-30 16:30:56 +10001#ifndef _ASM_POWERPC_PGALLOC_64_H
2#define _ASM_POWERPC_PGALLOC_64_H
3/*
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
David Gibsonf88df142007-04-30 16:30:56 +100010#include <linux/slab.h>
11#include <linux/cpumask.h>
12#include <linux/percpu.h>
13
David Gibsona0668cd2009-10-28 16:27:18 +000014/*
15 * Functions that deal with pagetables that could be at any level of
16 * the table need to be passed an "index_size" so they know how to
17 * handle allocation. For PTE pages (which are linked to a struct
18 * page for now, and drawn from the main get_free_pages() pool), the
19 * allocation size will be (2^index_size * sizeof(pointer)) and
20 * allocations are drawn from the kmem_cache in PGT_CACHE(index_size).
21 *
22 * The maximum index size needs to be big enough to allow any
23 * pagetable sizes we need, but small enough to fit in the low bits of
24 * any page table pointer. In other words all pagetables, even tiny
25 * ones, must be aligned to allow at least enough low 0 bits to
26 * contain this value. This value is also used as a mask, so it must
27 * be one less than a power of two.
28 */
29#define MAX_PGTABLE_INDEX_SIZE 0xf
30
David Gibsonf88df142007-04-30 16:30:56 +100031extern struct kmem_cache *pgtable_cache[];
David Gibsona0668cd2009-10-28 16:27:18 +000032#define PGT_CACHE(shift) (pgtable_cache[(shift)-1])
David Gibsonf88df142007-04-30 16:30:56 +100033
34static inline pgd_t *pgd_alloc(struct mm_struct *mm)
35{
David Gibsona0668cd2009-10-28 16:27:18 +000036 return kmem_cache_alloc(PGT_CACHE(PGD_INDEX_SIZE), GFP_KERNEL);
David Gibsonf88df142007-04-30 16:30:56 +100037}
38
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080039static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
David Gibsonf88df142007-04-30 16:30:56 +100040{
David Gibsona0668cd2009-10-28 16:27:18 +000041 kmem_cache_free(PGT_CACHE(PGD_INDEX_SIZE), pgd);
David Gibsonf88df142007-04-30 16:30:56 +100042}
43
44#ifndef CONFIG_PPC_64K_PAGES
45
46#define pgd_populate(MM, PGD, PUD) pgd_set(PGD, PUD)
47
48static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
49{
David Gibsona0668cd2009-10-28 16:27:18 +000050 return kmem_cache_alloc(PGT_CACHE(PUD_INDEX_SIZE),
David Gibsonf88df142007-04-30 16:30:56 +100051 GFP_KERNEL|__GFP_REPEAT);
52}
53
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080054static inline void pud_free(struct mm_struct *mm, pud_t *pud)
David Gibsonf88df142007-04-30 16:30:56 +100055{
David Gibsona0668cd2009-10-28 16:27:18 +000056 kmem_cache_free(PGT_CACHE(PUD_INDEX_SIZE), pud);
David Gibsonf88df142007-04-30 16:30:56 +100057}
58
59static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
60{
61 pud_set(pud, (unsigned long)pmd);
62}
63
64#define pmd_populate(mm, pmd, pte_page) \
65 pmd_populate_kernel(mm, pmd, page_address(pte_page))
66#define pmd_populate_kernel(mm, pmd, pte) pmd_set(pmd, (unsigned long)(pte))
Martin Schwidefsky2f569af2008-02-08 04:22:04 -080067#define pmd_pgtable(pmd) pmd_page(pmd)
David Gibsonf88df142007-04-30 16:30:56 +100068
69
70#else /* CONFIG_PPC_64K_PAGES */
71
72#define pud_populate(mm, pud, pmd) pud_set(pud, (unsigned long)pmd)
73
74static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
75 pte_t *pte)
76{
77 pmd_set(pmd, (unsigned long)pte);
78}
79
80#define pmd_populate(mm, pmd, pte_page) \
81 pmd_populate_kernel(mm, pmd, page_address(pte_page))
Martin Schwidefsky2f569af2008-02-08 04:22:04 -080082#define pmd_pgtable(pmd) pmd_page(pmd)
David Gibsonf88df142007-04-30 16:30:56 +100083
84#endif /* CONFIG_PPC_64K_PAGES */
85
86static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
87{
David Gibsona0668cd2009-10-28 16:27:18 +000088 return kmem_cache_alloc(PGT_CACHE(PMD_INDEX_SIZE),
David Gibsonf88df142007-04-30 16:30:56 +100089 GFP_KERNEL|__GFP_REPEAT);
90}
91
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080092static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
David Gibsonf88df142007-04-30 16:30:56 +100093{
David Gibsona0668cd2009-10-28 16:27:18 +000094 kmem_cache_free(PGT_CACHE(PMD_INDEX_SIZE), pmd);
David Gibsonf88df142007-04-30 16:30:56 +100095}
96
97static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm,
98 unsigned long address)
99{
Hugh Dickins517e2262007-05-09 14:38:48 +1000100 return (pte_t *)__get_free_page(GFP_KERNEL | __GFP_REPEAT | __GFP_ZERO);
David Gibsonf88df142007-04-30 16:30:56 +1000101}
102
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800103static inline pgtable_t pte_alloc_one(struct mm_struct *mm,
104 unsigned long address)
David Gibsonf88df142007-04-30 16:30:56 +1000105{
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800106 struct page *page;
107 pte_t *pte;
108
109 pte = pte_alloc_one_kernel(mm, address);
110 if (!pte)
111 return NULL;
112 page = virt_to_page(pte);
113 pgtable_page_ctor(page);
114 return page;
David Gibsonf88df142007-04-30 16:30:56 +1000115}
116
David Gibsona0668cd2009-10-28 16:27:18 +0000117static inline void pgtable_free(void *table, unsigned index_size)
David Gibsonf88df142007-04-30 16:30:56 +1000118{
David Gibsona0668cd2009-10-28 16:27:18 +0000119 if (!index_size)
120 free_page((unsigned long)table);
121 else {
122 BUG_ON(index_size > MAX_PGTABLE_INDEX_SIZE);
123 kmem_cache_free(PGT_CACHE(index_size), table);
124 }
David Gibsonf88df142007-04-30 16:30:56 +1000125}
126
David Gibsona0668cd2009-10-28 16:27:18 +0000127#define __pmd_free_tlb(tlb, pmd, addr) \
128 pgtable_free_tlb(tlb, pmd, PMD_INDEX_SIZE)
David Gibsonf88df142007-04-30 16:30:56 +1000129#ifndef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000130#define __pud_free_tlb(tlb, pud, addr) \
David Gibsona0668cd2009-10-28 16:27:18 +0000131 pgtable_free_tlb(tlb, pud, PUD_INDEX_SIZE)
132
David Gibsonf88df142007-04-30 16:30:56 +1000133#endif /* CONFIG_PPC_64K_PAGES */
134
135#define check_pgt_cache() do { } while (0)
136
137#endif /* _ASM_POWERPC_PGALLOC_64_H */