Statistics
| Branch: | Revision:

root / hw / pci.c @ 0986ac3b

History | View | Annotate | Download (14.2 kB)

1 69b91039 bellard
/*
2 69b91039 bellard
 * QEMU PCI bus manager
3 69b91039 bellard
 *
4 69b91039 bellard
 * Copyright (c) 2004 Fabrice Bellard
5 69b91039 bellard
 * 
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 69b91039 bellard
#include "vl.h"
25 69b91039 bellard
26 69b91039 bellard
//#define DEBUG_PCI
27 69b91039 bellard
28 30468f78 bellard
struct PCIBus {
29 30468f78 bellard
    int bus_num;
30 30468f78 bellard
    int devfn_min;
31 502a5395 pbrook
    pci_set_irq_fn set_irq;
32 30468f78 bellard
    uint32_t config_reg; /* XXX: suppress */
33 384d8876 bellard
    /* low level pic */
34 384d8876 bellard
    SetIRQFunc *low_set_irq;
35 384d8876 bellard
    void *irq_opaque;
36 30468f78 bellard
    PCIDevice *devices[256];
37 30468f78 bellard
};
38 69b91039 bellard
39 69b91039 bellard
target_phys_addr_t pci_mem_base;
40 0ac32c83 bellard
static int pci_irq_index;
41 30468f78 bellard
static PCIBus *first_bus;
42 30468f78 bellard
43 502a5395 pbrook
PCIBus *pci_register_bus(pci_set_irq_fn set_irq, void *pic, int devfn_min)
44 30468f78 bellard
{
45 30468f78 bellard
    PCIBus *bus;
46 30468f78 bellard
    bus = qemu_mallocz(sizeof(PCIBus));
47 502a5395 pbrook
    bus->set_irq = set_irq;
48 502a5395 pbrook
    bus->irq_opaque = pic;
49 502a5395 pbrook
    bus->devfn_min = devfn_min;
50 30468f78 bellard
    first_bus = bus;
51 30468f78 bellard
    return bus;
52 30468f78 bellard
}
53 69b91039 bellard
54 502a5395 pbrook
int pci_bus_num(PCIBus *s)
55 502a5395 pbrook
{
56 502a5395 pbrook
    return s->bus_num;
57 502a5395 pbrook
}
58 502a5395 pbrook
59 30ca2aab bellard
void generic_pci_save(QEMUFile* f, void *opaque)
60 30ca2aab bellard
{
61 30ca2aab bellard
    PCIDevice* s=(PCIDevice*)opaque;
62 30ca2aab bellard
63 30ca2aab bellard
    qemu_put_buffer(f, s->config, 256);
64 30ca2aab bellard
}
65 30ca2aab bellard
66 30ca2aab bellard
int generic_pci_load(QEMUFile* f, void *opaque, int version_id)
67 30ca2aab bellard
{
68 30ca2aab bellard
    PCIDevice* s=(PCIDevice*)opaque;
69 30ca2aab bellard
70 30ca2aab bellard
    if (version_id != 1)
71 30ca2aab bellard
        return -EINVAL;
72 30ca2aab bellard
73 30ca2aab bellard
    qemu_get_buffer(f, s->config, 256);
74 30ca2aab bellard
    return 0;
75 30ca2aab bellard
}
76 30ca2aab bellard
77 69b91039 bellard
/* -1 for devfn means auto assign */
78 30468f78 bellard
PCIDevice *pci_register_device(PCIBus *bus, const char *name, 
79 30468f78 bellard
                               int instance_size, int devfn,
80 69b91039 bellard
                               PCIConfigReadFunc *config_read, 
81 69b91039 bellard
                               PCIConfigWriteFunc *config_write)
