Statistics
| Branch: | Revision:

root / hw / pci.c @ a85d6887

History | View | Annotate | Download (26 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 69b91039 bellard
32 30468f78 bellard
struct PCIBus {
33 02e2da45 Paul Brook
    BusState qbus;
34 30468f78 bellard
    int bus_num;
35 30468f78 bellard
    int devfn_min;
36 502a5395 pbrook
    pci_set_irq_fn set_irq;
37 d2b59317 pbrook
    pci_map_irq_fn map_irq;
38 30468f78 bellard
    uint32_t config_reg; /* XXX: suppress */
39 384d8876 bellard
    /* low level pic */
40 384d8876 bellard
    SetIRQFunc *low_set_irq;
41 d537cf6c pbrook
    qemu_irq *irq_opaque;
42 30468f78 bellard
    PCIDevice *devices[256];
43 80b3ada7 pbrook
    PCIDevice *parent_dev;
44 80b3ada7 pbrook
    PCIBus *next;
45 d2b59317 pbrook
    /* The bus IRQ state is the logical OR of the connected devices.
46 d2b59317 pbrook
       Keep a count of the number of devices with raised IRQs.  */
47 52fc1d83 balrog
    int nirq;
48 80b3ada7 pbrook
    int irq_count[];
49 30468f78 bellard
};
50 69b91039 bellard
51 1941d19c bellard
static void pci_update_mappings(PCIDevice *d);
52 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level);
53 1941d19c bellard
54 69b91039 bellard
target_phys_addr_t pci_mem_base;
55 d350d97d aliguori
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
56 d350d97d aliguori
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
57 30468f78 bellard
static PCIBus *first_bus;
58 30468f78 bellard
59 52fc1d83 balrog
static void pcibus_save(QEMUFile *f, void *opaque)
60 52fc1d83 balrog
{
61 52fc1d83 balrog
    PCIBus *bus = (PCIBus *)opaque;
62 52fc1d83 balrog
    int i;
63 52fc1d83 balrog
64 52fc1d83 balrog
    qemu_put_be32(f, bus->nirq);
65 52fc1d83 balrog
    for (i = 0; i < bus->nirq; i++)
66 52fc1d83 balrog
        qemu_put_be32(f, bus->irq_count[i]);
67 52fc1d83 balrog
}
68 52fc1d83 balrog
69 52fc1d83 balrog
static int  pcibus_load(QEMUFile *f, void *opaque, int version_id)
70 52fc1d83 balrog
{
71 52fc1d83 balrog
    PCIBus *bus = (PCIBus *)opaque;
72 52fc1d83 balrog
    int i, nirq;
73 52fc1d83 balrog
74 52fc1d83 balrog
    if (version_id != 1)
75 52fc1d83 balrog
        return -EINVAL;
76 52fc1d83 balrog
77 52fc1d83 balrog
    nirq = qemu_get_be32(f);
78 52fc1d83 balrog
    if (bus->nirq != nirq) {
79 52fc1d83 balrog
        fprintf(stderr, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
80 52fc1d83 balrog
                nirq, bus->nirq);
81 52fc1d83 balrog
        return -EINVAL;
82 52fc1d83 balrog
    }
83 52fc1d83 balrog
84 52fc1d83 balrog
    for (i = 0; i < nirq; i++)
85 52fc1d83 balrog
        bus->irq_count[i] = qemu_get_be32(f);
86 52fc1d83 balrog
87 52fc1d83 balrog
    return 0;
88 52fc1d83 balrog
}
89 52fc1d83 balrog
90 02e2da45 Paul Brook
PCIBus *pci_register_bus(DeviceState *parent, const char *name,
91 02e2da45 Paul Brook
                         pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
92 d537cf6c pbrook
                         qemu_irq *pic, int devfn_min, int nirq)
93 30468f78 bellard
{
94 30468f78 bellard
    PCIBus *bus;
95 52fc1d83 balrog
    static int nbus = 0;
96 52fc1d83 balrog
97 02e2da45 Paul Brook
    bus = FROM_QBUS(PCIBus, qbus_create(BUS_TYPE_PCI,
98 02e2da45 Paul Brook
                                        sizeof(PCIBus) + (nirq * sizeof(int)),
99 02e2da45 Paul Brook
                                        parent, name));
100 502a5395 pbrook
    bus->set_irq = set_irq;
101 d2b59317 pbrook
    bus->map_irq = map_irq;
102 502a5395 pbrook
    bus->irq_opaque = pic;
103 502a5395 pbrook
    bus->devfn_min = devfn_min;
104 52fc1d83 balrog
    bus->nirq = nirq;
105 425c608c Isaku Yamahata
    bus->next = first_bus;
106 30468f78 bellard
    first_bus = bus;
107 52fc1d83 balrog
    register_savevm("PCIBUS", nbus++, 1, pcibus_save, pcibus_load, bus);
108 30468f78 bellard
    return bus;
109 30468f78 bellard
}
110 69b91039 bellard
111 9596ebb7 pbrook
static PCIBus *pci_register_secondary_bus(PCIDevice *dev, pci_map_irq_fn map_irq)
112 80b3ada7 pbrook
{
113 80b3ada7 pbrook
    PCIBus *bus;
114 80b3ada7 pbrook
    bus = qemu_mallocz(sizeof(PCIBus));
115 80b3ada7 pbrook
    bus->map_irq = map_irq;
116 80b3ada7 pbrook
    bus->parent_dev = dev;
117 80b3ada7 pbrook
    bus->next = dev->bus->next;
118 80b3ada7 pbrook
    dev->bus->next = bus;
119 80b3ada7 pbrook
    return bus;
120 80b3ada7 pbrook
}
121 80b3ada7 pbrook
122 502a5395 pbrook
int pci_bus_num(PCIBus *s)
123 502a5395 pbrook
{
124 502a5395 pbrook
    return s->bus_num;
125 502a5395 pbrook
}
126 502a5395 pbrook
127 1941d19c bellard
void pci_device_save(PCIDevice *s, QEMUFile *f)
128 30ca2aab bellard
{
129 52fc1d83 balrog
    int i;
130 52fc1d83 balrog
131 52fc1d83 balrog
    qemu_put_be32(f, 2); /* PCI device version */
132 30ca2aab bellard
    qemu_put_buffer(f, s->config, 256);
133 52fc1d83 balrog
    for (i = 0; i < 4; i++)
134 52fc1d83 balrog
        qemu_put_be32(f, s->irq_state[i]);
135 30ca2aab bellard
}
136 30ca2aab bellard
137 1941d19c bellard
int pci_device_load(PCIDevice *s, QEMUFile *f)
138 30ca2aab bellard
{
139 1941d19c bellard
    uint32_t version_id;
140 52fc1d83 balrog
    int i;
141 52fc1d83 balrog
142 1941d19c bellard
    version_id = qemu_get_be32(f);
143 52fc1d83 balrog
    if (version_id > 2)
144 30ca2aab bellard
        return -EINVAL;
145 30ca2aab bellard
    qemu_get_buffer(f, s->config, 256);
146 1941d19c bellard
    pci_update_mappings(s);
147 52fc1d83 balrog
148 52fc1d83 balrog
    if (version_id >= 2)
149 52fc1d83 balrog
        for (i = 0; i < 4; i ++)
150 52fc1d83 balrog
            s->irq_state[i] = qemu_get_be32(f);
151 52fc1d83 balrog
152 30ca2aab bellard
    return 0;
153 30ca2aab bellard
}
154 30ca2aab bellard
155 d350d97d aliguori
static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
156 d350d97d aliguori
{
157 d350d97d aliguori
    uint16_t *id;
158 d350d97d aliguori
159 d350d97d aliguori
    id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
160 d350d97d aliguori
    id[0] = cpu_to_le16(pci_default_sub_vendor_id);
161 d350d97d aliguori
    id[1] = cpu_to_le16(pci_default_sub_device_id);
162 d350d97d aliguori
    return 0;
163 d350d97d aliguori
}
164 d350d97d aliguori
165 880345c4 aliguori
/*
166 880345c4 aliguori
 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
167 880345c4 aliguori
 */
