blob: eb8c6c8c4851a9a7e25a555dd3e61c7b0613fb4e [file] [log] [blame]
Christophe Leroy9b081e12016-12-07 08:47:24 +01001/*
2 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 *
5 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
6 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
7 * Copyright (C) 1996 Paul Mackerras
8 *
9 * Derived from "arch/i386/mm/init.c"
10 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
11 *
12 * Dave Engebretsen <engebret@us.ibm.com>
13 * Rework for PPC64 port.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 */
21
22#undef DEBUG
23
24#include <linux/string.h>
25#include <asm/pgalloc.h>
26#include <asm/pgtable.h>
27
28static void pgd_ctor(void *addr)
29{
30 memset(addr, 0, PGD_TABLE_SIZE);
31}
32
33static void pud_ctor(void *addr)
34{
35 memset(addr, 0, PUD_TABLE_SIZE);
36}
37
38static void pmd_ctor(void *addr)
39{
40 memset(addr, 0, PMD_TABLE_SIZE);
41}
42
43struct kmem_cache *pgtable_cache[MAX_PGTABLE_INDEX_SIZE];
Paul Mackerrasba9b3992017-01-30 21:21:38 +110044EXPORT_SYMBOL_GPL(pgtable_cache); /* used by kvm_hv module */
Christophe Leroy9b081e12016-12-07 08:47:24 +010045
46/*
47 * Create a kmem_cache() for pagetables. This is not used for PTE
48 * pages - they're linked to struct page, come from the normal free
49 * pages pool and have a different entry size (see real_pte_t) to
50 * everything else. Caches created by this function are used for all
51 * the higher level pagetables, and for hugepage pagetables.
52 */
53void pgtable_cache_add(unsigned shift, void (*ctor)(void *))
54{
55 char *name;
56 unsigned long table_size = sizeof(void *) << shift;
57 unsigned long align = table_size;
58
59 /* When batching pgtable pointers for RCU freeing, we store
60 * the index size in the low bits. Table alignment must be
61 * big enough to fit it.
62 *
63 * Likewise, hugeapge pagetable pointers contain a (different)
64 * shift value in the low bits. All tables must be aligned so
65 * as to leave enough 0 bits in the address to contain it. */
66 unsigned long minalign = max(MAX_PGTABLE_INDEX_SIZE + 1,
67 HUGEPD_SHIFT_MASK + 1);
68 struct kmem_cache *new;
69
70 /* It would be nice if this was a BUILD_BUG_ON(), but at the
71 * moment, gcc doesn't seem to recognize is_power_of_2 as a
72 * constant expression, so so much for that. */
73 BUG_ON(!is_power_of_2(minalign));
74 BUG_ON((shift < 1) || (shift > MAX_PGTABLE_INDEX_SIZE));
75
76 if (PGT_CACHE(shift))
77 return; /* Already have a cache of this size */
78
79 align = max_t(unsigned long, align, minalign);
80 name = kasprintf(GFP_KERNEL, "pgtable-2^%d", shift);
81 new = kmem_cache_create(name, table_size, align, 0, ctor);
Nicholas Pigginbf5ca682017-01-04 01:55:17 +100082 if (!new)
83 panic("Could not allocate pgtable cache for order %d", shift);
84
Christophe Leroy9b081e12016-12-07 08:47:24 +010085 kfree(name);
86 pgtable_cache[shift - 1] = new;
Nicholas Pigginbf5ca682017-01-04 01:55:17 +100087
Christophe Leroy9b081e12016-12-07 08:47:24 +010088 pr_debug("Allocated pgtable cache for order %d\n", shift);
89}
Paul Mackerrasba9b3992017-01-30 21:21:38 +110090EXPORT_SYMBOL_GPL(pgtable_cache_add); /* used by kvm_hv module */
Christophe Leroy9b081e12016-12-07 08:47:24 +010091
92void pgtable_cache_init(void)
93{
94 pgtable_cache_add(PGD_INDEX_SIZE, pgd_ctor);
95
Nicholas Pigginbf5ca682017-01-04 01:55:17 +100096 if (PMD_CACHE_INDEX && !PGT_CACHE(PMD_CACHE_INDEX))
Christophe Leroy9b081e12016-12-07 08:47:24 +010097 pgtable_cache_add(PMD_CACHE_INDEX, pmd_ctor);
98 /*
99 * In all current configs, when the PUD index exists it's the
100 * same size as either the pgd or pmd index except with THP enabled
101 * on book3s 64
102 */
103 if (PUD_INDEX_SIZE && !PGT_CACHE(PUD_INDEX_SIZE))
104 pgtable_cache_add(PUD_INDEX_SIZE, pud_ctor);
Christophe Leroy9b081e12016-12-07 08:47:24 +0100105}