Statistics
| Branch: | Revision:

root / hw / pci.c @ bc927e48

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