Statistics
| Branch: | Revision:

root / hw / pci.c @ e927d487

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