168 880345c4 aliguori
static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
169 880345c4 aliguori
{
170 880345c4 aliguori
    const char *p;
171 880345c4 aliguori
    char *e;
172 880345c4 aliguori
    unsigned long val;
173 880345c4 aliguori
    unsigned long dom = 0, bus = 0;
174 880345c4 aliguori
    unsigned slot = 0;
175 880345c4 aliguori
176 880345c4 aliguori
    p = addr;
177 880345c4 aliguori
    val = strtoul(p, &e, 16);
178 880345c4 aliguori
    if (e == p)
179 880345c4 aliguori
        return -1;
180 880345c4 aliguori
    if (*e == ':') {
181 880345c4 aliguori
        bus = val;
182 880345c4 aliguori
        p = e + 1;
183 880345c4 aliguori
        val = strtoul(p, &e, 16);
184 880345c4 aliguori
        if (e == p)
185 880345c4 aliguori
            return -1;
186 880345c4 aliguori
        if (*e == ':') {
187 880345c4 aliguori
            dom = bus;
188 880345c4 aliguori
            bus = val;
189 880345c4 aliguori
            p = e + 1;
190 880345c4 aliguori
            val = strtoul(p, &e, 16);
191 880345c4 aliguori
            if (e == p)
192 880345c4 aliguori
                return -1;
193 880345c4 aliguori
        }
194 880345c4 aliguori
    }
195 880345c4 aliguori
196 880345c4 aliguori
    if (dom > 0xffff || bus > 0xff || val > 0x1f)
197 880345c4 aliguori
        return -1;
198 880345c4 aliguori
199 880345c4 aliguori
    slot = val;
200 880345c4 aliguori
201 880345c4 aliguori
    if (*e)
202 880345c4 aliguori
        return -1;
203 880345c4 aliguori
204 880345c4 aliguori
    /* Note: QEMU doesn't implement domains other than 0 */
205 880345c4 aliguori
    if (dom != 0 || pci_find_bus(bus) == NULL)
206 880345c4 aliguori
        return -1;
207 880345c4 aliguori
208 880345c4 aliguori
    *domp = dom;
209 880345c4 aliguori
    *busp = bus;
210 880345c4 aliguori
    *slotp = slot;
211 880345c4 aliguori
    return 0;
212 880345c4 aliguori
}
213 880345c4 aliguori
214 880345c4 aliguori
int pci_read_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
215 880345c4 aliguori
{
216 880345c4 aliguori
    char devaddr[32];
217 880345c4 aliguori
218 880345c4 aliguori
    if (!get_param_value(devaddr, sizeof(devaddr), "pci_addr", addr))
219 880345c4 aliguori
        return -1;
220 880345c4 aliguori
221 880345c4 aliguori
    return pci_parse_devaddr(devaddr, domp, busp, slotp);
222 880345c4 aliguori
}
223 880345c4 aliguori
224 880345c4 aliguori
int pci_assign_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
225 880345c4 aliguori
{
226 880345c4 aliguori
    char devaddr[32];
227 880345c4 aliguori
228 880345c4 aliguori
    if (!get_param_value(devaddr, sizeof(devaddr), "pci_addr", addr))
229 880345c4 aliguori
        return -1;
230 880345c4 aliguori
231 880345c4 aliguori
    if (!strcmp(devaddr, "auto")) {
232 880345c4 aliguori
        *domp = *busp = 0;
233 880345c4 aliguori
        *slotp = -1;
234 880345c4 aliguori
        /* want to support dom/bus auto-assign at some point */
235 880345c4 aliguori
        return 0;
236 880345c4 aliguori
    }
237 880345c4 aliguori
238 880345c4 aliguori
    return pci_parse_devaddr(devaddr, domp, busp, slotp);
239 880345c4 aliguori
}
240 880345c4 aliguori
241 69b91039 bellard
/* -1 for devfn means auto assign */
242 6b1b92d3 Paul Brook
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
243 6b1b92d3 Paul Brook
                                         const char *name, int devfn,
244 6b1b92d3 Paul Brook
                                         PCIConfigReadFunc *config_read,
245 6b1b92d3 Paul Brook
                                         PCIConfigWriteFunc *config_write)
246 69b91039 bellard
{
247 69b91039 bellard
    if (devfn < 0) {
248 30468f78 bellard
        for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
249 30468f78 bellard
            if (!bus->devices[devfn])
250 69b91039 bellard
                goto found;
251 69b91039 bellard
        }
252 69b91039 bellard
        return NULL;
253 69b91039 bellard
    found: ;
254 69b91039 bellard
    }
255 30468f78 bellard
    pci_dev->bus = bus;
256 69b91039 bellard
    pci_dev->devfn = devfn;
257 69b91039 bellard
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
258 d2b59317 pbrook
    memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
259 d350d97d aliguori
    pci_set_default_subsystem_id(pci_dev);
260 0ac32c83 bellard
261 0ac32c83 bellard
    if (!config_read)
262 0ac32c83 bellard
        config_read = pci_default_read_config;
263 0ac32c83 bellard
    if (!config_write)
264 0ac32c83 bellard
        config_write = pci_default_write_config;
265 69b91039 bellard
    pci_dev->config_read = config_read;
266 69b91039 bellard
    pci_dev->config_write = config_write;
267 30468f78 bellard
    bus->devices[devfn] = pci_dev;
268 d537cf6c pbrook
    pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, 4);
269 69b91039 bellard
    return pci_dev;
