i8254.c 18.6 KB
Newer Older
S
Sheng Yang 已提交
1 2 3 4 5 6 7
/*
 * 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
N
Nicolas Kaiser 已提交
8
 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
S
Sheng Yang 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * 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 已提交
33 34
#define pr_fmt(fmt) "pit: " fmt

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

38
#include "ioapic.h"
S
Sheng Yang 已提交
39 40
#include "irq.h"
#include "i8254.h"
41
#include "x86.h"
S
Sheng Yang 已提交
42 43

#ifndef CONFIG_X86_64
R
Roman Zippel 已提交
44
#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
S
Sheng Yang 已提交
45 46 47 48 49 50 51 52 53
#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

54
static void pit_set_gate(struct kvm_pit *pit, int channel, u32 val)
S
Sheng Yang 已提交
55
{
56
	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
S
Sheng Yang 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

	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;
}

77
static int pit_get_gate(struct kvm_pit *pit, int channel)
S
Sheng Yang 已提交
78
{
79
	return pit->pit_state.channels[channel].gate;
S
Sheng Yang 已提交
80 81
}

82
static s64 __kpit_elapsed(struct kvm_pit *pit)
83 84 85
{
	s64 elapsed;
	ktime_t remaining;
86
	struct kvm_kpit_state *ps = &pit->pit_state;
87

88
	if (!ps->period)
89 90
		return 0;

91 92 93 94 95 96 97 98 99
	/*
	 * 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.
	 */
100 101
	remaining = hrtimer_get_remaining(&ps->timer);
	elapsed = ps->period - ktime_to_ns(remaining);
102 103 104 105

	return elapsed;
}

106
static s64 kpit_elapsed(struct kvm_pit *pit, struct kvm_kpit_channel_state *c,
107 108 109
			int channel)
{
	if (channel == 0)
110
		return __kpit_elapsed(pit);
111 112 113 114

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

115
static int pit_get_count(struct kvm_pit *pit, int channel)
S
Sheng Yang 已提交
116
{
117
	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
S
Sheng Yang 已提交
118 119 120
	s64 d, t;
	int counter;

121
	t = kpit_elapsed(pit, c, channel);
122
	d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC);
S
Sheng Yang 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

	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;
}

142
static int pit_get_out(struct kvm_pit *pit, int channel)
S
Sheng Yang 已提交
143
{
144
	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
S
Sheng Yang 已提交
145 146 147
	s64 d, t;
	int out;

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

	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;
}

174
static void pit_latch_count(struct kvm_pit *pit, int channel)
S
Sheng Yang 已提交
175
{
176
	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
S
Sheng Yang 已提交
177 178

	if (!c->count_latched) {
179
		c->latched_count = pit_get_count(pit, channel);
S
Sheng Yang 已提交
180 181 182 183
		c->count_latched = c->rw_mode;
	}
}

184
static void pit_latch_status(struct kvm_pit *pit, int channel)
S
Sheng Yang 已提交
185
{
186
	struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
S
Sheng Yang 已提交
187 188 189

	if (!c->status_latched) {
		/* TODO: Return NULL COUNT (bit 6). */
190
		c->status = ((pit_get_out(pit, channel) << 7) |
S
Sheng Yang 已提交
191 192 193 194 195 196 197
				(c->rw_mode << 4) |
				(c->mode << 1) |
				c->bcd);
		c->status_latched = 1;
	}
}

198 199 200 201 202
static inline struct kvm_pit *pit_state_to_pit(struct kvm_kpit_state *ps)
{
	return container_of(ps, struct kvm_pit, pit_state);
}

203
static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
204 205 206
{
	struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
						 irq_ack_notifier);
207
	struct kvm_pit *pit = pit_state_to_pit(ps);
208

209 210 211 212 213
	atomic_set(&ps->irq_ack, 1);
	/* irq_ack should be set before pending is read.  Order accesses with
	 * inc(pending) in pit_timer_fn and xchg(irq_ack, 0) in pit_do_work.
	 */
	smp_mb();
