Statistics
| Branch: | Revision:

root / hw / pci.c @ 905fdcb5

History | View | Annotate | Download (20.2 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 87ecb68b pbrook
#include "console.h"
27 87ecb68b pbrook
#include "net.h"
28 69b91039 bellard
29 69b91039 bellard
//#define DEBUG_PCI
30 69b91039 bellard
31 30468f78 bellard
struct PCIBus {
32 30468f78 bellard
    int bus_num;
33 30468f78 bellard
    int devfn_min;
34 502a5395 pbrook
    pci_set_irq_fn set_irq;
35 d2b59317 pbrook
    pci_map_irq_fn map_irq;
36 30468f78 bellard
    uint32_t config_reg; /* XXX: suppress */
37 384d8876 bellard
    /* low level pic */
38 384d8876 bellard
    SetIRQFunc *low_set_irq;
39 d537cf6c pbrook
    qemu_irq *irq_opaque;
40 30468f78 bellard
    PCIDevice *devices[256];
41 80b3ada7 pbrook
    PCIDevice *parent_dev;
42 80b3ada7 pbrook
    PCIBus *next;
43 d2b59317 pbrook
    /* The bus IRQ state is the logical OR of the connected devices.
44 d2b59317 pbrook
       Keep a count of the number of devices with raised IRQs.  */
45 52fc1d83 balrog
    int nirq;
46 80b3ada7 pbrook
    int irq_count[];
47 30468f78 bellard
};
48 69b91039 bellard
49 1941d19c bellard
static void pci_update_mappings(PCIDevice *d);
50 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level);
51 1941d19c bellard
52 69b91039 bellard
target_phys_addr_t pci_mem_base;
53 0ac32c83 bellard
static int pci_irq_index;
54 30468f78 bellard
static PCIBus *first_bus;
55 30468f78 bellard
56 52fc1d83 balrog
static void pcibus_save(QEMUFile *f, void *opaque)
57 52fc1d83 balrog
{
58 52fc1d83 balrog
    PCIBus *bus = (PCIBus *)opaque;
59 52fc1d83 balrog
    int i;
60 52fc1d83 balrog
61 52fc1d83 balrog
    qemu_put_be32(f, bus->nirq);
62 52fc1d83 balrog
    for (i = 0; i < bus->nirq; i++)
63 52fc1d83 balrog
        qemu_put_be32(f, bus->irq_count[i]);
64 52fc1d83 balrog
}
65 52fc1d83 balrog
66 52fc1d83 balrog
static int  pcibus_load(QEMUFile *f, void *opaque, int version_id)
67 52fc1d83 balrog
{
68 52fc1d83 balrog
    PCIBus *bus = (PCIBus *)opaque;
69 52fc1d83 balrog
    int i, nirq;
70 52fc1d83 balrog
71 52fc1d83 balrog
    if (version_id != 1)
72 52fc1d83 balrog
        return -EINVAL;
73 52fc1d83 balrog
74 52fc1d83 balrog
    nirq = qemu_get_be32(f);
75 52fc1d83 balrog
    if (bus->nirq != nirq) {
76 52fc1d83 balrog
        fprintf(stderr, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
77 52fc1d83 balrog
                nirq, bus->nirq);
78 52fc1d83 balrog
        return -EINVAL;
79 52fc1d83 balrog
    }
80 52fc1d83 balrog
81 52fc1d83 balrog
    for (i = 0; i < nirq; i++)
82 52fc1d83 balrog
        bus->irq_count[i] = qemu_get_be32(f);
83 52fc1d83 balrog
84 52fc1d83 balrog
    return 0;
85 52fc1d83 balrog
}
86 52fc1d83 balrog
87 d2b59317 pbrook
PCIBus *pci_register_bus(pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
88 d537cf6c pbrook
                         qemu_irq *pic, int devfn_min, int nirq)
