1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Counter facility support definitions for the Linux perf
4  *
5  * Copyright IBM Corp. 2019
6  * Author(s): Hendrik Brueckner <brueckner@linux.ibm.com>
7  */
8 #ifndef _ASM_S390_CPU_MCF_H
9 #define _ASM_S390_CPU_MCF_H
10 
11 #include <linux/perf_event.h>
12 #include <asm/cpu_mf.h>
13 
14 enum cpumf_ctr_set {
15 	CPUMF_CTR_SET_BASIC   = 0,    /* Basic Counter Set */
16 	CPUMF_CTR_SET_USER    = 1,    /* Problem-State Counter Set */
17 	CPUMF_CTR_SET_CRYPTO  = 2,    /* Crypto-Activity Counter Set */
18 	CPUMF_CTR_SET_EXT     = 3,    /* Extended Counter Set */
19 	CPUMF_CTR_SET_MT_DIAG = 4,    /* MT-diagnostic Counter Set */
20 
21 	/* Maximum number of counter sets */
22 	CPUMF_CTR_SET_MAX,
23 };
24 
25 #define CPUMF_LCCTL_ENABLE_SHIFT    16
26 #define CPUMF_LCCTL_ACTCTL_SHIFT     0
27 
ctr_set_enable(u64 * state,u64 ctrsets)28 static inline void ctr_set_enable(u64 *state, u64 ctrsets)
29 {
30 	*state |= ctrsets << CPUMF_LCCTL_ENABLE_SHIFT;
31 }
32 
ctr_set_disable(u64 * state,u64 ctrsets)33 static inline void ctr_set_disable(u64 *state, u64 ctrsets)
34 {
35 	*state &= ~(ctrsets << CPUMF_LCCTL_ENABLE_SHIFT);
36 }
37 
ctr_set_start(u64 * state,u64 ctrsets)38 static inline void ctr_set_start(u64 *state, u64 ctrsets)
39 {
40 	*state |= ctrsets << CPUMF_LCCTL_ACTCTL_SHIFT;
41 }
42 
ctr_set_stop(u64 * state,u64 ctrsets)43 static inline void ctr_set_stop(u64 *state, u64 ctrsets)
44 {
45 	*state &= ~(ctrsets << CPUMF_LCCTL_ACTCTL_SHIFT);
46 }
47 
ctr_stcctm(enum cpumf_ctr_set set,u64 range,u64 * dest)48 static inline int ctr_stcctm(enum cpumf_ctr_set set, u64 range, u64 *dest)
49 {
50 	switch (set) {
51 	case CPUMF_CTR_SET_BASIC:
52 		return stcctm(BASIC, range, dest);
53 	case CPUMF_CTR_SET_USER:
54 		return stcctm(PROBLEM_STATE, range, dest);
55 	case CPUMF_CTR_SET_CRYPTO:
56 		return stcctm(CRYPTO_ACTIVITY, range, dest);
57 	case CPUMF_CTR_SET_EXT:
58 		return stcctm(EXTENDED, range, dest);
59 	case CPUMF_CTR_SET_MT_DIAG:
60 		return stcctm(MT_DIAG_CLEARING, range, dest);
61 	case CPUMF_CTR_SET_MAX:
62 		return 3;
63 	}
64 	return 3;
65 }
66 
67 struct cpu_cf_events {
68 	struct cpumf_ctr_info	info;
69 	atomic_t		ctr_set[CPUMF_CTR_SET_MAX];
70 	atomic64_t		alert;
71 	u64			state;		/* For perf_event_open SVC */
72 	u64			dev_state;	/* For /dev/hwctr */
73 	unsigned int		flags;
74 	size_t used;			/* Bytes used in data */
75 	size_t usedss;			/* Bytes used in start/stop */
76 	unsigned char start[PAGE_SIZE];	/* Counter set at event add */
77 	unsigned char stop[PAGE_SIZE];	/* Counter set at event delete */
78 	unsigned char data[PAGE_SIZE];	/* Counter set at /dev/hwctr */
79 	unsigned int sets;		/* # Counter set saved in memory */
80 };
81 DECLARE_PER_CPU(struct cpu_cf_events, cpu_cf_events);
82 
83 bool kernel_cpumcf_avail(void);
84 int __kernel_cpumcf_begin(void);
85 unsigned long kernel_cpumcf_alert(int clear);
86 void __kernel_cpumcf_end(void);
87 
kernel_cpumcf_begin(void)88 static inline int kernel_cpumcf_begin(void)
89 {
90 	if (!cpum_cf_avail())
91 		return -ENODEV;
92 
93 	preempt_disable();
94 	return __kernel_cpumcf_begin();
95 }
kernel_cpumcf_end(void)96 static inline void kernel_cpumcf_end(void)
97 {
98 	__kernel_cpumcf_end();
99 	preempt_enable();
100 }
101 
102 /* Return true if store counter set multiple instruction is available */
stccm_avail(void)103 static inline int stccm_avail(void)
104 {
105 	return test_facility(142);
106 }
107 
108 size_t cpum_cf_ctrset_size(enum cpumf_ctr_set ctrset,
109 			   struct cpumf_ctr_info *info);
110 int cfset_online_cpu(unsigned int cpu);
111 int cfset_offline_cpu(unsigned int cpu);
112 #endif /* _ASM_S390_CPU_MCF_H */
113