OSDN Git Service

Merge tag 'ceph-for-5.1-rc3' of git://github.com/ceph/ceph-client
[uclinux-h8/linux.git] / arch / s390 / kernel / perf_cpum_cf_diag.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Performance event support for s390x - CPU-measurement Counter Sets
4  *
5  *  Copyright IBM Corp. 2019
6  *  Author(s): Hendrik Brueckner <brueckner@linux.ibm.com>
7  *             Thomas Richer <tmricht@linux.ibm.com>
8  */
9 #define KMSG_COMPONENT  "cpum_cf_diag"
10 #define pr_fmt(fmt)     KMSG_COMPONENT ": " fmt
11
12 #include <linux/kernel.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/percpu.h>
15 #include <linux/notifier.h>
16 #include <linux/init.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/processor.h>
20
21 #include <asm/ctl_reg.h>
22 #include <asm/irq.h>
23 #include <asm/cpu_mcf.h>
24 #include <asm/timex.h>
25 #include <asm/debug.h>
26
27 #define CF_DIAG_CTRSET_DEF              0xfeef  /* Counter set header mark */
28
29 static unsigned int cf_diag_cpu_speed;
30 static debug_info_t *cf_diag_dbg;
31
32 struct cf_diag_csd {            /* Counter set data per CPU */
33         size_t used;                    /* Bytes used in data/start */
34         unsigned char start[PAGE_SIZE]; /* Counter set at event start */
35         unsigned char data[PAGE_SIZE];  /* Counter set at event delete */
36 };
37 DEFINE_PER_CPU(struct cf_diag_csd, cf_diag_csd);
38
39 /* Counter sets are stored as data stream in a page sized memory buffer and
40  * exported to user space via raw data attached to the event sample data.
41  * Each counter set starts with an eight byte header consisting of:
42  * - a two byte eye catcher (0xfeef)
43  * - a one byte counter set number
44  * - a two byte counter set size (indicates the number of counters in this set)
45  * - a three byte reserved value (must be zero) to make the header the same
46  *   size as a counter value.
47  * All counter values are eight byte in size.
48  *
49  * All counter sets are followed by a 64 byte trailer.
50  * The trailer consists of a:
51  * - flag field indicating valid fields when corresponding bit set
52  * - the counter facility first and second version number
53  * - the CPU speed if nonzero
54  * - the time stamp the counter sets have been collected
55  * - the time of day (TOD) base value
56  * - the machine type.
57  *
58  * The counter sets are saved when the process is prepared to be executed on a
59  * CPU and saved again when the process is going to be removed from a CPU.
60  * The difference of both counter sets are calculated and stored in the event
61  * sample data area.
62  */
63
64 struct cf_ctrset_entry {        /* CPU-M CF counter set entry (8 byte) */
65         unsigned int def:16;    /* 0-15  Data Entry Format */
66         unsigned int set:16;    /* 16-31 Counter set identifier */
67         unsigned int ctr:16;    /* 32-47 Number of stored counters */
68         unsigned int res1:16;   /* 48-63 Reserved */
69 };
70
71 struct cf_trailer_entry {       /* CPU-M CF_DIAG trailer (64 byte) */
72         /* 0 - 7 */
73         union {
74                 struct {
75                         unsigned int clock_base:1;      /* TOD clock base set */
76                         unsigned int speed:1;           /* CPU speed set */
77                         /* Measurement alerts */
78                         unsigned int mtda:1;    /* Loss of MT ctr. data alert */
79                         unsigned int caca:1;    /* Counter auth. change alert */
80                         unsigned int lcda:1;    /* Loss of counter data alert */
81                 };
82                 unsigned long flags;    /* 0-63    All indicators */
83         };
84         /* 8 - 15 */
85         unsigned int cfvn:16;                   /* 64-79   Ctr First Version */
86         unsigned int csvn:16;                   /* 80-95   Ctr Second Version */
87         unsigned int cpu_speed:32;              /* 96-127  CPU speed */
88         /* 16 - 23 */
89         unsigned long timestamp;                /* 128-191 Timestamp (TOD) */
90         /* 24 - 55 */
91         union {
92                 struct {
93                         unsigned long progusage1;
94                         unsigned long progusage2;
95                         unsigned long progusage3;
96                         unsigned long tod_base;
97                 };
98                 unsigned long progusage[4];
99         };
100         /* 56 - 63 */
101         unsigned int mach_type:16;              /* Machine type */
102         unsigned int res1:16;                   /* Reserved */
103         unsigned int res2:32;                   /* Reserved */
104 };
105
106 /* Create the trailer data at the end of a page. */
107 static void cf_diag_trailer(struct cf_trailer_entry *te)
108 {
109         struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
110         struct cpuid cpuid;
111
112         te->cfvn = cpuhw->info.cfvn;            /* Counter version numbers */
113         te->csvn = cpuhw->info.csvn;
114
115         get_cpu_id(&cpuid);                     /* Machine type */
116         te->mach_type = cpuid.machine;
117         te->cpu_speed = cf_diag_cpu_speed;
118         if (te->cpu_speed)
119                 te->speed = 1;
120         te->clock_base = 1;                     /* Save clock base */
121         memcpy(&te->tod_base, &tod_clock_base[1], 8);
122         store_tod_clock((__u64 *)&te->timestamp);
123 }
124
125 /*
126  * Change the CPUMF state to active.
127  * Enable and activate the CPU-counter sets according
128  * to the per-cpu control state.
129  */
130 static void cf_diag_enable(struct pmu *pmu)
131 {
132         struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
133         int err;
134
135         debug_sprintf_event(cf_diag_dbg, 5,
136                             "%s pmu %p cpu %d flags %#x state %#llx\n",
137                             __func__, pmu, smp_processor_id(), cpuhw->flags,
138                             cpuhw->state);
139         if (cpuhw->flags & PMU_F_ENABLED)
140                 return;
141
142         err = lcctl(cpuhw->state);
143         if (err) {
144                 pr_err("Enabling the performance measuring unit "
145                        "failed with rc=%x\n", err);
146                 return;
147         }
148         cpuhw->flags |= PMU_F_ENABLED;
149 }
150
151 /*
152  * Change the CPUMF state to inactive.
153  * Disable and enable (inactive) the CPU-counter sets according
154  * to the per-cpu control state.
155  */
156 static void cf_diag_disable(struct pmu *pmu)
157 {
158         struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
159         u64 inactive;
160         int err;
161
162         debug_sprintf_event(cf_diag_dbg, 5,
163                             "%s pmu %p cpu %d flags %#x state %#llx\n",
164                             __func__, pmu, smp_processor_id(), cpuhw->flags,
165                             cpuhw->state);
166         if (!(cpuhw->flags & PMU_F_ENABLED))
167                 return;
168
169         inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
170         err = lcctl(inactive);
171         if (err) {
172                 pr_err("Disabling the performance measuring unit "
173                        "failed with rc=%x\n", err);
174                 return;
175         }
176         cpuhw->flags &= ~PMU_F_ENABLED;
177 }
178
179 /* Number of perf events counting hardware events */
180 static atomic_t cf_diag_events = ATOMIC_INIT(0);
181
182 /* Release the PMU if event is the last perf event */
183 static void cf_diag_perf_event_destroy(struct perf_event *event)
184 {
185         debug_sprintf_event(cf_diag_dbg, 5,
186                             "%s event %p cpu %d cf_diag_events %d\n",
187                             __func__, event, event->cpu,
188                             atomic_read(&cf_diag_events));
189         if (atomic_dec_return(&cf_diag_events) == 0)
190                 __kernel_cpumcf_end();
191 }
192
193 /* Setup the event. Test for authorized counter sets and only include counter
194  * sets which are authorized at the time of the setup. Including unauthorized
195  * counter sets result in specification exception (and panic).
196  */
197 static int __hw_perf_event_init(struct perf_event *event)
198 {
199         struct perf_event_attr *attr = &event->attr;
200         struct cpu_cf_events *cpuhw;
201         enum cpumf_ctr_set i;
202         int err = 0;
203
204         debug_sprintf_event(cf_diag_dbg, 5, "%s event %p cpu %d\n", __func__,
205                             event, event->cpu);
206
207         event->hw.config = attr->config;
208         event->hw.config_base = 0;
209
210         /* Add all authorized counter sets to config_base. The
211          * the hardware init function is either called per-cpu or just once
212          * for all CPUS (event->cpu == -1).  This depends on the whether
213          * counting is started for all CPUs or on a per workload base where
214          * the perf event moves from one CPU to another CPU.
215          * Checking the authorization on any CPU is fine as the hardware
216          * applies the same authorization settings to all CPUs.
217          */
218         cpuhw = &get_cpu_var(cpu_cf_events);
219         for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i)
220                 if (cpuhw->info.auth_ctl & cpumf_ctr_ctl[i])
221                         event->hw.config_base |= cpumf_ctr_ctl[i];
222         put_cpu_var(cpu_cf_events);
223
224         /* No authorized counter sets, nothing to count/sample */
225         if (!event->hw.config_base) {
226                 err = -EINVAL;
227                 goto out;
228         }
229
230         /* Set sample_period to indicate sampling */
231         event->hw.sample_period = attr->sample_period;
232         local64_set(&event->hw.period_left, event->hw.sample_period);
233         event->hw.last_period  = event->hw.sample_period;
234 out:
235         debug_sprintf_event(cf_diag_dbg, 5, "%s err %d config_base %#lx\n",
236                             __func__, err, event->hw.config_base);
237         return err;
238 }
239
240 static int cf_diag_event_init(struct perf_event *event)
241 {
242         struct perf_event_attr *attr = &event->attr;
243         int err = -ENOENT;
244
245         debug_sprintf_event(cf_diag_dbg, 5,
246                             "%s event %p cpu %d config %#llx "
247                             "sample_type %#llx cf_diag_events %d\n", __func__,
248                             event, event->cpu, attr->config, attr->sample_type,
249                             atomic_read(&cf_diag_events));
250
251         if (event->attr.config != PERF_EVENT_CPUM_CF_DIAG ||
252             event->attr.type != PERF_TYPE_RAW)
253                 goto out;
254
255         /* Raw events are used to access counters directly,
256          * hence do not permit excludes.
257          * This event is usesless without PERF_SAMPLE_RAW to return counter set
258          * values as raw data.
259          */
260         if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv ||
261             !(attr->sample_type & (PERF_SAMPLE_CPU | PERF_SAMPLE_RAW))) {
262                 err = -EOPNOTSUPP;
263                 goto out;
264         }
265
266         /* Initialize for using the CPU-measurement counter facility */
267         if (atomic_inc_return(&cf_diag_events) == 1) {
268                 if (__kernel_cpumcf_begin()) {
269                         atomic_dec(&cf_diag_events);
270                         err = -EBUSY;
271                         goto out;
272                 }
273         }
274         event->destroy = cf_diag_perf_event_destroy;
275
276         err = __hw_perf_event_init(event);
277         if (unlikely(err))
278                 event->destroy(event);
279 out:
280         debug_sprintf_event(cf_diag_dbg, 5, "%s err %d\n", __func__, err);
281         return err;
282 }
283
284 static void cf_diag_read(struct perf_event *event)
285 {
286         debug_sprintf_event(cf_diag_dbg, 5, "%s event %p\n", __func__, event);
287 }
288
289 /* Return the maximum possible counter set size (in number of 8 byte counters)
290  * depending on type and model number.
291  */
292 static size_t cf_diag_ctrset_size(enum cpumf_ctr_set ctrset,
293                                  struct cpumf_ctr_info *info)
294 {
295         size_t ctrset_size = 0;
296
297         switch (ctrset) {
298         case CPUMF_CTR_SET_BASIC:
299                 if (info->cfvn >= 1)
300                         ctrset_size = 6;
301                 break;
302         case CPUMF_CTR_SET_USER:
303                 if (info->cfvn == 1)
304                         ctrset_size = 6;
305                 else if (info->cfvn >= 3)
306                         ctrset_size = 2;
307                 break;
308         case CPUMF_CTR_SET_CRYPTO:
309                 ctrset_size = 16;
310                 break;
311         case CPUMF_CTR_SET_EXT:
312                 if (info->csvn == 1)
313                         ctrset_size = 32;
314                 else if (info->csvn == 2)
315                         ctrset_size = 48;
316                 else if (info->csvn >= 3)
317                         ctrset_size = 128;
318                 break;
319         case CPUMF_CTR_SET_MT_DIAG:
320                 if (info->csvn > 3)
321                         ctrset_size = 48;
322                 break;
323         case CPUMF_CTR_SET_MAX:
324                 break;
325         }
326
327         return ctrset_size;
328 }
329
330 /* Calculate memory needed to store all counter sets together with header and
331  * trailer data. This is independend of the counter set authorization which
332  * can vary depending on the configuration.
333  */
334 static size_t cf_diag_ctrset_maxsize(struct cpumf_ctr_info *info)
335 {
336         size_t max_size = sizeof(struct cf_trailer_entry);
337         enum cpumf_ctr_set i;
338
339         for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
340                 size_t size = cf_diag_ctrset_size(i, info);
341
342                 if (size)
343                         max_size += size * sizeof(u64) +
344                                     sizeof(struct cf_ctrset_entry);
345         }
346         debug_sprintf_event(cf_diag_dbg, 5, "%s max_size %zu\n", __func__,
347                             max_size);
348
349         return max_size;
350 }
351
352 /* Read a counter set. The counter set number determines which counter set and
353  * the CPUM-CF first and second version number determine the number of
354  * available counters in this counter set.
355  * Each counter set starts with header containing the counter set number and
356  * the number of 8 byte counters.
357  *
358  * The functions returns the number of bytes occupied by this counter set
359  * including the header.
360  * If there is no counter in the counter set, this counter set is useless and
361  * zero is returned on this case.
362  */
363 static size_t cf_diag_getctrset(struct cf_ctrset_entry *ctrdata, int ctrset,
364                                 size_t room)
365 {
366         struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
367         size_t ctrset_size, need = 0;
368         int rc = 3;                             /* Assume write failure */
369
370         ctrdata->def = CF_DIAG_CTRSET_DEF;
371         ctrdata->set = ctrset;
372         ctrdata->res1 = 0;
373         ctrset_size = cf_diag_ctrset_size(ctrset, &cpuhw->info);
374
375         if (ctrset_size) {                      /* Save data */
376                 need = ctrset_size * sizeof(u64) + sizeof(*ctrdata);
377                 if (need <= room)
378                         rc = ctr_stcctm(ctrset, ctrset_size,
379                                         (u64 *)(ctrdata + 1));
380                 if (rc != 3)
381                         ctrdata->ctr = ctrset_size;
382                 else
383                         need = 0;
384         }
385
386         debug_sprintf_event(cf_diag_dbg, 6,
387                             "%s ctrset %d ctrset_size %zu cfvn %d csvn %d"
388                             " need %zd rc:%d\n",
389                             __func__, ctrset, ctrset_size, cpuhw->info.cfvn,
390                             cpuhw->info.csvn, need, rc);
391         return need;
392 }
393
394 /* Read out all counter sets and save them in the provided data buffer.
395  * The last 64 byte host an artificial trailer entry.
396  */
397 static size_t cf_diag_getctr(void *data, size_t sz, unsigned long auth)
398 {
399         struct cf_trailer_entry *trailer;
400         size_t offset = 0, done;
401         int i;
402
403         memset(data, 0, sz);
404         sz -= sizeof(*trailer);                 /* Always room for trailer */
405         for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
406                 struct cf_ctrset_entry *ctrdata = data + offset;
407
408                 if (!(auth & cpumf_ctr_ctl[i]))
409                         continue;       /* Counter set not authorized */
410
411                 done = cf_diag_getctrset(ctrdata, i, sz - offset);
412                 offset += done;
413                 debug_sprintf_event(cf_diag_dbg, 6,
414                                     "%s ctrset %d offset %zu done %zu\n",
415                                      __func__, i, offset, done);
416         }
417         trailer = data + offset;
418         cf_diag_trailer(trailer);
419         return offset + sizeof(*trailer);
420 }
421
422 /* Calculate the difference for each counter in a counter set. */
423 static void cf_diag_diffctrset(u64 *pstart, u64 *pstop, int counters)
424 {
425         for (; --counters >= 0; ++pstart, ++pstop)
426                 if (*pstop >= *pstart)
427                         *pstop -= *pstart;
428                 else
429                         *pstop = *pstart - *pstop;
430 }
431
432 /* Scan the counter sets and calculate the difference of each counter
433  * in each set. The result is the increment of each counter during the
434  * period the counter set has been activated.
435  *
436  * Return true on success.
437  */
438 static int cf_diag_diffctr(struct cf_diag_csd *csd, unsigned long auth)
439 {
440         struct cf_trailer_entry *trailer_start, *trailer_stop;
441         struct cf_ctrset_entry *ctrstart, *ctrstop;
442         size_t offset = 0;
443
444         auth &= (1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1;
445         do {
446                 ctrstart = (struct cf_ctrset_entry *)(csd->start + offset);
447                 ctrstop = (struct cf_ctrset_entry *)(csd->data + offset);
448
449                 if (memcmp(ctrstop, ctrstart, sizeof(*ctrstop))) {
450                         pr_err("cpum_cf_diag counter set compare error "
451                                 "in set %i\n", ctrstart->set);
452                         return 0;
453                 }
454                 auth &= ~cpumf_ctr_ctl[ctrstart->set];
455                 if (ctrstart->def == CF_DIAG_CTRSET_DEF) {
456                         cf_diag_diffctrset((u64 *)(ctrstart + 1),
457                                           (u64 *)(ctrstop + 1), ctrstart->ctr);
458                         offset += ctrstart->ctr * sizeof(u64) +
459                                   sizeof(*ctrstart);
460                 }
461                 debug_sprintf_event(cf_diag_dbg, 6,
462                                     "%s set %d ctr %d offset %zu auth %lx\n",
463                                     __func__, ctrstart->set, ctrstart->ctr,
464                                     offset, auth);
465         } while (ctrstart->def && auth);
466
467         /* Save time_stamp from start of event in stop's trailer */
468         trailer_start = (struct cf_trailer_entry *)(csd->start + offset);
469         trailer_stop = (struct cf_trailer_entry *)(csd->data + offset);
470         trailer_stop->progusage[0] = trailer_start->timestamp;
471
472         return 1;
473 }
474
475 /* Create perf event sample with the counter sets as raw data.  The sample
476  * is then pushed to the event subsystem and the function checks for
477  * possible event overflows. If an event overflow occurs, the PMU is
478  * stopped.
479  *
480  * Return non-zero if an event overflow occurred.
481  */
482 static int cf_diag_push_sample(struct perf_event *event,
483                                struct cf_diag_csd *csd)
484 {
485         struct perf_sample_data data;
486         struct perf_raw_record raw;
487         struct pt_regs regs;
488         int overflow;
489
490         /* Setup perf sample */
491         perf_sample_data_init(&data, 0, event->hw.last_period);
492         memset(&regs, 0, sizeof(regs));
493         memset(&raw, 0, sizeof(raw));
494
495         if (event->attr.sample_type & PERF_SAMPLE_CPU)
496                 data.cpu_entry.cpu = event->cpu;
497         if (event->attr.sample_type & PERF_SAMPLE_RAW) {
498                 raw.frag.size = csd->used;
499                 raw.frag.data = csd->data;
500                 raw.size = csd->used;
501                 data.raw = &raw;
502         }
503
504         overflow = perf_event_overflow(event, &data, &regs);
505         debug_sprintf_event(cf_diag_dbg, 6,
506                             "%s event %p cpu %d sample_type %#llx raw %d "
507                             "ov %d\n", __func__, event, event->cpu,
508                             event->attr.sample_type, raw.size, overflow);
509         if (overflow)
510                 event->pmu->stop(event, 0);
511
512         perf_event_update_userpage(event);
513         return overflow;
514 }
515
516 static void cf_diag_start(struct perf_event *event, int flags)
517 {
518         struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
519         struct cf_diag_csd *csd = this_cpu_ptr(&cf_diag_csd);
520         struct hw_perf_event *hwc = &event->hw;
521
522         debug_sprintf_event(cf_diag_dbg, 5,
523                             "%s event %p cpu %d flags %#x hwc-state %#x\n",
524                             __func__, event, event->cpu, flags, hwc->state);
525         if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
526                 return;
527
528         /* (Re-)enable and activate all counter sets */
529         lcctl(0);               /* Reset counter sets */
530         hwc->state = 0;
531         ctr_set_multiple_enable(&cpuhw->state, hwc->config_base);
532         lcctl(cpuhw->state);    /* Enable counter sets */
533         csd->used = cf_diag_getctr(csd->start, sizeof(csd->start),
534                                    event->hw.config_base);
535         ctr_set_multiple_start(&cpuhw->state, hwc->config_base);
536         /* Function cf_diag_enable() starts the counter sets. */
537 }
538
539 static void cf_diag_stop(struct perf_event *event, int flags)
540 {
541         struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
542         struct cf_diag_csd *csd = this_cpu_ptr(&cf_diag_csd);
543         struct hw_perf_event *hwc = &event->hw;
544
545         debug_sprintf_event(cf_diag_dbg, 5,
546                             "%s event %p cpu %d flags %#x hwc-state %#x\n",
547                             __func__, event, event->cpu, flags, hwc->state);
548
549         /* Deactivate all counter sets */
550         ctr_set_multiple_stop(&cpuhw->state, hwc->config_base);
551         local64_inc(&event->count);
552         csd->used = cf_diag_getctr(csd->data, sizeof(csd->data),
553                                    event->hw.config_base);
554         if (cf_diag_diffctr(csd, event->hw.config_base))
555                 cf_diag_push_sample(event, csd);
556         hwc->state |= PERF_HES_STOPPED;
557 }
558
559 static int cf_diag_add(struct perf_event *event, int flags)
560 {
561         struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
562         int err = 0;
563
564         debug_sprintf_event(cf_diag_dbg, 5,
565                             "%s event %p cpu %d flags %#x cpuhw:%p\n",
566                             __func__, event, event->cpu, flags, cpuhw);
567
568         if (cpuhw->flags & PMU_F_IN_USE) {
569                 err = -EAGAIN;
570                 goto out;
571         }
572
573         event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
574
575         cpuhw->flags |= PMU_F_IN_USE;
576         if (flags & PERF_EF_START)
577                 cf_diag_start(event, PERF_EF_RELOAD);
578 out:
579         debug_sprintf_event(cf_diag_dbg, 5, "%s err %d\n", __func__, err);
580         return err;
581 }
582
583 static void cf_diag_del(struct perf_event *event, int flags)
584 {
585         struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
586
587         debug_sprintf_event(cf_diag_dbg, 5,
588                             "%s event %p cpu %d flags %#x\n",
589                            __func__, event, event->cpu, flags);
590
591         cf_diag_stop(event, PERF_EF_UPDATE);
592         ctr_set_multiple_stop(&cpuhw->state, event->hw.config_base);
593         ctr_set_multiple_disable(&cpuhw->state, event->hw.config_base);
594         cpuhw->flags &= ~PMU_F_IN_USE;
595 }
596
597 CPUMF_EVENT_ATTR(CF_DIAG, CF_DIAG, PERF_EVENT_CPUM_CF_DIAG);
598
599 static struct attribute *cf_diag_events_attr[] = {
600         CPUMF_EVENT_PTR(CF_DIAG, CF_DIAG),
601         NULL,
602 };
603
604 PMU_FORMAT_ATTR(event, "config:0-63");
605
606 static struct attribute *cf_diag_format_attr[] = {
607         &format_attr_event.attr,
608         NULL,
609 };
610
611 static struct attribute_group cf_diag_events_group = {
612         .name = "events",
613         .attrs = cf_diag_events_attr,
614 };
615 static struct attribute_group cf_diag_format_group = {
616         .name = "format",
617         .attrs = cf_diag_format_attr,
618 };
619 static const struct attribute_group *cf_diag_attr_groups[] = {
620         &cf_diag_events_group,
621         &cf_diag_format_group,
622         NULL,
623 };
624
625 /* Performance monitoring unit for s390x */
626 static struct pmu cf_diag = {
627         .task_ctx_nr  = perf_sw_context,
628         .pmu_enable   = cf_diag_enable,
629         .pmu_disable  = cf_diag_disable,
630         .event_init   = cf_diag_event_init,
631         .add          = cf_diag_add,
632         .del          = cf_diag_del,
633         .start        = cf_diag_start,
634         .stop         = cf_diag_stop,
635         .read         = cf_diag_read,
636
637         .attr_groups  = cf_diag_attr_groups
638 };
639
640 /* Get the CPU speed, try sampling facility first and CPU attributes second. */
641 static void cf_diag_get_cpu_speed(void)
642 {
643         if (cpum_sf_avail()) {                  /* Sampling facility first */
644                 struct hws_qsi_info_block si;
645
646                 memset(&si, 0, sizeof(si));
647                 if (!qsi(&si)) {
648                         cf_diag_cpu_speed = si.cpu_speed;
649                         return;
650                 }
651         }
652
653         if (test_facility(34)) {                /* CPU speed extract static part */
654                 unsigned long mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
655
656                 if (mhz != -1UL)
657                         cf_diag_cpu_speed = mhz & 0xffffffff;
658         }
659 }
660
661 /* Initialize the counter set PMU to generate complete counter set data as
662  * event raw data. This relies on the CPU Measurement Counter Facility device
663  * already being loaded and initialized.
664  */
665 static int __init cf_diag_init(void)
666 {
667         struct cpumf_ctr_info info;
668         size_t need;
669         int rc;
670
671         if (!kernel_cpumcf_avail() || !stccm_avail() || qctri(&info))
672                 return -ENODEV;
673         cf_diag_get_cpu_speed();
674
675         /* Make sure the counter set data fits into predefined buffer. */
676         need = cf_diag_ctrset_maxsize(&info);
677         if (need > sizeof(((struct cf_diag_csd *)0)->start)) {
678                 pr_err("Insufficient memory for PMU(cpum_cf_diag) need=%zu\n",
679                        need);
680                 return -ENOMEM;
681         }
682
683         /* Setup s390dbf facility */
684         cf_diag_dbg = debug_register(KMSG_COMPONENT, 2, 1, 128);
685         if (!cf_diag_dbg) {
686                 pr_err("Registration of s390dbf(cpum_cf_diag) failed\n");
687                 return -ENOMEM;
688         }
689         debug_register_view(cf_diag_dbg, &debug_sprintf_view);
690
691         rc = perf_pmu_register(&cf_diag, "cpum_cf_diag", PERF_TYPE_RAW);
692         if (rc) {
693                 debug_unregister_view(cf_diag_dbg, &debug_sprintf_view);
694                 debug_unregister(cf_diag_dbg);
695                 pr_err("Registration of PMU(cpum_cf_diag) failed with rc=%i\n",
696                        rc);
697         }
698         return rc;
699 }
700 arch_initcall(cf_diag_init);