xref: /openbmc/linux/arch/s390/kernel/perf_cpum_cf.c (revision 0d5f0dc83073cd36d1e92bbcbcc3bc046918bc69)
1a17ae4c3SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2212188a5SHendrik Brueckner /*
3212188a5SHendrik Brueckner  * Performance event support for s390x - CPU-measurement Counter Facility
4212188a5SHendrik Brueckner  *
51e99c242SThomas Richter  *  Copyright IBM Corp. 2012, 2023
646a984ffSThomas Richter  *  Author(s): Hendrik Brueckner <brueckner@linux.ibm.com>
7a029a4eaSThomas Richter  *	       Thomas Richter <tmricht@linux.ibm.com>
8212188a5SHendrik Brueckner  */
9212188a5SHendrik Brueckner #define KMSG_COMPONENT	"cpum_cf"
10212188a5SHendrik Brueckner #define pr_fmt(fmt)	KMSG_COMPONENT ": " fmt
11212188a5SHendrik Brueckner 
12212188a5SHendrik Brueckner #include <linux/kernel.h>
13212188a5SHendrik Brueckner #include <linux/kernel_stat.h>
14212188a5SHendrik Brueckner #include <linux/percpu.h>
15212188a5SHendrik Brueckner #include <linux/notifier.h>
16212188a5SHendrik Brueckner #include <linux/init.h>
17212188a5SHendrik Brueckner #include <linux/export.h>
18a029a4eaSThomas Richter #include <linux/miscdevice.h>
191e99c242SThomas Richter #include <linux/perf_event.h>
20a029a4eaSThomas Richter 
211e99c242SThomas Richter #include <asm/cpu_mf.h>
22a029a4eaSThomas Richter #include <asm/hwctrset.h>
23a029a4eaSThomas Richter #include <asm/debug.h>
24a029a4eaSThomas Richter 
251e99c242SThomas Richter enum cpumf_ctr_set {
261e99c242SThomas Richter 	CPUMF_CTR_SET_BASIC   = 0,    /* Basic Counter Set */
271e99c242SThomas Richter 	CPUMF_CTR_SET_USER    = 1,    /* Problem-State Counter Set */
281e99c242SThomas Richter 	CPUMF_CTR_SET_CRYPTO  = 2,    /* Crypto-Activity Counter Set */
291e99c242SThomas Richter 	CPUMF_CTR_SET_EXT     = 3,    /* Extended Counter Set */
301e99c242SThomas Richter 	CPUMF_CTR_SET_MT_DIAG = 4,    /* MT-diagnostic Counter Set */
311e99c242SThomas Richter 
321e99c242SThomas Richter 	/* Maximum number of counter sets */
331e99c242SThomas Richter 	CPUMF_CTR_SET_MAX,
341e99c242SThomas Richter };
351e99c242SThomas Richter 
361e99c242SThomas Richter #define CPUMF_LCCTL_ENABLE_SHIFT    16
371e99c242SThomas Richter #define CPUMF_LCCTL_ACTCTL_SHIFT     0
381e99c242SThomas Richter 
391e99c242SThomas Richter static inline void ctr_set_enable(u64 *state, u64 ctrsets)
401e99c242SThomas Richter {
411e99c242SThomas Richter 	*state |= ctrsets << CPUMF_LCCTL_ENABLE_SHIFT;
421e99c242SThomas Richter }
431e99c242SThomas Richter 
441e99c242SThomas Richter static inline void ctr_set_disable(u64 *state, u64 ctrsets)
451e99c242SThomas Richter {
461e99c242SThomas Richter 	*state &= ~(ctrsets << CPUMF_LCCTL_ENABLE_SHIFT);
471e99c242SThomas Richter }
481e99c242SThomas Richter 
491e99c242SThomas Richter static inline void ctr_set_start(u64 *state, u64 ctrsets)
501e99c242SThomas Richter {
511e99c242SThomas Richter 	*state |= ctrsets << CPUMF_LCCTL_ACTCTL_SHIFT;
521e99c242SThomas Richter }
531e99c242SThomas Richter 
541e99c242SThomas Richter static inline void ctr_set_stop(u64 *state, u64 ctrsets)
551e99c242SThomas Richter {
561e99c242SThomas Richter 	*state &= ~(ctrsets << CPUMF_LCCTL_ACTCTL_SHIFT);
571e99c242SThomas Richter }
581e99c242SThomas Richter 
591e99c242SThomas Richter static inline int ctr_stcctm(enum cpumf_ctr_set set, u64 range, u64 *dest)
601e99c242SThomas Richter {
611e99c242SThomas Richter 	switch (set) {
621e99c242SThomas Richter 	case CPUMF_CTR_SET_BASIC:
631e99c242SThomas Richter 		return stcctm(BASIC, range, dest);
641e99c242SThomas Richter 	case CPUMF_CTR_SET_USER:
651e99c242SThomas Richter 		return stcctm(PROBLEM_STATE, range, dest);
661e99c242SThomas Richter 	case CPUMF_CTR_SET_CRYPTO:
671e99c242SThomas Richter 		return stcctm(CRYPTO_ACTIVITY, range, dest);
681e99c242SThomas Richter 	case CPUMF_CTR_SET_EXT:
691e99c242SThomas Richter 		return stcctm(EXTENDED, range, dest);
701e99c242SThomas Richter 	case CPUMF_CTR_SET_MT_DIAG:
711e99c242SThomas Richter 		return stcctm(MT_DIAG_CLEARING, range, dest);
721e99c242SThomas Richter 	case CPUMF_CTR_SET_MAX:
731e99c242SThomas Richter 		return 3;
741e99c242SThomas Richter 	}
751e99c242SThomas Richter 	return 3;
761e99c242SThomas Richter }
771e99c242SThomas Richter 
781e99c242SThomas Richter struct cpu_cf_events {
791e99c242SThomas Richter 	struct cpumf_ctr_info	info;
801e99c242SThomas Richter 	atomic_t		ctr_set[CPUMF_CTR_SET_MAX];
811e99c242SThomas Richter 	u64			state;		/* For perf_event_open SVC */
821e99c242SThomas Richter 	u64			dev_state;	/* For /dev/hwctr */
831e99c242SThomas Richter 	unsigned int		flags;
841e99c242SThomas Richter 	size_t used;			/* Bytes used in data */
851e99c242SThomas Richter 	size_t usedss;			/* Bytes used in start/stop */
861e99c242SThomas Richter 	unsigned char start[PAGE_SIZE];	/* Counter set at event add */
871e99c242SThomas Richter 	unsigned char stop[PAGE_SIZE];	/* Counter set at event delete */
881e99c242SThomas Richter 	unsigned char data[PAGE_SIZE];	/* Counter set at /dev/hwctr */
891e99c242SThomas Richter 	unsigned int sets;		/* # Counter set saved in memory */
901e99c242SThomas Richter };
911e99c242SThomas Richter 
921e99c242SThomas Richter /* Per-CPU event structure for the counter facility */
931e99c242SThomas Richter static DEFINE_PER_CPU(struct cpu_cf_events, cpu_cf_events);
941e99c242SThomas Richter 
95a029a4eaSThomas Richter static unsigned int cfdiag_cpu_speed;	/* CPU speed for CF_DIAG trailer */
96a029a4eaSThomas Richter static debug_info_t *cf_dbg;
97a029a4eaSThomas Richter 
98a029a4eaSThomas Richter #define	CF_DIAG_CTRSET_DEF		0xfeef	/* Counter set header mark */
99a029a4eaSThomas Richter 						/* interval in seconds */
100a029a4eaSThomas Richter 
101a029a4eaSThomas Richter /* Counter sets are stored as data stream in a page sized memory buffer and
102a029a4eaSThomas Richter  * exported to user space via raw data attached to the event sample data.
103a029a4eaSThomas Richter  * Each counter set starts with an eight byte header consisting of:
104a029a4eaSThomas Richter  * - a two byte eye catcher (0xfeef)
105a029a4eaSThomas Richter  * - a one byte counter set number
106a029a4eaSThomas Richter  * - a two byte counter set size (indicates the number of counters in this set)
107a029a4eaSThomas Richter  * - a three byte reserved value (must be zero) to make the header the same
108a029a4eaSThomas Richter  *   size as a counter value.
109a029a4eaSThomas Richter  * All counter values are eight byte in size.
110a029a4eaSThomas Richter  *
111a029a4eaSThomas Richter  * All counter sets are followed by a 64 byte trailer.
112a029a4eaSThomas Richter  * The trailer consists of a:
113a029a4eaSThomas Richter  * - flag field indicating valid fields when corresponding bit set
114a029a4eaSThomas Richter  * - the counter facility first and second version number
115a029a4eaSThomas Richter  * - the CPU speed if nonzero
116a029a4eaSThomas Richter  * - the time stamp the counter sets have been collected
117a029a4eaSThomas Richter  * - the time of day (TOD) base value
118a029a4eaSThomas Richter  * - the machine type.
119a029a4eaSThomas Richter  *
120a029a4eaSThomas Richter  * The counter sets are saved when the process is prepared to be executed on a
121a029a4eaSThomas Richter  * CPU and saved again when the process is going to be removed from a CPU.
122a029a4eaSThomas Richter  * The difference of both counter sets are calculated and stored in the event
123a029a4eaSThomas Richter  * sample data area.
124a029a4eaSThomas Richter  */
125a029a4eaSThomas Richter struct cf_ctrset_entry {	/* CPU-M CF counter set entry (8 byte) */
126a029a4eaSThomas Richter 	unsigned int def:16;	/* 0-15  Data Entry Format */
127a029a4eaSThomas Richter 	unsigned int set:16;	/* 16-31 Counter set identifier */
128a029a4eaSThomas Richter 	unsigned int ctr:16;	/* 32-47 Number of stored counters */
129a029a4eaSThomas Richter 	unsigned int res1:16;	/* 48-63 Reserved */
130a029a4eaSThomas Richter };
131a029a4eaSThomas Richter 
132a029a4eaSThomas Richter struct cf_trailer_entry {	/* CPU-M CF_DIAG trailer (64 byte) */
133a029a4eaSThomas Richter 	/* 0 - 7 */
134a029a4eaSThomas Richter 	union {
135a029a4eaSThomas Richter 		struct {
136a029a4eaSThomas Richter 			unsigned int clock_base:1;	/* TOD clock base set */
137a029a4eaSThomas Richter 			unsigned int speed:1;		/* CPU speed set */
138a029a4eaSThomas Richter 			/* Measurement alerts */
139a029a4eaSThomas Richter 			unsigned int mtda:1;	/* Loss of MT ctr. data alert */
140a029a4eaSThomas Richter 			unsigned int caca:1;	/* Counter auth. change alert */
141a029a4eaSThomas Richter 			unsigned int lcda:1;	/* Loss of counter data alert */
142a029a4eaSThomas Richter 		};
143a029a4eaSThomas Richter 		unsigned long flags;	/* 0-63    All indicators */
144a029a4eaSThomas Richter 	};
145a029a4eaSThomas Richter 	/* 8 - 15 */
146a029a4eaSThomas Richter 	unsigned int cfvn:16;			/* 64-79   Ctr First Version */
147a029a4eaSThomas Richter 	unsigned int csvn:16;			/* 80-95   Ctr Second Version */
148a029a4eaSThomas Richter 	unsigned int cpu_speed:32;		/* 96-127  CPU speed */
149a029a4eaSThomas Richter 	/* 16 - 23 */
150a029a4eaSThomas Richter 	unsigned long timestamp;		/* 128-191 Timestamp (TOD) */
151a029a4eaSThomas Richter 	/* 24 - 55 */
152a029a4eaSThomas Richter 	union {
153a029a4eaSThomas Richter 		struct {
154a029a4eaSThomas Richter 			unsigned long progusage1;
155a029a4eaSThomas Richter 			unsigned long progusage2;
156a029a4eaSThomas Richter 			unsigned long progusage3;
157a029a4eaSThomas Richter 			unsigned long tod_base;
158a029a4eaSThomas Richter 		};
159a029a4eaSThomas Richter 		unsigned long progusage[4];
160a029a4eaSThomas Richter 	};
161a029a4eaSThomas Richter 	/* 56 - 63 */
162a029a4eaSThomas Richter 	unsigned int mach_type:16;		/* Machine type */
163a029a4eaSThomas Richter 	unsigned int res1:16;			/* Reserved */
164a029a4eaSThomas Richter 	unsigned int res2:32;			/* Reserved */
165a029a4eaSThomas Richter };
166a029a4eaSThomas Richter 
167a029a4eaSThomas Richter /* Create the trailer data at the end of a page. */
168a029a4eaSThomas Richter static void cfdiag_trailer(struct cf_trailer_entry *te)
169a029a4eaSThomas Richter {
170a029a4eaSThomas Richter 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
171a029a4eaSThomas Richter 	struct cpuid cpuid;
172a029a4eaSThomas Richter 
173a029a4eaSThomas Richter 	te->cfvn = cpuhw->info.cfvn;		/* Counter version numbers */
174a029a4eaSThomas Richter 	te->csvn = cpuhw->info.csvn;
175a029a4eaSThomas Richter 
176a029a4eaSThomas Richter 	get_cpu_id(&cpuid);			/* Machine type */
177a029a4eaSThomas Richter 	te->mach_type = cpuid.machine;
178a029a4eaSThomas Richter 	te->cpu_speed = cfdiag_cpu_speed;
179a029a4eaSThomas Richter 	if (te->cpu_speed)
180a029a4eaSThomas Richter 		te->speed = 1;
181a029a4eaSThomas Richter 	te->clock_base = 1;			/* Save clock base */
182a029a4eaSThomas Richter 	te->tod_base = tod_clock_base.tod;
183a029a4eaSThomas Richter 	te->timestamp = get_tod_clock_fast();
184a029a4eaSThomas Richter }
185a029a4eaSThomas Richter 
186345d2a4dSThomas Richter /*
187345d2a4dSThomas Richter  * Return the maximum possible counter set size (in number of 8 byte counters)
188345d2a4dSThomas Richter  * depending on type and model number.
189345d2a4dSThomas Richter  */
190345d2a4dSThomas Richter static size_t cpum_cf_ctrset_size(enum cpumf_ctr_set ctrset,
191345d2a4dSThomas Richter 				  struct cpumf_ctr_info *info)
192345d2a4dSThomas Richter {
193345d2a4dSThomas Richter 	size_t ctrset_size = 0;
194345d2a4dSThomas Richter 
195345d2a4dSThomas Richter 	switch (ctrset) {
196345d2a4dSThomas Richter 	case CPUMF_CTR_SET_BASIC:
197345d2a4dSThomas Richter 		if (info->cfvn >= 1)
198345d2a4dSThomas Richter 			ctrset_size = 6;
199345d2a4dSThomas Richter 		break;
200345d2a4dSThomas Richter 	case CPUMF_CTR_SET_USER:
201345d2a4dSThomas Richter 		if (info->cfvn == 1)
202345d2a4dSThomas Richter 			ctrset_size = 6;
203345d2a4dSThomas Richter 		else if (info->cfvn >= 3)
204345d2a4dSThomas Richter 			ctrset_size = 2;
205345d2a4dSThomas Richter 		break;
206345d2a4dSThomas Richter 	case CPUMF_CTR_SET_CRYPTO:
207345d2a4dSThomas Richter 		if (info->csvn >= 1 && info->csvn <= 5)
208345d2a4dSThomas Richter 			ctrset_size = 16;
209345d2a4dSThomas Richter 		else if (info->csvn == 6 || info->csvn == 7)
210345d2a4dSThomas Richter 			ctrset_size = 20;
211345d2a4dSThomas Richter 		break;
212345d2a4dSThomas Richter 	case CPUMF_CTR_SET_EXT:
213345d2a4dSThomas Richter 		if (info->csvn == 1)
214345d2a4dSThomas Richter 			ctrset_size = 32;
215345d2a4dSThomas Richter 		else if (info->csvn == 2)
216345d2a4dSThomas Richter 			ctrset_size = 48;
217345d2a4dSThomas Richter 		else if (info->csvn >= 3 && info->csvn <= 5)
218345d2a4dSThomas Richter 			ctrset_size = 128;
219345d2a4dSThomas Richter 		else if (info->csvn == 6 || info->csvn == 7)
220345d2a4dSThomas Richter 			ctrset_size = 160;
221345d2a4dSThomas Richter 		break;
222345d2a4dSThomas Richter 	case CPUMF_CTR_SET_MT_DIAG:
223345d2a4dSThomas Richter 		if (info->csvn > 3)
224345d2a4dSThomas Richter 			ctrset_size = 48;
225345d2a4dSThomas Richter 		break;
226345d2a4dSThomas Richter 	case CPUMF_CTR_SET_MAX:
227345d2a4dSThomas Richter 		break;
228345d2a4dSThomas Richter 	}
229345d2a4dSThomas Richter 
230345d2a4dSThomas Richter 	return ctrset_size;
231345d2a4dSThomas Richter }
232345d2a4dSThomas Richter 
233a029a4eaSThomas Richter /* Read a counter set. The counter set number determines the counter set and
234a029a4eaSThomas Richter  * the CPUM-CF first and second version number determine the number of
235a029a4eaSThomas Richter  * available counters in each counter set.
236a029a4eaSThomas Richter  * Each counter set starts with header containing the counter set number and
237a029a4eaSThomas Richter  * the number of eight byte counters.
238a029a4eaSThomas Richter  *
239a029a4eaSThomas Richter  * The functions returns the number of bytes occupied by this counter set
240a029a4eaSThomas Richter  * including the header.
241a029a4eaSThomas Richter  * If there is no counter in the counter set, this counter set is useless and
242a029a4eaSThomas Richter  * zero is returned on this case.
243a029a4eaSThomas Richter  *
244a029a4eaSThomas Richter  * Note that the counter sets may not be enabled or active and the stcctm
245a029a4eaSThomas Richter  * instruction might return error 3. Depending on error_ok value this is ok,
246a029a4eaSThomas Richter  * for example when called from cpumf_pmu_start() call back function.
247a029a4eaSThomas Richter  */
248a029a4eaSThomas Richter static size_t cfdiag_getctrset(struct cf_ctrset_entry *ctrdata, int ctrset,
249a029a4eaSThomas Richter 			       size_t room, bool error_ok)
250a029a4eaSThomas Richter {
251a029a4eaSThomas Richter 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
252a029a4eaSThomas Richter 	size_t ctrset_size, need = 0;
253a029a4eaSThomas Richter 	int rc = 3;				/* Assume write failure */
254a029a4eaSThomas Richter 
255a029a4eaSThomas Richter 	ctrdata->def = CF_DIAG_CTRSET_DEF;
256a029a4eaSThomas Richter 	ctrdata->set = ctrset;
257a029a4eaSThomas Richter 	ctrdata->res1 = 0;
258a029a4eaSThomas Richter 	ctrset_size = cpum_cf_ctrset_size(ctrset, &cpuhw->info);
259a029a4eaSThomas Richter 
260a029a4eaSThomas Richter 	if (ctrset_size) {			/* Save data */
261a029a4eaSThomas Richter 		need = ctrset_size * sizeof(u64) + sizeof(*ctrdata);
262a029a4eaSThomas Richter 		if (need <= room) {
263a029a4eaSThomas Richter 			rc = ctr_stcctm(ctrset, ctrset_size,
264a029a4eaSThomas Richter 					(u64 *)(ctrdata + 1));
265a029a4eaSThomas Richter 		}
266a029a4eaSThomas Richter 		if (rc != 3 || error_ok)
267a029a4eaSThomas Richter 			ctrdata->ctr = ctrset_size;
268a029a4eaSThomas Richter 		else
269a029a4eaSThomas Richter 			need = 0;
270a029a4eaSThomas Richter 	}
271a029a4eaSThomas Richter 
272a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 3,
273a029a4eaSThomas Richter 			    "%s ctrset %d ctrset_size %zu cfvn %d csvn %d"
274a029a4eaSThomas Richter 			    " need %zd rc %d\n", __func__, ctrset, ctrset_size,
275a029a4eaSThomas Richter 			    cpuhw->info.cfvn, cpuhw->info.csvn, need, rc);
276a029a4eaSThomas Richter 	return need;
277a029a4eaSThomas Richter }
278a029a4eaSThomas Richter 
2795dddfaacSHeiko Carstens static const u64 cpumf_ctr_ctl[CPUMF_CTR_SET_MAX] = {
2805dddfaacSHeiko Carstens 	[CPUMF_CTR_SET_BASIC]	= 0x02,
2815dddfaacSHeiko Carstens 	[CPUMF_CTR_SET_USER]	= 0x04,
2825dddfaacSHeiko Carstens 	[CPUMF_CTR_SET_CRYPTO]	= 0x08,
2835dddfaacSHeiko Carstens 	[CPUMF_CTR_SET_EXT]	= 0x01,
2845dddfaacSHeiko Carstens 	[CPUMF_CTR_SET_MT_DIAG] = 0x20,
2855dddfaacSHeiko Carstens };
2865dddfaacSHeiko Carstens 
287a029a4eaSThomas Richter /* Read out all counter sets and save them in the provided data buffer.
288a029a4eaSThomas Richter  * The last 64 byte host an artificial trailer entry.
289a029a4eaSThomas Richter  */
290a029a4eaSThomas Richter static size_t cfdiag_getctr(void *data, size_t sz, unsigned long auth,
291a029a4eaSThomas Richter 			    bool error_ok)
292a029a4eaSThomas Richter {
293a029a4eaSThomas Richter 	struct cf_trailer_entry *trailer;
294a029a4eaSThomas Richter 	size_t offset = 0, done;
295a029a4eaSThomas Richter 	int i;
296a029a4eaSThomas Richter 
297a029a4eaSThomas Richter 	memset(data, 0, sz);
298a029a4eaSThomas Richter 	sz -= sizeof(*trailer);		/* Always room for trailer */
299a029a4eaSThomas Richter 	for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
300a029a4eaSThomas Richter 		struct cf_ctrset_entry *ctrdata = data + offset;
301a029a4eaSThomas Richter 
302a029a4eaSThomas Richter 		if (!(auth & cpumf_ctr_ctl[i]))
303a029a4eaSThomas Richter 			continue;	/* Counter set not authorized */
304a029a4eaSThomas Richter 
305a029a4eaSThomas Richter 		done = cfdiag_getctrset(ctrdata, i, sz - offset, error_ok);
306a029a4eaSThomas Richter 		offset += done;
307a029a4eaSThomas Richter 	}
308a029a4eaSThomas Richter 	trailer = data + offset;
309a029a4eaSThomas Richter 	cfdiag_trailer(trailer);
310a029a4eaSThomas Richter 	return offset + sizeof(*trailer);
311a029a4eaSThomas Richter }
312a029a4eaSThomas Richter 
313a029a4eaSThomas Richter /* Calculate the difference for each counter in a counter set. */
314a029a4eaSThomas Richter static void cfdiag_diffctrset(u64 *pstart, u64 *pstop, int counters)
315a029a4eaSThomas Richter {
316a029a4eaSThomas Richter 	for (; --counters >= 0; ++pstart, ++pstop)
317a029a4eaSThomas Richter 		if (*pstop >= *pstart)
318a029a4eaSThomas Richter 			*pstop -= *pstart;
319a029a4eaSThomas Richter 		else
320a029a4eaSThomas Richter 			*pstop = *pstart - *pstop + 1;
321a029a4eaSThomas Richter }
322a029a4eaSThomas Richter 
323a029a4eaSThomas Richter /* Scan the counter sets and calculate the difference of each counter
324a029a4eaSThomas Richter  * in each set. The result is the increment of each counter during the
325a029a4eaSThomas Richter  * period the counter set has been activated.
326a029a4eaSThomas Richter  *
327a029a4eaSThomas Richter  * Return true on success.
328a029a4eaSThomas Richter  */
329a029a4eaSThomas Richter static int cfdiag_diffctr(struct cpu_cf_events *cpuhw, unsigned long auth)
330a029a4eaSThomas Richter {
331a029a4eaSThomas Richter 	struct cf_trailer_entry *trailer_start, *trailer_stop;
332a029a4eaSThomas Richter 	struct cf_ctrset_entry *ctrstart, *ctrstop;
333a029a4eaSThomas Richter 	size_t offset = 0;
334a029a4eaSThomas Richter 
335a029a4eaSThomas Richter 	auth &= (1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1;
336a029a4eaSThomas Richter 	do {
337a029a4eaSThomas Richter 		ctrstart = (struct cf_ctrset_entry *)(cpuhw->start + offset);
338a029a4eaSThomas Richter 		ctrstop = (struct cf_ctrset_entry *)(cpuhw->stop + offset);
339a029a4eaSThomas Richter 
340a029a4eaSThomas Richter 		if (memcmp(ctrstop, ctrstart, sizeof(*ctrstop))) {
341a029a4eaSThomas Richter 			pr_err_once("cpum_cf_diag counter set compare error "
342a029a4eaSThomas Richter 				    "in set %i\n", ctrstart->set);
343a029a4eaSThomas Richter 			return 0;
344a029a4eaSThomas Richter 		}
345a029a4eaSThomas Richter 		auth &= ~cpumf_ctr_ctl[ctrstart->set];
346a029a4eaSThomas Richter 		if (ctrstart->def == CF_DIAG_CTRSET_DEF) {
347a029a4eaSThomas Richter 			cfdiag_diffctrset((u64 *)(ctrstart + 1),
348a029a4eaSThomas Richter 					  (u64 *)(ctrstop + 1), ctrstart->ctr);
349a029a4eaSThomas Richter 			offset += ctrstart->ctr * sizeof(u64) +
350a029a4eaSThomas Richter 							sizeof(*ctrstart);
351a029a4eaSThomas Richter 		}
352a029a4eaSThomas Richter 	} while (ctrstart->def && auth);
353a029a4eaSThomas Richter 
354a029a4eaSThomas Richter 	/* Save time_stamp from start of event in stop's trailer */
355a029a4eaSThomas Richter 	trailer_start = (struct cf_trailer_entry *)(cpuhw->start + offset);
356a029a4eaSThomas Richter 	trailer_stop = (struct cf_trailer_entry *)(cpuhw->stop + offset);
357a029a4eaSThomas Richter 	trailer_stop->progusage[0] = trailer_start->timestamp;
358a029a4eaSThomas Richter 
359a029a4eaSThomas Richter 	return 1;
360a029a4eaSThomas Richter }
361212188a5SHendrik Brueckner 
362ee699f32SHendrik Brueckner static enum cpumf_ctr_set get_counter_set(u64 event)
363212188a5SHendrik Brueckner {
364ee699f32SHendrik Brueckner 	int set = CPUMF_CTR_SET_MAX;
365212188a5SHendrik Brueckner 
366212188a5SHendrik Brueckner 	if (event < 32)
367212188a5SHendrik Brueckner 		set = CPUMF_CTR_SET_BASIC;
368212188a5SHendrik Brueckner 	else if (event < 64)
369212188a5SHendrik Brueckner 		set = CPUMF_CTR_SET_USER;
370212188a5SHendrik Brueckner 	else if (event < 128)
371212188a5SHendrik Brueckner 		set = CPUMF_CTR_SET_CRYPTO;
37246a984ffSThomas Richter 	else if (event < 288)
373212188a5SHendrik Brueckner 		set = CPUMF_CTR_SET_EXT;
374ee699f32SHendrik Brueckner 	else if (event >= 448 && event < 496)
375ee699f32SHendrik Brueckner 		set = CPUMF_CTR_SET_MT_DIAG;
376212188a5SHendrik Brueckner 
377212188a5SHendrik Brueckner 	return set;
378212188a5SHendrik Brueckner }
379212188a5SHendrik Brueckner 
380a029a4eaSThomas Richter static int validate_ctr_version(const struct hw_perf_event *hwc,
381a029a4eaSThomas Richter 				enum cpumf_ctr_set set)
382212188a5SHendrik Brueckner {
383f1c0b831SHendrik Brueckner 	struct cpu_cf_events *cpuhw;
384212188a5SHendrik Brueckner 	int err = 0;
385ee699f32SHendrik Brueckner 	u16 mtdiag_ctl;
386212188a5SHendrik Brueckner 
387f1c0b831SHendrik Brueckner 	cpuhw = &get_cpu_var(cpu_cf_events);
388212188a5SHendrik Brueckner 
389212188a5SHendrik Brueckner 	/* check required version for counter sets */
390a029a4eaSThomas Richter 	switch (set) {
391212188a5SHendrik Brueckner 	case CPUMF_CTR_SET_BASIC:
392212188a5SHendrik Brueckner 	case CPUMF_CTR_SET_USER:
393212188a5SHendrik Brueckner 		if (cpuhw->info.cfvn < 1)
394212188a5SHendrik Brueckner 			err = -EOPNOTSUPP;
395212188a5SHendrik Brueckner 		break;
396212188a5SHendrik Brueckner 	case CPUMF_CTR_SET_CRYPTO:
39746a984ffSThomas Richter 		if ((cpuhw->info.csvn >= 1 && cpuhw->info.csvn <= 5 &&
39846a984ffSThomas Richter 		     hwc->config > 79) ||
39946a984ffSThomas Richter 		    (cpuhw->info.csvn >= 6 && hwc->config > 83))
40046a984ffSThomas Richter 			err = -EOPNOTSUPP;
40146a984ffSThomas Richter 		break;
402212188a5SHendrik Brueckner 	case CPUMF_CTR_SET_EXT:
403212188a5SHendrik Brueckner 		if (cpuhw->info.csvn < 1)
404212188a5SHendrik Brueckner 			err = -EOPNOTSUPP;
405f47586b2SHendrik Brueckner 		if ((cpuhw->info.csvn == 1 && hwc->config > 159) ||
406f47586b2SHendrik Brueckner 		    (cpuhw->info.csvn == 2 && hwc->config > 175) ||
40746a984ffSThomas Richter 		    (cpuhw->info.csvn >= 3 && cpuhw->info.csvn <= 5
40846a984ffSThomas Richter 		     && hwc->config > 255) ||
40946a984ffSThomas Richter 		    (cpuhw->info.csvn >= 6 && hwc->config > 287))
410f47586b2SHendrik Brueckner 			err = -EOPNOTSUPP;
411212188a5SHendrik Brueckner 		break;
412ee699f32SHendrik Brueckner 	case CPUMF_CTR_SET_MT_DIAG:
413ee699f32SHendrik Brueckner 		if (cpuhw->info.csvn <= 3)
414ee699f32SHendrik Brueckner 			err = -EOPNOTSUPP;
415ee699f32SHendrik Brueckner 		/*
416ee699f32SHendrik Brueckner 		 * MT-diagnostic counters are read-only.  The counter set
417ee699f32SHendrik Brueckner 		 * is automatically enabled and activated on all CPUs with
418ee699f32SHendrik Brueckner 		 * multithreading (SMT).  Deactivation of multithreading
419ee699f32SHendrik Brueckner 		 * also disables the counter set.  State changes are ignored
420ee699f32SHendrik Brueckner 		 * by lcctl().	Because Linux controls SMT enablement through
421ee699f32SHendrik Brueckner 		 * a kernel parameter only, the counter set is either disabled
422ee699f32SHendrik Brueckner 		 * or enabled and active.
423ee699f32SHendrik Brueckner 		 *
424ee699f32SHendrik Brueckner 		 * Thus, the counters can only be used if SMT is on and the
425ee699f32SHendrik Brueckner 		 * counter set is enabled and active.
426ee699f32SHendrik Brueckner 		 */
42730e145f8SHendrik Brueckner 		mtdiag_ctl = cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG];
428ee699f32SHendrik Brueckner 		if (!((cpuhw->info.auth_ctl & mtdiag_ctl) &&
429ee699f32SHendrik Brueckner 		      (cpuhw->info.enable_ctl & mtdiag_ctl) &&
430ee699f32SHendrik Brueckner 		      (cpuhw->info.act_ctl & mtdiag_ctl)))
431ee699f32SHendrik Brueckner 			err = -EOPNOTSUPP;
432ee699f32SHendrik Brueckner 		break;
433a029a4eaSThomas Richter 	case CPUMF_CTR_SET_MAX:
434a029a4eaSThomas Richter 		err = -EOPNOTSUPP;
435212188a5SHendrik Brueckner 	}
436212188a5SHendrik Brueckner 
437f1c0b831SHendrik Brueckner 	put_cpu_var(cpu_cf_events);
438212188a5SHendrik Brueckner 	return err;
439212188a5SHendrik Brueckner }
440212188a5SHendrik Brueckner 
441212188a5SHendrik Brueckner static int validate_ctr_auth(const struct hw_perf_event *hwc)
442212188a5SHendrik Brueckner {
443f1c0b831SHendrik Brueckner 	struct cpu_cf_events *cpuhw;
444212188a5SHendrik Brueckner 	int err = 0;
445212188a5SHendrik Brueckner 
446f1c0b831SHendrik Brueckner 	cpuhw = &get_cpu_var(cpu_cf_events);
447212188a5SHendrik Brueckner 
44858f8e9daSHendrik Brueckner 	/* Check authorization for cpu counter sets.
44958f8e9daSHendrik Brueckner 	 * If the particular CPU counter set is not authorized,
45058f8e9daSHendrik Brueckner 	 * return with -ENOENT in order to fall back to other
45158f8e9daSHendrik Brueckner 	 * PMUs that might suffice the event request.
45258f8e9daSHendrik Brueckner 	 */
453a029a4eaSThomas Richter 	if (!(hwc->config_base & cpuhw->info.auth_ctl))
45458f8e9daSHendrik Brueckner 		err = -ENOENT;
455212188a5SHendrik Brueckner 
456f1c0b831SHendrik Brueckner 	put_cpu_var(cpu_cf_events);
457212188a5SHendrik Brueckner 	return err;
458212188a5SHendrik Brueckner }
459212188a5SHendrik Brueckner 
460212188a5SHendrik Brueckner /*
461212188a5SHendrik Brueckner  * Change the CPUMF state to active.
462212188a5SHendrik Brueckner  * Enable and activate the CPU-counter sets according
463212188a5SHendrik Brueckner  * to the per-cpu control state.
464212188a5SHendrik Brueckner  */
465212188a5SHendrik Brueckner static void cpumf_pmu_enable(struct pmu *pmu)
466212188a5SHendrik Brueckner {
467f1c0b831SHendrik Brueckner 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
468212188a5SHendrik Brueckner 	int err;
469212188a5SHendrik Brueckner 
470212188a5SHendrik Brueckner 	if (cpuhw->flags & PMU_F_ENABLED)
471212188a5SHendrik Brueckner 		return;
472212188a5SHendrik Brueckner 
473a029a4eaSThomas Richter 	err = lcctl(cpuhw->state | cpuhw->dev_state);
474212188a5SHendrik Brueckner 	if (err) {
475212188a5SHendrik Brueckner 		pr_err("Enabling the performance measuring unit "
476af0ee94eSHeiko Carstens 		       "failed with rc=%x\n", err);
477212188a5SHendrik Brueckner 		return;
478212188a5SHendrik Brueckner 	}
479212188a5SHendrik Brueckner 
480212188a5SHendrik Brueckner 	cpuhw->flags |= PMU_F_ENABLED;
481212188a5SHendrik Brueckner }
482212188a5SHendrik Brueckner 
483212188a5SHendrik Brueckner /*
484212188a5SHendrik Brueckner  * Change the CPUMF state to inactive.
485212188a5SHendrik Brueckner  * Disable and enable (inactive) the CPU-counter sets according
486212188a5SHendrik Brueckner  * to the per-cpu control state.
487212188a5SHendrik Brueckner  */
488212188a5SHendrik Brueckner static void cpumf_pmu_disable(struct pmu *pmu)
489212188a5SHendrik Brueckner {
490f1c0b831SHendrik Brueckner 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
491212188a5SHendrik Brueckner 	int err;
492212188a5SHendrik Brueckner 	u64 inactive;
493212188a5SHendrik Brueckner 
494212188a5SHendrik Brueckner 	if (!(cpuhw->flags & PMU_F_ENABLED))
495212188a5SHendrik Brueckner 		return;
496212188a5SHendrik Brueckner 
497212188a5SHendrik Brueckner 	inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
498a029a4eaSThomas Richter 	inactive |= cpuhw->dev_state;
499212188a5SHendrik Brueckner 	err = lcctl(inactive);
500212188a5SHendrik Brueckner 	if (err) {
501212188a5SHendrik Brueckner 		pr_err("Disabling the performance measuring unit "
502af0ee94eSHeiko Carstens 		       "failed with rc=%x\n", err);
503212188a5SHendrik Brueckner 		return;
504212188a5SHendrik Brueckner 	}
505212188a5SHendrik Brueckner 
506212188a5SHendrik Brueckner 	cpuhw->flags &= ~PMU_F_ENABLED;
507212188a5SHendrik Brueckner }
508212188a5SHendrik Brueckner 
509*0d5f0dc8SThomas Richter #define PMC_INIT      0UL
510*0d5f0dc8SThomas Richter #define PMC_RELEASE   1UL
5111e99c242SThomas Richter 
5121e99c242SThomas Richter static void cpum_cf_setup_cpu(void *flags)
5131e99c242SThomas Richter {
5141e99c242SThomas Richter 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
5151e99c242SThomas Richter 
516*0d5f0dc8SThomas Richter 	switch ((unsigned long)flags) {
5171e99c242SThomas Richter 	case PMC_INIT:
5181e99c242SThomas Richter 		memset(&cpuhw->info, 0, sizeof(cpuhw->info));
5191e99c242SThomas Richter 		qctri(&cpuhw->info);
5201e99c242SThomas Richter 		cpuhw->flags |= PMU_F_RESERVED;
5211e99c242SThomas Richter 		break;
5221e99c242SThomas Richter 
5231e99c242SThomas Richter 	case PMC_RELEASE:
5241e99c242SThomas Richter 		cpuhw->flags &= ~PMU_F_RESERVED;
5251e99c242SThomas Richter 		break;
5261e99c242SThomas Richter 	}
5271e99c242SThomas Richter 
5281e99c242SThomas Richter 	/* Disable CPU counter sets */
5291e99c242SThomas Richter 	lcctl(0);
5301e99c242SThomas Richter 	debug_sprintf_event(cf_dbg, 5, "%s flags %#x flags %#x state %#llx\n",
5311e99c242SThomas Richter 			    __func__, *(int *)flags, cpuhw->flags,
5321e99c242SThomas Richter 			    cpuhw->state);
5331e99c242SThomas Richter }
5341e99c242SThomas Richter 
5351e99c242SThomas Richter /* Initialize the CPU-measurement counter facility */
5361e99c242SThomas Richter static int __kernel_cpumcf_begin(void)
5371e99c242SThomas Richter {
538*0d5f0dc8SThomas Richter 	on_each_cpu(cpum_cf_setup_cpu, (void *)PMC_INIT, 1);
5391e99c242SThomas Richter 	irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
5401e99c242SThomas Richter 
5411e99c242SThomas Richter 	return 0;
5421e99c242SThomas Richter }
5431e99c242SThomas Richter 
5441e99c242SThomas Richter /* Release the CPU-measurement counter facility */
5451e99c242SThomas Richter static void __kernel_cpumcf_end(void)
5461e99c242SThomas Richter {
547*0d5f0dc8SThomas Richter 	on_each_cpu(cpum_cf_setup_cpu, (void *)PMC_RELEASE, 1);
5481e99c242SThomas Richter 	irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
5491e99c242SThomas Richter }
550212188a5SHendrik Brueckner 
551212188a5SHendrik Brueckner /* Number of perf events counting hardware events */
552212188a5SHendrik Brueckner static atomic_t num_events = ATOMIC_INIT(0);
553212188a5SHendrik Brueckner /* Used to avoid races in calling reserve/release_cpumf_hardware */
554212188a5SHendrik Brueckner static DEFINE_MUTEX(pmc_reserve_mutex);
555212188a5SHendrik Brueckner 
556212188a5SHendrik Brueckner /* Release the PMU if event is the last perf event */
557212188a5SHendrik Brueckner static void hw_perf_event_destroy(struct perf_event *event)
558212188a5SHendrik Brueckner {
559212188a5SHendrik Brueckner 	mutex_lock(&pmc_reserve_mutex);
560212188a5SHendrik Brueckner 	if (atomic_dec_return(&num_events) == 0)
5613d33345aSHendrik Brueckner 		__kernel_cpumcf_end();
562212188a5SHendrik Brueckner 	mutex_unlock(&pmc_reserve_mutex);
563212188a5SHendrik Brueckner }
564212188a5SHendrik Brueckner 
565212188a5SHendrik Brueckner /* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
566212188a5SHendrik Brueckner static const int cpumf_generic_events_basic[] = {
567212188a5SHendrik Brueckner 	[PERF_COUNT_HW_CPU_CYCLES]	    = 0,
568212188a5SHendrik Brueckner 	[PERF_COUNT_HW_INSTRUCTIONS]	    = 1,
569212188a5SHendrik Brueckner 	[PERF_COUNT_HW_CACHE_REFERENCES]    = -1,
570212188a5SHendrik Brueckner 	[PERF_COUNT_HW_CACHE_MISSES]	    = -1,
571212188a5SHendrik Brueckner 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
572212188a5SHendrik Brueckner 	[PERF_COUNT_HW_BRANCH_MISSES]	    = -1,
573212188a5SHendrik Brueckner 	[PERF_COUNT_HW_BUS_CYCLES]	    = -1,
574212188a5SHendrik Brueckner };
575212188a5SHendrik Brueckner /* CPUMF <-> perf event mappings for userspace (problem-state set) */
576212188a5SHendrik Brueckner static const int cpumf_generic_events_user[] = {
577212188a5SHendrik Brueckner 	[PERF_COUNT_HW_CPU_CYCLES]	    = 32,
578212188a5SHendrik Brueckner 	[PERF_COUNT_HW_INSTRUCTIONS]	    = 33,
579212188a5SHendrik Brueckner 	[PERF_COUNT_HW_CACHE_REFERENCES]    = -1,
580212188a5SHendrik Brueckner 	[PERF_COUNT_HW_CACHE_MISSES]	    = -1,
581212188a5SHendrik Brueckner 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
582212188a5SHendrik Brueckner 	[PERF_COUNT_HW_BRANCH_MISSES]	    = -1,
583212188a5SHendrik Brueckner 	[PERF_COUNT_HW_BUS_CYCLES]	    = -1,
584212188a5SHendrik Brueckner };
585212188a5SHendrik Brueckner 
586a029a4eaSThomas Richter static void cpumf_hw_inuse(void)
587a029a4eaSThomas Richter {
588a029a4eaSThomas Richter 	mutex_lock(&pmc_reserve_mutex);
589a029a4eaSThomas Richter 	if (atomic_inc_return(&num_events) == 1)
590a029a4eaSThomas Richter 		__kernel_cpumcf_begin();
591a029a4eaSThomas Richter 	mutex_unlock(&pmc_reserve_mutex);
592a029a4eaSThomas Richter }
593a029a4eaSThomas Richter 
59491d5364dSThomas Richter static int is_userspace_event(u64 ev)
59591d5364dSThomas Richter {
59691d5364dSThomas Richter 	return cpumf_generic_events_user[PERF_COUNT_HW_CPU_CYCLES] == ev ||
59791d5364dSThomas Richter 	       cpumf_generic_events_user[PERF_COUNT_HW_INSTRUCTIONS] == ev;
59891d5364dSThomas Richter }
59991d5364dSThomas Richter 
6006a82e23fSThomas Richter static int __hw_perf_event_init(struct perf_event *event, unsigned int type)
601212188a5SHendrik Brueckner {
602212188a5SHendrik Brueckner 	struct perf_event_attr *attr = &event->attr;
603212188a5SHendrik Brueckner 	struct hw_perf_event *hwc = &event->hw;
604ee699f32SHendrik Brueckner 	enum cpumf_ctr_set set;
60547b74785SThomas Richter 	int err = 0;
606212188a5SHendrik Brueckner 	u64 ev;
607212188a5SHendrik Brueckner 
6086a82e23fSThomas Richter 	switch (type) {
609212188a5SHendrik Brueckner 	case PERF_TYPE_RAW:
610212188a5SHendrik Brueckner 		/* Raw events are used to access counters directly,
611212188a5SHendrik Brueckner 		 * hence do not permit excludes */
612212188a5SHendrik Brueckner 		if (attr->exclude_kernel || attr->exclude_user ||
613212188a5SHendrik Brueckner 		    attr->exclude_hv)
614212188a5SHendrik Brueckner 			return -EOPNOTSUPP;
615212188a5SHendrik Brueckner 		ev = attr->config;
616212188a5SHendrik Brueckner 		break;
617212188a5SHendrik Brueckner 
618212188a5SHendrik Brueckner 	case PERF_TYPE_HARDWARE:
619613a41b0SThomas Richter 		if (is_sampling_event(event))	/* No sampling support */
620613a41b0SThomas Richter 			return -ENOENT;
621212188a5SHendrik Brueckner 		ev = attr->config;
622212188a5SHendrik Brueckner 		if (!attr->exclude_user && attr->exclude_kernel) {
62391d5364dSThomas Richter 			/*
62491d5364dSThomas Richter 			 * Count user space (problem-state) only
62591d5364dSThomas Richter 			 * Handle events 32 and 33 as 0:u and 1:u
62691d5364dSThomas Richter 			 */
62791d5364dSThomas Richter 			if (!is_userspace_event(ev)) {
628212188a5SHendrik Brueckner 				if (ev >= ARRAY_SIZE(cpumf_generic_events_user))
629212188a5SHendrik Brueckner 					return -EOPNOTSUPP;
630212188a5SHendrik Brueckner 				ev = cpumf_generic_events_user[ev];
63191d5364dSThomas Richter 			}
632212188a5SHendrik Brueckner 		} else if (!attr->exclude_kernel && attr->exclude_user) {
63391d5364dSThomas Richter 			/* No support for kernel space counters only */
634212188a5SHendrik Brueckner 			return -EOPNOTSUPP;
63591d5364dSThomas Richter 		} else {
63691d5364dSThomas Richter 			/* Count user and kernel space, incl. events 32 + 33 */
63791d5364dSThomas Richter 			if (!is_userspace_event(ev)) {
638212188a5SHendrik Brueckner 				if (ev >= ARRAY_SIZE(cpumf_generic_events_basic))
639212188a5SHendrik Brueckner 					return -EOPNOTSUPP;
640212188a5SHendrik Brueckner 				ev = cpumf_generic_events_basic[ev];
641212188a5SHendrik Brueckner 			}
64291d5364dSThomas Richter 		}
643212188a5SHendrik Brueckner 		break;
644212188a5SHendrik Brueckner 
645212188a5SHendrik Brueckner 	default:
646212188a5SHendrik Brueckner 		return -ENOENT;
647212188a5SHendrik Brueckner 	}
648212188a5SHendrik Brueckner 
649212188a5SHendrik Brueckner 	if (ev == -1)
650212188a5SHendrik Brueckner 		return -ENOENT;
651212188a5SHendrik Brueckner 
65220ba46daSHendrik Brueckner 	if (ev > PERF_CPUM_CF_MAX_CTR)
6530bb2ae1bSThomas Richter 		return -ENOENT;
654212188a5SHendrik Brueckner 
655ee699f32SHendrik Brueckner 	/* Obtain the counter set to which the specified counter belongs */
656ee699f32SHendrik Brueckner 	set = get_counter_set(ev);
657ee699f32SHendrik Brueckner 	switch (set) {
658ee699f32SHendrik Brueckner 	case CPUMF_CTR_SET_BASIC:
659ee699f32SHendrik Brueckner 	case CPUMF_CTR_SET_USER:
660ee699f32SHendrik Brueckner 	case CPUMF_CTR_SET_CRYPTO:
661ee699f32SHendrik Brueckner 	case CPUMF_CTR_SET_EXT:
662ee699f32SHendrik Brueckner 	case CPUMF_CTR_SET_MT_DIAG:
663ee699f32SHendrik Brueckner 		/*
664ee699f32SHendrik Brueckner 		 * Use the hardware perf event structure to store the
665ee699f32SHendrik Brueckner 		 * counter number in the 'config' member and the counter
666a029a4eaSThomas Richter 		 * set number in the 'config_base' as bit mask.
667a029a4eaSThomas Richter 		 * It is later used to enable/disable the counter(s).
668212188a5SHendrik Brueckner 		 */
669212188a5SHendrik Brueckner 		hwc->config = ev;
670a029a4eaSThomas Richter 		hwc->config_base = cpumf_ctr_ctl[set];
671ee699f32SHendrik Brueckner 		break;
672ee699f32SHendrik Brueckner 	case CPUMF_CTR_SET_MAX:
673ee699f32SHendrik Brueckner 		/* The counter could not be associated to a counter set */
674ee699f32SHendrik Brueckner 		return -EINVAL;
6751c0a9c79SJiapeng Chong 	}
676212188a5SHendrik Brueckner 
677212188a5SHendrik Brueckner 	/* Initialize for using the CPU-measurement counter facility */
678a029a4eaSThomas Richter 	cpumf_hw_inuse();
679212188a5SHendrik Brueckner 	event->destroy = hw_perf_event_destroy;
680212188a5SHendrik Brueckner 
681212188a5SHendrik Brueckner 	/* Finally, validate version and authorization of the counter set */
682212188a5SHendrik Brueckner 	err = validate_ctr_auth(hwc);
683212188a5SHendrik Brueckner 	if (!err)
684a029a4eaSThomas Richter 		err = validate_ctr_version(hwc, set);
685212188a5SHendrik Brueckner 
686212188a5SHendrik Brueckner 	return err;
687212188a5SHendrik Brueckner }
688212188a5SHendrik Brueckner 
689be857b7fSThomas Richter /* Events CPU_CYLCES and INSTRUCTIONS can be submitted with two different
690be857b7fSThomas Richter  * attribute::type values:
691be857b7fSThomas Richter  * - PERF_TYPE_HARDWARE:
692be857b7fSThomas Richter  * - pmu->type:
693be857b7fSThomas Richter  * Handle both type of invocations identical. They address the same hardware.
694be857b7fSThomas Richter  * The result is different when event modifiers exclude_kernel and/or
695be857b7fSThomas Richter  * exclude_user are also set.
696be857b7fSThomas Richter  */
697be857b7fSThomas Richter static int cpumf_pmu_event_type(struct perf_event *event)
698be857b7fSThomas Richter {
699be857b7fSThomas Richter 	u64 ev = event->attr.config;
700be857b7fSThomas Richter 
701be857b7fSThomas Richter 	if (cpumf_generic_events_basic[PERF_COUNT_HW_CPU_CYCLES] == ev ||
702be857b7fSThomas Richter 	    cpumf_generic_events_basic[PERF_COUNT_HW_INSTRUCTIONS] == ev ||
703be857b7fSThomas Richter 	    cpumf_generic_events_user[PERF_COUNT_HW_CPU_CYCLES] == ev ||
704be857b7fSThomas Richter 	    cpumf_generic_events_user[PERF_COUNT_HW_INSTRUCTIONS] == ev)
705be857b7fSThomas Richter 		return PERF_TYPE_HARDWARE;
706be857b7fSThomas Richter 	return PERF_TYPE_RAW;
707be857b7fSThomas Richter }
708be857b7fSThomas Richter 
709212188a5SHendrik Brueckner static int cpumf_pmu_event_init(struct perf_event *event)
710212188a5SHendrik Brueckner {
7116a82e23fSThomas Richter 	unsigned int type = event->attr.type;
712212188a5SHendrik Brueckner 	int err;
713212188a5SHendrik Brueckner 
7146a82e23fSThomas Richter 	if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_RAW)
7156a82e23fSThomas Richter 		err = __hw_perf_event_init(event, type);
7166a82e23fSThomas Richter 	else if (event->pmu->type == type)
7176a82e23fSThomas Richter 		/* Registered as unknown PMU */
718be857b7fSThomas Richter 		err = __hw_perf_event_init(event, cpumf_pmu_event_type(event));
7196a82e23fSThomas Richter 	else
720212188a5SHendrik Brueckner 		return -ENOENT;
721212188a5SHendrik Brueckner 
722212188a5SHendrik Brueckner 	if (unlikely(err) && event->destroy)
723212188a5SHendrik Brueckner 		event->destroy(event);
724212188a5SHendrik Brueckner 
725212188a5SHendrik Brueckner 	return err;
726212188a5SHendrik Brueckner }
727212188a5SHendrik Brueckner 
728212188a5SHendrik Brueckner static int hw_perf_event_reset(struct perf_event *event)
729212188a5SHendrik Brueckner {
730212188a5SHendrik Brueckner 	u64 prev, new;
731212188a5SHendrik Brueckner 	int err;
732212188a5SHendrik Brueckner 
733212188a5SHendrik Brueckner 	do {
734212188a5SHendrik Brueckner 		prev = local64_read(&event->hw.prev_count);
735212188a5SHendrik Brueckner 		err = ecctr(event->hw.config, &new);
736212188a5SHendrik Brueckner 		if (err) {
737212188a5SHendrik Brueckner 			if (err != 3)
738212188a5SHendrik Brueckner 				break;
739212188a5SHendrik Brueckner 			/* The counter is not (yet) available. This
740212188a5SHendrik Brueckner 			 * might happen if the counter set to which
741212188a5SHendrik Brueckner 			 * this counter belongs is in the disabled
742212188a5SHendrik Brueckner 			 * state.
743212188a5SHendrik Brueckner 			 */
744212188a5SHendrik Brueckner 			new = 0;
745212188a5SHendrik Brueckner 		}
746212188a5SHendrik Brueckner 	} while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
747212188a5SHendrik Brueckner 
748212188a5SHendrik Brueckner 	return err;
749212188a5SHendrik Brueckner }
750212188a5SHendrik Brueckner 
751485527baSHendrik Brueckner static void hw_perf_event_update(struct perf_event *event)
752212188a5SHendrik Brueckner {
753212188a5SHendrik Brueckner 	u64 prev, new, delta;
754212188a5SHendrik Brueckner 	int err;
755212188a5SHendrik Brueckner 
756212188a5SHendrik Brueckner 	do {
757212188a5SHendrik Brueckner 		prev = local64_read(&event->hw.prev_count);
758212188a5SHendrik Brueckner 		err = ecctr(event->hw.config, &new);
759212188a5SHendrik Brueckner 		if (err)
760485527baSHendrik Brueckner 			return;
761212188a5SHendrik Brueckner 	} while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
762212188a5SHendrik Brueckner 
763212188a5SHendrik Brueckner 	delta = (prev <= new) ? new - prev
764212188a5SHendrik Brueckner 			      : (-1ULL - prev) + new + 1;	 /* overflow */
765212188a5SHendrik Brueckner 	local64_add(delta, &event->count);
766212188a5SHendrik Brueckner }
767212188a5SHendrik Brueckner 
768212188a5SHendrik Brueckner static void cpumf_pmu_read(struct perf_event *event)
769212188a5SHendrik Brueckner {
770212188a5SHendrik Brueckner 	if (event->hw.state & PERF_HES_STOPPED)
771212188a5SHendrik Brueckner 		return;
772212188a5SHendrik Brueckner 
773212188a5SHendrik Brueckner 	hw_perf_event_update(event);
774212188a5SHendrik Brueckner }
775212188a5SHendrik Brueckner 
776212188a5SHendrik Brueckner static void cpumf_pmu_start(struct perf_event *event, int flags)
777212188a5SHendrik Brueckner {
778f1c0b831SHendrik Brueckner 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
779212188a5SHendrik Brueckner 	struct hw_perf_event *hwc = &event->hw;
780a029a4eaSThomas Richter 	int i;
781212188a5SHendrik Brueckner 
78215e5b53fSThomas Richter 	if (!(hwc->state & PERF_HES_STOPPED))
783212188a5SHendrik Brueckner 		return;
784212188a5SHendrik Brueckner 
785212188a5SHendrik Brueckner 	hwc->state = 0;
786212188a5SHendrik Brueckner 
787212188a5SHendrik Brueckner 	/* (Re-)enable and activate the counter set */
788212188a5SHendrik Brueckner 	ctr_set_enable(&cpuhw->state, hwc->config_base);
789212188a5SHendrik Brueckner 	ctr_set_start(&cpuhw->state, hwc->config_base);
790212188a5SHendrik Brueckner 
791212188a5SHendrik Brueckner 	/* The counter set to which this counter belongs can be already active.
792212188a5SHendrik Brueckner 	 * Because all counters in a set are active, the event->hw.prev_count
793212188a5SHendrik Brueckner 	 * needs to be synchronized.  At this point, the counter set can be in
794212188a5SHendrik Brueckner 	 * the inactive or disabled state.
795212188a5SHendrik Brueckner 	 */
796a029a4eaSThomas Richter 	if (hwc->config == PERF_EVENT_CPUM_CF_DIAG) {
797a029a4eaSThomas Richter 		cpuhw->usedss = cfdiag_getctr(cpuhw->start,
798a029a4eaSThomas Richter 					      sizeof(cpuhw->start),
799a029a4eaSThomas Richter 					      hwc->config_base, true);
800a029a4eaSThomas Richter 	} else {
801212188a5SHendrik Brueckner 		hw_perf_event_reset(event);
802a029a4eaSThomas Richter 	}
803212188a5SHendrik Brueckner 
804a029a4eaSThomas Richter 	/* Increment refcount for counter sets */
805a029a4eaSThomas Richter 	for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i)
806a029a4eaSThomas Richter 		if ((hwc->config_base & cpumf_ctr_ctl[i]))
807a029a4eaSThomas Richter 			atomic_inc(&cpuhw->ctr_set[i]);
808a029a4eaSThomas Richter }
809a029a4eaSThomas Richter 
810a029a4eaSThomas Richter /* Create perf event sample with the counter sets as raw data.	The sample
811a029a4eaSThomas Richter  * is then pushed to the event subsystem and the function checks for
812a029a4eaSThomas Richter  * possible event overflows. If an event overflow occurs, the PMU is
813a029a4eaSThomas Richter  * stopped.
814a029a4eaSThomas Richter  *
815a029a4eaSThomas Richter  * Return non-zero if an event overflow occurred.
816a029a4eaSThomas Richter  */
817a029a4eaSThomas Richter static int cfdiag_push_sample(struct perf_event *event,
818a029a4eaSThomas Richter 			      struct cpu_cf_events *cpuhw)
819a029a4eaSThomas Richter {
820a029a4eaSThomas Richter 	struct perf_sample_data data;
821a029a4eaSThomas Richter 	struct perf_raw_record raw;
822a029a4eaSThomas Richter 	struct pt_regs regs;
823a029a4eaSThomas Richter 	int overflow;
824a029a4eaSThomas Richter 
825a029a4eaSThomas Richter 	/* Setup perf sample */
826a029a4eaSThomas Richter 	perf_sample_data_init(&data, 0, event->hw.last_period);
827a029a4eaSThomas Richter 	memset(&regs, 0, sizeof(regs));
828a029a4eaSThomas Richter 	memset(&raw, 0, sizeof(raw));
829a029a4eaSThomas Richter 
830a029a4eaSThomas Richter 	if (event->attr.sample_type & PERF_SAMPLE_CPU)
831a029a4eaSThomas Richter 		data.cpu_entry.cpu = event->cpu;
832a029a4eaSThomas Richter 	if (event->attr.sample_type & PERF_SAMPLE_RAW) {
833a029a4eaSThomas Richter 		raw.frag.size = cpuhw->usedss;
834a029a4eaSThomas Richter 		raw.frag.data = cpuhw->stop;
835a029a4eaSThomas Richter 		raw.size = raw.frag.size;
836a029a4eaSThomas Richter 		data.raw = &raw;
837838d9bb6SNamhyung Kim 		data.sample_flags |= PERF_SAMPLE_RAW;
838a029a4eaSThomas Richter 	}
839a029a4eaSThomas Richter 
840a029a4eaSThomas Richter 	overflow = perf_event_overflow(event, &data, &regs);
841a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 3,
842a029a4eaSThomas Richter 			    "%s event %#llx sample_type %#llx raw %d ov %d\n",
843a029a4eaSThomas Richter 			    __func__, event->hw.config,
844a029a4eaSThomas Richter 			    event->attr.sample_type, raw.size, overflow);
845a029a4eaSThomas Richter 	if (overflow)
846a029a4eaSThomas Richter 		event->pmu->stop(event, 0);
847a029a4eaSThomas Richter 
848a029a4eaSThomas Richter 	perf_event_update_userpage(event);
849a029a4eaSThomas Richter 	return overflow;
850212188a5SHendrik Brueckner }
851212188a5SHendrik Brueckner 
852212188a5SHendrik Brueckner static void cpumf_pmu_stop(struct perf_event *event, int flags)
853212188a5SHendrik Brueckner {
854f1c0b831SHendrik Brueckner 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
855212188a5SHendrik Brueckner 	struct hw_perf_event *hwc = &event->hw;
856a029a4eaSThomas Richter 	int i;
857212188a5SHendrik Brueckner 
858212188a5SHendrik Brueckner 	if (!(hwc->state & PERF_HES_STOPPED)) {
859212188a5SHendrik Brueckner 		/* Decrement reference count for this counter set and if this
860212188a5SHendrik Brueckner 		 * is the last used counter in the set, clear activation
861212188a5SHendrik Brueckner 		 * control and set the counter set state to inactive.
862212188a5SHendrik Brueckner 		 */
863a029a4eaSThomas Richter 		for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
864a029a4eaSThomas Richter 			if (!(hwc->config_base & cpumf_ctr_ctl[i]))
865a029a4eaSThomas Richter 				continue;
866a029a4eaSThomas Richter 			if (!atomic_dec_return(&cpuhw->ctr_set[i]))
867a029a4eaSThomas Richter 				ctr_set_stop(&cpuhw->state, cpumf_ctr_ctl[i]);
868a029a4eaSThomas Richter 		}
8690cceeab5SThomas Richter 		hwc->state |= PERF_HES_STOPPED;
870212188a5SHendrik Brueckner 	}
871212188a5SHendrik Brueckner 
872212188a5SHendrik Brueckner 	if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
873a029a4eaSThomas Richter 		if (hwc->config == PERF_EVENT_CPUM_CF_DIAG) {
874a029a4eaSThomas Richter 			local64_inc(&event->count);
875a029a4eaSThomas Richter 			cpuhw->usedss = cfdiag_getctr(cpuhw->stop,
876a029a4eaSThomas Richter 						      sizeof(cpuhw->stop),
877a029a4eaSThomas Richter 						      event->hw.config_base,
878a029a4eaSThomas Richter 						      false);
879a029a4eaSThomas Richter 			if (cfdiag_diffctr(cpuhw, event->hw.config_base))
880a029a4eaSThomas Richter 				cfdiag_push_sample(event, cpuhw);
8819d48c7afSThomas Richter 		} else if (cpuhw->flags & PMU_F_RESERVED) {
8829d48c7afSThomas Richter 			/* Only update when PMU not hotplugged off */
883212188a5SHendrik Brueckner 			hw_perf_event_update(event);
8849d48c7afSThomas Richter 		}
8850cceeab5SThomas Richter 		hwc->state |= PERF_HES_UPTODATE;
886212188a5SHendrik Brueckner 	}
887212188a5SHendrik Brueckner }
888212188a5SHendrik Brueckner 
889212188a5SHendrik Brueckner static int cpumf_pmu_add(struct perf_event *event, int flags)
890212188a5SHendrik Brueckner {
891f1c0b831SHendrik Brueckner 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
892212188a5SHendrik Brueckner 
893212188a5SHendrik Brueckner 	ctr_set_enable(&cpuhw->state, event->hw.config_base);
894212188a5SHendrik Brueckner 	event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
895212188a5SHendrik Brueckner 
896212188a5SHendrik Brueckner 	if (flags & PERF_EF_START)
897212188a5SHendrik Brueckner 		cpumf_pmu_start(event, PERF_EF_RELOAD);
898212188a5SHendrik Brueckner 
899212188a5SHendrik Brueckner 	return 0;
900212188a5SHendrik Brueckner }
901212188a5SHendrik Brueckner 
902212188a5SHendrik Brueckner static void cpumf_pmu_del(struct perf_event *event, int flags)
903212188a5SHendrik Brueckner {
904f1c0b831SHendrik Brueckner 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
905a029a4eaSThomas Richter 	int i;
906212188a5SHendrik Brueckner 
907212188a5SHendrik Brueckner 	cpumf_pmu_stop(event, PERF_EF_UPDATE);
908212188a5SHendrik Brueckner 
909212188a5SHendrik Brueckner 	/* Check if any counter in the counter set is still used.  If not used,
910212188a5SHendrik Brueckner 	 * change the counter set to the disabled state.  This also clears the
911212188a5SHendrik Brueckner 	 * content of all counters in the set.
912212188a5SHendrik Brueckner 	 *
913212188a5SHendrik Brueckner 	 * When a new perf event has been added but not yet started, this can
914212188a5SHendrik Brueckner 	 * clear enable control and resets all counters in a set.  Therefore,
915212188a5SHendrik Brueckner 	 * cpumf_pmu_start() always has to reenable a counter set.
916212188a5SHendrik Brueckner 	 */
917a029a4eaSThomas Richter 	for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i)
918a029a4eaSThomas Richter 		if (!atomic_read(&cpuhw->ctr_set[i]))
919a029a4eaSThomas Richter 			ctr_set_disable(&cpuhw->state, cpumf_ctr_ctl[i]);
920212188a5SHendrik Brueckner }
921212188a5SHendrik Brueckner 
922212188a5SHendrik Brueckner /* Performance monitoring unit for s390x */
923212188a5SHendrik Brueckner static struct pmu cpumf_pmu = {
9249254e70cSHendrik Brueckner 	.task_ctx_nr  = perf_sw_context,
9259254e70cSHendrik Brueckner 	.capabilities = PERF_PMU_CAP_NO_INTERRUPT,
926212188a5SHendrik Brueckner 	.pmu_enable   = cpumf_pmu_enable,
927212188a5SHendrik Brueckner 	.pmu_disable  = cpumf_pmu_disable,
928212188a5SHendrik Brueckner 	.event_init   = cpumf_pmu_event_init,
929212188a5SHendrik Brueckner 	.add	      = cpumf_pmu_add,
930212188a5SHendrik Brueckner 	.del	      = cpumf_pmu_del,
931212188a5SHendrik Brueckner 	.start	      = cpumf_pmu_start,
932212188a5SHendrik Brueckner 	.stop	      = cpumf_pmu_stop,
933212188a5SHendrik Brueckner 	.read	      = cpumf_pmu_read,
934212188a5SHendrik Brueckner };
935212188a5SHendrik Brueckner 
936*0d5f0dc8SThomas Richter static int cpum_cf_setup(unsigned int cpu, unsigned long flags)
9371e99c242SThomas Richter {
9381e99c242SThomas Richter 	local_irq_disable();
939*0d5f0dc8SThomas Richter 	cpum_cf_setup_cpu((void *)flags);
9401e99c242SThomas Richter 	local_irq_enable();
9411e99c242SThomas Richter 	return 0;
9421e99c242SThomas Richter }
9431e99c242SThomas Richter 
9441e99c242SThomas Richter static int cfset_online_cpu(unsigned int cpu);
9451e99c242SThomas Richter static int cpum_cf_online_cpu(unsigned int cpu)
9461e99c242SThomas Richter {
9471e99c242SThomas Richter 	debug_sprintf_event(cf_dbg, 4, "%s cpu %d in_irq %ld\n", __func__,
9481e99c242SThomas Richter 			    cpu, in_interrupt());
9491e99c242SThomas Richter 	cpum_cf_setup(cpu, PMC_INIT);
9501e99c242SThomas Richter 	return cfset_online_cpu(cpu);
9511e99c242SThomas Richter }
9521e99c242SThomas Richter 
9531e99c242SThomas Richter static int cfset_offline_cpu(unsigned int cpu);
9541e99c242SThomas Richter static int cpum_cf_offline_cpu(unsigned int cpu)
9551e99c242SThomas Richter {
9561e99c242SThomas Richter 	debug_sprintf_event(cf_dbg, 4, "%s cpu %d\n", __func__, cpu);
9571e99c242SThomas Richter 	cfset_offline_cpu(cpu);
9581e99c242SThomas Richter 	return cpum_cf_setup(cpu, PMC_RELEASE);
9591e99c242SThomas Richter }
9601e99c242SThomas Richter 
9617a8f09acSThomas Richter /* Return true if store counter set multiple instruction is available */
9627a8f09acSThomas Richter static inline int stccm_avail(void)
9637a8f09acSThomas Richter {
9647a8f09acSThomas Richter 	return test_facility(142);
9657a8f09acSThomas Richter }
9667a8f09acSThomas Richter 
9671e99c242SThomas Richter /* CPU-measurement alerts for the counter facility */
9681e99c242SThomas Richter static void cpumf_measurement_alert(struct ext_code ext_code,
9691e99c242SThomas Richter 				    unsigned int alert, unsigned long unused)
9701e99c242SThomas Richter {
9711e99c242SThomas Richter 	struct cpu_cf_events *cpuhw;
9721e99c242SThomas Richter 
9731e99c242SThomas Richter 	if (!(alert & CPU_MF_INT_CF_MASK))
9741e99c242SThomas Richter 		return;
9751e99c242SThomas Richter 
9761e99c242SThomas Richter 	inc_irq_stat(IRQEXT_CMC);
9771e99c242SThomas Richter 	cpuhw = this_cpu_ptr(&cpu_cf_events);
9781e99c242SThomas Richter 
9791e99c242SThomas Richter 	/*
9801e99c242SThomas Richter 	 * Measurement alerts are shared and might happen when the PMU
9811e99c242SThomas Richter 	 * is not reserved.  Ignore these alerts in this case.
9821e99c242SThomas Richter 	 */
9831e99c242SThomas Richter 	if (!(cpuhw->flags & PMU_F_RESERVED))
9841e99c242SThomas Richter 		return;
9851e99c242SThomas Richter 
9861e99c242SThomas Richter 	/* counter authorization change alert */
9871e99c242SThomas Richter 	if (alert & CPU_MF_INT_CF_CACA)
9881e99c242SThomas Richter 		qctri(&cpuhw->info);
9891e99c242SThomas Richter 
9901e99c242SThomas Richter 	/* loss of counter data alert */
9911e99c242SThomas Richter 	if (alert & CPU_MF_INT_CF_LCDA)
9921e99c242SThomas Richter 		pr_err("CPU[%i] Counter data was lost\n", smp_processor_id());
9931e99c242SThomas Richter 
9941e99c242SThomas Richter 	/* loss of MT counter data alert */
9951e99c242SThomas Richter 	if (alert & CPU_MF_INT_CF_MTDA)
9961e99c242SThomas Richter 		pr_warn("CPU[%i] MT counter data was lost\n",
9971e99c242SThomas Richter 			smp_processor_id());
9981e99c242SThomas Richter }
9991e99c242SThomas Richter 
1000a029a4eaSThomas Richter static int cfset_init(void);
1001212188a5SHendrik Brueckner static int __init cpumf_pmu_init(void)
1002212188a5SHendrik Brueckner {
1003212188a5SHendrik Brueckner 	int rc;
1004212188a5SHendrik Brueckner 
1005ea53e699SThomas Richter 	if (!cpum_cf_avail())
1006212188a5SHendrik Brueckner 		return -ENODEV;
1007212188a5SHendrik Brueckner 
10081e99c242SThomas Richter 	/*
10091e99c242SThomas Richter 	 * Clear bit 15 of cr0 to unauthorize problem-state to
10101e99c242SThomas Richter 	 * extract measurement counters
10111e99c242SThomas Richter 	 */
10121e99c242SThomas Richter 	ctl_clear_bit(0, 48);
10131e99c242SThomas Richter 
10141e99c242SThomas Richter 	/* register handler for measurement-alert interruptions */
10151e99c242SThomas Richter 	rc = register_external_irq(EXT_IRQ_MEASURE_ALERT,
10161e99c242SThomas Richter 				   cpumf_measurement_alert);
10171e99c242SThomas Richter 	if (rc) {
10181e99c242SThomas Richter 		pr_err("Registering for CPU-measurement alerts failed with rc=%i\n", rc);
10191e99c242SThomas Richter 		return rc;
10201e99c242SThomas Richter 	}
10211e99c242SThomas Richter 
1022a029a4eaSThomas Richter 	/* Setup s390dbf facility */
1023a029a4eaSThomas Richter 	cf_dbg = debug_register(KMSG_COMPONENT, 2, 1, 128);
1024a029a4eaSThomas Richter 	if (!cf_dbg) {
1025a029a4eaSThomas Richter 		pr_err("Registration of s390dbf(cpum_cf) failed\n");
10261e99c242SThomas Richter 		rc = -ENOMEM;
10271e99c242SThomas Richter 		goto out1;
10287d244643Skernel test robot 	}
1029a029a4eaSThomas Richter 	debug_register_view(cf_dbg, &debug_sprintf_view);
1030a029a4eaSThomas Richter 
1031c7168325SHendrik Brueckner 	cpumf_pmu.attr_groups = cpumf_cf_event_group();
10326a82e23fSThomas Richter 	rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", -1);
1033a029a4eaSThomas Richter 	if (rc) {
1034212188a5SHendrik Brueckner 		pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc);
10351e99c242SThomas Richter 		goto out2;
1036a029a4eaSThomas Richter 	} else if (stccm_avail()) {	/* Setup counter set device */
1037a029a4eaSThomas Richter 		cfset_init();
1038a029a4eaSThomas Richter 	}
10391e99c242SThomas Richter 
10401e99c242SThomas Richter 	rc = cpuhp_setup_state(CPUHP_AP_PERF_S390_CF_ONLINE,
10411e99c242SThomas Richter 			       "perf/s390/cf:online",
10421e99c242SThomas Richter 			       cpum_cf_online_cpu, cpum_cf_offline_cpu);
10431e99c242SThomas Richter 	return rc;
10441e99c242SThomas Richter 
10451e99c242SThomas Richter out2:
10461e99c242SThomas Richter 	debug_unregister_view(cf_dbg, &debug_sprintf_view);
10471e99c242SThomas Richter 	debug_unregister(cf_dbg);
10481e99c242SThomas Richter out1:
10491e99c242SThomas Richter 	unregister_external_irq(EXT_IRQ_MEASURE_ALERT, cpumf_measurement_alert);
1050212188a5SHendrik Brueckner 	return rc;
1051212188a5SHendrik Brueckner }
1052a029a4eaSThomas Richter 
1053a029a4eaSThomas Richter /* Support for the CPU Measurement Facility counter set extraction using
1054a029a4eaSThomas Richter  * device /dev/hwctr. This allows user space programs to extract complete
1055a029a4eaSThomas Richter  * counter set via normal file operations.
1056a029a4eaSThomas Richter  */
1057a029a4eaSThomas Richter 
105845338031SThomas Richter static atomic_t cfset_opencnt = ATOMIC_INIT(0);		/* Access count */
1059a029a4eaSThomas Richter static DEFINE_MUTEX(cfset_ctrset_mutex);/* Synchronize access to hardware */
1060a029a4eaSThomas Richter struct cfset_call_on_cpu_parm {		/* Parm struct for smp_call_on_cpu */
1061a029a4eaSThomas Richter 	unsigned int sets;		/* Counter set bit mask */
1062a029a4eaSThomas Richter 	atomic_t cpus_ack;		/* # CPUs successfully executed func */
1063a029a4eaSThomas Richter };
1064a029a4eaSThomas Richter 
106545338031SThomas Richter static struct cfset_session {		/* CPUs and counter set bit mask */
106645338031SThomas Richter 	struct list_head head;		/* Head of list of active processes */
106745338031SThomas Richter } cfset_session = {
106845338031SThomas Richter 	.head = LIST_HEAD_INIT(cfset_session.head)
106945338031SThomas Richter };
107045338031SThomas Richter 
107145338031SThomas Richter struct cfset_request {			/* CPUs and counter set bit mask */
1072a029a4eaSThomas Richter 	unsigned long ctrset;		/* Bit mask of counter set to read */
1073a029a4eaSThomas Richter 	cpumask_t mask;			/* CPU mask to read from */
107445338031SThomas Richter 	struct list_head node;		/* Chain to cfset_session.head */
107545338031SThomas Richter };
1076a029a4eaSThomas Richter 
107745338031SThomas Richter static void cfset_session_init(void)
1078a029a4eaSThomas Richter {
107945338031SThomas Richter 	INIT_LIST_HEAD(&cfset_session.head);
108045338031SThomas Richter }
108145338031SThomas Richter 
108245338031SThomas Richter /* Remove current request from global bookkeeping. Maintain a counter set bit
108345338031SThomas Richter  * mask on a per CPU basis.
108445338031SThomas Richter  * Done in process context under mutex protection.
108545338031SThomas Richter  */
108645338031SThomas Richter static void cfset_session_del(struct cfset_request *p)
108745338031SThomas Richter {
108845338031SThomas Richter 	list_del(&p->node);
108945338031SThomas Richter }
109045338031SThomas Richter 
109145338031SThomas Richter /* Add current request to global bookkeeping. Maintain a counter set bit mask
109245338031SThomas Richter  * on a per CPU basis.
109345338031SThomas Richter  * Done in process context under mutex protection.
109445338031SThomas Richter  */
109545338031SThomas Richter static void cfset_session_add(struct cfset_request *p)
109645338031SThomas Richter {
109745338031SThomas Richter 	list_add(&p->node, &cfset_session.head);
1098a029a4eaSThomas Richter }
1099a029a4eaSThomas Richter 
1100a029a4eaSThomas Richter /* The /dev/hwctr device access uses PMU_F_IN_USE to mark the device access
1101a029a4eaSThomas Richter  * path is currently used.
1102a029a4eaSThomas Richter  * The cpu_cf_events::dev_state is used to denote counter sets in use by this
1103a029a4eaSThomas Richter  * interface. It is always or'ed in. If this interface is not active, its
1104a029a4eaSThomas Richter  * value is zero and no additional counter sets will be included.
1105a029a4eaSThomas Richter  *
1106a029a4eaSThomas Richter  * The cpu_cf_events::state is used by the perf_event_open SVC and remains
1107a029a4eaSThomas Richter  * unchanged.
1108a029a4eaSThomas Richter  *
1109a029a4eaSThomas Richter  * perf_pmu_enable() and perf_pmu_enable() and its call backs
1110a029a4eaSThomas Richter  * cpumf_pmu_enable() and  cpumf_pmu_disable() are called by the
1111a029a4eaSThomas Richter  * performance measurement subsystem to enable per process
1112a029a4eaSThomas Richter  * CPU Measurement counter facility.
1113a029a4eaSThomas Richter  * The XXX_enable() and XXX_disable functions are used to turn off
1114a029a4eaSThomas Richter  * x86 performance monitoring interrupt (PMI) during scheduling.
1115a029a4eaSThomas Richter  * s390 uses these calls to temporarily stop and resume the active CPU
1116a029a4eaSThomas Richter  * counters sets during scheduling.
1117a029a4eaSThomas Richter  *
1118a029a4eaSThomas Richter  * We do allow concurrent access of perf_event_open() SVC and /dev/hwctr
1119a029a4eaSThomas Richter  * device access.  The perf_event_open() SVC interface makes a lot of effort
1120a029a4eaSThomas Richter  * to only run the counters while the calling process is actively scheduled
1121a029a4eaSThomas Richter  * to run.
1122a029a4eaSThomas Richter  * When /dev/hwctr interface is also used at the same time, the counter sets
1123a029a4eaSThomas Richter  * will keep running, even when the process is scheduled off a CPU.
1124a029a4eaSThomas Richter  * However this is not a problem and does not lead to wrong counter values
1125a029a4eaSThomas Richter  * for the perf_event_open() SVC. The current counter value will be recorded
1126a029a4eaSThomas Richter  * during schedule-in. At schedule-out time the current counter value is
1127a029a4eaSThomas Richter  * extracted again and the delta is calculated and added to the event.
1128a029a4eaSThomas Richter  */
1129a029a4eaSThomas Richter /* Stop all counter sets via ioctl interface */
1130a029a4eaSThomas Richter static void cfset_ioctl_off(void *parm)
1131a029a4eaSThomas Richter {
1132a029a4eaSThomas Richter 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
1133a029a4eaSThomas Richter 	struct cfset_call_on_cpu_parm *p = parm;
1134a029a4eaSThomas Richter 	int rc;
1135a029a4eaSThomas Richter 
113645338031SThomas Richter 	/* Check if any counter set used by /dev/hwc */
1137a029a4eaSThomas Richter 	for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc)
113845338031SThomas Richter 		if ((p->sets & cpumf_ctr_ctl[rc])) {
113945338031SThomas Richter 			if (!atomic_dec_return(&cpuhw->ctr_set[rc])) {
114045338031SThomas Richter 				ctr_set_disable(&cpuhw->dev_state,
114145338031SThomas Richter 						cpumf_ctr_ctl[rc]);
114245338031SThomas Richter 				ctr_set_stop(&cpuhw->dev_state,
114345338031SThomas Richter 					     cpumf_ctr_ctl[rc]);
114445338031SThomas Richter 			}
114545338031SThomas Richter 		}
114645338031SThomas Richter 	/* Keep perf_event_open counter sets */
114745338031SThomas Richter 	rc = lcctl(cpuhw->dev_state | cpuhw->state);
1148a029a4eaSThomas Richter 	if (rc)
1149a029a4eaSThomas Richter 		pr_err("Counter set stop %#llx of /dev/%s failed rc=%i\n",
1150a029a4eaSThomas Richter 		       cpuhw->state, S390_HWCTR_DEVICE, rc);
115145338031SThomas Richter 	if (!cpuhw->dev_state)
1152a029a4eaSThomas Richter 		cpuhw->flags &= ~PMU_F_IN_USE;
1153a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 4, "%s rc %d state %#llx dev_state %#llx\n",
1154a029a4eaSThomas Richter 			    __func__, rc, cpuhw->state, cpuhw->dev_state);
1155a029a4eaSThomas Richter }
1156a029a4eaSThomas Richter 
1157a029a4eaSThomas Richter /* Start counter sets on particular CPU */
1158a029a4eaSThomas Richter static void cfset_ioctl_on(void *parm)
1159a029a4eaSThomas Richter {
1160a029a4eaSThomas Richter 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
1161a029a4eaSThomas Richter 	struct cfset_call_on_cpu_parm *p = parm;
1162a029a4eaSThomas Richter 	int rc;
1163a029a4eaSThomas Richter 
1164a029a4eaSThomas Richter 	cpuhw->flags |= PMU_F_IN_USE;
1165a029a4eaSThomas Richter 	ctr_set_enable(&cpuhw->dev_state, p->sets);
1166a029a4eaSThomas Richter 	ctr_set_start(&cpuhw->dev_state, p->sets);
1167a029a4eaSThomas Richter 	for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc)
1168a029a4eaSThomas Richter 		if ((p->sets & cpumf_ctr_ctl[rc]))
1169a029a4eaSThomas Richter 			atomic_inc(&cpuhw->ctr_set[rc]);
1170a029a4eaSThomas Richter 	rc = lcctl(cpuhw->dev_state | cpuhw->state);	/* Start counter sets */
1171a029a4eaSThomas Richter 	if (!rc)
1172a029a4eaSThomas Richter 		atomic_inc(&p->cpus_ack);
1173a029a4eaSThomas Richter 	else
1174a029a4eaSThomas Richter 		pr_err("Counter set start %#llx of /dev/%s failed rc=%i\n",
1175a029a4eaSThomas Richter 		       cpuhw->dev_state | cpuhw->state, S390_HWCTR_DEVICE, rc);
1176a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 4, "%s rc %d state %#llx dev_state %#llx\n",
1177a029a4eaSThomas Richter 			    __func__, rc, cpuhw->state, cpuhw->dev_state);
1178a029a4eaSThomas Richter }
1179a029a4eaSThomas Richter 
1180a029a4eaSThomas Richter static void cfset_release_cpu(void *p)
1181a029a4eaSThomas Richter {
1182a029a4eaSThomas Richter 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
1183a029a4eaSThomas Richter 	int rc;
1184a029a4eaSThomas Richter 
1185a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 4, "%s state %#llx dev_state %#llx\n",
1186a029a4eaSThomas Richter 			    __func__, cpuhw->state, cpuhw->dev_state);
118745338031SThomas Richter 	cpuhw->dev_state = 0;
1188a029a4eaSThomas Richter 	rc = lcctl(cpuhw->state);	/* Keep perf_event_open counter sets */
1189a029a4eaSThomas Richter 	if (rc)
1190a029a4eaSThomas Richter 		pr_err("Counter set release %#llx of /dev/%s failed rc=%i\n",
1191a029a4eaSThomas Richter 		       cpuhw->state, S390_HWCTR_DEVICE, rc);
119245338031SThomas Richter }
119345338031SThomas Richter 
119445338031SThomas Richter /* This modifies the process CPU mask to adopt it to the currently online
119545338031SThomas Richter  * CPUs. Offline CPUs can not be addresses. This call terminates the access
119645338031SThomas Richter  * and is usually followed by close() or a new iotcl(..., START, ...) which
119745338031SThomas Richter  * creates a new request structure.
119845338031SThomas Richter  */
119945338031SThomas Richter static void cfset_all_stop(struct cfset_request *req)
120045338031SThomas Richter {
120145338031SThomas Richter 	struct cfset_call_on_cpu_parm p = {
120245338031SThomas Richter 		.sets = req->ctrset,
120345338031SThomas Richter 	};
120445338031SThomas Richter 
120545338031SThomas Richter 	cpumask_and(&req->mask, &req->mask, cpu_online_mask);
120645338031SThomas Richter 	on_each_cpu_mask(&req->mask, cfset_ioctl_off, &p, 1);
1207a029a4eaSThomas Richter }
1208a029a4eaSThomas Richter 
1209a029a4eaSThomas Richter /* Release function is also called when application gets terminated without
1210a029a4eaSThomas Richter  * doing a proper ioctl(..., S390_HWCTR_STOP, ...) command.
1211a029a4eaSThomas Richter  */
1212a029a4eaSThomas Richter static int cfset_release(struct inode *inode, struct file *file)
1213a029a4eaSThomas Richter {
121445338031SThomas Richter 	mutex_lock(&cfset_ctrset_mutex);
121545338031SThomas Richter 	/* Open followed by close/exit has no private_data */
121645338031SThomas Richter 	if (file->private_data) {
121745338031SThomas Richter 		cfset_all_stop(file->private_data);
121845338031SThomas Richter 		cfset_session_del(file->private_data);
121945338031SThomas Richter 		kfree(file->private_data);
122045338031SThomas Richter 		file->private_data = NULL;
122145338031SThomas Richter 	}
122245338031SThomas Richter 	if (!atomic_dec_return(&cfset_opencnt))
1223a029a4eaSThomas Richter 		on_each_cpu(cfset_release_cpu, NULL, 1);
122445338031SThomas Richter 	mutex_unlock(&cfset_ctrset_mutex);
122545338031SThomas Richter 
1226a029a4eaSThomas Richter 	hw_perf_event_destroy(NULL);
1227a029a4eaSThomas Richter 	return 0;
1228a029a4eaSThomas Richter }
1229a029a4eaSThomas Richter 
1230a029a4eaSThomas Richter static int cfset_open(struct inode *inode, struct file *file)
1231a029a4eaSThomas Richter {
1232a029a4eaSThomas Richter 	if (!capable(CAP_SYS_ADMIN))
1233a029a4eaSThomas Richter 		return -EPERM;
123445338031SThomas Richter 	mutex_lock(&cfset_ctrset_mutex);
123545338031SThomas Richter 	if (atomic_inc_return(&cfset_opencnt) == 1)
123645338031SThomas Richter 		cfset_session_init();
123745338031SThomas Richter 	mutex_unlock(&cfset_ctrset_mutex);
1238a029a4eaSThomas Richter 
1239a029a4eaSThomas Richter 	cpumf_hw_inuse();
1240a029a4eaSThomas Richter 	file->private_data = NULL;
1241a029a4eaSThomas Richter 	/* nonseekable_open() never fails */
1242a029a4eaSThomas Richter 	return nonseekable_open(inode, file);
1243a029a4eaSThomas Richter }
1244a029a4eaSThomas Richter 
124545338031SThomas Richter static int cfset_all_start(struct cfset_request *req)
1246a029a4eaSThomas Richter {
1247a029a4eaSThomas Richter 	struct cfset_call_on_cpu_parm p = {
124845338031SThomas Richter 		.sets = req->ctrset,
1249a029a4eaSThomas Richter 		.cpus_ack = ATOMIC_INIT(0),
1250a029a4eaSThomas Richter 	};
1251a029a4eaSThomas Richter 	cpumask_var_t mask;
1252a029a4eaSThomas Richter 	int rc = 0;
1253a029a4eaSThomas Richter 
1254a029a4eaSThomas Richter 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1255a029a4eaSThomas Richter 		return -ENOMEM;
125645338031SThomas Richter 	cpumask_and(mask, &req->mask, cpu_online_mask);
1257a029a4eaSThomas Richter 	on_each_cpu_mask(mask, cfset_ioctl_on, &p, 1);
1258a029a4eaSThomas Richter 	if (atomic_read(&p.cpus_ack) != cpumask_weight(mask)) {
1259a029a4eaSThomas Richter 		on_each_cpu_mask(mask, cfset_ioctl_off, &p, 1);
1260a029a4eaSThomas Richter 		rc = -EIO;
1261a029a4eaSThomas Richter 		debug_sprintf_event(cf_dbg, 4, "%s CPUs missing", __func__);
1262a029a4eaSThomas Richter 	}
1263a029a4eaSThomas Richter 	free_cpumask_var(mask);
1264a029a4eaSThomas Richter 	return rc;
1265a029a4eaSThomas Richter }
1266a029a4eaSThomas Richter 
1267a029a4eaSThomas Richter /* Return the maximum required space for all possible CPUs in case one
1268a029a4eaSThomas Richter  * CPU will be onlined during the START, READ, STOP cycles.
1269a029a4eaSThomas Richter  * To find out the size of the counter sets, any one CPU will do. They
1270a029a4eaSThomas Richter  * all have the same counter sets.
1271a029a4eaSThomas Richter  */
1272a029a4eaSThomas Richter static size_t cfset_needspace(unsigned int sets)
1273a029a4eaSThomas Richter {
1274a029a4eaSThomas Richter 	struct cpu_cf_events *cpuhw = get_cpu_ptr(&cpu_cf_events);
1275a029a4eaSThomas Richter 	size_t bytes = 0;
1276a029a4eaSThomas Richter 	int i;
1277a029a4eaSThomas Richter 
1278a029a4eaSThomas Richter 	for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1279a029a4eaSThomas Richter 		if (!(sets & cpumf_ctr_ctl[i]))
1280a029a4eaSThomas Richter 			continue;
1281a029a4eaSThomas Richter 		bytes += cpum_cf_ctrset_size(i, &cpuhw->info) * sizeof(u64) +
1282a029a4eaSThomas Richter 			 sizeof(((struct s390_ctrset_setdata *)0)->set) +
1283a029a4eaSThomas Richter 			 sizeof(((struct s390_ctrset_setdata *)0)->no_cnts);
1284a029a4eaSThomas Richter 	}
1285a029a4eaSThomas Richter 	bytes = sizeof(((struct s390_ctrset_read *)0)->no_cpus) + nr_cpu_ids *
1286a029a4eaSThomas Richter 		(bytes + sizeof(((struct s390_ctrset_cpudata *)0)->cpu_nr) +
1287a029a4eaSThomas Richter 		     sizeof(((struct s390_ctrset_cpudata *)0)->no_sets));
1288a029a4eaSThomas Richter 	put_cpu_ptr(&cpu_cf_events);
1289a029a4eaSThomas Richter 	return bytes;
1290a029a4eaSThomas Richter }
1291a029a4eaSThomas Richter 
1292a029a4eaSThomas Richter static int cfset_all_copy(unsigned long arg, cpumask_t *mask)
1293a029a4eaSThomas Richter {
1294a029a4eaSThomas Richter 	struct s390_ctrset_read __user *ctrset_read;
1295a029a4eaSThomas Richter 	unsigned int cpu, cpus, rc;
1296a029a4eaSThomas Richter 	void __user *uptr;
1297a029a4eaSThomas Richter 
1298a029a4eaSThomas Richter 	ctrset_read = (struct s390_ctrset_read __user *)arg;
1299a029a4eaSThomas Richter 	uptr = ctrset_read->data;
1300a029a4eaSThomas Richter 	for_each_cpu(cpu, mask) {
1301a029a4eaSThomas Richter 		struct cpu_cf_events *cpuhw = per_cpu_ptr(&cpu_cf_events, cpu);
1302a029a4eaSThomas Richter 		struct s390_ctrset_cpudata __user *ctrset_cpudata;
1303a029a4eaSThomas Richter 
1304a029a4eaSThomas Richter 		ctrset_cpudata = uptr;
1305a029a4eaSThomas Richter 		rc  = put_user(cpu, &ctrset_cpudata->cpu_nr);
1306a029a4eaSThomas Richter 		rc |= put_user(cpuhw->sets, &ctrset_cpudata->no_sets);
1307a029a4eaSThomas Richter 		rc |= copy_to_user(ctrset_cpudata->data, cpuhw->data,
1308a029a4eaSThomas Richter 				   cpuhw->used);
1309a029a4eaSThomas Richter 		if (rc)
1310a029a4eaSThomas Richter 			return -EFAULT;
1311a029a4eaSThomas Richter 		uptr += sizeof(struct s390_ctrset_cpudata) + cpuhw->used;
1312a029a4eaSThomas Richter 		cond_resched();
1313a029a4eaSThomas Richter 	}
1314a029a4eaSThomas Richter 	cpus = cpumask_weight(mask);
1315a029a4eaSThomas Richter 	if (put_user(cpus, &ctrset_read->no_cpus))
1316a029a4eaSThomas Richter 		return -EFAULT;
1317a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 4, "%s copied %ld\n", __func__,
1318a029a4eaSThomas Richter 			    uptr - (void __user *)ctrset_read->data);
1319a029a4eaSThomas Richter 	return 0;
1320a029a4eaSThomas Richter }
1321a029a4eaSThomas Richter 
1322a029a4eaSThomas Richter static size_t cfset_cpuset_read(struct s390_ctrset_setdata *p, int ctrset,
1323a029a4eaSThomas Richter 				int ctrset_size, size_t room)
1324a029a4eaSThomas Richter {
1325a029a4eaSThomas Richter 	size_t need = 0;
1326a029a4eaSThomas Richter 	int rc = -1;
1327a029a4eaSThomas Richter 
1328a029a4eaSThomas Richter 	need = sizeof(*p) + sizeof(u64) * ctrset_size;
1329a029a4eaSThomas Richter 	if (need <= room) {
1330a029a4eaSThomas Richter 		p->set = cpumf_ctr_ctl[ctrset];
1331a029a4eaSThomas Richter 		p->no_cnts = ctrset_size;
1332a029a4eaSThomas Richter 		rc = ctr_stcctm(ctrset, ctrset_size, (u64 *)p->cv);
1333a029a4eaSThomas Richter 		if (rc == 3)		/* Nothing stored */
1334a029a4eaSThomas Richter 			need = 0;
1335a029a4eaSThomas Richter 	}
1336a029a4eaSThomas Richter 	return need;
1337a029a4eaSThomas Richter }
1338a029a4eaSThomas Richter 
1339a029a4eaSThomas Richter /* Read all counter sets. */
1340a029a4eaSThomas Richter static void cfset_cpu_read(void *parm)
1341a029a4eaSThomas Richter {
1342a029a4eaSThomas Richter 	struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
1343a029a4eaSThomas Richter 	struct cfset_call_on_cpu_parm *p = parm;
1344a029a4eaSThomas Richter 	int set, set_size;
1345a029a4eaSThomas Richter 	size_t space;
1346a029a4eaSThomas Richter 
1347a029a4eaSThomas Richter 	/* No data saved yet */
1348a029a4eaSThomas Richter 	cpuhw->used = 0;
1349a029a4eaSThomas Richter 	cpuhw->sets = 0;
1350a029a4eaSThomas Richter 	memset(cpuhw->data, 0, sizeof(cpuhw->data));
1351a029a4eaSThomas Richter 
1352a029a4eaSThomas Richter 	/* Scan the counter sets */
1353a029a4eaSThomas Richter 	for (set = CPUMF_CTR_SET_BASIC; set < CPUMF_CTR_SET_MAX; ++set) {
1354a029a4eaSThomas Richter 		struct s390_ctrset_setdata *sp = (void *)cpuhw->data +
1355a029a4eaSThomas Richter 						 cpuhw->used;
1356a029a4eaSThomas Richter 
1357a029a4eaSThomas Richter 		if (!(p->sets & cpumf_ctr_ctl[set]))
1358a029a4eaSThomas Richter 			continue;	/* Counter set not in list */
1359a029a4eaSThomas Richter 		set_size = cpum_cf_ctrset_size(set, &cpuhw->info);
1360a029a4eaSThomas Richter 		space = sizeof(cpuhw->data) - cpuhw->used;
1361a029a4eaSThomas Richter 		space = cfset_cpuset_read(sp, set, set_size, space);
1362a029a4eaSThomas Richter 		if (space) {
1363a029a4eaSThomas Richter 			cpuhw->used += space;
1364a029a4eaSThomas Richter 			cpuhw->sets += 1;
1365a029a4eaSThomas Richter 		}
1366a029a4eaSThomas Richter 	}
1367a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 4, "%s sets %d used %zd\n", __func__,
1368a029a4eaSThomas Richter 			    cpuhw->sets, cpuhw->used);
1369a029a4eaSThomas Richter }
1370a029a4eaSThomas Richter 
137145338031SThomas Richter static int cfset_all_read(unsigned long arg, struct cfset_request *req)
1372a029a4eaSThomas Richter {
1373a029a4eaSThomas Richter 	struct cfset_call_on_cpu_parm p;
1374a029a4eaSThomas Richter 	cpumask_var_t mask;
1375a029a4eaSThomas Richter 	int rc;
1376a029a4eaSThomas Richter 
1377a029a4eaSThomas Richter 	if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1378a029a4eaSThomas Richter 		return -ENOMEM;
1379a029a4eaSThomas Richter 
138045338031SThomas Richter 	p.sets = req->ctrset;
138145338031SThomas Richter 	cpumask_and(mask, &req->mask, cpu_online_mask);
1382a029a4eaSThomas Richter 	on_each_cpu_mask(mask, cfset_cpu_read, &p, 1);
1383a029a4eaSThomas Richter 	rc = cfset_all_copy(arg, mask);
1384a029a4eaSThomas Richter 	free_cpumask_var(mask);
1385a029a4eaSThomas Richter 	return rc;
1386a029a4eaSThomas Richter }
1387a029a4eaSThomas Richter 
138845338031SThomas Richter static long cfset_ioctl_read(unsigned long arg, struct cfset_request *req)
1389a029a4eaSThomas Richter {
1390a029a4eaSThomas Richter 	struct s390_ctrset_read read;
139145338031SThomas Richter 	int ret = -ENODATA;
1392a029a4eaSThomas Richter 
139345338031SThomas Richter 	if (req && req->ctrset) {
1394a029a4eaSThomas Richter 		if (copy_from_user(&read, (char __user *)arg, sizeof(read)))
1395a029a4eaSThomas Richter 			return -EFAULT;
139645338031SThomas Richter 		ret = cfset_all_read(arg, req);
139745338031SThomas Richter 	}
1398a029a4eaSThomas Richter 	return ret;
1399a029a4eaSThomas Richter }
1400a029a4eaSThomas Richter 
140145338031SThomas Richter static long cfset_ioctl_stop(struct file *file)
1402a029a4eaSThomas Richter {
140345338031SThomas Richter 	struct cfset_request *req = file->private_data;
140445338031SThomas Richter 	int ret = -ENXIO;
1405a029a4eaSThomas Richter 
140645338031SThomas Richter 	if (req) {
140745338031SThomas Richter 		cfset_all_stop(req);
140845338031SThomas Richter 		cfset_session_del(req);
140945338031SThomas Richter 		kfree(req);
141045338031SThomas Richter 		file->private_data = NULL;
141145338031SThomas Richter 		ret = 0;
1412a029a4eaSThomas Richter 	}
1413a029a4eaSThomas Richter 	return ret;
1414a029a4eaSThomas Richter }
1415a029a4eaSThomas Richter 
141645338031SThomas Richter static long cfset_ioctl_start(unsigned long arg, struct file *file)
1417a029a4eaSThomas Richter {
1418a029a4eaSThomas Richter 	struct s390_ctrset_start __user *ustart;
1419a029a4eaSThomas Richter 	struct s390_ctrset_start start;
142045338031SThomas Richter 	struct cfset_request *preq;
1421a029a4eaSThomas Richter 	void __user *umask;
1422a029a4eaSThomas Richter 	unsigned int len;
1423a029a4eaSThomas Richter 	int ret = 0;
1424a029a4eaSThomas Richter 	size_t need;
1425a029a4eaSThomas Richter 
142645338031SThomas Richter 	if (file->private_data)
1427a029a4eaSThomas Richter 		return -EBUSY;
1428a029a4eaSThomas Richter 	ustart = (struct s390_ctrset_start __user *)arg;
1429a029a4eaSThomas Richter 	if (copy_from_user(&start, ustart, sizeof(start)))
1430a029a4eaSThomas Richter 		return -EFAULT;
1431a029a4eaSThomas Richter 	if (start.version != S390_HWCTR_START_VERSION)
1432a029a4eaSThomas Richter 		return -EINVAL;
1433a029a4eaSThomas Richter 	if (start.counter_sets & ~(cpumf_ctr_ctl[CPUMF_CTR_SET_BASIC] |
1434a029a4eaSThomas Richter 				   cpumf_ctr_ctl[CPUMF_CTR_SET_USER] |
1435a029a4eaSThomas Richter 				   cpumf_ctr_ctl[CPUMF_CTR_SET_CRYPTO] |
1436a029a4eaSThomas Richter 				   cpumf_ctr_ctl[CPUMF_CTR_SET_EXT] |
1437a029a4eaSThomas Richter 				   cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG]))
1438a029a4eaSThomas Richter 		return -EINVAL;		/* Invalid counter set */
1439a029a4eaSThomas Richter 	if (!start.counter_sets)
1440a029a4eaSThomas Richter 		return -EINVAL;		/* No counter set at all? */
144145338031SThomas Richter 
144245338031SThomas Richter 	preq = kzalloc(sizeof(*preq), GFP_KERNEL);
144345338031SThomas Richter 	if (!preq)
144445338031SThomas Richter 		return -ENOMEM;
144545338031SThomas Richter 	cpumask_clear(&preq->mask);
1446a029a4eaSThomas Richter 	len = min_t(u64, start.cpumask_len, cpumask_size());
1447a029a4eaSThomas Richter 	umask = (void __user *)start.cpumask;
144845338031SThomas Richter 	if (copy_from_user(&preq->mask, umask, len)) {
144945338031SThomas Richter 		kfree(preq);
1450a029a4eaSThomas Richter 		return -EFAULT;
145145338031SThomas Richter 	}
145245338031SThomas Richter 	if (cpumask_empty(&preq->mask)) {
145345338031SThomas Richter 		kfree(preq);
1454a029a4eaSThomas Richter 		return -EINVAL;
145545338031SThomas Richter 	}
1456a029a4eaSThomas Richter 	need = cfset_needspace(start.counter_sets);
145745338031SThomas Richter 	if (put_user(need, &ustart->data_bytes)) {
145845338031SThomas Richter 		kfree(preq);
145945338031SThomas Richter 		return -EFAULT;
146045338031SThomas Richter 	}
146145338031SThomas Richter 	preq->ctrset = start.counter_sets;
146245338031SThomas Richter 	ret = cfset_all_start(preq);
146345338031SThomas Richter 	if (!ret) {
146445338031SThomas Richter 		cfset_session_add(preq);
146545338031SThomas Richter 		file->private_data = preq;
146645338031SThomas Richter 		debug_sprintf_event(cf_dbg, 4, "%s set %#lx need %ld ret %d\n",
146745338031SThomas Richter 				    __func__, preq->ctrset, need, ret);
146845338031SThomas Richter 	} else {
146945338031SThomas Richter 		kfree(preq);
147045338031SThomas Richter 	}
1471a029a4eaSThomas Richter 	return ret;
1472a029a4eaSThomas Richter }
1473a029a4eaSThomas Richter 
1474a029a4eaSThomas Richter /* Entry point to the /dev/hwctr device interface.
1475a029a4eaSThomas Richter  * The ioctl system call supports three subcommands:
1476a029a4eaSThomas Richter  * S390_HWCTR_START: Start the specified counter sets on a CPU list. The
1477a029a4eaSThomas Richter  *    counter set keeps running until explicitly stopped. Returns the number
1478a029a4eaSThomas Richter  *    of bytes needed to store the counter values. If another S390_HWCTR_START
1479a029a4eaSThomas Richter  *    ioctl subcommand is called without a previous S390_HWCTR_STOP stop
148045338031SThomas Richter  *    command on the same file descriptor, -EBUSY is returned.
1481a029a4eaSThomas Richter  * S390_HWCTR_READ: Read the counter set values from specified CPU list given
1482a029a4eaSThomas Richter  *    with the S390_HWCTR_START command.
1483a029a4eaSThomas Richter  * S390_HWCTR_STOP: Stops the counter sets on the CPU list given with the
1484a029a4eaSThomas Richter  *    previous S390_HWCTR_START subcommand.
1485a029a4eaSThomas Richter  */
1486a029a4eaSThomas Richter static long cfset_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1487a029a4eaSThomas Richter {
1488a029a4eaSThomas Richter 	int ret;
1489a029a4eaSThomas Richter 
1490a73de293SSebastian Andrzej Siewior 	cpus_read_lock();
1491a029a4eaSThomas Richter 	mutex_lock(&cfset_ctrset_mutex);
1492a029a4eaSThomas Richter 	switch (cmd) {
1493a029a4eaSThomas Richter 	case S390_HWCTR_START:
149445338031SThomas Richter 		ret = cfset_ioctl_start(arg, file);
1495a029a4eaSThomas Richter 		break;
1496a029a4eaSThomas Richter 	case S390_HWCTR_STOP:
149745338031SThomas Richter 		ret = cfset_ioctl_stop(file);
1498a029a4eaSThomas Richter 		break;
1499a029a4eaSThomas Richter 	case S390_HWCTR_READ:
150045338031SThomas Richter 		ret = cfset_ioctl_read(arg, file->private_data);
1501a029a4eaSThomas Richter 		break;
1502a029a4eaSThomas Richter 	default:
1503a029a4eaSThomas Richter 		ret = -ENOTTY;
1504a029a4eaSThomas Richter 		break;
1505a029a4eaSThomas Richter 	}
1506a029a4eaSThomas Richter 	mutex_unlock(&cfset_ctrset_mutex);
1507a73de293SSebastian Andrzej Siewior 	cpus_read_unlock();
1508a029a4eaSThomas Richter 	return ret;
1509a029a4eaSThomas Richter }
1510a029a4eaSThomas Richter 
1511a029a4eaSThomas Richter static const struct file_operations cfset_fops = {
1512a029a4eaSThomas Richter 	.owner = THIS_MODULE,
1513a029a4eaSThomas Richter 	.open = cfset_open,
1514a029a4eaSThomas Richter 	.release = cfset_release,
1515a029a4eaSThomas Richter 	.unlocked_ioctl	= cfset_ioctl,
1516a029a4eaSThomas Richter 	.compat_ioctl = cfset_ioctl,
1517a029a4eaSThomas Richter 	.llseek = no_llseek
1518a029a4eaSThomas Richter };
1519a029a4eaSThomas Richter 
1520a029a4eaSThomas Richter static struct miscdevice cfset_dev = {
1521a029a4eaSThomas Richter 	.name	= S390_HWCTR_DEVICE,
1522a029a4eaSThomas Richter 	.minor	= MISC_DYNAMIC_MINOR,
1523a029a4eaSThomas Richter 	.fops	= &cfset_fops,
1524a029a4eaSThomas Richter };
1525a029a4eaSThomas Richter 
152645338031SThomas Richter /* Hotplug add of a CPU. Scan through all active processes and add
152745338031SThomas Richter  * that CPU to the list of CPUs supplied with ioctl(..., START, ...).
152845338031SThomas Richter  */
15291e99c242SThomas Richter static int cfset_online_cpu(unsigned int cpu)
1530a029a4eaSThomas Richter {
1531a029a4eaSThomas Richter 	struct cfset_call_on_cpu_parm p;
153245338031SThomas Richter 	struct cfset_request *rp;
1533a029a4eaSThomas Richter 
1534a029a4eaSThomas Richter 	mutex_lock(&cfset_ctrset_mutex);
153545338031SThomas Richter 	if (!list_empty(&cfset_session.head)) {
153645338031SThomas Richter 		list_for_each_entry(rp, &cfset_session.head, node) {
153745338031SThomas Richter 			p.sets = rp->ctrset;
1538a029a4eaSThomas Richter 			cfset_ioctl_on(&p);
153945338031SThomas Richter 			cpumask_set_cpu(cpu, &rp->mask);
154045338031SThomas Richter 		}
1541a029a4eaSThomas Richter 	}
1542a029a4eaSThomas Richter 	mutex_unlock(&cfset_ctrset_mutex);
1543a029a4eaSThomas Richter 	return 0;
1544a029a4eaSThomas Richter }
1545a029a4eaSThomas Richter 
154645338031SThomas Richter /* Hotplug remove of a CPU. Scan through all active processes and clear
154745338031SThomas Richter  * that CPU from the list of CPUs supplied with ioctl(..., START, ...).
154845338031SThomas Richter  */
15491e99c242SThomas Richter static int cfset_offline_cpu(unsigned int cpu)
1550a029a4eaSThomas Richter {
1551a029a4eaSThomas Richter 	struct cfset_call_on_cpu_parm p;
155245338031SThomas Richter 	struct cfset_request *rp;
1553a029a4eaSThomas Richter 
1554a029a4eaSThomas Richter 	mutex_lock(&cfset_ctrset_mutex);
155545338031SThomas Richter 	if (!list_empty(&cfset_session.head)) {
155645338031SThomas Richter 		list_for_each_entry(rp, &cfset_session.head, node) {
155745338031SThomas Richter 			p.sets = rp->ctrset;
1558a029a4eaSThomas Richter 			cfset_ioctl_off(&p);
155945338031SThomas Richter 			cpumask_clear_cpu(cpu, &rp->mask);
156045338031SThomas Richter 		}
1561a029a4eaSThomas Richter 	}
1562a029a4eaSThomas Richter 	mutex_unlock(&cfset_ctrset_mutex);
1563a029a4eaSThomas Richter 	return 0;
1564a029a4eaSThomas Richter }
1565a029a4eaSThomas Richter 
1566a029a4eaSThomas Richter static void cfdiag_read(struct perf_event *event)
1567a029a4eaSThomas Richter {
1568a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 3, "%s event %#llx count %ld\n", __func__,
1569a029a4eaSThomas Richter 			    event->attr.config, local64_read(&event->count));
1570a029a4eaSThomas Richter }
1571a029a4eaSThomas Richter 
1572a029a4eaSThomas Richter static int get_authctrsets(void)
1573a029a4eaSThomas Richter {
1574a029a4eaSThomas Richter 	struct cpu_cf_events *cpuhw;
1575a029a4eaSThomas Richter 	unsigned long auth = 0;
1576a029a4eaSThomas Richter 	enum cpumf_ctr_set i;
1577a029a4eaSThomas Richter 
1578a029a4eaSThomas Richter 	cpuhw = &get_cpu_var(cpu_cf_events);
1579a029a4eaSThomas Richter 	for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1580a029a4eaSThomas Richter 		if (cpuhw->info.auth_ctl & cpumf_ctr_ctl[i])
1581a029a4eaSThomas Richter 			auth |= cpumf_ctr_ctl[i];
1582a029a4eaSThomas Richter 	}
1583a029a4eaSThomas Richter 	put_cpu_var(cpu_cf_events);
1584a029a4eaSThomas Richter 	return auth;
1585a029a4eaSThomas Richter }
1586a029a4eaSThomas Richter 
1587a029a4eaSThomas Richter /* Setup the event. Test for authorized counter sets and only include counter
1588a029a4eaSThomas Richter  * sets which are authorized at the time of the setup. Including unauthorized
1589a029a4eaSThomas Richter  * counter sets result in specification exception (and panic).
1590a029a4eaSThomas Richter  */
1591a029a4eaSThomas Richter static int cfdiag_event_init2(struct perf_event *event)
1592a029a4eaSThomas Richter {
1593a029a4eaSThomas Richter 	struct perf_event_attr *attr = &event->attr;
1594a029a4eaSThomas Richter 	int err = 0;
1595a029a4eaSThomas Richter 
1596a029a4eaSThomas Richter 	/* Set sample_period to indicate sampling */
1597a029a4eaSThomas Richter 	event->hw.config = attr->config;
1598a029a4eaSThomas Richter 	event->hw.sample_period = attr->sample_period;
1599a029a4eaSThomas Richter 	local64_set(&event->hw.period_left, event->hw.sample_period);
1600a029a4eaSThomas Richter 	local64_set(&event->count, 0);
1601a029a4eaSThomas Richter 	event->hw.last_period = event->hw.sample_period;
1602a029a4eaSThomas Richter 
1603a029a4eaSThomas Richter 	/* Add all authorized counter sets to config_base. The
1604a029a4eaSThomas Richter 	 * the hardware init function is either called per-cpu or just once
1605a029a4eaSThomas Richter 	 * for all CPUS (event->cpu == -1).  This depends on the whether
1606a029a4eaSThomas Richter 	 * counting is started for all CPUs or on a per workload base where
1607a029a4eaSThomas Richter 	 * the perf event moves from one CPU to another CPU.
1608a029a4eaSThomas Richter 	 * Checking the authorization on any CPU is fine as the hardware
1609a029a4eaSThomas Richter 	 * applies the same authorization settings to all CPUs.
1610a029a4eaSThomas Richter 	 */
1611a029a4eaSThomas Richter 	event->hw.config_base = get_authctrsets();
1612a029a4eaSThomas Richter 
1613a029a4eaSThomas Richter 	/* No authorized counter sets, nothing to count/sample */
1614a029a4eaSThomas Richter 	if (!event->hw.config_base)
1615a029a4eaSThomas Richter 		err = -EINVAL;
1616a029a4eaSThomas Richter 
1617a029a4eaSThomas Richter 	debug_sprintf_event(cf_dbg, 5, "%s err %d config_base %#lx\n",
1618a029a4eaSThomas Richter 			    __func__, err, event->hw.config_base);
1619a029a4eaSThomas Richter 	return err;
1620a029a4eaSThomas Richter }
1621a029a4eaSThomas Richter 
1622a029a4eaSThomas Richter static int cfdiag_event_init(struct perf_event *event)
1623a029a4eaSThomas Richter {
1624a029a4eaSThomas Richter 	struct perf_event_attr *attr = &event->attr;
1625a029a4eaSThomas Richter 	int err = -ENOENT;
1626a029a4eaSThomas Richter 
1627a029a4eaSThomas Richter 	if (event->attr.config != PERF_EVENT_CPUM_CF_DIAG ||
1628a029a4eaSThomas Richter 	    event->attr.type != event->pmu->type)
1629a029a4eaSThomas Richter 		goto out;
1630a029a4eaSThomas Richter 
1631a029a4eaSThomas Richter 	/* Raw events are used to access counters directly,
1632a029a4eaSThomas Richter 	 * hence do not permit excludes.
1633a029a4eaSThomas Richter 	 * This event is useless without PERF_SAMPLE_RAW to return counter set
1634a029a4eaSThomas Richter 	 * values as raw data.
1635a029a4eaSThomas Richter 	 */
1636a029a4eaSThomas Richter 	if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv ||
1637a029a4eaSThomas Richter 	    !(attr->sample_type & (PERF_SAMPLE_CPU | PERF_SAMPLE_RAW))) {
1638a029a4eaSThomas Richter 		err = -EOPNOTSUPP;
1639a029a4eaSThomas Richter 		goto out;
1640a029a4eaSThomas Richter 	}
1641a029a4eaSThomas Richter 
1642a029a4eaSThomas Richter 	/* Initialize for using the CPU-measurement counter facility */
1643a029a4eaSThomas Richter 	cpumf_hw_inuse();
1644a029a4eaSThomas Richter 	event->destroy = hw_perf_event_destroy;
1645a029a4eaSThomas Richter 
1646a029a4eaSThomas Richter 	err = cfdiag_event_init2(event);
1647a029a4eaSThomas Richter 	if (unlikely(err))
1648a029a4eaSThomas Richter 		event->destroy(event);
1649a029a4eaSThomas Richter out:
1650a029a4eaSThomas Richter 	return err;
1651a029a4eaSThomas Richter }
1652a029a4eaSThomas Richter 
1653a029a4eaSThomas Richter /* Create cf_diag/events/CF_DIAG event sysfs file. This counter is used
1654a029a4eaSThomas Richter  * to collect the complete counter sets for a scheduled process. Target
1655a029a4eaSThomas Richter  * are complete counter sets attached as raw data to the artificial event.
1656a029a4eaSThomas Richter  * This results in complete counter sets available when a process is
1657a029a4eaSThomas Richter  * scheduled. Contains the delta of every counter while the process was
1658a029a4eaSThomas Richter  * running.
1659a029a4eaSThomas Richter  */
1660a029a4eaSThomas Richter CPUMF_EVENT_ATTR(CF_DIAG, CF_DIAG, PERF_EVENT_CPUM_CF_DIAG);
1661a029a4eaSThomas Richter 
1662a029a4eaSThomas Richter static struct attribute *cfdiag_events_attr[] = {
1663a029a4eaSThomas Richter 	CPUMF_EVENT_PTR(CF_DIAG, CF_DIAG),
1664a029a4eaSThomas Richter 	NULL,
1665a029a4eaSThomas Richter };
1666a029a4eaSThomas Richter 
1667a029a4eaSThomas Richter PMU_FORMAT_ATTR(event, "config:0-63");
1668a029a4eaSThomas Richter 
1669a029a4eaSThomas Richter static struct attribute *cfdiag_format_attr[] = {
1670a029a4eaSThomas Richter 	&format_attr_event.attr,
1671a029a4eaSThomas Richter 	NULL,
1672a029a4eaSThomas Richter };
1673a029a4eaSThomas Richter 
1674a029a4eaSThomas Richter static struct attribute_group cfdiag_events_group = {
1675a029a4eaSThomas Richter 	.name = "events",
1676a029a4eaSThomas Richter 	.attrs = cfdiag_events_attr,
1677a029a4eaSThomas Richter };
1678a029a4eaSThomas Richter static struct attribute_group cfdiag_format_group = {
1679a029a4eaSThomas Richter 	.name = "format",
1680a029a4eaSThomas Richter 	.attrs = cfdiag_format_attr,
1681a029a4eaSThomas Richter };
1682a029a4eaSThomas Richter static const struct attribute_group *cfdiag_attr_groups[] = {
1683a029a4eaSThomas Richter 	&cfdiag_events_group,
1684a029a4eaSThomas Richter 	&cfdiag_format_group,
1685a029a4eaSThomas Richter 	NULL,
1686a029a4eaSThomas Richter };
1687a029a4eaSThomas Richter 
1688a029a4eaSThomas Richter /* Performance monitoring unit for event CF_DIAG. Since this event
1689a029a4eaSThomas Richter  * is also started and stopped via the perf_event_open() system call, use
1690a029a4eaSThomas Richter  * the same event enable/disable call back functions. They do not
1691a029a4eaSThomas Richter  * have a pointer to the perf_event strcture as first parameter.
1692a029a4eaSThomas Richter  *
1693a029a4eaSThomas Richter  * The functions XXX_add, XXX_del, XXX_start and XXX_stop are also common.
1694a029a4eaSThomas Richter  * Reuse them and distinguish the event (always first parameter) via
1695a029a4eaSThomas Richter  * 'config' member.
1696a029a4eaSThomas Richter  */
1697a029a4eaSThomas Richter static struct pmu cf_diag = {
1698a029a4eaSThomas Richter 	.task_ctx_nr  = perf_sw_context,
1699a029a4eaSThomas Richter 	.event_init   = cfdiag_event_init,
1700a029a4eaSThomas Richter 	.pmu_enable   = cpumf_pmu_enable,
1701a029a4eaSThomas Richter 	.pmu_disable  = cpumf_pmu_disable,
1702a029a4eaSThomas Richter 	.add	      = cpumf_pmu_add,
1703a029a4eaSThomas Richter 	.del	      = cpumf_pmu_del,
1704a029a4eaSThomas Richter 	.start	      = cpumf_pmu_start,
1705a029a4eaSThomas Richter 	.stop	      = cpumf_pmu_stop,
1706a029a4eaSThomas Richter 	.read	      = cfdiag_read,
1707a029a4eaSThomas Richter 
1708a029a4eaSThomas Richter 	.attr_groups  = cfdiag_attr_groups
1709a029a4eaSThomas Richter };
1710a029a4eaSThomas Richter 
1711a029a4eaSThomas Richter /* Calculate memory needed to store all counter sets together with header and
1712a029a4eaSThomas Richter  * trailer data. This is independent of the counter set authorization which
1713a029a4eaSThomas Richter  * can vary depending on the configuration.
1714a029a4eaSThomas Richter  */
1715a029a4eaSThomas Richter static size_t cfdiag_maxsize(struct cpumf_ctr_info *info)
1716a029a4eaSThomas Richter {
1717a029a4eaSThomas Richter 	size_t max_size = sizeof(struct cf_trailer_entry);
1718a029a4eaSThomas Richter 	enum cpumf_ctr_set i;
1719a029a4eaSThomas Richter 
1720a029a4eaSThomas Richter 	for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1721a029a4eaSThomas Richter 		size_t size = cpum_cf_ctrset_size(i, info);
1722a029a4eaSThomas Richter 
1723a029a4eaSThomas Richter 		if (size)
1724a029a4eaSThomas Richter 			max_size += size * sizeof(u64) +
1725a029a4eaSThomas Richter 				    sizeof(struct cf_ctrset_entry);
1726a029a4eaSThomas Richter 	}
1727a029a4eaSThomas Richter 	return max_size;
1728a029a4eaSThomas Richter }
1729a029a4eaSThomas Richter 
1730a029a4eaSThomas Richter /* Get the CPU speed, try sampling facility first and CPU attributes second. */
1731a029a4eaSThomas Richter static void cfdiag_get_cpu_speed(void)
1732a029a4eaSThomas Richter {
17334efd417fSVasily Gorbik 	unsigned long mhz;
17344efd417fSVasily Gorbik 
1735a029a4eaSThomas Richter 	if (cpum_sf_avail()) {			/* Sampling facility first */
1736a029a4eaSThomas Richter 		struct hws_qsi_info_block si;
1737a029a4eaSThomas Richter 
1738a029a4eaSThomas Richter 		memset(&si, 0, sizeof(si));
1739a029a4eaSThomas Richter 		if (!qsi(&si)) {
1740a029a4eaSThomas Richter 			cfdiag_cpu_speed = si.cpu_speed;
1741a029a4eaSThomas Richter 			return;
1742a029a4eaSThomas Richter 		}
1743a029a4eaSThomas Richter 	}
1744a029a4eaSThomas Richter 
1745a029a4eaSThomas Richter 	/* Fallback: CPU speed extract static part. Used in case
1746a029a4eaSThomas Richter 	 * CPU Measurement Sampling Facility is turned off.
1747a029a4eaSThomas Richter 	 */
17484efd417fSVasily Gorbik 	mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
1749a029a4eaSThomas Richter 	if (mhz != -1UL)
1750a029a4eaSThomas Richter 		cfdiag_cpu_speed = mhz & 0xffffffff;
1751a029a4eaSThomas Richter }
1752a029a4eaSThomas Richter 
1753a029a4eaSThomas Richter static int cfset_init(void)
1754a029a4eaSThomas Richter {
1755a029a4eaSThomas Richter 	struct cpumf_ctr_info info;
1756a029a4eaSThomas Richter 	size_t need;
1757a029a4eaSThomas Richter 	int rc;
1758a029a4eaSThomas Richter 
1759a029a4eaSThomas Richter 	if (qctri(&info))
1760a029a4eaSThomas Richter 		return -ENODEV;
1761a029a4eaSThomas Richter 
1762a029a4eaSThomas Richter 	cfdiag_get_cpu_speed();
1763a029a4eaSThomas Richter 	/* Make sure the counter set data fits into predefined buffer. */
1764a029a4eaSThomas Richter 	need = cfdiag_maxsize(&info);
1765a029a4eaSThomas Richter 	if (need > sizeof(((struct cpu_cf_events *)0)->start)) {
1766a029a4eaSThomas Richter 		pr_err("Insufficient memory for PMU(cpum_cf_diag) need=%zu\n",
1767a029a4eaSThomas Richter 		       need);
1768a029a4eaSThomas Richter 		return -ENOMEM;
1769a029a4eaSThomas Richter 	}
1770a029a4eaSThomas Richter 
1771a029a4eaSThomas Richter 	rc = misc_register(&cfset_dev);
1772a029a4eaSThomas Richter 	if (rc) {
1773a029a4eaSThomas Richter 		pr_err("Registration of /dev/%s failed rc=%i\n",
1774a029a4eaSThomas Richter 		       cfset_dev.name, rc);
1775a029a4eaSThomas Richter 		goto out;
1776a029a4eaSThomas Richter 	}
1777a029a4eaSThomas Richter 
1778a029a4eaSThomas Richter 	rc = perf_pmu_register(&cf_diag, "cpum_cf_diag", -1);
1779a029a4eaSThomas Richter 	if (rc) {
1780a029a4eaSThomas Richter 		misc_deregister(&cfset_dev);
1781a029a4eaSThomas Richter 		pr_err("Registration of PMU(cpum_cf_diag) failed with rc=%i\n",
1782a029a4eaSThomas Richter 		       rc);
1783a029a4eaSThomas Richter 	}
1784a029a4eaSThomas Richter out:
1785a029a4eaSThomas Richter 	return rc;
1786a029a4eaSThomas Richter }
1787a029a4eaSThomas Richter 
1788a029a4eaSThomas Richter device_initcall(cpumf_pmu_init);
1789