270 69b91039 bellard
}
271 69b91039 bellard
272 6b1b92d3 Paul Brook
PCIDevice *pci_register_device(PCIBus *bus, const char *name,
273 6b1b92d3 Paul Brook
                               int instance_size, int devfn,
274 6b1b92d3 Paul Brook
                               PCIConfigReadFunc *config_read,
275 6b1b92d3 Paul Brook
                               PCIConfigWriteFunc *config_write)
276 6b1b92d3 Paul Brook
{
277 6b1b92d3 Paul Brook
    PCIDevice *pci_dev;
278 6b1b92d3 Paul Brook
279 6b1b92d3 Paul Brook
    pci_dev = qemu_mallocz(instance_size);
280 6b1b92d3 Paul Brook
    pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
281 6b1b92d3 Paul Brook
                                     config_read, config_write);
282 6b1b92d3 Paul Brook
    return pci_dev;
283 6b1b92d3 Paul Brook
}
284 5851e08c aliguori
static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
285 5851e08c aliguori
{
286 5851e08c aliguori
    return addr + pci_mem_base;
287 5851e08c aliguori
}
288 5851e08c aliguori
289 5851e08c aliguori
static void pci_unregister_io_regions(PCIDevice *pci_dev)
290 5851e08c aliguori
{
291 5851e08c aliguori
    PCIIORegion *r;
292 5851e08c aliguori
    int i;
293 5851e08c aliguori
294 5851e08c aliguori
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
295 5851e08c aliguori
        r = &pci_dev->io_regions[i];
296 5851e08c aliguori
        if (!r->size || r->addr == -1)
297 5851e08c aliguori
            continue;
298 5851e08c aliguori
        if (r->type == PCI_ADDRESS_SPACE_IO) {
299 5851e08c aliguori
            isa_unassign_ioport(r->addr, r->size);
300 5851e08c aliguori
        } else {
301 5851e08c aliguori
            cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
302 5851e08c aliguori
                                                     r->size,
303 5851e08c aliguori
                                                     IO_MEM_UNASSIGNED);
304 5851e08c aliguori
        }
305 5851e08c aliguori
    }
306 5851e08c aliguori
}
307 5851e08c aliguori
308 5851e08c aliguori
int pci_unregister_device(PCIDevice *pci_dev)
309 5851e08c aliguori
{
310 5851e08c aliguori
    int ret = 0;
311 5851e08c aliguori
312 5851e08c aliguori
    if (pci_dev->unregister)
313 5851e08c aliguori
        ret = pci_dev->unregister(pci_dev);
314 5851e08c aliguori
    if (ret)
315 5851e08c aliguori
        return ret;
316 5851e08c aliguori
317 5851e08c aliguori
    pci_unregister_io_regions(pci_dev);
318 5851e08c aliguori
319 5851e08c aliguori
    qemu_free_irqs(pci_dev->irq);
320 5851e08c aliguori
    pci_dev->bus->devices[pci_dev->devfn] = NULL;
321 02e2da45 Paul Brook
    qdev_free(&pci_dev->qdev);
322 5851e08c aliguori
    return 0;
323 5851e08c aliguori
}
324 5851e08c aliguori
325 5fafdf24 ths
void pci_register_io_region(PCIDevice *pci_dev, int region_num,
326 5fafdf24 ths
                            uint32_t size, int type,
327 69b91039 bellard
                            PCIMapIORegionFunc *map_func)
328 69b91039 bellard
{
329 69b91039 bellard
    PCIIORegion *r;
330 d7ce493a pbrook
    uint32_t addr;
331 69b91039 bellard
332 8a8696a3 bellard
    if ((unsigned int)region_num >= PCI_NUM_REGIONS)
333 69b91039 bellard
        return;
334 a4c20c6a aliguori
335 a4c20c6a aliguori
    if (size & (size-1)) {
336 a4c20c6a aliguori
        fprintf(stderr, "ERROR: PCI region size must be pow2 "
337 a4c20c6a aliguori
                    "type=0x%x, size=0x%x\n", type, size);
338 a4c20c6a aliguori
        exit(1);
339 a4c20c6a aliguori
    }
340 a4c20c6a aliguori
341 69b91039 bellard
    r = &pci_dev->io_regions[region_num];
342 69b91039 bellard
    r->addr = -1;
343 69b91039 bellard
    r->size = size;
344 69b91039 bellard
    r->type = type;
345 69b91039 bellard
    r->map_func = map_func;
346 d7ce493a pbrook
    if (region_num == PCI_ROM_SLOT) {
347 d7ce493a pbrook
        addr = 0x30;
348 d7ce493a pbrook
    } else {
349 d7ce493a pbrook
        addr = 0x10 + region_num * 4;
350 d7ce493a pbrook
    }
351 d7ce493a pbrook
    *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
352 69b91039 bellard
}
353 69b91039 bellard
354 0ac32c83 bellard
static void pci_update_mappings(PCIDevice *d)
355 0ac32c83 bellard
{
356 0ac32c83 bellard
    PCIIORegion *r;
357 0ac32c83 bellard
    int cmd, i;
358 8a8696a3 bellard
    uint32_t last_addr, new_addr, config_ofs;
359 3b46e624 ths
360 0ac32c83 bellard
    cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
361 8a8696a3 bellard
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
362 0ac32c83 bellard
        r = &d->io_regions[i];
363 8a8696a3 bellard
        if (i == PCI_ROM_SLOT) {
364 8a8696a3 bellard
            config_ofs = 0x30;
365 8a8696a3 bellard
        } else {
366 8a8696a3 bellard
            config_ofs = 0x10 + i * 4;
367 8a8696a3 bellard
        }
368 0ac32c83 bellard
        if (r->size != 0) {
369 0ac32c83 bellard
            if (r->type & PCI_ADDRESS_SPACE_IO) {
370 0ac32c83 bellard
                if (cmd & PCI_COMMAND_IO) {
371 5fafdf24 ths
                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
372 8a8696a3 bellard
                                                         config_ofs));
373 0ac32c83 bellard
                    new_addr = new_addr & ~(r->size - 1);
374 0ac32c83 bellard
                    last_addr = new_addr + r->size - 1;
375 0ac32c83 bellard
                    /* NOTE: we have only 64K ioports on PC */
376 0ac32c83 bellard
                    if (last_addr <= new_addr || new_addr == 0 ||
377 0ac32c83 bellard
                        last_addr >= 0x10000) {
378 0ac32c83 bellard
                        new_addr = -1;
379 0ac32c83 bellard
                    }
380 0ac32c83 bellard
                } else {
381 0ac32c83 bellard
                    new_addr = -1;
382 0ac32c83 bellard
                }
383 0ac32c83 bellard
            } else {
384 0ac32c83 bellard
                if (cmd & PCI_COMMAND_MEMORY) {
385 5fafdf24 ths
                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
386 8a8696a3 bellard
                                                         config_ofs));
