VirtualBox

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

Last change on this file since 62629 was 62389, checked in by vboxsync, 9 years ago

PCI: fix limit check for 64-bit BAR index

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

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