Statistics
| Branch: | Revision:

root / hw / pci.c @ 070297d2

History | View | Annotate | Download (43.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 "pci_host.h"
27
#include "monitor.h"
28
#include "net.h"
29
#include "sysemu.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
    uint32_t config_reg; /* XXX: suppress */
45
    void *irq_opaque;
46
    PCIDevice *devices[256];
47
    PCIDevice *parent_dev;
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_END_OF_LIST()
67
    }
68
};
69

    
70
static void pci_update_mappings(PCIDevice *d);
71
static void pci_set_irq(void *opaque, int irq_num, int level);
72

    
73
target_phys_addr_t pci_mem_base;
74
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
75
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
76

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

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

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

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

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

    
107
static void pci_device_reset(PCIDevice *dev)
108
{
109
    int r;
110

    
111
    memset(dev->irq_state, 0, sizeof dev->irq_state);
112
    dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
113
                                  PCI_COMMAND_MASTER);
114
    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
115
    dev->config[PCI_INTERRUPT_LINE] = 0x0;
116
    for (r = 0; r < PCI_NUM_REGIONS; ++r) {
117
        if (!dev->io_regions[r].size) {
118
            continue;
119
        }
120
        pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type);
121
    }
122
    pci_update_mappings(dev);
123
}
124

    
125
static void pci_bus_reset(void *opaque)
126
{
127
    PCIBus *bus = opaque;
128
    int i;
129

    
130
    for (i = 0; i < bus->nirq; i++) {
131
        bus->irq_count[i] = 0;
132
    }
133
    for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
134
        if (bus->devices[i]) {
135
            pci_device_reset(bus->devices[i]);
136
        }
137
    }
138
}
139

    
140
static void pci_host_bus_register(int domain, PCIBus *bus)
141
{
142
    struct PCIHostBus *host;
143
    host = qemu_mallocz(sizeof(*host));
144
    host->domain = domain;
145
    host->bus = bus;
146
    QLIST_INSERT_HEAD(&host_buses, host, next);
147
}
148

    
149
PCIBus *pci_find_host_bus(int domain)
150
{
151
    struct PCIHostBus *host;
152

    
153
    QLIST_FOREACH(host, &host_buses, next) {
154
        if (host->domain == domain) {
155
            return host->bus;
156
        }
157
    }
158

    
159
    return NULL;
160
}
161

    
162
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
163
                         const char *name, int devfn_min)
164
{
165
    static int nbus = 0;
166

    
167
    qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
168
    bus->devfn_min = devfn_min;
169

    
170
    /* host bridge */
171
    QLIST_INIT(&bus->child);
172
    pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
173

    
174
    vmstate_register(nbus++, &vmstate_pcibus, bus);
175
    qemu_register_reset(pci_bus_reset, bus);
176
}
177

    
178
PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
179
{
180
    PCIBus *bus;
181

    
182
    bus = qemu_mallocz(sizeof(*bus));
183
    bus->qbus.qdev_allocated = 1;
184
    pci_bus_new_inplace(bus, parent, name, devfn_min);
185
    return bus;
186
}
187

    
188
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
189
                  void *irq_opaque, int nirq)
190
{
191
    bus->set_irq = set_irq;
192
    bus->map_irq = map_irq;
193
    bus->irq_opaque = irq_opaque;
194
    bus->nirq = nirq;
195
    bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
196
}
197

    
198
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
199
{
200
    bus->qbus.allow_hotplug = 1;
201
    bus->hotplug = hotplug;
202
}
203

    
204
PCIBus *pci_register_bus(DeviceState *parent, const char *name,
205
                         pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
206
                         void *irq_opaque, int devfn_min, int nirq)
207
{
208
    PCIBus *bus;
209

    
210
    bus = pci_bus_new(parent, name, devfn_min);
211
    pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
212
    return bus;
213
}
214

    
215
static void pci_register_secondary_bus(PCIBus *parent,
216
                                       PCIBus *bus,
217
                                       PCIDevice *dev,
218
                                       pci_map_irq_fn map_irq,
219
                                       const char *name)
220
{
221
    qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name);
222
    bus->map_irq = map_irq;
223
    bus->parent_dev = dev;
224

    
225
    QLIST_INIT(&bus->child);
226
    QLIST_INSERT_HEAD(&parent->child, bus, sibling);
227
}
228

    
229
static void pci_unregister_secondary_bus(PCIBus *bus)
230
{
231
    assert(QLIST_EMPTY(&bus->child));
232
    QLIST_REMOVE(bus, sibling);
233
}
234

    
235
int pci_bus_num(PCIBus *s)
236
{
237
    if (!s->parent_dev)
238
        return 0;       /* pci host bridge */
239
    return s->parent_dev->config[PCI_SECONDARY_BUS];
240
}
241

    
242
static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
243
{
244
    PCIDevice *s = container_of(pv, PCIDevice, config);
245
    uint8_t *config;
246
    int i;
247

    
248
    assert(size == pci_config_size(s));
249
    config = qemu_malloc(size);
250

    
251
    qemu_get_buffer(f, config, size);
252
    for (i = 0; i < size; ++i) {
253
        if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
254
            qemu_free(config);
255
            return -EINVAL;
256
        }
257
    }
258
    memcpy(s->config, config, size);
259

    
260
    pci_update_mappings(s);
261

    
262
    qemu_free(config);
263
    return 0;
