Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/timer/i8254_common.c
Line
Count
Source
1
/*
2
 * QEMU 8253/8254 - common bits of emulated and KVM kernel model
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 "hw/isa/isa.h"
28
#include "qemu/module.h"
29
#include "qemu/timer.h"
30
#include "hw/timer/i8254.h"
31
#include "hw/timer/i8254_internal.h"
32
#include "migration/vmstate.h"
33
34
/* val must be 0 or 1 */
35
void pit_set_gate(PITCommonState *pit, int channel, int val)
36
0
{
37
0
    PITChannelState *s = &pit->channels[channel];
38
0
    PITCommonClass *c = PIT_COMMON_GET_CLASS(pit);
39
40
0
    c->set_channel_gate(pit, s, val);
41
0
}
42
43
/* get pit output bit */
44
int pit_get_out(PITChannelState *s, int64_t current_time)
45
18.1M
{
46
18.1M
    uint64_t d;
47
18.1M
    int out;
48
49
18.1M
    d = muldiv64(current_time - s->count_load_time, PIT_FREQ,
50
18.1M
                 NANOSECONDS_PER_SECOND);
51
18.1M
    switch (s->mode) {
52
0
    default:
53
63
    case 0:
54
63
    case 1:
55
63
        out = (d >= s->count);
56
63
        break;
57
0
    case 2:
58
0
        if ((d % s->count) == 0 && d != 0) {
59
0
            out = 1;
60
0
        } else {
61
0
            out = 0;
62
0
        }
63
0
        break;
64
18.1M
    case 3:
65
18.1M
        out = (d % s->count) < ((s->count + 1) >> 1);
66
18.1M
        break;
67
0
    case 4:
68
0
    case 5:
69
0
        out = (d == s->count);
70
0
        break;
71
18.1M
    }
72
18.1M
    return out;
73
18.1M
}
74
75
/* return -1 if no transition will occur.  */
76
int64_t pit_get_next_transition_time(PITChannelState *s, int64_t current_time)
77
18.2M
{
78
18.2M
    uint64_t d, next_time, base;
79
18.2M
    int period2;
80
81
18.2M
    d = muldiv64(current_time - s->count_load_time, PIT_FREQ,
82
18.2M
                 NANOSECONDS_PER_SECOND);
83
18.2M
    switch (s->mode) {
84
0
    default:
85
63
    case 0:
86
63
    case 1:
87
63
        if (d < s->count) {
88
0
            next_time = s->count;
89
63
        } else {
90
63
            return -1;
91
63
        }
92
0
        break;
93
0
    case 2:
94
0
        base = QEMU_ALIGN_DOWN(d, s->count);
95
0
        if ((d - base) == 0 && d != 0) {
96
0
            next_time = base + s->count;
97
0
        } else {
98
0
            next_time = base + s->count + 1;
99
0
        }
100
0
        break;
101
18.2M
    case 3:
102
18.2M
        base = QEMU_ALIGN_DOWN(d, s->count);
103
18.2M
        period2 = ((s->count + 1) >> 1);
104
18.2M
        if ((d - base) < period2) {
105
9.18M
            next_time = base + period2;
106
9.18M
        } else {
107
9.04M
            next_time = base + s->count;
108
9.04M
        }
109
18.2M
        break;
110
0
    case 4:
111
0
    case 5:
112
0
        if (d < s->count) {
113
0
            next_time = s->count;
114
0
        } else if (d == s->count) {
115
0
            next_time = s->count + 1;
116
0
        } else {
117
0
            return -1;
118
0
        }
119
0
        break;
120
18.2M
    }
121
    /* convert to timer units */
122
18.2M
    next_time = s->count_load_time + muldiv64(next_time, NANOSECONDS_PER_SECOND,
123
18.2M
                                              PIT_FREQ);
124
    /* fix potential rounding problems */
125
    /* XXX: better solution: use a clock at PIT_FREQ Hz */
126
18.2M
    if (next_time <= current_time) {
127
9.04M
        next_time = current_time + 1;
128
9.04M
    }
129
18.2M
    return next_time;
130
18.2M
}
131
132
void pit_get_channel_info_common(PITCommonState *s, PITChannelState *sc,
133
                                 PITChannelInfo *info)
