Statistics
| Branch: | Revision:

root / hw / pci.c @ e327e323

History | View | Annotate | Download (55.9 kB)

1 69b91039 bellard
/*
2 69b91039 bellard
 * QEMU PCI bus manager
3 69b91039 bellard
 *
4 69b91039 bellard
 * Copyright (c) 2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 69b91039 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 69b91039 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 69b91039 bellard
 * in the Software without restriction, including without limitation the rights
9 69b91039 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 69b91039 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 69b91039 bellard
 * furnished to do so, subject to the following conditions:
12 69b91039 bellard
 *
13 69b91039 bellard
 * The above copyright notice and this permission notice shall be included in
14 69b91039 bellard
 * all copies or substantial portions of the Software.
15 69b91039 bellard
 *
16 69b91039 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 69b91039 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 69b91039 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 69b91039 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 69b91039 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 69b91039 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 69b91039 bellard
 * THE SOFTWARE.
23 69b91039 bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "pci.h"
26 376253ec aliguori
#include "monitor.h"
27 87ecb68b pbrook
#include "net.h"
28 880345c4 aliguori
#include "sysemu.h"
29 c2039bd0 Anthony Liguori
#include "loader.h"
30 163c8a59 Luiz Capitulino
#include "qemu-objects.h"
31 69b91039 bellard
32 69b91039 bellard
//#define DEBUG_PCI
33 d8d2e079 Isaku Yamahata
#ifdef DEBUG_PCI
34 2e49d64a Isaku Yamahata
# define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
35 d8d2e079 Isaku Yamahata
#else
36 d8d2e079 Isaku Yamahata
# define PCI_DPRINTF(format, ...)       do { } while (0)
37 d8d2e079 Isaku Yamahata
#endif
38 69b91039 bellard
39 30468f78 bellard
struct PCIBus {
40 02e2da45 Paul Brook
    BusState qbus;
41 30468f78 bellard
    int devfn_min;
42 502a5395 pbrook
    pci_set_irq_fn set_irq;
43 d2b59317 pbrook
    pci_map_irq_fn map_irq;
44 ee995ffb Gerd Hoffmann
    pci_hotplug_fn hotplug;
45 87c30546 Isaku Yamahata
    DeviceState *hotplug_qdev;
46 5d4e84c8 Juan Quintela
    void *irq_opaque;
47 30468f78 bellard
    PCIDevice *devices[256];
48 80b3ada7 pbrook
    PCIDevice *parent_dev;
49 2e01c8cf Blue Swirl
    target_phys_addr_t mem_base;
50 e822a52a Isaku Yamahata
51 e822a52a Isaku Yamahata
    QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
52 e822a52a Isaku Yamahata
    QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
53 e822a52a Isaku Yamahata
54 d2b59317 pbrook
    /* The bus IRQ state is the logical OR of the connected devices.
55 d2b59317 pbrook
       Keep a count of the number of devices with raised IRQs.  */
56 52fc1d83 balrog
    int nirq;
57 10c4c98a Gerd Hoffmann
    int *irq_count;
58 10c4c98a Gerd Hoffmann
};
59 10c4c98a Gerd Hoffmann
60 10c4c98a Gerd Hoffmann
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
61 4f43c1ff Alex Williamson
static char *pcibus_get_dev_path(DeviceState *dev);
62 10c4c98a Gerd Hoffmann
63 10c4c98a Gerd Hoffmann
static struct BusInfo pci_bus_info = {
64 10c4c98a Gerd Hoffmann
    .name       = "PCI",
65 10c4c98a Gerd Hoffmann
    .size       = sizeof(PCIBus),
66 10c4c98a Gerd Hoffmann
    .print_dev  = pcibus_dev_print,
67 4f43c1ff Alex Williamson
    .get_dev_path = pcibus_get_dev_path,
68 ee6847d1 Gerd Hoffmann
    .props      = (Property[]) {
69 54586bd1 Gerd Hoffmann
        DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
70 8c52c8f3 Gerd Hoffmann
        DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
71 88169ddf Gerd Hoffmann
        DEFINE_PROP_UINT32("rombar",  PCIDevice, rom_bar, 1),
72 54586bd1 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST()
73 ee6847d1 Gerd Hoffmann
    }
74 30468f78 bellard
};
75 69b91039 bellard
76 1941d19c bellard
static void pci_update_mappings(PCIDevice *d);
77 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level);
78 8c52c8f3 Gerd Hoffmann
static int pci_add_option_rom(PCIDevice *pdev);
79 230741dc Alex Williamson
static void pci_del_option_rom(PCIDevice *pdev);
80 1941d19c bellard
81 d350d97d aliguori
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
82 d350d97d aliguori
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
83 e822a52a Isaku Yamahata
84 e822a52a Isaku Yamahata
struct PCIHostBus {
85 e822a52a Isaku Yamahata
    int domain;
86 e822a52a Isaku Yamahata
    struct PCIBus *bus;
87 e822a52a Isaku Yamahata
    QLIST_ENTRY(PCIHostBus) next;
88 e822a52a Isaku Yamahata
};
89 e822a52a Isaku Yamahata
static QLIST_HEAD(, PCIHostBus) host_buses;
90 30468f78 bellard
91 2d1e9f96 Juan Quintela
static const VMStateDescription vmstate_pcibus = {
92 2d1e9f96 Juan Quintela
    .name = "PCIBUS",
93 2d1e9f96 Juan Quintela
    .version_id = 1,
94 2d1e9f96 Juan Quintela
    .minimum_version_id = 1,
95 2d1e9f96 Juan Quintela
    .minimum_version_id_old = 1,
96 2d1e9f96 Juan Quintela
    .fields      = (VMStateField []) {
97 2d1e9f96 Juan Quintela
        VMSTATE_INT32_EQUAL(nirq, PCIBus),
98 c7bde572 Juan Quintela
        VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
99 2d1e9f96 Juan Quintela
        VMSTATE_END_OF_LIST()
100 52fc1d83 balrog
    }
101 2d1e9f96 Juan Quintela
};
102 52fc1d83 balrog
103 b3b11697 Isaku Yamahata
static int pci_bar(PCIDevice *d, int reg)
104 5330de09 Michael S. Tsirkin
{
105 b3b11697 Isaku Yamahata
    uint8_t type;
106 b3b11697 Isaku Yamahata
107 b3b11697 Isaku Yamahata
    if (reg != PCI_ROM_SLOT)
108 b3b11697 Isaku Yamahata
        return PCI_BASE_ADDRESS_0 + reg * 4;
109 b3b11697 Isaku Yamahata
110 b3b11697 Isaku Yamahata
    type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
111 b3b11697 Isaku Yamahata
    return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
112 5330de09 Michael S. Tsirkin
}
113 5330de09 Michael S. Tsirkin
114 d036bb21 Michael S. Tsirkin
static inline int pci_irq_state(PCIDevice *d, int irq_num)
115 d036bb21 Michael S. Tsirkin
{
116 d036bb21 Michael S. Tsirkin
        return (d->irq_state >> irq_num) & 0x1;
117 d036bb21 Michael S. Tsirkin
}
118 d036bb21 Michael S. Tsirkin
119 d036bb21 Michael S. Tsirkin
static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
120 d036bb21 Michael S. Tsirkin
{
121 d036bb21 Michael S. Tsirkin
        d->irq_state &= ~(0x1 << irq_num);
122 d036bb21 Michael S. Tsirkin
        d->irq_state |= level << irq_num;
123 d036bb21 Michael S. Tsirkin
}
124 d036bb21 Michael S. Tsirkin
125 d036bb21 Michael S. Tsirkin
static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
126 d036bb21 Michael S. Tsirkin
{
127 d036bb21 Michael S. Tsirkin
    PCIBus *bus;
128 d036bb21 Michael S. Tsirkin
    for (;;) {
129 d036bb21 Michael S. Tsirkin
        bus = pci_dev->bus;
130 d036bb21 Michael S. Tsirkin
        irq_num = bus->map_irq(pci_dev, irq_num);
131 d036bb21 Michael S. Tsirkin
        if (bus->set_irq)
132 d036bb21 Michael S. Tsirkin
            break;
133 d036bb21 Michael S. Tsirkin
        pci_dev = bus->parent_dev;
134 d036bb21 Michael S. Tsirkin
    }
135 d036bb21 Michael S. Tsirkin
    bus->irq_count[irq_num] += change;
136 d036bb21 Michael S. Tsirkin
    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
137 d036bb21 Michael S. Tsirkin
}
138 d036bb21 Michael S. Tsirkin
139 f9bf77dd Michael S. Tsirkin
/* Update interrupt status bit in config space on interrupt
140 f9bf77dd Michael S. Tsirkin
 * state change. */
141 f9bf77dd Michael S. Tsirkin
static void pci_update_irq_status(PCIDevice *dev)
142 f9bf77dd Michael S. Tsirkin
{
143 f9bf77dd Michael S. Tsirkin
    if (dev->irq_state) {
144 f9bf77dd Michael S. Tsirkin
        dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
145 f9bf77dd Michael S. Tsirkin
    } else {
146 f9bf77dd Michael S. Tsirkin
        dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
147 f9bf77dd Michael S. Tsirkin
    }
148 f9bf77dd Michael S. Tsirkin
}
149 f9bf77dd Michael S. Tsirkin
150 5330de09 Michael S. Tsirkin
static void pci_device_reset(PCIDevice *dev)
151 5330de09 Michael S. Tsirkin
{
152 c0b1905b Michael S. Tsirkin
    int r;
153 c0b1905b Michael S. Tsirkin
154 d036bb21 Michael S. Tsirkin
    dev->irq_state = 0;
155 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(dev);
156 c0b1905b Michael S. Tsirkin
    dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
157 c0b1905b Michael S. Tsirkin
                                  PCI_COMMAND_MASTER);
158 c0b1905b Michael S. Tsirkin
    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
159 c0b1905b Michael S. Tsirkin
    dev->config[PCI_INTERRUPT_LINE] = 0x0;
160 c0b1905b Michael S. Tsirkin
    for (r = 0; r < PCI_NUM_REGIONS; ++r) {
161 c0b1905b Michael S. Tsirkin
        if (!dev->io_regions[r].size) {
162 c0b1905b Michael S. Tsirkin
            continue;
163 c0b1905b Michael S. Tsirkin
        }
164 b3b11697 Isaku Yamahata
        pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type);
165 c0b1905b Michael S. Tsirkin
    }
166 c0b1905b Michael S. Tsirkin
    pci_update_mappings(dev);
167 5330de09 Michael S. Tsirkin
}
168 5330de09 Michael S. Tsirkin
169 6eaa6847 Gleb Natapov
static void pci_bus_reset(void *opaque)
170 6eaa6847 Gleb Natapov
{
171 a60380a5 Juan Quintela
    PCIBus *bus = opaque;
172 6eaa6847 Gleb Natapov
    int i;
173 6eaa6847 Gleb Natapov
174 6eaa6847 Gleb Natapov
    for (i = 0; i < bus->nirq; i++) {
175 6eaa6847 Gleb Natapov
        bus->irq_count[i] = 0;
176 6eaa6847 Gleb Natapov
    }
177 5330de09 Michael S. Tsirkin
    for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
178 5330de09 Michael S. Tsirkin
        if (bus->devices[i]) {
179 5330de09 Michael S. Tsirkin
            pci_device_reset(bus->devices[i]);
180 5330de09 Michael S. Tsirkin
        }
181 6eaa6847 Gleb Natapov
    }
182 6eaa6847 Gleb Natapov
}
183 6eaa6847 Gleb Natapov
184 e822a52a Isaku Yamahata
static void pci_host_bus_register(int domain, PCIBus *bus)
185 e822a52a Isaku Yamahata
{
186 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
187 e822a52a Isaku Yamahata
    host = qemu_mallocz(sizeof(*host));
188 e822a52a Isaku Yamahata
    host->domain = domain;
189 e822a52a Isaku Yamahata
    host->bus = bus;
190 e822a52a Isaku Yamahata
    QLIST_INSERT_HEAD(&host_buses, host, next);
191 e822a52a Isaku Yamahata
}
192 e822a52a Isaku Yamahata
193 c469e1dd Isaku Yamahata
PCIBus *pci_find_root_bus(int domain)
194 e822a52a Isaku Yamahata
{
195 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
196 e822a52a Isaku Yamahata
197 e822a52a Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
198 e822a52a Isaku Yamahata
        if (host->domain == domain) {
199 e822a52a Isaku Yamahata
            return host->bus;
200 e822a52a Isaku Yamahata
        }
201 e822a52a Isaku Yamahata
    }
202 e822a52a Isaku Yamahata
203 e822a52a Isaku Yamahata
    return NULL;
204 e822a52a Isaku Yamahata
}
205 e822a52a Isaku Yamahata
206 e075e788 Isaku Yamahata
int pci_find_domain(const PCIBus *bus)
207 e075e788 Isaku Yamahata
{
208 e075e788 Isaku Yamahata
    PCIDevice *d;
209 e075e788 Isaku Yamahata
    struct PCIHostBus *host;
210 e075e788 Isaku Yamahata
211 e075e788 Isaku Yamahata
    /* obtain root bus */
212 e075e788 Isaku Yamahata
    while ((d = bus->parent_dev) != NULL) {
213 e075e788 Isaku Yamahata
        bus = d->bus;
214 e075e788 Isaku Yamahata
    }
215 e075e788 Isaku Yamahata
216 e075e788 Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
217 e075e788 Isaku Yamahata
        if (host->bus == bus) {
218 e075e788 Isaku Yamahata
            return host->domain;
219 e075e788 Isaku Yamahata
        }
220 e075e788 Isaku Yamahata
    }
221 e075e788 Isaku Yamahata
222 e075e788 Isaku Yamahata
    abort();    /* should not be reached */
223 e075e788 Isaku Yamahata
    return -1;
224 e075e788 Isaku Yamahata
}
225 e075e788 Isaku Yamahata
226 21eea4b3 Gerd Hoffmann
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
227 21eea4b3 Gerd Hoffmann
                         const char *name, int devfn_min)
228 30468f78 bellard
{
229 21eea4b3 Gerd Hoffmann
    qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
230 6fa84913 Isaku Yamahata
    assert(PCI_FUNC(devfn_min) == 0);
231 502a5395 pbrook
    bus->devfn_min = devfn_min;
232 e822a52a Isaku Yamahata
233 e822a52a Isaku Yamahata
    /* host bridge */
234 e822a52a Isaku Yamahata
    QLIST_INIT(&bus->child);
235 e822a52a Isaku Yamahata
    pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
236 e822a52a Isaku Yamahata
237 0be71e32 Alex Williamson
    vmstate_register(NULL, -1, &vmstate_pcibus, bus);
238 a08d4367 Jan Kiszka
    qemu_register_reset(pci_bus_reset, bus);
239 21eea4b3 Gerd Hoffmann
}
240 21eea4b3 Gerd Hoffmann
241 21eea4b3 Gerd Hoffmann
PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
242 21eea4b3 Gerd Hoffmann
{
243 21eea4b3 Gerd Hoffmann
    PCIBus *bus;
244 21eea4b3 Gerd Hoffmann
245 21eea4b3 Gerd Hoffmann
    bus = qemu_mallocz(sizeof(*bus));
246 21eea4b3 Gerd Hoffmann
    bus->qbus.qdev_allocated = 1;
247 21eea4b3 Gerd Hoffmann
    pci_bus_new_inplace(bus, parent, name, devfn_min);
248 21eea4b3 Gerd Hoffmann
    return bus;
249 21eea4b3 Gerd Hoffmann
}
250 21eea4b3 Gerd Hoffmann
251 21eea4b3 Gerd Hoffmann
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
252 21eea4b3 Gerd Hoffmann
                  void *irq_opaque, int nirq)
