diff options
Diffstat (limited to 'arch/x86/kernel/vmiclock_32.c')
-rw-r--r-- | arch/x86/kernel/vmiclock_32.c | 320 |
1 files changed, 320 insertions, 0 deletions
diff --git a/arch/x86/kernel/vmiclock_32.c b/arch/x86/kernel/vmiclock_32.c new file mode 100644 index 000000000000..b1b5ab08b26e --- /dev/null +++ b/arch/x86/kernel/vmiclock_32.c | |||
@@ -0,0 +1,320 @@ | |||
1 | /* | ||
2 | * VMI paravirtual timer support routines. | ||
3 | * | ||
4 | * Copyright (C) 2007, 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 | */ | ||
22 | |||
23 | #include <linux/smp.h> | ||
24 | #include <linux/interrupt.h> | ||
25 | #include <linux/cpumask.h> | ||
26 | #include <linux/clocksource.h> | ||
27 | #include <linux/clockchips.h> | ||
28 | |||
29 | #include <asm/vmi.h> | ||
30 | #include <asm/vmi_time.h> | ||
31 | #include <asm/arch_hooks.h> | ||
32 | #include <asm/apicdef.h> | ||
33 | #include <asm/apic.h> | ||
34 | #include <asm/timer.h> | ||
35 | #include <asm/i8253.h> | ||
36 | |||
37 | #include <irq_vectors.h> | ||
38 | #include "io_ports.h" | ||
39 | |||
40 | #define VMI_ONESHOT (VMI_ALARM_IS_ONESHOT | VMI_CYCLES_REAL | vmi_get_alarm_wiring()) | ||
41 | #define VMI_PERIODIC (VMI_ALARM_IS_PERIODIC | VMI_CYCLES_REAL | vmi_get_alarm_wiring()) | ||
42 | |||
43 | static DEFINE_PER_CPU(struct clock_event_device, local_events); | ||
44 | |||
45 | static inline u32 vmi_counter(u32 flags) | ||
46 | { | ||
47 | /* Given VMI_ONESHOT or VMI_PERIODIC, return the corresponding | ||
48 | * cycle counter. */ | ||
49 | return flags & VMI_ALARM_COUNTER_MASK; | ||
50 | } | ||
51 | |||
52 | /* paravirt_ops.get_wallclock = vmi_get_wallclock */ | ||
53 | unsigned long vmi_get_wallclock(void) | ||
54 | { | ||
55 | unsigned long long wallclock; | ||
56 | wallclock = vmi_timer_ops.get_wallclock(); // nsec | ||
57 | (void)do_div(wallclock, 1000000000); // sec | ||
58 | |||
59 | return wallclock; | ||
60 | } | ||
61 | |||
62 | /* paravirt_ops.set_wallclock = vmi_set_wallclock */ | ||
63 | int vmi_set_wallclock(unsigned long now) | ||
64 | { | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | /* paravirt_ops.sched_clock = vmi_sched_clock */ | ||
69 | unsigned long long vmi_sched_clock(void) | ||
70 | { | ||
71 | return cycles_2_ns(vmi_timer_ops.get_cycle_counter(VMI_CYCLES_AVAILABLE)); | ||
72 | } | ||
73 | |||
74 | /* paravirt_ops.get_cpu_khz = vmi_cpu_khz */ | ||
75 | unsigned long vmi_cpu_khz(void) | ||
76 | { | ||
77 | unsigned long long khz; | ||
78 | khz = vmi_timer_ops.get_cycle_frequency(); | ||
79 | (void)do_div(khz, 1000); | ||
80 | return khz; | ||
81 | } | ||
82 | |||
83 | static inline unsigned int vmi_get_timer_vector(void) | ||
84 | { | ||
85 | #ifdef CONFIG_X86_IO_APIC | ||
86 | return FIRST_DEVICE_VECTOR; | ||
87 | #else | ||
88 | return FIRST_EXTERNAL_VECTOR; | ||
89 | #endif | ||
90 | } | ||
91 | |||
92 | /** vmi clockchip */ | ||
93 | #ifdef CONFIG_X86_LOCAL_APIC | ||
94 | static unsigned int startup_timer_irq(unsigned int irq) | ||
95 | { | ||
96 | unsigned long val = apic_read(APIC_LVTT); | ||
97 | apic_write(APIC_LVTT, vmi_get_timer_vector()); | ||
98 | |||
99 | return (val & APIC_SEND_PENDING); | ||
100 | } | ||
101 | |||
102 | static void mask_timer_irq(unsigned int irq) | ||
103 | { | ||
104 | unsigned long val = apic_read(APIC_LVTT); | ||
105 | apic_write(APIC_LVTT, val | APIC_LVT_MASKED); | ||
106 | } | ||
107 | |||
108 | static void unmask_timer_irq(unsigned int irq) | ||
109 | { | ||
110 | unsigned long val = apic_read(APIC_LVTT); | ||
111 | apic_write(APIC_LVTT, val & ~APIC_LVT_MASKED); | ||
112 | } | ||
113 | |||
114 | static void ack_timer_irq(unsigned int irq) | ||
115 | { | ||
116 | ack_APIC_irq(); | ||
117 | } | ||
118 | |||
119 | static struct irq_chip vmi_chip __read_mostly = { | ||
120 | .name = "VMI-LOCAL", | ||
121 | .startup = startup_timer_irq, | ||
122 | .mask = mask_timer_irq, | ||
123 | .unmask = unmask_timer_irq, | ||
124 | .ack = ack_timer_irq | ||
125 | }; | ||
126 | #endif | ||
127 | |||
128 | /** vmi clockevent */ | ||
129 | #define VMI_ALARM_WIRED_IRQ0 0x00000000 | ||
130 | #define VMI_ALARM_WIRED_LVTT 0x00010000 | ||
131 | static int vmi_wiring = VMI_ALARM_WIRED_IRQ0; | ||
132 | |||
133 | static inline int vmi_get_alarm_wiring(void) | ||
134 | { | ||
135 | return vmi_wiring; | ||
136 | } | ||
137 | |||
138 | static void vmi_timer_set_mode(enum clock_event_mode mode, | ||
139 | struct clock_event_device *evt) | ||
140 | { | ||
141 | cycle_t now, cycles_per_hz; | ||
142 | BUG_ON(!irqs_disabled()); | ||
143 | |||
144 | switch (mode) { | ||
145 | case CLOCK_EVT_MODE_ONESHOT: | ||
146 | case CLOCK_EVT_MODE_RESUME: | ||
147 | break; | ||
148 | case CLOCK_EVT_MODE_PERIODIC: | ||
149 | cycles_per_hz = vmi_timer_ops.get_cycle_frequency(); | ||
150 | (void)do_div(cycles_per_hz, HZ); | ||
151 | now = vmi_timer_ops.get_cycle_counter(vmi_counter(VMI_PERIODIC)); | ||
152 | vmi_timer_ops.set_alarm(VMI_PERIODIC, now, cycles_per_hz); | ||
153 | break; | ||
154 | case CLOCK_EVT_MODE_UNUSED: | ||
155 | case CLOCK_EVT_MODE_SHUTDOWN: | ||
156 | switch (evt->mode) { | ||
157 | case CLOCK_EVT_MODE_ONESHOT: | ||
158 | vmi_timer_ops.cancel_alarm(VMI_ONESHOT); | ||
159 | break; | ||
160 | case CLOCK_EVT_MODE_PERIODIC: | ||
161 | vmi_timer_ops.cancel_alarm(VMI_PERIODIC); | ||
162 | break; | ||
163 | default: | ||
164 | break; | ||
165 | } | ||
166 | break; | ||
167 | default: | ||
168 | break; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | static int vmi_timer_next_event(unsigned long delta, | ||
173 | struct clock_event_device *evt) | ||
174 | { | ||
175 | /* Unfortunately, set_next_event interface only passes relative | ||
176 | * expiry, but we want absolute expiry. It'd be better if were | ||
177 | * were passed an aboslute expiry, since a bunch of time may | ||
178 | * have been stolen between the time the delta is computed and | ||
179 | * when we set the alarm below. */ | ||
180 | cycle_t now = vmi_timer_ops.get_cycle_counter(vmi_counter(VMI_ONESHOT)); | ||
181 | |||
182 | BUG_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT); | ||
183 | vmi_timer_ops.set_alarm(VMI_ONESHOT, now + delta, 0); | ||
184 | return 0; | ||
185 | } | ||
186 | |||
187 | static struct clock_event_device vmi_clockevent = { | ||
188 | .name = "vmi-timer", | ||
189 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, | ||
190 | .shift = 22, | ||
191 | .set_mode = vmi_timer_set_mode, | ||
192 | .set_next_event = vmi_timer_next_event, | ||
193 | .rating = 1000, | ||
194 | .irq = 0, | ||
195 | }; | ||
196 | |||
197 | static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id) | ||
198 | { | ||
199 | struct clock_event_device *evt = &__get_cpu_var(local_events); | ||
200 | evt->event_handler(evt); | ||
201 | return IRQ_HANDLED; | ||
202 | } | ||
203 | |||
204 | static struct irqaction vmi_clock_action = { | ||
205 | .name = "vmi-timer", | ||
206 | .handler = vmi_timer_interrupt, | ||
207 | .flags = IRQF_DISABLED | IRQF_NOBALANCING, | ||
208 | .mask = CPU_MASK_ALL, | ||
209 | }; | ||
210 | |||
211 | static void __devinit vmi_time_init_clockevent(void) | ||
212 | { | ||
213 | cycle_t cycles_per_msec; | ||
214 | struct clock_event_device *evt; | ||
215 | |||
216 | int cpu = smp_processor_id(); | ||
217 | evt = &__get_cpu_var(local_events); | ||
218 | |||
219 | /* Use cycles_per_msec since div_sc params are 32-bits. */ | ||
220 | cycles_per_msec = vmi_timer_ops.get_cycle_frequency(); | ||
221 | (void)do_div(cycles_per_msec, 1000); | ||
222 | |||
223 | memcpy(evt, &vmi_clockevent, sizeof(*evt)); | ||
224 | /* Must pick .shift such that .mult fits in 32-bits. Choosing | ||
225 | * .shift to be 22 allows 2^(32-22) cycles per nano-seconds | ||
226 | * before overflow. */ | ||
227 | evt->mult = div_sc(cycles_per_msec, NSEC_PER_MSEC, evt->shift); | ||
228 | /* Upper bound is clockevent's use of ulong for cycle deltas. */ | ||
229 | evt->max_delta_ns = clockevent_delta2ns(ULONG_MAX, evt); | ||
230 | evt->min_delta_ns = clockevent_delta2ns(1, evt); | ||
231 | evt->cpumask = cpumask_of_cpu(cpu); | ||
232 | |||
233 | printk(KERN_WARNING "vmi: registering clock event %s. mult=%lu shift=%u\n", | ||
234 | evt->name, evt->mult, evt->shift); | ||
235 | clockevents_register_device(evt); | ||
236 | } | ||
237 | |||
238 | void __init vmi_time_init(void) | ||
239 | { | ||
240 | /* Disable PIT: BIOSes start PIT CH0 with 18.2hz peridic. */ | ||
241 | outb_p(0x3a, PIT_MODE); /* binary, mode 5, LSB/MSB, ch 0 */ | ||
242 | |||
243 | vmi_time_init_clockevent(); | ||
244 | setup_irq(0, &vmi_clock_action); | ||
245 | } | ||
246 | |||
247 | #ifdef CONFIG_X86_LOCAL_APIC | ||
248 | void __devinit vmi_time_bsp_init(void) | ||
249 | { | ||
250 | /* | ||
251 | * On APIC systems, we want local timers to fire on each cpu. We do | ||
252 | * this by programming LVTT to deliver timer events to the IRQ handler | ||
253 | * for IRQ-0, since we can't re-use the APIC local timer handler | ||
254 | * without interfering with that code. | ||
255 | */ | ||
256 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); | ||
257 | local_irq_disable(); | ||
258 | #ifdef CONFIG_X86_SMP | ||
259 | /* | ||
260 | * XXX handle_percpu_irq only defined for SMP; we need to switch over | ||
261 | * to using it, since this is a local interrupt, which each CPU must | ||
262 | * handle individually without locking out or dropping simultaneous | ||
263 | * local timers on other CPUs. We also don't want to trigger the | ||
264 | * quirk workaround code for interrupts which gets invoked from | ||
265 | * handle_percpu_irq via eoi, so we use our own IRQ chip. | ||
266 | */ | ||
267 | set_irq_chip_and_handler_name(0, &vmi_chip, handle_percpu_irq, "lvtt"); | ||
268 | #else | ||
269 | set_irq_chip_and_handler_name(0, &vmi_chip, handle_edge_irq, "lvtt"); | ||
270 | #endif | ||
271 | vmi_wiring = VMI_ALARM_WIRED_LVTT; | ||
272 | apic_write(APIC_LVTT, vmi_get_timer_vector()); | ||
273 | local_irq_enable(); | ||
274 | clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); | ||
275 | } | ||
276 | |||
277 | void __devinit vmi_time_ap_init(void) | ||
278 | { | ||
279 | vmi_time_init_clockevent(); | ||
280 | apic_write(APIC_LVTT, vmi_get_timer_vector()); | ||
281 | } | ||
282 | #endif | ||
283 | |||
284 | /** vmi clocksource */ | ||
285 | |||
286 | static cycle_t read_real_cycles(void) | ||
287 | { | ||
288 | return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_REAL); | ||
289 | } | ||
290 | |||
291 | static struct clocksource clocksource_vmi = { | ||
292 | .name = "vmi-timer", | ||
293 | .rating = 450, | ||
294 | .read = read_real_cycles, | ||
295 | .mask = CLOCKSOURCE_MASK(64), | ||
296 | .mult = 0, /* to be set */ | ||
297 | .shift = 22, | ||
298 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, | ||
299 | }; | ||
300 | |||
301 | static int __init init_vmi_clocksource(void) | ||
302 | { | ||
303 | cycle_t cycles_per_msec; | ||
304 | |||
305 | if (!vmi_timer_ops.get_cycle_frequency) | ||
306 | return 0; | ||
307 | /* Use khz2mult rather than hz2mult since hz arg is only 32-bits. */ | ||
308 | cycles_per_msec = vmi_timer_ops.get_cycle_frequency(); | ||
309 | (void)do_div(cycles_per_msec, 1000); | ||
310 | |||
311 | /* Note that clocksource.{mult, shift} converts in the opposite direction | ||
312 | * as clockevents. */ | ||
313 | clocksource_vmi.mult = clocksource_khz2mult(cycles_per_msec, | ||
314 | clocksource_vmi.shift); | ||
315 | |||
316 | printk(KERN_WARNING "vmi: registering clock source khz=%lld\n", cycles_per_msec); | ||
317 | return clocksource_register(&clocksource_vmi); | ||
318 | |||
319 | } | ||
320 | module_init(init_vmi_clocksource); | ||