drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / arch / arm64 / include / asm / pkeys.h
blob0ca5f83ce148f59ba00f7eef55463c3b6dba2b45
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2023 Arm Ltd.
5 * Based on arch/x86/include/asm/pkeys.h
6 */
8 #ifndef _ASM_ARM64_PKEYS_H
9 #define _ASM_ARM64_PKEYS_H
11 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2)
13 #define arch_max_pkey() 8
15 int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
16 unsigned long init_val);
18 static inline bool arch_pkeys_enabled(void)
20 return system_supports_poe();
23 static inline int vma_pkey(struct vm_area_struct *vma)
25 return (vma->vm_flags & ARCH_VM_PKEY_FLAGS) >> VM_PKEY_SHIFT;
28 static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
29 int prot, int pkey)
31 if (pkey != -1)
32 return pkey;
34 return vma_pkey(vma);
37 static inline int execute_only_pkey(struct mm_struct *mm)
39 // Execute-only mappings are handled by EPAN/FEAT_PAN3.
40 return -1;
43 #define mm_pkey_allocation_map(mm) (mm)->context.pkey_allocation_map
44 #define mm_set_pkey_allocated(mm, pkey) do { \
45 mm_pkey_allocation_map(mm) |= (1U << pkey); \
46 } while (0)
47 #define mm_set_pkey_free(mm, pkey) do { \
48 mm_pkey_allocation_map(mm) &= ~(1U << pkey); \
49 } while (0)
51 static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
54 * "Allocated" pkeys are those that have been returned
55 * from pkey_alloc() or pkey 0 which is allocated
56 * implicitly when the mm is created.
58 if (pkey < 0 || pkey >= arch_max_pkey())
59 return false;
61 return mm_pkey_allocation_map(mm) & (1U << pkey);
65 * Returns a positive, 3-bit key on success, or -1 on failure.
67 static inline int mm_pkey_alloc(struct mm_struct *mm)
70 * Note: this is the one and only place we make sure
71 * that the pkey is valid as far as the hardware is
72 * concerned. The rest of the kernel trusts that
73 * only good, valid pkeys come out of here.
75 u8 all_pkeys_mask = GENMASK(arch_max_pkey() - 1, 0);
76 int ret;
78 if (!arch_pkeys_enabled())
79 return -1;
82 * Are we out of pkeys? We must handle this specially
83 * because ffz() behavior is undefined if there are no
84 * zeros.
86 if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
87 return -1;
89 ret = ffz(mm_pkey_allocation_map(mm));
91 mm_set_pkey_allocated(mm, ret);
93 return ret;
96 static inline int mm_pkey_free(struct mm_struct *mm, int pkey)
98 if (!mm_pkey_is_allocated(mm, pkey))
99 return -EINVAL;
101 mm_set_pkey_free(mm, pkey);
103 return 0;
106 #endif /* _ASM_ARM64_PKEYS_H */