Statistics
| Branch: | Revision:

root / hw / pci.c @ e1a068b2

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