xref: /openbmc/qemu/hw/i386/kvm/i8254.c (revision 99d423e5)
1 /*
2  * KVM in-kernel PIT (i8254) support
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  * Copyright (c) 2012      Jan Kiszka, Siemens AG
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  */
25 
26 #include "qemu/osdep.h"
27 #include <linux/kvm.h>
28 #include "qapi/error.h"
29 #include "qemu/timer.h"
30 #include "sysemu/sysemu.h"
31 #include "hw/timer/i8254.h"
32 #include "hw/timer/i8254_internal.h"
33 #include "sysemu/kvm.h"
34 
35 #define KVM_PIT_REINJECT_BIT 0
36 
37 #define CALIBRATION_ROUNDS   3
38 
39 #define KVM_PIT(obj) OBJECT_CHECK(KVMPITState, (obj), TYPE_KVM_I8254)
40 #define KVM_PIT_CLASS(class) \
41     OBJECT_CLASS_CHECK(KVMPITClass, (class), TYPE_KVM_I8254)
42 #define KVM_PIT_GET_CLASS(obj) \
43     OBJECT_GET_CLASS(KVMPITClass, (obj), TYPE_KVM_I8254)
44 
45 typedef struct KVMPITState {
46     PITCommonState parent_obj;
47 
48     LostTickPolicy lost_tick_policy;
49     bool vm_stopped;
50     int64_t kernel_clock_offset;
51 } KVMPITState;
52 
53 typedef struct KVMPITClass {
54     PITCommonClass parent_class;
55 
56     DeviceRealize parent_realize;
57 } KVMPITClass;
58 
59 static int64_t abs64(int64_t v)
60 {
61     return v < 0 ? -v : v;
62 }
63 
64 static void kvm_pit_update_clock_offset(KVMPITState *s)
65 {
66     int64_t offset, clock_offset;
67     struct timespec ts;
68     int i;
69 
70     /*
71      * Measure the delta between CLOCK_MONOTONIC, the base used for
72      * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
73      * minimum of several samples to filter out scheduling noise.
74      */
75     clock_offset = INT64_MAX;
76     for (i = 0; i < CALIBRATION_ROUNDS; i++) {
77         offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
78         clock_gettime(CLOCK_MONOTONIC, &ts);
79         offset -= ts.tv_nsec;
80         offset -= (int64_t)ts.tv_sec * 1000000000;
81         if (abs64(offset) < abs64(clock_offset)) {
82             clock_offset = offset;
83         }
84     }
85     s->kernel_clock_offset = clock_offset;
86 }
87 
88 static void kvm_pit_get(PITCommonState *pit)
89 {
90     KVMPITState *s = KVM_PIT(pit);
91     struct kvm_pit_state2 kpit;
92     struct kvm_pit_channel_state *kchan;
93     struct PITChannelState *sc;
94     int i, ret;
95 
96     /* No need to re-read the state if VM is stopped. */
97     if (s->vm_stopped) {
98         return;
99     }
100 
101     if (kvm_has_pit_state2()) {
102         ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit);
103         if (ret < 0) {
104             fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(ret));
105             abort();
106         }
107         pit->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY;
108     } else {
109         /*
110          * kvm_pit_state2 is superset of kvm_pit_state struct,
111          * so we can use it for KVM_GET_PIT as well.
112          */
113         ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT, &kpit);
114         if (ret < 0) {
115             fprintf(stderr, "KVM_GET_PIT failed: %s\n", strerror(ret));
116             abort();
117         }
118     }
119     for (i = 0; i < 3; i++) {
120         kchan = &kpit.channels[i];
121         sc = &pit->channels[i];
122         sc->count = kchan->count;
123         sc->latched_count = kchan->latched_count;
124         sc->count_latched = kchan->count_latched;
125         sc->status_latched = kchan->status_latched;
126         sc->status = kchan->status;
127         sc->read_state = kchan->read_state;
128         sc->write_state = kchan->write_state;
129         sc->write_latch = kchan->write_latch;
130         sc->rw_mode = kchan->rw_mode;
131         sc->mode = kchan->mode;
132         sc->bcd = kchan->bcd;
133         sc->gate = kchan->gate;
134         sc->count_load_time = kchan->count_load_time + s->kernel_clock_offset;
135     }
136 
137     sc = &pit->channels[0];
138     sc->next_transition_time =
139         pit_get_next_transition_time(sc, sc->count_load_time);
140 }
141 
142 static void kvm_pit_put(PITCommonState *pit)
143 {
144     KVMPITState *s = KVM_PIT(pit);
145     struct kvm_pit_state2 kpit = {};
146     struct kvm_pit_channel_state *kchan;
147     struct PITChannelState *sc;
148     int i, ret;
149 
150     /* The offset keeps changing as long as the VM is stopped. */
151     if (s->vm_stopped) {
152         kvm_pit_update_clock_offset(s);
153     }
154 
155     kpit.flags = pit->channels[0].irq_disabled ? KVM_PIT_FLAGS_HPET_LEGACY : 0;
156     for (i = 0; i < 3; i++) {
157         kchan = &kpit.channels[i];
158         sc = &pit->channels[i];
159         kchan->count = sc->count;
160         kchan->latched_count = sc->latched_count;
161         kchan->count_latched = sc->count_latched;
162         kchan->status_latched = sc->status_latched;
163         kchan->status = sc->status;
164         kchan->read_state = sc->read_state;
165         kchan->write_state = sc->write_state;
166         kchan->write_latch = sc->write_latch;
167         kchan->rw_mode = sc->rw_mode;
168         kchan->mode = sc->mode;
169         kchan->bcd = sc->bcd;
170         kchan->gate = sc->gate;
171         kchan->count_load_time = sc->count_load_time - s->kernel_clock_offset;
172     }
173 
174     ret = kvm_vm_ioctl(kvm_state,
175                        kvm_has_pit_state2() ? KVM_SET_PIT2 : KVM_SET_PIT,
176                        &kpit);
177     if (ret < 0) {
178         fprintf(stderr, "%s failed: %s\n",
179                 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
180                 strerror(ret));
181         abort();
182     }
183 }
184 
185 static void kvm_pit_set_gate(PITCommonState *s, PITChannelState *sc, int val)
186 {
187     kvm_pit_get(s);
188 
189     switch (sc->mode) {
190     default:
191     case 0:
192     case 4:
193         /* XXX: just disable/enable counting */
194         break;
195     case 1:
196     case 2:
197     case 3:
198     case 5:
199         if (sc->gate < val) {
200             /* restart counting on rising edge */
201             sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
202         }
203         break;
204     }
205     sc->gate = val;
206 
207     kvm_pit_put(s);
208 }
209 
210 static void kvm_pit_get_channel_info(PITCommonState *s, PITChannelState *sc,
211                                      PITChannelInfo *info)
212 {
213     kvm_pit_get(s);
214 
215     pit_get_channel_info_common(s, sc, info);
216 }
217 
218 static void kvm_pit_reset(DeviceState *dev)
219 {
220     PITCommonState *s = PIT_COMMON(dev);
221 
222     pit_reset_common(s);
223 
224     kvm_pit_put(s);
225 }
226 
227 static void kvm_pit_irq_control(void *opaque, int n, int enable)
228 {
229     PITCommonState *pit = opaque;
230     PITChannelState *s = &pit->channels[0];
231 
232     kvm_pit_get(pit);
233 
234     s->irq_disabled = !enable;
235 
236     kvm_pit_put(pit);
237 }
238 
239 static void kvm_pit_vm_state_change(void *opaque, int running,
240                                     RunState state)
241 {
242     KVMPITState *s = opaque;
243 
244     if (running) {
245         kvm_pit_update_clock_offset(s);
246         kvm_pit_put(PIT_COMMON(s));
247         s->vm_stopped = false;
248     } else {
249         kvm_pit_update_clock_offset(s);
250         kvm_pit_get(PIT_COMMON(s));
251         s->vm_stopped = true;
252     }
253 }
254 
255 static void kvm_pit_realizefn(DeviceState *dev, Error **errp)
256 {
257     PITCommonState *pit = PIT_COMMON(dev);
258     KVMPITClass *kpc = KVM_PIT_GET_CLASS(dev);
259     KVMPITState *s = KVM_PIT(pit);
260     struct kvm_pit_config config = {
261         .flags = 0,
262     };
263     int ret;
264 
265     if (kvm_check_extension(kvm_state, KVM_CAP_PIT2)) {
266         ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT2, &config);
267     } else {
268         ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT);
269     }
270     if (ret < 0) {
271         error_setg(errp, "Create kernel PIC irqchip failed: %s",
272                    strerror(ret));
273         return;
274     }
275     switch (s->lost_tick_policy) {
276     case LOST_TICK_POLICY_DELAY:
277         break; /* enabled by default */
278     case LOST_TICK_POLICY_DISCARD:
279         if (kvm_check_extension(kvm_state, KVM_CAP_REINJECT_CONTROL)) {
280             struct kvm_reinject_control control = { .pit_reinject = 0 };
281 
282             ret = kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
283             if (ret < 0) {
284                 error_setg(errp,
285                            "Can't disable in-kernel PIT reinjection: %s",
286                            strerror(ret));
287                 return;
288             }
289         }
290         break;
291     default:
292         error_setg(errp, "Lost tick policy not supported.");
293         return;
294     }
295 
296     memory_region_init_io(&pit->ioports, OBJECT(dev), NULL, NULL, "kvm-pit", 4);
297 
298     qdev_init_gpio_in(dev, kvm_pit_irq_control, 1);
299 
300     qemu_add_vm_change_state_handler(kvm_pit_vm_state_change, s);
301 
302     kpc->parent_realize(dev, errp);
303 }
304 
305 static Property kvm_pit_properties[] = {
306     DEFINE_PROP_UINT32("iobase", PITCommonState, iobase,  -1),
307     DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState,
308                                lost_tick_policy, LOST_TICK_POLICY_DELAY),
309     DEFINE_PROP_END_OF_LIST(),
310 };
311 
312 static void kvm_pit_class_init(ObjectClass *klass, void *data)
313 {
314     KVMPITClass *kpc = KVM_PIT_CLASS(klass);
315     PITCommonClass *k = PIT_COMMON_CLASS(klass);
316     DeviceClass *dc = DEVICE_CLASS(klass);
317 
318     device_class_set_parent_realize(dc, kvm_pit_realizefn,
319                                     &kpc->parent_realize);
320     k->set_channel_gate = kvm_pit_set_gate;
321     k->get_channel_info = kvm_pit_get_channel_info;
322     dc->reset = kvm_pit_reset;
323     dc->props = kvm_pit_properties;
324 }
325 
326 static const TypeInfo kvm_pit_info = {
327     .name          = TYPE_KVM_I8254,
328     .parent        = TYPE_PIT_COMMON,
329     .instance_size = sizeof(KVMPITState),
330     .class_init = kvm_pit_class_init,
331     .class_size = sizeof(KVMPITClass),
332 };
333 
334 static void kvm_pit_register(void)
335 {
336     type_register_static(&kvm_pit_info);
337 }
338 
339 type_init(kvm_pit_register)
340