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