Statistics
| Branch: | Revision:

root / hw / pci.c @ c71b5b4a

History | View | Annotate | Download (46.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 376253ec aliguori
#include "monitor.h"
27 87ecb68b pbrook
#include "net.h"
28 880345c4 aliguori
#include "sysemu.h"
29 c2039bd0 Anthony Liguori
#include "loader.h"
30 69b91039 bellard
31 69b91039 bellard
//#define DEBUG_PCI
32 d8d2e079 Isaku Yamahata
#ifdef DEBUG_PCI
33 2e49d64a Isaku Yamahata
# define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
34 d8d2e079 Isaku Yamahata
#else
35 d8d2e079 Isaku Yamahata
# define PCI_DPRINTF(format, ...)       do { } while (0)
36 d8d2e079 Isaku Yamahata
#endif
37 69b91039 bellard
38 30468f78 bellard
struct PCIBus {
39 02e2da45 Paul Brook
    BusState qbus;
40 30468f78 bellard
    int devfn_min;
41 502a5395 pbrook
    pci_set_irq_fn set_irq;
42 d2b59317 pbrook
    pci_map_irq_fn map_irq;
43 ee995ffb Gerd Hoffmann
    pci_hotplug_fn hotplug;
44 30468f78 bellard
    uint32_t config_reg; /* XXX: suppress */
45 5d4e84c8 Juan Quintela
    void *irq_opaque;
46 30468f78 bellard
    PCIDevice *devices[256];
47 80b3ada7 pbrook
    PCIDevice *parent_dev;
48 2e01c8cf Blue Swirl
    target_phys_addr_t mem_base;
49 e822a52a Isaku Yamahata
50 e822a52a Isaku Yamahata
    QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
51 e822a52a Isaku Yamahata
    QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
52 e822a52a Isaku Yamahata
53 d2b59317 pbrook
    /* The bus IRQ state is the logical OR of the connected devices.
54 d2b59317 pbrook
       Keep a count of the number of devices with raised IRQs.  */
55 52fc1d83 balrog
    int nirq;
56 10c4c98a Gerd Hoffmann
    int *irq_count;
57 10c4c98a Gerd Hoffmann
};
58 10c4c98a Gerd Hoffmann
59 10c4c98a Gerd Hoffmann
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
60 10c4c98a Gerd Hoffmann
61 10c4c98a Gerd Hoffmann
static struct BusInfo pci_bus_info = {
62 10c4c98a Gerd Hoffmann
    .name       = "PCI",
63 10c4c98a Gerd Hoffmann
    .size       = sizeof(PCIBus),
64 10c4c98a Gerd Hoffmann
    .print_dev  = pcibus_dev_print,
65 ee6847d1 Gerd Hoffmann
    .props      = (Property[]) {
66 54586bd1 Gerd Hoffmann
        DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
67 8c52c8f3 Gerd Hoffmann
        DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
68 54586bd1 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST()
69 ee6847d1 Gerd Hoffmann
    }
70 30468f78 bellard
};
71 69b91039 bellard
72 1941d19c bellard
static void pci_update_mappings(PCIDevice *d);
73 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level);
74 8c52c8f3 Gerd Hoffmann
static int pci_add_option_rom(PCIDevice *pdev);
75 1941d19c bellard
76 d350d97d aliguori
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
77 d350d97d aliguori
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
78 e822a52a Isaku Yamahata
79 e822a52a Isaku Yamahata
struct PCIHostBus {
80 e822a52a Isaku Yamahata
    int domain;
81 e822a52a Isaku Yamahata
    struct PCIBus *bus;
82 e822a52a Isaku Yamahata
    QLIST_ENTRY(PCIHostBus) next;
83 e822a52a Isaku Yamahata
};
84 e822a52a Isaku Yamahata
static QLIST_HEAD(, PCIHostBus) host_buses;
85 30468f78 bellard
86 2d1e9f96 Juan Quintela
static const VMStateDescription vmstate_pcibus = {
87 2d1e9f96 Juan Quintela
    .name = "PCIBUS",
88 2d1e9f96 Juan Quintela
    .version_id = 1,
89 2d1e9f96 Juan Quintela
    .minimum_version_id = 1,
90 2d1e9f96 Juan Quintela
    .minimum_version_id_old = 1,
91 2d1e9f96 Juan Quintela
    .fields      = (VMStateField []) {
92 2d1e9f96 Juan Quintela
        VMSTATE_INT32_EQUAL(nirq, PCIBus),
93 c7bde572 Juan Quintela
        VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
94 2d1e9f96 Juan Quintela
        VMSTATE_END_OF_LIST()
95 52fc1d83 balrog
    }
96 2d1e9f96 Juan Quintela
};
97 52fc1d83 balrog
98 b3b11697 Isaku Yamahata
static int pci_bar(PCIDevice *d, int reg)
99 5330de09 Michael S. Tsirkin
{
100 b3b11697 Isaku Yamahata
    uint8_t type;
101 b3b11697 Isaku Yamahata
102 b3b11697 Isaku Yamahata
    if (reg != PCI_ROM_SLOT)
103 b3b11697 Isaku Yamahata
        return PCI_BASE_ADDRESS_0 + reg * 4;
104 b3b11697 Isaku Yamahata
105 b3b11697 Isaku Yamahata
    type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
106 b3b11697 Isaku Yamahata
    return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
107 5330de09 Michael S. Tsirkin
}
108 5330de09 Michael S. Tsirkin
109 d036bb21 Michael S. Tsirkin
static inline int pci_irq_state(PCIDevice *d, int irq_num)
110 d036bb21 Michael S. Tsirkin
{
111 d036bb21 Michael S. Tsirkin
        return (d->irq_state >> irq_num) & 0x1;
112 d036bb21 Michael S. Tsirkin
}
113 d036bb21 Michael S. Tsirkin
114 d036bb21 Michael S. Tsirkin
static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
115 d036bb21 Michael S. Tsirkin
{
116 d036bb21 Michael S. Tsirkin
        d->irq_state &= ~(0x1 << irq_num);
117 d036bb21 Michael S. Tsirkin
        d->irq_state |= level << irq_num;
118 d036bb21 Michael S. Tsirkin
}
119 d036bb21 Michael S. Tsirkin
120 d036bb21 Michael S. Tsirkin
static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
121 d036bb21 Michael S. Tsirkin
{
122 d036bb21 Michael S. Tsirkin
    PCIBus *bus;
123 d036bb21 Michael S. Tsirkin
    for (;;) {
124 d036bb21 Michael S. Tsirkin
        bus = pci_dev->bus;
125 d036bb21 Michael S. Tsirkin
        irq_num = bus->map_irq(pci_dev, irq_num);
126 d036bb21 Michael S. Tsirkin
        if (bus->set_irq)
127 d036bb21 Michael S. Tsirkin
            break;
128 d036bb21 Michael S. Tsirkin
        pci_dev = bus->parent_dev;
129 d036bb21 Michael S. Tsirkin
    }
130 d036bb21 Michael S. Tsirkin
    bus->irq_count[irq_num] += change;
131 d036bb21 Michael S. Tsirkin
    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
132 d036bb21 Michael S. Tsirkin
}
133 d036bb21 Michael S. Tsirkin
134 f9bf77dd Michael S. Tsirkin
/* Update interrupt status bit in config space on interrupt
135 f9bf77dd Michael S. Tsirkin
 * state change. */
136 f9bf77dd Michael S. Tsirkin
static void pci_update_irq_status(PCIDevice *dev)
137 f9bf77dd Michael S. Tsirkin
{
138 f9bf77dd Michael S. Tsirkin
    if (dev->irq_state) {
139 f9bf77dd Michael S. Tsirkin
        dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
140 f9bf77dd Michael S. Tsirkin
    } else {
141 f9bf77dd Michael S. Tsirkin
        dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
142 f9bf77dd Michael S. Tsirkin
    }
143 f9bf77dd Michael S. Tsirkin
}
144 f9bf77dd Michael S. Tsirkin
145 5330de09 Michael S. Tsirkin
static void pci_device_reset(PCIDevice *dev)
146 5330de09 Michael S. Tsirkin
{
147 c0b1905b Michael S. Tsirkin
    int r;
148 c0b1905b Michael S. Tsirkin
149 d036bb21 Michael S. Tsirkin
    dev->irq_state = 0;
150 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(dev);
151 c0b1905b Michael S. Tsirkin
    dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
152 c0b1905b Michael S. Tsirkin
                                  PCI_COMMAND_MASTER);
153 c0b1905b Michael S. Tsirkin
    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
154 c0b1905b Michael S. Tsirkin
    dev->config[PCI_INTERRUPT_LINE] = 0x0;
155 c0b1905b Michael S. Tsirkin
    for (r = 0; r < PCI_NUM_REGIONS; ++r) {
156 c0b1905b Michael S. Tsirkin
        if (!dev->io_regions[r].size) {
157 c0b1905b Michael S. Tsirkin
            continue;
158 c0b1905b Michael S. Tsirkin
        }
159 b3b11697 Isaku Yamahata
        pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type);
160 c0b1905b Michael S. Tsirkin
    }
161 c0b1905b Michael S. Tsirkin
    pci_update_mappings(dev);
162 5330de09 Michael S. Tsirkin
}
163 5330de09 Michael S. Tsirkin
164 6eaa6847 Gleb Natapov
static void pci_bus_reset(void *opaque)
165 6eaa6847 Gleb Natapov
{
166 a60380a5 Juan Quintela
    PCIBus *bus = opaque;
167 6eaa6847 Gleb Natapov
    int i;
168 6eaa6847 Gleb Natapov
169 6eaa6847 Gleb Natapov
    for (i = 0; i < bus->nirq; i++) {
170 6eaa6847 Gleb Natapov
        bus->irq_count[i] = 0;
171 6eaa6847 Gleb Natapov
    }
172 5330de09 Michael S. Tsirkin
    for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
173 5330de09 Michael S. Tsirkin
        if (bus->devices[i]) {
174 5330de09 Michael S. Tsirkin
            pci_device_reset(bus->devices[i]);
175 5330de09 Michael S. Tsirkin
        }
176 6eaa6847 Gleb Natapov
    }
177 6eaa6847 Gleb Natapov
}
178 6eaa6847 Gleb Natapov
179 e822a52a Isaku Yamahata
static void pci_host_bus_register(int domain, PCIBus *bus)
180 e822a52a Isaku Yamahata
{
181 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
182 e822a52a Isaku Yamahata
    host = qemu_mallocz(sizeof(*host));
183 e822a52a Isaku Yamahata
    host->domain = domain;
184 e822a52a Isaku Yamahata
    host->bus = bus;
185 e822a52a Isaku Yamahata
    QLIST_INSERT_HEAD(&host_buses, host, next);
186 e822a52a Isaku Yamahata
}
187 e822a52a Isaku Yamahata
188 c469e1dd Isaku Yamahata
PCIBus *pci_find_root_bus(int domain)
189 e822a52a Isaku Yamahata
{
190 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
191 e822a52a Isaku Yamahata
192 e822a52a Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
193 e822a52a Isaku Yamahata
        if (host->domain == domain) {
194 e822a52a Isaku Yamahata
            return host->bus;
195 e822a52a Isaku Yamahata
        }
196 e822a52a Isaku Yamahata
    }
197 e822a52a Isaku Yamahata
198 e822a52a Isaku Yamahata
    return NULL;
199 e822a52a Isaku Yamahata
}
200 e822a52a Isaku Yamahata
201 21eea4b3 Gerd Hoffmann
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
202 21eea4b3 Gerd Hoffmann
                         const char *name, int devfn_min)
203 30468f78 bellard
{
204 21eea4b3 Gerd Hoffmann
    qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
205 502a5395 pbrook
    bus->devfn_min = devfn_min;
206 e822a52a Isaku Yamahata
207 e822a52a Isaku Yamahata
    /* host bridge */
208 e822a52a Isaku Yamahata
    QLIST_INIT(&bus->child);
209 e822a52a Isaku Yamahata
    pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
210 e822a52a Isaku Yamahata
211 5084bca1 Juan Quintela
    vmstate_register(-1, &vmstate_pcibus, bus);
212 a08d4367 Jan Kiszka
    qemu_register_reset(pci_bus_reset, bus);
213 21eea4b3 Gerd Hoffmann
}
214 21eea4b3 Gerd Hoffmann
215 21eea4b3 Gerd Hoffmann
PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
216 21eea4b3 Gerd Hoffmann
{
217 21eea4b3 Gerd Hoffmann
    PCIBus *bus;
218 21eea4b3 Gerd Hoffmann
219 21eea4b3 Gerd Hoffmann
    bus = qemu_mallocz(sizeof(*bus));
220 21eea4b3 Gerd Hoffmann
    bus->qbus.qdev_allocated = 1;
221 21eea4b3 Gerd Hoffmann
    pci_bus_new_inplace(bus, parent, name, devfn_min);
222 21eea4b3 Gerd Hoffmann
    return bus;
223 21eea4b3 Gerd Hoffmann
}
224 21eea4b3 Gerd Hoffmann
225 21eea4b3 Gerd Hoffmann
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
226 21eea4b3 Gerd Hoffmann
                  void *irq_opaque, int nirq)
