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