264
}
265

    
266
/* just put buffer */
267
static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
268
{
269
    const uint8_t **v = pv;
270
    assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
271
    qemu_put_buffer(f, *v, size);
272
}
273

    
274
static VMStateInfo vmstate_info_pci_config = {
275
    .name = "pci config",
276
    .get  = get_pci_config_device,
277
    .put  = put_pci_config_device,
278
};
279

    
280
const VMStateDescription vmstate_pci_device = {
281
    .name = "PCIDevice",
282
    .version_id = 2,
283
    .minimum_version_id = 1,
284
    .minimum_version_id_old = 1,
285
    .fields      = (VMStateField []) {
286
        VMSTATE_INT32_LE(version_id, PCIDevice),
287
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
288
                                   vmstate_info_pci_config,
289
                                   PCI_CONFIG_SPACE_SIZE),
290
        VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
291
        VMSTATE_END_OF_LIST()
292
    }
293
};
294

    
295
const VMStateDescription vmstate_pcie_device = {
296
    .name = "PCIDevice",
297
    .version_id = 2,
298
    .minimum_version_id = 1,
299
    .minimum_version_id_old = 1,
300
    .fields      = (VMStateField []) {
301
        VMSTATE_INT32_LE(version_id, PCIDevice),
302
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
303
                                   vmstate_info_pci_config,
304
                                   PCIE_CONFIG_SPACE_SIZE),
305
        VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
306
        VMSTATE_END_OF_LIST()
307
    }
308
};
309

    
310
static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
311
{
312
    return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
313
}
314

    
315
void pci_device_save(PCIDevice *s, QEMUFile *f)
316
{
317
    vmstate_save_state(f, pci_get_vmstate(s), s);
318
}
319

    
320
int pci_device_load(PCIDevice *s, QEMUFile *f)
321
{
322
    return vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
323
}
324

    
325
static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
326
{
327
    uint16_t *id;
328

    
329
    id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
330
    id[0] = cpu_to_le16(pci_default_sub_vendor_id);
331
    id[1] = cpu_to_le16(pci_default_sub_device_id);
332
    return 0;
333
}
334

    
335
/*
336
 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
337
 */
338
static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
339
{
340
    const char *p;
341
    char *e;
342
    unsigned long val;
343
    unsigned long dom = 0, bus = 0;
344
    unsigned slot = 0;
345

    
346
    p = addr;
347
    val = strtoul(p, &e, 16);
348
    if (e == p)
349
        return -1;
350
    if (*e == ':') {
351
        bus = val;
352
        p = e + 1;
353
        val = strtoul(p, &e, 16);
354
        if (e == p)
355
            return -1;
356
        if (*e == ':') {
357
            dom = bus;
358
            bus = val;
359
            p = e + 1;
360
            val = strtoul(p, &e, 16);
361
            if (e == p)
362
                return -1;
363
        }
364
    }
365

    
366
    if (dom > 0xffff || bus > 0xff || val > 0x1f)
367
        return -1;
368

    
369
    slot = val;
370

    
371
    if (*e)
372
        return -1;
373

    
374
    /* Note: QEMU doesn't implement domains other than 0 */
375
    if (!pci_find_bus(pci_find_host_bus(dom), bus))
376
        return -1;
377

    
378
    *domp = dom;
379
    *busp = bus;
380
    *slotp = slot;
381
    return 0;
382
}
383

    
384
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
385
                     unsigned *slotp)
386
{
387
    /* strip legacy tag */
388
    if (!strncmp(addr, "pci_addr=", 9)) {
389
        addr += 9;
390
    }
391
    if (pci_parse_devaddr(addr, domp, busp, slotp)) {
392
        monitor_printf(mon, "Invalid pci address\n");
393
        return -1;
394
    }
395
    return 0;
396
}
397

    
398
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
399
{
400
    int dom, bus;
401
    unsigned slot;
402

    
403
    if (!devaddr) {
404
        *devfnp = -1;
405
        return pci_find_bus(pci_find_host_bus(0), 0);
406
    }
407

    
408
    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
409
        return NULL;
410
    }
411

    
412
    *devfnp = slot << 3;
413
    return pci_find_bus(pci_find_host_bus(0), bus);
414
}
415

    
416
static void pci_init_cmask(PCIDevice *dev)
417
{
418
    pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
419
    pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
420
    dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
421
    dev->cmask[PCI_REVISION_ID] = 0xff;
422
    dev->cmask[PCI_CLASS_PROG] = 0xff;
423
    pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
424
    dev->cmask[PCI_HEADER_TYPE] = 0xff;
425
    dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
426
}
427

    
428
static void pci_init_wmask(PCIDevice *dev)
429
{
430
    int i;
431
    int config_size = pci_config_size(dev);
432

    
433
    dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
434
    dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
435
    pci_set_word(dev->wmask + PCI_COMMAND,
436
                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
437
    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
438
        dev->wmask[i] = 0xff;
439
}
440

    
441
static void pci_init_wmask_bridge(PCIDevice *d)
442
{
443
    /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
444
       PCI_SEC_LETENCY_TIMER */
445
    memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
446

    
447
    /* base and limit */
448
    d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
449
    d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
450
    pci_set_word(d->wmask + PCI_MEMORY_BASE,
451
                 PCI_MEMORY_RANGE_MASK & 0xffff);
452
    pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
453
                 PCI_MEMORY_RANGE_MASK & 0xffff);
454
    pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
455
                 PCI_PREF_RANGE_MASK & 0xffff);
456
    pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
457
                 PCI_PREF_RANGE_MASK & 0xffff);
458

    
459
    /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
460
    memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
461

    
462
    pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
