Disable preadv/pwritev support
[qemu-kvm/fedora.git] / hw / pci.c
blob9bcf49dfa2eff134ac062b1a728960629fb82592
1 /*
2 * QEMU PCI bus manager
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
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "pci.h"
26 #include "monitor.h"
27 #include "net.h"
28 #include "sysemu.h"
29 #include "pc.h"
30 #include "qemu-kvm.h"
31 #include "device-assignment.h"
33 //#define DEBUG_PCI
34 #ifdef DEBUG_PCI
35 # define PCI_DPRINTF(format, ...) printf(format, __VA_ARGS__)
36 #else
37 # define PCI_DPRINTF(format, ...) do { } while (0)
38 #endif
40 struct PCIBus {
41 BusState qbus;
42 int bus_num;
43 int devfn_min;
44 pci_set_irq_fn set_irq;
45 pci_map_irq_fn map_irq;
46 uint32_t config_reg; /* XXX: suppress */
47 /* low level pic */
48 SetIRQFunc *low_set_irq;
49 qemu_irq *irq_opaque;
50 PCIDevice *devices[256];
51 PCIDevice *parent_dev;
52 PCIBus *next;
53 /* The bus IRQ state is the logical OR of the connected devices.
54 Keep a count of the number of devices with raised IRQs. */
55 int nirq;
56 int *irq_count;
59 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
61 static struct BusInfo pci_bus_info = {
62 .name = "PCI",
63 .size = sizeof(PCIBus),
64 .print_dev = pcibus_dev_print,
65 .props = (Property[]) {
67 .name = "devfn",
68 .info = &qdev_prop_uint32,
69 .offset = offsetof(PCIDevice, devfn),
70 .defval = (uint32_t[]) { -1 },
72 {/* end of list */}
76 static void pci_update_mappings(PCIDevice *d);
77 static void pci_set_irq(void *opaque, int irq_num, int level);
79 target_phys_addr_t pci_mem_base;
80 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
81 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
82 static PCIBus *first_bus;
84 static void pcibus_save(QEMUFile *f, void *opaque)
86 PCIBus *bus = (PCIBus *)opaque;
87 int i;
89 qemu_put_be32(f, bus->nirq);
90 for (i = 0; i < bus->nirq; i++)
91 qemu_put_be32(f, bus->irq_count[i]);
94 static int pcibus_load(QEMUFile *f, void *opaque, int version_id)
96 PCIBus *bus = (PCIBus *)opaque;
97 int i, nirq;
99 if (version_id != 1)
100 return -EINVAL;
102 nirq = qemu_get_be32(f);
103 if (bus->nirq != nirq) {
104 fprintf(stderr, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
105 nirq, bus->nirq);
106 return -EINVAL;
109 for (i = 0; i < nirq; i++)
110 bus->irq_count[i] = qemu_get_be32(f);
112 return 0;
115 static void pci_bus_reset(void *opaque)
117 PCIBus *bus = (PCIBus *)opaque;
118 int i;
120 for (i = 0; i < bus->nirq; i++) {
121 bus->irq_count[i] = 0;
123 for (i = 0; i < 256; i++) {
124 if (bus->devices[i])
125 memset(bus->devices[i]->irq_state, 0,
126 sizeof(bus->devices[i]->irq_state));
130 PCIBus *pci_register_bus(DeviceState *parent, const char *name,
131 pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
132 qemu_irq *pic, int devfn_min, int nirq)
134 PCIBus *bus;
135 static int nbus = 0;
137 bus = FROM_QBUS(PCIBus, qbus_create(&pci_bus_info, parent, name));
138 bus->set_irq = set_irq;
139 bus->map_irq = map_irq;
140 bus->irq_opaque = pic;
141 bus->devfn_min = devfn_min;
142 bus->nirq = nirq;
143 bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
144 bus->next = first_bus;
145 first_bus = bus;
146 register_savevm("PCIBUS", nbus++, 1, pcibus_save, pcibus_load, bus);
147 qemu_register_reset(pci_bus_reset, bus);
148 return bus;
151 static PCIBus *pci_register_secondary_bus(PCIDevice *dev, pci_map_irq_fn map_irq)
153 PCIBus *bus;
155 bus = qemu_mallocz(sizeof(PCIBus));
156 bus->map_irq = map_irq;
157 bus->parent_dev = dev;
158 bus->next = dev->bus->next;
159 dev->bus->next = bus;
160 return bus;
163 int pci_bus_num(PCIBus *s)
165 return s->bus_num;
168 void pci_device_save(PCIDevice *s, QEMUFile *f)
170 int i;
172 qemu_put_be32(f, 2); /* PCI device version */
173 qemu_put_buffer(f, s->config, 256);
174 for (i = 0; i < 4; i++)
175 qemu_put_be32(f, s->irq_state[i]);
178 int pci_device_load(PCIDevice *s, QEMUFile *f)
180 uint8_t config[PCI_CONFIG_SPACE_SIZE];
181 uint32_t version_id;
182 int i;
184 version_id = qemu_get_be32(f);
185 if (version_id > 2)
186 return -EINVAL;
187 qemu_get_buffer(f, config, sizeof config);
188 for (i = 0; i < sizeof config; ++i)
189 if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i])
190 return -EINVAL;
191 memcpy(s->config, config, sizeof config);
193 pci_update_mappings(s);
195 if (version_id >= 2)
196 for (i = 0; i < 4; i ++)
197 s->irq_state[i] = qemu_get_be32(f);
198 return 0;
201 static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
203 uint16_t *id;
205 id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
206 id[0] = cpu_to_le16(pci_default_sub_vendor_id);
207 id[1] = cpu_to_le16(pci_default_sub_device_id);
208 return 0;
212 * Parse pci address in qemu command
213 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
215 static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
217 const char *p;
218 char *e;
219 unsigned long val;
220 unsigned long dom = 0, bus = 0;
221 unsigned slot = 0;
223 p = addr;
224 val = strtoul(p, &e, 16);
225 if (e == p)
226 return -1;
227 if (*e == ':') {
228 bus = val;
229 p = e + 1;
230 val = strtoul(p, &e, 16);
231 if (e == p)
232 return -1;
233 if (*e == ':') {
234 dom = bus;
235 bus = val;
236 p = e + 1;
237 val = strtoul(p, &e, 16);
238 if (e == p)
239 return -1;
243 if (dom > 0xffff || bus > 0xff || val > 0x1f)
244 return -1;
246 slot = val;
248 if (*e)
249 return -1;
251 /* Note: QEMU doesn't implement domains other than 0 */
252 if (dom != 0 || pci_find_bus(bus) == NULL)
253 return -1;
255 *domp = dom;
256 *busp = bus;
257 *slotp = slot;
258 return 0;
262 * Parse device bdf in device assignment command:
264 * -pcidevice host=bus:dev.func
266 * Parse <bus>:<slot>.<func> return -1 on error
268 int pci_parse_host_devaddr(const char *addr, int *busp,
269 int *slotp, int *funcp)
271 const char *p;
272 char *e;
273 int val;
274 int bus = 0, slot = 0, func = 0;
276 p = addr;
277 val = strtoul(p, &e, 16);
278 if (e == p)
279 return -1;
280 if (*e == ':') {
281 bus = val;
282 p = e + 1;
283 val = strtoul(p, &e, 16);
284 if (e == p)
285 return -1;
286 if (*e == '.') {
287 slot = val;
288 p = e + 1;
289 val = strtoul(p, &e, 16);
290 if (e == p)
291 return -1;
292 func = val;
293 } else
294 return -1;
295 } else
296 return -1;
298 if (bus > 0xff || slot > 0x1f || func > 0x7)
299 return -1;
301 if (*e)
302 return -1;
304 *busp = bus;
305 *slotp = slot;
306 *funcp = func;
307 return 0;
310 int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
311 unsigned *slotp)
313 /* strip legacy tag */
314 if (!strncmp(addr, "pci_addr=", 9)) {
315 addr += 9;
317 if (pci_parse_devaddr(addr, domp, busp, slotp)) {
318 monitor_printf(mon, "Invalid pci address\n");
319 return -1;
321 return 0;
324 PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
326 int dom, bus;
327 unsigned slot;
329 if (!devaddr) {
330 *devfnp = -1;
331 return pci_find_bus(0);
334 if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
335 return NULL;
338 *devfnp = slot << 3;
339 return pci_find_bus(bus);
342 static void pci_init_cmask(PCIDevice *dev)
344 pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
345 pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
346 dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
347 dev->cmask[PCI_REVISION_ID] = 0xff;
348 dev->cmask[PCI_CLASS_PROG] = 0xff;
349 pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
350 dev->cmask[PCI_HEADER_TYPE] = 0xff;
351 dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
354 static void pci_init_wmask(PCIDevice *dev)
356 int i;
357 dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
358 dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
359 dev->wmask[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY
360 | PCI_COMMAND_MASTER;
361 for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i)
362 dev->wmask[i] = 0xff;
365 /* -1 for devfn means auto assign */
366 static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
367 const char *name, int devfn,
368 PCIConfigReadFunc *config_read,
369 PCIConfigWriteFunc *config_write)
371 if (devfn < 0) {
372 for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
373 if (!bus->devices[devfn])
374 goto found;
376 return NULL;
377 found: ;
378 } else if (bus->devices[devfn]) {
379 return NULL;
381 pci_dev->bus = bus;
382 pci_dev->devfn = devfn;
383 pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
384 memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
385 pci_set_default_subsystem_id(pci_dev);
386 pci_init_cmask(pci_dev);
387 pci_init_wmask(pci_dev);
389 if (!config_read)
390 config_read = pci_default_read_config;
391 if (!config_write)
392 config_write = pci_default_write_config;
393 pci_dev->config_read = config_read;
394 pci_dev->config_write = config_write;
395 bus->devices[devfn] = pci_dev;
396 pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, 4);
397 return pci_dev;
400 PCIDevice *pci_register_device(PCIBus *bus, const char *name,
401 int instance_size, int devfn,
402 PCIConfigReadFunc *config_read,
403 PCIConfigWriteFunc *config_write)
405 PCIDevice *pci_dev;
407 pci_dev = qemu_mallocz(instance_size);
408 pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
409 config_read, config_write);
410 return pci_dev;
412 static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
414 return addr + pci_mem_base;
417 static void pci_unregister_io_regions(PCIDevice *pci_dev)
419 PCIIORegion *r;
420 int i;
422 for(i = 0; i < PCI_NUM_REGIONS; i++) {
423 r = &pci_dev->io_regions[i];
424 if (!r->size || r->addr == -1)
425 continue;
426 if (r->type == PCI_ADDRESS_SPACE_IO) {
427 isa_unassign_ioport(r->addr, r->size);
428 } else {
429 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
430 r->size,
431 IO_MEM_UNASSIGNED);
436 int pci_unregister_device(PCIDevice *pci_dev, int assigned)
438 int ret = 0;
440 if (pci_dev->unregister)
441 ret = pci_dev->unregister(pci_dev);
442 if (ret)
443 return ret;
445 pci_unregister_io_regions(pci_dev);
447 qemu_free_irqs(pci_dev->irq);
448 pci_dev->bus->devices[pci_dev->devfn] = NULL;
450 if (assigned)
451 qemu_free(pci_dev);
452 else
453 qdev_free(&pci_dev->qdev);
454 return 0;
457 void pci_register_bar(PCIDevice *pci_dev, int region_num,
458 uint32_t size, int type,
459 PCIMapIORegionFunc *map_func)
461 PCIIORegion *r;
462 uint32_t addr;
463 uint32_t wmask;
465 if ((unsigned int)region_num >= PCI_NUM_REGIONS)
466 return;
468 if (size & (size-1)) {
469 fprintf(stderr, "ERROR: PCI region size must be pow2 "
470 "type=0x%x, size=0x%x\n", type, size);
471 exit(1);
474 r = &pci_dev->io_regions[region_num];
475 r->addr = -1;
476 r->size = size;
477 r->type = type;
478 r->map_func = map_func;
480 wmask = ~(size - 1);
481 if (region_num == PCI_ROM_SLOT) {
482 addr = 0x30;
483 /* ROM enable bit is writeable */
484 wmask |= 1;
485 } else {
486 addr = 0x10 + region_num * 4;
488 *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
489 *(uint32_t *)(pci_dev->wmask + addr) = cpu_to_le32(wmask);
490 *(uint32_t *)(pci_dev->cmask + addr) = 0xffffffff;
493 static void pci_update_mappings(PCIDevice *d)
495 PCIIORegion *r;
496 int cmd, i;
497 uint32_t last_addr, new_addr, config_ofs;
499 cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
500 for(i = 0; i < PCI_NUM_REGIONS; i++) {
501 r = &d->io_regions[i];
502 if (i == PCI_ROM_SLOT) {
503 config_ofs = 0x30;
504 } else {
505 config_ofs = 0x10 + i * 4;
507 if (r->size != 0) {
508 if (r->type & PCI_ADDRESS_SPACE_IO) {
509 if (cmd & PCI_COMMAND_IO) {
510 new_addr = le32_to_cpu(*(uint32_t *)(d->config +
511 config_ofs));
512 new_addr = new_addr & ~(r->size - 1);
513 last_addr = new_addr + r->size - 1;
514 /* NOTE: we have only 64K ioports on PC */
515 if (last_addr <= new_addr || new_addr == 0 ||
516 last_addr >= 0x10000) {
517 new_addr = -1;
519 } else {
520 new_addr = -1;
522 } else {
523 if (cmd & PCI_COMMAND_MEMORY) {
524 new_addr = le32_to_cpu(*(uint32_t *)(d->config +
525 config_ofs));
526 /* the ROM slot has a specific enable bit */
527 if (i == PCI_ROM_SLOT && !(new_addr & 1))
528 goto no_mem_map;
529 new_addr = new_addr & ~(r->size - 1);
530 last_addr = new_addr + r->size - 1;
531 /* NOTE: we do not support wrapping */
532 /* XXX: as we cannot support really dynamic
533 mappings, we handle specific values as invalid
534 mappings. */
535 if (last_addr <= new_addr || new_addr == 0 ||
536 last_addr == -1) {
537 new_addr = -1;
539 } else {
540 no_mem_map:
541 new_addr = -1;
544 /* now do the real mapping */
545 if (new_addr != r->addr) {
546 if (r->addr != -1) {
547 if (r->type & PCI_ADDRESS_SPACE_IO) {
548 int class;
549 /* NOTE: specific hack for IDE in PC case:
550 only one byte must be mapped. */
551 class = d->config[0x0a] | (d->config[0x0b] << 8);
552 if (class == 0x0101 && r->size == 4) {
553 isa_unassign_ioport(r->addr + 2, 1);
554 } else {
555 isa_unassign_ioport(r->addr, r->size);
557 } else {
558 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
559 r->size,
560 IO_MEM_UNASSIGNED);
561 qemu_unregister_coalesced_mmio(r->addr, r->size);
564 r->addr = new_addr;
565 if (r->addr != -1) {
566 r->map_func(d, i, r->addr, r->size, r->type);
573 static uint32_t pci_read_config(PCIDevice *d,
574 uint32_t address, int len)
576 uint32_t val;
578 switch(len) {
579 default:
580 case 4:
581 if (address <= 0xfc) {
582 val = le32_to_cpu(*(uint32_t *)(d->config + address));
583 break;
585 /* fall through */
586 case 2:
587 if (address <= 0xfe) {
588 val = le16_to_cpu(*(uint16_t *)(d->config + address));
589 break;
591 /* fall through */
592 case 1:
593 val = d->config[address];
594 break;
596 return val;
599 static void pci_write_config(PCIDevice *pci_dev,
600 uint32_t address, uint32_t val, int len)
602 int i;
603 for (i = 0; i < len; i++) {
604 pci_dev->config[address + i] = val & 0xff;
605 val >>= 8;
609 int pci_access_cap_config(PCIDevice *pci_dev, uint32_t address, int len)
611 if (pci_dev->cap.supported && address >= pci_dev->cap.start &&
612 (address + len) < pci_dev->cap.start + pci_dev->cap.length)
613 return 1;
614 return 0;
617 uint32_t pci_default_cap_read_config(PCIDevice *pci_dev,
618 uint32_t address, int len)
620 return pci_read_config(pci_dev, address, len);
623 void pci_default_cap_write_config(PCIDevice *pci_dev,
624 uint32_t address, uint32_t val, int len)
626 pci_write_config(pci_dev, address, val, len);
629 uint32_t pci_default_read_config(PCIDevice *d,
630 uint32_t address, int len)
632 if (pci_access_cap_config(d, address, len))
633 return d->cap.config_read(d, address, len);
635 return pci_read_config(d, address, len);
638 void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
640 uint8_t orig[PCI_CONFIG_SPACE_SIZE];
641 int i;
643 if (pci_access_cap_config(d, addr, l)) {
644 d->cap.config_write(d, addr, val, l);
645 return;
648 /* not efficient, but simple */
649 memcpy(orig, d->config, PCI_CONFIG_SPACE_SIZE);
650 for(i = 0; i < l && addr < PCI_CONFIG_SPACE_SIZE; val >>= 8, ++i, ++addr) {
651 uint8_t wmask = d->wmask[addr];
652 d->config[addr] = (d->config[addr] & ~wmask) | (val & wmask);
655 #ifdef USE_KVM_DEVICE_ASSIGNMENT
656 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel() &&
657 addr >= PIIX_CONFIG_IRQ_ROUTE &&
658 addr < PIIX_CONFIG_IRQ_ROUTE + 4)
659 assigned_dev_update_irqs();
660 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
662 if (memcmp(orig + PCI_BASE_ADDRESS_0, d->config + PCI_BASE_ADDRESS_0, 24)
663 || ((orig[PCI_COMMAND] ^ d->config[PCI_COMMAND])
664 & (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)))
665 pci_update_mappings(d);
669 void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
671 PCIBus *s = opaque;
672 PCIDevice *pci_dev;
673 int config_addr, bus_num;
675 #if 0
676 PCI_DPRINTF("pci_data_write: addr=%08x val=%08x len=%d\n",
677 addr, val, len);
678 #endif
679 bus_num = (addr >> 16) & 0xff;
680 while (s && s->bus_num != bus_num)
681 s = s->next;
682 if (!s)
683 return;
684 pci_dev = s->devices[(addr >> 8) & 0xff];
685 if (!pci_dev)
686 return;
687 config_addr = addr & 0xff;
688 PCI_DPRINTF("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
689 pci_dev->name, config_addr, val, len);
690 pci_dev->config_write(pci_dev, config_addr, val, len);
693 uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
695 PCIBus *s = opaque;
696 PCIDevice *pci_dev;
697 int config_addr, bus_num;
698 uint32_t val;
700 bus_num = (addr >> 16) & 0xff;
701 while (s && s->bus_num != bus_num)
702 s= s->next;
703 if (!s)
704 goto fail;
705 pci_dev = s->devices[(addr >> 8) & 0xff];
706 if (!pci_dev) {
707 fail:
708 switch(len) {
709 case 1:
710 val = 0xff;
711 break;
712 case 2:
713 val = 0xffff;
714 break;
715 default:
716 case 4:
717 val = 0xffffffff;
718 break;
720 goto the_end;
722 config_addr = addr & 0xff;
723 val = pci_dev->config_read(pci_dev, config_addr, len);
724 PCI_DPRINTF("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
725 pci_dev->name, config_addr, val, len);
726 the_end:
727 #if 0
728 PCI_DPRINTF("pci_data_read: addr=%08x val=%08x len=%d\n",
729 addr, val, len);
730 #endif
731 return val;
734 /***********************************************************/
735 /* generic PCI irq support */
737 /* 0 <= irq_num <= 3. level must be 0 or 1 */
738 static void pci_set_irq(void *opaque, int irq_num, int level)
740 PCIDevice *pci_dev = (PCIDevice *)opaque;
741 PCIBus *bus;
742 int change;
744 change = level - pci_dev->irq_state[irq_num];
745 if (!change)
746 return;
748 pci_dev->irq_state[irq_num] = level;
750 #if defined(TARGET_IA64)
751 ioapic_set_irq(pci_dev, irq_num, level);
752 #endif
754 for (;;) {
755 bus = pci_dev->bus;
756 irq_num = bus->map_irq(pci_dev, irq_num);
757 if (bus->set_irq)
758 break;
759 pci_dev = bus->parent_dev;
761 bus->irq_count[irq_num] += change;
762 bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
765 int pci_map_irq(PCIDevice *pci_dev, int pin)
767 return pci_dev->bus->map_irq(pci_dev, pin);
770 /***********************************************************/
771 /* monitor info on PCI */
773 typedef struct {
774 uint16_t class;
775 const char *desc;
776 } pci_class_desc;
778 static const pci_class_desc pci_class_descriptions[] =
780 { 0x0100, "SCSI controller"},
781 { 0x0101, "IDE controller"},
782 { 0x0102, "Floppy controller"},
783 { 0x0103, "IPI controller"},
784 { 0x0104, "RAID controller"},
785 { 0x0106, "SATA controller"},
786 { 0x0107, "SAS controller"},
787 { 0x0180, "Storage controller"},
788 { 0x0200, "Ethernet controller"},
789 { 0x0201, "Token Ring controller"},
790 { 0x0202, "FDDI controller"},
791 { 0x0203, "ATM controller"},
792 { 0x0280, "Network controller"},
793 { 0x0300, "VGA controller"},
794 { 0x0301, "XGA controller"},
795 { 0x0302, "3D controller"},
796 { 0x0380, "Display controller"},
797 { 0x0400, "Video controller"},
798 { 0x0401, "Audio controller"},
799 { 0x0402, "Phone"},
800 { 0x0480, "Multimedia controller"},
801 { 0x0500, "RAM controller"},
802 { 0x0501, "Flash controller"},
803 { 0x0580, "Memory controller"},
804 { 0x0600, "Host bridge"},
805 { 0x0601, "ISA bridge"},
806 { 0x0602, "EISA bridge"},
807 { 0x0603, "MC bridge"},
808 { 0x0604, "PCI bridge"},
809 { 0x0605, "PCMCIA bridge"},
810 { 0x0606, "NUBUS bridge"},
811 { 0x0607, "CARDBUS bridge"},
812 { 0x0608, "RACEWAY bridge"},
813 { 0x0680, "Bridge"},
814 { 0x0c03, "USB controller"},
815 { 0, NULL}
818 static void pci_info_device(PCIDevice *d)
820 Monitor *mon = cur_mon;
821 int i, class;
822 PCIIORegion *r;
823 const pci_class_desc *desc;
825 monitor_printf(mon, " Bus %2d, device %3d, function %d:\n",
826 d->bus->bus_num, d->devfn >> 3, d->devfn & 7);
827 class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
828 monitor_printf(mon, " ");
829 desc = pci_class_descriptions;
830 while (desc->desc && class != desc->class)
831 desc++;
832 if (desc->desc) {
833 monitor_printf(mon, "%s", desc->desc);
834 } else {
835 monitor_printf(mon, "Class %04x", class);
837 monitor_printf(mon, ": PCI device %04x:%04x\n",
838 le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
839 le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
841 if (d->config[PCI_INTERRUPT_PIN] != 0) {
842 monitor_printf(mon, " IRQ %d.\n",
843 d->config[PCI_INTERRUPT_LINE]);
845 if (class == 0x0604) {
846 monitor_printf(mon, " BUS %d.\n", d->config[0x19]);
848 for(i = 0;i < PCI_NUM_REGIONS; i++) {
849 r = &d->io_regions[i];
850 if (r->size != 0) {
851 monitor_printf(mon, " BAR%d: ", i);
852 if (r->type & PCI_ADDRESS_SPACE_IO) {
853 monitor_printf(mon, "I/O at 0x%04x [0x%04x].\n",
854 r->addr, r->addr + r->size - 1);
855 } else {
856 monitor_printf(mon, "32 bit memory at 0x%08x [0x%08x].\n",
857 r->addr, r->addr + r->size - 1);
861 monitor_printf(mon, " id \"%s\"\n", d->qdev.id ? d->qdev.id : "");
862 if (class == 0x0604 && d->config[0x19] != 0) {
863 pci_for_each_device(d->config[0x19], pci_info_device);
867 void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d))
869 PCIBus *bus = first_bus;
870 PCIDevice *d;
871 int devfn;
873 while (bus && bus->bus_num != bus_num)
874 bus = bus->next;
875 if (bus) {
876 for(devfn = 0; devfn < 256; devfn++) {
877 d = bus->devices[devfn];
878 if (d)
879 fn(d);
884 void pci_info(Monitor *mon)
886 pci_for_each_device(0, pci_info_device);
889 PCIDevice *pci_create(const char *name, const char *devaddr)
891 PCIBus *bus;
892 int devfn;
893 DeviceState *dev;
895 bus = pci_get_bus_devfn(&devfn, devaddr);
896 if (!bus) {
897 fprintf(stderr, "Invalid PCI device address %s for device %s\n",
898 devaddr, name);
899 exit(1);
902 dev = qdev_create(&bus->qbus, name);
903 qdev_prop_set_uint32(dev, "devfn", devfn);
904 return (PCIDevice *)dev;
907 static const char * const pci_nic_models[] = {
908 "ne2k_pci",
909 "i82551",
910 "i82557b",
911 "i82559er",
912 "rtl8139",
913 "e1000",
914 "pcnet",
915 "virtio",
916 NULL
919 static const char * const pci_nic_names[] = {
920 "ne2k_pci",
921 "i82551",
922 "i82557b",
923 "i82559er",
924 "rtl8139",
925 "e1000",
926 "pcnet",
927 "virtio-net-pci",
928 NULL
931 /* Initialize a PCI NIC. */
932 PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
933 const char *default_devaddr)
935 const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
936 PCIDevice *pci_dev;
937 DeviceState *dev;
938 int i;
940 i = qemu_find_nic_model(nd, pci_nic_models, default_model);
941 if (i < 0)
942 return NULL;
944 pci_dev = pci_create(pci_nic_names[i], devaddr);
945 dev = &pci_dev->qdev;
946 if (nd->id)
947 dev->id = qemu_strdup(nd->id);
948 dev->nd = nd;
949 qdev_init(dev);
950 nd->private = dev;
951 return pci_dev;
954 PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
955 const char *default_devaddr)
957 PCIDevice *res;
959 if (qemu_show_nic_models(nd->model, pci_nic_models))
960 exit(0);
962 res = pci_nic_init(nd, default_model, default_devaddr);
963 if (!res)
964 exit(1);
965 return res;
968 typedef struct {
969 PCIDevice dev;
970 PCIBus *bus;
971 } PCIBridge;
973 static void pci_bridge_write_config(PCIDevice *d,
974 uint32_t address, uint32_t val, int len)
976 PCIBridge *s = (PCIBridge *)d;
978 pci_default_write_config(d, address, val, len);
979 s->bus->bus_num = d->config[PCI_SECONDARY_BUS];
982 PCIBus *pci_find_bus(int bus_num)
984 PCIBus *bus = first_bus;
986 while (bus && bus->bus_num != bus_num)
987 bus = bus->next;
989 return bus;
992 PCIDevice *pci_find_device(int bus_num, int slot, int function)
994 PCIBus *bus = pci_find_bus(bus_num);
996 if (!bus)
997 return NULL;
999 return bus->devices[PCI_DEVFN(slot, function)];
1002 PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
1003 pci_map_irq_fn map_irq, const char *name)
1005 PCIBridge *s;
1006 s = (PCIBridge *)pci_register_device(bus, name, sizeof(PCIBridge),
1007 devfn, NULL, pci_bridge_write_config);
1009 pci_config_set_vendor_id(s->dev.config, vid);
1010 pci_config_set_device_id(s->dev.config, did);
1012 s->dev.config[0x04] = 0x06; // command = bus master, pci mem
1013 s->dev.config[0x05] = 0x00;
1014 s->dev.config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
1015 s->dev.config[0x07] = 0x00; // status = fast devsel
1016 s->dev.config[0x08] = 0x00; // revision
1017 s->dev.config[0x09] = 0x00; // programming i/f
1018 pci_config_set_class(s->dev.config, PCI_CLASS_BRIDGE_PCI);
1019 s->dev.config[0x0D] = 0x10; // latency_timer
1020 s->dev.config[PCI_HEADER_TYPE] =
1021 PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE; // header_type
1022 s->dev.config[0x1E] = 0xa0; // secondary status
1024 s->bus = pci_register_secondary_bus(&s->dev, map_irq);
1025 return s->bus;
1028 static void pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1030 PCIDevice *pci_dev = (PCIDevice *)qdev;
1031 PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1032 PCIBus *bus;
1033 int devfn;
1035 bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1036 devfn = pci_dev->devfn;
1037 pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1038 info->config_read, info->config_write);
1039 assert(pci_dev);
1040 info->init(pci_dev);
1043 void pci_qdev_register(PCIDeviceInfo *info)
1045 info->qdev.init = pci_qdev_init;
1046 info->qdev.bus_info = &pci_bus_info;
1047 qdev_register(&info->qdev);
1050 void pci_qdev_register_many(PCIDeviceInfo *info)
1052 while (info->qdev.name) {
1053 pci_qdev_register(info);
1054 info++;
1058 PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1060 DeviceState *dev;
1062 dev = qdev_create(&bus->qbus, name);
1063 qdev_prop_set_uint32(dev, "devfn", devfn);
1064 qdev_init(dev);
1066 return (PCIDevice *)dev;
1069 int pci_enable_capability_support(PCIDevice *pci_dev,
1070 uint32_t config_start,
1071 PCICapConfigReadFunc *config_read,
1072 PCICapConfigWriteFunc *config_write,
1073 PCICapConfigInitFunc *config_init)
1075 if (!pci_dev)
1076 return -ENODEV;
1078 pci_dev->config[0x06] |= 0x10; // status = capabilities
1080 if (config_start == 0)
1081 pci_dev->cap.start = PCI_CAPABILITY_CONFIG_DEFAULT_START_ADDR;
1082 else if (config_start >= 0x40 && config_start < 0xff)
1083 pci_dev->cap.start = config_start;
1084 else
1085 return -EINVAL;
1087 if (config_read)
1088 pci_dev->cap.config_read = config_read;
1089 else
1090 pci_dev->cap.config_read = pci_default_cap_read_config;
1091 if (config_write)
1092 pci_dev->cap.config_write = config_write;
1093 else
1094 pci_dev->cap.config_write = pci_default_cap_write_config;
1095 pci_dev->cap.supported = 1;
1096 pci_dev->config[PCI_CAPABILITY_LIST] = pci_dev->cap.start;
1097 return config_init(pci_dev);
1100 static int pci_find_space(PCIDevice *pdev, uint8_t size)
1102 int offset = PCI_CONFIG_HEADER_SIZE;
1103 int i;
1104 for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i)
1105 if (pdev->used[i])
1106 offset = i + 1;
1107 else if (i - offset + 1 == size)
1108 return offset;
1109 return 0;
1112 static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1113 uint8_t *prev_p)
1115 uint8_t next, prev;
1117 if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1118 return 0;
1120 for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1121 prev = next + PCI_CAP_LIST_NEXT)
1122 if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1123 break;
1125 if (prev_p)
1126 *prev_p = prev;
1127 return next;
1130 /* Reserve space and add capability to the linked list in pci config space */
1131 int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1133 uint8_t offset = pci_find_space(pdev, size);
1134 uint8_t *config = pdev->config + offset;
1135 if (!offset)
1136 return -ENOSPC;
1137 config[PCI_CAP_LIST_ID] = cap_id;
1138 config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1139 pdev->config[PCI_CAPABILITY_LIST] = offset;
1140 pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1141 memset(pdev->used + offset, 0xFF, size);
1142 /* Make capability read-only by default */
1143 memset(pdev->wmask + offset, 0, size);
1144 /* Check capability by default */
1145 memset(pdev->cmask + offset, 0xFF, size);
1146 return offset;
1149 /* Unlink capability from the pci config space. */
1150 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1152 uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1153 if (!offset)
1154 return;
1155 pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1156 /* Make capability writeable again */
1157 memset(pdev->wmask + offset, 0xff, size);
1158 /* Clear cmask as device-specific registers can't be checked */
1159 memset(pdev->cmask + offset, 0, size);
1160 memset(pdev->used + offset, 0, size);
1162 if (!pdev->config[PCI_CAPABILITY_LIST])
1163 pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1166 /* Reserve space for capability at a known offset (to call after load). */
1167 void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1169 memset(pdev->used + offset, 0xff, size);
1172 uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1174 return pci_find_capability_list(pdev, cap_id, NULL);
1177 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1179 PCIDevice *d = (PCIDevice *)dev;
1180 const pci_class_desc *desc;
1181 char ctxt[64];
1182 PCIIORegion *r;
1183 int i, class;
1185 class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
1186 desc = pci_class_descriptions;
1187 while (desc->desc && class != desc->class)
1188 desc++;
1189 if (desc->desc) {
1190 snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1191 } else {
1192 snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1195 monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1196 "pci id %04x:%04x (sub %04x:%04x)\n",
1197 indent, "", ctxt,
1198 d->bus->bus_num, d->devfn >> 3, d->devfn & 7,
1199 le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
1200 le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))),
1201 le16_to_cpu(*((uint16_t *)(d->config + PCI_SUBSYSTEM_VENDOR_ID))),
1202 le16_to_cpu(*((uint16_t *)(d->config + PCI_SUBSYSTEM_ID))));
1203 for (i = 0; i < PCI_NUM_REGIONS; i++) {
1204 r = &d->io_regions[i];
1205 if (!r->size)
1206 continue;
1207 monitor_printf(mon, "%*sbar %d: %s at 0x%x [0x%x]\n", indent, "",
1208 i, r->type & PCI_ADDRESS_SPACE_IO ? "i/o" : "mem",
1209 r->addr, r->addr + r->size - 1);