]> git.proxmox.com Git - qemu.git/blame - hw/kvm/i8254.c
kvm/apic: correct short memset
[qemu.git] / hw / 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 */
25#include "qemu-timer.h"
26#include "hw/i8254.h"
27#include "hw/i8254_internal.h"
28#include "kvm.h"
29
30#define KVM_PIT_REINJECT_BIT 0
31
32typedef struct KVMPITState {
33 PITCommonState pit;
34 LostTickPolicy lost_tick_policy;
35} KVMPITState;
36
37static void kvm_pit_get(PITCommonState *s)
38{
39 struct kvm_pit_state2 kpit;
40 struct kvm_pit_channel_state *kchan;
41 struct PITChannelState *sc;
42 int i, ret;
43
44 if (kvm_has_pit_state2()) {
45 ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit);
46 if (ret < 0) {
47 fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(ret));
48 abort();
49 }
50 s->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY;
51 } else {
52 /*
53 * kvm_pit_state2 is superset of kvm_pit_state struct,
54 * so we can use it for KVM_GET_PIT as well.
55 */
56 ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT, &kpit);
57 if (ret < 0) {
58 fprintf(stderr, "KVM_GET_PIT failed: %s\n", strerror(ret));
59 abort();
60 }
61 }
62 for (i = 0; i < 3; i++) {
63 kchan = &kpit.channels[i];
64 sc = &s->channels[i];
65 sc->count = kchan->count;
66 sc->latched_count = kchan->latched_count;
67 sc->count_latched = kchan->count_latched;
68 sc->status_latched = kchan->status_latched;
69 sc->status = kchan->status;
70 sc->read_state = kchan->read_state;
71 sc->write_state = kchan->write_state;
72 sc->write_latch = kchan->write_latch;
73 sc->rw_mode = kchan->rw_mode;
74 sc->mode = kchan->mode;
75 sc->bcd = kchan->bcd;
76 sc->gate = kchan->gate;
77 sc->count_load_time = kchan->count_load_time;
78 }
79
80 sc = &s->channels[0];
81 sc->next_transition_time =
82 pit_get_next_transition_time(sc, sc->count_load_time);
83}
84
85static void kvm_pit_put(PITCommonState *s)
86{
87 struct kvm_pit_state2 kpit;
88 struct kvm_pit_channel_state *kchan;
89 struct PITChannelState *sc;
90 int i, ret;
91
92 kpit.flags = s->channels[0].irq_disabled ? KVM_PIT_FLAGS_HPET_LEGACY : 0;
93 for (i = 0; i < 3; i++) {
94 kchan = &kpit.channels[i];
95 sc = &s->channels[i];
96 kchan->count = sc->count;
97 kchan->latched_count = sc->latched_count;
98 kchan->count_latched = sc->count_latched;
99 kchan->status_latched = sc->status_latched;
100 kchan->status = sc->status;
101 kchan->read_state = sc->read_state;
102 kchan->write_state = sc->write_state;
103 kchan->write_latch = sc->write_latch;
104 kchan->rw_mode = sc->rw_mode;
105 kchan->mode = sc->mode;
106 kchan->bcd = sc->bcd;
107 kchan->gate = sc->gate;
108 kchan->count_load_time = sc->count_load_time;
109 }
110
111 ret = kvm_vm_ioctl(kvm_state,
112 kvm_has_pit_state2() ? KVM_SET_PIT2 : KVM_SET_PIT,
113 &kpit);
114 if (ret < 0) {
115 fprintf(stderr, "%s failed: %s\n",
116 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
117 strerror(ret));
118 abort();
119 }
120}
121
122static void kvm_pit_set_gate(PITCommonState *s, PITChannelState *sc, int val)
123{
124 kvm_pit_get(s);
125
126 switch (sc->mode) {
127 default:
128 case 0:
129 case 4:
130 /* XXX: just disable/enable counting */
131 break;
132 case 1:
133 case 2:
134 case 3:
135 case 5:
136 if (sc->gate < val) {
137 /* restart counting on rising edge */
138 sc->count_load_time = qemu_get_clock_ns(vm_clock);
139 }
140 break;
141 }
142 sc->gate = val;
143
144 kvm_pit_put(s);
145}
146
147static void kvm_pit_get_channel_info(PITCommonState *s, PITChannelState *sc,
148 PITChannelInfo *info)
149{
150 kvm_pit_get(s);
151
152 pit_get_channel_info_common(s, sc, info);
153}
154
155static void kvm_pit_reset(DeviceState *dev)
156{
157 PITCommonState *s = DO_UPCAST(PITCommonState, dev.qdev, dev);
158
159 pit_reset_common(s);
160
161 kvm_pit_put(s);
162}
163
164static void kvm_pit_irq_control(void *opaque, int n, int enable)
165{
166 PITCommonState *pit = opaque;
167 PITChannelState *s = &pit->channels[0];
168
169 kvm_pit_get(pit);
170
171 s->irq_disabled = !enable;
172
173 kvm_pit_put(pit);
174}
175
176static int kvm_pit_initfn(PITCommonState *pit)
177{
178 KVMPITState *s = DO_UPCAST(KVMPITState, pit, pit);
179 struct kvm_pit_config config = {
180 .flags = 0,
181 };
182 int ret;
183
184 if (kvm_check_extension(kvm_state, KVM_CAP_PIT2)) {
185 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT2, &config);
186 } else {
187 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT);
188 }
189 if (ret < 0) {
190 fprintf(stderr, "Create kernel PIC irqchip failed: %s\n",
191 strerror(ret));
192 return ret;
193 }
194 switch (s->lost_tick_policy) {
195 case LOST_TICK_DELAY:
196 break; /* enabled by default */
197 case LOST_TICK_DISCARD:
198 if (kvm_check_extension(kvm_state, KVM_CAP_REINJECT_CONTROL)) {
199 struct kvm_reinject_control control = { .pit_reinject = 0 };
200
201 ret = kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
202 if (ret < 0) {
203 fprintf(stderr,
204 "Can't disable in-kernel PIT reinjection: %s\n",
205 strerror(ret));
206 return ret;
207 }
208 }
209 break;
210 default:
211 return -EINVAL;
212 }
213
214 memory_region_init_reservation(&pit->ioports, "kvm-pit", 4);
215
216 qdev_init_gpio_in(&pit->dev.qdev, kvm_pit_irq_control, 1);
217
218 return 0;
219}
220
221static Property kvm_pit_properties[] = {
222 DEFINE_PROP_HEX32("iobase", KVMPITState, pit.iobase, -1),
223 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState,
224 lost_tick_policy, LOST_TICK_DELAY),
225 DEFINE_PROP_END_OF_LIST(),
226};
227
228static void kvm_pit_class_init(ObjectClass *klass, void *data)
229{
230 PITCommonClass *k = PIT_COMMON_CLASS(klass);
231 DeviceClass *dc = DEVICE_CLASS(klass);
232
233 k->init = kvm_pit_initfn;
234 k->set_channel_gate = kvm_pit_set_gate;
235 k->get_channel_info = kvm_pit_get_channel_info;
236 k->pre_save = kvm_pit_get;
237 k->post_load = kvm_pit_put;
238 dc->reset = kvm_pit_reset;
239 dc->props = kvm_pit_properties;
240}
241
242static TypeInfo kvm_pit_info = {
243 .name = "kvm-pit",
244 .parent = TYPE_PIT_COMMON,
245 .instance_size = sizeof(KVMPITState),
246 .class_init = kvm_pit_class_init,
247};
248
249static void kvm_pit_register(void)
250{
251 type_register_static(&kvm_pit_info);
252}
253
254type_init(kvm_pit_register)