227 21eea4b3 Gerd Hoffmann
{
228 21eea4b3 Gerd Hoffmann
    bus->set_irq = set_irq;
229 21eea4b3 Gerd Hoffmann
    bus->map_irq = map_irq;
230 21eea4b3 Gerd Hoffmann
    bus->irq_opaque = irq_opaque;
231 21eea4b3 Gerd Hoffmann
    bus->nirq = nirq;
232 21eea4b3 Gerd Hoffmann
    bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
233 21eea4b3 Gerd Hoffmann
}
234 21eea4b3 Gerd Hoffmann
235 ee995ffb Gerd Hoffmann
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
236 ee995ffb Gerd Hoffmann
{
237 ee995ffb Gerd Hoffmann
    bus->qbus.allow_hotplug = 1;
238 ee995ffb Gerd Hoffmann
    bus->hotplug = hotplug;
239 ee995ffb Gerd Hoffmann
}
240 ee995ffb Gerd Hoffmann
241 2e01c8cf Blue Swirl
void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
242 2e01c8cf Blue Swirl
{
243 2e01c8cf Blue Swirl
    bus->mem_base = base;
244 2e01c8cf Blue Swirl
}
245 2e01c8cf Blue Swirl
246 21eea4b3 Gerd Hoffmann
PCIBus *pci_register_bus(DeviceState *parent, const char *name,
247 21eea4b3 Gerd Hoffmann
                         pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
248 21eea4b3 Gerd Hoffmann
                         void *irq_opaque, int devfn_min, int nirq)
249 21eea4b3 Gerd Hoffmann
{
250 21eea4b3 Gerd Hoffmann
    PCIBus *bus;
251 21eea4b3 Gerd Hoffmann
252 21eea4b3 Gerd Hoffmann
    bus = pci_bus_new(parent, name, devfn_min);
253 21eea4b3 Gerd Hoffmann
    pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
254 30468f78 bellard
    return bus;
255 30468f78 bellard
}
256 69b91039 bellard
257 e822a52a Isaku Yamahata
static void pci_register_secondary_bus(PCIBus *parent,
258 e822a52a Isaku Yamahata
                                       PCIBus *bus,
259 03587182 Gerd Hoffmann
                                       PCIDevice *dev,
260 03587182 Gerd Hoffmann
                                       pci_map_irq_fn map_irq,
261 03587182 Gerd Hoffmann
                                       const char *name)
262 80b3ada7 pbrook
{
263 03587182 Gerd Hoffmann
    qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name);
264 80b3ada7 pbrook
    bus->map_irq = map_irq;
265 80b3ada7 pbrook
    bus->parent_dev = dev;
266 e822a52a Isaku Yamahata
267 e822a52a Isaku Yamahata
    QLIST_INIT(&bus->child);
268 e822a52a Isaku Yamahata
    QLIST_INSERT_HEAD(&parent->child, bus, sibling);
269 e822a52a Isaku Yamahata
}
270 e822a52a Isaku Yamahata
271 e822a52a Isaku Yamahata
static void pci_unregister_secondary_bus(PCIBus *bus)
272 e822a52a Isaku Yamahata
{
273 e822a52a Isaku Yamahata
    assert(QLIST_EMPTY(&bus->child));
274 e822a52a Isaku Yamahata
    QLIST_REMOVE(bus, sibling);
275 80b3ada7 pbrook
}
276 80b3ada7 pbrook
277 502a5395 pbrook
int pci_bus_num(PCIBus *s)
278 502a5395 pbrook
{
279 e94ff650 Isaku Yamahata
    if (!s->parent_dev)
280 e94ff650 Isaku Yamahata
        return 0;       /* pci host bridge */
281 e94ff650 Isaku Yamahata
    return s->parent_dev->config[PCI_SECONDARY_BUS];
282 502a5395 pbrook
}
283 502a5395 pbrook
284 73534f2f Juan Quintela
static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
285 30ca2aab bellard
{
286 73534f2f Juan Quintela
    PCIDevice *s = container_of(pv, PCIDevice, config);
287 a9f49946 Isaku Yamahata
    uint8_t *config;
288 52fc1d83 balrog
    int i;
289 52fc1d83 balrog
290 a9f49946 Isaku Yamahata
    assert(size == pci_config_size(s));
291 a9f49946 Isaku Yamahata
    config = qemu_malloc(size);
292 a9f49946 Isaku Yamahata
293 a9f49946 Isaku Yamahata
    qemu_get_buffer(f, config, size);
294 a9f49946 Isaku Yamahata
    for (i = 0; i < size; ++i) {
295 a9f49946 Isaku Yamahata
        if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
296 a9f49946 Isaku Yamahata
            qemu_free(config);
297 bd4b65ee Michael S. Tsirkin
            return -EINVAL;
298 a9f49946 Isaku Yamahata
        }
299 a9f49946 Isaku Yamahata
    }
300 a9f49946 Isaku Yamahata
    memcpy(s->config, config, size);
301 bd4b65ee Michael S. Tsirkin
302 1941d19c bellard
    pci_update_mappings(s);
303 52fc1d83 balrog
304 a9f49946 Isaku Yamahata
    qemu_free(config);
305 30ca2aab bellard
    return 0;
306 30ca2aab bellard
}
307 30ca2aab bellard
308 73534f2f Juan Quintela
/* just put buffer */
309 84e2e3eb Juan Quintela
static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
310 73534f2f Juan Quintela
{
311 dbe73d7f Juan Quintela
    const uint8_t **v = pv;
312 a9f49946 Isaku Yamahata
    assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
313 dbe73d7f Juan Quintela
    qemu_put_buffer(f, *v, size);
314 73534f2f Juan Quintela
}
315 73534f2f Juan Quintela
316 73534f2f Juan Quintela
static VMStateInfo vmstate_info_pci_config = {
317 73534f2f Juan Quintela
    .name = "pci config",
318 73534f2f Juan Quintela
    .get  = get_pci_config_device,
319 73534f2f Juan Quintela
    .put  = put_pci_config_device,
320 73534f2f Juan Quintela
};
321 73534f2f Juan Quintela
322 d036bb21 Michael S. Tsirkin
static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
323 d036bb21 Michael S. Tsirkin
{
324 d036bb21 Michael S. Tsirkin
    PCIDevice *s = container_of(pv, PCIDevice, config);
325 d036bb21 Michael S. Tsirkin
    uint32_t irq_state[PCI_NUM_PINS];
326 d036bb21 Michael S. Tsirkin
    int i;
327 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
328 d036bb21 Michael S. Tsirkin
        irq_state[i] = qemu_get_be32(f);
329 d036bb21 Michael S. Tsirkin
        if (irq_state[i] != 0x1 && irq_state[i] != 0) {
330 d036bb21 Michael S. Tsirkin
            fprintf(stderr, "irq state %d: must be 0 or 1.\n",
331 d036bb21 Michael S. Tsirkin
                    irq_state[i]);
332 d036bb21 Michael S. Tsirkin
            return -EINVAL;
333 d036bb21 Michael S. Tsirkin
        }
334 d036bb21 Michael S. Tsirkin
    }
335 d036bb21 Michael S. Tsirkin
336 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
337 d036bb21 Michael S. Tsirkin
        pci_set_irq_state(s, i, irq_state[i]);
338 d036bb21 Michael S. Tsirkin
    }
339 d036bb21 Michael S. Tsirkin
340 d036bb21 Michael S. Tsirkin
    return 0;
341 d036bb21 Michael S. Tsirkin
}
342 d036bb21 Michael S. Tsirkin
343 d036bb21 Michael S. Tsirkin
static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
344 d036bb21 Michael S. Tsirkin
{
345 d036bb21 Michael S. Tsirkin
    int i;
346 d036bb21 Michael S. Tsirkin
    PCIDevice *s = container_of(pv, PCIDevice, config);
347 d036bb21 Michael S. Tsirkin
348 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
349 d036bb21 Michael S. Tsirkin
        qemu_put_be32(f, pci_irq_state(s, i));
350 d036bb21 Michael S. Tsirkin
    }
351 d036bb21 Michael S. Tsirkin
}
352 d036bb21 Michael S. Tsirkin
353 d036bb21 Michael S. Tsirkin
static VMStateInfo vmstate_info_pci_irq_state = {
354 d036bb21 Michael S. Tsirkin
    .name = "pci irq state",
355 d036bb21 Michael S. Tsirkin
    .get  = get_pci_irq_state,
356 d036bb21 Michael S. Tsirkin
    .put  = put_pci_irq_state,
357 d036bb21 Michael S. Tsirkin
};
358 d036bb21 Michael S. Tsirkin
359 73534f2f Juan Quintela
const VMStateDescription vmstate_pci_device = {
360 73534f2f Juan Quintela
    .name = "PCIDevice",
361 73534f2f Juan Quintela
    .version_id = 2,
362 73534f2f Juan Quintela
    .minimum_version_id = 1,
363 73534f2f Juan Quintela
    .minimum_version_id_old = 1,
364 73534f2f Juan Quintela
    .fields      = (VMStateField []) {
365 73534f2f Juan Quintela
        VMSTATE_INT32_LE(version_id, PCIDevice),
366 a9f49946 Isaku Yamahata
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
367 a9f49946 Isaku Yamahata
                                   vmstate_info_pci_config,
368 a9f49946 Isaku Yamahata
                                   PCI_CONFIG_SPACE_SIZE),
369 d036bb21 Michael S. Tsirkin
        VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
370 d036bb21 Michael S. Tsirkin
                                   vmstate_info_pci_irq_state,
371 d036bb21 Michael S. Tsirkin
                                   PCI_NUM_PINS * sizeof(int32_t)),
372 a9f49946 Isaku Yamahata
        VMSTATE_END_OF_LIST()
373 a9f49946 Isaku Yamahata
    }
374 a9f49946 Isaku Yamahata
};
375 a9f49946 Isaku Yamahata
376 a9f49946 Isaku Yamahata
const VMStateDescription vmstate_pcie_device = {
377 a9f49946 Isaku Yamahata
    .name = "PCIDevice",
378 a9f49946 Isaku Yamahata
    .version_id = 2,
379 a9f49946 Isaku Yamahata
    .minimum_version_id = 1,
380 a9f49946 Isaku Yamahata
    .minimum_version_id_old = 1,
381 a9f49946 Isaku Yamahata
    .fields      = (VMStateField []) {
382 a9f49946 Isaku Yamahata
        VMSTATE_INT32_LE(version_id, PCIDevice),
383 a9f49946 Isaku Yamahata
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
384 a9f49946 Isaku Yamahata
                                   vmstate_info_pci_config,
385 a9f49946 Isaku Yamahata
                                   PCIE_CONFIG_SPACE_SIZE),
386 d036bb21 Michael S. Tsirkin
        VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
387 d036bb21 Michael S. Tsirkin
                                   vmstate_info_pci_irq_state,
388 d036bb21 Michael S. Tsirkin
                                   PCI_NUM_PINS * sizeof(int32_t)),
389 73534f2f Juan Quintela
        VMSTATE_END_OF_LIST()
390 73534f2f Juan Quintela
    }
391 73534f2f Juan Quintela
};
392 73534f2f Juan Quintela
393 a9f49946 Isaku Yamahata
static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
394 a9f49946 Isaku Yamahata
{
395 a9f49946 Isaku Yamahata
    return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
396 a9f49946 Isaku Yamahata
}
397 a9f49946 Isaku Yamahata
398 73534f2f Juan Quintela
void pci_device_save(PCIDevice *s, QEMUFile *f)
399 73534f2f Juan Quintela
{
400 f9bf77dd Michael S. Tsirkin
    /* Clear interrupt status bit: it is implicit
401 f9bf77dd Michael S. Tsirkin
     * in irq_state which we are saving.
402 f9bf77dd Michael S. Tsirkin
     * This makes us compatible with old devices
403 f9bf77dd Michael S. Tsirkin
     * which never set or clear this bit. */
404 f9bf77dd Michael S. Tsirkin
    s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
405 a9f49946 Isaku Yamahata
    vmstate_save_state(f, pci_get_vmstate(s), s);
406 f9bf77dd Michael S. Tsirkin
    /* Restore the interrupt status bit. */
407 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(s);
408 73534f2f Juan Quintela
}
409 73534f2f Juan Quintela
410 73534f2f Juan Quintela
int pci_device_load(PCIDevice *s, QEMUFile *f)
411 73534f2f Juan Quintela
{
412 f9bf77dd Michael S. Tsirkin
    int ret;
413 f9bf77dd Michael S. Tsirkin
    ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
414 f9bf77dd Michael S. Tsirkin
    /* Restore the interrupt status bit. */
415 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(s);
416 f9bf77dd Michael S. Tsirkin
    return ret;
417 73534f2f Juan Quintela
}
418 73534f2f Juan Quintela
419 d350d97d aliguori
static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
420 d350d97d aliguori
{
421 d350d97d aliguori
    uint16_t *id;
422 d350d97d aliguori
423 d350d97d aliguori
    id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
424 d350d97d aliguori
    id[0] = cpu_to_le16(pci_default_sub_vendor_id);
425 d350d97d aliguori
    id[1] = cpu_to_le16(pci_default_sub_device_id);
426 d350d97d aliguori
    return 0;
427 d350d97d aliguori
}
428 d350d97d aliguori
429 880345c4 aliguori
/*
430 880345c4 aliguori
 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
431 880345c4 aliguori
 */
