ia64/pv_ops/xen: implement xen pv_time_ops.
[linux-2.6.git] / arch / ia64 / xen / time.c
1 /******************************************************************************
2  * arch/ia64/xen/time.c
3  *
4  * Copyright (c) 2008 Isaku Yamahata <yamahata at valinux co jp>
5  *                    VA Linux Systems Japan K.K.
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
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>
28
29 #include <asm/xen/hypervisor.h>
30
31 #include <xen/interface/vcpu.h>
32
33 #include "../kernel/fsyscall_gtod_data.h"
34
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);
38
39 /* taken from i386/kernel/time-xen.c */
40 static void xen_init_missing_ticks_accounting(int cpu)
41 {
42         struct vcpu_register_runstate_memory_area area;
43         struct vcpu_runstate_info *runstate = &per_cpu(runstate, cpu);
44         int rc;
45
46         memset(runstate, 0, sizeof(*runstate));
47
48         area.addr.v = runstate;
49         rc = HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area, cpu,
50                                 &area);
51         WARN_ON(rc && rc != -ENOSYS);
52
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];
56 }
57
58 /*
59  * Runstate accounting
60  */
61 /* stolen from arch/x86/xen/time.c */
62 static void get_runstate_snapshot(struct vcpu_runstate_info *res)
63 {
64         u64 state_time;
65         struct vcpu_runstate_info *state;
66
67         BUG_ON(preemptible());
68
69         state = &__get_cpu_var(runstate);
70
71         /*
72          * The runstate info is always updated by the hypervisor on
73          * the current CPU, so there's no need to use anything
74          * stronger than a compiler barrier when fetching it.
75          */
76         do {
77                 state_time = state->state_entry_time;
78                 rmb();
79                 *res = *state;
80                 rmb();
81         } while (state->state_entry_time != state_time);
82 }
83
84 #define NS_PER_TICK (1000000000LL/HZ)
85
86 static unsigned long
87 consider_steal_time(unsigned long new_itm)
88 {
89         unsigned long stolen, blocked;
90         unsigned long delta_itm = 0, stolentick = 0;
91         int cpu = smp_processor_id();
92         struct vcpu_runstate_info runstate;
93         struct task_struct *p = current;
94
95         get_runstate_snapshot(&runstate);
96
97         /*
98          * Check for vcpu migration effect
99          * In this case, itc value is reversed.
100          * This causes huge stolen value.
101          * This function just checks and reject this effect.
102          */
103         if (!time_after_eq(runstate.time[RUNSTATE_blocked],
104                            per_cpu(processed_blocked_time, cpu)))
105                 blocked = 0;
106
107         if (!time_after_eq(runstate.time[RUNSTATE_runnable] +
108                            runstate.time[RUNSTATE_offline],
109                            per_cpu(processed_stolen_time, cpu)))
110                 stolen = 0;
111
112         if (!time_after(delta_itm + new_itm, ia64_get_itc()))
113                 stolentick = ia64_get_itc() - new_itm;
114
115         do_div(stolentick, NS_PER_TICK);
116         stolentick++;
117
118         do_div(stolen, NS_PER_TICK);
119
120         if (stolen > stolentick)
121                 stolen = stolentick;
122
123         stolentick -= stolen;
124         do_div(blocked, NS_PER_TICK);
125
126         if (blocked > stolentick)
127                 blocked = stolentick;
128
129         if (stolen > 0 || blocked > 0) {
130                 account_steal_time(NULL, jiffies_to_cputime(stolen));
131                 account_steal_time(idle_task(cpu), jiffies_to_cputime(blocked));
132                 run_local_timers();
133
134                 if (rcu_pending(cpu))
135                         rcu_check_callbacks(cpu, user_mode(get_irq_regs()));
136
137                 scheduler_tick();
138                 run_posix_cpu_timers(p);
139                 delta_itm += local_cpu_data->itm_delta * (stolen + blocked);
140
141                 if (cpu == time_keeper_id) {
142                         write_seqlock(&xtime_lock);
143                         do_timer(stolen + blocked);
144                         local_cpu_data->itm_next = delta_itm + new_itm;
145                         write_sequnlock(&xtime_lock);
146                 } else {
147                         local_cpu_data->itm_next = delta_itm + new_itm;
148                 }
149                 per_cpu(processed_stolen_time, cpu) += NS_PER_TICK * stolen;
150                 per_cpu(processed_blocked_time, cpu) += NS_PER_TICK * blocked;
151         }
152         return delta_itm;
153 }
154
155 static int xen_do_steal_accounting(unsigned long *new_itm)
156 {
157         unsigned long delta_itm;
158         delta_itm = consider_steal_time(*new_itm);
159         *new_itm += delta_itm;
160         if (time_after(*new_itm, ia64_get_itc()) && delta_itm)
161                 return 1;
162
163         return 0;
164 }
165
166 static void xen_itc_jitter_data_reset(void)
167 {
168         u64 lcycle, ret;
169
170         do {
171                 lcycle = itc_jitter_data.itc_lastcycle;
172                 ret = cmpxchg(&itc_jitter_data.itc_lastcycle, lcycle, 0);
173         } while (unlikely(ret != lcycle));
174 }
175
176 struct pv_time_ops xen_time_ops __initdata = {
177         .init_missing_ticks_accounting  = xen_init_missing_ticks_accounting,
178         .do_steal_accounting            = xen_do_steal_accounting,
179         .clocksource_resume             = xen_itc_jitter_data_reset,
180 };