aboutsummaryrefslogtreecommitdiffstats
path: root/xen/arch/ia64/xentime.c
blob: 8031bedf34e58500236a9fe8f799dcefba167346 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * xen/arch/ia64/time.c
 *
 * Copyright (C) 2005 Hewlett-Packard Co
 *	Dan Magenheimer <dan.magenheimer@hp.com>
 */

#include <linux/config.h>

#include <linux/cpu.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/profile.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/interrupt.h>
#include <linux/efi.h>
#include <linux/profile.h>
#include <linux/timex.h>

#include <asm/machvec.h>
#include <asm/delay.h>
#include <asm/hw_irq.h>
#include <asm/ptrace.h>
#include <asm/sal.h>
#include <asm/sections.h>
#include <asm/system.h>
#ifdef XEN
#include <linux/jiffies.h>	// not included by xen/sched.h
#endif
#include <xen/softirq.h>

#define TIME_KEEPER_ID  0
extern unsigned long wall_jiffies;

static s_time_t        stime_irq;       /* System time at last 'time update' */

unsigned long domain0_ready = 0;

#ifndef CONFIG_VTI
static inline u64 get_time_delta(void)
{
	return ia64_get_itc();
}
#else // CONFIG_VTI
static s_time_t        stime_irq = 0x0;       /* System time at last 'time update' */
unsigned long itc_scale;
unsigned long itc_at_irq;
static unsigned long   wc_sec, wc_usec; /* UTC time at last 'time update'.   */
//static rwlock_t        time_lock = RW_LOCK_UNLOCKED;
static irqreturn_t vmx_timer_interrupt (int irq, void *dev_id, struct pt_regs *regs);

static inline u64 get_time_delta(void)
{
    s64      delta_itc;
    u64      delta, cur_itc;
    
    cur_itc = ia64_get_itc();

    delta_itc = (s64)(cur_itc - itc_at_irq);
    if ( unlikely(delta_itc < 0) ) delta_itc = 0;
    delta = ((u64)delta_itc) * itc_scale;
    delta = delta >> 32;

    return delta;
}

u64 tick_to_ns(u64 tick)
{
    return (tick * itc_scale) >> 32;
}
#endif // CONFIG_VTI

s_time_t get_s_time(void)
{
    s_time_t now;
    unsigned long flags;

    read_lock_irqsave(&xtime_lock, flags);

    now = stime_irq + get_time_delta();

    /* Ensure that the returned system time is monotonically increasing. */
    {
        static s_time_t prev_now = 0;
        if ( unlikely(now < prev_now) )
            now = prev_now;
        prev_now = now;
    }

    read_unlock_irqrestore(&xtime_lock, flags);

    return now; 
}

void update_dom_time(struct vcpu *v)
{
// FIXME: implement this?
//	printf("update_dom_time: called, not implemented, skipping\n");
	return;
}

/* Set clock to <secs,usecs> after 00:00:00 UTC, 1 January, 1970. */
void do_settime(unsigned long secs, unsigned long usecs, u64 system_time_base)
{
#ifdef  CONFIG_VTI
    s64 delta;
    long _usecs = (long)usecs;

    write_lock_irq(&xtime_lock);

    delta = (s64)(stime_irq - system_time_base);

    _usecs += (long)(delta/1000);
    while ( _usecs >= 1000000 ) 
    {
        _usecs -= 1000000;
        secs++;
    }

    wc_sec  = secs;
    wc_usec = _usecs;

    write_unlock_irq(&xtime_lock);

    update_dom_time(current->domain);
#else
// FIXME: Should this be do_settimeofday (from linux)???
	printf("do_settime: called, not implemented, stopping\n");
	dummy();
#endif
}

irqreturn_t
xen_timer_interrupt (int irq, void *dev_id, struct pt_regs *regs)
{
	unsigned long new_itm;

#define HEARTBEAT_FREQ 16	// period in seconds
#ifdef HEARTBEAT_FREQ
	static long count = 0;
	if (!(++count & ((HEARTBEAT_FREQ*1024)-1))) {
		printf("Heartbeat... iip=%p,psr.i=%d,pend=%d\n",
			regs->cr_iip,
			current->vcpu_info->arch.interrupt_delivery_enabled,
			current->vcpu_info->arch.pending_interruption);
		count = 0;
	}
#endif
#ifndef XEN
	if (unlikely(cpu_is_offline(smp_processor_id()))) {
		return IRQ_HANDLED;
	}
#endif
#ifdef XEN
	if (current->domain == dom0) {
		// FIXME: there's gotta be a better way of doing this...
		// We have to ensure that domain0 is launched before we
		// call vcpu_timer_expired on it
		//domain0_ready = 1; // moved to xensetup.c
		current->vcpu_info->arch.pending_interruption = 1;
	}
	if (domain0_ready && vcpu_timer_expired(dom0->vcpu[0])) {
		vcpu_pend_timer(dom0->vcpu[0]);
		//vcpu_set_next_timer(dom0->vcpu[0]);
		domain_wake(dom0->vcpu[0]);
	}
	if (!is_idle_task(current->domain) && current->domain != dom0) {
		if (vcpu_timer_expired(current)) {
			vcpu_pend_timer(current);
			// ensure another timer interrupt happens even if domain doesn't
			vcpu_set_next_timer(current);
			domain_wake(current);
		}
	}
	raise_actimer_softirq();
#endif

#ifndef XEN
	platform_timer_interrupt(irq, dev_id, regs);
#endif

	new_itm = local_cpu_data->itm_next;

	if (!time_after(ia64_get_itc(), new_itm))
#ifdef XEN
		return;
#else
		printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
		       ia64_get_itc(), new_itm);