89 30468f78 bellard
{
90 30468f78 bellard
    PCIBus *bus;
91 52fc1d83 balrog
    static int nbus = 0;
92 52fc1d83 balrog
93 80b3ada7 pbrook
    bus = qemu_mallocz(sizeof(PCIBus) + (nirq * sizeof(int)));
94 502a5395 pbrook
    bus->set_irq = set_irq;
95 d2b59317 pbrook
    bus->map_irq = map_irq;
96 502a5395 pbrook
    bus->irq_opaque = pic;
97 502a5395 pbrook
    bus->devfn_min = devfn_min;
98 52fc1d83 balrog
    bus->nirq = nirq;
99 30468f78 bellard
    first_bus = bus;
100 52fc1d83 balrog
    register_savevm("PCIBUS", nbus++, 1, pcibus_save, pcibus_load, bus);
101 30468f78 bellard
    return bus;
102 30468f78 bellard
}
103 69b91039 bellard
104 9596ebb7 pbrook
static PCIBus *pci_register_secondary_bus(PCIDevice *dev, pci_map_irq_fn map_irq)
105 80b3ada7 pbrook
{
106 80b3ada7 pbrook
    PCIBus *bus;
107 80b3ada7 pbrook
    bus = qemu_mallocz(sizeof(PCIBus));
108 80b3ada7 pbrook
    bus->map_irq = map_irq;
109 80b3ada7 pbrook
    bus->parent_dev = dev;
110 80b3ada7 pbrook
    bus->next = dev->bus->next;
111 80b3ada7 pbrook
    dev->bus->next = bus;
112 80b3ada7 pbrook
    return bus;
113 80b3ada7 pbrook
}
114 80b3ada7 pbrook
115 502a5395 pbrook
int pci_bus_num(PCIBus *s)
116 502a5395 pbrook
{
117 502a5395 pbrook
    return s->bus_num;
118 502a5395 pbrook
}
119 502a5395 pbrook
120 1941d19c bellard
void pci_device_save(PCIDevice *s, QEMUFile *f)
121 30ca2aab bellard
{
122 52fc1d83 balrog
    int i;
123 52fc1d83 balrog
124 52fc1d83 balrog
    qemu_put_be32(f, 2); /* PCI device version */
125 30ca2aab bellard
    qemu_put_buffer(f, s->config, 256);
126 52fc1d83 balrog
    for (i = 0; i < 4; i++)
127 52fc1d83 balrog
        qemu_put_be32(f, s->irq_state[i]);
128 30ca2aab bellard
}
129 30ca2aab bellard
130 1941d19c bellard
int pci_device_load(PCIDevice *s, QEMUFile *f)
131 30ca2aab bellard
{
132 1941d19c bellard
    uint32_t version_id;
133 52fc1d83 balrog
    int i;
134 52fc1d83 balrog
135 1941d19c bellard
    version_id = qemu_get_be32(f);
136 52fc1d83 balrog
    if (version_id > 2)
137 30ca2aab bellard
        return -EINVAL;
138 30ca2aab bellard
    qemu_get_buffer(f, s->config, 256);
139 1941d19c bellard
    pci_update_mappings(s);
140 52fc1d83 balrog
141 52fc1d83 balrog
    if (version_id >= 2)
142 52fc1d83 balrog
        for (i = 0; i < 4; i ++)
143 52fc1d83 balrog
            s->irq_state[i] = qemu_get_be32(f);
144 52fc1d83 balrog
145 30ca2aab bellard
    return 0;
146 30ca2aab bellard
}
147 30ca2aab bellard
148 69b91039 bellard
/* -1 for devfn means auto assign */
149 5fafdf24 ths
PCIDevice *pci_register_device(PCIBus *bus, const char *name,
150 30468f78 bellard
                               int instance_size, int devfn,
151 5fafdf24 ths
                               PCIConfigReadFunc *config_read,
152 69b91039 bellard
                               PCIConfigWriteFunc *config_write)
153 69b91039 bellard
{
154 30468f78 bellard
    PCIDevice *pci_dev;
155 69b91039 bellard
156 0ac32c83 bellard
    if (pci_irq_index >= PCI_DEVICES_MAX)
157 0ac32c83 bellard
        return NULL;
158 3b46e624 ths
159 69b91039 bellard
    if (devfn < 0) {
160 30468f78 bellard
        for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
161 30468f78 bellard
            if (!bus->devices[devfn])
162 69b91039 bellard
                goto found;
163 69b91039 bellard
        }
164 69b91039 bellard
        return NULL;
165 69b91039 bellard
    found: ;
166 69b91039 bellard
    }
167 69b91039 bellard
    pci_dev = qemu_mallocz(instance_size);
168 69b91039 bellard
    if (!pci_dev)
169 69b91039 bellard
        return NULL;
170 30468f78 bellard
    pci_dev->bus = bus;
171 69b91039 bellard
    pci_dev->devfn = devfn;
172 69b91039 bellard
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
173 d2b59317 pbrook
    memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
174 0ac32c83 bellard
175 0ac32c83 bellard
    if (!config_read)
176 0ac32c83 bellard
        config_read = pci_default_read_config;
177 0ac32c83 bellard
    if (!config_write)
178 0ac32c83 bellard
        config_write = pci_default_write_config;
179 69b91039 bellard
    pci_dev->config_read = config_read;
180 69b91039 bellard
    pci_dev->config_write = config_write;
181 0ac32c83 bellard
    pci_dev->irq_index = pci_irq_index++;
182 30468f78 bellard
    bus->devices[devfn] = pci_dev;
183 d537cf6c pbrook
    pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, 4);
184 69b91039 bellard
    return pci_dev;
185 69b91039 bellard
}
186 69b91039 bellard
187 5fafdf24 ths
void pci_register_io_region(PCIDevice *pci_dev, int region_num,
188 5fafdf24 ths
                            uint32_t size, int type,
189 69b91039 bellard
                            PCIMapIORegionFunc *map_func)
