1
2
3
4
5
6
7
8
9
10
11#define KMSG_COMPONENT "cpum_sf"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
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>
21#include <linux/mm.h>
22#include <linux/moduleparam.h>
23#include <asm/cpu_mf.h>
24#include <asm/irq.h>
25#include <asm/debug.h>
26#include <asm/timex.h>
27
28
29
30
31
32#define CPUM_SF_MIN_SDBT 1
33
34
35
36
37
38#define CPUM_SF_SDB_PER_TABLE ((PAGE_SIZE - 8) / 8)
39
40
41
42
43
44#define CPUM_SF_SDBT_TL_OFFSET (CPUM_SF_SDB_PER_TABLE * 8)
45static inline int require_table_link(const void *sdbt)
46{
47 return ((unsigned long) sdbt & ~PAGE_MASK) == CPUM_SF_SDBT_TL_OFFSET;
48}
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68static unsigned long __read_mostly CPUM_SF_MIN_SDB = 15;
69static unsigned long __read_mostly CPUM_SF_MAX_SDB = 8176;
70static unsigned long __read_mostly CPUM_SF_SDB_DIAG_FACTOR = 1;
71
72struct sf_buffer {
73 unsigned long *sdbt;
74
75 unsigned long num_sdb;
76 unsigned long num_sdbt;
77 unsigned long *tail;
78};
79
80struct cpu_hw_sf {
81
82 struct hws_qsi_info_block qsi;
83
84 struct hws_lsctl_request_block lsctl;
85 struct sf_buffer sfb;
86 unsigned int flags;
87 struct perf_event *event;
88};
89static DEFINE_PER_CPU(struct cpu_hw_sf, cpu_hw_sf);
90
91
92static debug_info_t *sfdbg;
93
94
95
96
97static int sf_disable(void)
98{
99 struct hws_lsctl_request_block sreq;
100
101 memset(&sreq, 0, sizeof(sreq));
102 return lsctl(&sreq);
103}
104
105
106
107
108static int sf_buffer_available(struct cpu_hw_sf *cpuhw)
109{
110 return !!cpuhw->sfb.sdbt;
111}
112
113
114
115
116static void free_sampling_buffer(struct sf_buffer *sfb)
117{
118 unsigned long *sdbt, *curr;
119
120 if (!sfb->sdbt)
121 return;
122
123 sdbt = sfb->sdbt;
124 curr = sdbt;
125
126
127 while (1) {
128 if (!*curr || !sdbt)
129 break;
130
131
132 if (is_link_entry(curr)) {
133 curr = get_next_sdbt(curr);
134 if (sdbt)
135 free_page((unsigned long) sdbt);
136
137
138 if (curr == sfb->sdbt)
139 break;
140 else
141 sdbt = curr;
142 } else {
143
144 if (*curr) {
145 free_page(*curr);
146 curr++;
147 }
148 }
149 }
150
151 debug_sprintf_event(sfdbg, 5,
152 "free_sampling_buffer: freed sdbt=%p\n", sfb->sdbt);
153 memset(sfb, 0, sizeof(*sfb));
154}
155
156static int alloc_sample_data_block(unsigned long *sdbt, gfp_t gfp_flags)
157{
158 unsigned long sdb, *trailer;
159
160
161 sdb = get_zeroed_page(gfp_flags);
162 if (!sdb)
163 return -ENOMEM;
164 trailer = trailer_entry_ptr(sdb);
165 *trailer = SDB_TE_ALERT_REQ_MASK;
166
167
168 *sdbt = sdb;
169
170 return 0;
171}
172
173
174
175
176
177
178
179
180
181
182
183
184static int realloc_sampling_buffer(struct sf_buffer *sfb,
185 unsigned long num_sdb, gfp_t gfp_flags)
186{
187 int i, rc;
188 unsigned long *new, *tail;
189
190 if (!sfb->sdbt || !sfb->tail)
191 return -EINVAL;
192
193 if (!is_link_entry(sfb->tail))
194 return -EINVAL;
195
196
197
198
199
200
201 tail = sfb->tail;
202
203
204
205
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"
209 "tail=%p\n",
210 (void *) sfb->sdbt, (void *) tail);
211 return -EINVAL;
212 }
213
214
215 rc = 0;
216 for (i = 0; i < num_sdb; i++) {
217
218 if (require_table_link(tail)) {
219 new = (unsigned long *) get_zeroed_page(gfp_flags);
220 if (!new) {
221 rc = -ENOMEM;
222 break;
223 }
224 sfb->num_sdbt++;
225
226 *tail = (unsigned long)(void *) new + 1;
227 tail = new;
228 }
229
230
231
232
233
234
235 rc = alloc_sample_data_block(tail, gfp_flags);
236 if (rc)
237 break;
238 sfb->num_sdb++;
239 tail++;
240 }
241
242
243 *tail = (unsigned long) sfb->sdbt + 1;
244 sfb->tail = tail;
245
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);
249 return rc;
250}
251
252
253
254
255
256
257
258
259
260
261
262
263static int alloc_sampling_buffer(struct sf_buffer *sfb, unsigned long num_sdb)
264{
265 int rc;
266
267 if (sfb->sdbt)
268 return -EINVAL;
269
270
271 sfb->sdbt = (unsigned long *) get_zeroed_page(GFP_KERNEL);
272 if (!sfb->sdbt)
273 return -ENOMEM;
274 sfb->num_sdb = 0;
275 sfb->num_sdbt = 1;
276
277
278
279
280 sfb->tail = sfb->sdbt;
281 *sfb->tail = (unsigned long)(void *) sfb->sdbt + 1;
282
283
284 rc = realloc_sampling_buffer(sfb, num_sdb, GFP_KERNEL);
285 if (rc) {
286 free_sampling_buffer(sfb);
287 debug_sprintf_event(sfdbg, 4, "alloc_sampling_buffer: "
288 "realloc_sampling_buffer failed with rc=%i\n", rc);
289 } else
290 debug_sprintf_event(sfdbg, 4,
291 "alloc_sampling_buffer: tear=%p dear=%p\n",
292 sfb->sdbt, (void *) *sfb->sdbt);
293 return rc;
294}
295
296static void sfb_set_limits(unsigned long min, unsigned long max)
297{
298 struct hws_qsi_info_block si;
299
300 CPUM_SF_MIN_SDB = min;
301 CPUM_SF_MAX_SDB = max;
302
303 memset(&si, 0, sizeof(si));
304 if (!qsi(&si))
305 CPUM_SF_SDB_DIAG_FACTOR = DIV_ROUND_UP(si.dsdes, si.bsdes);
306}
307
308static unsigned long sfb_max_limit(struct hw_perf_event *hwc)
309{
310 return SAMPL_DIAG_MODE(hwc) ? CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR
311 : CPUM_SF_MAX_SDB;
312}
313
314static unsigned long sfb_pending_allocs(struct sf_buffer *sfb,
315 struct hw_perf_event *hwc)
316{
317 if (!sfb->sdbt)
318 return SFB_ALLOC_REG(hwc);
319 if (SFB_ALLOC_REG(hwc) > sfb->num_sdb)
320 return SFB_ALLOC_REG(hwc) - sfb->num_sdb;
321 return 0;
322}
323
324static int sfb_has_pending_allocs(struct sf_buffer *sfb,
325 struct hw_perf_event *hwc)
326{
327 return sfb_pending_allocs(sfb, hwc) > 0;
328}
329
330static void sfb_account_allocs(unsigned long num, struct hw_perf_event *hwc)
331{
332
333 num = min_t(unsigned long, num, sfb_max_limit(hwc) - SFB_ALLOC_REG(hwc));
334 if (num)
335 SFB_ALLOC_REG(hwc) += num;
336}
337
338static void sfb_init_allocs(unsigned long num, struct hw_perf_event *hwc)
339{
340 SFB_ALLOC_REG(hwc) = 0;
341 sfb_account_allocs(num, hwc);
342}
343
344static size_t event_sample_size(struct hw_perf_event *hwc)
345{
346 struct sf_raw_sample *sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(hwc);
347 size_t sample_size;
348
349
350
351
352
353 sample_size = sfr->bsdes;
354 if (SAMPL_DIAG_MODE(hwc))
355 sample_size += sfr->dsdes;
356
357 return sample_size;
358}
359
360static void deallocate_buffers(struct cpu_hw_sf *cpuhw)
361{
362 if (cpuhw->sfb.sdbt)
363 free_sampling_buffer(&cpuhw->sfb);
364}
365
366static int allocate_buffers(struct cpu_hw_sf *cpuhw, struct hw_perf_event *hwc)
367{
368 unsigned long n_sdb, freq, factor;
369 size_t sfr_size, sample_size;
370 struct sf_raw_sample *sfr;
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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);
392 if (!sfr)
393 return -ENOMEM;
394 sfr->size = sfr_size;
395 sfr->bsdes = cpuhw->qsi.bsdes;
396 sfr->dsdes = cpuhw->qsi.dsdes;
397 RAWSAMPLE_REG(hwc) = (unsigned long) sfr;
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423 sample_size = event_sample_size(hwc);
424 freq = sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc));
425 factor = 1;
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;
429
430
431
432
433
434
435
436
437
438 sfb_init_allocs(n_sdb, hwc);
439 if (sf_buffer_available(cpuhw))
440 return 0;
441
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),
446 sample_size, cpuhw);
447
448 return alloc_sampling_buffer(&cpuhw->sfb,
449 sfb_pending_allocs(&cpuhw->sfb, hwc));
450}
451
452static unsigned long min_percent(unsigned int percent, unsigned long base,
453 unsigned long min)
454{
455 return min_t(unsigned long, min, DIV_ROUND_UP(percent * base, 100));
456}
457
458static unsigned long compute_sfb_extent(unsigned long ratio, unsigned long base)
459{
460
461
462
463
464
465 if (ratio <= 5)
466 return 0;
467 if (ratio <= 25)
468 return min_percent(1, base, 1);
469 if (ratio <= 50)
470 return min_percent(1, base, 1);
471 if (ratio <= 75)
472 return min_percent(2, base, 2);
473 if (ratio <= 100)
474 return min_percent(3, base, 3);
475 if (ratio <= 250)
476 return min_percent(4, base, 4);
477
478 return min_percent(5, base, 8);
479}
480
481static void sfb_account_overflows(struct cpu_hw_sf *cpuhw,
482 struct hw_perf_event *hwc)
483{
484 unsigned long ratio, num;
485
486 if (!OVERFLOW_REG(hwc))
487 return;
488
489
490
491
492
493
494
495
496 ratio = DIV_ROUND_UP(100 * OVERFLOW_REG(hwc) * cpuhw->sfb.num_sdb,
497 sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc)));
498
499
500 num = compute_sfb_extent(ratio, cpuhw->sfb.num_sdb);
501 if (num)
502 sfb_account_allocs(num, hwc);
503
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;
507}
508
509
510
511
512
513
514
515
516
517
518
519
520static void extend_sampling_buffer(struct sf_buffer *sfb,
521 struct hw_perf_event *hwc)
522{
523 unsigned long num, num_old;
524 int rc;
525
526 num = sfb_pending_allocs(sfb, hwc);
527 if (!num)
528 return;
529 num_old = sfb->num_sdb;
530
531
532
533
534 sf_disable();
535
536
537
538
539
540
541 rc = realloc_sampling_buffer(sfb, num, GFP_ATOMIC);
542 if (rc)
543 debug_sprintf_event(sfdbg, 5, "sfb: extend: realloc "
544 "failed with rc=%i\n", rc);
545
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));
551}
552
553
554
555static atomic_t num_events;
556
557static DEFINE_MUTEX(pmc_reserve_mutex);
558
559#define PMC_INIT 0
560#define PMC_RELEASE 1
561#define PMC_FAILURE 2
562static void setup_pmc_cpu(void *flags)
563{
564 int err;
565 struct cpu_hw_sf *cpusf = this_cpu_ptr(&cpu_hw_sf);
566
567 err = 0;
568 switch (*((int *) flags)) {
569 case PMC_INIT:
570 memset(cpusf, 0, sizeof(*cpusf));
571 err = qsi(&cpusf->qsi);
572 if (err)
573 break;
574 cpusf->flags |= PMU_F_RESERVED;
575 err = sf_disable();
576 if (err)
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);
581 break;
582 case PMC_RELEASE:
583 cpusf->flags &= ~PMU_F_RESERVED;
584 err = sf_disable();
585 if (err) {
586 pr_err("Switching off the sampling facility failed "
587 "with rc=%i\n", err);
588 } else
589 deallocate_buffers(cpusf);
590 debug_sprintf_event(sfdbg, 5,
591 "setup_pmc_cpu: released: cpuhw=%p\n", cpusf);
592 break;
593 }
594 if (err)
595 *((int *) flags) |= PMC_FAILURE;
596}
597
598static void release_pmc_hardware(void)
599{
600 int flags = PMC_RELEASE;
601
602 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
603 on_each_cpu(setup_pmc_cpu, &flags, 1);
604}
605
606static int reserve_pmc_hardware(void)
607{
608 int flags = PMC_INIT;
609
610 on_each_cpu(setup_pmc_cpu, &flags, 1);
611 if (flags & PMC_FAILURE) {
612 release_pmc_hardware();
613 return -ENODEV;
614 }
615 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
616
617 return 0;
618}
619
620static void hw_perf_event_destroy(struct perf_event *event)
621{
622
623 if (RAWSAMPLE_REG(&event->hw))
624 kfree((void *) RAWSAMPLE_REG(&event->hw));
625
626
627 if (!atomic_add_unless(&num_events, -1, 1)) {
628 mutex_lock(&pmc_reserve_mutex);
629 if (atomic_dec_return(&num_events) == 0)
630 release_pmc_hardware();
631 mutex_unlock(&pmc_reserve_mutex);
632 }
633}
634
635static void hw_init_period(struct hw_perf_event *hwc, u64 period)
636{
637 hwc->sample_period = period;
638 hwc->last_period = hwc->sample_period;
639 local64_set(&hwc->period_left, hwc->sample_period);
640}
641
642static void hw_reset_registers(struct hw_perf_event *hwc,
643 unsigned long *sdbt_origin)
644{
645 struct sf_raw_sample *sfr;
646
647
648 TEAR_REG(hwc) = (unsigned long) sdbt_origin;
649
650
651 sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(hwc);
652 memset(&sfr->basic, 0, sizeof(sfr->basic));
653 memset(&sfr->diag, 0, sfr->dsdes);
654}
655
656static unsigned long hw_limit_rate(const struct hws_qsi_info_block *si,
657 unsigned long rate)
658{
659 return clamp_t(unsigned long, rate,
660 si->min_sampl_rate, si->max_sampl_rate);
661}
662
663static int __hw_perf_event_init(struct perf_event *event)
664{
665 struct cpu_hw_sf *cpuhw;
666 struct hws_qsi_info_block si;
667 struct perf_event_attr *attr = &event->attr;
668 struct hw_perf_event *hwc = &event->hw;
669 unsigned long rate;
670 int cpu, err;
671
672
673 err = 0;
674 if (!atomic_inc_not_zero(&num_events)) {
675 mutex_lock(&pmc_reserve_mutex);
676 if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
677 err = -EBUSY;
678 else
679 atomic_inc(&num_events);
680 mutex_unlock(&pmc_reserve_mutex);
681 }
682 event->destroy = hw_perf_event_destroy;
683
684 if (err)
685 goto out;
686
687
688
689
690
691
692
693
694
695
696 memset(&si, 0, sizeof(si));
697 cpuhw = NULL;
698 if (event->cpu == -1)
699 qsi(&si);
700 else {
701
702
703
704 cpuhw = &per_cpu(cpu_hw_sf, event->cpu);
705 si = cpuhw->qsi;
706 }
707
708
709
710
711
712 if (!si.as) {
713 err = -ENOENT;
714 goto out;
715 }
716
717
718 SAMPL_FLAGS(hwc) = PERF_CPUM_SF_BASIC_MODE;
719
720
721
722
723 if (attr->config == PERF_EVENT_CPUM_SF_DIAG) {
724 if (!si.ad) {
725 err = -EPERM;
726 goto out;
727 }
728 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_DIAG_MODE;
729 }
730
731
732 if (attr->config1 & PERF_CPUM_SF_FULL_BLOCKS)
733 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FULL_BLOCKS;
734
735
736
737
738
739
740 rate = 0;
741 if (attr->freq) {
742 rate = freq_to_sample_rate(&si, attr->sample_freq);
743 rate = hw_limit_rate(&si, rate);
744 attr->freq = 0;
745 attr->sample_period = rate;
746 } else {
747
748
749
750
751 rate = hw_limit_rate(&si, hwc->sample_period);
752
753
754
755
756
757
758
759 if (sample_rate_to_freq(&si, rate) >
760 sysctl_perf_event_sample_rate) {
761 err = -EINVAL;
762 debug_sprintf_event(sfdbg, 1, "Sampling rate exceeds maximum perf sample rate\n");
763 goto out;
764 }
765 }
766 SAMPL_RATE(hwc) = rate;
767 hw_init_period(hwc, SAMPL_RATE(hwc));
768
769
770 hwc->extra_reg.reg = REG_OVERFLOW;
771 OVERFLOW_REG(hwc) = 0;
772
773
774
775
776
777
778 if (cpuhw)
779
780 err = allocate_buffers(cpuhw, hwc);
781 else {
782
783
784
785 for_each_online_cpu(cpu) {
786 cpuhw = &per_cpu(cpu_hw_sf, cpu);
787 err = allocate_buffers(cpuhw, hwc);
788 if (err)
789 break;
790 }
791 }
792out:
793 return err;
794}
795
796static int cpumsf_pmu_event_init(struct perf_event *event)
797{
798 int err;
799
800
801 if (has_branch_stack(event))
802 return -EOPNOTSUPP;
803
804 switch (event->attr.type) {
805 case PERF_TYPE_RAW:
806 if ((event->attr.config != PERF_EVENT_CPUM_SF) &&
807 (event->attr.config != PERF_EVENT_CPUM_SF_DIAG))
808 return -ENOENT;
809 break;
810 case PERF_TYPE_HARDWARE:
811
812
813
814
815
816 if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES)
817 return -ENOENT;
818 if (!is_sampling_event(event))
819 return -ENOENT;
820 break;
821 default:
822 return -ENOENT;
823 }
824
825
826 if (event->cpu >= nr_cpumask_bits ||
827 (event->cpu >= 0 && !cpu_online(event->cpu)))
828 return -ENODEV;
829
830
831
832
833 if (event->attr.exclude_hv)
834 event->attr.exclude_hv = 0;
835 if (event->attr.exclude_idle)
836 event->attr.exclude_idle = 0;
837
838 err = __hw_perf_event_init(event);
839 if (unlikely(err))
840 if (event->destroy)
841 event->destroy(event);
842 return err;
843}
844
845static void cpumsf_pmu_enable(struct pmu *pmu)
846{
847 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
848 struct hw_perf_event *hwc;
849 int err;
850
851 if (cpuhw->flags & PMU_F_ENABLED)
852 return;
853
854 if (cpuhw->flags & PMU_F_ERR_MASK)
855 return;
856
857
858
859
860
861
862
863
864
865
866
867
868 if (cpuhw->event) {
869 hwc = &cpuhw->event->hw;
870
871 sfb_account_overflows(cpuhw, hwc);
872 if (sfb_has_pending_allocs(&cpuhw->sfb, hwc))
873 extend_sampling_buffer(&cpuhw->sfb, hwc);
874 }
875
876
877 cpuhw->flags |= PMU_F_ENABLED;
878 barrier();
879
880 err = lsctl(&cpuhw->lsctl);
881 if (err) {
882 cpuhw->flags &= ~PMU_F_ENABLED;
883 pr_err("Loading sampling controls failed: op=%i err=%i\n",
884 1, err);
885 return;
886 }
887
888 debug_sprintf_event(sfdbg, 6, "pmu_enable: es=%i cs=%i ed=%i cd=%i "
889 "tear=%p dear=%p\n", cpuhw->lsctl.es, cpuhw->lsctl.cs,
890 cpuhw->lsctl.ed, cpuhw->lsctl.cd,
891 (void *) cpuhw->lsctl.tear, (void *) cpuhw->lsctl.dear);
892}
893
894static void cpumsf_pmu_disable(struct pmu *pmu)
895{
896 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
897 struct hws_lsctl_request_block inactive;
898 struct hws_qsi_info_block si;
899 int err;
900
901 if (!(cpuhw->flags & PMU_F_ENABLED))
902 return;
903
904 if (cpuhw->flags & PMU_F_ERR_MASK)
905 return;
906
907
908 inactive = cpuhw->lsctl;
909 inactive.cs = 0;
910 inactive.cd = 0;
911
912 err = lsctl(&inactive);
913 if (err) {
914 pr_err("Loading sampling controls failed: op=%i err=%i\n",
915 2, err);
916 return;
917 }
918
919
920 if (!qsi(&si)) {
921
922
923
924
925
926 if (si.es) {
927 cpuhw->lsctl.tear = si.tear;
928 cpuhw->lsctl.dear = si.dear;
929 }
930 } else
931 debug_sprintf_event(sfdbg, 3, "cpumsf_pmu_disable: "
932 "qsi() failed with err=%i\n", err);
933
934 cpuhw->flags &= ~PMU_F_ENABLED;
935}
936
937
938
939
940
941
942
943
944
945
946static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
947 struct perf_sf_sde_regs *sde_regs)
948{
949 if (event->attr.exclude_user && user_mode(regs))
950 return 1;
951 if (event->attr.exclude_kernel && !user_mode(regs))
952 return 1;
953 if (event->attr.exclude_guest && sde_regs->in_guest)
954 return 1;
955 if (event->attr.exclude_host && !sde_regs->in_guest)
956 return 1;
957 return 0;
958}
959
960
961
962
963
964
965
966
967
968
969
970
971static int perf_push_sample(struct perf_event *event, struct sf_raw_sample *sfr)
972{
973 int overflow;
974 struct pt_regs regs;
975 struct perf_sf_sde_regs *sde_regs;
976 struct perf_sample_data data;
977 struct perf_raw_record raw = {
978 .frag = {
979 .size = sfr->size,
980 .data = sfr,
981 },
982 };
983
984
985 perf_sample_data_init(&data, 0, event->hw.last_period);
986 data.raw = &raw;
987
988
989
990
991
992
993 memset(®s, 0, sizeof(regs));
994 regs.int_code = 0x1407;
995 regs.int_parm = CPU_MF_INT_SF_PRA;
996 sde_regs = (struct perf_sf_sde_regs *) ®s.int_parm_long;
997
998 regs.psw.addr = sfr->basic.ia;
999 if (sfr->basic.T)
1000 regs.psw.mask |= PSW_MASK_DAT;
1001 if (sfr->basic.W)
1002 regs.psw.mask |= PSW_MASK_WAIT;
1003 if (sfr->basic.P)
1004 regs.psw.mask |= PSW_MASK_PSTATE;
1005 switch (sfr->basic.AS) {
1006 case 0x0:
1007 regs.psw.mask |= PSW_ASC_PRIMARY;
1008 break;
1009 case 0x1:
1010 regs.psw.mask |= PSW_ASC_ACCREG;
1011 break;
1012 case 0x2:
1013 regs.psw.mask |= PSW_ASC_SECONDARY;
1014 break;
1015 case 0x3:
1016 regs.psw.mask |= PSW_ASC_HOME;
1017 break;
1018 }
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029 if (sfr->basic.gpp || sfr->basic.prim_asn != (u16) sfr->basic.hpp)
1030 sde_regs->in_guest = 1;
1031
1032 overflow = 0;
1033 if (perf_exclude_event(event, ®s, sde_regs))
1034 goto out;
1035 if (perf_event_overflow(event, &data, ®s)) {
1036 overflow = 1;
1037 event->pmu->stop(event, 0);
1038 }
1039 perf_event_update_userpage(event);
1040out:
1041 return overflow;
1042}
1043
1044static void perf_event_count_update(struct perf_event *event, u64 count)
1045{
1046 local64_add(count, &event->count);
1047}
1048
1049static int sample_format_is_valid(struct hws_combined_entry *sample,
1050 unsigned int flags)
1051{
1052 if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
1053
1054
1055
1056 if (sample->basic.def != 0x0001)
1057 return 0;
1058 if (flags & PERF_CPUM_SF_DIAG_MODE)
1059
1060
1061
1062
1063 if (sample->diag.def < 0x8001)
1064 return 0;
1065 return 1;
1066}
1067
1068static int sample_is_consistent(struct hws_combined_entry *sample,
1069 unsigned long flags)
1070{
1071
1072
1073
1074
1075
1076 if (unlikely(!(flags & PERF_CPUM_SF_BASIC_MODE)))
1077 return 0;
1078
1079
1080
1081
1082
1083 if (sample->basic.I || sample->basic.W)
1084 return 0;
1085 return 1;
1086}
1087
1088static void reset_sample_slot(struct hws_combined_entry *sample,
1089 unsigned long flags)
1090{
1091 if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
1092 sample->basic.def = 0;
1093 if (flags & PERF_CPUM_SF_DIAG_MODE)
1094 sample->diag.def = 0;
1095}
1096
1097static void sfr_store_sample(struct sf_raw_sample *sfr,
1098 struct hws_combined_entry *sample)
1099{
1100 if (likely(sfr->format & PERF_CPUM_SF_BASIC_MODE))
1101 sfr->basic = sample->basic;
1102 if (sfr->format & PERF_CPUM_SF_DIAG_MODE)
1103 memcpy(&sfr->diag, &sample->diag, sfr->dsdes);
1104}
1105
1106static void debug_sample_entry(struct hws_combined_entry *sample,
1107 struct hws_trailer_entry *te,
1108 unsigned long flags)
1109{
1110 debug_sprintf_event(sfdbg, 4, "hw_collect_samples: Found unknown "
1111 "sampling data entry: te->f=%i basic.def=%04x (%p)"
1112 " diag.def=%04x (%p)\n", te->f,
1113 sample->basic.def, &sample->basic,
1114 (flags & PERF_CPUM_SF_DIAG_MODE)
1115 ? sample->diag.def : 0xFFFF,
1116 (flags & PERF_CPUM_SF_DIAG_MODE)
1117 ? &sample->diag : NULL);
1118}
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140static void hw_collect_samples(struct perf_event *event, unsigned long *sdbt,
1141 unsigned long long *overflow)
1142{
1143 unsigned long flags = SAMPL_FLAGS(&event->hw);
1144 struct hws_combined_entry *sample;
1145 struct hws_trailer_entry *te;
1146 struct sf_raw_sample *sfr;
1147 size_t sample_size;
1148
1149
1150 sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(&event->hw);
1151 sfr->format = flags & PERF_CPUM_SF_MODE_MASK;
1152
1153 sample_size = event_sample_size(&event->hw);
1154 te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
1155 sample = (struct hws_combined_entry *) *sdbt;
1156 while ((unsigned long *) sample < (unsigned long *) te) {
1157
1158 if (!sample->basic.def)
1159 break;
1160
1161
1162 perf_event_count_update(event, SAMPL_RATE(&event->hw));
1163
1164
1165 if (sample_format_is_valid(sample, flags)) {
1166
1167
1168
1169
1170 if (!*overflow) {
1171 if (sample_is_consistent(sample, flags)) {
1172
1173 sfr_store_sample(sfr, sample);
1174 *overflow = perf_push_sample(event, sfr);
1175 }
1176 } else
1177
1178 *overflow += 1;
1179 } else {
1180 debug_sample_entry(sample, te, flags);
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191 if (!te->f)
1192 break;
1193 }
1194
1195
1196 reset_sample_slot(sample, flags);
1197 sample += sample_size;
1198 }
1199}
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215static void hw_perf_event_update(struct perf_event *event, int flush_all)
1216{
1217 struct hw_perf_event *hwc = &event->hw;
1218 struct hws_trailer_entry *te;
1219 unsigned long *sdbt;
1220 unsigned long long event_overflow, sampl_overflow, num_sdb, te_flags;
1221 int done;
1222
1223 if (flush_all && SDB_FULL_BLOCKS(hwc))
1224 flush_all = 0;
1225
1226 sdbt = (unsigned long *) TEAR_REG(hwc);
1227 done = event_overflow = sampl_overflow = num_sdb = 0;
1228 while (!done) {
1229
1230 te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
1231
1232
1233 if (!te->f) {
1234 done = 1;
1235 if (!flush_all)
1236 break;
1237 }
1238
1239
1240 if (te->overflow)
1241
1242
1243
1244
1245 sampl_overflow += te->overflow;
1246
1247
1248 debug_sprintf_event(sfdbg, 6, "hw_perf_event_update: sdbt=%p "
1249 "overflow=%llu timestamp=0x%llx\n",
1250 sdbt, te->overflow,
1251 (te->f) ? trailer_timestamp(te) : 0ULL);
1252
1253
1254
1255
1256
1257 hw_collect_samples(event, sdbt, &event_overflow);
1258 num_sdb++;
1259
1260
1261 do {
1262 te_flags = te->flags & ~SDB_TE_BUFFER_FULL_MASK;
1263 te_flags |= SDB_TE_ALERT_REQ_MASK;
1264 } while (!cmpxchg_double(&te->flags, &te->overflow,
1265 te->flags, te->overflow,
1266 te_flags, 0ULL));
1267
1268
1269 sdbt++;
1270 if (is_link_entry(sdbt))
1271 sdbt = get_next_sdbt(sdbt);
1272
1273
1274 TEAR_REG(hwc) = (unsigned long) sdbt;
1275
1276
1277
1278
1279 if (flush_all && done)
1280 break;
1281
1282
1283
1284
1285 if (event_overflow)
1286 flush_all = 1;
1287 }
1288
1289
1290 if (sampl_overflow)
1291 OVERFLOW_REG(hwc) = DIV_ROUND_UP(OVERFLOW_REG(hwc) +
1292 sampl_overflow, 1 + num_sdb);
1293 if (sampl_overflow || event_overflow)
1294 debug_sprintf_event(sfdbg, 4, "hw_perf_event_update: "
1295 "overflow stats: sample=%llu event=%llu\n",
1296 sampl_overflow, event_overflow);
1297}
1298
1299static void cpumsf_pmu_read(struct perf_event *event)
1300{
1301
1302}
1303
1304
1305
1306
1307static void cpumsf_pmu_start(struct perf_event *event, int flags)
1308{
1309 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
1310
1311 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
1312 return;
1313
1314 if (flags & PERF_EF_RELOAD)
1315 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1316
1317 perf_pmu_disable(event->pmu);
1318 event->hw.state = 0;
1319 cpuhw->lsctl.cs = 1;
1320 if (SAMPL_DIAG_MODE(&event->hw))
1321 cpuhw->lsctl.cd = 1;
1322 perf_pmu_enable(event->pmu);
1323}
1324
1325
1326
1327
1328static void cpumsf_pmu_stop(struct perf_event *event, int flags)
1329{
1330 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
1331
1332 if (event->hw.state & PERF_HES_STOPPED)
1333 return;
1334
1335 perf_pmu_disable(event->pmu);
1336 cpuhw->lsctl.cs = 0;
1337 cpuhw->lsctl.cd = 0;
1338 event->hw.state |= PERF_HES_STOPPED;
1339
1340 if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
1341 hw_perf_event_update(event, 1);
1342 event->hw.state |= PERF_HES_UPTODATE;
1343 }
1344 perf_pmu_enable(event->pmu);
1345}
1346
1347static int cpumsf_pmu_add(struct perf_event *event, int flags)
1348{
1349 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
1350 int err;
1351
1352 if (cpuhw->flags & PMU_F_IN_USE)
1353 return -EAGAIN;
1354
1355 if (!cpuhw->sfb.sdbt)
1356 return -EINVAL;
1357
1358 err = 0;
1359 perf_pmu_disable(event->pmu);
1360
1361 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1362
1363
1364
1365
1366
1367
1368 cpuhw->lsctl.s = 0;
1369 cpuhw->lsctl.h = 1;
1370 cpuhw->lsctl.tear = (unsigned long) cpuhw->sfb.sdbt;
1371 cpuhw->lsctl.dear = *(unsigned long *) cpuhw->sfb.sdbt;
1372 cpuhw->lsctl.interval = SAMPL_RATE(&event->hw);
1373 hw_reset_registers(&event->hw, cpuhw->sfb.sdbt);
1374
1375
1376
1377 if (WARN_ON_ONCE(cpuhw->lsctl.es == 1 || cpuhw->lsctl.ed == 1)) {
1378 err = -EAGAIN;
1379 goto out;
1380 }
1381 cpuhw->lsctl.es = 1;
1382 if (SAMPL_DIAG_MODE(&event->hw))
1383 cpuhw->lsctl.ed = 1;
1384
1385
1386 cpuhw->event = event;
1387 cpuhw->flags |= PMU_F_IN_USE;
1388
1389 if (flags & PERF_EF_START)
1390 cpumsf_pmu_start(event, PERF_EF_RELOAD);
1391out:
1392 perf_event_update_userpage(event);
1393 perf_pmu_enable(event->pmu);
1394 return err;
1395}
1396
1397static void cpumsf_pmu_del(struct perf_event *event, int flags)
1398{
1399 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
1400
1401 perf_pmu_disable(event->pmu);
1402 cpumsf_pmu_stop(event, PERF_EF_UPDATE);
1403
1404 cpuhw->lsctl.es = 0;
1405 cpuhw->lsctl.ed = 0;
1406 cpuhw->flags &= ~PMU_F_IN_USE;
1407 cpuhw->event = NULL;
1408
1409 perf_event_update_userpage(event);
1410 perf_pmu_enable(event->pmu);
1411}
1412
1413CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC, PERF_EVENT_CPUM_SF);
1414CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC_DIAG, PERF_EVENT_CPUM_SF_DIAG);
1415
1416static struct attribute *cpumsf_pmu_events_attr[] = {
1417 CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC),
1418 NULL,
1419 NULL,
1420};
1421
1422PMU_FORMAT_ATTR(event, "config:0-63");
1423
1424static struct attribute *cpumsf_pmu_format_attr[] = {
1425 &format_attr_event.attr,
1426 NULL,
1427};
1428
1429static struct attribute_group cpumsf_pmu_events_group = {
1430 .name = "events",
1431 .attrs = cpumsf_pmu_events_attr,
1432};
1433static struct attribute_group cpumsf_pmu_format_group = {
1434 .name = "format",
1435 .attrs = cpumsf_pmu_format_attr,
1436};
1437static const struct attribute_group *cpumsf_pmu_attr_groups[] = {
1438 &cpumsf_pmu_events_group,
1439 &cpumsf_pmu_format_group,
1440 NULL,
1441};
1442
1443static struct pmu cpumf_sampling = {
1444 .pmu_enable = cpumsf_pmu_enable,
1445 .pmu_disable = cpumsf_pmu_disable,
1446
1447 .event_init = cpumsf_pmu_event_init,
1448 .add = cpumsf_pmu_add,
1449 .del = cpumsf_pmu_del,
1450
1451 .start = cpumsf_pmu_start,
1452 .stop = cpumsf_pmu_stop,
1453 .read = cpumsf_pmu_read,
1454
1455 .attr_groups = cpumsf_pmu_attr_groups,
1456};
1457
1458static void cpumf_measurement_alert(struct ext_code ext_code,
1459 unsigned int alert, unsigned long unused)
1460{
1461 struct cpu_hw_sf *cpuhw;
1462
1463 if (!(alert & CPU_MF_INT_SF_MASK))
1464 return;
1465 inc_irq_stat(IRQEXT_CMS);
1466 cpuhw = this_cpu_ptr(&cpu_hw_sf);
1467
1468
1469
1470 if (!(cpuhw->flags & PMU_F_RESERVED))
1471 return;
1472
1473
1474
1475
1476
1477 if (alert & CPU_MF_INT_SF_PRA) {
1478 if (cpuhw->flags & PMU_F_IN_USE)
1479 hw_perf_event_update(cpuhw->event, 0);
1480 else
1481 WARN_ON_ONCE(!(cpuhw->flags & PMU_F_IN_USE));
1482 }
1483
1484
1485 if (alert != CPU_MF_INT_SF_PRA)
1486 debug_sprintf_event(sfdbg, 6, "measurement alert: 0x%x\n", alert);
1487
1488
1489 if (alert & CPU_MF_INT_SF_SACA)
1490 qsi(&cpuhw->qsi);
1491
1492
1493 if (alert & CPU_MF_INT_SF_LSDA) {
1494 pr_err("Sample data was lost\n");
1495 cpuhw->flags |= PMU_F_ERR_LSDA;
1496 sf_disable();
1497 }
1498
1499
1500 if (alert & (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE)) {
1501 pr_err("A sampling buffer entry is incorrect (alert=0x%x)\n",
1502 alert);
1503 cpuhw->flags |= PMU_F_ERR_IBE;
1504 sf_disable();
1505 }
1506}
1507static int cpusf_pmu_setup(unsigned int cpu, int flags)
1508{
1509
1510
1511
1512 if (!atomic_read(&num_events))
1513 return 0;
1514
1515 local_irq_disable();
1516 setup_pmc_cpu(&flags);
1517 local_irq_enable();
1518 return 0;
1519}
1520
1521static int s390_pmu_sf_online_cpu(unsigned int cpu)
1522{
1523 return cpusf_pmu_setup(cpu, PMC_INIT);
1524}
1525
1526static int s390_pmu_sf_offline_cpu(unsigned int cpu)
1527{
1528 return cpusf_pmu_setup(cpu, PMC_RELEASE);
1529}
1530
1531static int param_get_sfb_size(char *buffer, const struct kernel_param *kp)
1532{
1533 if (!cpum_sf_avail())
1534 return -ENODEV;
1535 return sprintf(buffer, "%lu,%lu", CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
1536}
1537
1538static int param_set_sfb_size(const char *val, const struct kernel_param *kp)
1539{
1540 int rc;
1541 unsigned long min, max;
1542
1543 if (!cpum_sf_avail())
1544 return -ENODEV;
1545 if (!val || !strlen(val))
1546 return -EINVAL;
1547
1548
1549 min = CPUM_SF_MIN_SDB;
1550 max = CPUM_SF_MAX_SDB;
1551 if (strchr(val, ','))
1552 rc = (sscanf(val, "%lu,%lu", &min, &max) == 2) ? 0 : -EINVAL;
1553 else
1554 rc = kstrtoul(val, 10, &max);
1555
1556 if (min < 2 || min >= max || max > get_num_physpages())
1557 rc = -EINVAL;
1558 if (rc)
1559 return rc;
1560
1561 sfb_set_limits(min, max);
1562 pr_info("The sampling buffer limits have changed to: "
1563 "min=%lu max=%lu (diag=x%lu)\n",
1564 CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB, CPUM_SF_SDB_DIAG_FACTOR);
1565 return 0;
1566}
1567
1568#define param_check_sfb_size(name, p) __param_check(name, p, void)
1569static const struct kernel_param_ops param_ops_sfb_size = {
1570 .set = param_set_sfb_size,
1571 .get = param_get_sfb_size,
1572};
1573
1574#define RS_INIT_FAILURE_QSI 0x0001
1575#define RS_INIT_FAILURE_BSDES 0x0002
1576#define RS_INIT_FAILURE_ALRT 0x0003
1577#define RS_INIT_FAILURE_PERF 0x0004
1578static void __init pr_cpumsf_err(unsigned int reason)
1579{
1580 pr_err("Sampling facility support for perf is not available: "
1581 "reason=%04x\n", reason);
1582}
1583
1584static int __init init_cpum_sampling_pmu(void)
1585{
1586 struct hws_qsi_info_block si;
1587 int err;
1588
1589 if (!cpum_sf_avail())
1590 return -ENODEV;
1591
1592 memset(&si, 0, sizeof(si));
1593 if (qsi(&si)) {
1594 pr_cpumsf_err(RS_INIT_FAILURE_QSI);
1595 return -ENODEV;
1596 }
1597
1598 if (si.bsdes != sizeof(struct hws_basic_entry)) {
1599 pr_cpumsf_err(RS_INIT_FAILURE_BSDES);
1600 return -EINVAL;
1601 }
1602
1603 if (si.ad) {
1604 sfb_set_limits(CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
1605 cpumsf_pmu_events_attr[1] =
1606 CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC_DIAG);
1607 }
1608
1609 sfdbg = debug_register(KMSG_COMPONENT, 2, 1, 80);
1610 if (!sfdbg)
1611 pr_err("Registering for s390dbf failed\n");
1612 debug_register_view(sfdbg, &debug_sprintf_view);
1613
1614 err = register_external_irq(EXT_IRQ_MEASURE_ALERT,
1615 cpumf_measurement_alert);
1616 if (err) {
1617 pr_cpumsf_err(RS_INIT_FAILURE_ALRT);
1618 goto out;
1619 }
1620
1621 err = perf_pmu_register(&cpumf_sampling, "cpum_sf", PERF_TYPE_RAW);
1622 if (err) {
1623 pr_cpumsf_err(RS_INIT_FAILURE_PERF);
1624 unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
1625 cpumf_measurement_alert);
1626 goto out;
1627 }
1628
1629 cpuhp_setup_state(CPUHP_AP_PERF_S390_SF_ONLINE, "AP_PERF_S390_SF_ONLINE",
1630 s390_pmu_sf_online_cpu, s390_pmu_sf_offline_cpu);
1631out:
1632 return err;
1633}
1634arch_initcall(init_cpum_sampling_pmu);
1635core_param(cpum_sfb_size, CPUM_SF_MAX_SDB, sfb_size, 0640);
1636