432 880345c4 aliguori
static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
433 880345c4 aliguori
{
434 880345c4 aliguori
    const char *p;
435 880345c4 aliguori
    char *e;
436 880345c4 aliguori
    unsigned long val;
437 880345c4 aliguori
    unsigned long dom = 0, bus = 0;
438 880345c4 aliguori
    unsigned slot = 0;
439 880345c4 aliguori
440 880345c4 aliguori
    p = addr;
441 880345c4 aliguori
    val = strtoul(p, &e, 16);
442 880345c4 aliguori
    if (e == p)
443 880345c4 aliguori
        return -1;
444 880345c4 aliguori
    if (*e == ':') {
445 880345c4 aliguori
        bus = val;
446 880345c4 aliguori
        p = e + 1;
447 880345c4 aliguori
        val = strtoul(p, &e, 16);
448 880345c4 aliguori
        if (e == p)
449 880345c4 aliguori
            return -1;
450 880345c4 aliguori
        if (*e == ':') {
451 880345c4 aliguori
            dom = bus;
452 880345c4 aliguori
            bus = val;
453 880345c4 aliguori
            p = e + 1;
454 880345c4 aliguori
            val = strtoul(p, &e, 16);
455 880345c4 aliguori
            if (e == p)
456 880345c4 aliguori
                return -1;
457 880345c4 aliguori
        }
458 880345c4 aliguori
    }
459 880345c4 aliguori
460 880345c4 aliguori
    if (dom > 0xffff || bus > 0xff || val > 0x1f)
461 880345c4 aliguori
        return -1;
462 880345c4 aliguori
463 880345c4 aliguori
    slot = val;
464 880345c4 aliguori
465 880345c4 aliguori
    if (*e)
466 880345c4 aliguori
        return -1;
467 880345c4 aliguori
468 880345c4 aliguori
    /* Note: QEMU doesn't implement domains other than 0 */
469 c469e1dd Isaku Yamahata
    if (!pci_find_bus(pci_find_root_bus(dom), bus))
470 880345c4 aliguori
        return -1;
471 880345c4 aliguori
472 880345c4 aliguori
    *domp = dom;
473 880345c4 aliguori
    *busp = bus;
474 880345c4 aliguori
    *slotp = slot;
475 880345c4 aliguori
    return 0;
476 880345c4 aliguori
}
477 880345c4 aliguori
478 e9283f8b Jan Kiszka
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
479 e9283f8b Jan Kiszka
                     unsigned *slotp)
480 880345c4 aliguori
{
481 e9283f8b Jan Kiszka
    /* strip legacy tag */
482 e9283f8b Jan Kiszka
    if (!strncmp(addr, "pci_addr=", 9)) {
483 e9283f8b Jan Kiszka
        addr += 9;
484 e9283f8b Jan Kiszka
    }
485 e9283f8b Jan Kiszka
    if (pci_parse_devaddr(addr, domp, busp, slotp)) {
486 e9283f8b Jan Kiszka
        monitor_printf(mon, "Invalid pci address\n");
487 880345c4 aliguori
        return -1;
488 e9283f8b Jan Kiszka
    }
489 e9283f8b Jan Kiszka
    return 0;
490 880345c4 aliguori
}
491 880345c4 aliguori
492 49bd1458 Markus Armbruster
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
493 5607c388 Markus Armbruster
{
494 5607c388 Markus Armbruster
    int dom, bus;
495 5607c388 Markus Armbruster
    unsigned slot;
496 5607c388 Markus Armbruster
497 5607c388 Markus Armbruster
    if (!devaddr) {
498 5607c388 Markus Armbruster
        *devfnp = -1;
499 c469e1dd Isaku Yamahata
        return pci_find_bus(pci_find_root_bus(0), 0);
500 5607c388 Markus Armbruster
    }
501 5607c388 Markus Armbruster
502 5607c388 Markus Armbruster
    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
503 5607c388 Markus Armbruster
        return NULL;
504 5607c388 Markus Armbruster
    }
505 5607c388 Markus Armbruster
506 5607c388 Markus Armbruster
    *devfnp = slot << 3;
507 c469e1dd Isaku Yamahata
    return pci_find_bus(pci_find_root_bus(0), bus);
508 5607c388 Markus Armbruster
}
509 5607c388 Markus Armbruster
510 bd4b65ee Michael S. Tsirkin
static void pci_init_cmask(PCIDevice *dev)
511 bd4b65ee Michael S. Tsirkin
{
512 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
513 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
514 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
515 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_REVISION_ID] = 0xff;
516 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_CLASS_PROG] = 0xff;
517 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
518 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_HEADER_TYPE] = 0xff;
519 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
520 bd4b65ee Michael S. Tsirkin
}
521 bd4b65ee Michael S. Tsirkin
522 b7ee1603 Michael S. Tsirkin
static void pci_init_wmask(PCIDevice *dev)
523 b7ee1603 Michael S. Tsirkin
{
524 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(dev);
525 a9f49946 Isaku Yamahata
526 b7ee1603 Michael S. Tsirkin
    dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
527 b7ee1603 Michael S. Tsirkin
    dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
528 67a51b48 Isaku Yamahata
    pci_set_word(dev->wmask + PCI_COMMAND,
529 d587e078 Anthony Liguori
                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
530 3e21ffc9 Isaku Yamahata
531 3e21ffc9 Isaku Yamahata
    memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
532 3e21ffc9 Isaku Yamahata
           config_size - PCI_CONFIG_HEADER_SIZE);
533 b7ee1603 Michael S. Tsirkin
}
534 b7ee1603 Michael S. Tsirkin
535 fb231628 Isaku Yamahata
static void pci_init_wmask_bridge(PCIDevice *d)
536 fb231628 Isaku Yamahata
{
537 fb231628 Isaku Yamahata
    /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
538 fb231628 Isaku Yamahata
       PCI_SEC_LETENCY_TIMER */
539 fb231628 Isaku Yamahata
    memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
540 fb231628 Isaku Yamahata
541 fb231628 Isaku Yamahata
    /* base and limit */
542 fb231628 Isaku Yamahata
    d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
543 fb231628 Isaku Yamahata
    d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
544 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_MEMORY_BASE,
545 fb231628 Isaku Yamahata
                 PCI_MEMORY_RANGE_MASK & 0xffff);
546 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
547 fb231628 Isaku Yamahata
                 PCI_MEMORY_RANGE_MASK & 0xffff);
548 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
549 fb231628 Isaku Yamahata
                 PCI_PREF_RANGE_MASK & 0xffff);
550 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
551 fb231628 Isaku Yamahata
                 PCI_PREF_RANGE_MASK & 0xffff);
552 fb231628 Isaku Yamahata
553 fb231628 Isaku Yamahata
    /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
554 fb231628 Isaku Yamahata
    memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
555 fb231628 Isaku Yamahata
556 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
557 fb231628 Isaku Yamahata
}
558 fb231628 Isaku Yamahata
559 a9f49946 Isaku Yamahata
static void pci_config_alloc(PCIDevice *pci_dev)
560 a9f49946 Isaku Yamahata
{
561 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(pci_dev);
562 a9f49946 Isaku Yamahata
563 a9f49946 Isaku Yamahata
    pci_dev->config = qemu_mallocz(config_size);
564 a9f49946 Isaku Yamahata
    pci_dev->cmask = qemu_mallocz(config_size);
565 a9f49946 Isaku Yamahata
    pci_dev->wmask = qemu_mallocz(config_size);
566 a9f49946 Isaku Yamahata
    pci_dev->used = qemu_mallocz(config_size);
567 a9f49946 Isaku Yamahata
}
568 a9f49946 Isaku Yamahata
569 a9f49946 Isaku Yamahata
static void pci_config_free(PCIDevice *pci_dev)
570 a9f49946 Isaku Yamahata
{
571 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->config);
572 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->cmask);
573 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->wmask);
574 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->used);
575 a9f49946 Isaku Yamahata
}
576 a9f49946 Isaku Yamahata
577 69b91039 bellard
/* -1 for devfn means auto assign */
578 6b1b92d3 Paul Brook
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
579 6b1b92d3 Paul Brook
                                         const char *name, int devfn,
580 6b1b92d3 Paul Brook
                                         PCIConfigReadFunc *config_read,
581 fb231628 Isaku Yamahata
                                         PCIConfigWriteFunc *config_write,
582 fb231628 Isaku Yamahata
                                         uint8_t header_type)
583 69b91039 bellard
{
584 69b91039 bellard
    if (devfn < 0) {
585 b47b0706 Isaku Yamahata
        for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
586 b47b0706 Isaku Yamahata
            devfn += 8) {
587 30468f78 bellard
            if (!bus->devices[devfn])
588 69b91039 bellard
                goto found;
589 69b91039 bellard
        }
590 09e3acc6 Gerd Hoffmann
        qemu_error("PCI: no devfn available for %s, all in use\n", name);
591 09e3acc6 Gerd Hoffmann
        return NULL;
592 69b91039 bellard
    found: ;
593 07b7d053 Markus Armbruster
    } else if (bus->devices[devfn]) {
594 09e3acc6 Gerd Hoffmann
        qemu_error("PCI: devfn %d not available for %s, in use by %s\n", devfn,
595 c364c974 Blue Swirl
                 name, bus->devices[devfn]->name);
596 09e3acc6 Gerd Hoffmann
        return NULL;
597 69b91039 bellard
    }
598 30468f78 bellard
    pci_dev->bus = bus;
599 69b91039 bellard
    pci_dev->devfn = devfn;
600 69b91039 bellard
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
601 d036bb21 Michael S. Tsirkin
    pci_dev->irq_state = 0;
602 a9f49946 Isaku Yamahata
    pci_config_alloc(pci_dev);
603 fb231628 Isaku Yamahata
604 fb231628 Isaku Yamahata
    header_type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
605 fb231628 Isaku Yamahata
    if (header_type == PCI_HEADER_TYPE_NORMAL) {
606 fb231628 Isaku Yamahata
        pci_set_default_subsystem_id(pci_dev);
607 fb231628 Isaku Yamahata
    }
608 bd4b65ee Michael S. Tsirkin
    pci_init_cmask(pci_dev);
609 b7ee1603 Michael S. Tsirkin
    pci_init_wmask(pci_dev);
610 fb231628 Isaku Yamahata
    if (header_type == PCI_HEADER_TYPE_BRIDGE) {
611 fb231628 Isaku Yamahata
        pci_init_wmask_bridge(pci_dev);
612 fb231628 Isaku Yamahata
    }
613 0ac32c83 bellard
614 0ac32c83 bellard
    if (!config_read)
615 0ac32c83 bellard
        config_read = pci_default_read_config;
616 0ac32c83 bellard
    if (!config_write)
617 0ac32c83 bellard
        config_write = pci_default_write_config;
618 69b91039 bellard
    pci_dev->config_read = config_read;
619 69b91039 bellard
    pci_dev->config_write = config_write;
620 30468f78 bellard
    bus->devices[devfn] = pci_dev;
621 e369cad7 Isaku Yamahata
    pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
622 f16c4abf Juan Quintela
    pci_dev->version_id = 2; /* Current pci device vmstate version */
623 69b91039 bellard
    return pci_dev;
624 69b91039 bellard
}
625 69b91039 bellard
626 6b1b92d3 Paul Brook
PCIDevice *pci_register_device(PCIBus *bus, const char *name,
627 6b1b92d3 Paul Brook
                               int instance_size, int devfn,
628 6b1b92d3 Paul Brook
                               PCIConfigReadFunc *config_read,
629 6b1b92d3 Paul Brook
                               PCIConfigWriteFunc *config_write)
630 6b1b92d3 Paul Brook
{
631 6b1b92d3 Paul Brook
    PCIDevice *pci_dev;
632 6b1b92d3 Paul Brook
633 6b1b92d3 Paul Brook
    pci_dev = qemu_mallocz(instance_size);
634 6b1b92d3 Paul Brook
    pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
635 fb231628 Isaku Yamahata
                                     config_read, config_write,
636 fb231628 Isaku Yamahata
                                     PCI_HEADER_TYPE_NORMAL);
637 09e3acc6 Gerd Hoffmann
    if (pci_dev == NULL) {
638 09e3acc6 Gerd Hoffmann
        hw_error("PCI: can't register device\n");
639 09e3acc6 Gerd Hoffmann
    }
640 6b1b92d3 Paul Brook
    return pci_dev;
641 6b1b92d3 Paul Brook
}
642 2e01c8cf Blue Swirl
643 2e01c8cf Blue Swirl
static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
644 2e01c8cf Blue Swirl
                                          target_phys_addr_t addr)
