Statistics
| Branch: | Revision:

root / hw / pci.c @ a1e47211

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