Statistics
| Branch: | Revision:

root / hw / pci.c @ 99443c21

History | View | Annotate | Download (54 kB)

1
/*
2
 * QEMU PCI bus manager
3
 *
4
 * Copyright (c) 2004 Fabrice Bellard
5
 *
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
 */
24
#include "hw.h"
25
#include "pci.h"
26
#include "pci_bridge.h"
27
#include "pci_internals.h"
28
#include "msix.h"
29
#include "msi.h"
30
#include "monitor.h"
31
#include "net.h"
32
#include "sysemu.h"
33
#include "loader.h"
34
#include "qemu-objects.h"
35

    
36
//#define DEBUG_PCI
37
#ifdef DEBUG_PCI
38
# define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
39
#else
40
# define PCI_DPRINTF(format, ...)       do { } while (0)
41
#endif
42

    
43
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
44
static char *pcibus_get_dev_path(DeviceState *dev);
45

    
46
struct BusInfo pci_bus_info = {
47
    .name       = "PCI",
48
    .size       = sizeof(PCIBus),
49
    .print_dev  = pcibus_dev_print,
50
    .get_dev_path = pcibus_get_dev_path,
51
    .props      = (Property[]) {
52
        DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
53
        DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
54
        DEFINE_PROP_UINT32("rombar",  PCIDevice, rom_bar, 1),
55
        DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
56
                        QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
57
        DEFINE_PROP_END_OF_LIST()
58
    }
59
};
60

    
61
static void pci_update_mappings(PCIDevice *d);
62
static void pci_set_irq(void *opaque, int irq_num, int level);
63
static int pci_add_option_rom(PCIDevice *pdev);
64
static void pci_del_option_rom(PCIDevice *pdev);
65

    
66
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
67
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
68

    
69
struct PCIHostBus {
70
    int domain;
71
    struct PCIBus *bus;
72
    QLIST_ENTRY(PCIHostBus) next;
73
};
74
static QLIST_HEAD(, PCIHostBus) host_buses;
75

    
76
static const VMStateDescription vmstate_pcibus = {
77
    .name = "PCIBUS",
78
    .version_id = 1,
79
    .minimum_version_id = 1,
80
    .minimum_version_id_old = 1,
81
    .fields      = (VMStateField []) {
82
        VMSTATE_INT32_EQUAL(nirq, PCIBus),
83
        VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
84
        VMSTATE_END_OF_LIST()
85
    }
86
};
87

    
88
static int pci_bar(PCIDevice *d, int reg)
89
{
90
    uint8_t type;
91

    
92
    if (reg != PCI_ROM_SLOT)
93
        return PCI_BASE_ADDRESS_0 + reg * 4;
94

    
95
    type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
96
    return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
97
}
98

    
99
static inline int pci_irq_state(PCIDevice *d, int irq_num)
100
{
101
        return (d->irq_state >> irq_num) & 0x1;
102
}
103

    
104
static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
105
{
106
        d->irq_state &= ~(0x1 << irq_num);
107
        d->irq_state |= level << irq_num;
108
}
109

    
110
static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
111
{
112
    PCIBus *bus;
113
    for (;;) {
114
        bus = pci_dev->bus;
115
        irq_num = bus->map_irq(pci_dev, irq_num);
116
        if (bus->set_irq)
117
            break;
118
        pci_dev = bus->parent_dev;
119
    }
120
    bus->irq_count[irq_num] += change;
121
    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
122
}
123

    
124
/* Update interrupt status bit in config space on interrupt
125
 * state change. */
126
static void pci_update_irq_status(PCIDevice *dev)
127
{
128
    if (dev->irq_state) {
129
        dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
130
    } else {
131
        dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
132
    }
133
}
134

    
135
static void pci_device_reset(PCIDevice *dev)
136
{
137
    int r;
138

    
139
    dev->irq_state = 0;
140
    pci_update_irq_status(dev);
141
    /* Clear all writeable bits */
142
    pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
143
                                 pci_get_word(dev->wmask + PCI_COMMAND));
144
    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
145
    dev->config[PCI_INTERRUPT_LINE] = 0x0;
146
    for (r = 0; r < PCI_NUM_REGIONS; ++r) {
147
        PCIIORegion *region = &dev->io_regions[r];
148
        if (!region->size) {
149
            continue;
150
        }
151

    
152
        if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
153
            region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
154
            pci_set_quad(dev->config + pci_bar(dev, r), region->type);
155
        } else {
156
            pci_set_long(dev->config + pci_bar(dev, r), region->type);
157
        }
158
    }
159
    pci_update_mappings(dev);
160
}
161

    
162
static void pci_bus_reset(void *opaque)
163
{
164
    PCIBus *bus = opaque;
165
    int i;
166

    
167
    for (i = 0; i < bus->nirq; i++) {
168
        bus->irq_count[i] = 0;
169
    }
170
    for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
171
        if (bus->devices[i]) {
172
            pci_device_reset(bus->devices[i]);
173
        }
174
    }
175
}
176

    
177
static void pci_host_bus_register(int domain, PCIBus *bus)
178
{
179
    struct PCIHostBus *host;
180
    host = qemu_mallocz(sizeof(*host));
181
    host->domain = domain;
182
    host->bus = bus;
183
    QLIST_INSERT_HEAD(&host_buses, host, next);
184
}
185

    
186
PCIBus *pci_find_root_bus(int domain)
187
{
188
    struct PCIHostBus *host;
189

    
190
    QLIST_FOREACH(host, &host_buses, next) {
191
        if (host->domain == domain) {
192
            return host->bus;
193
        }
194
    }
195

    
196
    return NULL;
197
}
198

    
199
int pci_find_domain(const PCIBus *bus)
200
{
201
    PCIDevice *d;
202
    struct PCIHostBus *host;
203

    
204
    /* obtain root bus */
205
    while ((d = bus->parent_dev) != NULL) {
206
        bus = d->bus;
207
    }
208

    
209
    QLIST_FOREACH(host, &host_buses, next) {
210
        if (host->bus == bus) {
211
            return host->domain;
212
        }
213
    }
214

    
215
    abort();    /* should not be reached */
216
    return -1;
217
}
218

    
219
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
220
                         const char *name, int devfn_min)
221
{
222
    qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
223
    assert(PCI_FUNC(devfn_min) == 0);
224
    bus->devfn_min = devfn_min;
225

    
226
    /* host bridge */
227
    QLIST_INIT(&bus->child);
228
    pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
229

    
230
    vmstate_register(NULL, -1, &vmstate_pcibus, bus);
231
    qemu_register_reset(pci_bus_reset, bus);
232
}
233

    
234
PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
235
{
236
    PCIBus *bus;
237

    
238
    bus = qemu_mallocz(sizeof(*bus));
239
    bus->qbus.qdev_allocated = 1;
240
    pci_bus_new_inplace(bus, parent, name, devfn_min);
241
    return bus;
242
}
243

    
244
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
245
                  void *irq_opaque, int nirq)
246
{
247
    bus->set_irq = set_irq;
248
    bus->map_irq = map_irq;
249
    bus->irq_opaque = irq_opaque;
250
    bus->nirq = nirq;
251
    bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
252
}
253

    
254
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev)
255
{
256
    bus->qbus.allow_hotplug = 1;
257
    bus->hotplug = hotplug;
258
    bus->hotplug_qdev = qdev;
259
}
260

    
261
void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
262
{
263
    bus->mem_base = base;
264
}
265

    
266
PCIBus *pci_register_bus(DeviceState *parent, const char *name,
267
                         pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
268
                         void *irq_opaque, int devfn_min, int nirq)