253 21eea4b3 Gerd Hoffmann
{
254 21eea4b3 Gerd Hoffmann
    bus->set_irq = set_irq;
255 21eea4b3 Gerd Hoffmann
    bus->map_irq = map_irq;
256 21eea4b3 Gerd Hoffmann
    bus->irq_opaque = irq_opaque;
257 21eea4b3 Gerd Hoffmann
    bus->nirq = nirq;
258 21eea4b3 Gerd Hoffmann
    bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
259 21eea4b3 Gerd Hoffmann
}
260 21eea4b3 Gerd Hoffmann
261 87c30546 Isaku Yamahata
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev)
262 ee995ffb Gerd Hoffmann
{
263 ee995ffb Gerd Hoffmann
    bus->qbus.allow_hotplug = 1;
264 ee995ffb Gerd Hoffmann
    bus->hotplug = hotplug;
265 87c30546 Isaku Yamahata
    bus->hotplug_qdev = qdev;
266 ee995ffb Gerd Hoffmann
}
267 ee995ffb Gerd Hoffmann
268 2e01c8cf Blue Swirl
void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
269 2e01c8cf Blue Swirl
{
270 2e01c8cf Blue Swirl
    bus->mem_base = base;
271 2e01c8cf Blue Swirl
}
272 2e01c8cf Blue Swirl
273 21eea4b3 Gerd Hoffmann
PCIBus *pci_register_bus(DeviceState *parent, const char *name,
274 21eea4b3 Gerd Hoffmann
                         pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
275 21eea4b3 Gerd Hoffmann
                         void *irq_opaque, int devfn_min, int nirq)
276 21eea4b3 Gerd Hoffmann
{
277 21eea4b3 Gerd Hoffmann
    PCIBus *bus;
278 21eea4b3 Gerd Hoffmann
279 21eea4b3 Gerd Hoffmann
    bus = pci_bus_new(parent, name, devfn_min);
280 21eea4b3 Gerd Hoffmann
    pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
281 30468f78 bellard
    return bus;
282 30468f78 bellard
}
283 69b91039 bellard
284 e822a52a Isaku Yamahata
static void pci_register_secondary_bus(PCIBus *parent,
285 e822a52a Isaku Yamahata
                                       PCIBus *bus,
286 03587182 Gerd Hoffmann
                                       PCIDevice *dev,
287 03587182 Gerd Hoffmann
                                       pci_map_irq_fn map_irq,
288 03587182 Gerd Hoffmann
                                       const char *name)
289 80b3ada7 pbrook
{
290 03587182 Gerd Hoffmann
    qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name);
291 80b3ada7 pbrook
    bus->map_irq = map_irq;
292 80b3ada7 pbrook
    bus->parent_dev = dev;
293 e822a52a Isaku Yamahata
294 e822a52a Isaku Yamahata
    QLIST_INIT(&bus->child);
295 e822a52a Isaku Yamahata
    QLIST_INSERT_HEAD(&parent->child, bus, sibling);
296 e822a52a Isaku Yamahata
}
297 e822a52a Isaku Yamahata
298 e822a52a Isaku Yamahata
static void pci_unregister_secondary_bus(PCIBus *bus)
299 e822a52a Isaku Yamahata
{
300 e822a52a Isaku Yamahata
    assert(QLIST_EMPTY(&bus->child));
301 e822a52a Isaku Yamahata
    QLIST_REMOVE(bus, sibling);
302 80b3ada7 pbrook
}
303 80b3ada7 pbrook
304 502a5395 pbrook
int pci_bus_num(PCIBus *s)
305 502a5395 pbrook
{
306 e94ff650 Isaku Yamahata
    if (!s->parent_dev)
307 e94ff650 Isaku Yamahata
        return 0;       /* pci host bridge */
308 e94ff650 Isaku Yamahata
    return s->parent_dev->config[PCI_SECONDARY_BUS];
309 502a5395 pbrook
}
310 502a5395 pbrook
311 73534f2f Juan Quintela
static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
312 30ca2aab bellard
{
313 73534f2f Juan Quintela
    PCIDevice *s = container_of(pv, PCIDevice, config);
314 a9f49946 Isaku Yamahata
    uint8_t *config;
315 52fc1d83 balrog
    int i;
316 52fc1d83 balrog
317 a9f49946 Isaku Yamahata
    assert(size == pci_config_size(s));
318 a9f49946 Isaku Yamahata
    config = qemu_malloc(size);
319 a9f49946 Isaku Yamahata
320 a9f49946 Isaku Yamahata
    qemu_get_buffer(f, config, size);
321 a9f49946 Isaku Yamahata
    for (i = 0; i < size; ++i) {
322 a9f49946 Isaku Yamahata
        if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
323 a9f49946 Isaku Yamahata
            qemu_free(config);
324 bd4b65ee Michael S. Tsirkin
            return -EINVAL;
325 a9f49946 Isaku Yamahata
        }
326 a9f49946 Isaku Yamahata
    }
327 a9f49946 Isaku Yamahata
    memcpy(s->config, config, size);
328 bd4b65ee Michael S. Tsirkin
329 1941d19c bellard
    pci_update_mappings(s);
330 52fc1d83 balrog
331 a9f49946 Isaku Yamahata
    qemu_free(config);
332 30ca2aab bellard
    return 0;
333 30ca2aab bellard
}
334 30ca2aab bellard
335 73534f2f Juan Quintela
/* just put buffer */
336 84e2e3eb Juan Quintela
static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
337 73534f2f Juan Quintela
{
338 dbe73d7f Juan Quintela
    const uint8_t **v = pv;
339 a9f49946 Isaku Yamahata
    assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
340 dbe73d7f Juan Quintela
    qemu_put_buffer(f, *v, size);
341 73534f2f Juan Quintela
}
342 73534f2f Juan Quintela
343 73534f2f Juan Quintela
static VMStateInfo vmstate_info_pci_config = {
344 73534f2f Juan Quintela
    .name = "pci config",
345 73534f2f Juan Quintela
    .get  = get_pci_config_device,
346 73534f2f Juan Quintela
    .put  = put_pci_config_device,
347 73534f2f Juan Quintela
};
348 73534f2f Juan Quintela
349 d036bb21 Michael S. Tsirkin
static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
350 d036bb21 Michael S. Tsirkin
{
351 c3f8f611 Michael S. Tsirkin
    PCIDevice *s = container_of(pv, PCIDevice, irq_state);
352 d036bb21 Michael S. Tsirkin
    uint32_t irq_state[PCI_NUM_PINS];
353 d036bb21 Michael S. Tsirkin
    int i;
354 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
355 d036bb21 Michael S. Tsirkin
        irq_state[i] = qemu_get_be32(f);
356 d036bb21 Michael S. Tsirkin
        if (irq_state[i] != 0x1 && irq_state[i] != 0) {
357 d036bb21 Michael S. Tsirkin
            fprintf(stderr, "irq state %d: must be 0 or 1.\n",
358 d036bb21 Michael S. Tsirkin
                    irq_state[i]);
359 d036bb21 Michael S. Tsirkin
            return -EINVAL;
360 d036bb21 Michael S. Tsirkin
        }
361 d036bb21 Michael S. Tsirkin
    }
362 d036bb21 Michael S. Tsirkin
363 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
364 d036bb21 Michael S. Tsirkin
        pci_set_irq_state(s, i, irq_state[i]);
365 d036bb21 Michael S. Tsirkin
    }
366 d036bb21 Michael S. Tsirkin
367 d036bb21 Michael S. Tsirkin
    return 0;
368 d036bb21 Michael S. Tsirkin
}
369 d036bb21 Michael S. Tsirkin
370 d036bb21 Michael S. Tsirkin
static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
371 d036bb21 Michael S. Tsirkin
{
372 d036bb21 Michael S. Tsirkin
    int i;
373 c3f8f611 Michael S. Tsirkin
    PCIDevice *s = container_of(pv, PCIDevice, irq_state);
374 d036bb21 Michael S. Tsirkin
375 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
376 d036bb21 Michael S. Tsirkin
        qemu_put_be32(f, pci_irq_state(s, i));
377 d036bb21 Michael S. Tsirkin
    }
378 d036bb21 Michael S. Tsirkin
}
379 d036bb21 Michael S. Tsirkin
380 d036bb21 Michael S. Tsirkin
static VMStateInfo vmstate_info_pci_irq_state = {
381 d036bb21 Michael S. Tsirkin
    .name = "pci irq state",
382 d036bb21 Michael S. Tsirkin
    .get  = get_pci_irq_state,
383 d036bb21 Michael S. Tsirkin
    .put  = put_pci_irq_state,
384 d036bb21 Michael S. Tsirkin
};
385 d036bb21 Michael S. Tsirkin
386 73534f2f Juan Quintela
const VMStateDescription vmstate_pci_device = {
387 73534f2f Juan Quintela
    .name = "PCIDevice",
388 73534f2f Juan Quintela
    .version_id = 2,
389 73534f2f Juan Quintela
    .minimum_version_id = 1,
390 73534f2f Juan Quintela
    .minimum_version_id_old = 1,
391 73534f2f Juan Quintela
    .fields      = (VMStateField []) {
392 73534f2f Juan Quintela
        VMSTATE_INT32_LE(version_id, PCIDevice),
393 a9f49946 Isaku Yamahata
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
394 a9f49946 Isaku Yamahata
                                   vmstate_info_pci_config,
395 a9f49946 Isaku Yamahata
                                   PCI_CONFIG_SPACE_SIZE),
396 d036bb21 Michael S. Tsirkin
        VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
397 d036bb21 Michael S. Tsirkin
                                   vmstate_info_pci_irq_state,
398 d036bb21 Michael S. Tsirkin
                                   PCI_NUM_PINS * sizeof(int32_t)),
399 a9f49946 Isaku Yamahata
        VMSTATE_END_OF_LIST()
400 a9f49946 Isaku Yamahata
    }
401 a9f49946 Isaku Yamahata
};
402 a9f49946 Isaku Yamahata
403 a9f49946 Isaku Yamahata
const VMStateDescription vmstate_pcie_device = {
404 a9f49946 Isaku Yamahata
    .name = "PCIDevice",
405 a9f49946 Isaku Yamahata
    .version_id = 2,
406 a9f49946 Isaku Yamahata
    .minimum_version_id = 1,
407 a9f49946 Isaku Yamahata
    .minimum_version_id_old = 1,
408 a9f49946 Isaku Yamahata
    .fields      = (VMStateField []) {
409 a9f49946 Isaku Yamahata
        VMSTATE_INT32_LE(version_id, PCIDevice),
410 a9f49946 Isaku Yamahata
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
411 a9f49946 Isaku Yamahata
                                   vmstate_info_pci_config,
412 a9f49946 Isaku Yamahata
                                   PCIE_CONFIG_SPACE_SIZE),
413 d036bb21 Michael S. Tsirkin
        VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
414 d036bb21 Michael S. Tsirkin
                                   vmstate_info_pci_irq_state,
415 d036bb21 Michael S. Tsirkin
                                   PCI_NUM_PINS * sizeof(int32_t)),
416 73534f2f Juan Quintela
        VMSTATE_END_OF_LIST()
417 73534f2f Juan Quintela
    }
418 73534f2f Juan Quintela
};
419 73534f2f Juan Quintela
420 a9f49946 Isaku Yamahata
static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
421 a9f49946 Isaku Yamahata
{
422 a9f49946 Isaku Yamahata
    return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
423 a9f49946 Isaku Yamahata
}
424 a9f49946 Isaku Yamahata
425 73534f2f Juan Quintela
void pci_device_save(PCIDevice *s, QEMUFile *f)
426 73534f2f Juan Quintela
{
427 f9bf77dd Michael S. Tsirkin
    /* Clear interrupt status bit: it is implicit
428 f9bf77dd Michael S. Tsirkin
     * in irq_state which we are saving.
429 f9bf77dd Michael S. Tsirkin
     * This makes us compatible with old devices
430 f9bf77dd Michael S. Tsirkin
     * which never set or clear this bit. */
431 f9bf77dd Michael S. Tsirkin
    s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
432 a9f49946 Isaku Yamahata
    vmstate_save_state(f, pci_get_vmstate(s), s);
433 f9bf77dd Michael S. Tsirkin
    /* Restore the interrupt status bit. */
434 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(s);
435 73534f2f Juan Quintela
}
436 73534f2f Juan Quintela
437 73534f2f Juan Quintela
int pci_device_load(PCIDevice *s, QEMUFile *f)
438 73534f2f Juan Quintela
{
439 f9bf77dd Michael S. Tsirkin
    int ret;
440 f9bf77dd Michael S. Tsirkin
    ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
441 f9bf77dd Michael S. Tsirkin
    /* Restore the interrupt status bit. */
442 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(s);
443 f9bf77dd Michael S. Tsirkin
    return ret;
444 73534f2f Juan Quintela
}
445 73534f2f Juan Quintela
446 5e434f4e Isaku Yamahata
static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
447 d350d97d aliguori
{
448 5e434f4e Isaku Yamahata
    pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
449 5e434f4e Isaku Yamahata
                 pci_default_sub_vendor_id);
450 5e434f4e Isaku Yamahata
    pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
451 5e434f4e Isaku Yamahata
                 pci_default_sub_device_id);
452 d350d97d aliguori
}
453 d350d97d aliguori
454 880345c4 aliguori
/*
455 880345c4 aliguori
 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
456 880345c4 aliguori
 */
457 880345c4 aliguori
static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
458 880345c4 aliguori
{
459 880345c4 aliguori
    const char *p;
460 880345c4 aliguori
    char *e;
461 880345c4 aliguori
    unsigned long val;
462 880345c4 aliguori
    unsigned long dom = 0, bus = 0;
463 880345c4 aliguori
    unsigned slot = 0;
464 880345c4 aliguori
465 880345c4 aliguori
    p = addr;
466 880345c4 aliguori
    val = strtoul(p, &e, 16);
467 880345c4 aliguori
    if (e == p)
468 880345c4 aliguori
        return -1;
469 880345c4 aliguori
    if (*e == ':') {
470 880345c4 aliguori
        bus = val;
471 880345c4 aliguori
        p = e + 1;
472 880345c4 aliguori
        val = strtoul(p, &e, 16);
473 880345c4 aliguori
        if (e == p)
474 880345c4 aliguori
            return -1;
475 880345c4 aliguori
        if (*e == ':') {
476 880345c4 aliguori
            dom = bus;
477 880345c4 aliguori
            bus = val;
478 880345c4 aliguori
            p = e + 1;
479 880345c4 aliguori
            val = strtoul(p, &e, 16);
480 880345c4 aliguori
            if (e == p)
481 880345c4 aliguori
                return -1;
482 880345c4 aliguori
        }
483 880345c4 aliguori
    }
484 880345c4 aliguori
485 880345c4 aliguori
    if (dom > 0xffff || bus > 0xff || val > 0x1f)
486 880345c4 aliguori
        return -1;
487 880345c4 aliguori
488 880345c4 aliguori
    slot = val;
489 880345c4 aliguori
490 880345c4 aliguori
    if (*e)
491 880345c4 aliguori
        return -1;
492 880345c4 aliguori
493 880345c4 aliguori
    /* Note: QEMU doesn't implement domains other than 0 */
494 c469e1dd Isaku Yamahata
    if (!pci_find_bus(pci_find_root_bus(dom), bus))
495 880345c4 aliguori
        return -1;
496 880345c4 aliguori
497 880345c4 aliguori
    *domp = dom;
498 880345c4 aliguori
    *busp = bus;
499 880345c4 aliguori
    *slotp = slot;
500 880345c4 aliguori
    return 0;
501 880345c4 aliguori
}
502 880345c4 aliguori
503 e9283f8b Jan Kiszka
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
504 e9283f8b Jan Kiszka
                     unsigned *slotp)