645 5851e08c aliguori
{
646 2e01c8cf Blue Swirl
    return addr + bus->mem_base;
647 5851e08c aliguori
}
648 5851e08c aliguori
649 5851e08c aliguori
static void pci_unregister_io_regions(PCIDevice *pci_dev)
650 5851e08c aliguori
{
651 5851e08c aliguori
    PCIIORegion *r;
652 5851e08c aliguori
    int i;
653 5851e08c aliguori
654 5851e08c aliguori
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
655 5851e08c aliguori
        r = &pci_dev->io_regions[i];
656 182f9c8a Isaku Yamahata
        if (!r->size || r->addr == PCI_BAR_UNMAPPED)
657 5851e08c aliguori
            continue;
658 0392a017 Isaku Yamahata
        if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
659 a0c7a97e Isaku Yamahata
            isa_unassign_ioport(r->addr, r->filtered_size);
660 5851e08c aliguori
        } else {
661 2e01c8cf Blue Swirl
            cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus,
662 2e01c8cf Blue Swirl
                                                         r->addr),
663 2e01c8cf Blue Swirl
                                         r->filtered_size,
664 2e01c8cf Blue Swirl
                                         IO_MEM_UNASSIGNED);
665 5851e08c aliguori
        }
666 5851e08c aliguori
    }
667 5851e08c aliguori
}
668 5851e08c aliguori
669 a36a344d Gerd Hoffmann
static int pci_unregister_device(DeviceState *dev)
670 5851e08c aliguori
{
671 a36a344d Gerd Hoffmann
    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
672 e3936fa5 Gerd Hoffmann
    PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
673 5851e08c aliguori
    int ret = 0;
674 5851e08c aliguori
675 e3936fa5 Gerd Hoffmann
    if (info->exit)
676 e3936fa5 Gerd Hoffmann
        ret = info->exit(pci_dev);
677 5851e08c aliguori
    if (ret)
678 5851e08c aliguori
        return ret;
679 5851e08c aliguori
680 5851e08c aliguori
    pci_unregister_io_regions(pci_dev);
681 5851e08c aliguori
682 5851e08c aliguori
    qemu_free_irqs(pci_dev->irq);
683 5851e08c aliguori
    pci_dev->bus->devices[pci_dev->devfn] = NULL;
684 a9f49946 Isaku Yamahata
    pci_config_free(pci_dev);
685 5851e08c aliguori
    return 0;
686 5851e08c aliguori
}
687 5851e08c aliguori
688 28c2c264 Avi Kivity
void pci_register_bar(PCIDevice *pci_dev, int region_num,
689 6e355d90 Isaku Yamahata
                            pcibus_t size, int type,
690 69b91039 bellard
                            PCIMapIORegionFunc *map_func)
691 69b91039 bellard
{
692 69b91039 bellard
    PCIIORegion *r;
693 d7ce493a pbrook
    uint32_t addr;
694 6e355d90 Isaku Yamahata
    pcibus_t wmask;
695 69b91039 bellard
696 8a8696a3 bellard
    if ((unsigned int)region_num >= PCI_NUM_REGIONS)
697 69b91039 bellard
        return;
698 a4c20c6a aliguori
699 a4c20c6a aliguori
    if (size & (size-1)) {
700 a4c20c6a aliguori
        fprintf(stderr, "ERROR: PCI region size must be pow2 "
701 89e8b13c Isaku Yamahata
                    "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
702 a4c20c6a aliguori
        exit(1);
703 a4c20c6a aliguori
    }
704 a4c20c6a aliguori
705 69b91039 bellard
    r = &pci_dev->io_regions[region_num];
706 182f9c8a Isaku Yamahata
    r->addr = PCI_BAR_UNMAPPED;
707 69b91039 bellard
    r->size = size;
708 a0c7a97e Isaku Yamahata
    r->filtered_size = size;
709 69b91039 bellard
    r->type = type;
710 69b91039 bellard
    r->map_func = map_func;
711 b7ee1603 Michael S. Tsirkin
712 b7ee1603 Michael S. Tsirkin
    wmask = ~(size - 1);
713 b3b11697 Isaku Yamahata
    addr = pci_bar(pci_dev, region_num);
714 d7ce493a pbrook
    if (region_num == PCI_ROM_SLOT) {
715 b7ee1603 Michael S. Tsirkin
        /* ROM enable bit is writeable */
716 5330de09 Michael S. Tsirkin
        wmask |= PCI_ROM_ADDRESS_ENABLE;
717 d7ce493a pbrook
    }
718 b0ff8eb2 Isaku Yamahata
    pci_set_long(pci_dev->config + addr, type);
719 14421258 Isaku Yamahata
    if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
720 14421258 Isaku Yamahata
        r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
721 14421258 Isaku Yamahata
        pci_set_quad(pci_dev->wmask + addr, wmask);
722 14421258 Isaku Yamahata
        pci_set_quad(pci_dev->cmask + addr, ~0ULL);
723 14421258 Isaku Yamahata
    } else {
724 14421258 Isaku Yamahata
        pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
725 14421258 Isaku Yamahata
        pci_set_long(pci_dev->cmask + addr, 0xffffffff);
726 14421258 Isaku Yamahata
    }
727 69b91039 bellard
}
728 69b91039 bellard
729 a0c7a97e Isaku Yamahata
static uint32_t pci_config_get_io_base(PCIDevice *d,
730 a0c7a97e Isaku Yamahata
                                       uint32_t base, uint32_t base_upper16)
731 a0c7a97e Isaku Yamahata
{
732 a0c7a97e Isaku Yamahata
    uint32_t val;
733 a0c7a97e Isaku Yamahata
734 a0c7a97e Isaku Yamahata
    val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
735 a0c7a97e Isaku Yamahata
    if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
736 10c9c329 Isaku Yamahata
        val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
737 a0c7a97e Isaku Yamahata
    }
738 a0c7a97e Isaku Yamahata
    return val;
739 a0c7a97e Isaku Yamahata
}
740 a0c7a97e Isaku Yamahata
741 d46636b8 Isaku Yamahata
static pcibus_t pci_config_get_memory_base(PCIDevice *d, uint32_t base)
742 a0c7a97e Isaku Yamahata
{
743 d46636b8 Isaku Yamahata
    return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
744 a0c7a97e Isaku Yamahata
        << 16;
745 a0c7a97e Isaku Yamahata
}
746 a0c7a97e Isaku Yamahata
747 d46636b8 Isaku Yamahata
static pcibus_t pci_config_get_pref_base(PCIDevice *d,
748 a0c7a97e Isaku Yamahata
                                         uint32_t base, uint32_t upper)
749 a0c7a97e Isaku Yamahata
{
750 d46636b8 Isaku Yamahata
    pcibus_t tmp;
751 d46636b8 Isaku Yamahata
    pcibus_t val;
752 d46636b8 Isaku Yamahata
753 d46636b8 Isaku Yamahata
    tmp = (pcibus_t)pci_get_word(d->config + base);
754 d46636b8 Isaku Yamahata
    val = (tmp & PCI_PREF_RANGE_MASK) << 16;
755 d46636b8 Isaku Yamahata
    if (tmp & PCI_PREF_RANGE_TYPE_64) {
756 d46636b8 Isaku Yamahata
        val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
757 d46636b8 Isaku Yamahata
    }
758 a0c7a97e Isaku Yamahata
    return val;
759 a0c7a97e Isaku Yamahata
}
760 a0c7a97e Isaku Yamahata
761 a0c7a97e Isaku Yamahata
static pcibus_t pci_bridge_get_base(PCIDevice *bridge, uint8_t type)
762 a0c7a97e Isaku Yamahata
{
763 a0c7a97e Isaku Yamahata
    pcibus_t base;
764 a0c7a97e Isaku Yamahata
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
765 a0c7a97e Isaku Yamahata
        base = pci_config_get_io_base(bridge,
766 a0c7a97e Isaku Yamahata
                                      PCI_IO_BASE, PCI_IO_BASE_UPPER16);
767 a0c7a97e Isaku Yamahata
    } else {
768 a0c7a97e Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
769 a0c7a97e Isaku Yamahata
            base = pci_config_get_pref_base(
770 a0c7a97e Isaku Yamahata
                bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
771 a0c7a97e Isaku Yamahata
        } else {
772 a0c7a97e Isaku Yamahata
            base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
773 a0c7a97e Isaku Yamahata
        }
774 a0c7a97e Isaku Yamahata
    }
775 a0c7a97e Isaku Yamahata
776 a0c7a97e Isaku Yamahata
    return base;
777 a0c7a97e Isaku Yamahata
}
778 a0c7a97e Isaku Yamahata
779 a0c7a97e Isaku Yamahata
static pcibus_t pci_bridge_get_limit(PCIDevice *bridge, uint8_t type)
780 a0c7a97e Isaku Yamahata
{
781 a0c7a97e Isaku Yamahata
    pcibus_t limit;
782 a0c7a97e Isaku Yamahata
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
783 a0c7a97e Isaku Yamahata
        limit = pci_config_get_io_base(bridge,
784 a0c7a97e Isaku Yamahata
                                      PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
785 a0c7a97e Isaku Yamahata
        limit |= 0xfff;         /* PCI bridge spec 3.2.5.6. */
786 a0c7a97e Isaku Yamahata
    } else {
787 a0c7a97e Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
788 a0c7a97e Isaku Yamahata
            limit = pci_config_get_pref_base(
789 a0c7a97e Isaku Yamahata
                bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
790 a0c7a97e Isaku Yamahata
        } else {
791 a0c7a97e Isaku Yamahata
            limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
792 a0c7a97e Isaku Yamahata
        }
793 a0c7a97e Isaku Yamahata
        limit |= 0xfffff;       /* PCI bridge spec 3.2.5.{1, 8}. */
794 a0c7a97e Isaku Yamahata
    }
795 a0c7a97e Isaku Yamahata
    return limit;
796 a0c7a97e Isaku Yamahata
}
797 a0c7a97e Isaku Yamahata
798 a0c7a97e Isaku Yamahata
static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size,
799 a0c7a97e Isaku Yamahata
                              uint8_t type)
800 a0c7a97e Isaku Yamahata
{
801 a0c7a97e Isaku Yamahata
    pcibus_t base = *addr;
802 a0c7a97e Isaku Yamahata
    pcibus_t limit = *addr + *size - 1;
803 a0c7a97e Isaku Yamahata
    PCIDevice *br;
804 a0c7a97e Isaku Yamahata
805 a0c7a97e Isaku Yamahata
    for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
806 a0c7a97e Isaku Yamahata
        uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
807 a0c7a97e Isaku Yamahata
808 a0c7a97e Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_SPACE_IO) {
809 a0c7a97e Isaku Yamahata
            if (!(cmd & PCI_COMMAND_IO)) {
810 a0c7a97e Isaku Yamahata
                goto no_map;
811 a0c7a97e Isaku Yamahata
            }
812 a0c7a97e Isaku Yamahata
        } else {
813 a0c7a97e Isaku Yamahata
            if (!(cmd & PCI_COMMAND_MEMORY)) {
814 a0c7a97e Isaku Yamahata
                goto no_map;
815 a0c7a97e Isaku Yamahata
            }
816 a0c7a97e Isaku Yamahata
        }
817 a0c7a97e Isaku Yamahata
818 a0c7a97e Isaku Yamahata
        base = MAX(base, pci_bridge_get_base(br, type));
819 a0c7a97e Isaku Yamahata
        limit = MIN(limit, pci_bridge_get_limit(br, type));
820 a0c7a97e Isaku Yamahata
    }
821 a0c7a97e Isaku Yamahata
822 a0c7a97e Isaku Yamahata
    if (base > limit) {
823 88a95564 Michael S. Tsirkin
        goto no_map;
824 a0c7a97e Isaku Yamahata
    }
825 88a95564 Michael S. Tsirkin
    *addr = base;
826 88a95564 Michael S. Tsirkin
    *size = limit - base + 1;
827 88a95564 Michael S. Tsirkin
    return;
828 88a95564 Michael S. Tsirkin
no_map:
829 88a95564 Michael S. Tsirkin
    *addr = PCI_BAR_UNMAPPED;
830 88a95564 Michael S. Tsirkin
    *size = 0;
831 a0c7a97e Isaku Yamahata
}
832 a0c7a97e Isaku Yamahata
833 876a350d Michael S. Tsirkin
static pcibus_t pci_bar_address(PCIDevice *d,
834 876a350d Michael S. Tsirkin
                                int reg, uint8_t type, pcibus_t size)
