hash-64k.h 9.2 KB
Newer Older
1 2 3 4
#ifndef _ASM_POWERPC_BOOK3S_64_HASH_64K_H
#define _ASM_POWERPC_BOOK3S_64_HASH_64K_H

#define PTE_INDEX_SIZE  8
5 6
#define PMD_INDEX_SIZE  5
#define PUD_INDEX_SIZE	5
7 8 9 10
#define PGD_INDEX_SIZE  12

#define PTRS_PER_PTE	(1 << PTE_INDEX_SIZE)
#define PTRS_PER_PMD	(1 << PMD_INDEX_SIZE)
11
#define PTRS_PER_PUD	(1 << PUD_INDEX_SIZE)
12 13 14 15 16 17 18 19 20 21
#define PTRS_PER_PGD	(1 << PGD_INDEX_SIZE)

/* With 4k base page size, hugepage PTEs go at the PMD level */
#define MIN_HUGEPTE_SHIFT	PAGE_SHIFT

/* PMD_SHIFT determines what a second-level page table entry can map */
#define PMD_SHIFT	(PAGE_SHIFT + PTE_INDEX_SIZE)
#define PMD_SIZE	(1UL << PMD_SHIFT)
#define PMD_MASK	(~(PMD_SIZE-1))

22 23 24 25 26 27 28
/* PUD_SHIFT determines what a third-level page table entry can map */
#define PUD_SHIFT	(PMD_SHIFT + PMD_INDEX_SIZE)
#define PUD_SIZE	(1UL << PUD_SHIFT)
#define PUD_MASK	(~(PUD_SIZE-1))

/* PGDIR_SHIFT determines what a fourth-level page table entry can map */
#define PGDIR_SHIFT	(PUD_SHIFT + PUD_INDEX_SIZE)
29 30 31
#define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
#define PGDIR_MASK	(~(PGDIR_SIZE-1))

32 33
#define H_PAGE_COMBO	0x00001000 /* this is a combo 4k page */
#define H_PAGE_4K_PFN	0x00002000 /* PFN is for a single 4k page */
34
/*
35 36
 * We need to differentiate between explicit huge page and THP huge
 * page, since THP huge page also need to track real subpage details
37
 */
38 39 40 41 42 43 44
#define H_PAGE_THP_HUGE  H_PAGE_4K_PFN

/*
 * Used to track subpage group valid if H_PAGE_COMBO is set
 * This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND
 */
#define H_PAGE_COMBO_VALID	(H_PAGE_F_GIX | H_PAGE_F_SECOND)
45 46

/* PTE flags to conserve for HPTE identification */
47 48
#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \
			 H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO)
49 50 51 52 53 54 55 56 57 58 59
/*
 * we support 16 fragments per PTE page of 64K size.
 */
#define PTE_FRAG_NR	16
/*
 * We use a 2K PTE page fragment and another 2K for storing
 * real_pte_t hash index
 */
#define PTE_FRAG_SIZE_SHIFT  12
#define PTE_FRAG_SIZE (1UL << PTE_FRAG_SIZE_SHIFT)

60 61 62 63 64 65
/* Bits to mask out from a PMD to get to the PTE page */
#define PMD_MASKED_BITS		0xc0000000000000ffUL
/* Bits to mask out from a PUD to get to the PMD page */
#define PUD_MASKED_BITS		0xc0000000000000ffUL
/* Bits to mask out from a PGD to get to the PUD page */
#define PGD_MASKED_BITS		0xc0000000000000ffUL
66

67
#ifndef __ASSEMBLY__
68
#include <asm/errno.h>
69

70 71 72 73 74 75
/*
 * With 64K pages on hash table, we have a special PTE format that
 * uses a second "half" of the page table to encode sub-page information
 * in order to deal with 64K made of 4K HW pages. Thus we override the
 * generic accessors and iterators here
 */
76 77 78 79
#define __real_pte __real_pte
static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep)
{
	real_pte_t rpte;
80
	unsigned long *hidxp;
81 82 83

	rpte.pte = pte;
	rpte.hidx = 0;
84
	if (pte_val(pte) & H_PAGE_COMBO) {
85
		/*
86
		 * Make sure we order the hidx load against the H_PAGE_COMBO
87 88 89
		 * check. The store side ordering is done in __hash_page_4K
		 */
		smp_rmb();
90 91
		hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
		rpte.hidx = *hidxp;
92 93 94 95 96 97
	}
	return rpte;
}

static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)
{
98
	if ((pte_val(rpte.pte) & H_PAGE_COMBO))
99
		return (rpte.hidx >> (index<<2)) & 0xf;
100
	return (pte_val(rpte.pte) >> H_PAGE_F_GIX_SHIFT) & 0xf;
101 102
}