505 880345c4 aliguori
{
506 e9283f8b Jan Kiszka
    /* strip legacy tag */
507 e9283f8b Jan Kiszka
    if (!strncmp(addr, "pci_addr=", 9)) {
508 e9283f8b Jan Kiszka
        addr += 9;
509 e9283f8b Jan Kiszka
    }
510 e9283f8b Jan Kiszka
    if (pci_parse_devaddr(addr, domp, busp, slotp)) {
511 e9283f8b Jan Kiszka
        monitor_printf(mon, "Invalid pci address\n");
512 880345c4 aliguori
        return -1;
513 e9283f8b Jan Kiszka
    }
514 e9283f8b Jan Kiszka
    return 0;
515 880345c4 aliguori
}
516 880345c4 aliguori
517 49bd1458 Markus Armbruster
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
518 5607c388 Markus Armbruster
{
519 5607c388 Markus Armbruster
    int dom, bus;
520 5607c388 Markus Armbruster
    unsigned slot;
521 5607c388 Markus Armbruster
522 5607c388 Markus Armbruster
    if (!devaddr) {
523 5607c388 Markus Armbruster
        *devfnp = -1;
524 c469e1dd Isaku Yamahata
        return pci_find_bus(pci_find_root_bus(0), 0);
525 5607c388 Markus Armbruster
    }
526 5607c388 Markus Armbruster
527 5607c388 Markus Armbruster
    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
528 5607c388 Markus Armbruster
        return NULL;
529 5607c388 Markus Armbruster
    }
530 5607c388 Markus Armbruster
531 5607c388 Markus Armbruster
    *devfnp = slot << 3;
532 e075e788 Isaku Yamahata
    return pci_find_bus(pci_find_root_bus(dom), bus);
533 5607c388 Markus Armbruster
}
534 5607c388 Markus Armbruster
535 bd4b65ee Michael S. Tsirkin
static void pci_init_cmask(PCIDevice *dev)
536 bd4b65ee Michael S. Tsirkin
{
537 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
538 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
539 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
540 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_REVISION_ID] = 0xff;
541 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_CLASS_PROG] = 0xff;
542 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
543 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_HEADER_TYPE] = 0xff;
544 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
545 bd4b65ee Michael S. Tsirkin
}
546 bd4b65ee Michael S. Tsirkin
547 b7ee1603 Michael S. Tsirkin
static void pci_init_wmask(PCIDevice *dev)
548 b7ee1603 Michael S. Tsirkin
{
549 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(dev);
550 a9f49946 Isaku Yamahata
551 b7ee1603 Michael S. Tsirkin
    dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
552 b7ee1603 Michael S. Tsirkin
    dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
553 67a51b48 Isaku Yamahata
    pci_set_word(dev->wmask + PCI_COMMAND,
554 a7b15a5c Michael S. Tsirkin
                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
555 a7b15a5c Michael S. Tsirkin
                 PCI_COMMAND_INTX_DISABLE);
556 3e21ffc9 Isaku Yamahata
557 3e21ffc9 Isaku Yamahata
    memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
558 3e21ffc9 Isaku Yamahata
           config_size - PCI_CONFIG_HEADER_SIZE);
559 b7ee1603 Michael S. Tsirkin
}
560 b7ee1603 Michael S. Tsirkin
561 fb231628 Isaku Yamahata
static void pci_init_wmask_bridge(PCIDevice *d)
562 fb231628 Isaku Yamahata
{
563 fb231628 Isaku Yamahata
    /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
564 fb231628 Isaku Yamahata
       PCI_SEC_LETENCY_TIMER */
565 fb231628 Isaku Yamahata
    memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
566 fb231628 Isaku Yamahata
567 fb231628 Isaku Yamahata
    /* base and limit */
568 fb231628 Isaku Yamahata
    d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
569 fb231628 Isaku Yamahata
    d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
570 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_MEMORY_BASE,
571 fb231628 Isaku Yamahata
                 PCI_MEMORY_RANGE_MASK & 0xffff);
572 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
573 fb231628 Isaku Yamahata
                 PCI_MEMORY_RANGE_MASK & 0xffff);
574 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
575 fb231628 Isaku Yamahata
                 PCI_PREF_RANGE_MASK & 0xffff);
576 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
577 fb231628 Isaku Yamahata
                 PCI_PREF_RANGE_MASK & 0xffff);
578 fb231628 Isaku Yamahata
579 fb231628 Isaku Yamahata
    /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
580 fb231628 Isaku Yamahata
    memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
581 fb231628 Isaku Yamahata
582 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
583 fb231628 Isaku Yamahata
}
584 fb231628 Isaku Yamahata
585 a9f49946 Isaku Yamahata
static void pci_config_alloc(PCIDevice *pci_dev)
586 a9f49946 Isaku Yamahata
{
587 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(pci_dev);
588 a9f49946 Isaku Yamahata
589 a9f49946 Isaku Yamahata
    pci_dev->config = qemu_mallocz(config_size);
590 a9f49946 Isaku Yamahata
    pci_dev->cmask = qemu_mallocz(config_size);
591 a9f49946 Isaku Yamahata
    pci_dev->wmask = qemu_mallocz(config_size);
592 a9f49946 Isaku Yamahata
    pci_dev->used = qemu_mallocz(config_size);
593 a9f49946 Isaku Yamahata
}
594 a9f49946 Isaku Yamahata
595 a9f49946 Isaku Yamahata
static void pci_config_free(PCIDevice *pci_dev)
596 a9f49946 Isaku Yamahata
{
597 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->config);
598 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->cmask);
599 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->wmask);
600 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->used);
601 a9f49946 Isaku Yamahata
}
602 a9f49946 Isaku Yamahata
603 69b91039 bellard
/* -1 for devfn means auto assign */
604 6b1b92d3 Paul Brook
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
605 6b1b92d3 Paul Brook
                                         const char *name, int devfn,
606 6b1b92d3 Paul Brook
                                         PCIConfigReadFunc *config_read,
607 fb231628 Isaku Yamahata
                                         PCIConfigWriteFunc *config_write,
608 e327e323 Isaku Yamahata
                                         bool is_bridge)
609 69b91039 bellard
{
610 69b91039 bellard
    if (devfn < 0) {
611 b47b0706 Isaku Yamahata
        for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
612 6fa84913 Isaku Yamahata
            devfn += PCI_FUNC_MAX) {
613 30468f78 bellard
            if (!bus->devices[devfn])
614 69b91039 bellard
                goto found;
615 69b91039 bellard
        }
616 3709c1b7 Daniel P. Berrange
        error_report("PCI: no slot/function available for %s, all in use", name);
617 09e3acc6 Gerd Hoffmann
        return NULL;
618 69b91039 bellard
    found: ;
619 07b7d053 Markus Armbruster
    } else if (bus->devices[devfn]) {
620 3709c1b7 Daniel P. Berrange
        error_report("PCI: slot %d function %d not available for %s, in use by %s",
621 3709c1b7 Daniel P. Berrange
                     PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name);
622 09e3acc6 Gerd Hoffmann
        return NULL;
623 69b91039 bellard
    }
624 30468f78 bellard
    pci_dev->bus = bus;
625 69b91039 bellard
    pci_dev->devfn = devfn;
626 69b91039 bellard
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
627 d036bb21 Michael S. Tsirkin
    pci_dev->irq_state = 0;
628 a9f49946 Isaku Yamahata
    pci_config_alloc(pci_dev);
629 fb231628 Isaku Yamahata
630 e327e323 Isaku Yamahata
    if (!is_bridge) {
631 fb231628 Isaku Yamahata
        pci_set_default_subsystem_id(pci_dev);
632 fb231628 Isaku Yamahata
    }
633 bd4b65ee Michael S. Tsirkin
    pci_init_cmask(pci_dev);
634 b7ee1603 Michael S. Tsirkin
    pci_init_wmask(pci_dev);
635 e327e323 Isaku Yamahata
    if (is_bridge) {
636 fb231628 Isaku Yamahata
        pci_init_wmask_bridge(pci_dev);
637 fb231628 Isaku Yamahata
    }
638 0ac32c83 bellard
639 0ac32c83 bellard
    if (!config_read)
640 0ac32c83 bellard
        config_read = pci_default_read_config;
641 0ac32c83 bellard
    if (!config_write)
642 0ac32c83 bellard
        config_write = pci_default_write_config;
643 69b91039 bellard
    pci_dev->config_read = config_read;
644 69b91039 bellard
    pci_dev->config_write = config_write;
645 30468f78 bellard
    bus->devices[devfn] = pci_dev;
646 e369cad7 Isaku Yamahata
    pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
647 f16c4abf Juan Quintela
    pci_dev->version_id = 2; /* Current pci device vmstate version */
648 69b91039 bellard
    return pci_dev;
649 69b91039 bellard
}
650 69b91039 bellard
651 925fe64a Alex Williamson
static void do_pci_unregister_device(PCIDevice *pci_dev)
652 925fe64a Alex Williamson
{
653 925fe64a Alex Williamson
    qemu_free_irqs(pci_dev->irq);
654 925fe64a Alex Williamson
    pci_dev->bus->devices[pci_dev->devfn] = NULL;
655 925fe64a Alex Williamson
    pci_config_free(pci_dev);
656 925fe64a Alex Williamson
}
657 925fe64a Alex Williamson
658 6b1b92d3 Paul Brook
PCIDevice *pci_register_device(PCIBus *bus, const char *name,
659 6b1b92d3 Paul Brook
                               int instance_size, int devfn,
660 6b1b92d3 Paul Brook
                               PCIConfigReadFunc *config_read,
661 6b1b92d3 Paul Brook
                               PCIConfigWriteFunc *config_write)
662 6b1b92d3 Paul Brook
{
663 6b1b92d3 Paul Brook
    PCIDevice *pci_dev;
664 6b1b92d3 Paul Brook
665 6b1b92d3 Paul Brook
    pci_dev = qemu_mallocz(instance_size);
666 6b1b92d3 Paul Brook
    pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
667 fb231628 Isaku Yamahata
                                     config_read, config_write,
668 fb231628 Isaku Yamahata
                                     PCI_HEADER_TYPE_NORMAL);
669 09e3acc6 Gerd Hoffmann
    if (pci_dev == NULL) {
670 09e3acc6 Gerd Hoffmann
        hw_error("PCI: can't register device\n");
671 09e3acc6 Gerd Hoffmann
    }
672 6b1b92d3 Paul Brook
    return pci_dev;
673 6b1b92d3 Paul Brook
}
674 2e01c8cf Blue Swirl
675 2e01c8cf Blue Swirl
static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
676 2e01c8cf Blue Swirl
                                          target_phys_addr_t addr)
677 5851e08c aliguori
{
678 2e01c8cf Blue Swirl
    return addr + bus->mem_base;
679 5851e08c aliguori
}
680 5851e08c aliguori
681 5851e08c aliguori
static void pci_unregister_io_regions(PCIDevice *pci_dev)
682 5851e08c aliguori
{
683 5851e08c aliguori
    PCIIORegion *r;
684 5851e08c aliguori
    int i;
685 5851e08c aliguori
686 5851e08c aliguori
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
687 5851e08c aliguori
        r = &pci_dev->io_regions[i];
688 182f9c8a Isaku Yamahata
        if (!r->size || r->addr == PCI_BAR_UNMAPPED)
689 5851e08c aliguori
            continue;
690 0392a017 Isaku Yamahata
        if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
691 a0c7a97e Isaku Yamahata
            isa_unassign_ioport(r->addr, r->filtered_size);
692 5851e08c aliguori
        } else {
693 2e01c8cf Blue Swirl
            cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus,
694 2e01c8cf Blue Swirl
                                                         r->addr),
695 2e01c8cf Blue Swirl
                                         r->filtered_size,
696 2e01c8cf Blue Swirl
                                         IO_MEM_UNASSIGNED);
697 5851e08c aliguori
        }
698 5851e08c aliguori
    }
699 5851e08c aliguori
}
700 5851e08c aliguori
701 a36a344d Gerd Hoffmann
static int pci_unregister_device(DeviceState *dev)
702 5851e08c aliguori
{
703 a36a344d Gerd Hoffmann
    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
704 e3936fa5 Gerd Hoffmann
    PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
705 5851e08c aliguori
    int ret = 0;
706 5851e08c aliguori
707 e3936fa5 Gerd Hoffmann
    if (info->exit)
708 e3936fa5 Gerd Hoffmann
        ret = info->exit(pci_dev);
709 5851e08c aliguori
    if (ret)
710 5851e08c aliguori
        return ret;
711 5851e08c aliguori
712 5851e08c aliguori
    pci_unregister_io_regions(pci_dev);
713 230741dc Alex Williamson
    pci_del_option_rom(pci_dev);
714 925fe64a Alex Williamson
    do_pci_unregister_device(pci_dev);
715 5851e08c aliguori
    return 0;
716 5851e08c aliguori
}
717 5851e08c aliguori
718 28c2c264 Avi Kivity
void pci_register_bar(PCIDevice *pci_dev, int region_num,
719 6e355d90 Isaku Yamahata
                            pcibus_t size, int type,
720 69b91039 bellard
                            PCIMapIORegionFunc *map_func)
721 69b91039 bellard
{
722 69b91039 bellard
    PCIIORegion *r;
723 d7ce493a pbrook
    uint32_t addr;
724 6e355d90 Isaku Yamahata
    pcibus_t wmask;
725 69b91039 bellard
726 8a8696a3 bellard
    if ((unsigned int)region_num >= PCI_NUM_REGIONS)
727 69b91039 bellard
        return;
728 a4c20c6a aliguori
729 a4c20c6a aliguori
    if (size & (size-1)) {
730 a4c20c6a aliguori
        fprintf(stderr, "ERROR: PCI region size must be pow2 "
731 89e8b13c Isaku Yamahata
                    "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
732 a4c20c6a aliguori
        exit(1);
733 a4c20c6a aliguori
    }
734 a4c20c6a aliguori
735 69b91039 bellard
    r = &pci_dev->io_regions[region_num];
736 182f9c8a Isaku Yamahata
    r->addr = PCI_BAR_UNMAPPED;
737 69b91039 bellard
    r->size = size;
738 a0c7a97e Isaku Yamahata
    r->filtered_size = size;
739 69b91039 bellard
    r->type = type;
740 69b91039 bellard
    r->map_func = map_func;
741 b7ee1603 Michael S. Tsirkin
742 b7ee1603 Michael S. Tsirkin
    wmask = ~(size - 1);
743 b3b11697 Isaku Yamahata
    addr = pci_bar(pci_dev, region_num);
744 d7ce493a pbrook
    if (region_num == PCI_ROM_SLOT) {
745 b7ee1603 Michael S. Tsirkin
        /* ROM enable bit is writeable */
746 5330de09 Michael S. Tsirkin
        wmask |= PCI_ROM_ADDRESS_ENABLE;
747 d7ce493a pbrook
    }
748 b0ff8eb2 Isaku Yamahata
    pci_set_long(pci_dev->config + addr, type);
749 14421258 Isaku Yamahata
    if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
750 14421258 Isaku Yamahata
        r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
751 14421258 Isaku Yamahata
        pci_set_quad(pci_dev->wmask + addr, wmask);
752 14421258 Isaku Yamahata
        pci_set_quad(pci_dev->cmask + addr, ~0ULL);
753 14421258 Isaku Yamahata
    } else {
754 14421258 Isaku Yamahata
        pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
755 14421258 Isaku Yamahata
        pci_set_long(pci_dev->cmask + addr, 0xffffffff);
756 14421258 Isaku Yamahata
    }
757 69b91039 bellard
}
758 69b91039 bellard
759 a0c7a97e Isaku Yamahata
static uint32_t pci_config_get_io_base(PCIDevice *d,
760 a0c7a97e Isaku Yamahata
                                       uint32_t base, uint32_t base_upper16)
761 a0c7a97e Isaku Yamahata
{
762 a0c7a97e Isaku Yamahata
    uint32_t val;
763 a0c7a97e Isaku Yamahata
764 a0c7a97e Isaku Yamahata
    val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
765 a0c7a97e Isaku Yamahata
    if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
766 10c9c329 Isaku Yamahata
        val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
767 a0c7a97e Isaku Yamahata
    }