387 8a8696a3 bellard
                    /* the ROM slot has a specific enable bit */
388 8a8696a3 bellard
                    if (i == PCI_ROM_SLOT && !(new_addr & 1))
389 8a8696a3 bellard
                        goto no_mem_map;
390 0ac32c83 bellard
                    new_addr = new_addr & ~(r->size - 1);
391 0ac32c83 bellard
                    last_addr = new_addr + r->size - 1;
392 0ac32c83 bellard
                    /* NOTE: we do not support wrapping */
393 0ac32c83 bellard
                    /* XXX: as we cannot support really dynamic
394 0ac32c83 bellard
                       mappings, we handle specific values as invalid
395 0ac32c83 bellard
                       mappings. */
396 0ac32c83 bellard
                    if (last_addr <= new_addr || new_addr == 0 ||
397 0ac32c83 bellard
                        last_addr == -1) {
398 0ac32c83 bellard
                        new_addr = -1;
399 0ac32c83 bellard
                    }
400 0ac32c83 bellard
                } else {
401 8a8696a3 bellard
                no_mem_map:
402 0ac32c83 bellard
                    new_addr = -1;
403 0ac32c83 bellard
                }
404 0ac32c83 bellard
            }
405 0ac32c83 bellard
            /* now do the real mapping */
406 0ac32c83 bellard
            if (new_addr != r->addr) {
407 0ac32c83 bellard
                if (r->addr != -1) {
408 0ac32c83 bellard
                    if (r->type & PCI_ADDRESS_SPACE_IO) {
409 0ac32c83 bellard
                        int class;
410 0ac32c83 bellard
                        /* NOTE: specific hack for IDE in PC case:
411 0ac32c83 bellard
                           only one byte must be mapped. */
412 0ac32c83 bellard
                        class = d->config[0x0a] | (d->config[0x0b] << 8);
413 0ac32c83 bellard
                        if (class == 0x0101 && r->size == 4) {
414 0ac32c83 bellard
                            isa_unassign_ioport(r->addr + 2, 1);
415 0ac32c83 bellard
                        } else {
416 0ac32c83 bellard
                            isa_unassign_ioport(r->addr, r->size);
417 0ac32c83 bellard
                        }
418 0ac32c83 bellard
                    } else {
419 502a5395 pbrook
                        cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
420 5fafdf24 ths
                                                     r->size,
421 0ac32c83 bellard
                                                     IO_MEM_UNASSIGNED);
422 f65ed4c1 aliguori
                        qemu_unregister_coalesced_mmio(r->addr, r->size);
423 0ac32c83 bellard
                    }
424 0ac32c83 bellard
                }
425 0ac32c83 bellard
                r->addr = new_addr;
426 0ac32c83 bellard
                if (r->addr != -1) {
427 0ac32c83 bellard
                    r->map_func(d, i, r->addr, r->size, r->type);
428 0ac32c83 bellard
                }
429 0ac32c83 bellard
            }
430 0ac32c83 bellard
        }
431 0ac32c83 bellard
    }
432 0ac32c83 bellard
}
433 0ac32c83 bellard
434 5fafdf24 ths
uint32_t pci_default_read_config(PCIDevice *d,
435 0ac32c83 bellard
                                 uint32_t address, int len)
436 69b91039 bellard
{
437 0ac32c83 bellard
    uint32_t val;
438 a2d4e44b ths
439 0ac32c83 bellard
    switch(len) {
440 0ac32c83 bellard
    default:
441 0ac32c83 bellard
    case 4:
442 a2d4e44b ths
        if (address <= 0xfc) {
443 a2d4e44b ths
            val = le32_to_cpu(*(uint32_t *)(d->config + address));
444 a2d4e44b ths
            break;
445 a2d4e44b ths
        }
446 a2d4e44b ths
        /* fall through */
447 a2d4e44b ths
    case 2:
448 a2d4e44b ths
        if (address <= 0xfe) {
449 a2d4e44b ths
            val = le16_to_cpu(*(uint16_t *)(d->config + address));
450 a2d4e44b ths
            break;
451 a2d4e44b ths
        }
452 a2d4e44b ths
        /* fall through */
453 a2d4e44b ths
    case 1:
454 a2d4e44b ths
        val = d->config[address];
455 0ac32c83 bellard
        break;
456 0ac32c83 bellard
    }
457 0ac32c83 bellard
    return val;
458 0ac32c83 bellard
}
459 0ac32c83 bellard
460 5fafdf24 ths
void pci_default_write_config(PCIDevice *d,
461 0ac32c83 bellard
                              uint32_t address, uint32_t val, int len)
462 0ac32c83 bellard
{
463 0ac32c83 bellard
    int can_write, i;
464 7bf5be70 bellard
    uint32_t end, addr;
465 0ac32c83 bellard
466 5fafdf24 ths
    if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) ||
467 8a8696a3 bellard
                     (address >= 0x30 && address < 0x34))) {
468 0ac32c83 bellard
        PCIIORegion *r;
469 0ac32c83 bellard
        int reg;
470 0ac32c83 bellard
471 8a8696a3 bellard
        if ( address >= 0x30 ) {
472 8a8696a3 bellard
            reg = PCI_ROM_SLOT;
473 8a8696a3 bellard
        }else{
474 8a8696a3 bellard
            reg = (address - 0x10) >> 2;
475 8a8696a3 bellard
        }
476 0ac32c83 bellard
        r = &d->io_regions[reg];
477 0ac32c83 bellard
        if (r->size == 0)
478 0ac32c83 bellard
            goto default_config;
479 0ac32c83 bellard
        /* compute the stored value */
480 8a8696a3 bellard
        if (reg == PCI_ROM_SLOT) {
481 8a8696a3 bellard
            /* keep ROM enable bit */
482 8a8696a3 bellard
            val &= (~(r->size - 1)) | 1;
483 8a8696a3 bellard
        } else {
484 8a8696a3 bellard
            val &= ~(r->size - 1);
485 8a8696a3 bellard
            val |= r->type;
486 8a8696a3 bellard
        }
487 8a8696a3 bellard
        *(uint32_t *)(d->config + address) = cpu_to_le32(val);
488 0ac32c83 bellard
        pci_update_mappings(d);
489 69b91039 bellard
        return;
490 0ac32c83 bellard
    }
491 0ac32c83 bellard
 default_config:
492 0ac32c83 bellard
    /* not efficient, but simple */
493 7bf5be70 bellard
    addr = address;
