33 #define pr_fmt(fmt) "pit: " fmt
36 #include <linux/slab.h>
42 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
44 #define mod_64(x, y) ((x) % (y))
47 #define RW_STATE_LSB 1
48 #define RW_STATE_MSB 2
49 #define RW_STATE_WORD0 3
50 #define RW_STATE_WORD1 4
67 res.l.high = div64_u64(rh, c);
68 res.l.low = div64_u64(((
mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
77 WARN_ON(!mutex_is_locked(&kvm->
arch.vpit->pit_state.lock));
98 static int pit_get_gate(
struct kvm *kvm,
int channel)
100 WARN_ON(!mutex_is_locked(&kvm->
arch.vpit->pit_state.lock));
102 return kvm->
arch.vpit->pit_state.channels[
channel].gate;
105 static s64 __kpit_elapsed(
struct kvm *kvm)
124 elapsed = ps->
period - ktime_to_ns(remaining);
134 return __kpit_elapsed(kvm);
139 static int pit_get_count(
struct kvm *kvm,
int channel)
146 WARN_ON(!mutex_is_locked(&kvm->
arch.vpit->pit_state.lock));
148 t = kpit_elapsed(kvm, c, channel);
156 counter = (c->
count -
d) & 0xffff;
169 static int pit_get_out(
struct kvm *kvm,
int channel)
176 WARN_ON(!mutex_is_locked(&kvm->
arch.vpit->pit_state.lock));
178 t = kpit_elapsed(kvm, c, channel);
184 out = (d >= c->
count);
187 out = (d < c->
count);
197 out = (d == c->
count);
204 static void pit_latch_count(
struct kvm *kvm,
int channel)
209 WARN_ON(!mutex_is_locked(&kvm->
arch.vpit->pit_state.lock));
217 static void pit_latch_status(
struct kvm *kvm,
int channel)
222 WARN_ON(!mutex_is_locked(&kvm->
arch.vpit->pit_state.lock));
226 c->
status = ((pit_get_out(kvm, channel) << 7) |
261 if (!kvm_vcpu_is_bsp(vcpu) || !pit)
269 static void destroy_pit_timer(
struct kvm_pit *pit)
278 struct kvm *kvm = pit->
kvm;
306 if (kvm->
arch.vapics_in_nmi_mode > 0)
329 static void create_pit_timer(
struct kvm *kvm,
u32 val,
int is_period)
339 pr_debug(
"create pit timer, interval is %llu nsec\n", interval);
347 ps->
timer.function = pit_timer_fn;
357 static void pit_load_count(
struct kvm *kvm,
int channel,
u32 val)
363 pr_debug(
"load_count val is %d, channel is %d\n", val, channel);
386 create_pit_timer(kvm, val, 0);
390 create_pit_timer(kvm, val, 1);
393 destroy_pit_timer(kvm->
arch.vpit);
400 if (hpet_legacy_start) {
402 saved_mode = kvm->
arch.vpit->pit_state.channels[0].mode;
403 kvm->
arch.vpit->pit_state.channels[0].mode = 0xff;
404 pit_load_count(kvm, channel, val);
405 kvm->
arch.vpit->pit_state.channels[0].mode = saved_mode;
407 pit_load_count(kvm, channel, val);
421 static inline int pit_in_range(
gpa_t addr)
428 gpa_t addr,
int len,
const void *data)
430 struct kvm_pit *pit = dev_to_pit(
this);
432 struct kvm *kvm = pit->
kvm;
436 if (!pit_in_range(addr))
445 pr_debug(
"write addr is 0x%x, len is %d, val is 0x%x\n",
446 (
unsigned int)addr, len, val);
452 for (channel = 0; channel < 3; channel++) {
454 if (val & (2 << channel)) {
456 pit_latch_count(kvm, channel);
458 pit_latch_status(kvm, channel);
466 pit_latch_count(kvm, channel);
471 s->
mode = (val >> 1) & 7;
483 pit_load_count(kvm, addr, val);
486 pit_load_count(kvm, addr, val << 8);
493 pit_load_count(kvm, addr, s->
write_latch | (val << 8));
504 gpa_t addr,
int len,
void *data)
506 struct kvm_pit *pit = dev_to_pit(
this);
508 struct kvm *kvm = pit->
kvm;
511 if (!pit_in_range(addr))
545 count = pit_get_count(kvm, addr);
549 count = pit_get_count(kvm, addr);
550 ret = (count >> 8) & 0xff;
553 count = pit_get_count(kvm, addr);
558 count = pit_get_count(kvm, addr);
559 ret = (count >> 8) & 0xff;
565 if (len >
sizeof(ret))
567 memcpy(data, (
char *)&ret, len);
574 gpa_t addr,
int len,
const void *data)
576 struct kvm_pit *pit = speaker_to_pit(
this);
578 struct kvm *kvm = pit->
kvm;
585 pit_set_gate(kvm, 2, val & 1);
591 gpa_t addr,
int len,
void *data)
593 struct kvm_pit *pit = speaker_to_pit(
this);
595 struct kvm *kvm = pit->
kvm;
596 unsigned int refresh_clock;
602 refresh_clock = ((
unsigned int)ktime_to_ns(
ktime_get()) >> 14) & 1;
606 (pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
607 if (len >
sizeof(ret))
609 memcpy(data, (
char *)&ret, len);
621 for (i = 0; i < 3; i++) {
625 pit_load_count(pit->
kvm, i, 0);
644 .read = pit_ioport_read,
645 .write = pit_ioport_write,
649 .read = speaker_ioport_read,
650 .write = speaker_ioport_write,
676 pid = get_pid(task_tgid(
current));
682 "kvm-pit/%d", pid_nr);
691 kvm->
arch.vpit = pit;
695 pit_state->
pit = pit;
708 kvm_iodevice_init(&pit->
dev, &pit_dev_ops);
715 kvm_iodevice_init(&pit->
speaker_dev, &speaker_dev_ops);
720 goto fail_unregister;
741 if (kvm->
arch.vpit) {
744 &kvm->
arch.vpit->speaker_dev);
746 &kvm->
arch.vpit->mask_notifier);
748 &kvm->
arch.vpit->pit_state.irq_ack_notifier);
750 timer = &kvm->
arch.vpit->pit_state.timer;