VirtualBox

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

Last change on this file since 85179 was 84334, checked in by vboxsync, 5 years ago

Devices/{DevPIC,DevPIT-i8254}: Doxygen

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