i8254.c 17.7 KB
Newer Older
S
Sheng Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * 8253/8254 interval timer emulation
 *
 * Copyright (c) 2003-2004 Fabrice Bellard
 * Copyright (c) 2006 Intel Corporation
 * Copyright (c) 2007 Keir Fraser, XenSource Inc
 * Copyright (c) 2008 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Authors:
 *   Sheng Yang <sheng.yang@intel.com>
 *   Based on QEMU and Xen.
 */

J
Joe Perches 已提交
32 33
#define pr_fmt(fmt) "pit: " fmt

S
Sheng Yang 已提交
34 35 36 37 38 39
#include <linux/kvm_host.h>

#include "irq.h"
#include "i8254.h"

#ifndef CONFIG_X86_64
R
Roman Zippel 已提交
40
#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
S
Sheng Yang 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#else
#define mod_64(x, y) ((x) % (y))
#endif

#define RW_STATE_LSB 1
#define RW_STATE_MSB 2
#define RW_STATE_WORD0 3
#define RW_STATE_WORD1 4

/* Compute with 96 bit intermediate result: (a*b)/c */
static u64 muldiv64(u64 a, u32 b, u32 c)
{
	union {
		u64 ll;
		struct {
			u32 low, high;
		} l;
	} u, res;
	u64 rl, rh;

	u.ll = a;
	rl = (u64)u.l.low * (u64)b;
	rh = (u64)u.l.high * (u64)b;
	rh += (rl >> 32);
R
Roman Zippel 已提交
65 66
	res.l.high = div64_u64(rh, c);
	res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
S
Sheng Yang 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	return res.ll;
}

static void pit_set_gate(struct kvm *kvm, int channel, u32 val)
{
	struct kvm_kpit_channel_state *c =
		&kvm->arch.vpit->pit_state.channels[channel];

	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));

	switch (c->mode) {
	default:
	case 0:
	case 4:
		/* XXX: just disable/enable counting */
		break;
	case 1:
	case 2:
	case 3:
	case 5:
		/* Restart counting on rising edge. */
		if (c->gate < val)
			c->count_load_time = ktime_get();
		break;
	}

	c->gate = val;
}

96
static int pit_get_gate(struct kvm *kvm, int channel)
S
Sheng Yang 已提交
97 98 99 100 101 102
{
	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));

	return kvm->arch.vpit->pit_state.channels[channel].gate;
}

103 104 105 106 107 108
static s64 __kpit_elapsed(struct kvm *kvm)
{
	s64 elapsed;
	ktime_t remaining;
	struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;

109 110 111
	if (!ps->pit_timer.period)
		return 0;

112 113 114 115 116 117 118 119 120
	/*
	 * The Counter does not stop when it reaches zero. In
	 * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
	 * the highest count, either FFFF hex for binary counting
	 * or 9999 for BCD counting, and continues counting.
	 * Modes 2 and 3 are periodic; the Counter reloads
	 * itself with the initial count and continues counting
	 * from there.
	 */
121
	remaining = hrtimer_get_remaining(&ps->pit_timer.timer);
122 123
	elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
	elapsed = mod_64(elapsed, ps->pit_timer.period);
124 125 126 127 128 129 130 131 132 133 134 135 136

	return elapsed;
}

static s64 kpit_elapsed(struct kvm *kvm, struct kvm_kpit_channel_state *c,
			int channel)
{
	if (channel == 0)
		return __kpit_elapsed(kvm);

	return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
}

S
Sheng Yang 已提交
137 138 139 140 141 142 143 144 145
static int pit_get_count(struct kvm *kvm, int channel)
{
	struct kvm_kpit_channel_state *c =
		&kvm->arch.vpit->pit_state.channels[channel];
	s64 d, t;
	int counter;

	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));

146
	t = kpit_elapsed(kvm, c, channel);
S
Sheng Yang 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);

	switch (c->mode) {
	case 0:
	case 1:
	case 4:
	case 5:
		counter = (c->count - d) & 0xffff;
		break;
	case 3:
		/* XXX: may be incorrect for odd counts */
		counter = c->count - (mod_64((2 * d), c->count));
		break;
	default:
		counter = c->count - mod_64(d, c->count);
		break;
	}
	return counter;
}