494 0ac32c83 bellard
    for(i = 0; i < len; i++) {
495 0ac32c83 bellard
        /* default read/write accesses */
496 1f62d938 bellard
        switch(d->config[0x0e]) {
497 0ac32c83 bellard
        case 0x00:
498 1f62d938 bellard
        case 0x80:
499 1f62d938 bellard
            switch(addr) {
500 1f62d938 bellard
            case 0x00:
501 1f62d938 bellard
            case 0x01:
502 1f62d938 bellard
            case 0x02:
503 1f62d938 bellard
            case 0x03:
504 c2c5104b aliguori
            case 0x06:
505 c2c5104b aliguori
            case 0x07:
506 1f62d938 bellard
            case 0x08:
507 1f62d938 bellard
            case 0x09:
508 1f62d938 bellard
            case 0x0a:
509 1f62d938 bellard
            case 0x0b:
510 1f62d938 bellard
            case 0x0e:
511 1f62d938 bellard
            case 0x10 ... 0x27: /* base */
512 8098ed41 aurel32
            case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
513 1f62d938 bellard
            case 0x30 ... 0x33: /* rom */
514 1f62d938 bellard
            case 0x3d:
515 1f62d938 bellard
                can_write = 0;
516 1f62d938 bellard
                break;
517 1f62d938 bellard
            default:
518 1f62d938 bellard
                can_write = 1;
519 1f62d938 bellard
                break;
520 1f62d938 bellard
            }
521 0ac32c83 bellard
            break;
522 0ac32c83 bellard
        default:
523 1f62d938 bellard
        case 0x01:
524 1f62d938 bellard
            switch(addr) {
525 1f62d938 bellard
            case 0x00:
526 1f62d938 bellard
            case 0x01:
527 1f62d938 bellard
            case 0x02:
528 1f62d938 bellard
            case 0x03:
529 c2c5104b aliguori
            case 0x06:
530 c2c5104b aliguori
            case 0x07:
531 1f62d938 bellard
            case 0x08:
532 1f62d938 bellard
            case 0x09:
533 1f62d938 bellard
            case 0x0a:
534 1f62d938 bellard
            case 0x0b:
535 1f62d938 bellard
            case 0x0e:
536 8098ed41 aurel32
            case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
537 1f62d938 bellard
            case 0x38 ... 0x3b: /* rom */
538 1f62d938 bellard
            case 0x3d:
539 1f62d938 bellard
                can_write = 0;
540 1f62d938 bellard
                break;
541 1f62d938 bellard
            default:
542 1f62d938 bellard
                can_write = 1;
543 1f62d938 bellard
                break;
544 1f62d938 bellard
            }
545 0ac32c83 bellard
            break;
546 0ac32c83 bellard
        }
547 0ac32c83 bellard
        if (can_write) {
548 8098ed41 aurel32
            /* Mask out writes to reserved bits in registers */
549 8098ed41 aurel32
            switch (addr) {
550 475dc65f aurel32
            case 0x05:
551 475dc65f aurel32
                val &= ~PCI_COMMAND_RESERVED_MASK_HI;
552 475dc65f aurel32
                break;
553 8098ed41 aurel32
            case 0x06:
554 8098ed41 aurel32
                val &= ~PCI_STATUS_RESERVED_MASK_LO;
555 8098ed41 aurel32
                break;
556 8098ed41 aurel32
            case 0x07:
557 8098ed41 aurel32
                val &= ~PCI_STATUS_RESERVED_MASK_HI;
558 8098ed41 aurel32
                break;
559 8098ed41 aurel32
            }
560 7bf5be70 bellard
            d->config[addr] = val;
561 0ac32c83 bellard
        }
562 a2d4e44b ths
        if (++addr > 0xff)
563 a2d4e44b ths
                break;
564 0ac32c83 bellard
        val >>= 8;
565 0ac32c83 bellard
    }
566 0ac32c83 bellard
567 0ac32c83 bellard
    end = address + len;
568 0ac32c83 bellard
    if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
569 0ac32c83 bellard
        /* if the command register is modified, we must modify the mappings */
570 0ac32c83 bellard
        pci_update_mappings(d);
571 69b91039 bellard
    }
572 69b91039 bellard
}
573 69b91039 bellard
574 502a5395 pbrook
void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
575 69b91039 bellard
{
576 30468f78 bellard
    PCIBus *s = opaque;
577 30468f78 bellard
    PCIDevice *pci_dev;
578 30468f78 bellard
    int config_addr, bus_num;
579 3b46e624 ths
580 69b91039 bellard
#if defined(DEBUG_PCI) && 0
581 69b91039 bellard
    printf("pci_data_write: addr=%08x val=%08x len=%d\n",
582 502a5395 pbrook
           addr, val, len);
583 69b91039 bellard
#endif
584 502a5395 pbrook
    bus_num = (addr >> 16) & 0xff;
585 80b3ada7 pbrook
    while (s && s->bus_num != bus_num)
586 80b3ada7 pbrook
        s = s->next;
587 80b3ada7 pbrook
    if (!s)
588 69b91039 bellard
        return;
589 502a5395 pbrook
    pci_dev = s->devices[(addr >> 8) & 0xff];
590 69b91039 bellard
    if (!pci_dev)
591 69b91039 bellard
        return;
592 502a5395 pbrook
    config_addr = addr & 0xff;
593 69b91039 bellard
#if defined(DEBUG_PCI)
594 69b91039 bellard
    printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
595 69b91039 bellard
           pci_dev->name, config_addr, val, len);
596 69b91039 bellard
#endif
597 0ac32c83 bellard
    pci_dev->config_write(pci_dev, config_addr, val, len);
598 69b91039 bellard
}
599 69b91039 bellard
600 502a5395 pbrook
uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
601 69b91039 bellard
{
602 30468f78 bellard
    PCIBus *s = opaque;
603 30468f78 bellard
    PCIDevice *pci_dev;
604 30468f78 bellard
    int config_addr, bus_num;
605 69b91039 bellard
    uint32_t val;
606 69b91039 bellard
607 502a5395 pbrook
    bus_num = (addr >> 16) & 0xff;
608 80b3ada7 pbrook
    while (s && s->bus_num != bus_num)
609 80b3ada7 pbrook
        s= s->next;
610 80b3ada7 pbrook
    if (!s)
611 69b91039 bellard
        goto fail;
612 502a5395 pbrook
    pci_dev = s->devices[(addr >> 8) & 0xff];
613 69b91039 bellard
    if (!pci_dev) {
614 69b91039 bellard
    fail:
615 63ce9e0a bellard
        switch(len) {
616 63ce9e0a bellard
        case 1:
617 63ce9e0a bellard
            val = 0xff;
618 63ce9e0a bellard
            break;
619 63ce9e0a bellard
        case 2:
620 63ce9e0a bellard
            val = 0xffff;
621 63ce9e0a bellard
            break;
622 63ce9e0a bellard
        default:
623 63ce9e0a bellard
        case 4:
624 63ce9e0a bellard
            val = 0xffffffff;
625 63ce9e0a bellard
            break;
626 63ce9e0a bellard
        }
627 69b91039 bellard
        goto the_end;
628 69b91039 bellard
    }
629 502a5395 pbrook
    config_addr = addr & 0xff;
630 69b91039 bellard
    val = pci_dev->config_read(pci_dev, config_addr, len);
631 69b91039 bellard
#if defined(DEBUG_PCI)
632 69b91039 bellard
    printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
633 69b91039 bellard
           pci_dev->name, config_addr, val, len);