269
{
270
    PCIBus *bus;
271

    
272
    bus = pci_bus_new(parent, name, devfn_min);
273
    pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
274
    return bus;
275
}
276

    
277
int pci_bus_num(PCIBus *s)
278
{
279
    if (!s->parent_dev)
280
        return 0;       /* pci host bridge */
281
    return s->parent_dev->config[PCI_SECONDARY_BUS];
282
}
283

    
284
static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
285
{
286
    PCIDevice *s = container_of(pv, PCIDevice, config);
287
    uint8_t *config;
288
    int i;
289

    
290
    assert(size == pci_config_size(s));
291
    config = qemu_malloc(size);
292

    
293
    qemu_get_buffer(f, config, size);
294
    for (i = 0; i < size; ++i) {
295
        if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
296
            qemu_free(config);
297
            return -EINVAL;
298
        }
299
    }
300
    memcpy(s->config, config, size);
301

    
302
    pci_update_mappings(s);
303

    
304
    qemu_free(config);
305
    return 0;
306
}
307

    
308
/* just put buffer */
309
static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
310
{
311
    const uint8_t **v = pv;
312
    assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
313
    qemu_put_buffer(f, *v, size);
314
}
315

    
316
static VMStateInfo vmstate_info_pci_config = {
317
    .name = "pci config",
318
    .get  = get_pci_config_device,
319
    .put  = put_pci_config_device,
320
};
321

    
322
static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
323
{
324
    PCIDevice *s = container_of(pv, PCIDevice, irq_state);
325
    uint32_t irq_state[PCI_NUM_PINS];
326
    int i;
327
    for (i = 0; i < PCI_NUM_PINS; ++i) {
328
        irq_state[i] = qemu_get_be32(f);
329
        if (irq_state[i] != 0x1 && irq_state[i] != 0) {
330
            fprintf(stderr, "irq state %d: must be 0 or 1.\n",
331
                    irq_state[i]);
332
            return -EINVAL;
333
        }
334
    }
335

    
336
    for (i = 0; i < PCI_NUM_PINS; ++i) {
337
        pci_set_irq_state(s, i, irq_state[i]);
338
    }
339

    
340
    return 0;
341
}
342

    
343
static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
344
{
345
    int i;
346
    PCIDevice *s = container_of(pv, PCIDevice, irq_state);
347

    
348
    for (i = 0; i < PCI_NUM_PINS; ++i) {
349
        qemu_put_be32(f, pci_irq_state(s, i));
350
    }
351
}
352

    
353
static VMStateInfo vmstate_info_pci_irq_state = {
354
    .name = "pci irq state",
355
    .get  = get_pci_irq_state,
356
    .put  = put_pci_irq_state,
357
};
358

    
359
const VMStateDescription vmstate_pci_device = {
360
    .name = "PCIDevice",
361
    .version_id = 2,
362
    .minimum_version_id = 1,
363
    .minimum_version_id_old = 1,
364
    .fields      = (VMStateField []) {
365
        VMSTATE_INT32_LE(version_id, PCIDevice),
366
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
367
                                   vmstate_info_pci_config,
368
                                   PCI_CONFIG_SPACE_SIZE),
369
        VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
370
                                   vmstate_info_pci_irq_state,
371
                                   PCI_NUM_PINS * sizeof(int32_t)),
372
        VMSTATE_END_OF_LIST()
373
    }
374
};
375

    
376
const VMStateDescription vmstate_pcie_device = {
377
    .name = "PCIDevice",
378
    .version_id = 2,
379
    .minimum_version_id = 1,
380
    .minimum_version_id_old = 1,
381
    .fields      = (VMStateField []) {
382
        VMSTATE_INT32_LE(version_id, PCIDevice),
383
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
384
                                   vmstate_info_pci_config,
385
                                   PCIE_CONFIG_SPACE_SIZE),
386
        VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
387
                                   vmstate_info_pci_irq_state,
388
                                   PCI_NUM_PINS * sizeof(int32_t)),
389
        VMSTATE_END_OF_LIST()
390
    }
391
};
392

    
393
static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
394
{
395
    return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
396
}
397

    
398
void pci_device_save(PCIDevice *s, QEMUFile *f)
399
{
400
    /* Clear interrupt status bit: it is implicit
401
     * in irq_state which we are saving.
402
     * This makes us compatible with old devices
403
     * which never set or clear this bit. */
404
    s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
405
    vmstate_save_state(f, pci_get_vmstate(s), s);
406
    /* Restore the interrupt status bit. */
407
    pci_update_irq_status(s);
408
}
409

    
410
int pci_device_load(PCIDevice *s, QEMUFile *f)
411
{
412
    int ret;
413
    ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
414
    /* Restore the interrupt status bit. */
415
    pci_update_irq_status(s);
416
    return ret;
417
}
418

    
419
static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
420
{
421
    pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
422
                 pci_default_sub_vendor_id);
423
    pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
424
                 pci_default_sub_device_id);
425
}
426

    
427
/*
428
 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
429
 *       [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
430
 */
431
int pci_parse_devaddr(const char *addr, int *domp, int *busp,
432
                      unsigned int *slotp, unsigned int *funcp)
433
{
434
    const char *p;
435
    char *e;
436
    unsigned long val;
437
    unsigned long dom = 0, bus = 0;
438
    unsigned int slot = 0;
439
    unsigned int func = 0;
440

    
441
    p = addr;
442
    val = strtoul(p, &e, 16);
443
    if (e == p)
444
        return -1;
445
    if (*e == ':') {
446
        bus = val;
447
        p = e + 1;
448
        val = strtoul(p, &e, 16);
449
        if (e == p)
450
            return -1;
451
        if (*e == ':') {
452
            dom = bus;
453
            bus = val;
454
            p = e + 1;
455
            val = strtoul(p, &e, 16);
456
            if (e == p)
457
                return -1;
458
        }
459
    }
460

    
461
    slot = val;
462

    
463
    if (funcp != NULL) {
464
        if (*e != '.')
465
            return -1;
466

    
467
        p = e + 1;
468
        val = strtoul(p, &e, 16);
469
        if (e == p)
470
            return -1;
471

    
472
        func = val;
473
    }
474

    
475
    /* if funcp == NULL func is 0 */
476
    if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
477
        return -1;
478

    
479
    if (*e)
480
        return -1;
481

    
482
    /* Note: QEMU doesn't implement domains other than 0 */
483
    if (!pci_find_bus(pci_find_root_bus(dom), bus))
484
        return -1;
485

    
486
    *domp = dom;
487
    *busp = bus;
488
    *slotp = slot;
489
    if (funcp != NULL)
490
        *funcp = func;
491
    return 0;
492
}
493

    
494
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
495
                     unsigned *slotp)
496
{
497
    /* strip legacy tag */
498
    if (!strncmp(addr, "pci_addr=", 9)) {
499
        addr += 9;
500
    }
501
    if (pci_parse_devaddr(addr, domp, busp, slotp, NULL)) {
502
        monitor_printf(mon, "Invalid pci address\n");
503
        return -1;
504
    }
505
    return 0;
506
}
507

    
508
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
509
{
510
    int dom, bus;
511
    unsigned slot;
512

    
513
    if (!devaddr) {
514
        *devfnp = -1;
515
        return pci_find_bus(pci_find_root_bus(0), 0);
516
    }
517

    
518
    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
519
        return NULL;
520
    }
521

    
522
    *devfnp = slot << 3;
523
    return pci_find_bus(pci_find_root_bus(dom), bus);