82 69b91039 bellard
{
83 30468f78 bellard
    PCIDevice *pci_dev;
84 69b91039 bellard
85 0ac32c83 bellard
    if (pci_irq_index >= PCI_DEVICES_MAX)
86 0ac32c83 bellard
        return NULL;
87 0ac32c83 bellard
    
88 69b91039 bellard
    if (devfn < 0) {
89 30468f78 bellard
        for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
90 30468f78 bellard
            if (!bus->devices[devfn])
91 69b91039 bellard
                goto found;
92 69b91039 bellard
        }
93 69b91039 bellard
        return NULL;
94 69b91039 bellard
    found: ;
95 69b91039 bellard
    }
96 69b91039 bellard
    pci_dev = qemu_mallocz(instance_size);
97 69b91039 bellard
    if (!pci_dev)
98 69b91039 bellard
        return NULL;
99 30468f78 bellard
    pci_dev->bus = bus;
100 69b91039 bellard
    pci_dev->devfn = devfn;
101 69b91039 bellard
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
102 0ac32c83 bellard
103 0ac32c83 bellard
    if (!config_read)
104 0ac32c83 bellard
        config_read = pci_default_read_config;
105 0ac32c83 bellard
    if (!config_write)
106 0ac32c83 bellard
        config_write = pci_default_write_config;
107 69b91039 bellard
    pci_dev->config_read = config_read;
108 69b91039 bellard
    pci_dev->config_write = config_write;
109 0ac32c83 bellard
    pci_dev->irq_index = pci_irq_index++;
110 30468f78 bellard
    bus->devices[devfn] = pci_dev;
111 69b91039 bellard
    return pci_dev;
112 69b91039 bellard
}
113 69b91039 bellard
114 69b91039 bellard
void pci_register_io_region(PCIDevice *pci_dev, int region_num, 
115 69b91039 bellard
                            uint32_t size, int type, 
116 69b91039 bellard
                            PCIMapIORegionFunc *map_func)