214
	if (atomic_dec_if_positive(&ps->pending) > 0)
215
		kthread_queue_work(pit->worker, &pit->expired);
216 217
}

M
Marcelo Tosatti 已提交
218 219 220 221 222
void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
{
	struct kvm_pit *pit = vcpu->kvm->arch.vpit;
	struct hrtimer *timer;

223
	if (!kvm_vcpu_is_bsp(vcpu) || !pit)
M
Marcelo Tosatti 已提交
224 225
		return;

226
	timer = &pit->pit_state.timer;
227
	mutex_lock(&pit->pit_state.lock);
M
Marcelo Tosatti 已提交
228
	if (hrtimer_cancel(timer))
229
		hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
230
	mutex_unlock(&pit->pit_state.lock);
M
Marcelo Tosatti 已提交
231 232
}

233
static void destroy_pit_timer(struct kvm_pit *pit)
S
Sheng Yang 已提交
234
{
235
	hrtimer_cancel(&pit->pit_state.timer);
P
Petr Mladek 已提交
236
	kthread_flush_work(&pit->expired);
S
Sheng Yang 已提交
237 238
}

239
static void pit_do_work(struct kthread_work *work)
240 241 242 243 244 245 246
{
	struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
	struct kvm *kvm = pit->kvm;
	struct kvm_vcpu *vcpu;
	int i;
	struct kvm_kpit_state *ps = &pit->pit_state;

247
	if (atomic_read(&ps->reinject) && !atomic_xchg(&ps->irq_ack, 0))
248 249
		return;

250 251
	kvm_set_irq(kvm, pit->irq_source_id, 0, 1, false);
	kvm_set_irq(kvm, pit->irq_source_id, 0, 0, false);
252 253 254 255 256 257 258 259 260

	/*
	 * Provides NMI watchdog support via Virtual Wire mode.
	 * The route is: PIT -> LVT0 in NMI mode.
	 *
	 * Note: Our Virtual Wire implementation does not follow
	 * the MP specification.  We propagate a PIT interrupt to all
	 * VCPUs and only when LVT0 is in NMI mode.  The interrupt can
	 * also be simultaneously delivered through PIC and IOAPIC.
261
	 */
262 263 264
	if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0)
		kvm_for_each_vcpu(i, vcpu, kvm)
			kvm_apic_nmi_wd_deliver(vcpu);
265 266 267 268
}

static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
{
269
	struct kvm_kpit_state *ps = container_of(data, struct kvm_kpit_state, timer);
270
	struct kvm_pit *pt = pit_state_to_pit(ps);
271

272
	if (atomic_read(&ps->reinject))
273
		atomic_inc(&ps->pending);
274

275
	kthread_queue_work(pt->worker, &pt->expired);
276

277 278
	if (ps->is_periodic) {
		hrtimer_add_expires_ns(&ps->timer, ps->period);
279 280 281 282 283
		return HRTIMER_RESTART;
	} else
		return HRTIMER_NORESTART;
}

284 285 286
static inline void kvm_pit_reset_reinject(struct kvm_pit *pit)
{
	atomic_set(&pit->pit_state.pending, 0);
287
	atomic_set(&pit->pit_state.irq_ack, 1);
288 289
}