524
}
525

    
526
static void pci_init_cmask(PCIDevice *dev)
527
{
528
    pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
529
    pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
530
    dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
531
    dev->cmask[PCI_REVISION_ID] = 0xff;
532
    dev->cmask[PCI_CLASS_PROG] = 0xff;
533
    pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
534
    dev->cmask[PCI_HEADER_TYPE] = 0xff;
535
    dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
536
}
537

    
538
static void pci_init_wmask(PCIDevice *dev)
539
{
540
    int config_size = pci_config_size(dev);
541

    
542
    dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
543
    dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
544
    pci_set_word(dev->wmask + PCI_COMMAND,
545
                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
546
                 PCI_COMMAND_INTX_DISABLE);
547

    
548
    memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
549
           config_size - PCI_CONFIG_HEADER_SIZE);
550
}
551

    
552
static void pci_init_wmask_bridge(PCIDevice *d)
553
{
554
    /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
555
       PCI_SEC_LETENCY_TIMER */
556
    memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
557

    
558
    /* base and limit */
559
    d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
560
    d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
561
    pci_set_word(d->wmask + PCI_MEMORY_BASE,
562
                 PCI_MEMORY_RANGE_MASK & 0xffff);
563
    pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
564
                 PCI_MEMORY_RANGE_MASK & 0xffff);
565
    pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
566
                 PCI_PREF_RANGE_MASK & 0xffff);
567
    pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
568
                 PCI_PREF_RANGE_MASK & 0xffff);
569

    
570
    /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
571
    memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
572

    
573
    pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
574
}
575

    
576
static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev)
577
{
578
    uint8_t slot = PCI_SLOT(dev->devfn);
579
    uint8_t func;
580

    
581
    if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
582
        dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
583
    }
584

    
585
    /*
586
     * multifuction bit is interpreted in two ways as follows.
587
     *   - all functions must set the bit to 1.
588
     *     Example: Intel X53
589
     *   - function 0 must set the bit, but the rest function (> 0)
590
     *     is allowed to leave the bit to 0.
591
     *     Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
592
     *
593
     * So OS (at least Linux) checks the bit of only function 0,
594
     * and doesn't see the bit of function > 0.
595
     *
596
     * The below check allows both interpretation.
597
     */
598
    if (PCI_FUNC(dev->devfn)) {
599
        PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
600
        if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
601
            /* function 0 should set multifunction bit */
602
            error_report("PCI: single function device can't be populated "
603
                         "in function %x.%x", slot, PCI_FUNC(dev->devfn));
604
            return -1;
605
        }
606
        return 0;
607
    }
608

    
609
    if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
610
        return 0;
611
    }
612
    /* function 0 indicates single function, so function > 0 must be NULL */
613
    for (func = 1; func < PCI_FUNC_MAX; ++func) {
614
        if (bus->devices[PCI_DEVFN(slot, func)]) {
615
            error_report("PCI: %x.0 indicates single function, "
616
                         "but %x.%x is already populated.",
617
                         slot, slot, func);
618
            return -1;
619
        }
620
    }
621
    return 0;
622
}
623

    
624
static void pci_config_alloc(PCIDevice *pci_dev)
625
{
626
    int config_size = pci_config_size(pci_dev);
627

    
628
    pci_dev->config = qemu_mallocz(config_size);
629
    pci_dev->cmask = qemu_mallocz(config_size);
630
    pci_dev->wmask = qemu_mallocz(config_size);
631
    pci_dev->w1cmask = qemu_mallocz(config_size);
632
    pci_dev->used = qemu_mallocz(config_size);
633
}
634

    
635
static void pci_config_free(PCIDevice *pci_dev)
636
{
637
    qemu_free(pci_dev->config);
638
    qemu_free(pci_dev->cmask);
639
    qemu_free(pci_dev->wmask);
640
    qemu_free(pci_dev->w1cmask);
641
    qemu_free(pci_dev->used);
642
}
643

    
644
/* -1 for devfn means auto assign */
645
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
646
                                         const char *name, int devfn,
647
                                         PCIConfigReadFunc *config_read,
648
                                         PCIConfigWriteFunc *config_write,
649
                                         bool is_bridge)
650
{
651
    if (devfn < 0) {
652
        for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
653
            devfn += PCI_FUNC_MAX) {
654
            if (!bus->devices[devfn])
655
                goto found;
656
        }
657
        error_report("PCI: no slot/function available for %s, all in use", name);
658
        return NULL;
659
    found: ;
660
    } else if (bus->devices[devfn]) {
661
        error_report("PCI: slot %d function %d not available for %s, in use by %s",
662
                     PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name);
663
        return NULL;
664
    }
665
    pci_dev->bus = bus;
666
    pci_dev->devfn = devfn;
667
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
668
    pci_dev->irq_state = 0;
669
    pci_config_alloc(pci_dev);
670

    
671
    if (!is_bridge) {
672
        pci_set_default_subsystem_id(pci_dev);
673
    }
674
    pci_init_cmask(pci_dev);
675
    pci_init_wmask(pci_dev);
676
    if (is_bridge) {
677
        pci_init_wmask_bridge(pci_dev);
678
    }
679
    if (pci_init_multifunction(bus, pci_dev)) {
680
        pci_config_free(pci_dev);
681
        return NULL;
682
    }
683

    
684
    if (!config_read)
685
        config_read = pci_default_read_config;
686
    if (!config_write)
687
        config_write = pci_default_write_config;
688
    pci_dev->config_read = config_read;
689
    pci_dev->config_write = config_write;
690
    bus->devices[devfn] = pci_dev;
691
    pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
692
    pci_dev->version_id = 2; /* Current pci device vmstate version */
693
    return pci_dev;
694
}
695

    
696
static void do_pci_unregister_device(PCIDevice *pci_dev)
697
{
698
    qemu_free_irqs(pci_dev->irq);
699
    pci_dev->bus->devices[pci_dev->devfn] = NULL;
700
    pci_config_free(pci_dev);
701
}
702

    
703
PCIDevice *pci_register_device(PCIBus *bus, const char *name,
704
                               int instance_size, int devfn,
705
                               PCIConfigReadFunc *config_read,
706
                               PCIConfigWriteFunc *config_write)
707
{
708
    PCIDevice *pci_dev;
709

    
710
    pci_dev = qemu_mallocz(instance_size);
711
    pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
712
                                     config_read, config_write,
713
                                     PCI_HEADER_TYPE_NORMAL);
714
    if (pci_dev == NULL) {
715
        hw_error("PCI: can't register device\n");
716
    }
717
    return pci_dev;
718
}
719

    
720
static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
721
                                          target_phys_addr_t addr)
722
{
723
    return addr + bus->mem_base;
724
}
725

    
726
static void pci_unregister_io_regions(PCIDevice *pci_dev)
727
{
728
    PCIIORegion *r;
729
    int i;
730

    
731
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
732
        r = &pci_dev->io_regions[i];
733
        if (!r->size || r->addr == PCI_BAR_UNMAPPED)
734
            continue;
735
        if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
736
            isa_unassign_ioport(r->addr, r->filtered_size);
737
        } else {
738
            cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus,
739
                                                         r->addr),
740
                                         r->filtered_size,
741
                                         IO_MEM_UNASSIGNED);
742
        }
743
    }
