Statistics
| Branch: | Revision:

root / hw / pci.c @ f5654039

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