634 69b91039 bellard
#endif
635 69b91039 bellard
 the_end:
636 69b91039 bellard
#if defined(DEBUG_PCI) && 0
637 69b91039 bellard
    printf("pci_data_read: addr=%08x val=%08x len=%d\n",
638 502a5395 pbrook
           addr, val, len);
639 69b91039 bellard
#endif
640 69b91039 bellard
    return val;
641 69b91039 bellard
}
642 69b91039 bellard
643 502a5395 pbrook
/***********************************************************/
644 502a5395 pbrook
/* generic PCI irq support */
645 30468f78 bellard
646 502a5395 pbrook
/* 0 <= irq_num <= 3. level must be 0 or 1 */
647 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level)
648 69b91039 bellard
{
649 d537cf6c pbrook
    PCIDevice *pci_dev = (PCIDevice *)opaque;
650 80b3ada7 pbrook
    PCIBus *bus;
651 80b3ada7 pbrook
    int change;
652 3b46e624 ths
653 80b3ada7 pbrook
    change = level - pci_dev->irq_state[irq_num];
654 80b3ada7 pbrook
    if (!change)
655 80b3ada7 pbrook
        return;
656 d2b59317 pbrook
657 d2b59317 pbrook
    pci_dev->irq_state[irq_num] = level;
658 5e966ce6 pbrook
    for (;;) {
659 5e966ce6 pbrook
        bus = pci_dev->bus;
660 80b3ada7 pbrook
        irq_num = bus->map_irq(pci_dev, irq_num);
661 5e966ce6 pbrook
        if (bus->set_irq)
662 5e966ce6 pbrook
            break;
663 80b3ada7 pbrook
        pci_dev = bus->parent_dev;
664 80b3ada7 pbrook
    }
665 80b3ada7 pbrook
    bus->irq_count[irq_num] += change;
666 d2b59317 pbrook
    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
667 69b91039 bellard
}
668 69b91039 bellard
669 502a5395 pbrook
/***********************************************************/
670 502a5395 pbrook
/* monitor info on PCI */
671 0ac32c83 bellard
672 6650ee6d pbrook
typedef struct {
673 6650ee6d pbrook
    uint16_t class;
674 6650ee6d pbrook
    const char *desc;
675 6650ee6d pbrook
} pci_class_desc;
676 6650ee6d pbrook
677 09bc878a blueswir1
static const pci_class_desc pci_class_descriptions[] =
678 6650ee6d pbrook
{
679 4ca9c76f pbrook
    { 0x0100, "SCSI controller"},
680 6650ee6d pbrook
    { 0x0101, "IDE controller"},
681 dcb5b19a ths
    { 0x0102, "Floppy controller"},
682 dcb5b19a ths
    { 0x0103, "IPI controller"},
683 dcb5b19a ths
    { 0x0104, "RAID controller"},
684 dcb5b19a ths
    { 0x0106, "SATA controller"},
685 dcb5b19a ths
    { 0x0107, "SAS controller"},
686 dcb5b19a ths
    { 0x0180, "Storage controller"},
687 6650ee6d pbrook
    { 0x0200, "Ethernet controller"},
688 dcb5b19a ths
    { 0x0201, "Token Ring controller"},
689 dcb5b19a ths
    { 0x0202, "FDDI controller"},
690 dcb5b19a ths
    { 0x0203, "ATM controller"},
691 dcb5b19a ths
    { 0x0280, "Network controller"},
692 6650ee6d pbrook
    { 0x0300, "VGA controller"},
693 dcb5b19a ths
    { 0x0301, "XGA controller"},
694 dcb5b19a ths
    { 0x0302, "3D controller"},
695 dcb5b19a ths
    { 0x0380, "Display controller"},
696 dcb5b19a ths
    { 0x0400, "Video controller"},
697 dcb5b19a ths
    { 0x0401, "Audio controller"},
698 dcb5b19a ths
    { 0x0402, "Phone"},
699 dcb5b19a ths
    { 0x0480, "Multimedia controller"},
700 dcb5b19a ths
    { 0x0500, "RAM controller"},
701 dcb5b19a ths
    { 0x0501, "Flash controller"},
702 dcb5b19a ths
    { 0x0580, "Memory controller"},
703 6650ee6d pbrook
    { 0x0600, "Host bridge"},
704 6650ee6d pbrook
    { 0x0601, "ISA bridge"},
705 dcb5b19a ths
    { 0x0602, "EISA bridge"},
706 dcb5b19a ths
    { 0x0603, "MC bridge"},
707 6650ee6d pbrook
    { 0x0604, "PCI bridge"},
708 dcb5b19a ths
    { 0x0605, "PCMCIA bridge"},
709 dcb5b19a ths
    { 0x0606, "NUBUS bridge"},
710 dcb5b19a ths
    { 0x0607, "CARDBUS bridge"},
711 dcb5b19a ths
    { 0x0608, "RACEWAY bridge"},
712 dcb5b19a ths
    { 0x0680, "Bridge"},
713 6650ee6d pbrook
    { 0x0c03, "USB controller"},
714 6650ee6d pbrook
    { 0, NULL}
715 6650ee6d pbrook
};
716 6650ee6d pbrook
717 502a5395 pbrook
static void pci_info_device(PCIDevice *d)
718 30468f78 bellard
{
719 376253ec aliguori
    Monitor *mon = cur_mon;
720 502a5395 pbrook
    int i, class;
721 502a5395 pbrook
    PCIIORegion *r;
722 09bc878a blueswir1
    const pci_class_desc *desc;
723 30468f78 bellard
724 376253ec aliguori
    monitor_printf(mon, "  Bus %2d, device %3d, function %d:\n",
725 376253ec aliguori
                   d->bus->bus_num, d->devfn >> 3, d->devfn & 7);
726 502a5395 pbrook
    class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
727 376253ec aliguori
    monitor_printf(mon, "    ");
728 6650ee6d pbrook
    desc = pci_class_descriptions;
729 6650ee6d pbrook
    while (desc->desc && class != desc->class)
730 6650ee6d pbrook
        desc++;
731 6650ee6d pbrook
    if (desc->desc) {
732 376253ec aliguori
        monitor_printf(mon, "%s", desc->desc);
733 6650ee6d pbrook
    } else {
734 376253ec aliguori
        monitor_printf(mon, "Class %04x", class);
735 72cc6cfe bellard
    }
736 376253ec aliguori
    monitor_printf(mon, ": PCI device %04x:%04x\n",
737 502a5395 pbrook
           le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
738 502a5395 pbrook
           le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
739 30468f78 bellard
740 502a5395 pbrook
    if (d->config[PCI_INTERRUPT_PIN] != 0) {
741 376253ec aliguori
        monitor_printf(mon, "      IRQ %d.\n",
742 376253ec aliguori
                       d->config[PCI_INTERRUPT_LINE]);
743 30468f78 bellard
    }
744 80b3ada7 pbrook
    if (class == 0x0604) {
745 376253ec aliguori
        monitor_printf(mon, "      BUS %d.\n", d->config[0x19]);
746 80b3ada7 pbrook
    }
747 502a5395 pbrook
    for(i = 0;i < PCI_NUM_REGIONS; i++) {
748 502a5395 pbrook
        r = &d->io_regions[i];
749 502a5395 pbrook
        if (r->size != 0) {
750 376253ec aliguori
            monitor_printf(mon, "      BAR%d: ", i);
751 502a5395 pbrook
            if (r->type & PCI_ADDRESS_SPACE_IO) {
752 376253ec aliguori
                monitor_printf(mon, "I/O at 0x%04x [0x%04x].\n",
753 376253ec aliguori
                               r->addr, r->addr + r->size - 1);
754 502a5395 pbrook
            } else {
755 376253ec aliguori
                monitor_printf(mon, "32 bit memory at 0x%08x [0x%08x].\n",
756 376253ec aliguori
                               r->addr, r->addr + r->size - 1);
757 502a5395 pbrook
            }
758 502a5395 pbrook
        }
759 77d4bc34 bellard
    }
760 80b3ada7 pbrook
    if (class == 0x0604 && d->config[0x19] != 0) {
761 80b3ada7 pbrook
        pci_for_each_device(d->config[0x19], pci_info_device);
762 80b3ada7 pbrook
    }
763 384d8876 bellard
}
764 384d8876 bellard
765 80b3ada7 pbrook
void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d))
766 384d8876 bellard
{
767 502a5395 pbrook
    PCIBus *bus = first_bus;
768 384d8876 bellard
    PCIDevice *d;
769 502a5395 pbrook
    int devfn;
770 3b46e624 ths
771 80b3ada7 pbrook
    while (bus && bus->bus_num != bus_num)
772 80b3ada7 pbrook
        bus = bus->next;
773 502a5395 pbrook
    if (bus) {
774 502a5395 pbrook
        for(devfn = 0; devfn < 256; devfn++) {
775 502a5395 pbrook
            d = bus->devices[devfn];
776 502a5395 pbrook
            if (d)
777 502a5395 pbrook
                fn(d);
778 502a5395 pbrook
        }
779 f2aa58c6 bellard
    }
780 f2aa58c6 bellard
}
781 f2aa58c6 bellard
782 376253ec aliguori
void pci_info(Monitor *mon)
783 f2aa58c6 bellard
{
784 80b3ada7 pbrook
    pci_for_each_device(0, pci_info_device);
785 77d4bc34 bellard
}
786 a41b2ff2 pbrook
787 cb457d76 aliguori
static const char * const pci_nic_models[] = {
788 cb457d76 aliguori
    "ne2k_pci",
789 cb457d76 aliguori
    "i82551",
790 cb457d76 aliguori
    "i82557b",
791 cb457d76 aliguori
    "i82559er",
792 cb457d76 aliguori
    "rtl8139",
793 cb457d76 aliguori
    "e1000",
794 cb457d76 aliguori
    "pcnet",
795 cb457d76 aliguori
    "virtio",
796 cb457d76 aliguori
    NULL
797 cb457d76 aliguori
};
798 cb457d76 aliguori
799 9d07d757 Paul Brook
static const char * const pci_nic_names[] = {
800 9d07d757 Paul Brook
    "ne2k_pci",
801 9d07d757 Paul Brook
    "i82551",
802 9d07d757 Paul Brook
    "i82557b",
803 9d07d757 Paul Brook
    "i82559er",
804 9d07d757 Paul Brook
    "rtl8139",
805 9d07d757 Paul Brook
    "e1000",
806 9d07d757 Paul Brook
    "pcnet",
807 53c25cea Paul Brook
    "virtio-net-pci",
808 cb457d76 aliguori
    NULL
809 cb457d76 aliguori
};
810 cb457d76 aliguori
811 a41b2ff2 pbrook
/* Initialize a PCI NIC.  */
812 72da4208 aliguori
PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
813 cb457d76 aliguori
                  const char *default_model)