835 876a350d Michael S. Tsirkin
{
836 876a350d Michael S. Tsirkin
    pcibus_t new_addr, last_addr;
837 876a350d Michael S. Tsirkin
    int bar = pci_bar(d, reg);
838 876a350d Michael S. Tsirkin
    uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
839 876a350d Michael S. Tsirkin
840 876a350d Michael S. Tsirkin
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
841 876a350d Michael S. Tsirkin
        if (!(cmd & PCI_COMMAND_IO)) {
842 876a350d Michael S. Tsirkin
            return PCI_BAR_UNMAPPED;
843 876a350d Michael S. Tsirkin
        }
844 876a350d Michael S. Tsirkin
        new_addr = pci_get_long(d->config + bar) & ~(size - 1);
845 876a350d Michael S. Tsirkin
        last_addr = new_addr + size - 1;
846 876a350d Michael S. Tsirkin
        /* NOTE: we have only 64K ioports on PC */
847 876a350d Michael S. Tsirkin
        if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) {
848 876a350d Michael S. Tsirkin
            return PCI_BAR_UNMAPPED;
849 876a350d Michael S. Tsirkin
        }
850 876a350d Michael S. Tsirkin
        return new_addr;
851 876a350d Michael S. Tsirkin
    }
852 876a350d Michael S. Tsirkin
853 876a350d Michael S. Tsirkin
    if (!(cmd & PCI_COMMAND_MEMORY)) {
854 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
855 876a350d Michael S. Tsirkin
    }
856 876a350d Michael S. Tsirkin
    if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
857 876a350d Michael S. Tsirkin
        new_addr = pci_get_quad(d->config + bar);
858 876a350d Michael S. Tsirkin
    } else {
859 876a350d Michael S. Tsirkin
        new_addr = pci_get_long(d->config + bar);
860 876a350d Michael S. Tsirkin
    }
861 876a350d Michael S. Tsirkin
    /* the ROM slot has a specific enable bit */
862 876a350d Michael S. Tsirkin
    if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
863 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
864 876a350d Michael S. Tsirkin
    }
865 876a350d Michael S. Tsirkin
    new_addr &= ~(size - 1);
866 876a350d Michael S. Tsirkin
    last_addr = new_addr + size - 1;
867 876a350d Michael S. Tsirkin
    /* NOTE: we do not support wrapping */
868 876a350d Michael S. Tsirkin
    /* XXX: as we cannot support really dynamic
869 876a350d Michael S. Tsirkin
       mappings, we handle specific values as invalid
870 876a350d Michael S. Tsirkin
       mappings. */
871 876a350d Michael S. Tsirkin
    if (last_addr <= new_addr || new_addr == 0 ||
872 876a350d Michael S. Tsirkin
        last_addr == PCI_BAR_UNMAPPED) {
873 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
874 876a350d Michael S. Tsirkin
    }
875 876a350d Michael S. Tsirkin
876 876a350d Michael S. Tsirkin
    /* Now pcibus_t is 64bit.
877 876a350d Michael S. Tsirkin
     * Check if 32 bit BAR wraps around explicitly.
878 876a350d Michael S. Tsirkin
     * Without this, PC ide doesn't work well.
879 876a350d Michael S. Tsirkin
     * TODO: remove this work around.
880 876a350d Michael S. Tsirkin
     */
881 876a350d Michael S. Tsirkin
    if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
882 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
883 876a350d Michael S. Tsirkin
    }
884 876a350d Michael S. Tsirkin
885 876a350d Michael S. Tsirkin
    /*
886 876a350d Michael S. Tsirkin
     * OS is allowed to set BAR beyond its addressable
887 876a350d Michael S. Tsirkin
     * bits. For example, 32 bit OS can set 64bit bar
888 876a350d Michael S. Tsirkin
     * to >4G. Check it. TODO: we might need to support
889 876a350d Michael S. Tsirkin
     * it in the future for e.g. PAE.
890 876a350d Michael S. Tsirkin
     */
891 876a350d Michael S. Tsirkin
    if (last_addr >= TARGET_PHYS_ADDR_MAX) {
892 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
893 876a350d Michael S. Tsirkin
    }
894 876a350d Michael S. Tsirkin
895 876a350d Michael S. Tsirkin
    return new_addr;
896 876a350d Michael S. Tsirkin
}
897 876a350d Michael S. Tsirkin
898 0ac32c83 bellard
static void pci_update_mappings(PCIDevice *d)
899 0ac32c83 bellard
{
900 0ac32c83 bellard
    PCIIORegion *r;
901 876a350d Michael S. Tsirkin
    int i;
902 c71b5b4a Blue Swirl
    pcibus_t new_addr, filtered_size;
903 3b46e624 ths
904 8a8696a3 bellard
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
905 0ac32c83 bellard
        r = &d->io_regions[i];
906 a9688570 Isaku Yamahata
907 a9688570 Isaku Yamahata
        /* this region isn't registered */
908 ec503442 Isaku Yamahata
        if (!r->size)
909 a9688570 Isaku Yamahata
            continue;
910 a9688570 Isaku Yamahata
911 876a350d Michael S. Tsirkin
        new_addr = pci_bar_address(d, i, r->type, r->size);
912 a9688570 Isaku Yamahata
913 a0c7a97e Isaku Yamahata
        /* bridge filtering */
914 a0c7a97e Isaku Yamahata
        filtered_size = r->size;
915 a0c7a97e Isaku Yamahata
        if (new_addr != PCI_BAR_UNMAPPED) {
916 a0c7a97e Isaku Yamahata
            pci_bridge_filter(d, &new_addr, &filtered_size, r->type);
917 a0c7a97e Isaku Yamahata
        }
918 a0c7a97e Isaku Yamahata
919 a9688570 Isaku Yamahata
        /* This bar isn't changed */
920 a0c7a97e Isaku Yamahata
        if (new_addr == r->addr && filtered_size == r->filtered_size)
921 a9688570 Isaku Yamahata
            continue;
922 a9688570 Isaku Yamahata
923 a9688570 Isaku Yamahata
        /* now do the real mapping */
924 a9688570 Isaku Yamahata
        if (r->addr != PCI_BAR_UNMAPPED) {
925 a9688570 Isaku Yamahata
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
926 a9688570 Isaku Yamahata
                int class;
927 a9688570 Isaku Yamahata
                /* NOTE: specific hack for IDE in PC case:
928 a9688570 Isaku Yamahata
                   only one byte must be mapped. */
929 a9688570 Isaku Yamahata
                class = pci_get_word(d->config + PCI_CLASS_DEVICE);
930 a9688570 Isaku Yamahata
                if (class == 0x0101 && r->size == 4) {
931 a9688570 Isaku Yamahata
                    isa_unassign_ioport(r->addr + 2, 1);
932 a9688570 Isaku Yamahata
                } else {
933 a0c7a97e Isaku Yamahata
                    isa_unassign_ioport(r->addr, r->filtered_size);
934 0ac32c83 bellard
                }
935 a9688570 Isaku Yamahata
            } else {
936 c71b5b4a Blue Swirl
                cpu_register_physical_memory(pci_to_cpu_addr(d->bus, r->addr),
937 a0c7a97e Isaku Yamahata
                                             r->filtered_size,
938 a9688570 Isaku Yamahata
                                             IO_MEM_UNASSIGNED);
939 a0c7a97e Isaku Yamahata
                qemu_unregister_coalesced_mmio(r->addr, r->filtered_size);
940 0ac32c83 bellard
            }
941 0ac32c83 bellard
        }
942 a9688570 Isaku Yamahata
        r->addr = new_addr;
943 a0c7a97e Isaku Yamahata
        r->filtered_size = filtered_size;
944 a9688570 Isaku Yamahata
        if (r->addr != PCI_BAR_UNMAPPED) {
945 a0c7a97e Isaku Yamahata
            /*
946 a0c7a97e Isaku Yamahata
             * TODO: currently almost all the map funcions assumes
947 a0c7a97e Isaku Yamahata
             * filtered_size == size and addr & ~(size - 1) == addr.
948 a0c7a97e Isaku Yamahata
             * However with bridge filtering, they aren't always true.
949 a0c7a97e Isaku Yamahata
             * Teach them such cases, such that filtered_size < size and
950 a0c7a97e Isaku Yamahata
             * addr & (size - 1) != 0.
951 a0c7a97e Isaku Yamahata
             */
952 c71b5b4a Blue Swirl
            r->map_func(d, i, r->addr, r->filtered_size, r->type);
953 a9688570 Isaku Yamahata
        }
954 0ac32c83 bellard
    }
955 0ac32c83 bellard
}
956 0ac32c83 bellard
957 5fafdf24 ths
uint32_t pci_default_read_config(PCIDevice *d,
958 0ac32c83 bellard
                                 uint32_t address, int len)
959 69b91039 bellard
{
960 5029fe12 Isaku Yamahata
    uint32_t val = 0;
961 5029fe12 Isaku Yamahata
    assert(len == 1 || len == 2 || len == 4);
962 a9f49946 Isaku Yamahata
    len = MIN(len, pci_config_size(d) - address);
963 5029fe12 Isaku Yamahata
    memcpy(&val, d->config + address, len);
964 5029fe12 Isaku Yamahata
    return le32_to_cpu(val);
965 0ac32c83 bellard
}
966 0ac32c83 bellard
967 b7ee1603 Michael S. Tsirkin
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
968 0ac32c83 bellard
{
969 d587e078 Anthony Liguori
    int i;
970 a9f49946 Isaku Yamahata
    uint32_t config_size = pci_config_size(d);
971 0ac32c83 bellard
972 91011d4f Stefan Weil
    for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) {
973 91011d4f Stefan Weil
        uint8_t wmask = d->wmask[addr + i];
974 91011d4f Stefan Weil
        d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
975 0ac32c83 bellard
    }
976 260c0cd3 Isaku Yamahata
    if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
977 edb00035 Isaku Yamahata
        ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
978 edb00035 Isaku Yamahata
        ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
979 260c0cd3 Isaku Yamahata
        range_covers_byte(addr, l, PCI_COMMAND))
980 0ac32c83 bellard
        pci_update_mappings(d);
