2 * @file nmi_timer_int.c
4 * @remark Copyright 2011 Advanced Micro Devices, Inc.
6 * @author Robert Richter <robert.richter@amd.com>
9 #include <linux/init.h>
10 #include <linux/smp.h>
11 #include <linux/errno.h>
12 #include <linux/oprofile.h>
13 #include <linux/perf_event.h>
15 #ifdef CONFIG_OPROFILE_NMI_TIMER
17 static DEFINE_PER_CPU(struct perf_event *, nmi_timer_events);
18 static int ctr_running;
20 static struct perf_event_attr nmi_timer_attr = {
21 .type = PERF_TYPE_HARDWARE,
22 .config = PERF_COUNT_HW_CPU_CYCLES,
23 .size = sizeof(struct perf_event_attr),
28 static void nmi_timer_callback(struct perf_event *event,
29 struct perf_sample_data *data,
32 event->hw.interrupts = 0; /* don't throttle interrupts */
33 oprofile_add_sample(regs, 0);
36 static int nmi_timer_start_cpu(int cpu)
38 struct perf_event *event = per_cpu(nmi_timer_events, cpu);
41 event = perf_event_create_kernel_counter(&nmi_timer_attr, cpu, NULL,
42 nmi_timer_callback, NULL);
44 return PTR_ERR(event);
45 per_cpu(nmi_timer_events, cpu) = event;
48 if (event && ctr_running)
49 perf_event_enable(event);
54 static void nmi_timer_stop_cpu(int cpu)
56 struct perf_event *event = per_cpu(nmi_timer_events, cpu);
58 if (event && ctr_running)
59 perf_event_disable(event);
62 static int nmi_timer_cpu_online(unsigned int cpu)
64 nmi_timer_start_cpu(cpu);
67 static int nmi_timer_cpu_predown(unsigned int cpu)
69 nmi_timer_stop_cpu(cpu);
73 static int nmi_timer_start(void)
79 for_each_online_cpu(cpu)
80 nmi_timer_start_cpu(cpu);
86 static void nmi_timer_stop(void)
91 for_each_online_cpu(cpu)
92 nmi_timer_stop_cpu(cpu);
97 static enum cpuhp_state hp_online;
99 static void nmi_timer_shutdown(void)
101 struct perf_event *event;
104 cpuhp_remove_state(hp_online);
105 for_each_possible_cpu(cpu) {
106 event = per_cpu(nmi_timer_events, cpu);
109 perf_event_disable(event);
110 per_cpu(nmi_timer_events, cpu) = NULL;
111 perf_event_release_kernel(event);
115 static int nmi_timer_setup(void)
120 /* clock cycles per tick: */
121 period = (u64)cpu_khz * 1000;
123 nmi_timer_attr.sample_period = period;
125 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "oprofile/nmi:online",
126 nmi_timer_cpu_online, nmi_timer_cpu_predown);
128 nmi_timer_shutdown();
135 int __init op_nmi_timer_init(struct oprofile_operations *ops)
139 err = nmi_timer_setup();
142 nmi_timer_shutdown(); /* only check, don't alloc */
144 ops->create_files = NULL;
145 ops->setup = nmi_timer_setup;
146 ops->shutdown = nmi_timer_shutdown;
147 ops->start = nmi_timer_start;
148 ops->stop = nmi_timer_stop;
149 ops->cpu_type = "timer";
151 printk(KERN_INFO "oprofile: using NMI timer interrupt.\n");