xref: /openbmc/linux/arch/x86/kvm/i8254.c (revision 0ff77873b1318fc2d77a85e70690d3cd6cafbd41)
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
87837699fSSheng Yang  *
97837699fSSheng Yang  * Permission is hereby granted, free of charge, to any person obtaining a copy
107837699fSSheng Yang  * of this software and associated documentation files (the "Software"), to deal
117837699fSSheng Yang  * in the Software without restriction, including without limitation the rights
127837699fSSheng Yang  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
137837699fSSheng Yang  * copies of the Software, and to permit persons to whom the Software is
147837699fSSheng Yang  * furnished to do so, subject to the following conditions:
157837699fSSheng Yang  *
167837699fSSheng Yang  * The above copyright notice and this permission notice shall be included in
177837699fSSheng Yang  * all copies or substantial portions of the Software.
187837699fSSheng Yang  *
197837699fSSheng Yang  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
207837699fSSheng Yang  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
217837699fSSheng Yang  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
227837699fSSheng Yang  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
237837699fSSheng Yang  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
247837699fSSheng Yang  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
257837699fSSheng Yang  * THE SOFTWARE.
267837699fSSheng Yang  *
277837699fSSheng Yang  * Authors:
287837699fSSheng Yang  *   Sheng Yang <sheng.yang@intel.com>
297837699fSSheng Yang  *   Based on QEMU and Xen.
307837699fSSheng Yang  */
317837699fSSheng Yang 
327837699fSSheng Yang #include <linux/kvm_host.h>
337837699fSSheng Yang 
347837699fSSheng Yang #include "irq.h"
357837699fSSheng Yang #include "i8254.h"
367837699fSSheng Yang 
377837699fSSheng Yang #ifndef CONFIG_X86_64
386f6d6a1aSRoman Zippel #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
397837699fSSheng Yang #else
407837699fSSheng Yang #define mod_64(x, y) ((x) % (y))
417837699fSSheng Yang #endif
427837699fSSheng Yang 
437837699fSSheng Yang #define RW_STATE_LSB 1
447837699fSSheng Yang #define RW_STATE_MSB 2
457837699fSSheng Yang #define RW_STATE_WORD0 3
467837699fSSheng Yang #define RW_STATE_WORD1 4
477837699fSSheng Yang 
487837699fSSheng Yang /* Compute with 96 bit intermediate result: (a*b)/c */
497837699fSSheng Yang static u64 muldiv64(u64 a, u32 b, u32 c)
507837699fSSheng Yang {
517837699fSSheng Yang 	union {
527837699fSSheng Yang 		u64 ll;
537837699fSSheng Yang 		struct {
547837699fSSheng Yang 			u32 low, high;
557837699fSSheng Yang 		} l;
567837699fSSheng Yang 	} u, res;
577837699fSSheng Yang 	u64 rl, rh;
587837699fSSheng Yang 
597837699fSSheng Yang 	u.ll = a;
607837699fSSheng Yang 	rl = (u64)u.l.low * (u64)b;
617837699fSSheng Yang 	rh = (u64)u.l.high * (u64)b;
627837699fSSheng Yang 	rh += (rl >> 32);
636f6d6a1aSRoman Zippel 	res.l.high = div64_u64(rh, c);
646f6d6a1aSRoman Zippel 	res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
657837699fSSheng Yang 	return res.ll;
667837699fSSheng Yang }
677837699fSSheng Yang 
687837699fSSheng Yang static void pit_set_gate(struct kvm *kvm, int channel, u32 val)
697837699fSSheng Yang {
707837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
717837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
727837699fSSheng Yang 
737837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
747837699fSSheng Yang 
757837699fSSheng Yang 	switch (c->mode) {
767837699fSSheng Yang 	default:
777837699fSSheng Yang 	case 0:
787837699fSSheng Yang 	case 4:
797837699fSSheng Yang 		/* XXX: just disable/enable counting */
807837699fSSheng Yang 		break;
817837699fSSheng Yang 	case 1:
827837699fSSheng Yang 	case 2:
837837699fSSheng Yang 	case 3:
847837699fSSheng Yang 	case 5:
857837699fSSheng Yang 		/* Restart counting on rising edge. */
867837699fSSheng Yang 		if (c->gate < val)
877837699fSSheng Yang 			c->count_load_time = ktime_get();
887837699fSSheng Yang 		break;
897837699fSSheng Yang 	}
907837699fSSheng Yang 
917837699fSSheng Yang 	c->gate = val;
927837699fSSheng Yang }
937837699fSSheng Yang 
948b2cf73cSHarvey Harrison static int pit_get_gate(struct kvm *kvm, int channel)
957837699fSSheng Yang {
967837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
977837699fSSheng Yang 
987837699fSSheng Yang 	return kvm->arch.vpit->pit_state.channels[channel].gate;
997837699fSSheng Yang }
1007837699fSSheng Yang 
101fd668423SMarcelo Tosatti static s64 __kpit_elapsed(struct kvm *kvm)
102fd668423SMarcelo Tosatti {
103fd668423SMarcelo Tosatti 	s64 elapsed;
104fd668423SMarcelo Tosatti 	ktime_t remaining;
105fd668423SMarcelo Tosatti 	struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
106fd668423SMarcelo Tosatti 
107*0ff77873SMarcelo Tosatti 	if (!ps->pit_timer.period)
108*0ff77873SMarcelo Tosatti 		return 0;
109*0ff77873SMarcelo Tosatti 
110ede2ccc5SMarcelo Tosatti 	/*
111ede2ccc5SMarcelo Tosatti 	 * The Counter does not stop when it reaches zero. In
112ede2ccc5SMarcelo Tosatti 	 * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
113ede2ccc5SMarcelo Tosatti 	 * the highest count, either FFFF hex for binary counting
114ede2ccc5SMarcelo Tosatti 	 * or 9999 for BCD counting, and continues counting.
115ede2ccc5SMarcelo Tosatti 	 * Modes 2 and 3 are periodic; the Counter reloads
116ede2ccc5SMarcelo Tosatti 	 * itself with the initial count and continues counting
117ede2ccc5SMarcelo Tosatti 	 * from there.
118ede2ccc5SMarcelo Tosatti 	 */
119fd668423SMarcelo Tosatti 	remaining = hrtimer_expires_remaining(&ps->pit_timer.timer);
120fd668423SMarcelo Tosatti 	elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
121ede2ccc5SMarcelo Tosatti 	elapsed = mod_64(elapsed, ps->pit_timer.period);
122fd668423SMarcelo Tosatti 
123fd668423SMarcelo Tosatti 	return elapsed;
124fd668423SMarcelo Tosatti }
125fd668423SMarcelo Tosatti 
126fd668423SMarcelo Tosatti static s64 kpit_elapsed(struct kvm *kvm, struct kvm_kpit_channel_state *c,
127fd668423SMarcelo Tosatti 			int channel)
128fd668423SMarcelo Tosatti {
129fd668423SMarcelo Tosatti 	if (channel == 0)
130fd668423SMarcelo Tosatti 		return __kpit_elapsed(kvm);
131fd668423SMarcelo Tosatti 
132fd668423SMarcelo Tosatti 	return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
133fd668423SMarcelo Tosatti }
134fd668423SMarcelo Tosatti 
1357837699fSSheng Yang static int pit_get_count(struct kvm *kvm, int channel)
1367837699fSSheng Yang {
1377837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
1387837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
1397837699fSSheng Yang 	s64 d, t;
1407837699fSSheng Yang 	int counter;
1417837699fSSheng Yang 
1427837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
1437837699fSSheng Yang 
144fd668423SMarcelo Tosatti 	t = kpit_elapsed(kvm, c, channel);
1457837699fSSheng Yang 	d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
1467837699fSSheng Yang 
1477837699fSSheng Yang 	switch (c->mode) {
1487837699fSSheng Yang 	case 0:
1497837699fSSheng Yang 	case 1:
1507837699fSSheng Yang 	case 4:
1517837699fSSheng Yang 	case 5:
1527837699fSSheng Yang 		counter = (c->count - d) & 0xffff;
1537837699fSSheng Yang 		break;
1547837699fSSheng Yang 	case 3:
1557837699fSSheng Yang 		/* XXX: may be incorrect for odd counts */
1567837699fSSheng Yang 		counter = c->count - (mod_64((2 * d), c->count));
1577837699fSSheng Yang 		break;
1587837699fSSheng Yang 	default:
1597837699fSSheng Yang 		counter = c->count - mod_64(d, c->count);
1607837699fSSheng Yang 		break;
1617837699fSSheng Yang 	}
1627837699fSSheng Yang 	return counter;
1637837699fSSheng Yang }
1647837699fSSheng Yang 
1657837699fSSheng Yang static int pit_get_out(struct kvm *kvm, int channel)
1667837699fSSheng Yang {
1677837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
1687837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
1697837699fSSheng Yang 	s64 d, t;
1707837699fSSheng Yang 	int out;
1717837699fSSheng Yang 
1727837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
1737837699fSSheng Yang 
174fd668423SMarcelo Tosatti 	t = kpit_elapsed(kvm, c, channel);
1757837699fSSheng Yang 	d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
1767837699fSSheng Yang 
1777837699fSSheng Yang 	switch (c->mode) {
1787837699fSSheng Yang 	default:
1797837699fSSheng Yang 	case 0:
1807837699fSSheng Yang 		out = (d >= c->count);
1817837699fSSheng Yang 		break;
1827837699fSSheng Yang 	case 1:
1837837699fSSheng Yang 		out = (d < c->count);
1847837699fSSheng Yang 		break;
1857837699fSSheng Yang 	case 2:
1867837699fSSheng Yang 		out = ((mod_64(d, c->count) == 0) && (d != 0));
1877837699fSSheng Yang 		break;
1887837699fSSheng Yang 	case 3:
1897837699fSSheng Yang 		out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
1907837699fSSheng Yang 		break;
1917837699fSSheng Yang 	case 4:
1927837699fSSheng Yang 	case 5:
1937837699fSSheng Yang 		out = (d == c->count);
1947837699fSSheng Yang 		break;
1957837699fSSheng Yang 	}
1967837699fSSheng Yang 
1977837699fSSheng Yang 	return out;
1987837699fSSheng Yang }
1997837699fSSheng Yang 
2007837699fSSheng Yang static void pit_latch_count(struct kvm *kvm, int channel)
2017837699fSSheng Yang {
2027837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
2037837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
2047837699fSSheng Yang 
2057837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
2067837699fSSheng Yang 
2077837699fSSheng Yang 	if (!c->count_latched) {
2087837699fSSheng Yang 		c->latched_count = pit_get_count(kvm, channel);
2097837699fSSheng Yang 		c->count_latched = c->rw_mode;
2107837699fSSheng Yang 	}
2117837699fSSheng Yang }
2127837699fSSheng Yang 
2137837699fSSheng Yang static void pit_latch_status(struct kvm *kvm, int channel)
2147837699fSSheng Yang {
2157837699fSSheng Yang 	struct kvm_kpit_channel_state *c =
2167837699fSSheng Yang 		&kvm->arch.vpit->pit_state.channels[channel];
2177837699fSSheng Yang 
2187837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
2197837699fSSheng Yang 
2207837699fSSheng Yang 	if (!c->status_latched) {
2217837699fSSheng Yang 		/* TODO: Return NULL COUNT (bit 6). */
2227837699fSSheng Yang 		c->status = ((pit_get_out(kvm, channel) << 7) |
2237837699fSSheng Yang 				(c->rw_mode << 4) |
2247837699fSSheng Yang 				(c->mode << 1) |
2257837699fSSheng Yang 				c->bcd);
2267837699fSSheng Yang 		c->status_latched = 1;
2277837699fSSheng Yang 	}
2287837699fSSheng Yang }
2297837699fSSheng Yang 
2303d80840dSMarcelo Tosatti int pit_has_pending_timer(struct kvm_vcpu *vcpu)
2313d80840dSMarcelo Tosatti {
2323d80840dSMarcelo Tosatti 	struct kvm_pit *pit = vcpu->kvm->arch.vpit;
2333d80840dSMarcelo Tosatti 
2343cf57fedSMarcelo Tosatti 	if (pit && vcpu->vcpu_id == 0 && pit->pit_state.irq_ack)
2353d80840dSMarcelo Tosatti 		return atomic_read(&pit->pit_state.pit_timer.pending);
2363d80840dSMarcelo Tosatti 	return 0;
2373d80840dSMarcelo Tosatti }
2383d80840dSMarcelo Tosatti 
239ee032c99SHarvey Harrison static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
2403cf57fedSMarcelo Tosatti {
2413cf57fedSMarcelo Tosatti 	struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
2423cf57fedSMarcelo Tosatti 						 irq_ack_notifier);
2433cf57fedSMarcelo Tosatti 	spin_lock(&ps->inject_lock);
2443cf57fedSMarcelo Tosatti 	if (atomic_dec_return(&ps->pit_timer.pending) < 0)
245dc7404ceSAvi Kivity 		atomic_inc(&ps->pit_timer.pending);
2463cf57fedSMarcelo Tosatti 	ps->irq_ack = 1;
2473cf57fedSMarcelo Tosatti 	spin_unlock(&ps->inject_lock);
2483cf57fedSMarcelo Tosatti }
2493cf57fedSMarcelo Tosatti 
2502f599714SMarcelo Tosatti void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
2512f599714SMarcelo Tosatti {
2522f599714SMarcelo Tosatti 	struct kvm_pit *pit = vcpu->kvm->arch.vpit;
2532f599714SMarcelo Tosatti 	struct hrtimer *timer;
2542f599714SMarcelo Tosatti 
2552f599714SMarcelo Tosatti 	if (vcpu->vcpu_id != 0 || !pit)
2562f599714SMarcelo Tosatti 		return;
2572f599714SMarcelo Tosatti 
2582f599714SMarcelo Tosatti 	timer = &pit->pit_state.pit_timer.timer;
2592f599714SMarcelo Tosatti 	if (hrtimer_cancel(timer))
260beb20d52SArjan van de Ven 		hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
2612f599714SMarcelo Tosatti }
2622f599714SMarcelo Tosatti 
263d3c7b77dSMarcelo Tosatti static void destroy_pit_timer(struct kvm_timer *pt)
2647837699fSSheng Yang {
2657837699fSSheng Yang 	pr_debug("pit: execute del timer!\n");
2667837699fSSheng Yang 	hrtimer_cancel(&pt->timer);
2677837699fSSheng Yang }
2687837699fSSheng Yang 
269d3c7b77dSMarcelo Tosatti static bool kpit_is_periodic(struct kvm_timer *ktimer)
270d3c7b77dSMarcelo Tosatti {
271d3c7b77dSMarcelo Tosatti 	struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state,
272d3c7b77dSMarcelo Tosatti 						 pit_timer);
273d3c7b77dSMarcelo Tosatti 	return ps->is_periodic;
274d3c7b77dSMarcelo Tosatti }
275d3c7b77dSMarcelo Tosatti 
276386eb6e8SHannes Eder static struct kvm_timer_ops kpit_ops = {
277d3c7b77dSMarcelo Tosatti 	.is_periodic = kpit_is_periodic,
278d3c7b77dSMarcelo Tosatti };
279d3c7b77dSMarcelo Tosatti 
2803cf57fedSMarcelo Tosatti static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
2817837699fSSheng Yang {
282d3c7b77dSMarcelo Tosatti 	struct kvm_timer *pt = &ps->pit_timer;
2837837699fSSheng Yang 	s64 interval;
2847837699fSSheng Yang 
2857837699fSSheng Yang 	interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
2867837699fSSheng Yang 
2877837699fSSheng Yang 	pr_debug("pit: create pit timer, interval is %llu nsec\n", interval);
2887837699fSSheng Yang 
2897837699fSSheng Yang 	/* TODO The new value only affected after the retriggered */
2907837699fSSheng Yang 	hrtimer_cancel(&pt->timer);
291ede2ccc5SMarcelo Tosatti 	pt->period = interval;
292d3c7b77dSMarcelo Tosatti 	ps->is_periodic = is_period;
293d3c7b77dSMarcelo Tosatti 
294d3c7b77dSMarcelo Tosatti 	pt->timer.function = kvm_timer_fn;
295d3c7b77dSMarcelo Tosatti 	pt->t_ops = &kpit_ops;
296d3c7b77dSMarcelo Tosatti 	pt->kvm = ps->pit->kvm;
297d3c7b77dSMarcelo Tosatti 	pt->vcpu_id = 0;
298d3c7b77dSMarcelo Tosatti 
2997837699fSSheng Yang 	atomic_set(&pt->pending, 0);
3003cf57fedSMarcelo Tosatti 	ps->irq_ack = 1;
3017837699fSSheng Yang 
3027837699fSSheng Yang 	hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
3037837699fSSheng Yang 		      HRTIMER_MODE_ABS);
3047837699fSSheng Yang }
3057837699fSSheng Yang 
3067837699fSSheng Yang static void pit_load_count(struct kvm *kvm, int channel, u32 val)
3077837699fSSheng Yang {
3087837699fSSheng Yang 	struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
3097837699fSSheng Yang 
3107837699fSSheng Yang 	WARN_ON(!mutex_is_locked(&ps->lock));
3117837699fSSheng Yang 
3127837699fSSheng Yang 	pr_debug("pit: load_count val is %d, channel is %d\n", val, channel);
3137837699fSSheng Yang 
3147837699fSSheng Yang 	/*
315ede2ccc5SMarcelo Tosatti 	 * The largest possible initial count is 0; this is equivalent
316ede2ccc5SMarcelo Tosatti 	 * to 216 for binary counting and 104 for BCD counting.
3177837699fSSheng Yang 	 */
3187837699fSSheng Yang 	if (val == 0)
3197837699fSSheng Yang 		val = 0x10000;
3207837699fSSheng Yang 
3217837699fSSheng Yang 	ps->channels[channel].count = val;
3227837699fSSheng Yang 
323fd668423SMarcelo Tosatti 	if (channel != 0) {
324fd668423SMarcelo Tosatti 		ps->channels[channel].count_load_time = ktime_get();
3257837699fSSheng Yang 		return;
326fd668423SMarcelo Tosatti 	}
3277837699fSSheng Yang 
3287837699fSSheng Yang 	/* Two types of timer
3297837699fSSheng Yang 	 * mode 1 is one shot, mode 2 is period, otherwise del timer */
3307837699fSSheng Yang 	switch (ps->channels[0].mode) {
331ede2ccc5SMarcelo Tosatti 	case 0:
3327837699fSSheng Yang 	case 1:
333ece15babSMarcelo Tosatti         /* FIXME: enhance mode 4 precision */
334ece15babSMarcelo Tosatti 	case 4:
3353cf57fedSMarcelo Tosatti 		create_pit_timer(ps, val, 0);
3367837699fSSheng Yang 		break;
3377837699fSSheng Yang 	case 2:
338f6975545SAurelien Jarno 	case 3:
3393cf57fedSMarcelo Tosatti 		create_pit_timer(ps, val, 1);
3407837699fSSheng Yang 		break;
3417837699fSSheng Yang 	default:
3427837699fSSheng Yang 		destroy_pit_timer(&ps->pit_timer);
3437837699fSSheng Yang 	}
3447837699fSSheng Yang }
3457837699fSSheng Yang 
346e0f63cb9SSheng Yang void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val)
347e0f63cb9SSheng Yang {
348e0f63cb9SSheng Yang 	mutex_lock(&kvm->arch.vpit->pit_state.lock);
349e0f63cb9SSheng Yang 	pit_load_count(kvm, channel, val);
350e0f63cb9SSheng Yang 	mutex_unlock(&kvm->arch.vpit->pit_state.lock);
351e0f63cb9SSheng Yang }
352e0f63cb9SSheng Yang 
3537837699fSSheng Yang static void pit_ioport_write(struct kvm_io_device *this,
3547837699fSSheng Yang 			     gpa_t addr, int len, const void *data)
3557837699fSSheng Yang {
3567837699fSSheng Yang 	struct kvm_pit *pit = (struct kvm_pit *)this->private;
3577837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
3587837699fSSheng Yang 	struct kvm *kvm = pit->kvm;
3597837699fSSheng Yang 	int channel, access;
3607837699fSSheng Yang 	struct kvm_kpit_channel_state *s;
3617837699fSSheng Yang 	u32 val = *(u32 *) data;
3627837699fSSheng Yang 
3637837699fSSheng Yang 	val  &= 0xff;
3647837699fSSheng Yang 	addr &= KVM_PIT_CHANNEL_MASK;
3657837699fSSheng Yang 
3667837699fSSheng Yang 	mutex_lock(&pit_state->lock);
3677837699fSSheng Yang 
3687837699fSSheng Yang 	if (val != 0)
3697837699fSSheng Yang 		pr_debug("pit: write addr is 0x%x, len is %d, val is 0x%x\n",
3707837699fSSheng Yang 			  (unsigned int)addr, len, val);
3717837699fSSheng Yang 
3727837699fSSheng Yang 	if (addr == 3) {
3737837699fSSheng Yang 		channel = val >> 6;
3747837699fSSheng Yang 		if (channel == 3) {
3757837699fSSheng Yang 			/* Read-Back Command. */
3767837699fSSheng Yang 			for (channel = 0; channel < 3; channel++) {
3777837699fSSheng Yang 				s = &pit_state->channels[channel];
3787837699fSSheng Yang 				if (val & (2 << channel)) {
3797837699fSSheng Yang 					if (!(val & 0x20))
3807837699fSSheng Yang 						pit_latch_count(kvm, channel);
3817837699fSSheng Yang 					if (!(val & 0x10))
3827837699fSSheng Yang 						pit_latch_status(kvm, channel);
3837837699fSSheng Yang 				}
3847837699fSSheng Yang 			}
3857837699fSSheng Yang 		} else {
3867837699fSSheng Yang 			/* Select Counter <channel>. */
3877837699fSSheng Yang 			s = &pit_state->channels[channel];
3887837699fSSheng Yang 			access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
3897837699fSSheng Yang 			if (access == 0) {
3907837699fSSheng Yang 				pit_latch_count(kvm, channel);
3917837699fSSheng Yang 			} else {
3927837699fSSheng Yang 				s->rw_mode = access;
3937837699fSSheng Yang 				s->read_state = access;
3947837699fSSheng Yang 				s->write_state = access;
3957837699fSSheng Yang 				s->mode = (val >> 1) & 7;
3967837699fSSheng Yang 				if (s->mode > 5)
3977837699fSSheng Yang 					s->mode -= 4;
3987837699fSSheng Yang 				s->bcd = val & 1;
3997837699fSSheng Yang 			}
4007837699fSSheng Yang 		}
4017837699fSSheng Yang 	} else {
4027837699fSSheng Yang 		/* Write Count. */
4037837699fSSheng Yang 		s = &pit_state->channels[addr];
4047837699fSSheng Yang 		switch (s->write_state) {
4057837699fSSheng Yang 		default:
4067837699fSSheng Yang 		case RW_STATE_LSB:
4077837699fSSheng Yang 			pit_load_count(kvm, addr, val);
4087837699fSSheng Yang 			break;
4097837699fSSheng Yang 		case RW_STATE_MSB:
4107837699fSSheng Yang 			pit_load_count(kvm, addr, val << 8);
4117837699fSSheng Yang 			break;
4127837699fSSheng Yang 		case RW_STATE_WORD0:
4137837699fSSheng Yang 			s->write_latch = val;
4147837699fSSheng Yang 			s->write_state = RW_STATE_WORD1;
4157837699fSSheng Yang 			break;
4167837699fSSheng Yang 		case RW_STATE_WORD1:
4177837699fSSheng Yang 			pit_load_count(kvm, addr, s->write_latch | (val << 8));
4187837699fSSheng Yang 			s->write_state = RW_STATE_WORD0;
4197837699fSSheng Yang 			break;
4207837699fSSheng Yang 		}
4217837699fSSheng Yang 	}
4227837699fSSheng Yang 
4237837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
4247837699fSSheng Yang }
4257837699fSSheng Yang 
4267837699fSSheng Yang static void pit_ioport_read(struct kvm_io_device *this,
4277837699fSSheng Yang 			    gpa_t addr, int len, void *data)
4287837699fSSheng Yang {
4297837699fSSheng Yang 	struct kvm_pit *pit = (struct kvm_pit *)this->private;
4307837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
4317837699fSSheng Yang 	struct kvm *kvm = pit->kvm;
4327837699fSSheng Yang 	int ret, count;
4337837699fSSheng Yang 	struct kvm_kpit_channel_state *s;
4347837699fSSheng Yang 
4357837699fSSheng Yang 	addr &= KVM_PIT_CHANNEL_MASK;
4367837699fSSheng Yang 	s = &pit_state->channels[addr];
4377837699fSSheng Yang 
4387837699fSSheng Yang 	mutex_lock(&pit_state->lock);
4397837699fSSheng Yang 
4407837699fSSheng Yang 	if (s->status_latched) {
4417837699fSSheng Yang 		s->status_latched = 0;
4427837699fSSheng Yang 		ret = s->status;
4437837699fSSheng Yang 	} else if (s->count_latched) {
4447837699fSSheng Yang 		switch (s->count_latched) {
4457837699fSSheng Yang 		default:
4467837699fSSheng Yang 		case RW_STATE_LSB:
4477837699fSSheng Yang 			ret = s->latched_count & 0xff;
4487837699fSSheng Yang 			s->count_latched = 0;
4497837699fSSheng Yang 			break;
4507837699fSSheng Yang 		case RW_STATE_MSB:
4517837699fSSheng Yang 			ret = s->latched_count >> 8;
4527837699fSSheng Yang 			s->count_latched = 0;
4537837699fSSheng Yang 			break;
4547837699fSSheng Yang 		case RW_STATE_WORD0:
4557837699fSSheng Yang 			ret = s->latched_count & 0xff;
4567837699fSSheng Yang 			s->count_latched = RW_STATE_MSB;
4577837699fSSheng Yang 			break;
4587837699fSSheng Yang 		}
4597837699fSSheng Yang 	} else {
4607837699fSSheng Yang 		switch (s->read_state) {
4617837699fSSheng Yang 		default:
4627837699fSSheng Yang 		case RW_STATE_LSB:
4637837699fSSheng Yang 			count = pit_get_count(kvm, addr);
4647837699fSSheng Yang 			ret = count & 0xff;
4657837699fSSheng Yang 			break;
4667837699fSSheng Yang 		case RW_STATE_MSB:
4677837699fSSheng Yang 			count = pit_get_count(kvm, addr);
4687837699fSSheng Yang 			ret = (count >> 8) & 0xff;
4697837699fSSheng Yang 			break;
4707837699fSSheng Yang 		case RW_STATE_WORD0:
4717837699fSSheng Yang 			count = pit_get_count(kvm, addr);
4727837699fSSheng Yang 			ret = count & 0xff;
4737837699fSSheng Yang 			s->read_state = RW_STATE_WORD1;
4747837699fSSheng Yang 			break;
4757837699fSSheng Yang 		case RW_STATE_WORD1:
4767837699fSSheng Yang 			count = pit_get_count(kvm, addr);
4777837699fSSheng Yang 			ret = (count >> 8) & 0xff;
4787837699fSSheng Yang 			s->read_state = RW_STATE_WORD0;
4797837699fSSheng Yang 			break;
4807837699fSSheng Yang 		}
4817837699fSSheng Yang 	}
4827837699fSSheng Yang 
4837837699fSSheng Yang 	if (len > sizeof(ret))
4847837699fSSheng Yang 		len = sizeof(ret);
4857837699fSSheng Yang 	memcpy(data, (char *)&ret, len);
4867837699fSSheng Yang 
4877837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
4887837699fSSheng Yang }
4897837699fSSheng Yang 
49092760499SLaurent Vivier static int pit_in_range(struct kvm_io_device *this, gpa_t addr,
49192760499SLaurent Vivier 			int len, int is_write)
4927837699fSSheng Yang {
4937837699fSSheng Yang 	return ((addr >= KVM_PIT_BASE_ADDRESS) &&
4947837699fSSheng Yang 		(addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
4957837699fSSheng Yang }
4967837699fSSheng Yang 
4977837699fSSheng Yang static void speaker_ioport_write(struct kvm_io_device *this,
4987837699fSSheng Yang 				 gpa_t addr, int len, const void *data)
4997837699fSSheng Yang {
5007837699fSSheng Yang 	struct kvm_pit *pit = (struct kvm_pit *)this->private;
5017837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
5027837699fSSheng Yang 	struct kvm *kvm = pit->kvm;
5037837699fSSheng Yang 	u32 val = *(u32 *) data;
5047837699fSSheng Yang 
5057837699fSSheng Yang 	mutex_lock(&pit_state->lock);
5067837699fSSheng Yang 	pit_state->speaker_data_on = (val >> 1) & 1;
5077837699fSSheng Yang 	pit_set_gate(kvm, 2, val & 1);
5087837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
5097837699fSSheng Yang }
5107837699fSSheng Yang 
5117837699fSSheng Yang static void speaker_ioport_read(struct kvm_io_device *this,
5127837699fSSheng Yang 				gpa_t addr, int len, void *data)
5137837699fSSheng Yang {
5147837699fSSheng Yang 	struct kvm_pit *pit = (struct kvm_pit *)this->private;
5157837699fSSheng Yang 	struct kvm_kpit_state *pit_state = &pit->pit_state;
5167837699fSSheng Yang 	struct kvm *kvm = pit->kvm;
5177837699fSSheng Yang 	unsigned int refresh_clock;
5187837699fSSheng Yang 	int ret;
5197837699fSSheng Yang 
5207837699fSSheng Yang 	/* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
5217837699fSSheng Yang 	refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
5227837699fSSheng Yang 
5237837699fSSheng Yang 	mutex_lock(&pit_state->lock);
5247837699fSSheng Yang 	ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
5257837699fSSheng Yang 		(pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
5267837699fSSheng Yang 	if (len > sizeof(ret))
5277837699fSSheng Yang 		len = sizeof(ret);
5287837699fSSheng Yang 	memcpy(data, (char *)&ret, len);
5297837699fSSheng Yang 	mutex_unlock(&pit_state->lock);
5307837699fSSheng Yang }
5317837699fSSheng Yang 
53292760499SLaurent Vivier static int speaker_in_range(struct kvm_io_device *this, gpa_t addr,
53392760499SLaurent Vivier 			    int len, int is_write)
5347837699fSSheng Yang {
5357837699fSSheng Yang 	return (addr == KVM_SPEAKER_BASE_ADDRESS);
5367837699fSSheng Yang }
5377837699fSSheng Yang 
538308b0f23SSheng Yang void kvm_pit_reset(struct kvm_pit *pit)
5397837699fSSheng Yang {
5407837699fSSheng Yang 	int i;
541308b0f23SSheng Yang 	struct kvm_kpit_channel_state *c;
542308b0f23SSheng Yang 
543308b0f23SSheng Yang 	mutex_lock(&pit->pit_state.lock);
544308b0f23SSheng Yang 	for (i = 0; i < 3; i++) {
545308b0f23SSheng Yang 		c = &pit->pit_state.channels[i];
546308b0f23SSheng Yang 		c->mode = 0xff;
547308b0f23SSheng Yang 		c->gate = (i != 2);
548308b0f23SSheng Yang 		pit_load_count(pit->kvm, i, 0);
549308b0f23SSheng Yang 	}
550308b0f23SSheng Yang 	mutex_unlock(&pit->pit_state.lock);
551308b0f23SSheng Yang 
552308b0f23SSheng Yang 	atomic_set(&pit->pit_state.pit_timer.pending, 0);
5533cf57fedSMarcelo Tosatti 	pit->pit_state.irq_ack = 1;
554308b0f23SSheng Yang }
555308b0f23SSheng Yang 
5564780c659SAvi Kivity static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
5574780c659SAvi Kivity {
5584780c659SAvi Kivity 	struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
5594780c659SAvi Kivity 
5604780c659SAvi Kivity 	if (!mask) {
5614780c659SAvi Kivity 		atomic_set(&pit->pit_state.pit_timer.pending, 0);
5624780c659SAvi Kivity 		pit->pit_state.irq_ack = 1;
5634780c659SAvi Kivity 	}
5644780c659SAvi Kivity }
5654780c659SAvi Kivity 
566308b0f23SSheng Yang struct kvm_pit *kvm_create_pit(struct kvm *kvm)
567308b0f23SSheng Yang {
5687837699fSSheng Yang 	struct kvm_pit *pit;
5697837699fSSheng Yang 	struct kvm_kpit_state *pit_state;
5707837699fSSheng Yang 
5717837699fSSheng Yang 	pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
5727837699fSSheng Yang 	if (!pit)
5737837699fSSheng Yang 		return NULL;
5747837699fSSheng Yang 
5755550af4dSSheng Yang 	pit->irq_source_id = kvm_request_irq_source_id(kvm);
576e17d1dc0SAvi Kivity 	if (pit->irq_source_id < 0) {
577e17d1dc0SAvi Kivity 		kfree(pit);
5785550af4dSSheng Yang 		return NULL;
579e17d1dc0SAvi Kivity 	}
5805550af4dSSheng Yang 
5817837699fSSheng Yang 	mutex_init(&pit->pit_state.lock);
5827837699fSSheng Yang 	mutex_lock(&pit->pit_state.lock);
5833cf57fedSMarcelo Tosatti 	spin_lock_init(&pit->pit_state.inject_lock);
5847837699fSSheng Yang 
5857837699fSSheng Yang 	/* Initialize PIO device */
5867837699fSSheng Yang 	pit->dev.read = pit_ioport_read;
5877837699fSSheng Yang 	pit->dev.write = pit_ioport_write;
5887837699fSSheng Yang 	pit->dev.in_range = pit_in_range;
5897837699fSSheng Yang 	pit->dev.private = pit;
5907837699fSSheng Yang 	kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
5917837699fSSheng Yang 
5927837699fSSheng Yang 	pit->speaker_dev.read = speaker_ioport_read;
5937837699fSSheng Yang 	pit->speaker_dev.write = speaker_ioport_write;
5947837699fSSheng Yang 	pit->speaker_dev.in_range = speaker_in_range;
5957837699fSSheng Yang 	pit->speaker_dev.private = pit;
5967837699fSSheng Yang 	kvm_io_bus_register_dev(&kvm->pio_bus, &pit->speaker_dev);
5977837699fSSheng Yang 
5987837699fSSheng Yang 	kvm->arch.vpit = pit;
5997837699fSSheng Yang 	pit->kvm = kvm;
6007837699fSSheng Yang 
6017837699fSSheng Yang 	pit_state = &pit->pit_state;
6027837699fSSheng Yang 	pit_state->pit = pit;
6037837699fSSheng Yang 	hrtimer_init(&pit_state->pit_timer.timer,
6047837699fSSheng Yang 		     CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
6053cf57fedSMarcelo Tosatti 	pit_state->irq_ack_notifier.gsi = 0;
6063cf57fedSMarcelo Tosatti 	pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
6073cf57fedSMarcelo Tosatti 	kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
60852d939a0SMarcelo Tosatti 	pit_state->pit_timer.reinject = true;
6097837699fSSheng Yang 	mutex_unlock(&pit->pit_state.lock);
6107837699fSSheng Yang 
611308b0f23SSheng Yang 	kvm_pit_reset(pit);
6127837699fSSheng Yang 
6134780c659SAvi Kivity 	pit->mask_notifier.func = pit_mask_notifer;
6144780c659SAvi Kivity 	kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
6154780c659SAvi Kivity 
6167837699fSSheng Yang 	return pit;
6177837699fSSheng Yang }
6187837699fSSheng Yang 
6197837699fSSheng Yang void kvm_free_pit(struct kvm *kvm)
6207837699fSSheng Yang {
6217837699fSSheng Yang 	struct hrtimer *timer;
6227837699fSSheng Yang 
6237837699fSSheng Yang 	if (kvm->arch.vpit) {
6244780c659SAvi Kivity 		kvm_unregister_irq_mask_notifier(kvm, 0,
6254780c659SAvi Kivity 					       &kvm->arch.vpit->mask_notifier);
6267837699fSSheng Yang 		mutex_lock(&kvm->arch.vpit->pit_state.lock);
6277837699fSSheng Yang 		timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
6287837699fSSheng Yang 		hrtimer_cancel(timer);
6295550af4dSSheng Yang 		kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
6307837699fSSheng Yang 		mutex_unlock(&kvm->arch.vpit->pit_state.lock);
6317837699fSSheng Yang 		kfree(kvm->arch.vpit);
6327837699fSSheng Yang 	}
6337837699fSSheng Yang }
6347837699fSSheng Yang 
6358b2cf73cSHarvey Harrison static void __inject_pit_timer_intr(struct kvm *kvm)
6367837699fSSheng Yang {
63723930f95SJan Kiszka 	struct kvm_vcpu *vcpu;
63823930f95SJan Kiszka 	int i;
63923930f95SJan Kiszka 
6407837699fSSheng Yang 	mutex_lock(&kvm->lock);
6415550af4dSSheng Yang 	kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
6425550af4dSSheng Yang 	kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
6437837699fSSheng Yang 	mutex_unlock(&kvm->lock);
64423930f95SJan Kiszka 
64523930f95SJan Kiszka 	/*
6468fdb2351SJan Kiszka 	 * Provides NMI watchdog support via Virtual Wire mode.
6478fdb2351SJan Kiszka 	 * The route is: PIT -> PIC -> LVT0 in NMI mode.
6488fdb2351SJan Kiszka 	 *
6498fdb2351SJan Kiszka 	 * Note: Our Virtual Wire implementation is simplified, only
6508fdb2351SJan Kiszka 	 * propagating PIT interrupts to all VCPUs when they have set
6518fdb2351SJan Kiszka 	 * LVT0 to NMI delivery. Other PIC interrupts are just sent to
6528fdb2351SJan Kiszka 	 * VCPU0, and only if its LVT0 is in EXTINT mode.
65323930f95SJan Kiszka 	 */
654cc6e462cSJan Kiszka 	if (kvm->arch.vapics_in_nmi_mode > 0)
65523930f95SJan Kiszka 		for (i = 0; i < KVM_MAX_VCPUS; ++i) {
65623930f95SJan Kiszka 			vcpu = kvm->vcpus[i];
6578fdb2351SJan Kiszka 			if (vcpu)
6588fdb2351SJan Kiszka 				kvm_apic_nmi_wd_deliver(vcpu);
65923930f95SJan Kiszka 		}
6607837699fSSheng Yang }
6617837699fSSheng Yang 
6627837699fSSheng Yang void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
6637837699fSSheng Yang {
6647837699fSSheng Yang 	struct kvm_pit *pit = vcpu->kvm->arch.vpit;
6657837699fSSheng Yang 	struct kvm *kvm = vcpu->kvm;
6667837699fSSheng Yang 	struct kvm_kpit_state *ps;
6677837699fSSheng Yang 
6687837699fSSheng Yang 	if (vcpu && pit) {
6693cf57fedSMarcelo Tosatti 		int inject = 0;
6707837699fSSheng Yang 		ps = &pit->pit_state;
6717837699fSSheng Yang 
6723cf57fedSMarcelo Tosatti 		/* Try to inject pending interrupts when
6733cf57fedSMarcelo Tosatti 		 * last one has been acked.
6743cf57fedSMarcelo Tosatti 		 */
6753cf57fedSMarcelo Tosatti 		spin_lock(&ps->inject_lock);
6763cf57fedSMarcelo Tosatti 		if (atomic_read(&ps->pit_timer.pending) && ps->irq_ack) {
6773cf57fedSMarcelo Tosatti 			ps->irq_ack = 0;
6783cf57fedSMarcelo Tosatti 			inject = 1;
6793cf57fedSMarcelo Tosatti 		}
6803cf57fedSMarcelo Tosatti 		spin_unlock(&ps->inject_lock);
6813cf57fedSMarcelo Tosatti 		if (inject)
6827837699fSSheng Yang 			__inject_pit_timer_intr(kvm);
6837837699fSSheng Yang 	}
6847837699fSSheng Yang }
685