768 a0c7a97e Isaku Yamahata
    return val;
769 a0c7a97e Isaku Yamahata
}
770 a0c7a97e Isaku Yamahata
771 d46636b8 Isaku Yamahata
static pcibus_t pci_config_get_memory_base(PCIDevice *d, uint32_t base)
772 a0c7a97e Isaku Yamahata
{
773 d46636b8 Isaku Yamahata
    return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
774 a0c7a97e Isaku Yamahata
        << 16;
775 a0c7a97e Isaku Yamahata
}
776 a0c7a97e Isaku Yamahata
777 d46636b8 Isaku Yamahata
static pcibus_t pci_config_get_pref_base(PCIDevice *d,
778 a0c7a97e Isaku Yamahata
                                         uint32_t base, uint32_t upper)
779 a0c7a97e Isaku Yamahata
{
780 d46636b8 Isaku Yamahata
    pcibus_t tmp;
781 d46636b8 Isaku Yamahata
    pcibus_t val;
782 d46636b8 Isaku Yamahata
783 d46636b8 Isaku Yamahata
    tmp = (pcibus_t)pci_get_word(d->config + base);
784 d46636b8 Isaku Yamahata
    val = (tmp & PCI_PREF_RANGE_MASK) << 16;
785 d46636b8 Isaku Yamahata
    if (tmp & PCI_PREF_RANGE_TYPE_64) {
786 d46636b8 Isaku Yamahata
        val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
787 d46636b8 Isaku Yamahata
    }
788 a0c7a97e Isaku Yamahata
    return val;
789 a0c7a97e Isaku Yamahata
}
790 a0c7a97e Isaku Yamahata
791 a0c7a97e Isaku Yamahata
static pcibus_t pci_bridge_get_base(PCIDevice *bridge, uint8_t type)
792 a0c7a97e Isaku Yamahata
{
793 a0c7a97e Isaku Yamahata
    pcibus_t base;
794 a0c7a97e Isaku Yamahata
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
795 a0c7a97e Isaku Yamahata
        base = pci_config_get_io_base(bridge,
796 a0c7a97e Isaku Yamahata
                                      PCI_IO_BASE, PCI_IO_BASE_UPPER16);
797 a0c7a97e Isaku Yamahata
    } else {
798 a0c7a97e Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
799 a0c7a97e Isaku Yamahata
            base = pci_config_get_pref_base(
800 a0c7a97e Isaku Yamahata
                bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
801 a0c7a97e Isaku Yamahata
        } else {
802 a0c7a97e Isaku Yamahata
            base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
803 a0c7a97e Isaku Yamahata
        }
804 a0c7a97e Isaku Yamahata
    }
805 a0c7a97e Isaku Yamahata
806 a0c7a97e Isaku Yamahata
    return base;
807 a0c7a97e Isaku Yamahata
}
808 a0c7a97e Isaku Yamahata
809 a0c7a97e Isaku Yamahata
static pcibus_t pci_bridge_get_limit(PCIDevice *bridge, uint8_t type)
810 a0c7a97e Isaku Yamahata
{
811 a0c7a97e Isaku Yamahata
    pcibus_t limit;
812 a0c7a97e Isaku Yamahata
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
813 a0c7a97e Isaku Yamahata
        limit = pci_config_get_io_base(bridge,
814 a0c7a97e Isaku Yamahata
                                      PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
815 a0c7a97e Isaku Yamahata
        limit |= 0xfff;         /* PCI bridge spec 3.2.5.6. */
816 a0c7a97e Isaku Yamahata
    } else {
817 a0c7a97e Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
818 a0c7a97e Isaku Yamahata
            limit = pci_config_get_pref_base(
819 a0c7a97e Isaku Yamahata
                bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
820 a0c7a97e Isaku Yamahata
        } else {
821 a0c7a97e Isaku Yamahata
            limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
822 a0c7a97e Isaku Yamahata
        }
823 a0c7a97e Isaku Yamahata
        limit |= 0xfffff;       /* PCI bridge spec 3.2.5.{1, 8}. */
824 a0c7a97e Isaku Yamahata
    }
825 a0c7a97e Isaku Yamahata
    return limit;
826 a0c7a97e Isaku Yamahata
}
827 a0c7a97e Isaku Yamahata
828 a0c7a97e Isaku Yamahata
static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size,
829 a0c7a97e Isaku Yamahata
                              uint8_t type)
830 a0c7a97e Isaku Yamahata
{
831 a0c7a97e Isaku Yamahata
    pcibus_t base = *addr;
832 a0c7a97e Isaku Yamahata
    pcibus_t limit = *addr + *size - 1;
833 a0c7a97e Isaku Yamahata
    PCIDevice *br;
834 a0c7a97e Isaku Yamahata
835 a0c7a97e Isaku Yamahata
    for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
836 a0c7a97e Isaku Yamahata
        uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
837 a0c7a97e Isaku Yamahata
838 a0c7a97e Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_SPACE_IO) {
839 a0c7a97e Isaku Yamahata
            if (!(cmd & PCI_COMMAND_IO)) {
840 a0c7a97e Isaku Yamahata
                goto no_map;
841 a0c7a97e Isaku Yamahata
            }
842 a0c7a97e Isaku Yamahata
        } else {
843 a0c7a97e Isaku Yamahata
            if (!(cmd & PCI_COMMAND_MEMORY)) {
844 a0c7a97e Isaku Yamahata
                goto no_map;
845 a0c7a97e Isaku Yamahata
            }
846 a0c7a97e Isaku Yamahata
        }
847 a0c7a97e Isaku Yamahata
848 a0c7a97e Isaku Yamahata
        base = MAX(base, pci_bridge_get_base(br, type));
849 a0c7a97e Isaku Yamahata
        limit = MIN(limit, pci_bridge_get_limit(br, type));
850 a0c7a97e Isaku Yamahata
    }
851 a0c7a97e Isaku Yamahata
852 a0c7a97e Isaku Yamahata
    if (base > limit) {
853 88a95564 Michael S. Tsirkin
        goto no_map;
854 a0c7a97e Isaku Yamahata
    }
855 88a95564 Michael S. Tsirkin
    *addr = base;
856 88a95564 Michael S. Tsirkin
    *size = limit - base + 1;
857 88a95564 Michael S. Tsirkin
    return;
858 88a95564 Michael S. Tsirkin
no_map:
859 88a95564 Michael S. Tsirkin
    *addr = PCI_BAR_UNMAPPED;
860 88a95564 Michael S. Tsirkin
    *size = 0;
861 a0c7a97e Isaku Yamahata
}
862 a0c7a97e Isaku Yamahata
863 876a350d Michael S. Tsirkin
static pcibus_t pci_bar_address(PCIDevice *d,
864 876a350d Michael S. Tsirkin
                                int reg, uint8_t type, pcibus_t size)
865 876a350d Michael S. Tsirkin
{
866 876a350d Michael S. Tsirkin
    pcibus_t new_addr, last_addr;
867 876a350d Michael S. Tsirkin
    int bar = pci_bar(d, reg);
868 876a350d Michael S. Tsirkin
    uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
869 876a350d Michael S. Tsirkin
870 876a350d Michael S. Tsirkin
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
871 876a350d Michael S. Tsirkin
        if (!(cmd & PCI_COMMAND_IO)) {
872 876a350d Michael S. Tsirkin
            return PCI_BAR_UNMAPPED;
873 876a350d Michael S. Tsirkin
        }
874 876a350d Michael S. Tsirkin
        new_addr = pci_get_long(d->config + bar) & ~(size - 1);
875 876a350d Michael S. Tsirkin
        last_addr = new_addr + size - 1;
876 876a350d Michael S. Tsirkin
        /* NOTE: we have only 64K ioports on PC */
877 876a350d Michael S. Tsirkin
        if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) {
878 876a350d Michael S. Tsirkin
            return PCI_BAR_UNMAPPED;
879 876a350d Michael S. Tsirkin
        }
880 876a350d Michael S. Tsirkin
        return new_addr;
881 876a350d Michael S. Tsirkin
    }
882 876a350d Michael S. Tsirkin
883 876a350d Michael S. Tsirkin
    if (!(cmd & PCI_COMMAND_MEMORY)) {
884 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
885 876a350d Michael S. Tsirkin
    }
886 876a350d Michael S. Tsirkin
    if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
887 876a350d Michael S. Tsirkin
        new_addr = pci_get_quad(d->config + bar);
888 876a350d Michael S. Tsirkin
    } else {
889 876a350d Michael S. Tsirkin
        new_addr = pci_get_long(d->config + bar);
890 876a350d Michael S. Tsirkin
    }
891 876a350d Michael S. Tsirkin
    /* the ROM slot has a specific enable bit */
892 876a350d Michael S. Tsirkin
    if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
893 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
894 876a350d Michael S. Tsirkin
    }
895 876a350d Michael S. Tsirkin
    new_addr &= ~(size - 1);
896 876a350d Michael S. Tsirkin
    last_addr = new_addr + size - 1;
897 876a350d Michael S. Tsirkin
    /* NOTE: we do not support wrapping */
898 876a350d Michael S. Tsirkin
    /* XXX: as we cannot support really dynamic
899 876a350d Michael S. Tsirkin
       mappings, we handle specific values as invalid
900 876a350d Michael S. Tsirkin
       mappings. */
901 876a350d Michael S. Tsirkin
    if (last_addr <= new_addr || new_addr == 0 ||
902 876a350d Michael S. Tsirkin
        last_addr == PCI_BAR_UNMAPPED) {
903 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
904 876a350d Michael S. Tsirkin
    }
905 876a350d Michael S. Tsirkin
906 876a350d Michael S. Tsirkin
    /* Now pcibus_t is 64bit.
907 876a350d Michael S. Tsirkin
     * Check if 32 bit BAR wraps around explicitly.
908 876a350d Michael S. Tsirkin
     * Without this, PC ide doesn't work well.
909 876a350d Michael S. Tsirkin
     * TODO: remove this work around.
910 876a350d Michael S. Tsirkin
     */
911 876a350d Michael S. Tsirkin
    if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
912 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
913 876a350d Michael S. Tsirkin
    }
914 876a350d Michael S. Tsirkin
915 876a350d Michael S. Tsirkin
    /*
916 876a350d Michael S. Tsirkin
     * OS is allowed to set BAR beyond its addressable
917 876a350d Michael S. Tsirkin
     * bits. For example, 32 bit OS can set 64bit bar
918 876a350d Michael S. Tsirkin
     * to >4G. Check it. TODO: we might need to support
919 876a350d Michael S. Tsirkin
     * it in the future for e.g. PAE.
920 876a350d Michael S. Tsirkin
     */
921 876a350d Michael S. Tsirkin
    if (last_addr >= TARGET_PHYS_ADDR_MAX) {
922 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
923 876a350d Michael S. Tsirkin
    }
924 876a350d Michael S. Tsirkin
925 876a350d Michael S. Tsirkin
    return new_addr;
926 876a350d Michael S. Tsirkin
}
927 876a350d Michael S. Tsirkin
928 0ac32c83 bellard
static void pci_update_mappings(PCIDevice *d)
929 0ac32c83 bellard
{
930 0ac32c83 bellard
    PCIIORegion *r;
931 876a350d Michael S. Tsirkin
    int i;
932 c71b5b4a Blue Swirl
    pcibus_t new_addr, filtered_size;
933 3b46e624 ths
934 8a8696a3 bellard
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
935 0ac32c83 bellard
        r = &d->io_regions[i];
936 a9688570 Isaku Yamahata
937 a9688570 Isaku Yamahata
        /* this region isn't registered */
938 ec503442 Isaku Yamahata
        if (!r->size)
939 a9688570 Isaku Yamahata
            continue;
940 a9688570 Isaku Yamahata
941 876a350d Michael S. Tsirkin
        new_addr = pci_bar_address(d, i, r->type, r->size);
942 a9688570 Isaku Yamahata
943 a0c7a97e Isaku Yamahata
        /* bridge filtering */
944 a0c7a97e Isaku Yamahata
        filtered_size = r->size;
945 a0c7a97e Isaku Yamahata
        if (new_addr != PCI_BAR_UNMAPPED) {
946 a0c7a97e Isaku Yamahata
            pci_bridge_filter(d, &new_addr, &filtered_size, r->type);
947 a0c7a97e Isaku Yamahata
        }
948 a0c7a97e Isaku Yamahata
949 a9688570 Isaku Yamahata
        /* This bar isn't changed */
950 a0c7a97e Isaku Yamahata
        if (new_addr == r->addr && filtered_size == r->filtered_size)
951 a9688570 Isaku Yamahata
            continue;
952 a9688570 Isaku Yamahata
953 a9688570 Isaku Yamahata
        /* now do the real mapping */
954 a9688570 Isaku Yamahata
        if (r->addr != PCI_BAR_UNMAPPED) {
955 a9688570 Isaku Yamahata
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
956 a9688570 Isaku Yamahata
                int class;
957 a9688570 Isaku Yamahata
                /* NOTE: specific hack for IDE in PC case:
958 a9688570 Isaku Yamahata
                   only one byte must be mapped. */
959 a9688570 Isaku Yamahata
                class = pci_get_word(d->config + PCI_CLASS_DEVICE);
960 a9688570 Isaku Yamahata
                if (class == 0x0101 && r->size == 4) {
961 a9688570 Isaku Yamahata
                    isa_unassign_ioport(r->addr + 2, 1);
962 a9688570 Isaku Yamahata
                } else {
963 a0c7a97e Isaku Yamahata
                    isa_unassign_ioport(r->addr, r->filtered_size);
964 0ac32c83 bellard
                }
965 a9688570 Isaku Yamahata
            } else {
966 c71b5b4a Blue Swirl
                cpu_register_physical_memory(pci_to_cpu_addr(d->bus, r->addr),
967 a0c7a97e Isaku Yamahata
                                             r->filtered_size,
968 a9688570 Isaku Yamahata
                                             IO_MEM_UNASSIGNED);
969 a0c7a97e Isaku Yamahata
                qemu_unregister_coalesced_mmio(r->addr, r->filtered_size);
970 0ac32c83 bellard
            }
971 0ac32c83 bellard
        }
972 a9688570 Isaku Yamahata
        r->addr = new_addr;
973 a0c7a97e Isaku Yamahata
        r->filtered_size = filtered_size;
974 a9688570 Isaku Yamahata
        if (r->addr != PCI_BAR_UNMAPPED) {
975 a0c7a97e Isaku Yamahata
            /*
976 a0c7a97e Isaku Yamahata
             * TODO: currently almost all the map funcions assumes
977 a0c7a97e Isaku Yamahata
             * filtered_size == size and addr & ~(size - 1) == addr.
978 a0c7a97e Isaku Yamahata
             * However with bridge filtering, they aren't always true.
979 a0c7a97e Isaku Yamahata
             * Teach them such cases, such that filtered_size < size and
980 a0c7a97e Isaku Yamahata
             * addr & (size - 1) != 0.
981 a0c7a97e Isaku Yamahata
             */
982 cf616802 Blue Swirl
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
983 cf616802 Blue Swirl
                r->map_func(d, i, r->addr, r->filtered_size, r->type);
984 cf616802 Blue Swirl
            } else {
985 cf616802 Blue Swirl
                r->map_func(d, i, pci_to_cpu_addr(d->bus, r->addr),
986 cf616802 Blue Swirl
                            r->filtered_size, r->type);
987 cf616802 Blue Swirl
            }
988 a9688570 Isaku Yamahata
        }
989 0ac32c83 bellard
    }
990 0ac32c83 bellard
}
991 0ac32c83 bellard
992 a7b15a5c Michael S. Tsirkin
static inline int pci_irq_disabled(PCIDevice *d)
993 a7b15a5c Michael S. Tsirkin
{
994 a7b15a5c Michael S. Tsirkin
    return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
995 a7b15a5c Michael S. Tsirkin
}
996 a7b15a5c Michael S. Tsirkin
997 a7b15a5c Michael S. Tsirkin
/* Called after interrupt disabled field update in config space,
998 a7b15a5c Michael S. Tsirkin
 * assert/deassert interrupts if necessary.
999 a7b15a5c Michael S. Tsirkin
 * Gets original interrupt disable bit value (before update). */
