Statistics
| Branch: | Revision:

root / hw / pci.c @ 5084bca1

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