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