2 * VMI paravirtual timer support routines.
4 * Copyright (C) 2005, VMware, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Send feedback to dhecht@vmware.com
26 * Portions of this code from arch/i386/kernel/timers/timer_tsc.c.
27 * Portions of the CONFIG_NO_IDLE_HZ code from arch/s390/kernel/time.c.
28 * See comments there for proper credits.
31 #include <linux/spinlock.h>
32 #include <linux/init.h>
33 #include <linux/errno.h>
34 #include <linux/jiffies.h>
35 #include <linux/interrupt.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/rcupdate.h>
38 #include <linux/clocksource.h>
40 #include <asm/timer.h>
43 #include <asm/div64.h>
44 #include <asm/timer.h>
48 #include <asm/vmi_time.h>
50 #include <mach_timer.h>
53 #ifdef CONFIG_X86_LOCAL_APIC
54 #define VMI_ALARM_WIRING VMI_ALARM_WIRED_LVTT
56 #define VMI_ALARM_WIRING VMI_ALARM_WIRED_IRQ0
59 /* Cached VMI operations */
60 struct vmi_timer_ops vmi_timer_ops;
62 #ifdef CONFIG_NO_IDLE_HZ
64 /* /proc/sys/kernel/hz_timer state. */
68 static DEFINE_PER_CPU(unsigned long, vmi_idle_no_hz_irqs);
69 static DEFINE_PER_CPU(unsigned long, vmi_idle_no_hz_jiffies);
70 static DEFINE_PER_CPU(unsigned long, idle_start_jiffies);
72 #endif /* CONFIG_NO_IDLE_HZ */
74 /* Number of alarms per second. By default this is CONFIG_VMI_ALARM_HZ. */
75 static int alarm_hz = CONFIG_VMI_ALARM_HZ;
77 /* Cache of the value get_cycle_frequency / HZ. */
78 static signed long long cycles_per_jiffy;
80 /* Cache of the value get_cycle_frequency / alarm_hz. */
81 static signed long long cycles_per_alarm;
83 /* The number of cycles accounted for by the 'jiffies'/'xtime' count.
84 * Protected by xtime_lock. */
85 static unsigned long long real_cycles_accounted_system;
87 /* The number of cycles accounted for by update_process_times(), per cpu. */
88 static DEFINE_PER_CPU(unsigned long long, process_times_cycles_accounted_cpu);
90 /* The number of stolen cycles accounted, per cpu. */
91 static DEFINE_PER_CPU(unsigned long long, stolen_cycles_accounted_cpu);
94 static cycle_t read_real_cycles(void)
96 return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_REAL);
99 static cycle_t read_available_cycles(void)
101 return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_AVAILABLE);
105 static cycle_t read_stolen_cycles(void)
107 return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_STOLEN);
111 static struct clocksource clocksource_vmi = {
114 .read = read_real_cycles,
115 .mask = CLOCKSOURCE_MASK(64),
116 .mult = 0, /* to be set */
118 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
122 /* Timer interrupt handler. */
123 static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id);
125 static struct irqaction vmi_timer_irq = {
135 static int __init vmi_timer_alarm_rate_setup(char* str)
138 if (get_option(&str, &alarm_rate) == 1 && alarm_rate > 0) {
139 alarm_hz = alarm_rate;
140 printk(KERN_WARNING "VMI timer alarm HZ set to %d\n", alarm_hz);
144 __setup("vmi_timer_alarm_hz=", vmi_timer_alarm_rate_setup);
148 static void vmi_get_wallclock_ts(struct timespec *ts)
150 unsigned long long wallclock;
151 wallclock = vmi_timer_ops.get_wallclock(); // nsec units
152 ts->tv_nsec = do_div(wallclock, 1000000000);
153 ts->tv_sec = wallclock;
156 unsigned long vmi_get_wallclock(void)
159 vmi_get_wallclock_ts(&ts);
163 int vmi_set_wallclock(unsigned long now)
168 unsigned long long vmi_get_sched_cycles(void)
170 return read_available_cycles();
173 unsigned long vmi_cpu_khz(void)
175 unsigned long long khz;
177 khz = vmi_timer_ops.get_cycle_frequency();
178 (void)do_div(khz, 1000);
182 void __init vmi_time_init(void)
184 unsigned long long cycles_per_sec, cycles_per_msec;
187 local_irq_save(flags);
188 setup_irq(0, &vmi_timer_irq);
189 #ifdef CONFIG_X86_LOCAL_APIC
190 set_intr_gate(LOCAL_TIMER_VECTOR, apic_vmi_timer_interrupt);
193 real_cycles_accounted_system = read_real_cycles();
194 per_cpu(process_times_cycles_accounted_cpu, 0) = read_available_cycles();
196 cycles_per_sec = vmi_timer_ops.get_cycle_frequency();
197 cycles_per_jiffy = cycles_per_sec;
198 (void)do_div(cycles_per_jiffy, HZ);
199 cycles_per_alarm = cycles_per_sec;
200 (void)do_div(cycles_per_alarm, alarm_hz);
201 cycles_per_msec = cycles_per_sec;
202 (void)do_div(cycles_per_msec, 1000);
204 printk(KERN_WARNING "VMI timer cycles/sec = %llu ; cycles/jiffy = %llu ;"
205 "cycles/alarm = %llu\n", cycles_per_sec, cycles_per_jiffy,
208 clocksource_vmi.mult = clocksource_khz2mult(cycles_per_msec,
209 clocksource_vmi.shift);
210 if (clocksource_register(&clocksource_vmi))
211 printk(KERN_WARNING "Error registering VMITIME clocksource.");
214 outb_p(0x3a, PIT_MODE); /* binary, mode 5, LSB/MSB, ch 0 */
216 /* schedule the alarm. do this in phase with process_times_cycles_accounted_cpu
217 * reduce the latency calling update_process_times. */
218 vmi_timer_ops.set_alarm(
219 VMI_ALARM_WIRED_IRQ0 | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
220 per_cpu(process_times_cycles_accounted_cpu, 0) + cycles_per_alarm,
223 local_irq_restore(flags);
226 #ifdef CONFIG_X86_LOCAL_APIC
228 void __init vmi_timer_setup_boot_alarm(void)
232 /* Route the interrupt to the correct vector. */
233 apic_write_around(APIC_LVTT, LOCAL_TIMER_VECTOR);
235 /* Cancel the IRQ0 wired alarm, and setup the LVTT alarm. */
236 vmi_timer_ops.cancel_alarm(VMI_CYCLES_AVAILABLE);
237 vmi_timer_ops.set_alarm(
238 VMI_ALARM_WIRED_LVTT | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
239 per_cpu(process_times_cycles_accounted_cpu, 0) + cycles_per_alarm,
244 /* Initialize the time accounting variables for an AP on an SMP system.
245 * Also, set the local alarm for the AP. */
246 void __devinit vmi_timer_setup_secondary_alarm(void)
248 int cpu = smp_processor_id();
250 /* Route the interrupt to the correct vector. */
251 apic_write_around(APIC_LVTT, LOCAL_TIMER_VECTOR);
253 per_cpu(process_times_cycles_accounted_cpu, cpu) = read_available_cycles();
255 vmi_timer_ops.set_alarm(
256 VMI_ALARM_WIRED_LVTT | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
257 per_cpu(process_times_cycles_accounted_cpu, cpu) + cycles_per_alarm,
263 /* Update system wide (real) time accounting (e.g. jiffies, xtime). */
264 static void vmi_account_real_cycles(unsigned long long cur_real_cycles)
266 long long cycles_not_accounted;
268 write_seqlock(&xtime_lock);
270 cycles_not_accounted = cur_real_cycles - real_cycles_accounted_system;
271 while (cycles_not_accounted >= cycles_per_jiffy) {
272 /* systems wide jiffies. */
275 cycles_not_accounted -= cycles_per_jiffy;
276 real_cycles_accounted_system += cycles_per_jiffy;
279 write_sequnlock(&xtime_lock);
282 /* Update per-cpu process times. */
283 static void vmi_account_process_times_cycles(struct pt_regs *regs, int cpu,
284 unsigned long long cur_process_times_cycles)
286 long long cycles_not_accounted;
287 cycles_not_accounted = cur_process_times_cycles -
288 per_cpu(process_times_cycles_accounted_cpu, cpu);
290 while (cycles_not_accounted >= cycles_per_jiffy) {
291 /* Account time to the current process. This includes
292 * calling into the scheduler to decrement the timeslice
293 * and possibly reschedule.*/
294 update_process_times(user_mode(regs));
295 /* XXX handle /proc/profile multiplier. */
296 profile_tick(CPU_PROFILING);
298 cycles_not_accounted -= cycles_per_jiffy;
299 per_cpu(process_times_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
303 #ifdef CONFIG_NO_IDLE_HZ
304 /* Update per-cpu idle times. Used when a no-hz halt is ended. */
305 static void vmi_account_no_hz_idle_cycles(int cpu,
306 unsigned long long cur_process_times_cycles)
308 long long cycles_not_accounted;
309 unsigned long no_idle_hz_jiffies = 0;
311 cycles_not_accounted = cur_process_times_cycles -
312 per_cpu(process_times_cycles_accounted_cpu, cpu);
314 while (cycles_not_accounted >= cycles_per_jiffy) {
315 no_idle_hz_jiffies++;
316 cycles_not_accounted -= cycles_per_jiffy;
317 per_cpu(process_times_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
319 /* Account time to the idle process. */
320 account_steal_time(idle_task(cpu), jiffies_to_cputime(no_idle_hz_jiffies));
324 /* Update per-cpu stolen time. */
325 static void vmi_account_stolen_cycles(int cpu,
326 unsigned long long cur_real_cycles,
327 unsigned long long cur_avail_cycles)
329 long long stolen_cycles_not_accounted;
330 unsigned long stolen_jiffies = 0;
332 if (cur_real_cycles < cur_avail_cycles)
335 stolen_cycles_not_accounted = cur_real_cycles - cur_avail_cycles -
336 per_cpu(stolen_cycles_accounted_cpu, cpu);
338 while (stolen_cycles_not_accounted >= cycles_per_jiffy) {
340 stolen_cycles_not_accounted -= cycles_per_jiffy;
341 per_cpu(stolen_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
343 /* HACK: pass NULL to force time onto cpustat->steal. */
344 account_steal_time(NULL, jiffies_to_cputime(stolen_jiffies));
347 /* Body of either IRQ0 interrupt handler (UP no local-APIC) or
348 * local-APIC LVTT interrupt handler (UP & local-APIC or SMP). */
349 static void vmi_local_timer_interrupt(int cpu)
351 unsigned long long cur_real_cycles, cur_process_times_cycles;
353 cur_real_cycles = read_real_cycles();
354 cur_process_times_cycles = read_available_cycles();
355 /* Update system wide (real) time state (xtime, jiffies). */
356 vmi_account_real_cycles(cur_real_cycles);
357 /* Update per-cpu process times. */
358 vmi_account_process_times_cycles(get_irq_regs(), cpu, cur_process_times_cycles);
359 /* Update time stolen from this cpu by the hypervisor. */
360 vmi_account_stolen_cycles(cpu, cur_real_cycles, cur_process_times_cycles);
363 #ifdef CONFIG_NO_IDLE_HZ
365 /* Must be called only from idle loop, with interrupts disabled. */
366 int vmi_stop_hz_timer(void)
368 /* Note that cpu_set, cpu_clear are (SMP safe) atomic on x86. */
370 unsigned long seq, next;
371 unsigned long long real_cycles_expiry;
372 int cpu = smp_processor_id();
374 BUG_ON(!irqs_disabled());
375 if (sysctl_hz_timer != 0)
378 cpu_set(cpu, nohz_cpu_mask);
381 if (rcu_needs_cpu(cpu) || local_softirq_pending() ||
382 (next = next_timer_interrupt(),
383 time_before_eq(next, jiffies + HZ/CONFIG_VMI_ALARM_HZ))) {
384 cpu_clear(cpu, nohz_cpu_mask);
388 /* Convert jiffies to the real cycle counter. */
390 seq = read_seqbegin(&xtime_lock);
391 real_cycles_expiry = real_cycles_accounted_system +
392 (long)(next - jiffies) * cycles_per_jiffy;
393 } while (read_seqretry(&xtime_lock, seq));
395 /* This cpu is going idle. Disable the periodic alarm. */
396 vmi_timer_ops.cancel_alarm(VMI_CYCLES_AVAILABLE);
397 per_cpu(idle_start_jiffies, cpu) = jiffies;
398 /* Set the real time alarm to expire at the next event. */
399 vmi_timer_ops.set_alarm(
400 VMI_ALARM_WIRING | VMI_ALARM_IS_ONESHOT | VMI_CYCLES_REAL,
401 real_cycles_expiry, 0);
405 static void vmi_reenable_hz_timer(int cpu)
407 /* For /proc/vmi/info idle_hz stat. */
408 per_cpu(vmi_idle_no_hz_jiffies, cpu) += jiffies - per_cpu(idle_start_jiffies, cpu);
409 per_cpu(vmi_idle_no_hz_irqs, cpu)++;
411 /* Don't bother explicitly cancelling the one-shot alarm -- at
412 * worse we will receive a spurious timer interrupt. */
413 vmi_timer_ops.set_alarm(
414 VMI_ALARM_WIRING | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
415 per_cpu(process_times_cycles_accounted_cpu, cpu) + cycles_per_alarm,
417 /* Indicate this cpu is no longer nohz idle. */
418 cpu_clear(cpu, nohz_cpu_mask);
421 /* Called from interrupt handlers when (local) HZ timer is disabled. */
422 void vmi_account_time_restart_hz_timer(void)
424 unsigned long long cur_real_cycles, cur_process_times_cycles;
425 int cpu = smp_processor_id();
427 BUG_ON(!irqs_disabled());
428 /* Account the time during which the HZ timer was disabled. */
429 cur_real_cycles = read_real_cycles();
430 cur_process_times_cycles = read_available_cycles();
431 /* Update system wide (real) time state (xtime, jiffies). */
432 vmi_account_real_cycles(cur_real_cycles);
433 /* Update per-cpu idle times. */
434 vmi_account_no_hz_idle_cycles(cpu, cur_process_times_cycles);
435 /* Update time stolen from this cpu by the hypervisor. */
436 vmi_account_stolen_cycles(cpu, cur_real_cycles, cur_process_times_cycles);
437 /* Reenable the hz timer. */
438 vmi_reenable_hz_timer(cpu);
441 #endif /* CONFIG_NO_IDLE_HZ */
443 /* UP (and no local-APIC) VMI-timer alarm interrupt handler.
444 * Handler for IRQ0. Not used when SMP or X86_LOCAL_APIC after
445 * APIC setup and setup_boot_vmi_alarm() is called. */
446 static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id)
448 vmi_local_timer_interrupt(smp_processor_id());
452 #ifdef CONFIG_X86_LOCAL_APIC
454 /* SMP VMI-timer alarm interrupt handler. Handler for LVTT vector.
455 * Also used in UP when CONFIG_X86_LOCAL_APIC.
456 * The wrapper code is from arch/i386/kernel/apic.c#smp_apic_timer_interrupt. */
457 void smp_apic_vmi_timer_interrupt(struct pt_regs *regs)
459 struct pt_regs *old_regs = set_irq_regs(regs);
460 int cpu = smp_processor_id();
463 * the NMI deadlock-detector uses this.
465 per_cpu(irq_stat,cpu).apic_timer_irqs++;
468 * NOTE! We'd better ACK the irq immediately,
469 * because timer handling can be slow.
474 * update_process_times() expects us to have done irq_enter().
475 * Besides, if we don't timer interrupts ignore the global
476 * interrupt lock, which is the WrongThing (tm) to do.
479 vmi_local_timer_interrupt(cpu);
481 set_irq_regs(old_regs);
484 #endif /* CONFIG_X86_LOCAL_APIC */