VirtualBox

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

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

PCI: refactoring for PCIe work

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 88.8 KB
Line 
1/* $Id: DevPciIch9.cpp 32776 2010-09-27 13:31:42Z vboxsync $ */
2/** @file
3 * DevPCI - ICH9 southbridge PCI bus emulation Device.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19 * Header Files *
20 *******************************************************************************/
21#define LOG_GROUP LOG_GROUP_DEV_PCI
22/* Hack to get PCIDEVICEINT declare at the right point - include "PCIInternal.h". */
23#define PCI_INCLUDE_PRIVATE
24#include <VBox/pci.h>
25#include <VBox/pdmdev.h>
26#include <iprt/asm.h>
27#include <iprt/assert.h>
28#include <iprt/string.h>
29
30#include "../Builtins.h"
31
32/**
33 * PCI Bus instance.
34 */
35typedef struct PCIBus
36{
37 /** Bus number. */
38 int32_t iBus;
39 /** Number of bridges attached to the bus. */
40 uint32_t cBridges;
41
42 /** Array of PCI devices. We assume 32 slots, each with 8 functions. */
43 R3PTRTYPE(PPCIDEVICE) apDevices[256];
44 /** Array of bridges attached to the bus. */
45 R3PTRTYPE(PPCIDEVICE *) papBridgesR3;
46
47 /** R3 pointer to the device instance. */
48 PPDMDEVINSR3 pDevInsR3;
49 /** Pointer to the PCI R3 helpers. */
50 PCPDMPCIHLPR3 pPciHlpR3;
51
52 /** R0 pointer to the device instance. */
53 PPDMDEVINSR0 pDevInsR0;
54 /** Pointer to the PCI R0 helpers. */
55 PCPDMPCIHLPR0 pPciHlpR0;
56
57 /** RC pointer to the device instance. */
58 PPDMDEVINSRC pDevInsRC;
59 /** Pointer to the PCI RC helpers. */
60 PCPDMPCIHLPRC pPciHlpRC;
61
62 /** The PCI device for the PCI bridge. */
63 PCIDEVICE aPciDev;
64
65} PCIBUS, *PPCIBUS;
66
67
68/** @def PCI_IRQ_PINS
69 * Number of pins for interrupts (PIRQ#0...PIRQ#3)
70 */
71#define PCI_IRQ_PINS 4
72
73/** @def PCI_APIC_IRQ_PINS
74 * Number of pins for interrupts if the APIC is used.
75 */
76#define PCI_APIC_IRQ_PINS 8
77
78/**
79 * PCI Globals - This is the host-to-pci bridge and the root bus.
80 */
81typedef struct
82{
83 /** R3 pointer to the device instance. */
84 PPDMDEVINSR3 pDevInsR3;
85 /** R0 pointer to the device instance. */
86 PPDMDEVINSR0 pDevInsR0;
87 /** RC pointer to the device instance. */
88 PPDMDEVINSRC pDevInsRC;
89
90#if HC_ARCH_BITS == 64
91 uint32_t Alignment0;
92#endif
93
94 /** I/O APIC irq levels */
95 volatile uint32_t uaPciApicIrqLevels[PCI_APIC_IRQ_PINS];
96
97#if 1 /* Will be moved into the BIOS soon. */
98 /** The next I/O port address which the PCI BIOS will use. */
99 uint32_t uPciBiosIo;
100 /** The next MMIO address which the PCI BIOS will use. */
101 uint32_t uPciBiosMmio;
102 /** Actual bus number. */
103 uint8_t uBus;
104#endif
105 /* Physical address of PCI config space MMIO region */
106 uint64_t u64PciConfigMMioAddress;
107 /* Length of PCI config space MMIO region */
108 uint64_t u64PciConfigMMioLength;
109
110
111 /** Config register. */
112 uint32_t uConfigReg;
113
114 /** PCI bus which is attached to the host-to-PCI bridge. */
115 PCIBUS aPciBus;
116
117} PCIGLOBALS, *PPCIGLOBALS;
118
119
120typedef struct {
121 uint8_t iBus;
122 uint8_t iDeviceFunc;
123 uint16_t iRegister;
124} PciAddress;
125
126/*******************************************************************************
127 * Defined Constants And Macros *
128 *******************************************************************************/
129
130/** @def VBOX_ICH9PCI_SAVED_STATE_VERSION
131 * Saved state version of the ICH9 PCI bus device.
132 */
133#define VBOX_ICH9PCI_SAVED_STATE_VERSION 1
134
135/** Converts a bus instance pointer to a device instance pointer. */
136#define PCIBUS_2_DEVINS(pPciBus) ((pPciBus)->CTX_SUFF(pDevIns))
137/** Converts a device instance pointer to a PCIGLOBALS pointer. */
138#define DEVINS_2_PCIGLOBALS(pDevIns) ((PPCIGLOBALS)(PDMINS_2_DATA(pDevIns, PPCIGLOBALS)))
139/** Converts a device instance pointer to a PCIBUS pointer. */
140#define DEVINS_2_PCIBUS(pDevIns) ((PPCIBUS)(&PDMINS_2_DATA(pDevIns, PPCIGLOBALS)->aPciBus))
141/** Converts a pointer to a PCI root bus instance to a PCIGLOBALS pointer.
142 */
143#define PCIROOTBUS_2_PCIGLOBALS(pPciBus) ( (PPCIGLOBALS)((uintptr_t)(pPciBus) - RT_OFFSETOF(PCIGLOBALS, aPciBus)) )
144
145
146/** @def PCI_LOCK
147 * Acquires the PDM lock. This is a NOP if locking is disabled. */
148/** @def PCI_UNLOCK
149 * Releases the PDM lock. This is a NOP if locking is disabled. */
150#define PCI_LOCK(pDevIns, rc) \
151 do { \
152 int rc2 = DEVINS_2_PCIBUS(pDevIns)->CTX_SUFF(pPciHlp)->pfnLock((pDevIns), rc); \
153 if (rc2 != VINF_SUCCESS) \
154 return rc2; \
155 } while (0)
156#define PCI_UNLOCK(pDevIns) \
157 DEVINS_2_PCIBUS(pDevIns)->CTX_SUFF(pPciHlp)->pfnUnlock(pDevIns)
158
159#ifndef VBOX_DEVICE_STRUCT_TESTCASE
160
161RT_C_DECLS_BEGIN
162
163PDMBOTHCBDECL(void) ich9pciSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel);
164PDMBOTHCBDECL(void) ich9pcibridgeSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel);
165PDMBOTHCBDECL(int) ich9pciIOPortAddressWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
166PDMBOTHCBDECL(int) ich9pciIOPortAddressRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
167PDMBOTHCBDECL(int) ich9pciIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
168PDMBOTHCBDECL(int) ich9pciIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
169PDMBOTHCBDECL(int) ich9pciMcfgMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
170PDMBOTHCBDECL(int) ich9pciMcfgMMIORead (PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
171
172RT_C_DECLS_END
173
174/* Prototypes */
175static void ich9pciSetIrqInternal(PPCIGLOBALS pGlobals, uint8_t uDevFn, PPCIDEVICE pPciDev, int iIrq, int iLevel);
176#ifdef IN_RING3
177static int ich9pciRegisterInternal(PPCIBUS pBus, int iDev, PPCIDEVICE pPciDev, const char *pszName);
178static void ich9pciUpdateMappings(PCIDevice *d);
179static DECLCALLBACK(uint32_t) ich9pciConfigRead(PCIDevice *aDev, uint32_t u32Address, unsigned len);
180DECLINLINE(PPCIDEVICE) ich9pciFindBridge(PPCIBUS pBus, uint8_t iBus);
181static void ich9pciBiosInitDevice(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint8_t cBridgeDepth, uint8_t *paBridgePositions);
182#endif
183
184// See 7.2.2. PCI Express Enhanced Configuration Mechanism for details of address
185// mapping, we take n=8 approach
186DECLINLINE(void) ich9pciPhysToPciAddr(PPCIGLOBALS pGlobals, RTGCPHYS GCPhysAddr, PciAddress* pPciAddr)
187{
188 GCPhysAddr = GCPhysAddr - pGlobals->u64PciConfigMMioAddress;
189 pPciAddr->iBus = (GCPhysAddr >> 20) & ((1<<8) - 1);
190 pPciAddr->iDeviceFunc = (GCPhysAddr >> 15) & ((1<<(5+3)) - 1); // 5 bits - device, 3 bits - function
191 pPciAddr->iRegister = (GCPhysAddr >> 0) & ((1<<(6+4+2)) - 1); // 6 bits - register, 4 bits - extended register, 2 bits -Byte Enable
192}
193
194DECLINLINE(void) ich9pciStateToPciAddr(PPCIGLOBALS pGlobals, RTGCPHYS addr, PciAddress* pPciAddr)
195{
196 pPciAddr->iBus = (pGlobals->uConfigReg >> 16) & 0xff;
197 pPciAddr->iDeviceFunc = (pGlobals->uConfigReg >> 8) & 0xff;
198 pPciAddr->iRegister = (pGlobals->uConfigReg & 0xfc) | (addr & 3);
199}
200
201PDMBOTHCBDECL(void) ich9pciSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel)
202{
203 ich9pciSetIrqInternal(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), pPciDev->devfn, pPciDev, iIrq, iLevel);
204}
205
206PDMBOTHCBDECL(void) ich9pcibridgeSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel)
207{
208 /*
209 * The PCI-to-PCI bridge specification defines how the interrupt pins
210 * are routed from the secondary to the primary bus (see chapter 9).
211 * iIrq gives the interrupt pin the pci device asserted.
212 * We change iIrq here according to the spec and call the SetIrq function
213 * of our parent passing the device which asserted the interrupt instead of the device of the bridge.
214 */
215 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
216 PPCIDEVICE pPciDevBus = pPciDev;
217 int iIrqPinBridge = iIrq;
218 uint8_t uDevFnBridge = 0;
219
220 /* Walk the chain until we reach the host bus. */
221 do
222 {
223 uDevFnBridge = pBus->aPciDev.devfn;
224 iIrqPinBridge = ((pPciDevBus->devfn >> 3) + iIrqPinBridge) & 3;
225
226 /* Get the parent. */
227 pBus = pBus->aPciDev.Int.s.CTX_SUFF(pBus);
228 pPciDevBus = &pBus->aPciDev;
229 } while (pBus->iBus != 0);
230
231 AssertMsgReturnVoid(pBus->iBus == 0, ("This is not the host pci bus iBus=%d\n", pBus->iBus));
232 ich9pciSetIrqInternal(PCIROOTBUS_2_PCIGLOBALS(pBus), uDevFnBridge, pPciDev, iIrqPinBridge, iLevel);
233}
234
235/**
236 * Port I/O Handler for PCI address OUT operations.
237 *
238 * @returns VBox status code.
239 *
240 * @param pDevIns The device instance.
241 * @param pvUser User argument - ignored.
242 * @param uPort Port number used for the OUT operation.
243 * @param u32 The value to output.
244 * @param cb The value size in bytes.
245 */
246PDMBOTHCBDECL(int) ich9pciIOPortAddressWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
247{
248 Log(("ich9pciIOPortAddressWrite: Port=%#x u32=%#x cb=%d\n", Port, u32, cb));
249 NOREF(pvUser);
250 if (cb == 4)
251 {
252 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
253 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_WRITE);
254 pThis->uConfigReg = u32 & ~3; /* Bits 0-1 are reserved and we silently clear them */
255 PCI_UNLOCK(pDevIns);
256 }
257 return VINF_SUCCESS;
258}
259
260/**
261 * Port I/O Handler for PCI address IN operations.
262 *
263 * @returns VBox status code.
264 *
265 * @param pDevIns The device instance.
266 * @param pvUser User argument - ignored.
267 * @param uPort Port number used for the IN operation.
268 * @param pu32 Where to store the result.
269 * @param cb Number of bytes read.
270 */
271PDMBOTHCBDECL(int) ich9pciIOPortAddressRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
272{
273 NOREF(pvUser);
274 if (cb == 4)
275 {
276 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
277 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_READ);
278 *pu32 = pThis->uConfigReg;
279 PCI_UNLOCK(pDevIns);
280 Log(("pciIOPortAddressRead: Port=%#x cb=%d -> %#x\n", Port, cb, *pu32));
281 return VINF_SUCCESS;
282 }
283
284 Log(("ich9pciIOPortAddressRead: Port=%#x cb=%d VERR_IOM_IOPORT_UNUSED\n", Port, cb));
285
286 return VERR_IOM_IOPORT_UNUSED;
287}
288
289static int ich9pciDataWriteAddr(PPCIGLOBALS pGlobals, PciAddress* pAddr, uint32_t val, int len)
290{
291
292 if (pAddr->iRegister > 0xff)
293 {
294 LogRel(("PCI: attempt to write extended register: %x (%d) <- val\n", pAddr->iRegister, len, val));
295 return 0;
296 }
297
298 if (pAddr->iBus != 0)
299 {
300 if (pGlobals->aPciBus.cBridges)
301 {
302#ifdef IN_RING3 /** @todo do lookup in R0/RC too! */
303 PPCIDEVICE pBridgeDevice = ich9pciFindBridge(&pGlobals->aPciBus, pAddr->iBus);
304 if (pBridgeDevice)
305 {
306 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigWrite);
307 pBridgeDevice->Int.s.pfnBridgeConfigWrite(pBridgeDevice->pDevIns, pAddr->iBus, pAddr->iDeviceFunc, pAddr->iRegister, val, len);
308 }
309#else
310 return VINF_IOM_HC_IOPORT_WRITE;
311#endif
312 }
313 }
314 else
315 {
316 if (pGlobals->aPciBus.apDevices[pAddr->iDeviceFunc])
317 {
318#ifdef IN_RING3
319 R3PTRTYPE(PCIDevice *) aDev = pGlobals->aPciBus.apDevices[pAddr->iDeviceFunc];
320 Log(("ich9pciConfigWrite: %s: addr=%02x val=%08x len=%d\n", aDev->name, pAddr->iRegister, val, len));
321 aDev->Int.s.pfnConfigWrite(aDev, pAddr->iRegister, val, len);
322#else
323 return VINF_IOM_HC_IOPORT_WRITE;
324#endif
325 }
326 }
327 return VINF_SUCCESS;
328}
329
330static int ich9pciDataWrite(PPCIGLOBALS pGlobals, uint32_t addr, uint32_t val, int len)
331{
332 PciAddress aPciAddr;
333
334 Log(("ich9pciDataWrite: addr=%08x val=%08x len=%d\n", pGlobals->uConfigReg, val, len));
335
336 if (!(pGlobals->uConfigReg & (1 << 31)))
337 return VINF_SUCCESS;
338
339 if ((pGlobals->uConfigReg & 0x3) != 0)
340 return VINF_SUCCESS;
341
342 /* Compute destination device */
343 ich9pciStateToPciAddr(pGlobals, addr, &aPciAddr);
344
345 return ich9pciDataWriteAddr(pGlobals, &aPciAddr, val, len);
346}
347
348/**
349 * Port I/O Handler for PCI data OUT operations.
350 *
351 * @returns VBox status code.
352 *
353 * @param pDevIns The device instance.
354 * @param pvUser User argument - ignored.
355 * @param uPort Port number used for the OUT operation.
356 * @param u32 The value to output.
357 * @param cb The value size in bytes.
358 */
359PDMBOTHCBDECL(int) ich9pciIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
360{
361 Log(("pciIOPortDataWrite: Port=%#x u32=%#x cb=%d\n", Port, u32, cb));
362 NOREF(pvUser);
363 int rc = VINF_SUCCESS;
364 if (!(Port % cb))
365 {
366 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_WRITE);
367 rc = ich9pciDataWrite(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), Port, u32, cb);
368 PCI_UNLOCK(pDevIns);
369 }
370 else
371 AssertMsgFailed(("Unaligned write to port %#x u32=%#x cb=%d\n", Port, u32, cb));
372 return rc;
373}
374
375static int ich9pciDataReadAddr(PPCIGLOBALS pGlobals, PciAddress* pPciAddr, int len, uint32_t *pu32)
376{
377 if (pPciAddr->iRegister > 0xff)
378 {
379 LogRel(("PCI: attempt to read extended register: %x\n", pPciAddr->iRegister));
380 *pu32 = 0;
381 return 0;
382 }
383
384
385 if (pPciAddr->iBus != 0)
386 {
387 if (pGlobals->aPciBus.cBridges)
388 {
389#ifdef IN_RING3 /** @todo do lookup in R0/RC too! */
390 PPCIDEVICE pBridgeDevice = ich9pciFindBridge(&pGlobals->aPciBus, pPciAddr->iBus);
391 if (pBridgeDevice)
392 {
393 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigRead);
394 *pu32 = pBridgeDevice->Int.s.pfnBridgeConfigRead(pBridgeDevice->pDevIns, pPciAddr->iBus, pPciAddr->iDeviceFunc, pPciAddr->iRegister, len);
395 }
396#else
397 return VINF_IOM_HC_IOPORT_READ;
398#endif
399 }
400 }
401 else
402 {
403 if (pGlobals->aPciBus.apDevices[pPciAddr->iDeviceFunc])
404 {
405#ifdef IN_RING3
406 R3PTRTYPE(PCIDevice *) aDev = pGlobals->aPciBus.apDevices[pPciAddr->iDeviceFunc];
407 *pu32 = aDev->Int.s.pfnConfigRead(aDev, pPciAddr->iRegister, len);
408 Log(("ich9pciConfigRead: %s: addr=%02x val=%08x len=%d\n", aDev->name, pPciAddr->iRegister, *pu32, len));
409#else
410 return VINF_IOM_HC_IOPORT_READ;
411#endif
412 }
413 }
414
415 return VINF_SUCCESS;
416}
417
418
419static int ich9pciDataRead(PPCIGLOBALS pGlobals, uint32_t addr, int len, uint32_t *pu32)
420{
421 PciAddress aPciAddr;
422
423 *pu32 = 0xffffffff;
424
425 if (!(pGlobals->uConfigReg & (1 << 31)))
426 return VINF_SUCCESS;
427
428 if ((pGlobals->uConfigReg & 0x3) != 0)
429 return VINF_SUCCESS;
430
431 /* Compute destination device */
432 ich9pciStateToPciAddr(pGlobals, addr, &aPciAddr);
433
434 return ich9pciDataReadAddr(pGlobals, &aPciAddr, len, pu32);
435}
436
437/**
438 * Port I/O Handler for PCI data IN operations.
439 *
440 * @returns VBox status code.
441 *
442 * @param pDevIns The device instance.
443 * @param pvUser User argument - ignored.
444 * @param uPort Port number used for the IN operation.
445 * @param pu32 Where to store the result.
446 * @param cb Number of bytes read.
447 */
448PDMBOTHCBDECL(int) ich9pciIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
449{
450 NOREF(pvUser);
451 if (!(Port % cb))
452 {
453 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_READ);
454 int rc = ich9pciDataRead(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), Port, cb, pu32);
455 PCI_UNLOCK(pDevIns);
456 Log(("pciIOPortDataRead: Port=%#x cb=%#x -> %#x (%Rrc)\n", Port, cb, *pu32, rc));
457 return rc;
458 }
459 AssertMsgFailed(("Unaligned read from port %#x cb=%d\n", Port, cb));
460 return VERR_IOM_IOPORT_UNUSED;
461}
462
463/* Compute mapping of PCI slot and IRQ number to APIC interrupt line */
464static inline int ich9pciSlot2ApicIrq(uint8_t uSlot, int irq_num)
465{
466 return (irq_num + uSlot) & 7;
467}
468
469/* Add one more level up request on APIC input line */
470static inline void ich9pciApicLevelUp(PPCIGLOBALS pGlobals, int irq_num)
471{
472 ASMAtomicIncU32(&pGlobals->uaPciApicIrqLevels[irq_num]);
473}
474
475/* Remove one level up request on APIC input line */
476static inline void ich9pciApicLevelDown(PPCIGLOBALS pGlobals, int irq_num)
477{
478 ASMAtomicDecU32(&pGlobals->uaPciApicIrqLevels[irq_num]);
479}
480
481static void ich9pciApicSetIrq(PPCIBUS pBus, uint8_t uDevFn, PCIDevice *pPciDev, int irq_num1, int iLevel, int iForcedIrq)
482{
483 /* This is only allowed to be called with a pointer to the root bus. */
484 AssertMsg(pBus->iBus == 0, ("iBus=%u\n", pBus->iBus));
485
486 if (iForcedIrq == -1)
487 {
488 int apic_irq, apic_level;
489 PPCIGLOBALS pGlobals = PCIROOTBUS_2_PCIGLOBALS(pBus);
490 int irq_num = ich9pciSlot2ApicIrq(uDevFn >> 3, irq_num1);
491
492 if ((iLevel & PDM_IRQ_LEVEL_HIGH) == PDM_IRQ_LEVEL_HIGH)
493 ich9pciApicLevelUp(pGlobals, irq_num);
494 else if ((iLevel & PDM_IRQ_LEVEL_HIGH) == PDM_IRQ_LEVEL_LOW)
495 ich9pciApicLevelDown(pGlobals, irq_num);
496
497 apic_irq = irq_num + 0x10;
498 apic_level = pGlobals->uaPciApicIrqLevels[irq_num] != 0;
499 Log3(("ich9pciApicSetIrq: %s: irq_num1=%d level=%d apic_irq=%d apic_level=%d irq_num1=%d\n",
500 R3STRING(pPciDev->name), irq_num1, iLevel, apic_irq, apic_level, irq_num));
501 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), apic_irq, apic_level);
502
503 if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP)
504 {
505 /**
506 * we raised it few lines above, as PDM_IRQ_LEVEL_FLIP_FLOP has
507 * PDM_IRQ_LEVEL_HIGH bit set
508 */
509 ich9pciApicLevelDown(pGlobals, irq_num);
510 pPciDev->Int.s.uIrqPinState = PDM_IRQ_LEVEL_LOW;
511 apic_level = pGlobals->uaPciApicIrqLevels[irq_num] != 0;
512 Log3(("ich9pciApicSetIrq: %s: irq_num1=%d level=%d apic_irq=%d apic_level=%d irq_num1=%d (flop)\n",
513 R3STRING(pPciDev->name), irq_num1, iLevel, apic_irq, apic_level, irq_num));
514 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), apic_irq, apic_level);
515 }
516 } else {
517 Log3(("ich9pciApicSetIrq: %s: irq_num1=%d level=%d acpi_irq=%d\n",
518 R3STRING(pPciDev->name), irq_num1, iLevel, iForcedIrq));
519 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), iForcedIrq, iLevel);
520 }
521}
522
523static void ich9pciSetIrqInternal(PPCIGLOBALS pGlobals, uint8_t uDevFn, PPCIDEVICE pPciDev, int iIrq, int iLevel)
524{
525 PPCIBUS pBus = &pGlobals->aPciBus;
526 const bool fIsAcpiDevice = PCIDevGetDeviceId(pPciDev) == 0x7113;
527
528 /* Check if the state changed. */
529 if (pPciDev->Int.s.uIrqPinState != iLevel)
530 {
531 pPciDev->Int.s.uIrqPinState = (iLevel & PDM_IRQ_LEVEL_HIGH);
532
533 /* Send interrupt to I/O APIC only now. */
534 if (fIsAcpiDevice)
535 /*
536 * ACPI needs special treatment since SCI is hardwired and
537 * should not be affected by PCI IRQ routing tables at the
538 * same time SCI IRQ is shared in PCI sense hence this
539 * kludge (i.e. we fetch the hardwired value from ACPIs
540 * PCI device configuration space).
541 */
542 ich9pciApicSetIrq(pBus, uDevFn, pPciDev, -1, iLevel, PCIDevGetInterruptLine(pPciDev));
543 else
544 ich9pciApicSetIrq(pBus, uDevFn, pPciDev, iIrq, iLevel, -1);
545 }
546}
547
548PDMBOTHCBDECL(int) ich9pciMcfgMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
549{
550 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
551 PciAddress aDest;
552 uint32_t u32 = 0;
553
554 ich9pciPhysToPciAddr(pGlobals, GCPhysAddr, &aDest);
555
556 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_WRITE);
557 switch (cb)
558 {
559 case 1:
560 u32 = *(uint8_t*)pv;
561 break;
562 case 2:
563 u32 = *(uint16_t*)pv;
564 break;
565 case 4:
566 u32 = *(uint32_t*)pv;
567 break;
568 default:
569 Assert(false);
570 break;
571 }
572 int rc = ich9pciDataWriteAddr(pGlobals, &aDest, u32, cb);
573 PCI_UNLOCK(pDevIns);
574
575 return rc;
576}
577
578PDMBOTHCBDECL(int) ich9pciMcfgMMIORead (PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
579{
580 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
581 PciAddress aDest;
582 uint32_t rv = 0;
583
584 ich9pciPhysToPciAddr(pGlobals, GCPhysAddr, &aDest);
585
586 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_WRITE);
587 int rc = ich9pciDataReadAddr(pGlobals, &aDest, cb, &rv);
588 if (rc == VINF_SUCCESS)
589 {
590 switch (cb)
591 {
592 case 1:
593 *(uint8_t*)pv = (uint8_t)rv;
594 break;
595 case 2:
596 *(uint16_t*)pv = (uint16_t)rv;
597 break;
598 case 4:
599 *(uint32_t*)pv = (uint32_t)rv;
600 break;
601 default:
602 Assert(false);
603 break;
604 }
605 }
606 PCI_UNLOCK(pDevIns);
607
608 return rc;
609}
610
611#ifdef IN_RING3
612
613DECLINLINE(PPCIDEVICE) ich9pciFindBridge(PPCIBUS pBus, uint8_t iBus)
614{
615 /* Search for a fitting bridge. */
616 for (uint32_t iBridge = 0; iBridge < pBus->cBridges; iBridge++)
617 {
618 /*
619 * Examine secondary and subordinate bus number.
620 * If the target bus is in the range we pass the request on to the bridge.
621 */
622 PPCIDEVICE pBridgeTemp = pBus->papBridgesR3[iBridge];
623 AssertMsg(pBridgeTemp && PCIIsPci2PciBridge(pBridgeTemp),
624 ("Device is not a PCI bridge but on the list of PCI bridges\n"));
625
626 if ( iBus >= pBridgeTemp->config[VBOX_PCI_SECONDARY_BUS]
627 && iBus <= pBridgeTemp->config[VBOX_PCI_SUBORDINATE_BUS])
628 return pBridgeTemp;
629 }
630
631 /* Nothing found. */
632 return NULL;
633}
634
635static inline uint32_t ich9pciGetRegionReg(int iRegion)
636{
637 return (iRegion == PCI_ROM_SLOT) ?
638 VBOX_PCI_ROM_ADDRESS : (VBOX_PCI_BASE_ADDRESS_0 + iRegion * 4);
639}
640
641#define INVALID_PCI_ADDRESS ~0U
642
643static void ich9pciUpdateMappings(PCIDevice* pDev)
644{
645 PPCIBUS pBus = pDev->Int.s.CTX_SUFF(pBus);
646 uint32_t uLast, uNew;
647
648 int iCmd = PCIDevGetCommand(pDev);
649 for (int iRegion = 0; iRegion < PCI_NUM_REGIONS; iRegion++)
650 {
651 PCIIORegion* pRegion = &pDev->Int.s.aIORegions[iRegion];
652 uint32_t uConfigReg = ich9pciGetRegionReg(iRegion);
653 int32_t iRegionSize = pRegion->size;
654 int rc;
655
656 if (iRegionSize == 0)
657 continue;
658
659 if (pRegion->type & PCI_ADDRESS_SPACE_IO)
660 {
661 /* port IO region */
662 if (iCmd & PCI_COMMAND_IOACCESS)
663 {
664 /* IO access allowed */
665 uNew = ich9pciConfigRead(pDev, uConfigReg, 4);
666 uNew &= ~(iRegionSize - 1);
667 uLast = uNew + iRegionSize - 1;
668 /* only 64K ioports on PC */
669 if (uLast <= uNew || uNew == 0 || uLast >= 0x10000)
670 uNew = INVALID_PCI_ADDRESS;
671 } else
672 uNew = INVALID_PCI_ADDRESS;
673 }
674 else
675 {
676 /* MMIO region */
677 if (iCmd & PCI_COMMAND_MEMACCESS)
678 {
679 uNew = ich9pciConfigRead(pDev, uConfigReg, 4);
680 /* the ROM slot has a specific enable bit */
681 if (iRegion == PCI_ROM_SLOT && !(uNew & 1))
682 uNew = INVALID_PCI_ADDRESS;
683 else
684 {
685 uNew &= ~(iRegionSize - 1);
686 uLast = uNew + iRegionSize - 1;
687 /* NOTE: we do not support wrapping */
688 /* XXX: as we cannot support really dynamic
689 mappings, we handle specific values as invalid
690 mappings. */
691 if (uLast <= uNew || uNew == 0 || uLast == INVALID_PCI_ADDRESS)
692 uNew = INVALID_PCI_ADDRESS;
693 }
694 } else
695 uNew = INVALID_PCI_ADDRESS;
696 }
697 /* now do the real mapping */
698 if (uNew != pRegion->addr)
699 {
700 if (pRegion->addr != INVALID_PCI_ADDRESS)
701 {
702 if (pRegion->type & PCI_ADDRESS_SPACE_IO)
703 {
704 /* Port IO */
705 int devclass;
706 /* NOTE: specific hack for IDE in PC case:
707 only one byte must be mapped. */
708 /// @todo: do we need it?
709 devclass = pDev->config[0x0a] | (pDev->config[0x0b] << 8);
710 if (devclass == 0x0101 && iRegionSize == 4)
711 {
712 rc = PDMDevHlpIOPortDeregister(pDev->pDevIns, pRegion->addr + 2, 1);
713 AssertRC(rc);
714 }
715 else
716 {
717 rc = PDMDevHlpIOPortDeregister(pDev->pDevIns, pRegion->addr, pRegion->size);
718 AssertRC(rc);
719 }
720 }
721 else
722 {
723 RTGCPHYS GCPhysBase = pRegion->addr;
724 if (pBus->pPciHlpR3->pfnIsMMIO2Base(pBus->pDevInsR3, pDev->pDevIns, GCPhysBase))
725 {
726 /* unmap it. */
727 rc = pRegion->map_func(pDev, iRegion, NIL_RTGCPHYS, pRegion->size, (PCIADDRESSSPACE)(pRegion->type));
728 AssertRC(rc);
729 rc = PDMDevHlpMMIO2Unmap(pDev->pDevIns, iRegion, GCPhysBase);
730 }
731 else
732 rc = PDMDevHlpMMIODeregister(pDev->pDevIns, GCPhysBase, pRegion->size);
733 AssertMsgRC(rc, ("rc=%Rrc d=%s i=%d GCPhysBase=%RGp size=%#x\n", rc, pDev->name, iRegion, GCPhysBase, pRegion->size));
734 }
735 }
736 pRegion->addr = uNew;
737 if (pRegion->addr != INVALID_PCI_ADDRESS)
738 {
739 /* finally, map the region */
740 rc = pRegion->map_func(pDev, iRegion,
741 pRegion->addr, pRegion->size,
742 (PCIADDRESSSPACE)(pRegion->type));
743 AssertRC(rc);
744 }
745 }
746 }
747}
748
749static DECLCALLBACK(int) ich9pciRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, const char *pszName, int iDev)
750{
751 PPCIBUS pBus = DEVINS_2_PCIBUS(pDevIns);
752
753 /*
754 * Check input.
755 */
756 if ( !pszName
757 || !pPciDev
758 || iDev >= (int)RT_ELEMENTS(pBus->apDevices)
759 )
760 {
761 AssertMsgFailed(("Invalid argument! pszName=%s pPciDev=%p iDev=%d\n", pszName, pPciDev, iDev));
762 return VERR_INVALID_PARAMETER;
763 }
764
765 /*
766 * Register the device.
767 */
768 return ich9pciRegisterInternal(pBus, iDev, pPciDev, pszName);
769}
770
771static DECLCALLBACK(int) ich9pcibridgeRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, const char *pszName, int iDev)
772{
773
774 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
775
776 /*
777 * Check input.
778 */
779 if ( !pszName
780 || !pPciDev
781 || iDev >= (int)RT_ELEMENTS(pBus->apDevices))
782 {
783 AssertMsgFailed(("Invalid argument! pszName=%s pPciDev=%p iDev=%d\n", pszName, pPciDev, iDev));
784 return VERR_INVALID_PARAMETER;
785 }
786
787 /*
788 * Register the device.
789 */
790 return ich9pciRegisterInternal(pBus, iDev, pPciDev, pszName);
791}
792
793static DECLCALLBACK(int) ich9pciIORegionRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iRegion, uint32_t cbRegion, PCIADDRESSSPACE enmType, PFNPCIIOREGIONMAP pfnCallback)
794{
795 /*
796 * Validate.
797 */
798 AssertMsgReturn( enmType == PCI_ADDRESS_SPACE_MEM
799 || enmType == PCI_ADDRESS_SPACE_IO
800 || enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH,
801 ("Invalid enmType=%#x? Or was this a bitmask after all...\n", enmType),
802 VERR_INVALID_PARAMETER);
803 AssertMsgReturn((unsigned)iRegion < PCI_NUM_REGIONS,
804 ("Invalid iRegion=%d PCI_NUM_REGIONS=%d\n", iRegion, PCI_NUM_REGIONS),
805 VERR_INVALID_PARAMETER);
806 int iLastSet = ASMBitLastSetU32(cbRegion);
807 AssertMsgReturn( iLastSet != 0
808 && RT_BIT_32(iLastSet - 1) == cbRegion,
809 ("Invalid cbRegion=%#x iLastSet=%#x (not a power of 2 or 0)\n", cbRegion, iLastSet),
810 VERR_INVALID_PARAMETER);
811
812 /*
813 * Register the I/O region.
814 */
815 PPCIIOREGION pRegion = &pPciDev->Int.s.aIORegions[iRegion];
816 pRegion->addr = INVALID_PCI_ADDRESS;
817 pRegion->size = cbRegion;
818 pRegion->type = enmType;
819 pRegion->map_func = pfnCallback;
820
821 /* Set type in the config space. */
822 uint32_t u32Address = ich9pciGetRegionReg(iRegion);
823 uint32_t u32Value = (enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH ? (1 << 3) : 0)
824 | (enmType == PCI_ADDRESS_SPACE_IO ? 1 : 0);
825 *(uint32_t *)(pPciDev->config + u32Address) = RT_H2LE_U32(u32Value);
826
827 return VINF_SUCCESS;
828}
829
830static DECLCALLBACK(void) ich9pciSetConfigCallbacks(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PFNPCICONFIGREAD pfnRead, PPFNPCICONFIGREAD ppfnReadOld,
831 PFNPCICONFIGWRITE pfnWrite, PPFNPCICONFIGWRITE ppfnWriteOld)
832{
833 if (ppfnReadOld)
834 *ppfnReadOld = pPciDev->Int.s.pfnConfigRead;
835 pPciDev->Int.s.pfnConfigRead = pfnRead;
836
837 if (ppfnWriteOld)
838 *ppfnWriteOld = pPciDev->Int.s.pfnConfigWrite;
839 pPciDev->Int.s.pfnConfigWrite = pfnWrite;
840}
841
842/**
843 * Saves a state of the PCI device.
844 *
845 * @returns VBox status code.
846 * @param pDevIns Device instance of the PCI Bus.
847 * @param pPciDev Pointer to PCI device.
848 * @param pSSM The handle to save the state to.
849 */
850static DECLCALLBACK(int) ich9pciGenericSaveExec(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PSSMHANDLE pSSM)
851{
852 return SSMR3PutMem(pSSM, &pPciDev->config[0], sizeof(pPciDev->config));
853}
854
855static int ich9pciR3CommonSaveExec(PPCIBUS pBus, PSSMHANDLE pSSM)
856{
857 /*
858 * Iterate thru all the devices.
859 */
860 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
861 {
862 PPCIDEVICE pDev = pBus->apDevices[i];
863 if (pDev)
864 {
865 /* Device position */
866 SSMR3PutU32(pSSM, i);
867 /* PCI config registers */
868 SSMR3PutMem(pSSM, pDev->config, sizeof(pDev->config));
869
870 /* IRQ pin state */
871 int rc = SSMR3PutS32(pSSM, pDev->Int.s.uIrqPinState);
872 if (RT_FAILURE(rc))
873 return rc;
874 }
875 }
876 return SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
877}
878
879static DECLCALLBACK(int) ich9pciR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
880{
881 PPCIBUS pThis = PDMINS_2_DATA(pDevIns, PPCIBUS);
882 return ich9pciR3CommonSaveExec(pThis, pSSM);
883}
884
885
886static DECLCALLBACK(int) ich9pcibridgeR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
887{
888 PPCIBUS pThis = PDMINS_2_DATA(pDevIns, PPCIBUS);
889 return ich9pciR3CommonSaveExec(pThis, pSSM);
890}
891
892
893static void ich9pcibridgeConfigWrite(PPDMDEVINSR3 pDevIns, uint8_t iBus, uint8_t iDevice, uint32_t u32Address, uint32_t u32Value, unsigned cb)
894{
895 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
896
897 LogFlowFunc((": pDevIns=%p iBus=%d iDevice=%d u32Address=%u u32Value=%u cb=%d\n", pDevIns, iBus, iDevice, u32Address, u32Value, cb));
898
899 /* If the current bus is not the target bus search for the bus which contains the device. */
900 if (iBus != PCIDevGetByte(&pBus->aPciDev, VBOX_PCI_SECONDARY_BUS))
901 {
902 PPCIDEVICE pBridgeDevice = ich9pciFindBridge(pBus, iBus);
903 if (pBridgeDevice)
904 {
905 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigWrite);
906 pBridgeDevice->Int.s.pfnBridgeConfigWrite(pBridgeDevice->pDevIns, iBus, iDevice, u32Address, u32Value, cb);
907 }
908 }
909 else
910 {
911 /* This is the target bus, pass the write to the device. */
912 PPCIDEVICE pPciDev = pBus->apDevices[iDevice];
913 if (pPciDev)
914 {
915 Log(("%s: %s: addr=%02x val=%08x len=%d\n", __FUNCTION__, pPciDev->name, u32Address, u32Value, cb));
916 pPciDev->Int.s.pfnConfigWrite(pPciDev, u32Address, u32Value, cb);
917 }
918 }
919}
920
921static uint32_t ich9pcibridgeConfigRead(PPDMDEVINSR3 pDevIns, uint8_t iBus, uint8_t iDevice, uint32_t u32Address, unsigned cb)
922{
923 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
924 uint32_t u32Value = 0xffffffff; /* Return value in case there is no device. */
925
926 LogFlowFunc((": pDevIns=%p iBus=%d iDevice=%d u32Address=%u cb=%d\n", pDevIns, iBus, iDevice, u32Address, cb));
927
928 /* If the current bus is not the target bus search for the bus which contains the device. */
929 if (iBus != PCIDevGetByte(&pBus->aPciDev, VBOX_PCI_SECONDARY_BUS))
930 {
931 PPCIDEVICE pBridgeDevice = ich9pciFindBridge(pBus, iBus);
932 if (pBridgeDevice)
933 {
934 AssertPtr( pBridgeDevice->Int.s.pfnBridgeConfigRead);
935 u32Value = pBridgeDevice->Int.s.pfnBridgeConfigRead(pBridgeDevice->pDevIns, iBus, iDevice, u32Address, cb);
936 }
937 }
938 else
939 {
940 /* This is the target bus, pass the read to the device. */
941 PPCIDEVICE pPciDev = pBus->apDevices[iDevice];
942 if (pPciDev)
943 {
944 u32Value = pPciDev->Int.s.pfnConfigRead(pPciDev, u32Address, cb);
945 Log(("%s: %s: u32Address=%02x u32Value=%08x cb=%d\n", __FUNCTION__, pPciDev->name, u32Address, u32Value, cb));
946 }
947 }
948
949 return u32Value;
950}
951
952
953/**
954 * Common routine for restoring the config registers of a PCI device.
955 *
956 * @param pDev The PCI device.
957 * @param pbSrcConfig The configuration register values to be loaded.
958 * @param fIsBridge Whether this is a bridge device or not.
959 */
960static void pciR3CommonRestoreConfig(PPCIDEVICE pDev, uint8_t const *pbSrcConfig, bool fIsBridge)
961{
962 /*
963 * This table defines the fields for normal devices and bridge devices, and
964 * the order in which they need to be restored.
965 */
966 static const struct PciField
967 {
968 uint8_t off;
969 uint8_t cb;
970 uint8_t fWritable;
971 uint8_t fBridge;
972 const char *pszName;
973 } s_aFields[] =
974 {
975 /* off,cb,fW,fB, pszName */
976 { VBOX_PCI_VENDOR_ID, 2, 0, 3, "VENDOR_ID" },
977 { VBOX_PCI_DEVICE_ID, 2, 0, 3, "DEVICE_ID" },
978 { VBOX_PCI_STATUS, 2, 1, 3, "STATUS" },
979 { VBOX_PCI_REVISION_ID, 1, 0, 3, "REVISION_ID" },
980 { VBOX_PCI_CLASS_PROG, 1, 0, 3, "CLASS_PROG" },
981 { VBOX_PCI_CLASS_SUB, 1, 0, 3, "CLASS_SUB" },
982 { VBOX_PCI_CLASS_DEVICE, 1, 0, 3, "CLASS_BASE" },
983 { VBOX_PCI_CACHE_LINE_SIZE, 1, 1, 3, "CACHE_LINE_SIZE" },
984 { VBOX_PCI_LATENCY_TIMER, 1, 1, 3, "LATENCY_TIMER" },
985 { VBOX_PCI_HEADER_TYPE, 1, 0, 3, "HEADER_TYPE" },
986 { VBOX_PCI_BIST, 1, 1, 3, "BIST" },
987 { VBOX_PCI_BASE_ADDRESS_0, 4, 1, 3, "BASE_ADDRESS_0" },
988 { VBOX_PCI_BASE_ADDRESS_1, 4, 1, 3, "BASE_ADDRESS_1" },
989 { VBOX_PCI_BASE_ADDRESS_2, 4, 1, 1, "BASE_ADDRESS_2" },
990 { VBOX_PCI_PRIMARY_BUS, 1, 1, 2, "PRIMARY_BUS" }, // fWritable = ??
991 { VBOX_PCI_SECONDARY_BUS, 1, 1, 2, "SECONDARY_BUS" }, // fWritable = ??
992 { VBOX_PCI_SUBORDINATE_BUS, 1, 1, 2, "SUBORDINATE_BUS" }, // fWritable = ??
993 { VBOX_PCI_SEC_LATENCY_TIMER, 1, 1, 2, "SEC_LATENCY_TIMER" }, // fWritable = ??
994 { VBOX_PCI_BASE_ADDRESS_3, 4, 1, 1, "BASE_ADDRESS_3" },
995 { VBOX_PCI_IO_BASE, 1, 1, 2, "IO_BASE" }, // fWritable = ??
996 { VBOX_PCI_IO_LIMIT, 1, 1, 2, "IO_LIMIT" }, // fWritable = ??
997 { VBOX_PCI_SEC_STATUS, 2, 1, 2, "SEC_STATUS" }, // fWritable = ??
998 { VBOX_PCI_BASE_ADDRESS_4, 4, 1, 1, "BASE_ADDRESS_4" },
999 { VBOX_PCI_MEMORY_BASE, 2, 1, 2, "MEMORY_BASE" }, // fWritable = ??
1000 { VBOX_PCI_MEMORY_LIMIT, 2, 1, 2, "MEMORY_LIMIT" }, // fWritable = ??
1001 { VBOX_PCI_BASE_ADDRESS_4, 4, 1, 1, "BASE_ADDRESS_4" },
1002 { VBOX_PCI_PREF_MEMORY_BASE, 2, 1, 2, "PREF_MEMORY_BASE" }, // fWritable = ??
1003 { VBOX_PCI_PREF_MEMORY_LIMIT, 2, 1, 2, "PREF_MEMORY_LIMIT" }, // fWritable = ??
1004 { VBOX_PCI_CARDBUS_CIS, 4, 1, 1, "CARDBUS_CIS" }, // fWritable = ??
1005 { VBOX_PCI_PREF_BASE_UPPER32, 4, 1, 2, "PREF_BASE_UPPER32" }, // fWritable = ??
1006 { VBOX_PCI_SUBSYSTEM_VENDOR_ID, 2, 0, 1, "SUBSYSTEM_VENDOR_ID" },// fWritable = !?
1007 { VBOX_PCI_PREF_LIMIT_UPPER32, 4, 1, 2, "PREF_LIMIT_UPPER32" },// fWritable = ??
1008 { VBOX_PCI_SUBSYSTEM_ID, 2, 0, 1, "SUBSYSTEM_ID" }, // fWritable = !?
1009 { VBOX_PCI_ROM_ADDRESS, 4, 1, 1, "ROM_ADDRESS" }, // fWritable = ?!
1010 { VBOX_PCI_IO_BASE_UPPER16, 2, 1, 2, "IO_BASE_UPPER16" }, // fWritable = ?!
1011 { VBOX_PCI_IO_LIMIT_UPPER16, 2, 1, 2, "IO_LIMIT_UPPER16" }, // fWritable = ?!
1012 { VBOX_PCI_CAPABILITY_LIST, 4, 0, 3, "CAPABILITY_LIST" }, // fWritable = !? cb=!?
1013 { VBOX_PCI_RESERVED_38, 4, 1, 1, "RESERVED_38" }, // ???
1014 { VBOX_PCI_ROM_ADDRESS_BR, 4, 1, 2, "ROM_ADDRESS_BR" }, // fWritable = !? cb=!? fBridge=!?
1015 { VBOX_PCI_INTERRUPT_LINE, 1, 1, 3, "INTERRUPT_LINE" }, // fBridge=??
1016 { VBOX_PCI_INTERRUPT_PIN, 1, 0, 3, "INTERRUPT_PIN" }, // fBridge=??
1017 { VBOX_PCI_MIN_GNT, 1, 0, 1, "MIN_GNT" },
1018 { VBOX_PCI_BRIDGE_CONTROL, 2, 1, 2, "BRIDGE_CONTROL" }, // fWritable = !?
1019 { VBOX_PCI_MAX_LAT, 1, 0, 3, "MAX_LAT" }, // fBridge=!?
1020 /* The COMMAND register must come last as it requires the *ADDRESS*
1021 registers to be restored before we pretent to change it from 0 to
1022 whatever value the guest assigned it. */
1023 { VBOX_PCI_COMMAND, 2, 1, 3, "COMMAND" },
1024 };
1025
1026#ifdef RT_STRICT
1027 /* Check that we've got full register coverage. */
1028 uint32_t bmDevice[0x40 / 32];
1029 uint32_t bmBridge[0x40 / 32];
1030 RT_ZERO(bmDevice);
1031 RT_ZERO(bmBridge);
1032 for (uint32_t i = 0; i < RT_ELEMENTS(s_aFields); i++)
1033 {
1034 uint8_t off = s_aFields[i].off;
1035 uint8_t cb = s_aFields[i].cb;
1036 uint8_t f = s_aFields[i].fBridge;
1037 while (cb-- > 0)
1038 {
1039 if (f & 1) AssertMsg(!ASMBitTest(bmDevice, off), ("%#x\n", off));
1040 if (f & 2) AssertMsg(!ASMBitTest(bmBridge, off), ("%#x\n", off));
1041 if (f & 1) ASMBitSet(bmDevice, off);
1042 if (f & 2) ASMBitSet(bmBridge, off);
1043 off++;
1044 }
1045 }
1046 for (uint32_t off = 0; off < 0x40; off++)
1047 {
1048 AssertMsg(ASMBitTest(bmDevice, off), ("%#x\n", off));
1049 AssertMsg(ASMBitTest(bmBridge, off), ("%#x\n", off));
1050 }
1051#endif
1052
1053 /*
1054 * Loop thru the fields covering the 64 bytes of standard registers.
1055 */
1056 uint8_t const fBridge = fIsBridge ? 2 : 1;
1057 uint8_t *pbDstConfig = &pDev->config[0];
1058 for (uint32_t i = 0; i < RT_ELEMENTS(s_aFields); i++)
1059 if (s_aFields[i].fBridge & fBridge)
1060 {
1061 uint8_t const off = s_aFields[i].off;
1062 uint8_t const cb = s_aFields[i].cb;
1063 uint32_t u32Src;
1064 uint32_t u32Dst;
1065 switch (cb)
1066 {
1067 case 1:
1068 u32Src = pbSrcConfig[off];
1069 u32Dst = pbDstConfig[off];
1070 break;
1071 case 2:
1072 u32Src = *(uint16_t const *)&pbSrcConfig[off];
1073 u32Dst = *(uint16_t const *)&pbDstConfig[off];
1074 break;
1075 case 4:
1076 u32Src = *(uint32_t const *)&pbSrcConfig[off];
1077 u32Dst = *(uint32_t const *)&pbDstConfig[off];
1078 break;
1079 default:
1080 AssertFailed();
1081 continue;
1082 }
1083
1084 if ( u32Src != u32Dst
1085 || off == VBOX_PCI_COMMAND)
1086 {
1087 if (u32Src != u32Dst)
1088 {
1089 if (!s_aFields[i].fWritable)
1090 LogRel(("PCI: %8s/%u: %2u-bit field %s: %x -> %x - !READ ONLY!\n",
1091 pDev->name, pDev->pDevIns->iInstance, cb*8, s_aFields[i].pszName, u32Dst, u32Src));
1092 else
1093 LogRel(("PCI: %8s/%u: %2u-bit field %s: %x -> %x\n",
1094 pDev->name, pDev->pDevIns->iInstance, cb*8, s_aFields[i].pszName, u32Dst, u32Src));
1095 }
1096 if (off == VBOX_PCI_COMMAND)
1097 PCIDevSetCommand(pDev, 0); /* For remapping, see ich9pciR3CommonLoadExec. */
1098 pDev->Int.s.pfnConfigWrite(pDev, off, u32Src, cb);
1099 }
1100 }
1101
1102 /*
1103 * The device dependent registers.
1104 *
1105 * We will not use ConfigWrite here as we have no clue about the size
1106 * of the registers, so the device is responsible for correctly
1107 * restoring functionality governed by these registers.
1108 */
1109 for (uint32_t off = 0x40; off < sizeof(pDev->config); off++)
1110 if (pbDstConfig[off] != pbSrcConfig[off])
1111 {
1112 LogRel(("PCI: %8s/%u: register %02x: %02x -> %02x\n",
1113 pDev->name, pDev->pDevIns->iInstance, off, pbDstConfig[off], pbSrcConfig[off])); /** @todo make this Log() later. */
1114 pbDstConfig[off] = pbSrcConfig[off];
1115 }
1116}
1117
1118/**
1119 * Common worker for ich9pciR3LoadExec and ich9pcibridgeR3LoadExec.
1120 *
1121 * @returns VBox status code.
1122 * @param pBus The bus which data is being loaded.
1123 * @param pSSM The saved state handle.
1124 * @param uVersion The data version.
1125 * @param uPass The pass.
1126 */
1127static DECLCALLBACK(int) ich9pciR3CommonLoadExec(PPCIBUS pBus, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1128{
1129 uint32_t u32;
1130 uint32_t i;
1131 int rc;
1132
1133 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1134
1135 /*
1136 * Iterate thru all the devices and write 0 to the COMMAND register so
1137 * that all the memory is unmapped before we start restoring the saved
1138 * mapping locations.
1139 *
1140 * The register value is restored afterwards so we can do proper
1141 * LogRels in pciR3CommonRestoreConfig.
1142 */
1143 for (i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
1144 {
1145 PPCIDEVICE pDev = pBus->apDevices[i];
1146 if (pDev)
1147 {
1148 uint16_t u16 = PCIDevGetCommand(pDev);
1149 pDev->Int.s.pfnConfigWrite(pDev, VBOX_PCI_COMMAND, 0, 2);
1150 PCIDevSetCommand(pDev, u16);
1151 Assert(PCIDevGetCommand(pDev) == u16);
1152 }
1153 }
1154
1155 /*
1156 * Iterate all the devices.
1157 */
1158 for (i = 0;; i++)
1159 {
1160 PCIDEVICE DevTmp;
1161 PPCIDEVICE pDev;
1162
1163 /* index / terminator */
1164 rc = SSMR3GetU32(pSSM, &u32);
1165 if (RT_FAILURE(rc))
1166 return rc;
1167 if (u32 == (uint32_t)~0)
1168 break;
1169 if ( u32 >= RT_ELEMENTS(pBus->apDevices)
1170 || u32 < i)
1171 {
1172 AssertMsgFailed(("u32=%#x i=%#x\n", u32, i));
1173 return rc;
1174 }
1175
1176 /* skip forward to the device checking that no new devices are present. */
1177 for (; i < u32; i++)
1178 {
1179 pDev = pBus->apDevices[i];
1180 if (pDev)
1181 {
1182 LogRel(("New device in slot %#x, %s (vendor=%#06x device=%#06x)\n", i, pDev->name,
1183 PCIDevGetVendorId(pDev), PCIDevGetDeviceId(pDev)));
1184 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1185 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("New device in slot %#x, %s (vendor=%#06x device=%#06x)"),
1186 i, pDev->name, PCIDevGetVendorId(pDev), PCIDevGetDeviceId(pDev));
1187 }
1188 }
1189
1190 /* get the data */
1191 DevTmp.Int.s.uIrqPinState = ~0; /* Invalid value in case we have an older saved state to force a state change in pciSetIrq. */
1192 SSMR3GetMem(pSSM, DevTmp.config, sizeof(DevTmp.config));
1193
1194 rc = SSMR3GetS32(pSSM, &DevTmp.Int.s.uIrqPinState);
1195 if (RT_FAILURE(rc))
1196 return rc;
1197
1198 /* check that it's still around. */
1199 pDev = pBus->apDevices[i];
1200 if (!pDev)
1201 {
1202 LogRel(("Device in slot %#x has been removed! vendor=%#06x device=%#06x\n", i,
1203 PCIDevGetVendorId(&DevTmp), PCIDevGetDeviceId(&DevTmp)));
1204 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1205 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device in slot %#x has been removed! vendor=%#06x device=%#06x"),
1206 i, PCIDevGetVendorId(&DevTmp), PCIDevGetDeviceId(&DevTmp));
1207 continue;
1208 }
1209
1210 /* match the vendor id assuming that this will never be changed. */
1211 if ( DevTmp.config[0] != pDev->config[0]
1212 || DevTmp.config[1] != pDev->config[1])
1213 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device in slot %#x (%s) vendor id mismatch! saved=%.4Rhxs current=%.4Rhxs"),
1214 i, pDev->name, DevTmp.config, pDev->config);
1215
1216 /* commit the loaded device config. */
1217 pciR3CommonRestoreConfig(pDev, &DevTmp.config[0], false ); /** @todo fix bridge fun! */
1218
1219 pDev->Int.s.uIrqPinState = DevTmp.Int.s.uIrqPinState;
1220 }
1221
1222 return VINF_SUCCESS;
1223}
1224
1225/**
1226 * Loads a saved PCI device state.
1227 *
1228 * @returns VBox status code.
1229 * @param pDevIns Device instance of the PCI Bus.
1230 * @param pPciDev Pointer to PCI device.
1231 * @param pSSM The handle to the saved state.
1232 */
1233static DECLCALLBACK(int) ich9pciGenericLoadExec(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PSSMHANDLE pSSM)
1234{
1235 return SSMR3GetMem(pSSM, &pPciDev->config[0], sizeof(pPciDev->config));
1236}
1237
1238static DECLCALLBACK(int) ich9pciR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1239{
1240 PPCIBUS pThis = PDMINS_2_DATA(pDevIns, PPCIBUS);
1241 if (uVersion > VBOX_ICH9PCI_SAVED_STATE_VERSION)
1242 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1243 return ich9pciR3CommonLoadExec(pThis, pSSM, uVersion, uPass);
1244}
1245
1246static DECLCALLBACK(int) ich9pcibridgeR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1247{
1248 PPCIBUS pThis = PDMINS_2_DATA(pDevIns, PPCIBUS);
1249 if (uVersion > VBOX_ICH9PCI_SAVED_STATE_VERSION)
1250 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1251 return ich9pciR3CommonLoadExec(pThis, pSSM, uVersion, uPass);
1252}
1253
1254static uint32_t ich9pciConfigRead(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t len)
1255{
1256 /* Set destination address */
1257 /// @todo: device locking?
1258 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
1259 (uDevFn << 8) | (addr & ~3);
1260 uint32_t u32Val;
1261 int rc = ich9pciDataRead(pGlobals, addr & 3, len, &u32Val);
1262 AssertRC(rc);
1263 switch (len)
1264 {
1265 case 1:
1266 u32Val &= 0xff;
1267 break;
1268 case 2:
1269 u32Val &= 0xffff;
1270 break;
1271 }
1272 return u32Val;
1273}
1274
1275static void ich9pciConfigWrite(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t val, uint32_t len)
1276{
1277 /* Set destination address */
1278 /// @todo: device locking?
1279 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
1280 (uDevFn << 8) | addr;
1281 ich9pciDataWrite(pGlobals, 0, val, len);
1282}
1283
1284static void ich9pciSetRegionAddress(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, int iRegion, uint32_t addr)
1285{
1286 uint32_t uReg = ich9pciGetRegionReg(iRegion);
1287
1288 /* Read memory type first. */
1289 uint8_t uResourceType = ich9pciConfigRead(pGlobals, uBus, uDevFn, uReg, 1);
1290 /* Read command register. */
1291 uint16_t uCmd = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_COMMAND, 2);
1292
1293 if ( iRegion == PCI_ROM_SLOT )
1294 uCmd |= PCI_COMMAND_MEMACCESS;
1295 else if ((uResourceType & PCI_ADDRESS_SPACE_IO) == PCI_ADDRESS_SPACE_IO)
1296 uCmd |= PCI_COMMAND_IOACCESS; /* Enable I/O space access. */
1297 else /* The region is MMIO. */
1298 uCmd |= PCI_COMMAND_MEMACCESS; /* Enable MMIO access. */
1299
1300 /* Write address of the device. */
1301 ich9pciConfigWrite(pGlobals, uBus, uDevFn, uReg, addr, 4);
1302
1303 /* enable memory mappings */
1304 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_COMMAND, uCmd, 2);
1305}
1306
1307
1308static void ich9pciBiosInitBridge(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint8_t cBridgeDepth, uint8_t *paBridgePositions)
1309{
1310 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_SECONDARY_BUS, pGlobals->uBus, 1);
1311 /* Temporary until we know how many other bridges are behind this one. */
1312 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_SUBORDINATE_BUS, 0xff, 1);
1313
1314 /* Add position of this bridge into the array. */
1315 paBridgePositions[cBridgeDepth+1] = (uDevFn >> 3);
1316
1317 /*
1318 * The I/O range for the bridge must be aligned to a 4KB boundary.
1319 * This does not change anything really as the access to the device is not going
1320 * through the bridge but we want to be compliant to the spec.
1321 */
1322 if ((pGlobals->uPciBiosIo % 4096) != 0)
1323 {
1324 pGlobals->uPciBiosIo = RT_ALIGN_32(pGlobals->uPciBiosIo, 4*1024);
1325 Log(("%s: Aligned I/O start address. New address %#x\n", __FUNCTION__, pGlobals->uPciBiosIo));
1326 }
1327 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_IO_BASE, (pGlobals->uPciBiosIo >> 8) & 0xf0, 1);
1328
1329 /* The MMIO range for the bridge must be aligned to a 1MB boundary. */
1330 if ((pGlobals->uPciBiosMmio % (1024 * 1024)) != 0)
1331 {
1332 pGlobals->uPciBiosMmio = RT_ALIGN_32(pGlobals->uPciBiosMmio, 1024*1024);
1333 Log(("%s: Aligned MMIO start address. New address %#x\n", __FUNCTION__, pGlobals->uPciBiosMmio));
1334 }
1335 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_MEMORY_BASE, (pGlobals->uPciBiosMmio >> 16) & UINT32_C(0xffff0), 2);
1336
1337 /* Save values to compare later to. */
1338 uint32_t u32IoAddressBase = pGlobals->uPciBiosIo;
1339 uint32_t u32MMIOAddressBase = pGlobals->uPciBiosMmio;
1340
1341 /* Init devices behind the bridge and possibly other bridges as well. */
1342 for (int iDev = 0; iDev <= 255; iDev++)
1343 ich9pciBiosInitDevice(pGlobals, uBus + 1, iDev, cBridgeDepth + 1, paBridgePositions);
1344
1345 /* The number of bridges behind the this one is now available. */
1346 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_SUBORDINATE_BUS, pGlobals->uBus, 1);
1347
1348 /*
1349 * Set I/O limit register. If there is no device with I/O space behind the bridge
1350 * we set a lower value than in the base register.
1351 * The result with a real bridge is that no I/O transactions are passed to the secondary
1352 * interface. Again this doesn't really matter here but we want to be compliant to the spec.
1353 */
1354 if ((u32IoAddressBase != pGlobals->uPciBiosIo) && ((pGlobals->uPciBiosIo % 4096) != 0))
1355 {
1356 /* The upper boundary must be one byte less than a 4KB boundary. */
1357 pGlobals->uPciBiosIo = RT_ALIGN_32(pGlobals->uPciBiosIo, 4*1024);
1358 }
1359
1360 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_IO_LIMIT, ((pGlobals->uPciBiosIo >> 8) & 0xf0) - 1, 1);
1361
1362 /* Same with the MMIO limit register but with 1MB boundary here. */
1363 if ((u32MMIOAddressBase != pGlobals->uPciBiosMmio) && ((pGlobals->uPciBiosMmio % (1024 * 1024)) != 0))
1364 {
1365 /* The upper boundary must be one byte less than a 1MB boundary. */
1366 pGlobals->uPciBiosMmio = RT_ALIGN_32(pGlobals->uPciBiosMmio, 1024*1024);
1367 }
1368 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_MEMORY_LIMIT, ((pGlobals->uPciBiosMmio >> 16) & UINT32_C(0xfff0)) - 1, 2);
1369
1370 /*
1371 * Set the prefetch base and limit registers. We currently have no device with a prefetchable region
1372 * which may be behind a bridge. Thatswhy it is unconditionally disabled here atm by writing a higher value into
1373 * the base register than in the limit register.
1374 */
1375 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_MEMORY_BASE, 0xfff0, 2);
1376 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_MEMORY_LIMIT, 0x0, 2);
1377 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_BASE_UPPER32, 0x00, 4);
1378 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_LIMIT_UPPER32, 0x00, 4);
1379}
1380
1381static void ich9pciBiosInitDevice(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint8_t cBridgeDepth, uint8_t *paBridgePositions)
1382{
1383 uint32_t *paddr;
1384 uint16_t uDevClass, uVendor, uDevice;
1385 uint8_t uCmd;
1386
1387 uDevClass = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_CLASS_DEVICE, 2);
1388 uVendor = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_VENDOR_ID, 2);
1389 uDevice = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_DEVICE_ID, 2);
1390
1391 /* If device is present */
1392 if (uVendor == 0xffff)
1393 return;
1394
1395 switch (uDevClass)
1396 {
1397 case 0x0101:
1398 /* IDE controller */
1399 ich9pciConfigWrite(pGlobals, uBus, uDevFn, 0x40, 0x8000, 2); /* enable IDE0 */
1400 ich9pciConfigWrite(pGlobals, uBus, uDevFn, 0x42, 0x8000, 2); /* enable IDE1 */
1401 goto default_map;
1402 break;
1403 case 0x0300:
1404 /* VGA controller */
1405 if (uVendor != 0x80ee)
1406 goto default_map;
1407 /* VGA: map frame buffer to default Bochs VBE address */
1408 ich9pciSetRegionAddress(pGlobals, uBus, uDevFn, 0, 0xE0000000);
1409 /*
1410 * Legacy VGA I/O ports are implicitly decoded by a VGA class device. But
1411 * only the framebuffer (i.e., a memory region) is explicitly registered via
1412 * ich9pciSetRegionAddress, so I/O decoding must be enabled manually.
1413 */
1414 uCmd = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_COMMAND, 1);
1415 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_COMMAND,
1416 /* Enable I/O space access. */
1417 uCmd | PCI_COMMAND_IOACCESS,
1418 1);
1419 break;
1420 case 0x0800:
1421 /* PIC */
1422 if (uVendor == 0x1014)
1423 {
1424 /* IBM */
1425 if (uDevice == 0x0046 || uDevice == 0xFFFF)
1426 /* MPIC & MPIC2 */
1427 ich9pciSetRegionAddress(pGlobals, uBus, uDevFn, 0, 0x80800000 + 0x00040000);
1428 }
1429 break;
1430 case 0xff00:
1431 if ((uVendor == 0x0106b)
1432 && (uDevice == 0x0017 || uDevice == 0x0022))
1433 {
1434 /* macio bridge */
1435 ich9pciSetRegionAddress(pGlobals, uBus, uDevFn, 0, 0x80800000);
1436 }
1437 break;
1438 case 0x0604:
1439 /* PCI-to-PCI bridge. */
1440 ich9pciConfigWrite(pGlobals, uBus, uDevFn, VBOX_PCI_PRIMARY_BUS, uBus, 1);
1441
1442 AssertMsg(pGlobals->uBus < 255, ("Too many bridges on the bus\n"));
1443 pGlobals->uBus++;
1444 ich9pciBiosInitBridge(pGlobals, uBus, uDevFn, cBridgeDepth, paBridgePositions);
1445 break;
1446 default:
1447 default_map:
1448 {
1449 /* default memory mappings */
1450 /*
1451 * We ignore ROM region here.
1452 */
1453 for (int iRegion = 0; iRegion < (PCI_NUM_REGIONS-1); iRegion++)
1454 {
1455 uint32_t u32Address = ich9pciGetRegionReg(iRegion);
1456
1457 /* Calculate size. */
1458 uint8_t u8ResourceType = ich9pciConfigRead(pGlobals, uBus, uDevFn, u32Address, 1);
1459 ich9pciConfigWrite(pGlobals, uBus, uDevFn, u32Address, UINT32_C(0xffffffff), 4);
1460 uint32_t u32Size = ich9pciConfigRead(pGlobals, uBus, uDevFn, u32Address, 4);
1461 /* Clear resource information depending on resource type. */
1462 if ((u8ResourceType & PCI_COMMAND_IOACCESS) == PCI_COMMAND_IOACCESS) /* I/O */
1463 u32Size &= ~(0x01);
1464 else /* MMIO */
1465 u32Size &= ~(0x0f);
1466
1467 bool fIsPio = ((u8ResourceType & PCI_COMMAND_IOACCESS) == PCI_COMMAND_IOACCESS);
1468 /*
1469 * Invert all bits and add 1 to get size of the region.
1470 * (From PCI implementation note)
1471 */
1472 if (fIsPio && (u32Size & UINT32_C(0xffff0000)) == 0)
1473 u32Size = (~(u32Size | UINT32_C(0xffff0000))) + 1;
1474 else
1475 u32Size = (~u32Size) + 1;
1476
1477 Log(("%s: Size of region %u for device %d on bus %d is %u\n", __FUNCTION__, iRegion, uDevFn, uBus, u32Size));
1478
1479 if (u32Size)
1480 {
1481 paddr = fIsPio ? &pGlobals->uPciBiosIo : &pGlobals->uPciBiosMmio;
1482 *paddr = (*paddr + u32Size - 1) & ~(u32Size - 1);
1483 Log(("%s: Start address of %s region %u is %#x\n", __FUNCTION__, (fIsPio ? "I/O" : "MMIO"), iRegion, *paddr));
1484 ich9pciSetRegionAddress(pGlobals, uBus, uDevFn, iRegion, *paddr);
1485 *paddr += u32Size;
1486 Log(("%s: New address is %#x\n", __FUNCTION__, *paddr));
1487 }
1488 }
1489 break;
1490 }
1491 }
1492
1493 /* map the interrupt */
1494 uint32_t uPin = ich9pciConfigRead(pGlobals, uBus, uDevFn, VBOX_PCI_INTERRUPT_PIN, 1);
1495 if (uPin != 0)
1496 {
1497 uint8_t uBridgeDevFn = uDevFn;
1498 uPin--;
1499
1500 /* We need to go up to the host bus to see which irq this device will assert there. */
1501 while (cBridgeDepth != 0)
1502 {
1503 /* Get the pin the device would assert on the bridge. */
1504 uPin = ((uBridgeDevFn >> 3) + uPin) & 3;
1505 uBridgeDevFn = paBridgePositions[cBridgeDepth];
1506 cBridgeDepth--;
1507 }
1508#if 0
1509 uPin = pci_slot_get_pirq(uDevFn, pin);
1510 pic_irq = pci_irqs[pin];
1511 ich9pciConfigWrite(pGlobals, uBus, uDevFn, PCI_INTERRUPT_LINE, pic_irq);
1512#endif
1513 }
1514}
1515
1516static const uint8_t auPciIrqs[4] = { 11, 9, 11, 9 };
1517
1518static DECLCALLBACK(int) ich9pciFakePCIBIOS(PPDMDEVINS pDevIns)
1519{
1520 unsigned i;
1521 uint8_t elcr[2] = {0, 0};
1522 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1523 PVM pVM = PDMDevHlpGetVM(pDevIns);
1524 Assert(pVM);
1525
1526 /*
1527 * Set the start addresses.
1528 */
1529 pGlobals->uPciBiosIo = 0xd000;
1530 pGlobals->uPciBiosMmio = UINT32_C(0xf0000000);
1531 pGlobals->uBus = 0;
1532
1533 /*
1534 * Activate IRQ mappings.
1535 */
1536 for (i = 0; i < 4; i++)
1537 {
1538 uint8_t irq = auPciIrqs[i];
1539 /* Set to trigger level. */
1540 elcr[irq >> 3] |= (1 << (irq & 7));
1541 }
1542
1543 /* Tell to the PIC. */
1544 VBOXSTRICTRC rcStrict = IOMIOPortWrite(pVM, 0x4d0, elcr[0], sizeof(uint8_t));
1545 if (rcStrict == VINF_SUCCESS)
1546 rcStrict = IOMIOPortWrite(pVM, 0x4d1, elcr[1], sizeof(uint8_t));
1547 if (rcStrict != VINF_SUCCESS)
1548 {
1549 AssertMsgFailed(("Writing to PIC failed! rcStrict=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1550 return RT_SUCCESS(rcStrict) ? VERR_INTERNAL_ERROR : VBOXSTRICTRC_VAL(rcStrict);
1551 }
1552
1553 /*
1554 * Init the devices.
1555 */
1556 for (i = 0; i < 256; i++)
1557 {
1558 uint8_t aBridgePositions[256];
1559
1560 memset(aBridgePositions, 0, sizeof(aBridgePositions));
1561 Log2(("PCI: Initializing device %d (%#x)\n",
1562 i, 0x80000000 | (i << 8)));
1563 ich9pciBiosInitDevice(pGlobals, 0, i, 0, aBridgePositions);
1564 }
1565
1566 return VINF_SUCCESS;
1567}
1568
1569static DECLCALLBACK(uint32_t) ich9pciConfigRead(PCIDevice *aDev, uint32_t u32Address, unsigned len)
1570{
1571 if ((u32Address + len) > 256 && (u32Address + len) < 4096)
1572 {
1573 AssertMsgReturn(false, ("Read from extended registers falled back to generic code\n"), 0);
1574 }
1575
1576 AssertMsgReturn(u32Address + len <= 256, ("Read after end of PCI config space\n"),
1577 0);
1578 switch (len)
1579 {
1580 case 1:
1581 return aDev->config[u32Address];
1582 case 2:
1583 return RT_LE2H_U16(*(uint16_t *)(aDev->config + u32Address));
1584 default:
1585 case 4:
1586 return RT_LE2H_U32(*(uint32_t *)(aDev->config + u32Address));
1587 }
1588}
1589
1590
1591/**
1592 * See paragraph 7.5 of PCI Express specification (p. 349) for definition of
1593 * registers and their writability policy.
1594 */
1595static DECLCALLBACK(void) ich9pciConfigWrite(PCIDevice *aDev, uint32_t u32Address,
1596 uint32_t val, unsigned len)
1597{
1598 if ((u32Address + len) > 256 && (u32Address + len) < 4096)
1599 {
1600 AssertMsgReturnVoid(false, ("Write to extended registers falled back to generic code\n"));
1601 }
1602
1603 AssertMsgReturnVoid(u32Address + len <= 256, ("Write after end of PCI config space\n"));
1604
1605 /* Fast case - update one of BARs or ROM address, 'while' only for 'break' */
1606 while ( len == 4
1607 && ( ( u32Address >= VBOX_PCI_BASE_ADDRESS_0
1608 && u32Address < VBOX_PCI_BASE_ADDRESS_0 + 6 * 4)
1609 || ( u32Address >= VBOX_PCI_ROM_ADDRESS
1610 && u32Address < VBOX_PCI_ROM_ADDRESS+4)
1611 )
1612 )
1613 {
1614 PCIIORegion *pRegion;
1615 int reg, regionSize;
1616
1617 reg = (u32Address >= VBOX_PCI_ROM_ADDRESS) ? PCI_ROM_SLOT : (u32Address - VBOX_PCI_BASE_ADDRESS_0) >> 2;
1618 pRegion = &aDev->Int.s.aIORegions[reg];
1619 regionSize = pRegion->size;
1620 if (regionSize == 0)
1621 break;
1622 /* compute the stored value */
1623 if (reg == PCI_ROM_SLOT) {
1624 /* keep ROM enable bit */
1625 val &= (~(regionSize - 1)) | 1;
1626 } else {
1627 val &= ~(regionSize - 1);
1628 val |= pRegion->type;
1629 }
1630 *(uint32_t *)(aDev->config + u32Address) = RT_H2LE_U32(val);
1631 ich9pciUpdateMappings(aDev);
1632 return;
1633 }
1634
1635 uint32_t addr = u32Address;
1636 bool fUpdateMappings = false;
1637 for (uint32_t i = 0; i < len; i++)
1638 {
1639 bool fWritable = false;
1640 switch (PCIDevGetHeaderType(aDev))
1641 {
1642 case 0x00: /* normal device */
1643 case 0x80: /* multi-function device */
1644 switch (addr)
1645 {
1646 /* Read-only registers, see */
1647 case VBOX_PCI_VENDOR_ID: case VBOX_PCI_VENDOR_ID+1:
1648 case VBOX_PCI_DEVICE_ID: case VBOX_PCI_DEVICE_ID+1:
1649 case VBOX_PCI_REVISION_ID:
1650 case VBOX_PCI_CLASS_PROG:
1651 case VBOX_PCI_CLASS_SUB:
1652 case VBOX_PCI_CLASS_BASE:
1653 case VBOX_PCI_HEADER_TYPE:
1654 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:
1655 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:
1656 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:
1657 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:
1658 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:
1659 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:
1660 case VBOX_PCI_SUBSYSTEM_VENDOR_ID: case VBOX_PCI_SUBSYSTEM_VENDOR_ID+1:
1661 case VBOX_PCI_SUBSYSTEM_ID: case VBOX_PCI_SUBSYSTEM_ID+1:
1662 case VBOX_PCI_ROM_ADDRESS: case VBOX_PCI_ROM_ADDRESS+1: case VBOX_PCI_ROM_ADDRESS+2: case VBOX_PCI_ROM_ADDRESS+3:
1663 case VBOX_PCI_CAPABILITY_LIST:
1664 case VBOX_PCI_INTERRUPT_PIN:
1665 fWritable = false;
1666 break;
1667 /* Others can be written */
1668 default:
1669 fWritable = true;
1670 break;
1671 }
1672 break;
1673 default:
1674 case 0x01: /* bridge */
1675 switch (addr)
1676 {
1677 /* Read-only registers */
1678 case VBOX_PCI_VENDOR_ID: case VBOX_PCI_VENDOR_ID+1:
1679 case VBOX_PCI_DEVICE_ID: case VBOX_PCI_DEVICE_ID+1:
1680 case VBOX_PCI_REVISION_ID:
1681 case VBOX_PCI_CLASS_PROG:
1682 case VBOX_PCI_CLASS_SUB:
1683 case VBOX_PCI_CLASS_BASE:
1684 case VBOX_PCI_HEADER_TYPE:
1685 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:
1686 case VBOX_PCI_INTERRUPT_PIN:
1687 fWritable = false;
1688 break;
1689 default:
1690 fWritable = true;
1691 break;
1692 }
1693 break;
1694 }
1695
1696 switch (addr)
1697 {
1698 case VBOX_PCI_COMMAND: /* Command register, bits 0-7. */
1699 fUpdateMappings = true;
1700 aDev->config[addr] = val;
1701 break;
1702 case VBOX_PCI_COMMAND+1: /* Command register, bits 8-15. */
1703 /* don't change reserved bits (11-15) */
1704 val &= UINT32_C(~0xf8);
1705 fUpdateMappings = true;
1706 aDev->config[addr] = val;
1707 break;
1708 case VBOX_PCI_STATUS: /* Status register, bits 0-7. */
1709 /* don't change read-only bits => actually all lower bits are read-only */
1710 val &= UINT32_C(~0xff);
1711 /* status register, low part: clear bits by writing a '1' to the corresponding bit */
1712 aDev->config[addr] &= ~val;
1713 break;
1714 case VBOX_PCI_STATUS+1: /* Status register, bits 8-15. */
1715 /* don't change read-only bits */
1716 val &= UINT32_C(~0x06);
1717 /* status register, high part: clear bits by writing a '1' to the corresponding bit */
1718 aDev->config[addr] &= ~val;
1719 break;
1720 default:
1721 if (fWritable)
1722 aDev->config[addr] = val;
1723 }
1724 addr++;
1725 val >>= 8;
1726 }
1727
1728 if (fUpdateMappings)
1729 /* if the command register is modified, we must modify the mappings */
1730 ich9pciUpdateMappings(aDev);
1731}
1732
1733/* Slot/functions assignment per table at p. 12 of ICH9 family spec update */
1734static const struct {
1735 const char* pszName;
1736 int32_t iSlot;
1737 int32_t iFunction;
1738} PciSlotAssignments[] = {
1739 {
1740 "piix3ide", 1, 1 // do we really need it?
1741 },
1742 {
1743 "lan", 25, 0 /* LAN controller */
1744 },
1745 {
1746 "hda", 27, 0 /* High Definition Audio */
1747 },
1748 {
1749 "i82801", 30, 0 /* Host Controller */
1750 },
1751 {
1752 "lpc", 31, 0 /* Low Pin Count bus */
1753 },
1754 {
1755 "ahci", 31, 2 /* SATA controller */
1756 },
1757 {
1758 "smbus", 31, 3 /* System Management Bus */
1759 },
1760 {
1761 "thermal", 31, 6 /* Thermal controller */
1762 },
1763};
1764
1765static int assignPosition(PPCIBUS pBus, PPCIDEVICE pPciDev, const char *pszName)
1766{
1767 /* Hardcoded slots/functions, per chipset spec */
1768 for (size_t i = 0; i < RT_ELEMENTS(PciSlotAssignments); i++)
1769 {
1770 if (!strcmp(pszName, PciSlotAssignments[i].pszName))
1771 {
1772 PCISetRequestedDevfunc(pPciDev);
1773 return (PciSlotAssignments[i].iSlot << 3) + PciSlotAssignments[i].iFunction;
1774 }
1775 }
1776
1777 /* Otherwise when assigning a slot, we need to make sure all its functions are available */
1778 for (int iPos = 0; iPos < (int)RT_ELEMENTS(pBus->apDevices); iPos += 8)
1779 if ( !pBus->apDevices[iPos]
1780 && !pBus->apDevices[iPos + 1]
1781 && !pBus->apDevices[iPos + 2]
1782 && !pBus->apDevices[iPos + 3]
1783 && !pBus->apDevices[iPos + 4]
1784 && !pBus->apDevices[iPos + 5]
1785 && !pBus->apDevices[iPos + 6]
1786 && !pBus->apDevices[iPos + 7])
1787 {
1788 PCIClearRequestedDevfunc(pPciDev);
1789 return iPos;
1790 }
1791
1792 return -1;
1793}
1794
1795static bool hasHardAssignedDevsInSlot(PPCIBUS pBus, int iSlot)
1796{
1797 PCIDevice** aSlot = &pBus->apDevices[iSlot << 3];
1798
1799 return (aSlot[0] && PCIIsRequestedDevfunc(aSlot[0]))
1800 || (aSlot[1] && PCIIsRequestedDevfunc(aSlot[1]))
1801 || (aSlot[2] && PCIIsRequestedDevfunc(aSlot[2]))
1802 || (aSlot[3] && PCIIsRequestedDevfunc(aSlot[3]))
1803 || (aSlot[4] && PCIIsRequestedDevfunc(aSlot[4]))
1804 || (aSlot[5] && PCIIsRequestedDevfunc(aSlot[5]))
1805 || (aSlot[6] && PCIIsRequestedDevfunc(aSlot[6]))
1806 || (aSlot[7] && PCIIsRequestedDevfunc(aSlot[7]))
1807 ;
1808}
1809
1810static int ich9pciRegisterInternal(PPCIBUS pBus, int iDev, PPCIDEVICE pPciDev, const char *pszName)
1811{
1812 /*
1813 * Find device position
1814 */
1815 if (iDev < 0)
1816 {
1817 iDev = assignPosition(pBus, pPciDev, pszName);
1818 if (iDev < 0)
1819 {
1820 AssertMsgFailed(("Couldn't find free spot!\n"));
1821 return VERR_PDM_TOO_PCI_MANY_DEVICES;
1822 }
1823 }
1824
1825 /*
1826 * Check if we can really take this slot, possibly by relocating
1827 * its current habitant, if it wasn't hard assigned too.
1828 */
1829 if (PCIIsRequestedDevfunc(pPciDev) &&
1830 pBus->apDevices[iDev] &&
1831 PCIIsRequestedDevfunc(pBus->apDevices[iDev]))
1832 {
1833 /*
1834 * Smth like hasHardAssignedDevsInSlot(pBus, iDev >> 3) shall be use to make
1835 * it compatible with DevPCI.cpp version, but this way we cannot assign
1836 * in accordance with the chipset spec.
1837 */
1838 AssertReleaseMsgFailed(("Configuration error:'%s' and '%s' are both configured as device %d\n",
1839 pszName, pBus->apDevices[iDev]->name, iDev));
1840 return VERR_INTERNAL_ERROR;
1841 }
1842
1843 if (pBus->apDevices[iDev])
1844 {
1845 /* if we got here, we shall (and usually can) relocate the device */
1846 int iRelDev = assignPosition(pBus, pBus->apDevices[iDev], pBus->apDevices[iDev]->name);
1847 if (iRelDev < 0 || iRelDev == iDev)
1848 {
1849 AssertMsgFailed(("Couldn't find free spot!\n"));
1850 return VERR_PDM_TOO_PCI_MANY_DEVICES;
1851 }
1852 /* Copy device function by function to its new position */
1853 for (int i = 0; i < 8; i++)
1854 {
1855 if (!pBus->apDevices[iDev + i])
1856 continue;
1857 Log(("PCI: relocating '%s' from slot %#x to %#x\n", pBus->apDevices[iDev + i]->name, iDev + i, iRelDev + i));
1858 pBus->apDevices[iRelDev + i] = pBus->apDevices[iDev + i];
1859 pBus->apDevices[iRelDev + i]->devfn = iRelDev + i;
1860 pBus->apDevices[iDev + i] = NULL;
1861 }
1862 }
1863
1864 /*
1865 * Fill in device information.
1866 */
1867 pPciDev->devfn = iDev;
1868 pPciDev->name = pszName;
1869 pPciDev->Int.s.pBusR3 = pBus;
1870 pPciDev->Int.s.pBusR0 = MMHyperR3ToR0(PDMDevHlpGetVM(pBus->CTX_SUFF(pDevIns)), pBus);
1871 pPciDev->Int.s.pBusRC = MMHyperR3ToRC(PDMDevHlpGetVM(pBus->CTX_SUFF(pDevIns)), pBus);
1872 pPciDev->Int.s.pfnConfigRead = ich9pciConfigRead;
1873 pPciDev->Int.s.pfnConfigWrite = ich9pciConfigWrite;
1874 pBus->apDevices[iDev] = pPciDev;
1875 if (PCIIsPci2PciBridge(pPciDev))
1876 {
1877 AssertMsg(pBus->cBridges < RT_ELEMENTS(pBus->apDevices), ("Number of bridges exceeds the number of possible devices on the bus\n"));
1878 AssertMsg(pPciDev->Int.s.pfnBridgeConfigRead && pPciDev->Int.s.pfnBridgeConfigWrite,
1879 ("device is a bridge but does not implement read/write functions\n"));
1880 pBus->papBridgesR3[pBus->cBridges] = pPciDev;
1881 pBus->cBridges++;
1882 }
1883
1884 Log(("PCI: Registered device %d function %d (%#x) '%s'.\n",
1885 iDev >> 3, iDev & 7, 0x80000000 | (iDev << 8), pszName));
1886
1887 return VINF_SUCCESS;
1888}
1889
1890
1891/**
1892 * Info handler, device version.
1893 *
1894 * @param pDevIns Device instance which registered the info.
1895 * @param pHlp Callback functions for doing output.
1896 * @param pszArgs Argument string. Optional and specific to the handler.
1897 */
1898static DECLCALLBACK(void) ich9pciInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1899{
1900 PPCIBUS pBus = DEVINS_2_PCIBUS(pDevIns);
1901 uint32_t iBus = 0, iDev;
1902
1903
1904 for (iDev = 0; iDev < RT_ELEMENTS(pBus->apDevices); iDev++)
1905 {
1906 PPCIDEVICE pPciDev = pBus->apDevices[iDev];
1907 if (pPciDev != NULL)
1908 pHlp->pfnPrintf(pHlp, "%02x:%02x:%02x %s: %x-%x\n",
1909 iBus, (iDev >> 3) & 0xff, iDev & 0x7,
1910 pPciDev->name,
1911 PCIDevGetVendorId(pPciDev), PCIDevGetDeviceId(pPciDev)
1912 );
1913 }
1914}
1915
1916
1917static DECLCALLBACK(int) ich9pciConstruct(PPDMDEVINS pDevIns,
1918 int iInstance,
1919 PCFGMNODE pCfg)
1920{
1921 int rc;
1922 Assert(iInstance == 0);
1923
1924 /*
1925 * Validate and read configuration.
1926 */
1927 if (!CFGMR3AreValuesValid(pCfg,
1928 "IOAPIC\0"
1929 "GCEnabled\0"
1930 "R0Enabled\0"
1931 "McfgBase\0"
1932 "McfgLength\0"
1933 ))
1934 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
1935
1936 /* query whether we got an IOAPIC */
1937 bool fUseIoApic;
1938 rc = CFGMR3QueryBoolDef(pCfg, "IOAPIC", &fUseIoApic, false);
1939 if (RT_FAILURE(rc))
1940 return PDMDEV_SET_ERROR(pDevIns, rc,
1941 N_("Configuration error: Failed to query boolean value \"IOAPIC\""));
1942
1943 /* check if RC code is enabled. */
1944 bool fGCEnabled;
1945 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &fGCEnabled, true);
1946 if (RT_FAILURE(rc))
1947 return PDMDEV_SET_ERROR(pDevIns, rc,
1948 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
1949
1950 /* check if R0 code is enabled. */
1951 bool fR0Enabled;
1952 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &fR0Enabled, true);
1953 if (RT_FAILURE(rc))
1954 return PDMDEV_SET_ERROR(pDevIns, rc,
1955 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
1956
1957 Log(("PCI: fUseIoApic=%RTbool fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fUseIoApic, fGCEnabled, fR0Enabled));
1958
1959 /*
1960 * Init data.
1961 */
1962 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1963 PPCIBUS pBus = &pGlobals->aPciBus;
1964 /* Zero out everything */
1965 memset(pGlobals, 0, sizeof(*pGlobals));
1966 /* And fill values */
1967 if (!fUseIoApic)
1968 return PDMDEV_SET_ERROR(pDevIns, rc,
1969 N_("Must use IO-APIC with ICH9 chipset"));
1970 rc = CFGMR3QueryU64Def(pCfg, "McfgBase", &pGlobals->u64PciConfigMMioAddress, 0);
1971 if (RT_FAILURE(rc))
1972 return PDMDEV_SET_ERROR(pDevIns, rc,
1973 N_("Configuration error: Failed to read \"McfgBase\""));
1974 rc = CFGMR3QueryU64Def(pCfg, "McfgLength", &pGlobals->u64PciConfigMMioLength, 0);
1975 if (RT_FAILURE(rc))
1976 return PDMDEV_SET_ERROR(pDevIns, rc,
1977 N_("Configuration error: Failed to read \"McfgLength\""));
1978
1979 pGlobals->pDevInsR3 = pDevIns;
1980 pGlobals->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
1981 pGlobals->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1982
1983 pGlobals->aPciBus.pDevInsR3 = pDevIns;
1984 pGlobals->aPciBus.pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
1985 pGlobals->aPciBus.pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1986 pGlobals->aPciBus.papBridgesR3 = (PPCIDEVICE *)PDMDevHlpMMHeapAllocZ(pDevIns, sizeof(PPCIDEVICE) * RT_ELEMENTS(pGlobals->aPciBus.apDevices));
1987
1988 /*
1989 * Register bus
1990 */
1991 PDMPCIBUSREG PciBusReg;
1992 PciBusReg.u32Version = PDM_PCIBUSREG_VERSION;
1993 PciBusReg.pfnRegisterR3 = ich9pciRegister;
1994 PciBusReg.pfnIORegionRegisterR3 = ich9pciIORegionRegister;
1995 PciBusReg.pfnSetConfigCallbacksR3 = ich9pciSetConfigCallbacks;
1996 PciBusReg.pfnSetIrqR3 = ich9pciSetIrq;
1997 PciBusReg.pfnSaveExecR3 = ich9pciGenericSaveExec;
1998 PciBusReg.pfnLoadExecR3 = ich9pciGenericLoadExec;
1999 PciBusReg.pfnFakePCIBIOSR3 = ich9pciFakePCIBIOS;
2000 PciBusReg.pszSetIrqRC = fGCEnabled ? "ich9pciSetIrq" : NULL;
2001 PciBusReg.pszSetIrqR0 = fR0Enabled ? "ich9pciSetIrq" : NULL;
2002 rc = PDMDevHlpPCIBusRegister(pDevIns, &PciBusReg, &pBus->pPciHlpR3);
2003 if (RT_FAILURE(rc))
2004 return PDMDEV_SET_ERROR(pDevIns, rc,
2005 N_("Failed to register ourselves as a PCI Bus"));
2006 if (pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION)
2007 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2008 N_("PCI helper version mismatch; got %#x expected %#x"),
2009 pBus->pPciHlpR3->u32Version, PDM_PCIHLPR3_VERSION);
2010
2011 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2012 pBus->pPciHlpR0 = pBus->pPciHlpR3->pfnGetR0Helpers(pDevIns);
2013
2014 /*
2015 * Fill in PCI configs and add them to the bus.
2016 */
2017
2018 /**
2019 * We emulate 82801IB ICH9 IO chip used in Q35,
2020 * see http://ark.intel.com/Product.aspx?id=31892
2021 *
2022 * Stepping S-Spec Top Marking
2023 *
2024 * A2 SLA9M NH82801IB
2025 */
2026 PCIDevSetVendorId( &pBus->aPciDev, 0x8086); /* Intel */
2027 PCIDevSetDeviceId( &pBus->aPciDev, 0x244e); /* Desktop */
2028 PCIDevSetRevisionId(&pBus->aPciDev, 0x92); /* rev. A2 */
2029 PCIDevSetClassSub( &pBus->aPciDev, 0x00); /* Host/PCI bridge */
2030 PCIDevSetClassBase( &pBus->aPciDev, 0x06); /* bridge */
2031 PCIDevSetHeaderType(&pBus->aPciDev, 0x00); /* normal device */
2032
2033 pBus->aPciDev.pDevIns = pDevIns;
2034 /* We register Host<->PCI controller on the bus */
2035 ich9pciRegisterInternal(pBus, -1, &pBus->aPciDev, "i82801");
2036
2037 /*
2038 * Register I/O ports and save state.
2039 */
2040 rc = PDMDevHlpIOPortRegister(pDevIns, 0x0cf8, 1, NULL, ich9pciIOPortAddressWrite, ich9pciIOPortAddressRead, NULL, NULL, "ICH9 (PCI)");
2041 if (RT_FAILURE(rc))
2042 return rc;
2043 rc = PDMDevHlpIOPortRegister(pDevIns, 0x0cfc, 4, NULL, ich9pciIOPortDataWrite, ich9pciIOPortDataRead, NULL, NULL, "ICH9 (PCI)");
2044 if (RT_FAILURE(rc))
2045 return rc;
2046 if (fGCEnabled)
2047 {
2048 rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x0cf8, 1, NIL_RTGCPTR, "ich9pciIOPortAddressWrite", "ich9pciIOPortAddressRead", NULL, NULL, "ICH9 (PCI)");
2049 if (RT_FAILURE(rc))
2050 return rc;
2051 rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x0cfc, 4, NIL_RTGCPTR, "ich9pciIOPortDataWrite", "ich9pciIOPortDataRead", NULL, NULL, "ICH9 (PCI)");
2052 if (RT_FAILURE(rc))
2053 return rc;
2054 }
2055 if (fR0Enabled)
2056 {
2057 rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x0cf8, 1, NIL_RTR0PTR, "ich9pciIOPortAddressWrite", "ich9pciIOPortAddressRead", NULL, NULL, "ICH9 (PCI)");
2058 if (RT_FAILURE(rc))
2059 return rc;
2060 rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x0cfc, 4, NIL_RTR0PTR, "ich9pciIOPortDataWrite", "ich9pciIOPortDataRead", NULL, NULL, "ICH9 (PCI)");
2061 if (RT_FAILURE(rc))
2062 return rc;
2063 }
2064
2065 if (pGlobals->u64PciConfigMMioAddress != 0)
2066 {
2067 rc = PDMDevHlpMMIORegister(pDevIns, pGlobals->u64PciConfigMMioAddress, pGlobals->u64PciConfigMMioLength, pGlobals,
2068 ich9pciMcfgMMIOWrite, ich9pciMcfgMMIORead, NULL, "MCFG ranges");
2069 if (RT_FAILURE(rc))
2070 {
2071 AssertMsgRC(rc, ("Cannot register MCFG MMIO: %Rrc\n", rc));
2072 return rc;
2073 }
2074
2075 if (fGCEnabled)
2076 {
2077
2078 rc = PDMDevHlpMMIORegisterRC(pDevIns,
2079 pGlobals->u64PciConfigMMioAddress,
2080 pGlobals->u64PciConfigMMioLength,
2081 0,
2082 "ich9pciMcfgMMIOWrite",
2083 "ich9pciMcfgMMIORead",
2084 NULL);
2085 if (RT_FAILURE(rc))
2086 {
2087 AssertMsgRC(rc, ("Cannot register MCFG MMIO (GC): %Rrc\n", rc));
2088 return rc;
2089 }
2090 }
2091
2092
2093 if (fR0Enabled)
2094 {
2095
2096 rc = PDMDevHlpMMIORegisterR0(pDevIns,
2097 pGlobals->u64PciConfigMMioAddress,
2098 pGlobals->u64PciConfigMMioLength,
2099 0,
2100 "ich9pciMcfgMMIOWrite",
2101 "ich9pciMcfgMMIORead",
2102 NULL);
2103 if (RT_FAILURE(rc))
2104 {
2105 AssertMsgRC(rc, ("Cannot register MCFG MMIO (R0): %Rrc\n", rc));
2106 return rc;
2107 }
2108 }
2109 }
2110
2111
2112 /** @todo: other chipset devices shall be registered too */
2113 /** @todo: what to with bridges? */
2114
2115 PDMDevHlpDBGFInfoRegister(pDevIns, "pci", "Display PCI bus status. (no arguments)", ich9pciInfo);
2116
2117 return VINF_SUCCESS;
2118}
2119
2120/**
2121 * @copydoc FNPDMDEVRELOCATE
2122 */
2123static DECLCALLBACK(void) ich9pciRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2124{
2125 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
2126 PPCIBUS pBus = &pGlobals->aPciBus;
2127 pGlobals->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2128
2129 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2130 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2131
2132 /* Relocate RC pointers for the attached pci devices. */
2133 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
2134 {
2135 if (pBus->apDevices[i])
2136 pBus->apDevices[i]->Int.s.pBusRC += offDelta;
2137 }
2138
2139}
2140
2141/**
2142 * @interface_method_impl{PDMDEVREG,pfnConstruct}
2143 */
2144static DECLCALLBACK(int) ich9pcibridgeConstruct(PPDMDEVINS pDevIns,
2145 int iInstance,
2146 PCFGMNODE pCfg)
2147{
2148 int rc;
2149
2150 /*
2151 * Validate and read configuration.
2152 */
2153 if (!CFGMR3AreValuesValid(pCfg, "GCEnabled\0" "R0Enabled\0"))
2154 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
2155
2156 /* check if RC code is enabled. */
2157 bool fGCEnabled;
2158 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &fGCEnabled, true);
2159 if (RT_FAILURE(rc))
2160 return PDMDEV_SET_ERROR(pDevIns, rc,
2161 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
2162
2163 /* check if R0 code is enabled. */
2164 bool fR0Enabled;
2165 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &fR0Enabled, true);
2166 if (RT_FAILURE(rc))
2167 return PDMDEV_SET_ERROR(pDevIns, rc,
2168 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
2169 Log(("PCI: fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fGCEnabled, fR0Enabled));
2170
2171 /*
2172 * Init data and register the PCI bus.
2173 */
2174 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2175 pBus->pDevInsR3 = pDevIns;
2176 pBus->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2177 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2178 pBus->papBridgesR3 = (PPCIDEVICE *)PDMDevHlpMMHeapAllocZ(pDevIns, sizeof(PPCIDEVICE) * RT_ELEMENTS(pBus->apDevices));
2179
2180 PDMPCIBUSREG PciBusReg;
2181 PciBusReg.u32Version = PDM_PCIBUSREG_VERSION;
2182 PciBusReg.pfnRegisterR3 = ich9pcibridgeRegister;
2183 PciBusReg.pfnIORegionRegisterR3 = ich9pciIORegionRegister;
2184 PciBusReg.pfnSetConfigCallbacksR3 = ich9pciSetConfigCallbacks;
2185 PciBusReg.pfnSetIrqR3 = ich9pcibridgeSetIrq;
2186 PciBusReg.pfnSaveExecR3 = ich9pciGenericSaveExec;
2187 PciBusReg.pfnLoadExecR3 = ich9pciGenericLoadExec;
2188 PciBusReg.pfnFakePCIBIOSR3 = NULL; /* Only needed for the first bus. */
2189 PciBusReg.pszSetIrqRC = fGCEnabled ? "ich9pcibridgeSetIrq" : NULL;
2190 PciBusReg.pszSetIrqR0 = fR0Enabled ? "ich9pcibridgeSetIrq" : NULL;
2191 rc = PDMDevHlpPCIBusRegister(pDevIns, &PciBusReg, &pBus->pPciHlpR3);
2192 if (RT_FAILURE(rc))
2193 return PDMDEV_SET_ERROR(pDevIns, rc,
2194 N_("Failed to register ourselves as a PCI Bus"));
2195 if (pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION)
2196 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2197 N_("PCI helper version mismatch; got %#x expected %#x"),
2198 pBus->pPciHlpR3->u32Version, PDM_PCIHLPR3_VERSION);
2199
2200 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2201 pBus->pPciHlpR0 = pBus->pPciHlpR3->pfnGetR0Helpers(pDevIns);
2202
2203 /*
2204 * Fill in PCI configs and add them to the bus.
2205 */
2206 PCIDevSetVendorId( &pBus->aPciDev, 0x8086); /* Intel */
2207 PCIDevSetDeviceId( &pBus->aPciDev, 0x2448); /* 82801 Mobile PCI bridge. */
2208 PCIDevSetRevisionId(&pBus->aPciDev, 0xf2);
2209 PCIDevSetClassSub( &pBus->aPciDev, 0x04); /* pci2pci */
2210 PCIDevSetClassBase( &pBus->aPciDev, 0x06); /* PCI_bridge */
2211 PCIDevSetClassProg( &pBus->aPciDev, 0x01); /* Supports subtractive decoding. */
2212 PCIDevSetHeaderType(&pBus->aPciDev, 0x01); /* Single function device which adheres to the PCI-to-PCI bridge spec. */
2213 PCIDevSetCommand( &pBus->aPciDev, 0x00);
2214 PCIDevSetStatus( &pBus->aPciDev, 0x20); /* 66MHz Capable. */
2215 PCIDevSetInterruptLine(&pBus->aPciDev, 0x00); /* This device does not assert interrupts. */
2216
2217 /*
2218 * This device does not generate interrupts. Interrupt delivery from
2219 * devices attached to the bus is unaffected.
2220 */
2221 PCIDevSetInterruptPin (&pBus->aPciDev, 0x00);
2222
2223 pBus->aPciDev.pDevIns = pDevIns;
2224
2225 /* Bridge-specific data */
2226 PCISetPci2PciBridge(&pBus->aPciDev);
2227 pBus->aPciDev.Int.s.pfnBridgeConfigRead = ich9pcibridgeConfigRead;
2228 pBus->aPciDev.Int.s.pfnBridgeConfigWrite = ich9pcibridgeConfigWrite;
2229
2230 /*
2231 * Register this PCI bridge. The called function will take care on which bus we will get registered.
2232 */
2233 rc = PDMDevHlpPCIRegister (pDevIns, &pBus->aPciDev);
2234 if (RT_FAILURE(rc))
2235 return rc;
2236
2237 /*
2238 * The iBus property doesn't really represent the bus number
2239 * because the guest and the BIOS can choose different bus numbers
2240 * for them.
2241 * The bus number is mainly for the setIrq function to indicate
2242 * when the host bus is reached which will have iBus = 0.
2243 * Thathswhy the + 1.
2244 */
2245 pBus->iBus = iInstance + 1;
2246
2247 /*
2248 * Register SSM handlers. We use the same saved state version as for the host bridge
2249 * to make changes easier.
2250 */
2251 rc = PDMDevHlpSSMRegisterEx(pDevIns, VBOX_ICH9PCI_SAVED_STATE_VERSION, sizeof(*pBus) + 16*128, "pgm",
2252 NULL, NULL, NULL,
2253 NULL, ich9pcibridgeR3SaveExec, NULL,
2254 NULL, ich9pcibridgeR3LoadExec, NULL);
2255 if (RT_FAILURE(rc))
2256 return rc;
2257
2258
2259 return VINF_SUCCESS;
2260}
2261
2262/**
2263 * @copydoc FNPDMDEVRESET
2264 */
2265static DECLCALLBACK(void) ich9pcibridgeReset(PPDMDEVINS pDevIns)
2266{
2267 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2268
2269 /* Reset config space to default values. */
2270 pBus->aPciDev.config[VBOX_PCI_PRIMARY_BUS] = 0;
2271 pBus->aPciDev.config[VBOX_PCI_SECONDARY_BUS] = 0;
2272 pBus->aPciDev.config[VBOX_PCI_SUBORDINATE_BUS] = 0;
2273}
2274
2275
2276/**
2277 * @copydoc FNPDMDEVRELOCATE
2278 */
2279static DECLCALLBACK(void) ich9pcibridgeRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2280{
2281 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2282 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2283
2284 /* Relocate RC pointers for the attached pci devices. */
2285 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->apDevices); i++)
2286 {
2287 if (pBus->apDevices[i])
2288 pBus->apDevices[i]->Int.s.pBusRC += offDelta;
2289 }
2290
2291}
2292
2293/**
2294 * The PCI bus device registration structure.
2295 */
2296const PDMDEVREG g_DevicePciIch9 =
2297{
2298 /* u32Version */
2299 PDM_DEVREG_VERSION,
2300 /* szName */
2301 "ich9pci",
2302 /* szRCMod */
2303 "VBoxDDGC.gc",
2304 /* szR0Mod */
2305 "VBoxDDR0.r0",
2306 /* pszDescription */
2307 "ICH9 PCI bridge",
2308 /* fFlags */
2309 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2310 /* fClass */
2311 PDM_DEVREG_CLASS_BUS_PCI | PDM_DEVREG_CLASS_BUS_ISA,
2312 /* cMaxInstances */
2313 1,
2314 /* cbInstance */
2315 sizeof(PCIGLOBALS),
2316 /* pfnConstruct */
2317 ich9pciConstruct,
2318 /* pfnDestruct */
2319 NULL,
2320 /* pfnRelocate */
2321 ich9pciRelocate,
2322 /* pfnIOCtl */
2323 NULL,
2324 /* pfnPowerOn */
2325 NULL,
2326 /* pfnReset */
2327 NULL,
2328 /* pfnSuspend */
2329 NULL,
2330 /* pfnResume */
2331 NULL,
2332 /* pfnAttach */
2333 NULL,
2334 /* pfnDetach */
2335 NULL,
2336 /* pfnQueryInterface */
2337 NULL,
2338 /* pfnInitComplete */
2339 NULL,
2340 /* pfnPowerOff */
2341 NULL,
2342 /* pfnSoftReset */
2343 NULL,
2344 /* u32VersionEnd */
2345 PDM_DEVREG_VERSION
2346};
2347
2348/**
2349 * The device registration structure
2350 * for the PCI-to-PCI bridge.
2351 */
2352const PDMDEVREG g_DevicePciIch9Bridge =
2353{
2354 /* u32Version */
2355 PDM_DEVREG_VERSION,
2356 /* szName */
2357 "ich9pcibridge",
2358 /* szRCMod */
2359 "VBoxDDGC.gc",
2360 /* szR0Mod */
2361 "VBoxDDR0.r0",
2362 /* pszDescription */
2363 "ICH9 PCI to PCI bridge",
2364 /* fFlags */
2365 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2366 /* fClass */
2367 PDM_DEVREG_CLASS_BUS_PCI,
2368 /* cMaxInstances */
2369 ~0,
2370 /* cbInstance */
2371 sizeof(PCIBUS),
2372 /* pfnConstruct */
2373 ich9pcibridgeConstruct,
2374 /* pfnDestruct */
2375 NULL,
2376 /* pfnRelocate */
2377 ich9pcibridgeRelocate,
2378 /* pfnIOCtl */
2379 NULL,
2380 /* pfnPowerOn */
2381 NULL,
2382 /* pfnReset */
2383 ich9pcibridgeReset,
2384 /* pfnSuspend */
2385 NULL,
2386 /* pfnResume */
2387 NULL,
2388 /* pfnAttach */
2389 NULL,
2390 /* pfnDetach */
2391 NULL,
2392 /* pfnQueryInterface */
2393 NULL,
2394 /* pfnInitComplete */
2395 NULL,
2396 /* pfnPowerOff */
2397 NULL,
2398 /* pfnSoftReset */
2399 NULL,
2400 /* u32VersionEnd */
2401 PDM_DEVREG_VERSION
2402};
2403
2404#endif /* IN_RING3 */
2405#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

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