744
}
745

    
746
static int pci_unregister_device(DeviceState *dev)
747
{
748
    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
749
    PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
750
    int ret = 0;
751

    
752
    if (info->exit)
753
        ret = info->exit(pci_dev);
754
    if (ret)
755
        return ret;
756

    
757
    pci_unregister_io_regions(pci_dev);
758
    pci_del_option_rom(pci_dev);
759
    do_pci_unregister_device(pci_dev);
760
    return 0;
761
}
762

    
763
void pci_register_bar(PCIDevice *pci_dev, int region_num,
764
                            pcibus_t size, uint8_t type,
765
                            PCIMapIORegionFunc *map_func)
766
{
767
    PCIIORegion *r;
768
    uint32_t addr;
769
    uint64_t wmask;
770

    
771
    assert(region_num >= 0);
772
    assert(region_num < PCI_NUM_REGIONS);
773
    if (size & (size-1)) {
774
        fprintf(stderr, "ERROR: PCI region size must be pow2 "
775
                    "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
776
        exit(1);
777
    }
778

    
779
    r = &pci_dev->io_regions[region_num];
780
    r->addr = PCI_BAR_UNMAPPED;
781
    r->size = size;
782
    r->filtered_size = size;
783
    r->type = type;
784
    r->map_func = map_func;
785

    
786
    wmask = ~(size - 1);
787
    addr = pci_bar(pci_dev, region_num);
788
    if (region_num == PCI_ROM_SLOT) {
789
        /* ROM enable bit is writeable */
790
        wmask |= PCI_ROM_ADDRESS_ENABLE;
791
    }
792
    pci_set_long(pci_dev->config + addr, type);
793
    if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
794
        r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
795
        pci_set_quad(pci_dev->wmask + addr, wmask);
796
        pci_set_quad(pci_dev->cmask + addr, ~0ULL);
797
    } else {
798
        pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
799
        pci_set_long(pci_dev->cmask + addr, 0xffffffff);
800
    }
801
}
802

    
803
static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size,
804
                              uint8_t type)
805
{
806
    pcibus_t base = *addr;
807
    pcibus_t limit = *addr + *size - 1;
808
    PCIDevice *br;
809

    
810
    for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
811
        uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
812

    
813
        if (type & PCI_BASE_ADDRESS_SPACE_IO) {
814
            if (!(cmd & PCI_COMMAND_IO)) {
815
                goto no_map;
816
            }
817
        } else {
818
            if (!(cmd & PCI_COMMAND_MEMORY)) {
819
                goto no_map;
820
            }
821
        }
822

    
823
        base = MAX(base, pci_bridge_get_base(br, type));
824
        limit = MIN(limit, pci_bridge_get_limit(br, type));
825
    }
826

    
827
    if (base > limit) {
828
        goto no_map;
829
    }
830
    *addr = base;
831
    *size = limit - base + 1;
832
    return;
833
no_map:
834
    *addr = PCI_BAR_UNMAPPED;
835
    *size = 0;
836
}
837

    
838
static pcibus_t pci_bar_address(PCIDevice *d,
839
                                int reg, uint8_t type, pcibus_t size)
840
{
841
    pcibus_t new_addr, last_addr;
842
    int bar = pci_bar(d, reg);
843
    uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
844

    
845
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
846
        if (!(cmd & PCI_COMMAND_IO)) {
847
            return PCI_BAR_UNMAPPED;
848
        }
849
        new_addr = pci_get_long(d->config + bar) & ~(size - 1);
850
        last_addr = new_addr + size - 1;
851
        /* NOTE: we have only 64K ioports on PC */
852
        if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) {
853
            return PCI_BAR_UNMAPPED;
854
        }
855
        return new_addr;
856
    }
857

    
858
    if (!(cmd & PCI_COMMAND_MEMORY)) {
859
        return PCI_BAR_UNMAPPED;
860
    }
861
    if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
862
        new_addr = pci_get_quad(d->config + bar);
863
    } else {
864
        new_addr = pci_get_long(d->config + bar);
865
    }
866
    /* the ROM slot has a specific enable bit */
867
    if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
868
        return PCI_BAR_UNMAPPED;
869
    }
870
    new_addr &= ~(size - 1);
871
    last_addr = new_addr + size - 1;
872
    /* NOTE: we do not support wrapping */
873
    /* XXX: as we cannot support really dynamic
874
       mappings, we handle specific values as invalid
875
       mappings. */
876
    if (last_addr <= new_addr || new_addr == 0 ||
877
        last_addr == PCI_BAR_UNMAPPED) {
878
        return PCI_BAR_UNMAPPED;
879
    }
880

    
881
    /* Now pcibus_t is 64bit.
882
     * Check if 32 bit BAR wraps around explicitly.
883
     * Without this, PC ide doesn't work well.
884
     * TODO: remove this work around.
885
     */
886
    if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
887
        return PCI_BAR_UNMAPPED;
888
    }
889

    
890
    /*
891
     * OS is allowed to set BAR beyond its addressable
892
     * bits. For example, 32 bit OS can set 64bit bar
893
     * to >4G. Check it. TODO: we might need to support
894
     * it in the future for e.g. PAE.
895
     */
896
    if (last_addr >= TARGET_PHYS_ADDR_MAX) {
897
        return PCI_BAR_UNMAPPED;
898
    }
899

    
900
    return new_addr;
901
}
902

    
903
static void pci_update_mappings(PCIDevice *d)
904
{
905
    PCIIORegion *r;
906
    int i;
907
    pcibus_t new_addr, filtered_size;
908

    
909
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
910
        r = &d->io_regions[i];
911

    
912
        /* this region isn't registered */
913
        if (!r->size)
914
            continue;
915

    
916
        new_addr = pci_bar_address(d, i, r->type, r->size);
917

    
918
        /* bridge filtering */
919
        filtered_size = r->size;
920
        if (new_addr != PCI_BAR_UNMAPPED) {
921
            pci_bridge_filter(d, &new_addr, &filtered_size, r->type);
922
        }
923

    
924
        /* This bar isn't changed */
925
        if (new_addr == r->addr && filtered_size == r->filtered_size)
926
            continue;
927

    
928
        /* now do the real mapping */
929
        if (r->addr != PCI_BAR_UNMAPPED) {
930
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
931
                int class;
932
                /* NOTE: specific hack for IDE in PC case:
933
                   only one byte must be mapped. */
934
                class = pci_get_word(d->config + PCI_CLASS_DEVICE);
935
                if (class == 0x0101 && r->size == 4) {
936
                    isa_unassign_ioport(r->addr + 2, 1);
937
                } else {
938
                    isa_unassign_ioport(r->addr, r->filtered_size);
939
                }
940
            } else {
941
                cpu_register_physical_memory(pci_to_cpu_addr(d->bus, r->addr),
942
                                             r->filtered_size,
943
                                             IO_MEM_UNASSIGNED);
944
                qemu_unregister_coalesced_mmio(r->addr, r->filtered_size);
945
            }
946
        }
947
        r->addr = new_addr;
948
        r->filtered_size = filtered_size;
949
        if (r->addr != PCI_BAR_UNMAPPED) {
950
            /*
951
             * TODO: currently almost all the map funcions assumes
952
             * filtered_size == size and addr & ~(size - 1) == addr.
953
             * However with bridge filtering, they aren't always true.
954
             * Teach them such cases, such that filtered_size < size and
955
             * addr & (size - 1) != 0.
956
             */
957
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
958
                r->map_func(d, i, r->addr, r->filtered_size, r->type);
959
            } else {
960
                r->map_func(d, i, pci_to_cpu_addr(d->bus, r->addr),
961
                            r->filtered_size, r->type);
962
            }
963
        }
964
    }
