Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 1 | /* |
| 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 Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2009 Red Hat, Inc. and/or its affiliates. |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 9 | * |
| 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 Perches | a78d9626 | 2009-12-09 10:45:35 -0800 | [diff] [blame] | 33 | #define pr_fmt(fmt) "pit: " fmt |
| 34 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 35 | #include <linux/kvm_host.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 37 | |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 38 | #include "ioapic.h" |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 39 | #include "irq.h" |
| 40 | #include "i8254.h" |
Marcelo Tosatti | 9ed96e8 | 2014-01-06 12:00:02 -0200 | [diff] [blame] | 41 | #include "x86.h" |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 42 | |
| 43 | #ifndef CONFIG_X86_64 |
Roman Zippel | 6f6d6a1 | 2008-05-01 04:34:28 -0700 | [diff] [blame] | 44 | #define mod_64(x, y) ((x) - (y) * div64_u64(x, y)) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 45 | #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ář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 54 | static void pit_set_gate(struct kvm_pit *pit, int channel, u32 val) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 55 | { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 56 | struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel]; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 57 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 58 | 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ář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 77 | static int pit_get_gate(struct kvm_pit *pit, int channel) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 78 | { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 79 | return pit->pit_state.channels[channel].gate; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 82 | static s64 __kpit_elapsed(struct kvm_pit *pit) |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 83 | { |
| 84 | s64 elapsed; |
| 85 | ktime_t remaining; |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 86 | struct kvm_kpit_state *ps = &pit->pit_state; |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 87 | |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 88 | if (!ps->period) |
Marcelo Tosatti | 0ff7787 | 2009-07-02 20:02:15 -0300 | [diff] [blame] | 89 | return 0; |
| 90 | |
Marcelo Tosatti | ede2ccc | 2009-04-08 13:14:19 -0300 | [diff] [blame] | 91 | /* |
| 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 Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 100 | remaining = hrtimer_get_remaining(&ps->timer); |
| 101 | elapsed = ps->period - ktime_to_ns(remaining); |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 102 | |
| 103 | return elapsed; |
| 104 | } |
| 105 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 106 | static s64 kpit_elapsed(struct kvm_pit *pit, struct kvm_kpit_channel_state *c, |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 107 | int channel) |
| 108 | { |
| 109 | if (channel == 0) |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 110 | return __kpit_elapsed(pit); |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 111 | |
| 112 | return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time)); |
| 113 | } |
| 114 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 115 | static int pit_get_count(struct kvm_pit *pit, int channel) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 116 | { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 117 | struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel]; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 118 | s64 d, t; |
| 119 | int counter; |
| 120 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 121 | t = kpit_elapsed(pit, c, channel); |
Paolo Bonzini | 0e4d441 | 2016-03-04 09:28:41 +0100 | [diff] [blame] | 122 | d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 123 | |
| 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ář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 142 | static int pit_get_out(struct kvm_pit *pit, int channel) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 143 | { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 144 | struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel]; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 145 | s64 d, t; |
| 146 | int out; |
| 147 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 148 | t = kpit_elapsed(pit, c, channel); |
Paolo Bonzini | 0e4d441 | 2016-03-04 09:28:41 +0100 | [diff] [blame] | 149 | d = mul_u64_u32_div(t, KVM_PIT_FREQ, NSEC_PER_SEC); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 150 | |
| 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ář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 174 | static void pit_latch_count(struct kvm_pit *pit, int channel) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 175 | { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 176 | struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel]; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 177 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 178 | if (!c->count_latched) { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 179 | c->latched_count = pit_get_count(pit, channel); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 180 | c->count_latched = c->rw_mode; |
| 181 | } |
| 182 | } |
| 183 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 184 | static void pit_latch_status(struct kvm_pit *pit, int channel) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 185 | { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 186 | struct kvm_kpit_channel_state *c = &pit->pit_state.channels[channel]; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 187 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 188 | if (!c->status_latched) { |
| 189 | /* TODO: Return NULL COUNT (bit 6). */ |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 190 | c->status = ((pit_get_out(pit, channel) << 7) | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 191 | (c->rw_mode << 4) | |
| 192 | (c->mode << 1) | |
| 193 | c->bcd); |
| 194 | c->status_latched = 1; |
| 195 | } |
| 196 | } |
| 197 | |
Radim Krčmář | a3e1311 | 2016-03-02 22:56:48 +0100 | [diff] [blame] | 198 | static 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 Harrison | ee032c9 | 2008-08-11 16:54:20 -0700 | [diff] [blame] | 203 | static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian) |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 204 | { |
| 205 | struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state, |
| 206 | irq_ack_notifier); |
Radim Krčmář | a3e1311 | 2016-03-02 22:56:48 +0100 | [diff] [blame] | 207 | struct kvm_pit *pit = pit_state_to_pit(ps); |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 208 | |
Radim Krčmář | ddf5450 | 2016-03-02 22:56:41 +0100 | [diff] [blame] | 209 | 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ář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 214 | if (atomic_dec_if_positive(&ps->pending) > 0) |
Petr Mladek | 36da91b | 2016-10-19 13:50:47 +0200 | [diff] [blame] | 215 | kthread_queue_work(pit->worker, &pit->expired); |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 216 | } |
| 217 | |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 218 | void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu) |
| 219 | { |
| 220 | struct kvm_pit *pit = vcpu->kvm->arch.vpit; |
| 221 | struct hrtimer *timer; |
| 222 | |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 223 | if (!kvm_vcpu_is_bsp(vcpu) || !pit) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 224 | return; |
| 225 | |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 226 | timer = &pit->pit_state.timer; |
Andy Honig | 2febc83 | 2014-08-27 14:42:54 -0700 | [diff] [blame] | 227 | mutex_lock(&pit->pit_state.lock); |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 228 | if (hrtimer_cancel(timer)) |
Arjan van de Ven | beb20d52 | 2008-09-01 14:55:57 -0700 | [diff] [blame] | 229 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS); |
Andy Honig | 2febc83 | 2014-08-27 14:42:54 -0700 | [diff] [blame] | 230 | mutex_unlock(&pit->pit_state.lock); |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 233 | static void destroy_pit_timer(struct kvm_pit *pit) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 234 | { |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 235 | hrtimer_cancel(&pit->pit_state.timer); |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 236 | kthread_flush_work(&pit->expired); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Jan Kiszka | b6ddf05 | 2012-04-24 16:40:17 +0200 | [diff] [blame] | 239 | static void pit_do_work(struct kthread_work *work) |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 240 | { |
| 241 | struct kvm_pit *pit = container_of(work, struct kvm_pit, expired); |
| 242 | struct kvm *kvm = pit->kvm; |
| 243 | struct kvm_vcpu *vcpu; |
| 244 | int i; |
| 245 | struct kvm_kpit_state *ps = &pit->pit_state; |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 246 | |
Radim Krčmář | a0aace5 | 2016-03-02 22:56:52 +0100 | [diff] [blame] | 247 | if (atomic_read(&ps->reinject) && !atomic_xchg(&ps->irq_ack, 0)) |
Radim Krčmář | ddf5450 | 2016-03-02 22:56:41 +0100 | [diff] [blame] | 248 | return; |
| 249 | |
Radim Krčmář | 4a2095d | 2016-03-02 22:56:49 +0100 | [diff] [blame] | 250 | kvm_set_irq(kvm, pit->irq_source_id, 0, 1, false); |
| 251 | kvm_set_irq(kvm, pit->irq_source_id, 0, 0, false); |
Radim Krčmář | ddf5450 | 2016-03-02 22:56:41 +0100 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Provides NMI watchdog support via Virtual Wire mode. |
| 255 | * The route is: PIT -> LVT0 in NMI mode. |
| 256 | * |
| 257 | * Note: Our Virtual Wire implementation does not follow |
| 258 | * the MP specification. We propagate a PIT interrupt to all |
| 259 | * VCPUs and only when LVT0 is in NMI mode. The interrupt can |
| 260 | * also be simultaneously delivered through PIC and IOAPIC. |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 261 | */ |
Radim Krčmář | ddf5450 | 2016-03-02 22:56:41 +0100 | [diff] [blame] | 262 | if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0) |
| 263 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 264 | kvm_apic_nmi_wd_deliver(vcpu); |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static enum hrtimer_restart pit_timer_fn(struct hrtimer *data) |
| 268 | { |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 269 | struct kvm_kpit_state *ps = container_of(data, struct kvm_kpit_state, timer); |
Radim Krčmář | a3e1311 | 2016-03-02 22:56:48 +0100 | [diff] [blame] | 270 | struct kvm_pit *pt = pit_state_to_pit(ps); |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 271 | |
Radim Krčmář | a0aace5 | 2016-03-02 22:56:52 +0100 | [diff] [blame] | 272 | if (atomic_read(&ps->reinject)) |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 273 | atomic_inc(&ps->pending); |
Radim Krčmář | 7dd0fdf | 2016-03-02 22:56:38 +0100 | [diff] [blame] | 274 | |
Petr Mladek | 36da91b | 2016-10-19 13:50:47 +0200 | [diff] [blame] | 275 | kthread_queue_work(pt->worker, &pt->expired); |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 276 | |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 277 | if (ps->is_periodic) { |
| 278 | hrtimer_add_expires_ns(&ps->timer, ps->period); |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 279 | return HRTIMER_RESTART; |
| 280 | } else |
| 281 | return HRTIMER_NORESTART; |
| 282 | } |
| 283 | |
Radim Krčmář | fd700a0 | 2016-03-02 22:56:40 +0100 | [diff] [blame] | 284 | static inline void kvm_pit_reset_reinject(struct kvm_pit *pit) |
| 285 | { |
| 286 | atomic_set(&pit->pit_state.pending, 0); |
Radim Krčmář | ddf5450 | 2016-03-02 22:56:41 +0100 | [diff] [blame] | 287 | atomic_set(&pit->pit_state.irq_ack, 1); |
Radim Krčmář | fd700a0 | 2016-03-02 22:56:40 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 290 | void kvm_pit_set_reinject(struct kvm_pit *pit, bool reinject) |
| 291 | { |
| 292 | struct kvm_kpit_state *ps = &pit->pit_state; |
| 293 | struct kvm *kvm = pit->kvm; |
| 294 | |
Radim Krčmář | a0aace5 | 2016-03-02 22:56:52 +0100 | [diff] [blame] | 295 | if (atomic_read(&ps->reinject) == reinject) |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 296 | return; |
| 297 | |
Suravee Suthikulpanit | e2ed407 | 2019-11-14 14:15:16 -0600 | [diff] [blame] | 298 | /* |
| 299 | * AMD SVM AVIC accelerates EOI write and does not trap. |
| 300 | * This cause in-kernel PIT re-inject mode to fail |
| 301 | * since it checks ps->irq_ack before kvm_set_irq() |
| 302 | * and relies on the ack notifier to timely queue |
| 303 | * the pt->worker work iterm and reinject the missed tick. |
| 304 | * So, deactivate APICv when PIT is in reinject mode. |
| 305 | */ |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 306 | if (reinject) { |
Suravee Suthikulpanit | e2ed407 | 2019-11-14 14:15:16 -0600 | [diff] [blame] | 307 | kvm_request_apicv_update(kvm, false, |
| 308 | APICV_INHIBIT_REASON_PIT_REINJ); |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 309 | /* The initial state is preserved while ps->reinject == 0. */ |
| 310 | kvm_pit_reset_reinject(pit); |
| 311 | kvm_register_irq_ack_notifier(kvm, &ps->irq_ack_notifier); |
| 312 | kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier); |
| 313 | } else { |
Suravee Suthikulpanit | e2ed407 | 2019-11-14 14:15:16 -0600 | [diff] [blame] | 314 | kvm_request_apicv_update(kvm, true, |
| 315 | APICV_INHIBIT_REASON_PIT_REINJ); |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 316 | kvm_unregister_irq_ack_notifier(kvm, &ps->irq_ack_notifier); |
| 317 | kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier); |
| 318 | } |
| 319 | |
Radim Krčmář | a0aace5 | 2016-03-02 22:56:52 +0100 | [diff] [blame] | 320 | atomic_set(&ps->reinject, reinject); |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 323 | static void create_pit_timer(struct kvm_pit *pit, u32 val, int is_period) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 324 | { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 325 | struct kvm_kpit_state *ps = &pit->pit_state; |
| 326 | struct kvm *kvm = pit->kvm; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 327 | s64 interval; |
| 328 | |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 329 | if (!ioapic_in_kernel(kvm) || |
| 330 | ps->flags & KVM_PIT_FLAGS_HPET_LEGACY) |
Jan Kiszka | 0924ab2 | 2011-12-14 19:25:13 +0100 | [diff] [blame] | 331 | return; |
| 332 | |
Paolo Bonzini | 0e4d441 | 2016-03-04 09:28:41 +0100 | [diff] [blame] | 333 | interval = mul_u64_u32_div(val, NSEC_PER_SEC, KVM_PIT_FREQ); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 334 | |
Joe Perches | a78d9626 | 2009-12-09 10:45:35 -0800 | [diff] [blame] | 335 | pr_debug("create pit timer, interval is %llu nsec\n", interval); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 336 | |
| 337 | /* TODO The new value only affected after the retriggered */ |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 338 | hrtimer_cancel(&ps->timer); |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 339 | kthread_flush_work(&pit->expired); |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 340 | ps->period = interval; |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 341 | ps->is_periodic = is_period; |
| 342 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 343 | kvm_pit_reset_reinject(pit); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 344 | |
Marcelo Tosatti | 9ed96e8 | 2014-01-06 12:00:02 -0200 | [diff] [blame] | 345 | /* |
| 346 | * Do not allow the guest to program periodic timers with small |
| 347 | * interval, since the hrtimers are not throttled by the host |
| 348 | * scheduler. |
| 349 | */ |
| 350 | if (ps->is_periodic) { |
| 351 | s64 min_period = min_timer_period_us * 1000LL; |
| 352 | |
| 353 | if (ps->period < min_period) { |
| 354 | pr_info_ratelimited( |
| 355 | "kvm: requested %lld ns " |
| 356 | "i8254 timer period limited to %lld ns\n", |
| 357 | ps->period, min_period); |
| 358 | ps->period = min_period; |
| 359 | } |
| 360 | } |
| 361 | |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 362 | hrtimer_start(&ps->timer, ktime_add_ns(ktime_get(), interval), |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 363 | HRTIMER_MODE_ABS); |
| 364 | } |
| 365 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 366 | static void pit_load_count(struct kvm_pit *pit, int channel, u32 val) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 367 | { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 368 | struct kvm_kpit_state *ps = &pit->pit_state; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 369 | |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 370 | pr_debug("load_count val is %u, channel is %d\n", val, channel); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 371 | |
| 372 | /* |
Marcelo Tosatti | ede2ccc | 2009-04-08 13:14:19 -0300 | [diff] [blame] | 373 | * The largest possible initial count is 0; this is equivalent |
| 374 | * to 216 for binary counting and 104 for BCD counting. |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 375 | */ |
| 376 | if (val == 0) |
| 377 | val = 0x10000; |
| 378 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 379 | ps->channels[channel].count = val; |
| 380 | |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 381 | if (channel != 0) { |
| 382 | ps->channels[channel].count_load_time = ktime_get(); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 383 | return; |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 384 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 385 | |
| 386 | /* Two types of timer |
| 387 | * mode 1 is one shot, mode 2 is period, otherwise del timer */ |
| 388 | switch (ps->channels[0].mode) { |
Marcelo Tosatti | ede2ccc | 2009-04-08 13:14:19 -0300 | [diff] [blame] | 389 | case 0: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 390 | case 1: |
Marcelo Tosatti | ece15ba | 2008-04-30 13:23:54 -0300 | [diff] [blame] | 391 | /* FIXME: enhance mode 4 precision */ |
| 392 | case 4: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 393 | create_pit_timer(pit, val, 0); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 394 | break; |
| 395 | case 2: |
Aurelien Jarno | f697554 | 2008-05-02 17:02:23 +0200 | [diff] [blame] | 396 | case 3: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 397 | create_pit_timer(pit, val, 1); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 398 | break; |
| 399 | default: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 400 | destroy_pit_timer(pit); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 404 | void kvm_pit_load_count(struct kvm_pit *pit, int channel, u32 val, |
| 405 | int hpet_legacy_start) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 406 | { |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 407 | u8 saved_mode; |
Radim Krčmář | b69d920 | 2016-03-02 22:56:42 +0100 | [diff] [blame] | 408 | |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 409 | WARN_ON_ONCE(!mutex_is_locked(&pit->pit_state.lock)); |
Radim Krčmář | b69d920 | 2016-03-02 22:56:42 +0100 | [diff] [blame] | 410 | |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 411 | if (hpet_legacy_start) { |
| 412 | /* save existing mode for later reenablement */ |
Paolo Bonzini | e5e57e7 | 2016-01-07 13:50:38 +0100 | [diff] [blame] | 413 | WARN_ON(channel != 0); |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 414 | saved_mode = pit->pit_state.channels[0].mode; |
| 415 | pit->pit_state.channels[0].mode = 0xff; /* disable timer */ |
| 416 | pit_load_count(pit, channel, val); |
| 417 | pit->pit_state.channels[0].mode = saved_mode; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 418 | } else { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 419 | pit_load_count(pit, channel, val); |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 420 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 421 | } |
| 422 | |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 423 | static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev) |
| 424 | { |
| 425 | return container_of(dev, struct kvm_pit, dev); |
| 426 | } |
| 427 | |
| 428 | static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev) |
| 429 | { |
| 430 | return container_of(dev, struct kvm_pit, speaker_dev); |
| 431 | } |
| 432 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 433 | static inline int pit_in_range(gpa_t addr) |
| 434 | { |
| 435 | return ((addr >= KVM_PIT_BASE_ADDRESS) && |
| 436 | (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH)); |
| 437 | } |
| 438 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 439 | static int pit_ioport_write(struct kvm_vcpu *vcpu, |
| 440 | struct kvm_io_device *this, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 441 | gpa_t addr, int len, const void *data) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 442 | { |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 443 | struct kvm_pit *pit = dev_to_pit(this); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 444 | struct kvm_kpit_state *pit_state = &pit->pit_state; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 445 | int channel, access; |
| 446 | struct kvm_kpit_channel_state *s; |
| 447 | u32 val = *(u32 *) data; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 448 | if (!pit_in_range(addr)) |
| 449 | return -EOPNOTSUPP; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 450 | |
| 451 | val &= 0xff; |
| 452 | addr &= KVM_PIT_CHANNEL_MASK; |
| 453 | |
| 454 | mutex_lock(&pit_state->lock); |
| 455 | |
| 456 | if (val != 0) |
Joe Perches | a78d9626 | 2009-12-09 10:45:35 -0800 | [diff] [blame] | 457 | pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n", |
| 458 | (unsigned int)addr, len, val); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 459 | |
| 460 | if (addr == 3) { |
| 461 | channel = val >> 6; |
| 462 | if (channel == 3) { |
| 463 | /* Read-Back Command. */ |
| 464 | for (channel = 0; channel < 3; channel++) { |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 465 | if (val & (2 << channel)) { |
| 466 | if (!(val & 0x20)) |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 467 | pit_latch_count(pit, channel); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 468 | if (!(val & 0x10)) |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 469 | pit_latch_status(pit, channel); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | } else { |
| 473 | /* Select Counter <channel>. */ |
| 474 | s = &pit_state->channels[channel]; |
| 475 | access = (val >> 4) & KVM_PIT_CHANNEL_MASK; |
| 476 | if (access == 0) { |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 477 | pit_latch_count(pit, channel); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 478 | } else { |
| 479 | s->rw_mode = access; |
| 480 | s->read_state = access; |
| 481 | s->write_state = access; |
| 482 | s->mode = (val >> 1) & 7; |
| 483 | if (s->mode > 5) |
| 484 | s->mode -= 4; |
| 485 | s->bcd = val & 1; |
| 486 | } |
| 487 | } |
| 488 | } else { |
| 489 | /* Write Count. */ |
| 490 | s = &pit_state->channels[addr]; |
| 491 | switch (s->write_state) { |
| 492 | default: |
| 493 | case RW_STATE_LSB: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 494 | pit_load_count(pit, addr, val); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 495 | break; |
| 496 | case RW_STATE_MSB: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 497 | pit_load_count(pit, addr, val << 8); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 498 | break; |
| 499 | case RW_STATE_WORD0: |
| 500 | s->write_latch = val; |
| 501 | s->write_state = RW_STATE_WORD1; |
| 502 | break; |
| 503 | case RW_STATE_WORD1: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 504 | pit_load_count(pit, addr, s->write_latch | (val << 8)); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 505 | s->write_state = RW_STATE_WORD0; |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | mutex_unlock(&pit_state->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 511 | return 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 512 | } |
| 513 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 514 | static int pit_ioport_read(struct kvm_vcpu *vcpu, |
| 515 | struct kvm_io_device *this, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 516 | gpa_t addr, int len, void *data) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 517 | { |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 518 | struct kvm_pit *pit = dev_to_pit(this); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 519 | struct kvm_kpit_state *pit_state = &pit->pit_state; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 520 | int ret, count; |
| 521 | struct kvm_kpit_channel_state *s; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 522 | if (!pit_in_range(addr)) |
| 523 | return -EOPNOTSUPP; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 524 | |
| 525 | addr &= KVM_PIT_CHANNEL_MASK; |
Marcelo Tosatti | ee73f65 | 2010-01-29 17:28:41 -0200 | [diff] [blame] | 526 | if (addr == 3) |
| 527 | return 0; |
| 528 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 529 | s = &pit_state->channels[addr]; |
| 530 | |
| 531 | mutex_lock(&pit_state->lock); |
| 532 | |
| 533 | if (s->status_latched) { |
| 534 | s->status_latched = 0; |
| 535 | ret = s->status; |
| 536 | } else if (s->count_latched) { |
| 537 | switch (s->count_latched) { |
| 538 | default: |
| 539 | case RW_STATE_LSB: |
| 540 | ret = s->latched_count & 0xff; |
| 541 | s->count_latched = 0; |
| 542 | break; |
| 543 | case RW_STATE_MSB: |
| 544 | ret = s->latched_count >> 8; |
| 545 | s->count_latched = 0; |
| 546 | break; |
| 547 | case RW_STATE_WORD0: |
| 548 | ret = s->latched_count & 0xff; |
| 549 | s->count_latched = RW_STATE_MSB; |
| 550 | break; |
| 551 | } |
| 552 | } else { |
| 553 | switch (s->read_state) { |
| 554 | default: |
| 555 | case RW_STATE_LSB: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 556 | count = pit_get_count(pit, addr); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 557 | ret = count & 0xff; |
| 558 | break; |
| 559 | case RW_STATE_MSB: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 560 | count = pit_get_count(pit, addr); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 561 | ret = (count >> 8) & 0xff; |
| 562 | break; |
| 563 | case RW_STATE_WORD0: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 564 | count = pit_get_count(pit, addr); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 565 | ret = count & 0xff; |
| 566 | s->read_state = RW_STATE_WORD1; |
| 567 | break; |
| 568 | case RW_STATE_WORD1: |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 569 | count = pit_get_count(pit, addr); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 570 | ret = (count >> 8) & 0xff; |
| 571 | s->read_state = RW_STATE_WORD0; |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if (len > sizeof(ret)) |
| 577 | len = sizeof(ret); |
| 578 | memcpy(data, (char *)&ret, len); |
| 579 | |
| 580 | mutex_unlock(&pit_state->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 581 | return 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 582 | } |
| 583 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 584 | static int speaker_ioport_write(struct kvm_vcpu *vcpu, |
| 585 | struct kvm_io_device *this, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 586 | gpa_t addr, int len, const void *data) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 587 | { |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 588 | struct kvm_pit *pit = speaker_to_pit(this); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 589 | struct kvm_kpit_state *pit_state = &pit->pit_state; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 590 | u32 val = *(u32 *) data; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 591 | if (addr != KVM_SPEAKER_BASE_ADDRESS) |
| 592 | return -EOPNOTSUPP; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 593 | |
| 594 | mutex_lock(&pit_state->lock); |
| 595 | pit_state->speaker_data_on = (val >> 1) & 1; |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 596 | pit_set_gate(pit, 2, val & 1); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 597 | mutex_unlock(&pit_state->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 598 | return 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 599 | } |
| 600 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 601 | static int speaker_ioport_read(struct kvm_vcpu *vcpu, |
| 602 | struct kvm_io_device *this, |
| 603 | gpa_t addr, int len, void *data) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 604 | { |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 605 | struct kvm_pit *pit = speaker_to_pit(this); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 606 | struct kvm_kpit_state *pit_state = &pit->pit_state; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 607 | unsigned int refresh_clock; |
| 608 | int ret; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 609 | if (addr != KVM_SPEAKER_BASE_ADDRESS) |
| 610 | return -EOPNOTSUPP; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 611 | |
| 612 | /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */ |
| 613 | refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1; |
| 614 | |
| 615 | mutex_lock(&pit_state->lock); |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 616 | ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(pit, 2) | |
| 617 | (pit_get_out(pit, 2) << 5) | (refresh_clock << 4)); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 618 | if (len > sizeof(ret)) |
| 619 | len = sizeof(ret); |
| 620 | memcpy(data, (char *)&ret, len); |
| 621 | mutex_unlock(&pit_state->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 622 | return 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 623 | } |
| 624 | |
Radim Krčmář | b39c90b | 2016-03-02 22:56:44 +0100 | [diff] [blame] | 625 | static void kvm_pit_reset(struct kvm_pit *pit) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 626 | { |
| 627 | int i; |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 628 | struct kvm_kpit_channel_state *c; |
| 629 | |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 630 | pit->pit_state.flags = 0; |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 631 | for (i = 0; i < 3; i++) { |
| 632 | c = &pit->pit_state.channels[i]; |
| 633 | c->mode = 0xff; |
| 634 | c->gate = (i != 2); |
Radim Krčmář | 09edea7 | 2016-03-02 22:56:43 +0100 | [diff] [blame] | 635 | pit_load_count(pit, i, 0); |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 636 | } |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 637 | |
Radim Krčmář | fd700a0 | 2016-03-02 22:56:40 +0100 | [diff] [blame] | 638 | kvm_pit_reset_reinject(pit); |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 639 | } |
| 640 | |
Avi Kivity | 4780c659 | 2009-01-04 18:06:06 +0200 | [diff] [blame] | 641 | static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask) |
| 642 | { |
| 643 | struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier); |
| 644 | |
Radim Krčmář | fd700a0 | 2016-03-02 22:56:40 +0100 | [diff] [blame] | 645 | if (!mask) |
| 646 | kvm_pit_reset_reinject(pit); |
Avi Kivity | 4780c659 | 2009-01-04 18:06:06 +0200 | [diff] [blame] | 647 | } |
| 648 | |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 649 | static const struct kvm_io_device_ops pit_dev_ops = { |
| 650 | .read = pit_ioport_read, |
| 651 | .write = pit_ioport_write, |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | static const struct kvm_io_device_ops speaker_dev_ops = { |
| 655 | .read = speaker_ioport_read, |
| 656 | .write = speaker_ioport_write, |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 657 | }; |
| 658 | |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 659 | struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 660 | { |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 661 | struct kvm_pit *pit; |
| 662 | struct kvm_kpit_state *pit_state; |
Jan Kiszka | b6ddf05 | 2012-04-24 16:40:17 +0200 | [diff] [blame] | 663 | struct pid *pid; |
| 664 | pid_t pid_nr; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 665 | int ret; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 666 | |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 667 | pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL_ACCOUNT); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 668 | if (!pit) |
| 669 | return NULL; |
| 670 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 671 | pit->irq_source_id = kvm_request_irq_source_id(kvm); |
Radim Krčmář | 10d2482 | 2016-03-02 22:56:46 +0100 | [diff] [blame] | 672 | if (pit->irq_source_id < 0) |
| 673 | goto fail_request; |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 674 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 675 | mutex_init(&pit->pit_state.lock); |
Chris Lalancette | 33572ac | 2010-06-16 17:11:11 -0400 | [diff] [blame] | 676 | |
Jan Kiszka | b6ddf05 | 2012-04-24 16:40:17 +0200 | [diff] [blame] | 677 | pid = get_pid(task_tgid(current)); |
| 678 | pid_nr = pid_vnr(pid); |
| 679 | put_pid(pid); |
| 680 | |
Petr Mladek | 36da91b | 2016-10-19 13:50:47 +0200 | [diff] [blame] | 681 | pit->worker = kthread_create_worker(0, "kvm-pit/%d", pid_nr); |
| 682 | if (IS_ERR(pit->worker)) |
Radim Krčmář | 10d2482 | 2016-03-02 22:56:46 +0100 | [diff] [blame] | 683 | goto fail_kthread; |
| 684 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 685 | kthread_init_work(&pit->expired, pit_do_work); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 686 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 687 | pit->kvm = kvm; |
| 688 | |
| 689 | pit_state = &pit->pit_state; |
Avi Kivity | 26ef192 | 2012-07-26 18:01:53 +0300 | [diff] [blame] | 690 | hrtimer_init(&pit_state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
Radim Krčmář | ab4c147 | 2016-03-02 22:56:51 +0100 | [diff] [blame] | 691 | pit_state->timer.function = pit_timer_fn; |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 692 | |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 693 | pit_state->irq_ack_notifier.gsi = 0; |
| 694 | pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq; |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 695 | pit->mask_notifier.func = pit_mask_notifer; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 696 | |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 697 | kvm_pit_reset(pit); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 698 | |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 699 | kvm_pit_set_reinject(pit, true); |
Avi Kivity | 4780c659 | 2009-01-04 18:06:06 +0200 | [diff] [blame] | 700 | |
Paolo Bonzini | 250715a | 2016-06-01 14:09:24 +0200 | [diff] [blame] | 701 | mutex_lock(&kvm->slots_lock); |
Gregory Haskins | 6b66ac1 | 2009-06-01 12:54:56 -0400 | [diff] [blame] | 702 | kvm_iodevice_init(&pit->dev, &pit_dev_ops); |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 703 | ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS, |
| 704 | KVM_PIT_MEM_LENGTH, &pit->dev); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 705 | if (ret < 0) |
Radim Krčmář | 10d2482 | 2016-03-02 22:56:46 +0100 | [diff] [blame] | 706 | goto fail_register_pit; |
Gregory Haskins | 6b66ac1 | 2009-06-01 12:54:56 -0400 | [diff] [blame] | 707 | |
| 708 | if (flags & KVM_PIT_SPEAKER_DUMMY) { |
| 709 | kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 710 | ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 711 | KVM_SPEAKER_BASE_ADDRESS, 4, |
| 712 | &pit->speaker_dev); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 713 | if (ret < 0) |
Radim Krčmář | 10d2482 | 2016-03-02 22:56:46 +0100 | [diff] [blame] | 714 | goto fail_register_speaker; |
Gregory Haskins | 6b66ac1 | 2009-06-01 12:54:56 -0400 | [diff] [blame] | 715 | } |
Paolo Bonzini | 250715a | 2016-06-01 14:09:24 +0200 | [diff] [blame] | 716 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | 6b66ac1 | 2009-06-01 12:54:56 -0400 | [diff] [blame] | 717 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 718 | return pit; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 719 | |
Radim Krčmář | 10d2482 | 2016-03-02 22:56:46 +0100 | [diff] [blame] | 720 | fail_register_speaker: |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 721 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev); |
Radim Krčmář | 10d2482 | 2016-03-02 22:56:46 +0100 | [diff] [blame] | 722 | fail_register_pit: |
Paolo Bonzini | 250715a | 2016-06-01 14:09:24 +0200 | [diff] [blame] | 723 | mutex_unlock(&kvm->slots_lock); |
Radim Krčmář | 71474e2 | 2016-03-02 22:56:45 +0100 | [diff] [blame] | 724 | kvm_pit_set_reinject(pit, false); |
Petr Mladek | 36da91b | 2016-10-19 13:50:47 +0200 | [diff] [blame] | 725 | kthread_destroy_worker(pit->worker); |
Radim Krčmář | 10d2482 | 2016-03-02 22:56:46 +0100 | [diff] [blame] | 726 | fail_kthread: |
| 727 | kvm_free_irq_source_id(kvm, pit->irq_source_id); |
| 728 | fail_request: |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 729 | kfree(pit); |
| 730 | return NULL; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | void kvm_free_pit(struct kvm *kvm) |
| 734 | { |
Radim Krčmář | 08e5ccf3 | 2016-03-02 22:56:47 +0100 | [diff] [blame] | 735 | struct kvm_pit *pit = kvm->arch.vpit; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 736 | |
Radim Krčmář | 08e5ccf3 | 2016-03-02 22:56:47 +0100 | [diff] [blame] | 737 | if (pit) { |
Radim Krčmář | fb53072 | 2017-07-10 20:53:28 +0200 | [diff] [blame] | 738 | mutex_lock(&kvm->slots_lock); |
Radim Krčmář | 08e5ccf3 | 2016-03-02 22:56:47 +0100 | [diff] [blame] | 739 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev); |
| 740 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->speaker_dev); |
Radim Krčmář | fb53072 | 2017-07-10 20:53:28 +0200 | [diff] [blame] | 741 | mutex_unlock(&kvm->slots_lock); |
Radim Krčmář | 08e5ccf3 | 2016-03-02 22:56:47 +0100 | [diff] [blame] | 742 | kvm_pit_set_reinject(pit, false); |
| 743 | hrtimer_cancel(&pit->pit_state.timer); |
Petr Mladek | 36da91b | 2016-10-19 13:50:47 +0200 | [diff] [blame] | 744 | kthread_destroy_worker(pit->worker); |
Radim Krčmář | 08e5ccf3 | 2016-03-02 22:56:47 +0100 | [diff] [blame] | 745 | kvm_free_irq_source_id(kvm, pit->irq_source_id); |
| 746 | kfree(pit); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 747 | } |
| 748 | } |