]> Git Repo - qemu.git/blame - hw/pci/pci.c
qed: Implement .bdrv_drain
[qemu.git] / hw / pci / 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 */
c759b24f
MT
24#include "hw/hw.h"
25#include "hw/pci/pci.h"
26#include "hw/pci/pci_bridge.h"
06aac7bd 27#include "hw/pci/pci_bus.h"
568f0690 28#include "hw/pci/pci_host.h"
83c9089e 29#include "monitor/monitor.h"
1422e32d 30#include "net/net.h"
9c17d615 31#include "sysemu/sysemu.h"
c759b24f 32#include "hw/loader.h"
d49b6836 33#include "qemu/error-report.h"
1de7afc9 34#include "qemu/range.h"
79627472 35#include "qmp-commands.h"
7828d750 36#include "trace.h"
c759b24f
MT
37#include "hw/pci/msi.h"
38#include "hw/pci/msix.h"
022c62cb 39#include "exec/address-spaces.h"
5e954943 40#include "hw/hotplug.h"
e4024630 41#include "hw/boards.h"
69b91039
FB
42
43//#define DEBUG_PCI
d8d2e079 44#ifdef DEBUG_PCI
2e49d64a 45# define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
d8d2e079
IY
46#else
47# define PCI_DPRINTF(format, ...) do { } while (0)
48#endif
69b91039 49
10c4c98a 50static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
4f43c1ff 51static char *pcibus_get_dev_path(DeviceState *dev);
5e0259e7 52static char *pcibus_get_fw_dev_path(DeviceState *dev);
dcc20931 53static void pcibus_reset(BusState *qbus);
10c4c98a 54
3cb75a7c
PB
55static Property pci_props[] = {
56 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
57 DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
58 DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1),
59 DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
60 QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
61 DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present,
62 QEMU_PCI_CAP_SERR_BITNR, true),
63 DEFINE_PROP_END_OF_LIST()
64};
65
d2f69df7
BD
66static const VMStateDescription vmstate_pcibus = {
67 .name = "PCIBUS",
68 .version_id = 1,
69 .minimum_version_id = 1,
d49805ae 70 .fields = (VMStateField[]) {
d2f69df7
BD
71 VMSTATE_INT32_EQUAL(nirq, PCIBus),
72 VMSTATE_VARRAY_INT32(irq_count, PCIBus,
73 nirq, 0, vmstate_info_int32,
74 int32_t),
75 VMSTATE_END_OF_LIST()
76 }
77};
78
79static void pci_bus_realize(BusState *qbus, Error **errp)
80{
81 PCIBus *bus = PCI_BUS(qbus);
82
83 vmstate_register(NULL, -1, &vmstate_pcibus, bus);
84}
85
86static void pci_bus_unrealize(BusState *qbus, Error **errp)
87{
88 PCIBus *bus = PCI_BUS(qbus);
89
90 vmstate_unregister(NULL, &vmstate_pcibus, bus);
91}
92
ce6a28ee
MA
93static bool pcibus_is_root(PCIBus *bus)
94{
95 return !bus->parent_dev;
96}
97
602141d9
MA
98static int pcibus_num(PCIBus *bus)
99{
100 if (pcibus_is_root(bus)) {
101 return 0; /* pci host bridge */
102 }
103 return bus->parent_dev->config[PCI_SECONDARY_BUS];
104}
105
6a3042b2
MA
106static uint16_t pcibus_numa_node(PCIBus *bus)
107{
108 return NUMA_NODE_UNASSIGNED;
109}
110
0d936928
AL
111static void pci_bus_class_init(ObjectClass *klass, void *data)
112{
113 BusClass *k = BUS_CLASS(klass);
ce6a28ee 114 PCIBusClass *pbc = PCI_BUS_CLASS(klass);
0d936928
AL
115
116 k->print_dev = pcibus_dev_print;
117 k->get_dev_path = pcibus_get_dev_path;
118 k->get_fw_dev_path = pcibus_get_fw_dev_path;
d2f69df7
BD
119 k->realize = pci_bus_realize;
120 k->unrealize = pci_bus_unrealize;
0d936928 121 k->reset = pcibus_reset;
ce6a28ee
MA
122
123 pbc->is_root = pcibus_is_root;
602141d9 124 pbc->bus_num = pcibus_num;
6a3042b2 125 pbc->numa_node = pcibus_numa_node;
0d936928
AL
126}
127
128static const TypeInfo pci_bus_info = {
129 .name = TYPE_PCI_BUS,
130 .parent = TYPE_BUS,
131 .instance_size = sizeof(PCIBus),
ce6a28ee 132 .class_size = sizeof(PCIBusClass),
0d936928 133 .class_init = pci_bus_class_init,
30468f78 134};
69b91039 135
3a861c46
AW
136static const TypeInfo pcie_bus_info = {
137 .name = TYPE_PCIE_BUS,
138 .parent = TYPE_PCI_BUS,
139};
140
d662210a 141static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
1941d19c 142static void pci_update_mappings(PCIDevice *d);
d98f08f5 143static void pci_irq_handler(void *opaque, int irq_num, int level);
133e9b22 144static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, Error **);
230741dc 145static void pci_del_option_rom(PCIDevice *pdev);
1941d19c 146
d350d97d
AL
147static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
148static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
e822a52a 149
7588e2b0 150static QLIST_HEAD(, PCIHostState) pci_host_bridges;
30468f78 151
cf8c704d 152int pci_bar(PCIDevice *d, int reg)
5330de09 153{
b3b11697
IY
154 uint8_t type;
155
156 if (reg != PCI_ROM_SLOT)
157 return PCI_BASE_ADDRESS_0 + reg * 4;
158
159 type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
160 return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
5330de09
MT
161}
162
d036bb21
MT
163static inline int pci_irq_state(PCIDevice *d, int irq_num)
164{
165 return (d->irq_state >> irq_num) & 0x1;
166}
167
168static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
169{
170 d->irq_state &= ~(0x1 << irq_num);
171 d->irq_state |= level << irq_num;
172}
173
174static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
175{
176 PCIBus *bus;
177 for (;;) {
178 bus = pci_dev->bus;
179 irq_num = bus->map_irq(pci_dev, irq_num);
180 if (bus->set_irq)
181 break;
182 pci_dev = bus->parent_dev;
183 }
184 bus->irq_count[irq_num] += change;
185 bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
186}
187
9ddf8437
IY
188int pci_bus_get_irq_level(PCIBus *bus, int irq_num)
189{
190 assert(irq_num >= 0);
191 assert(irq_num < bus->nirq);
192 return !!bus->irq_count[irq_num];
193}
194
f9bf77dd
MT
195/* Update interrupt status bit in config space on interrupt
196 * state change. */
197static void pci_update_irq_status(PCIDevice *dev)
198{
199 if (dev->irq_state) {
200 dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
201 } else {
202 dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
203 }
204}
205
4c92325b
IY
206void pci_device_deassert_intx(PCIDevice *dev)
207{
208 int i;
209 for (i = 0; i < PCI_NUM_PINS; ++i) {
d98f08f5 210 pci_irq_handler(dev, i, 0);
4c92325b
IY
211 }
212}
213
dcc20931 214static void pci_do_device_reset(PCIDevice *dev)
5330de09 215{
c0b1905b 216 int r;
6fc4925b 217
4c92325b 218 pci_device_deassert_intx(dev);
58b59014
CR
219 assert(dev->irq_state == 0);
220
ebabb67a 221 /* Clear all writable bits */
99443c21 222 pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
f9aebe2e
MT
223 pci_get_word(dev->wmask + PCI_COMMAND) |
224 pci_get_word(dev->w1cmask + PCI_COMMAND));
89d437df
IY
225 pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
226 pci_get_word(dev->wmask + PCI_STATUS) |
227 pci_get_word(dev->w1cmask + PCI_STATUS));
c0b1905b
MT
228 dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
229 dev->config[PCI_INTERRUPT_LINE] = 0x0;
230 for (r = 0; r < PCI_NUM_REGIONS; ++r) {
71ebd6dc
IY
231 PCIIORegion *region = &dev->io_regions[r];
232 if (!region->size) {
c0b1905b
MT
233 continue;
234 }
71ebd6dc
IY
235
236 if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
237 region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
238 pci_set_quad(dev->config + pci_bar(dev, r), region->type);
239 } else {
240 pci_set_long(dev->config + pci_bar(dev, r), region->type);
241 }
c0b1905b
MT
242 }
243 pci_update_mappings(dev);
cbd2d434
JK
244
245 msi_reset(dev);
246 msix_reset(dev);
5330de09
MT
247}
248
dcc20931
PB
249/*
250 * This function is called on #RST and FLR.
251 * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
252 */
253void pci_device_reset(PCIDevice *dev)
254{
255 qdev_reset_all(&dev->qdev);
256 pci_do_device_reset(dev);
257}
258
9bb33586
IY
259/*
260 * Trigger pci bus reset under a given bus.
dcc20931
PB
261 * Called via qbus_reset_all on RST# assert, after the devices
262 * have been reset qdev_reset_all-ed already.
9bb33586 263 */
dcc20931 264static void pcibus_reset(BusState *qbus)
6eaa6847 265{
81e3e75b 266 PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
6eaa6847
GN
267 int i;
268
5330de09
MT
269 for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
270 if (bus->devices[i]) {
dcc20931 271 pci_do_device_reset(bus->devices[i]);
5330de09 272 }
6eaa6847 273 }
9bb33586 274
9bdbbfc3
PB
275 for (i = 0; i < bus->nirq; i++) {
276 assert(bus->irq_count[i] == 0);
277 }
9bb33586
IY
278}
279
7588e2b0 280static void pci_host_bus_register(PCIBus *bus, DeviceState *parent)
e822a52a 281{
7588e2b0
DG
282 PCIHostState *host_bridge = PCI_HOST_BRIDGE(parent);
283
284 QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next);
e822a52a
IY
285}
286
1ef7a2a2 287PCIBus *pci_find_primary_bus(void)
e822a52a 288{
9bc47305 289 PCIBus *primary_bus = NULL;
7588e2b0 290 PCIHostState *host;
e822a52a 291
7588e2b0 292 QLIST_FOREACH(host, &pci_host_bridges, next) {
9bc47305
DG
293 if (primary_bus) {
294 /* We have multiple root buses, refuse to select a primary */
295 return NULL;
e822a52a 296 }
9bc47305 297 primary_bus = host->bus;
e822a52a
IY
298 }
299
9bc47305 300 return primary_bus;
e822a52a
IY
301}
302
c473d18d 303PCIBus *pci_device_root_bus(const PCIDevice *d)
e075e788 304{
c473d18d 305 PCIBus *bus = d->bus;
e075e788 306
ce6a28ee
MA
307 while (!pci_bus_is_root(bus)) {
308 d = bus->parent_dev;
309 assert(d != NULL);
310
e075e788
IY
311 bus = d->bus;
312 }
313
c473d18d
DG
314 return bus;
315}
316
568f0690 317const char *pci_root_bus_path(PCIDevice *dev)
c473d18d 318{
568f0690
DG
319 PCIBus *rootbus = pci_device_root_bus(dev);
320 PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
321 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge);
c473d18d 322
568f0690
DG
323 assert(host_bridge->bus == rootbus);
324
325 if (hc->root_bus_path) {
326 return (*hc->root_bus_path)(host_bridge, rootbus);
e075e788
IY
327 }
328
568f0690 329 return rootbus->qbus.name;
e075e788
IY
330}
331
4fec6404 332static void pci_bus_init(PCIBus *bus, DeviceState *parent,
1e39101c 333 const char *name,
aee97b84
AK
334 MemoryRegion *address_space_mem,
335 MemoryRegion *address_space_io,
1e39101c 336 uint8_t devfn_min)
30468f78 337{
6fa84913 338 assert(PCI_FUNC(devfn_min) == 0);
502a5395 339 bus->devfn_min = devfn_min;
5968eca3
AK
340 bus->address_space_mem = address_space_mem;
341 bus->address_space_io = address_space_io;
e822a52a
IY
342
343 /* host bridge */
344 QLIST_INIT(&bus->child);
2b8cc89a 345
7588e2b0 346 pci_host_bus_register(bus, parent);
21eea4b3
GH
347}
348
8c0bf9e2
AW
349bool pci_bus_is_express(PCIBus *bus)
350{
351 return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
352}
353
0889464a
AW
354bool pci_bus_is_root(PCIBus *bus)
355{
ce6a28ee 356 return PCI_BUS_GET_CLASS(bus)->is_root(bus);
0889464a
AW
357}
358
dd301ca6 359void pci_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent,
4fec6404
PB
360 const char *name,
361 MemoryRegion *address_space_mem,
362 MemoryRegion *address_space_io,
60a0e443 363 uint8_t devfn_min, const char *typename)
4fec6404 364{
fb17dfe0 365 qbus_create_inplace(bus, bus_size, typename, parent, name);
4fec6404
PB
366 pci_bus_init(bus, parent, name, address_space_mem,
367 address_space_io, devfn_min);
368}
369
1e39101c 370PCIBus *pci_bus_new(DeviceState *parent, const char *name,
aee97b84
AK
371 MemoryRegion *address_space_mem,
372 MemoryRegion *address_space_io,
60a0e443 373 uint8_t devfn_min, const char *typename)
21eea4b3
GH
374{
375 PCIBus *bus;
376
60a0e443 377 bus = PCI_BUS(qbus_create(typename, parent, name));
4fec6404
PB
378 pci_bus_init(bus, parent, name, address_space_mem,
379 address_space_io, devfn_min);
21eea4b3
GH
380 return bus;
381}
382
383void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
384 void *irq_opaque, int nirq)
385{
386 bus->set_irq = set_irq;
387 bus->map_irq = map_irq;
388 bus->irq_opaque = irq_opaque;
389 bus->nirq = nirq;
7267c094 390 bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0]));
21eea4b3
GH
391}
392
393PCIBus *pci_register_bus(DeviceState *parent, const char *name,
394 pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
1e39101c 395 void *irq_opaque,
aee97b84
AK
396 MemoryRegion *address_space_mem,
397 MemoryRegion *address_space_io,
60a0e443 398 uint8_t devfn_min, int nirq, const char *typename)
21eea4b3
GH
399{
400 PCIBus *bus;
401
aee97b84 402 bus = pci_bus_new(parent, name, address_space_mem,
60a0e443 403 address_space_io, devfn_min, typename);
21eea4b3 404 pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
30468f78
FB
405 return bus;
406}
69b91039 407
502a5395
PB
408int pci_bus_num(PCIBus *s)
409{
602141d9 410 return PCI_BUS_GET_CLASS(s)->bus_num(s);
502a5395
PB
411}
412
6a3042b2
MA
413int pci_bus_numa_node(PCIBus *bus)
414{
415 return PCI_BUS_GET_CLASS(bus)->numa_node(bus);
502a5395
PB
416}
417
73534f2f 418static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
30ca2aab 419{
73534f2f 420 PCIDevice *s = container_of(pv, PCIDevice, config);
e78e9ae4 421 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(s);
a9f49946 422 uint8_t *config;
52fc1d83
AZ
423 int i;
424
a9f49946 425 assert(size == pci_config_size(s));
7267c094 426 config = g_malloc(size);
a9f49946
IY
427
428 qemu_get_buffer(f, config, size);
429 for (i = 0; i < size; ++i) {
f9aebe2e
MT
430 if ((config[i] ^ s->config[i]) &
431 s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
7c59364d
DDAG
432 error_report("%s: Bad config data: i=0x%x read: %x device: %x "
433 "cmask: %x wmask: %x w1cmask:%x", __func__,
434 i, config[i], s->config[i],
435 s->cmask[i], s->wmask[i], s->w1cmask[i]);
7267c094 436 g_free(config);
bd4b65ee 437 return -EINVAL;
a9f49946
IY
438 }
439 }
440 memcpy(s->config, config, size);
bd4b65ee 441
1941d19c 442 pci_update_mappings(s);
e78e9ae4 443 if (pc->is_bridge) {
f055e96b 444 PCIBridge *b = PCI_BRIDGE(s);
e78e9ae4
DK
445 pci_bridge_update_mappings(b);
446 }
52fc1d83 447
4ea375bf
GH
448 memory_region_set_enabled(&s->bus_master_enable_region,
449 pci_get_word(s->config + PCI_COMMAND)
450 & PCI_COMMAND_MASTER);
451
7267c094 452 g_free(config);
30ca2aab
FB
453 return 0;
454}
455
73534f2f 456/* just put buffer */
84e2e3eb 457static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
73534f2f 458{
dbe73d7f 459 const uint8_t **v = pv;
a9f49946 460 assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
dbe73d7f 461 qemu_put_buffer(f, *v, size);
73534f2f
JQ
462}
463
464static VMStateInfo vmstate_info_pci_config = {
465 .name = "pci config",
466 .get = get_pci_config_device,
467 .put = put_pci_config_device,
468};
469
d036bb21
MT
470static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
471{
c3f8f611 472 PCIDevice *s = container_of(pv, PCIDevice, irq_state);
d036bb21
MT
473 uint32_t irq_state[PCI_NUM_PINS];
474 int i;
475 for (i = 0; i < PCI_NUM_PINS; ++i) {
476 irq_state[i] = qemu_get_be32(f);
477 if (irq_state[i] != 0x1 && irq_state[i] != 0) {
478 fprintf(stderr, "irq state %d: must be 0 or 1.\n",
479 irq_state[i]);
480 return -EINVAL;
481 }
482 }
483
484 for (i = 0; i < PCI_NUM_PINS; ++i) {
485 pci_set_irq_state(s, i, irq_state[i]);
486 }
487
488 return 0;
489}
490
491static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
492{
493 int i;
c3f8f611 494 PCIDevice *s = container_of(pv, PCIDevice, irq_state);
d036bb21
MT
495
496 for (i = 0; i < PCI_NUM_PINS; ++i) {
497 qemu_put_be32(f, pci_irq_state(s, i));
498 }
499}
500
501static VMStateInfo vmstate_info_pci_irq_state = {
502 .name = "pci irq state",
503 .get = get_pci_irq_state,
504 .put = put_pci_irq_state,
505};
506
73534f2f
JQ
507const VMStateDescription vmstate_pci_device = {
508 .name = "PCIDevice",
509 .version_id = 2,
510 .minimum_version_id = 1,
d49805ae 511 .fields = (VMStateField[]) {
3476436a 512 VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice),
a9f49946
IY
513 VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
514 vmstate_info_pci_config,
515 PCI_CONFIG_SPACE_SIZE),
d036bb21
MT
516 VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
517 vmstate_info_pci_irq_state,
518 PCI_NUM_PINS * sizeof(int32_t)),
a9f49946
IY
519 VMSTATE_END_OF_LIST()
520 }
521};
522
523const VMStateDescription vmstate_pcie_device = {
1de53459 524 .name = "PCIEDevice",
a9f49946
IY
525 .version_id = 2,
526 .minimum_version_id = 1,
d49805ae 527 .fields = (VMStateField[]) {
3476436a 528 VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice),
a9f49946
IY
529 VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
530 vmstate_info_pci_config,
531 PCIE_CONFIG_SPACE_SIZE),
d036bb21
MT
532 VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
533 vmstate_info_pci_irq_state,
534 PCI_NUM_PINS * sizeof(int32_t)),
73534f2f
JQ
535 VMSTATE_END_OF_LIST()
536 }
537};
538
a9f49946
IY
539static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
540{
541 return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
542}
543
73534f2f
JQ
544void pci_device_save(PCIDevice *s, QEMUFile *f)
545{
f9bf77dd
MT
546 /* Clear interrupt status bit: it is implicit
547 * in irq_state which we are saving.
548 * This makes us compatible with old devices
549 * which never set or clear this bit. */
550 s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
8118f095 551 vmstate_save_state(f, pci_get_vmstate(s), s, NULL);
f9bf77dd
MT
552 /* Restore the interrupt status bit. */
553 pci_update_irq_status(s);
73534f2f
JQ
554}
555
556int pci_device_load(PCIDevice *s, QEMUFile *f)
557{
f9bf77dd
MT
558 int ret;
559 ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
560 /* Restore the interrupt status bit. */
561 pci_update_irq_status(s);
562 return ret;
73534f2f
JQ
563}
564
5e434f4e 565static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
d350d97d 566{
5e434f4e
IY
567 pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
568 pci_default_sub_vendor_id);
569 pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
570 pci_default_sub_device_id);
d350d97d
AL
571}
572
880345c4 573/*
43c945f1
IY
574 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
575 * [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
880345c4 576 */
6dbcb819
MA
577static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
578 unsigned int *slotp, unsigned int *funcp)
880345c4
AL
579{
580 const char *p;
581 char *e;
582 unsigned long val;
583 unsigned long dom = 0, bus = 0;
43c945f1
IY
584 unsigned int slot = 0;
585 unsigned int func = 0;
880345c4
AL
586
587 p = addr;
588 val = strtoul(p, &e, 16);
589 if (e == p)
590 return -1;
591 if (*e == ':') {
592 bus = val;
593 p = e + 1;
594 val = strtoul(p, &e, 16);
595 if (e == p)
596 return -1;
597 if (*e == ':') {
598 dom = bus;
599 bus = val;
600 p = e + 1;
601 val = strtoul(p, &e, 16);
602 if (e == p)
603 return -1;
604 }
605 }
606
880345c4
AL
607 slot = val;
608
43c945f1
IY
609 if (funcp != NULL) {
610 if (*e != '.')
611 return -1;
612
613 p = e + 1;
614 val = strtoul(p, &e, 16);
615 if (e == p)
616 return -1;
617
618 func = val;
619 }
620
621 /* if funcp == NULL func is 0 */
622 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
623 return -1;
624
880345c4
AL
625 if (*e)
626 return -1;
627
880345c4
AL
628 *domp = dom;
629 *busp = bus;
630 *slotp = slot;
43c945f1
IY
631 if (funcp != NULL)
632 *funcp = func;
880345c4
AL
633 return 0;
634}
635
6dbcb819
MA
636static PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root,
637 const char *devaddr)
5607c388
MA
638{
639 int dom, bus;
640 unsigned slot;
641
1ef7a2a2
DG
642 if (!root) {
643 fprintf(stderr, "No primary PCI bus\n");
644 return NULL;
645 }
646
b645000e
S
647 assert(!root->parent_dev);
648
5607c388
MA
649 if (!devaddr) {
650 *devfnp = -1;
1ef7a2a2 651 return pci_find_bus_nr(root, 0);
5607c388
MA
652 }
653
43c945f1 654 if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
5607c388
MA
655 return NULL;
656 }
657
1ef7a2a2
DG
658 if (dom != 0) {
659 fprintf(stderr, "No support for non-zero PCI domains\n");
660 return NULL;
661 }
662
6ff534b6 663 *devfnp = PCI_DEVFN(slot, 0);
1ef7a2a2 664 return pci_find_bus_nr(root, bus);
5607c388
MA
665}
666
bd4b65ee
MT
667static void pci_init_cmask(PCIDevice *dev)
668{
669 pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
670 pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
671 dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
672 dev->cmask[PCI_REVISION_ID] = 0xff;
673 dev->cmask[PCI_CLASS_PROG] = 0xff;
674 pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
675 dev->cmask[PCI_HEADER_TYPE] = 0xff;
676 dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
677}
678
b7ee1603
MT
679static void pci_init_wmask(PCIDevice *dev)
680{
a9f49946
IY
681 int config_size = pci_config_size(dev);
682
b7ee1603
MT
683 dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
684 dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
67a51b48 685 pci_set_word(dev->wmask + PCI_COMMAND,
a7b15a5c
MT
686 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
687 PCI_COMMAND_INTX_DISABLE);
b1aeb926
IY
688 if (dev->cap_present & QEMU_PCI_CAP_SERR) {
689 pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
690 }
3e21ffc9
IY
691
692 memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
693 config_size - PCI_CONFIG_HEADER_SIZE);
b7ee1603
MT
694}
695
89d437df
IY
696static void pci_init_w1cmask(PCIDevice *dev)
697{
698 /*
f6bdfcc9 699 * Note: It's okay to set w1cmask even for readonly bits as
89d437df
IY
700 * long as their value is hardwired to 0.
701 */
702 pci_set_word(dev->w1cmask + PCI_STATUS,
703 PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
704 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
705 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
706}
707
d5f27e88 708static void pci_init_mask_bridge(PCIDevice *d)
fb231628
IY
709{
710 /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
711 PCI_SEC_LETENCY_TIMER */
712 memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
713
714 /* base and limit */
715 d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
716 d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
717 pci_set_word(d->wmask + PCI_MEMORY_BASE,
718 PCI_MEMORY_RANGE_MASK & 0xffff);
719 pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
720 PCI_MEMORY_RANGE_MASK & 0xffff);
721 pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
722 PCI_PREF_RANGE_MASK & 0xffff);
723 pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
724 PCI_PREF_RANGE_MASK & 0xffff);
725
726 /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
727 memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
728
d5f27e88 729 /* Supported memory and i/o types */
68917102
MT
730 d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16;
731 d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16;
d5f27e88
MT
732 pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE,
733 PCI_PREF_RANGE_TYPE_64);
734 pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT,
735 PCI_PREF_RANGE_TYPE_64);
736
45eb768c
MT
737 /*
738 * TODO: Bridges default to 10-bit VGA decoding but we currently only
739 * implement 16-bit decoding (no alias support).
740 */
f6bdfcc9
MT
741 pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
742 PCI_BRIDGE_CTL_PARITY |
743 PCI_BRIDGE_CTL_SERR |
744 PCI_BRIDGE_CTL_ISA |
745 PCI_BRIDGE_CTL_VGA |
746 PCI_BRIDGE_CTL_VGA_16BIT |
747 PCI_BRIDGE_CTL_MASTER_ABORT |
748 PCI_BRIDGE_CTL_BUS_RESET |
749 PCI_BRIDGE_CTL_FAST_BACK |
750 PCI_BRIDGE_CTL_DISCARD |
751 PCI_BRIDGE_CTL_SEC_DISCARD |
f6bdfcc9
MT
752 PCI_BRIDGE_CTL_DISCARD_SERR);
753 /* Below does not do anything as we never set this bit, put here for
754 * completeness. */
755 pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
756 PCI_BRIDGE_CTL_DISCARD_STATUS);
d5f27e88 757 d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK;
15ab7a75 758 d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK;
d5f27e88
MT
759 pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE,
760 PCI_PREF_RANGE_TYPE_MASK);
15ab7a75
MT
761 pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT,
762 PCI_PREF_RANGE_TYPE_MASK);
fb231628
IY
763}
764
133e9b22 765static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp)
6eab3de1
IY
766{
767 uint8_t slot = PCI_SLOT(dev->devfn);
768 uint8_t func;
769
770 if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
771 dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
772 }
773
774 /*
b0cd712c 775 * multifunction bit is interpreted in two ways as follows.
6eab3de1
IY
776 * - all functions must set the bit to 1.
777 * Example: Intel X53
778 * - function 0 must set the bit, but the rest function (> 0)
779 * is allowed to leave the bit to 0.
780 * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
781 *
782 * So OS (at least Linux) checks the bit of only function 0,
783 * and doesn't see the bit of function > 0.
784 *
785 * The below check allows both interpretation.
786 */
787 if (PCI_FUNC(dev->devfn)) {
788 PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
789 if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
790 /* function 0 should set multifunction bit */
133e9b22
MA
791 error_setg(errp, "PCI: single function device can't be populated "
792 "in function %x.%x", slot, PCI_FUNC(dev->devfn));
793 return;
6eab3de1 794 }
133e9b22 795 return;
6eab3de1
IY
796 }
797
798 if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
133e9b22 799 return;
6eab3de1
IY
800 }
801 /* function 0 indicates single function, so function > 0 must be NULL */
802 for (func = 1; func < PCI_FUNC_MAX; ++func) {
803 if (bus->devices[PCI_DEVFN(slot, func)]) {
133e9b22
MA
804 error_setg(errp, "PCI: %x.0 indicates single function, "
805 "but %x.%x is already populated.",
806 slot, slot, func);
807 return;
6eab3de1
IY
808 }
809 }
6eab3de1
IY
810}
811
a9f49946
IY
812static void pci_config_alloc(PCIDevice *pci_dev)
813{
814 int config_size = pci_config_size(pci_dev);
815
7267c094
AL
816 pci_dev->config = g_malloc0(config_size);
817 pci_dev->cmask = g_malloc0(config_size);
818 pci_dev->wmask = g_malloc0(config_size);
819 pci_dev->w1cmask = g_malloc0(config_size);
820 pci_dev->used = g_malloc0(config_size);
a9f49946
IY
821}
822
823static void pci_config_free(PCIDevice *pci_dev)
824{
7267c094
AL
825 g_free(pci_dev->config);
826 g_free(pci_dev->cmask);
827 g_free(pci_dev->wmask);
828 g_free(pci_dev->w1cmask);
829 g_free(pci_dev->used);
a9f49946
IY
830}
831
30607764
MA
832static void do_pci_unregister_device(PCIDevice *pci_dev)
833{
834 pci_dev->bus->devices[pci_dev->devfn] = NULL;
835 pci_config_free(pci_dev);
836
837 address_space_destroy(&pci_dev->bus_master_as);
30607764
MA
838}
839
69b91039 840/* -1 for devfn means auto assign */
6b1b92d3 841static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
133e9b22
MA
842 const char *name, int devfn,
843 Error **errp)
69b91039 844{
40021f08
AL
845 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
846 PCIConfigReadFunc *config_read = pc->config_read;
847 PCIConfigWriteFunc *config_write = pc->config_write;
133e9b22 848 Error *local_err = NULL;
e00387d5 849 AddressSpace *dma_as;
3f1e1478
C
850 DeviceState *dev = DEVICE(pci_dev);
851
852 pci_dev->bus = bus;
113f89df 853
69b91039 854 if (devfn < 0) {
b47b0706 855 for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
6fa84913 856 devfn += PCI_FUNC_MAX) {
30468f78 857 if (!bus->devices[devfn])
69b91039
FB
858 goto found;
859 }
133e9b22
MA
860 error_setg(errp, "PCI: no slot/function available for %s, all in use",
861 name);
09e3acc6 862 return NULL;
69b91039 863 found: ;
07b7d053 864 } else if (bus->devices[devfn]) {
133e9b22
MA
865 error_setg(errp, "PCI: slot %d function %d not available for %s,"
866 " in use by %s",
867 PCI_SLOT(devfn), PCI_FUNC(devfn), name,
868 bus->devices[devfn]->name);
09e3acc6 869 return NULL;
3f1e1478
C
870 } else if (dev->hotplugged &&
871 pci_get_function_0(pci_dev)) {
872 error_setg(errp, "PCI: slot %d function 0 already ocuppied by %s,"
873 " new func %s cannot be exposed to guest.",
874 PCI_SLOT(devfn),
875 bus->devices[PCI_DEVFN(PCI_SLOT(devfn), 0)]->name,
876 name);
877
878 return NULL;
69b91039 879 }
e00387d5 880
efc8188e 881 pci_dev->devfn = devfn;
9eda7d37 882 dma_as = pci_device_iommu_address_space(pci_dev);
24addbc7 883
40c5dce9
PB
884 memory_region_init_alias(&pci_dev->bus_master_enable_region,
885 OBJECT(pci_dev), "bus master",
e00387d5
AK
886 dma_as->root, 0, memory_region_size(dma_as->root));
887 memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
7dca8043
AK
888 address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region,
889 name);
e00387d5 890
69b91039 891 pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
d036bb21 892 pci_dev->irq_state = 0;
a9f49946 893 pci_config_alloc(pci_dev);
fb231628 894
40021f08
AL
895 pci_config_set_vendor_id(pci_dev->config, pc->vendor_id);
896 pci_config_set_device_id(pci_dev->config, pc->device_id);
897 pci_config_set_revision(pci_dev->config, pc->revision);
898 pci_config_set_class(pci_dev->config, pc->class_id);
113f89df 899
40021f08
AL
900 if (!pc->is_bridge) {
901 if (pc->subsystem_vendor_id || pc->subsystem_id) {
113f89df 902 pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
40021f08 903 pc->subsystem_vendor_id);
113f89df 904 pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
40021f08 905 pc->subsystem_id);
113f89df
IY
906 } else {
907 pci_set_default_subsystem_id(pci_dev);
908 }
909 } else {
910 /* subsystem_vendor_id/subsystem_id are only for header type 0 */
40021f08
AL
911 assert(!pc->subsystem_vendor_id);
912 assert(!pc->subsystem_id);
fb231628 913 }
bd4b65ee 914 pci_init_cmask(pci_dev);
b7ee1603 915 pci_init_wmask(pci_dev);
89d437df 916 pci_init_w1cmask(pci_dev);
40021f08 917 if (pc->is_bridge) {
d5f27e88 918 pci_init_mask_bridge(pci_dev);
fb231628 919 }
133e9b22
MA
920 pci_init_multifunction(bus, pci_dev, &local_err);
921 if (local_err) {
922 error_propagate(errp, local_err);
30607764 923 do_pci_unregister_device(pci_dev);
6eab3de1
IY
924 return NULL;
925 }
0ac32c83
FB
926
927 if (!config_read)
928 config_read = pci_default_read_config;
929 if (!config_write)
930 config_write = pci_default_write_config;
69b91039
FB
931 pci_dev->config_read = config_read;
932 pci_dev->config_write = config_write;
30468f78 933 bus->devices[devfn] = pci_dev;
f16c4abf 934 pci_dev->version_id = 2; /* Current pci device vmstate version */
69b91039
FB
935 return pci_dev;
936}
937
5851e08c
AL
938static void pci_unregister_io_regions(PCIDevice *pci_dev)
939{
940 PCIIORegion *r;
941 int i;
942
943 for(i = 0; i < PCI_NUM_REGIONS; i++) {
944 r = &pci_dev->io_regions[i];
182f9c8a 945 if (!r->size || r->addr == PCI_BAR_UNMAPPED)
5851e08c 946 continue;
03952339 947 memory_region_del_subregion(r->address_space, r->memory);
5851e08c 948 }
e01fd687
AW
949
950 pci_unregister_vga(pci_dev);
5851e08c
AL
951}
952
133e9b22 953static void pci_qdev_unrealize(DeviceState *dev, Error **errp)
5851e08c 954{
40021f08
AL
955 PCIDevice *pci_dev = PCI_DEVICE(dev);
956 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
5851e08c
AL
957
958 pci_unregister_io_regions(pci_dev);
230741dc 959 pci_del_option_rom(pci_dev);
7cf1b0fd 960
f90c2bcd
AW
961 if (pc->exit) {
962 pc->exit(pci_dev);
963 }
5851e08c 964
925fe64a 965 do_pci_unregister_device(pci_dev);
5851e08c
AL
966}
967
e824b2cc
AK
968void pci_register_bar(PCIDevice *pci_dev, int region_num,
969 uint8_t type, MemoryRegion *memory)
69b91039
FB
970{
971 PCIIORegion *r;
d7ce493a 972 uint32_t addr;
5a9ff381 973 uint64_t wmask;
cfc0be25 974 pcibus_t size = memory_region_size(memory);
a4c20c6a 975
2bbb9c2f
IY
976 assert(region_num >= 0);
977 assert(region_num < PCI_NUM_REGIONS);
a4c20c6a
AL
978 if (size & (size-1)) {
979 fprintf(stderr, "ERROR: PCI region size must be pow2 "
89e8b13c 980 "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
a4c20c6a
AL
981 exit(1);
982 }
983
69b91039 984 r = &pci_dev->io_regions[region_num];
182f9c8a 985 r->addr = PCI_BAR_UNMAPPED;
69b91039
FB
986 r->size = size;
987 r->type = type;
79ff8cb0 988 r->memory = NULL;
b7ee1603
MT
989
990 wmask = ~(size - 1);
b3b11697 991 addr = pci_bar(pci_dev, region_num);
d7ce493a 992 if (region_num == PCI_ROM_SLOT) {
ebabb67a 993 /* ROM enable bit is writable */
5330de09 994 wmask |= PCI_ROM_ADDRESS_ENABLE;
d7ce493a 995 }
b0ff8eb2 996 pci_set_long(pci_dev->config + addr, type);
14421258
IY
997 if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
998 r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
999 pci_set_quad(pci_dev->wmask + addr, wmask);
1000 pci_set_quad(pci_dev->cmask + addr, ~0ULL);
1001 } else {
1002 pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
1003 pci_set_long(pci_dev->cmask + addr, 0xffffffff);
1004 }
79ff8cb0 1005 pci_dev->io_regions[region_num].memory = memory;
5968eca3 1006 pci_dev->io_regions[region_num].address_space
cfc0be25 1007 = type & PCI_BASE_ADDRESS_SPACE_IO
5968eca3
AK
1008 ? pci_dev->bus->address_space_io
1009 : pci_dev->bus->address_space_mem;
79ff8cb0
AK
1010}
1011
e01fd687
AW
1012static void pci_update_vga(PCIDevice *pci_dev)
1013{
1014 uint16_t cmd;
1015
1016 if (!pci_dev->has_vga) {
1017 return;
1018 }
1019
1020 cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
1021
1022 memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM],
1023 cmd & PCI_COMMAND_MEMORY);
1024 memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO],
1025 cmd & PCI_COMMAND_IO);
1026 memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI],
1027 cmd & PCI_COMMAND_IO);
1028}
1029
1030void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
1031 MemoryRegion *io_lo, MemoryRegion *io_hi)
1032{
1033 assert(!pci_dev->has_vga);
1034
1035 assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE);
1036 pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem;
1037 memory_region_add_subregion_overlap(pci_dev->bus->address_space_mem,
1038 QEMU_PCI_VGA_MEM_BASE, mem, 1);
1039
1040 assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE);
1041 pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo;
1042 memory_region_add_subregion_overlap(pci_dev->bus->address_space_io,
1043 QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1);
1044
1045 assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE);
1046 pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi;
1047 memory_region_add_subregion_overlap(pci_dev->bus->address_space_io,
1048 QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1);
1049 pci_dev->has_vga = true;
1050
1051 pci_update_vga(pci_dev);
1052}
1053
1054void pci_unregister_vga(PCIDevice *pci_dev)
1055{
1056 if (!pci_dev->has_vga) {
1057 return;
1058 }
1059
1060 memory_region_del_subregion(pci_dev->bus->address_space_mem,
1061 pci_dev->vga_regions[QEMU_PCI_VGA_MEM]);
1062 memory_region_del_subregion(pci_dev->bus->address_space_io,
1063 pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]);
1064 memory_region_del_subregion(pci_dev->bus->address_space_io,
1065 pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]);
1066 pci_dev->has_vga = false;
1067}
1068
16a96f28
AK
1069pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
1070{
1071 return pci_dev->io_regions[region_num].addr;
1072}
1073
876a350d
MT
1074static pcibus_t pci_bar_address(PCIDevice *d,
1075 int reg, uint8_t type, pcibus_t size)
1076{
1077 pcibus_t new_addr, last_addr;
1078 int bar = pci_bar(d, reg);
1079 uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
e4024630
LV
1080 Object *machine = qdev_get_machine();
1081 ObjectClass *oc = object_get_class(machine);
1082 MachineClass *mc = MACHINE_CLASS(oc);
1083 bool allow_0_address = mc->pci_allow_0_address;
876a350d
MT
1084
1085 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
1086 if (!(cmd & PCI_COMMAND_IO)) {
1087 return PCI_BAR_UNMAPPED;
1088 }
1089 new_addr = pci_get_long(d->config + bar) & ~(size - 1);
1090 last_addr = new_addr + size - 1;
9f1a029a
HP
1091 /* Check if 32 bit BAR wraps around explicitly.
1092 * TODO: make priorities correct and remove this work around.
1093 */
e4024630
LV
1094 if (last_addr <= new_addr || last_addr >= UINT32_MAX ||
1095 (!allow_0_address && new_addr == 0)) {
876a350d
MT
1096 return PCI_BAR_UNMAPPED;
1097 }
1098 return new_addr;
1099 }
1100
1101 if (!(cmd & PCI_COMMAND_MEMORY)) {
1102 return PCI_BAR_UNMAPPED;
1103 }
1104 if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1105 new_addr = pci_get_quad(d->config + bar);
1106 } else {
1107 new_addr = pci_get_long(d->config + bar);
1108 }
1109 /* the ROM slot has a specific enable bit */
1110 if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
1111 return PCI_BAR_UNMAPPED;
1112 }
1113 new_addr &= ~(size - 1);
1114 last_addr = new_addr + size - 1;
1115 /* NOTE: we do not support wrapping */
1116 /* XXX: as we cannot support really dynamic
1117 mappings, we handle specific values as invalid
1118 mappings. */
e4024630
LV
1119 if (last_addr <= new_addr || last_addr == PCI_BAR_UNMAPPED ||
1120 (!allow_0_address && new_addr == 0)) {
876a350d
MT
1121 return PCI_BAR_UNMAPPED;
1122 }
1123
1124 /* Now pcibus_t is 64bit.
1125 * Check if 32 bit BAR wraps around explicitly.
1126 * Without this, PC ide doesn't work well.
1127 * TODO: remove this work around.
1128 */
1129 if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
1130 return PCI_BAR_UNMAPPED;
1131 }
1132
1133 /*
1134 * OS is allowed to set BAR beyond its addressable
1135 * bits. For example, 32 bit OS can set 64bit bar
1136 * to >4G. Check it. TODO: we might need to support
1137 * it in the future for e.g. PAE.
1138 */
a8170e5e 1139 if (last_addr >= HWADDR_MAX) {
876a350d
MT
1140 return PCI_BAR_UNMAPPED;
1141 }
1142
1143 return new_addr;
1144}
1145
0ac32c83
FB
1146static void pci_update_mappings(PCIDevice *d)
1147{
1148 PCIIORegion *r;
876a350d 1149 int i;
7df32ca0 1150 pcibus_t new_addr;
3b46e624 1151
8a8696a3 1152 for(i = 0; i < PCI_NUM_REGIONS; i++) {
0ac32c83 1153 r = &d->io_regions[i];
a9688570
IY
1154
1155 /* this region isn't registered */
ec503442 1156 if (!r->size)
a9688570
IY
1157 continue;
1158
876a350d 1159 new_addr = pci_bar_address(d, i, r->type, r->size);
a9688570
IY
1160
1161 /* This bar isn't changed */
7df32ca0 1162 if (new_addr == r->addr)
a9688570
IY
1163 continue;
1164
1165 /* now do the real mapping */
1166 if (r->addr != PCI_BAR_UNMAPPED) {
7828d750 1167 trace_pci_update_mappings_del(d, pci_bus_num(d->bus),
7828d750 1168 PCI_SLOT(d->devfn),
0f288f85 1169 PCI_FUNC(d->devfn),
7828d750 1170 i, r->addr, r->size);
03952339 1171 memory_region_del_subregion(r->address_space, r->memory);
0ac32c83 1172 }
a9688570
IY
1173 r->addr = new_addr;
1174 if (r->addr != PCI_BAR_UNMAPPED) {
7828d750 1175 trace_pci_update_mappings_add(d, pci_bus_num(d->bus),
7828d750 1176 PCI_SLOT(d->devfn),
0f288f85 1177 PCI_FUNC(d->devfn),
7828d750 1178 i, r->addr, r->size);
8b881e77
AK
1179 memory_region_add_subregion_overlap(r->address_space,
1180 r->addr, r->memory, 1);
a9688570 1181 }
0ac32c83 1182 }
e01fd687
AW
1183
1184 pci_update_vga(d);
0ac32c83
FB
1185}
1186
a7b15a5c
MT
1187static inline int pci_irq_disabled(PCIDevice *d)
1188{
1189 return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
1190}
1191
1192/* Called after interrupt disabled field update in config space,
1193 * assert/deassert interrupts if necessary.
1194 * Gets original interrupt disable bit value (before update). */
1195static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
1196{
1197 int i, disabled = pci_irq_disabled(d);
1198 if (disabled == was_irq_disabled)
1199 return;
1200 for (i = 0; i < PCI_NUM_PINS; ++i) {
1201 int state = pci_irq_state(d, i);
1202 pci_change_irq_level(d, i, disabled ? -state : state);
1203 }
1204}
1205
5fafdf24 1206uint32_t pci_default_read_config(PCIDevice *d,
0ac32c83 1207 uint32_t address, int len)
69b91039 1208{
5029fe12 1209 uint32_t val = 0;
42e4126b 1210
5029fe12
IY
1211 memcpy(&val, d->config + address, len);
1212 return le32_to_cpu(val);
0ac32c83
FB
1213}
1214
d7efb7e0 1215void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
0ac32c83 1216{
a7b15a5c 1217 int i, was_irq_disabled = pci_irq_disabled(d);
d7efb7e0 1218 uint32_t val = val_in;
0ac32c83 1219
42e4126b 1220 for (i = 0; i < l; val >>= 8, ++i) {
91011d4f 1221 uint8_t wmask = d->wmask[addr + i];
92ba5f51
IY
1222 uint8_t w1cmask = d->w1cmask[addr + i];
1223 assert(!(wmask & w1cmask));
91011d4f 1224 d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
92ba5f51 1225 d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
0ac32c83 1226 }
260c0cd3 1227 if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
edb00035
IY
1228 ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1229 ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
260c0cd3 1230 range_covers_byte(addr, l, PCI_COMMAND))
0ac32c83 1231 pci_update_mappings(d);
a7b15a5c 1232
1c380f94 1233 if (range_covers_byte(addr, l, PCI_COMMAND)) {
a7b15a5c 1234 pci_update_irq_disabled(d, was_irq_disabled);
1c380f94
AK
1235 memory_region_set_enabled(&d->bus_master_enable_region,
1236 pci_get_word(d->config + PCI_COMMAND)
1237 & PCI_COMMAND_MASTER);
1238 }
95d65800 1239
d7efb7e0
KO
1240 msi_write_config(d, addr, val_in, l);
1241 msix_write_config(d, addr, val_in, l);
69b91039
FB
1242}
1243
502a5395
PB
1244/***********************************************************/
1245/* generic PCI irq support */
30468f78 1246
502a5395 1247/* 0 <= irq_num <= 3. level must be 0 or 1 */
d98f08f5 1248static void pci_irq_handler(void *opaque, int irq_num, int level)
69b91039 1249{
a60380a5 1250 PCIDevice *pci_dev = opaque;
80b3ada7 1251 int change;
3b46e624 1252
d036bb21 1253 change = level - pci_irq_state(pci_dev, irq_num);
80b3ada7
PB
1254 if (!change)
1255 return;
d2b59317 1256
d036bb21 1257 pci_set_irq_state(pci_dev, irq_num, level);
f9bf77dd 1258 pci_update_irq_status(pci_dev);
a7b15a5c
MT
1259 if (pci_irq_disabled(pci_dev))
1260 return;
d036bb21 1261 pci_change_irq_level(pci_dev, irq_num, change);
69b91039
FB
1262}
1263
d98f08f5
MA
1264static inline int pci_intx(PCIDevice *pci_dev)
1265{
1266 return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
1267}
1268
1269qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
1270{
1271 int intx = pci_intx(pci_dev);
1272
1273 return qemu_allocate_irq(pci_irq_handler, pci_dev, intx);
1274}
1275
1276void pci_set_irq(PCIDevice *pci_dev, int level)
1277{
1278 int intx = pci_intx(pci_dev);
1279 pci_irq_handler(pci_dev, intx, level);
1280}
1281
3afa9bb4
MT
1282/* Special hooks used by device assignment */
1283void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq)
1284{
0889464a 1285 assert(pci_bus_is_root(bus));
3afa9bb4
MT
1286 bus->route_intx_to_irq = route_intx_to_irq;
1287}
1288
1289PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin)
1290{
1291 PCIBus *bus;
1292
1293 do {
1294 bus = dev->bus;
1295 pin = bus->map_irq(dev, pin);
1296 dev = bus->parent_dev;
1297 } while (dev);
05c0621e
AW
1298
1299 if (!bus->route_intx_to_irq) {
312fd5f2 1300 error_report("PCI: Bug - unimplemented PCI INTx routing (%s)",
05c0621e
AW
1301 object_get_typename(OBJECT(bus->qbus.parent)));
1302 return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 };
1303 }
1304
3afa9bb4 1305 return bus->route_intx_to_irq(bus->irq_opaque, pin);
0ae16251
JK
1306}
1307
d6e65d54
AW
1308bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new)
1309{
1310 return old->mode != new->mode || old->irq != new->irq;
1311}
1312
0ae16251
JK
1313void pci_bus_fire_intx_routing_notifier(PCIBus *bus)
1314{
1315 PCIDevice *dev;
1316 PCIBus *sec;
1317 int i;
1318
1319 for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
1320 dev = bus->devices[i];
1321 if (dev && dev->intx_routing_notifier) {
1322 dev->intx_routing_notifier(dev);
1323 }
e5368f0d
AW
1324 }
1325
1326 QLIST_FOREACH(sec, &bus->child, sibling) {
1327 pci_bus_fire_intx_routing_notifier(sec);
0ae16251
JK
1328 }
1329}
1330
1331void pci_device_set_intx_routing_notifier(PCIDevice *dev,
1332 PCIINTxRoutingNotifier notifier)
1333{
1334 dev->intx_routing_notifier = notifier;
69b91039
FB
1335}
1336
91e56159
IY
1337/*
1338 * PCI-to-PCI bridge specification
1339 * 9.1: Interrupt routing. Table 9-1
1340 *
1341 * the PCI Express Base Specification, Revision 2.1
1342 * 2.2.8.1: INTx interrutp signaling - Rules
1343 * the Implementation Note
1344 * Table 2-20
1345 */
1346/*
1347 * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD
1348 * 0-origin unlike PCI interrupt pin register.
1349 */
1350int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
1351{
1352 return (pin + PCI_SLOT(pci_dev->devfn)) % PCI_NUM_PINS;
1353}
1354
502a5395
PB
1355/***********************************************************/
1356/* monitor info on PCI */
0ac32c83 1357
6650ee6d
PB
1358typedef struct {
1359 uint16_t class;
1360 const char *desc;
5e0259e7
GN
1361 const char *fw_name;
1362 uint16_t fw_ign_bits;
6650ee6d
PB
1363} pci_class_desc;
1364
09bc878a 1365static const pci_class_desc pci_class_descriptions[] =
6650ee6d 1366{
5e0259e7
GN
1367 { 0x0001, "VGA controller", "display"},
1368 { 0x0100, "SCSI controller", "scsi"},
1369 { 0x0101, "IDE controller", "ide"},
1370 { 0x0102, "Floppy controller", "fdc"},
1371 { 0x0103, "IPI controller", "ipi"},
1372 { 0x0104, "RAID controller", "raid"},
dcb5b19a
TS
1373 { 0x0106, "SATA controller"},
1374 { 0x0107, "SAS controller"},
1375 { 0x0180, "Storage controller"},
5e0259e7
GN
1376 { 0x0200, "Ethernet controller", "ethernet"},
1377 { 0x0201, "Token Ring controller", "token-ring"},
1378 { 0x0202, "FDDI controller", "fddi"},
1379 { 0x0203, "ATM controller", "atm"},
dcb5b19a 1380 { 0x0280, "Network controller"},
5e0259e7 1381 { 0x0300, "VGA controller", "display", 0x00ff},
dcb5b19a
TS
1382 { 0x0301, "XGA controller"},
1383 { 0x0302, "3D controller"},
1384 { 0x0380, "Display controller"},
5e0259e7
GN
1385 { 0x0400, "Video controller", "video"},
1386 { 0x0401, "Audio controller", "sound"},
dcb5b19a 1387 { 0x0402, "Phone"},
602ef4d9 1388 { 0x0403, "Audio controller", "sound"},
dcb5b19a 1389 { 0x0480, "Multimedia controller"},
5e0259e7
GN
1390 { 0x0500, "RAM controller", "memory"},
1391 { 0x0501, "Flash controller", "flash"},
dcb5b19a 1392 { 0x0580, "Memory controller"},
5e0259e7
GN
1393 { 0x0600, "Host bridge", "host"},
1394 { 0x0601, "ISA bridge", "isa"},
1395 { 0x0602, "EISA bridge", "eisa"},
1396 { 0x0603, "MC bridge", "mca"},
4c41425d 1397 { 0x0604, "PCI bridge", "pci-bridge"},
5e0259e7
GN
1398 { 0x0605, "PCMCIA bridge", "pcmcia"},
1399 { 0x0606, "NUBUS bridge", "nubus"},
1400 { 0x0607, "CARDBUS bridge", "cardbus"},
dcb5b19a
TS
1401 { 0x0608, "RACEWAY bridge"},
1402 { 0x0680, "Bridge"},
5e0259e7
GN
1403 { 0x0700, "Serial port", "serial"},
1404 { 0x0701, "Parallel port", "parallel"},
1405 { 0x0800, "Interrupt controller", "interrupt-controller"},
1406 { 0x0801, "DMA controller", "dma-controller"},
1407 { 0x0802, "Timer", "timer"},
1408 { 0x0803, "RTC", "rtc"},
1409 { 0x0900, "Keyboard", "keyboard"},
1410 { 0x0901, "Pen", "pen"},
1411 { 0x0902, "Mouse", "mouse"},
1412 { 0x0A00, "Dock station", "dock", 0x00ff},
1413 { 0x0B00, "i386 cpu", "cpu", 0x00ff},
1414 { 0x0c00, "Fireware contorller", "fireware"},
1415 { 0x0c01, "Access bus controller", "access-bus"},
1416 { 0x0c02, "SSA controller", "ssa"},
1417 { 0x0c03, "USB controller", "usb"},
1418 { 0x0c04, "Fibre channel controller", "fibre-channel"},
f7748569 1419 { 0x0c05, "SMBus"},
6650ee6d
PB
1420 { 0, NULL}
1421};
1422
163c8a59 1423static void pci_for_each_device_under_bus(PCIBus *bus,
7aa8cbb9
AP
1424 void (*fn)(PCIBus *b, PCIDevice *d,
1425 void *opaque),
1426 void *opaque)
30468f78 1427{
163c8a59
LC
1428 PCIDevice *d;
1429 int devfn;
30468f78 1430
163c8a59
LC
1431 for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1432 d = bus->devices[devfn];
1433 if (d) {
7aa8cbb9 1434 fn(bus, d, opaque);
163c8a59
LC
1435 }
1436 }
1437}
1438
1439void pci_for_each_device(PCIBus *bus, int bus_num,
7aa8cbb9
AP
1440 void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
1441 void *opaque)
163c8a59 1442{
d662210a 1443 bus = pci_find_bus_nr(bus, bus_num);
163c8a59
LC
1444
1445 if (bus) {
7aa8cbb9 1446 pci_for_each_device_under_bus(bus, fn, opaque);
163c8a59
LC
1447 }
1448}
1449
79627472 1450static const pci_class_desc *get_class_desc(int class)
163c8a59 1451{
79627472 1452 const pci_class_desc *desc;
163c8a59 1453
79627472
LC
1454 desc = pci_class_descriptions;
1455 while (desc->desc && class != desc->class) {
1456 desc++;
30468f78 1457 }
b4dccd8d 1458
79627472
LC
1459 return desc;
1460}
14421258 1461
79627472 1462static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
163c8a59 1463
79627472
LC
1464static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
1465{
1466 PciMemoryRegionList *head = NULL, *cur_item = NULL;
1467 int i;
163c8a59 1468
79627472
LC
1469 for (i = 0; i < PCI_NUM_REGIONS; i++) {
1470 const PCIIORegion *r = &dev->io_regions[i];
1471 PciMemoryRegionList *region;
1472
1473 if (!r->size) {
1474 continue;
502a5395 1475 }
163c8a59 1476
79627472
LC
1477 region = g_malloc0(sizeof(*region));
1478 region->value = g_malloc0(sizeof(*region->value));
163c8a59 1479
79627472
LC
1480 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1481 region->value->type = g_strdup("io");
1482 } else {
1483 region->value->type = g_strdup("memory");
1484 region->value->has_prefetch = true;
1485 region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
1486 region->value->has_mem_type_64 = true;
1487 region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
d5e4acf7 1488 }
163c8a59 1489
79627472
LC
1490 region->value->bar = i;
1491 region->value->address = r->addr;
1492 region->value->size = r->size;
163c8a59 1493
79627472
LC
1494 /* XXX: waiting for the qapi to support GSList */
1495 if (!cur_item) {
1496 head = cur_item = region;
1497 } else {
1498 cur_item->next = region;
1499 cur_item = region;
163c8a59 1500 }
80b3ada7 1501 }
384d8876 1502
79627472 1503 return head;
163c8a59
LC
1504}
1505
79627472
LC
1506static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
1507 int bus_num)
163c8a59 1508{
79627472 1509 PciBridgeInfo *info;
9fa02cd1 1510 PciMemoryRange *range;
163c8a59 1511
9fa02cd1 1512 info = g_new0(PciBridgeInfo, 1);
163c8a59 1513
9fa02cd1
EB
1514 info->bus = g_new0(PciBusInfo, 1);
1515 info->bus->number = dev->config[PCI_PRIMARY_BUS];
1516 info->bus->secondary = dev->config[PCI_SECONDARY_BUS];
1517 info->bus->subordinate = dev->config[PCI_SUBORDINATE_BUS];
163c8a59 1518
9fa02cd1
EB
1519 range = info->bus->io_range = g_new0(PciMemoryRange, 1);
1520 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
1521 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
163c8a59 1522
9fa02cd1
EB
1523 range = info->bus->memory_range = g_new0(PciMemoryRange, 1);
1524 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
1525 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
163c8a59 1526
9fa02cd1
EB
1527 range = info->bus->prefetchable_range = g_new0(PciMemoryRange, 1);
1528 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
1529 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
163c8a59 1530
79627472 1531 if (dev->config[PCI_SECONDARY_BUS] != 0) {
d662210a 1532 PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]);
79627472
LC
1533 if (child_bus) {
1534 info->has_devices = true;
1535 info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]);
1536 }
163c8a59
LC
1537 }
1538
79627472 1539 return info;
163c8a59
LC
1540}
1541
79627472
LC
1542static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
1543 int bus_num)
163c8a59 1544{
79627472
LC
1545 const pci_class_desc *desc;
1546 PciDeviceInfo *info;
b5937f29 1547 uint8_t type;
79627472 1548 int class;
163c8a59 1549
9fa02cd1 1550 info = g_new0(PciDeviceInfo, 1);
79627472
LC
1551 info->bus = bus_num;
1552 info->slot = PCI_SLOT(dev->devfn);
1553 info->function = PCI_FUNC(dev->devfn);
1554
9fa02cd1 1555 info->class_info = g_new0(PciDeviceClass, 1);
79627472 1556 class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
9fa02cd1 1557 info->class_info->q_class = class;
79627472
LC
1558 desc = get_class_desc(class);
1559 if (desc->desc) {
9fa02cd1
EB
1560 info->class_info->has_desc = true;
1561 info->class_info->desc = g_strdup(desc->desc);
79627472
LC
1562 }
1563
9fa02cd1
EB
1564 info->id = g_new0(PciDeviceId, 1);
1565 info->id->vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
1566 info->id->device = pci_get_word(dev->config + PCI_DEVICE_ID);
79627472
LC
1567 info->regions = qmp_query_pci_regions(dev);
1568 info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
163c8a59
LC
1569
1570 if (dev->config[PCI_INTERRUPT_PIN] != 0) {
79627472
LC
1571 info->has_irq = true;
1572 info->irq = dev->config[PCI_INTERRUPT_LINE];
163c8a59
LC
1573 }
1574
b5937f29
IY
1575 type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1576 if (type == PCI_HEADER_TYPE_BRIDGE) {
79627472
LC
1577 info->has_pci_bridge = true;
1578 info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
163c8a59
LC
1579 }
1580
79627472 1581 return info;
163c8a59
LC
1582}
1583
79627472 1584static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
384d8876 1585{
79627472 1586 PciDeviceInfoList *info, *head = NULL, *cur_item = NULL;
163c8a59 1587 PCIDevice *dev;
79627472 1588 int devfn;
163c8a59
LC
1589
1590 for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1591 dev = bus->devices[devfn];
1592 if (dev) {
79627472
LC
1593 info = g_malloc0(sizeof(*info));
1594 info->value = qmp_query_pci_device(dev, bus, bus_num);
1595
1596 /* XXX: waiting for the qapi to support GSList */
1597 if (!cur_item) {
1598 head = cur_item = info;
1599 } else {
1600 cur_item->next = info;
1601 cur_item = info;
1602 }
163c8a59 1603 }
1074df4f 1604 }
163c8a59 1605
79627472 1606 return head;
1074df4f
IY
1607}
1608
79627472 1609static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
1074df4f 1610{
79627472
LC
1611 PciInfo *info = NULL;
1612
d662210a 1613 bus = pci_find_bus_nr(bus, bus_num);
502a5395 1614 if (bus) {
79627472
LC
1615 info = g_malloc0(sizeof(*info));
1616 info->bus = bus_num;
1617 info->devices = qmp_query_pci_devices(bus, bus_num);
f2aa58c6 1618 }
163c8a59 1619
79627472 1620 return info;
f2aa58c6
FB
1621}
1622
79627472 1623PciInfoList *qmp_query_pci(Error **errp)
f2aa58c6 1624{
79627472 1625 PciInfoList *info, *head = NULL, *cur_item = NULL;
7588e2b0 1626 PCIHostState *host_bridge;
163c8a59 1627
7588e2b0 1628 QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
79627472 1629 info = g_malloc0(sizeof(*info));
cb2ed8b3
MA
1630 info->value = qmp_query_pci_bus(host_bridge->bus,
1631 pci_bus_num(host_bridge->bus));
79627472
LC
1632
1633 /* XXX: waiting for the qapi to support GSList */
1634 if (!cur_item) {
1635 head = cur_item = info;
1636 } else {
1637 cur_item->next = info;
1638 cur_item = info;
163c8a59 1639 }
e822a52a 1640 }
163c8a59 1641
79627472 1642 return head;
77d4bc34 1643}
a41b2ff2 1644
cb457d76
AL
1645static const char * const pci_nic_models[] = {
1646 "ne2k_pci",
1647 "i82551",
1648 "i82557b",
1649 "i82559er",
1650 "rtl8139",
1651 "e1000",
1652 "pcnet",
1653 "virtio",
1654 NULL
1655};
1656
9d07d757
PB
1657static const char * const pci_nic_names[] = {
1658 "ne2k_pci",
1659 "i82551",
1660 "i82557b",
1661 "i82559er",
1662 "rtl8139",
1663 "e1000",
1664 "pcnet",
53c25cea 1665 "virtio-net-pci",
cb457d76
AL
1666 NULL
1667};
1668
a41b2ff2 1669/* Initialize a PCI NIC. */
51f7cb97 1670PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
6dbcb819 1671 const char *default_model,
51f7cb97 1672 const char *default_devaddr)
a41b2ff2 1673{
5607c388 1674 const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
558ecef2 1675 Error *err = NULL;
07caea31 1676 PCIBus *bus;
5607c388 1677 PCIDevice *pci_dev;
9d07d757 1678 DeviceState *dev;
51f7cb97 1679 int devfn;
cb457d76
AL
1680 int i;
1681
51f7cb97
TH
1682 if (qemu_show_nic_models(nd->model, pci_nic_models)) {
1683 exit(0);
1684 }
1685
07caea31 1686 i = qemu_find_nic_model(nd, pci_nic_models, default_model);
51f7cb97
TH
1687 if (i < 0) {
1688 exit(1);
1689 }
07caea31 1690
29b358f9 1691 bus = pci_get_bus_devfn(&devfn, rootbus, devaddr);
07caea31 1692 if (!bus) {
1ecda02b
MA
1693 error_report("Invalid PCI device address %s for device %s",
1694 devaddr, pci_nic_names[i]);
51f7cb97 1695 exit(1);
07caea31
MA
1696 }
1697
499cf102 1698 pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
9ee05825 1699 dev = &pci_dev->qdev;
1cc33683 1700 qdev_set_nic_properties(dev, nd);
558ecef2
MA
1701
1702 object_property_set_bool(OBJECT(dev), true, "realized", &err);
1703 if (err) {
51f7cb97 1704 error_report_err(err);
558ecef2 1705 object_unparent(OBJECT(dev));
07caea31 1706 exit(1);
558ecef2 1707 }
51f7cb97
TH
1708
1709 return pci_dev;
07caea31
MA
1710}
1711
129d42fb
AJ
1712PCIDevice *pci_vga_init(PCIBus *bus)
1713{
1714 switch (vga_interface_type) {
1715 case VGA_CIRRUS:
1716 return pci_create_simple(bus, -1, "cirrus-vga");
1717 case VGA_QXL:
1718 return pci_create_simple(bus, -1, "qxl-vga");
1719 case VGA_STD:
1720 return pci_create_simple(bus, -1, "VGA");
1721 case VGA_VMWARE:
1722 return pci_create_simple(bus, -1, "vmware-svga");
a94f0c5c
GH
1723 case VGA_VIRTIO:
1724 return pci_create_simple(bus, -1, "virtio-vga");
129d42fb
AJ
1725 case VGA_NONE:
1726 default: /* Other non-PCI types. Checking for unsupported types is already
1727 done in vl.c. */
1728 return NULL;
1729 }
1730}
1731
929176c3
MT
1732/* Whether a given bus number is in range of the secondary
1733 * bus of the given bridge device. */
1734static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
1735{
1736 return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
1737 PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
09e5b819 1738 dev->config[PCI_SECONDARY_BUS] <= bus_num &&
929176c3
MT
1739 bus_num <= dev->config[PCI_SUBORDINATE_BUS];
1740}
1741
09e5b819
MA
1742/* Whether a given bus number is in a range of a root bus */
1743static bool pci_root_bus_in_range(PCIBus *bus, int bus_num)
1744{
1745 int i;
1746
1747 for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
1748 PCIDevice *dev = bus->devices[i];
1749
1750 if (dev && PCI_DEVICE_GET_CLASS(dev)->is_bridge) {
1751 if (pci_secondary_bus_in_range(dev, bus_num)) {
1752 return true;
1753 }
1754 }
1755 }
1756
1757 return false;
1758}
1759
d662210a 1760static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
3ae80618 1761{
470e6363 1762 PCIBus *sec;
3ae80618 1763
470e6363 1764 if (!bus) {
e822a52a 1765 return NULL;
470e6363 1766 }
3ae80618 1767
e822a52a
IY
1768 if (pci_bus_num(bus) == bus_num) {
1769 return bus;
1770 }
1771
929176c3 1772 /* Consider all bus numbers in range for the host pci bridge. */
0889464a 1773 if (!pci_bus_is_root(bus) &&
929176c3
MT
1774 !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
1775 return NULL;
1776 }
1777
e822a52a 1778 /* try child bus */
929176c3
MT
1779 for (; bus; bus = sec) {
1780 QLIST_FOREACH(sec, &bus->child, sibling) {
09e5b819 1781 if (pci_bus_num(sec) == bus_num) {
929176c3
MT
1782 return sec;
1783 }
09e5b819
MA
1784 /* PXB buses assumed to be children of bus 0 */
1785 if (pci_bus_is_root(sec)) {
1786 if (pci_root_bus_in_range(sec, bus_num)) {
1787 break;
1788 }
1789 } else {
1790 if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
1791 break;
1792 }
c021f8e6 1793 }
e822a52a
IY
1794 }
1795 }
1796
1797 return NULL;
3ae80618
AL
1798}
1799
eb0acfdd
MT
1800void pci_for_each_bus_depth_first(PCIBus *bus,
1801 void *(*begin)(PCIBus *bus, void *parent_state),
1802 void (*end)(PCIBus *bus, void *state),
1803 void *parent_state)
1804{
1805 PCIBus *sec;
1806 void *state;
1807
1808 if (!bus) {
1809 return;
1810 }
1811
1812 if (begin) {
1813 state = begin(bus, parent_state);
1814 } else {
1815 state = parent_state;
1816 }
1817
1818 QLIST_FOREACH(sec, &bus->child, sibling) {
1819 pci_for_each_bus_depth_first(sec, begin, end, state);
1820 }
1821
1822 if (end) {
1823 end(bus, state);
1824 }
1825}
1826
1827
5256d8bf 1828PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
3ae80618 1829{
d662210a 1830 bus = pci_find_bus_nr(bus, bus_num);
3ae80618
AL
1831
1832 if (!bus)
1833 return NULL;
1834
5256d8bf 1835 return bus->devices[devfn];
3ae80618
AL
1836}
1837
133e9b22 1838static void pci_qdev_realize(DeviceState *qdev, Error **errp)
6b1b92d3
PB
1839{
1840 PCIDevice *pci_dev = (PCIDevice *)qdev;
40021f08 1841 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
133e9b22 1842 Error *local_err = NULL;
6b1b92d3 1843 PCIBus *bus;
ab85ceb1 1844 bool is_default_rom;
6b1b92d3 1845
a9f49946 1846 /* initialize cap_present for pci_is_express() and pci_config_size() */
40021f08 1847 if (pc->is_express) {
a9f49946
IY
1848 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1849 }
1850
fef7fbc9 1851 bus = PCI_BUS(qdev_get_parent_bus(qdev));
6e008585
AL
1852 pci_dev = do_pci_register_device(pci_dev, bus,
1853 object_get_typename(OBJECT(qdev)),
133e9b22 1854 pci_dev->devfn, errp);
09e3acc6 1855 if (pci_dev == NULL)
133e9b22 1856 return;
2897ae02 1857
7ee6c1e1
MA
1858 if (pc->realize) {
1859 pc->realize(pci_dev, &local_err);
1860 if (local_err) {
1861 error_propagate(errp, local_err);
c2afc922 1862 do_pci_unregister_device(pci_dev);
133e9b22 1863 return;
c2afc922 1864 }
925fe64a 1865 }
8c52c8f3
GH
1866
1867 /* rom loading */
ab85ceb1 1868 is_default_rom = false;
40021f08
AL
1869 if (pci_dev->romfile == NULL && pc->romfile != NULL) {
1870 pci_dev->romfile = g_strdup(pc->romfile);
ab85ceb1
SW
1871 is_default_rom = true;
1872 }
178e785f 1873
133e9b22
MA
1874 pci_add_option_rom(pci_dev, is_default_rom, &local_err);
1875 if (local_err) {
1876 error_propagate(errp, local_err);
1877 pci_qdev_unrealize(DEVICE(pci_dev), NULL);
1878 return;
178e785f 1879 }
ee995ffb
GH
1880}
1881
7ee6c1e1
MA
1882static void pci_default_realize(PCIDevice *dev, Error **errp)
1883{
1884 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1885
1886 if (pc->init) {
1887 if (pc->init(dev) < 0) {
1888 error_setg(errp, "Device initialization failed");
1889 return;
1890 }
1891 }
1892}
1893
49823868
IY
1894PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
1895 const char *name)
6b1b92d3
PB
1896{
1897 DeviceState *dev;
1898
02e2da45 1899 dev = qdev_create(&bus->qbus, name);
09f1bbcd 1900 qdev_prop_set_int32(dev, "addr", devfn);
49823868 1901 qdev_prop_set_bit(dev, "multifunction", multifunction);
40021f08 1902 return PCI_DEVICE(dev);
71077c1c 1903}
6b1b92d3 1904
49823868
IY
1905PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
1906 bool multifunction,
1907 const char *name)
71077c1c 1908{
49823868 1909 PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name);
e23a1b33 1910 qdev_init_nofail(&dev->qdev);
71077c1c 1911 return dev;
6b1b92d3 1912}
6f4cbd39 1913
49823868
IY
1914PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1915{
1916 return pci_create_multifunction(bus, devfn, false, name);
1917}
1918
1919PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1920{
1921 return pci_create_simple_multifunction(bus, devfn, false, name);
1922}
1923
b56d701f 1924static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
6f4cbd39
MT
1925{
1926 int offset = PCI_CONFIG_HEADER_SIZE;
1927 int i;
b56d701f 1928 for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
6f4cbd39
MT
1929 if (pdev->used[i])
1930 offset = i + 1;
1931 else if (i - offset + 1 == size)
1932 return offset;
b56d701f 1933 }
6f4cbd39
MT
1934 return 0;
1935}
1936
1937static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1938 uint8_t *prev_p)
1939{
1940 uint8_t next, prev;
1941
1942 if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1943 return 0;
1944
1945 for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1946 prev = next + PCI_CAP_LIST_NEXT)
1947 if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1948 break;
1949
1950 if (prev_p)
1951 *prev_p = prev;
1952 return next;
1953}
1954
c9abe111
JK
1955static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
1956{
1957 uint8_t next, prev, found = 0;
1958
1959 if (!(pdev->used[offset])) {
1960 return 0;
1961 }
1962
1963 assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST);
1964
1965 for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1966 prev = next + PCI_CAP_LIST_NEXT) {
1967 if (next <= offset && next > found) {
1968 found = next;
1969 }
1970 }
1971 return found;
1972}
1973
ab85ceb1
SW
1974/* Patch the PCI vendor and device ids in a PCI rom image if necessary.
1975 This is needed for an option rom which is used for more than one device. */
1976static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)
1977{
1978 uint16_t vendor_id;
1979 uint16_t device_id;
1980 uint16_t rom_vendor_id;
1981 uint16_t rom_device_id;
1982 uint16_t rom_magic;
1983 uint16_t pcir_offset;
1984 uint8_t checksum;
1985
1986 /* Words in rom data are little endian (like in PCI configuration),
1987 so they can be read / written with pci_get_word / pci_set_word. */
1988
1989 /* Only a valid rom will be patched. */
1990 rom_magic = pci_get_word(ptr);
1991 if (rom_magic != 0xaa55) {
1992 PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
1993 return;
1994 }
1995 pcir_offset = pci_get_word(ptr + 0x18);
1996 if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
1997 PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
1998 return;
1999 }
2000
2001 vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
2002 device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
2003 rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
2004 rom_device_id = pci_get_word(ptr + pcir_offset + 6);
2005
2006 PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
2007 vendor_id, device_id, rom_vendor_id, rom_device_id);
2008
2009 checksum = ptr[6];
2010
2011 if (vendor_id != rom_vendor_id) {
2012 /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
2013 checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
2014 checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
2015 PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2016 ptr[6] = checksum;
2017 pci_set_word(ptr + pcir_offset + 4, vendor_id);
2018 }
2019
2020 if (device_id != rom_device_id) {
2021 /* Patch device id and checksum (at offset 6 for etherboot roms). */
2022 checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
2023 checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
2024 PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2025 ptr[6] = checksum;
2026 pci_set_word(ptr + pcir_offset + 6, device_id);
2027 }
2028}
2029
c2039bd0 2030/* Add an option rom for the device */
133e9b22
MA
2031static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
2032 Error **errp)
c2039bd0
AL
2033{
2034 int size;
2035 char *path;
2036 void *ptr;
1724f049 2037 char name[32];
4be9f0d1 2038 const VMStateDescription *vmsd;
c2039bd0 2039
8c52c8f3 2040 if (!pdev->romfile)
133e9b22 2041 return;
8c52c8f3 2042 if (strlen(pdev->romfile) == 0)
133e9b22 2043 return;
8c52c8f3 2044
88169ddf
GH
2045 if (!pdev->rom_bar) {
2046 /*
2047 * Load rom via fw_cfg instead of creating a rom bar,
2048 * for 0.11 compatibility.
2049 */
2050 int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
db80c7b9
MA
2051
2052 /*
2053 * Hot-plugged devices can't use the option ROM
2054 * if the rom bar is disabled.
2055 */
2056 if (DEVICE(pdev)->hotplugged) {
133e9b22
MA
2057 error_setg(errp, "Hot-plugged device without ROM bar"
2058 " can't have an option ROM");
2059 return;
db80c7b9
MA
2060 }
2061
88169ddf
GH
2062 if (class == 0x0300) {
2063 rom_add_vga(pdev->romfile);
2064 } else {
2e55e842 2065 rom_add_option(pdev->romfile, -1);
88169ddf 2066 }
133e9b22 2067 return;
88169ddf
GH
2068 }
2069
8c52c8f3 2070 path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
c2039bd0 2071 if (path == NULL) {
7267c094 2072 path = g_strdup(pdev->romfile);
c2039bd0
AL
2073 }
2074
2075 size = get_image_size(path);
8c52c8f3 2076 if (size < 0) {
133e9b22 2077 error_setg(errp, "failed to find romfile \"%s\"", pdev->romfile);
8c7f3dd0 2078 g_free(path);
133e9b22 2079 return;
8c7f3dd0 2080 } else if (size == 0) {
133e9b22 2081 error_setg(errp, "romfile \"%s\" is empty", pdev->romfile);
7267c094 2082 g_free(path);
133e9b22 2083 return;
8c52c8f3 2084 }
9bff5d81 2085 size = pow2ceil(size);
c2039bd0 2086
4be9f0d1
AL
2087 vmsd = qdev_get_vmsd(DEVICE(pdev));
2088
2089 if (vmsd) {
2090 snprintf(name, sizeof(name), "%s.rom", vmsd->name);
2091 } else {
f79f2bfc 2092 snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev)));
4be9f0d1 2093 }
14caaf7f 2094 pdev->has_rom = true;
f8ed85ac 2095 memory_region_init_ram(&pdev->rom, OBJECT(pdev), name, size, &error_fatal);
c5705a77 2096 vmstate_register_ram(&pdev->rom, &pdev->qdev);
14caaf7f 2097 ptr = memory_region_get_ram_ptr(&pdev->rom);
c2039bd0 2098 load_image(path, ptr);
7267c094 2099 g_free(path);
c2039bd0 2100
ab85ceb1
SW
2101 if (is_default_rom) {
2102 /* Only the default rom images will be patched (if needed). */
2103 pci_patch_ids(pdev, ptr, size);
2104 }
2105
e824b2cc 2106 pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
c2039bd0
AL
2107}
2108
230741dc
AW
2109static void pci_del_option_rom(PCIDevice *pdev)
2110{
14caaf7f 2111 if (!pdev->has_rom)
230741dc
AW
2112 return;
2113
c5705a77 2114 vmstate_unregister_ram(&pdev->rom, &pdev->qdev);
14caaf7f 2115 pdev->has_rom = false;
230741dc
AW
2116}
2117
ca77089d 2118/*
ca77089d
IY
2119 * if offset = 0,
2120 * Find and reserve space and add capability to the linked list
9a2a6623
CH
2121 * in pci config space
2122 */
ca77089d
IY
2123int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
2124 uint8_t offset, uint8_t size)
cd9aa33e
LE
2125{
2126 int ret;
2127 Error *local_err = NULL;
2128
2129 ret = pci_add_capability2(pdev, cap_id, offset, size, &local_err);
2130 if (local_err) {
2131 assert(ret < 0);
565f65d2 2132 error_report_err(local_err);
cd9aa33e
LE
2133 } else {
2134 /* success implies a positive offset in config space */
2135 assert(ret > 0);
2136 }
2137 return ret;
2138}
2139
2140int pci_add_capability2(PCIDevice *pdev, uint8_t cap_id,
2141 uint8_t offset, uint8_t size,
2142 Error **errp)
6f4cbd39 2143{
ca77089d 2144 uint8_t *config;
c9abe111
JK
2145 int i, overlapping_cap;
2146
ca77089d
IY
2147 if (!offset) {
2148 offset = pci_find_space(pdev, size);
2149 if (!offset) {
cd9aa33e 2150 error_setg(errp, "out of PCI config space");
ca77089d
IY
2151 return -ENOSPC;
2152 }
c9abe111
JK
2153 } else {
2154 /* Verify that capabilities don't overlap. Note: device assignment
2155 * depends on this check to verify that the device is not broken.
2156 * Should never trigger for emulated devices, but it's helpful
2157 * for debugging these. */
2158 for (i = offset; i < offset + size; i++) {
2159 overlapping_cap = pci_find_capability_at_offset(pdev, i);
2160 if (overlapping_cap) {
cd9aa33e
LE
2161 error_setg(errp, "%s:%02x:%02x.%x "
2162 "Attempt to add PCI capability %x at offset "
2163 "%x overlaps existing capability %x at offset %x",
2164 pci_root_bus_path(pdev), pci_bus_num(pdev->bus),
2165 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2166 cap_id, offset, overlapping_cap, i);
c9abe111
JK
2167 return -EINVAL;
2168 }
2169 }
ca77089d
IY
2170 }
2171
2172 config = pdev->config + offset;
6f4cbd39
MT
2173 config[PCI_CAP_LIST_ID] = cap_id;
2174 config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
2175 pdev->config[PCI_CAPABILITY_LIST] = offset;
2176 pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
e26631b7 2177 memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));
6f4cbd39
MT
2178 /* Make capability read-only by default */
2179 memset(pdev->wmask + offset, 0, size);
bd4b65ee
MT
2180 /* Check capability by default */
2181 memset(pdev->cmask + offset, 0xFF, size);
6f4cbd39
MT
2182 return offset;
2183}
2184
2185/* Unlink capability from the pci config space. */
2186void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
2187{
2188 uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
2189 if (!offset)
2190 return;
2191 pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
ebabb67a 2192 /* Make capability writable again */
6f4cbd39 2193 memset(pdev->wmask + offset, 0xff, size);
1a4f5971 2194 memset(pdev->w1cmask + offset, 0, size);
bd4b65ee
MT
2195 /* Clear cmask as device-specific registers can't be checked */
2196 memset(pdev->cmask + offset, 0, size);
e26631b7 2197 memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4));
6f4cbd39
MT
2198
2199 if (!pdev->config[PCI_CAPABILITY_LIST])
2200 pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
2201}
2202
6f4cbd39
MT
2203uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
2204{
2205 return pci_find_capability_list(pdev, cap_id, NULL);
2206}
10c4c98a
GH
2207
2208static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
2209{
2210 PCIDevice *d = (PCIDevice *)dev;
2211 const pci_class_desc *desc;
2212 char ctxt[64];
2213 PCIIORegion *r;
2214 int i, class;
2215
b0ff8eb2 2216 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
10c4c98a
GH
2217 desc = pci_class_descriptions;
2218 while (desc->desc && class != desc->class)
2219 desc++;
2220 if (desc->desc) {
2221 snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
2222 } else {
2223 snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
2224 }
2225
2226 monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
2227 "pci id %04x:%04x (sub %04x:%04x)\n",
7f5feab4 2228 indent, "", ctxt, pci_bus_num(d->bus),
e822a52a 2229 PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
b0ff8eb2
IY
2230 pci_get_word(d->config + PCI_VENDOR_ID),
2231 pci_get_word(d->config + PCI_DEVICE_ID),
2232 pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
2233 pci_get_word(d->config + PCI_SUBSYSTEM_ID));
10c4c98a
GH
2234 for (i = 0; i < PCI_NUM_REGIONS; i++) {
2235 r = &d->io_regions[i];
2236 if (!r->size)
2237 continue;
89e8b13c
IY
2238 monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
2239 " [0x%"FMT_PCIBUS"]\n",
2240 indent, "",
0392a017 2241 i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
10c4c98a
GH
2242 r->addr, r->addr + r->size - 1);
2243 }
2244}
03587182 2245
5e0259e7
GN
2246static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
2247{
2248 PCIDevice *d = (PCIDevice *)dev;
2249 const char *name = NULL;
2250 const pci_class_desc *desc = pci_class_descriptions;
2251 int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2252
2253 while (desc->desc &&
2254 (class & ~desc->fw_ign_bits) !=
2255 (desc->class & ~desc->fw_ign_bits)) {
2256 desc++;
2257 }
2258
2259 if (desc->desc) {
2260 name = desc->fw_name;
2261 }
2262
2263 if (name) {
2264 pstrcpy(buf, len, name);
2265 } else {
2266 snprintf(buf, len, "pci%04x,%04x",
2267 pci_get_word(d->config + PCI_VENDOR_ID),
2268 pci_get_word(d->config + PCI_DEVICE_ID));
2269 }
2270
2271 return buf;
2272}
2273
2274static char *pcibus_get_fw_dev_path(DeviceState *dev)
2275{
2276 PCIDevice *d = (PCIDevice *)dev;
2277 char path[50], name[33];
2278 int off;
2279
2280 off = snprintf(path, sizeof(path), "%s@%x",
2281 pci_dev_fw_name(dev, name, sizeof name),
2282 PCI_SLOT(d->devfn));
2283 if (PCI_FUNC(d->devfn))
2284 snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
a5cf8262 2285 return g_strdup(path);
5e0259e7
GN
2286}
2287
4f43c1ff
AW
2288static char *pcibus_get_dev_path(DeviceState *dev)
2289{
a6a7005d
MT
2290 PCIDevice *d = container_of(dev, PCIDevice, qdev);
2291 PCIDevice *t;
2292 int slot_depth;
2293 /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
2294 * 00 is added here to make this format compatible with
2295 * domain:Bus:Slot.Func for systems without nested PCI bridges.
2296 * Slot.Function list specifies the slot and function numbers for all
2297 * devices on the path from root to the specific device. */
568f0690
DG
2298 const char *root_bus_path;
2299 int root_bus_len;
2991181a 2300 char slot[] = ":SS.F";
2991181a 2301 int slot_len = sizeof slot - 1 /* For '\0' */;
a6a7005d
MT
2302 int path_len;
2303 char *path, *p;
2991181a 2304 int s;
a6a7005d 2305
568f0690
DG
2306 root_bus_path = pci_root_bus_path(d);
2307 root_bus_len = strlen(root_bus_path);
2308
a6a7005d
MT
2309 /* Calculate # of slots on path between device and root. */;
2310 slot_depth = 0;
2311 for (t = d; t; t = t->bus->parent_dev) {
2312 ++slot_depth;
2313 }
2314
568f0690 2315 path_len = root_bus_len + slot_len * slot_depth;
a6a7005d
MT
2316
2317 /* Allocate memory, fill in the terminating null byte. */
7267c094 2318 path = g_malloc(path_len + 1 /* For '\0' */);
a6a7005d
MT
2319 path[path_len] = '\0';
2320
568f0690 2321 memcpy(path, root_bus_path, root_bus_len);
a6a7005d
MT
2322
2323 /* Fill in slot numbers. We walk up from device to root, so need to print
2324 * them in the reverse order, last to first. */
2325 p = path + path_len;
2326 for (t = d; t; t = t->bus->parent_dev) {
2327 p -= slot_len;
2991181a 2328 s = snprintf(slot, sizeof slot, ":%02x.%x",
4c900518 2329 PCI_SLOT(t->devfn), PCI_FUNC(t->devfn));
2991181a
MT
2330 assert(s == slot_len);
2331 memcpy(p, slot, slot_len);
a6a7005d
MT
2332 }
2333
2334 return path;
4f43c1ff
AW
2335}
2336
f3006dd1
IY
2337static int pci_qdev_find_recursive(PCIBus *bus,
2338 const char *id, PCIDevice **pdev)
2339{
2340 DeviceState *qdev = qdev_find_recursive(&bus->qbus, id);
2341 if (!qdev) {
2342 return -ENODEV;
2343 }
2344
2345 /* roughly check if given qdev is pci device */
4be9f0d1 2346 if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) {
40021f08 2347 *pdev = PCI_DEVICE(qdev);
f3006dd1
IY
2348 return 0;
2349 }
2350 return -EINVAL;
2351}
2352
2353int pci_qdev_find_device(const char *id, PCIDevice **pdev)
2354{
7588e2b0 2355 PCIHostState *host_bridge;
f3006dd1
IY
2356 int rc = -ENODEV;
2357
7588e2b0
DG
2358 QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
2359 int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev);
f3006dd1
IY
2360 if (!tmp) {
2361 rc = 0;
2362 break;
2363 }
2364 if (tmp != -ENODEV) {
2365 rc = tmp;
2366 }
2367 }
2368
2369 return rc;
2370}
f5e6fed8
AK
2371
2372MemoryRegion *pci_address_space(PCIDevice *dev)
2373{
2374 return dev->bus->address_space_mem;
2375}
e11d6439
RH
2376
2377MemoryRegion *pci_address_space_io(PCIDevice *dev)
2378{
2379 return dev->bus->address_space_io;
2380}
40021f08 2381
39bffca2
AL
2382static void pci_device_class_init(ObjectClass *klass, void *data)
2383{
2384 DeviceClass *k = DEVICE_CLASS(klass);
7ee6c1e1
MA
2385 PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
2386
133e9b22
MA
2387 k->realize = pci_qdev_realize;
2388 k->unrealize = pci_qdev_unrealize;
0d936928 2389 k->bus_type = TYPE_PCI_BUS;
bce54474 2390 k->props = pci_props;
7ee6c1e1 2391 pc->realize = pci_default_realize;
39bffca2
AL
2392}
2393
9eda7d37
AK
2394AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
2395{
2396 PCIBus *bus = PCI_BUS(dev->bus);
5af2ae23 2397 PCIBus *iommu_bus = bus;
9eda7d37 2398
5af2ae23
BH
2399 while(iommu_bus && !iommu_bus->iommu_fn && iommu_bus->parent_dev) {
2400 iommu_bus = PCI_BUS(iommu_bus->parent_dev->bus);
9eda7d37 2401 }
5af2ae23
BH
2402 if (iommu_bus && iommu_bus->iommu_fn) {
2403 return iommu_bus->iommu_fn(bus, iommu_bus->iommu_opaque, dev->devfn);
9eda7d37 2404 }
9eda7d37
AK
2405 return &address_space_memory;
2406}
2407
e00387d5 2408void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
5fa45de5 2409{
e00387d5
AK
2410 bus->iommu_fn = fn;
2411 bus->iommu_opaque = opaque;
5fa45de5
DG
2412}
2413
43864069
MT
2414static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque)
2415{
2416 Range *range = opaque;
2417 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
2418 uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND);
77d6f4ea 2419 int i;
43864069
MT
2420
2421 if (!(cmd & PCI_COMMAND_MEMORY)) {
2422 return;
2423 }
2424
2425 if (pc->is_bridge) {
2426 pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
2427 pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
2428
2429 base = MAX(base, 0x1ULL << 32);
2430
2431 if (limit >= base) {
2432 Range pref_range;
2433 pref_range.begin = base;
2434 pref_range.end = limit + 1;
2435 range_extend(range, &pref_range);
2436 }
2437 }
77d6f4ea
MT
2438 for (i = 0; i < PCI_NUM_REGIONS; ++i) {
2439 PCIIORegion *r = &dev->io_regions[i];
43864069
MT
2440 Range region_range;
2441
77d6f4ea
MT
2442 if (!r->size ||
2443 (r->type & PCI_BASE_ADDRESS_SPACE_IO) ||
2444 !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
2445 continue;
2446 }
2447 region_range.begin = pci_bar_address(dev, i, r->type, r->size);
2448 region_range.end = region_range.begin + r->size;
2449
2450 if (region_range.begin == PCI_BAR_UNMAPPED) {
43864069
MT
2451 continue;
2452 }
43864069
MT
2453
2454 region_range.begin = MAX(region_range.begin, 0x1ULL << 32);
2455
2456 if (region_range.end - 1 >= region_range.begin) {
2457 range_extend(range, &region_range);
2458 }
2459 }
2460}
2461
2462void pci_bus_get_w64_range(PCIBus *bus, Range *range)
2463{
2464 range->begin = range->end = 0;
2465 pci_for_each_device_under_bus(bus, pci_dev_get_w64, range);
2466}
2467
3f1e1478
C
2468static bool pcie_has_upstream_port(PCIDevice *dev)
2469{
2470 PCIDevice *parent_dev = pci_bridge_get_device(dev->bus);
2471
2472 /* Device associated with an upstream port.
2473 * As there are several types of these, it's easier to check the
2474 * parent device: upstream ports are always connected to
2475 * root or downstream ports.
2476 */
2477 return parent_dev &&
2478 pci_is_express(parent_dev) &&
2479 parent_dev->exp.exp_cap &&
2480 (pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_ROOT_PORT ||
2481 pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_DOWNSTREAM);
2482}
2483
2484PCIDevice *pci_get_function_0(PCIDevice *pci_dev)
2485{
2486 if(pcie_has_upstream_port(pci_dev)) {
2487 /* With an upstream PCIe port, we only support 1 device at slot 0 */
2488 return pci_dev->bus->devices[0];
2489 } else {
2490 /* Other bus types might support multiple devices at slots 0-31 */
2491 return pci_dev->bus->devices[PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 0)];
2492 }
2493}
2494
8c43a6f0 2495static const TypeInfo pci_device_type_info = {
40021f08
AL
2496 .name = TYPE_PCI_DEVICE,
2497 .parent = TYPE_DEVICE,
2498 .instance_size = sizeof(PCIDevice),
2499 .abstract = true,
2500 .class_size = sizeof(PCIDeviceClass),
39bffca2 2501 .class_init = pci_device_class_init,
40021f08
AL
2502};
2503
83f7d43a 2504static void pci_register_types(void)
40021f08 2505{
0d936928 2506 type_register_static(&pci_bus_info);
3a861c46 2507 type_register_static(&pcie_bus_info);
40021f08
AL
2508 type_register_static(&pci_device_type_info);
2509}
2510
83f7d43a 2511type_init(pci_register_types)
This page took 1.326224 seconds and 4 git commands to generate.