IRQ handling:
[qemu-kvm/fedora.git] / hw / piix_pci.c
blobd143d59ae2c9400889353eb0a0a60c6746de73c1
1 /*
2 * QEMU i440FX/PIIX3 PCI Bridge Emulation
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:
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
22 * THE SOFTWARE.
25 #include "vl.h"
26 typedef uint32_t pci_addr_t;
27 #include "pci_host.h"
29 typedef PCIHostState I440FXState;
31 static void i440fx_addr_writel(void* opaque, uint32_t addr, uint32_t val)
33 I440FXState *s = opaque;
34 s->config_reg = val;
37 static uint32_t i440fx_addr_readl(void* opaque, uint32_t addr)
39 I440FXState *s = opaque;
40 return s->config_reg;
43 static void piix3_set_irq(PCIDevice *pci_dev, void *pic, int irq_num, int level);
45 PCIBus *i440fx_init(void)
47 PCIBus *b;
48 PCIDevice *d;
49 I440FXState *s;
51 s = qemu_mallocz(sizeof(I440FXState));
52 b = pci_register_bus(piix3_set_irq, NULL, 0);
53 s->bus = b;
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,
66 NULL, NULL);
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
76 return b;
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
90 mapping. */
91 static inline int pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
93 int slot_addend;
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)
100 int pic_level;
101 #if (PCI_IRQ_WORDS == 2)
102 pic_level = ((pci_irq_levels[irq_num][0] |
103 pci_irq_levels[irq_num][1]) != 0);
104 #else
106 int i;
107 pic_level = 0;
108 for(i = 0; i < PCI_IRQ_WORDS; i++) {
109 if (pci_irq_levels[irq_num][i]) {
110 pic_level = 1;
111 break;
115 #endif
116 return pic_level;
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;
122 uint32_t *p;
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 */
131 /* XXX: optimize */
132 pic_irq = piix3_dev->config[0x60 + irq_num];
133 if (pic_irq < 16) {
134 /* the pic level is the logical OR of all the PCI irqs mapped
135 to it */
136 pic_level = 0;
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[0x61] = 0x80;
162 pci_conf[0x62] = 0x80;
163 pci_conf[0x63] = 0x80;
164 pci_conf[0x69] = 0x02;
165 pci_conf[0x70] = 0x80;
166 pci_conf[0x71] = 0x80;
167 pci_conf[0x76] = 0x0c;
168 pci_conf[0x77] = 0x0c;
169 pci_conf[0x78] = 0x02;
170 pci_conf[0x79] = 0x00;
171 pci_conf[0x80] = 0x00;
172 pci_conf[0x82] = 0x00;
173 pci_conf[0xa0] = 0x08;
174 pci_conf[0xa0] = 0x08;
175 pci_conf[0xa2] = 0x00;
176 pci_conf[0xa3] = 0x00;
177 pci_conf[0xa4] = 0x00;
178 pci_conf[0xa5] = 0x00;
179 pci_conf[0xa6] = 0x00;
180 pci_conf[0xa7] = 0x00;
181 pci_conf[0xa8] = 0x0f;
182 pci_conf[0xaa] = 0x00;
183 pci_conf[0xab] = 0x00;
184 pci_conf[0xac] = 0x00;
185 pci_conf[0xae] = 0x00;
188 int piix3_init(PCIBus *bus)
190 PCIDevice *d;
191 uint8_t *pci_conf;
193 d = pci_register_device(bus, "PIIX3", sizeof(PCIDevice),
194 -1, NULL, NULL);
195 register_savevm("PIIX3", 0, 1, generic_pci_save, generic_pci_load, d);
197 piix3_dev = d;
198 pci_conf = d->config;
200 pci_conf[0x00] = 0x86; // Intel
201 pci_conf[0x01] = 0x80;
202 pci_conf[0x02] = 0x00; // 82371SB PIIX3 PCI-to-ISA bridge (Step A1)
203 pci_conf[0x03] = 0x70;
204 pci_conf[0x0a] = 0x01; // class_sub = PCI_ISA
205 pci_conf[0x0b] = 0x06; // class_base = PCI_bridge
206 pci_conf[0x0e] = 0x80; // header_type = PCI_multifunction, generic
208 piix3_reset(d);
209 return d->devfn;
212 /***********************************************************/
213 /* XXX: the following should be moved to the PC BIOS */
215 static __attribute__((unused)) uint32_t isa_inb(uint32_t addr)
217 return cpu_inb(NULL, addr);
220 static void isa_outb(uint32_t val, uint32_t addr)
222 cpu_outb(NULL, addr, val);
225 static __attribute__((unused)) uint32_t isa_inw(uint32_t addr)
227 return cpu_inw(NULL, addr);
230 static __attribute__((unused)) void isa_outw(uint32_t val, uint32_t addr)
232 cpu_outw(NULL, addr, val);
235 static __attribute__((unused)) uint32_t isa_inl(uint32_t addr)
237 return cpu_inl(NULL, addr);
240 static __attribute__((unused)) void isa_outl(uint32_t val, uint32_t addr)
242 cpu_outl(NULL, addr, val);
245 static uint32_t pci_bios_io_addr;
246 static uint32_t pci_bios_mem_addr;
247 /* host irqs corresponding to PCI irqs A-D */
248 static uint8_t pci_irqs[4] = { 11, 9, 11, 9 };
250 static void pci_config_writel(PCIDevice *d, uint32_t addr, uint32_t val)
252 PCIBus *s = d->bus;
253 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
254 pci_data_write(s, addr, val, 4);
257 static void pci_config_writew(PCIDevice *d, uint32_t addr, uint32_t val)
259 PCIBus *s = d->bus;
260 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
261 pci_data_write(s, addr, val, 2);
264 static void pci_config_writeb(PCIDevice *d, uint32_t addr, uint32_t val)
266 PCIBus *s = d->bus;
267 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
268 pci_data_write(s, addr, val, 1);
271 static __attribute__((unused)) uint32_t pci_config_readl(PCIDevice *d, uint32_t addr)
273 PCIBus *s = d->bus;
274 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
275 return pci_data_read(s, addr, 4);
278 static uint32_t pci_config_readw(PCIDevice *d, uint32_t addr)
280 PCIBus *s = d->bus;
281 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
282 return pci_data_read(s, addr, 2);
285 static uint32_t pci_config_readb(PCIDevice *d, uint32_t addr)
287 PCIBus *s = d->bus;
288 addr |= (pci_bus_num(s) << 16) | (d->devfn << 8);
289 return pci_data_read(s, addr, 1);
292 static void pci_set_io_region_addr(PCIDevice *d, int region_num, uint32_t addr)
294 PCIIORegion *r;
295 uint16_t cmd;
296 uint32_t ofs;
298 if ( region_num == PCI_ROM_SLOT ) {
299 ofs = 0x30;
300 }else{
301 ofs = 0x10 + region_num * 4;
304 pci_config_writel(d, ofs, addr);
305 r = &d->io_regions[region_num];
307 /* enable memory mappings */
308 cmd = pci_config_readw(d, PCI_COMMAND);
309 if ( region_num == PCI_ROM_SLOT )
310 cmd |= 2;
311 else if (r->type & PCI_ADDRESS_SPACE_IO)
312 cmd |= 1;
313 else
314 cmd |= 2;
315 pci_config_writew(d, PCI_COMMAND, cmd);
318 static void pci_bios_init_device(PCIDevice *d)
320 int class;
321 PCIIORegion *r;
322 uint32_t *paddr;
323 int i, pin, pic_irq, vendor_id, device_id;
325 class = pci_config_readw(d, PCI_CLASS_DEVICE);
326 vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
327 device_id = pci_config_readw(d, PCI_DEVICE_ID);
328 switch(class) {
329 case 0x0101:
330 if (vendor_id == 0x8086 && device_id == 0x7010) {
331 /* PIIX3 IDE */
332 pci_config_writew(d, 0x40, 0x8000); // enable IDE0
333 pci_config_writew(d, 0x42, 0x8000); // enable IDE1
334 goto default_map;
335 } else {
336 /* IDE: we map it as in ISA mode */
337 pci_set_io_region_addr(d, 0, 0x1f0);
338 pci_set_io_region_addr(d, 1, 0x3f4);
339 pci_set_io_region_addr(d, 2, 0x170);
340 pci_set_io_region_addr(d, 3, 0x374);
342 break;
343 case 0x0300:
344 if (vendor_id != 0x1234)
345 goto default_map;
346 /* VGA: map frame buffer to default Bochs VBE address */
347 pci_set_io_region_addr(d, 0, 0xE0000000);
348 break;
349 case 0x0800:
350 /* PIC */
351 vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
352 device_id = pci_config_readw(d, PCI_DEVICE_ID);
353 if (vendor_id == 0x1014) {
354 /* IBM */
355 if (device_id == 0x0046 || device_id == 0xFFFF) {
356 /* MPIC & MPIC2 */
357 pci_set_io_region_addr(d, 0, 0x80800000 + 0x00040000);
360 break;
361 case 0xff00:
362 if (vendor_id == 0x0106b &&
363 (device_id == 0x0017 || device_id == 0x0022)) {
364 /* macio bridge */
365 pci_set_io_region_addr(d, 0, 0x80800000);
367 break;
368 default:
369 default_map:
370 /* default memory mappings */
371 for(i = 0; i < PCI_NUM_REGIONS; i++) {
372 r = &d->io_regions[i];
373 if (r->size) {
374 if (r->type & PCI_ADDRESS_SPACE_IO)
375 paddr = &pci_bios_io_addr;
376 else
377 paddr = &pci_bios_mem_addr;
378 *paddr = (*paddr + r->size - 1) & ~(r->size - 1);
379 pci_set_io_region_addr(d, i, *paddr);
380 *paddr += r->size;
383 break;
386 /* map the interrupt */
387 pin = pci_config_readb(d, PCI_INTERRUPT_PIN);
388 if (pin != 0) {
389 pin = pci_slot_get_pirq(d, pin - 1);
390 pic_irq = pci_irqs[pin];
391 pci_config_writeb(d, PCI_INTERRUPT_LINE, pic_irq);
396 * This function initializes the PCI devices as a normal PCI BIOS
397 * would do. It is provided just in case the BIOS has no support for
398 * PCI.
400 void pci_bios_init(void)
402 int i, irq;
403 uint8_t elcr[2];
405 pci_bios_io_addr = 0xc000;
406 pci_bios_mem_addr = 0xf0000000;
408 /* activate IRQ mappings */
409 elcr[0] = 0x00;
410 elcr[1] = 0x00;
411 for(i = 0; i < 4; i++) {
412 irq = pci_irqs[i];
413 /* set to trigger level */
414 elcr[irq >> 3] |= (1 << (irq & 7));
415 /* activate irq remapping in PIIX */
416 pci_config_writeb(piix3_dev, 0x60 + i, irq);
418 isa_outb(elcr[0], 0x4d0);
419 isa_outb(elcr[1], 0x4d1);
421 pci_for_each_device(pci_bios_init_device);