Statistics
| Branch: | Revision:

root / hw / pci.c @ 4b8f1c88

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