117 69b91039 bellard
{
118 69b91039 bellard
    PCIIORegion *r;
119 d7ce493a pbrook
    uint32_t addr;
120 69b91039 bellard
121 8a8696a3 bellard
    if ((unsigned int)region_num >= PCI_NUM_REGIONS)
122 69b91039 bellard
        return;
123 69b91039 bellard
    r = &pci_dev->io_regions[region_num];
124 69b91039 bellard
    r->addr = -1;
125 69b91039 bellard
    r->size = size;
126 69b91039 bellard
    r->type = type;
127 69b91039 bellard
    r->map_func = map_func;
128 d7ce493a pbrook
    if (region_num == PCI_ROM_SLOT) {
129 d7ce493a pbrook
        addr = 0x30;
130 d7ce493a pbrook
    } else {
131 d7ce493a pbrook
        addr = 0x10 + region_num * 4;
132 d7ce493a pbrook
    }
133 d7ce493a pbrook
    *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
134 69b91039 bellard
}
135 69b91039 bellard
136 502a5395 pbrook
target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
137 69b91039 bellard
{
138 502a5395 pbrook
    return addr + pci_mem_base;
139 69b91039 bellard
}
140 69b91039 bellard
141 0ac32c83 bellard
static void pci_update_mappings(PCIDevice *d)
142 0ac32c83 bellard
{
143 0ac32c83 bellard
    PCIIORegion *r;
144 0ac32c83 bellard
    int cmd, i;
145 8a8696a3 bellard
    uint32_t last_addr, new_addr, config_ofs;
146 0ac32c83 bellard
    
147 0ac32c83 bellard
    cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
148 8a8696a3 bellard
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
149 0ac32c83 bellard
        r = &d->io_regions[i];
150 8a8696a3 bellard
        if (i == PCI_ROM_SLOT) {
151 8a8696a3 bellard
            config_ofs = 0x30;
152 8a8696a3 bellard
        } else {
153 8a8696a3 bellard
            config_ofs = 0x10 + i * 4;
154 8a8696a3 bellard
        }
155 0ac32c83 bellard
        if (r->size != 0) {
156 0ac32c83 bellard
            if (r->type & PCI_ADDRESS_SPACE_IO) {
157 0ac32c83 bellard
                if (cmd & PCI_COMMAND_IO) {
158 0ac32c83 bellard
                    new_addr = le32_to_cpu(*(uint32_t *)(d->config + 
159 8a8696a3 bellard
                                                         config_ofs));
160 0ac32c83 bellard
                    new_addr = new_addr & ~(r->size - 1);
161 0ac32c83 bellard
                    last_addr = new_addr + r->size - 1;
162 0ac32c83 bellard
                    /* NOTE: we have only 64K ioports on PC */
163 0ac32c83 bellard
                    if (last_addr <= new_addr || new_addr == 0 ||
164 0ac32c83 bellard
                        last_addr >= 0x10000) {
165 0ac32c83 bellard
                        new_addr = -1;
166 0ac32c83 bellard
                    }
167 0ac32c83 bellard
                } else {
168 0ac32c83 bellard
                    new_addr = -1;
169 0ac32c83 bellard
                }
170 0ac32c83 bellard
            } else {
171 0ac32c83 bellard
                if (cmd & PCI_COMMAND_MEMORY) {
172 0ac32c83 bellard
                    new_addr = le32_to_cpu(*(uint32_t *)(d->config + 
173 8a8696a3 bellard
                                                         config_ofs));
174 8a8696a3 bellard
                    /* the ROM slot has a specific enable bit */
175 8a8696a3 bellard
                    if (i == PCI_ROM_SLOT && !(new_addr & 1))
176 8a8696a3 bellard
                        goto no_mem_map;
177 0ac32c83 bellard
                    new_addr = new_addr & ~(r->size - 1);
178 0ac32c83 bellard
                    last_addr = new_addr + r->size - 1;
179 0ac32c83 bellard
                    /* NOTE: we do not support wrapping */
180 0ac32c83 bellard
                    /* XXX: as we cannot support really dynamic
181 0ac32c83 bellard
                       mappings, we handle specific values as invalid
182 0ac32c83 bellard
                       mappings. */
183 0ac32c83 bellard
                    if (last_addr <= new_addr || new_addr == 0 ||
184 0ac32c83 bellard
                        last_addr == -1) {
185 0ac32c83 bellard
                        new_addr = -1;
186 0ac32c83 bellard
                    }
187 0ac32c83 bellard
                } else {
188 8a8696a3 bellard
                no_mem_map:
189 0ac32c83 bellard
                    new_addr = -1;
190 0ac32c83 bellard
                }
191 0ac32c83 bellard
            }
192 0ac32c83 bellard
            /* now do the real mapping */
193 0ac32c83 bellard
            if (new_addr != r->addr) {
194 0ac32c83 bellard
                if (r->addr != -1) {
195 0ac32c83 bellard
                    if (r->type & PCI_ADDRESS_SPACE_IO) {
196 0ac32c83 bellard
                        int class;
197 0ac32c83 bellard
                        /* NOTE: specific hack for IDE in PC case:
198 0ac32c83 bellard
                           only one byte must be mapped. */
199 0ac32c83 bellard
                        class = d->config[0x0a] | (d->config[0x0b] << 8);
200 0ac32c83 bellard
                        if (class == 0x0101 && r->size == 4) {
201 0ac32c83 bellard
                            isa_unassign_ioport(r->addr + 2, 1);
202 0ac32c83 bellard
                        } else {
203 0ac32c83 bellard
                            isa_unassign_ioport(r->addr, r->size);
204 0ac32c83 bellard
                        }
205 0ac32c83 bellard
                    } else {
206 502a5395 pbrook
                        cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
207 0ac32c83 bellard
                                                     r->size, 
208 0ac32c83 bellard
                                                     IO_MEM_UNASSIGNED);
209 0ac32c83 bellard
                    }
210 0ac32c83 bellard
                }
211 0ac32c83 bellard
                r->addr = new_addr;
212 0ac32c83 bellard
                if (r->addr != -1) {
213 0ac32c83 bellard
                    r->map_func(d, i, r->addr, r->size, r->type);
214 0ac32c83 bellard
                }
215 0ac32c83 bellard
            }
216 0ac32c83 bellard
        }
217 0ac32c83 bellard
    }
