VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevPit-i8254.cpp@ 24213

Last change on this file since 24213 was 24087, checked in by vboxsync, 15 years ago

DevPit-i8254: Save and verify config. Saved state version changed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 39.1 KB
Line 
1/* $Id: DevPit-i8254.cpp 24087 2009-10-26 15:48:03Z vboxsync $ */
2/** @file
3 * DevPIT-i8254 - Intel 8254 Programmable Interval Timer (PIT) And Dummy Speaker Device.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 * --------------------------------------------------------------------
21 *
22 * This code is based on:
23 *
24 * QEMU 8253/8254 interval timer emulation
25 *
26 * Copyright (c) 2003-2004 Fabrice Bellard
27 *
28 * Permission is hereby granted, free of charge, to any person obtaining a copy
29 * of this software and associated documentation files (the "Software"), to deal
30 * in the Software without restriction, including without limitation the rights
31 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32 * copies of the Software, and to permit persons to whom the Software is
33 * furnished to do so, subject to the following conditions:
34 *
35 * The above copyright notice and this permission notice shall be included in
36 * all copies or substantial portions of the Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
41 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 * THE SOFTWARE.
45 */
46
47/*******************************************************************************
48* Header Files *
49*******************************************************************************/
50#define LOG_GROUP LOG_GROUP_DEV_PIT
51#include <VBox/pdmdev.h>
52#include <VBox/log.h>
53#include <VBox/stam.h>
54#include <iprt/assert.h>
55#include <iprt/asm.h>
56
57#include "../Builtins.h"
58
59
60/*******************************************************************************
61* Defined Constants And Macros *
62*******************************************************************************/
63/** The PIT frequency. */
64#define PIT_FREQ 1193182
65
66#define RW_STATE_LSB 1
67#define RW_STATE_MSB 2
68#define RW_STATE_WORD0 3
69#define RW_STATE_WORD1 4
70
71/** The current saved state version. */
72#define PIT_SAVED_STATE_VERSION 3
73/** The saved state version used by VirtualBox 3.0 and earlier.
74 * This did not include the config part. */
75#define PIT_SAVED_STATE_VERSION_VBOX_30 2
76
77/** @def FAKE_REFRESH_CLOCK
78 * Define this to flip the 15usec refresh bit on every read.
79 * If not defined, it will be flipped correctly. */
80/* #define FAKE_REFRESH_CLOCK */
81#ifdef DOXYGEN_RUNNING
82# define FAKE_REFRESH_CLOCK
83#endif
84
85
86/*******************************************************************************
87* Structures and Typedefs *
88*******************************************************************************/
89typedef struct PITChannelState
90{
91 /** Pointer to the instance data - R3 Ptr. */
92 R3PTRTYPE(struct PITState *) pPitR3;
93 /** The timer - R3 Ptr. */
94 PTMTIMERR3 pTimerR3;
95 /** Pointer to the instance data - R0 Ptr. */
96 R0PTRTYPE(struct PITState *) pPitR0;
97 /** The timer - R0 Ptr. */
98 PTMTIMERR0 pTimerR0;
99 /** Pointer to the instance data - RC Ptr. */
100 RCPTRTYPE(struct PITState *) pPitRC;
101 /** The timer - RC Ptr. */
102 PTMTIMERRC pTimerRC;
103 /** The virtual time stamp at the last reload. (only used in mode 2 for now) */
104 uint64_t u64ReloadTS;
105 /** The actual time of the next tick.
106 * As apposed to the next_transition_time which contains the correct time of the next tick. */
107 uint64_t u64NextTS;
108
109 /** (count_load_time is only set by TMTimerGet() which returns uint64_t) */
110 uint64_t count_load_time;
111 /* irq handling */
112 int64_t next_transition_time;
113 int32_t irq;
114 /** Number of release log entries. Used to prevent floading. */
115 uint32_t cRelLogEntries;
116
117 uint32_t count; /* can be 65536 */
118 uint16_t latched_count;
119 uint8_t count_latched;
120 uint8_t status_latched;
121
122 uint8_t status;
123 uint8_t read_state;
124 uint8_t write_state;
125 uint8_t write_latch;
126
127 uint8_t rw_mode;
128 uint8_t mode;
129 uint8_t bcd; /* not supported */
130 uint8_t gate; /* timer start */
131
132} PITChannelState;
133
134typedef struct PITState
135{
136 PITChannelState channels[3];
137 /** Speaker data. */
138 int32_t speaker_data_on;
139#ifdef FAKE_REFRESH_CLOCK
140 /** Speaker dummy. */
141 int32_t dummy_refresh_clock;
142#else
143 uint32_t Alignment1;
144#endif
145 /** Config: I/O port base. */
146 RTIOPORT IOPortBaseCfg;
147 /** Config: Speaker enabled. */
148 bool fSpeakerCfg;
149 bool afAlignment0[HC_ARCH_BITS == 32 ? 1 : 5];
150 /** Pointer to the device instance. */
151 PPDMDEVINSR3 pDevIns;
152 /** Number of IRQs that's been raised. */
153 STAMCOUNTER StatPITIrq;
154 /** Profiling the timer callback handler. */
155 STAMPROFILEADV StatPITHandler;
156} PITState;
157
158
159#ifndef VBOX_DEVICE_STRUCT_TESTCASE
160/*******************************************************************************
161* Internal Functions *
162*******************************************************************************/
163RT_C_DECLS_BEGIN
164PDMBOTHCBDECL(int) pitIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
165PDMBOTHCBDECL(int) pitIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
166PDMBOTHCBDECL(int) pitIOPortSpeakerRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
167#ifdef IN_RING3
168PDMBOTHCBDECL(int) pitIOPortSpeakerWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
169static void pit_irq_timer_update(PITChannelState *s, uint64_t current_time, uint64_t now);
170#endif
171RT_C_DECLS_END
172
173
174
175
176static int pit_get_count(PITChannelState *s)
177{
178 uint64_t d;
179 int counter;
180 PTMTIMER pTimer = s->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
181
182 if (s->mode == 2)
183 {
184 if (s->u64NextTS == UINT64_MAX)
185 {
186 d = ASMMultU64ByU32DivByU32(TMTimerGet(pTimer) - s->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
187 return s->count - (d % s->count); /** @todo check this value. */
188 }
189 uint64_t Interval = s->u64NextTS - s->u64ReloadTS;
190 if (!Interval)
191 return s->count - 1; /** @todo This is WRONG! But I'm too tired to fix it properly and just want to shut up a DIV/0 trap now. */
192 d = TMTimerGet(pTimer);
193 d = ASMMultU64ByU32DivByU32(d - s->u64ReloadTS, s->count, Interval);
194 if (d >= s->count)
195 return 1;
196 return s->count - d;
197 }
198 d = ASMMultU64ByU32DivByU32(TMTimerGet(pTimer) - s->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
199 switch(s->mode) {
200 case 0:
201 case 1:
202 case 4:
203 case 5:
204 counter = (s->count - d) & 0xffff;
205 break;
206 case 3:
207 /* XXX: may be incorrect for odd counts */
208 counter = s->count - ((2 * d) % s->count);
209 break;
210 default:
211 counter = s->count - (d % s->count);
212 break;
213 }
214 /** @todo check that we don't return 0, in most modes (all?) the counter shouldn't be zero. */
215 return counter;
216}
217
218/* get pit output bit */
219static int pit_get_out1(PITChannelState *s, int64_t current_time)
220{
221 uint64_t d;
222 PTMTIMER pTimer = s->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
223 int out;
224
225 d = ASMMultU64ByU32DivByU32(current_time - s->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
226 switch(s->mode) {
227 default:
228 case 0:
229 out = (d >= s->count);
230 break;
231 case 1:
232 out = (d < s->count);
233 break;
234 case 2:
235 Log2(("pit_get_out1: d=%llx c=%x %x \n", d, s->count, (unsigned)(d % s->count)));
236 if ((d % s->count) == 0 && d != 0)
237 out = 1;
238 else
239 out = 0;
240 break;
241 case 3:
242 out = (d % s->count) < ((s->count + 1) >> 1);
243 break;
244 case 4:
245 case 5:
246 out = (d == s->count);
247 break;
248 }
249 return out;
250}
251
252
253static int pit_get_out(PITState *pit, int channel, int64_t current_time)
254{
255 PITChannelState *s = &pit->channels[channel];
256 return pit_get_out1(s, current_time);
257}
258
259
260static int pit_get_gate(PITState *pit, int channel)
261{
262 PITChannelState *s = &pit->channels[channel];
263 return s->gate;
264}
265
266
267/* if already latched, do not latch again */
268static void pit_latch_count(PITChannelState *s)
269{
270 if (!s->count_latched) {
271 s->latched_count = pit_get_count(s);
272 s->count_latched = s->rw_mode;
273 LogFlow(("pit_latch_count: latched_count=%#06x / %10RU64 ns (c=%#06x m=%d)\n",
274 s->latched_count, ASMMultU64ByU32DivByU32(s->count - s->latched_count, 1000000000, PIT_FREQ), s->count, s->mode));
275 }
276}
277
278#ifdef IN_RING3
279
280/* val must be 0 or 1 */
281static void pit_set_gate(PITState *pit, int channel, int val)
282{
283 PITChannelState *s = &pit->channels[channel];
284 PTMTIMER pTimer = s->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
285 Assert((val & 1) == val);
286
287 switch(s->mode) {
288 default:
289 case 0:
290 case 4:
291 /* XXX: just disable/enable counting */
292 break;
293 case 1:
294 case 5:
295 if (s->gate < val) {
296 /* restart counting on rising edge */
297 Log(("pit_set_gate: restarting mode %d\n", s->mode));
298 s->count_load_time = TMTimerGet(pTimer);
299 pit_irq_timer_update(s, s->count_load_time, s->count_load_time);
300 }
301 break;
302 case 2:
303 case 3:
304 if (s->gate < val) {
305 /* restart counting on rising edge */
306 Log(("pit_set_gate: restarting mode %d\n", s->mode));
307 s->count_load_time = s->u64ReloadTS = TMTimerGet(pTimer);
308 pit_irq_timer_update(s, s->count_load_time, s->count_load_time);
309 }
310 /* XXX: disable/enable counting */
311 break;
312 }
313 s->gate = val;
314}
315
316DECLINLINE(void) pit_load_count(PITChannelState *s, int val)
317{
318 PTMTIMER pTimer = s->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
319 if (val == 0)
320 val = 0x10000;
321 s->count_load_time = s->u64ReloadTS = TMTimerGet(pTimer);
322 s->count = val;
323 pit_irq_timer_update(s, s->count_load_time, s->count_load_time);
324
325 /* log the new rate (ch 0 only). */
326 if ( s->pTimerR3 /* ch 0 */
327 && s->cRelLogEntries++ < 32)
328 LogRel(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=0)\n",
329 s->mode, s->count, s->count, PIT_FREQ / s->count, (PIT_FREQ * 100 / s->count) % 100));
330 else
331 Log(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=0)\n",
332 s->mode, s->count, s->count, PIT_FREQ / s->count, (PIT_FREQ * 100 / s->count) % 100));
333}
334
335/* return -1 if no transition will occur. */
336static int64_t pit_get_next_transition_time(PITChannelState *s,
337 uint64_t current_time)
338{
339 PTMTIMER pTimer = s->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
340 uint64_t d, next_time, base;
341 uint32_t period2;
342
343 d = ASMMultU64ByU32DivByU32(current_time - s->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
344 switch(s->mode) {
345 default:
346 case 0:
347 case 1:
348 if (d < s->count)
349 next_time = s->count;
350 else
351 return -1;
352 break;
353 /*
354 * Mode 2: The period is count + 1 PIT ticks.
355 * When the counter reaches 1 we sent the output low (for channel 0 that
356 * means raise an irq). On the next tick, where we should be decrementing
357 * from 1 to 0, the count is loaded and the output goes high (channel 0
358 * means clearing the irq).
359 *
360 * In VBox we simplify the tick cycle between 1 and 0 and immediately clears
361 * the irq. We also don't set it until we reach 0, which is a tick late - will
362 * try fix that later some day.
363 */
364 case 2:
365 base = (d / s->count) * s->count;
366#ifndef VBOX /* see above */
367 if ((d - base) == 0 && d != 0)
368 next_time = base + s->count;
369 else
370#endif
371 next_time = base + s->count + 1;
372 break;
373 case 3:
374 base = (d / s->count) * s->count;
375 period2 = ((s->count + 1) >> 1);
376 if ((d - base) < period2)
377 next_time = base + period2;
378 else
379 next_time = base + s->count;
380 break;
381 case 4:
382 case 5:
383 if (d < s->count)
384 next_time = s->count;
385 else if (d == s->count)
386 next_time = s->count + 1;
387 else
388 return -1;
389 break;
390 }
391 /* convert to timer units */
392 LogFlow(("PIT: next_time=%'14RU64 %'20RU64 mode=%#x count=%#06x\n", next_time,
393 ASMMultU64ByU32DivByU32(next_time, TMTimerGetFreq(pTimer), PIT_FREQ), s->mode, s->count));
394 next_time = s->count_load_time + ASMMultU64ByU32DivByU32(next_time, TMTimerGetFreq(pTimer), PIT_FREQ);
395 /* fix potential rounding problems */
396 /* XXX: better solution: use a clock at PIT_FREQ Hz */
397 if (next_time <= current_time)
398 next_time = current_time + 1;
399 return next_time;
400}
401
402static void pit_irq_timer_update(PITChannelState *s, uint64_t current_time, uint64_t now)
403{
404 int64_t expire_time;
405 int irq_level;
406 PPDMDEVINS pDevIns;
407 PTMTIMER pTimer = s->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
408
409 if (!s->CTX_SUFF(pTimer))
410 return;
411 expire_time = pit_get_next_transition_time(s, current_time);
412 irq_level = pit_get_out1(s, current_time);
413
414 /* We just flip-flop the irq level to save that extra timer call, which isn't generally required (we haven't served it for months). */
415 pDevIns = s->CTX_SUFF(pPit)->pDevIns;
416 PDMDevHlpISASetIrq(pDevIns, s->irq, irq_level);
417 if (irq_level)
418 PDMDevHlpISASetIrq(pDevIns, s->irq, 0);
419 if (irq_level)
420 {
421 s->u64ReloadTS = now;
422 STAM_COUNTER_INC(&s->CTX_SUFF(pPit)->StatPITIrq);
423 }
424
425 if (expire_time != -1)
426 {
427 Log3(("pit_irq_timer_update: next=%'RU64 now=%'RU64\n", expire_time, now));
428 s->u64NextTS = expire_time;
429 TMTimerSet(s->CTX_SUFF(pTimer), s->u64NextTS);
430 }
431 else
432 {
433 LogFlow(("PIT: m=%d count=%#4x irq_level=%#x stopped\n", s->mode, s->count, irq_level));
434 TMTimerStop(s->CTX_SUFF(pTimer));
435 s->u64NextTS = UINT64_MAX;
436 }
437 s->next_transition_time = expire_time;
438}
439
440#endif /* IN_RING3 */
441
442
443/**
444 * Port I/O Handler for IN operations.
445 *
446 * @returns VBox status code.
447 *
448 * @param pDevIns The device instance.
449 * @param pvUser User argument - ignored.
450 * @param Port Port number used for the IN operation.
451 * @param pu32 Where to store the result.
452 * @param cb Number of bytes read.
453 */
454PDMBOTHCBDECL(int) pitIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
455{
456 Log2(("pitIOPortRead: Port=%#x cb=%x\n", Port, cb));
457 NOREF(pvUser);
458 Port &= 3;
459 if (cb != 1 || Port == 3)
460 {
461 Log(("pitIOPortRead: Port=%#x cb=%x *pu32=unused!\n", Port, cb));
462 return VERR_IOM_IOPORT_UNUSED;
463 }
464
465 PITState *pit = PDMINS_2_DATA(pDevIns, PITState *);
466 int ret;
467 PITChannelState *s = &pit->channels[Port];
468 if (s->status_latched)
469 {
470 s->status_latched = 0;
471 ret = s->status;
472 }
473 else if (s->count_latched)
474 {
475 switch (s->count_latched)
476 {
477 default:
478 case RW_STATE_LSB:
479 ret = s->latched_count & 0xff;
480 s->count_latched = 0;
481 break;
482 case RW_STATE_MSB:
483 ret = s->latched_count >> 8;
484 s->count_latched = 0;
485 break;
486 case RW_STATE_WORD0:
487 ret = s->latched_count & 0xff;
488 s->count_latched = RW_STATE_MSB;
489 break;
490 }
491 }
492 else
493 {
494 int count;
495 switch (s->read_state)
496 {
497 default:
498 case RW_STATE_LSB:
499 count = pit_get_count(s);
500 ret = count & 0xff;
501 break;
502 case RW_STATE_MSB:
503 count = pit_get_count(s);
504 ret = (count >> 8) & 0xff;
505 break;
506 case RW_STATE_WORD0:
507 count = pit_get_count(s);
508 ret = count & 0xff;
509 s->read_state = RW_STATE_WORD1;
510 break;
511 case RW_STATE_WORD1:
512 count = pit_get_count(s);
513 ret = (count >> 8) & 0xff;
514 s->read_state = RW_STATE_WORD0;
515 break;
516 }
517 }
518
519 *pu32 = ret;
520 Log2(("pitIOPortRead: Port=%#x cb=%x *pu32=%#04x\n", Port, cb, *pu32));
521 return VINF_SUCCESS;
522}
523
524
525/**
526 * Port I/O Handler for OUT operations.
527 *
528 * @returns VBox status code.
529 *
530 * @param pDevIns The device instance.
531 * @param pvUser User argument - ignored.
532 * @param Port Port number used for the IN operation.
533 * @param u32 The value to output.
534 * @param cb The value size in bytes.
535 */
536PDMBOTHCBDECL(int) pitIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
537{
538 Log2(("pitIOPortWrite: Port=%#x cb=%x u32=%#04x\n", Port, cb, u32));
539 NOREF(pvUser);
540 if (cb != 1)
541 return VINF_SUCCESS;
542
543 PITState *pit = PDMINS_2_DATA(pDevIns, PITState *);
544 Port &= 3;
545 if (Port == 3)
546 {
547 /*
548 * Port 43h - Mode/Command Register.
549 * 7 6 5 4 3 2 1 0
550 * * * . . . . . . Select channel: 0 0 = Channel 0
551 * 0 1 = Channel 1
552 * 1 0 = Channel 2
553 * 1 1 = Read-back command (8254 only)
554 * (Illegal on 8253)
555 * (Illegal on PS/2 {JAM})
556 * . . * * . . . . Command/Access mode: 0 0 = Latch count value command
557 * 0 1 = Access mode: lobyte only
558 * 1 0 = Access mode: hibyte only
559 * 1 1 = Access mode: lobyte/hibyte
560 * . . . . * * * . Operating mode: 0 0 0 = Mode 0, 0 0 1 = Mode 1,
561 * 0 1 0 = Mode 2, 0 1 1 = Mode 3,
562 * 1 0 0 = Mode 4, 1 0 1 = Mode 5,
563 * 1 1 0 = Mode 2, 1 1 1 = Mode 3
564 * . . . . . . . * BCD/Binary mode: 0 = 16-bit binary, 1 = four-digit BCD
565 */
566 unsigned channel = u32 >> 6;
567 if (channel == 3)
568 {
569 /* read-back command */
570 for (channel = 0; channel < RT_ELEMENTS(pit->channels); channel++)
571 {
572 PITChannelState *s = &pit->channels[channel];
573 if (u32 & (2 << channel)) {
574 if (!(u32 & 0x20))
575 pit_latch_count(s);
576 if (!(u32 & 0x10) && !s->status_latched)
577 {
578 /* status latch */
579 /* XXX: add BCD and null count */
580 PTMTIMER pTimer = s->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
581 s->status = (pit_get_out1(s, TMTimerGet(pTimer)) << 7)
582 | (s->rw_mode << 4)
583 | (s->mode << 1)
584 | s->bcd;
585 s->status_latched = 1;
586 }
587 }
588 }
589 }
590 else
591 {
592 PITChannelState *s = &pit->channels[channel];
593 unsigned access = (u32 >> 4) & 3;
594 if (access == 0)
595 pit_latch_count(s);
596 else
597 {
598 s->rw_mode = access;
599 s->read_state = access;
600 s->write_state = access;
601
602 s->mode = (u32 >> 1) & 7;
603 s->bcd = u32 & 1;
604 /* XXX: update irq timer ? */
605 }
606 }
607 }
608 else
609 {
610#ifndef IN_RING3
611 return VINF_IOM_HC_IOPORT_WRITE;
612#else /* IN_RING3 */
613 /*
614 * Port 40-42h - Channel Data Ports.
615 */
616 PITChannelState *s = &pit->channels[Port];
617 switch(s->write_state)
618 {
619 default:
620 case RW_STATE_LSB:
621 pit_load_count(s, u32);
622 break;
623 case RW_STATE_MSB:
624 pit_load_count(s, u32 << 8);
625 break;
626 case RW_STATE_WORD0:
627 s->write_latch = u32;
628 s->write_state = RW_STATE_WORD1;
629 break;
630 case RW_STATE_WORD1:
631 pit_load_count(s, s->write_latch | (u32 << 8));
632 s->write_state = RW_STATE_WORD0;
633 break;
634 }
635#endif /* !IN_RING3 */
636 }
637 return VINF_SUCCESS;
638}
639
640
641/**
642 * Port I/O Handler for speaker IN operations.
643 *
644 * @returns VBox status code.
645 *
646 * @param pDevIns The device instance.
647 * @param pvUser User argument - ignored.
648 * @param Port Port number used for the IN operation.
649 * @param pu32 Where to store the result.
650 * @param cb Number of bytes read.
651 */
652PDMBOTHCBDECL(int) pitIOPortSpeakerRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
653{
654 NOREF(pvUser);
655 if (cb == 1)
656 {
657 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
658 const uint64_t u64Now = TMTimerGet(pThis->channels[0].CTX_SUFF(pTimer));
659 Assert(TMTimerGetFreq(pThis->channels[0].CTX_SUFF(pTimer)) == 1000000000); /* lazy bird. */
660
661 /* bit 6,7 Parity error stuff. */
662 /* bit 5 - mirrors timer 2 output condition. */
663 const int fOut = pit_get_out(pThis, 2, u64Now);
664 /* bit 4 - toggled with each (DRAM?) refresh request, every 15.085 µs.
665 ASSUMES ns timer freq, see assertion above. */
666#ifndef FAKE_REFRESH_CLOCK
667 const int fRefresh = (u64Now / 15085) & 1;
668#else
669 pThis->dummy_refresh_clock ^= 1;
670 const int fRefresh = pThis->dummy_refresh_clock;
671#endif
672 /* bit 2,3 NMI / parity status stuff. */
673 /* bit 1 - speaker data status */
674 const int fSpeakerStatus = pThis->speaker_data_on;
675 /* bit 0 - timer 2 clock gate to speaker status. */
676 const int fTimer2GateStatus = pit_get_gate(pThis, 2);
677
678 *pu32 = fTimer2GateStatus
679 | (fSpeakerStatus << 1)
680 | (fRefresh << 4)
681 | (fOut << 5);
682 Log(("pitIOPortSpeakerRead: Port=%#x cb=%x *pu32=%#x\n", Port, cb, *pu32));
683 return VINF_SUCCESS;
684 }
685 Log(("pitIOPortSpeakerRead: Port=%#x cb=%x *pu32=unused!\n", Port, cb));
686 return VERR_IOM_IOPORT_UNUSED;
687}
688
689#ifdef IN_RING3
690
691/**
692 * Port I/O Handler for speaker OUT operations.
693 *
694 * @returns VBox status code.
695 *
696 * @param pDevIns The device instance.
697 * @param pvUser User argument - ignored.
698 * @param Port Port number used for the IN operation.
699 * @param u32 The value to output.
700 * @param cb The value size in bytes.
701 */
702PDMBOTHCBDECL(int) pitIOPortSpeakerWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
703{
704 NOREF(pvUser);
705 if (cb == 1)
706 {
707 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
708 pThis->speaker_data_on = (u32 >> 1) & 1;
709 pit_set_gate(pThis, 2, u32 & 1);
710 }
711 Log(("pitIOPortSpeakerWrite: Port=%#x cb=%x u32=%#x\n", Port, cb, u32));
712 return VINF_SUCCESS;
713}
714
715
716/**
717 * @copydoc FNSSMDEVLIVEEXEC
718 */
719static DECLCALLBACK(int) pitLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
720{
721 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
722 SSMR3PutIOPort(pSSM, pThis->IOPortBaseCfg);
723 SSMR3PutU8( pSSM, pThis->channels[0].irq);
724 SSMR3PutBool( pSSM, pThis->fSpeakerCfg);
725 return VINF_SSM_DONT_CALL_AGAIN;
726}
727
728
729/**
730 * @copydoc FNSSMDEVSAVEEXEC
731 */
732static DECLCALLBACK(int) pitSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
733{
734 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
735 unsigned i;
736
737 /* The config. */
738 pitLiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
739
740 /* The state. */
741 for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
742 {
743 PITChannelState *s = &pThis->channels[i];
744 SSMR3PutU32(pSSM, s->count);
745 SSMR3PutU16(pSSM, s->latched_count);
746 SSMR3PutU8(pSSM, s->count_latched);
747 SSMR3PutU8(pSSM, s->status_latched);
748 SSMR3PutU8(pSSM, s->status);
749 SSMR3PutU8(pSSM, s->read_state);
750 SSMR3PutU8(pSSM, s->write_state);
751 SSMR3PutU8(pSSM, s->write_latch);
752 SSMR3PutU8(pSSM, s->rw_mode);
753 SSMR3PutU8(pSSM, s->mode);
754 SSMR3PutU8(pSSM, s->bcd);
755 SSMR3PutU8(pSSM, s->gate);
756 SSMR3PutU64(pSSM, s->count_load_time);
757 SSMR3PutU64(pSSM, s->u64NextTS);
758 SSMR3PutU64(pSSM, s->u64ReloadTS);
759 SSMR3PutS64(pSSM, s->next_transition_time);
760 if (s->CTX_SUFF(pTimer))
761 TMR3TimerSave(s->CTX_SUFF(pTimer), pSSM);
762 }
763
764 SSMR3PutS32(pSSM, pThis->speaker_data_on);
765#ifdef FAKE_REFRESH_CLOCK
766 return SSMR3PutS32(pSSM, pThis->dummy_refresh_clock);
767#else
768 return SSMR3PutS32(pSSM, 0);
769#endif
770}
771
772
773/**
774 * @copydoc FNSSMDEVLOADEXEC
775 */
776static DECLCALLBACK(int) pitLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
777{
778 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
779 int rc;
780
781 if ( uVersion != PIT_SAVED_STATE_VERSION
782 && uVersion != PIT_SAVED_STATE_VERSION_VBOX_30)
783 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
784
785 /* The config. */
786 if (uVersion > PIT_SAVED_STATE_VERSION_VBOX_30)
787 {
788 RTIOPORT IOPortBaseCfg;
789 rc = SSMR3GetIOPort(pSSM, &IOPortBaseCfg); AssertRCReturn(rc, rc);
790 if (IOPortBaseCfg != pThis->IOPortBaseCfg)
791 {
792 LogRel(("PIT: Config mismatch - IOPortBaseCfg: saved=%RTiop config=%RTiop\n", IOPortBaseCfg, pThis->IOPortBaseCfg));
793 return VERR_SSM_LOAD_CONFIG_MISMATCH;
794 }
795
796 uint8_t u8Irq;
797 rc = SSMR3GetU8(pSSM, &u8Irq); AssertRCReturn(rc, rc);
798 if (u8Irq != pThis->channels[0].irq)
799 {
800 LogRel(("PIT: Config mismatch - u8Irq: saved=%#x config=%#x\n", u8Irq, pThis->channels[0].irq));
801 return VERR_SSM_LOAD_CONFIG_MISMATCH;
802 }
803
804 bool fSpeakerCfg;
805 rc = SSMR3GetBool(pSSM, &fSpeakerCfg); AssertRCReturn(rc, rc);
806 if (fSpeakerCfg != pThis->fSpeakerCfg)
807 {
808 LogRel(("PIT: Config mismatch - fSpeakerCfg: saved=%RTbool config=%RTbool\n", fSpeakerCfg, pThis->fSpeakerCfg));
809 return VERR_SSM_LOAD_CONFIG_MISMATCH;
810 }
811 }
812
813 if (uPass != SSM_PASS_FINAL)
814 return VINF_SUCCESS;
815
816 /* The state. */
817 for (unsigned i = 0; i < RT_ELEMENTS(pThis->channels); i++)
818 {
819 PITChannelState *s = &pThis->channels[i];
820 SSMR3GetU32(pSSM, &s->count);
821 SSMR3GetU16(pSSM, &s->latched_count);
822 SSMR3GetU8(pSSM, &s->count_latched);
823 SSMR3GetU8(pSSM, &s->status_latched);
824 SSMR3GetU8(pSSM, &s->status);
825 SSMR3GetU8(pSSM, &s->read_state);
826 SSMR3GetU8(pSSM, &s->write_state);
827 SSMR3GetU8(pSSM, &s->write_latch);
828 SSMR3GetU8(pSSM, &s->rw_mode);
829 SSMR3GetU8(pSSM, &s->mode);
830 SSMR3GetU8(pSSM, &s->bcd);
831 SSMR3GetU8(pSSM, &s->gate);
832 SSMR3GetU64(pSSM, &s->count_load_time);
833 SSMR3GetU64(pSSM, &s->u64NextTS);
834 SSMR3GetU64(pSSM, &s->u64ReloadTS);
835 SSMR3GetS64(pSSM, &s->next_transition_time);
836 if (s->CTX_SUFF(pTimer))
837 {
838 TMR3TimerLoad(s->CTX_SUFF(pTimer), pSSM);
839 LogRel(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=%d) (restore)\n",
840 s->mode, s->count, s->count, PIT_FREQ / s->count, (PIT_FREQ * 100 / s->count) % 100, i));
841 }
842 pThis->channels[0].cRelLogEntries = 0;
843 }
844
845 SSMR3GetS32(pSSM, &pThis->speaker_data_on);
846#ifdef FAKE_REFRESH_CLOCK
847 return SSMR3GetS32(pSSM, &pThis->dummy_refresh_clock);
848#else
849 int32_t u32Dummy;
850 return SSMR3GetS32(pSSM, &u32Dummy);
851#endif
852}
853
854
855/**
856 * Device timer callback function.
857 *
858 * @param pDevIns Device instance of the device which registered the timer.
859 * @param pTimer The timer handle.
860 * @param pvUser Pointer to the PIT channel state.
861 */
862static DECLCALLBACK(void) pitTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
863{
864 PITChannelState *s = (PITChannelState *)pvUser;
865 STAM_PROFILE_ADV_START(&s->CTX_SUFF(pPit)->StatPITHandler, a);
866 Log(("pitTimer\n"));
867 pit_irq_timer_update(s, s->next_transition_time, TMTimerGet(pTimer));
868 STAM_PROFILE_ADV_STOP(&s->CTX_SUFF(pPit)->StatPITHandler, a);
869}
870
871
872/**
873 * Relocation notification.
874 *
875 * @returns VBox status.
876 * @param pDevIns The device instance data.
877 * @param offDelta The delta relative to the old address.
878 */
879static DECLCALLBACK(void) pitRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
880{
881 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
882 unsigned i;
883 LogFlow(("pitRelocate: \n"));
884
885 for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
886 {
887 PITChannelState *pCh = &pThis->channels[i];
888 if (pCh->pTimerR3)
889 pCh->pTimerRC = TMTimerRCPtr(pCh->pTimerR3);
890 pThis->channels[i].pPitRC = PDMINS_2_DATA_RCPTR(pDevIns);
891 }
892}
893
894/** @todo remove this! */
895static DECLCALLBACK(void) pitInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs);
896
897/**
898 * Reset notification.
899 *
900 * @returns VBox status.
901 * @param pDevIns The device instance data.
902 */
903static DECLCALLBACK(void) pitReset(PPDMDEVINS pDevIns)
904{
905 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
906 unsigned i;
907 LogFlow(("pitReset: \n"));
908
909 for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
910 {
911 PITChannelState *s = &pThis->channels[i];
912
913#if 1 /* Set everything back to virgin state. (might not be strictly correct) */
914 s->latched_count = 0;
915 s->count_latched = 0;
916 s->status_latched = 0;
917 s->status = 0;
918 s->read_state = 0;
919 s->write_state = 0;
920 s->write_latch = 0;
921 s->rw_mode = 0;
922 s->bcd = 0;
923#endif
924 s->u64NextTS = UINT64_MAX;
925 s->cRelLogEntries = 0;
926 s->mode = 3;
927 s->gate = (i != 2);
928 pit_load_count(s, 0);
929 }
930}
931
932
933/**
934 * Info handler, device version.
935 *
936 * @param pDevIns Device instance which registered the info.
937 * @param pHlp Callback functions for doing output.
938 * @param pszArgs Argument string. Optional and specific to the handler.
939 */
940static DECLCALLBACK(void) pitInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
941{
942 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
943 unsigned i;
944 for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
945 {
946 const PITChannelState *pCh = &pThis->channels[i];
947
948 pHlp->pfnPrintf(pHlp,
949 "PIT (i8254) channel %d status: irq=%#x\n"
950 " count=%08x" " latched_count=%04x count_latched=%02x\n"
951 " status=%02x status_latched=%02x read_state=%02x\n"
952 " write_state=%02x write_latch=%02x rw_mode=%02x\n"
953 " mode=%02x bcd=%02x gate=%02x\n"
954 " count_load_time=%016RX64 next_transition_time=%016RX64\n"
955 " u64ReloadTS=%016RX64 u64NextTS=%016RX64\n"
956 ,
957 i, pCh->irq,
958 pCh->count, pCh->latched_count, pCh->count_latched,
959 pCh->status, pCh->status_latched, pCh->read_state,
960 pCh->write_state, pCh->write_latch, pCh->rw_mode,
961 pCh->mode, pCh->bcd, pCh->gate,
962 pCh->count_load_time, pCh->next_transition_time,
963 pCh->u64ReloadTS, pCh->u64NextTS);
964 }
965#ifdef FAKE_REFRESH_CLOCK
966 pHlp->pfnPrintf(pHlp, "speaker_data_on=%#x dummy_refresh_clock=%#x\n",
967 pThis->speaker_data_on, pThis->dummy_refresh_clock);
968#else
969 pHlp->pfnPrintf(pHlp, "speaker_data_on=%#x\n", pThis->speaker_data_on);
970#endif
971}
972
973
974/**
975 * Construct a device instance for a VM.
976 *
977 * @returns VBox status.
978 * @param pDevIns The device instance data.
979 * If the registration structure is needed, pDevIns->pDevReg points to it.
980 * @param iInstance Instance number. Use this to figure out which registers and such to use.
981 * The device number is also found in pDevIns->iInstance, but since it's
982 * likely to be freqently used PDM passes it as parameter.
983 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
984 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
985 * iInstance it's expected to be used a bit in this function.
986 */
987static DECLCALLBACK(int) pitConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
988{
989 PITState *pThis = PDMINS_2_DATA(pDevIns, PITState *);
990 int rc;
991 uint8_t u8Irq;
992 uint16_t u16Base;
993 bool fSpeaker;
994 bool fGCEnabled;
995 bool fR0Enabled;
996 unsigned i;
997 Assert(iInstance == 0);
998
999 /*
1000 * Validate configuration.
1001 */
1002 if (!CFGMR3AreValuesValid(pCfgHandle, "Irq\0" "Base\0" "SpeakerEnabled\0" "GCEnabled\0" "R0Enabled\0"))
1003 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
1004
1005 /*
1006 * Init the data.
1007 */
1008 rc = CFGMR3QueryU8Def(pCfgHandle, "Irq", &u8Irq, 0);
1009 if (RT_FAILURE(rc))
1010 return PDMDEV_SET_ERROR(pDevIns, rc,
1011 N_("Configuration error: Querying \"Irq\" as a uint8_t failed"));
1012
1013 rc = CFGMR3QueryU16Def(pCfgHandle, "Base", &u16Base, 0x40);
1014 if (RT_FAILURE(rc))
1015 return PDMDEV_SET_ERROR(pDevIns, rc,
1016 N_("Configuration error: Querying \"Base\" as a uint16_t failed"));
1017
1018 rc = CFGMR3QueryBoolDef(pCfgHandle, "SpeakerEnabled", &fSpeaker, true);
1019 if (RT_FAILURE(rc))
1020 return PDMDEV_SET_ERROR(pDevIns, rc,
1021 N_("Configuration error: Querying \"SpeakerEnabled\" as a bool failed"));
1022
1023 rc = CFGMR3QueryBoolDef(pCfgHandle, "GCEnabled", &fGCEnabled, true);
1024 if (RT_FAILURE(rc))
1025 return PDMDEV_SET_ERROR(pDevIns, rc,
1026 N_("Configuration error: Querying \"GCEnabled\" as a bool failed"));
1027
1028 rc = CFGMR3QueryBoolDef(pCfgHandle, "R0Enabled", &fR0Enabled, true);
1029 if (RT_FAILURE(rc))
1030 return PDMDEV_SET_ERROR(pDevIns, rc,
1031 N_("Configuration error: failed to read R0Enabled as boolean"));
1032
1033 pThis->pDevIns = pDevIns;
1034 pThis->IOPortBaseCfg = u16Base;
1035 pThis->fSpeakerCfg = fSpeaker;
1036 pThis->channels[0].irq = u8Irq;
1037 for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
1038 {
1039 pThis->channels[i].pPitR3 = pThis;
1040 pThis->channels[i].pPitR0 = PDMINS_2_DATA_R0PTR(pDevIns);
1041 pThis->channels[i].pPitRC = PDMINS_2_DATA_RCPTR(pDevIns);
1042 }
1043
1044 /*
1045 * Create timer, register I/O Ports and save state.
1046 */
1047 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, pitTimer, &pThis->channels[0],
1048 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "i8254 Programmable Interval Timer",
1049 &pThis->channels[0].pTimerR3);
1050 if (RT_FAILURE(rc))
1051 return rc;
1052 pThis->channels[0].pTimerRC = TMTimerRCPtr(pThis->channels[0].pTimerR3);
1053 pThis->channels[0].pTimerR0 = TMTimerR0Ptr(pThis->channels[0].pTimerR3);
1054
1055 rc = PDMDevHlpIOPortRegister(pDevIns, u16Base, 4, NULL, pitIOPortWrite, pitIOPortRead, NULL, NULL, "i8254 Programmable Interval Timer");
1056 if (RT_FAILURE(rc))
1057 return rc;
1058 if (fGCEnabled)
1059 {
1060 rc = PDMDevHlpIOPortRegisterGC(pDevIns, u16Base, 4, 0, "pitIOPortWrite", "pitIOPortRead", NULL, NULL, "i8254 Programmable Interval Timer");
1061 if (RT_FAILURE(rc))
1062 return rc;
1063 }
1064 if (fR0Enabled)
1065 {
1066 rc = PDMDevHlpIOPortRegisterR0(pDevIns, u16Base, 4, 0, "pitIOPortWrite", "pitIOPortRead", NULL, NULL, "i8254 Programmable Interval Timer");
1067 if (RT_FAILURE(rc))
1068 return rc;
1069 }
1070
1071 if (fSpeaker)
1072 {
1073 rc = PDMDevHlpIOPortRegister(pDevIns, 0x61, 1, NULL, pitIOPortSpeakerWrite, pitIOPortSpeakerRead, NULL, NULL, "PC Speaker");
1074 if (RT_FAILURE(rc))
1075 return rc;
1076 if (fGCEnabled)
1077 {
1078 rc = PDMDevHlpIOPortRegisterGC(pDevIns, 0x61, 1, 0, NULL, "pitIOPortSpeakerRead", NULL, NULL, "PC Speaker");
1079 if (RT_FAILURE(rc))
1080 return rc;
1081 }
1082 }
1083
1084 rc = PDMDevHlpSSMRegister3(pDevIns, PIT_SAVED_STATE_VERSION, sizeof(*pThis), pitLiveExec, pitSaveExec, pitLoadExec);
1085 if (RT_FAILURE(rc))
1086 return rc;
1087
1088 /*
1089 * Initialize the device state.
1090 */
1091 pitReset(pDevIns);
1092
1093 /*
1094 * Register statistics and debug info.
1095 */
1096 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPITIrq, STAMTYPE_COUNTER, "/TM/PIT/Irq", STAMUNIT_OCCURENCES, "The number of times a timer interrupt was triggered.");
1097 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPITHandler, STAMTYPE_PROFILE, "/TM/PIT/Handler", STAMUNIT_TICKS_PER_CALL, "Profiling timer callback handler.");
1098
1099 PDMDevHlpDBGFInfoRegister(pDevIns, "pit", "Display PIT (i8254) status. (no arguments)", pitInfo);
1100
1101 return VINF_SUCCESS;
1102}
1103
1104
1105/**
1106 * The device registration structure.
1107 */
1108const PDMDEVREG g_DeviceI8254 =
1109{
1110 /* u32Version */
1111 PDM_DEVREG_VERSION,
1112 /* szDeviceName */
1113 "i8254",
1114 /* szRCMod */
1115 "VBoxDDGC.gc",
1116 /* szR0Mod */
1117 "VBoxDDR0.r0",
1118 /* pszDescription */
1119 "Intel 8254 Programmable Interval Timer (PIT) And Dummy Speaker Device",
1120 /* fFlags */
1121 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
1122 /* fClass */
1123 PDM_DEVREG_CLASS_PIT,
1124 /* cMaxInstances */
1125 1,
1126 /* cbInstance */
1127 sizeof(PITState),
1128 /* pfnConstruct */
1129 pitConstruct,
1130 /* pfnDestruct */
1131 NULL,
1132 /* pfnRelocate */
1133 pitRelocate,
1134 /* pfnIOCtl */
1135 NULL,
1136 /* pfnPowerOn */
1137 NULL,
1138 /* pfnReset */
1139 pitReset,
1140 /* pfnSuspend */
1141 NULL,
1142 /* pfnResume */
1143 NULL,
1144 /* pfnAttach */
1145 NULL,
1146 /* pfnDetach */
1147 NULL,
1148 /* pfnQueryInterface. */
1149 NULL,
1150 /* pfnInitComplete */
1151 NULL,
1152 /* pfnPowerOff */
1153 NULL,
1154 /* pfnSoftReset */
1155 NULL,
1156 /* u32VersionEnd */
1157 PDM_DEVREG_VERSION
1158};
1159
1160#endif /* IN_RING3 */
1161#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette