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