290 291 292 293 294
void kvm_pit_set_reinject(struct kvm_pit *pit, bool reinject)
{
	struct kvm_kpit_state *ps = &pit->pit_state;
	struct kvm *kvm = pit->kvm;

295
	if (atomic_read(&ps->reinject) == reinject)
296 297
		return;

298 299 300 301 302 303 304 305
	/*
	 * AMD SVM AVIC accelerates EOI write and does not trap.
	 * This cause in-kernel PIT re-inject mode to fail
	 * since it checks ps->irq_ack before kvm_set_irq()
	 * and relies on the ack notifier to timely queue
	 * the pt->worker work iterm and reinject the missed tick.
	 * So, deactivate APICv when PIT is in reinject mode.
	 */
306
	if (reinject) {
307 308
		kvm_request_apicv_update(kvm, false,
					 APICV_INHIBIT_REASON_PIT_REINJ);
309 310 311 312 313
		/* The initial state is preserved while ps->reinject == 0. */
		kvm_pit_reset_reinject(pit);
		kvm_register_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
		kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
	} else {
314 315
		kvm_request_apicv_update(kvm, true,
					 APICV_INHIBIT_REASON_PIT_REINJ);
316 317 318 319
		kvm_unregister_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
		kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
	}

320
	atomic_set(&ps->reinject, reinject);
321 322
}

323
static void create_pit_timer(struct kvm_pit *pit, u32 val, int is_period)
S
Sheng Yang 已提交
324
{
325 326
	struct kvm_kpit_state *ps = &pit->pit_state;
	struct kvm *kvm = pit->kvm;
S
Sheng Yang 已提交
327 328
	s64 interval;

329 330
	if (!ioapic_in_kernel(kvm) ||
	    ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)
331 332
		return;

333
	interval = mul_u64_u32_div(val, NSEC_PER_SEC, KVM_PIT_FREQ);
S
Sheng Yang 已提交
334

J
Joe Perches 已提交
335
	pr_debug("create pit timer, interval is %llu nsec\n", interval);
S
Sheng Yang 已提交
336 337

	/* TODO The new value only affected after the retriggered */
338
	hrtimer_cancel(&ps->timer);
P
Petr Mladek 已提交
339
	kthread_flush_work(&pit->expired);
340
	ps->period = interval;
341 342
	ps->is_periodic = is_period;

343
	kvm_pit_reset_reinject(pit);
S
Sheng Yang 已提交
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
	/*
	 * Do not allow the guest to program periodic timers with small
	 * interval, since the hrtimers are not throttled by the host
	 * scheduler.
	 */
	if (ps->is_periodic) {
		s64 min_period = min_timer_period_us * 1000LL;

		if (ps->period < min_period) {
			pr_info_ratelimited(
			    "kvm: requested %lld ns "
			    "i8254 timer period limited to %lld ns\n",
			    ps->period, min_period);
			ps->period = min_period;
		}
	}

362
	hrtimer_start(&ps->timer, ktime_add_ns(ktime_get(), interval),
S
Sheng Yang 已提交
363 364 365
		      HRTIMER_MODE_ABS);
}

366
static void pit_load_count(struct kvm_pit *pit, int channel, u32 val)
S
Sheng Yang 已提交
367
{
368
	struct kvm_kpit_state *ps = &pit->pit_state;
S
Sheng Yang 已提交
369

J
Joe Perches 已提交
370
	pr_debug("load_count val is %d, channel is %d\n", val, channel);
S
Sheng Yang 已提交
371 372

	/*
373 374
	 * The largest possible initial count is 0; this is equivalent
	 * to 216 for binary counting and 104 for BCD counting.
S
Sheng Yang 已提交
375 376 377 378 379 380
	 */
	if (val == 0)
		val = 0x10000;

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

381 382
	if (channel != 0) {
		ps->channels[channel].count_load_time = ktime_get();
S
Sheng Yang 已提交
383
		return;
384
	}
S
Sheng Yang 已提交
385 386 387 388

	/* Two types of timer
	 * mode 1 is one shot, mode 2 is period, otherwise del timer */
	switch (ps->channels[0].mode) {
389
	case 0:
S
Sheng Yang 已提交
390
	case 1:
M
Marcelo Tosatti 已提交
391 392
        /* FIXME: enhance mode 4 precision */
	case 4:
393
		create_pit_timer(pit, val, 0);
S
Sheng Yang 已提交
394 395
		break;
	case 2:
A
Aurelien Jarno 已提交
396
	case 3:
397
		create_pit_timer(pit, val, 1);
S
Sheng Yang 已提交
398 399
		break;
	default:
400
		destroy_pit_timer(pit);
S
Sheng Yang 已提交
401 402 403
	}
}

