2 * Performance event support for the System z CPU-measurement Sampling Facility
4 * Copyright IBM Corp. 2013
5 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (version 2 only)
9 * as published by the Free Software Foundation.
11 #define KMSG_COMPONENT "cpum_sf"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/perf_event.h>
17 #include <linux/percpu.h>
18 #include <linux/notifier.h>
19 #include <linux/export.h>
20 #include <linux/slab.h>
22 #include <linux/moduleparam.h>
23 #include <asm/cpu_mf.h>
25 #include <asm/debug.h>
26 #include <asm/timex.h>
28 /* Minimum number of sample-data-block-tables:
29 * At least one table is required for the sampling buffer structure.
30 * A single table contains up to 511 pointers to sample-data-blocks.
32 #define CPUM_SF_MIN_SDBT 1
34 /* Number of sample-data-blocks per sample-data-block-table (SDBT):
35 * A table contains SDB pointers (8 bytes) and one table-link entry
36 * that points to the origin of the next SDBT.
38 #define CPUM_SF_SDB_PER_TABLE ((PAGE_SIZE - 8) / 8)
40 /* Maximum page offset for an SDBT table-link entry:
41 * If this page offset is reached, a table-link entry to the next SDBT
44 #define CPUM_SF_SDBT_TL_OFFSET (CPUM_SF_SDB_PER_TABLE * 8)
45 static inline int require_table_link(const void *sdbt)
47 return ((unsigned long) sdbt & ~PAGE_MASK) == CPUM_SF_SDBT_TL_OFFSET;
50 /* Minimum and maximum sampling buffer sizes:
52 * This number represents the maximum size of the sampling buffer taking
53 * the number of sample-data-block-tables into account. Note that these
54 * numbers apply to the basic-sampling function only.
55 * The maximum number of SDBs is increased by CPUM_SF_SDB_DIAG_FACTOR if
56 * the diagnostic-sampling function is active.
58 * Sampling buffer size Buffer characteristics
59 * ---------------------------------------------------
60 * 64KB == 16 pages (4KB per page)
61 * 1 page for SDB-tables
64 * 32MB == 8192 pages (4KB per page)
65 * 16 pages for SDB-tables
68 static unsigned long __read_mostly CPUM_SF_MIN_SDB = 15;
69 static unsigned long __read_mostly CPUM_SF_MAX_SDB = 8176;
70 static unsigned long __read_mostly CPUM_SF_SDB_DIAG_FACTOR = 1;
73 unsigned long *sdbt; /* Sample-data-block-table origin */
74 /* buffer characteristics (required for buffer increments) */
75 unsigned long num_sdb; /* Number of sample-data-blocks */
76 unsigned long num_sdbt; /* Number of sample-data-block-tables */
77 unsigned long *tail; /* last sample-data-block-table */
81 /* CPU-measurement sampling information block */
82 struct hws_qsi_info_block qsi;
83 /* CPU-measurement sampling control block */
84 struct hws_lsctl_request_block lsctl;
85 struct sf_buffer sfb; /* Sampling buffer */
86 unsigned int flags; /* Status flags */
87 struct perf_event *event; /* Scheduled perf event */
89 static DEFINE_PER_CPU(struct cpu_hw_sf, cpu_hw_sf);
92 static debug_info_t *sfdbg;
95 * sf_disable() - Switch off sampling facility
97 static int sf_disable(void)
99 struct hws_lsctl_request_block sreq;
101 memset(&sreq, 0, sizeof(sreq));
106 * sf_buffer_available() - Check for an allocated sampling buffer
108 static int sf_buffer_available(struct cpu_hw_sf *cpuhw)
110 return !!cpuhw->sfb.sdbt;
114 * deallocate sampling facility buffer
116 static void free_sampling_buffer(struct sf_buffer *sfb)
118 unsigned long *sdbt, *curr;
126 /* Free the SDBT after all SDBs are processed... */
131 /* Process table-link entries */
132 if (is_link_entry(curr)) {
133 curr = get_next_sdbt(curr);
135 free_page((unsigned long) sdbt);
137 /* If the origin is reached, sampling buffer is freed */
138 if (curr == sfb->sdbt)
143 /* Process SDB pointer */
151 debug_sprintf_event(sfdbg, 5,
152 "free_sampling_buffer: freed sdbt=%p\n", sfb->sdbt);
153 memset(sfb, 0, sizeof(*sfb));
156 static int alloc_sample_data_block(unsigned long *sdbt, gfp_t gfp_flags)
158 unsigned long sdb, *trailer;
160 /* Allocate and initialize sample-data-block */
161 sdb = get_zeroed_page(gfp_flags);
164 trailer = trailer_entry_ptr(sdb);
165 *trailer = SDB_TE_ALERT_REQ_MASK;
167 /* Link SDB into the sample-data-block-table */
174 * realloc_sampling_buffer() - extend sampler memory
176 * Allocates new sample-data-blocks and adds them to the specified sampling
179 * Important: This modifies the sampling buffer and must be called when the
180 * sampling facility is disabled.
182 * Returns zero on success, non-zero otherwise.
184 static int realloc_sampling_buffer(struct sf_buffer *sfb,
185 unsigned long num_sdb, gfp_t gfp_flags)
188 unsigned long *new, *tail;
190 if (!sfb->sdbt || !sfb->tail)
193 if (!is_link_entry(sfb->tail))
196 /* Append to the existing sampling buffer, overwriting the table-link
198 * The tail variables always points to the "tail" (last and table-link)
199 * entry in an SDB-table.
203 /* Do a sanity check whether the table-link entry points to
204 * the sampling buffer origin.
206 if (sfb->sdbt != get_next_sdbt(tail)) {
207 debug_sprintf_event(sfdbg, 3, "realloc_sampling_buffer: "
208 "sampling buffer is not linked: origin=%p"
210 (void *) sfb->sdbt, (void *) tail);
214 /* Allocate remaining SDBs */
216 for (i = 0; i < num_sdb; i++) {
217 /* Allocate a new SDB-table if it is full. */
218 if (require_table_link(tail)) {
219 new = (unsigned long *) get_zeroed_page(gfp_flags);
225 /* Link current page to tail of chain */
226 *tail = (unsigned long)(void *) new + 1;
230 /* Allocate a new sample-data-block.
231 * If there is not enough memory, stop the realloc process
232 * and simply use what was allocated. If this is a temporary
233 * issue, a new realloc call (if required) might succeed.
235 rc = alloc_sample_data_block(tail, gfp_flags);
242 /* Link sampling buffer to its origin */
243 *tail = (unsigned long) sfb->sdbt + 1;
246 debug_sprintf_event(sfdbg, 4, "realloc_sampling_buffer: new buffer"
247 " settings: sdbt=%lu sdb=%lu\n",
248 sfb->num_sdbt, sfb->num_sdb);
253 * allocate_sampling_buffer() - allocate sampler memory
255 * Allocates and initializes a sampling buffer structure using the
256 * specified number of sample-data-blocks (SDB). For each allocation,
257 * a 4K page is used. The number of sample-data-block-tables (SDBT)
258 * are calculated from SDBs.
259 * Also set the ALERT_REQ mask in each SDBs trailer.
261 * Returns zero on success, non-zero otherwise.
263 static int alloc_sampling_buffer(struct sf_buffer *sfb, unsigned long num_sdb)
270 /* Allocate the sample-data-block-table origin */
271 sfb->sdbt = (unsigned long *) get_zeroed_page(GFP_KERNEL);
277 /* Link the table origin to point to itself to prepare for
278 * realloc_sampling_buffer() invocation.
280 sfb->tail = sfb->sdbt;
281 *sfb->tail = (unsigned long)(void *) sfb->sdbt + 1;
283 /* Allocate requested number of sample-data-blocks */
284 rc = realloc_sampling_buffer(sfb, num_sdb, GFP_KERNEL);
286 free_sampling_buffer(sfb);
287 debug_sprintf_event(sfdbg, 4, "alloc_sampling_buffer: "
288 "realloc_sampling_buffer failed with rc=%i\n", rc);
290 debug_sprintf_event(sfdbg, 4,
291 "alloc_sampling_buffer: tear=%p dear=%p\n",
292 sfb->sdbt, (void *) *sfb->sdbt);
296 static void sfb_set_limits(unsigned long min, unsigned long max)
298 struct hws_qsi_info_block si;
300 CPUM_SF_MIN_SDB = min;
301 CPUM_SF_MAX_SDB = max;
303 memset(&si, 0, sizeof(si));
305 CPUM_SF_SDB_DIAG_FACTOR = DIV_ROUND_UP(si.dsdes, si.bsdes);
308 static unsigned long sfb_max_limit(struct hw_perf_event *hwc)
310 return SAMPL_DIAG_MODE(hwc) ? CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR
314 static unsigned long sfb_pending_allocs(struct sf_buffer *sfb,
315 struct hw_perf_event *hwc)
318 return SFB_ALLOC_REG(hwc);
319 if (SFB_ALLOC_REG(hwc) > sfb->num_sdb)
320 return SFB_ALLOC_REG(hwc) - sfb->num_sdb;
324 static int sfb_has_pending_allocs(struct sf_buffer *sfb,
325 struct hw_perf_event *hwc)
327 return sfb_pending_allocs(sfb, hwc) > 0;
330 static void sfb_account_allocs(unsigned long num, struct hw_perf_event *hwc)
332 /* Limit the number of SDBs to not exceed the maximum */
333 num = min_t(unsigned long, num, sfb_max_limit(hwc) - SFB_ALLOC_REG(hwc));
335 SFB_ALLOC_REG(hwc) += num;
338 static void sfb_init_allocs(unsigned long num, struct hw_perf_event *hwc)
340 SFB_ALLOC_REG(hwc) = 0;
341 sfb_account_allocs(num, hwc);
344 static size_t event_sample_size(struct hw_perf_event *hwc)
346 struct sf_raw_sample *sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(hwc);
349 /* The sample size depends on the sampling function: The basic-sampling
350 * function must be always enabled, diagnostic-sampling function is
353 sample_size = sfr->bsdes;
354 if (SAMPL_DIAG_MODE(hwc))
355 sample_size += sfr->dsdes;
360 static void deallocate_buffers(struct cpu_hw_sf *cpuhw)
363 free_sampling_buffer(&cpuhw->sfb);
366 static int allocate_buffers(struct cpu_hw_sf *cpuhw, struct hw_perf_event *hwc)
368 unsigned long n_sdb, freq, factor;
369 size_t sfr_size, sample_size;
370 struct sf_raw_sample *sfr;
372 /* Allocate raw sample buffer
374 * The raw sample buffer is used to temporarily store sampling data
375 * entries for perf raw sample processing. The buffer size mainly
376 * depends on the size of diagnostic-sampling data entries which is
377 * machine-specific. The exact size calculation includes:
378 * 1. The first 4 bytes of diagnostic-sampling data entries are
379 * already reflected in the sf_raw_sample structure. Subtract
381 * 2. The perf raw sample data must be 8-byte aligned (u64) and
382 * perf's internal data size must be considered too. So add
383 * an additional u32 for correct alignment and subtract before
384 * allocating the buffer.
385 * 3. Store the raw sample buffer pointer in the perf event
386 * hardware structure.
388 sfr_size = ALIGN((sizeof(*sfr) - sizeof(sfr->diag) + cpuhw->qsi.dsdes) +
389 sizeof(u32), sizeof(u64));
390 sfr_size -= sizeof(u32);
391 sfr = kzalloc(sfr_size, GFP_KERNEL);
394 sfr->size = sfr_size;
395 sfr->bsdes = cpuhw->qsi.bsdes;
396 sfr->dsdes = cpuhw->qsi.dsdes;
397 RAWSAMPLE_REG(hwc) = (unsigned long) sfr;
399 /* Calculate sampling buffers using 4K pages
401 * 1. Determine the sample data size which depends on the used
402 * sampling functions, for example, basic-sampling or
403 * basic-sampling with diagnostic-sampling.
405 * 2. Use the sampling frequency as input. The sampling buffer is
406 * designed for almost one second. This can be adjusted through
407 * the "factor" variable.
408 * In any case, alloc_sampling_buffer() sets the Alert Request
409 * Control indicator to trigger a measurement-alert to harvest
410 * sample-data-blocks (sdb).
412 * 3. Compute the number of sample-data-blocks and ensure a minimum
413 * of CPUM_SF_MIN_SDB. Also ensure the upper limit does not
414 * exceed a "calculated" maximum. The symbolic maximum is
415 * designed for basic-sampling only and needs to be increased if
416 * diagnostic-sampling is active.
417 * See also the remarks for these symbolic constants.
419 * 4. Compute the number of sample-data-block-tables (SDBT) and
420 * ensure a minimum of CPUM_SF_MIN_SDBT (one table can manage up
423 sample_size = event_sample_size(hwc);
424 freq = sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc));
426 n_sdb = DIV_ROUND_UP(freq, factor * ((PAGE_SIZE-64) / sample_size));
427 if (n_sdb < CPUM_SF_MIN_SDB)
428 n_sdb = CPUM_SF_MIN_SDB;
430 /* If there is already a sampling buffer allocated, it is very likely
431 * that the sampling facility is enabled too. If the event to be
432 * initialized requires a greater sampling buffer, the allocation must
433 * be postponed. Changing the sampling buffer requires the sampling
434 * facility to be in the disabled state. So, account the number of
435 * required SDBs and let cpumsf_pmu_enable() resize the buffer just
436 * before the event is started.
438 sfb_init_allocs(n_sdb, hwc);
439 if (sf_buffer_available(cpuhw))
442 debug_sprintf_event(sfdbg, 3,
443 "allocate_buffers: rate=%lu f=%lu sdb=%lu/%lu"
444 " sample_size=%lu cpuhw=%p\n",
445 SAMPL_RATE(hwc), freq, n_sdb, sfb_max_limit(hwc),
448 return alloc_sampling_buffer(&cpuhw->sfb,
449 sfb_pending_allocs(&cpuhw->sfb, hwc));
452 static unsigned long min_percent(unsigned int percent, unsigned long base,
455 return min_t(unsigned long, min, DIV_ROUND_UP(percent * base, 100));
458 static unsigned long compute_sfb_extent(unsigned long ratio, unsigned long base)
460 /* Use a percentage-based approach to extend the sampling facility
461 * buffer. Accept up to 5% sample data loss.
462 * Vary the extents between 1% to 5% of the current number of
463 * sample-data-blocks.
468 return min_percent(1, base, 1);
470 return min_percent(1, base, 1);
472 return min_percent(2, base, 2);
474 return min_percent(3, base, 3);
476 return min_percent(4, base, 4);
478 return min_percent(5, base, 8);
481 static void sfb_account_overflows(struct cpu_hw_sf *cpuhw,
482 struct hw_perf_event *hwc)
484 unsigned long ratio, num;
486 if (!OVERFLOW_REG(hwc))
489 /* The sample_overflow contains the average number of sample data
490 * that has been lost because sample-data-blocks were full.
492 * Calculate the total number of sample data entries that has been
493 * discarded. Then calculate the ratio of lost samples to total samples
494 * per second in percent.
496 ratio = DIV_ROUND_UP(100 * OVERFLOW_REG(hwc) * cpuhw->sfb.num_sdb,
497 sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc)));
499 /* Compute number of sample-data-blocks */
500 num = compute_sfb_extent(ratio, cpuhw->sfb.num_sdb);
502 sfb_account_allocs(num, hwc);
504 debug_sprintf_event(sfdbg, 5, "sfb: overflow: overflow=%llu ratio=%lu"
505 " num=%lu\n", OVERFLOW_REG(hwc), ratio, num);
506 OVERFLOW_REG(hwc) = 0;
509 /* extend_sampling_buffer() - Extend sampling buffer
510 * @sfb: Sampling buffer structure (for local CPU)
511 * @hwc: Perf event hardware structure
513 * Use this function to extend the sampling buffer based on the overflow counter
514 * and postponed allocation extents stored in the specified Perf event hardware.
516 * Important: This function disables the sampling facility in order to safely
517 * change the sampling buffer structure. Do not call this function
518 * when the PMU is active.
520 static void extend_sampling_buffer(struct sf_buffer *sfb,
521 struct hw_perf_event *hwc)
523 unsigned long num, num_old;
526 num = sfb_pending_allocs(sfb, hwc);
529 num_old = sfb->num_sdb;
531 /* Disable the sampling facility to reset any states and also
532 * clear pending measurement alerts.
536 /* Extend the sampling buffer.
537 * This memory allocation typically happens in an atomic context when
538 * called by perf. Because this is a reallocation, it is fine if the
539 * new SDB-request cannot be satisfied immediately.
541 rc = realloc_sampling_buffer(sfb, num, GFP_ATOMIC);
543 debug_sprintf_event(sfdbg, 5, "sfb: extend: realloc "
544 "failed with rc=%i\n", rc);
546 if (sfb_has_pending_allocs(sfb, hwc))
547 debug_sprintf_event(sfdbg, 5, "sfb: extend: "
548 "req=%lu alloc=%lu remaining=%lu\n",
549 num, sfb->num_sdb - num_old,
550 sfb_pending_allocs(sfb, hwc));
554 /* Number of perf events counting hardware events */
555 static atomic_t num_events;
556 /* Used to avoid races in calling reserve/release_cpumf_hardware */
557 static DEFINE_MUTEX(pmc_reserve_mutex);
560 #define PMC_RELEASE 1
561 #define PMC_FAILURE 2
562 static void setup_pmc_cpu(void *flags)
565 struct cpu_hw_sf *cpusf = &__get_cpu_var(cpu_hw_sf);
568 switch (*((int *) flags)) {
570 memset(cpusf, 0, sizeof(*cpusf));
571 err = qsi(&cpusf->qsi);
574 cpusf->flags |= PMU_F_RESERVED;
577 pr_err("Switching off the sampling facility failed "
578 "with rc=%i\n", err);
579 debug_sprintf_event(sfdbg, 5,
580 "setup_pmc_cpu: initialized: cpuhw=%p\n", cpusf);
583 cpusf->flags &= ~PMU_F_RESERVED;
586 pr_err("Switching off the sampling facility failed "
587 "with rc=%i\n", err);
589 deallocate_buffers(cpusf);
590 debug_sprintf_event(sfdbg, 5,
591 "setup_pmc_cpu: released: cpuhw=%p\n", cpusf);
595 *((int *) flags) |= PMC_FAILURE;
598 static void release_pmc_hardware(void)
600 int flags = PMC_RELEASE;
602 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
603 on_each_cpu(setup_pmc_cpu, &flags, 1);
604 perf_release_sampling();
607 static int reserve_pmc_hardware(void)
609 int flags = PMC_INIT;
612 err = perf_reserve_sampling();
615 on_each_cpu(setup_pmc_cpu, &flags, 1);
616 if (flags & PMC_FAILURE) {
617 release_pmc_hardware();
620 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
625 static void hw_perf_event_destroy(struct perf_event *event)
627 /* Free raw sample buffer */
628 if (RAWSAMPLE_REG(&event->hw))
629 kfree((void *) RAWSAMPLE_REG(&event->hw));
631 /* Release PMC if this is the last perf event */
632 if (!atomic_add_unless(&num_events, -1, 1)) {
633 mutex_lock(&pmc_reserve_mutex);
634 if (atomic_dec_return(&num_events) == 0)
635 release_pmc_hardware();
636 mutex_unlock(&pmc_reserve_mutex);
640 static void hw_init_period(struct hw_perf_event *hwc, u64 period)
642 hwc->sample_period = period;
643 hwc->last_period = hwc->sample_period;
644 local64_set(&hwc->period_left, hwc->sample_period);
647 static void hw_reset_registers(struct hw_perf_event *hwc,
648 unsigned long *sdbt_origin)
650 struct sf_raw_sample *sfr;
652 /* (Re)set to first sample-data-block-table */
653 TEAR_REG(hwc) = (unsigned long) sdbt_origin;
655 /* (Re)set raw sampling buffer register */
656 sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(hwc);
657 memset(&sfr->basic, 0, sizeof(sfr->basic));
658 memset(&sfr->diag, 0, sfr->dsdes);
661 static unsigned long hw_limit_rate(const struct hws_qsi_info_block *si,
664 return clamp_t(unsigned long, rate,
665 si->min_sampl_rate, si->max_sampl_rate);
668 static int __hw_perf_event_init(struct perf_event *event)
670 struct cpu_hw_sf *cpuhw;
671 struct hws_qsi_info_block si;
672 struct perf_event_attr *attr = &event->attr;
673 struct hw_perf_event *hwc = &event->hw;
677 /* Reserve CPU-measurement sampling facility */
679 if (!atomic_inc_not_zero(&num_events)) {
680 mutex_lock(&pmc_reserve_mutex);
681 if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
684 atomic_inc(&num_events);
685 mutex_unlock(&pmc_reserve_mutex);
687 event->destroy = hw_perf_event_destroy;
692 /* Access per-CPU sampling information (query sampling info) */
694 * The event->cpu value can be -1 to count on every CPU, for example,
695 * when attaching to a task. If this is specified, use the query
696 * sampling info from the current CPU, otherwise use event->cpu to
697 * retrieve the per-CPU information.
698 * Later, cpuhw indicates whether to allocate sampling buffers for a
699 * particular CPU (cpuhw!=NULL) or each online CPU (cpuw==NULL).
701 memset(&si, 0, sizeof(si));
703 if (event->cpu == -1)
706 /* Event is pinned to a particular CPU, retrieve the per-CPU
707 * sampling structure for accessing the CPU-specific QSI.
709 cpuhw = &per_cpu(cpu_hw_sf, event->cpu);
713 /* Check sampling facility authorization and, if not authorized,
714 * fall back to other PMUs. It is safe to check any CPU because
715 * the authorization is identical for all configured CPUs.
722 /* Always enable basic sampling */
723 SAMPL_FLAGS(hwc) = PERF_CPUM_SF_BASIC_MODE;
725 /* Check if diagnostic sampling is requested. Deny if the required
726 * sampling authorization is missing.
728 if (attr->config == PERF_EVENT_CPUM_SF_DIAG) {
733 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_DIAG_MODE;
736 /* Check and set other sampling flags */
737 if (attr->config1 & PERF_CPUM_SF_FULL_BLOCKS)
738 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FULL_BLOCKS;
740 /* The sampling information (si) contains information about the
741 * min/max sampling intervals and the CPU speed. So calculate the
742 * correct sampling interval and avoid the whole period adjust
747 if (!attr->sample_freq) {
751 rate = freq_to_sample_rate(&si, attr->sample_freq);
752 rate = hw_limit_rate(&si, rate);
754 attr->sample_period = rate;
756 /* The min/max sampling rates specifies the valid range
757 * of sample periods. If the specified sample period is
758 * out of range, limit the period to the range boundary.
760 rate = hw_limit_rate(&si, hwc->sample_period);
762 /* The perf core maintains a maximum sample rate that is
763 * configurable through the sysctl interface. Ensure the
764 * sampling rate does not exceed this value. This also helps
765 * to avoid throttling when pushing samples with
766 * perf_event_overflow().
768 if (sample_rate_to_freq(&si, rate) >
769 sysctl_perf_event_sample_rate) {
771 debug_sprintf_event(sfdbg, 1, "Sampling rate exceeds maximum perf sample rate\n");
775 SAMPL_RATE(hwc) = rate;
776 hw_init_period(hwc, SAMPL_RATE(hwc));
778 /* Initialize sample data overflow accounting */
779 hwc->extra_reg.reg = REG_OVERFLOW;
780 OVERFLOW_REG(hwc) = 0;
782 /* Allocate the per-CPU sampling buffer using the CPU information
783 * from the event. If the event is not pinned to a particular
784 * CPU (event->cpu == -1; or cpuhw == NULL), allocate sampling
785 * buffers for each online CPU.
788 /* Event is pinned to a particular CPU */
789 err = allocate_buffers(cpuhw, hwc);
791 /* Event is not pinned, allocate sampling buffer on
794 for_each_online_cpu(cpu) {
795 cpuhw = &per_cpu(cpu_hw_sf, cpu);
796 err = allocate_buffers(cpuhw, hwc);
805 static int cpumsf_pmu_event_init(struct perf_event *event)
809 /* No support for taken branch sampling */
810 if (has_branch_stack(event))
813 switch (event->attr.type) {
815 if ((event->attr.config != PERF_EVENT_CPUM_SF) &&
816 (event->attr.config != PERF_EVENT_CPUM_SF_DIAG))
819 case PERF_TYPE_HARDWARE:
820 /* Support sampling of CPU cycles in addition to the
821 * counter facility. However, the counter facility
822 * is more precise and, hence, restrict this PMU to
823 * sampling events only.
825 if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES)
827 if (!is_sampling_event(event))
834 /* Check online status of the CPU to which the event is pinned */
835 if (event->cpu >= nr_cpumask_bits ||
836 (event->cpu >= 0 && !cpu_online(event->cpu)))
839 /* Force reset of idle/hv excludes regardless of what the
842 if (event->attr.exclude_hv)
843 event->attr.exclude_hv = 0;
844 if (event->attr.exclude_idle)
845 event->attr.exclude_idle = 0;
847 err = __hw_perf_event_init(event);
850 event->destroy(event);
854 static void cpumsf_pmu_enable(struct pmu *pmu)
856 struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
857 struct hw_perf_event *hwc;
860 if (cpuhw->flags & PMU_F_ENABLED)
863 if (cpuhw->flags & PMU_F_ERR_MASK)
866 /* Check whether to extent the sampling buffer.
868 * Two conditions trigger an increase of the sampling buffer for a
870 * 1. Postponed buffer allocations from the event initialization.
871 * 2. Sampling overflows that contribute to pending allocations.
873 * Note that the extend_sampling_buffer() function disables the sampling
874 * facility, but it can be fully re-enabled using sampling controls that
875 * have been saved in cpumsf_pmu_disable().
878 hwc = &cpuhw->event->hw;
879 /* Account number of overflow-designated buffer extents */
880 sfb_account_overflows(cpuhw, hwc);
881 if (sfb_has_pending_allocs(&cpuhw->sfb, hwc))
882 extend_sampling_buffer(&cpuhw->sfb, hwc);
885 /* (Re)enable the PMU and sampling facility */
886 cpuhw->flags |= PMU_F_ENABLED;
889 err = lsctl(&cpuhw->lsctl);
891 cpuhw->flags &= ~PMU_F_ENABLED;
892 pr_err("Loading sampling controls failed: op=%i err=%i\n",
897 debug_sprintf_event(sfdbg, 6, "pmu_enable: es=%i cs=%i ed=%i cd=%i "
898 "tear=%p dear=%p\n", cpuhw->lsctl.es, cpuhw->lsctl.cs,
899 cpuhw->lsctl.ed, cpuhw->lsctl.cd,
900 (void *) cpuhw->lsctl.tear, (void *) cpuhw->lsctl.dear);
903 static void cpumsf_pmu_disable(struct pmu *pmu)
905 struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
906 struct hws_lsctl_request_block inactive;
907 struct hws_qsi_info_block si;
910 if (!(cpuhw->flags & PMU_F_ENABLED))
913 if (cpuhw->flags & PMU_F_ERR_MASK)
916 /* Switch off sampling activation control */
917 inactive = cpuhw->lsctl;
921 err = lsctl(&inactive);
923 pr_err("Loading sampling controls failed: op=%i err=%i\n",
928 /* Save state of TEAR and DEAR register contents */
930 /* TEAR/DEAR values are valid only if the sampling facility is
931 * enabled. Note that cpumsf_pmu_disable() might be called even
932 * for a disabled sampling facility because cpumsf_pmu_enable()
933 * controls the enable/disable state.
936 cpuhw->lsctl.tear = si.tear;
937 cpuhw->lsctl.dear = si.dear;
940 debug_sprintf_event(sfdbg, 3, "cpumsf_pmu_disable: "
941 "qsi() failed with err=%i\n", err);
943 cpuhw->flags &= ~PMU_F_ENABLED;
946 /* perf_exclude_event() - Filter event
947 * @event: The perf event
948 * @regs: pt_regs structure
949 * @sde_regs: Sample-data-entry (sde) regs structure
951 * Filter perf events according to their exclude specification.
953 * Return non-zero if the event shall be excluded.
955 static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
956 struct perf_sf_sde_regs *sde_regs)
958 if (event->attr.exclude_user && user_mode(regs))
960 if (event->attr.exclude_kernel && !user_mode(regs))
962 if (event->attr.exclude_guest && sde_regs->in_guest)
964 if (event->attr.exclude_host && !sde_regs->in_guest)
969 /* perf_push_sample() - Push samples to perf
970 * @event: The perf event
971 * @sample: Hardware sample data
973 * Use the hardware sample data to create perf event sample. The sample
974 * is the pushed to the event subsystem and the function checks for
975 * possible event overflows. If an event overflow occurs, the PMU is
978 * Return non-zero if an event overflow occurred.
980 static int perf_push_sample(struct perf_event *event, struct sf_raw_sample *sfr)
984 struct perf_sf_sde_regs *sde_regs;
985 struct perf_sample_data data;
986 struct perf_raw_record raw;
988 /* Setup perf sample */
989 perf_sample_data_init(&data, 0, event->hw.last_period);
990 raw.size = sfr->size;
994 /* Setup pt_regs to look like an CPU-measurement external interrupt
995 * using the Program Request Alert code. The regs.int_parm_long
996 * field which is unused contains additional sample-data-entry related
999 memset(®s, 0, sizeof(regs));
1000 regs.int_code = 0x1407;
1001 regs.int_parm = CPU_MF_INT_SF_PRA;
1002 sde_regs = (struct perf_sf_sde_regs *) ®s.int_parm_long;
1004 regs.psw.addr = sfr->basic.ia;
1006 regs.psw.mask |= PSW_MASK_DAT;
1008 regs.psw.mask |= PSW_MASK_WAIT;
1010 regs.psw.mask |= PSW_MASK_PSTATE;
1011 switch (sfr->basic.AS) {
1013 regs.psw.mask |= PSW_ASC_PRIMARY;
1016 regs.psw.mask |= PSW_ASC_ACCREG;
1019 regs.psw.mask |= PSW_ASC_SECONDARY;
1022 regs.psw.mask |= PSW_ASC_HOME;
1026 /* The host-program-parameter (hpp) contains the sie control
1027 * block that is set by sie64a() in entry64.S. Check if hpp
1028 * refers to a valid control block and set sde_regs flags
1029 * accordingly. This would allow to use hpp values for other
1031 * For now, simply use a non-zero value as guest indicator.
1034 sde_regs->in_guest = 1;
1037 if (perf_exclude_event(event, ®s, sde_regs))
1039 if (perf_event_overflow(event, &data, ®s)) {
1041 event->pmu->stop(event, 0);
1043 perf_event_update_userpage(event);
1048 static void perf_event_count_update(struct perf_event *event, u64 count)
1050 local64_add(count, &event->count);
1053 static int sample_format_is_valid(struct hws_combined_entry *sample,
1056 if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
1057 /* Only basic-sampling data entries with data-entry-format
1058 * version of 0x0001 can be processed.
1060 if (sample->basic.def != 0x0001)
1062 if (flags & PERF_CPUM_SF_DIAG_MODE)
1063 /* The data-entry-format number of diagnostic-sampling data
1064 * entries can vary. Because diagnostic data is just passed
1065 * through, do only a sanity check on the DEF.
1067 if (sample->diag.def < 0x8001)
1072 static int sample_is_consistent(struct hws_combined_entry *sample,
1073 unsigned long flags)
1075 /* This check applies only to basic-sampling data entries of potentially
1076 * combined-sampling data entries. Invalid entries cannot be processed
1077 * by the PMU and, thus, do not deliver an associated
1078 * diagnostic-sampling data entry.
1080 if (unlikely(!(flags & PERF_CPUM_SF_BASIC_MODE)))
1083 * Samples are skipped, if they are invalid or for which the
1084 * instruction address is not predictable, i.e., the wait-state bit is
1087 if (sample->basic.I || sample->basic.W)
1092 static void reset_sample_slot(struct hws_combined_entry *sample,
1093 unsigned long flags)
1095 if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
1096 sample->basic.def = 0;
1097 if (flags & PERF_CPUM_SF_DIAG_MODE)
1098 sample->diag.def = 0;
1101 static void sfr_store_sample(struct sf_raw_sample *sfr,
1102 struct hws_combined_entry *sample)
1104 if (likely(sfr->format & PERF_CPUM_SF_BASIC_MODE))
1105 sfr->basic = sample->basic;
1106 if (sfr->format & PERF_CPUM_SF_DIAG_MODE)
1107 memcpy(&sfr->diag, &sample->diag, sfr->dsdes);
1110 static void debug_sample_entry(struct hws_combined_entry *sample,
1111 struct hws_trailer_entry *te,
1112 unsigned long flags)
1114 debug_sprintf_event(sfdbg, 4, "hw_collect_samples: Found unknown "
1115 "sampling data entry: te->f=%i basic.def=%04x (%p)"
1116 " diag.def=%04x (%p)\n", te->f,
1117 sample->basic.def, &sample->basic,
1118 (flags & PERF_CPUM_SF_DIAG_MODE)
1119 ? sample->diag.def : 0xFFFF,
1120 (flags & PERF_CPUM_SF_DIAG_MODE)
1121 ? &sample->diag : NULL);
1124 /* hw_collect_samples() - Walk through a sample-data-block and collect samples
1125 * @event: The perf event
1126 * @sdbt: Sample-data-block table
1127 * @overflow: Event overflow counter
1129 * Walks through a sample-data-block and collects sampling data entries that are
1130 * then pushed to the perf event subsystem. Depending on the sampling function,
1131 * there can be either basic-sampling or combined-sampling data entries. A
1132 * combined-sampling data entry consists of a basic- and a diagnostic-sampling
1133 * data entry. The sampling function is determined by the flags in the perf
1134 * event hardware structure. The function always works with a combined-sampling
1135 * data entry but ignores the the diagnostic portion if it is not available.
1137 * Note that the implementation focuses on basic-sampling data entries and, if
1138 * such an entry is not valid, the entire combined-sampling data entry is
1141 * The overflow variables counts the number of samples that has been discarded
1142 * due to a perf event overflow.
1144 static void hw_collect_samples(struct perf_event *event, unsigned long *sdbt,
1145 unsigned long long *overflow)
1147 unsigned long flags = SAMPL_FLAGS(&event->hw);
1148 struct hws_combined_entry *sample;
1149 struct hws_trailer_entry *te;
1150 struct sf_raw_sample *sfr;
1153 /* Prepare and initialize raw sample data */
1154 sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(&event->hw);
1155 sfr->format = flags & PERF_CPUM_SF_MODE_MASK;
1157 sample_size = event_sample_size(&event->hw);
1158 te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
1159 sample = (struct hws_combined_entry *) *sdbt;
1160 while ((unsigned long *) sample < (unsigned long *) te) {
1161 /* Check for an empty sample */
1162 if (!sample->basic.def)
1165 /* Update perf event period */
1166 perf_event_count_update(event, SAMPL_RATE(&event->hw));
1168 /* Check sampling data entry */
1169 if (sample_format_is_valid(sample, flags)) {
1170 /* If an event overflow occurred, the PMU is stopped to
1171 * throttle event delivery. Remaining sample data is
1175 if (sample_is_consistent(sample, flags)) {
1176 /* Deliver sample data to perf */
1177 sfr_store_sample(sfr, sample);
1178 *overflow = perf_push_sample(event, sfr);
1181 /* Count discarded samples */
1184 debug_sample_entry(sample, te, flags);
1185 /* Sample slot is not yet written or other record.
1187 * This condition can occur if the buffer was reused
1188 * from a combined basic- and diagnostic-sampling.
1189 * If only basic-sampling is then active, entries are
1190 * written into the larger diagnostic entries.
1191 * This is typically the case for sample-data-blocks
1192 * that are not full. Stop processing if the first
1193 * invalid format was detected.
1199 /* Reset sample slot and advance to next sample */
1200 reset_sample_slot(sample, flags);
1201 sample += sample_size;
1205 /* hw_perf_event_update() - Process sampling buffer
1206 * @event: The perf event
1207 * @flush_all: Flag to also flush partially filled sample-data-blocks
1209 * Processes the sampling buffer and create perf event samples.
1210 * The sampling buffer position are retrieved and saved in the TEAR_REG
1211 * register of the specified perf event.
1213 * Only full sample-data-blocks are processed. Specify the flash_all flag
1214 * to also walk through partially filled sample-data-blocks. It is ignored
1215 * if PERF_CPUM_SF_FULL_BLOCKS is set. The PERF_CPUM_SF_FULL_BLOCKS flag
1216 * enforces the processing of full sample-data-blocks only (trailer entries
1217 * with the block-full-indicator bit set).
1219 static void hw_perf_event_update(struct perf_event *event, int flush_all)
1221 struct hw_perf_event *hwc = &event->hw;
1222 struct hws_trailer_entry *te;
1223 unsigned long *sdbt;
1224 unsigned long long event_overflow, sampl_overflow, num_sdb, te_flags;
1227 if (flush_all && SDB_FULL_BLOCKS(hwc))
1230 sdbt = (unsigned long *) TEAR_REG(hwc);
1231 done = event_overflow = sampl_overflow = num_sdb = 0;
1233 /* Get the trailer entry of the sample-data-block */
1234 te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
1236 /* Leave loop if no more work to do (block full indicator) */
1243 /* Check the sample overflow count */
1245 /* Account sample overflows and, if a particular limit
1246 * is reached, extend the sampling buffer.
1247 * For details, see sfb_account_overflows().
1249 sampl_overflow += te->overflow;
1251 /* Timestamps are valid for full sample-data-blocks only */
1252 debug_sprintf_event(sfdbg, 6, "hw_perf_event_update: sdbt=%p "
1253 "overflow=%llu timestamp=0x%llx\n",
1255 (te->f) ? trailer_timestamp(te) : 0ULL);
1257 /* Collect all samples from a single sample-data-block and
1258 * flag if an (perf) event overflow happened. If so, the PMU
1259 * is stopped and remaining samples will be discarded.
1261 hw_collect_samples(event, sdbt, &event_overflow);
1264 /* Reset trailer (using compare-double-and-swap) */
1266 te_flags = te->flags & ~SDB_TE_BUFFER_FULL_MASK;
1267 te_flags |= SDB_TE_ALERT_REQ_MASK;
1268 } while (!cmpxchg_double(&te->flags, &te->overflow,
1269 te->flags, te->overflow,
1272 /* Advance to next sample-data-block */
1274 if (is_link_entry(sdbt))
1275 sdbt = get_next_sdbt(sdbt);
1277 /* Update event hardware registers */
1278 TEAR_REG(hwc) = (unsigned long) sdbt;
1280 /* Stop processing sample-data if all samples of the current
1281 * sample-data-block were flushed even if it was not full.
1283 if (flush_all && done)
1286 /* If an event overflow happened, discard samples by
1287 * processing any remaining sample-data-blocks.
1293 /* Account sample overflows in the event hardware structure */
1295 OVERFLOW_REG(hwc) = DIV_ROUND_UP(OVERFLOW_REG(hwc) +
1296 sampl_overflow, 1 + num_sdb);
1297 if (sampl_overflow || event_overflow)
1298 debug_sprintf_event(sfdbg, 4, "hw_perf_event_update: "
1299 "overflow stats: sample=%llu event=%llu\n",
1300 sampl_overflow, event_overflow);
1303 static void cpumsf_pmu_read(struct perf_event *event)
1305 /* Nothing to do ... updates are interrupt-driven */
1308 /* Activate sampling control.
1309 * Next call of pmu_enable() starts sampling.
1311 static void cpumsf_pmu_start(struct perf_event *event, int flags)
1313 struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
1315 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
1318 if (flags & PERF_EF_RELOAD)
1319 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1321 perf_pmu_disable(event->pmu);
1322 event->hw.state = 0;
1323 cpuhw->lsctl.cs = 1;
1324 if (SAMPL_DIAG_MODE(&event->hw))
1325 cpuhw->lsctl.cd = 1;
1326 perf_pmu_enable(event->pmu);
1329 /* Deactivate sampling control.
1330 * Next call of pmu_enable() stops sampling.
1332 static void cpumsf_pmu_stop(struct perf_event *event, int flags)
1334 struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
1336 if (event->hw.state & PERF_HES_STOPPED)
1339 perf_pmu_disable(event->pmu);
1340 cpuhw->lsctl.cs = 0;
1341 cpuhw->lsctl.cd = 0;
1342 event->hw.state |= PERF_HES_STOPPED;
1344 if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
1345 hw_perf_event_update(event, 1);
1346 event->hw.state |= PERF_HES_UPTODATE;
1348 perf_pmu_enable(event->pmu);
1351 static int cpumsf_pmu_add(struct perf_event *event, int flags)
1353 struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
1356 if (cpuhw->flags & PMU_F_IN_USE)
1359 if (!cpuhw->sfb.sdbt)
1363 perf_pmu_disable(event->pmu);
1365 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1367 /* Set up sampling controls. Always program the sampling register
1368 * using the SDB-table start. Reset TEAR_REG event hardware register
1369 * that is used by hw_perf_event_update() to store the sampling buffer
1370 * position after samples have been flushed.
1374 cpuhw->lsctl.tear = (unsigned long) cpuhw->sfb.sdbt;
1375 cpuhw->lsctl.dear = *(unsigned long *) cpuhw->sfb.sdbt;
1376 cpuhw->lsctl.interval = SAMPL_RATE(&event->hw);
1377 hw_reset_registers(&event->hw, cpuhw->sfb.sdbt);
1379 /* Ensure sampling functions are in the disabled state. If disabled,
1380 * switch on sampling enable control. */
1381 if (WARN_ON_ONCE(cpuhw->lsctl.es == 1 || cpuhw->lsctl.ed == 1)) {
1385 cpuhw->lsctl.es = 1;
1386 if (SAMPL_DIAG_MODE(&event->hw))
1387 cpuhw->lsctl.ed = 1;
1389 /* Set in_use flag and store event */
1390 event->hw.idx = 0; /* only one sampling event per CPU supported */
1391 cpuhw->event = event;
1392 cpuhw->flags |= PMU_F_IN_USE;
1394 if (flags & PERF_EF_START)
1395 cpumsf_pmu_start(event, PERF_EF_RELOAD);
1397 perf_event_update_userpage(event);
1398 perf_pmu_enable(event->pmu);
1402 static void cpumsf_pmu_del(struct perf_event *event, int flags)
1404 struct cpu_hw_sf *cpuhw = &__get_cpu_var(cpu_hw_sf);
1406 perf_pmu_disable(event->pmu);
1407 cpumsf_pmu_stop(event, PERF_EF_UPDATE);
1409 cpuhw->lsctl.es = 0;
1410 cpuhw->lsctl.ed = 0;
1411 cpuhw->flags &= ~PMU_F_IN_USE;
1412 cpuhw->event = NULL;
1414 perf_event_update_userpage(event);
1415 perf_pmu_enable(event->pmu);
1418 static int cpumsf_pmu_event_idx(struct perf_event *event)
1420 return event->hw.idx;
1423 CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC, PERF_EVENT_CPUM_SF);
1424 CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC_DIAG, PERF_EVENT_CPUM_SF_DIAG);
1426 static struct attribute *cpumsf_pmu_events_attr[] = {
1427 CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC),
1428 CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC_DIAG),
1432 PMU_FORMAT_ATTR(event, "config:0-63");
1434 static struct attribute *cpumsf_pmu_format_attr[] = {
1435 &format_attr_event.attr,
1439 static struct attribute_group cpumsf_pmu_events_group = {
1441 .attrs = cpumsf_pmu_events_attr,
1443 static struct attribute_group cpumsf_pmu_format_group = {
1445 .attrs = cpumsf_pmu_format_attr,
1447 static const struct attribute_group *cpumsf_pmu_attr_groups[] = {
1448 &cpumsf_pmu_events_group,
1449 &cpumsf_pmu_format_group,
1453 static struct pmu cpumf_sampling = {
1454 .pmu_enable = cpumsf_pmu_enable,
1455 .pmu_disable = cpumsf_pmu_disable,
1457 .event_init = cpumsf_pmu_event_init,
1458 .add = cpumsf_pmu_add,
1459 .del = cpumsf_pmu_del,
1461 .start = cpumsf_pmu_start,
1462 .stop = cpumsf_pmu_stop,
1463 .read = cpumsf_pmu_read,
1465 .event_idx = cpumsf_pmu_event_idx,
1466 .attr_groups = cpumsf_pmu_attr_groups,
1469 static void cpumf_measurement_alert(struct ext_code ext_code,
1470 unsigned int alert, unsigned long unused)
1472 struct cpu_hw_sf *cpuhw;
1474 if (!(alert & CPU_MF_INT_SF_MASK))
1476 inc_irq_stat(IRQEXT_CMS);
1477 cpuhw = &__get_cpu_var(cpu_hw_sf);
1479 /* Measurement alerts are shared and might happen when the PMU
1480 * is not reserved. Ignore these alerts in this case. */
1481 if (!(cpuhw->flags & PMU_F_RESERVED))
1484 /* The processing below must take care of multiple alert events that
1485 * might be indicated concurrently. */
1487 /* Program alert request */
1488 if (alert & CPU_MF_INT_SF_PRA) {
1489 if (cpuhw->flags & PMU_F_IN_USE)
1490 hw_perf_event_update(cpuhw->event, 0);
1492 WARN_ON_ONCE(!(cpuhw->flags & PMU_F_IN_USE));
1495 /* Report measurement alerts only for non-PRA codes */
1496 if (alert != CPU_MF_INT_SF_PRA)
1497 debug_sprintf_event(sfdbg, 6, "measurement alert: 0x%x\n", alert);
1499 /* Sampling authorization change request */
1500 if (alert & CPU_MF_INT_SF_SACA)
1503 /* Loss of sample data due to high-priority machine activities */
1504 if (alert & CPU_MF_INT_SF_LSDA) {
1505 pr_err("Sample data was lost\n");
1506 cpuhw->flags |= PMU_F_ERR_LSDA;
1510 /* Invalid sampling buffer entry */
1511 if (alert & (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE)) {
1512 pr_err("A sampling buffer entry is incorrect (alert=0x%x)\n",
1514 cpuhw->flags |= PMU_F_ERR_IBE;
1519 static int cpumf_pmu_notifier(struct notifier_block *self,
1520 unsigned long action, void *hcpu)
1522 unsigned int cpu = (long) hcpu;
1525 /* Ignore the notification if no events are scheduled on the PMU.
1526 * This might be racy...
1528 if (!atomic_read(&num_events))
1531 switch (action & ~CPU_TASKS_FROZEN) {
1533 case CPU_ONLINE_FROZEN:
1535 smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
1537 case CPU_DOWN_PREPARE:
1538 flags = PMC_RELEASE;
1539 smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
1548 static int param_get_sfb_size(char *buffer, const struct kernel_param *kp)
1550 if (!cpum_sf_avail())
1552 return sprintf(buffer, "%lu,%lu", CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
1555 static int param_set_sfb_size(const char *val, const struct kernel_param *kp)
1558 unsigned long min, max;
1560 if (!cpum_sf_avail())
1562 if (!val || !strlen(val))
1565 /* Valid parameter values: "min,max" or "max" */
1566 min = CPUM_SF_MIN_SDB;
1567 max = CPUM_SF_MAX_SDB;
1568 if (strchr(val, ','))
1569 rc = (sscanf(val, "%lu,%lu", &min, &max) == 2) ? 0 : -EINVAL;
1571 rc = kstrtoul(val, 10, &max);
1573 if (min < 2 || min >= max || max > get_num_physpages())
1578 sfb_set_limits(min, max);
1579 pr_info("The sampling buffer limits have changed to: "
1580 "min=%lu max=%lu (diag=x%lu)\n",
1581 CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB, CPUM_SF_SDB_DIAG_FACTOR);
1585 #define param_check_sfb_size(name, p) __param_check(name, p, void)
1586 static struct kernel_param_ops param_ops_sfb_size = {
1587 .set = param_set_sfb_size,
1588 .get = param_get_sfb_size,
1591 #define RS_INIT_FAILURE_QSI 0x0001
1592 #define RS_INIT_FAILURE_BSDES 0x0002
1593 #define RS_INIT_FAILURE_ALRT 0x0003
1594 #define RS_INIT_FAILURE_PERF 0x0004
1595 static void __init pr_cpumsf_err(unsigned int reason)
1597 pr_err("Sampling facility support for perf is not available: "
1598 "reason=%04x\n", reason);
1601 static int __init init_cpum_sampling_pmu(void)
1603 struct hws_qsi_info_block si;
1606 if (!cpum_sf_avail())
1609 memset(&si, 0, sizeof(si));
1611 pr_cpumsf_err(RS_INIT_FAILURE_QSI);
1615 if (si.bsdes != sizeof(struct hws_basic_entry)) {
1616 pr_cpumsf_err(RS_INIT_FAILURE_BSDES);
1621 sfb_set_limits(CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
1623 sfdbg = debug_register(KMSG_COMPONENT, 2, 1, 80);
1625 pr_err("Registering for s390dbf failed\n");
1626 debug_register_view(sfdbg, &debug_sprintf_view);
1628 err = register_external_irq(EXT_IRQ_MEASURE_ALERT,
1629 cpumf_measurement_alert);
1631 pr_cpumsf_err(RS_INIT_FAILURE_ALRT);
1635 err = perf_pmu_register(&cpumf_sampling, "cpum_sf", PERF_TYPE_RAW);
1637 pr_cpumsf_err(RS_INIT_FAILURE_PERF);
1638 unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
1639 cpumf_measurement_alert);
1642 perf_cpu_notifier(cpumf_pmu_notifier);
1646 arch_initcall(init_cpum_sampling_pmu);
1647 core_param(cpum_sfb_size, CPUM_SF_MAX_SDB, sfb_size, 0640);