static int pit_get_out(struct kvm *kvm, int channel)
{
	struct kvm_kpit_channel_state *c =
		&kvm->arch.vpit->pit_state.channels[channel];
	s64 d, t;
	int out;

	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));

176
	t = kpit_elapsed(kvm, c, channel);
S
Sheng Yang 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);

	switch (c->mode) {
	default:
	case 0:
		out = (d >= c->count);
		break;
	case 1:
		out = (d < c->count);
		break;
	case 2:
		out = ((mod_64(d, c->count) == 0) && (d != 0));
		break;
	case 3:
		out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
		break;
	case 4:
	case 5:
		out = (d == c->count);
		break;
	}

	return out;
}

static void pit_latch_count(struct kvm *kvm, int channel)
{
	struct kvm_kpit_channel_state *c =
		&kvm->arch.vpit->pit_state.channels[channel];

	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));

	if (!c->count_latched) {
		c->latched_count = pit_get_count(kvm, channel);
		c->count_latched = c->rw_mode;
	}
}

static void pit_latch_status(struct kvm *kvm, int channel)
{
	struct kvm_kpit_channel_state *c =
		&kvm->arch.vpit->pit_state.channels[channel];

	WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));

	if (!c->status_latched) {
		/* TODO: Return NULL COUNT (bit 6). */
		c->status = ((pit_get_out(kvm, channel) << 7) |
				(c->rw_mode << 4) |
				(c->mode << 1) |
				c->bcd);
		c->status_latched = 1;
	}
}

232 233 234 235
int pit_has_pending_timer(struct kvm_vcpu *vcpu)
{
	struct kvm_pit *pit = vcpu->kvm->arch.vpit;

236
	if (pit && kvm_vcpu_is_bsp(vcpu) && pit->pit_state.irq_ack)
237 238 239 240
		return atomic_read(&pit->pit_state.pit_timer.pending);
	return 0;
}

241
static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
242 243 244 245 246
{
	struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
						 irq_ack_notifier);
	spin_lock(&ps->inject_lock);
	if (atomic_dec_return(&ps->pit_timer.pending) < 0)
247
		atomic_inc(&ps->pit_timer.pending);
248 249 250 251
	ps->irq_ack = 1;
	spin_unlock(&ps->inject_lock);
}

M
Marcelo Tosatti 已提交
252 253 254 255 256
void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
{
	struct kvm_pit *pit = vcpu->kvm->arch.vpit;
	struct hrtimer *timer;

257
	if (!kvm_vcpu_is_bsp(vcpu) || !pit)
M
Marcelo Tosatti 已提交
258 259 260 261
		return;

	timer = &pit->pit_state.pit_timer.timer;
	if (hrtimer_cancel(timer))
262
		hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
M
Marcelo Tosatti 已提交
263 264
}

265
static void destroy_pit_timer(struct kvm_timer *pt)
S
Sheng Yang 已提交
266
{
J
Joe Perches 已提交
267
	pr_debug("execute del timer!\n");
S
Sheng Yang 已提交
268 269 270
	hrtimer_cancel(&pt->timer);
}

271 272 273 274 275 276 277
static bool kpit_is_periodic(struct kvm_timer *ktimer)
{
	struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state,
						 pit_timer);
	return ps->is_periodic;
}

278
static struct kvm_timer_ops kpit_ops = {
279 280 281
	.is_periodic = kpit_is_periodic,
};

282
static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
S
Sheng Yang 已提交
283
{
284
	struct kvm_timer *pt = &ps->pit_timer;
S
Sheng Yang 已提交
285 286 287 288
	s64 interval;

	interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);

J
Joe Perches 已提交
289
	pr_debug("create pit timer, interval is %llu nsec\n", interval);
S
Sheng Yang 已提交
290 291 292

	/* TODO The new value only affected after the retriggered */
	hrtimer_cancel(&pt->timer);
293
	pt->period = interval;
