Statistics
| Branch: | Revision:

root / hw / pci.c @ c9f398e5

History | View | Annotate | Download (47.2 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 "monitor.h"
27
#include "net.h"
28
#include "sysemu.h"
29
#include "loader.h"
30

    
31
//#define DEBUG_PCI
32
#ifdef DEBUG_PCI
33
# define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
34
#else
35
# define PCI_DPRINTF(format, ...)       do { } while (0)
36
#endif
37

    
38
struct PCIBus {
39
    BusState qbus;
40
    int devfn_min;
41
    pci_set_irq_fn set_irq;
42
    pci_map_irq_fn map_irq;
43
    pci_hotplug_fn hotplug;
44
    void *irq_opaque;
45
    PCIDevice *devices[256];
46
    PCIDevice *parent_dev;
47
    target_phys_addr_t mem_base;
48

    
49
    QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
50
    QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
51

    
52
    /* The bus IRQ state is the logical OR of the connected devices.
53
       Keep a count of the number of devices with raised IRQs.  */
54
    int nirq;
55
    int *irq_count;
56
};
57

    
58
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
59

    
60
static struct BusInfo pci_bus_info = {
61
    .name       = "PCI",
62
    .size       = sizeof(PCIBus),
63
    .print_dev  = pcibus_dev_print,
64
    .props      = (Property[]) {
65
        DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
66
        DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
67
        DEFINE_PROP_END_OF_LIST()
68
    }
69
};
70

    
71
static void pci_update_mappings(PCIDevice *d);
72
static void pci_set_irq(void *opaque, int irq_num, int level);
73
static int pci_add_option_rom(PCIDevice *pdev);
74

    
75
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
76
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
77

    
78
struct PCIHostBus {
79
    int domain;
80
    struct PCIBus *bus;
81
    QLIST_ENTRY(PCIHostBus) next;
82
};
83
static QLIST_HEAD(, PCIHostBus) host_buses;
84

    
85
static const VMStateDescription vmstate_pcibus = {
86
    .name = "PCIBUS",
87
    .version_id = 1,
88
    .minimum_version_id = 1,
89
    .minimum_version_id_old = 1,
90
    .fields      = (VMStateField []) {
91
        VMSTATE_INT32_EQUAL(nirq, PCIBus),
92
        VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
93
        VMSTATE_END_OF_LIST()
94
    }
95
};
96

    
97
static int pci_bar(PCIDevice *d, int reg)
98
{
99
    uint8_t type;
100

    
101
    if (reg != PCI_ROM_SLOT)
102
        return PCI_BASE_ADDRESS_0 + reg * 4;
103

    
104
    type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
105
    return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
106
}
107

    
108
static inline int pci_irq_state(PCIDevice *d, int irq_num)
109
{
110
        return (d->irq_state >> irq_num) & 0x1;
111
}
112

    
113
static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
114
{
115
        d->irq_state &= ~(0x1 << irq_num);
116
        d->irq_state |= level << irq_num;
117
}
118

    
119
static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
120
{
121
    PCIBus *bus;
122
    for (;;) {
123
        bus = pci_dev->bus;
124
        irq_num = bus->map_irq(pci_dev, irq_num);
125
        if (bus->set_irq)
126
            break;
127
        pci_dev = bus->parent_dev;
128
    }
129
    bus->irq_count[irq_num] += change;
130
    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
131
}
132

    
133
/* Update interrupt status bit in config space on interrupt
134
 * state change. */
135
static void pci_update_irq_status(PCIDevice *dev)
136
{
137
    if (dev->irq_state) {
138
        dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
139
    } else {
140
        dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
141
    }
142
}
143

    
144
static void pci_device_reset(PCIDevice *dev)
145
{
146
    int r;
147

    
148
    dev->irq_state = 0;
149
    pci_update_irq_status(dev);
150
    dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
151
                                  PCI_COMMAND_MASTER);
152
    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
153
    dev->config[PCI_INTERRUPT_LINE] = 0x0;
154
    for (r = 0; r < PCI_NUM_REGIONS; ++r) {
155
        if (!dev->io_regions[r].size) {
156
            continue;
157
        }
158
        pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type);
159
    }
160
    pci_update_mappings(dev);
161
}
162

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

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

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

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

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

    
197
    return NULL;
198
}
199

    
200
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
201
                         const char *name, int devfn_min)
202
{
203
    qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
204
    bus->devfn_min = devfn_min;
205

    
206
    /* host bridge */
207
    QLIST_INIT(&bus->child);
208
    pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
209

    
210
    vmstate_register(-1, &vmstate_pcibus, bus);
211
    qemu_register_reset(pci_bus_reset, bus);
212
}
213

    
214
PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
215
{
216
    PCIBus *bus;
217

    
218
    bus = qemu_mallocz(sizeof(*bus));
219
    bus->qbus.qdev_allocated = 1;
220
    pci_bus_new_inplace(bus, parent, name, devfn_min);
221
    return bus;
222
}
223

    
224
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
225
                  void *irq_opaque, int nirq)