134
0
{
135
0
    info->gate = sc->gate;
136
0
    info->mode = sc->mode;
137
0
    info->initial_count = sc->count;
138
0
    info->out = pit_get_out(sc, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
139
0
}
140
141
void pit_get_channel_info(PITCommonState *pit, int channel, PITChannelInfo *info)
142
0
{
143
0
    PITChannelState *s = &pit->channels[channel];
144
0
    PITCommonClass *c = PIT_COMMON_GET_CLASS(pit);
145
146
0
    c->get_channel_info(pit, s, info);
147
0
}
148
149
void pit_reset_common(PITCommonState *pit)
150
72.6k
{
151
72.6k
    PITChannelState *s;
152
72.6k
    int i;
153
154
290k
    for (i = 0; i < 3; i++) {
155
218k
        s = &pit->channels[i];
156
218k
        s->mode = 3;
157
218k
        s->gate = (i != 2);
158
218k
        s->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
159
218k
        s->count = 0x10000;
160
218k
        if (i == 0 && !s->irq_disabled) {
161
72.6k
            s->next_transition_time =
162
72.6k
                pit_get_next_transition_time(s, s->count_load_time);
163
72.6k
        }
164
218k
    }
165
72.6k
}
166
167
static void pit_common_realize(DeviceState *dev, Error **errp)
168
63
{
169
63
    ISADevice *isadev = ISA_DEVICE(dev);
170
63
    PITCommonState *pit = PIT_COMMON(dev);
171
172
63
    isa_register_ioport(isadev, &pit->ioports, pit->iobase);
173
174
63
    qdev_set_legacy_instance_id(dev, pit->iobase, 2);
175
63
}
176
177
static const VMStateDescription vmstate_pit_channel = {
178
    .name = "pit channel",
179
    .version_id = 2,
180
    .minimum_version_id = 2,
181
    .fields = (const VMStateField[]) {
182
        VMSTATE_INT32(count, PITChannelState),
183
        VMSTATE_UINT16(latched_count, PITChannelState),
184
        VMSTATE_UINT8(count_latched, PITChannelState),
185
        VMSTATE_UINT8(status_latched, PITChannelState),
186
        VMSTATE_UINT8(status, PITChannelState),
187
        VMSTATE_UINT8(read_state, PITChannelState),
188
        VMSTATE_UINT8(write_state, PITChannelState),
189
        VMSTATE_UINT8(write_latch, PITChannelState),
190
        VMSTATE_UINT8(rw_mode, PITChannelState),
191
        VMSTATE_UINT8(mode, PITChannelState),
192
        VMSTATE_UINT8(bcd, PITChannelState),
193
        VMSTATE_UINT8(gate, PITChannelState),
194
        VMSTATE_INT64(count_load_time, PITChannelState),
195
        VMSTATE_INT64(next_transition_time, PITChannelState),
196
        VMSTATE_END_OF_LIST()
197
    }
198
};
199
200
static int pit_dispatch_pre_save(void *opaque)
201
0
{
202
0
    PITCommonState *s = opaque;
203
0
    PITCommonClass *c = PIT_COMMON_GET_CLASS(s);
204
205
0
    if (c->pre_save) {
206
0
        c->pre_save(s);
207
0
    }
208
209
0
    return 0;
210
0
}
211
212
static int pit_dispatch_post_load(void *opaque, int version_id)
213
0
{
214
0
    PITCommonState *s = opaque;
215
0
    PITCommonClass *c = PIT_COMMON_GET_CLASS(s);
216
217
0
    if (c->post_load) {
218
0
        c->post_load(s);
219
0
    }
220
0
    return 0;
221
0
}
222
223
static const VMStateDescription vmstate_pit_common = {
224
    .name = "i8254",
225
    .version_id = 3,
226
    .minimum_version_id = 2,
227
    .pre_save = pit_dispatch_pre_save,
228
    .post_load = pit_dispatch_post_load,
229
    .fields = (const VMStateField[]) {
230
        VMSTATE_UINT32_V(channels[0].irq_disabled, PITCommonState, 3),
231
        VMSTATE_STRUCT_ARRAY(channels, PITCommonState, 3, 2,
232
                             vmstate_pit_channel, PITChannelState),
233
        VMSTATE_INT64(channels[0].next_transition_time,
234
                      PITCommonState), /* formerly irq_timer */
235
        VMSTATE_END_OF_LIST()
236
    }
237
};
238
239
static const Property pit_common_properties[] = {
240
    DEFINE_PROP_UINT32("iobase", PITCommonState, iobase,  -1),
241
};
242
243
static void pit_common_class_init(ObjectClass *klass, const void *data)
244
63
{
245
63
    DeviceClass *dc = DEVICE_CLASS(klass);
246
247
63
    dc->realize = pit_common_realize;
248
63
    dc->vmsd = &vmstate_pit_common;
249
    /*
250
     * Reason: unlike ordinary ISA devices, the PIT may need to be
251
     * wired to the HPET, and because of that, some wiring is always
252
     * done by board code.
253
     */
254
63
    dc->user_creatable = false;
255
63
    device_class_set_props(dc, pit_common_properties);
256
63
}
257
258
static const TypeInfo pit_common_type = {
259
    .name          = TYPE_PIT_COMMON,
260
    .parent        = TYPE_ISA_DEVICE,
261
    .instance_size = sizeof(PITCommonState),
262
    .class_size    = sizeof(PITCommonClass),
263
    .class_init    = pit_common_class_init,
264
    .abstract      = true,
265
};
266
267
static void register_devices(void)
268
63
{
269
63
    type_register_static(&pit_common_type);
270
63
}
271
272
type_init(register_devices);