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 #define ALERT_REQ_MASK 0x4000000000000000ul
30 #define BUFFER_FULL_MASK 0x8000000000000000ul
32 DECLARE_PER_CPU(struct hws_cpu_buffer, sampler_cpu_buffer);
34 struct hws_execute_parms {
39 DEFINE_PER_CPU(struct hws_cpu_buffer, sampler_cpu_buffer);
40 EXPORT_PER_CPU_SYMBOL(sampler_cpu_buffer);
42 static DEFINE_MUTEX(hws_sem);
43 static DEFINE_MUTEX(hws_sem_oom);
45 static unsigned char hws_flush_all;
46 static unsigned int hws_oom;
47 static struct workqueue_struct *hws_wq;
49 static unsigned int hws_state;
57 /* set to 1 if called by kernel during memory allocation */
58 static unsigned char oom_killer_was_active;
59 /* size of SDBT and SDB as of allocate API */
60 static unsigned long num_sdbt = 100;
61 static unsigned long num_sdb = 511;
62 /* sampling interval (machine cycles) */
63 static unsigned long interval;
65 static unsigned long min_sampler_rate;
66 static unsigned long max_sampler_rate;
68 static int ssctl(void *buffer)
72 /* set in order to detect a program check */
76 "0: .insn s,0xB2870000,0(%1)\n"
80 EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
81 : "+d" (cc), "+a" (buffer)
82 : "m" (*((struct hws_ssctl_request_block *)buffer))
85 return cc ? -EINVAL : 0 ;
88 static int qsi(void *buffer)
94 "0: .insn s,0xB2860000,0(%1)\n"
97 EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
98 : "=d" (cc), "+a" (buffer)
99 : "m" (*((struct hws_qsi_info_block *)buffer))
102 return cc ? -EINVAL : 0;
105 static void execute_qsi(void *parms)
107 struct hws_execute_parms *ep = parms;
109 ep->rc = qsi(ep->buffer);
112 static void execute_ssctl(void *parms)
114 struct hws_execute_parms *ep = parms;
116 ep->rc = ssctl(ep->buffer);
119 static int smp_ctl_ssctl_stop(int cpu)
122 struct hws_execute_parms ep;
123 struct hws_cpu_buffer *cb;
125 cb = &per_cpu(sampler_cpu_buffer, cpu);
130 ep.buffer = &cb->ssctl;
131 smp_call_function_single(cpu, execute_ssctl, &ep, 1);
134 printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
138 ep.buffer = &cb->qsi;
139 smp_call_function_single(cpu, execute_qsi, &ep, 1);
141 if (cb->qsi.es || cb->qsi.cs) {
142 printk(KERN_EMERG "CPUMF sampling did not stop properly.\n");
149 static int smp_ctl_ssctl_deactivate(int cpu)
152 struct hws_execute_parms ep;
153 struct hws_cpu_buffer *cb;
155 cb = &per_cpu(sampler_cpu_buffer, cpu);
160 ep.buffer = &cb->ssctl;
161 smp_call_function_single(cpu, execute_ssctl, &ep, 1);
164 printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
166 ep.buffer = &cb->qsi;
167 smp_call_function_single(cpu, execute_qsi, &ep, 1);
170 printk(KERN_EMERG "CPUMF sampling was not set inactive.\n");
175 static int smp_ctl_ssctl_enable_activate(int cpu, unsigned long interval)
178 struct hws_execute_parms ep;
179 struct hws_cpu_buffer *cb;
181 cb = &per_cpu(sampler_cpu_buffer, cpu);
184 cb->ssctl.tear = cb->first_sdbt;
185 cb->ssctl.dear = *(unsigned long *) cb->first_sdbt;
186 cb->ssctl.interval = interval;
190 ep.buffer = &cb->ssctl;
191 smp_call_function_single(cpu, execute_ssctl, &ep, 1);
194 printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
196 ep.buffer = &cb->qsi;
197 smp_call_function_single(cpu, execute_qsi, &ep, 1);
199 printk(KERN_ERR "hwsampler: CPU %d CPUMF QSI failed.\n", cpu);
204 static int smp_ctl_qsi(int cpu)
206 struct hws_execute_parms ep;
207 struct hws_cpu_buffer *cb;
209 cb = &per_cpu(sampler_cpu_buffer, cpu);
211 ep.buffer = &cb->qsi;
212 smp_call_function_single(cpu, execute_qsi, &ep, 1);
217 static inline unsigned long *trailer_entry_ptr(unsigned long v)
223 ret -= sizeof(struct hws_trailer_entry);
225 return (unsigned long *) ret;
228 static void hws_ext_handler(struct ext_code ext_code,
229 unsigned int param32, unsigned long param64)
231 struct hws_cpu_buffer *cb = &__get_cpu_var(sampler_cpu_buffer);
233 if (!(param32 & CPU_MF_INT_SF_MASK))
236 inc_irq_stat(IRQEXT_CMS);
237 atomic_xchg(&cb->ext_params, atomic_read(&cb->ext_params) | param32);
240 queue_work(hws_wq, &cb->worker);
243 static void worker(struct work_struct *work);
245 static void add_samples_to_oprofile(unsigned cpu, unsigned long *,
246 unsigned long *dear);
248 static void init_all_cpu_buffers(void)
251 struct hws_cpu_buffer *cb;
253 for_each_online_cpu(cpu) {
254 cb = &per_cpu(sampler_cpu_buffer, cpu);
255 memset(cb, 0, sizeof(struct hws_cpu_buffer));
259 static int is_link_entry(unsigned long *s)
261 return *s & 0x1ul ? 1 : 0;
264 static unsigned long *get_next_sdbt(unsigned long *s)
266 return (unsigned long *) (*s & ~0x1ul);
269 static int prepare_cpu_buffers(void)
273 struct hws_cpu_buffer *cb;
276 for_each_online_cpu(cpu) {
277 cb = &per_cpu(sampler_cpu_buffer, cpu);
278 atomic_set(&cb->ext_params, 0);
279 cb->worker_entry = 0;
280 cb->sample_overflow = 0;
282 cb->incorrect_sdbt_entry = 0;
283 cb->invalid_entry_address = 0;
284 cb->loss_of_sample_data = 0;
285 cb->sample_auth_change_alert = 0;
295 * allocate_sdbt() - allocate sampler memory
296 * @cpu: the cpu for which sampler memory is allocated
298 * A 4K page is allocated for each requested SDBT.
299 * A maximum of 511 4K pages are allocated for the SDBs in each of the SDBTs.
300 * Set ALERT_REQ mask in each SDBs trailer.
301 * Returns zero if successful, <0 otherwise.
303 static int allocate_sdbt(int cpu)
309 unsigned long *trailer;
310 struct hws_cpu_buffer *cb;
312 cb = &per_cpu(sampler_cpu_buffer, cpu);
320 for (j = 0; j < num_sdbt; j++) {
321 sdbt = (unsigned long *)get_zeroed_page(GFP_KERNEL);
323 mutex_lock(&hws_sem_oom);
324 /* OOM killer might have been activated */
326 if (oom_killer_was_active || !sdbt) {
328 free_page((unsigned long)sdbt);
330 goto allocate_sdbt_error;
332 if (cb->first_sdbt == 0)
333 cb->first_sdbt = (unsigned long)sdbt;
335 /* link current page to tail of chain */
337 *tail = (unsigned long)(void *)sdbt + 1;
339 mutex_unlock(&hws_sem_oom);
341 for (k = 0; k < num_sdb; k++) {
342 /* get and set SDB page */
343 sdb = get_zeroed_page(GFP_KERNEL);
345 mutex_lock(&hws_sem_oom);
346 /* OOM killer might have been activated */
348 if (oom_killer_was_active || !sdb) {
352 goto allocate_sdbt_error;
355 trailer = trailer_entry_ptr(*sdbt);
356 *trailer = ALERT_REQ_MASK;
358 mutex_unlock(&hws_sem_oom);
362 mutex_lock(&hws_sem_oom);
363 if (oom_killer_was_active)
364 goto allocate_sdbt_error;
368 *tail = (unsigned long)
369 ((void *)cb->first_sdbt) + 1;
372 mutex_unlock(&hws_sem_oom);
377 goto allocate_sdbt_exit;
381 * deallocate_sdbt() - deallocate all sampler memory
383 * For each online CPU all SDBT trees are deallocated.
384 * Returns the number of freed pages.
386 static int deallocate_sdbt(void)
393 for_each_online_cpu(cpu) {
397 struct hws_cpu_buffer *cb;
399 cb = &per_cpu(sampler_cpu_buffer, cpu);
404 sdbt = cb->first_sdbt;
405 curr = (unsigned long *) sdbt;
408 /* we'll free the SDBT after all SDBs are processed... */
413 /* watch for link entry reset if found */
414 if (is_link_entry(curr)) {
415 curr = get_next_sdbt(curr);
419 /* we are done if we reach the start */
420 if ((unsigned long) curr == start)
423 sdbt = (unsigned long) curr;
425 /* process SDB pointer */
438 static int start_sampling(int cpu)
441 struct hws_cpu_buffer *cb;
443 cb = &per_cpu(sampler_cpu_buffer, cpu);
444 rc = smp_ctl_ssctl_enable_activate(cpu, interval);
446 printk(KERN_INFO "hwsampler: CPU %d ssctl failed.\n", cpu);
452 printk(KERN_INFO "hwsampler: CPU %d ssctl not enabled.\n", cpu);
457 printk(KERN_INFO "hwsampler: CPU %d ssctl not active.\n", cpu);
462 "hwsampler: CPU %d, CPUMF Sampling started, interval %lu.\n",
471 static int stop_sampling(int cpu)
475 struct hws_cpu_buffer *cb;
477 rc = smp_ctl_qsi(cpu);
480 cb = &per_cpu(sampler_cpu_buffer, cpu);
481 if (!rc && !cb->qsi.es)
482 printk(KERN_INFO "hwsampler: CPU %d, already stopped.\n", cpu);
484 rc = smp_ctl_ssctl_stop(cpu);
486 printk(KERN_INFO "hwsampler: CPU %d, ssctl stop error %d.\n",
491 printk(KERN_INFO "hwsampler: CPU %d, CPUMF Sampling stopped.\n", cpu);
496 printk(KERN_ERR "hwsampler: CPU %d CPUMF Request alert,"
497 " count=%lu.\n", cpu, v);
499 v = cb->loss_of_sample_data;
501 printk(KERN_ERR "hwsampler: CPU %d CPUMF Loss of sample data,"
502 " count=%lu.\n", cpu, v);
504 v = cb->invalid_entry_address;
506 printk(KERN_ERR "hwsampler: CPU %d CPUMF Invalid entry address,"
507 " count=%lu.\n", cpu, v);
509 v = cb->incorrect_sdbt_entry;
512 "hwsampler: CPU %d CPUMF Incorrect SDBT address,"
513 " count=%lu.\n", cpu, v);
515 v = cb->sample_auth_change_alert;
518 "hwsampler: CPU %d CPUMF Sample authorization change,"
519 " count=%lu.\n", cpu, v);
524 static int check_hardware_prerequisites(void)
526 if (!test_facility(68))
531 * hws_oom_callback() - the OOM callback function
533 * In case the callback is invoked during memory allocation for the
534 * hw sampler, all obtained memory is deallocated and a flag is set
535 * so main sampler memory allocation can exit with a failure code.
536 * In case the callback is invoked during sampling the hw sampler
537 * is deactivated for all CPUs.
539 static int hws_oom_callback(struct notifier_block *nfb,
540 unsigned long dummy, void *parm)
542 unsigned long *freed;
544 struct hws_cpu_buffer *cb;
548 mutex_lock(&hws_sem_oom);
550 if (hws_state == HWS_DEALLOCATED) {
551 /* during memory allocation */
552 if (oom_killer_was_active == 0) {
553 oom_killer_was_active = 1;
554 *freed += deallocate_sdbt();
559 cb = &per_cpu(sampler_cpu_buffer, cpu);
562 for_each_online_cpu(i) {
563 smp_ctl_ssctl_deactivate(i);
569 "hwsampler: CPU %d, OOM notify during CPUMF Sampling.\n",
574 mutex_unlock(&hws_sem_oom);
579 static struct notifier_block hws_oom_notifier = {
580 .notifier_call = hws_oom_callback
583 static int hws_cpu_callback(struct notifier_block *nfb,
584 unsigned long action, void *hcpu)
586 /* We do not have sampler space available for all possible CPUs.
587 All CPUs should be online when hw sampling is activated. */
588 return (hws_state <= HWS_DEALLOCATED) ? NOTIFY_OK : NOTIFY_BAD;
591 static struct notifier_block hws_cpu_notifier = {
592 .notifier_call = hws_cpu_callback
596 * hwsampler_deactivate() - set hardware sampling temporarily inactive
597 * @cpu: specifies the CPU to be set inactive.
599 * Returns 0 on success, !0 on failure.
601 int hwsampler_deactivate(unsigned int cpu)
604 * Deactivate hw sampling temporarily and flush the buffer
605 * by pushing all the pending samples to oprofile buffer.
607 * This function can be called under one of the following conditions:
608 * Memory unmap, task is exiting.
611 struct hws_cpu_buffer *cb;
614 mutex_lock(&hws_sem);
616 cb = &per_cpu(sampler_cpu_buffer, cpu);
617 if (hws_state == HWS_STARTED) {
618 rc = smp_ctl_qsi(cpu);
621 rc = smp_ctl_ssctl_deactivate(cpu);
624 "hwsampler: CPU %d, CPUMF Deactivation failed.\n", cpu);
626 hws_state = HWS_STOPPING;
629 /* Add work to queue to read pending samples.*/
630 queue_work_on(cpu, hws_wq, &cb->worker);
634 mutex_unlock(&hws_sem);
637 flush_workqueue(hws_wq);
643 * hwsampler_activate() - activate/resume hardware sampling which was deactivated
644 * @cpu: specifies the CPU to be set active.
646 * Returns 0 on success, !0 on failure.
648 int hwsampler_activate(unsigned int cpu)
651 * Re-activate hw sampling. This should be called in pair with
652 * hwsampler_deactivate().
655 struct hws_cpu_buffer *cb;
658 mutex_lock(&hws_sem);
660 cb = &per_cpu(sampler_cpu_buffer, cpu);
661 if (hws_state == HWS_STARTED) {
662 rc = smp_ctl_qsi(cpu);
666 rc = smp_ctl_ssctl_enable_activate(cpu, interval);
669 "CPU %d, CPUMF activate sampling failed.\n",
675 mutex_unlock(&hws_sem);
680 static int check_qsi_on_setup(void)
684 struct hws_cpu_buffer *cb;
686 for_each_online_cpu(cpu) {
687 cb = &per_cpu(sampler_cpu_buffer, cpu);
688 rc = smp_ctl_qsi(cpu);
694 printk(KERN_INFO "hwsampler: CPUMF sampling is not authorized.\n");
699 printk(KERN_WARNING "hwsampler: CPUMF is still enabled.\n");
700 rc = smp_ctl_ssctl_stop(cpu);
705 "CPU %d, CPUMF Sampling stopped now.\n", cpu);
711 static int check_qsi_on_start(void)
715 struct hws_cpu_buffer *cb;
717 for_each_online_cpu(cpu) {
718 cb = &per_cpu(sampler_cpu_buffer, cpu);
719 rc = smp_ctl_qsi(cpu);
734 static void worker_on_start(unsigned int cpu)
736 struct hws_cpu_buffer *cb;
738 cb = &per_cpu(sampler_cpu_buffer, cpu);
739 cb->worker_entry = cb->first_sdbt;
742 static int worker_check_error(unsigned int cpu, int ext_params)
746 struct hws_cpu_buffer *cb;
749 cb = &per_cpu(sampler_cpu_buffer, cpu);
750 sdbt = (unsigned long *) cb->worker_entry;
755 if (ext_params & CPU_MF_INT_SF_PRA)
758 if (ext_params & CPU_MF_INT_SF_LSDA)
759 cb->loss_of_sample_data++;
761 if (ext_params & CPU_MF_INT_SF_IAE) {
762 cb->invalid_entry_address++;
766 if (ext_params & CPU_MF_INT_SF_ISE) {
767 cb->incorrect_sdbt_entry++;
771 if (ext_params & CPU_MF_INT_SF_SACA) {
772 cb->sample_auth_change_alert++;
779 static void worker_on_finish(unsigned int cpu)
782 struct hws_cpu_buffer *cb;
784 cb = &per_cpu(sampler_cpu_buffer, cpu);
787 rc = smp_ctl_qsi(cpu);
791 "hwsampler: CPU %d, CPUMF Stop/Deactivate sampling.\n",
793 rc = smp_ctl_ssctl_stop(cpu);
796 "hwsampler: CPU %d, CPUMF Deactivation failed.\n",
799 for_each_online_cpu(i) {
804 queue_work_on(i, hws_wq,
812 static void worker_on_interrupt(unsigned int cpu)
816 struct hws_cpu_buffer *cb;
818 cb = &per_cpu(sampler_cpu_buffer, cpu);
820 sdbt = (unsigned long *) cb->worker_entry;
823 /* do not proceed if stop was entered,
824 * forget the buffers not yet processed */
825 while (!done && !cb->stop_mode) {
826 unsigned long *trailer;
827 struct hws_trailer_entry *te;
828 unsigned long *dear = 0;
830 trailer = trailer_entry_ptr(*sdbt);
831 /* leave loop if no more work to do */
832 if (!(*trailer & BUFFER_FULL_MASK)) {
838 te = (struct hws_trailer_entry *)trailer;
839 cb->sample_overflow += te->overflow;
841 add_samples_to_oprofile(cpu, sdbt, dear);
844 xchg((unsigned char *) te, 0x40);
846 /* advance to next sdb slot in current sdbt */
848 /* in case link bit is set use address w/o link bit */
849 if (is_link_entry(sdbt))
850 sdbt = get_next_sdbt(sdbt);
852 cb->worker_entry = (unsigned long)sdbt;
856 static void add_samples_to_oprofile(unsigned int cpu, unsigned long *sdbt,
859 struct hws_data_entry *sample_data_ptr;
860 unsigned long *trailer;
862 trailer = trailer_entry_ptr(*sdbt);
869 sample_data_ptr = (struct hws_data_entry *)(*sdbt);
871 while ((unsigned long *)sample_data_ptr < trailer) {
872 struct pt_regs *regs = NULL;
873 struct task_struct *tsk = NULL;
876 * Check sampling mode, 1 indicates basic (=customer) sampling
879 if (sample_data_ptr->def != 1) {
880 /* sample slot is not yet written */
883 /* make sure we don't use it twice,
884 * the next time the sampler will set it again */
885 sample_data_ptr->def = 0;
889 if (sample_data_ptr->P == 1) {
890 /* userspace sample */
891 unsigned int pid = sample_data_ptr->prim_asn;
892 if (!counter_config.user)
895 tsk = pid_task(find_vpid(pid), PIDTYPE_PID);
897 regs = task_pt_regs(tsk);
900 /* kernelspace sample */
901 if (!counter_config.kernel)
903 regs = task_pt_regs(current);
906 mutex_lock(&hws_sem);
907 oprofile_add_ext_hw_sample(sample_data_ptr->ia, regs, 0,
908 !sample_data_ptr->P, tsk);
909 mutex_unlock(&hws_sem);
915 static void worker(struct work_struct *work)
919 struct hws_cpu_buffer *cb;
921 cb = container_of(work, struct hws_cpu_buffer, worker);
922 cpu = smp_processor_id();
923 ext_params = atomic_xchg(&cb->ext_params, 0);
925 if (!cb->worker_entry)
926 worker_on_start(cpu);
928 if (worker_check_error(cpu, ext_params))
932 worker_on_interrupt(cpu);
935 worker_on_finish(cpu);
939 * hwsampler_allocate() - allocate memory for the hardware sampler
940 * @sdbt: number of SDBTs per online CPU (must be > 0)
941 * @sdb: number of SDBs per SDBT (minimum 1, maximum 511)
943 * Returns 0 on success, !0 on failure.
945 int hwsampler_allocate(unsigned long sdbt, unsigned long sdb)
948 mutex_lock(&hws_sem);
951 if (hws_state != HWS_DEALLOCATED)
957 if (sdb > MAX_NUM_SDB || sdb < MIN_NUM_SDB)
963 oom_killer_was_active = 0;
964 register_oom_notifier(&hws_oom_notifier);
966 for_each_online_cpu(cpu) {
967 if (allocate_sdbt(cpu)) {
968 unregister_oom_notifier(&hws_oom_notifier);
972 unregister_oom_notifier(&hws_oom_notifier);
973 if (oom_killer_was_active)
976 hws_state = HWS_STOPPED;
980 mutex_unlock(&hws_sem);
985 printk(KERN_ERR "hwsampler: CPUMF Memory allocation failed.\n");
990 * hwsampler_deallocate() - deallocate hardware sampler memory
992 * Returns 0 on success, !0 on failure.
994 int hwsampler_deallocate(void)
998 mutex_lock(&hws_sem);
1001 if (hws_state != HWS_STOPPED)
1002 goto deallocate_exit;
1004 measurement_alert_subclass_unregister();
1007 hws_state = HWS_DEALLOCATED;
1011 mutex_unlock(&hws_sem);
1016 unsigned long hwsampler_query_min_interval(void)
1018 return min_sampler_rate;
1021 unsigned long hwsampler_query_max_interval(void)
1023 return max_sampler_rate;
1026 unsigned long hwsampler_get_sample_overflow_count(unsigned int cpu)
1028 struct hws_cpu_buffer *cb;
1030 cb = &per_cpu(sampler_cpu_buffer, cpu);
1032 return cb->sample_overflow;
1035 int hwsampler_setup(void)
1039 struct hws_cpu_buffer *cb;
1041 mutex_lock(&hws_sem);
1047 hws_state = HWS_INIT;
1049 init_all_cpu_buffers();
1051 rc = check_hardware_prerequisites();
1055 rc = check_qsi_on_setup();
1060 hws_wq = create_workqueue("hwsampler");
1064 register_cpu_notifier(&hws_cpu_notifier);
1066 for_each_online_cpu(cpu) {
1067 cb = &per_cpu(sampler_cpu_buffer, cpu);
1068 INIT_WORK(&cb->worker, worker);
1069 rc = smp_ctl_qsi(cpu);
1071 if (min_sampler_rate != cb->qsi.min_sampl_rate) {
1072 if (min_sampler_rate) {
1074 "hwsampler: different min sampler rate values.\n");
1075 if (min_sampler_rate < cb->qsi.min_sampl_rate)
1077 cb->qsi.min_sampl_rate;
1079 min_sampler_rate = cb->qsi.min_sampl_rate;
1081 if (max_sampler_rate != cb->qsi.max_sampl_rate) {
1082 if (max_sampler_rate) {
1084 "hwsampler: different max sampler rate values.\n");
1085 if (max_sampler_rate > cb->qsi.max_sampl_rate)
1087 cb->qsi.max_sampl_rate;
1089 max_sampler_rate = cb->qsi.max_sampl_rate;
1092 register_external_interrupt(0x1407, hws_ext_handler);
1094 hws_state = HWS_DEALLOCATED;
1098 mutex_unlock(&hws_sem);
1102 int hwsampler_shutdown(void)
1106 mutex_lock(&hws_sem);
1109 if (hws_state == HWS_DEALLOCATED || hws_state == HWS_STOPPED) {
1110 mutex_unlock(&hws_sem);
1113 flush_workqueue(hws_wq);
1115 mutex_lock(&hws_sem);
1117 if (hws_state == HWS_STOPPED) {
1118 measurement_alert_subclass_unregister();
1122 destroy_workqueue(hws_wq);
1126 unregister_external_interrupt(0x1407, hws_ext_handler);
1127 hws_state = HWS_INIT;
1130 mutex_unlock(&hws_sem);
1132 unregister_cpu_notifier(&hws_cpu_notifier);
1138 * hwsampler_start_all() - start hardware sampling on all online CPUs
1139 * @rate: specifies the used interval when samples are taken
1141 * Returns 0 on success, !0 on failure.
1143 int hwsampler_start_all(unsigned long rate)
1147 mutex_lock(&hws_sem);
1152 if (hws_state != HWS_STOPPED)
1153 goto start_all_exit;
1157 /* fail if rate is not valid */
1158 if (interval < min_sampler_rate || interval > max_sampler_rate)
1159 goto start_all_exit;
1161 rc = check_qsi_on_start();
1163 goto start_all_exit;
1165 rc = prepare_cpu_buffers();
1167 goto start_all_exit;
1169 for_each_online_cpu(cpu) {
1170 rc = start_sampling(cpu);
1175 for_each_online_cpu(cpu) {
1178 goto start_all_exit;
1180 hws_state = HWS_STARTED;
1184 mutex_unlock(&hws_sem);
1189 register_oom_notifier(&hws_oom_notifier);
1192 /* now let them in, 1407 CPUMF external interrupts */
1193 measurement_alert_subclass_register();
1199 * hwsampler_stop_all() - stop hardware sampling on all online CPUs
1201 * Returns 0 on success, !0 on failure.
1203 int hwsampler_stop_all(void)
1205 int tmp_rc, rc, cpu;
1206 struct hws_cpu_buffer *cb;
1208 mutex_lock(&hws_sem);
1211 if (hws_state == HWS_INIT) {
1212 mutex_unlock(&hws_sem);
1215 hws_state = HWS_STOPPING;
1216 mutex_unlock(&hws_sem);
1218 for_each_online_cpu(cpu) {
1219 cb = &per_cpu(sampler_cpu_buffer, cpu);
1221 tmp_rc = stop_sampling(cpu);
1227 flush_workqueue(hws_wq);
1229 mutex_lock(&hws_sem);
1231 unregister_oom_notifier(&hws_oom_notifier);
1234 hws_state = HWS_STOPPED;
1235 mutex_unlock(&hws_sem);