463
}
464

    
465
static void pci_config_alloc(PCIDevice *pci_dev)
466
{
467
    int config_size = pci_config_size(pci_dev);
468

    
469
    pci_dev->config = qemu_mallocz(config_size);
470
    pci_dev->cmask = qemu_mallocz(config_size);
471
    pci_dev->wmask = qemu_mallocz(config_size);
472
    pci_dev->used = qemu_mallocz(config_size);
473
}
474

    
475
static void pci_config_free(PCIDevice *pci_dev)
476
{
477
    qemu_free(pci_dev->config);
478
    qemu_free(pci_dev->cmask);
479
    qemu_free(pci_dev->wmask);
480
    qemu_free(pci_dev->used);
481
}
482

    
483
/* -1 for devfn means auto assign */
484
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
485
                                         const char *name, int devfn,
486
                                         PCIConfigReadFunc *config_read,
487
                                         PCIConfigWriteFunc *config_write,
488
                                         uint8_t header_type)
489
{
490
    if (devfn < 0) {
491
        for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
492
            if (!bus->devices[devfn])
493
                goto found;
494
        }
495
        hw_error("PCI: no devfn available for %s, all in use\n", name);
496
    found: ;
497
    } else if (bus->devices[devfn]) {
498
        hw_error("PCI: devfn %d not available for %s, in use by %s\n", devfn,
499
                 name, bus->devices[devfn]->name);
500
    }
501
    pci_dev->bus = bus;
502
    pci_dev->devfn = devfn;
503
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
504
    memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
505
    pci_config_alloc(pci_dev);
506

    
507
    header_type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
508
    if (header_type == PCI_HEADER_TYPE_NORMAL) {
509
        pci_set_default_subsystem_id(pci_dev);
510
    }
511
    pci_init_cmask(pci_dev);
512
    pci_init_wmask(pci_dev);
513
    if (header_type == PCI_HEADER_TYPE_BRIDGE) {
514
        pci_init_wmask_bridge(pci_dev);
515
    }
516

    
517
    if (!config_read)
518
        config_read = pci_default_read_config;
519
    if (!config_write)
520
        config_write = pci_default_write_config;
521
    pci_dev->config_read = config_read;
522
    pci_dev->config_write = config_write;
523
    bus->devices[devfn] = pci_dev;
524
    pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
525
    pci_dev->version_id = 2; /* Current pci device vmstate version */
526
    return pci_dev;
527
}
528

    
529
PCIDevice *pci_register_device(PCIBus *bus, const char *name,
530
                               int instance_size, int devfn,
531
                               PCIConfigReadFunc *config_read,
532
                               PCIConfigWriteFunc *config_write)
533
{
534
    PCIDevice *pci_dev;
535

    
536
    pci_dev = qemu_mallocz(instance_size);
537
    pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
538
                                     config_read, config_write,
539
                                     PCI_HEADER_TYPE_NORMAL);
540
    return pci_dev;
541
}
542
static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
543
{
544
    return addr + pci_mem_base;
545
}
546

    
547
static void pci_unregister_io_regions(PCIDevice *pci_dev)
548
{
549
    PCIIORegion *r;
550
    int i;
551

    
552
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
553
        r = &pci_dev->io_regions[i];
554
        if (!r->size || r->addr == PCI_BAR_UNMAPPED)
555
            continue;
556
        if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
557
            isa_unassign_ioport(r->addr, r->filtered_size);
558
        } else {
559
            cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
560
                                                     r->filtered_size,
561
                                                     IO_MEM_UNASSIGNED);
562
        }
563
    }
564
}
565

    
566
static int pci_unregister_device(DeviceState *dev)
567
{
568
    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
569
    PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
570
    int ret = 0;
571

    
572
    if (info->exit)
573
        ret = info->exit(pci_dev);
574
    if (ret)
575
        return ret;
576

    
577
    pci_unregister_io_regions(pci_dev);
578

    
579
    qemu_free_irqs(pci_dev->irq);
580
    pci_dev->bus->devices[pci_dev->devfn] = NULL;
581
    pci_config_free(pci_dev);
582
    return 0;
583
}
584

    
585
void pci_register_bar(PCIDevice *pci_dev, int region_num,
586
                            pcibus_t size, int type,
587
                            PCIMapIORegionFunc *map_func)
588
{
589
    PCIIORegion *r;
590
    uint32_t addr;
591
    pcibus_t wmask;
592

    
593
    if ((unsigned int)region_num >= PCI_NUM_REGIONS)
594
        return;
595

    
596
    if (size & (size-1)) {
597
        fprintf(stderr, "ERROR: PCI region size must be pow2 "
598
                    "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
599
        exit(1);
600
    }
601

    
602
    r = &pci_dev->io_regions[region_num];
603
    r->addr = PCI_BAR_UNMAPPED;
604
    r->size = size;
605
    r->filtered_size = size;
606
    r->type = type;
607
    r->map_func = map_func;
608

    
609
    wmask = ~(size - 1);
610
    addr = pci_bar(pci_dev, region_num);
611
    if (region_num == PCI_ROM_SLOT) {
612
        /* ROM enable bit is writeable */
613
        wmask |= PCI_ROM_ADDRESS_ENABLE;
614
    }
615
    pci_set_long(pci_dev->config + addr, type);
616
    if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
617
        r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
618
        pci_set_quad(pci_dev->wmask + addr, wmask);
619
        pci_set_quad(pci_dev->cmask + addr, ~0ULL);
620
    } else {
621
        pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
622
        pci_set_long(pci_dev->cmask + addr, 0xffffffff);
623
    }
624
}
625

    
626
static uint32_t pci_config_get_io_base(PCIDevice *d,
627
                                       uint32_t base, uint32_t base_upper16)
628
{
629
    uint32_t val;
630

    
631
    val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
632
    if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
633
        val |= (uint32_t)pci_get_word(d->config + PCI_IO_BASE_UPPER16) << 16;
634
    }
