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