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