This source file includes following definitions.
- require_table_link
- sf_disable
- sf_buffer_available
- free_sampling_buffer
- alloc_sample_data_block
- realloc_sampling_buffer
- alloc_sampling_buffer
- sfb_set_limits
- sfb_max_limit
- sfb_pending_allocs
- sfb_has_pending_allocs
- sfb_account_allocs
- sfb_init_allocs
- deallocate_buffers
- allocate_buffers
- min_percent
- compute_sfb_extent
- sfb_account_overflows
- extend_sampling_buffer
- setup_pmc_cpu
- release_pmc_hardware
- reserve_pmc_hardware
- hw_perf_event_destroy
- hw_init_period
- hw_reset_registers
- hw_limit_rate
- cpumsf_pid_type
- cpumsf_output_event_pid
- getrate
- __hw_perf_event_init_rate
- __hw_perf_event_init
- cpumsf_pmu_event_init
- cpumsf_pmu_enable
- cpumsf_pmu_disable
- perf_exclude_event
- perf_push_sample
- perf_event_count_update
- debug_sample_entry
- hw_collect_samples
- hw_perf_event_update
- aux_sdb_trailer
- aux_output_end
- aux_output_begin
- aux_set_alert
- aux_reset_buffer
- hw_collect_aux
- aux_buffer_free
- aux_sdb_init
- aux_buffer_setup
- cpumsf_pmu_read
- cpumsf_pmu_check_period
- cpumsf_pmu_start
- cpumsf_pmu_stop
- cpumsf_pmu_add
- cpumsf_pmu_del
- cpumf_measurement_alert
- cpusf_pmu_setup
- s390_pmu_sf_online_cpu
- s390_pmu_sf_offline_cpu
- param_get_sfb_size
- param_set_sfb_size
- pr_cpumsf_err
- init_cpum_sampling_pmu
1
2
3
4
5
6
7
8 #define KMSG_COMPONENT "cpum_sf"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
11 #include <linux/kernel.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/perf_event.h>
14 #include <linux/percpu.h>
15 #include <linux/pid.h>
16 #include <linux/notifier.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/mm.h>
20 #include <linux/moduleparam.h>
21 #include <asm/cpu_mf.h>
22 #include <asm/irq.h>
23 #include <asm/debug.h>
24 #include <asm/timex.h>
25
26
27
28
29
30 #define CPUM_SF_MIN_SDBT 1
31
32
33
34
35
36 #define CPUM_SF_SDB_PER_TABLE ((PAGE_SIZE - 8) / 8)
37
38
39
40
41
42 #define CPUM_SF_SDBT_TL_OFFSET (CPUM_SF_SDB_PER_TABLE * 8)
43 static inline int require_table_link(const void *sdbt)
44 {
45 return ((unsigned long) sdbt & ~PAGE_MASK) == CPUM_SF_SDBT_TL_OFFSET;
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 static unsigned long __read_mostly CPUM_SF_MIN_SDB = 15;
67 static unsigned long __read_mostly CPUM_SF_MAX_SDB = 8176;
68 static unsigned long __read_mostly CPUM_SF_SDB_DIAG_FACTOR = 1;
69
70 struct sf_buffer {
71 unsigned long *sdbt;
72
73 unsigned long num_sdb;
74 unsigned long num_sdbt;
75 unsigned long *tail;
76 };
77
78 struct aux_buffer {
79 struct sf_buffer sfb;
80 unsigned long head;
81 unsigned long alert_mark;
82 unsigned long empty_mark;
83 unsigned long *sdb_index;
84 unsigned long *sdbt_index;
85 };
86
87 struct cpu_hw_sf {
88
89 struct hws_qsi_info_block qsi;
90
91 struct hws_lsctl_request_block lsctl;
92 struct sf_buffer sfb;
93 unsigned int flags;
94 struct perf_event *event;
95 struct perf_output_handle handle;
96 };
97 static DEFINE_PER_CPU(struct cpu_hw_sf, cpu_hw_sf);
98
99
100 static debug_info_t *sfdbg;
101
102
103
104
105 static int sf_disable(void)
106 {
107 struct hws_lsctl_request_block sreq;
108
109 memset(&sreq, 0, sizeof(sreq));
110 return lsctl(&sreq);
111 }
112
113
114
115
116 static int sf_buffer_available(struct cpu_hw_sf *cpuhw)
117 {
118 return !!cpuhw->sfb.sdbt;
119 }
120
121
122
123
124 static void free_sampling_buffer(struct sf_buffer *sfb)
125 {
126 unsigned long *sdbt, *curr;
127
128 if (!sfb->sdbt)
129 return;
130
131 sdbt = sfb->sdbt;
132 curr = sdbt;
133
134
135 while (1) {
136 if (!*curr || !sdbt)
137 break;
138
139
140 if (is_link_entry(curr)) {
141 curr = get_next_sdbt(curr);
142 if (sdbt)
143 free_page((unsigned long) sdbt);
144
145
146 if (curr == sfb->sdbt)
147 break;
148 else
149 sdbt = curr;
150 } else {
151
152 if (*curr) {
153 free_page(*curr);
154 curr++;
155 }
156 }
157 }
158
159 debug_sprintf_event(sfdbg, 5,
160 "free_sampling_buffer: freed sdbt=%p\n", sfb->sdbt);
161 memset(sfb, 0, sizeof(*sfb));
162 }
163
164 static int alloc_sample_data_block(unsigned long *sdbt, gfp_t gfp_flags)
165 {
166 unsigned long sdb, *trailer;
167
168
169 sdb = get_zeroed_page(gfp_flags);
170 if (!sdb)
171 return -ENOMEM;
172 trailer = trailer_entry_ptr(sdb);
173 *trailer = SDB_TE_ALERT_REQ_MASK;
174
175
176 *sdbt = sdb;
177
178 return 0;
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192 static int realloc_sampling_buffer(struct sf_buffer *sfb,
193 unsigned long num_sdb, gfp_t gfp_flags)
194 {
195 int i, rc;
196 unsigned long *new, *tail, *tail_prev = NULL;
197
198 if (!sfb->sdbt || !sfb->tail)
199 return -EINVAL;
200
201 if (!is_link_entry(sfb->tail))
202 return -EINVAL;
203
204
205
206
207
208
209 tail = sfb->tail;
210
211
212
213
214 if (sfb->sdbt != get_next_sdbt(tail)) {
215 debug_sprintf_event(sfdbg, 3, "realloc_sampling_buffer: "
216 "sampling buffer is not linked: origin=%p"
217 "tail=%p\n",
218 (void *) sfb->sdbt, (void *) tail);
219 return -EINVAL;
220 }
221
222
223 rc = 0;
224 for (i = 0; i < num_sdb; i++) {
225
226 if (require_table_link(tail)) {
227 new = (unsigned long *) get_zeroed_page(gfp_flags);
228 if (!new) {
229 rc = -ENOMEM;
230 break;
231 }
232 sfb->num_sdbt++;
233
234 *tail = (unsigned long)(void *) new + 1;
235 tail_prev = tail;
236 tail = new;
237 }
238
239
240
241
242
243
244 rc = alloc_sample_data_block(tail, gfp_flags);
245 if (rc) {
246
247
248
249
250
251 if (tail_prev) {
252 sfb->num_sdbt--;
253 free_page((unsigned long) new);
254 tail = tail_prev;
255 }
256 break;
257 }
258 sfb->num_sdb++;
259 tail++;
260 tail_prev = new = NULL;
261 }
262
263
264 *tail = (unsigned long) sfb->sdbt + 1;
265 sfb->tail = tail;
266
267 debug_sprintf_event(sfdbg, 4, "realloc_sampling_buffer: new buffer"
268 " settings: sdbt=%lu sdb=%lu\n",
269 sfb->num_sdbt, sfb->num_sdb);
270 return rc;
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284 static int alloc_sampling_buffer(struct sf_buffer *sfb, unsigned long num_sdb)
285 {
286 int rc;
287
288 if (sfb->sdbt)
289 return -EINVAL;
290
291
292 sfb->sdbt = (unsigned long *) get_zeroed_page(GFP_KERNEL);
293 if (!sfb->sdbt)
294 return -ENOMEM;
295 sfb->num_sdb = 0;
296 sfb->num_sdbt = 1;
297
298
299
300
301 sfb->tail = sfb->sdbt;
302 *sfb->tail = (unsigned long)(void *) sfb->sdbt + 1;
303
304
305 rc = realloc_sampling_buffer(sfb, num_sdb, GFP_KERNEL);
306 if (rc) {
307 free_sampling_buffer(sfb);
308 debug_sprintf_event(sfdbg, 4, "alloc_sampling_buffer: "
309 "realloc_sampling_buffer failed with rc=%i\n", rc);
310 } else
311 debug_sprintf_event(sfdbg, 4,
312 "alloc_sampling_buffer: tear=%p dear=%p\n",
313 sfb->sdbt, (void *) *sfb->sdbt);
314 return rc;
315 }
316
317 static void sfb_set_limits(unsigned long min, unsigned long max)
318 {
319 struct hws_qsi_info_block si;
320
321 CPUM_SF_MIN_SDB = min;
322 CPUM_SF_MAX_SDB = max;
323
324 memset(&si, 0, sizeof(si));
325 if (!qsi(&si))
326 CPUM_SF_SDB_DIAG_FACTOR = DIV_ROUND_UP(si.dsdes, si.bsdes);
327 }
328
329 static unsigned long sfb_max_limit(struct hw_perf_event *hwc)
330 {
331 return SAMPL_DIAG_MODE(hwc) ? CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR
332 : CPUM_SF_MAX_SDB;
333 }
334
335 static unsigned long sfb_pending_allocs(struct sf_buffer *sfb,
336 struct hw_perf_event *hwc)
337 {
338 if (!sfb->sdbt)
339 return SFB_ALLOC_REG(hwc);
340 if (SFB_ALLOC_REG(hwc) > sfb->num_sdb)
341 return SFB_ALLOC_REG(hwc) - sfb->num_sdb;
342 return 0;
343 }
344
345 static int sfb_has_pending_allocs(struct sf_buffer *sfb,
346 struct hw_perf_event *hwc)
347 {
348 return sfb_pending_allocs(sfb, hwc) > 0;
349 }
350
351 static void sfb_account_allocs(unsigned long num, struct hw_perf_event *hwc)
352 {
353
354 num = min_t(unsigned long, num, sfb_max_limit(hwc) - SFB_ALLOC_REG(hwc));
355 if (num)
356 SFB_ALLOC_REG(hwc) += num;
357 }
358
359 static void sfb_init_allocs(unsigned long num, struct hw_perf_event *hwc)
360 {
361 SFB_ALLOC_REG(hwc) = 0;
362 sfb_account_allocs(num, hwc);
363 }
364
365 static void deallocate_buffers(struct cpu_hw_sf *cpuhw)
366 {
367 if (cpuhw->sfb.sdbt)
368 free_sampling_buffer(&cpuhw->sfb);
369 }
370
371 static int allocate_buffers(struct cpu_hw_sf *cpuhw, struct hw_perf_event *hwc)
372 {
373 unsigned long n_sdb, freq, factor;
374 size_t sample_size;
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400 sample_size = sizeof(struct hws_basic_entry);
401 freq = sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc));
402 factor = 1;
403 n_sdb = DIV_ROUND_UP(freq, factor * ((PAGE_SIZE-64) / sample_size));
404 if (n_sdb < CPUM_SF_MIN_SDB)
405 n_sdb = CPUM_SF_MIN_SDB;
406
407
408
409
410
411
412
413
414
415 sfb_init_allocs(n_sdb, hwc);
416 if (sf_buffer_available(cpuhw))
417 return 0;
418
419 debug_sprintf_event(sfdbg, 3,
420 "allocate_buffers: rate=%lu f=%lu sdb=%lu/%lu"
421 " sample_size=%lu cpuhw=%p\n",
422 SAMPL_RATE(hwc), freq, n_sdb, sfb_max_limit(hwc),
423 sample_size, cpuhw);
424
425 return alloc_sampling_buffer(&cpuhw->sfb,
426 sfb_pending_allocs(&cpuhw->sfb, hwc));
427 }
428
429 static unsigned long min_percent(unsigned int percent, unsigned long base,
430 unsigned long min)
431 {
432 return min_t(unsigned long, min, DIV_ROUND_UP(percent * base, 100));
433 }
434
435 static unsigned long compute_sfb_extent(unsigned long ratio, unsigned long base)
436 {
437
438
439
440
441
442 if (ratio <= 5)
443 return 0;
444 if (ratio <= 25)
445 return min_percent(1, base, 1);
446 if (ratio <= 50)
447 return min_percent(1, base, 1);
448 if (ratio <= 75)
449 return min_percent(2, base, 2);
450 if (ratio <= 100)
451 return min_percent(3, base, 3);
452 if (ratio <= 250)
453 return min_percent(4, base, 4);
454
455 return min_percent(5, base, 8);
456 }
457
458 static void sfb_account_overflows(struct cpu_hw_sf *cpuhw,
459 struct hw_perf_event *hwc)
460 {
461 unsigned long ratio, num;
462
463 if (!OVERFLOW_REG(hwc))
464 return;
465
466
467
468
469
470
471
472
473 ratio = DIV_ROUND_UP(100 * OVERFLOW_REG(hwc) * cpuhw->sfb.num_sdb,
474 sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc)));
475
476
477 num = compute_sfb_extent(ratio, cpuhw->sfb.num_sdb);
478 if (num)
479 sfb_account_allocs(num, hwc);
480
481 debug_sprintf_event(sfdbg, 5, "sfb: overflow: overflow=%llu ratio=%lu"
482 " num=%lu\n", OVERFLOW_REG(hwc), ratio, num);
483 OVERFLOW_REG(hwc) = 0;
484 }
485
486
487
488
489
490
491
492
493
494
495
496
497 static void extend_sampling_buffer(struct sf_buffer *sfb,
498 struct hw_perf_event *hwc)
499 {
500 unsigned long num, num_old;
501 int rc;
502
503 num = sfb_pending_allocs(sfb, hwc);
504 if (!num)
505 return;
506 num_old = sfb->num_sdb;
507
508
509
510
511 sf_disable();
512
513
514
515
516
517
518 rc = realloc_sampling_buffer(sfb, num, GFP_ATOMIC);
519 if (rc)
520 debug_sprintf_event(sfdbg, 5, "sfb: extend: realloc "
521 "failed with rc=%i\n", rc);
522
523 if (sfb_has_pending_allocs(sfb, hwc))
524 debug_sprintf_event(sfdbg, 5, "sfb: extend: "
525 "req=%lu alloc=%lu remaining=%lu\n",
526 num, sfb->num_sdb - num_old,
527 sfb_pending_allocs(sfb, hwc));
528 }
529
530
531 static atomic_t num_events;
532
533 static DEFINE_MUTEX(pmc_reserve_mutex);
534
535 #define PMC_INIT 0
536 #define PMC_RELEASE 1
537 #define PMC_FAILURE 2
538 static void setup_pmc_cpu(void *flags)
539 {
540 int err;
541 struct cpu_hw_sf *cpusf = this_cpu_ptr(&cpu_hw_sf);
542
543 err = 0;
544 switch (*((int *) flags)) {
545 case PMC_INIT:
546 memset(cpusf, 0, sizeof(*cpusf));
547 err = qsi(&cpusf->qsi);
548 if (err)
549 break;
550 cpusf->flags |= PMU_F_RESERVED;
551 err = sf_disable();
552 if (err)
553 pr_err("Switching off the sampling facility failed "
554 "with rc=%i\n", err);
555 debug_sprintf_event(sfdbg, 5,
556 "setup_pmc_cpu: initialized: cpuhw=%p\n", cpusf);
557 break;
558 case PMC_RELEASE:
559 cpusf->flags &= ~PMU_F_RESERVED;
560 err = sf_disable();
561 if (err) {
562 pr_err("Switching off the sampling facility failed "
563 "with rc=%i\n", err);
564 } else
565 deallocate_buffers(cpusf);
566 debug_sprintf_event(sfdbg, 5,
567 "setup_pmc_cpu: released: cpuhw=%p\n", cpusf);
568 break;
569 }
570 if (err)
571 *((int *) flags) |= PMC_FAILURE;
572 }
573
574 static void release_pmc_hardware(void)
575 {
576 int flags = PMC_RELEASE;
577
578 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
579 on_each_cpu(setup_pmc_cpu, &flags, 1);
580 }
581
582 static int reserve_pmc_hardware(void)
583 {
584 int flags = PMC_INIT;
585
586 on_each_cpu(setup_pmc_cpu, &flags, 1);
587 if (flags & PMC_FAILURE) {
588 release_pmc_hardware();
589 return -ENODEV;
590 }
591 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
592
593 return 0;
594 }
595
596 static void hw_perf_event_destroy(struct perf_event *event)
597 {
598
599 if (!atomic_add_unless(&num_events, -1, 1)) {
600 mutex_lock(&pmc_reserve_mutex);
601 if (atomic_dec_return(&num_events) == 0)
602 release_pmc_hardware();
603 mutex_unlock(&pmc_reserve_mutex);
604 }
605 }
606
607 static void hw_init_period(struct hw_perf_event *hwc, u64 period)
608 {
609 hwc->sample_period = period;
610 hwc->last_period = hwc->sample_period;
611 local64_set(&hwc->period_left, hwc->sample_period);
612 }
613
614 static void hw_reset_registers(struct hw_perf_event *hwc,
615 unsigned long *sdbt_origin)
616 {
617
618 TEAR_REG(hwc) = (unsigned long) sdbt_origin;
619 }
620
621 static unsigned long hw_limit_rate(const struct hws_qsi_info_block *si,
622 unsigned long rate)
623 {
624 return clamp_t(unsigned long, rate,
625 si->min_sampl_rate, si->max_sampl_rate);
626 }
627
628 static u32 cpumsf_pid_type(struct perf_event *event,
629 u32 pid, enum pid_type type)
630 {
631 struct task_struct *tsk;
632
633
634 if (!pid)
635 goto out;
636
637 tsk = find_task_by_pid_ns(pid, &init_pid_ns);
638 pid = -1;
639 if (tsk) {
640
641
642
643
644 if (event->parent)
645 event = event->parent;
646 pid = __task_pid_nr_ns(tsk, type, event->ns);
647
648
649
650
651 if (!pid && !pid_alive(tsk))
652 pid = -1;
653 }
654 out:
655 return pid;
656 }
657
658 static void cpumsf_output_event_pid(struct perf_event *event,
659 struct perf_sample_data *data,
660 struct pt_regs *regs)
661 {
662 u32 pid;
663 struct perf_event_header header;
664 struct perf_output_handle handle;
665
666
667
668
669
670 pid = data->tid_entry.pid;
671
672
673 rcu_read_lock();
674
675 perf_prepare_sample(&header, data, event, regs);
676 if (perf_output_begin(&handle, event, header.size))
677 goto out;
678
679
680 data->tid_entry.pid = cpumsf_pid_type(event, pid, PIDTYPE_TGID);
681 data->tid_entry.tid = cpumsf_pid_type(event, pid, PIDTYPE_PID);
682
683 perf_output_sample(&handle, &header, data, event);
684 perf_output_end(&handle);
685 out:
686 rcu_read_unlock();
687 }
688
689 static unsigned long getrate(bool freq, unsigned long sample,
690 struct hws_qsi_info_block *si)
691 {
692 unsigned long rate;
693
694 if (freq) {
695 rate = freq_to_sample_rate(si, sample);
696 rate = hw_limit_rate(si, rate);
697 } else {
698
699
700
701
702 rate = hw_limit_rate(si, sample);
703
704
705
706
707
708
709
710 if (sample_rate_to_freq(si, rate) >
711 sysctl_perf_event_sample_rate) {
712 debug_sprintf_event(sfdbg, 1,
713 "Sampling rate exceeds maximum "
714 "perf sample rate\n");
715 rate = 0;
716 }
717 }
718 return rate;
719 }
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738 static int __hw_perf_event_init_rate(struct perf_event *event,
739 struct hws_qsi_info_block *si)
740 {
741 struct perf_event_attr *attr = &event->attr;
742 struct hw_perf_event *hwc = &event->hw;
743 unsigned long rate;
744
745 if (attr->freq) {
746 if (!attr->sample_freq)
747 return -EINVAL;
748 rate = getrate(attr->freq, attr->sample_freq, si);
749 attr->freq = 0;
750 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FREQ_MODE;
751 } else {
752 rate = getrate(attr->freq, attr->sample_period, si);
753 if (!rate)
754 return -EINVAL;
755 }
756 attr->sample_period = rate;
757 SAMPL_RATE(hwc) = rate;
758 hw_init_period(hwc, SAMPL_RATE(hwc));
759 debug_sprintf_event(sfdbg, 4, "__hw_perf_event_init_rate:"
760 "cpu:%d period:%llx freq:%d,%#lx\n", event->cpu,
761 event->attr.sample_period, event->attr.freq,
762 SAMPLE_FREQ_MODE(hwc));
763 return 0;
764 }
765
766 static int __hw_perf_event_init(struct perf_event *event)
767 {
768 struct cpu_hw_sf *cpuhw;
769 struct hws_qsi_info_block si;
770 struct perf_event_attr *attr = &event->attr;
771 struct hw_perf_event *hwc = &event->hw;
772 int cpu, err;
773
774
775 err = 0;
776 if (!atomic_inc_not_zero(&num_events)) {
777 mutex_lock(&pmc_reserve_mutex);
778 if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
779 err = -EBUSY;
780 else
781 atomic_inc(&num_events);
782 mutex_unlock(&pmc_reserve_mutex);
783 }
784 event->destroy = hw_perf_event_destroy;
785
786 if (err)
787 goto out;
788
789
790
791
792
793
794
795
796
797
798 memset(&si, 0, sizeof(si));
799 cpuhw = NULL;
800 if (event->cpu == -1)
801 qsi(&si);
802 else {
803
804
805
806 cpuhw = &per_cpu(cpu_hw_sf, event->cpu);
807 si = cpuhw->qsi;
808 }
809
810
811
812
813
814 if (!si.as) {
815 err = -ENOENT;
816 goto out;
817 }
818
819 if (si.ribm & CPU_MF_SF_RIBM_NOTAV) {
820 pr_warn("CPU Measurement Facility sampling is temporarily not available\n");
821 err = -EBUSY;
822 goto out;
823 }
824
825
826 SAMPL_FLAGS(hwc) = PERF_CPUM_SF_BASIC_MODE;
827
828
829
830
831 if (attr->config == PERF_EVENT_CPUM_SF_DIAG) {
832 if (!si.ad) {
833 err = -EPERM;
834 goto out;
835 }
836 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_DIAG_MODE;
837 }
838
839
840 if (attr->config1 & PERF_CPUM_SF_FULL_BLOCKS)
841 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FULL_BLOCKS;
842
843 err = __hw_perf_event_init_rate(event, &si);
844 if (err)
845 goto out;
846
847
848 hwc->extra_reg.reg = REG_OVERFLOW;
849 OVERFLOW_REG(hwc) = 0;
850
851
852 if (attr->config == PERF_EVENT_CPUM_SF_DIAG)
853 return 0;
854
855
856
857
858
859
860 if (cpuhw)
861
862 err = allocate_buffers(cpuhw, hwc);
863 else {
864
865
866
867 for_each_online_cpu(cpu) {
868 cpuhw = &per_cpu(cpu_hw_sf, cpu);
869 err = allocate_buffers(cpuhw, hwc);
870 if (err)
871 break;
872 }
873 }
874
875
876
877
878
879 if (event->attr.sample_type & PERF_SAMPLE_TID)
880 if (is_default_overflow_handler(event))
881 event->overflow_handler = cpumsf_output_event_pid;
882 out:
883 return err;
884 }
885
886 static int cpumsf_pmu_event_init(struct perf_event *event)
887 {
888 int err;
889
890
891 if (has_branch_stack(event))
892 return -EOPNOTSUPP;
893
894 switch (event->attr.type) {
895 case PERF_TYPE_RAW:
896 if ((event->attr.config != PERF_EVENT_CPUM_SF) &&
897 (event->attr.config != PERF_EVENT_CPUM_SF_DIAG))
898 return -ENOENT;
899 break;
900 case PERF_TYPE_HARDWARE:
901
902
903
904
905
906 if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES)
907 return -ENOENT;
908 if (!is_sampling_event(event))
909 return -ENOENT;
910 break;
911 default:
912 return -ENOENT;
913 }
914
915
916 if (event->cpu >= 0 && !cpu_online(event->cpu))
917 return -ENODEV;
918
919
920
921
922 if (event->attr.exclude_hv)
923 event->attr.exclude_hv = 0;
924 if (event->attr.exclude_idle)
925 event->attr.exclude_idle = 0;
926
927 err = __hw_perf_event_init(event);
928 if (unlikely(err))
929 if (event->destroy)
930 event->destroy(event);
931 return err;
932 }
933
934 static void cpumsf_pmu_enable(struct pmu *pmu)
935 {
936 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
937 struct hw_perf_event *hwc;
938 int err;
939
940 if (cpuhw->flags & PMU_F_ENABLED)
941 return;
942
943 if (cpuhw->flags & PMU_F_ERR_MASK)
944 return;
945
946
947
948
949
950
951
952
953
954
955
956
957 if (cpuhw->event) {
958 hwc = &cpuhw->event->hw;
959 if (!(SAMPL_DIAG_MODE(hwc))) {
960
961
962
963
964 sfb_account_overflows(cpuhw, hwc);
965 if (sfb_has_pending_allocs(&cpuhw->sfb, hwc))
966 extend_sampling_buffer(&cpuhw->sfb, hwc);
967 }
968
969 cpuhw->lsctl.interval = SAMPL_RATE(&cpuhw->event->hw);
970 }
971
972
973 cpuhw->flags |= PMU_F_ENABLED;
974 barrier();
975
976 err = lsctl(&cpuhw->lsctl);
977 if (err) {
978 cpuhw->flags &= ~PMU_F_ENABLED;
979 pr_err("Loading sampling controls failed: op=%i err=%i\n",
980 1, err);
981 return;
982 }
983
984
985 lpp(&S390_lowcore.lpp);
986
987 debug_sprintf_event(sfdbg, 6, "pmu_enable: es=%i cs=%i ed=%i cd=%i "
988 "interval:%lx tear=%p dear=%p\n",
989 cpuhw->lsctl.es, cpuhw->lsctl.cs, cpuhw->lsctl.ed,
990 cpuhw->lsctl.cd, cpuhw->lsctl.interval,
991 (void *) cpuhw->lsctl.tear,
992 (void *) cpuhw->lsctl.dear);
993 }
994
995 static void cpumsf_pmu_disable(struct pmu *pmu)
996 {
997 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
998 struct hws_lsctl_request_block inactive;
999 struct hws_qsi_info_block si;
1000 int err;
1001
1002 if (!(cpuhw->flags & PMU_F_ENABLED))
1003 return;
1004
1005 if (cpuhw->flags & PMU_F_ERR_MASK)
1006 return;
1007
1008
1009 inactive = cpuhw->lsctl;
1010 inactive.cs = 0;
1011 inactive.cd = 0;
1012
1013 err = lsctl(&inactive);
1014 if (err) {
1015 pr_err("Loading sampling controls failed: op=%i err=%i\n",
1016 2, err);
1017 return;
1018 }
1019
1020
1021 if (!qsi(&si)) {
1022
1023
1024
1025
1026
1027 if (si.es) {
1028 cpuhw->lsctl.tear = si.tear;
1029 cpuhw->lsctl.dear = si.dear;
1030 }
1031 } else
1032 debug_sprintf_event(sfdbg, 3, "cpumsf_pmu_disable: "
1033 "qsi() failed with err=%i\n", err);
1034
1035 cpuhw->flags &= ~PMU_F_ENABLED;
1036 }
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047 static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
1048 struct perf_sf_sde_regs *sde_regs)
1049 {
1050 if (event->attr.exclude_user && user_mode(regs))
1051 return 1;
1052 if (event->attr.exclude_kernel && !user_mode(regs))
1053 return 1;
1054 if (event->attr.exclude_guest && sde_regs->in_guest)
1055 return 1;
1056 if (event->attr.exclude_host && !sde_regs->in_guest)
1057 return 1;
1058 return 0;
1059 }
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072 static int perf_push_sample(struct perf_event *event,
1073 struct hws_basic_entry *basic)
1074 {
1075 int overflow;
1076 struct pt_regs regs;
1077 struct perf_sf_sde_regs *sde_regs;
1078 struct perf_sample_data data;
1079
1080
1081 perf_sample_data_init(&data, 0, event->hw.last_period);
1082
1083
1084
1085
1086
1087
1088 memset(®s, 0, sizeof(regs));
1089 regs.int_code = 0x1407;
1090 regs.int_parm = CPU_MF_INT_SF_PRA;
1091 sde_regs = (struct perf_sf_sde_regs *) ®s.int_parm_long;
1092
1093 psw_bits(regs.psw).ia = basic->ia;
1094 psw_bits(regs.psw).dat = basic->T;
1095 psw_bits(regs.psw).wait = basic->W;
1096 psw_bits(regs.psw).pstate = basic->P;
1097 psw_bits(regs.psw).as = basic->AS;
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110 switch (basic->CL) {
1111 case 1:
1112 sde_regs->in_guest = 0;
1113 break;
1114 case 2:
1115 sde_regs->in_guest = 1;
1116 break;
1117 default:
1118 if (basic->gpp || basic->prim_asn != 0xffff)
1119 sde_regs->in_guest = 1;
1120 break;
1121 }
1122
1123
1124
1125
1126
1127 data.tid_entry.pid = basic->hpp & LPP_PID_MASK;
1128
1129 overflow = 0;
1130 if (perf_exclude_event(event, ®s, sde_regs))
1131 goto out;
1132 if (perf_event_overflow(event, &data, ®s)) {
1133 overflow = 1;
1134 event->pmu->stop(event, 0);
1135 }
1136 perf_event_update_userpage(event);
1137 out:
1138 return overflow;
1139 }
1140
1141 static void perf_event_count_update(struct perf_event *event, u64 count)
1142 {
1143 local64_add(count, &event->count);
1144 }
1145
1146 static void debug_sample_entry(struct hws_basic_entry *sample,
1147 struct hws_trailer_entry *te)
1148 {
1149 debug_sprintf_event(sfdbg, 4, "hw_collect_samples: Found unknown "
1150 "sampling data entry: te->f=%i basic.def=%04x "
1151 "(%p)\n",
1152 te->f, sample->def, sample);
1153 }
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175 static void hw_collect_samples(struct perf_event *event, unsigned long *sdbt,
1176 unsigned long long *overflow)
1177 {
1178 struct hws_trailer_entry *te;
1179 struct hws_basic_entry *sample;
1180
1181 te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
1182 sample = (struct hws_basic_entry *) *sdbt;
1183 while ((unsigned long *) sample < (unsigned long *) te) {
1184
1185 if (!sample->def)
1186 break;
1187
1188
1189 perf_event_count_update(event, SAMPL_RATE(&event->hw));
1190
1191
1192 if (sample->def == 0x0001) {
1193
1194
1195
1196
1197 if (!*overflow) {
1198
1199 if (sample->I == 0 && sample->W == 0) {
1200
1201 *overflow = perf_push_sample(event,
1202 sample);
1203 }
1204 } else
1205
1206 *overflow += 1;
1207 } else {
1208 debug_sample_entry(sample, te);
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219 if (!te->f)
1220 break;
1221 }
1222
1223
1224 sample->def = 0;
1225 sample++;
1226 }
1227 }
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243 static void hw_perf_event_update(struct perf_event *event, int flush_all)
1244 {
1245 struct hw_perf_event *hwc = &event->hw;
1246 struct hws_trailer_entry *te;
1247 unsigned long *sdbt;
1248 unsigned long long event_overflow, sampl_overflow, num_sdb, te_flags;
1249 int done;
1250
1251
1252
1253
1254
1255 if (SAMPL_DIAG_MODE(&event->hw))
1256 return;
1257
1258 if (flush_all && SDB_FULL_BLOCKS(hwc))
1259 flush_all = 0;
1260
1261 sdbt = (unsigned long *) TEAR_REG(hwc);
1262 done = event_overflow = sampl_overflow = num_sdb = 0;
1263 while (!done) {
1264
1265 te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
1266
1267
1268 if (!te->f) {
1269 done = 1;
1270 if (!flush_all)
1271 break;
1272 }
1273
1274
1275 if (te->overflow)
1276
1277
1278
1279
1280 sampl_overflow += te->overflow;
1281
1282
1283 debug_sprintf_event(sfdbg, 6, "hw_perf_event_update: sdbt=%p "
1284 "overflow=%llu timestamp=%#llx\n",
1285 sdbt, te->overflow,
1286 (te->f) ? trailer_timestamp(te) : 0ULL);
1287
1288
1289
1290
1291
1292 hw_collect_samples(event, sdbt, &event_overflow);
1293 num_sdb++;
1294
1295
1296 do {
1297 te_flags = te->flags & ~SDB_TE_BUFFER_FULL_MASK;
1298 te_flags |= SDB_TE_ALERT_REQ_MASK;
1299 } while (!cmpxchg_double(&te->flags, &te->overflow,
1300 te->flags, te->overflow,
1301 te_flags, 0ULL));
1302
1303
1304 sdbt++;
1305 if (is_link_entry(sdbt))
1306 sdbt = get_next_sdbt(sdbt);
1307
1308
1309 TEAR_REG(hwc) = (unsigned long) sdbt;
1310
1311
1312
1313
1314 if (flush_all && done)
1315 break;
1316 }
1317
1318
1319 if (sampl_overflow)
1320 OVERFLOW_REG(hwc) = DIV_ROUND_UP(OVERFLOW_REG(hwc) +
1321 sampl_overflow, 1 + num_sdb);
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331 if (event_overflow) {
1332 SAMPL_RATE(hwc) += DIV_ROUND_UP(SAMPL_RATE(hwc), 10);
1333 debug_sprintf_event(sfdbg, 1, "%s: rate adjustment %ld\n",
1334 __func__,
1335 DIV_ROUND_UP(SAMPL_RATE(hwc), 10));
1336 }
1337
1338 if (sampl_overflow || event_overflow)
1339 debug_sprintf_event(sfdbg, 4, "hw_perf_event_update: "
1340 "overflow stats: sample=%llu event=%llu\n",
1341 sampl_overflow, event_overflow);
1342 }
1343
1344 #define AUX_SDB_INDEX(aux, i) ((i) % aux->sfb.num_sdb)
1345 #define AUX_SDB_NUM(aux, start, end) (end >= start ? end - start + 1 : 0)
1346 #define AUX_SDB_NUM_ALERT(aux) AUX_SDB_NUM(aux, aux->head, aux->alert_mark)
1347 #define AUX_SDB_NUM_EMPTY(aux) AUX_SDB_NUM(aux, aux->head, aux->empty_mark)
1348
1349
1350
1351
1352 static struct hws_trailer_entry *aux_sdb_trailer(struct aux_buffer *aux,
1353 unsigned long index)
1354 {
1355 unsigned long sdb;
1356
1357 index = AUX_SDB_INDEX(aux, index);
1358 sdb = aux->sdb_index[index];
1359 return (struct hws_trailer_entry *)trailer_entry_ptr(sdb);
1360 }
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371 static void aux_output_end(struct perf_output_handle *handle)
1372 {
1373 unsigned long i, range_scan, idx;
1374 struct aux_buffer *aux;
1375 struct hws_trailer_entry *te;
1376
1377 aux = perf_get_aux(handle);
1378 if (!aux)
1379 return;
1380
1381 range_scan = AUX_SDB_NUM_ALERT(aux);
1382 for (i = 0, idx = aux->head; i < range_scan; i++, idx++) {
1383 te = aux_sdb_trailer(aux, idx);
1384 if (!(te->flags & SDB_TE_BUFFER_FULL_MASK))
1385 break;
1386 }
1387
1388 perf_aux_output_end(handle, i << PAGE_SHIFT);
1389
1390
1391 te = aux_sdb_trailer(aux, aux->alert_mark);
1392 te->flags &= ~SDB_TE_ALERT_REQ_MASK;
1393
1394 debug_sprintf_event(sfdbg, 6, "aux_output_end: collect %lx SDBs\n", i);
1395 }
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406 static int aux_output_begin(struct perf_output_handle *handle,
1407 struct aux_buffer *aux,
1408 struct cpu_hw_sf *cpuhw)
1409 {
1410 unsigned long range;
1411 unsigned long i, range_scan, idx;
1412 unsigned long head, base, offset;
1413 struct hws_trailer_entry *te;
1414
1415 if (WARN_ON_ONCE(handle->head & ~PAGE_MASK))
1416 return -EINVAL;
1417
1418 aux->head = handle->head >> PAGE_SHIFT;
1419 range = (handle->size + 1) >> PAGE_SHIFT;
1420 if (range <= 1)
1421 return -ENOMEM;
1422
1423
1424
1425
1426
1427 if (range > AUX_SDB_NUM_EMPTY(aux)) {
1428 range_scan = range - AUX_SDB_NUM_EMPTY(aux);
1429 idx = aux->empty_mark + 1;
1430 for (i = 0; i < range_scan; i++, idx++) {
1431 te = aux_sdb_trailer(aux, idx);
1432 te->flags = te->flags & ~SDB_TE_BUFFER_FULL_MASK;
1433 te->flags = te->flags & ~SDB_TE_ALERT_REQ_MASK;
1434 te->overflow = 0;
1435 }
1436
1437 aux->empty_mark = aux->head + range - 1;
1438 }
1439
1440
1441 aux->alert_mark = aux->head + range/2 - 1;
1442 te = aux_sdb_trailer(aux, aux->alert_mark);
1443 te->flags = te->flags | SDB_TE_ALERT_REQ_MASK;
1444
1445
1446 head = AUX_SDB_INDEX(aux, aux->head);
1447 base = aux->sdbt_index[head / CPUM_SF_SDB_PER_TABLE];
1448 offset = head % CPUM_SF_SDB_PER_TABLE;
1449 cpuhw->lsctl.tear = base + offset * sizeof(unsigned long);
1450 cpuhw->lsctl.dear = aux->sdb_index[head];
1451
1452 debug_sprintf_event(sfdbg, 6, "aux_output_begin: "
1453 "head->alert_mark->empty_mark (num_alert, range)"
1454 "[%lx -> %lx -> %lx] (%lx, %lx) "
1455 "tear index %lx, tear %lx dear %lx\n",
1456 aux->head, aux->alert_mark, aux->empty_mark,
1457 AUX_SDB_NUM_ALERT(aux), range,
1458 head / CPUM_SF_SDB_PER_TABLE,
1459 cpuhw->lsctl.tear,
1460 cpuhw->lsctl.dear);
1461
1462 return 0;
1463 }
1464
1465
1466
1467
1468
1469
1470
1471 static bool aux_set_alert(struct aux_buffer *aux, unsigned long alert_index,
1472 unsigned long long *overflow)
1473 {
1474 unsigned long long orig_overflow, orig_flags, new_flags;
1475 struct hws_trailer_entry *te;
1476
1477 te = aux_sdb_trailer(aux, alert_index);
1478 do {
1479 orig_flags = te->flags;
1480 orig_overflow = te->overflow;
1481 *overflow = orig_overflow;
1482 if (orig_flags & SDB_TE_BUFFER_FULL_MASK) {
1483
1484
1485
1486
1487
1488 return false;
1489 }
1490 new_flags = orig_flags | SDB_TE_ALERT_REQ_MASK;
1491 } while (!cmpxchg_double(&te->flags, &te->overflow,
1492 orig_flags, orig_overflow,
1493 new_flags, 0ULL));
1494 return true;
1495 }
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519 static bool aux_reset_buffer(struct aux_buffer *aux, unsigned long range,
1520 unsigned long long *overflow)
1521 {
1522 unsigned long long orig_overflow, orig_flags, new_flags;
1523 unsigned long i, range_scan, idx;
1524 struct hws_trailer_entry *te;
1525
1526 if (range <= AUX_SDB_NUM_EMPTY(aux))
1527
1528
1529
1530
1531
1532 return aux_set_alert(aux, aux->alert_mark, overflow);
1533
1534 if (aux->alert_mark <= aux->empty_mark)
1535
1536
1537
1538
1539 if (!aux_set_alert(aux, aux->alert_mark, overflow))
1540 return false;
1541
1542
1543
1544
1545
1546
1547 range_scan = range - AUX_SDB_NUM_EMPTY(aux);
1548 idx = aux->empty_mark + 1;
1549 for (i = 0; i < range_scan; i++, idx++) {
1550 te = aux_sdb_trailer(aux, idx);
1551 do {
1552 orig_flags = te->flags;
1553 orig_overflow = te->overflow;
1554 new_flags = orig_flags & ~SDB_TE_BUFFER_FULL_MASK;
1555 if (idx == aux->alert_mark)
1556 new_flags |= SDB_TE_ALERT_REQ_MASK;
1557 else
1558 new_flags &= ~SDB_TE_ALERT_REQ_MASK;
1559 } while (!cmpxchg_double(&te->flags, &te->overflow,
1560 orig_flags, orig_overflow,
1561 new_flags, 0ULL));
1562 *overflow += orig_overflow;
1563 }
1564
1565
1566 aux->empty_mark = aux->head + range - 1;
1567
1568 return true;
1569 }
1570
1571
1572
1573
1574 static void hw_collect_aux(struct cpu_hw_sf *cpuhw)
1575 {
1576 struct aux_buffer *aux;
1577 int done = 0;
1578 unsigned long range = 0, size;
1579 unsigned long long overflow = 0;
1580 struct perf_output_handle *handle = &cpuhw->handle;
1581 unsigned long num_sdb;
1582
1583 aux = perf_get_aux(handle);
1584 if (WARN_ON_ONCE(!aux))
1585 return;
1586
1587
1588 size = AUX_SDB_NUM_ALERT(aux) << PAGE_SHIFT;
1589 perf_aux_output_end(handle, size);
1590 num_sdb = aux->sfb.num_sdb;
1591
1592 num_sdb = aux->sfb.num_sdb;
1593 while (!done) {
1594
1595 aux = perf_aux_output_begin(handle, cpuhw->event);
1596 if (handle->size == 0) {
1597 pr_err("The AUX buffer with %lu pages for the "
1598 "diagnostic-sampling mode is full\n",
1599 num_sdb);
1600 debug_sprintf_event(sfdbg, 1, "AUX buffer used up\n");
1601 break;
1602 }
1603 if (WARN_ON_ONCE(!aux))
1604 return;
1605
1606
1607 aux->head = handle->head >> PAGE_SHIFT;
1608 range = (handle->size + 1) >> PAGE_SHIFT;
1609 if (range == 1)
1610 aux->alert_mark = aux->head;
1611 else
1612 aux->alert_mark = aux->head + range/2 - 1;
1613
1614 if (aux_reset_buffer(aux, range, &overflow)) {
1615 if (!overflow) {
1616 done = 1;
1617 break;
1618 }
1619 size = range << PAGE_SHIFT;
1620 perf_aux_output_end(&cpuhw->handle, size);
1621 pr_err("Sample data caused the AUX buffer with %lu "
1622 "pages to overflow\n", num_sdb);
1623 debug_sprintf_event(sfdbg, 1, "head %lx range %lx "
1624 "overflow %llx\n",
1625 aux->head, range, overflow);
1626 } else {
1627 size = AUX_SDB_NUM_ALERT(aux) << PAGE_SHIFT;
1628 perf_aux_output_end(&cpuhw->handle, size);
1629 debug_sprintf_event(sfdbg, 6, "head %lx alert %lx "
1630 "already full, try another\n",
1631 aux->head, aux->alert_mark);
1632 }
1633 }
1634
1635 if (done)
1636 debug_sprintf_event(sfdbg, 6, "aux_reset_buffer: "
1637 "[%lx -> %lx -> %lx] (%lx, %lx)\n",
1638 aux->head, aux->alert_mark, aux->empty_mark,
1639 AUX_SDB_NUM_ALERT(aux), range);
1640 }
1641
1642
1643
1644
1645 static void aux_buffer_free(void *data)
1646 {
1647 struct aux_buffer *aux = data;
1648 unsigned long i, num_sdbt;
1649
1650 if (!aux)
1651 return;
1652
1653
1654 num_sdbt = aux->sfb.num_sdbt;
1655 for (i = 0; i < num_sdbt; i++)
1656 free_page(aux->sdbt_index[i]);
1657
1658 kfree(aux->sdbt_index);
1659 kfree(aux->sdb_index);
1660 kfree(aux);
1661
1662 debug_sprintf_event(sfdbg, 4, "aux_buffer_free: free "
1663 "%lu SDBTs\n", num_sdbt);
1664 }
1665
1666 static void aux_sdb_init(unsigned long sdb)
1667 {
1668 struct hws_trailer_entry *te;
1669
1670 te = (struct hws_trailer_entry *)trailer_entry_ptr(sdb);
1671
1672
1673 te->clock_base = 1;
1674 memcpy(&te->progusage2, &tod_clock_base[1], 8);
1675 }
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691 static void *aux_buffer_setup(struct perf_event *event, void **pages,
1692 int nr_pages, bool snapshot)
1693 {
1694 struct sf_buffer *sfb;
1695 struct aux_buffer *aux;
1696 unsigned long *new, *tail;
1697 int i, n_sdbt;
1698
1699 if (!nr_pages || !pages)
1700 return NULL;
1701
1702 if (nr_pages > CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR) {
1703 pr_err("AUX buffer size (%i pages) is larger than the "
1704 "maximum sampling buffer limit\n",
1705 nr_pages);
1706 return NULL;
1707 } else if (nr_pages < CPUM_SF_MIN_SDB * CPUM_SF_SDB_DIAG_FACTOR) {
1708 pr_err("AUX buffer size (%i pages) is less than the "
1709 "minimum sampling buffer limit\n",
1710 nr_pages);
1711 return NULL;
1712 }
1713
1714
1715 aux = kmalloc(sizeof(struct aux_buffer), GFP_KERNEL);
1716 if (!aux)
1717 goto no_aux;
1718 sfb = &aux->sfb;
1719
1720
1721 n_sdbt = (nr_pages + CPUM_SF_SDB_PER_TABLE - 1) / CPUM_SF_SDB_PER_TABLE;
1722 aux->sdbt_index = kmalloc_array(n_sdbt, sizeof(void *), GFP_KERNEL);
1723 if (!aux->sdbt_index)
1724 goto no_sdbt_index;
1725
1726
1727 aux->sdb_index = kmalloc_array(nr_pages, sizeof(void *), GFP_KERNEL);
1728 if (!aux->sdb_index)
1729 goto no_sdb_index;
1730
1731
1732 sfb->num_sdbt = 0;
1733 sfb->sdbt = (unsigned long *) get_zeroed_page(GFP_KERNEL);
1734 if (!sfb->sdbt)
1735 goto no_sdbt;
1736 aux->sdbt_index[sfb->num_sdbt++] = (unsigned long)sfb->sdbt;
1737 tail = sfb->tail = sfb->sdbt;
1738
1739
1740
1741
1742
1743 for (i = 0; i < nr_pages; i++, tail++) {
1744 if (require_table_link(tail)) {
1745 new = (unsigned long *) get_zeroed_page(GFP_KERNEL);
1746 if (!new)
1747 goto no_sdbt;
1748 aux->sdbt_index[sfb->num_sdbt++] = (unsigned long)new;
1749
1750 *tail = (unsigned long)(void *) new + 1;
1751 tail = new;
1752 }
1753
1754 *tail = (unsigned long)pages[i];
1755 aux->sdb_index[i] = (unsigned long)pages[i];
1756 aux_sdb_init((unsigned long)pages[i]);
1757 }
1758 sfb->num_sdb = nr_pages;
1759
1760
1761 *tail = (unsigned long) sfb->sdbt + 1;
1762 sfb->tail = tail;
1763
1764
1765
1766
1767
1768
1769 aux->empty_mark = sfb->num_sdb - 1;
1770
1771 debug_sprintf_event(sfdbg, 4, "aux_buffer_setup: setup %lu SDBTs"
1772 " and %lu SDBs\n",
1773 sfb->num_sdbt, sfb->num_sdb);
1774
1775 return aux;
1776
1777 no_sdbt:
1778
1779 for (i = 0; i < sfb->num_sdbt; i++)
1780 free_page(aux->sdbt_index[i]);
1781 kfree(aux->sdb_index);
1782 no_sdb_index:
1783 kfree(aux->sdbt_index);
1784 no_sdbt_index:
1785 kfree(aux);
1786 no_aux:
1787 return NULL;
1788 }
1789
1790 static void cpumsf_pmu_read(struct perf_event *event)
1791 {
1792
1793 }
1794
1795
1796
1797
1798
1799 static int cpumsf_pmu_check_period(struct perf_event *event, u64 value)
1800 {
1801 struct hws_qsi_info_block si;
1802 unsigned long rate;
1803 bool do_freq;
1804
1805 memset(&si, 0, sizeof(si));
1806 if (event->cpu == -1) {
1807 if (qsi(&si))
1808 return -ENODEV;
1809 } else {
1810
1811
1812
1813 struct cpu_hw_sf *cpuhw = &per_cpu(cpu_hw_sf, event->cpu);
1814
1815 si = cpuhw->qsi;
1816 }
1817
1818 do_freq = !!SAMPLE_FREQ_MODE(&event->hw);
1819 rate = getrate(do_freq, value, &si);
1820 if (!rate)
1821 return -EINVAL;
1822
1823 event->attr.sample_period = rate;
1824 SAMPL_RATE(&event->hw) = rate;
1825 hw_init_period(&event->hw, SAMPL_RATE(&event->hw));
1826 debug_sprintf_event(sfdbg, 4, "cpumsf_pmu_check_period:"
1827 "cpu:%d value:%llx period:%llx freq:%d\n",
1828 event->cpu, value,
1829 event->attr.sample_period, do_freq);
1830 return 0;
1831 }
1832
1833
1834
1835
1836 static void cpumsf_pmu_start(struct perf_event *event, int flags)
1837 {
1838 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
1839
1840 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
1841 return;
1842
1843 if (flags & PERF_EF_RELOAD)
1844 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1845
1846 perf_pmu_disable(event->pmu);
1847 event->hw.state = 0;
1848 cpuhw->lsctl.cs = 1;
1849 if (SAMPL_DIAG_MODE(&event->hw))
1850 cpuhw->lsctl.cd = 1;
1851 perf_pmu_enable(event->pmu);
1852 }
1853
1854
1855
1856
1857 static void cpumsf_pmu_stop(struct perf_event *event, int flags)
1858 {
1859 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
1860
1861 if (event->hw.state & PERF_HES_STOPPED)
1862 return;
1863
1864 perf_pmu_disable(event->pmu);
1865 cpuhw->lsctl.cs = 0;
1866 cpuhw->lsctl.cd = 0;
1867 event->hw.state |= PERF_HES_STOPPED;
1868
1869 if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
1870 hw_perf_event_update(event, 1);
1871 event->hw.state |= PERF_HES_UPTODATE;
1872 }
1873 perf_pmu_enable(event->pmu);
1874 }
1875
1876 static int cpumsf_pmu_add(struct perf_event *event, int flags)
1877 {
1878 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
1879 struct aux_buffer *aux;
1880 int err;
1881
1882 if (cpuhw->flags & PMU_F_IN_USE)
1883 return -EAGAIN;
1884
1885 if (!SAMPL_DIAG_MODE(&event->hw) && !cpuhw->sfb.sdbt)
1886 return -EINVAL;
1887
1888 err = 0;
1889 perf_pmu_disable(event->pmu);
1890
1891 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1892
1893
1894
1895
1896
1897
1898 cpuhw->lsctl.s = 0;
1899 cpuhw->lsctl.h = 1;
1900 cpuhw->lsctl.interval = SAMPL_RATE(&event->hw);
1901 if (!SAMPL_DIAG_MODE(&event->hw)) {
1902 cpuhw->lsctl.tear = (unsigned long) cpuhw->sfb.sdbt;
1903 cpuhw->lsctl.dear = *(unsigned long *) cpuhw->sfb.sdbt;
1904 hw_reset_registers(&event->hw, cpuhw->sfb.sdbt);
1905 }
1906
1907
1908
1909 if (WARN_ON_ONCE(cpuhw->lsctl.es == 1 || cpuhw->lsctl.ed == 1)) {
1910 err = -EAGAIN;
1911 goto out;
1912 }
1913 if (SAMPL_DIAG_MODE(&event->hw)) {
1914 aux = perf_aux_output_begin(&cpuhw->handle, event);
1915 if (!aux) {
1916 err = -EINVAL;
1917 goto out;
1918 }
1919 err = aux_output_begin(&cpuhw->handle, aux, cpuhw);
1920 if (err)
1921 goto out;
1922 cpuhw->lsctl.ed = 1;
1923 }
1924 cpuhw->lsctl.es = 1;
1925
1926
1927 cpuhw->event = event;
1928 cpuhw->flags |= PMU_F_IN_USE;
1929
1930 if (flags & PERF_EF_START)
1931 cpumsf_pmu_start(event, PERF_EF_RELOAD);
1932 out:
1933 perf_event_update_userpage(event);
1934 perf_pmu_enable(event->pmu);
1935 return err;
1936 }
1937
1938 static void cpumsf_pmu_del(struct perf_event *event, int flags)
1939 {
1940 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
1941
1942 perf_pmu_disable(event->pmu);
1943 cpumsf_pmu_stop(event, PERF_EF_UPDATE);
1944
1945 cpuhw->lsctl.es = 0;
1946 cpuhw->lsctl.ed = 0;
1947 cpuhw->flags &= ~PMU_F_IN_USE;
1948 cpuhw->event = NULL;
1949
1950 if (SAMPL_DIAG_MODE(&event->hw))
1951 aux_output_end(&cpuhw->handle);
1952 perf_event_update_userpage(event);
1953 perf_pmu_enable(event->pmu);
1954 }
1955
1956 CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC, PERF_EVENT_CPUM_SF);
1957 CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC_DIAG, PERF_EVENT_CPUM_SF_DIAG);
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975 enum {
1976 SF_CYCLES_BASIC_ATTR_IDX = 0,
1977 SF_CYCLES_BASIC_DIAG_ATTR_IDX,
1978 SF_CYCLES_ATTR_MAX
1979 };
1980
1981 static struct attribute *cpumsf_pmu_events_attr[SF_CYCLES_ATTR_MAX + 1] = {
1982 [SF_CYCLES_BASIC_ATTR_IDX] = CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC)
1983 };
1984
1985 PMU_FORMAT_ATTR(event, "config:0-63");
1986
1987 static struct attribute *cpumsf_pmu_format_attr[] = {
1988 &format_attr_event.attr,
1989 NULL,
1990 };
1991
1992 static struct attribute_group cpumsf_pmu_events_group = {
1993 .name = "events",
1994 .attrs = cpumsf_pmu_events_attr,
1995 };
1996
1997 static struct attribute_group cpumsf_pmu_format_group = {
1998 .name = "format",
1999 .attrs = cpumsf_pmu_format_attr,
2000 };
2001
2002 static const struct attribute_group *cpumsf_pmu_attr_groups[] = {
2003 &cpumsf_pmu_events_group,
2004 &cpumsf_pmu_format_group,
2005 NULL,
2006 };
2007
2008 static struct pmu cpumf_sampling = {
2009 .pmu_enable = cpumsf_pmu_enable,
2010 .pmu_disable = cpumsf_pmu_disable,
2011
2012 .event_init = cpumsf_pmu_event_init,
2013 .add = cpumsf_pmu_add,
2014 .del = cpumsf_pmu_del,
2015
2016 .start = cpumsf_pmu_start,
2017 .stop = cpumsf_pmu_stop,
2018 .read = cpumsf_pmu_read,
2019
2020 .attr_groups = cpumsf_pmu_attr_groups,
2021
2022 .setup_aux = aux_buffer_setup,
2023 .free_aux = aux_buffer_free,
2024
2025 .check_period = cpumsf_pmu_check_period,
2026 };
2027
2028 static void cpumf_measurement_alert(struct ext_code ext_code,
2029 unsigned int alert, unsigned long unused)
2030 {
2031 struct cpu_hw_sf *cpuhw;
2032
2033 if (!(alert & CPU_MF_INT_SF_MASK))
2034 return;
2035 inc_irq_stat(IRQEXT_CMS);
2036 cpuhw = this_cpu_ptr(&cpu_hw_sf);
2037
2038
2039
2040 if (!(cpuhw->flags & PMU_F_RESERVED))
2041 return;
2042
2043
2044
2045
2046
2047 if (alert & CPU_MF_INT_SF_PRA) {
2048 if (cpuhw->flags & PMU_F_IN_USE)
2049 if (SAMPL_DIAG_MODE(&cpuhw->event->hw))
2050 hw_collect_aux(cpuhw);
2051 else
2052 hw_perf_event_update(cpuhw->event, 0);
2053 else
2054 WARN_ON_ONCE(!(cpuhw->flags & PMU_F_IN_USE));
2055 }
2056
2057
2058 if (alert != CPU_MF_INT_SF_PRA)
2059 debug_sprintf_event(sfdbg, 6, "measurement alert: %#x\n",
2060 alert);
2061
2062
2063 if (alert & CPU_MF_INT_SF_SACA)
2064 qsi(&cpuhw->qsi);
2065
2066
2067 if (alert & CPU_MF_INT_SF_LSDA) {
2068 pr_err("Sample data was lost\n");
2069 cpuhw->flags |= PMU_F_ERR_LSDA;
2070 sf_disable();
2071 }
2072
2073
2074 if (alert & (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE)) {
2075 pr_err("A sampling buffer entry is incorrect (alert=0x%x)\n",
2076 alert);
2077 cpuhw->flags |= PMU_F_ERR_IBE;
2078 sf_disable();
2079 }
2080 }
2081
2082 static int cpusf_pmu_setup(unsigned int cpu, int flags)
2083 {
2084
2085
2086
2087 if (!atomic_read(&num_events))
2088 return 0;
2089
2090 local_irq_disable();
2091 setup_pmc_cpu(&flags);
2092 local_irq_enable();
2093 return 0;
2094 }
2095
2096 static int s390_pmu_sf_online_cpu(unsigned int cpu)
2097 {
2098 return cpusf_pmu_setup(cpu, PMC_INIT);
2099 }
2100
2101 static int s390_pmu_sf_offline_cpu(unsigned int cpu)
2102 {
2103 return cpusf_pmu_setup(cpu, PMC_RELEASE);
2104 }
2105
2106 static int param_get_sfb_size(char *buffer, const struct kernel_param *kp)
2107 {
2108 if (!cpum_sf_avail())
2109 return -ENODEV;
2110 return sprintf(buffer, "%lu,%lu", CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
2111 }
2112
2113 static int param_set_sfb_size(const char *val, const struct kernel_param *kp)
2114 {
2115 int rc;
2116 unsigned long min, max;
2117
2118 if (!cpum_sf_avail())
2119 return -ENODEV;
2120 if (!val || !strlen(val))
2121 return -EINVAL;
2122
2123
2124 min = CPUM_SF_MIN_SDB;
2125 max = CPUM_SF_MAX_SDB;
2126 if (strchr(val, ','))
2127 rc = (sscanf(val, "%lu,%lu", &min, &max) == 2) ? 0 : -EINVAL;
2128 else
2129 rc = kstrtoul(val, 10, &max);
2130
2131 if (min < 2 || min >= max || max > get_num_physpages())
2132 rc = -EINVAL;
2133 if (rc)
2134 return rc;
2135
2136 sfb_set_limits(min, max);
2137 pr_info("The sampling buffer limits have changed to: "
2138 "min=%lu max=%lu (diag=x%lu)\n",
2139 CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB, CPUM_SF_SDB_DIAG_FACTOR);
2140 return 0;
2141 }
2142
2143 #define param_check_sfb_size(name, p) __param_check(name, p, void)
2144 static const struct kernel_param_ops param_ops_sfb_size = {
2145 .set = param_set_sfb_size,
2146 .get = param_get_sfb_size,
2147 };
2148
2149 #define RS_INIT_FAILURE_QSI 0x0001
2150 #define RS_INIT_FAILURE_BSDES 0x0002
2151 #define RS_INIT_FAILURE_ALRT 0x0003
2152 #define RS_INIT_FAILURE_PERF 0x0004
2153 static void __init pr_cpumsf_err(unsigned int reason)
2154 {
2155 pr_err("Sampling facility support for perf is not available: "
2156 "reason=%04x\n", reason);
2157 }
2158
2159 static int __init init_cpum_sampling_pmu(void)
2160 {
2161 struct hws_qsi_info_block si;
2162 int err;
2163
2164 if (!cpum_sf_avail())
2165 return -ENODEV;
2166
2167 memset(&si, 0, sizeof(si));
2168 if (qsi(&si)) {
2169 pr_cpumsf_err(RS_INIT_FAILURE_QSI);
2170 return -ENODEV;
2171 }
2172
2173 if (!si.as && !si.ad)
2174 return -ENODEV;
2175
2176 if (si.bsdes != sizeof(struct hws_basic_entry)) {
2177 pr_cpumsf_err(RS_INIT_FAILURE_BSDES);
2178 return -EINVAL;
2179 }
2180
2181 if (si.ad) {
2182 sfb_set_limits(CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
2183
2184
2185
2186 cpumsf_pmu_events_attr[SF_CYCLES_BASIC_DIAG_ATTR_IDX] =
2187 CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC_DIAG);
2188 }
2189
2190 sfdbg = debug_register(KMSG_COMPONENT, 2, 1, 80);
2191 if (!sfdbg) {
2192 pr_err("Registering for s390dbf failed\n");
2193 return -ENOMEM;
2194 }
2195 debug_register_view(sfdbg, &debug_sprintf_view);
2196
2197 err = register_external_irq(EXT_IRQ_MEASURE_ALERT,
2198 cpumf_measurement_alert);
2199 if (err) {
2200 pr_cpumsf_err(RS_INIT_FAILURE_ALRT);
2201 debug_unregister(sfdbg);
2202 goto out;
2203 }
2204
2205 err = perf_pmu_register(&cpumf_sampling, "cpum_sf", PERF_TYPE_RAW);
2206 if (err) {
2207 pr_cpumsf_err(RS_INIT_FAILURE_PERF);
2208 unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
2209 cpumf_measurement_alert);
2210 debug_unregister(sfdbg);
2211 goto out;
2212 }
2213
2214 cpuhp_setup_state(CPUHP_AP_PERF_S390_SF_ONLINE, "perf/s390/sf:online",
2215 s390_pmu_sf_online_cpu, s390_pmu_sf_offline_cpu);
2216 out:
2217 return err;
2218 }
2219
2220 arch_initcall(init_cpum_sampling_pmu);
2221 core_param(cpum_sfb_size, CPUM_SF_MAX_SDB, sfb_size, 0640);