226
{
227
    bus->set_irq = set_irq;
228
    bus->map_irq = map_irq;
229
    bus->irq_opaque = irq_opaque;
230
    bus->nirq = nirq;
231
    bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
232
}
233

    
234
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
235
{
236
    bus->qbus.allow_hotplug = 1;
237
    bus->hotplug = hotplug;
238
}
239

    
240
void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
241
{
242
    bus->mem_base = base;
243
}
244

    
245
PCIBus *pci_register_bus(DeviceState *parent, const char *name,
246
                         pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
247
                         void *irq_opaque, int devfn_min, int nirq)
248
{
249
    PCIBus *bus;
250

    
251
    bus = pci_bus_new(parent, name, devfn_min);
252
    pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
253
    return bus;
254
}
255

    
256
static void pci_register_secondary_bus(PCIBus *parent,
257
                                       PCIBus *bus,
258
                                       PCIDevice *dev,
259
                                       pci_map_irq_fn map_irq,
260
                                       const char *name)
261
{
262
    qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name);
263
    bus->map_irq = map_irq;
264
    bus->parent_dev = dev;
265

    
266
    QLIST_INIT(&bus->child);
267
    QLIST_INSERT_HEAD(&parent->child, bus, sibling);
268
}
269

    
270
static void pci_unregister_secondary_bus(PCIBus *bus)
271
{
272
    assert(QLIST_EMPTY(&bus->child));
273
    QLIST_REMOVE(bus, sibling);
274
}
275

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

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

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

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

    
301
    pci_update_mappings(s);
302

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

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

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

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

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

    
339
    return 0;
340
}
341

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

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

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

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

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

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

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

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

    
418
static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
419
{
420
    uint16_t *id;
421

    
422
    id = (void*)(&pci_dev->config[PCI_SUBSYSTEM_VENDOR_ID]);
423
    id[0] = cpu_to_le16(pci_default_sub_vendor_id);
424
    id[1] = cpu_to_le16(pci_default_sub_device_id);
425
    return 0;
426
}
427

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

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

    
459
    if (dom > 0xffff || bus > 0xff || val > 0x1f)
460
        return -1;
461

    
462
    slot = val;
463

    
464
    if (*e)
465
        return -1;
466

    
467
    /* Note: QEMU doesn't implement domains other than 0 */
468
    if (!pci_find_bus(pci_find_root_bus(dom), bus))
469
        return -1;
470

    
471
    *domp = dom;
472
    *busp = bus;
473
    *slotp = slot;
474
    return 0;
475
}
476

    
477
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
478
                     unsigned *slotp)
479
{
480
    /* strip legacy tag */
481
    if (!strncmp(addr, "pci_addr=", 9)) {
482
        addr += 9;
483
    }
484
    if (pci_parse_devaddr(addr, domp, busp, slotp)) {
485
        monitor_printf(mon, "Invalid pci address\n");
486
        return -1;
487
    }
488
    return 0;
489
}
490

    
491
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
492
{
493
    int dom, bus;
494
    unsigned slot;
495

    
496
    if (!devaddr) {
497
        *devfnp = -1;
498
        return pci_find_bus(pci_find_root_bus(0), 0);
499
    }
500

    
501
    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
502
        return NULL;
503
    }
504

    
505
    *devfnp = slot << 3;
506
    return pci_find_bus(pci_find_root_bus(0), bus);
507
}
508

    
509
static void pci_init_cmask(PCIDevice *dev)
510
{
511
    pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
512
    pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
513
    dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
514
    dev->cmask[PCI_REVISION_ID] = 0xff;
515
    dev->cmask[PCI_CLASS_PROG] = 0xff;
516
    pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
517
    dev->cmask[PCI_HEADER_TYPE] = 0xff;
518
    dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
519
}
520

    
521
static void pci_init_wmask(PCIDevice *dev)
522
{
523
    int config_size = pci_config_size(dev);
524

    
525
    dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
526
    dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
527
    pci_set_word(dev->wmask + PCI_COMMAND,
528
                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
529
                 PCI_COMMAND_INTX_DISABLE);
530

    
531
    memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
532
           config_size - PCI_CONFIG_HEADER_SIZE);
533
}
534

    
535
static void pci_init_wmask_bridge(PCIDevice *d)
536
{
537
    /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
538
       PCI_SEC_LETENCY_TIMER */
539
    memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
540

    
541
    /* base and limit */
542
    d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
543
    d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
544
    pci_set_word(d->wmask + PCI_MEMORY_BASE,
545
                 PCI_MEMORY_RANGE_MASK & 0xffff);
546
    pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
547
                 PCI_MEMORY_RANGE_MASK & 0xffff);
548
    pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
549
                 PCI_PREF_RANGE_MASK & 0xffff);
550
    pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
551
                 PCI_PREF_RANGE_MASK & 0xffff);
552

    
553
    /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
554
    memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
555

    
556
    pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
557
}
558

    
559
static void pci_config_alloc(PCIDevice *pci_dev)
560
{
561
    int config_size = pci_config_size(pci_dev);
562

    
563
    pci_dev->config = qemu_mallocz(config_size);
564
    pci_dev->cmask = qemu_mallocz(config_size);
565
    pci_dev->wmask = qemu_mallocz(config_size);
566
    pci_dev->used = qemu_mallocz(config_size);
567
}
568

    
569
static void pci_config_free(PCIDevice *pci_dev)
570
{
571
    qemu_free(pci_dev->config);
572
    qemu_free(pci_dev->cmask);
573
    qemu_free(pci_dev->wmask);
574
    qemu_free(pci_dev->used);
575
}
576

    
577
/* -1 for devfn means auto assign */
578
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
579
                                         const char *name, int devfn,
