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