include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / kvm / i8254.c
CommitLineData
7837699f
SY
1/*
2 * 8253/8254 interval timer emulation
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2006 Intel Corporation
6 * Copyright (c) 2007 Keir Fraser, XenSource Inc
7 * Copyright (c) 2008 Intel Corporation
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 *
27 * Authors:
28 * Sheng Yang <sheng.yang@intel.com>
29 * Based on QEMU and Xen.
30 */
31
a78d9626
JP
32#define pr_fmt(fmt) "pit: " fmt
33
7837699f 34#include <linux/kvm_host.h>
5a0e3ad6 35#include <linux/slab.h>
7837699f
SY
36
37#include "irq.h"
38#include "i8254.h"
39
40#ifndef CONFIG_X86_64
6f6d6a1a 41#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
7837699f
SY
42#else
43#define mod_64(x, y) ((x) % (y))
44#endif
45
46#define RW_STATE_LSB 1
47#define RW_STATE_MSB 2
48#define RW_STATE_WORD0 3
49#define RW_STATE_WORD1 4
50
51/* Compute with 96 bit intermediate result: (a*b)/c */
52static u64 muldiv64(u64 a, u32 b, u32 c)
53{
54 union {
55 u64 ll;
56 struct {
57 u32 low, high;
58 } l;
59 } u, res;
60 u64 rl, rh;
61
62 u.ll = a;
63 rl = (u64)u.l.low * (u64)b;
64 rh = (u64)u.l.high * (u64)b;
65 rh += (rl >> 32);
6f6d6a1a
RZ
66 res.l.high = div64_u64(rh, c);
67 res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
7837699f
SY
68 return res.ll;
69}
70
71static void pit_set_gate(struct kvm *kvm, int channel, u32 val)
72{
73 struct kvm_kpit_channel_state *c =
74 &kvm->arch.vpit->pit_state.channels[channel];
75
76 WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
77
78 switch (c->mode) {
79 default:
80 case 0:
81 case 4:
82 /* XXX: just disable/enable counting */
83 break;
84 case 1:
85 case 2:
86 case 3:
87 case 5:
88 /* Restart counting on rising edge. */
89 if (c->gate < val)
90 c->count_load_time = ktime_get();
91 break;
92 }
93
94 c->gate = val;
95}
96
8b2cf73c 97static int pit_get_gate(struct kvm *kvm, int channel)
7837699f
SY
98{
99 WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
100
101 return kvm->arch.vpit->pit_state.channels[channel].gate;
102}
103
fd668423
MT
104static s64 __kpit_elapsed(struct kvm *kvm)
105{
106 s64 elapsed;
107 ktime_t remaining;
108 struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
109
0ff77873
MT
110 if (!ps->pit_timer.period)
111 return 0;
112
ede2ccc5
MT
113 /*
114 * The Counter does not stop when it reaches zero. In
115 * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
116 * the highest count, either FFFF hex for binary counting
117 * or 9999 for BCD counting, and continues counting.
118 * Modes 2 and 3 are periodic; the Counter reloads
119 * itself with the initial count and continues counting
120 * from there.
121 */
ace15464 122 remaining = hrtimer_get_remaining(&ps->pit_timer.timer);
ede2ccc5
MT
123 elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
124 elapsed = mod_64(elapsed, ps->pit_timer.period);
fd668423
MT
125
126 return elapsed;
127}
128
129static s64 kpit_elapsed(struct kvm *kvm, struct kvm_kpit_channel_state *c,
130 int channel)
131{
132 if (channel == 0)
133 return __kpit_elapsed(kvm);
134
135 return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
136}
137
7837699f
SY
138static int pit_get_count(struct kvm *kvm, int channel)
139{
140 struct kvm_kpit_channel_state *c =
141 &kvm->arch.vpit->pit_state.channels[channel];
142 s64 d, t;
143 int counter;
144
145 WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
146
fd668423 147 t = kpit_elapsed(kvm, c, channel);
7837699f
SY
148 d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
149
150 switch (c->mode) {
151 case 0:
152 case 1:
153 case 4:
154 case 5:
155 counter = (c->count - d) & 0xffff;
156 break;
157 case 3:
158 /* XXX: may be incorrect for odd counts */
159 counter = c->count - (mod_64((2 * d), c->count));
160 break;
161 default:
162 counter = c->count - mod_64(d, c->count);
163 break;
164 }
165 return counter;
166}
167
168static int pit_get_out(struct kvm *kvm, int channel)
169{
170 struct kvm_kpit_channel_state *c =
171 &kvm->arch.vpit->pit_state.channels[channel];
172 s64 d, t;
173 int out;
174
175 WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
176
fd668423 177 t = kpit_elapsed(kvm, c, channel);
7837699f
SY
178 d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
179
180 switch (c->mode) {
181 default:
182 case 0:
183 out = (d >= c->count);
184 break;
185 case 1:
186 out = (d < c->count);
187 break;
188 case 2:
189 out = ((mod_64(d, c->count) == 0) && (d != 0));
190 break;
191 case 3:
192 out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
193 break;
194 case 4:
195 case 5:
196 out = (d == c->count);
197 break;
198 }
199
200 return out;
201}
202
203static void pit_latch_count(struct kvm *kvm, int channel)
204{
205 struct kvm_kpit_channel_state *c =
206 &kvm->arch.vpit->pit_state.channels[channel];
207
208 WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
209
210 if (!c->count_latched) {
211 c->latched_count = pit_get_count(kvm, channel);
212 c->count_latched = c->rw_mode;
213 }
214}
215
216static void pit_latch_status(struct kvm *kvm, int channel)
217{
218 struct kvm_kpit_channel_state *c =
219 &kvm->arch.vpit->pit_state.channels[channel];
220
221 WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
222
223 if (!c->status_latched) {
224 /* TODO: Return NULL COUNT (bit 6). */
225 c->status = ((pit_get_out(kvm, channel) << 7) |
226 (c->rw_mode << 4) |
227 (c->mode << 1) |
228 c->bcd);
229 c->status_latched = 1;
230 }
231}
232
3d80840d
MT
233int pit_has_pending_timer(struct kvm_vcpu *vcpu)
234{
235 struct kvm_pit *pit = vcpu->kvm->arch.vpit;
236
c5af89b6 237 if (pit && kvm_vcpu_is_bsp(vcpu) && pit->pit_state.irq_ack)
3d80840d 238 return atomic_read(&pit->pit_state.pit_timer.pending);
3d80840d
MT
239 return 0;
240}
241
ee032c99 242static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
3cf57fed
MT
243{
244 struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
245 irq_ack_notifier);
fa8273e9 246 raw_spin_lock(&ps->inject_lock);
3cf57fed 247 if (atomic_dec_return(&ps->pit_timer.pending) < 0)
dc7404ce 248 atomic_inc(&ps->pit_timer.pending);
3cf57fed 249 ps->irq_ack = 1;
fa8273e9 250 raw_spin_unlock(&ps->inject_lock);
3cf57fed
MT
251}
252
2f599714
MT
253void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
254{
255 struct kvm_pit *pit = vcpu->kvm->arch.vpit;
256 struct hrtimer *timer;
257
c5af89b6 258 if (!kvm_vcpu_is_bsp(vcpu) || !pit)
2f599714
MT
259 return;
260
261 timer = &pit->pit_state.pit_timer.timer;
262 if (hrtimer_cancel(timer))
beb20d52 263 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
2f599714
MT
264}
265
d3c7b77d 266static void destroy_pit_timer(struct kvm_timer *pt)
7837699f 267{
a78d9626 268 pr_debug("execute del timer!\n");
7837699f
SY
269 hrtimer_cancel(&pt->timer);
270}
271
d3c7b77d
MT
272static bool kpit_is_periodic(struct kvm_timer *ktimer)
273{
274 struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state,
275 pit_timer);
276 return ps->is_periodic;
277}
278
386eb6e8 279static struct kvm_timer_ops kpit_ops = {
d3c7b77d
MT
280 .is_periodic = kpit_is_periodic,
281};
282
3cf57fed 283static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
7837699f 284{
d3c7b77d 285 struct kvm_timer *pt = &ps->pit_timer;
7837699f
SY
286 s64 interval;
287
288 interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
289
a78d9626 290 pr_debug("create pit timer, interval is %llu nsec\n", interval);
7837699f
SY
291
292 /* TODO The new value only affected after the retriggered */
293 hrtimer_cancel(&pt->timer);
ede2ccc5 294 pt->period = interval;
d3c7b77d
MT
295 ps->is_periodic = is_period;
296
297 pt->timer.function = kvm_timer_fn;
298 pt->t_ops = &kpit_ops;
299 pt->kvm = ps->pit->kvm;
1ed0ce00 300 pt->vcpu = pt->kvm->bsp_vcpu;
d3c7b77d 301
7837699f 302 atomic_set(&pt->pending, 0);
3cf57fed 303 ps->irq_ack = 1;
7837699f
SY
304
305 hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
306 HRTIMER_MODE_ABS);
307}
308
309static void pit_load_count(struct kvm *kvm, int channel, u32 val)
310{
311 struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
312
313 WARN_ON(!mutex_is_locked(&ps->lock));
314
a78d9626 315 pr_debug("load_count val is %d, channel is %d\n", val, channel);
7837699f
SY
316
317 /*
ede2ccc5
MT
318 * The largest possible initial count is 0; this is equivalent
319 * to 216 for binary counting and 104 for BCD counting.
7837699f
SY
320 */
321 if (val == 0)
322 val = 0x10000;
323
7837699f
SY
324 ps->channels[channel].count = val;
325
fd668423
MT
326 if (channel != 0) {
327 ps->channels[channel].count_load_time = ktime_get();
7837699f 328 return;
fd668423 329 }
7837699f
SY
330
331 /* Two types of timer
332 * mode 1 is one shot, mode 2 is period, otherwise del timer */
333 switch (ps->channels[0].mode) {
ede2ccc5 334 case 0:
7837699f 335 case 1:
ece15bab
MT
336 /* FIXME: enhance mode 4 precision */
337 case 4:
e9f42757
BK
338 if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)) {
339 create_pit_timer(ps, val, 0);
340 }
7837699f
SY
341 break;
342 case 2:
f6975545 343 case 3:
e9f42757
BK
344 if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)){
345 create_pit_timer(ps, val, 1);
346 }
7837699f
SY
347 break;
348 default:
349 destroy_pit_timer(&ps->pit_timer);
350 }
351}
352
e9f42757 353void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start)
e0f63cb9 354{
e9f42757
BK
355 u8 saved_mode;
356 if (hpet_legacy_start) {
357 /* save existing mode for later reenablement */
358 saved_mode = kvm->arch.vpit->pit_state.channels[0].mode;
359 kvm->arch.vpit->pit_state.channels[0].mode = 0xff; /* disable timer */
360 pit_load_count(kvm, channel, val);
361 kvm->arch.vpit->pit_state.channels[0].mode = saved_mode;
362 } else {
363 pit_load_count(kvm, channel, val);
364 }
e0f63cb9
SY
365}
366
d76685c4
GH
367static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
368{
369 return container_of(dev, struct kvm_pit, dev);
370}
371
372static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
373{
374 return container_of(dev, struct kvm_pit, speaker_dev);
375}
376
bda9020e
MT
377static inline int pit_in_range(gpa_t addr)
378{
379 return ((addr >= KVM_PIT_BASE_ADDRESS) &&
380 (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
381}
382
383static int pit_ioport_write(struct kvm_io_device *this,
384 gpa_t addr, int len, const void *data)
7837699f 385{
d76685c4 386 struct kvm_pit *pit = dev_to_pit(this);
7837699f
SY
387 struct kvm_kpit_state *pit_state = &pit->pit_state;
388 struct kvm *kvm = pit->kvm;
389 int channel, access;
390 struct kvm_kpit_channel_state *s;
391 u32 val = *(u32 *) data;
bda9020e
MT
392 if (!pit_in_range(addr))
393 return -EOPNOTSUPP;
7837699f
SY
394
395 val &= 0xff;
396 addr &= KVM_PIT_CHANNEL_MASK;
397
398 mutex_lock(&pit_state->lock);
399
400 if (val != 0)
a78d9626
JP
401 pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
402 (unsigned int)addr, len, val);
7837699f
SY
403
404 if (addr == 3) {
405 channel = val >> 6;
406 if (channel == 3) {
407 /* Read-Back Command. */
408 for (channel = 0; channel < 3; channel++) {
409 s = &pit_state->channels[channel];
410 if (val & (2 << channel)) {
411 if (!(val & 0x20))
412 pit_latch_count(kvm, channel);
413 if (!(val & 0x10))
414 pit_latch_status(kvm, channel);
415 }
416 }
417 } else {
418 /* Select Counter <channel>. */
419 s = &pit_state->channels[channel];
420 access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
421 if (access == 0) {
422 pit_latch_count(kvm, channel);
423 } else {
424 s->rw_mode = access;
425 s->read_state = access;
426 s->write_state = access;
427 s->mode = (val >> 1) & 7;
428 if (s->mode > 5)
429 s->mode -= 4;
430 s->bcd = val & 1;
431 }
432 }
433 } else {
434 /* Write Count. */
435 s = &pit_state->channels[addr];
436 switch (s->write_state) {
437 default:
438 case RW_STATE_LSB:
439 pit_load_count(kvm, addr, val);
440 break;
441 case RW_STATE_MSB:
442 pit_load_count(kvm, addr, val << 8);
443 break;
444 case RW_STATE_WORD0:
445 s->write_latch = val;
446 s->write_state = RW_STATE_WORD1;
447 break;
448 case RW_STATE_WORD1:
449 pit_load_count(kvm, addr, s->write_latch | (val << 8));
450 s->write_state = RW_STATE_WORD0;
451 break;
452 }
453 }
454
455 mutex_unlock(&pit_state->lock);
bda9020e 456 return 0;
7837699f
SY
457}
458
bda9020e
MT
459static int pit_ioport_read(struct kvm_io_device *this,
460 gpa_t addr, int len, void *data)
7837699f 461{
d76685c4 462 struct kvm_pit *pit = dev_to_pit(this);
7837699f
SY
463 struct kvm_kpit_state *pit_state = &pit->pit_state;
464 struct kvm *kvm = pit->kvm;
465 int ret, count;
466 struct kvm_kpit_channel_state *s;
bda9020e
MT
467 if (!pit_in_range(addr))
468 return -EOPNOTSUPP;
7837699f
SY
469
470 addr &= KVM_PIT_CHANNEL_MASK;
ee73f656
MT
471 if (addr == 3)
472 return 0;
473
7837699f
SY
474 s = &pit_state->channels[addr];
475
476 mutex_lock(&pit_state->lock);
477
478 if (s->status_latched) {
479 s->status_latched = 0;
480 ret = s->status;
481 } else if (s->count_latched) {
482 switch (s->count_latched) {
483 default:
484 case RW_STATE_LSB:
485 ret = s->latched_count & 0xff;
486 s->count_latched = 0;
487 break;
488 case RW_STATE_MSB:
489 ret = s->latched_count >> 8;
490 s->count_latched = 0;
491 break;
492 case RW_STATE_WORD0:
493 ret = s->latched_count & 0xff;
494 s->count_latched = RW_STATE_MSB;
495 break;
496 }
497 } else {
498 switch (s->read_state) {
499 default:
500 case RW_STATE_LSB:
501 count = pit_get_count(kvm, addr);
502 ret = count & 0xff;
503 break;
504 case RW_STATE_MSB:
505 count = pit_get_count(kvm, addr);
506 ret = (count >> 8) & 0xff;
507 break;
508 case RW_STATE_WORD0:
509 count = pit_get_count(kvm, addr);
510 ret = count & 0xff;
511 s->read_state = RW_STATE_WORD1;
512 break;
513 case RW_STATE_WORD1:
514 count = pit_get_count(kvm, addr);
515 ret = (count >> 8) & 0xff;
516 s->read_state = RW_STATE_WORD0;
517 break;
518 }
519 }
520
521 if (len > sizeof(ret))
522 len = sizeof(ret);
523 memcpy(data, (char *)&ret, len);
524
525 mutex_unlock(&pit_state->lock);
bda9020e 526 return 0;
7837699f
SY
527}
528
bda9020e
MT
529static int speaker_ioport_write(struct kvm_io_device *this,
530 gpa_t addr, int len, const void *data)
7837699f 531{
d76685c4 532 struct kvm_pit *pit = speaker_to_pit(this);
7837699f
SY
533 struct kvm_kpit_state *pit_state = &pit->pit_state;
534 struct kvm *kvm = pit->kvm;
535 u32 val = *(u32 *) data;
bda9020e
MT
536 if (addr != KVM_SPEAKER_BASE_ADDRESS)
537 return -EOPNOTSUPP;
7837699f
SY
538
539 mutex_lock(&pit_state->lock);
540 pit_state->speaker_data_on = (val >> 1) & 1;
541 pit_set_gate(kvm, 2, val & 1);
542 mutex_unlock(&pit_state->lock);
bda9020e 543 return 0;
7837699f
SY
544}
545
bda9020e
MT
546static int speaker_ioport_read(struct kvm_io_device *this,
547 gpa_t addr, int len, void *data)
7837699f 548{
d76685c4 549 struct kvm_pit *pit = speaker_to_pit(this);
7837699f
SY
550 struct kvm_kpit_state *pit_state = &pit->pit_state;
551 struct kvm *kvm = pit->kvm;
552 unsigned int refresh_clock;
553 int ret;
bda9020e
MT
554 if (addr != KVM_SPEAKER_BASE_ADDRESS)
555 return -EOPNOTSUPP;
7837699f
SY
556
557 /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
558 refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
559
560 mutex_lock(&pit_state->lock);
561 ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
562 (pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
563 if (len > sizeof(ret))
564 len = sizeof(ret);
565 memcpy(data, (char *)&ret, len);
566 mutex_unlock(&pit_state->lock);
bda9020e 567 return 0;
7837699f
SY
568}
569
308b0f23 570void kvm_pit_reset(struct kvm_pit *pit)
7837699f
SY
571{
572 int i;
308b0f23
SY
573 struct kvm_kpit_channel_state *c;
574
575 mutex_lock(&pit->pit_state.lock);
e9f42757 576 pit->pit_state.flags = 0;
308b0f23
SY
577 for (i = 0; i < 3; i++) {
578 c = &pit->pit_state.channels[i];
579 c->mode = 0xff;
580 c->gate = (i != 2);
581 pit_load_count(pit->kvm, i, 0);
582 }
583 mutex_unlock(&pit->pit_state.lock);
584
585 atomic_set(&pit->pit_state.pit_timer.pending, 0);
3cf57fed 586 pit->pit_state.irq_ack = 1;
308b0f23
SY
587}
588
4780c659
AK
589static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
590{
591 struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
592
593 if (!mask) {
594 atomic_set(&pit->pit_state.pit_timer.pending, 0);
595 pit->pit_state.irq_ack = 1;
596 }
597}
598
d76685c4
GH
599static const struct kvm_io_device_ops pit_dev_ops = {
600 .read = pit_ioport_read,
601 .write = pit_ioport_write,
d76685c4
GH
602};
603
604static const struct kvm_io_device_ops speaker_dev_ops = {
605 .read = speaker_ioport_read,
606 .write = speaker_ioport_write,
d76685c4
GH
607};
608
79fac95e 609/* Caller must hold slots_lock */
c5ff41ce 610struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
308b0f23 611{
7837699f
SY
612 struct kvm_pit *pit;
613 struct kvm_kpit_state *pit_state;
090b7aff 614 int ret;
7837699f
SY
615
616 pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
617 if (!pit)
618 return NULL;
619
5550af4d 620 pit->irq_source_id = kvm_request_irq_source_id(kvm);
e17d1dc0
AK
621 if (pit->irq_source_id < 0) {
622 kfree(pit);
5550af4d 623 return NULL;
e17d1dc0 624 }
5550af4d 625
7837699f
SY
626 mutex_init(&pit->pit_state.lock);
627 mutex_lock(&pit->pit_state.lock);
fa8273e9 628 raw_spin_lock_init(&pit->pit_state.inject_lock);
7837699f 629
7837699f
SY
630 kvm->arch.vpit = pit;
631 pit->kvm = kvm;
632
633 pit_state = &pit->pit_state;
634 pit_state->pit = pit;
635 hrtimer_init(&pit_state->pit_timer.timer,
636 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
3cf57fed
MT
637 pit_state->irq_ack_notifier.gsi = 0;
638 pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
639 kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
52d939a0 640 pit_state->pit_timer.reinject = true;
7837699f
SY
641 mutex_unlock(&pit->pit_state.lock);
642
308b0f23 643 kvm_pit_reset(pit);
7837699f 644
4780c659
AK
645 pit->mask_notifier.func = pit_mask_notifer;
646 kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
647
6b66ac1a 648 kvm_iodevice_init(&pit->dev, &pit_dev_ops);
e93f8a0f 649 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &pit->dev);
090b7aff
GH
650 if (ret < 0)
651 goto fail;
6b66ac1a
GH
652
653 if (flags & KVM_PIT_SPEAKER_DUMMY) {
654 kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
e93f8a0f 655 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
090b7aff
GH
656 &pit->speaker_dev);
657 if (ret < 0)
658 goto fail_unregister;
6b66ac1a
GH
659 }
660
7837699f 661 return pit;
090b7aff
GH
662
663fail_unregister:
e93f8a0f 664 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
090b7aff
GH
665
666fail:
d225f53b
WY
667 kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
668 kvm_unregister_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
669 kvm_free_irq_source_id(kvm, pit->irq_source_id);
090b7aff
GH
670
671 kfree(pit);
672 return NULL;
7837699f
SY
673}
674
675void kvm_free_pit(struct kvm *kvm)
676{
677 struct hrtimer *timer;
678
679 if (kvm->arch.vpit) {
4780c659
AK
680 kvm_unregister_irq_mask_notifier(kvm, 0,
681 &kvm->arch.vpit->mask_notifier);
84fde248
GN
682 kvm_unregister_irq_ack_notifier(kvm,
683 &kvm->arch.vpit->pit_state.irq_ack_notifier);
7837699f
SY
684 mutex_lock(&kvm->arch.vpit->pit_state.lock);
685 timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
686 hrtimer_cancel(timer);
5550af4d 687 kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
7837699f
SY
688 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
689 kfree(kvm->arch.vpit);
690 }
691}
692
8b2cf73c 693static void __inject_pit_timer_intr(struct kvm *kvm)
7837699f 694{
23930f95
JK
695 struct kvm_vcpu *vcpu;
696 int i;
697
5550af4d
SY
698 kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
699 kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
23930f95
JK
700
701 /*
8fdb2351
JK
702 * Provides NMI watchdog support via Virtual Wire mode.
703 * The route is: PIT -> PIC -> LVT0 in NMI mode.
704 *
705 * Note: Our Virtual Wire implementation is simplified, only
706 * propagating PIT interrupts to all VCPUs when they have set
707 * LVT0 to NMI delivery. Other PIC interrupts are just sent to
708 * VCPU0, and only if its LVT0 is in EXTINT mode.
23930f95 709 */
cc6e462c 710 if (kvm->arch.vapics_in_nmi_mode > 0)
988a2cae
GN
711 kvm_for_each_vcpu(i, vcpu, kvm)
712 kvm_apic_nmi_wd_deliver(vcpu);
7837699f
SY
713}
714
715void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
716{
717 struct kvm_pit *pit = vcpu->kvm->arch.vpit;
718 struct kvm *kvm = vcpu->kvm;
719 struct kvm_kpit_state *ps;
720
95fb4eb6 721 if (pit) {
3cf57fed 722 int inject = 0;
7837699f
SY
723 ps = &pit->pit_state;
724
3cf57fed
MT
725 /* Try to inject pending interrupts when
726 * last one has been acked.
727 */
fa8273e9 728 raw_spin_lock(&ps->inject_lock);
3cf57fed
MT
729 if (atomic_read(&ps->pit_timer.pending) && ps->irq_ack) {
730 ps->irq_ack = 0;
731 inject = 1;
7837699f 732 }
fa8273e9 733 raw_spin_unlock(&ps->inject_lock);
3cf57fed
MT
734 if (inject)
735 __inject_pit_timer_intr(kvm);
7837699f
SY
736 }
737}