Statistics
| Branch: | Revision:

root / hw / pci.c @ ee118d95

History | View | Annotate | Download (26.1 kB)

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