190 69b91039 bellard
{
191 69b91039 bellard
    PCIIORegion *r;
192 d7ce493a pbrook
    uint32_t addr;
193 69b91039 bellard
194 8a8696a3 bellard
    if ((unsigned int)region_num >= PCI_NUM_REGIONS)
195 69b91039 bellard
        return;
196 69b91039 bellard
    r = &pci_dev->io_regions[region_num];
197 69b91039 bellard
    r->addr = -1;
198 69b91039 bellard
    r->size = size;
199 69b91039 bellard
    r->type = type;
200 69b91039 bellard
    r->map_func = map_func;
201 d7ce493a pbrook
    if (region_num == PCI_ROM_SLOT) {
202 d7ce493a pbrook
        addr = 0x30;
203 d7ce493a pbrook
    } else {
204 d7ce493a pbrook
        addr = 0x10 + region_num * 4;
205 d7ce493a pbrook
    }
206 d7ce493a pbrook
    *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
207 69b91039 bellard
}
208 69b91039 bellard
209 9596ebb7 pbrook
static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
210 69b91039 bellard
{
211 502a5395 pbrook
    return addr + pci_mem_base;
212 69b91039 bellard
}
213 69b91039 bellard
214 0ac32c83 bellard
static void pci_update_mappings(PCIDevice *d)
215 0ac32c83 bellard
{
216 0ac32c83 bellard
    PCIIORegion *r;
217 0ac32c83 bellard
    int cmd, i;
218 8a8696a3 bellard
    uint32_t last_addr, new_addr, config_ofs;
219 3b46e624 ths
220 0ac32c83 bellard
    cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
221 8a8696a3 bellard
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
222 0ac32c83 bellard
        r = &d->io_regions[i];
223 8a8696a3 bellard
        if (i == PCI_ROM_SLOT) {
224 8a8696a3 bellard
            config_ofs = 0x30;
225 8a8696a3 bellard
        } else {
226 8a8696a3 bellard
            config_ofs = 0x10 + i * 4;
227 8a8696a3 bellard
        }
228 0ac32c83 bellard
        if (r->size != 0) {
229 0ac32c83 bellard
            if (r->type & PCI_ADDRESS_SPACE_IO) {
230 0ac32c83 bellard
                if (cmd & PCI_COMMAND_IO) {
231 5fafdf24 ths
                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
232 8a8696a3 bellard
                                                         config_ofs));
233 0ac32c83 bellard
                    new_addr = new_addr & ~(r->size - 1);
234 0ac32c83 bellard
                    last_addr = new_addr + r->size - 1;
235 0ac32c83 bellard
                    /* NOTE: we have only 64K ioports on PC */
236 0ac32c83 bellard
                    if (last_addr <= new_addr || new_addr == 0 ||
237 0ac32c83 bellard
                        last_addr >= 0x10000) {
238 0ac32c83 bellard
                        new_addr = -1;
239 0ac32c83 bellard
                    }
240 0ac32c83 bellard
                } else {
241 0ac32c83 bellard
                    new_addr = -1;
242 0ac32c83 bellard
                }
243 0ac32c83 bellard
            } else {
244 0ac32c83 bellard
                if (cmd & PCI_COMMAND_MEMORY) {
245 5fafdf24 ths
                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
246 8a8696a3 bellard
                                                         config_ofs));
247 8a8696a3 bellard
                    /* the ROM slot has a specific enable bit */
248 8a8696a3 bellard
                    if (i == PCI_ROM_SLOT && !(new_addr & 1))
249 8a8696a3 bellard
                        goto no_mem_map;
250 0ac32c83 bellard
                    new_addr = new_addr & ~(r->size - 1);
251 0ac32c83 bellard
                    last_addr = new_addr + r->size - 1;
252 0ac32c83 bellard
                    /* NOTE: we do not support wrapping */
253 0ac32c83 bellard
                    /* XXX: as we cannot support really dynamic
254 0ac32c83 bellard
                       mappings, we handle specific values as invalid
255 0ac32c83 bellard
                       mappings. */
256 0ac32c83 bellard
                    if (last_addr <= new_addr || new_addr == 0 ||
257 0ac32c83 bellard
                        last_addr == -1) {
258 0ac32c83 bellard
                        new_addr = -1;
259 0ac32c83 bellard
                    }
260 0ac32c83 bellard
                } else {
261 8a8696a3 bellard
                no_mem_map:
262 0ac32c83 bellard
                    new_addr = -1;
263 0ac32c83 bellard
                }
264 0ac32c83 bellard
            }
265 0ac32c83 bellard
            /* now do the real mapping */
266 0ac32c83 bellard
            if (new_addr != r->addr) {
267 0ac32c83 bellard
                if (r->addr != -1) {
268 0ac32c83 bellard
                    if (r->type & PCI_ADDRESS_SPACE_IO) {
269 0ac32c83 bellard
                        int class;
270 0ac32c83 bellard
                        /* NOTE: specific hack for IDE in PC case:
271 0ac32c83 bellard
                           only one byte must be mapped. */
272 0ac32c83 bellard
                        class = d->config[0x0a] | (d->config[0x0b] << 8);
273 0ac32c83 bellard
                        if (class == 0x0101 && r->size == 4) {
274 0ac32c83 bellard
                            isa_unassign_ioport(r->addr + 2, 1);
275 0ac32c83 bellard
                        } else {
276 0ac32c83 bellard
                            isa_unassign_ioport(r->addr, r->size);
277 0ac32c83 bellard
                        }
278 0ac32c83 bellard
                    } else {
279 502a5395 pbrook
                        cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
280 5fafdf24 ths
                                                     r->size,
281 0ac32c83 bellard
                                                     IO_MEM_UNASSIGNED);
282 0ac32c83 bellard
                    }
283 0ac32c83 bellard
                }
