Statistics
| Branch: | Revision:

root / hw / pci.c @ 6059631c

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