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)
46 void (*set_irq
)(PCIDevice
*pci_dev
, int irq_num
, int level
);
47 uint32_t config_reg
; /* XXX: suppress */
48 openpic_t
*openpic
; /* XXX: suppress */
49 PCIDevice
*devices
[256];
52 target_phys_addr_t pci_mem_base
;
53 static int pci_irq_index
;
54 static uint32_t pci_irq_levels
[4][PCI_IRQ_WORDS
];
55 static PCIBus
*first_bus
;
57 static PCIBus
*pci_register_bus(void)
60 bus
= qemu_mallocz(sizeof(PCIBus
));
65 /* -1 for devfn means auto assign */
66 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
67 int instance_size
, int devfn
,
68 PCIConfigReadFunc
*config_read
,
69 PCIConfigWriteFunc
*config_write
)
73 if (pci_irq_index
>= PCI_DEVICES_MAX
)
77 for(devfn
= bus
->devfn_min
; devfn
< 256; devfn
+= 8) {
78 if (!bus
->devices
[devfn
])
84 pci_dev
= qemu_mallocz(instance_size
);
88 pci_dev
->devfn
= devfn
;
89 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
92 config_read
= pci_default_read_config
;
94 config_write
= pci_default_write_config
;
95 pci_dev
->config_read
= config_read
;
96 pci_dev
->config_write
= config_write
;
97 pci_dev
->irq_index
= pci_irq_index
++;
98 bus
->devices
[devfn
] = pci_dev
;
102 void pci_register_io_region(PCIDevice
*pci_dev
, int region_num
,
103 uint32_t size
, int type
,
104 PCIMapIORegionFunc
*map_func
)
108 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
110 r
= &pci_dev
->io_regions
[region_num
];
114 r
->map_func
= map_func
;
117 static void pci_addr_writel(void* opaque
, uint32_t addr
, uint32_t val
)
123 static uint32_t pci_addr_readl(void* opaque
, uint32_t addr
)
126 return s
->config_reg
;
129 static void pci_update_mappings(PCIDevice
*d
)
133 uint32_t last_addr
, new_addr
, config_ofs
;
135 cmd
= le16_to_cpu(*(uint16_t *)(d
->config
+ PCI_COMMAND
));
136 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
137 r
= &d
->io_regions
[i
];
138 if (i
== PCI_ROM_SLOT
) {
141 config_ofs
= 0x10 + i
* 4;
144 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
145 if (cmd
& PCI_COMMAND_IO
) {
146 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
148 new_addr
= new_addr
& ~(r
->size
- 1);
149 last_addr
= new_addr
+ r
->size
- 1;
150 /* NOTE: we have only 64K ioports on PC */
151 if (last_addr
<= new_addr
|| new_addr
== 0 ||
152 last_addr
>= 0x10000) {
159 if (cmd
& PCI_COMMAND_MEMORY
) {
160 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
162 /* the ROM slot has a specific enable bit */
163 if (i
== PCI_ROM_SLOT
&& !(new_addr
& 1))
165 new_addr
= new_addr
& ~(r
->size
- 1);
166 last_addr
= new_addr
+ r
->size
- 1;
167 /* NOTE: we do not support wrapping */
168 /* XXX: as we cannot support really dynamic
169 mappings, we handle specific values as invalid
171 if (last_addr
<= new_addr
|| new_addr
== 0 ||
180 /* now do the real mapping */
181 if (new_addr
!= r
->addr
) {
183 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
185 /* NOTE: specific hack for IDE in PC case:
186 only one byte must be mapped. */
187 class = d
->config
[0x0a] | (d
->config
[0x0b] << 8);
188 if (class == 0x0101 && r
->size
== 4) {
189 isa_unassign_ioport(r
->addr
+ 2, 1);
191 isa_unassign_ioport(r
->addr
, r
->size
);
194 cpu_register_physical_memory(r
->addr
+ pci_mem_base
,
201 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
208 uint32_t pci_default_read_config(PCIDevice
*d
,
209 uint32_t address
, int len
)
214 val
= d
->config
[address
];
217 val
= le16_to_cpu(*(uint16_t *)(d
->config
+ address
));
221 val
= le32_to_cpu(*(uint32_t *)(d
->config
+ address
));
227 void pci_default_write_config(PCIDevice
*d
,
228 uint32_t address
, uint32_t val
, int len
)
233 if (len
== 4 && ((address
>= 0x10 && address
< 0x10 + 4 * 6) ||
234 (address
>= 0x30 && address
< 0x34))) {
238 if ( address
>= 0x30 ) {
241 reg
= (address
- 0x10) >> 2;
243 r
= &d
->io_regions
[reg
];
246 /* compute the stored value */
247 if (reg
== PCI_ROM_SLOT
) {
248 /* keep ROM enable bit */
249 val
&= (~(r
->size
- 1)) | 1;
251 val
&= ~(r
->size
- 1);
254 *(uint32_t *)(d
->config
+ address
) = cpu_to_le32(val
);
255 pci_update_mappings(d
);
259 /* not efficient, but simple */
261 for(i
= 0; i
< len
; i
++) {
262 /* default read/write accesses */
263 switch(d
->config
[0x0e]) {
276 case 0x10 ... 0x27: /* base */
277 case 0x30 ... 0x33: /* rom */
298 case 0x38 ... 0x3b: /* rom */
309 d
->config
[addr
] = val
;
316 if (end
> PCI_COMMAND
&& address
< (PCI_COMMAND
+ 2)) {
317 /* if the command register is modified, we must modify the mappings */
318 pci_update_mappings(d
);
322 static void pci_data_write(void *opaque
, uint32_t addr
,
323 uint32_t val
, int len
)
327 int config_addr
, bus_num
;
329 #if defined(DEBUG_PCI) && 0
330 printf("pci_data_write: addr=%08x val=%08x len=%d\n",
331 s
->config_reg
, val
, len
);
333 if (!(s
->config_reg
& (1 << 31))) {
336 if ((s
->config_reg
& 0x3) != 0) {
339 bus_num
= (s
->config_reg
>> 16) & 0xff;
342 pci_dev
= s
->devices
[(s
->config_reg
>> 8) & 0xff];
345 config_addr
= (s
->config_reg
& 0xfc) | (addr
& 3);
346 #if defined(DEBUG_PCI)
347 printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
348 pci_dev
->name
, config_addr
, val
, len
);
350 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
353 static uint32_t pci_data_read(void *opaque
, uint32_t addr
,
358 int config_addr
, bus_num
;
361 if (!(s
->config_reg
& (1 << 31)))
363 if ((s
->config_reg
& 0x3) != 0)
365 bus_num
= (s
->config_reg
>> 16) & 0xff;
368 pci_dev
= s
->devices
[(s
->config_reg
>> 8) & 0xff];
385 config_addr
= (s
->config_reg
& 0xfc) | (addr
& 3);
386 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
387 #if defined(DEBUG_PCI)
388 printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
389 pci_dev
->name
, config_addr
, val
, len
);
392 #if defined(DEBUG_PCI) && 0
393 printf("pci_data_read: addr=%08x val=%08x len=%d\n",
394 s
->config_reg
, val
, len
);
399 static void pci_data_writeb(void* opaque
, uint32_t addr
, uint32_t val
)
401 pci_data_write(opaque
, addr
, val
, 1);
404 static void pci_data_writew(void* opaque
, uint32_t addr
, uint32_t val
)
406 pci_data_write(opaque
, addr
, val
, 2);
409 static void pci_data_writel(void* opaque
, uint32_t addr
, uint32_t val
)
411 pci_data_write(opaque
, addr
, val
, 4);
414 static uint32_t pci_data_readb(void* opaque
, uint32_t addr
)
416 return pci_data_read(opaque
, addr
, 1);
419 static uint32_t pci_data_readw(void* opaque
, uint32_t addr
)
421 return pci_data_read(opaque
, addr
, 2);
424 static uint32_t pci_data_readl(void* opaque
, uint32_t addr
)
426 return pci_data_read(opaque
, addr
, 4);
429 /* i440FX PCI bridge */
431 static void piix3_set_irq(PCIDevice
*pci_dev
, int irq_num
, int level
);
433 PCIBus
*i440fx_init(void)
438 s
= pci_register_bus();
439 s
->set_irq
= piix3_set_irq
;
441 register_ioport_write(0xcf8, 4, 4, pci_addr_writel
, s
);
442 register_ioport_read(0xcf8, 4, 4, pci_addr_readl
, s
);
444 register_ioport_write(0xcfc, 4, 1, pci_data_writeb
, s
);
445 register_ioport_write(0xcfc, 4, 2, pci_data_writew
, s
);
446 register_ioport_write(0xcfc, 4, 4, pci_data_writel
, s
);
447 register_ioport_read(0xcfc, 4, 1, pci_data_readb
, s
);
448 register_ioport_read(0xcfc, 4, 2, pci_data_readw
, s
);
449 register_ioport_read(0xcfc, 4, 4, pci_data_readl
, s
);
451 d
= pci_register_device(s
, "i440FX", sizeof(PCIDevice
), 0,
454 d
->config
[0x00] = 0x86; // vendor_id
455 d
->config
[0x01] = 0x80;
456 d
->config
[0x02] = 0x37; // device_id
457 d
->config
[0x03] = 0x12;
458 d
->config
[0x08] = 0x02; // revision
459 d
->config
[0x0a] = 0x00; // class_sub = host2pci
460 d
->config
[0x0b] = 0x06; // class_base = PCI_bridge
461 d
->config
[0x0e] = 0x00; // header_type
465 /* PIIX3 PCI to ISA bridge */
467 typedef struct PIIX3State
{
471 PIIX3State
*piix3_state
;
473 /* return the global irq number corresponding to a given device irq
474 pin. We could also use the bus number to have a more precise
476 static inline int pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
479 slot_addend
= (pci_dev
->devfn
>> 3);
480 return (irq_num
+ slot_addend
) & 3;
483 static void piix3_set_irq(PCIDevice
*pci_dev
, int irq_num
, int level
)
485 int irq_index
, shift
, pic_irq
, pic_level
;
488 irq_num
= pci_slot_get_pirq(pci_dev
, irq_num
);
489 irq_index
= pci_dev
->irq_index
;
490 p
= &pci_irq_levels
[irq_num
][irq_index
>> 5];
491 shift
= (irq_index
& 0x1f);
492 *p
= (*p
& ~(1 << shift
)) | (level
<< shift
);
494 /* now we change the pic irq level according to the piix irq mappings */
495 pic_irq
= piix3_state
->dev
.config
[0x60 + irq_num
];
497 /* the pic level is the logical OR of all the PCI irqs mapped
500 #if (PCI_IRQ_WORDS == 2)
501 pic_level
= ((pci_irq_levels
[irq_num
][0] |
502 pci_irq_levels
[irq_num
][1]) != 0);
507 for(i
= 0; i
< PCI_IRQ_WORDS
; i
++) {
508 if (pci_irq_levels
[irq_num
][i
]) {
515 pic_set_irq(pic_irq
, pic_level
);
519 static void piix3_reset(PIIX3State
*d
)
521 uint8_t *pci_conf
= d
->dev
.config
;
523 pci_conf
[0x04] = 0x07; // master, memory and I/O
524 pci_conf
[0x05] = 0x00;
525 pci_conf
[0x06] = 0x00;
526 pci_conf
[0x07] = 0x02; // PCI_status_devsel_medium
527 pci_conf
[0x4c] = 0x4d;
528 pci_conf
[0x4e] = 0x03;
529 pci_conf
[0x4f] = 0x00;
530 pci_conf
[0x60] = 0x80;
531 pci_conf
[0x69] = 0x02;
532 pci_conf
[0x70] = 0x80;
533 pci_conf
[0x76] = 0x0c;
534 pci_conf
[0x77] = 0x0c;
535 pci_conf
[0x78] = 0x02;
536 pci_conf
[0x79] = 0x00;
537 pci_conf
[0x80] = 0x00;
538 pci_conf
[0x82] = 0x00;
539 pci_conf
[0xa0] = 0x08;
540 pci_conf
[0xa0] = 0x08;
541 pci_conf
[0xa2] = 0x00;
542 pci_conf
[0xa3] = 0x00;
543 pci_conf
[0xa4] = 0x00;
544 pci_conf
[0xa5] = 0x00;
545 pci_conf
[0xa6] = 0x00;
546 pci_conf
[0xa7] = 0x00;
547 pci_conf
[0xa8] = 0x0f;
548 pci_conf
[0xaa] = 0x00;
549 pci_conf
[0xab] = 0x00;
550 pci_conf
[0xac] = 0x00;
551 pci_conf
[0xae] = 0x00;
554 void piix3_init(PCIBus
*bus
)
559 d
= (PIIX3State
*)pci_register_device(bus
, "PIIX3", sizeof(PIIX3State
),
562 pci_conf
= d
->dev
.config
;
564 pci_conf
[0x00] = 0x86; // Intel
565 pci_conf
[0x01] = 0x80;
566 pci_conf
[0x02] = 0x00; // 82371SB PIIX3 PCI-to-ISA bridge (Step A1)
567 pci_conf
[0x03] = 0x70;
568 pci_conf
[0x0a] = 0x01; // class_sub = PCI_ISA
569 pci_conf
[0x0b] = 0x06; // class_base = PCI_bridge
570 pci_conf
[0x0e] = 0x80; // header_type = PCI_multifunction, generic
577 static inline void set_config(PCIBus
*s
, target_phys_addr_t addr
)
581 for(i
= 0; i
< 11; i
++) {
582 if ((addr
& (1 << (11 + i
))) != 0)
585 devfn
= ((addr
>> 8) & 7) | (i
<< 3);
586 s
->config_reg
= 0x80000000 | (addr
& 0xfc) | (devfn
<< 8);
589 static void PPC_PCIIO_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
593 pci_data_write(s
, addr
, val
, 1);
596 static void PPC_PCIIO_writew (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
600 #ifdef TARGET_WORDS_BIGENDIAN
603 pci_data_write(s
, addr
, val
, 2);
606 static void PPC_PCIIO_writel (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
610 #ifdef TARGET_WORDS_BIGENDIAN
613 pci_data_write(s
, addr
, val
, 4);
616 static uint32_t PPC_PCIIO_readb (void *opaque
, target_phys_addr_t addr
)
621 val
= pci_data_read(s
, addr
, 1);
625 static uint32_t PPC_PCIIO_readw (void *opaque
, target_phys_addr_t addr
)
630 val
= pci_data_read(s
, addr
, 2);
631 #ifdef TARGET_WORDS_BIGENDIAN
637 static uint32_t PPC_PCIIO_readl (void *opaque
, target_phys_addr_t addr
)
642 val
= pci_data_read(s
, addr
, 4);
643 #ifdef TARGET_WORDS_BIGENDIAN
649 static CPUWriteMemoryFunc
*PPC_PCIIO_write
[] = {
655 static CPUReadMemoryFunc
*PPC_PCIIO_read
[] = {
661 static void prep_set_irq(PCIDevice
*d
, int irq_num
, int level
)
663 /* XXX: we do not simulate the hardware - we rely on the BIOS to
664 set correctly for irq line field */
665 pic_set_irq(d
->config
[PCI_INTERRUPT_LINE
], level
);
668 PCIBus
*pci_prep_init(void)
674 s
= pci_register_bus();
675 s
->set_irq
= prep_set_irq
;
677 PPC_io_memory
= cpu_register_io_memory(0, PPC_PCIIO_read
,
679 cpu_register_physical_memory(0x80800000, 0x00400000, PPC_io_memory
);
681 d
= pci_register_device(s
, "PREP PCI Bridge", sizeof(PCIDevice
), 0,
684 /* XXX: put correct IDs */
685 d
->config
[0x00] = 0x11; // vendor_id
686 d
->config
[0x01] = 0x10;
687 d
->config
[0x02] = 0x26; // device_id
688 d
->config
[0x03] = 0x00;
689 d
->config
[0x08] = 0x02; // revision
690 d
->config
[0x0a] = 0x04; // class_sub = pci2pci
691 d
->config
[0x0b] = 0x06; // class_base = PCI_bridge
692 d
->config
[0x0e] = 0x01; // header_type
700 /* Grackle PCI host */
701 static void pci_grackle_config_writel (void *opaque
, target_phys_addr_t addr
,
705 #ifdef TARGET_WORDS_BIGENDIAN
711 static uint32_t pci_grackle_config_readl (void *opaque
, target_phys_addr_t addr
)
717 #ifdef TARGET_WORDS_BIGENDIAN
723 static CPUWriteMemoryFunc
*pci_grackle_config_write
[] = {
724 &pci_grackle_config_writel
,
725 &pci_grackle_config_writel
,
726 &pci_grackle_config_writel
,
729 static CPUReadMemoryFunc
*pci_grackle_config_read
[] = {
730 &pci_grackle_config_readl
,
731 &pci_grackle_config_readl
,
732 &pci_grackle_config_readl
,
735 static void pci_grackle_writeb (void *opaque
, target_phys_addr_t addr
,
739 pci_data_write(s
, addr
, val
, 1);
742 static void pci_grackle_writew (void *opaque
, target_phys_addr_t addr
,
746 #ifdef TARGET_WORDS_BIGENDIAN
749 pci_data_write(s
, addr
, val
, 2);
752 static void pci_grackle_writel (void *opaque
, target_phys_addr_t addr
,
756 #ifdef TARGET_WORDS_BIGENDIAN
759 pci_data_write(s
, addr
, val
, 4);
762 static uint32_t pci_grackle_readb (void *opaque
, target_phys_addr_t addr
)
766 val
= pci_data_read(s
, addr
, 1);
770 static uint32_t pci_grackle_readw (void *opaque
, target_phys_addr_t addr
)
774 val
= pci_data_read(s
, addr
, 2);
775 #ifdef TARGET_WORDS_BIGENDIAN
781 static uint32_t pci_grackle_readl (void *opaque
, target_phys_addr_t addr
)
786 val
= pci_data_read(s
, addr
, 4);
787 #ifdef TARGET_WORDS_BIGENDIAN
793 static CPUWriteMemoryFunc
*pci_grackle_write
[] = {
799 static CPUReadMemoryFunc
*pci_grackle_read
[] = {
806 /* Uninorth PCI host (for all Mac99 and newer machines */
807 static void pci_unin_main_config_writel (void *opaque
, target_phys_addr_t addr
,
813 #ifdef TARGET_WORDS_BIGENDIAN
817 for (i
= 11; i
< 32; i
++) {
818 if ((val
& (1 << i
)) != 0)
822 s
->config_reg
= 0x80000000 | (1 << 16) | (val
& 0x7FC) | (i
<< 11);
824 s
->config_reg
= 0x80000000 | (0 << 16) | (val
& 0x7FC) | (i
<< 11);
828 static uint32_t pci_unin_main_config_readl (void *opaque
,
829 target_phys_addr_t addr
)
835 devfn
= (s
->config_reg
>> 8) & 0xFF;
836 val
= (1 << (devfn
>> 3)) | ((devfn
& 0x07) << 8) | (s
->config_reg
& 0xFC);
837 #ifdef TARGET_WORDS_BIGENDIAN
844 static CPUWriteMemoryFunc
*pci_unin_main_config_write
[] = {
845 &pci_unin_main_config_writel
,
846 &pci_unin_main_config_writel
,
847 &pci_unin_main_config_writel
,
850 static CPUReadMemoryFunc
*pci_unin_main_config_read
[] = {
851 &pci_unin_main_config_readl
,
852 &pci_unin_main_config_readl
,
853 &pci_unin_main_config_readl
,
856 static void pci_unin_main_writeb (void *opaque
, target_phys_addr_t addr
,
860 pci_data_write(s
, addr
& 7, val
, 1);
863 static void pci_unin_main_writew (void *opaque
, target_phys_addr_t addr
,
867 #ifdef TARGET_WORDS_BIGENDIAN
870 pci_data_write(s
, addr
& 7, val
, 2);
873 static void pci_unin_main_writel (void *opaque
, target_phys_addr_t addr
,
877 #ifdef TARGET_WORDS_BIGENDIAN
880 pci_data_write(s
, addr
& 7, val
, 4);
883 static uint32_t pci_unin_main_readb (void *opaque
, target_phys_addr_t addr
)
888 val
= pci_data_read(s
, addr
& 7, 1);
893 static uint32_t pci_unin_main_readw (void *opaque
, target_phys_addr_t addr
)
898 val
= pci_data_read(s
, addr
& 7, 2);
899 #ifdef TARGET_WORDS_BIGENDIAN
906 static uint32_t pci_unin_main_readl (void *opaque
, target_phys_addr_t addr
)
911 val
= pci_data_read(s
, addr
, 4);
912 #ifdef TARGET_WORDS_BIGENDIAN
919 static CPUWriteMemoryFunc
*pci_unin_main_write
[] = {
920 &pci_unin_main_writeb
,
921 &pci_unin_main_writew
,
922 &pci_unin_main_writel
,
925 static CPUReadMemoryFunc
*pci_unin_main_read
[] = {
926 &pci_unin_main_readb
,
927 &pci_unin_main_readw
,
928 &pci_unin_main_readl
,
933 static void pci_unin_config_writel (void *opaque
, target_phys_addr_t addr
,
938 #ifdef TARGET_WORDS_BIGENDIAN
941 s
->config_reg
= 0x80000000 | (val
& ~0x00000001);
944 static uint32_t pci_unin_config_readl (void *opaque
,
945 target_phys_addr_t addr
)
950 val
= (s
->config_reg
| 0x00000001) & ~0x80000000;
951 #ifdef TARGET_WORDS_BIGENDIAN
958 static CPUWriteMemoryFunc
*pci_unin_config_write
[] = {
959 &pci_unin_config_writel
,
960 &pci_unin_config_writel
,
961 &pci_unin_config_writel
,
964 static CPUReadMemoryFunc
*pci_unin_config_read
[] = {
965 &pci_unin_config_readl
,
966 &pci_unin_config_readl
,
967 &pci_unin_config_readl
,
970 static void pci_unin_writeb (void *opaque
, target_phys_addr_t addr
,
974 pci_data_write(s
, addr
& 3, val
, 1);
977 static void pci_unin_writew (void *opaque
, target_phys_addr_t addr
,
981 #ifdef TARGET_WORDS_BIGENDIAN
984 pci_data_write(s
, addr
& 3, val
, 2);
987 static void pci_unin_writel (void *opaque
, target_phys_addr_t addr
,
991 #ifdef TARGET_WORDS_BIGENDIAN
994 pci_data_write(s
, addr
& 3, val
, 4);
997 static uint32_t pci_unin_readb (void *opaque
, target_phys_addr_t addr
)
1002 val
= pci_data_read(s
, addr
& 3, 1);
1007 static uint32_t pci_unin_readw (void *opaque
, target_phys_addr_t addr
)
1012 val
= pci_data_read(s
, addr
& 3, 2);
1013 #ifdef TARGET_WORDS_BIGENDIAN
1020 static uint32_t pci_unin_readl (void *opaque
, target_phys_addr_t addr
)
1025 val
= pci_data_read(s
, addr
& 3, 4);
1026 #ifdef TARGET_WORDS_BIGENDIAN
1033 static CPUWriteMemoryFunc
*pci_unin_write
[] = {
1039 static CPUReadMemoryFunc
*pci_unin_read
[] = {
1046 static void pmac_set_irq(PCIDevice
*d
, int irq_num
, int level
)
1049 /* XXX: we do not simulate the hardware - we rely on the BIOS to
1050 set correctly for irq line field */
1051 openpic
= d
->bus
->openpic
;
1054 openpic_set_irq(openpic
, d
->config
[PCI_INTERRUPT_LINE
], level
);
1058 void pci_pmac_set_openpic(PCIBus
*bus
, openpic_t
*openpic
)
1060 bus
->openpic
= openpic
;
1063 PCIBus
*pci_pmac_init(void)
1067 int pci_mem_config
, pci_mem_data
;
1069 /* Use values found on a real PowerMac */
1070 /* Uninorth main bus */
1071 s
= pci_register_bus();
1072 s
->set_irq
= pmac_set_irq
;
1074 pci_mem_config
= cpu_register_io_memory(0, pci_unin_main_config_read
,
1075 pci_unin_main_config_write
, s
);
1076 pci_mem_data
= cpu_register_io_memory(0, pci_unin_main_read
,
1077 pci_unin_main_write
, s
);
1078 cpu_register_physical_memory(0xf2800000, 0x1000, pci_mem_config
);
1079 cpu_register_physical_memory(0xf2c00000, 0x1000, pci_mem_data
);
1080 s
->devfn_min
= 11 << 3;
1081 d
= pci_register_device(s
, "Uni-north main", sizeof(PCIDevice
),
1082 11 << 3, NULL
, NULL
);
1083 d
->config
[0x00] = 0x6b; // vendor_id : Apple
1084 d
->config
[0x01] = 0x10;
1085 d
->config
[0x02] = 0x1F; // device_id
1086 d
->config
[0x03] = 0x00;
1087 d
->config
[0x08] = 0x00; // revision
1088 d
->config
[0x0A] = 0x00; // class_sub = pci host
1089 d
->config
[0x0B] = 0x06; // class_base = PCI_bridge
1090 d
->config
[0x0C] = 0x08; // cache_line_size
1091 d
->config
[0x0D] = 0x10; // latency_timer
1092 d
->config
[0x0E] = 0x00; // header_type
1093 d
->config
[0x34] = 0x00; // capabilities_pointer
1095 #if 0 // XXX: not activated as PPC BIOS doesn't handle mutiple buses properly
1096 /* pci-to-pci bridge */
1097 d
= pci_register_device("Uni-north bridge", sizeof(PCIDevice
), 0, 13 << 3,
1099 d
->config
[0x00] = 0x11; // vendor_id : TI
1100 d
->config
[0x01] = 0x10;
1101 d
->config
[0x02] = 0x26; // device_id
1102 d
->config
[0x03] = 0x00;
1103 d
->config
[0x08] = 0x05; // revision
1104 d
->config
[0x0A] = 0x04; // class_sub = pci2pci
1105 d
->config
[0x0B] = 0x06; // class_base = PCI_bridge
1106 d
->config
[0x0C] = 0x08; // cache_line_size
1107 d
->config
[0x0D] = 0x20; // latency_timer
1108 d
->config
[0x0E] = 0x01; // header_type
1110 d
->config
[0x18] = 0x01; // primary_bus
1111 d
->config
[0x19] = 0x02; // secondary_bus
1112 d
->config
[0x1A] = 0x02; // subordinate_bus
1113 d
->config
[0x1B] = 0x20; // secondary_latency_timer
1114 d
->config
[0x1C] = 0x11; // io_base
1115 d
->config
[0x1D] = 0x01; // io_limit
1116 d
->config
[0x20] = 0x00; // memory_base
1117 d
->config
[0x21] = 0x80;
1118 d
->config
[0x22] = 0x00; // memory_limit
1119 d
->config
[0x23] = 0x80;
1120 d
->config
[0x24] = 0x01; // prefetchable_memory_base
1121 d
->config
[0x25] = 0x80;
1122 d
->config
[0x26] = 0xF1; // prefectchable_memory_limit
1123 d
->config
[0x27] = 0x7F;
1124 // d->config[0x34] = 0xdc // capabilities_pointer
1126 #if 0 // XXX: not needed for now
1127 /* Uninorth AGP bus */
1129 pci_mem_config
= cpu_register_io_memory(0, pci_unin_config_read
,
1130 pci_unin_config_write
, s
);
1131 pci_mem_data
= cpu_register_io_memory(0, pci_unin_read
,
1133 cpu_register_physical_memory(0xf0800000, 0x1000, pci_mem_config
);
1134 cpu_register_physical_memory(0xf0c00000, 0x1000, pci_mem_data
);
1136 d
= pci_register_device("Uni-north AGP", sizeof(PCIDevice
), 0, 11 << 3,
1138 d
->config
[0x00] = 0x6b; // vendor_id : Apple
1139 d
->config
[0x01] = 0x10;
1140 d
->config
[0x02] = 0x20; // device_id
1141 d
->config
[0x03] = 0x00;
1142 d
->config
[0x08] = 0x00; // revision
1143 d
->config
[0x0A] = 0x00; // class_sub = pci host
1144 d
->config
[0x0B] = 0x06; // class_base = PCI_bridge
1145 d
->config
[0x0C] = 0x08; // cache_line_size
1146 d
->config
[0x0D] = 0x10; // latency_timer
1147 d
->config
[0x0E] = 0x00; // header_type
1148 // d->config[0x34] = 0x80; // capabilities_pointer
1151 #if 0 // XXX: not needed for now
1152 /* Uninorth internal bus */
1154 pci_mem_config
= cpu_register_io_memory(0, pci_unin_config_read
,
1155 pci_unin_config_write
, s
);
1156 pci_mem_data
= cpu_register_io_memory(0, pci_unin_read
,
1158 cpu_register_physical_memory(0xf4800000, 0x1000, pci_mem_config
);
1159 cpu_register_physical_memory(0xf4c00000, 0x1000, pci_mem_data
);
1161 d
= pci_register_device("Uni-north internal", sizeof(PCIDevice
),
1162 3, 11 << 3, NULL
, NULL
);
1163 d
->config
[0x00] = 0x6b; // vendor_id : Apple
1164 d
->config
[0x01] = 0x10;
1165 d
->config
[0x02] = 0x1E; // device_id
1166 d
->config
[0x03] = 0x00;
1167 d
->config
[0x08] = 0x00; // revision
1168 d
->config
[0x0A] = 0x00; // class_sub = pci host
1169 d
->config
[0x0B] = 0x06; // class_base = PCI_bridge
1170 d
->config
[0x0C] = 0x08; // cache_line_size
1171 d
->config
[0x0D] = 0x10; // latency_timer
1172 d
->config
[0x0E] = 0x00; // header_type
1173 d
->config
[0x34] = 0x00; // capabilities_pointer
1177 /* same values as PearPC - check this */
1178 d
->config
[0x00] = 0x11; // vendor_id
1179 d
->config
[0x01] = 0x10;
1180 d
->config
[0x02] = 0x26; // device_id
1181 d
->config
[0x03] = 0x00;
1182 d
->config
[0x08] = 0x02; // revision
1183 d
->config
[0x0a] = 0x04; // class_sub = pci2pci
1184 d
->config
[0x0b] = 0x06; // class_base = PCI_bridge
1185 d
->config
[0x0e] = 0x01; // header_type
1187 d
->config
[0x18] = 0x0; // primary_bus
1188 d
->config
[0x19] = 0x1; // secondary_bus
1189 d
->config
[0x1a] = 0x1; // subordinate_bus
1190 d
->config
[0x1c] = 0x10; // io_base
1191 d
->config
[0x1d] = 0x20; // io_limit
1193 d
->config
[0x20] = 0x80; // memory_base
1194 d
->config
[0x21] = 0x80;
1195 d
->config
[0x22] = 0x90; // memory_limit
1196 d
->config
[0x23] = 0x80;
1198 d
->config
[0x24] = 0x00; // prefetchable_memory_base
1199 d
->config
[0x25] = 0x84;
1200 d
->config
[0x26] = 0x00; // prefetchable_memory_limit
1201 d
->config
[0x27] = 0x85;
1206 /***********************************************************/
1207 /* generic PCI irq support */
1209 /* 0 <= irq_num <= 3. level must be 0 or 1 */
1210 void pci_set_irq(PCIDevice
*pci_dev
, int irq_num
, int level
)
1212 PCIBus
*bus
= pci_dev
->bus
;
1213 bus
->set_irq(pci_dev
, irq_num
, level
);
1216 /***********************************************************/
1217 /* monitor info on PCI */
1219 static void pci_info_device(PCIDevice
*d
)
1224 printf(" Bus %2d, device %3d, function %d:\n",
1225 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7);
1226 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
1230 printf("IDE controller");
1233 printf("Ethernet controller");
1236 printf("VGA controller");
1239 printf("Class %04x", class);
1242 printf(": PCI device %04x:%04x\n",
1243 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
1244 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))));
1246 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
1247 printf(" IRQ %d.\n", d
->config
[PCI_INTERRUPT_LINE
]);
1249 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
1250 r
= &d
->io_regions
[i
];
1252 printf(" BAR%d: ", i
);
1253 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
1254 printf("I/O at 0x%04x [0x%04x].\n",
1255 r
->addr
, r
->addr
+ r
->size
- 1);
1257 printf("32 bit memory at 0x%08x [0x%08x].\n",
1258 r
->addr
, r
->addr
+ r
->size
- 1);
1266 PCIBus
*bus
= first_bus
;
1271 for(devfn
= 0; devfn
< 256; devfn
++) {
1272 d
= bus
->devices
[devfn
];
1279 /***********************************************************/
1280 /* XXX: the following should be moved to the PC BIOS */
1282 static __attribute__((unused
)) uint32_t isa_inb(uint32_t addr
)
1284 return cpu_inb(cpu_single_env
, addr
);
1287 static void isa_outb(uint32_t val
, uint32_t addr
)
1289 cpu_outb(cpu_single_env
, addr
, val
);
1292 static __attribute__((unused
)) uint32_t isa_inw(uint32_t addr
)
1294 return cpu_inw(cpu_single_env
, addr
);
1297 static __attribute__((unused
)) void isa_outw(uint32_t val
, uint32_t addr
)
1299 cpu_outw(cpu_single_env
, addr
, val
);
1302 static __attribute__((unused
)) uint32_t isa_inl(uint32_t addr
)
1304 return cpu_inl(cpu_single_env
, addr
);
1307 static __attribute__((unused
)) void isa_outl(uint32_t val
, uint32_t addr
)
1309 cpu_outl(cpu_single_env
, addr
, val
);
1312 static void pci_config_writel(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
1315 s
->config_reg
= 0x80000000 | (s
->bus_num
<< 16) |
1316 (d
->devfn
<< 8) | addr
;
1317 pci_data_write(s
, 0, val
, 4);
1320 static void pci_config_writew(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
1323 s
->config_reg
= 0x80000000 | (s
->bus_num
<< 16) |
1324 (d
->devfn
<< 8) | (addr
& ~3);
1325 pci_data_write(s
, addr
& 3, val
, 2);
1328 static void pci_config_writeb(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
1331 s
->config_reg
= 0x80000000 | (s
->bus_num
<< 16) |
1332 (d
->devfn
<< 8) | (addr
& ~3);
1333 pci_data_write(s
, addr
& 3, val
, 1);
1336 static __attribute__((unused
)) uint32_t pci_config_readl(PCIDevice
*d
, uint32_t addr
)
1339 s
->config_reg
= 0x80000000 | (s
->bus_num
<< 16) |
1340 (d
->devfn
<< 8) | addr
;
1341 return pci_data_read(s
, 0, 4);
1344 static uint32_t pci_config_readw(PCIDevice
*d
, uint32_t addr
)
1347 s
->config_reg
= 0x80000000 | (s
->bus_num
<< 16) |
1348 (d
->devfn
<< 8) | (addr
& ~3);
1349 return pci_data_read(s
, addr
& 3, 2);
1352 static uint32_t pci_config_readb(PCIDevice
*d
, uint32_t addr
)
1355 s
->config_reg
= 0x80000000 | (s
->bus_num
<< 16) |
1356 (d
->devfn
<< 8) | (addr
& ~3);
1357 return pci_data_read(s
, addr
& 3, 1);
1360 static uint32_t pci_bios_io_addr
;
1361 static uint32_t pci_bios_mem_addr
;
1362 /* host irqs corresponding to PCI irqs A-D */
1363 static uint8_t pci_irqs
[4] = { 11, 9, 11, 9 };
1365 static void pci_set_io_region_addr(PCIDevice
*d
, int region_num
, uint32_t addr
)
1371 if ( region_num
== PCI_ROM_SLOT
) {
1374 ofs
= 0x10 + region_num
* 4;
1377 pci_config_writel(d
, ofs
, addr
);
1378 r
= &d
->io_regions
[region_num
];
1380 /* enable memory mappings */
1381 cmd
= pci_config_readw(d
, PCI_COMMAND
);
1382 if ( region_num
== PCI_ROM_SLOT
)
1384 else if (r
->type
& PCI_ADDRESS_SPACE_IO
)
1388 pci_config_writew(d
, PCI_COMMAND
, cmd
);
1391 static void pci_bios_init_device(PCIDevice
*d
)
1396 int i
, pin
, pic_irq
, vendor_id
, device_id
;
1398 class = pci_config_readw(d
, PCI_CLASS_DEVICE
);
1399 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
1400 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
1403 if (vendor_id
== 0x8086 && device_id
== 0x7010) {
1405 pci_config_writew(d
, PCI_COMMAND
, PCI_COMMAND_IO
);
1406 pci_config_writew(d
, 0x40, 0x8000); // enable IDE0
1407 pci_config_writew(d
, 0x42, 0x8000); // enable IDE1
1409 /* IDE: we map it as in ISA mode */
1410 pci_set_io_region_addr(d
, 0, 0x1f0);
1411 pci_set_io_region_addr(d
, 1, 0x3f4);
1412 pci_set_io_region_addr(d
, 2, 0x170);
1413 pci_set_io_region_addr(d
, 3, 0x374);
1417 if (vendor_id
!= 0x1234)
1419 /* VGA: map frame buffer to default Bochs VBE address */
1420 pci_set_io_region_addr(d
, 0, 0xE0000000);
1424 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
1425 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
1426 if (vendor_id
== 0x1014) {
1428 if (device_id
== 0x0046 || device_id
== 0xFFFF) {
1430 pci_set_io_region_addr(d
, 0, 0x80800000 + 0x00040000);
1435 if (vendor_id
== 0x0106b &&
1436 (device_id
== 0x0017 || device_id
== 0x0022)) {
1438 pci_set_io_region_addr(d
, 0, 0x80800000);
1443 /* default memory mappings */
1444 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1445 r
= &d
->io_regions
[i
];
1447 if (r
->type
& PCI_ADDRESS_SPACE_IO
)
1448 paddr
= &pci_bios_io_addr
;
1450 paddr
= &pci_bios_mem_addr
;
1451 *paddr
= (*paddr
+ r
->size
- 1) & ~(r
->size
- 1);
1452 pci_set_io_region_addr(d
, i
, *paddr
);
1459 /* map the interrupt */
1460 pin
= pci_config_readb(d
, PCI_INTERRUPT_PIN
);
1462 pin
= pci_slot_get_pirq(d
, pin
- 1);
1463 pic_irq
= pci_irqs
[pin
];
1464 pci_config_writeb(d
, PCI_INTERRUPT_LINE
, pic_irq
);
1469 * This function initializes the PCI devices as a normal PCI BIOS
1470 * would do. It is provided just in case the BIOS has no support for
1473 void pci_bios_init(void)
1480 pci_bios_io_addr
= 0xc000;
1481 pci_bios_mem_addr
= 0xf0000000;
1483 /* activate IRQ mappings */
1486 for(i
= 0; i
< 4; i
++) {
1488 /* set to trigger level */
1489 elcr
[irq
>> 3] |= (1 << (irq
& 7));
1490 /* activate irq remapping in PIIX */
1491 pci_config_writeb((PCIDevice
*)piix3_state
, 0x60 + i
, irq
);
1493 isa_outb(elcr
[0], 0x4d0);
1494 isa_outb(elcr
[1], 0x4d1);
1498 for(devfn
= 0; devfn
< 256; devfn
++) {
1499 d
= bus
->devices
[devfn
];
1501 pci_bios_init_device(d
);