981 69b91039 bellard
}
982 69b91039 bellard
983 502a5395 pbrook
/***********************************************************/
984 502a5395 pbrook
/* generic PCI irq support */
985 30468f78 bellard
986 502a5395 pbrook
/* 0 <= irq_num <= 3. level must be 0 or 1 */
987 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level)
988 69b91039 bellard
{
989 a60380a5 Juan Quintela
    PCIDevice *pci_dev = opaque;
990 80b3ada7 pbrook
    int change;
991 3b46e624 ths
992 d036bb21 Michael S. Tsirkin
    change = level - pci_irq_state(pci_dev, irq_num);
993 80b3ada7 pbrook
    if (!change)
994 80b3ada7 pbrook
        return;
995 d2b59317 pbrook
996 d036bb21 Michael S. Tsirkin
    pci_set_irq_state(pci_dev, irq_num, level);
997 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(pci_dev);
998 d036bb21 Michael S. Tsirkin
    pci_change_irq_level(pci_dev, irq_num, change);
999 69b91039 bellard
}
1000 69b91039 bellard
1001 502a5395 pbrook
/***********************************************************/
1002 502a5395 pbrook
/* monitor info on PCI */
1003 0ac32c83 bellard
1004 6650ee6d pbrook
typedef struct {
1005 6650ee6d pbrook
    uint16_t class;
1006 6650ee6d pbrook
    const char *desc;
1007 6650ee6d pbrook
} pci_class_desc;
1008 6650ee6d pbrook
1009 09bc878a blueswir1
static const pci_class_desc pci_class_descriptions[] =
1010 6650ee6d pbrook
{
1011 4ca9c76f pbrook
    { 0x0100, "SCSI controller"},
1012 6650ee6d pbrook
    { 0x0101, "IDE controller"},
1013 dcb5b19a ths
    { 0x0102, "Floppy controller"},
1014 dcb5b19a ths
    { 0x0103, "IPI controller"},
1015 dcb5b19a ths
    { 0x0104, "RAID controller"},
1016 dcb5b19a ths
    { 0x0106, "SATA controller"},
1017 dcb5b19a ths
    { 0x0107, "SAS controller"},
1018 dcb5b19a ths
    { 0x0180, "Storage controller"},
1019 6650ee6d pbrook
    { 0x0200, "Ethernet controller"},
1020 dcb5b19a ths
    { 0x0201, "Token Ring controller"},
1021 dcb5b19a ths
    { 0x0202, "FDDI controller"},
1022 dcb5b19a ths
    { 0x0203, "ATM controller"},
1023 dcb5b19a ths
    { 0x0280, "Network controller"},
1024 6650ee6d pbrook
    { 0x0300, "VGA controller"},
1025 dcb5b19a ths
    { 0x0301, "XGA controller"},
1026 dcb5b19a ths
    { 0x0302, "3D controller"},
1027 dcb5b19a ths
    { 0x0380, "Display controller"},
1028 dcb5b19a ths
    { 0x0400, "Video controller"},
1029 dcb5b19a ths
    { 0x0401, "Audio controller"},
1030 dcb5b19a ths
    { 0x0402, "Phone"},
1031 dcb5b19a ths
    { 0x0480, "Multimedia controller"},
1032 dcb5b19a ths
    { 0x0500, "RAM controller"},
1033 dcb5b19a ths
    { 0x0501, "Flash controller"},
1034 dcb5b19a ths
    { 0x0580, "Memory controller"},
1035 6650ee6d pbrook
    { 0x0600, "Host bridge"},
1036 6650ee6d pbrook
    { 0x0601, "ISA bridge"},
1037 dcb5b19a ths
    { 0x0602, "EISA bridge"},
1038 dcb5b19a ths
    { 0x0603, "MC bridge"},
1039 6650ee6d pbrook
    { 0x0604, "PCI bridge"},
1040 dcb5b19a ths
    { 0x0605, "PCMCIA bridge"},
1041 dcb5b19a ths
    { 0x0606, "NUBUS bridge"},
1042 dcb5b19a ths
    { 0x0607, "CARDBUS bridge"},
1043 dcb5b19a ths
    { 0x0608, "RACEWAY bridge"},
1044 dcb5b19a ths
    { 0x0680, "Bridge"},
1045 6650ee6d pbrook
    { 0x0c03, "USB controller"},
1046 6650ee6d pbrook
    { 0, NULL}
1047 6650ee6d pbrook
};
1048 6650ee6d pbrook
1049 e822a52a Isaku Yamahata
static void pci_info_device(PCIBus *bus, PCIDevice *d)
1050 30468f78 bellard
{
1051 376253ec aliguori
    Monitor *mon = cur_mon;
1052 502a5395 pbrook
    int i, class;
1053 502a5395 pbrook
    PCIIORegion *r;
1054 09bc878a blueswir1
    const pci_class_desc *desc;
1055 30468f78 bellard
1056 376253ec aliguori
    monitor_printf(mon, "  Bus %2d, device %3d, function %d:\n",
1057 e94ff650 Isaku Yamahata
                   pci_bus_num(d->bus),
1058 e94ff650 Isaku Yamahata
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
1059 b0ff8eb2 Isaku Yamahata
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1060 376253ec aliguori
    monitor_printf(mon, "    ");
1061 6650ee6d pbrook
    desc = pci_class_descriptions;
1062 6650ee6d pbrook
    while (desc->desc && class != desc->class)
1063 6650ee6d pbrook
        desc++;
1064 6650ee6d pbrook
    if (desc->desc) {
1065 376253ec aliguori
        monitor_printf(mon, "%s", desc->desc);
1066 6650ee6d pbrook
    } else {
1067 376253ec aliguori
        monitor_printf(mon, "Class %04x", class);
1068 72cc6cfe bellard
    }
1069 376253ec aliguori
    monitor_printf(mon, ": PCI device %04x:%04x\n",
1070 b0ff8eb2 Isaku Yamahata
           pci_get_word(d->config + PCI_VENDOR_ID),
1071 b0ff8eb2 Isaku Yamahata
           pci_get_word(d->config + PCI_DEVICE_ID));
1072 30468f78 bellard
1073 502a5395 pbrook
    if (d->config[PCI_INTERRUPT_PIN] != 0) {
1074 376253ec aliguori
        monitor_printf(mon, "      IRQ %d.\n",
1075 376253ec aliguori
                       d->config[PCI_INTERRUPT_LINE]);
1076 30468f78 bellard
    }
1077 80b3ada7 pbrook
    if (class == 0x0604) {
1078 b4dccd8d Isaku Yamahata
        uint64_t base;
1079 b4dccd8d Isaku Yamahata
        uint64_t limit;
1080 b4dccd8d Isaku Yamahata
1081 376253ec aliguori
        monitor_printf(mon, "      BUS %d.\n", d->config[0x19]);
1082 b4dccd8d Isaku Yamahata
        monitor_printf(mon, "      secondary bus %d.\n",
1083 b4dccd8d Isaku Yamahata
                       d->config[PCI_SECONDARY_BUS]);
1084 b4dccd8d Isaku Yamahata
        monitor_printf(mon, "      subordinate bus %d.\n",
1085 b4dccd8d Isaku Yamahata
                       d->config[PCI_SUBORDINATE_BUS]);
1086 b4dccd8d Isaku Yamahata
1087 b4dccd8d Isaku Yamahata
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_IO);
1088 b4dccd8d Isaku Yamahata
        limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_IO);
1089 b4dccd8d Isaku Yamahata
        monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
1090 b4dccd8d Isaku Yamahata
                       base, limit);
1091 b4dccd8d Isaku Yamahata
1092 b4dccd8d Isaku Yamahata
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY);
1093 f88d7509 Isaku Yamahata
        limit= pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY);
1094 b4dccd8d Isaku Yamahata
        monitor_printf(mon,
1095 b4dccd8d Isaku Yamahata
                       "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
1096 b4dccd8d Isaku Yamahata
                       base, limit);
1097 b4dccd8d Isaku Yamahata
1098 b4dccd8d Isaku Yamahata
        base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY |
1099 b4dccd8d Isaku Yamahata
                                   PCI_BASE_ADDRESS_MEM_PREFETCH);
1100 b4dccd8d Isaku Yamahata
        limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY |
1101 b4dccd8d Isaku Yamahata
                                     PCI_BASE_ADDRESS_MEM_PREFETCH);
1102 b4dccd8d Isaku Yamahata
        monitor_printf(mon, "      prefetchable memory range "
1103 b4dccd8d Isaku Yamahata
                       "[0x%08"PRIx64", 0x%08"PRIx64"]\n", base, limit);
1104 80b3ada7 pbrook
    }
1105 502a5395 pbrook
    for(i = 0;i < PCI_NUM_REGIONS; i++) {
1106 502a5395 pbrook
        r = &d->io_regions[i];
1107 502a5395 pbrook
        if (r->size != 0) {
1108 376253ec aliguori
            monitor_printf(mon, "      BAR%d: ", i);
1109 0392a017 Isaku Yamahata
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1110 89e8b13c Isaku Yamahata
                monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
1111 89e8b13c Isaku Yamahata
                               " [0x%04"FMT_PCIBUS"].\n",
1112 376253ec aliguori
                               r->addr, r->addr + r->size - 1);
1113 502a5395 pbrook
            } else {
1114 14421258 Isaku Yamahata
                const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ?
1115 14421258 Isaku Yamahata
                    "64 bit" : "32 bit";
1116 14421258 Isaku Yamahata
                const char *prefetch =
1117 14421258 Isaku Yamahata
                    r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ?
1118 14421258 Isaku Yamahata
                    " prefetchable" : "";
1119 14421258 Isaku Yamahata
1120 14421258 Isaku Yamahata
                monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
1121 89e8b13c Isaku Yamahata
                               " [0x%08"FMT_PCIBUS"].\n",
1122 14421258 Isaku Yamahata
                               type, prefetch,
1123 376253ec aliguori
                               r->addr, r->addr + r->size - 1);
1124 502a5395 pbrook
            }
1125 502a5395 pbrook
        }
1126 77d4bc34 bellard
    }
1127 8ad12514 Gerd Hoffmann
    monitor_printf(mon, "      id \"%s\"\n", d->qdev.id ? d->qdev.id : "");
1128 80b3ada7 pbrook
    if (class == 0x0604 && d->config[0x19] != 0) {
1129 e822a52a Isaku Yamahata
        pci_for_each_device(bus, d->config[0x19], pci_info_device);
1130 80b3ada7 pbrook
    }
1131 384d8876 bellard
}
1132 384d8876 bellard
1133 1074df4f Isaku Yamahata
static void pci_for_each_device_under_bus(PCIBus *bus,
1134 1074df4f Isaku Yamahata
                                          void (*fn)(PCIBus *b, PCIDevice *d))
1135 384d8876 bellard
{
1136 384d8876 bellard
    PCIDevice *d;
1137 502a5395 pbrook
    int devfn;
1138 3b46e624 ths
1139 b47b0706 Isaku Yamahata
    for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1140 1074df4f Isaku Yamahata
        d = bus->devices[devfn];
1141 1074df4f Isaku Yamahata
        if (d)
1142 1074df4f Isaku Yamahata
            fn(bus, d);
1143 1074df4f Isaku Yamahata
    }
1144 1074df4f Isaku Yamahata
}
1145 1074df4f Isaku Yamahata
1146 1074df4f Isaku Yamahata
void pci_for_each_device(PCIBus *bus, int bus_num,
1147 1074df4f Isaku Yamahata
                         void (*fn)(PCIBus *b, PCIDevice *d))
1148 1074df4f Isaku Yamahata
{
1149 e822a52a Isaku Yamahata
    bus = pci_find_bus(bus, bus_num);
1150 1074df4f Isaku Yamahata
1151 502a5395 pbrook
    if (bus) {
1152 1074df4f Isaku Yamahata
        pci_for_each_device_under_bus(bus, fn);
1153 f2aa58c6 bellard
    }
1154 f2aa58c6 bellard
}
1155 f2aa58c6 bellard
1156 376253ec aliguori
void pci_info(Monitor *mon)
1157 f2aa58c6 bellard
{
1158 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
1159 e822a52a Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
1160 e822a52a Isaku Yamahata
        pci_for_each_device(host->bus, 0, pci_info_device);
1161 e822a52a Isaku Yamahata
    }
1162 77d4bc34 bellard
}
1163 a41b2ff2 pbrook
1164 cb457d76 aliguori
static const char * const pci_nic_models[] = {
1165 cb457d76 aliguori
    "ne2k_pci",
1166 cb457d76 aliguori
    "i82551",
1167 cb457d76 aliguori
    "i82557b",
1168 cb457d76 aliguori
    "i82559er",
1169 cb457d76 aliguori
    "rtl8139",
1170 cb457d76 aliguori
    "e1000",
1171 cb457d76 aliguori
    "pcnet",
1172 cb457d76 aliguori
    "virtio",
1173 cb457d76 aliguori
    NULL
1174 cb457d76 aliguori
};
1175 cb457d76 aliguori
1176 9d07d757 Paul Brook
static const char * const pci_nic_names[] = {
1177 9d07d757 Paul Brook
    "ne2k_pci",
1178 9d07d757 Paul Brook
    "i82551",
1179 9d07d757 Paul Brook
    "i82557b",
1180 9d07d757 Paul Brook
    "i82559er",
1181 9d07d757 Paul Brook
    "rtl8139",
1182 9d07d757 Paul Brook
    "e1000",
1183 9d07d757 Paul Brook
    "pcnet",
1184 53c25cea Paul Brook
    "virtio-net-pci",
1185 cb457d76 aliguori
    NULL
1186 cb457d76 aliguori
};
1187 cb457d76 aliguori
1188 a41b2ff2 pbrook
/* Initialize a PCI NIC.  */
1189 33e66b86 Markus Armbruster
/* FIXME callers should check for failure, but don't */
1190 5607c388 Markus Armbruster
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
1191 5607c388 Markus Armbruster
                        const char *default_devaddr)
1192 a41b2ff2 pbrook
{
1193 5607c388 Markus Armbruster
    const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1194 07caea31 Markus Armbruster
    PCIBus *bus;
1195 07caea31 Markus Armbruster
    int devfn;
1196 5607c388 Markus Armbruster
    PCIDevice *pci_dev;
1197 9d07d757 Paul Brook
    DeviceState *dev;
1198 cb457d76 aliguori
    int i;
1199 cb457d76 aliguori
1200 07caea31 Markus Armbruster
    i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1201 07caea31 Markus Armbruster
    if (i < 0)
1202 07caea31 Markus Armbruster
        return NULL;
1203 07caea31 Markus Armbruster
1204 07caea31 Markus Armbruster
    bus = pci_get_bus_devfn(&devfn, devaddr);
1205 07caea31 Markus Armbruster
    if (!bus) {
1206 07caea31 Markus Armbruster
        qemu_error("Invalid PCI device address %s for device %s\n",
1207 07caea31 Markus Armbruster
                   devaddr, pci_nic_names[i]);
1208 07caea31 Markus Armbruster
        return NULL;
1209 07caea31 Markus Armbruster
    }
1210 07caea31 Markus Armbruster
1211 499cf102 Markus Armbruster
    pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1212 9ee05825 Markus Armbruster
    dev = &pci_dev->qdev;
1213 dea7b3b9 Mark McLoughlin
    if (nd->name)
1214 dea7b3b9 Mark McLoughlin
        dev->id = qemu_strdup(nd->name);
1215 1cc33683 Gerd Hoffmann
    qdev_set_nic_properties(dev, nd);
1216 07caea31 Markus Armbruster
    if (qdev_init(dev) < 0)
1217 07caea31 Markus Armbruster
        return NULL;
1218 9ee05825 Markus Armbruster
    return pci_dev;
1219 a41b2ff2 pbrook
}
1220 a41b2ff2 pbrook
1221 07caea31 Markus Armbruster
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
1222 07caea31 Markus Armbruster
                               const char *default_devaddr)