814 a41b2ff2 pbrook
{
815 9d07d757 Paul Brook
    DeviceState *dev;
816 cb457d76 aliguori
    int i;
817 cb457d76 aliguori
818 cb457d76 aliguori
    qemu_check_nic_model_list(nd, pci_nic_models, default_model);
819 cb457d76 aliguori
820 9d07d757 Paul Brook
    for (i = 0; pci_nic_models[i]; i++) {
821 72da4208 aliguori
        if (strcmp(nd->model, pci_nic_models[i]) == 0) {
822 02e2da45 Paul Brook
            dev = qdev_create(&bus->qbus, pci_nic_names[i]);
823 9d07d757 Paul Brook
            qdev_set_prop_int(dev, "devfn", devfn);
824 9d07d757 Paul Brook
            qdev_set_netdev(dev, nd);
825 9d07d757 Paul Brook
            qdev_init(dev);
826 9d07d757 Paul Brook
            nd->private = dev;
827 9d07d757 Paul Brook
            return (PCIDevice *)dev;
828 72da4208 aliguori
        }
829 9d07d757 Paul Brook
    }
830 72da4208 aliguori
831 72da4208 aliguori
    return NULL;
832 a41b2ff2 pbrook
}
833 a41b2ff2 pbrook
834 80b3ada7 pbrook
typedef struct {
835 80b3ada7 pbrook
    PCIDevice dev;
836 80b3ada7 pbrook
    PCIBus *bus;
837 80b3ada7 pbrook
} PCIBridge;
838 80b3ada7 pbrook
839 9596ebb7 pbrook
static void pci_bridge_write_config(PCIDevice *d,
840 80b3ada7 pbrook
                             uint32_t address, uint32_t val, int len)