635
    return val;
636
}
637

    
638
static uint64_t pci_config_get_memory_base(PCIDevice *d, uint32_t base)
639
{
640
    return ((uint64_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
641
        << 16;
642
}
643

    
644
static uint64_t pci_config_get_pref_base(PCIDevice *d,
645
                                         uint32_t base, uint32_t upper)
646
{
647
    uint64_t val;
648
    val = ((uint64_t)pci_get_word(d->config + base) &
649
           PCI_PREF_RANGE_MASK) << 16;
650
    val |= (uint64_t)pci_get_long(d->config + upper) << 32;
651
    return val;
652
}
653

    
654
static pcibus_t pci_bridge_get_base(PCIDevice *bridge, uint8_t type)
655
{
656
    pcibus_t base;
657
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
658
        base = pci_config_get_io_base(bridge,
659
                                      PCI_IO_BASE, PCI_IO_BASE_UPPER16);
660
    } else {
661
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
662
            base = pci_config_get_pref_base(
663
                bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
664
        } else {
665
            base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
666
        }
667
    }
668

    
669
    return base;
670
}
671

    
672
static pcibus_t pci_bridge_get_limit(PCIDevice *bridge, uint8_t type)
673
{
674
    pcibus_t limit;
675
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
676
        limit = pci_config_get_io_base(bridge,
677
                                      PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
678
        limit |= 0xfff;         /* PCI bridge spec 3.2.5.6. */
679
    } else {
680
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
681
            limit = pci_config_get_pref_base(
682
                bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
683
        } else {
684
            limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
685
        }
686
        limit |= 0xfffff;       /* PCI bridge spec 3.2.5.{1, 8}. */
687
    }
688
    return limit;
689
}
690

    
691
static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size,
692
                              uint8_t type)
693
{
694
    pcibus_t base = *addr;
695
    pcibus_t limit = *addr + *size - 1;
696
    PCIDevice *br;
697

    
698
    for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
699
        uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
700

    
701
        if (type & PCI_BASE_ADDRESS_SPACE_IO) {
702
            if (!(cmd & PCI_COMMAND_IO)) {
703
                goto no_map;
704
            }
705
        } else {
706
            if (!(cmd & PCI_COMMAND_MEMORY)) {
707
                goto no_map;
708
            }
709
        }
710

    
711
        base = MAX(base, pci_bridge_get_base(br, type));
712
        limit = MIN(limit, pci_bridge_get_limit(br, type));
713
    }
714

    
715
    if (base > limit) {
716
    no_map:
717
        *addr = PCI_BAR_UNMAPPED;
718
        *size = 0;
719
    } else {
720
        *addr = base;
721
        *size = limit - base + 1;
722
    }
723
}
724

    
725
static void pci_update_mappings(PCIDevice *d)
726
{
727
    PCIIORegion *r;
728
    int cmd, i;
729
    pcibus_t last_addr, new_addr;
730
    pcibus_t filtered_size;
731

    
732
    cmd = pci_get_word(d->config + PCI_COMMAND);
733
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
734
        r = &d->io_regions[i];
735

    
736
        /* this region isn't registered */
737
        if (r->size == 0)
738
            continue;
739

    
740
        if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
741
            if (cmd & PCI_COMMAND_IO) {
742
                new_addr = pci_get_long(d->config + pci_bar(d, i));
743
                new_addr = new_addr & ~(r->size - 1);
744
                last_addr = new_addr + r->size - 1;
745
                /* NOTE: we have only 64K ioports on PC */
746
                if (last_addr <= new_addr || new_addr == 0 ||
747
                    last_addr >= 0x10000) {
748
                    new_addr = PCI_BAR_UNMAPPED;
749
                }
750
            } else {
751
                new_addr = PCI_BAR_UNMAPPED;
752
            }
753
        } else {
754
            if (cmd & PCI_COMMAND_MEMORY) {
755
                if (r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
756
                    new_addr = pci_get_quad(d->config + pci_bar(d, i));
757
                } else {
758
                    new_addr = pci_get_long(d->config + pci_bar(d, i));
759
                }
760
                /* the ROM slot has a specific enable bit */
761
                if (i == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE))
762
                    goto no_mem_map;
763
                new_addr = new_addr & ~(r->size - 1);
764
                last_addr = new_addr + r->size - 1;
765
                /* NOTE: we do not support wrapping */
766
                /* XXX: as we cannot support really dynamic
767
                   mappings, we handle specific values as invalid
768
                   mappings. */
769
                if (last_addr <= new_addr || new_addr == 0 ||
770
                    last_addr == PCI_BAR_UNMAPPED ||
771

    
772
                    /* Now pcibus_t is 64bit.
773
                     * Check if 32 bit BAR wrap around explicitly.
774
                     * Without this, PC ide doesn't work well.
775
                     * TODO: remove this work around.
776
                     */
777
                    (!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) &&
778
                     last_addr >= UINT32_MAX) ||
779

    
780
                    /*
781
                     * OS is allowed to set BAR beyond its addressable
782
                     * bits. For example, 32 bit OS can set 64bit bar
783
                     * to >4G. Check it.
784
                     */
785
                    last_addr >= TARGET_PHYS_ADDR_MAX) {
786
                    new_addr = PCI_BAR_UNMAPPED;
787
                }
788
            } else {
789
            no_mem_map:
790
                new_addr = PCI_BAR_UNMAPPED;
791
            }
792
        }
793

    
794
        /* bridge filtering */
795
        filtered_size = r->size;
