summaryrefslogtreecommitdiff
path: root/drivers/oprofile/timer_int.c
blob: 246c24a99b5ec55cdff1c675ee34dd116b832f19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
 * @file timer_int.c
 *
 * @remark Copyright 2002 OProfile authors
 * @remark Read the file COPYING
 *
 * @author John Levon <levon@movementarian.org>
 */

#include <linux/kernel.h>
#include <linux/notifier.h>
#include <linux/smp.h>
#include <linux/oprofile.h>
#include <linux/profile.h>
#include <linux/init.h>
#include <linux/cpu.h>
#include <linux/hrtimer.h>
#include <asm/irq_regs.h>
#include <asm/ptrace.h>

/* 2013-01-14 YJChen: Add Begin */
#include <asm/mach/irq.h>
#include <linux/rtc.h>
#include <mach/hardware.h>
/* 2013-01-14 YJChen: Add End */

#include "oprof.h"

static DEFINE_PER_CPU(struct hrtimer, oprofile_hrtimer);
static int ctr_running;

static enum hrtimer_restart oprofile_hrtimer_notify(struct hrtimer *hrtimer)
{
	oprofile_add_sample(get_irq_regs(), 0);
	hrtimer_forward_now(hrtimer, ns_to_ktime(TICK_NSEC));
	return HRTIMER_RESTART;
}

static void __oprofile_hrtimer_start(void *unused)
{
	struct hrtimer *hrtimer = &__get_cpu_var(oprofile_hrtimer);

	if (!ctr_running)
		return;

	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	hrtimer->function = oprofile_hrtimer_notify;

	hrtimer_start(hrtimer, ns_to_ktime(TICK_NSEC),
		      HRTIMER_MODE_REL_PINNED);
}

static int oprofile_hrtimer_start(void)
{
	get_online_cpus();
	ctr_running = 1;
	on_each_cpu(__oprofile_hrtimer_start, NULL, 1);
	put_online_cpus();
	return 0;
}

static void __oprofile_hrtimer_stop(int cpu)
{
	struct hrtimer *hrtimer = &per_cpu(oprofile_hrtimer, cpu);

	if (!ctr_running)
		return;

	hrtimer_cancel(hrtimer);
}

static void oprofile_hrtimer_stop(void)
{
	int cpu;

	get_online_cpus();
	for_each_online_cpu(cpu)
		__oprofile_hrtimer_stop(cpu);
	ctr_running = 0;
	put_online_cpus();
}

static int __cpuinit oprofile_cpu_notify(struct notifier_block *self,
					 unsigned long action, void *hcpu)
{
	long cpu = (long) hcpu;

	switch (action) {
	case CPU_ONLINE:
	case CPU_ONLINE_FROZEN:
		smp_call_function_single(cpu, __oprofile_hrtimer_start,
					 NULL, 1);
		break;
	case CPU_DEAD:
	case CPU_DEAD_FROZEN:
		__oprofile_hrtimer_stop(cpu);
		break;
	}
	return NOTIFY_OK;
}

static struct notifier_block __refdata oprofile_cpu_notifier = {
	.notifier_call = oprofile_cpu_notify,
};

static int oprofile_hrtimer_setup(void)
{
	return register_hotcpu_notifier(&oprofile_cpu_notifier);
}

static void oprofile_hrtimer_shutdown(void)
{
	unregister_hotcpu_notifier(&oprofile_cpu_notifier);
}

int oprofile_timer_init(struct oprofile_operations *ops)
{
	ops->create_files	= NULL;
	ops->setup		= oprofile_hrtimer_setup;
	ops->shutdown	= oprofile_hrtimer_shutdown;
	ops->start		= oprofile_hrtimer_start;
	ops->stop		= oprofile_hrtimer_stop;
	ops->cpu_type	= "timer";
	printk(KERN_INFO "oprofile: using timer interrupt.\n");
	return 0;
}

/* 2013-01-14 YJChen: Add Begin */
extern unsigned int wmt_read_oscr(void);

static int timer_notify(struct pt_regs *regs)
{
	oprofile_add_sample(regs, 0);
	return 0;
}

static irqreturn_t
wmt_timer2_interrupt(int irq, void *dev_id)
{
    unsigned int next_match;
    int flag = 0;
    do {
        struct pt_regs *regs;

        if (flag == 1)
            printk(KERN_INFO "wmt_timer2_interrupt while\n");

        flag = 1;

        //struct pt_regs *regs = get_irq_regs(); // fix compile warning: ISO C90 forbids mixed declarations and code
        regs = get_irq_regs();
        timer_notify(regs);

        /* Clear match on OS Timer 2 */
        OSTS_VAL = OSTS_M2;
        next_match = OSM2_VAL = wmt_read_oscr() + TIMER2_LATCH;
    } while ((signed long)(next_match - wmt_read_oscr()) <= 10/*0*/);

    return IRQ_HANDLED;
}

static int timer2_start(void)
{
    unsigned int nCount;
    unsigned int i = 0;

    /* Use OS Timer 2 as oprofile timer. */
    i = request_irq(IRQ_OST2, wmt_timer2_interrupt, IRQF_DISABLED, "timer2", NULL);
    if (i != 0) {
        printk(KERN_ERR "oprofile: unable to request IRQ%u for timer2\n", IRQ_OST2);
        return i;
    }

    nCount = wmt_read_oscr();
    //printk(KERN_INFO "%s Enter nCount=%d\n", __FUNCTION__, nCount);

    /* Set initial match */
    while (OSTA_VAL & OSTA_MWA2)
        ;

    /* Disable match on timer 2 to cause interrupts. */
    OSTI_VAL &= ~OSTI_E2;

    OSM2_VAL = nCount + TIMER2_LATCH;

    /* Enable match on timer 2 to cause interrupts. */
    OSTI_VAL |= OSTI_E2;

    /* Let OS Timer free run. */
    if ((OSTC_VAL & OSTC_ENABLE) == 0)
        OSTC_VAL |= OSTC_ENABLE;

	return 0;
}

static void timer2_stop(void)
{
    /* Disable match on timer 2 to cause interrupts. */
    OSTI_VAL &= ~OSTI_E2;

    /* Set initial match */
    while (OSTA_VAL & OSTA_MWA2)
        ;

    OSM2_VAL = 0;

    free_irq(IRQ_OST2, NULL);
}

int __init oprofile_timer2_init(struct oprofile_operations *ops)
{
	int rc;

	rc = register_hotcpu_notifier(&oprofile_cpu_notifier);
	if (rc)
		return rc;

    ops->create_files = NULL;
    ops->setup = oprofile_hrtimer_setup;
    ops->shutdown = oprofile_hrtimer_shutdown;
    ops->start = timer2_start;
    ops->stop = timer2_stop;
    ops->cpu_type = "timer";
	printk(KERN_INFO "oprofile: using timer2 interrupt.\n");
    return 0;
}
/* 2013-01-14 YJChen: Add End */