VirtualBox

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

Last change on this file since 81899 was 81898, checked in by vboxsync, 5 years ago

DevPit-i8254: Do CFGM, SSM and critsects thru the device helpers. bugref:9218

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 56.8 KB
Line 
1/* $Id: DevPit-i8254.cpp 81898 2019-11-16 16:59:23Z vboxsync $ */
2/** @file
3 * DevPIT-i8254 - Intel 8254 Programmable Interval Timer (PIT) And Dummy Speaker Device.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
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 *
18 * This code is based on:
19 *
20 * QEMU 8253/8254 interval timer emulation
21 *
22 * Copyright (c) 2003-2004 Fabrice Bellard
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
37 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40 * THE SOFTWARE.
41 */
42
43
44/*********************************************************************************************************************************
45* Header Files *
46*********************************************************************************************************************************/
47#define LOG_GROUP LOG_GROUP_DEV_PIT
48#include <VBox/vmm/pdmdev.h>
49#include <VBox/log.h>
50#include <VBox/vmm/stam.h>
51#include <iprt/assert.h>
52#include <iprt/asm-math.h>
53
54#ifdef IN_RING3
55# ifdef RT_OS_LINUX
56# include <fcntl.h>
57# include <errno.h>
58# include <unistd.h>
59# include <stdio.h>
60# include <linux/kd.h>
61# include <linux/input.h>
62# include <sys/ioctl.h>
63# endif
64# include <iprt/alloc.h>
65# include <iprt/string.h>
66# include <iprt/uuid.h>
67#endif /* IN_RING3 */
68
69#include "VBoxDD.h"
70
71
72/*********************************************************************************************************************************
73* Defined Constants And Macros *
74*********************************************************************************************************************************/
75/** The PIT frequency. */
76#define PIT_FREQ 1193182
77
78#define RW_STATE_LSB 1
79#define RW_STATE_MSB 2
80#define RW_STATE_WORD0 3
81#define RW_STATE_WORD1 4
82
83/** The current saved state version. */
84#define PIT_SAVED_STATE_VERSION 4
85/** The saved state version used by VirtualBox 3.1 and earlier.
86 * This did not include disable by HPET flag. */
87#define PIT_SAVED_STATE_VERSION_VBOX_31 3
88/** The saved state version used by VirtualBox 3.0 and earlier.
89 * This did not include the config part. */
90#define PIT_SAVED_STATE_VERSION_VBOX_30 2
91
92/** @def FAKE_REFRESH_CLOCK
93 * Define this to flip the 15usec refresh bit on every read.
94 * If not defined, it will be flipped correctly. */
95/* #define FAKE_REFRESH_CLOCK */
96#ifdef DOXYGEN_RUNNING
97# define FAKE_REFRESH_CLOCK
98#endif
99
100/** The effective counter mode - if bit 1 is set, bit 2 is ignored. */
101#define EFFECTIVE_MODE(x) ((x) & ~(((x) & 2) << 1))
102
103
104/**
105 * Acquires the PIT lock or returns.
106 */
107#define DEVPIT_LOCK_RETURN(a_pDevIns, a_pThis, a_rcBusy) \
108 do { \
109 int rcLock = PDMDevHlpCritSectEnter((a_pDevIns), &(a_pThis)->CritSect, (a_rcBusy)); \
110 if (rcLock != VINF_SUCCESS) \
111 return rcLock; \
112 } while (0)
113
114/**
115 * Releases the PIT lock.
116 */
117#define DEVPIT_UNLOCK(a_pDevIns, a_pThis) \
118 do { PDMDevHlpCritSectLeave((a_pDevIns), &(a_pThis)->CritSect); } while (0)
119
120
121/**
122 * Acquires the TM lock and PIT lock, returns on failure.
123 */
124#define DEVPIT_LOCK_BOTH_RETURN(a_pDevIns, a_pThis, a_rcBusy) \
125 do { \
126 int rcLock = TMTimerLock((a_pThis)->channels[0].CTX_SUFF(pTimer), (a_rcBusy)); \
127 if (rcLock != VINF_SUCCESS) \
128 return rcLock; \
129 rcLock = PDMDevHlpCritSectEnter((a_pDevIns), &(a_pThis)->CritSect, (a_rcBusy)); \
130 if (rcLock != VINF_SUCCESS) \
131 { \
132 TMTimerUnlock((a_pThis)->channels[0].CTX_SUFF(pTimer)); \
133 return rcLock; \
134 } \
135 } while (0)
136
137#ifdef IN_RING3
138/**
139 * Acquires the TM lock and PIT lock, ignores failures.
140 */
141# define DEVPIT_R3_LOCK_BOTH(a_pDevIns, a_pThis) \
142 do { \
143 TMTimerLock((a_pThis)->channels[0].CTX_SUFF(pTimer), VERR_IGNORED); \
144 PDMDevHlpCritSectEnter((a_pDevIns), &(a_pThis)->CritSect, VERR_IGNORED); \
145 } while (0)
146#endif /* IN_RING3 */
147
148/**
149 * Releases the PIT lock and TM lock.
150 */
151#define DEVPIT_UNLOCK_BOTH(a_pDevIns, a_pThis) \
152 do { \
153 PDMDevHlpCritSectLeave((a_pDevIns), &(a_pThis)->CritSect); \
154 TMTimerUnlock((a_pThis)->channels[0].CTX_SUFF(pTimer)); \
155 } while (0)
156
157
158
159/*********************************************************************************************************************************
160* Structures and Typedefs *
161*********************************************************************************************************************************/
162/**
163 * The state of one PIT channel.
164 */
165typedef struct PITCHANNEL
166{
167 /** Pointer to the instance data - R3 Ptr. */
168 R3PTRTYPE(struct PITSTATE *) pPitR3;
169 /** The timer - R3 Ptr.
170 * @note Only channel 0 has a timer. */
171 PTMTIMERR3 pTimerR3;
172 /** Pointer to the instance data - R0 Ptr. */
173 R0PTRTYPE(struct PITSTATE *) pPitR0;
174 /** The timer - R0 Ptr.
175 * @note Only channel 0 has a timer. */
176 PTMTIMERR0 pTimerR0;
177 /** Pointer to the instance data - RC Ptr. */
178 RCPTRTYPE(struct PITSTATE *) pPitRC;
179 /** The timer - RC Ptr.
180 * @note Only channel 0 has a timer. */
181 PTMTIMERRC pTimerRC;
182 /** The virtual time stamp at the last reload. (only used in mode 2 for now) */
183 uint64_t u64ReloadTS;
184 /** The actual time of the next tick.
185 * As apposed to the next_transition_time which contains the correct time of the next tick. */
186 uint64_t u64NextTS;
187
188 /** (count_load_time is only set by TMTimerGet() which returns uint64_t) */
189 uint64_t count_load_time;
190 /* irq handling */
191 int64_t next_transition_time;
192 int32_t irq;
193 /** Number of release log entries. Used to prevent flooding. */
194 uint32_t cRelLogEntries;
195
196 uint32_t count; /* can be 65536 */
197 uint16_t latched_count;
198 uint8_t count_latched;
199 uint8_t status_latched;
200
201 uint8_t status;
202 uint8_t read_state;
203 uint8_t write_state;
204 uint8_t write_latch;
205
206 uint8_t rw_mode;
207 uint8_t mode;
208 uint8_t bcd; /* not supported */
209 uint8_t gate; /* timer start */
210
211} PITCHANNEL;
212/** Pointer to the state of one PIT channel. */
213typedef PITCHANNEL *PPITCHANNEL;
214
215/** Speaker emulation state. */
216typedef enum PITSPEAKEREMU
217{
218 PIT_SPEAKER_EMU_NONE = 0,
219 PIT_SPEAKER_EMU_CONSOLE,
220 PIT_SPEAKER_EMU_EVDEV,
221 PIT_SPEAKER_EMU_TTY
222} PITSPEAKEREMU;
223
224/**
225 * The whole PIT state.
226 */
227typedef struct PITSTATE
228{
229 /** Channel state. Must come first? */
230 PITCHANNEL channels[3];
231 /** Speaker data. */
232 int32_t speaker_data_on;
233#ifdef FAKE_REFRESH_CLOCK
234 /** Refresh dummy. */
235 int32_t dummy_refresh_clock;
236#else
237 uint32_t Alignment1;
238#endif
239 /** Config: I/O port base. */
240 RTIOPORT IOPortBaseCfg;
241 /** Config: Speaker enabled. */
242 bool fSpeakerCfg;
243 /** Disconnect PIT from the interrupt controllers if requested by HPET. */
244 bool fDisabledByHpet;
245 /** Config: What to do with speaker activity. */
246 PITSPEAKEREMU enmSpeakerEmu;
247#ifdef RT_OS_LINUX
248 /** File handle for host speaker functionality. */
249 int hHostSpeaker;
250 int afAlignment2;
251#endif
252 /** PIT port interface. */
253 PDMIHPETLEGACYNOTIFY IHpetLegacyNotify;
254 /** Pointer to the device instance. */
255 PPDMDEVINSR3 pDevIns;
256 /** Number of IRQs that's been raised. */
257 STAMCOUNTER StatPITIrq;
258 /** Profiling the timer callback handler. */
259 STAMPROFILEADV StatPITHandler;
260 /** Critical section protecting the state. */
261 PDMCRITSECT CritSect;
262} PITSTATE;
263/** Pointer to the PIT device state. */
264typedef PITSTATE *PPITSTATE;
265
266
267#ifndef VBOX_DEVICE_STRUCT_TESTCASE
268
269
270/*********************************************************************************************************************************
271* Internal Functions *
272*********************************************************************************************************************************/
273#ifdef IN_RING3
274static void pit_irq_timer_update(PPITCHANNEL pChan, uint64_t current_time, uint64_t now, bool in_timer);
275#endif
276
277
278#ifdef IN_RING3
279# ifdef RT_OS_LINUX
280static int pitTryDeviceOpen(const char *pszPath, int flags)
281{
282 int fd = open(pszPath, flags);
283 if (fd == -1)
284 LogRel(("PIT: speaker: cannot open \"%s\", errno=%d\n", pszPath, errno));
285 else
286 LogRel(("PIT: speaker: opened \"%s\"\n", pszPath));
287 return fd;
288}
289
290static int pitTryDeviceOpenSanitizeIoctl(const char *pszPath, int flags)
291{
292 int fd = open(pszPath, flags);
293 if (fd == -1)
294 LogRel(("PIT: speaker: cannot open \"%s\", errno=%d\n", pszPath, errno));
295 else
296 {
297 int errno_eviocgsnd0 = 0;
298 int errno_kiocsound = 0;
299 if (ioctl(fd, EVIOCGSND(0)) == -1)
300 {
301 errno_eviocgsnd0 = errno;
302 if (ioctl(fd, KIOCSOUND, 1) == -1)
303 errno_kiocsound = errno;
304 else
305 ioctl(fd, KIOCSOUND, 0);
306 }
307 if (errno_eviocgsnd0 && errno_kiocsound)
308 {
309 LogRel(("PIT: speaker: cannot use \"%s\", ioctl failed errno=%d/errno=%d\n", pszPath, errno_eviocgsnd0, errno_kiocsound));
310 close(fd);
311 fd = -1;
312 }
313 else
314 LogRel(("PIT: speaker: opened \"%s\"\n", pszPath));
315 }
316 return fd;
317}
318# endif /* RT_OS_LINUX */
319#endif /* IN_RING3 */
320
321static int pit_get_count(PPITCHANNEL pChan)
322{
323 uint64_t d;
324 PTMTIMER pTimer = pChan->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
325 Assert(TMTimerIsLockOwner(pTimer));
326
327 if (EFFECTIVE_MODE(pChan->mode) == 2)
328 {
329 if (pChan->u64NextTS == UINT64_MAX)
330 {
331 d = ASMMultU64ByU32DivByU32(TMTimerGet(pTimer) - pChan->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
332 return pChan->count - (d % pChan->count); /** @todo check this value. */
333 }
334 uint64_t Interval = pChan->u64NextTS - pChan->u64ReloadTS;
335 if (!Interval)
336 return pChan->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. */
337 d = TMTimerGet(pTimer);
338 d = ASMMultU64ByU32DivByU32(d - pChan->u64ReloadTS, pChan->count, Interval);
339 if (d >= pChan->count)
340 return 1;
341 return pChan->count - d;
342 }
343
344 d = ASMMultU64ByU32DivByU32(TMTimerGet(pTimer) - pChan->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
345 int counter;
346 switch (EFFECTIVE_MODE(pChan->mode))
347 {
348 case 0:
349 case 1:
350 case 4:
351 case 5:
352 counter = (pChan->count - d) & 0xffff;
353 break;
354 case 3:
355 /* XXX: may be incorrect for odd counts */
356 counter = pChan->count - ((2 * d) % pChan->count);
357 break;
358 default:
359 counter = pChan->count - (d % pChan->count);
360 break;
361 }
362 /** @todo check that we don't return 0, in most modes (all?) the counter shouldn't be zero. */
363 return counter;
364}
365
366/* get pit output bit */
367static int pit_get_out1(PPITCHANNEL pChan, int64_t current_time)
368{
369 PTMTIMER pTimer = pChan->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
370 uint64_t d;
371 int out;
372
373 d = ASMMultU64ByU32DivByU32(current_time - pChan->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
374 switch (EFFECTIVE_MODE(pChan->mode))
375 {
376 default:
377 case 0:
378 out = (d >= pChan->count);
379 break;
380 case 1:
381 out = (d < pChan->count);
382 break;
383 case 2:
384 Log2(("pit_get_out1: d=%llx c=%x %x \n", d, pChan->count, (unsigned)(d % pChan->count)));
385 if ((d % pChan->count) == 0 && d != 0)
386 out = 1;
387 else
388 out = 0;
389 break;
390 case 3:
391 out = (d % pChan->count) < ((pChan->count + 1) >> 1);
392 break;
393 case 4:
394 case 5:
395 out = (d != pChan->count);
396 break;
397 }
398 return out;
399}
400
401
402static int pit_get_out(PPITSTATE pThis, int channel, int64_t current_time)
403{
404 PPITCHANNEL pChan = &pThis->channels[channel];
405 return pit_get_out1(pChan, current_time);
406}
407
408
409static int pit_get_gate(PPITSTATE pThis, int channel)
410{
411 PPITCHANNEL pChan = &pThis->channels[channel];
412 return pChan->gate;
413}
414
415
416/* if already latched, do not latch again */
417static void pit_latch_count(PPITCHANNEL pChan)
418{
419 if (!pChan->count_latched)
420 {
421 pChan->latched_count = pit_get_count(pChan);
422 pChan->count_latched = pChan->rw_mode;
423 LogFlow(("pit_latch_count: latched_count=%#06x / %10RU64 ns (c=%#06x m=%d)\n",
424 pChan->latched_count, ASMMultU64ByU32DivByU32(pChan->count - pChan->latched_count, 1000000000, PIT_FREQ),
425 pChan->count, pChan->mode));
426 }
427}
428
429#ifdef IN_RING3
430
431/* val must be 0 or 1 */
432static void pit_set_gate(PPITSTATE pThis, int channel, int val)
433{
434 PPITCHANNEL pChan = &pThis->channels[channel];
435 PTMTIMER pTimer = pChan->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
436
437 Assert((val & 1) == val);
438 Assert(TMTimerIsLockOwner(pTimer));
439
440 switch (EFFECTIVE_MODE(pChan->mode))
441 {
442 default:
443 case 0:
444 case 4:
445 /* XXX: just disable/enable counting */
446 break;
447 case 1:
448 case 5:
449 if (pChan->gate < val)
450 {
451 /* restart counting on rising edge */
452 Log(("pit_set_gate: restarting mode %d\n", pChan->mode));
453 pChan->count_load_time = TMTimerGet(pTimer);
454 pit_irq_timer_update(pChan, pChan->count_load_time, pChan->count_load_time, false);
455 }
456 break;
457 case 2:
458 case 3:
459 if (pChan->gate < val)
460 {
461 /* restart counting on rising edge */
462 Log(("pit_set_gate: restarting mode %d\n", pChan->mode));
463 pChan->count_load_time = pChan->u64ReloadTS = TMTimerGet(pTimer);
464 pit_irq_timer_update(pChan, pChan->count_load_time, pChan->count_load_time, false);
465 }
466 /* XXX: disable/enable counting */
467 break;
468 }
469 pChan->gate = val;
470}
471
472static void pit_load_count(PPITCHANNEL pChan, int val)
473{
474 PTMTIMER pTimer = pChan->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
475 Assert(TMTimerIsLockOwner(pTimer));
476
477 if (val == 0)
478 val = 0x10000;
479 pChan->count_load_time = pChan->u64ReloadTS = TMTimerGet(pTimer);
480 pChan->count = val;
481 pit_irq_timer_update(pChan, pChan->count_load_time, pChan->count_load_time, false);
482
483 /* log the new rate (ch 0 only). */
484 if (pChan->pTimerR3 /* ch 0 */)
485 {
486 if (pChan->cRelLogEntries++ < 32)
487 LogRel(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=0)\n",
488 pChan->mode, pChan->count, pChan->count, PIT_FREQ / pChan->count, (PIT_FREQ * 100 / pChan->count) % 100));
489 else
490 Log(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=0)\n",
491 pChan->mode, pChan->count, pChan->count, PIT_FREQ / pChan->count, (PIT_FREQ * 100 / pChan->count) % 100));
492 TMTimerSetFrequencyHint(pChan->CTX_SUFF(pTimer), PIT_FREQ / pChan->count);
493 }
494 else
495 Log(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=%d)\n",
496 pChan->mode, pChan->count, pChan->count, PIT_FREQ / pChan->count, (PIT_FREQ * 100 / pChan->count) % 100,
497 pChan - &pChan->CTX_SUFF(pPit)->channels[0]));
498}
499
500/* return -1 if no transition will occur. */
501static int64_t pit_get_next_transition_time(PPITCHANNEL pChan, uint64_t current_time)
502{
503 PTMTIMER pTimer = pChan->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
504 uint64_t d, next_time, base;
505 uint32_t period2;
506
507 d = ASMMultU64ByU32DivByU32(current_time - pChan->count_load_time, PIT_FREQ, TMTimerGetFreq(pTimer));
508 switch(EFFECTIVE_MODE(pChan->mode))
509 {
510 default:
511 case 0:
512 case 1:
513 if (d < pChan->count)
514 next_time = pChan->count;
515 else
516 return -1;
517 break;
518
519 /*
520 * Mode 2: The period is 'count' PIT ticks.
521 * When the counter reaches 1 we set the output low (for channel 0 that
522 * means lowering IRQ0). On the next tick, where we should be decrementing
523 * from 1 to 0, the count is loaded and the output goes high (channel 0
524 * means raising IRQ0 again and triggering timer interrupt).
525 *
526 * In VirtualBox we compress the pulse and flip-flop the IRQ line at the
527 * end of the period, which signals an interrupt at the exact same time.
528 */
529 case 2:
530 base = (d / pChan->count) * pChan->count;
531#ifndef VBOX /* see above */
532 if ((d - base) == 0 && d != 0)
533 next_time = base + pChan->count - 1;
534 else
535#endif
536 next_time = base + pChan->count;
537 break;
538 case 3:
539 base = (d / pChan->count) * pChan->count;
540 period2 = ((pChan->count + 1) >> 1);
541 if ((d - base) < period2)
542 next_time = base + period2;
543 else
544 next_time = base + pChan->count;
545 break;
546
547 /* Modes 4 and 5 generate a short pulse at the end of the time delay. This
548 * is similar to mode 2, except modes 4/5 aren't periodic. We use the same
549 * optimization - only use one timer callback and pulse the IRQ.
550 * Note: Tickless Linux kernels use PIT mode 4 with 'nolapic'.
551 */
552 case 4:
553 case 5:
554#ifdef VBOX
555 if (d <= pChan->count)
556 next_time = pChan->count;
557#else
558 if (d < pChan->count)
559 next_time = pChan->count;
560 else if (d == pChan->count)
561 next_time = pChan->count + 1;
562#endif
563 else
564 return -1;
565 break;
566 }
567
568 /* convert to timer units */
569 LogFlow(("PIT: next_time=%'14RU64 %'20RU64 mode=%#x count=%#06x\n", next_time,
570 ASMMultU64ByU32DivByU32(next_time, TMTimerGetFreq(pTimer), PIT_FREQ), pChan->mode, pChan->count));
571 next_time = pChan->count_load_time + ASMMultU64ByU32DivByU32(next_time, TMTimerGetFreq(pTimer), PIT_FREQ);
572
573 /* fix potential rounding problems */
574 if (next_time <= current_time)
575 next_time = current_time;
576
577 /* Add one to next_time; if we don't, integer truncation will cause
578 * the algorithm to think that at the end of each period, it'pChan still
579 * within the first one instead of at the beginning of the next one.
580 */
581 return next_time + 1;
582}
583
584static void pit_irq_timer_update(PPITCHANNEL pChan, uint64_t current_time, uint64_t now, bool in_timer)
585{
586 int64_t expire_time;
587 int irq_level;
588 Assert(TMTimerIsLockOwner(pChan->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer)));
589
590 if (!pChan->CTX_SUFF(pTimer))
591 return;
592 expire_time = pit_get_next_transition_time(pChan, current_time);
593 irq_level = pit_get_out1(pChan, current_time) ? PDM_IRQ_LEVEL_HIGH : PDM_IRQ_LEVEL_LOW;
594
595 /* If PIT is disabled by HPET - simply disconnect ticks from interrupt controllers,
596 * but do not modify other aspects of device operation.
597 */
598 if (!pChan->pPitR3->fDisabledByHpet)
599 {
600 PPDMDEVINS pDevIns = pChan->CTX_SUFF(pPit)->pDevIns;
601
602 switch (EFFECTIVE_MODE(pChan->mode))
603 {
604 case 2:
605 case 4:
606 case 5:
607 /* We just flip-flop the IRQ line to save an extra timer call,
608 * which isn't generally required. However, the pulse is only
609 * generated when running on the timer callback (and thus on
610 * the trailing edge of the output signal pulse).
611 */
612 if (in_timer)
613 {
614 PDMDevHlpISASetIrq(pDevIns, pChan->irq, PDM_IRQ_LEVEL_FLIP_FLOP);
615 break;
616 }
617 RT_FALL_THRU();
618 default:
619 PDMDevHlpISASetIrq(pDevIns, pChan->irq, irq_level);
620 break;
621 }
622 }
623
624 if (irq_level)
625 {
626 pChan->u64ReloadTS = now;
627 STAM_COUNTER_INC(&pChan->CTX_SUFF(pPit)->StatPITIrq);
628 }
629
630 if (expire_time != -1)
631 {
632 Log3(("pit_irq_timer_update: next=%'RU64 now=%'RU64\n", expire_time, now));
633 pChan->u64NextTS = expire_time;
634 TMTimerSet(pChan->CTX_SUFF(pTimer), pChan->u64NextTS);
635 }
636 else
637 {
638 LogFlow(("PIT: m=%d count=%#4x irq_level=%#x stopped\n", pChan->mode, pChan->count, irq_level));
639 TMTimerStop(pChan->CTX_SUFF(pTimer));
640 pChan->u64NextTS = UINT64_MAX;
641 }
642 pChan->next_transition_time = expire_time;
643}
644
645#endif /* IN_RING3 */
646
647
648/**
649 * @callback_method_impl{FNIOMIOPORTIN}
650 */
651PDMBOTHCBDECL(int) pitIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT uPort, uint32_t *pu32, unsigned cb)
652{
653 Log2(("pitIOPortRead: uPort=%#x cb=%x\n", uPort, cb));
654 NOREF(pvUser);
655 uPort &= 3;
656 if (cb != 1 || uPort == 3)
657 {
658 Log(("pitIOPortRead: uPort=%#x cb=%x *pu32=unused!\n", uPort, cb));
659 return VERR_IOM_IOPORT_UNUSED;
660 }
661 RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia */
662
663 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
664 PPITCHANNEL pChan = &pThis->channels[uPort];
665 int ret;
666
667 DEVPIT_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_READ);
668 if (pChan->status_latched)
669 {
670 pChan->status_latched = 0;
671 ret = pChan->status;
672 DEVPIT_UNLOCK(pDevIns, pThis);
673 }
674 else if (pChan->count_latched)
675 {
676 switch (pChan->count_latched)
677 {
678 default:
679 case RW_STATE_LSB:
680 ret = pChan->latched_count & 0xff;
681 pChan->count_latched = 0;
682 break;
683 case RW_STATE_MSB:
684 ret = pChan->latched_count >> 8;
685 pChan->count_latched = 0;
686 break;
687 case RW_STATE_WORD0:
688 ret = pChan->latched_count & 0xff;
689 pChan->count_latched = RW_STATE_MSB;
690 break;
691 }
692 DEVPIT_UNLOCK(pDevIns, pThis);
693 }
694 else
695 {
696 DEVPIT_UNLOCK(pDevIns, pThis);
697 DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_READ);
698 int count;
699 switch (pChan->read_state)
700 {
701 default:
702 case RW_STATE_LSB:
703 count = pit_get_count(pChan);
704 ret = count & 0xff;
705 break;
706 case RW_STATE_MSB:
707 count = pit_get_count(pChan);
708 ret = (count >> 8) & 0xff;
709 break;
710 case RW_STATE_WORD0:
711 count = pit_get_count(pChan);
712 ret = count & 0xff;
713 pChan->read_state = RW_STATE_WORD1;
714 break;
715 case RW_STATE_WORD1:
716 count = pit_get_count(pChan);
717 ret = (count >> 8) & 0xff;
718 pChan->read_state = RW_STATE_WORD0;
719 break;
720 }
721 DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
722 }
723
724 *pu32 = ret;
725 Log2(("pitIOPortRead: uPort=%#x cb=%x *pu32=%#04x\n", uPort, cb, *pu32));
726 return VINF_SUCCESS;
727}
728
729
730/**
731 * @callback_method_impl{FNIOMIOPORTOUT}
732 */
733PDMBOTHCBDECL(int) pitIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT uPort, uint32_t u32, unsigned cb)
734{
735 Log2(("pitIOPortWrite: uPort=%#x cb=%x u32=%#04x\n", uPort, cb, u32));
736 NOREF(pvUser);
737 if (cb != 1)
738 return VINF_SUCCESS;
739
740 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
741 uPort &= 3;
742 if (uPort == 3)
743 {
744 /*
745 * Port 43h - Mode/Command Register.
746 * 7 6 5 4 3 2 1 0
747 * * * . . . . . . Select channel: 0 0 = Channel 0
748 * 0 1 = Channel 1
749 * 1 0 = Channel 2
750 * 1 1 = Read-back command (8254 only)
751 * (Illegal on 8253)
752 * (Illegal on PS/2 {JAM})
753 * . . * * . . . . Command/Access mode: 0 0 = Latch count value command
754 * 0 1 = Access mode: lobyte only
755 * 1 0 = Access mode: hibyte only
756 * 1 1 = Access mode: lobyte/hibyte
757 * . . . . * * * . Operating mode: 0 0 0 = Mode 0, 0 0 1 = Mode 1,
758 * 0 1 0 = Mode 2, 0 1 1 = Mode 3,
759 * 1 0 0 = Mode 4, 1 0 1 = Mode 5,
760 * 1 1 0 = Mode 2, 1 1 1 = Mode 3
761 * . . . . . . . * BCD/Binary mode: 0 = 16-bit binary, 1 = four-digit BCD
762 */
763 unsigned channel = (u32 >> 6) & 0x3;
764 RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia */
765 if (channel == 3)
766 {
767 /* read-back command */
768 DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_WRITE);
769 for (channel = 0; channel < RT_ELEMENTS(pThis->channels); channel++)
770 {
771 PPITCHANNEL pChan = &pThis->channels[channel];
772 if (u32 & (2 << channel))
773 {
774 if (!(u32 & 0x20))
775 pit_latch_count(pChan);
776 if (!(u32 & 0x10) && !pChan->status_latched)
777 {
778 /* status latch */
779 /* XXX: add BCD and null count */
780 PTMTIMER pTimer = pChan->CTX_SUFF(pPit)->channels[0].CTX_SUFF(pTimer);
781 pChan->status = (pit_get_out1(pChan, TMTimerGet(pTimer)) << 7)
782 | (pChan->rw_mode << 4)
783 | (pChan->mode << 1)
784 | pChan->bcd;
785 pChan->status_latched = 1;
786 }
787 }
788 }
789 DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
790 }
791 else
792 {
793 PPITCHANNEL pChan = &pThis->channels[channel];
794 unsigned access = (u32 >> 4) & 3;
795 if (access == 0)
796 {
797 DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_WRITE);
798 pit_latch_count(pChan);
799 DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
800 }
801 else
802 {
803 DEVPIT_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_WRITE);
804 pChan->rw_mode = access;
805 pChan->read_state = access;
806 pChan->write_state = access;
807
808 pChan->mode = (u32 >> 1) & 7;
809 pChan->bcd = u32 & 1;
810 /* XXX: update irq timer ? */
811 DEVPIT_UNLOCK(pDevIns, pThis);
812 }
813 }
814 }
815 else
816 {
817#ifndef IN_RING3
818 /** @todo There is no reason not to do this in all contexts these
819 * days... */
820 return VINF_IOM_R3_IOPORT_WRITE;
821#else /* IN_RING3 */
822 /*
823 * Port 40-42h - Channel Data Ports.
824 */
825 RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia */
826 PPITCHANNEL pChan = &pThis->channels[uPort];
827 DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_WRITE);
828 switch (pChan->write_state)
829 {
830 default:
831 case RW_STATE_LSB:
832 pit_load_count(pChan, u32);
833 break;
834 case RW_STATE_MSB:
835 pit_load_count(pChan, u32 << 8);
836 break;
837 case RW_STATE_WORD0:
838 pChan->write_latch = u32;
839 pChan->write_state = RW_STATE_WORD1;
840 break;
841 case RW_STATE_WORD1:
842 pit_load_count(pChan, pChan->write_latch | (u32 << 8));
843 pChan->write_state = RW_STATE_WORD0;
844 break;
845 }
846 DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
847#endif /* !IN_RING3 */
848 }
849 return VINF_SUCCESS;
850}
851
852
853/**
854 * @callback_method_impl{FNIOMIOPORTIN, Speaker}
855 */
856PDMBOTHCBDECL(int) pitIOPortSpeakerRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT uPort, uint32_t *pu32, unsigned cb)
857{
858 RT_NOREF(pvUser, uPort);
859 if (cb == 1)
860 {
861 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
862 DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_READ);
863
864 const uint64_t u64Now = TMTimerGet(pThis->channels[0].CTX_SUFF(pTimer));
865 Assert(TMTimerGetFreq(pThis->channels[0].CTX_SUFF(pTimer)) == 1000000000); /* lazy bird. */
866
867 /* bit 6,7 Parity error stuff. */
868 /* bit 5 - mirrors timer 2 output condition. */
869 const int fOut = pit_get_out(pThis, 2, u64Now);
870 /* bit 4 - toggled with each (DRAM?) refresh request, every 15.085 u-op Chan.
871 ASSUMES ns timer freq, see assertion above. */
872#ifndef FAKE_REFRESH_CLOCK
873 const int fRefresh = (u64Now / 15085) & 1;
874#else
875 pThis->dummy_refresh_clock ^= 1;
876 const int fRefresh = pThis->dummy_refresh_clock;
877#endif
878 /* bit 2,3 NMI / parity status stuff. */
879 /* bit 1 - speaker data status */
880 const int fSpeakerStatus = pThis->speaker_data_on;
881 /* bit 0 - timer 2 clock gate to speaker status. */
882 const int fTimer2GateStatus = pit_get_gate(pThis, 2);
883
884 DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
885
886 *pu32 = fTimer2GateStatus
887 | (fSpeakerStatus << 1)
888 | (fRefresh << 4)
889 | (fOut << 5);
890 Log(("pitIOPortSpeakerRead: uPort=%#x cb=%x *pu32=%#x\n", uPort, cb, *pu32));
891 return VINF_SUCCESS;
892 }
893 Log(("pitIOPortSpeakerRead: uPort=%#x cb=%x *pu32=unused!\n", uPort, cb));
894 return VERR_IOM_IOPORT_UNUSED;
895}
896
897#ifdef IN_RING3
898
899/**
900 * @callback_method_impl{FNIOMIOPORTOUT, Speaker}
901 */
902PDMBOTHCBDECL(int) pitIOPortSpeakerWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT uPort, uint32_t u32, unsigned cb)
903{
904 RT_NOREF(pvUser, uPort);
905 if (cb == 1)
906 {
907 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
908 DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VERR_IGNORED);
909
910 pThis->speaker_data_on = (u32 >> 1) & 1;
911 pit_set_gate(pThis, 2, u32 & 1);
912
913 /** @todo r=klaus move this to a (system-specific) driver, which can
914 * abstract the details, and if necessary create a thread to minimize
915 * impact on VM execution. */
916# ifdef RT_OS_LINUX
917 if (pThis->enmSpeakerEmu != PIT_SPEAKER_EMU_NONE)
918 {
919 PPITCHANNEL pChan = &pThis->channels[2];
920 if (pThis->speaker_data_on)
921 {
922 Log2Func(("starting beep freq=%d\n", PIT_FREQ / pChan->count));
923 switch (pThis->enmSpeakerEmu)
924 {
925 case PIT_SPEAKER_EMU_CONSOLE:
926 {
927 int res;
928 res = ioctl(pThis->hHostSpeaker, KIOCSOUND, pChan->count);
929 if (res == -1)
930 {
931 LogRel(("PIT: speaker: ioctl failed errno=%d, disabling emulation\n", errno));
932 pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_NONE;
933 }
934 break;
935 }
936 case PIT_SPEAKER_EMU_EVDEV:
937 {
938 struct input_event e;
939 e.type = EV_SND;
940 e.code = SND_TONE;
941 e.value = PIT_FREQ / pChan->count;
942 int res = write(pThis->hHostSpeaker, &e, sizeof(struct input_event));
943 NOREF(res);
944 break;
945 }
946 case PIT_SPEAKER_EMU_TTY:
947 {
948 int res = write(pThis->hHostSpeaker, "\a", 1);
949 NOREF(res);
950 break;
951 }
952 case PIT_SPEAKER_EMU_NONE:
953 break;
954 default:
955 Log2Func(("unknown speaker emulation %d, disabling emulation\n", pThis->enmSpeakerEmu));
956 pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_NONE;
957 }
958 }
959 else
960 {
961 Log2Func(("stopping beep\n"));
962 switch (pThis->enmSpeakerEmu)
963 {
964 case PIT_SPEAKER_EMU_CONSOLE:
965 /* No error checking here. The Linux device driver
966 * implementation considers it an error (errno=22,
967 * EINVAL) to stop sound if it hasn't been started.
968 * Of course we could detect this by checking only
969 * for enabled->disabled transitions and ignoring
970 * disabled->disabled ones, but it's not worth the
971 * effort. */
972 ioctl(pThis->hHostSpeaker, KIOCSOUND, 0);
973 break;
974 case PIT_SPEAKER_EMU_EVDEV:
975 {
976 struct input_event e;
977 e.type = EV_SND;
978 e.code = SND_TONE;
979 e.value = 0;
980 int res = write(pThis->hHostSpeaker, &e, sizeof(struct input_event));
981 NOREF(res);
982 break;
983 }
984 case PIT_SPEAKER_EMU_TTY:
985 break;
986 case PIT_SPEAKER_EMU_NONE:
987 break;
988 default:
989 Log2Func(("unknown speaker emulation %d, disabling emulation\n", pThis->enmSpeakerEmu));
990 pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_NONE;
991 }
992 }
993 }
994# endif /* RT_OS_LINUX */
995
996 DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
997 }
998 Log(("pitIOPortSpeakerWrite: uPort=%#x cb=%x u32=%#x\n", uPort, cb, u32));
999 return VINF_SUCCESS;
1000}
1001
1002
1003/* -=-=-=-=-=- Saved state -=-=-=-=-=- */
1004
1005/**
1006 * @callback_method_impl{FNSSMDEVLIVEEXEC}
1007 */
1008static DECLCALLBACK(int) pitLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
1009{
1010 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
1011 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1012 RT_NOREF(uPass);
1013 pHlp->pfnSSMPutIOPort(pSSM, pThis->IOPortBaseCfg);
1014 pHlp->pfnSSMPutU8( pSSM, pThis->channels[0].irq);
1015 pHlp->pfnSSMPutBool( pSSM, pThis->fSpeakerCfg);
1016 return VINF_SSM_DONT_CALL_AGAIN;
1017}
1018
1019
1020/**
1021 * @callback_method_impl{FNSSMDEVSAVEEXEC}
1022 */
1023static DECLCALLBACK(int) pitSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1024{
1025 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
1026 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1027 PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
1028
1029 /* The config. */
1030 pitLiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
1031
1032 /* The state. */
1033 for (unsigned i = 0; i < RT_ELEMENTS(pThis->channels); i++)
1034 {
1035 PPITCHANNEL pChan = &pThis->channels[i];
1036 pHlp->pfnSSMPutU32(pSSM, pChan->count);
1037 pHlp->pfnSSMPutU16(pSSM, pChan->latched_count);
1038 pHlp->pfnSSMPutU8(pSSM, pChan->count_latched);
1039 pHlp->pfnSSMPutU8(pSSM, pChan->status_latched);
1040 pHlp->pfnSSMPutU8(pSSM, pChan->status);
1041 pHlp->pfnSSMPutU8(pSSM, pChan->read_state);
1042 pHlp->pfnSSMPutU8(pSSM, pChan->write_state);
1043 pHlp->pfnSSMPutU8(pSSM, pChan->write_latch);
1044 pHlp->pfnSSMPutU8(pSSM, pChan->rw_mode);
1045 pHlp->pfnSSMPutU8(pSSM, pChan->mode);
1046 pHlp->pfnSSMPutU8(pSSM, pChan->bcd);
1047 pHlp->pfnSSMPutU8(pSSM, pChan->gate);
1048 pHlp->pfnSSMPutU64(pSSM, pChan->count_load_time);
1049 pHlp->pfnSSMPutU64(pSSM, pChan->u64NextTS);
1050 pHlp->pfnSSMPutU64(pSSM, pChan->u64ReloadTS);
1051 pHlp->pfnSSMPutS64(pSSM, pChan->next_transition_time);
1052 if (pChan->CTX_SUFF(pTimer))
1053 TMR3TimerSave(pChan->CTX_SUFF(pTimer), pSSM);
1054 }
1055
1056 pHlp->pfnSSMPutS32(pSSM, pThis->speaker_data_on);
1057#ifdef FAKE_REFRESH_CLOCK
1058 pHlp->pfnSSMPutS32(pSSM, pThis->dummy_refresh_clock);
1059#else
1060 pHlp->pfnSSMPutS32(pSSM, 0);
1061#endif
1062
1063 pHlp->pfnSSMPutBool(pSSM, pThis->fDisabledByHpet);
1064
1065 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1066 return VINF_SUCCESS;
1067}
1068
1069
1070/**
1071 * @callback_method_impl{FNSSMDEVLOADEXEC}
1072 */
1073static DECLCALLBACK(int) pitLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1074{
1075 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
1076 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1077 int rc;
1078
1079 if ( uVersion != PIT_SAVED_STATE_VERSION
1080 && uVersion != PIT_SAVED_STATE_VERSION_VBOX_30
1081 && uVersion != PIT_SAVED_STATE_VERSION_VBOX_31)
1082 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1083
1084 /* The config. */
1085 if (uVersion > PIT_SAVED_STATE_VERSION_VBOX_30)
1086 {
1087 RTIOPORT IOPortBaseCfg;
1088 rc = pHlp->pfnSSMGetIOPort(pSSM, &IOPortBaseCfg); AssertRCReturn(rc, rc);
1089 if (IOPortBaseCfg != pThis->IOPortBaseCfg)
1090 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - IOPortBaseCfg: saved=%RTiop config=%RTiop"),
1091 IOPortBaseCfg, pThis->IOPortBaseCfg);
1092
1093 uint8_t u8Irq;
1094 rc = pHlp->pfnSSMGetU8(pSSM, &u8Irq); AssertRCReturn(rc, rc);
1095 if (u8Irq != pThis->channels[0].irq)
1096 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - u8Irq: saved=%#x config=%#x"),
1097 u8Irq, pThis->channels[0].irq);
1098
1099 bool fSpeakerCfg;
1100 rc = pHlp->pfnSSMGetBool(pSSM, &fSpeakerCfg); AssertRCReturn(rc, rc);
1101 if (fSpeakerCfg != pThis->fSpeakerCfg)
1102 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fSpeakerCfg: saved=%RTbool config=%RTbool"),
1103 fSpeakerCfg, pThis->fSpeakerCfg);
1104 }
1105
1106 if (uPass != SSM_PASS_FINAL)
1107 return VINF_SUCCESS;
1108
1109 /* The state. */
1110 for (unsigned i = 0; i < RT_ELEMENTS(pThis->channels); i++)
1111 {
1112 PPITCHANNEL pChan = &pThis->channels[i];
1113 pHlp->pfnSSMGetU32(pSSM, &pChan->count);
1114 pHlp->pfnSSMGetU16(pSSM, &pChan->latched_count);
1115 pHlp->pfnSSMGetU8(pSSM, &pChan->count_latched);
1116 pHlp->pfnSSMGetU8(pSSM, &pChan->status_latched);
1117 pHlp->pfnSSMGetU8(pSSM, &pChan->status);
1118 pHlp->pfnSSMGetU8(pSSM, &pChan->read_state);
1119 pHlp->pfnSSMGetU8(pSSM, &pChan->write_state);
1120 pHlp->pfnSSMGetU8(pSSM, &pChan->write_latch);
1121 pHlp->pfnSSMGetU8(pSSM, &pChan->rw_mode);
1122 pHlp->pfnSSMGetU8(pSSM, &pChan->mode);
1123 pHlp->pfnSSMGetU8(pSSM, &pChan->bcd);
1124 pHlp->pfnSSMGetU8(pSSM, &pChan->gate);
1125 pHlp->pfnSSMGetU64(pSSM, &pChan->count_load_time);
1126 pHlp->pfnSSMGetU64(pSSM, &pChan->u64NextTS);
1127 pHlp->pfnSSMGetU64(pSSM, &pChan->u64ReloadTS);
1128 pHlp->pfnSSMGetS64(pSSM, &pChan->next_transition_time);
1129 if (pChan->CTX_SUFF(pTimer))
1130 {
1131 TMR3TimerLoad(pChan->CTX_SUFF(pTimer), pSSM);
1132 LogRel(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=%d) (restore)\n",
1133 pChan->mode, pChan->count, pChan->count, PIT_FREQ / pChan->count, (PIT_FREQ * 100 / pChan->count) % 100, i));
1134 PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
1135 TMTimerSetFrequencyHint(pChan->CTX_SUFF(pTimer), PIT_FREQ / pChan->count);
1136 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1137 }
1138 pThis->channels[i].cRelLogEntries = 0;
1139 }
1140
1141 pHlp->pfnSSMGetS32(pSSM, &pThis->speaker_data_on);
1142#ifdef FAKE_REFRESH_CLOCK
1143 pHlp->pfnSSMGetS32(pSSM, &pThis->dummy_refresh_clock);
1144#else
1145 int32_t u32Dummy;
1146 pHlp->pfnSSMGetS32(pSSM, &u32Dummy);
1147#endif
1148 if (uVersion > PIT_SAVED_STATE_VERSION_VBOX_31)
1149 pHlp->pfnSSMGetBool(pSSM, &pThis->fDisabledByHpet);
1150
1151 return VINF_SUCCESS;
1152}
1153
1154
1155/* -=-=-=-=-=- Timer -=-=-=-=-=- */
1156
1157/**
1158 * @callback_method_impl{FNTMTIMERDEV}
1159 * @param pvUser Pointer to the PIT channel state.
1160 */
1161static DECLCALLBACK(void) pitTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1162{
1163 RT_NOREF(pDevIns);
1164 PPITCHANNEL pChan = (PPITCHANNEL)pvUser;
1165 STAM_PROFILE_ADV_START(&pChan->CTX_SUFF(pPit)->StatPITHandler, a);
1166
1167 Log(("pitTimer\n"));
1168 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &PDMDEVINS_2_DATA(pDevIns, PPITSTATE)->CritSect));
1169 Assert(TMTimerIsLockOwner(pTimer));
1170
1171 pit_irq_timer_update(pChan, pChan->next_transition_time, TMTimerGet(pTimer), true);
1172
1173 STAM_PROFILE_ADV_STOP(&pChan->CTX_SUFF(pPit)->StatPITHandler, a);
1174}
1175
1176
1177/* -=-=-=-=-=- Debug Info -=-=-=-=-=- */
1178
1179/**
1180 * @callback_method_impl{FNDBGFHANDLERDEV}
1181 */
1182static DECLCALLBACK(void) pitInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1183{
1184 RT_NOREF(pszArgs);
1185 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
1186 unsigned i;
1187 for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
1188 {
1189 const PITCHANNEL *pChan = &pThis->channels[i];
1190
1191 pHlp->pfnPrintf(pHlp,
1192 "PIT (i8254) channel %d status: irq=%#x\n"
1193 " count=%08x" " latched_count=%04x count_latched=%02x\n"
1194 " status=%02x status_latched=%02x read_state=%02x\n"
1195 " write_state=%02x write_latch=%02x rw_mode=%02x\n"
1196 " mode=%02x bcd=%02x gate=%02x\n"
1197 " count_load_time=%016RX64 next_transition_time=%016RX64\n"
1198 " u64ReloadTS=%016RX64 u64NextTS=%016RX64\n"
1199 ,
1200 i, pChan->irq,
1201 pChan->count, pChan->latched_count, pChan->count_latched,
1202 pChan->status, pChan->status_latched, pChan->read_state,
1203 pChan->write_state, pChan->write_latch, pChan->rw_mode,
1204 pChan->mode, pChan->bcd, pChan->gate,
1205 pChan->count_load_time, pChan->next_transition_time,
1206 pChan->u64ReloadTS, pChan->u64NextTS);
1207 }
1208#ifdef FAKE_REFRESH_CLOCK
1209 pHlp->pfnPrintf(pHlp, "speaker_data_on=%#x dummy_refresh_clock=%#x\n",
1210 pThis->speaker_data_on, pThis->dummy_refresh_clock);
1211#else
1212 pHlp->pfnPrintf(pHlp, "speaker_data_on=%#x\n", pThis->speaker_data_on);
1213#endif
1214 if (pThis->fDisabledByHpet)
1215 pHlp->pfnPrintf(pHlp, "Disabled by HPET\n");
1216}
1217
1218
1219/* -=-=-=-=-=- IHpetLegacyNotify -=-=-=-=-=- */
1220
1221/**
1222 * @interface_method_impl{PDMIHPETLEGACYNOTIFY,pfnModeChanged}
1223 */
1224static DECLCALLBACK(void) pitNotifyHpetLegacyNotify_ModeChanged(PPDMIHPETLEGACYNOTIFY pInterface, bool fActivated)
1225{
1226 PPITSTATE pThis = RT_FROM_MEMBER(pInterface, PITSTATE, IHpetLegacyNotify);
1227 PPDMDEVINS pDevIns = pThis->pDevIns;
1228 PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
1229
1230 pThis->fDisabledByHpet = fActivated;
1231
1232 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1233}
1234
1235
1236/* -=-=-=-=-=- PDMDEVINS::IBase -=-=-=-=-=- */
1237
1238/**
1239 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1240 */
1241static DECLCALLBACK(void *) pitQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1242{
1243 PPDMDEVINS pDevIns = RT_FROM_MEMBER(pInterface, PDMDEVINS, IBase);
1244 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
1245 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDevIns->IBase);
1246 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHPETLEGACYNOTIFY, &pThis->IHpetLegacyNotify);
1247 return NULL;
1248}
1249
1250
1251/* -=-=-=-=-=- PDMDEVREG -=-=-=-=-=- */
1252
1253/**
1254 * @interface_method_impl{PDMDEVREG,pfnRelocate}
1255 */
1256static DECLCALLBACK(void) pitRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1257{
1258 RT_NOREF(offDelta);
1259 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
1260 LogFlow(("pitRelocate: \n"));
1261
1262 for (unsigned i = 0; i < RT_ELEMENTS(pThis->channels); i++)
1263 {
1264 PPITCHANNEL pChan = &pThis->channels[i];
1265 if (pChan->pTimerR3)
1266 pChan->pTimerRC = TMTimerRCPtr(pChan->pTimerR3);
1267 pThis->channels[i].pPitRC = PDMINS_2_DATA_RCPTR(pDevIns);
1268 }
1269}
1270
1271
1272/**
1273 * @interface_method_impl{PDMDEVREG,pfnReset}
1274 */
1275static DECLCALLBACK(void) pitReset(PPDMDEVINS pDevIns)
1276{
1277 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
1278 LogFlow(("pitReset: \n"));
1279
1280 DEVPIT_R3_LOCK_BOTH(pDevIns, pThis);
1281
1282 pThis->fDisabledByHpet = false;
1283
1284 for (unsigned i = 0; i < RT_ELEMENTS(pThis->channels); i++)
1285 {
1286 PPITCHANNEL pChan = &pThis->channels[i];
1287
1288#if 1 /* Set everything back to virgin state. (might not be strictly correct) */
1289 pChan->latched_count = 0;
1290 pChan->count_latched = 0;
1291 pChan->status_latched = 0;
1292 pChan->status = 0;
1293 pChan->read_state = 0;
1294 pChan->write_state = 0;
1295 pChan->write_latch = 0;
1296 pChan->rw_mode = 0;
1297 pChan->bcd = 0;
1298#endif
1299 pChan->u64NextTS = UINT64_MAX;
1300 pChan->cRelLogEntries = 0;
1301 pChan->mode = 3;
1302 pChan->gate = (i != 2);
1303 pit_load_count(pChan, 0);
1304 }
1305
1306 DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
1307}
1308
1309
1310/**
1311 * @interface_method_impl{PDMDEVREG,pfnConstruct}
1312 */
1313static DECLCALLBACK(int) pitConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
1314{
1315 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1316 PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
1317 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1318 int rc;
1319 uint8_t u8Irq;
1320 uint16_t u16Base;
1321 bool fSpeaker;
1322 unsigned i;
1323 Assert(iInstance == 0);
1324
1325 /*
1326 * Validate and read the configuration.
1327 */
1328 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "Irq|Base|SpeakerEnabled|PassthroughSpeaker|PassthroughSpeakerDevice", "");
1329
1330 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "Irq", &u8Irq, 0);
1331 if (RT_FAILURE(rc))
1332 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"Irq\" as a uint8_t failed"));
1333
1334 rc = pHlp->pfnCFGMQueryU16Def(pCfg, "Base", &u16Base, 0x40);
1335 if (RT_FAILURE(rc))
1336 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"Base\" as a uint16_t failed"));
1337
1338 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "SpeakerEnabled", &fSpeaker, true);
1339 if (RT_FAILURE(rc))
1340 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"SpeakerEnabled\" as a bool failed"));
1341
1342 uint8_t uPassthroughSpeaker;
1343 char *pszPassthroughSpeakerDevice = NULL;
1344 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "PassthroughSpeaker", &uPassthroughSpeaker, 0);
1345 if (RT_FAILURE(rc))
1346 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: failed to read PassthroughSpeaker as uint8_t"));
1347 if (uPassthroughSpeaker)
1348 {
1349 rc = pHlp->pfnCFGMQueryStringAllocDef(pCfg, "PassthroughSpeakerDevice", &pszPassthroughSpeakerDevice, NULL);
1350 if (RT_FAILURE(rc))
1351 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: failed to read PassthroughSpeakerDevice as string"));
1352 }
1353
1354 /*
1355 * Init the data.
1356 */
1357 pThis->pDevIns = pDevIns;
1358 pThis->IOPortBaseCfg = u16Base;
1359 pThis->fSpeakerCfg = fSpeaker;
1360 pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_NONE;
1361 if (uPassthroughSpeaker)
1362 {
1363 /** @todo r=klaus move this to a (system-specific) driver */
1364#ifdef RT_OS_LINUX
1365 int fd = -1;
1366 if ((uPassthroughSpeaker == 1 || uPassthroughSpeaker == 100) && fd == -1)
1367 fd = pitTryDeviceOpenSanitizeIoctl("/dev/input/by-path/platform-pcspkr-event-spkr", O_WRONLY);
1368 if ((uPassthroughSpeaker == 2 || uPassthroughSpeaker == 100) && fd == -1)
1369 fd = pitTryDeviceOpenSanitizeIoctl("/dev/tty", O_WRONLY);
1370 if ((uPassthroughSpeaker == 3 || uPassthroughSpeaker == 100) && fd == -1)
1371 {
1372 fd = pitTryDeviceOpenSanitizeIoctl("/dev/tty0", O_WRONLY);
1373 if (fd == -1)
1374 fd = pitTryDeviceOpenSanitizeIoctl("/dev/vc/0", O_WRONLY);
1375 }
1376 if ((uPassthroughSpeaker == 9 || uPassthroughSpeaker == 100) && pszPassthroughSpeakerDevice && fd == -1)
1377 fd = pitTryDeviceOpenSanitizeIoctl(pszPassthroughSpeakerDevice, O_WRONLY);
1378 if (pThis->enmSpeakerEmu == PIT_SPEAKER_EMU_NONE && fd != -1)
1379 {
1380 pThis->hHostSpeaker = fd;
1381 if (ioctl(fd, EVIOCGSND(0)) != -1)
1382 {
1383 pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_EVDEV;
1384 LogRel(("PIT: speaker: emulation mode evdev\n"));
1385 }
1386 else
1387 {
1388 pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_CONSOLE;
1389 LogRel(("PIT: speaker: emulation mode console\n"));
1390 }
1391 }
1392 if ((uPassthroughSpeaker == 70 || uPassthroughSpeaker == 100) && fd == -1)
1393 fd = pitTryDeviceOpen("/dev/tty", O_WRONLY);
1394 if ((uPassthroughSpeaker == 79 || uPassthroughSpeaker == 100) && pszPassthroughSpeakerDevice && fd == -1)
1395 fd = pitTryDeviceOpen(pszPassthroughSpeakerDevice, O_WRONLY);
1396 if (pThis->enmSpeakerEmu == PIT_SPEAKER_EMU_NONE && fd != -1)
1397 {
1398 pThis->hHostSpeaker = fd;
1399 pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_TTY;
1400 LogRel(("PIT: speaker: emulation mode tty\n"));
1401 }
1402 if (pThis->enmSpeakerEmu == PIT_SPEAKER_EMU_NONE)
1403 {
1404 Assert(fd == -1);
1405 LogRel(("PIT: speaker: no emulation possible\n"));
1406 }
1407#else
1408 LogRel(("PIT: speaker: emulation deactivated\n"));
1409#endif
1410 if (pszPassthroughSpeakerDevice)
1411 {
1412 PDMDevHlpMMHeapFree(pDevIns, pszPassthroughSpeakerDevice);
1413 pszPassthroughSpeakerDevice = NULL;
1414 }
1415 }
1416 pThis->channels[0].irq = u8Irq;
1417 for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
1418 {
1419 pThis->channels[i].pPitR3 = pThis;
1420 pThis->channels[i].pPitR0 = PDMINS_2_DATA_R0PTR(pDevIns);
1421 pThis->channels[i].pPitRC = PDMINS_2_DATA_RCPTR(pDevIns);
1422 }
1423
1424 /*
1425 * Interfaces
1426 */
1427 /* IBase */
1428 pDevIns->IBase.pfnQueryInterface = pitQueryInterface;
1429 /* IHpetLegacyNotify */
1430 pThis->IHpetLegacyNotify.pfnModeChanged = pitNotifyHpetLegacyNotify_ModeChanged;
1431
1432 /*
1433 * We do our own locking. This must be done before creating timers.
1434 */
1435 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "pit#%u", iInstance);
1436 AssertRCReturn(rc, rc);
1437
1438 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
1439 AssertRCReturn(rc, rc);
1440
1441 /*
1442 * Create the timer, make it take our critsect.
1443 */
1444 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, pitTimer, &pThis->channels[0],
1445 TMTIMER_FLAGS_NO_CRIT_SECT, "i8254 Programmable Interval Timer",
1446 &pThis->channels[0].pTimerR3);
1447 if (RT_FAILURE(rc))
1448 return rc;
1449 pThis->channels[0].pTimerRC = TMTimerRCPtr(pThis->channels[0].pTimerR3);
1450 pThis->channels[0].pTimerR0 = TMTimerR0Ptr(pThis->channels[0].pTimerR3);
1451 rc = TMR3TimerSetCritSect(pThis->channels[0].pTimerR3, &pThis->CritSect);
1452 AssertRCReturn(rc, rc);
1453
1454 /*
1455 * Register I/O ports.
1456 */
1457 rc = PDMDevHlpIOPortRegister(pDevIns, u16Base, 4, NULL, pitIOPortWrite, pitIOPortRead, NULL, NULL, "i8254 Programmable Interval Timer");
1458 if (RT_FAILURE(rc))
1459 return rc;
1460 if (pDevIns->fRCEnabled)
1461 {
1462 rc = PDMDevHlpIOPortRegisterRC(pDevIns, u16Base, 4, 0, "pitIOPortWrite", "pitIOPortRead", NULL, NULL, "i8254 Programmable Interval Timer");
1463 if (RT_FAILURE(rc))
1464 return rc;
1465 }
1466 if (pDevIns->fR0Enabled)
1467 {
1468 rc = PDMDevHlpIOPortRegisterR0(pDevIns, u16Base, 4, 0, "pitIOPortWrite", "pitIOPortRead", NULL, NULL, "i8254 Programmable Interval Timer");
1469 if (RT_FAILURE(rc))
1470 return rc;
1471 }
1472
1473 if (fSpeaker)
1474 {
1475 rc = PDMDevHlpIOPortRegister(pDevIns, 0x61, 1, NULL, pitIOPortSpeakerWrite, pitIOPortSpeakerRead, NULL, NULL, "PC Speaker");
1476 if (RT_FAILURE(rc))
1477 return rc;
1478 if (pDevIns->fRCEnabled)
1479 {
1480 rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x61, 1, 0, NULL, "pitIOPortSpeakerRead", NULL, NULL, "PC Speaker");
1481 if (RT_FAILURE(rc))
1482 return rc;
1483 }
1484 }
1485
1486 /*
1487 * Saved state.
1488 */
1489 rc = PDMDevHlpSSMRegister3(pDevIns, PIT_SAVED_STATE_VERSION, sizeof(*pThis), pitLiveExec, pitSaveExec, pitLoadExec);
1490 if (RT_FAILURE(rc))
1491 return rc;
1492
1493 /*
1494 * Initialize the device state.
1495 */
1496 pitReset(pDevIns);
1497
1498 /*
1499 * Register statistics and debug info.
1500 */
1501 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPITIrq, STAMTYPE_COUNTER, "/TM/PIT/Irq", STAMUNIT_OCCURENCES, "The number of times a timer interrupt was triggered.");
1502 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPITHandler, STAMTYPE_PROFILE, "/TM/PIT/Handler", STAMUNIT_TICKS_PER_CALL, "Profiling timer callback handler.");
1503
1504 PDMDevHlpDBGFInfoRegister(pDevIns, "pit", "Display PIT (i8254) status. (no arguments)", pitInfo);
1505
1506 return VINF_SUCCESS;
1507}
1508
1509#endif /* IN_RING3 */
1510
1511/**
1512 * The device registration structure.
1513 */
1514const PDMDEVREG g_DeviceI8254 =
1515{
1516 /* .u32Version = */ PDM_DEVREG_VERSION,
1517 /* .uReserved0 = */ 0,
1518 /* .szName = */ "i8254",
1519 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ,
1520 /* .fClass = */ PDM_DEVREG_CLASS_PIT,
1521 /* .cMaxInstances = */ 1,
1522 /* .uSharedVersion = */ 42,
1523 /* .cbInstanceShared = */ sizeof(PITSTATE),
1524 /* .cbInstanceCC = */ 0,
1525 /* .cbInstanceRC = */ 0,
1526 /* .cMaxPciDevices = */ 0,
1527 /* .cMaxMsixVectors = */ 0,
1528 /* .pszDescription = */ "Intel 8254 Programmable Interval Timer (PIT) And Dummy Speaker Device",
1529#if defined(IN_RING3)
1530 /* .pszRCMod = */ "VBoxDDRC.rc",
1531 /* .pszR0Mod = */ "VBoxDDR0.r0",
1532 /* .pfnConstruct = */ pitConstruct,
1533 /* .pfnDestruct = */ NULL,
1534 /* .pfnRelocate = */ pitRelocate,
1535 /* .pfnMemSetup = */ NULL,
1536 /* .pfnPowerOn = */ NULL,
1537 /* .pfnReset = */ pitReset,
1538 /* .pfnSuspend = */ NULL,
1539 /* .pfnResume = */ NULL,
1540 /* .pfnAttach = */ NULL,
1541 /* .pfnDetach = */ NULL,
1542 /* .pfnQueryInterface = */ NULL,
1543 /* .pfnInitComplete = */ NULL,
1544 /* .pfnPowerOff = */ NULL,
1545 /* .pfnSoftReset = */ NULL,
1546 /* .pfnReserved0 = */ NULL,
1547 /* .pfnReserved1 = */ NULL,
1548 /* .pfnReserved2 = */ NULL,
1549 /* .pfnReserved3 = */ NULL,
1550 /* .pfnReserved4 = */ NULL,
1551 /* .pfnReserved5 = */ NULL,
1552 /* .pfnReserved6 = */ NULL,
1553 /* .pfnReserved7 = */ NULL,
1554#elif defined(IN_RING0)
1555 /* .pfnEarlyConstruct = */ NULL,
1556 /* .pfnConstruct = */ NULL,
1557 /* .pfnDestruct = */ NULL,
1558 /* .pfnFinalDestruct = */ NULL,
1559 /* .pfnRequest = */ NULL,
1560 /* .pfnReserved0 = */ NULL,
1561 /* .pfnReserved1 = */ NULL,
1562 /* .pfnReserved2 = */ NULL,
1563 /* .pfnReserved3 = */ NULL,
1564 /* .pfnReserved4 = */ NULL,
1565 /* .pfnReserved5 = */ NULL,
1566 /* .pfnReserved6 = */ NULL,
1567 /* .pfnReserved7 = */ NULL,
1568#elif defined(IN_RC)
1569 /* .pfnConstruct = */ NULL,
1570 /* .pfnReserved0 = */ NULL,
1571 /* .pfnReserved1 = */ NULL,
1572 /* .pfnReserved2 = */ NULL,
1573 /* .pfnReserved3 = */ NULL,
1574 /* .pfnReserved4 = */ NULL,
1575 /* .pfnReserved5 = */ NULL,
1576 /* .pfnReserved6 = */ NULL,
1577 /* .pfnReserved7 = */ NULL,
1578#else
1579# error "Not in IN_RING3, IN_RING0 or IN_RC!"
1580#endif
1581 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
1582};
1583
1584#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

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