796
        if (new_addr != PCI_BAR_UNMAPPED) {
797
            pci_bridge_filter(d, &new_addr, &filtered_size, r->type);
798
        }
799

    
800
        /* This bar isn't changed */
801
        if (new_addr == r->addr && filtered_size == r->filtered_size)
802
            continue;
803

    
804
        /* now do the real mapping */
805
        if (r->addr != PCI_BAR_UNMAPPED) {
806
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
807
                int class;
808
                /* NOTE: specific hack for IDE in PC case:
809
                   only one byte must be mapped. */
810
                class = pci_get_word(d->config + PCI_CLASS_DEVICE);
811
                if (class == 0x0101 && r->size == 4) {
812
                    isa_unassign_ioport(r->addr + 2, 1);
813
                } else {
814
                    isa_unassign_ioport(r->addr, r->filtered_size);
815
                }
816
            } else {
817
                cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
818
                                             r->filtered_size,
819
                                             IO_MEM_UNASSIGNED);
820
                qemu_unregister_coalesced_mmio(r->addr, r->filtered_size);
821
            }
822
        }
823
        r->addr = new_addr;
824
        r->filtered_size = filtered_size;
825
        if (r->addr != PCI_BAR_UNMAPPED) {
826
            /*
827
             * TODO: currently almost all the map funcions assumes
828
             * filtered_size == size and addr & ~(size - 1) == addr.
829
             * However with bridge filtering, they aren't always true.
830
             * Teach them such cases, such that filtered_size < size and
831
             * addr & (size - 1) != 0.
832
             */
833
            r->map_func(d, i, r->addr, r->filtered_size, r->type);
834
        }
835
    }
836
}
837

    
838
uint32_t pci_default_read_config(PCIDevice *d,
839
                                 uint32_t address, int len)
840
{
841
    uint32_t val = 0;
842
    assert(len == 1 || len == 2 || len == 4);
843
    len = MIN(len, pci_config_size(d) - address);
844
    memcpy(&val, d->config + address, len);
845
    return le32_to_cpu(val);
846
}
847

    
848
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
849
{
850
    int i;
851
    uint32_t config_size = pci_config_size(d);
852

    
853
    for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) {
854
        uint8_t wmask = d->wmask[addr + i];
855
        d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
856
    }
857
    if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
858
        ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
859
        ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
860
        range_covers_byte(addr, l, PCI_COMMAND))
861
        pci_update_mappings(d);
862
}
863

    
864
/***********************************************************/
865
/* generic PCI irq support */
866

    
867
/* 0 <= irq_num <= 3. level must be 0 or 1 */
868
static void pci_set_irq(void *opaque, int irq_num, int level)
869
{
870
    PCIDevice *pci_dev = opaque;
871
    PCIBus *bus;
872
    int change;
873

    
874
    change = level - pci_dev->irq_state[irq_num];
875
    if (!change)
876
        return;
877

    
878
    pci_dev->irq_state[irq_num] = level;
879
    for (;;) {
880
        bus = pci_dev->bus;
881
        irq_num = bus->map_irq(pci_dev, irq_num);
882
        if (bus->set_irq)
883
            break;
884
        pci_dev = bus->parent_dev;
885
    }
886
    bus->irq_count[irq_num] += change;
887
    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
888
}
889

    
890
/***********************************************************/
891
/* monitor info on PCI */
892

    
893
typedef struct {
894
    uint16_t class;
895
    const char *desc;
896
} pci_class_desc;
897

    
898
static const pci_class_desc pci_class_descriptions[] =
899
{
900
    { 0x0100, "SCSI controller"},
901
    { 0x0101, "IDE controller"},
902
    { 0x0102, "Floppy controller"},
903
    { 0x0103, "IPI controller"},
904
    { 0x0104, "RAID controller"},
905
    { 0x0106, "SATA controller"},
906
    { 0x0107, "SAS controller"},
907
    { 0x0180, "Storage controller"},
908
    { 0x0200, "Ethernet controller"},
909
    { 0x0201, "Token Ring controller"},
910
    { 0x0202, "FDDI controller"},
911
    { 0x0203, "ATM controller"},
912
    { 0x0280, "Network controller"},
913
    { 0x0300, "VGA controller"},
914
    { 0x0301, "XGA controller"},
915
    { 0x0302, "3D controller"},
916
    { 0x0380, "Display controller"},
917
    { 0x0400, "Video controller"},
918
    { 0x0401, "Audio controller"},
919
    { 0x0402, "Phone"},
920
    { 0x0480, "Multimedia controller"},
921
    { 0x0500, "RAM controller"},
922
    { 0x0501, "Flash controller"},
923
    { 0x0580, "Memory controller"},
924
    { 0x0600, "Host bridge"},
925
    { 0x0601, "ISA bridge"},
926
    { 0x0602, "EISA bridge"},
927
    { 0x0603, "MC bridge"},
928
    { 0x0604, "PCI bridge"},
929
    { 0x0605, "PCMCIA bridge"},
930
    { 0x0606, "NUBUS bridge"},
931
    { 0x0607, "CARDBUS bridge"},
932
    { 0x0608, "RACEWAY bridge"},
933
    { 0x0680, "Bridge"},
934
    { 0x0c03, "USB controller"},
935
    { 0, NULL}
936
};
937

    
938
static void pci_info_device(PCIBus *bus, PCIDevice *d)
939
{
940
    Monitor *mon = cur_mon;
941
    int i, class;
942
    PCIIORegion *r;
943
    const pci_class_desc *desc;
944

    
945
    monitor_printf(mon, "  Bus %2d, device %3d, function %d:\n",
946
                   pci_bus_num(d->bus),
947
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
948
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
949
    monitor_printf(mon, "    ");
950
    desc = pci_class_descriptions;
951
    while (desc->desc && class != desc->class)
952
        desc++;
953
    if (desc->desc) {
954
        monitor_printf(mon, "%s", desc->desc);
955
    } else {
956
        monitor_printf(mon, "Class %04x", class);
957
    }
958
    monitor_printf(mon, ": PCI device %04x:%04x\n",
959
           pci_get_word(d->config + PCI_VENDOR_ID),
960
           pci_get_word(d->config + PCI_DEVICE_ID));
961

    
962
    if (d->config[PCI_INTERRUPT_PIN] != 0) {
963
        monitor_printf(mon, "      IRQ %d.\n",
964
                       d->config[PCI_INTERRUPT_LINE]);
965
    }
966
    if (class == 0x0604) {
967
        uint64_t base;
968
        uint64_t limit;
969

    
970
        monitor_printf(mon, "      BUS %d.\n", d->config[0x19]);
971
        monitor_printf(mon, "      secondary bus %d.\n",
972
                       d->config[PCI_SECONDARY_BUS]);
973
        monitor_printf(mon, "      subordinate bus %d.\n",
974
                       d->config[PCI_SUBORDINATE_BUS]);
975

    
976
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_IO);
977
        limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_IO);
978
        monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
979
                       base, limit);
