VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevIoApic.cpp@ 91942

Last change on this file since 91942 was 90436, checked in by vboxsync, 3 years ago

VMM,Dev*: Handle PDMCritSectEnter failures in relation to the PDM critsect. bugref:6695

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 77.8 KB
Line 
1/* $Id: DevIoApic.cpp 90436 2021-07-30 16:03:48Z vboxsync $ */
2/** @file
3 * IO APIC - Input/Output Advanced Programmable Interrupt Controller.
4 */
5
6/*
7 * Copyright (C) 2016-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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_IOAPIC
23#include <VBox/log.h>
24#include <VBox/vmm/hm.h>
25#include <VBox/msi.h>
26#include <VBox/pci.h>
27#include <VBox/vmm/pdmdev.h>
28
29#include "VBoxDD.h"
30#include <iprt/x86.h>
31#include <iprt/string.h>
32
33
34/*********************************************************************************************************************************
35* Defined Constants And Macros *
36*********************************************************************************************************************************/
37/** The current IO APIC saved state version. */
38#define IOAPIC_SAVED_STATE_VERSION 3
39/** The current IO APIC saved state version. */
40#define IOAPIC_SAVED_STATE_VERSION_NO_FLIPFLOP_MAP 2
41/** The saved state version used by VirtualBox 5.0 and
42 * earlier. */
43#define IOAPIC_SAVED_STATE_VERSION_VBOX_50 1
44
45/** Implementation specified by the "Intel I/O Controller Hub 9
46 * (ICH9) Family" */
47#define IOAPIC_VERSION_ICH9 0x20
48/** Implementation specified by the "82093AA I/O Advanced Programmable Interrupt
49Controller" */
50#define IOAPIC_VERSION_82093AA 0x11
51
52/** The default MMIO base physical address. */
53#define IOAPIC_MMIO_BASE_PHYSADDR UINT64_C(0xfec00000)
54/** The size of the MMIO range. */
55#define IOAPIC_MMIO_SIZE X86_PAGE_4K_SIZE
56/** The mask for getting direct registers from physical address. */
57#define IOAPIC_MMIO_REG_MASK 0xff
58
59/** The number of interrupt input pins. */
60#define IOAPIC_NUM_INTR_PINS 24
61/** Maximum redirection entires. */
62#define IOAPIC_MAX_RTE_INDEX (IOAPIC_NUM_INTR_PINS - 1)
63/** Reduced RTEs used by SIO.A (82379AB). */
64#define IOAPIC_REDUCED_MAX_RTE_INDEX (16 - 1)
65
66/** Version register - Gets the version. */
67#define IOAPIC_VER_GET_VER(a_Reg) ((a_Reg) & 0xff)
68/** Version register - Gets the maximum redirection entry. */
69#define IOAPIC_VER_GET_MRE(a_Reg) (((a_Reg) >> 16) & 0xff)
70/** Version register - Gets whether Pin Assertion Register (PRQ) is
71 * supported. */
72#define IOAPIC_VER_HAS_PRQ(a_Reg) RT_BOOL((a_Reg) & RT_BIT_32(15))
73
74/** Index register - Valid write mask. */
75#define IOAPIC_INDEX_VALID_WRITE_MASK UINT32_C(0xff)
76
77/** Arbitration register - Gets the ID. */
78#define IOAPIC_ARB_GET_ID(a_Reg) ((a_Reg) >> 24 & 0xf)
79
80/** ID register - Gets the ID. */
81#define IOAPIC_ID_GET_ID(a_Reg) ((a_Reg) >> 24 & 0xff)
82
83/** Redirection table entry - Vector. */
84#define IOAPIC_RTE_VECTOR UINT64_C(0xff)
85/** Redirection table entry - Delivery mode. */
86#define IOAPIC_RTE_DELIVERY_MODE (RT_BIT_64(8) | RT_BIT_64(9) | RT_BIT_64(10))
87/** Redirection table entry - Destination mode. */
88#define IOAPIC_RTE_DEST_MODE RT_BIT_64(11)
89/** Redirection table entry - Delivery status. */
90#define IOAPIC_RTE_DELIVERY_STATUS RT_BIT_64(12)
91/** Redirection table entry - Interrupt input pin polarity. */
92#define IOAPIC_RTE_POLARITY RT_BIT_64(13)
93/** Redirection table entry - Remote IRR. */
94#define IOAPIC_RTE_REMOTE_IRR RT_BIT_64(14)
95/** Redirection table entry - Trigger Mode. */
96#define IOAPIC_RTE_TRIGGER_MODE RT_BIT_64(15)
97/** Redirection table entry - Number of bits to shift to get the Mask. */
98#define IOAPIC_RTE_MASK_BIT 16
99/** Redirection table entry - The Mask. */
100#define IOAPIC_RTE_MASK RT_BIT_64(IOAPIC_RTE_MASK_BIT)
101/** Redirection table entry - Extended Destination ID. */
102#define IOAPIC_RTE_EXT_DEST_ID UINT64_C(0x00ff000000000000)
103/** Redirection table entry - Destination. */
104#define IOAPIC_RTE_DEST UINT64_C(0xff00000000000000)
105
106/** Redirection table entry - Gets the destination. */
107#define IOAPIC_RTE_GET_DEST(a_Reg) ((a_Reg) >> 56 & 0xff)
108/** Redirection table entry - Gets the mask flag. */
109#define IOAPIC_RTE_GET_MASK(a_Reg) (((a_Reg) >> IOAPIC_RTE_MASK_BIT) & 0x1)
110/** Redirection table entry - Checks whether it's masked. */
111#define IOAPIC_RTE_IS_MASKED(a_Reg) ((a_Reg) & IOAPIC_RTE_MASK)
112/** Redirection table entry - Gets the trigger mode. */
113#define IOAPIC_RTE_GET_TRIGGER_MODE(a_Reg) (((a_Reg) >> 15) & 0x1)
114/** Redirection table entry - Gets the remote IRR flag. */
115#define IOAPIC_RTE_GET_REMOTE_IRR(a_Reg) (((a_Reg) >> 14) & 0x1)
116/** Redirection table entry - Gets the interrupt pin polarity. */
117#define IOAPIC_RTE_GET_POLARITY(a_Reg) (((a_Reg) >> 13) & 0x1)
118/** Redirection table entry - Gets the delivery status. */
119#define IOAPIC_RTE_GET_DELIVERY_STATUS(a_Reg) (((a_Reg) >> 12) & 0x1)
120/** Redirection table entry - Gets the destination mode. */
121#define IOAPIC_RTE_GET_DEST_MODE(a_Reg) (((a_Reg) >> 11) & 0x1)
122/** Redirection table entry - Gets the delivery mode. */
123#define IOAPIC_RTE_GET_DELIVERY_MODE(a_Reg) (((a_Reg) >> 8) & 0x7)
124/** Redirection table entry - Gets the vector. */
125#define IOAPIC_RTE_GET_VECTOR(a_Reg) ((a_Reg) & IOAPIC_RTE_VECTOR)
126
127/** @name DMAR variant interpretation of RTE fields.
128 * @{ */
129/** Redirection table entry - Number of bits to shift to get Interrupt
130 * Index[14:0]. */
131#define IOAPIC_RTE_INTR_INDEX_LO_BIT 49
132/** Redirection table entry - Interrupt Index[14:0]. */
133#define IOAPIC_RTE_INTR_INDEX_LO UINT64_C(0xfffe000000000000)
134/** Redirection table entry - Number of bits to shift to get interrupt format. */
135#define IOAPIC_RTE_INTR_FORMAT_BIT 48
136/** Redirection table entry - Interrupt format. */
137#define IOAPIC_RTE_INTR_FORMAT RT_BIT_64(IOAPIC_RTE_INTR_FORMAT_BIT)
138/** Redirection table entry - Number of bits to shift to get Interrupt Index[15]. */
139#define IOAPIC_RTE_INTR_INDEX_HI_BIT 11
140/** Redirection table entry - Interrupt Index[15]. */
141#define IOAPIC_RTE_INTR_INDEX_HI RT_BIT_64(11)
142
143/** Redirection table entry - Gets the Interrupt Index[14:0]. */
144#define IOAPIC_RTE_GET_INTR_INDEX_LO(a_Reg) ((a_Reg) >> IOAPIC_RTE_INTR_INDEX_LO_BIT)
145/** Redirection table entry - Gets the Interrupt format. */
146#define IOAPIC_RTE_GET_INTR_FORMAT(a_Reg) (((a_Reg) >> IOAPIC_RTE_INTR_FORMAT_BIT) & 0x1)
147/** Redirection table entry - Gets the Interrupt Index[15]. */
148#define IOAPIC_RTE_GET_INTR_INDEX_HI(a_Reg) (((a_Reg) >> IOAPIC_RTE_INTR_INDEX_HI_BIT) & 0x1)
149/** @} */
150
151/** Redirection table entry - Valid write mask for 82093AA. */
152#define IOAPIC_RTE_VALID_WRITE_MASK_82093AA ( IOAPIC_RTE_DEST | IOAPIC_RTE_MASK | IOAPIC_RTE_TRIGGER_MODE \
153 | IOAPIC_RTE_POLARITY | IOAPIC_RTE_DEST_MODE | IOAPIC_RTE_DELIVERY_MODE \
154 | IOAPIC_RTE_VECTOR)
155/** Redirection table entry - Valid read mask for 82093AA. */
156#define IOAPIC_RTE_VALID_READ_MASK_82093AA ( IOAPIC_RTE_DEST | IOAPIC_RTE_MASK | IOAPIC_RTE_TRIGGER_MODE \
157 | IOAPIC_RTE_REMOTE_IRR | IOAPIC_RTE_POLARITY | IOAPIC_RTE_DELIVERY_STATUS \
158 | IOAPIC_RTE_DEST_MODE | IOAPIC_RTE_DELIVERY_MODE | IOAPIC_RTE_VECTOR)
159
160/** Redirection table entry - Valid write mask for ICH9. */
161/** @note The remote IRR bit has been reverted to read-only as it turns out the
162 * ICH9 spec. is wrong, see @bugref{8386#c46}. */
163#define IOAPIC_RTE_VALID_WRITE_MASK_ICH9 ( IOAPIC_RTE_DEST | IOAPIC_RTE_MASK | IOAPIC_RTE_TRIGGER_MODE \
164 /*| IOAPIC_RTE_REMOTE_IRR */| IOAPIC_RTE_POLARITY | IOAPIC_RTE_DEST_MODE \
165 | IOAPIC_RTE_DELIVERY_MODE | IOAPIC_RTE_VECTOR)
166/** Redirection table entry - Valid read mask (incl. ExtDestID) for ICH9. */
167#define IOAPIC_RTE_VALID_READ_MASK_ICH9 ( IOAPIC_RTE_DEST | IOAPIC_RTE_EXT_DEST_ID | IOAPIC_RTE_MASK \
168 | IOAPIC_RTE_TRIGGER_MODE | IOAPIC_RTE_REMOTE_IRR | IOAPIC_RTE_POLARITY \
169 | IOAPIC_RTE_DELIVERY_STATUS | IOAPIC_RTE_DEST_MODE | IOAPIC_RTE_DELIVERY_MODE \
170 | IOAPIC_RTE_VECTOR)
171
172/** Redirection table entry - Valid write mask for DMAR variant. */
173#define IOAPIC_RTE_VALID_WRITE_MASK_DMAR ( IOAPIC_RTE_INTR_INDEX_LO | IOAPIC_RTE_INTR_FORMAT | IOAPIC_RTE_MASK \
174 | IOAPIC_RTE_TRIGGER_MODE | IOAPIC_RTE_POLARITY | IOAPIC_RTE_INTR_INDEX_HI \
175 | IOAPIC_RTE_DELIVERY_MODE | IOAPIC_RTE_VECTOR)
176/** Redirection table entry - Valid read mask for DMAR variant. */
177#define IOAPIC_RTE_VALID_READ_MASK_DMAR ( IOAPIC_RTE_INTR_INDEX_LO | IOAPIC_RTE_INTR_FORMAT | IOAPIC_RTE_MASK \
178 | IOAPIC_RTE_TRIGGER_MODE | IOAPIC_RTE_REMOTE_IRR | IOAPIC_RTE_POLARITY \
179 | IOAPIC_RTE_DELIVERY_STATUS | IOAPIC_RTE_INTR_INDEX_HI | IOAPIC_RTE_DELIVERY_MODE \
180 | IOAPIC_RTE_VECTOR)
181
182/** Redirection table entry - Trigger mode edge. */
183#define IOAPIC_RTE_TRIGGER_MODE_EDGE 0
184/** Redirection table entry - Trigger mode level. */
185#define IOAPIC_RTE_TRIGGER_MODE_LEVEL 1
186/** Redirection table entry - Destination mode physical. */
187#define IOAPIC_RTE_DEST_MODE_PHYSICAL 0
188/** Redirection table entry - Destination mode logical. */
189#define IOAPIC_RTE_DEST_MODE_LOGICAL 1
190
191
192/** Index of indirect registers in the I/O APIC register table. */
193#define IOAPIC_INDIRECT_INDEX_ID 0x0
194#define IOAPIC_INDIRECT_INDEX_VERSION 0x1
195#define IOAPIC_INDIRECT_INDEX_ARB 0x2 /* Older I/O APIC only. */
196#define IOAPIC_INDIRECT_INDEX_REDIR_TBL_START 0x10 /* First valid RTE register index. */
197#define IOAPIC_INDIRECT_INDEX_RTE_END 0x3F /* Last valid RTE register index (24 RTEs). */
198#define IOAPIC_REDUCED_INDIRECT_INDEX_RTE_END 0x2F /* Last valid RTE register index (16 RTEs). */
199
200/** Offset of direct registers in the I/O APIC MMIO space. */
201#define IOAPIC_DIRECT_OFF_INDEX 0x00
202#define IOAPIC_DIRECT_OFF_DATA 0x10
203#define IOAPIC_DIRECT_OFF_EOI 0x40 /* Newer I/O APIC only. */
204
205/* Use PDM critsect for now for I/O APIC locking, see @bugref{8245#c121}. */
206#define IOAPIC_WITH_PDM_CRITSECT
207#ifdef IOAPIC_WITH_PDM_CRITSECT
208# define IOAPIC_LOCK(a_pDevIns, a_pThis, a_pThisCC, rcBusy) (a_pThisCC)->pIoApicHlp->pfnLock((a_pDevIns), (rcBusy))
209# define IOAPIC_UNLOCK(a_pDevIns, a_pThis, a_pThisCC) (a_pThisCC)->pIoApicHlp->pfnUnlock((a_pDevIns))
210# define IOAPIC_LOCK_IS_OWNER(a_pDevIns, a_pThis, a_pThisCC) (a_pThisCC)->pIoApicHlp->pfnLockIsOwner((a_pDevIns))
211#else
212# define IOAPIC_LOCK(a_pDevIns, a_pThis, a_pThisCC, rcBusy) PDMDevHlpCritSectEnter((a_pDevIns), &(a_pThis)->CritSect, (rcBusy))
213# define IOAPIC_UNLOCK(a_pDevIns, a_pThis, a_pThisCC) PDMDevHlpCritSectLeave((a_pDevIns), &(a_pThis)->CritSect)
214# define IOAPIC_LOCK_IS_OWNER(a_pDevIns, a_pThis, a_pThisCC) PDMDevHlpCritSectIsOwner((a_pDevIns), &(a_pThis)->CritSect)
215#endif
216
217
218/*********************************************************************************************************************************
219* Structures and Typedefs *
220*********************************************************************************************************************************/
221/**
222 * I/O APIC chipset (and variants) we support.
223 */
224typedef enum IOAPICTYPE
225{
226 IOAPICTYPE_ICH9 = 1,
227 IOAPICTYPE_DMAR,
228 IOAPICTYPE_82093AA,
229 IOAPICTYPE_82379AB,
230 IOAPICTYPE_32BIT_HACK = 0x7fffffff
231} IOAPICTYPE;
232AssertCompileSize(IOAPICTYPE, 4);
233
234/**
235 * The shared I/O APIC device state.
236 */
237typedef struct IOAPIC
238{
239 /** The ID register. */
240 uint8_t volatile u8Id;
241 /** The index register. */
242 uint8_t volatile u8Index;
243 /** Number of CPUs. */
244 uint8_t cCpus;
245 /** I/O APIC version. */
246 uint8_t u8ApicVer;
247 /** I/O APIC ID mask. */
248 uint8_t u8IdMask;
249 /** Maximum Redirection Table Entry (RTE) Entry. */
250 uint8_t u8MaxRte;
251 /** Last valid RTE indirect register index. */
252 uint8_t u8LastRteRegIdx;
253 /* Alignment padding. */
254 uint8_t u8Padding0[1];
255 /** Redirection table entry - Valid write mask. */
256 uint64_t u64RteWriteMask;
257 /** Redirection table entry - Valid read mask. */
258 uint64_t u64RteReadMask;
259
260 /** The redirection table registers. */
261 uint64_t au64RedirTable[IOAPIC_NUM_INTR_PINS];
262 /** The IRQ tags and source IDs for each pin (tracing purposes). */
263 uint32_t au32TagSrc[IOAPIC_NUM_INTR_PINS];
264 /** Bitmap keeping the flip-flop-ness of pending interrupts.
265 * The information held here is only relevan between SetIrq and the
266 * delivery, thus no real need to initialize or reset this. */
267 uint64_t bmFlipFlop[(IOAPIC_NUM_INTR_PINS + 63) / 64];
268
269 /** The internal IRR reflecting state of the interrupt lines. */
270 uint32_t uIrr;
271 /** The I/O APIC chipset type. */
272 IOAPICTYPE enmType;
273 /** The I/O APIC PCI address. */
274 PCIBDF uPciAddress;
275 /** Padding. */
276 uint32_t uPadding0;
277
278#ifndef IOAPIC_WITH_PDM_CRITSECT
279 /** The critsect for updating to the RTEs. */
280 PDMCRITSECT CritSect;
281#endif
282
283 /** The MMIO region. */
284 IOMMMIOHANDLE hMmio;
285
286#ifdef VBOX_WITH_STATISTICS
287 /** Number of MMIO reads in RZ. */
288 STAMCOUNTER StatMmioReadRZ;
289 /** Number of MMIO reads in R3. */
290 STAMCOUNTER StatMmioReadR3;
291
292 /** Number of MMIO writes in RZ. */
293 STAMCOUNTER StatMmioWriteRZ;
294 /** Number of MMIO writes in R3. */
295 STAMCOUNTER StatMmioWriteR3;
296
297 /** Number of SetIrq calls in RZ. */
298 STAMCOUNTER StatSetIrqRZ;
299 /** Number of SetIrq calls in R3. */
300 STAMCOUNTER StatSetIrqR3;
301
302 /** Number of SetEoi calls in RZ. */
303 STAMCOUNTER StatSetEoiRZ;
304 /** Number of SetEoi calls in R3. */
305 STAMCOUNTER StatSetEoiR3;
306
307 /** Number of redundant edge-triggered interrupts. */
308 STAMCOUNTER StatRedundantEdgeIntr;
309 /** Number of redundant level-triggered interrupts. */
310 STAMCOUNTER StatRedundantLevelIntr;
311 /** Number of suppressed level-triggered interrupts (by remote IRR). */
312 STAMCOUNTER StatSuppressedLevelIntr;
313 /** Number of IOMMU remapped interrupts (signaled by RTE). */
314 STAMCOUNTER StatIommuRemappedIntr;
315 /** Number of IOMMU discarded interrupts (signaled by RTE). */
316 STAMCOUNTER StatIommuDiscardedIntr;
317 /** Number of IOMMU remapped MSIs. */
318 STAMCOUNTER StatIommuRemappedMsi;
319 /** Number of IOMMU denied or failed MSIs. */
320 STAMCOUNTER StatIommuDiscardedMsi;
321 /** Number of returns to ring-3 due to Set RTE lock contention. */
322 STAMCOUNTER StatSetRteContention;
323 /** Number of level-triggered interrupts dispatched to the local APIC(s). */
324 STAMCOUNTER StatLevelIrqSent;
325 /** Number of EOIs received for level-triggered interrupts from the local
326 * APIC(s). */
327 STAMCOUNTER StatEoiReceived;
328 /** The time an interrupt level spent in the pending state. */
329 STAMPROFILEADV aStatLevelAct[IOAPIC_NUM_INTR_PINS];
330#endif
331 /** Per-vector stats. */
332 STAMCOUNTER aStatVectors[256];
333} IOAPIC;
334AssertCompileMemberAlignment(IOAPIC, au64RedirTable, 8);
335/** Pointer to shared IOAPIC data. */
336typedef IOAPIC *PIOAPIC;
337/** Pointer to const shared IOAPIC data. */
338typedef IOAPIC const *PCIOAPIC;
339
340
341/**
342 * The I/O APIC device state for ring-3.
343 */
344typedef struct IOAPICR3
345{
346 /** The IOAPIC helpers. */
347 R3PTRTYPE(PCPDMIOAPICHLP) pIoApicHlp;
348} IOAPICR3;
349/** Pointer to the I/O APIC device state for ring-3. */
350typedef IOAPICR3 *PIOAPICR3;
351
352
353/**
354 * The I/O APIC device state for ring-0.
355 */
356typedef struct IOAPICR0
357{
358 /** The IOAPIC helpers. */
359 R0PTRTYPE(PCPDMIOAPICHLP) pIoApicHlp;
360} IOAPICR0;
361/** Pointer to the I/O APIC device state for ring-0. */
362typedef IOAPICR0 *PIOAPICR0;
363
364
365/**
366 * The I/O APIC device state for raw-mode.
367 */
368typedef struct IOAPICRC
369{
370 /** The IOAPIC helpers. */
371 RCPTRTYPE(PCPDMIOAPICHLP) pIoApicHlp;
372} IOAPICRC;
373/** Pointer to the I/O APIC device state for raw-mode. */
374typedef IOAPICRC *PIOAPICRC;
375
376
377/** The I/O APIC device state for the current context. */
378typedef CTX_SUFF(IOAPIC) IOAPICCC;
379/** Pointer to the I/O APIC device state for the current context. */
380typedef CTX_SUFF(PIOAPIC) PIOAPICCC;
381
382
383/**
384 * xAPIC interrupt.
385 */
386typedef struct XAPICINTR
387{
388 /** The interrupt vector. */
389 uint8_t u8Vector;
390 /** The destination (mask or ID). */
391 uint8_t u8Dest;
392 /** The destination mode. */
393 uint8_t u8DestMode;
394 /** Delivery mode. */
395 uint8_t u8DeliveryMode;
396 /** Trigger mode. */
397 uint8_t u8TriggerMode;
398 /** Redirection hint. */
399 uint8_t u8RedirHint;
400 /** Polarity. */
401 uint8_t u8Polarity;
402 /** Padding. */
403 uint8_t abPadding0;
404} XAPICINTR;
405/** Pointer to an I/O xAPIC interrupt struct. */
406typedef XAPICINTR *PXAPICINTR;
407/** Pointer to a const xAPIC interrupt struct. */
408typedef XAPICINTR const *PCXAPICINTR;
409
410
411#ifndef VBOX_DEVICE_STRUCT_TESTCASE
412
413/**
414 * Gets the arbitration register.
415 *
416 * @returns The arbitration.
417 */
418DECLINLINE(uint32_t) ioapicGetArb(void)
419{
420 Log2(("IOAPIC: ioapicGetArb: returns 0\n"));
421 return 0;
422}
423
424
425/**
426 * Gets the version register.
427 *
428 * @returns The version.
429 */
430DECLINLINE(uint32_t) ioapicGetVersion(PCIOAPIC pThis)
431{
432 uint32_t uValue = RT_MAKE_U32(pThis->u8ApicVer, pThis->u8MaxRte);
433 Log2(("IOAPIC: ioapicGetVersion: returns %#RX32\n", uValue));
434 return uValue;
435}
436
437
438/**
439 * Sets the ID register.
440 *
441 * @param pThis The shared I/O APIC device state.
442 * @param uValue The value to set.
443 */
444DECLINLINE(void) ioapicSetId(PIOAPIC pThis, uint32_t uValue)
445{
446 Log2(("IOAPIC: ioapicSetId: uValue=%#RX32\n", uValue));
447 ASMAtomicWriteU8(&pThis->u8Id, (uValue >> 24) & pThis->u8IdMask);
448}
449
450
451/**
452 * Gets the ID register.
453 *
454 * @returns The ID.
455 * @param pThis The shared I/O APIC device state.
456 */
457DECLINLINE(uint32_t) ioapicGetId(PCIOAPIC pThis)
458{
459 uint32_t uValue = (uint32_t)pThis->u8Id << 24;
460 Log2(("IOAPIC: ioapicGetId: returns %#RX32\n", uValue));
461 return uValue;
462}
463
464
465/**
466 * Sets the index register.
467 *
468 * @param pThis The shared I/O APIC device state.
469 * @param uValue The value to set.
470 */
471DECLINLINE(void) ioapicSetIndex(PIOAPIC pThis, uint32_t uValue)
472{
473 LogFlow(("IOAPIC: ioapicSetIndex: uValue=%#RX32\n", uValue));
474 ASMAtomicWriteU8(&pThis->u8Index, uValue & IOAPIC_INDEX_VALID_WRITE_MASK);
475}
476
477
478/**
479 * Gets the index register.
480 *
481 * @returns The index value.
482 */
483DECLINLINE(uint32_t) ioapicGetIndex(PCIOAPIC pThis)
484{
485 uint32_t const uValue = pThis->u8Index;
486 LogFlow(("IOAPIC: ioapicGetIndex: returns %#x\n", uValue));
487 return uValue;
488}
489
490
491/**
492 * Converts an MSI message to an APIC interrupt.
493 *
494 * @param pMsi The MSI message to convert.
495 * @param pIntr Where to store the APIC interrupt.
496 */
497DECLINLINE(void) ioapicGetApicIntrFromMsi(PCMSIMSG pMsi, PXAPICINTR pIntr)
498{
499 /*
500 * Parse the message from the physical address and data.
501 * Do -not- zero out other fields in the APIC interrupt.
502 *
503 * See Intel spec. 10.11.1 "Message Address Register Format".
504 * See Intel spec. 10.11.2 "Message Data Register Format".
505 */
506 pIntr->u8Dest = pMsi->Addr.n.u8DestId;
507 pIntr->u8DestMode = pMsi->Addr.n.u1DestMode;
508 pIntr->u8RedirHint = pMsi->Addr.n.u1RedirHint;
509
510 pIntr->u8Vector = pMsi->Data.n.u8Vector;
511 pIntr->u8TriggerMode = pMsi->Data.n.u1TriggerMode;
512 pIntr->u8DeliveryMode = pMsi->Data.n.u3DeliveryMode;
513}
514
515
516#if defined(VBOX_WITH_IOMMU_AMD) || defined(VBOX_WITH_IOMMU_INTEL)
517/**
518 * Convert an RTE into an MSI message.
519 *
520 * @param u64Rte The RTE to convert.
521 * @param enmType The I/O APIC chipset type.
522 * @param pMsi Where to store the MSI message.
523 */
524DECLINLINE(void) ioapicGetMsiFromRte(uint64_t u64Rte, IOAPICTYPE enmType, PMSIMSG pMsi)
525{
526 bool const fRemappable = IOAPIC_RTE_GET_INTR_FORMAT(u64Rte);
527 if (!fRemappable)
528 {
529 pMsi->Addr.n.u12Addr = VBOX_MSI_ADDR_BASE >> VBOX_MSI_ADDR_SHIFT;
530 pMsi->Addr.n.u8DestId = IOAPIC_RTE_GET_DEST(u64Rte);
531 pMsi->Addr.n.u1RedirHint = 0;
532 pMsi->Addr.n.u1DestMode = IOAPIC_RTE_GET_DEST_MODE(u64Rte);
533
534 pMsi->Data.n.u8Vector = IOAPIC_RTE_GET_VECTOR(u64Rte);
535 pMsi->Data.n.u3DeliveryMode = IOAPIC_RTE_GET_DELIVERY_MODE(u64Rte);
536 pMsi->Data.n.u1TriggerMode = IOAPIC_RTE_GET_TRIGGER_MODE(u64Rte);
537 /* pMsi->Data.n.u1Level = ??? */
538 /** @todo r=ramshankar: Level triggered MSIs don't make much sense though
539 * possible in theory? Maybe document this more explicitly... */
540 }
541 else
542 {
543 Assert(enmType == IOAPICTYPE_DMAR);
544 NOREF(enmType);
545
546 /*
547 * The spec. mentions that SHV will be 0 when delivery mode is 0 (fixed), but
548 * not what SHV will be if delivery mode is not 0. I ASSUME copying delivery
549 * mode into SHV here is what hardware actually does.
550 *
551 * See Intel VT-d spec. 5.1.5.1 "I/OxAPIC Programming".
552 */
553 pMsi->Addr.dmar_remap.u12Addr = VBOX_MSI_ADDR_BASE >> VBOX_MSI_ADDR_SHIFT;
554 pMsi->Addr.dmar_remap.u14IntrIndexLo = IOAPIC_RTE_GET_INTR_INDEX_LO(u64Rte);
555 pMsi->Addr.dmar_remap.fIntrFormat = 1;
556 pMsi->Addr.dmar_remap.fShv = IOAPIC_RTE_GET_DELIVERY_MODE(u64Rte);
557 pMsi->Addr.dmar_remap.u1IntrIndexHi = IOAPIC_RTE_GET_INTR_INDEX_HI(u64Rte);
558
559 pMsi->Data.dmar_remap.u16SubHandle = 0;
560 }
561}
562#endif
563
564
565/**
566 * Signals the next pending interrupt for the specified Redirection Table Entry
567 * (RTE).
568 *
569 * @param pDevIns The device instance.
570 * @param pThis The shared I/O APIC device state.
571 * @param pThisCC The I/O APIC device state for the current context.
572 * @param idxRte The index of the RTE (validated).
573 *
574 * @remarks It is the responsibility of the caller to verify that an interrupt is
575 * pending for the pin corresponding to the RTE before calling this
576 * function.
577 */
578static void ioapicSignalIntrForRte(PPDMDEVINS pDevIns, PIOAPIC pThis, PIOAPICCC pThisCC, uint8_t idxRte)
579{
580 Assert(IOAPIC_LOCK_IS_OWNER(pDevIns, pThis, pThisCC));
581
582 /*
583 * Ensure the interrupt isn't masked.
584 */
585 uint64_t const u64Rte = pThis->au64RedirTable[idxRte];
586 if (!IOAPIC_RTE_IS_MASKED(u64Rte))
587 { /* likely */ }
588 else
589 return;
590
591 /* We cannot accept another level-triggered interrupt until remote IRR has been cleared. */
592 uint8_t const u8TriggerMode = IOAPIC_RTE_GET_TRIGGER_MODE(u64Rte);
593 if (u8TriggerMode == IOAPIC_RTE_TRIGGER_MODE_LEVEL)
594 {
595 uint8_t const u8RemoteIrr = IOAPIC_RTE_GET_REMOTE_IRR(u64Rte);
596 if (u8RemoteIrr)
597 {
598 STAM_COUNTER_INC(&pThis->StatSuppressedLevelIntr);
599 return;
600 }
601 }
602
603 XAPICINTR ApicIntr;
604 RT_ZERO(ApicIntr);
605 ApicIntr.u8Vector = IOAPIC_RTE_GET_VECTOR(u64Rte);
606 ApicIntr.u8Dest = IOAPIC_RTE_GET_DEST(u64Rte);
607 ApicIntr.u8DestMode = IOAPIC_RTE_GET_DEST_MODE(u64Rte);
608 ApicIntr.u8DeliveryMode = IOAPIC_RTE_GET_DELIVERY_MODE(u64Rte);
609 ApicIntr.u8Polarity = IOAPIC_RTE_GET_POLARITY(u64Rte);
610 ApicIntr.u8TriggerMode = u8TriggerMode;
611 //ApicIntr.u8RedirHint = 0;
612
613 /** @todo We might be able to release the IOAPIC(PDM) lock here and re-acquire it
614 * before setting the remote IRR bit below. The APIC and IOMMU should not
615 * require the caller to hold the PDM lock. */
616
617#if defined(VBOX_WITH_IOMMU_AMD) || defined(VBOX_WITH_IOMMU_INTEL)
618 /*
619 * The interrupt may need to be remapped (or discarded) if an IOMMU is present.
620 * For line-based interrupts we must use the southbridge I/O APIC's BDF as
621 * the origin of the interrupt, see @bugref{9654#c74}.
622 */
623 MSIMSG MsiIn;
624 RT_ZERO(MsiIn);
625 ioapicGetMsiFromRte(u64Rte, pThis->enmType, &MsiIn);
626
627 MSIMSG MsiOut;
628 int const rcRemap = pThisCC->pIoApicHlp->pfnIommuMsiRemap(pDevIns, pThis->uPciAddress, &MsiIn, &MsiOut);
629 if ( rcRemap == VERR_IOMMU_NOT_PRESENT
630 || rcRemap == VERR_IOMMU_CANNOT_CALL_SELF)
631 { /* likely - assuming majority of VMs don't have IOMMU configured. */ }
632 else if (RT_SUCCESS(rcRemap))
633 {
634 /* Update the APIC interrupt with the remapped data. */
635 ioapicGetApicIntrFromMsi(&MsiOut, &ApicIntr);
636
637 /* Ensure polarity hasn't changed (trigger mode might change with Intel IOMMUs). */
638 Assert(ApicIntr.u8Polarity == IOAPIC_RTE_GET_POLARITY(u64Rte));
639 STAM_COUNTER_INC(&pThis->StatIommuRemappedIntr);
640 }
641 else
642 {
643 STAM_COUNTER_INC(&pThis->StatIommuDiscardedIntr);
644 return;
645 }
646#endif
647
648 uint32_t const u32TagSrc = pThis->au32TagSrc[idxRte];
649 Log2(("IOAPIC: Signaling %s-triggered interrupt. Dest=%#x DestMode=%s Vector=%#x (%u)\n",
650 ApicIntr.u8TriggerMode == IOAPIC_RTE_TRIGGER_MODE_EDGE ? "edge" : "level", ApicIntr.u8Dest,
651 ApicIntr.u8DestMode == IOAPIC_RTE_DEST_MODE_PHYSICAL ? "physical" : "logical",
652 ApicIntr.u8Vector, ApicIntr.u8Vector));
653
654 /*
655 * Deliver to the local APIC via the system/3-wire-APIC bus.
656 */
657 int rc = pThisCC->pIoApicHlp->pfnApicBusDeliver(pDevIns,
658 ApicIntr.u8Dest,
659 ApicIntr.u8DestMode,
660 ApicIntr.u8DeliveryMode,
661 ApicIntr.u8Vector,
662 ApicIntr.u8Polarity,
663 ApicIntr.u8TriggerMode,
664 u32TagSrc);
665 /* Can't reschedule to R3. */
666 Assert(rc == VINF_SUCCESS || rc == VERR_APIC_INTR_DISCARDED);
667#ifdef DEBUG_ramshankar
668 if (rc == VERR_APIC_INTR_DISCARDED)
669 AssertMsgFailed(("APIC: Interrupt discarded u8Vector=%#x (%u) u64Rte=%#RX64\n", u8Vector, u8Vector, u64Rte));
670#endif
671
672 if (rc == VINF_SUCCESS)
673 {
674 /*
675 * For level-triggered interrupts, we set the remote IRR bit to indicate
676 * the local APIC has accepted the interrupt.
677 *
678 * For edge-triggered interrupts, we should not clear the IRR bit as it
679 * should remain intact to reflect the state of the interrupt line.
680 * The device will explicitly transition to inactive state via the
681 * ioapicSetIrq() callback.
682 */
683 if (u8TriggerMode == IOAPIC_RTE_TRIGGER_MODE_LEVEL)
684 {
685 Assert(u8TriggerMode == IOAPIC_RTE_TRIGGER_MODE_LEVEL);
686 pThis->au64RedirTable[idxRte] |= IOAPIC_RTE_REMOTE_IRR;
687 STAM_COUNTER_INC(&pThis->StatLevelIrqSent);
688 STAM_PROFILE_ADV_START(&pThis->aStatLevelAct[idxRte], a);
689 }
690 /*
691 * Edge-triggered flip-flops gets cleaned up here as the device code will
692 * not do any explicit ioapicSetIrq and we won't receive any EOI either.
693 */
694 else if (ASMBitTest(pThis->bmFlipFlop, idxRte))
695 {
696 Log2(("IOAPIC: Clearing IRR for edge flip-flop %#x uTagSrc=%#x\n", idxRte, pThis->au32TagSrc[idxRte]));
697 pThis->au32TagSrc[idxRte] = 0;
698 pThis->uIrr &= ~RT_BIT_32(idxRte);
699 }
700 }
701}
702
703
704/**
705 * Gets the redirection table entry.
706 *
707 * @returns The redirection table entry.
708 * @param pThis The shared I/O APIC device state.
709 * @param uIndex The index value.
710 */
711DECLINLINE(uint32_t) ioapicGetRedirTableEntry(PCIOAPIC pThis, uint32_t uIndex)
712{
713 uint8_t const idxRte = (uIndex - IOAPIC_INDIRECT_INDEX_REDIR_TBL_START) >> 1;
714 AssertMsgReturn(idxRte < RT_ELEMENTS(pThis->au64RedirTable),
715 ("Invalid index %u, expected < %u\n", idxRte, RT_ELEMENTS(pThis->au64RedirTable)),
716 UINT32_MAX);
717 uint32_t uValue;
718 if (!(uIndex & 1))
719 uValue = RT_LO_U32(pThis->au64RedirTable[idxRte]) & RT_LO_U32(pThis->u64RteReadMask);
720 else
721 uValue = RT_HI_U32(pThis->au64RedirTable[idxRte]) & RT_HI_U32(pThis->u64RteReadMask);
722
723 LogFlow(("IOAPIC: ioapicGetRedirTableEntry: uIndex=%#RX32 idxRte=%u returns %#RX32\n", uIndex, idxRte, uValue));
724 return uValue;
725}
726
727
728/**
729 * Sets the redirection table entry.
730 *
731 * @returns Strict VBox status code (VINF_IOM_R3_MMIO_WRITE / VINF_SUCCESS).
732 * @param pDevIns The device instance.
733 * @param pThis The shared I/O APIC device state.
734 * @param pThisCC The I/O APIC device state for the current context.
735 * @param uIndex The index value.
736 * @param uValue The value to set.
737 */
738static VBOXSTRICTRC ioapicSetRedirTableEntry(PPDMDEVINS pDevIns, PIOAPIC pThis, PIOAPICCC pThisCC,
739 uint32_t uIndex, uint32_t uValue)
740{
741 uint8_t const idxRte = (uIndex - IOAPIC_INDIRECT_INDEX_REDIR_TBL_START) >> 1;
742 AssertMsgReturn(idxRte < RT_ELEMENTS(pThis->au64RedirTable),
743 ("Invalid index %u, expected < %u\n", idxRte, RT_ELEMENTS(pThis->au64RedirTable)),
744 VINF_SUCCESS);
745
746 VBOXSTRICTRC rc = IOAPIC_LOCK(pDevIns, pThis, pThisCC, VINF_IOM_R3_MMIO_WRITE);
747 if (rc == VINF_SUCCESS)
748 {
749 /*
750 * Write the low or high 32-bit value into the specified 64-bit RTE register,
751 * update only the valid, writable bits.
752 *
753 * We need to preserve the read-only bits as it can have dire consequences
754 * otherwise, see @bugref{8386#c24}.
755 */
756 uint64_t const u64Rte = pThis->au64RedirTable[idxRte];
757 if (!(uIndex & 1))
758 {
759 uint32_t const u32RtePreserveLo = RT_LO_U32(u64Rte) & ~RT_LO_U32(pThis->u64RteWriteMask);
760 uint32_t const u32RteNewLo = (uValue & RT_LO_U32(pThis->u64RteWriteMask)) | u32RtePreserveLo;
761 uint64_t const u64RteHi = u64Rte & UINT64_C(0xffffffff00000000);
762 pThis->au64RedirTable[idxRte] = u64RteHi | u32RteNewLo;
763 }
764 else
765 {
766 uint32_t const u32RtePreserveHi = RT_HI_U32(u64Rte) & ~RT_HI_U32(pThis->u64RteWriteMask);
767 uint32_t const u32RteLo = RT_LO_U32(u64Rte);
768 uint64_t const u64RteNewHi = ((uint64_t)((uValue & RT_HI_U32(pThis->u64RteWriteMask)) | u32RtePreserveHi) << 32);
769 pThis->au64RedirTable[idxRte] = u64RteNewHi | u32RteLo;
770 }
771
772 LogFlow(("IOAPIC: ioapicSetRedirTableEntry: uIndex=%#RX32 idxRte=%u uValue=%#RX32\n", uIndex, idxRte, uValue));
773
774 /*
775 * Signal the next pending interrupt for this RTE.
776 */
777 uint32_t const uPinMask = UINT32_C(1) << idxRte;
778 if (pThis->uIrr & uPinMask)
779 {
780 LogFlow(("IOAPIC: ioapicSetRedirTableEntry: Signalling pending interrupt. idxRte=%u\n", idxRte));
781 ioapicSignalIntrForRte(pDevIns, pThis, pThisCC, idxRte);
782 }
783
784 IOAPIC_UNLOCK(pDevIns, pThis, pThisCC);
785 }
786 else
787 STAM_COUNTER_INC(&pThis->StatSetRteContention);
788
789 return rc;
790}
791
792
793/**
794 * Gets the data register.
795 *
796 * @returns The data value.
797 * @param pThis The shared I/O APIC device state.
798 */
799static uint32_t ioapicGetData(PCIOAPIC pThis)
800{
801 uint8_t const uIndex = pThis->u8Index;
802 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
803 if ( uIndex >= IOAPIC_INDIRECT_INDEX_REDIR_TBL_START
804 && uIndex <= pThis->u8LastRteRegIdx)
805 return ioapicGetRedirTableEntry(pThis, uIndex);
806
807 uint32_t uValue;
808 switch (uIndex)
809 {
810 case IOAPIC_INDIRECT_INDEX_ID:
811 uValue = ioapicGetId(pThis);
812 break;
813
814 case IOAPIC_INDIRECT_INDEX_VERSION:
815 uValue = ioapicGetVersion(pThis);
816 break;
817
818 case IOAPIC_INDIRECT_INDEX_ARB:
819 if (pThis->u8ApicVer == IOAPIC_VERSION_82093AA)
820 {
821 uValue = ioapicGetArb();
822 break;
823 }
824 RT_FALL_THRU();
825
826 default:
827 uValue = UINT32_C(0xffffffff);
828 Log2(("IOAPIC: Attempt to read register at invalid index %#x\n", uIndex));
829 break;
830 }
831 return uValue;
832}
833
834
835/**
836 * Sets the data register.
837 *
838 * @returns Strict VBox status code.
839 * @param pDevIns The device instance.
840 * @param pThis The shared I/O APIC device state.
841 * @param pThisCC The I/O APIC device state for the current context.
842 * @param uValue The value to set.
843 */
844static VBOXSTRICTRC ioapicSetData(PPDMDEVINS pDevIns, PIOAPIC pThis, PIOAPICCC pThisCC, uint32_t uValue)
845{
846 uint8_t const uIndex = pThis->u8Index;
847 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
848 LogFlow(("IOAPIC: ioapicSetData: uIndex=%#x uValue=%#RX32\n", uIndex, uValue));
849
850 if ( uIndex >= IOAPIC_INDIRECT_INDEX_REDIR_TBL_START
851 && uIndex <= pThis->u8LastRteRegIdx)
852 return ioapicSetRedirTableEntry(pDevIns, pThis, pThisCC, uIndex, uValue);
853
854 if (uIndex == IOAPIC_INDIRECT_INDEX_ID)
855 ioapicSetId(pThis, uValue);
856 else
857 Log2(("IOAPIC: ioapicSetData: Invalid index %#RX32, ignoring write request with uValue=%#RX32\n", uIndex, uValue));
858
859 return VINF_SUCCESS;
860}
861
862
863/**
864 * @interface_method_impl{PDMIOAPICREG,pfnSetEoi}
865 */
866static DECLCALLBACK(void) ioapicSetEoi(PPDMDEVINS pDevIns, uint8_t u8Vector)
867{
868 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
869 PIOAPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOAPICCC);
870
871 LogFlow(("IOAPIC: ioapicSetEoi: u8Vector=%#x (%u)\n", u8Vector, u8Vector));
872 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatSetEoi));
873
874 bool fRemoteIrrCleared = false;
875 int rc = IOAPIC_LOCK(pDevIns, pThis, pThisCC, VINF_SUCCESS);
876 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, NULL, rc);
877
878 for (uint8_t idxRte = 0; idxRte < RT_ELEMENTS(pThis->au64RedirTable); idxRte++)
879 {
880 uint64_t const u64Rte = pThis->au64RedirTable[idxRte];
881/** @todo r=bird: bugref:10073: I've changed it to ignore edge triggered
882 * entries here since the APIC will only call us for those? Not doing so
883 * confuses ended up with spurious HPET/RTC IRQs in SMP linux because of it
884 * sharing the vector with a level-triggered IRQ (like vboxguest) delivered on a
885 * different CPU.
886 *
887 * Maybe we should also/instead filter on the source APIC number? */
888 if ( IOAPIC_RTE_GET_VECTOR(u64Rte) == u8Vector
889 && IOAPIC_RTE_GET_TRIGGER_MODE(u64Rte) != IOAPIC_RTE_TRIGGER_MODE_EDGE)
890 {
891#ifdef DEBUG_ramshankar
892 /* This assertion may trigger when restoring saved-states created using the old, incorrect I/O APIC code. */
893 Assert(IOAPIC_RTE_GET_REMOTE_IRR(u64Rte));
894#endif
895 pThis->au64RedirTable[idxRte] &= ~IOAPIC_RTE_REMOTE_IRR;
896 fRemoteIrrCleared = true;
897 STAM_PROFILE_ADV_STOP(&pThis->aStatLevelAct[idxRte], a);
898 STAM_COUNTER_INC(&pThis->StatEoiReceived);
899 Log2(("IOAPIC: ioapicSetEoi: Cleared remote IRR, idxRte=%u vector=%#x (%u)\n", idxRte, u8Vector, u8Vector));
900
901 /*
902 * Signal the next pending interrupt for this RTE.
903 */
904 uint32_t const uPinMask = UINT32_C(1) << idxRte;
905 if (pThis->uIrr & uPinMask)
906 ioapicSignalIntrForRte(pDevIns, pThis, pThisCC, idxRte);
907 }
908 }
909
910 IOAPIC_UNLOCK(pDevIns, pThis, pThisCC);
911
912#ifndef VBOX_WITH_IOMMU_AMD
913 AssertMsg(fRemoteIrrCleared, ("Failed to clear remote IRR for vector %#x (%u)\n", u8Vector, u8Vector));
914#endif
915}
916
917
918/**
919 * @interface_method_impl{PDMIOAPICREG,pfnSetIrq}
920 */
921static DECLCALLBACK(void) ioapicSetIrq(PPDMDEVINS pDevIns, PCIBDF uBusDevFn, int iIrq, int iLevel, uint32_t uTagSrc)
922{
923 RT_NOREF(uBusDevFn); /** @todo r=ramshankar: Remove this argument if it's also unnecessary with Intel IOMMU. */
924#define IOAPIC_ASSERT_IRQ(a_uBusDevFn, a_idxRte, a_PinMask, a_fForceTag) do { \
925 pThis->au32TagSrc[(a_idxRte)] = (a_fForceTag) || !pThis->au32TagSrc[(a_idxRte)] ? uTagSrc : RT_BIT_32(31); \
926 pThis->uIrr |= a_PinMask; \
927 ioapicSignalIntrForRte(pDevIns, pThis, pThisCC, (a_idxRte)); \
928 } while (0)
929
930 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
931 PIOAPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOAPICCC);
932 LogFlow(("IOAPIC: ioapicSetIrq: iIrq=%d iLevel=%d uTagSrc=%#x\n", iIrq, iLevel, uTagSrc));
933
934 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatSetIrq));
935
936 if (RT_LIKELY((unsigned)iIrq < RT_ELEMENTS(pThis->au64RedirTable)))
937 {
938 int rc = IOAPIC_LOCK(pDevIns, pThis, pThisCC, VINF_SUCCESS);
939 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, NULL, rc);
940
941 uint8_t const idxRte = iIrq;
942 uint32_t const uPinMask = UINT32_C(1) << idxRte;
943 uint32_t const u32RteLo = RT_LO_U32(pThis->au64RedirTable[idxRte]);
944 uint8_t const u8TriggerMode = IOAPIC_RTE_GET_TRIGGER_MODE(u32RteLo);
945
946 bool fActive = RT_BOOL(iLevel & 1);
947 /** @todo Polarity is busted elsewhere, we need to fix that
948 * first. See @bugref{8386#c7}. */
949#if 0
950 uint8_t const u8Polarity = IOAPIC_RTE_GET_POLARITY(u32RteLo);
951 fActive ^= u8Polarity; */
952#endif
953 if (!fActive)
954 {
955 pThis->uIrr &= ~uPinMask;
956 pThis->au32TagSrc[idxRte] = 0;
957 IOAPIC_UNLOCK(pDevIns, pThis, pThisCC);
958 return;
959 }
960
961 bool const fFlipFlop = ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP);
962 if (!fFlipFlop)
963 {
964 ASMBitClear(pThis->bmFlipFlop, idxRte);
965
966 uint32_t const uPrevIrr = pThis->uIrr & uPinMask;
967 if (u8TriggerMode == IOAPIC_RTE_TRIGGER_MODE_EDGE)
968 {
969 /*
970 * For edge-triggered interrupts, we need to act only on a low to high edge transition.
971 * See ICH9 spec. 13.5.7 "REDIR_TBL: Redirection Table (LPC I/F-D31:F0)".
972 */
973 if (!uPrevIrr)
974 IOAPIC_ASSERT_IRQ(uBusDevFn, idxRte, uPinMask, false);
975 else
976 {
977 STAM_COUNTER_INC(&pThis->StatRedundantEdgeIntr);
978 Log2(("IOAPIC: Redundant edge-triggered interrupt %#x (%u)\n", idxRte, idxRte));
979 }
980 }
981 else
982 {
983 Assert(u8TriggerMode == IOAPIC_RTE_TRIGGER_MODE_LEVEL);
984
985 /*
986 * For level-triggered interrupts, redundant interrupts are not a problem
987 * and will eventually be delivered anyway after an EOI, but our PDM devices
988 * should not typically call us with no change to the level.
989 */
990 if (!uPrevIrr)
991 { /* likely */ }
992 else
993 {
994 STAM_COUNTER_INC(&pThis->StatRedundantLevelIntr);
995 Log2(("IOAPIC: Redundant level-triggered interrupt %#x (%u)\n", idxRte, idxRte));
996 }
997
998 IOAPIC_ASSERT_IRQ(uBusDevFn, idxRte, uPinMask, false);
999 }
1000 }
1001 else
1002 {
1003 /*
1004 * The device is flip-flopping the interrupt line, which implies we should de-assert
1005 * and assert the interrupt line. The interrupt line is left in the asserted state
1006 * after a flip-flop request. The de-assert is a NOP wrts to signaling an interrupt
1007 * hence just the assert is done.
1008 *
1009 * Update @bugref{10073}: We now de-assert the interrupt line once it has been
1010 * delivered to the APIC to prevent it from getting re-delivered by accident (e.g.
1011 * on RTE write or by buggy EOI code). The XT-PIC works differently because of the
1012 * INTA, so it's set IRQ function will do what's described above: first lower the
1013 * interrupt line and then immediately raising it again, leaving the IRR flag set
1014 * most of the time. (How a real HPET/IOAPIC does this is a really good question
1015 * and would be observable if we could get at the IRR register of the IOAPIC...
1016 * Maybe by modifying the RTE? Our code will retrigger the interrupt that way.) */
1017 ASMBitSet(pThis->bmFlipFlop, idxRte);
1018 IOAPIC_ASSERT_IRQ(uBusDevFn, idxRte, uPinMask, true);
1019 }
1020
1021 IOAPIC_UNLOCK(pDevIns, pThis, pThisCC);
1022 }
1023#undef IOAPIC_ASSERT_IRQ
1024}
1025
1026
1027/**
1028 * @interface_method_impl{PDMIOAPICREG,pfnSendMsi}
1029 */
1030static DECLCALLBACK(void) ioapicSendMsi(PPDMDEVINS pDevIns, PCIBDF uBusDevFn, PCMSIMSG pMsi, uint32_t uTagSrc)
1031{
1032 PIOAPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOAPICCC);
1033 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1034 LogFlow(("IOAPIC: ioapicSendMsi: uBusDevFn=%#x Addr=%#RX64 Data=%#RX32 uTagSrc=%#x\n",
1035 uBusDevFn, pMsi->Addr.u64, pMsi->Data.u32, uTagSrc));
1036
1037 XAPICINTR ApicIntr;
1038 RT_ZERO(ApicIntr);
1039
1040#if defined(VBOX_WITH_IOMMU_AMD) || defined(VBOX_WITH_IOMMU_INTEL)
1041 /*
1042 * The MSI may need to be remapped (or discarded) if an IOMMU is present.
1043 *
1044 * If the Bus:Dev:Fn isn't valid, it is ASSUMED the device generating the
1045 * MSI is the IOMMU itself and hence isn't subjected to remapping. This
1046 * is the case with Intel IOMMUs.
1047 *
1048 * AMD IOMMUs are full fledged PCI devices, hence the BDF will be a
1049 * valid PCI slot, but interrupts generated by the IOMMU will be handled
1050 * by VERR_IOMMU_CANNOT_CALL_SELF case.
1051 */
1052 MSIMSG MsiOut;
1053 if (PCIBDF_IS_VALID(uBusDevFn))
1054 {
1055 int const rcRemap = pThisCC->pIoApicHlp->pfnIommuMsiRemap(pDevIns, uBusDevFn, pMsi, &MsiOut);
1056 if ( rcRemap == VERR_IOMMU_NOT_PRESENT
1057 || rcRemap == VERR_IOMMU_CANNOT_CALL_SELF)
1058 { /* likely - assuming majority of VMs don't have IOMMU configured. */ }
1059 else if (RT_SUCCESS(rcRemap))
1060 {
1061 STAM_COUNTER_INC(&pThis->StatIommuRemappedMsi);
1062 pMsi = &MsiOut;
1063 }
1064 else
1065 {
1066 STAM_COUNTER_INC(&pThis->StatIommuDiscardedMsi);
1067 return;
1068 }
1069 }
1070#else
1071 NOREF(uBusDevFn);
1072#endif
1073
1074 ioapicGetApicIntrFromMsi(pMsi, &ApicIntr);
1075
1076 /*
1077 * Deliver to the local APIC via the system/3-wire-APIC bus.
1078 */
1079 STAM_REL_COUNTER_INC(&pThis->aStatVectors[ApicIntr.u8Vector]);
1080
1081 int rc = pThisCC->pIoApicHlp->pfnApicBusDeliver(pDevIns,
1082 ApicIntr.u8Dest,
1083 ApicIntr.u8DestMode,
1084 ApicIntr.u8DeliveryMode,
1085 ApicIntr.u8Vector,
1086 0 /* u8Polarity - N/A */,
1087 ApicIntr.u8TriggerMode,
1088 uTagSrc);
1089 /* Can't reschedule to R3. */
1090 Assert(rc == VINF_SUCCESS || rc == VERR_APIC_INTR_DISCARDED); NOREF(rc);
1091}
1092
1093
1094/**
1095 * @callback_method_impl{FNIOMMMIONEWREAD}
1096 */
1097static DECLCALLBACK(VBOXSTRICTRC) ioapicMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
1098{
1099 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1100 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioRead));
1101 Assert(cb == 4); RT_NOREF_PV(cb); /* registered for dwords only */
1102 RT_NOREF_PV(pvUser);
1103
1104 VBOXSTRICTRC rc = VINF_SUCCESS;
1105 uint32_t *puValue = (uint32_t *)pv;
1106 uint32_t offReg = off & IOAPIC_MMIO_REG_MASK;
1107 switch (offReg)
1108 {
1109 case IOAPIC_DIRECT_OFF_INDEX:
1110 *puValue = ioapicGetIndex(pThis);
1111 break;
1112
1113 case IOAPIC_DIRECT_OFF_DATA:
1114 *puValue = ioapicGetData(pThis);
1115 break;
1116
1117 default:
1118 Log2(("IOAPIC: ioapicMmioRead: Invalid offset. off=%#RGp offReg=%#x\n", off, offReg));
1119 rc = VINF_IOM_MMIO_UNUSED_FF;
1120 break;
1121 }
1122
1123 LogFlow(("IOAPIC: ioapicMmioRead: offReg=%#x, returns %#RX32\n", offReg, *puValue));
1124 return rc;
1125}
1126
1127
1128/**
1129 * @callback_method_impl{FNIOMMMIONEWWRITE}
1130 */
1131static DECLCALLBACK(VBOXSTRICTRC) ioapicMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
1132{
1133 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1134 PIOAPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOAPICCC);
1135 RT_NOREF_PV(pvUser);
1136
1137 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioWrite));
1138
1139 Assert(!(off & 3));
1140 Assert(cb == 4); RT_NOREF_PV(cb); /* registered for dwords only */
1141
1142 VBOXSTRICTRC rc = VINF_SUCCESS;
1143 uint32_t const uValue = *(uint32_t const *)pv;
1144 uint32_t const offReg = off & IOAPIC_MMIO_REG_MASK;
1145
1146 LogFlow(("IOAPIC: ioapicMmioWrite: pThis=%p off=%#RGp cb=%u uValue=%#RX32\n", pThis, off, cb, uValue));
1147 switch (offReg)
1148 {
1149 case IOAPIC_DIRECT_OFF_INDEX:
1150 ioapicSetIndex(pThis, uValue);
1151 break;
1152
1153 case IOAPIC_DIRECT_OFF_DATA:
1154 rc = ioapicSetData(pDevIns, pThis, pThisCC, uValue);
1155 break;
1156
1157 case IOAPIC_DIRECT_OFF_EOI:
1158 if (pThis->u8ApicVer == IOAPIC_VERSION_ICH9)
1159 ioapicSetEoi(pDevIns, uValue);
1160 else
1161 Log(("IOAPIC: ioapicMmioWrite: Write to EOI register ignored!\n"));
1162 break;
1163
1164 default:
1165 Log2(("IOAPIC: ioapicMmioWrite: Invalid offset. off=%#RGp offReg=%#x\n", off, offReg));
1166 break;
1167 }
1168
1169 return rc;
1170}
1171
1172
1173#ifdef IN_RING3
1174
1175/** @interface_method_impl{DBGFREGDESC,pfnGet} */
1176static DECLCALLBACK(int) ioapicR3DbgReg_GetIndex(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
1177{
1178 RT_NOREF(pDesc);
1179 pValue->u32 = ioapicGetIndex(PDMDEVINS_2_DATA((PPDMDEVINS)pvUser, PCIOAPIC));
1180 return VINF_SUCCESS;
1181}
1182
1183
1184/** @interface_method_impl{DBGFREGDESC,pfnSet} */
1185static DECLCALLBACK(int) ioapicR3DbgReg_SetIndex(void *pvUser, PCDBGFREGDESC pDesc, PCDBGFREGVAL pValue, PCDBGFREGVAL pfMask)
1186{
1187 RT_NOREF(pDesc, pfMask);
1188 ioapicSetIndex(PDMDEVINS_2_DATA((PPDMDEVINS)pvUser, PIOAPIC), pValue->u8);
1189 return VINF_SUCCESS;
1190}
1191
1192
1193/** @interface_method_impl{DBGFREGDESC,pfnGet} */
1194static DECLCALLBACK(int) ioapicR3DbgReg_GetData(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
1195{
1196 RT_NOREF(pDesc);
1197 pValue->u32 = ioapicGetData((PDMDEVINS_2_DATA((PPDMDEVINS)pvUser, PCIOAPIC)));
1198 return VINF_SUCCESS;
1199}
1200
1201
1202/** @interface_method_impl{DBGFREGDESC,pfnSet} */
1203static DECLCALLBACK(int) ioapicR3DbgReg_SetData(void *pvUser, PCDBGFREGDESC pDesc, PCDBGFREGVAL pValue, PCDBGFREGVAL pfMask)
1204{
1205 PPDMDEVINS pDevIns = (PPDMDEVINS)pvUser;
1206 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1207 PIOAPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOAPICCC);
1208 RT_NOREF(pDesc, pfMask);
1209 return VBOXSTRICTRC_VAL(ioapicSetData(pDevIns, pThis, pThisCC, pValue->u32));
1210}
1211
1212
1213/** @interface_method_impl{DBGFREGDESC,pfnGet} */
1214static DECLCALLBACK(int) ioapicR3DbgReg_GetVersion(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
1215{
1216 PCIOAPIC pThis = PDMDEVINS_2_DATA((PPDMDEVINS)pvUser, PCIOAPIC);
1217 RT_NOREF(pDesc);
1218 pValue->u32 = ioapicGetVersion(pThis);
1219 return VINF_SUCCESS;
1220}
1221
1222
1223/** @interface_method_impl{DBGFREGDESC,pfnGet} */
1224static DECLCALLBACK(int) ioapicR3DbgReg_GetArb(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
1225{
1226 RT_NOREF(pvUser, pDesc);
1227 pValue->u32 = ioapicGetArb();
1228 return VINF_SUCCESS;
1229}
1230
1231
1232/** @interface_method_impl{DBGFREGDESC,pfnGet} */
1233static DECLCALLBACK(int) ioapicR3DbgReg_GetRte(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
1234{
1235 PCIOAPIC pThis = PDMDEVINS_2_DATA((PPDMDEVINS)pvUser, PCIOAPIC);
1236 Assert(pDesc->offRegister < RT_ELEMENTS(pThis->au64RedirTable));
1237 pValue->u64 = pThis->au64RedirTable[pDesc->offRegister];
1238 return VINF_SUCCESS;
1239}
1240
1241
1242/** @interface_method_impl{DBGFREGDESC,pfnSet} */
1243static DECLCALLBACK(int) ioapicR3DbgReg_SetRte(void *pvUser, PCDBGFREGDESC pDesc, PCDBGFREGVAL pValue, PCDBGFREGVAL pfMask)
1244{
1245 RT_NOREF(pfMask);
1246 PIOAPIC pThis = PDMDEVINS_2_DATA((PPDMDEVINS)pvUser, PIOAPIC);
1247 /* No locks, no checks, just do it. */
1248 Assert(pDesc->offRegister < RT_ELEMENTS(pThis->au64RedirTable));
1249 pThis->au64RedirTable[pDesc->offRegister] = pValue->u64;
1250 return VINF_SUCCESS;
1251}
1252
1253
1254/** IOREDTBLn sub fields. */
1255static DBGFREGSUBFIELD const g_aRteSubs[] =
1256{
1257 { "vector", 0, 8, 0, 0, NULL, NULL },
1258 { "dlvr_mode", 8, 3, 0, 0, NULL, NULL },
1259 { "dest_mode", 11, 1, 0, 0, NULL, NULL },
1260 { "dlvr_status", 12, 1, 0, DBGFREGSUBFIELD_FLAGS_READ_ONLY, NULL, NULL },
1261 { "polarity", 13, 1, 0, 0, NULL, NULL },
1262 { "remote_irr", 14, 1, 0, DBGFREGSUBFIELD_FLAGS_READ_ONLY, NULL, NULL },
1263 { "trigger_mode", 15, 1, 0, 0, NULL, NULL },
1264 { "mask", 16, 1, 0, 0, NULL, NULL },
1265 { "ext_dest_id", 48, 8, 0, DBGFREGSUBFIELD_FLAGS_READ_ONLY, NULL, NULL },
1266 { "dest", 56, 8, 0, 0, NULL, NULL },
1267 DBGFREGSUBFIELD_TERMINATOR()
1268};
1269
1270
1271/** Register descriptors for DBGF. */
1272static DBGFREGDESC const g_aRegDesc[] =
1273{
1274 { "index", DBGFREG_END, DBGFREGVALTYPE_U8, 0, 0, ioapicR3DbgReg_GetIndex, ioapicR3DbgReg_SetIndex, NULL, NULL },
1275 { "data", DBGFREG_END, DBGFREGVALTYPE_U32, 0, 0, ioapicR3DbgReg_GetData, ioapicR3DbgReg_SetData, NULL, NULL },
1276 { "version", DBGFREG_END, DBGFREGVALTYPE_U32, DBGFREG_FLAGS_READ_ONLY, 0, ioapicR3DbgReg_GetVersion, NULL, NULL, NULL },
1277 { "arb", DBGFREG_END, DBGFREGVALTYPE_U32, DBGFREG_FLAGS_READ_ONLY, 0, ioapicR3DbgReg_GetArb, NULL, NULL, NULL },
1278 { "rte0", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 0, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1279 { "rte1", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 1, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1280 { "rte2", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 2, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1281 { "rte3", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 3, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1282 { "rte4", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 4, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1283 { "rte5", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 5, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1284 { "rte6", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 6, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1285 { "rte7", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 7, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1286 { "rte8", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 8, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1287 { "rte9", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 9, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1288 { "rte10", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 10, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1289 { "rte11", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 11, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1290 { "rte12", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 12, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1291 { "rte13", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 13, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1292 { "rte14", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 14, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1293 { "rte15", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 15, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1294 { "rte16", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 16, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1295 { "rte17", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 17, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1296 { "rte18", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 18, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1297 { "rte19", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 19, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1298 { "rte20", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 20, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1299 { "rte21", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 21, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1300 { "rte22", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 22, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1301 { "rte23", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 23, ioapicR3DbgReg_GetRte, ioapicR3DbgReg_SetRte, NULL, &g_aRteSubs[0] },
1302 DBGFREGDESC_TERMINATOR()
1303};
1304
1305
1306/**
1307 * @callback_method_impl{FNDBGFHANDLERDEV}
1308 */
1309static DECLCALLBACK(void) ioapicR3DbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1310{
1311 RT_NOREF(pszArgs);
1312 PCIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1313 LogFlow(("IOAPIC: ioapicR3DbgInfo: pThis=%p pszArgs=%s\n", pThis, pszArgs));
1314
1315 bool const fLegacy = RTStrCmp(pszArgs, "legacy") == 0;
1316
1317 static const char * const s_apszDeliveryModes[] =
1318 {
1319 " fixed",
1320 "lowpri",
1321 " smi",
1322 " rsvd",
1323 " nmi",
1324 " init",
1325 " rsvd",
1326 "extint"
1327 };
1328 static const char * const s_apszDestMode[] = { "phys", "log " };
1329 static const char * const s_apszTrigMode[] = { " edge", "level" };
1330 static const char * const s_apszPolarity[] = { "acthi", "actlo" };
1331 static const char * const s_apszDeliveryStatus[] = { "idle", "pend" };
1332
1333 pHlp->pfnPrintf(pHlp, "I/O APIC at %#010x:\n", IOAPIC_MMIO_BASE_PHYSADDR);
1334
1335 uint32_t const uId = ioapicGetId(pThis);
1336 pHlp->pfnPrintf(pHlp, " ID = %#RX32\n", uId);
1337 pHlp->pfnPrintf(pHlp, " ID = %#x\n", IOAPIC_ID_GET_ID(uId));
1338
1339 uint32_t const uVer = ioapicGetVersion(pThis);
1340 pHlp->pfnPrintf(pHlp, " Version = %#RX32\n", uVer);
1341 pHlp->pfnPrintf(pHlp, " Version = %#x\n", IOAPIC_VER_GET_VER(uVer));
1342 pHlp->pfnPrintf(pHlp, " Pin Assert Reg. Support = %RTbool\n", IOAPIC_VER_HAS_PRQ(uVer));
1343 pHlp->pfnPrintf(pHlp, " Max. Redirection Entry = %u\n", IOAPIC_VER_GET_MRE(uVer));
1344
1345 if (pThis->u8ApicVer == IOAPIC_VERSION_82093AA)
1346 {
1347 uint32_t const uArb = ioapicGetArb();
1348 pHlp->pfnPrintf(pHlp, " Arbitration = %#RX32\n", uArb);
1349 pHlp->pfnPrintf(pHlp, " Arbitration ID = %#x\n", IOAPIC_ARB_GET_ID(uArb));
1350 }
1351
1352 pHlp->pfnPrintf(pHlp, " Current index = %#x\n", ioapicGetIndex(pThis));
1353
1354 pHlp->pfnPrintf(pHlp, " I/O Redirection Table and IRR:\n");
1355 if ( pThis->enmType != IOAPICTYPE_DMAR
1356 || fLegacy)
1357 {
1358 pHlp->pfnPrintf(pHlp, " idx dst_mode dst_addr mask irr trigger rirr polar dlvr_st dlvr_mode vector rte\n");
1359 pHlp->pfnPrintf(pHlp, " ---------------------------------------------------------------------------------------------\n");
1360
1361 uint8_t const idxMaxRte = RT_MIN(pThis->u8MaxRte, RT_ELEMENTS(pThis->au64RedirTable) - 1);
1362 for (uint8_t idxRte = 0; idxRte <= idxMaxRte; idxRte++)
1363 {
1364 const uint64_t u64Rte = pThis->au64RedirTable[idxRte];
1365 const char *pszDestMode = s_apszDestMode[IOAPIC_RTE_GET_DEST_MODE(u64Rte)];
1366 const uint8_t uDest = IOAPIC_RTE_GET_DEST(u64Rte);
1367 const uint8_t uMask = IOAPIC_RTE_GET_MASK(u64Rte);
1368 const char *pszTriggerMode = s_apszTrigMode[IOAPIC_RTE_GET_TRIGGER_MODE(u64Rte)];
1369 const uint8_t uRemoteIrr = IOAPIC_RTE_GET_REMOTE_IRR(u64Rte);
1370 const char *pszPolarity = s_apszPolarity[IOAPIC_RTE_GET_POLARITY(u64Rte)];
1371 const char *pszDeliveryStatus = s_apszDeliveryStatus[IOAPIC_RTE_GET_DELIVERY_STATUS(u64Rte)];
1372 const uint8_t uDeliveryMode = IOAPIC_RTE_GET_DELIVERY_MODE(u64Rte);
1373 Assert(uDeliveryMode < RT_ELEMENTS(s_apszDeliveryModes));
1374 const char *pszDeliveryMode = s_apszDeliveryModes[uDeliveryMode];
1375 const uint8_t uVector = IOAPIC_RTE_GET_VECTOR(u64Rte);
1376
1377 pHlp->pfnPrintf(pHlp, " %02d %s %02x %u %u %s %u %s %s %s %3u (%016llx)\n",
1378 idxRte,
1379 pszDestMode,
1380 uDest,
1381 uMask,
1382 (pThis->uIrr >> idxRte) & 1,
1383 pszTriggerMode,
1384 uRemoteIrr,
1385 pszPolarity,
1386 pszDeliveryStatus,
1387 pszDeliveryMode,
1388 uVector,
1389 u64Rte);
1390 }
1391 }
1392 else
1393 {
1394 pHlp->pfnPrintf(pHlp, " idx intr_idx fmt mask irr trigger rirr polar dlvr_st dlvr_mode vector rte\n");
1395 pHlp->pfnPrintf(pHlp, " ----------------------------------------------------------------------------------------\n");
1396
1397 uint8_t const idxMaxRte = RT_MIN(pThis->u8MaxRte, RT_ELEMENTS(pThis->au64RedirTable) - 1);
1398 for (uint8_t idxRte = 0; idxRte <= idxMaxRte; idxRte++)
1399 {
1400 const uint64_t u64Rte = pThis->au64RedirTable[idxRte];
1401 const uint16_t idxIntrLo = IOAPIC_RTE_GET_INTR_INDEX_LO(u64Rte);
1402 const uint8_t fIntrFormat = IOAPIC_RTE_GET_INTR_FORMAT(u64Rte);
1403 const uint8_t uMask = IOAPIC_RTE_GET_MASK(u64Rte);
1404 const char *pszTriggerMode = s_apszTrigMode[IOAPIC_RTE_GET_TRIGGER_MODE(u64Rte)];
1405 const uint8_t uRemoteIrr = IOAPIC_RTE_GET_REMOTE_IRR(u64Rte);
1406 const char *pszPolarity = s_apszPolarity[IOAPIC_RTE_GET_POLARITY(u64Rte)];
1407 const char *pszDeliveryStatus = s_apszDeliveryStatus[IOAPIC_RTE_GET_DELIVERY_STATUS(u64Rte)];
1408 const uint8_t uDeliveryMode = IOAPIC_RTE_GET_DELIVERY_MODE(u64Rte);
1409 Assert(uDeliveryMode < RT_ELEMENTS(s_apszDeliveryModes));
1410 const char *pszDeliveryMode = s_apszDeliveryModes[uDeliveryMode];
1411 const uint16_t idxIntrHi = IOAPIC_RTE_GET_INTR_INDEX_HI(u64Rte);
1412 const uint8_t uVector = IOAPIC_RTE_GET_VECTOR(u64Rte);
1413 const uint16_t idxIntr = idxIntrLo | (idxIntrHi << 15);
1414 pHlp->pfnPrintf(pHlp, " %02d %4u %u %u %u %s %u %s %s %s %3u (%016llx)\n",
1415 idxRte,
1416 idxIntr,
1417 fIntrFormat,
1418 uMask,
1419 (pThis->uIrr >> idxRte) & 1,
1420 pszTriggerMode,
1421 uRemoteIrr,
1422 pszPolarity,
1423 pszDeliveryStatus,
1424 pszDeliveryMode,
1425 uVector,
1426 u64Rte);
1427 }
1428 }
1429}
1430
1431
1432/**
1433 * @copydoc FNSSMDEVSAVEEXEC
1434 */
1435static DECLCALLBACK(int) ioapicR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1436{
1437 PCIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PCIOAPIC);
1438 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1439 LogFlow(("IOAPIC: ioapicR3SaveExec\n"));
1440
1441 pHlp->pfnSSMPutU32(pSSM, pThis->uIrr);
1442 pHlp->pfnSSMPutU8(pSSM, pThis->u8Id);
1443 pHlp->pfnSSMPutU8(pSSM, pThis->u8Index);
1444 for (uint8_t idxRte = 0; idxRte < RT_ELEMENTS(pThis->au64RedirTable); idxRte++)
1445 pHlp->pfnSSMPutU64(pSSM, pThis->au64RedirTable[idxRte]);
1446
1447 for (uint8_t idx = 0; idx < RT_ELEMENTS(pThis->bmFlipFlop); idx++)
1448 pHlp->pfnSSMPutU64(pSSM, pThis->bmFlipFlop[idx]);
1449
1450 return VINF_SUCCESS;
1451}
1452
1453
1454/**
1455 * @copydoc FNSSMDEVLOADEXEC
1456 */
1457static DECLCALLBACK(int) ioapicR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1458{
1459 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1460 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1461 LogFlow(("APIC: apicR3LoadExec: uVersion=%u uPass=%#x\n", uVersion, uPass));
1462
1463 Assert(uPass == SSM_PASS_FINAL);
1464 NOREF(uPass);
1465
1466 /* Weed out invalid versions. */
1467 if ( uVersion != IOAPIC_SAVED_STATE_VERSION
1468 && uVersion != IOAPIC_SAVED_STATE_VERSION_NO_FLIPFLOP_MAP
1469 && uVersion != IOAPIC_SAVED_STATE_VERSION_VBOX_50)
1470 {
1471 LogRel(("IOAPIC: ioapicR3LoadExec: Invalid/unrecognized saved-state version %u (%#x)\n", uVersion, uVersion));
1472 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1473 }
1474
1475 if (uVersion >= IOAPIC_SAVED_STATE_VERSION_NO_FLIPFLOP_MAP)
1476 pHlp->pfnSSMGetU32(pSSM, &pThis->uIrr);
1477
1478 pHlp->pfnSSMGetU8V(pSSM, &pThis->u8Id);
1479 pHlp->pfnSSMGetU8V(pSSM, &pThis->u8Index);
1480 for (uint8_t idxRte = 0; idxRte < RT_ELEMENTS(pThis->au64RedirTable); idxRte++)
1481 pHlp->pfnSSMGetU64(pSSM, &pThis->au64RedirTable[idxRte]);
1482
1483 if (uVersion > IOAPIC_SAVED_STATE_VERSION_NO_FLIPFLOP_MAP)
1484 for (uint8_t idx = 0; idx < RT_ELEMENTS(pThis->bmFlipFlop); idx++)
1485 pHlp->pfnSSMGetU64(pSSM, &pThis->bmFlipFlop[idx]);
1486
1487 return VINF_SUCCESS;
1488}
1489
1490
1491/**
1492 * @interface_method_impl{PDMDEVREG,pfnReset}
1493 */
1494static DECLCALLBACK(void) ioapicR3Reset(PPDMDEVINS pDevIns)
1495{
1496 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1497 PIOAPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOAPICCC);
1498 LogFlow(("IOAPIC: ioapicR3Reset: pThis=%p\n", pThis));
1499
1500 /* There might be devices threads calling ioapicSetIrq() in parallel, hence the lock. */
1501 IOAPIC_LOCK(pDevIns, pThis, pThisCC, VERR_IGNORED);
1502
1503 pThis->uIrr = 0;
1504 pThis->u8Index = 0;
1505 pThis->u8Id = 0;
1506
1507 for (uint8_t idxRte = 0; idxRte < RT_ELEMENTS(pThis->au64RedirTable); idxRte++)
1508 {
1509 pThis->au64RedirTable[idxRte] = IOAPIC_RTE_MASK;
1510 pThis->au32TagSrc[idxRte] = 0;
1511 }
1512
1513 IOAPIC_UNLOCK(pDevIns, pThis, pThisCC);
1514}
1515
1516
1517/**
1518 * @interface_method_impl{PDMDEVREG,pfnRelocate}
1519 */
1520static DECLCALLBACK(void) ioapicR3Relocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1521{
1522 PIOAPICRC pThisRC = PDMINS_2_DATA_RC(pDevIns, PIOAPICRC);
1523 LogFlow(("IOAPIC: ioapicR3Relocate: pThis=%p offDelta=%RGi\n", PDMDEVINS_2_DATA(pDevIns, PIOAPIC), offDelta));
1524
1525 pThisRC->pIoApicHlp += offDelta;
1526}
1527
1528
1529/**
1530 * @interface_method_impl{PDMDEVREG,pfnDestruct}
1531 */
1532static DECLCALLBACK(int) ioapicR3Destruct(PPDMDEVINS pDevIns)
1533{
1534 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
1535 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1536 LogFlow(("IOAPIC: ioapicR3Destruct: pThis=%p\n", pThis));
1537
1538# ifndef IOAPIC_WITH_PDM_CRITSECT
1539 /*
1540 * Destroy the RTE critical section.
1541 */
1542 if (PDMDevHlpCritSectIsInitialized(pDevIns, &pThis->CritSect))
1543 PDMDevHlpCritSectDelete(pDevIns, &pThis->CritSect);
1544# else
1545 RT_NOREF_PV(pThis);
1546# endif
1547
1548 return VINF_SUCCESS;
1549}
1550
1551
1552/**
1553 * @interface_method_impl{PDMDEVREG,pfnConstruct}
1554 */
1555static DECLCALLBACK(int) ioapicR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
1556{
1557 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1558 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1559 PIOAPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOAPICCC);
1560 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1561 LogFlow(("IOAPIC: ioapicR3Construct: pThis=%p iInstance=%d\n", pThis, iInstance));
1562 Assert(iInstance == 0); RT_NOREF(iInstance);
1563
1564 /*
1565 * Validate and read the configuration.
1566 */
1567 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "NumCPUs|ChipType|PCIAddress", "");
1568
1569 /* The number of CPUs is currently unused, but left in CFGM and saved-state in case an ID of 0
1570 upsets some guest which we haven't yet been tested. */
1571 uint32_t cCpus;
1572 int rc = pHlp->pfnCFGMQueryU32Def(pCfg, "NumCPUs", &cCpus, 1);
1573 if (RT_FAILURE(rc))
1574 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"NumCPUs\""));
1575 pThis->cCpus = (uint8_t)cCpus;
1576
1577 char szChipType[16];
1578 rc = pHlp->pfnCFGMQueryStringDef(pCfg, "ChipType", &szChipType[0], sizeof(szChipType), "ICH9");
1579 if (RT_FAILURE(rc))
1580 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query string value \"ChipType\""));
1581
1582 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "PCIAddress", &pThis->uPciAddress, NIL_PCIBDF);
1583 if (RT_FAILURE(rc))
1584 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query 32-bit integer \"PCIAddress\""));
1585
1586 if (!strcmp(szChipType, "ICH9"))
1587 {
1588 /* Newer 2007-ish I/O APIC integrated into ICH southbridges. */
1589 pThis->enmType = IOAPICTYPE_ICH9;
1590 pThis->u8ApicVer = IOAPIC_VERSION_ICH9;
1591 pThis->u8IdMask = 0xff;
1592 pThis->u8MaxRte = IOAPIC_MAX_RTE_INDEX;
1593 pThis->u8LastRteRegIdx = IOAPIC_INDIRECT_INDEX_RTE_END;
1594 pThis->u64RteWriteMask = IOAPIC_RTE_VALID_WRITE_MASK_ICH9;
1595 pThis->u64RteReadMask = IOAPIC_RTE_VALID_READ_MASK_ICH9;
1596 }
1597 else if (!strcmp(szChipType, "DMAR"))
1598 {
1599 /* Intel DMAR compatible I/O APIC integrated into ICH southbridges. */
1600 /* Identical to ICH9, but interprets RTEs and MSI address and data fields differently. */
1601 pThis->enmType = IOAPICTYPE_DMAR;
1602 pThis->u8ApicVer = IOAPIC_VERSION_ICH9;
1603 pThis->u8IdMask = 0xff;
1604 pThis->u8MaxRte = IOAPIC_MAX_RTE_INDEX;
1605 pThis->u8LastRteRegIdx = IOAPIC_INDIRECT_INDEX_RTE_END;
1606 pThis->u64RteWriteMask = IOAPIC_RTE_VALID_WRITE_MASK_DMAR;
1607 pThis->u64RteReadMask = IOAPIC_RTE_VALID_READ_MASK_DMAR;
1608 }
1609 else if (!strcmp(szChipType, "82093AA"))
1610 {
1611 /* Older 1995-ish discrete I/O APIC, used in P6 class systems. */
1612 pThis->enmType = IOAPICTYPE_82093AA;
1613 pThis->u8ApicVer = IOAPIC_VERSION_82093AA;
1614 pThis->u8IdMask = 0x0f;
1615 pThis->u8MaxRte = IOAPIC_MAX_RTE_INDEX;
1616 pThis->u8LastRteRegIdx = IOAPIC_INDIRECT_INDEX_RTE_END;
1617 pThis->u64RteWriteMask = IOAPIC_RTE_VALID_WRITE_MASK_82093AA;
1618 pThis->u64RteReadMask = IOAPIC_RTE_VALID_READ_MASK_82093AA;
1619 }
1620 else if (!strcmp(szChipType, "82379AB"))
1621 {
1622 /* Even older 1993-ish I/O APIC built into SIO.A, used in EISA and early PCI systems. */
1623 /* Exact same version and behavior as 82093AA, only the number of RTEs is different. */
1624 pThis->enmType = IOAPICTYPE_82379AB;
1625 pThis->u8ApicVer = IOAPIC_VERSION_82093AA;
1626 pThis->u8IdMask = 0x0f;
1627 pThis->u8MaxRte = IOAPIC_REDUCED_MAX_RTE_INDEX;
1628 pThis->u8LastRteRegIdx = IOAPIC_REDUCED_INDIRECT_INDEX_RTE_END;
1629 pThis->u64RteWriteMask = IOAPIC_RTE_VALID_WRITE_MASK_82093AA;
1630 pThis->u64RteReadMask = IOAPIC_RTE_VALID_READ_MASK_82093AA;
1631 }
1632 else
1633 return PDMDevHlpVMSetError(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES, RT_SRC_POS,
1634 N_("I/O APIC configuration error: The \"ChipType\" value \"%s\" is unsupported"), szChipType);
1635 Log2(("IOAPIC: cCpus=%u fRZEnabled=%RTbool szChipType=%s\n", cCpus, pDevIns->fR0Enabled | pDevIns->fRCEnabled, szChipType));
1636
1637 /*
1638 * We will use our own critical section for the IOAPIC device.
1639 */
1640 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
1641 AssertRCReturn(rc, rc);
1642
1643# ifndef IOAPIC_WITH_PDM_CRITSECT
1644 /*
1645 * Setup the critical section to protect concurrent writes to the RTEs.
1646 */
1647 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "IOAPIC");
1648 AssertRCReturn(rc, rc);
1649# endif
1650
1651 /*
1652 * Register the IOAPIC.
1653 */
1654 PDMIOAPICREG IoApicReg;
1655 IoApicReg.u32Version = PDM_IOAPICREG_VERSION;
1656 IoApicReg.pfnSetIrq = ioapicSetIrq;
1657 IoApicReg.pfnSendMsi = ioapicSendMsi;
1658 IoApicReg.pfnSetEoi = ioapicSetEoi;
1659 IoApicReg.u32TheEnd = PDM_IOAPICREG_VERSION;
1660 rc = PDMDevHlpIoApicRegister(pDevIns, &IoApicReg, &pThisCC->pIoApicHlp);
1661 AssertRCReturn(rc, rc);
1662 AssertPtr(pThisCC->pIoApicHlp->pfnApicBusDeliver);
1663 AssertPtr(pThisCC->pIoApicHlp->pfnLock);
1664 AssertPtr(pThisCC->pIoApicHlp->pfnUnlock);
1665 AssertPtr(pThisCC->pIoApicHlp->pfnLockIsOwner);
1666 AssertPtr(pThisCC->pIoApicHlp->pfnIommuMsiRemap);
1667
1668 /*
1669 * Register MMIO region.
1670 */
1671 rc = PDMDevHlpMmioCreateAndMap(pDevIns, IOAPIC_MMIO_BASE_PHYSADDR, IOAPIC_MMIO_SIZE, ioapicMmioWrite, ioapicMmioRead,
1672 IOMMMIO_FLAGS_READ_DWORD | IOMMMIO_FLAGS_WRITE_DWORD_ZEROED, "I/O APIC", &pThis->hMmio);
1673 AssertRCReturn(rc, rc);
1674
1675 /*
1676 * Register the saved state.
1677 */
1678 rc = PDMDevHlpSSMRegister(pDevIns, IOAPIC_SAVED_STATE_VERSION, sizeof(*pThis), ioapicR3SaveExec, ioapicR3LoadExec);
1679 AssertRCReturn(rc, rc);
1680
1681 /*
1682 * Register debugger info item.
1683 */
1684 rc = PDMDevHlpDBGFInfoRegister(pDevIns, "ioapic", "Display IO APIC state.", ioapicR3DbgInfo);
1685 AssertRCReturn(rc, rc);
1686
1687 /*
1688 * Register debugger register access.
1689 */
1690 rc = PDMDevHlpDBGFRegRegister(pDevIns, g_aRegDesc);
1691 AssertRCReturn(rc, rc);
1692
1693# ifdef VBOX_WITH_STATISTICS
1694 /*
1695 * Statistics.
1696 */
1697 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadRZ, STAMTYPE_COUNTER, "RZ/MmioRead", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in RZ.");
1698 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteRZ, STAMTYPE_COUNTER, "RZ/MmioWrite", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in RZ.");
1699 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetIrqRZ, STAMTYPE_COUNTER, "RZ/SetIrq", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in RZ.");
1700 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetEoiRZ, STAMTYPE_COUNTER, "RZ/SetEoi", STAMUNIT_OCCURENCES, "Number of IOAPIC SetEoi calls in RZ.");
1701
1702 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadR3, STAMTYPE_COUNTER, "R3/MmioRead", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in R3");
1703 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteR3, STAMTYPE_COUNTER, "R3/MmioWrite", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in R3.");
1704 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetIrqR3, STAMTYPE_COUNTER, "R3/SetIrq", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in R3.");
1705 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetEoiR3, STAMTYPE_COUNTER, "R3/SetEoi", STAMUNIT_OCCURENCES, "Number of IOAPIC SetEoi calls in R3.");
1706
1707 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRedundantEdgeIntr, STAMTYPE_COUNTER, "RedundantEdgeIntr", STAMUNIT_OCCURENCES, "Number of redundant edge-triggered interrupts (no IRR change).");
1708 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRedundantLevelIntr, STAMTYPE_COUNTER, "RedundantLevelIntr", STAMUNIT_OCCURENCES, "Number of redundant level-triggered interrupts (no IRR change).");
1709 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSuppressedLevelIntr, STAMTYPE_COUNTER, "SuppressedLevelIntr", STAMUNIT_OCCURENCES, "Number of suppressed level-triggered interrupts by remote IRR.");
1710
1711 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIommuRemappedIntr, STAMTYPE_COUNTER, "Iommu/RemappedIntr", STAMUNIT_OCCURENCES, "Number of interrupts remapped by the IOMMU.");
1712 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIommuRemappedMsi, STAMTYPE_COUNTER, "Iommu/RemappedMsi", STAMUNIT_OCCURENCES, "Number of MSIs remapped by the IOMMU.");
1713 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIommuDiscardedIntr, STAMTYPE_COUNTER, "Iommu/DiscardedIntr", STAMUNIT_OCCURENCES, "Number of interrupts discarded by the IOMMU.");
1714 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIommuDiscardedMsi, STAMTYPE_COUNTER, "Iommu/DiscardedMsi", STAMUNIT_OCCURENCES, "Number of MSIs discarded by the IOMMU.");
1715
1716 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetRteContention, STAMTYPE_COUNTER, "CritSect/ContentionSetRte", STAMUNIT_OCCURENCES, "Number of times the critsect is busy during RTE writes causing trips to R3.");
1717
1718 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatLevelIrqSent, STAMTYPE_COUNTER, "LevelIntr/Sent", STAMUNIT_OCCURENCES, "Number of level-triggered interrupts sent to the local APIC(s).");
1719 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatEoiReceived, STAMTYPE_COUNTER, "LevelIntr/Recv", STAMUNIT_OCCURENCES, "Number of EOIs received for level-triggered interrupts from the local APIC(s).");
1720
1721 for (size_t i = 0; i < RT_ELEMENTS(pThis->aStatLevelAct); ++i)
1722 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->aStatLevelAct[i], STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Time spent in the level active state", "IntPending/%02x", i);
1723# endif
1724 for (size_t i = 0; i < RT_ELEMENTS(pThis->aStatVectors); i++)
1725 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->aStatVectors[i], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1726 "Number of ioapicSendMsi/pfnApicBusDeliver calls for the vector.", "Vectors/%02x", i);
1727
1728 /*
1729 * Init. the device state.
1730 */
1731 LogRel(("IOAPIC: Version=%d.%d ChipType=%s\n", pThis->u8ApicVer >> 4, pThis->u8ApicVer & 0x0f, szChipType));
1732 ioapicR3Reset(pDevIns);
1733
1734 return VINF_SUCCESS;
1735}
1736
1737#else /* !IN_RING3 */
1738
1739/**
1740 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
1741 */
1742static DECLCALLBACK(int) ioapicRZConstruct(PPDMDEVINS pDevIns)
1743{
1744 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1745 PIOAPIC pThis = PDMDEVINS_2_DATA(pDevIns, PIOAPIC);
1746 PIOAPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOAPICCC);
1747
1748 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
1749 AssertRCReturn(rc, rc);
1750
1751 PDMIOAPICREG IoApicReg;
1752 IoApicReg.u32Version = PDM_IOAPICREG_VERSION;
1753 IoApicReg.pfnSetIrq = ioapicSetIrq;
1754 IoApicReg.pfnSendMsi = ioapicSendMsi;
1755 IoApicReg.pfnSetEoi = ioapicSetEoi;
1756 IoApicReg.u32TheEnd = PDM_IOAPICREG_VERSION;
1757 rc = PDMDevHlpIoApicSetUpContext(pDevIns, &IoApicReg, &pThisCC->pIoApicHlp);
1758 AssertRCReturn(rc, rc);
1759 AssertPtr(pThisCC->pIoApicHlp->pfnApicBusDeliver);
1760 AssertPtr(pThisCC->pIoApicHlp->pfnLock);
1761 AssertPtr(pThisCC->pIoApicHlp->pfnUnlock);
1762 AssertPtr(pThisCC->pIoApicHlp->pfnLockIsOwner);
1763 AssertPtr(pThisCC->pIoApicHlp->pfnIommuMsiRemap);
1764
1765 rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmio, ioapicMmioWrite, ioapicMmioRead, NULL /*pvUser*/);
1766 AssertRCReturn(rc, rc);
1767
1768 return VINF_SUCCESS;
1769}
1770
1771#endif /* !IN_RING3 */
1772
1773/**
1774 * IO APIC device registration structure.
1775 */
1776const PDMDEVREG g_DeviceIOAPIC =
1777{
1778 /* .u32Version = */ PDM_DEVREG_VERSION,
1779 /* .uReserved0 = */ 0,
1780 /* .szName = */ "ioapic",
1781 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE
1782 | PDM_DEVREG_FLAGS_REQUIRE_R0 | PDM_DEVREG_FLAGS_REQUIRE_RC,
1783 /* .fClass = */ PDM_DEVREG_CLASS_PIC,
1784 /* .cMaxInstances = */ 1,
1785 /* .uSharedVersion = */ 42,
1786 /* .cbInstanceShared = */ sizeof(IOAPIC),
1787 /* .cbInstanceCC = */ sizeof(IOAPICCC),
1788 /* .cbInstanceRC = */ sizeof(IOAPICRC),
1789 /* .cMaxPciDevices = */ 0,
1790 /* .cMaxMsixVectors = */ 0,
1791 /* .pszDescription = */ "I/O Advanced Programmable Interrupt Controller (IO-APIC) Device",
1792#if defined(IN_RING3)
1793 /* .pszRCMod = */ "VBoxDDRC.rc",
1794 /* .pszR0Mod = */ "VBoxDDR0.r0",
1795 /* .pfnConstruct = */ ioapicR3Construct,
1796 /* .pfnDestruct = */ ioapicR3Destruct,
1797 /* .pfnRelocate = */ ioapicR3Relocate,
1798 /* .pfnMemSetup = */ NULL,
1799 /* .pfnPowerOn = */ NULL,
1800 /* .pfnReset = */ ioapicR3Reset,
1801 /* .pfnSuspend = */ NULL,
1802 /* .pfnResume = */ NULL,
1803 /* .pfnAttach = */ NULL,
1804 /* .pfnDetach = */ NULL,
1805 /* .pfnQueryInterface = */ NULL,
1806 /* .pfnInitComplete = */ NULL,
1807 /* .pfnPowerOff = */ NULL,
1808 /* .pfnSoftReset = */ NULL,
1809 /* .pfnReserved0 = */ NULL,
1810 /* .pfnReserved1 = */ NULL,
1811 /* .pfnReserved2 = */ NULL,
1812 /* .pfnReserved3 = */ NULL,
1813 /* .pfnReserved4 = */ NULL,
1814 /* .pfnReserved5 = */ NULL,
1815 /* .pfnReserved6 = */ NULL,
1816 /* .pfnReserved7 = */ NULL,
1817#elif defined(IN_RING0)
1818 /* .pfnEarlyConstruct = */ NULL,
1819 /* .pfnConstruct = */ ioapicRZConstruct,
1820 /* .pfnDestruct = */ NULL,
1821 /* .pfnFinalDestruct = */ NULL,
1822 /* .pfnRequest = */ NULL,
1823 /* .pfnReserved0 = */ NULL,
1824 /* .pfnReserved1 = */ NULL,
1825 /* .pfnReserved2 = */ NULL,
1826 /* .pfnReserved3 = */ NULL,
1827 /* .pfnReserved4 = */ NULL,
1828 /* .pfnReserved5 = */ NULL,
1829 /* .pfnReserved6 = */ NULL,
1830 /* .pfnReserved7 = */ NULL,
1831#elif defined(IN_RC)
1832 /* .pfnConstruct = */ ioapicRZConstruct,
1833 /* .pfnReserved0 = */ NULL,
1834 /* .pfnReserved1 = */ NULL,
1835 /* .pfnReserved2 = */ NULL,
1836 /* .pfnReserved3 = */ NULL,
1837 /* .pfnReserved4 = */ NULL,
1838 /* .pfnReserved5 = */ NULL,
1839 /* .pfnReserved6 = */ NULL,
1840 /* .pfnReserved7 = */ NULL,
1841#else
1842# error "Not in IN_RING3, IN_RING0 or IN_RC!"
1843#endif
1844 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
1845};
1846
1847
1848#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
1849
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