KVM: kvm_io_device: extend in_range() to manage len and write attribute
[linux-2.6.git] / arch / x86 / kvm / i8254.c
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
38 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
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);
63         res.l.high = div64_u64(rh, c);
64         res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
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
94 static int pit_get_gate(struct kvm *kvm, int channel)
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
101 static int pit_get_count(struct kvm *kvm, int channel)
102 {
103         struct kvm_kpit_channel_state *c =
104                 &kvm->arch.vpit->pit_state.channels[channel];
105         s64 d, t;
106         int counter;
107
108         WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
109
110         t = ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
111         d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
112
113         switch (c->mode) {
114         case 0:
115         case 1:
116         case 4:
117         case 5:
118                 counter = (c->count - d) & 0xffff;
119                 break;
120         case 3:
121                 /* XXX: may be incorrect for odd counts */
122                 counter = c->count - (mod_64((2 * d), c->count));
123                 break;
124         default:
125                 counter = c->count - mod_64(d, c->count);
126                 break;
127         }
128         return counter;
129 }
130
131 static int pit_get_out(struct kvm *kvm, int channel)
132 {
133         struct kvm_kpit_channel_state *c =
134                 &kvm->arch.vpit->pit_state.channels[channel];
135         s64 d, t;
136         int out;
137
138         WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
139
140         t = ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
141         d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
142
143         switch (c->mode) {
144         default:
145         case 0:
146                 out = (d >= c->count);
147                 break;
148         case 1:
149                 out = (d < c->count);
150                 break;
151         case 2:
152                 out = ((mod_64(d, c->count) == 0) && (d != 0));
153                 break;
154         case 3:
155                 out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
156                 break;
157         case 4:
158         case 5:
159                 out = (d == c->count);
160                 break;
161         }
162
163         return out;
164 }
165
166 static void pit_latch_count(struct kvm *kvm, int channel)
167 {
168         struct kvm_kpit_channel_state *c =
169                 &kvm->arch.vpit->pit_state.channels[channel];
170
171         WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
172
173         if (!c->count_latched) {
174                 c->latched_count = pit_get_count(kvm, channel);
175                 c->count_latched = c->rw_mode;
176         }
177 }
178
179 static void pit_latch_status(struct kvm *kvm, int channel)
180 {
181         struct kvm_kpit_channel_state *c =
182                 &kvm->arch.vpit->pit_state.channels[channel];
183
184         WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
185
186         if (!c->status_latched) {
187                 /* TODO: Return NULL COUNT (bit 6). */
188                 c->status = ((pit_get_out(kvm, channel) << 7) |
189                                 (c->rw_mode << 4) |
190                                 (c->mode << 1) |
191                                 c->bcd);
192                 c->status_latched = 1;
193         }
194 }
195
196 static int __pit_timer_fn(struct kvm_kpit_state *ps)
197 {
198         struct kvm_vcpu *vcpu0 = ps->pit->kvm->vcpus[0];
199         struct kvm_kpit_timer *pt = &ps->pit_timer;
200
201         atomic_inc(&pt->pending);
202         smp_mb__after_atomic_inc();
203         if (vcpu0) {
204                 set_bit(KVM_REQ_PENDING_TIMER, &vcpu0->requests);
205                 if (waitqueue_active(&vcpu0->wq)) {
206                         vcpu0->arch.mp_state = KVM_MP_STATE_RUNNABLE;
207                         wake_up_interruptible(&vcpu0->wq);
208                 }
209         }
210
211         pt->timer.expires = ktime_add_ns(pt->timer.expires, pt->period);
212         pt->scheduled = ktime_to_ns(pt->timer.expires);
213
214         return (pt->period == 0 ? 0 : 1);
215 }
216
217 int pit_has_pending_timer(struct kvm_vcpu *vcpu)
218 {
219         struct kvm_pit *pit = vcpu->kvm->arch.vpit;
220
221         if (pit && vcpu->vcpu_id == 0 && pit->pit_state.inject_pending)
222                 return atomic_read(&pit->pit_state.pit_timer.pending);
223
224         return 0;
225 }
226
227 static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
228 {
229         struct kvm_kpit_state *ps;
230         int restart_timer = 0;
231
232         ps = container_of(data, struct kvm_kpit_state, pit_timer.timer);
233
234         restart_timer = __pit_timer_fn(ps);
235
236         if (restart_timer)
237                 return HRTIMER_RESTART;
238         else
239                 return HRTIMER_NORESTART;
240 }
241
242 void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
243 {
244         struct kvm_pit *pit = vcpu->kvm->arch.vpit;
245         struct hrtimer *timer;
246
247         if (vcpu->vcpu_id != 0 || !pit)
248                 return;
249
250         timer = &pit->pit_state.pit_timer.timer;
251         if (hrtimer_cancel(timer))
252                 hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
253 }
254
255 static void destroy_pit_timer(struct kvm_kpit_timer *pt)
256 {
257         pr_debug("pit: execute del timer!\n");
258         hrtimer_cancel(&pt->timer);
259 }
260
261 static void create_pit_timer(struct kvm_kpit_timer *pt, u32 val, int is_period)
262 {
263         s64 interval;
264
265         interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
266
267         pr_debug("pit: create pit timer, interval is %llu nsec\n", interval);
268
269         /* TODO The new value only affected after the retriggered */
270         hrtimer_cancel(&pt->timer);
271         pt->period = (is_period == 0) ? 0 : interval;
272         pt->timer.function = pit_timer_fn;
273         atomic_set(&pt->pending, 0);
274
275         hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
276                       HRTIMER_MODE_ABS);
277 }
278
279 static void pit_load_count(struct kvm *kvm, int channel, u32 val)
280 {
281         struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
282
283         WARN_ON(!mutex_is_locked(&ps->lock));
284
285         pr_debug("pit: load_count val is %d, channel is %d\n", val, channel);
286
287         /*
288          * Though spec said the state of 8254 is undefined after power-up,
289          * seems some tricky OS like Windows XP depends on IRQ0 interrupt
290          * when booting up.
291          * So here setting initialize rate for it, and not a specific number
292          */
293         if (val == 0)
294                 val = 0x10000;
295
296         ps->channels[channel].count_load_time = ktime_get();
297         ps->channels[channel].count = val;
298
299         if (channel != 0)
300                 return;
301
302         /* Two types of timer
303          * mode 1 is one shot, mode 2 is period, otherwise del timer */
304         switch (ps->channels[0].mode) {
305         case 1:
306         /* FIXME: enhance mode 4 precision */
307         case 4:
308                 create_pit_timer(&ps->pit_timer, val, 0);
309                 break;
310         case 2:
311         case 3:
312                 create_pit_timer(&ps->pit_timer, val, 1);
313                 break;
314         default:
315                 destroy_pit_timer(&ps->pit_timer);
316         }
317 }
318
319 void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val)
320 {
321         mutex_lock(&kvm->arch.vpit->pit_state.lock);
322         pit_load_count(kvm, channel, val);
323         mutex_unlock(&kvm->arch.vpit->pit_state.lock);
324 }
325
326 static void pit_ioport_write(struct kvm_io_device *this,
327                              gpa_t addr, int len, const void *data)
328 {
329         struct kvm_pit *pit = (struct kvm_pit *)this->private;
330         struct kvm_kpit_state *pit_state = &pit->pit_state;
331         struct kvm *kvm = pit->kvm;
332         int channel, access;
333         struct kvm_kpit_channel_state *s;
334         u32 val = *(u32 *) data;
335
336         val  &= 0xff;
337         addr &= KVM_PIT_CHANNEL_MASK;
338
339         mutex_lock(&pit_state->lock);
340
341         if (val != 0)
342                 pr_debug("pit: write addr is 0x%x, len is %d, val is 0x%x\n",
343                           (unsigned int)addr, len, val);
344
345         if (addr == 3) {
346                 channel = val >> 6;
347                 if (channel == 3) {
348                         /* Read-Back Command. */
349                         for (channel = 0; channel < 3; channel++) {
350                                 s = &pit_state->channels[channel];
351                                 if (val & (2 << channel)) {
352                                         if (!(val & 0x20))
353                                                 pit_latch_count(kvm, channel);
354                                         if (!(val & 0x10))
355                                                 pit_latch_status(kvm, channel);
356                                 }
357                         }
358                 } else {
359                         /* Select Counter <channel>. */
360                         s = &pit_state->channels[channel];
361                         access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
362                         if (access == 0) {
363                                 pit_latch_count(kvm, channel);
364                         } else {
365                                 s->rw_mode = access;
366                                 s->read_state = access;
367                                 s->write_state = access;
368                                 s->mode = (val >> 1) & 7;
369                                 if (s->mode > 5)
370                                         s->mode -= 4;
371                                 s->bcd = val & 1;
372                         }
373                 }
374         } else {
375                 /* Write Count. */
376                 s = &pit_state->channels[addr];
377                 switch (s->write_state) {
378                 default:
379                 case RW_STATE_LSB:
380                         pit_load_count(kvm, addr, val);
381                         break;
382                 case RW_STATE_MSB:
383                         pit_load_count(kvm, addr, val << 8);
384                         break;
385                 case RW_STATE_WORD0:
386                         s->write_latch = val;
387                         s->write_state = RW_STATE_WORD1;
388                         break;
389                 case RW_STATE_WORD1:
390                         pit_load_count(kvm, addr, s->write_latch | (val << 8));
391                         s->write_state = RW_STATE_WORD0;
392                         break;
393                 }
394         }
395
396         mutex_unlock(&pit_state->lock);
397 }
398
399 static void pit_ioport_read(struct kvm_io_device *this,
400                             gpa_t addr, int len, void *data)
401 {
402         struct kvm_pit *pit = (struct kvm_pit *)this->private;
403         struct kvm_kpit_state *pit_state = &pit->pit_state;
404         struct kvm *kvm = pit->kvm;
405         int ret, count;
406         struct kvm_kpit_channel_state *s;
407
408         addr &= KVM_PIT_CHANNEL_MASK;
409         s = &pit_state->channels[addr];
410
411         mutex_lock(&pit_state->lock);
412
413         if (s->status_latched) {
414                 s->status_latched = 0;
415                 ret = s->status;
416         } else if (s->count_latched) {
417                 switch (s->count_latched) {
418                 default:
419                 case RW_STATE_LSB:
420                         ret = s->latched_count & 0xff;
421                         s->count_latched = 0;
422                         break;
423                 case RW_STATE_MSB:
424                         ret = s->latched_count >> 8;
425                         s->count_latched = 0;
426                         break;
427                 case RW_STATE_WORD0:
428                         ret = s->latched_count & 0xff;
429                         s->count_latched = RW_STATE_MSB;
430                         break;
431                 }
432         } else {
433                 switch (s->read_state) {
434                 default:
435                 case RW_STATE_LSB:
436                         count = pit_get_count(kvm, addr);
437                         ret = count & 0xff;
438                         break;
439                 case RW_STATE_MSB:
440                         count = pit_get_count(kvm, addr);
441                         ret = (count >> 8) & 0xff;
442                         break;
443                 case RW_STATE_WORD0:
444                         count = pit_get_count(kvm, addr);
445                         ret = count & 0xff;
446                         s->read_state = RW_STATE_WORD1;
447                         break;
448                 case RW_STATE_WORD1:
449                         count = pit_get_count(kvm, addr);
450                         ret = (count >> 8) & 0xff;
451                         s->read_state = RW_STATE_WORD0;
452                         break;
453                 }
454         }
455
456         if (len > sizeof(ret))
457                 len = sizeof(ret);
458         memcpy(data, (char *)&ret, len);
459
460         mutex_unlock(&pit_state->lock);
461 }
462
463 static int pit_in_range(struct kvm_io_device *this, gpa_t addr,
464                         int len, int is_write)
465 {
466         return ((addr >= KVM_PIT_BASE_ADDRESS) &&
467                 (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
468 }
469
470 static void speaker_ioport_write(struct kvm_io_device *this,
471                                  gpa_t addr, int len, const void *data)
472 {
473         struct kvm_pit *pit = (struct kvm_pit *)this->private;
474         struct kvm_kpit_state *pit_state = &pit->pit_state;
475         struct kvm *kvm = pit->kvm;
476         u32 val = *(u32 *) data;
477
478         mutex_lock(&pit_state->lock);
479         pit_state->speaker_data_on = (val >> 1) & 1;
480         pit_set_gate(kvm, 2, val & 1);
481         mutex_unlock(&pit_state->lock);
482 }
483
484 static void speaker_ioport_read(struct kvm_io_device *this,
485                                 gpa_t addr, int len, void *data)
486 {
487         struct kvm_pit *pit = (struct kvm_pit *)this->private;
488         struct kvm_kpit_state *pit_state = &pit->pit_state;
489         struct kvm *kvm = pit->kvm;
490         unsigned int refresh_clock;
491         int ret;
492
493         /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
494         refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
495
496         mutex_lock(&pit_state->lock);
497         ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
498                 (pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
499         if (len > sizeof(ret))
500                 len = sizeof(ret);
501         memcpy(data, (char *)&ret, len);
502         mutex_unlock(&pit_state->lock);
503 }
504
505 static int speaker_in_range(struct kvm_io_device *this, gpa_t addr,
506                             int len, int is_write)
507 {
508         return (addr == KVM_SPEAKER_BASE_ADDRESS);
509 }
510
511 void kvm_pit_reset(struct kvm_pit *pit)
512 {
513         int i;
514         struct kvm_kpit_channel_state *c;
515
516         mutex_lock(&pit->pit_state.lock);
517         for (i = 0; i < 3; i++) {
518                 c = &pit->pit_state.channels[i];
519                 c->mode = 0xff;
520                 c->gate = (i != 2);
521                 pit_load_count(pit->kvm, i, 0);
522         }
523         mutex_unlock(&pit->pit_state.lock);
524
525         atomic_set(&pit->pit_state.pit_timer.pending, 0);
526         pit->pit_state.inject_pending = 1;
527 }
528
529 struct kvm_pit *kvm_create_pit(struct kvm *kvm)
530 {
531         struct kvm_pit *pit;
532         struct kvm_kpit_state *pit_state;
533
534         pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
535         if (!pit)
536                 return NULL;
537
538         mutex_init(&pit->pit_state.lock);
539         mutex_lock(&pit->pit_state.lock);
540
541         /* Initialize PIO device */
542         pit->dev.read = pit_ioport_read;
543         pit->dev.write = pit_ioport_write;
544         pit->dev.in_range = pit_in_range;
545         pit->dev.private = pit;
546         kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
547
548         pit->speaker_dev.read = speaker_ioport_read;
549         pit->speaker_dev.write = speaker_ioport_write;
550         pit->speaker_dev.in_range = speaker_in_range;
551         pit->speaker_dev.private = pit;
552         kvm_io_bus_register_dev(&kvm->pio_bus, &pit->speaker_dev);
553
554         kvm->arch.vpit = pit;
555         pit->kvm = kvm;
556
557         pit_state = &pit->pit_state;
558         pit_state->pit = pit;
559         hrtimer_init(&pit_state->pit_timer.timer,
560                      CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
561         mutex_unlock(&pit->pit_state.lock);
562
563         kvm_pit_reset(pit);
564
565         return pit;
566 }
567
568 void kvm_free_pit(struct kvm *kvm)
569 {
570         struct hrtimer *timer;
571
572         if (kvm->arch.vpit) {
573                 mutex_lock(&kvm->arch.vpit->pit_state.lock);
574                 timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
575                 hrtimer_cancel(timer);
576                 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
577                 kfree(kvm->arch.vpit);
578         }
579 }
580
581 static void __inject_pit_timer_intr(struct kvm *kvm)
582 {
583         mutex_lock(&kvm->lock);
584         kvm_ioapic_set_irq(kvm->arch.vioapic, 0, 1);
585         kvm_ioapic_set_irq(kvm->arch.vioapic, 0, 0);
586         kvm_pic_set_irq(pic_irqchip(kvm), 0, 1);
587         kvm_pic_set_irq(pic_irqchip(kvm), 0, 0);
588         mutex_unlock(&kvm->lock);
589 }
590
591 void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
592 {
593         struct kvm_pit *pit = vcpu->kvm->arch.vpit;
594         struct kvm *kvm = vcpu->kvm;
595         struct kvm_kpit_state *ps;
596
597         if (vcpu && pit) {
598                 ps = &pit->pit_state;
599
600                 /* Try to inject pending interrupts when:
601                  * 1. Pending exists
602                  * 2. Last interrupt was accepted or waited for too long time*/
603                 if (atomic_read(&ps->pit_timer.pending) &&
604                     (ps->inject_pending ||
605                     (jiffies - ps->last_injected_time
606                                 >= KVM_MAX_PIT_INTR_INTERVAL))) {
607                         ps->inject_pending = 0;
608                         __inject_pit_timer_intr(kvm);
609                         ps->last_injected_time = jiffies;
610                 }
611         }
612 }
613
614 void kvm_pit_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
615 {
616         struct kvm_arch *arch = &vcpu->kvm->arch;
617         struct kvm_kpit_state *ps;
618
619         if (vcpu && arch->vpit) {
620                 ps = &arch->vpit->pit_state;
621                 if (atomic_read(&ps->pit_timer.pending) &&
622                 (((arch->vpic->pics[0].imr & 1) == 0 &&
623                   arch->vpic->pics[0].irq_base == vec) ||
624                   (arch->vioapic->redirtbl[0].fields.vector == vec &&
625                   arch->vioapic->redirtbl[0].fields.mask != 1))) {
626                         ps->inject_pending = 1;
627                         atomic_dec(&ps->pit_timer.pending);
628                         ps->channels[0].count_load_time = ktime_get();
629                 }
630         }
631 }