VirtualBox

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

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

warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 85.5 KB
Line 
1/* $Id: DevPCI.cpp 14474 2008-11-21 16:43:27Z 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*******************************************************************************/
217__BEGIN_DECLS
218
219PDMBOTHCBDECL(void) pciSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel);
220PDMBOTHCBDECL(void) pcibridgeSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel);
221
222#ifdef IN_RING3
223DECLINLINE(PPCIDEVICE) pciFindBridge(PPCIBUS pBus, uint8_t iBus);
224#endif
225
226__END_DECLS
227
228#define DEBUG_PCI
229
230#define PCI_VENDOR_ID 0x00 /* 16 bits */
231#define PCI_DEVICE_ID 0x02 /* 16 bits */
232#define PCI_COMMAND 0x04 /* 16 bits */
233#define PCI_COMMAND_IO 0x01 /* Enable response in I/O space */
234#define PCI_COMMAND_MEMORY 0x02 /* Enable response in Memory space */
235#define PCI_CLASS_DEVICE 0x0a /* Device class */
236#define PCI_INTERRUPT_LINE 0x3c /* 8 bits */
237#define PCI_INTERRUPT_PIN 0x3d /* 8 bits */
238#define PCI_MIN_GNT 0x3e /* 8 bits */
239#define PCI_MAX_LAT 0x3f /* 8 bits */
240
241#ifdef IN_RING3
242
243static void pci_addr_writel(PPCIGLOBALS pGlobals, uint32_t addr, uint32_t val)
244{
245 pGlobals->uConfigReg = val;
246}
247
248static uint32_t pci_addr_readl(PPCIGLOBALS pGlobals, uint32_t addr)
249{
250 return pGlobals->uConfigReg;
251}
252
253static void pci_update_mappings(PCIDevice *d)
254{
255 PPCIBUS pBus = d->Int.s.CTX_SUFF(pBus);
256 PCIIORegion *r;
257 int cmd, i;
258 uint32_t last_addr, new_addr, config_ofs;
259
260 cmd = RT_LE2H_U16(*(uint16_t *)(d->config + PCI_COMMAND));
261 for(i = 0; i < PCI_NUM_REGIONS; i++) {
262 r = &d->Int.s.aIORegions[i];
263 if (i == PCI_ROM_SLOT) {
264 config_ofs = 0x30;
265 } else {
266 config_ofs = 0x10 + i * 4;
267 }
268 if (r->size != 0) {
269 if (r->type & PCI_ADDRESS_SPACE_IO) {
270 if (cmd & PCI_COMMAND_IO) {
271 new_addr = RT_LE2H_U32(*(uint32_t *)(d->config +
272 config_ofs));
273 new_addr = new_addr & ~(r->size - 1);
274 last_addr = new_addr + r->size - 1;
275 /* NOTE: we have only 64K ioports on PC */
276 if (last_addr <= new_addr || new_addr == 0 ||
277 last_addr >= 0x10000) {
278 new_addr = ~0U;
279 }
280 } else {
281 new_addr = ~0U;
282 }
283 } else {
284 if (cmd & PCI_COMMAND_MEMORY) {
285 new_addr = RT_LE2H_U32(*(uint32_t *)(d->config +
286 config_ofs));
287 /* the ROM slot has a specific enable bit */
288 if (i == PCI_ROM_SLOT && !(new_addr & 1))
289 goto no_mem_map;
290 new_addr = new_addr & ~(r->size - 1);
291 last_addr = new_addr + r->size - 1;
292 /* NOTE: we do not support wrapping */
293 /* XXX: as we cannot support really dynamic
294 mappings, we handle specific values as invalid
295 mappings. */
296 if (last_addr <= new_addr || new_addr == 0 ||
297 last_addr == ~0U) {
298 new_addr = ~0U;
299 }
300 } else {
301 no_mem_map:
302 new_addr = ~0U;
303 }
304 }
305 /* now do the real mapping */
306 if (new_addr != r->addr) {
307 if (r->addr != ~0U) {
308 if (r->type & PCI_ADDRESS_SPACE_IO) {
309 int devclass;
310 /* NOTE: specific hack for IDE in PC case:
311 only one byte must be mapped. */
312 devclass = d->config[0x0a] | (d->config[0x0b] << 8);
313 if (devclass == 0x0101 && r->size == 4) {
314 int rc = d->pDevIns->pDevHlpR3->pfnIOPortDeregister(d->pDevIns, r->addr + 2, 1);
315 AssertRC(rc);
316 } else {
317 int rc = d->pDevIns->pDevHlpR3->pfnIOPortDeregister(d->pDevIns, r->addr, r->size);
318 AssertRC(rc);
319 }
320 } else {
321 RTGCPHYS GCPhysBase = r->addr;
322 int rc;
323 if (pBus->pPciHlpR3->pfnIsMMIO2Base(pBus->pDevInsR3, d->pDevIns, GCPhysBase))
324 {
325 /* unmap it. */
326 rc = r->map_func(d, i, NIL_RTGCPHYS, r->size, (PCIADDRESSSPACE)(r->type));
327 AssertRC(rc);
328 rc = PDMDevHlpMMIO2Unmap(d->pDevIns, i, GCPhysBase);
329 }
330 else
331 rc = d->pDevIns->pDevHlpR3->pfnMMIODeregister(d->pDevIns, GCPhysBase, r->size);
332 AssertMsgRC(rc, ("rc=%Rrc d=%s i=%d GCPhysBase=%RGp size=%#x\n", rc, d->name, i, GCPhysBase, r->size));
333 }
334 }
335 r->addr = new_addr;
336 if (r->addr != ~0U) {
337 int rc = r->map_func(d, i,
338 r->addr + (r->type & PCI_ADDRESS_SPACE_IO ? 0 : 0),
339 r->size, (PCIADDRESSSPACE)(r->type));
340 AssertRC(rc);
341 }
342 }
343 }
344 }
345}
346
347
348static DECLCALLBACK(uint32_t) pci_default_read_config(PCIDevice *d, uint32_t address, unsigned len)
349{
350 uint32_t val;
351 switch(len) {
352 case 1:
353 val = d->config[address];
354 break;
355 case 2:
356 val = RT_LE2H_U16(*(uint16_t *)(d->config + address));
357 break;
358 default:
359 case 4:
360 val = RT_LE2H_U32(*(uint32_t *)(d->config + address));
361 break;
362 }
363 return val;
364}
365
366static DECLCALLBACK(void) pci_default_write_config(PCIDevice *d, uint32_t address, uint32_t val, unsigned len)
367{
368 int can_write;
369 unsigned i;
370 uint32_t end, addr;
371
372 if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) ||
373 (address >= 0x30 && address < 0x34))) {
374 PCIIORegion *r;
375 int reg;
376
377 if ( address >= 0x30 ) {
378 reg = PCI_ROM_SLOT;
379 }else{
380 reg = (address - 0x10) >> 2;
381 }
382 r = &d->Int.s.aIORegions[reg];
383 if (r->size == 0)
384 goto default_config;
385 /* compute the stored value */
386 if (reg == PCI_ROM_SLOT) {
387 /* keep ROM enable bit */
388 val &= (~(r->size - 1)) | 1;
389 } else {
390 val &= ~(r->size - 1);
391 val |= r->type;
392 }
393 *(uint32_t *)(d->config + address) = RT_H2LE_U32(val);
394 pci_update_mappings(d);
395 return;
396 }
397 default_config:
398 /* not efficient, but simple */
399 addr = address;
400 for(i = 0; i < len; i++) {
401 /* default read/write accesses */
402 switch(d->config[0x0e]) {
403 case 0x00:
404 case 0x80:
405 switch(addr) {
406 case 0x00:
407 case 0x01:
408 case 0x02:
409 case 0x03:
410 case 0x08:
411 case 0x09:
412 case 0x0a:
413 case 0x0b:
414 case 0x0e:
415 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17: /* base */
416 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
417 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
418 case 0x30: case 0x31: case 0x32: case 0x33: /* rom */
419 case 0x3d:
420 can_write = 0;
421 break;
422 default:
423 can_write = 1;
424 break;
425 }
426 break;
427 default:
428 case 0x01:
429 switch(addr) {
430 case 0x00:
431 case 0x01:
432 case 0x02:
433 case 0x03:
434 case 0x08:
435 case 0x09:
436 case 0x0a:
437 case 0x0b:
438 case 0x0e:
439 case 0x38: case 0x39: case 0x3a: case 0x3b: /* rom */
440 case 0x3d:
441 can_write = 0;
442 break;
443 default:
444 can_write = 1;
445 break;
446 }
447 break;
448 }
449#ifdef VBOX
450 /* status register: only clear bits by writing a '1' at the corresponding bit */
451 if (addr == 0x06)
452 {
453 d->config[addr] &= ~val;
454 d->config[addr] |= 0x08; /* interrupt status */
455 }
456 else if (addr == 0x07)
457 {
458 d->config[addr] &= ~val;
459 }
460 else
461#endif
462 if (can_write) {
463 d->config[addr] = val;
464 }
465 addr++;
466 val >>= 8;
467 }
468
469 end = address + len;
470 if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
471 /* if the command register is modified, we must modify the mappings */
472 pci_update_mappings(d);
473 }
474}
475
476static void pci_data_write(PPCIGLOBALS pGlobals, uint32_t addr, uint32_t val, int len)
477{
478 PCIDevice *pci_dev;
479 uint8_t iBus, iDevice;
480 uint32_t config_addr;
481
482 Log(("pci_data_write: addr=%08x val=%08x len=%d\n", pGlobals->uConfigReg, val, len));
483
484 if (!(pGlobals->uConfigReg & (1 << 31))) {
485 return;
486 }
487 if ((pGlobals->uConfigReg & 0x3) != 0) {
488 return;
489 }
490 iBus = (pGlobals->uConfigReg >> 16) & 0xff;
491 iDevice = (pGlobals->uConfigReg >> 8) & 0xff;
492 config_addr = (pGlobals->uConfigReg & 0xfc) | (addr & 3);
493 if (iBus != 0)
494 {
495 PPCIDEVICE pBridgeDevice = pciFindBridge(&pGlobals->PciBus, iBus);
496 if (pBridgeDevice)
497 {
498 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigWrite);
499 pBridgeDevice->Int.s.pfnBridgeConfigWrite(pBridgeDevice->pDevIns, iBus, iDevice, config_addr, val, len);
500 }
501 }
502 else
503 {
504 pci_dev = pGlobals->PciBus.devices[iDevice];
505 if (!pci_dev)
506 return;
507 Log(("pci_config_write: %s: addr=%02x val=%08x len=%d\n", pci_dev->name, config_addr, val, len));
508 pci_dev->Int.s.pfnConfigWrite(pci_dev, config_addr, val, len);
509 }
510}
511
512static uint32_t pci_data_read(PPCIGLOBALS pGlobals, uint32_t addr, int len)
513{
514 uint8_t iBus, iDevice;
515 uint32_t config_addr;
516 uint32_t val = 0xffffffff;
517
518 if (!(pGlobals->uConfigReg & (1 << 31)))
519 goto the_end;
520 if ((pGlobals->uConfigReg & 0x3) != 0)
521 goto the_end;
522 iBus = (pGlobals->uConfigReg >> 16) & 0xff;
523 iDevice = (pGlobals->uConfigReg >> 8) & 0xff;
524 config_addr = (pGlobals->uConfigReg & 0xfc) | (addr & 3);
525 if (iBus != 0)
526 {
527 PPCIDEVICE pBridgeDevice = pciFindBridge(&pGlobals->PciBus, iBus);
528 if (pBridgeDevice)
529 {
530 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigRead);
531 val = pBridgeDevice->Int.s.pfnBridgeConfigRead(pBridgeDevice->pDevIns, iBus, iDevice, config_addr, len);
532 }
533 }
534 else
535 {
536 PCIDevice *pci_dev;
537
538 pci_dev = pGlobals->PciBus.devices[iDevice];
539 if (!pci_dev) {
540 goto the_end;
541 }
542 val = pci_dev->Int.s.pfnConfigRead(pci_dev, config_addr, len);
543 Log(("pci_config_read: %s: addr=%02x val=%08x len=%d\n", pci_dev->name, config_addr, val, len));
544 }
545
546 the_end:
547 return val;
548}
549
550#endif /* IN_RING3 */
551
552
553/* return the global irq number corresponding to a given device irq
554 pin. We could also use the bus number to have a more precise
555 mapping.
556 This is the implementation note described in the PCI spec chapter 2.2.6 */
557static inline int pci_slot_get_pirq(uint8_t uDevFn, int irq_num)
558{
559 int slot_addend;
560 slot_addend = (uDevFn >> 3) - 1;
561 return (irq_num + slot_addend) & 3;
562}
563
564static inline int pci_slot_get_apic_pirq(uint8_t uDevFn, int irq_num)
565{
566 return (irq_num + (uDevFn >> 3)) & 7;
567}
568
569static inline int get_pci_irq_apic_level(PPCIGLOBALS pGlobals, int irq_num)
570{
571 return (pGlobals->pci_apic_irq_levels[irq_num] != 0);
572}
573
574static void apic_set_irq(PPCIBUS pBus, uint8_t uDevFn, PCIDevice *pPciDev, int irq_num1, int iLevel, int acpi_irq)
575{
576 /* This is only allowed to be called with a pointer to the host bus. */
577 AssertMsg(pBus->iBus == 0, ("iBus=%u\n", pBus->iBus));
578
579 if (acpi_irq == -1) {
580 int apic_irq, apic_level;
581 PPCIGLOBALS pGlobals = PCIBUS_2_PCIGLOBALS(pBus);
582 int irq_num = pci_slot_get_apic_pirq(uDevFn, irq_num1);
583
584 if ((iLevel & PDM_IRQ_LEVEL_HIGH) == PDM_IRQ_LEVEL_HIGH)
585 ASMAtomicIncU32(&pGlobals->pci_apic_irq_levels[irq_num]);
586 else if ((iLevel & PDM_IRQ_LEVEL_HIGH) == PDM_IRQ_LEVEL_LOW)
587 ASMAtomicDecU32(&pGlobals->pci_apic_irq_levels[irq_num]);
588
589 apic_irq = irq_num + 0x10;
590 apic_level = get_pci_irq_apic_level(pGlobals, irq_num);
591 Log3(("apic_set_irq: %s: irq_num1=%d level=%d apic_irq=%d apic_level=%d irq_num1=%d\n",
592 R3STRING(pPciDev->name), irq_num1, iLevel, apic_irq, apic_level, irq_num));
593 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), apic_irq, apic_level);
594
595 if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP) {
596 ASMAtomicDecU32(&pGlobals->pci_apic_irq_levels[irq_num]);
597 pPciDev->Int.s.uIrqPinState = PDM_IRQ_LEVEL_LOW;
598 apic_level = get_pci_irq_apic_level(pGlobals, irq_num);
599 Log3(("apic_set_irq: %s: irq_num1=%d level=%d apic_irq=%d apic_level=%d irq_num1=%d (flop)\n",
600 R3STRING(pPciDev->name), irq_num1, iLevel, apic_irq, apic_level, irq_num));
601 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), apic_irq, apic_level);
602 }
603 } else {
604 Log3(("apic_set_irq: %s: irq_num1=%d level=%d acpi_irq=%d\n",
605 R3STRING(pPciDev->name), irq_num1, iLevel, acpi_irq));
606 pBus->CTX_SUFF(pPciHlp)->pfnIoApicSetIrq(pBus->CTX_SUFF(pDevIns), acpi_irq, iLevel);
607 }
608}
609
610DECLINLINE(int) get_pci_irq_level(PPCIGLOBALS pGlobals, int irq_num)
611{
612 return (pGlobals->pci_irq_levels[irq_num] != 0);
613}
614
615/**
616 * Set the IRQ for a PCI device on the host bus - shared by host bus and bridge.
617 *
618 * @param pDevIns Device instance of the host PCI Bus.
619 * @param uDevFn The device number on the host bus which will raise the IRQ
620 * @param pPciDev The PCI device structure which raised the interrupt.
621 * @param iIrq IRQ number to set.
622 * @param iLevel IRQ level.
623 * @remark uDevFn and pPciDev->devfn are not the same if the device is behind a bridge.
624 * In that case uDevFn will be the slot of the bridge which is needed to calculate the
625 * PIRQ value.
626 */
627static void pciSetIrqInternal(PPCIGLOBALS pGlobals, uint8_t uDevFn, PPCIDEVICE pPciDev, int iIrq, int iLevel)
628{
629 PPCIBUS pBus = &pGlobals->PciBus;
630 uint8_t *pbCfg = pGlobals->PIIX3State.dev.config;
631 const bool fIsAcpiDevice = pPciDev->config[2] == 0x13 && pPciDev->config[3] == 0x71;
632 const bool fIsApicEnabled = pGlobals->fUseIoApic && pbCfg[0xde] == 0xbe && pbCfg[0xad] == 0xef;
633 int pic_irq, pic_level;
634
635 /* Check if the state changed. */
636 if (pPciDev->Int.s.uIrqPinState != iLevel)
637 {
638 pPciDev->Int.s.uIrqPinState = (iLevel & PDM_IRQ_LEVEL_HIGH);
639
640 /* apic only */
641 if (fIsApicEnabled)
642 {
643 if (fIsAcpiDevice)
644 /*
645 * ACPI needs special treatment since SCI is hardwired and
646 * should not be affected by PCI IRQ routing tables at the
647 * same time SCI IRQ is shared in PCI sense hence this
648 * kludge (i.e. we fetch the hardwired value from ACPIs
649 * PCI device configuration space).
650 */
651 apic_set_irq(pBus, uDevFn, pPciDev, -1, iLevel, pPciDev->config[PCI_INTERRUPT_LINE]);
652 else
653 apic_set_irq(pBus, uDevFn, pPciDev, iIrq, iLevel, -1);
654 return;
655 }
656
657 if (fIsAcpiDevice)
658 {
659 /* As per above treat ACPI in a special way */
660 pic_irq = pPciDev->config[PCI_INTERRUPT_LINE];
661 pGlobals->acpi_irq = pic_irq;
662 pGlobals->acpi_irq_level = iLevel & PDM_IRQ_LEVEL_HIGH;
663 }
664 else
665 {
666 int irq_num;
667 irq_num = pci_slot_get_pirq(uDevFn, iIrq);
668
669 if (pPciDev->Int.s.uIrqPinState == PDM_IRQ_LEVEL_HIGH)
670 ASMAtomicIncU32(&pGlobals->pci_irq_levels[irq_num]);
671 else if (pPciDev->Int.s.uIrqPinState == PDM_IRQ_LEVEL_LOW)
672 ASMAtomicDecU32(&pGlobals->pci_irq_levels[irq_num]);
673
674 /* now we change the pic irq level according to the piix irq mappings */
675 pic_irq = pbCfg[0x60 + irq_num];
676 if (pic_irq >= 16)
677 {
678 if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP)
679 {
680 ASMAtomicDecU32(&pGlobals->pci_irq_levels[irq_num]);
681 pPciDev->Int.s.uIrqPinState = PDM_IRQ_LEVEL_LOW;
682 }
683
684 return;
685 }
686 }
687
688 /* the pic level is the logical OR of all the PCI irqs mapped to it */
689 pic_level = 0;
690 if (pic_irq == pbCfg[0x60])
691 pic_level |= get_pci_irq_level(pGlobals, 0);
692 if (pic_irq == pbCfg[0x61])
693 pic_level |= get_pci_irq_level(pGlobals, 1);
694 if (pic_irq == pbCfg[0x62])
695 pic_level |= get_pci_irq_level(pGlobals, 2);
696 if (pic_irq == pbCfg[0x63])
697 pic_level |= get_pci_irq_level(pGlobals, 3);
698 if (pic_irq == pGlobals->acpi_irq)
699 pic_level |= pGlobals->acpi_irq_level;
700
701 Log3(("pciSetIrq: %s: iLevel=%d iIrq=%d pic_irq=%d pic_level=%d\n",
702 R3STRING(pPciDev->name), iLevel, iIrq, pic_irq, pic_level));
703 pBus->CTX_SUFF(pPciHlp)->pfnIsaSetIrq(pBus->CTX_SUFF(pDevIns), pic_irq, pic_level);
704
705 /** @todo optimize pci irq flip-flop some rainy day. */
706 if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP)
707 pciSetIrqInternal(pGlobals, uDevFn, pPciDev, iIrq, PDM_IRQ_LEVEL_LOW);
708 }
709}
710
711/**
712 * Set the IRQ for a PCI device on the host bus.
713 *
714 * @param pDevIns Device instance of the PCI Bus.
715 * @param pPciDev The PCI device structure.
716 * @param iIrq IRQ number to set.
717 * @param iLevel IRQ level.
718 */
719PDMBOTHCBDECL(void) pciSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel)
720{
721 pciSetIrqInternal(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), pPciDev->devfn, pPciDev, iIrq, iLevel);
722}
723
724#ifdef IN_RING3
725
726/**
727 * Finds a bridge on the bus which contains the destination bus.
728 *
729 * @return Pointer to the device instance data of the bus or
730 * NULL if no bridge was found.
731 * @param pBus Pointer to the bus to search on.
732 * @param iBus Destination bus number.
733 */
734DECLINLINE(PPCIDEVICE) pciFindBridge(PPCIBUS pBus, uint8_t iBus)
735{
736 /* Search for a fitting bridge. */
737 for (uint32_t iBridge = 0; iBridge < pBus->cBridges; iBridge++)
738 {
739 /*
740 * Examine secondary and subordinate bus number.
741 * If the target bus is in the range we pass the request on to the bridge.
742 */
743 PPCIDEVICE pBridgeTemp = pBus->papBridgesR3[iBridge];
744 AssertMsg(pBridgeTemp && pBridgeTemp->Int.s.fPciToPciBridge,
745 ("Device is not a PCI bridge but on the list of PCI bridges\n"));
746
747 if ( iBus >= pBridgeTemp->config[VBOX_PCI_SECONDARY_BUS]
748 && iBus <= pBridgeTemp->config[VBOX_PCI_SUBORDINATE_BUS])
749 return pBridgeTemp;
750 }
751
752 /* Nothing found. */
753 return NULL;
754}
755
756static void piix3_reset(PIIX3State *d)
757{
758 uint8_t *pci_conf = d->dev.config;
759
760 pci_conf[0x04] = 0x07; /* master, memory and I/O */
761 pci_conf[0x05] = 0x00;
762 pci_conf[0x06] = 0x00;
763 pci_conf[0x07] = 0x02; /* PCI_status_devsel_medium */
764 pci_conf[0x4c] = 0x4d;
765 pci_conf[0x4e] = 0x03;
766 pci_conf[0x4f] = 0x00;
767 pci_conf[0x60] = 0x80;
768 pci_conf[0x69] = 0x02;
769 pci_conf[0x70] = 0x80;
770 pci_conf[0x76] = 0x0c;
771 pci_conf[0x77] = 0x0c;
772 pci_conf[0x78] = 0x02;
773 pci_conf[0x79] = 0x00;
774 pci_conf[0x80] = 0x00;
775 pci_conf[0x82] = 0x02; /* Get rid of the Linux guest "Enabling Passive Release" PCI quirk warning. */
776 pci_conf[0xa0] = 0x08;
777 pci_conf[0xa0] = 0x08;
778 pci_conf[0xa2] = 0x00;
779 pci_conf[0xa3] = 0x00;
780 pci_conf[0xa4] = 0x00;
781 pci_conf[0xa5] = 0x00;
782 pci_conf[0xa6] = 0x00;
783 pci_conf[0xa7] = 0x00;
784 pci_conf[0xa8] = 0x0f;
785 pci_conf[0xaa] = 0x00;
786 pci_conf[0xab] = 0x00;
787 pci_conf[0xac] = 0x00;
788 pci_conf[0xae] = 0x00;
789}
790
791static void pci_config_writel(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t val)
792{
793 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
794 (uDevFn << 8) | addr;
795 pci_data_write(pGlobals, 0, val, 4);
796}
797
798static void pci_config_writew(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t val)
799{
800 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
801 (uDevFn << 8) | (addr & ~3);
802 pci_data_write(pGlobals, addr & 3, val, 2);
803}
804
805static void pci_config_writeb(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr, uint32_t val)
806{
807 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
808 (uDevFn << 8) | (addr & ~3);
809 pci_data_write(pGlobals, addr & 3, val, 1);
810}
811
812static uint32_t pci_config_readl(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr)
813{
814 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
815 (uDevFn << 8) | addr;
816 return pci_data_read(pGlobals, 0, 4);
817}
818
819static uint32_t pci_config_readw(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr)
820{
821 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
822 (uDevFn << 8) | (addr & ~3);
823 return pci_data_read(pGlobals, addr & 3, 2);
824}
825
826static uint32_t pci_config_readb(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint32_t addr)
827{
828 pGlobals->uConfigReg = 0x80000000 | (uBus << 16) |
829 (uDevFn << 8) | (addr & ~3);
830 return pci_data_read(pGlobals, addr & 3, 1);
831}
832
833/* host irqs corresponding to PCI irqs A-D */
834static const uint8_t pci_irqs[4] = { 11, 9, 11, 9 }; /* bird: added const */
835
836static void pci_set_io_region_addr(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, int region_num, uint32_t addr)
837{
838 uint16_t cmd;
839 uint32_t ofs;
840
841 if ( region_num == PCI_ROM_SLOT )
842 ofs = 0x30;
843 else
844 ofs = 0x10 + region_num * 4;
845
846 /* Read memory type first. */
847 uint8_t uRessourceType = pci_config_readb(pGlobals, uBus, uDevFn, ofs);
848
849 /* Read command register. */
850 cmd = pci_config_readw(pGlobals, uBus, uDevFn, PCI_COMMAND);
851 if ( region_num == PCI_ROM_SLOT )
852 cmd |= 2;
853 else if ((uRessourceType & 0x01) == 1) /* Test if region is I/O space. */
854 cmd |= 1; /* Enable I/O space access. */
855 else /* The region is MMIO. */
856 cmd |= 2; /* Enable MMIO access. */
857
858 /* Write address of the device. */
859 pci_config_writel(pGlobals, uBus, uDevFn, ofs, addr);
860
861 /* enable memory mappings */
862 pci_config_writew(pGlobals, uBus, uDevFn, PCI_COMMAND, cmd);
863}
864
865static void pci_bios_init_device(PPCIGLOBALS pGlobals, uint8_t uBus, uint8_t uDevFn, uint8_t cBridgeDepth, uint8_t *paBridgePositions)
866{
867 uint32_t *paddr;
868 int i, pin, pic_irq;
869 uint16_t devclass, vendor_id, device_id;
870
871 devclass = pci_config_readw(pGlobals, uBus, uDevFn, PCI_CLASS_DEVICE);
872 vendor_id = pci_config_readw(pGlobals, uBus, uDevFn, PCI_VENDOR_ID);
873 device_id = pci_config_readw(pGlobals, uBus, uDevFn, PCI_DEVICE_ID);
874
875 /* Check if device is present. */
876 if (vendor_id != 0xffff)
877 {
878 switch(devclass)
879 {
880 case 0x0101:
881 if ( (vendor_id == 0x8086)
882 && (device_id == 0x7010 || device_id == 0x7111))
883 {
884 /* PIIX3 or PIIX4 IDE */
885 pci_config_writew(pGlobals, uBus, uDevFn, 0x40, 0x8000); /* enable IDE0 */
886 pci_config_writew(pGlobals, uBus, uDevFn, 0x42, 0x8000); /* enable IDE1 */
887 goto default_map;
888 }
889 else
890 {
891 /* IDE: we map it as in ISA mode */
892 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 0, 0x1f0);
893 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 1, 0x3f4);
894 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 2, 0x170);
895 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 3, 0x374);
896 }
897 break;
898 case 0x0300:
899 if (vendor_id != 0x80ee)
900 goto default_map;
901 /* VGA: map frame buffer to default Bochs VBE address */
902 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 0, 0xE0000000);
903 break;
904 case 0x0800:
905 /* PIC */
906 vendor_id = pci_config_readw(pGlobals, uBus, uDevFn, PCI_VENDOR_ID);
907 device_id = pci_config_readw(pGlobals, uBus, uDevFn, PCI_DEVICE_ID);
908 if (vendor_id == 0x1014)
909 {
910 /* IBM */
911 if (device_id == 0x0046 || device_id == 0xFFFF)
912 {
913 /* MPIC & MPIC2 */
914 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 0, 0x80800000 + 0x00040000);
915 }
916 }
917 break;
918 case 0xff00:
919 if ( (vendor_id == 0x0106b)
920 && (device_id == 0x0017 || device_id == 0x0022))
921 {
922 /* macio bridge */
923 pci_set_io_region_addr(pGlobals, uBus, uDevFn, 0, 0x80800000);
924 }
925 break;
926 case 0x0604:
927 {
928 /* Init PCI-to-PCI bridge. */
929 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_PRIMARY_BUS, uBus);
930
931 AssertMsg(pGlobals->uBus < 255, ("Too many bridges on the bus\n"));
932 pGlobals->uBus++;
933 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_SECONDARY_BUS, pGlobals->uBus);
934 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_SUBORDINATE_BUS, 0xff); /* Temporary until we know how many other bridges are behind this one. */
935
936 /* Add position of this bridge into the array. */
937 paBridgePositions[cBridgeDepth+1] = (uDevFn >> 3);
938
939 /*
940 * The I/O range for the bridge must be aligned to a 4KB boundary.
941 * This does not change anything really as the access to the device is not going
942 * through the bridge but we want to be compliant to the spec.
943 */
944 if ((pGlobals->pci_bios_io_addr % 4096) != 0)
945 pGlobals->pci_bios_io_addr = RT_ALIGN_32(pGlobals->pci_bios_io_addr, 4*1024);
946 Log(("%s: Aligned I/O start address. New address %#x\n", __FUNCTION__, pGlobals->pci_bios_io_addr));
947 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_IO_BASE, (pGlobals->pci_bios_io_addr >> 8) & 0xf0);
948
949 /* The MMIO range for the bridge must be aligned to a 1MB boundary. */
950 if ((pGlobals->pci_bios_mem_addr % (1024 * 1024)) != 0)
951 pGlobals->pci_bios_mem_addr = RT_ALIGN_32(pGlobals->pci_bios_mem_addr, 1024*1024);
952 Log(("%s: Aligned MMIO start address. New address %#x\n", __FUNCTION__, pGlobals->pci_bios_mem_addr));
953 pci_config_writew(pGlobals, uBus, uDevFn, VBOX_PCI_MEMORY_BASE, (pGlobals->pci_bios_mem_addr >> 16) & UINT32_C(0xffff0));
954
955 /* Save values to compare later to. */
956 uint32_t u32IoAddressBase = pGlobals->pci_bios_io_addr;
957 uint32_t u32MMIOAddressBase = pGlobals->pci_bios_mem_addr;
958
959 /* Init devices behind the bridge and possibly other bridges as well. */
960 for (int i = 0; i <= 255; i++)
961 pci_bios_init_device(pGlobals, uBus + 1, i, cBridgeDepth + 1, paBridgePositions);
962
963 /* The number of bridges behind the this one is now available. */
964 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_SUBORDINATE_BUS, pGlobals->uBus);
965
966 /*
967 * Set I/O limit register. If there is no device with I/O space behind the bridge
968 * we set a lower value than in the base register.
969 * The result with a real bridge is that no I/O transactions are passed to the secondary
970 * interface. Again this doesn't really matter here but we want to be compliant to the spec.
971 */
972 if ((u32IoAddressBase != pGlobals->pci_bios_io_addr) && ((pGlobals->pci_bios_io_addr % 4096) != 0))
973 {
974 /* The upper boundary must be one byte less than a 4KB boundary. */
975 pGlobals->pci_bios_io_addr = RT_ALIGN_32(pGlobals->pci_bios_io_addr, 4*1024);
976 }
977 pci_config_writeb(pGlobals, uBus, uDevFn, VBOX_PCI_IO_LIMIT, ((pGlobals->pci_bios_io_addr >> 8) & 0xf0) - 1);
978
979 /* Same with the MMIO limit register but with 1MB boundary here. */
980 if ((u32MMIOAddressBase != pGlobals->pci_bios_mem_addr) && ((pGlobals->pci_bios_mem_addr % (1024 * 1024)) != 0))
981 {
982 /* The upper boundary must be one byte less than a 1MB boundary. */
983 pGlobals->pci_bios_mem_addr = RT_ALIGN_32(pGlobals->pci_bios_mem_addr, 1024*1024);
984 }
985 pci_config_writew(pGlobals, uBus, uDevFn, VBOX_PCI_MEMORY_LIMIT, ((pGlobals->pci_bios_mem_addr >> 16) & UINT32_C(0xfff0)) - 1);
986
987 /*
988 * Set the prefetch base and limit registers. We currently have no device with a prefetchable region
989 * which may be behind a bridge. Thatswhy it is unconditionally disabled here atm by writing a higher value into
990 * the base register than in the limit register.
991 */
992 pci_config_writew(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_MEMORY_BASE, 0xfff0);
993 pci_config_writew(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_MEMORY_LIMIT, 0x0);
994 pci_config_writel(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_BASE_UPPER32, 0x00);
995 pci_config_writel(pGlobals, uBus, uDevFn, VBOX_PCI_PREF_LIMIT_UPPER32, 0x00);
996 break;
997 }
998 default:
999 default_map:
1000 {
1001 /* default memory mappings */
1002 /*
1003 * PCI_NUM_REGIONS is 7 because of the rom region but there are only 6 base address register defined by the PCI spec.
1004 * Leaving only PCI_NUM_REGIONS would cause reading another and enabling a memory region which does not exist.
1005 */
1006 for(i = 0; i < (PCI_NUM_REGIONS-1); i++)
1007 {
1008 uint32_t u32Size;
1009 uint8_t u8RessourceType;
1010 uint32_t u32Address = 0x10 + i * 4;
1011
1012 /* Calculate size. */
1013 u8RessourceType = pci_config_readb(pGlobals, uBus, uDevFn, u32Address);
1014 pci_config_writel(pGlobals, uBus, uDevFn, u32Address, UINT32_C(0xffffffff));
1015 u32Size = pci_config_readl(pGlobals, uBus, uDevFn, u32Address);
1016 /* Clear ressource information depending on ressource type. */
1017 if ((u8RessourceType & 0x01) == 1) /* I/O */
1018 u32Size &= ~(0x01);
1019 else /* MMIO */
1020 u32Size &= ~(0x0f);
1021
1022 /*
1023 * Invert all bits and add 1 to get size of the region.
1024 * (From PCI implementation note)
1025 */
1026 if (((u8RessourceType & 0x01) == 1) && (u32Size & UINT32_C(0xffff0000)) == 0)
1027 u32Size = (~(u32Size | UINT32_C(0xffff0000))) + 1;
1028 else
1029 u32Size = (~u32Size) + 1;
1030
1031 Log(("%s: Size of region %u for device %d on bus %d is %u\n", __FUNCTION__, i, uDevFn, uBus, u32Size));
1032
1033 if (u32Size)
1034 {
1035 if ((u8RessourceType & 0x01) == 1)
1036 paddr = &pGlobals->pci_bios_io_addr;
1037 else
1038 paddr = &pGlobals->pci_bios_mem_addr;
1039 *paddr = (*paddr + u32Size - 1) & ~(u32Size - 1);
1040 Log(("%s: Start address of %s region %u is %#x\n", __FUNCTION__, ((u8RessourceType & 0x01) == 1 ? "I/O" : "MMIO"), i, *paddr));
1041 pci_set_io_region_addr(pGlobals, uBus, uDevFn, i, *paddr);
1042 *paddr += u32Size;
1043 Log(("%s: New address is %#x\n", __FUNCTION__, *paddr));
1044 }
1045 }
1046 break;
1047 }
1048 }
1049
1050 /* map the interrupt */
1051 pin = pci_config_readb(pGlobals, uBus, uDevFn, PCI_INTERRUPT_PIN);
1052 if (pin != 0)
1053 {
1054 uint8_t uBridgeDevFn = uDevFn;
1055 pin--;
1056
1057 /* We need to go up to the host bus to see which irq this device will assert there. */
1058 while (cBridgeDepth != 0)
1059 {
1060 /* Get the pin the device would assert on the bridge. */
1061 pin = ((uBridgeDevFn >> 3) + pin) & 3;
1062 uBridgeDevFn = paBridgePositions[cBridgeDepth];
1063 cBridgeDepth--;
1064 }
1065
1066 pin = pci_slot_get_pirq(uDevFn, pin);
1067 pic_irq = pci_irqs[pin];
1068 pci_config_writeb(pGlobals, uBus, uDevFn, PCI_INTERRUPT_LINE, pic_irq);
1069 }
1070 }
1071}
1072
1073/* -=-=-=-=-=- wrappers -=-=-=-=-=- */
1074
1075/**
1076 * Port I/O Handler for PCI address OUT operations.
1077 *
1078 * @returns VBox status code.
1079 *
1080 * @param pDevIns The device instance.
1081 * @param pvUser User argument - ignored.
1082 * @param uPort Port number used for the IN operation.
1083 * @param u32 The value to output.
1084 * @param cb The value size in bytes.
1085 */
1086static DECLCALLBACK(int) pciIOPortAddressWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
1087{
1088 Log(("pciIOPortAddressWrite: Port=%#x u32=%#x cb=%d\n", Port, u32, cb));
1089 NOREF(pvUser);
1090 if (cb == 4)
1091 {
1092 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_WRITE);
1093 pci_addr_writel(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), Port, u32);
1094 PCI_UNLOCK(pDevIns);
1095 }
1096 /* else: 440FX does "pass through to the bus" for other writes, what ever that means.
1097 * Linux probes for cmd640 using byte writes/reads during ide init. We'll just ignore it. */
1098 return VINF_SUCCESS;
1099}
1100
1101/**
1102 * Port I/O Handler for PCI address IN operations.
1103 *
1104 * @returns VBox status code.
1105 *
1106 * @param pDevIns The device instance.
1107 * @param pvUser User argument - ignored.
1108 * @param uPort Port number used for the IN operation.
1109 * @param pu32 Where to store the result.
1110 * @param cb Number of bytes read.
1111 */
1112static DECLCALLBACK(int) pciIOPortAddressRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
1113{
1114 NOREF(pvUser);
1115 if (cb == 4)
1116 {
1117 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_READ);
1118 *pu32 = pci_addr_readl(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), Port);
1119 PCI_UNLOCK(pDevIns);
1120 Log(("pciIOPortAddressRead: Port=%#x cb=%d -> %#x\n", Port, cb, *pu32));
1121 return VINF_SUCCESS;
1122 }
1123 /* else: 440FX does "pass through to the bus" for other writes, what ever that means.
1124 * Linux probes for cmd640 using byte writes/reads during ide init. We'll just ignore it. */
1125 Log(("pciIOPortAddressRead: Port=%#x cb=%d VERR_IOM_IOPORT_UNUSED\n", Port, cb));
1126 return VERR_IOM_IOPORT_UNUSED;
1127}
1128
1129
1130/**
1131 * Port I/O Handler for PCI data OUT operations.
1132 *
1133 * @returns VBox status code.
1134 *
1135 * @param pDevIns The device instance.
1136 * @param pvUser User argument - ignored.
1137 * @param uPort Port number used for the IN operation.
1138 * @param u32 The value to output.
1139 * @param cb The value size in bytes.
1140 */
1141static DECLCALLBACK(int) pciIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
1142{
1143 Log(("pciIOPortDataWrite: Port=%#x u32=%#x cb=%d\n", Port, u32, cb));
1144 NOREF(pvUser);
1145 if (!(Port % cb))
1146 {
1147 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_WRITE);
1148 pci_data_write(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), Port, u32, cb);
1149 PCI_UNLOCK(pDevIns);
1150 }
1151 else
1152 AssertMsgFailed(("Write to port %#x u32=%#x cb=%d\n", Port, u32, cb));
1153 return VINF_SUCCESS;
1154}
1155
1156
1157/**
1158 * Port I/O Handler for PCI data IN operations.
1159 *
1160 * @returns VBox status code.
1161 *
1162 * @param pDevIns The device instance.
1163 * @param pvUser User argument - ignored.
1164 * @param uPort Port number used for the IN operation.
1165 * @param pu32 Where to store the result.
1166 * @param cb Number of bytes read.
1167 */
1168static DECLCALLBACK(int) pciIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
1169{
1170 NOREF(pvUser);
1171 if (!(Port % cb))
1172 {
1173 PCI_LOCK(pDevIns, VINF_IOM_HC_IOPORT_READ);
1174 *pu32 = pci_data_read(PDMINS_2_DATA(pDevIns, PPCIGLOBALS), Port, cb);
1175 PCI_UNLOCK(pDevIns);
1176 Log(("pciIOPortDataRead: Port=%#x cb=%#x -> %#x\n", Port, cb, *pu32));
1177 return VINF_SUCCESS;
1178 }
1179 AssertMsgFailed(("Read from port %#x cb=%d\n", Port, cb));
1180 return VERR_IOM_IOPORT_UNUSED;
1181}
1182
1183
1184/**
1185 * Saves a state of the PCI device.
1186 *
1187 * @returns VBox status code.
1188 * @param pDevIns Device instance of the PCI Bus.
1189 * @param pPciDev Pointer to PCI device.
1190 * @param pSSMHandle The handle to save the state to.
1191 */
1192static DECLCALLBACK(int) pciGenericSaveExec(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PSSMHANDLE pSSMHandle)
1193{
1194 return SSMR3PutMem(pSSMHandle, &pPciDev->config[0], sizeof(pPciDev->config));
1195}
1196
1197
1198/**
1199 * Loads a saved PCI device state.
1200 *
1201 * @returns VBox status code.
1202 * @param pDevIns Device instance of the PCI Bus.
1203 * @param pPciDev Pointer to PCI device.
1204 * @param pSSMHandle The handle to the saved state.
1205 */
1206static DECLCALLBACK(int) pciGenericLoadExec(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PSSMHANDLE pSSMHandle)
1207{
1208 return SSMR3GetMem(pSSMHandle, &pPciDev->config[0], sizeof(pPciDev->config));
1209}
1210
1211
1212/**
1213 * Saves a state of the PCI device.
1214 *
1215 * @returns VBox status code.
1216 * @param pDevIns The device instance.
1217 * @param pPciDev Pointer to PCI device.
1218 * @param pSSMHandle The handle to save the state to.
1219 */
1220static DECLCALLBACK(int) pciSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1221{
1222 uint32_t i;
1223 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1224 PPCIBUS pBus = &pThis->PciBus;
1225
1226 /*
1227 * Bus state data.
1228 */
1229 SSMR3PutU32(pSSMHandle, pThis->uConfigReg);
1230 SSMR3PutBool(pSSMHandle, pThis->fUseIoApic);
1231 /*
1232 * Save IRQ states.
1233 */
1234 for (i = 0; i < PCI_IRQ_PINS; i++)
1235 SSMR3PutU32(pSSMHandle, pThis->pci_irq_levels[i]);
1236 for (i = 0; i < PCI_APIC_IRQ_PINS; i++)
1237 SSMR3PutU32(pSSMHandle, pThis->pci_apic_irq_levels[i]);
1238
1239 SSMR3PutU32(pSSMHandle, pThis->acpi_irq_level);
1240 SSMR3PutS32(pSSMHandle, pThis->acpi_irq);
1241
1242 SSMR3PutU32(pSSMHandle, ~0); /* separator */
1243
1244 /*
1245 * Iterate all the devices.
1246 */
1247 for (i = 0; i < RT_ELEMENTS(pBus->devices); i++)
1248 {
1249 PPCIDEVICE pDev = pBus->devices[i];
1250 if (pDev)
1251 {
1252 int rc;
1253 SSMR3PutU32(pSSMHandle, i);
1254 SSMR3PutMem(pSSMHandle, pDev->config, sizeof(pDev->config));
1255
1256 rc = SSMR3PutS32(pSSMHandle, pDev->Int.s.uIrqPinState);
1257 if (RT_FAILURE(rc))
1258 return rc;
1259 }
1260 }
1261 return SSMR3PutU32(pSSMHandle, ~0); /* terminator */
1262}
1263
1264/**
1265 * Loads a saved PCI device state.
1266 *
1267 * @returns VBox status code.
1268 * @param pDevIns The device instance.
1269 * @param pSSMHandle The handle to the saved state.
1270 * @param u32Version The data unit version number.
1271 */
1272static DECLCALLBACK(int) pciLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
1273{
1274 PPCIGLOBALS pThis = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1275 PPCIBUS pBus = &pThis->PciBus;
1276 uint32_t u32;
1277 uint32_t i;
1278 int rc;
1279
1280 /*
1281 * Check the version.
1282 */
1283 if (u32Version > VBOX_PCI_SAVED_STATE_VERSION)
1284 {
1285 AssertFailed();
1286 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1287 }
1288
1289 /*
1290 * Bus state data.
1291 */
1292 SSMR3GetU32(pSSMHandle, &pThis->uConfigReg);
1293 if (u32Version > 1)
1294 SSMR3GetBool(pSSMHandle, &pThis->fUseIoApic);
1295
1296 /* Load IRQ states. */
1297 if (u32Version > 2)
1298 {
1299 for (uint8_t i = 0; i < PCI_IRQ_PINS; i++)
1300 SSMR3GetU32(pSSMHandle, (uint32_t *)&pThis->pci_irq_levels[i]);
1301 for (uint8_t i = 0; i < PCI_APIC_IRQ_PINS; i++)
1302 SSMR3GetU32(pSSMHandle, (uint32_t *)&pThis->pci_apic_irq_levels[i]);
1303
1304 SSMR3GetU32(pSSMHandle, &pThis->acpi_irq_level);
1305 SSMR3GetS32(pSSMHandle, &pThis->acpi_irq);
1306 }
1307
1308 /* separator */
1309 rc = SSMR3GetU32(pSSMHandle, &u32);
1310 if (RT_FAILURE(rc))
1311 return rc;
1312 if (u32 != (uint32_t)~0)
1313 AssertMsgFailedReturn(("u32=%#x\n", u32), rc);
1314
1315 /*
1316 * Iterate all the devices.
1317 */
1318 for (i = 0;; i++)
1319 {
1320 PCIDEVICE DevTmp;
1321 PPCIDEVICE pDev;
1322
1323 /* index / terminator */
1324 rc = SSMR3GetU32(pSSMHandle, &u32);
1325 if (RT_FAILURE(rc))
1326 return rc;
1327 if (u32 == (uint32_t)~0)
1328 break;
1329 if ( u32 >= RT_ELEMENTS(pBus->devices)
1330 || u32 < i)
1331 {
1332 AssertMsgFailed(("u32=%#x i=%#x\n", u32, i));
1333 return rc;
1334 }
1335
1336 /* skip forward to the device checking that no new devices are present. */
1337 for (; i < u32; i++)
1338 {
1339 if (pBus->devices[i])
1340 {
1341 LogRel(("New device in slot %#x, %s (vendor=%#06x device=%#06x)\n", i, pBus->devices[i]->name,
1342 PCIDevGetVendorId(pBus->devices[i]), PCIDevGetDeviceId(pBus->devices[i])));
1343 if (SSMR3HandleGetAfter(pSSMHandle) != SSMAFTER_DEBUG_IT)
1344 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
1345 }
1346 }
1347
1348 /* Get the data */
1349 DevTmp.Int.s.uIrqPinState = ~0; /* Invalid value in case we have an older saved state to force a state change in pciSetIrq. */
1350 SSMR3GetMem(pSSMHandle, DevTmp.config, sizeof(DevTmp.config));
1351 if (u32Version < 3)
1352 {
1353 int32_t i32Temp;
1354 /* Irq value not needed anymore. */
1355 rc = SSMR3GetS32(pSSMHandle, &i32Temp);
1356 if (RT_FAILURE(rc))
1357 return rc;
1358 }
1359 else
1360 {
1361 rc = SSMR3GetS32(pSSMHandle, &DevTmp.Int.s.uIrqPinState);
1362 if (RT_FAILURE(rc))
1363 return rc;
1364 }
1365
1366 /* check that it's still around. */
1367 pDev = pBus->devices[i];
1368 if (!pDev)
1369 {
1370 LogRel(("Device in slot %#x has been removed! vendor=%#06x device=%#06x\n", i,
1371 PCIDevGetVendorId(&DevTmp), PCIDevGetDeviceId(&DevTmp)));
1372 if (SSMR3HandleGetAfter(pSSMHandle) != SSMAFTER_DEBUG_IT)
1373 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
1374 continue;
1375 }
1376
1377 /* match the vendor id assuming that this will never be changed. */
1378 if ( DevTmp.config[0] != pDev->config[0]
1379 || DevTmp.config[1] != pDev->config[1])
1380 {
1381 LogRel(("Device in slot %#x (%s) vendor id mismatch! saved=%.4Rhxs current=%.4Rhxs\n",
1382 i, pDev->name, DevTmp.config, pDev->config));
1383 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
1384 }
1385
1386 /* commit the loaded device config. */
1387 memcpy(pDev->config, DevTmp.config, sizeof(pDev->config));
1388
1389 pDev->Int.s.uIrqPinState = DevTmp.Int.s.uIrqPinState;
1390 }
1391
1392 return VINF_SUCCESS;
1393}
1394
1395
1396/* -=-=-=-=-=- real code -=-=-=-=-=- */
1397
1398/**
1399 * Registers the device with the specified PCI bus.
1400 *
1401 * @returns VBox status code.
1402 * @param pBus The bus to register with.
1403 * @param iDev The PCI device ordinal.
1404 * @param pPciDev The PCI device structure.
1405 * @param pszName Pointer to device name (permanent, readonly). For debugging, not unique.
1406 */
1407static int pciRegisterInternal(PPCIBUS pBus, int iDev, PPCIDEVICE pPciDev, const char *pszName)
1408{
1409 /*
1410 * Find device slot.
1411 */
1412 if (iDev < 0)
1413 {
1414 /*
1415 * Special check for the IDE controller which is our function 1 device
1416 * before searching.
1417 */
1418 if ( !strcmp(pszName, "piix3ide")
1419 && !pBus->devices[9])
1420 iDev = 9;
1421 else
1422 {
1423 Assert(!(pBus->iDevSearch % 8));
1424 for (iDev = pBus->iDevSearch; iDev < (int)RT_ELEMENTS(pBus->devices); iDev += 8)
1425 if ( !pBus->devices[iDev]
1426 && !pBus->devices[iDev + 1]
1427 && !pBus->devices[iDev + 2]
1428 && !pBus->devices[iDev + 3]
1429 && !pBus->devices[iDev + 4]
1430 && !pBus->devices[iDev + 5]
1431 && !pBus->devices[iDev + 6]
1432 && !pBus->devices[iDev + 7])
1433 break;
1434 if (iDev >= (int)RT_ELEMENTS(pBus->devices))
1435 {
1436 AssertMsgFailed(("Couldn't find free spot!\n"));
1437 return VERR_PDM_TOO_PCI_MANY_DEVICES;
1438 }
1439 }
1440 pPciDev->Int.s.fRequestedDevFn = false;
1441 }
1442 else
1443 {
1444 /*
1445 * An explicit request.
1446 *
1447 * If the slot is occupied we'll have to relocate the device
1448 * currently occupying it first. This can only be done if the
1449 * existing device wasn't explicitly assigned. Also we limit
1450 * ourselves to function 0 devices.
1451 *
1452 * If you start setting devices + function in the
1453 * config, do it for all pci devices!
1454 */
1455 //AssertReleaseMsg(iDev > 8 || pBus->iBus != 0, ("iDev=%d pszName=%s\n", iDev, pszName));
1456 if (pBus->devices[iDev])
1457 {
1458 int iDevRel;
1459 AssertReleaseMsg(!(iDev % 8), ("PCI Configuration Conflict! iDev=%d pszName=%s clashes with %s\n",
1460 iDev, pszName, pBus->devices[iDev]->name));
1461 if ( pBus->devices[iDev]->Int.s.fRequestedDevFn
1462 || (pBus->devices[iDev + 1] && pBus->devices[iDev + 1]->Int.s.fRequestedDevFn)
1463 || (pBus->devices[iDev + 2] && pBus->devices[iDev + 2]->Int.s.fRequestedDevFn)
1464 || (pBus->devices[iDev + 3] && pBus->devices[iDev + 3]->Int.s.fRequestedDevFn)
1465 || (pBus->devices[iDev + 4] && pBus->devices[iDev + 4]->Int.s.fRequestedDevFn)
1466 || (pBus->devices[iDev + 5] && pBus->devices[iDev + 5]->Int.s.fRequestedDevFn)
1467 || (pBus->devices[iDev + 6] && pBus->devices[iDev + 6]->Int.s.fRequestedDevFn)
1468 || (pBus->devices[iDev + 7] && pBus->devices[iDev + 7]->Int.s.fRequestedDevFn))
1469 {
1470 AssertReleaseMsgFailed(("Configuration error:'%s' and '%s' are both configured as device %d\n",
1471 pszName, pBus->devices[iDev]->name, iDev));
1472 return VERR_INTERNAL_ERROR;
1473 }
1474
1475 /* Find free slot for the device(s) we're moving and move them. */
1476 for (iDevRel = pBus->iDevSearch; iDevRel < (int)RT_ELEMENTS(pBus->devices); iDevRel += 8)
1477 {
1478 if ( !pBus->devices[iDevRel]
1479 && !pBus->devices[iDevRel + 1]
1480 && !pBus->devices[iDevRel + 2]
1481 && !pBus->devices[iDevRel + 3]
1482 && !pBus->devices[iDevRel + 4]
1483 && !pBus->devices[iDevRel + 5]
1484 && !pBus->devices[iDevRel + 6]
1485 && !pBus->devices[iDevRel + 7])
1486 {
1487 int i = 0;
1488 for (i = 0; i < 8; i++)
1489 {
1490 if (!pBus->devices[iDev + i])
1491 continue;
1492 Log(("PCI: relocating '%s' from slot %#x to %#x\n", pBus->devices[iDev + i]->name, iDev + i, iDevRel + i));
1493 pBus->devices[iDevRel + i] = pBus->devices[iDev + i];
1494 pBus->devices[iDevRel + i]->devfn = i;
1495 pBus->devices[iDev + i] = NULL;
1496 }
1497 }
1498 }
1499 if (pBus->devices[iDev])
1500 {
1501 AssertMsgFailed(("Couldn't find free spot!\n"));
1502 return VERR_PDM_TOO_PCI_MANY_DEVICES;
1503 }
1504 } /* if conflict */
1505 pPciDev->Int.s.fRequestedDevFn = true;
1506 }
1507
1508 Assert(!pBus->devices[iDev]);
1509 pPciDev->devfn = iDev;
1510 pPciDev->name = pszName;
1511 pPciDev->Int.s.pBusR3 = pBus;
1512 pPciDev->Int.s.pBusR0 = MMHyperR3ToR0(PDMDevHlpGetVM(pBus->CTX_SUFF(pDevIns)), pBus);
1513 pPciDev->Int.s.pBusRC = MMHyperR3ToRC(PDMDevHlpGetVM(pBus->CTX_SUFF(pDevIns)), pBus);
1514 pPciDev->Int.s.pfnConfigRead = pci_default_read_config;
1515 pPciDev->Int.s.pfnConfigWrite = pci_default_write_config;
1516 pBus->devices[iDev] = pPciDev;
1517 if (pPciDev->Int.s.fPciToPciBridge)
1518 {
1519 AssertMsg(pBus->cBridges < RT_ELEMENTS(pBus->devices), ("Number of bridges exceeds the number of possible devices on the bus\n"));
1520 AssertMsg(pPciDev->Int.s.pfnBridgeConfigRead && pPciDev->Int.s.pfnBridgeConfigWrite,
1521 ("device is a bridge but does not implement read/write functions\n"));
1522 pBus->papBridgesR3[pBus->cBridges] = pPciDev;
1523 pBus->cBridges++;
1524 }
1525
1526 Log(("PCI: Registered device %d function %d (%#x) '%s'.\n",
1527 iDev >> 3, iDev & 7, 0x80000000 | (iDev << 8), pszName));
1528
1529 return VINF_SUCCESS;
1530}
1531
1532
1533/**
1534 * Registers the device with the default PCI bus.
1535 *
1536 * @returns VBox status code.
1537 * @param pDevIns Device instance of the PCI Bus.
1538 * @param pPciDev The PCI device structure.
1539 * Any PCI enabled device must keep this in it's instance data!
1540 * Fill in the PCI data config before registration, please.
1541 * @param pszName Pointer to device name (permanent, readonly). For debugging, not unique.
1542 * @param iDev The PCI device number. Use a negative value for auto assigning one.
1543 */
1544static DECLCALLBACK(int) pciRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, const char *pszName, int iDev)
1545{
1546 PPCIBUS pBus = DEVINS_2_PCIBUS(pDevIns);
1547
1548 /*
1549 * Check input.
1550 */
1551 if ( !pszName
1552 || !pPciDev
1553 || iDev >= (int)RT_ELEMENTS(pBus->devices)
1554 || (iDev >= 0 && iDev <= 8))
1555 {
1556 AssertMsgFailed(("Invalid argument! pszName=%s pPciDev=%p iDev=%d\n", pszName, pPciDev, iDev));
1557 return VERR_INVALID_PARAMETER;
1558 }
1559
1560 /*
1561 * Register the device.
1562 */
1563 return pciRegisterInternal(pBus, iDev, pPciDev, pszName);
1564}
1565
1566
1567static DECLCALLBACK(int) pciIORegionRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iRegion, uint32_t cbRegion, PCIADDRESSSPACE enmType, PFNPCIIOREGIONMAP pfnCallback)
1568{
1569 /*
1570 * Validate.
1571 */
1572 if ( enmType != PCI_ADDRESS_SPACE_MEM
1573 && enmType != PCI_ADDRESS_SPACE_IO
1574 && enmType != PCI_ADDRESS_SPACE_MEM_PREFETCH)
1575 {
1576 AssertMsgFailed(("Invalid enmType=%#x? Or was this a bitmask after all...\n", enmType));
1577 return VERR_INVALID_PARAMETER;
1578 }
1579 if ((unsigned)iRegion >= PCI_NUM_REGIONS)
1580 {
1581 AssertMsgFailed(("Invalid iRegion=%d PCI_NUM_REGIONS=%d\n", iRegion, PCI_NUM_REGIONS));
1582 return VERR_INVALID_PARAMETER;
1583 }
1584
1585 /*
1586 * Register the I/O region.
1587 */
1588 PPCIIOREGION pRegion = &pPciDev->Int.s.aIORegions[iRegion];
1589 pRegion->addr = ~0U;
1590 pRegion->size = cbRegion;
1591 pRegion->type = enmType;
1592 pRegion->map_func = pfnCallback;
1593
1594 /* Set type in the config space. */
1595 uint32_t u32Address = 0x10 + iRegion * 4;
1596 uint32_t u32Value = (enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH ? (1 << 3) : 0)
1597 | (enmType == PCI_ADDRESS_SPACE_IO ? 1 : 0);
1598 *(uint32_t *)(pPciDev->config + u32Address) = RT_H2LE_U32(u32Value);
1599
1600 return VINF_SUCCESS;
1601}
1602
1603
1604/**
1605 * @copydoc PDMPCIBUSREG::pfnSetConfigCallbacksR3
1606 */
1607static DECLCALLBACK(void) pciSetConfigCallbacks(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, PFNPCICONFIGREAD pfnRead, PPFNPCICONFIGREAD ppfnReadOld,
1608 PFNPCICONFIGWRITE pfnWrite, PPFNPCICONFIGWRITE ppfnWriteOld)
1609{
1610 if (ppfnReadOld)
1611 *ppfnReadOld = pPciDev->Int.s.pfnConfigRead;
1612 pPciDev->Int.s.pfnConfigRead = pfnRead;
1613
1614 if (ppfnWriteOld)
1615 *ppfnWriteOld = pPciDev->Int.s.pfnConfigWrite;
1616 pPciDev->Int.s.pfnConfigWrite = pfnWrite;
1617}
1618
1619
1620/**
1621 * Called to perform the job of the bios.
1622 *
1623 * @returns VBox status.
1624 * @param pDevIns Device instance of the first bus.
1625 */
1626static DECLCALLBACK(int) pciFakePCIBIOS(PPDMDEVINS pDevIns)
1627{
1628 int rc;
1629 unsigned i;
1630 uint8_t elcr[2] = {0, 0};
1631 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1632 PVM pVM = PDMDevHlpGetVM(pDevIns);
1633 Assert(pVM);
1634
1635 /*
1636 * Set the start addresses.
1637 */
1638 pGlobals->pci_bios_io_addr = 0xc000;
1639 pGlobals->pci_bios_mem_addr = UINT32_C(0xf0000000);
1640 pGlobals->uBus = 0;
1641
1642 /*
1643 * Activate IRQ mappings.
1644 */
1645 for (i = 0; i < 4; i++)
1646 {
1647 uint8_t irq = pci_irqs[i];
1648 /* Set to trigger level. */
1649 elcr[irq >> 3] |= (1 << (irq & 7));
1650 /* Activate irq remapping in PIIX3. */
1651 pci_config_writeb(pGlobals, 0, pGlobals->PIIX3State.dev.devfn, 0x60 + i, irq);
1652 }
1653
1654 /* Tell to the PIC. */
1655 rc = IOMIOPortWrite(pVM, 0x4d0, elcr[0], sizeof(uint8_t));
1656 if (rc == VINF_SUCCESS)
1657 rc = IOMIOPortWrite(pVM, 0x4d1, elcr[1], sizeof(uint8_t));
1658 if (rc != VINF_SUCCESS)
1659 {
1660 AssertMsgFailed(("Writing to PIC failed!\n"));
1661 return RT_SUCCESS(rc) ? VERR_INTERNAL_ERROR : rc;
1662 }
1663
1664 /*
1665 * Init the devices.
1666 */
1667 for (i = 0; i < 256; i++)
1668 {
1669 uint8_t aBridgePositions[256];
1670
1671 memset(aBridgePositions, 0, sizeof(aBridgePositions));
1672 Log2(("PCI: Initializing device %d (%#x)\n",
1673 i, 0x80000000 | (i << 8)));
1674 pci_bios_init_device(pGlobals, 0, i, 0, aBridgePositions);
1675 }
1676
1677 return VINF_SUCCESS;
1678}
1679
1680/**
1681 * @copydoc FNPDMDEVRELOCATE
1682 */
1683static DECLCALLBACK(void) pciRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1684{
1685 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1686 PPCIBUS pBus = &pGlobals->PciBus;
1687 pGlobals->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1688
1689 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
1690 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1691
1692 /* Relocate RC pointers for the attached pci devices. */
1693 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->devices); i++)
1694 {
1695 if (pBus->devices[i])
1696 pBus->devices[i]->Int.s.pBusRC += offDelta;
1697 }
1698}
1699
1700
1701/**
1702 * Construct a host to PCI Bus device instance for a VM.
1703 *
1704 * @returns VBox status.
1705 * @param pDevIns The device instance data.
1706 * If the registration structure is needed, pDevIns->pDevReg points to it.
1707 * @param iInstance Instance number. Use this to figure out which registers and such to use.
1708 * The device number is also found in pDevIns->iInstance, but since it's
1709 * likely to be freqently used PDM passes it as parameter.
1710 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
1711 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
1712 * iInstance it's expected to be used a bit in this function.
1713 */
1714static DECLCALLBACK(int) pciConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
1715{
1716 int rc;
1717 Assert(iInstance == 0);
1718
1719 /*
1720 * Validate and read configuration.
1721 */
1722 if (!CFGMR3AreValuesValid(pCfgHandle, "IOAPIC\0" "GCEnabled\0" "R0Enabled\0"))
1723 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
1724
1725 /* query whether we got an IOAPIC */
1726 bool fUseIoApic;
1727 rc = CFGMR3QueryBoolDef(pCfgHandle, "IOAPIC", &fUseIoApic, false);
1728 if (RT_FAILURE(rc))
1729 return PDMDEV_SET_ERROR(pDevIns, rc,
1730 N_("Configuration error: Failed to query boolean value \"IOAPIC\""));
1731
1732 /* check if RC code is enabled. */
1733 bool fGCEnabled;
1734 rc = CFGMR3QueryBoolDef(pCfgHandle, "GCEnabled", &fGCEnabled, true);
1735 if (RT_FAILURE(rc))
1736 return PDMDEV_SET_ERROR(pDevIns, rc,
1737 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
1738
1739 /* check if R0 code is enabled. */
1740 bool fR0Enabled;
1741 rc = CFGMR3QueryBoolDef(pCfgHandle, "R0Enabled", &fR0Enabled, true);
1742 if (RT_FAILURE(rc))
1743 return PDMDEV_SET_ERROR(pDevIns, rc,
1744 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
1745 Log(("PCI: fUseIoApic=%RTbool fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fUseIoApic, fGCEnabled, fR0Enabled));
1746
1747 /*
1748 * Init data and register the PCI bus.
1749 */
1750 PPCIGLOBALS pGlobals = PDMINS_2_DATA(pDevIns, PPCIGLOBALS);
1751 pGlobals->pci_bios_io_addr = 0xc000;
1752 pGlobals->pci_bios_mem_addr = 0xf0000000;
1753 memset((void *)&pGlobals->pci_irq_levels, 0, sizeof(pGlobals->pci_irq_levels));
1754 pGlobals->fUseIoApic = fUseIoApic;
1755 memset((void *)&pGlobals->pci_apic_irq_levels, 0, sizeof(pGlobals->pci_apic_irq_levels));
1756
1757 pGlobals->pDevInsR3 = pDevIns;
1758 pGlobals->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
1759 pGlobals->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1760
1761 pGlobals->PciBus.pDevInsR3 = pDevIns;
1762 pGlobals->PciBus.pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
1763 pGlobals->PciBus.pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1764 pGlobals->PciBus.papBridgesR3 = (PPCIDEVICE *)PDMDevHlpMMHeapAllocZ(pDevIns, sizeof(PPCIDEVICE) * RT_ELEMENTS(pGlobals->PciBus.devices));
1765
1766 PDMPCIBUSREG PciBusReg;
1767 PPCIBUS pBus = &pGlobals->PciBus;
1768 PciBusReg.u32Version = PDM_PCIBUSREG_VERSION;
1769 PciBusReg.pfnRegisterR3 = pciRegister;
1770 PciBusReg.pfnIORegionRegisterR3 = pciIORegionRegister;
1771 PciBusReg.pfnSetConfigCallbacksR3 = pciSetConfigCallbacks;
1772 PciBusReg.pfnSetIrqR3 = pciSetIrq;
1773 PciBusReg.pfnSaveExecR3 = pciGenericSaveExec;
1774 PciBusReg.pfnLoadExecR3 = pciGenericLoadExec;
1775 PciBusReg.pfnFakePCIBIOSR3 = pciFakePCIBIOS;
1776 PciBusReg.pszSetIrqRC = fGCEnabled ? "pciSetIrq" : NULL;
1777 PciBusReg.pszSetIrqR0 = fR0Enabled ? "pciSetIrq" : NULL;
1778 rc = pDevIns->pDevHlpR3->pfnPCIBusRegister(pDevIns, &PciBusReg, &pBus->pPciHlpR3);
1779 if (RT_FAILURE(rc))
1780 return PDMDEV_SET_ERROR(pDevIns, rc,
1781 N_("Failed to register ourselves as a PCI Bus"));
1782 if (pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION)
1783 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
1784 N_("PCI helper version mismatch; got %#x expected %#x"),
1785 pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION);
1786
1787 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
1788 pBus->pPciHlpR0 = pBus->pPciHlpR3->pfnGetR0Helpers(pDevIns);
1789
1790 /*
1791 * Fill in PCI configs and add them to the bus.
1792 */
1793 /* i440FX */
1794 PCIDevSetVendorId( &pBus->PciDev, 0x8086); /* Intel */
1795 PCIDevSetDeviceId( &pBus->PciDev, 0x1237);
1796 PCIDevSetRevisionId(&pBus->PciDev, 0x02);
1797 PCIDevSetClassSub( &pBus->PciDev, 0x00); /* host2pci */
1798 PCIDevSetClassBase( &pBus->PciDev, 0x06); /* PCI_bridge */
1799 PCIDevSetHeaderType(&pBus->PciDev, 0x00);
1800
1801 pBus->PciDev.pDevIns = pDevIns;
1802 pBus->PciDev.Int.s.fRequestedDevFn= true;
1803 pciRegisterInternal(pBus, 0, &pBus->PciDev, "i440FX");
1804
1805 /* PIIX3 */
1806 PCIDevSetVendorId( &pGlobals->PIIX3State.dev, 0x8086); /* Intel */
1807 PCIDevSetDeviceId( &pGlobals->PIIX3State.dev, 0x7000); /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
1808 PCIDevSetClassSub( &pGlobals->PIIX3State.dev, 0x01); /* PCI_ISA */
1809 PCIDevSetClassBase( &pGlobals->PIIX3State.dev, 0x06); /* PCI_bridge */
1810 PCIDevSetHeaderType(&pGlobals->PIIX3State.dev, 0x80); /* PCI_multifunction, generic */
1811
1812 pGlobals->PIIX3State.dev.pDevIns = pDevIns;
1813 pGlobals->PIIX3State.dev.Int.s.fRequestedDevFn= true;
1814 pciRegisterInternal(pBus, 8, &pGlobals->PIIX3State.dev, "PIIX3");
1815 piix3_reset(&pGlobals->PIIX3State);
1816
1817 pBus->iDevSearch = 16;
1818
1819 /*
1820 * Register I/O ports and save state.
1821 */
1822 rc = PDMDevHlpIOPortRegister(pDevIns, 0x0cf8, 1, NULL, pciIOPortAddressWrite, pciIOPortAddressRead, NULL, NULL, "i440FX (PCI)");
1823 if (RT_FAILURE(rc))
1824 return rc;
1825 rc = PDMDevHlpIOPortRegister(pDevIns, 0x0cfc, 4, NULL, pciIOPortDataWrite, pciIOPortDataRead, NULL, NULL, "i440FX (PCI)");
1826 if (RT_FAILURE(rc))
1827 return rc;
1828 rc = PDMDevHlpSSMRegister(pDevIns, "pci", iInstance, VBOX_PCI_SAVED_STATE_VERSION, sizeof(*pBus),
1829 NULL, pciSaveExec, NULL, NULL, pciLoadExec, NULL);
1830 if (RT_FAILURE(rc))
1831 return rc;
1832
1833 return VINF_SUCCESS;
1834}
1835
1836
1837/**
1838 * The device registration structure.
1839 */
1840const PDMDEVREG g_DevicePCI =
1841{
1842 /* u32Version */
1843 PDM_DEVREG_VERSION,
1844 /* szDeviceName */
1845 "pci",
1846 /* szRCMod */
1847 "VBoxDDGC.gc",
1848 /* szR0Mod */
1849 "VBoxDDR0.r0",
1850 /* pszDescription */
1851 "i440FX PCI bridge and PIIX3 ISA bridge.",
1852 /* fFlags */
1853 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
1854 /* fClass */
1855 PDM_DEVREG_CLASS_BUS_PCI | PDM_DEVREG_CLASS_BUS_ISA,
1856 /* cMaxInstances */
1857 1,
1858 /* cbInstance */
1859 sizeof(PCIGLOBALS),
1860 /* pfnConstruct */
1861 pciConstruct,
1862 /* pfnDestruct */
1863 NULL,
1864 /* pfnRelocate */
1865 pciRelocate,
1866 /* pfnIOCtl */
1867 NULL,
1868 /* pfnPowerOn */
1869 NULL,
1870 /* pfnReset */
1871 NULL,
1872 /* pfnSuspend */
1873 NULL,
1874 /* pfnResume */
1875 NULL,
1876 /* pfnAttach */
1877 NULL,
1878 /* pfnDetach */
1879 NULL,
1880 /* pfnQueryInterface */
1881 NULL,
1882 /* pfnInitComplete */
1883 NULL,
1884 /* pfnPowerOff */
1885 NULL,
1886 /* pfnSoftReset */
1887 NULL,
1888 /* u32VersionEnd */
1889 PDM_DEVREG_VERSION
1890
1891};
1892#endif /* IN_RING3 */
1893
1894/**
1895 * Set the IRQ for a PCI device on a secondary bus.
1896 *
1897 * @param pDevIns Device instance of the PCI Bus.
1898 * @param pPciDev The PCI device structure.
1899 * @param iIrq IRQ number to set.
1900 * @param iLevel IRQ level.
1901 */
1902PDMBOTHCBDECL(void) pcibridgeSetIrq(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, int iIrq, int iLevel)
1903{
1904 /*
1905 * The PCI-to-PCI bridge specification defines how the interrupt pins
1906 * are routed from the secondary to the primary bus (see chapter 9).
1907 * iIrq gives the interrupt pin the pci device asserted.
1908 * We change iIrq here according to the spec and call the SetIrq function
1909 * of our parent passing the device which asserted the interrupt instead of the device of the bridge.
1910 */
1911 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
1912 int iIrqPinBridge = 0;
1913 uint8_t uDevFnBridge = pPciDev->devfn;
1914
1915 /* Walk the chain until we reach the host bus. */
1916 while (pBus->iBus != 0)
1917 {
1918 uDevFnBridge = pBus->PciDev.devfn;
1919 iIrqPinBridge = ((uDevFnBridge >> 3) + iIrqPinBridge) & 3;
1920 /* Get the parent. */
1921 pBus = pBus->PciDev.Int.s.CTX_SUFF(pBus);
1922 }
1923
1924 AssertMsg(pBus->iBus == 0, ("This is not the host pci bus iBus=%d\n", pBus->iBus));
1925 pciSetIrqInternal(PCIBUS_2_PCIGLOBALS(pBus), uDevFnBridge, pPciDev, iIrqPinBridge, iLevel);
1926}
1927
1928#ifdef IN_RING3
1929
1930static void pcibridgeConfigWrite(PPDMDEVINSR3 pDevIns, uint8_t iBus, uint8_t iDevice, uint32_t u32Address, uint32_t u32Value, unsigned cb)
1931{
1932 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
1933
1934 LogFlowFunc((": pDevIns=%p iBus=%d iDevice=%d u32Address=%u u32Value=%u cb=%d\n", pDevIns, iBus, iDevice, u32Address, u32Value, cb));
1935
1936 /* If the current bus is not the target bus search for the bus which contains the device. */
1937 if (iBus != pBus->PciDev.config[VBOX_PCI_SECONDARY_BUS])
1938 {
1939 PPCIDEVICE pBridgeDevice = pciFindBridge(pBus, iBus);
1940 if (pBridgeDevice)
1941 {
1942 AssertPtr(pBridgeDevice->Int.s.pfnBridgeConfigWrite);
1943 pBridgeDevice->Int.s.pfnBridgeConfigWrite(pBridgeDevice->pDevIns, iBus, iDevice, u32Address, u32Value, cb);
1944 }
1945 }
1946 else
1947 {
1948 /* This is the target bus, pass the write to the device. */
1949 PPCIDEVICE pPciDev = pBus->devices[iDevice];
1950 if (pPciDev)
1951 {
1952 Log(("%s: %s: addr=%02x val=%08x len=%d\n", __FUNCTION__, pPciDev->name, u32Address, u32Value, cb));
1953 pPciDev->Int.s.pfnConfigWrite(pPciDev, u32Address, u32Value, cb);
1954 }
1955 }
1956}
1957
1958static uint32_t pcibridgeConfigRead(PPDMDEVINSR3 pDevIns, uint8_t iBus, uint8_t iDevice, uint32_t u32Address, unsigned cb)
1959{
1960 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
1961 uint32_t u32Value = 0xffffffff; /* Return value in case there is no device. */
1962
1963 LogFlowFunc((": pDevIns=%p iBus=%d iDevice=%d u32Address=%u cb=%d\n", pDevIns, iBus, iDevice, u32Address, cb));
1964
1965 /* If the current bus is not the target bus search for the bus which contains the device. */
1966 if (iBus != pBus->PciDev.config[VBOX_PCI_SECONDARY_BUS])
1967 {
1968 PPCIDEVICE pBridgeDevice = pciFindBridge(pBus, iBus);
1969 if (pBridgeDevice)
1970 {
1971 AssertPtr( pBridgeDevice->Int.s.pfnBridgeConfigRead);
1972 u32Value = pBridgeDevice->Int.s.pfnBridgeConfigRead(pBridgeDevice->pDevIns, iBus, iDevice, u32Address, cb);
1973 }
1974 }
1975 else
1976 {
1977 /* This is the target bus, pass the read to the device. */
1978 PPCIDEVICE pPciDev = pBus->devices[iDevice];
1979 if (pPciDev)
1980 {
1981 u32Value = pPciDev->Int.s.pfnConfigRead(pPciDev, u32Address, cb);
1982 Log(("%s: %s: u32Address=%02x u32Value=%08x cb=%d\n", __FUNCTION__, pPciDev->name, u32Address, u32Value, cb));
1983 }
1984 }
1985
1986 return u32Value;
1987}
1988
1989/**
1990 * Saves a state of a PCI bridge device.
1991 *
1992 * @returns VBox status code.
1993 * @param pDevIns The device instance.
1994 * @param pPciDev Pointer to PCI device.
1995 * @param pSSMHandle The handle to save the state to.
1996 */
1997static DECLCALLBACK(int) pcibridgeSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1998{
1999 uint32_t i;
2000 PPCIBUS pThis = PDMINS_2_DATA(pDevIns, PPCIBUS);
2001
2002 /*
2003 * Iterate all the devices.
2004 */
2005 for (i = 0; i < RT_ELEMENTS(pThis->devices); i++)
2006 {
2007 PPCIDEVICE pDev = pThis->devices[i];
2008 if (pDev)
2009 {
2010 int rc;
2011 SSMR3PutU32(pSSMHandle, i);
2012 SSMR3PutMem(pSSMHandle, pDev->config, sizeof(pDev->config));
2013
2014 rc = SSMR3PutS32(pSSMHandle, pDev->Int.s.uIrqPinState);
2015 if (RT_FAILURE(rc))
2016 return rc;
2017 }
2018 }
2019 return SSMR3PutU32(pSSMHandle, ~0); /* terminator */
2020}
2021
2022/**
2023 * Loads a saved PCI bridge device state.
2024 *
2025 * @returns VBox status code.
2026 * @param pDevIns The device instance.
2027 * @param pSSMHandle The handle to the saved state.
2028 * @param u32Version The data unit version number.
2029 */
2030static DECLCALLBACK(int) pcibridgeLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
2031{
2032 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2033 uint32_t u32;
2034 uint32_t i;
2035 int rc;
2036
2037 /*
2038 * Check the version.
2039 */
2040 if (u32Version > VBOX_PCI_SAVED_STATE_VERSION)
2041 {
2042 AssertFailed();
2043 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2044 }
2045
2046 /*
2047 * Iterate all the devices.
2048 */
2049 for (i = 0;; i++)
2050 {
2051 PCIDEVICE DevTmp;
2052 PPCIDEVICE pDev;
2053
2054 /* index / terminator */
2055 rc = SSMR3GetU32(pSSMHandle, &u32);
2056 if (RT_FAILURE(rc))
2057 return rc;
2058 if (u32 == (uint32_t)~0)
2059 break;
2060 if ( u32 >= RT_ELEMENTS(pBus->devices)
2061 || u32 < i)
2062 {
2063 AssertMsgFailed(("u32=%#x i=%#x\n", u32, i));
2064 return rc;
2065 }
2066
2067 /* skip forward to the device checking that no new devices are present. */
2068 for (; i < u32; i++)
2069 {
2070 if (pBus->devices[i])
2071 {
2072 LogRel(("New device in slot %#x, %s (vendor=%#06x device=%#06x)\n", i, pBus->devices[i]->name,
2073 PCIDevGetVendorId(pBus->devices[i]), PCIDevGetDeviceId(pBus->devices[i])));
2074 if (SSMR3HandleGetAfter(pSSMHandle) != SSMAFTER_DEBUG_IT)
2075 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
2076 }
2077 }
2078
2079 /* get the data */
2080 DevTmp.Int.s.uIrqPinState = 0;
2081 SSMR3GetMem(pSSMHandle, DevTmp.config, sizeof(DevTmp.config));
2082 rc = SSMR3GetS32(pSSMHandle, &DevTmp.Int.s.uIrqPinState);
2083 if (RT_FAILURE(rc))
2084 return rc;
2085
2086 /* check that it's still around. */
2087 pDev = pBus->devices[i];
2088 if (!pDev)
2089 {
2090 LogRel(("Device in slot %#x has been removed! vendor=%#06x device=%#06x\n", i,
2091 PCIDevGetVendorId(&DevTmp), PCIDevGetDeviceId(&DevTmp)));
2092 if (SSMR3HandleGetAfter(pSSMHandle) != SSMAFTER_DEBUG_IT)
2093 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
2094 continue;
2095 }
2096
2097 /* match the vendor id assuming that this will never be changed. */
2098 if ( DevTmp.config[0] != pDev->config[0]
2099 || DevTmp.config[1] != pDev->config[1])
2100 {
2101 LogRel(("Device in slot %#x (%s) vendor id mismatch! saved=%.4Rhxs current=%.4Rhxs\n",
2102 i, pDev->name, DevTmp.config, pDev->config));
2103 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
2104 }
2105
2106 /* commit the loaded device config. */
2107 memcpy(pDev->config, DevTmp.config, sizeof(pDev->config));
2108
2109 pDev->Int.s.uIrqPinState = DevTmp.Int.s.uIrqPinState;
2110 }
2111
2112 return VINF_SUCCESS;
2113}
2114
2115/**
2116 * @copydoc FNPDMDEVRESET
2117 */
2118static DECLCALLBACK(void) pcibridgeReset(PPDMDEVINS pDevIns)
2119{
2120 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2121
2122 /* Reset config space to default values. */
2123 pBus->PciDev.config[VBOX_PCI_PRIMARY_BUS] = 0;
2124 pBus->PciDev.config[VBOX_PCI_SECONDARY_BUS] = 0;
2125 pBus->PciDev.config[VBOX_PCI_SUBORDINATE_BUS] = 0;
2126}
2127
2128/**
2129 * @copydoc FNPDMDEVRELOCATE
2130 */
2131static DECLCALLBACK(void) pcibridgeRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2132{
2133 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2134 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2135
2136 /* Relocate RC pointers for the attached pci devices. */
2137 for (uint32_t i = 0; i < RT_ELEMENTS(pBus->devices); i++)
2138 {
2139 if (pBus->devices[i])
2140 pBus->devices[i]->Int.s.pBusRC += offDelta;
2141 }
2142}
2143
2144/**
2145 * Registers the device with the default PCI bus.
2146 *
2147 * @returns VBox status code.
2148 * @param pDevIns Device instance of the PCI Bus.
2149 * @param pPciDev The PCI device structure.
2150 * Any PCI enabled device must keep this in it's instance data!
2151 * Fill in the PCI data config before registration, please.
2152 * @param pszName Pointer to device name (permanent, readonly). For debugging, not unique.
2153 * @param iDev The PCI device number. Use a negative value for auto assigning one.
2154 */
2155static DECLCALLBACK(int) pcibridgeRegister(PPDMDEVINS pDevIns, PPCIDEVICE pPciDev, const char *pszName, int iDev)
2156{
2157 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2158
2159 /*
2160 * Check input.
2161 */
2162 if ( !pszName
2163 || !pPciDev
2164 || iDev >= (int)RT_ELEMENTS(pBus->devices))
2165 {
2166 AssertMsgFailed(("Invalid argument! pszName=%s pPciDev=%p iDev=%d\n", pszName, pPciDev, iDev));
2167 return VERR_INVALID_PARAMETER;
2168 }
2169
2170 /*
2171 * Register the device.
2172 */
2173 return pciRegisterInternal(pBus, iDev, pPciDev, pszName);
2174}
2175
2176/**
2177 * Construct a PCI bridge device instance for a VM.
2178 *
2179 * @returns VBox status.
2180 * @param pDevIns The device instance data.
2181 * If the registration structure is needed, pDevIns->pDevReg points to it.
2182 * @param iInstance Instance number. Use this to figure out which registers and such to use.
2183 * The device number is also found in pDevIns->iInstance, but since it's
2184 * likely to be freqently used PDM passes it as parameter.
2185 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
2186 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
2187 * iInstance it's expected to be used a bit in this function.
2188 */
2189static DECLCALLBACK(int) pcibridgeConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
2190{
2191 int rc;
2192
2193 /*
2194 * Validate and read configuration.
2195 */
2196 if (!CFGMR3AreValuesValid(pCfgHandle, "GCEnabled\0" "R0Enabled\0"))
2197 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
2198
2199 /* check if RC code is enabled. */
2200 bool fGCEnabled;
2201 rc = CFGMR3QueryBoolDef(pCfgHandle, "GCEnabled", &fGCEnabled, true);
2202 if (RT_FAILURE(rc))
2203 return PDMDEV_SET_ERROR(pDevIns, rc,
2204 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
2205
2206 /* check if R0 code is enabled. */
2207 bool fR0Enabled;
2208 rc = CFGMR3QueryBoolDef(pCfgHandle, "R0Enabled", &fR0Enabled, true);
2209 if (RT_FAILURE(rc))
2210 return PDMDEV_SET_ERROR(pDevIns, rc,
2211 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
2212 Log(("PCI: fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fGCEnabled, fR0Enabled));
2213
2214 /*
2215 * Init data and register the PCI bus.
2216 */
2217 PPCIBUS pBus = PDMINS_2_DATA(pDevIns, PPCIBUS);
2218 pBus->pDevInsR3 = pDevIns;
2219 pBus->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2220 pBus->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2221 pBus->papBridgesR3 = (PPCIDEVICE *)PDMDevHlpMMHeapAllocZ(pDevIns, sizeof(PPCIDEVICE) * RT_ELEMENTS(pBus->devices));
2222
2223 PDMPCIBUSREG PciBusReg;
2224 PciBusReg.u32Version = PDM_PCIBUSREG_VERSION;
2225 PciBusReg.pfnRegisterR3 = pcibridgeRegister;
2226 PciBusReg.pfnIORegionRegisterR3 = pciIORegionRegister;
2227 PciBusReg.pfnSetConfigCallbacksR3 = pciSetConfigCallbacks;
2228 PciBusReg.pfnSetIrqR3 = pcibridgeSetIrq;
2229 PciBusReg.pfnSaveExecR3 = pciGenericSaveExec;
2230 PciBusReg.pfnLoadExecR3 = pciGenericLoadExec;
2231 PciBusReg.pfnFakePCIBIOSR3 = NULL; /* Only needed for the first bus. */
2232 PciBusReg.pszSetIrqRC = fGCEnabled ? "pcibridgeSetIrq" : NULL;
2233 PciBusReg.pszSetIrqR0 = fR0Enabled ? "pcibridgeSetIrq" : NULL;
2234 rc = pDevIns->pDevHlpR3->pfnPCIBusRegister(pDevIns, &PciBusReg, &pBus->pPciHlpR3);
2235 if (RT_FAILURE(rc))
2236 return PDMDEV_SET_ERROR(pDevIns, rc,
2237 N_("Failed to register ourselves as a PCI Bus"));
2238 if (pBus->pPciHlpR3->u32Version != PDM_PCIHLPR3_VERSION)
2239 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2240 N_("PCI helper version mismatch; got %#x expected %#x"),
2241 pBus->pPciHlpR3->u32Version, PDM_PCIHLPR3_VERSION);
2242
2243 pBus->pPciHlpRC = pBus->pPciHlpR3->pfnGetRCHelpers(pDevIns);
2244 pBus->pPciHlpR0 = pBus->pPciHlpR3->pfnGetR0Helpers(pDevIns);
2245
2246 /*
2247 * Fill in PCI configs and add them to the bus.
2248 */
2249 PCIDevSetVendorId( &pBus->PciDev, 0x8086); /* Intel */
2250 PCIDevSetDeviceId( &pBus->PciDev, 0x2448); /* 82801 Mobile PCI bridge. */
2251 PCIDevSetRevisionId(&pBus->PciDev, 0xf2);
2252 PCIDevSetClassSub( &pBus->PciDev, 0x04); /* pci2pci */
2253 PCIDevSetClassBase( &pBus->PciDev, 0x06); /* PCI_bridge */
2254 PCIDevSetClassProg( &pBus->PciDev, 0x01); /* Supports subtractive decoding. */
2255 PCIDevSetHeaderType(&pBus->PciDev, 0x01); /* Single function device which adheres to the PCI-to-PCI bridge spec. */
2256 PCIDevSetCommand( &pBus->PciDev, 0x00);
2257 PCIDevSetStatus( &pBus->PciDev, 0x20); /* 66MHz Capable. */
2258 PCIDevSetInterruptLine(&pBus->PciDev, 0x00); /* This device does not assert interrupts. */
2259
2260 /*
2261 * This device does not generate interrupts. Interrupt delivery from
2262 * devices attached to the bus is unaffected.
2263 */
2264 PCIDevSetInterruptPin (&pBus->PciDev, 0x00);
2265
2266 pBus->PciDev.pDevIns = pDevIns;
2267 pBus->PciDev.Int.s.fPciToPciBridge = true;
2268 pBus->PciDev.Int.s.pfnBridgeConfigRead = pcibridgeConfigRead;
2269 pBus->PciDev.Int.s.pfnBridgeConfigWrite = pcibridgeConfigWrite;
2270
2271 /*
2272 * Register this PCI bridge. The called function will take care on which bus we will get registered.
2273 */
2274 rc = PDMDevHlpPCIRegister (pDevIns, &pBus->PciDev);
2275 if (RT_FAILURE(rc))
2276 return rc;
2277
2278 pBus->iDevSearch = 0;
2279 /*
2280 * The iBus property doesn't really represent the bus number
2281 * because the guest and the BIOS can choose different bus numbers
2282 * for them.
2283 * The bus number is mainly for the setIrq function to indicate
2284 * when the host bus is reached which will have iBus = 0.
2285 * Thathswhy the + 1.
2286 */
2287 pBus->iBus = iInstance + 1;
2288
2289 /*
2290 * Register SSM handlers. We use the same saved state version as for the host bridge
2291 * to make changes easier.
2292 */
2293 rc = PDMDevHlpSSMRegister(pDevIns, "pcibridge", iInstance, VBOX_PCI_SAVED_STATE_VERSION, sizeof(*pBus),
2294 NULL, pcibridgeSaveExec, NULL, NULL, pcibridgeLoadExec, NULL);
2295 if (RT_FAILURE(rc))
2296 return rc;
2297
2298 return VINF_SUCCESS;
2299}
2300
2301/**
2302 * The device registration structure
2303 * for the PCI-to-PCI bridge.
2304 */
2305const PDMDEVREG g_DevicePCIBridge =
2306{
2307 /* u32Version */
2308 PDM_DEVREG_VERSION,
2309 /* szDeviceName */
2310 "pcibridge",
2311 /* szRCMod */
2312 "VBoxDDGC.gc",
2313 /* szR0Mod */
2314 "VBoxDDR0.r0",
2315 /* pszDescription */
2316 "82801 Mobile PCI to PCI bridge",
2317 /* fFlags */
2318 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2319 /* fClass */
2320 PDM_DEVREG_CLASS_BUS_PCI,
2321 /* cMaxInstances */
2322 ~0,
2323 /* cbInstance */
2324 sizeof(PCIBUS),
2325 /* pfnConstruct */
2326 pcibridgeConstruct,
2327 /* pfnDestruct */
2328 NULL,
2329 /* pfnRelocate */
2330 pcibridgeRelocate,
2331 /* pfnIOCtl */
2332 NULL,
2333 /* pfnPowerOn */
2334 NULL,
2335 /* pfnReset */
2336 pcibridgeReset,
2337 /* pfnSuspend */
2338 NULL,
2339 /* pfnResume */
2340 NULL,
2341 /* pfnAttach */
2342 NULL,
2343 /* pfnDetach */
2344 NULL,
2345 /* pfnQueryInterface */
2346 NULL,
2347 /* pfnInitComplete */
2348 NULL,
2349 /* pfnPowerOff */
2350 NULL,
2351 /* pfnSoftReset */
2352 NULL,
2353 /* u32VersionEnd */
2354 PDM_DEVREG_VERSION
2355};
2356
2357#endif /* IN_RING3 */
2358#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

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