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