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