580
                                         PCIConfigReadFunc *config_read,
581
                                         PCIConfigWriteFunc *config_write,
582
                                         uint8_t header_type)
583
{
584
    if (devfn < 0) {
585
        for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
586
            devfn += 8) {
587
            if (!bus->devices[devfn])
588
                goto found;
589
        }
590
        qemu_error("PCI: no devfn available for %s, all in use\n", name);
591
        return NULL;
592
    found: ;
593
    } else if (bus->devices[devfn]) {
594
        qemu_error("PCI: devfn %d not available for %s, in use by %s\n", devfn,
595
                 name, bus->devices[devfn]->name);
596
        return NULL;
597
    }
598
    pci_dev->bus = bus;
599
    pci_dev->devfn = devfn;
600
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
601
    pci_dev->irq_state = 0;
602
    pci_config_alloc(pci_dev);
603

    
604
    header_type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
605
    if (header_type == PCI_HEADER_TYPE_NORMAL) {
606
        pci_set_default_subsystem_id(pci_dev);
607
    }
608
    pci_init_cmask(pci_dev);
609
    pci_init_wmask(pci_dev);
610
    if (header_type == PCI_HEADER_TYPE_BRIDGE) {
611
        pci_init_wmask_bridge(pci_dev);
612
    }
613

    
614
    if (!config_read)
615
        config_read = pci_default_read_config;
616
    if (!config_write)
617
        config_write = pci_default_write_config;
618
    pci_dev->config_read = config_read;
619
    pci_dev->config_write = config_write;
620
    bus->devices[devfn] = pci_dev;
621
    pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
622
    pci_dev->version_id = 2; /* Current pci device vmstate version */
623
    return pci_dev;
624
}
625

    
626
PCIDevice *pci_register_device(PCIBus *bus, const char *name,
627
                               int instance_size, int devfn,
628
                               PCIConfigReadFunc *config_read,
629
                               PCIConfigWriteFunc *config_write)
630
{
631
    PCIDevice *pci_dev;
632

    
633
    pci_dev = qemu_mallocz(instance_size);
634
    pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
635
                                     config_read, config_write,
636
                                     PCI_HEADER_TYPE_NORMAL);
637
    if (pci_dev == NULL) {
638
        hw_error("PCI: can't register device\n");
639
    }
640
    return pci_dev;
641
}
642

    
643
static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
644
                                          target_phys_addr_t addr)
645
{
646
    return addr + bus->mem_base;
647
}
648

    
649
static void pci_unregister_io_regions(PCIDevice *pci_dev)
650
{
651
    PCIIORegion *r;
652
    int i;
653

    
654
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
655
        r = &pci_dev->io_regions[i];
656
        if (!r->size || r->addr == PCI_BAR_UNMAPPED)
657
            continue;
658
        if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
659
            isa_unassign_ioport(r->addr, r->filtered_size);
660
        } else {
661
            cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus,
662
                                                         r->addr),
663
                                         r->filtered_size,
664
                                         IO_MEM_UNASSIGNED);
665
        }
666
    }
667
}
668

    
669
static int pci_unregister_device(DeviceState *dev)
670
{
671
    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
672
    PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
673
    int ret = 0;
674

    
675
    if (info->exit)
676
        ret = info->exit(pci_dev);
677
    if (ret)
678
        return ret;
679

    
680
    pci_unregister_io_regions(pci_dev);
681

    
682
    qemu_free_irqs(pci_dev->irq);
683
    pci_dev->bus->devices[pci_dev->devfn] = NULL;
684
    pci_config_free(pci_dev);
685
    return 0;
686
}
687

    
688
void pci_register_bar(PCIDevice *pci_dev, int region_num,
689
                            pcibus_t size, int type,
690
                            PCIMapIORegionFunc *map_func)
691
{
692
    PCIIORegion *r;
693
    uint32_t addr;
694
    pcibus_t wmask;
695

    
696
    if ((unsigned int)region_num >= PCI_NUM_REGIONS)
697
        return;
698

    
699
    if (size & (size-1)) {
700
        fprintf(stderr, "ERROR: PCI region size must be pow2 "
701
                    "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
702
        exit(1);
703
    }
704

    
705
    r = &pci_dev->io_regions[region_num];
706
    r->addr = PCI_BAR_UNMAPPED;
707
    r->size = size;
708
    r->filtered_size = size;
709
    r->type = type;
710
    r->map_func = map_func;
711

    
712
    wmask = ~(size - 1);
713
    addr = pci_bar(pci_dev, region_num);
714
    if (region_num == PCI_ROM_SLOT) {
715
        /* ROM enable bit is writeable */
716
        wmask |= PCI_ROM_ADDRESS_ENABLE;
717
    }
718
    pci_set_long(pci_dev->config + addr, type);
719
    if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
720
        r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
721
        pci_set_quad(pci_dev->wmask + addr, wmask);
722
        pci_set_quad(pci_dev->cmask + addr, ~0ULL);
723
    } else {
724
        pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
725
        pci_set_long(pci_dev->cmask + addr, 0xffffffff);
726
    }
727
}
728

    
729
static uint32_t pci_config_get_io_base(PCIDevice *d,
730
                                       uint32_t base, uint32_t base_upper16)
