kvm: x86: Add user space part for in-kernel i8254
[qemu/ar7.git] / hw / kvm / i8254.c
blobbb5fe07d1e0e743309172c4650bbc79eccad7384
1 /*
2 * KVM in-kernel PIT (i8254) support
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2012 Jan Kiszka, Siemens AG
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 #include "qemu-timer.h"
26 #include "hw/i8254.h"
27 #include "hw/i8254_internal.h"
28 #include "kvm.h"
30 #define KVM_PIT_REINJECT_BIT 0
32 typedef struct KVMPITState {
33 PITCommonState pit;
34 LostTickPolicy lost_tick_policy;
35 } KVMPITState;
37 static void kvm_pit_get(PITCommonState *s)
39 struct kvm_pit_state2 kpit;
40 struct kvm_pit_channel_state *kchan;
41 struct PITChannelState *sc;
42 int i, ret;
44 if (kvm_has_pit_state2()) {
45 ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit);
46 if (ret < 0) {
47 fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(ret));
48 abort();
50 s->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY;
51 } else {
53 * kvm_pit_state2 is superset of kvm_pit_state struct,
54 * so we can use it for KVM_GET_PIT as well.
56 ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT, &kpit);
57 if (ret < 0) {
58 fprintf(stderr, "KVM_GET_PIT failed: %s\n", strerror(ret));
59 abort();
62 for (i = 0; i < 3; i++) {
63 kchan = &kpit.channels[i];
64 sc = &s->channels[i];
65 sc->count = kchan->count;
66 sc->latched_count = kchan->latched_count;
67 sc->count_latched = kchan->count_latched;
68 sc->status_latched = kchan->status_latched;
69 sc->status = kchan->status;
70 sc->read_state = kchan->read_state;
71 sc->write_state = kchan->write_state;
72 sc->write_latch = kchan->write_latch;
73 sc->rw_mode = kchan->rw_mode;
74 sc->mode = kchan->mode;
75 sc->bcd = kchan->bcd;
76 sc->gate = kchan->gate;
77 sc->count_load_time = kchan->count_load_time;
80 sc = &s->channels[0];
81 sc->next_transition_time =
82 pit_get_next_transition_time(sc, sc->count_load_time);
85 static void kvm_pit_put(PITCommonState *s)
87 struct kvm_pit_state2 kpit;
88 struct kvm_pit_channel_state *kchan;
89 struct PITChannelState *sc;
90 int i, ret;
92 kpit.flags = s->channels[0].irq_disabled ? KVM_PIT_FLAGS_HPET_LEGACY : 0;
93 for (i = 0; i < 3; i++) {
94 kchan = &kpit.channels[i];
95 sc = &s->channels[i];
96 kchan->count = sc->count;
97 kchan->latched_count = sc->latched_count;
98 kchan->count_latched = sc->count_latched;
99 kchan->status_latched = sc->status_latched;
100 kchan->status = sc->status;
101 kchan->read_state = sc->read_state;
102 kchan->write_state = sc->write_state;
103 kchan->write_latch = sc->write_latch;
104 kchan->rw_mode = sc->rw_mode;
105 kchan->mode = sc->mode;
106 kchan->bcd = sc->bcd;
107 kchan->gate = sc->gate;
108 kchan->count_load_time = sc->count_load_time;
111 ret = kvm_vm_ioctl(kvm_state,
112 kvm_has_pit_state2() ? KVM_SET_PIT2 : KVM_SET_PIT,
113 &kpit);
114 if (ret < 0) {
115 fprintf(stderr, "%s failed: %s\n",
116 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
117 strerror(ret));
118 abort();
122 static void kvm_pit_set_gate(PITCommonState *s, PITChannelState *sc, int val)
124 kvm_pit_get(s);
126 switch (sc->mode) {
127 default:
128 case 0:
129 case 4:
130 /* XXX: just disable/enable counting */
131 break;
132 case 1:
133 case 2:
134 case 3:
135 case 5:
136 if (sc->gate < val) {
137 /* restart counting on rising edge */
138 sc->count_load_time = qemu_get_clock_ns(vm_clock);
140 break;
142 sc->gate = val;
144 kvm_pit_put(s);
147 static void kvm_pit_get_channel_info(PITCommonState *s, PITChannelState *sc,
148 PITChannelInfo *info)
150 kvm_pit_get(s);
152 pit_get_channel_info_common(s, sc, info);
155 static void kvm_pit_reset(DeviceState *dev)
157 PITCommonState *s = DO_UPCAST(PITCommonState, dev.qdev, dev);
159 pit_reset_common(s);
161 kvm_pit_put(s);
164 static void kvm_pit_irq_control(void *opaque, int n, int enable)
166 PITCommonState *pit = opaque;
167 PITChannelState *s = &pit->channels[0];
169 kvm_pit_get(pit);
171 s->irq_disabled = !enable;
173 kvm_pit_put(pit);
176 static int kvm_pit_initfn(PITCommonState *pit)
178 KVMPITState *s = DO_UPCAST(KVMPITState, pit, pit);
179 struct kvm_pit_config config = {
180 .flags = 0,
182 int ret;
184 if (kvm_check_extension(kvm_state, KVM_CAP_PIT2)) {
185 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT2, &config);
186 } else {
187 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT);
189 if (ret < 0) {
190 fprintf(stderr, "Create kernel PIC irqchip failed: %s\n",
191 strerror(ret));
192 return ret;
194 switch (s->lost_tick_policy) {
195 case LOST_TICK_DELAY:
196 break; /* enabled by default */
197 case LOST_TICK_DISCARD:
198 if (kvm_check_extension(kvm_state, KVM_CAP_REINJECT_CONTROL)) {
199 struct kvm_reinject_control control = { .pit_reinject = 0 };
201 ret = kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
202 if (ret < 0) {
203 fprintf(stderr,
204 "Can't disable in-kernel PIT reinjection: %s\n",
205 strerror(ret));
206 return ret;
209 break;
210 default:
211 return -EINVAL;
214 memory_region_init_reservation(&pit->ioports, "kvm-pit", 4);
216 qdev_init_gpio_in(&pit->dev.qdev, kvm_pit_irq_control, 1);
218 return 0;
221 static Property kvm_pit_properties[] = {
222 DEFINE_PROP_HEX32("iobase", KVMPITState, pit.iobase, -1),
223 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState,
224 lost_tick_policy, LOST_TICK_DELAY),
225 DEFINE_PROP_END_OF_LIST(),
228 static void kvm_pit_class_init(ObjectClass *klass, void *data)
230 PITCommonClass *k = PIT_COMMON_CLASS(klass);
231 DeviceClass *dc = DEVICE_CLASS(klass);
233 k->init = kvm_pit_initfn;
234 k->set_channel_gate = kvm_pit_set_gate;
235 k->get_channel_info = kvm_pit_get_channel_info;
236 k->pre_save = kvm_pit_get;
237 k->post_load = kvm_pit_put;
238 dc->reset = kvm_pit_reset;
239 dc->props = kvm_pit_properties;
242 static TypeInfo kvm_pit_info = {
243 .name = "kvm-pit",
244 .parent = TYPE_PIT_COMMON,
245 .instance_size = sizeof(KVMPITState),
246 .class_init = kvm_pit_class_init,
249 static void kvm_pit_register(void)
251 type_register_static(&kvm_pit_info);
254 type_init(kvm_pit_register)