1000 a7b15a5c Michael S. Tsirkin
static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
1001 a7b15a5c Michael S. Tsirkin
{
1002 a7b15a5c Michael S. Tsirkin
    int i, disabled = pci_irq_disabled(d);
1003 a7b15a5c Michael S. Tsirkin
    if (disabled == was_irq_disabled)
1004 a7b15a5c Michael S. Tsirkin
        return;
1005 a7b15a5c Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
1006 a7b15a5c Michael S. Tsirkin
        int state = pci_irq_state(d, i);
1007 a7b15a5c Michael S. Tsirkin
        pci_change_irq_level(d, i, disabled ? -state : state);
1008 a7b15a5c Michael S. Tsirkin
    }
1009 a7b15a5c Michael S. Tsirkin
}
1010 a7b15a5c Michael S. Tsirkin
1011 5fafdf24 ths
uint32_t pci_default_read_config(PCIDevice *d,
1012 0ac32c83 bellard
                                 uint32_t address, int len)
1013 69b91039 bellard
{
1014 5029fe12 Isaku Yamahata
    uint32_t val = 0;
1015 5029fe12 Isaku Yamahata
    assert(len == 1 || len == 2 || len == 4);
1016 a9f49946 Isaku Yamahata
    len = MIN(len, pci_config_size(d) - address);
1017 5029fe12 Isaku Yamahata
    memcpy(&val, d->config + address, len);
1018 5029fe12 Isaku Yamahata
    return le32_to_cpu(val);
1019 0ac32c83 bellard
}
1020 0ac32c83 bellard
1021 b7ee1603 Michael S. Tsirkin
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
1022 0ac32c83 bellard
{
1023 a7b15a5c Michael S. Tsirkin
    int i, was_irq_disabled = pci_irq_disabled(d);
1024 a9f49946 Isaku Yamahata
    uint32_t config_size = pci_config_size(d);
1025 0ac32c83 bellard
1026 91011d4f Stefan Weil
    for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) {
1027 91011d4f Stefan Weil
        uint8_t wmask = d->wmask[addr + i];
1028 91011d4f Stefan Weil
        d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
1029 0ac32c83 bellard
    }
1030 260c0cd3 Isaku Yamahata
    if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1031 edb00035 Isaku Yamahata
        ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1032 edb00035 Isaku Yamahata
        ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1033 260c0cd3 Isaku Yamahata
        range_covers_byte(addr, l, PCI_COMMAND))
1034 0ac32c83 bellard
        pci_update_mappings(d);
1035 a7b15a5c Michael S. Tsirkin
1036 a7b15a5c Michael S. Tsirkin
    if (range_covers_byte(addr, l, PCI_COMMAND))
1037 a7b15a5c Michael S. Tsirkin
        pci_update_irq_disabled(d, was_irq_disabled);
1038 69b91039 bellard
}
1039 69b91039 bellard
1040 502a5395 pbrook
/***********************************************************/
1041 502a5395 pbrook
/* generic PCI irq support */
1042 30468f78 bellard
1043 502a5395 pbrook
/* 0 <= irq_num <= 3. level must be 0 or 1 */
1044 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level)
1045 69b91039 bellard
{
1046 a60380a5 Juan Quintela
    PCIDevice *pci_dev = opaque;
1047 80b3ada7 pbrook
    int change;
1048 3b46e624 ths
1049 d036bb21 Michael S. Tsirkin
    change = level - pci_irq_state(pci_dev, irq_num);
1050 80b3ada7 pbrook
    if (!change)
1051 80b3ada7 pbrook
        return;
1052 d2b59317 pbrook
1053 d036bb21 Michael S. Tsirkin
    pci_set_irq_state(pci_dev, irq_num, level);
1054 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(pci_dev);
1055 a7b15a5c Michael S. Tsirkin
    if (pci_irq_disabled(pci_dev))
1056 a7b15a5c Michael S. Tsirkin
        return;
1057 d036bb21 Michael S. Tsirkin
    pci_change_irq_level(pci_dev, irq_num, change);
1058 69b91039 bellard
}
1059 69b91039 bellard
1060 502a5395 pbrook
/***********************************************************/
1061 502a5395 pbrook
/* monitor info on PCI */
1062 0ac32c83 bellard
1063 6650ee6d pbrook
typedef struct {
1064 6650ee6d pbrook
    uint16_t class;
1065 6650ee6d pbrook
    const char *desc;
1066 6650ee6d pbrook
} pci_class_desc;
1067 6650ee6d pbrook
1068 09bc878a blueswir1
static const pci_class_desc pci_class_descriptions[] =
1069 6650ee6d pbrook
{
1070 4ca9c76f pbrook
    { 0x0100, "SCSI controller"},
1071 6650ee6d pbrook
    { 0x0101, "IDE controller"},
1072 dcb5b19a ths
    { 0x0102, "Floppy controller"},
1073 dcb5b19a ths
    { 0x0103, "IPI controller"},
1074 dcb5b19a ths
    { 0x0104, "RAID controller"},
1075 dcb5b19a ths
    { 0x0106, "SATA controller"},
1076 dcb5b19a ths
    { 0x0107, "SAS controller"},
1077 dcb5b19a ths
    { 0x0180, "Storage controller"},
1078 6650ee6d pbrook
    { 0x0200, "Ethernet controller"},
1079 dcb5b19a ths
    { 0x0201, "Token Ring controller"},
1080 dcb5b19a ths
    { 0x0202, "FDDI controller"},
1081 dcb5b19a ths
    { 0x0203, "ATM controller"},
1082 dcb5b19a ths
    { 0x0280, "Network controller"},
1083 6650ee6d pbrook
    { 0x0300, "VGA controller"},
1084 dcb5b19a ths
    { 0x0301, "XGA controller"},
1085 dcb5b19a ths
    { 0x0302, "3D controller"},
1086 dcb5b19a ths
    { 0x0380, "Display controller"},
1087 dcb5b19a ths
    { 0x0400, "Video controller"},
1088 dcb5b19a ths
    { 0x0401, "Audio controller"},
1089 dcb5b19a ths
    { 0x0402, "Phone"},
1090 dcb5b19a ths
    { 0x0480, "Multimedia controller"},
1091 dcb5b19a ths
    { 0x0500, "RAM controller"},
1092 dcb5b19a ths
    { 0x0501, "Flash controller"},
1093 dcb5b19a ths
    { 0x0580, "Memory controller"},
1094 6650ee6d pbrook
    { 0x0600, "Host bridge"},
1095 6650ee6d pbrook
    { 0x0601, "ISA bridge"},
1096 dcb5b19a ths
    { 0x0602, "EISA bridge"},
1097 dcb5b19a ths
    { 0x0603, "MC bridge"},
1098 6650ee6d pbrook
    { 0x0604, "PCI bridge"},
1099 dcb5b19a ths
    { 0x0605, "PCMCIA bridge"},
1100 dcb5b19a ths
    { 0x0606, "NUBUS bridge"},
1101 dcb5b19a ths
    { 0x0607, "CARDBUS bridge"},
1102 dcb5b19a ths
    { 0x0608, "RACEWAY bridge"},
1103 dcb5b19a ths
    { 0x0680, "Bridge"},
1104 6650ee6d pbrook
    { 0x0c03, "USB controller"},
1105 6650ee6d pbrook
    { 0, NULL}
1106 6650ee6d pbrook
};
1107 6650ee6d pbrook
1108 163c8a59 Luiz Capitulino
static void pci_for_each_device_under_bus(PCIBus *bus,
1109 163c8a59 Luiz Capitulino
                                          void (*fn)(PCIBus *b, PCIDevice *d))
1110 30468f78 bellard
{
1111 163c8a59 Luiz Capitulino
    PCIDevice *d;
1112 163c8a59 Luiz Capitulino
    int devfn;
1113 30468f78 bellard
1114 163c8a59 Luiz Capitulino
    for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1115 163c8a59 Luiz Capitulino
        d = bus->devices[devfn];
1116 163c8a59 Luiz Capitulino
        if (d) {
1117 163c8a59 Luiz Capitulino
            fn(bus, d);
1118 163c8a59 Luiz Capitulino
        }
1119 163c8a59 Luiz Capitulino
    }
1120 163c8a59 Luiz Capitulino
}
1121 163c8a59 Luiz Capitulino
1122 163c8a59 Luiz Capitulino
void pci_for_each_device(PCIBus *bus, int bus_num,
1123 163c8a59 Luiz Capitulino
                         void (*fn)(PCIBus *b, PCIDevice *d))
1124 163c8a59 Luiz Capitulino
{
1125 163c8a59 Luiz Capitulino
    bus = pci_find_bus(bus, bus_num);
1126 163c8a59 Luiz Capitulino
1127 163c8a59 Luiz Capitulino
    if (bus) {
1128 163c8a59 Luiz Capitulino
        pci_for_each_device_under_bus(bus, fn);
1129 163c8a59 Luiz Capitulino
    }
1130 163c8a59 Luiz Capitulino
}
1131 163c8a59 Luiz Capitulino
1132 163c8a59 Luiz Capitulino
static void pci_device_print(Monitor *mon, QDict *device)
1133 163c8a59 Luiz Capitulino
{
1134 163c8a59 Luiz Capitulino
    QDict *qdict;
1135 163c8a59 Luiz Capitulino
    QListEntry *entry;
1136 163c8a59 Luiz Capitulino
    uint64_t addr, size;
1137 163c8a59 Luiz Capitulino
1138 163c8a59 Luiz Capitulino
    monitor_printf(mon, "  Bus %2" PRId64 ", ", qdict_get_int(device, "bus"));
1139 163c8a59 Luiz Capitulino
    monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
1140 163c8a59 Luiz Capitulino
                        qdict_get_int(device, "slot"),
1141 163c8a59 Luiz Capitulino
                        qdict_get_int(device, "function"));
1142 376253ec aliguori
    monitor_printf(mon, "    ");
1143 163c8a59 Luiz Capitulino
1144 163c8a59 Luiz Capitulino
    qdict = qdict_get_qdict(device, "class_info");
1145 163c8a59 Luiz Capitulino
    if (qdict_haskey(qdict, "desc")) {
1146 163c8a59 Luiz Capitulino
        monitor_printf(mon, "%s", qdict_get_str(qdict, "desc"));
1147 6650ee6d pbrook
    } else {
1148 163c8a59 Luiz Capitulino
        monitor_printf(mon, "Class %04" PRId64, qdict_get_int(qdict, "class"));
1149 72cc6cfe bellard
    }
1150 30468f78 bellard
1151 163c8a59 Luiz Capitulino
    qdict = qdict_get_qdict(device, "id");
1152 163c8a59 Luiz Capitulino
    monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
1153 163c8a59 Luiz Capitulino
                        qdict_get_int(qdict, "device"),
1154 163c8a59 Luiz Capitulino
                        qdict_get_int(qdict, "vendor"));
1155 163c8a59 Luiz Capitulino
1156 163c8a59 Luiz Capitulino
    if (qdict_haskey(device, "irq")) {
1157 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      IRQ %" PRId64 ".\n",
1158 163c8a59 Luiz Capitulino
                            qdict_get_int(device, "irq"));
1159 30468f78 bellard
    }
1160 b4dccd8d Isaku Yamahata
1161 163c8a59 Luiz Capitulino
    if (qdict_haskey(device, "pci_bridge")) {
1162 163c8a59 Luiz Capitulino
        QDict *info;
1163 163c8a59 Luiz Capitulino
1164 163c8a59 Luiz Capitulino
        qdict = qdict_get_qdict(device, "pci_bridge");
1165 163c8a59 Luiz Capitulino
1166 163c8a59 Luiz Capitulino
        info = qdict_get_qdict(qdict, "bus");
1167 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      BUS %" PRId64 ".\n",
1168 163c8a59 Luiz Capitulino
                            qdict_get_int(info, "number"));
1169 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
1170 163c8a59 Luiz Capitulino
                            qdict_get_int(info, "secondary"));
1171 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
1172 163c8a59 Luiz Capitulino
                            qdict_get_int(info, "subordinate"));
1173 b4dccd8d Isaku Yamahata
1174 163c8a59 Luiz Capitulino
        info = qdict_get_qdict(qdict, "io_range");
1175 b4dccd8d Isaku Yamahata
        monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
1176 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "base"),
1177 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "limit"));
1178 b4dccd8d Isaku Yamahata
1179 163c8a59 Luiz Capitulino
        info = qdict_get_qdict(qdict, "memory_range");
1180 b4dccd8d Isaku Yamahata
        monitor_printf(mon,
1181 b4dccd8d Isaku Yamahata
                       "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
1182 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "base"),
1183 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "limit"));
1184 b4dccd8d Isaku Yamahata
1185 163c8a59 Luiz Capitulino
        info = qdict_get_qdict(qdict, "prefetchable_range");
1186 b4dccd8d Isaku Yamahata
        monitor_printf(mon, "      prefetchable memory range "
1187 163c8a59 Luiz Capitulino
                       "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
1188 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "base"),
1189 163c8a59 Luiz Capitulino
        qdict_get_int(info, "limit"));
1190 80b3ada7 pbrook
    }
1191 14421258 Isaku Yamahata
1192 163c8a59 Luiz Capitulino
    QLIST_FOREACH_ENTRY(qdict_get_qlist(device, "regions"), entry) {
1193 163c8a59 Luiz Capitulino
        qdict = qobject_to_qdict(qlist_entry_obj(entry));
1194 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      BAR%d: ", (int) qdict_get_int(qdict, "bar"));
1195 163c8a59 Luiz Capitulino
1196 163c8a59 Luiz Capitulino
        addr = qdict_get_int(qdict, "address");
1197 163c8a59 Luiz Capitulino
        size = qdict_get_int(qdict, "size");
1198 163c8a59 Luiz Capitulino
1199 163c8a59 Luiz Capitulino
        if (!strcmp(qdict_get_str(qdict, "type"), "io")) {
1200 163c8a59 Luiz Capitulino
            monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
1201 163c8a59 Luiz Capitulino
                                " [0x%04"FMT_PCIBUS"].\n",
1202 163c8a59 Luiz Capitulino
                                addr, addr + size - 1);
1203 163c8a59 Luiz Capitulino
        } else {
1204 163c8a59 Luiz Capitulino
            monitor_printf(mon, "%d bit%s memory at 0x%08"FMT_PCIBUS
1205 89e8b13c Isaku Yamahata
                               " [0x%08"FMT_PCIBUS"].\n",
1206 163c8a59 Luiz Capitulino
                                qdict_get_bool(qdict, "mem_type_64") ? 64 : 32,
1207 163c8a59 Luiz Capitulino
                                qdict_get_bool(qdict, "prefetch") ?
1208 163c8a59 Luiz Capitulino
                                " prefetchable" : "", addr, addr + size - 1);
1209 502a5395 pbrook
        }
1210 77d4bc34 bellard
    }
1211 163c8a59 Luiz Capitulino
1212 163c8a59 Luiz Capitulino
    monitor_printf(mon, "      id \"%s\"\n", qdict_get_str(device, "qdev_id"));
1213 163c8a59 Luiz Capitulino
1214 d5e4acf7 Luiz Capitulino
    if (qdict_haskey(device, "pci_bridge")) {
1215 d5e4acf7 Luiz Capitulino
        qdict = qdict_get_qdict(device, "pci_bridge");
1216 d5e4acf7 Luiz Capitulino
        if (qdict_haskey(qdict, "devices")) {
1217 d5e4acf7 Luiz Capitulino
            QListEntry *dev;
1218 d5e4acf7 Luiz Capitulino
            QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
1219 d5e4acf7 Luiz Capitulino
                pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev)));