731
{
732
    uint32_t val;
733

    
734
    val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
735
    if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
736
        val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
737
    }
738
    return val;
739
}
740

    
741
static pcibus_t pci_config_get_memory_base(PCIDevice *d, uint32_t base)
742
{
743
    return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
744
        << 16;
745
}
746

    
747
static pcibus_t pci_config_get_pref_base(PCIDevice *d,
748
                                         uint32_t base, uint32_t upper)
749
{
750
    pcibus_t tmp;
751
    pcibus_t val;
752

    
753
    tmp = (pcibus_t)pci_get_word(d->config + base);
754
    val = (tmp & PCI_PREF_RANGE_MASK) << 16;
755
    if (tmp & PCI_PREF_RANGE_TYPE_64) {
756
        val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
757
    }
758
    return val;
759
}
760

    
761
static pcibus_t pci_bridge_get_base(PCIDevice *bridge, uint8_t type)
762
{
763
    pcibus_t base;
764
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
765
        base = pci_config_get_io_base(bridge,
766
                                      PCI_IO_BASE, PCI_IO_BASE_UPPER16);
767
    } else {
768
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
769
            base = pci_config_get_pref_base(
770
                bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
771
        } else {
772
            base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
773
        }
774
    }
775

    
776
    return base;
777
}
778

    
779
static pcibus_t pci_bridge_get_limit(PCIDevice *bridge, uint8_t type)
780
{
781
    pcibus_t limit;
782
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
783
        limit = pci_config_get_io_base(bridge,
784
                                      PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
785
        limit |= 0xfff;         /* PCI bridge spec 3.2.5.6. */
786
    } else {
787
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
788
            limit = pci_config_get_pref_base(
789
                bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
790
        } else {
791
            limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
792
        }
793
        limit |= 0xfffff;       /* PCI bridge spec 3.2.5.{1, 8}. */
794
    }
795
    return limit;
796
}
797

    
798
static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size,
799
                              uint8_t type)
800
{
801
    pcibus_t base = *addr;
802
    pcibus_t limit = *addr + *size - 1;
803
    PCIDevice *br;
804

    
805
    for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
806
        uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
807

    
808
        if (type & PCI_BASE_ADDRESS_SPACE_IO) {
809
            if (!(cmd & PCI_COMMAND_IO)) {
810
                goto no_map;
811
            }
812
        } else {
813
            if (!(cmd & PCI_COMMAND_MEMORY)) {
814
                goto no_map;
815
            }
816
        }
817

    
818
        base = MAX(base, pci_bridge_get_base(br, type));
819
        limit = MIN(limit, pci_bridge_get_limit(br, type));
820
    }
821

    
822
    if (base > limit) {
823
        goto no_map;
824
    }
825
    *addr = base;
826
    *size = limit - base + 1;
827
    return;
828
no_map:
829
    *addr = PCI_BAR_UNMAPPED;
830
    *size = 0;
831
}
832

    
833
static pcibus_t pci_bar_address(PCIDevice *d,
834
                                int reg, uint8_t type, pcibus_t size)
835
{
836
    pcibus_t new_addr, last_addr;
837
    int bar = pci_bar(d, reg);
838
    uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
839

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

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

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

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

    
895
    return new_addr;
896
}
897

    
898
static void pci_update_mappings(PCIDevice *d)
899
{
900
    PCIIORegion *r;
901
    int i;
902
    pcibus_t new_addr, filtered_size;
903

    
904
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
905
        r = &d->io_regions[i];
906

    
907
        /* this region isn't registered */
908
        if (!r->size)
909
            continue;
910

    
911
        new_addr = pci_bar_address(d, i, r->type, r->size);
912

    
913
        /* bridge filtering */
914
        filtered_size = r->size;
915
        if (new_addr != PCI_BAR_UNMAPPED) {
916
            pci_bridge_filter(d, &new_addr, &filtered_size, r->type);
917
        }
918

    
919
        /* This bar isn't changed */
920
        if (new_addr == r->addr && filtered_size == r->filtered_size)
921
            continue;
922

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

    
962
static inline int pci_irq_disabled(PCIDevice *d)
963
{
964
    return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
965
}
966

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

    
981
uint32_t pci_default_read_config(PCIDevice *d,
982
                                 uint32_t address, int len)
983
{
984
    uint32_t val = 0;
985
    assert(len == 1 || len == 2 || len == 4);
986
    len = MIN(len, pci_config_size(d) - address);
987
    memcpy(&val, d->config + address, len);
988
    return le32_to_cpu(val);
989
}
990

    
991
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
992
{
993
    int i, was_irq_disabled = pci_irq_disabled(d);
994
    uint32_t config_size = pci_config_size(d);
995

    
996
    for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) {
997
        uint8_t wmask = d->wmask[addr + i];
998
        d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
999
    }
1000
    if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1001
        ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1002
        ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1003
        range_covers_byte(addr, l, PCI_COMMAND))
1004
        pci_update_mappings(d);
1005

    
1006
    if (range_covers_byte(addr, l, PCI_COMMAND))
1007
        pci_update_irq_disabled(d, was_irq_disabled);