284 0ac32c83 bellard
                r->addr = new_addr;
285 0ac32c83 bellard
                if (r->addr != -1) {
286 0ac32c83 bellard
                    r->map_func(d, i, r->addr, r->size, r->type);
287 0ac32c83 bellard
                }
288 0ac32c83 bellard
            }
289 0ac32c83 bellard
        }
290 0ac32c83 bellard
    }
291 0ac32c83 bellard
}
292 0ac32c83 bellard
293 5fafdf24 ths
uint32_t pci_default_read_config(PCIDevice *d,
294 0ac32c83 bellard
                                 uint32_t address, int len)
295 69b91039 bellard
{
296 0ac32c83 bellard
    uint32_t val;
297 a2d4e44b ths
298 0ac32c83 bellard
    switch(len) {
299 0ac32c83 bellard
    default:
300 0ac32c83 bellard
    case 4:
301 a2d4e44b ths
        if (address <= 0xfc) {
302 a2d4e44b ths
            val = le32_to_cpu(*(uint32_t *)(d->config + address));
303 a2d4e44b ths
            break;
304 a2d4e44b ths
        }
305 a2d4e44b ths
        /* fall through */
306 a2d4e44b ths
    case 2:
307 a2d4e44b ths
        if (address <= 0xfe) {
308 a2d4e44b ths
            val = le16_to_cpu(*(uint16_t *)(d->config + address));
309 a2d4e44b ths
            break;
310 a2d4e44b ths
        }
311 a2d4e44b ths
        /* fall through */
312 a2d4e44b ths
    case 1:
313 a2d4e44b ths
        val = d->config[address];
314 0ac32c83 bellard
        break;
315 0ac32c83 bellard
    }
316 0ac32c83 bellard
    return val;
317 0ac32c83 bellard
}
318 0ac32c83 bellard
319 5fafdf24 ths
void pci_default_write_config(PCIDevice *d,
320 0ac32c83 bellard
                              uint32_t address, uint32_t val, int len)
321 0ac32c83 bellard
{
322 0ac32c83 bellard
    int can_write, i;
323 7bf5be70 bellard
    uint32_t end, addr;
324 0ac32c83 bellard
325 5fafdf24 ths
    if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) ||
326 8a8696a3 bellard
                     (address >= 0x30 && address < 0x34))) {
327 0ac32c83 bellard
        PCIIORegion *r;
328 0ac32c83 bellard
        int reg;
329 0ac32c83 bellard
330 8a8696a3 bellard
        if ( address >= 0x30 ) {
331 8a8696a3 bellard
            reg = PCI_ROM_SLOT;
332 8a8696a3 bellard
        }else{
333 8a8696a3 bellard
            reg = (address - 0x10) >> 2;
334 8a8696a3 bellard
        }
335 0ac32c83 bellard
        r = &d->io_regions[reg];
336 0ac32c83 bellard
        if (r->size == 0)
337 0ac32c83 bellard
            goto default_config;
338 0ac32c83 bellard
        /* compute the stored value */
339 8a8696a3 bellard
        if (reg == PCI_ROM_SLOT) {
340 8a8696a3 bellard
            /* keep ROM enable bit */
341 8a8696a3 bellard
            val &= (~(r->size - 1)) | 1;
342 8a8696a3 bellard
        } else {
343 8a8696a3 bellard
            val &= ~(r->size - 1);
344 8a8696a3 bellard
            val |= r->type;
345 8a8696a3 bellard
        }
346 8a8696a3 bellard
        *(uint32_t *)(d->config + address) = cpu_to_le32(val);
347 0ac32c83 bellard
        pci_update_mappings(d);
348 69b91039 bellard
        return;
349 0ac32c83 bellard
    }
350 0ac32c83 bellard
 default_config:
351 0ac32c83 bellard
    /* not efficient, but simple */
352 7bf5be70 bellard
    addr = address;
