VirtualBox

source: vbox/trunk/src/VBox/Devices/Bus/DevPCI.cpp@ 21593

Last change on this file since 21593 was 20374, checked in by vboxsync, 16 years ago

*: s/RT_\(BEGIN|END\)_DECLS/RT_C_DECLS_\1/g

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 98.1 KB
Line 
1/* $Id: DevPCI.cpp 20374 2009-06-08 00:43:21Z vboxsync $ */
2/** @file
3 * DevPCI - PCI BUS Device.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 * --------------------------------------------------------------------
21 *
22 * This code is based on:
23 *
24 * QEMU PCI bus manager
25 *
26 * Copyright (c) 2004 Fabrice Bellard
27 *
28 * Permission is hereby granted, free of charge, to any person obtaining a copy
29 * of this software and associated documentation files (the "Software"), to deal
30 * in the Software without restriction, including without limitation the rights
31 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32 * copies of the Software, and to permit persons to whom the Software is
33 * furnished to do so, subject to the following conditions:
34 *
35 * The above copyright notice and this permission notice shall be included in
36 * all copies or substantial portions of the Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
41 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 * THE SOFTWARE.
45 */
46
47/*******************************************************************************
48* Header Files *
49*******************************************************************************/
50#define LOG_GROUP LOG_GROUP_DEV_PCI
51/* Hack to get PCIDEVICEINT declare at the right point - include "PCIInternal.h". */
52#define PCI_INCLUDE_PRIVATE
53#include <VBox/pci.h>
54#include <VBox/pdmdev.h>
55#include <iprt/assert.h>
56#include <iprt/string.h>
57
58#include "../Builtins.h"
59
60
61/*******************************************************************************
62* Structures and Typedefs *
63*******************************************************************************/
64/**
65 * PIIX3 ISA Bridge state.
66 */
67typedef struct PIIX3State
68{
69 /** The PCI device of the bridge. */
70 PCIDEVICE dev;
71} PIIX3State, PIIX3, *PPIIX3;
72
73/**
74 * PCI Bus instance.
75 */
76typedef struct PCIBus
77{
78 /** Bus number. */
79 int32_t iBus;
80 /** Start device number. */
81 int32_t iDevSearch;
82 /** Number of bridges attached to the bus. */
83 uint32_t cBridges;
84
85 uint32_t Alignment0;
86
87 /** Array of PCI devices. */
88 R3PTRTYPE(PPCIDEVICE) devices[256];
89 /** Array of bridges attached to the bus. */
90 R3PTRTYPE(PPCIDEVICE *) papBridgesR3;
91
92 /** R3 pointer to the device instance. */
93 PPDMDEVINSR3 pDevInsR3;
94 /** Pointer to the PCI R3 helpers. */
95 PCPDMPCIHLPR3 pPciHlpR3;
96
97 /** R0 pointer to the device instance. */
98 PPDMDEVINSR0 pDevInsR0;
99 /** Pointer to the PCI R0 helpers. */
100 PCPDMPCIHLPR0 pPciHlpR0;
101
102 /** RC pointer to the device instance. */
103 PPDMDEVINSRC pDevInsRC;
104 /** Pointer to the PCI RC helpers. */
105 PCPDMPCIHLPRC pPciHlpRC;
106
107 /** The PCI device for the PCI bridge. */
108 PCIDEVICE PciDev;
109
110} PCIBUS;
111/** Pointer to a PCIBUS instance. */
112typedef PCIBUS *PPCIBUS;
113typedef PCIBUS PCIBus;
114
115/** @def PCI_IRQ_PINS
116 * Number of pins for interrupts (PIRQ#0...PIRQ#3)
117 */
118#define PCI_IRQ_PINS 4
119
120/** @def PCI_APIC_IRQ_PINS
121 * Number of pins for interrupts if the APIC is used.
122 */
123#define PCI_APIC_IRQ_PINS 8
124
125/**
126 * PCI Globals - This is the host-to-pci bridge and the root bus.
127 */
128typedef struct PCIGLOBALS
129{
130 /** Irq levels for the four PCI Irqs.
131 * These count how many devices asserted
132 * the IRQ line. If greater 0 an IRQ is sent to the guest.
133 * If it drops to 0 the IRQ is deasserted.
134 */
135 volatile uint32_t pci_irq_levels[PCI_IRQ_PINS];
136
137#if 1 /* Will be moved into the BIOS soon. */
138 /** The next I/O port address which the PCI BIOS will use. */
139 uint32_t pci_bios_io_addr;
140 /** The next MMIO address which the PCI BIOS will use. */
141 uint32_t pci_bios_mem_addr;
142 /** Actual bus number. */
143 uint8_t uBus;
144#endif
145
146 /** I/O APIC usage flag */
147 bool fUseIoApic;
148 /** I/O APIC irq levels */
149 volatile uint32_t pci_apic_irq_levels[PCI_APIC_IRQ_PINS];
150 /** ACPI IRQ level */
151 uint32_t acpi_irq_level;
152 /** ACPI PIC IRQ */
153 int acpi_irq;
154 /** Config register. */
155 uint32_t uConfigReg;
156
157 /** R3 pointer to the device instance. */
158 PPDMDEVINSR3 pDevInsR3;
159 /** R0 pointer to the device instance. */
160 PPDMDEVINSR0 pDevInsR0;
161 /** RC pointer to the device instance. */
162 PPDMDEVINSRC pDevInsRC;
163
164#if HC_ARCH_BITS == 64
165 uint32_t Alignment0;
166#endif
167
168 /** ISA bridge state. */
169 PIIX3 PIIX3State;
170 /** PCI bus which is attached to the host-to-PCI bridge. */
171 PCIBUS PciBus;
172
173} PCIGLOBALS;
174/** Pointer to per VM data. */
175typedef PCIGLOBALS *PPCIGLOBALS;
176
177
178/*******************************************************************************
179* Defined Constants And Macros *
180*******************************************************************************/
181
182/** Converts a bus instance pointer to a device instance pointer. */
183#define PCIBUS_2_DEVINS(pPciBus) ((pPciBus)->CTX_SUFF(pDevIns))
184/** Converts a device instance pointer to a PCIGLOBALS pointer. */
185#define DEVINS_2_PCIGLOBALS(pDevIns) ((PPCIGLOBALS)(PDMINS_2_DATA(pDevIns, PPCIGLOBALS)))
186/** Converts a device instance pointer to a PCIBUS pointer. */
187#define DEVINS_2_PCIBUS(pDevIns) ((PPCIBUS)(&PDMINS_2_DATA(pDevIns, PPCIGLOBALS)->PciBus))
188
189/** Converts a pointer to a PCI bus instance to a PCIGLOBALS pointer.
190 * @note This works only if the bus number is 0!!!
191 */
192#define PCIBUS_2_PCIGLOBALS(pPciBus) ( (PPCIGLOBALS)((uintptr_t)(pPciBus) - RT_OFFSETOF(PCIGLOBALS, PciBus)) )
193
194/** @def PCI_LOCK
195 * Acquires the PDM lock. This is a NOP if locking is disabled. */
196/** @def PCI_UNLOCK
197 * Releases the PDM lock. This is a NOP if locking is disabled. */
198#define PCI_LOCK(pDevIns, rc) \
199 do { \
200 int rc2 = DEVINS_2_PCIBUS(pDevIns)->CTX_SUFF(pPciHlp)->pfnLock((pDevIns), rc); \
201 if (rc2 != VINF_SUCCESS) \
202 return rc2; \
203 } while (0)
204#define PCI_UNLOCK(pDevIns) \
205 DEVINS_2_PCIBUS(pDevIns)->CTX_SUFF(pPciHlp)->pfnUnlock(pDevIns)
206
207/** @def VBOX_PCI_SAVED_STATE_VERSION
208 * Saved state version of the PCI bus device.
209 */
210#define VBOX_PCI_SAVED_STATE_VERSION 3
211
212
213#ifndef VBOX_DEVICE_STRUCT_TESTCASE
214/*******************************************************************************
215* Internal Functions *
216*******************************************************************************/
217RT_C_DECLS_BEGIN
218
219PDMBOTHCBDECL(void) pciSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel);
220PDMBOTHCBDECL(void) pcibridgeSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel);
221PDMBOTHCBDECL(int) pciIOPortAddressWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
222PDMBOTHCBDECL(int) pciIOPortAddressRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
223PDMBOTHCBDECL(int) pciIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
224PDMBOTHCBDECL(int) pciIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
225
226#ifdef IN_RING3
227DECLINLINE(PPCIDEVICE) pciFindBridge(PPCIBUS pBus, uint8_t iBus);
228#endif
229
230RT_C_DECLS_END
231
232#define DEBUG_PCI
233
234#define PCI_VENDOR_ID 0x00 /* 16 bits */
235#define PCI_DEVICE_ID 0x02 /* 16 bits */
236#define PCI_COMMAND 0x04 /* 16 bits */
237#define PCI_COMMAND_IO 0x01 /* Enable response in I/O space */
238#define PCI_COMMAND_MEMORY 0x02 /* Enable response in Memory space */
239#define PCI_CLASS_DEVICE 0x0a /* Device class */
240#define PCI_INTERRUPT_LINE 0x3c /* 8 bits */
241#define PCI_INTERRUPT_PIN 0x3d /* 8 bits */
242#define PCI_MIN_GNT 0x3e /* 8 bits */
243#define PCI_MAX_LAT 0x3f /* 8 bits */
244
245
246#ifdef IN_RING3
247
248static void pci_update_mappings(PCIDevice *d)
249{
250 PPCIBUS pBus = d->Int.s.CTX_SUFF(pBus);
251 PCIIORegion *r;
252 int cmd, i;
253 uint32_t last_addr, new_addr, config_ofs;
254
255 cmd = RT_LE2H_U16(*(uint16_t *)(d->config + PCI_COMMAND));
256 for(i = 0; i < PCI_NUM_REGIONS; i++) {
257 r = &d->Int.s.aIORegions[i];
258 if (i == PCI_ROM_SLOT) {
259 config_ofs = 0x30;
260 } else {
261 config_ofs = 0x10 + i * 4;
262 }
263 if (r->size != 0) {
264 if (r->type & PCI_ADDRESS_SPACE_IO) {
265 if (cmd & PCI_COMMAND_IO) {
266 new_addr = RT_LE2H_U32(*(uint32_t *)(d->config +
267 config_ofs));
268 new_addr = new_addr & ~(r->size - 1);
269 last_addr = new_addr + r->size - 1;
270 /* NOTE: we have only 64K ioports on PC */
271 if (last_addr <= new_addr || new_addr == 0 ||
272 last_addr >= 0x10000) {
273 new_addr = ~0U;
274 }
275 } else {
276 new_addr = ~0U;
277 }
278 } else {
279 if (cmd & PCI_COMMAND_MEMORY) {
280 new_addr = RT_LE2H_U32(*(uint32_t *)(d->config +
281 config_ofs));
282 /* the ROM slot has a specific enable bit */
283 if (i == PCI_ROM_SLOT && !(new_addr & 1))
284 goto no_mem_map;
285 new_addr = new_addr & ~(r->size - 1);
286 last_addr = new_addr + r->size - 1;
287 /* NOTE: we do not support wrapping */
288 /* XXX: as we cannot support really dynamic
289 mappings, we handle specific values as invalid
290 mappings. */
291 if (last_addr <= new_addr || new_addr == 0 ||
292 last_addr == ~0U) {
293 new_addr = ~0U;
294 }
295 } else {
296 no_mem_map:
297 new_addr = ~0U;
298 }
299 }
300 /* now do the real mapping */
301 if (new_addr != r->addr) {
302 if (r->addr != ~0U) {
303 if (r->type & PCI_ADDRESS_SPACE_IO) {
304 int devclass;
305 /* NOTE: specific hack for IDE in PC case:
306 only one byte must be mapped. */
307 devclass = d->config[0x0a] | (d->config[0x0b] << 8);
308 if (devclass == 0x0101 && r->size == 4) {
309 int rc = d->pDevIns->pDevHlpR3->pfnIOPortDeregister(d->pDevIns, r->addr + 2, 1);
310 AssertRC(rc);
311 } else {
312 int rc = d->pDevIns->pDevHlpR3->pfnIOPortDeregister(d->pDevIns, r->addr, r->size);
313 AssertRC(rc);
314 }
315 } else {
316 RTGCPHYS GCPhysBase = r->addr;
317 int rc;
318 if (pBus->pPciHlpR3->pfnIsMMIO2Base(pBus->pDevInsR3, d->pDevIns, GCPhysBase))
319 {
320 /* unmap it. */
321 rc = r->map_func(d, i, NIL_RTGCPHYS, r->size, (PCIADDRESSSPACE)(r->type));
322 AssertRC(rc);
323 rc = PDMDevHlpMMIO2Unmap(d->pDevIns, i, GCPhysBase);
324 }
325 else
326 rc = d->pDevIns->pDevHlpR3->pfnMMIODeregister(d->pDevIns, GCPhysBase, r->size);
327 AssertMsgRC(rc, ("rc=%Rrc d=%s i=%d GCPhysBase=%RGp size=%#x\n", rc, d->name, i, GCPhysBase, r->size));
328 }
329 }
330 r->addr = new_addr;
331 if (r->addr != ~0U) {
332 int rc = r->map_func(d, i,
333 r->addr + (r->type & PCI_ADDRESS_SPACE_IO ? 0 : 0),
334 r->size, (PCIADDRESSSPACE)(r->type));
335 AssertRC(rc);
336 }
337 }
338 }
339 }
340}
341
342
343static DECLCALLBACK(uint32_t) pci_default_read_config(PCIDevice *d, uint32_t address, unsigned len)
344{
345 uint32_t val;
346 switch(len) {
347 case 1:
348 val = d->config[address];
349 break;
350 case 2:
351 val = RT_LE2H_U16(*(uint16_t *)(d->config + address));
352 break;
353 default:
354 case 4:
355 val = RT_LE2H_U32(*(uint32_t *)(d->config + address));
356 break;
357 }
358 return val;
359}
360
361static DECLCALLBACK(void) pci_default_write_config(PCIDevice *d, uint32_t address, uint32_t val, unsigned len)
362{
363 int can_write;
364 unsigned i;
365 uint32_t end, addr;
366
367 if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) ||
368 (address >= 0x30 && address < 0x34))) {
369 PCIIORegion *r;
370 int reg;
371
372 if ( address >= 0x30 ) {
373 reg = PCI_ROM_SLOT;
374 }else{
375 reg = (address - 0x10) >> 2;
376 }
377 r = &d->Int.s.aIORegions[reg];
378 if (r->size == 0)
379 goto default_config;
380 /* compute the stored value */
381 if (reg == PCI_ROM_SLOT) {
382 /* keep ROM enable bit */
383 val &= (~(r->size - 1)) | 1;
384 } else {
385 val &= ~(r->size - 1);
386 val |= r->type;
387 }
388 *(uint32_t *)(d->config + address) = RT_H2LE_U32(val);
389 pci_update_mappings(d);
390 return;
391 }
392 default_config:
393 /* not efficient, but simple */
394 addr = address;
395 for(i = 0; i < len; i++) {
396 /* default read/write accesses */
397 switch(d->config[0x0e]) {
398 case 0x00: /* normal device */
399 case 0x80: /* multi-function device */
400 switch(addr) {
401 case 0x00:
402 case 0x01:
403 case 0x02:
404 case 0x03:
405 case 0x08:
406 case 0x09:
407 case 0x0a:
408 case 0x0b:
409 case 0x0e:
410 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17: /* base */
411 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
412 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
413 case 0x2c: case 0x2d: /* subsystem ID */
414 case 0x2e: case 0x2f: /* vendor ID */
415 case 0x30: case 0x31: case 0x32: case 0x33: /* rom */
416 case 0x3d:
417 can_write = 0;
418 break;
419 default:
420 can_write = 1;
421 break;
422 }
423 break;
424 default:
425 case 0x01: /* bridge */
426 switch(addr) {
427 case 0x00:
428 case 0x01:
429 case 0x02:
430 case 0x03:
431 case 0x08:
432 case 0x09:
433 case 0x0a:
434 case 0x0b:
435 case 0x0e:
436 case 0x38: case 0x39: case 0x3a: case 0x3b: /* rom */
437 case 0x3d:
438 can_write = 0;
439 break;
440 default:
441 can_write = 1;
442 break;
443 }
444 break;
445 }
446#ifdef VBOX
447 if (addr == 0x06)
448 {
449 /* don't change read-only bits => actually all lower bits are read-only */
450 val &= UINT32_C(~0xff);
451 /* status register, low part: clear bits by writing a '1' to the corresponding bit */
452 d->config[addr] &= ~val;
453 }
454 else if (addr == 0x07)
455 {
456 /* don't change read-only bits */
457 val &= UINT32_C(~0x06);
458 /* status register, high part: clear bits by writing a '1' to the corresponding bit */
459 d->config[addr] &= ~val;
460 }
461 else
462#endif
463 if (can_write) {
464 d->config[addr] = val;
465 }
466 addr++;
467 val >>= 8;
468 }
469
470 end = address + len;
471 if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
472 /* if the command register is modified, we must modify the mappings */
473 pci_update_mappings(d);
474 }
475}
476
477#endif /* IN_RING3 */
478
479static int pci_data_write(PPCIGLOBALS pGlobals, uint32_t addr, uint32_t val, int len)
480{
481 uint8_t iBus, iDevice;
482 uint32_t config_addr;
483
484 Log(("pci_data_write: addr=%08x val=%08x len=%d\n", pGlobals->uConfigReg, val, len));
485
486 if (!(pGlobals->uConfigReg & (1 << 31))) {
487 return VINF_SUCCESS;
488 }
489 if ((pGlobals->uConfigReg & 0x3) != 0) {
490 return VINF_SUCCESS;
491 }
492 iBus = (pGlobals->uConfigReg >> 16) & 0xff;
493 iDevice = (pGlobals->uConfigReg >> 8) & 0xff;
494 config_addr = (pGlobals->uConfigReg & 0xfc) | (addr & 3);
495 if (iBus != 0)
496 {
497 if (pGlobals->PciBus.cBridges)
498 {
499#ifdef IN_RING3 /** @todo do lookup in R0/RC too! */
500 PPCIDEVICE pBridgeDevice = pciFindBridge(&pGlobals->PciBus, iBus);
501 if (pBridgeDevice)
502 {
503 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigWrite);
504 pBridgeDevice->Int.s.pfnBridgeConfigWrite(pBridgeDevice->pDevIns, iBus, iDevice, config_addr, val, len);
505 }
506#else
507 return VINF_IOM_HC_IOPORT_WRITE;
508#endif
509 }
510 }
511 else
512 {
513 R3PTRTYPE(PCIDevice *) pci_dev = pGlobals->PciBus.devices[iDevice];
514 if (pci_dev)
515 {
516#ifdef IN_RING3
517 Log(("pci_config_write: %s: addr=%02x val=%08x len=%d\n", pci_dev->name, config_addr, val, len));
518 pci_dev->Int.s.pfnConfigWrite(pci_dev, config_addr, val, len);
519#else
520 return VINF_IOM_HC_IOPORT_WRITE;
521#endif
522 }
523 }
524 return VINF_SUCCESS;
525}
526
527static int pci_data_read(PPCIGLOBALS pGlobals, uint32_t addr, int len, uint32_t *pu32)
528{
529 uint8_t iBus, iDevice;
530 uint32_t config_addr;
531
532 *pu32 = 0xffffffff;
533
534 if (!(pGlobals->uConfigReg & (1 << 31)))
535 return VINF_SUCCESS;
536 if ((pGlobals->uConfigReg & 0x3) != 0)
537 return VINF_SUCCESS;
538 iBus = (pGlobals->uConfigReg >> 16) & 0xff;
539 iDevice = (pGlobals->uConfigReg >> 8) & 0xff;
540 config_addr = (pGlobals->uConfigReg & 0xfc) | (addr & 3);
541 if (iBus != 0)
542 {
543 if (pGlobals->PciBus.cBridges)
544 {
545#ifdef IN_RING3 /** @todo do lookup in R0/RC too! */
546 PPCIDEVICE pBridgeDevice = pciFindBridge(&pGlobals->PciBus, iBus);
547 if (pBridgeDevice)
548 {
549 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigRead);
550 *pu32 = pBridgeDevice->Int.s.pfnBridgeConfigRead(pBridgeDevice->pDevIns, iBus, iDevice, config_addr, len);
551 }
552#else
553 return VINF_IOM_HC_IOPORT_READ;
554#endif
555 }
556 }
557 else
558 {
559 R3PTRTYPE(PCIDevice *) pci_dev = pGlobals->PciBus.devices[iDevice];
560 if (pci_dev)
561 {
562#ifdef IN_RING3
563 *pu32 = pci_dev->Int.s.pfnConfigRead(pci_dev, config_addr, len);
564 Log(("pci_config_read: %s: addr=%02x val=%08x len=%d\n", pci_dev->name, config_addr, *pu32, len));
565#else
566 return VINF_IOM_HC_IOPORT_READ;
567#endif
568 }
569 }
570
571 return VINF_SUCCESS;
572}
573
574
575
576/* return the global irq number corresponding to a given device irq
577 pin. We could also use the bus number to have a more precise
578 mapping.
579 This is the implementation note described in the PCI spec chapter 2.2.6 */
580static inline int pci_slot_get_pirq(uint8_t uDevFn, int irq_num)
581{
582 int slot_addend;
583 slot_addend = (uDevFn >> 3) - 1;
584 return (irq_num + slot_addend) & 3;
585}
586
587static inline int pci_slot_get_apic_pirq(uint8_t uDevFn, int irq_num)
588{
589 return (irq_num + (uDevFn >> 3)) & 7;
590}
591
592static inline int get_pci_irq_apic_level(PPCIGLOBALS pGlobals, int irq_num)
593{
594 return (pGlobals->pci_apic_irq_levels[irq_num] != 0);
595}
596
597static void apic_set_irq(PPCIBUS pBus, uint8_t uDevFn, PCIDevice *pPciDev, int irq_num1, int iLevel, int acpi_irq)
598{
599 /* This is only allowed to be called with a pointer to the host bus. */
600 AssertMsg(pBus->iBus == 0, ("iBus=%u\n", pBus->iBus));
601
602 if (acpi_irq == -1) {
603 int apic_irq, apic_level;
604 PPCIGLOBALS pGlobals = PCIBUS_2_PCIGLOBALS(pBus);
605 int irq_num = pci_slot_get_apic_pirq(uDevFn, irq_num1);
606
607 if ((iLevel & PDM_IRQ_LEVEL_HIGH) == PDM_IRQ_LEVEL_HIGH)
608 ASMAtomicIncU32(&pGlobals->pci_apic_irq_levels[irq_num]);
609 else if ((iLevel & PDM_IRQ_LEVEL_HIGH) == PDM_IRQ_LEVEL_LOW)
610 ASMAtomicDecU32(&pGlobals->pci_apic_irq_levels[irq_num]);
611
612 apic_irq = irq_num + 0x10;
613 apic_level = get_pci_irq_apic_level(pGlobals, irq_num);
614 Log3(("apic_set_irq: %s: irq_num1=%d level=%d apic_irq=%d apic_level=%d irq_num1=%d\n",
615 R3STRING(pPciDev->name), irq_num1, iLevel, apic_irq, apic_level, irq_num));
616 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), apic_irq, apic_level);
617
618 if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP) {
619 ASMAtomicDecU32(&pGlobals->pci_apic_irq_levels[irq_num]);
620 pPciDev->Int.s.uIrqPinState = PDM_IRQ_LEVEL_LOW;
621 apic_level = get_pci_irq_apic_level(pGlobals, irq_num);
622 Log3(("apic_set_irq: %s: irq_num1=%d level=%d apic_irq=%d apic_level=%d irq_num1=%d (flop)\n",
623 R3STRING(pPciDev->name), irq_num1, iLevel, apic_irq, apic_level, irq_num));
624 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), apic_irq, apic_level);
625 }
626 } else {
627 Log3(("apic_set_irq: %s: irq_num1=%d level=%d acpi_irq=%d\n",
628 R3STRING(pPciDev->name), irq_num1, iLevel, acpi_irq));
629 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), acpi_irq, iLevel);
630 }
631}
632
633DECLINLINE(int) get_pci_irq_level(PPCIGLOBALS pGlobals, int irq_num)
634{
635 return (pGlobals->pci_irq_levels[irq_num] != 0);
636}
637
638/**
639 * Set the IRQ for a PCI device on the host bus - shared by host bus and bridge.
640 *
641 * @param pDevIns Device instance of the host PCI Bus.
642 * @param uDevFn The device number on the host bus which will raise the IRQ
643 * @param pPciDev The PCI device structure which raised the interrupt.
644 * @param iIrq IRQ number to set.
645 * @param iLevel IRQ level.
646 * @remark uDevFn and pPciDev->devfn are not the same if the device is behind a bridge.
647 * In that case uDevFn will be the slot of the bridge which is needed to calculate the
648 * PIRQ value.
649 */
650static void pciSetIrqInternal(PPCIGLOBALS pGlobals, uint8_t uDevFn, PPCIDEVICE pPciDev, int iIrq, int iLevel)
651{
652 PPCIBUS pBus = &pGlobals->PciBus;
653 uint8_t *pbCfg = pGlobals->PIIX3State.dev.config;
654 const bool fIsAcpiDevice = pPciDev->config[2] == 0x13 && pPciDev->config[3] == 0x71;
655 const bool fIsApicEnabled = pGlobals->fUseIoApic && pbCfg[0xde] == 0xbe && pbCfg[0xad] == 0xef;
656 int pic_irq, pic_level;
657
658 /* Check if the state changed. */
659 if (pPciDev->Int.s.uIrqPinState != iLevel)
660 {
661 pPciDev->Int.s.uIrqPinState = (iLevel & PDM_IRQ_LEVEL_HIGH);
662
663 /* apic only */
664 if (fIsApicEnabled)
665 {
666 if (fIsAcpiDevice)
667 /*
668 * ACPI needs special treatment since SCI is hardwired and
669 * should not be affected by PCI IRQ routing tables at the
670 * same time SCI IRQ is shared in PCI sense hence this
671 * kludge (i.e. we fetch the hardwired value from ACPIs
672 * PCI device configuration space).
673 */
674 apic_set_irq(pBus, uDevFn, pPciDev, -1, iLevel, pPciDev->config[PCI_INTERRUPT_LINE]);
675 else
676 apic_set_irq(pBus, uDevFn, pPciDev, iIrq, iLevel, -1);
677 return;
678 }
679
680 if (fIsAcpiDevice)
681 {
682 /* As per above treat ACPI in a special way */
683 pic_irq = pPciDev->config[PCI_INTERRUPT_LINE];
684 pGlobals->acpi_irq = pic_irq;
685 pGlobals->acpi_irq_level = iLevel & PDM_IRQ_LEVEL_HIGH;
686 }
687 else
688 {
689 int irq_num;
690 irq_num = pci_slot_get_pirq(uDevFn, iIrq);
691
692 if (pPciDev->Int.s.uIrqPinState == PDM_IRQ_LEVEL_HIGH)
693 ASMAtomicIncU32(&pGlobals->pci_irq_levels[irq_num]);
694 else if (pPciDev->Int.s.uIrqPinState == PDM_IRQ_LEVEL_LOW)
695 ASMAtomicDecU32(&pGlobals->pci_irq_levels[irq_num]);
696
697 /* now we change the pic irq level according to the piix irq mappings */
698 pic_irq = pbCfg[0x60 + irq_num];
699 if (pic_irq >= 16)
700 {
701 if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP)
702 {
703 ASMAtomicDecU32(&pGlobals->pci_irq_levels[irq_num]);
704 pPciDev->Int.s.uIrqPinState = PDM_IRQ_LEVEL_LOW;
705 }
706
707 return;
708 }
709 }
710
711 /* the pic level is the logical OR of all the PCI irqs mapped to it */
712 pic_level = 0;
713 if (pic_irq == pbCfg[0x60])
714 pic_level |= get_pci_irq_level(pGlobals, 0);
715 if (pic_irq == pbCfg[0x61])
716 pic_level |= get_pci_irq_level(pGlobals, 1);
717 if (pic_irq == pbCfg[0x62])
718 pic_level |= get_pci_irq_level(pGlobals, 2);
719 if (pic_irq == pbCfg[0x63])
720 pic_level |= get_pci_irq_level(pGlobals, 3);
721 if (pic_irq == pGlobals->acpi_irq)
722 pic_level |= pGlobals->acpi_irq_level;
723
724 Log3(("pciSetIrq: %s: iLevel=%d iIrq=%d pic_irq=%d pic_level=%d\n",
725 R3STRING(pPciDev->name), iLevel, iIrq, pic_irq, pic_level));
726 pBus->CTX_SUFF(pPciHlp)->pfnIsaSetIrq(pBus->CTX_SUFF(pDevIns), pic_irq, pic_level);
727
728 /** @todo optimize pci irq flip-flop some rainy day. */
729 if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP)
730 pciSetIrqInternal(pGlobals, uDevFn, pPciDev, iIrq, PDM_IRQ_LEVEL_LOW);
731 }
732}
733
734/**
735 * Set the IRQ for a PCI device on the host bus.
736 *
737 * @param pDevIns Device instance of the PCI Bus.
738 * @param pPciDev The PCI device structure.
739 * @param iIrq IRQ number to set.
740 * @param iLevel IRQ level.
741 */
742PDMBOTHCBDECL(void) pciSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel)
743{
744 pciSetIrqInternal(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), pPciDev->devfn, pPciDev, iIrq, iLevel);
745}
746
747#ifdef IN_RING3
748
749/**
750 * Finds a bridge on the bus which contains the destination bus.
751 *
752 * @return Pointer to the device instance data of the bus or
753 * NULL if no bridge was found.
754 * @param pBus Pointer to the bus to search on.
755 * @param iBus Destination bus number.
756 */
757DECLINLINE(PPCIDEVICE) pciFindBridge(PPCIBUS pBus, uint8_t iBus)
758{
759 /* Search for a fitting bridge. */
760 for (uint32_t iBridge = 0; iBridge < pBus->cBridges; iBridge++)
761 {
762 /*
763 * Examine secondary and subordinate bus number.
764 * If the target bus is in the range we pass the request on to the bridge.
765 */
766 PPCIDEVICE pBridgeTemp = pBus->papBridgesR3[iBridge];
767 AssertMsg(pBridgeTemp && pBridgeTemp->Int.s.fPciToPciBridge,
768 ("Device is not a PCI bridge but on the list of PCI bridges\n"));
769
770 if ( iBus >= pBridgeTemp->config[VBOX_PCI_SECONDARY_BUS]
771 && iBus <= pBridgeTemp->config[VBOX_PCI_SUBORDINATE_BUS])
772 return pBridgeTemp;
773 }
774
775 /* Nothing found. */
776 return NULL;
777}
778
779static void piix3_reset(PIIX3State *d)
780{
781 uint8_t *pci_conf = d->dev.config;
782
783 pci_conf[0x04] = 0x07; /* master, memory and I/O */
784 pci_conf[0x05] = 0x00;
785 pci_conf[0x06] = 0x00;
786 pci_conf[0x07] = 0x02; /* PCI_status_devsel_medium */
787 pci_conf[0x4c] = 0x4d;
788 pci_conf[0x4e] = 0x03;
789 pci_conf[0x4f] = 0x00;
790 pci_conf[0x60] = 0x80;
791 pci_conf[0x69] = 0x02;
792 pci_conf[0x70] = 0x80;
793 pci_conf[0x76] = 0x0c;
794 pci_conf[0x77] = 0x0c;
795 pci_conf[0x78] = 0x02;
796 pci_conf[0x79] = 0x00;
797 pci_conf[0x80] = 0x00;
798 pci_conf[0x82] = 0x02; /* Get rid of the Linux guest "Enabling Passive Release" PCI quirk warning. */
799 pci_conf[0xa0] = 0x08;
800 pci_conf[0xa0] = 0x08;
801 pci_conf[0xa2] = 0x00;
802 pci_conf[0xa3] = 0x00;
803 pci_conf[0xa4] = 0x00;
804 pci_conf[0xa5] = 0x00;
805 pci_conf[0xa6] = 0x00;
806 pci_conf[0xa7] = 0x00;
807 pci_conf[0xa8] = 0x0f;
808 pci_conf[0xaa] = 0x00;
809 pci_conf[0xab] = 0x00;
810 pci_conf[0xac] = 0x00;
811 pci_conf[0xae] = 0x00;
812}
813
814static void pci_config_writel(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t val)
815{
816 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
817 (uDevFn << 8) | addr;
818 pci_data_write(pGlobals, 0, val, 4);
819}
820
821static void pci_config_writew(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t val)
822{
823 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
824 (uDevFn << 8) | (addr & ~3);
825 pci_data_write(pGlobals, addr & 3, val, 2);
826}
827
828static void pci_config_writeb(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t val)
829{
830 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
831 (uDevFn << 8) | (addr & ~3);
832 pci_data_write(pGlobals, addr & 3, val, 1);
833}
834
835static uint32_t pci_config_readl(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr)
836{
837 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
838 (uDevFn << 8) | addr;
839 uint32_t u32Val;
840 int rc = pci_data_read(pGlobals, 0, 4, &u32Val);
841 AssertRC(rc);
842 return u32Val;
843}
844
845static uint32_t pci_config_readw(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr)
846{
847 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
848 (uDevFn << 8) | (addr & ~3);
849 uint32_t u32Val;
850 int rc = pci_data_read(pGlobals, addr & 3, 2, &u32Val);
851 AssertRC(rc);
852 return u32Val;
853}
854
855static uint32_t pci_config_readb(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr)
856{
857 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
858 (uDevFn << 8) | (addr & ~3);
859 uint32_t u32Val;
860 int rc = pci_data_read(pGlobals, addr & 3, 1, &u32Val);
861 AssertRC(rc);
862 return u32Val;
863}
864
865/* host irqs corresponding to PCI irqs A-D */
866static const uint8_t pci_irqs[4] = { 11, 9, 11, 9 }; /* bird: added const */
867
868static void pci_set_io_region_addr(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, int region_num, uint32_t addr)
869{
870 uint16_t cmd;
871 uint32_t ofs;
872
873 if ( region_num == PCI_ROM_SLOT )
874 ofs = 0x30;
875 else
876 ofs = 0x10 + region_num * 4;
877
878 /* Read memory type first. */
879 uint8_t uRessourceType = pci_config_readb(pGlobals, uBus, uDevFn, ofs);
880
881 /* Read command register. */
882 cmd = pci_config_readw(pGlobals, uBus, uDevFn, PCI_COMMAND);
883 if ( region_num == PCI_ROM_SLOT )
884 cmd |= 2;
885 else if ((uRessourceType & 0x01) == 1) /* Test if region is I/O space. */
886 cmd |= 1; /* Enable I/O space access. */
887 else /* The region is MMIO. */
888 cmd |= 2; /* Enable MMIO access. */
889
890 /* Write address of the device. */
891 pci_config_writel(pGlobals, uBus, uDevFn, ofs, addr);
892
893 /* enable memory mappings */
894 pci_config_writew(pGlobals, uBus, uDevFn, PCI_COMMAND, cmd);
895}
896
897static void pci_bios_init_device(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint8_t cBridgeDepth, uint8_t *paBridgePositions)
898{
899 uint32_t *paddr;
900 int i, pin, pic_irq;
901 uint16_t devclass, vendor_id, device_id;
902
903 devclass = pci_config_readw(pGlobals, uBus, uDevFn, PCI_CLASS_DEVICE);
904 vendor_id = pci_config_readw(pGlobals, uBus, uDevFn, PCI_VENDOR_ID);
905 device_id = pci_config_readw(pGlobals, uBus, uDevFn, PCI_DEVICE_ID);
906
907 /* Check if device is present. */
908 if (vendor_id != 0xffff)
909 {
910 switch(devclass)
911 {
912 case 0x0101:
913 if ( (vendor_id == 0x8086)
914 && (device_id == 0x7010 || device_id == 0x7111 || device_id == 0x269e))
915 {
916 /* PIIX3, PIIX4 or ICH6 IDE */
917 pci_config_writew(pGlobals, uBus, uDevFn, 0x40, 0x8000); /* enable IDE0 */
918 pci_config_writew(pGlobals, uBus, uDevFn, 0x42, 0x8000); /* enable IDE1 */
919 goto default_map;
920 }
921 else
922 {
923 /* IDE: we map it as in ISA mode */
924 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 0, 0x1f0);
925 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 1, 0x3f4);
926 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 2, 0x170);
927 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 3, 0x374);
928 }
929 break;
930 case 0x0300:
931 if (vendor_id != 0x80ee)
932 goto default_map;
933 /* VGA: map frame buffer to default Bochs VBE address */
934 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 0, 0xE0000000);
935#ifdef VBOX_WITH_EFI
936 /* The following is necessary for the VGA check in
937 PciVgaMiniPortDriverBindingSupported to succeed. */
938 /** @todo Seems we're missing some I/O registers or something on the VGA device... Compare real/virt hw with lspci. */
939 pci_config_writew(pGlobals, uBus, uDevFn, PCI_COMMAND,
940 pci_config_readw(pGlobals, uBus, uDevFn, PCI_COMMAND)
941 | 1 /* Enable I/O space access. */);
942#endif
943 break;
944 case 0x0800:
945 /* PIC */
946 vendor_id = pci_config_readw(pGlobals, uBus, uDevFn, PCI_VENDOR_ID);
947 device_id = pci_config_readw(pGlobals, uBus, uDevFn, PCI_DEVICE_ID);
948 if (vendor_id == 0x1014)
949 {
950 /* IBM */
951 if (device_id == 0x0046 || device_id == 0xFFFF)
952 {
953 /* MPIC & MPIC2 */
954 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 0, 0x80800000 + 0x00040000);
955 }
956 }
957 break;
958 case 0xff00:
959 if ( (vendor_id == 0x0106b)
960 && (device_id == 0x0017 || device_id == 0x0022))
961 {
962 /* macio bridge */
963 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 0, 0x80800000);
964 }
965 break;
966 case 0x0604:
967 {
968 /* Init PCI-to-PCI bridge. */
969 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_PRIMARY_BUS, uBus);
970
971 AssertMsg(pGlobals->uBus < 255, ("Too many bridges on the bus\n"));
972 pGlobals->uBus++;
973 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_SECONDARY_BUS, pGlobals->uBus);
974 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_SUBORDINATE_BUS, 0xff); /* Temporary until we know how many other bridges are behind this one. */
975
976 /* Add position of this bridge into the array. */
977 paBridgePositions[cBridgeDepth+1] = (uDevFn >> 3);
978
979 /*
980 * The I/O range for the bridge must be aligned to a 4KB boundary.
981 * This does not change anything really as the access to the device is not going
982 * through the bridge but we want to be compliant to the spec.
983 */
984 if ((pGlobals->pci_bios_io_addr % 4096) != 0)
985 pGlobals->pci_bios_io_addr = RT_ALIGN_32(pGlobals->pci_bios_io_addr, 4*1024);
986 Log(("%s: Aligned I/O start address. New address %#x\n", __FUNCTION__, pGlobals->pci_bios_io_addr));
987 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_IO_BASE, (pGlobals->pci_bios_io_addr >> 8) & 0xf0);
988
989 /* The MMIO range for the bridge must be aligned to a 1MB boundary. */
990 if ((pGlobals->pci_bios_mem_addr % (1024 * 1024)) != 0)
991 pGlobals->pci_bios_mem_addr = RT_ALIGN_32(pGlobals->pci_bios_mem_addr, 1024*1024);
992 Log(("%s: Aligned MMIO start address. New address %#x\n", __FUNCTION__, pGlobals->pci_bios_mem_addr));
993 pci_config_writew(pGlobals, uBus, uDevFn, VBOX_PCI_MEMORY_BASE, (pGlobals->pci_bios_mem_addr >> 16) & UINT32_C(0xffff0));
994
995 /* Save values to compare later to. */
996 uint32_t u32IoAddressBase = pGlobals->pci_bios_io_addr;
997 uint32_t u32MMIOAddressBase = pGlobals->pci_bios_mem_addr;
998
999 /* Init devices behind the bridge and possibly other bridges as well. */
1000 for (int i = 0; i <= 255; i++)
1001 pci_bios_init_device(pGlobals, uBus + 1, i, cBridgeDepth + 1, paBridgePositions);
1002
1003 /* The number of bridges behind the this one is now available. */
1004 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_SUBORDINATE_BUS, pGlobals->uBus);
1005
1006 /*
1007 * Set I/O limit register. If there is no device with I/O space behind the bridge
1008 * we set a lower value than in the base register.
1009 * The result with a real bridge is that no I/O transactions are passed to the secondary
1010 * interface. Again this doesn't really matter here but we want to be compliant to the spec.
1011 */
1012 if ((u32IoAddressBase != pGlobals->pci_bios_io_addr) && ((pGlobals->pci_bios_io_addr % 4096) != 0))
1013 {
1014 /* The upper boundary must be one byte less than a 4KB boundary. */
1015 pGlobals->pci_bios_io_addr = RT_ALIGN_32(pGlobals->pci_bios_io_addr, 4*1024);
1016 }
1017 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_IO_LIMIT, ((pGlobals->pci_bios_io_addr >> 8) & 0xf0) - 1);
1018
1019 /* Same with the MMIO limit register but with 1MB boundary here. */
1020 if ((u32MMIOAddressBase != pGlobals->pci_bios_mem_addr) && ((pGlobals->pci_bios_mem_addr % (1024 * 1024)) != 0))
1021 {
1022 /* The upper boundary must be one byte less than a 1MB boundary. */
1023 pGlobals->pci_bios_mem_addr = RT_ALIGN_32(pGlobals->pci_bios_mem_addr, 1024*1024);
1024 }
1025 pci_config_writew(pGlobals, uBus, uDevFn, VBOX_PCI_MEMORY_LIMIT, ((pGlobals->pci_bios_mem_addr >> 16) & UINT32_C(0xfff0)) - 1);
1026
1027 /*
1028 * Set the prefetch base and limit registers. We currently have no device with a prefetchable region
1029 * which may be behind a bridge. Thatswhy it is unconditionally disabled here atm by writing a higher value into
1030 * the base register than in the limit register.
1031 */
1032 pci_config_writew(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_MEMORY_BASE, 0xfff0);
1033 pci_config_writew(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_MEMORY_LIMIT, 0x0);
1034 pci_config_writel(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_BASE_UPPER32, 0x00);
1035 pci_config_writel(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_LIMIT_UPPER32, 0x00);
1036 break;
1037 }
1038 default:
1039 default_map:
1040 {
1041 /* default memory mappings */
1042 /*
1043 * PCI_NUM_REGIONS is 7 because of the rom region but there are only 6 base address register defined by the PCI spec.
1044 * Leaving only PCI_NUM_REGIONS would cause reading another and enabling a memory region which does not exist.
1045 */
1046 for(i = 0; i < (PCI_NUM_REGIONS-1); i++)
1047 {
1048 uint32_t u32Size;
1049 uint8_t u8RessourceType;
1050 uint32_t u32Address = 0x10 + i * 4;
1051
1052 /* Calculate size. */
1053 u8RessourceType = pci_config_readb(pGlobals, uBus, uDevFn, u32Address);
1054 pci_config_writel(pGlobals, uBus, uDevFn, u32Address, UINT32_C(0xffffffff));
1055 u32Size = pci_config_readl(pGlobals, uBus, uDevFn, u32Address);
1056 /* Clear ressource information depending on ressource type. */
1057 if ((u8RessourceType & 0x01) == 1) /* I/O */
1058 u32Size &= ~(0x01);
1059 else /* MMIO */
1060 u32Size &= ~(0x0f);
1061
1062 /*
1063 * Invert all bits and add 1 to get size of the region.
1064 * (From PCI implementation note)
1065 */
1066 if (((u8RessourceType & 0x01) == 1) && (u32Size & UINT32_C(0xffff0000)) == 0)
1067 u32Size = (~(u32Size | UINT32_C(0xffff0000))) + 1;
1068 else
1069 u32Size = (~u32Size) + 1;
1070
1071 Log(("%s: Size of region %u for device %d on bus %d is %u\n", __FUNCTION__, i, uDevFn, uBus, u32Size));
1072
1073 if (u32Size)
1074 {
1075 if ((u8RessourceType & 0x01) == 1)
1076 paddr = &pGlobals->pci_bios_io_addr;
1077 else
1078 paddr = &pGlobals->pci_bios_mem_addr;
1079 *paddr = (*paddr + u32Size - 1) & ~(u32Size - 1);
1080 Log(("%s: Start address of %s region %u is %#x\n", __FUNCTION__, ((u8RessourceType & 0x01) == 1 ? "I/O" : "MMIO"), i, *paddr));
1081 pci_set_io_region_addr(pGlobals, uBus, uDevFn, i, *paddr);
1082 *paddr += u32Size;
1083 Log(("%s: New address is %#x\n", __FUNCTION__, *paddr));
1084 }
1085 }
1086 break;
1087 }
1088 }
1089
1090 /* map the interrupt */
1091 pin = pci_config_readb(pGlobals, uBus, uDevFn, PCI_INTERRUPT_PIN);
1092 if (pin != 0)
1093 {
1094 uint8_t uBridgeDevFn = uDevFn;
1095 pin--;
1096
1097 /* We need to go up to the host bus to see which irq this device will assert there. */
1098 while (cBridgeDepth != 0)
1099 {
1100 /* Get the pin the device would assert on the bridge. */
1101 pin = ((uBridgeDevFn >> 3) + pin) & 3;
1102 uBridgeDevFn = paBridgePositions[cBridgeDepth];
1103 cBridgeDepth--;
1104 }
1105
1106 pin = pci_slot_get_pirq(uDevFn, pin);
1107 pic_irq = pci_irqs[pin];
1108 pci_config_writeb(pGlobals, uBus, uDevFn, PCI_INTERRUPT_LINE, pic_irq);
1109 }
1110 }
1111}
1112
1113#endif /* IN_RING3 */
1114
1115/* -=-=-=-=-=- wrappers -=-=-=-=-=- */
1116
1117/**
1118 * Port I/O Handler for PCI address OUT operations.
1119 *
1120 * @returns VBox status code.
1121 *
1122 * @param pDevIns The device instance.
1123 * @param pvUser User argument - ignored.
1124 * @param uPort Port number used for the IN operation.
1125 * @param u32 The value to output.
1126 * @param cb The value size in bytes.
1127 */
1128PDMBOTHCBDECL(int) pciIOPortAddressWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
1129{
1130 Log(("pciIOPortAddressWrite: Port=%#x u32=%#x cb=%d\n", Port, u32, cb));
1131 NOREF(pvUser);
1132 if (cb == 4)
1133 {
1134 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1135 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_WRITE);
1136 pThis->uConfigReg = u32;
1137 PCI_UNLOCK(pDevIns);
1138 }
1139 /* else: 440FX does "pass through to the bus" for other writes, what ever that means.
1140 * Linux probes for cmd640 using byte writes/reads during ide init. We'll just ignore it. */
1141 return VINF_SUCCESS;
1142}
1143
1144
1145/**
1146 * Port I/O Handler for PCI address IN operations.
1147 *
1148 * @returns VBox status code.
1149 *
1150 * @param pDevIns The device instance.
1151 * @param pvUser User argument - ignored.
1152 * @param uPort Port number used for the IN operation.
1153 * @param pu32 Where to store the result.
1154 * @param cb Number of bytes read.
1155 */
1156PDMBOTHCBDECL(int) pciIOPortAddressRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
1157{
1158 NOREF(pvUser);
1159 if (cb == 4)
1160 {
1161 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1162 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_READ);
1163 *pu32 = pThis->uConfigReg;
1164 PCI_UNLOCK(pDevIns);
1165 Log(("pciIOPortAddressRead: Port=%#x cb=%d -> %#x\n", Port, cb, *pu32));
1166 return VINF_SUCCESS;
1167 }
1168 /* else: 440FX does "pass through to the bus" for other writes, what ever that means.
1169 * Linux probes for cmd640 using byte writes/reads during ide init. We'll just ignore it. */
1170 Log(("pciIOPortAddressRead: Port=%#x cb=%d VERR_IOM_IOPORT_UNUSED\n", Port, cb));
1171 return VERR_IOM_IOPORT_UNUSED;
1172}
1173
1174
1175/**
1176 * Port I/O Handler for PCI data OUT operations.
1177 *
1178 * @returns VBox status code.
1179 *
1180 * @param pDevIns The device instance.
1181 * @param pvUser User argument - ignored.
1182 * @param uPort Port number used for the IN operation.
1183 * @param u32 The value to output.
1184 * @param cb The value size in bytes.
1185 */
1186PDMBOTHCBDECL(int) pciIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
1187{
1188 Log(("pciIOPortDataWrite: Port=%#x u32=%#x cb=%d\n", Port, u32, cb));
1189 NOREF(pvUser);
1190 int rc = VINF_SUCCESS;
1191 if (!(Port % cb))
1192 {
1193 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_WRITE);
1194 rc = pci_data_write(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), Port, u32, cb);
1195 PCI_UNLOCK(pDevIns);
1196 }
1197 else
1198 AssertMsgFailed(("Write to port %#x u32=%#x cb=%d\n", Port, u32, cb));
1199 return rc;
1200}
1201
1202
1203/**
1204 * Port I/O Handler for PCI data IN operations.
1205 *
1206 * @returns VBox status code.
1207 *
1208 * @param pDevIns The device instance.
1209 * @param pvUser User argument - ignored.
1210 * @param uPort Port number used for the IN operation.
1211 * @param pu32 Where to store the result.
1212 * @param cb Number of bytes read.
1213 */
1214PDMBOTHCBDECL(int) pciIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
1215{
1216 NOREF(pvUser);
1217 if (!(Port % cb))
1218 {
1219 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_READ);
1220 int rc = pci_data_read(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), Port, cb, pu32);
1221 PCI_UNLOCK(pDevIns);
1222 Log(("pciIOPortDataRead: Port=%#x cb=%#x -> %#x (%Rrc)\n", Port, cb, *pu32, rc));
1223 return rc;
1224 }
1225 AssertMsgFailed(("Read from port %#x cb=%d\n", Port, cb));
1226 return VERR_IOM_IOPORT_UNUSED;
1227}
1228
1229#ifdef IN_RING3
1230
1231/**
1232 * Saves a state of the PCI device.
1233 *
1234 * @returns VBox status code.
1235 * @param pDevIns Device instance of the PCI Bus.
1236 * @param pPciDev Pointer to PCI device.
1237 * @param pSSMHandle The handle to save the state to.
1238 */
1239static DECLCALLBACK(int) pciGenericSaveExec(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PSSMHANDLE pSSMHandle)
1240{
1241 return SSMR3PutMem(pSSMHandle, &pPciDev->config[0], sizeof(pPciDev->config));
1242}
1243
1244
1245/**
1246 * Loads a saved PCI device state.
1247 *
1248 * @returns VBox status code.
1249 * @param pDevIns Device instance of the PCI Bus.
1250 * @param pPciDev Pointer to PCI device.
1251 * @param pSSMHandle The handle to the saved state.
1252 */
1253static DECLCALLBACK(int) pciGenericLoadExec(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PSSMHANDLE pSSMHandle)
1254{
1255 return SSMR3GetMem(pSSMHandle, &pPciDev->config[0], sizeof(pPciDev->config));
1256}
1257
1258
1259/**
1260 * Saves a state of the PCI device.
1261 *
1262 * @returns VBox status code.
1263 * @param pDevIns The device instance.
1264 * @param pPciDev Pointer to PCI device.
1265 * @param pSSMHandle The handle to save the state to.
1266 */
1267static DECLCALLBACK(int) pciR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1268{
1269 uint32_t i;
1270 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1271 PPCIBUS pBus = &pThis->PciBus;
1272
1273 /*
1274 * Bus state data.
1275 */
1276 SSMR3PutU32(pSSMHandle, pThis->uConfigReg);
1277 SSMR3PutBool(pSSMHandle, pThis->fUseIoApic);
1278
1279 /*
1280 * Save IRQ states.
1281 */
1282 for (i = 0; i < PCI_IRQ_PINS; i++)
1283 SSMR3PutU32(pSSMHandle, pThis->pci_irq_levels[i]);
1284 for (i = 0; i < PCI_APIC_IRQ_PINS; i++)
1285 SSMR3PutU32(pSSMHandle, pThis->pci_apic_irq_levels[i]);
1286
1287 SSMR3PutU32(pSSMHandle, pThis->acpi_irq_level);
1288 SSMR3PutS32(pSSMHandle, pThis->acpi_irq);
1289
1290 SSMR3PutU32(pSSMHandle, ~0); /* separator */
1291
1292 /*
1293 * Iterate thru all the devices.
1294 */
1295 for (i = 0; i < RT_ELEMENTS(pBus->devices); i++)
1296 {
1297 PPCIDEVICE pDev = pBus->devices[i];
1298 if (pDev)
1299 {
1300 int rc;
1301 SSMR3PutU32(pSSMHandle, i);
1302 SSMR3PutMem(pSSMHandle, pDev->config, sizeof(pDev->config));
1303
1304 rc = SSMR3PutS32(pSSMHandle, pDev->Int.s.uIrqPinState);
1305 if (RT_FAILURE(rc))
1306 return rc;
1307 }
1308 }
1309 return SSMR3PutU32(pSSMHandle, ~0); /* terminator */
1310}
1311
1312
1313/**
1314 * Disables all PCI devices prior to state loading.
1315 *
1316 * @returns VINF_SUCCESS.
1317 * @param pBus The PCI bus instance.
1318 */
1319static int pciR3CommonLoadPrep(PPCIBUS pBus)
1320{
1321 /*
1322 * Iterate thru all the devices and write 0 to the COMMAND register.
1323 * The register value is restored afterwards so we can do proper
1324 * LogRels in pciR3CommonRestoreConfig.
1325 */
1326 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->devices); i++)
1327 {
1328 PPCIDEVICE pDev = pBus->devices[i];
1329 if (pDev)
1330 {
1331 uint16_t u16 = PCIDevGetCommand(pDev);
1332 pDev->Int.s.pfnConfigWrite(pDev, VBOX_PCI_COMMAND, 0, 2);
1333 PCIDevSetCommand(pDev, u16);
1334 Assert(PCIDevGetCommand(pDev) == u16);
1335 }
1336 }
1337 return VINF_SUCCESS;
1338}
1339
1340
1341/**
1342 * Prepares a state load.
1343 *
1344 * This will disable all the device so that the I/O regions gets unmapped.
1345 *
1346 * @returns VINF_SUCCESS
1347 * @param pDevIns The device instance.
1348 * @param pSSMHandle The saved state handle.
1349 */
1350static DECLCALLBACK(int) pciR3LoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1351{
1352 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1353 return pciR3CommonLoadPrep(&pThis->PciBus);
1354}
1355
1356
1357/**
1358 * Common routine for restoring the config registers of a PCI device.
1359 *
1360 * @param pDev The PCI device.
1361 * @param pbSrcConfig The configuration register values to be loaded.
1362 * @param fIsBridge Whether this is a bridge device or not.
1363 */
1364static void pciR3CommonRestoreConfig(PPCIDEVICE pDev, uint8_t const *pbSrcConfig, bool fIsBridge)
1365{
1366 /*
1367 * This table defines the fields for normal devices and bridge devices, and
1368 * the order in which they need to be restored.
1369 */
1370 static const struct PciField
1371 {
1372 uint8_t off;
1373 uint8_t cb;
1374 uint8_t fWritable;
1375 uint8_t fBridge;
1376 const char *pszName;
1377 } s_aFields[] =
1378 {
1379 /* off,cb,fW,fB, pszName */
1380 { 0x00, 2, 0, 3, "VENDOR_ID" },
1381 { 0x02, 2, 0, 3, "DEVICE_ID" },
1382 { 0x06, 2, 1, 3, "STATUS" },
1383 { 0x08, 1, 0, 3, "REVISION_ID" },
1384 { 0x09, 1, 0, 3, "CLASS_PROG" },
1385 { 0x0a, 1, 0, 3, "CLASS_SUB" },
1386 { 0x0b, 1, 0, 3, "CLASS_BASE" },
1387 { 0x0c, 1, 0, 3, "CACHE_LINE_SIZE" }, // fWritable = ??
1388 { 0x0d, 1, 0, 3, "LATENCY_TIMER" }, // fWritable = ??
1389 { 0x0e, 1, 0, 3, "HEADER_TYPE" }, // fWritable = ??
1390 { 0x0f, 1, 0, 3, "BIST" }, // fWritable = ??
1391 { 0x10, 4, 1, 3, "BASE_ADDRESS_0" },
1392 { 0x14, 4, 1, 3, "BASE_ADDRESS_1" },
1393 { 0x18, 4, 1, 1, "BASE_ADDRESS_2" },
1394 { 0x18, 1, 1, 2, "PRIMARY_BUS" }, // fWritable = ??
1395 { 0x19, 1, 1, 2, "SECONDARY_BUS" }, // fWritable = ??
1396 { 0x1a, 1, 1, 2, "SUBORDINATE_BUS" }, // fWritable = ??
1397 { 0x1b, 1, 1, 2, "SEC_LATENCY_TIMER" }, // fWritable = ??
1398 { 0x1c, 4, 1, 1, "BASE_ADDRESS_3" },
1399 { 0x1c, 1, 1, 2, "IO_BASE" }, // fWritable = ??
1400 { 0x1d, 1, 1, 2, "IO_LIMIT" }, // fWritable = ??
1401 { 0x1e, 2, 1, 2, "SEC_STATUS" }, // fWritable = ??
1402 { 0x20, 4, 1, 1, "BASE_ADDRESS_4" },
1403 { 0x20, 2, 1, 2, "MEMORY_BASE" }, // fWritable = ??
1404 { 0x22, 2, 1, 2, "MEMORY_LIMIT" }, // fWritable = ??
1405 { 0x24, 4, 1, 1, "BASE_ADDRESS_4" },
1406 { 0x24, 2, 1, 2, "PREF_MEMORY_BASE" }, // fWritable = ??
1407 { 0x26, 2, 1, 2, "PREF_MEMORY_LIMIT" }, // fWritable = ??
1408 { 0x28, 4, 1, 1, "CARDBUS_CIS" }, // fWritable = ??
1409 { 0x28, 4, 1, 2, "PREF_BASE_UPPER32" }, // fWritable = ??
1410 { 0x2c, 2, 0, 1, "SUBSYSTEM_VENDOR_ID" },// fWritable = !?
1411 { 0x2c, 4, 1, 2, "PREF_LIMIT_UPPER32" },// fWritable = ??
1412 { 0x2e, 2, 0, 1, "SUBSYSTEM_ID" }, // fWritable = !?
1413 { 0x30, 4, 1, 1, "ROM_ADDRESS" }, // fWritable = ?!
1414 { 0x30, 2, 1, 2, "IO_BASE_UPPER16" }, // fWritable = ?!
1415 { 0x32, 2, 1, 2, "IO_LIMIT_UPPER16" }, // fWritable = ?!
1416 { 0x34, 4, 0, 3, "CAPABILITY_LIST" }, // fWritable = !? cb=!?
1417 { 0x38, 4, 1, 1, "???" }, // ???
1418 { 0x38, 4, 1, 2, "ROM_ADDRESS_BR" }, // fWritable = !? cb=!? fBridge=!?
1419 { 0x3c, 1, 1, 3, "INTERRUPT_LINE" }, // fBridge=??
1420 { 0x3d, 1, 0, 3, "INTERRUPT_PIN" }, // fBridge=??
1421 { 0x3e, 1, 0, 1, "MIN_GNT" }, // fWritable = !?
1422 { 0x3e, 1, 1, 2, "BRIDGE_CONTROL" }, // fWritable = !? cb=!?
1423 { 0x3f, 1, 1, 3, "MAX_LAT" }, // fWritable = !? fBridge=!?
1424 /* The COMMAND register must come last as it requires the *ADDRESS*
1425 registers to be restored before we pretent to change it from 0 to
1426 whatever value the guest assigned it. */
1427 { 0x04, 2, 1, 3, "COMMAND" },
1428 };
1429
1430#ifdef RT_STRICT
1431 /* Check that we've got full register coverage. */
1432 uint32_t bmDevice[0x40 / 32];
1433 uint32_t bmBridge[0x40 / 32];
1434 RT_ZERO(bmDevice);
1435 RT_ZERO(bmBridge);
1436 for (uint32_t i = 0; i < RT_ELEMENTS(s_aFields); i++)
1437 {
1438 uint8_t off = s_aFields[i].off;
1439 uint8_t cb = s_aFields[i].cb;
1440 uint8_t f = s_aFields[i].fBridge;
1441 while (cb-- > 0)
1442 {
1443 if (f & 1) AssertMsg(!ASMBitTest(bmDevice, off), ("%#x\n", off));
1444 if (f & 2) AssertMsg(!ASMBitTest(bmBridge, off), ("%#x\n", off));
1445 if (f & 1) ASMBitSet(bmDevice, off);
1446 if (f & 2) ASMBitSet(bmBridge, off);
1447 off++;
1448 }
1449 }
1450 for (uint32_t off = 0; off < 0x40; off++)
1451 {
1452 AssertMsg(ASMBitTest(bmDevice, off), ("%#x\n", off));
1453 AssertMsg(ASMBitTest(bmBridge, off), ("%#x\n", off));
1454 }
1455#endif
1456
1457 /*
1458 * Loop thru the fields covering the 64 bytes of standard registers.
1459 */
1460 uint8_t const fBridge = fIsBridge ? 2 : 1;
1461 uint8_t *pbDstConfig = &pDev->config[0];
1462 for (uint32_t i = 0; i < RT_ELEMENTS(s_aFields); i++)
1463 if (s_aFields[i].fBridge & fBridge)
1464 {
1465 uint8_t const off = s_aFields[i].off;
1466 uint8_t const cb = s_aFields[i].cb;
1467 uint32_t u32Src;
1468 uint32_t u32Dst;
1469 switch (cb)
1470 {
1471 case 1:
1472 u32Src = pbSrcConfig[off];
1473 u32Dst = pbDstConfig[off];
1474 break;
1475 case 2:
1476 u32Src = *(uint16_t const *)&pbSrcConfig[off];
1477 u32Dst = *(uint16_t const *)&pbDstConfig[off];
1478 break;
1479 case 4:
1480 u32Src = *(uint32_t const *)&pbSrcConfig[off];
1481 u32Dst = *(uint32_t const *)&pbDstConfig[off];
1482 break;
1483 default:
1484 AssertFailed();
1485 continue;
1486 }
1487
1488 if ( u32Src != u32Dst
1489 || off == VBOX_PCI_COMMAND)
1490 {
1491 if (u32Src != u32Dst)
1492 {
1493 if (!s_aFields[i].fWritable)
1494 LogRel(("PCI: %8s/%u: %2u-bit field %s: %x -> %x - !READ ONLY!\n",
1495 pDev->name, pDev->pDevIns->iInstance, cb*8, s_aFields[i].pszName, u32Dst, u32Src));
1496 else
1497 LogRel(("PCI: %8s/%u: %2u-bit field %s: %x -> %x\n",
1498 pDev->name, pDev->pDevIns->iInstance, cb*8, s_aFields[i].pszName, u32Dst, u32Src));
1499 }
1500 if (off == VBOX_PCI_COMMAND)
1501 PCIDevSetCommand(pDev, 0); /* For remapping, see pciR3CommonLoadPrep. */
1502 pDev->Int.s.pfnConfigWrite(pDev, off, u32Src, cb);
1503 }
1504 }
1505
1506 /*
1507 * The device dependent registers.
1508 *
1509 * We will not use ConfigWrite here as we have no clue about the size
1510 * of the registers, so the device is responsible for correctly
1511 * restoring functionality governed by these registers.
1512 */
1513 for (uint32_t off = 0x40; off < sizeof(pDev->config); off++)
1514 if (pbDstConfig[off] != pbSrcConfig[off])
1515 {
1516 LogRel(("PCI: %8s/%u: register %02x: %02x -> %02x\n",
1517 pDev->name, pDev->pDevIns->iInstance, off, pbDstConfig[off], pbSrcConfig[off])); /** @todo make this Log() later. */
1518 pbDstConfig[off] = pbSrcConfig[off];
1519 }
1520}
1521
1522
1523/**
1524 * Loads a saved PCI device state.
1525 *
1526 * @returns VBox status code.
1527 * @param pDevIns The device instance.
1528 * @param pSSMHandle The handle to the saved state.
1529 * @param u32Version The data unit version number.
1530 */
1531static DECLCALLBACK(int) pciR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
1532{
1533 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1534 PPCIBUS pBus = &pThis->PciBus;
1535 uint32_t u32;
1536 uint32_t i;
1537 int rc;
1538
1539 /*
1540 * Check the version.
1541 */
1542 if (u32Version > VBOX_PCI_SAVED_STATE_VERSION)
1543 {
1544 AssertFailed();
1545 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1546 }
1547
1548 /*
1549 * Bus state data.
1550 */
1551 SSMR3GetU32(pSSMHandle, &pThis->uConfigReg);
1552 if (u32Version > 1)
1553 SSMR3GetBool(pSSMHandle, &pThis->fUseIoApic);
1554
1555 /* Load IRQ states. */
1556 if (u32Version > 2)
1557 {
1558 for (uint8_t i = 0; i < PCI_IRQ_PINS; i++)
1559 SSMR3GetU32(pSSMHandle, (uint32_t *)&pThis->pci_irq_levels[i]);
1560 for (uint8_t i = 0; i < PCI_APIC_IRQ_PINS; i++)
1561 SSMR3GetU32(pSSMHandle, (uint32_t *)&pThis->pci_apic_irq_levels[i]);
1562
1563 SSMR3GetU32(pSSMHandle, &pThis->acpi_irq_level);
1564 SSMR3GetS32(pSSMHandle, &pThis->acpi_irq);
1565 }
1566
1567 /* separator */
1568 rc = SSMR3GetU32(pSSMHandle, &u32);
1569 if (RT_FAILURE(rc))
1570 return rc;
1571 if (u32 != (uint32_t)~0)
1572 AssertMsgFailedReturn(("u32=%#x\n", u32), rc);
1573
1574 /*
1575 * Iterate all the devices.
1576 */
1577 for (i = 0;; i++)
1578 {
1579 PCIDEVICE DevTmp;
1580 PPCIDEVICE pDev;
1581
1582 /* index / terminator */
1583 rc = SSMR3GetU32(pSSMHandle, &u32);
1584 if (RT_FAILURE(rc))
1585 return rc;
1586 if (u32 == (uint32_t)~0)
1587 break;
1588 if ( u32 >= RT_ELEMENTS(pBus->devices)
1589 || u32 < i)
1590 {
1591 AssertMsgFailed(("u32=%#x i=%#x\n", u32, i));
1592 return rc;
1593 }
1594
1595 /* skip forward to the device checking that no new devices are present. */
1596 for (; i < u32; i++)
1597 {
1598 if (pBus->devices[i])
1599 {
1600 LogRel(("New device in slot %#x, %s (vendor=%#06x device=%#06x)\n", i, pBus->devices[i]->name,
1601 PCIDevGetVendorId(pBus->devices[i]), PCIDevGetDeviceId(pBus->devices[i])));
1602 if (SSMR3HandleGetAfter(pSSMHandle) != SSMAFTER_DEBUG_IT)
1603 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
1604 }
1605 }
1606
1607 /* Get the data */
1608 DevTmp.Int.s.uIrqPinState = ~0; /* Invalid value in case we have an older saved state to force a state change in pciSetIrq. */
1609 SSMR3GetMem(pSSMHandle, DevTmp.config, sizeof(DevTmp.config));
1610 if (u32Version < 3)
1611 {
1612 int32_t i32Temp;
1613 /* Irq value not needed anymore. */
1614 rc = SSMR3GetS32(pSSMHandle, &i32Temp);
1615 if (RT_FAILURE(rc))
1616 return rc;
1617 }
1618 else
1619 {
1620 rc = SSMR3GetS32(pSSMHandle, &DevTmp.Int.s.uIrqPinState);
1621 if (RT_FAILURE(rc))
1622 return rc;
1623 }
1624
1625 /* check that it's still around. */
1626 pDev = pBus->devices[i];
1627 if (!pDev)
1628 {
1629 LogRel(("Device in slot %#x has been removed! vendor=%#06x device=%#06x\n", i,
1630 PCIDevGetVendorId(&DevTmp), PCIDevGetDeviceId(&DevTmp)));
1631 if (SSMR3HandleGetAfter(pSSMHandle) != SSMAFTER_DEBUG_IT)
1632 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
1633 continue;
1634 }
1635
1636 /* match the vendor id assuming that this will never be changed. */
1637 if ( DevTmp.config[0] != pDev->config[0]
1638 || DevTmp.config[1] != pDev->config[1])
1639 {
1640 LogRel(("Device in slot %#x (%s) vendor id mismatch! saved=%.4Rhxs current=%.4Rhxs\n",
1641 i, pDev->name, DevTmp.config, pDev->config));
1642 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
1643 }
1644
1645 /* commit the loaded device config. */
1646 pciR3CommonRestoreConfig(pDev, &DevTmp.config[0], false ); /** @todo fix bridge fun! */
1647
1648 pDev->Int.s.uIrqPinState = DevTmp.Int.s.uIrqPinState;
1649 }
1650
1651 return VINF_SUCCESS;
1652}
1653
1654
1655/* -=-=-=-=-=- real code -=-=-=-=-=- */
1656
1657/**
1658 * Registers the device with the specified PCI bus.
1659 *
1660 * @returns VBox status code.
1661 * @param pBus The bus to register with.
1662 * @param iDev The PCI device ordinal.
1663 * @param pPciDev The PCI device structure.
1664 * @param pszName Pointer to device name (permanent, readonly). For debugging, not unique.
1665 */
1666static int pciRegisterInternal(PPCIBUS pBus, int iDev, PPCIDEVICE pPciDev, const char *pszName)
1667{
1668 /*
1669 * Find device slot.
1670 */
1671 if (iDev < 0)
1672 {
1673 /*
1674 * Special check for the IDE controller which is our function 1 device
1675 * before searching.
1676 */
1677 if ( !strcmp(pszName, "piix3ide")
1678 && !pBus->devices[9])
1679 iDev = 9;
1680#ifdef VBOX_WITH_LPC
1681 /* LPC bus expected to be there by some guests, better make an additional argument to PDM
1682 device helpers, but requires significant rewrite */
1683 else if (!strcmp(pszName, "lpc")
1684 && !pBus->devices[0xf8])
1685 iDev = 0xf8;
1686#endif
1687 else
1688 {
1689 Assert(!(pBus->iDevSearch % 8));
1690 for (iDev = pBus->iDevSearch; iDev < (int)RT_ELEMENTS(pBus->devices); iDev += 8)
1691 if ( !pBus->devices[iDev]
1692 && !pBus->devices[iDev + 1]
1693 && !pBus->devices[iDev + 2]
1694 && !pBus->devices[iDev + 3]
1695 && !pBus->devices[iDev + 4]
1696 && !pBus->devices[iDev + 5]
1697 && !pBus->devices[iDev + 6]
1698 && !pBus->devices[iDev + 7])
1699 break;
1700 if (iDev >= (int)RT_ELEMENTS(pBus->devices))
1701 {
1702 AssertMsgFailed(("Couldn't find free spot!\n"));
1703 return VERR_PDM_TOO_PCI_MANY_DEVICES;
1704 }
1705 }
1706 pPciDev->Int.s.fRequestedDevFn = false;
1707 }
1708 else
1709 {
1710 /*
1711 * An explicit request.
1712 *
1713 * If the slot is occupied we'll have to relocate the device
1714 * currently occupying it first. This can only be done if the
1715 * existing device wasn't explicitly assigned. Also we limit
1716 * ourselves to function 0 devices.
1717 *
1718 * If you start setting devices + function in the
1719 * config, do it for all pci devices!
1720 */
1721 //AssertReleaseMsg(iDev > 8 || pBus->iBus != 0, ("iDev=%d pszName=%s\n", iDev, pszName));
1722 if (pBus->devices[iDev])
1723 {
1724 int iDevRel;
1725 AssertReleaseMsg(!(iDev % 8), ("PCI Configuration Conflict! iDev=%d pszName=%s clashes with %s\n",
1726 iDev, pszName, pBus->devices[iDev]->name));
1727 if ( pBus->devices[iDev]->Int.s.fRequestedDevFn
1728 || (pBus->devices[iDev + 1] && pBus->devices[iDev + 1]->Int.s.fRequestedDevFn)
1729 || (pBus->devices[iDev + 2] && pBus->devices[iDev + 2]->Int.s.fRequestedDevFn)
1730 || (pBus->devices[iDev + 3] && pBus->devices[iDev + 3]->Int.s.fRequestedDevFn)
1731 || (pBus->devices[iDev + 4] && pBus->devices[iDev + 4]->Int.s.fRequestedDevFn)
1732 || (pBus->devices[iDev + 5] && pBus->devices[iDev + 5]->Int.s.fRequestedDevFn)
1733 || (pBus->devices[iDev + 6] && pBus->devices[iDev + 6]->Int.s.fRequestedDevFn)
1734 || (pBus->devices[iDev + 7] && pBus->devices[iDev + 7]->Int.s.fRequestedDevFn))
1735 {
1736 AssertReleaseMsgFailed(("Configuration error:'%s' and '%s' are both configured as device %d\n",
1737 pszName, pBus->devices[iDev]->name, iDev));
1738 return VERR_INTERNAL_ERROR;
1739 }
1740
1741 /* Find free slot for the device(s) we're moving and move them. */
1742 for (iDevRel = pBus->iDevSearch; iDevRel < (int)RT_ELEMENTS(pBus->devices); iDevRel += 8)
1743 {
1744 if ( !pBus->devices[iDevRel]
1745 && !pBus->devices[iDevRel + 1]
1746 && !pBus->devices[iDevRel + 2]
1747 && !pBus->devices[iDevRel + 3]
1748 && !pBus->devices[iDevRel + 4]
1749 && !pBus->devices[iDevRel + 5]
1750 && !pBus->devices[iDevRel + 6]
1751 && !pBus->devices[iDevRel + 7])
1752 {
1753 int i = 0;
1754 for (i = 0; i < 8; i++)
1755 {
1756 if (!pBus->devices[iDev + i])
1757 continue;
1758 Log(("PCI: relocating '%s' from slot %#x to %#x\n", pBus->devices[iDev + i]->name, iDev + i, iDevRel + i));
1759 pBus->devices[iDevRel + i] = pBus->devices[iDev + i];
1760 pBus->devices[iDevRel + i]->devfn = i;
1761 pBus->devices[iDev + i] = NULL;
1762 }
1763 }
1764 }
1765 if (pBus->devices[iDev])
1766 {
1767 AssertMsgFailed(("Couldn't find free spot!\n"));
1768 return VERR_PDM_TOO_PCI_MANY_DEVICES;
1769 }
1770 } /* if conflict */
1771 pPciDev->Int.s.fRequestedDevFn = true;
1772 }
1773
1774 Assert(!pBus->devices[iDev]);
1775 pPciDev->devfn = iDev;
1776 pPciDev->name = pszName;
1777 pPciDev->Int.s.pBusR3 = pBus;
1778 pPciDev->Int.s.pBusR0 = MMHyperR3ToR0(PDMDevHlpGetVM(pBus->CTX_SUFF(pDevIns)), pBus);
1779 pPciDev->Int.s.pBusRC = MMHyperR3ToRC(PDMDevHlpGetVM(pBus->CTX_SUFF(pDevIns)), pBus);
1780 pPciDev->Int.s.pfnConfigRead = pci_default_read_config;
1781 pPciDev->Int.s.pfnConfigWrite = pci_default_write_config;
1782 pBus->devices[iDev] = pPciDev;
1783 if (pPciDev->Int.s.fPciToPciBridge)
1784 {
1785 AssertMsg(pBus->cBridges < RT_ELEMENTS(pBus->devices), ("Number of bridges exceeds the number of possible devices on the bus\n"));
1786 AssertMsg(pPciDev->Int.s.pfnBridgeConfigRead && pPciDev->Int.s.pfnBridgeConfigWrite,
1787 ("device is a bridge but does not implement read/write functions\n"));
1788 pBus->papBridgesR3[pBus->cBridges] = pPciDev;
1789 pBus->cBridges++;
1790 }
1791
1792 Log(("PCI: Registered device %d function %d (%#x) '%s'.\n",
1793 iDev >> 3, iDev & 7, 0x80000000 | (iDev << 8), pszName));
1794
1795 return VINF_SUCCESS;
1796}
1797
1798
1799/**
1800 * Registers the device with the default PCI bus.
1801 *
1802 * @returns VBox status code.
1803 * @param pDevIns Device instance of the PCI Bus.
1804 * @param pPciDev The PCI device structure.
1805 * Any PCI enabled device must keep this in it's instance data!
1806 * Fill in the PCI data config before registration, please.
1807 * @param pszName Pointer to device name (permanent, readonly). For debugging, not unique.
1808 * @param iDev The PCI device number. Use a negative value for auto assigning one.
1809 */
1810static DECLCALLBACK(int) pciRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, const char *pszName, int iDev)
1811{
1812 PPCIBUS pBus = DEVINS_2_PCIBUS(pDevIns);
1813
1814 /*
1815 * Check input.
1816 */
1817 if ( !pszName
1818 || !pPciDev
1819 || iDev >= (int)RT_ELEMENTS(pBus->devices)
1820 || (iDev >= 0 && iDev <= 8))
1821 {
1822 AssertMsgFailed(("Invalid argument! pszName=%s pPciDev=%p iDev=%d\n", pszName, pPciDev, iDev));
1823 return VERR_INVALID_PARAMETER;
1824 }
1825
1826 /*
1827 * Register the device.
1828 */
1829 return pciRegisterInternal(pBus, iDev, pPciDev, pszName);
1830}
1831
1832
1833static DECLCALLBACK(int) pciIORegionRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iRegion, uint32_t cbRegion, PCIADDRESSSPACE enmType, PFNPCIIOREGIONMAP pfnCallback)
1834{
1835 /*
1836 * Validate.
1837 */
1838 AssertMsgReturn( enmType == PCI_ADDRESS_SPACE_MEM
1839 || enmType == PCI_ADDRESS_SPACE_IO
1840 || enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH,
1841 ("Invalid enmType=%#x? Or was this a bitmask after all...\n", enmType),
1842 VERR_INVALID_PARAMETER);
1843 AssertMsgReturn((unsigned)iRegion < PCI_NUM_REGIONS,
1844 ("Invalid iRegion=%d PCI_NUM_REGIONS=%d\n", iRegion, PCI_NUM_REGIONS),
1845 VERR_INVALID_PARAMETER);
1846 int iLastSet = ASMBitLastSetU32(cbRegion);
1847 AssertMsgReturn( iLastSet != 0
1848 && RT_BIT_32(iLastSet - 1) == cbRegion,
1849 ("Invalid cbRegion=%#x iLastSet=%#x (not a power of 2 or 0)\n", cbRegion, iLastSet),
1850 VERR_INVALID_PARAMETER);
1851
1852 /*
1853 * Register the I/O region.
1854 */
1855 PPCIIOREGION pRegion = &pPciDev->Int.s.aIORegions[iRegion];
1856 pRegion->addr = ~0U;
1857 pRegion->size = cbRegion;
1858 pRegion->type = enmType;
1859 pRegion->map_func = pfnCallback;
1860
1861 /* Set type in the config space. */
1862 uint32_t u32Address = 0x10 + iRegion * 4;
1863 uint32_t u32Value = (enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH ? (1 << 3) : 0)
1864 | (enmType == PCI_ADDRESS_SPACE_IO ? 1 : 0);
1865 *(uint32_t *)(pPciDev->config + u32Address) = RT_H2LE_U32(u32Value);
1866
1867 return VINF_SUCCESS;
1868}
1869
1870
1871/**
1872 * @copydoc PDMPCIBUSREG::pfnSetConfigCallbacksR3
1873 */
1874static DECLCALLBACK(void) pciSetConfigCallbacks(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PFNPCICONFIGREAD pfnRead, PPFNPCICONFIGREAD ppfnReadOld,
1875 PFNPCICONFIGWRITE pfnWrite, PPFNPCICONFIGWRITE ppfnWriteOld)
1876{
1877 if (ppfnReadOld)
1878 *ppfnReadOld = pPciDev->Int.s.pfnConfigRead;
1879 pPciDev->Int.s.pfnConfigRead = pfnRead;
1880
1881 if (ppfnWriteOld)
1882 *ppfnWriteOld = pPciDev->Int.s.pfnConfigWrite;
1883 pPciDev->Int.s.pfnConfigWrite = pfnWrite;
1884}
1885
1886
1887/**
1888 * Called to perform the job of the bios.
1889 *
1890 * @returns VBox status.
1891 * @param pDevIns Device instance of the first bus.
1892 */
1893static DECLCALLBACK(int) pciFakePCIBIOS(PPDMDEVINS pDevIns)
1894{
1895 int rc;
1896 unsigned i;
1897 uint8_t elcr[2] = {0, 0};
1898 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1899 PVM pVM = PDMDevHlpGetVM(pDevIns);
1900 Assert(pVM);
1901
1902 /*
1903 * Set the start addresses.
1904 */
1905 pGlobals->pci_bios_io_addr = 0xd000;
1906 pGlobals->pci_bios_mem_addr = UINT32_C(0xf0000000);
1907 pGlobals->uBus = 0;
1908
1909 /*
1910 * Activate IRQ mappings.
1911 */
1912 for (i = 0; i < 4; i++)
1913 {
1914 uint8_t irq = pci_irqs[i];
1915 /* Set to trigger level. */
1916 elcr[irq >> 3] |= (1 << (irq & 7));
1917 /* Activate irq remapping in PIIX3. */
1918 pci_config_writeb(pGlobals, 0, pGlobals->PIIX3State.dev.devfn, 0x60 + i, irq);
1919 }
1920
1921 /* Tell to the PIC. */
1922 rc = IOMIOPortWrite(pVM, 0x4d0, elcr[0], sizeof(uint8_t));
1923 if (rc == VINF_SUCCESS)
1924 rc = IOMIOPortWrite(pVM, 0x4d1, elcr[1], sizeof(uint8_t));
1925 if (rc != VINF_SUCCESS)
1926 {
1927 AssertMsgFailed(("Writing to PIC failed!\n"));
1928 return RT_SUCCESS(rc) ? VERR_INTERNAL_ERROR : rc;
1929 }
1930
1931 /*
1932 * Init the devices.
1933 */
1934 for (i = 0; i < 256; i++)
1935 {
1936 uint8_t aBridgePositions[256];
1937
1938 memset(aBridgePositions, 0, sizeof(aBridgePositions));
1939 Log2(("PCI: Initializing device %d (%#x)\n",
1940 i, 0x80000000 | (i << 8)));
1941 pci_bios_init_device(pGlobals, 0, i, 0, aBridgePositions);
1942 }
1943
1944 return VINF_SUCCESS;
1945}
1946
1947/**
1948 * @copydoc FNPDMDEVRELOCATE
1949 */
1950static DECLCALLBACK(void) pciRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1951{
1952 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1953 PPCIBUS pBus = &pGlobals->PciBus;
1954 pGlobals->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1955
1956 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
1957 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1958
1959 /* Relocate RC pointers for the attached pci devices. */
1960 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->devices); i++)
1961 {
1962 if (pBus->devices[i])
1963 pBus->devices[i]->Int.s.pBusRC += offDelta;
1964 }
1965}
1966
1967
1968/**
1969 * Construct a host to PCI Bus device instance for a VM.
1970 *
1971 * @returns VBox status.
1972 * @param pDevIns The device instance data.
1973 * If the registration structure is needed, pDevIns->pDevReg points to it.
1974 * @param iInstance Instance number. Use this to figure out which registers and such to use.
1975 * The device number is also found in pDevIns->iInstance, but since it's
1976 * likely to be freqently used PDM passes it as parameter.
1977 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
1978 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
1979 * iInstance it's expected to be used a bit in this function.
1980 */
1981static DECLCALLBACK(int) pciConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
1982{
1983 int rc;
1984 Assert(iInstance == 0);
1985
1986 /*
1987 * Validate and read configuration.
1988 */
1989 if (!CFGMR3AreValuesValid(pCfgHandle, "IOAPIC\0" "GCEnabled\0" "R0Enabled\0"))
1990 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
1991
1992 /* query whether we got an IOAPIC */
1993 bool fUseIoApic;
1994 rc = CFGMR3QueryBoolDef(pCfgHandle, "IOAPIC", &fUseIoApic, false);
1995 if (RT_FAILURE(rc))
1996 return PDMDEV_SET_ERROR(pDevIns, rc,
1997 N_("Configuration error: Failed to query boolean value \"IOAPIC\""));
1998
1999 /* check if RC code is enabled. */
2000 bool fGCEnabled;
2001 rc = CFGMR3QueryBoolDef(pCfgHandle, "GCEnabled", &fGCEnabled, true);
2002 if (RT_FAILURE(rc))
2003 return PDMDEV_SET_ERROR(pDevIns, rc,
2004 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
2005
2006 /* check if R0 code is enabled. */
2007 bool fR0Enabled;
2008 rc = CFGMR3QueryBoolDef(pCfgHandle, "R0Enabled", &fR0Enabled, true);
2009 if (RT_FAILURE(rc))
2010 return PDMDEV_SET_ERROR(pDevIns, rc,
2011 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
2012 Log(("PCI: fUseIoApic=%RTbool fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fUseIoApic, fGCEnabled, fR0Enabled));
2013
2014 /*
2015 * Init data and register the PCI bus.
2016 */
2017 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
2018 pGlobals->pci_bios_io_addr = 0xc000;
2019 pGlobals->pci_bios_mem_addr = 0xf0000000;
2020 memset((void *)&pGlobals->pci_irq_levels, 0, sizeof(pGlobals->pci_irq_levels));
2021 pGlobals->fUseIoApic = fUseIoApic;
2022 memset((void *)&pGlobals->pci_apic_irq_levels, 0, sizeof(pGlobals->pci_apic_irq_levels));
2023
2024 pGlobals->pDevInsR3 = pDevIns;
2025 pGlobals->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2026 pGlobals->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2027
2028 pGlobals->PciBus.pDevInsR3 = pDevIns;
2029 pGlobals->PciBus.pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2030 pGlobals->PciBus.pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2031 pGlobals->PciBus.papBridgesR3 = (PPCIDEVICE *)PDMDevHlpMMHeapAllocZ(pDevIns, sizeof(PPCIDEVICE) * RT_ELEMENTS(pGlobals->PciBus.devices));
2032
2033 PDMPCIBUSREG PciBusReg;
2034 PPCIBUS pBus = &pGlobals->PciBus;
2035 PciBusReg.u32Version = PDM_PCIBUSREG_VERSION;
2036 PciBusReg.pfnRegisterR3 = pciRegister;
2037 PciBusReg.pfnIORegionRegisterR3 = pciIORegionRegister;
2038 PciBusReg.pfnSetConfigCallbacksR3 = pciSetConfigCallbacks;
2039 PciBusReg.pfnSetIrqR3 = pciSetIrq;
2040 PciBusReg.pfnSaveExecR3 = pciGenericSaveExec;
2041 PciBusReg.pfnLoadExecR3 = pciGenericLoadExec;
2042 PciBusReg.pfnFakePCIBIOSR3 = pciFakePCIBIOS;
2043 PciBusReg.pszSetIrqRC = fGCEnabled ? "pciSetIrq" : NULL;
2044 PciBusReg.pszSetIrqR0 = fR0Enabled ? "pciSetIrq" : NULL;
2045 rc = pDevIns->pDevHlpR3->pfnPCIBusRegister(pDevIns, &PciBusReg, &pBus->pPciHlpR3);
2046 if (RT_FAILURE(rc))
2047 return PDMDEV_SET_ERROR(pDevIns, rc,
2048 N_("Failed to register ourselves as a PCI Bus"));
2049 if (pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION)
2050 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2051 N_("PCI helper version mismatch; got %#x expected %#x"),
2052 pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION);
2053
2054 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2055 pBus->pPciHlpR0 = pBus->pPciHlpR3->pfnGetR0Helpers(pDevIns);
2056
2057 /*
2058 * Fill in PCI configs and add them to the bus.
2059 */
2060 /* i440FX */
2061 PCIDevSetVendorId( &pBus->PciDev, 0x8086); /* Intel */
2062 PCIDevSetDeviceId( &pBus->PciDev, 0x1237);
2063 PCIDevSetRevisionId(&pBus->PciDev, 0x02);
2064 PCIDevSetClassSub( &pBus->PciDev, 0x00); /* host2pci */
2065 PCIDevSetClassBase( &pBus->PciDev, 0x06); /* PCI_bridge */
2066 PCIDevSetHeaderType(&pBus->PciDev, 0x00);
2067
2068 pBus->PciDev.pDevIns = pDevIns;
2069 pBus->PciDev.Int.s.fRequestedDevFn= true;
2070 pciRegisterInternal(pBus, 0, &pBus->PciDev, "i440FX");
2071
2072 /* PIIX3 */
2073 PCIDevSetVendorId( &pGlobals->PIIX3State.dev, 0x8086); /* Intel */
2074 PCIDevSetDeviceId( &pGlobals->PIIX3State.dev, 0x7000); /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
2075 PCIDevSetClassSub( &pGlobals->PIIX3State.dev, 0x01); /* PCI_ISA */
2076 PCIDevSetClassBase( &pGlobals->PIIX3State.dev, 0x06); /* PCI_bridge */
2077 PCIDevSetHeaderType(&pGlobals->PIIX3State.dev, 0x80); /* PCI_multifunction, generic */
2078
2079 pGlobals->PIIX3State.dev.pDevIns = pDevIns;
2080 pGlobals->PIIX3State.dev.Int.s.fRequestedDevFn= true;
2081 pciRegisterInternal(pBus, 8, &pGlobals->PIIX3State.dev, "PIIX3");
2082 piix3_reset(&pGlobals->PIIX3State);
2083
2084 pBus->iDevSearch = 16;
2085
2086 /*
2087 * Register I/O ports and save state.
2088 */
2089 rc = PDMDevHlpIOPortRegister(pDevIns, 0x0cf8, 1, NULL, pciIOPortAddressWrite, pciIOPortAddressRead, NULL, NULL, "i440FX (PCI)");
2090 if (RT_FAILURE(rc))
2091 return rc;
2092 rc = PDMDevHlpIOPortRegister(pDevIns, 0x0cfc, 4, NULL, pciIOPortDataWrite, pciIOPortDataRead, NULL, NULL, "i440FX (PCI)");
2093 if (RT_FAILURE(rc))
2094 return rc;
2095 if (fGCEnabled)
2096 {
2097 rc = PDMDevHlpIOPortRegisterGC(pDevIns, 0x0cf8, 1, NIL_RTGCPTR, "pciIOPortAddressWrite", "pciIOPortAddressRead", NULL, NULL, "i440FX (PCI)");
2098 if (RT_FAILURE(rc))
2099 return rc;
2100 rc = PDMDevHlpIOPortRegisterGC(pDevIns, 0x0cfc, 4, NIL_RTGCPTR, "pciIOPortDataWrite", "pciIOPortDataRead", NULL, NULL, "i440FX (PCI)");
2101 if (RT_FAILURE(rc))
2102 return rc;
2103 }
2104 if (fR0Enabled)
2105 {
2106 rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x0cf8, 1, NIL_RTR0PTR, "pciIOPortAddressWrite", "pciIOPortAddressRead", NULL, NULL, "i440FX (PCI)");
2107 if (RT_FAILURE(rc))
2108 return rc;
2109 rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x0cfc, 4, NIL_RTR0PTR, "pciIOPortDataWrite", "pciIOPortDataRead", NULL, NULL, "i440FX (PCI)");
2110 if (RT_FAILURE(rc))
2111 return rc;
2112 }
2113
2114 rc = SSMR3RegisterDevice(PDMDevHlpGetVM(pDevIns), pDevIns, "pci", iInstance, VBOX_PCI_SAVED_STATE_VERSION, sizeof(*pBus) + 16*128, "pgm",
2115 NULL, pciR3SaveExec, NULL, pciR3LoadPrep, pciR3LoadExec, NULL);
2116 if (RT_FAILURE(rc))
2117 return rc;
2118
2119 return VINF_SUCCESS;
2120}
2121
2122
2123/**
2124 * The device registration structure.
2125 */
2126const PDMDEVREG g_DevicePCI =
2127{
2128 /* u32Version */
2129 PDM_DEVREG_VERSION,
2130 /* szDeviceName */
2131 "pci",
2132 /* szRCMod */
2133 "VBoxDDGC.gc",
2134 /* szR0Mod */
2135 "VBoxDDR0.r0",
2136 /* pszDescription */
2137 "i440FX PCI bridge and PIIX3 ISA bridge.",
2138 /* fFlags */
2139 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2140 /* fClass */
2141 PDM_DEVREG_CLASS_BUS_PCI | PDM_DEVREG_CLASS_BUS_ISA,
2142 /* cMaxInstances */
2143 1,
2144 /* cbInstance */
2145 sizeof(PCIGLOBALS),
2146 /* pfnConstruct */
2147 pciConstruct,
2148 /* pfnDestruct */
2149 NULL,
2150 /* pfnRelocate */
2151 pciRelocate,
2152 /* pfnIOCtl */
2153 NULL,
2154 /* pfnPowerOn */
2155 NULL,
2156 /* pfnReset */
2157 NULL,
2158 /* pfnSuspend */
2159 NULL,
2160 /* pfnResume */
2161 NULL,
2162 /* pfnAttach */
2163 NULL,
2164 /* pfnDetach */
2165 NULL,
2166 /* pfnQueryInterface */
2167 NULL,
2168 /* pfnInitComplete */
2169 NULL,
2170 /* pfnPowerOff */
2171 NULL,
2172 /* pfnSoftReset */
2173 NULL,
2174 /* u32VersionEnd */
2175 PDM_DEVREG_VERSION
2176
2177};
2178#endif /* IN_RING3 */
2179
2180/**
2181 * Set the IRQ for a PCI device on a secondary bus.
2182 *
2183 * @param pDevIns Device instance of the PCI Bus.
2184 * @param pPciDev The PCI device structure.
2185 * @param iIrq IRQ number to set.
2186 * @param iLevel IRQ level.
2187 */
2188PDMBOTHCBDECL(void) pcibridgeSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel)
2189{
2190 /*
2191 * The PCI-to-PCI bridge specification defines how the interrupt pins
2192 * are routed from the secondary to the primary bus (see chapter 9).
2193 * iIrq gives the interrupt pin the pci device asserted.
2194 * We change iIrq here according to the spec and call the SetIrq function
2195 * of our parent passing the device which asserted the interrupt instead of the device of the bridge.
2196 */
2197 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2198 int iIrqPinBridge = 0;
2199 uint8_t uDevFnBridge = pPciDev->devfn;
2200
2201 /* Walk the chain until we reach the host bus. */
2202 while (pBus->iBus != 0)
2203 {
2204 uDevFnBridge = pBus->PciDev.devfn;
2205 iIrqPinBridge = ((uDevFnBridge >> 3) + iIrqPinBridge) & 3;
2206 /* Get the parent. */
2207 pBus = pBus->PciDev.Int.s.CTX_SUFF(pBus);
2208 }
2209
2210 AssertMsg(pBus->iBus == 0, ("This is not the host pci bus iBus=%d\n", pBus->iBus));
2211 pciSetIrqInternal(PCIBUS_2_PCIGLOBALS(pBus), uDevFnBridge, pPciDev, iIrqPinBridge, iLevel);
2212}
2213
2214#ifdef IN_RING3
2215
2216static void pcibridgeConfigWrite(PPDMDEVINSR3 pDevIns, uint8_t iBus, uint8_t iDevice, uint32_t u32Address, uint32_t u32Value, unsigned cb)
2217{
2218 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2219
2220 LogFlowFunc((": pDevIns=%p iBus=%d iDevice=%d u32Address=%u u32Value=%u cb=%d\n", pDevIns, iBus, iDevice, u32Address, u32Value, cb));
2221
2222 /* If the current bus is not the target bus search for the bus which contains the device. */
2223 if (iBus != pBus->PciDev.config[VBOX_PCI_SECONDARY_BUS])
2224 {
2225 PPCIDEVICE pBridgeDevice = pciFindBridge(pBus, iBus);
2226 if (pBridgeDevice)
2227 {
2228 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigWrite);
2229 pBridgeDevice->Int.s.pfnBridgeConfigWrite(pBridgeDevice->pDevIns, iBus, iDevice, u32Address, u32Value, cb);
2230 }
2231 }
2232 else
2233 {
2234 /* This is the target bus, pass the write to the device. */
2235 PPCIDEVICE pPciDev = pBus->devices[iDevice];
2236 if (pPciDev)
2237 {
2238 Log(("%s: %s: addr=%02x val=%08x len=%d\n", __FUNCTION__, pPciDev->name, u32Address, u32Value, cb));
2239 pPciDev->Int.s.pfnConfigWrite(pPciDev, u32Address, u32Value, cb);
2240 }
2241 }
2242}
2243
2244static uint32_t pcibridgeConfigRead(PPDMDEVINSR3 pDevIns, uint8_t iBus, uint8_t iDevice, uint32_t u32Address, unsigned cb)
2245{
2246 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2247 uint32_t u32Value = 0xffffffff; /* Return value in case there is no device. */
2248
2249 LogFlowFunc((": pDevIns=%p iBus=%d iDevice=%d u32Address=%u cb=%d\n", pDevIns, iBus, iDevice, u32Address, cb));
2250
2251 /* If the current bus is not the target bus search for the bus which contains the device. */
2252 if (iBus != pBus->PciDev.config[VBOX_PCI_SECONDARY_BUS])
2253 {
2254 PPCIDEVICE pBridgeDevice = pciFindBridge(pBus, iBus);
2255 if (pBridgeDevice)
2256 {
2257 AssertPtr( pBridgeDevice->Int.s.pfnBridgeConfigRead);
2258 u32Value = pBridgeDevice->Int.s.pfnBridgeConfigRead(pBridgeDevice->pDevIns, iBus, iDevice, u32Address, cb);
2259 }
2260 }
2261 else
2262 {
2263 /* This is the target bus, pass the read to the device. */
2264 PPCIDEVICE pPciDev = pBus->devices[iDevice];
2265 if (pPciDev)
2266 {
2267 u32Value = pPciDev->Int.s.pfnConfigRead(pPciDev, u32Address, cb);
2268 Log(("%s: %s: u32Address=%02x u32Value=%08x cb=%d\n", __FUNCTION__, pPciDev->name, u32Address, u32Value, cb));
2269 }
2270 }
2271
2272 return u32Value;
2273}
2274
2275/**
2276 * Saves a state of a PCI bridge device.
2277 *
2278 * @returns VBox status code.
2279 * @param pDevIns The device instance.
2280 * @param pPciDev Pointer to PCI device.
2281 * @param pSSMHandle The handle to save the state to.
2282 */
2283static DECLCALLBACK(int) pcibridgeR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
2284{
2285/** @todo make common with pciR3SaveExec! */
2286 uint32_t i;
2287 PPCIBUS pThis = PDMINS_2_DATA(pDevIns, PPCIBUS);
2288
2289 /*
2290 * Iterate all the devices.
2291 */
2292 for (i = 0; i < RT_ELEMENTS(pThis->devices); i++)
2293 {
2294 PPCIDEVICE pDev = pThis->devices[i];
2295 if (pDev)
2296 {
2297 int rc;
2298 SSMR3PutU32(pSSMHandle, i);
2299 SSMR3PutMem(pSSMHandle, pDev->config, sizeof(pDev->config));
2300
2301 rc = SSMR3PutS32(pSSMHandle, pDev->Int.s.uIrqPinState);
2302 if (RT_FAILURE(rc))
2303 return rc;
2304 }
2305 }
2306 return SSMR3PutU32(pSSMHandle, ~0); /* terminator */
2307}
2308
2309
2310/**
2311 * Prepares a state load.
2312 *
2313 * This will disable all the device so that the I/O regions gets unmapped.
2314 *
2315 * @returns VINF_SUCCESS
2316 * @param pDevIns The device instance.
2317 * @param pSSMHandle The saved state handle.
2318 */
2319static DECLCALLBACK(int) pcibridgeR3LoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
2320{
2321 PPCIBUS pThis = PDMINS_2_DATA(pDevIns, PPCIBUS);
2322 return pciR3CommonLoadPrep(pThis);
2323}
2324
2325
2326/**
2327 * Loads a saved PCI bridge device state.
2328 *
2329 * @returns VBox status code.
2330 * @param pDevIns The device instance.
2331 * @param pSSMHandle The handle to the saved state.
2332 * @param u32Version The data unit version number.
2333 */
2334static DECLCALLBACK(int) pcibridgeR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
2335{
2336 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2337 uint32_t u32;
2338 uint32_t i;
2339 int rc;
2340
2341/** @todo r=bird: this is a copy of pciR3LoadExec. combine the two! */
2342
2343 /*
2344 * Check the version.
2345 */
2346 if (u32Version > VBOX_PCI_SAVED_STATE_VERSION)
2347 {
2348 AssertFailed();
2349 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2350 }
2351
2352 /*
2353 * Iterate all the devices.
2354 */
2355 for (i = 0;; i++)
2356 {
2357 PCIDEVICE DevTmp;
2358 PPCIDEVICE pDev;
2359
2360 /* index / terminator */
2361 rc = SSMR3GetU32(pSSMHandle, &u32);
2362 if (RT_FAILURE(rc))
2363 return rc;
2364 if (u32 == (uint32_t)~0)
2365 break;
2366 if ( u32 >= RT_ELEMENTS(pBus->devices)
2367 || u32 < i)
2368 {
2369 AssertMsgFailed(("u32=%#x i=%#x\n", u32, i));
2370 return rc;
2371 }
2372
2373 /* skip forward to the device checking that no new devices are present. */
2374 for (; i < u32; i++)
2375 {
2376 if (pBus->devices[i])
2377 {
2378 LogRel(("New device in slot %#x, %s (vendor=%#06x device=%#06x)\n", i, pBus->devices[i]->name,
2379 PCIDevGetVendorId(pBus->devices[i]), PCIDevGetDeviceId(pBus->devices[i])));
2380 if (SSMR3HandleGetAfter(pSSMHandle) != SSMAFTER_DEBUG_IT)
2381 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
2382 }
2383 }
2384
2385 /* get the data */
2386 DevTmp.Int.s.uIrqPinState = 0;
2387 SSMR3GetMem(pSSMHandle, DevTmp.config, sizeof(DevTmp.config));
2388 rc = SSMR3GetS32(pSSMHandle, &DevTmp.Int.s.uIrqPinState);
2389 if (RT_FAILURE(rc))
2390 return rc;
2391
2392 /* check that it's still around. */
2393 pDev = pBus->devices[i];
2394 if (!pDev)
2395 {
2396 LogRel(("Device in slot %#x has been removed! vendor=%#06x device=%#06x\n", i,
2397 PCIDevGetVendorId(&DevTmp), PCIDevGetDeviceId(&DevTmp)));
2398 if (SSMR3HandleGetAfter(pSSMHandle) != SSMAFTER_DEBUG_IT)
2399 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
2400 continue;
2401 }
2402
2403 /* match the vendor id assuming that this will never be changed. */
2404 if ( DevTmp.config[0] != pDev->config[0]
2405 || DevTmp.config[1] != pDev->config[1])
2406 {
2407 LogRel(("Device in slot %#x (%s) vendor id mismatch! saved=%.4Rhxs current=%.4Rhxs\n",
2408 i, pDev->name, DevTmp.config, pDev->config));
2409 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
2410 }
2411
2412 /* commit the loaded device config. */
2413 pciR3CommonRestoreConfig(pDev, &DevTmp.config[0], false ); /** @todo fix bridge fun! */
2414
2415 pDev->Int.s.uIrqPinState = DevTmp.Int.s.uIrqPinState;
2416 }
2417
2418 return VINF_SUCCESS;
2419}
2420
2421/**
2422 * @copydoc FNPDMDEVRESET
2423 */
2424static DECLCALLBACK(void) pcibridgeReset(PPDMDEVINS pDevIns)
2425{
2426 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2427
2428 /* Reset config space to default values. */
2429 pBus->PciDev.config[VBOX_PCI_PRIMARY_BUS] = 0;
2430 pBus->PciDev.config[VBOX_PCI_SECONDARY_BUS] = 0;
2431 pBus->PciDev.config[VBOX_PCI_SUBORDINATE_BUS] = 0;
2432}
2433
2434/**
2435 * @copydoc FNPDMDEVRELOCATE
2436 */
2437static DECLCALLBACK(void) pcibridgeRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2438{
2439 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2440 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2441
2442 /* Relocate RC pointers for the attached pci devices. */
2443 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->devices); i++)
2444 {
2445 if (pBus->devices[i])
2446 pBus->devices[i]->Int.s.pBusRC += offDelta;
2447 }
2448}
2449
2450/**
2451 * Registers the device with the default PCI bus.
2452 *
2453 * @returns VBox status code.
2454 * @param pDevIns Device instance of the PCI Bus.
2455 * @param pPciDev The PCI device structure.
2456 * Any PCI enabled device must keep this in it's instance data!
2457 * Fill in the PCI data config before registration, please.
2458 * @param pszName Pointer to device name (permanent, readonly). For debugging, not unique.
2459 * @param iDev The PCI device number. Use a negative value for auto assigning one.
2460 */
2461static DECLCALLBACK(int) pcibridgeRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, const char *pszName, int iDev)
2462{
2463 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2464
2465 /*
2466 * Check input.
2467 */
2468 if ( !pszName
2469 || !pPciDev
2470 || iDev >= (int)RT_ELEMENTS(pBus->devices))
2471 {
2472 AssertMsgFailed(("Invalid argument! pszName=%s pPciDev=%p iDev=%d\n", pszName, pPciDev, iDev));
2473 return VERR_INVALID_PARAMETER;
2474 }
2475
2476 /*
2477 * Register the device.
2478 */
2479 return pciRegisterInternal(pBus, iDev, pPciDev, pszName);
2480}
2481
2482/**
2483 * Construct a PCI bridge device instance for a VM.
2484 *
2485 * @returns VBox status.
2486 * @param pDevIns The device instance data.
2487 * If the registration structure is needed, pDevIns->pDevReg points to it.
2488 * @param iInstance Instance number. Use this to figure out which registers and such to use.
2489 * The device number is also found in pDevIns->iInstance, but since it's
2490 * likely to be freqently used PDM passes it as parameter.
2491 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
2492 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
2493 * iInstance it's expected to be used a bit in this function.
2494 */
2495static DECLCALLBACK(int) pcibridgeConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
2496{
2497 int rc;
2498
2499 /*
2500 * Validate and read configuration.
2501 */
2502 if (!CFGMR3AreValuesValid(pCfgHandle, "GCEnabled\0" "R0Enabled\0"))
2503 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
2504
2505 /* check if RC code is enabled. */
2506 bool fGCEnabled;
2507 rc = CFGMR3QueryBoolDef(pCfgHandle, "GCEnabled", &fGCEnabled, true);
2508 if (RT_FAILURE(rc))
2509 return PDMDEV_SET_ERROR(pDevIns, rc,
2510 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
2511
2512 /* check if R0 code is enabled. */
2513 bool fR0Enabled;
2514 rc = CFGMR3QueryBoolDef(pCfgHandle, "R0Enabled", &fR0Enabled, true);
2515 if (RT_FAILURE(rc))
2516 return PDMDEV_SET_ERROR(pDevIns, rc,
2517 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
2518 Log(("PCI: fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fGCEnabled, fR0Enabled));
2519
2520 /*
2521 * Init data and register the PCI bus.
2522 */
2523 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2524 pBus->pDevInsR3 = pDevIns;
2525 pBus->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2526 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2527 pBus->papBridgesR3 = (PPCIDEVICE *)PDMDevHlpMMHeapAllocZ(pDevIns, sizeof(PPCIDEVICE) * RT_ELEMENTS(pBus->devices));
2528
2529 PDMPCIBUSREG PciBusReg;
2530 PciBusReg.u32Version = PDM_PCIBUSREG_VERSION;
2531 PciBusReg.pfnRegisterR3 = pcibridgeRegister;
2532 PciBusReg.pfnIORegionRegisterR3 = pciIORegionRegister;
2533 PciBusReg.pfnSetConfigCallbacksR3 = pciSetConfigCallbacks;
2534 PciBusReg.pfnSetIrqR3 = pcibridgeSetIrq;
2535 PciBusReg.pfnSaveExecR3 = pciGenericSaveExec;
2536 PciBusReg.pfnLoadExecR3 = pciGenericLoadExec;
2537 PciBusReg.pfnFakePCIBIOSR3 = NULL; /* Only needed for the first bus. */
2538 PciBusReg.pszSetIrqRC = fGCEnabled ? "pcibridgeSetIrq" : NULL;
2539 PciBusReg.pszSetIrqR0 = fR0Enabled ? "pcibridgeSetIrq" : NULL;
2540 rc = pDevIns->pDevHlpR3->pfnPCIBusRegister(pDevIns, &PciBusReg, &pBus->pPciHlpR3);
2541 if (RT_FAILURE(rc))
2542 return PDMDEV_SET_ERROR(pDevIns, rc,
2543 N_("Failed to register ourselves as a PCI Bus"));
2544 if (pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION)
2545 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2546 N_("PCI helper version mismatch; got %#x expected %#x"),
2547 pBus->pPciHlpR3->u32Version, PDM_PCIHLPR3_VERSION);
2548
2549 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2550 pBus->pPciHlpR0 = pBus->pPciHlpR3->pfnGetR0Helpers(pDevIns);
2551
2552 /*
2553 * Fill in PCI configs and add them to the bus.
2554 */
2555 PCIDevSetVendorId( &pBus->PciDev, 0x8086); /* Intel */
2556 PCIDevSetDeviceId( &pBus->PciDev, 0x2448); /* 82801 Mobile PCI bridge. */
2557 PCIDevSetRevisionId(&pBus->PciDev, 0xf2);
2558 PCIDevSetClassSub( &pBus->PciDev, 0x04); /* pci2pci */
2559 PCIDevSetClassBase( &pBus->PciDev, 0x06); /* PCI_bridge */
2560 PCIDevSetClassProg( &pBus->PciDev, 0x01); /* Supports subtractive decoding. */
2561 PCIDevSetHeaderType(&pBus->PciDev, 0x01); /* Single function device which adheres to the PCI-to-PCI bridge spec. */
2562 PCIDevSetCommand( &pBus->PciDev, 0x00);
2563 PCIDevSetStatus( &pBus->PciDev, 0x20); /* 66MHz Capable. */
2564 PCIDevSetInterruptLine(&pBus->PciDev, 0x00); /* This device does not assert interrupts. */
2565
2566 /*
2567 * This device does not generate interrupts. Interrupt delivery from
2568 * devices attached to the bus is unaffected.
2569 */
2570 PCIDevSetInterruptPin (&pBus->PciDev, 0x00);
2571
2572 pBus->PciDev.pDevIns = pDevIns;
2573 pBus->PciDev.Int.s.fPciToPciBridge = true;
2574 pBus->PciDev.Int.s.pfnBridgeConfigRead = pcibridgeConfigRead;
2575 pBus->PciDev.Int.s.pfnBridgeConfigWrite = pcibridgeConfigWrite;
2576
2577 /*
2578 * Register this PCI bridge. The called function will take care on which bus we will get registered.
2579 */
2580 rc = PDMDevHlpPCIRegister (pDevIns, &pBus->PciDev);
2581 if (RT_FAILURE(rc))
2582 return rc;
2583
2584 pBus->iDevSearch = 0;
2585 /*
2586 * The iBus property doesn't really represent the bus number
2587 * because the guest and the BIOS can choose different bus numbers
2588 * for them.
2589 * The bus number is mainly for the setIrq function to indicate
2590 * when the host bus is reached which will have iBus = 0.
2591 * Thathswhy the + 1.
2592 */
2593 pBus->iBus = iInstance + 1;
2594
2595 /*
2596 * Register SSM handlers. We use the same saved state version as for the host bridge
2597 * to make changes easier.
2598 */
2599 rc = SSMR3RegisterDevice(PDMDevHlpGetVM(pDevIns), pDevIns, "pcibridge", iInstance, VBOX_PCI_SAVED_STATE_VERSION, sizeof(*pBus) + 16*128, "pgm",
2600 NULL, pcibridgeR3SaveExec, NULL, pcibridgeR3LoadPrep, pcibridgeR3LoadExec, NULL);
2601 if (RT_FAILURE(rc))
2602 return rc;
2603
2604 return VINF_SUCCESS;
2605}
2606
2607/**
2608 * The device registration structure
2609 * for the PCI-to-PCI bridge.
2610 */
2611const PDMDEVREG g_DevicePCIBridge =
2612{
2613 /* u32Version */
2614 PDM_DEVREG_VERSION,
2615 /* szDeviceName */
2616 "pcibridge",
2617 /* szRCMod */
2618 "VBoxDDGC.gc",
2619 /* szR0Mod */
2620 "VBoxDDR0.r0",
2621 /* pszDescription */
2622 "82801 Mobile PCI to PCI bridge",
2623 /* fFlags */
2624 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2625 /* fClass */
2626 PDM_DEVREG_CLASS_BUS_PCI,
2627 /* cMaxInstances */
2628 ~0,
2629 /* cbInstance */
2630 sizeof(PCIBUS),
2631 /* pfnConstruct */
2632 pcibridgeConstruct,
2633 /* pfnDestruct */
2634 NULL,
2635 /* pfnRelocate */
2636 pcibridgeRelocate,
2637 /* pfnIOCtl */
2638 NULL,
2639 /* pfnPowerOn */
2640 NULL,
2641 /* pfnReset */
2642 pcibridgeReset,
2643 /* pfnSuspend */
2644 NULL,
2645 /* pfnResume */
2646 NULL,
2647 /* pfnAttach */
2648 NULL,
2649 /* pfnDetach */
2650 NULL,
2651 /* pfnQueryInterface */
2652 NULL,
2653 /* pfnInitComplete */
2654 NULL,
2655 /* pfnPowerOff */
2656 NULL,
2657 /* pfnSoftReset */
2658 NULL,
2659 /* u32VersionEnd */
2660 PDM_DEVREG_VERSION
2661};
2662
2663#endif /* IN_RING3 */
2664#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

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