spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / arch / ia64 / xen / time.c
blob1f8244a78bee026948376340024c50c29f922c59
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/timex.h>
31 #include <asm/xen/hypervisor.h>
33 #include <xen/interface/vcpu.h>
35 #include "../kernel/fsyscall_gtod_data.h"
37 static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate);
38 static DEFINE_PER_CPU(unsigned long, xen_stolen_time);
39 static DEFINE_PER_CPU(unsigned long, xen_blocked_time);
41 /* taken from i386/kernel/time-xen.c */
42 static void xen_init_missing_ticks_accounting(int cpu)
44 struct vcpu_register_runstate_memory_area area;
45 struct vcpu_runstate_info *runstate = &per_cpu(xen_runstate, cpu);
46 int rc;
48 memset(runstate, 0, sizeof(*runstate));
50 area.addr.v = runstate;
51 rc = HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area, cpu,
52 &area);
53 WARN_ON(rc && rc != -ENOSYS);
55 per_cpu(xen_blocked_time, cpu) = runstate->time[RUNSTATE_blocked];
56 per_cpu(xen_stolen_time, cpu) = runstate->time[RUNSTATE_runnable]
57 + runstate->time[RUNSTATE_offline];
61 * Runstate accounting
63 /* stolen from arch/x86/xen/time.c */
64 static void get_runstate_snapshot(struct vcpu_runstate_info *res)
66 u64 state_time;
67 struct vcpu_runstate_info *state;
69 BUG_ON(preemptible());
71 state = &__get_cpu_var(xen_runstate);
74 * The runstate info is always updated by the hypervisor on
75 * the current CPU, so there's no need to use anything
76 * stronger than a compiler barrier when fetching it.
78 do {
79 state_time = state->state_entry_time;
80 rmb();
81 *res = *state;
82 rmb();
83 } while (state->state_entry_time != state_time);
86 #define NS_PER_TICK (1000000000LL/HZ)
88 static unsigned long
89 consider_steal_time(unsigned long new_itm)
91 unsigned long stolen, blocked;
92 unsigned long delta_itm = 0, stolentick = 0;
93 int cpu = smp_processor_id();
94 struct vcpu_runstate_info runstate;
95 struct task_struct *p = current;
97 get_runstate_snapshot(&runstate);
100 * Check for vcpu migration effect
101 * In this case, itc value is reversed.
102 * This causes huge stolen value.
103 * This function just checks and reject this effect.
105 if (!time_after_eq(runstate.time[RUNSTATE_blocked],
106 per_cpu(xen_blocked_time, cpu)))
107 blocked = 0;
109 if (!time_after_eq(runstate.time[RUNSTATE_runnable] +
110 runstate.time[RUNSTATE_offline],
111 per_cpu(xen_stolen_time, cpu)))
112 stolen = 0;
114 if (!time_after(delta_itm + new_itm, ia64_get_itc()))
115 stolentick = ia64_get_itc() - new_itm;
117 do_div(stolentick, NS_PER_TICK);
118 stolentick++;
120 do_div(stolen, NS_PER_TICK);
122 if (stolen > stolentick)
123 stolen = stolentick;
125 stolentick -= stolen;
126 do_div(blocked, NS_PER_TICK);
128 if (blocked > stolentick)
129 blocked = stolentick;
131 if (stolen > 0 || blocked > 0) {
132 account_steal_ticks(stolen);
133 account_idle_ticks(blocked);
134 run_local_timers();
136 rcu_check_callbacks(cpu, user_mode(get_irq_regs()));
138 scheduler_tick();
139 run_posix_cpu_timers(p);
140 delta_itm += local_cpu_data->itm_delta * (stolen + blocked);
142 if (cpu == time_keeper_id)
143 xtime_update(stolen + blocked);
145 local_cpu_data->itm_next = delta_itm + new_itm;
147 per_cpu(xen_stolen_time, cpu) += NS_PER_TICK * stolen;
148 per_cpu(xen_blocked_time, cpu) += NS_PER_TICK * blocked;
150 return delta_itm;
153 static int xen_do_steal_accounting(unsigned long *new_itm)
155 unsigned long delta_itm;
156 delta_itm = consider_steal_time(*new_itm);
157 *new_itm += delta_itm;
158 if (time_after(*new_itm, ia64_get_itc()) && delta_itm)
159 return 1;
161 return 0;
164 static void xen_itc_jitter_data_reset(void)
166 u64 lcycle, ret;
168 do {
169 lcycle = itc_jitter_data.itc_lastcycle;
170 ret = cmpxchg(&itc_jitter_data.itc_lastcycle, lcycle, 0);
171 } while (unlikely(ret != lcycle));
174 /* based on xen_sched_clock() in arch/x86/xen/time.c. */
176 * This relies on HAVE_UNSTABLE_SCHED_CLOCK. If it can't be defined,
177 * something similar logic should be implemented here.
180 * Xen sched_clock implementation. Returns the number of unstolen
181 * nanoseconds, which is nanoseconds the VCPU spent in RUNNING+BLOCKED
182 * states.
184 static unsigned long long xen_sched_clock(void)
186 struct vcpu_runstate_info runstate;
188 unsigned long long now;
189 unsigned long long offset;
190 unsigned long long ret;
193 * Ideally sched_clock should be called on a per-cpu basis
194 * anyway, so preempt should already be disabled, but that's
195 * not current practice at the moment.
197 preempt_disable();
200 * both ia64_native_sched_clock() and xen's runstate are
201 * based on mAR.ITC. So difference of them makes sense.
203 now = ia64_native_sched_clock();
205 get_runstate_snapshot(&runstate);
207 WARN_ON(runstate.state != RUNSTATE_running);
209 offset = 0;
210 if (now > runstate.state_entry_time)
211 offset = now - runstate.state_entry_time;
212 ret = runstate.time[RUNSTATE_blocked] +
213 runstate.time[RUNSTATE_running] +
214 offset;
216 preempt_enable();
218 return ret;
221 struct pv_time_ops xen_time_ops __initdata = {
222 .init_missing_ticks_accounting = xen_init_missing_ticks_accounting,
223 .do_steal_accounting = xen_do_steal_accounting,
224 .clocksource_resume = xen_itc_jitter_data_reset,
225 .sched_clock = xen_sched_clock,
228 /* Called after suspend, to resume time. */
229 static void xen_local_tick_resume(void)
231 /* Just trigger a tick. */
232 ia64_cpu_local_tick();
233 touch_softlockup_watchdog();
236 void
237 xen_timer_resume(void)
239 unsigned int cpu;
241 xen_local_tick_resume();
243 for_each_online_cpu(cpu)
244 xen_init_missing_ticks_accounting(cpu);
247 static void ia64_cpu_local_tick_fn(void *unused)
249 xen_local_tick_resume();
250 xen_init_missing_ticks_accounting(smp_processor_id());
253 void
254 xen_timer_resume_on_aps(void)
256 smp_call_function(&ia64_cpu_local_tick_fn, NULL, 1);