blob: 939521c7bbcbc94f91d3e7a429fce16435ba13a1 [file] [log] [blame]
pbrook502a5392006-05-13 16:11:23 +00001/*
2 * QEMU i440FX/PIIX3 PCI Bridge Emulation
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
25#include "vl.h"
26typedef uint32_t pci_addr_t;
27#include "pci_host.h"
28
29typedef PCIHostState I440FXState;
30
31static void i440fx_addr_writel(void* opaque, uint32_t addr, uint32_t val)
32{
33 I440FXState *s = opaque;
34 s->config_reg = val;
35}
36
37static uint32_t i440fx_addr_readl(void* opaque, uint32_t addr)
38{
39 I440FXState *s = opaque;
40 return s->config_reg;
41}
42
pbrookd2b59312006-09-24 00:16:34 +000043static void piix3_set_irq(void *pic, int irq_num, int level);
44
45/* return the global irq number corresponding to a given device irq
46 pin. We could also use the bus number to have a more precise
47 mapping. */
48static int pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
49{
50 int slot_addend;
51 slot_addend = (pci_dev->devfn >> 3) - 1;
52 return (irq_num + slot_addend) & 3;
53}
pbrook502a5392006-05-13 16:11:23 +000054
55PCIBus *i440fx_init(void)
56{
57 PCIBus *b;
58 PCIDevice *d;
59 I440FXState *s;
60
61 s = qemu_mallocz(sizeof(I440FXState));
pbrookd2b59312006-09-24 00:16:34 +000062 b = pci_register_bus(piix3_set_irq, pci_slot_get_pirq, NULL, 0);
pbrook502a5392006-05-13 16:11:23 +000063 s->bus = b;
64
65 register_ioport_write(0xcf8, 4, 4, i440fx_addr_writel, s);
66 register_ioport_read(0xcf8, 4, 4, i440fx_addr_readl, s);
67
68 register_ioport_write(0xcfc, 4, 1, pci_host_data_writeb, s);
69 register_ioport_write(0xcfc, 4, 2, pci_host_data_writew, s);
70 register_ioport_write(0xcfc, 4, 4, pci_host_data_writel, s);
71 register_ioport_read(0xcfc, 4, 1, pci_host_data_readb, s);
72 register_ioport_read(0xcfc, 4, 2, pci_host_data_readw, s);
73 register_ioport_read(0xcfc, 4, 4, pci_host_data_readl, s);
74
75 d = pci_register_device(b, "i440FX", sizeof(PCIDevice), 0,
76 NULL, NULL);
77
78 d->config[0x00] = 0x86; // vendor_id
79 d->config[0x01] = 0x80;
80 d->config[0x02] = 0x37; // device_id
81 d->config[0x03] = 0x12;
82 d->config[0x08] = 0x02; // revision
83 d->config[0x0a] = 0x00; // class_sub = host2pci
84 d->config[0x0b] = 0x06; // class_base = PCI_bridge
85 d->config[0x0e] = 0x00; // header_type
86 return b;
87}
88
89/* PIIX3 PCI to ISA bridge */
90
91static PCIDevice *piix3_dev;
92
93/* just used for simpler irq handling. */
94#define PCI_IRQ_WORDS ((PCI_DEVICES_MAX + 31) / 32)
95
pbrookd2b59312006-09-24 00:16:34 +000096static int pci_irq_levels[4];
pbrook502a5392006-05-13 16:11:23 +000097
pbrookd2b59312006-09-24 00:16:34 +000098static void piix3_set_irq(void *pic, int irq_num, int level)
pbrook502a5392006-05-13 16:11:23 +000099{
pbrookd2b59312006-09-24 00:16:34 +0000100 int i, pic_irq, pic_level;
pbrook502a5392006-05-13 16:11:23 +0000101
pbrookd2b59312006-09-24 00:16:34 +0000102 pci_irq_levels[irq_num] = level;
pbrook502a5392006-05-13 16:11:23 +0000103
104 /* now we change the pic irq level according to the piix irq mappings */
105 /* XXX: optimize */
106 pic_irq = piix3_dev->config[0x60 + irq_num];
107 if (pic_irq < 16) {
pbrookd2b59312006-09-24 00:16:34 +0000108 /* The pic level is the logical OR of all the PCI irqs mapped
pbrook502a5392006-05-13 16:11:23 +0000109 to it */
110 pic_level = 0;
pbrookd2b59312006-09-24 00:16:34 +0000111 for (i = 0; i < 4; i++) {
112 if (pic_irq == piix3_dev->config[0x60 + i])
113 pic_level |= pci_irq_levels[i];
114 }
pbrook502a5392006-05-13 16:11:23 +0000115 pic_set_irq(pic_irq, pic_level);
116 }
117}
118
119static void piix3_reset(PCIDevice *d)
120{
121 uint8_t *pci_conf = d->config;
122
123 pci_conf[0x04] = 0x07; // master, memory and I/O
124 pci_conf[0x05] = 0x00;
125 pci_conf[0x06] = 0x00;
126 pci_conf[0x07] = 0x02; // PCI_status_devsel_medium
127 pci_conf[0x4c] = 0x4d;
128 pci_conf[0x4e] = 0x03;
129 pci_conf[0x4f] = 0x00;
130 pci_conf[0x60] = 0x80;
131 pci_conf[0x69] = 0x02;
132 pci_conf[0x70] = 0x80;
133 pci_conf[0x76] = 0x0c;
134 pci_conf[0x77] = 0x0c;
135 pci_conf[0x78] = 0x02;
136 pci_conf[0x79] = 0x00;
137 pci_conf[0x80] = 0x00;
138 pci_conf[0x82] = 0x00;
139 pci_conf[0xa0] = 0x08;
140 pci_conf[0xa0] = 0x08;
141 pci_conf[0xa2] = 0x00;
142 pci_conf[0xa3] = 0x00;
143 pci_conf[0xa4] = 0x00;
144 pci_conf[0xa5] = 0x00;
145 pci_conf[0xa6] = 0x00;
146 pci_conf[0xa7] = 0x00;
147 pci_conf[0xa8] = 0x0f;
148 pci_conf[0xaa] = 0x00;
149 pci_conf[0xab] = 0x00;
150 pci_conf[0xac] = 0x00;
151 pci_conf[0xae] = 0x00;
152}
153
bellard1941d192006-08-17 10:46:34 +0000154static void piix_save(QEMUFile* f, void *opaque)
155{
156 PCIDevice *d = opaque;
157 pci_device_save(d, f);
158}
159
160static int piix_load(QEMUFile* f, void *opaque, int version_id)
161{
162 PCIDevice *d = opaque;
163 if (version_id != 2)
164 return -EINVAL;
165 return pci_device_load(d, f);
166}
167
pbrook502a5392006-05-13 16:11:23 +0000168int piix3_init(PCIBus *bus)
169{
170 PCIDevice *d;
171 uint8_t *pci_conf;
172
173 d = pci_register_device(bus, "PIIX3", sizeof(PCIDevice),
174 -1, NULL, NULL);
bellard1941d192006-08-17 10:46:34 +0000175 register_savevm("PIIX3", 0, 2, piix_save, piix_load, d);
pbrook502a5392006-05-13 16:11:23 +0000176
177 piix3_dev = d;
178 pci_conf = d->config;
179
180 pci_conf[0x00] = 0x86; // Intel
181 pci_conf[0x01] = 0x80;
182 pci_conf[0x02] = 0x00; // 82371SB PIIX3 PCI-to-ISA bridge (Step A1)
183 pci_conf[0x03] = 0x70;
184 pci_conf[0x0a] = 0x01; // class_sub = PCI_ISA
185 pci_conf[0x0b] = 0x06; // class_base = PCI_bridge
186 pci_conf[0x0e] = 0x80; // header_type = PCI_multifunction, generic
187
188 piix3_reset(d);
189 return d->devfn;
190}
191
192/***********************************************************/
193/* XXX: the following should be moved to the PC BIOS */
194
195static __attribute__((unused)) uint32_t isa_inb(uint32_t addr)
196{
197 return cpu_inb(NULL, addr);
198}
199
200static void isa_outb(uint32_t val, uint32_t addr)
201{
202 cpu_outb(NULL, addr, val);
203}
204
205static __attribute__((unused)) uint32_t isa_inw(uint32_t addr)
206{
207 return cpu_inw(NULL, addr);
208}
209
210static __attribute__((unused)) void isa_outw(uint32_t val, uint32_t addr)
211{
212 cpu_outw(NULL, addr, val);
213}
214
215static __attribute__((unused)) uint32_t isa_inl(uint32_t addr)
216{
217 return cpu_inl(NULL, addr);
218}
219
220static __attribute__((unused)) void isa_outl(uint32_t val, uint32_t addr)
221{
222 cpu_outl(NULL, addr, val);
223}
224
225static uint32_t pci_bios_io_addr;
226static uint32_t pci_bios_mem_addr;
227/* host irqs corresponding to PCI irqs A-D */
228static uint8_t pci_irqs[4] = { 11, 9, 11, 9 };
229
230static void pci_config_writel(PCIDevice *d, uint32_t addr, uint32_t val)
231{
232 PCIBus *s = d->bus;
233 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
234 pci_data_write(s, addr, val, 4);
235}
236
237static void pci_config_writew(PCIDevice *d, uint32_t addr, uint32_t val)
238{
239 PCIBus *s = d->bus;
240 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
241 pci_data_write(s, addr, val, 2);
242}
243
244static void pci_config_writeb(PCIDevice *d, uint32_t addr, uint32_t val)
245{
246 PCIBus *s = d->bus;
247 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
248 pci_data_write(s, addr, val, 1);
249}
250
251static __attribute__((unused)) uint32_t pci_config_readl(PCIDevice *d, uint32_t addr)
252{
253 PCIBus *s = d->bus;
254 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
255 return pci_data_read(s, addr, 4);
256}
257
258static uint32_t pci_config_readw(PCIDevice *d, uint32_t addr)
259{
260 PCIBus *s = d->bus;
261 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
262 return pci_data_read(s, addr, 2);
263}
264
265static uint32_t pci_config_readb(PCIDevice *d, uint32_t addr)
266{
267 PCIBus *s = d->bus;
268 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
269 return pci_data_read(s, addr, 1);
270}
271
272static void pci_set_io_region_addr(PCIDevice *d, int region_num, uint32_t addr)
273{
274 PCIIORegion *r;
275 uint16_t cmd;
276 uint32_t ofs;
277
278 if ( region_num == PCI_ROM_SLOT ) {
279 ofs = 0x30;
280 }else{
281 ofs = 0x10 + region_num * 4;
282 }
283
284 pci_config_writel(d, ofs, addr);
285 r = &d->io_regions[region_num];
286
287 /* enable memory mappings */
288 cmd = pci_config_readw(d, PCI_COMMAND);
289 if ( region_num == PCI_ROM_SLOT )
290 cmd |= 2;
291 else if (r->type & PCI_ADDRESS_SPACE_IO)
292 cmd |= 1;
293 else
294 cmd |= 2;
295 pci_config_writew(d, PCI_COMMAND, cmd);
296}
297
298static void pci_bios_init_device(PCIDevice *d)
299{
300 int class;
301 PCIIORegion *r;
302 uint32_t *paddr;
303 int i, pin, pic_irq, vendor_id, device_id;
304
305 class = pci_config_readw(d, PCI_CLASS_DEVICE);
306 vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
307 device_id = pci_config_readw(d, PCI_DEVICE_ID);
308 switch(class) {
309 case 0x0101:
310 if (vendor_id == 0x8086 && device_id == 0x7010) {
311 /* PIIX3 IDE */
312 pci_config_writew(d, 0x40, 0x8000); // enable IDE0
313 pci_config_writew(d, 0x42, 0x8000); // enable IDE1
314 goto default_map;
315 } else {
316 /* IDE: we map it as in ISA mode */
317 pci_set_io_region_addr(d, 0, 0x1f0);
318 pci_set_io_region_addr(d, 1, 0x3f4);
319 pci_set_io_region_addr(d, 2, 0x170);
320 pci_set_io_region_addr(d, 3, 0x374);
321 }
322 break;
323 case 0x0300:
324 if (vendor_id != 0x1234)
325 goto default_map;
326 /* VGA: map frame buffer to default Bochs VBE address */
327 pci_set_io_region_addr(d, 0, 0xE0000000);
328 break;
329 case 0x0800:
330 /* PIC */
331 vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
332 device_id = pci_config_readw(d, PCI_DEVICE_ID);
333 if (vendor_id == 0x1014) {
334 /* IBM */
335 if (device_id == 0x0046 || device_id == 0xFFFF) {
336 /* MPIC & MPIC2 */
337 pci_set_io_region_addr(d, 0, 0x80800000 + 0x00040000);
338 }
339 }
340 break;
341 case 0xff00:
342 if (vendor_id == 0x0106b &&
343 (device_id == 0x0017 || device_id == 0x0022)) {
344 /* macio bridge */
345 pci_set_io_region_addr(d, 0, 0x80800000);
346 }
347 break;
348 default:
349 default_map:
350 /* default memory mappings */
351 for(i = 0; i < PCI_NUM_REGIONS; i++) {
352 r = &d->io_regions[i];
353 if (r->size) {
354 if (r->type & PCI_ADDRESS_SPACE_IO)
355 paddr = &pci_bios_io_addr;
356 else
357 paddr = &pci_bios_mem_addr;
358 *paddr = (*paddr + r->size - 1) & ~(r->size - 1);
359 pci_set_io_region_addr(d, i, *paddr);
360 *paddr += r->size;
361 }
362 }
363 break;
364 }
365
366 /* map the interrupt */
367 pin = pci_config_readb(d, PCI_INTERRUPT_PIN);
368 if (pin != 0) {
369 pin = pci_slot_get_pirq(d, pin - 1);
370 pic_irq = pci_irqs[pin];
371 pci_config_writeb(d, PCI_INTERRUPT_LINE, pic_irq);
372 }
373}
374
375/*
376 * This function initializes the PCI devices as a normal PCI BIOS
377 * would do. It is provided just in case the BIOS has no support for
378 * PCI.
379 */
380void pci_bios_init(void)
381{
382 int i, irq;
383 uint8_t elcr[2];
384
385 pci_bios_io_addr = 0xc000;
386 pci_bios_mem_addr = 0xf0000000;
387
388 /* activate IRQ mappings */
389 elcr[0] = 0x00;
390 elcr[1] = 0x00;
391 for(i = 0; i < 4; i++) {
392 irq = pci_irqs[i];
393 /* set to trigger level */
394 elcr[irq >> 3] |= (1 << (irq & 7));
395 /* activate irq remapping in PIIX */
396 pci_config_writeb(piix3_dev, 0x60 + i, irq);
397 }
398 isa_outb(elcr[0], 0x4d0);
399 isa_outb(elcr[1], 0x4d1);
400
401 pci_for_each_device(pci_bios_init_device);
402}
403