Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file timer_int.c |
| 3 | * |
| 4 | * @remark Copyright 2002 OProfile authors |
| 5 | * @remark Read the file COPYING |
| 6 | * |
| 7 | * @author John Levon <levon@movementarian.org> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/notifier.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/oprofile.h> |
| 14 | #include <linux/profile.h> |
| 15 | #include <linux/init.h> |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 16 | #include <linux/cpu.h> |
| 17 | #include <linux/hrtimer.h> |
| 18 | #include <asm/irq_regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/ptrace.h> |
| 20 | |
| 21 | #include "oprof.h" |
| 22 | |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 23 | static DEFINE_PER_CPU(struct hrtimer, oprofile_hrtimer); |
Santosh Shilimkar | 4ac3dbe | 2010-10-27 11:17:15 -0400 | [diff] [blame] | 24 | static int ctr_running; |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 25 | |
| 26 | static enum hrtimer_restart oprofile_hrtimer_notify(struct hrtimer *hrtimer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 28 | oprofile_add_sample(get_irq_regs(), 0); |
| 29 | hrtimer_forward_now(hrtimer, ns_to_ktime(TICK_NSEC)); |
| 30 | return HRTIMER_RESTART; |
| 31 | } |
| 32 | |
| 33 | static void __oprofile_hrtimer_start(void *unused) |
| 34 | { |
Christoph Lameter | 879d927 | 2014-08-17 12:30:31 -0500 | [diff] [blame] | 35 | struct hrtimer *hrtimer = this_cpu_ptr(&oprofile_hrtimer); |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 36 | |
Santosh Shilimkar | 4ac3dbe | 2010-10-27 11:17:15 -0400 | [diff] [blame] | 37 | if (!ctr_running) |
| 38 | return; |
| 39 | |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 40 | hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 41 | hrtimer->function = oprofile_hrtimer_notify; |
| 42 | |
| 43 | hrtimer_start(hrtimer, ns_to_ktime(TICK_NSEC), |
| 44 | HRTIMER_MODE_REL_PINNED); |
| 45 | } |
| 46 | |
| 47 | static int oprofile_hrtimer_start(void) |
| 48 | { |
Santosh Shilimkar | 4ac3dbe | 2010-10-27 11:17:15 -0400 | [diff] [blame] | 49 | get_online_cpus(); |
| 50 | ctr_running = 1; |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 51 | on_each_cpu(__oprofile_hrtimer_start, NULL, 1); |
Santosh Shilimkar | 4ac3dbe | 2010-10-27 11:17:15 -0400 | [diff] [blame] | 52 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 56 | static void __oprofile_hrtimer_stop(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 58 | struct hrtimer *hrtimer = &per_cpu(oprofile_hrtimer, cpu); |
| 59 | |
Santosh Shilimkar | 4ac3dbe | 2010-10-27 11:17:15 -0400 | [diff] [blame] | 60 | if (!ctr_running) |
| 61 | return; |
| 62 | |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 63 | hrtimer_cancel(hrtimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 66 | static void oprofile_hrtimer_stop(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 68 | int cpu; |
| 69 | |
Santosh Shilimkar | 4ac3dbe | 2010-10-27 11:17:15 -0400 | [diff] [blame] | 70 | get_online_cpus(); |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 71 | for_each_online_cpu(cpu) |
| 72 | __oprofile_hrtimer_stop(cpu); |
Santosh Shilimkar | 4ac3dbe | 2010-10-27 11:17:15 -0400 | [diff] [blame] | 73 | ctr_running = 0; |
| 74 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Sebastian Andrzej Siewior | a4e0591 | 2016-09-06 19:04:45 +0200 | [diff] [blame] | 77 | static int oprofile_timer_online(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Sebastian Andrzej Siewior | a4e0591 | 2016-09-06 19:04:45 +0200 | [diff] [blame] | 79 | local_irq_disable(); |
| 80 | __oprofile_hrtimer_start(NULL); |
| 81 | local_irq_enable(); |
| 82 | return 0; |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Sebastian Andrzej Siewior | a4e0591 | 2016-09-06 19:04:45 +0200 | [diff] [blame] | 85 | static int oprofile_timer_prep_down(unsigned int cpu) |
| 86 | { |
| 87 | __oprofile_hrtimer_stop(cpu); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static enum cpuhp_state hp_online; |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 92 | |
Robert Richter | 75c43a2 | 2011-10-14 15:46:10 +0200 | [diff] [blame] | 93 | static int oprofile_hrtimer_setup(void) |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 94 | { |
Sebastian Andrzej Siewior | a4e0591 | 2016-09-06 19:04:45 +0200 | [diff] [blame] | 95 | int ret; |
| 96 | |
| 97 | ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 98 | "oprofile/timer:online", |
| 99 | oprofile_timer_online, |
| 100 | oprofile_timer_prep_down); |
| 101 | if (ret < 0) |
| 102 | return ret; |
| 103 | hp_online = ret; |
| 104 | return 0; |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Robert Richter | 75c43a2 | 2011-10-14 15:46:10 +0200 | [diff] [blame] | 107 | static void oprofile_hrtimer_shutdown(void) |
Martin Schwidefsky | bc078e4 | 2010-03-02 16:01:10 +0100 | [diff] [blame] | 108 | { |
Sebastian Andrzej Siewior | a4e0591 | 2016-09-06 19:04:45 +0200 | [diff] [blame] | 109 | cpuhp_remove_state_nocalls(hp_online); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
Robert Richter | 75c43a2 | 2011-10-14 15:46:10 +0200 | [diff] [blame] | 111 | |
| 112 | int oprofile_timer_init(struct oprofile_operations *ops) |
| 113 | { |
| 114 | ops->create_files = NULL; |
| 115 | ops->setup = oprofile_hrtimer_setup; |
| 116 | ops->shutdown = oprofile_hrtimer_shutdown; |
| 117 | ops->start = oprofile_hrtimer_start; |
| 118 | ops->stop = oprofile_hrtimer_stop; |
| 119 | ops->cpu_type = "timer"; |
| 120 | printk(KERN_INFO "oprofile: using timer interrupt.\n"); |
| 121 | return 0; |
| 122 | } |