ia64/pv_ops/xen: implement xen pv_time_ops.
[pv_ops_mirror.git] / arch / ia64 / xen / time.c
blob43d483d3a4dad20fa893204e1e45bd218da78abb
1 /******************************************************************************
2 * arch/ia64/xen/time.c
4 * Copyright (c) 2008 Isaku Yamahata <yamahata at valinux co jp>
5 * VA Linux Systems Japan K.K.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/delay.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/posix-timers.h>
26 #include <linux/irq.h>
27 #include <linux/clocksource.h>
29 #include <asm/xen/hypervisor.h>
31 #include <xen/interface/vcpu.h>
33 #include "../kernel/fsyscall_gtod_data.h"
35 DEFINE_PER_CPU(struct vcpu_runstate_info, runstate);
36 DEFINE_PER_CPU(unsigned long, processed_stolen_time);
37 DEFINE_PER_CPU(unsigned long, processed_blocked_time);
39 /* taken from i386/kernel/time-xen.c */
40 static void xen_init_missing_ticks_accounting(int cpu)
42 struct vcpu_register_runstate_memory_area area;
43 struct vcpu_runstate_info *runstate = &per_cpu(runstate, cpu);
44 int rc;
46 memset(runstate, 0, sizeof(*runstate));
48 area.addr.v = runstate;
49 rc = HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area, cpu,
50 &area);
51 WARN_ON(rc && rc != -ENOSYS);
53 per_cpu(processed_blocked_time, cpu) = runstate->time[RUNSTATE_blocked];
54 per_cpu(processed_stolen_time, cpu) = runstate->time[RUNSTATE_runnable]
55 + runstate->time[RUNSTATE_offline];
58 #define NS_PER_TICK (1000000000LL/HZ)
60 static unsigned long
61 consider_steal_time(unsigned long new_itm)
63 unsigned long stolen, blocked, sched_time;
64 unsigned long delta_itm = 0, stolentick = 0;
65 int cpu = smp_processor_id();
66 struct vcpu_runstate_info *runstate;
67 struct task_struct *p = current;
69 runstate = &per_cpu(runstate, smp_processor_id());
71 do {
72 sched_time = runstate->state_entry_time;
73 mb();
74 stolen = runstate->time[RUNSTATE_runnable] +
75 runstate->time[RUNSTATE_offline] -
76 per_cpu(processed_stolen_time, cpu);
77 blocked = runstate->time[RUNSTATE_blocked] -
78 per_cpu(processed_blocked_time, cpu);
79 mb();
80 } while (sched_time != runstate->state_entry_time);
83 * Check for vcpu migration effect
84 * In this case, itc value is reversed.
85 * This causes huge stolen value.
86 * This function just checks and reject this effect.
88 if (!time_after_eq(runstate->time[RUNSTATE_blocked],
89 per_cpu(processed_blocked_time, cpu)))
90 blocked = 0;
92 if (!time_after_eq(runstate->time[RUNSTATE_runnable] +
93 runstate->time[RUNSTATE_offline],
94 per_cpu(processed_stolen_time, cpu)))
95 stolen = 0;
97 if (!time_after(delta_itm + new_itm, ia64_get_itc()))
98 stolentick = ia64_get_itc() - delta_itm - new_itm;
100 do_div(stolentick, NS_PER_TICK);
101 stolentick++;
103 do_div(stolen, NS_PER_TICK);
105 if (stolen > stolentick)
106 stolen = stolentick;
108 stolentick -= stolen;
109 do_div(blocked, NS_PER_TICK);
111 if (blocked > stolentick)
112 blocked = stolentick;
114 if (stolen > 0 || blocked > 0) {
115 account_steal_time(NULL, jiffies_to_cputime(stolen));
116 account_steal_time(idle_task(cpu), jiffies_to_cputime(blocked));
117 run_local_timers();
119 if (rcu_pending(cpu))
120 rcu_check_callbacks(cpu, user_mode(get_irq_regs()));
122 scheduler_tick();
123 run_posix_cpu_timers(p);
124 delta_itm += local_cpu_data->itm_delta * (stolen + blocked);
126 if (cpu == time_keeper_id) {
127 write_seqlock(&xtime_lock);
128 do_timer(stolen + blocked);
129 local_cpu_data->itm_next = delta_itm + new_itm;
130 write_sequnlock(&xtime_lock);
131 } else {
132 local_cpu_data->itm_next = delta_itm + new_itm;
134 per_cpu(processed_stolen_time, cpu) += NS_PER_TICK * stolen;
135 per_cpu(processed_blocked_time, cpu) += NS_PER_TICK * blocked;
137 return delta_itm;
140 static int xen_do_steal_accounting(unsigned long *new_itm)
142 unsigned long delta_itm;
143 delta_itm = consider_steal_time(*new_itm);
144 *new_itm += delta_itm;
145 if (time_after(*new_itm, ia64_get_itc()) && delta_itm)
146 return 1;
148 return 0;
151 static void xen_itc_jitter_data_reset(void)
153 u64 lcycle, ret;
155 do {
156 lcycle = itc_jitter_data.itc_lastcycle;
157 ret = cmpxchg(&itc_jitter_data.itc_lastcycle, lcycle, 0);
158 } while (unlikely(ret != lcycle));
161 struct pv_time_ops xen_time_ops __initdata = {
162 .init_missing_ticks_accounting = xen_init_missing_ticks_accounting,
163 .do_steal_accounting = xen_do_steal_accounting,
164 .clocksource_resume = xen_itc_jitter_data_reset,