218 0ac32c83 bellard
}
219 0ac32c83 bellard
220 0ac32c83 bellard
uint32_t pci_default_read_config(PCIDevice *d, 
221 0ac32c83 bellard
                                 uint32_t address, int len)
222 69b91039 bellard
{
223 0ac32c83 bellard
    uint32_t val;
224 0ac32c83 bellard
    switch(len) {
225 0ac32c83 bellard
    case 1:
226 0ac32c83 bellard
        val = d->config[address];
227 0ac32c83 bellard
        break;
228 0ac32c83 bellard
    case 2:
229 0ac32c83 bellard
        val = le16_to_cpu(*(uint16_t *)(d->config + address));
230 0ac32c83 bellard
        break;
231 0ac32c83 bellard
    default:
232 0ac32c83 bellard
    case 4:
233 0ac32c83 bellard
        val = le32_to_cpu(*(uint32_t *)(d->config + address));
234 0ac32c83 bellard
        break;
235 0ac32c83 bellard
    }
236 0ac32c83 bellard
    return val;
237 0ac32c83 bellard
}
238 0ac32c83 bellard
239 0ac32c83 bellard
void pci_default_write_config(PCIDevice *d, 
240 0ac32c83 bellard
                              uint32_t address, uint32_t val, int len)
241 0ac32c83 bellard
{
242 0ac32c83 bellard
    int can_write, i;
243 7bf5be70 bellard
    uint32_t end, addr;
244 0ac32c83 bellard
245 8a8696a3 bellard
    if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) || 
246 8a8696a3 bellard
                     (address >= 0x30 && address < 0x34))) {
247 0ac32c83 bellard
        PCIIORegion *r;
248 0ac32c83 bellard
        int reg;
249 0ac32c83 bellard
250 8a8696a3 bellard
        if ( address >= 0x30 ) {
251 8a8696a3 bellard
            reg = PCI_ROM_SLOT;
252 8a8696a3 bellard
        }else{
253 8a8696a3 bellard
            reg = (address - 0x10) >> 2;
254 8a8696a3 bellard
        }
255 0ac32c83 bellard
        r = &d->io_regions[reg];
256 0ac32c83 bellard
        if (r->size == 0)
257 0ac32c83 bellard
            goto default_config;
258 0ac32c83 bellard
        /* compute the stored value */
259 8a8696a3 bellard
        if (reg == PCI_ROM_SLOT) {
260 8a8696a3 bellard
            /* keep ROM enable bit */
261 8a8696a3 bellard
            val &= (~(r->size - 1)) | 1;
262 8a8696a3 bellard
        } else {
263 8a8696a3 bellard
            val &= ~(r->size - 1);
264 8a8696a3 bellard
            val |= r->type;
265 8a8696a3 bellard
        }
266 8a8696a3 bellard
        *(uint32_t *)(d->config + address) = cpu_to_le32(val);
267 0ac32c83 bellard
        pci_update_mappings(d);
268 69b91039 bellard
        return;
269 0ac32c83 bellard
    }
270 0ac32c83 bellard
 default_config:
271 0ac32c83 bellard
    /* not efficient, but simple */
272 7bf5be70 bellard
    addr = address;
