root / hw / qdev.c @ 44677ded
History | View | Annotate | Download (27.7 kB)
1 |
/*
|
---|---|
2 |
* Dynamic device configuration and creation.
|
3 |
*
|
4 |
* Copyright (c) 2009 CodeSourcery
|
5 |
*
|
6 |
* This library is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU Lesser General Public
|
8 |
* License as published by the Free Software Foundation; either
|
9 |
* version 2 of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This library is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
* Lesser General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU Lesser General Public
|
17 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
|
20 |
/* The theory here is that it should be possible to create a machine without
|
21 |
knowledge of specific devices. Historically board init routines have
|
22 |
passed a bunch of arguments to each device, requiring the board know
|
23 |
exactly which device it is dealing with. This file provides an abstract
|
24 |
API for device configuration and initialization. Devices will generally
|
25 |
inherit from a particular bus (e.g. PCI or I2C) rather than
|
26 |
this API directly. */
|
27 |
|
28 |
#include "net.h" |
29 |
#include "qdev.h" |
30 |
#include "sysemu.h" |
31 |
#include "monitor.h" |
32 |
|
33 |
static int qdev_hotplug = 0; |
34 |
static bool qdev_hot_added = false; |
35 |
static bool qdev_hot_removed = false; |
36 |
|
37 |
/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
|
38 |
static BusState *main_system_bus;
|
39 |
static void main_system_bus_create(void); |
40 |
|
41 |
DeviceInfo *device_info_list; |
42 |
|
43 |
static BusState *qbus_find_recursive(BusState *bus, const char *name, |
44 |
const BusInfo *info);
|
45 |
static BusState *qbus_find(const char *path); |
46 |
|
47 |
/* Register a new device type. */
|
48 |
void qdev_register(DeviceInfo *info)
|
49 |
{ |
50 |
assert(info->size >= sizeof(DeviceState));
|
51 |
assert(!info->next); |
52 |
|
53 |
info->next = device_info_list; |
54 |
device_info_list = info; |
55 |
} |
56 |
|
57 |
static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name) |
58 |
{ |
59 |
DeviceInfo *info; |
60 |
|
61 |
/* first check device names */
|
62 |
for (info = device_info_list; info != NULL; info = info->next) { |
63 |
if (bus_info && info->bus_info != bus_info)
|
64 |
continue;
|
65 |
if (strcmp(info->name, name) != 0) |
66 |
continue;
|
67 |
return info;
|
68 |
} |
69 |
|
70 |
/* failing that check the aliases */
|
71 |
for (info = device_info_list; info != NULL; info = info->next) { |
72 |
if (bus_info && info->bus_info != bus_info)
|
73 |
continue;
|
74 |
if (!info->alias)
|
75 |
continue;
|
76 |
if (strcmp(info->alias, name) != 0) |
77 |
continue;
|
78 |
return info;
|
79 |
} |
80 |
return NULL; |
81 |
} |
82 |
|
83 |
static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
|
84 |
{ |
85 |
DeviceState *dev; |
86 |
|
87 |
assert(bus->info == info->bus_info); |
88 |
dev = g_malloc0(info->size); |
89 |
dev->info = info; |
90 |
dev->parent_bus = bus; |
91 |
qdev_prop_set_defaults(dev, dev->info->props); |
92 |
qdev_prop_set_defaults(dev, dev->parent_bus->info->props); |
93 |
qdev_prop_set_globals(dev); |
94 |
QTAILQ_INSERT_HEAD(&bus->children, dev, sibling); |
95 |
if (qdev_hotplug) {
|
96 |
assert(bus->allow_hotplug); |
97 |
dev->hotplugged = 1;
|
98 |
qdev_hot_added = true;
|
99 |
} |
100 |
dev->instance_id_alias = -1;
|
101 |
QTAILQ_INIT(&dev->properties); |
102 |
dev->state = DEV_STATE_CREATED; |
103 |
return dev;
|
104 |
} |
105 |
|
106 |
/* Create a new device. This only initializes the device state structure
|
107 |
and allows properties to be set. qdev_init should be called to
|
108 |
initialize the actual device emulation. */
|
109 |
DeviceState *qdev_create(BusState *bus, const char *name) |
110 |
{ |
111 |
DeviceState *dev; |
112 |
|
113 |
dev = qdev_try_create(bus, name); |
114 |
if (!dev) {
|
115 |
if (bus) {
|
116 |
hw_error("Unknown device '%s' for bus '%s'\n", name,
|
117 |
bus->info->name); |
118 |
} else {
|
119 |
hw_error("Unknown device '%s' for default sysbus\n", name);
|
120 |
} |
121 |
} |
122 |
|
123 |
return dev;
|
124 |
} |
125 |
|
126 |
DeviceState *qdev_try_create(BusState *bus, const char *name) |
127 |
{ |
128 |
DeviceInfo *info; |
129 |
|
130 |
if (!bus) {
|
131 |
bus = sysbus_get_default(); |
132 |
} |
133 |
|
134 |
info = qdev_find_info(bus->info, name); |
135 |
if (!info) {
|
136 |
return NULL; |
137 |
} |
138 |
|
139 |
return qdev_create_from_info(bus, info);
|
140 |
} |
141 |
|
142 |
static void qdev_print_devinfo(DeviceInfo *info) |
143 |
{ |
144 |
error_printf("name \"%s\", bus %s",
|
145 |
info->name, info->bus_info->name); |
146 |
if (info->alias) {
|
147 |
error_printf(", alias \"%s\"", info->alias);
|
148 |
} |
149 |
if (info->desc) {
|
150 |
error_printf(", desc \"%s\"", info->desc);
|
151 |
} |
152 |
if (info->no_user) {
|
153 |
error_printf(", no-user");
|
154 |
} |
155 |
error_printf("\n");
|
156 |
} |
157 |
|
158 |
static int set_property(const char *name, const char *value, void *opaque) |
159 |
{ |
160 |
DeviceState *dev = opaque; |
161 |
|
162 |
if (strcmp(name, "driver") == 0) |
163 |
return 0; |
164 |
if (strcmp(name, "bus") == 0) |
165 |
return 0; |
166 |
|
167 |
if (qdev_prop_parse(dev, name, value) == -1) { |
168 |
return -1; |
169 |
} |
170 |
return 0; |
171 |
} |
172 |
|
173 |
int qdev_device_help(QemuOpts *opts)
|
174 |
{ |
175 |
const char *driver; |
176 |
DeviceInfo *info; |
177 |
Property *prop; |
178 |
|
179 |
driver = qemu_opt_get(opts, "driver");
|
180 |
if (driver && !strcmp(driver, "?")) { |
181 |
for (info = device_info_list; info != NULL; info = info->next) { |
182 |
if (info->no_user) {
|
183 |
continue; /* not available, don't show */ |
184 |
} |
185 |
qdev_print_devinfo(info); |
186 |
} |
187 |
return 1; |
188 |
} |
189 |
|
190 |
if (!driver || !qemu_opt_get(opts, "?")) { |
191 |
return 0; |
192 |
} |
193 |
|
194 |
info = qdev_find_info(NULL, driver);
|
195 |
if (!info) {
|
196 |
return 0; |
197 |
} |
198 |
|
199 |
for (prop = info->props; prop && prop->name; prop++) {
|
200 |
/*
|
201 |
* TODO Properties without a parser are just for dirty hacks.
|
202 |
* qdev_prop_ptr is the only such PropertyInfo. It's marked
|
203 |
* for removal. This conditional should be removed along with
|
204 |
* it.
|
205 |
*/
|
206 |
if (!prop->info->parse) {
|
207 |
continue; /* no way to set it, don't show */ |
208 |
} |
209 |
error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name);
|
210 |
} |
211 |
for (prop = info->bus_info->props; prop && prop->name; prop++) {
|
212 |
if (!prop->info->parse) {
|
213 |
continue; /* no way to set it, don't show */ |
214 |
} |
215 |
error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name);
|
216 |
} |
217 |
return 1; |
218 |
} |
219 |
|
220 |
DeviceState *qdev_device_add(QemuOpts *opts) |
221 |
{ |
222 |
const char *driver, *path, *id; |
223 |
DeviceInfo *info; |
224 |
DeviceState *qdev; |
225 |
BusState *bus; |
226 |
|
227 |
driver = qemu_opt_get(opts, "driver");
|
228 |
if (!driver) {
|
229 |
qerror_report(QERR_MISSING_PARAMETER, "driver");
|
230 |
return NULL; |
231 |
} |
232 |
|
233 |
/* find driver */
|
234 |
info = qdev_find_info(NULL, driver);
|
235 |
if (!info || info->no_user) {
|
236 |
qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "a driver name"); |
237 |
error_printf_unless_qmp("Try with argument '?' for a list.\n");
|
238 |
return NULL; |
239 |
} |
240 |
|
241 |
/* find bus */
|
242 |
path = qemu_opt_get(opts, "bus");
|
243 |
if (path != NULL) { |
244 |
bus = qbus_find(path); |
245 |
if (!bus) {
|
246 |
return NULL; |
247 |
} |
248 |
if (bus->info != info->bus_info) {
|
249 |
qerror_report(QERR_BAD_BUS_FOR_DEVICE, |
250 |
driver, bus->info->name); |
251 |
return NULL; |
252 |
} |
253 |
} else {
|
254 |
bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
|
255 |
if (!bus) {
|
256 |
qerror_report(QERR_NO_BUS_FOR_DEVICE, |
257 |
info->name, info->bus_info->name); |
258 |
return NULL; |
259 |
} |
260 |
} |
261 |
if (qdev_hotplug && !bus->allow_hotplug) {
|
262 |
qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); |
263 |
return NULL; |
264 |
} |
265 |
|
266 |
/* create device, set properties */
|
267 |
qdev = qdev_create_from_info(bus, info); |
268 |
id = qemu_opts_id(opts); |
269 |
if (id) {
|
270 |
qdev->id = id; |
271 |
} |
272 |
if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { |
273 |
qdev_free(qdev); |
274 |
return NULL; |
275 |
} |
276 |
if (qdev_init(qdev) < 0) { |
277 |
qerror_report(QERR_DEVICE_INIT_FAILED, driver); |
278 |
return NULL; |
279 |
} |
280 |
qdev->opts = opts; |
281 |
return qdev;
|
282 |
} |
283 |
|
284 |
/* Initialize a device. Device properties should be set before calling
|
285 |
this function. IRQs and MMIO regions should be connected/mapped after
|
286 |
calling this function.
|
287 |
On failure, destroy the device and return negative value.
|
288 |
Return 0 on success. */
|
289 |
int qdev_init(DeviceState *dev)
|
290 |
{ |
291 |
int rc;
|
292 |
|
293 |
assert(dev->state == DEV_STATE_CREATED); |
294 |
rc = dev->info->init(dev, dev->info); |
295 |
if (rc < 0) { |
296 |
qdev_free(dev); |
297 |
return rc;
|
298 |
} |
299 |
if (dev->info->vmsd) {
|
300 |
vmstate_register_with_alias_id(dev, -1, dev->info->vmsd, dev,
|
301 |
dev->instance_id_alias, |
302 |
dev->alias_required_for_version); |
303 |
} |
304 |
dev->state = DEV_STATE_INITIALIZED; |
305 |
if (dev->hotplugged && dev->info->reset) {
|
306 |
dev->info->reset(dev); |
307 |
} |
308 |
return 0; |
309 |
} |
310 |
|
311 |
void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
312 |
int required_for_version)
|
313 |
{ |
314 |
assert(dev->state == DEV_STATE_CREATED); |
315 |
dev->instance_id_alias = alias_id; |
316 |
dev->alias_required_for_version = required_for_version; |
317 |
} |
318 |
|
319 |
int qdev_unplug(DeviceState *dev)
|
320 |
{ |
321 |
if (!dev->parent_bus->allow_hotplug) {
|
322 |
qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); |
323 |
return -1; |
324 |
} |
325 |
assert(dev->info->unplug != NULL);
|
326 |
|
327 |
if (dev->ref != 0) { |
328 |
qerror_report(QERR_DEVICE_IN_USE, dev->id?:"");
|
329 |
return -1; |
330 |
} |
331 |
|
332 |
qdev_hot_removed = true;
|
333 |
|
334 |
return dev->info->unplug(dev);
|
335 |
} |
336 |
|
337 |
static int qdev_reset_one(DeviceState *dev, void *opaque) |
338 |
{ |
339 |
if (dev->info->reset) {
|
340 |
dev->info->reset(dev); |
341 |
} |
342 |
|
343 |
return 0; |
344 |
} |
345 |
|
346 |
BusState *sysbus_get_default(void)
|
347 |
{ |
348 |
if (!main_system_bus) {
|
349 |
main_system_bus_create(); |
350 |
} |
351 |
return main_system_bus;
|
352 |
} |
353 |
|
354 |
static int qbus_reset_one(BusState *bus, void *opaque) |
355 |
{ |
356 |
if (bus->info->reset) {
|
357 |
return bus->info->reset(bus);
|
358 |
} |
359 |
return 0; |
360 |
} |
361 |
|
362 |
void qdev_reset_all(DeviceState *dev)
|
363 |
{ |
364 |
qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
|
365 |
} |
366 |
|
367 |
void qbus_reset_all_fn(void *opaque) |
368 |
{ |
369 |
BusState *bus = opaque; |
370 |
qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
|
371 |
} |
372 |
|
373 |
/* can be used as ->unplug() callback for the simple cases */
|
374 |
int qdev_simple_unplug_cb(DeviceState *dev)
|
375 |
{ |
376 |
/* just zap it */
|
377 |
qdev_free(dev); |
378 |
return 0; |
379 |
} |
380 |
|
381 |
|
382 |
/* Like qdev_init(), but terminate program via error_report() instead of
|
383 |
returning an error value. This is okay during machine creation.
|
384 |
Don't use for hotplug, because there callers need to recover from
|
385 |
failure. Exception: if you know the device's init() callback can't
|
386 |
fail, then qdev_init_nofail() can't fail either, and is therefore
|
387 |
usable even then. But relying on the device implementation that
|
388 |
way is somewhat unclean, and best avoided. */
|
389 |
void qdev_init_nofail(DeviceState *dev)
|
390 |
{ |
391 |
DeviceInfo *info = dev->info; |
392 |
|
393 |
if (qdev_init(dev) < 0) { |
394 |
error_report("Initialization of device %s failed", info->name);
|
395 |
exit(1);
|
396 |
} |
397 |
} |
398 |
|
399 |
static void qdev_property_del_all(DeviceState *dev) |
400 |
{ |
401 |
while (!QTAILQ_EMPTY(&dev->properties)) {
|
402 |
DeviceProperty *prop = QTAILQ_FIRST(&dev->properties); |
403 |
|
404 |
QTAILQ_REMOVE(&dev->properties, prop, node); |
405 |
|
406 |
if (prop->release) {
|
407 |
prop->release(dev, prop->name, prop->opaque); |
408 |
} |
409 |
|
410 |
g_free(prop->name); |
411 |
g_free(prop->type); |
412 |
g_free(prop); |
413 |
} |
414 |
} |
415 |
|
416 |
/* Unlink device from bus and free the structure. */
|
417 |
void qdev_free(DeviceState *dev)
|
418 |
{ |
419 |
BusState *bus; |
420 |
Property *prop; |
421 |
|
422 |
qdev_property_del_all(dev); |
423 |
|
424 |
if (dev->state == DEV_STATE_INITIALIZED) {
|
425 |
while (dev->num_child_bus) {
|
426 |
bus = QLIST_FIRST(&dev->child_bus); |
427 |
qbus_free(bus); |
428 |
} |
429 |
if (dev->info->vmsd)
|
430 |
vmstate_unregister(dev, dev->info->vmsd, dev); |
431 |
if (dev->info->exit)
|
432 |
dev->info->exit(dev); |
433 |
if (dev->opts)
|
434 |
qemu_opts_del(dev->opts); |
435 |
} |
436 |
QTAILQ_REMOVE(&dev->parent_bus->children, dev, sibling); |
437 |
for (prop = dev->info->props; prop && prop->name; prop++) {
|
438 |
if (prop->info->free) {
|
439 |
prop->info->free(dev, prop); |
440 |
} |
441 |
} |
442 |
g_free(dev); |
443 |
} |
444 |
|
445 |
void qdev_machine_creation_done(void) |
446 |
{ |
447 |
/*
|
448 |
* ok, initial machine setup is done, starting from now we can
|
449 |
* only create hotpluggable devices
|
450 |
*/
|
451 |
qdev_hotplug = 1;
|
452 |
} |
453 |
|
454 |
bool qdev_machine_modified(void) |
455 |
{ |
456 |
return qdev_hot_added || qdev_hot_removed;
|
457 |
} |
458 |
|
459 |
/* Get a character (serial) device interface. */
|
460 |
CharDriverState *qdev_init_chardev(DeviceState *dev) |
461 |
{ |
462 |
static int next_serial; |
463 |
|
464 |
/* FIXME: This function needs to go away: use chardev properties! */
|
465 |
return serial_hds[next_serial++];
|
466 |
} |
467 |
|
468 |
BusState *qdev_get_parent_bus(DeviceState *dev) |
469 |
{ |
470 |
return dev->parent_bus;
|
471 |
} |
472 |
|
473 |
void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) |
474 |
{ |
475 |
assert(dev->num_gpio_in == 0);
|
476 |
dev->num_gpio_in = n; |
477 |
dev->gpio_in = qemu_allocate_irqs(handler, dev, n); |
478 |
} |
479 |
|
480 |
void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) |
481 |
{ |
482 |
assert(dev->num_gpio_out == 0);
|
483 |
dev->num_gpio_out = n; |
484 |
dev->gpio_out = pins; |
485 |
} |
486 |
|
487 |
qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
|
488 |
{ |
489 |
assert(n >= 0 && n < dev->num_gpio_in);
|
490 |
return dev->gpio_in[n];
|
491 |
} |
492 |
|
493 |
void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) |
494 |
{ |
495 |
assert(n >= 0 && n < dev->num_gpio_out);
|
496 |
dev->gpio_out[n] = pin; |
497 |
} |
498 |
|
499 |
void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
|
500 |
{ |
501 |
qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
|
502 |
if (nd->vlan)
|
503 |
qdev_prop_set_vlan(dev, "vlan", nd->vlan);
|
504 |
if (nd->netdev)
|
505 |
qdev_prop_set_netdev(dev, "netdev", nd->netdev);
|
506 |
if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
|
507 |
qdev_prop_exists(dev, "vectors")) {
|
508 |
qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
|
509 |
} |
510 |
nd->instantiated = 1;
|
511 |
} |
512 |
|
513 |
BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
514 |
{ |
515 |
BusState *bus; |
516 |
|
517 |
QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
518 |
if (strcmp(name, bus->name) == 0) { |
519 |
return bus;
|
520 |
} |
521 |
} |
522 |
return NULL; |
523 |
} |
524 |
|
525 |
int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
|
526 |
qbus_walkerfn *busfn, void *opaque)
|
527 |
{ |
528 |
DeviceState *dev; |
529 |
int err;
|
530 |
|
531 |
if (busfn) {
|
532 |
err = busfn(bus, opaque); |
533 |
if (err) {
|
534 |
return err;
|
535 |
} |
536 |
} |
537 |
|
538 |
QTAILQ_FOREACH(dev, &bus->children, sibling) { |
539 |
err = qdev_walk_children(dev, devfn, busfn, opaque); |
540 |
if (err < 0) { |
541 |
return err;
|
542 |
} |
543 |
} |
544 |
|
545 |
return 0; |
546 |
} |
547 |
|
548 |
int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
|
549 |
qbus_walkerfn *busfn, void *opaque)
|
550 |
{ |
551 |
BusState *bus; |
552 |
int err;
|
553 |
|
554 |
if (devfn) {
|
555 |
err = devfn(dev, opaque); |
556 |
if (err) {
|
557 |
return err;
|
558 |
} |
559 |
} |
560 |
|
561 |
QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
562 |
err = qbus_walk_children(bus, devfn, busfn, opaque); |
563 |
if (err < 0) { |
564 |
return err;
|
565 |
} |
566 |
} |
567 |
|
568 |
return 0; |
569 |
} |
570 |
|
571 |
static BusState *qbus_find_recursive(BusState *bus, const char *name, |
572 |
const BusInfo *info)
|
573 |
{ |
574 |
DeviceState *dev; |
575 |
BusState *child, *ret; |
576 |
int match = 1; |
577 |
|
578 |
if (name && (strcmp(bus->name, name) != 0)) { |
579 |
match = 0;
|
580 |
} |
581 |
if (info && (bus->info != info)) {
|
582 |
match = 0;
|
583 |
} |
584 |
if (match) {
|
585 |
return bus;
|
586 |
} |
587 |
|
588 |
QTAILQ_FOREACH(dev, &bus->children, sibling) { |
589 |
QLIST_FOREACH(child, &dev->child_bus, sibling) { |
590 |
ret = qbus_find_recursive(child, name, info); |
591 |
if (ret) {
|
592 |
return ret;
|
593 |
} |
594 |
} |
595 |
} |
596 |
return NULL; |
597 |
} |
598 |
|
599 |
DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
600 |
{ |
601 |
DeviceState *dev, *ret; |
602 |
BusState *child; |
603 |
|
604 |
QTAILQ_FOREACH(dev, &bus->children, sibling) { |
605 |
if (dev->id && strcmp(dev->id, id) == 0) |
606 |
return dev;
|
607 |
QLIST_FOREACH(child, &dev->child_bus, sibling) { |
608 |
ret = qdev_find_recursive(child, id); |
609 |
if (ret) {
|
610 |
return ret;
|
611 |
} |
612 |
} |
613 |
} |
614 |
return NULL; |
615 |
} |
616 |
|
617 |
static void qbus_list_bus(DeviceState *dev) |
618 |
{ |
619 |
BusState *child; |
620 |
const char *sep = " "; |
621 |
|
622 |
error_printf("child busses at \"%s\":",
|
623 |
dev->id ? dev->id : dev->info->name); |
624 |
QLIST_FOREACH(child, &dev->child_bus, sibling) { |
625 |
error_printf("%s\"%s\"", sep, child->name);
|
626 |
sep = ", ";
|
627 |
} |
628 |
error_printf("\n");
|
629 |
} |
630 |
|
631 |
static void qbus_list_dev(BusState *bus) |
632 |
{ |
633 |
DeviceState *dev; |
634 |
const char *sep = " "; |
635 |
|
636 |
error_printf("devices at \"%s\":", bus->name);
|
637 |
QTAILQ_FOREACH(dev, &bus->children, sibling) { |
638 |
error_printf("%s\"%s\"", sep, dev->info->name);
|
639 |
if (dev->id)
|
640 |
error_printf("/\"%s\"", dev->id);
|
641 |
sep = ", ";
|
642 |
} |
643 |
error_printf("\n");
|
644 |
} |
645 |
|
646 |
static BusState *qbus_find_bus(DeviceState *dev, char *elem) |
647 |
{ |
648 |
BusState *child; |
649 |
|
650 |
QLIST_FOREACH(child, &dev->child_bus, sibling) { |
651 |
if (strcmp(child->name, elem) == 0) { |
652 |
return child;
|
653 |
} |
654 |
} |
655 |
return NULL; |
656 |
} |
657 |
|
658 |
static DeviceState *qbus_find_dev(BusState *bus, char *elem) |
659 |
{ |
660 |
DeviceState *dev; |
661 |
|
662 |
/*
|
663 |
* try to match in order:
|
664 |
* (1) instance id, if present
|
665 |
* (2) driver name
|
666 |
* (3) driver alias, if present
|
667 |
*/
|
668 |
QTAILQ_FOREACH(dev, &bus->children, sibling) { |
669 |
if (dev->id && strcmp(dev->id, elem) == 0) { |
670 |
return dev;
|
671 |
} |
672 |
} |
673 |
QTAILQ_FOREACH(dev, &bus->children, sibling) { |
674 |
if (strcmp(dev->info->name, elem) == 0) { |
675 |
return dev;
|
676 |
} |
677 |
} |
678 |
QTAILQ_FOREACH(dev, &bus->children, sibling) { |
679 |
if (dev->info->alias && strcmp(dev->info->alias, elem) == 0) { |
680 |
return dev;
|
681 |
} |
682 |
} |
683 |
return NULL; |
684 |
} |
685 |
|
686 |
static BusState *qbus_find(const char *path) |
687 |
{ |
688 |
DeviceState *dev; |
689 |
BusState *bus; |
690 |
char elem[128]; |
691 |
int pos, len;
|
692 |
|
693 |
/* find start element */
|
694 |
if (path[0] == '/') { |
695 |
bus = main_system_bus; |
696 |
pos = 0;
|
697 |
} else {
|
698 |
if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { |
699 |
assert(!path[0]);
|
700 |
elem[0] = len = 0; |
701 |
} |
702 |
bus = qbus_find_recursive(main_system_bus, elem, NULL);
|
703 |
if (!bus) {
|
704 |
qerror_report(QERR_BUS_NOT_FOUND, elem); |
705 |
return NULL; |
706 |
} |
707 |
pos = len; |
708 |
} |
709 |
|
710 |
for (;;) {
|
711 |
assert(path[pos] == '/' || !path[pos]);
|
712 |
while (path[pos] == '/') { |
713 |
pos++; |
714 |
} |
715 |
if (path[pos] == '\0') { |
716 |
return bus;
|
717 |
} |
718 |
|
719 |
/* find device */
|
720 |
if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
721 |
assert(0);
|
722 |
elem[0] = len = 0; |
723 |
} |
724 |
pos += len; |
725 |
dev = qbus_find_dev(bus, elem); |
726 |
if (!dev) {
|
727 |
qerror_report(QERR_DEVICE_NOT_FOUND, elem); |
728 |
if (!monitor_cur_is_qmp()) {
|
729 |
qbus_list_dev(bus); |
730 |
} |
731 |
return NULL; |
732 |
} |
733 |
|
734 |
assert(path[pos] == '/' || !path[pos]);
|
735 |
while (path[pos] == '/') { |
736 |
pos++; |
737 |
} |
738 |
if (path[pos] == '\0') { |
739 |
/* last specified element is a device. If it has exactly
|
740 |
* one child bus accept it nevertheless */
|
741 |
switch (dev->num_child_bus) {
|
742 |
case 0: |
743 |
qerror_report(QERR_DEVICE_NO_BUS, elem); |
744 |
return NULL; |
745 |
case 1: |
746 |
return QLIST_FIRST(&dev->child_bus);
|
747 |
default:
|
748 |
qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem); |
749 |
if (!monitor_cur_is_qmp()) {
|
750 |
qbus_list_bus(dev); |
751 |
} |
752 |
return NULL; |
753 |
} |
754 |
} |
755 |
|
756 |
/* find bus */
|
757 |
if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
758 |
assert(0);
|
759 |
elem[0] = len = 0; |
760 |
} |
761 |
pos += len; |
762 |
bus = qbus_find_bus(dev, elem); |
763 |
if (!bus) {
|
764 |
qerror_report(QERR_BUS_NOT_FOUND, elem); |
765 |
if (!monitor_cur_is_qmp()) {
|
766 |
qbus_list_bus(dev); |
767 |
} |
768 |
return NULL; |
769 |
} |
770 |
} |
771 |
} |
772 |
|
773 |
void qbus_create_inplace(BusState *bus, BusInfo *info,
|
774 |
DeviceState *parent, const char *name) |
775 |
{ |
776 |
char *buf;
|
777 |
int i,len;
|
778 |
|
779 |
bus->info = info; |
780 |
bus->parent = parent; |
781 |
|
782 |
if (name) {
|
783 |
/* use supplied name */
|
784 |
bus->name = g_strdup(name); |
785 |
} else if (parent && parent->id) { |
786 |
/* parent device has id -> use it for bus name */
|
787 |
len = strlen(parent->id) + 16;
|
788 |
buf = g_malloc(len); |
789 |
snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
|
790 |
bus->name = buf; |
791 |
} else {
|
792 |
/* no id -> use lowercase bus type for bus name */
|
793 |
len = strlen(info->name) + 16;
|
794 |
buf = g_malloc(len); |
795 |
len = snprintf(buf, len, "%s.%d", info->name,
|
796 |
parent ? parent->num_child_bus : 0);
|
797 |
for (i = 0; i < len; i++) |
798 |
buf[i] = qemu_tolower(buf[i]); |
799 |
bus->name = buf; |
800 |
} |
801 |
|
802 |
QTAILQ_INIT(&bus->children); |
803 |
if (parent) {
|
804 |
QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling); |
805 |
parent->num_child_bus++; |
806 |
} else if (bus != main_system_bus) { |
807 |
/* TODO: once all bus devices are qdevified,
|
808 |
only reset handler for main_system_bus should be registered here. */
|
809 |
qemu_register_reset(qbus_reset_all_fn, bus); |
810 |
} |
811 |
} |
812 |
|
813 |
BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name) |
814 |
{ |
815 |
BusState *bus; |
816 |
|
817 |
bus = g_malloc0(info->size); |
818 |
bus->qdev_allocated = 1;
|
819 |
qbus_create_inplace(bus, info, parent, name); |
820 |
return bus;
|
821 |
} |
822 |
|
823 |
static void main_system_bus_create(void) |
824 |
{ |
825 |
/* assign main_system_bus before qbus_create_inplace()
|
826 |
* in order to make "if (bus != main_system_bus)" work */
|
827 |
main_system_bus = g_malloc0(system_bus_info.size); |
828 |
main_system_bus->qdev_allocated = 1;
|
829 |
qbus_create_inplace(main_system_bus, &system_bus_info, NULL,
|
830 |
"main-system-bus");
|
831 |
} |
832 |
|
833 |
void qbus_free(BusState *bus)
|
834 |
{ |
835 |
DeviceState *dev; |
836 |
|
837 |
while ((dev = QTAILQ_FIRST(&bus->children)) != NULL) { |
838 |
qdev_free(dev); |
839 |
} |
840 |
if (bus->parent) {
|
841 |
QLIST_REMOVE(bus, sibling); |
842 |
bus->parent->num_child_bus--; |
843 |
} else {
|
844 |
assert(bus != main_system_bus); /* main_system_bus is never freed */
|
845 |
qemu_unregister_reset(qbus_reset_all_fn, bus); |
846 |
} |
847 |
g_free((void*)bus->name);
|
848 |
if (bus->qdev_allocated) {
|
849 |
g_free(bus); |
850 |
} |
851 |
} |
852 |
|
853 |
#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) |
854 |
static void qbus_print(Monitor *mon, BusState *bus, int indent); |
855 |
|
856 |
static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, |
857 |
const char *prefix, int indent) |
858 |
{ |
859 |
char buf[64]; |
860 |
|
861 |
if (!props)
|
862 |
return;
|
863 |
while (props->name) {
|
864 |
/*
|
865 |
* TODO Properties without a print method are just for dirty
|
866 |
* hacks. qdev_prop_ptr is the only such PropertyInfo. It's
|
867 |
* marked for removal. The test props->info->print should be
|
868 |
* removed along with it.
|
869 |
*/
|
870 |
if (props->info->print) {
|
871 |
props->info->print(dev, props, buf, sizeof(buf));
|
872 |
qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
|
873 |
} |
874 |
props++; |
875 |
} |
876 |
} |
877 |
|
878 |
static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
879 |
{ |
880 |
BusState *child; |
881 |
qdev_printf("dev: %s, id \"%s\"\n", dev->info->name,
|
882 |
dev->id ? dev->id : "");
|
883 |
indent += 2;
|
884 |
if (dev->num_gpio_in) {
|
885 |
qdev_printf("gpio-in %d\n", dev->num_gpio_in);
|
886 |
} |
887 |
if (dev->num_gpio_out) {
|
888 |
qdev_printf("gpio-out %d\n", dev->num_gpio_out);
|
889 |
} |
890 |
qdev_print_props(mon, dev, dev->info->props, "dev", indent);
|
891 |
qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
|
892 |
if (dev->parent_bus->info->print_dev)
|
893 |
dev->parent_bus->info->print_dev(mon, dev, indent); |
894 |
QLIST_FOREACH(child, &dev->child_bus, sibling) { |
895 |
qbus_print(mon, child, indent); |
896 |
} |
897 |
} |
898 |
|
899 |
static void qbus_print(Monitor *mon, BusState *bus, int indent) |
900 |
{ |
901 |
struct DeviceState *dev;
|
902 |
|
903 |
qdev_printf("bus: %s\n", bus->name);
|
904 |
indent += 2;
|
905 |
qdev_printf("type %s\n", bus->info->name);
|
906 |
QTAILQ_FOREACH(dev, &bus->children, sibling) { |
907 |
qdev_print(mon, dev, indent); |
908 |
} |
909 |
} |
910 |
#undef qdev_printf
|
911 |
|
912 |
void do_info_qtree(Monitor *mon)
|
913 |
{ |
914 |
if (main_system_bus)
|
915 |
qbus_print(mon, main_system_bus, 0);
|
916 |
} |
917 |
|
918 |
void do_info_qdm(Monitor *mon)
|
919 |
{ |
920 |
DeviceInfo *info; |
921 |
|
922 |
for (info = device_info_list; info != NULL; info = info->next) { |
923 |
qdev_print_devinfo(info); |
924 |
} |
925 |
} |
926 |
|
927 |
int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) |
928 |
{ |
929 |
QemuOpts *opts; |
930 |
|
931 |
opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
|
932 |
if (!opts) {
|
933 |
return -1; |
934 |
} |
935 |
if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
|
936 |
qemu_opts_del(opts); |
937 |
return 0; |
938 |
} |
939 |
if (!qdev_device_add(opts)) {
|
940 |
qemu_opts_del(opts); |
941 |
return -1; |
942 |
} |
943 |
return 0; |
944 |
} |
945 |
|
946 |
int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
947 |
{ |
948 |
const char *id = qdict_get_str(qdict, "id"); |
949 |
DeviceState *dev; |
950 |
|
951 |
dev = qdev_find_recursive(main_system_bus, id); |
952 |
if (NULL == dev) { |
953 |
qerror_report(QERR_DEVICE_NOT_FOUND, id); |
954 |
return -1; |
955 |
} |
956 |
return qdev_unplug(dev);
|
957 |
} |
958 |
|
959 |
static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size) |
960 |
{ |
961 |
int l = 0; |
962 |
|
963 |
if (dev && dev->parent_bus) {
|
964 |
char *d;
|
965 |
l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size); |
966 |
if (dev->parent_bus->info->get_fw_dev_path) {
|
967 |
d = dev->parent_bus->info->get_fw_dev_path(dev); |
968 |
l += snprintf(p + l, size - l, "%s", d);
|
969 |
g_free(d); |
970 |
} else {
|
971 |
l += snprintf(p + l, size - l, "%s", dev->info->name);
|
972 |
} |
973 |
} |
974 |
l += snprintf(p + l , size - l, "/");
|
975 |
|
976 |
return l;
|
977 |
} |
978 |
|
979 |
char* qdev_get_fw_dev_path(DeviceState *dev)
|
980 |
{ |
981 |
char path[128]; |
982 |
int l;
|
983 |
|
984 |
l = qdev_get_fw_dev_path_helper(dev, path, 128);
|
985 |
|
986 |
path[l-1] = '\0'; |
987 |
|
988 |
return strdup(path);
|
989 |
} |
990 |
|
991 |
void qdev_ref(DeviceState *dev)
|
992 |
{ |
993 |
dev->ref++; |
994 |
} |
995 |
|
996 |
void qdev_unref(DeviceState *dev)
|
997 |
{ |
998 |
g_assert(dev->ref > 0);
|
999 |
dev->ref--; |
1000 |
} |
1001 |
|
1002 |
void qdev_property_add(DeviceState *dev, const char *name, const char *type, |
1003 |
DevicePropertyAccessor *get, DevicePropertyAccessor *set, |
1004 |
DevicePropertyRelease *release, |
1005 |
void *opaque, Error **errp)
|
1006 |
{ |
1007 |
DeviceProperty *prop = g_malloc0(sizeof(*prop));
|
1008 |
|
1009 |
prop->name = g_strdup(name); |
1010 |
prop->type = g_strdup(type); |
1011 |
|
1012 |
prop->get = get; |
1013 |
prop->set = set; |
1014 |
prop->release = release; |
1015 |
prop->opaque = opaque; |
1016 |
|
1017 |
QTAILQ_INSERT_TAIL(&dev->properties, prop, node); |
1018 |
} |
1019 |
|
1020 |
static DeviceProperty *qdev_property_find(DeviceState *dev, const char *name) |
1021 |
{ |
1022 |
DeviceProperty *prop; |
1023 |
|
1024 |
QTAILQ_FOREACH(prop, &dev->properties, node) { |
1025 |
if (strcmp(prop->name, name) == 0) { |
1026 |
return prop;
|
1027 |
} |
1028 |
} |
1029 |
|
1030 |
return NULL; |
1031 |
} |
1032 |
|
1033 |
void qdev_property_get(DeviceState *dev, Visitor *v, const char *name, |
1034 |
Error **errp) |
1035 |
{ |
1036 |
DeviceProperty *prop = qdev_property_find(dev, name); |
1037 |
|
1038 |
if (prop == NULL) { |
1039 |
error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
|
1040 |
return;
|
1041 |
} |
1042 |
|
1043 |
if (!prop->get) {
|
1044 |
error_set(errp, QERR_PERMISSION_DENIED); |
1045 |
} else {
|
1046 |
prop->get(dev, v, prop->opaque, name, errp); |
1047 |
} |
1048 |
} |
1049 |
|
1050 |
void qdev_property_set(DeviceState *dev, Visitor *v, const char *name, |
1051 |
Error **errp) |
1052 |
{ |
1053 |
DeviceProperty *prop = qdev_property_find(dev, name); |
1054 |
|
1055 |
if (prop == NULL) { |
1056 |
error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
|
1057 |
return;
|
1058 |
} |
1059 |
|
1060 |
if (!prop->set) {
|
1061 |
error_set(errp, QERR_PERMISSION_DENIED); |
1062 |
} else {
|
1063 |
prop->set(dev, prop->opaque, v, name, errp); |
1064 |
} |
1065 |
} |
1066 |
|
1067 |
const char *qdev_property_get_type(DeviceState *dev, const char *name, Error **errp) |
1068 |
{ |
1069 |
DeviceProperty *prop = qdev_property_find(dev, name); |
1070 |
|
1071 |
if (prop == NULL) { |
1072 |
error_set(errp, QERR_PROPERTY_NOT_FOUND, dev->id?:"", name);
|
1073 |
return NULL; |
1074 |
} |
1075 |
|
1076 |
return prop->type;
|
1077 |
} |