965
}
966

    
967
static inline int pci_irq_disabled(PCIDevice *d)
968
{
969
    return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
970
}
971

    
972
/* Called after interrupt disabled field update in config space,
973
 * assert/deassert interrupts if necessary.
974
 * Gets original interrupt disable bit value (before update). */
975
static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
976
{
977
    int i, disabled = pci_irq_disabled(d);
978
    if (disabled == was_irq_disabled)
979
        return;
980
    for (i = 0; i < PCI_NUM_PINS; ++i) {
981
        int state = pci_irq_state(d, i);
982
        pci_change_irq_level(d, i, disabled ? -state : state);
983
    }
984
}
985

    
986
uint32_t pci_default_read_config(PCIDevice *d,
987
                                 uint32_t address, int len)
988
{
989
    uint32_t val = 0;
990
    assert(len == 1 || len == 2 || len == 4);
991
    len = MIN(len, pci_config_size(d) - address);
992
    memcpy(&val, d->config + address, len);
993
    return le32_to_cpu(val);
994
}
995

    
996
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
997
{
998
    int i, was_irq_disabled = pci_irq_disabled(d);
999
    uint32_t config_size = pci_config_size(d);
1000

    
1001
    for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) {
1002
        uint8_t wmask = d->wmask[addr + i];
1003
        uint8_t w1cmask = d->w1cmask[addr + i];
1004
        assert(!(wmask & w1cmask));
1005
        d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
1006
        d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
1007
    }
1008
    if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1009
        ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1010
        ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1011
        range_covers_byte(addr, l, PCI_COMMAND))
1012
        pci_update_mappings(d);
1013

    
1014
    if (range_covers_byte(addr, l, PCI_COMMAND))
1015
        pci_update_irq_disabled(d, was_irq_disabled);
1016
}
1017

    
1018
/***********************************************************/
1019
/* generic PCI irq support */
1020

    
1021
/* 0 <= irq_num <= 3. level must be 0 or 1 */
1022
static void pci_set_irq(void *opaque, int irq_num, int level)
1023
{
1024
    PCIDevice *pci_dev = opaque;
1025
    int change;
1026

    
1027
    change = level - pci_irq_state(pci_dev, irq_num);
1028
    if (!change)
1029
        return;
1030

    
1031
    pci_set_irq_state(pci_dev, irq_num, level);
1032
    pci_update_irq_status(pci_dev);
1033
    if (pci_irq_disabled(pci_dev))
1034
        return;
1035
    pci_change_irq_level(pci_dev, irq_num, change);
1036
}
1037

    
1038
bool pci_msi_enabled(PCIDevice *dev)
1039
{
1040
    return msix_enabled(dev) || msi_enabled(dev);
1041
}
1042

    
1043
void pci_msi_notify(PCIDevice *dev, unsigned int vector)
1044
{
1045
    if (msix_enabled(dev)) {
1046
        msix_notify(dev, vector);
1047
    } else if (msi_enabled(dev)) {
1048
        msi_notify(dev, vector);
1049
    } else {
1050
        /* MSI/MSI-X must be enabled */
1051
        abort();
1052
    }
1053
}
1054

    
1055
/***********************************************************/
1056
/* monitor info on PCI */
1057

    
1058
typedef struct {
1059
    uint16_t class;
1060
    const char *desc;
1061
} pci_class_desc;
1062

    
1063
static const pci_class_desc pci_class_descriptions[] =
1064
{
1065
    { 0x0100, "SCSI controller"},
1066
    { 0x0101, "IDE controller"},
1067
    { 0x0102, "Floppy controller"},
1068
    { 0x0103, "IPI controller"},
1069
    { 0x0104, "RAID controller"},
1070
    { 0x0106, "SATA controller"},
1071
    { 0x0107, "SAS controller"},
1072
    { 0x0180, "Storage controller"},
1073
    { 0x0200, "Ethernet controller"},
1074
    { 0x0201, "Token Ring controller"},
1075
    { 0x0202, "FDDI controller"},
1076
    { 0x0203, "ATM controller"},
1077
    { 0x0280, "Network controller"},
1078
    { 0x0300, "VGA controller"},
1079
    { 0x0301, "XGA controller"},
1080
    { 0x0302, "3D controller"},
1081
    { 0x0380, "Display controller"},
1082
    { 0x0400, "Video controller"},
1083
    { 0x0401, "Audio controller"},
1084
    { 0x0402, "Phone"},
1085
    { 0x0480, "Multimedia controller"},
1086
    { 0x0500, "RAM controller"},
1087
    { 0x0501, "Flash controller"},
1088
    { 0x0580, "Memory controller"},
1089
    { 0x0600, "Host bridge"},
1090
    { 0x0601, "ISA bridge"},
1091
    { 0x0602, "EISA bridge"},
1092
    { 0x0603, "MC bridge"},
1093
    { 0x0604, "PCI bridge"},
1094
    { 0x0605, "PCMCIA bridge"},
1095
    { 0x0606, "NUBUS bridge"},
1096
    { 0x0607, "CARDBUS bridge"},
1097
    { 0x0608, "RACEWAY bridge"},
1098
    { 0x0680, "Bridge"},
1099
    { 0x0c03, "USB controller"},
1100
    { 0, NULL}
1101
};
1102

    
1103
static void pci_for_each_device_under_bus(PCIBus *bus,
1104
                                          void (*fn)(PCIBus *b, PCIDevice *d))
1105
{
1106
    PCIDevice *d;
1107
    int devfn;
1108

    
1109
    for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1110
        d = bus->devices[devfn];
1111
        if (d) {
1112
            fn(bus, d);
1113
        }
1114
    }
1115
}
1116

    
1117
void pci_for_each_device(PCIBus *bus, int bus_num,
1118
                         void (*fn)(PCIBus *b, PCIDevice *d))
1119
{
1120
    bus = pci_find_bus(bus, bus_num);
1121

    
1122
    if (bus) {
1123
        pci_for_each_device_under_bus(bus, fn);
1124
    }
1125
}
1126

    
1127
static void pci_device_print(Monitor *mon, QDict *device)
1128
{
1129
    QDict *qdict;
1130
    QListEntry *entry;
1131
    uint64_t addr, size;
1132

    
1133
    monitor_printf(mon, "  Bus %2" PRId64 ", ", qdict_get_int(device, "bus"));
1134
    monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
1135
                        qdict_get_int(device, "slot"),
1136
                        qdict_get_int(device, "function"));
1137
    monitor_printf(mon, "    ");
1138

    
1139
    qdict = qdict_get_qdict(device, "class_info");
1140
    if (qdict_haskey(qdict, "desc")) {
1141
        monitor_printf(mon, "%s", qdict_get_str(qdict, "desc"));
1142
    } else {
1143
        monitor_printf(mon, "Class %04" PRId64, qdict_get_int(qdict, "class"));
1144
    }
1145

    
1146
    qdict = qdict_get_qdict(device, "id");
1147
    monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
1148
                        qdict_get_int(qdict, "device"),
1149
                        qdict_get_int(qdict, "vendor"));
1150

    
1151
    if (qdict_haskey(device, "irq")) {
1152
        monitor_printf(mon, "      IRQ %" PRId64 ".\n",
1153
                            qdict_get_int(device, "irq"));
1154
    }
1155

    
1156
    if (qdict_haskey(device, "pci_bridge")) {
1157
        QDict *info;
1158

    
1159
        qdict = qdict_get_qdict(device, "pci_bridge");
1160

    
1161
        info = qdict_get_qdict(qdict, "bus");
1162
        monitor_printf(mon, "      BUS %" PRId64 ".\n",
1163
                            qdict_get_int(info, "number"));
1164
        monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
1165
                            qdict_get_int(info, "secondary"));
