Linux 3.11-rc3
[cris-mirror.git] / arch / s390 / oprofile / hwsampler.c
blobb5b2916895e08dd8b562fb7784628c4a0294f55b
1 /*
2 * Copyright IBM Corp. 2010
3 * Author: Heinz Graalfs <graalfs@de.ibm.com>
4 */
6 #include <linux/kernel_stat.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/smp.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>
21 #include <asm/irq.h>
23 #include "hwsampler.h"
24 #include "op_counter.h"
26 #define MAX_NUM_SDB 511
27 #define MIN_NUM_SDB 1
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 {
35 void *buffer;
36 signed int rc;
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;
50 enum {
51 HWS_INIT = 1,
52 HWS_DEALLOCATED,
53 HWS_STOPPED,
54 HWS_STARTED,
55 HWS_STOPPING };
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)
70 int cc;
72 /* set in order to detect a program check */
73 cc = 1;
75 asm volatile(
76 "0: .insn s,0xB2870000,0(%1)\n"
77 "1: ipm %0\n"
78 " srl %0,28\n"
79 "2:\n"
80 EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
81 : "+d" (cc), "+a" (buffer)
82 : "m" (*((struct hws_ssctl_request_block *)buffer))
83 : "cc", "memory");
85 return cc ? -EINVAL : 0 ;
88 static int qsi(void *buffer)
90 int cc;
91 cc = 1;
93 asm volatile(
94 "0: .insn s,0xB2860000,0(%1)\n"
95 "1: lhi %0,0\n"
96 "2:\n"
97 EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
98 : "=d" (cc), "+a" (buffer)
99 : "m" (*((struct hws_qsi_info_block *)buffer))
100 : "cc", "memory");
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)
121 int rc;
122 struct hws_execute_parms ep;
123 struct hws_cpu_buffer *cb;
125 cb = &per_cpu(sampler_cpu_buffer, cpu);
127 cb->ssctl.es = 0;
128 cb->ssctl.cs = 0;
130 ep.buffer = &cb->ssctl;
131 smp_call_function_single(cpu, execute_ssctl, &ep, 1);
132 rc = ep.rc;
133 if (rc) {
134 printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
135 dump_stack();
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");
143 dump_stack();
146 return rc;
149 static int smp_ctl_ssctl_deactivate(int cpu)
151 int rc;
152 struct hws_execute_parms ep;
153 struct hws_cpu_buffer *cb;
155 cb = &per_cpu(sampler_cpu_buffer, cpu);
157 cb->ssctl.es = 1;
158 cb->ssctl.cs = 0;
160 ep.buffer = &cb->ssctl;
161 smp_call_function_single(cpu, execute_ssctl, &ep, 1);
162 rc = ep.rc;
163 if (rc)
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);
169 if (cb->qsi.cs)
170 printk(KERN_EMERG "CPUMF sampling was not set inactive.\n");
172 return rc;
175 static int smp_ctl_ssctl_enable_activate(int cpu, unsigned long interval)
177 int rc;
178 struct hws_execute_parms ep;
179 struct hws_cpu_buffer *cb;
181 cb = &per_cpu(sampler_cpu_buffer, cpu);
183 cb->ssctl.h = 1;
184 cb->ssctl.tear = cb->first_sdbt;
185 cb->ssctl.dear = *(unsigned long *) cb->first_sdbt;
186 cb->ssctl.interval = interval;
187 cb->ssctl.es = 1;
188 cb->ssctl.cs = 1;
190 ep.buffer = &cb->ssctl;
191 smp_call_function_single(cpu, execute_ssctl, &ep, 1);
192 rc = ep.rc;
193 if (rc)
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);
198 if (ep.rc)
199 printk(KERN_ERR "hwsampler: CPU %d CPUMF QSI failed.\n", cpu);
201 return rc;
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);
214 return ep.rc;
217 static inline unsigned long *trailer_entry_ptr(unsigned long v)
219 void *ret;
221 ret = (void *)v;
222 ret += PAGE_SIZE;
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))
234 return;
236 inc_irq_stat(IRQEXT_CMS);
237 atomic_xchg(&cb->ext_params, atomic_read(&cb->ext_params) | param32);
239 if (hws_wq)
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)
250 int cpu;
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)
271 int cpu;
272 int rc;
273 struct hws_cpu_buffer *cb;
275 rc = 0;
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;
281 cb->req_alert = 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;
286 cb->finish = 0;
287 cb->oom = 0;
288 cb->stop_mode = 0;
291 return rc;
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)
305 int j, k, rc;
306 unsigned long *sdbt;
307 unsigned long sdb;
308 unsigned long *tail;
309 unsigned long *trailer;
310 struct hws_cpu_buffer *cb;
312 cb = &per_cpu(sampler_cpu_buffer, cpu);
314 if (cb->first_sdbt)
315 return -EINVAL;
317 sdbt = NULL;
318 tail = sdbt;
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 */
325 barrier();
326 if (oom_killer_was_active || !sdbt) {
327 if (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 */
336 if (tail)
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 */
347 barrier();
348 if (oom_killer_was_active || !sdb) {
349 if (sdb)
350 free_page(sdb);
352 goto allocate_sdbt_error;
354 *sdbt = sdb;
355 trailer = trailer_entry_ptr(*sdbt);
356 *trailer = ALERT_REQ_MASK;
357 sdbt++;
358 mutex_unlock(&hws_sem_oom);
360 tail = sdbt;
362 mutex_lock(&hws_sem_oom);
363 if (oom_killer_was_active)
364 goto allocate_sdbt_error;
366 rc = 0;
367 if (tail)
368 *tail = (unsigned long)
369 ((void *)cb->first_sdbt) + 1;
371 allocate_sdbt_exit:
372 mutex_unlock(&hws_sem_oom);
373 return rc;
375 allocate_sdbt_error:
376 rc = -ENOMEM;
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)
388 int cpu;
389 int counter;
391 counter = 0;
393 for_each_online_cpu(cpu) {
394 unsigned long start;
395 unsigned long sdbt;
396 unsigned long *curr;
397 struct hws_cpu_buffer *cb;
399 cb = &per_cpu(sampler_cpu_buffer, cpu);
401 if (!cb->first_sdbt)
402 continue;
404 sdbt = cb->first_sdbt;
405 curr = (unsigned long *) sdbt;
406 start = sdbt;
408 /* we'll free the SDBT after all SDBs are processed... */
409 while (1) {
410 if (!*curr || !sdbt)
411 break;
413 /* watch for link entry reset if found */
414 if (is_link_entry(curr)) {
415 curr = get_next_sdbt(curr);
416 if (sdbt)
417 free_page(sdbt);
419 /* we are done if we reach the start */
420 if ((unsigned long) curr == start)
421 break;
422 else
423 sdbt = (unsigned long) curr;
424 } else {
425 /* process SDB pointer */
426 if (*curr) {
427 free_page(*curr);
428 curr++;
431 counter++;
433 cb->first_sdbt = 0;
435 return counter;
438 static int start_sampling(int cpu)
440 int rc;
441 struct hws_cpu_buffer *cb;
443 cb = &per_cpu(sampler_cpu_buffer, cpu);
444 rc = smp_ctl_ssctl_enable_activate(cpu, interval);
445 if (rc) {
446 printk(KERN_INFO "hwsampler: CPU %d ssctl failed.\n", cpu);
447 goto start_exit;
450 rc = -EINVAL;
451 if (!cb->qsi.es) {
452 printk(KERN_INFO "hwsampler: CPU %d ssctl not enabled.\n", cpu);
453 goto start_exit;
456 if (!cb->qsi.cs) {
457 printk(KERN_INFO "hwsampler: CPU %d ssctl not active.\n", cpu);
458 goto start_exit;
461 printk(KERN_INFO
462 "hwsampler: CPU %d, CPUMF Sampling started, interval %lu.\n",
463 cpu, interval);
465 rc = 0;
467 start_exit:
468 return rc;
471 static int stop_sampling(int cpu)
473 unsigned long v;
474 int rc;
475 struct hws_cpu_buffer *cb;
477 rc = smp_ctl_qsi(cpu);
478 WARN_ON(rc);
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);
485 if (rc) {
486 printk(KERN_INFO "hwsampler: CPU %d, ssctl stop error %d.\n",
487 cpu, rc);
488 goto stop_exit;
491 printk(KERN_INFO "hwsampler: CPU %d, CPUMF Sampling stopped.\n", cpu);
493 stop_exit:
494 v = cb->req_alert;
495 if (v)
496 printk(KERN_ERR "hwsampler: CPU %d CPUMF Request alert,"
497 " count=%lu.\n", cpu, v);
499 v = cb->loss_of_sample_data;
500 if (v)
501 printk(KERN_ERR "hwsampler: CPU %d CPUMF Loss of sample data,"
502 " count=%lu.\n", cpu, v);
504 v = cb->invalid_entry_address;
505 if (v)
506 printk(KERN_ERR "hwsampler: CPU %d CPUMF Invalid entry address,"
507 " count=%lu.\n", cpu, v);
509 v = cb->incorrect_sdbt_entry;
510 if (v)
511 printk(KERN_ERR
512 "hwsampler: CPU %d CPUMF Incorrect SDBT address,"
513 " count=%lu.\n", cpu, v);
515 v = cb->sample_auth_change_alert;
516 if (v)
517 printk(KERN_ERR
518 "hwsampler: CPU %d CPUMF Sample authorization change,"
519 " count=%lu.\n", cpu, v);
521 return rc;
524 static int check_hardware_prerequisites(void)
526 if (!test_facility(68))
527 return -EOPNOTSUPP;
528 return 0;
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;
543 int cpu;
544 struct hws_cpu_buffer *cb;
546 freed = parm;
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();
556 } else {
557 int i;
558 cpu = get_cpu();
559 cb = &per_cpu(sampler_cpu_buffer, cpu);
561 if (!cb->oom) {
562 for_each_online_cpu(i) {
563 smp_ctl_ssctl_deactivate(i);
564 cb->oom = 1;
566 cb->finish = 1;
568 printk(KERN_INFO
569 "hwsampler: CPU %d, OOM notify during CPUMF Sampling.\n",
570 cpu);
574 mutex_unlock(&hws_sem_oom);
576 return NOTIFY_OK;
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.
610 int rc;
611 struct hws_cpu_buffer *cb;
613 rc = 0;
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);
619 WARN_ON(rc);
620 if (cb->qsi.cs) {
621 rc = smp_ctl_ssctl_deactivate(cpu);
622 if (rc) {
623 printk(KERN_INFO
624 "hwsampler: CPU %d, CPUMF Deactivation failed.\n", cpu);
625 cb->finish = 1;
626 hws_state = HWS_STOPPING;
627 } else {
628 hws_flush_all = 1;
629 /* Add work to queue to read pending samples.*/
630 queue_work_on(cpu, hws_wq, &cb->worker);
634 mutex_unlock(&hws_sem);
636 if (hws_wq)
637 flush_workqueue(hws_wq);
639 return rc;
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().
654 int rc;
655 struct hws_cpu_buffer *cb;
657 rc = 0;
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);
663 WARN_ON(rc);
664 if (!cb->qsi.cs) {
665 hws_flush_all = 0;
666 rc = smp_ctl_ssctl_enable_activate(cpu, interval);
667 if (rc) {
668 printk(KERN_ERR
669 "CPU %d, CPUMF activate sampling failed.\n",
670 cpu);
675 mutex_unlock(&hws_sem);
677 return rc;
680 static int check_qsi_on_setup(void)
682 int rc;
683 unsigned int cpu;
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);
689 WARN_ON(rc);
690 if (rc)
691 return -EOPNOTSUPP;
693 if (!cb->qsi.as) {
694 printk(KERN_INFO "hwsampler: CPUMF sampling is not authorized.\n");
695 return -EINVAL;
698 if (cb->qsi.es) {
699 printk(KERN_WARNING "hwsampler: CPUMF is still enabled.\n");
700 rc = smp_ctl_ssctl_stop(cpu);
701 if (rc)
702 return -EINVAL;
704 printk(KERN_INFO
705 "CPU %d, CPUMF Sampling stopped now.\n", cpu);
708 return 0;
711 static int check_qsi_on_start(void)
713 unsigned int cpu;
714 int rc;
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);
720 WARN_ON(rc);
722 if (!cb->qsi.as)
723 return -EINVAL;
725 if (cb->qsi.es)
726 return -EINVAL;
728 if (cb->qsi.cs)
729 return -EINVAL;
731 return 0;
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)
744 int rc;
745 unsigned long *sdbt;
746 struct hws_cpu_buffer *cb;
748 rc = 0;
749 cb = &per_cpu(sampler_cpu_buffer, cpu);
750 sdbt = (unsigned long *) cb->worker_entry;
752 if (!sdbt || !*sdbt)
753 return -EINVAL;
755 if (ext_params & CPU_MF_INT_SF_PRA)
756 cb->req_alert++;
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++;
763 rc = -EINVAL;
766 if (ext_params & CPU_MF_INT_SF_ISE) {
767 cb->incorrect_sdbt_entry++;
768 rc = -EINVAL;
771 if (ext_params & CPU_MF_INT_SF_SACA) {
772 cb->sample_auth_change_alert++;
773 rc = -EINVAL;
776 return rc;
779 static void worker_on_finish(unsigned int cpu)
781 int rc, i;
782 struct hws_cpu_buffer *cb;
784 cb = &per_cpu(sampler_cpu_buffer, cpu);
786 if (cb->finish) {
787 rc = smp_ctl_qsi(cpu);
788 WARN_ON(rc);
789 if (cb->qsi.es) {
790 printk(KERN_INFO
791 "hwsampler: CPU %d, CPUMF Stop/Deactivate sampling.\n",
792 cpu);
793 rc = smp_ctl_ssctl_stop(cpu);
794 if (rc)
795 printk(KERN_INFO
796 "hwsampler: CPU %d, CPUMF Deactivation failed.\n",
797 cpu);
799 for_each_online_cpu(i) {
800 if (i == cpu)
801 continue;
802 if (!cb->finish) {
803 cb->finish = 1;
804 queue_work_on(i, hws_wq,
805 &cb->worker);
812 static void worker_on_interrupt(unsigned int cpu)
814 unsigned long *sdbt;
815 unsigned char done;
816 struct hws_cpu_buffer *cb;
818 cb = &per_cpu(sampler_cpu_buffer, cpu);
820 sdbt = (unsigned long *) cb->worker_entry;
822 done = 0;
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)) {
833 done = 1;
834 if (!hws_flush_all)
835 continue;
838 te = (struct hws_trailer_entry *)trailer;
839 cb->sample_overflow += te->overflow;
841 add_samples_to_oprofile(cpu, sdbt, dear);
843 /* reset trailer */
844 xchg((unsigned char *) te, 0x40);
846 /* advance to next sdb slot in current sdbt */
847 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,
857 unsigned long *dear)
859 struct hws_data_entry *sample_data_ptr;
860 unsigned long *trailer;
862 trailer = trailer_entry_ptr(*sdbt);
863 if (dear) {
864 if (dear > trailer)
865 return;
866 trailer = dear;
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
877 * mode.
879 if (sample_data_ptr->def != 1) {
880 /* sample slot is not yet written */
881 break;
882 } else {
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;
888 /* Get pt_regs. */
889 if (sample_data_ptr->P == 1) {
890 /* userspace sample */
891 unsigned int pid = sample_data_ptr->prim_asn;
892 if (!counter_config.user)
893 goto skip_sample;
894 rcu_read_lock();
895 tsk = pid_task(find_vpid(pid), PIDTYPE_PID);
896 if (tsk)
897 regs = task_pt_regs(tsk);
898 rcu_read_unlock();
899 } else {
900 /* kernelspace sample */
901 if (!counter_config.kernel)
902 goto skip_sample;
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);
910 skip_sample:
911 sample_data_ptr++;
915 static void worker(struct work_struct *work)
917 unsigned int cpu;
918 int ext_params;
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))
929 return;
931 if (!cb->finish)
932 worker_on_interrupt(cpu);
934 if (cb->finish)
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)
947 int cpu, rc;
948 mutex_lock(&hws_sem);
950 rc = -EINVAL;
951 if (hws_state != HWS_DEALLOCATED)
952 goto allocate_exit;
954 if (sdbt < 1)
955 goto allocate_exit;
957 if (sdb > MAX_NUM_SDB || sdb < MIN_NUM_SDB)
958 goto allocate_exit;
960 num_sdbt = sdbt;
961 num_sdb = 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);
969 goto allocate_error;
972 unregister_oom_notifier(&hws_oom_notifier);
973 if (oom_killer_was_active)
974 goto allocate_error;
976 hws_state = HWS_STOPPED;
977 rc = 0;
979 allocate_exit:
980 mutex_unlock(&hws_sem);
981 return rc;
983 allocate_error:
984 rc = -ENOMEM;
985 printk(KERN_ERR "hwsampler: CPUMF Memory allocation failed.\n");
986 goto allocate_exit;
990 * hwsampler_deallocate() - deallocate hardware sampler memory
992 * Returns 0 on success, !0 on failure.
994 int hwsampler_deallocate(void)
996 int rc;
998 mutex_lock(&hws_sem);
1000 rc = -EINVAL;
1001 if (hws_state != HWS_STOPPED)
1002 goto deallocate_exit;
1004 measurement_alert_subclass_unregister();
1005 deallocate_sdbt();
1007 hws_state = HWS_DEALLOCATED;
1008 rc = 0;
1010 deallocate_exit:
1011 mutex_unlock(&hws_sem);
1013 return rc;
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)
1037 int rc;
1038 int cpu;
1039 struct hws_cpu_buffer *cb;
1041 mutex_lock(&hws_sem);
1043 rc = -EINVAL;
1044 if (hws_state)
1045 goto setup_exit;
1047 hws_state = HWS_INIT;
1049 init_all_cpu_buffers();
1051 rc = check_hardware_prerequisites();
1052 if (rc)
1053 goto setup_exit;
1055 rc = check_qsi_on_setup();
1056 if (rc)
1057 goto setup_exit;
1059 rc = -EINVAL;
1060 hws_wq = create_workqueue("hwsampler");
1061 if (!hws_wq)
1062 goto setup_exit;
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);
1070 WARN_ON(rc);
1071 if (min_sampler_rate != cb->qsi.min_sampl_rate) {
1072 if (min_sampler_rate) {
1073 printk(KERN_WARNING
1074 "hwsampler: different min sampler rate values.\n");
1075 if (min_sampler_rate < cb->qsi.min_sampl_rate)
1076 min_sampler_rate =
1077 cb->qsi.min_sampl_rate;
1078 } else
1079 min_sampler_rate = cb->qsi.min_sampl_rate;
1081 if (max_sampler_rate != cb->qsi.max_sampl_rate) {
1082 if (max_sampler_rate) {
1083 printk(KERN_WARNING
1084 "hwsampler: different max sampler rate values.\n");
1085 if (max_sampler_rate > cb->qsi.max_sampl_rate)
1086 max_sampler_rate =
1087 cb->qsi.max_sampl_rate;
1088 } else
1089 max_sampler_rate = cb->qsi.max_sampl_rate;
1092 register_external_interrupt(0x1407, hws_ext_handler);
1094 hws_state = HWS_DEALLOCATED;
1095 rc = 0;
1097 setup_exit:
1098 mutex_unlock(&hws_sem);
1099 return rc;
1102 int hwsampler_shutdown(void)
1104 int rc;
1106 mutex_lock(&hws_sem);
1108 rc = -EINVAL;
1109 if (hws_state == HWS_DEALLOCATED || hws_state == HWS_STOPPED) {
1110 mutex_unlock(&hws_sem);
1112 if (hws_wq)
1113 flush_workqueue(hws_wq);
1115 mutex_lock(&hws_sem);
1117 if (hws_state == HWS_STOPPED) {
1118 measurement_alert_subclass_unregister();
1119 deallocate_sdbt();
1121 if (hws_wq) {
1122 destroy_workqueue(hws_wq);
1123 hws_wq = NULL;
1126 unregister_external_interrupt(0x1407, hws_ext_handler);
1127 hws_state = HWS_INIT;
1128 rc = 0;
1130 mutex_unlock(&hws_sem);
1132 unregister_cpu_notifier(&hws_cpu_notifier);
1134 return rc;
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)
1145 int rc, cpu;
1147 mutex_lock(&hws_sem);
1149 hws_oom = 0;
1151 rc = -EINVAL;
1152 if (hws_state != HWS_STOPPED)
1153 goto start_all_exit;
1155 interval = rate;
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();
1162 if (rc)
1163 goto start_all_exit;
1165 rc = prepare_cpu_buffers();
1166 if (rc)
1167 goto start_all_exit;
1169 for_each_online_cpu(cpu) {
1170 rc = start_sampling(cpu);
1171 if (rc)
1172 break;
1174 if (rc) {
1175 for_each_online_cpu(cpu) {
1176 stop_sampling(cpu);
1178 goto start_all_exit;
1180 hws_state = HWS_STARTED;
1181 rc = 0;
1183 start_all_exit:
1184 mutex_unlock(&hws_sem);
1186 if (rc)
1187 return rc;
1189 register_oom_notifier(&hws_oom_notifier);
1190 hws_oom = 1;
1191 hws_flush_all = 0;
1192 /* now let them in, 1407 CPUMF external interrupts */
1193 measurement_alert_subclass_register();
1195 return 0;
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);
1210 rc = 0;
1211 if (hws_state == HWS_INIT) {
1212 mutex_unlock(&hws_sem);
1213 return rc;
1215 hws_state = HWS_STOPPING;
1216 mutex_unlock(&hws_sem);
1218 for_each_online_cpu(cpu) {
1219 cb = &per_cpu(sampler_cpu_buffer, cpu);
1220 cb->stop_mode = 1;
1221 tmp_rc = stop_sampling(cpu);
1222 if (tmp_rc)
1223 rc = tmp_rc;
1226 if (hws_wq)
1227 flush_workqueue(hws_wq);
1229 mutex_lock(&hws_sem);
1230 if (hws_oom) {
1231 unregister_oom_notifier(&hws_oom_notifier);
1232 hws_oom = 0;
1234 hws_state = HWS_STOPPED;
1235 mutex_unlock(&hws_sem);
1237 return rc;