#endif

#ifdef XEN
//	printf("GOT TO HERE!!!!!!!!!!!\n");
	//while(1);
#else
	profile_tick(CPU_PROFILING, regs);
#endif

	while (1) {
#ifndef XEN
		update_process_times(user_mode(regs));
#endif

		new_itm += local_cpu_data->itm_delta;

		if (smp_processor_id() == TIME_KEEPER_ID) {
			/*
			 * Here we are in the timer irq handler. We have irqs locally
			 * disabled, but we don't know if the timer_bh is running on
			 * another CPU. We need to avoid to SMP race by acquiring the
			 * xtime_lock.
			 */
#ifdef TURN_ME_OFF_FOR_NOW_IA64_XEN
			write_seqlock(&xtime_lock);
#endif
#ifdef TURN_ME_OFF_FOR_NOW_IA64_XEN
			do_timer(regs);
#endif
			local_cpu_data->itm_next = new_itm;
#ifdef TURN_ME_OFF_FOR_NOW_IA64_XEN
			write_sequnlock(&xtime_lock);
#endif
		} else
			local_cpu_data->itm_next = new_itm;

		if (time_after(new_itm, ia64_get_itc()))
			break;
	}

	do {
		/*
		 * If we're too close to the next clock tick for
		 * comfort, we increase the safety margin by
		 * intentionally dropping the next tick(s).  We do NOT
		 * update itm.next because that would force us to call
		 * do_timer() which in turn would let our clock run
		 * too fast (with the potentially devastating effect
		 * of losing monotony of time).
		 */
		while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
			new_itm += local_cpu_data->itm_delta;
//#ifdef XEN
//		vcpu_set_next_timer(current);
//#else
//printf("***** timer_interrupt: Setting itm to %lx\n",new_itm);
		ia64_set_itm(new_itm);
//#endif
		/* double check, in case we got hit by a (slow) PMI: */
	} while (time_after_eq(ia64_get_itc(), new_itm));
	return IRQ_HANDLED;
}

static struct irqaction xen_timer_irqaction = {
#ifdef CONFIG_VTI
	.handler =	vmx_timer_interrupt,
#else // CONFIG_VTI
	.handler =	xen_timer_interrupt,
#endif // CONFIG_VTI
#ifndef XEN
	.flags =	SA_INTERRUPT,
#endif
	.name =		"timer"
};

void __init
xen_time_init (void)
{
	register_percpu_irq(IA64_TIMER_VECTOR, &xen_timer_irqaction);
	ia64_init_itm();
}


#ifdef CONFIG_VTI

/* Late init function (after all CPUs are booted). */
int __init init_xen_time()
{
    struct timespec tm;

    itc_scale  = 1000000000UL << 32 ;
    itc_scale /= local_cpu_data->itc_freq;

    /* System time ticks from zero. */
    stime_irq = (s_time_t)0;
    itc_at_irq = ia64_get_itc();

    /* Wallclock time starts as the initial RTC time. */
    efi_gettimeofday(&tm);
    wc_sec  = tm.tv_sec;
    wc_usec = tm.tv_nsec/1000;


    printk("Time init:\n");
    printk(".... System Time: %ldns\n", NOW());
    printk(".... scale:       %16lX\n", itc_scale);
    printk(".... Wall Clock:  %lds %ldus\n", wc_sec, wc_usec);

    return 0;
}

static irqreturn_t
vmx_timer_interrupt (int irq, void *dev_id, struct pt_regs *regs)
{
    unsigned long new_itm;
    struct vcpu *v = current;


    new_itm = local_cpu_data->itm_next;

    if (!time_after(ia64_get_itc(), new_itm))
        return;

    while (1) {
#ifdef CONFIG_SMP
        /*
         * For UP, this is done in do_timer().  Weird, but
         * fixing that would require updates to all
         * platforms.
         */
        update_process_times(user_mode(v, regs));
#endif
        new_itm += local_cpu_data->itm_delta;

        if (smp_processor_id() == TIME_KEEPER_ID) {
            /*
             * Here we are in the timer irq handler. We have irqs locally
             * disabled, but we don't know if the timer_bh is running on
             * another CPU. We need to avoid to SMP race by acquiring the
             * xtime_lock.
             */
            local_cpu_data->itm_next = new_itm;
            
            write_lock_irq(&xtime_lock);
            /* Update jiffies counter. */
            (*(unsigned long *)&jiffies_64)++;

            /* Update wall time. */
            wc_usec += 1000000/HZ;
            if ( wc_usec >= 1000000 )
            {
                wc_usec -= 1000000;
                wc_sec++;
            }

            /* Updates system time (nanoseconds since boot). */
            stime_irq += MILLISECS(1000/HZ);
            itc_at_irq = ia64_get_itc();

            write_unlock_irq(&xtime_lock);
            
        } else
            local_cpu_data->itm_next = new_itm;

        if (time_after(new_itm, ia64_get_itc()))
            break;
    }

    do {
        /*
         * If we're too close to the next clock tick for
         * comfort, we increase the safety margin by
         * intentionally dropping the next tick(s).  We do NOT
         * update itm.next because that would force us to call
         * do_timer() which in turn would let our clock run
         * too fast (with the potentially devastating effect
         * of losing monotony of time).
         */
        while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
            new_itm += local_cpu_data->itm_delta;
        ia64_set_itm(new_itm);
        /* double check, in case we got hit by a (slow) PMI: */
    } while (time_after_eq(ia64_get_itc(), new_itm));
    raise_softirq(AC_TIMER_SOFTIRQ);
    
    return IRQ_HANDLED;
}
#endif // CONFIG_VTI