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
89611c187SNicolas Kaiser * Copyright 2009 Red Hat, Inc. and/or its affiliates.
97837699fSSheng Yang *
107837699fSSheng Yang * Permission is hereby granted, free of charge, to any person obtaining a copy
117837699fSSheng Yang * of this software and associated documentation files (the "Software"), to deal
127837699fSSheng Yang * in the Software without restriction, including without limitation the rights
137837699fSSheng Yang * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
147837699fSSheng Yang * copies of the Software, and to permit persons to whom the Software is
157837699fSSheng Yang * furnished to do so, subject to the following conditions:
167837699fSSheng Yang *
177837699fSSheng Yang * The above copyright notice and this permission notice shall be included in
187837699fSSheng Yang * all copies or substantial portions of the Software.
197837699fSSheng Yang *
207837699fSSheng Yang * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
217837699fSSheng Yang * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
227837699fSSheng Yang * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
237837699fSSheng Yang * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
247837699fSSheng Yang * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
257837699fSSheng Yang * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
267837699fSSheng Yang * THE SOFTWARE.
277837699fSSheng Yang *
287837699fSSheng Yang * Authors:
297837699fSSheng Yang * Sheng Yang <sheng.yang@intel.com>
307837699fSSheng Yang * Based on QEMU and Xen.
317837699fSSheng Yang */
327837699fSSheng Yang
33*8d20bd63SSean Christopherson #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34a78d9626SJoe Perches
357837699fSSheng Yang #include <linux/kvm_host.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
377837699fSSheng Yang
3849df6397SSteve Rutherford #include "ioapic.h"
397837699fSSheng Yang #include "irq.h"
407837699fSSheng Yang #include "i8254.h"
419ed96e87SMarcelo Tosatti #include "x86.h"
427837699fSSheng Yang
437837699fSSheng Yang #ifndef CONFIG_X86_64
446f6d6a1aSRoman Zippel #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
457837699fSSheng Yang #else
467837699fSSheng Yang #define mod_64(x, y) ((x) % (y))
477837699fSSheng Yang #endif
487837699fSSheng Yang
497837699fSSheng Yang #define RW_STATE_LSB 1
507837699fSSheng Yang #define RW_STATE_MSB 2
517837699fSSheng Yang #define RW_STATE_WORD0 3
527837699fSSheng Yang #define RW_STATE_WORD1 4
537837699fSSheng Yang
pit_set_gate(struct kvm_pit * pit,int channel,u32 val)5409edea72SRadim Krčmář static void pit_set_gate(struct kvm_pit *pit, int channel, u32 val)
557837699fSSheng Yang {
5609edea72SRadim Krčmář struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
577837699fSSheng Yang
587837699fSSheng Yang switch (c->mode) {
597837699fSSheng Yang default:
607837699fSSheng Yang case 0:
617837699fSSheng Yang case 4:
627837699fSSheng Yang /* XXX: just disable/enable counting */
637837699fSSheng Yang break;
647837699fSSheng Yang case 1:
657837699fSSheng Yang case 2:
667837699fSSheng Yang case 3:
677837699fSSheng Yang case 5:
687837699fSSheng Yang /* Restart counting on rising edge. */
697837699fSSheng Yang if (c->gate < val)
707837699fSSheng Yang c->count_load_time = ktime_get();
717837699fSSheng Yang break;
727837699fSSheng Yang }
737837699fSSheng Yang
747837699fSSheng Yang c->gate = val;
757837699fSSheng Yang }
767837699fSSheng Yang
pit_get_gate(struct kvm_pit * pit,int channel)7709edea72SRadim Krčmář static int pit_get_gate(struct kvm_pit *pit, int channel)
787837699fSSheng Yang {
7909edea72SRadim Krčmář return pit->pit_state.channels[channel].gate;
807837699fSSheng Yang }
817837699fSSheng Yang
__kpit_elapsed(struct kvm_pit * pit)8209edea72SRadim Krčmář static s64 __kpit_elapsed(struct kvm_pit *pit)
83fd668423SMarcelo Tosatti {
84fd668423SMarcelo Tosatti s64 elapsed;
85fd668423SMarcelo Tosatti ktime_t remaining;
8609edea72SRadim Krčmář struct kvm_kpit_state *ps = &pit->pit_state;
87fd668423SMarcelo Tosatti
8826ef1924SAvi Kivity if (!ps->period)
890ff77873SMarcelo Tosatti return 0;
900ff77873SMarcelo Tosatti
91ede2ccc5SMarcelo Tosatti /*
92ede2ccc5SMarcelo Tosatti * The Counter does not stop when it reaches zero. In
93ede2ccc5SMarcelo Tosatti * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
94ede2ccc5SMarcelo Tosatti * the highest count, either FFFF hex for binary counting
95ede2ccc5SMarcelo Tosatti * or 9999 for BCD counting, and continues counting.
96ede2ccc5SMarcelo Tosatti * Modes 2 and 3 are periodic; the Counter reloads
97ede2ccc5SMarcelo Tosatti * itself with the initial count and continues counting
98ede2ccc5SMarcelo Tosatti * from there.
99ede2ccc5SMarcelo Tosatti */
10026ef1924SAvi Kivity remaining = hrtimer_get_remaining(&ps->timer);
10126ef1924SAvi Kivity elapsed = ps->period - ktime_to_ns(remaining);
102fd668423SMarcelo Tosatti
103fd668423SMarcelo Tosatti return elapsed;
104fd668423SMarcelo Tosatti }
105fd668423SMarcelo Tosatti
kpit_elapsed(struct kvm_pit * pit,struct kvm_kpit_channel_state * c,int channel)10609edea72SRadim Krčmář static s64 kpit_elapsed(struct kvm_pit *pit, struct kvm_kpit_channel_state *c,
107fd668423SMarcelo Tosatti int channel)
108fd668423SMarcelo Tosatti {
109fd668423SMarcelo Tosatti if (channel == 0)
11009edea72SRadim Krčmář return __kpit_elapsed(pit);
111fd668423SMarcelo Tosatti
112fd668423SMarcelo Tosatti return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
113fd668423SMarcelo Tosatti }
114fd668423SMarcelo Tosatti
pit_get_count(struct kvm_pit * pit,int channel)11509edea72SRadim Krčmář static int pit_get_count(struct kvm_pit *pit, int channel)
1167837699fSSheng Yang {
11709edea72SRadim Krčmář struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
1187837699fSSheng Yang s64 d, t;
1197837699fSSheng Yang int counter;
1207837699fSSheng Yang
12109edea72SRadim Krčmář t = kpit_elapsed(pit, c, channel);
1220e4d4415SPaolo Bonzini d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC);
1237837699fSSheng Yang
1247837699fSSheng Yang switch (c->mode) {
1257837699fSSheng Yang case 0:
1267837699fSSheng Yang case 1:
1277837699fSSheng Yang case 4:
1287837699fSSheng Yang case 5:
1297837699fSSheng Yang counter = (c->count - d) & 0xffff;
1307837699fSSheng Yang break;
1317837699fSSheng Yang case 3:
1327837699fSSheng Yang /* XXX: may be incorrect for odd counts */
1337837699fSSheng Yang counter = c->count - (mod_64((2 * d), c->count));
1347837699fSSheng Yang break;
1357837699fSSheng Yang default:
1367837699fSSheng Yang counter = c->count - mod_64(d, c->count);
1377837699fSSheng Yang break;
1387837699fSSheng Yang }
1397837699fSSheng Yang return counter;
1407837699fSSheng Yang }
1417837699fSSheng Yang
pit_get_out(struct kvm_pit * pit,int channel)14209edea72SRadim Krčmář static int pit_get_out(struct kvm_pit *pit, int channel)
1437837699fSSheng Yang {
14409edea72SRadim Krčmář struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
1457837699fSSheng Yang s64 d, t;
1467837699fSSheng Yang int out;
1477837699fSSheng Yang
14809edea72SRadim Krčmář t = kpit_elapsed(pit, c, channel);
1490e4d4415SPaolo Bonzini d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC);
1507837699fSSheng Yang
1517837699fSSheng Yang switch (c->mode) {
1527837699fSSheng Yang default:
1537837699fSSheng Yang case 0:
1547837699fSSheng Yang out = (d >= c->count);
1557837699fSSheng Yang break;
1567837699fSSheng Yang case 1:
1577837699fSSheng Yang out = (d < c->count);
1587837699fSSheng Yang break;
1597837699fSSheng Yang case 2:
1607837699fSSheng Yang out = ((mod_64(d, c->count) == 0) && (d != 0));
1617837699fSSheng Yang break;
1627837699fSSheng Yang case 3:
1637837699fSSheng Yang out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
1647837699fSSheng Yang break;
1657837699fSSheng Yang case 4:
1667837699fSSheng Yang case 5:
1677837699fSSheng Yang out = (d == c->count);
1687837699fSSheng Yang break;
1697837699fSSheng Yang }
1707837699fSSheng Yang
1717837699fSSheng Yang return out;
1727837699fSSheng Yang }
1737837699fSSheng Yang
pit_latch_count(struct kvm_pit * pit,int channel)17409edea72SRadim Krčmář static void pit_latch_count(struct kvm_pit *pit, int channel)
1757837699fSSheng Yang {
17609edea72SRadim Krčmář struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
1777837699fSSheng Yang
1787837699fSSheng Yang if (!c->count_latched) {
17909edea72SRadim Krčmář c->latched_count = pit_get_count(pit, channel);
1807837699fSSheng Yang c->count_latched = c->rw_mode;
1817837699fSSheng Yang }
1827837699fSSheng Yang }
1837837699fSSheng Yang
pit_latch_status(struct kvm_pit * pit,int channel)18409edea72SRadim Krčmář static void pit_latch_status(struct kvm_pit *pit, int channel)
1857837699fSSheng Yang {
18609edea72SRadim Krčmář struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
1877837699fSSheng Yang
1887837699fSSheng Yang if (!c->status_latched) {
1897837699fSSheng Yang /* TODO: Return NULL COUNT (bit 6). */
19009edea72SRadim Krčmář c->status = ((pit_get_out(pit, channel) << 7) |
1917837699fSSheng Yang (c->rw_mode << 4) |
1927837699fSSheng Yang (c->mode << 1) |
1937837699fSSheng Yang c->bcd);
1947837699fSSheng Yang c->status_latched = 1;
1957837699fSSheng Yang }
1967837699fSSheng Yang }
1977837699fSSheng Yang
pit_state_to_pit(struct kvm_kpit_state * ps)198a3e13115SRadim Krčmář static inline struct kvm_pit *pit_state_to_pit(struct kvm_kpit_state *ps)
199a3e13115SRadim Krčmář {
200a3e13115SRadim Krčmář return container_of(ps, struct kvm_pit, pit_state);
201a3e13115SRadim Krčmář }
202a3e13115SRadim Krčmář
kvm_pit_ack_irq(struct kvm_irq_ack_notifier * kian)203ee032c99SHarvey Harrison static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
2043cf57fedSMarcelo Tosatti {
2053cf57fedSMarcelo Tosatti struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
2063cf57fedSMarcelo Tosatti irq_ack_notifier);
207a3e13115SRadim Krčmář struct kvm_pit *pit = pit_state_to_pit(ps);
20833572ac0SChris Lalancette
209ddf54503SRadim Krčmář atomic_set(&ps->irq_ack, 1);
210ddf54503SRadim Krčmář /* irq_ack should be set before pending is read. Order accesses with
211ddf54503SRadim Krčmář * inc(pending) in pit_timer_fn and xchg(irq_ack, 0) in pit_do_work.
212ddf54503SRadim Krčmář */
213ddf54503SRadim Krčmář smp_mb();
21471474e2fSRadim Krčmář if (atomic_dec_if_positive(&ps->pending) > 0)
21536da91bdSPetr Mladek kthread_queue_work(pit->worker, &pit->expired);
2163cf57fedSMarcelo Tosatti }
2173cf57fedSMarcelo Tosatti
__kvm_migrate_pit_timer(struct kvm_vcpu * vcpu)2182f599714SMarcelo Tosatti void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
2192f599714SMarcelo Tosatti {
2202f599714SMarcelo Tosatti struct kvm_pit *pit = vcpu->kvm->arch.vpit;
2212f599714SMarcelo Tosatti struct hrtimer *timer;
2222f599714SMarcelo Tosatti
2230214f6bbSSean Christopherson /* Somewhat arbitrarily make vcpu0 the owner of the PIT. */
2240214f6bbSSean Christopherson if (vcpu->vcpu_id || !pit)
2252f599714SMarcelo Tosatti return;
2262f599714SMarcelo Tosatti
22726ef1924SAvi Kivity timer = &pit->pit_state.timer;
2282febc839SAndy Honig mutex_lock(&pit->pit_state.lock);
2292f599714SMarcelo Tosatti if (hrtimer_cancel(timer))
230beb20d52SArjan van de Ven hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
2312febc839SAndy Honig mutex_unlock(&pit->pit_state.lock);
2322f599714SMarcelo Tosatti }
2332f599714SMarcelo Tosatti
destroy_pit_timer(struct kvm_pit * pit)23433572ac0SChris Lalancette static void destroy_pit_timer(struct kvm_pit *pit)
2357837699fSSheng Yang {
23626ef1924SAvi Kivity hrtimer_cancel(&pit->pit_state.timer);
2373989144fSPetr Mladek kthread_flush_work(&pit->expired);
2387837699fSSheng Yang }
2397837699fSSheng Yang
pit_do_work(struct kthread_work * work)240b6ddf05fSJan Kiszka static void pit_do_work(struct kthread_work *work)
24133572ac0SChris Lalancette {
24233572ac0SChris Lalancette struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
24333572ac0SChris Lalancette struct kvm *kvm = pit->kvm;
24433572ac0SChris Lalancette struct kvm_vcpu *vcpu;
24546808a4cSMarc Zyngier unsigned long i;
24633572ac0SChris Lalancette struct kvm_kpit_state *ps = &pit->pit_state;
24733572ac0SChris Lalancette
248a0aace5aSRadim Krčmář if (atomic_read(&ps->reinject) && !atomic_xchg(&ps->irq_ack, 0))
249ddf54503SRadim Krčmář return;
250ddf54503SRadim Krčmář
2514a2095dfSRadim Krčmář kvm_set_irq(kvm, pit->irq_source_id, 0, 1, false);
2524a2095dfSRadim Krčmář kvm_set_irq(kvm, pit->irq_source_id, 0, 0, false);
25333572ac0SChris Lalancette
25433572ac0SChris Lalancette /*
25533572ac0SChris Lalancette * Provides NMI watchdog support via Virtual Wire mode.
256ddf54503SRadim Krčmář * The route is: PIT -> LVT0 in NMI mode.
25733572ac0SChris Lalancette *
258ddf54503SRadim Krčmář * Note: Our Virtual Wire implementation does not follow
259ddf54503SRadim Krčmář * the MP specification. We propagate a PIT interrupt to all
260ddf54503SRadim Krčmář * VCPUs and only when LVT0 is in NMI mode. The interrupt can
261ddf54503SRadim Krčmář * also be simultaneously delivered through PIC and IOAPIC.
26233572ac0SChris Lalancette */
26342720138SRadim Krčmář if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0)
26433572ac0SChris Lalancette kvm_for_each_vcpu(i, vcpu, kvm)
26533572ac0SChris Lalancette kvm_apic_nmi_wd_deliver(vcpu);
26633572ac0SChris Lalancette }
26733572ac0SChris Lalancette
pit_timer_fn(struct hrtimer * data)26833572ac0SChris Lalancette static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
26933572ac0SChris Lalancette {
27026ef1924SAvi Kivity struct kvm_kpit_state *ps = container_of(data, struct kvm_kpit_state, timer);
271a3e13115SRadim Krčmář struct kvm_pit *pt = pit_state_to_pit(ps);
27233572ac0SChris Lalancette
273a0aace5aSRadim Krčmář if (atomic_read(&ps->reinject))
27426ef1924SAvi Kivity atomic_inc(&ps->pending);
2757dd0fdffSRadim Krčmář
27636da91bdSPetr Mladek kthread_queue_work(pt->worker, &pt->expired);
27733572ac0SChris Lalancette
27826ef1924SAvi Kivity if (ps->is_periodic) {
27926ef1924SAvi Kivity hrtimer_add_expires_ns(&ps->timer, ps->period);
28033572ac0SChris Lalancette return HRTIMER_RESTART;
28133572ac0SChris Lalancette } else
28233572ac0SChris Lalancette return HRTIMER_NORESTART;
28333572ac0SChris Lalancette }
28433572ac0SChris Lalancette
kvm_pit_reset_reinject(struct kvm_pit * pit)285fd700a00SRadim Krčmář static inline void kvm_pit_reset_reinject(struct kvm_pit *pit)
286fd700a00SRadim Krčmář {
287fd700a00SRadim Krčmář atomic_set(&pit->pit_state.pending, 0);
288ddf54503SRadim Krčmář atomic_set(&pit->pit_state.irq_ack, 1);
289fd700a00SRadim Krčmář }
290fd700a00SRadim Krčmář
kvm_pit_set_reinject(struct kvm_pit * pit,bool reinject)29171474e2fSRadim Krčmář void kvm_pit_set_reinject(struct kvm_pit *pit, bool reinject)
29271474e2fSRadim Krčmář {
29371474e2fSRadim Krčmář struct kvm_kpit_state *ps = &pit->pit_state;
29471474e2fSRadim Krčmář struct kvm *kvm = pit->kvm;
29571474e2fSRadim Krčmář
296a0aace5aSRadim Krčmář if (atomic_read(&ps->reinject) == reinject)
29771474e2fSRadim Krčmář return;
29871474e2fSRadim Krčmář
299e2ed4078SSuravee Suthikulpanit /*
300e2ed4078SSuravee Suthikulpanit * AMD SVM AVIC accelerates EOI write and does not trap.
301e2ed4078SSuravee Suthikulpanit * This cause in-kernel PIT re-inject mode to fail
302e2ed4078SSuravee Suthikulpanit * since it checks ps->irq_ack before kvm_set_irq()
303e2ed4078SSuravee Suthikulpanit * and relies on the ack notifier to timely queue
304e2ed4078SSuravee Suthikulpanit * the pt->worker work iterm and reinject the missed tick.
305e2ed4078SSuravee Suthikulpanit * So, deactivate APICv when PIT is in reinject mode.
306e2ed4078SSuravee Suthikulpanit */
30771474e2fSRadim Krčmář if (reinject) {
308320af55aSSean Christopherson kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PIT_REINJ);
30971474e2fSRadim Krčmář /* The initial state is preserved while ps->reinject == 0. */
31071474e2fSRadim Krčmář kvm_pit_reset_reinject(pit);
31171474e2fSRadim Krčmář kvm_register_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
31271474e2fSRadim Krčmář kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
31371474e2fSRadim Krčmář } else {
314320af55aSSean Christopherson kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PIT_REINJ);
31571474e2fSRadim Krčmář kvm_unregister_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
31671474e2fSRadim Krčmář kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
31771474e2fSRadim Krčmář }
31871474e2fSRadim Krčmář
319a0aace5aSRadim Krčmář atomic_set(&ps->reinject, reinject);
32071474e2fSRadim Krčmář }
32171474e2fSRadim Krčmář
create_pit_timer(struct kvm_pit * pit,u32 val,int is_period)32209edea72SRadim Krčmář static void create_pit_timer(struct kvm_pit *pit, u32 val, int is_period)
3237837699fSSheng Yang {
32409edea72SRadim Krčmář struct kvm_kpit_state *ps = &pit->pit_state;
32509edea72SRadim Krčmář struct kvm *kvm = pit->kvm;
3267837699fSSheng Yang s64 interval;
3277837699fSSheng Yang
32849df6397SSteve Rutherford if (!ioapic_in_kernel(kvm) ||
32949df6397SSteve Rutherford ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)
3300924ab2cSJan Kiszka return;
3310924ab2cSJan Kiszka
3320e4d4415SPaolo Bonzini interval = mul_u64_u32_div(val, NSEC_PER_SEC, KVM_PIT_FREQ);
3337837699fSSheng Yang
334a78d9626SJoe Perches pr_debug("create pit timer, interval is %llu nsec\n", interval);
3357837699fSSheng Yang
3367837699fSSheng Yang /* TODO The new value only affected after the retriggered */
33726ef1924SAvi Kivity hrtimer_cancel(&ps->timer);
3383989144fSPetr Mladek kthread_flush_work(&pit->expired);
33926ef1924SAvi Kivity ps->period = interval;
340d3c7b77dSMarcelo Tosatti ps->is_periodic = is_period;
341d3c7b77dSMarcelo Tosatti
34209edea72SRadim Krčmář kvm_pit_reset_reinject(pit);
3437837699fSSheng Yang
3449ed96e87SMarcelo Tosatti /*
3459ed96e87SMarcelo Tosatti * Do not allow the guest to program periodic timers with small
3469ed96e87SMarcelo Tosatti * interval, since the hrtimers are not throttled by the host
3479ed96e87SMarcelo Tosatti * scheduler.
3489ed96e87SMarcelo Tosatti */
3499ed96e87SMarcelo Tosatti if (ps->is_periodic) {
3509ed96e87SMarcelo Tosatti s64 min_period = min_timer_period_us * 1000LL;
3519ed96e87SMarcelo Tosatti
3529ed96e87SMarcelo Tosatti if (ps->period < min_period) {
3539ed96e87SMarcelo Tosatti pr_info_ratelimited(
354*8d20bd63SSean Christopherson "requested %lld ns "
3559ed96e87SMarcelo Tosatti "i8254 timer period limited to %lld ns\n",
3569ed96e87SMarcelo Tosatti ps->period, min_period);
3579ed96e87SMarcelo Tosatti ps->period = min_period;
3589ed96e87SMarcelo Tosatti }
3599ed96e87SMarcelo Tosatti }
3609ed96e87SMarcelo Tosatti
36126ef1924SAvi Kivity hrtimer_start(&ps->timer, ktime_add_ns(ktime_get(), interval),
3627837699fSSheng Yang HRTIMER_MODE_ABS);
3637837699fSSheng Yang }
3647837699fSSheng Yang
pit_load_count(struct kvm_pit * pit,int channel,u32 val)36509edea72SRadim Krčmář static void pit_load_count(struct kvm_pit *pit, int channel, u32 val)
3667837699fSSheng Yang {
36709edea72SRadim Krčmář struct kvm_kpit_state *ps = &pit->pit_state;
3687837699fSSheng Yang
369e6302698SMiaohe Lin pr_debug("load_count val is %u, channel is %d\n", val, channel);
3707837699fSSheng Yang
3717837699fSSheng Yang /*
372ede2ccc5SMarcelo Tosatti * The largest possible initial count is 0; this is equivalent
373ede2ccc5SMarcelo Tosatti * to 216 for binary counting and 104 for BCD counting.
3747837699fSSheng Yang */
3757837699fSSheng Yang if (val == 0)
3767837699fSSheng Yang val = 0x10000;
3777837699fSSheng Yang
3787837699fSSheng Yang ps->channels[channel].count = val;
3797837699fSSheng Yang
380fd668423SMarcelo Tosatti if (channel != 0) {
381fd668423SMarcelo Tosatti ps->channels[channel].count_load_time = ktime_get();
3827837699fSSheng Yang return;
383fd668423SMarcelo Tosatti }
3847837699fSSheng Yang
3857837699fSSheng Yang /* Two types of timer
3867837699fSSheng Yang * mode 1 is one shot, mode 2 is period, otherwise del timer */
3877837699fSSheng Yang switch (ps->channels[0].mode) {
388ede2ccc5SMarcelo Tosatti case 0:
3897837699fSSheng Yang case 1:
390ece15babSMarcelo Tosatti /* FIXME: enhance mode 4 precision */
391ece15babSMarcelo Tosatti case 4:
39209edea72SRadim Krčmář create_pit_timer(pit, val, 0);
3937837699fSSheng Yang break;
3947837699fSSheng Yang case 2:
395f6975545SAurelien Jarno case 3:
39609edea72SRadim Krčmář create_pit_timer(pit, val, 1);
3977837699fSSheng Yang break;
3987837699fSSheng Yang default:
39909edea72SRadim Krčmář destroy_pit_timer(pit);
4007837699fSSheng Yang }
4017837699fSSheng Yang }
4027837699fSSheng Yang
kvm_pit_load_count(struct kvm_pit * pit,int channel,u32 val,int hpet_legacy_start)40309edea72SRadim Krčmář void kvm_pit_load_count(struct kvm_pit *pit, int channel, u32 val,
40409edea72SRadim Krčmář int hpet_legacy_start)
405e0f63cb9SSheng Yang {
406e9f42757SBeth Kon u8 saved_mode;
407b69d920fSRadim Krčmář
40809edea72SRadim Krčmář WARN_ON_ONCE(!mutex_is_locked(&pit->pit_state.lock));
409b69d920fSRadim Krčmář
410e9f42757SBeth Kon if (hpet_legacy_start) {
411e9f42757SBeth Kon /* save existing mode for later reenablement */
412e5e57e7aSPaolo Bonzini WARN_ON(channel != 0);
41309edea72SRadim Krčmář saved_mode = pit->pit_state.channels[0].mode;
41409edea72SRadim Krčmář pit->pit_state.channels[0].mode = 0xff; /* disable timer */
41509edea72SRadim Krčmář pit_load_count(pit, channel, val);
41609edea72SRadim Krčmář pit->pit_state.channels[0].mode = saved_mode;
417e9f42757SBeth Kon } else {
41809edea72SRadim Krčmář pit_load_count(pit, channel, val);
419e9f42757SBeth Kon }
420e0f63cb9SSheng Yang }
421e0f63cb9SSheng Yang
dev_to_pit(struct kvm_io_device * dev)422d76685c4SGregory Haskins static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
423d76685c4SGregory Haskins {
424d76685c4SGregory Haskins return container_of(dev, struct kvm_pit, dev);
425d76685c4SGregory Haskins }
426d76685c4SGregory Haskins
speaker_to_pit(struct kvm_io_device * dev)427d76685c4SGregory Haskins static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
428d76685c4SGregory Haskins {
429d76685c4SGregory Haskins return container_of(dev, struct kvm_pit, speaker_dev);
430d76685c4SGregory Haskins }
431d76685c4SGregory Haskins
pit_in_range(gpa_t addr)432bda9020eSMichael S. Tsirkin static inline int pit_in_range(gpa_t addr)
433bda9020eSMichael S. Tsirkin {
434bda9020eSMichael S. Tsirkin return ((addr >= KVM_PIT_BASE_ADDRESS) &&
435bda9020eSMichael S. Tsirkin (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
436bda9020eSMichael S. Tsirkin }
437bda9020eSMichael S. Tsirkin
pit_ioport_write(struct kvm_vcpu * vcpu,struct kvm_io_device * this,gpa_t addr,int len,const void * data)438e32edf4fSNikolay Nikolaev static int pit_ioport_write(struct kvm_vcpu *vcpu,
439e32edf4fSNikolay Nikolaev struct kvm_io_device *this,
4407837699fSSheng Yang gpa_t addr, int len, const void *data)
4417837699fSSheng Yang {
442d76685c4SGregory Haskins struct kvm_pit *pit = dev_to_pit(this);
4437837699fSSheng Yang struct kvm_kpit_state *pit_state = &pit->pit_state;
4447837699fSSheng Yang int channel, access;
4457837699fSSheng Yang struct kvm_kpit_channel_state *s;
4467837699fSSheng Yang u32 val = *(u32 *) data;
447bda9020eSMichael S. Tsirkin if (!pit_in_range(addr))
448bda9020eSMichael S. Tsirkin return -EOPNOTSUPP;
4497837699fSSheng Yang
4507837699fSSheng Yang val &= 0xff;
4517837699fSSheng Yang addr &= KVM_PIT_CHANNEL_MASK;
4527837699fSSheng Yang
4537837699fSSheng Yang mutex_lock(&pit_state->lock);
4547837699fSSheng Yang
4557837699fSSheng Yang if (val != 0)
456a78d9626SJoe Perches pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
4577837699fSSheng Yang (unsigned int)addr, len, val);
4587837699fSSheng Yang
4597837699fSSheng Yang if (addr == 3) {
4607837699fSSheng Yang channel = val >> 6;
4617837699fSSheng Yang if (channel == 3) {
4627837699fSSheng Yang /* Read-Back Command. */
4637837699fSSheng Yang for (channel = 0; channel < 3; channel++) {
4647837699fSSheng Yang if (val & (2 << channel)) {
4657837699fSSheng Yang if (!(val & 0x20))
46609edea72SRadim Krčmář pit_latch_count(pit, channel);
4677837699fSSheng Yang if (!(val & 0x10))
46809edea72SRadim Krčmář pit_latch_status(pit, channel);
4697837699fSSheng Yang }
4707837699fSSheng Yang }
4717837699fSSheng Yang } else {
4727837699fSSheng Yang /* Select Counter <channel>. */
4737837699fSSheng Yang s = &pit_state->channels[channel];
4747837699fSSheng Yang access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
4757837699fSSheng Yang if (access == 0) {
47609edea72SRadim Krčmář pit_latch_count(pit, channel);
4777837699fSSheng Yang } else {
4787837699fSSheng Yang s->rw_mode = access;
4797837699fSSheng Yang s->read_state = access;
4807837699fSSheng Yang s->write_state = access;
4817837699fSSheng Yang s->mode = (val >> 1) & 7;
4827837699fSSheng Yang if (s->mode > 5)
4837837699fSSheng Yang s->mode -= 4;
4847837699fSSheng Yang s->bcd = val & 1;
4857837699fSSheng Yang }
4867837699fSSheng Yang }
4877837699fSSheng Yang } else {
4887837699fSSheng Yang /* Write Count. */
4897837699fSSheng Yang s = &pit_state->channels[addr];
4907837699fSSheng Yang switch (s->write_state) {
4917837699fSSheng Yang default:
4927837699fSSheng Yang case RW_STATE_LSB:
49309edea72SRadim Krčmář pit_load_count(pit, addr, val);
4947837699fSSheng Yang break;
4957837699fSSheng Yang case RW_STATE_MSB:
49609edea72SRadim Krčmář pit_load_count(pit, addr, val << 8);
4977837699fSSheng Yang break;
4987837699fSSheng Yang case RW_STATE_WORD0:
4997837699fSSheng Yang s->write_latch = val;
5007837699fSSheng Yang s->write_state = RW_STATE_WORD1;
5017837699fSSheng Yang break;
5027837699fSSheng Yang case RW_STATE_WORD1:
50309edea72SRadim Krčmář pit_load_count(pit, addr, s->write_latch | (val << 8));
5047837699fSSheng Yang s->write_state = RW_STATE_WORD0;
5057837699fSSheng Yang break;
5067837699fSSheng Yang }
5077837699fSSheng Yang }
5087837699fSSheng Yang
5097837699fSSheng Yang mutex_unlock(&pit_state->lock);
510bda9020eSMichael S. Tsirkin return 0;
5117837699fSSheng Yang }
5127837699fSSheng Yang
pit_ioport_read(struct kvm_vcpu * vcpu,struct kvm_io_device * this,gpa_t addr,int len,void * data)513e32edf4fSNikolay Nikolaev static int pit_ioport_read(struct kvm_vcpu *vcpu,
514e32edf4fSNikolay Nikolaev struct kvm_io_device *this,
5157837699fSSheng Yang gpa_t addr, int len, void *data)
5167837699fSSheng Yang {
517d76685c4SGregory Haskins struct kvm_pit *pit = dev_to_pit(this);
5187837699fSSheng Yang struct kvm_kpit_state *pit_state = &pit->pit_state;
5197837699fSSheng Yang int ret, count;
5207837699fSSheng Yang struct kvm_kpit_channel_state *s;
521bda9020eSMichael S. Tsirkin if (!pit_in_range(addr))
522bda9020eSMichael S. Tsirkin return -EOPNOTSUPP;
5237837699fSSheng Yang
5247837699fSSheng Yang addr &= KVM_PIT_CHANNEL_MASK;
525ee73f656SMarcelo Tosatti if (addr == 3)
526ee73f656SMarcelo Tosatti return 0;
527ee73f656SMarcelo Tosatti
5287837699fSSheng Yang s = &pit_state->channels[addr];
5297837699fSSheng Yang
5307837699fSSheng Yang mutex_lock(&pit_state->lock);
5317837699fSSheng Yang
5327837699fSSheng Yang if (s->status_latched) {
5337837699fSSheng Yang s->status_latched = 0;
5347837699fSSheng Yang ret = s->status;
5357837699fSSheng Yang } else if (s->count_latched) {
5367837699fSSheng Yang switch (s->count_latched) {
5377837699fSSheng Yang default:
5387837699fSSheng Yang case RW_STATE_LSB:
5397837699fSSheng Yang ret = s->latched_count & 0xff;
5407837699fSSheng Yang s->count_latched = 0;
5417837699fSSheng Yang break;
5427837699fSSheng Yang case RW_STATE_MSB:
5437837699fSSheng Yang ret = s->latched_count >> 8;
5447837699fSSheng Yang s->count_latched = 0;
5457837699fSSheng Yang break;
5467837699fSSheng Yang case RW_STATE_WORD0:
5477837699fSSheng Yang ret = s->latched_count & 0xff;
5487837699fSSheng Yang s->count_latched = RW_STATE_MSB;
5497837699fSSheng Yang break;
5507837699fSSheng Yang }
5517837699fSSheng Yang } else {
5527837699fSSheng Yang switch (s->read_state) {
5537837699fSSheng Yang default:
5547837699fSSheng Yang case RW_STATE_LSB:
55509edea72SRadim Krčmář count = pit_get_count(pit, addr);
5567837699fSSheng Yang ret = count & 0xff;
5577837699fSSheng Yang break;
5587837699fSSheng Yang case RW_STATE_MSB:
55909edea72SRadim Krčmář count = pit_get_count(pit, addr);
5607837699fSSheng Yang ret = (count >> 8) & 0xff;
5617837699fSSheng Yang break;
5627837699fSSheng Yang case RW_STATE_WORD0:
56309edea72SRadim Krčmář count = pit_get_count(pit, addr);
5647837699fSSheng Yang ret = count & 0xff;
5657837699fSSheng Yang s->read_state = RW_STATE_WORD1;
5667837699fSSheng Yang break;
5677837699fSSheng Yang case RW_STATE_WORD1:
56809edea72SRadim Krčmář count = pit_get_count(pit, addr);
5697837699fSSheng Yang ret = (count >> 8) & 0xff;
5707837699fSSheng Yang s->read_state = RW_STATE_WORD0;
5717837699fSSheng Yang break;
5727837699fSSheng Yang }
5737837699fSSheng Yang }
5747837699fSSheng Yang
5757837699fSSheng Yang if (len > sizeof(ret))
5767837699fSSheng Yang len = sizeof(ret);
5777837699fSSheng Yang memcpy(data, (char *)&ret, len);
5787837699fSSheng Yang
5797837699fSSheng Yang mutex_unlock(&pit_state->lock);
580bda9020eSMichael S. Tsirkin return 0;
5817837699fSSheng Yang }
5827837699fSSheng Yang
speaker_ioport_write(struct kvm_vcpu * vcpu,struct kvm_io_device * this,gpa_t addr,int len,const void * data)583e32edf4fSNikolay Nikolaev static int speaker_ioport_write(struct kvm_vcpu *vcpu,
584e32edf4fSNikolay Nikolaev struct kvm_io_device *this,
5857837699fSSheng Yang gpa_t addr, int len, const void *data)
5867837699fSSheng Yang {
587d76685c4SGregory Haskins struct kvm_pit *pit = speaker_to_pit(this);
5887837699fSSheng Yang struct kvm_kpit_state *pit_state = &pit->pit_state;
5897837699fSSheng Yang u32 val = *(u32 *) data;
590bda9020eSMichael S. Tsirkin if (addr != KVM_SPEAKER_BASE_ADDRESS)
591bda9020eSMichael S. Tsirkin return -EOPNOTSUPP;
5927837699fSSheng Yang
5937837699fSSheng Yang mutex_lock(&pit_state->lock);
594b1728622SPaul Durrant if (val & (1 << 1))
595b1728622SPaul Durrant pit_state->flags |= KVM_PIT_FLAGS_SPEAKER_DATA_ON;
596b1728622SPaul Durrant else
597b1728622SPaul Durrant pit_state->flags &= ~KVM_PIT_FLAGS_SPEAKER_DATA_ON;
59809edea72SRadim Krčmář pit_set_gate(pit, 2, val & 1);
5997837699fSSheng Yang mutex_unlock(&pit_state->lock);
600bda9020eSMichael S. Tsirkin return 0;
6017837699fSSheng Yang }
6027837699fSSheng Yang
speaker_ioport_read(struct kvm_vcpu * vcpu,struct kvm_io_device * this,gpa_t addr,int len,void * data)603e32edf4fSNikolay Nikolaev static int speaker_ioport_read(struct kvm_vcpu *vcpu,
604e32edf4fSNikolay Nikolaev struct kvm_io_device *this,
6057837699fSSheng Yang gpa_t addr, int len, void *data)
6067837699fSSheng Yang {
607d76685c4SGregory Haskins struct kvm_pit *pit = speaker_to_pit(this);
6087837699fSSheng Yang struct kvm_kpit_state *pit_state = &pit->pit_state;
6097837699fSSheng Yang unsigned int refresh_clock;
6107837699fSSheng Yang int ret;
611bda9020eSMichael S. Tsirkin if (addr != KVM_SPEAKER_BASE_ADDRESS)
612bda9020eSMichael S. Tsirkin return -EOPNOTSUPP;
6137837699fSSheng Yang
6147837699fSSheng Yang /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
6157837699fSSheng Yang refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
6167837699fSSheng Yang
6177837699fSSheng Yang mutex_lock(&pit_state->lock);
618b1728622SPaul Durrant ret = (!!(pit_state->flags & KVM_PIT_FLAGS_SPEAKER_DATA_ON) << 1) |
619b1728622SPaul Durrant pit_get_gate(pit, 2) | (pit_get_out(pit, 2) << 5) |
620b1728622SPaul Durrant (refresh_clock << 4);
6217837699fSSheng Yang if (len > sizeof(ret))
6227837699fSSheng Yang len = sizeof(ret);
6237837699fSSheng Yang memcpy(data, (char *)&ret, len);
6247837699fSSheng Yang mutex_unlock(&pit_state->lock);
625bda9020eSMichael S. Tsirkin return 0;
6267837699fSSheng Yang }
6277837699fSSheng Yang
kvm_pit_reset(struct kvm_pit * pit)628b39c90b6SRadim Krčmář static void kvm_pit_reset(struct kvm_pit *pit)
6297837699fSSheng Yang {
6307837699fSSheng Yang int i;
631308b0f23SSheng Yang struct kvm_kpit_channel_state *c;
632308b0f23SSheng Yang
633e9f42757SBeth Kon pit->pit_state.flags = 0;
634308b0f23SSheng Yang for (i = 0; i < 3; i++) {
635308b0f23SSheng Yang c = &pit->pit_state.channels[i];
636308b0f23SSheng Yang c->mode = 0xff;
637308b0f23SSheng Yang c->gate = (i != 2);
63809edea72SRadim Krčmář pit_load_count(pit, i, 0);
639308b0f23SSheng Yang }
640308b0f23SSheng Yang
641fd700a00SRadim Krčmář kvm_pit_reset_reinject(pit);
642308b0f23SSheng Yang }
643308b0f23SSheng Yang
pit_mask_notifer(struct kvm_irq_mask_notifier * kimn,bool mask)6444780c659SAvi Kivity static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
6454780c659SAvi Kivity {
6464780c659SAvi Kivity struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
6474780c659SAvi Kivity
648fd700a00SRadim Krčmář if (!mask)
649fd700a00SRadim Krčmář kvm_pit_reset_reinject(pit);
6504780c659SAvi Kivity }
6514780c659SAvi Kivity
652d76685c4SGregory Haskins static const struct kvm_io_device_ops pit_dev_ops = {
653d76685c4SGregory Haskins .read = pit_ioport_read,
654d76685c4SGregory Haskins .write = pit_ioport_write,
655d76685c4SGregory Haskins };
656d76685c4SGregory Haskins
657d76685c4SGregory Haskins static const struct kvm_io_device_ops speaker_dev_ops = {
658d76685c4SGregory Haskins .read = speaker_ioport_read,
659d76685c4SGregory Haskins .write = speaker_ioport_write,
660d76685c4SGregory Haskins };
661d76685c4SGregory Haskins
kvm_create_pit(struct kvm * kvm,u32 flags)662c5ff41ceSJan Kiszka struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
663308b0f23SSheng Yang {
6647837699fSSheng Yang struct kvm_pit *pit;
6657837699fSSheng Yang struct kvm_kpit_state *pit_state;
666b6ddf05fSJan Kiszka struct pid *pid;
667b6ddf05fSJan Kiszka pid_t pid_nr;
668090b7affSGregory Haskins int ret;
6697837699fSSheng Yang
670254272ceSBen Gardon pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL_ACCOUNT);
6717837699fSSheng Yang if (!pit)
6727837699fSSheng Yang return NULL;
6737837699fSSheng Yang
6745550af4dSSheng Yang pit->irq_source_id = kvm_request_irq_source_id(kvm);
67510d24821SRadim Krčmář if (pit->irq_source_id < 0)
67610d24821SRadim Krčmář goto fail_request;
6775550af4dSSheng Yang
6787837699fSSheng Yang mutex_init(&pit->pit_state.lock);
67933572ac0SChris Lalancette
680b6ddf05fSJan Kiszka pid = get_pid(task_tgid(current));
681b6ddf05fSJan Kiszka pid_nr = pid_vnr(pid);
682b6ddf05fSJan Kiszka put_pid(pid);
683b6ddf05fSJan Kiszka
68436da91bdSPetr Mladek pit->worker = kthread_create_worker(0, "kvm-pit/%d", pid_nr);
68536da91bdSPetr Mladek if (IS_ERR(pit->worker))
68610d24821SRadim Krčmář goto fail_kthread;
68710d24821SRadim Krčmář
6883989144fSPetr Mladek kthread_init_work(&pit->expired, pit_do_work);
6897837699fSSheng Yang
6907837699fSSheng Yang pit->kvm = kvm;
6917837699fSSheng Yang
6927837699fSSheng Yang pit_state = &pit->pit_state;
69326ef1924SAvi Kivity hrtimer_init(&pit_state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
694ab4c1476SRadim Krčmář pit_state->timer.function = pit_timer_fn;
69571474e2fSRadim Krčmář
6963cf57fedSMarcelo Tosatti pit_state->irq_ack_notifier.gsi = 0;
6973cf57fedSMarcelo Tosatti pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
69871474e2fSRadim Krčmář pit->mask_notifier.func = pit_mask_notifer;
6997837699fSSheng Yang
700308b0f23SSheng Yang kvm_pit_reset(pit);
7017837699fSSheng Yang
70271474e2fSRadim Krčmář kvm_pit_set_reinject(pit, true);
7034780c659SAvi Kivity
704250715a6SPaolo Bonzini mutex_lock(&kvm->slots_lock);
7056b66ac1aSGregory Haskins kvm_iodevice_init(&pit->dev, &pit_dev_ops);
706743eeb0bSSasha Levin ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS,
707743eeb0bSSasha Levin KVM_PIT_MEM_LENGTH, &pit->dev);
708090b7affSGregory Haskins if (ret < 0)
70910d24821SRadim Krčmář goto fail_register_pit;
7106b66ac1aSGregory Haskins
7116b66ac1aSGregory Haskins if (flags & KVM_PIT_SPEAKER_DUMMY) {
7126b66ac1aSGregory Haskins kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
713e93f8a0fSMarcelo Tosatti ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
714743eeb0bSSasha Levin KVM_SPEAKER_BASE_ADDRESS, 4,
715090b7affSGregory Haskins &pit->speaker_dev);
716090b7affSGregory Haskins if (ret < 0)
71710d24821SRadim Krčmář goto fail_register_speaker;
7186b66ac1aSGregory Haskins }
719250715a6SPaolo Bonzini mutex_unlock(&kvm->slots_lock);
7206b66ac1aSGregory Haskins
7217837699fSSheng Yang return pit;
722090b7affSGregory Haskins
72310d24821SRadim Krčmář fail_register_speaker:
724e93f8a0fSMarcelo Tosatti kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
72510d24821SRadim Krčmář fail_register_pit:
726250715a6SPaolo Bonzini mutex_unlock(&kvm->slots_lock);
72771474e2fSRadim Krčmář kvm_pit_set_reinject(pit, false);
72836da91bdSPetr Mladek kthread_destroy_worker(pit->worker);
72910d24821SRadim Krčmář fail_kthread:
73010d24821SRadim Krčmář kvm_free_irq_source_id(kvm, pit->irq_source_id);
73110d24821SRadim Krčmář fail_request:
732090b7affSGregory Haskins kfree(pit);
733090b7affSGregory Haskins return NULL;
7347837699fSSheng Yang }
7357837699fSSheng Yang
kvm_free_pit(struct kvm * kvm)7367837699fSSheng Yang void kvm_free_pit(struct kvm *kvm)
7377837699fSSheng Yang {
73808e5ccf3SRadim Krčmář struct kvm_pit *pit = kvm->arch.vpit;
7397837699fSSheng Yang
74008e5ccf3SRadim Krčmář if (pit) {
741fb530729SRadim Krčmář mutex_lock(&kvm->slots_lock);
74208e5ccf3SRadim Krčmář kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
74308e5ccf3SRadim Krčmář kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->speaker_dev);
744fb530729SRadim Krčmář mutex_unlock(&kvm->slots_lock);
74508e5ccf3SRadim Krčmář kvm_pit_set_reinject(pit, false);
74608e5ccf3SRadim Krčmář hrtimer_cancel(&pit->pit_state.timer);
74736da91bdSPetr Mladek kthread_destroy_worker(pit->worker);
74808e5ccf3SRadim Krčmář kvm_free_irq_source_id(kvm, pit->irq_source_id);
74908e5ccf3SRadim Krčmář kfree(pit);
7507837699fSSheng Yang }
7517837699fSSheng Yang }
752