]> Git Repo - qemu.git/blob - hw/pci.c
pci: make pci configuration transaction more accurate.
[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 "monitor.h"
27 #include "net.h"
28 #include "sysemu.h"
29
30 //#define DEBUG_PCI
31 #ifdef DEBUG_PCI
32 # define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
33 #else
34 # define PCI_DPRINTF(format, ...)       do { } while (0)
35 #endif
36
37 struct PCIBus {
38     BusState qbus;
39     int devfn_min;
40     pci_set_irq_fn set_irq;
41     pci_map_irq_fn map_irq;
42     pci_hotplug_fn hotplug;
43     uint32_t config_reg; /* XXX: suppress */
44     void *irq_opaque;
45     PCIDevice *devices[256];
46     PCIDevice *parent_dev;
47
48     QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
49     QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
50
51     /* The bus IRQ state is the logical OR of the connected devices.
52        Keep a count of the number of devices with raised IRQs.  */
53     int nirq;
54     int *irq_count;
55 };
56
57 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
58
59 static struct BusInfo pci_bus_info = {
60     .name       = "PCI",
61     .size       = sizeof(PCIBus),
62     .print_dev  = pcibus_dev_print,
63     .props      = (Property[]) {
64         DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
65         DEFINE_PROP_END_OF_LIST()
66     }
67 };
68
69 static void pci_update_mappings(PCIDevice *d);
70 static void pci_set_irq(void *opaque, int irq_num, int level);
71
72 target_phys_addr_t pci_mem_base;
73 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
74 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
75
76 struct PCIHostBus {
77     int domain;
78     struct PCIBus *bus;
79     QLIST_ENTRY(PCIHostBus) next;
80 };
81 static QLIST_HEAD(, PCIHostBus) host_buses;
82
83 static const VMStateDescription vmstate_pcibus = {
84     .name = "PCIBUS",
85     .version_id = 1,
86     .minimum_version_id = 1,
87     .minimum_version_id_old = 1,
88     .fields      = (VMStateField []) {
89         VMSTATE_INT32_EQUAL(nirq, PCIBus),
90         VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
91         VMSTATE_END_OF_LIST()
92     }
93 };
94
95 static int pci_bar(PCIDevice *d, int reg)
96 {
97     uint8_t type;
98
99     if (reg != PCI_ROM_SLOT)
100         return PCI_BASE_ADDRESS_0 + reg * 4;
101
102     type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
103     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
104 }
105
106 static void pci_device_reset(PCIDevice *dev)
107 {
108     int r;
109
110     memset(dev->irq_state, 0, sizeof dev->irq_state);
111     dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
112                                   PCI_COMMAND_MASTER);
113     dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
114     dev->config[PCI_INTERRUPT_LINE] = 0x0;
115     for (r = 0; r < PCI_NUM_REGIONS; ++r) {
116         if (!dev->io_regions[r].size) {
117             continue;
118         }
119         pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type);
120     }
121     pci_update_mappings(dev);
122 }
123
124 static void pci_bus_reset(void *opaque)
125 {
126     PCIBus *bus = opaque;
127     int i;
128
129     for (i = 0; i < bus->nirq; i++) {
130         bus->irq_count[i] = 0;
131     }
132     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
133         if (bus->devices[i]) {
134             pci_device_reset(bus->devices[i]);
135         }
136     }
137 }
138
139 static void pci_host_bus_register(int domain, PCIBus *bus)
140 {
141     struct PCIHostBus *host;
142     host = qemu_mallocz(sizeof(*host));
143     host->domain = domain;
144     host->bus = bus;
145     QLIST_INSERT_HEAD(&host_buses, host, next);
146 }
147
148 PCIBus *pci_find_host_bus(int domain)
149 {
150     struct PCIHostBus *host;
151
152     QLIST_FOREACH(host, &host_buses, next) {
153         if (host->domain == domain) {
154             return host->bus;
155         }
156     }
157
158     return NULL;
159 }
160
161 void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
162                          const char *name, int devfn_min)
163 {
164     static int nbus = 0;
165
166     qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
167     bus->devfn_min = devfn_min;
168
169     /* host bridge */
170     QLIST_INIT(&bus->child);
171     pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
172
173     vmstate_register(nbus++, &vmstate_pcibus, bus);
174     qemu_register_reset(pci_bus_reset, bus);
175 }
176
177 PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
178 {
179     PCIBus *bus;
180
181     bus = qemu_mallocz(sizeof(*bus));
182     bus->qbus.qdev_allocated = 1;
183     pci_bus_new_inplace(bus, parent, name, devfn_min);
184     return bus;
185 }
186
187 void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
188                   void *irq_opaque, int nirq)
189 {
190     bus->set_irq = set_irq;
191     bus->map_irq = map_irq;
192     bus->irq_opaque = irq_opaque;
193     bus->nirq = nirq;
194     bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
195 }
196
197 void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
198 {
199     bus->qbus.allow_hotplug = 1;
200     bus->hotplug = hotplug;
201 }
202
203 PCIBus *pci_register_bus(DeviceState *parent, const char *name,
204                          pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
205                          void *irq_opaque, int devfn_min, int nirq)
206 {
207     PCIBus *bus;
208
209     bus = pci_bus_new(parent, name, devfn_min);
210     pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
211     return bus;
212 }
213
214 static void pci_register_secondary_bus(PCIBus *parent,
215                                        PCIBus *bus,
216                                        PCIDevice *dev,
217                                        pci_map_irq_fn map_irq,
218                                        const char *name)
219 {
220     qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name);
221     bus->map_irq = map_irq;
222     bus->parent_dev = dev;
223
224     QLIST_INIT(&bus->child);
225     QLIST_INSERT_HEAD(&parent->child, bus, sibling);
226 }
227
228 static void pci_unregister_secondary_bus(PCIBus *bus)
229 {
230     assert(QLIST_EMPTY(&bus->child));
231     QLIST_REMOVE(bus, sibling);
232 }
233
234 int pci_bus_num(PCIBus *s)
235 {
236     if (!s->parent_dev)
237         return 0;       /* pci host bridge */
238     return s->parent_dev->config[PCI_SECONDARY_BUS];
239 }
240
241 static uint8_t pci_sub_bus(PCIBus *s)
242 {
243     if (!s->parent_dev)
244         return 255;     /* pci host bridge */
245     return s->parent_dev->config[PCI_SUBORDINATE_BUS];
246 }
247
248 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
249 {
250     PCIDevice *s = container_of(pv, PCIDevice, config);
251     uint8_t config[PCI_CONFIG_SPACE_SIZE];
252     int i;
253
254     assert(size == sizeof config);
255     qemu_get_buffer(f, config, sizeof config);
256     for (i = 0; i < sizeof config; ++i)
257         if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i])
258             return -EINVAL;
259     memcpy(s->config, config, sizeof config);
260
261     pci_update_mappings(s);
262
263     return 0;
264 }
265
266 /* just put buffer */
267 static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
268 {
269     const uint8_t *v = pv;
270     qemu_put_buffer(f, v, size);
271 }
272
273 static VMStateInfo vmstate_info_pci_config = {
274     .name = "pci config",
275     .get  = get_pci_config_device,
276     .put  = put_pci_config_device,
277 };
278
279 const VMStateDescription vmstate_pci_device = {
280     .name = "PCIDevice",
281     .version_id = 2,
282     .minimum_version_id = 1,
283     .minimum_version_id_old = 1,
284     .fields      = (VMStateField []) {
285         VMSTATE_INT32_LE(version_id, PCIDevice),
286         VMSTATE_SINGLE(config, PCIDevice, 0, vmstate_info_pci_config,
287                        typeof_field(PCIDevice,config)),
288         VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
289         VMSTATE_END_OF_LIST()
290     }
291 };
292
293 void pci_device_save(PCIDevice *s, QEMUFile *f)
294 {
295     vmstate_save_state(f, &vmstate_pci_device, s);
296 }
297
298 int pci_device_load(PCIDevice *s, QEMUFile *f)
299 {
300     return vmstate_load_state(f, &vmstate_pci_device, s, s->version_id);
301 }
302
303 static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
304 {
305     uint16_t *id;
306
307     id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
308     id[0] = cpu_to_le16(pci_default_sub_vendor_id);
309     id[1] = cpu_to_le16(pci_default_sub_device_id);
310     return 0;
311 }
312
313 /*
314  * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
315  */
316 static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
317 {
318     const char *p;
319     char *e;
320     unsigned long val;
321     unsigned long dom = 0, bus = 0;
322     unsigned slot = 0;
323
324     p = addr;
325     val = strtoul(p, &e, 16);
326     if (e == p)
327         return -1;
328     if (*e == ':') {
329         bus = val;
330         p = e + 1;
331         val = strtoul(p, &e, 16);
332         if (e == p)
333             return -1;
334         if (*e == ':') {
335             dom = bus;
336             bus = val;
337             p = e + 1;
338             val = strtoul(p, &e, 16);
339             if (e == p)
340                 return -1;
341         }
342     }
343
344     if (dom > 0xffff || bus > 0xff || val > 0x1f)
345         return -1;
346
347     slot = val;
348
349     if (*e)
350         return -1;
351
352     /* Note: QEMU doesn't implement domains other than 0 */
353     if (!pci_find_bus(pci_find_host_bus(dom), bus))
354         return -1;
355
356     *domp = dom;
357     *busp = bus;
358     *slotp = slot;
359     return 0;
360 }
361
362 int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
363                      unsigned *slotp)
364 {
365     /* strip legacy tag */
366     if (!strncmp(addr, "pci_addr=", 9)) {
367         addr += 9;
368     }
369     if (pci_parse_devaddr(addr, domp, busp, slotp)) {
370         monitor_printf(mon, "Invalid pci address\n");
371         return -1;
372     }
373     return 0;
374 }
375
376 PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
377 {
378     int dom, bus;
379     unsigned slot;
380
381     if (!devaddr) {
382         *devfnp = -1;
383         return pci_find_bus(pci_find_host_bus(0), 0);
384     }
385
386     if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
387         return NULL;
388     }
389
390     *devfnp = slot << 3;
391     return pci_find_bus(pci_find_host_bus(0), bus);
392 }
393
394 static void pci_init_cmask(PCIDevice *dev)
395 {
396     pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
397     pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
398     dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
399     dev->cmask[PCI_REVISION_ID] = 0xff;
400     dev->cmask[PCI_CLASS_PROG] = 0xff;
401     pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
402     dev->cmask[PCI_HEADER_TYPE] = 0xff;
403     dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
404 }
405
406 static void pci_init_wmask(PCIDevice *dev)
407 {
408     int i;
409     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
410     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
411     pci_set_word(dev->wmask + PCI_COMMAND,
412                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
413     for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i)
414         dev->wmask[i] = 0xff;
415 }
416
417 /* -1 for devfn means auto assign */
418 static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
419                                          const char *name, int devfn,
420                                          PCIConfigReadFunc *config_read,
421                                          PCIConfigWriteFunc *config_write)
422 {
423     if (devfn < 0) {
424         for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
425             if (!bus->devices[devfn])
426                 goto found;
427         }
428         return NULL;
429     found: ;
430     } else if (bus->devices[devfn]) {
431         return NULL;
432     }
433     pci_dev->bus = bus;
434     pci_dev->devfn = devfn;
435     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
436     memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
437     pci_set_default_subsystem_id(pci_dev);
438     pci_init_cmask(pci_dev);
439     pci_init_wmask(pci_dev);
440
441     if (!config_read)
442         config_read = pci_default_read_config;
443     if (!config_write)
444         config_write = pci_default_write_config;
445     pci_dev->config_read = config_read;
446     pci_dev->config_write = config_write;
447     bus->devices[devfn] = pci_dev;
448     pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
449     pci_dev->version_id = 2; /* Current pci device vmstate version */
450     return pci_dev;
451 }
452
453 PCIDevice *pci_register_device(PCIBus *bus, const char *name,
454                                int instance_size, int devfn,
455                                PCIConfigReadFunc *config_read,
456                                PCIConfigWriteFunc *config_write)
457 {
458     PCIDevice *pci_dev;
459
460     pci_dev = qemu_mallocz(instance_size);
461     pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
462                                      config_read, config_write);
463     return pci_dev;
464 }
465 static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
466 {
467     return addr + pci_mem_base;
468 }
469
470 static void pci_unregister_io_regions(PCIDevice *pci_dev)
471 {
472     PCIIORegion *r;
473     int i;
474
475     for(i = 0; i < PCI_NUM_REGIONS; i++) {
476         r = &pci_dev->io_regions[i];
477         if (!r->size || r->addr == PCI_BAR_UNMAPPED)
478             continue;
479         if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
480             isa_unassign_ioport(r->addr, r->size);
481         } else {
482             cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
483                                                      r->size,
484                                                      IO_MEM_UNASSIGNED);
485         }
486     }
487 }
488
489 static int pci_unregister_device(DeviceState *dev)
490 {
491     PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
492     PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
493     int ret = 0;
494
495     if (info->exit)
496         ret = info->exit(pci_dev);
497     if (ret)
498         return ret;
499
500     pci_unregister_io_regions(pci_dev);
501
502     qemu_free_irqs(pci_dev->irq);
503     pci_dev->bus->devices[pci_dev->devfn] = NULL;
504     return 0;
505 }
506
507 void pci_register_bar(PCIDevice *pci_dev, int region_num,
508                             pcibus_t size, int type,
509                             PCIMapIORegionFunc *map_func)
510 {
511     PCIIORegion *r;
512     uint32_t addr;
513     pcibus_t wmask;
514
515     if ((unsigned int)region_num >= PCI_NUM_REGIONS)
516         return;
517
518     if (size & (size-1)) {
519         fprintf(stderr, "ERROR: PCI region size must be pow2 "
520                     "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
521         exit(1);
522     }
523
524     r = &pci_dev->io_regions[region_num];
525     r->addr = PCI_BAR_UNMAPPED;
526     r->size = size;
527     r->type = type;
528     r->map_func = map_func;
529
530     wmask = ~(size - 1);
531     addr = pci_bar(pci_dev, region_num);
532     if (region_num == PCI_ROM_SLOT) {
533         /* ROM enable bit is writeable */
534         wmask |= PCI_ROM_ADDRESS_ENABLE;
535     }
536     pci_set_long(pci_dev->config + addr, type);
537     if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
538         r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
539         pci_set_quad(pci_dev->wmask + addr, wmask);
540         pci_set_quad(pci_dev->cmask + addr, ~0ULL);
541     } else {
542         pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
543         pci_set_long(pci_dev->cmask + addr, 0xffffffff);
544     }
545 }
546
547 static void pci_update_mappings(PCIDevice *d)
548 {
549     PCIIORegion *r;
550     int cmd, i;
551     pcibus_t last_addr, new_addr;
552
553     cmd = pci_get_word(d->config + PCI_COMMAND);
554     for(i = 0; i < PCI_NUM_REGIONS; i++) {
555         r = &d->io_regions[i];
556         if (r->size != 0) {
557             if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
558                 if (cmd & PCI_COMMAND_IO) {
559                     new_addr = pci_get_long(d->config + pci_bar(d, i));
560                     new_addr = new_addr & ~(r->size - 1);
561                     last_addr = new_addr + r->size - 1;
562                     /* NOTE: we have only 64K ioports on PC */
563                     if (last_addr <= new_addr || new_addr == 0 ||
564                         last_addr >= 0x10000) {
565                         new_addr = PCI_BAR_UNMAPPED;
566                     }
567                 } else {
568                     new_addr = PCI_BAR_UNMAPPED;
569                 }
570             } else {
571                 if (cmd & PCI_COMMAND_MEMORY) {
572                     if (r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
573                         new_addr = pci_get_quad(d->config + pci_bar(d, i));
574                     } else {
575                         new_addr = pci_get_long(d->config + pci_bar(d, i));
576                     }
577                     /* the ROM slot has a specific enable bit */
578                     if (i == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE))
579                         goto no_mem_map;
580                     new_addr = new_addr & ~(r->size - 1);
581                     last_addr = new_addr + r->size - 1;
582                     /* NOTE: we do not support wrapping */
583                     /* XXX: as we cannot support really dynamic
584                        mappings, we handle specific values as invalid
585                        mappings. */
586                     if (last_addr <= new_addr || new_addr == 0 ||
587                         last_addr == PCI_BAR_UNMAPPED ||
588
589                         /* Now pcibus_t is 64bit.
590                          * Check if 32 bit BAR wrap around explicitly.
591                          * Without this, PC ide doesn't work well.
592                          * TODO: remove this work around.
593                          */
594                         (!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) &&
595                          last_addr >= UINT32_MAX) ||
596
597                         /*
598                          * OS is allowed to set BAR beyond its addressable
599                          * bits. For example, 32 bit OS can set 64bit bar
600                          * to >4G. Check it.
601                          */
602                         last_addr >= TARGET_PHYS_ADDR_MAX) {
603                         new_addr = PCI_BAR_UNMAPPED;
604                     }
605                 } else {
606                 no_mem_map:
607                     new_addr = PCI_BAR_UNMAPPED;
608                 }
609             }
610             /* now do the real mapping */
611             if (new_addr != r->addr) {
612                 if (r->addr != PCI_BAR_UNMAPPED) {
613                     if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
614                         int class;
615                         /* NOTE: specific hack for IDE in PC case:
616                            only one byte must be mapped. */
617                         class = pci_get_word(d->config + PCI_CLASS_DEVICE);
618                         if (class == 0x0101 && r->size == 4) {
619                             isa_unassign_ioport(r->addr + 2, 1);
620                         } else {
621                             isa_unassign_ioport(r->addr, r->size);
622                         }
623                     } else {
624                         cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
625                                                      r->size,
626                                                      IO_MEM_UNASSIGNED);
627                         qemu_unregister_coalesced_mmio(r->addr, r->size);
628                     }
629                 }
630                 r->addr = new_addr;
631                 if (r->addr != PCI_BAR_UNMAPPED) {
632                     r->map_func(d, i, r->addr, r->size, r->type);
633                 }
634             }
635         }
636     }
637 }
638
639 uint32_t pci_default_read_config(PCIDevice *d,
640                                  uint32_t address, int len)
641 {
642     uint32_t val = 0;
643     assert(len == 1 || len == 2 || len == 4);
644     len = MIN(len, PCI_CONFIG_SPACE_SIZE - address);
645     memcpy(&val, d->config + address, len);
646     return le32_to_cpu(val);
647 }
648
649 void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
650 {
651     uint8_t orig[PCI_CONFIG_SPACE_SIZE];
652     int i;
653
654     /* not efficient, but simple */
655     memcpy(orig, d->config, PCI_CONFIG_SPACE_SIZE);
656     for(i = 0; i < l && addr < PCI_CONFIG_SPACE_SIZE; val >>= 8, ++i, ++addr) {
657         uint8_t wmask = d->wmask[addr];
658         d->config[addr] = (d->config[addr] & ~wmask) | (val & wmask);
659     }
660     if (memcmp(orig + PCI_BASE_ADDRESS_0, d->config + PCI_BASE_ADDRESS_0, 24)
661         || ((orig[PCI_COMMAND] ^ d->config[PCI_COMMAND])
662             & (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)))
663         pci_update_mappings(d);
664 }
665
666 void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
667 {
668     PCIBus *s = opaque;
669     PCIDevice *pci_dev;
670     int config_addr, bus_num;
671
672 #if 0
673     PCI_DPRINTF("pci_data_write: addr=%08x val=%08x len=%d\n",
674                 addr, val, len);
675 #endif
676     bus_num = (addr >> 16) & 0xff;
677     s = pci_find_bus(s, bus_num);
678     if (!s)
679         return;
680     pci_dev = s->devices[(addr >> 8) & 0xff];
681     if (!pci_dev)
682         return;
683     config_addr = addr & 0xff;
684     PCI_DPRINTF("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
685                 pci_dev->name, config_addr, val, len);
686     pci_dev->config_write(pci_dev, config_addr, val, len);
687 }
688
689 uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
690 {
691     PCIBus *s = opaque;
692     PCIDevice *pci_dev;
693     int config_addr, bus_num;
694     uint32_t val;
695
696     bus_num = (addr >> 16) & 0xff;
697     s = pci_find_bus(s, bus_num);
698     if (!s)
699         goto fail;
700     pci_dev = s->devices[(addr >> 8) & 0xff];
701     if (!pci_dev) {
702     fail:
703         switch(len) {
704         case 1:
705             val = 0xff;
706             break;
707         case 2:
708             val = 0xffff;
709             break;
710         default:
711         case 4:
712             val = 0xffffffff;
713             break;
714         }
715         goto the_end;
716     }
717     config_addr = addr & 0xff;
718     val = pci_dev->config_read(pci_dev, config_addr, len);
719     PCI_DPRINTF("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
720                 pci_dev->name, config_addr, val, len);
721  the_end:
722 #if 0
723     PCI_DPRINTF("pci_data_read: addr=%08x val=%08x len=%d\n",
724                 addr, val, len);
725 #endif
726     return val;
727 }
728
729 /***********************************************************/
730 /* generic PCI irq support */
731
732 /* 0 <= irq_num <= 3. level must be 0 or 1 */
733 static void pci_set_irq(void *opaque, int irq_num, int level)
734 {
735     PCIDevice *pci_dev = opaque;
736     PCIBus *bus;
737     int change;
738
739     change = level - pci_dev->irq_state[irq_num];
740     if (!change)
741         return;
742
743     pci_dev->irq_state[irq_num] = level;
744     for (;;) {
745         bus = pci_dev->bus;
746         irq_num = bus->map_irq(pci_dev, irq_num);
747         if (bus->set_irq)
748             break;
749         pci_dev = bus->parent_dev;
750     }
751     bus->irq_count[irq_num] += change;
752     bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
753 }
754
755 /***********************************************************/
756 /* monitor info on PCI */
757
758 typedef struct {
759     uint16_t class;
760     const char *desc;
761 } pci_class_desc;
762
763 static const pci_class_desc pci_class_descriptions[] =
764 {
765     { 0x0100, "SCSI controller"},
766     { 0x0101, "IDE controller"},
767     { 0x0102, "Floppy controller"},
768     { 0x0103, "IPI controller"},
769     { 0x0104, "RAID controller"},
770     { 0x0106, "SATA controller"},
771     { 0x0107, "SAS controller"},
772     { 0x0180, "Storage controller"},
773     { 0x0200, "Ethernet controller"},
774     { 0x0201, "Token Ring controller"},
775     { 0x0202, "FDDI controller"},
776     { 0x0203, "ATM controller"},
777     { 0x0280, "Network controller"},
778     { 0x0300, "VGA controller"},
779     { 0x0301, "XGA controller"},
780     { 0x0302, "3D controller"},
781     { 0x0380, "Display controller"},
782     { 0x0400, "Video controller"},
783     { 0x0401, "Audio controller"},
784     { 0x0402, "Phone"},
785     { 0x0480, "Multimedia controller"},
786     { 0x0500, "RAM controller"},
787     { 0x0501, "Flash controller"},
788     { 0x0580, "Memory controller"},
789     { 0x0600, "Host bridge"},
790     { 0x0601, "ISA bridge"},
791     { 0x0602, "EISA bridge"},
792     { 0x0603, "MC bridge"},
793     { 0x0604, "PCI bridge"},
794     { 0x0605, "PCMCIA bridge"},
795     { 0x0606, "NUBUS bridge"},
796     { 0x0607, "CARDBUS bridge"},
797     { 0x0608, "RACEWAY bridge"},
798     { 0x0680, "Bridge"},
799     { 0x0c03, "USB controller"},
800     { 0, NULL}
801 };
802
803 static void pci_info_device(PCIBus *bus, PCIDevice *d)
804 {
805     Monitor *mon = cur_mon;
806     int i, class;
807     PCIIORegion *r;
808     const pci_class_desc *desc;
809
810     monitor_printf(mon, "  Bus %2d, device %3d, function %d:\n",
811                    pci_bus_num(d->bus),
812                    PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
813     class = pci_get_word(d->config + PCI_CLASS_DEVICE);
814     monitor_printf(mon, "    ");
815     desc = pci_class_descriptions;
816     while (desc->desc && class != desc->class)
817         desc++;
818     if (desc->desc) {
819         monitor_printf(mon, "%s", desc->desc);
820     } else {
821         monitor_printf(mon, "Class %04x", class);
822     }
823     monitor_printf(mon, ": PCI device %04x:%04x\n",
824            pci_get_word(d->config + PCI_VENDOR_ID),
825            pci_get_word(d->config + PCI_DEVICE_ID));
826
827     if (d->config[PCI_INTERRUPT_PIN] != 0) {
828         monitor_printf(mon, "      IRQ %d.\n",
829                        d->config[PCI_INTERRUPT_LINE]);
830     }
831     if (class == 0x0604) {
832         monitor_printf(mon, "      BUS %d.\n", d->config[0x19]);
833     }
834     for(i = 0;i < PCI_NUM_REGIONS; i++) {
835         r = &d->io_regions[i];
836         if (r->size != 0) {
837             monitor_printf(mon, "      BAR%d: ", i);
838             if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
839                 monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
840                                " [0x%04"FMT_PCIBUS"].\n",
841                                r->addr, r->addr + r->size - 1);
842             } else {
843                 const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ?
844                     "64 bit" : "32 bit";
845                 const char *prefetch =
846                     r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ?
847                     " prefetchable" : "";
848
849                 monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
850                                " [0x%08"FMT_PCIBUS"].\n",
851                                type, prefetch,
852                                r->addr, r->addr + r->size - 1);
853             }
854         }
855     }
856     monitor_printf(mon, "      id \"%s\"\n", d->qdev.id ? d->qdev.id : "");
857     if (class == 0x0604 && d->config[0x19] != 0) {
858         pci_for_each_device(bus, d->config[0x19], pci_info_device);
859     }
860 }
861
862 void pci_for_each_device(PCIBus *bus, int bus_num,
863                          void (*fn)(PCIBus *b, PCIDevice *d))
864 {
865     PCIDevice *d;
866     int devfn;
867
868     bus = pci_find_bus(bus, bus_num);
869     if (bus) {
870         for(devfn = 0; devfn < 256; devfn++) {
871             d = bus->devices[devfn];
872             if (d)
873                 fn(bus, d);
874         }
875     }
876 }
877
878 void pci_info(Monitor *mon)
879 {
880     struct PCIHostBus *host;
881     QLIST_FOREACH(host, &host_buses, next) {
882         pci_for_each_device(host->bus, 0, pci_info_device);
883     }
884 }
885
886 static const char * const pci_nic_models[] = {
887     "ne2k_pci",
888     "i82551",
889     "i82557b",
890     "i82559er",
891     "rtl8139",
892     "e1000",
893     "pcnet",
894     "virtio",
895     NULL
896 };
897
898 static const char * const pci_nic_names[] = {
899     "ne2k_pci",
900     "i82551",
901     "i82557b",
902     "i82559er",
903     "rtl8139",
904     "e1000",
905     "pcnet",
906     "virtio-net-pci",
907     NULL
908 };
909
910 /* Initialize a PCI NIC.  */
911 /* FIXME callers should check for failure, but don't */
912 PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
913                         const char *default_devaddr)
914 {
915     const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
916     PCIBus *bus;
917     int devfn;
918     PCIDevice *pci_dev;
919     DeviceState *dev;
920     int i;
921
922     i = qemu_find_nic_model(nd, pci_nic_models, default_model);
923     if (i < 0)
924         return NULL;
925
926     bus = pci_get_bus_devfn(&devfn, devaddr);
927     if (!bus) {
928         qemu_error("Invalid PCI device address %s for device %s\n",
929                    devaddr, pci_nic_names[i]);
930         return NULL;
931     }
932
933     pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
934     dev = &pci_dev->qdev;
935     if (nd->name)
936         dev->id = qemu_strdup(nd->name);
937     qdev_set_nic_properties(dev, nd);
938     if (qdev_init(dev) < 0)
939         return NULL;
940     return pci_dev;
941 }
942
943 PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
944                                const char *default_devaddr)
945 {
946     PCIDevice *res;
947
948     if (qemu_show_nic_models(nd->model, pci_nic_models))
949         exit(0);
950
951     res = pci_nic_init(nd, default_model, default_devaddr);
952     if (!res)
953         exit(1);
954     return res;
955 }
956
957 typedef struct {
958     PCIDevice dev;
959     PCIBus bus;
960     uint32_t vid;
961     uint32_t did;
962 } PCIBridge;
963
964 static void pci_bridge_write_config(PCIDevice *d,
965                              uint32_t address, uint32_t val, int len)
966 {
967     pci_default_write_config(d, address, val, len);
968 }
969
970 PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
971 {
972     PCIBus *sec;
973
974     if (!bus)
975         return NULL;
976
977     if (pci_bus_num(bus) == bus_num) {
978         return bus;
979     }
980
981     /* try child bus */
982     QLIST_FOREACH(sec, &bus->child, sibling) {
983         if (pci_bus_num(sec) <= bus_num && bus_num <= pci_sub_bus(sec)) {
984             return pci_find_bus(sec, bus_num);
985         }
986     }
987
988     return NULL;
989 }
990
991 PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
992 {
993     bus = pci_find_bus(bus, bus_num);
994
995     if (!bus)
996         return NULL;
997
998     return bus->devices[PCI_DEVFN(slot, function)];
999 }
1000
1001 static int pci_bridge_initfn(PCIDevice *dev)
1002 {
1003     PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev);
1004
1005     pci_config_set_vendor_id(s->dev.config, s->vid);
1006     pci_config_set_device_id(s->dev.config, s->did);
1007
1008     /* TODO: intial value
1009      * command register:
1010      * According to PCI bridge spec, after reset
1011      *   bus master bit is off
1012      *   memory space enable bit is off
1013      * According to manual (805-1251.pdf).(See abp_pbi.c for its links.)
1014      *   the reset value should be zero unless the boot pin is tied high
1015      *   (which is tru) and thus it should be PCI_COMMAND_MEMORY.
1016      *
1017      * For now, don't touch the value.
1018      * Later command register will be set to zero and apb_pci.c will
1019      * override the value.
1020      * Same for latency timer, and multi function bit of header type.
1021      */
1022     pci_set_word(dev->config + PCI_COMMAND,
1023                  PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1024
1025     pci_set_word(dev->config + PCI_STATUS,
1026                  PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1027     pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
1028     dev->config[PCI_LATENCY_TIMER] = 0x10;
1029     dev->config[PCI_HEADER_TYPE] =
1030         PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE;
1031     pci_set_word(dev->config + PCI_SEC_STATUS,
1032                  PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1033     return 0;
1034 }
1035
1036 static int pci_bridge_exitfn(PCIDevice *pci_dev)
1037 {
1038     PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
1039     PCIBus *bus = &s->bus;
1040     pci_unregister_secondary_bus(bus);
1041     return 0;
1042 }
1043
1044 PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
1045                         pci_map_irq_fn map_irq, const char *name)
1046 {
1047     PCIDevice *dev;
1048     PCIBridge *s;
1049
1050     dev = pci_create(bus, devfn, "pci-bridge");
1051     qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
1052     qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
1053     qdev_init_nofail(&dev->qdev);
1054
1055     s = DO_UPCAST(PCIBridge, dev, dev);
1056     pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name);
1057     return &s->bus;
1058 }
1059
1060 static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1061 {
1062     PCIDevice *pci_dev = (PCIDevice *)qdev;
1063     PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1064     PCIBus *bus;
1065     int devfn, rc;
1066
1067     bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1068     devfn = pci_dev->devfn;
1069     pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1070                                      info->config_read, info->config_write);
1071     assert(pci_dev);
1072     rc = info->init(pci_dev);
1073     if (rc != 0)
1074         return rc;
1075     if (qdev->hotplugged)
1076         bus->hotplug(pci_dev, 1);
1077     return 0;
1078 }
1079
1080 static int pci_unplug_device(DeviceState *qdev)
1081 {
1082     PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1083
1084     dev->bus->hotplug(dev, 0);
1085     return 0;
1086 }
1087
1088 void pci_qdev_register(PCIDeviceInfo *info)
1089 {
1090     info->qdev.init = pci_qdev_init;
1091     info->qdev.unplug = pci_unplug_device;
1092     info->qdev.exit = pci_unregister_device;
1093     info->qdev.bus_info = &pci_bus_info;
1094     qdev_register(&info->qdev);
1095 }
1096
1097 void pci_qdev_register_many(PCIDeviceInfo *info)
1098 {
1099     while (info->qdev.name) {
1100         pci_qdev_register(info);
1101         info++;
1102     }
1103 }
1104
1105 PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1106 {
1107     DeviceState *dev;
1108
1109     dev = qdev_create(&bus->qbus, name);
1110     qdev_prop_set_uint32(dev, "addr", devfn);
1111     return DO_UPCAST(PCIDevice, qdev, dev);
1112 }
1113
1114 PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1115 {
1116     PCIDevice *dev = pci_create(bus, devfn, name);
1117     qdev_init_nofail(&dev->qdev);
1118     return dev;
1119 }
1120
1121 static int pci_find_space(PCIDevice *pdev, uint8_t size)
1122 {
1123     int offset = PCI_CONFIG_HEADER_SIZE;
1124     int i;
1125     for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i)
1126         if (pdev->used[i])
1127             offset = i + 1;
1128         else if (i - offset + 1 == size)
1129             return offset;
1130     return 0;
1131 }
1132
1133 static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1134                                         uint8_t *prev_p)
1135 {
1136     uint8_t next, prev;
1137
1138     if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1139         return 0;
1140
1141     for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1142          prev = next + PCI_CAP_LIST_NEXT)
1143         if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1144             break;
1145
1146     if (prev_p)
1147         *prev_p = prev;
1148     return next;
1149 }
1150
1151 /* Reserve space and add capability to the linked list in pci config space */
1152 int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1153 {
1154     uint8_t offset = pci_find_space(pdev, size);
1155     uint8_t *config = pdev->config + offset;
1156     if (!offset)
1157         return -ENOSPC;
1158     config[PCI_CAP_LIST_ID] = cap_id;
1159     config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1160     pdev->config[PCI_CAPABILITY_LIST] = offset;
1161     pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1162     memset(pdev->used + offset, 0xFF, size);
1163     /* Make capability read-only by default */
1164     memset(pdev->wmask + offset, 0, size);
1165     /* Check capability by default */
1166     memset(pdev->cmask + offset, 0xFF, size);
1167     return offset;
1168 }
1169
1170 /* Unlink capability from the pci config space. */
1171 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1172 {
1173     uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1174     if (!offset)
1175         return;
1176     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1177     /* Make capability writeable again */
1178     memset(pdev->wmask + offset, 0xff, size);
1179     /* Clear cmask as device-specific registers can't be checked */
1180     memset(pdev->cmask + offset, 0, size);
1181     memset(pdev->used + offset, 0, size);
1182
1183     if (!pdev->config[PCI_CAPABILITY_LIST])
1184         pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1185 }
1186
1187 /* Reserve space for capability at a known offset (to call after load). */
1188 void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1189 {
1190     memset(pdev->used + offset, 0xff, size);
1191 }
1192
1193 uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1194 {
1195     return pci_find_capability_list(pdev, cap_id, NULL);
1196 }
1197
1198 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1199 {
1200     PCIDevice *d = (PCIDevice *)dev;
1201     const pci_class_desc *desc;
1202     char ctxt[64];
1203     PCIIORegion *r;
1204     int i, class;
1205
1206     class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1207     desc = pci_class_descriptions;
1208     while (desc->desc && class != desc->class)
1209         desc++;
1210     if (desc->desc) {
1211         snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1212     } else {
1213         snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1214     }
1215
1216     monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1217                    "pci id %04x:%04x (sub %04x:%04x)\n",
1218                    indent, "", ctxt,
1219                    d->config[PCI_SECONDARY_BUS],
1220                    PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1221                    pci_get_word(d->config + PCI_VENDOR_ID),
1222                    pci_get_word(d->config + PCI_DEVICE_ID),
1223                    pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1224                    pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1225     for (i = 0; i < PCI_NUM_REGIONS; i++) {
1226         r = &d->io_regions[i];
1227         if (!r->size)
1228             continue;
1229         monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1230                        " [0x%"FMT_PCIBUS"]\n",
1231                        indent, "",
1232                        i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1233                        r->addr, r->addr + r->size - 1);
1234     }
1235 }
1236
1237 static PCIDeviceInfo bridge_info = {
1238     .qdev.name    = "pci-bridge",
1239     .qdev.size    = sizeof(PCIBridge),
1240     .init         = pci_bridge_initfn,
1241     .exit         = pci_bridge_exitfn,
1242     .config_write = pci_bridge_write_config,
1243     .qdev.props   = (Property[]) {
1244         DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0),
1245         DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0),
1246         DEFINE_PROP_END_OF_LIST(),
1247     }
1248 };
1249
1250 static void pci_register_devices(void)
1251 {
1252     pci_qdev_register(&bridge_info);
1253 }
1254
1255 device_init(pci_register_devices)
This page took 0.094419 seconds and 4 git commands to generate.