1223 07caea31 Markus Armbruster
{
1224 07caea31 Markus Armbruster
    PCIDevice *res;
1225 07caea31 Markus Armbruster
1226 07caea31 Markus Armbruster
    if (qemu_show_nic_models(nd->model, pci_nic_models))
1227 07caea31 Markus Armbruster
        exit(0);
1228 07caea31 Markus Armbruster
1229 07caea31 Markus Armbruster
    res = pci_nic_init(nd, default_model, default_devaddr);
1230 07caea31 Markus Armbruster
    if (!res)
1231 07caea31 Markus Armbruster
        exit(1);
1232 07caea31 Markus Armbruster
    return res;
1233 07caea31 Markus Armbruster
}
1234 07caea31 Markus Armbruster
1235 80b3ada7 pbrook
typedef struct {
1236 80b3ada7 pbrook
    PCIDevice dev;
1237 03587182 Gerd Hoffmann
    PCIBus bus;
1238 03587182 Gerd Hoffmann
    uint32_t vid;
1239 03587182 Gerd Hoffmann
    uint32_t did;
1240 80b3ada7 pbrook
} PCIBridge;
1241 80b3ada7 pbrook
1242 a0c7a97e Isaku Yamahata
1243 a0c7a97e Isaku Yamahata
static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d)
1244 a0c7a97e Isaku Yamahata
{
1245 a0c7a97e Isaku Yamahata
    pci_update_mappings(d);
1246 a0c7a97e Isaku Yamahata
}
1247 a0c7a97e Isaku Yamahata
1248 a0c7a97e Isaku Yamahata
static void pci_bridge_update_mappings(PCIBus *b)
1249 a0c7a97e Isaku Yamahata
{
1250 a0c7a97e Isaku Yamahata
    PCIBus *child;
1251 a0c7a97e Isaku Yamahata
1252 a0c7a97e Isaku Yamahata
    pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn);
1253 a0c7a97e Isaku Yamahata
1254 a0c7a97e Isaku Yamahata
    QLIST_FOREACH(child, &b->child, sibling) {
1255 a0c7a97e Isaku Yamahata
        pci_bridge_update_mappings(child);
1256 a0c7a97e Isaku Yamahata
    }
1257 a0c7a97e Isaku Yamahata
}
1258 a0c7a97e Isaku Yamahata
1259 9596ebb7 pbrook
static void pci_bridge_write_config(PCIDevice *d,
1260 80b3ada7 pbrook
                             uint32_t address, uint32_t val, int len)
1261 80b3ada7 pbrook
{
1262 80b3ada7 pbrook
    pci_default_write_config(d, address, val, len);
1263 a0c7a97e Isaku Yamahata
1264 a0c7a97e Isaku Yamahata
    if (/* io base/limit */
1265 a0c7a97e Isaku Yamahata
        ranges_overlap(address, len, PCI_IO_BASE, 2) ||
1266 a0c7a97e Isaku Yamahata
1267 a0c7a97e Isaku Yamahata
        /* memory base/limit, prefetchable base/limit and
1268 a0c7a97e Isaku Yamahata
           io base/limit upper 16 */
1269 a0c7a97e Isaku Yamahata
        ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
1270 a0c7a97e Isaku Yamahata
        pci_bridge_update_mappings(d->bus);
1271 a0c7a97e Isaku Yamahata
    }
1272 80b3ada7 pbrook
}
1273 80b3ada7 pbrook
1274 e822a52a Isaku Yamahata
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
1275 3ae80618 aliguori
{
1276 e822a52a Isaku Yamahata
    PCIBus *sec;
1277 3ae80618 aliguori
1278 e822a52a Isaku Yamahata
    if (!bus)
1279 e822a52a Isaku Yamahata
        return NULL;
1280 3ae80618 aliguori
1281 e822a52a Isaku Yamahata
    if (pci_bus_num(bus) == bus_num) {
1282 e822a52a Isaku Yamahata
        return bus;
1283 e822a52a Isaku Yamahata
    }
1284 e822a52a Isaku Yamahata
1285 e822a52a Isaku Yamahata
    /* try child bus */
1286 e822a52a Isaku Yamahata
    QLIST_FOREACH(sec, &bus->child, sibling) {
1287 070297d2 Isaku Yamahata
1288 070297d2 Isaku Yamahata
        if (!bus->parent_dev /* pci host bridge */
1289 070297d2 Isaku Yamahata
            || (pci_bus_num(sec) <= bus_num &&
1290 070297d2 Isaku Yamahata
                bus->parent_dev->config[PCI_SUBORDINATE_BUS])) {
1291 e822a52a Isaku Yamahata
            return pci_find_bus(sec, bus_num);
1292 e822a52a Isaku Yamahata
        }
1293 e822a52a Isaku Yamahata
    }
1294 e822a52a Isaku Yamahata
1295 e822a52a Isaku Yamahata
    return NULL;
1296 3ae80618 aliguori
}
1297 3ae80618 aliguori
1298 e822a52a Isaku Yamahata
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
1299 3ae80618 aliguori
{
1300 e822a52a Isaku Yamahata
    bus = pci_find_bus(bus, bus_num);
1301 3ae80618 aliguori
1302 3ae80618 aliguori
    if (!bus)
1303 3ae80618 aliguori
        return NULL;
1304 3ae80618 aliguori
1305 3ae80618 aliguori
    return bus->devices[PCI_DEVFN(slot, function)];
1306 3ae80618 aliguori
}
1307 3ae80618 aliguori
1308 03587182 Gerd Hoffmann
static int pci_bridge_initfn(PCIDevice *dev)
1309 80b3ada7 pbrook
{
1310 03587182 Gerd Hoffmann
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev);
1311 480b9f24 blueswir1
1312 03587182 Gerd Hoffmann
    pci_config_set_vendor_id(s->dev.config, s->vid);
1313 03587182 Gerd Hoffmann
    pci_config_set_device_id(s->dev.config, s->did);
1314 480b9f24 blueswir1
1315 74c01823 Isaku Yamahata
    pci_set_word(dev->config + PCI_STATUS,
1316 74c01823 Isaku Yamahata
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1317 74c01823 Isaku Yamahata
    pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
1318 d6318738 Michael S. Tsirkin
    dev->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_BRIDGE;
1319 74c01823 Isaku Yamahata
    pci_set_word(dev->config + PCI_SEC_STATUS,
1320 74c01823 Isaku Yamahata
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1321 03587182 Gerd Hoffmann
    return 0;
1322 03587182 Gerd Hoffmann
}
1323 80b3ada7 pbrook
1324 e822a52a Isaku Yamahata
static int pci_bridge_exitfn(PCIDevice *pci_dev)
1325 e822a52a Isaku Yamahata
{
1326 e822a52a Isaku Yamahata
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
1327 e822a52a Isaku Yamahata
    PCIBus *bus = &s->bus;
1328 e822a52a Isaku Yamahata
    pci_unregister_secondary_bus(bus);
1329 e822a52a Isaku Yamahata
    return 0;
1330 e822a52a Isaku Yamahata
}
1331 e822a52a Isaku Yamahata
1332 03587182 Gerd Hoffmann
PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
1333 03587182 Gerd Hoffmann
                        pci_map_irq_fn map_irq, const char *name)
1334 03587182 Gerd Hoffmann
{
1335 03587182 Gerd Hoffmann
    PCIDevice *dev;
1336 03587182 Gerd Hoffmann
    PCIBridge *s;
1337 03587182 Gerd Hoffmann
1338 499cf102 Markus Armbruster
    dev = pci_create(bus, devfn, "pci-bridge");
1339 03587182 Gerd Hoffmann
    qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
1340 03587182 Gerd Hoffmann
    qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
1341 e23a1b33 Markus Armbruster
    qdev_init_nofail(&dev->qdev);
1342 03587182 Gerd Hoffmann
1343 03587182 Gerd Hoffmann
    s = DO_UPCAST(PCIBridge, dev, dev);
1344 e822a52a Isaku Yamahata
    pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name);
1345 03587182 Gerd Hoffmann
    return &s->bus;
1346 80b3ada7 pbrook
}
1347 6b1b92d3 Paul Brook
1348 d6318738 Michael S. Tsirkin
PCIDevice *pci_bridge_get_device(PCIBus *bus)
1349 d6318738 Michael S. Tsirkin
{
1350 d6318738 Michael S. Tsirkin
    return bus->parent_dev;
1351 d6318738 Michael S. Tsirkin
}
1352 d6318738 Michael S. Tsirkin
1353 81a322d4 Gerd Hoffmann
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1354 6b1b92d3 Paul Brook
{
1355 6b1b92d3 Paul Brook
    PCIDevice *pci_dev = (PCIDevice *)qdev;
1356 02e2da45 Paul Brook
    PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1357 6b1b92d3 Paul Brook
    PCIBus *bus;
1358 ee995ffb Gerd Hoffmann
    int devfn, rc;
1359 6b1b92d3 Paul Brook
1360 a9f49946 Isaku Yamahata
    /* initialize cap_present for pci_is_express() and pci_config_size() */
1361 a9f49946 Isaku Yamahata
    if (info->is_express) {
1362 a9f49946 Isaku Yamahata
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1363 a9f49946 Isaku Yamahata
    }
1364 a9f49946 Isaku Yamahata
1365 02e2da45 Paul Brook
    bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1366 ee6847d1 Gerd Hoffmann
    devfn = pci_dev->devfn;
1367 16eaedf2 Gerd Hoffmann
    pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1368 fb231628 Isaku Yamahata
                                     info->config_read, info->config_write,
1369 fb231628 Isaku Yamahata
                                     info->header_type);
1370 09e3acc6 Gerd Hoffmann
    if (pci_dev == NULL)
1371 09e3acc6 Gerd Hoffmann
        return -1;
1372 ee995ffb Gerd Hoffmann
    rc = info->init(pci_dev);
1373 ee995ffb Gerd Hoffmann
    if (rc != 0)
1374 ee995ffb Gerd Hoffmann
        return rc;
1375 8c52c8f3 Gerd Hoffmann
1376 8c52c8f3 Gerd Hoffmann
    /* rom loading */
1377 8c52c8f3 Gerd Hoffmann
    if (pci_dev->romfile == NULL && info->romfile != NULL)
1378 8c52c8f3 Gerd Hoffmann
        pci_dev->romfile = qemu_strdup(info->romfile);
1379 8c52c8f3 Gerd Hoffmann
    pci_add_option_rom(pci_dev);
1380 8c52c8f3 Gerd Hoffmann
1381 ee995ffb Gerd Hoffmann
    if (qdev->hotplugged)
1382 ee995ffb Gerd Hoffmann
        bus->hotplug(pci_dev, 1);
1383 ee995ffb Gerd Hoffmann
    return 0;
1384 ee995ffb Gerd Hoffmann
}
1385 ee995ffb Gerd Hoffmann
1386 ee995ffb Gerd Hoffmann
static int pci_unplug_device(DeviceState *qdev)
1387 ee995ffb Gerd Hoffmann
{
1388 ee995ffb Gerd Hoffmann
    PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1389 ee995ffb Gerd Hoffmann
1390 ee995ffb Gerd Hoffmann
    dev->bus->hotplug(dev, 0);
1391 ee995ffb Gerd Hoffmann
    return 0;
1392 6b1b92d3 Paul Brook
}
1393 6b1b92d3 Paul Brook
1394 0aab0d3a Gerd Hoffmann
void pci_qdev_register(PCIDeviceInfo *info)
1395 6b1b92d3 Paul Brook
{
1396 02e2da45 Paul Brook
    info->qdev.init = pci_qdev_init;
1397 ee995ffb Gerd Hoffmann
    info->qdev.unplug = pci_unplug_device;
1398 a36a344d Gerd Hoffmann
    info->qdev.exit = pci_unregister_device;
1399 10c4c98a Gerd Hoffmann
    info->qdev.bus_info = &pci_bus_info;
1400 074f2fff Gerd Hoffmann
    qdev_register(&info->qdev);
1401 6b1b92d3 Paul Brook
}
1402 6b1b92d3 Paul Brook
1403 0aab0d3a Gerd Hoffmann
void pci_qdev_register_many(PCIDeviceInfo *info)
1404 0aab0d3a Gerd Hoffmann
{
1405 0aab0d3a Gerd Hoffmann
    while (info->qdev.name) {
1406 0aab0d3a Gerd Hoffmann
        pci_qdev_register(info);
1407 0aab0d3a Gerd Hoffmann
        info++;
1408 0aab0d3a Gerd Hoffmann
    }
1409 0aab0d3a Gerd Hoffmann
}
1410 0aab0d3a Gerd Hoffmann
1411 499cf102 Markus Armbruster
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1412 6b1b92d3 Paul Brook
{
1413 6b1b92d3 Paul Brook
    DeviceState *dev;
1414 6b1b92d3 Paul Brook
1415 02e2da45 Paul Brook
    dev = qdev_create(&bus->qbus, name);
1416 a6307b08 Gerd Hoffmann
    qdev_prop_set_uint32(dev, "addr", devfn);
1417 71077c1c Gerd Hoffmann
    return DO_UPCAST(PCIDevice, qdev, dev);
1418 71077c1c Gerd Hoffmann
}
1419 6b1b92d3 Paul Brook
1420 71077c1c Gerd Hoffmann
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1421 71077c1c Gerd Hoffmann
{
1422 499cf102 Markus Armbruster
    PCIDevice *dev = pci_create(bus, devfn, name);
1423 e23a1b33 Markus Armbruster
    qdev_init_nofail(&dev->qdev);
1424 71077c1c Gerd Hoffmann
    return dev;
1425 6b1b92d3 Paul Brook
}
1426 6f4cbd39 Michael S. Tsirkin
1427 6f4cbd39 Michael S. Tsirkin
static int pci_find_space(PCIDevice *pdev, uint8_t size)
1428 6f4cbd39 Michael S. Tsirkin
{
1429 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(pdev);
1430 6f4cbd39 Michael S. Tsirkin
    int offset = PCI_CONFIG_HEADER_SIZE;
1431 6f4cbd39 Michael S. Tsirkin
    int i;
1432 a9f49946 Isaku Yamahata
    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
1433 6f4cbd39 Michael S. Tsirkin
        if (pdev->used[i])
1434 6f4cbd39 Michael S. Tsirkin
            offset = i + 1;
1435 6f4cbd39 Michael S. Tsirkin
        else if (i - offset + 1 == size)
1436 6f4cbd39 Michael S. Tsirkin
            return offset;
1437 6f4cbd39 Michael S. Tsirkin
    return 0;
1438 6f4cbd39 Michael S. Tsirkin
}
1439 6f4cbd39 Michael S. Tsirkin
1440 6f4cbd39 Michael S. Tsirkin
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1441 6f4cbd39 Michael S. Tsirkin
                                        uint8_t *prev_p)