1166
        monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
1167
                            qdict_get_int(info, "subordinate"));
1168

    
1169
        info = qdict_get_qdict(qdict, "io_range");
1170
        monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
1171
                       qdict_get_int(info, "base"),
1172
                       qdict_get_int(info, "limit"));
1173

    
1174
        info = qdict_get_qdict(qdict, "memory_range");
1175
        monitor_printf(mon,
1176
                       "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
1177
                       qdict_get_int(info, "base"),
1178
                       qdict_get_int(info, "limit"));
1179

    
1180
        info = qdict_get_qdict(qdict, "prefetchable_range");
1181
        monitor_printf(mon, "      prefetchable memory range "
1182
                       "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
1183
                       qdict_get_int(info, "base"),
1184
        qdict_get_int(info, "limit"));
1185
    }
1186

    
1187
    QLIST_FOREACH_ENTRY(qdict_get_qlist(device, "regions"), entry) {
1188
        qdict = qobject_to_qdict(qlist_entry_obj(entry));
1189
        monitor_printf(mon, "      BAR%d: ", (int) qdict_get_int(qdict, "bar"));
1190

    
1191
        addr = qdict_get_int(qdict, "address");
1192
        size = qdict_get_int(qdict, "size");
1193

    
1194
        if (!strcmp(qdict_get_str(qdict, "type"), "io")) {
1195
            monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
1196
                                " [0x%04"FMT_PCIBUS"].\n",
1197
                                addr, addr + size - 1);
1198
        } else {
1199
            monitor_printf(mon, "%d bit%s memory at 0x%08"FMT_PCIBUS
1200
                               " [0x%08"FMT_PCIBUS"].\n",
1201
                                qdict_get_bool(qdict, "mem_type_64") ? 64 : 32,
1202
                                qdict_get_bool(qdict, "prefetch") ?
1203
                                " prefetchable" : "", addr, addr + size - 1);
1204
        }
1205
    }
1206

    
1207
    monitor_printf(mon, "      id \"%s\"\n", qdict_get_str(device, "qdev_id"));
1208

    
1209
    if (qdict_haskey(device, "pci_bridge")) {
1210
        qdict = qdict_get_qdict(device, "pci_bridge");
1211
        if (qdict_haskey(qdict, "devices")) {
1212
            QListEntry *dev;
1213
            QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
1214
                pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev)));
1215
            }
1216
        }
1217
    }
1218
}
1219

    
1220
void do_pci_info_print(Monitor *mon, const QObject *data)
1221
{
1222
    QListEntry *bus, *dev;
1223

    
1224
    QLIST_FOREACH_ENTRY(qobject_to_qlist(data), bus) {
1225
        QDict *qdict = qobject_to_qdict(qlist_entry_obj(bus));
1226
        QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
1227
            pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev)));
1228
        }
1229
    }
1230
}
1231

    
1232
static QObject *pci_get_dev_class(const PCIDevice *dev)
1233
{
1234
    int class;
1235
    const pci_class_desc *desc;
1236

    
1237
    class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
1238
    desc = pci_class_descriptions;
1239
    while (desc->desc && class != desc->class)
1240
        desc++;
1241

    
1242
    if (desc->desc) {
1243
        return qobject_from_jsonf("{ 'desc': %s, 'class': %d }",
1244
                                  desc->desc, class);
1245
    } else {
1246
        return qobject_from_jsonf("{ 'class': %d }", class);
1247
    }
1248
}
1249

    
1250
static QObject *pci_get_dev_id(const PCIDevice *dev)
1251
{
1252
    return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }",
1253
                              pci_get_word(dev->config + PCI_VENDOR_ID),
1254
                              pci_get_word(dev->config + PCI_DEVICE_ID));
1255
}
1256

    
1257
static QObject *pci_get_regions_list(const PCIDevice *dev)
1258
{
1259
    int i;
1260
    QList *regions_list;
1261

    
1262
    regions_list = qlist_new();
1263

    
1264
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
1265
        QObject *obj;
1266
        const PCIIORegion *r = &dev->io_regions[i];
1267

    
1268
        if (!r->size) {
1269
            continue;
1270
        }
1271

    
1272
        if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1273
            obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
1274
                                     "'address': %" PRId64 ", "
1275
                                     "'size': %" PRId64 " }",
1276
                                     i, r->addr, r->size);
1277
        } else {
1278
            int mem_type_64 = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64;
1279

    
1280
            obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
1281
                                     "'mem_type_64': %i, 'prefetch': %i, "
1282
                                     "'address': %" PRId64 ", "
1283
                                     "'size': %" PRId64 " }",
1284
                                     i, mem_type_64,
1285
                                     r->type & PCI_BASE_ADDRESS_MEM_PREFETCH,
1286
                                     r->addr, r->size);
1287
        }
1288

    
1289
        qlist_append_obj(regions_list, obj);
1290
    }
1291

    
1292
    return QOBJECT(regions_list);
1293
}
1294

    
1295
static QObject *pci_get_devices_list(PCIBus *bus, int bus_num);
1296

    
1297
static QObject *pci_get_dev_dict(PCIDevice *dev, PCIBus *bus, int bus_num)
1298
{
1299
    uint8_t type;
1300
    QObject *obj;
1301

    
1302
    obj = qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d,"                                       "'class_info': %p, 'id': %p, 'regions': %p,"
1303
                              " 'qdev_id': %s }",
1304
                              bus_num,
1305
                              PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
1306
                              pci_get_dev_class(dev), pci_get_dev_id(dev),
1307
                              pci_get_regions_list(dev),
1308
                              dev->qdev.id ? dev->qdev.id : "");
1309

    
1310
    if (dev->config[PCI_INTERRUPT_PIN] != 0) {
1311
        QDict *qdict = qobject_to_qdict(obj);
1312
        qdict_put(qdict, "irq", qint_from_int(dev->config[PCI_INTERRUPT_LINE]));
1313
    }
1314

    
1315
    type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1316
    if (type == PCI_HEADER_TYPE_BRIDGE) {
1317
        QDict *qdict;
1318
        QObject *pci_bridge;
1319

    
1320
        pci_bridge = qobject_from_jsonf("{ 'bus': "
1321
        "{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
1322
        "'io_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, "
1323
        "'memory_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, "
1324
        "'prefetchable_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "} }",
1325
        dev->config[PCI_PRIMARY_BUS], dev->config[PCI_SECONDARY_BUS],
1326
        dev->config[PCI_SUBORDINATE_BUS],
1327
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO),
1328
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO),
1329
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY),
1330
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY),
1331
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY |
1332
                               PCI_BASE_ADDRESS_MEM_PREFETCH),
1333
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY |
1334
                                PCI_BASE_ADDRESS_MEM_PREFETCH));
1335

    
1336
        if (dev->config[PCI_SECONDARY_BUS] != 0) {
1337
            PCIBus *child_bus = pci_find_bus(bus, dev->config[PCI_SECONDARY_BUS]);
1338

    
1339
            if (child_bus) {
1340
                qdict = qobject_to_qdict(pci_bridge);
1341
                qdict_put_obj(qdict, "devices",
1342
                              pci_get_devices_list(child_bus,
1343
                                                   dev->config[PCI_SECONDARY_BUS]));
1344
            }
1345
        }
1346
        qdict = qobject_to_qdict(obj);
1347
        qdict_put_obj(qdict, "pci_bridge", pci_bridge);
1348
    }
