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