2 * arch/s390/oprofile/hwsampler.c
4 * Copyright IBM Corp. 2010
5 * Author: Heinz Graalfs <graalfs@de.ibm.com>
8 #include <linux/kernel_stat.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/smp.h>
12 #include <linux/errno.h>
13 #include <linux/workqueue.h>
14 #include <linux/interrupt.h>
15 #include <linux/notifier.h>
16 #include <linux/cpu.h>
17 #include <linux/semaphore.h>
18 #include <linux/oom.h>
19 #include <linux/oprofile.h>
21 #include <asm/lowcore.h>
24 #include "hwsampler.h"
25 #include "op_counter.h"
27 #define MAX_NUM_SDB 511
30 #define ALERT_REQ_MASK 0x4000000000000000ul
31 #define BUFFER_FULL_MASK 0x8000000000000000ul
33 #define EI_IEA (1 << 31) /* invalid entry address */
34 #define EI_ISE (1 << 30) /* incorrect SDBT entry */
35 #define EI_PRA (1 << 29) /* program request alert */
36 #define EI_SACA (1 << 23) /* sampler authorization change alert */
37 #define EI_LSDA (1 << 22) /* loss of sample data alert */
39 DECLARE_PER_CPU(struct hws_cpu_buffer
, sampler_cpu_buffer
);
41 struct hws_execute_parms
{
46 DEFINE_PER_CPU(struct hws_cpu_buffer
, sampler_cpu_buffer
);
47 EXPORT_PER_CPU_SYMBOL(sampler_cpu_buffer
);
49 static DEFINE_MUTEX(hws_sem
);
50 static DEFINE_MUTEX(hws_sem_oom
);
52 static unsigned char hws_flush_all
;
53 static unsigned int hws_oom
;
54 static struct workqueue_struct
*hws_wq
;
56 static unsigned int hws_state
;
64 /* set to 1 if called by kernel during memory allocation */
65 static unsigned char oom_killer_was_active
;
66 /* size of SDBT and SDB as of allocate API */
67 static unsigned long num_sdbt
= 100;
68 static unsigned long num_sdb
= 511;
69 /* sampling interval (machine cycles) */
70 static unsigned long interval
;
72 static unsigned long min_sampler_rate
;
73 static unsigned long max_sampler_rate
;
75 static int ssctl(void *buffer
)
79 /* set in order to detect a program check */
83 "0: .insn s,0xB2870000,0(%1)\n"
87 EX_TABLE(0b
, 2b
) EX_TABLE(1b
, 2b
)
88 : "+d" (cc
), "+a" (buffer
)
89 : "m" (*((struct hws_ssctl_request_block
*)buffer
))
92 return cc
? -EINVAL
: 0 ;
95 static int qsi(void *buffer
)
101 "0: .insn s,0xB2860000,0(%1)\n"
104 EX_TABLE(0b
, 2b
) EX_TABLE(1b
, 2b
)
105 : "=d" (cc
), "+a" (buffer
)
106 : "m" (*((struct hws_qsi_info_block
*)buffer
))
109 return cc
? -EINVAL
: 0;
112 static void execute_qsi(void *parms
)
114 struct hws_execute_parms
*ep
= parms
;
116 ep
->rc
= qsi(ep
->buffer
);
119 static void execute_ssctl(void *parms
)
121 struct hws_execute_parms
*ep
= parms
;
123 ep
->rc
= ssctl(ep
->buffer
);
126 static int smp_ctl_ssctl_stop(int cpu
)
129 struct hws_execute_parms ep
;
130 struct hws_cpu_buffer
*cb
;
132 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
137 ep
.buffer
= &cb
->ssctl
;
138 smp_call_function_single(cpu
, execute_ssctl
, &ep
, 1);
141 printk(KERN_ERR
"hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu
);
145 ep
.buffer
= &cb
->qsi
;
146 smp_call_function_single(cpu
, execute_qsi
, &ep
, 1);
148 if (cb
->qsi
.es
|| cb
->qsi
.cs
) {
149 printk(KERN_EMERG
"CPUMF sampling did not stop properly.\n");
156 static int smp_ctl_ssctl_deactivate(int cpu
)
159 struct hws_execute_parms ep
;
160 struct hws_cpu_buffer
*cb
;
162 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
167 ep
.buffer
= &cb
->ssctl
;
168 smp_call_function_single(cpu
, execute_ssctl
, &ep
, 1);
171 printk(KERN_ERR
"hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu
);
173 ep
.buffer
= &cb
->qsi
;
174 smp_call_function_single(cpu
, execute_qsi
, &ep
, 1);
177 printk(KERN_EMERG
"CPUMF sampling was not set inactive.\n");
182 static int smp_ctl_ssctl_enable_activate(int cpu
, unsigned long interval
)
185 struct hws_execute_parms ep
;
186 struct hws_cpu_buffer
*cb
;
188 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
191 cb
->ssctl
.tear
= cb
->first_sdbt
;
192 cb
->ssctl
.dear
= *(unsigned long *) cb
->first_sdbt
;
193 cb
->ssctl
.interval
= interval
;
197 ep
.buffer
= &cb
->ssctl
;
198 smp_call_function_single(cpu
, execute_ssctl
, &ep
, 1);
201 printk(KERN_ERR
"hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu
);
203 ep
.buffer
= &cb
->qsi
;
204 smp_call_function_single(cpu
, execute_qsi
, &ep
, 1);
206 printk(KERN_ERR
"hwsampler: CPU %d CPUMF QSI failed.\n", cpu
);
211 static int smp_ctl_qsi(int cpu
)
213 struct hws_execute_parms ep
;
214 struct hws_cpu_buffer
*cb
;
216 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
218 ep
.buffer
= &cb
->qsi
;
219 smp_call_function_single(cpu
, execute_qsi
, &ep
, 1);
224 static inline unsigned long *trailer_entry_ptr(unsigned long v
)
230 ret
-= sizeof(struct hws_trailer_entry
);
232 return (unsigned long *) ret
;
235 /* prototypes for external interrupt handler and worker */
236 static void hws_ext_handler(unsigned int ext_int_code
,
237 unsigned int param32
, unsigned long param64
);
239 static void worker(struct work_struct
*work
);
241 static void add_samples_to_oprofile(unsigned cpu
, unsigned long *,
242 unsigned long *dear
);
244 static void init_all_cpu_buffers(void)
247 struct hws_cpu_buffer
*cb
;
249 for_each_online_cpu(cpu
) {
250 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
251 memset(cb
, 0, sizeof(struct hws_cpu_buffer
));
255 static int is_link_entry(unsigned long *s
)
257 return *s
& 0x1ul
? 1 : 0;
260 static unsigned long *get_next_sdbt(unsigned long *s
)
262 return (unsigned long *) (*s
& ~0x1ul
);
265 static int prepare_cpu_buffers(void)
269 struct hws_cpu_buffer
*cb
;
272 for_each_online_cpu(cpu
) {
273 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
274 atomic_set(&cb
->ext_params
, 0);
275 cb
->worker_entry
= 0;
276 cb
->sample_overflow
= 0;
278 cb
->incorrect_sdbt_entry
= 0;
279 cb
->invalid_entry_address
= 0;
280 cb
->loss_of_sample_data
= 0;
281 cb
->sample_auth_change_alert
= 0;
291 * allocate_sdbt() - allocate sampler memory
292 * @cpu: the cpu for which sampler memory is allocated
294 * A 4K page is allocated for each requested SDBT.
295 * A maximum of 511 4K pages are allocated for the SDBs in each of the SDBTs.
296 * Set ALERT_REQ mask in each SDBs trailer.
297 * Returns zero if successful, <0 otherwise.
299 static int allocate_sdbt(int cpu
)
305 unsigned long *trailer
;
306 struct hws_cpu_buffer
*cb
;
308 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
316 for (j
= 0; j
< num_sdbt
; j
++) {
317 sdbt
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
319 mutex_lock(&hws_sem_oom
);
320 /* OOM killer might have been activated */
322 if (oom_killer_was_active
|| !sdbt
) {
324 free_page((unsigned long)sdbt
);
326 goto allocate_sdbt_error
;
328 if (cb
->first_sdbt
== 0)
329 cb
->first_sdbt
= (unsigned long)sdbt
;
331 /* link current page to tail of chain */
333 *tail
= (unsigned long)(void *)sdbt
+ 1;
335 mutex_unlock(&hws_sem_oom
);
337 for (k
= 0; k
< num_sdb
; k
++) {
338 /* get and set SDB page */
339 sdb
= get_zeroed_page(GFP_KERNEL
);
341 mutex_lock(&hws_sem_oom
);
342 /* OOM killer might have been activated */
344 if (oom_killer_was_active
|| !sdb
) {
348 goto allocate_sdbt_error
;
351 trailer
= trailer_entry_ptr(*sdbt
);
352 *trailer
= ALERT_REQ_MASK
;
354 mutex_unlock(&hws_sem_oom
);
358 mutex_lock(&hws_sem_oom
);
359 if (oom_killer_was_active
)
360 goto allocate_sdbt_error
;
364 *tail
= (unsigned long)
365 ((void *)cb
->first_sdbt
) + 1;
368 mutex_unlock(&hws_sem_oom
);
373 goto allocate_sdbt_exit
;
377 * deallocate_sdbt() - deallocate all sampler memory
379 * For each online CPU all SDBT trees are deallocated.
380 * Returns the number of freed pages.
382 static int deallocate_sdbt(void)
389 for_each_online_cpu(cpu
) {
393 struct hws_cpu_buffer
*cb
;
395 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
400 sdbt
= cb
->first_sdbt
;
401 curr
= (unsigned long *) sdbt
;
404 /* we'll free the SDBT after all SDBs are processed... */
409 /* watch for link entry reset if found */
410 if (is_link_entry(curr
)) {
411 curr
= get_next_sdbt(curr
);
415 /* we are done if we reach the start */
416 if ((unsigned long) curr
== start
)
419 sdbt
= (unsigned long) curr
;
421 /* process SDB pointer */
434 static int start_sampling(int cpu
)
437 struct hws_cpu_buffer
*cb
;
439 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
440 rc
= smp_ctl_ssctl_enable_activate(cpu
, interval
);
442 printk(KERN_INFO
"hwsampler: CPU %d ssctl failed.\n", cpu
);
448 printk(KERN_INFO
"hwsampler: CPU %d ssctl not enabled.\n", cpu
);
453 printk(KERN_INFO
"hwsampler: CPU %d ssctl not active.\n", cpu
);
458 "hwsampler: CPU %d, CPUMF Sampling started, interval %lu.\n",
467 static int stop_sampling(int cpu
)
471 struct hws_cpu_buffer
*cb
;
473 rc
= smp_ctl_qsi(cpu
);
476 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
477 if (!rc
&& !cb
->qsi
.es
)
478 printk(KERN_INFO
"hwsampler: CPU %d, already stopped.\n", cpu
);
480 rc
= smp_ctl_ssctl_stop(cpu
);
482 printk(KERN_INFO
"hwsampler: CPU %d, ssctl stop error %d.\n",
487 printk(KERN_INFO
"hwsampler: CPU %d, CPUMF Sampling stopped.\n", cpu
);
492 printk(KERN_ERR
"hwsampler: CPU %d CPUMF Request alert,"
493 " count=%lu.\n", cpu
, v
);
495 v
= cb
->loss_of_sample_data
;
497 printk(KERN_ERR
"hwsampler: CPU %d CPUMF Loss of sample data,"
498 " count=%lu.\n", cpu
, v
);
500 v
= cb
->invalid_entry_address
;
502 printk(KERN_ERR
"hwsampler: CPU %d CPUMF Invalid entry address,"
503 " count=%lu.\n", cpu
, v
);
505 v
= cb
->incorrect_sdbt_entry
;
508 "hwsampler: CPU %d CPUMF Incorrect SDBT address,"
509 " count=%lu.\n", cpu
, v
);
511 v
= cb
->sample_auth_change_alert
;
514 "hwsampler: CPU %d CPUMF Sample authorization change,"
515 " count=%lu.\n", cpu
, v
);
520 static int check_hardware_prerequisites(void)
522 if (!test_facility(68))
527 * hws_oom_callback() - the OOM callback function
529 * In case the callback is invoked during memory allocation for the
530 * hw sampler, all obtained memory is deallocated and a flag is set
531 * so main sampler memory allocation can exit with a failure code.
532 * In case the callback is invoked during sampling the hw sampler
533 * is deactivated for all CPUs.
535 static int hws_oom_callback(struct notifier_block
*nfb
,
536 unsigned long dummy
, void *parm
)
538 unsigned long *freed
;
540 struct hws_cpu_buffer
*cb
;
544 mutex_lock(&hws_sem_oom
);
546 if (hws_state
== HWS_DEALLOCATED
) {
547 /* during memory allocation */
548 if (oom_killer_was_active
== 0) {
549 oom_killer_was_active
= 1;
550 *freed
+= deallocate_sdbt();
555 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
558 for_each_online_cpu(i
) {
559 smp_ctl_ssctl_deactivate(i
);
565 "hwsampler: CPU %d, OOM notify during CPUMF Sampling.\n",
570 mutex_unlock(&hws_sem_oom
);
575 static struct notifier_block hws_oom_notifier
= {
576 .notifier_call
= hws_oom_callback
579 static int hws_cpu_callback(struct notifier_block
*nfb
,
580 unsigned long action
, void *hcpu
)
582 /* We do not have sampler space available for all possible CPUs.
583 All CPUs should be online when hw sampling is activated. */
584 return (hws_state
<= HWS_DEALLOCATED
) ? NOTIFY_OK
: NOTIFY_BAD
;
587 static struct notifier_block hws_cpu_notifier
= {
588 .notifier_call
= hws_cpu_callback
592 * hwsampler_deactivate() - set hardware sampling temporarily inactive
593 * @cpu: specifies the CPU to be set inactive.
595 * Returns 0 on success, !0 on failure.
597 int hwsampler_deactivate(unsigned int cpu
)
600 * Deactivate hw sampling temporarily and flush the buffer
601 * by pushing all the pending samples to oprofile buffer.
603 * This function can be called under one of the following conditions:
604 * Memory unmap, task is exiting.
607 struct hws_cpu_buffer
*cb
;
610 mutex_lock(&hws_sem
);
612 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
613 if (hws_state
== HWS_STARTED
) {
614 rc
= smp_ctl_qsi(cpu
);
617 rc
= smp_ctl_ssctl_deactivate(cpu
);
620 "hwsampler: CPU %d, CPUMF Deactivation failed.\n", cpu
);
622 hws_state
= HWS_STOPPING
;
625 /* Add work to queue to read pending samples.*/
626 queue_work_on(cpu
, hws_wq
, &cb
->worker
);
630 mutex_unlock(&hws_sem
);
633 flush_workqueue(hws_wq
);
639 * hwsampler_activate() - activate/resume hardware sampling which was deactivated
640 * @cpu: specifies the CPU to be set active.
642 * Returns 0 on success, !0 on failure.
644 int hwsampler_activate(unsigned int cpu
)
647 * Re-activate hw sampling. This should be called in pair with
648 * hwsampler_deactivate().
651 struct hws_cpu_buffer
*cb
;
654 mutex_lock(&hws_sem
);
656 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
657 if (hws_state
== HWS_STARTED
) {
658 rc
= smp_ctl_qsi(cpu
);
662 rc
= smp_ctl_ssctl_enable_activate(cpu
, interval
);
665 "CPU %d, CPUMF activate sampling failed.\n",
671 mutex_unlock(&hws_sem
);
676 static void hws_ext_handler(unsigned int ext_int_code
,
677 unsigned int param32
, unsigned long param64
)
679 struct hws_cpu_buffer
*cb
;
681 kstat_cpu(smp_processor_id()).irqs
[EXTINT_CPM
]++;
682 cb
= &__get_cpu_var(sampler_cpu_buffer
);
683 atomic_xchg(&cb
->ext_params
, atomic_read(&cb
->ext_params
) | param32
);
685 queue_work(hws_wq
, &cb
->worker
);
688 static int check_qsi_on_setup(void)
692 struct hws_cpu_buffer
*cb
;
694 for_each_online_cpu(cpu
) {
695 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
696 rc
= smp_ctl_qsi(cpu
);
702 printk(KERN_INFO
"hwsampler: CPUMF sampling is not authorized.\n");
707 printk(KERN_WARNING
"hwsampler: CPUMF is still enabled.\n");
708 rc
= smp_ctl_ssctl_stop(cpu
);
713 "CPU %d, CPUMF Sampling stopped now.\n", cpu
);
719 static int check_qsi_on_start(void)
723 struct hws_cpu_buffer
*cb
;
725 for_each_online_cpu(cpu
) {
726 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
727 rc
= smp_ctl_qsi(cpu
);
742 static void worker_on_start(unsigned int cpu
)
744 struct hws_cpu_buffer
*cb
;
746 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
747 cb
->worker_entry
= cb
->first_sdbt
;
750 static int worker_check_error(unsigned int cpu
, int ext_params
)
754 struct hws_cpu_buffer
*cb
;
757 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
758 sdbt
= (unsigned long *) cb
->worker_entry
;
763 if (ext_params
& EI_PRA
)
766 if (ext_params
& EI_LSDA
)
767 cb
->loss_of_sample_data
++;
769 if (ext_params
& EI_IEA
) {
770 cb
->invalid_entry_address
++;
774 if (ext_params
& EI_ISE
) {
775 cb
->incorrect_sdbt_entry
++;
779 if (ext_params
& EI_SACA
) {
780 cb
->sample_auth_change_alert
++;
787 static void worker_on_finish(unsigned int cpu
)
790 struct hws_cpu_buffer
*cb
;
792 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
795 rc
= smp_ctl_qsi(cpu
);
799 "hwsampler: CPU %d, CPUMF Stop/Deactivate sampling.\n",
801 rc
= smp_ctl_ssctl_stop(cpu
);
804 "hwsampler: CPU %d, CPUMF Deactivation failed.\n",
807 for_each_online_cpu(i
) {
812 queue_work_on(i
, hws_wq
,
820 static void worker_on_interrupt(unsigned int cpu
)
824 struct hws_cpu_buffer
*cb
;
826 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
828 sdbt
= (unsigned long *) cb
->worker_entry
;
831 /* do not proceed if stop was entered,
832 * forget the buffers not yet processed */
833 while (!done
&& !cb
->stop_mode
) {
834 unsigned long *trailer
;
835 struct hws_trailer_entry
*te
;
836 unsigned long *dear
= 0;
838 trailer
= trailer_entry_ptr(*sdbt
);
839 /* leave loop if no more work to do */
840 if (!(*trailer
& BUFFER_FULL_MASK
)) {
846 te
= (struct hws_trailer_entry
*)trailer
;
847 cb
->sample_overflow
+= te
->overflow
;
849 add_samples_to_oprofile(cpu
, sdbt
, dear
);
852 xchg((unsigned char *) te
, 0x40);
854 /* advance to next sdb slot in current sdbt */
856 /* in case link bit is set use address w/o link bit */
857 if (is_link_entry(sdbt
))
858 sdbt
= get_next_sdbt(sdbt
);
860 cb
->worker_entry
= (unsigned long)sdbt
;
864 static void add_samples_to_oprofile(unsigned int cpu
, unsigned long *sdbt
,
867 struct hws_data_entry
*sample_data_ptr
;
868 unsigned long *trailer
;
870 trailer
= trailer_entry_ptr(*sdbt
);
877 sample_data_ptr
= (struct hws_data_entry
*)(*sdbt
);
879 while ((unsigned long *)sample_data_ptr
< trailer
) {
880 struct pt_regs
*regs
= NULL
;
881 struct task_struct
*tsk
= NULL
;
884 * Check sampling mode, 1 indicates basic (=customer) sampling
887 if (sample_data_ptr
->def
!= 1) {
888 /* sample slot is not yet written */
891 /* make sure we don't use it twice,
892 * the next time the sampler will set it again */
893 sample_data_ptr
->def
= 0;
897 if (sample_data_ptr
->P
== 1) {
898 /* userspace sample */
899 unsigned int pid
= sample_data_ptr
->prim_asn
;
900 if (!counter_config
.user
)
903 tsk
= pid_task(find_vpid(pid
), PIDTYPE_PID
);
905 regs
= task_pt_regs(tsk
);
908 /* kernelspace sample */
909 if (!counter_config
.kernel
)
911 regs
= task_pt_regs(current
);
914 mutex_lock(&hws_sem
);
915 oprofile_add_ext_hw_sample(sample_data_ptr
->ia
, regs
, 0,
916 !sample_data_ptr
->P
, tsk
);
917 mutex_unlock(&hws_sem
);
923 static void worker(struct work_struct
*work
)
927 struct hws_cpu_buffer
*cb
;
929 cb
= container_of(work
, struct hws_cpu_buffer
, worker
);
930 cpu
= smp_processor_id();
931 ext_params
= atomic_xchg(&cb
->ext_params
, 0);
933 if (!cb
->worker_entry
)
934 worker_on_start(cpu
);
936 if (worker_check_error(cpu
, ext_params
))
940 worker_on_interrupt(cpu
);
943 worker_on_finish(cpu
);
947 * hwsampler_allocate() - allocate memory for the hardware sampler
948 * @sdbt: number of SDBTs per online CPU (must be > 0)
949 * @sdb: number of SDBs per SDBT (minimum 1, maximum 511)
951 * Returns 0 on success, !0 on failure.
953 int hwsampler_allocate(unsigned long sdbt
, unsigned long sdb
)
956 mutex_lock(&hws_sem
);
959 if (hws_state
!= HWS_DEALLOCATED
)
965 if (sdb
> MAX_NUM_SDB
|| sdb
< MIN_NUM_SDB
)
971 oom_killer_was_active
= 0;
972 register_oom_notifier(&hws_oom_notifier
);
974 for_each_online_cpu(cpu
) {
975 if (allocate_sdbt(cpu
)) {
976 unregister_oom_notifier(&hws_oom_notifier
);
980 unregister_oom_notifier(&hws_oom_notifier
);
981 if (oom_killer_was_active
)
984 hws_state
= HWS_STOPPED
;
988 mutex_unlock(&hws_sem
);
993 printk(KERN_ERR
"hwsampler: CPUMF Memory allocation failed.\n");
998 * hwsampler_deallocate() - deallocate hardware sampler memory
1000 * Returns 0 on success, !0 on failure.
1002 int hwsampler_deallocate(void)
1006 mutex_lock(&hws_sem
);
1009 if (hws_state
!= HWS_STOPPED
)
1010 goto deallocate_exit
;
1012 ctl_clear_bit(0, 5); /* set bit 58 CR0 off */
1015 hws_state
= HWS_DEALLOCATED
;
1019 mutex_unlock(&hws_sem
);
1024 unsigned long hwsampler_query_min_interval(void)
1026 return min_sampler_rate
;
1029 unsigned long hwsampler_query_max_interval(void)
1031 return max_sampler_rate
;
1034 unsigned long hwsampler_get_sample_overflow_count(unsigned int cpu
)
1036 struct hws_cpu_buffer
*cb
;
1038 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
1040 return cb
->sample_overflow
;
1043 int hwsampler_setup(void)
1047 struct hws_cpu_buffer
*cb
;
1049 mutex_lock(&hws_sem
);
1055 hws_state
= HWS_INIT
;
1057 init_all_cpu_buffers();
1059 rc
= check_hardware_prerequisites();
1063 rc
= check_qsi_on_setup();
1068 hws_wq
= create_workqueue("hwsampler");
1072 register_cpu_notifier(&hws_cpu_notifier
);
1074 for_each_online_cpu(cpu
) {
1075 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
1076 INIT_WORK(&cb
->worker
, worker
);
1077 rc
= smp_ctl_qsi(cpu
);
1079 if (min_sampler_rate
!= cb
->qsi
.min_sampl_rate
) {
1080 if (min_sampler_rate
) {
1082 "hwsampler: different min sampler rate values.\n");
1083 if (min_sampler_rate
< cb
->qsi
.min_sampl_rate
)
1085 cb
->qsi
.min_sampl_rate
;
1087 min_sampler_rate
= cb
->qsi
.min_sampl_rate
;
1089 if (max_sampler_rate
!= cb
->qsi
.max_sampl_rate
) {
1090 if (max_sampler_rate
) {
1092 "hwsampler: different max sampler rate values.\n");
1093 if (max_sampler_rate
> cb
->qsi
.max_sampl_rate
)
1095 cb
->qsi
.max_sampl_rate
;
1097 max_sampler_rate
= cb
->qsi
.max_sampl_rate
;
1100 register_external_interrupt(0x1407, hws_ext_handler
);
1102 hws_state
= HWS_DEALLOCATED
;
1106 mutex_unlock(&hws_sem
);
1110 int hwsampler_shutdown(void)
1114 mutex_lock(&hws_sem
);
1117 if (hws_state
== HWS_DEALLOCATED
|| hws_state
== HWS_STOPPED
) {
1118 mutex_unlock(&hws_sem
);
1121 flush_workqueue(hws_wq
);
1123 mutex_lock(&hws_sem
);
1125 if (hws_state
== HWS_STOPPED
) {
1126 ctl_clear_bit(0, 5); /* set bit 58 CR0 off */
1130 destroy_workqueue(hws_wq
);
1134 unregister_external_interrupt(0x1407, hws_ext_handler
);
1135 hws_state
= HWS_INIT
;
1138 mutex_unlock(&hws_sem
);
1140 unregister_cpu_notifier(&hws_cpu_notifier
);
1146 * hwsampler_start_all() - start hardware sampling on all online CPUs
1147 * @rate: specifies the used interval when samples are taken
1149 * Returns 0 on success, !0 on failure.
1151 int hwsampler_start_all(unsigned long rate
)
1155 mutex_lock(&hws_sem
);
1160 if (hws_state
!= HWS_STOPPED
)
1161 goto start_all_exit
;
1165 /* fail if rate is not valid */
1166 if (interval
< min_sampler_rate
|| interval
> max_sampler_rate
)
1167 goto start_all_exit
;
1169 rc
= check_qsi_on_start();
1171 goto start_all_exit
;
1173 rc
= prepare_cpu_buffers();
1175 goto start_all_exit
;
1177 for_each_online_cpu(cpu
) {
1178 rc
= start_sampling(cpu
);
1183 for_each_online_cpu(cpu
) {
1186 goto start_all_exit
;
1188 hws_state
= HWS_STARTED
;
1192 mutex_unlock(&hws_sem
);
1197 register_oom_notifier(&hws_oom_notifier
);
1200 /* now let them in, 1407 CPUMF external interrupts */
1201 ctl_set_bit(0, 5); /* set CR0 bit 58 */
1207 * hwsampler_stop_all() - stop hardware sampling on all online CPUs
1209 * Returns 0 on success, !0 on failure.
1211 int hwsampler_stop_all(void)
1213 int tmp_rc
, rc
, cpu
;
1214 struct hws_cpu_buffer
*cb
;
1216 mutex_lock(&hws_sem
);
1219 if (hws_state
== HWS_INIT
) {
1220 mutex_unlock(&hws_sem
);
1223 hws_state
= HWS_STOPPING
;
1224 mutex_unlock(&hws_sem
);
1226 for_each_online_cpu(cpu
) {
1227 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
1229 tmp_rc
= stop_sampling(cpu
);
1235 flush_workqueue(hws_wq
);
1237 mutex_lock(&hws_sem
);
1239 unregister_oom_notifier(&hws_oom_notifier
);
1242 hws_state
= HWS_STOPPED
;
1243 mutex_unlock(&hws_sem
);