1349

    
1350
    return obj;
1351
}
1352

    
1353
static QObject *pci_get_devices_list(PCIBus *bus, int bus_num)
1354
{
1355
    int devfn;
1356
    PCIDevice *dev;
1357
    QList *dev_list;
1358

    
1359
    dev_list = qlist_new();
1360

    
1361
    for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1362
        dev = bus->devices[devfn];
1363
        if (dev) {
1364
            qlist_append_obj(dev_list, pci_get_dev_dict(dev, bus, bus_num));
1365
        }
1366
    }
1367

    
1368
    return QOBJECT(dev_list);
1369
}
1370

    
1371
static QObject *pci_get_bus_dict(PCIBus *bus, int bus_num)
1372
{
1373
    bus = pci_find_bus(bus, bus_num);
1374
    if (bus) {
1375
        return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }",
1376
                                  bus_num, pci_get_devices_list(bus, bus_num));
1377
    }
1378

    
1379
    return NULL;
1380
}
1381

    
1382
void do_pci_info(Monitor *mon, QObject **ret_data)
1383
{
1384
    QList *bus_list;
1385
    struct PCIHostBus *host;
1386

    
1387
    bus_list = qlist_new();
1388

    
1389
    QLIST_FOREACH(host, &host_buses, next) {
1390
        QObject *obj = pci_get_bus_dict(host->bus, 0);
1391
        if (obj) {
1392
            qlist_append_obj(bus_list, obj);
1393
        }
1394
    }
1395

    
1396
    *ret_data = QOBJECT(bus_list);
1397
}
1398

    
1399
static const char * const pci_nic_models[] = {
1400
    "ne2k_pci",
1401
    "i82551",
1402
    "i82557b",
1403
    "i82559er",
1404
    "rtl8139",
1405
    "e1000",
1406
    "pcnet",
1407
    "virtio",
1408
    NULL
1409
};
1410

    
1411
static const char * const pci_nic_names[] = {
1412
    "ne2k_pci",
1413
    "i82551",
1414
    "i82557b",
1415
    "i82559er",
1416
    "rtl8139",
1417
    "e1000",
1418
    "pcnet",
1419
    "virtio-net-pci",
1420
    NULL
1421
};
1422

    
1423
/* Initialize a PCI NIC.  */
1424
/* FIXME callers should check for failure, but don't */
1425
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
1426
                        const char *default_devaddr)
1427
{
1428
    const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1429
    PCIBus *bus;
1430
    int devfn;
1431
    PCIDevice *pci_dev;
1432
    DeviceState *dev;
1433
    int i;
1434

    
1435
    i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1436
    if (i < 0)
1437
        return NULL;
1438

    
1439
    bus = pci_get_bus_devfn(&devfn, devaddr);
1440
    if (!bus) {
1441
        error_report("Invalid PCI device address %s for device %s",
1442
                     devaddr, pci_nic_names[i]);
1443
        return NULL;
1444
    }
1445

    
1446
    pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1447
    dev = &pci_dev->qdev;
1448
    qdev_set_nic_properties(dev, nd);
1449
    if (qdev_init(dev) < 0)
1450
        return NULL;
1451
    return pci_dev;
1452
}
1453

    
1454
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
1455
                               const char *default_devaddr)
1456
{
1457
    PCIDevice *res;
1458

    
1459
    if (qemu_show_nic_models(nd->model, pci_nic_models))
1460
        exit(0);
1461

    
1462
    res = pci_nic_init(nd, default_model, default_devaddr);
1463
    if (!res)
1464
        exit(1);
1465
    return res;
1466
}
1467

    
1468
static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d)
1469
{
1470
    pci_update_mappings(d);
1471
}
1472

    
1473
void pci_bridge_update_mappings(PCIBus *b)
1474
{
1475
    PCIBus *child;
1476

    
1477
    pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn);
1478

    
1479
    QLIST_FOREACH(child, &b->child, sibling) {
1480
        pci_bridge_update_mappings(child);
1481
    }
1482
}
1483

    
1484
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
1485
{
1486
    PCIBus *sec;
1487

    
1488
    if (!bus) {
1489
        return NULL;
1490
    }
1491

    
1492
    if (pci_bus_num(bus) == bus_num) {
1493
        return bus;
1494
    }
1495

    
1496
    /* try child bus */
1497
    if (!bus->parent_dev /* host pci bridge */ ||
1498
        (bus->parent_dev->config[PCI_SECONDARY_BUS] < bus_num &&
1499
         bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS])) {
1500
        for (; bus; bus = sec) {
1501
            QLIST_FOREACH(sec, &bus->child, sibling) {
1502
                assert(sec->parent_dev);
1503
                if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
1504
                    return sec;
1505
                }
1506
                if (sec->parent_dev->config[PCI_SECONDARY_BUS] < bus_num &&
1507
                    bus_num <= sec->parent_dev->config[PCI_SUBORDINATE_BUS]) {
1508
                    break;
1509
                }
1510
            }
1511
        }
1512
    }
1513

    
1514
    return NULL;
1515
}
1516

    
1517
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
1518
{
1519
    bus = pci_find_bus(bus, bus_num);
1520

    
1521
    if (!bus)
1522
        return NULL;
1523

    
1524
    return bus->devices[PCI_DEVFN(slot, function)];
1525
}
1526

    
1527
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1528
{
1529
    PCIDevice *pci_dev = (PCIDevice *)qdev;
1530
    PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1531
    PCIBus *bus;
1532
    int devfn, rc;
1533

    
1534
    /* initialize cap_present for pci_is_express() and pci_config_size() */
1535
    if (info->is_express) {
1536
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1537
    }
1538

    
1539
    bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1540
    devfn = pci_dev->devfn;
1541
    pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1542
                                     info->config_read, info->config_write,
1543
                                     info->is_bridge);
1544
    if (pci_dev == NULL)
1545
        return -1;
1546
    rc = info->init(pci_dev);
1547
    if (rc != 0) {
1548
        do_pci_unregister_device(pci_dev);
1549
        return rc;
1550
    }
1551

    
1552
    /* rom loading */
1553
    if (pci_dev->romfile == NULL && info->romfile != NULL)
1554
        pci_dev->romfile = qemu_strdup(info->romfile);
1555
    pci_add_option_rom(pci_dev);
1556

    
1557
    if (bus->hotplug) {
1558
        /* lower layer must check qdev->hotplugged */
1559
        rc = bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
1560
        if (rc != 0) {
1561
            int r = pci_unregister_device(&pci_dev->qdev);
1562
            assert(!r);
1563
            return rc;
1564
        }
1565
    }
1566
    return 0;
1567
}
1568

    
1569
static int pci_unplug_device(DeviceState *qdev)
1570
{
1571
    PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1572

    
1573
    return dev->bus->hotplug(dev->bus->hotplug_qdev, dev, 0);
1574
}
1575

    
1576
void pci_qdev_register(PCIDeviceInfo *info)
1577
{
1578
    info->qdev.init = pci_qdev_init;
1579
    info->qdev.unplug = pci_unplug_device;
1580
    info->qdev.exit = pci_unregister_device;
1581
    info->qdev.bus_info = &pci_bus_info;
1582
    qdev_register(&info->qdev);
1583
}
1584

    
1585
void pci_qdev_register_many(PCIDeviceInfo *info)
1586
{
1587
    while (info->qdev.name) {
1588
        pci_qdev_register(info);
1589
        info++;
1590
    }
1591
}
1592

    
1593
PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
1594
                                    const char *name)
