4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #define PCI_VENDOR_ID 0x00 /* 16 bits */
29 #define PCI_DEVICE_ID 0x02 /* 16 bits */
30 #define PCI_COMMAND 0x04 /* 16 bits */
31 #define PCI_COMMAND_IO 0x1 /* Enable response in I/O space */
32 #define PCI_COMMAND_MEMORY 0x2 /* Enable response in Memory space */
33 #define PCI_CLASS_DEVICE 0x0a /* Device class */
34 #define PCI_INTERRUPT_LINE 0x3c /* 8 bits */
35 #define PCI_INTERRUPT_PIN 0x3d /* 8 bits */
36 #define PCI_MIN_GNT 0x3e /* 8 bits */
37 #define PCI_MAX_LAT 0x3f /* 8 bits */
39 /* just used for simpler irq handling. */
40 #define PCI_DEVICES_MAX 64
41 #define PCI_IRQ_WORDS ((PCI_DEVICES_MAX + 31) / 32)
43 typedef struct PCIBridge
{
45 PCIDevice
**pci_bus
[256];
48 static PCIBridge pci_bridge
;
49 target_phys_addr_t pci_mem_base
;
50 static int pci_irq_index
;
51 static uint32_t pci_irq_levels
[4][PCI_IRQ_WORDS
];
53 /* -1 for devfn means auto assign */
54 PCIDevice
*pci_register_device(const char *name
, int instance_size
,
55 int bus_num
, int devfn
,
56 PCIConfigReadFunc
*config_read
,
57 PCIConfigWriteFunc
*config_write
)
59 PCIBridge
*s
= &pci_bridge
;
60 PCIDevice
*pci_dev
, **bus
;
62 if (pci_irq_index
>= PCI_DEVICES_MAX
)
65 if (!s
->pci_bus
[bus_num
]) {
66 s
->pci_bus
[bus_num
] = qemu_mallocz(256 * sizeof(PCIDevice
*));
67 if (!s
->pci_bus
[bus_num
])
70 bus
= s
->pci_bus
[bus_num
];
72 for(devfn
= 0 ; devfn
< 256; devfn
+= 8) {
79 pci_dev
= qemu_mallocz(instance_size
);
82 pci_dev
->bus_num
= bus_num
;
83 pci_dev
->devfn
= devfn
;
84 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
87 config_read
= pci_default_read_config
;
89 config_write
= pci_default_write_config
;
90 pci_dev
->config_read
= config_read
;
91 pci_dev
->config_write
= config_write
;
92 pci_dev
->irq_index
= pci_irq_index
++;
97 void pci_register_io_region(PCIDevice
*pci_dev
, int region_num
,
98 uint32_t size
, int type
,
99 PCIMapIORegionFunc
*map_func
)
103 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
105 r
= &pci_dev
->io_regions
[region_num
];
109 r
->map_func
= map_func
;
112 static void pci_addr_writel(void* opaque
, uint32_t addr
, uint32_t val
)
114 PCIBridge
*s
= opaque
;
118 static uint32_t pci_addr_readl(void* opaque
, uint32_t addr
)
120 PCIBridge
*s
= opaque
;
121 return s
->config_reg
;
124 static void pci_update_mappings(PCIDevice
*d
)
128 uint32_t last_addr
, new_addr
, config_ofs
;
130 cmd
= le16_to_cpu(*(uint16_t *)(d
->config
+ PCI_COMMAND
));
131 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
132 r
= &d
->io_regions
[i
];
133 if (i
== PCI_ROM_SLOT
) {
136 config_ofs
= 0x10 + i
* 4;
139 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
140 if (cmd
& PCI_COMMAND_IO
) {
141 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
143 new_addr
= new_addr
& ~(r
->size
- 1);
144 last_addr
= new_addr
+ r
->size
- 1;
145 /* NOTE: we have only 64K ioports on PC */
146 if (last_addr
<= new_addr
|| new_addr
== 0 ||
147 last_addr
>= 0x10000) {
154 if (cmd
& PCI_COMMAND_MEMORY
) {
155 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
157 /* the ROM slot has a specific enable bit */
158 if (i
== PCI_ROM_SLOT
&& !(new_addr
& 1))
160 new_addr
= new_addr
& ~(r
->size
- 1);
161 last_addr
= new_addr
+ r
->size
- 1;
162 /* NOTE: we do not support wrapping */
163 /* XXX: as we cannot support really dynamic
164 mappings, we handle specific values as invalid
166 if (last_addr
<= new_addr
|| new_addr
== 0 ||
175 /* now do the real mapping */
176 if (new_addr
!= r
->addr
) {
178 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
180 /* NOTE: specific hack for IDE in PC case:
181 only one byte must be mapped. */
182 class = d
->config
[0x0a] | (d
->config
[0x0b] << 8);
183 if (class == 0x0101 && r
->size
== 4) {
184 isa_unassign_ioport(r
->addr
+ 2, 1);
186 isa_unassign_ioport(r
->addr
, r
->size
);
189 cpu_register_physical_memory(r
->addr
+ pci_mem_base
,
196 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
203 uint32_t pci_default_read_config(PCIDevice
*d
,
204 uint32_t address
, int len
)
209 val
= d
->config
[address
];
212 val
= le16_to_cpu(*(uint16_t *)(d
->config
+ address
));
216 val
= le32_to_cpu(*(uint32_t *)(d
->config
+ address
));
222 void pci_default_write_config(PCIDevice
*d
,
223 uint32_t address
, uint32_t val
, int len
)
228 if (len
== 4 && ((address
>= 0x10 && address
< 0x10 + 4 * 6) ||
229 (address
>= 0x30 && address
< 0x34))) {
233 if ( address
>= 0x30 ) {
236 reg
= (address
- 0x10) >> 2;
238 r
= &d
->io_regions
[reg
];
241 /* compute the stored value */
242 if (reg
== PCI_ROM_SLOT
) {
243 /* keep ROM enable bit */
244 val
&= (~(r
->size
- 1)) | 1;
246 val
&= ~(r
->size
- 1);
249 *(uint32_t *)(d
->config
+ address
) = cpu_to_le32(val
);
250 pci_update_mappings(d
);
254 /* not efficient, but simple */
256 for(i
= 0; i
< len
; i
++) {
257 /* default read/write accesses */
276 d
->config
[addr
] = val
;
283 if (end
> PCI_COMMAND
&& address
< (PCI_COMMAND
+ 2)) {
284 /* if the command register is modified, we must modify the mappings */
285 pci_update_mappings(d
);
289 static void pci_data_write(void *opaque
, uint32_t addr
,
290 uint32_t val
, int len
)
292 PCIBridge
*s
= opaque
;
293 PCIDevice
**bus
, *pci_dev
;
296 #if defined(DEBUG_PCI) && 0
297 printf("pci_data_write: addr=%08x val=%08x len=%d\n",
298 s
->config_reg
, val
, len
);
300 if (!(s
->config_reg
& (1 << 31))) {
303 if ((s
->config_reg
& 0x3) != 0) {
306 bus
= s
->pci_bus
[(s
->config_reg
>> 16) & 0xff];
309 pci_dev
= bus
[(s
->config_reg
>> 8) & 0xff];
312 config_addr
= (s
->config_reg
& 0xfc) | (addr
& 3);
313 #if defined(DEBUG_PCI)
314 printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
315 pci_dev
->name
, config_addr
, val
, len
);
317 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
320 static uint32_t pci_data_read(void *opaque
, uint32_t addr
,
323 PCIBridge
*s
= opaque
;
324 PCIDevice
**bus
, *pci_dev
;
328 if (!(s
->config_reg
& (1 << 31)))
330 if ((s
->config_reg
& 0x3) != 0)
332 bus
= s
->pci_bus
[(s
->config_reg
>> 16) & 0xff];
335 pci_dev
= bus
[(s
->config_reg
>> 8) & 0xff];
352 config_addr
= (s
->config_reg
& 0xfc) | (addr
& 3);
353 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
354 #if defined(DEBUG_PCI)
355 printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
356 pci_dev
->name
, config_addr
, val
, len
);
359 #if defined(DEBUG_PCI) && 0
360 printf("pci_data_read: addr=%08x val=%08x len=%d\n",
361 s
->config_reg
, val
, len
);
366 static void pci_data_writeb(void* opaque
, uint32_t addr
, uint32_t val
)
368 pci_data_write(opaque
, addr
, val
, 1);
371 static void pci_data_writew(void* opaque
, uint32_t addr
, uint32_t val
)
373 pci_data_write(opaque
, addr
, val
, 2);
376 static void pci_data_writel(void* opaque
, uint32_t addr
, uint32_t val
)
378 pci_data_write(opaque
, addr
, val
, 4);
381 static uint32_t pci_data_readb(void* opaque
, uint32_t addr
)
383 return pci_data_read(opaque
, addr
, 1);
386 static uint32_t pci_data_readw(void* opaque
, uint32_t addr
)
388 return pci_data_read(opaque
, addr
, 2);
391 static uint32_t pci_data_readl(void* opaque
, uint32_t addr
)
393 return pci_data_read(opaque
, addr
, 4);
396 /* i440FX PCI bridge */
398 void i440fx_init(void)
400 PCIBridge
*s
= &pci_bridge
;
403 register_ioport_write(0xcf8, 4, 4, pci_addr_writel
, s
);
404 register_ioport_read(0xcf8, 4, 4, pci_addr_readl
, s
);
406 register_ioport_write(0xcfc, 4, 1, pci_data_writeb
, s
);
407 register_ioport_write(0xcfc, 4, 2, pci_data_writew
, s
);
408 register_ioport_write(0xcfc, 4, 4, pci_data_writel
, s
);
409 register_ioport_read(0xcfc, 4, 1, pci_data_readb
, s
);
410 register_ioport_read(0xcfc, 4, 2, pci_data_readw
, s
);
411 register_ioport_read(0xcfc, 4, 4, pci_data_readl
, s
);
413 d
= pci_register_device("i440FX", sizeof(PCIDevice
), 0, 0,
416 d
->config
[0x00] = 0x86; // vendor_id
417 d
->config
[0x01] = 0x80;
418 d
->config
[0x02] = 0x37; // device_id
419 d
->config
[0x03] = 0x12;
420 d
->config
[0x08] = 0x02; // revision
421 d
->config
[0x0a] = 0x04; // class_sub = pci2pci
422 d
->config
[0x0b] = 0x06; // class_base = PCI_bridge
423 d
->config
[0x0c] = 0x01; // line_size in 32 bit words
424 d
->config
[0x0e] = 0x01; // header_type
427 /* PIIX3 PCI to ISA bridge */
429 typedef struct PIIX3State
{
433 PIIX3State
*piix3_state
;
435 static void piix3_reset(PIIX3State
*d
)
437 uint8_t *pci_conf
= d
->dev
.config
;
439 pci_conf
[0x04] = 0x07; // master, memory and I/O
440 pci_conf
[0x05] = 0x00;
441 pci_conf
[0x06] = 0x00;
442 pci_conf
[0x07] = 0x02; // PCI_status_devsel_medium
443 pci_conf
[0x4c] = 0x4d;
444 pci_conf
[0x4e] = 0x03;
445 pci_conf
[0x4f] = 0x00;
446 pci_conf
[0x60] = 0x80;
447 pci_conf
[0x69] = 0x02;
448 pci_conf
[0x70] = 0x80;
449 pci_conf
[0x76] = 0x0c;
450 pci_conf
[0x77] = 0x0c;
451 pci_conf
[0x78] = 0x02;
452 pci_conf
[0x79] = 0x00;
453 pci_conf
[0x80] = 0x00;
454 pci_conf
[0x82] = 0x00;
455 pci_conf
[0xa0] = 0x08;
456 pci_conf
[0xa0] = 0x08;
457 pci_conf
[0xa2] = 0x00;
458 pci_conf
[0xa3] = 0x00;
459 pci_conf
[0xa4] = 0x00;
460 pci_conf
[0xa5] = 0x00;
461 pci_conf
[0xa6] = 0x00;
462 pci_conf
[0xa7] = 0x00;
463 pci_conf
[0xa8] = 0x0f;
464 pci_conf
[0xaa] = 0x00;
465 pci_conf
[0xab] = 0x00;
466 pci_conf
[0xac] = 0x00;
467 pci_conf
[0xae] = 0x00;
470 void piix3_init(void)
475 d
= (PIIX3State
*)pci_register_device("PIIX3", sizeof(PIIX3State
),
479 pci_conf
= d
->dev
.config
;
481 pci_conf
[0x00] = 0x86; // Intel
482 pci_conf
[0x01] = 0x80;
483 pci_conf
[0x02] = 0x00; // 82371SB PIIX3 PCI-to-ISA bridge (Step A1)
484 pci_conf
[0x03] = 0x70;
485 pci_conf
[0x0a] = 0x01; // class_sub = PCI_ISA
486 pci_conf
[0x0b] = 0x06; // class_base = PCI_bridge
487 pci_conf
[0x0e] = 0x80; // header_type = PCI_multifunction, generic
494 static inline void set_config(PCIBridge
*s
, target_phys_addr_t addr
)
498 for(i
= 0; i
< 11; i
++) {
499 if ((addr
& (1 << (11 + i
))) != 0)
502 devfn
= ((addr
>> 8) & 7) | (i
<< 3);
503 s
->config_reg
= 0x80000000 | (addr
& 0xfc) | (devfn
<< 8);
506 static void PPC_PCIIO_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
508 PCIBridge
*s
= opaque
;
510 pci_data_write(s
, addr
, val
, 1);
513 static void PPC_PCIIO_writew (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
515 PCIBridge
*s
= opaque
;
517 #ifdef TARGET_WORDS_BIGENDIAN
520 pci_data_write(s
, addr
, val
, 2);
523 static void PPC_PCIIO_writel (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
525 PCIBridge
*s
= opaque
;
527 #ifdef TARGET_WORDS_BIGENDIAN
530 pci_data_write(s
, addr
, val
, 4);
533 static uint32_t PPC_PCIIO_readb (void *opaque
, target_phys_addr_t addr
)
535 PCIBridge
*s
= opaque
;
538 val
= pci_data_read(s
, addr
, 1);
542 static uint32_t PPC_PCIIO_readw (void *opaque
, target_phys_addr_t addr
)
544 PCIBridge
*s
= opaque
;
547 val
= pci_data_read(s
, addr
, 2);
548 #ifdef TARGET_WORDS_BIGENDIAN
554 static uint32_t PPC_PCIIO_readl (void *opaque
, target_phys_addr_t addr
)
556 PCIBridge
*s
= opaque
;
559 val
= pci_data_read(s
, addr
, 4);
560 #ifdef TARGET_WORDS_BIGENDIAN
566 static CPUWriteMemoryFunc
*PPC_PCIIO_write
[] = {
572 static CPUReadMemoryFunc
*PPC_PCIIO_read
[] = {
578 void pci_prep_init(void)
580 PCIBridge
*s
= &pci_bridge
;
584 PPC_io_memory
= cpu_register_io_memory(0, PPC_PCIIO_read
,
586 cpu_register_physical_memory(0x80800000, 0x00400000, PPC_io_memory
);
588 d
= pci_register_device("PREP PCI Bridge", sizeof(PCIDevice
), 0, 0,
591 /* XXX: put correct IDs */
592 d
->config
[0x00] = 0x11; // vendor_id
593 d
->config
[0x01] = 0x10;
594 d
->config
[0x02] = 0x26; // device_id
595 d
->config
[0x03] = 0x00;
596 d
->config
[0x08] = 0x02; // revision
597 d
->config
[0x0a] = 0x04; // class_sub = pci2pci
598 d
->config
[0x0b] = 0x06; // class_base = PCI_bridge
599 d
->config
[0x0e] = 0x01; // header_type
605 static void pci_pmac_config_writel (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
607 PCIBridge
*s
= opaque
;
608 #ifdef TARGET_WORDS_BIGENDIAN
614 static uint32_t pci_pmac_config_readl (void *opaque
, target_phys_addr_t addr
)
616 PCIBridge
*s
= opaque
;
620 #ifdef TARGET_WORDS_BIGENDIAN
626 static CPUWriteMemoryFunc
*pci_pmac_config_write
[] = {
627 &pci_pmac_config_writel
,
628 &pci_pmac_config_writel
,
629 &pci_pmac_config_writel
,
632 static CPUReadMemoryFunc
*pci_pmac_config_read
[] = {
633 &pci_pmac_config_readl
,
634 &pci_pmac_config_readl
,
635 &pci_pmac_config_readl
,
638 static void pci_pmac_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
640 PCIBridge
*s
= opaque
;
641 pci_data_write(s
, addr
, val
, 1);
644 static void pci_pmac_writew (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
646 PCIBridge
*s
= opaque
;
647 #ifdef TARGET_WORDS_BIGENDIAN
650 pci_data_write(s
, addr
, val
, 2);
653 static void pci_pmac_writel (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
655 PCIBridge
*s
= opaque
;
656 #ifdef TARGET_WORDS_BIGENDIAN
659 pci_data_write(s
, addr
, val
, 4);
662 static uint32_t pci_pmac_readb (void *opaque
, target_phys_addr_t addr
)
664 PCIBridge
*s
= opaque
;
666 val
= pci_data_read(s
, addr
, 1);
670 static uint32_t pci_pmac_readw (void *opaque
, target_phys_addr_t addr
)
672 PCIBridge
*s
= opaque
;
674 val
= pci_data_read(s
, addr
, 2);
675 #ifdef TARGET_WORDS_BIGENDIAN
681 static uint32_t pci_pmac_readl (void *opaque
, target_phys_addr_t addr
)
683 PCIBridge
*s
= opaque
;
686 val
= pci_data_read(s
, addr
, 4);
687 #ifdef TARGET_WORDS_BIGENDIAN
693 static CPUWriteMemoryFunc
*pci_pmac_write
[] = {
699 static CPUReadMemoryFunc
*pci_pmac_read
[] = {
705 void pci_pmac_init(void)
707 PCIBridge
*s
= &pci_bridge
;
709 int pci_mem_config
, pci_mem_data
;
711 pci_mem_config
= cpu_register_io_memory(0, pci_pmac_config_read
,
712 pci_pmac_config_write
, s
);
713 pci_mem_data
= cpu_register_io_memory(0, pci_pmac_read
, pci_pmac_write
, s
);
715 cpu_register_physical_memory(0xfec00000, 0x1000, pci_mem_config
);
716 cpu_register_physical_memory(0xfee00000, 0x1000, pci_mem_data
);
718 d
= pci_register_device("MPC106", sizeof(PCIDevice
), 0, 0,
721 /* same values as PearPC - check this */
722 d
->config
[0x00] = 0x11; // vendor_id
723 d
->config
[0x01] = 0x10;
724 d
->config
[0x02] = 0x26; // device_id
725 d
->config
[0x03] = 0x00;
726 d
->config
[0x08] = 0x02; // revision
727 d
->config
[0x0a] = 0x04; // class_sub = pci2pci
728 d
->config
[0x0b] = 0x06; // class_base = PCI_bridge
729 d
->config
[0x0e] = 0x01; // header_type
731 d
->config
[0x18] = 0x0; // primary_bus
732 d
->config
[0x19] = 0x1; // secondary_bus
733 d
->config
[0x1a] = 0x1; // subordinate_bus
734 d
->config
[0x1c] = 0x10; // io_base
735 d
->config
[0x1d] = 0x20; // io_limit
737 d
->config
[0x20] = 0x80; // memory_base
738 d
->config
[0x21] = 0x80;
739 d
->config
[0x22] = 0x90; // memory_limit
740 d
->config
[0x23] = 0x80;
742 d
->config
[0x24] = 0x00; // prefetchable_memory_base
743 d
->config
[0x25] = 0x84;
744 d
->config
[0x26] = 0x00; // prefetchable_memory_limit
745 d
->config
[0x27] = 0x85;
748 /***********************************************************/
749 /* generic PCI irq support */
751 /* return the global irq number corresponding to a given device irq
752 pin. We could also use the bus number to have a more precise
754 static inline int pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
757 slot_addend
= (pci_dev
->devfn
>> 3);
758 return (irq_num
+ slot_addend
) & 3;
761 /* 0 <= irq_num <= 3. level must be 0 or 1 */
763 void pci_set_irq(PCIDevice
*pci_dev
, int irq_num
, int level
)
767 void pci_set_irq(PCIDevice
*pci_dev
, int irq_num
, int level
)
769 int irq_index
, shift
, pic_irq
, pic_level
;
772 irq_num
= pci_slot_get_pirq(pci_dev
, irq_num
);
773 irq_index
= pci_dev
->irq_index
;
774 p
= &pci_irq_levels
[irq_num
][irq_index
>> 5];
775 shift
= (irq_index
& 0x1f);
776 *p
= (*p
& ~(1 << shift
)) | (level
<< shift
);
778 /* now we change the pic irq level according to the piix irq mappings */
779 pic_irq
= piix3_state
->dev
.config
[0x60 + irq_num
];
781 /* the pic level is the logical OR of all the PCI irqs mapped
784 #if (PCI_IRQ_WORDS == 2)
785 pic_level
= ((pci_irq_levels
[irq_num
][0] |
786 pci_irq_levels
[irq_num
][1]) != 0);
791 for(i
= 0; i
< PCI_IRQ_WORDS
; i
++) {
792 if (pci_irq_levels
[irq_num
][i
]) {
799 pic_set_irq(pic_irq
, pic_level
);
804 /***********************************************************/
805 /* monitor info on PCI */
807 static void pci_info_device(PCIDevice
*d
)
812 printf(" Bus %2d, device %3d, function %d:\n",
813 d
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7);
814 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
818 printf("IDE controller");
821 printf("Ethernet controller");
824 printf("VGA controller");
827 printf("Class %04x", class);
830 printf(": PCI device %04x:%04x\n",
831 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
832 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))));
834 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
835 printf(" IRQ %d.\n", d
->config
[PCI_INTERRUPT_LINE
]);
837 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
838 r
= &d
->io_regions
[i
];
840 printf(" BAR%d: ", i
);
841 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
842 printf("I/O at 0x%04x [0x%04x].\n",
843 r
->addr
, r
->addr
+ r
->size
- 1);
845 printf("32 bit memory at 0x%08x [0x%08x].\n",
846 r
->addr
, r
->addr
+ r
->size
- 1);
854 PCIBridge
*s
= &pci_bridge
;
858 for(bus_num
= 0; bus_num
< 256; bus_num
++) {
859 bus
= s
->pci_bus
[bus_num
];
861 for(devfn
= 0; devfn
< 256; devfn
++) {
863 pci_info_device(bus
[devfn
]);
869 /***********************************************************/
870 /* XXX: the following should be moved to the PC BIOS */
872 static uint32_t isa_inb(uint32_t addr
)
874 return cpu_inb(cpu_single_env
, addr
);
877 static void isa_outb(uint32_t val
, uint32_t addr
)
879 cpu_outb(cpu_single_env
, addr
, val
);
882 static uint32_t isa_inw(uint32_t addr
)
884 return cpu_inw(cpu_single_env
, addr
);
887 static void isa_outw(uint32_t val
, uint32_t addr
)
889 cpu_outw(cpu_single_env
, addr
, val
);
892 static uint32_t isa_inl(uint32_t addr
)
894 return cpu_inl(cpu_single_env
, addr
);
897 static void isa_outl(uint32_t val
, uint32_t addr
)
899 cpu_outl(cpu_single_env
, addr
, val
);
902 static void pci_config_writel(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
904 PCIBridge
*s
= &pci_bridge
;
905 s
->config_reg
= 0x80000000 | (d
->bus_num
<< 16) |
906 (d
->devfn
<< 8) | addr
;
907 pci_data_write(s
, 0, val
, 4);
910 static void pci_config_writew(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
912 PCIBridge
*s
= &pci_bridge
;
913 s
->config_reg
= 0x80000000 | (d
->bus_num
<< 16) |
914 (d
->devfn
<< 8) | (addr
& ~3);
915 pci_data_write(s
, addr
& 3, val
, 2);
918 static void pci_config_writeb(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
920 PCIBridge
*s
= &pci_bridge
;
921 s
->config_reg
= 0x80000000 | (d
->bus_num
<< 16) |
922 (d
->devfn
<< 8) | (addr
& ~3);
923 pci_data_write(s
, addr
& 3, val
, 1);
926 static uint32_t pci_config_readl(PCIDevice
*d
, uint32_t addr
)
928 PCIBridge
*s
= &pci_bridge
;
929 s
->config_reg
= 0x80000000 | (d
->bus_num
<< 16) |
930 (d
->devfn
<< 8) | addr
;
931 return pci_data_read(s
, 0, 4);
934 static uint32_t pci_config_readw(PCIDevice
*d
, uint32_t addr
)
936 PCIBridge
*s
= &pci_bridge
;
937 s
->config_reg
= 0x80000000 | (d
->bus_num
<< 16) |
938 (d
->devfn
<< 8) | (addr
& ~3);
939 return pci_data_read(s
, addr
& 3, 2);
942 static uint32_t pci_config_readb(PCIDevice
*d
, uint32_t addr
)
944 PCIBridge
*s
= &pci_bridge
;
945 s
->config_reg
= 0x80000000 | (d
->bus_num
<< 16) |
946 (d
->devfn
<< 8) | (addr
& ~3);
947 return pci_data_read(s
, addr
& 3, 1);
950 static uint32_t pci_bios_io_addr
;
951 static uint32_t pci_bios_mem_addr
;
952 /* host irqs corresponding to PCI irqs A-D */
953 static uint8_t pci_irqs
[4] = { 11, 9, 11, 9 };
955 static void pci_set_io_region_addr(PCIDevice
*d
, int region_num
, uint32_t addr
)
961 if ( region_num
== PCI_ROM_SLOT
) {
964 ofs
= 0x10 + region_num
* 4;
967 pci_config_writel(d
, ofs
, addr
);
968 r
= &d
->io_regions
[region_num
];
970 /* enable memory mappings */
971 cmd
= pci_config_readw(d
, PCI_COMMAND
);
972 if ( region_num
== PCI_ROM_SLOT
)
974 else if (r
->type
& PCI_ADDRESS_SPACE_IO
)
978 pci_config_writew(d
, PCI_COMMAND
, cmd
);
981 static void pci_bios_init_device(PCIDevice
*d
)
986 int i
, pin
, pic_irq
, vendor_id
, device_id
;
988 class = pci_config_readw(d
, PCI_CLASS_DEVICE
);
991 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
992 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
993 if (vendor_id
== 0x8086 && device_id
== 0x7010) {
995 pci_config_writew(d
, PCI_COMMAND
, PCI_COMMAND_IO
);
996 pci_config_writew(d
, 0x40, 0x8000); // enable IDE0
998 /* IDE: we map it as in ISA mode */
999 pci_set_io_region_addr(d
, 0, 0x1f0);
1000 pci_set_io_region_addr(d
, 1, 0x3f4);
1001 pci_set_io_region_addr(d
, 2, 0x170);
1002 pci_set_io_region_addr(d
, 3, 0x374);
1006 /* VGA: map frame buffer to default Bochs VBE address */
1007 pci_set_io_region_addr(d
, 0, 0xE0000000);
1010 /* default memory mappings */
1011 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1012 r
= &d
->io_regions
[i
];
1014 if (r
->type
& PCI_ADDRESS_SPACE_IO
)
1015 paddr
= &pci_bios_io_addr
;
1017 paddr
= &pci_bios_mem_addr
;
1018 *paddr
= (*paddr
+ r
->size
- 1) & ~(r
->size
- 1);
1019 pci_set_io_region_addr(d
, i
, *paddr
);
1026 /* map the interrupt */
1027 pin
= pci_config_readb(d
, PCI_INTERRUPT_PIN
);
1029 pin
= pci_slot_get_pirq(d
, pin
- 1);
1030 pic_irq
= pci_irqs
[pin
];
1031 pci_config_writeb(d
, PCI_INTERRUPT_LINE
, pic_irq
);
1036 * This function initializes the PCI devices as a normal PCI BIOS
1037 * would do. It is provided just in case the BIOS has no support for
1040 void pci_bios_init(void)
1042 PCIBridge
*s
= &pci_bridge
;
1044 int bus_num
, devfn
, i
, irq
;
1047 pci_bios_io_addr
= 0xc000;
1048 pci_bios_mem_addr
= 0xf0000000;
1050 /* activate IRQ mappings */
1053 for(i
= 0; i
< 4; i
++) {
1055 /* set to trigger level */
1056 elcr
[irq
>> 3] |= (1 << (irq
& 7));
1057 /* activate irq remapping in PIIX */
1058 pci_config_writeb((PCIDevice
*)piix3_state
, 0x60 + i
, irq
);
1060 isa_outb(elcr
[0], 0x4d0);
1061 isa_outb(elcr
[1], 0x4d1);
1063 for(bus_num
= 0; bus_num
< 256; bus_num
++) {
1064 bus
= s
->pci_bus
[bus_num
];
1066 for(devfn
= 0; devfn
< 256; devfn
++) {
1068 pci_bios_init_device(bus
[devfn
]);
1075 * This function initializes the PCI devices as a normal PCI BIOS
1076 * would do. It is provided just in case the BIOS has no support for
1079 void pci_ppc_bios_init(void)
1081 PCIBridge
*s
= &pci_bridge
;
1083 int bus_num
, devfn
, i
, irq
;
1086 pci_bios_io_addr
= 0xc000;
1087 pci_bios_mem_addr
= 0xc0000000;
1090 /* activate IRQ mappings */
1093 for(i
= 0; i
< 4; i
++) {
1095 /* set to trigger level */
1096 elcr
[irq
>> 3] |= (1 << (irq
& 7));
1097 /* activate irq remapping in PIIX */
1098 pci_config_writeb((PCIDevice
*)piix3_state
, 0x60 + i
, irq
);
1100 isa_outb(elcr
[0], 0x4d0);
1101 isa_outb(elcr
[1], 0x4d1);
1104 for(bus_num
= 0; bus_num
< 256; bus_num
++) {
1105 bus
= s
->pci_bus
[bus_num
];
1107 for(devfn
= 0; devfn
< 256; devfn
++) {
1109 pci_bios_init_device(bus
[devfn
]);