root / hw / pci.c @ a9f49946
History | View | Annotate | Download (36.7 kB)
1 |
/*
|
---|---|
2 |
* QEMU PCI bus manager
|
3 |
*
|
4 |
* Copyright (c) 2004 Fabrice Bellard
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include "hw.h" |
25 |
#include "pci.h" |
26 |
#include "pci_host.h" |
27 |
#include "monitor.h" |
28 |
#include "net.h" |
29 |
#include "sysemu.h" |
30 |
|
31 |
//#define DEBUG_PCI
|
32 |
#ifdef DEBUG_PCI
|
33 |
# define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
34 |
#else
|
35 |
# define PCI_DPRINTF(format, ...) do { } while (0) |
36 |
#endif
|
37 |
|
38 |
struct PCIBus {
|
39 |
BusState qbus; |
40 |
int devfn_min;
|
41 |
pci_set_irq_fn set_irq; |
42 |
pci_map_irq_fn map_irq; |
43 |
pci_hotplug_fn hotplug; |
44 |
uint32_t config_reg; /* XXX: suppress */
|
45 |
void *irq_opaque;
|
46 |
PCIDevice *devices[256];
|
47 |
PCIDevice *parent_dev; |
48 |
|
49 |
QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
|
50 |
QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
|
51 |
|
52 |
/* The bus IRQ state is the logical OR of the connected devices.
|
53 |
Keep a count of the number of devices with raised IRQs. */
|
54 |
int nirq;
|
55 |
int *irq_count;
|
56 |
}; |
57 |
|
58 |
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
59 |
|
60 |
static struct BusInfo pci_bus_info = { |
61 |
.name = "PCI",
|
62 |
.size = sizeof(PCIBus),
|
63 |
.print_dev = pcibus_dev_print, |
64 |
.props = (Property[]) { |
65 |
DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), |
66 |
DEFINE_PROP_END_OF_LIST() |
67 |
} |
68 |
}; |
69 |
|
70 |
static void pci_update_mappings(PCIDevice *d); |
71 |
static void pci_set_irq(void *opaque, int irq_num, int level); |
72 |
|
73 |
target_phys_addr_t pci_mem_base; |
74 |
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
|
75 |
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
|
76 |
|
77 |
struct PCIHostBus {
|
78 |
int domain;
|
79 |
struct PCIBus *bus;
|
80 |
QLIST_ENTRY(PCIHostBus) next; |
81 |
}; |
82 |
static QLIST_HEAD(, PCIHostBus) host_buses;
|
83 |
|
84 |
static const VMStateDescription vmstate_pcibus = { |
85 |
.name = "PCIBUS",
|
86 |
.version_id = 1,
|
87 |
.minimum_version_id = 1,
|
88 |
.minimum_version_id_old = 1,
|
89 |
.fields = (VMStateField []) { |
90 |
VMSTATE_INT32_EQUAL(nirq, PCIBus), |
91 |
VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
|
92 |
VMSTATE_END_OF_LIST() |
93 |
} |
94 |
}; |
95 |
|
96 |
static int pci_bar(PCIDevice *d, int reg) |
97 |
{ |
98 |
uint8_t type; |
99 |
|
100 |
if (reg != PCI_ROM_SLOT)
|
101 |
return PCI_BASE_ADDRESS_0 + reg * 4; |
102 |
|
103 |
type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
104 |
return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
|
105 |
} |
106 |
|
107 |
static void pci_device_reset(PCIDevice *dev) |
108 |
{ |
109 |
int r;
|
110 |
|
111 |
memset(dev->irq_state, 0, sizeof dev->irq_state); |
112 |
dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | |
113 |
PCI_COMMAND_MASTER); |
114 |
dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
|
115 |
dev->config[PCI_INTERRUPT_LINE] = 0x0;
|
116 |
for (r = 0; r < PCI_NUM_REGIONS; ++r) { |
117 |
if (!dev->io_regions[r].size) {
|
118 |
continue;
|
119 |
} |
120 |
pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type); |
121 |
} |
122 |
pci_update_mappings(dev); |
123 |
} |
124 |
|
125 |
static void pci_bus_reset(void *opaque) |
126 |
{ |
127 |
PCIBus *bus = opaque; |
128 |
int i;
|
129 |
|
130 |
for (i = 0; i < bus->nirq; i++) { |
131 |
bus->irq_count[i] = 0;
|
132 |
} |
133 |
for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
134 |
if (bus->devices[i]) {
|
135 |
pci_device_reset(bus->devices[i]); |
136 |
} |
137 |
} |
138 |
} |
139 |
|
140 |
static void pci_host_bus_register(int domain, PCIBus *bus) |
141 |
{ |
142 |
struct PCIHostBus *host;
|
143 |
host = qemu_mallocz(sizeof(*host));
|
144 |
host->domain = domain; |
145 |
host->bus = bus; |
146 |
QLIST_INSERT_HEAD(&host_buses, host, next); |
147 |
} |
148 |
|
149 |
PCIBus *pci_find_host_bus(int domain)
|
150 |
{ |
151 |
struct PCIHostBus *host;
|
152 |
|
153 |
QLIST_FOREACH(host, &host_buses, next) { |
154 |
if (host->domain == domain) {
|
155 |
return host->bus;
|
156 |
} |
157 |
} |
158 |
|
159 |
return NULL; |
160 |
} |
161 |
|
162 |
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
|
163 |
const char *name, int devfn_min) |
164 |
{ |
165 |
static int nbus = 0; |
166 |
|
167 |
qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name); |
168 |
bus->devfn_min = devfn_min; |
169 |
|
170 |
/* host bridge */
|
171 |
QLIST_INIT(&bus->child); |
172 |
pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */ |
173 |
|
174 |
vmstate_register(nbus++, &vmstate_pcibus, bus); |
175 |
qemu_register_reset(pci_bus_reset, bus); |
176 |
} |
177 |
|
178 |
PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min) |
179 |
{ |
180 |
PCIBus *bus; |
181 |
|
182 |
bus = qemu_mallocz(sizeof(*bus));
|
183 |
bus->qbus.qdev_allocated = 1;
|
184 |
pci_bus_new_inplace(bus, parent, name, devfn_min); |
185 |
return bus;
|
186 |
} |
187 |
|
188 |
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
|
189 |
void *irq_opaque, int nirq) |
190 |
{ |
191 |
bus->set_irq = set_irq; |
192 |
bus->map_irq = map_irq; |
193 |
bus->irq_opaque = irq_opaque; |
194 |
bus->nirq = nirq; |
195 |
bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0])); |
196 |
} |
197 |
|
198 |
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
|
199 |
{ |
200 |
bus->qbus.allow_hotplug = 1;
|
201 |
bus->hotplug = hotplug; |
202 |
} |
203 |
|
204 |
PCIBus *pci_register_bus(DeviceState *parent, const char *name, |
205 |
pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, |
206 |
void *irq_opaque, int devfn_min, int nirq) |
207 |
{ |
208 |
PCIBus *bus; |
209 |
|
210 |
bus = pci_bus_new(parent, name, devfn_min); |
211 |
pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq); |
212 |
return bus;
|
213 |
} |
214 |
|
215 |
static void pci_register_secondary_bus(PCIBus *parent, |
216 |
PCIBus *bus, |
217 |
PCIDevice *dev, |
218 |
pci_map_irq_fn map_irq, |
219 |
const char *name) |
220 |
{ |
221 |
qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name); |
222 |
bus->map_irq = map_irq; |
223 |
bus->parent_dev = dev; |
224 |
|
225 |
QLIST_INIT(&bus->child); |
226 |
QLIST_INSERT_HEAD(&parent->child, bus, sibling); |
227 |
} |
228 |
|
229 |
static void pci_unregister_secondary_bus(PCIBus *bus) |
230 |
{ |
231 |
assert(QLIST_EMPTY(&bus->child)); |
232 |
QLIST_REMOVE(bus, sibling); |
233 |
} |
234 |
|
235 |
int pci_bus_num(PCIBus *s)
|
236 |
{ |
237 |
if (!s->parent_dev)
|
238 |
return 0; /* pci host bridge */ |
239 |
return s->parent_dev->config[PCI_SECONDARY_BUS];
|
240 |
} |
241 |
|
242 |
static uint8_t pci_sub_bus(PCIBus *s)
|
243 |
{ |
244 |
if (!s->parent_dev)
|
245 |
return 255; /* pci host bridge */ |
246 |
return s->parent_dev->config[PCI_SUBORDINATE_BUS];
|
247 |
} |
248 |
|
249 |
static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) |
250 |
{ |
251 |
PCIDevice *s = container_of(pv, PCIDevice, config); |
252 |
uint8_t *config; |
253 |
int i;
|
254 |
|
255 |
assert(size == pci_config_size(s)); |
256 |
config = qemu_malloc(size); |
257 |
|
258 |
qemu_get_buffer(f, config, size); |
259 |
for (i = 0; i < size; ++i) { |
260 |
if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
|
261 |
qemu_free(config); |
262 |
return -EINVAL;
|
263 |
} |
264 |
} |
265 |
memcpy(s->config, config, size); |
266 |
|
267 |
pci_update_mappings(s); |
268 |
|
269 |
qemu_free(config); |
270 |
return 0; |
271 |
} |
272 |
|
273 |
/* just put buffer */
|
274 |
static void put_pci_config_device(QEMUFile *f, void *pv, size_t size) |
275 |
{ |
276 |
const uint8_t *v = pv;
|
277 |
assert(size == pci_config_size(container_of(pv, PCIDevice, config))); |
278 |
qemu_put_buffer(f, v, size); |
279 |
} |
280 |
|
281 |
static VMStateInfo vmstate_info_pci_config = {
|
282 |
.name = "pci config",
|
283 |
.get = get_pci_config_device, |
284 |
.put = put_pci_config_device, |
285 |
}; |
286 |
|
287 |
const VMStateDescription vmstate_pci_device = {
|
288 |
.name = "PCIDevice",
|
289 |
.version_id = 2,
|
290 |
.minimum_version_id = 1,
|
291 |
.minimum_version_id_old = 1,
|
292 |
.fields = (VMStateField []) { |
293 |
VMSTATE_INT32_LE(version_id, PCIDevice), |
294 |
VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
295 |
vmstate_info_pci_config, |
296 |
PCI_CONFIG_SPACE_SIZE), |
297 |
VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
|
298 |
VMSTATE_END_OF_LIST() |
299 |
} |
300 |
}; |
301 |
|
302 |
const VMStateDescription vmstate_pcie_device = {
|
303 |
.name = "PCIDevice",
|
304 |
.version_id = 2,
|
305 |
.minimum_version_id = 1,
|
306 |
.minimum_version_id_old = 1,
|
307 |
.fields = (VMStateField []) { |
308 |
VMSTATE_INT32_LE(version_id, PCIDevice), |
309 |
VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
310 |
vmstate_info_pci_config, |
311 |
PCIE_CONFIG_SPACE_SIZE), |
312 |
VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
|
313 |
VMSTATE_END_OF_LIST() |
314 |
} |
315 |
}; |
316 |
|
317 |
static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s) |
318 |
{ |
319 |
return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
|
320 |
} |
321 |
|
322 |
void pci_device_save(PCIDevice *s, QEMUFile *f)
|
323 |
{ |
324 |
vmstate_save_state(f, pci_get_vmstate(s), s); |
325 |
} |
326 |
|
327 |
int pci_device_load(PCIDevice *s, QEMUFile *f)
|
328 |
{ |
329 |
return vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
|
330 |
} |
331 |
|
332 |
static int pci_set_default_subsystem_id(PCIDevice *pci_dev) |
333 |
{ |
334 |
uint16_t *id; |
335 |
|
336 |
id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
|
337 |
id[0] = cpu_to_le16(pci_default_sub_vendor_id);
|
338 |
id[1] = cpu_to_le16(pci_default_sub_device_id);
|
339 |
return 0; |
340 |
} |
341 |
|
342 |
/*
|
343 |
* Parse [[<domain>:]<bus>:]<slot>, return -1 on error
|
344 |
*/
|
345 |
static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp) |
346 |
{ |
347 |
const char *p; |
348 |
char *e;
|
349 |
unsigned long val; |
350 |
unsigned long dom = 0, bus = 0; |
351 |
unsigned slot = 0; |
352 |
|
353 |
p = addr; |
354 |
val = strtoul(p, &e, 16);
|
355 |
if (e == p)
|
356 |
return -1; |
357 |
if (*e == ':') { |
358 |
bus = val; |
359 |
p = e + 1;
|
360 |
val = strtoul(p, &e, 16);
|
361 |
if (e == p)
|
362 |
return -1; |
363 |
if (*e == ':') { |
364 |
dom = bus; |
365 |
bus = val; |
366 |
p = e + 1;
|
367 |
val = strtoul(p, &e, 16);
|
368 |
if (e == p)
|
369 |
return -1; |
370 |
} |
371 |
} |
372 |
|
373 |
if (dom > 0xffff || bus > 0xff || val > 0x1f) |
374 |
return -1; |
375 |
|
376 |
slot = val; |
377 |
|
378 |
if (*e)
|
379 |
return -1; |
380 |
|
381 |
/* Note: QEMU doesn't implement domains other than 0 */
|
382 |
if (!pci_find_bus(pci_find_host_bus(dom), bus))
|
383 |
return -1; |
384 |
|
385 |
*domp = dom; |
386 |
*busp = bus; |
387 |
*slotp = slot; |
388 |
return 0; |
389 |
} |
390 |
|
391 |
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp, |
392 |
unsigned *slotp)
|
393 |
{ |
394 |
/* strip legacy tag */
|
395 |
if (!strncmp(addr, "pci_addr=", 9)) { |
396 |
addr += 9;
|
397 |
} |
398 |
if (pci_parse_devaddr(addr, domp, busp, slotp)) {
|
399 |
monitor_printf(mon, "Invalid pci address\n");
|
400 |
return -1; |
401 |
} |
402 |
return 0; |
403 |
} |
404 |
|
405 |
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr) |
406 |
{ |
407 |
int dom, bus;
|
408 |
unsigned slot;
|
409 |
|
410 |
if (!devaddr) {
|
411 |
*devfnp = -1;
|
412 |
return pci_find_bus(pci_find_host_bus(0), 0); |
413 |
} |
414 |
|
415 |
if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) { |
416 |
return NULL; |
417 |
} |
418 |
|
419 |
*devfnp = slot << 3;
|
420 |
return pci_find_bus(pci_find_host_bus(0), bus); |
421 |
} |
422 |
|
423 |
static void pci_init_cmask(PCIDevice *dev) |
424 |
{ |
425 |
pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
|
426 |
pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
|
427 |
dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; |
428 |
dev->cmask[PCI_REVISION_ID] = 0xff;
|
429 |
dev->cmask[PCI_CLASS_PROG] = 0xff;
|
430 |
pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
|
431 |
dev->cmask[PCI_HEADER_TYPE] = 0xff;
|
432 |
dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
|
433 |
} |
434 |
|
435 |
static void pci_init_wmask(PCIDevice *dev) |
436 |
{ |
437 |
int i;
|
438 |
int config_size = pci_config_size(dev);
|
439 |
|
440 |
dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
|
441 |
dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
|
442 |
pci_set_word(dev->wmask + PCI_COMMAND, |
443 |
PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
444 |
for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
|
445 |
dev->wmask[i] = 0xff;
|
446 |
} |
447 |
|
448 |
static void pci_config_alloc(PCIDevice *pci_dev) |
449 |
{ |
450 |
int config_size = pci_config_size(pci_dev);
|
451 |
|
452 |
pci_dev->config = qemu_mallocz(config_size); |
453 |
pci_dev->cmask = qemu_mallocz(config_size); |
454 |
pci_dev->wmask = qemu_mallocz(config_size); |
455 |
pci_dev->used = qemu_mallocz(config_size); |
456 |
} |
457 |
|
458 |
static void pci_config_free(PCIDevice *pci_dev) |
459 |
{ |
460 |
qemu_free(pci_dev->config); |
461 |
qemu_free(pci_dev->cmask); |
462 |
qemu_free(pci_dev->wmask); |
463 |
qemu_free(pci_dev->used); |
464 |
} |
465 |
|
466 |
/* -1 for devfn means auto assign */
|
467 |
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
|
468 |
const char *name, int devfn, |
469 |
PCIConfigReadFunc *config_read, |
470 |
PCIConfigWriteFunc *config_write) |
471 |
{ |
472 |
if (devfn < 0) { |
473 |
for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) { |
474 |
if (!bus->devices[devfn])
|
475 |
goto found;
|
476 |
} |
477 |
return NULL; |
478 |
found: ;
|
479 |
} else if (bus->devices[devfn]) { |
480 |
return NULL; |
481 |
} |
482 |
pci_dev->bus = bus; |
483 |
pci_dev->devfn = devfn; |
484 |
pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
|
485 |
memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state)); |
486 |
pci_config_alloc(pci_dev); |
487 |
pci_set_default_subsystem_id(pci_dev); |
488 |
pci_init_cmask(pci_dev); |
489 |
pci_init_wmask(pci_dev); |
490 |
|
491 |
if (!config_read)
|
492 |
config_read = pci_default_read_config; |
493 |
if (!config_write)
|
494 |
config_write = pci_default_write_config; |
495 |
pci_dev->config_read = config_read; |
496 |
pci_dev->config_write = config_write; |
497 |
bus->devices[devfn] = pci_dev; |
498 |
pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS); |
499 |
pci_dev->version_id = 2; /* Current pci device vmstate version */ |
500 |
return pci_dev;
|
501 |
} |
502 |
|
503 |
PCIDevice *pci_register_device(PCIBus *bus, const char *name, |
504 |
int instance_size, int devfn, |
505 |
PCIConfigReadFunc *config_read, |
506 |
PCIConfigWriteFunc *config_write) |
507 |
{ |
508 |
PCIDevice *pci_dev; |
509 |
|
510 |
pci_dev = qemu_mallocz(instance_size); |
511 |
pci_dev = do_pci_register_device(pci_dev, bus, name, devfn, |
512 |
config_read, config_write); |
513 |
return pci_dev;
|
514 |
} |
515 |
static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
|
516 |
{ |
517 |
return addr + pci_mem_base;
|
518 |
} |
519 |
|
520 |
static void pci_unregister_io_regions(PCIDevice *pci_dev) |
521 |
{ |
522 |
PCIIORegion *r; |
523 |
int i;
|
524 |
|
525 |
for(i = 0; i < PCI_NUM_REGIONS; i++) { |
526 |
r = &pci_dev->io_regions[i]; |
527 |
if (!r->size || r->addr == PCI_BAR_UNMAPPED)
|
528 |
continue;
|
529 |
if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
|
530 |
isa_unassign_ioport(r->addr, r->size); |
531 |
} else {
|
532 |
cpu_register_physical_memory(pci_to_cpu_addr(r->addr), |
533 |
r->size, |
534 |
IO_MEM_UNASSIGNED); |
535 |
} |
536 |
} |
537 |
} |
538 |
|
539 |
static int pci_unregister_device(DeviceState *dev) |
540 |
{ |
541 |
PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev); |
542 |
PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info); |
543 |
int ret = 0; |
544 |
|
545 |
if (info->exit)
|
546 |
ret = info->exit(pci_dev); |
547 |
if (ret)
|
548 |
return ret;
|
549 |
|
550 |
pci_unregister_io_regions(pci_dev); |
551 |
|
552 |
qemu_free_irqs(pci_dev->irq); |
553 |
pci_dev->bus->devices[pci_dev->devfn] = NULL;
|
554 |
pci_config_free(pci_dev); |
555 |
return 0; |
556 |
} |
557 |
|
558 |
void pci_register_bar(PCIDevice *pci_dev, int region_num, |
559 |
pcibus_t size, int type,
|
560 |
PCIMapIORegionFunc *map_func) |
561 |
{ |
562 |
PCIIORegion *r; |
563 |
uint32_t addr; |
564 |
pcibus_t wmask; |
565 |
|
566 |
if ((unsigned int)region_num >= PCI_NUM_REGIONS) |
567 |
return;
|
568 |
|
569 |
if (size & (size-1)) { |
570 |
fprintf(stderr, "ERROR: PCI region size must be pow2 "
|
571 |
"type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size); |
572 |
exit(1);
|
573 |
} |
574 |
|
575 |
r = &pci_dev->io_regions[region_num]; |
576 |
r->addr = PCI_BAR_UNMAPPED; |
577 |
r->size = size; |
578 |
r->type = type; |
579 |
r->map_func = map_func; |
580 |
|
581 |
wmask = ~(size - 1);
|
582 |
addr = pci_bar(pci_dev, region_num); |
583 |
if (region_num == PCI_ROM_SLOT) {
|
584 |
/* ROM enable bit is writeable */
|
585 |
wmask |= PCI_ROM_ADDRESS_ENABLE; |
586 |
} |
587 |
pci_set_long(pci_dev->config + addr, type); |
588 |
if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
589 |
r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
590 |
pci_set_quad(pci_dev->wmask + addr, wmask); |
591 |
pci_set_quad(pci_dev->cmask + addr, ~0ULL);
|
592 |
} else {
|
593 |
pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
|
594 |
pci_set_long(pci_dev->cmask + addr, 0xffffffff);
|
595 |
} |
596 |
} |
597 |
|
598 |
static void pci_update_mappings(PCIDevice *d) |
599 |
{ |
600 |
PCIIORegion *r; |
601 |
int cmd, i;
|
602 |
pcibus_t last_addr, new_addr; |
603 |
|
604 |
cmd = pci_get_word(d->config + PCI_COMMAND); |
605 |
for(i = 0; i < PCI_NUM_REGIONS; i++) { |
606 |
r = &d->io_regions[i]; |
607 |
if (r->size != 0) { |
608 |
if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
609 |
if (cmd & PCI_COMMAND_IO) {
|
610 |
new_addr = pci_get_long(d->config + pci_bar(d, i)); |
611 |
new_addr = new_addr & ~(r->size - 1);
|
612 |
last_addr = new_addr + r->size - 1;
|
613 |
/* NOTE: we have only 64K ioports on PC */
|
614 |
if (last_addr <= new_addr || new_addr == 0 || |
615 |
last_addr >= 0x10000) {
|
616 |
new_addr = PCI_BAR_UNMAPPED; |
617 |
} |
618 |
} else {
|
619 |
new_addr = PCI_BAR_UNMAPPED; |
620 |
} |
621 |
} else {
|
622 |
if (cmd & PCI_COMMAND_MEMORY) {
|
623 |
if (r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
|
624 |
new_addr = pci_get_quad(d->config + pci_bar(d, i)); |
625 |
} else {
|
626 |
new_addr = pci_get_long(d->config + pci_bar(d, i)); |
627 |
} |
628 |
/* the ROM slot has a specific enable bit */
|
629 |
if (i == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE))
|
630 |
goto no_mem_map;
|
631 |
new_addr = new_addr & ~(r->size - 1);
|
632 |
last_addr = new_addr + r->size - 1;
|
633 |
/* NOTE: we do not support wrapping */
|
634 |
/* XXX: as we cannot support really dynamic
|
635 |
mappings, we handle specific values as invalid
|
636 |
mappings. */
|
637 |
if (last_addr <= new_addr || new_addr == 0 || |
638 |
last_addr == PCI_BAR_UNMAPPED || |
639 |
|
640 |
/* Now pcibus_t is 64bit.
|
641 |
* Check if 32 bit BAR wrap around explicitly.
|
642 |
* Without this, PC ide doesn't work well.
|
643 |
* TODO: remove this work around.
|
644 |
*/
|
645 |
(!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) && |
646 |
last_addr >= UINT32_MAX) || |
647 |
|
648 |
/*
|
649 |
* OS is allowed to set BAR beyond its addressable
|
650 |
* bits. For example, 32 bit OS can set 64bit bar
|
651 |
* to >4G. Check it.
|
652 |
*/
|
653 |
last_addr >= TARGET_PHYS_ADDR_MAX) { |
654 |
new_addr = PCI_BAR_UNMAPPED; |
655 |
} |
656 |
} else {
|
657 |
no_mem_map:
|
658 |
new_addr = PCI_BAR_UNMAPPED; |
659 |
} |
660 |
} |
661 |
/* now do the real mapping */
|
662 |
if (new_addr != r->addr) {
|
663 |
if (r->addr != PCI_BAR_UNMAPPED) {
|
664 |
if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
665 |
int class;
|
666 |
/* NOTE: specific hack for IDE in PC case:
|
667 |
only one byte must be mapped. */
|
668 |
class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
669 |
if (class == 0x0101 && r->size == 4) { |
670 |
isa_unassign_ioport(r->addr + 2, 1); |
671 |
} else {
|
672 |
isa_unassign_ioport(r->addr, r->size); |
673 |
} |
674 |
} else {
|
675 |
cpu_register_physical_memory(pci_to_cpu_addr(r->addr), |
676 |
r->size, |
677 |
IO_MEM_UNASSIGNED); |
678 |
qemu_unregister_coalesced_mmio(r->addr, r->size); |
679 |
} |
680 |
} |
681 |
r->addr = new_addr; |
682 |
if (r->addr != PCI_BAR_UNMAPPED) {
|
683 |
r->map_func(d, i, r->addr, r->size, r->type); |
684 |
} |
685 |
} |
686 |
} |
687 |
} |
688 |
} |
689 |
|
690 |
uint32_t pci_default_read_config(PCIDevice *d, |
691 |
uint32_t address, int len)
|
692 |
{ |
693 |
uint32_t val = 0;
|
694 |
assert(len == 1 || len == 2 || len == 4); |
695 |
len = MIN(len, pci_config_size(d) - address); |
696 |
memcpy(&val, d->config + address, len); |
697 |
return le32_to_cpu(val);
|
698 |
} |
699 |
|
700 |
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) |
701 |
{ |
702 |
uint8_t orig[PCI_CONFIG_SPACE_SIZE]; |
703 |
int i;
|
704 |
uint32_t config_size = pci_config_size(d); |
705 |
|
706 |
/* not efficient, but simple */
|
707 |
memcpy(orig, d->config, PCI_CONFIG_SPACE_SIZE); |
708 |
for(i = 0; i < l && addr < config_size; val >>= 8, ++i, ++addr) { |
709 |
uint8_t wmask = d->wmask[addr]; |
710 |
d->config[addr] = (d->config[addr] & ~wmask) | (val & wmask); |
711 |
} |
712 |
if (memcmp(orig + PCI_BASE_ADDRESS_0, d->config + PCI_BASE_ADDRESS_0, 24) |
713 |
|| ((orig[PCI_COMMAND] ^ d->config[PCI_COMMAND]) |
714 |
& (PCI_COMMAND_MEMORY | PCI_COMMAND_IO))) |
715 |
pci_update_mappings(d); |
716 |
} |
717 |
|
718 |
/***********************************************************/
|
719 |
/* generic PCI irq support */
|
720 |
|
721 |
/* 0 <= irq_num <= 3. level must be 0 or 1 */
|
722 |
static void pci_set_irq(void *opaque, int irq_num, int level) |
723 |
{ |
724 |
PCIDevice *pci_dev = opaque; |
725 |
PCIBus *bus; |
726 |
int change;
|
727 |
|
728 |
change = level - pci_dev->irq_state[irq_num]; |
729 |
if (!change)
|
730 |
return;
|
731 |
|
732 |
pci_dev->irq_state[irq_num] = level; |
733 |
for (;;) {
|
734 |
bus = pci_dev->bus; |
735 |
irq_num = bus->map_irq(pci_dev, irq_num); |
736 |
if (bus->set_irq)
|
737 |
break;
|
738 |
pci_dev = bus->parent_dev; |
739 |
} |
740 |
bus->irq_count[irq_num] += change; |
741 |
bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
|
742 |
} |
743 |
|
744 |
/***********************************************************/
|
745 |
/* monitor info on PCI */
|
746 |
|
747 |
typedef struct { |
748 |
uint16_t class; |
749 |
const char *desc; |
750 |
} pci_class_desc; |
751 |
|
752 |
static const pci_class_desc pci_class_descriptions[] = |
753 |
{ |
754 |
{ 0x0100, "SCSI controller"}, |
755 |
{ 0x0101, "IDE controller"}, |
756 |
{ 0x0102, "Floppy controller"}, |
757 |
{ 0x0103, "IPI controller"}, |
758 |
{ 0x0104, "RAID controller"}, |
759 |
{ 0x0106, "SATA controller"}, |
760 |
{ 0x0107, "SAS controller"}, |
761 |
{ 0x0180, "Storage controller"}, |
762 |
{ 0x0200, "Ethernet controller"}, |
763 |
{ 0x0201, "Token Ring controller"}, |
764 |
{ 0x0202, "FDDI controller"}, |
765 |
{ 0x0203, "ATM controller"}, |
766 |
{ 0x0280, "Network controller"}, |
767 |
{ 0x0300, "VGA controller"}, |
768 |
{ 0x0301, "XGA controller"}, |
769 |
{ 0x0302, "3D controller"}, |
770 |
{ 0x0380, "Display controller"}, |
771 |
{ 0x0400, "Video controller"}, |
772 |
{ 0x0401, "Audio controller"}, |
773 |
{ 0x0402, "Phone"}, |
774 |
{ 0x0480, "Multimedia controller"}, |
775 |
{ 0x0500, "RAM controller"}, |
776 |
{ 0x0501, "Flash controller"}, |
777 |
{ 0x0580, "Memory controller"}, |
778 |
{ 0x0600, "Host bridge"}, |
779 |
{ 0x0601, "ISA bridge"}, |
780 |
{ 0x0602, "EISA bridge"}, |
781 |
{ 0x0603, "MC bridge"}, |
782 |
{ 0x0604, "PCI bridge"}, |
783 |
{ 0x0605, "PCMCIA bridge"}, |
784 |
{ 0x0606, "NUBUS bridge"}, |
785 |
{ 0x0607, "CARDBUS bridge"}, |
786 |
{ 0x0608, "RACEWAY bridge"}, |
787 |
{ 0x0680, "Bridge"}, |
788 |
{ 0x0c03, "USB controller"}, |
789 |
{ 0, NULL} |
790 |
}; |
791 |
|
792 |
static void pci_info_device(PCIBus *bus, PCIDevice *d) |
793 |
{ |
794 |
Monitor *mon = cur_mon; |
795 |
int i, class;
|
796 |
PCIIORegion *r; |
797 |
const pci_class_desc *desc;
|
798 |
|
799 |
monitor_printf(mon, " Bus %2d, device %3d, function %d:\n",
|
800 |
pci_bus_num(d->bus), |
801 |
PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); |
802 |
class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
803 |
monitor_printf(mon, " ");
|
804 |
desc = pci_class_descriptions; |
805 |
while (desc->desc && class != desc->class)
|
806 |
desc++; |
807 |
if (desc->desc) {
|
808 |
monitor_printf(mon, "%s", desc->desc);
|
809 |
} else {
|
810 |
monitor_printf(mon, "Class %04x", class);
|
811 |
} |
812 |
monitor_printf(mon, ": PCI device %04x:%04x\n",
|
813 |
pci_get_word(d->config + PCI_VENDOR_ID), |
814 |
pci_get_word(d->config + PCI_DEVICE_ID)); |
815 |
|
816 |
if (d->config[PCI_INTERRUPT_PIN] != 0) { |
817 |
monitor_printf(mon, " IRQ %d.\n",
|
818 |
d->config[PCI_INTERRUPT_LINE]); |
819 |
} |
820 |
if (class == 0x0604) { |
821 |
monitor_printf(mon, " BUS %d.\n", d->config[0x19]); |
822 |
} |
823 |
for(i = 0;i < PCI_NUM_REGIONS; i++) { |
824 |
r = &d->io_regions[i]; |
825 |
if (r->size != 0) { |
826 |
monitor_printf(mon, " BAR%d: ", i);
|
827 |
if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
828 |
monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
|
829 |
" [0x%04"FMT_PCIBUS"].\n", |
830 |
r->addr, r->addr + r->size - 1);
|
831 |
} else {
|
832 |
const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ? |
833 |
"64 bit" : "32 bit"; |
834 |
const char *prefetch = |
835 |
r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ? |
836 |
" prefetchable" : ""; |
837 |
|
838 |
monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
|
839 |
" [0x%08"FMT_PCIBUS"].\n", |
840 |
type, prefetch, |
841 |
r->addr, r->addr + r->size - 1);
|
842 |
} |
843 |
} |
844 |
} |
845 |
monitor_printf(mon, " id \"%s\"\n", d->qdev.id ? d->qdev.id : ""); |
846 |
if (class == 0x0604 && d->config[0x19] != 0) { |
847 |
pci_for_each_device(bus, d->config[0x19], pci_info_device);
|
848 |
} |
849 |
} |
850 |
|
851 |
void pci_for_each_device(PCIBus *bus, int bus_num, |
852 |
void (*fn)(PCIBus *b, PCIDevice *d))
|
853 |
{ |
854 |
PCIDevice *d; |
855 |
int devfn;
|
856 |
|
857 |
bus = pci_find_bus(bus, bus_num); |
858 |
if (bus) {
|
859 |
for(devfn = 0; devfn < 256; devfn++) { |
860 |
d = bus->devices[devfn]; |
861 |
if (d)
|
862 |
fn(bus, d); |
863 |
} |
864 |
} |
865 |
} |
866 |
|
867 |
void pci_info(Monitor *mon)
|
868 |
{ |
869 |
struct PCIHostBus *host;
|
870 |
QLIST_FOREACH(host, &host_buses, next) { |
871 |
pci_for_each_device(host->bus, 0, pci_info_device);
|
872 |
} |
873 |
} |
874 |
|
875 |
static const char * const pci_nic_models[] = { |
876 |
"ne2k_pci",
|
877 |
"i82551",
|
878 |
"i82557b",
|
879 |
"i82559er",
|
880 |
"rtl8139",
|
881 |
"e1000",
|
882 |
"pcnet",
|
883 |
"virtio",
|
884 |
NULL
|
885 |
}; |
886 |
|
887 |
static const char * const pci_nic_names[] = { |
888 |
"ne2k_pci",
|
889 |
"i82551",
|
890 |
"i82557b",
|
891 |
"i82559er",
|
892 |
"rtl8139",
|
893 |
"e1000",
|
894 |
"pcnet",
|
895 |
"virtio-net-pci",
|
896 |
NULL
|
897 |
}; |
898 |
|
899 |
/* Initialize a PCI NIC. */
|
900 |
/* FIXME callers should check for failure, but don't */
|
901 |
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model, |
902 |
const char *default_devaddr) |
903 |
{ |
904 |
const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; |
905 |
PCIBus *bus; |
906 |
int devfn;
|
907 |
PCIDevice *pci_dev; |
908 |
DeviceState *dev; |
909 |
int i;
|
910 |
|
911 |
i = qemu_find_nic_model(nd, pci_nic_models, default_model); |
912 |
if (i < 0) |
913 |
return NULL; |
914 |
|
915 |
bus = pci_get_bus_devfn(&devfn, devaddr); |
916 |
if (!bus) {
|
917 |
qemu_error("Invalid PCI device address %s for device %s\n",
|
918 |
devaddr, pci_nic_names[i]); |
919 |
return NULL; |
920 |
} |
921 |
|
922 |
pci_dev = pci_create(bus, devfn, pci_nic_names[i]); |
923 |
dev = &pci_dev->qdev; |
924 |
if (nd->name)
|
925 |
dev->id = qemu_strdup(nd->name); |
926 |
qdev_set_nic_properties(dev, nd); |
927 |
if (qdev_init(dev) < 0) |
928 |
return NULL; |
929 |
return pci_dev;
|
930 |
} |
931 |
|
932 |
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model, |
933 |
const char *default_devaddr) |
934 |
{ |
935 |
PCIDevice *res; |
936 |
|
937 |
if (qemu_show_nic_models(nd->model, pci_nic_models))
|
938 |
exit(0);
|
939 |
|
940 |
res = pci_nic_init(nd, default_model, default_devaddr); |
941 |
if (!res)
|
942 |
exit(1);
|
943 |
return res;
|
944 |
} |
945 |
|
946 |
typedef struct { |
947 |
PCIDevice dev; |
948 |
PCIBus bus; |
949 |
uint32_t vid; |
950 |
uint32_t did; |
951 |
} PCIBridge; |
952 |
|
953 |
static void pci_bridge_write_config(PCIDevice *d, |
954 |
uint32_t address, uint32_t val, int len)
|
955 |
{ |
956 |
pci_default_write_config(d, address, val, len); |
957 |
} |
958 |
|
959 |
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
|
960 |
{ |
961 |
PCIBus *sec; |
962 |
|
963 |
if (!bus)
|
964 |
return NULL; |
965 |
|
966 |
if (pci_bus_num(bus) == bus_num) {
|
967 |
return bus;
|
968 |
} |
969 |
|
970 |
/* try child bus */
|
971 |
QLIST_FOREACH(sec, &bus->child, sibling) { |
972 |
if (pci_bus_num(sec) <= bus_num && bus_num <= pci_sub_bus(sec)) {
|
973 |
return pci_find_bus(sec, bus_num);
|
974 |
} |
975 |
} |
976 |
|
977 |
return NULL; |
978 |
} |
979 |
|
980 |
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function) |
981 |
{ |
982 |
bus = pci_find_bus(bus, bus_num); |
983 |
|
984 |
if (!bus)
|
985 |
return NULL; |
986 |
|
987 |
return bus->devices[PCI_DEVFN(slot, function)];
|
988 |
} |
989 |
|
990 |
static int pci_bridge_initfn(PCIDevice *dev) |
991 |
{ |
992 |
PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev); |
993 |
|
994 |
pci_config_set_vendor_id(s->dev.config, s->vid); |
995 |
pci_config_set_device_id(s->dev.config, s->did); |
996 |
|
997 |
/* TODO: intial value
|
998 |
* command register:
|
999 |
* According to PCI bridge spec, after reset
|
1000 |
* bus master bit is off
|
1001 |
* memory space enable bit is off
|
1002 |
* According to manual (805-1251.pdf).(See abp_pbi.c for its links.)
|
1003 |
* the reset value should be zero unless the boot pin is tied high
|
1004 |
* (which is tru) and thus it should be PCI_COMMAND_MEMORY.
|
1005 |
*
|
1006 |
* For now, don't touch the value.
|
1007 |
* Later command register will be set to zero and apb_pci.c will
|
1008 |
* override the value.
|
1009 |
* Same for latency timer, and multi function bit of header type.
|
1010 |
*/
|
1011 |
pci_set_word(dev->config + PCI_COMMAND, |
1012 |
PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
1013 |
|
1014 |
pci_set_word(dev->config + PCI_STATUS, |
1015 |
PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); |
1016 |
pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI); |
1017 |
dev->config[PCI_LATENCY_TIMER] = 0x10;
|
1018 |
dev->config[PCI_HEADER_TYPE] = |
1019 |
PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE; |
1020 |
pci_set_word(dev->config + PCI_SEC_STATUS, |
1021 |
PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); |
1022 |
return 0; |
1023 |
} |
1024 |
|
1025 |
static int pci_bridge_exitfn(PCIDevice *pci_dev) |
1026 |
{ |
1027 |
PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev); |
1028 |
PCIBus *bus = &s->bus; |
1029 |
pci_unregister_secondary_bus(bus); |
1030 |
return 0; |
1031 |
} |
1032 |
|
1033 |
PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
|
1034 |
pci_map_irq_fn map_irq, const char *name) |
1035 |
{ |
1036 |
PCIDevice *dev; |
1037 |
PCIBridge *s; |
1038 |
|
1039 |
dev = pci_create(bus, devfn, "pci-bridge");
|
1040 |
qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
|
1041 |
qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
|
1042 |
qdev_init_nofail(&dev->qdev); |
1043 |
|
1044 |
s = DO_UPCAST(PCIBridge, dev, dev); |
1045 |
pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name); |
1046 |
return &s->bus;
|
1047 |
} |
1048 |
|
1049 |
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base) |
1050 |
{ |
1051 |
PCIDevice *pci_dev = (PCIDevice *)qdev; |
1052 |
PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev); |
1053 |
PCIBus *bus; |
1054 |
int devfn, rc;
|
1055 |
|
1056 |
/* initialize cap_present for pci_is_express() and pci_config_size() */
|
1057 |
if (info->is_express) {
|
1058 |
pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1059 |
} |
1060 |
|
1061 |
bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev)); |
1062 |
devfn = pci_dev->devfn; |
1063 |
pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn, |
1064 |
info->config_read, info->config_write); |
1065 |
assert(pci_dev); |
1066 |
rc = info->init(pci_dev); |
1067 |
if (rc != 0) |
1068 |
return rc;
|
1069 |
if (qdev->hotplugged)
|
1070 |
bus->hotplug(pci_dev, 1);
|
1071 |
return 0; |
1072 |
} |
1073 |
|
1074 |
static int pci_unplug_device(DeviceState *qdev) |
1075 |
{ |
1076 |
PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev); |
1077 |
|
1078 |
dev->bus->hotplug(dev, 0);
|
1079 |
return 0; |
1080 |
} |
1081 |
|
1082 |
void pci_qdev_register(PCIDeviceInfo *info)
|
1083 |
{ |
1084 |
info->qdev.init = pci_qdev_init; |
1085 |
info->qdev.unplug = pci_unplug_device; |
1086 |
info->qdev.exit = pci_unregister_device; |
1087 |
info->qdev.bus_info = &pci_bus_info; |
1088 |
qdev_register(&info->qdev); |
1089 |
} |
1090 |
|
1091 |
void pci_qdev_register_many(PCIDeviceInfo *info)
|
1092 |
{ |
1093 |
while (info->qdev.name) {
|
1094 |
pci_qdev_register(info); |
1095 |
info++; |
1096 |
} |
1097 |
} |
1098 |
|
1099 |
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name) |
1100 |
{ |
1101 |
DeviceState *dev; |
1102 |
|
1103 |
dev = qdev_create(&bus->qbus, name); |
1104 |
qdev_prop_set_uint32(dev, "addr", devfn);
|
1105 |
return DO_UPCAST(PCIDevice, qdev, dev);
|
1106 |
} |
1107 |
|
1108 |
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) |
1109 |
{ |
1110 |
PCIDevice *dev = pci_create(bus, devfn, name); |
1111 |
qdev_init_nofail(&dev->qdev); |
1112 |
return dev;
|
1113 |
} |
1114 |
|
1115 |
static int pci_find_space(PCIDevice *pdev, uint8_t size) |
1116 |
{ |
1117 |
int config_size = pci_config_size(pdev);
|
1118 |
int offset = PCI_CONFIG_HEADER_SIZE;
|
1119 |
int i;
|
1120 |
for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
|
1121 |
if (pdev->used[i])
|
1122 |
offset = i + 1;
|
1123 |
else if (i - offset + 1 == size) |
1124 |
return offset;
|
1125 |
return 0; |
1126 |
} |
1127 |
|
1128 |
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
|
1129 |
uint8_t *prev_p) |
1130 |
{ |
1131 |
uint8_t next, prev; |
1132 |
|
1133 |
if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
|
1134 |
return 0; |
1135 |
|
1136 |
for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
|
1137 |
prev = next + PCI_CAP_LIST_NEXT) |
1138 |
if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
|
1139 |
break;
|
1140 |
|
1141 |
if (prev_p)
|
1142 |
*prev_p = prev; |
1143 |
return next;
|
1144 |
} |
1145 |
|
1146 |
/* Reserve space and add capability to the linked list in pci config space */
|
1147 |
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1148 |
{ |
1149 |
uint8_t offset = pci_find_space(pdev, size); |
1150 |
uint8_t *config = pdev->config + offset; |
1151 |
if (!offset)
|
1152 |
return -ENOSPC;
|
1153 |
config[PCI_CAP_LIST_ID] = cap_id; |
1154 |
config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; |
1155 |
pdev->config[PCI_CAPABILITY_LIST] = offset; |
1156 |
pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; |
1157 |
memset(pdev->used + offset, 0xFF, size);
|
1158 |
/* Make capability read-only by default */
|
1159 |
memset(pdev->wmask + offset, 0, size);
|
1160 |
/* Check capability by default */
|
1161 |
memset(pdev->cmask + offset, 0xFF, size);
|
1162 |
return offset;
|
1163 |
} |
1164 |
|
1165 |
/* Unlink capability from the pci config space. */
|
1166 |
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1167 |
{ |
1168 |
uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); |
1169 |
if (!offset)
|
1170 |
return;
|
1171 |
pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; |
1172 |
/* Make capability writeable again */
|
1173 |
memset(pdev->wmask + offset, 0xff, size);
|
1174 |
/* Clear cmask as device-specific registers can't be checked */
|
1175 |
memset(pdev->cmask + offset, 0, size);
|
1176 |
memset(pdev->used + offset, 0, size);
|
1177 |
|
1178 |
if (!pdev->config[PCI_CAPABILITY_LIST])
|
1179 |
pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; |
1180 |
} |
1181 |
|
1182 |
/* Reserve space for capability at a known offset (to call after load). */
|
1183 |
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
|
1184 |
{ |
1185 |
memset(pdev->used + offset, 0xff, size);
|
1186 |
} |
1187 |
|
1188 |
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) |
1189 |
{ |
1190 |
return pci_find_capability_list(pdev, cap_id, NULL); |
1191 |
} |
1192 |
|
1193 |
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
1194 |
{ |
1195 |
PCIDevice *d = (PCIDevice *)dev; |
1196 |
const pci_class_desc *desc;
|
1197 |
char ctxt[64]; |
1198 |
PCIIORegion *r; |
1199 |
int i, class;
|
1200 |
|
1201 |
class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
1202 |
desc = pci_class_descriptions; |
1203 |
while (desc->desc && class != desc->class)
|
1204 |
desc++; |
1205 |
if (desc->desc) {
|
1206 |
snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); |
1207 |
} else {
|
1208 |
snprintf(ctxt, sizeof(ctxt), "Class %04x", class); |
1209 |
} |
1210 |
|
1211 |
monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
|
1212 |
"pci id %04x:%04x (sub %04x:%04x)\n",
|
1213 |
indent, "", ctxt,
|
1214 |
d->config[PCI_SECONDARY_BUS], |
1215 |
PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), |
1216 |
pci_get_word(d->config + PCI_VENDOR_ID), |
1217 |
pci_get_word(d->config + PCI_DEVICE_ID), |
1218 |
pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), |
1219 |
pci_get_word(d->config + PCI_SUBSYSTEM_ID)); |
1220 |
for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1221 |
r = &d->io_regions[i]; |
1222 |
if (!r->size)
|
1223 |
continue;
|
1224 |
monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
|
1225 |
" [0x%"FMT_PCIBUS"]\n", |
1226 |
indent, "",
|
1227 |
i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", |
1228 |
r->addr, r->addr + r->size - 1);
|
1229 |
} |
1230 |
} |
1231 |
|
1232 |
static PCIDeviceInfo bridge_info = {
|
1233 |
.qdev.name = "pci-bridge",
|
1234 |
.qdev.size = sizeof(PCIBridge),
|
1235 |
.init = pci_bridge_initfn, |
1236 |
.exit = pci_bridge_exitfn, |
1237 |
.config_write = pci_bridge_write_config, |
1238 |
.qdev.props = (Property[]) { |
1239 |
DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0), |
1240 |
DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0), |
1241 |
DEFINE_PROP_END_OF_LIST(), |
1242 |
} |
1243 |
}; |
1244 |
|
1245 |
static void pci_register_devices(void) |
1246 |
{ |
1247 |
pci_qdev_register(&bridge_info); |
1248 |
} |
1249 |
|
1250 |
device_init(pci_register_devices) |