404 405
void kvm_pit_load_count(struct kvm_pit *pit, int channel, u32 val,
		int hpet_legacy_start)
406
{
B
Beth Kon 已提交
407
	u8 saved_mode;
408

409
	WARN_ON_ONCE(!mutex_is_locked(&pit->pit_state.lock));
410

B
Beth Kon 已提交
411 412
	if (hpet_legacy_start) {
		/* save existing mode for later reenablement */
413
		WARN_ON(channel != 0);
414 415 416 417
		saved_mode = pit->pit_state.channels[0].mode;
		pit->pit_state.channels[0].mode = 0xff; /* disable timer */
		pit_load_count(pit, channel, val);
		pit->pit_state.channels[0].mode = saved_mode;
B
Beth Kon 已提交
418
	} else {
419
		pit_load_count(pit, channel, val);
B
Beth Kon 已提交
420
	}
421 422
}

G
Gregory Haskins 已提交
423 424 425 426 427 428 429 430 431 432
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);
}

433 434 435 436 437 438
static inline int pit_in_range(gpa_t addr)
{
	return ((addr >= KVM_PIT_BASE_ADDRESS) &&
		(addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
}

439 440
static int pit_ioport_write(struct kvm_vcpu *vcpu,
				struct kvm_io_device *this,
441
			    gpa_t addr, int len, const void *data)
S
Sheng Yang 已提交
442
{
G
Gregory Haskins 已提交
443
	struct kvm_pit *pit = dev_to_pit(this);
S
Sheng Yang 已提交
444 445 446 447
	struct kvm_kpit_state *pit_state = &pit->pit_state;
	int channel, access;
	struct kvm_kpit_channel_state *s;
	u32 val = *(u32 *) data;
448 449
	if (!pit_in_range(addr))
		return -EOPNOTSUPP;
S
Sheng Yang 已提交
450 451 452 453 454 455 456

	val  &= 0xff;
	addr &= KVM_PIT_CHANNEL_MASK;

	mutex_lock(&pit_state->lock);

	if (val != 0)
J
Joe Perches 已提交
457 458
		pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
			 (unsigned int)addr, len, val);
S
Sheng Yang 已提交
459 460 461 462 463 464 465 466 467

	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))
468
						pit_latch_count(pit, channel);
S
Sheng Yang 已提交
469
					if (!(val & 0x10))
470
						pit_latch_status(pit, channel);
S
Sheng Yang 已提交
471 472 473 474 475 476 477
				}
			}
		} else {
			/* Select Counter <channel>. */
			s = &pit_state->channels[channel];
			access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
			if (access == 0) {
478
				pit_latch_count(pit, channel);
S
Sheng Yang 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
			} 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:
495
			pit_load_count(pit, addr, val);
S
Sheng Yang 已提交
496 497
			break;
		case RW_STATE_MSB:
498
			pit_load_count(pit, addr, val << 8);
S
Sheng Yang 已提交
499 500 501 502 503 504
			break;
		case RW_STATE_WORD0:
			s->write_latch = val;
			s->write_state = RW_STATE_WORD1;
			break;
		case RW_STATE_WORD1:
505
			pit_load_count(pit, addr, s->write_latch | (val << 8));
S
Sheng Yang 已提交
506 507 508 509 510 511
			s->write_state = RW_STATE_WORD0;
			break;
		}
	}

	mutex_unlock(&pit_state->lock);
512
	return 0;
S
Sheng Yang 已提交
513 514
}

515 516
static int pit_ioport_read(struct kvm_vcpu *vcpu,
			   struct kvm_io_device *this,
517
			   gpa_t addr, int len, void *data)
