blob: 0b65a764ed3a1741b6fa550ecb469f800621d327 [file] [log] [blame]
Sheng Yang78376992008-01-28 05:10:22 +08001/*
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
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2009 Red Hat, Inc. and/or its affiliates.
Sheng Yang78376992008-01-28 05:10:22 +08009 *
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
Joe Perchesa78d96262009-12-09 10:45:35 -080033#define pr_fmt(fmt) "pit: " fmt
34
Sheng Yang78376992008-01-28 05:10:22 +080035#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Sheng Yang78376992008-01-28 05:10:22 +080037
Steve Rutherford49df6392015-07-29 23:21:40 -070038#include "ioapic.h"
Sheng Yang78376992008-01-28 05:10:22 +080039#include "irq.h"
40#include "i8254.h"
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -020041#include "x86.h"
Sheng Yang78376992008-01-28 05:10:22 +080042
43#ifndef CONFIG_X86_64
Roman Zippel6f6d6a12008-05-01 04:34:28 -070044#define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
Sheng Yang78376992008-01-28 05:10:22 +080045#else
46#define mod_64(x, y) ((x) % (y))
47#endif
48
49#define RW_STATE_LSB 1
50#define RW_STATE_MSB 2
51#define RW_STATE_WORD0 3
52#define RW_STATE_WORD1 4
53
Radim Krčmář09edea72016-03-02 22:56:43 +010054static void pit_set_gate(struct kvm_pit *pit, int channel, u32 val)
Sheng Yang78376992008-01-28 05:10:22 +080055{
Radim Krčmář09edea72016-03-02 22:56:43 +010056 struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
Sheng Yang78376992008-01-28 05:10:22 +080057
Sheng Yang78376992008-01-28 05:10:22 +080058 switch (c->mode) {
59 default:
60 case 0:
61 case 4:
62 /* XXX: just disable/enable counting */
63 break;
64 case 1:
65 case 2:
66 case 3:
67 case 5:
68 /* Restart counting on rising edge. */
69 if (c->gate < val)
70 c->count_load_time = ktime_get();
71 break;
72 }
73
74 c->gate = val;
75}
76
Radim Krčmář09edea72016-03-02 22:56:43 +010077static int pit_get_gate(struct kvm_pit *pit, int channel)
Sheng Yang78376992008-01-28 05:10:22 +080078{
Radim Krčmář09edea72016-03-02 22:56:43 +010079 return pit->pit_state.channels[channel].gate;
Sheng Yang78376992008-01-28 05:10:22 +080080}
81
Radim Krčmář09edea72016-03-02 22:56:43 +010082static s64 __kpit_elapsed(struct kvm_pit *pit)
Marcelo Tosattifd668422009-02-23 10:57:40 -030083{
84 s64 elapsed;
85 ktime_t remaining;
Radim Krčmář09edea72016-03-02 22:56:43 +010086 struct kvm_kpit_state *ps = &pit->pit_state;
Marcelo Tosattifd668422009-02-23 10:57:40 -030087
Avi Kivity26ef1922012-07-26 18:01:53 +030088 if (!ps->period)
Marcelo Tosatti0ff77872009-07-02 20:02:15 -030089 return 0;
90
Marcelo Tosattiede2ccc2009-04-08 13:14:19 -030091 /*
92 * The Counter does not stop when it reaches zero. In
93 * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
94 * the highest count, either FFFF hex for binary counting
95 * or 9999 for BCD counting, and continues counting.
96 * Modes 2 and 3 are periodic; the Counter reloads
97 * itself with the initial count and continues counting
98 * from there.
99 */
Avi Kivity26ef1922012-07-26 18:01:53 +0300100 remaining = hrtimer_get_remaining(&ps->timer);
101 elapsed = ps->period - ktime_to_ns(remaining);
Marcelo Tosattifd668422009-02-23 10:57:40 -0300102
103 return elapsed;
104}
105
Radim Krčmář09edea72016-03-02 22:56:43 +0100106static s64 kpit_elapsed(struct kvm_pit *pit, struct kvm_kpit_channel_state *c,
Marcelo Tosattifd668422009-02-23 10:57:40 -0300107 int channel)
108{
109 if (channel == 0)
Radim Krčmář09edea72016-03-02 22:56:43 +0100110 return __kpit_elapsed(pit);
Marcelo Tosattifd668422009-02-23 10:57:40 -0300111
112 return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
113}
114
Radim Krčmář09edea72016-03-02 22:56:43 +0100115static int pit_get_count(struct kvm_pit *pit, int channel)
Sheng Yang78376992008-01-28 05:10:22 +0800116{
Radim Krčmář09edea72016-03-02 22:56:43 +0100117 struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
Sheng Yang78376992008-01-28 05:10:22 +0800118 s64 d, t;
119 int counter;
120
Radim Krčmář09edea72016-03-02 22:56:43 +0100121 t = kpit_elapsed(pit, c, channel);
Paolo Bonzini0e4d4412016-03-04 09:28:41 +0100122 d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC);
Sheng Yang78376992008-01-28 05:10:22 +0800123
124 switch (c->mode) {
125 case 0:
126 case 1:
127 case 4:
128 case 5:
129 counter = (c->count - d) & 0xffff;
130 break;
131 case 3:
132 /* XXX: may be incorrect for odd counts */
133 counter = c->count - (mod_64((2 * d), c->count));
134 break;
135 default:
136 counter = c->count - mod_64(d, c->count);
137 break;
138 }
139 return counter;
140}
141
Radim Krčmář09edea72016-03-02 22:56:43 +0100142static int pit_get_out(struct kvm_pit *pit, int channel)
Sheng Yang78376992008-01-28 05:10:22 +0800143{
Radim Krčmář09edea72016-03-02 22:56:43 +0100144 struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
Sheng Yang78376992008-01-28 05:10:22 +0800145 s64 d, t;
146 int out;
147
Radim Krčmář09edea72016-03-02 22:56:43 +0100148 t = kpit_elapsed(pit, c, channel);
Paolo Bonzini0e4d4412016-03-04 09:28:41 +0100149 d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC);
Sheng Yang78376992008-01-28 05:10:22 +0800150
151 switch (c->mode) {
152 default:
153 case 0:
154 out = (d >= c->count);
155 break;
156 case 1:
157 out = (d < c->count);
158 break;
159 case 2:
160 out = ((mod_64(d, c->count) == 0) && (d != 0));
161 break;
162 case 3:
163 out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
164 break;
165 case 4:
166 case 5:
167 out = (d == c->count);
168 break;
169 }
170
171 return out;
172}
173
Radim Krčmář09edea72016-03-02 22:56:43 +0100174static void pit_latch_count(struct kvm_pit *pit, int channel)
Sheng Yang78376992008-01-28 05:10:22 +0800175{
Radim Krčmář09edea72016-03-02 22:56:43 +0100176 struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
Sheng Yang78376992008-01-28 05:10:22 +0800177
Sheng Yang78376992008-01-28 05:10:22 +0800178 if (!c->count_latched) {
Radim Krčmář09edea72016-03-02 22:56:43 +0100179 c->latched_count = pit_get_count(pit, channel);
Sheng Yang78376992008-01-28 05:10:22 +0800180 c->count_latched = c->rw_mode;
181 }
182}
183
Radim Krčmář09edea72016-03-02 22:56:43 +0100184static void pit_latch_status(struct kvm_pit *pit, int channel)
Sheng Yang78376992008-01-28 05:10:22 +0800185{
Radim Krčmář09edea72016-03-02 22:56:43 +0100186 struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel];
Sheng Yang78376992008-01-28 05:10:22 +0800187
Sheng Yang78376992008-01-28 05:10:22 +0800188 if (!c->status_latched) {
189 /* TODO: Return NULL COUNT (bit 6). */
Radim Krčmář09edea72016-03-02 22:56:43 +0100190 c->status = ((pit_get_out(pit, channel) << 7) |
Sheng Yang78376992008-01-28 05:10:22 +0800191 (c->rw_mode << 4) |
192 (c->mode << 1) |
193 c->bcd);
194 c->status_latched = 1;
195 }
196}
197
Radim Krčmářa3e13112016-03-02 22:56:48 +0100198static inline struct kvm_pit *pit_state_to_pit(struct kvm_kpit_state *ps)
199{
200 return container_of(ps, struct kvm_pit, pit_state);
201}
202
Harvey Harrisonee032c92008-08-11 16:54:20 -0700203static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
Marcelo Tosatti3cf57fe2008-07-26 17:01:01 -0300204{
205 struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
206 irq_ack_notifier);
Radim Krčmářa3e13112016-03-02 22:56:48 +0100207 struct kvm_pit *pit = pit_state_to_pit(ps);
Chris Lalancette33572ac2010-06-16 17:11:11 -0400208
Radim Krčmářddf54502016-03-02 22:56:41 +0100209 atomic_set(&ps->irq_ack, 1);
210 /* irq_ack should be set before pending is read. Order accesses with
211 * inc(pending) in pit_timer_fn and xchg(irq_ack, 0) in pit_do_work.
212 */
213 smp_mb();
Radim Krčmář71474e22016-03-02 22:56:45 +0100214 if (atomic_dec_if_positive(&ps->pending) > 0)
Petr Mladek36da91b2016-10-19 13:50:47 +0200215 kthread_queue_work(pit->worker, &pit->expired);
Marcelo Tosatti3cf57fe2008-07-26 17:01:01 -0300216}
217
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300218void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
219{
220 struct kvm_pit *pit = vcpu->kvm->arch.vpit;
221 struct hrtimer *timer;
222
Sean Christopherson0214f6b2021-07-13 09:32:51 -0700223 /* Somewhat arbitrarily make vcpu0 the owner of the PIT. */
224 if (vcpu->vcpu_id || !pit)
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300225 return;
226
Avi Kivity26ef1922012-07-26 18:01:53 +0300227 timer = &pit->pit_state.timer;
Andy Honig2febc832014-08-27 14:42:54 -0700228 mutex_lock(&pit->pit_state.lock);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300229 if (hrtimer_cancel(timer))
Arjan van de Venbeb20d522008-09-01 14:55:57 -0700230 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
Andy Honig2febc832014-08-27 14:42:54 -0700231 mutex_unlock(&pit->pit_state.lock);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300232}
233
Chris Lalancette33572ac2010-06-16 17:11:11 -0400234static void destroy_pit_timer(struct kvm_pit *pit)
Sheng Yang78376992008-01-28 05:10:22 +0800235{
Avi Kivity26ef1922012-07-26 18:01:53 +0300236 hrtimer_cancel(&pit->pit_state.timer);
Petr Mladek39891442016-10-11 13:55:20 -0700237 kthread_flush_work(&pit->expired);
Sheng Yang78376992008-01-28 05:10:22 +0800238}
239
Jan Kiszkab6ddf052012-04-24 16:40:17 +0200240static void pit_do_work(struct kthread_work *work)
Chris Lalancette33572ac2010-06-16 17:11:11 -0400241{
242 struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
243 struct kvm *kvm = pit->kvm;
244 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +0000245 unsigned long i;
Chris Lalancette33572ac2010-06-16 17:11:11 -0400246 struct kvm_kpit_state *ps = &pit->pit_state;
Chris Lalancette33572ac2010-06-16 17:11:11 -0400247
Radim Krčmářa0aace52016-03-02 22:56:52 +0100248 if (atomic_read(&ps->reinject) && !atomic_xchg(&ps->irq_ack, 0))
Radim Krčmářddf54502016-03-02 22:56:41 +0100249 return;
250
Radim Krčmář4a2095d2016-03-02 22:56:49 +0100251 kvm_set_irq(kvm, pit->irq_source_id, 0, 1, false);
252 kvm_set_irq(kvm, pit->irq_source_id, 0, 0, false);
Radim Krčmářddf54502016-03-02 22:56:41 +0100253
254 /*
255 * Provides NMI watchdog support via Virtual Wire mode.
256 * The route is: PIT -> LVT0 in NMI mode.
257 *
258 * Note: Our Virtual Wire implementation does not follow
259 * the MP specification. We propagate a PIT interrupt to all
260 * VCPUs and only when LVT0 is in NMI mode. The interrupt can
261 * also be simultaneously delivered through PIC and IOAPIC.
Chris Lalancette33572ac2010-06-16 17:11:11 -0400262 */
Radim Krčmářddf54502016-03-02 22:56:41 +0100263 if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0)
264 kvm_for_each_vcpu(i, vcpu, kvm)
265 kvm_apic_nmi_wd_deliver(vcpu);
Chris Lalancette33572ac2010-06-16 17:11:11 -0400266}
267
268static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
269{
Avi Kivity26ef1922012-07-26 18:01:53 +0300270 struct kvm_kpit_state *ps = container_of(data, struct kvm_kpit_state, timer);
Radim Krčmářa3e13112016-03-02 22:56:48 +0100271 struct kvm_pit *pt = pit_state_to_pit(ps);
Chris Lalancette33572ac2010-06-16 17:11:11 -0400272
Radim Krčmářa0aace52016-03-02 22:56:52 +0100273 if (atomic_read(&ps->reinject))
Avi Kivity26ef1922012-07-26 18:01:53 +0300274 atomic_inc(&ps->pending);
Radim Krčmář7dd0fdf2016-03-02 22:56:38 +0100275
Petr Mladek36da91b2016-10-19 13:50:47 +0200276 kthread_queue_work(pt->worker, &pt->expired);
Chris Lalancette33572ac2010-06-16 17:11:11 -0400277
Avi Kivity26ef1922012-07-26 18:01:53 +0300278 if (ps->is_periodic) {
279 hrtimer_add_expires_ns(&ps->timer, ps->period);
Chris Lalancette33572ac2010-06-16 17:11:11 -0400280 return HRTIMER_RESTART;
281 } else
282 return HRTIMER_NORESTART;
283}
284
Radim Krčmářfd700a02016-03-02 22:56:40 +0100285static inline void kvm_pit_reset_reinject(struct kvm_pit *pit)
286{
287 atomic_set(&pit->pit_state.pending, 0);
Radim Krčmářddf54502016-03-02 22:56:41 +0100288 atomic_set(&pit->pit_state.irq_ack, 1);
Radim Krčmářfd700a02016-03-02 22:56:40 +0100289}
290
Radim Krčmář71474e22016-03-02 22:56:45 +0100291void kvm_pit_set_reinject(struct kvm_pit *pit, bool reinject)
292{
293 struct kvm_kpit_state *ps = &pit->pit_state;
294 struct kvm *kvm = pit->kvm;
295
Radim Krčmářa0aace52016-03-02 22:56:52 +0100296 if (atomic_read(&ps->reinject) == reinject)
Radim Krčmář71474e22016-03-02 22:56:45 +0100297 return;
298
Suravee Suthikulpanite2ed4072019-11-14 14:15:16 -0600299 /*
300 * AMD SVM AVIC accelerates EOI write and does not trap.
301 * This cause in-kernel PIT re-inject mode to fail
302 * since it checks ps->irq_ack before kvm_set_irq()
303 * and relies on the ack notifier to timely queue
304 * the pt->worker work iterm and reinject the missed tick.
305 * So, deactivate APICv when PIT is in reinject mode.
306 */
Radim Krčmář71474e22016-03-02 22:56:45 +0100307 if (reinject) {
Suravee Suthikulpanite2ed4072019-11-14 14:15:16 -0600308 kvm_request_apicv_update(kvm, false,
309 APICV_INHIBIT_REASON_PIT_REINJ);
Radim Krčmář71474e22016-03-02 22:56:45 +0100310 /* The initial state is preserved while ps->reinject == 0. */
311 kvm_pit_reset_reinject(pit);
312 kvm_register_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
313 kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
314 } else {
Suravee Suthikulpanite2ed4072019-11-14 14:15:16 -0600315 kvm_request_apicv_update(kvm, true,
316 APICV_INHIBIT_REASON_PIT_REINJ);
Radim Krčmář71474e22016-03-02 22:56:45 +0100317 kvm_unregister_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
318 kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
319 }
320
Radim Krčmářa0aace52016-03-02 22:56:52 +0100321 atomic_set(&ps->reinject, reinject);
Radim Krčmář71474e22016-03-02 22:56:45 +0100322}
323
Radim Krčmář09edea72016-03-02 22:56:43 +0100324static void create_pit_timer(struct kvm_pit *pit, u32 val, int is_period)
Sheng Yang78376992008-01-28 05:10:22 +0800325{
Radim Krčmář09edea72016-03-02 22:56:43 +0100326 struct kvm_kpit_state *ps = &pit->pit_state;
327 struct kvm *kvm = pit->kvm;
Sheng Yang78376992008-01-28 05:10:22 +0800328 s64 interval;
329
Steve Rutherford49df6392015-07-29 23:21:40 -0700330 if (!ioapic_in_kernel(kvm) ||
331 ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)
Jan Kiszka0924ab22011-12-14 19:25:13 +0100332 return;
333
Paolo Bonzini0e4d4412016-03-04 09:28:41 +0100334 interval = mul_u64_u32_div(val, NSEC_PER_SEC, KVM_PIT_FREQ);
Sheng Yang78376992008-01-28 05:10:22 +0800335
Joe Perchesa78d96262009-12-09 10:45:35 -0800336 pr_debug("create pit timer, interval is %llu nsec\n", interval);
Sheng Yang78376992008-01-28 05:10:22 +0800337
338 /* TODO The new value only affected after the retriggered */
Avi Kivity26ef1922012-07-26 18:01:53 +0300339 hrtimer_cancel(&ps->timer);
Petr Mladek39891442016-10-11 13:55:20 -0700340 kthread_flush_work(&pit->expired);
Avi Kivity26ef1922012-07-26 18:01:53 +0300341 ps->period = interval;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -0300342 ps->is_periodic = is_period;
343
Radim Krčmář09edea72016-03-02 22:56:43 +0100344 kvm_pit_reset_reinject(pit);
Sheng Yang78376992008-01-28 05:10:22 +0800345
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200346 /*
347 * Do not allow the guest to program periodic timers with small
348 * interval, since the hrtimers are not throttled by the host
349 * scheduler.
350 */
351 if (ps->is_periodic) {
352 s64 min_period = min_timer_period_us * 1000LL;
353
354 if (ps->period < min_period) {
355 pr_info_ratelimited(
356 "kvm: requested %lld ns "
357 "i8254 timer period limited to %lld ns\n",
358 ps->period, min_period);
359 ps->period = min_period;
360 }
361 }
362
Avi Kivity26ef1922012-07-26 18:01:53 +0300363 hrtimer_start(&ps->timer, ktime_add_ns(ktime_get(), interval),
Sheng Yang78376992008-01-28 05:10:22 +0800364 HRTIMER_MODE_ABS);
365}
366
Radim Krčmář09edea72016-03-02 22:56:43 +0100367static void pit_load_count(struct kvm_pit *pit, int channel, u32 val)
Sheng Yang78376992008-01-28 05:10:22 +0800368{
Radim Krčmář09edea72016-03-02 22:56:43 +0100369 struct kvm_kpit_state *ps = &pit->pit_state;
Sheng Yang78376992008-01-28 05:10:22 +0800370
Miaohe Line6302692020-02-15 10:44:22 +0800371 pr_debug("load_count val is %u, channel is %d\n", val, channel);
Sheng Yang78376992008-01-28 05:10:22 +0800372
373 /*
Marcelo Tosattiede2ccc2009-04-08 13:14:19 -0300374 * The largest possible initial count is 0; this is equivalent
375 * to 216 for binary counting and 104 for BCD counting.
Sheng Yang78376992008-01-28 05:10:22 +0800376 */
377 if (val == 0)
378 val = 0x10000;
379
Sheng Yang78376992008-01-28 05:10:22 +0800380 ps->channels[channel].count = val;
381
Marcelo Tosattifd668422009-02-23 10:57:40 -0300382 if (channel != 0) {
383 ps->channels[channel].count_load_time = ktime_get();
Sheng Yang78376992008-01-28 05:10:22 +0800384 return;
Marcelo Tosattifd668422009-02-23 10:57:40 -0300385 }
Sheng Yang78376992008-01-28 05:10:22 +0800386
387 /* Two types of timer
388 * mode 1 is one shot, mode 2 is period, otherwise del timer */
389 switch (ps->channels[0].mode) {
Marcelo Tosattiede2ccc2009-04-08 13:14:19 -0300390 case 0:
Sheng Yang78376992008-01-28 05:10:22 +0800391 case 1:
Marcelo Tosattiece15ba2008-04-30 13:23:54 -0300392 /* FIXME: enhance mode 4 precision */
393 case 4:
Radim Krčmář09edea72016-03-02 22:56:43 +0100394 create_pit_timer(pit, val, 0);
Sheng Yang78376992008-01-28 05:10:22 +0800395 break;
396 case 2:
Aurelien Jarnof6975542008-05-02 17:02:23 +0200397 case 3:
Radim Krčmář09edea72016-03-02 22:56:43 +0100398 create_pit_timer(pit, val, 1);
Sheng Yang78376992008-01-28 05:10:22 +0800399 break;
400 default:
Radim Krčmář09edea72016-03-02 22:56:43 +0100401 destroy_pit_timer(pit);
Sheng Yang78376992008-01-28 05:10:22 +0800402 }
403}
404
Radim Krčmář09edea72016-03-02 22:56:43 +0100405void kvm_pit_load_count(struct kvm_pit *pit, int channel, u32 val,
406 int hpet_legacy_start)
Sheng Yange0f63cb2008-03-04 00:50:59 +0800407{
Beth Kone9f42752009-07-07 11:50:38 -0400408 u8 saved_mode;
Radim Krčmářb69d9202016-03-02 22:56:42 +0100409
Radim Krčmář09edea72016-03-02 22:56:43 +0100410 WARN_ON_ONCE(!mutex_is_locked(&pit->pit_state.lock));
Radim Krčmářb69d9202016-03-02 22:56:42 +0100411
Beth Kone9f42752009-07-07 11:50:38 -0400412 if (hpet_legacy_start) {
413 /* save existing mode for later reenablement */
Paolo Bonzinie5e57e72016-01-07 13:50:38 +0100414 WARN_ON(channel != 0);
Radim Krčmář09edea72016-03-02 22:56:43 +0100415 saved_mode = pit->pit_state.channels[0].mode;
416 pit->pit_state.channels[0].mode = 0xff; /* disable timer */
417 pit_load_count(pit, channel, val);
418 pit->pit_state.channels[0].mode = saved_mode;
Beth Kone9f42752009-07-07 11:50:38 -0400419 } else {
Radim Krčmář09edea72016-03-02 22:56:43 +0100420 pit_load_count(pit, channel, val);
Beth Kone9f42752009-07-07 11:50:38 -0400421 }
Sheng Yange0f63cb2008-03-04 00:50:59 +0800422}
423
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400424static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
425{
426 return container_of(dev, struct kvm_pit, dev);
427}
428
429static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
430{
431 return container_of(dev, struct kvm_pit, speaker_dev);
432}
433
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300434static inline int pit_in_range(gpa_t addr)
435{
436 return ((addr >= KVM_PIT_BASE_ADDRESS) &&
437 (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
438}
439
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000440static int pit_ioport_write(struct kvm_vcpu *vcpu,
441 struct kvm_io_device *this,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300442 gpa_t addr, int len, const void *data)
Sheng Yang78376992008-01-28 05:10:22 +0800443{
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400444 struct kvm_pit *pit = dev_to_pit(this);
Sheng Yang78376992008-01-28 05:10:22 +0800445 struct kvm_kpit_state *pit_state = &pit->pit_state;
Sheng Yang78376992008-01-28 05:10:22 +0800446 int channel, access;
447 struct kvm_kpit_channel_state *s;
448 u32 val = *(u32 *) data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300449 if (!pit_in_range(addr))
450 return -EOPNOTSUPP;
Sheng Yang78376992008-01-28 05:10:22 +0800451
452 val &= 0xff;
453 addr &= KVM_PIT_CHANNEL_MASK;
454
455 mutex_lock(&pit_state->lock);
456
457 if (val != 0)
Joe Perchesa78d96262009-12-09 10:45:35 -0800458 pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
459 (unsigned int)addr, len, val);
Sheng Yang78376992008-01-28 05:10:22 +0800460
461 if (addr == 3) {
462 channel = val >> 6;
463 if (channel == 3) {
464 /* Read-Back Command. */
465 for (channel = 0; channel < 3; channel++) {
Sheng Yang78376992008-01-28 05:10:22 +0800466 if (val & (2 << channel)) {
467 if (!(val & 0x20))
Radim Krčmář09edea72016-03-02 22:56:43 +0100468 pit_latch_count(pit, channel);
Sheng Yang78376992008-01-28 05:10:22 +0800469 if (!(val & 0x10))
Radim Krčmář09edea72016-03-02 22:56:43 +0100470 pit_latch_status(pit, channel);
Sheng Yang78376992008-01-28 05:10:22 +0800471 }
472 }
473 } else {
474 /* Select Counter <channel>. */
475 s = &pit_state->channels[channel];
476 access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
477 if (access == 0) {
Radim Krčmář09edea72016-03-02 22:56:43 +0100478 pit_latch_count(pit, channel);
Sheng Yang78376992008-01-28 05:10:22 +0800479 } else {
480 s->rw_mode = access;
481 s->read_state = access;
482 s->write_state = access;
483 s->mode = (val >> 1) & 7;
484 if (s->mode > 5)
485 s->mode -= 4;
486 s->bcd = val & 1;
487 }
488 }
489 } else {
490 /* Write Count. */
491 s = &pit_state->channels[addr];
492 switch (s->write_state) {
493 default:
494 case RW_STATE_LSB:
Radim Krčmář09edea72016-03-02 22:56:43 +0100495 pit_load_count(pit, addr, val);
Sheng Yang78376992008-01-28 05:10:22 +0800496 break;
497 case RW_STATE_MSB:
Radim Krčmář09edea72016-03-02 22:56:43 +0100498 pit_load_count(pit, addr, val << 8);
Sheng Yang78376992008-01-28 05:10:22 +0800499 break;
500 case RW_STATE_WORD0:
501 s->write_latch = val;
502 s->write_state = RW_STATE_WORD1;
503 break;
504 case RW_STATE_WORD1:
Radim Krčmář09edea72016-03-02 22:56:43 +0100505 pit_load_count(pit, addr, s->write_latch | (val << 8));
Sheng Yang78376992008-01-28 05:10:22 +0800506 s->write_state = RW_STATE_WORD0;
507 break;
508 }
509 }
510
511 mutex_unlock(&pit_state->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300512 return 0;
Sheng Yang78376992008-01-28 05:10:22 +0800513}
514
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000515static int pit_ioport_read(struct kvm_vcpu *vcpu,
516 struct kvm_io_device *this,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300517 gpa_t addr, int len, void *data)
Sheng Yang78376992008-01-28 05:10:22 +0800518{
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400519 struct kvm_pit *pit = dev_to_pit(this);
Sheng Yang78376992008-01-28 05:10:22 +0800520 struct kvm_kpit_state *pit_state = &pit->pit_state;
Sheng Yang78376992008-01-28 05:10:22 +0800521 int ret, count;
522 struct kvm_kpit_channel_state *s;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300523 if (!pit_in_range(addr))
524 return -EOPNOTSUPP;
Sheng Yang78376992008-01-28 05:10:22 +0800525
526 addr &= KVM_PIT_CHANNEL_MASK;
Marcelo Tosattiee73f652010-01-29 17:28:41 -0200527 if (addr == 3)
528 return 0;
529
Sheng Yang78376992008-01-28 05:10:22 +0800530 s = &pit_state->channels[addr];
531
532 mutex_lock(&pit_state->lock);
533
534 if (s->status_latched) {
535 s->status_latched = 0;
536 ret = s->status;
537 } else if (s->count_latched) {
538 switch (s->count_latched) {
539 default:
540 case RW_STATE_LSB:
541 ret = s->latched_count & 0xff;
542 s->count_latched = 0;
543 break;
544 case RW_STATE_MSB:
545 ret = s->latched_count >> 8;
546 s->count_latched = 0;
547 break;
548 case RW_STATE_WORD0:
549 ret = s->latched_count & 0xff;
550 s->count_latched = RW_STATE_MSB;
551 break;
552 }
553 } else {
554 switch (s->read_state) {
555 default:
556 case RW_STATE_LSB:
Radim Krčmář09edea72016-03-02 22:56:43 +0100557 count = pit_get_count(pit, addr);
Sheng Yang78376992008-01-28 05:10:22 +0800558 ret = count & 0xff;
559 break;
560 case RW_STATE_MSB:
Radim Krčmář09edea72016-03-02 22:56:43 +0100561 count = pit_get_count(pit, addr);
Sheng Yang78376992008-01-28 05:10:22 +0800562 ret = (count >> 8) & 0xff;
563 break;
564 case RW_STATE_WORD0:
Radim Krčmář09edea72016-03-02 22:56:43 +0100565 count = pit_get_count(pit, addr);
Sheng Yang78376992008-01-28 05:10:22 +0800566 ret = count & 0xff;
567 s->read_state = RW_STATE_WORD1;
568 break;
569 case RW_STATE_WORD1:
Radim Krčmář09edea72016-03-02 22:56:43 +0100570 count = pit_get_count(pit, addr);
Sheng Yang78376992008-01-28 05:10:22 +0800571 ret = (count >> 8) & 0xff;
572 s->read_state = RW_STATE_WORD0;
573 break;
574 }
575 }
576
577 if (len > sizeof(ret))
578 len = sizeof(ret);
579 memcpy(data, (char *)&ret, len);
580
581 mutex_unlock(&pit_state->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300582 return 0;
Sheng Yang78376992008-01-28 05:10:22 +0800583}
584
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000585static int speaker_ioport_write(struct kvm_vcpu *vcpu,
586 struct kvm_io_device *this,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300587 gpa_t addr, int len, const void *data)
Sheng Yang78376992008-01-28 05:10:22 +0800588{
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400589 struct kvm_pit *pit = speaker_to_pit(this);
Sheng Yang78376992008-01-28 05:10:22 +0800590 struct kvm_kpit_state *pit_state = &pit->pit_state;
Sheng Yang78376992008-01-28 05:10:22 +0800591 u32 val = *(u32 *) data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300592 if (addr != KVM_SPEAKER_BASE_ADDRESS)
593 return -EOPNOTSUPP;
Sheng Yang78376992008-01-28 05:10:22 +0800594
595 mutex_lock(&pit_state->lock);
596 pit_state->speaker_data_on = (val >> 1) & 1;
Radim Krčmář09edea72016-03-02 22:56:43 +0100597 pit_set_gate(pit, 2, val & 1);
Sheng Yang78376992008-01-28 05:10:22 +0800598 mutex_unlock(&pit_state->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300599 return 0;
Sheng Yang78376992008-01-28 05:10:22 +0800600}
601
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000602static int speaker_ioport_read(struct kvm_vcpu *vcpu,
603 struct kvm_io_device *this,
604 gpa_t addr, int len, void *data)
Sheng Yang78376992008-01-28 05:10:22 +0800605{
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400606 struct kvm_pit *pit = speaker_to_pit(this);
Sheng Yang78376992008-01-28 05:10:22 +0800607 struct kvm_kpit_state *pit_state = &pit->pit_state;
Sheng Yang78376992008-01-28 05:10:22 +0800608 unsigned int refresh_clock;
609 int ret;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300610 if (addr != KVM_SPEAKER_BASE_ADDRESS)
611 return -EOPNOTSUPP;
Sheng Yang78376992008-01-28 05:10:22 +0800612
613 /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
614 refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
615
616 mutex_lock(&pit_state->lock);
Radim Krčmář09edea72016-03-02 22:56:43 +0100617 ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(pit, 2) |
618 (pit_get_out(pit, 2) << 5) | (refresh_clock << 4));
Sheng Yang78376992008-01-28 05:10:22 +0800619 if (len > sizeof(ret))
620 len = sizeof(ret);
621 memcpy(data, (char *)&ret, len);
622 mutex_unlock(&pit_state->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300623 return 0;
Sheng Yang78376992008-01-28 05:10:22 +0800624}
625
Radim Krčmářb39c90b2016-03-02 22:56:44 +0100626static void kvm_pit_reset(struct kvm_pit *pit)
Sheng Yang78376992008-01-28 05:10:22 +0800627{
628 int i;
Sheng Yang308b0f22008-03-13 10:22:26 +0800629 struct kvm_kpit_channel_state *c;
630
Beth Kone9f42752009-07-07 11:50:38 -0400631 pit->pit_state.flags = 0;
Sheng Yang308b0f22008-03-13 10:22:26 +0800632 for (i = 0; i < 3; i++) {
633 c = &pit->pit_state.channels[i];
634 c->mode = 0xff;
635 c->gate = (i != 2);
Radim Krčmář09edea72016-03-02 22:56:43 +0100636 pit_load_count(pit, i, 0);
Sheng Yang308b0f22008-03-13 10:22:26 +0800637 }
Sheng Yang308b0f22008-03-13 10:22:26 +0800638
Radim Krčmářfd700a02016-03-02 22:56:40 +0100639 kvm_pit_reset_reinject(pit);
Sheng Yang308b0f22008-03-13 10:22:26 +0800640}
641
Avi Kivity4780c6592009-01-04 18:06:06 +0200642static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
643{
644 struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
645
Radim Krčmářfd700a02016-03-02 22:56:40 +0100646 if (!mask)
647 kvm_pit_reset_reinject(pit);
Avi Kivity4780c6592009-01-04 18:06:06 +0200648}
649
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400650static const struct kvm_io_device_ops pit_dev_ops = {
651 .read = pit_ioport_read,
652 .write = pit_ioport_write,
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400653};
654
655static const struct kvm_io_device_ops speaker_dev_ops = {
656 .read = speaker_ioport_read,
657 .write = speaker_ioport_write,
Gregory Haskinsd76685c42009-06-01 12:54:50 -0400658};
659
Jan Kiszkac5ff41c2009-05-14 22:42:53 +0200660struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
Sheng Yang308b0f22008-03-13 10:22:26 +0800661{
Sheng Yang78376992008-01-28 05:10:22 +0800662 struct kvm_pit *pit;
663 struct kvm_kpit_state *pit_state;
Jan Kiszkab6ddf052012-04-24 16:40:17 +0200664 struct pid *pid;
665 pid_t pid_nr;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400666 int ret;
Sheng Yang78376992008-01-28 05:10:22 +0800667
Ben Gardon254272c2019-02-11 11:02:50 -0800668 pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL_ACCOUNT);
Sheng Yang78376992008-01-28 05:10:22 +0800669 if (!pit)
670 return NULL;
671
Sheng Yang5550af42008-10-15 20:15:06 +0800672 pit->irq_source_id = kvm_request_irq_source_id(kvm);
Radim Krčmář10d24822016-03-02 22:56:46 +0100673 if (pit->irq_source_id < 0)
674 goto fail_request;
Sheng Yang5550af42008-10-15 20:15:06 +0800675
Sheng Yang78376992008-01-28 05:10:22 +0800676 mutex_init(&pit->pit_state.lock);
Chris Lalancette33572ac2010-06-16 17:11:11 -0400677
Jan Kiszkab6ddf052012-04-24 16:40:17 +0200678 pid = get_pid(task_tgid(current));
679 pid_nr = pid_vnr(pid);
680 put_pid(pid);
681
Petr Mladek36da91b2016-10-19 13:50:47 +0200682 pit->worker = kthread_create_worker(0, "kvm-pit/%d", pid_nr);
683 if (IS_ERR(pit->worker))
Radim Krčmář10d24822016-03-02 22:56:46 +0100684 goto fail_kthread;
685
Petr Mladek39891442016-10-11 13:55:20 -0700686 kthread_init_work(&pit->expired, pit_do_work);
Sheng Yang78376992008-01-28 05:10:22 +0800687
Sheng Yang78376992008-01-28 05:10:22 +0800688 pit->kvm = kvm;
689
690 pit_state = &pit->pit_state;
Avi Kivity26ef1922012-07-26 18:01:53 +0300691 hrtimer_init(&pit_state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Radim Krčmářab4c1472016-03-02 22:56:51 +0100692 pit_state->timer.function = pit_timer_fn;
Radim Krčmář71474e22016-03-02 22:56:45 +0100693
Marcelo Tosatti3cf57fe2008-07-26 17:01:01 -0300694 pit_state->irq_ack_notifier.gsi = 0;
695 pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
Radim Krčmář71474e22016-03-02 22:56:45 +0100696 pit->mask_notifier.func = pit_mask_notifer;
Sheng Yang78376992008-01-28 05:10:22 +0800697
Sheng Yang308b0f22008-03-13 10:22:26 +0800698 kvm_pit_reset(pit);
Sheng Yang78376992008-01-28 05:10:22 +0800699
Radim Krčmář71474e22016-03-02 22:56:45 +0100700 kvm_pit_set_reinject(pit, true);
Avi Kivity4780c6592009-01-04 18:06:06 +0200701
Paolo Bonzini250715a2016-06-01 14:09:24 +0200702 mutex_lock(&kvm->slots_lock);
Gregory Haskins6b66ac12009-06-01 12:54:56 -0400703 kvm_iodevice_init(&pit->dev, &pit_dev_ops);
Sasha Levin743eeb02011-07-27 16:00:48 +0300704 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS,
705 KVM_PIT_MEM_LENGTH, &pit->dev);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400706 if (ret < 0)
Radim Krčmář10d24822016-03-02 22:56:46 +0100707 goto fail_register_pit;
Gregory Haskins6b66ac12009-06-01 12:54:56 -0400708
709 if (flags & KVM_PIT_SPEAKER_DUMMY) {
710 kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200711 ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
Sasha Levin743eeb02011-07-27 16:00:48 +0300712 KVM_SPEAKER_BASE_ADDRESS, 4,
713 &pit->speaker_dev);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400714 if (ret < 0)
Radim Krčmář10d24822016-03-02 22:56:46 +0100715 goto fail_register_speaker;
Gregory Haskins6b66ac12009-06-01 12:54:56 -0400716 }
Paolo Bonzini250715a2016-06-01 14:09:24 +0200717 mutex_unlock(&kvm->slots_lock);
Gregory Haskins6b66ac12009-06-01 12:54:56 -0400718
Sheng Yang78376992008-01-28 05:10:22 +0800719 return pit;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400720
Radim Krčmář10d24822016-03-02 22:56:46 +0100721fail_register_speaker:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200722 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
Radim Krčmář10d24822016-03-02 22:56:46 +0100723fail_register_pit:
Paolo Bonzini250715a2016-06-01 14:09:24 +0200724 mutex_unlock(&kvm->slots_lock);
Radim Krčmář71474e22016-03-02 22:56:45 +0100725 kvm_pit_set_reinject(pit, false);
Petr Mladek36da91b2016-10-19 13:50:47 +0200726 kthread_destroy_worker(pit->worker);
Radim Krčmář10d24822016-03-02 22:56:46 +0100727fail_kthread:
728 kvm_free_irq_source_id(kvm, pit->irq_source_id);
729fail_request:
Gregory Haskins090b7af2009-07-07 17:08:44 -0400730 kfree(pit);
731 return NULL;
Sheng Yang78376992008-01-28 05:10:22 +0800732}
733
734void kvm_free_pit(struct kvm *kvm)
735{
Radim Krčmář08e5ccf32016-03-02 22:56:47 +0100736 struct kvm_pit *pit = kvm->arch.vpit;
Sheng Yang78376992008-01-28 05:10:22 +0800737
Radim Krčmář08e5ccf32016-03-02 22:56:47 +0100738 if (pit) {
Radim Krčmářfb530722017-07-10 20:53:28 +0200739 mutex_lock(&kvm->slots_lock);
Radim Krčmář08e5ccf32016-03-02 22:56:47 +0100740 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
741 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->speaker_dev);
Radim Krčmářfb530722017-07-10 20:53:28 +0200742 mutex_unlock(&kvm->slots_lock);
Radim Krčmář08e5ccf32016-03-02 22:56:47 +0100743 kvm_pit_set_reinject(pit, false);
744 hrtimer_cancel(&pit->pit_state.timer);
Petr Mladek36da91b2016-10-19 13:50:47 +0200745 kthread_destroy_worker(pit->worker);
Radim Krčmář08e5ccf32016-03-02 22:56:47 +0100746 kvm_free_irq_source_id(kvm, pit->irq_source_id);
747 kfree(pit);
Sheng Yang78376992008-01-28 05:10:22 +0800748 }
749}