root / hw / pci.c @ f9aebe2e
History | View | Annotate | Download (54.1 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 "pci_bridge.h" |
27 |
#include "pci_internals.h" |
28 |
#include "msix.h" |
29 |
#include "msi.h" |
30 |
#include "monitor.h" |
31 |
#include "net.h" |
32 |
#include "sysemu.h" |
33 |
#include "loader.h" |
34 |
#include "qemu-objects.h" |
35 |
|
36 |
//#define DEBUG_PCI
|
37 |
#ifdef DEBUG_PCI
|
38 |
# define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
39 |
#else
|
40 |
# define PCI_DPRINTF(format, ...) do { } while (0) |
41 |
#endif
|
42 |
|
43 |
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
44 |
static char *pcibus_get_dev_path(DeviceState *dev); |
45 |
|
46 |
struct BusInfo pci_bus_info = {
|
47 |
.name = "PCI",
|
48 |
.size = sizeof(PCIBus),
|
49 |
.print_dev = pcibus_dev_print, |
50 |
.get_dev_path = pcibus_get_dev_path, |
51 |
.props = (Property[]) { |
52 |
DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), |
53 |
DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
|
54 |
DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1), |
55 |
DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
|
56 |
QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
|
57 |
DEFINE_PROP_END_OF_LIST() |
58 |
} |
59 |
}; |
60 |
|
61 |
static void pci_update_mappings(PCIDevice *d); |
62 |
static void pci_set_irq(void *opaque, int irq_num, int level); |
63 |
static int pci_add_option_rom(PCIDevice *pdev); |
64 |
static void pci_del_option_rom(PCIDevice *pdev); |
65 |
|
66 |
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
|
67 |
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
|
68 |
|
69 |
struct PCIHostBus {
|
70 |
int domain;
|
71 |
struct PCIBus *bus;
|
72 |
QLIST_ENTRY(PCIHostBus) next; |
73 |
}; |
74 |
static QLIST_HEAD(, PCIHostBus) host_buses;
|
75 |
|
76 |
static const VMStateDescription vmstate_pcibus = { |
77 |
.name = "PCIBUS",
|
78 |
.version_id = 1,
|
79 |
.minimum_version_id = 1,
|
80 |
.minimum_version_id_old = 1,
|
81 |
.fields = (VMStateField []) { |
82 |
VMSTATE_INT32_EQUAL(nirq, PCIBus), |
83 |
VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
|
84 |
VMSTATE_END_OF_LIST() |
85 |
} |
86 |
}; |
87 |
|
88 |
static int pci_bar(PCIDevice *d, int reg) |
89 |
{ |
90 |
uint8_t type; |
91 |
|
92 |
if (reg != PCI_ROM_SLOT)
|
93 |
return PCI_BASE_ADDRESS_0 + reg * 4; |
94 |
|
95 |
type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
96 |
return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
|
97 |
} |
98 |
|
99 |
static inline int pci_irq_state(PCIDevice *d, int irq_num) |
100 |
{ |
101 |
return (d->irq_state >> irq_num) & 0x1; |
102 |
} |
103 |
|
104 |
static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level) |
105 |
{ |
106 |
d->irq_state &= ~(0x1 << irq_num);
|
107 |
d->irq_state |= level << irq_num; |
108 |
} |
109 |
|
110 |
static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change) |
111 |
{ |
112 |
PCIBus *bus; |
113 |
for (;;) {
|
114 |
bus = pci_dev->bus; |
115 |
irq_num = bus->map_irq(pci_dev, irq_num); |
116 |
if (bus->set_irq)
|
117 |
break;
|
118 |
pci_dev = bus->parent_dev; |
119 |
} |
120 |
bus->irq_count[irq_num] += change; |
121 |
bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
|
122 |
} |
123 |
|
124 |
/* Update interrupt status bit in config space on interrupt
|
125 |
* state change. */
|
126 |
static void pci_update_irq_status(PCIDevice *dev) |
127 |
{ |
128 |
if (dev->irq_state) {
|
129 |
dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT; |
130 |
} else {
|
131 |
dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; |
132 |
} |
133 |
} |
134 |
|
135 |
static void pci_device_reset(PCIDevice *dev) |
136 |
{ |
137 |
int r;
|
138 |
|
139 |
dev->irq_state = 0;
|
140 |
pci_update_irq_status(dev); |
141 |
/* Clear all writeable bits */
|
142 |
pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, |
143 |
pci_get_word(dev->wmask + PCI_COMMAND) | |
144 |
pci_get_word(dev->w1cmask + PCI_COMMAND)); |
145 |
dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
|
146 |
dev->config[PCI_INTERRUPT_LINE] = 0x0;
|
147 |
for (r = 0; r < PCI_NUM_REGIONS; ++r) { |
148 |
PCIIORegion *region = &dev->io_regions[r]; |
149 |
if (!region->size) {
|
150 |
continue;
|
151 |
} |
152 |
|
153 |
if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
154 |
region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
155 |
pci_set_quad(dev->config + pci_bar(dev, r), region->type); |
156 |
} else {
|
157 |
pci_set_long(dev->config + pci_bar(dev, r), region->type); |
158 |
} |
159 |
} |
160 |
pci_update_mappings(dev); |
161 |
} |
162 |
|
163 |
static void pci_bus_reset(void *opaque) |
164 |
{ |
165 |
PCIBus *bus = opaque; |
166 |
int i;
|
167 |
|
168 |
for (i = 0; i < bus->nirq; i++) { |
169 |
bus->irq_count[i] = 0;
|
170 |
} |
171 |
for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
172 |
if (bus->devices[i]) {
|
173 |
pci_device_reset(bus->devices[i]); |
174 |
} |
175 |
} |
176 |
} |
177 |
|
178 |
static void pci_host_bus_register(int domain, PCIBus *bus) |
179 |
{ |
180 |
struct PCIHostBus *host;
|
181 |
host = qemu_mallocz(sizeof(*host));
|
182 |
host->domain = domain; |
183 |
host->bus = bus; |
184 |
QLIST_INSERT_HEAD(&host_buses, host, next); |
185 |
} |
186 |
|
187 |
PCIBus *pci_find_root_bus(int domain)
|
188 |
{ |
189 |
struct PCIHostBus *host;
|
190 |
|
191 |
QLIST_FOREACH(host, &host_buses, next) { |
192 |
if (host->domain == domain) {
|
193 |
return host->bus;
|
194 |
} |
195 |
} |
196 |
|
197 |
return NULL; |
198 |
} |
199 |
|
200 |
int pci_find_domain(const PCIBus *bus) |
201 |
{ |
202 |
PCIDevice *d; |
203 |
struct PCIHostBus *host;
|
204 |
|
205 |
/* obtain root bus */
|
206 |
while ((d = bus->parent_dev) != NULL) { |
207 |
bus = d->bus; |
208 |
} |
209 |
|
210 |
QLIST_FOREACH(host, &host_buses, next) { |
211 |
if (host->bus == bus) {
|
212 |
return host->domain;
|
213 |
} |
214 |
} |
215 |
|
216 |
abort(); /* should not be reached */
|
217 |
return -1; |
218 |
} |
219 |
|
220 |
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
|
221 |
const char *name, int devfn_min) |
222 |
{ |
223 |
qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name); |
224 |
assert(PCI_FUNC(devfn_min) == 0);
|
225 |
bus->devfn_min = devfn_min; |
226 |
|
227 |
/* host bridge */
|
228 |
QLIST_INIT(&bus->child); |
229 |
pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */ |
230 |
|
231 |
vmstate_register(NULL, -1, &vmstate_pcibus, bus); |
232 |
qemu_register_reset(pci_bus_reset, bus); |
233 |
} |
234 |
|
235 |
PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min) |
236 |
{ |
237 |
PCIBus *bus; |
238 |
|
239 |
bus = qemu_mallocz(sizeof(*bus));
|
240 |
bus->qbus.qdev_allocated = 1;
|
241 |
pci_bus_new_inplace(bus, parent, name, devfn_min); |
242 |
return bus;
|
243 |
} |
244 |
|
245 |
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
|
246 |
void *irq_opaque, int nirq) |
247 |
{ |
248 |
bus->set_irq = set_irq; |
249 |
bus->map_irq = map_irq; |
250 |
bus->irq_opaque = irq_opaque; |
251 |
bus->nirq = nirq; |
252 |
bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0])); |
253 |
} |
254 |
|
255 |
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev)
|
256 |
{ |
257 |
bus->qbus.allow_hotplug = 1;
|
258 |
bus->hotplug = hotplug; |
259 |
bus->hotplug_qdev = qdev; |
260 |
} |
261 |
|
262 |
void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
|
263 |
{ |
264 |
bus->mem_base = base; |
265 |
} |
266 |
|
267 |
PCIBus *pci_register_bus(DeviceState *parent, const char *name, |
268 |
pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, |
269 |
void *irq_opaque, int devfn_min, int nirq) |
270 |
{ |
271 |
PCIBus *bus; |
272 |
|
273 |
bus = pci_bus_new(parent, name, devfn_min); |
274 |
pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq); |
275 |
return bus;
|
276 |
} |
277 |
|
278 |
int pci_bus_num(PCIBus *s)
|
279 |
{ |
280 |
if (!s->parent_dev)
|
281 |
return 0; /* pci host bridge */ |
282 |
return s->parent_dev->config[PCI_SECONDARY_BUS];
|
283 |
} |
284 |
|
285 |
static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) |
286 |
{ |
287 |
PCIDevice *s = container_of(pv, PCIDevice, config); |
288 |
uint8_t *config; |
289 |
int i;
|
290 |
|
291 |
assert(size == pci_config_size(s)); |
292 |
config = qemu_malloc(size); |
293 |
|
294 |
qemu_get_buffer(f, config, size); |
295 |
for (i = 0; i < size; ++i) { |
296 |
if ((config[i] ^ s->config[i]) &
|
297 |
s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) { |
298 |
qemu_free(config); |
299 |
return -EINVAL;
|
300 |
} |
301 |
} |
302 |
memcpy(s->config, config, size); |
303 |
|
304 |
pci_update_mappings(s); |
305 |
|
306 |
qemu_free(config); |
307 |
return 0; |
308 |
} |
309 |
|
310 |
/* just put buffer */
|
311 |
static void put_pci_config_device(QEMUFile *f, void *pv, size_t size) |
312 |
{ |
313 |
const uint8_t **v = pv;
|
314 |
assert(size == pci_config_size(container_of(pv, PCIDevice, config))); |
315 |
qemu_put_buffer(f, *v, size); |
316 |
} |
317 |
|
318 |
static VMStateInfo vmstate_info_pci_config = {
|
319 |
.name = "pci config",
|
320 |
.get = get_pci_config_device, |
321 |
.put = put_pci_config_device, |
322 |
}; |
323 |
|
324 |
static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
325 |
{ |
326 |
PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
327 |
uint32_t irq_state[PCI_NUM_PINS]; |
328 |
int i;
|
329 |
for (i = 0; i < PCI_NUM_PINS; ++i) { |
330 |
irq_state[i] = qemu_get_be32(f); |
331 |
if (irq_state[i] != 0x1 && irq_state[i] != 0) { |
332 |
fprintf(stderr, "irq state %d: must be 0 or 1.\n",
|
333 |
irq_state[i]); |
334 |
return -EINVAL;
|
335 |
} |
336 |
} |
337 |
|
338 |
for (i = 0; i < PCI_NUM_PINS; ++i) { |
339 |
pci_set_irq_state(s, i, irq_state[i]); |
340 |
} |
341 |
|
342 |
return 0; |
343 |
} |
344 |
|
345 |
static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
346 |
{ |
347 |
int i;
|
348 |
PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
349 |
|
350 |
for (i = 0; i < PCI_NUM_PINS; ++i) { |
351 |
qemu_put_be32(f, pci_irq_state(s, i)); |
352 |
} |
353 |
} |
354 |
|
355 |
static VMStateInfo vmstate_info_pci_irq_state = {
|
356 |
.name = "pci irq state",
|
357 |
.get = get_pci_irq_state, |
358 |
.put = put_pci_irq_state, |
359 |
}; |
360 |
|
361 |
const VMStateDescription vmstate_pci_device = {
|
362 |
.name = "PCIDevice",
|
363 |
.version_id = 2,
|
364 |
.minimum_version_id = 1,
|
365 |
.minimum_version_id_old = 1,
|
366 |
.fields = (VMStateField []) { |
367 |
VMSTATE_INT32_LE(version_id, PCIDevice), |
368 |
VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
369 |
vmstate_info_pci_config, |
370 |
PCI_CONFIG_SPACE_SIZE), |
371 |
VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
|
372 |
vmstate_info_pci_irq_state, |
373 |
PCI_NUM_PINS * sizeof(int32_t)),
|
374 |
VMSTATE_END_OF_LIST() |
375 |
} |
376 |
}; |
377 |
|
378 |
const VMStateDescription vmstate_pcie_device = {
|
379 |
.name = "PCIDevice",
|
380 |
.version_id = 2,
|
381 |
.minimum_version_id = 1,
|
382 |
.minimum_version_id_old = 1,
|
383 |
.fields = (VMStateField []) { |
384 |
VMSTATE_INT32_LE(version_id, PCIDevice), |
385 |
VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
386 |
vmstate_info_pci_config, |
387 |
PCIE_CONFIG_SPACE_SIZE), |
388 |
VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
|
389 |
vmstate_info_pci_irq_state, |
390 |
PCI_NUM_PINS * sizeof(int32_t)),
|
391 |
VMSTATE_END_OF_LIST() |
392 |
} |
393 |
}; |
394 |
|
395 |
static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s) |
396 |
{ |
397 |
return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
|
398 |
} |
399 |
|
400 |
void pci_device_save(PCIDevice *s, QEMUFile *f)
|
401 |
{ |
402 |
/* Clear interrupt status bit: it is implicit
|
403 |
* in irq_state which we are saving.
|
404 |
* This makes us compatible with old devices
|
405 |
* which never set or clear this bit. */
|
406 |
s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; |
407 |
vmstate_save_state(f, pci_get_vmstate(s), s); |
408 |
/* Restore the interrupt status bit. */
|
409 |
pci_update_irq_status(s); |
410 |
} |
411 |
|
412 |
int pci_device_load(PCIDevice *s, QEMUFile *f)
|
413 |
{ |
414 |
int ret;
|
415 |
ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id); |
416 |
/* Restore the interrupt status bit. */
|
417 |
pci_update_irq_status(s); |
418 |
return ret;
|
419 |
} |
420 |
|
421 |
static void pci_set_default_subsystem_id(PCIDevice *pci_dev) |
422 |
{ |
423 |
pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, |
424 |
pci_default_sub_vendor_id); |
425 |
pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, |
426 |
pci_default_sub_device_id); |
427 |
} |
428 |
|
429 |
/*
|
430 |
* Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
|
431 |
* [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
|
432 |
*/
|
433 |
int pci_parse_devaddr(const char *addr, int *domp, int *busp, |
434 |
unsigned int *slotp, unsigned int *funcp) |
435 |
{ |
436 |
const char *p; |
437 |
char *e;
|
438 |
unsigned long val; |
439 |
unsigned long dom = 0, bus = 0; |
440 |
unsigned int slot = 0; |
441 |
unsigned int func = 0; |
442 |
|
443 |
p = addr; |
444 |
val = strtoul(p, &e, 16);
|
445 |
if (e == p)
|
446 |
return -1; |
447 |
if (*e == ':') { |
448 |
bus = val; |
449 |
p = e + 1;
|
450 |
val = strtoul(p, &e, 16);
|
451 |
if (e == p)
|
452 |
return -1; |
453 |
if (*e == ':') { |
454 |
dom = bus; |
455 |
bus = val; |
456 |
p = e + 1;
|
457 |
val = strtoul(p, &e, 16);
|
458 |
if (e == p)
|
459 |
return -1; |
460 |
} |
461 |
} |
462 |
|
463 |
slot = val; |
464 |
|
465 |
if (funcp != NULL) { |
466 |
if (*e != '.') |
467 |
return -1; |
468 |
|
469 |
p = e + 1;
|
470 |
val = strtoul(p, &e, 16);
|
471 |
if (e == p)
|
472 |
return -1; |
473 |
|
474 |
func = val; |
475 |
} |
476 |
|
477 |
/* if funcp == NULL func is 0 */
|
478 |
if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) |
479 |
return -1; |
480 |
|
481 |
if (*e)
|
482 |
return -1; |
483 |
|
484 |
/* Note: QEMU doesn't implement domains other than 0 */
|
485 |
if (!pci_find_bus(pci_find_root_bus(dom), bus))
|
486 |
return -1; |
487 |
|
488 |
*domp = dom; |
489 |
*busp = bus; |
490 |
*slotp = slot; |
491 |
if (funcp != NULL) |
492 |
*funcp = func; |
493 |
return 0; |
494 |
} |
495 |
|
496 |
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp, |
497 |
unsigned *slotp)
|
498 |
{ |
499 |
/* strip legacy tag */
|
500 |
if (!strncmp(addr, "pci_addr=", 9)) { |
501 |
addr += 9;
|
502 |
} |
503 |
if (pci_parse_devaddr(addr, domp, busp, slotp, NULL)) { |
504 |
monitor_printf(mon, "Invalid pci address\n");
|
505 |
return -1; |
506 |
} |
507 |
return 0; |
508 |
} |
509 |
|
510 |
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr) |
511 |
{ |
512 |
int dom, bus;
|
513 |
unsigned slot;
|
514 |
|
515 |
if (!devaddr) {
|
516 |
*devfnp = -1;
|
517 |
return pci_find_bus(pci_find_root_bus(0), 0); |
518 |
} |
519 |
|
520 |
if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) { |
521 |
return NULL; |
522 |
} |
523 |
|
524 |
*devfnp = slot << 3;
|
525 |
return pci_find_bus(pci_find_root_bus(dom), bus);
|
526 |
} |
527 |
|
528 |
static void pci_init_cmask(PCIDevice *dev) |
529 |
{ |
530 |
pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
|
531 |
pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
|
532 |
dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; |
533 |
dev->cmask[PCI_REVISION_ID] = 0xff;
|
534 |
dev->cmask[PCI_CLASS_PROG] = 0xff;
|
535 |
pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
|
536 |
dev->cmask[PCI_HEADER_TYPE] = 0xff;
|
537 |
dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
|
538 |
} |
539 |
|
540 |
static void pci_init_wmask(PCIDevice *dev) |
541 |
{ |
542 |
int config_size = pci_config_size(dev);
|
543 |
|
544 |
dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
|
545 |
dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
|
546 |
pci_set_word(dev->wmask + PCI_COMMAND, |
547 |
PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | |
548 |
PCI_COMMAND_INTX_DISABLE); |
549 |
|
550 |
memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
|
551 |
config_size - PCI_CONFIG_HEADER_SIZE); |
552 |
} |
553 |
|
554 |
static void pci_init_wmask_bridge(PCIDevice *d) |
555 |
{ |
556 |
/* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
|
557 |
PCI_SEC_LETENCY_TIMER */
|
558 |
memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4); |
559 |
|
560 |
/* base and limit */
|
561 |
d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
|
562 |
d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
|
563 |
pci_set_word(d->wmask + PCI_MEMORY_BASE, |
564 |
PCI_MEMORY_RANGE_MASK & 0xffff);
|
565 |
pci_set_word(d->wmask + PCI_MEMORY_LIMIT, |
566 |
PCI_MEMORY_RANGE_MASK & 0xffff);
|
567 |
pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE, |
568 |
PCI_PREF_RANGE_MASK & 0xffff);
|
569 |
pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT, |
570 |
PCI_PREF_RANGE_MASK & 0xffff);
|
571 |
|
572 |
/* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
|
573 |
memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8); |
574 |
|
575 |
pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
|
576 |
} |
577 |
|
578 |
static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev) |
579 |
{ |
580 |
uint8_t slot = PCI_SLOT(dev->devfn); |
581 |
uint8_t func; |
582 |
|
583 |
if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
|
584 |
dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION; |
585 |
} |
586 |
|
587 |
/*
|
588 |
* multifuction bit is interpreted in two ways as follows.
|
589 |
* - all functions must set the bit to 1.
|
590 |
* Example: Intel X53
|
591 |
* - function 0 must set the bit, but the rest function (> 0)
|
592 |
* is allowed to leave the bit to 0.
|
593 |
* Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
|
594 |
*
|
595 |
* So OS (at least Linux) checks the bit of only function 0,
|
596 |
* and doesn't see the bit of function > 0.
|
597 |
*
|
598 |
* The below check allows both interpretation.
|
599 |
*/
|
600 |
if (PCI_FUNC(dev->devfn)) {
|
601 |
PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
|
602 |
if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
|
603 |
/* function 0 should set multifunction bit */
|
604 |
error_report("PCI: single function device can't be populated "
|
605 |
"in function %x.%x", slot, PCI_FUNC(dev->devfn));
|
606 |
return -1; |
607 |
} |
608 |
return 0; |
609 |
} |
610 |
|
611 |
if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
|
612 |
return 0; |
613 |
} |
614 |
/* function 0 indicates single function, so function > 0 must be NULL */
|
615 |
for (func = 1; func < PCI_FUNC_MAX; ++func) { |
616 |
if (bus->devices[PCI_DEVFN(slot, func)]) {
|
617 |
error_report("PCI: %x.0 indicates single function, "
|
618 |
"but %x.%x is already populated.",
|
619 |
slot, slot, func); |
620 |
return -1; |
621 |
} |
622 |
} |
623 |
return 0; |
624 |
} |
625 |
|
626 |
static void pci_config_alloc(PCIDevice *pci_dev) |
627 |
{ |
628 |
int config_size = pci_config_size(pci_dev);
|
629 |
|
630 |
pci_dev->config = qemu_mallocz(config_size); |
631 |
pci_dev->cmask = qemu_mallocz(config_size); |
632 |
pci_dev->wmask = qemu_mallocz(config_size); |
633 |
pci_dev->w1cmask = qemu_mallocz(config_size); |
634 |
pci_dev->used = qemu_mallocz(config_size); |
635 |
} |
636 |
|
637 |
static void pci_config_free(PCIDevice *pci_dev) |
638 |
{ |
639 |
qemu_free(pci_dev->config); |
640 |
qemu_free(pci_dev->cmask); |
641 |
qemu_free(pci_dev->wmask); |
642 |
qemu_free(pci_dev->w1cmask); |
643 |
qemu_free(pci_dev->used); |
644 |
} |
645 |
|
646 |
/* -1 for devfn means auto assign */
|
647 |
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
|
648 |
const char *name, int devfn, |
649 |
PCIConfigReadFunc *config_read, |
650 |
PCIConfigWriteFunc *config_write, |
651 |
bool is_bridge)
|
652 |
{ |
653 |
if (devfn < 0) { |
654 |
for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
|
655 |
devfn += PCI_FUNC_MAX) { |
656 |
if (!bus->devices[devfn])
|
657 |
goto found;
|
658 |
} |
659 |
error_report("PCI: no slot/function available for %s, all in use", name);
|
660 |
return NULL; |
661 |
found: ;
|
662 |
} else if (bus->devices[devfn]) { |
663 |
error_report("PCI: slot %d function %d not available for %s, in use by %s",
|
664 |
PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name); |
665 |
return NULL; |
666 |
} |
667 |
pci_dev->bus = bus; |
668 |
pci_dev->devfn = devfn; |
669 |
pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
|
670 |
pci_dev->irq_state = 0;
|
671 |
pci_config_alloc(pci_dev); |
672 |
|
673 |
if (!is_bridge) {
|
674 |
pci_set_default_subsystem_id(pci_dev); |
675 |
} |
676 |
pci_init_cmask(pci_dev); |
677 |
pci_init_wmask(pci_dev); |
678 |
if (is_bridge) {
|
679 |
pci_init_wmask_bridge(pci_dev); |
680 |
} |
681 |
if (pci_init_multifunction(bus, pci_dev)) {
|
682 |
pci_config_free(pci_dev); |
683 |
return NULL; |
684 |
} |
685 |
|
686 |
if (!config_read)
|
687 |
config_read = pci_default_read_config; |
688 |
if (!config_write)
|
689 |
config_write = pci_default_write_config; |
690 |
pci_dev->config_read = config_read; |
691 |
pci_dev->config_write = config_write; |
692 |
bus->devices[devfn] = pci_dev; |
693 |
pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS); |
694 |
pci_dev->version_id = 2; /* Current pci device vmstate version */ |
695 |
return pci_dev;
|
696 |
} |
697 |
|
698 |
static void do_pci_unregister_device(PCIDevice *pci_dev) |
699 |
{ |
700 |
qemu_free_irqs(pci_dev->irq); |
701 |
pci_dev->bus->devices[pci_dev->devfn] = NULL;
|
702 |
pci_config_free(pci_dev); |
703 |
} |
704 |
|
705 |
PCIDevice *pci_register_device(PCIBus *bus, const char *name, |
706 |
int instance_size, int devfn, |
707 |
PCIConfigReadFunc *config_read, |
708 |
PCIConfigWriteFunc *config_write) |
709 |
{ |
710 |
PCIDevice *pci_dev; |
711 |
|
712 |
pci_dev = qemu_mallocz(instance_size); |
713 |
pci_dev = do_pci_register_device(pci_dev, bus, name, devfn, |
714 |
config_read, config_write, |
715 |
PCI_HEADER_TYPE_NORMAL); |
716 |
if (pci_dev == NULL) { |
717 |
hw_error("PCI: can't register device\n");
|
718 |
} |
719 |
return pci_dev;
|
720 |
} |
721 |
|
722 |
static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
|
723 |
target_phys_addr_t addr) |
724 |
{ |
725 |
return addr + bus->mem_base;
|
726 |
} |
727 |
|
728 |
static void pci_unregister_io_regions(PCIDevice *pci_dev) |
729 |
{ |
730 |
PCIIORegion *r; |
731 |
int i;
|
732 |
|
733 |
for(i = 0; i < PCI_NUM_REGIONS; i++) { |
734 |
r = &pci_dev->io_regions[i]; |
735 |
if (!r->size || r->addr == PCI_BAR_UNMAPPED)
|
736 |
continue;
|
737 |
if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
|
738 |
isa_unassign_ioport(r->addr, r->filtered_size); |
739 |
} else {
|
740 |
cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus, |
741 |
r->addr), |
742 |
r->filtered_size, |
743 |
IO_MEM_UNASSIGNED); |
744 |
} |
745 |
} |
746 |
} |
747 |
|
748 |
static int pci_unregister_device(DeviceState *dev) |
749 |
{ |
750 |
PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev); |
751 |
PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info); |
752 |
int ret = 0; |
753 |
|
754 |
if (info->exit)
|
755 |
ret = info->exit(pci_dev); |
756 |
if (ret)
|
757 |
return ret;
|
758 |
|
759 |
pci_unregister_io_regions(pci_dev); |
760 |
pci_del_option_rom(pci_dev); |
761 |
do_pci_unregister_device(pci_dev); |
762 |
return 0; |
763 |
} |
764 |
|
765 |
void pci_register_bar(PCIDevice *pci_dev, int region_num, |
766 |
pcibus_t size, uint8_t type, |
767 |
PCIMapIORegionFunc *map_func) |
768 |
{ |
769 |
PCIIORegion *r; |
770 |
uint32_t addr; |
771 |
uint64_t wmask; |
772 |
|
773 |
assert(region_num >= 0);
|
774 |
assert(region_num < PCI_NUM_REGIONS); |
775 |
if (size & (size-1)) { |
776 |
fprintf(stderr, "ERROR: PCI region size must be pow2 "
|
777 |
"type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size); |
778 |
exit(1);
|
779 |
} |
780 |
|
781 |
r = &pci_dev->io_regions[region_num]; |
782 |
r->addr = PCI_BAR_UNMAPPED; |
783 |
r->size = size; |
784 |
r->filtered_size = size; |
785 |
r->type = type; |
786 |
r->map_func = map_func; |
787 |
|
788 |
wmask = ~(size - 1);
|
789 |
addr = pci_bar(pci_dev, region_num); |
790 |
if (region_num == PCI_ROM_SLOT) {
|
791 |
/* ROM enable bit is writeable */
|
792 |
wmask |= PCI_ROM_ADDRESS_ENABLE; |
793 |
} |
794 |
pci_set_long(pci_dev->config + addr, type); |
795 |
if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
796 |
r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
797 |
pci_set_quad(pci_dev->wmask + addr, wmask); |
798 |
pci_set_quad(pci_dev->cmask + addr, ~0ULL);
|
799 |
} else {
|
800 |
pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
|
801 |
pci_set_long(pci_dev->cmask + addr, 0xffffffff);
|
802 |
} |
803 |
} |
804 |
|
805 |
static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size, |
806 |
uint8_t type) |
807 |
{ |
808 |
pcibus_t base = *addr; |
809 |
pcibus_t limit = *addr + *size - 1;
|
810 |
PCIDevice *br; |
811 |
|
812 |
for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
|
813 |
uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
814 |
|
815 |
if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
816 |
if (!(cmd & PCI_COMMAND_IO)) {
|
817 |
goto no_map;
|
818 |
} |
819 |
} else {
|
820 |
if (!(cmd & PCI_COMMAND_MEMORY)) {
|
821 |
goto no_map;
|
822 |
} |
823 |
} |
824 |
|
825 |
base = MAX(base, pci_bridge_get_base(br, type)); |
826 |
limit = MIN(limit, pci_bridge_get_limit(br, type)); |
827 |
} |
828 |
|
829 |
if (base > limit) {
|
830 |
goto no_map;
|
831 |
} |
832 |
*addr = base; |
833 |
*size = limit - base + 1;
|
834 |
return;
|
835 |
no_map:
|
836 |
*addr = PCI_BAR_UNMAPPED; |
837 |
*size = 0;
|
838 |
} |
839 |
|
840 |
static pcibus_t pci_bar_address(PCIDevice *d,
|
841 |
int reg, uint8_t type, pcibus_t size)
|
842 |
{ |
843 |
pcibus_t new_addr, last_addr; |
844 |
int bar = pci_bar(d, reg);
|
845 |
uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
846 |
|
847 |
if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
848 |
if (!(cmd & PCI_COMMAND_IO)) {
|
849 |
return PCI_BAR_UNMAPPED;
|
850 |
} |
851 |
new_addr = pci_get_long(d->config + bar) & ~(size - 1);
|
852 |
last_addr = new_addr + size - 1;
|
853 |
/* NOTE: we have only 64K ioports on PC */
|
854 |
if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) { |
855 |
return PCI_BAR_UNMAPPED;
|
856 |
} |
857 |
return new_addr;
|
858 |
} |
859 |
|
860 |
if (!(cmd & PCI_COMMAND_MEMORY)) {
|
861 |
return PCI_BAR_UNMAPPED;
|
862 |
} |
863 |
if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
|
864 |
new_addr = pci_get_quad(d->config + bar); |
865 |
} else {
|
866 |
new_addr = pci_get_long(d->config + bar); |
867 |
} |
868 |
/* the ROM slot has a specific enable bit */
|
869 |
if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
|
870 |
return PCI_BAR_UNMAPPED;
|
871 |
} |
872 |
new_addr &= ~(size - 1);
|
873 |
last_addr = new_addr + size - 1;
|
874 |
/* NOTE: we do not support wrapping */
|
875 |
/* XXX: as we cannot support really dynamic
|
876 |
mappings, we handle specific values as invalid
|
877 |
mappings. */
|
878 |
if (last_addr <= new_addr || new_addr == 0 || |
879 |
last_addr == PCI_BAR_UNMAPPED) { |
880 |
return PCI_BAR_UNMAPPED;
|
881 |
} |
882 |
|
883 |
/* Now pcibus_t is 64bit.
|
884 |
* Check if 32 bit BAR wraps around explicitly.
|
885 |
* Without this, PC ide doesn't work well.
|
886 |
* TODO: remove this work around.
|
887 |
*/
|
888 |
if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
|
889 |
return PCI_BAR_UNMAPPED;
|
890 |
} |
891 |
|
892 |
/*
|
893 |
* OS is allowed to set BAR beyond its addressable
|
894 |
* bits. For example, 32 bit OS can set 64bit bar
|
895 |
* to >4G. Check it. TODO: we might need to support
|
896 |
* it in the future for e.g. PAE.
|
897 |
*/
|
898 |
if (last_addr >= TARGET_PHYS_ADDR_MAX) {
|
899 |
return PCI_BAR_UNMAPPED;
|
900 |
} |
901 |
|
902 |
return new_addr;
|
903 |
} |
904 |
|
905 |
static void pci_update_mappings(PCIDevice *d) |
906 |
{ |
907 |
PCIIORegion *r; |
908 |
int i;
|
909 |
pcibus_t new_addr, filtered_size; |
910 |
|
911 |
for(i = 0; i < PCI_NUM_REGIONS; i++) { |
912 |
r = &d->io_regions[i]; |
913 |
|
914 |
/* this region isn't registered */
|
915 |
if (!r->size)
|
916 |
continue;
|
917 |
|
918 |
new_addr = pci_bar_address(d, i, r->type, r->size); |
919 |
|
920 |
/* bridge filtering */
|
921 |
filtered_size = r->size; |
922 |
if (new_addr != PCI_BAR_UNMAPPED) {
|
923 |
pci_bridge_filter(d, &new_addr, &filtered_size, r->type); |
924 |
} |
925 |
|
926 |
/* This bar isn't changed */
|
927 |
if (new_addr == r->addr && filtered_size == r->filtered_size)
|
928 |
continue;
|
929 |
|
930 |
/* now do the real mapping */
|
931 |
if (r->addr != PCI_BAR_UNMAPPED) {
|
932 |
if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
933 |
int class;
|
934 |
/* NOTE: specific hack for IDE in PC case:
|
935 |
only one byte must be mapped. */
|
936 |
class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
937 |
if (class == 0x0101 && r->size == 4) { |
938 |
isa_unassign_ioport(r->addr + 2, 1); |
939 |
} else {
|
940 |
isa_unassign_ioport(r->addr, r->filtered_size); |
941 |
} |
942 |
} else {
|
943 |
cpu_register_physical_memory(pci_to_cpu_addr(d->bus, r->addr), |
944 |
r->filtered_size, |
945 |
IO_MEM_UNASSIGNED); |
946 |
qemu_unregister_coalesced_mmio(r->addr, r->filtered_size); |
947 |
} |
948 |
} |
949 |
r->addr = new_addr; |
950 |
r->filtered_size = filtered_size; |
951 |
if (r->addr != PCI_BAR_UNMAPPED) {
|
952 |
/*
|
953 |
* TODO: currently almost all the map funcions assumes
|
954 |
* filtered_size == size and addr & ~(size - 1) == addr.
|
955 |
* However with bridge filtering, they aren't always true.
|
956 |
* Teach them such cases, such that filtered_size < size and
|
957 |
* addr & (size - 1) != 0.
|
958 |
*/
|
959 |
if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
960 |
r->map_func(d, i, r->addr, r->filtered_size, r->type); |
961 |
} else {
|
962 |
r->map_func(d, i, pci_to_cpu_addr(d->bus, r->addr), |
963 |
r->filtered_size, r->type); |
964 |
} |
965 |
} |
966 |
} |
967 |
} |
968 |
|
969 |
static inline int pci_irq_disabled(PCIDevice *d) |
970 |
{ |
971 |
return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
|
972 |
} |
973 |
|
974 |
/* Called after interrupt disabled field update in config space,
|
975 |
* assert/deassert interrupts if necessary.
|
976 |
* Gets original interrupt disable bit value (before update). */
|
977 |
static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled) |
978 |
{ |
979 |
int i, disabled = pci_irq_disabled(d);
|
980 |
if (disabled == was_irq_disabled)
|
981 |
return;
|
982 |
for (i = 0; i < PCI_NUM_PINS; ++i) { |
983 |
int state = pci_irq_state(d, i);
|
984 |
pci_change_irq_level(d, i, disabled ? -state : state); |
985 |
} |
986 |
} |
987 |
|
988 |
uint32_t pci_default_read_config(PCIDevice *d, |
989 |
uint32_t address, int len)
|
990 |
{ |
991 |
uint32_t val = 0;
|
992 |
assert(len == 1 || len == 2 || len == 4); |
993 |
len = MIN(len, pci_config_size(d) - address); |
994 |
memcpy(&val, d->config + address, len); |
995 |
return le32_to_cpu(val);
|
996 |
} |
997 |
|
998 |
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) |
999 |
{ |
1000 |
int i, was_irq_disabled = pci_irq_disabled(d);
|
1001 |
uint32_t config_size = pci_config_size(d); |
1002 |
|
1003 |
for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) { |
1004 |
uint8_t wmask = d->wmask[addr + i]; |
1005 |
uint8_t w1cmask = d->w1cmask[addr + i]; |
1006 |
assert(!(wmask & w1cmask)); |
1007 |
d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask); |
1008 |
d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
|
1009 |
} |
1010 |
if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) || |
1011 |
ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
|
1012 |
ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
|
1013 |
range_covers_byte(addr, l, PCI_COMMAND)) |
1014 |
pci_update_mappings(d); |
1015 |
|
1016 |
if (range_covers_byte(addr, l, PCI_COMMAND))
|
1017 |
pci_update_irq_disabled(d, was_irq_disabled); |
1018 |
} |
1019 |
|
1020 |
/***********************************************************/
|
1021 |
/* generic PCI irq support */
|
1022 |
|
1023 |
/* 0 <= irq_num <= 3. level must be 0 or 1 */
|
1024 |
static void pci_set_irq(void *opaque, int irq_num, int level) |
1025 |
{ |
1026 |
PCIDevice *pci_dev = opaque; |
1027 |
int change;
|
1028 |
|
1029 |
change = level - pci_irq_state(pci_dev, irq_num); |
1030 |
if (!change)
|
1031 |
return;
|
1032 |
|
1033 |
pci_set_irq_state(pci_dev, irq_num, level); |
1034 |
pci_update_irq_status(pci_dev); |
1035 |
if (pci_irq_disabled(pci_dev))
|
1036 |
return;
|
1037 |
pci_change_irq_level(pci_dev, irq_num, change); |
1038 |
} |
1039 |
|
1040 |
bool pci_msi_enabled(PCIDevice *dev)
|
1041 |
{ |
1042 |
return msix_enabled(dev) || msi_enabled(dev);
|
1043 |
} |
1044 |
|
1045 |
void pci_msi_notify(PCIDevice *dev, unsigned int vector) |
1046 |
{ |
1047 |
if (msix_enabled(dev)) {
|
1048 |
msix_notify(dev, vector); |
1049 |
} else if (msi_enabled(dev)) { |
1050 |
msi_notify(dev, vector); |
1051 |
} else {
|
1052 |
/* MSI/MSI-X must be enabled */
|
1053 |
abort(); |
1054 |
} |
1055 |
} |
1056 |
|
1057 |
/***********************************************************/
|
1058 |
/* monitor info on PCI */
|
1059 |
|
1060 |
typedef struct { |
1061 |
uint16_t class; |
1062 |
const char *desc; |
1063 |
} pci_class_desc; |
1064 |
|
1065 |
static const pci_class_desc pci_class_descriptions[] = |
1066 |
{ |
1067 |
{ 0x0100, "SCSI controller"}, |
1068 |
{ 0x0101, "IDE controller"}, |
1069 |
{ 0x0102, "Floppy controller"}, |
1070 |
{ 0x0103, "IPI controller"}, |
1071 |
{ 0x0104, "RAID controller"}, |
1072 |
{ 0x0106, "SATA controller"}, |
1073 |
{ 0x0107, "SAS controller"}, |
1074 |
{ 0x0180, "Storage controller"}, |
1075 |
{ 0x0200, "Ethernet controller"}, |
1076 |
{ 0x0201, "Token Ring controller"}, |
1077 |
{ 0x0202, "FDDI controller"}, |
1078 |
{ 0x0203, "ATM controller"}, |
1079 |
{ 0x0280, "Network controller"}, |
1080 |
{ 0x0300, "VGA controller"}, |
1081 |
{ 0x0301, "XGA controller"}, |
1082 |
{ 0x0302, "3D controller"}, |
1083 |
{ 0x0380, "Display controller"}, |
1084 |
{ 0x0400, "Video controller"}, |
1085 |
{ 0x0401, "Audio controller"}, |
1086 |
{ 0x0402, "Phone"}, |
1087 |
{ 0x0480, "Multimedia controller"}, |
1088 |
{ 0x0500, "RAM controller"}, |
1089 |
{ 0x0501, "Flash controller"}, |
1090 |
{ 0x0580, "Memory controller"}, |
1091 |
{ 0x0600, "Host bridge"}, |
1092 |
{ 0x0601, "ISA bridge"}, |
1093 |
{ 0x0602, "EISA bridge"}, |
1094 |
{ 0x0603, "MC bridge"}, |
1095 |
{ 0x0604, "PCI bridge"}, |
1096 |
{ 0x0605, "PCMCIA bridge"}, |
1097 |
{ 0x0606, "NUBUS bridge"}, |
1098 |
{ 0x0607, "CARDBUS bridge"}, |
1099 |
{ 0x0608, "RACEWAY bridge"}, |
1100 |
{ 0x0680, "Bridge"}, |
1101 |
{ 0x0c03, "USB controller"}, |
1102 |
{ 0, NULL} |
1103 |
}; |
1104 |
|
1105 |
static void pci_for_each_device_under_bus(PCIBus *bus, |
1106 |
void (*fn)(PCIBus *b, PCIDevice *d))
|
1107 |
{ |
1108 |
PCIDevice *d; |
1109 |
int devfn;
|
1110 |
|
1111 |
for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1112 |
d = bus->devices[devfn]; |
1113 |
if (d) {
|
1114 |
fn(bus, d); |
1115 |
} |
1116 |
} |
1117 |
} |
1118 |
|
1119 |
void pci_for_each_device(PCIBus *bus, int bus_num, |
1120 |
void (*fn)(PCIBus *b, PCIDevice *d))
|
1121 |
{ |
1122 |
bus = pci_find_bus(bus, bus_num); |
1123 |
|
1124 |
if (bus) {
|
1125 |
pci_for_each_device_under_bus(bus, fn); |
1126 |
} |
1127 |
} |
1128 |
|
1129 |
static void pci_device_print(Monitor *mon, QDict *device) |
1130 |
{ |
1131 |
QDict *qdict; |
1132 |
QListEntry *entry; |
1133 |
uint64_t addr, size; |
1134 |
|
1135 |
monitor_printf(mon, " Bus %2" PRId64 ", ", qdict_get_int(device, "bus")); |
1136 |
monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n", |
1137 |
qdict_get_int(device, "slot"),
|
1138 |
qdict_get_int(device, "function"));
|
1139 |
monitor_printf(mon, " ");
|
1140 |
|
1141 |
qdict = qdict_get_qdict(device, "class_info");
|
1142 |
if (qdict_haskey(qdict, "desc")) { |
1143 |
monitor_printf(mon, "%s", qdict_get_str(qdict, "desc")); |
1144 |
} else {
|
1145 |
monitor_printf(mon, "Class %04" PRId64, qdict_get_int(qdict, "class")); |
1146 |
} |
1147 |
|
1148 |
qdict = qdict_get_qdict(device, "id");
|
1149 |
monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n", |
1150 |
qdict_get_int(qdict, "device"),
|
1151 |
qdict_get_int(qdict, "vendor"));
|
1152 |
|
1153 |
if (qdict_haskey(device, "irq")) { |
1154 |
monitor_printf(mon, " IRQ %" PRId64 ".\n", |
1155 |
qdict_get_int(device, "irq"));
|
1156 |
} |
1157 |
|
1158 |
if (qdict_haskey(device, "pci_bridge")) { |
1159 |
QDict *info; |
1160 |
|
1161 |
qdict = qdict_get_qdict(device, "pci_bridge");
|
1162 |
|
1163 |
info = qdict_get_qdict(qdict, "bus");
|
1164 |
monitor_printf(mon, " BUS %" PRId64 ".\n", |
1165 |
qdict_get_int(info, "number"));
|
1166 |
monitor_printf(mon, " secondary bus %" PRId64 ".\n", |
1167 |
qdict_get_int(info, "secondary"));
|
1168 |
monitor_printf(mon, " subordinate bus %" PRId64 ".\n", |
1169 |
qdict_get_int(info, "subordinate"));
|
1170 |
|
1171 |
info = qdict_get_qdict(qdict, "io_range");
|
1172 |
monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n", |
1173 |
qdict_get_int(info, "base"),
|
1174 |
qdict_get_int(info, "limit"));
|
1175 |
|
1176 |
info = qdict_get_qdict(qdict, "memory_range");
|
1177 |
monitor_printf(mon, |
1178 |
" memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n", |
1179 |
qdict_get_int(info, "base"),
|
1180 |
qdict_get_int(info, "limit"));
|
1181 |
|
1182 |
info = qdict_get_qdict(qdict, "prefetchable_range");
|
1183 |
monitor_printf(mon, " prefetchable memory range "
|
1184 |
"[0x%08"PRIx64", 0x%08"PRIx64"]\n", |
1185 |
qdict_get_int(info, "base"),
|
1186 |
qdict_get_int(info, "limit"));
|
1187 |
} |
1188 |
|
1189 |
QLIST_FOREACH_ENTRY(qdict_get_qlist(device, "regions"), entry) {
|
1190 |
qdict = qobject_to_qdict(qlist_entry_obj(entry)); |
1191 |
monitor_printf(mon, " BAR%d: ", (int) qdict_get_int(qdict, "bar")); |
1192 |
|
1193 |
addr = qdict_get_int(qdict, "address");
|
1194 |
size = qdict_get_int(qdict, "size");
|
1195 |
|
1196 |
if (!strcmp(qdict_get_str(qdict, "type"), "io")) { |
1197 |
monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
|
1198 |
" [0x%04"FMT_PCIBUS"].\n", |
1199 |
addr, addr + size - 1);
|
1200 |
} else {
|
1201 |
monitor_printf(mon, "%d bit%s memory at 0x%08"FMT_PCIBUS
|
1202 |
" [0x%08"FMT_PCIBUS"].\n", |
1203 |
qdict_get_bool(qdict, "mem_type_64") ? 64 : 32, |
1204 |
qdict_get_bool(qdict, "prefetch") ?
|
1205 |
" prefetchable" : "", addr, addr + size - 1); |
1206 |
} |
1207 |
} |
1208 |
|
1209 |
monitor_printf(mon, " id \"%s\"\n", qdict_get_str(device, "qdev_id")); |
1210 |
|
1211 |
if (qdict_haskey(device, "pci_bridge")) { |
1212 |
qdict = qdict_get_qdict(device, "pci_bridge");
|
1213 |
if (qdict_haskey(qdict, "devices")) { |
1214 |
QListEntry *dev; |
1215 |
QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
|
1216 |
pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev))); |
1217 |
} |
1218 |
} |
1219 |
} |
1220 |
} |
1221 |
|
1222 |
void do_pci_info_print(Monitor *mon, const QObject *data) |
1223 |
{ |
1224 |
QListEntry *bus, *dev; |
1225 |
|
1226 |
QLIST_FOREACH_ENTRY(qobject_to_qlist(data), bus) { |
1227 |
QDict *qdict = qobject_to_qdict(qlist_entry_obj(bus)); |
1228 |
QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
|
1229 |
pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev))); |
1230 |
} |
1231 |
} |
1232 |
} |
1233 |
|
1234 |
static QObject *pci_get_dev_class(const PCIDevice *dev) |
1235 |
{ |
1236 |
int class;
|
1237 |
const pci_class_desc *desc;
|
1238 |
|
1239 |
class = pci_get_word(dev->config + PCI_CLASS_DEVICE); |
1240 |
desc = pci_class_descriptions; |
1241 |
while (desc->desc && class != desc->class)
|
1242 |
desc++; |
1243 |
|
1244 |
if (desc->desc) {
|
1245 |
return qobject_from_jsonf("{ 'desc': %s, 'class': %d }", |
1246 |
desc->desc, class); |
1247 |
} else {
|
1248 |
return qobject_from_jsonf("{ 'class': %d }", class); |
1249 |
} |
1250 |
} |
1251 |
|
1252 |
static QObject *pci_get_dev_id(const PCIDevice *dev) |
1253 |
{ |
1254 |
return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }", |
1255 |
pci_get_word(dev->config + PCI_VENDOR_ID), |
1256 |
pci_get_word(dev->config + PCI_DEVICE_ID)); |
1257 |
} |
1258 |
|
1259 |
static QObject *pci_get_regions_list(const PCIDevice *dev) |
1260 |
{ |
1261 |
int i;
|
1262 |
QList *regions_list; |
1263 |
|
1264 |
regions_list = qlist_new(); |
1265 |
|
1266 |
for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1267 |
QObject *obj; |
1268 |
const PCIIORegion *r = &dev->io_regions[i];
|
1269 |
|
1270 |
if (!r->size) {
|
1271 |
continue;
|
1272 |
} |
1273 |
|
1274 |
if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1275 |
obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
|
1276 |
"'address': %" PRId64 ", " |
1277 |
"'size': %" PRId64 " }", |
1278 |
i, r->addr, r->size); |
1279 |
} else {
|
1280 |
int mem_type_64 = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64;
|
1281 |
|
1282 |
obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
|
1283 |
"'mem_type_64': %i, 'prefetch': %i, "
|
1284 |
"'address': %" PRId64 ", " |
1285 |
"'size': %" PRId64 " }", |
1286 |
i, mem_type_64, |
1287 |
r->type & PCI_BASE_ADDRESS_MEM_PREFETCH, |
1288 |
r->addr, r->size); |
1289 |
} |
1290 |
|
1291 |
qlist_append_obj(regions_list, obj); |
1292 |
} |
1293 |
|
1294 |
return QOBJECT(regions_list);
|
1295 |
} |
1296 |
|
1297 |
static QObject *pci_get_devices_list(PCIBus *bus, int bus_num); |
1298 |
|
1299 |
static QObject *pci_get_dev_dict(PCIDevice *dev, PCIBus *bus, int bus_num) |
1300 |
{ |
1301 |
uint8_t type; |
1302 |
QObject *obj; |
1303 |
|
1304 |
obj = qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d," "'class_info': %p, 'id': %p, 'regions': %p," |
1305 |
" 'qdev_id': %s }",
|
1306 |
bus_num, |
1307 |
PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
1308 |
pci_get_dev_class(dev), pci_get_dev_id(dev), |
1309 |
pci_get_regions_list(dev), |
1310 |
dev->qdev.id ? dev->qdev.id : "");
|
1311 |
|
1312 |
if (dev->config[PCI_INTERRUPT_PIN] != 0) { |
1313 |
QDict *qdict = qobject_to_qdict(obj); |
1314 |
qdict_put(qdict, "irq", qint_from_int(dev->config[PCI_INTERRUPT_LINE]));
|
1315 |
} |
1316 |
|
1317 |
type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
1318 |
if (type == PCI_HEADER_TYPE_BRIDGE) {
|
1319 |
QDict *qdict; |
1320 |
QObject *pci_bridge; |
1321 |
|
1322 |
pci_bridge = qobject_from_jsonf("{ 'bus': "
|
1323 |
"{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
|
1324 |
"'io_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, " |
1325 |
"'memory_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, " |
1326 |
"'prefetchable_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "} }", |
1327 |
dev->config[PCI_PRIMARY_BUS], dev->config[PCI_SECONDARY_BUS], |
1328 |
dev->config[PCI_SUBORDINATE_BUS], |
1329 |
pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO), |
1330 |
pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO), |
1331 |
pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY), |
1332 |
pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY), |
1333 |
pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY | |
1334 |
PCI_BASE_ADDRESS_MEM_PREFETCH), |
1335 |
pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY | |
1336 |
PCI_BASE_ADDRESS_MEM_PREFETCH)); |
1337 |
|
1338 |
if (dev->config[PCI_SECONDARY_BUS] != 0) { |
1339 |
PCIBus *child_bus = pci_find_bus(bus, dev->config[PCI_SECONDARY_BUS]); |
1340 |
|
1341 |
if (child_bus) {
|
1342 |
qdict = qobject_to_qdict(pci_bridge); |
1343 |
qdict_put_obj(qdict, "devices",
|
1344 |
pci_get_devices_list(child_bus, |
1345 |
dev->config[PCI_SECONDARY_BUS])); |
1346 |
} |
1347 |
} |
1348 |
qdict = qobject_to_qdict(obj); |
1349 |
qdict_put_obj(qdict, "pci_bridge", pci_bridge);
|
1350 |
} |
1351 |
|
1352 |
return obj;
|
1353 |
} |
1354 |
|
1355 |
static QObject *pci_get_devices_list(PCIBus *bus, int bus_num) |
1356 |
{ |
1357 |
int devfn;
|
1358 |
PCIDevice *dev; |
1359 |
QList *dev_list; |
1360 |
|
1361 |
dev_list = qlist_new(); |
1362 |
|
1363 |
for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1364 |
dev = bus->devices[devfn]; |
1365 |
if (dev) {
|
1366 |
qlist_append_obj(dev_list, pci_get_dev_dict(dev, bus, bus_num)); |
1367 |
} |
1368 |
} |
1369 |
|
1370 |
return QOBJECT(dev_list);
|
1371 |
} |
1372 |
|
1373 |
static QObject *pci_get_bus_dict(PCIBus *bus, int bus_num) |
1374 |
{ |
1375 |
bus = pci_find_bus(bus, bus_num); |
1376 |
if (bus) {
|
1377 |
return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }", |
1378 |
bus_num, pci_get_devices_list(bus, bus_num)); |
1379 |
} |
1380 |
|
1381 |
return NULL; |
1382 |
} |
1383 |
|
1384 |
void do_pci_info(Monitor *mon, QObject **ret_data)
|
1385 |
{ |
1386 |
QList *bus_list; |
1387 |
struct PCIHostBus *host;
|
1388 |
|
1389 |
bus_list = qlist_new(); |
1390 |
|
1391 |
QLIST_FOREACH(host, &host_buses, next) { |
1392 |
QObject *obj = pci_get_bus_dict(host->bus, 0);
|
1393 |
if (obj) {
|
1394 |
qlist_append_obj(bus_list, obj); |
1395 |
} |
1396 |
} |
1397 |
|
1398 |
*ret_data = QOBJECT(bus_list); |
1399 |
} |
1400 |
|
1401 |
static const char * const pci_nic_models[] = { |
1402 |
"ne2k_pci",
|
1403 |
"i82551",
|
1404 |
"i82557b",
|
1405 |
"i82559er",
|
1406 |
"rtl8139",
|
1407 |
"e1000",
|
1408 |
"pcnet",
|
1409 |
"virtio",
|
1410 |
NULL
|
1411 |
}; |
1412 |
|
1413 |
static const char * const pci_nic_names[] = { |
1414 |
"ne2k_pci",
|
1415 |
"i82551",
|
1416 |
"i82557b",
|
1417 |
"i82559er",
|
1418 |
"rtl8139",
|
1419 |
"e1000",
|
1420 |
"pcnet",
|
1421 |
"virtio-net-pci",
|
1422 |
NULL
|
1423 |
}; |
1424 |
|
1425 |
/* Initialize a PCI NIC. */
|
1426 |
/* FIXME callers should check for failure, but don't */
|
1427 |
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model, |
1428 |
const char *default_devaddr) |
1429 |
{ |
1430 |
const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; |
1431 |
PCIBus *bus; |
1432 |
int devfn;
|
1433 |
PCIDevice *pci_dev; |
1434 |
DeviceState *dev; |
1435 |
int i;
|
1436 |
|
1437 |
i = qemu_find_nic_model(nd, pci_nic_models, default_model); |
1438 |
if (i < 0) |
1439 |
return NULL; |
1440 |
|
1441 |
bus = pci_get_bus_devfn(&devfn, devaddr); |
1442 |
if (!bus) {
|
1443 |
error_report("Invalid PCI device address %s for device %s",
|
1444 |
devaddr, pci_nic_names[i]); |
1445 |
return NULL; |
1446 |
} |
1447 |
|
1448 |
pci_dev = pci_create(bus, devfn, pci_nic_names[i]); |
1449 |
dev = &pci_dev->qdev; |
1450 |
qdev_set_nic_properties(dev, nd); |
1451 |
if (qdev_init(dev) < 0) |
1452 |
return NULL; |
1453 |
return pci_dev;
|
1454 |
} |
1455 |
|
1456 |
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model, |
1457 |
const char *default_devaddr) |
1458 |
{ |
1459 |
PCIDevice *res; |
1460 |
|
1461 |
if (qemu_show_nic_models(nd->model, pci_nic_models))
|
1462 |
exit(0);
|
1463 |
|
1464 |
res = pci_nic_init(nd, default_model, default_devaddr); |
1465 |
if (!res)
|
1466 |
exit(1);
|
1467 |
return res;
|
1468 |
} |
1469 |
|
1470 |
static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d) |
1471 |
{ |
1472 |
pci_update_mappings(d); |
1473 |
} |
1474 |
|
1475 |
void pci_bridge_update_mappings(PCIBus *b)
|
1476 |
{ |
1477 |
PCIBus *child; |
1478 |
|
1479 |
pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn); |
1480 |
|
1481 |
QLIST_FOREACH(child, &b->child, sibling) { |
1482 |
pci_bridge_update_mappings(child); |
1483 |
} |
1484 |
} |
1485 |
|
1486 |
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
|
1487 |
{ |
1488 |
PCIBus *sec; |
1489 |
|
1490 |
if (!bus) {
|
1491 |
return NULL; |
1492 |
} |
1493 |
|
1494 |
if (pci_bus_num(bus) == bus_num) {
|
1495 |
return bus;
|
1496 |
} |
1497 |
|
1498 |
/* try child bus */
|
1499 |
if (!bus->parent_dev /* host pci bridge */ || |
1500 |
(bus->parent_dev->config[PCI_SECONDARY_BUS] < bus_num && |
1501 |
bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS])) { |
1502 |
for (; bus; bus = sec) {
|
1503 |
QLIST_FOREACH(sec, &bus->child, sibling) { |
1504 |
assert(sec->parent_dev); |
1505 |
if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
|
1506 |
return sec;
|
1507 |
} |
1508 |
if (sec->parent_dev->config[PCI_SECONDARY_BUS] < bus_num &&
|
1509 |
bus_num <= sec->parent_dev->config[PCI_SUBORDINATE_BUS]) { |
1510 |
break;
|
1511 |
} |
1512 |
} |
1513 |
} |
1514 |
} |
1515 |
|
1516 |
return NULL; |
1517 |
} |
1518 |
|
1519 |
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function) |
1520 |
{ |
1521 |
bus = pci_find_bus(bus, bus_num); |
1522 |
|
1523 |
if (!bus)
|
1524 |
return NULL; |
1525 |
|
1526 |
return bus->devices[PCI_DEVFN(slot, function)];
|
1527 |
} |
1528 |
|
1529 |
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base) |
1530 |
{ |
1531 |
PCIDevice *pci_dev = (PCIDevice *)qdev; |
1532 |
PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev); |
1533 |
PCIBus *bus; |
1534 |
int devfn, rc;
|
1535 |
|
1536 |
/* initialize cap_present for pci_is_express() and pci_config_size() */
|
1537 |
if (info->is_express) {
|
1538 |
pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1539 |
} |
1540 |
|
1541 |
bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev)); |
1542 |
devfn = pci_dev->devfn; |
1543 |
pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn, |
1544 |
info->config_read, info->config_write, |
1545 |
info->is_bridge); |
1546 |
if (pci_dev == NULL) |
1547 |
return -1; |
1548 |
rc = info->init(pci_dev); |
1549 |
if (rc != 0) { |
1550 |
do_pci_unregister_device(pci_dev); |
1551 |
return rc;
|
1552 |
} |
1553 |
|
1554 |
/* rom loading */
|
1555 |
if (pci_dev->romfile == NULL && info->romfile != NULL) |
1556 |
pci_dev->romfile = qemu_strdup(info->romfile); |
1557 |
pci_add_option_rom(pci_dev); |
1558 |
|
1559 |
if (bus->hotplug) {
|
1560 |
/* lower layer must check qdev->hotplugged */
|
1561 |
rc = bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
|
1562 |
if (rc != 0) { |
1563 |
int r = pci_unregister_device(&pci_dev->qdev);
|
1564 |
assert(!r); |
1565 |
return rc;
|
1566 |
} |
1567 |
} |
1568 |
return 0; |
1569 |
} |
1570 |
|
1571 |
static int pci_unplug_device(DeviceState *qdev) |
1572 |
{ |
1573 |
PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev); |
1574 |
|
1575 |
return dev->bus->hotplug(dev->bus->hotplug_qdev, dev, 0); |
1576 |
} |
1577 |
|
1578 |
void pci_qdev_register(PCIDeviceInfo *info)
|
1579 |
{ |
1580 |
info->qdev.init = pci_qdev_init; |
1581 |
info->qdev.unplug = pci_unplug_device; |
1582 |
info->qdev.exit = pci_unregister_device; |
1583 |
info->qdev.bus_info = &pci_bus_info; |
1584 |
qdev_register(&info->qdev); |
1585 |
} |
1586 |
|
1587 |
void pci_qdev_register_many(PCIDeviceInfo *info)
|
1588 |
{ |
1589 |
while (info->qdev.name) {
|
1590 |
pci_qdev_register(info); |
1591 |
info++; |
1592 |
} |
1593 |
} |
1594 |
|
1595 |
PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction, |
1596 |
const char *name) |
1597 |
{ |
1598 |
DeviceState *dev; |
1599 |
|
1600 |
dev = qdev_create(&bus->qbus, name); |
1601 |
qdev_prop_set_uint32(dev, "addr", devfn);
|
1602 |
qdev_prop_set_bit(dev, "multifunction", multifunction);
|
1603 |
return DO_UPCAST(PCIDevice, qdev, dev);
|
1604 |
} |
1605 |
|
1606 |
PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
|
1607 |
bool multifunction,
|
1608 |
const char *name) |
1609 |
{ |
1610 |
PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name); |
1611 |
qdev_init_nofail(&dev->qdev); |
1612 |
return dev;
|
1613 |
} |
1614 |
|
1615 |
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name) |
1616 |
{ |
1617 |
return pci_create_multifunction(bus, devfn, false, name); |
1618 |
} |
1619 |
|
1620 |
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) |
1621 |
{ |
1622 |
return pci_create_simple_multifunction(bus, devfn, false, name); |
1623 |
} |
1624 |
|
1625 |
static int pci_find_space(PCIDevice *pdev, uint8_t size) |
1626 |
{ |
1627 |
int config_size = pci_config_size(pdev);
|
1628 |
int offset = PCI_CONFIG_HEADER_SIZE;
|
1629 |
int i;
|
1630 |
for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
|
1631 |
if (pdev->used[i])
|
1632 |
offset = i + 1;
|
1633 |
else if (i - offset + 1 == size) |
1634 |
return offset;
|
1635 |
return 0; |
1636 |
} |
1637 |
|
1638 |
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
|
1639 |
uint8_t *prev_p) |
1640 |
{ |
1641 |
uint8_t next, prev; |
1642 |
|
1643 |
if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
|
1644 |
return 0; |
1645 |
|
1646 |
for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
|
1647 |
prev = next + PCI_CAP_LIST_NEXT) |
1648 |
if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
|
1649 |
break;
|
1650 |
|
1651 |
if (prev_p)
|
1652 |
*prev_p = prev; |
1653 |
return next;
|
1654 |
} |
1655 |
|
1656 |
static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type) |
1657 |
{ |
1658 |
cpu_register_physical_memory(addr, size, pdev->rom_offset); |
1659 |
} |
1660 |
|
1661 |
/* Add an option rom for the device */
|
1662 |
static int pci_add_option_rom(PCIDevice *pdev) |
1663 |
{ |
1664 |
int size;
|
1665 |
char *path;
|
1666 |
void *ptr;
|
1667 |
char name[32]; |
1668 |
|
1669 |
if (!pdev->romfile)
|
1670 |
return 0; |
1671 |
if (strlen(pdev->romfile) == 0) |
1672 |
return 0; |
1673 |
|
1674 |
if (!pdev->rom_bar) {
|
1675 |
/*
|
1676 |
* Load rom via fw_cfg instead of creating a rom bar,
|
1677 |
* for 0.11 compatibility.
|
1678 |
*/
|
1679 |
int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
|
1680 |
if (class == 0x0300) { |
1681 |
rom_add_vga(pdev->romfile); |
1682 |
} else {
|
1683 |
rom_add_option(pdev->romfile); |
1684 |
} |
1685 |
return 0; |
1686 |
} |
1687 |
|
1688 |
path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile); |
1689 |
if (path == NULL) { |
1690 |
path = qemu_strdup(pdev->romfile); |
1691 |
} |
1692 |
|
1693 |
size = get_image_size(path); |
1694 |
if (size < 0) { |
1695 |
error_report("%s: failed to find romfile \"%s\"",
|
1696 |
__FUNCTION__, pdev->romfile); |
1697 |
return -1; |
1698 |
} |
1699 |
if (size & (size - 1)) { |
1700 |
size = 1 << qemu_fls(size);
|
1701 |
} |
1702 |
|
1703 |
if (pdev->qdev.info->vmsd)
|
1704 |
snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->vmsd->name); |
1705 |
else
|
1706 |
snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->name); |
1707 |
pdev->rom_offset = qemu_ram_alloc(&pdev->qdev, name, size); |
1708 |
|
1709 |
ptr = qemu_get_ram_ptr(pdev->rom_offset); |
1710 |
load_image(path, ptr); |
1711 |
qemu_free(path); |
1712 |
|
1713 |
pci_register_bar(pdev, PCI_ROM_SLOT, size, |
1714 |
0, pci_map_option_rom);
|
1715 |
|
1716 |
return 0; |
1717 |
} |
1718 |
|
1719 |
static void pci_del_option_rom(PCIDevice *pdev) |
1720 |
{ |
1721 |
if (!pdev->rom_offset)
|
1722 |
return;
|
1723 |
|
1724 |
qemu_ram_free(pdev->rom_offset); |
1725 |
pdev->rom_offset = 0;
|
1726 |
} |
1727 |
|
1728 |
/*
|
1729 |
* if !offset
|
1730 |
* Reserve space and add capability to the linked list in pci config space
|
1731 |
*
|
1732 |
* if offset = 0,
|
1733 |
* Find and reserve space and add capability to the linked list
|
1734 |
* in pci config space */
|
1735 |
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
|
1736 |
uint8_t offset, uint8_t size) |
1737 |
{ |
1738 |
uint8_t *config; |
1739 |
if (!offset) {
|
1740 |
offset = pci_find_space(pdev, size); |
1741 |
if (!offset) {
|
1742 |
return -ENOSPC;
|
1743 |
} |
1744 |
} |
1745 |
|
1746 |
config = pdev->config + offset; |
1747 |
config[PCI_CAP_LIST_ID] = cap_id; |
1748 |
config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; |
1749 |
pdev->config[PCI_CAPABILITY_LIST] = offset; |
1750 |
pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; |
1751 |
memset(pdev->used + offset, 0xFF, size);
|
1752 |
/* Make capability read-only by default */
|
1753 |
memset(pdev->wmask + offset, 0, size);
|
1754 |
/* Check capability by default */
|
1755 |
memset(pdev->cmask + offset, 0xFF, size);
|
1756 |
return offset;
|
1757 |
} |
1758 |
|
1759 |
/* Unlink capability from the pci config space. */
|
1760 |
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1761 |
{ |
1762 |
uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); |
1763 |
if (!offset)
|
1764 |
return;
|
1765 |
pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; |
1766 |
/* Make capability writeable again */
|
1767 |
memset(pdev->wmask + offset, 0xff, size);
|
1768 |
memset(pdev->w1cmask + offset, 0, size);
|
1769 |
/* Clear cmask as device-specific registers can't be checked */
|
1770 |
memset(pdev->cmask + offset, 0, size);
|
1771 |
memset(pdev->used + offset, 0, size);
|
1772 |
|
1773 |
if (!pdev->config[PCI_CAPABILITY_LIST])
|
1774 |
pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; |
1775 |
} |
1776 |
|
1777 |
/* Reserve space for capability at a known offset (to call after load). */
|
1778 |
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
|
1779 |
{ |
1780 |
memset(pdev->used + offset, 0xff, size);
|
1781 |
} |
1782 |
|
1783 |
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) |
1784 |
{ |
1785 |
return pci_find_capability_list(pdev, cap_id, NULL); |
1786 |
} |
1787 |
|
1788 |
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
1789 |
{ |
1790 |
PCIDevice *d = (PCIDevice *)dev; |
1791 |
const pci_class_desc *desc;
|
1792 |
char ctxt[64]; |
1793 |
PCIIORegion *r; |
1794 |
int i, class;
|
1795 |
|
1796 |
class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
1797 |
desc = pci_class_descriptions; |
1798 |
while (desc->desc && class != desc->class)
|
1799 |
desc++; |
1800 |
if (desc->desc) {
|
1801 |
snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); |
1802 |
} else {
|
1803 |
snprintf(ctxt, sizeof(ctxt), "Class %04x", class); |
1804 |
} |
1805 |
|
1806 |
monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
|
1807 |
"pci id %04x:%04x (sub %04x:%04x)\n",
|
1808 |
indent, "", ctxt,
|
1809 |
d->config[PCI_SECONDARY_BUS], |
1810 |
PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), |
1811 |
pci_get_word(d->config + PCI_VENDOR_ID), |
1812 |
pci_get_word(d->config + PCI_DEVICE_ID), |
1813 |
pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), |
1814 |
pci_get_word(d->config + PCI_SUBSYSTEM_ID)); |
1815 |
for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1816 |
r = &d->io_regions[i]; |
1817 |
if (!r->size)
|
1818 |
continue;
|
1819 |
monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
|
1820 |
" [0x%"FMT_PCIBUS"]\n", |
1821 |
indent, "",
|
1822 |
i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", |
1823 |
r->addr, r->addr + r->size - 1);
|
1824 |
} |
1825 |
} |
1826 |
|
1827 |
static char *pcibus_get_dev_path(DeviceState *dev) |
1828 |
{ |
1829 |
PCIDevice *d = (PCIDevice *)dev; |
1830 |
char path[16]; |
1831 |
|
1832 |
snprintf(path, sizeof(path), "%04x:%02x:%02x.%x", |
1833 |
pci_find_domain(d->bus), d->config[PCI_SECONDARY_BUS], |
1834 |
PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); |
1835 |
|
1836 |
return strdup(path);
|
1837 |
} |
1838 |
|