VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevHPET.cpp@ 37608

Last change on this file since 37608 was 37541, checked in by vboxsync, 14 years ago

DevHPET: Some minor cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 49.6 KB
Line 
1/* $Id: DevHPET.cpp 37541 2011-06-17 13:09:56Z vboxsync $ */
2/** @file
3 * HPET virtual device - high precision event timer emulation
4 */
5
6/*
7 * Copyright (C) 2009-2011 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/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_DEV_HPET
22#include <VBox/vmm/pdmdev.h>
23#include <VBox/vmm/stam.h>
24#include <VBox/log.h>
25#include <iprt/assert.h>
26#include <iprt/asm-math.h>
27#include <iprt/string.h>
28
29#include "VBoxDD.h"
30
31
32/*******************************************************************************
33* Defined Constants And Macros *
34*******************************************************************************/
35/*
36 * Current limitations:
37 * - not entirely correct time of interrupt, i.e. never
38 * schedule interrupt earlier than in 1ms
39 * - statistics not implemented
40 * - level-triggered mode not implemented
41 */
42
43/** Base address for MMIO. */
44#define HPET_BASE 0xfed00000
45
46/** The number of timers for PIIX4 / PIIX3. */
47#define HPET_NUM_TIMERS_PIIX 3
48/** The number of timers for ICH9. */
49#define HPET_NUM_TIMERS_ICH9 4
50
51/** HPET clock period for PIIX4 / PIIX3.
52 * 10000000 femtoseconds == 10ns.
53 */
54#define HPET_CLK_PERIOD_PIIX UINT32_C(10000000)
55
56/** HPET clock period for ICH9.
57 * 69841279 femtoseconds == 69.84 ns (1 / 14.31818MHz).
58 */
59#define HPET_CLK_PERIOD_ICH9 UINT32_C(69841279)
60
61/*
62 * Femptosecods in nanosecond
63 */
64#define FS_PER_NS 1000000
65
66/*
67 * Interrupt type
68 */
69#define HPET_TIMER_TYPE_LEVEL 1
70#define HPET_TIMER_TYPE_EDGE 0
71
72/* Delivery mode */
73/* Via APIC */
74#define HPET_TIMER_DELIVERY_APIC 0
75/* Via FSB */
76#define HPET_TIMER_DELIVERY_FSB 1
77
78#define HPET_TIMER_CAP_FSB_INT_DEL (1 << 15)
79#define HPET_TIMER_CAP_PER_INT (1 << 4)
80
81#define HPET_CFG_ENABLE 0x001 /* ENABLE_CNF */
82#define HPET_CFG_LEGACY 0x002 /* LEG_RT_CNF */
83
84#define HPET_ID 0x000
85#define HPET_PERIOD 0x004
86#define HPET_CFG 0x010
87#define HPET_STATUS 0x020
88#define HPET_COUNTER 0x0f0
89#define HPET_TN_CFG 0x000
90#define HPET_TN_CMP 0x008
91#define HPET_TN_ROUTE 0x010
92#define HPET_CFG_WRITE_MASK 0x3
93
94#define HPET_TN_INT_TYPE RT_BIT_64(1)
95#define HPET_TN_ENABLE RT_BIT_64(2)
96#define HPET_TN_PERIODIC RT_BIT_64(3)
97#define HPET_TN_PERIODIC_CAP RT_BIT_64(4)
98#define HPET_TN_SIZE_CAP RT_BIT_64(5)
99#define HPET_TN_SETVAL RT_BIT_64(6)
100#define HPET_TN_32BIT RT_BIT_64(8)
101#define HPET_TN_INT_ROUTE_MASK UINT64_C(0x3e00)
102#define HPET_TN_CFG_WRITE_MASK UINT64_C(0x3e46)
103#define HPET_TN_INT_ROUTE_SHIFT 9
104#define HPET_TN_INT_ROUTE_CAP_SHIFT 32
105#define HPET_TN_CFG_BITS_READONLY_OR_RESERVED 0xffff80b1U
106
107/** Extract the timer count from the capabilities.
108 * @todo Check if the mask is correct. */
109#define HPET_CAP_GET_TIMERS(a_u32) ( ((a_u32) >> 8) & 0xf )
110
111/** The version of the saved state. */
112#define HPET_SAVED_STATE_VERSION 2
113/** Empty saved state */
114#define HPET_SAVED_STATE_VERSION_EMPTY 1
115
116
117/**
118 * Acquires the HPET lock or returns.
119 */
120#define DEVHPET_LOCK_RETURN(a_pThis, a_rcBusy) \
121 do { \
122 int rcLock = PDMCritSectEnter(&(a_pThis)->csLock, (a_rcBusy)); \
123 if (rcLock != VINF_SUCCESS) \
124 return rcLock; \
125 } while (0)
126
127/**
128 * Releases the HPET lock.
129 */
130#define DEVHPET_UNLOCK(a_pThis) \
131 do { PDMCritSectLeave(&(a_pThis)->csLock); } while (0)
132
133
134/**
135 * Acquires the TM lock and HPET lock, returns on failure.
136 */
137#define DEVHPET_LOCK_BOTH_RETURN(a_pThis, a_rcBusy) \
138 do { \
139 int rcLock = TMTimerLock((a_pThis)->aTimers[0].CTX_SUFF(pTimer), (a_rcBusy)); \
140 if (rcLock != VINF_SUCCESS) \
141 return rcLock; \
142 rcLock = PDMCritSectEnter(&(a_pThis)->csLock, (a_rcBusy)); \
143 if (rcLock != VINF_SUCCESS) \
144 { \
145 TMTimerUnlock((a_pThis)->aTimers[0].CTX_SUFF(pTimer)); \
146 return rcLock; \
147 } \
148 } while (0)
149
150
151/**
152 * Releases the HPET lock and TM lock.
153 */
154#define DEVHPET_UNLOCK_BOTH(a_pThis) \
155 do { \
156 PDMCritSectLeave(&(a_pThis)->csLock); \
157 TMTimerUnlock((a_pThis)->aTimers[0].CTX_SUFF(pTimer)); \
158 } while (0)
159
160
161/*******************************************************************************
162* Structures and Typedefs *
163*******************************************************************************/
164struct HpetState;
165typedef struct HpetTimer
166{
167 /** The HPET timer - R3 Ptr. */
168 PTMTIMERR3 pTimerR3;
169 /** Pointer to the instance data - R3 Ptr. */
170 R3PTRTYPE(struct HpetState *) pHpetR3;
171
172 /** The HPET timer - R0 Ptr. */
173 PTMTIMERR0 pTimerR0;
174 /** Pointer to the instance data - R0 Ptr. */
175 R0PTRTYPE(struct HpetState *) pHpetR0;
176
177 /** The HPET timer - RC Ptr. */
178 PTMTIMERRC pTimerRC;
179 /** Pointer to the instance data - RC Ptr. */
180 RCPTRTYPE(struct HpetState *) pHpetRC;
181
182 /** Timer index. */
183 uint8_t idxTimer;
184 /** Wrap. */
185 uint8_t u8Wrap;
186 /** Alignment. */
187 uint32_t alignment0;
188
189 /** @name Memory-mapped, software visible timer registers.
190 * @{ */
191 /** Configuration/capabilities. */
192 uint64_t u64Config;
193 /** Comparator. */
194 uint64_t u64Cmp;
195 /** FSB route, not supported now. */
196 uint64_t u64Fsb;
197 /** @} */
198
199 /** @name Hidden register state.
200 * @{ */
201 /** Last value written to comparator. */
202 uint64_t u64Period;
203 /** @} */
204} HpetTimer;
205AssertCompileMemberAlignment(HpetTimer, u64Config, sizeof(uint64_t));
206
207typedef struct HpetState
208{
209 /** Pointer to the device instance. - R3 ptr. */
210 PPDMDEVINSR3 pDevInsR3;
211 /** The HPET helpers - R3 Ptr. */
212 PCPDMHPETHLPR3 pHpetHlpR3;
213
214 /** Pointer to the device instance. - R0 ptr. */
215 PPDMDEVINSR0 pDevInsR0;
216 /** The HPET helpers - R0 Ptr. */
217 PCPDMHPETHLPR0 pHpetHlpR0;
218
219 /** Pointer to the device instance. - RC ptr. */
220 PPDMDEVINSRC pDevInsRC;
221 /** The HPET helpers - RC Ptr. */
222 PCPDMHPETHLPRC pHpetHlpRC;
223
224 /** Timer structures. */
225 HpetTimer aTimers[RT_MAX(HPET_NUM_TIMERS_PIIX, HPET_NUM_TIMERS_ICH9)];
226
227 /** Offset realtive to the virtual sync clock. */
228 uint64_t u64HpetOffset;
229
230 /** @name Memory-mapped, software visible registers
231 * @{ */
232 /** Capabilities. */
233 uint32_t u32Capabilities;
234 /** HPET_PERIOD - . */
235 uint32_t u32Period;
236 /** Configuration. */
237 uint64_t u64HpetConfig;
238 /** Interrupt status register. */
239 uint64_t u64Isr;
240 /** Main counter. */
241 uint64_t u64HpetCounter;
242 /** @} */
243
244 /** Global device lock. */
245 PDMCRITSECT csLock;
246
247 /** If we emulate ICH9 HPET (different frequency & timer count). */
248 bool fIch9;
249 uint8_t padding0[7];
250} HpetState;
251
252
253#ifndef VBOX_DEVICE_STRUCT_TESTCASE
254
255
256DECLINLINE(bool) hpet32bitTimer(HpetTimer *pHpetTimer)
257{
258 uint64_t u64Cfg = pHpetTimer->u64Config;
259
260 return ((u64Cfg & HPET_TN_SIZE_CAP) == 0) || ((u64Cfg & HPET_TN_32BIT) != 0);
261}
262
263DECLINLINE(uint64_t) hpetInvalidValue(HpetTimer *pHpetTimer)
264{
265 return hpet32bitTimer(pHpetTimer) ? UINT32_MAX : UINT64_MAX;
266}
267
268DECLINLINE(uint32_t) hpetTimeAfter32(uint64_t a, uint64_t b)
269{
270 return ((int32_t)(b) - (int32_t)(a) <= 0);
271}
272
273DECLINLINE(uint32_t) hpetTimeAfter64(uint64_t a, uint64_t b)
274{
275 return ((int64_t)(b) - (int64_t)(a) <= 0);
276}
277
278DECLINLINE(uint64_t) hpetTicksToNs(HpetState *pThis, uint64_t value)
279{
280 return ASMMultU64ByU32DivByU32(value, pThis->u32Period, FS_PER_NS);
281}
282
283DECLINLINE(uint64_t) nsToHpetTicks(HpetState const *pThis, uint64_t u64Value)
284{
285 return ASMMultU64ByU32DivByU32(u64Value, FS_PER_NS, pThis->u32Period);
286}
287
288DECLINLINE(uint64_t) hpetGetTicks(HpetState const *pThis)
289{
290 /*
291 * We can use any timer to get current time, they all go
292 * with the same speed.
293 */
294 return nsToHpetTicks(pThis,
295 TMTimerGet(pThis->aTimers[0].CTX_SUFF(pTimer))
296 + pThis->u64HpetOffset);
297}
298
299DECLINLINE(uint64_t) hpetUpdateMasked(uint64_t u64NewValue,
300 uint64_t u64OldValue,
301 uint64_t u64Mask)
302{
303 u64NewValue &= u64Mask;
304 u64NewValue |= (u64OldValue & ~u64Mask);
305 return u64NewValue;
306}
307
308DECLINLINE(bool) hpetBitJustSet(uint64_t u64OldValue,
309 uint64_t u64NewValue,
310 uint64_t u64Mask)
311{
312 return !(u64OldValue & u64Mask)
313 && !!(u64NewValue & u64Mask);
314}
315
316DECLINLINE(bool) hpetBitJustCleared(uint64_t u64OldValue,
317 uint64_t u64NewValue,
318 uint64_t u64Mask)
319{
320 return !!(u64OldValue & u64Mask)
321 && !(u64NewValue & u64Mask);
322}
323
324DECLINLINE(uint64_t) hpetComputeDiff(HpetTimer *pHpetTimer,
325 uint64_t u64Now)
326{
327
328 if (hpet32bitTimer(pHpetTimer))
329 {
330 uint32_t u32Diff;
331
332 u32Diff = (uint32_t)pHpetTimer->u64Cmp - (uint32_t)u64Now;
333 u32Diff = ((int32_t)u32Diff > 0) ? u32Diff : (uint32_t)0;
334 return (uint64_t)u32Diff;
335 }
336 else
337 {
338 uint64_t u64Diff;
339
340 u64Diff = pHpetTimer->u64Cmp - u64Now;
341 u64Diff = ((int64_t)u64Diff > 0) ? u64Diff : (uint64_t)0;
342 return u64Diff;
343 }
344}
345
346
347static void hpetAdjustComparator(HpetTimer *pHpetTimer, uint64_t u64Now)
348{
349 uint64_t u64Period = pHpetTimer->u64Period;
350 if ( (pHpetTimer->u64Config & HPET_TN_PERIODIC)
351 && u64Period != 0)
352 {
353 /* While loop is suboptimal */
354 if (hpet32bitTimer(pHpetTimer))
355 {
356 while (hpetTimeAfter32(u64Now, pHpetTimer->u64Cmp))
357 pHpetTimer->u64Cmp = (uint32_t)(pHpetTimer->u64Cmp + u64Period);
358 }
359 else
360 {
361 while (hpetTimeAfter64(u64Now, pHpetTimer->u64Cmp))
362 pHpetTimer->u64Cmp += u64Period;
363 }
364 }
365}
366
367
368/**
369 * Sets the frequency hint if it's a periodic timer.
370 *
371 * @param pThis The HPET state.
372 * @param pHpetTimer The timer.
373 */
374DECLINLINE(void) hpetTimerSetFrequencyHint(HpetState *pThis, HpetTimer *pHpetTimer)
375{
376 if (pHpetTimer->u64Config & HPET_TN_PERIODIC)
377 {
378 uint64_t const u64Period = pHpetTimer->u64Period;
379 uint32_t const u32Freq = pThis->u32Period;
380 if (u64Period > 0 && u64Period < u32Freq)
381 TMTimerSetFrequencyHint(pHpetTimer->CTX_SUFF(pTimer), u32Freq / (uint32_t)u64Period);
382 }
383}
384
385
386static void hpetProgramTimer(HpetTimer *pHpetTimer)
387{
388 /* no wrapping on new timers */
389 pHpetTimer->u8Wrap = 0;
390
391 uint64_t u64Ticks = hpetGetTicks(pHpetTimer->CTX_SUFF(pHpet));
392 hpetAdjustComparator(pHpetTimer, u64Ticks);
393
394 uint64_t u64Diff = hpetComputeDiff(pHpetTimer, u64Ticks);
395
396 /*
397 * HPET spec says in one-shot 32-bit mode, generate an interrupt when
398 * counter wraps in addition to an interrupt with comparator match.
399 */
400 if ( hpet32bitTimer(pHpetTimer)
401 && !(pHpetTimer->u64Config & HPET_TN_PERIODIC))
402 {
403 uint32_t u32TillWrap = 0xffffffff - (uint32_t)u64Ticks + 1;
404 if (u32TillWrap < (uint32_t)u64Diff)
405 {
406 Log(("wrap on timer %d: till=%u ticks=%lld diff64=%lld\n",
407 pHpetTimer->idxTimer, u32TillWrap, u64Ticks, u64Diff));
408 u64Diff = u32TillWrap;
409 pHpetTimer->u8Wrap = 1;
410 }
411 }
412
413 /*
414 * HACK ALERT! Avoid killing VM with interrupts.
415 */
416#if 1 /** @todo: HACK, rethink, may have negative impact on the guest */
417 if (u64Diff == 0)
418 u64Diff = 100000; /* 1 millisecond */
419#endif
420
421 Log4(("HPET: next IRQ in %lld ticks (%lld ns)\n", u64Diff, hpetTicksToNs(pHpetTimer->CTX_SUFF(pHpet), u64Diff)));
422 TMTimerSetNano(pHpetTimer->CTX_SUFF(pTimer), hpetTicksToNs(pHpetTimer->CTX_SUFF(pHpet), u64Diff));
423 hpetTimerSetFrequencyHint(pHpetTimer->CTX_SUFF(pHpet), pHpetTimer);
424}
425
426
427/* -=-=-=-=-=- Timer register accesses -=-=-=-=-=- */
428
429
430/**
431 * Reads a HPET timer register.
432 *
433 * @returns VBox strict status code.
434 * @param pThis The HPET instance.
435 * @param iTimerNo The timer index.
436 * @param iTimerReg The index of the timer register to read.
437 * @param pu32Value Where to return the register value.
438 *
439 * @remarks ASSUMES the caller does holds the HPET lock.
440 */
441static int hpetTimerRegRead32(HpetState const *pThis, uint32_t iTimerNo, uint32_t iTimerReg, uint32_t *pu32Value)
442{
443 Assert(PDMCritSectIsOwner(&pThis->csLock));
444
445 if (iTimerNo >= HPET_CAP_GET_TIMERS(pThis->u32Capabilities))
446 {
447 static unsigned s_cOccurences = 0;
448 if (s_cOccurences++ < 10)
449 LogRel(("HPET: using timer above configured range: %d\n", iTimerNo));
450 *pu32Value = 0;
451 return VINF_SUCCESS;
452 }
453
454 HpetTimer const *pHpetTimer = &pThis->aTimers[iTimerNo];
455 uint32_t u32Value;
456 switch (iTimerReg)
457 {
458 case HPET_TN_CFG:
459 u32Value = (uint32_t)pHpetTimer->u64Config;
460 Log(("read HPET_TN_CFG on %d: %#x\n", iTimerNo, u32Value));
461 break;
462
463 case HPET_TN_CFG + 4:
464 u32Value = (uint32_t)(pHpetTimer->u64Config >> 32);
465 Log(("read HPET_TN_CFG+4 on %d: %#x\n", iTimerNo, u32Value));
466 break;
467
468 case HPET_TN_CMP:
469 u32Value = (uint32_t)pHpetTimer->u64Cmp;
470 Log(("read HPET_TN_CMP on %d: %#x (%#llx)\n", pHpetTimer->idxTimer, u32Value, pHpetTimer->u64Cmp));
471 break;
472
473 case HPET_TN_CMP + 4:
474 u32Value = (uint32_t)(pHpetTimer->u64Cmp >> 32);
475 Log(("read HPET_TN_CMP+4 on %d: %#x (%#llx)\n", pHpetTimer->idxTimer, u32Value, pHpetTimer->u64Cmp));
476 break;
477
478 case HPET_TN_ROUTE:
479 u32Value = (uint32_t)(pHpetTimer->u64Fsb >> 32); /** @todo Looks wrong, but since it's not supported, who cares. */
480 Log(("read HPET_TN_ROUTE on %d: %#x\n", iTimerNo, u32Value));
481 break;
482
483 default:
484 {
485 static unsigned s_cOccurences = 0;
486 if (s_cOccurences++ < 10)
487 LogRel(("invalid HPET register read %d on %d\n", iTimerReg, pHpetTimer->idxTimer));
488 u32Value = 0;
489 break;
490 }
491 }
492 *pu32Value = u32Value;
493 return VINF_SUCCESS;
494}
495
496
497/**
498 * 32-bit write to a HPET timer register.
499 *
500 * @returns Strict VBox status code.
501 *
502 * @param pThis The HPET state.
503 * @param idxReg The register being written to.
504 * @param u32NewValue The value being written.
505 *
506 * @remarks The caller should not hold the device lock, unless it also holds
507 * the TM lock.
508 */
509static int hpetTimerRegWrite32(HpetState *pThis, uint32_t iTimerNo, uint32_t iTimerReg, uint32_t u32NewValue)
510{
511 Assert(!PDMCritSectIsOwner(&pThis->csLock) || TMTimerIsLockOwner(pThis->aTimers[0].CTX_SUFF(pTimer)));
512
513 if (iTimerNo >= HPET_CAP_GET_TIMERS(pThis->u32Capabilities))
514 {
515 static unsigned s_cOccurences = 0;
516 if (s_cOccurences++ < 10)
517 LogRel(("HPET: using timer above configured range: %d\n", iTimerNo));
518 return VINF_SUCCESS;
519 }
520 HpetTimer *pHpetTimer = &pThis->aTimers[iTimerNo];
521
522 switch (iTimerReg)
523 {
524 case HPET_TN_CFG:
525 {
526 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
527 Log(("write HPET_TN_CFG: %d: %x\n", iTimerNo, u32NewValue));
528 uint64_t const iOldValue = (uint32_t)pHpetTimer->u64Config;
529
530 uint64_t u64Mask = HPET_TN_CFG_WRITE_MASK;
531 if (pHpetTimer->u64Config & HPET_TN_PERIODIC_CAP)
532 u64Mask |= HPET_TN_PERIODIC;
533
534 if (pHpetTimer->u64Config & HPET_TN_SIZE_CAP)
535 u64Mask |= HPET_TN_32BIT;
536 else
537 u32NewValue &= ~HPET_TN_32BIT;
538
539 if (u32NewValue & HPET_TN_32BIT)
540 {
541 Log(("setting timer %d to 32-bit mode\n", iTimerNo));
542 pHpetTimer->u64Cmp = (uint32_t)pHpetTimer->u64Cmp;
543 pHpetTimer->u64Period = (uint32_t)pHpetTimer->u64Period;
544 }
545 if ((u32NewValue & HPET_TN_INT_TYPE) == HPET_TIMER_TYPE_LEVEL)
546 {
547 static unsigned s_cOccurences = 0;
548 if (s_cOccurences++ < 10)
549 LogRel(("level-triggered config not yet supported\n"));
550 AssertFailed();
551 }
552
553 /* We only care about lower 32-bits so far */
554 pHpetTimer->u64Config = hpetUpdateMasked(u32NewValue, iOldValue, u64Mask);
555 DEVHPET_UNLOCK(pThis);
556 break;
557 }
558
559 case HPET_TN_CFG + 4: /* Interrupt capabilities */
560 {
561 Log(("write HPET_TN_CFG + 4, useless\n"));
562 break;
563 }
564
565 case HPET_TN_CMP: /* lower bits of comparator register */
566 {
567 DEVHPET_LOCK_BOTH_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
568 Log(("write HPET_TN_CMP on %d: %#x\n", iTimerNo, u32NewValue));
569
570 if (pHpetTimer->u64Config & HPET_TN_PERIODIC)
571 {
572 u32NewValue &= hpetInvalidValue(pHpetTimer) >> 1; /** @todo check this in the docs and add a not why? */
573 pHpetTimer->u64Period = RT_MAKE_U64(u32NewValue, pHpetTimer->u64Period);
574 }
575 pHpetTimer->u64Cmp = RT_MAKE_U64(u32NewValue, pHpetTimer->u64Cmp);
576 pHpetTimer->u64Config &= ~HPET_TN_SETVAL;
577 Log2(("after HPET_TN_CMP cmp=%#llx per=%#llx\n", pHpetTimer->u64Cmp, pHpetTimer->u64Period));
578
579 if (pThis->u64HpetConfig & HPET_CFG_ENABLE)
580 hpetProgramTimer(pHpetTimer);
581 DEVHPET_UNLOCK_BOTH(pThis);
582 break;
583 }
584
585 case HPET_TN_CMP + 4: /* upper bits of comparator register */
586 {
587 DEVHPET_LOCK_BOTH_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
588 Log(("write HPET_TN_CMP + 4 on %d: %#x\n", iTimerNo, u32NewValue));
589 if (!hpet32bitTimer(pHpetTimer))
590 {
591 if (pHpetTimer->u64Config & HPET_TN_PERIODIC)
592 pHpetTimer->u64Period = RT_MAKE_U64(pHpetTimer->u64Period, u32NewValue);
593 pHpetTimer->u64Cmp = RT_MAKE_U64(pHpetTimer->u64Cmp, u32NewValue);
594
595 Log2(("after HPET_TN_CMP+4 cmp=%llx per=%llx tmr=%d\n", pHpetTimer->u64Cmp, pHpetTimer->u64Period, iTimerNo));
596
597 pHpetTimer->u64Config &= ~HPET_TN_SETVAL;
598
599 if (pThis->u64HpetConfig & HPET_CFG_ENABLE)
600 hpetProgramTimer(pHpetTimer);
601 }
602 DEVHPET_UNLOCK_BOTH(pThis);
603 break;
604 }
605
606 case HPET_TN_ROUTE:
607 {
608 Log(("write HPET_TN_ROUTE\n"));
609 break;
610 }
611
612 case HPET_TN_ROUTE + 4:
613 {
614 Log(("write HPET_TN_ROUTE + 4\n"));
615 break;
616 }
617
618 default:
619 {
620 static unsigned s_cOccurences = 0;
621 if (s_cOccurences++ < 10)
622 LogRel(("invalid timer register write: %d\n", iTimerReg));
623 break;
624 }
625 }
626
627 return VINF_SUCCESS;
628}
629
630
631/* -=-=-=-=-=- Non-timer register accesses -=-=-=-=-=- */
632
633
634/**
635 * Read a 32-bit HPET register.
636 *
637 * @returns Strict VBox status code.
638 * @param pThis The HPET state.
639 * @param idxReg The register to read.
640 * @param pu32Value Where to return the register value.
641 *
642 * @remarks The caller must not own the device lock if HPET_COUNTER is read.
643 */
644static int hpetConfigRegRead32(HpetState *pThis, uint32_t idxReg, uint32_t *pu32Value)
645{
646 Assert(!PDMCritSectIsOwner(&pThis->csLock) || (idxReg != HPET_COUNTER && idxReg != HPET_COUNTER + 4));
647
648 uint32_t u32Value;
649 switch (idxReg)
650 {
651 case HPET_ID:
652 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
653 u32Value = pThis->u32Capabilities;
654 DEVHPET_UNLOCK(pThis);
655 Log(("read HPET_ID: %#x\n", u32Value));
656 break;
657
658 case HPET_PERIOD:
659 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
660 u32Value = pThis->u32Period;
661 DEVHPET_UNLOCK(pThis);
662 Log(("read HPET_PERIOD: %#x\n", u32Value));
663 break;
664
665 case HPET_CFG:
666 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
667 u32Value = (uint32_t)pThis->u64HpetConfig;
668 DEVHPET_UNLOCK(pThis);
669 Log(("read HPET_CFG: %#x\n", u32Value));
670 break;
671
672 case HPET_CFG + 4:
673 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
674 u32Value = (uint32_t)(pThis->u64HpetConfig >> 32);
675 DEVHPET_UNLOCK(pThis);
676 Log(("read of HPET_CFG + 4: %#x\n", u32Value));
677 break;
678
679 case HPET_COUNTER:
680 case HPET_COUNTER + 4:
681 {
682 DEVHPET_LOCK_BOTH_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
683
684 uint64_t u64Ticks;
685 if (pThis->u64HpetConfig & HPET_CFG_ENABLE)
686 u64Ticks = hpetGetTicks(pThis);
687 else
688 u64Ticks = pThis->u64HpetCounter;
689
690 DEVHPET_UNLOCK_BOTH(pThis);
691
692 /** @todo is it correct? */
693 u32Value = (idxReg == HPET_COUNTER) ? (uint32_t)u64Ticks : (uint32_t)(u64Ticks >> 32);
694 Log(("read HPET_COUNTER: %s part value %x (%#llx)\n",
695 (idxReg == HPET_COUNTER) ? "low" : "high", u32Value, u64Ticks));
696 break;
697 }
698
699 case HPET_STATUS:
700 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
701 u32Value = (uint32_t)pThis->u64Isr;
702 DEVHPET_UNLOCK(pThis);
703 Log(("read HPET_STATUS: %#x\n", u32Value));
704 break;
705
706 default:
707 Log(("invalid HPET register read: %x\n", idxReg));
708 u32Value = 0;
709 break;
710 }
711
712 *pu32Value = u32Value;
713 return VINF_SUCCESS;
714}
715
716
717/**
718 * 32-bit write to a config register.
719 *
720 * @returns Strict VBox status code.
721 *
722 * @param pThis The HPET state.
723 * @param idxReg The register being written to.
724 * @param u32NewValue The value being written.
725 *
726 * @remarks The caller should not hold the device lock, unless it also holds
727 * the TM lock.
728 */
729static int hpetConfigRegWrite32(HpetState *pThis, uint32_t idxReg, uint32_t u32NewValue)
730{
731 Assert(!PDMCritSectIsOwner(&pThis->csLock) || TMTimerIsLockOwner(pThis->aTimers[0].CTX_SUFF(pTimer)));
732
733 int rc = VINF_SUCCESS;
734 switch (idxReg)
735 {
736 case HPET_ID:
737 case HPET_ID + 4:
738 {
739 Log(("write HPET_ID, useless\n"));
740 break;
741 }
742
743 case HPET_CFG:
744 {
745 DEVHPET_LOCK_BOTH_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
746 uint32_t const iOldValue = (uint32_t)(pThis->u64HpetConfig);
747 Log(("write HPET_CFG: %x (old %x)\n", u32NewValue, iOldValue));
748
749 /*
750 * This check must be here, before actual update, as hpetLegacyMode
751 * may request retry in R3 - so we must keep state intact.
752 */
753 if ( ((iOldValue ^ u32NewValue) & HPET_CFG_LEGACY)
754 && pThis->pHpetHlpR3 != NIL_RTR3PTR)
755 {
756#ifdef IN_RING3
757 rc = pThis->pHpetHlpR3->pfnSetLegacyMode(pThis->pDevInsR3, RT_BOOL(u32NewValue & HPET_CFG_LEGACY));
758 if (rc != VINF_SUCCESS)
759#else
760 rc = VINF_IOM_HC_MMIO_WRITE;
761#endif
762 {
763 DEVHPET_UNLOCK_BOTH(pThis);
764 break;
765 }
766 }
767
768 pThis->u64HpetConfig = hpetUpdateMasked(u32NewValue, iOldValue, HPET_CFG_WRITE_MASK);
769
770 uint32_t const cTimers = HPET_CAP_GET_TIMERS(pThis->u32Capabilities);
771 if (hpetBitJustSet(iOldValue, u32NewValue, HPET_CFG_ENABLE))
772 {
773/** @todo Only get the time stamp once when reprogramming? */
774 /* Enable main counter and interrupt generation. */
775 pThis->u64HpetOffset = hpetTicksToNs(pThis, pThis->u64HpetCounter)
776 - TMTimerGet(pThis->aTimers[0].CTX_SUFF(pTimer));
777 for (uint32_t i = 0; i < cTimers; i++)
778 if (pThis->aTimers[i].u64Cmp != hpetInvalidValue(&pThis->aTimers[i]))
779 hpetProgramTimer(&pThis->aTimers[i]);
780 }
781 else if (hpetBitJustCleared(iOldValue, u32NewValue, HPET_CFG_ENABLE))
782 {
783 /* Halt main counter and disable interrupt generation. */
784 pThis->u64HpetCounter = hpetGetTicks(pThis);
785 for (uint32_t i = 0; i < cTimers; i++)
786 TMTimerStop(pThis->aTimers[i].CTX_SUFF(pTimer));
787 }
788
789 DEVHPET_UNLOCK_BOTH(pThis);
790 break;
791 }
792
793 case HPET_CFG + 4:
794 {
795 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
796 pThis->u64HpetConfig = hpetUpdateMasked((uint64_t)u32NewValue << 32,
797 pThis->u64HpetConfig,
798 UINT64_C(0xffffffff00000000));
799 Log(("write HPET_CFG + 4: %x -> %#llx\n", u32NewValue, pThis->u64HpetConfig));
800 DEVHPET_UNLOCK(pThis);
801 break;
802 }
803
804 case HPET_STATUS:
805 {
806 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
807 /* Clear ISR for all set bits in u32NewValue, see p. 14 of the HPET spec. */
808 pThis->u64Isr &= ~((uint64_t)u32NewValue);
809 Log(("write HPET_STATUS: %x -> ISR=%#llx\n", u32NewValue, pThis->u64Isr));
810 DEVHPET_UNLOCK(pThis);
811 break;
812 }
813
814 case HPET_STATUS + 4:
815 {
816 Log(("write HPET_STATUS + 4: %x\n", u32NewValue));
817 if (u32NewValue != 0)
818 {
819 static unsigned s_cOccurrences = 0;
820 if (s_cOccurrences++ < 10)
821 LogRel(("Writing HPET_STATUS + 4 with non-zero, ignored\n"));
822 }
823 break;
824 }
825
826 case HPET_COUNTER:
827 {
828 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
829 pThis->u64HpetCounter = RT_MAKE_U64(u32NewValue, pThis->u64HpetCounter);
830 Log(("write HPET_COUNTER: %#x -> %llx\n", u32NewValue, pThis->u64HpetCounter));
831 DEVHPET_UNLOCK(pThis);
832 break;
833 }
834
835 case HPET_COUNTER + 4:
836 {
837 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
838 pThis->u64HpetCounter = RT_MAKE_U64(pThis->u64HpetCounter, u32NewValue);
839 Log(("write HPET_COUNTER + 4: %#x -> %llx\n", u32NewValue, pThis->u64HpetCounter));
840 DEVHPET_UNLOCK(pThis);
841 break;
842 }
843
844 default:
845 {
846 static unsigned s_cOccurences = 0;
847 if (s_cOccurences++ < 10)
848 LogRel(("invalid HPET config write: %x\n", idxReg));
849 break;
850 }
851 }
852
853 return rc;
854}
855
856
857/* -=-=-=-=-=- MMIO callbacks -=-=-=-=-=- */
858
859
860/**
861 * @callback_method_impl{FNIOMMMIOREAD}
862 */
863PDMBOTHCBDECL(int) hpetMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
864{
865 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState*);
866 uint32_t const idxReg = (uint32_t)(GCPhysAddr - HPET_BASE);
867
868 LogFlow(("hpetMMIORead (%d): %llx (%x)\n", cb, (uint64_t)GCPhysAddr, idxReg));
869
870 int rc = VINF_SUCCESS;
871 switch (cb)
872 {
873 case 4:
874 if (idxReg >= 0x100 && idxReg < 0x400)
875 {
876 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
877 rc = hpetTimerRegRead32(pThis,
878 (idxReg - 0x100) / 0x20,
879 (idxReg - 0x100) % 0x20,
880 (uint32_t *)pv);
881 DEVHPET_UNLOCK(pThis);
882 }
883 else
884 rc = hpetConfigRegRead32(pThis, idxReg, (uint32_t *)pv);
885 break;
886
887 case 8:
888 {
889 /* Unaligned accesses not allowed */
890 if (RT_UNLIKELY(idxReg % 8 != 0))
891 {
892 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "idxReg=%#x cb=8\n", idxReg);
893 break;
894 }
895
896 /* Split the access except for timing sensitive registers. The
897 others assume the protection of the lock. */
898 PRTUINT64U pValue = (PRTUINT64U)pv;
899 if (idxReg == HPET_COUNTER)
900 {
901 /* When reading HPET counter we must read it in a single read,
902 to avoid unexpected time jumps on 32-bit overflow. */
903 DEVHPET_LOCK_BOTH_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
904 if (pThis->u64HpetConfig & HPET_CFG_ENABLE)
905 pValue->u = hpetGetTicks(pThis);
906 else
907 pValue->u = pThis->u64HpetCounter;
908 DEVHPET_UNLOCK_BOTH(pThis);
909 }
910 else
911 {
912 DEVHPET_LOCK_RETURN(pThis, VINF_IOM_HC_MMIO_READ);
913 if (idxReg >= 0x100 && idxReg < 0x400)
914 {
915 uint32_t iTimer = (idxReg - 0x100) / 0x20;
916 uint32_t iTimerReg = (idxReg - 0x100) % 0x20;
917 rc = hpetTimerRegRead32(pThis, iTimer, iTimerReg, &pValue->s.Lo);
918 if (rc == VINF_SUCCESS)
919 rc = hpetTimerRegRead32(pThis, iTimer, iTimerReg + 4, &pValue->s.Hi);
920 }
921 else
922 {
923 /* for most 8-byte accesses we just split them, happens under lock anyway. */
924 rc = hpetConfigRegRead32(pThis, idxReg, &pValue->s.Lo);
925 if (rc == VINF_SUCCESS)
926 rc = hpetConfigRegRead32(pThis, idxReg + 4, &pValue->s.Hi);
927 }
928 DEVHPET_UNLOCK(pThis);
929 }
930 break;
931 }
932
933 case 1:
934 case 2:
935 Log(("Narrow read: %d\n", cb));
936 rc = VINF_SUCCESS;
937 break;
938
939 default:
940 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
941 rc = VINF_SUCCESS;
942 }
943
944 return rc;
945}
946
947
948/**
949 * @callback_method_impl{FNIOMMMIOWRITE}
950 */
951PDMBOTHCBDECL(int) hpetMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
952{
953 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState*);
954 uint32_t idxReg = (uint32_t)(GCPhysAddr - HPET_BASE);
955 LogFlow(("hpetMMIOWrite: cb=%u reg=%03x (%RGp) val=%llx\n",
956 cb, idxReg, GCPhysAddr, cb == 4 ? *(uint32_t *)pv : cb == 8 ? *(uint64_t *)pv : 0xdeadbeef));
957
958 int rc;
959 switch (cb)
960 {
961 case 4:
962 if (idxReg >= 0x100 && idxReg < 0x400)
963 rc = hpetTimerRegWrite32(pThis,
964 (idxReg - 0x100) / 0x20,
965 (idxReg - 0x100) % 0x20,
966 *(uint32_t const *)pv);
967 else
968 rc = hpetConfigRegWrite32(pThis, idxReg, *(uint32_t const *)pv);
969 break;
970
971 case 8:
972 {
973 /* Unaligned accesses are not allowed. */
974 if (RT_UNLIKELY(idxReg % 8 != 0))
975 {
976 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "idxReg=%#x cb=8\n", idxReg);
977 break;
978 }
979
980 /* Split the access and rely on the locking to prevent trouble. */
981 DEVHPET_LOCK_BOTH_RETURN(pThis, VINF_IOM_HC_MMIO_WRITE);
982 RTUINT64U uValue;
983 uValue.u = *(uint64_t const *)pv;
984 if (idxReg >= 0x100 && idxReg < 0x400)
985 {
986 uint32_t iTimer = (idxReg - 0x100) / 0x20;
987 uint32_t iTimerReg = (idxReg - 0x100) % 0x20;
988/** @todo Consider handling iTimerReg == HPET_TN_CMP specially here */
989 rc = hpetTimerRegWrite32(pThis, iTimer, iTimerReg, uValue.s.Lo);
990 if (RT_LIKELY(rc == VINF_SUCCESS))
991 rc = hpetTimerRegWrite32(pThis, iTimer, iTimerReg + 4, uValue.s.Hi);
992 }
993 else
994 {
995 rc = hpetConfigRegWrite32(pThis, idxReg, uValue.s.Lo);
996 if (RT_LIKELY(rc == VINF_SUCCESS))
997 rc = hpetConfigRegWrite32(pThis, idxReg + 4, uValue.s.Hi);
998 }
999 DEVHPET_UNLOCK_BOTH(pThis);
1000 break;
1001 }
1002
1003 case 1:
1004 case 2:
1005 Log(("Narrow write: %d\n", cb));
1006 rc = VINF_SUCCESS;
1007 break;
1008
1009 default:
1010 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
1011 rc = VERR_INTERNAL_ERROR;
1012 break;
1013 }
1014
1015 return rc;
1016}
1017
1018#ifdef IN_RING3
1019
1020/* -=-=-=-=-=- Timer Callback Processing -=-=-=-=-=- */
1021
1022/**
1023 * Gets the IRQ of an HPET timer.
1024 *
1025 * @returns IRQ number.
1026 * @param pHpetTimer The HPET timer.
1027 */
1028static uint32_t hpetTimerCbGetIrq(struct HpetTimer const *pHpetTimer)
1029{
1030 /*
1031 * Per spec, in legacy mode HPET timers wired as:
1032 * timer 0: IRQ0 for PIC and IRQ2 for APIC
1033 * timer 1: IRQ8 for both PIC and APIC
1034 *
1035 * ISA IRQ delivery logic will take care of correct delivery
1036 * to the different ICs.
1037 */
1038 if ( (pHpetTimer->idxTimer <= 1)
1039 && (pHpetTimer->CTX_SUFF(pHpet)->u64HpetConfig & HPET_CFG_LEGACY))
1040 return (pHpetTimer->idxTimer == 0) ? 0 : 8;
1041
1042 return (pHpetTimer->u64Config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
1043}
1044
1045
1046/**
1047 * Used by hpetTimerCb to update the IRQ status.
1048 *
1049 * @param pThis The HPET device state.
1050 * @param pHpetTimer The HPET timer.
1051 */
1052static void hpetTimerCbUpdateIrq(HpetState *pThis, struct HpetTimer *pHpetTimer)
1053{
1054 /** @todo: is it correct? */
1055 if ( !!(pHpetTimer->u64Config & HPET_TN_ENABLE)
1056 && !!(pThis->u64HpetConfig & HPET_CFG_ENABLE))
1057 {
1058 uint32_t irq = hpetTimerCbGetIrq(pHpetTimer);
1059 Log4(("HPET: raising IRQ %d\n", irq));
1060
1061 /* ISR bits are only set in level-triggered mode. */
1062 if ((pHpetTimer->u64Config & HPET_TN_INT_TYPE) == HPET_TIMER_TYPE_LEVEL)
1063 pThis->u64Isr |= (uint64_t)(1 << pHpetTimer->idxTimer);
1064
1065 /* We trigger flip/flop in edge-triggered mode and do nothing in
1066 level-triggered mode yet. */
1067 if ((pHpetTimer->u64Config & HPET_TN_INT_TYPE) == HPET_TIMER_TYPE_EDGE)
1068 pThis->pHpetHlpR3->pfnSetIrq(pThis->CTX_SUFF(pDevIns), irq, PDM_IRQ_LEVEL_FLIP_FLOP);
1069 else
1070 AssertFailed();
1071 /** @todo: implement IRQs in level-triggered mode */
1072 }
1073}
1074
1075/**
1076 * Device timer callback function.
1077 *
1078 * @param pDevIns Device instance of the device which registered the timer.
1079 * @param pTimer The timer handle.
1080 * @param pvUser Pointer to the HPET timer state.
1081 */
1082static DECLCALLBACK(void) hpetTimerCb(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1083{
1084 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState *);
1085 HpetTimer *pHpetTimer = (HpetTimer *)pvUser;
1086 uint64_t u64Period = pHpetTimer->u64Period;
1087 uint64_t u64CurTick = hpetGetTicks(pThis);
1088 uint64_t u64Diff;
1089
1090 if ((pHpetTimer->u64Config & HPET_TN_PERIODIC) && (u64Period != 0))
1091 {
1092 hpetAdjustComparator(pHpetTimer, u64CurTick);
1093
1094 u64Diff = hpetComputeDiff(pHpetTimer, u64CurTick);
1095
1096 Log4(("HPET: periodical: next in %llu\n", hpetTicksToNs(pThis, u64Diff)));
1097 TMTimerSetNano(pTimer, hpetTicksToNs(pThis, u64Diff));
1098 }
1099 else if ( hpet32bitTimer(pHpetTimer)
1100 && !(pHpetTimer->u64Config & HPET_TN_PERIODIC))
1101 {
1102 if (pHpetTimer->u8Wrap)
1103 {
1104 u64Diff = hpetComputeDiff(pHpetTimer, u64CurTick);
1105 TMTimerSetNano(pTimer, hpetTicksToNs(pThis, u64Diff));
1106 pHpetTimer->u8Wrap = 0;
1107 }
1108 }
1109
1110 /* Should it really be under lock, does it really matter? */
1111 hpetTimerCbUpdateIrq(pThis, pHpetTimer);
1112}
1113
1114
1115/* -=-=-=-=-=- DBGF Info Handlers -=-=-=-=-=- */
1116
1117
1118/**
1119 * @callback_method_impl{FNDBGFHANDLERDEV}
1120 */
1121static DECLCALLBACK(void) hpetInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1122{
1123 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState *);
1124
1125 pHlp->pfnPrintf(pHlp,
1126 "HPET status:\n"
1127 " config=%016RX64 isr=%016RX64\n"
1128 " offset=%016RX64 counter=%016RX64 frequency=%08x\n"
1129 " legacy-mode=%s timer-count=%u\n",
1130 pThis->u64HpetConfig, pThis->u64Isr,
1131 pThis->u64HpetOffset, pThis->u64HpetCounter, pThis->u32Period,
1132 !!(pThis->u64HpetConfig & HPET_CFG_LEGACY) ? "on " : "off",
1133 HPET_CAP_GET_TIMERS(pThis->u32Capabilities));
1134 pHlp->pfnPrintf(pHlp,
1135 "Timers:\n");
1136 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aTimers); i++)
1137 {
1138 pHlp->pfnPrintf(pHlp, " %d: comparator=%016RX64 period(hidden)=%016RX64 cfg=%016RX64\n",
1139 pThis->aTimers[i].idxTimer,
1140 pThis->aTimers[i].u64Cmp,
1141 pThis->aTimers[i].u64Period,
1142 pThis->aTimers[i].u64Config);
1143 }
1144}
1145
1146
1147/* -=-=-=-=-=- Saved State -=-=-=-=-=- */
1148
1149
1150/**
1151 * @callback_method_impl{FNSSMDEVLIVEEXEC}
1152 */
1153static DECLCALLBACK(int) hpetLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
1154{
1155 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState *);
1156
1157 SSMR3PutU8(pSSM, HPET_CAP_GET_TIMERS(pThis->u32Capabilities));
1158
1159 return VINF_SSM_DONT_CALL_AGAIN;
1160}
1161
1162
1163/**
1164 * @callback_method_impl{FNSSMDEVSAVEEXEC}
1165 */
1166static DECLCALLBACK(int) hpetSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1167{
1168 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState *);
1169
1170 /*
1171 * The config.
1172 */
1173 hpetLiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
1174
1175 /*
1176 * The state.
1177 */
1178 uint32_t const cTimers = HPET_CAP_GET_TIMERS(pThis->u32Capabilities);
1179 for (uint32_t iTimer = 0; iTimer < cTimers; iTimer++)
1180 {
1181 HpetTimer *pHpetTimer = &pThis->aTimers[iTimer];
1182 TMR3TimerSave(pHpetTimer->pTimerR3, pSSM);
1183 SSMR3PutU8(pSSM, pHpetTimer->u8Wrap);
1184 SSMR3PutU64(pSSM, pHpetTimer->u64Config);
1185 SSMR3PutU64(pSSM, pHpetTimer->u64Cmp);
1186 SSMR3PutU64(pSSM, pHpetTimer->u64Fsb);
1187 SSMR3PutU64(pSSM, pHpetTimer->u64Period);
1188 }
1189
1190 SSMR3PutU64(pSSM, pThis->u64HpetOffset);
1191 uint64_t u64CapPer = RT_MAKE_U64(pThis->u32Capabilities, pThis->u32Period);
1192 SSMR3PutU64(pSSM, u64CapPer);
1193 SSMR3PutU64(pSSM, pThis->u64HpetConfig);
1194 SSMR3PutU64(pSSM, pThis->u64Isr);
1195 return SSMR3PutU64(pSSM, pThis->u64HpetCounter);
1196}
1197
1198
1199/**
1200 * @callback_method_impl{FNSSMDEVLOADEXEC}
1201 */
1202static DECLCALLBACK(int) hpetLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1203{
1204 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState *);
1205
1206 /*
1207 * Version checks.
1208 */
1209 if (uVersion == HPET_SAVED_STATE_VERSION_EMPTY)
1210 return VINF_SUCCESS;
1211 if (uVersion != HPET_SAVED_STATE_VERSION)
1212 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1213
1214 /*
1215 * The config.
1216 */
1217 uint8_t cTimers;
1218 int rc = SSMR3GetU8(pSSM, &cTimers);
1219 AssertRCReturn(rc, rc);
1220 if (cTimers > RT_ELEMENTS(pThis->aTimers))
1221 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - too many timers: saved=%#x config=%#x"),
1222 cTimers, RT_ELEMENTS(pThis->aTimers));
1223
1224 if (uPass != SSM_PASS_FINAL)
1225 return VINF_SUCCESS;
1226
1227 /*
1228 * The state.
1229 */
1230 for (uint32_t iTimer = 0; iTimer < cTimers; iTimer++)
1231 {
1232 HpetTimer *pHpetTimer = &pThis->aTimers[iTimer];
1233 TMR3TimerLoad(pHpetTimer->pTimerR3, pSSM);
1234 SSMR3GetU8(pSSM, &pHpetTimer->u8Wrap);
1235 SSMR3GetU64(pSSM, &pHpetTimer->u64Config);
1236 SSMR3GetU64(pSSM, &pHpetTimer->u64Cmp);
1237 SSMR3GetU64(pSSM, &pHpetTimer->u64Fsb);
1238 SSMR3GetU64(pSSM, &pHpetTimer->u64Period);
1239 }
1240
1241 SSMR3GetU64(pSSM, &pThis->u64HpetOffset);
1242 uint64_t u64CapPer;
1243 SSMR3GetU64(pSSM, &u64CapPer);
1244 SSMR3GetU64(pSSM, &pThis->u64HpetConfig);
1245 SSMR3GetU64(pSSM, &pThis->u64Isr);
1246 rc = SSMR3GetU64(pSSM, &pThis->u64HpetCounter);
1247 if (RT_FAILURE(rc))
1248 return rc;
1249 if (HPET_CAP_GET_TIMERS(RT_LO_U32(u64CapPer)) != cTimers)
1250 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Capabilities does not match timer count: cTimers=%#x caps=%#x"),
1251 cTimers, (unsigned)HPET_CAP_GET_TIMERS(u64CapPer));
1252 pThis->u32Capabilities = RT_LO_U32(u64CapPer);
1253 pThis->u32Period = RT_HI_U32(u64CapPer);
1254
1255 /*
1256 * Set the timer frequency hints.
1257 */
1258 PDMCritSectEnter(&pThis->csLock, VERR_IGNORED);
1259 for (uint32_t iTimer = 0; iTimer < cTimers; iTimer++)
1260 {
1261 HpetTimer *pHpetTimer = &pThis->aTimers[iTimer];
1262 if (TMTimerIsActive(pHpetTimer->CTX_SUFF(pTimer)))
1263 hpetTimerSetFrequencyHint(pThis, pHpetTimer);
1264 }
1265 PDMCritSectLeave(&pThis->csLock);
1266 return VINF_SUCCESS;
1267}
1268
1269
1270/* -=-=-=-=-=- PDMDEVREG -=-=-=-=-=- */
1271
1272
1273/**
1274 * @interface_method_impl{PDMDEVREG,pfnRelocate}
1275 */
1276static DECLCALLBACK(void) hpetRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1277{
1278 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState *);
1279 LogFlow(("hpetRelocate:\n"));
1280
1281 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1282 pThis->pHpetHlpRC = pThis->pHpetHlpR3->pfnGetRCHelpers(pDevIns);
1283
1284 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aTimers); i++)
1285 {
1286 HpetTimer *pTm = &pThis->aTimers[i];
1287 if (pTm->pTimerR3)
1288 pTm->pTimerRC = TMTimerRCPtr(pTm->pTimerR3);
1289 pTm->pHpetRC = PDMINS_2_DATA_RCPTR(pDevIns);
1290 }
1291}
1292
1293
1294/**
1295 * @interface_method_impl{PDMDEVREG,pfnReset}
1296 */
1297static DECLCALLBACK(void) hpetReset(PPDMDEVINS pDevIns)
1298{
1299 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState *);
1300 LogFlow(("hpetReset:\n"));
1301
1302 /*
1303 * The timers first.
1304 */
1305 TMTimerLock(pThis->aTimers[0].pTimerR3, VERR_IGNORED);
1306 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aTimers); i++)
1307 {
1308 HpetTimer *pHpetTimer = &pThis->aTimers[i];
1309 Assert(pHpetTimer->idxTimer == i);
1310 TMTimerStop(pHpetTimer->pTimerR3);
1311
1312 /* capable of periodic operations and 64-bits */
1313 if (pThis->fIch9)
1314 pHpetTimer->u64Config = (i == 0)
1315 ? (HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP)
1316 : 0;
1317 else
1318 pHpetTimer->u64Config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
1319
1320 /* We can do all IRQs */
1321 uint32_t u32RoutingCap = 0xffffffff;
1322 pHpetTimer->u64Config |= ((uint64_t)u32RoutingCap) << 32;
1323 pHpetTimer->u64Period = 0;
1324 pHpetTimer->u8Wrap = 0;
1325 pHpetTimer->u64Cmp = hpetInvalidValue(pHpetTimer);
1326 }
1327 TMTimerUnlock(pThis->aTimers[0].pTimerR3);
1328
1329 /*
1330 * The HPET state.
1331 */
1332 pThis->u64HpetConfig = 0;
1333 pThis->u64HpetCounter = 0;
1334 pThis->u64HpetOffset = 0;
1335
1336 /* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
1337 pThis->u32Capabilities = (1 << 15) /* LEG_RT_CAP - LegacyReplacementRoute capable. */
1338 | (1 << 13) /* COUNTER_SIZE_CAP - Main counter is 64-bit capable. */
1339 | 1; /* REV_ID - Revision, must not be 0 */
1340 if (pThis->fIch9) /* NUM_TIM_CAP - Number of timers -1. */
1341 pThis->u32Capabilities |= (HPET_NUM_TIMERS_ICH9 - 1) << 8;
1342 else
1343 pThis->u32Capabilities |= (HPET_NUM_TIMERS_PIIX - 1) << 8;
1344 pThis->u32Capabilities |= UINT32_C(0x80860000); /* VENDOR */
1345 AssertCompile(HPET_NUM_TIMERS_ICH9 <= RT_ELEMENTS(pThis->aTimers));
1346 AssertCompile(HPET_NUM_TIMERS_PIIX <= RT_ELEMENTS(pThis->aTimers));
1347
1348 pThis->u32Period = pThis->fIch9 ? HPET_CLK_PERIOD_ICH9 : HPET_CLK_PERIOD_PIIX;
1349
1350 /*
1351 * Notify the PIT/RTC devices.
1352 */
1353 if (pThis->pHpetHlpR3)
1354 pThis->pHpetHlpR3->pfnSetLegacyMode(pDevIns, false /*fActive*/);
1355}
1356
1357
1358/**
1359 * @interface_method_impl{PDMDEVREG,pfnConstruct}
1360 */
1361static DECLCALLBACK(int) hpetConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
1362{
1363 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1364 HpetState *pThis = PDMINS_2_DATA(pDevIns, HpetState *);
1365
1366 /* Only one HPET device now, as we use fixed MMIO region. */
1367 Assert(iInstance == 0);
1368
1369 /*
1370 * Validate and read the configuration.
1371 */
1372 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "GCEnabled|R0Enabled|ICH9", "");
1373
1374 bool fRCEnabled;
1375 int rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &fRCEnabled, true);
1376 if (RT_FAILURE(rc))
1377 return PDMDEV_SET_ERROR(pDevIns, rc,
1378 N_("Configuration error: Querying \"GCEnabled\" as a bool failed"));
1379
1380 bool fR0Enabled;
1381 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &fR0Enabled, true);
1382 if (RT_FAILURE(rc))
1383 return PDMDEV_SET_ERROR(pDevIns, rc,
1384 N_("Configuration error: failed to read R0Enabled as boolean"));
1385
1386 rc = CFGMR3QueryBoolDef(pCfg, "ICH9", &pThis->fIch9, false);
1387 if (RT_FAILURE(rc))
1388 return PDMDEV_SET_ERROR(pDevIns, rc,
1389 N_("Configuration error: failed to read ICH9 as boolean"));
1390
1391 /*
1392 * Initialize the device state.
1393 */
1394 pThis->pDevInsR3 = pDevIns;
1395 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
1396 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1397
1398 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->csLock, RT_SRC_POS, "HPET#%u", pDevIns->iInstance);
1399 AssertRCReturn(rc, rc);
1400
1401 /* No automatic locking. */
1402 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
1403 AssertRCReturn(rc, rc);
1404
1405 /* Init the HPET timers (init all regardless of how many we expose). */
1406 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aTimers); i++)
1407 {
1408 HpetTimer *pHpetTimer = &pThis->aTimers[i];
1409
1410 pHpetTimer->idxTimer = i;
1411 pHpetTimer->pHpetR3 = pThis;
1412 pHpetTimer->pHpetR0 = PDMINS_2_DATA_R0PTR(pDevIns);
1413 pHpetTimer->pHpetRC = PDMINS_2_DATA_RCPTR(pDevIns);
1414
1415 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, hpetTimerCb, pHpetTimer,
1416 TMTIMER_FLAGS_NO_CRIT_SECT, "HPET Timer",
1417 &pThis->aTimers[i].pTimerR3);
1418 AssertRCReturn(rc, rc);
1419 pThis->aTimers[i].pTimerRC = TMTimerRCPtr(pThis->aTimers[i].pTimerR3);
1420 pThis->aTimers[i].pTimerR0 = TMTimerR0Ptr(pThis->aTimers[i].pTimerR3);
1421 rc = TMR3TimerSetCritSect(pThis->aTimers[i].pTimerR3, &pThis->csLock);
1422 AssertRCReturn(rc, rc);
1423 }
1424
1425 /* This must be done prior to registering the HPET, right? */
1426 hpetReset(pDevIns);
1427
1428 /*
1429 * Register the HPET and get helpers.
1430 */
1431 PDMHPETREG HpetReg;
1432 HpetReg.u32Version = PDM_HPETREG_VERSION;
1433 rc = PDMDevHlpHPETRegister(pDevIns, &HpetReg, &pThis->pHpetHlpR3);
1434 AssertRCReturn(rc, rc);
1435
1436 /*
1437 * Register the MMIO range, PDM API requests page aligned
1438 * addresses and sizes.
1439 */
1440 rc = PDMDevHlpMMIORegister(pDevIns, HPET_BASE, 0x1000, pThis,
1441 hpetMMIOWrite, hpetMMIORead, NULL, "HPET Memory");
1442 AssertRCReturn(rc, rc);
1443
1444 if (fRCEnabled)
1445 {
1446 rc = PDMDevHlpMMIORegisterRC(pDevIns, HPET_BASE, 0x1000, 0,
1447 "hpetMMIOWrite", "hpetMMIORead", NULL);
1448 AssertRCReturn(rc, rc);
1449
1450 pThis->pHpetHlpRC = pThis->pHpetHlpR3->pfnGetRCHelpers(pDevIns);
1451 AssertReturn(pThis->pHpetHlpRC != NIL_RTRCPTR, VERR_INTERNAL_ERROR);
1452 }
1453
1454 if (fR0Enabled)
1455 {
1456 rc = PDMDevHlpMMIORegisterR0(pDevIns, HPET_BASE, 0x1000, 0,
1457 "hpetMMIOWrite", "hpetMMIORead", NULL);
1458 AssertRCReturn(rc, rc);
1459
1460 pThis->pHpetHlpR0 = pThis->pHpetHlpR3->pfnGetR0Helpers(pDevIns);
1461 AssertReturn(pThis->pHpetHlpR0 != NIL_RTR0PTR, VERR_INTERNAL_ERROR);
1462 }
1463
1464 /* Register SSM callbacks */
1465 rc = PDMDevHlpSSMRegister3(pDevIns, HPET_SAVED_STATE_VERSION, sizeof(*pThis), hpetLiveExec, hpetSaveExec, hpetLoadExec);
1466 AssertRCReturn(rc, rc);
1467
1468 /* Register an info callback. */
1469 PDMDevHlpDBGFInfoRegister(pDevIns, "hpet", "Display HPET status. (no arguments)", hpetInfo);
1470
1471 return VINF_SUCCESS;
1472}
1473
1474
1475/**
1476 * The device registration structure.
1477 */
1478const PDMDEVREG g_DeviceHPET =
1479{
1480 /* u32Version */
1481 PDM_DEVREG_VERSION,
1482 /* szName */
1483 "hpet",
1484 /* szRCMod */
1485 "VBoxDDGC.gc",
1486 /* szR0Mod */
1487 "VBoxDDR0.r0",
1488 /* pszDescription */
1489 " High Precision Event Timer (HPET) Device",
1490 /* fFlags */
1491 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
1492 /* fClass */
1493 PDM_DEVREG_CLASS_PIT,
1494 /* cMaxInstances */
1495 1,
1496 /* cbInstance */
1497 sizeof(HpetState),
1498 /* pfnConstruct */
1499 hpetConstruct,
1500 /* pfnDestruct */
1501 NULL,
1502 /* pfnRelocate */
1503 hpetRelocate,
1504 /* pfnIOCtl */
1505 NULL,
1506 /* pfnPowerOn */
1507 NULL,
1508 /* pfnReset */
1509 hpetReset,
1510 /* pfnSuspend */
1511 NULL,
1512 /* pfnResume */
1513 NULL,
1514 /* pfnAttach */
1515 NULL,
1516 /* pfnDetach */
1517 NULL,
1518 /* pfnQueryInterface. */
1519 NULL,
1520 /* pfnInitComplete */
1521 NULL,
1522 /* pfnPowerOff */
1523 NULL,
1524 /* pfnSoftReset */
1525 NULL,
1526 /* u32VersionEnd */
1527 PDM_DEVREG_VERSION
1528};
1529
1530#endif /* IN_RING3 */
1531#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
1532
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