1008
}
1009

    
1010
/***********************************************************/
1011
/* generic PCI irq support */
1012

    
1013
/* 0 <= irq_num <= 3. level must be 0 or 1 */
1014
static void pci_set_irq(void *opaque, int irq_num, int level)
1015
{
1016
    PCIDevice *pci_dev = opaque;
1017
    int change;
1018

    
1019
    change = level - pci_irq_state(pci_dev, irq_num);
1020
    if (!change)
1021
        return;
1022

    
1023
    pci_set_irq_state(pci_dev, irq_num, level);
1024
    pci_update_irq_status(pci_dev);
1025
    if (pci_irq_disabled(pci_dev))
1026
        return;
1027
    pci_change_irq_level(pci_dev, irq_num, change);
1028
}
1029

    
1030
/***********************************************************/
1031
/* monitor info on PCI */
1032

    
1033
typedef struct {
1034
    uint16_t class;
1035
    const char *desc;
1036
} pci_class_desc;
1037

    
1038
static const pci_class_desc pci_class_descriptions[] =
1039
{
1040
    { 0x0100, "SCSI controller"},
1041
    { 0x0101, "IDE controller"},
1042
    { 0x0102, "Floppy controller"},
1043
    { 0x0103, "IPI controller"},
1044
    { 0x0104, "RAID controller"},
1045
    { 0x0106, "SATA controller"},
1046
    { 0x0107, "SAS controller"},
1047
    { 0x0180, "Storage controller"},
1048
    { 0x0200, "Ethernet controller"},
1049
    { 0x0201, "Token Ring controller"},
1050
    { 0x0202, "FDDI controller"},
1051
    { 0x0203, "ATM controller"},
1052
    { 0x0280, "Network controller"},
1053
    { 0x0300, "VGA controller"},
1054
    { 0x0301, "XGA controller"},
1055
    { 0x0302, "3D controller"},
1056
    { 0x0380, "Display controller"},
1057
    { 0x0400, "Video controller"},
1058
    { 0x0401, "Audio controller"},
1059
    { 0x0402, "Phone"},
1060
    { 0x0480, "Multimedia controller"},
1061
    { 0x0500, "RAM controller"},
1062
    { 0x0501, "Flash controller"},
1063
    { 0x0580, "Memory controller"},
1064
    { 0x0600, "Host bridge"},
1065
    { 0x0601, "ISA bridge"},
1066
    { 0x0602, "EISA bridge"},
1067
    { 0x0603, "MC bridge"},
1068
    { 0x0604, "PCI bridge"},
1069
    { 0x0605, "PCMCIA bridge"},
1070
    { 0x0606, "NUBUS bridge"},
1071
    { 0x0607, "CARDBUS bridge"},
1072
    { 0x0608, "RACEWAY bridge"},
1073
    { 0x0680, "Bridge"},
1074
    { 0x0c03, "USB controller"},
1075
    { 0, NULL}
1076
};
1077

    
1078
static void pci_info_device(PCIBus *bus, PCIDevice *d)
1079
{
1080
    Monitor *mon = cur_mon;
1081
    int i, class;
1082
    PCIIORegion *r;
1083
    const pci_class_desc *desc;
1084

    
1085
    monitor_printf(mon, "  Bus %2d, device %3d, function %d:\n",
1086
                   pci_bus_num(d->bus),
1087
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
1088
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1089
    monitor_printf(mon, "    ");
1090
    desc = pci_class_descriptions;
1091
    while (desc->desc && class != desc->class)
1092
        desc++;
1093
    if (desc->desc) {
1094
        monitor_printf(mon, "%s", desc->desc);
1095
    } else {
1096
        monitor_printf(mon, "Class %04x", class);
1097
    }
1098
    monitor_printf(mon, ": PCI device %04x:%04x\n",
1099
           pci_get_word(d->config + PCI_VENDOR_ID),
1100
           pci_get_word(d->config + PCI_DEVICE_ID));
1101

    
1102
    if (d->config[PCI_INTERRUPT_PIN] != 0) {
1103
        monitor_printf(mon, "      IRQ %d.\n",
1104
                       d->config[PCI_INTERRUPT_LINE]);
1105
    }
1106
    if (class == 0x0604) {
1107
        uint64_t base;
1108
        uint64_t limit;
1109

    
1110
        monitor_printf(mon, "      BUS %d.\n", d->config[0x19]);
1111
        monitor_printf(mon, "      secondary bus %d.\n",
1112
                       d->config[PCI_SECONDARY_BUS]);
1113
        monitor_printf(mon, "      subordinate bus %d.\n",
1114
                       d->config[PCI_SUBORDINATE_BUS]);
1115

    
1116
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_IO);
1117
        limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_IO);
1118
        monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
1119
                       base, limit);
1120

    
1121
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY);
1122
        limit= pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY);
1123
        monitor_printf(mon,
1124
                       "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
1125
                       base, limit);
1126

    
1127
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY |
1128
                                   PCI_BASE_ADDRESS_MEM_PREFETCH);
1129
        limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY |
1130
                                     PCI_BASE_ADDRESS_MEM_PREFETCH);
1131
        monitor_printf(mon, "      prefetchable memory range "
1132
                       "[0x%08"PRIx64", 0x%08"PRIx64"]\n", base, limit);
1133
    }
