2 * QEMU i440FX/PIIX3 PCI Bridge Emulation
4 * Copyright (c) 2006 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
26 typedef uint32_t pci_addr_t
;
29 typedef PCIHostState I440FXState
;
31 static void i440fx_addr_writel(void* opaque
, uint32_t addr
, uint32_t val
)
33 I440FXState
*s
= opaque
;
37 static uint32_t i440fx_addr_readl(void* opaque
, uint32_t addr
)
39 I440FXState
*s
= opaque
;
43 static void piix3_set_irq(PCIDevice
*pci_dev
, void *pic
, int irq_num
, int level
);
45 PCIBus
*i440fx_init(void)
51 s
= qemu_mallocz(sizeof(I440FXState
));
52 b
= pci_register_bus(piix3_set_irq
, NULL
, 0);
55 register_ioport_write(0xcf8, 4, 4, i440fx_addr_writel
, s
);
56 register_ioport_read(0xcf8, 4, 4, i440fx_addr_readl
, s
);
58 register_ioport_write(0xcfc, 4, 1, pci_host_data_writeb
, s
);
59 register_ioport_write(0xcfc, 4, 2, pci_host_data_writew
, s
);
60 register_ioport_write(0xcfc, 4, 4, pci_host_data_writel
, s
);
61 register_ioport_read(0xcfc, 4, 1, pci_host_data_readb
, s
);
62 register_ioport_read(0xcfc, 4, 2, pci_host_data_readw
, s
);
63 register_ioport_read(0xcfc, 4, 4, pci_host_data_readl
, s
);
65 d
= pci_register_device(b
, "i440FX", sizeof(PCIDevice
), 0,
68 d
->config
[0x00] = 0x86; // vendor_id
69 d
->config
[0x01] = 0x80;
70 d
->config
[0x02] = 0x37; // device_id
71 d
->config
[0x03] = 0x12;
72 d
->config
[0x08] = 0x02; // revision
73 d
->config
[0x0a] = 0x00; // class_sub = host2pci
74 d
->config
[0x0b] = 0x06; // class_base = PCI_bridge
75 d
->config
[0x0e] = 0x00; // header_type
79 /* PIIX3 PCI to ISA bridge */
81 static PCIDevice
*piix3_dev
;
83 /* just used for simpler irq handling. */
84 #define PCI_IRQ_WORDS ((PCI_DEVICES_MAX + 31) / 32)
86 static uint32_t pci_irq_levels
[4][PCI_IRQ_WORDS
];
88 /* return the global irq number corresponding to a given device irq
89 pin. We could also use the bus number to have a more precise
91 static inline int pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
94 slot_addend
= (pci_dev
->devfn
>> 3) - 1;
95 return (irq_num
+ slot_addend
) & 3;
98 static inline int get_pci_irq_level(int irq_num
)
101 #if (PCI_IRQ_WORDS == 2)
102 pic_level
= ((pci_irq_levels
[irq_num
][0] |
103 pci_irq_levels
[irq_num
][1]) != 0);
108 for(i
= 0; i
< PCI_IRQ_WORDS
; i
++) {
109 if (pci_irq_levels
[irq_num
][i
]) {
119 static void piix3_set_irq(PCIDevice
*pci_dev
, void *pic
, int irq_num
, int level
)
121 int irq_index
, shift
, pic_irq
, pic_level
;
124 irq_num
= pci_slot_get_pirq(pci_dev
, irq_num
);
125 irq_index
= pci_dev
->irq_index
;
126 p
= &pci_irq_levels
[irq_num
][irq_index
>> 5];
127 shift
= (irq_index
& 0x1f);
128 *p
= (*p
& ~(1 << shift
)) | (level
<< shift
);
130 /* now we change the pic irq level according to the piix irq mappings */
132 pic_irq
= piix3_dev
->config
[0x60 + irq_num
];
134 /* the pic level is the logical OR of all the PCI irqs mapped
137 if (pic_irq
== piix3_dev
->config
[0x60])
138 pic_level
|= get_pci_irq_level(0);
139 if (pic_irq
== piix3_dev
->config
[0x61])
140 pic_level
|= get_pci_irq_level(1);
141 if (pic_irq
== piix3_dev
->config
[0x62])
142 pic_level
|= get_pci_irq_level(2);
143 if (pic_irq
== piix3_dev
->config
[0x63])
144 pic_level
|= get_pci_irq_level(3);
145 pic_set_irq(pic_irq
, pic_level
);
149 static void piix3_reset(PCIDevice
*d
)
151 uint8_t *pci_conf
= d
->config
;
153 pci_conf
[0x04] = 0x07; // master, memory and I/O
154 pci_conf
[0x05] = 0x00;
155 pci_conf
[0x06] = 0x00;
156 pci_conf
[0x07] = 0x02; // PCI_status_devsel_medium
157 pci_conf
[0x4c] = 0x4d;
158 pci_conf
[0x4e] = 0x03;
159 pci_conf
[0x4f] = 0x00;
160 pci_conf
[0x60] = 0x80;
161 pci_conf
[0x69] = 0x02;
162 pci_conf
[0x70] = 0x80;
163 pci_conf
[0x76] = 0x0c;
164 pci_conf
[0x77] = 0x0c;
165 pci_conf
[0x78] = 0x02;
166 pci_conf
[0x79] = 0x00;
167 pci_conf
[0x80] = 0x00;
168 pci_conf
[0x82] = 0x00;
169 pci_conf
[0xa0] = 0x08;
170 pci_conf
[0xa0] = 0x08;
171 pci_conf
[0xa2] = 0x00;
172 pci_conf
[0xa3] = 0x00;
173 pci_conf
[0xa4] = 0x00;
174 pci_conf
[0xa5] = 0x00;
175 pci_conf
[0xa6] = 0x00;
176 pci_conf
[0xa7] = 0x00;
177 pci_conf
[0xa8] = 0x0f;
178 pci_conf
[0xaa] = 0x00;
179 pci_conf
[0xab] = 0x00;
180 pci_conf
[0xac] = 0x00;
181 pci_conf
[0xae] = 0x00;
184 int piix3_init(PCIBus
*bus
)
189 d
= pci_register_device(bus
, "PIIX3", sizeof(PCIDevice
),
191 register_savevm("PIIX3", 0, 1, generic_pci_save
, generic_pci_load
, d
);
194 pci_conf
= d
->config
;
196 pci_conf
[0x00] = 0x86; // Intel
197 pci_conf
[0x01] = 0x80;
198 pci_conf
[0x02] = 0x00; // 82371SB PIIX3 PCI-to-ISA bridge (Step A1)
199 pci_conf
[0x03] = 0x70;
200 pci_conf
[0x0a] = 0x01; // class_sub = PCI_ISA
201 pci_conf
[0x0b] = 0x06; // class_base = PCI_bridge
202 pci_conf
[0x0e] = 0x80; // header_type = PCI_multifunction, generic
208 /***********************************************************/
209 /* XXX: the following should be moved to the PC BIOS */
211 static __attribute__((unused
)) uint32_t isa_inb(uint32_t addr
)
213 return cpu_inb(NULL
, addr
);
216 static void isa_outb(uint32_t val
, uint32_t addr
)
218 cpu_outb(NULL
, addr
, val
);
221 static __attribute__((unused
)) uint32_t isa_inw(uint32_t addr
)
223 return cpu_inw(NULL
, addr
);
226 static __attribute__((unused
)) void isa_outw(uint32_t val
, uint32_t addr
)
228 cpu_outw(NULL
, addr
, val
);
231 static __attribute__((unused
)) uint32_t isa_inl(uint32_t addr
)
233 return cpu_inl(NULL
, addr
);
236 static __attribute__((unused
)) void isa_outl(uint32_t val
, uint32_t addr
)
238 cpu_outl(NULL
, addr
, val
);
241 static uint32_t pci_bios_io_addr
;
242 static uint32_t pci_bios_mem_addr
;
243 /* host irqs corresponding to PCI irqs A-D */
244 static uint8_t pci_irqs
[4] = { 11, 9, 11, 9 };
246 static void pci_config_writel(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
249 addr
|= (pci_bus_num(s
) << 16) | (d
->devfn
<< 8);
250 pci_data_write(s
, addr
, val
, 4);
253 static void pci_config_writew(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
256 addr
|= (pci_bus_num(s
) << 16) | (d
->devfn
<< 8);
257 pci_data_write(s
, addr
, val
, 2);
260 static void pci_config_writeb(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
263 addr
|= (pci_bus_num(s
) << 16) | (d
->devfn
<< 8);
264 pci_data_write(s
, addr
, val
, 1);
267 static __attribute__((unused
)) uint32_t pci_config_readl(PCIDevice
*d
, uint32_t addr
)
270 addr
|= (pci_bus_num(s
) << 16) | (d
->devfn
<< 8);
271 return pci_data_read(s
, addr
, 4);
274 static uint32_t pci_config_readw(PCIDevice
*d
, uint32_t addr
)
277 addr
|= (pci_bus_num(s
) << 16) | (d
->devfn
<< 8);
278 return pci_data_read(s
, addr
, 2);
281 static uint32_t pci_config_readb(PCIDevice
*d
, uint32_t addr
)
284 addr
|= (pci_bus_num(s
) << 16) | (d
->devfn
<< 8);
285 return pci_data_read(s
, addr
, 1);
288 static void pci_set_io_region_addr(PCIDevice
*d
, int region_num
, uint32_t addr
)
294 if ( region_num
== PCI_ROM_SLOT
) {
297 ofs
= 0x10 + region_num
* 4;
300 pci_config_writel(d
, ofs
, addr
);
301 r
= &d
->io_regions
[region_num
];
303 /* enable memory mappings */
304 cmd
= pci_config_readw(d
, PCI_COMMAND
);
305 if ( region_num
== PCI_ROM_SLOT
)
307 else if (r
->type
& PCI_ADDRESS_SPACE_IO
)
311 pci_config_writew(d
, PCI_COMMAND
, cmd
);
314 static void pci_bios_init_device(PCIDevice
*d
)
319 int i
, pin
, pic_irq
, vendor_id
, device_id
;
321 class = pci_config_readw(d
, PCI_CLASS_DEVICE
);
322 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
323 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
326 if (vendor_id
== 0x8086 && device_id
== 0x7010) {
328 pci_config_writew(d
, 0x40, 0x8000); // enable IDE0
329 pci_config_writew(d
, 0x42, 0x8000); // enable IDE1
332 /* IDE: we map it as in ISA mode */
333 pci_set_io_region_addr(d
, 0, 0x1f0);
334 pci_set_io_region_addr(d
, 1, 0x3f4);
335 pci_set_io_region_addr(d
, 2, 0x170);
336 pci_set_io_region_addr(d
, 3, 0x374);
340 if (vendor_id
!= 0x1234)
342 /* VGA: map frame buffer to default Bochs VBE address */
343 pci_set_io_region_addr(d
, 0, 0xE0000000);
347 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
348 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
349 if (vendor_id
== 0x1014) {
351 if (device_id
== 0x0046 || device_id
== 0xFFFF) {
353 pci_set_io_region_addr(d
, 0, 0x80800000 + 0x00040000);
358 if (vendor_id
== 0x0106b &&
359 (device_id
== 0x0017 || device_id
== 0x0022)) {
361 pci_set_io_region_addr(d
, 0, 0x80800000);
366 /* default memory mappings */
367 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
368 r
= &d
->io_regions
[i
];
370 if (r
->type
& PCI_ADDRESS_SPACE_IO
)
371 paddr
= &pci_bios_io_addr
;
373 paddr
= &pci_bios_mem_addr
;
374 *paddr
= (*paddr
+ r
->size
- 1) & ~(r
->size
- 1);
375 pci_set_io_region_addr(d
, i
, *paddr
);
382 /* map the interrupt */
383 pin
= pci_config_readb(d
, PCI_INTERRUPT_PIN
);
385 pin
= pci_slot_get_pirq(d
, pin
- 1);
386 pic_irq
= pci_irqs
[pin
];
387 pci_config_writeb(d
, PCI_INTERRUPT_LINE
, pic_irq
);
392 * This function initializes the PCI devices as a normal PCI BIOS
393 * would do. It is provided just in case the BIOS has no support for
396 void pci_bios_init(void)
401 pci_bios_io_addr
= 0xc000;
402 pci_bios_mem_addr
= 0xf0000000;
404 /* activate IRQ mappings */
407 for(i
= 0; i
< 4; i
++) {
409 /* set to trigger level */
410 elcr
[irq
>> 3] |= (1 << (irq
& 7));
411 /* activate irq remapping in PIIX */
412 pci_config_writeb(piix3_dev
, 0x60 + i
, irq
);
414 isa_outb(elcr
[0], 0x4d0);
415 isa_outb(elcr
[1], 0x4d1);
417 pci_for_each_device(pci_bios_init_device
);