]> Git Repo - qemu.git/blob - hw/pci.c
pci: make command SERR bit writable
[qemu.git] / hw / pci.c
1 /*
2  * QEMU PCI bus manager
3  *
4  * Copyright (c) 2004 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 #include "hw.h"
25 #include "pci.h"
26 #include "pci_bridge.h"
27 #include "pci_internals.h"
28 #include "msix.h"
29 #include "msi.h"
30 #include "monitor.h"
31 #include "net.h"
32 #include "sysemu.h"
33 #include "loader.h"
34 #include "qemu-objects.h"
35 #include "range.h"
36
37 //#define DEBUG_PCI
38 #ifdef DEBUG_PCI
39 # define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
40 #else
41 # define PCI_DPRINTF(format, ...)       do { } while (0)
42 #endif
43
44 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
45 static char *pcibus_get_dev_path(DeviceState *dev);
46 static int pcibus_reset(BusState *qbus);
47
48 struct BusInfo pci_bus_info = {
49     .name       = "PCI",
50     .size       = sizeof(PCIBus),
51     .print_dev  = pcibus_dev_print,
52     .get_dev_path = pcibus_get_dev_path,
53     .reset      = pcibus_reset,
54     .props      = (Property[]) {
55         DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
56         DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
57         DEFINE_PROP_UINT32("rombar",  PCIDevice, rom_bar, 1),
58         DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
59                         QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
60         DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present,
61                         QEMU_PCI_CAP_SERR_BITNR, true),
62         DEFINE_PROP_END_OF_LIST()
63     }
64 };
65
66 static void pci_update_mappings(PCIDevice *d);
67 static void pci_set_irq(void *opaque, int irq_num, int level);
68 static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom);
69 static void pci_del_option_rom(PCIDevice *pdev);
70
71 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
72 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
73
74 struct PCIHostBus {
75     int domain;
76     struct PCIBus *bus;
77     QLIST_ENTRY(PCIHostBus) next;
78 };
79 static QLIST_HEAD(, PCIHostBus) host_buses;
80
81 static const VMStateDescription vmstate_pcibus = {
82     .name = "PCIBUS",
83     .version_id = 1,
84     .minimum_version_id = 1,
85     .minimum_version_id_old = 1,
86     .fields      = (VMStateField []) {
87         VMSTATE_INT32_EQUAL(nirq, PCIBus),
88         VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
89         VMSTATE_END_OF_LIST()
90     }
91 };
92
93 static int pci_bar(PCIDevice *d, int reg)
94 {
95     uint8_t type;
96
97     if (reg != PCI_ROM_SLOT)
98         return PCI_BASE_ADDRESS_0 + reg * 4;
99
100     type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
101     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
102 }
103
104 static inline int pci_irq_state(PCIDevice *d, int irq_num)
105 {
106         return (d->irq_state >> irq_num) & 0x1;
107 }
108
109 static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
110 {
111         d->irq_state &= ~(0x1 << irq_num);
112         d->irq_state |= level << irq_num;
113 }
114
115 static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
116 {
117     PCIBus *bus;
118     for (;;) {
119         bus = pci_dev->bus;
120         irq_num = bus->map_irq(pci_dev, irq_num);
121         if (bus->set_irq)
122             break;
123         pci_dev = bus->parent_dev;
124     }
125     bus->irq_count[irq_num] += change;
126     bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
127 }
128
129 /* Update interrupt status bit in config space on interrupt
130  * state change. */
131 static void pci_update_irq_status(PCIDevice *dev)
132 {
133     if (dev->irq_state) {
134         dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
135     } else {
136         dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
137     }
138 }
139
140 static void pci_device_reset(PCIDevice *dev)
141 {
142     int r;
143     /* TODO: call the below unconditionally once all pci devices
144      * are qdevified */
145     if (dev->qdev.info) {
146         qdev_reset_all(&dev->qdev);
147     }
148
149     dev->irq_state = 0;
150     pci_update_irq_status(dev);
151     /* Clear all writeable bits */
152     pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
153                                  pci_get_word(dev->wmask + PCI_COMMAND) |
154                                  pci_get_word(dev->w1cmask + PCI_COMMAND));
155     pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
156                                  pci_get_word(dev->wmask + PCI_STATUS) |
157                                  pci_get_word(dev->w1cmask + PCI_STATUS));
158     dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
159     dev->config[PCI_INTERRUPT_LINE] = 0x0;
160     for (r = 0; r < PCI_NUM_REGIONS; ++r) {
161         PCIIORegion *region = &dev->io_regions[r];
162         if (!region->size) {
163             continue;
164         }
165
166         if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
167             region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
168             pci_set_quad(dev->config + pci_bar(dev, r), region->type);
169         } else {
170             pci_set_long(dev->config + pci_bar(dev, r), region->type);
171         }
172     }
173     pci_update_mappings(dev);
174 }
175
176 /*
177  * Trigger pci bus reset under a given bus.
178  * To be called on RST# assert.
179  */
180 void pci_bus_reset(PCIBus *bus)
181 {
182     int i;
183
184     for (i = 0; i < bus->nirq; i++) {
185         bus->irq_count[i] = 0;
186     }
187     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
188         if (bus->devices[i]) {
189             pci_device_reset(bus->devices[i]);
190         }
191     }
192 }
193
194 static int pcibus_reset(BusState *qbus)
195 {
196     pci_bus_reset(DO_UPCAST(PCIBus, qbus, qbus));
197
198     /* topology traverse is done by pci_bus_reset().
199        Tell qbus/qdev walker not to traverse the tree */
200     return 1;
201 }
202
203 static void pci_host_bus_register(int domain, PCIBus *bus)
204 {
205     struct PCIHostBus *host;
206     host = qemu_mallocz(sizeof(*host));
207     host->domain = domain;
208     host->bus = bus;
209     QLIST_INSERT_HEAD(&host_buses, host, next);
210 }
211
212 PCIBus *pci_find_root_bus(int domain)
213 {
214     struct PCIHostBus *host;
215
216     QLIST_FOREACH(host, &host_buses, next) {
217         if (host->domain == domain) {
218             return host->bus;
219         }
220     }
221
222     return NULL;
223 }
224
225 int pci_find_domain(const PCIBus *bus)
226 {
227     PCIDevice *d;
228     struct PCIHostBus *host;
229
230     /* obtain root bus */
231     while ((d = bus->parent_dev) != NULL) {
232         bus = d->bus;
233     }
234
235     QLIST_FOREACH(host, &host_buses, next) {
236         if (host->bus == bus) {
237             return host->domain;
238         }
239     }
240
241     abort();    /* should not be reached */
242     return -1;
243 }
244
245 void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
246                          const char *name, int devfn_min)
247 {
248     qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
249     assert(PCI_FUNC(devfn_min) == 0);
250     bus->devfn_min = devfn_min;
251
252     /* host bridge */
253     QLIST_INIT(&bus->child);
254     pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
255
256     vmstate_register(NULL, -1, &vmstate_pcibus, bus);
257 }
258
259 PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
260 {
261     PCIBus *bus;
262
263     bus = qemu_mallocz(sizeof(*bus));
264     bus->qbus.qdev_allocated = 1;
265     pci_bus_new_inplace(bus, parent, name, devfn_min);
266     return bus;
267 }
268
269 void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
270                   void *irq_opaque, int nirq)
271 {
272     bus->set_irq = set_irq;
273     bus->map_irq = map_irq;
274     bus->irq_opaque = irq_opaque;
275     bus->nirq = nirq;
276     bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
277 }
278
279 void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev)
280 {
281     bus->qbus.allow_hotplug = 1;
282     bus->hotplug = hotplug;
283     bus->hotplug_qdev = qdev;
284 }
285
286 void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
287 {
288     bus->mem_base = base;
289 }
290
291 PCIBus *pci_register_bus(DeviceState *parent, const char *name,
292                          pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
293                          void *irq_opaque, int devfn_min, int nirq)
294 {
295     PCIBus *bus;
296
297     bus = pci_bus_new(parent, name, devfn_min);
298     pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
299     return bus;
300 }
301
302 int pci_bus_num(PCIBus *s)
303 {
304     if (!s->parent_dev)
305         return 0;       /* pci host bridge */
306     return s->parent_dev->config[PCI_SECONDARY_BUS];
307 }
308
309 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
310 {
311     PCIDevice *s = container_of(pv, PCIDevice, config);
312     uint8_t *config;
313     int i;
314
315     assert(size == pci_config_size(s));
316     config = qemu_malloc(size);
317
318     qemu_get_buffer(f, config, size);
319     for (i = 0; i < size; ++i) {
320         if ((config[i] ^ s->config[i]) &
321             s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
322             qemu_free(config);
323             return -EINVAL;
324         }
325     }
326     memcpy(s->config, config, size);
327
328     pci_update_mappings(s);
329
330     qemu_free(config);
331     return 0;
332 }
333
334 /* just put buffer */
335 static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
336 {
337     const uint8_t **v = pv;
338     assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
339     qemu_put_buffer(f, *v, size);
340 }
341
342 static VMStateInfo vmstate_info_pci_config = {
343     .name = "pci config",
344     .get  = get_pci_config_device,
345     .put  = put_pci_config_device,
346 };
347
348 static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
349 {
350     PCIDevice *s = container_of(pv, PCIDevice, irq_state);
351     uint32_t irq_state[PCI_NUM_PINS];
352     int i;
353     for (i = 0; i < PCI_NUM_PINS; ++i) {
354         irq_state[i] = qemu_get_be32(f);
355         if (irq_state[i] != 0x1 && irq_state[i] != 0) {
356             fprintf(stderr, "irq state %d: must be 0 or 1.\n",
357                     irq_state[i]);
358             return -EINVAL;
359         }
360     }
361
362     for (i = 0; i < PCI_NUM_PINS; ++i) {
363         pci_set_irq_state(s, i, irq_state[i]);
364     }
365
366     return 0;
367 }
368
369 static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
370 {
371     int i;
372     PCIDevice *s = container_of(pv, PCIDevice, irq_state);
373
374     for (i = 0; i < PCI_NUM_PINS; ++i) {
375         qemu_put_be32(f, pci_irq_state(s, i));
376     }
377 }
378
379 static VMStateInfo vmstate_info_pci_irq_state = {
380     .name = "pci irq state",
381     .get  = get_pci_irq_state,
382     .put  = put_pci_irq_state,
383 };
384
385 const VMStateDescription vmstate_pci_device = {
386     .name = "PCIDevice",
387     .version_id = 2,
388     .minimum_version_id = 1,
389     .minimum_version_id_old = 1,
390     .fields      = (VMStateField []) {
391         VMSTATE_INT32_LE(version_id, PCIDevice),
392         VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
393                                    vmstate_info_pci_config,
394                                    PCI_CONFIG_SPACE_SIZE),
395         VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
396                                    vmstate_info_pci_irq_state,
397                                    PCI_NUM_PINS * sizeof(int32_t)),
398         VMSTATE_END_OF_LIST()
399     }
400 };
401
402 const VMStateDescription vmstate_pcie_device = {
403     .name = "PCIDevice",
404     .version_id = 2,
405     .minimum_version_id = 1,
406     .minimum_version_id_old = 1,
407     .fields      = (VMStateField []) {
408         VMSTATE_INT32_LE(version_id, PCIDevice),
409         VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
410                                    vmstate_info_pci_config,
411                                    PCIE_CONFIG_SPACE_SIZE),
412         VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
413                                    vmstate_info_pci_irq_state,
414                                    PCI_NUM_PINS * sizeof(int32_t)),
415         VMSTATE_END_OF_LIST()
416     }
417 };
418
419 static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
420 {
421     return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
422 }
423
424 void pci_device_save(PCIDevice *s, QEMUFile *f)
425 {
426     /* Clear interrupt status bit: it is implicit
427      * in irq_state which we are saving.
428      * This makes us compatible with old devices
429      * which never set or clear this bit. */
430     s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
431     vmstate_save_state(f, pci_get_vmstate(s), s);
432     /* Restore the interrupt status bit. */
433     pci_update_irq_status(s);
434 }
435
436 int pci_device_load(PCIDevice *s, QEMUFile *f)
437 {
438     int ret;
439     ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
440     /* Restore the interrupt status bit. */
441     pci_update_irq_status(s);
442     return ret;
443 }
444
445 static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
446 {
447     pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
448                  pci_default_sub_vendor_id);
449     pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
450                  pci_default_sub_device_id);
451 }
452
453 /*
454  * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
455  *       [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
456  */
457 int pci_parse_devaddr(const char *addr, int *domp, int *busp,
458                       unsigned int *slotp, unsigned int *funcp)
459 {
460     const char *p;
461     char *e;
462     unsigned long val;
463     unsigned long dom = 0, bus = 0;
464     unsigned int slot = 0;
465     unsigned int func = 0;
466
467     p = addr;
468     val = strtoul(p, &e, 16);
469     if (e == p)
470         return -1;
471     if (*e == ':') {
472         bus = val;
473         p = e + 1;
474         val = strtoul(p, &e, 16);
475         if (e == p)
476             return -1;
477         if (*e == ':') {
478             dom = bus;
479             bus = val;
480             p = e + 1;
481             val = strtoul(p, &e, 16);
482             if (e == p)
483                 return -1;
484         }
485     }
486
487     slot = val;
488
489     if (funcp != NULL) {
490         if (*e != '.')
491             return -1;
492
493         p = e + 1;
494         val = strtoul(p, &e, 16);
495         if (e == p)
496             return -1;
497
498         func = val;
499     }
500
501     /* if funcp == NULL func is 0 */
502     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
503         return -1;
504
505     if (*e)
506         return -1;
507
508     /* Note: QEMU doesn't implement domains other than 0 */
509     if (!pci_find_bus(pci_find_root_bus(dom), bus))
510         return -1;
511
512     *domp = dom;
513     *busp = bus;
514     *slotp = slot;
515     if (funcp != NULL)
516         *funcp = func;
517     return 0;
518 }
519
520 int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
521                      unsigned *slotp)
522 {
523     /* strip legacy tag */
524     if (!strncmp(addr, "pci_addr=", 9)) {
525         addr += 9;
526     }
527     if (pci_parse_devaddr(addr, domp, busp, slotp, NULL)) {
528         monitor_printf(mon, "Invalid pci address\n");
529         return -1;
530     }
531     return 0;
532 }
533
534 PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
535 {
536     int dom, bus;
537     unsigned slot;
538
539     if (!devaddr) {
540         *devfnp = -1;
541         return pci_find_bus(pci_find_root_bus(0), 0);
542     }
543
544     if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
545         return NULL;
546     }
547
548     *devfnp = slot << 3;
549     return pci_find_bus(pci_find_root_bus(dom), bus);
550 }
551
552 static void pci_init_cmask(PCIDevice *dev)
553 {
554     pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
555     pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
556     dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
557     dev->cmask[PCI_REVISION_ID] = 0xff;
558     dev->cmask[PCI_CLASS_PROG] = 0xff;
559     pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
560     dev->cmask[PCI_HEADER_TYPE] = 0xff;
561     dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
562 }
563
564 static void pci_init_wmask(PCIDevice *dev)
565 {
566     int config_size = pci_config_size(dev);
567
568     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
569     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
570     pci_set_word(dev->wmask + PCI_COMMAND,
571                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
572                  PCI_COMMAND_INTX_DISABLE);
573     if (dev->cap_present & QEMU_PCI_CAP_SERR) {
574         pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
575     }
576
577     memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
578            config_size - PCI_CONFIG_HEADER_SIZE);
579 }
580
581 static void pci_init_w1cmask(PCIDevice *dev)
582 {
583     /*
584      * Note: It's okay to set w1cmask even for readonly bits as
585      * long as their value is hardwired to 0.
586      */
587     pci_set_word(dev->w1cmask + PCI_STATUS,
588                  PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
589                  PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
590                  PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
591 }
592
593 static void pci_init_wmask_bridge(PCIDevice *d)
594 {
595     /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
596        PCI_SEC_LETENCY_TIMER */
597     memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
598
599     /* base and limit */
600     d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
601     d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
602     pci_set_word(d->wmask + PCI_MEMORY_BASE,
603                  PCI_MEMORY_RANGE_MASK & 0xffff);
604     pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
605                  PCI_MEMORY_RANGE_MASK & 0xffff);
606     pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
607                  PCI_PREF_RANGE_MASK & 0xffff);
608     pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
609                  PCI_PREF_RANGE_MASK & 0xffff);
610
611     /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
612     memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
613
614 /* TODO: add this define to pci_regs.h in linux and then in qemu. */
615 #define  PCI_BRIDGE_CTL_VGA_16BIT       0x10    /* VGA 16-bit decode */
616 #define  PCI_BRIDGE_CTL_DISCARD         0x100   /* Primary discard timer */
617 #define  PCI_BRIDGE_CTL_SEC_DISCARD     0x200   /* Secondary discard timer */
618 #define  PCI_BRIDGE_CTL_DISCARD_STATUS  0x400   /* Discard timer status */
619 #define  PCI_BRIDGE_CTL_DISCARD_SERR    0x800   /* Discard timer SERR# enable */
620     pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
621                  PCI_BRIDGE_CTL_PARITY |
622                  PCI_BRIDGE_CTL_SERR |
623                  PCI_BRIDGE_CTL_ISA |
624                  PCI_BRIDGE_CTL_VGA |
625                  PCI_BRIDGE_CTL_VGA_16BIT |
626                  PCI_BRIDGE_CTL_MASTER_ABORT |
627                  PCI_BRIDGE_CTL_BUS_RESET |
628                  PCI_BRIDGE_CTL_FAST_BACK |
629                  PCI_BRIDGE_CTL_DISCARD |
630                  PCI_BRIDGE_CTL_SEC_DISCARD |
631                  PCI_BRIDGE_CTL_DISCARD_STATUS |
632                  PCI_BRIDGE_CTL_DISCARD_SERR);
633     /* Below does not do anything as we never set this bit, put here for
634      * completeness. */
635     pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
636                  PCI_BRIDGE_CTL_DISCARD_STATUS);
637 }
638
639 static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev)
640 {
641     uint8_t slot = PCI_SLOT(dev->devfn);
642     uint8_t func;
643
644     if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
645         dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
646     }
647
648     /*
649      * multifunction bit is interpreted in two ways as follows.
650      *   - all functions must set the bit to 1.
651      *     Example: Intel X53
652      *   - function 0 must set the bit, but the rest function (> 0)
653      *     is allowed to leave the bit to 0.
654      *     Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
655      *
656      * So OS (at least Linux) checks the bit of only function 0,
657      * and doesn't see the bit of function > 0.
658      *
659      * The below check allows both interpretation.
660      */
661     if (PCI_FUNC(dev->devfn)) {
662         PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
663         if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
664             /* function 0 should set multifunction bit */
665             error_report("PCI: single function device can't be populated "
666                          "in function %x.%x", slot, PCI_FUNC(dev->devfn));
667             return -1;
668         }
669         return 0;
670     }
671
672     if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
673         return 0;
674     }
675     /* function 0 indicates single function, so function > 0 must be NULL */
676     for (func = 1; func < PCI_FUNC_MAX; ++func) {
677         if (bus->devices[PCI_DEVFN(slot, func)]) {
678             error_report("PCI: %x.0 indicates single function, "
679                          "but %x.%x is already populated.",
680                          slot, slot, func);
681             return -1;
682         }
683     }
684     return 0;
685 }
686
687 static void pci_config_alloc(PCIDevice *pci_dev)
688 {
689     int config_size = pci_config_size(pci_dev);
690
691     pci_dev->config = qemu_mallocz(config_size);
692     pci_dev->cmask = qemu_mallocz(config_size);
693     pci_dev->wmask = qemu_mallocz(config_size);
694     pci_dev->w1cmask = qemu_mallocz(config_size);
695     pci_dev->used = qemu_mallocz(config_size);
696 }
697
698 static void pci_config_free(PCIDevice *pci_dev)
699 {
700     qemu_free(pci_dev->config);
701     qemu_free(pci_dev->cmask);
702     qemu_free(pci_dev->wmask);
703     qemu_free(pci_dev->w1cmask);
704     qemu_free(pci_dev->used);
705 }
706
707 /* -1 for devfn means auto assign */
708 static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
709                                          const char *name, int devfn,
710                                          PCIConfigReadFunc *config_read,
711                                          PCIConfigWriteFunc *config_write,
712                                          bool is_bridge)
713 {
714     if (devfn < 0) {
715         for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
716             devfn += PCI_FUNC_MAX) {
717             if (!bus->devices[devfn])
718                 goto found;
719         }
720         error_report("PCI: no slot/function available for %s, all in use", name);
721         return NULL;
722     found: ;
723     } else if (bus->devices[devfn]) {
724         error_report("PCI: slot %d function %d not available for %s, in use by %s",
725                      PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name);
726         return NULL;
727     }
728     pci_dev->bus = bus;
729     pci_dev->devfn = devfn;
730     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
731     pci_dev->irq_state = 0;
732     pci_config_alloc(pci_dev);
733
734     if (!is_bridge) {
735         pci_set_default_subsystem_id(pci_dev);
736     }
737     pci_init_cmask(pci_dev);
738     pci_init_wmask(pci_dev);
739     pci_init_w1cmask(pci_dev);
740     if (is_bridge) {
741         pci_init_wmask_bridge(pci_dev);
742     }
743     if (pci_init_multifunction(bus, pci_dev)) {
744         pci_config_free(pci_dev);
745         return NULL;
746     }
747
748     if (!config_read)
749         config_read = pci_default_read_config;
750     if (!config_write)
751         config_write = pci_default_write_config;
752     pci_dev->config_read = config_read;
753     pci_dev->config_write = config_write;
754     bus->devices[devfn] = pci_dev;
755     pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
756     pci_dev->version_id = 2; /* Current pci device vmstate version */
757     return pci_dev;
758 }
759
760 static void do_pci_unregister_device(PCIDevice *pci_dev)
761 {
762     qemu_free_irqs(pci_dev->irq);
763     pci_dev->bus->devices[pci_dev->devfn] = NULL;
764     pci_config_free(pci_dev);
765 }
766
767 PCIDevice *pci_register_device(PCIBus *bus, const char *name,
768                                int instance_size, int devfn,
769                                PCIConfigReadFunc *config_read,
770                                PCIConfigWriteFunc *config_write)
771 {
772     PCIDevice *pci_dev;
773
774     pci_dev = qemu_mallocz(instance_size);
775     pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
776                                      config_read, config_write,
777                                      PCI_HEADER_TYPE_NORMAL);
778     if (pci_dev == NULL) {
779         hw_error("PCI: can't register device\n");
780     }
781     return pci_dev;
782 }
783
784 static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
785                                           target_phys_addr_t addr)
786 {
787     return addr + bus->mem_base;
788 }
789
790 static void pci_unregister_io_regions(PCIDevice *pci_dev)
791 {
792     PCIIORegion *r;
793     int i;
794
795     for(i = 0; i < PCI_NUM_REGIONS; i++) {
796         r = &pci_dev->io_regions[i];
797         if (!r->size || r->addr == PCI_BAR_UNMAPPED)
798             continue;
799         if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
800             isa_unassign_ioport(r->addr, r->filtered_size);
801         } else {
802             cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus,
803                                                          r->addr),
804                                          r->filtered_size,
805                                          IO_MEM_UNASSIGNED);
806         }
807     }
808 }
809
810 static int pci_unregister_device(DeviceState *dev)
811 {
812     PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
813     PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
814     int ret = 0;
815
816     if (info->exit)
817         ret = info->exit(pci_dev);
818     if (ret)
819         return ret;
820
821     pci_unregister_io_regions(pci_dev);
822     pci_del_option_rom(pci_dev);
823     do_pci_unregister_device(pci_dev);
824     return 0;
825 }
826
827 void pci_register_bar(PCIDevice *pci_dev, int region_num,
828                             pcibus_t size, uint8_t type,
829                             PCIMapIORegionFunc *map_func)
830 {
831     PCIIORegion *r;
832     uint32_t addr;
833     uint64_t wmask;
834
835     assert(region_num >= 0);
836     assert(region_num < PCI_NUM_REGIONS);
837     if (size & (size-1)) {
838         fprintf(stderr, "ERROR: PCI region size must be pow2 "
839                     "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
840         exit(1);
841     }
842
843     r = &pci_dev->io_regions[region_num];
844     r->addr = PCI_BAR_UNMAPPED;
845     r->size = size;
846     r->filtered_size = size;
847     r->type = type;
848     r->map_func = map_func;
849
850     wmask = ~(size - 1);
851     addr = pci_bar(pci_dev, region_num);
852     if (region_num == PCI_ROM_SLOT) {
853         /* ROM enable bit is writeable */
854         wmask |= PCI_ROM_ADDRESS_ENABLE;
855     }
856     pci_set_long(pci_dev->config + addr, type);
857     if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
858         r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
859         pci_set_quad(pci_dev->wmask + addr, wmask);
860         pci_set_quad(pci_dev->cmask + addr, ~0ULL);
861     } else {
862         pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
863         pci_set_long(pci_dev->cmask + addr, 0xffffffff);
864     }
865 }
866
867 static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size,
868                               uint8_t type)
869 {
870     pcibus_t base = *addr;
871     pcibus_t limit = *addr + *size - 1;
872     PCIDevice *br;
873
874     for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
875         uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
876
877         if (type & PCI_BASE_ADDRESS_SPACE_IO) {
878             if (!(cmd & PCI_COMMAND_IO)) {
879                 goto no_map;
880             }
881         } else {
882             if (!(cmd & PCI_COMMAND_MEMORY)) {
883                 goto no_map;
884             }
885         }
886
887         base = MAX(base, pci_bridge_get_base(br, type));
888         limit = MIN(limit, pci_bridge_get_limit(br, type));
889     }
890
891     if (base > limit) {
892         goto no_map;
893     }
894     *addr = base;
895     *size = limit - base + 1;
896     return;
897 no_map:
898     *addr = PCI_BAR_UNMAPPED;
899     *size = 0;
900 }
901
902 static pcibus_t pci_bar_address(PCIDevice *d,
903                                 int reg, uint8_t type, pcibus_t size)
904 {
905     pcibus_t new_addr, last_addr;
906     int bar = pci_bar(d, reg);
907     uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
908
909     if (type & PCI_BASE_ADDRESS_SPACE_IO) {
910         if (!(cmd & PCI_COMMAND_IO)) {
911             return PCI_BAR_UNMAPPED;
912         }
913         new_addr = pci_get_long(d->config + bar) & ~(size - 1);
914         last_addr = new_addr + size - 1;
915         /* NOTE: we have only 64K ioports on PC */
916         if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) {
917             return PCI_BAR_UNMAPPED;
918         }
919         return new_addr;
920     }
921
922     if (!(cmd & PCI_COMMAND_MEMORY)) {
923         return PCI_BAR_UNMAPPED;
924     }
925     if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
926         new_addr = pci_get_quad(d->config + bar);
927     } else {
928         new_addr = pci_get_long(d->config + bar);
929     }
930     /* the ROM slot has a specific enable bit */
931     if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
932         return PCI_BAR_UNMAPPED;
933     }
934     new_addr &= ~(size - 1);
935     last_addr = new_addr + size - 1;
936     /* NOTE: we do not support wrapping */
937     /* XXX: as we cannot support really dynamic
938        mappings, we handle specific values as invalid
939        mappings. */
940     if (last_addr <= new_addr || new_addr == 0 ||
941         last_addr == PCI_BAR_UNMAPPED) {
942         return PCI_BAR_UNMAPPED;
943     }
944
945     /* Now pcibus_t is 64bit.
946      * Check if 32 bit BAR wraps around explicitly.
947      * Without this, PC ide doesn't work well.
948      * TODO: remove this work around.
949      */
950     if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
951         return PCI_BAR_UNMAPPED;
952     }
953
954     /*
955      * OS is allowed to set BAR beyond its addressable
956      * bits. For example, 32 bit OS can set 64bit bar
957      * to >4G. Check it. TODO: we might need to support
958      * it in the future for e.g. PAE.
959      */
960     if (last_addr >= TARGET_PHYS_ADDR_MAX) {
961         return PCI_BAR_UNMAPPED;
962     }
963
964     return new_addr;
965 }
966
967 static void pci_update_mappings(PCIDevice *d)
968 {
969     PCIIORegion *r;
970     int i;
971     pcibus_t new_addr, filtered_size;
972
973     for(i = 0; i < PCI_NUM_REGIONS; i++) {
974         r = &d->io_regions[i];
975
976         /* this region isn't registered */
977         if (!r->size)
978             continue;
979
980         new_addr = pci_bar_address(d, i, r->type, r->size);
981
982         /* bridge filtering */
983         filtered_size = r->size;
984         if (new_addr != PCI_BAR_UNMAPPED) {
985             pci_bridge_filter(d, &new_addr, &filtered_size, r->type);
986         }
987
988         /* This bar isn't changed */
989         if (new_addr == r->addr && filtered_size == r->filtered_size)
990             continue;
991
992         /* now do the real mapping */
993         if (r->addr != PCI_BAR_UNMAPPED) {
994             if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
995                 int class;
996                 /* NOTE: specific hack for IDE in PC case:
997                    only one byte must be mapped. */
998                 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
999                 if (class == 0x0101 && r->size == 4) {
1000                     isa_unassign_ioport(r->addr + 2, 1);
1001                 } else {
1002                     isa_unassign_ioport(r->addr, r->filtered_size);
1003                 }
1004             } else {
1005                 cpu_register_physical_memory(pci_to_cpu_addr(d->bus, r->addr),
1006                                              r->filtered_size,
1007                                              IO_MEM_UNASSIGNED);
1008                 qemu_unregister_coalesced_mmio(r->addr, r->filtered_size);
1009             }
1010         }
1011         r->addr = new_addr;
1012         r->filtered_size = filtered_size;
1013         if (r->addr != PCI_BAR_UNMAPPED) {
1014             /*
1015              * TODO: currently almost all the map funcions assumes
1016              * filtered_size == size and addr & ~(size - 1) == addr.
1017              * However with bridge filtering, they aren't always true.
1018              * Teach them such cases, such that filtered_size < size and
1019              * addr & (size - 1) != 0.
1020              */
1021             if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1022                 r->map_func(d, i, r->addr, r->filtered_size, r->type);
1023             } else {
1024                 r->map_func(d, i, pci_to_cpu_addr(d->bus, r->addr),
1025                             r->filtered_size, r->type);
1026             }
1027         }
1028     }
1029 }
1030
1031 static inline int pci_irq_disabled(PCIDevice *d)
1032 {
1033     return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
1034 }
1035
1036 /* Called after interrupt disabled field update in config space,
1037  * assert/deassert interrupts if necessary.
1038  * Gets original interrupt disable bit value (before update). */
1039 static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
1040 {
1041     int i, disabled = pci_irq_disabled(d);
1042     if (disabled == was_irq_disabled)
1043         return;
1044     for (i = 0; i < PCI_NUM_PINS; ++i) {
1045         int state = pci_irq_state(d, i);
1046         pci_change_irq_level(d, i, disabled ? -state : state);
1047     }
1048 }
1049
1050 uint32_t pci_default_read_config(PCIDevice *d,
1051                                  uint32_t address, int len)
1052 {
1053     uint32_t val = 0;
1054     assert(len == 1 || len == 2 || len == 4);
1055     len = MIN(len, pci_config_size(d) - address);
1056     memcpy(&val, d->config + address, len);
1057     return le32_to_cpu(val);
1058 }
1059
1060 void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
1061 {
1062     int i, was_irq_disabled = pci_irq_disabled(d);
1063     uint32_t config_size = pci_config_size(d);
1064
1065     for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) {
1066         uint8_t wmask = d->wmask[addr + i];
1067         uint8_t w1cmask = d->w1cmask[addr + i];
1068         assert(!(wmask & w1cmask));
1069         d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
1070         d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
1071     }
1072     if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1073         ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1074         ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1075         range_covers_byte(addr, l, PCI_COMMAND))
1076         pci_update_mappings(d);
1077
1078     if (range_covers_byte(addr, l, PCI_COMMAND))
1079         pci_update_irq_disabled(d, was_irq_disabled);
1080 }
1081
1082 /***********************************************************/
1083 /* generic PCI irq support */
1084
1085 /* 0 <= irq_num <= 3. level must be 0 or 1 */
1086 static void pci_set_irq(void *opaque, int irq_num, int level)
1087 {
1088     PCIDevice *pci_dev = opaque;
1089     int change;
1090
1091     change = level - pci_irq_state(pci_dev, irq_num);
1092     if (!change)
1093         return;
1094
1095     pci_set_irq_state(pci_dev, irq_num, level);
1096     pci_update_irq_status(pci_dev);
1097     if (pci_irq_disabled(pci_dev))
1098         return;
1099     pci_change_irq_level(pci_dev, irq_num, change);
1100 }
1101
1102 bool pci_msi_enabled(PCIDevice *dev)
1103 {
1104     return msix_enabled(dev) || msi_enabled(dev);
1105 }
1106
1107 void pci_msi_notify(PCIDevice *dev, unsigned int vector)
1108 {
1109     if (msix_enabled(dev)) {
1110         msix_notify(dev, vector);
1111     } else if (msi_enabled(dev)) {
1112         msi_notify(dev, vector);
1113     } else {
1114         /* MSI/MSI-X must be enabled */
1115         abort();
1116     }
1117 }
1118
1119 /***********************************************************/
1120 /* monitor info on PCI */
1121
1122 typedef struct {
1123     uint16_t class;
1124     const char *desc;
1125 } pci_class_desc;
1126
1127 static const pci_class_desc pci_class_descriptions[] =
1128 {
1129     { 0x0100, "SCSI controller"},
1130     { 0x0101, "IDE controller"},
1131     { 0x0102, "Floppy controller"},
1132     { 0x0103, "IPI controller"},
1133     { 0x0104, "RAID controller"},
1134     { 0x0106, "SATA controller"},
1135     { 0x0107, "SAS controller"},
1136     { 0x0180, "Storage controller"},
1137     { 0x0200, "Ethernet controller"},
1138     { 0x0201, "Token Ring controller"},
1139     { 0x0202, "FDDI controller"},
1140     { 0x0203, "ATM controller"},
1141     { 0x0280, "Network controller"},
1142     { 0x0300, "VGA controller"},
1143     { 0x0301, "XGA controller"},
1144     { 0x0302, "3D controller"},
1145     { 0x0380, "Display controller"},
1146     { 0x0400, "Video controller"},
1147     { 0x0401, "Audio controller"},
1148     { 0x0402, "Phone"},
1149     { 0x0480, "Multimedia controller"},
1150     { 0x0500, "RAM controller"},
1151     { 0x0501, "Flash controller"},
1152     { 0x0580, "Memory controller"},
1153     { 0x0600, "Host bridge"},
1154     { 0x0601, "ISA bridge"},
1155     { 0x0602, "EISA bridge"},
1156     { 0x0603, "MC bridge"},
1157     { 0x0604, "PCI bridge"},
1158     { 0x0605, "PCMCIA bridge"},
1159     { 0x0606, "NUBUS bridge"},
1160     { 0x0607, "CARDBUS bridge"},
1161     { 0x0608, "RACEWAY bridge"},
1162     { 0x0680, "Bridge"},
1163     { 0x0c03, "USB controller"},
1164     { 0, NULL}
1165 };
1166
1167 static void pci_for_each_device_under_bus(PCIBus *bus,
1168                                           void (*fn)(PCIBus *b, PCIDevice *d))
1169 {
1170     PCIDevice *d;
1171     int devfn;
1172
1173     for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1174         d = bus->devices[devfn];
1175         if (d) {
1176             fn(bus, d);
1177         }
1178     }
1179 }
1180
1181 void pci_for_each_device(PCIBus *bus, int bus_num,
1182                          void (*fn)(PCIBus *b, PCIDevice *d))
1183 {
1184     bus = pci_find_bus(bus, bus_num);
1185
1186     if (bus) {
1187         pci_for_each_device_under_bus(bus, fn);
1188     }
1189 }
1190
1191 static void pci_device_print(Monitor *mon, QDict *device)
1192 {
1193     QDict *qdict;
1194     QListEntry *entry;
1195     uint64_t addr, size;
1196
1197     monitor_printf(mon, "  Bus %2" PRId64 ", ", qdict_get_int(device, "bus"));
1198     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
1199                         qdict_get_int(device, "slot"),
1200                         qdict_get_int(device, "function"));
1201     monitor_printf(mon, "    ");
1202
1203     qdict = qdict_get_qdict(device, "class_info");
1204     if (qdict_haskey(qdict, "desc")) {
1205         monitor_printf(mon, "%s", qdict_get_str(qdict, "desc"));
1206     } else {
1207         monitor_printf(mon, "Class %04" PRId64, qdict_get_int(qdict, "class"));
1208     }
1209
1210     qdict = qdict_get_qdict(device, "id");
1211     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
1212                         qdict_get_int(qdict, "device"),
1213                         qdict_get_int(qdict, "vendor"));
1214
1215     if (qdict_haskey(device, "irq")) {
1216         monitor_printf(mon, "      IRQ %" PRId64 ".\n",
1217                             qdict_get_int(device, "irq"));
1218     }
1219
1220     if (qdict_haskey(device, "pci_bridge")) {
1221         QDict *info;
1222
1223         qdict = qdict_get_qdict(device, "pci_bridge");
1224
1225         info = qdict_get_qdict(qdict, "bus");
1226         monitor_printf(mon, "      BUS %" PRId64 ".\n",
1227                             qdict_get_int(info, "number"));
1228         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
1229                             qdict_get_int(info, "secondary"));
1230         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
1231                             qdict_get_int(info, "subordinate"));
1232
1233         info = qdict_get_qdict(qdict, "io_range");
1234         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
1235                        qdict_get_int(info, "base"),
1236                        qdict_get_int(info, "limit"));
1237
1238         info = qdict_get_qdict(qdict, "memory_range");
1239         monitor_printf(mon,
1240                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
1241                        qdict_get_int(info, "base"),
1242                        qdict_get_int(info, "limit"));
1243
1244         info = qdict_get_qdict(qdict, "prefetchable_range");
1245         monitor_printf(mon, "      prefetchable memory range "
1246                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
1247                        qdict_get_int(info, "base"),
1248         qdict_get_int(info, "limit"));
1249     }
1250
1251     QLIST_FOREACH_ENTRY(qdict_get_qlist(device, "regions"), entry) {
1252         qdict = qobject_to_qdict(qlist_entry_obj(entry));
1253         monitor_printf(mon, "      BAR%d: ", (int) qdict_get_int(qdict, "bar"));
1254
1255         addr = qdict_get_int(qdict, "address");
1256         size = qdict_get_int(qdict, "size");
1257
1258         if (!strcmp(qdict_get_str(qdict, "type"), "io")) {
1259             monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
1260                                 " [0x%04"FMT_PCIBUS"].\n",
1261                                 addr, addr + size - 1);
1262         } else {
1263             monitor_printf(mon, "%d bit%s memory at 0x%08"FMT_PCIBUS
1264                                " [0x%08"FMT_PCIBUS"].\n",
1265                                 qdict_get_bool(qdict, "mem_type_64") ? 64 : 32,
1266                                 qdict_get_bool(qdict, "prefetch") ?
1267                                 " prefetchable" : "", addr, addr + size - 1);
1268         }
1269     }
1270
1271     monitor_printf(mon, "      id \"%s\"\n", qdict_get_str(device, "qdev_id"));
1272
1273     if (qdict_haskey(device, "pci_bridge")) {
1274         qdict = qdict_get_qdict(device, "pci_bridge");
1275         if (qdict_haskey(qdict, "devices")) {
1276             QListEntry *dev;
1277             QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
1278                 pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev)));
1279             }
1280         }
1281     }
1282 }
1283
1284 void do_pci_info_print(Monitor *mon, const QObject *data)
1285 {
1286     QListEntry *bus, *dev;
1287
1288     QLIST_FOREACH_ENTRY(qobject_to_qlist(data), bus) {
1289         QDict *qdict = qobject_to_qdict(qlist_entry_obj(bus));
1290         QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
1291             pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev)));
1292         }
1293     }
1294 }
1295
1296 static QObject *pci_get_dev_class(const PCIDevice *dev)
1297 {
1298     int class;
1299     const pci_class_desc *desc;
1300
1301     class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
1302     desc = pci_class_descriptions;
1303     while (desc->desc && class != desc->class)
1304         desc++;
1305
1306     if (desc->desc) {
1307         return qobject_from_jsonf("{ 'desc': %s, 'class': %d }",
1308                                   desc->desc, class);
1309     } else {
1310         return qobject_from_jsonf("{ 'class': %d }", class);
1311     }
1312 }
1313
1314 static QObject *pci_get_dev_id(const PCIDevice *dev)
1315 {
1316     return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }",
1317                               pci_get_word(dev->config + PCI_VENDOR_ID),
1318                               pci_get_word(dev->config + PCI_DEVICE_ID));
1319 }
1320
1321 static QObject *pci_get_regions_list(const PCIDevice *dev)
1322 {
1323     int i;
1324     QList *regions_list;
1325
1326     regions_list = qlist_new();
1327
1328     for (i = 0; i < PCI_NUM_REGIONS; i++) {
1329         QObject *obj;
1330         const PCIIORegion *r = &dev->io_regions[i];
1331
1332         if (!r->size) {
1333             continue;
1334         }
1335
1336         if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1337             obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
1338                                      "'address': %" PRId64 ", "
1339                                      "'size': %" PRId64 " }",
1340                                      i, r->addr, r->size);
1341         } else {
1342             int mem_type_64 = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64;
1343
1344             obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
1345                                      "'mem_type_64': %i, 'prefetch': %i, "
1346                                      "'address': %" PRId64 ", "
1347                                      "'size': %" PRId64 " }",
1348                                      i, mem_type_64,
1349                                      r->type & PCI_BASE_ADDRESS_MEM_PREFETCH,
1350                                      r->addr, r->size);
1351         }
1352
1353         qlist_append_obj(regions_list, obj);
1354     }
1355
1356     return QOBJECT(regions_list);
1357 }
1358
1359 static QObject *pci_get_devices_list(PCIBus *bus, int bus_num);
1360
1361 static QObject *pci_get_dev_dict(PCIDevice *dev, PCIBus *bus, int bus_num)
1362 {
1363     uint8_t type;
1364     QObject *obj;
1365
1366     obj = qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d,"                                       "'class_info': %p, 'id': %p, 'regions': %p,"
1367                               " 'qdev_id': %s }",
1368                               bus_num,
1369                               PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
1370                               pci_get_dev_class(dev), pci_get_dev_id(dev),
1371                               pci_get_regions_list(dev),
1372                               dev->qdev.id ? dev->qdev.id : "");
1373
1374     if (dev->config[PCI_INTERRUPT_PIN] != 0) {
1375         QDict *qdict = qobject_to_qdict(obj);
1376         qdict_put(qdict, "irq", qint_from_int(dev->config[PCI_INTERRUPT_LINE]));
1377     }
1378
1379     type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1380     if (type == PCI_HEADER_TYPE_BRIDGE) {
1381         QDict *qdict;
1382         QObject *pci_bridge;
1383
1384         pci_bridge = qobject_from_jsonf("{ 'bus': "
1385         "{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
1386         "'io_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, "
1387         "'memory_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, "
1388         "'prefetchable_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "} }",
1389         dev->config[PCI_PRIMARY_BUS], dev->config[PCI_SECONDARY_BUS],
1390         dev->config[PCI_SUBORDINATE_BUS],
1391         pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO),
1392         pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO),
1393         pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY),
1394         pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY),
1395         pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY |
1396                                PCI_BASE_ADDRESS_MEM_PREFETCH),
1397         pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY |
1398                                 PCI_BASE_ADDRESS_MEM_PREFETCH));
1399
1400         if (dev->config[PCI_SECONDARY_BUS] != 0) {
1401             PCIBus *child_bus = pci_find_bus(bus, dev->config[PCI_SECONDARY_BUS]);
1402
1403             if (child_bus) {
1404                 qdict = qobject_to_qdict(pci_bridge);
1405                 qdict_put_obj(qdict, "devices",
1406                               pci_get_devices_list(child_bus,
1407                                                    dev->config[PCI_SECONDARY_BUS]));
1408             }
1409         }
1410         qdict = qobject_to_qdict(obj);
1411         qdict_put_obj(qdict, "pci_bridge", pci_bridge);
1412     }
1413
1414     return obj;
1415 }
1416
1417 static QObject *pci_get_devices_list(PCIBus *bus, int bus_num)
1418 {
1419     int devfn;
1420     PCIDevice *dev;
1421     QList *dev_list;
1422
1423     dev_list = qlist_new();
1424
1425     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1426         dev = bus->devices[devfn];
1427         if (dev) {
1428             qlist_append_obj(dev_list, pci_get_dev_dict(dev, bus, bus_num));
1429         }
1430     }
1431
1432     return QOBJECT(dev_list);
1433 }
1434
1435 static QObject *pci_get_bus_dict(PCIBus *bus, int bus_num)
1436 {
1437     bus = pci_find_bus(bus, bus_num);
1438     if (bus) {
1439         return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }",
1440                                   bus_num, pci_get_devices_list(bus, bus_num));
1441     }
1442
1443     return NULL;
1444 }
1445
1446 void do_pci_info(Monitor *mon, QObject **ret_data)
1447 {
1448     QList *bus_list;
1449     struct PCIHostBus *host;
1450
1451     bus_list = qlist_new();
1452
1453     QLIST_FOREACH(host, &host_buses, next) {
1454         QObject *obj = pci_get_bus_dict(host->bus, 0);
1455         if (obj) {
1456             qlist_append_obj(bus_list, obj);
1457         }
1458     }
1459
1460     *ret_data = QOBJECT(bus_list);
1461 }
1462
1463 static const char * const pci_nic_models[] = {
1464     "ne2k_pci",
1465     "i82551",
1466     "i82557b",
1467     "i82559er",
1468     "rtl8139",
1469     "e1000",
1470     "pcnet",
1471     "virtio",
1472     NULL
1473 };
1474
1475 static const char * const pci_nic_names[] = {
1476     "ne2k_pci",
1477     "i82551",
1478     "i82557b",
1479     "i82559er",
1480     "rtl8139",
1481     "e1000",
1482     "pcnet",
1483     "virtio-net-pci",
1484     NULL
1485 };
1486
1487 /* Initialize a PCI NIC.  */
1488 /* FIXME callers should check for failure, but don't */
1489 PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
1490                         const char *default_devaddr)
1491 {
1492     const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1493     PCIBus *bus;
1494     int devfn;
1495     PCIDevice *pci_dev;
1496     DeviceState *dev;
1497     int i;
1498
1499     i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1500     if (i < 0)
1501         return NULL;
1502
1503     bus = pci_get_bus_devfn(&devfn, devaddr);
1504     if (!bus) {
1505         error_report("Invalid PCI device address %s for device %s",
1506                      devaddr, pci_nic_names[i]);
1507         return NULL;
1508     }
1509
1510     pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1511     dev = &pci_dev->qdev;
1512     qdev_set_nic_properties(dev, nd);
1513     if (qdev_init(dev) < 0)
1514         return NULL;
1515     return pci_dev;
1516 }
1517
1518 PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
1519                                const char *default_devaddr)
1520 {
1521     PCIDevice *res;
1522
1523     if (qemu_show_nic_models(nd->model, pci_nic_models))
1524         exit(0);
1525
1526     res = pci_nic_init(nd, default_model, default_devaddr);
1527     if (!res)
1528         exit(1);
1529     return res;
1530 }
1531
1532 static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d)
1533 {
1534     pci_update_mappings(d);
1535 }
1536
1537 void pci_bridge_update_mappings(PCIBus *b)
1538 {
1539     PCIBus *child;
1540
1541     pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn);
1542
1543     QLIST_FOREACH(child, &b->child, sibling) {
1544         pci_bridge_update_mappings(child);
1545     }
1546 }
1547
1548 /* Whether a given bus number is in range of the secondary
1549  * bus of the given bridge device. */
1550 static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
1551 {
1552     return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
1553              PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
1554         dev->config[PCI_SECONDARY_BUS] < bus_num &&
1555         bus_num <= dev->config[PCI_SUBORDINATE_BUS];
1556 }
1557
1558 PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
1559 {
1560     PCIBus *sec;
1561
1562     if (!bus) {
1563         return NULL;
1564     }
1565
1566     if (pci_bus_num(bus) == bus_num) {
1567         return bus;
1568     }
1569
1570     /* Consider all bus numbers in range for the host pci bridge. */
1571     if (bus->parent_dev &&
1572         !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
1573         return NULL;
1574     }
1575
1576     /* try child bus */
1577     for (; bus; bus = sec) {
1578         QLIST_FOREACH(sec, &bus->child, sibling) {
1579             assert(sec->parent_dev);
1580             if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
1581                 return sec;
1582             }
1583             if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
1584                 break;
1585             }
1586         }
1587     }
1588
1589     return NULL;
1590 }
1591
1592 PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
1593 {
1594     bus = pci_find_bus(bus, bus_num);
1595
1596     if (!bus)
1597         return NULL;
1598
1599     return bus->devices[PCI_DEVFN(slot, function)];
1600 }
1601
1602 static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1603 {
1604     PCIDevice *pci_dev = (PCIDevice *)qdev;
1605     PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1606     PCIBus *bus;
1607     int devfn, rc;
1608     bool is_default_rom;
1609
1610     /* initialize cap_present for pci_is_express() and pci_config_size() */
1611     if (info->is_express) {
1612         pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1613     }
1614
1615     bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1616     devfn = pci_dev->devfn;
1617     pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1618                                      info->config_read, info->config_write,
1619                                      info->is_bridge);
1620     if (pci_dev == NULL)
1621         return -1;
1622     rc = info->init(pci_dev);
1623     if (rc != 0) {
1624         do_pci_unregister_device(pci_dev);
1625         return rc;
1626     }
1627
1628     /* rom loading */
1629     is_default_rom = false;
1630     if (pci_dev->romfile == NULL && info->romfile != NULL) {
1631         pci_dev->romfile = qemu_strdup(info->romfile);
1632         is_default_rom = true;
1633     }
1634     pci_add_option_rom(pci_dev, is_default_rom);
1635
1636     if (bus->hotplug) {
1637         /* Let buses differentiate between hotplug and when device is
1638          * enabled during qemu machine creation. */
1639         rc = bus->hotplug(bus->hotplug_qdev, pci_dev,
1640                           qdev->hotplugged ? PCI_HOTPLUG_ENABLED:
1641                           PCI_COLDPLUG_ENABLED);
1642         if (rc != 0) {
1643             int r = pci_unregister_device(&pci_dev->qdev);
1644             assert(!r);
1645             return rc;
1646         }
1647     }
1648     return 0;
1649 }
1650
1651 static int pci_unplug_device(DeviceState *qdev)
1652 {
1653     PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1654
1655     return dev->bus->hotplug(dev->bus->hotplug_qdev, dev,
1656                              PCI_HOTPLUG_DISABLED);
1657 }
1658
1659 void pci_qdev_register(PCIDeviceInfo *info)
1660 {
1661     info->qdev.init = pci_qdev_init;
1662     info->qdev.unplug = pci_unplug_device;
1663     info->qdev.exit = pci_unregister_device;
1664     info->qdev.bus_info = &pci_bus_info;
1665     qdev_register(&info->qdev);
1666 }
1667
1668 void pci_qdev_register_many(PCIDeviceInfo *info)
1669 {
1670     while (info->qdev.name) {
1671         pci_qdev_register(info);
1672         info++;
1673     }
1674 }
1675
1676 PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
1677                                     const char *name)
1678 {
1679     DeviceState *dev;
1680
1681     dev = qdev_create(&bus->qbus, name);
1682     qdev_prop_set_uint32(dev, "addr", devfn);
1683     qdev_prop_set_bit(dev, "multifunction", multifunction);
1684     return DO_UPCAST(PCIDevice, qdev, dev);
1685 }
1686
1687 PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
1688                                            bool multifunction,
1689                                            const char *name)
1690 {
1691     PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name);
1692     qdev_init_nofail(&dev->qdev);
1693     return dev;
1694 }
1695
1696 PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1697 {
1698     return pci_create_multifunction(bus, devfn, false, name);
1699 }
1700
1701 PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1702 {
1703     return pci_create_simple_multifunction(bus, devfn, false, name);
1704 }
1705
1706 static int pci_find_space(PCIDevice *pdev, uint8_t size)
1707 {
1708     int config_size = pci_config_size(pdev);
1709     int offset = PCI_CONFIG_HEADER_SIZE;
1710     int i;
1711     for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
1712         if (pdev->used[i])
1713             offset = i + 1;
1714         else if (i - offset + 1 == size)
1715             return offset;
1716     return 0;
1717 }
1718
1719 static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1720                                         uint8_t *prev_p)
1721 {
1722     uint8_t next, prev;
1723
1724     if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1725         return 0;
1726
1727     for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1728          prev = next + PCI_CAP_LIST_NEXT)
1729         if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1730             break;
1731
1732     if (prev_p)
1733         *prev_p = prev;
1734     return next;
1735 }
1736
1737 static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type)
1738 {
1739     cpu_register_physical_memory(addr, size, pdev->rom_offset);
1740 }
1741
1742 /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
1743    This is needed for an option rom which is used for more than one device. */
1744 static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)
1745 {
1746     uint16_t vendor_id;
1747     uint16_t device_id;
1748     uint16_t rom_vendor_id;
1749     uint16_t rom_device_id;
1750     uint16_t rom_magic;
1751     uint16_t pcir_offset;
1752     uint8_t checksum;
1753
1754     /* Words in rom data are little endian (like in PCI configuration),
1755        so they can be read / written with pci_get_word / pci_set_word. */
1756
1757     /* Only a valid rom will be patched. */
1758     rom_magic = pci_get_word(ptr);
1759     if (rom_magic != 0xaa55) {
1760         PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
1761         return;
1762     }
1763     pcir_offset = pci_get_word(ptr + 0x18);
1764     if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
1765         PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
1766         return;
1767     }
1768
1769     vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
1770     device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
1771     rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
1772     rom_device_id = pci_get_word(ptr + pcir_offset + 6);
1773
1774     PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
1775                 vendor_id, device_id, rom_vendor_id, rom_device_id);
1776
1777     checksum = ptr[6];
1778
1779     if (vendor_id != rom_vendor_id) {
1780         /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
1781         checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
1782         checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
1783         PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
1784         ptr[6] = checksum;
1785         pci_set_word(ptr + pcir_offset + 4, vendor_id);
1786     }
1787
1788     if (device_id != rom_device_id) {
1789         /* Patch device id and checksum (at offset 6 for etherboot roms). */
1790         checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
1791         checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
1792         PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
1793         ptr[6] = checksum;
1794         pci_set_word(ptr + pcir_offset + 6, device_id);
1795     }
1796 }
1797
1798 /* Add an option rom for the device */
1799 static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom)
1800 {
1801     int size;
1802     char *path;
1803     void *ptr;
1804     char name[32];
1805
1806     if (!pdev->romfile)
1807         return 0;
1808     if (strlen(pdev->romfile) == 0)
1809         return 0;
1810
1811     if (!pdev->rom_bar) {
1812         /*
1813          * Load rom via fw_cfg instead of creating a rom bar,
1814          * for 0.11 compatibility.
1815          */
1816         int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
1817         if (class == 0x0300) {
1818             rom_add_vga(pdev->romfile);
1819         } else {
1820             rom_add_option(pdev->romfile);
1821         }
1822         return 0;
1823     }
1824
1825     path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
1826     if (path == NULL) {
1827         path = qemu_strdup(pdev->romfile);
1828     }
1829
1830     size = get_image_size(path);
1831     if (size < 0) {
1832         error_report("%s: failed to find romfile \"%s\"",
1833                      __FUNCTION__, pdev->romfile);
1834         return -1;
1835     }
1836     if (size & (size - 1)) {
1837         size = 1 << qemu_fls(size);
1838     }
1839
1840     if (pdev->qdev.info->vmsd)
1841         snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->vmsd->name);
1842     else
1843         snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->name);
1844     pdev->rom_offset = qemu_ram_alloc(&pdev->qdev, name, size);
1845
1846     ptr = qemu_get_ram_ptr(pdev->rom_offset);
1847     load_image(path, ptr);
1848     qemu_free(path);
1849
1850     if (is_default_rom) {
1851         /* Only the default rom images will be patched (if needed). */
1852         pci_patch_ids(pdev, ptr, size);
1853     }
1854
1855     pci_register_bar(pdev, PCI_ROM_SLOT, size,
1856                      0, pci_map_option_rom);
1857
1858     return 0;
1859 }
1860
1861 static void pci_del_option_rom(PCIDevice *pdev)
1862 {
1863     if (!pdev->rom_offset)
1864         return;
1865
1866     qemu_ram_free(pdev->rom_offset);
1867     pdev->rom_offset = 0;
1868 }
1869
1870 /*
1871  * if !offset
1872  * Reserve space and add capability to the linked list in pci config space
1873  *
1874  * if offset = 0,
1875  * Find and reserve space and add capability to the linked list
1876  * in pci config space */
1877 int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
1878                        uint8_t offset, uint8_t size)
1879 {
1880     uint8_t *config;
1881     if (!offset) {
1882         offset = pci_find_space(pdev, size);
1883         if (!offset) {
1884             return -ENOSPC;
1885         }
1886     }
1887
1888     config = pdev->config + offset;
1889     config[PCI_CAP_LIST_ID] = cap_id;
1890     config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1891     pdev->config[PCI_CAPABILITY_LIST] = offset;
1892     pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1893     memset(pdev->used + offset, 0xFF, size);
1894     /* Make capability read-only by default */
1895     memset(pdev->wmask + offset, 0, size);
1896     /* Check capability by default */
1897     memset(pdev->cmask + offset, 0xFF, size);
1898     return offset;
1899 }
1900
1901 /* Unlink capability from the pci config space. */
1902 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1903 {
1904     uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1905     if (!offset)
1906         return;
1907     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1908     /* Make capability writeable again */
1909     memset(pdev->wmask + offset, 0xff, size);
1910     memset(pdev->w1cmask + offset, 0, size);
1911     /* Clear cmask as device-specific registers can't be checked */
1912     memset(pdev->cmask + offset, 0, size);
1913     memset(pdev->used + offset, 0, size);
1914
1915     if (!pdev->config[PCI_CAPABILITY_LIST])
1916         pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1917 }
1918
1919 /* Reserve space for capability at a known offset (to call after load). */
1920 void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1921 {
1922     memset(pdev->used + offset, 0xff, size);
1923 }
1924
1925 uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1926 {
1927     return pci_find_capability_list(pdev, cap_id, NULL);
1928 }
1929
1930 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1931 {
1932     PCIDevice *d = (PCIDevice *)dev;
1933     const pci_class_desc *desc;
1934     char ctxt[64];
1935     PCIIORegion *r;
1936     int i, class;
1937
1938     class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1939     desc = pci_class_descriptions;
1940     while (desc->desc && class != desc->class)
1941         desc++;
1942     if (desc->desc) {
1943         snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1944     } else {
1945         snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1946     }
1947
1948     monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1949                    "pci id %04x:%04x (sub %04x:%04x)\n",
1950                    indent, "", ctxt, pci_bus_num(d->bus),
1951                    PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1952                    pci_get_word(d->config + PCI_VENDOR_ID),
1953                    pci_get_word(d->config + PCI_DEVICE_ID),
1954                    pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1955                    pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1956     for (i = 0; i < PCI_NUM_REGIONS; i++) {
1957         r = &d->io_regions[i];
1958         if (!r->size)
1959             continue;
1960         monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1961                        " [0x%"FMT_PCIBUS"]\n",
1962                        indent, "",
1963                        i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1964                        r->addr, r->addr + r->size - 1);
1965     }
1966 }
1967
1968 static char *pcibus_get_dev_path(DeviceState *dev)
1969 {
1970     PCIDevice *d = (PCIDevice *)dev;
1971     char path[16];
1972
1973     snprintf(path, sizeof(path), "%04x:%02x:%02x.%x",
1974              pci_find_domain(d->bus), d->config[PCI_SECONDARY_BUS],
1975              PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
1976
1977     return strdup(path);
1978 }
1979
This page took 0.138943 seconds and 4 git commands to generate.