1134
    for(i = 0;i < PCI_NUM_REGIONS; i++) {
1135
        r = &d->io_regions[i];
1136
        if (r->size != 0) {
1137
            monitor_printf(mon, "      BAR%d: ", i);
1138
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1139
                monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
1140
                               " [0x%04"FMT_PCIBUS"].\n",
1141
                               r->addr, r->addr + r->size - 1);
1142
            } else {
1143
                const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ?
1144
                    "64 bit" : "32 bit";
1145
                const char *prefetch =
1146
                    r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ?
1147
                    " prefetchable" : "";
1148

    
1149
                monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
1150
                               " [0x%08"FMT_PCIBUS"].\n",
1151
                               type, prefetch,
1152
                               r->addr, r->addr + r->size - 1);
1153
            }
1154
        }
1155
    }
1156
    monitor_printf(mon, "      id \"%s\"\n", d->qdev.id ? d->qdev.id : "");
1157
    if (class == 0x0604 && d->config[0x19] != 0) {
1158
        pci_for_each_device(bus, d->config[0x19], pci_info_device);
1159
    }
1160
}
1161

    
1162
static void pci_for_each_device_under_bus(PCIBus *bus,
1163
                                          void (*fn)(PCIBus *b, PCIDevice *d))
1164
{
1165
    PCIDevice *d;
1166
    int devfn;
1167

    
1168
    for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1169
        d = bus->devices[devfn];
1170
        if (d)
1171
            fn(bus, d);
1172
    }
1173
}
1174

    
1175
void pci_for_each_device(PCIBus *bus, int bus_num,
1176
                         void (*fn)(PCIBus *b, PCIDevice *d))
1177
{
1178
    bus = pci_find_bus(bus, bus_num);
1179

    
1180
    if (bus) {
1181
        pci_for_each_device_under_bus(bus, fn);
1182
    }
1183
}
1184

    
1185
void pci_info(Monitor *mon)
1186
{
1187
    struct PCIHostBus *host;
1188
    QLIST_FOREACH(host, &host_buses, next) {
1189
        pci_for_each_device(host->bus, 0, pci_info_device);
1190
    }
1191
}
1192

    
1193
static const char * const pci_nic_models[] = {
1194
    "ne2k_pci",
1195
    "i82551",
1196
    "i82557b",
1197
    "i82559er",
1198
    "rtl8139",
1199
    "e1000",
1200
    "pcnet",
1201
    "virtio",
1202
    NULL
1203
};
1204

    
1205
static const char * const pci_nic_names[] = {
1206
    "ne2k_pci",
1207
    "i82551",
1208
    "i82557b",
1209
    "i82559er",
1210
    "rtl8139",
1211
    "e1000",
1212
    "pcnet",
1213
    "virtio-net-pci",
1214
    NULL
1215
};
1216

    
1217
/* Initialize a PCI NIC.  */
1218
/* FIXME callers should check for failure, but don't */
1219
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
1220
                        const char *default_devaddr)
1221
{
1222
    const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1223
    PCIBus *bus;
1224
    int devfn;
1225
    PCIDevice *pci_dev;
1226
    DeviceState *dev;
1227
    int i;
1228

    
1229
    i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1230
    if (i < 0)
1231
        return NULL;
1232

    
1233
    bus = pci_get_bus_devfn(&devfn, devaddr);
1234
    if (!bus) {
1235
        qemu_error("Invalid PCI device address %s for device %s\n",
1236
                   devaddr, pci_nic_names[i]);
1237
        return NULL;
1238
    }
1239

    
1240
    pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1241
    dev = &pci_dev->qdev;
1242
    if (nd->name)
1243
        dev->id = qemu_strdup(nd->name);
1244
    qdev_set_nic_properties(dev, nd);
1245
    if (qdev_init(dev) < 0)
1246
        return NULL;
1247
    return pci_dev;
1248
}
1249

    
1250
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
1251
                               const char *default_devaddr)
1252
{
1253
    PCIDevice *res;
1254

    
1255
    if (qemu_show_nic_models(nd->model, pci_nic_models))
1256
        exit(0);
1257

    
1258
    res = pci_nic_init(nd, default_model, default_devaddr);
1259
    if (!res)
1260
        exit(1);
1261
    return res;
1262
}
1263

    
1264
typedef struct {
1265
    PCIDevice dev;
1266
    PCIBus bus;
1267
    uint32_t vid;
1268
    uint32_t did;
1269
} PCIBridge;
1270

    
1271

    
1272
static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d)
1273
{
1274
    pci_update_mappings(d);
1275
}
1276

    
1277
static void pci_bridge_update_mappings(PCIBus *b)
1278
{
1279
    PCIBus *child;
1280

    
1281
    pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn);
1282

    
1283
    QLIST_FOREACH(child, &b->child, sibling) {
1284
        pci_bridge_update_mappings(child);
1285
    }
1286
}
1287

    
1288
static void pci_bridge_write_config(PCIDevice *d,
1289
                             uint32_t address, uint32_t val, int len)