S
Sheng Yang 已提交
518
{
G
Gregory Haskins 已提交
519
	struct kvm_pit *pit = dev_to_pit(this);
S
Sheng Yang 已提交
520 521 522
	struct kvm_kpit_state *pit_state = &pit->pit_state;
	int ret, count;
	struct kvm_kpit_channel_state *s;
523 524
	if (!pit_in_range(addr))
		return -EOPNOTSUPP;
S
Sheng Yang 已提交
525 526

	addr &= KVM_PIT_CHANNEL_MASK;
527 528 529
	if (addr == 3)
		return 0;

S
Sheng Yang 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
	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:
557
			count = pit_get_count(pit, addr);
S
Sheng Yang 已提交
558 559 560
			ret = count & 0xff;
			break;
		case RW_STATE_MSB:
561
			count = pit_get_count(pit, addr);
S
Sheng Yang 已提交
562 563 564
			ret = (count >> 8) & 0xff;
			break;
		case RW_STATE_WORD0:
565
			count = pit_get_count(pit, addr);
S
Sheng Yang 已提交
566 567 568 569
			ret = count & 0xff;
			s->read_state = RW_STATE_WORD1;
			break;
		case RW_STATE_WORD1:
570
			count = pit_get_count(pit, addr);
S
Sheng Yang 已提交
571 572 573 574 575 576 577 578 579 580 581
			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);
582
	return 0;
S
Sheng Yang 已提交
583 584
}

585 586
static int speaker_ioport_write(struct kvm_vcpu *vcpu,
				struct kvm_io_device *this,
587
				gpa_t addr, int len, const void *data)
S
Sheng Yang 已提交
588
{
G
Gregory Haskins 已提交
589
	struct kvm_pit *pit = speaker_to_pit(this);
S
Sheng Yang 已提交
590 591
	struct kvm_kpit_state *pit_state = &pit->pit_state;
	u32 val = *(u32 *) data;
592 593
	if (addr != KVM_SPEAKER_BASE_ADDRESS)
		return -EOPNOTSUPP;
S
Sheng Yang 已提交
594 595 596

	mutex_lock(&pit_state->lock);
	pit_state->speaker_data_on = (val >> 1) & 1;
597
	pit_set_gate(pit, 2, val & 1);
S
Sheng Yang 已提交
598
	mutex_unlock(&pit_state->lock);
599
	return 0;
S
Sheng Yang 已提交
600 601
}

602 603 604
static int speaker_ioport_read(struct kvm_vcpu *vcpu,
				   struct kvm_io_device *this,
				   gpa_t addr, int len, void *data)
