7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/errno.h>
16 #include <linux/oom.h>
26 #define MAX_NUM_SDB 511
29 #define ALERT_REQ_MASK 0x4000000000000000ul
30 #define BUFFER_FULL_MASK 0x8000000000000000ul
45 static unsigned char hws_flush_all;
46 static unsigned int hws_oom;
49 static unsigned int hws_state;
58 static unsigned char oom_killer_was_active;
60 static unsigned long num_sdbt = 100;
61 static unsigned long num_sdb = 511;
65 static unsigned long min_sampler_rate;
66 static unsigned long max_sampler_rate;
68 static int ssctl(
void *
buffer)
76 "0: .insn s,0xB2870000,0(%1)\n"
81 :
"+d" (
cc),
"+a" (buffer)
88 static int qsi(
void *
buffer)
94 "0: .insn s,0xB2860000,0(%1)\n"
98 : "=
d" (cc), "+
a" (buffer)
105 static
void execute_qsi(
void *
parms)
112 static void execute_ssctl(
void *
parms)
119 static int smp_ctl_ssctl_stop(
int cpu)
125 cb = &
per_cpu(sampler_cpu_buffer, cpu);
130 ep.buffer = &cb->
ssctl;
138 ep.buffer = &cb->
qsi;
141 if (cb->
qsi.es || cb->
qsi.cs) {
149 static int smp_ctl_ssctl_deactivate(
int cpu)
155 cb = &
per_cpu(sampler_cpu_buffer, cpu);
160 ep.buffer = &cb->
ssctl;
166 ep.buffer = &cb->
qsi;
175 static int smp_ctl_ssctl_enable_activate(
int cpu,
unsigned long interval)
181 cb = &
per_cpu(sampler_cpu_buffer, cpu);
186 cb->
ssctl.interval = interval;
190 ep.buffer = &cb->
ssctl;
196 ep.buffer = &cb->
qsi;
204 static int smp_ctl_qsi(
int cpu)
209 cb = &
per_cpu(sampler_cpu_buffer, cpu);
211 ep.buffer = &cb->
qsi;
217 static inline unsigned long *trailer_entry_ptr(
unsigned long v)
225 return (
unsigned long *)
ret;
229 unsigned int param32,
unsigned long param64)
245 static void add_samples_to_oprofile(
unsigned cpu,
unsigned long *,
246 unsigned long *dear);
248 static void init_all_cpu_buffers(
void)
254 cb = &
per_cpu(sampler_cpu_buffer, cpu);
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)
277 cb = &
per_cpu(sampler_cpu_buffer, cpu);
303 static int allocate_sdbt(
int cpu)
309 unsigned long *trailer;
312 cb = &
per_cpu(sampler_cpu_buffer, cpu);
320 for (j = 0; j < num_sdbt; j++) {
326 if (oom_killer_was_active || !sdbt) {
330 goto allocate_sdbt_error;
337 *tail = (
unsigned long)(
void *)sdbt + 1;
341 for (k = 0; k < num_sdb; k++) {
348 if (oom_killer_was_active || !sdb) {
352 goto allocate_sdbt_error;
355 trailer = trailer_entry_ptr(*sdbt);
363 if (oom_killer_was_active)
364 goto allocate_sdbt_error;
368 *tail = (
unsigned long)
377 goto allocate_sdbt_exit;
386 static int deallocate_sdbt(
void)
399 cb = &
per_cpu(sampler_cpu_buffer, cpu);
405 curr = (
unsigned long *) sdbt;
414 if (is_link_entry(curr)) {
415 curr = get_next_sdbt(curr);
420 if ((
unsigned long) curr == start)
423 sdbt = (
unsigned long) curr;
438 static int start_sampling(
int cpu)
443 cb = &
per_cpu(sampler_cpu_buffer, cpu);
444 rc = smp_ctl_ssctl_enable_activate(cpu, interval);
462 "hwsampler: CPU %d, CPUMF Sampling started, interval %lu.\n",
471 static int stop_sampling(
int cpu)
477 rc = smp_ctl_qsi(cpu);
480 cb = &
per_cpu(sampler_cpu_buffer, cpu);
481 if (!rc && !cb->
qsi.es)
484 rc = smp_ctl_ssctl_stop(cpu);
497 " count=%lu.\n", cpu, v);
502 " count=%lu.\n", cpu, v);
507 " count=%lu.\n", cpu, v);
512 "hwsampler: CPU %d CPUMF Incorrect SDBT address,"
513 " count=%lu.\n", cpu, v);
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))
542 unsigned long *freed;
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);
563 smp_ctl_ssctl_deactivate(i);
569 "hwsampler: CPU %d, OOM notify during CPUMF Sampling.\n",
580 .notifier_call = hws_oom_callback
584 unsigned long action,
void *hcpu)
592 .notifier_call = hws_cpu_callback
616 cb = &
per_cpu(sampler_cpu_buffer, cpu);
618 rc = smp_ctl_qsi(cpu);
621 rc = smp_ctl_ssctl_deactivate(cpu);
624 "hwsampler: CPU %d, CPUMF Deactivation failed.\n", cpu);
660 cb = &
per_cpu(sampler_cpu_buffer, cpu);
662 rc = smp_ctl_qsi(cpu);
666 rc = smp_ctl_ssctl_enable_activate(cpu, interval);
669 "CPU %d, CPUMF activate sampling failed.\n",
680 static int check_qsi_on_setup(
void)
687 cb = &
per_cpu(sampler_cpu_buffer, cpu);
688 rc = smp_ctl_qsi(cpu);
700 rc = smp_ctl_ssctl_stop(cpu);
705 "CPU %d, CPUMF Sampling stopped now.\n", cpu);
711 static int check_qsi_on_start(
void)
718 cb = &
per_cpu(sampler_cpu_buffer, cpu);
719 rc = smp_ctl_qsi(cpu);
734 static void worker_on_start(
unsigned int cpu)
738 cb = &
per_cpu(sampler_cpu_buffer, cpu);
742 static int worker_check_error(
unsigned int cpu,
int ext_params)
749 cb = &
per_cpu(sampler_cpu_buffer, cpu);
779 static void worker_on_finish(
unsigned int cpu)
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",
812 static void worker_on_interrupt(
unsigned int cpu)
818 cb = &
per_cpu(sampler_cpu_buffer, cpu);
826 unsigned long *trailer;
828 unsigned long *dear = 0;
830 trailer = trailer_entry_ptr(*sdbt);
841 add_samples_to_oprofile(cpu, sdbt, dear);
844 xchg((
unsigned char *) te, 0x40);
849 if (is_link_entry(sdbt))
850 sdbt = get_next_sdbt(sdbt);
856 static void add_samples_to_oprofile(
unsigned int cpu,
unsigned long *sdbt,
860 unsigned long *trailer;
862 trailer = trailer_entry_ptr(*sdbt);
871 while ((
unsigned long *)sample_data_ptr < trailer) {
879 if (sample_data_ptr->
def != 1) {
885 sample_data_ptr->
def = 0;
889 if (sample_data_ptr->
P == 1) {
908 !sample_data_ptr->
P, tsk);
926 worker_on_start(cpu);
928 if (worker_check_error(cpu, ext_params))
932 worker_on_interrupt(cpu);
935 worker_on_finish(cpu);
963 oom_killer_was_active = 0;
967 if (allocate_sdbt(cpu)) {
973 if (oom_killer_was_active)
1002 goto deallocate_exit;
1018 return min_sampler_rate;
1023 return max_sampler_rate;
1030 cb = &
per_cpu(sampler_cpu_buffer, cpu);
1049 init_all_cpu_buffers();
1051 rc = check_hardware_prerequisites();
1055 rc = check_qsi_on_setup();
1064 register_cpu_notifier(&hws_cpu_notifier);
1067 cb = &
per_cpu(sampler_cpu_buffer, cpu);
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;
1132 unregister_cpu_notifier(&hws_cpu_notifier);
1153 goto start_all_exit;
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;
1170 rc = start_sampling(cpu);
1178 goto start_all_exit;
1205 int tmp_rc,
rc,
cpu;
1219 cb = &
per_cpu(sampler_cpu_buffer, cpu);
1221 tmp_rc = stop_sampling(cpu);