980

    
981
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY);
982
        limit= pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY);
983
        monitor_printf(mon,
984
                       "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
985
                       base, limit);
986

    
987
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY |
988
                                   PCI_BASE_ADDRESS_MEM_PREFETCH);
989
        limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY |
990
                                     PCI_BASE_ADDRESS_MEM_PREFETCH);
991
        monitor_printf(mon, "      prefetchable memory range "
992
                       "[0x%08"PRIx64", 0x%08"PRIx64"]\n", base, limit);
993
    }
994
    for(i = 0;i < PCI_NUM_REGIONS; i++) {
995
        r = &d->io_regions[i];
996
        if (r->size != 0) {
997
            monitor_printf(mon, "      BAR%d: ", i);
998
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
999
                monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
1000
                               " [0x%04"FMT_PCIBUS"].\n",
1001
                               r->addr, r->addr + r->size - 1);
1002
            } else {
1003
                const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ?
1004
                    "64 bit" : "32 bit";
1005
                const char *prefetch =
1006
                    r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ?
1007
                    " prefetchable" : "";
1008

    
1009
                monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
1010
                               " [0x%08"FMT_PCIBUS"].\n",
1011
                               type, prefetch,
1012
                               r->addr, r->addr + r->size - 1);
1013
            }
1014
        }
1015
    }
1016
    monitor_printf(mon, "      id \"%s\"\n", d->qdev.id ? d->qdev.id : "");
1017
    if (class == 0x0604 && d->config[0x19] != 0) {
1018
        pci_for_each_device(bus, d->config[0x19], pci_info_device);
1019
    }
1020
}
1021

    
1022
static void pci_for_each_device_under_bus(PCIBus *bus,
1023
                                          void (*fn)(PCIBus *b, PCIDevice *d))
1024
{
1025
    PCIDevice *d;
1026
    int devfn;
1027

    
1028
    for(devfn = 0; devfn < 256; devfn++) {
1029
        d = bus->devices[devfn];
1030
        if (d)
1031
            fn(bus, d);
1032
    }
1033
}
1034

    
1035
void pci_for_each_device(PCIBus *bus, int bus_num,
1036
                         void (*fn)(PCIBus *b, PCIDevice *d))
1037
{
1038
    bus = pci_find_bus(bus, bus_num);
1039

    
1040
    if (bus) {
1041
        pci_for_each_device_under_bus(bus, fn);
1042
    }
1043
}
1044

    
1045
void pci_info(Monitor *mon)
1046
{
1047
    struct PCIHostBus *host;
1048
    QLIST_FOREACH(host, &host_buses, next) {
1049
        pci_for_each_device(host->bus, 0, pci_info_device);
1050
    }
1051
}
1052

    
1053
static const char * const pci_nic_models[] = {
1054
    "ne2k_pci",
1055
    "i82551",
1056
    "i82557b",
1057
    "i82559er",
1058
    "rtl8139",
1059
    "e1000",
1060
    "pcnet",
1061
    "virtio",
1062
    NULL
1063
};
1064

    
1065
static const char * const pci_nic_names[] = {
1066
    "ne2k_pci",
1067
    "i82551",
1068
    "i82557b",
1069
    "i82559er",
1070
    "rtl8139",
1071
    "e1000",
1072
    "pcnet",
1073
    "virtio-net-pci",
1074
    NULL
1075
};
1076

    
1077
/* Initialize a PCI NIC.  */
1078
/* FIXME callers should check for failure, but don't */
1079
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
1080
                        const char *default_devaddr)
1081
{
1082
    const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1083
    PCIBus *bus;
1084
    int devfn;
1085
    PCIDevice *pci_dev;
1086
    DeviceState *dev;
1087
    int i;
1088

    
1089
    i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1090
    if (i < 0)
1091
        return NULL;
1092

    
1093
    bus = pci_get_bus_devfn(&devfn, devaddr);
1094
    if (!bus) {
1095
        qemu_error("Invalid PCI device address %s for device %s\n",
1096
                   devaddr, pci_nic_names[i]);
1097
        return NULL;
1098
    }
1099

    
1100
    pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1101
    dev = &pci_dev->qdev;
1102
    if (nd->name)
1103
        dev->id = qemu_strdup(nd->name);
1104
    qdev_set_nic_properties(dev, nd);
1105
    if (qdev_init(dev) < 0)
1106
        return NULL;
1107
    return pci_dev;
1108
}
1109

    
1110
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
1111
                               const char *default_devaddr)
