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