VirtualBox

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

Last change on this file since 65282 was 65101, checked in by vboxsync, 8 years ago

Devices: doxygen fixes

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