1112
{
1113
    PCIDevice *res;
1114

    
1115
    if (qemu_show_nic_models(nd->model, pci_nic_models))
1116
        exit(0);
1117

    
1118
    res = pci_nic_init(nd, default_model, default_devaddr);
1119
    if (!res)
1120
        exit(1);
1121
    return res;
1122
}
1123

    
1124
typedef struct {
1125
    PCIDevice dev;
1126
    PCIBus bus;
1127
    uint32_t vid;
1128
    uint32_t did;
1129
} PCIBridge;
1130

    
1131

    
1132
static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d)
1133
{
1134
    pci_update_mappings(d);
1135
}
1136

    
1137
static void pci_bridge_update_mappings(PCIBus *b)
1138
{
1139
    PCIBus *child;
1140

    
1141
    pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn);
1142

    
1143
    QLIST_FOREACH(child, &b->child, sibling) {
1144
        pci_bridge_update_mappings(child);
1145
    }
1146
}
1147

    
1148
static void pci_bridge_write_config(PCIDevice *d,
1149
                             uint32_t address, uint32_t val, int len)
1150
{
1151
    pci_default_write_config(d, address, val, len);
1152

    
1153
    if (/* io base/limit */
1154
        ranges_overlap(address, len, PCI_IO_BASE, 2) ||
1155

    
1156
        /* memory base/limit, prefetchable base/limit and
1157
           io base/limit upper 16 */
1158
        ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
1159
        pci_bridge_update_mappings(d->bus);
1160
    }
1161
}
1162

    
1163
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
1164
{
1165
    PCIBus *sec;
1166

    
1167
    if (!bus)
1168
        return NULL;
1169

    
1170
    if (pci_bus_num(bus) == bus_num) {
1171
        return bus;
1172
    }
1173

    
1174
    /* try child bus */
1175
    QLIST_FOREACH(sec, &bus->child, sibling) {
1176

    
1177
        if (!bus->parent_dev /* pci host bridge */
1178
            || (pci_bus_num(sec) <= bus_num &&
1179
                bus->parent_dev->config[PCI_SUBORDINATE_BUS])) {
1180
            return pci_find_bus(sec, bus_num);
1181
        }
1182
    }
1183

    
1184
    return NULL;
1185
}
1186

    
1187
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
1188
{
1189
    bus = pci_find_bus(bus, bus_num);
1190

    
1191
    if (!bus)
1192
        return NULL;
1193

    
1194
    return bus->devices[PCI_DEVFN(slot, function)];
1195
}
1196

    
1197
static int pci_bridge_initfn(PCIDevice *dev)
1198
{
1199
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev);
1200

    
1201
    pci_config_set_vendor_id(s->dev.config, s->vid);
1202
    pci_config_set_device_id(s->dev.config, s->did);
1203

    
1204
    /* TODO: intial value
1205
     * command register:
1206
     * According to PCI bridge spec, after reset
1207
     *   bus master bit is off
1208
     *   memory space enable bit is off
1209
     * According to manual (805-1251.pdf).(See abp_pbi.c for its links.)
1210
     *   the reset value should be zero unless the boot pin is tied high
1211
     *   (which is tru) and thus it should be PCI_COMMAND_MEMORY.
1212
     *
1213
     * For now, don't touch the value.
1214
     * Later command register will be set to zero and apb_pci.c will
1215
     * override the value.
1216
     * Same for latency timer, and multi function bit of header type.
1217
     */
1218
    pci_set_word(dev->config + PCI_COMMAND,
1219
                 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1220

    
1221
    pci_set_word(dev->config + PCI_STATUS,
1222
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1223
    pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
1224
    dev->config[PCI_LATENCY_TIMER] = 0x10;
1225
    dev->config[PCI_HEADER_TYPE] =
1226
        PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE;
1227
    pci_set_word(dev->config + PCI_SEC_STATUS,
1228
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1229
    return 0;
1230
}
1231

    
1232
static int pci_bridge_exitfn(PCIDevice *pci_dev)
1233
{
1234
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
1235
    PCIBus *bus = &s->bus;
1236
    pci_unregister_secondary_bus(bus);
1237
    return 0;
1238
}
1239

    
1240
PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
1241
                        pci_map_irq_fn map_irq, const char *name)
1242
{
1243
    PCIDevice *dev;
1244
    PCIBridge *s;
1245

    
1246
    dev = pci_create(bus, devfn, "pci-bridge");
1247
    qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
1248
    qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
1249
    qdev_init_nofail(&dev->qdev);
1250

    
1251
    s = DO_UPCAST(PCIBridge, dev, dev);
1252
    pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name);
1253
    return &s->bus;
1254
}
1255

    
1256
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1257
{
1258
    PCIDevice *pci_dev = (PCIDevice *)qdev;
1259
    PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1260
    PCIBus *bus;
1261
    int devfn, rc;
1262

    
1263
    /* initialize cap_present for pci_is_express() and pci_config_size() */
1264
    if (info->is_express) {
1265
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1266
    }
1267

    
1268
    bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1269
    devfn = pci_dev->devfn;
1270
    pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1271
                                     info->config_read, info->config_write,
1272
                                     info->header_type);
1273
    rc = info->init(pci_dev);
1274
    if (rc != 0)
1275
        return rc;
1276
    if (qdev->hotplugged)
1277
        bus->hotplug(pci_dev, 1);
1278
    return 0;
