VirtualBox

source: vbox/trunk/src/VBox/Devices/Bus/DevPciIch9.cpp@ 51591

Last change on this file since 51591 was 51485, checked in by vboxsync, 11 years ago

ICH9: Reset more, improved debug info output.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 110.4 KB
Line 
1/* $Id: DevPciIch9.cpp 51485 2014-06-02 11:19:19Z vboxsync $ */
2/** @file
3 * DevPCI - ICH9 southbridge PCI bus emulation device.
4 *
5 * @note bird: I've cleaned up DevPCI.cpp to some extend, this file has not
6 * be cleaned up and because of pending code merge.
7 */
8
9/*
10 * Copyright (C) 2010-2013 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 */
20
21/*******************************************************************************
22* Header Files *
23*******************************************************************************/
24#define LOG_GROUP LOG_GROUP_DEV_PCI
25/* Hack to get PCIDEVICEINT declared at the right point - include "PCIInternal.h". */
26#define PCI_INCLUDE_PRIVATE
27#define PCIBus ICH9PCIBus
28#include <VBox/pci.h>
29#include <VBox/msi.h>
30#include <VBox/vmm/pdmdev.h>
31#include <iprt/asm.h>
32#include <iprt/assert.h>
33#include <iprt/string.h>
34#ifdef IN_RING3
35#include <iprt/alloc.h>
36#endif
37
38#include "VBoxDD.h"
39#include "MsiCommon.h"
40
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45/**
46 * PCI Bus instance.
47 */
48typedef struct ICH9PCIBus
49{
50 /** Bus number. */
51 int32_t iBus;
52 /** Number of bridges attached to the bus. */
53 uint32_t cBridges;
54
55 /** Array of PCI devices. We assume 32 slots, each with 8 functions. */
56 R3PTRTYPE(PPCIDEVICE) apDevices[256];
57 /** Array of bridges attached to the bus. */
58 R3PTRTYPE(PPCIDEVICE *) papBridgesR3;
59
60 /** R3 pointer to the device instance. */
61 PPDMDEVINSR3 pDevInsR3;
62 /** Pointer to the PCI R3 helpers. */
63 PCPDMPCIHLPR3 pPciHlpR3;
64
65 /** R0 pointer to the device instance. */
66 PPDMDEVINSR0 pDevInsR0;
67 /** Pointer to the PCI R0 helpers. */
68 PCPDMPCIHLPR0 pPciHlpR0;
69
70 /** RC pointer to the device instance. */
71 PPDMDEVINSRC pDevInsRC;
72 /** Pointer to the PCI RC helpers. */
73 PCPDMPCIHLPRC pPciHlpRC;
74
75 /** The PCI device for the PCI bridge. */
76 PCIDEVICE aPciDev;
77
78} ICH9PCIBUS, *PICH9PCIBUS;
79
80
81/** @def PCI_APIC_IRQ_PINS
82 * Number of pins for interrupts if the APIC is used.
83 */
84#define PCI_APIC_IRQ_PINS 8
85
86/**
87 * PCI Globals - This is the host-to-pci bridge and the root bus.
88 */
89typedef struct
90{
91 /** R3 pointer to the device instance. */
92 PPDMDEVINSR3 pDevInsR3;
93 /** R0 pointer to the device instance. */
94 PPDMDEVINSR0 pDevInsR0;
95 /** RC pointer to the device instance. */
96 PPDMDEVINSRC pDevInsRC;
97
98#if HC_ARCH_BITS == 64
99 uint32_t Alignment0;
100#endif
101
102 /** Value latched in Configuration Address Port (0CF8h) */
103 uint32_t uConfigReg;
104
105 /** I/O APIC irq levels */
106 volatile uint32_t uaPciApicIrqLevels[PCI_APIC_IRQ_PINS];
107
108#if 1 /* Will be moved into the BIOS soon. */
109 /** The next I/O port address which the PCI BIOS will use. */
110 uint32_t uPciBiosIo;
111 /** The next MMIO address which the PCI BIOS will use. */
112 uint32_t uPciBiosMmio;
113 /** Actual bus number. */
114 uint8_t uBus;
115#endif
116 /** Physical address of PCI config space MMIO region. */
117 uint64_t u64PciConfigMMioAddress;
118 /** Length of PCI config space MMIO region. */
119 uint64_t u64PciConfigMMioLength;
120
121 /** PCI bus which is attached to the host-to-PCI bridge. */
122 ICH9PCIBUS aPciBus;
123} ICH9PCIGLOBALS, *PICH9PCIGLOBALS;
124
125
126/**
127 * PCI configuration space address.
128 */
129typedef struct
130{
131 uint8_t iBus;
132 uint8_t iDeviceFunc;
133 uint16_t iRegister;
134} PciAddress;
135
136#ifndef VBOX_DEVICE_STRUCT_TESTCASE
137
138/*******************************************************************************
139* Defined Constants And Macros *
140*******************************************************************************/
141
142/** @def VBOX_ICH9PCI_SAVED_STATE_VERSION
143 * Saved state version of the ICH9 PCI bus device.
144 */
145#define VBOX_ICH9PCI_SAVED_STATE_VERSION_NOMSI 1
146#define VBOX_ICH9PCI_SAVED_STATE_VERSION_MSI 2
147#define VBOX_ICH9PCI_SAVED_STATE_VERSION_CURRENT VBOX_ICH9PCI_SAVED_STATE_VERSION_MSI
148
149/** Converts a bus instance pointer to a device instance pointer. */
150#define PCIBUS_2_DEVINS(pPciBus) ((pPciBus)->CTX_SUFF(pDevIns))
151/** Converts a device instance pointer to a ICH9PCIGLOBALS pointer. */
152#define DEVINS_2_PCIGLOBALS(pDevIns) ((PICH9PCIGLOBALS)(PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS)))
153/** Converts a device instance pointer to a PCIBUS pointer. */
154#define DEVINS_2_PCIBUS(pDevIns) ((PICH9PCIBUS)(&PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS)->aPciBus))
155/** Converts a pointer to a PCI root bus instance to a PCIGLOBALS pointer. */
156#define PCIROOTBUS_2_PCIGLOBALS(pPciBus) ( (PICH9PCIGLOBALS)((uintptr_t)(pPciBus) - RT_OFFSETOF(ICH9PCIGLOBALS, aPciBus)) )
157
158/** @def PCI_LOCK
159 * Acquires the PDM lock. This is a NOP if locking is disabled. */
160/** @def PCI_UNLOCK
161 * Releases the PDM lock. This is a NOP if locking is disabled. */
162#define PCI_LOCK(pDevIns, rc) \
163 do { \
164 int rc2 = DEVINS_2_PCIBUS(pDevIns)->CTX_SUFF(pPciHlp)->pfnLock((pDevIns), rc); \
165 if (rc2 != VINF_SUCCESS) \
166 return rc2; \
167 } while (0)
168#define PCI_UNLOCK(pDevIns) \
169 DEVINS_2_PCIBUS(pDevIns)->CTX_SUFF(pPciHlp)->pfnUnlock(pDevIns)
170
171/* Prototypes */
172static void ich9pciSetIrqInternal(PICH9PCIGLOBALS pGlobals, uint8_t uDevFn, PPCIDEVICE pPciDev,
173 int iIrq, int iLevel, uint32_t uTagSrc);
174#ifdef IN_RING3
175static void ich9pcibridgeReset(PPDMDEVINS pDevIns);
176static int ich9pciRegisterInternal(PICH9PCIBUS pBus, int iDev, PPCIDEVICE pPciDev, const char *pszName);
177static void ich9pciUpdateMappings(PCIDevice *pDev);
178static DECLCALLBACK(uint32_t) ich9pciConfigReadDev(PCIDevice *aDev, uint32_t u32Address, unsigned len);
179DECLINLINE(PPCIDEVICE) ich9pciFindBridge(PICH9PCIBUS pBus, uint8_t iBus);
180static void ich9pciBiosInitDevice(PICH9PCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn);
181#endif
182
183// See 7.2.2. PCI Express Enhanced Configuration Mechanism for details of address
184// mapping, we take n=6 approach
185DECLINLINE(void) ich9pciPhysToPciAddr(PICH9PCIGLOBALS pGlobals, RTGCPHYS GCPhysAddr, PciAddress* pPciAddr)
186{
187 NOREF(pGlobals);
188 pPciAddr->iBus = (GCPhysAddr >> 20) & ((1<<6) - 1);
189 pPciAddr->iDeviceFunc = (GCPhysAddr >> 12) & ((1<<(5+3)) - 1); // 5 bits - device, 3 bits - function
190 pPciAddr->iRegister = (GCPhysAddr >> 0) & ((1<<(6+4+2)) - 1); // 6 bits - register, 4 bits - extended register, 2 bits -Byte Enable
191}
192
193DECLINLINE(void) ich9pciStateToPciAddr(PICH9PCIGLOBALS pGlobals, RTGCPHYS addr, PciAddress* pPciAddr)
194{
195 pPciAddr->iBus = (pGlobals->uConfigReg >> 16) & 0xff;
196 pPciAddr->iDeviceFunc = (pGlobals->uConfigReg >> 8) & 0xff;
197 pPciAddr->iRegister = (pGlobals->uConfigReg & 0xfc) | (addr & 3);
198}
199
200PDMBOTHCBDECL(void) ich9pciSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel, uint32_t uTagSrc)
201{
202 LogFlowFunc(("invoked by %p/%d: iIrq=%d iLevel=%d uTagSrc=%#x\n", pDevIns, pDevIns->iInstance, iIrq, iLevel, uTagSrc));
203 ich9pciSetIrqInternal(PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS), pPciDev->devfn, pPciDev, iIrq, iLevel, uTagSrc);
204}
205
206PDMBOTHCBDECL(void) ich9pcibridgeSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel, uint32_t uTagSrc)
207{
208 /*
209 * The PCI-to-PCI bridge specification defines how the interrupt pins
210 * are routed from the secondary to the primary bus (see chapter 9).
211 * iIrq gives the interrupt pin the pci device asserted.
212 * We change iIrq here according to the spec and call the SetIrq function
213 * of our parent passing the device which asserted the interrupt instead of the device of the bridge.
214 */
215 PICH9PCIBUS pBus = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
216 PPCIDEVICE pPciDevBus = pPciDev;
217 int iIrqPinBridge = iIrq;
218 uint8_t uDevFnBridge = 0;
219
220 /* Walk the chain until we reach the host bus. */
221 do
222 {
223 uDevFnBridge = pBus->aPciDev.devfn;
224 iIrqPinBridge = ((pPciDevBus->devfn >> 3) + iIrqPinBridge) & 3;
225
226 /* Get the parent. */
227 pBus = pBus->aPciDev.Int.s.CTX_SUFF(pBus);
228 pPciDevBus = &pBus->aPciDev;
229 } while (pBus->iBus != 0);
230
231 AssertMsgReturnVoid(pBus->iBus == 0, ("This is not the host pci bus iBus=%d\n", pBus->iBus));
232 ich9pciSetIrqInternal(PCIROOTBUS_2_PCIGLOBALS(pBus), uDevFnBridge, pPciDev, iIrqPinBridge, iLevel, uTagSrc);
233}
234
235
236/**
237 * Port I/O Handler for PCI address OUT operations.
238 *
239 * Emulates writes to Configuration Address Port at 0CF8h for
240 * Configuration Mechanism #1.
241 *
242 * @returns VBox status code.
243 *
244 * @param pDevIns ICH9 device instance.
245 * @param pvUser User argument - ignored.
246 * @param uPort Port number used for the OUT operation.
247 * @param u32 The value to output.
248 * @param cb The value size in bytes.
249 */
250PDMBOTHCBDECL(int) ich9pciIOPortAddressWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
251{
252 LogFlow(("ich9pciIOPortAddressWrite: Port=%#x u32=%#x cb=%d\n", Port, u32, cb));
253 NOREF(pvUser);
254 if (cb == 4)
255 {
256 PICH9PCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
257
258 /*
259 * bits [1:0] are hard-wired, read-only and must return zeroes
260 * when read.
261 */
262 u32 &= ~3;
263
264 PCI_LOCK(pDevIns, VINF_IOM_R3_IOPORT_WRITE);
265 pThis->uConfigReg = u32;
266 PCI_UNLOCK(pDevIns);
267 }
268
269 return VINF_SUCCESS;
270}
271
272
273/**
274 * Port I/O Handler for PCI address IN operations.
275 *
276 * Emulates reads from Configuration Address Port at 0CF8h for
277 * Configuration Mechanism #1.
278 *
279 * @returns VBox status code.
280 *
281 * @param pDevIns ICH9 device instance.
282 * @param pvUser User argument - ignored.
283 * @param uPort Port number used for the IN operation.
284 * @param pu32 Where to store the result.
285 * @param cb Number of bytes read.
286 */
287PDMBOTHCBDECL(int) ich9pciIOPortAddressRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
288{
289 NOREF(pvUser);
290 if (cb == 4)
291 {
292 PICH9PCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
293
294 PCI_LOCK(pDevIns, VINF_IOM_R3_IOPORT_READ);
295 *pu32 = pThis->uConfigReg;
296 PCI_UNLOCK(pDevIns);
297
298 LogFlow(("ich9pciIOPortAddressRead: Port=%#x cb=%d -> %#x\n", Port, cb, *pu32));
299 return VINF_SUCCESS;
300 }
301
302 Log(("ich9pciIOPortAddressRead: Port=%#x cb=%d VERR_IOM_IOPORT_UNUSED\n", Port, cb));
303 return VERR_IOM_IOPORT_UNUSED;
304}
305
306
307/*
308 * Perform configuration space write.
309 */
310static int ich9pciDataWriteAddr(PICH9PCIGLOBALS pGlobals, PciAddress* pAddr,
311 uint32_t val, int cb, int rcReschedule)
312{
313 int rc = VINF_SUCCESS;
314#ifdef IN_RING3
315 NOREF(rcReschedule);
316#endif
317
318 if (pAddr->iBus != 0) /* forward to subordinate bus */
319 {
320 if (pGlobals->aPciBus.cBridges)
321 {
322#ifdef IN_RING3 /** @todo do lookup in R0/RC too! */
323 PPCIDEVICE pBridgeDevice = ich9pciFindBridge(&pGlobals->aPciBus, pAddr->iBus);
324 if (pBridgeDevice)
325 {
326 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigWrite);
327 pBridgeDevice->Int.s.pfnBridgeConfigWrite(pBridgeDevice->pDevIns, pAddr->iBus, pAddr->iDeviceFunc,
328 pAddr->iRegister, val, cb);
329 }
330#else
331 rc = rcReschedule;
332#endif
333 }
334 }
335 else /* forward to directly connected device */
336 {
337 R3PTRTYPE(PCIDevice *) aDev = pGlobals->aPciBus.apDevices[pAddr->iDeviceFunc];
338 if (aDev)
339 {
340#ifdef IN_RING3
341 aDev->Int.s.pfnConfigWrite(aDev, pAddr->iRegister, val, cb);
342#else
343 rc = rcReschedule;
344#endif
345 }
346 }
347
348 Log2(("ich9pciDataWriteAddr: %02x:%02x:%02x reg %x(%d) %x %Rrc\n",
349 pAddr->iBus, pAddr->iDeviceFunc >> 3, pAddr->iDeviceFunc & 0x7, pAddr->iRegister,
350 cb, val, rc));
351 return rc;
352}
353
354
355/*
356 * Decode value latched in Configuration Address Port and perform
357 * requsted write to the target configuration space register.
358 *
359 * XXX: This code should be probably moved to its only caller
360 * (ich9pciIOPortDataWrite) to avoid prolifiration of confusingly
361 * similarly named functions.
362 */
363static int ich9pciDataWrite(PICH9PCIGLOBALS pGlobals, uint32_t addr, uint32_t val, int len)
364{
365 LogFlow(("ich9pciDataWrite: config=%08x val=%08x len=%d\n", pGlobals->uConfigReg, val, len));
366
367 /* Configuration space mapping enabled? */
368 if (!(pGlobals->uConfigReg & (1 << 31)))
369 return VINF_SUCCESS;
370
371 /* Decode target device and configuration space register */
372 PciAddress aPciAddr;
373 ich9pciStateToPciAddr(pGlobals, addr, &aPciAddr);
374
375 /* Perform configuration space write */
376 return ich9pciDataWriteAddr(pGlobals, &aPciAddr, val, len, VINF_IOM_R3_IOPORT_WRITE);
377}
378
379
380/**
381 * Port I/O Handler for PCI data OUT operations.
382 *
383 * Emulates writes to Configuration Data Port at 0CFCh for
384 * Configuration Mechanism #1.
385 *
386 * @returns VBox status code.
387 *
388 * @param pDevIns ICH9 device instance.
389 * @param pvUser User argument - ignored.
390 * @param uPort Port number used for the OUT operation.
391 * @param u32 The value to output.
392 * @param cb The value size in bytes.
393 */
394PDMBOTHCBDECL(int) ich9pciIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
395{
396 LogFlow(("ich9pciIOPortDataWrite: Port=%#x u32=%#x cb=%d\n", Port, u32, cb));
397 NOREF(pvUser);
398 int rc = VINF_SUCCESS;
399 if (!(Port % cb))
400 {
401 PICH9PCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
402
403 PCI_LOCK(pDevIns, VINF_IOM_R3_IOPORT_WRITE);
404 rc = ich9pciDataWrite(pThis, Port, u32, cb);
405 PCI_UNLOCK(pDevIns);
406 }
407 else
408 AssertMsgFailed(("Unaligned write to port %#x u32=%#x cb=%d\n", Port, u32, cb));
409 return rc;
410}
411
412
413static void ich9pciNoMem(void* ptr, int cb)
414{
415 for (int i = 0; i < cb; i++)
416 ((uint8_t*)ptr)[i] = 0xff;
417}
418
419
420/*
421 * Perform configuration space read.
422 */
423static int ich9pciDataReadAddr(PICH9PCIGLOBALS pGlobals, PciAddress* pPciAddr, int cb,
424 uint32_t *pu32, int rcReschedule)
425{
426 int rc = VINF_SUCCESS;
427#ifdef IN_RING3
428 NOREF(rcReschedule);
429#endif
430
431 if (pPciAddr->iBus != 0) /* forward to subordinate bus */
432 {
433 if (pGlobals->aPciBus.cBridges)
434 {
435#ifdef IN_RING3 /** @todo do lookup in R0/RC too! */
436 PPCIDEVICE pBridgeDevice = ich9pciFindBridge(&pGlobals->aPciBus, pPciAddr->iBus);
437 if (pBridgeDevice)
438 {
439 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigRead);
440 *pu32 = pBridgeDevice->Int.s.pfnBridgeConfigRead(pBridgeDevice->pDevIns, pPciAddr->iBus, pPciAddr->iDeviceFunc, pPciAddr->iRegister, cb);
441 }
442 else
443 ich9pciNoMem(pu32, cb);
444#else
445 rc = rcReschedule;
446#endif
447 }
448 else
449 ich9pciNoMem(pu32, cb);
450 }
451 else /* forward to directly connected device */
452 {
453 R3PTRTYPE(PCIDevice *) aDev = pGlobals->aPciBus.apDevices[pPciAddr->iDeviceFunc];
454 if (aDev)
455 {
456#ifdef IN_RING3
457 *pu32 = aDev->Int.s.pfnConfigRead(aDev, pPciAddr->iRegister, cb);
458#else
459 rc = rcReschedule;
460#endif
461 }
462 else
463 ich9pciNoMem(pu32, cb);
464 }
465
466 Log3(("ich9pciDataReadAddr: %02x:%02x:%02x reg %x(%d) gave %x %Rrc\n",
467 pPciAddr->iBus, pPciAddr->iDeviceFunc >> 3, pPciAddr->iDeviceFunc & 0x7, pPciAddr->iRegister,
468 cb, *pu32, rc));
469 return rc;
470}
471
472
473/*
474 * Decode value latched in Configuration Address Port and perform
475 * requsted read from the target configuration space register.
476 *
477 * XXX: This code should be probably moved to its only caller
478 * (ich9pciIOPortDataRead) to avoid prolifiration of confusingly
479 * similarly named functions.
480 */
481static int ich9pciDataRead(PICH9PCIGLOBALS pGlobals, uint32_t addr, int cb, uint32_t *pu32)
482{
483 LogFlow(("ich9pciDataRead: config=%x cb=%d\n", pGlobals->uConfigReg, cb));
484
485 *pu32 = 0xffffffff;
486
487 /* Configuration space mapping enabled? */
488 if (!(pGlobals->uConfigReg & (1 << 31)))
489 return VINF_SUCCESS;
490
491 /* Decode target device and configuration space register */
492 PciAddress aPciAddr;
493 ich9pciStateToPciAddr(pGlobals, addr, &aPciAddr);
494
495 /* Perform configuration space read */
496 return ich9pciDataReadAddr(pGlobals, &aPciAddr, cb, pu32, VINF_IOM_R3_IOPORT_READ);
497}
498
499
500/**
501 * Port I/O Handler for PCI data IN operations.
502 *
503 * Emulates reads from Configuration Data Port at 0CFCh for
504 * Configuration Mechanism #1.
505 *
506 * @returns VBox status code.
507 *
508 * @param pDevIns ICH9 device instance.
509 * @param pvUser User argument - ignored.
510 * @param uPort Port number used for the IN operation.
511 * @param pu32 Where to store the result.
512 * @param cb Number of bytes read.
513 */
514PDMBOTHCBDECL(int) ich9pciIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
515{
516 NOREF(pvUser);
517 if (!(Port % cb))
518 {
519 PICH9PCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
520
521 PCI_LOCK(pDevIns, VINF_IOM_R3_IOPORT_READ);
522 int rc = ich9pciDataRead(pThis, Port, cb, pu32);
523 PCI_UNLOCK(pDevIns);
524
525 LogFlow(("ich9pciIOPortDataRead: Port=%#x cb=%#x -> %#x (%Rrc)\n", Port, cb, *pu32, rc));
526 return rc;
527 }
528 AssertMsgFailed(("Unaligned read from port %#x cb=%d\n", Port, cb));
529 return VERR_IOM_IOPORT_UNUSED;
530}
531
532
533/* Compute mapping of PCI slot and IRQ number to APIC interrupt line */
534DECLINLINE(int) ich9pciSlot2ApicIrq(uint8_t uSlot, int irq_num)
535{
536 return (irq_num + uSlot) & 7;
537}
538
539/* return the global irq number corresponding to a given device irq
540 pin. We could also use the bus number to have a more precise
541 mapping. This is the implementation note described in the PCI spec chapter 2.2.6 */
542DECLINLINE(int) ich9pciSlotGetPirq(uint8_t uBus, uint8_t uDevFn, int iIrqNum)
543{
544 NOREF(uBus);
545 int iSlotAddend = (uDevFn >> 3) - 1;
546 return (iIrqNum + iSlotAddend) & 3;
547}
548
549/* irqs corresponding to PCI irqs A-D, must match pci_irq_list in rombios.c */
550static const uint8_t aPciIrqs[4] = { 11, 10, 9, 5 };
551
552/* Add one more level up request on APIC input line */
553DECLINLINE(void) ich9pciApicLevelUp(PICH9PCIGLOBALS pGlobals, int irq_num)
554{
555 ASMAtomicIncU32(&pGlobals->uaPciApicIrqLevels[irq_num]);
556}
557
558/* Remove one level up request on APIC input line */
559DECLINLINE(void) ich9pciApicLevelDown(PICH9PCIGLOBALS pGlobals, int irq_num)
560{
561 ASMAtomicDecU32(&pGlobals->uaPciApicIrqLevels[irq_num]);
562}
563
564static void ich9pciApicSetIrq(PICH9PCIBUS pBus, uint8_t uDevFn, PCIDevice *pPciDev, int irq_num1, int iLevel,
565 uint32_t uTagSrc, int iForcedIrq)
566{
567 /* This is only allowed to be called with a pointer to the root bus. */
568 AssertMsg(pBus->iBus == 0, ("iBus=%u\n", pBus->iBus));
569
570 if (iForcedIrq == -1)
571 {
572 int apic_irq, apic_level;
573 PICH9PCIGLOBALS pGlobals = PCIROOTBUS_2_PCIGLOBALS(pBus);
574 int irq_num = ich9pciSlot2ApicIrq(uDevFn >> 3, irq_num1);
575
576 if ((iLevel & PDM_IRQ_LEVEL_HIGH) == PDM_IRQ_LEVEL_HIGH)
577 ich9pciApicLevelUp(pGlobals, irq_num);
578 else if ((iLevel & PDM_IRQ_LEVEL_HIGH) == PDM_IRQ_LEVEL_LOW)
579 ich9pciApicLevelDown(pGlobals, irq_num);
580
581 apic_irq = irq_num + 0x10;
582 apic_level = pGlobals->uaPciApicIrqLevels[irq_num] != 0;
583 Log3(("ich9pciApicSetIrq: %s: irq_num1=%d level=%d apic_irq=%d apic_level=%d irq_num1=%d uTagSrc=%#x\n",
584 R3STRING(pPciDev->name), irq_num1, iLevel, apic_irq, apic_level, irq_num, uTagSrc));
585 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), apic_irq, apic_level, uTagSrc);
586
587 if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP)
588 {
589 /*
590 * we raised it few lines above, as PDM_IRQ_LEVEL_FLIP_FLOP has
591 * PDM_IRQ_LEVEL_HIGH bit set
592 */
593 ich9pciApicLevelDown(pGlobals, irq_num);
594 pPciDev->Int.s.uIrqPinState = PDM_IRQ_LEVEL_LOW;
595 apic_level = pGlobals->uaPciApicIrqLevels[irq_num] != 0;
596 Log3(("ich9pciApicSetIrq: %s: irq_num1=%d level=%d apic_irq=%d apic_level=%d irq_num1=%d uTagSrc=%#x (flop)\n",
597 R3STRING(pPciDev->name), irq_num1, iLevel, apic_irq, apic_level, irq_num, uTagSrc));
598 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), apic_irq, apic_level, uTagSrc);
599 }
600 } else {
601 Log3(("ich9pciApicSetIrq: (forced) %s: irq_num1=%d level=%d acpi_irq=%d uTagSrc=%#x\n",
602 R3STRING(pPciDev->name), irq_num1, iLevel, iForcedIrq, uTagSrc));
603 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), iForcedIrq, iLevel, uTagSrc);
604 }
605}
606
607static void ich9pciSetIrqInternal(PICH9PCIGLOBALS pGlobals, uint8_t uDevFn, PPCIDEVICE pPciDev,
608 int iIrq, int iLevel, uint32_t uTagSrc)
609{
610
611 if (PCIDevIsIntxDisabled(pPciDev))
612 {
613 if (MsiIsEnabled(pPciDev))
614 {
615 LogFlowFunc(("PCI Dev %p : MSI\n", pPciDev));
616 PPDMDEVINS pDevIns = pGlobals->aPciBus.CTX_SUFF(pDevIns);
617 MsiNotify(pDevIns, pGlobals->aPciBus.CTX_SUFF(pPciHlp), pPciDev, iIrq, iLevel, uTagSrc);
618 }
619
620 if (MsixIsEnabled(pPciDev))
621 {
622 LogFlowFunc(("PCI Dev %p : MSI-X\n", pPciDev));
623 PPDMDEVINS pDevIns = pGlobals->aPciBus.CTX_SUFF(pDevIns);
624 MsixNotify(pDevIns, pGlobals->aPciBus.CTX_SUFF(pPciHlp), pPciDev, iIrq, iLevel, uTagSrc);
625 }
626 return;
627 }
628
629 PICH9PCIBUS pBus = &pGlobals->aPciBus;
630 const bool fIsAcpiDevice = PCIDevGetDeviceId(pPciDev) == 0x7113;
631
632 LogFlowFunc(("PCI Dev %p : IRQ\n", pPciDev));
633 /* Check if the state changed. */
634 if (pPciDev->Int.s.uIrqPinState != iLevel)
635 {
636 pPciDev->Int.s.uIrqPinState = (iLevel & PDM_IRQ_LEVEL_HIGH);
637
638 /* Send interrupt to I/O APIC only now. */
639 if (fIsAcpiDevice)
640 /*
641 * ACPI needs special treatment since SCI is hardwired and
642 * should not be affected by PCI IRQ routing tables at the
643 * same time SCI IRQ is shared in PCI sense hence this
644 * kludge (i.e. we fetch the hardwired value from ACPIs
645 * PCI device configuration space).
646 */
647 ich9pciApicSetIrq(pBus, uDevFn, pPciDev, -1, iLevel, uTagSrc, PCIDevGetInterruptLine(pPciDev));
648 else
649 ich9pciApicSetIrq(pBus, uDevFn, pPciDev, iIrq, iLevel, uTagSrc, -1);
650 }
651}
652
653
654/**
655 * Memory mapped I/O Handler for write operations.
656 *
657 * Emulates writes to configuration space.
658 *
659 * @returns VBox status code.
660 *
661 * @param pDevIns The device instance.
662 * @param pvUser User argument.
663 * @param GCPhysAddr Physical address (in GC) where the read starts.
664 * @param pv Where to fetch the result.
665 * @param cb Number of bytes to write.
666 * @remarks Caller enters the device critical section.
667 */
668PDMBOTHCBDECL(int) ich9pciMcfgMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
669{
670 PICH9PCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
671 uint32_t u32 = 0;
672 NOREF(pvUser);
673
674 Log2(("ich9pciMcfgMMIOWrite: %RGp(%d) \n", GCPhysAddr, cb));
675
676 PCI_LOCK(pDevIns, VINF_IOM_R3_MMIO_WRITE);
677
678 /* Decode target device and configuration space register */
679 PciAddress aDest;
680 ich9pciPhysToPciAddr(pGlobals, GCPhysAddr, &aDest);
681
682 switch (cb)
683 {
684 case 1:
685 u32 = *(uint8_t*)pv;
686 break;
687 case 2:
688 u32 = *(uint16_t*)pv;
689 break;
690 case 4:
691 u32 = *(uint32_t*)pv;
692 break;
693 default:
694 Assert(false);
695 break;
696 }
697
698 /* Perform configuration space write */
699 int rc = ich9pciDataWriteAddr(pGlobals, &aDest, u32, cb, VINF_IOM_R3_MMIO_WRITE);
700 PCI_UNLOCK(pDevIns);
701
702 return rc;
703}
704
705
706/**
707 * Memory mapped I/O Handler for read operations.
708 *
709 * Emulates reads from configuration space.
710 *
711 * @returns VBox status code.
712 *
713 * @param pDevIns The device instance.
714 * @param pvUser User argument.
715 * @param GCPhysAddr Physical address (in GC) where the read starts.
716 * @param pv Where to store the result.
717 * @param cb Number of bytes read.
718 * @remarks Caller enters the device critical section.
719 */
720PDMBOTHCBDECL(int) ich9pciMcfgMMIORead (PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
721{
722 PICH9PCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
723 uint32_t rv;
724 NOREF(pvUser);
725
726 LogFlow(("ich9pciMcfgMMIORead: %RGp(%d) \n", GCPhysAddr, cb));
727
728 PCI_LOCK(pDevIns, VINF_IOM_R3_MMIO_READ);
729
730 /* Decode target device and configuration space register */
731 PciAddress aDest;
732 ich9pciPhysToPciAddr(pGlobals, GCPhysAddr, &aDest);
733
734 /* Perform configuration space read */
735 int rc = ich9pciDataReadAddr(pGlobals, &aDest, cb, &rv, VINF_IOM_R3_MMIO_READ);
736
737 if (RT_SUCCESS(rc))
738 {
739 switch (cb)
740 {
741 case 1:
742 *(uint8_t*)pv = (uint8_t)rv;
743 break;
744 case 2:
745 *(uint16_t*)pv = (uint16_t)rv;
746 break;
747 case 4:
748 *(uint32_t*)pv = (uint32_t)rv;
749 break;
750 default:
751 Assert(false);
752 break;
753 }
754 }
755 PCI_UNLOCK(pDevIns);
756
757 return rc;
758}
759
760#ifdef IN_RING3
761
762DECLINLINE(PPCIDEVICE) ich9pciFindBridge(PICH9PCIBUS pBus, uint8_t iBus)
763{
764 /* Search for a fitting bridge. */
765 for (uint32_t iBridge = 0; iBridge < pBus->cBridges; iBridge++)
766 {
767 /*
768 * Examine secondary and subordinate bus number.
769 * If the target bus is in the range we pass the request on to the bridge.
770 */
771 PPCIDEVICE pBridge = pBus->papBridgesR3[iBridge];
772 AssertMsg(pBridge && pciDevIsPci2PciBridge(pBridge),
773 ("Device is not a PCI bridge but on the list of PCI bridges\n"));
774 uint32_t uSecondary = PCIDevGetByte(pBridge, VBOX_PCI_SECONDARY_BUS);
775 uint32_t uSubordinate = PCIDevGetByte(pBridge, VBOX_PCI_SUBORDINATE_BUS);
776 Log3(("ich9pciFindBridge on bus %p, bridge %d: %d in %d..%d\n", pBus, iBridge, iBus, uSecondary, uSubordinate));
777 if (iBus >= uSecondary && iBus <= uSubordinate)
778 return pBridge;
779 }
780
781 /* Nothing found. */
782 return NULL;
783}
784
785static uint32_t ich9pciGetCfg(PCIDevice* aDev, int32_t iRegister, int cb)
786{
787 return aDev->Int.s.pfnConfigRead(aDev, iRegister, cb);
788}
789
790static uint8_t ich9pciGetByte(PCIDevice* aDev, int32_t iRegister)
791{
792 return (uint8_t)ich9pciGetCfg(aDev, iRegister, 1);
793}
794
795static uint16_t ich9pciGetWord(PCIDevice* aDev, int32_t iRegister)
796{
797 return (uint16_t)ich9pciGetCfg(aDev, iRegister, 2);
798}
799
800static uint32_t ich9pciGetDWord(PCIDevice* aDev, int32_t iRegister)
801{
802 return (uint32_t)ich9pciGetCfg(aDev, iRegister, 4);
803}
804
805DECLINLINE(uint32_t) ich9pciGetRegionReg(int iRegion)
806{
807 return (iRegion == VBOX_PCI_ROM_SLOT) ?
808 VBOX_PCI_ROM_ADDRESS : (VBOX_PCI_BASE_ADDRESS_0 + iRegion * 4);
809}
810
811#define INVALID_PCI_ADDRESS ~0U
812
813static int ich9pciUnmapRegion(PPCIDEVICE pDev, int iRegion)
814{
815 PCIIORegion* pRegion = &pDev->Int.s.aIORegions[iRegion];
816 int rc = VINF_SUCCESS;
817 PICH9PCIBUS pBus = pDev->Int.s.CTX_SUFF(pBus);
818
819 Assert (pRegion->size != 0);
820
821 if (pRegion->addr != INVALID_PCI_ADDRESS)
822 {
823 if (pRegion->type & PCI_ADDRESS_SPACE_IO)
824 {
825 /* Port IO */
826 rc = PDMDevHlpIOPortDeregister(pDev->pDevIns, pRegion->addr, pRegion->size);
827 AssertRC(rc);
828 }
829 else
830 {
831 RTGCPHYS GCPhysBase = pRegion->addr;
832 if (pBus->pPciHlpR3->pfnIsMMIO2Base(pBus->pDevInsR3, pDev->pDevIns, GCPhysBase))
833 {
834 /* unmap it. */
835 rc = pRegion->map_func(pDev, iRegion, NIL_RTGCPHYS, pRegion->size, (PCIADDRESSSPACE)(pRegion->type));
836 AssertRC(rc);
837 rc = PDMDevHlpMMIO2Unmap(pDev->pDevIns, iRegion, GCPhysBase);
838 }
839 else
840 rc = PDMDevHlpMMIODeregister(pDev->pDevIns, GCPhysBase, pRegion->size);
841 }
842
843 pRegion->addr = INVALID_PCI_ADDRESS;
844 }
845
846 return rc;
847}
848
849static void ich9pciUpdateMappings(PCIDevice* pDev)
850{
851 uint64_t uLast, uNew;
852
853 int iCmd = ich9pciGetWord(pDev, VBOX_PCI_COMMAND);
854 for (int iRegion = 0; iRegion < PCI_NUM_REGIONS; iRegion++)
855 {
856 PCIIORegion* pRegion = &pDev->Int.s.aIORegions[iRegion];
857 uint32_t uConfigReg = ich9pciGetRegionReg(iRegion);
858 int64_t iRegionSize = pRegion->size;
859 int rc;
860
861 if (iRegionSize == 0)
862 continue;
863
864 bool f64Bit = (pRegion->type & PCI_ADDRESS_SPACE_BAR64) != 0;
865
866 if (pRegion->type & PCI_ADDRESS_SPACE_IO)
867 {
868 /* port IO region */
869 if (iCmd & PCI_COMMAND_IOACCESS)
870 {
871 /* IO access allowed */
872 uNew = ich9pciGetDWord(pDev, uConfigReg);
873 uNew &= ~(iRegionSize - 1);
874 uLast = uNew + iRegionSize - 1;
875 /* only 64K ioports on PC */
876 if (uLast <= uNew || uNew == 0 || uLast >= 0x10000)
877 uNew = INVALID_PCI_ADDRESS;
878 } else
879 uNew = INVALID_PCI_ADDRESS;
880 }
881 else
882 {
883 /* MMIO region */
884 if (iCmd & PCI_COMMAND_MEMACCESS)
885 {
886 uNew = ich9pciGetDWord(pDev, uConfigReg);
887
888 if (f64Bit)
889 {
890 uNew |= ((uint64_t)ich9pciGetDWord(pDev, uConfigReg+4)) << 32;
891 if (uNew > UINT64_C(0x0000010000000000))
892 {
893 /* Workaround for REM being unhapping with mapping very lange 64-bit addresses */
894 Log(("Ignoring too 64-bit BAR: %llx\n", uNew));
895 uNew = INVALID_PCI_ADDRESS;
896 }
897 }
898
899 /* the ROM slot has a specific enable bit */
900 if (iRegion == PCI_ROM_SLOT && !(uNew & 1))
901 uNew = INVALID_PCI_ADDRESS;
902 else
903 {
904 uNew &= ~(iRegionSize - 1);
905 uLast = uNew + iRegionSize - 1;
906 /* NOTE: we do not support wrapping */
907 /* XXX: as we cannot support really dynamic
908 mappings, we handle specific values as invalid
909 mappings. */
910 if (uLast <= uNew || uNew == 0 || uLast == INVALID_PCI_ADDRESS)
911 uNew = INVALID_PCI_ADDRESS;
912 }
913 } else
914 uNew = INVALID_PCI_ADDRESS;
915 }
916 /* now do the real mapping */
917 if (uNew != pRegion->addr)
918 {
919 if (pRegion->addr != INVALID_PCI_ADDRESS)
920 ich9pciUnmapRegion(pDev, iRegion);
921
922 pRegion->addr = uNew;
923 if (pRegion->addr != INVALID_PCI_ADDRESS)
924 {
925
926 /* finally, map the region */
927 rc = pRegion->map_func(pDev, iRegion,
928 pRegion->addr, pRegion->size,
929 (PCIADDRESSSPACE)(pRegion->type));
930 AssertRC(rc);
931 }
932 }
933 }
934}
935
936static DECLCALLBACK(int) ich9pciRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, const char *pszName, int iDev)
937{
938 PICH9PCIBUS pBus = DEVINS_2_PCIBUS(pDevIns);
939
940 /*
941 * Check input.
942 */
943 if ( !pszName
944 || !pPciDev
945 || iDev >= (int)RT_ELEMENTS(pBus->apDevices)
946 )
947 {
948 AssertMsgFailed(("Invalid argument! pszName=%s pPciDev=%p iDev=%d\n", pszName, pPciDev, iDev));
949 return VERR_INVALID_PARAMETER;
950 }
951
952 /*
953 * Register the device.
954 */
955 return ich9pciRegisterInternal(pBus, iDev, pPciDev, pszName);
956}
957
958
959static DECLCALLBACK(int) ich9pciRegisterMsi(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PPDMMSIREG pMsiReg)
960{
961 NOREF(pDevIns);
962 int rc;
963
964 rc = MsiInit(pPciDev, pMsiReg);
965 if (RT_FAILURE(rc))
966 return rc;
967
968 rc = MsixInit(pPciDev->Int.s.CTX_SUFF(pBus)->CTX_SUFF(pPciHlp), pPciDev, pMsiReg);
969 if (RT_FAILURE(rc))
970 return rc;
971
972 return VINF_SUCCESS;
973}
974
975
976static DECLCALLBACK(int) ich9pcibridgeRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, const char *pszName, int iDev)
977{
978
979 PICH9PCIBUS pBus = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
980
981 /*
982 * Check input.
983 */
984 if ( !pszName
985 || !pPciDev
986 || iDev >= (int)RT_ELEMENTS(pBus->apDevices))
987 {
988 AssertMsgFailed(("Invalid argument! pszName=%s pPciDev=%p iDev=%d\n", pszName, pPciDev, iDev));
989 return VERR_INVALID_PARAMETER;
990 }
991
992 /*
993 * Register the device.
994 */
995 return ich9pciRegisterInternal(pBus, iDev, pPciDev, pszName);
996}
997
998static DECLCALLBACK(int) ich9pciIORegionRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iRegion, uint32_t cbRegion, PCIADDRESSSPACE enmType, PFNPCIIOREGIONMAP pfnCallback)
999{
1000 NOREF(pDevIns);
1001
1002 /*
1003 * Validate.
1004 */
1005 AssertMsgReturn( enmType == (PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR32)
1006 || enmType == (PCI_ADDRESS_SPACE_MEM_PREFETCH | PCI_ADDRESS_SPACE_BAR32)
1007 || enmType == (PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64)
1008 || enmType == (PCI_ADDRESS_SPACE_MEM_PREFETCH | PCI_ADDRESS_SPACE_BAR64)
1009 || enmType == PCI_ADDRESS_SPACE_IO
1010 ,
1011 ("Invalid enmType=%#x? Or was this a bitmask after all...\n", enmType),
1012 VERR_INVALID_PARAMETER);
1013 AssertMsgReturn((unsigned)iRegion < PCI_NUM_REGIONS,
1014 ("Invalid iRegion=%d PCI_NUM_REGIONS=%d\n", iRegion, PCI_NUM_REGIONS),
1015 VERR_INVALID_PARAMETER);
1016 int iLastSet = ASMBitLastSetU32(cbRegion);
1017 AssertMsgReturn( iLastSet != 0
1018 && RT_BIT_32(iLastSet - 1) == cbRegion,
1019 ("Invalid cbRegion=%#x iLastSet=%#x (not a power of 2 or 0)\n", cbRegion, iLastSet),
1020 VERR_INVALID_PARAMETER);
1021
1022 Log(("ich9pciIORegionRegister: %s region %d size %d type %x\n",
1023 pPciDev->name, iRegion, cbRegion, enmType));
1024
1025 /* Make sure that we haven't marked this region as continuation of 64-bit region. */
1026 Assert(pPciDev->Int.s.aIORegions[iRegion].type != 0xff);
1027
1028 /*
1029 * Register the I/O region.
1030 */
1031 PPCIIOREGION pRegion = &pPciDev->Int.s.aIORegions[iRegion];
1032 pRegion->addr = INVALID_PCI_ADDRESS;
1033 pRegion->size = cbRegion;
1034 pRegion->type = enmType;
1035 pRegion->map_func = pfnCallback;
1036
1037 if ((enmType & PCI_ADDRESS_SPACE_BAR64) != 0)
1038 {
1039 AssertMsgReturn(iRegion < 4,
1040 ("Region %d cannot be 64-bit\n", iRegion),
1041 VERR_INVALID_PARAMETER);
1042 /* Mark next region as continuation of this one. */
1043 pPciDev->Int.s.aIORegions[iRegion+1].type = 0xff;
1044 }
1045
1046 /* Set type in the PCI config space. */
1047 uint32_t u32Value = ((uint32_t)enmType) & (PCI_ADDRESS_SPACE_IO | PCI_ADDRESS_SPACE_BAR64 | PCI_ADDRESS_SPACE_MEM_PREFETCH);
1048 PCIDevSetDWord(pPciDev, ich9pciGetRegionReg(iRegion), u32Value);
1049
1050 return VINF_SUCCESS;
1051}
1052
1053static DECLCALLBACK(void) ich9pciSetConfigCallbacks(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PFNPCICONFIGREAD pfnRead, PPFNPCICONFIGREAD ppfnReadOld,
1054 PFNPCICONFIGWRITE pfnWrite, PPFNPCICONFIGWRITE ppfnWriteOld)
1055{
1056 NOREF(pDevIns);
1057
1058 if (ppfnReadOld)
1059 *ppfnReadOld = pPciDev->Int.s.pfnConfigRead;
1060 pPciDev->Int.s.pfnConfigRead = pfnRead;
1061
1062 if (ppfnWriteOld)
1063 *ppfnWriteOld = pPciDev->Int.s.pfnConfigWrite;
1064 pPciDev->Int.s.pfnConfigWrite = pfnWrite;
1065}
1066
1067static int ich9pciR3CommonSaveExec(PICH9PCIBUS pBus, PSSMHANDLE pSSM)
1068{
1069 /*
1070 * Iterate thru all the devices.
1071 */
1072 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
1073 {
1074 PPCIDEVICE pDev = pBus->apDevices[i];
1075 if (pDev)
1076 {
1077 /* Device position */
1078 SSMR3PutU32(pSSM, i);
1079 /* PCI config registers */
1080 SSMR3PutMem(pSSM, pDev->config, sizeof(pDev->config));
1081
1082 /* Device flags */
1083 int rc = SSMR3PutU32(pSSM, pDev->Int.s.fFlags);
1084 if (RT_FAILURE(rc))
1085 return rc;
1086
1087 /* IRQ pin state */
1088 rc = SSMR3PutS32(pSSM, pDev->Int.s.uIrqPinState);
1089 if (RT_FAILURE(rc))
1090 return rc;
1091
1092 /* MSI info */
1093 rc = SSMR3PutU8(pSSM, pDev->Int.s.u8MsiCapOffset);
1094 if (RT_FAILURE(rc))
1095 return rc;
1096 rc = SSMR3PutU8(pSSM, pDev->Int.s.u8MsiCapSize);
1097 if (RT_FAILURE(rc))
1098 return rc;
1099
1100 /* MSI-X info */
1101 rc = SSMR3PutU8(pSSM, pDev->Int.s.u8MsixCapOffset);
1102 if (RT_FAILURE(rc))
1103 return rc;
1104 rc = SSMR3PutU8(pSSM, pDev->Int.s.u8MsixCapSize);
1105 if (RT_FAILURE(rc))
1106 return rc;
1107 /* Save MSI-X page state */
1108 if (pDev->Int.s.u8MsixCapOffset != 0)
1109 {
1110 Assert(pDev->Int.s.pMsixPageR3 != NULL);
1111 SSMR3PutMem(pSSM, pDev->Int.s.pMsixPageR3, 0x1000);
1112 if (RT_FAILURE(rc))
1113 return rc;
1114 }
1115 }
1116 }
1117 return SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
1118}
1119
1120static DECLCALLBACK(int) ich9pciR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1121{
1122 PICH9PCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
1123
1124 /*
1125 * Bus state data.
1126 */
1127 SSMR3PutU32(pSSM, pThis->uConfigReg);
1128
1129 /*
1130 * Save IRQ states.
1131 */
1132 for (int i = 0; i < PCI_APIC_IRQ_PINS; i++)
1133 SSMR3PutU32(pSSM, pThis->uaPciApicIrqLevels[i]);
1134
1135 SSMR3PutU32(pSSM, ~0); /* separator */
1136
1137 return ich9pciR3CommonSaveExec(&pThis->aPciBus, pSSM);
1138}
1139
1140
1141static DECLCALLBACK(int) ich9pcibridgeR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1142{
1143 PICH9PCIBUS pThis = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
1144 return ich9pciR3CommonSaveExec(pThis, pSSM);
1145}
1146
1147
1148static void ich9pcibridgeConfigWrite(PPDMDEVINSR3 pDevIns, uint8_t iBus, uint8_t iDevice, uint32_t u32Address, uint32_t u32Value, unsigned cb)
1149{
1150 PICH9PCIBUS pBus = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
1151
1152 LogFlowFunc((": pDevIns=%p iBus=%d iDevice=%d u32Address=%u u32Value=%u cb=%d\n", pDevIns, iBus, iDevice, u32Address, u32Value, cb));
1153
1154 /* If the current bus is not the target bus search for the bus which contains the device. */
1155 if (iBus != PCIDevGetByte(&pBus->aPciDev, VBOX_PCI_SECONDARY_BUS))
1156 {
1157 PPCIDEVICE pBridgeDevice = ich9pciFindBridge(pBus, iBus);
1158 if (pBridgeDevice)
1159 {
1160 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigWrite);
1161 pBridgeDevice->Int.s.pfnBridgeConfigWrite(pBridgeDevice->pDevIns, iBus, iDevice, u32Address, u32Value, cb);
1162 }
1163 }
1164 else
1165 {
1166 /* This is the target bus, pass the write to the device. */
1167 PPCIDEVICE pPciDev = pBus->apDevices[iDevice];
1168 if (pPciDev)
1169 {
1170 Log(("%s: %s: addr=%02x val=%08x len=%d\n", __FUNCTION__, pPciDev->name, u32Address, u32Value, cb));
1171 pPciDev->Int.s.pfnConfigWrite(pPciDev, u32Address, u32Value, cb);
1172 }
1173 }
1174}
1175
1176static uint32_t ich9pcibridgeConfigRead(PPDMDEVINSR3 pDevIns, uint8_t iBus, uint8_t iDevice, uint32_t u32Address, unsigned cb)
1177{
1178 PICH9PCIBUS pBus = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
1179 uint32_t u32Value;
1180
1181 LogFlowFunc((": pDevIns=%p iBus=%d iDevice=%d u32Address=%u cb=%d\n", pDevIns, iBus, iDevice, u32Address, cb));
1182
1183 /* If the current bus is not the target bus search for the bus which contains the device. */
1184 if (iBus != PCIDevGetByte(&pBus->aPciDev, VBOX_PCI_SECONDARY_BUS))
1185 {
1186 PPCIDEVICE pBridgeDevice = ich9pciFindBridge(pBus, iBus);
1187 if (pBridgeDevice)
1188 {
1189 AssertPtr( pBridgeDevice->Int.s.pfnBridgeConfigRead);
1190 u32Value = pBridgeDevice->Int.s.pfnBridgeConfigRead(pBridgeDevice->pDevIns, iBus, iDevice, u32Address, cb);
1191 }
1192 else
1193 ich9pciNoMem(&u32Value, 4);
1194 }
1195 else
1196 {
1197 /* This is the target bus, pass the read to the device. */
1198 PPCIDEVICE pPciDev = pBus->apDevices[iDevice];
1199 if (pPciDev)
1200 {
1201 u32Value = pPciDev->Int.s.pfnConfigRead(pPciDev, u32Address, cb);
1202 Log(("%s: %s: u32Address=%02x u32Value=%08x cb=%d\n", __FUNCTION__, pPciDev->name, u32Address, u32Value, cb));
1203 }
1204 else
1205 ich9pciNoMem(&u32Value, 4);
1206 }
1207
1208 return u32Value;
1209}
1210
1211
1212/**
1213 * Common routine for restoring the config registers of a PCI device.
1214 *
1215 * @param pDev The PCI device.
1216 * @param pbSrcConfig The configuration register values to be loaded.
1217 * @param fIsBridge Whether this is a bridge device or not.
1218 */
1219static void pciR3CommonRestoreConfig(PPCIDEVICE pDev, uint8_t const *pbSrcConfig, bool fIsBridge)
1220{
1221 /*
1222 * This table defines the fields for normal devices and bridge devices, and
1223 * the order in which they need to be restored.
1224 */
1225 static const struct PciField
1226 {
1227 uint8_t off;
1228 uint8_t cb;
1229 uint8_t fWritable;
1230 uint8_t fBridge;
1231 const char *pszName;
1232 } s_aFields[] =
1233 {
1234 /* off,cb,fW,fB, pszName */
1235 { 0x00, 2, 0, 3, "VENDOR_ID" },
1236 { 0x02, 2, 0, 3, "DEVICE_ID" },
1237 { 0x06, 2, 1, 3, "STATUS" },
1238 { 0x08, 1, 0, 3, "REVISION_ID" },
1239 { 0x09, 1, 0, 3, "CLASS_PROG" },
1240 { 0x0a, 1, 0, 3, "CLASS_SUB" },
1241 { 0x0b, 1, 0, 3, "CLASS_BASE" },
1242 { 0x0c, 1, 1, 3, "CACHE_LINE_SIZE" },
1243 { 0x0d, 1, 1, 3, "LATENCY_TIMER" },
1244 { 0x0e, 1, 0, 3, "HEADER_TYPE" },
1245 { 0x0f, 1, 1, 3, "BIST" },
1246 { 0x10, 4, 1, 3, "BASE_ADDRESS_0" },
1247 { 0x14, 4, 1, 3, "BASE_ADDRESS_1" },
1248 { 0x18, 4, 1, 1, "BASE_ADDRESS_2" },
1249 { 0x18, 1, 1, 2, "PRIMARY_BUS" }, // fWritable = ??
1250 { 0x19, 1, 1, 2, "SECONDARY_BUS" }, // fWritable = ??
1251 { 0x1a, 1, 1, 2, "SUBORDINATE_BUS" }, // fWritable = ??
1252 { 0x1b, 1, 1, 2, "SEC_LATENCY_TIMER" }, // fWritable = ??
1253 { 0x1c, 4, 1, 1, "BASE_ADDRESS_3" },
1254 { 0x1c, 1, 1, 2, "IO_BASE" }, // fWritable = ??
1255 { 0x1d, 1, 1, 2, "IO_LIMIT" }, // fWritable = ??
1256 { 0x1e, 2, 1, 2, "SEC_STATUS" }, // fWritable = ??
1257 { 0x20, 4, 1, 1, "BASE_ADDRESS_4" },
1258 { 0x20, 2, 1, 2, "MEMORY_BASE" }, // fWritable = ??
1259 { 0x22, 2, 1, 2, "MEMORY_LIMIT" }, // fWritable = ??
1260 { 0x24, 4, 1, 1, "BASE_ADDRESS_5" },
1261 { 0x24, 2, 1, 2, "PREF_MEMORY_BASE" }, // fWritable = ??
1262 { 0x26, 2, 1, 2, "PREF_MEMORY_LIMIT" }, // fWritable = ??
1263 { 0x28, 4, 1, 1, "CARDBUS_CIS" }, // fWritable = ??
1264 { 0x28, 4, 1, 2, "PREF_BASE_UPPER32" }, // fWritable = ??
1265 { 0x2c, 2, 0, 1, "SUBSYSTEM_VENDOR_ID" },// fWritable = !?
1266 { 0x2c, 4, 1, 2, "PREF_LIMIT_UPPER32" },// fWritable = ??
1267 { 0x2e, 2, 0, 1, "SUBSYSTEM_ID" }, // fWritable = !?
1268 { 0x30, 4, 1, 1, "ROM_ADDRESS" }, // fWritable = ?!
1269 { 0x30, 2, 1, 2, "IO_BASE_UPPER16" }, // fWritable = ?!
1270 { 0x32, 2, 1, 2, "IO_LIMIT_UPPER16" }, // fWritable = ?!
1271 { 0x34, 4, 0, 3, "CAPABILITY_LIST" }, // fWritable = !? cb=!?
1272 { 0x38, 4, 1, 1, "RESERVED_38" }, // ???
1273 { 0x38, 4, 1, 2, "ROM_ADDRESS_BR" }, // fWritable = !? cb=!? fBridge=!?
1274 { 0x3c, 1, 1, 3, "INTERRUPT_LINE" }, // fBridge=??
1275 { 0x3d, 1, 0, 3, "INTERRUPT_PIN" }, // fBridge=??
1276 { 0x3e, 1, 0, 1, "MIN_GNT" },
1277 { 0x3e, 2, 1, 2, "BRIDGE_CONTROL" }, // fWritable = !?
1278 { 0x3f, 1, 0, 1, "MAX_LAT" },
1279 /* The COMMAND register must come last as it requires the *ADDRESS*
1280 registers to be restored before we pretent to change it from 0 to
1281 whatever value the guest assigned it. */
1282 { 0x04, 2, 1, 3, "COMMAND" },
1283 };
1284
1285#ifdef RT_STRICT
1286 /* Check that we've got full register coverage. */
1287 uint32_t bmDevice[0x40 / 32];
1288 uint32_t bmBridge[0x40 / 32];
1289 RT_ZERO(bmDevice);
1290 RT_ZERO(bmBridge);
1291 for (uint32_t i = 0; i < RT_ELEMENTS(s_aFields); i++)
1292 {
1293 uint8_t off = s_aFields[i].off;
1294 uint8_t cb = s_aFields[i].cb;
1295 uint8_t f = s_aFields[i].fBridge;
1296 while (cb-- > 0)
1297 {
1298 if (f & 1) AssertMsg(!ASMBitTest(bmDevice, off), ("%#x\n", off));
1299 if (f & 2) AssertMsg(!ASMBitTest(bmBridge, off), ("%#x\n", off));
1300 if (f & 1) ASMBitSet(bmDevice, off);
1301 if (f & 2) ASMBitSet(bmBridge, off);
1302 off++;
1303 }
1304 }
1305 for (uint32_t off = 0; off < 0x40; off++)
1306 {
1307 AssertMsg(ASMBitTest(bmDevice, off), ("%#x\n", off));
1308 AssertMsg(ASMBitTest(bmBridge, off), ("%#x\n", off));
1309 }
1310#endif
1311
1312 /*
1313 * Loop thru the fields covering the 64 bytes of standard registers.
1314 */
1315 uint8_t const fBridge = fIsBridge ? 2 : 1;
1316 Assert(!pciDevIsPassthrough(pDev));
1317 uint8_t *pbDstConfig = &pDev->config[0];
1318
1319 for (uint32_t i = 0; i < RT_ELEMENTS(s_aFields); i++)
1320 if (s_aFields[i].fBridge & fBridge)
1321 {
1322 uint8_t const off = s_aFields[i].off;
1323 uint8_t const cb = s_aFields[i].cb;
1324 uint32_t u32Src;
1325 uint32_t u32Dst;
1326 switch (cb)
1327 {
1328 case 1:
1329 u32Src = pbSrcConfig[off];
1330 u32Dst = pbDstConfig[off];
1331 break;
1332 case 2:
1333 u32Src = *(uint16_t const *)&pbSrcConfig[off];
1334 u32Dst = *(uint16_t const *)&pbDstConfig[off];
1335 break;
1336 case 4:
1337 u32Src = *(uint32_t const *)&pbSrcConfig[off];
1338 u32Dst = *(uint32_t const *)&pbDstConfig[off];
1339 break;
1340 default:
1341 AssertFailed();
1342 continue;
1343 }
1344
1345 if ( u32Src != u32Dst
1346 || off == VBOX_PCI_COMMAND)
1347 {
1348 if (u32Src != u32Dst)
1349 {
1350 if (!s_aFields[i].fWritable)
1351 LogRel(("PCI: %8s/%u: %2u-bit field %s: %x -> %x - !READ ONLY!\n",
1352 pDev->name, pDev->pDevIns->iInstance, cb*8, s_aFields[i].pszName, u32Dst, u32Src));
1353 else
1354 LogRel(("PCI: %8s/%u: %2u-bit field %s: %x -> %x\n",
1355 pDev->name, pDev->pDevIns->iInstance, cb*8, s_aFields[i].pszName, u32Dst, u32Src));
1356 }
1357 if (off == VBOX_PCI_COMMAND)
1358 PCIDevSetCommand(pDev, 0); /* For remapping, see ich9pciR3CommonLoadExec. */
1359 pDev->Int.s.pfnConfigWrite(pDev, off, u32Src, cb);
1360 }
1361 }
1362
1363 /*
1364 * The device dependent registers.
1365 *
1366 * We will not use ConfigWrite here as we have no clue about the size
1367 * of the registers, so the device is responsible for correctly
1368 * restoring functionality governed by these registers.
1369 */
1370 for (uint32_t off = 0x40; off < sizeof(pDev->config); off++)
1371 if (pbDstConfig[off] != pbSrcConfig[off])
1372 {
1373 LogRel(("PCI: %8s/%u: register %02x: %02x -> %02x\n",
1374 pDev->name, pDev->pDevIns->iInstance, off, pbDstConfig[off], pbSrcConfig[off])); /** @todo make this Log() later. */
1375 pbDstConfig[off] = pbSrcConfig[off];
1376 }
1377}
1378
1379/**
1380 * Common worker for ich9pciR3LoadExec and ich9pcibridgeR3LoadExec.
1381 *
1382 * @returns VBox status code.
1383 * @param pBus The bus which data is being loaded.
1384 * @param pSSM The saved state handle.
1385 * @param uVersion The data version.
1386 * @param uPass The pass.
1387 */
1388static DECLCALLBACK(int) ich9pciR3CommonLoadExec(PICH9PCIBUS pBus, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1389{
1390 uint32_t u32;
1391 uint32_t i;
1392 int rc;
1393
1394 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1395 if (uVersion != VBOX_ICH9PCI_SAVED_STATE_VERSION_CURRENT)
1396 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1397
1398 /*
1399 * Iterate thru all the devices and write 0 to the COMMAND register so
1400 * that all the memory is unmapped before we start restoring the saved
1401 * mapping locations.
1402 *
1403 * The register value is restored afterwards so we can do proper
1404 * LogRels in pciR3CommonRestoreConfig.
1405 */
1406 for (i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
1407 {
1408 PPCIDEVICE pDev = pBus->apDevices[i];
1409 if (pDev)
1410 {
1411 uint16_t u16 = PCIDevGetCommand(pDev);
1412 pDev->Int.s.pfnConfigWrite(pDev, VBOX_PCI_COMMAND, 0, 2);
1413 PCIDevSetCommand(pDev, u16);
1414 Assert(PCIDevGetCommand(pDev) == u16);
1415 }
1416 }
1417
1418 void *pvMsixPage = RTMemTmpAllocZ(0x1000);
1419 AssertReturn(pvMsixPage, VERR_NO_TMP_MEMORY);
1420
1421 /*
1422 * Iterate all the devices.
1423 */
1424 for (i = 0;; i++)
1425 {
1426 PPCIDEVICE pDev;
1427 PCIDEVICE DevTmp;
1428
1429 /* index / terminator */
1430 rc = SSMR3GetU32(pSSM, &u32);
1431 if (RT_FAILURE(rc))
1432 break;
1433 if (u32 == (uint32_t)~0)
1434 break;
1435 AssertMsgBreak(u32 < RT_ELEMENTS(pBus->apDevices) && u32 >= i, ("u32=%#x i=%#x\n", u32, i));
1436
1437 /* skip forward to the device checking that no new devices are present. */
1438 for (; i < u32; i++)
1439 {
1440 pDev = pBus->apDevices[i];
1441 if (pDev)
1442 {
1443 LogRel(("PCI: New device in slot %#x, %s (vendor=%#06x device=%#06x)\n", i, pDev->name,
1444 PCIDevGetVendorId(pDev), PCIDevGetDeviceId(pDev)));
1445 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1446 {
1447 rc = SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("New device in slot %#x, %s (vendor=%#06x device=%#06x)"),
1448 i, pDev->name, PCIDevGetVendorId(pDev), PCIDevGetDeviceId(pDev));
1449 break;
1450 }
1451 }
1452 }
1453 if (RT_FAILURE(rc))
1454 break;
1455
1456 /* get the data */
1457 DevTmp.Int.s.fFlags = 0;
1458 DevTmp.Int.s.u8MsiCapOffset = 0;
1459 DevTmp.Int.s.u8MsiCapSize = 0;
1460 DevTmp.Int.s.u8MsixCapOffset = 0;
1461 DevTmp.Int.s.u8MsixCapSize = 0;
1462 DevTmp.Int.s.uIrqPinState = ~0; /* Invalid value in case we have an older saved state to force a state change in pciSetIrq. */
1463 SSMR3GetMem(pSSM, DevTmp.config, sizeof(DevTmp.config));
1464
1465 SSMR3GetU32(pSSM, &DevTmp.Int.s.fFlags);
1466 SSMR3GetS32(pSSM, &DevTmp.Int.s.uIrqPinState);
1467 SSMR3GetU8(pSSM, &DevTmp.Int.s.u8MsiCapOffset);
1468 SSMR3GetU8(pSSM, &DevTmp.Int.s.u8MsiCapSize);
1469 SSMR3GetU8(pSSM, &DevTmp.Int.s.u8MsixCapOffset);
1470 rc = SSMR3GetU8(pSSM, &DevTmp.Int.s.u8MsixCapSize);
1471 if (RT_FAILURE(rc))
1472 break;
1473
1474 /* Load MSI-X page state */
1475 if (DevTmp.Int.s.u8MsixCapOffset != 0)
1476 {
1477 Assert(pvMsixPage != NULL);
1478 rc = SSMR3GetMem(pSSM, pvMsixPage, 0x1000);
1479 if (RT_FAILURE(rc))
1480 break;
1481 }
1482
1483 /* check that it's still around. */
1484 pDev = pBus->apDevices[i];
1485 if (!pDev)
1486 {
1487 LogRel(("PCI: Device in slot %#x has been removed! vendor=%#06x device=%#06x\n", i,
1488 PCIDevGetVendorId(&DevTmp), PCIDevGetDeviceId(&DevTmp)));
1489 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1490 {
1491 rc = SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device in slot %#x has been removed! vendor=%#06x device=%#06x"),
1492 i, PCIDevGetVendorId(&DevTmp), PCIDevGetDeviceId(&DevTmp));
1493 break;
1494 }
1495 continue;
1496 }
1497
1498 /* match the vendor id assuming that this will never be changed. */
1499 if (PCIDevGetVendorId(&DevTmp) != PCIDevGetVendorId(pDev))
1500 {
1501 rc = SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device in slot %#x (%s) vendor id mismatch! saved=%.4Rhxs current=%.4Rhxs"),
1502 i, pDev->name, PCIDevGetVendorId(&DevTmp), PCIDevGetVendorId(pDev));
1503 break;
1504 }
1505
1506 /* commit the loaded device config. */
1507 Assert(!pciDevIsPassthrough(pDev));
1508 pciR3CommonRestoreConfig(pDev, &DevTmp.config[0], false ); /** @todo fix bridge fun! */
1509
1510 pDev->Int.s.uIrqPinState = DevTmp.Int.s.uIrqPinState;
1511 pDev->Int.s.u8MsiCapOffset = DevTmp.Int.s.u8MsiCapOffset;
1512 pDev->Int.s.u8MsiCapSize = DevTmp.Int.s.u8MsiCapSize;
1513 pDev->Int.s.u8MsixCapOffset = DevTmp.Int.s.u8MsixCapOffset;
1514 pDev->Int.s.u8MsixCapSize = DevTmp.Int.s.u8MsixCapSize;
1515 if (DevTmp.Int.s.u8MsixCapSize != 0)
1516 {
1517 Assert(pDev->Int.s.pMsixPageR3 != NULL);
1518 memcpy(pDev->Int.s.pMsixPageR3, pvMsixPage, 0x1000);
1519 }
1520 }
1521
1522 RTMemTmpFree(pvMsixPage);
1523
1524 return rc;
1525}
1526
1527static DECLCALLBACK(int) ich9pciR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1528{
1529 PICH9PCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
1530 PICH9PCIBUS pBus = &pThis->aPciBus;
1531 uint32_t u32;
1532 int rc;
1533
1534 /* We ignore this version as there's no saved state with it anyway */
1535 if (uVersion == VBOX_ICH9PCI_SAVED_STATE_VERSION_NOMSI)
1536 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1537 if (uVersion > VBOX_ICH9PCI_SAVED_STATE_VERSION_MSI)
1538 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1539
1540 /*
1541 * Bus state data.
1542 */
1543 SSMR3GetU32(pSSM, &pThis->uConfigReg);
1544
1545 /*
1546 * Load IRQ states.
1547 */
1548 for (int i = 0; i < PCI_APIC_IRQ_PINS; i++)
1549 SSMR3GetU32(pSSM, (uint32_t*)&pThis->uaPciApicIrqLevels[i]);
1550
1551 /* separator */
1552 rc = SSMR3GetU32(pSSM, &u32);
1553 if (RT_FAILURE(rc))
1554 return rc;
1555 if (u32 != (uint32_t)~0)
1556 AssertMsgFailedReturn(("u32=%#x\n", u32), rc);
1557
1558 return ich9pciR3CommonLoadExec(pBus, pSSM, uVersion, uPass);
1559}
1560
1561static DECLCALLBACK(int) ich9pcibridgeR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1562{
1563 PICH9PCIBUS pThis = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
1564 if (uVersion > VBOX_ICH9PCI_SAVED_STATE_VERSION_MSI)
1565 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1566 return ich9pciR3CommonLoadExec(pThis, pSSM, uVersion, uPass);
1567}
1568
1569
1570/*
1571 * Perform imeediate read of configuration space register.
1572 * Cannot be rescheduled, as already in R3.
1573 */
1574static uint32_t ich9pciConfigRead(PICH9PCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t len)
1575{
1576 PciAddress aPciAddr;
1577 aPciAddr.iBus = uBus;
1578 aPciAddr.iDeviceFunc = uDevFn;
1579 aPciAddr.iRegister = addr;
1580
1581 uint32_t u32Val;
1582 int rc = ich9pciDataReadAddr(pGlobals, &aPciAddr, len, &u32Val, VERR_INTERNAL_ERROR);
1583 AssertRC(rc);
1584
1585 return u32Val;
1586}
1587
1588
1589/*
1590 * Perform imeediate write to configuration space register.
1591 * Cannot be rescheduled, as already in R3.
1592 */
1593static void ich9pciConfigWrite(PICH9PCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t val, uint32_t len)
1594{
1595 PciAddress aPciAddr;
1596 aPciAddr.iBus = uBus;
1597 aPciAddr.iDeviceFunc = uDevFn;
1598 aPciAddr.iRegister = addr;
1599
1600 int rc = ich9pciDataWriteAddr(pGlobals, &aPciAddr, val, len, VERR_INTERNAL_ERROR);
1601 AssertRC(rc);
1602}
1603
1604
1605static void ich9pciSetRegionAddress(PICH9PCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, int iRegion, uint64_t addr)
1606{
1607 uint32_t uReg = ich9pciGetRegionReg(iRegion);
1608
1609 /* Read memory type first. */
1610 uint8_t uResourceType = ich9pciConfigRead(pGlobals, uBus, uDevFn, uReg, 1);
1611 /* Read command register. */
1612 uint16_t uCmd = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_COMMAND, 2);
1613
1614 Log(("Set region address: %02x:%02x.%d region %d address=%lld\n",
1615 uBus, uDevFn>>3, uDevFn&7, addr));
1616
1617 if ( iRegion == PCI_ROM_SLOT )
1618 uCmd |= PCI_COMMAND_MEMACCESS;
1619 else if ((uResourceType & PCI_ADDRESS_SPACE_IO) == PCI_ADDRESS_SPACE_IO)
1620 uCmd |= PCI_COMMAND_IOACCESS; /* Enable I/O space access. */
1621 else /* The region is MMIO. */
1622 uCmd |= PCI_COMMAND_MEMACCESS; /* Enable MMIO access. */
1623
1624 bool f64Bit = (uResourceType & PCI_ADDRESS_SPACE_BAR64) != 0;
1625
1626 /* Write address of the device. */
1627 ich9pciConfigWrite(pGlobals, uBus, uDevFn, uReg, (uint32_t)addr, 4);
1628 if (f64Bit)
1629 ich9pciConfigWrite(pGlobals, uBus, uDevFn, uReg + 4, (uint32_t)(addr >> 32), 4);
1630
1631 /* enable memory mappings */
1632 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_COMMAND, uCmd, 2);
1633}
1634
1635
1636static void ich9pciBiosInitBridge(PICH9PCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn)
1637{
1638 Log(("BIOS init bridge: %02x::%02x.%d\n", uBus, uDevFn >> 3, uDevFn & 7));
1639
1640 /*
1641 * The I/O range for the bridge must be aligned to a 4KB boundary.
1642 * This does not change anything really as the access to the device is not going
1643 * through the bridge but we want to be compliant to the spec.
1644 */
1645 if ((pGlobals->uPciBiosIo % 4096) != 0)
1646 {
1647 pGlobals->uPciBiosIo = RT_ALIGN_32(pGlobals->uPciBiosIo, 4*1024);
1648 Log(("%s: Aligned I/O start address. New address %#x\n", __FUNCTION__, pGlobals->uPciBiosIo));
1649 }
1650 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_IO_BASE, (pGlobals->uPciBiosIo >> 8) & 0xf0, 1);
1651
1652 /* The MMIO range for the bridge must be aligned to a 1MB boundary. */
1653 if ((pGlobals->uPciBiosMmio % (1024 * 1024)) != 0)
1654 {
1655 pGlobals->uPciBiosMmio = RT_ALIGN_32(pGlobals->uPciBiosMmio, 1024*1024);
1656 Log(("%s: Aligned MMIO start address. New address %#x\n", __FUNCTION__, pGlobals->uPciBiosMmio));
1657 }
1658 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_MEMORY_BASE, (pGlobals->uPciBiosMmio >> 16) & UINT32_C(0xffff0), 2);
1659
1660 /* Save values to compare later to. */
1661 uint32_t u32IoAddressBase = pGlobals->uPciBiosIo;
1662 uint32_t u32MMIOAddressBase = pGlobals->uPciBiosMmio;
1663 uint8_t uBridgeBus = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_SECONDARY_BUS, 1);
1664
1665 /* Init devices behind the bridge and possibly other bridges as well. */
1666 for (int iDev = 0; iDev <= 255; iDev++)
1667 ich9pciBiosInitDevice(pGlobals, uBridgeBus, iDev);
1668
1669 /*
1670 * Set I/O limit register. If there is no device with I/O space behind the bridge
1671 * we set a lower value than in the base register.
1672 * The result with a real bridge is that no I/O transactions are passed to the secondary
1673 * interface. Again this doesn't really matter here but we want to be compliant to the spec.
1674 */
1675 if ((u32IoAddressBase != pGlobals->uPciBiosIo) && ((pGlobals->uPciBiosIo % 4096) != 0))
1676 {
1677 /* The upper boundary must be one byte less than a 4KB boundary. */
1678 pGlobals->uPciBiosIo = RT_ALIGN_32(pGlobals->uPciBiosIo, 4*1024);
1679 }
1680
1681 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_IO_LIMIT, ((pGlobals->uPciBiosIo >> 8) & 0xf0) - 1, 1);
1682
1683 /* Same with the MMIO limit register but with 1MB boundary here. */
1684 if ((u32MMIOAddressBase != pGlobals->uPciBiosMmio) && ((pGlobals->uPciBiosMmio % (1024 * 1024)) != 0))
1685 {
1686 /* The upper boundary must be one byte less than a 1MB boundary. */
1687 pGlobals->uPciBiosMmio = RT_ALIGN_32(pGlobals->uPciBiosMmio, 1024*1024);
1688 }
1689 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_MEMORY_LIMIT, ((pGlobals->uPciBiosMmio >> 16) & UINT32_C(0xfff0)) - 1, 2);
1690
1691 /*
1692 * Set the prefetch base and limit registers. We currently have no device with a prefetchable region
1693 * which may be behind a bridge. That's why it is unconditionally disabled here atm by writing a higher value into
1694 * the base register than in the limit register.
1695 */
1696 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_MEMORY_BASE, 0xfff0, 2);
1697 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_MEMORY_LIMIT, 0x0, 2);
1698 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_BASE_UPPER32, 0x00, 4);
1699 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_LIMIT_UPPER32, 0x00, 4);
1700}
1701
1702static void ich9pciBiosInitDevice(PICH9PCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn)
1703{
1704 uint16_t uDevClass, uVendor, uDevice;
1705 uint8_t uCmd;
1706
1707 uDevClass = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_CLASS_DEVICE, 2);
1708 uVendor = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_VENDOR_ID, 2);
1709 uDevice = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_DEVICE_ID, 2);
1710
1711 /* If device is present */
1712 if (uVendor == 0xffff)
1713 return;
1714
1715 Log(("BIOS init device: %02x:%02x.%d\n", uBus, uDevFn >> 3, uDevFn & 7));
1716
1717 switch (uDevClass)
1718 {
1719 case 0x0101:
1720 /* IDE controller */
1721 ich9pciConfigWrite(pGlobals, uBus, uDevFn, 0x40, 0x8000, 2); /* enable IDE0 */
1722 ich9pciConfigWrite(pGlobals, uBus, uDevFn, 0x42, 0x8000, 2); /* enable IDE1 */
1723 goto default_map;
1724 break;
1725 case 0x0300:
1726 /* VGA controller */
1727 if (uVendor != 0x80ee)
1728 goto default_map;
1729 /* VGA: map frame buffer to default Bochs VBE address */
1730 ich9pciSetRegionAddress(pGlobals, uBus, uDevFn, 0, 0xE0000000);
1731 /*
1732 * Legacy VGA I/O ports are implicitly decoded by a VGA class device. But
1733 * only the framebuffer (i.e., a memory region) is explicitly registered via
1734 * ich9pciSetRegionAddress, so I/O decoding must be enabled manually.
1735 */
1736 uCmd = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_COMMAND, 1);
1737 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_COMMAND,
1738 /* Enable I/O space access. */
1739 uCmd | PCI_COMMAND_IOACCESS,
1740 1);
1741 break;
1742 case 0x0604:
1743 /* PCI-to-PCI bridge. */
1744 AssertMsg(pGlobals->uBus < 255, ("Too many bridges on the bus\n"));
1745 ich9pciBiosInitBridge(pGlobals, uBus, uDevFn);
1746 break;
1747 default:
1748 default_map:
1749 {
1750 /* default memory mappings */
1751 /*
1752 * We ignore ROM region here.
1753 */
1754 for (int iRegion = 0; iRegion < (PCI_NUM_REGIONS-1); iRegion++)
1755 {
1756 uint32_t u32Address = ich9pciGetRegionReg(iRegion);
1757
1758 /* Calculate size - we write all 1s into the BAR, and then evaluate which bits
1759 are cleared. . */
1760 uint8_t u8ResourceType = ich9pciConfigRead(pGlobals, uBus, uDevFn, u32Address, 1);
1761
1762 bool f64bit = (u8ResourceType & PCI_ADDRESS_SPACE_BAR64) != 0;
1763 bool fIsPio = ((u8ResourceType & PCI_COMMAND_IOACCESS) == PCI_COMMAND_IOACCESS);
1764 uint64_t cbRegSize64 = 0;
1765
1766 if (f64bit)
1767 {
1768 ich9pciConfigWrite(pGlobals, uBus, uDevFn, u32Address, UINT32_C(0xffffffff), 4);
1769 ich9pciConfigWrite(pGlobals, uBus, uDevFn, u32Address+4, UINT32_C(0xffffffff), 4);
1770 cbRegSize64 = ich9pciConfigRead(pGlobals, uBus, uDevFn, u32Address, 4);
1771 cbRegSize64 |= ((uint64_t)ich9pciConfigRead(pGlobals, uBus, uDevFn, u32Address+4, 4) << 32);
1772 cbRegSize64 &= ~UINT64_C(0x0f);
1773 cbRegSize64 = (~cbRegSize64) + 1;
1774
1775 /* No 64-bit PIO regions possible. */
1776#ifndef DEBUG_bird /* EFI triggers this for DevAHCI. */
1777 AssertMsg((u8ResourceType & PCI_COMMAND_IOACCESS) == 0, ("type=%#x rgn=%d\n", u8ResourceType, iRegion));
1778#endif
1779 }
1780 else
1781 {
1782 uint32_t cbRegSize32;
1783 ich9pciConfigWrite(pGlobals, uBus, uDevFn, u32Address, UINT32_C(0xffffffff), 4);
1784 cbRegSize32 = ich9pciConfigRead(pGlobals, uBus, uDevFn, u32Address, 4);
1785
1786 /* Clear resource information depending on resource type. */
1787 if (fIsPio) /* PIO */
1788 cbRegSize32 &= ~UINT32_C(0x01);
1789 else /* MMIO */
1790 cbRegSize32 &= ~UINT32_C(0x0f);
1791
1792 /*
1793 * Invert all bits and add 1 to get size of the region.
1794 * (From PCI implementation note)
1795 */
1796 if (fIsPio && (cbRegSize32 & UINT32_C(0xffff0000)) == 0)
1797 cbRegSize32 = (~(cbRegSize32 | UINT32_C(0xffff0000))) + 1;
1798 else
1799 cbRegSize32 = (~cbRegSize32) + 1;
1800
1801 cbRegSize64 = cbRegSize32;
1802 }
1803#ifndef DEBUG_bird /* EFI triggers this for DevAHCI. */
1804 Assert(cbRegSize64 == (uint32_t)cbRegSize64);
1805#endif
1806 Log2(("%s: Size of region %u for device %d on bus %d is %lld\n", __FUNCTION__, iRegion, uDevFn, uBus, cbRegSize64));
1807
1808 if (cbRegSize64)
1809 {
1810 uint32_t cbRegSize32 = (uint32_t)cbRegSize64;
1811 uint32_t* paddr = fIsPio ? &pGlobals->uPciBiosIo : &pGlobals->uPciBiosMmio;
1812 *paddr = (*paddr + cbRegSize32 - 1) & ~(cbRegSize32 - 1);
1813 Log(("%s: Start address of %s region %u is %#x\n", __FUNCTION__, (fIsPio ? "I/O" : "MMIO"), iRegion, *paddr));
1814 ich9pciSetRegionAddress(pGlobals, uBus, uDevFn, iRegion, *paddr);
1815 *paddr += cbRegSize32;
1816 Log2(("%s: New address is %#x\n", __FUNCTION__, *paddr));
1817
1818 if (f64bit)
1819 iRegion++; /* skip next region */
1820 }
1821 }
1822 break;
1823 }
1824 }
1825
1826 /* map the interrupt */
1827 uint32_t iPin = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_INTERRUPT_PIN, 1);
1828 if (iPin != 0)
1829 {
1830 iPin--;
1831
1832 if (uBus != 0)
1833 {
1834 /* Find bus this device attached to. */
1835 PICH9PCIBUS pBus = &pGlobals->aPciBus;
1836 while (1)
1837 {
1838 PPCIDEVICE pBridge = ich9pciFindBridge(pBus, uBus);
1839 if (!pBridge)
1840 {
1841 Assert(false);
1842 break;
1843 }
1844 if (uBus == PCIDevGetByte(pBridge, VBOX_PCI_SECONDARY_BUS))
1845 {
1846 /* OK, found bus this device attached to. */
1847 break;
1848 }
1849 pBus = PDMINS_2_DATA(pBridge->pDevIns, PICH9PCIBUS);
1850 }
1851
1852 /* We need to go up to the host bus to see which irq pin this
1853 * device will use there. See logic in ich9pcibridgeSetIrq().
1854 */
1855 while (pBus->iBus != 0)
1856 {
1857 /* Get the pin the device would assert on the bridge. */
1858 iPin = ((pBus->aPciDev.devfn >> 3) + iPin) & 3;
1859 pBus = pBus->aPciDev.Int.s.pBusR3;
1860 };
1861 }
1862
1863 int iIrq = aPciIrqs[ich9pciSlotGetPirq(uBus, uDevFn, iPin)];
1864 Log(("Using pin %d and IRQ %d for device %02x:%02x.%d\n",
1865 iPin, iIrq, uBus, uDevFn>>3, uDevFn&7));
1866 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_INTERRUPT_LINE, iIrq, 1);
1867 }
1868}
1869
1870/**
1871 * Initializes bridges registers used for routing.
1872 *
1873 * @returns nothing.
1874 * @param pGlobals Global device instance data used to generate unique bus numbers.
1875 * @param pBus The PCI bus to initialize.
1876 * @param uBusPrimary The primary bus number the bus is connected to.
1877 * @param uBusSecondary The secondary bus number, i.e. the bus number behind the bridge.
1878 */
1879static void ich9pciInitBridgeTopology(PICH9PCIGLOBALS pGlobals, PICH9PCIBUS pBus, unsigned uBusPrimary,
1880 unsigned uBusSecondary)
1881{
1882 PPCIDEVICE pBridgeDev = &pBus->aPciDev;
1883
1884 /* Set only if we are not on the root bus, it has no primary bus attached. */
1885 if (uBusSecondary != 0)
1886 {
1887 PCIDevSetByte(pBridgeDev, VBOX_PCI_PRIMARY_BUS, uBusPrimary);
1888 PCIDevSetByte(pBridgeDev, VBOX_PCI_SECONDARY_BUS, uBusSecondary);
1889 }
1890
1891 for (uint32_t iBridge = 0; iBridge < pBus->cBridges; iBridge++)
1892 {
1893 PPCIDEVICE pBridge = pBus->papBridgesR3[iBridge];
1894 AssertMsg(pBridge && pciDevIsPci2PciBridge(pBridge),
1895 ("Device is not a PCI bridge but on the list of PCI bridges\n"));
1896 PICH9PCIBUS pChildBus = PDMINS_2_DATA(pBridge->pDevIns, PICH9PCIBUS);
1897 pGlobals->uBus++;
1898 ich9pciInitBridgeTopology(pGlobals, pChildBus, uBusSecondary, pGlobals->uBus);
1899 }
1900 PCIDevSetByte(pBridgeDev, VBOX_PCI_SUBORDINATE_BUS, pGlobals->uBus);
1901 Log2(("ich9pciInitBridgeTopology: for bus %p: primary=%d secondary=%d subordinate=%d\n",
1902 pBus,
1903 PCIDevGetByte(pBridgeDev, VBOX_PCI_PRIMARY_BUS),
1904 PCIDevGetByte(pBridgeDev, VBOX_PCI_SECONDARY_BUS),
1905 PCIDevGetByte(pBridgeDev, VBOX_PCI_SUBORDINATE_BUS)
1906 ));
1907}
1908
1909
1910static DECLCALLBACK(int) ich9pciFakePCIBIOS(PPDMDEVINS pDevIns)
1911{
1912 PICH9PCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
1913 PVM pVM = PDMDevHlpGetVM(pDevIns);
1914 Assert(pVM);
1915
1916 /*
1917 * Set the start addresses.
1918 */
1919 pGlobals->uPciBiosIo = 0xd000;
1920 pGlobals->uPciBiosMmio = UINT32_C(0xf0000000);
1921 pGlobals->uBus = 0;
1922
1923 /*
1924 * Assign bridge topology, for further routing to work.
1925 */
1926 PICH9PCIBUS pBus = &pGlobals->aPciBus;
1927 ich9pciInitBridgeTopology(pGlobals, pBus, 0, 0);
1928
1929 /*
1930 * Init the devices.
1931 */
1932 for (int i = 0; i < 256; i++)
1933 {
1934 ich9pciBiosInitDevice(pGlobals, 0, i);
1935 }
1936
1937 return VINF_SUCCESS;
1938}
1939
1940
1941/*
1942 * Configuration space read callback (PCIDEVICEINT::pfnConfigRead) for
1943 * connected devices.
1944 */
1945static DECLCALLBACK(uint32_t) ich9pciConfigReadDev(PCIDevice *aDev, uint32_t u32Address, unsigned len)
1946{
1947 if ((u32Address + len) > 256 && (u32Address + len) < 4096)
1948 {
1949 LogRel(("PCI: Read from extended register %d fallen back to generic code\n",
1950 u32Address));
1951 return 0;
1952 }
1953
1954 AssertMsgReturn(u32Address + len <= 256, ("Read after the end of PCI config space\n"),
1955 0);
1956 if ( pciDevIsMsiCapable(aDev)
1957 && (u32Address >= aDev->Int.s.u8MsiCapOffset)
1958 && (u32Address < (unsigned)(aDev->Int.s.u8MsiCapOffset + aDev->Int.s.u8MsiCapSize))
1959 )
1960 {
1961 return MsiPciConfigRead(aDev->Int.s.CTX_SUFF(pBus)->CTX_SUFF(pDevIns), aDev, u32Address, len);
1962 }
1963
1964 if ( pciDevIsMsixCapable(aDev)
1965 && (u32Address >= aDev->Int.s.u8MsixCapOffset)
1966 && (u32Address < (unsigned)(aDev->Int.s.u8MsixCapOffset + aDev->Int.s.u8MsixCapSize))
1967 )
1968 {
1969 return MsixPciConfigRead(aDev->Int.s.CTX_SUFF(pBus)->CTX_SUFF(pDevIns), aDev, u32Address, len);
1970 }
1971
1972 AssertMsgReturn(u32Address + len <= 256, ("Read after end of PCI config space\n"),
1973 0);
1974 switch (len)
1975 {
1976 case 1:
1977 return PCIDevGetByte(aDev, u32Address);
1978 case 2:
1979 return PCIDevGetWord(aDev, u32Address);
1980 case 4:
1981 return PCIDevGetDWord(aDev, u32Address);
1982 default:
1983 Assert(false);
1984 return 0;
1985 }
1986}
1987
1988
1989DECLINLINE(void) ich9pciWriteBarByte(PCIDevice *aDev, int iRegion, int iOffset, uint8_t u8Val)
1990{
1991 PCIIORegion * pRegion = &aDev->Int.s.aIORegions[iRegion];
1992 int64_t iRegionSize = pRegion->size;
1993
1994 Log3(("ich9pciWriteBarByte: region=%d off=%d val=%x size=%d\n",
1995 iRegion, iOffset, u8Val, iRegionSize));
1996
1997 if (iOffset > 3)
1998 Assert((pRegion->type & PCI_ADDRESS_SPACE_BAR64) != 0);
1999
2000 /* Check if we're writing to upper part of 64-bit BAR. */
2001 if (pRegion->type == 0xff)
2002 {
2003 ich9pciWriteBarByte(aDev, iRegion-1, iOffset+4, u8Val);
2004 return;
2005 }
2006
2007 /* Region doesn't exist */
2008 if (iRegionSize == 0)
2009 return;
2010
2011 uint32_t uAddr = ich9pciGetRegionReg(iRegion) + iOffset;
2012 /* Region size must be power of two */
2013 Assert((iRegionSize & (iRegionSize - 1)) == 0);
2014 uint8_t uMask = ((iRegionSize - 1) >> (iOffset*8) ) & 0xff;
2015
2016 if (iOffset == 0)
2017 {
2018 uMask |= (pRegion->type & PCI_ADDRESS_SPACE_IO) ?
2019 (1 << 2) - 1 /* 2 lowest bits for IO region */ :
2020 (1 << 4) - 1 /* 4 lowest bits for memory region, also ROM enable bit for ROM region */;
2021
2022 }
2023
2024 uint8_t u8Old = PCIDevGetByte(aDev, uAddr) & uMask;
2025 u8Val = (u8Old & uMask) | (u8Val & ~uMask);
2026
2027 Log3(("ich9pciWriteBarByte: was %x writing %x\n", u8Old, u8Val));
2028
2029 PCIDevSetByte(aDev, uAddr, u8Val);
2030}
2031
2032
2033/**
2034 * Configuration space write callback (PCIDEVICEINT::pfnConfigWrite)
2035 * for connected devices.
2036 *
2037 * See paragraph 7.5 of PCI Express specification (p. 349) for
2038 * definition of registers and their writability policy.
2039 */
2040static DECLCALLBACK(void) ich9pciConfigWriteDev(PCIDevice *aDev, uint32_t u32Address,
2041 uint32_t val, unsigned len)
2042{
2043 Assert(len <= 4);
2044
2045 if ((u32Address + len) > 256 && (u32Address + len) < 4096)
2046 {
2047 LogRel(("PCI: Write to extended register %d fallen back to generic code\n",
2048 u32Address));
2049 return;
2050 }
2051
2052 AssertMsgReturnVoid(u32Address + len <= 256, ("Write after end of PCI config space\n"));
2053
2054 if ( pciDevIsMsiCapable(aDev)
2055 && (u32Address >= aDev->Int.s.u8MsiCapOffset)
2056 && (u32Address < (unsigned)(aDev->Int.s.u8MsiCapOffset + aDev->Int.s.u8MsiCapSize))
2057 )
2058 {
2059 MsiPciConfigWrite(aDev->Int.s.CTX_SUFF(pBus)->CTX_SUFF(pDevIns),
2060 aDev->Int.s.CTX_SUFF(pBus)->CTX_SUFF(pPciHlp),
2061 aDev, u32Address, val, len);
2062 return;
2063 }
2064
2065 if ( pciDevIsMsixCapable(aDev)
2066 && (u32Address >= aDev->Int.s.u8MsixCapOffset)
2067 && (u32Address < (unsigned)(aDev->Int.s.u8MsixCapOffset + aDev->Int.s.u8MsixCapSize))
2068 )
2069 {
2070 MsixPciConfigWrite(aDev->Int.s.CTX_SUFF(pBus)->CTX_SUFF(pDevIns),
2071 aDev->Int.s.CTX_SUFF(pBus)->CTX_SUFF(pPciHlp),
2072 aDev, u32Address, val, len);
2073 return;
2074 }
2075
2076 uint32_t addr = u32Address;
2077 bool fUpdateMappings = false;
2078 bool fP2PBridge = false;
2079 /*bool fPassthrough = pciDevIsPassthrough(aDev);*/
2080 uint8_t u8HeaderType = ich9pciGetByte(aDev, VBOX_PCI_HEADER_TYPE);
2081
2082 for (uint32_t i = 0; i < len; i++)
2083 {
2084 bool fWritable = false;
2085 bool fRom = false;
2086 switch (u8HeaderType)
2087 {
2088 case 0x00: /* normal device */
2089 case 0x80: /* multi-function device */
2090 switch (addr)
2091 {
2092 /* Read-only registers */
2093 case VBOX_PCI_VENDOR_ID: case VBOX_PCI_VENDOR_ID+1:
2094 case VBOX_PCI_DEVICE_ID: case VBOX_PCI_DEVICE_ID+1:
2095 case VBOX_PCI_REVISION_ID:
2096 case VBOX_PCI_CLASS_PROG:
2097 case VBOX_PCI_CLASS_SUB:
2098 case VBOX_PCI_CLASS_BASE:
2099 case VBOX_PCI_HEADER_TYPE:
2100 case VBOX_PCI_SUBSYSTEM_VENDOR_ID: case VBOX_PCI_SUBSYSTEM_VENDOR_ID+1:
2101 case VBOX_PCI_SUBSYSTEM_ID: case VBOX_PCI_SUBSYSTEM_ID+1:
2102 case VBOX_PCI_ROM_ADDRESS: case VBOX_PCI_ROM_ADDRESS+1: case VBOX_PCI_ROM_ADDRESS+2: case VBOX_PCI_ROM_ADDRESS+3:
2103 case VBOX_PCI_CAPABILITY_LIST:
2104 case VBOX_PCI_INTERRUPT_PIN:
2105 fWritable = false;
2106 break;
2107 /* Others can be written */
2108 default:
2109 fWritable = true;
2110 break;
2111 }
2112 break;
2113 case 0x01: /* PCI-PCI bridge */
2114 fP2PBridge = true;
2115 switch (addr)
2116 {
2117 /* Read-only registers */
2118 case VBOX_PCI_VENDOR_ID: case VBOX_PCI_VENDOR_ID+1:
2119 case VBOX_PCI_DEVICE_ID: case VBOX_PCI_DEVICE_ID+1:
2120 case VBOX_PCI_REVISION_ID:
2121 case VBOX_PCI_CLASS_PROG:
2122 case VBOX_PCI_CLASS_SUB:
2123 case VBOX_PCI_CLASS_BASE:
2124 case VBOX_PCI_HEADER_TYPE:
2125 case VBOX_PCI_ROM_ADDRESS_BR: case VBOX_PCI_ROM_ADDRESS_BR+1: case VBOX_PCI_ROM_ADDRESS_BR+2: case VBOX_PCI_ROM_ADDRESS_BR+3:
2126 case VBOX_PCI_INTERRUPT_PIN:
2127 fWritable = false;
2128 break;
2129 default:
2130 fWritable = true;
2131 break;
2132 }
2133 break;
2134 default:
2135 AssertMsgFailed(("Unknown header type %x\n", PCIDevGetHeaderType(aDev)));
2136 fWritable = false;
2137 break;
2138 }
2139
2140 uint8_t u8Val = (uint8_t)val;
2141 switch (addr)
2142 {
2143 case VBOX_PCI_COMMAND: /* Command register, bits 0-7. */
2144 fUpdateMappings = true;
2145 goto default_case;
2146 case VBOX_PCI_COMMAND+1: /* Command register, bits 8-15. */
2147 /* don't change reserved bits (11-15) */
2148 u8Val &= UINT32_C(~0xf8);
2149 fUpdateMappings = true;
2150 goto default_case;
2151 case VBOX_PCI_STATUS: /* Status register, bits 0-7. */
2152 /* don't change read-only bits => actually all lower bits are read-only */
2153 u8Val &= UINT32_C(~0xff);
2154 /* status register, low part: clear bits by writing a '1' to the corresponding bit */
2155 aDev->config[addr] &= ~u8Val;
2156 break;
2157 case VBOX_PCI_STATUS+1: /* Status register, bits 8-15. */
2158 /* don't change read-only bits */
2159 u8Val &= UINT32_C(~0x06);
2160 /* status register, high part: clear bits by writing a '1' to the corresponding bit */
2161 aDev->config[addr] &= ~u8Val;
2162 break;
2163 case VBOX_PCI_ROM_ADDRESS: case VBOX_PCI_ROM_ADDRESS +1: case VBOX_PCI_ROM_ADDRESS +2: case VBOX_PCI_ROM_ADDRESS +3:
2164 fRom = true;
2165 case VBOX_PCI_BASE_ADDRESS_0: case VBOX_PCI_BASE_ADDRESS_0+1: case VBOX_PCI_BASE_ADDRESS_0+2: case VBOX_PCI_BASE_ADDRESS_0+3:
2166 case VBOX_PCI_BASE_ADDRESS_1: case VBOX_PCI_BASE_ADDRESS_1+1: case VBOX_PCI_BASE_ADDRESS_1+2: case VBOX_PCI_BASE_ADDRESS_1+3:
2167 case VBOX_PCI_BASE_ADDRESS_2: case VBOX_PCI_BASE_ADDRESS_2+1: case VBOX_PCI_BASE_ADDRESS_2+2: case VBOX_PCI_BASE_ADDRESS_2+3:
2168 case VBOX_PCI_BASE_ADDRESS_3: case VBOX_PCI_BASE_ADDRESS_3+1: case VBOX_PCI_BASE_ADDRESS_3+2: case VBOX_PCI_BASE_ADDRESS_3+3:
2169 case VBOX_PCI_BASE_ADDRESS_4: case VBOX_PCI_BASE_ADDRESS_4+1: case VBOX_PCI_BASE_ADDRESS_4+2: case VBOX_PCI_BASE_ADDRESS_4+3:
2170 case VBOX_PCI_BASE_ADDRESS_5: case VBOX_PCI_BASE_ADDRESS_5+1: case VBOX_PCI_BASE_ADDRESS_5+2: case VBOX_PCI_BASE_ADDRESS_5+3:
2171 {
2172 /* We check that, as same PCI register numbers as BARs may mean different registers for bridges */
2173 if (fP2PBridge)
2174 goto default_case;
2175 else
2176 {
2177 int iRegion = fRom ? VBOX_PCI_ROM_SLOT : (addr - VBOX_PCI_BASE_ADDRESS_0) >> 2;
2178 int iOffset = addr & 0x3;
2179 ich9pciWriteBarByte(aDev, iRegion, iOffset, u8Val);
2180 fUpdateMappings = true;
2181 }
2182 break;
2183 }
2184 default:
2185 default_case:
2186 if (fWritable)
2187 PCIDevSetByte(aDev, addr, u8Val);
2188 }
2189 addr++;
2190 val >>= 8;
2191 }
2192
2193 if (fUpdateMappings)
2194 /* if the command/base address register is modified, we must modify the mappings */
2195 ich9pciUpdateMappings(aDev);
2196}
2197
2198static bool assignPosition(PICH9PCIBUS pBus, PPCIDEVICE pPciDev, const char *pszName, int iDevFn, PciAddress* aPosition)
2199{
2200 NOREF(pszName);
2201 aPosition->iBus = 0;
2202 aPosition->iDeviceFunc = iDevFn;
2203 aPosition->iRegister = 0; /* N/A */
2204
2205 /* Explicit slot request */
2206 if (iDevFn >= 0 && iDevFn < (int)RT_ELEMENTS(pBus->apDevices))
2207 return true;
2208
2209 int iStartPos = 0;
2210
2211 /* Otherwise when assigning a slot, we need to make sure all its functions are available */
2212 for (int iPos = iStartPos; iPos < (int)RT_ELEMENTS(pBus->apDevices); iPos += 8)
2213 {
2214 if ( !pBus->apDevices[iPos]
2215 && !pBus->apDevices[iPos + 1]
2216 && !pBus->apDevices[iPos + 2]
2217 && !pBus->apDevices[iPos + 3]
2218 && !pBus->apDevices[iPos + 4]
2219 && !pBus->apDevices[iPos + 5]
2220 && !pBus->apDevices[iPos + 6]
2221 && !pBus->apDevices[iPos + 7])
2222 {
2223 pciDevClearRequestedDevfunc(pPciDev);
2224 aPosition->iDeviceFunc = iPos;
2225 return true;
2226 }
2227 }
2228
2229 return false;
2230}
2231
2232#ifdef SOME_UNUSED_FUNCTION
2233static bool hasHardAssignedDevsInSlot(PICH9PCIBUS pBus, int iSlot)
2234{
2235 PCIDevice** aSlot = &pBus->apDevices[iSlot << 3];
2236
2237 return (aSlot[0] && pciDevIsRequestedDevfunc(aSlot[0]))
2238 || (aSlot[1] && pciDevIsRequestedDevfunc(aSlot[1]))
2239 || (aSlot[2] && pciDevIsRequestedDevfunc(aSlot[2]))
2240 || (aSlot[3] && pciDevIsRequestedDevfunc(aSlot[3]))
2241 || (aSlot[4] && pciDevIsRequestedDevfunc(aSlot[4]))
2242 || (aSlot[5] && pciDevIsRequestedDevfunc(aSlot[5]))
2243 || (aSlot[6] && pciDevIsRequestedDevfunc(aSlot[6]))
2244 || (aSlot[7] && pciDevIsRequestedDevfunc(aSlot[7]))
2245 ;
2246}
2247#endif
2248
2249static int ich9pciRegisterInternal(PICH9PCIBUS pBus, int iDev, PPCIDEVICE pPciDev, const char *pszName)
2250{
2251 PciAddress aPosition;
2252 aPosition.iBus = 0;
2253 aPosition.iDeviceFunc = 0;
2254 aPosition.iRegister = 0;
2255
2256 /*
2257 * Find device position
2258 */
2259 if (!assignPosition(pBus, pPciDev, pszName, iDev, &aPosition))
2260 {
2261 AssertMsgFailed(("Couldn't asssign position!\n"));
2262 return VERR_PDM_TOO_PCI_MANY_DEVICES;
2263 }
2264
2265 AssertMsgReturn(aPosition.iBus == 0,
2266 ("Assigning behind the bridge not implemented yet\n"),
2267 VERR_PDM_TOO_PCI_MANY_DEVICES);
2268
2269
2270 iDev = aPosition.iDeviceFunc;
2271 /*
2272 * Check if we can really take this slot, possibly by relocating
2273 * its current habitant, if it wasn't hard assigned too.
2274 */
2275 if (pciDevIsRequestedDevfunc(pPciDev) &&
2276 pBus->apDevices[iDev] &&
2277 pciDevIsRequestedDevfunc(pBus->apDevices[iDev]))
2278 {
2279 AssertReleaseMsgFailed(("Configuration error:'%s' and '%s' are both configured as device %d\n",
2280 pszName, pBus->apDevices[iDev]->name, iDev));
2281 return VERR_INTERNAL_ERROR;
2282 }
2283
2284 if (pBus->apDevices[iDev])
2285 {
2286 /* if we got here, we shall (and usually can) relocate the device */
2287 bool assigned = assignPosition(pBus, pBus->apDevices[iDev], pBus->apDevices[iDev]->name, -1, &aPosition);
2288 AssertMsgReturn(aPosition.iBus == 0,
2289 ("Assigning behind the bridge not implemented yet\n"),
2290 VERR_PDM_TOO_PCI_MANY_DEVICES);
2291 int iRelDev = aPosition.iDeviceFunc;
2292 if (!assigned || iRelDev == iDev)
2293 {
2294 AssertMsgFailed(("Couldn't find free spot!\n"));
2295 return VERR_PDM_TOO_PCI_MANY_DEVICES;
2296 }
2297 /* Copy device function by function to its new position */
2298 for (int i = 0; i < 8; i++)
2299 {
2300 if (!pBus->apDevices[iDev + i])
2301 continue;
2302 Log(("PCI: relocating '%s' from slot %#x to %#x\n", pBus->apDevices[iDev + i]->name, iDev + i, iRelDev + i));
2303 pBus->apDevices[iRelDev + i] = pBus->apDevices[iDev + i];
2304 pBus->apDevices[iRelDev + i]->devfn = iRelDev + i;
2305 pBus->apDevices[iDev + i] = NULL;
2306 }
2307 }
2308
2309 /*
2310 * Fill in device information.
2311 */
2312 pPciDev->devfn = iDev;
2313 pPciDev->name = pszName;
2314 pPciDev->Int.s.pBusR3 = pBus;
2315 pPciDev->Int.s.pBusR0 = MMHyperR3ToR0(PDMDevHlpGetVM(pBus->CTX_SUFF(pDevIns)), pBus);
2316 pPciDev->Int.s.pBusRC = MMHyperR3ToRC(PDMDevHlpGetVM(pBus->CTX_SUFF(pDevIns)), pBus);
2317 pPciDev->Int.s.pfnConfigRead = ich9pciConfigReadDev;
2318 pPciDev->Int.s.pfnConfigWrite = ich9pciConfigWriteDev;
2319 pBus->apDevices[iDev] = pPciDev;
2320 if (pciDevIsPci2PciBridge(pPciDev))
2321 {
2322 AssertMsg(pBus->cBridges < RT_ELEMENTS(pBus->apDevices), ("Number of bridges exceeds the number of possible devices on the bus\n"));
2323 AssertMsg(pPciDev->Int.s.pfnBridgeConfigRead && pPciDev->Int.s.pfnBridgeConfigWrite,
2324 ("device is a bridge but does not implement read/write functions\n"));
2325 Log2(("Setting bridge %d on bus %p\n", pBus->cBridges, pBus));
2326 pBus->papBridgesR3[pBus->cBridges] = pPciDev;
2327 pBus->cBridges++;
2328 }
2329
2330 Log(("PCI: Registered device %d function %d on bus %d (%#x) '%s'.\n",
2331 iDev >> 3, iDev & 7, pBus->iBus, 0x80000000 | (iDev << 8), pszName));
2332
2333 return VINF_SUCCESS;
2334}
2335
2336static void printIndent(PCDBGFINFOHLP pHlp, int iIndent)
2337{
2338 for (int i = 0; i < iIndent; i++)
2339 {
2340 pHlp->pfnPrintf(pHlp, " ");
2341 }
2342}
2343
2344static void ich9pciBusInfo(PICH9PCIBUS pBus, PCDBGFINFOHLP pHlp, int iIndent, bool fRegisters)
2345{
2346 for (uint32_t iDev = 0; iDev < RT_ELEMENTS(pBus->apDevices); iDev++)
2347 {
2348 PPCIDEVICE pPciDev = pBus->apDevices[iDev];
2349 if (pPciDev != NULL)
2350 {
2351 printIndent(pHlp, iIndent);
2352
2353 /*
2354 * For passthrough devices MSI/MSI-X mostly reflects the way interrupts delivered to the guest,
2355 * as host driver handles real devices interrupts.
2356 */
2357 pHlp->pfnPrintf(pHlp, "%02x:%02x:%02x %s%s: %04x-%04x",
2358 pBus->iBus, (iDev >> 3) & 0xff, iDev & 0x7,
2359 pPciDev->name,
2360 pciDevIsPassthrough(pPciDev) ? " (PASSTHROUGH)" : "",
2361 ich9pciGetWord(pPciDev, VBOX_PCI_VENDOR_ID), ich9pciGetWord(pPciDev, VBOX_PCI_DEVICE_ID)
2362 );
2363 if (ich9pciGetByte(pPciDev, VBOX_PCI_INTERRUPT_PIN) != 0)
2364 pHlp->pfnPrintf(pHlp, " IRQ%d", ich9pciGetByte(pPciDev, VBOX_PCI_INTERRUPT_LINE));
2365 pHlp->pfnPrintf(pHlp, "\n");
2366
2367 if (pciDevIsMsiCapable(pPciDev) || pciDevIsMsixCapable(pPciDev))
2368 {
2369 printIndent(pHlp, iIndent + 2);
2370
2371 if (pciDevIsMsiCapable(pPciDev))
2372 pHlp->pfnPrintf(pHlp, "MSI:%s ", MsiIsEnabled(pPciDev) ? "on" : "off");
2373
2374 if (pciDevIsMsixCapable(pPciDev))
2375 pHlp->pfnPrintf(pHlp, "MSI-X:%s ", MsixIsEnabled(pPciDev) ? "on" : "off");
2376
2377 pHlp->pfnPrintf(pHlp, "\n");
2378 }
2379
2380 uint16_t iCmd = ich9pciGetWord(pPciDev, VBOX_PCI_COMMAND);
2381 if ((iCmd & (VBOX_PCI_COMMAND_IO | VBOX_PCI_COMMAND_MEMORY)) != 0)
2382 {
2383 for (int iRegion = 0; iRegion < PCI_NUM_REGIONS; iRegion++)
2384 {
2385 PCIIORegion* pRegion = &pPciDev->Int.s.aIORegions[iRegion];
2386 uint64_t iRegionSize = pRegion->size;
2387
2388 if (iRegionSize == 0)
2389 continue;
2390
2391 uint32_t u32Addr = ich9pciGetDWord(pPciDev, ich9pciGetRegionReg(iRegion));
2392 const char * pszDesc;
2393 char szDescBuf[128];
2394
2395 bool f64Bit = !!(pRegion->type & PCI_ADDRESS_SPACE_BAR64);
2396 if (pRegion->type & PCI_ADDRESS_SPACE_IO)
2397 {
2398 pszDesc = "IO";
2399 u32Addr &= ~0x3;
2400 }
2401 else
2402 {
2403 RTStrPrintf(szDescBuf, sizeof(szDescBuf), "MMIO%s%s",
2404 f64Bit ? "64" : "32",
2405 (pRegion->type & PCI_ADDRESS_SPACE_MEM_PREFETCH) ? " PREFETCH" : "");
2406 pszDesc = szDescBuf;
2407 u32Addr &= ~0xf;
2408 }
2409
2410 printIndent(pHlp, iIndent + 2);
2411 pHlp->pfnPrintf(pHlp, "%s region #%d: %x..%x\n",
2412 pszDesc, iRegion, u32Addr, u32Addr+iRegionSize);
2413 if (f64Bit)
2414 iRegion++;
2415 }
2416 }
2417
2418 printIndent(pHlp, iIndent + 2);
2419 uint16_t iStatus = ich9pciGetWord(pPciDev, VBOX_PCI_STATUS);
2420 pHlp->pfnPrintf(pHlp, "Command: %04X, Status: %04X\n",
2421 iCmd, iStatus);
2422 printIndent(pHlp, iIndent + 2);
2423 pHlp->pfnPrintf(pHlp, "Bus master: %s\n",
2424 iCmd & VBOX_PCI_COMMAND_MASTER ? "Yes" : "No");
2425
2426 if (fRegisters)
2427 {
2428 printIndent(pHlp, iIndent + 2);
2429 pHlp->pfnPrintf(pHlp, "PCI registers:\n");
2430 for (int iReg = 0; iReg < 0x100; )
2431 {
2432 int iPerLine = 0x10;
2433 Assert (0x100 % iPerLine == 0);
2434 printIndent(pHlp, iIndent + 3);
2435
2436 while (iPerLine-- > 0)
2437 {
2438 pHlp->pfnPrintf(pHlp, "%02x ", ich9pciGetByte(pPciDev, iReg++));
2439 }
2440 pHlp->pfnPrintf(pHlp, "\n");
2441 }
2442 }
2443 }
2444 }
2445
2446 if (pBus->cBridges > 0)
2447 {
2448 printIndent(pHlp, iIndent);
2449 pHlp->pfnPrintf(pHlp, "Registered %d bridges, subordinate buses info follows\n", pBus->cBridges);
2450 for (uint32_t iBridge = 0; iBridge < pBus->cBridges; iBridge++)
2451 {
2452 PICH9PCIBUS pBusSub = PDMINS_2_DATA(pBus->papBridgesR3[iBridge]->pDevIns, PICH9PCIBUS);
2453 ich9pciBusInfo(pBusSub, pHlp, iIndent + 1, fRegisters);
2454 }
2455 }
2456}
2457
2458/**
2459 * Info handler, device version.
2460 *
2461 * @param pDevIns Device instance which registered the info.
2462 * @param pHlp Callback functions for doing output.
2463 * @param pszArgs Argument string. Optional and specific to the handler.
2464 */
2465static DECLCALLBACK(void) ich9pciInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
2466{
2467 PICH9PCIBUS pBus = DEVINS_2_PCIBUS(pDevIns);
2468
2469 if (pszArgs == NULL || !strcmp(pszArgs, "basic"))
2470 {
2471 ich9pciBusInfo(pBus, pHlp, 0, false);
2472 }
2473 else if (!strcmp(pszArgs, "verbose"))
2474 {
2475 ich9pciBusInfo(pBus, pHlp, 0, true);
2476 }
2477 else
2478 {
2479 pHlp->pfnPrintf(pHlp, "Invalid argument. Recognized arguments are 'basic', 'verbose'.\n");
2480 }
2481}
2482
2483
2484static DECLCALLBACK(int) ich9pciConstruct(PPDMDEVINS pDevIns,
2485 int iInstance,
2486 PCFGMNODE pCfg)
2487{
2488 Assert(iInstance == 0);
2489 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2490
2491 /*
2492 * Validate and read configuration.
2493 */
2494 if (!CFGMR3AreValuesValid(pCfg,
2495 "IOAPIC\0"
2496 "GCEnabled\0"
2497 "R0Enabled\0"
2498 "McfgBase\0"
2499 "McfgLength\0"
2500 ))
2501 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
2502
2503 /* query whether we got an IOAPIC */
2504 bool fUseIoApic;
2505 int rc = CFGMR3QueryBoolDef(pCfg, "IOAPIC", &fUseIoApic, false);
2506 if (RT_FAILURE(rc))
2507 return PDMDEV_SET_ERROR(pDevIns, rc,
2508 N_("Configuration error: Failed to query boolean value \"IOAPIC\""));
2509
2510 /* check if RC code is enabled. */
2511 bool fGCEnabled;
2512 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &fGCEnabled, true);
2513 if (RT_FAILURE(rc))
2514 return PDMDEV_SET_ERROR(pDevIns, rc,
2515 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
2516 /* check if R0 code is enabled. */
2517 bool fR0Enabled;
2518 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &fR0Enabled, true);
2519 if (RT_FAILURE(rc))
2520 return PDMDEV_SET_ERROR(pDevIns, rc,
2521 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
2522
2523 Log(("PCI: fUseIoApic=%RTbool fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fUseIoApic, fGCEnabled, fR0Enabled));
2524
2525 /*
2526 * Init data.
2527 */
2528 PICH9PCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
2529 PICH9PCIBUS pBus = &pGlobals->aPciBus;
2530 /* Zero out everything */
2531 memset(pGlobals, 0, sizeof(*pGlobals));
2532 /* And fill values */
2533 if (!fUseIoApic)
2534 return PDMDEV_SET_ERROR(pDevIns, rc,
2535 N_("Must use IO-APIC with ICH9 chipset"));
2536 rc = CFGMR3QueryU64Def(pCfg, "McfgBase", &pGlobals->u64PciConfigMMioAddress, 0);
2537 if (RT_FAILURE(rc))
2538 return PDMDEV_SET_ERROR(pDevIns, rc,
2539 N_("Configuration error: Failed to read \"McfgBase\""));
2540 rc = CFGMR3QueryU64Def(pCfg, "McfgLength", &pGlobals->u64PciConfigMMioLength, 0);
2541 if (RT_FAILURE(rc))
2542 return PDMDEV_SET_ERROR(pDevIns, rc,
2543 N_("Configuration error: Failed to read \"McfgLength\""));
2544
2545 pGlobals->pDevInsR3 = pDevIns;
2546 pGlobals->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2547 pGlobals->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2548
2549 pGlobals->aPciBus.pDevInsR3 = pDevIns;
2550 pGlobals->aPciBus.pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2551 pGlobals->aPciBus.pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2552 pGlobals->aPciBus.papBridgesR3 = (PPCIDEVICE *)PDMDevHlpMMHeapAllocZ(pDevIns, sizeof(PPCIDEVICE) * RT_ELEMENTS(pGlobals->aPciBus.apDevices));
2553
2554 /*
2555 * Register bus
2556 */
2557 PDMPCIBUSREG PciBusReg;
2558 PciBusReg.u32Version = PDM_PCIBUSREG_VERSION;
2559 PciBusReg.pfnRegisterR3 = ich9pciRegister;
2560 PciBusReg.pfnRegisterMsiR3 = ich9pciRegisterMsi;
2561 PciBusReg.pfnIORegionRegisterR3 = ich9pciIORegionRegister;
2562 PciBusReg.pfnSetConfigCallbacksR3 = ich9pciSetConfigCallbacks;
2563 PciBusReg.pfnSetIrqR3 = ich9pciSetIrq;
2564 PciBusReg.pfnFakePCIBIOSR3 = ich9pciFakePCIBIOS;
2565 PciBusReg.pszSetIrqRC = fGCEnabled ? "ich9pciSetIrq" : NULL;
2566 PciBusReg.pszSetIrqR0 = fR0Enabled ? "ich9pciSetIrq" : NULL;
2567 rc = PDMDevHlpPCIBusRegister(pDevIns, &PciBusReg, &pBus->pPciHlpR3);
2568 if (RT_FAILURE(rc))
2569 return PDMDEV_SET_ERROR(pDevIns, rc,
2570 N_("Failed to register ourselves as a PCI Bus"));
2571 if (pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION)
2572 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2573 N_("PCI helper version mismatch; got %#x expected %#x"),
2574 pBus->pPciHlpR3->u32Version, PDM_PCIHLPR3_VERSION);
2575
2576 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2577 pBus->pPciHlpR0 = pBus->pPciHlpR3->pfnGetR0Helpers(pDevIns);
2578
2579 /*
2580 * Fill in PCI configs and add them to the bus.
2581 */
2582 /** @todo: Disabled for now because this causes error messages with Linux guests.
2583 * The guest loads the x38_edac device which tries to map a memory region
2584 * using an address given at place 0x48 - 0x4f in the PCi config space.
2585 * This fails. because we don't register such a region.
2586 */
2587#if 0
2588 /* Host bridge device */
2589 PCIDevSetVendorId( &pBus->aPciDev, 0x8086); /* Intel */
2590 PCIDevSetDeviceId( &pBus->aPciDev, 0x29e0); /* Desktop */
2591 PCIDevSetRevisionId(&pBus->aPciDev, 0x01); /* rev. 01 */
2592 PCIDevSetClassBase( &pBus->aPciDev, 0x06); /* bridge */
2593 PCIDevSetClassSub( &pBus->aPciDev, 0x00); /* Host/PCI bridge */
2594 PCIDevSetClassProg( &pBus->aPciDev, 0x00); /* Host/PCI bridge */
2595 PCIDevSetHeaderType(&pBus->aPciDev, 0x00); /* bridge */
2596 PCIDevSetWord(&pBus->aPciDev, VBOX_PCI_SEC_STATUS, 0x0280); /* secondary status */
2597
2598 pBus->aPciDev.pDevIns = pDevIns;
2599 /* We register Host<->PCI controller on the bus */
2600 ich9pciRegisterInternal(pBus, 0, &pBus->aPciDev, "dram");
2601#endif
2602
2603 /*
2604 * Register I/O ports and save state.
2605 */
2606 rc = PDMDevHlpIOPortRegister(pDevIns, 0x0cf8, 1, NULL, ich9pciIOPortAddressWrite, ich9pciIOPortAddressRead, NULL, NULL, "ICH9 (PCI)");
2607 if (RT_FAILURE(rc))
2608 return rc;
2609 rc = PDMDevHlpIOPortRegister(pDevIns, 0x0cfc, 4, NULL, ich9pciIOPortDataWrite, ich9pciIOPortDataRead, NULL, NULL, "ICH9 (PCI)");
2610 if (RT_FAILURE(rc))
2611 return rc;
2612 if (fGCEnabled)
2613 {
2614 rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x0cf8, 1, NIL_RTGCPTR, "ich9pciIOPortAddressWrite", "ich9pciIOPortAddressRead", NULL, NULL, "ICH9 (PCI)");
2615 if (RT_FAILURE(rc))
2616 return rc;
2617 rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x0cfc, 4, NIL_RTGCPTR, "ich9pciIOPortDataWrite", "ich9pciIOPortDataRead", NULL, NULL, "ICH9 (PCI)");
2618 if (RT_FAILURE(rc))
2619 return rc;
2620 }
2621 if (fR0Enabled)
2622 {
2623 rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x0cf8, 1, NIL_RTR0PTR, "ich9pciIOPortAddressWrite", "ich9pciIOPortAddressRead", NULL, NULL, "ICH9 (PCI)");
2624 if (RT_FAILURE(rc))
2625 return rc;
2626 rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x0cfc, 4, NIL_RTR0PTR, "ich9pciIOPortDataWrite", "ich9pciIOPortDataRead", NULL, NULL, "ICH9 (PCI)");
2627 if (RT_FAILURE(rc))
2628 return rc;
2629 }
2630
2631 if (pGlobals->u64PciConfigMMioAddress != 0)
2632 {
2633 rc = PDMDevHlpMMIORegister(pDevIns, pGlobals->u64PciConfigMMioAddress, pGlobals->u64PciConfigMMioLength, NULL /*pvUser*/,
2634 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
2635 ich9pciMcfgMMIOWrite, ich9pciMcfgMMIORead, "MCFG ranges");
2636 AssertMsgRCReturn(rc, ("rc=%Rrc %#llx/%#llx\n", rc, pGlobals->u64PciConfigMMioAddress, pGlobals->u64PciConfigMMioLength), rc);
2637
2638 if (fGCEnabled)
2639 {
2640 rc = PDMDevHlpMMIORegisterRC(pDevIns, pGlobals->u64PciConfigMMioAddress, pGlobals->u64PciConfigMMioLength,
2641 NIL_RTRCPTR /*pvUser*/, "ich9pciMcfgMMIOWrite", "ich9pciMcfgMMIORead");
2642 AssertRCReturn(rc, rc);
2643 }
2644
2645
2646 if (fR0Enabled)
2647 {
2648 rc = PDMDevHlpMMIORegisterR0(pDevIns, pGlobals->u64PciConfigMMioAddress, pGlobals->u64PciConfigMMioLength,
2649 NIL_RTR0PTR /*pvUser*/, "ich9pciMcfgMMIOWrite", "ich9pciMcfgMMIORead");
2650 AssertRCReturn(rc, rc);
2651 }
2652 }
2653
2654 rc = PDMDevHlpSSMRegisterEx(pDevIns, VBOX_ICH9PCI_SAVED_STATE_VERSION_CURRENT,
2655 sizeof(*pBus) + 16*128, "pgm",
2656 NULL, NULL, NULL,
2657 NULL, ich9pciR3SaveExec, NULL,
2658 NULL, ich9pciR3LoadExec, NULL);
2659 if (RT_FAILURE(rc))
2660 return rc;
2661
2662
2663 /** @todo: other chipset devices shall be registered too */
2664
2665 PDMDevHlpDBGFInfoRegister(pDevIns, "pci", "Display PCI bus status. Recognizes 'basic' or 'verbose' "
2666 "as arguments, defaults to 'basic'.", ich9pciInfo);
2667
2668 return VINF_SUCCESS;
2669}
2670
2671static void ich9pciResetDevice(PPCIDEVICE pDev)
2672{
2673 /* Clear regions */
2674 for (int iRegion = 0; iRegion < PCI_NUM_REGIONS; iRegion++)
2675 {
2676 PCIIORegion* pRegion = &pDev->Int.s.aIORegions[iRegion];
2677 if (pRegion->size == 0)
2678 continue;
2679
2680 ich9pciUnmapRegion(pDev, iRegion);
2681 }
2682
2683 if (pciDevIsPassthrough(pDev))
2684 {
2685 // no reset handler - we can do what we need in PDM reset handler
2686 // @todo: is it correct?
2687 }
2688 else
2689 {
2690 PCIDevSetCommand(pDev,
2691 PCIDevGetCommand(pDev)
2692 &
2693 ~(VBOX_PCI_COMMAND_IO | VBOX_PCI_COMMAND_MEMORY |
2694 VBOX_PCI_COMMAND_MASTER | VBOX_PCI_COMMAND_SPECIAL |
2695 VBOX_PCI_COMMAND_PARITY | VBOX_PCI_COMMAND_SERR |
2696 VBOX_PCI_COMMAND_FAST_BACK | VBOX_PCI_COMMAND_INTX_DISABLE));
2697
2698 /* Bridge device reset handlers processed later */
2699 if (!pciDevIsPci2PciBridge(pDev))
2700 {
2701 PCIDevSetByte(pDev, VBOX_PCI_CACHE_LINE_SIZE, 0x0);
2702 PCIDevSetInterruptLine(pDev, 0x0);
2703 }
2704
2705 /* Reset MSI message control. */
2706 if (pciDevIsMsiCapable(pDev))
2707 {
2708 /* Extracted from MsiPciConfigWrite(). */
2709 pDev->config[pDev->Int.s.u8MsiCapOffset + VBOX_MSI_CAP_MESSAGE_CONTROL] &= 0x8e;
2710 }
2711
2712 /* Reset MSI-X message control. */
2713 if (pciDevIsMsixCapable(pDev))
2714 {
2715 /* Extracted from MsixPciConfigWrite(); no side effects. */
2716 pDev->config[pDev->Int.s.u8MsixCapOffset + VBOX_MSIX_CAP_MESSAGE_CONTROL + 1] &= 0x3f;
2717 }
2718 }
2719}
2720
2721
2722/**
2723 * @copydoc FNPDMDEVRESET
2724 */
2725static DECLCALLBACK(void) ich9pciReset(PPDMDEVINS pDevIns)
2726{
2727 PICH9PCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
2728 PICH9PCIBUS pBus = &pGlobals->aPciBus;
2729
2730 /* PCI-specific reset for each device. */
2731 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
2732 {
2733 if (pBus->apDevices[i])
2734 ich9pciResetDevice(pBus->apDevices[i]);
2735 }
2736
2737 for (uint32_t iBridge = 0; iBridge < pBus->cBridges; iBridge++)
2738 {
2739 if (pBus->papBridgesR3[iBridge])
2740 ich9pcibridgeReset(pBus->papBridgesR3[iBridge]->pDevIns);
2741 }
2742
2743 ich9pciFakePCIBIOS(pDevIns);
2744}
2745
2746static void ich9pciRelocateDevice(PPCIDEVICE pDev, RTGCINTPTR offDelta)
2747{
2748 if (pDev)
2749 {
2750 pDev->Int.s.pBusRC += offDelta;
2751 if (pDev->Int.s.pMsixPageRC)
2752 pDev->Int.s.pMsixPageRC += offDelta;
2753 }
2754}
2755
2756/**
2757 * @copydoc FNPDMDEVRELOCATE
2758 */
2759static DECLCALLBACK(void) ich9pciRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2760{
2761 PICH9PCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PICH9PCIGLOBALS);
2762 PICH9PCIBUS pBus = &pGlobals->aPciBus;
2763 pGlobals->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2764
2765 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2766 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2767
2768 /* Relocate RC pointers for the attached pci devices. */
2769 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
2770 ich9pciRelocateDevice(pBus->apDevices[i], offDelta);
2771
2772}
2773
2774/**
2775 * @interface_method_impl{PDMDEVREG,pfnConstruct}
2776 */
2777static DECLCALLBACK(int) ich9pcibridgeConstruct(PPDMDEVINS pDevIns,
2778 int iInstance,
2779 PCFGMNODE pCfg)
2780{
2781 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2782
2783 /*
2784 * Validate and read configuration.
2785 */
2786 if (!CFGMR3AreValuesValid(pCfg, "GCEnabled\0" "R0Enabled\0"))
2787 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
2788
2789 /* check if RC code is enabled. */
2790 bool fGCEnabled;
2791 int rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &fGCEnabled, true);
2792 if (RT_FAILURE(rc))
2793 return PDMDEV_SET_ERROR(pDevIns, rc,
2794 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
2795
2796 /* check if R0 code is enabled. */
2797 bool fR0Enabled;
2798 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &fR0Enabled, true);
2799 if (RT_FAILURE(rc))
2800 return PDMDEV_SET_ERROR(pDevIns, rc,
2801 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
2802 Log(("PCI: fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fGCEnabled, fR0Enabled));
2803
2804 /*
2805 * Init data and register the PCI bus.
2806 */
2807 PICH9PCIBUS pBus = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
2808 pBus->pDevInsR3 = pDevIns;
2809 pBus->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2810 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2811 pBus->papBridgesR3 = (PPCIDEVICE *)PDMDevHlpMMHeapAllocZ(pDevIns, sizeof(PPCIDEVICE) * RT_ELEMENTS(pBus->apDevices));
2812
2813 PDMPCIBUSREG PciBusReg;
2814 PciBusReg.u32Version = PDM_PCIBUSREG_VERSION;
2815 PciBusReg.pfnRegisterR3 = ich9pcibridgeRegister;
2816 PciBusReg.pfnRegisterMsiR3 = ich9pciRegisterMsi;
2817 PciBusReg.pfnIORegionRegisterR3 = ich9pciIORegionRegister;
2818 PciBusReg.pfnSetConfigCallbacksR3 = ich9pciSetConfigCallbacks;
2819 PciBusReg.pfnSetIrqR3 = ich9pcibridgeSetIrq;
2820 PciBusReg.pfnFakePCIBIOSR3 = NULL; /* Only needed for the first bus. */
2821 PciBusReg.pszSetIrqRC = fGCEnabled ? "ich9pcibridgeSetIrq" : NULL;
2822 PciBusReg.pszSetIrqR0 = fR0Enabled ? "ich9pcibridgeSetIrq" : NULL;
2823 rc = PDMDevHlpPCIBusRegister(pDevIns, &PciBusReg, &pBus->pPciHlpR3);
2824 if (RT_FAILURE(rc))
2825 return PDMDEV_SET_ERROR(pDevIns, rc,
2826 N_("Failed to register ourselves as a PCI Bus"));
2827 if (pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION)
2828 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2829 N_("PCI helper version mismatch; got %#x expected %#x"),
2830 pBus->pPciHlpR3->u32Version, PDM_PCIHLPR3_VERSION);
2831
2832 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2833 pBus->pPciHlpR0 = pBus->pPciHlpR3->pfnGetR0Helpers(pDevIns);
2834
2835 /* Disable default device locking. */
2836 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
2837 AssertRCReturn(rc, rc);
2838
2839 /*
2840 * Fill in PCI configs and add them to the bus.
2841 */
2842 PCIDevSetVendorId( &pBus->aPciDev, 0x8086); /* Intel */
2843 PCIDevSetDeviceId( &pBus->aPciDev, 0x2448); /* 82801 Mobile PCI bridge. */
2844 PCIDevSetRevisionId(&pBus->aPciDev, 0xf2);
2845 PCIDevSetClassSub( &pBus->aPciDev, 0x04); /* pci2pci */
2846 PCIDevSetClassBase( &pBus->aPciDev, 0x06); /* PCI_bridge */
2847 PCIDevSetClassProg( &pBus->aPciDev, 0x01); /* Supports subtractive decoding. */
2848 PCIDevSetHeaderType(&pBus->aPciDev, 0x01); /* Single function device which adheres to the PCI-to-PCI bridge spec. */
2849 PCIDevSetCommand( &pBus->aPciDev, 0x00);
2850 PCIDevSetStatus( &pBus->aPciDev, 0x20); /* 66MHz Capable. */
2851 PCIDevSetInterruptLine(&pBus->aPciDev, 0x00); /* This device does not assert interrupts. */
2852
2853 /*
2854 * This device does not generate interrupts. Interrupt delivery from
2855 * devices attached to the bus is unaffected.
2856 */
2857 PCIDevSetInterruptPin (&pBus->aPciDev, 0x00);
2858
2859 pBus->aPciDev.pDevIns = pDevIns;
2860
2861 /* Bridge-specific data */
2862 pciDevSetPci2PciBridge(&pBus->aPciDev);
2863 pBus->aPciDev.Int.s.pfnBridgeConfigRead = ich9pcibridgeConfigRead;
2864 pBus->aPciDev.Int.s.pfnBridgeConfigWrite = ich9pcibridgeConfigWrite;
2865
2866 /*
2867 * Register this PCI bridge. The called function will take care on which bus we will get registered.
2868 */
2869 rc = PDMDevHlpPCIRegister (pDevIns, &pBus->aPciDev);
2870 if (RT_FAILURE(rc))
2871 return rc;
2872
2873 /*
2874 * The iBus property doesn't really represent the bus number
2875 * because the guest and the BIOS can choose different bus numbers
2876 * for them.
2877 * The bus number is mainly for the setIrq function to indicate
2878 * when the host bus is reached which will have iBus = 0.
2879 * That's why the + 1.
2880 */
2881 pBus->iBus = iInstance + 1;
2882
2883 /*
2884 * Register SSM handlers. We use the same saved state version as for the host bridge
2885 * to make changes easier.
2886 */
2887 rc = PDMDevHlpSSMRegisterEx(pDevIns, VBOX_ICH9PCI_SAVED_STATE_VERSION_CURRENT,
2888 sizeof(*pBus) + 16*128,
2889 "pgm" /* before */,
2890 NULL, NULL, NULL,
2891 NULL, ich9pcibridgeR3SaveExec, NULL,
2892 NULL, ich9pcibridgeR3LoadExec, NULL);
2893 if (RT_FAILURE(rc))
2894 return rc;
2895
2896
2897 return VINF_SUCCESS;
2898}
2899
2900/**
2901 * @copydoc FNPDMDEVRESET
2902 */
2903static void ich9pcibridgeReset(PPDMDEVINS pDevIns)
2904{
2905 PICH9PCIBUS pBus = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
2906
2907 /* Reset config space to default values. */
2908 PCIDevSetByte(&pBus->aPciDev, VBOX_PCI_PRIMARY_BUS, 0);
2909 PCIDevSetByte(&pBus->aPciDev, VBOX_PCI_SECONDARY_BUS, 0);
2910 PCIDevSetByte(&pBus->aPciDev, VBOX_PCI_SUBORDINATE_BUS, 0);
2911
2912 /* PCI-specific reset for each device. */
2913 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
2914 {
2915 if (pBus->apDevices[i])
2916 ich9pciResetDevice(pBus->apDevices[i]);
2917 }
2918}
2919
2920
2921/**
2922 * @copydoc FNPDMDEVRELOCATE
2923 */
2924static DECLCALLBACK(void) ich9pcibridgeRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2925{
2926 PICH9PCIBUS pBus = PDMINS_2_DATA(pDevIns, PICH9PCIBUS);
2927 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2928
2929 /* Relocate RC pointers for the attached pci devices. */
2930 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
2931 ich9pciRelocateDevice(pBus->apDevices[i], offDelta);
2932}
2933
2934/**
2935 * The PCI bus device registration structure.
2936 */
2937const PDMDEVREG g_DevicePciIch9 =
2938{
2939 /* u32Version */
2940 PDM_DEVREG_VERSION,
2941 /* szName */
2942 "ich9pci",
2943 /* szRCMod */
2944 "VBoxDDGC.gc",
2945 /* szR0Mod */
2946 "VBoxDDR0.r0",
2947 /* pszDescription */
2948 "ICH9 PCI bridge",
2949 /* fFlags */
2950 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2951 /* fClass */
2952 PDM_DEVREG_CLASS_BUS_PCI | PDM_DEVREG_CLASS_BUS_ISA,
2953 /* cMaxInstances */
2954 1,
2955 /* cbInstance */
2956 sizeof(ICH9PCIGLOBALS),
2957 /* pfnConstruct */
2958 ich9pciConstruct,
2959 /* pfnDestruct */
2960 NULL,
2961 /* pfnRelocate */
2962 ich9pciRelocate,
2963 /* pfnMemSetup */
2964 NULL,
2965 /* pfnPowerOn */
2966 NULL,
2967 /* pfnReset */
2968 ich9pciReset,
2969 /* pfnSuspend */
2970 NULL,
2971 /* pfnResume */
2972 NULL,
2973 /* pfnAttach */
2974 NULL,
2975 /* pfnDetach */
2976 NULL,
2977 /* pfnQueryInterface */
2978 NULL,
2979 /* pfnInitComplete */
2980 NULL,
2981 /* pfnPowerOff */
2982 NULL,
2983 /* pfnSoftReset */
2984 NULL,
2985 /* u32VersionEnd */
2986 PDM_DEVREG_VERSION
2987};
2988
2989/**
2990 * The device registration structure
2991 * for the PCI-to-PCI bridge.
2992 */
2993const PDMDEVREG g_DevicePciIch9Bridge =
2994{
2995 /* u32Version */
2996 PDM_DEVREG_VERSION,
2997 /* szName */
2998 "ich9pcibridge",
2999 /* szRCMod */
3000 "VBoxDDGC.gc",
3001 /* szR0Mod */
3002 "VBoxDDR0.r0",
3003 /* pszDescription */
3004 "ICH9 PCI to PCI bridge",
3005 /* fFlags */
3006 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
3007 /* fClass */
3008 PDM_DEVREG_CLASS_BUS_PCI,
3009 /* cMaxInstances */
3010 ~0U,
3011 /* cbInstance */
3012 sizeof(ICH9PCIBUS),
3013 /* pfnConstruct */
3014 ich9pcibridgeConstruct,
3015 /* pfnDestruct */
3016 NULL,
3017 /* pfnRelocate */
3018 ich9pcibridgeRelocate,
3019 /* pfnMemSetup */
3020 NULL,
3021 /* pfnPowerOn */
3022 NULL,
3023 /* pfnReset */
3024 NULL, /* Must be NULL, to make sure only bus driver handles reset */
3025 /* pfnSuspend */
3026 NULL,
3027 /* pfnResume */
3028 NULL,
3029 /* pfnAttach */
3030 NULL,
3031 /* pfnDetach */
3032 NULL,
3033 /* pfnQueryInterface */
3034 NULL,
3035 /* pfnInitComplete */
3036 NULL,
3037 /* pfnPowerOff */
3038 NULL,
3039 /* pfnSoftReset */
3040 NULL,
3041 /* u32VersionEnd */
3042 PDM_DEVREG_VERSION
3043};
3044
3045#endif /* IN_RING3 */
3046#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

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