273 0ac32c83 bellard
    for(i = 0; i < len; i++) {
274 0ac32c83 bellard
        /* default read/write accesses */
275 1f62d938 bellard
        switch(d->config[0x0e]) {
276 0ac32c83 bellard
        case 0x00:
277 1f62d938 bellard
        case 0x80:
278 1f62d938 bellard
            switch(addr) {
279 1f62d938 bellard
            case 0x00:
280 1f62d938 bellard
            case 0x01:
281 1f62d938 bellard
            case 0x02:
282 1f62d938 bellard
            case 0x03:
283 1f62d938 bellard
            case 0x08:
284 1f62d938 bellard
            case 0x09:
285 1f62d938 bellard
            case 0x0a:
286 1f62d938 bellard
            case 0x0b:
287 1f62d938 bellard
            case 0x0e:
288 1f62d938 bellard
            case 0x10 ... 0x27: /* base */
289 1f62d938 bellard
            case 0x30 ... 0x33: /* rom */
290 1f62d938 bellard
            case 0x3d:
291 1f62d938 bellard
                can_write = 0;
292 1f62d938 bellard
                break;
293 1f62d938 bellard
            default:
294 1f62d938 bellard
                can_write = 1;
295 1f62d938 bellard
                break;
296 1f62d938 bellard
            }
297 0ac32c83 bellard
            break;
298 0ac32c83 bellard
        default:
299 1f62d938 bellard
        case 0x01:
300 1f62d938 bellard
            switch(addr) {
301 1f62d938 bellard
            case 0x00:
302 1f62d938 bellard
            case 0x01:
303 1f62d938 bellard
            case 0x02:
304 1f62d938 bellard
            case 0x03:
305 1f62d938 bellard
            case 0x08:
306 1f62d938 bellard
            case 0x09:
307 1f62d938 bellard
            case 0x0a:
308 1f62d938 bellard
            case 0x0b:
309 1f62d938 bellard
            case 0x0e:
310 1f62d938 bellard
            case 0x38 ... 0x3b: /* rom */
311 1f62d938 bellard
            case 0x3d:
312 1f62d938 bellard
                can_write = 0;
313 1f62d938 bellard
                break;
314 1f62d938 bellard
            default:
315 1f62d938 bellard
                can_write = 1;
316 1f62d938 bellard
                break;
317 1f62d938 bellard
            }
318 0ac32c83 bellard
            break;
319 0ac32c83 bellard
        }
320 0ac32c83 bellard
        if (can_write) {
321 7bf5be70 bellard
            d->config[addr] = val;
322 0ac32c83 bellard
        }
323 7bf5be70 bellard
        addr++;
324 0ac32c83 bellard
        val >>= 8;
325 0ac32c83 bellard
    }
326 0ac32c83 bellard
327 0ac32c83 bellard
    end = address + len;
328 0ac32c83 bellard
    if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
329 0ac32c83 bellard
        /* if the command register is modified, we must modify the mappings */
330 0ac32c83 bellard
        pci_update_mappings(d);
331 69b91039 bellard
    }
332 69b91039 bellard
}
333 69b91039 bellard
334 502a5395 pbrook
void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
335 69b91039 bellard
{
336 30468f78 bellard
    PCIBus *s = opaque;
337 30468f78 bellard
    PCIDevice *pci_dev;
338 30468f78 bellard
    int config_addr, bus_num;
339 69b91039 bellard
    
340 69b91039 bellard
#if defined(DEBUG_PCI) && 0
341 69b91039 bellard
    printf("pci_data_write: addr=%08x val=%08x len=%d\n",
342 502a5395 pbrook
           addr, val, len);
343 69b91039 bellard
#endif
344 502a5395 pbrook
    bus_num = (addr >> 16) & 0xff;
345 30468f78 bellard
    if (bus_num != 0)
346 69b91039 bellard
        return;
347 502a5395 pbrook
    pci_dev = s->devices[(addr >> 8) & 0xff];
348 69b91039 bellard
    if (!pci_dev)
349 69b91039 bellard
        return;
350 502a5395 pbrook
    config_addr = addr & 0xff;
351 69b91039 bellard
#if defined(DEBUG_PCI)
352 69b91039 bellard
    printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
353 69b91039 bellard
           pci_dev->name, config_addr, val, len);
354 69b91039 bellard
#endif
355 0ac32c83 bellard
    pci_dev->config_write(pci_dev, config_addr, val, len);
