xref: /openbmc/linux/arch/x86/kvm/i8254.c (revision 0214f6bbe564632adba299e38023d681c1bd68c5)
17837699fSSheng Yang /*
27837699fSSheng Yang  * 8253/8254 interval timer emulation
37837699fSSheng Yang  *
47837699fSSheng Yang  * Copyright (c) 2003-2004 Fabrice Bellard
57837699fSSheng Yang  * Copyright (c) 2006 Intel Corporation
67837699fSSheng Yang  * Copyright (c) 2007 Keir Fraser, XenSource Inc
77837699fSSheng Yang  * Copyright (c) 2008 Intel Corporation
89611c187SNicolas Kaiser  * Copyright 2009 Red Hat, Inc. and/or its affiliates.
97837699fSSheng Yang  *
107837699fSSheng Yang  * Permission is hereby granted, free of charge, to any person obtaining a copy
117837699fSSheng Yang  * of this software and associated documentation files (the "Software"), to deal
127837699fSSheng Yang  * in the Software without restriction, including without limitation the rights
137837699fSSheng Yang  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
147837699fSSheng Yang  * copies of the Software, and to permit persons to whom the Software is
157837699fSSheng Yang  * furnished to do so, subject to the following conditions:
167837699fSSheng Yang  *
177837699fSSheng Yang  * The above copyright notice and this permission notice shall be included in
187837699fSSheng Yang  * all copies or substantial portions of the Software.
197837699fSSheng Yang  *
207837699fSSheng Yang  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
217837699fSSheng Yang  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
227837699fSSheng Yang  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
237837699fSSheng Yang  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
247837699fSSheng Yang  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
257837699fSSheng Yang  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
267837699fSSheng Yang  * THE SOFTWARE.
277837699fSSheng Yang  *
287837699fSSheng Yang  * Authors:
297837699fSSheng Yang  *   Sheng Yang <sheng.yang@intel.com>
307837699fSSheng Yang  *   Based on QEMU and Xen.
317837699fSSheng Yang  */
327837699fSSheng Yang 
33a78d9626SJoe Perches #define pr_fmt(fmt) "pit: " fmt
34a78d9626SJoe Perches 
357837699fSSheng Yang #include <linux/kvm_host.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
377837699fSSheng Yang 
3849df6397SSteve Rutherford #include "ioapic.h"
397837699fSSheng Yang #include "irq.h"
407837699fSSheng Yang #include "i8254.h"
419ed96e87SMarcelo Tosatti #include "x86.h"
427837699fSSheng Yang 
437837699fSSheng Yang #ifndef CONFIG_X86_64
446f6d6a1aSRoman Zippel #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
457837699fSSheng Yang #else
467837699fSSheng Yang #define mod_64(x, y) ((x) % (y))
477837699fSSheng Yang #endif
487837699fSSheng Yang 
497837699fSSheng Yang #define RW_STATE_LSB 1
507837699fSSheng Yang #define RW_STATE_MSB 2
517837699fSSheng Yang #define RW_STATE_WORD0 3
527837699fSSheng Yang #define RW_STATE_WORD1 4
537837699fSSheng Yang 
5409edea72SRadim Krčmář static void pit_set_gate(struct kvm_pit *pit, int channel, u32 val)
557837699fSSheng Yang {
5609edea72SRadim Krčmář 	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
577837699fSSheng Yang 
587837699fSSheng Yang 	switch (c->mode) {
597837699fSSheng Yang 	default:
607837699fSSheng Yang 	case 0:
617837699fSSheng Yang 	case 4:
627837699fSSheng Yang 		/* XXX: just disable/enable counting */
637837699fSSheng Yang 		break;
647837699fSSheng Yang 	case 1:
657837699fSSheng Yang 	case 2:
667837699fSSheng Yang 	case 3:
677837699fSSheng Yang 	case 5:
687837699fSSheng Yang 		/* Restart counting on rising edge. */
697837699fSSheng Yang 		if (c->gate < val)
707837699fSSheng Yang 			c->count_load_time = ktime_get();
717837699fSSheng Yang 		break;
727837699fSSheng Yang 	}
737837699fSSheng Yang 
747837699fSSheng Yang 	c->gate = val;
757837699fSSheng Yang }
767837699fSSheng Yang 
7709edea72SRadim Krčmář static int pit_get_gate(struct kvm_pit *pit, int channel)
787837699fSSheng Yang {
7909edea72SRadim Krčmář 	return pit->pit_state.channels[channel].gate;
807837699fSSheng Yang }
817837699fSSheng Yang 
8209edea72SRadim Krčmář static s64 __kpit_elapsed(struct kvm_pit *pit)
83fd668423SMarcelo Tosatti {
84fd668423SMarcelo Tosatti 	s64 elapsed;
85fd668423SMarcelo Tosatti 	ktime_t remaining;
8609edea72SRadim Krčmář 	struct kvm_kpit_state *ps = &pit->pit_state;
87fd668423SMarcelo Tosatti 
8826ef1924SAvi Kivity 	if (!ps->period)
890ff77873SMarcelo Tosatti 		return 0;
900ff77873SMarcelo Tosatti 
91ede2ccc5SMarcelo Tosatti 	/*
92ede2ccc5SMarcelo Tosatti 	 * The Counter does not stop when it reaches zero. In
93ede2ccc5SMarcelo Tosatti 	 * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
94ede2ccc5SMarcelo Tosatti 	 * the highest count, either FFFF hex for binary counting
95ede2ccc5SMarcelo Tosatti 	 * or 9999 for BCD counting, and continues counting.
96ede2ccc5SMarcelo Tosatti 	 * Modes 2 and 3 are periodic; the Counter reloads
97ede2ccc5SMarcelo Tosatti 	 * itself with the initial count and continues counting
98ede2ccc5SMarcelo Tosatti 	 * from there.
99ede2ccc5SMarcelo Tosatti 	 */
10026ef1924SAvi Kivity 	remaining = hrtimer_get_remaining(&ps->timer);
10126ef1924SAvi Kivity 	elapsed = ps->period - ktime_to_ns(remaining);
102fd668423SMarcelo Tosatti 
103fd668423SMarcelo Tosatti 	return elapsed;
104fd668423SMarcelo Tosatti }
105fd668423SMarcelo Tosatti 
10609edea72SRadim Krčmář static s64 kpit_elapsed(struct kvm_pit *pit, struct kvm_kpit_channel_state *c,
107fd668423SMarcelo Tosatti 			int channel)
108fd668423SMarcelo Tosatti {
109fd668423SMarcelo Tosatti 	if (channel == 0)
11009edea72SRadim Krčmář 		return __kpit_elapsed(pit);
111fd668423SMarcelo Tosatti 
112fd668423SMarcelo Tosatti 	return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
113fd668423SMarcelo Tosatti }
114fd668423SMarcelo Tosatti 
11509edea72SRadim Krčmář static int pit_get_count(struct kvm_pit *pit, int channel)
1167837699fSSheng Yang {
11709edea72SRadim Krčmář 	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
1187837699fSSheng Yang 	s64 d, t;
1197837699fSSheng Yang 	int counter;
1207837699fSSheng Yang 
12109edea72SRadim Krčmář 	t = kpit_elapsed(pit, c, channel);
1220e4d4415SPaolo Bonzini 	d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC);
1237837699fSSheng Yang 
1247837699fSSheng Yang 	switch (c->mode) {
1257837699fSSheng Yang 	case 0:
1267837699fSSheng Yang 	case 1:
1277837699fSSheng Yang 	case 4:
1287837699fSSheng Yang 	case 5:
1297837699fSSheng Yang 		counter = (c->count - d) & 0xffff;
1307837699fSSheng Yang 		break;
1317837699fSSheng Yang 	case 3:
1327837699fSSheng Yang 		/* XXX: may be incorrect for odd counts */
1337837699fSSheng Yang 		counter = c->count - (mod_64((2 * d), c->count));
1347837699fSSheng Yang 		break;
1357837699fSSheng Yang 	default:
1367837699fSSheng Yang 		counter = c->count - mod_64(d, c->count);
1377837699fSSheng Yang 		break;
1387837699fSSheng Yang 	}
1397837699fSSheng Yang 	return counter;
1407837699fSSheng Yang }
1417837699fSSheng Yang 
14209edea72SRadim Krčmář static int pit_get_out(struct kvm_pit *pit, int channel)
1437837699fSSheng Yang {
14409edea72SRadim Krčmář 	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
1457837699fSSheng Yang 	s64 d, t;
1467837699fSSheng Yang 	int out;
1477837699fSSheng Yang 
14809edea72SRadim Krčmář 	t = kpit_elapsed(pit, c, channel);
1490e4d4415SPaolo Bonzini 	d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC);
1507837699fSSheng Yang 
1517837699fSSheng Yang 	switch (c->mode) {
1527837699fSSheng Yang 	default:
1537837699fSSheng Yang 	case 0:
1547837699fSSheng Yang 		out = (d >= c->count);
1557837699fSSheng Yang 		break;
1567837699fSSheng Yang 	case 1:
1577837699fSSheng Yang 		out = (d < c->count);
1587837699fSSheng Yang 		break;
1597837699fSSheng Yang 	case 2:
1607837699fSSheng Yang 		out = ((mod_64(d, c->count) == 0) && (d != 0));
1617837699fSSheng Yang 		break;
1627837699fSSheng Yang 	case 3:
1637837699fSSheng Yang 		out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
1647837699fSSheng Yang 		break;
1657837699fSSheng Yang 	case 4:
1667837699fSSheng Yang 	case 5:
1677837699fSSheng Yang 		out = (d == c->count);
1687837699fSSheng Yang 		break;
1697837699fSSheng Yang 	}
1707837699fSSheng Yang 
1717837699fSSheng Yang 	return out;
1727837699fSSheng Yang }
1737837699fSSheng Yang 
17409edea72SRadim Krčmář static void pit_latch_count(struct kvm_pit *pit, int channel)
1757837699fSSheng Yang {
17609edea72SRadim Krčmář 	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
1777837699fSSheng Yang 
1787837699fSSheng Yang 	if (!c->count_latched) {
17909edea72SRadim Krčmář 		c->latched_count = pit_get_count(pit, channel);
1807837699fSSheng Yang 		c->count_latched = c->rw_mode;
1817837699fSSheng Yang 	}
1827837699fSSheng Yang }
1837837699fSSheng Yang 
18409edea72SRadim Krčmář static void pit_latch_status(struct kvm_pit *pit, int channel)
1857837699fSSheng Yang {
18609edea72SRadim Krčmář 	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
1877837699fSSheng Yang 
1887837699fSSheng Yang 	if (!c->status_latched) {
1897837699fSSheng Yang 		/* TODO: Return NULL COUNT (bit 6). */
19009edea72SRadim Krčmář 		c->status = ((pit_get_out(pit, channel) << 7) |
1917837699fSSheng Yang 				(c->rw_mode << 4) |
1927837699fSSheng Yang 				(c->mode << 1) |
1937837699fSSheng Yang 				c->bcd);
1947837699fSSheng Yang 		c->status_latched = 1;
1957837699fSSheng Yang 	}
1967837699fSSheng Yang }
1977837699fSSheng Yang 
198a3e13115SRadim Krčmář static inline struct kvm_pit *pit_state_to_pit(struct kvm_kpit_state *ps)
199a3e13115SRadim Krčmář {
200a3e13115SRadim Krčmář 	return container_of(ps, struct kvm_pit, pit_state);
201a3e13115SRadim Krčmář }
202a3e13115SRadim Krčmář 
203ee032c99SHarvey Harrison static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
2043cf57fedSMarcelo Tosatti {
2053cf57fedSMarcelo Tosatti 	struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
2063cf57fedSMarcelo Tosatti 						 irq_ack_notifier);
207a3e13115SRadim Krčmář 	struct kvm_pit *pit = pit_state_to_pit(ps);
20833572ac0SChris Lalancette 
209ddf54503SRadim Krčmář 	atomic_set(&ps->irq_ack, 1);
210ddf54503SRadim Krčmář 	/* irq_ack should be set before pending is read.  Order accesses with
211ddf54503SRadim Krčmář 	 * inc(pending) in pit_timer_fn and xchg(irq_ack, 0) in pit_do_work.
212ddf54503SRadim Krčmář 	 */
213ddf54503SRadim Krčmář 	smp_mb();
21471474e2fSRadim Krčmář 	if (atomic_dec_if_positive(&ps->pending) > 0)
21536da91bdSPetr Mladek 		kthread_queue_work(pit->worker, &pit->expired);
2163cf57fedSMarcelo Tosatti }
2173cf57fedSMarcelo Tosatti 
2182f599714SMarcelo Tosatti void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
2192f599714SMarcelo Tosatti {
2202f599714SMarcelo Tosatti 	struct kvm_pit *pit = vcpu->kvm->arch.vpit;
2212f599714SMarcelo Tosatti 	struct hrtimer *timer;
2222f599714SMarcelo Tosatti 
223*0214f6bbSSean Christopherson 	/* Somewhat arbitrarily make vcpu0 the owner of the PIT. */
224*0214f6bbSSean Christopherson 	if (vcpu->vcpu_id || !pit)
2252f599714SMarcelo Tosatti 		return;
2262f599714SMarcelo Tosatti 
22726ef1924SAvi Kivity 	timer = &pit->pit_state.timer;
2282febc839SAndy Honig 	mutex_lock(&pit->pit_state.lock);
2292f599714SMarcelo Tosatti 	if (hrtimer_cancel(timer))
230beb20d52SArjan van de Ven 		hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
2312febc839SAndy Honig 	mutex_unlock(&pit->pit_state.lock);
2322f599714SMarcelo Tosatti }
2332f599714SMarcelo Tosatti 
23433572ac0SChris Lalancette static void destroy_pit_timer(struct kvm_pit *pit)
2357837699fSSheng Yang {
23626ef1924SAvi Kivity 	hrtimer_cancel(&pit->pit_state.timer);
2373989144fSPetr Mladek 	kthread_flush_work(&pit->expired);
2387837699fSSheng Yang }
2397837699fSSheng Yang 
240b6ddf05fSJan Kiszka static void pit_do_work(struct kthread_work *work)
24133572ac0SChris Lalancette {
24233572ac0SChris Lalancette 	struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
24333572ac0SChris Lalancette 	struct kvm *kvm = pit->kvm;
24433572ac0SChris Lalancette 	struct kvm_vcpu *vcpu;
24533572ac0SChris Lalancette 	int i;
24633572ac0SChris Lalancette 	struct kvm_kpit_state *ps = &pit->pit_state;
24733572ac0SChris Lalancette 
248a0aace5aSRadim Krčmář 	if (atomic_read(&ps->reinject) && !atomic_xchg(&ps->irq_ack, 0))
249ddf54503SRadim Krčmář 		return;
250ddf54503SRadim Krčmář 
2514a2095dfSRadim Krčmář 	kvm_set_irq(kvm, pit->irq_source_id, 0, 1, false);
2524a2095dfSRadim Krčmář 	kvm_set_irq(kvm, pit->irq_source_id, 0, 0, false);
25333572ac0SChris Lalancette 
25433572ac0SChris Lalancette 	/*
25533572ac0SChris Lalancette 	 * Provides NMI watchdog support via Virtual Wire mode.
256ddf54503SRadim Krčmář 	 * The route is: PIT -> LVT0 in NMI mode.
25733572ac0SChris Lalancette 	 *
258ddf54503SRadim Krčmář 	 * Note: Our Virtual Wire implementation does not follow
259ddf54503SRadim Krčmář 	 * the MP specification.  We propagate a PIT interrupt to all
260ddf54503SRadim Krčmář 	 * VCPUs and only when LVT0 is in NMI mode.  The interrupt can
261ddf54503SRadim Krčmář 	 * also be simultaneously delivered through PIC and IOAPIC.
26233572ac0SChris Lalancette 	 */
26342720138SRadim Krčmář 	if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0)
26433572ac0SChris Lalancette 		kvm_for_each_vcpu(i, vcpu, kvm)
26533572ac0SChris Lalancette 			kvm_apic_nmi_wd_deliver(vcpu);
26633572ac0SChris Lalancette }
26733572ac0SChris Lalancette 
26833572ac0SChris Lalancette static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
26933572ac0SChris Lalancette {
27026ef1924SAvi Kivity 	struct kvm_kpit_state *ps = container_of(data, struct kvm_kpit_state, timer);
271a3e13115SRadim Krčmář 	struct kvm_pit *pt = pit_state_to_pit(ps);
27233572ac0SChris Lalancette 
273a0aace5aSRadim Krčmář 	if (atomic_read(&ps->reinject))
27426ef1924SAvi Kivity 		atomic_inc(&ps->pending);
2757dd0fdffSRadim Krčmář 
27636da91bdSPetr Mladek 	kthread_queue_work(pt->worker, &pt->expired);
27733572ac0SChris Lalancette 
27826ef1924SAvi Kivity 	if (ps->is_periodic) {
27926ef1924SAvi Kivity 		hrtimer_add_expires_ns(&ps->timer, ps->period);
28033572ac0SChris Lalancette 		return HRTIMER_RESTART;
28133572ac0SChris Lalancette 	} else
28233572ac0SChris Lalancette 		return HRTIMER_NORESTART;
28333572ac0SChris Lalancette }
28433572ac0SChris Lalancette 
285fd700a00SRadim Krčmář static inline void kvm_pit_reset_reinject(struct kvm_pit *pit)
286fd700a00SRadim Krčmář {
287fd700a00SRadim Krčmář 	atomic_set(&pit->pit_state.pending, 0);
288ddf54503SRadim Krčmář 	atomic_set(&pit->pit_state.irq_ack, 1);
289fd700a00SRadim Krčmář }
290fd700a00SRadim Krčmář 
29171474e2fSRadim Krčmář void kvm_pit_set_reinject(struct kvm_pit *pit, bool reinject)
29271474e2fSRadim Krčmář {
29371474e2fSRadim Krčmář 	struct kvm_kpit_state *ps = &pit->pit_state;
29471474e2fSRadim Krčmář 	struct kvm *kvm = pit->kvm;
29571474e2fSRadim Krčmář 
296a0aace5aSRadim Krčmář 	if (atomic_read(&ps->reinject) == reinject)
29771474e2fSRadim Krčmář 		return;
29871474e2fSRadim Krčmář 
299e2ed4078SSuravee Suthikulpanit 	/*
300e2ed4078SSuravee Suthikulpanit 	 * AMD SVM AVIC accelerates EOI write and does not trap.
301e2ed4078SSuravee Suthikulpanit 	 * This cause in-kernel PIT re-inject mode to fail
302e2ed4078SSuravee Suthikulpanit 	 * since it checks ps->irq_ack before kvm_set_irq()
303e2ed4078SSuravee Suthikulpanit 	 * and relies on the ack notifier to timely queue
304e2ed4078SSuravee Suthikulpanit 	 * the pt->worker work iterm and reinject the missed tick.
305e2ed4078SSuravee Suthikulpanit 	 * So, deactivate APICv when PIT is in reinject mode.
306e2ed4078SSuravee Suthikulpanit 	 */
30771474e2fSRadim Krčmář 	if (reinject) {
308e2ed4078SSuravee Suthikulpanit 		kvm_request_apicv_update(kvm, false,
309e2ed4078SSuravee Suthikulpanit 					 APICV_INHIBIT_REASON_PIT_REINJ);
31071474e2fSRadim Krčmář 		/* The initial state is preserved while ps->reinject == 0. */
31171474e2fSRadim Krčmář 		kvm_pit_reset_reinject(pit);
31271474e2fSRadim Krčmář 		kvm_register_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
31371474e2fSRadim Krčmář 		kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
31471474e2fSRadim Krčmář 	} else {
315e2ed4078SSuravee Suthikulpanit 		kvm_request_apicv_update(kvm, true,
316e2ed4078SSuravee Suthikulpanit 					 APICV_INHIBIT_REASON_PIT_REINJ);
31771474e2fSRadim Krčmář 		kvm_unregister_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
31871474e2fSRadim Krčmář 		kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
31971474e2fSRadim Krčmář 	}
32071474e2fSRadim Krčmář 
321a0aace5aSRadim Krčmář 	atomic_set(&ps->reinject, reinject);
32271474e2fSRadim Krčmář }
32371474e2fSRadim Krčmář 
32409edea72SRadim Krčmář static void create_pit_timer(struct kvm_pit *pit, u32 val, int is_period)
3257837699fSSheng Yang {
32609edea72SRadim Krčmář 	struct kvm_kpit_state *ps = &pit->pit_state;
32709edea72SRadim Krčmář 	struct kvm *kvm = pit->kvm;
3287837699fSSheng Yang 	s64 interval;
3297837699fSSheng Yang 
33049df6397SSteve Rutherford 	if (!ioapic_in_kernel(kvm) ||
33149df6397SSteve Rutherford 	    ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)
3320924ab2cSJan Kiszka 		return;
3330924ab2cSJan Kiszka 
3340e4d4415SPaolo Bonzini 	interval = mul_u64_u32_div(val, NSEC_PER_SEC, KVM_PIT_FREQ);
3357837699fSSheng Yang 
336a78d9626SJoe Perches 	pr_debug("create pit timer, interval is %llu nsec\n", interval);
3377837699fSSheng Yang 
3387837699fSSheng Yang 	/* TODO The new value only affected after the retriggered */
33926ef1924SAvi Kivity 	hrtimer_cancel(&ps->timer);
3403989144fSPetr Mladek 	kthread_flush_work(&pit->expired);
34126ef1924SAvi Kivity 	ps->period = interval;
342d3c7b77dSMarcelo Tosatti 	ps->is_periodic = is_period;
343d3c7b77dSMarcelo Tosatti 
34409edea72SRadim Krčmář 	kvm_pit_reset_reinject(pit);
3457837699fSSheng Yang 
3469ed96e87SMarcelo Tosatti 	/*
3479ed96e87SMarcelo Tosatti 	 * Do not allow the guest to program periodic timers with small
3489ed96e87SMarcelo Tosatti 	 * interval, since the hrtimers are not throttled by the host
3499ed96e87SMarcelo Tosatti 	 * scheduler.
3509ed96e87SMarcelo Tosatti 	 */
3519ed96e87SMarcelo Tosatti 	if (ps->is_periodic) {
3529ed96e87SMarcelo Tosatti 		s64 min_period = min_timer_period_us * 1000LL;
3539ed96e87SMarcelo Tosatti 
3549ed96e87SMarcelo Tosatti 		if (ps->period < min_period) {
3559ed96e87SMarcelo Tosatti 			pr_info_ratelimited(
3569ed96e87SMarcelo Tosatti 			    "kvm: requested %lld ns "
3579ed96e87SMarcelo Tosatti 			    "i8254 timer period limited to %lld ns\n",
3589ed96e87SMarcelo Tosatti 			    ps->period, min_period);
3599ed96e87SMarcelo Tosatti 			ps->period = min_period;
3609ed96e87SMarcelo Tosatti 		}
3619ed96e87SMarcelo Tosatti 	}
3629ed96e87SMarcelo Tosatti 
36326ef1924SAvi Kivity 	hrtimer_start(&ps->timer, ktime_add_ns(ktime_get(), interval),
3647837699fSSheng Yang 		      HRTIMER_MODE_ABS);
3657837699fSSheng Yang }
3667837699fSSheng Yang 
36709edea72SRadim Krčmář static void pit_load_count(struct kvm_pit *pit, int channel, u32 val)
3687837699fSSheng Yang {
36909edea72SRadim Krčmář 	struct kvm_kpit_state *ps = &pit->pit_state;
3707837699fSSheng Yang 
371e6302698SMiaohe Lin 	pr_debug("load_count val is %u, channel is %d\n", val, channel);
3727837699fSSheng Yang 
3737837699fSSheng Yang 	/*
374ede2ccc5SMarcelo Tosatti 	 * The largest possible initial count is 0; this is equivalent
375ede2ccc5SMarcelo Tosatti 	 * to 216 for binary counting and 104 for BCD counting.
3767837699fSSheng Yang 	 */
3777837699fSSheng Yang 	if (val == 0)
3787837699fSSheng Yang 		val = 0x10000;
3797837699fSSheng Yang 
3807837699fSSheng Yang 	ps->channels[channel].count = val;
3817837699fSSheng Yang 
382fd668423SMarcelo Tosatti 	if (channel != 0) {
383fd668423SMarcelo Tosatti 		ps->channels[channel].count_load_time = ktime_get();
3847837699fSSheng Yang 		return;
385fd668423SMarcelo Tosatti 	}
3867837699fSSheng Yang 
3877837699fSSheng Yang 	/* Two types of timer
3887837699fSSheng Yang 	 * mode 1 is one shot, mode 2 is period, otherwise del timer */
3897837699fSSheng Yang 	switch (ps->channels[0].mode) {
390ede2ccc5SMarcelo Tosatti 	case 0:
3917837699fSSheng Yang 	case 1:
392ece15babSMarcelo Tosatti         /* FIXME: enhance mode 4 precision */
393ece15babSMarcelo Tosatti 	case 4:
39409edea72SRadim Krčmář 		create_pit_timer(pit, val, 0);
3957837699fSSheng Yang 		break;
3967837699fSSheng Yang 	case 2:
397f6975545SAurelien Jarno 	case 3:
39809edea72SRadim Krčmář 		create_pit_timer(pit, val, 1);
3997837699fSSheng Yang 		break;
4007837699fSSheng Yang 	default:
40109edea72SRadim Krčmář 		destroy_pit_timer(pit);
4027837699fSSheng Yang 	}
4037837699fSSheng Yang }
4047837699fSSheng Yang 
40509edea72SRadim Krčmář void kvm_pit_load_count(struct kvm_pit *pit, int channel, u32 val,
40609edea72SRadim Krčmář 		int hpet_legacy_start)
407e0f63cb9SSheng Yang {
408e9f42757SBeth Kon 	u8 saved_mode;
409b69d920fSRadim Krčmář 
41009edea72SRadim Krčmář 	WARN_ON_ONCE(!mutex_is_locked(&pit->pit_state.lock));
411b69d920fSRadim Krčmář 
412e9f42757SBeth Kon 	if (hpet_legacy_start) {
413e9f42757SBeth Kon 		/* save existing mode for later reenablement */
414e5e57e7aSPaolo Bonzini 		WARN_ON(channel != 0);
41509edea72SRadim Krčmář 		saved_mode = pit->pit_state.channels[0].mode;
41609edea72SRadim Krčmář 		pit->pit_state.channels[0].mode = 0xff; /* disable timer */
41709edea72SRadim Krčmář 		pit_load_count(pit, channel, val);
41809edea72SRadim Krčmář 		pit->pit_state.channels[0].mode = saved_mode;
419e9f42757SBeth Kon 	} else {
42009edea72SRadim Krčmář 		pit_load_count(pit, channel, val);
421e9f42757SBeth Kon 	}
422e0f63cb9SSheng Yang }
423e0f63cb9SSheng Yang 
424d76685c4SGregory Haskins static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
425d76685c4SGregory Haskins {
426d76685c4SGregory Haskins 	return container_of(dev, struct kvm_pit, dev);
427d76685c4SGregory Haskins }
428d76685c4SGregory Haskins 
429d76685c4SGregory Haskins static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
430d76685c4SGregory Haskins {
431d76685c4SGregory Haskins 	return container_of(dev, struct kvm_pit, speaker_dev);
432d76685c4SGregory Haskins }
433d76685c4SGregory Haskins 
434bda9020eSMichael S. Tsirkin static inline int pit_in_range(gpa_t addr)
435bda9020eSMichael S. Tsirkin {
436bda9020eSMichael S. Tsirkin 	return ((addr >= KVM_PIT_BASE_ADDRESS) &&
437bda9020eSMichael S. Tsirkin 		(addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
438bda9020eSMichael S. Tsirkin }
439bda9020eSMichael S. Tsirkin 
440e32edf4fSNikolay Nikolaev static int pit_ioport_write(struct kvm_vcpu *vcpu,
441e32edf4fSNikolay Nikolaev 				struct kvm_io_device *this,
4427837699fSSheng Yang 			    gpa_t addr, int len, const void *data)
4437837699fSSheng Yang {
444d76685c4SGregory Haskins 	struct kvm_pit *pit = dev_to_pit(this);
4457837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
4467837699fSSheng Yang 	int channel, access;
4477837699fSSheng Yang 	struct kvm_kpit_channel_state *s;
4487837699fSSheng Yang 	u32 val = *(u32 *) data;
449bda9020eSMichael S. Tsirkin 	if (!pit_in_range(addr))
450bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
4517837699fSSheng Yang 
4527837699fSSheng Yang 	val  &= 0xff;
4537837699fSSheng Yang 	addr &= KVM_PIT_CHANNEL_MASK;
4547837699fSSheng Yang 
4557837699fSSheng Yang 	mutex_lock(&pit_state->lock);
4567837699fSSheng Yang 
4577837699fSSheng Yang 	if (val != 0)
458a78d9626SJoe Perches 		pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
4597837699fSSheng Yang 			 (unsigned int)addr, len, val);
4607837699fSSheng Yang 
4617837699fSSheng Yang 	if (addr == 3) {
4627837699fSSheng Yang 		channel = val >> 6;
4637837699fSSheng Yang 		if (channel == 3) {
4647837699fSSheng Yang 			/* Read-Back Command. */
4657837699fSSheng Yang 			for (channel = 0; channel < 3; channel++) {
4667837699fSSheng Yang 				if (val & (2 << channel)) {
4677837699fSSheng Yang 					if (!(val & 0x20))
46809edea72SRadim Krčmář 						pit_latch_count(pit, channel);
4697837699fSSheng Yang 					if (!(val & 0x10))
47009edea72SRadim Krčmář 						pit_latch_status(pit, channel);
4717837699fSSheng Yang 				}
4727837699fSSheng Yang 			}
4737837699fSSheng Yang 		} else {
4747837699fSSheng Yang 			/* Select Counter <channel>. */
4757837699fSSheng Yang 			s = &pit_state->channels[channel];
4767837699fSSheng Yang 			access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
4777837699fSSheng Yang 			if (access == 0) {
47809edea72SRadim Krčmář 				pit_latch_count(pit, channel);
4797837699fSSheng Yang 			} else {
4807837699fSSheng Yang 				s->rw_mode = access;
4817837699fSSheng Yang 				s->read_state = access;
4827837699fSSheng Yang 				s->write_state = access;
4837837699fSSheng Yang 				s->mode = (val >> 1) & 7;
4847837699fSSheng Yang 				if (s->mode > 5)
4857837699fSSheng Yang 					s->mode -= 4;
4867837699fSSheng Yang 				s->bcd = val & 1;
4877837699fSSheng Yang 			}
4887837699fSSheng Yang 		}
4897837699fSSheng Yang 	} else {
4907837699fSSheng Yang 		/* Write Count. */
4917837699fSSheng Yang 		s = &pit_state->channels[addr];
4927837699fSSheng Yang 		switch (s->write_state) {
4937837699fSSheng Yang 		default:
4947837699fSSheng Yang 		case RW_STATE_LSB:
49509edea72SRadim Krčmář 			pit_load_count(pit, addr, val);
4967837699fSSheng Yang 			break;
4977837699fSSheng Yang 		case RW_STATE_MSB:
49809edea72SRadim Krčmář 			pit_load_count(pit, addr, val << 8);
4997837699fSSheng Yang 			break;
5007837699fSSheng Yang 		case RW_STATE_WORD0:
5017837699fSSheng Yang 			s->write_latch = val;
5027837699fSSheng Yang 			s->write_state = RW_STATE_WORD1;
5037837699fSSheng Yang 			break;
5047837699fSSheng Yang 		case RW_STATE_WORD1:
50509edea72SRadim Krčmář 			pit_load_count(pit, addr, s->write_latch | (val << 8));
5067837699fSSheng Yang 			s->write_state = RW_STATE_WORD0;
5077837699fSSheng Yang 			break;
5087837699fSSheng Yang 		}
5097837699fSSheng Yang 	}
5107837699fSSheng Yang 
5117837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
512bda9020eSMichael S. Tsirkin 	return 0;
5137837699fSSheng Yang }
5147837699fSSheng Yang 
515e32edf4fSNikolay Nikolaev static int pit_ioport_read(struct kvm_vcpu *vcpu,
516e32edf4fSNikolay Nikolaev 			   struct kvm_io_device *this,
5177837699fSSheng Yang 			   gpa_t addr, int len, void *data)
5187837699fSSheng Yang {
519d76685c4SGregory Haskins 	struct kvm_pit *pit = dev_to_pit(this);
5207837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
5217837699fSSheng Yang 	int ret, count;
5227837699fSSheng Yang 	struct kvm_kpit_channel_state *s;
523bda9020eSMichael S. Tsirkin 	if (!pit_in_range(addr))
524bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
5257837699fSSheng Yang 
5267837699fSSheng Yang 	addr &= KVM_PIT_CHANNEL_MASK;
527ee73f656SMarcelo Tosatti 	if (addr == 3)
528ee73f656SMarcelo Tosatti 		return 0;
529ee73f656SMarcelo Tosatti 
5307837699fSSheng Yang 	s = &pit_state->channels[addr];
5317837699fSSheng Yang 
5327837699fSSheng Yang 	mutex_lock(&pit_state->lock);
5337837699fSSheng Yang 
5347837699fSSheng Yang 	if (s->status_latched) {
5357837699fSSheng Yang 		s->status_latched = 0;
5367837699fSSheng Yang 		ret = s->status;
5377837699fSSheng Yang 	} else if (s->count_latched) {
5387837699fSSheng Yang 		switch (s->count_latched) {
5397837699fSSheng Yang 		default:
5407837699fSSheng Yang 		case RW_STATE_LSB:
5417837699fSSheng Yang 			ret = s->latched_count & 0xff;
5427837699fSSheng Yang 			s->count_latched = 0;
5437837699fSSheng Yang 			break;
5447837699fSSheng Yang 		case RW_STATE_MSB:
5457837699fSSheng Yang 			ret = s->latched_count >> 8;
5467837699fSSheng Yang 			s->count_latched = 0;
5477837699fSSheng Yang 			break;
5487837699fSSheng Yang 		case RW_STATE_WORD0:
5497837699fSSheng Yang 			ret = s->latched_count & 0xff;
5507837699fSSheng Yang 			s->count_latched = RW_STATE_MSB;
5517837699fSSheng Yang 			break;
5527837699fSSheng Yang 		}
5537837699fSSheng Yang 	} else {
5547837699fSSheng Yang 		switch (s->read_state) {
5557837699fSSheng Yang 		default:
5567837699fSSheng Yang 		case RW_STATE_LSB:
55709edea72SRadim Krčmář 			count = pit_get_count(pit, addr);
5587837699fSSheng Yang 			ret = count & 0xff;
5597837699fSSheng Yang 			break;
5607837699fSSheng Yang 		case RW_STATE_MSB:
56109edea72SRadim Krčmář 			count = pit_get_count(pit, addr);
5627837699fSSheng Yang 			ret = (count >> 8) & 0xff;
5637837699fSSheng Yang 			break;
5647837699fSSheng Yang 		case RW_STATE_WORD0:
56509edea72SRadim Krčmář 			count = pit_get_count(pit, addr);
5667837699fSSheng Yang 			ret = count & 0xff;
5677837699fSSheng Yang 			s->read_state = RW_STATE_WORD1;
5687837699fSSheng Yang 			break;
5697837699fSSheng Yang 		case RW_STATE_WORD1:
57009edea72SRadim Krčmář 			count = pit_get_count(pit, addr);
5717837699fSSheng Yang 			ret = (count >> 8) & 0xff;
5727837699fSSheng Yang 			s->read_state = RW_STATE_WORD0;
5737837699fSSheng Yang 			break;
5747837699fSSheng Yang 		}
5757837699fSSheng Yang 	}
5767837699fSSheng Yang 
5777837699fSSheng Yang 	if (len > sizeof(ret))
5787837699fSSheng Yang 		len = sizeof(ret);
5797837699fSSheng Yang 	memcpy(data, (char *)&ret, len);
5807837699fSSheng Yang 
5817837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
582bda9020eSMichael S. Tsirkin 	return 0;
5837837699fSSheng Yang }
5847837699fSSheng Yang 
585e32edf4fSNikolay Nikolaev static int speaker_ioport_write(struct kvm_vcpu *vcpu,
586e32edf4fSNikolay Nikolaev 				struct kvm_io_device *this,
5877837699fSSheng Yang 				gpa_t addr, int len, const void *data)
5887837699fSSheng Yang {
589d76685c4SGregory Haskins 	struct kvm_pit *pit = speaker_to_pit(this);
5907837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
5917837699fSSheng Yang 	u32 val = *(u32 *) data;
592bda9020eSMichael S. Tsirkin 	if (addr != KVM_SPEAKER_BASE_ADDRESS)
593bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
5947837699fSSheng Yang 
5957837699fSSheng Yang 	mutex_lock(&pit_state->lock);
5967837699fSSheng Yang 	pit_state->speaker_data_on = (val >> 1) & 1;
59709edea72SRadim Krčmář 	pit_set_gate(pit, 2, val & 1);
5987837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
599bda9020eSMichael S. Tsirkin 	return 0;
6007837699fSSheng Yang }
6017837699fSSheng Yang 
602e32edf4fSNikolay Nikolaev static int speaker_ioport_read(struct kvm_vcpu *vcpu,
603e32edf4fSNikolay Nikolaev 				   struct kvm_io_device *this,
6047837699fSSheng Yang 				   gpa_t addr, int len, void *data)
6057837699fSSheng Yang {
606d76685c4SGregory Haskins 	struct kvm_pit *pit = speaker_to_pit(this);
6077837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
6087837699fSSheng Yang 	unsigned int refresh_clock;
6097837699fSSheng Yang 	int ret;
610bda9020eSMichael S. Tsirkin 	if (addr != KVM_SPEAKER_BASE_ADDRESS)
611bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
6127837699fSSheng Yang 
6137837699fSSheng Yang 	/* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
6147837699fSSheng Yang 	refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
6157837699fSSheng Yang 
6167837699fSSheng Yang 	mutex_lock(&pit_state->lock);
61709edea72SRadim Krčmář 	ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(pit, 2) |
61809edea72SRadim Krčmář 		(pit_get_out(pit, 2) << 5) | (refresh_clock << 4));
6197837699fSSheng Yang 	if (len > sizeof(ret))
6207837699fSSheng Yang 		len = sizeof(ret);
6217837699fSSheng Yang 	memcpy(data, (char *)&ret, len);
6227837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
623bda9020eSMichael S. Tsirkin 	return 0;
6247837699fSSheng Yang }
6257837699fSSheng Yang 
626b39c90b6SRadim Krčmář static void kvm_pit_reset(struct kvm_pit *pit)
6277837699fSSheng Yang {
6287837699fSSheng Yang 	int i;
629308b0f23SSheng Yang 	struct kvm_kpit_channel_state *c;
630308b0f23SSheng Yang 
631e9f42757SBeth Kon 	pit->pit_state.flags = 0;
632308b0f23SSheng Yang 	for (i = 0; i < 3; i++) {
633308b0f23SSheng Yang 		c = &pit->pit_state.channels[i];
634308b0f23SSheng Yang 		c->mode = 0xff;
635308b0f23SSheng Yang 		c->gate = (i != 2);
63609edea72SRadim Krčmář 		pit_load_count(pit, i, 0);
637308b0f23SSheng Yang 	}
638308b0f23SSheng Yang 
639fd700a00SRadim Krčmář 	kvm_pit_reset_reinject(pit);
640308b0f23SSheng Yang }
641308b0f23SSheng Yang 
6424780c659SAvi Kivity static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
6434780c659SAvi Kivity {
6444780c659SAvi Kivity 	struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
6454780c659SAvi Kivity 
646fd700a00SRadim Krčmář 	if (!mask)
647fd700a00SRadim Krčmář 		kvm_pit_reset_reinject(pit);
6484780c659SAvi Kivity }
6494780c659SAvi Kivity 
650d76685c4SGregory Haskins static const struct kvm_io_device_ops pit_dev_ops = {
651d76685c4SGregory Haskins 	.read     = pit_ioport_read,
652d76685c4SGregory Haskins 	.write    = pit_ioport_write,
653d76685c4SGregory Haskins };
654d76685c4SGregory Haskins 
655d76685c4SGregory Haskins static const struct kvm_io_device_ops speaker_dev_ops = {
656d76685c4SGregory Haskins 	.read     = speaker_ioport_read,
657d76685c4SGregory Haskins 	.write    = speaker_ioport_write,
658d76685c4SGregory Haskins };
659d76685c4SGregory Haskins 
660c5ff41ceSJan Kiszka struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
661308b0f23SSheng Yang {
6627837699fSSheng Yang 	struct kvm_pit *pit;
6637837699fSSheng Yang 	struct kvm_kpit_state *pit_state;
664b6ddf05fSJan Kiszka 	struct pid *pid;
665b6ddf05fSJan Kiszka 	pid_t pid_nr;
666090b7affSGregory Haskins 	int ret;
6677837699fSSheng Yang 
668254272ceSBen Gardon 	pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL_ACCOUNT);
6697837699fSSheng Yang 	if (!pit)
6707837699fSSheng Yang 		return NULL;
6717837699fSSheng Yang 
6725550af4dSSheng Yang 	pit->irq_source_id = kvm_request_irq_source_id(kvm);
67310d24821SRadim Krčmář 	if (pit->irq_source_id < 0)
67410d24821SRadim Krčmář 		goto fail_request;
6755550af4dSSheng Yang 
6767837699fSSheng Yang 	mutex_init(&pit->pit_state.lock);
67733572ac0SChris Lalancette 
678b6ddf05fSJan Kiszka 	pid = get_pid(task_tgid(current));
679b6ddf05fSJan Kiszka 	pid_nr = pid_vnr(pid);
680b6ddf05fSJan Kiszka 	put_pid(pid);
681b6ddf05fSJan Kiszka 
68236da91bdSPetr Mladek 	pit->worker = kthread_create_worker(0, "kvm-pit/%d", pid_nr);
68336da91bdSPetr Mladek 	if (IS_ERR(pit->worker))
68410d24821SRadim Krčmář 		goto fail_kthread;
68510d24821SRadim Krčmář 
6863989144fSPetr Mladek 	kthread_init_work(&pit->expired, pit_do_work);
6877837699fSSheng Yang 
6887837699fSSheng Yang 	pit->kvm = kvm;
6897837699fSSheng Yang 
6907837699fSSheng Yang 	pit_state = &pit->pit_state;
69126ef1924SAvi Kivity 	hrtimer_init(&pit_state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
692ab4c1476SRadim Krčmář 	pit_state->timer.function = pit_timer_fn;
69371474e2fSRadim Krčmář 
6943cf57fedSMarcelo Tosatti 	pit_state->irq_ack_notifier.gsi = 0;
6953cf57fedSMarcelo Tosatti 	pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
69671474e2fSRadim Krčmář 	pit->mask_notifier.func = pit_mask_notifer;
6977837699fSSheng Yang 
698308b0f23SSheng Yang 	kvm_pit_reset(pit);
6997837699fSSheng Yang 
70071474e2fSRadim Krčmář 	kvm_pit_set_reinject(pit, true);
7014780c659SAvi Kivity 
702250715a6SPaolo Bonzini 	mutex_lock(&kvm->slots_lock);
7036b66ac1aSGregory Haskins 	kvm_iodevice_init(&pit->dev, &pit_dev_ops);
704743eeb0bSSasha Levin 	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS,
705743eeb0bSSasha Levin 				      KVM_PIT_MEM_LENGTH, &pit->dev);
706090b7affSGregory Haskins 	if (ret < 0)
70710d24821SRadim Krčmář 		goto fail_register_pit;
7086b66ac1aSGregory Haskins 
7096b66ac1aSGregory Haskins 	if (flags & KVM_PIT_SPEAKER_DUMMY) {
7106b66ac1aSGregory Haskins 		kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
711e93f8a0fSMarcelo Tosatti 		ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
712743eeb0bSSasha Levin 					      KVM_SPEAKER_BASE_ADDRESS, 4,
713090b7affSGregory Haskins 					      &pit->speaker_dev);
714090b7affSGregory Haskins 		if (ret < 0)
71510d24821SRadim Krčmář 			goto fail_register_speaker;
7166b66ac1aSGregory Haskins 	}
717250715a6SPaolo Bonzini 	mutex_unlock(&kvm->slots_lock);
7186b66ac1aSGregory Haskins 
7197837699fSSheng Yang 	return pit;
720090b7affSGregory Haskins 
72110d24821SRadim Krčmář fail_register_speaker:
722e93f8a0fSMarcelo Tosatti 	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
72310d24821SRadim Krčmář fail_register_pit:
724250715a6SPaolo Bonzini 	mutex_unlock(&kvm->slots_lock);
72571474e2fSRadim Krčmář 	kvm_pit_set_reinject(pit, false);
72636da91bdSPetr Mladek 	kthread_destroy_worker(pit->worker);
72710d24821SRadim Krčmář fail_kthread:
72810d24821SRadim Krčmář 	kvm_free_irq_source_id(kvm, pit->irq_source_id);
72910d24821SRadim Krčmář fail_request:
730090b7affSGregory Haskins 	kfree(pit);
731090b7affSGregory Haskins 	return NULL;
7327837699fSSheng Yang }
7337837699fSSheng Yang 
7347837699fSSheng Yang void kvm_free_pit(struct kvm *kvm)
7357837699fSSheng Yang {
73608e5ccf3SRadim Krčmář 	struct kvm_pit *pit = kvm->arch.vpit;
7377837699fSSheng Yang 
73808e5ccf3SRadim Krčmář 	if (pit) {
739fb530729SRadim Krčmář 		mutex_lock(&kvm->slots_lock);
74008e5ccf3SRadim Krčmář 		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
74108e5ccf3SRadim Krčmář 		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->speaker_dev);
742fb530729SRadim Krčmář 		mutex_unlock(&kvm->slots_lock);
74308e5ccf3SRadim Krčmář 		kvm_pit_set_reinject(pit, false);
74408e5ccf3SRadim Krčmář 		hrtimer_cancel(&pit->pit_state.timer);
74536da91bdSPetr Mladek 		kthread_destroy_worker(pit->worker);
74608e5ccf3SRadim Krčmář 		kvm_free_irq_source_id(kvm, pit->irq_source_id);
74708e5ccf3SRadim Krčmář 		kfree(pit);
7487837699fSSheng Yang 	}
7497837699fSSheng Yang }
750