VirtualBox

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

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

PCI: some of 64-bit BARs support (many real cards do that)

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