1595
{
1596
    DeviceState *dev;
1597

    
1598
    dev = qdev_create(&bus->qbus, name);
1599
    qdev_prop_set_uint32(dev, "addr", devfn);
1600
    qdev_prop_set_bit(dev, "multifunction", multifunction);
1601
    return DO_UPCAST(PCIDevice, qdev, dev);
1602
}
1603

    
1604
PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
1605
                                           bool multifunction,
1606
                                           const char *name)
1607
{
1608
    PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name);
1609
    qdev_init_nofail(&dev->qdev);
1610
    return dev;
1611
}
1612

    
1613
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1614
{
1615
    return pci_create_multifunction(bus, devfn, false, name);
1616
}
1617

    
1618
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1619
{
1620
    return pci_create_simple_multifunction(bus, devfn, false, name);
1621
}
1622

    
1623
static int pci_find_space(PCIDevice *pdev, uint8_t size)
1624
{
1625
    int config_size = pci_config_size(pdev);
1626
    int offset = PCI_CONFIG_HEADER_SIZE;
1627
    int i;
1628
    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
1629
        if (pdev->used[i])
1630
            offset = i + 1;
1631
        else if (i - offset + 1 == size)
1632
            return offset;
1633
    return 0;
1634
}
1635

    
1636
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1637
                                        uint8_t *prev_p)
1638
{
1639
    uint8_t next, prev;
1640

    
1641
    if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1642
        return 0;
1643

    
1644
    for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1645
         prev = next + PCI_CAP_LIST_NEXT)
1646
        if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1647
            break;
1648

    
1649
    if (prev_p)
1650
        *prev_p = prev;
1651
    return next;
1652
}
1653

    
1654
static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type)
1655
{
1656
    cpu_register_physical_memory(addr, size, pdev->rom_offset);
1657
}
1658

    
1659
/* Add an option rom for the device */
1660
static int pci_add_option_rom(PCIDevice *pdev)
1661
{
1662
    int size;
1663
    char *path;
1664
    void *ptr;
1665
    char name[32];
1666

    
1667
    if (!pdev->romfile)
1668
        return 0;
1669
    if (strlen(pdev->romfile) == 0)
1670
        return 0;
1671

    
1672
    if (!pdev->rom_bar) {
1673
        /*
1674
         * Load rom via fw_cfg instead of creating a rom bar,
1675
         * for 0.11 compatibility.
1676
         */
1677
        int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
1678
        if (class == 0x0300) {
1679
            rom_add_vga(pdev->romfile);
1680
        } else {
1681
            rom_add_option(pdev->romfile);
1682
        }
1683
        return 0;
1684
    }
1685

    
1686
    path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
1687
    if (path == NULL) {
1688
        path = qemu_strdup(pdev->romfile);
1689
    }
1690

    
1691
    size = get_image_size(path);
1692
    if (size < 0) {
1693
        error_report("%s: failed to find romfile \"%s\"",
1694
                     __FUNCTION__, pdev->romfile);
1695
        return -1;
1696
    }
1697
    if (size & (size - 1)) {
1698
        size = 1 << qemu_fls(size);
1699
    }
1700

    
1701
    if (pdev->qdev.info->vmsd)
1702
        snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->vmsd->name);
1703
    else
1704
        snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->name);
1705
    pdev->rom_offset = qemu_ram_alloc(&pdev->qdev, name, size);
1706

    
1707
    ptr = qemu_get_ram_ptr(pdev->rom_offset);
1708
    load_image(path, ptr);
1709
    qemu_free(path);
1710

    
1711
    pci_register_bar(pdev, PCI_ROM_SLOT, size,
1712
                     0, pci_map_option_rom);
1713

    
1714
    return 0;
1715
}
1716

    
1717
static void pci_del_option_rom(PCIDevice *pdev)
1718
{
1719
    if (!pdev->rom_offset)
1720
        return;
1721

    
1722
    qemu_ram_free(pdev->rom_offset);
1723
    pdev->rom_offset = 0;
1724
}
1725

    
1726
/*
1727
 * if !offset
1728
 * Reserve space and add capability to the linked list in pci config space
1729
 *
1730
 * if offset = 0,
1731
 * Find and reserve space and add capability to the linked list
1732
 * in pci config space */
1733
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
1734
                       uint8_t offset, uint8_t size)
1735
{
1736
    uint8_t *config;
1737
    if (!offset) {
1738
        offset = pci_find_space(pdev, size);
1739
        if (!offset) {
1740
            return -ENOSPC;
1741
        }
1742
    }
1743

    
1744
    config = pdev->config + offset;
1745
    config[PCI_CAP_LIST_ID] = cap_id;
1746
    config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1747
    pdev->config[PCI_CAPABILITY_LIST] = offset;
1748
    pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1749
    memset(pdev->used + offset, 0xFF, size);
1750
    /* Make capability read-only by default */
1751
    memset(pdev->wmask + offset, 0, size);
1752
    /* Check capability by default */
1753
    memset(pdev->cmask + offset, 0xFF, size);
1754
    return offset;
1755
}
1756

    
1757
/* Unlink capability from the pci config space. */
1758
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1759
{
1760
    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1761
    if (!offset)
1762
        return;
1763
    pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1764
    /* Make capability writeable again */
1765
    memset(pdev->wmask + offset, 0xff, size);
1766
    memset(pdev->w1cmask + offset, 0, size);
1767
    /* Clear cmask as device-specific registers can't be checked */
1768
    memset(pdev->cmask + offset, 0, size);
1769
    memset(pdev->used + offset, 0, size);
1770

    
1771
    if (!pdev->config[PCI_CAPABILITY_LIST])
1772
        pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1773
}
1774

    
1775
/* Reserve space for capability at a known offset (to call after load). */
1776
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1777
{
1778
    memset(pdev->used + offset, 0xff, size);
1779
}
1780

    
1781
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1782
{
1783
    return pci_find_capability_list(pdev, cap_id, NULL);
1784
}
1785

    
1786
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1787
{
1788
    PCIDevice *d = (PCIDevice *)dev;
1789
    const pci_class_desc *desc;
1790
    char ctxt[64];
1791
    PCIIORegion *r;
1792
    int i, class;
1793

    
1794
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1795
    desc = pci_class_descriptions;
1796
    while (desc->desc && class != desc->class)
1797
        desc++;
1798
    if (desc->desc) {
1799
        snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1800
    } else {
1801
        snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1802
    }
1803

    
1804
    monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1805
                   "pci id %04x:%04x (sub %04x:%04x)\n",
1806
                   indent, "", ctxt,
1807
                   d->config[PCI_SECONDARY_BUS],
1808
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1809
                   pci_get_word(d->config + PCI_VENDOR_ID),
1810
                   pci_get_word(d->config + PCI_DEVICE_ID),
1811
                   pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1812
                   pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1813
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
1814
        r = &d->io_regions[i];
1815
        if (!r->size)
1816
            continue;
1817
        monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1818
                       " [0x%"FMT_PCIBUS"]\n",
1819
                       indent, "",
1820
                       i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1821
                       r->addr, r->addr + r->size - 1);
1822
    }
1823
}
1824

    
1825
static char *pcibus_get_dev_path(DeviceState *dev)
1826
{
1827
    PCIDevice *d = (PCIDevice *)dev;
1828
    char path[16];
1829

    
1830
    snprintf(path, sizeof(path), "%04x:%02x:%02x.%x",
1831
             pci_find_domain(d->bus), d->config[PCI_SECONDARY_BUS],
1832
             PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
1833

    
1834
    return strdup(path);
1835
}
1836