841 80b3ada7 pbrook
{
842 80b3ada7 pbrook
    PCIBridge *s = (PCIBridge *)d;
843 80b3ada7 pbrook
844 80b3ada7 pbrook
    if (address == 0x19 || (address == 0x18 && len > 1)) {
845 80b3ada7 pbrook
        if (address == 0x19)
846 80b3ada7 pbrook
            s->bus->bus_num = val & 0xff;
847 80b3ada7 pbrook
        else
848 80b3ada7 pbrook
            s->bus->bus_num = (val >> 8) & 0xff;
849 80b3ada7 pbrook
#if defined(DEBUG_PCI)
850 80b3ada7 pbrook
        printf ("pci-bridge: %s: Assigned bus %d\n", d->name, s->bus->bus_num);
851 80b3ada7 pbrook
#endif
852 80b3ada7 pbrook
    }
853 80b3ada7 pbrook
    pci_default_write_config(d, address, val, len);
854 80b3ada7 pbrook
}
855 80b3ada7 pbrook
856 3ae80618 aliguori
PCIBus *pci_find_bus(int bus_num)
857 3ae80618 aliguori
{
858 3ae80618 aliguori
    PCIBus *bus = first_bus;
859 3ae80618 aliguori
860 3ae80618 aliguori
    while (bus && bus->bus_num != bus_num)
861 3ae80618 aliguori
        bus = bus->next;
862 3ae80618 aliguori
863 3ae80618 aliguori
    return bus;
864 3ae80618 aliguori
}
865 3ae80618 aliguori
866 3ae80618 aliguori
PCIDevice *pci_find_device(int bus_num, int slot, int function)
867 3ae80618 aliguori
{
868 3ae80618 aliguori
    PCIBus *bus = pci_find_bus(bus_num);
869 3ae80618 aliguori
870 3ae80618 aliguori
    if (!bus)
871 3ae80618 aliguori
        return NULL;
872 3ae80618 aliguori
873 3ae80618 aliguori
    return bus->devices[PCI_DEVFN(slot, function)];
874 3ae80618 aliguori
}
875 3ae80618 aliguori
876 480b9f24 blueswir1
PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
877 80b3ada7 pbrook
                        pci_map_irq_fn map_irq, const char *name)
878 80b3ada7 pbrook
{
879 80b3ada7 pbrook
    PCIBridge *s;
880 5fafdf24 ths
    s = (PCIBridge *)pci_register_device(bus, name, sizeof(PCIBridge),
881 80b3ada7 pbrook
                                         devfn, NULL, pci_bridge_write_config);
882 480b9f24 blueswir1
883 480b9f24 blueswir1
    pci_config_set_vendor_id(s->dev.config, vid);
884 480b9f24 blueswir1
    pci_config_set_device_id(s->dev.config, did);
885 480b9f24 blueswir1
886 80b3ada7 pbrook
    s->dev.config[0x04] = 0x06; // command = bus master, pci mem
887 80b3ada7 pbrook
    s->dev.config[0x05] = 0x00;
888 80b3ada7 pbrook
    s->dev.config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
889 80b3ada7 pbrook
    s->dev.config[0x07] = 0x00; // status = fast devsel
890 80b3ada7 pbrook
    s->dev.config[0x08] = 0x00; // revision
891 80b3ada7 pbrook
    s->dev.config[0x09] = 0x00; // programming i/f
892 173a543b blueswir1
    pci_config_set_class(s->dev.config, PCI_CLASS_BRIDGE_PCI);
893 80b3ada7 pbrook
    s->dev.config[0x0D] = 0x10; // latency_timer
894 6407f373 Isaku Yamahata
    s->dev.config[PCI_HEADER_TYPE] =
895 6407f373 Isaku Yamahata
        PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE; // header_type
896 80b3ada7 pbrook
    s->dev.config[0x1E] = 0xa0; // secondary status
897 80b3ada7 pbrook
898 80b3ada7 pbrook
    s->bus = pci_register_secondary_bus(&s->dev, map_irq);
899 80b3ada7 pbrook
    return s->bus;
900 80b3ada7 pbrook
}
901 6b1b92d3 Paul Brook
902 02e2da45 Paul Brook
typedef struct {
903 02e2da45 Paul Brook
    DeviceInfo qdev;
904 02e2da45 Paul Brook
    pci_qdev_initfn init;
905 02e2da45 Paul Brook
} PCIDeviceInfo;
906 02e2da45 Paul Brook
907 02e2da45 Paul Brook
static void pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
908 6b1b92d3 Paul Brook
{
909 6b1b92d3 Paul Brook
    PCIDevice *pci_dev = (PCIDevice *)qdev;
910 02e2da45 Paul Brook
    PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
911 6b1b92d3 Paul Brook
    PCIBus *bus;
912 6b1b92d3 Paul Brook
    int devfn;
913 6b1b92d3 Paul Brook
914 02e2da45 Paul Brook
    bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
915 6b1b92d3 Paul Brook
    devfn = qdev_get_prop_int(qdev, "devfn", -1);
916 6b1b92d3 Paul Brook
    pci_dev = do_pci_register_device(pci_dev, bus, "FIXME", devfn,
917 6b1b92d3 Paul Brook
                                     NULL, NULL);//FIXME:config_read, config_write);
918 6b1b92d3 Paul Brook
    assert(pci_dev);
919 02e2da45 Paul Brook
    info->init(pci_dev);
920 6b1b92d3 Paul Brook
}
921 6b1b92d3 Paul Brook
922 6b1b92d3 Paul Brook
void pci_qdev_register(const char *name, int size, pci_qdev_initfn init)
923 6b1b92d3 Paul Brook
{
924 02e2da45 Paul Brook
    PCIDeviceInfo *info;
925 02e2da45 Paul Brook
926 02e2da45 Paul Brook
    info = qemu_mallocz(sizeof(*info));
927 074f2fff Gerd Hoffmann
    info->qdev.name = qemu_strdup(name);
928 074f2fff Gerd Hoffmann
    info->qdev.size = size;
929 02e2da45 Paul Brook
    info->init = init;
930 02e2da45 Paul Brook
    info->qdev.init = pci_qdev_init;
931 02e2da45 Paul Brook
    info->qdev.bus_type = BUS_TYPE_PCI;
932 02e2da45 Paul Brook
933 074f2fff Gerd Hoffmann
    qdev_register(&info->qdev);
934 6b1b92d3 Paul Brook
}
935 6b1b92d3 Paul Brook
936 6b1b92d3 Paul Brook
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
937 6b1b92d3 Paul Brook
{
938 6b1b92d3 Paul Brook
    DeviceState *dev;
939 6b1b92d3 Paul Brook
940 02e2da45 Paul Brook
    dev = qdev_create(&bus->qbus, name);
941 6b1b92d3 Paul Brook
    qdev_set_prop_int(dev, "devfn", devfn);
942 6b1b92d3 Paul Brook
    qdev_init(dev);
943 6b1b92d3 Paul Brook
944 6b1b92d3 Paul Brook
    return (PCIDevice *)dev;
945 6b1b92d3 Paul Brook
}