1220 d5e4acf7 Luiz Capitulino
            }
1221 d5e4acf7 Luiz Capitulino
        }
1222 d5e4acf7 Luiz Capitulino
    }
1223 163c8a59 Luiz Capitulino
}
1224 163c8a59 Luiz Capitulino
1225 163c8a59 Luiz Capitulino
void do_pci_info_print(Monitor *mon, const QObject *data)
1226 163c8a59 Luiz Capitulino
{
1227 163c8a59 Luiz Capitulino
    QListEntry *bus, *dev;
1228 163c8a59 Luiz Capitulino
1229 163c8a59 Luiz Capitulino
    QLIST_FOREACH_ENTRY(qobject_to_qlist(data), bus) {
1230 163c8a59 Luiz Capitulino
        QDict *qdict = qobject_to_qdict(qlist_entry_obj(bus));
1231 163c8a59 Luiz Capitulino
        QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
1232 163c8a59 Luiz Capitulino
            pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev)));
1233 163c8a59 Luiz Capitulino
        }
1234 80b3ada7 pbrook
    }
1235 384d8876 bellard
}
1236 384d8876 bellard
1237 163c8a59 Luiz Capitulino
static QObject *pci_get_dev_class(const PCIDevice *dev)
1238 163c8a59 Luiz Capitulino
{
1239 163c8a59 Luiz Capitulino
    int class;
1240 163c8a59 Luiz Capitulino
    const pci_class_desc *desc;
1241 163c8a59 Luiz Capitulino
1242 163c8a59 Luiz Capitulino
    class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
1243 163c8a59 Luiz Capitulino
    desc = pci_class_descriptions;
1244 163c8a59 Luiz Capitulino
    while (desc->desc && class != desc->class)
1245 163c8a59 Luiz Capitulino
        desc++;
1246 163c8a59 Luiz Capitulino
1247 163c8a59 Luiz Capitulino
    if (desc->desc) {
1248 163c8a59 Luiz Capitulino
        return qobject_from_jsonf("{ 'desc': %s, 'class': %d }",
1249 163c8a59 Luiz Capitulino
                                  desc->desc, class);
1250 163c8a59 Luiz Capitulino
    } else {
1251 163c8a59 Luiz Capitulino
        return qobject_from_jsonf("{ 'class': %d }", class);
1252 163c8a59 Luiz Capitulino
    }
1253 163c8a59 Luiz Capitulino
}
1254 163c8a59 Luiz Capitulino
1255 163c8a59 Luiz Capitulino
static QObject *pci_get_dev_id(const PCIDevice *dev)
1256 163c8a59 Luiz Capitulino
{
1257 163c8a59 Luiz Capitulino
    return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }",
1258 163c8a59 Luiz Capitulino
                              pci_get_word(dev->config + PCI_VENDOR_ID),
1259 163c8a59 Luiz Capitulino
                              pci_get_word(dev->config + PCI_DEVICE_ID));
1260 163c8a59 Luiz Capitulino
}
1261 163c8a59 Luiz Capitulino
1262 163c8a59 Luiz Capitulino
static QObject *pci_get_regions_list(const PCIDevice *dev)
1263 163c8a59 Luiz Capitulino
{
1264 163c8a59 Luiz Capitulino
    int i;
1265 163c8a59 Luiz Capitulino
    QList *regions_list;
1266 163c8a59 Luiz Capitulino
1267 163c8a59 Luiz Capitulino
    regions_list = qlist_new();
1268 163c8a59 Luiz Capitulino
1269 163c8a59 Luiz Capitulino
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
1270 163c8a59 Luiz Capitulino
        QObject *obj;
1271 163c8a59 Luiz Capitulino
        const PCIIORegion *r = &dev->io_regions[i];
1272 163c8a59 Luiz Capitulino
1273 163c8a59 Luiz Capitulino
        if (!r->size) {
1274 163c8a59 Luiz Capitulino
            continue;
1275 163c8a59 Luiz Capitulino
        }
1276 163c8a59 Luiz Capitulino
1277 163c8a59 Luiz Capitulino
        if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1278 163c8a59 Luiz Capitulino
            obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
1279 163c8a59 Luiz Capitulino
                                     "'address': %" PRId64 ", "
1280 163c8a59 Luiz Capitulino
                                     "'size': %" PRId64 " }",
1281 163c8a59 Luiz Capitulino
                                     i, r->addr, r->size);
1282 163c8a59 Luiz Capitulino
        } else {
1283 163c8a59 Luiz Capitulino
            int mem_type_64 = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64;
1284 163c8a59 Luiz Capitulino
1285 163c8a59 Luiz Capitulino
            obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
1286 163c8a59 Luiz Capitulino
                                     "'mem_type_64': %i, 'prefetch': %i, "
1287 163c8a59 Luiz Capitulino
                                     "'address': %" PRId64 ", "
1288 163c8a59 Luiz Capitulino
                                     "'size': %" PRId64 " }",
1289 163c8a59 Luiz Capitulino
                                     i, mem_type_64,
1290 163c8a59 Luiz Capitulino
                                     r->type & PCI_BASE_ADDRESS_MEM_PREFETCH,
1291 163c8a59 Luiz Capitulino
                                     r->addr, r->size);
1292 163c8a59 Luiz Capitulino
        }
1293 163c8a59 Luiz Capitulino
1294 163c8a59 Luiz Capitulino
        qlist_append_obj(regions_list, obj);
1295 163c8a59 Luiz Capitulino
    }
1296 163c8a59 Luiz Capitulino
1297 163c8a59 Luiz Capitulino
    return QOBJECT(regions_list);
1298 163c8a59 Luiz Capitulino
}
1299 163c8a59 Luiz Capitulino
1300 d5e4acf7 Luiz Capitulino
static QObject *pci_get_devices_list(PCIBus *bus, int bus_num);
1301 d5e4acf7 Luiz Capitulino
1302 d5e4acf7 Luiz Capitulino
static QObject *pci_get_dev_dict(PCIDevice *dev, PCIBus *bus, int bus_num)
1303 163c8a59 Luiz Capitulino
{
1304 b5937f29 Isaku Yamahata
    uint8_t type;
1305 163c8a59 Luiz Capitulino
    QObject *obj;
1306 163c8a59 Luiz Capitulino
1307 163c8a59 Luiz Capitulino
    obj = qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d,"                                       "'class_info': %p, 'id': %p, 'regions': %p,"
1308 163c8a59 Luiz Capitulino
                              " 'qdev_id': %s }",
1309 163c8a59 Luiz Capitulino
                              bus_num,
1310 163c8a59 Luiz Capitulino
                              PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
1311 163c8a59 Luiz Capitulino
                              pci_get_dev_class(dev), pci_get_dev_id(dev),
1312 163c8a59 Luiz Capitulino
                              pci_get_regions_list(dev),
1313 163c8a59 Luiz Capitulino
                              dev->qdev.id ? dev->qdev.id : "");
1314 163c8a59 Luiz Capitulino
1315 163c8a59 Luiz Capitulino
    if (dev->config[PCI_INTERRUPT_PIN] != 0) {
1316 163c8a59 Luiz Capitulino
        QDict *qdict = qobject_to_qdict(obj);
1317 163c8a59 Luiz Capitulino
        qdict_put(qdict, "irq", qint_from_int(dev->config[PCI_INTERRUPT_LINE]));
1318 163c8a59 Luiz Capitulino
    }
1319 163c8a59 Luiz Capitulino
1320 b5937f29 Isaku Yamahata
    type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1321 b5937f29 Isaku Yamahata
    if (type == PCI_HEADER_TYPE_BRIDGE) {
1322 163c8a59 Luiz Capitulino
        QDict *qdict;
1323 163c8a59 Luiz Capitulino
        QObject *pci_bridge;
1324 163c8a59 Luiz Capitulino
1325 163c8a59 Luiz Capitulino
        pci_bridge = qobject_from_jsonf("{ 'bus': "
1326 163c8a59 Luiz Capitulino
        "{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
1327 163c8a59 Luiz Capitulino
        "'io_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, "
1328 163c8a59 Luiz Capitulino
        "'memory_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, "
1329 163c8a59 Luiz Capitulino
        "'prefetchable_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "} }",
1330 c021f8e6 Blue Swirl
        dev->config[PCI_PRIMARY_BUS], dev->config[PCI_SECONDARY_BUS],
1331 163c8a59 Luiz Capitulino
        dev->config[PCI_SUBORDINATE_BUS],
1332 163c8a59 Luiz Capitulino
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO),
1333 163c8a59 Luiz Capitulino
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO),
1334 163c8a59 Luiz Capitulino
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY),
1335 163c8a59 Luiz Capitulino
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY),
1336 163c8a59 Luiz Capitulino
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY |
1337 163c8a59 Luiz Capitulino
                               PCI_BASE_ADDRESS_MEM_PREFETCH),
1338 163c8a59 Luiz Capitulino
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY |
1339 163c8a59 Luiz Capitulino
                                PCI_BASE_ADDRESS_MEM_PREFETCH));
1340 163c8a59 Luiz Capitulino
1341 c021f8e6 Blue Swirl
        if (dev->config[PCI_SECONDARY_BUS] != 0) {
1342 c021f8e6 Blue Swirl
            PCIBus *child_bus = pci_find_bus(bus, dev->config[PCI_SECONDARY_BUS]);
1343 d5e4acf7 Luiz Capitulino
1344 c021f8e6 Blue Swirl
            if (child_bus) {
1345 c021f8e6 Blue Swirl
                qdict = qobject_to_qdict(pci_bridge);
1346 c021f8e6 Blue Swirl
                qdict_put_obj(qdict, "devices",
1347 c021f8e6 Blue Swirl
                              pci_get_devices_list(child_bus,
1348 c021f8e6 Blue Swirl
                                                   dev->config[PCI_SECONDARY_BUS]));
1349 c021f8e6 Blue Swirl
            }
1350 c021f8e6 Blue Swirl
        }
1351 163c8a59 Luiz Capitulino
        qdict = qobject_to_qdict(obj);
1352 163c8a59 Luiz Capitulino
        qdict_put_obj(qdict, "pci_bridge", pci_bridge);
1353 163c8a59 Luiz Capitulino
    }
1354 163c8a59 Luiz Capitulino
1355 163c8a59 Luiz Capitulino
    return obj;
1356 163c8a59 Luiz Capitulino
}
1357 163c8a59 Luiz Capitulino
1358 163c8a59 Luiz Capitulino
static QObject *pci_get_devices_list(PCIBus *bus, int bus_num)
1359 384d8876 bellard
{
1360 502a5395 pbrook
    int devfn;
1361 163c8a59 Luiz Capitulino
    PCIDevice *dev;
1362 163c8a59 Luiz Capitulino
    QList *dev_list;
1363 3b46e624 ths
1364 163c8a59 Luiz Capitulino
    dev_list = qlist_new();
1365 163c8a59 Luiz Capitulino
1366 163c8a59 Luiz Capitulino
    for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1367 163c8a59 Luiz Capitulino
        dev = bus->devices[devfn];
1368 163c8a59 Luiz Capitulino
        if (dev) {
1369 d5e4acf7 Luiz Capitulino
            qlist_append_obj(dev_list, pci_get_dev_dict(dev, bus, bus_num));
1370 163c8a59 Luiz Capitulino
        }
1371 1074df4f Isaku Yamahata
    }
1372 163c8a59 Luiz Capitulino
1373 163c8a59 Luiz Capitulino
    return QOBJECT(dev_list);
1374 1074df4f Isaku Yamahata
}
1375 1074df4f Isaku Yamahata
1376 163c8a59 Luiz Capitulino
static QObject *pci_get_bus_dict(PCIBus *bus, int bus_num)
1377 1074df4f Isaku Yamahata
{
1378 e822a52a Isaku Yamahata
    bus = pci_find_bus(bus, bus_num);
1379 502a5395 pbrook
    if (bus) {
1380 163c8a59 Luiz Capitulino
        return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }",
1381 163c8a59 Luiz Capitulino
                                  bus_num, pci_get_devices_list(bus, bus_num));
1382 f2aa58c6 bellard
    }
1383 163c8a59 Luiz Capitulino
1384 163c8a59 Luiz Capitulino
    return NULL;
1385 f2aa58c6 bellard
}
1386 f2aa58c6 bellard
1387 163c8a59 Luiz Capitulino
void do_pci_info(Monitor *mon, QObject **ret_data)
1388 f2aa58c6 bellard
{
1389 163c8a59 Luiz Capitulino
    QList *bus_list;
1390 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
1391 163c8a59 Luiz Capitulino
1392 163c8a59 Luiz Capitulino
    bus_list = qlist_new();
1393 163c8a59 Luiz Capitulino
1394 e822a52a Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
1395 163c8a59 Luiz Capitulino
        QObject *obj = pci_get_bus_dict(host->bus, 0);
1396 163c8a59 Luiz Capitulino
        if (obj) {
1397 163c8a59 Luiz Capitulino
            qlist_append_obj(bus_list, obj);
1398 163c8a59 Luiz Capitulino
        }
1399 e822a52a Isaku Yamahata
    }
1400 163c8a59 Luiz Capitulino
1401 163c8a59 Luiz Capitulino
    *ret_data = QOBJECT(bus_list);
1402 77d4bc34 bellard
}
1403 a41b2ff2 pbrook
1404 cb457d76 aliguori
static const char * const pci_nic_models[] = {
1405 cb457d76 aliguori
    "ne2k_pci",
1406 cb457d76 aliguori
    "i82551",
1407 cb457d76 aliguori
    "i82557b",
1408 cb457d76 aliguori
    "i82559er",
1409 cb457d76 aliguori
    "rtl8139",
1410 cb457d76 aliguori
    "e1000",
1411 cb457d76 aliguori
    "pcnet",
1412 cb457d76 aliguori
    "virtio",
1413 cb457d76 aliguori
    NULL
1414 cb457d76 aliguori
};
1415 cb457d76 aliguori
1416 9d07d757 Paul Brook
static const char * const pci_nic_names[] = {
1417 9d07d757 Paul Brook
    "ne2k_pci",
1418 9d07d757 Paul Brook
    "i82551",
1419 9d07d757 Paul Brook
    "i82557b",
1420 9d07d757 Paul Brook
    "i82559er",
1421 9d07d757 Paul Brook
    "rtl8139",
1422 9d07d757 Paul Brook
    "e1000",
1423 9d07d757 Paul Brook
    "pcnet",
1424 53c25cea Paul Brook
    "virtio-net-pci",
1425 cb457d76 aliguori
    NULL
1426 cb457d76 aliguori
};
1427 cb457d76 aliguori
1428 a41b2ff2 pbrook
/* Initialize a PCI NIC.  */
1429 33e66b86 Markus Armbruster
/* FIXME callers should check for failure, but don't */
1430 5607c388 Markus Armbruster
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
1431 5607c388 Markus Armbruster
                        const char *default_devaddr)
1432 a41b2ff2 pbrook
{
1433 5607c388 Markus Armbruster
    const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1434 07caea31 Markus Armbruster
    PCIBus *bus;
1435 07caea31 Markus Armbruster
    int devfn;
1436 5607c388 Markus Armbruster
    PCIDevice *pci_dev;
1437 9d07d757 Paul Brook
    DeviceState *dev;
1438 cb457d76 aliguori
    int i;
1439 cb457d76 aliguori
1440 07caea31 Markus Armbruster
    i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1441 07caea31 Markus Armbruster
    if (i < 0)
1442 07caea31 Markus Armbruster
        return NULL;
1443 07caea31 Markus Armbruster
1444 07caea31 Markus Armbruster
    bus = pci_get_bus_devfn(&devfn, devaddr);
1445 07caea31 Markus Armbruster
    if (!bus) {
1446 1ecda02b Markus Armbruster
        error_report("Invalid PCI device address %s for device %s",
1447 1ecda02b Markus Armbruster
                     devaddr, pci_nic_names[i]);
1448 07caea31 Markus Armbruster
        return NULL;
1449 07caea31 Markus Armbruster
    }
1450 07caea31 Markus Armbruster
1451 499cf102 Markus Armbruster
    pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1452 9ee05825 Markus Armbruster
    dev = &pci_dev->qdev;
1453 1cc33683 Gerd Hoffmann
    qdev_set_nic_properties(dev, nd);
1454 07caea31 Markus Armbruster
    if (qdev_init(dev) < 0)
1455 07caea31 Markus Armbruster
        return NULL;
1456 9ee05825 Markus Armbruster
    return pci_dev;
1457 a41b2ff2 pbrook
}
1458 a41b2ff2 pbrook
1459 07caea31 Markus Armbruster
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
1460 07caea31 Markus Armbruster
                               const char *default_devaddr)
