1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
226b6a3d9SAneesh Kumar K.V #ifndef _ASM_POWERPC_BOOK3S_64_HASH_H
326b6a3d9SAneesh Kumar K.V #define _ASM_POWERPC_BOOK3S_64_HASH_H
426b6a3d9SAneesh Kumar K.V #ifdef __KERNEL__
526b6a3d9SAneesh Kumar K.V 
6ec0c464cSChristophe Leroy #include <asm/asm-const.h>
7ec0c464cSChristophe Leroy 
8e34aa03cSAneesh Kumar K.V /*
9e34aa03cSAneesh Kumar K.V  * Common bits between 4K and 64K pages in a linux-style PTE.
101ec3f937SPaul Mackerras  * Additional bits may be defined in pgtable-hash64-*.h
11e34aa03cSAneesh Kumar K.V  *
12e34aa03cSAneesh Kumar K.V  */
13d2cf0050SAneesh Kumar K.V #define H_PTE_NONE_MASK		_PAGE_HPTEFLAGS
14e34aa03cSAneesh Kumar K.V 
15371352caSAneesh Kumar K.V #ifdef CONFIG_PPC_64K_PAGES
16371352caSAneesh Kumar K.V #include <asm/book3s/64/hash-64k.h>
17371352caSAneesh Kumar K.V #else
18371352caSAneesh Kumar K.V #include <asm/book3s/64/hash-4k.h>
19371352caSAneesh Kumar K.V #endif
20371352caSAneesh Kumar K.V 
2141b7a347SDaniel Axtens #define H_PTRS_PER_PTE		(1 << H_PTE_INDEX_SIZE)
2241b7a347SDaniel Axtens #define H_PTRS_PER_PMD		(1 << H_PMD_INDEX_SIZE)
2341b7a347SDaniel Axtens #define H_PTRS_PER_PUD		(1 << H_PUD_INDEX_SIZE)
2441b7a347SDaniel Axtens 
25da7ad366SAneesh Kumar K.V /* Bits to set in a PMD/PUD/PGD entry valid bit*/
26da7ad366SAneesh Kumar K.V #define HASH_PMD_VAL_BITS		(0x8000000000000000UL)
27da7ad366SAneesh Kumar K.V #define HASH_PUD_VAL_BITS		(0x8000000000000000UL)
28da7ad366SAneesh Kumar K.V #define HASH_PGD_VAL_BITS		(0x8000000000000000UL)
29da7ad366SAneesh Kumar K.V 
30371352caSAneesh Kumar K.V /*
31371352caSAneesh Kumar K.V  * Size of EA range mapped by our pagetables.
32371352caSAneesh Kumar K.V  */
33dd1842a2SAneesh Kumar K.V #define H_PGTABLE_EADDR_SIZE	(H_PTE_INDEX_SIZE + H_PMD_INDEX_SIZE + \
34dd1842a2SAneesh Kumar K.V 				 H_PUD_INDEX_SIZE + H_PGD_INDEX_SIZE + PAGE_SHIFT)
35dd1842a2SAneesh Kumar K.V #define H_PGTABLE_RANGE		(ASM_CONST(1) << H_PGTABLE_EADDR_SIZE)
360034d395SAneesh Kumar K.V /*
370034d395SAneesh Kumar K.V  * Top 2 bits are ignored in page table walk.
380034d395SAneesh Kumar K.V  */
390034d395SAneesh Kumar K.V #define EA_MASK			(~(0xcUL << 60))
40371352caSAneesh Kumar K.V 
41371352caSAneesh Kumar K.V /*
42fae22116SAneesh Kumar K.V  * We store the slot details in the second half of page table.
43fae22116SAneesh Kumar K.V  * Increase the pud level table so that hugetlb ptes can be stored
44fae22116SAneesh Kumar K.V  * at pud level.
45fae22116SAneesh Kumar K.V  */
46fae22116SAneesh Kumar K.V #if defined(CONFIG_HUGETLB_PAGE) &&  defined(CONFIG_PPC_64K_PAGES)
47fae22116SAneesh Kumar K.V #define H_PUD_CACHE_INDEX	(H_PUD_INDEX_SIZE + 1)
48fae22116SAneesh Kumar K.V #else
49fae22116SAneesh Kumar K.V #define H_PUD_CACHE_INDEX	(H_PUD_INDEX_SIZE)
50fae22116SAneesh Kumar K.V #endif
51371352caSAneesh Kumar K.V 
52371352caSAneesh Kumar K.V /*
530034d395SAneesh Kumar K.V  * +------------------------------+
540034d395SAneesh Kumar K.V  * |                              |
550034d395SAneesh Kumar K.V  * |                              |
560034d395SAneesh Kumar K.V  * |                              |
570034d395SAneesh Kumar K.V  * +------------------------------+  Kernel virtual map end (0xc00e000000000000)
580034d395SAneesh Kumar K.V  * |                              |
590034d395SAneesh Kumar K.V  * |                              |
600034d395SAneesh Kumar K.V  * |      512TB/16TB of vmemmap   |
610034d395SAneesh Kumar K.V  * |                              |
620034d395SAneesh Kumar K.V  * |                              |
630034d395SAneesh Kumar K.V  * +------------------------------+  Kernel vmemmap  start
640034d395SAneesh Kumar K.V  * |                              |
650034d395SAneesh Kumar K.V  * |      512TB/16TB of IO map    |
660034d395SAneesh Kumar K.V  * |                              |
670034d395SAneesh Kumar K.V  * +------------------------------+  Kernel IO map start
680034d395SAneesh Kumar K.V  * |                              |
690034d395SAneesh Kumar K.V  * |      512TB/16TB of vmap      |
700034d395SAneesh Kumar K.V  * |                              |
710034d395SAneesh Kumar K.V  * +------------------------------+  Kernel virt start (0xc008000000000000)
720034d395SAneesh Kumar K.V  * |                              |
730034d395SAneesh Kumar K.V  * |                              |
740034d395SAneesh Kumar K.V  * |                              |
750034d395SAneesh Kumar K.V  * +------------------------------+  Kernel linear (0xc.....)
763d8810e0SMichael Ellerman  */
773d8810e0SMichael Ellerman 
78d6a9996eSAneesh Kumar K.V #define H_VMALLOC_START		H_KERN_VIRT_START
790034d395SAneesh Kumar K.V #define H_VMALLOC_SIZE		H_KERN_MAP_SIZE
80d6a9996eSAneesh Kumar K.V #define H_VMALLOC_END		(H_VMALLOC_START + H_VMALLOC_SIZE)
81371352caSAneesh Kumar K.V 
8263ee9b2fSMichael Ellerman #define H_KERN_IO_START		H_VMALLOC_END
830034d395SAneesh Kumar K.V #define H_KERN_IO_SIZE		H_KERN_MAP_SIZE
840034d395SAneesh Kumar K.V #define H_KERN_IO_END		(H_KERN_IO_START + H_KERN_IO_SIZE)
850034d395SAneesh Kumar K.V 
860034d395SAneesh Kumar K.V #define H_VMEMMAP_START		H_KERN_IO_END
870034d395SAneesh Kumar K.V #define H_VMEMMAP_SIZE		H_KERN_MAP_SIZE
880034d395SAneesh Kumar K.V #define H_VMEMMAP_END		(H_VMEMMAP_START + H_VMEMMAP_SIZE)
8963ee9b2fSMichael Ellerman 
901c946c1bSAneesh Kumar K.V #define NON_LINEAR_REGION_ID(ea)	((((unsigned long)ea - H_KERN_VIRT_START) >> REGION_SHIFT) + 2)
911c946c1bSAneesh Kumar K.V 
92371352caSAneesh Kumar K.V /*
93371352caSAneesh Kumar K.V  * Region IDs
94371352caSAneesh Kumar K.V  */
951c946c1bSAneesh Kumar K.V #define USER_REGION_ID		0
965f53d286SAneesh Kumar K.V #define LINEAR_MAP_REGION_ID	1
971c946c1bSAneesh Kumar K.V #define VMALLOC_REGION_ID	NON_LINEAR_REGION_ID(H_VMALLOC_START)
981c946c1bSAneesh Kumar K.V #define IO_REGION_ID		NON_LINEAR_REGION_ID(H_KERN_IO_START)
991c946c1bSAneesh Kumar K.V #define VMEMMAP_REGION_ID	NON_LINEAR_REGION_ID(H_VMEMMAP_START)
100c179976cSAneesh Kumar K.V #define INVALID_REGION_ID	(VMEMMAP_REGION_ID + 1)
101371352caSAneesh Kumar K.V 
102371352caSAneesh Kumar K.V /*
103371352caSAneesh Kumar K.V  * Defines the address of the vmemap area, in its own region on
104371352caSAneesh Kumar K.V  * hash table CPUs.
105371352caSAneesh Kumar K.V  */
10626b6a3d9SAneesh Kumar K.V 
10726b6a3d9SAneesh Kumar K.V /* PTEIDX nibble */
10826b6a3d9SAneesh Kumar K.V #define _PTEIDX_SECONDARY	0x8
10926b6a3d9SAneesh Kumar K.V #define _PTEIDX_GROUP_IX	0x7
11026b6a3d9SAneesh Kumar K.V 
111ac94ac79SAneesh Kumar K.V #define H_PMD_BAD_BITS		(PTE_TABLE_SIZE-1)
112ac94ac79SAneesh Kumar K.V #define H_PUD_BAD_BITS		(PMD_TABLE_SIZE-1)
113371352caSAneesh Kumar K.V 
114371352caSAneesh Kumar K.V #ifndef __ASSEMBLY__
get_region_id(unsigned long ea)1150034d395SAneesh Kumar K.V static inline int get_region_id(unsigned long ea)
1160034d395SAneesh Kumar K.V {
1171c946c1bSAneesh Kumar K.V 	int region_id;
1180034d395SAneesh Kumar K.V 	int id = (ea >> 60UL);
1190034d395SAneesh Kumar K.V 
1200034d395SAneesh Kumar K.V 	if (id == 0)
1210034d395SAneesh Kumar K.V 		return USER_REGION_ID;
1220034d395SAneesh Kumar K.V 
123c179976cSAneesh Kumar K.V 	if (id != (PAGE_OFFSET >> 60))
124c179976cSAneesh Kumar K.V 		return INVALID_REGION_ID;
125c179976cSAneesh Kumar K.V 
1261c946c1bSAneesh Kumar K.V 	if (ea < H_KERN_VIRT_START)
1275f53d286SAneesh Kumar K.V 		return LINEAR_MAP_REGION_ID;
1281c946c1bSAneesh Kumar K.V 
1291c946c1bSAneesh Kumar K.V 	BUILD_BUG_ON(NON_LINEAR_REGION_ID(H_VMALLOC_START) != 2);
1301c946c1bSAneesh Kumar K.V 
1311c946c1bSAneesh Kumar K.V 	region_id = NON_LINEAR_REGION_ID(ea);
1321c946c1bSAneesh Kumar K.V 	return region_id;
1330034d395SAneesh Kumar K.V }
1340034d395SAneesh Kumar K.V 
hash__pmd_same(pmd_t pmd_a,pmd_t pmd_b)135cf53564bSAneesh Kumar K.V static inline int hash__pmd_same(pmd_t pmd_a, pmd_t pmd_b)
136cf53564bSAneesh Kumar K.V {
137cf53564bSAneesh Kumar K.V 	return (((pmd_raw(pmd_a) ^ pmd_raw(pmd_b)) & ~cpu_to_be64(_PAGE_HPTEFLAGS)) == 0);
138cf53564bSAneesh Kumar K.V }
139cf53564bSAneesh Kumar K.V 
140ac94ac79SAneesh Kumar K.V #define	hash__pmd_bad(pmd)		(pmd_val(pmd) & H_PMD_BAD_BITS)
141*27af67f3SAneesh Kumar K.V 
142*27af67f3SAneesh Kumar K.V /*
143*27af67f3SAneesh Kumar K.V  * pud comparison that will work with both pte and page table pointer.
144*27af67f3SAneesh Kumar K.V  */
hash__pud_same(pud_t pud_a,pud_t pud_b)145*27af67f3SAneesh Kumar K.V static inline int hash__pud_same(pud_t pud_a, pud_t pud_b)
146*27af67f3SAneesh Kumar K.V {
147*27af67f3SAneesh Kumar K.V 	return (((pud_raw(pud_a) ^ pud_raw(pud_b)) & ~cpu_to_be64(_PAGE_HPTEFLAGS)) == 0);
148*27af67f3SAneesh Kumar K.V }
149ac94ac79SAneesh Kumar K.V #define	hash__pud_bad(pud)		(pud_val(pud) & H_PUD_BAD_BITS)
150*27af67f3SAneesh Kumar K.V 
hash__p4d_bad(p4d_t p4d)1512fb47060SMike Rapoport static inline int hash__p4d_bad(p4d_t p4d)
152ac94ac79SAneesh Kumar K.V {
1532fb47060SMike Rapoport 	return (p4d_val(p4d) == 0);
154ac94ac79SAneesh Kumar K.V }
155cd65d697SBalbir Singh #ifdef CONFIG_STRICT_KERNEL_RWX
156cd65d697SBalbir Singh extern void hash__mark_rodata_ro(void);
157029d9252SMichael Ellerman extern void hash__mark_initmem_nx(void);
158cd65d697SBalbir Singh #endif
159371352caSAneesh Kumar K.V 
160371352caSAneesh Kumar K.V extern void hpte_need_flush(struct mm_struct *mm, unsigned long addr,
161371352caSAneesh Kumar K.V 			    pte_t *ptep, unsigned long pte, int huge);
162d94b827eSAneesh Kumar K.V unsigned long htab_convert_pte_flags(unsigned long pteflags, unsigned long flags);
163371352caSAneesh Kumar K.V /* Atomic PTE updates */
hash__pte_update(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned long clr,unsigned long set,int huge)164ac94ac79SAneesh Kumar K.V static inline unsigned long hash__pte_update(struct mm_struct *mm,
165371352caSAneesh Kumar K.V 					 unsigned long addr,
166371352caSAneesh Kumar K.V 					 pte_t *ptep, unsigned long clr,
167371352caSAneesh Kumar K.V 					 unsigned long set,
168371352caSAneesh Kumar K.V 					 int huge)
169371352caSAneesh Kumar K.V {
1705dc1ef85SAneesh Kumar K.V 	__be64 old_be, tmp_be;
1715dc1ef85SAneesh Kumar K.V 	unsigned long old;
172371352caSAneesh Kumar K.V 
173371352caSAneesh Kumar K.V 	__asm__ __volatile__(
174371352caSAneesh Kumar K.V 	"1:	ldarx	%0,0,%3		# pte_update\n\
1755dc1ef85SAneesh Kumar K.V 	and.	%1,%0,%6\n\
176371352caSAneesh Kumar K.V 	bne-	1b \n\
177371352caSAneesh Kumar K.V 	andc	%1,%0,%4 \n\
178371352caSAneesh Kumar K.V 	or	%1,%1,%7\n\
179371352caSAneesh Kumar K.V 	stdcx.	%1,0,%3 \n\
180371352caSAneesh Kumar K.V 	bne-	1b"
1815dc1ef85SAneesh Kumar K.V 	: "=&r" (old_be), "=&r" (tmp_be), "=m" (*ptep)
1825dc1ef85SAneesh Kumar K.V 	: "r" (ptep), "r" (cpu_to_be64(clr)), "m" (*ptep),
183945537dfSAneesh Kumar K.V 	  "r" (cpu_to_be64(H_PAGE_BUSY)), "r" (cpu_to_be64(set))
184371352caSAneesh Kumar K.V 	: "cc" );
185371352caSAneesh Kumar K.V 	/* huge pages use the old page table lock */
186371352caSAneesh Kumar K.V 	if (!huge)
187371352caSAneesh Kumar K.V 		assert_pte_locked(mm, addr);
188371352caSAneesh Kumar K.V 
1895dc1ef85SAneesh Kumar K.V 	old = be64_to_cpu(old_be);
190945537dfSAneesh Kumar K.V 	if (old & H_PAGE_HASHPTE)
191371352caSAneesh Kumar K.V 		hpte_need_flush(mm, addr, ptep, old, huge);
192371352caSAneesh Kumar K.V 
193371352caSAneesh Kumar K.V 	return old;
194371352caSAneesh Kumar K.V }
195371352caSAneesh Kumar K.V 
196371352caSAneesh Kumar K.V /* Set the dirty and/or accessed bits atomically in a linux PTE, this
197371352caSAneesh Kumar K.V  * function doesn't need to flush the hash entry
198371352caSAneesh Kumar K.V  */
hash__ptep_set_access_flags(pte_t * ptep,pte_t entry)199ac94ac79SAneesh Kumar K.V static inline void hash__ptep_set_access_flags(pte_t *ptep, pte_t entry)
200371352caSAneesh Kumar K.V {
2015dc1ef85SAneesh Kumar K.V 	__be64 old, tmp, val, mask;
202371352caSAneesh Kumar K.V 
203c7d54842SAneesh Kumar K.V 	mask = cpu_to_be64(_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_READ | _PAGE_WRITE |
2045dc1ef85SAneesh Kumar K.V 			   _PAGE_EXEC | _PAGE_SOFT_DIRTY);
2055dc1ef85SAneesh Kumar K.V 
2065dc1ef85SAneesh Kumar K.V 	val = pte_raw(entry) & mask;
207371352caSAneesh Kumar K.V 
208371352caSAneesh Kumar K.V 	__asm__ __volatile__(
209371352caSAneesh Kumar K.V 	"1:	ldarx	%0,0,%4\n\
2105dc1ef85SAneesh Kumar K.V 		and.	%1,%0,%6\n\
211371352caSAneesh Kumar K.V 		bne-	1b \n\
212371352caSAneesh Kumar K.V 		or	%0,%3,%0\n\
213371352caSAneesh Kumar K.V 		stdcx.	%0,0,%4\n\
214371352caSAneesh Kumar K.V 		bne-	1b"
215371352caSAneesh Kumar K.V 	:"=&r" (old), "=&r" (tmp), "=m" (*ptep)
216945537dfSAneesh Kumar K.V 	:"r" (val), "r" (ptep), "m" (*ptep), "r" (cpu_to_be64(H_PAGE_BUSY))
217371352caSAneesh Kumar K.V 	:"cc");
218371352caSAneesh Kumar K.V }
219371352caSAneesh Kumar K.V 
hash__pte_same(pte_t pte_a,pte_t pte_b)220ac94ac79SAneesh Kumar K.V static inline int hash__pte_same(pte_t pte_a, pte_t pte_b)
221ee3caed3SMichael Ellerman {
222ee3caed3SMichael Ellerman 	return (((pte_raw(pte_a) ^ pte_raw(pte_b)) & ~cpu_to_be64(_PAGE_HPTEFLAGS)) == 0);
223ee3caed3SMichael Ellerman }
224ee3caed3SMichael Ellerman 
hash__pte_none(pte_t pte)225ac94ac79SAneesh Kumar K.V static inline int hash__pte_none(pte_t pte)
226ac94ac79SAneesh Kumar K.V {
227ac94ac79SAneesh Kumar K.V 	return (pte_val(pte) & ~H_PTE_NONE_MASK) == 0;
228ac94ac79SAneesh Kumar K.V }
2291ca72129SAneesh Kumar K.V 
230318995b4SRam Pai unsigned long pte_get_hash_gslot(unsigned long vpn, unsigned long shift,
231318995b4SRam Pai 		int ssize, real_pte_t rpte, unsigned int subpg_index);
232318995b4SRam Pai 
2331ca72129SAneesh Kumar K.V /* This low level function performs the actual PTE insertion
2341ca72129SAneesh Kumar K.V  * Setting the PTE depends on the MMU type and other factors. It's
2351ca72129SAneesh Kumar K.V  * an horrible mess that I'm not going to try to clean up now but
2361ca72129SAneesh Kumar K.V  * I'm keeping it in one place rather than spread around
2371ca72129SAneesh Kumar K.V  */
hash__set_pte_at(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte,int percpu)238ac94ac79SAneesh Kumar K.V static inline void hash__set_pte_at(struct mm_struct *mm, unsigned long addr,
2391ca72129SAneesh Kumar K.V 				  pte_t *ptep, pte_t pte, int percpu)
2401ca72129SAneesh Kumar K.V {
2411ca72129SAneesh Kumar K.V 	/*
2421ca72129SAneesh Kumar K.V 	 * Anything else just stores the PTE normally. That covers all 64-bit
2431ca72129SAneesh Kumar K.V 	 * cases, and 32-bit non-hash with 32-bit PTEs.
2441ca72129SAneesh Kumar K.V 	 */
2451ca72129SAneesh Kumar K.V 	*ptep = pte;
2461ca72129SAneesh Kumar K.V }
2471ca72129SAneesh Kumar K.V 
248371352caSAneesh Kumar K.V #ifdef CONFIG_TRANSPARENT_HUGEPAGE
249371352caSAneesh Kumar K.V extern void hpte_do_hugepage_flush(struct mm_struct *mm, unsigned long addr,
250371352caSAneesh Kumar K.V 				   pmd_t *pmdp, unsigned long old_pmd);
251371352caSAneesh Kumar K.V #else
hpte_do_hugepage_flush(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp,unsigned long old_pmd)252371352caSAneesh Kumar K.V static inline void hpte_do_hugepage_flush(struct mm_struct *mm,
253371352caSAneesh Kumar K.V 					  unsigned long addr, pmd_t *pmdp,
254371352caSAneesh Kumar K.V 					  unsigned long old_pmd)
255371352caSAneesh Kumar K.V {
256371352caSAneesh Kumar K.V 	WARN(1, "%s called with THP disabled\n", __func__);
257371352caSAneesh Kumar K.V }
258371352caSAneesh Kumar K.V #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
259371352caSAneesh Kumar K.V 
26031a14faeSAneesh Kumar K.V 
261c766ee72SChristophe Leroy int hash__map_kernel_page(unsigned long ea, unsigned long pa, pgprot_t prot);
26231a14faeSAneesh Kumar K.V extern int __meminit hash__vmemmap_create_mapping(unsigned long start,
26331a14faeSAneesh Kumar K.V 					      unsigned long page_size,
26431a14faeSAneesh Kumar K.V 					      unsigned long phys);
26531a14faeSAneesh Kumar K.V extern void hash__vmemmap_remove_mapping(unsigned long start,
26631a14faeSAneesh Kumar K.V 				     unsigned long page_size);
26732b53c01SReza Arbab 
2684e00c5afSLogan Gunthorpe int hash__create_section_mapping(unsigned long start, unsigned long end,
2694e00c5afSLogan Gunthorpe 				 int nid, pgprot_t prot);
27032b53c01SReza Arbab int hash__remove_section_mapping(unsigned long start, unsigned long end);
27132b53c01SReza Arbab 
2724f703e7fSJoel Stanley void hash__kernel_map_pages(struct page *page, int numpages, int enable);
2734f703e7fSJoel Stanley 
274371352caSAneesh Kumar K.V #endif /* !__ASSEMBLY__ */
27526b6a3d9SAneesh Kumar K.V #endif /* __KERNEL__ */
27626b6a3d9SAneesh Kumar K.V #endif /* _ASM_POWERPC_BOOK3S_64_HASH_H */
277