Merge branch 'upstream-merge'
[qemu-kvm/markmc.git] / hw / i8254-kvm.c
blobc62ab6a091403933445e3483c5ce411ada0dab0f
1 /*
2 * QEMU 8253/8254 interval timer emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "pc.h"
26 #include "isa.h"
27 #include "qemu-timer.h"
28 #include "i8254.h"
29 #include "qemu-kvm.h"
31 extern VMStateDescription vmstate_pit;
33 static PITState pit_state;
35 static void kvm_pit_pre_save(void *opaque)
37 PITState *s = (void *)opaque;
38 struct kvm_pit_state2 pit2;
39 struct kvm_pit_channel_state *c;
40 struct PITChannelState *sc;
41 int i;
43 if(qemu_kvm_has_pit_state2()) {
44 kvm_get_pit2(kvm_context, &pit2);
45 s->flags = pit2.flags;
46 } else {
47 /* pit2 is superset of pit struct so just cast it and use it */
48 kvm_get_pit(kvm_context, (struct kvm_pit_state *)&pit2);
50 for (i = 0; i < 3; i++) {
51 c = &pit2.channels[i];
52 sc = &s->channels[i];
53 sc->count = c->count;
54 sc->latched_count = c->latched_count;
55 sc->count_latched = c->count_latched;
56 sc->status_latched = c->status_latched;
57 sc->status = c->status;
58 sc->read_state = c->read_state;
59 sc->write_state = c->write_state;
60 sc->write_latch = c->write_latch;
61 sc->rw_mode = c->rw_mode;
62 sc->mode = c->mode;
63 sc->bcd = c->bcd;
64 sc->gate = c->gate;
65 sc->count_load_time = c->count_load_time;
69 static int kvm_pit_post_load(void *opaque, int version_id)
71 PITState *s = opaque;
72 struct kvm_pit_state2 pit2;
73 struct kvm_pit_channel_state *c;
74 struct PITChannelState *sc;
75 int i;
77 pit2.flags = s->flags;
78 for (i = 0; i < 3; i++) {
79 c = &pit2.channels[i];
80 sc = &s->channels[i];
81 c->count = sc->count;
82 c->latched_count = sc->latched_count;
83 c->count_latched = sc->count_latched;
84 c->status_latched = sc->status_latched;
85 c->status = sc->status;
86 c->read_state = sc->read_state;
87 c->write_state = sc->write_state;
88 c->write_latch = sc->write_latch;
89 c->rw_mode = sc->rw_mode;
90 c->mode = sc->mode;
91 c->bcd = sc->bcd;
92 c->gate = sc->gate;
93 c->count_load_time = sc->count_load_time;
96 if(qemu_kvm_has_pit_state2()) {
97 kvm_set_pit2(kvm_context, &pit2);
98 } else {
99 kvm_set_pit(kvm_context, (struct kvm_pit_state *)&pit2);
101 return 0;
104 static void dummy_timer(void *opaque)
108 PITState *kvm_pit_init(int base, qemu_irq irq)
110 PITState *pit = &pit_state;
111 PITChannelState *s;
113 s = &pit->channels[0];
114 s->irq_timer = qemu_new_timer(vm_clock, dummy_timer, s);
115 vmstate_pit.pre_save = kvm_pit_pre_save;
116 vmstate_pit.post_load = kvm_pit_post_load;
117 vmstate_register(base, &vmstate_pit, pit);
118 qemu_register_reset(pit_reset, pit);
119 pit_reset(pit);
121 return pit;