1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Processor Activity Instrumentation support for cryptography counters
4 *
5 * Copyright IBM Corp. 2022
6 * Author(s): Thomas Richter <[email protected]>
7 */
8 #ifndef _ASM_S390_PAI_H
9 #define _ASM_S390_PAI_H
10
11 #include <linux/jump_label.h>
12 #include <asm/lowcore.h>
13 #include <asm/ptrace.h>
14 #include <asm/asm.h>
15
16 struct qpaci_info_block {
17 u64 header;
18 struct {
19 u64 : 8;
20 u64 num_cc : 8; /* # of supported crypto counters */
21 u64 : 9;
22 u64 num_nnpa : 7; /* # of supported NNPA counters */
23 u64 : 32;
24 };
25 };
26
qpaci(struct qpaci_info_block * info)27 static inline int qpaci(struct qpaci_info_block *info)
28 {
29 /* Size of info (in double words minus one) */
30 size_t size = sizeof(*info) / sizeof(u64) - 1;
31 int cc;
32
33 asm volatile(
34 " lgr 0,%[size]\n"
35 " .insn s,0xb28f0000,%[info]\n"
36 " lgr %[size],0\n"
37 CC_IPM(cc)
38 : CC_OUT(cc, cc), [info] "=Q" (*info), [size] "+&d" (size)
39 :
40 : CC_CLOBBER_LIST("0", "memory"));
41 return CC_TRANSFORM(cc) ? (size + 1) * sizeof(u64) : 0;
42 }
43
44 #define PAI_CRYPTO_BASE 0x1000 /* First event number */
45 #define PAI_CRYPTO_MAXCTR 256 /* Max # of event counters */
46 #define PAI_CRYPTO_KERNEL_OFFSET 2048
47 #define PAI_NNPA_BASE 0x1800 /* First event number */
48 #define PAI_NNPA_MAXCTR 128 /* Max # of event counters */
49
50 DECLARE_STATIC_KEY_FALSE(pai_key);
51
pai_kernel_enter(struct pt_regs * regs)52 static __always_inline void pai_kernel_enter(struct pt_regs *regs)
53 {
54 if (!IS_ENABLED(CONFIG_PERF_EVENTS))
55 return;
56 if (!static_branch_unlikely(&pai_key))
57 return;
58 if (!get_lowcore()->ccd)
59 return;
60 if (!user_mode(regs))
61 return;
62 WRITE_ONCE(get_lowcore()->ccd, get_lowcore()->ccd | PAI_CRYPTO_KERNEL_OFFSET);
63 }
64
pai_kernel_exit(struct pt_regs * regs)65 static __always_inline void pai_kernel_exit(struct pt_regs *regs)
66 {
67 if (!IS_ENABLED(CONFIG_PERF_EVENTS))
68 return;
69 if (!static_branch_unlikely(&pai_key))
70 return;
71 if (!get_lowcore()->ccd)
72 return;
73 if (!user_mode(regs))
74 return;
75 WRITE_ONCE(get_lowcore()->ccd, get_lowcore()->ccd & ~PAI_CRYPTO_KERNEL_OFFSET);
76 }
77
78 #define PAI_SAVE_AREA(x) ((x)->hw.event_base)
79 #define PAI_CPU_MASK(x) ((x)->hw.addr_filters)
80 #define PAI_SWLIST(x) (&(x)->hw.tp_list)
81
82 #endif
83