1290
{
1291
    pci_default_write_config(d, address, val, len);
1292

    
1293
    if (/* io base/limit */
1294
        ranges_overlap(address, len, PCI_IO_BASE, 2) ||
1295

    
1296
        /* memory base/limit, prefetchable base/limit and
1297
           io base/limit upper 16 */
1298
        ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
1299
        pci_bridge_update_mappings(d->bus);
1300
    }
1301
}
1302

    
1303
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
1304
{
1305
    PCIBus *sec;
1306

    
1307
    if (!bus)
1308
        return NULL;
1309

    
1310
    if (pci_bus_num(bus) == bus_num) {
1311
        return bus;
1312
    }
1313

    
1314
    /* try child bus */
1315
    QLIST_FOREACH(sec, &bus->child, sibling) {
1316

    
1317
        if (!bus->parent_dev /* pci host bridge */
1318
            || (pci_bus_num(sec) <= bus_num &&
1319
                bus->parent_dev->config[PCI_SUBORDINATE_BUS])) {
1320
            return pci_find_bus(sec, bus_num);
1321
        }
1322
    }
1323

    
1324
    return NULL;
1325
}
1326

    
1327
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
1328
{
1329
    bus = pci_find_bus(bus, bus_num);
1330

    
1331
    if (!bus)
1332
        return NULL;
1333

    
1334
    return bus->devices[PCI_DEVFN(slot, function)];
1335
}
1336

    
1337
static int pci_bridge_initfn(PCIDevice *dev)
1338
{
1339
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev);
1340

    
1341
    pci_config_set_vendor_id(s->dev.config, s->vid);
1342
    pci_config_set_device_id(s->dev.config, s->did);
1343

    
1344
    pci_set_word(dev->config + PCI_STATUS,
1345
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1346
    pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
1347
    dev->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_BRIDGE;
1348
    pci_set_word(dev->config + PCI_SEC_STATUS,
1349
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1350
    return 0;
1351
}
1352

    
1353
static int pci_bridge_exitfn(PCIDevice *pci_dev)
1354
{
1355
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
1356
    PCIBus *bus = &s->bus;
1357
    pci_unregister_secondary_bus(bus);
1358
    return 0;
1359
}
1360

    
1361
PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
1362
                        pci_map_irq_fn map_irq, const char *name)
1363
{
1364
    PCIDevice *dev;
1365
    PCIBridge *s;
1366

    
1367
    dev = pci_create(bus, devfn, "pci-bridge");
1368
    qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
1369
    qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
1370
    qdev_init_nofail(&dev->qdev);
1371

    
1372
    s = DO_UPCAST(PCIBridge, dev, dev);
1373
    pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name);
1374
    return &s->bus;
1375
}
1376

    
1377
PCIDevice *pci_bridge_get_device(PCIBus *bus)
1378
{
1379
    return bus->parent_dev;
1380
}
1381

    
1382
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1383
{
1384
    PCIDevice *pci_dev = (PCIDevice *)qdev;
1385
    PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1386
    PCIBus *bus;
1387
    int devfn, rc;
1388

    
1389
    /* initialize cap_present for pci_is_express() and pci_config_size() */
1390
    if (info->is_express) {
1391
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1392
    }
1393

    
1394
    bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1395
    devfn = pci_dev->devfn;
1396
    pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1397
                                     info->config_read, info->config_write,
1398
                                     info->header_type);
1399
    if (pci_dev == NULL)
1400
        return -1;
1401
    rc = info->init(pci_dev);
1402
    if (rc != 0)
1403
        return rc;
1404

    
1405
    /* rom loading */
1406
    if (pci_dev->romfile == NULL && info->romfile != NULL)
1407
        pci_dev->romfile = qemu_strdup(info->romfile);
1408
    pci_add_option_rom(pci_dev);
1409

    
1410
    if (qdev->hotplugged)
1411
        bus->hotplug(pci_dev, 1);
1412
    return 0;
1413
}
1414

    
1415
static int pci_unplug_device(DeviceState *qdev)
1416
{
1417
    PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1418

    
1419
    dev->bus->hotplug(dev, 0);
1420
    return 0;
1421
}
1422

    
1423
void pci_qdev_register(PCIDeviceInfo *info)
1424
{
1425
    info->qdev.init = pci_qdev_init;
1426
    info->qdev.unplug = pci_unplug_device;
1427
    info->qdev.exit = pci_unregister_device;
1428
    info->qdev.bus_info = &pci_bus_info;
1429
    qdev_register(&info->qdev);
1430
}
1431

    
1432
void pci_qdev_register_many(PCIDeviceInfo *info)
1433
{
1434
    while (info->qdev.name) {
1435
        pci_qdev_register(info);
1436
        info++;
1437
    }
1438
}
1439

    
1440
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1441
{
1442
    DeviceState *dev;
1443

    
1444
    dev = qdev_create(&bus->qbus, name);
1445
    qdev_prop_set_uint32(dev, "addr", devfn);
1446
    return DO_UPCAST(PCIDevice, qdev, dev);
1447
}
1448

    
1449
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1450
{
1451
    PCIDevice *dev = pci_create(bus, devfn, name);
1452
    qdev_init_nofail(&dev->qdev);
1453
    return dev;
1454
}
1455

    
1456
static int pci_find_space(PCIDevice *pdev, uint8_t size)
1457
{
1458
    int config_size = pci_config_size(pdev);
1459
    int offset = PCI_CONFIG_HEADER_SIZE;
1460
    int i;
1461
    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
1462
        if (pdev->used[i])
1463
            offset = i + 1;
1464
        else if (i - offset + 1 == size)
1465
            return offset;
1466
    return 0;
1467
}
1468

    
1469
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1470
                                        uint8_t *prev_p)