294 295 296 297 298
	ps->is_periodic = is_period;

	pt->timer.function = kvm_timer_fn;
	pt->t_ops = &kpit_ops;
	pt->kvm = ps->pit->kvm;
299
	pt->vcpu = pt->kvm->bsp_vcpu;
300

S
Sheng Yang 已提交
301
	atomic_set(&pt->pending, 0);
302
	ps->irq_ack = 1;
S
Sheng Yang 已提交
303 304 305 306 307 308 309 310 311 312 313

	hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
		      HRTIMER_MODE_ABS);
}

static void pit_load_count(struct kvm *kvm, int channel, u32 val)
{
	struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;

	WARN_ON(!mutex_is_locked(&ps->lock));

J
Joe Perches 已提交
314
	pr_debug("load_count val is %d, channel is %d\n", val, channel);
S
Sheng Yang 已提交
315 316

	/*
317 318
	 * The largest possible initial count is 0; this is equivalent
	 * to 216 for binary counting and 104 for BCD counting.
S
Sheng Yang 已提交
319 320 321 322 323 324
	 */
	if (val == 0)
		val = 0x10000;

	ps->channels[channel].count = val;

325 326
	if (channel != 0) {
		ps->channels[channel].count_load_time = ktime_get();
S
Sheng Yang 已提交
327
		return;
328
	}
S
Sheng Yang 已提交
329 330 331 332

	/* Two types of timer
	 * mode 1 is one shot, mode 2 is period, otherwise del timer */
	switch (ps->channels[0].mode) {
333
	case 0:
S
Sheng Yang 已提交
334
	case 1:
M
Marcelo Tosatti 已提交
335 336
        /* FIXME: enhance mode 4 precision */
	case 4:
B
Beth Kon 已提交
337 338 339
		if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)) {
			create_pit_timer(ps, val, 0);
		}
S
Sheng Yang 已提交
340 341
		break;
	case 2:
A
Aurelien Jarno 已提交
342
	case 3:
B
Beth Kon 已提交
343 344 345
		if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)){
			create_pit_timer(ps, val, 1);
		}
S
Sheng Yang 已提交
346 347 348 349 350 351
		break;
	default:
		destroy_pit_timer(&ps->pit_timer);
	}
}

B
Beth Kon 已提交
352
void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start)
353
{
B
Beth Kon 已提交
354 355 356 357 358 359 360 361 362 363
	u8 saved_mode;
	if (hpet_legacy_start) {
		/* save existing mode for later reenablement */
		saved_mode = kvm->arch.vpit->pit_state.channels[0].mode;
		kvm->arch.vpit->pit_state.channels[0].mode = 0xff; /* disable timer */
		pit_load_count(kvm, channel, val);
		kvm->arch.vpit->pit_state.channels[0].mode = saved_mode;
	} else {
		pit_load_count(kvm, channel, val);
	}
364 365
}

G
Gregory Haskins 已提交
366 367 368 369 370 371 372 373 374 375
static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
{
	return container_of(dev, struct kvm_pit, dev);
}

static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
{
	return container_of(dev, struct kvm_pit, speaker_dev);
}