1442 6f4cbd39 Michael S. Tsirkin
{
1443 6f4cbd39 Michael S. Tsirkin
    uint8_t next, prev;
1444 6f4cbd39 Michael S. Tsirkin
1445 6f4cbd39 Michael S. Tsirkin
    if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1446 6f4cbd39 Michael S. Tsirkin
        return 0;
1447 6f4cbd39 Michael S. Tsirkin
1448 6f4cbd39 Michael S. Tsirkin
    for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1449 6f4cbd39 Michael S. Tsirkin
         prev = next + PCI_CAP_LIST_NEXT)
1450 6f4cbd39 Michael S. Tsirkin
        if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1451 6f4cbd39 Michael S. Tsirkin
            break;
1452 6f4cbd39 Michael S. Tsirkin
1453 6f4cbd39 Michael S. Tsirkin
    if (prev_p)
1454 6f4cbd39 Michael S. Tsirkin
        *prev_p = prev;
1455 6f4cbd39 Michael S. Tsirkin
    return next;
1456 6f4cbd39 Michael S. Tsirkin
}
1457 6f4cbd39 Michael S. Tsirkin
1458 c2039bd0 Anthony Liguori
static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type)
1459 c2039bd0 Anthony Liguori
{
1460 c2039bd0 Anthony Liguori
    cpu_register_physical_memory(addr, size, pdev->rom_offset);
1461 c2039bd0 Anthony Liguori
}
1462 c2039bd0 Anthony Liguori
1463 c2039bd0 Anthony Liguori
/* Add an option rom for the device */
1464 8c52c8f3 Gerd Hoffmann
static int pci_add_option_rom(PCIDevice *pdev)
1465 c2039bd0 Anthony Liguori
{
1466 c2039bd0 Anthony Liguori
    int size;
1467 c2039bd0 Anthony Liguori
    char *path;
1468 c2039bd0 Anthony Liguori
    void *ptr;
1469 c2039bd0 Anthony Liguori
1470 8c52c8f3 Gerd Hoffmann
    if (!pdev->romfile)
1471 8c52c8f3 Gerd Hoffmann
        return 0;
1472 8c52c8f3 Gerd Hoffmann
    if (strlen(pdev->romfile) == 0)
1473 8c52c8f3 Gerd Hoffmann
        return 0;
1474 8c52c8f3 Gerd Hoffmann
1475 8c52c8f3 Gerd Hoffmann
    path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
1476 c2039bd0 Anthony Liguori
    if (path == NULL) {
1477 8c52c8f3 Gerd Hoffmann
        path = qemu_strdup(pdev->romfile);
1478 c2039bd0 Anthony Liguori
    }
1479 c2039bd0 Anthony Liguori
1480 c2039bd0 Anthony Liguori
    size = get_image_size(path);
1481 8c52c8f3 Gerd Hoffmann
    if (size < 0) {
1482 8c52c8f3 Gerd Hoffmann
        qemu_error("%s: failed to find romfile \"%s\"\n", __FUNCTION__,
1483 8c52c8f3 Gerd Hoffmann
                   pdev->romfile);
1484 8c52c8f3 Gerd Hoffmann
        return -1;
1485 8c52c8f3 Gerd Hoffmann
    }
1486 c2039bd0 Anthony Liguori
    if (size & (size - 1)) {
1487 c2039bd0 Anthony Liguori
        size = 1 << qemu_fls(size);
1488 c2039bd0 Anthony Liguori
    }
1489 c2039bd0 Anthony Liguori
1490 c2039bd0 Anthony Liguori
    pdev->rom_offset = qemu_ram_alloc(size);
1491 c2039bd0 Anthony Liguori
1492 c2039bd0 Anthony Liguori
    ptr = qemu_get_ram_ptr(pdev->rom_offset);
1493 c2039bd0 Anthony Liguori
    load_image(path, ptr);
1494 c2039bd0 Anthony Liguori
    qemu_free(path);
1495 c2039bd0 Anthony Liguori
1496 c2039bd0 Anthony Liguori
    pci_register_bar(pdev, PCI_ROM_SLOT, size,
1497 c2039bd0 Anthony Liguori
                     0, pci_map_option_rom);
1498 c2039bd0 Anthony Liguori
1499 c2039bd0 Anthony Liguori
    return 0;
1500 c2039bd0 Anthony Liguori
}
1501 c2039bd0 Anthony Liguori
1502 6f4cbd39 Michael S. Tsirkin
/* Reserve space and add capability to the linked list in pci config space */
1503 6f4cbd39 Michael S. Tsirkin
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1504 6f4cbd39 Michael S. Tsirkin
{
1505 6f4cbd39 Michael S. Tsirkin
    uint8_t offset = pci_find_space(pdev, size);
1506 6f4cbd39 Michael S. Tsirkin
    uint8_t *config = pdev->config + offset;
1507 6f4cbd39 Michael S. Tsirkin
    if (!offset)
1508 6f4cbd39 Michael S. Tsirkin
        return -ENOSPC;
1509 6f4cbd39 Michael S. Tsirkin
    config[PCI_CAP_LIST_ID] = cap_id;
1510 6f4cbd39 Michael S. Tsirkin
    config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1511 6f4cbd39 Michael S. Tsirkin
    pdev->config[PCI_CAPABILITY_LIST] = offset;
1512 6f4cbd39 Michael S. Tsirkin
    pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1513 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0xFF, size);
1514 6f4cbd39 Michael S. Tsirkin
    /* Make capability read-only by default */
1515 6f4cbd39 Michael S. Tsirkin
    memset(pdev->wmask + offset, 0, size);
1516 bd4b65ee Michael S. Tsirkin
    /* Check capability by default */
1517 bd4b65ee Michael S. Tsirkin
    memset(pdev->cmask + offset, 0xFF, size);
1518 6f4cbd39 Michael S. Tsirkin
    return offset;
1519 6f4cbd39 Michael S. Tsirkin
}
1520 6f4cbd39 Michael S. Tsirkin
1521 6f4cbd39 Michael S. Tsirkin
/* Unlink capability from the pci config space. */
1522 6f4cbd39 Michael S. Tsirkin
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1523 6f4cbd39 Michael S. Tsirkin
{
1524 6f4cbd39 Michael S. Tsirkin
    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1525 6f4cbd39 Michael S. Tsirkin
    if (!offset)
1526 6f4cbd39 Michael S. Tsirkin
        return;
1527 6f4cbd39 Michael S. Tsirkin
    pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1528 6f4cbd39 Michael S. Tsirkin
    /* Make capability writeable again */
1529 6f4cbd39 Michael S. Tsirkin
    memset(pdev->wmask + offset, 0xff, size);
1530 bd4b65ee Michael S. Tsirkin
    /* Clear cmask as device-specific registers can't be checked */
1531 bd4b65ee Michael S. Tsirkin
    memset(pdev->cmask + offset, 0, size);
1532 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0, size);
1533 6f4cbd39 Michael S. Tsirkin
1534 6f4cbd39 Michael S. Tsirkin
    if (!pdev->config[PCI_CAPABILITY_LIST])
1535 6f4cbd39 Michael S. Tsirkin
        pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1536 6f4cbd39 Michael S. Tsirkin
}
1537 6f4cbd39 Michael S. Tsirkin
1538 6f4cbd39 Michael S. Tsirkin
/* Reserve space for capability at a known offset (to call after load). */
1539 6f4cbd39 Michael S. Tsirkin
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1540 6f4cbd39 Michael S. Tsirkin
{
1541 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0xff, size);
1542 6f4cbd39 Michael S. Tsirkin
}
1543 6f4cbd39 Michael S. Tsirkin
1544 6f4cbd39 Michael S. Tsirkin
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1545 6f4cbd39 Michael S. Tsirkin
{
1546 6f4cbd39 Michael S. Tsirkin
    return pci_find_capability_list(pdev, cap_id, NULL);
1547 6f4cbd39 Michael S. Tsirkin
}
1548 10c4c98a Gerd Hoffmann
1549 10c4c98a Gerd Hoffmann
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1550 10c4c98a Gerd Hoffmann
{
1551 10c4c98a Gerd Hoffmann
    PCIDevice *d = (PCIDevice *)dev;
1552 10c4c98a Gerd Hoffmann
    const pci_class_desc *desc;
1553 10c4c98a Gerd Hoffmann
    char ctxt[64];
1554 10c4c98a Gerd Hoffmann
    PCIIORegion *r;
1555 10c4c98a Gerd Hoffmann
    int i, class;
1556 10c4c98a Gerd Hoffmann
1557 b0ff8eb2 Isaku Yamahata
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1558 10c4c98a Gerd Hoffmann
    desc = pci_class_descriptions;
1559 10c4c98a Gerd Hoffmann
    while (desc->desc && class != desc->class)
1560 10c4c98a Gerd Hoffmann
        desc++;
1561 10c4c98a Gerd Hoffmann
    if (desc->desc) {
1562 10c4c98a Gerd Hoffmann
        snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1563 10c4c98a Gerd Hoffmann
    } else {
1564 10c4c98a Gerd Hoffmann
        snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1565 10c4c98a Gerd Hoffmann
    }
1566 10c4c98a Gerd Hoffmann
1567 10c4c98a Gerd Hoffmann
    monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1568 10c4c98a Gerd Hoffmann
                   "pci id %04x:%04x (sub %04x:%04x)\n",
1569 10c4c98a Gerd Hoffmann
                   indent, "", ctxt,
1570 e822a52a Isaku Yamahata
                   d->config[PCI_SECONDARY_BUS],
1571 e822a52a Isaku Yamahata
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1572 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_VENDOR_ID),
1573 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_DEVICE_ID),
1574 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1575 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1576 10c4c98a Gerd Hoffmann
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
1577 10c4c98a Gerd Hoffmann
        r = &d->io_regions[i];
1578 10c4c98a Gerd Hoffmann
        if (!r->size)
1579 10c4c98a Gerd Hoffmann
            continue;
1580 89e8b13c Isaku Yamahata
        monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1581 89e8b13c Isaku Yamahata
                       " [0x%"FMT_PCIBUS"]\n",
1582 89e8b13c Isaku Yamahata
                       indent, "",
1583 0392a017 Isaku Yamahata
                       i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1584 10c4c98a Gerd Hoffmann
                       r->addr, r->addr + r->size - 1);
1585 10c4c98a Gerd Hoffmann
    }
1586 10c4c98a Gerd Hoffmann
}
1587 03587182 Gerd Hoffmann
1588 03587182 Gerd Hoffmann
static PCIDeviceInfo bridge_info = {
1589 03587182 Gerd Hoffmann
    .qdev.name    = "pci-bridge",
1590 03587182 Gerd Hoffmann
    .qdev.size    = sizeof(PCIBridge),
1591 03587182 Gerd Hoffmann
    .init         = pci_bridge_initfn,
1592 e822a52a Isaku Yamahata
    .exit         = pci_bridge_exitfn,
1593 03587182 Gerd Hoffmann
    .config_write = pci_bridge_write_config,
1594 03587182 Gerd Hoffmann
    .qdev.props   = (Property[]) {
1595 03587182 Gerd Hoffmann
        DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0),
1596 03587182 Gerd Hoffmann
        DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0),
1597 03587182 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
1598 03587182 Gerd Hoffmann
    }
1599 03587182 Gerd Hoffmann
};
1600 03587182 Gerd Hoffmann
1601 03587182 Gerd Hoffmann
static void pci_register_devices(void)
1602 03587182 Gerd Hoffmann
{
1603 03587182 Gerd Hoffmann
    pci_qdev_register(&bridge_info);
1604 03587182 Gerd Hoffmann
}
1605 03587182 Gerd Hoffmann
1606 03587182 Gerd Hoffmann
device_init(pci_register_devices)