blob: 4bb218e2eb14a545cbee2432486a2a4533d0fa4f [file] [log] [blame]
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001/*
2 * VMI paravirtual timer support routines.
3 *
4 * Copyright (C) 2005, VMware, Inc.
5 *
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.
10 *
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
15 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Send feedback to dhecht@vmware.com
22 *
23 */
24
25/*
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.
29 */
30
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>
39
40#include <asm/timer.h>
41#include <asm/io.h>
42#include <asm/apic.h>
43#include <asm/div64.h>
44#include <asm/timer.h>
45#include <asm/desc.h>
46
47#include <asm/vmi.h>
48#include <asm/vmi_time.h>
49
50#include <mach_timer.h>
51#include <io_ports.h>
52
53#ifdef CONFIG_X86_LOCAL_APIC
54#define VMI_ALARM_WIRING VMI_ALARM_WIRED_LVTT
55#else
56#define VMI_ALARM_WIRING VMI_ALARM_WIRED_IRQ0
57#endif
58
59/* Cached VMI operations */
60struct vmi_timer_ops vmi_timer_ops;
61
62#ifdef CONFIG_NO_IDLE_HZ
63
64/* /proc/sys/kernel/hz_timer state. */
65int sysctl_hz_timer;
66
67/* Some stats */
68static DEFINE_PER_CPU(unsigned long, vmi_idle_no_hz_irqs);
69static DEFINE_PER_CPU(unsigned long, vmi_idle_no_hz_jiffies);
70static DEFINE_PER_CPU(unsigned long, idle_start_jiffies);
71
72#endif /* CONFIG_NO_IDLE_HZ */
73
74/* Number of alarms per second. By default this is CONFIG_VMI_ALARM_HZ. */
75static int alarm_hz = CONFIG_VMI_ALARM_HZ;
76
77/* Cache of the value get_cycle_frequency / HZ. */
78static signed long long cycles_per_jiffy;
79
80/* Cache of the value get_cycle_frequency / alarm_hz. */
81static signed long long cycles_per_alarm;
82
83/* The number of cycles accounted for by the 'jiffies'/'xtime' count.
84 * Protected by xtime_lock. */
85static unsigned long long real_cycles_accounted_system;
86
87/* The number of cycles accounted for by update_process_times(), per cpu. */
88static DEFINE_PER_CPU(unsigned long long, process_times_cycles_accounted_cpu);
89
90/* The number of stolen cycles accounted, per cpu. */
91static DEFINE_PER_CPU(unsigned long long, stolen_cycles_accounted_cpu);
92
93/* Clock source. */
94static cycle_t read_real_cycles(void)
95{
96 return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_REAL);
97}
98
99static cycle_t read_available_cycles(void)
100{
101 return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_AVAILABLE);
102}
103
104#if 0
105static cycle_t read_stolen_cycles(void)
106{
107 return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_STOLEN);
108}
109#endif /* 0 */
110
111static struct clocksource clocksource_vmi = {
112 .name = "vmi-timer",
113 .rating = 450,
114 .read = read_real_cycles,
115 .mask = CLOCKSOURCE_MASK(64),
116 .mult = 0, /* to be set */
117 .shift = 22,
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800118 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100119};
120
121
122/* Timer interrupt handler. */
123static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id);
124
125static struct irqaction vmi_timer_irq = {
126 vmi_timer_interrupt,
127 SA_INTERRUPT,
128 CPU_MASK_NONE,
129 "VMI-alarm",
130 NULL,
131 NULL
132};
133
134/* Alarm rate */
135static int __init vmi_timer_alarm_rate_setup(char* str)
136{
137 int alarm_rate;
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);
141 }
142 return 1;
143}
144__setup("vmi_timer_alarm_hz=", vmi_timer_alarm_rate_setup);
145
146
147/* Initialization */
148static void vmi_get_wallclock_ts(struct timespec *ts)
149{
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;
154}
155
156static void update_xtime_from_wallclock(void)
157{
158 struct timespec ts;
159 vmi_get_wallclock_ts(&ts);
160 do_settimeofday(&ts);
161}
162
163unsigned long vmi_get_wallclock(void)
164{
165 struct timespec ts;
166 vmi_get_wallclock_ts(&ts);
167 return ts.tv_sec;
168}
169
170int vmi_set_wallclock(unsigned long now)
171{
172 return -1;
173}
174
Zachary Amsden6cb9a832007-03-05 00:30:35 -0800175unsigned long long vmi_get_sched_cycles(void)
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100176{
177 return read_available_cycles();
178}
179
Zachary Amsden1182d852007-03-05 00:30:36 -0800180unsigned long vmi_cpu_khz(void)
181{
182 unsigned long long khz;
183
184 khz = vmi_timer_ops.get_cycle_frequency();
185 (void)do_div(khz, 1000);
186 return khz;
187}
188
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100189void __init vmi_time_init(void)
190{
191 unsigned long long cycles_per_sec, cycles_per_msec;
Zachary Amsden90736e22007-02-13 13:26:21 +0100192 unsigned long flags;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100193
Zachary Amsden90736e22007-02-13 13:26:21 +0100194 local_irq_save(flags);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100195 setup_irq(0, &vmi_timer_irq);
196#ifdef CONFIG_X86_LOCAL_APIC
197 set_intr_gate(LOCAL_TIMER_VECTOR, apic_vmi_timer_interrupt);
198#endif
199
200 no_sync_cmos_clock = 1;
201
202 vmi_get_wallclock_ts(&xtime);
203 set_normalized_timespec(&wall_to_monotonic,
204 -xtime.tv_sec, -xtime.tv_nsec);
205
206 real_cycles_accounted_system = read_real_cycles();
207 update_xtime_from_wallclock();
208 per_cpu(process_times_cycles_accounted_cpu, 0) = read_available_cycles();
209
210 cycles_per_sec = vmi_timer_ops.get_cycle_frequency();
211
212 cycles_per_jiffy = cycles_per_sec;
213 (void)do_div(cycles_per_jiffy, HZ);
214 cycles_per_alarm = cycles_per_sec;
215 (void)do_div(cycles_per_alarm, alarm_hz);
216 cycles_per_msec = cycles_per_sec;
217 (void)do_div(cycles_per_msec, 1000);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100218
219 printk(KERN_WARNING "VMI timer cycles/sec = %llu ; cycles/jiffy = %llu ;"
220 "cycles/alarm = %llu\n", cycles_per_sec, cycles_per_jiffy,
221 cycles_per_alarm);
222
223 clocksource_vmi.mult = clocksource_khz2mult(cycles_per_msec,
224 clocksource_vmi.shift);
225 if (clocksource_register(&clocksource_vmi))
226 printk(KERN_WARNING "Error registering VMITIME clocksource.");
227
228 /* Disable PIT. */
229 outb_p(0x3a, PIT_MODE); /* binary, mode 5, LSB/MSB, ch 0 */
230
231 /* schedule the alarm. do this in phase with process_times_cycles_accounted_cpu
232 * reduce the latency calling update_process_times. */
233 vmi_timer_ops.set_alarm(
234 VMI_ALARM_WIRED_IRQ0 | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
235 per_cpu(process_times_cycles_accounted_cpu, 0) + cycles_per_alarm,
236 cycles_per_alarm);
Zachary Amsden90736e22007-02-13 13:26:21 +0100237
238 local_irq_restore(flags);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100239}
240
241#ifdef CONFIG_X86_LOCAL_APIC
242
243void __init vmi_timer_setup_boot_alarm(void)
244{
245 local_irq_disable();
246
247 /* Route the interrupt to the correct vector. */
248 apic_write_around(APIC_LVTT, LOCAL_TIMER_VECTOR);
249
250 /* Cancel the IRQ0 wired alarm, and setup the LVTT alarm. */
251 vmi_timer_ops.cancel_alarm(VMI_CYCLES_AVAILABLE);
252 vmi_timer_ops.set_alarm(
253 VMI_ALARM_WIRED_LVTT | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
254 per_cpu(process_times_cycles_accounted_cpu, 0) + cycles_per_alarm,
255 cycles_per_alarm);
256 local_irq_enable();
257}
258
259/* Initialize the time accounting variables for an AP on an SMP system.
260 * Also, set the local alarm for the AP. */
261void __init vmi_timer_setup_secondary_alarm(void)
262{
263 int cpu = smp_processor_id();
264
265 /* Route the interrupt to the correct vector. */
266 apic_write_around(APIC_LVTT, LOCAL_TIMER_VECTOR);
267
268 per_cpu(process_times_cycles_accounted_cpu, cpu) = read_available_cycles();
269
270 vmi_timer_ops.set_alarm(
271 VMI_ALARM_WIRED_LVTT | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
272 per_cpu(process_times_cycles_accounted_cpu, cpu) + cycles_per_alarm,
273 cycles_per_alarm);
274}
275
276#endif
277
278/* Update system wide (real) time accounting (e.g. jiffies, xtime). */
279static void vmi_account_real_cycles(unsigned long long cur_real_cycles)
280{
281 long long cycles_not_accounted;
282
283 write_seqlock(&xtime_lock);
284
285 cycles_not_accounted = cur_real_cycles - real_cycles_accounted_system;
286 while (cycles_not_accounted >= cycles_per_jiffy) {
Zachary Amsden7507ba32007-03-05 00:30:34 -0800287 /* systems wide jiffies. */
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100288 do_timer(1);
289
290 cycles_not_accounted -= cycles_per_jiffy;
291 real_cycles_accounted_system += cycles_per_jiffy;
292 }
293
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100294 write_sequnlock(&xtime_lock);
295}
296
297/* Update per-cpu process times. */
298static void vmi_account_process_times_cycles(struct pt_regs *regs, int cpu,
299 unsigned long long cur_process_times_cycles)
300{
301 long long cycles_not_accounted;
302 cycles_not_accounted = cur_process_times_cycles -
303 per_cpu(process_times_cycles_accounted_cpu, cpu);
304
305 while (cycles_not_accounted >= cycles_per_jiffy) {
306 /* Account time to the current process. This includes
307 * calling into the scheduler to decrement the timeslice
308 * and possibly reschedule.*/
309 update_process_times(user_mode(regs));
310 /* XXX handle /proc/profile multiplier. */
311 profile_tick(CPU_PROFILING);
312
313 cycles_not_accounted -= cycles_per_jiffy;
314 per_cpu(process_times_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
315 }
316}
317
318#ifdef CONFIG_NO_IDLE_HZ
319/* Update per-cpu idle times. Used when a no-hz halt is ended. */
320static void vmi_account_no_hz_idle_cycles(int cpu,
321 unsigned long long cur_process_times_cycles)
322{
323 long long cycles_not_accounted;
324 unsigned long no_idle_hz_jiffies = 0;
325
326 cycles_not_accounted = cur_process_times_cycles -
327 per_cpu(process_times_cycles_accounted_cpu, cpu);
328
329 while (cycles_not_accounted >= cycles_per_jiffy) {
330 no_idle_hz_jiffies++;
331 cycles_not_accounted -= cycles_per_jiffy;
332 per_cpu(process_times_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
333 }
334 /* Account time to the idle process. */
335 account_steal_time(idle_task(cpu), jiffies_to_cputime(no_idle_hz_jiffies));
336}
337#endif
338
339/* Update per-cpu stolen time. */
340static void vmi_account_stolen_cycles(int cpu,
341 unsigned long long cur_real_cycles,
342 unsigned long long cur_avail_cycles)
343{
344 long long stolen_cycles_not_accounted;
345 unsigned long stolen_jiffies = 0;
346
347 if (cur_real_cycles < cur_avail_cycles)
348 return;
349
350 stolen_cycles_not_accounted = cur_real_cycles - cur_avail_cycles -
351 per_cpu(stolen_cycles_accounted_cpu, cpu);
352
353 while (stolen_cycles_not_accounted >= cycles_per_jiffy) {
354 stolen_jiffies++;
355 stolen_cycles_not_accounted -= cycles_per_jiffy;
356 per_cpu(stolen_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
357 }
358 /* HACK: pass NULL to force time onto cpustat->steal. */
359 account_steal_time(NULL, jiffies_to_cputime(stolen_jiffies));
360}
361
362/* Body of either IRQ0 interrupt handler (UP no local-APIC) or
363 * local-APIC LVTT interrupt handler (UP & local-APIC or SMP). */
364static void vmi_local_timer_interrupt(int cpu)
365{
366 unsigned long long cur_real_cycles, cur_process_times_cycles;
367
368 cur_real_cycles = read_real_cycles();
369 cur_process_times_cycles = read_available_cycles();
370 /* Update system wide (real) time state (xtime, jiffies). */
371 vmi_account_real_cycles(cur_real_cycles);
372 /* Update per-cpu process times. */
373 vmi_account_process_times_cycles(get_irq_regs(), cpu, cur_process_times_cycles);
374 /* Update time stolen from this cpu by the hypervisor. */
375 vmi_account_stolen_cycles(cpu, cur_real_cycles, cur_process_times_cycles);
376}
377
378#ifdef CONFIG_NO_IDLE_HZ
379
380/* Must be called only from idle loop, with interrupts disabled. */
381int vmi_stop_hz_timer(void)
382{
383 /* Note that cpu_set, cpu_clear are (SMP safe) atomic on x86. */
384
385 unsigned long seq, next;
386 unsigned long long real_cycles_expiry;
387 int cpu = smp_processor_id();
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100388
389 BUG_ON(!irqs_disabled());
390 if (sysctl_hz_timer != 0)
391 return 0;
392
393 cpu_set(cpu, nohz_cpu_mask);
394 smp_mb();
Zachary Amsden7507ba32007-03-05 00:30:34 -0800395
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100396 if (rcu_needs_cpu(cpu) || local_softirq_pending() ||
Zachary Amsden7507ba32007-03-05 00:30:34 -0800397 (next = next_timer_interrupt(),
398 time_before_eq(next, jiffies + HZ/CONFIG_VMI_ALARM_HZ))) {
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100399 cpu_clear(cpu, nohz_cpu_mask);
Zachary Amsden7507ba32007-03-05 00:30:34 -0800400 return 0;
401 }
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100402
403 /* Convert jiffies to the real cycle counter. */
404 do {
405 seq = read_seqbegin(&xtime_lock);
406 real_cycles_expiry = real_cycles_accounted_system +
407 (long)(next - jiffies) * cycles_per_jiffy;
408 } while (read_seqretry(&xtime_lock, seq));
409
410 /* This cpu is going idle. Disable the periodic alarm. */
Zachary Amsden7507ba32007-03-05 00:30:34 -0800411 vmi_timer_ops.cancel_alarm(VMI_CYCLES_AVAILABLE);
412 per_cpu(idle_start_jiffies, cpu) = jiffies;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100413 /* Set the real time alarm to expire at the next event. */
414 vmi_timer_ops.set_alarm(
Zachary Amsden7507ba32007-03-05 00:30:34 -0800415 VMI_ALARM_WIRING | VMI_ALARM_IS_ONESHOT | VMI_CYCLES_REAL,
416 real_cycles_expiry, 0);
417 return 1;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100418}
419
420static void vmi_reenable_hz_timer(int cpu)
421{
422 /* For /proc/vmi/info idle_hz stat. */
423 per_cpu(vmi_idle_no_hz_jiffies, cpu) += jiffies - per_cpu(idle_start_jiffies, cpu);
424 per_cpu(vmi_idle_no_hz_irqs, cpu)++;
425
426 /* Don't bother explicitly cancelling the one-shot alarm -- at
427 * worse we will receive a spurious timer interrupt. */
428 vmi_timer_ops.set_alarm(
429 VMI_ALARM_WIRING | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
430 per_cpu(process_times_cycles_accounted_cpu, cpu) + cycles_per_alarm,
431 cycles_per_alarm);
432 /* Indicate this cpu is no longer nohz idle. */
433 cpu_clear(cpu, nohz_cpu_mask);
434}
435
436/* Called from interrupt handlers when (local) HZ timer is disabled. */
437void vmi_account_time_restart_hz_timer(void)
438{
439 unsigned long long cur_real_cycles, cur_process_times_cycles;
440 int cpu = smp_processor_id();
441
442 BUG_ON(!irqs_disabled());
443 /* Account the time during which the HZ timer was disabled. */
444 cur_real_cycles = read_real_cycles();
445 cur_process_times_cycles = read_available_cycles();
446 /* Update system wide (real) time state (xtime, jiffies). */
447 vmi_account_real_cycles(cur_real_cycles);
448 /* Update per-cpu idle times. */
449 vmi_account_no_hz_idle_cycles(cpu, cur_process_times_cycles);
450 /* Update time stolen from this cpu by the hypervisor. */
451 vmi_account_stolen_cycles(cpu, cur_real_cycles, cur_process_times_cycles);
452 /* Reenable the hz timer. */
453 vmi_reenable_hz_timer(cpu);
454}
455
456#endif /* CONFIG_NO_IDLE_HZ */
457
458/* UP (and no local-APIC) VMI-timer alarm interrupt handler.
459 * Handler for IRQ0. Not used when SMP or X86_LOCAL_APIC after
460 * APIC setup and setup_boot_vmi_alarm() is called. */
461static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id)
462{
463 vmi_local_timer_interrupt(smp_processor_id());
464 return IRQ_HANDLED;
465}
466
467#ifdef CONFIG_X86_LOCAL_APIC
468
469/* SMP VMI-timer alarm interrupt handler. Handler for LVTT vector.
470 * Also used in UP when CONFIG_X86_LOCAL_APIC.
471 * The wrapper code is from arch/i386/kernel/apic.c#smp_apic_timer_interrupt. */
472void smp_apic_vmi_timer_interrupt(struct pt_regs *regs)
473{
474 struct pt_regs *old_regs = set_irq_regs(regs);
475 int cpu = smp_processor_id();
476
477 /*
478 * the NMI deadlock-detector uses this.
479 */
480 per_cpu(irq_stat,cpu).apic_timer_irqs++;
481
482 /*
483 * NOTE! We'd better ACK the irq immediately,
484 * because timer handling can be slow.
485 */
486 ack_APIC_irq();
487
488 /*
489 * update_process_times() expects us to have done irq_enter().
490 * Besides, if we don't timer interrupts ignore the global
491 * interrupt lock, which is the WrongThing (tm) to do.
492 */
493 irq_enter();
494 vmi_local_timer_interrupt(cpu);
495 irq_exit();
496 set_irq_regs(old_regs);
497}
498
499#endif /* CONFIG_X86_LOCAL_APIC */