376 377 378 379 380 381 382 383
static inline int pit_in_range(gpa_t addr)
{
	return ((addr >= KVM_PIT_BASE_ADDRESS) &&
		(addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
}

static int pit_ioport_write(struct kvm_io_device *this,
			    gpa_t addr, int len, const void *data)
S
Sheng Yang 已提交
384
{
G
Gregory Haskins 已提交
385
	struct kvm_pit *pit = dev_to_pit(this);
S
Sheng Yang 已提交
386 387 388 389 390
	struct kvm_kpit_state *pit_state = &pit->pit_state;
	struct kvm *kvm = pit->kvm;
	int channel, access;
	struct kvm_kpit_channel_state *s;
	u32 val = *(u32 *) data;
391 392
	if (!pit_in_range(addr))
		return -EOPNOTSUPP;
S
Sheng Yang 已提交
393 394 395 396 397 398 399

	val  &= 0xff;
	addr &= KVM_PIT_CHANNEL_MASK;

	mutex_lock(&pit_state->lock);

	if (val != 0)
J
Joe Perches 已提交
400 401
		pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
			 (unsigned int)addr, len, val);
S
Sheng Yang 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

	if (addr == 3) {
		channel = val >> 6;
		if (channel == 3) {
			/* Read-Back Command. */
			for (channel = 0; channel < 3; channel++) {
				s = &pit_state->channels[channel];
				if (val & (2 << channel)) {
					if (!(val & 0x20))
						pit_latch_count(kvm, channel);
					if (!(val & 0x10))
						pit_latch_status(kvm, channel);
				}
			}
		} else {
			/* Select Counter <channel>. */
			s = &pit_state->channels[channel];
			access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
			if (access == 0) {
				pit_latch_count(kvm, channel);
			} else {
				s->rw_mode = access;
				s->read_state = access;
				s->write_state = access;
				s->mode = (val >> 1) & 7;
				if (s->mode > 5)
					s->mode -= 4;
				s->bcd = val & 1;
			}
		}
	} else {
		/* Write Count. */
		s = &pit_state->channels[addr];
		switch (s->write_state) {
		default:
		case RW_STATE_LSB:
			pit_load_count(kvm, addr, val);
			break;
		case RW_STATE_MSB:
			pit_load_count(kvm, addr, val << 8);
			break;
		case RW_STATE_WORD0:
			s->write_latch = val;
			s->write_state = RW_STATE_WORD1;
			break;
		case RW_STATE_WORD1:
			pit_load_count(kvm, addr, s->write_latch | (val << 8));
			s->write_state = RW_STATE_WORD0;
			break;
		}
	}

	mutex_unlock(&pit_state->lock);
455
	return 0;
S
Sheng Yang 已提交
456 457
}

458 459
static int pit_ioport_read(struct kvm_io_device *this,
			   gpa_t addr, int len, void *data)
S
Sheng Yang 已提交
460
{
G
Gregory Haskins 已提交
461
	struct kvm_pit *pit = dev_to_pit(this);
S
Sheng Yang 已提交
462 463 464 465
	struct kvm_kpit_state *pit_state = &pit->pit_state;
	struct kvm *kvm = pit->kvm;
	int ret, count;
	struct kvm_kpit_channel_state *s;
466 467
	if (!pit_in_range(addr))
		return -EOPNOTSUPP;
S
Sheng Yang 已提交
468 469

	addr &= KVM_PIT_CHANNEL_MASK;
470 471 472
	if (addr == 3)
		return 0;

S
Sheng Yang 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	s = &pit_state->channels[addr];

	mutex_lock(&pit_state->lock);

	if (s->status_latched) {
		s->status_latched = 0;
		ret = s->status;
	} else if (s->count_latched) {
		switch (s->count_latched) {
		default:
		case RW_STATE_LSB:
			ret = s->latched_count & 0xff;
			s->count_latched = 0;
			break;
		case RW_STATE_MSB:
			ret = s->latched_count >> 8;
			s->count_latched = 0;
			break;
		case RW_STATE_WORD0:
			ret = s->latched_count & 0xff;
			s->count_latched = RW_STATE_MSB;
			break;
		}
	} else {
		switch (s->read_state) {
		default:
		case RW_STATE_LSB:
			count = pit_get_count(kvm, addr);
			ret = count & 0xff;
			break;
		case RW_STATE_MSB:
			count = pit_get_count(kvm, addr);
			ret = (count >> 8) & 0xff;
			break;
		case RW_STATE_WORD0:
			count = pit_get_count(kvm, addr);
			ret = count & 0xff;
			s->read_state = RW_STATE_WORD1;
			break;
		case RW_STATE_WORD1:
			count = pit_get_count(kvm, addr);
			ret = (count >> 8) & 0xff;
			s->read_state = RW_STATE_WORD0;
			break;
		}
	}

	if (len > sizeof(ret))
		len = sizeof(ret);
	memcpy(data, (char *)&ret, len);

	mutex_unlock(&pit_state->lock);
525
	return 0;
S
Sheng Yang 已提交
526 527
}

528 529
static int speaker_ioport_write(struct kvm_io_device *this,
				gpa_t addr, int len, const void *data)
S
Sheng Yang 已提交
530
{
G
Gregory Haskins 已提交
531
	struct kvm_pit *pit = speaker_to_pit(this);
S
Sheng Yang 已提交
532 533 534
	struct kvm_kpit_state *pit_state = &pit->pit_state;
	struct kvm *kvm = pit->kvm;
	u32 val = *(u32 *) data;
535 536
	if (addr != KVM_SPEAKER_BASE_ADDRESS)
		return -EOPNOTSUPP;
S
Sheng Yang 已提交
537 538 539 540 541

	mutex_lock(&pit_state->lock);
	pit_state->speaker_data_on = (val >> 1) & 1;
	pit_set_gate(kvm, 2, val & 1);
	mutex_unlock(&pit_state->lock);
542
	return 0;
S
Sheng Yang 已提交
543 544
}

545 546
static int speaker_ioport_read(struct kvm_io_device *this,
			       gpa_t addr, int len, void *data)
S
Sheng Yang 已提交
547
{
G
Gregory Haskins 已提交
548
	struct kvm_pit *pit = speaker_to_pit(this);
S
Sheng Yang 已提交
549 550 551 552
	struct kvm_kpit_state *pit_state = &pit->pit_state;
	struct kvm *kvm = pit->kvm;
	unsigned int refresh_clock;
	int ret;
553 554
	if (addr != KVM_SPEAKER_BASE_ADDRESS)
		return -EOPNOTSUPP;
S
Sheng Yang 已提交
555 556 557 558 559 560 561 562 563 564 565

	/* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
	refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;

	mutex_lock(&pit_state->lock);
	ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
		(pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
	if (len > sizeof(ret))
		len = sizeof(ret);
	memcpy(data, (char *)&ret, len);
	mutex_unlock(&pit_state->lock);
566
	return 0;
S
Sheng Yang 已提交
567 568
}

569
void kvm_pit_reset(struct kvm_pit *pit)
S
Sheng Yang 已提交
570 571
{
	int i;
572 573 574
	struct kvm_kpit_channel_state *c;

	mutex_lock(&pit->pit_state.lock);
B
Beth Kon 已提交
575
	pit->pit_state.flags = 0;
576 577 578 579 580 581 582 583 584
	for (i = 0; i < 3; i++) {
		c = &pit->pit_state.channels[i];
		c->mode = 0xff;
		c->gate = (i != 2);
		pit_load_count(pit->kvm, i, 0);
	}
	mutex_unlock(&pit->pit_state.lock);

	atomic_set(&pit->pit_state.pit_timer.pending, 0);
585
	pit->pit_state.irq_ack = 1;
586 587
}

588 589 590 591 592 593 594 595 596 597
static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
{
	struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);

	if (!mask) {
		atomic_set(&pit->pit_state.pit_timer.pending, 0);
		pit->pit_state.irq_ack = 1;
	}
}

G
Gregory Haskins 已提交
598 599 600 601 602 603 604 605 606 607
static const struct kvm_io_device_ops pit_dev_ops = {
	.read     = pit_ioport_read,
	.write    = pit_ioport_write,
};

static const struct kvm_io_device_ops speaker_dev_ops = {
	.read     = speaker_ioport_read,
	.write    = speaker_ioport_write,
};

608
/* Caller must have writers lock on slots_lock */
609
struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
610
{
S
Sheng Yang 已提交
611 612
	struct kvm_pit *pit;
	struct kvm_kpit_state *pit_state;
613
	int ret;
S
Sheng Yang 已提交
614 615 616 617 618

	pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
	if (!pit)
		return NULL;

619
	pit->irq_source_id = kvm_request_irq_source_id(kvm);
620 621
	if (pit->irq_source_id < 0) {
		kfree(pit);
622
		return NULL;
623
	}
624

S
Sheng Yang 已提交
625 626
	mutex_init(&pit->pit_state.lock);
	mutex_lock(&pit->pit_state.lock);
627
	spin_lock_init(&pit->pit_state.inject_lock);
S
Sheng Yang 已提交
628 629 630 631 632 633 634 635

	kvm->arch.vpit = pit;
	pit->kvm = kvm;

	pit_state = &pit->pit_state;
	pit_state->pit = pit;
	hrtimer_init(&pit_state->pit_timer.timer,
		     CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
636 637 638
	pit_state->irq_ack_notifier.gsi = 0;
	pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
	kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
639
	pit_state->pit_timer.reinject = true;
S
Sheng Yang 已提交
640 641
	mutex_unlock(&pit->pit_state.lock);

642
	kvm_pit_reset(pit);
S
Sheng Yang 已提交
643

644 645 646
	pit->mask_notifier.func = pit_mask_notifer;
	kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);

647
	kvm_iodevice_init(&pit->dev, &pit_dev_ops);
M
Marcelo Tosatti 已提交
648
	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &pit->dev);
649 650
	if (ret < 0)
		goto fail;
651 652 653

	if (flags & KVM_PIT_SPEAKER_DUMMY) {
		kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
M
Marcelo Tosatti 已提交
654
		ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
655 656 657
						&pit->speaker_dev);
		if (ret < 0)
			goto fail_unregister;
658 659
	}

S
Sheng Yang 已提交
660
	return pit;
661 662

fail_unregister:
M
Marcelo Tosatti 已提交
663
	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
664 665 666 667 668 669 670

fail:
	if (pit->irq_source_id >= 0)
		kvm_free_irq_source_id(kvm, pit->irq_source_id);

	kfree(pit);
	return NULL;
S
Sheng Yang 已提交
671 672 673 674 675 676 677
}