353 0ac32c83 bellard
    for(i = 0; i < len; i++) {
354 0ac32c83 bellard
        /* default read/write accesses */
355 1f62d938 bellard
        switch(d->config[0x0e]) {
356 0ac32c83 bellard
        case 0x00:
357 1f62d938 bellard
        case 0x80:
358 1f62d938 bellard
            switch(addr) {
359 1f62d938 bellard
            case 0x00:
360 1f62d938 bellard
            case 0x01:
361 1f62d938 bellard
            case 0x02:
362 1f62d938 bellard
            case 0x03:
363 1f62d938 bellard
            case 0x08:
364 1f62d938 bellard
            case 0x09:
365 1f62d938 bellard
            case 0x0a:
366 1f62d938 bellard
            case 0x0b:
367 1f62d938 bellard
            case 0x0e:
368 1f62d938 bellard
            case 0x10 ... 0x27: /* base */
369 1f62d938 bellard
            case 0x30 ... 0x33: /* rom */
370 1f62d938 bellard
            case 0x3d:
371 1f62d938 bellard
                can_write = 0;
372 1f62d938 bellard
                break;
373 1f62d938 bellard
            default:
374 1f62d938 bellard
                can_write = 1;
375 1f62d938 bellard
                break;
376 1f62d938 bellard
            }
377 0ac32c83 bellard
            break;
378 0ac32c83 bellard
        default:
379 1f62d938 bellard
        case 0x01:
380 1f62d938 bellard
            switch(addr) {
381 1f62d938 bellard
            case 0x00:
382 1f62d938 bellard
            case 0x01:
383 1f62d938 bellard
            case 0x02:
384 1f62d938 bellard
            case 0x03:
385 1f62d938 bellard
            case 0x08:
386 1f62d938 bellard
            case 0x09:
387 1f62d938 bellard
            case 0x0a:
388 1f62d938 bellard
            case 0x0b:
389 1f62d938 bellard
            case 0x0e:
390 1f62d938 bellard
            case 0x38 ... 0x3b: /* rom */
391 1f62d938 bellard
            case 0x3d:
392 1f62d938 bellard
                can_write = 0;
393 1f62d938 bellard
                break;
394 1f62d938 bellard
            default:
395 1f62d938 bellard
                can_write = 1;
396 1f62d938 bellard
                break;
397 1f62d938 bellard
            }
398 0ac32c83 bellard
            break;
399 0ac32c83 bellard
        }
400 0ac32c83 bellard
        if (can_write) {
401 7bf5be70 bellard
            d->config[addr] = val;
402 0ac32c83 bellard
        }
403 a2d4e44b ths
        if (++addr > 0xff)
404 a2d4e44b ths
                break;
405 0ac32c83 bellard
        val >>= 8;
406 0ac32c83 bellard
    }
407 0ac32c83 bellard
408 0ac32c83 bellard
    end = address + len;
409 0ac32c83 bellard
    if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
410 0ac32c83 bellard
        /* if the command register is modified, we must modify the mappings */
411 0ac32c83 bellard
        pci_update_mappings(d);
412 69b91039 bellard
    }
413 69b91039 bellard
}
414 69b91039 bellard
415 502a5395 pbrook
void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
416 69b91039 bellard
{
417 30468f78 bellard
    PCIBus *s = opaque;
418 30468f78 bellard
    PCIDevice *pci_dev;
419 30468f78 bellard
    int config_addr, bus_num;
420 3b46e624 ths
421 69b91039 bellard
#if defined(DEBUG_PCI) && 0
422 69b91039 bellard
    printf("pci_data_write: addr=%08x val=%08x len=%d\n",
423 502a5395 pbrook
           addr, val, len);
424 69b91039 bellard
#endif
425 502a5395 pbrook
    bus_num = (addr >> 16) & 0xff;
426 80b3ada7 pbrook
    while (s && s->bus_num != bus_num)
427 80b3ada7 pbrook
        s = s->next;
428 80b3ada7 pbrook
    if (!s)
429 69b91039 bellard
        return;
430 502a5395 pbrook
    pci_dev = s->devices[(addr >> 8) & 0xff];
431 69b91039 bellard
    if (!pci_dev)
432 69b91039 bellard
        return;
433 502a5395 pbrook
    config_addr = addr & 0xff;
434 69b91039 bellard
#if defined(DEBUG_PCI)
435 69b91039 bellard
    printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
436 69b91039 bellard
           pci_dev->name, config_addr, val, len);
437 69b91039 bellard
#endif
438 0ac32c83 bellard
    pci_dev->config_write(pci_dev, config_addr, val, len);
439 69b91039 bellard
}
440 69b91039 bellard
441 502a5395 pbrook
uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
442 69b91039 bellard
{
443 30468f78 bellard
    PCIBus *s = opaque;
444 30468f78 bellard
    PCIDevice *pci_dev;
445 30468f78 bellard
    int config_addr, bus_num;
446 69b91039 bellard
    uint32_t val;
447 69b91039 bellard
448 502a5395 pbrook
    bus_num = (addr >> 16) & 0xff;
449 80b3ada7 pbrook
    while (s && s->bus_num != bus_num)
450 80b3ada7 pbrook
        s= s->next;
451 80b3ada7 pbrook
    if (!s)
452 69b91039 bellard
        goto fail;
453 502a5395 pbrook
    pci_dev = s->devices[(addr >> 8) & 0xff];
454 69b91039 bellard
    if (!pci_dev) {
455 69b91039 bellard
    fail:
456 63ce9e0a bellard
        switch(len) {
457 63ce9e0a bellard
        case 1:
458 63ce9e0a bellard
            val = 0xff;
459 63ce9e0a bellard
            break;
460 63ce9e0a bellard
        case 2:
461 63ce9e0a bellard
            val = 0xffff;
462 63ce9e0a bellard
            break;
463 63ce9e0a bellard
        default:
464 63ce9e0a bellard
        case 4:
465 63ce9e0a bellard
            val = 0xffffffff;
466 63ce9e0a bellard
            break;
467 63ce9e0a bellard
        }
468 69b91039 bellard
        goto the_end;
469 69b91039 bellard
    }
470 502a5395 pbrook
    config_addr = addr & 0xff;
471 69b91039 bellard
    val = pci_dev->config_read(pci_dev, config_addr, len);
472 69b91039 bellard
#if defined(DEBUG_PCI)
473 69b91039 bellard
    printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
474 69b91039 bellard
           pci_dev->name, config_addr, val, len);