1461 07caea31 Markus Armbruster
{
1462 07caea31 Markus Armbruster
    PCIDevice *res;
1463 07caea31 Markus Armbruster
1464 07caea31 Markus Armbruster
    if (qemu_show_nic_models(nd->model, pci_nic_models))
1465 07caea31 Markus Armbruster
        exit(0);
1466 07caea31 Markus Armbruster
1467 07caea31 Markus Armbruster
    res = pci_nic_init(nd, default_model, default_devaddr);
1468 07caea31 Markus Armbruster
    if (!res)
1469 07caea31 Markus Armbruster
        exit(1);
1470 07caea31 Markus Armbruster
    return res;
1471 07caea31 Markus Armbruster
}
1472 07caea31 Markus Armbruster
1473 80b3ada7 pbrook
typedef struct {
1474 80b3ada7 pbrook
    PCIDevice dev;
1475 03587182 Gerd Hoffmann
    PCIBus bus;
1476 03587182 Gerd Hoffmann
    uint32_t vid;
1477 03587182 Gerd Hoffmann
    uint32_t did;
1478 80b3ada7 pbrook
} PCIBridge;
1479 80b3ada7 pbrook
1480 a0c7a97e Isaku Yamahata
1481 a0c7a97e Isaku Yamahata
static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d)
1482 a0c7a97e Isaku Yamahata
{
1483 a0c7a97e Isaku Yamahata
    pci_update_mappings(d);
1484 a0c7a97e Isaku Yamahata
}
1485 a0c7a97e Isaku Yamahata
1486 a0c7a97e Isaku Yamahata
static void pci_bridge_update_mappings(PCIBus *b)
1487 a0c7a97e Isaku Yamahata
{
1488 a0c7a97e Isaku Yamahata
    PCIBus *child;
1489 a0c7a97e Isaku Yamahata
1490 a0c7a97e Isaku Yamahata
    pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn);
1491 a0c7a97e Isaku Yamahata
1492 a0c7a97e Isaku Yamahata
    QLIST_FOREACH(child, &b->child, sibling) {
1493 a0c7a97e Isaku Yamahata
        pci_bridge_update_mappings(child);
1494 a0c7a97e Isaku Yamahata
    }
1495 a0c7a97e Isaku Yamahata
}
1496 a0c7a97e Isaku Yamahata
1497 9596ebb7 pbrook
static void pci_bridge_write_config(PCIDevice *d,
1498 80b3ada7 pbrook
                             uint32_t address, uint32_t val, int len)
1499 80b3ada7 pbrook
{
1500 80b3ada7 pbrook
    pci_default_write_config(d, address, val, len);
1501 a0c7a97e Isaku Yamahata
1502 a0c7a97e Isaku Yamahata
    if (/* io base/limit */
1503 a0c7a97e Isaku Yamahata
        ranges_overlap(address, len, PCI_IO_BASE, 2) ||
1504 a0c7a97e Isaku Yamahata
1505 a0c7a97e Isaku Yamahata
        /* memory base/limit, prefetchable base/limit and
1506 a0c7a97e Isaku Yamahata
           io base/limit upper 16 */
1507 a0c7a97e Isaku Yamahata
        ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
1508 a0c7a97e Isaku Yamahata
        pci_bridge_update_mappings(d->bus);
1509 a0c7a97e Isaku Yamahata
    }
1510 80b3ada7 pbrook
}
1511 80b3ada7 pbrook
1512 e822a52a Isaku Yamahata
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
1513 3ae80618 aliguori
{
1514 470e6363 Isaku Yamahata
    PCIBus *sec;
1515 3ae80618 aliguori
1516 470e6363 Isaku Yamahata
    if (!bus) {
1517 e822a52a Isaku Yamahata
        return NULL;
1518 470e6363 Isaku Yamahata
    }
1519 3ae80618 aliguori
1520 e822a52a Isaku Yamahata
    if (pci_bus_num(bus) == bus_num) {
1521 e822a52a Isaku Yamahata
        return bus;
1522 e822a52a Isaku Yamahata
    }
1523 e822a52a Isaku Yamahata
1524 e822a52a Isaku Yamahata
    /* try child bus */
1525 470e6363 Isaku Yamahata
    if (!bus->parent_dev /* host pci bridge */ ||
1526 470e6363 Isaku Yamahata
        (bus->parent_dev->config[PCI_SECONDARY_BUS] < bus_num &&
1527 470e6363 Isaku Yamahata
         bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS])) {
1528 470e6363 Isaku Yamahata
        for (; bus; bus = sec) {
1529 470e6363 Isaku Yamahata
            QLIST_FOREACH(sec, &bus->child, sibling) {
1530 470e6363 Isaku Yamahata
                assert(sec->parent_dev);
1531 470e6363 Isaku Yamahata
                if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
1532 470e6363 Isaku Yamahata
                    return sec;
1533 470e6363 Isaku Yamahata
                }
1534 470e6363 Isaku Yamahata
                if (sec->parent_dev->config[PCI_SECONDARY_BUS] < bus_num &&
1535 470e6363 Isaku Yamahata
                    bus_num <= sec->parent_dev->config[PCI_SUBORDINATE_BUS]) {
1536 470e6363 Isaku Yamahata
                    break;
1537 470e6363 Isaku Yamahata
                }
1538 c021f8e6 Blue Swirl
            }
1539 e822a52a Isaku Yamahata
        }
1540 e822a52a Isaku Yamahata
    }
1541 e822a52a Isaku Yamahata
1542 e822a52a Isaku Yamahata
    return NULL;
1543 3ae80618 aliguori
}
1544 3ae80618 aliguori
1545 e822a52a Isaku Yamahata
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
1546 3ae80618 aliguori
{
1547 e822a52a Isaku Yamahata
    bus = pci_find_bus(bus, bus_num);
1548 3ae80618 aliguori
1549 3ae80618 aliguori
    if (!bus)
1550 3ae80618 aliguori
        return NULL;
1551 3ae80618 aliguori
1552 3ae80618 aliguori
    return bus->devices[PCI_DEVFN(slot, function)];
1553 3ae80618 aliguori
}
1554 3ae80618 aliguori
1555 03587182 Gerd Hoffmann
static int pci_bridge_initfn(PCIDevice *dev)
1556 80b3ada7 pbrook
{
1557 03587182 Gerd Hoffmann
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev);
1558 480b9f24 blueswir1
1559 03587182 Gerd Hoffmann
    pci_config_set_vendor_id(s->dev.config, s->vid);
1560 03587182 Gerd Hoffmann
    pci_config_set_device_id(s->dev.config, s->did);
1561 480b9f24 blueswir1
1562 74c01823 Isaku Yamahata
    pci_set_word(dev->config + PCI_STATUS,
1563 74c01823 Isaku Yamahata
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1564 74c01823 Isaku Yamahata
    pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
1565 e327e323 Isaku Yamahata
    dev->config[PCI_HEADER_TYPE] =
1566 e327e323 Isaku Yamahata
        (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) |
1567 e327e323 Isaku Yamahata
        PCI_HEADER_TYPE_BRIDGE;
1568 74c01823 Isaku Yamahata
    pci_set_word(dev->config + PCI_SEC_STATUS,
1569 74c01823 Isaku Yamahata
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1570 03587182 Gerd Hoffmann
    return 0;
1571 03587182 Gerd Hoffmann
}
1572 80b3ada7 pbrook
1573 e822a52a Isaku Yamahata
static int pci_bridge_exitfn(PCIDevice *pci_dev)
1574 e822a52a Isaku Yamahata
{
1575 e822a52a Isaku Yamahata
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
1576 e822a52a Isaku Yamahata
    PCIBus *bus = &s->bus;
1577 e822a52a Isaku Yamahata
    pci_unregister_secondary_bus(bus);
1578 e822a52a Isaku Yamahata
    return 0;
1579 e822a52a Isaku Yamahata
}
1580 e822a52a Isaku Yamahata
1581 03587182 Gerd Hoffmann
PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
1582 03587182 Gerd Hoffmann
                        pci_map_irq_fn map_irq, const char *name)
1583 03587182 Gerd Hoffmann
{
1584 03587182 Gerd Hoffmann
    PCIDevice *dev;
1585 03587182 Gerd Hoffmann
    PCIBridge *s;
1586 03587182 Gerd Hoffmann
1587 499cf102 Markus Armbruster
    dev = pci_create(bus, devfn, "pci-bridge");
1588 03587182 Gerd Hoffmann
    qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
1589 03587182 Gerd Hoffmann
    qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
1590 e23a1b33 Markus Armbruster
    qdev_init_nofail(&dev->qdev);
1591 03587182 Gerd Hoffmann
1592 03587182 Gerd Hoffmann
    s = DO_UPCAST(PCIBridge, dev, dev);
1593 e822a52a Isaku Yamahata
    pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name);
1594 03587182 Gerd Hoffmann
    return &s->bus;
1595 80b3ada7 pbrook
}
1596 6b1b92d3 Paul Brook
1597 d6318738 Michael S. Tsirkin
PCIDevice *pci_bridge_get_device(PCIBus *bus)
1598 d6318738 Michael S. Tsirkin
{
1599 d6318738 Michael S. Tsirkin
    return bus->parent_dev;
1600 d6318738 Michael S. Tsirkin
}
1601 d6318738 Michael S. Tsirkin
1602 81a322d4 Gerd Hoffmann
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1603 6b1b92d3 Paul Brook
{
1604 6b1b92d3 Paul Brook
    PCIDevice *pci_dev = (PCIDevice *)qdev;
1605 02e2da45 Paul Brook
    PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1606 6b1b92d3 Paul Brook
    PCIBus *bus;
1607 ee995ffb Gerd Hoffmann
    int devfn, rc;
1608 6b1b92d3 Paul Brook
1609 a9f49946 Isaku Yamahata
    /* initialize cap_present for pci_is_express() and pci_config_size() */
1610 a9f49946 Isaku Yamahata
    if (info->is_express) {
1611 a9f49946 Isaku Yamahata
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1612 a9f49946 Isaku Yamahata
    }
1613 a9f49946 Isaku Yamahata
1614 02e2da45 Paul Brook
    bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1615 ee6847d1 Gerd Hoffmann
    devfn = pci_dev->devfn;
1616 16eaedf2 Gerd Hoffmann
    pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1617 fb231628 Isaku Yamahata
                                     info->config_read, info->config_write,
1618 e327e323 Isaku Yamahata
                                     info->is_bridge);
1619 09e3acc6 Gerd Hoffmann
    if (pci_dev == NULL)
1620 09e3acc6 Gerd Hoffmann
        return -1;
1621 ee995ffb Gerd Hoffmann
    rc = info->init(pci_dev);
1622 925fe64a Alex Williamson
    if (rc != 0) {
1623 925fe64a Alex Williamson
        do_pci_unregister_device(pci_dev);
1624 ee995ffb Gerd Hoffmann
        return rc;
1625 925fe64a Alex Williamson
    }
1626 8c52c8f3 Gerd Hoffmann
1627 8c52c8f3 Gerd Hoffmann
    /* rom loading */
1628 8c52c8f3 Gerd Hoffmann
    if (pci_dev->romfile == NULL && info->romfile != NULL)
1629 8c52c8f3 Gerd Hoffmann
        pci_dev->romfile = qemu_strdup(info->romfile);
1630 8c52c8f3 Gerd Hoffmann
    pci_add_option_rom(pci_dev);
1631 8c52c8f3 Gerd Hoffmann
1632 ee995ffb Gerd Hoffmann
    if (qdev->hotplugged)
1633 87c30546 Isaku Yamahata
        bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
1634 ee995ffb Gerd Hoffmann
    return 0;
1635 ee995ffb Gerd Hoffmann
}
1636 ee995ffb Gerd Hoffmann
1637 ee995ffb Gerd Hoffmann
static int pci_unplug_device(DeviceState *qdev)
1638 ee995ffb Gerd Hoffmann
{
1639 ee995ffb Gerd Hoffmann
    PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1640 ee995ffb Gerd Hoffmann
1641 87c30546 Isaku Yamahata
    dev->bus->hotplug(dev->bus->hotplug_qdev, dev, 0);
1642 ee995ffb Gerd Hoffmann
    return 0;
1643 6b1b92d3 Paul Brook
}
1644 6b1b92d3 Paul Brook
1645 0aab0d3a Gerd Hoffmann
void pci_qdev_register(PCIDeviceInfo *info)
1646 6b1b92d3 Paul Brook
{
1647 02e2da45 Paul Brook
    info->qdev.init = pci_qdev_init;
1648 ee995ffb Gerd Hoffmann
    info->qdev.unplug = pci_unplug_device;
1649 a36a344d Gerd Hoffmann
    info->qdev.exit = pci_unregister_device;
1650 10c4c98a Gerd Hoffmann
    info->qdev.bus_info = &pci_bus_info;
1651 074f2fff Gerd Hoffmann
    qdev_register(&info->qdev);
1652 6b1b92d3 Paul Brook
}
1653 6b1b92d3 Paul Brook
1654 0aab0d3a Gerd Hoffmann
void pci_qdev_register_many(PCIDeviceInfo *info)
1655 0aab0d3a Gerd Hoffmann
{
1656 0aab0d3a Gerd Hoffmann
    while (info->qdev.name) {
1657 0aab0d3a Gerd Hoffmann
        pci_qdev_register(info);
1658 0aab0d3a Gerd Hoffmann
        info++;
1659 0aab0d3a Gerd Hoffmann
    }
1660 0aab0d3a Gerd Hoffmann
}
1661 0aab0d3a Gerd Hoffmann
1662 499cf102 Markus Armbruster
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1663 6b1b92d3 Paul Brook
{
1664 6b1b92d3 Paul Brook
    DeviceState *dev;
1665 6b1b92d3 Paul Brook
1666 02e2da45 Paul Brook
    dev = qdev_create(&bus->qbus, name);
1667 a6307b08 Gerd Hoffmann
    qdev_prop_set_uint32(dev, "addr", devfn);
1668 71077c1c Gerd Hoffmann
    return DO_UPCAST(PCIDevice, qdev, dev);
1669 71077c1c Gerd Hoffmann
}
1670 6b1b92d3 Paul Brook
1671 71077c1c Gerd Hoffmann
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1672 71077c1c Gerd Hoffmann
{
1673 499cf102 Markus Armbruster
    PCIDevice *dev = pci_create(bus, devfn, name);
1674 e23a1b33 Markus Armbruster
    qdev_init_nofail(&dev->qdev);
1675 71077c1c Gerd Hoffmann
    return dev;
1676 6b1b92d3 Paul Brook
}
1677 6f4cbd39 Michael S. Tsirkin
1678 6f4cbd39 Michael S. Tsirkin
static int pci_find_space(PCIDevice *pdev, uint8_t size)
1679 6f4cbd39 Michael S. Tsirkin
{
1680 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(pdev);
1681 6f4cbd39 Michael S. Tsirkin
    int offset = PCI_CONFIG_HEADER_SIZE;
1682 6f4cbd39 Michael S. Tsirkin
    int i;
1683 a9f49946 Isaku Yamahata
    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
1684 6f4cbd39 Michael S. Tsirkin
        if (pdev->used[i])
1685 6f4cbd39 Michael S. Tsirkin
            offset = i + 1;
1686 6f4cbd39 Michael S. Tsirkin
        else if (i - offset + 1 == size)
1687 6f4cbd39 Michael S. Tsirkin
            return offset;
1688 6f4cbd39 Michael S. Tsirkin
    return 0;
1689 6f4cbd39 Michael S. Tsirkin
}
1690 6f4cbd39 Michael S. Tsirkin
1691 6f4cbd39 Michael S. Tsirkin
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1692 6f4cbd39 Michael S. Tsirkin
                                        uint8_t *prev_p)