1471
{
1472
    uint8_t next, prev;
1473

    
1474
    if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1475
        return 0;
1476

    
1477
    for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1478
         prev = next + PCI_CAP_LIST_NEXT)
1479
        if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1480
            break;
1481

    
1482
    if (prev_p)
1483
        *prev_p = prev;
1484
    return next;
1485
}
1486

    
1487
static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type)
1488
{
1489
    cpu_register_physical_memory(addr, size, pdev->rom_offset);
1490
}
1491

    
1492
/* Add an option rom for the device */
1493
static int pci_add_option_rom(PCIDevice *pdev)
1494
{
1495
    int size;
1496
    char *path;
1497
    void *ptr;
1498

    
1499
    if (!pdev->romfile)
1500
        return 0;
1501
    if (strlen(pdev->romfile) == 0)
1502
        return 0;
1503

    
1504
    path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
1505
    if (path == NULL) {
1506
        path = qemu_strdup(pdev->romfile);
1507
    }
1508

    
1509
    size = get_image_size(path);
1510
    if (size < 0) {
1511
        qemu_error("%s: failed to find romfile \"%s\"\n", __FUNCTION__,
1512
                   pdev->romfile);
1513
        return -1;
1514
    }
1515
    if (size & (size - 1)) {
1516
        size = 1 << qemu_fls(size);
1517
    }
1518

    
1519
    pdev->rom_offset = qemu_ram_alloc(size);
1520

    
1521
    ptr = qemu_get_ram_ptr(pdev->rom_offset);
1522
    load_image(path, ptr);
1523
    qemu_free(path);
1524

    
1525
    pci_register_bar(pdev, PCI_ROM_SLOT, size,
1526
                     0, pci_map_option_rom);
1527

    
1528
    return 0;
1529
}
1530

    
1531
/* Reserve space and add capability to the linked list in pci config space */
1532
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1533
{
1534
    uint8_t offset = pci_find_space(pdev, size);
1535
    uint8_t *config = pdev->config + offset;
1536
    if (!offset)
1537
        return -ENOSPC;
1538
    config[PCI_CAP_LIST_ID] = cap_id;
1539
    config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1540
    pdev->config[PCI_CAPABILITY_LIST] = offset;
1541
    pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1542
    memset(pdev->used + offset, 0xFF, size);
1543
    /* Make capability read-only by default */
1544
    memset(pdev->wmask + offset, 0, size);
1545
    /* Check capability by default */
1546
    memset(pdev->cmask + offset, 0xFF, size);
1547
    return offset;
1548
}
1549

    
1550
/* Unlink capability from the pci config space. */
1551
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1552
{
1553
    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1554
    if (!offset)
1555
        return;
1556
    pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1557
    /* Make capability writeable again */
1558
    memset(pdev->wmask + offset, 0xff, size);
1559
    /* Clear cmask as device-specific registers can't be checked */
1560
    memset(pdev->cmask + offset, 0, size);
1561
    memset(pdev->used + offset, 0, size);
1562

    
1563
    if (!pdev->config[PCI_CAPABILITY_LIST])
1564
        pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1565
}
1566

    
1567
/* Reserve space for capability at a known offset (to call after load). */
1568
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1569
{
1570
    memset(pdev->used + offset, 0xff, size);
1571
}
1572

    
1573
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1574
{
1575
    return pci_find_capability_list(pdev, cap_id, NULL);
1576
}
1577

    
1578
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1579
{
1580
    PCIDevice *d = (PCIDevice *)dev;
1581
    const pci_class_desc *desc;
1582
    char ctxt[64];
1583
    PCIIORegion *r;
1584
    int i, class;
1585

    
1586
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1587
    desc = pci_class_descriptions;
1588
    while (desc->desc && class != desc->class)
1589
        desc++;
1590
    if (desc->desc) {
1591
        snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1592
    } else {
1593
        snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1594
    }
1595

    
1596
    monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1597
                   "pci id %04x:%04x (sub %04x:%04x)\n",
1598
                   indent, "", ctxt,
1599
                   d->config[PCI_SECONDARY_BUS],
1600
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1601
                   pci_get_word(d->config + PCI_VENDOR_ID),
1602
                   pci_get_word(d->config + PCI_DEVICE_ID),
1603
                   pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1604
                   pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1605
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
1606
        r = &d->io_regions[i];
1607
        if (!r->size)
1608
            continue;
1609
        monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1610
                       " [0x%"FMT_PCIBUS"]\n",
1611
                       indent, "",
1612
                       i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1613
                       r->addr, r->addr + r->size - 1);
1614
    }
1615
}
1616

    
1617
static PCIDeviceInfo bridge_info = {
1618
    .qdev.name    = "pci-bridge",
1619
    .qdev.size    = sizeof(PCIBridge),
1620
    .init         = pci_bridge_initfn,
1621
    .exit         = pci_bridge_exitfn,
1622
    .config_write = pci_bridge_write_config,
1623
    .qdev.props   = (Property[]) {
1624
        DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0),
1625
        DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0),
1626
        DEFINE_PROP_END_OF_LIST(),
1627
    }
1628
};
1629

    
1630
static void pci_register_devices(void)
1631
{
1632
    pci_qdev_register(&bridge_info);
1633
}
1634

    
1635
device_init(pci_register_devices)