xref: /openbmc/linux/arch/x86/kvm/i8254.c (revision 9611c187774f0e20c258c23ced2599c44bd2fef4)
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
8*9611c187SNicolas 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>
3733572ac0SChris Lalancette #include <linux/workqueue.h>
387837699fSSheng Yang 
397837699fSSheng Yang #include "irq.h"
407837699fSSheng Yang #include "i8254.h"
417837699fSSheng Yang 
427837699fSSheng Yang #ifndef CONFIG_X86_64
436f6d6a1aSRoman Zippel #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
447837699fSSheng Yang #else
457837699fSSheng Yang #define mod_64(x, y) ((x) % (y))
467837699fSSheng Yang #endif
477837699fSSheng Yang 
487837699fSSheng Yang #define RW_STATE_LSB 1
497837699fSSheng Yang #define RW_STATE_MSB 2
507837699fSSheng Yang #define RW_STATE_WORD0 3
517837699fSSheng Yang #define RW_STATE_WORD1 4
527837699fSSheng Yang 
537837699fSSheng Yang /* Compute with 96 bit intermediate result: (a*b)/c */
547837699fSSheng Yang static u64 muldiv64(u64 a, u32 b, u32 c)
557837699fSSheng Yang {
567837699fSSheng Yang 	union {
577837699fSSheng Yang 		u64 ll;
587837699fSSheng Yang 		struct {
597837699fSSheng Yang 			u32 low, high;
607837699fSSheng Yang 		} l;
617837699fSSheng Yang 	} u, res;
627837699fSSheng Yang 	u64 rl, rh;
637837699fSSheng Yang 
647837699fSSheng Yang 	u.ll = a;
657837699fSSheng Yang 	rl = (u64)u.l.low * (u64)b;
667837699fSSheng Yang 	rh = (u64)u.l.high * (u64)b;
677837699fSSheng Yang 	rh += (rl >> 32);
686f6d6a1aSRoman Zippel 	res.l.high = div64_u64(rh, c);
696f6d6a1aSRoman Zippel 	res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
707837699fSSheng Yang 	return res.ll;
717837699fSSheng Yang }
727837699fSSheng Yang 
737837699fSSheng Yang static void pit_set_gate(struct kvm *kvm, int channel, u32 val)
747837699fSSheng Yang {
757837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
767837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
777837699fSSheng Yang 
787837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
797837699fSSheng Yang 
807837699fSSheng Yang 	switch (c->mode) {
817837699fSSheng Yang 	default:
827837699fSSheng Yang 	case 0:
837837699fSSheng Yang 	case 4:
847837699fSSheng Yang 		/* XXX: just disable/enable counting */
857837699fSSheng Yang 		break;
867837699fSSheng Yang 	case 1:
877837699fSSheng Yang 	case 2:
887837699fSSheng Yang 	case 3:
897837699fSSheng Yang 	case 5:
907837699fSSheng Yang 		/* Restart counting on rising edge. */
917837699fSSheng Yang 		if (c->gate < val)
927837699fSSheng Yang 			c->count_load_time = ktime_get();
937837699fSSheng Yang 		break;
947837699fSSheng Yang 	}
957837699fSSheng Yang 
967837699fSSheng Yang 	c->gate = val;
977837699fSSheng Yang }
987837699fSSheng Yang 
998b2cf73cSHarvey Harrison static int pit_get_gate(struct kvm *kvm, int channel)
1007837699fSSheng Yang {
1017837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
1027837699fSSheng Yang 
1037837699fSSheng Yang 	return kvm->arch.vpit->pit_state.channels[channel].gate;
1047837699fSSheng Yang }
1057837699fSSheng Yang 
106fd668423SMarcelo Tosatti static s64 __kpit_elapsed(struct kvm *kvm)
107fd668423SMarcelo Tosatti {
108fd668423SMarcelo Tosatti 	s64 elapsed;
109fd668423SMarcelo Tosatti 	ktime_t remaining;
110fd668423SMarcelo Tosatti 	struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
111fd668423SMarcelo Tosatti 
1120ff77873SMarcelo Tosatti 	if (!ps->pit_timer.period)
1130ff77873SMarcelo Tosatti 		return 0;
1140ff77873SMarcelo Tosatti 
115ede2ccc5SMarcelo Tosatti 	/*
116ede2ccc5SMarcelo Tosatti 	 * The Counter does not stop when it reaches zero. In
117ede2ccc5SMarcelo Tosatti 	 * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
118ede2ccc5SMarcelo Tosatti 	 * the highest count, either FFFF hex for binary counting
119ede2ccc5SMarcelo Tosatti 	 * or 9999 for BCD counting, and continues counting.
120ede2ccc5SMarcelo Tosatti 	 * Modes 2 and 3 are periodic; the Counter reloads
121ede2ccc5SMarcelo Tosatti 	 * itself with the initial count and continues counting
122ede2ccc5SMarcelo Tosatti 	 * from there.
123ede2ccc5SMarcelo Tosatti 	 */
124ace15464SMarcelo Tosatti 	remaining = hrtimer_get_remaining(&ps->pit_timer.timer);
125fd668423SMarcelo Tosatti 	elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
126ede2ccc5SMarcelo Tosatti 	elapsed = mod_64(elapsed, ps->pit_timer.period);
127fd668423SMarcelo Tosatti 
128fd668423SMarcelo Tosatti 	return elapsed;
129fd668423SMarcelo Tosatti }
130fd668423SMarcelo Tosatti 
131fd668423SMarcelo Tosatti static s64 kpit_elapsed(struct kvm *kvm, struct kvm_kpit_channel_state *c,
132fd668423SMarcelo Tosatti 			int channel)
133fd668423SMarcelo Tosatti {
134fd668423SMarcelo Tosatti 	if (channel == 0)
135fd668423SMarcelo Tosatti 		return __kpit_elapsed(kvm);
136fd668423SMarcelo Tosatti 
137fd668423SMarcelo Tosatti 	return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
138fd668423SMarcelo Tosatti }
139fd668423SMarcelo Tosatti 
1407837699fSSheng Yang static int pit_get_count(struct kvm *kvm, int channel)
1417837699fSSheng Yang {
1427837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
1437837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
1447837699fSSheng Yang 	s64 d, t;
1457837699fSSheng Yang 	int counter;
1467837699fSSheng Yang 
1477837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
1487837699fSSheng Yang 
149fd668423SMarcelo Tosatti 	t = kpit_elapsed(kvm, c, channel);
1507837699fSSheng Yang 	d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
1517837699fSSheng Yang 
1527837699fSSheng Yang 	switch (c->mode) {
1537837699fSSheng Yang 	case 0:
1547837699fSSheng Yang 	case 1:
1557837699fSSheng Yang 	case 4:
1567837699fSSheng Yang 	case 5:
1577837699fSSheng Yang 		counter = (c->count - d) & 0xffff;
1587837699fSSheng Yang 		break;
1597837699fSSheng Yang 	case 3:
1607837699fSSheng Yang 		/* XXX: may be incorrect for odd counts */
1617837699fSSheng Yang 		counter = c->count - (mod_64((2 * d), c->count));
1627837699fSSheng Yang 		break;
1637837699fSSheng Yang 	default:
1647837699fSSheng Yang 		counter = c->count - mod_64(d, c->count);
1657837699fSSheng Yang 		break;
1667837699fSSheng Yang 	}
1677837699fSSheng Yang 	return counter;
1687837699fSSheng Yang }
1697837699fSSheng Yang 
1707837699fSSheng Yang static int pit_get_out(struct kvm *kvm, int channel)
1717837699fSSheng Yang {
1727837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
1737837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
1747837699fSSheng Yang 	s64 d, t;
1757837699fSSheng Yang 	int out;
1767837699fSSheng Yang 
1777837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
1787837699fSSheng Yang 
179fd668423SMarcelo Tosatti 	t = kpit_elapsed(kvm, c, channel);
1807837699fSSheng Yang 	d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
1817837699fSSheng Yang 
1827837699fSSheng Yang 	switch (c->mode) {
1837837699fSSheng Yang 	default:
1847837699fSSheng Yang 	case 0:
1857837699fSSheng Yang 		out = (d >= c->count);
1867837699fSSheng Yang 		break;
1877837699fSSheng Yang 	case 1:
1887837699fSSheng Yang 		out = (d < c->count);
1897837699fSSheng Yang 		break;
1907837699fSSheng Yang 	case 2:
1917837699fSSheng Yang 		out = ((mod_64(d, c->count) == 0) && (d != 0));
1927837699fSSheng Yang 		break;
1937837699fSSheng Yang 	case 3:
1947837699fSSheng Yang 		out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
1957837699fSSheng Yang 		break;
1967837699fSSheng Yang 	case 4:
1977837699fSSheng Yang 	case 5:
1987837699fSSheng Yang 		out = (d == c->count);
1997837699fSSheng Yang 		break;
2007837699fSSheng Yang 	}
2017837699fSSheng Yang 
2027837699fSSheng Yang 	return out;
2037837699fSSheng Yang }
2047837699fSSheng Yang 
2057837699fSSheng Yang static void pit_latch_count(struct kvm *kvm, int channel)
2067837699fSSheng Yang {
2077837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
2087837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
2097837699fSSheng Yang 
2107837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
2117837699fSSheng Yang 
2127837699fSSheng Yang 	if (!c->count_latched) {
2137837699fSSheng Yang 		c->latched_count = pit_get_count(kvm, channel);
2147837699fSSheng Yang 		c->count_latched = c->rw_mode;
2157837699fSSheng Yang 	}
2167837699fSSheng Yang }
2177837699fSSheng Yang 
2187837699fSSheng Yang static void pit_latch_status(struct kvm *kvm, int channel)
2197837699fSSheng Yang {
2207837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
2217837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
2227837699fSSheng Yang 
2237837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
2247837699fSSheng Yang 
2257837699fSSheng Yang 	if (!c->status_latched) {
2267837699fSSheng Yang 		/* TODO: Return NULL COUNT (bit 6). */
2277837699fSSheng Yang 		c->status = ((pit_get_out(kvm, channel) << 7) |
2287837699fSSheng Yang 				(c->rw_mode << 4) |
2297837699fSSheng Yang 				(c->mode << 1) |
2307837699fSSheng Yang 				c->bcd);
2317837699fSSheng Yang 		c->status_latched = 1;
2327837699fSSheng Yang 	}
2337837699fSSheng Yang }
2347837699fSSheng Yang 
235ee032c99SHarvey Harrison static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
2363cf57fedSMarcelo Tosatti {
2373cf57fedSMarcelo Tosatti 	struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
2383cf57fedSMarcelo Tosatti 						 irq_ack_notifier);
23933572ac0SChris Lalancette 	int value;
24033572ac0SChris Lalancette 
24133572ac0SChris Lalancette 	spin_lock(&ps->inject_lock);
24233572ac0SChris Lalancette 	value = atomic_dec_return(&ps->pit_timer.pending);
24333572ac0SChris Lalancette 	if (value < 0)
24433572ac0SChris Lalancette 		/* spurious acks can be generated if, for example, the
24533572ac0SChris Lalancette 		 * PIC is being reset.  Handle it gracefully here
24633572ac0SChris Lalancette 		 */
247dc7404ceSAvi Kivity 		atomic_inc(&ps->pit_timer.pending);
24833572ac0SChris Lalancette 	else if (value > 0)
24933572ac0SChris Lalancette 		/* in this case, we had multiple outstanding pit interrupts
25033572ac0SChris Lalancette 		 * that we needed to inject.  Reinject
25133572ac0SChris Lalancette 		 */
25233572ac0SChris Lalancette 		queue_work(ps->pit->wq, &ps->pit->expired);
2533cf57fedSMarcelo Tosatti 	ps->irq_ack = 1;
25433572ac0SChris Lalancette 	spin_unlock(&ps->inject_lock);
2553cf57fedSMarcelo Tosatti }
2563cf57fedSMarcelo Tosatti 
2572f599714SMarcelo Tosatti void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
2582f599714SMarcelo Tosatti {
2592f599714SMarcelo Tosatti 	struct kvm_pit *pit = vcpu->kvm->arch.vpit;
2602f599714SMarcelo Tosatti 	struct hrtimer *timer;
2612f599714SMarcelo Tosatti 
262c5af89b6SGleb Natapov 	if (!kvm_vcpu_is_bsp(vcpu) || !pit)
2632f599714SMarcelo Tosatti 		return;
2642f599714SMarcelo Tosatti 
2652f599714SMarcelo Tosatti 	timer = &pit->pit_state.pit_timer.timer;
2662f599714SMarcelo Tosatti 	if (hrtimer_cancel(timer))
267beb20d52SArjan van de Ven 		hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
2682f599714SMarcelo Tosatti }
2692f599714SMarcelo Tosatti 
27033572ac0SChris Lalancette static void destroy_pit_timer(struct kvm_pit *pit)
2717837699fSSheng Yang {
27233572ac0SChris Lalancette 	hrtimer_cancel(&pit->pit_state.pit_timer.timer);
27333572ac0SChris Lalancette 	cancel_work_sync(&pit->expired);
2747837699fSSheng Yang }
2757837699fSSheng Yang 
276d3c7b77dSMarcelo Tosatti static bool kpit_is_periodic(struct kvm_timer *ktimer)
277d3c7b77dSMarcelo Tosatti {
278d3c7b77dSMarcelo Tosatti 	struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state,
279d3c7b77dSMarcelo Tosatti 						 pit_timer);
280d3c7b77dSMarcelo Tosatti 	return ps->is_periodic;
281d3c7b77dSMarcelo Tosatti }
282d3c7b77dSMarcelo Tosatti 
283386eb6e8SHannes Eder static struct kvm_timer_ops kpit_ops = {
284d3c7b77dSMarcelo Tosatti 	.is_periodic = kpit_is_periodic,
285d3c7b77dSMarcelo Tosatti };
286d3c7b77dSMarcelo Tosatti 
28733572ac0SChris Lalancette static void pit_do_work(struct work_struct *work)
28833572ac0SChris Lalancette {
28933572ac0SChris Lalancette 	struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
29033572ac0SChris Lalancette 	struct kvm *kvm = pit->kvm;
29133572ac0SChris Lalancette 	struct kvm_vcpu *vcpu;
29233572ac0SChris Lalancette 	int i;
29333572ac0SChris Lalancette 	struct kvm_kpit_state *ps = &pit->pit_state;
29433572ac0SChris Lalancette 	int inject = 0;
29533572ac0SChris Lalancette 
29633572ac0SChris Lalancette 	/* Try to inject pending interrupts when
29733572ac0SChris Lalancette 	 * last one has been acked.
29833572ac0SChris Lalancette 	 */
29933572ac0SChris Lalancette 	spin_lock(&ps->inject_lock);
30033572ac0SChris Lalancette 	if (ps->irq_ack) {
30133572ac0SChris Lalancette 		ps->irq_ack = 0;
30233572ac0SChris Lalancette 		inject = 1;
30333572ac0SChris Lalancette 	}
30433572ac0SChris Lalancette 	spin_unlock(&ps->inject_lock);
30533572ac0SChris Lalancette 	if (inject) {
30633572ac0SChris Lalancette 		kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
30733572ac0SChris Lalancette 		kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
30833572ac0SChris Lalancette 
30933572ac0SChris Lalancette 		/*
31033572ac0SChris Lalancette 		 * Provides NMI watchdog support via Virtual Wire mode.
31133572ac0SChris Lalancette 		 * The route is: PIT -> PIC -> LVT0 in NMI mode.
31233572ac0SChris Lalancette 		 *
31333572ac0SChris Lalancette 		 * Note: Our Virtual Wire implementation is simplified, only
31433572ac0SChris Lalancette 		 * propagating PIT interrupts to all VCPUs when they have set
31533572ac0SChris Lalancette 		 * LVT0 to NMI delivery. Other PIC interrupts are just sent to
31633572ac0SChris Lalancette 		 * VCPU0, and only if its LVT0 is in EXTINT mode.
31733572ac0SChris Lalancette 		 */
31833572ac0SChris Lalancette 		if (kvm->arch.vapics_in_nmi_mode > 0)
31933572ac0SChris Lalancette 			kvm_for_each_vcpu(i, vcpu, kvm)
32033572ac0SChris Lalancette 				kvm_apic_nmi_wd_deliver(vcpu);
32133572ac0SChris Lalancette 	}
32233572ac0SChris Lalancette }
32333572ac0SChris Lalancette 
32433572ac0SChris Lalancette static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
32533572ac0SChris Lalancette {
32633572ac0SChris Lalancette 	struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
32733572ac0SChris Lalancette 	struct kvm_pit *pt = ktimer->kvm->arch.vpit;
32833572ac0SChris Lalancette 
32933572ac0SChris Lalancette 	if (ktimer->reinject || !atomic_read(&ktimer->pending)) {
33033572ac0SChris Lalancette 		atomic_inc(&ktimer->pending);
33133572ac0SChris Lalancette 		queue_work(pt->wq, &pt->expired);
33233572ac0SChris Lalancette 	}
33333572ac0SChris Lalancette 
33433572ac0SChris Lalancette 	if (ktimer->t_ops->is_periodic(ktimer)) {
33533572ac0SChris Lalancette 		hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
33633572ac0SChris Lalancette 		return HRTIMER_RESTART;
33733572ac0SChris Lalancette 	} else
33833572ac0SChris Lalancette 		return HRTIMER_NORESTART;
33933572ac0SChris Lalancette }
34033572ac0SChris Lalancette 
3413cf57fedSMarcelo Tosatti static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
3427837699fSSheng Yang {
343d3c7b77dSMarcelo Tosatti 	struct kvm_timer *pt = &ps->pit_timer;
3447837699fSSheng Yang 	s64 interval;
3457837699fSSheng Yang 
3467837699fSSheng Yang 	interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
3477837699fSSheng Yang 
348a78d9626SJoe Perches 	pr_debug("create pit timer, interval is %llu nsec\n", interval);
3497837699fSSheng Yang 
3507837699fSSheng Yang 	/* TODO The new value only affected after the retriggered */
3517837699fSSheng Yang 	hrtimer_cancel(&pt->timer);
35233572ac0SChris Lalancette 	cancel_work_sync(&ps->pit->expired);
353ede2ccc5SMarcelo Tosatti 	pt->period = interval;
354d3c7b77dSMarcelo Tosatti 	ps->is_periodic = is_period;
355d3c7b77dSMarcelo Tosatti 
35633572ac0SChris Lalancette 	pt->timer.function = pit_timer_fn;
357d3c7b77dSMarcelo Tosatti 	pt->t_ops = &kpit_ops;
358d3c7b77dSMarcelo Tosatti 	pt->kvm = ps->pit->kvm;
359d3c7b77dSMarcelo Tosatti 
3607837699fSSheng Yang 	atomic_set(&pt->pending, 0);
3613cf57fedSMarcelo Tosatti 	ps->irq_ack = 1;
3627837699fSSheng Yang 
3637837699fSSheng Yang 	hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
3647837699fSSheng Yang 		      HRTIMER_MODE_ABS);
3657837699fSSheng Yang }
3667837699fSSheng Yang 
3677837699fSSheng Yang static void pit_load_count(struct kvm *kvm, int channel, u32 val)
3687837699fSSheng Yang {
3697837699fSSheng Yang 	struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
3707837699fSSheng Yang 
3717837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&ps->lock));
3727837699fSSheng Yang 
373a78d9626SJoe Perches 	pr_debug("load_count val is %d, channel is %d\n", val, channel);
3747837699fSSheng Yang 
3757837699fSSheng Yang 	/*
376ede2ccc5SMarcelo Tosatti 	 * The largest possible initial count is 0; this is equivalent
377ede2ccc5SMarcelo Tosatti 	 * to 216 for binary counting and 104 for BCD counting.
3787837699fSSheng Yang 	 */
3797837699fSSheng Yang 	if (val == 0)
3807837699fSSheng Yang 		val = 0x10000;
3817837699fSSheng Yang 
3827837699fSSheng Yang 	ps->channels[channel].count = val;
3837837699fSSheng Yang 
384fd668423SMarcelo Tosatti 	if (channel != 0) {
385fd668423SMarcelo Tosatti 		ps->channels[channel].count_load_time = ktime_get();
3867837699fSSheng Yang 		return;
387fd668423SMarcelo Tosatti 	}
3887837699fSSheng Yang 
3897837699fSSheng Yang 	/* Two types of timer
3907837699fSSheng Yang 	 * mode 1 is one shot, mode 2 is period, otherwise del timer */
3917837699fSSheng Yang 	switch (ps->channels[0].mode) {
392ede2ccc5SMarcelo Tosatti 	case 0:
3937837699fSSheng Yang 	case 1:
394ece15babSMarcelo Tosatti         /* FIXME: enhance mode 4 precision */
395ece15babSMarcelo Tosatti 	case 4:
396e9f42757SBeth Kon 		if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)) {
3973cf57fedSMarcelo Tosatti 			create_pit_timer(ps, val, 0);
398e9f42757SBeth Kon 		}
3997837699fSSheng Yang 		break;
4007837699fSSheng Yang 	case 2:
401f6975545SAurelien Jarno 	case 3:
402e9f42757SBeth Kon 		if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)){
4033cf57fedSMarcelo Tosatti 			create_pit_timer(ps, val, 1);
404e9f42757SBeth Kon 		}
4057837699fSSheng Yang 		break;
4067837699fSSheng Yang 	default:
40733572ac0SChris Lalancette 		destroy_pit_timer(kvm->arch.vpit);
4087837699fSSheng Yang 	}
4097837699fSSheng Yang }
4107837699fSSheng Yang 
411e9f42757SBeth Kon void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start)
412e0f63cb9SSheng Yang {
413e9f42757SBeth Kon 	u8 saved_mode;
414e9f42757SBeth Kon 	if (hpet_legacy_start) {
415e9f42757SBeth Kon 		/* save existing mode for later reenablement */
416e9f42757SBeth Kon 		saved_mode = kvm->arch.vpit->pit_state.channels[0].mode;
417e9f42757SBeth Kon 		kvm->arch.vpit->pit_state.channels[0].mode = 0xff; /* disable timer */
418e0f63cb9SSheng Yang 		pit_load_count(kvm, channel, val);
419e9f42757SBeth Kon 		kvm->arch.vpit->pit_state.channels[0].mode = saved_mode;
420e9f42757SBeth Kon 	} else {
421e9f42757SBeth Kon 		pit_load_count(kvm, channel, val);
422e9f42757SBeth Kon 	}
423e0f63cb9SSheng Yang }
424e0f63cb9SSheng Yang 
425d76685c4SGregory Haskins static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
426d76685c4SGregory Haskins {
427d76685c4SGregory Haskins 	return container_of(dev, struct kvm_pit, dev);
428d76685c4SGregory Haskins }
429d76685c4SGregory Haskins 
430d76685c4SGregory Haskins static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
431d76685c4SGregory Haskins {
432d76685c4SGregory Haskins 	return container_of(dev, struct kvm_pit, speaker_dev);
433d76685c4SGregory Haskins }
434d76685c4SGregory Haskins 
435bda9020eSMichael S. Tsirkin static inline int pit_in_range(gpa_t addr)
436bda9020eSMichael S. Tsirkin {
437bda9020eSMichael S. Tsirkin 	return ((addr >= KVM_PIT_BASE_ADDRESS) &&
438bda9020eSMichael S. Tsirkin 		(addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
439bda9020eSMichael S. Tsirkin }
440bda9020eSMichael S. Tsirkin 
441bda9020eSMichael S. Tsirkin static int pit_ioport_write(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 	struct kvm *kvm = pit->kvm;
4477837699fSSheng Yang 	int channel, access;
4487837699fSSheng Yang 	struct kvm_kpit_channel_state *s;
4497837699fSSheng Yang 	u32 val = *(u32 *) data;
450bda9020eSMichael S. Tsirkin 	if (!pit_in_range(addr))
451bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
4527837699fSSheng Yang 
4537837699fSSheng Yang 	val  &= 0xff;
4547837699fSSheng Yang 	addr &= KVM_PIT_CHANNEL_MASK;
4557837699fSSheng Yang 
4567837699fSSheng Yang 	mutex_lock(&pit_state->lock);
4577837699fSSheng Yang 
4587837699fSSheng Yang 	if (val != 0)
459a78d9626SJoe Perches 		pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
4607837699fSSheng Yang 			 (unsigned int)addr, len, val);
4617837699fSSheng Yang 
4627837699fSSheng Yang 	if (addr == 3) {
4637837699fSSheng Yang 		channel = val >> 6;
4647837699fSSheng Yang 		if (channel == 3) {
4657837699fSSheng Yang 			/* Read-Back Command. */
4667837699fSSheng Yang 			for (channel = 0; channel < 3; channel++) {
4677837699fSSheng Yang 				s = &pit_state->channels[channel];
4687837699fSSheng Yang 				if (val & (2 << channel)) {
4697837699fSSheng Yang 					if (!(val & 0x20))
4707837699fSSheng Yang 						pit_latch_count(kvm, channel);
4717837699fSSheng Yang 					if (!(val & 0x10))
4727837699fSSheng Yang 						pit_latch_status(kvm, channel);
4737837699fSSheng Yang 				}
4747837699fSSheng Yang 			}
4757837699fSSheng Yang 		} else {
4767837699fSSheng Yang 			/* Select Counter <channel>. */
4777837699fSSheng Yang 			s = &pit_state->channels[channel];
4787837699fSSheng Yang 			access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
4797837699fSSheng Yang 			if (access == 0) {
4807837699fSSheng Yang 				pit_latch_count(kvm, channel);
4817837699fSSheng Yang 			} else {
4827837699fSSheng Yang 				s->rw_mode = access;
4837837699fSSheng Yang 				s->read_state = access;
4847837699fSSheng Yang 				s->write_state = access;
4857837699fSSheng Yang 				s->mode = (val >> 1) & 7;
4867837699fSSheng Yang 				if (s->mode > 5)
4877837699fSSheng Yang 					s->mode -= 4;
4887837699fSSheng Yang 				s->bcd = val & 1;
4897837699fSSheng Yang 			}
4907837699fSSheng Yang 		}
4917837699fSSheng Yang 	} else {
4927837699fSSheng Yang 		/* Write Count. */
4937837699fSSheng Yang 		s = &pit_state->channels[addr];
4947837699fSSheng Yang 		switch (s->write_state) {
4957837699fSSheng Yang 		default:
4967837699fSSheng Yang 		case RW_STATE_LSB:
4977837699fSSheng Yang 			pit_load_count(kvm, addr, val);
4987837699fSSheng Yang 			break;
4997837699fSSheng Yang 		case RW_STATE_MSB:
5007837699fSSheng Yang 			pit_load_count(kvm, addr, val << 8);
5017837699fSSheng Yang 			break;
5027837699fSSheng Yang 		case RW_STATE_WORD0:
5037837699fSSheng Yang 			s->write_latch = val;
5047837699fSSheng Yang 			s->write_state = RW_STATE_WORD1;
5057837699fSSheng Yang 			break;
5067837699fSSheng Yang 		case RW_STATE_WORD1:
5077837699fSSheng Yang 			pit_load_count(kvm, addr, s->write_latch | (val << 8));
5087837699fSSheng Yang 			s->write_state = RW_STATE_WORD0;
5097837699fSSheng Yang 			break;
5107837699fSSheng Yang 		}
5117837699fSSheng Yang 	}
5127837699fSSheng Yang 
5137837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
514bda9020eSMichael S. Tsirkin 	return 0;
5157837699fSSheng Yang }
5167837699fSSheng Yang 
517bda9020eSMichael S. Tsirkin static int pit_ioport_read(struct kvm_io_device *this,
5187837699fSSheng Yang 			   gpa_t addr, int len, void *data)
5197837699fSSheng Yang {
520d76685c4SGregory Haskins 	struct kvm_pit *pit = dev_to_pit(this);
5217837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
5227837699fSSheng Yang 	struct kvm *kvm = pit->kvm;
5237837699fSSheng Yang 	int ret, count;
5247837699fSSheng Yang 	struct kvm_kpit_channel_state *s;
525bda9020eSMichael S. Tsirkin 	if (!pit_in_range(addr))
526bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
5277837699fSSheng Yang 
5287837699fSSheng Yang 	addr &= KVM_PIT_CHANNEL_MASK;
529ee73f656SMarcelo Tosatti 	if (addr == 3)
530ee73f656SMarcelo Tosatti 		return 0;
531ee73f656SMarcelo Tosatti 
5327837699fSSheng Yang 	s = &pit_state->channels[addr];
5337837699fSSheng Yang 
5347837699fSSheng Yang 	mutex_lock(&pit_state->lock);
5357837699fSSheng Yang 
5367837699fSSheng Yang 	if (s->status_latched) {
5377837699fSSheng Yang 		s->status_latched = 0;
5387837699fSSheng Yang 		ret = s->status;
5397837699fSSheng Yang 	} else if (s->count_latched) {
5407837699fSSheng Yang 		switch (s->count_latched) {
5417837699fSSheng Yang 		default:
5427837699fSSheng Yang 		case RW_STATE_LSB:
5437837699fSSheng Yang 			ret = s->latched_count & 0xff;
5447837699fSSheng Yang 			s->count_latched = 0;
5457837699fSSheng Yang 			break;
5467837699fSSheng Yang 		case RW_STATE_MSB:
5477837699fSSheng Yang 			ret = s->latched_count >> 8;
5487837699fSSheng Yang 			s->count_latched = 0;
5497837699fSSheng Yang 			break;
5507837699fSSheng Yang 		case RW_STATE_WORD0:
5517837699fSSheng Yang 			ret = s->latched_count & 0xff;
5527837699fSSheng Yang 			s->count_latched = RW_STATE_MSB;
5537837699fSSheng Yang 			break;
5547837699fSSheng Yang 		}
5557837699fSSheng Yang 	} else {
5567837699fSSheng Yang 		switch (s->read_state) {
5577837699fSSheng Yang 		default:
5587837699fSSheng Yang 		case RW_STATE_LSB:
5597837699fSSheng Yang 			count = pit_get_count(kvm, addr);
5607837699fSSheng Yang 			ret = count & 0xff;
5617837699fSSheng Yang 			break;
5627837699fSSheng Yang 		case RW_STATE_MSB:
5637837699fSSheng Yang 			count = pit_get_count(kvm, addr);
5647837699fSSheng Yang 			ret = (count >> 8) & 0xff;
5657837699fSSheng Yang 			break;
5667837699fSSheng Yang 		case RW_STATE_WORD0:
5677837699fSSheng Yang 			count = pit_get_count(kvm, addr);
5687837699fSSheng Yang 			ret = count & 0xff;
5697837699fSSheng Yang 			s->read_state = RW_STATE_WORD1;
5707837699fSSheng Yang 			break;
5717837699fSSheng Yang 		case RW_STATE_WORD1:
5727837699fSSheng Yang 			count = pit_get_count(kvm, addr);
5737837699fSSheng Yang 			ret = (count >> 8) & 0xff;
5747837699fSSheng Yang 			s->read_state = RW_STATE_WORD0;
5757837699fSSheng Yang 			break;
5767837699fSSheng Yang 		}
5777837699fSSheng Yang 	}
5787837699fSSheng Yang 
5797837699fSSheng Yang 	if (len > sizeof(ret))
5807837699fSSheng Yang 		len = sizeof(ret);
5817837699fSSheng Yang 	memcpy(data, (char *)&ret, len);
5827837699fSSheng Yang 
5837837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
584bda9020eSMichael S. Tsirkin 	return 0;
5857837699fSSheng Yang }
5867837699fSSheng Yang 
587bda9020eSMichael S. Tsirkin static int speaker_ioport_write(struct kvm_io_device *this,
5887837699fSSheng Yang 				gpa_t addr, int len, const void *data)
5897837699fSSheng Yang {
590d76685c4SGregory Haskins 	struct kvm_pit *pit = speaker_to_pit(this);
5917837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
5927837699fSSheng Yang 	struct kvm *kvm = pit->kvm;
5937837699fSSheng Yang 	u32 val = *(u32 *) data;
594bda9020eSMichael S. Tsirkin 	if (addr != KVM_SPEAKER_BASE_ADDRESS)
595bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
5967837699fSSheng Yang 
5977837699fSSheng Yang 	mutex_lock(&pit_state->lock);
5987837699fSSheng Yang 	pit_state->speaker_data_on = (val >> 1) & 1;
5997837699fSSheng Yang 	pit_set_gate(kvm, 2, val & 1);
6007837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
601bda9020eSMichael S. Tsirkin 	return 0;
6027837699fSSheng Yang }
6037837699fSSheng Yang 
604bda9020eSMichael S. Tsirkin static int speaker_ioport_read(struct kvm_io_device *this,
6057837699fSSheng Yang 			       gpa_t addr, int len, void *data)
6067837699fSSheng Yang {
607d76685c4SGregory Haskins 	struct kvm_pit *pit = speaker_to_pit(this);
6087837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
6097837699fSSheng Yang 	struct kvm *kvm = pit->kvm;
6107837699fSSheng Yang 	unsigned int refresh_clock;
6117837699fSSheng Yang 	int ret;
612bda9020eSMichael S. Tsirkin 	if (addr != KVM_SPEAKER_BASE_ADDRESS)
613bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
6147837699fSSheng Yang 
6157837699fSSheng Yang 	/* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
6167837699fSSheng Yang 	refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
6177837699fSSheng Yang 
6187837699fSSheng Yang 	mutex_lock(&pit_state->lock);
6197837699fSSheng Yang 	ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
6207837699fSSheng Yang 		(pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
6217837699fSSheng Yang 	if (len > sizeof(ret))
6227837699fSSheng Yang 		len = sizeof(ret);
6237837699fSSheng Yang 	memcpy(data, (char *)&ret, len);
6247837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
625bda9020eSMichael S. Tsirkin 	return 0;
6267837699fSSheng Yang }
6277837699fSSheng Yang 
628308b0f23SSheng Yang void kvm_pit_reset(struct kvm_pit *pit)
6297837699fSSheng Yang {
6307837699fSSheng Yang 	int i;
631308b0f23SSheng Yang 	struct kvm_kpit_channel_state *c;
632308b0f23SSheng Yang 
633308b0f23SSheng Yang 	mutex_lock(&pit->pit_state.lock);
634e9f42757SBeth Kon 	pit->pit_state.flags = 0;
635308b0f23SSheng Yang 	for (i = 0; i < 3; i++) {
636308b0f23SSheng Yang 		c = &pit->pit_state.channels[i];
637308b0f23SSheng Yang 		c->mode = 0xff;
638308b0f23SSheng Yang 		c->gate = (i != 2);
639308b0f23SSheng Yang 		pit_load_count(pit->kvm, i, 0);
640308b0f23SSheng Yang 	}
641308b0f23SSheng Yang 	mutex_unlock(&pit->pit_state.lock);
642308b0f23SSheng Yang 
643308b0f23SSheng Yang 	atomic_set(&pit->pit_state.pit_timer.pending, 0);
6443cf57fedSMarcelo Tosatti 	pit->pit_state.irq_ack = 1;
645308b0f23SSheng Yang }
646308b0f23SSheng Yang 
6474780c659SAvi Kivity static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
6484780c659SAvi Kivity {
6494780c659SAvi Kivity 	struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
6504780c659SAvi Kivity 
6514780c659SAvi Kivity 	if (!mask) {
6524780c659SAvi Kivity 		atomic_set(&pit->pit_state.pit_timer.pending, 0);
6534780c659SAvi Kivity 		pit->pit_state.irq_ack = 1;
6544780c659SAvi Kivity 	}
6554780c659SAvi Kivity }
6564780c659SAvi Kivity 
657d76685c4SGregory Haskins static const struct kvm_io_device_ops pit_dev_ops = {
658d76685c4SGregory Haskins 	.read     = pit_ioport_read,
659d76685c4SGregory Haskins 	.write    = pit_ioport_write,
660d76685c4SGregory Haskins };
661d76685c4SGregory Haskins 
662d76685c4SGregory Haskins static const struct kvm_io_device_ops speaker_dev_ops = {
663d76685c4SGregory Haskins 	.read     = speaker_ioport_read,
664d76685c4SGregory Haskins 	.write    = speaker_ioport_write,
665d76685c4SGregory Haskins };
666d76685c4SGregory Haskins 
66779fac95eSMarcelo Tosatti /* Caller must hold slots_lock */
668c5ff41ceSJan Kiszka struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
669308b0f23SSheng Yang {
6707837699fSSheng Yang 	struct kvm_pit *pit;
6717837699fSSheng Yang 	struct kvm_kpit_state *pit_state;
672090b7affSGregory Haskins 	int ret;
6737837699fSSheng Yang 
6747837699fSSheng Yang 	pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
6757837699fSSheng Yang 	if (!pit)
6767837699fSSheng Yang 		return NULL;
6777837699fSSheng Yang 
6785550af4dSSheng Yang 	pit->irq_source_id = kvm_request_irq_source_id(kvm);
679e17d1dc0SAvi Kivity 	if (pit->irq_source_id < 0) {
680e17d1dc0SAvi Kivity 		kfree(pit);
6815550af4dSSheng Yang 		return NULL;
682e17d1dc0SAvi Kivity 	}
6835550af4dSSheng Yang 
6847837699fSSheng Yang 	mutex_init(&pit->pit_state.lock);
6857837699fSSheng Yang 	mutex_lock(&pit->pit_state.lock);
68633572ac0SChris Lalancette 	spin_lock_init(&pit->pit_state.inject_lock);
68733572ac0SChris Lalancette 
68833572ac0SChris Lalancette 	pit->wq = create_singlethread_workqueue("kvm-pit-wq");
68933572ac0SChris Lalancette 	if (!pit->wq) {
690673813e8SJiri Slaby 		mutex_unlock(&pit->pit_state.lock);
6916b5d7a9fSXiao Guangrong 		kvm_free_irq_source_id(kvm, pit->irq_source_id);
69233572ac0SChris Lalancette 		kfree(pit);
69333572ac0SChris Lalancette 		return NULL;
69433572ac0SChris Lalancette 	}
69533572ac0SChris Lalancette 	INIT_WORK(&pit->expired, pit_do_work);
6967837699fSSheng Yang 
6977837699fSSheng Yang 	kvm->arch.vpit = pit;
6987837699fSSheng Yang 	pit->kvm = kvm;
6997837699fSSheng Yang 
7007837699fSSheng Yang 	pit_state = &pit->pit_state;
7017837699fSSheng Yang 	pit_state->pit = pit;
7027837699fSSheng Yang 	hrtimer_init(&pit_state->pit_timer.timer,
7037837699fSSheng Yang 		     CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
7043cf57fedSMarcelo Tosatti 	pit_state->irq_ack_notifier.gsi = 0;
7053cf57fedSMarcelo Tosatti 	pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
7063cf57fedSMarcelo Tosatti 	kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
70752d939a0SMarcelo Tosatti 	pit_state->pit_timer.reinject = true;
7087837699fSSheng Yang 	mutex_unlock(&pit->pit_state.lock);
7097837699fSSheng Yang 
710308b0f23SSheng Yang 	kvm_pit_reset(pit);
7117837699fSSheng Yang 
7124780c659SAvi Kivity 	pit->mask_notifier.func = pit_mask_notifer;
7134780c659SAvi Kivity 	kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
7144780c659SAvi Kivity 
7156b66ac1aSGregory Haskins 	kvm_iodevice_init(&pit->dev, &pit_dev_ops);
716e93f8a0fSMarcelo Tosatti 	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &pit->dev);
717090b7affSGregory Haskins 	if (ret < 0)
718090b7affSGregory Haskins 		goto fail;
7196b66ac1aSGregory Haskins 
7206b66ac1aSGregory Haskins 	if (flags & KVM_PIT_SPEAKER_DUMMY) {
7216b66ac1aSGregory Haskins 		kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
722e93f8a0fSMarcelo Tosatti 		ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
723090b7affSGregory Haskins 						&pit->speaker_dev);
724090b7affSGregory Haskins 		if (ret < 0)
725090b7affSGregory Haskins 			goto fail_unregister;
7266b66ac1aSGregory Haskins 	}
7276b66ac1aSGregory Haskins 
7287837699fSSheng Yang 	return pit;
729090b7affSGregory Haskins 
730090b7affSGregory Haskins fail_unregister:
731e93f8a0fSMarcelo Tosatti 	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
732090b7affSGregory Haskins 
733090b7affSGregory Haskins fail:
734d225f53bSWei Yongjun 	kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
735d225f53bSWei Yongjun 	kvm_unregister_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
736090b7affSGregory Haskins 	kvm_free_irq_source_id(kvm, pit->irq_source_id);
7373185bf8cSXiaotian Feng 	destroy_workqueue(pit->wq);
738090b7affSGregory Haskins 	kfree(pit);
739090b7affSGregory Haskins 	return NULL;
7407837699fSSheng Yang }
7417837699fSSheng Yang 
7427837699fSSheng Yang void kvm_free_pit(struct kvm *kvm)
7437837699fSSheng Yang {
7447837699fSSheng Yang 	struct hrtimer *timer;
7457837699fSSheng Yang 
7467837699fSSheng Yang 	if (kvm->arch.vpit) {
747aea924f6SXiao Guangrong 		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &kvm->arch.vpit->dev);
748aea924f6SXiao Guangrong 		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
749aea924f6SXiao Guangrong 					      &kvm->arch.vpit->speaker_dev);
7504780c659SAvi Kivity 		kvm_unregister_irq_mask_notifier(kvm, 0,
7514780c659SAvi Kivity 					       &kvm->arch.vpit->mask_notifier);
75284fde248SGleb Natapov 		kvm_unregister_irq_ack_notifier(kvm,
75384fde248SGleb Natapov 				&kvm->arch.vpit->pit_state.irq_ack_notifier);
7547837699fSSheng Yang 		mutex_lock(&kvm->arch.vpit->pit_state.lock);
7557837699fSSheng Yang 		timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
7567837699fSSheng Yang 		hrtimer_cancel(timer);
75733572ac0SChris Lalancette 		cancel_work_sync(&kvm->arch.vpit->expired);
7585550af4dSSheng Yang 		kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
7597837699fSSheng Yang 		mutex_unlock(&kvm->arch.vpit->pit_state.lock);
76033572ac0SChris Lalancette 		destroy_workqueue(kvm->arch.vpit->wq);
7617837699fSSheng Yang 		kfree(kvm->arch.vpit);
7627837699fSSheng Yang 	}
7637837699fSSheng Yang }
764