1693 6f4cbd39 Michael S. Tsirkin
{
1694 6f4cbd39 Michael S. Tsirkin
    uint8_t next, prev;
1695 6f4cbd39 Michael S. Tsirkin
1696 6f4cbd39 Michael S. Tsirkin
    if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1697 6f4cbd39 Michael S. Tsirkin
        return 0;
1698 6f4cbd39 Michael S. Tsirkin
1699 6f4cbd39 Michael S. Tsirkin
    for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1700 6f4cbd39 Michael S. Tsirkin
         prev = next + PCI_CAP_LIST_NEXT)
1701 6f4cbd39 Michael S. Tsirkin
        if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1702 6f4cbd39 Michael S. Tsirkin
            break;
1703 6f4cbd39 Michael S. Tsirkin
1704 6f4cbd39 Michael S. Tsirkin
    if (prev_p)
1705 6f4cbd39 Michael S. Tsirkin
        *prev_p = prev;
1706 6f4cbd39 Michael S. Tsirkin
    return next;
1707 6f4cbd39 Michael S. Tsirkin
}
1708 6f4cbd39 Michael S. Tsirkin
1709 c2039bd0 Anthony Liguori
static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type)
1710 c2039bd0 Anthony Liguori
{
1711 c2039bd0 Anthony Liguori
    cpu_register_physical_memory(addr, size, pdev->rom_offset);
1712 c2039bd0 Anthony Liguori
}
1713 c2039bd0 Anthony Liguori
1714 c2039bd0 Anthony Liguori
/* Add an option rom for the device */
1715 8c52c8f3 Gerd Hoffmann
static int pci_add_option_rom(PCIDevice *pdev)
1716 c2039bd0 Anthony Liguori
{
1717 c2039bd0 Anthony Liguori
    int size;
1718 c2039bd0 Anthony Liguori
    char *path;
1719 c2039bd0 Anthony Liguori
    void *ptr;
1720 1724f049 Alex Williamson
    char name[32];
1721 c2039bd0 Anthony Liguori
1722 8c52c8f3 Gerd Hoffmann
    if (!pdev->romfile)
1723 8c52c8f3 Gerd Hoffmann
        return 0;
1724 8c52c8f3 Gerd Hoffmann
    if (strlen(pdev->romfile) == 0)
1725 8c52c8f3 Gerd Hoffmann
        return 0;
1726 8c52c8f3 Gerd Hoffmann
1727 88169ddf Gerd Hoffmann
    if (!pdev->rom_bar) {
1728 88169ddf Gerd Hoffmann
        /*
1729 88169ddf Gerd Hoffmann
         * Load rom via fw_cfg instead of creating a rom bar,
1730 88169ddf Gerd Hoffmann
         * for 0.11 compatibility.
1731 88169ddf Gerd Hoffmann
         */
1732 88169ddf Gerd Hoffmann
        int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
1733 88169ddf Gerd Hoffmann
        if (class == 0x0300) {
1734 88169ddf Gerd Hoffmann
            rom_add_vga(pdev->romfile);
1735 88169ddf Gerd Hoffmann
        } else {
1736 88169ddf Gerd Hoffmann
            rom_add_option(pdev->romfile);
1737 88169ddf Gerd Hoffmann
        }
1738 88169ddf Gerd Hoffmann
        return 0;
1739 88169ddf Gerd Hoffmann
    }
1740 88169ddf Gerd Hoffmann
1741 8c52c8f3 Gerd Hoffmann
    path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
1742 c2039bd0 Anthony Liguori
    if (path == NULL) {
1743 8c52c8f3 Gerd Hoffmann
        path = qemu_strdup(pdev->romfile);
1744 c2039bd0 Anthony Liguori
    }
1745 c2039bd0 Anthony Liguori
1746 c2039bd0 Anthony Liguori
    size = get_image_size(path);
1747 8c52c8f3 Gerd Hoffmann
    if (size < 0) {
1748 1ecda02b Markus Armbruster
        error_report("%s: failed to find romfile \"%s\"",
1749 1ecda02b Markus Armbruster
                     __FUNCTION__, pdev->romfile);
1750 8c52c8f3 Gerd Hoffmann
        return -1;
1751 8c52c8f3 Gerd Hoffmann
    }
1752 c2039bd0 Anthony Liguori
    if (size & (size - 1)) {
1753 c2039bd0 Anthony Liguori
        size = 1 << qemu_fls(size);
1754 c2039bd0 Anthony Liguori
    }
1755 c2039bd0 Anthony Liguori
1756 1724f049 Alex Williamson
    if (pdev->qdev.info->vmsd)
1757 1724f049 Alex Williamson
        snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->vmsd->name);
1758 1724f049 Alex Williamson
    else
1759 1724f049 Alex Williamson
        snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->name);
1760 1724f049 Alex Williamson
    pdev->rom_offset = qemu_ram_alloc(&pdev->qdev, name, size);
1761 c2039bd0 Anthony Liguori
1762 c2039bd0 Anthony Liguori
    ptr = qemu_get_ram_ptr(pdev->rom_offset);
1763 c2039bd0 Anthony Liguori
    load_image(path, ptr);
1764 c2039bd0 Anthony Liguori
    qemu_free(path);
1765 c2039bd0 Anthony Liguori
1766 c2039bd0 Anthony Liguori
    pci_register_bar(pdev, PCI_ROM_SLOT, size,
1767 c2039bd0 Anthony Liguori
                     0, pci_map_option_rom);
1768 c2039bd0 Anthony Liguori
1769 c2039bd0 Anthony Liguori
    return 0;
1770 c2039bd0 Anthony Liguori
}
1771 c2039bd0 Anthony Liguori
1772 230741dc Alex Williamson
static void pci_del_option_rom(PCIDevice *pdev)
1773 230741dc Alex Williamson
{
1774 230741dc Alex Williamson
    if (!pdev->rom_offset)
1775 230741dc Alex Williamson
        return;
1776 230741dc Alex Williamson
1777 230741dc Alex Williamson
    qemu_ram_free(pdev->rom_offset);
1778 230741dc Alex Williamson
    pdev->rom_offset = 0;
1779 230741dc Alex Williamson
}
1780 230741dc Alex Williamson
1781 6f4cbd39 Michael S. Tsirkin
/* Reserve space and add capability to the linked list in pci config space */
1782 1db5a3aa Michael S. Tsirkin
int pci_add_capability_at_offset(PCIDevice *pdev, uint8_t cap_id,
1783 1db5a3aa Michael S. Tsirkin
                                 uint8_t offset, uint8_t size)
1784 6f4cbd39 Michael S. Tsirkin
{
1785 6f4cbd39 Michael S. Tsirkin
    uint8_t *config = pdev->config + offset;
1786 6f4cbd39 Michael S. Tsirkin
    config[PCI_CAP_LIST_ID] = cap_id;
1787 6f4cbd39 Michael S. Tsirkin
    config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1788 6f4cbd39 Michael S. Tsirkin
    pdev->config[PCI_CAPABILITY_LIST] = offset;
1789 6f4cbd39 Michael S. Tsirkin
    pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1790 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0xFF, size);
1791 6f4cbd39 Michael S. Tsirkin
    /* Make capability read-only by default */
1792 6f4cbd39 Michael S. Tsirkin
    memset(pdev->wmask + offset, 0, size);
1793 bd4b65ee Michael S. Tsirkin
    /* Check capability by default */
1794 bd4b65ee Michael S. Tsirkin
    memset(pdev->cmask + offset, 0xFF, size);
1795 6f4cbd39 Michael S. Tsirkin
    return offset;
1796 6f4cbd39 Michael S. Tsirkin
}
1797 6f4cbd39 Michael S. Tsirkin
1798 1db5a3aa Michael S. Tsirkin
/* Find and reserve space and add capability to the linked list
1799 1db5a3aa Michael S. Tsirkin
 * in pci config space */
1800 1db5a3aa Michael S. Tsirkin
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1801 1db5a3aa Michael S. Tsirkin
{
1802 1db5a3aa Michael S. Tsirkin
    uint8_t offset = pci_find_space(pdev, size);
1803 1db5a3aa Michael S. Tsirkin
    if (!offset) {
1804 1db5a3aa Michael S. Tsirkin
        return -ENOSPC;
1805 1db5a3aa Michael S. Tsirkin
    }
1806 1db5a3aa Michael S. Tsirkin
    return pci_add_capability_at_offset(pdev, cap_id, offset, size);
1807 1db5a3aa Michael S. Tsirkin
}
1808 1db5a3aa Michael S. Tsirkin
1809 6f4cbd39 Michael S. Tsirkin
/* Unlink capability from the pci config space. */
1810 6f4cbd39 Michael S. Tsirkin
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1811 6f4cbd39 Michael S. Tsirkin
{
1812 6f4cbd39 Michael S. Tsirkin
    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1813 6f4cbd39 Michael S. Tsirkin
    if (!offset)
1814 6f4cbd39 Michael S. Tsirkin
        return;
1815 6f4cbd39 Michael S. Tsirkin
    pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1816 6f4cbd39 Michael S. Tsirkin
    /* Make capability writeable again */
1817 6f4cbd39 Michael S. Tsirkin
    memset(pdev->wmask + offset, 0xff, size);
1818 bd4b65ee Michael S. Tsirkin
    /* Clear cmask as device-specific registers can't be checked */
1819 bd4b65ee Michael S. Tsirkin
    memset(pdev->cmask + offset, 0, size);
1820 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0, size);
1821 6f4cbd39 Michael S. Tsirkin
1822 6f4cbd39 Michael S. Tsirkin
    if (!pdev->config[PCI_CAPABILITY_LIST])
1823 6f4cbd39 Michael S. Tsirkin
        pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1824 6f4cbd39 Michael S. Tsirkin
}
1825 6f4cbd39 Michael S. Tsirkin
1826 6f4cbd39 Michael S. Tsirkin
/* Reserve space for capability at a known offset (to call after load). */
1827 6f4cbd39 Michael S. Tsirkin
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1828 6f4cbd39 Michael S. Tsirkin
{
1829 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0xff, size);
1830 6f4cbd39 Michael S. Tsirkin
}
1831 6f4cbd39 Michael S. Tsirkin
1832 6f4cbd39 Michael S. Tsirkin
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1833 6f4cbd39 Michael S. Tsirkin
{
1834 6f4cbd39 Michael S. Tsirkin
    return pci_find_capability_list(pdev, cap_id, NULL);
1835 6f4cbd39 Michael S. Tsirkin
}
1836 10c4c98a Gerd Hoffmann
1837 10c4c98a Gerd Hoffmann
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1838 10c4c98a Gerd Hoffmann
{
1839 10c4c98a Gerd Hoffmann
    PCIDevice *d = (PCIDevice *)dev;
1840 10c4c98a Gerd Hoffmann
    const pci_class_desc *desc;
1841 10c4c98a Gerd Hoffmann
    char ctxt[64];
1842 10c4c98a Gerd Hoffmann
    PCIIORegion *r;
1843 10c4c98a Gerd Hoffmann
    int i, class;
1844 10c4c98a Gerd Hoffmann
1845 b0ff8eb2 Isaku Yamahata
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1846 10c4c98a Gerd Hoffmann
    desc = pci_class_descriptions;
1847 10c4c98a Gerd Hoffmann
    while (desc->desc && class != desc->class)
1848 10c4c98a Gerd Hoffmann
        desc++;
1849 10c4c98a Gerd Hoffmann
    if (desc->desc) {
1850 10c4c98a Gerd Hoffmann
        snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1851 10c4c98a Gerd Hoffmann
    } else {
1852 10c4c98a Gerd Hoffmann
        snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1853 10c4c98a Gerd Hoffmann
    }
1854 10c4c98a Gerd Hoffmann
1855 10c4c98a Gerd Hoffmann
    monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1856 10c4c98a Gerd Hoffmann
                   "pci id %04x:%04x (sub %04x:%04x)\n",
1857 10c4c98a Gerd Hoffmann
                   indent, "", ctxt,
1858 e822a52a Isaku Yamahata
                   d->config[PCI_SECONDARY_BUS],
1859 e822a52a Isaku Yamahata
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1860 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_VENDOR_ID),
1861 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_DEVICE_ID),
1862 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1863 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1864 10c4c98a Gerd Hoffmann
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
1865 10c4c98a Gerd Hoffmann
        r = &d->io_regions[i];
1866 10c4c98a Gerd Hoffmann
        if (!r->size)
1867 10c4c98a Gerd Hoffmann
            continue;
1868 89e8b13c Isaku Yamahata
        monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1869 89e8b13c Isaku Yamahata
                       " [0x%"FMT_PCIBUS"]\n",
1870 89e8b13c Isaku Yamahata
                       indent, "",
1871 0392a017 Isaku Yamahata
                       i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1872 10c4c98a Gerd Hoffmann
                       r->addr, r->addr + r->size - 1);
1873 10c4c98a Gerd Hoffmann
    }
1874 10c4c98a Gerd Hoffmann
}
1875 03587182 Gerd Hoffmann
1876 4f43c1ff Alex Williamson
static char *pcibus_get_dev_path(DeviceState *dev)
1877 4f43c1ff Alex Williamson
{
1878 4f43c1ff Alex Williamson
    PCIDevice *d = (PCIDevice *)dev;
1879 4f43c1ff Alex Williamson
    char path[16];
1880 4f43c1ff Alex Williamson
1881 4f43c1ff Alex Williamson
    snprintf(path, sizeof(path), "%04x:%02x:%02x.%x",
1882 4f43c1ff Alex Williamson
             pci_find_domain(d->bus), d->config[PCI_SECONDARY_BUS],
1883 4f43c1ff Alex Williamson
             PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
1884 4f43c1ff Alex Williamson
1885 4f43c1ff Alex Williamson
    return strdup(path);
1886 4f43c1ff Alex Williamson
}
1887 4f43c1ff Alex Williamson
1888 03587182 Gerd Hoffmann
static PCIDeviceInfo bridge_info = {
1889 03587182 Gerd Hoffmann
    .qdev.name    = "pci-bridge",
1890 03587182 Gerd Hoffmann
    .qdev.size    = sizeof(PCIBridge),
1891 03587182 Gerd Hoffmann
    .init         = pci_bridge_initfn,
1892 e822a52a Isaku Yamahata
    .exit         = pci_bridge_exitfn,
1893 03587182 Gerd Hoffmann
    .config_write = pci_bridge_write_config,
1894 e327e323 Isaku Yamahata
    .is_bridge    = 1,
1895 03587182 Gerd Hoffmann
    .qdev.props   = (Property[]) {
1896 03587182 Gerd Hoffmann
        DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0),
1897 03587182 Gerd Hoffmann
        DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0),
1898 03587182 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
1899 03587182 Gerd Hoffmann
    }
1900 03587182 Gerd Hoffmann
};
1901 03587182 Gerd Hoffmann
1902 03587182 Gerd Hoffmann
static void pci_register_devices(void)
1903 03587182 Gerd Hoffmann
{
1904 03587182 Gerd Hoffmann
    pci_qdev_register(&bridge_info);
1905 03587182 Gerd Hoffmann
}
1906 03587182 Gerd Hoffmann
1907 03587182 Gerd Hoffmann
device_init(pci_register_devices)