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 |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | * |
| 27 | * Authors: |
| 28 | * Sheng Yang <sheng.yang@intel.com> |
| 29 | * Based on QEMU and Xen. |
| 30 | */ |
| 31 | |
Joe Perches | a78d9626 | 2009-12-09 10:45:35 -0800 | [diff] [blame] | 32 | #define pr_fmt(fmt) "pit: " fmt |
| 33 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 34 | #include <linux/kvm_host.h> |
| 35 | |
| 36 | #include "irq.h" |
| 37 | #include "i8254.h" |
| 38 | |
| 39 | #ifndef CONFIG_X86_64 |
Roman Zippel | 6f6d6a1 | 2008-05-01 04:34:28 -0700 | [diff] [blame] | 40 | #define mod_64(x, y) ((x) - (y) * div64_u64(x, y)) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 41 | #else |
| 42 | #define mod_64(x, y) ((x) % (y)) |
| 43 | #endif |
| 44 | |
| 45 | #define RW_STATE_LSB 1 |
| 46 | #define RW_STATE_MSB 2 |
| 47 | #define RW_STATE_WORD0 3 |
| 48 | #define RW_STATE_WORD1 4 |
| 49 | |
| 50 | /* Compute with 96 bit intermediate result: (a*b)/c */ |
| 51 | static u64 muldiv64(u64 a, u32 b, u32 c) |
| 52 | { |
| 53 | union { |
| 54 | u64 ll; |
| 55 | struct { |
| 56 | u32 low, high; |
| 57 | } l; |
| 58 | } u, res; |
| 59 | u64 rl, rh; |
| 60 | |
| 61 | u.ll = a; |
| 62 | rl = (u64)u.l.low * (u64)b; |
| 63 | rh = (u64)u.l.high * (u64)b; |
| 64 | rh += (rl >> 32); |
Roman Zippel | 6f6d6a1 | 2008-05-01 04:34:28 -0700 | [diff] [blame] | 65 | res.l.high = div64_u64(rh, c); |
| 66 | res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 67 | return res.ll; |
| 68 | } |
| 69 | |
| 70 | static void pit_set_gate(struct kvm *kvm, int channel, u32 val) |
| 71 | { |
| 72 | struct kvm_kpit_channel_state *c = |
| 73 | &kvm->arch.vpit->pit_state.channels[channel]; |
| 74 | |
| 75 | WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock)); |
| 76 | |
| 77 | switch (c->mode) { |
| 78 | default: |
| 79 | case 0: |
| 80 | case 4: |
| 81 | /* XXX: just disable/enable counting */ |
| 82 | break; |
| 83 | case 1: |
| 84 | case 2: |
| 85 | case 3: |
| 86 | case 5: |
| 87 | /* Restart counting on rising edge. */ |
| 88 | if (c->gate < val) |
| 89 | c->count_load_time = ktime_get(); |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | c->gate = val; |
| 94 | } |
| 95 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 96 | static int pit_get_gate(struct kvm *kvm, int channel) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 97 | { |
| 98 | WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock)); |
| 99 | |
| 100 | return kvm->arch.vpit->pit_state.channels[channel].gate; |
| 101 | } |
| 102 | |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 103 | static s64 __kpit_elapsed(struct kvm *kvm) |
| 104 | { |
| 105 | s64 elapsed; |
| 106 | ktime_t remaining; |
| 107 | struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state; |
| 108 | |
Marcelo Tosatti | 0ff7787 | 2009-07-02 20:02:15 -0300 | [diff] [blame] | 109 | if (!ps->pit_timer.period) |
| 110 | return 0; |
| 111 | |
Marcelo Tosatti | ede2ccc | 2009-04-08 13:14:19 -0300 | [diff] [blame] | 112 | /* |
| 113 | * The Counter does not stop when it reaches zero. In |
| 114 | * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to |
| 115 | * the highest count, either FFFF hex for binary counting |
| 116 | * or 9999 for BCD counting, and continues counting. |
| 117 | * Modes 2 and 3 are periodic; the Counter reloads |
| 118 | * itself with the initial count and continues counting |
| 119 | * from there. |
| 120 | */ |
Marcelo Tosatti | ace1546 | 2009-10-08 10:55:03 -0300 | [diff] [blame] | 121 | remaining = hrtimer_get_remaining(&ps->pit_timer.timer); |
Marcelo Tosatti | ede2ccc | 2009-04-08 13:14:19 -0300 | [diff] [blame] | 122 | elapsed = ps->pit_timer.period - ktime_to_ns(remaining); |
| 123 | elapsed = mod_64(elapsed, ps->pit_timer.period); |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 124 | |
| 125 | return elapsed; |
| 126 | } |
| 127 | |
| 128 | static s64 kpit_elapsed(struct kvm *kvm, struct kvm_kpit_channel_state *c, |
| 129 | int channel) |
| 130 | { |
| 131 | if (channel == 0) |
| 132 | return __kpit_elapsed(kvm); |
| 133 | |
| 134 | return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time)); |
| 135 | } |
| 136 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 137 | static int pit_get_count(struct kvm *kvm, int channel) |
| 138 | { |
| 139 | struct kvm_kpit_channel_state *c = |
| 140 | &kvm->arch.vpit->pit_state.channels[channel]; |
| 141 | s64 d, t; |
| 142 | int counter; |
| 143 | |
| 144 | WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock)); |
| 145 | |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 146 | t = kpit_elapsed(kvm, c, channel); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 147 | d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC); |
| 148 | |
| 149 | switch (c->mode) { |
| 150 | case 0: |
| 151 | case 1: |
| 152 | case 4: |
| 153 | case 5: |
| 154 | counter = (c->count - d) & 0xffff; |
| 155 | break; |
| 156 | case 3: |
| 157 | /* XXX: may be incorrect for odd counts */ |
| 158 | counter = c->count - (mod_64((2 * d), c->count)); |
| 159 | break; |
| 160 | default: |
| 161 | counter = c->count - mod_64(d, c->count); |
| 162 | break; |
| 163 | } |
| 164 | return counter; |
| 165 | } |
| 166 | |
| 167 | static int pit_get_out(struct kvm *kvm, int channel) |
| 168 | { |
| 169 | struct kvm_kpit_channel_state *c = |
| 170 | &kvm->arch.vpit->pit_state.channels[channel]; |
| 171 | s64 d, t; |
| 172 | int out; |
| 173 | |
| 174 | WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock)); |
| 175 | |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 176 | t = kpit_elapsed(kvm, c, channel); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 177 | d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC); |
| 178 | |
| 179 | switch (c->mode) { |
| 180 | default: |
| 181 | case 0: |
| 182 | out = (d >= c->count); |
| 183 | break; |
| 184 | case 1: |
| 185 | out = (d < c->count); |
| 186 | break; |
| 187 | case 2: |
| 188 | out = ((mod_64(d, c->count) == 0) && (d != 0)); |
| 189 | break; |
| 190 | case 3: |
| 191 | out = (mod_64(d, c->count) < ((c->count + 1) >> 1)); |
| 192 | break; |
| 193 | case 4: |
| 194 | case 5: |
| 195 | out = (d == c->count); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | return out; |
| 200 | } |
| 201 | |
| 202 | static void pit_latch_count(struct kvm *kvm, int channel) |
| 203 | { |
| 204 | struct kvm_kpit_channel_state *c = |
| 205 | &kvm->arch.vpit->pit_state.channels[channel]; |
| 206 | |
| 207 | WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock)); |
| 208 | |
| 209 | if (!c->count_latched) { |
| 210 | c->latched_count = pit_get_count(kvm, channel); |
| 211 | c->count_latched = c->rw_mode; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static void pit_latch_status(struct kvm *kvm, int channel) |
| 216 | { |
| 217 | struct kvm_kpit_channel_state *c = |
| 218 | &kvm->arch.vpit->pit_state.channels[channel]; |
| 219 | |
| 220 | WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock)); |
| 221 | |
| 222 | if (!c->status_latched) { |
| 223 | /* TODO: Return NULL COUNT (bit 6). */ |
| 224 | c->status = ((pit_get_out(kvm, channel) << 7) | |
| 225 | (c->rw_mode << 4) | |
| 226 | (c->mode << 1) | |
| 227 | c->bcd); |
| 228 | c->status_latched = 1; |
| 229 | } |
| 230 | } |
| 231 | |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 232 | int pit_has_pending_timer(struct kvm_vcpu *vcpu) |
| 233 | { |
| 234 | struct kvm_pit *pit = vcpu->kvm->arch.vpit; |
| 235 | |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 236 | if (pit && kvm_vcpu_is_bsp(vcpu) && pit->pit_state.irq_ack) |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 237 | return atomic_read(&pit->pit_state.pit_timer.pending); |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Harvey Harrison | ee032c9 | 2008-08-11 16:54:20 -0700 | [diff] [blame] | 241 | static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian) |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 242 | { |
| 243 | struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state, |
| 244 | irq_ack_notifier); |
| 245 | spin_lock(&ps->inject_lock); |
| 246 | if (atomic_dec_return(&ps->pit_timer.pending) < 0) |
Avi Kivity | dc7404c | 2008-08-17 16:03:46 +0300 | [diff] [blame] | 247 | atomic_inc(&ps->pit_timer.pending); |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 248 | ps->irq_ack = 1; |
| 249 | spin_unlock(&ps->inject_lock); |
| 250 | } |
| 251 | |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 252 | void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu) |
| 253 | { |
| 254 | struct kvm_pit *pit = vcpu->kvm->arch.vpit; |
| 255 | struct hrtimer *timer; |
| 256 | |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 257 | if (!kvm_vcpu_is_bsp(vcpu) || !pit) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 258 | return; |
| 259 | |
| 260 | timer = &pit->pit_state.pit_timer.timer; |
| 261 | if (hrtimer_cancel(timer)) |
Arjan van de Ven | beb20d52 | 2008-09-01 14:55:57 -0700 | [diff] [blame] | 262 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS); |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 263 | } |
| 264 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 265 | static void destroy_pit_timer(struct kvm_timer *pt) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 266 | { |
Joe Perches | a78d9626 | 2009-12-09 10:45:35 -0800 | [diff] [blame] | 267 | pr_debug("execute del timer!\n"); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 268 | hrtimer_cancel(&pt->timer); |
| 269 | } |
| 270 | |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 271 | static bool kpit_is_periodic(struct kvm_timer *ktimer) |
| 272 | { |
| 273 | struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state, |
| 274 | pit_timer); |
| 275 | return ps->is_periodic; |
| 276 | } |
| 277 | |
Hannes Eder | 386eb6e | 2009-03-10 22:51:09 +0100 | [diff] [blame] | 278 | static struct kvm_timer_ops kpit_ops = { |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 279 | .is_periodic = kpit_is_periodic, |
| 280 | }; |
| 281 | |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 282 | static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 283 | { |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 284 | struct kvm_timer *pt = &ps->pit_timer; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 285 | s64 interval; |
| 286 | |
| 287 | interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ); |
| 288 | |
Joe Perches | a78d9626 | 2009-12-09 10:45:35 -0800 | [diff] [blame] | 289 | pr_debug("create pit timer, interval is %llu nsec\n", interval); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 290 | |
| 291 | /* TODO The new value only affected after the retriggered */ |
| 292 | hrtimer_cancel(&pt->timer); |
Marcelo Tosatti | ede2ccc | 2009-04-08 13:14:19 -0300 | [diff] [blame] | 293 | pt->period = interval; |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 294 | ps->is_periodic = is_period; |
| 295 | |
| 296 | pt->timer.function = kvm_timer_fn; |
| 297 | pt->t_ops = &kpit_ops; |
| 298 | pt->kvm = ps->pit->kvm; |
Gleb Natapov | 1ed0ce0 | 2009-06-09 15:56:27 +0300 | [diff] [blame] | 299 | pt->vcpu = pt->kvm->bsp_vcpu; |
Marcelo Tosatti | d3c7b77 | 2009-02-23 10:57:41 -0300 | [diff] [blame] | 300 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 301 | atomic_set(&pt->pending, 0); |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 302 | ps->irq_ack = 1; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 303 | |
| 304 | hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval), |
| 305 | HRTIMER_MODE_ABS); |
| 306 | } |
| 307 | |
| 308 | static void pit_load_count(struct kvm *kvm, int channel, u32 val) |
| 309 | { |
| 310 | struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state; |
| 311 | |
| 312 | WARN_ON(!mutex_is_locked(&ps->lock)); |
| 313 | |
Joe Perches | a78d9626 | 2009-12-09 10:45:35 -0800 | [diff] [blame] | 314 | pr_debug("load_count val is %d, channel is %d\n", val, channel); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 315 | |
| 316 | /* |
Marcelo Tosatti | ede2ccc | 2009-04-08 13:14:19 -0300 | [diff] [blame] | 317 | * The largest possible initial count is 0; this is equivalent |
| 318 | * to 216 for binary counting and 104 for BCD counting. |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 319 | */ |
| 320 | if (val == 0) |
| 321 | val = 0x10000; |
| 322 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 323 | ps->channels[channel].count = val; |
| 324 | |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 325 | if (channel != 0) { |
| 326 | ps->channels[channel].count_load_time = ktime_get(); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 327 | return; |
Marcelo Tosatti | fd66842 | 2009-02-23 10:57:40 -0300 | [diff] [blame] | 328 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 329 | |
| 330 | /* Two types of timer |
| 331 | * mode 1 is one shot, mode 2 is period, otherwise del timer */ |
| 332 | switch (ps->channels[0].mode) { |
Marcelo Tosatti | ede2ccc | 2009-04-08 13:14:19 -0300 | [diff] [blame] | 333 | case 0: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 334 | case 1: |
Marcelo Tosatti | ece15ba | 2008-04-30 13:23:54 -0300 | [diff] [blame] | 335 | /* FIXME: enhance mode 4 precision */ |
| 336 | case 4: |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 337 | if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)) { |
| 338 | create_pit_timer(ps, val, 0); |
| 339 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 340 | break; |
| 341 | case 2: |
Aurelien Jarno | f697554 | 2008-05-02 17:02:23 +0200 | [diff] [blame] | 342 | case 3: |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 343 | if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)){ |
| 344 | create_pit_timer(ps, val, 1); |
| 345 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 346 | break; |
| 347 | default: |
| 348 | destroy_pit_timer(&ps->pit_timer); |
| 349 | } |
| 350 | } |
| 351 | |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 352 | void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 353 | { |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 354 | u8 saved_mode; |
| 355 | if (hpet_legacy_start) { |
| 356 | /* save existing mode for later reenablement */ |
| 357 | saved_mode = kvm->arch.vpit->pit_state.channels[0].mode; |
| 358 | kvm->arch.vpit->pit_state.channels[0].mode = 0xff; /* disable timer */ |
| 359 | pit_load_count(kvm, channel, val); |
| 360 | kvm->arch.vpit->pit_state.channels[0].mode = saved_mode; |
| 361 | } else { |
| 362 | pit_load_count(kvm, channel, val); |
| 363 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 364 | } |
| 365 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 366 | static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev) |
| 367 | { |
| 368 | return container_of(dev, struct kvm_pit, dev); |
| 369 | } |
| 370 | |
| 371 | static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev) |
| 372 | { |
| 373 | return container_of(dev, struct kvm_pit, speaker_dev); |
| 374 | } |
| 375 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 376 | static inline int pit_in_range(gpa_t addr) |
| 377 | { |
| 378 | return ((addr >= KVM_PIT_BASE_ADDRESS) && |
| 379 | (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH)); |
| 380 | } |
| 381 | |
| 382 | static int pit_ioport_write(struct kvm_io_device *this, |
| 383 | gpa_t addr, int len, const void *data) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 384 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 385 | struct kvm_pit *pit = dev_to_pit(this); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 386 | struct kvm_kpit_state *pit_state = &pit->pit_state; |
| 387 | struct kvm *kvm = pit->kvm; |
| 388 | int channel, access; |
| 389 | struct kvm_kpit_channel_state *s; |
| 390 | u32 val = *(u32 *) data; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 391 | if (!pit_in_range(addr)) |
| 392 | return -EOPNOTSUPP; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 393 | |
| 394 | val &= 0xff; |
| 395 | addr &= KVM_PIT_CHANNEL_MASK; |
| 396 | |
| 397 | mutex_lock(&pit_state->lock); |
| 398 | |
| 399 | if (val != 0) |
Joe Perches | a78d9626 | 2009-12-09 10:45:35 -0800 | [diff] [blame] | 400 | pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n", |
| 401 | (unsigned int)addr, len, val); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 402 | |
| 403 | if (addr == 3) { |
| 404 | channel = val >> 6; |
| 405 | if (channel == 3) { |
| 406 | /* Read-Back Command. */ |
| 407 | for (channel = 0; channel < 3; channel++) { |
| 408 | s = &pit_state->channels[channel]; |
| 409 | if (val & (2 << channel)) { |
| 410 | if (!(val & 0x20)) |
| 411 | pit_latch_count(kvm, channel); |
| 412 | if (!(val & 0x10)) |
| 413 | pit_latch_status(kvm, channel); |
| 414 | } |
| 415 | } |
| 416 | } else { |
| 417 | /* Select Counter <channel>. */ |
| 418 | s = &pit_state->channels[channel]; |
| 419 | access = (val >> 4) & KVM_PIT_CHANNEL_MASK; |
| 420 | if (access == 0) { |
| 421 | pit_latch_count(kvm, channel); |
| 422 | } else { |
| 423 | s->rw_mode = access; |
| 424 | s->read_state = access; |
| 425 | s->write_state = access; |
| 426 | s->mode = (val >> 1) & 7; |
| 427 | if (s->mode > 5) |
| 428 | s->mode -= 4; |
| 429 | s->bcd = val & 1; |
| 430 | } |
| 431 | } |
| 432 | } else { |
| 433 | /* Write Count. */ |
| 434 | s = &pit_state->channels[addr]; |
| 435 | switch (s->write_state) { |
| 436 | default: |
| 437 | case RW_STATE_LSB: |
| 438 | pit_load_count(kvm, addr, val); |
| 439 | break; |
| 440 | case RW_STATE_MSB: |
| 441 | pit_load_count(kvm, addr, val << 8); |
| 442 | break; |
| 443 | case RW_STATE_WORD0: |
| 444 | s->write_latch = val; |
| 445 | s->write_state = RW_STATE_WORD1; |
| 446 | break; |
| 447 | case RW_STATE_WORD1: |
| 448 | pit_load_count(kvm, addr, s->write_latch | (val << 8)); |
| 449 | s->write_state = RW_STATE_WORD0; |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | mutex_unlock(&pit_state->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 455 | return 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 456 | } |
| 457 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 458 | static int pit_ioport_read(struct kvm_io_device *this, |
| 459 | gpa_t addr, int len, void *data) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 460 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 461 | struct kvm_pit *pit = dev_to_pit(this); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 462 | struct kvm_kpit_state *pit_state = &pit->pit_state; |
| 463 | struct kvm *kvm = pit->kvm; |
| 464 | int ret, count; |
| 465 | struct kvm_kpit_channel_state *s; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 466 | if (!pit_in_range(addr)) |
| 467 | return -EOPNOTSUPP; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 468 | |
| 469 | addr &= KVM_PIT_CHANNEL_MASK; |
Marcelo Tosatti | ee73f65 | 2010-01-29 17:28:41 -0200 | [diff] [blame] | 470 | if (addr == 3) |
| 471 | return 0; |
| 472 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 473 | s = &pit_state->channels[addr]; |
| 474 | |
| 475 | mutex_lock(&pit_state->lock); |
| 476 | |
| 477 | if (s->status_latched) { |
| 478 | s->status_latched = 0; |
| 479 | ret = s->status; |
| 480 | } else if (s->count_latched) { |
| 481 | switch (s->count_latched) { |
| 482 | default: |
| 483 | case RW_STATE_LSB: |
| 484 | ret = s->latched_count & 0xff; |
| 485 | s->count_latched = 0; |
| 486 | break; |
| 487 | case RW_STATE_MSB: |
| 488 | ret = s->latched_count >> 8; |
| 489 | s->count_latched = 0; |
| 490 | break; |
| 491 | case RW_STATE_WORD0: |
| 492 | ret = s->latched_count & 0xff; |
| 493 | s->count_latched = RW_STATE_MSB; |
| 494 | break; |
| 495 | } |
| 496 | } else { |
| 497 | switch (s->read_state) { |
| 498 | default: |
| 499 | case RW_STATE_LSB: |
| 500 | count = pit_get_count(kvm, addr); |
| 501 | ret = count & 0xff; |
| 502 | break; |
| 503 | case RW_STATE_MSB: |
| 504 | count = pit_get_count(kvm, addr); |
| 505 | ret = (count >> 8) & 0xff; |
| 506 | break; |
| 507 | case RW_STATE_WORD0: |
| 508 | count = pit_get_count(kvm, addr); |
| 509 | ret = count & 0xff; |
| 510 | s->read_state = RW_STATE_WORD1; |
| 511 | break; |
| 512 | case RW_STATE_WORD1: |
| 513 | count = pit_get_count(kvm, addr); |
| 514 | ret = (count >> 8) & 0xff; |
| 515 | s->read_state = RW_STATE_WORD0; |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if (len > sizeof(ret)) |
| 521 | len = sizeof(ret); |
| 522 | memcpy(data, (char *)&ret, len); |
| 523 | |
| 524 | mutex_unlock(&pit_state->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 525 | return 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 526 | } |
| 527 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 528 | static int speaker_ioport_write(struct kvm_io_device *this, |
| 529 | gpa_t addr, int len, const void *data) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 530 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 531 | struct kvm_pit *pit = speaker_to_pit(this); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 532 | struct kvm_kpit_state *pit_state = &pit->pit_state; |
| 533 | struct kvm *kvm = pit->kvm; |
| 534 | u32 val = *(u32 *) data; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 535 | if (addr != KVM_SPEAKER_BASE_ADDRESS) |
| 536 | return -EOPNOTSUPP; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 537 | |
| 538 | mutex_lock(&pit_state->lock); |
| 539 | pit_state->speaker_data_on = (val >> 1) & 1; |
| 540 | pit_set_gate(kvm, 2, val & 1); |
| 541 | mutex_unlock(&pit_state->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 542 | return 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 543 | } |
| 544 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 545 | static int speaker_ioport_read(struct kvm_io_device *this, |
| 546 | gpa_t addr, int len, void *data) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 547 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 548 | struct kvm_pit *pit = speaker_to_pit(this); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 549 | struct kvm_kpit_state *pit_state = &pit->pit_state; |
| 550 | struct kvm *kvm = pit->kvm; |
| 551 | unsigned int refresh_clock; |
| 552 | int ret; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 553 | if (addr != KVM_SPEAKER_BASE_ADDRESS) |
| 554 | return -EOPNOTSUPP; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 555 | |
| 556 | /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */ |
| 557 | refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1; |
| 558 | |
| 559 | mutex_lock(&pit_state->lock); |
| 560 | ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) | |
| 561 | (pit_get_out(kvm, 2) << 5) | (refresh_clock << 4)); |
| 562 | if (len > sizeof(ret)) |
| 563 | len = sizeof(ret); |
| 564 | memcpy(data, (char *)&ret, len); |
| 565 | mutex_unlock(&pit_state->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 566 | return 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 567 | } |
| 568 | |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 569 | void kvm_pit_reset(struct kvm_pit *pit) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 570 | { |
| 571 | int i; |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 572 | struct kvm_kpit_channel_state *c; |
| 573 | |
| 574 | mutex_lock(&pit->pit_state.lock); |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 575 | pit->pit_state.flags = 0; |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 576 | for (i = 0; i < 3; i++) { |
| 577 | c = &pit->pit_state.channels[i]; |
| 578 | c->mode = 0xff; |
| 579 | c->gate = (i != 2); |
| 580 | pit_load_count(pit->kvm, i, 0); |
| 581 | } |
| 582 | mutex_unlock(&pit->pit_state.lock); |
| 583 | |
| 584 | atomic_set(&pit->pit_state.pit_timer.pending, 0); |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 585 | pit->pit_state.irq_ack = 1; |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 586 | } |
| 587 | |
Avi Kivity | 4780c659 | 2009-01-04 18:06:06 +0200 | [diff] [blame] | 588 | static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask) |
| 589 | { |
| 590 | struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier); |
| 591 | |
| 592 | if (!mask) { |
| 593 | atomic_set(&pit->pit_state.pit_timer.pending, 0); |
| 594 | pit->pit_state.irq_ack = 1; |
| 595 | } |
| 596 | } |
| 597 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 598 | static const struct kvm_io_device_ops pit_dev_ops = { |
| 599 | .read = pit_ioport_read, |
| 600 | .write = pit_ioport_write, |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 601 | }; |
| 602 | |
| 603 | static const struct kvm_io_device_ops speaker_dev_ops = { |
| 604 | .read = speaker_ioport_read, |
| 605 | .write = speaker_ioport_write, |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 606 | }; |
| 607 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 608 | /* Caller must hold slots_lock */ |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 609 | struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 610 | { |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 611 | struct kvm_pit *pit; |
| 612 | struct kvm_kpit_state *pit_state; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 613 | int ret; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 614 | |
| 615 | pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL); |
| 616 | if (!pit) |
| 617 | return NULL; |
| 618 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 619 | pit->irq_source_id = kvm_request_irq_source_id(kvm); |
Avi Kivity | e17d1dc | 2008-11-11 13:09:36 +0200 | [diff] [blame] | 620 | if (pit->irq_source_id < 0) { |
| 621 | kfree(pit); |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 622 | return NULL; |
Avi Kivity | e17d1dc | 2008-11-11 13:09:36 +0200 | [diff] [blame] | 623 | } |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 624 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 625 | mutex_init(&pit->pit_state.lock); |
| 626 | mutex_lock(&pit->pit_state.lock); |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 627 | spin_lock_init(&pit->pit_state.inject_lock); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 628 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 629 | kvm->arch.vpit = pit; |
| 630 | pit->kvm = kvm; |
| 631 | |
| 632 | pit_state = &pit->pit_state; |
| 633 | pit_state->pit = pit; |
| 634 | hrtimer_init(&pit_state->pit_timer.timer, |
| 635 | CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 636 | pit_state->irq_ack_notifier.gsi = 0; |
| 637 | pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq; |
| 638 | kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 639 | pit_state->pit_timer.reinject = true; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 640 | mutex_unlock(&pit->pit_state.lock); |
| 641 | |
Sheng Yang | 308b0f2 | 2008-03-13 10:22:26 +0800 | [diff] [blame] | 642 | kvm_pit_reset(pit); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 643 | |
Avi Kivity | 4780c659 | 2009-01-04 18:06:06 +0200 | [diff] [blame] | 644 | pit->mask_notifier.func = pit_mask_notifer; |
| 645 | kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier); |
| 646 | |
Gregory Haskins | 6b66ac1 | 2009-06-01 12:54:56 -0400 | [diff] [blame] | 647 | kvm_iodevice_init(&pit->dev, &pit_dev_ops); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 648 | ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &pit->dev); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 649 | if (ret < 0) |
| 650 | goto fail; |
Gregory Haskins | 6b66ac1 | 2009-06-01 12:54:56 -0400 | [diff] [blame] | 651 | |
| 652 | if (flags & KVM_PIT_SPEAKER_DUMMY) { |
| 653 | kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 654 | ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 655 | &pit->speaker_dev); |
| 656 | if (ret < 0) |
| 657 | goto fail_unregister; |
Gregory Haskins | 6b66ac1 | 2009-06-01 12:54:56 -0400 | [diff] [blame] | 658 | } |
| 659 | |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 660 | return pit; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 661 | |
| 662 | fail_unregister: |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 663 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 664 | |
| 665 | fail: |
Wei Yongjun | d225f53 | 2010-02-08 17:03:51 +0800 | [diff] [blame^] | 666 | kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier); |
| 667 | kvm_unregister_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier); |
| 668 | kvm_free_irq_source_id(kvm, pit->irq_source_id); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 669 | |
| 670 | kfree(pit); |
| 671 | return NULL; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | void kvm_free_pit(struct kvm *kvm) |
| 675 | { |
| 676 | struct hrtimer *timer; |
| 677 | |
| 678 | if (kvm->arch.vpit) { |
Avi Kivity | 4780c659 | 2009-01-04 18:06:06 +0200 | [diff] [blame] | 679 | kvm_unregister_irq_mask_notifier(kvm, 0, |
| 680 | &kvm->arch.vpit->mask_notifier); |
Gleb Natapov | 84fde24 | 2009-07-16 17:03:30 +0300 | [diff] [blame] | 681 | kvm_unregister_irq_ack_notifier(kvm, |
| 682 | &kvm->arch.vpit->pit_state.irq_ack_notifier); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 683 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 684 | timer = &kvm->arch.vpit->pit_state.pit_timer.timer; |
| 685 | hrtimer_cancel(timer); |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 686 | kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 687 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 688 | kfree(kvm->arch.vpit); |
| 689 | } |
| 690 | } |
| 691 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 692 | static void __inject_pit_timer_intr(struct kvm *kvm) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 693 | { |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 694 | struct kvm_vcpu *vcpu; |
| 695 | int i; |
| 696 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 697 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1); |
| 698 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0); |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 699 | |
| 700 | /* |
Jan Kiszka | 8fdb235 | 2008-10-20 10:20:02 +0200 | [diff] [blame] | 701 | * Provides NMI watchdog support via Virtual Wire mode. |
| 702 | * The route is: PIT -> PIC -> LVT0 in NMI mode. |
| 703 | * |
| 704 | * Note: Our Virtual Wire implementation is simplified, only |
| 705 | * propagating PIT interrupts to all VCPUs when they have set |
| 706 | * LVT0 to NMI delivery. Other PIC interrupts are just sent to |
| 707 | * VCPU0, and only if its LVT0 is in EXTINT mode. |
Jan Kiszka | 23930f9 | 2008-09-26 09:30:52 +0200 | [diff] [blame] | 708 | */ |
Jan Kiszka | cc6e462 | 2008-10-20 10:20:03 +0200 | [diff] [blame] | 709 | if (kvm->arch.vapics_in_nmi_mode > 0) |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 710 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 711 | kvm_apic_nmi_wd_deliver(vcpu); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu) |
| 715 | { |
| 716 | struct kvm_pit *pit = vcpu->kvm->arch.vpit; |
| 717 | struct kvm *kvm = vcpu->kvm; |
| 718 | struct kvm_kpit_state *ps; |
| 719 | |
Bartlomiej Zolnierkiewicz | 95fb4eb | 2009-07-29 00:46:38 +0200 | [diff] [blame] | 720 | if (pit) { |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 721 | int inject = 0; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 722 | ps = &pit->pit_state; |
| 723 | |
Marcelo Tosatti | 3cf57fe | 2008-07-26 17:01:01 -0300 | [diff] [blame] | 724 | /* Try to inject pending interrupts when |
| 725 | * last one has been acked. |
| 726 | */ |
| 727 | spin_lock(&ps->inject_lock); |
| 728 | if (atomic_read(&ps->pit_timer.pending) && ps->irq_ack) { |
| 729 | ps->irq_ack = 0; |
| 730 | inject = 1; |
| 731 | } |
| 732 | spin_unlock(&ps->inject_lock); |
| 733 | if (inject) |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 734 | __inject_pit_timer_intr(kvm); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 735 | } |
| 736 | } |