103
#define __rpte_to_pte(r)	((r).pte)
104
extern bool __rpte_sub_valid(real_pte_t rpte, unsigned long index);
105 106
/*
 * Trick: we set __end to va + 64k, which happens works for
107 108
 * a 16M page as well as we want only one iteration
 */
109 110 111 112 113 114 115 116 117 118
#define pte_iterate_hashed_subpages(rpte, psize, vpn, index, shift)	\
	do {								\
		unsigned long __end = vpn + (1UL << (PAGE_SHIFT - VPN_SHIFT));	\
		unsigned __split = (psize == MMU_PAGE_4K ||		\
				    psize == MMU_PAGE_64K_AP);		\
		shift = mmu_psize_defs[psize].shift;			\
		for (index = 0; vpn < __end; index++,			\
			     vpn += (1L << (shift - VPN_SHIFT))) {	\
			if (!__split || __rpte_sub_valid(rpte, index))	\
				do {
119 120 121

#define pte_iterate_hashed_end() } while(0); } } while(0)

122
#define pte_pagesize_index(mm, addr, pte)	\
123
	(((pte) & H_PAGE_COMBO)? MMU_PAGE_4K: MMU_PAGE_64K)
124

125 126 127 128 129 130 131 132 133 134
extern int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
			   unsigned long pfn, unsigned long size, pgprot_t);
static inline int remap_4k_pfn(struct vm_area_struct *vma, unsigned long addr,
			       unsigned long pfn, pgprot_t prot)
{
	if (pfn > (PTE_RPN_MASK >> PAGE_SHIFT)) {
		WARN(1, "remap_4k_pfn called with wrong pfn value\n");
		return -EINVAL;
	}
	return remap_pfn_range(vma, addr, pfn, PAGE_SIZE,
135
			       __pgprot(pgprot_val(prot) | H_PAGE_4K_PFN));
136
}
137

138 139 140 141
#define PTE_TABLE_SIZE	PTE_FRAG_SIZE
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
#define PMD_TABLE_SIZE	((sizeof(pmd_t) << PMD_INDEX_SIZE) + (sizeof(unsigned long) << PMD_INDEX_SIZE))
#else
142
#define PMD_TABLE_SIZE	(sizeof(pmd_t) << PMD_INDEX_SIZE)
143
#endif
144
#define PUD_TABLE_SIZE	(sizeof(pud_t) << PUD_INDEX_SIZE)
145 146
#define PGD_TABLE_SIZE	(sizeof(pgd_t) << PGD_INDEX_SIZE)

147 148 149 150 151 152 153 154 155 156 157
#ifdef CONFIG_HUGETLB_PAGE
/*
 * We have PGD_INDEX_SIZ = 12 and PTE_INDEX_SIZE = 8, so that we can have
 * 16GB hugepage pte in PGD and 16MB hugepage pte at PMD;
 *
 * Defined in such a way that we can optimize away code block at build time
 * if CONFIG_HUGETLB_PAGE=n.
 */
static inline int pmd_huge(pmd_t pmd)
{
	/*
A
Aneesh Kumar K.V 已提交
158
	 * leaf pte for huge page
159
	 */
A
Aneesh Kumar K.V 已提交
160
	return !!(pmd_val(pmd) & _PAGE_PTE);
161 162 163 164 165
}

static inline int pud_huge(pud_t pud)
{
	/*
A
Aneesh Kumar K.V 已提交
166
	 * leaf pte for huge page
167
	 */
A
Aneesh Kumar K.V 已提交
168
	return !!(pud_val(pud) & _PAGE_PTE);
169 170 171 172 173
}

static inline int pgd_huge(pgd_t pgd)
{
	/*
A
Aneesh Kumar K.V 已提交
174
	 * leaf pte for huge page
175
	 */
A
Aneesh Kumar K.V 已提交
176
	return !!(pgd_val(pgd) & _PAGE_PTE);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
}
#define pgd_huge pgd_huge

#ifdef CONFIG_DEBUG_VM
extern int hugepd_ok(hugepd_t hpd);
#define is_hugepd(hpd)               (hugepd_ok(hpd))
#else
/*
 * With 64k page size, we have hugepage ptes in the pgd and pmd entries. We don't
 * need to setup hugepage directory for them. Our pte and page directory format
 * enable us to have this enabled.
 */
static inline int hugepd_ok(hugepd_t hpd)
{
	return 0;
}
#define is_hugepd(pdep)			0
#endif /* CONFIG_DEBUG_VM */

#endif /* CONFIG_HUGETLB_PAGE */

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
extern unsigned long pmd_hugepage_update(struct mm_struct *mm,
					 unsigned long addr,
					 pmd_t *pmdp,
					 unsigned long clr,
					 unsigned long set);
static inline char *get_hpte_slot_array(pmd_t *pmdp)
{
	/*
	 * The hpte hindex is stored in the pgtable whose address is in the
	 * second half of the PMD
	 *
	 * Order this load with the test for pmd_trans_huge in the caller
	 */
	smp_rmb();
	return *(char **)(pmdp + PTRS_PER_PMD);


}
/*
 * The linux hugepage PMD now include the pmd entries followed by the address
 * to the stashed pgtable_t. The stashed pgtable_t contains the hpte bits.
220
 * [ 000 | 1 bit secondary | 3 bit hidx | 1 bit valid]. We use one byte per
221 222 223
 * each HPTE entry. With 16MB hugepage and 64K HPTE we need 256 entries and
 * with 4K HPTE we need 4096 entries. Both will fit in a 4K pgtable_t.
 *
224
 * The top three bits are intentionally left as zero. This memory location
225 226 227 228 229 230
 * are also used as normal page PTE pointers. So if we have any pointers
 * left around while we collapse a hugepage, we need to make sure
 * _PAGE_PRESENT bit of that is zero when we look at them
 */
static inline unsigned int hpte_valid(unsigned char *hpte_slot_array, int index)
{
231
	return hpte_slot_array[index] & 0x1;
232 233 234 235 236
}

static inline unsigned int hpte_hash_index(unsigned char *hpte_slot_array,
					   int index)
{
237
	return hpte_slot_array[index] >> 1;
238 239 240 241 242
}

static inline void mark_hpte_slot_valid(unsigned char *hpte_slot_array,
					unsigned int index, unsigned int hidx)
{
243
	hpte_slot_array[index] = (hidx << 1) | 0x1;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
}

/*
 *
 * For core kernel code by design pmd_trans_huge is never run on any hugetlbfs
 * page. The hugetlbfs page table walking and mangling paths are totally
 * separated form the core VM paths and they're differentiated by
 *  VM_HUGETLB being set on vm_flags well before any pmd_trans_huge could run.
 *
 * pmd_trans_huge() is defined as false at build time if
 * CONFIG_TRANSPARENT_HUGEPAGE=n to optimize away code blocks at build
 * time in such case.
 *
 * For ppc64 we need to differntiate from explicit hugepages from THP, because
 * for THP we also track the subpage details at the pmd level. We don't do
 * that for explicit huge pages.
 *
 */
static inline int pmd_trans_huge(pmd_t pmd)
{
264 265
	return !!((pmd_val(pmd) & (_PAGE_PTE | H_PAGE_THP_HUGE)) ==
		  (_PAGE_PTE | H_PAGE_THP_HUGE));
266 267 268 269
}

static inline int pmd_large(pmd_t pmd)
{
A
Aneesh Kumar K.V 已提交
270
	return !!(pmd_val(pmd) & _PAGE_PTE);
271 272 273 274 275 276 277 278 279 280
}

static inline pmd_t pmd_mknotpresent(pmd_t pmd)
{
	return __pmd(pmd_val(pmd) & ~_PAGE_PRESENT);
}

#define __HAVE_ARCH_PMD_SAME
static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
{
281
	return (((pmd_raw(pmd_a) ^ pmd_raw(pmd_b)) & ~cpu_to_be64(_PAGE_HPTEFLAGS)) == 0);
282 283 284 285 286 287 288
}

static inline int __pmdp_test_and_clear_young(struct mm_struct *mm,
					      unsigned long addr, pmd_t *pmdp)
{
	unsigned long old;

289
	if ((pmd_val(*pmdp) & (_PAGE_ACCESSED | H_PAGE_HASHPTE)) == 0)
290 291 292 293 294 295 296 297 298 299
		return 0;
	old = pmd_hugepage_update(mm, addr, pmdp, _PAGE_ACCESSED, 0);
	return ((old & _PAGE_ACCESSED) != 0);
}

#define __HAVE_ARCH_PMDP_SET_WRPROTECT
static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long addr,
				      pmd_t *pmdp)
{

300
	if ((pmd_val(*pmdp) & _PAGE_WRITE) == 0)
301 302
		return;

303
	pmd_hugepage_update(mm, addr, pmdp, _PAGE_WRITE, 0);
304 305 306
}

#endif /*  CONFIG_TRANSPARENT_HUGEPAGE */
307
#endif	/* __ASSEMBLY__ */
308 309

#endif /* _ASM_POWERPC_BOOK3S_64_HASH_64K_H */