356 69b91039 bellard
}
357 69b91039 bellard
358 502a5395 pbrook
uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
359 69b91039 bellard
{
360 30468f78 bellard
    PCIBus *s = opaque;
361 30468f78 bellard
    PCIDevice *pci_dev;
362 30468f78 bellard
    int config_addr, bus_num;
363 69b91039 bellard
    uint32_t val;
364 69b91039 bellard
365 502a5395 pbrook
    bus_num = (addr >> 16) & 0xff;
366 30468f78 bellard
    if (bus_num != 0)
367 69b91039 bellard
        goto fail;
368 502a5395 pbrook
    pci_dev = s->devices[(addr >> 8) & 0xff];
369 69b91039 bellard
    if (!pci_dev) {
370 69b91039 bellard
    fail:
371 63ce9e0a bellard
        switch(len) {
372 63ce9e0a bellard
        case 1:
373 63ce9e0a bellard
            val = 0xff;
374 63ce9e0a bellard
            break;
375 63ce9e0a bellard
        case 2:
376 63ce9e0a bellard
            val = 0xffff;
377 63ce9e0a bellard
            break;
378 63ce9e0a bellard
        default:
379 63ce9e0a bellard
        case 4:
380 63ce9e0a bellard
            val = 0xffffffff;
381 63ce9e0a bellard
            break;
382 63ce9e0a bellard
        }
383 69b91039 bellard
        goto the_end;
384 69b91039 bellard
    }
385 502a5395 pbrook
    config_addr = addr & 0xff;
386 69b91039 bellard
    val = pci_dev->config_read(pci_dev, config_addr, len);
387 69b91039 bellard
#if defined(DEBUG_PCI)
388 69b91039 bellard
    printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
389 69b91039 bellard
           pci_dev->name, config_addr, val, len);
390 69b91039 bellard
#endif
391 69b91039 bellard
 the_end:
392 69b91039 bellard
#if defined(DEBUG_PCI) && 0
393 69b91039 bellard
    printf("pci_data_read: addr=%08x val=%08x len=%d\n",
394 502a5395 pbrook
           addr, val, len);
395 69b91039 bellard
#endif
396 69b91039 bellard
    return val;