475 69b91039 bellard
#endif
476 69b91039 bellard
 the_end:
477 69b91039 bellard
#if defined(DEBUG_PCI) && 0
478 69b91039 bellard
    printf("pci_data_read: addr=%08x val=%08x len=%d\n",
479 502a5395 pbrook
           addr, val, len);
480 69b91039 bellard
#endif
481 69b91039 bellard
    return val;
482 69b91039 bellard
}
483 69b91039 bellard
484 502a5395 pbrook
/***********************************************************/
485 502a5395 pbrook
/* generic PCI irq support */
486 30468f78 bellard
487 502a5395 pbrook
/* 0 <= irq_num <= 3. level must be 0 or 1 */
488 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level)
489 69b91039 bellard
{
490 d537cf6c pbrook
    PCIDevice *pci_dev = (PCIDevice *)opaque;
491 80b3ada7 pbrook
    PCIBus *bus;
492 80b3ada7 pbrook
    int change;
493 3b46e624 ths
494 80b3ada7 pbrook
    change = level - pci_dev->irq_state[irq_num];
495 80b3ada7 pbrook
    if (!change)
496 80b3ada7 pbrook
        return;
497 d2b59317 pbrook
498 d2b59317 pbrook
    pci_dev->irq_state[irq_num] = level;
499 5e966ce6 pbrook
    for (;;) {
500 5e966ce6 pbrook
        bus = pci_dev->bus;
501 80b3ada7 pbrook
        irq_num = bus->map_irq(pci_dev, irq_num);
502 5e966ce6 pbrook
        if (bus->set_irq)
503 5e966ce6 pbrook
            break;
504 80b3ada7 pbrook
        pci_dev = bus->parent_dev;
505 80b3ada7 pbrook
    }
506 80b3ada7 pbrook
    bus->irq_count[irq_num] += change;
507 d2b59317 pbrook
    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
508 69b91039 bellard
}
509 69b91039 bellard
510 502a5395 pbrook
/***********************************************************/
511 502a5395 pbrook
/* monitor info on PCI */
512 0ac32c83 bellard
513 6650ee6d pbrook
typedef struct {
514 6650ee6d pbrook
    uint16_t class;
515 6650ee6d pbrook
    const char *desc;
516 6650ee6d pbrook
} pci_class_desc;
517 6650ee6d pbrook
518 5fafdf24 ths
static pci_class_desc pci_class_descriptions[] =
519 6650ee6d pbrook
{
520 4ca9c76f pbrook
    { 0x0100, "SCSI controller"},
521 6650ee6d pbrook
    { 0x0101, "IDE controller"},
522 dcb5b19a ths
    { 0x0102, "Floppy controller"},
523 dcb5b19a ths
    { 0x0103, "IPI controller"},
524 dcb5b19a ths
    { 0x0104, "RAID controller"},
525 dcb5b19a ths
    { 0x0106, "SATA controller"},
526 dcb5b19a ths
    { 0x0107, "SAS controller"},
527 dcb5b19a ths
    { 0x0180, "Storage controller"},
528 6650ee6d pbrook
    { 0x0200, "Ethernet controller"},
529 dcb5b19a ths
    { 0x0201, "Token Ring controller"},
530 dcb5b19a ths
    { 0x0202, "FDDI controller"},
531 dcb5b19a ths
    { 0x0203, "ATM controller"},
532 dcb5b19a ths
    { 0x0280, "Network controller"},
533 6650ee6d pbrook
    { 0x0300, "VGA controller"},
534 dcb5b19a ths
    { 0x0301, "XGA controller"},
535 dcb5b19a ths
    { 0x0302, "3D controller"},
536 dcb5b19a ths
    { 0x0380, "Display controller"},
537 dcb5b19a ths
    { 0x0400, "Video controller"},
538 dcb5b19a ths
    { 0x0401, "Audio controller"},
539 dcb5b19a ths
    { 0x0402, "Phone"},
540 dcb5b19a ths
    { 0x0480, "Multimedia controller"},
541 dcb5b19a ths
    { 0x0500, "RAM controller"},
542 dcb5b19a ths
    { 0x0501, "Flash controller"},
543 dcb5b19a ths
    { 0x0580, "Memory controller"},
544 6650ee6d pbrook
    { 0x0600, "Host bridge"},
545 6650ee6d pbrook
    { 0x0601, "ISA bridge"},
546 dcb5b19a ths
    { 0x0602, "EISA bridge"},
547 dcb5b19a ths
    { 0x0603, "MC bridge"},
548 6650ee6d pbrook
    { 0x0604, "PCI bridge"},
549 dcb5b19a ths
    { 0x0605, "PCMCIA bridge"},
550 dcb5b19a ths
    { 0x0606, "NUBUS bridge"},
551 dcb5b19a ths
    { 0x0607, "CARDBUS bridge"},
552 dcb5b19a ths
    { 0x0608, "RACEWAY bridge"},
553 dcb5b19a ths
    { 0x0680, "Bridge"},
554 6650ee6d pbrook
    { 0x0c03, "USB controller"},
555 6650ee6d pbrook
    { 0, NULL}
556 6650ee6d pbrook
};
557 6650ee6d pbrook
558 502a5395 pbrook
static void pci_info_device(PCIDevice *d)
559 30468f78 bellard
{
560 502a5395 pbrook
    int i, class;
561 502a5395 pbrook
    PCIIORegion *r;
562 6650ee6d pbrook
    pci_class_desc *desc;
563 30468f78 bellard
564 502a5395 pbrook
    term_printf("  Bus %2d, device %3d, function %d:\n",
565 502a5395 pbrook
           d->bus->bus_num, d->devfn >> 3, d->devfn & 7);
566 502a5395 pbrook
    class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
567 502a5395 pbrook
    term_printf("    ");
568 6650ee6d pbrook
    desc = pci_class_descriptions;
569 6650ee6d pbrook
    while (desc->desc && class != desc->class)
570 6650ee6d pbrook
        desc++;
571 6650ee6d pbrook
    if (desc->desc) {
572 6650ee6d pbrook
        term_printf("%s", desc->desc);
573 6650ee6d pbrook
    } else {
574 502a5395 pbrook
        term_printf("Class %04x", class);
575 72cc6cfe bellard
    }
576 502a5395 pbrook
    term_printf(": PCI device %04x:%04x\n",
577 502a5395 pbrook
           le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
578 502a5395 pbrook
           le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
579 30468f78 bellard
580 502a5395 pbrook
    if (d->config[PCI_INTERRUPT_PIN] != 0) {
581 502a5395 pbrook
        term_printf("      IRQ %d.\n", d->config[PCI_INTERRUPT_LINE]);
582 30468f78 bellard
    }
583 80b3ada7 pbrook
    if (class == 0x0604) {
584 80b3ada7 pbrook
        term_printf("      BUS %d.\n", d->config[0x19]);
585 80b3ada7 pbrook
    }
586 502a5395 pbrook
    for(i = 0;i < PCI_NUM_REGIONS; i++) {
587 502a5395 pbrook
        r = &d->io_regions[i];
588 502a5395 pbrook
        if (r->size != 0) {
589 502a5395 pbrook
            term_printf("      BAR%d: ", i);
590 502a5395 pbrook
            if (r->type & PCI_ADDRESS_SPACE_IO) {
591 5fafdf24 ths
                term_printf("I/O at 0x%04x [0x%04x].\n",
592 502a5395 pbrook
                       r->addr, r->addr + r->size - 1);
593 502a5395 pbrook
            } else {
594 5fafdf24 ths
                term_printf("32 bit memory at 0x%08x [0x%08x].\n",
595 502a5395 pbrook
                       r->addr, r->addr + r->size - 1);
596 502a5395 pbrook
            }
597 502a5395 pbrook
        }
598 77d4bc34 bellard
    }
599 80b3ada7 pbrook
    if (class == 0x0604 && d->config[0x19] != 0) {
600 80b3ada7 pbrook
        pci_for_each_device(d->config[0x19], pci_info_device);
601 80b3ada7 pbrook
    }
602 384d8876 bellard
}
603 384d8876 bellard
604 80b3ada7 pbrook
void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d))
605 384d8876 bellard
{
606 502a5395 pbrook
    PCIBus *bus = first_bus;
607 384d8876 bellard
    PCIDevice *d;
608 502a5395 pbrook
    int devfn;
609 3b46e624 ths
610 80b3ada7 pbrook
    while (bus && bus->bus_num != bus_num)
611 80b3ada7 pbrook
        bus = bus->next;
612 502a5395 pbrook
    if (bus) {
613 502a5395 pbrook
        for(devfn = 0; devfn < 256; devfn++) {
614 502a5395 pbrook
            d = bus->devices[devfn];
615 502a5395 pbrook
            if (d)
616 502a5395 pbrook
                fn(d);
617 502a5395 pbrook
        }
618 f2aa58c6 bellard
    }
619 f2aa58c6 bellard
}
620 f2aa58c6 bellard
621 502a5395 pbrook
void pci_info(void)
622 f2aa58c6 bellard
{
623 80b3ada7 pbrook
    pci_for_each_device(0, pci_info_device);
624 77d4bc34 bellard
}
625 a41b2ff2 pbrook
626 a41b2ff2 pbrook
/* Initialize a PCI NIC.  */
627 abcebc7e ths
void pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn)
628 a41b2ff2 pbrook
{
629 a41b2ff2 pbrook
    if (strcmp(nd->model, "ne2k_pci") == 0) {
630 abcebc7e ths
        pci_ne2000_init(bus, nd, devfn);
631 663e8e51 ths
    } else if (strcmp(nd->model, "i82551") == 0) {
632 663e8e51 ths
        pci_i82551_init(bus, nd, devfn);
633 663e8e51 ths
    } else if (strcmp(nd->model, "i82557b") == 0) {
634 663e8e51 ths
        pci_i82557b_init(bus, nd, devfn);
635 663e8e51 ths
    } else if (strcmp(nd->model, "i82559er") == 0) {
636 663e8e51 ths
        pci_i82559er_init(bus, nd, devfn);
637 a41b2ff2 pbrook
    } else if (strcmp(nd->model, "rtl8139") == 0) {
638 abcebc7e ths
        pci_rtl8139_init(bus, nd, devfn);
639 7c23b892 balrog
    } else if (strcmp(nd->model, "e1000") == 0) {
640 7c23b892 balrog
        pci_e1000_init(bus, nd, devfn);
641 e3c2613f bellard
    } else if (strcmp(nd->model, "pcnet") == 0) {
642 abcebc7e ths
        pci_pcnet_init(bus, nd, devfn);
643 c4a7060c blueswir1
    } else if (strcmp(nd->model, "?") == 0) {
644 c4a7060c blueswir1
        fprintf(stderr, "qemu: Supported PCI NICs: i82551 i82557b i82559er"
645 7c23b892 balrog
                        " ne2k_pci pcnet rtl8139 e1000\n");
646 c4a7060c blueswir1
        exit (1);
647 a41b2ff2 pbrook
    } else {
648 a41b2ff2 pbrook
        fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
649 a41b2ff2 pbrook
        exit (1);
650 a41b2ff2 pbrook
    }
651 a41b2ff2 pbrook
}
652 a41b2ff2 pbrook
653 80b3ada7 pbrook
typedef struct {
654 80b3ada7 pbrook
    PCIDevice dev;
655 80b3ada7 pbrook
    PCIBus *bus;
656 80b3ada7 pbrook
} PCIBridge;
657 80b3ada7 pbrook
658 9596ebb7 pbrook
static void pci_bridge_write_config(PCIDevice *d,
659 80b3ada7 pbrook
                             uint32_t address, uint32_t val, int len)
