xref: /openbmc/linux/arch/powerpc/include/asm/book3s/64/hash-pkey.h (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b9658f83SAneesh Kumar K.V /* SPDX-License-Identifier: GPL-2.0 */
2b9658f83SAneesh Kumar K.V #ifndef _ASM_POWERPC_BOOK3S_64_HASH_PKEY_H
3b9658f83SAneesh Kumar K.V #define _ASM_POWERPC_BOOK3S_64_HASH_PKEY_H
4b9658f83SAneesh Kumar K.V 
5d94b827eSAneesh Kumar K.V /*  We use key 3 for KERNEL */
6d94b827eSAneesh Kumar K.V #define HASH_DEFAULT_KERNEL_KEY (HPTE_R_KEY_BIT0 | HPTE_R_KEY_BIT1)
7d94b827eSAneesh Kumar K.V 
hash__vmflag_to_pte_pkey_bits(u64 vm_flags)8b9658f83SAneesh Kumar K.V static inline u64 hash__vmflag_to_pte_pkey_bits(u64 vm_flags)
9b9658f83SAneesh Kumar K.V {
10b9658f83SAneesh Kumar K.V 	return (((vm_flags & VM_PKEY_BIT0) ? H_PTE_PKEY_BIT0 : 0x0UL) |
11b9658f83SAneesh Kumar K.V 		((vm_flags & VM_PKEY_BIT1) ? H_PTE_PKEY_BIT1 : 0x0UL) |
12b9658f83SAneesh Kumar K.V 		((vm_flags & VM_PKEY_BIT2) ? H_PTE_PKEY_BIT2 : 0x0UL) |
13b9658f83SAneesh Kumar K.V 		((vm_flags & VM_PKEY_BIT3) ? H_PTE_PKEY_BIT3 : 0x0UL) |
14b9658f83SAneesh Kumar K.V 		((vm_flags & VM_PKEY_BIT4) ? H_PTE_PKEY_BIT4 : 0x0UL));
15b9658f83SAneesh Kumar K.V }
16b9658f83SAneesh Kumar K.V 
pte_to_hpte_pkey_bits(u64 pteflags,unsigned long flags)17d94b827eSAneesh Kumar K.V static inline u64 pte_to_hpte_pkey_bits(u64 pteflags, unsigned long flags)
18b9658f83SAneesh Kumar K.V {
19d94b827eSAneesh Kumar K.V 	unsigned long pte_pkey;
20d94b827eSAneesh Kumar K.V 
21d94b827eSAneesh Kumar K.V 	pte_pkey = (((pteflags & H_PTE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL) |
22b9658f83SAneesh Kumar K.V 		    ((pteflags & H_PTE_PKEY_BIT3) ? HPTE_R_KEY_BIT3 : 0x0UL) |
23b9658f83SAneesh Kumar K.V 		    ((pteflags & H_PTE_PKEY_BIT2) ? HPTE_R_KEY_BIT2 : 0x0UL) |
24b9658f83SAneesh Kumar K.V 		    ((pteflags & H_PTE_PKEY_BIT1) ? HPTE_R_KEY_BIT1 : 0x0UL) |
25b9658f83SAneesh Kumar K.V 		    ((pteflags & H_PTE_PKEY_BIT0) ? HPTE_R_KEY_BIT0 : 0x0UL));
26d94b827eSAneesh Kumar K.V 
27*4589a2b7SChristophe Leroy 	if (mmu_has_feature(MMU_FTR_KUAP) ||
28d94b827eSAneesh Kumar K.V 	    mmu_has_feature(MMU_FTR_BOOK3S_KUEP)) {
29d94b827eSAneesh Kumar K.V 		if ((pte_pkey == 0) && (flags & HPTE_USE_KERNEL_KEY))
30d94b827eSAneesh Kumar K.V 			return HASH_DEFAULT_KERNEL_KEY;
31d94b827eSAneesh Kumar K.V 	}
32d94b827eSAneesh Kumar K.V 
33d94b827eSAneesh Kumar K.V 	return pte_pkey;
34b9658f83SAneesh Kumar K.V }
35b9658f83SAneesh Kumar K.V 
hash__pte_to_pkey_bits(u64 pteflags)36b9658f83SAneesh Kumar K.V static inline u16 hash__pte_to_pkey_bits(u64 pteflags)
37b9658f83SAneesh Kumar K.V {
38b9658f83SAneesh Kumar K.V 	return (((pteflags & H_PTE_PKEY_BIT4) ? 0x10 : 0x0UL) |
39b9658f83SAneesh Kumar K.V 		((pteflags & H_PTE_PKEY_BIT3) ? 0x8 : 0x0UL) |
40b9658f83SAneesh Kumar K.V 		((pteflags & H_PTE_PKEY_BIT2) ? 0x4 : 0x0UL) |
41b9658f83SAneesh Kumar K.V 		((pteflags & H_PTE_PKEY_BIT1) ? 0x2 : 0x0UL) |
42b9658f83SAneesh Kumar K.V 		((pteflags & H_PTE_PKEY_BIT0) ? 0x1 : 0x0UL));
43b9658f83SAneesh Kumar K.V }
44b9658f83SAneesh Kumar K.V 
45b9658f83SAneesh Kumar K.V #endif
46