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