660 80b3ada7 pbrook
{
661 80b3ada7 pbrook
    PCIBridge *s = (PCIBridge *)d;
662 80b3ada7 pbrook
663 80b3ada7 pbrook
    if (address == 0x19 || (address == 0x18 && len > 1)) {
664 80b3ada7 pbrook
        if (address == 0x19)
665 80b3ada7 pbrook
            s->bus->bus_num = val & 0xff;
666 80b3ada7 pbrook
        else
667 80b3ada7 pbrook
            s->bus->bus_num = (val >> 8) & 0xff;
668 80b3ada7 pbrook
#if defined(DEBUG_PCI)
669 80b3ada7 pbrook
        printf ("pci-bridge: %s: Assigned bus %d\n", d->name, s->bus->bus_num);
670 80b3ada7 pbrook
#endif
671 80b3ada7 pbrook
    }
672 80b3ada7 pbrook
    pci_default_write_config(d, address, val, len);
673 80b3ada7 pbrook
}
674 80b3ada7 pbrook
675 80b3ada7 pbrook
PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint32_t id,
676 80b3ada7 pbrook
                        pci_map_irq_fn map_irq, const char *name)
677 80b3ada7 pbrook
{
678 80b3ada7 pbrook
    PCIBridge *s;
679 5fafdf24 ths
    s = (PCIBridge *)pci_register_device(bus, name, sizeof(PCIBridge),
680 80b3ada7 pbrook
                                         devfn, NULL, pci_bridge_write_config);
681 80b3ada7 pbrook
    s->dev.config[0x00] = id >> 16;
682 451a4212 ths
    s->dev.config[0x01] = id >> 24;
683 80b3ada7 pbrook
    s->dev.config[0x02] = id; // device_id
684 80b3ada7 pbrook
    s->dev.config[0x03] = id >> 8;
685 80b3ada7 pbrook
    s->dev.config[0x04] = 0x06; // command = bus master, pci mem
686 80b3ada7 pbrook
    s->dev.config[0x05] = 0x00;
687 80b3ada7 pbrook
    s->dev.config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
688 80b3ada7 pbrook
    s->dev.config[0x07] = 0x00; // status = fast devsel
689 80b3ada7 pbrook
    s->dev.config[0x08] = 0x00; // revision
690 80b3ada7 pbrook
    s->dev.config[0x09] = 0x00; // programming i/f
691 80b3ada7 pbrook
    s->dev.config[0x0A] = 0x04; // class_sub = PCI to PCI bridge
692 80b3ada7 pbrook
    s->dev.config[0x0B] = 0x06; // class_base = PCI_bridge
693 80b3ada7 pbrook
    s->dev.config[0x0D] = 0x10; // latency_timer
694 80b3ada7 pbrook
    s->dev.config[0x0E] = 0x81; // header_type
695 80b3ada7 pbrook
    s->dev.config[0x1E] = 0xa0; // secondary status
696 80b3ada7 pbrook
697 80b3ada7 pbrook
    s->bus = pci_register_secondary_bus(&s->dev, map_irq);
698 80b3ada7 pbrook
    return s->bus;
699 80b3ada7 pbrook
}