]>
Commit | Line | Data |
---|---|---|
5d17c0d2 JK |
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 | */ | |
a9c94277 | 25 | |
b6a0aa05 | 26 | #include "qemu/osdep.h" |
a9c94277 | 27 | #include <linux/kvm.h> |
da34e65c | 28 | #include "qapi/error.h" |
0b8fa32f | 29 | #include "qemu/module.h" |
1de7afc9 | 30 | #include "qemu/timer.h" |
9c17d615 | 31 | #include "sysemu/sysemu.h" |
0d09e41a PB |
32 | #include "hw/timer/i8254.h" |
33 | #include "hw/timer/i8254_internal.h" | |
9c17d615 | 34 | #include "sysemu/kvm.h" |
5d17c0d2 JK |
35 | |
36 | #define KVM_PIT_REINJECT_BIT 0 | |
37 | ||
0cdd3d14 JK |
38 | #define CALIBRATION_ROUNDS 3 |
39 | ||
58cd9864 | 40 | #define KVM_PIT(obj) OBJECT_CHECK(KVMPITState, (obj), TYPE_KVM_I8254) |
a15d0912 AF |
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) | |
58cd9864 | 45 | |
5d17c0d2 | 46 | typedef struct KVMPITState { |
58cd9864 AF |
47 | PITCommonState parent_obj; |
48 | ||
5d17c0d2 | 49 | LostTickPolicy lost_tick_policy; |
205df4d1 JK |
50 | bool vm_stopped; |
51 | int64_t kernel_clock_offset; | |
5d17c0d2 JK |
52 | } KVMPITState; |
53 | ||
a15d0912 AF |
54 | typedef struct KVMPITClass { |
55 | PITCommonClass parent_class; | |
56 | ||
57 | DeviceRealize parent_realize; | |
58 | } KVMPITClass; | |
59 | ||
0cdd3d14 | 60 | static int64_t abs64(int64_t v) |
5d17c0d2 | 61 | { |
0cdd3d14 JK |
62 | return v < 0 ? -v : v; |
63 | } | |
64 | ||
205df4d1 | 65 | static void kvm_pit_update_clock_offset(KVMPITState *s) |
0cdd3d14 | 66 | { |
0cdd3d14 JK |
67 | int64_t offset, clock_offset; |
68 | struct timespec ts; | |
205df4d1 | 69 | int i; |
0cdd3d14 JK |
70 | |
71 | /* | |
72 | * Measure the delta between CLOCK_MONOTONIC, the base used for | |
bc72ad67 | 73 | * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the |
0cdd3d14 JK |
74 | * minimum of several samples to filter out scheduling noise. |
75 | */ | |
76 | clock_offset = INT64_MAX; | |
77 | for (i = 0; i < CALIBRATION_ROUNDS; i++) { | |
bc72ad67 | 78 | offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
0cdd3d14 JK |
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 | } | |
205df4d1 JK |
86 | s->kernel_clock_offset = clock_offset; |
87 | } | |
88 | ||
89 | static void kvm_pit_get(PITCommonState *pit) | |
90 | { | |
58cd9864 | 91 | KVMPITState *s = KVM_PIT(pit); |
205df4d1 JK |
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 | } | |
0cdd3d14 | 101 | |
5d17c0d2 JK |
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 | } | |
0cdd3d14 | 108 | pit->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY; |
5d17c0d2 JK |
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]; | |
0cdd3d14 | 122 | sc = &pit->channels[i]; |
5d17c0d2 JK |
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; | |
205df4d1 | 135 | sc->count_load_time = kchan->count_load_time + s->kernel_clock_offset; |
5d17c0d2 JK |
136 | } |
137 | ||
0cdd3d14 | 138 | sc = &pit->channels[0]; |
5d17c0d2 JK |
139 | sc->next_transition_time = |
140 | pit_get_next_transition_time(sc, sc->count_load_time); | |
141 | } | |
142 | ||
050a4606 | 143 | static void kvm_pit_put(PITCommonState *pit) |
5d17c0d2 | 144 | { |
58cd9864 | 145 | KVMPITState *s = KVM_PIT(pit); |
b0a05512 | 146 | struct kvm_pit_state2 kpit = {}; |
5d17c0d2 JK |
147 | struct kvm_pit_channel_state *kchan; |
148 | struct PITChannelState *sc; | |
149 | int i, ret; | |
150 | ||
050a4606 JK |
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; | |
5d17c0d2 JK |
157 | for (i = 0; i < 3; i++) { |
158 | kchan = &kpit.channels[i]; | |
050a4606 | 159 | sc = &pit->channels[i]; |
5d17c0d2 JK |
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; | |
050a4606 | 172 | kchan->count_load_time = sc->count_load_time - s->kernel_clock_offset; |
5d17c0d2 JK |
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 */ | |
bc72ad67 | 202 | sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
5d17c0d2 JK |
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 | { | |
58cd9864 | 221 | PITCommonState *s = PIT_COMMON(dev); |
5d17c0d2 JK |
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 | ||
0cdd3d14 JK |
240 | static void kvm_pit_vm_state_change(void *opaque, int running, |
241 | RunState state) | |
242 | { | |
243 | KVMPITState *s = opaque; | |
244 | ||
245 | if (running) { | |
205df4d1 | 246 | kvm_pit_update_clock_offset(s); |
be894f51 | 247 | kvm_pit_put(PIT_COMMON(s)); |
205df4d1 | 248 | s->vm_stopped = false; |
0cdd3d14 | 249 | } else { |
205df4d1 | 250 | kvm_pit_update_clock_offset(s); |
58cd9864 | 251 | kvm_pit_get(PIT_COMMON(s)); |
205df4d1 | 252 | s->vm_stopped = true; |
0cdd3d14 JK |
253 | } |
254 | } | |
255 | ||
a15d0912 | 256 | static void kvm_pit_realizefn(DeviceState *dev, Error **errp) |
5d17c0d2 | 257 | { |
a15d0912 AF |
258 | PITCommonState *pit = PIT_COMMON(dev); |
259 | KVMPITClass *kpc = KVM_PIT_GET_CLASS(dev); | |
58cd9864 | 260 | KVMPITState *s = KVM_PIT(pit); |
5d17c0d2 JK |
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) { | |
a15d0912 AF |
272 | error_setg(errp, "Create kernel PIC irqchip failed: %s", |
273 | strerror(ret)); | |
274 | return; | |
5d17c0d2 JK |
275 | } |
276 | switch (s->lost_tick_policy) { | |
104059da | 277 | case LOST_TICK_POLICY_DELAY: |
5d17c0d2 | 278 | break; /* enabled by default */ |
104059da | 279 | case LOST_TICK_POLICY_DISCARD: |
5d17c0d2 JK |
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) { | |
a15d0912 AF |
285 | error_setg(errp, |
286 | "Can't disable in-kernel PIT reinjection: %s", | |
287 | strerror(ret)); | |
288 | return; | |
5d17c0d2 JK |
289 | } |
290 | } | |
291 | break; | |
292 | default: | |
a15d0912 AF |
293 | error_setg(errp, "Lost tick policy not supported."); |
294 | return; | |
5d17c0d2 JK |
295 | } |
296 | ||
257a7430 | 297 | memory_region_init_io(&pit->ioports, OBJECT(dev), NULL, NULL, "kvm-pit", 4); |
5d17c0d2 | 298 | |
a15d0912 | 299 | qdev_init_gpio_in(dev, kvm_pit_irq_control, 1); |
5d17c0d2 | 300 | |
0cdd3d14 JK |
301 | qemu_add_vm_change_state_handler(kvm_pit_vm_state_change, s); |
302 | ||
a15d0912 | 303 | kpc->parent_realize(dev, errp); |
5d17c0d2 JK |
304 | } |
305 | ||
306 | static Property kvm_pit_properties[] = { | |
c7bcc85d | 307 | DEFINE_PROP_UINT32("iobase", PITCommonState, iobase, -1), |
5d17c0d2 | 308 | DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState, |
104059da | 309 | lost_tick_policy, LOST_TICK_POLICY_DELAY), |
5d17c0d2 JK |
310 | DEFINE_PROP_END_OF_LIST(), |
311 | }; | |
312 | ||
313 | static void kvm_pit_class_init(ObjectClass *klass, void *data) | |
314 | { | |
a15d0912 | 315 | KVMPITClass *kpc = KVM_PIT_CLASS(klass); |
5d17c0d2 JK |
316 | PITCommonClass *k = PIT_COMMON_CLASS(klass); |
317 | DeviceClass *dc = DEVICE_CLASS(klass); | |
318 | ||
bf853881 PMD |
319 | device_class_set_parent_realize(dc, kvm_pit_realizefn, |
320 | &kpc->parent_realize); | |
5d17c0d2 JK |
321 | k->set_channel_gate = kvm_pit_set_gate; |
322 | k->get_channel_info = kvm_pit_get_channel_info; | |
5d17c0d2 JK |
323 | dc->reset = kvm_pit_reset; |
324 | dc->props = kvm_pit_properties; | |
325 | } | |
326 | ||
8c43a6f0 | 327 | static const TypeInfo kvm_pit_info = { |
58cd9864 | 328 | .name = TYPE_KVM_I8254, |
5d17c0d2 JK |
329 | .parent = TYPE_PIT_COMMON, |
330 | .instance_size = sizeof(KVMPITState), | |
331 | .class_init = kvm_pit_class_init, | |
a15d0912 | 332 | .class_size = sizeof(KVMPITClass), |
5d17c0d2 JK |
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) |