Path: blob/master/arch/powerpc/include/asm/book3s/64/hash-64k.h
26519 views
/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _ASM_POWERPC_BOOK3S_64_HASH_64K_H2#define _ASM_POWERPC_BOOK3S_64_HASH_64K_H34#define H_PTE_INDEX_SIZE 8 // size: 8B << 8 = 2KB, maps 2^8 x 64KB = 16MB5#define H_PMD_INDEX_SIZE 10 // size: 8B << 10 = 8KB, maps 2^10 x 16MB = 16GB6#define H_PUD_INDEX_SIZE 10 // size: 8B << 10 = 8KB, maps 2^10 x 16GB = 16TB7#define H_PGD_INDEX_SIZE 8 // size: 8B << 8 = 2KB, maps 2^8 x 16TB = 4PB89/*10* If we store section details in page->flags we can't increase the MAX_PHYSMEM_BITS11* if we increase SECTIONS_WIDTH we will not store node details in page->flags and12* page_to_nid does a page->section->node lookup13* Hence only increase for VMEMMAP. Further depending on SPARSEMEM_EXTREME reduce14* memory requirements with large number of sections.15* 51 bits is the max physical real address on POWER916*/17#if defined(CONFIG_SPARSEMEM_VMEMMAP) && defined(CONFIG_SPARSEMEM_EXTREME)18#define H_MAX_PHYSMEM_BITS 5119#else20#define H_MAX_PHYSMEM_BITS 4621#endif2223/*24* Each context is 512TB size. SLB miss for first context/default context25* is handled in the hotpath.26*/27#define MAX_EA_BITS_PER_CONTEXT 4928#define REGION_SHIFT MAX_EA_BITS_PER_CONTEXT2930/*31* We use one context for each MAP area.32*/33#define H_KERN_MAP_SIZE (1UL << MAX_EA_BITS_PER_CONTEXT)3435/*36* Define the address range of the kernel non-linear virtual area37* 2PB38*/39#define H_KERN_VIRT_START ASM_CONST(0xc008000000000000)4041/*42* 64k aligned address free up few of the lower bits of RPN for us43* We steal that here. For more deatils look at pte_pfn/pfn_pte()44*/45#define H_PAGE_COMBO _RPAGE_RPN0 /* this is a combo 4k page */46#define H_PAGE_4K_PFN _RPAGE_RPN1 /* PFN is for a single 4k page */47#define H_PAGE_BUSY _RPAGE_RSV1 /* software: PTE & hash are busy */48#define H_PAGE_HASHPTE _RPAGE_RPN43 /* PTE has associated HPTE */4950/* memory key bits. */51#define H_PTE_PKEY_BIT4 _RPAGE_PKEY_BIT452#define H_PTE_PKEY_BIT3 _RPAGE_PKEY_BIT353#define H_PTE_PKEY_BIT2 _RPAGE_PKEY_BIT254#define H_PTE_PKEY_BIT1 _RPAGE_PKEY_BIT155#define H_PTE_PKEY_BIT0 _RPAGE_PKEY_BIT05657/*58* We need to differentiate between explicit huge page and THP huge59* page, since THP huge page also need to track real subpage details60*/61#define H_PAGE_THP_HUGE H_PAGE_4K_PFN6263/* PTE flags to conserve for HPTE identification */64#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO)65/*66* We use a 2K PTE page fragment and another 2K for storing67* real_pte_t hash index68* 8 bytes per each pte entry and another 8 bytes for storing69* slot details.70*/71#define H_PTE_FRAG_SIZE_SHIFT (H_PTE_INDEX_SIZE + 3 + 1)72#define H_PTE_FRAG_NR (PAGE_SIZE >> H_PTE_FRAG_SIZE_SHIFT)7374#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE)75#define H_PMD_FRAG_SIZE_SHIFT (H_PMD_INDEX_SIZE + 3 + 1)76#else77#define H_PMD_FRAG_SIZE_SHIFT (H_PMD_INDEX_SIZE + 3)78#endif79#define H_PMD_FRAG_NR (PAGE_SIZE >> H_PMD_FRAG_SIZE_SHIFT)8081#ifndef __ASSEMBLY__82#include <asm/errno.h>8384/*85* With 64K pages on hash table, we have a special PTE format that86* uses a second "half" of the page table to encode sub-page information87* in order to deal with 64K made of 4K HW pages. Thus we override the88* generic accessors and iterators here89*/90#define __real_pte __real_pte91static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep, int offset)92{93real_pte_t rpte;94unsigned long *hidxp;9596rpte.pte = pte;9798/*99* Ensure that we do not read the hidx before we read the PTE. Because100* the writer side is expected to finish writing the hidx first followed101* by the PTE, by using smp_wmb(). pte_set_hash_slot() ensures that.102*/103smp_rmb();104105hidxp = (unsigned long *)(ptep + offset);106rpte.hidx = *hidxp;107return rpte;108}109110/*111* shift the hidx representation by one-modulo-0xf; i.e hidx 0 is respresented112* as 1, 1 as 2,... , and 0xf as 0. This convention lets us represent a113* invalid hidx 0xf with a 0x0 bit value. PTEs are anyway zero'd when114* allocated. We dont have to zero them gain; thus save on the initialization.115*/116#define HIDX_UNSHIFT_BY_ONE(x) ((x + 0xfUL) & 0xfUL) /* shift backward by one */117#define HIDX_SHIFT_BY_ONE(x) ((x + 0x1UL) & 0xfUL) /* shift forward by one */118#define HIDX_BITS(x, index) (x << (index << 2))119#define BITS_TO_HIDX(x, index) ((x >> (index << 2)) & 0xfUL)120#define INVALID_RPTE_HIDX 0x0UL121122static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)123{124return HIDX_UNSHIFT_BY_ONE(BITS_TO_HIDX(rpte.hidx, index));125}126127/*128* Commit the hidx and return PTE bits that needs to be modified. The caller is129* expected to modify the PTE bits accordingly and commit the PTE to memory.130*/131static inline unsigned long pte_set_hidx(pte_t *ptep, real_pte_t rpte,132unsigned int subpg_index,133unsigned long hidx, int offset)134{135unsigned long *hidxp = (unsigned long *)(ptep + offset);136137rpte.hidx &= ~HIDX_BITS(0xfUL, subpg_index);138*hidxp = rpte.hidx | HIDX_BITS(HIDX_SHIFT_BY_ONE(hidx), subpg_index);139140/*141* Anyone reading PTE must ensure hidx bits are read after reading the142* PTE by using the read-side barrier smp_rmb(). __real_pte() can be143* used for that.144*/145smp_wmb();146147/* No PTE bits to be modified, return 0x0UL */148return 0x0UL;149}150151#define __rpte_to_pte(r) ((r).pte)152extern bool __rpte_sub_valid(real_pte_t rpte, unsigned long index);153/*154* Trick: we set __end to va + 64k, which happens works for155* a 16M page as well as we want only one iteration156*/157#define pte_iterate_hashed_subpages(rpte, psize, vpn, index, shift) \158do { \159unsigned long __end = vpn + (1UL << (PAGE_SHIFT - VPN_SHIFT)); \160unsigned __split = (psize == MMU_PAGE_4K || \161psize == MMU_PAGE_64K_AP); \162shift = mmu_psize_defs[psize].shift; \163for (index = 0; vpn < __end; index++, \164vpn += (1L << (shift - VPN_SHIFT))) { \165if (!__split || __rpte_sub_valid(rpte, index))166167#define pte_iterate_hashed_end() } } while(0)168169#define pte_pagesize_index(mm, addr, pte) \170(((pte) & H_PAGE_COMBO)? MMU_PAGE_4K: MMU_PAGE_64K)171172extern int remap_pfn_range(struct vm_area_struct *, unsigned long addr,173unsigned long pfn, unsigned long size, pgprot_t);174static inline int hash__remap_4k_pfn(struct vm_area_struct *vma, unsigned long addr,175unsigned long pfn, pgprot_t prot)176{177if (pfn > (PTE_RPN_MASK >> PAGE_SHIFT)) {178WARN(1, "remap_4k_pfn called with wrong pfn value\n");179return -EINVAL;180}181return remap_pfn_range(vma, addr, pfn, PAGE_SIZE,182__pgprot(pgprot_val(prot) | H_PAGE_4K_PFN));183}184185#define H_PTE_TABLE_SIZE PTE_FRAG_SIZE186#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined (CONFIG_HUGETLB_PAGE)187#define H_PMD_TABLE_SIZE ((sizeof(pmd_t) << PMD_INDEX_SIZE) + \188(sizeof(unsigned long) << PMD_INDEX_SIZE))189#else190#define H_PMD_TABLE_SIZE (sizeof(pmd_t) << PMD_INDEX_SIZE)191#endif192#ifdef CONFIG_HUGETLB_PAGE193#define H_PUD_TABLE_SIZE ((sizeof(pud_t) << PUD_INDEX_SIZE) + \194(sizeof(unsigned long) << PUD_INDEX_SIZE))195#else196#define H_PUD_TABLE_SIZE (sizeof(pud_t) << PUD_INDEX_SIZE)197#endif198#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << PGD_INDEX_SIZE)199200#ifdef CONFIG_TRANSPARENT_HUGEPAGE201static inline char *get_hpte_slot_array(pmd_t *pmdp)202{203/*204* The hpte hindex is stored in the pgtable whose address is in the205* second half of the PMD206*207* Order this load with the test for pmd_trans_huge in the caller208*/209smp_rmb();210return *(char **)(pmdp + PTRS_PER_PMD);211212213}214/*215* The linux hugepage PMD now include the pmd entries followed by the address216* to the stashed pgtable_t. The stashed pgtable_t contains the hpte bits.217* [ 000 | 1 bit secondary | 3 bit hidx | 1 bit valid]. We use one byte per218* each HPTE entry. With 16MB hugepage and 64K HPTE we need 256 entries and219* with 4K HPTE we need 4096 entries. Both will fit in a 4K pgtable_t.220*221* The top three bits are intentionally left as zero. This memory location222* are also used as normal page PTE pointers. So if we have any pointers223* left around while we collapse a hugepage, we need to make sure224* _PAGE_PRESENT bit of that is zero when we look at them225*/226static inline unsigned int hpte_valid(unsigned char *hpte_slot_array, int index)227{228return hpte_slot_array[index] & 0x1;229}230231static inline unsigned int hpte_hash_index(unsigned char *hpte_slot_array,232int index)233{234return hpte_slot_array[index] >> 1;235}236237static inline void mark_hpte_slot_valid(unsigned char *hpte_slot_array,238unsigned int index, unsigned int hidx)239{240hpte_slot_array[index] = (hidx << 1) | 0x1;241}242243/*244*245* For core kernel code by design pmd_trans_huge is never run on any hugetlbfs246* page. The hugetlbfs page table walking and mangling paths are totally247* separated form the core VM paths and they're differentiated by248* VM_HUGETLB being set on vm_flags well before any pmd_trans_huge could run.249*250* pmd_trans_huge() is defined as false at build time if251* CONFIG_TRANSPARENT_HUGEPAGE=n to optimize away code blocks at build252* time in such case.253*254* For ppc64 we need to differntiate from explicit hugepages from THP, because255* for THP we also track the subpage details at the pmd level. We don't do256* that for explicit huge pages.257*258*/259static inline int hash__pmd_trans_huge(pmd_t pmd)260{261return !!((pmd_val(pmd) & (_PAGE_PTE | H_PAGE_THP_HUGE)) ==262(_PAGE_PTE | H_PAGE_THP_HUGE));263}264265static inline pmd_t hash__pmd_mkhuge(pmd_t pmd)266{267return __pmd(pmd_val(pmd) | (_PAGE_PTE | H_PAGE_THP_HUGE));268}269270extern unsigned long hash__pmd_hugepage_update(struct mm_struct *mm,271unsigned long addr, pmd_t *pmdp,272unsigned long clr, unsigned long set);273extern pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma,274unsigned long address, pmd_t *pmdp);275extern void hash__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,276pgtable_t pgtable);277extern pgtable_t hash__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);278extern pmd_t hash__pmdp_huge_get_and_clear(struct mm_struct *mm,279unsigned long addr, pmd_t *pmdp);280extern int hash__has_transparent_hugepage(void);281#endif /* CONFIG_TRANSPARENT_HUGEPAGE */282283#endif /* __ASSEMBLY__ */284285#endif /* _ASM_POWERPC_BOOK3S_64_HASH_64K_H */286287288