void kvm_free_pit(struct kvm *kvm)
{
	struct hrtimer *timer;

	if (kvm->arch.vpit) {
678 679
		kvm_unregister_irq_mask_notifier(kvm, 0,
					       &kvm->arch.vpit->mask_notifier);
680 681
		kvm_unregister_irq_ack_notifier(kvm,
				&kvm->arch.vpit->pit_state.irq_ack_notifier);
S
Sheng Yang 已提交
682 683 684
		mutex_lock(&kvm->arch.vpit->pit_state.lock);
		timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
		hrtimer_cancel(timer);
685
		kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
S
Sheng Yang 已提交
686 687 688 689 690
		mutex_unlock(&kvm->arch.vpit->pit_state.lock);
		kfree(kvm->arch.vpit);
	}
}

691
static void __inject_pit_timer_intr(struct kvm *kvm)
S
Sheng Yang 已提交
692
{
693 694 695
	struct kvm_vcpu *vcpu;
	int i;

696 697
	kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
	kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
698 699

	/*
700 701 702 703 704 705 706
	 * Provides NMI watchdog support via Virtual Wire mode.
	 * The route is: PIT -> PIC -> LVT0 in NMI mode.
	 *
	 * Note: Our Virtual Wire implementation is simplified, only
	 * propagating PIT interrupts to all VCPUs when they have set
	 * LVT0 to NMI delivery. Other PIC interrupts are just sent to
	 * VCPU0, and only if its LVT0 is in EXTINT mode.
707
	 */
708
	if (kvm->arch.vapics_in_nmi_mode > 0)
709 710
		kvm_for_each_vcpu(i, vcpu, kvm)
			kvm_apic_nmi_wd_deliver(vcpu);
S
Sheng Yang 已提交
711 712 713 714 715 716 717 718
}

void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
{
	struct kvm_pit *pit = vcpu->kvm->arch.vpit;
	struct kvm *kvm = vcpu->kvm;
	struct kvm_kpit_state *ps;

719
	if (pit) {
720
		int inject = 0;
S
Sheng Yang 已提交
721 722
		ps = &pit->pit_state;

723 724 725 726 727 728 729
		/* Try to inject pending interrupts when
		 * last one has been acked.
		 */
		spin_lock(&ps->inject_lock);
		if (atomic_read(&ps->pit_timer.pending) && ps->irq_ack) {
			ps->irq_ack = 0;
			inject = 1;
S
Sheng Yang 已提交
730
		}
731 732 733
		spin_unlock(&ps->inject_lock);
		if (inject)
			__inject_pit_timer_intr(kvm);
S
Sheng Yang 已提交
734 735
	}
}