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