397 69b91039 bellard
}
398 69b91039 bellard
399 502a5395 pbrook
/***********************************************************/
400 502a5395 pbrook
/* generic PCI irq support */
401 30468f78 bellard
402 502a5395 pbrook
/* 0 <= irq_num <= 3. level must be 0 or 1 */
403 502a5395 pbrook
void pci_set_irq(PCIDevice *pci_dev, int irq_num, int level)
404 69b91039 bellard
{
405 502a5395 pbrook
    PCIBus *bus = pci_dev->bus;
406 502a5395 pbrook
    bus->set_irq(pci_dev, bus->irq_opaque, irq_num, level);
407 69b91039 bellard
}
408 69b91039 bellard
409 502a5395 pbrook
/***********************************************************/
410 502a5395 pbrook
/* monitor info on PCI */
411 0ac32c83 bellard
412 6650ee6d pbrook
typedef struct {
413 6650ee6d pbrook
    uint16_t class;
414 6650ee6d pbrook
    const char *desc;
415 6650ee6d pbrook
} pci_class_desc;
416 6650ee6d pbrook
417 6650ee6d pbrook
static pci_class_desc pci_class_descriptions[] = 
418 6650ee6d pbrook
{
419 6650ee6d pbrook
    { 0x0101, "IDE controller"},
420 6650ee6d pbrook
    { 0x0200, "Ethernet controller"},
421 6650ee6d pbrook
    { 0x0300, "VGA controller"},
422 6650ee6d pbrook
    { 0x0600, "Host bridge"},
423 6650ee6d pbrook
    { 0x0601, "ISA bridge"},
424 6650ee6d pbrook
    { 0x0604, "PCI bridge"},
425 6650ee6d pbrook
    { 0x0c03, "USB controller"},
426 6650ee6d pbrook
    { 0, NULL}
427 6650ee6d pbrook
};
428 6650ee6d pbrook
429 502a5395 pbrook
static void pci_info_device(PCIDevice *d)
430 30468f78 bellard
{
431 502a5395 pbrook
    int i, class;
432 502a5395 pbrook
    PCIIORegion *r;
433 6650ee6d pbrook
    pci_class_desc *desc;
434 30468f78 bellard
435 502a5395 pbrook
    term_printf("  Bus %2d, device %3d, function %d:\n",
436 502a5395 pbrook
           d->bus->bus_num, d->devfn >> 3, d->devfn & 7);
437 502a5395 pbrook
    class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
438 502a5395 pbrook
    term_printf("    ");
439 6650ee6d pbrook
    desc = pci_class_descriptions;
440 6650ee6d pbrook
    while (desc->desc && class != desc->class)
441 6650ee6d pbrook
        desc++;
442 6650ee6d pbrook
    if (desc->desc) {
443 6650ee6d pbrook
        term_printf("%s", desc->desc);
444 6650ee6d pbrook
    } else {
445 502a5395 pbrook
        term_printf("Class %04x", class);
446 72cc6cfe bellard
    }
447 502a5395 pbrook
    term_printf(": PCI device %04x:%04x\n",
448 502a5395 pbrook
           le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
449 502a5395 pbrook
           le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
450 30468f78 bellard
451 502a5395 pbrook
    if (d->config[PCI_INTERRUPT_PIN] != 0) {
452 502a5395 pbrook
        term_printf("      IRQ %d.\n", d->config[PCI_INTERRUPT_LINE]);
453 30468f78 bellard
    }
454 502a5395 pbrook
    for(i = 0;i < PCI_NUM_REGIONS; i++) {
455 502a5395 pbrook
        r = &d->io_regions[i];
456 502a5395 pbrook
        if (r->size != 0) {
457 502a5395 pbrook
            term_printf("      BAR%d: ", i);
458 502a5395 pbrook
            if (r->type & PCI_ADDRESS_SPACE_IO) {
459 502a5395 pbrook
                term_printf("I/O at 0x%04x [0x%04x].\n", 
460 502a5395 pbrook
                       r->addr, r->addr + r->size - 1);
461 502a5395 pbrook
            } else {
462 502a5395 pbrook
                term_printf("32 bit memory at 0x%08x [0x%08x].\n", 
463 502a5395 pbrook
                       r->addr, r->addr + r->size - 1);
464 502a5395 pbrook
            }
465 502a5395 pbrook
        }
466 77d4bc34 bellard
    }
467 384d8876 bellard
}
468 384d8876 bellard
469 502a5395 pbrook
void pci_for_each_device(void (*fn)(PCIDevice *d))
470 384d8876 bellard
{
471 502a5395 pbrook
    PCIBus *bus = first_bus;
472 384d8876 bellard
    PCIDevice *d;
473 502a5395 pbrook
    int devfn;
474 384d8876 bellard
    
475 502a5395 pbrook
    if (bus) {
476 502a5395 pbrook
        for(devfn = 0; devfn < 256; devfn++) {
477 502a5395 pbrook
            d = bus->devices[devfn];
478 502a5395 pbrook
            if (d)
479 502a5395 pbrook
                fn(d);
480 502a5395 pbrook
        }
481 f2aa58c6 bellard
    }
482 f2aa58c6 bellard
}
483 f2aa58c6 bellard
484 502a5395 pbrook
void pci_info(void)
485 f2aa58c6 bellard
{
486 502a5395 pbrook
    pci_for_each_device(pci_info_device);
487 77d4bc34 bellard
}
488 a41b2ff2 pbrook
489 a41b2ff2 pbrook
/* Initialize a PCI NIC.  */
490 a41b2ff2 pbrook
void pci_nic_init(PCIBus *bus, NICInfo *nd)
491 a41b2ff2 pbrook
{
492 a41b2ff2 pbrook
    if (strcmp(nd->model, "ne2k_pci") == 0) {
493 a41b2ff2 pbrook
        pci_ne2000_init(bus, nd);
494 a41b2ff2 pbrook
    } else if (strcmp(nd->model, "rtl8139") == 0) {
495 a41b2ff2 pbrook
        pci_rtl8139_init(bus, nd);
496 a41b2ff2 pbrook
    } else {
497 a41b2ff2 pbrook
        fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
498 a41b2ff2 pbrook
        exit (1);
499 a41b2ff2 pbrook
    }
500 a41b2ff2 pbrook
}