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