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