1 /* 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 28 #define CTOR(shift) static void ctor_##shift(void *addr) \ 29 { \ 30 memset(addr, 0, sizeof(void *) << (shift)); \ 31 } 32 33 CTOR(0); CTOR(1); CTOR(2); CTOR(3); CTOR(4); CTOR(5); CTOR(6); CTOR(7); 34 CTOR(8); CTOR(9); CTOR(10); CTOR(11); CTOR(12); CTOR(13); CTOR(14); CTOR(15); 35 36 static inline void (*ctor(int shift))(void *) 37 { 38 BUILD_BUG_ON(MAX_PGTABLE_INDEX_SIZE != 15); 39 40 switch (shift) { 41 case 0: return ctor_0; 42 case 1: return ctor_1; 43 case 2: return ctor_2; 44 case 3: return ctor_3; 45 case 4: return ctor_4; 46 case 5: return ctor_5; 47 case 6: return ctor_6; 48 case 7: return ctor_7; 49 case 8: return ctor_8; 50 case 9: return ctor_9; 51 case 10: return ctor_10; 52 case 11: return ctor_11; 53 case 12: return ctor_12; 54 case 13: return ctor_13; 55 case 14: return ctor_14; 56 case 15: return ctor_15; 57 } 58 return NULL; 59 } 60 61 struct kmem_cache *pgtable_cache[MAX_PGTABLE_INDEX_SIZE + 1]; 62 EXPORT_SYMBOL_GPL(pgtable_cache); /* used by kvm_hv module */ 63 64 /* 65 * Create a kmem_cache() for pagetables. This is not used for PTE 66 * pages - they're linked to struct page, come from the normal free 67 * pages pool and have a different entry size (see real_pte_t) to 68 * everything else. Caches created by this function are used for all 69 * the higher level pagetables, and for hugepage pagetables. 70 */ 71 void pgtable_cache_add(unsigned int shift) 72 { 73 char *name; 74 unsigned long table_size = sizeof(void *) << shift; 75 unsigned long align = table_size; 76 77 /* When batching pgtable pointers for RCU freeing, we store 78 * the index size in the low bits. Table alignment must be 79 * big enough to fit it. 80 * 81 * Likewise, hugeapge pagetable pointers contain a (different) 82 * shift value in the low bits. All tables must be aligned so 83 * as to leave enough 0 bits in the address to contain it. */ 84 unsigned long minalign = max(MAX_PGTABLE_INDEX_SIZE + 1, 85 HUGEPD_SHIFT_MASK + 1); 86 struct kmem_cache *new; 87 88 /* It would be nice if this was a BUILD_BUG_ON(), but at the 89 * moment, gcc doesn't seem to recognize is_power_of_2 as a 90 * constant expression, so so much for that. */ 91 BUG_ON(!is_power_of_2(minalign)); 92 BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE); 93 94 if (PGT_CACHE(shift)) 95 return; /* Already have a cache of this size */ 96 97 align = max_t(unsigned long, align, minalign); 98 name = kasprintf(GFP_KERNEL, "pgtable-2^%d", shift); 99 new = kmem_cache_create(name, table_size, align, 0, ctor(shift)); 100 if (!new) 101 panic("Could not allocate pgtable cache for order %d", shift); 102 103 kfree(name); 104 pgtable_cache[shift] = new; 105 106 pr_debug("Allocated pgtable cache for order %d\n", shift); 107 } 108 EXPORT_SYMBOL_GPL(pgtable_cache_add); /* used by kvm_hv module */ 109 110 void pgtable_cache_init(void) 111 { 112 pgtable_cache_add(PGD_INDEX_SIZE); 113 114 if (PMD_CACHE_INDEX) 115 pgtable_cache_add(PMD_CACHE_INDEX); 116 /* 117 * In all current configs, when the PUD index exists it's the 118 * same size as either the pgd or pmd index except with THP enabled 119 * on book3s 64 120 */ 121 if (PUD_CACHE_INDEX) 122 pgtable_cache_add(PUD_CACHE_INDEX); 123 } 124