summaryrefslogblamecommitdiff
path: root/arch/um/kernel/time.c
blob: 7678bcf830bae011f52e62eb4d804ae1f22fabcc (plain) (tree)
1
2
3
4
5
6
7
8
9
10
  
                                                                        


                         
                            

                          

                      
                      

               







                                                                  
                                 
                                              
                                                                            

      
                                        

                                     
                                 
                      
                            
                                      
                                                      
 

                                                  

                                                                        
                                 
                                     
 

                                                   
         
                                        
     
                  
      
                           




                                        
                           
                                       














                                                       
                                        
 
                                 

                            

                                                  
                    
 
                                 
                           



                                                                             

                                                            
 
                                                       
 
                           

 



                                

                     
                                                                             
                     
                                                                        

                                             
                             
                     

                                                                         










                                                                     
                                                                          


                                        
                                                     
 
                                     
                                
                            
                    
                                            
                   
                           
 
/*
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
 * Licensed under the GPL
 */

#include "linux/interrupt.h"
#include "linux/jiffies.h"
#include "linux/threads.h"
#include "asm/irq.h"
#include "asm/param.h"
#include "kern_util.h"
#include "os.h"

/*
 * Scheduler clock - returns current time in nanosec units.
 */
unsigned long long sched_clock(void)
{
	return (unsigned long long)jiffies_64 * (1000000000 / HZ);
}

#ifdef CONFIG_UML_REAL_TIME_CLOCK
static unsigned long long prev_nsecs[NR_CPUS];
static long long delta[NR_CPUS];		/* Deviation per interval */
#endif

void timer_irq(struct uml_pt_regs *regs)
{
	unsigned long long ticks = 0;
#ifdef CONFIG_UML_REAL_TIME_CLOCK
	int c = cpu();
	if (prev_nsecs[c]) {
		/* We've had 1 tick */
		unsigned long long nsecs = os_nsecs();

		delta[c] += nsecs - prev_nsecs[c];
		prev_nsecs[c] = nsecs;

		/* Protect against the host clock being set backwards */
		if (delta[c] < 0)
			delta[c] = 0;

		ticks += (delta[c] * HZ) / BILLION;
		delta[c] -= (ticks * BILLION) / HZ;
	}
	else prev_nsecs[c] = os_nsecs();
#else
	ticks = 1;
#endif
	while (ticks > 0) {
		do_IRQ(TIMER_IRQ, regs);
		ticks--;
	}
}

/* Protects local_offset */
static DEFINE_SPINLOCK(timer_spinlock);
static unsigned long long local_offset = 0;

static inline unsigned long long get_time(void)
{
	unsigned long long nsecs;
	unsigned long flags;

	spin_lock_irqsave(&timer_spinlock, flags);
	nsecs = os_nsecs();
	nsecs += local_offset;
	spin_unlock_irqrestore(&timer_spinlock, flags);

	return nsecs;
}

irqreturn_t um_timer(int irq, void *dev)
{
	unsigned long long nsecs;
	unsigned long flags;

	write_seqlock_irqsave(&xtime_lock, flags);

	do_timer(1);

#ifdef CONFIG_UML_REAL_TIME_CLOCK
	nsecs = get_time();
#else
	nsecs = (unsigned long long) xtime.tv_sec * BILLION + xtime.tv_nsec +
		BILLION / HZ;
#endif
	xtime.tv_sec = nsecs / NSEC_PER_SEC;
	xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;

	write_sequnlock_irqrestore(&xtime_lock, flags);

	return IRQ_HANDLED;
}

static void register_timer(void)
{
	int err;

	timer_init();

	err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
	if (err != 0)
		printk(KERN_ERR "register_timer : request_irq failed - "
		       "errno = %d\n", -err);

	err = set_interval();
	if (err != 0)
		printk(KERN_ERR "register_timer : set_interval failed - "
		       "errno = %d\n", -err);
}

extern void (*late_time_init)(void);

void time_init(void)
{
	long long nsecs;

	nsecs = os_nsecs();
	set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
				-nsecs % BILLION);
	set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION);
	late_time_init = register_timer;
}

void timer_handler(int sig, struct uml_pt_regs *regs)
{
	if (current_thread->cpu == 0)
		timer_irq(regs);
	local_irq_disable();
	irq_enter();
	update_process_times(regs->is_user);
	irq_exit();
	local_irq_enable();
}