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