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