S
Sheng Yang 已提交
605
{
G
Gregory Haskins 已提交
606
	struct kvm_pit *pit = speaker_to_pit(this);
S
Sheng Yang 已提交
607 608 609
	struct kvm_kpit_state *pit_state = &pit->pit_state;
	unsigned int refresh_clock;
	int ret;
610 611
	if (addr != KVM_SPEAKER_BASE_ADDRESS)
		return -EOPNOTSUPP;
S
Sheng Yang 已提交
612 613 614 615 616

	/* 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);
617 618
	ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(pit, 2) |
		(pit_get_out(pit, 2) << 5) | (refresh_clock << 4));
S
Sheng Yang 已提交
619 620 621 622
	if (len > sizeof(ret))
		len = sizeof(ret);
	memcpy(data, (char *)&ret, len);
	mutex_unlock(&pit_state->lock);
623
	return 0;
S
Sheng Yang 已提交
624 625
}

626
static void kvm_pit_reset(struct kvm_pit *pit)
S
Sheng Yang 已提交
627 628
{
	int i;
629 630
	struct kvm_kpit_channel_state *c;

B
Beth Kon 已提交
631
	pit->pit_state.flags = 0;
632 633 634 635
	for (i = 0; i < 3; i++) {
		c = &pit->pit_state.channels[i];
		c->mode = 0xff;
		c->gate = (i != 2);
636
		pit_load_count(pit, i, 0);
637 638
	}

639
	kvm_pit_reset_reinject(pit);
640 641
}

642 643 644 645
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);

646 647
	if (!mask)
		kvm_pit_reset_reinject(pit);
648 649
}

G
Gregory Haskins 已提交
650 651 652 653 654 655 656 657 658 659
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,
};

660
struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
661
{
S
Sheng Yang 已提交
662 663
	struct kvm_pit *pit;
	struct kvm_kpit_state *pit_state;
664 665
	struct pid *pid;
	pid_t pid_nr;
666
	int ret;
S
Sheng Yang 已提交
667

668
	pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL_ACCOUNT);
S
Sheng Yang 已提交
669 670 671
	if (!pit)
		return NULL;

672
	pit->irq_source_id = kvm_request_irq_source_id(kvm);
673 674
	if (pit->irq_source_id < 0)
		goto fail_request;
675

S
Sheng Yang 已提交
676
	mutex_init(&pit->pit_state.lock);
677

678 679 680 681
	pid = get_pid(task_tgid(current));
	pid_nr = pid_vnr(pid);
	put_pid(pid);

682 683
	pit->worker = kthread_create_worker(0, "kvm-pit/%d", pid_nr);
	if (IS_ERR(pit->worker))
684 685
		goto fail_kthread;

P
Petr Mladek 已提交
686
	kthread_init_work(&pit->expired, pit_do_work);
S
Sheng Yang 已提交
687 688 689 690

	pit->kvm = kvm;

	pit_state = &pit->pit_state;
691
	hrtimer_init(&pit_state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
692
	pit_state->timer.function = pit_timer_fn;
693

694 695
	pit_state->irq_ack_notifier.gsi = 0;
	pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
696
	pit->mask_notifier.func = pit_mask_notifer;
S
Sheng Yang 已提交
697

698
	kvm_pit_reset(pit);
S
Sheng Yang 已提交
699

700
	kvm_pit_set_reinject(pit, true);
701

702
	mutex_lock(&kvm->slots_lock);
703
	kvm_iodevice_init(&pit->dev, &pit_dev_ops);
704 705
	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS,
				      KVM_PIT_MEM_LENGTH, &pit->dev);
706
	if (ret < 0)
707
		goto fail_register_pit;
708 709 710

	if (flags & KVM_PIT_SPEAKER_DUMMY) {
		kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
M
Marcelo Tosatti 已提交
711
		ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
712 713
					      KVM_SPEAKER_BASE_ADDRESS, 4,
					      &pit->speaker_dev);
714
		if (ret < 0)
715
			goto fail_register_speaker;
716
	}
717
	mutex_unlock(&kvm->slots_lock);
718

S
Sheng Yang 已提交
719
	return pit;
720

721
fail_register_speaker:
M
Marcelo Tosatti 已提交
722
	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
723
fail_register_pit:
724
	mutex_unlock(&kvm->slots_lock);
725
	kvm_pit_set_reinject(pit, false);
726
	kthread_destroy_worker(pit->worker);
727 728 729
fail_kthread:
	kvm_free_irq_source_id(kvm, pit->irq_source_id);
fail_request:
730 731
	kfree(pit);
	return NULL;
S
Sheng Yang 已提交
732 733 734 735
}

void kvm_free_pit(struct kvm *kvm)
{
736 737 738
	struct kvm_pit *pit = kvm->arch.vpit;

	if (pit) {
739
		mutex_lock(&kvm->slots_lock);
740 741
		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->speaker_dev);
742
		mutex_unlock(&kvm->slots_lock);
743 744
		kvm_pit_set_reinject(pit, false);
		hrtimer_cancel(&pit->pit_state.timer);
745
		kthread_destroy_worker(pit->worker);
746 747
		kvm_free_irq_source_id(kvm, pit->irq_source_id);
		kfree(pit);
S
Sheng Yang 已提交
748 749
	}
}