1279
}
1280

    
1281
static int pci_unplug_device(DeviceState *qdev)
1282
{
1283
    PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1284

    
1285
    dev->bus->hotplug(dev, 0);
1286
    return 0;
1287
}
1288

    
1289
void pci_qdev_register(PCIDeviceInfo *info)
1290
{
1291
    info->qdev.init = pci_qdev_init;
1292
    info->qdev.unplug = pci_unplug_device;
1293
    info->qdev.exit = pci_unregister_device;
1294
    info->qdev.bus_info = &pci_bus_info;
1295
    qdev_register(&info->qdev);
1296
}
1297

    
1298
void pci_qdev_register_many(PCIDeviceInfo *info)
1299
{
1300
    while (info->qdev.name) {
1301
        pci_qdev_register(info);
1302
        info++;
1303
    }
1304
}
1305

    
1306
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1307
{
1308
    DeviceState *dev;
1309

    
1310
    dev = qdev_create(&bus->qbus, name);
1311
    qdev_prop_set_uint32(dev, "addr", devfn);
1312
    return DO_UPCAST(PCIDevice, qdev, dev);
1313
}
1314

    
1315
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1316
{
1317
    PCIDevice *dev = pci_create(bus, devfn, name);
1318
    qdev_init_nofail(&dev->qdev);
1319
    return dev;
1320
}
1321

    
1322
static int pci_find_space(PCIDevice *pdev, uint8_t size)
1323
{
1324
    int config_size = pci_config_size(pdev);
1325
    int offset = PCI_CONFIG_HEADER_SIZE;
1326
    int i;
1327
    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
1328
        if (pdev->used[i])
1329
            offset = i + 1;
1330
        else if (i - offset + 1 == size)
1331
            return offset;
1332
    return 0;
1333
}
1334

    
1335
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1336
                                        uint8_t *prev_p)
1337
{
1338
    uint8_t next, prev;
1339

    
1340
    if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1341
        return 0;
1342

    
1343
    for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1344
         prev = next + PCI_CAP_LIST_NEXT)
1345
        if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1346
            break;
1347

    
1348
    if (prev_p)
1349
        *prev_p = prev;
1350
    return next;
1351
}
1352

    
1353
/* Reserve space and add capability to the linked list in pci config space */
1354
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1355
{
1356
    uint8_t offset = pci_find_space(pdev, size);
1357
    uint8_t *config = pdev->config + offset;
1358
    if (!offset)
1359
        return -ENOSPC;
1360
    config[PCI_CAP_LIST_ID] = cap_id;
1361
    config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1362
    pdev->config[PCI_CAPABILITY_LIST] = offset;
1363
    pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1364
    memset(pdev->used + offset, 0xFF, size);
1365
    /* Make capability read-only by default */
1366
    memset(pdev->wmask + offset, 0, size);
1367
    /* Check capability by default */
1368
    memset(pdev->cmask + offset, 0xFF, size);
1369
    return offset;
1370
}
1371

    
1372
/* Unlink capability from the pci config space. */
1373
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1374
{
1375
    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1376
    if (!offset)
1377
        return;
1378
    pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1379
    /* Make capability writeable again */
1380
    memset(pdev->wmask + offset, 0xff, size);
1381
    /* Clear cmask as device-specific registers can't be checked */
1382
    memset(pdev->cmask + offset, 0, size);
1383
    memset(pdev->used + offset, 0, size);
1384

    
1385
    if (!pdev->config[PCI_CAPABILITY_LIST])
1386
        pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1387
}
1388

    
1389
/* Reserve space for capability at a known offset (to call after load). */
1390
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1391
{
1392
    memset(pdev->used + offset, 0xff, size);
1393
}
1394

    
1395
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1396
{
1397
    return pci_find_capability_list(pdev, cap_id, NULL);
1398
}
1399

    
1400
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1401
{
1402
    PCIDevice *d = (PCIDevice *)dev;
1403
    const pci_class_desc *desc;
1404
    char ctxt[64];
1405
    PCIIORegion *r;
1406
    int i, class;
1407

    
1408
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1409
    desc = pci_class_descriptions;
1410
    while (desc->desc && class != desc->class)
1411
        desc++;
1412
    if (desc->desc) {
1413
        snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1414
    } else {
1415
        snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1416
    }
1417

    
1418
    monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1419
                   "pci id %04x:%04x (sub %04x:%04x)\n",
1420
                   indent, "", ctxt,
1421
                   d->config[PCI_SECONDARY_BUS],
1422
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1423
                   pci_get_word(d->config + PCI_VENDOR_ID),
1424
                   pci_get_word(d->config + PCI_DEVICE_ID),
1425
                   pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1426
                   pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1427
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
1428
        r = &d->io_regions[i];
1429
        if (!r->size)
1430
            continue;
1431
        monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1432
                       " [0x%"FMT_PCIBUS"]\n",
1433
                       indent, "",
1434
                       i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1435
                       r->addr, r->addr + r->size - 1);
1436
    }
1437
}
1438

    
1439
static PCIDeviceInfo bridge_info = {
1440
    .qdev.name    = "pci-bridge",
1441
    .qdev.size    = sizeof(PCIBridge),
1442
    .init         = pci_bridge_initfn,
1443
    .exit         = pci_bridge_exitfn,
1444
    .config_write = pci_bridge_write_config,
1445
    .qdev.props   = (Property[]) {
1446
        DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0),
1447
        DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0),
1448
        DEFINE_PROP_END_OF_LIST(),
1449
    }
1450
};
1451

    
1452
static void pci_register_devices(void)
1453
{
1454
    pci_qdev_register(&bridge_info);
1455
}
1456

    
1457
device_init(pci_register_devices)