2 * Copyright IBM Corp. 2010
3 * Author: Heinz Graalfs <graalfs@de.ibm.com>
6 #include <linux/kernel_stat.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/workqueue.h>
12 #include <linux/interrupt.h>
13 #include <linux/notifier.h>
14 #include <linux/cpu.h>
15 #include <linux/semaphore.h>
16 #include <linux/oom.h>
17 #include <linux/oprofile.h>
19 #include <asm/facility.h>
20 #include <asm/cpu_mf.h>
23 #include "hwsampler.h"
24 #include "op_counter.h"
26 #define MAX_NUM_SDB 511
29 DECLARE_PER_CPU(struct hws_cpu_buffer
, sampler_cpu_buffer
);
31 struct hws_execute_parms
{
36 DEFINE_PER_CPU(struct hws_cpu_buffer
, sampler_cpu_buffer
);
37 EXPORT_PER_CPU_SYMBOL(sampler_cpu_buffer
);
39 static DEFINE_MUTEX(hws_sem
);
40 static DEFINE_MUTEX(hws_sem_oom
);
42 static unsigned char hws_flush_all
;
43 static unsigned int hws_oom
;
44 static unsigned int hws_alert
;
45 static struct workqueue_struct
*hws_wq
;
47 static unsigned int hws_state
;
55 /* set to 1 if called by kernel during memory allocation */
56 static unsigned char oom_killer_was_active
;
57 /* size of SDBT and SDB as of allocate API */
58 static unsigned long num_sdbt
= 100;
59 static unsigned long num_sdb
= 511;
60 /* sampling interval (machine cycles) */
61 static unsigned long interval
;
63 static unsigned long min_sampler_rate
;
64 static unsigned long max_sampler_rate
;
66 static void execute_qsi(void *parms
)
68 struct hws_execute_parms
*ep
= parms
;
70 ep
->rc
= qsi(ep
->buffer
);
73 static void execute_ssctl(void *parms
)
75 struct hws_execute_parms
*ep
= parms
;
77 ep
->rc
= lsctl(ep
->buffer
);
80 static int smp_ctl_ssctl_stop(int cpu
)
83 struct hws_execute_parms ep
;
84 struct hws_cpu_buffer
*cb
;
86 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
91 ep
.buffer
= &cb
->ssctl
;
92 smp_call_function_single(cpu
, execute_ssctl
, &ep
, 1);
95 printk(KERN_ERR
"hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu
);
100 smp_call_function_single(cpu
, execute_qsi
, &ep
, 1);
102 if (cb
->qsi
.es
|| cb
->qsi
.cs
) {
103 printk(KERN_EMERG
"CPUMF sampling did not stop properly.\n");
110 static int smp_ctl_ssctl_deactivate(int cpu
)
113 struct hws_execute_parms ep
;
114 struct hws_cpu_buffer
*cb
;
116 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
121 ep
.buffer
= &cb
->ssctl
;
122 smp_call_function_single(cpu
, execute_ssctl
, &ep
, 1);
125 printk(KERN_ERR
"hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu
);
127 ep
.buffer
= &cb
->qsi
;
128 smp_call_function_single(cpu
, execute_qsi
, &ep
, 1);
131 printk(KERN_EMERG
"CPUMF sampling was not set inactive.\n");
136 static int smp_ctl_ssctl_enable_activate(int cpu
, unsigned long interval
)
139 struct hws_execute_parms ep
;
140 struct hws_cpu_buffer
*cb
;
142 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
145 cb
->ssctl
.tear
= cb
->first_sdbt
;
146 cb
->ssctl
.dear
= *(unsigned long *) cb
->first_sdbt
;
147 cb
->ssctl
.interval
= interval
;
151 ep
.buffer
= &cb
->ssctl
;
152 smp_call_function_single(cpu
, execute_ssctl
, &ep
, 1);
155 printk(KERN_ERR
"hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu
);
157 ep
.buffer
= &cb
->qsi
;
158 smp_call_function_single(cpu
, execute_qsi
, &ep
, 1);
160 printk(KERN_ERR
"hwsampler: CPU %d CPUMF QSI failed.\n", cpu
);
165 static int smp_ctl_qsi(int cpu
)
167 struct hws_execute_parms ep
;
168 struct hws_cpu_buffer
*cb
;
170 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
172 ep
.buffer
= &cb
->qsi
;
173 smp_call_function_single(cpu
, execute_qsi
, &ep
, 1);
178 static void hws_ext_handler(struct ext_code ext_code
,
179 unsigned int param32
, unsigned long param64
)
181 struct hws_cpu_buffer
*cb
= &__get_cpu_var(sampler_cpu_buffer
);
183 if (!(param32
& CPU_MF_INT_SF_MASK
))
189 inc_irq_stat(IRQEXT_CMS
);
190 atomic_xchg(&cb
->ext_params
, atomic_read(&cb
->ext_params
) | param32
);
193 queue_work(hws_wq
, &cb
->worker
);
196 static void worker(struct work_struct
*work
);
198 static void add_samples_to_oprofile(unsigned cpu
, unsigned long *,
199 unsigned long *dear
);
201 static void init_all_cpu_buffers(void)
204 struct hws_cpu_buffer
*cb
;
206 for_each_online_cpu(cpu
) {
207 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
208 memset(cb
, 0, sizeof(struct hws_cpu_buffer
));
212 static int prepare_cpu_buffers(void)
216 struct hws_cpu_buffer
*cb
;
219 for_each_online_cpu(cpu
) {
220 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
221 atomic_set(&cb
->ext_params
, 0);
222 cb
->worker_entry
= 0;
223 cb
->sample_overflow
= 0;
225 cb
->incorrect_sdbt_entry
= 0;
226 cb
->invalid_entry_address
= 0;
227 cb
->loss_of_sample_data
= 0;
228 cb
->sample_auth_change_alert
= 0;
238 * allocate_sdbt() - allocate sampler memory
239 * @cpu: the cpu for which sampler memory is allocated
241 * A 4K page is allocated for each requested SDBT.
242 * A maximum of 511 4K pages are allocated for the SDBs in each of the SDBTs.
243 * Set ALERT_REQ mask in each SDBs trailer.
244 * Returns zero if successful, <0 otherwise.
246 static int allocate_sdbt(int cpu
)
252 unsigned long *trailer
;
253 struct hws_cpu_buffer
*cb
;
255 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
263 for (j
= 0; j
< num_sdbt
; j
++) {
264 sdbt
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
266 mutex_lock(&hws_sem_oom
);
267 /* OOM killer might have been activated */
269 if (oom_killer_was_active
|| !sdbt
) {
271 free_page((unsigned long)sdbt
);
273 goto allocate_sdbt_error
;
275 if (cb
->first_sdbt
== 0)
276 cb
->first_sdbt
= (unsigned long)sdbt
;
278 /* link current page to tail of chain */
280 *tail
= (unsigned long)(void *)sdbt
+ 1;
282 mutex_unlock(&hws_sem_oom
);
284 for (k
= 0; k
< num_sdb
; k
++) {
285 /* get and set SDB page */
286 sdb
= get_zeroed_page(GFP_KERNEL
);
288 mutex_lock(&hws_sem_oom
);
289 /* OOM killer might have been activated */
291 if (oom_killer_was_active
|| !sdb
) {
295 goto allocate_sdbt_error
;
298 trailer
= trailer_entry_ptr(*sdbt
);
299 *trailer
= SDB_TE_ALERT_REQ_MASK
;
301 mutex_unlock(&hws_sem_oom
);
305 mutex_lock(&hws_sem_oom
);
306 if (oom_killer_was_active
)
307 goto allocate_sdbt_error
;
311 *tail
= (unsigned long)
312 ((void *)cb
->first_sdbt
) + 1;
315 mutex_unlock(&hws_sem_oom
);
320 goto allocate_sdbt_exit
;
324 * deallocate_sdbt() - deallocate all sampler memory
326 * For each online CPU all SDBT trees are deallocated.
327 * Returns the number of freed pages.
329 static int deallocate_sdbt(void)
336 for_each_online_cpu(cpu
) {
340 struct hws_cpu_buffer
*cb
;
342 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
347 sdbt
= cb
->first_sdbt
;
348 curr
= (unsigned long *) sdbt
;
351 /* we'll free the SDBT after all SDBs are processed... */
356 /* watch for link entry reset if found */
357 if (is_link_entry(curr
)) {
358 curr
= get_next_sdbt(curr
);
362 /* we are done if we reach the start */
363 if ((unsigned long) curr
== start
)
366 sdbt
= (unsigned long) curr
;
368 /* process SDB pointer */
381 static int start_sampling(int cpu
)
384 struct hws_cpu_buffer
*cb
;
386 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
387 rc
= smp_ctl_ssctl_enable_activate(cpu
, interval
);
389 printk(KERN_INFO
"hwsampler: CPU %d ssctl failed.\n", cpu
);
395 printk(KERN_INFO
"hwsampler: CPU %d ssctl not enabled.\n", cpu
);
400 printk(KERN_INFO
"hwsampler: CPU %d ssctl not active.\n", cpu
);
405 "hwsampler: CPU %d, CPUMF Sampling started, interval %lu.\n",
414 static int stop_sampling(int cpu
)
418 struct hws_cpu_buffer
*cb
;
420 rc
= smp_ctl_qsi(cpu
);
423 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
424 if (!rc
&& !cb
->qsi
.es
)
425 printk(KERN_INFO
"hwsampler: CPU %d, already stopped.\n", cpu
);
427 rc
= smp_ctl_ssctl_stop(cpu
);
429 printk(KERN_INFO
"hwsampler: CPU %d, ssctl stop error %d.\n",
434 printk(KERN_INFO
"hwsampler: CPU %d, CPUMF Sampling stopped.\n", cpu
);
439 printk(KERN_ERR
"hwsampler: CPU %d CPUMF Request alert,"
440 " count=%lu.\n", cpu
, v
);
442 v
= cb
->loss_of_sample_data
;
444 printk(KERN_ERR
"hwsampler: CPU %d CPUMF Loss of sample data,"
445 " count=%lu.\n", cpu
, v
);
447 v
= cb
->invalid_entry_address
;
449 printk(KERN_ERR
"hwsampler: CPU %d CPUMF Invalid entry address,"
450 " count=%lu.\n", cpu
, v
);
452 v
= cb
->incorrect_sdbt_entry
;
455 "hwsampler: CPU %d CPUMF Incorrect SDBT address,"
456 " count=%lu.\n", cpu
, v
);
458 v
= cb
->sample_auth_change_alert
;
461 "hwsampler: CPU %d CPUMF Sample authorization change,"
462 " count=%lu.\n", cpu
, v
);
467 static int check_hardware_prerequisites(void)
469 if (!test_facility(68))
474 * hws_oom_callback() - the OOM callback function
476 * In case the callback is invoked during memory allocation for the
477 * hw sampler, all obtained memory is deallocated and a flag is set
478 * so main sampler memory allocation can exit with a failure code.
479 * In case the callback is invoked during sampling the hw sampler
480 * is deactivated for all CPUs.
482 static int hws_oom_callback(struct notifier_block
*nfb
,
483 unsigned long dummy
, void *parm
)
485 unsigned long *freed
;
487 struct hws_cpu_buffer
*cb
;
491 mutex_lock(&hws_sem_oom
);
493 if (hws_state
== HWS_DEALLOCATED
) {
494 /* during memory allocation */
495 if (oom_killer_was_active
== 0) {
496 oom_killer_was_active
= 1;
497 *freed
+= deallocate_sdbt();
502 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
505 for_each_online_cpu(i
) {
506 smp_ctl_ssctl_deactivate(i
);
512 "hwsampler: CPU %d, OOM notify during CPUMF Sampling.\n",
517 mutex_unlock(&hws_sem_oom
);
522 static struct notifier_block hws_oom_notifier
= {
523 .notifier_call
= hws_oom_callback
526 static int hws_cpu_callback(struct notifier_block
*nfb
,
527 unsigned long action
, void *hcpu
)
529 /* We do not have sampler space available for all possible CPUs.
530 All CPUs should be online when hw sampling is activated. */
531 return (hws_state
<= HWS_DEALLOCATED
) ? NOTIFY_OK
: NOTIFY_BAD
;
534 static struct notifier_block hws_cpu_notifier
= {
535 .notifier_call
= hws_cpu_callback
539 * hwsampler_deactivate() - set hardware sampling temporarily inactive
540 * @cpu: specifies the CPU to be set inactive.
542 * Returns 0 on success, !0 on failure.
544 int hwsampler_deactivate(unsigned int cpu
)
547 * Deactivate hw sampling temporarily and flush the buffer
548 * by pushing all the pending samples to oprofile buffer.
550 * This function can be called under one of the following conditions:
551 * Memory unmap, task is exiting.
554 struct hws_cpu_buffer
*cb
;
557 mutex_lock(&hws_sem
);
559 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
560 if (hws_state
== HWS_STARTED
) {
561 rc
= smp_ctl_qsi(cpu
);
564 rc
= smp_ctl_ssctl_deactivate(cpu
);
567 "hwsampler: CPU %d, CPUMF Deactivation failed.\n", cpu
);
569 hws_state
= HWS_STOPPING
;
572 /* Add work to queue to read pending samples.*/
573 queue_work_on(cpu
, hws_wq
, &cb
->worker
);
577 mutex_unlock(&hws_sem
);
580 flush_workqueue(hws_wq
);
586 * hwsampler_activate() - activate/resume hardware sampling which was deactivated
587 * @cpu: specifies the CPU to be set active.
589 * Returns 0 on success, !0 on failure.
591 int hwsampler_activate(unsigned int cpu
)
594 * Re-activate hw sampling. This should be called in pair with
595 * hwsampler_deactivate().
598 struct hws_cpu_buffer
*cb
;
601 mutex_lock(&hws_sem
);
603 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
604 if (hws_state
== HWS_STARTED
) {
605 rc
= smp_ctl_qsi(cpu
);
609 rc
= smp_ctl_ssctl_enable_activate(cpu
, interval
);
612 "CPU %d, CPUMF activate sampling failed.\n",
618 mutex_unlock(&hws_sem
);
623 static int check_qsi_on_setup(void)
627 struct hws_cpu_buffer
*cb
;
629 for_each_online_cpu(cpu
) {
630 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
631 rc
= smp_ctl_qsi(cpu
);
637 printk(KERN_INFO
"hwsampler: CPUMF sampling is not authorized.\n");
642 printk(KERN_WARNING
"hwsampler: CPUMF is still enabled.\n");
643 rc
= smp_ctl_ssctl_stop(cpu
);
648 "CPU %d, CPUMF Sampling stopped now.\n", cpu
);
654 static int check_qsi_on_start(void)
658 struct hws_cpu_buffer
*cb
;
660 for_each_online_cpu(cpu
) {
661 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
662 rc
= smp_ctl_qsi(cpu
);
677 static void worker_on_start(unsigned int cpu
)
679 struct hws_cpu_buffer
*cb
;
681 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
682 cb
->worker_entry
= cb
->first_sdbt
;
685 static int worker_check_error(unsigned int cpu
, int ext_params
)
689 struct hws_cpu_buffer
*cb
;
692 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
693 sdbt
= (unsigned long *) cb
->worker_entry
;
698 if (ext_params
& CPU_MF_INT_SF_PRA
)
701 if (ext_params
& CPU_MF_INT_SF_LSDA
)
702 cb
->loss_of_sample_data
++;
704 if (ext_params
& CPU_MF_INT_SF_IAE
) {
705 cb
->invalid_entry_address
++;
709 if (ext_params
& CPU_MF_INT_SF_ISE
) {
710 cb
->incorrect_sdbt_entry
++;
714 if (ext_params
& CPU_MF_INT_SF_SACA
) {
715 cb
->sample_auth_change_alert
++;
722 static void worker_on_finish(unsigned int cpu
)
725 struct hws_cpu_buffer
*cb
;
727 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
730 rc
= smp_ctl_qsi(cpu
);
734 "hwsampler: CPU %d, CPUMF Stop/Deactivate sampling.\n",
736 rc
= smp_ctl_ssctl_stop(cpu
);
739 "hwsampler: CPU %d, CPUMF Deactivation failed.\n",
742 for_each_online_cpu(i
) {
747 queue_work_on(i
, hws_wq
,
755 static void worker_on_interrupt(unsigned int cpu
)
759 struct hws_cpu_buffer
*cb
;
761 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
763 sdbt
= (unsigned long *) cb
->worker_entry
;
766 /* do not proceed if stop was entered,
767 * forget the buffers not yet processed */
768 while (!done
&& !cb
->stop_mode
) {
769 unsigned long *trailer
;
770 struct hws_trailer_entry
*te
;
771 unsigned long *dear
= 0;
773 trailer
= trailer_entry_ptr(*sdbt
);
774 /* leave loop if no more work to do */
775 if (!(*trailer
& SDB_TE_BUFFER_FULL_MASK
)) {
781 te
= (struct hws_trailer_entry
*)trailer
;
782 cb
->sample_overflow
+= te
->overflow
;
784 add_samples_to_oprofile(cpu
, sdbt
, dear
);
787 xchg((unsigned char *) te
, 0x40);
789 /* advance to next sdb slot in current sdbt */
791 /* in case link bit is set use address w/o link bit */
792 if (is_link_entry(sdbt
))
793 sdbt
= get_next_sdbt(sdbt
);
795 cb
->worker_entry
= (unsigned long)sdbt
;
799 static void add_samples_to_oprofile(unsigned int cpu
, unsigned long *sdbt
,
802 struct hws_basic_entry
*sample_data_ptr
;
803 unsigned long *trailer
;
805 trailer
= trailer_entry_ptr(*sdbt
);
812 sample_data_ptr
= (struct hws_basic_entry
*)(*sdbt
);
814 while ((unsigned long *)sample_data_ptr
< trailer
) {
815 struct pt_regs
*regs
= NULL
;
816 struct task_struct
*tsk
= NULL
;
819 * Check sampling mode, 1 indicates basic (=customer) sampling
822 if (sample_data_ptr
->def
!= 1) {
823 /* sample slot is not yet written */
826 /* make sure we don't use it twice,
827 * the next time the sampler will set it again */
828 sample_data_ptr
->def
= 0;
832 if (sample_data_ptr
->P
== 1) {
833 /* userspace sample */
834 unsigned int pid
= sample_data_ptr
->prim_asn
;
835 if (!counter_config
.user
)
838 tsk
= pid_task(find_vpid(pid
), PIDTYPE_PID
);
840 regs
= task_pt_regs(tsk
);
843 /* kernelspace sample */
844 if (!counter_config
.kernel
)
846 regs
= task_pt_regs(current
);
849 mutex_lock(&hws_sem
);
850 oprofile_add_ext_hw_sample(sample_data_ptr
->ia
, regs
, 0,
851 !sample_data_ptr
->P
, tsk
);
852 mutex_unlock(&hws_sem
);
858 static void worker(struct work_struct
*work
)
862 struct hws_cpu_buffer
*cb
;
864 cb
= container_of(work
, struct hws_cpu_buffer
, worker
);
865 cpu
= smp_processor_id();
866 ext_params
= atomic_xchg(&cb
->ext_params
, 0);
868 if (!cb
->worker_entry
)
869 worker_on_start(cpu
);
871 if (worker_check_error(cpu
, ext_params
))
875 worker_on_interrupt(cpu
);
878 worker_on_finish(cpu
);
882 * hwsampler_allocate() - allocate memory for the hardware sampler
883 * @sdbt: number of SDBTs per online CPU (must be > 0)
884 * @sdb: number of SDBs per SDBT (minimum 1, maximum 511)
886 * Returns 0 on success, !0 on failure.
888 int hwsampler_allocate(unsigned long sdbt
, unsigned long sdb
)
891 mutex_lock(&hws_sem
);
894 if (hws_state
!= HWS_DEALLOCATED
)
900 if (sdb
> MAX_NUM_SDB
|| sdb
< MIN_NUM_SDB
)
906 oom_killer_was_active
= 0;
907 register_oom_notifier(&hws_oom_notifier
);
909 for_each_online_cpu(cpu
) {
910 if (allocate_sdbt(cpu
)) {
911 unregister_oom_notifier(&hws_oom_notifier
);
915 unregister_oom_notifier(&hws_oom_notifier
);
916 if (oom_killer_was_active
)
919 hws_state
= HWS_STOPPED
;
923 mutex_unlock(&hws_sem
);
928 printk(KERN_ERR
"hwsampler: CPUMF Memory allocation failed.\n");
933 * hwsampler_deallocate() - deallocate hardware sampler memory
935 * Returns 0 on success, !0 on failure.
937 int hwsampler_deallocate(void)
941 mutex_lock(&hws_sem
);
944 if (hws_state
!= HWS_STOPPED
)
945 goto deallocate_exit
;
947 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT
);
951 hws_state
= HWS_DEALLOCATED
;
955 mutex_unlock(&hws_sem
);
960 unsigned long hwsampler_query_min_interval(void)
962 return min_sampler_rate
;
965 unsigned long hwsampler_query_max_interval(void)
967 return max_sampler_rate
;
970 unsigned long hwsampler_get_sample_overflow_count(unsigned int cpu
)
972 struct hws_cpu_buffer
*cb
;
974 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
976 return cb
->sample_overflow
;
979 int hwsampler_setup(void)
983 struct hws_cpu_buffer
*cb
;
985 mutex_lock(&hws_sem
);
991 hws_state
= HWS_INIT
;
993 init_all_cpu_buffers();
995 rc
= check_hardware_prerequisites();
999 rc
= check_qsi_on_setup();
1004 hws_wq
= create_workqueue("hwsampler");
1008 register_cpu_notifier(&hws_cpu_notifier
);
1010 for_each_online_cpu(cpu
) {
1011 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
1012 INIT_WORK(&cb
->worker
, worker
);
1013 rc
= smp_ctl_qsi(cpu
);
1015 if (min_sampler_rate
!= cb
->qsi
.min_sampl_rate
) {
1016 if (min_sampler_rate
) {
1018 "hwsampler: different min sampler rate values.\n");
1019 if (min_sampler_rate
< cb
->qsi
.min_sampl_rate
)
1021 cb
->qsi
.min_sampl_rate
;
1023 min_sampler_rate
= cb
->qsi
.min_sampl_rate
;
1025 if (max_sampler_rate
!= cb
->qsi
.max_sampl_rate
) {
1026 if (max_sampler_rate
) {
1028 "hwsampler: different max sampler rate values.\n");
1029 if (max_sampler_rate
> cb
->qsi
.max_sampl_rate
)
1031 cb
->qsi
.max_sampl_rate
;
1033 max_sampler_rate
= cb
->qsi
.max_sampl_rate
;
1036 register_external_interrupt(0x1407, hws_ext_handler
);
1038 hws_state
= HWS_DEALLOCATED
;
1042 mutex_unlock(&hws_sem
);
1046 int hwsampler_shutdown(void)
1050 mutex_lock(&hws_sem
);
1053 if (hws_state
== HWS_DEALLOCATED
|| hws_state
== HWS_STOPPED
) {
1054 mutex_unlock(&hws_sem
);
1057 flush_workqueue(hws_wq
);
1059 mutex_lock(&hws_sem
);
1061 if (hws_state
== HWS_STOPPED
) {
1062 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT
);
1067 destroy_workqueue(hws_wq
);
1071 unregister_external_interrupt(0x1407, hws_ext_handler
);
1072 hws_state
= HWS_INIT
;
1075 mutex_unlock(&hws_sem
);
1077 unregister_cpu_notifier(&hws_cpu_notifier
);
1083 * hwsampler_start_all() - start hardware sampling on all online CPUs
1084 * @rate: specifies the used interval when samples are taken
1086 * Returns 0 on success, !0 on failure.
1088 int hwsampler_start_all(unsigned long rate
)
1092 mutex_lock(&hws_sem
);
1097 if (hws_state
!= HWS_STOPPED
)
1098 goto start_all_exit
;
1102 /* fail if rate is not valid */
1103 if (interval
< min_sampler_rate
|| interval
> max_sampler_rate
)
1104 goto start_all_exit
;
1106 rc
= check_qsi_on_start();
1108 goto start_all_exit
;
1110 rc
= prepare_cpu_buffers();
1112 goto start_all_exit
;
1114 for_each_online_cpu(cpu
) {
1115 rc
= start_sampling(cpu
);
1120 for_each_online_cpu(cpu
) {
1123 goto start_all_exit
;
1125 hws_state
= HWS_STARTED
;
1129 mutex_unlock(&hws_sem
);
1134 register_oom_notifier(&hws_oom_notifier
);
1137 /* now let them in, 1407 CPUMF external interrupts */
1139 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT
);
1145 * hwsampler_stop_all() - stop hardware sampling on all online CPUs
1147 * Returns 0 on success, !0 on failure.
1149 int hwsampler_stop_all(void)
1151 int tmp_rc
, rc
, cpu
;
1152 struct hws_cpu_buffer
*cb
;
1154 mutex_lock(&hws_sem
);
1157 if (hws_state
== HWS_INIT
) {
1158 mutex_unlock(&hws_sem
);
1161 hws_state
= HWS_STOPPING
;
1162 mutex_unlock(&hws_sem
);
1164 for_each_online_cpu(cpu
) {
1165 cb
= &per_cpu(sampler_cpu_buffer
, cpu
);
1167 tmp_rc
= stop_sampling(cpu
);
1173 flush_workqueue(hws_wq
);
1175 mutex_lock(&hws_sem
);
1177 unregister_oom_notifier(&hws_oom_notifier
);
1180 hws_state
= HWS_STOPPED
;
1181 mutex_unlock(&hws_sem
);