]>
Commit | Line | Data |
---|---|---|
aae9460e PB |
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
aae9460e PB |
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 | ||
83c9f4ca | 28 | #include "hw/qdev.h" |
9c17d615 | 29 | #include "sysemu/sysemu.h" |
7b1b5d19 | 30 | #include "qapi/error.h" |
b4a42f81 | 31 | #include "qapi/qmp/qerror.h" |
7b1b5d19 | 32 | #include "qapi/visitor.h" |
0402a5d6 MT |
33 | #include "qapi/qmp/qjson.h" |
34 | #include "monitor/monitor.h" | |
0ee4de6c | 35 | #include "hw/hotplug.h" |
aae9460e | 36 | |
ee46d8a5 | 37 | int qdev_hotplug = 0; |
0ac8ef71 AW |
38 | static bool qdev_hot_added = false; |
39 | static bool qdev_hot_removed = false; | |
3418bd25 | 40 | |
4be9f0d1 AL |
41 | const VMStateDescription *qdev_get_vmsd(DeviceState *dev) |
42 | { | |
6e008585 AL |
43 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
44 | return dc->vmsd; | |
4be9f0d1 AL |
45 | } |
46 | ||
4be9f0d1 AL |
47 | const char *qdev_fw_name(DeviceState *dev) |
48 | { | |
6e008585 | 49 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
4be9f0d1 | 50 | |
6e008585 AL |
51 | if (dc->fw_name) { |
52 | return dc->fw_name; | |
4be9f0d1 AL |
53 | } |
54 | ||
55 | return object_get_typename(OBJECT(dev)); | |
56 | } | |
57 | ||
ca2cc788 PB |
58 | static void qdev_property_add_legacy(DeviceState *dev, Property *prop, |
59 | Error **errp); | |
60 | ||
0866aca1 | 61 | static void bus_remove_child(BusState *bus, DeviceState *child) |
0c17542d | 62 | { |
0866aca1 AL |
63 | BusChild *kid; |
64 | ||
65 | QTAILQ_FOREACH(kid, &bus->children, sibling) { | |
66 | if (kid->child == child) { | |
67 | char name[32]; | |
68 | ||
69 | snprintf(name, sizeof(name), "child[%d]", kid->index); | |
70 | QTAILQ_REMOVE(&bus->children, kid, sibling); | |
9d127820 PB |
71 | |
72 | /* This gives back ownership of kid->child back to us. */ | |
0866aca1 | 73 | object_property_del(OBJECT(bus), name, NULL); |
9d127820 | 74 | object_unref(OBJECT(kid->child)); |
0866aca1 AL |
75 | g_free(kid); |
76 | return; | |
77 | } | |
78 | } | |
79 | } | |
80 | ||
81 | static void bus_add_child(BusState *bus, DeviceState *child) | |
82 | { | |
83 | char name[32]; | |
84 | BusChild *kid = g_malloc0(sizeof(*kid)); | |
0c17542d | 85 | |
0c17542d MA |
86 | if (qdev_hotplug) { |
87 | assert(bus->allow_hotplug); | |
0c17542d | 88 | } |
a5296ca9 | 89 | |
0866aca1 AL |
90 | kid->index = bus->max_index++; |
91 | kid->child = child; | |
9d127820 | 92 | object_ref(OBJECT(kid->child)); |
a5296ca9 | 93 | |
0866aca1 AL |
94 | QTAILQ_INSERT_HEAD(&bus->children, kid, sibling); |
95 | ||
9d127820 | 96 | /* This transfers ownership of kid->child to the property. */ |
0866aca1 AL |
97 | snprintf(name, sizeof(name), "child[%d]", kid->index); |
98 | object_property_add_link(OBJECT(bus), name, | |
99 | object_get_typename(OBJECT(child)), | |
100 | (Object **)&kid->child, | |
101 | NULL); | |
102 | } | |
103 | ||
104 | void qdev_set_parent_bus(DeviceState *dev, BusState *bus) | |
105 | { | |
9fbe6127 | 106 | dev->parent_bus = bus; |
62d7ba66 | 107 | object_ref(OBJECT(bus)); |
0866aca1 | 108 | bus_add_child(bus, dev); |
0c17542d MA |
109 | } |
110 | ||
aae9460e PB |
111 | /* Create a new device. This only initializes the device state structure |
112 | and allows properties to be set. qdev_init should be called to | |
113 | initialize the actual device emulation. */ | |
02e2da45 | 114 | DeviceState *qdev_create(BusState *bus, const char *name) |
0bcdeda7 BS |
115 | { |
116 | DeviceState *dev; | |
117 | ||
118 | dev = qdev_try_create(bus, name); | |
119 | if (!dev) { | |
e92714c7 | 120 | if (bus) { |
312fd5f2 | 121 | error_report("Unknown device '%s' for bus '%s'", name, |
23e3fbec | 122 | object_get_typename(OBJECT(bus))); |
e92714c7 | 123 | } else { |
312fd5f2 | 124 | error_report("Unknown device '%s' for default sysbus", name); |
e92714c7 | 125 | } |
01ed1d52 | 126 | abort(); |
0bcdeda7 BS |
127 | } |
128 | ||
129 | return dev; | |
130 | } | |
131 | ||
da57febf | 132 | DeviceState *qdev_try_create(BusState *bus, const char *type) |
aae9460e | 133 | { |
9fbe6127 AL |
134 | DeviceState *dev; |
135 | ||
da57febf | 136 | if (object_class_by_name(type) == NULL) { |
4ed658ca AF |
137 | return NULL; |
138 | } | |
da57febf | 139 | dev = DEVICE(object_new(type)); |
9fbe6127 AL |
140 | if (!dev) { |
141 | return NULL; | |
142 | } | |
143 | ||
10c4c98a | 144 | if (!bus) { |
68694897 | 145 | bus = sysbus_get_default(); |
10c4c98a GH |
146 | } |
147 | ||
9fbe6127 | 148 | qdev_set_parent_bus(dev, bus); |
b09995ae | 149 | object_unref(OBJECT(dev)); |
9fbe6127 | 150 | return dev; |
aae9460e PB |
151 | } |
152 | ||
153 | /* Initialize a device. Device properties should be set before calling | |
154 | this function. IRQs and MMIO regions should be connected/mapped after | |
18cfeb52 MA |
155 | calling this function. |
156 | On failure, destroy the device and return negative value. | |
157 | Return 0 on success. */ | |
81a322d4 | 158 | int qdev_init(DeviceState *dev) |
aae9460e | 159 | { |
249d4172 | 160 | Error *local_err = NULL; |
959f733a | 161 | |
7983c8a3 | 162 | assert(!dev->realized); |
6e008585 | 163 | |
249d4172 AF |
164 | object_property_set_bool(OBJECT(dev), true, "realized", &local_err); |
165 | if (local_err != NULL) { | |
cffc5113 | 166 | qerror_report_err(local_err); |
249d4172 | 167 | error_free(local_err); |
02a5c4c9 | 168 | object_unparent(OBJECT(dev)); |
249d4172 | 169 | return -1; |
18cfeb52 | 170 | } |
249d4172 AF |
171 | return 0; |
172 | } | |
da57febf | 173 | |
249d4172 AF |
174 | static void device_realize(DeviceState *dev, Error **err) |
175 | { | |
176 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
da57febf | 177 | |
249d4172 AF |
178 | if (dc->init) { |
179 | int rc = dc->init(dev); | |
180 | if (rc < 0) { | |
181 | error_setg(err, "Device initialization failed."); | |
182 | return; | |
183 | } | |
5ab28c83 | 184 | } |
02e2da45 PB |
185 | } |
186 | ||
fe6c2117 AF |
187 | static void device_unrealize(DeviceState *dev, Error **errp) |
188 | { | |
189 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
190 | ||
191 | if (dc->exit) { | |
192 | int rc = dc->exit(dev); | |
193 | if (rc < 0) { | |
194 | error_setg(errp, "Device exit failed."); | |
195 | return; | |
196 | } | |
197 | } | |
198 | } | |
199 | ||
4d2ffa08 JK |
200 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
201 | int required_for_version) | |
202 | { | |
7983c8a3 | 203 | assert(!dev->realized); |
4d2ffa08 JK |
204 | dev->instance_id_alias = alias_id; |
205 | dev->alias_required_for_version = required_for_version; | |
206 | } | |
207 | ||
56f9107e | 208 | void qdev_unplug(DeviceState *dev, Error **errp) |
3418bd25 | 209 | { |
6e008585 AL |
210 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
211 | ||
120dc38f | 212 | if (dev->parent_bus && !dev->parent_bus->allow_hotplug) { |
56f9107e LC |
213 | error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); |
214 | return; | |
3418bd25 | 215 | } |
593831de | 216 | |
1a37eca1 IM |
217 | if (!dc->hotpluggable) { |
218 | error_set(errp, QERR_DEVICE_NO_HOTPLUG, | |
219 | object_get_typename(OBJECT(dev))); | |
220 | return; | |
221 | } | |
222 | ||
0ac8ef71 AW |
223 | qdev_hot_removed = true; |
224 | ||
5e954943 IM |
225 | if (dev->parent_bus && dev->parent_bus->hotplug_handler) { |
226 | hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp); | |
227 | } else { | |
228 | assert(dc->unplug != NULL); | |
229 | if (dc->unplug(dev) < 0) { /* legacy handler */ | |
230 | error_set(errp, QERR_UNDEFINED_ERROR); | |
231 | } | |
56f9107e | 232 | } |
3418bd25 GH |
233 | } |
234 | ||
ec990eb6 AL |
235 | static int qdev_reset_one(DeviceState *dev, void *opaque) |
236 | { | |
94afdadc | 237 | device_reset(dev); |
ec990eb6 AL |
238 | |
239 | return 0; | |
240 | } | |
241 | ||
b4694b7c IY |
242 | static int qbus_reset_one(BusState *bus, void *opaque) |
243 | { | |
0d936928 AL |
244 | BusClass *bc = BUS_GET_CLASS(bus); |
245 | if (bc->reset) { | |
dcc20931 | 246 | bc->reset(bus); |
b4694b7c IY |
247 | } |
248 | return 0; | |
249 | } | |
250 | ||
5af0a04b IY |
251 | void qdev_reset_all(DeviceState *dev) |
252 | { | |
dcc20931 | 253 | qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL); |
5af0a04b IY |
254 | } |
255 | ||
d0508c36 PB |
256 | void qbus_reset_all(BusState *bus) |
257 | { | |
dcc20931 | 258 | qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL); |
d0508c36 PB |
259 | } |
260 | ||
80376c3f IY |
261 | void qbus_reset_all_fn(void *opaque) |
262 | { | |
263 | BusState *bus = opaque; | |
d0508c36 | 264 | qbus_reset_all(bus); |
80376c3f IY |
265 | } |
266 | ||
3418bd25 GH |
267 | /* can be used as ->unplug() callback for the simple cases */ |
268 | int qdev_simple_unplug_cb(DeviceState *dev) | |
269 | { | |
270 | /* just zap it */ | |
02a5c4c9 | 271 | object_unparent(OBJECT(dev)); |
3418bd25 GH |
272 | return 0; |
273 | } | |
274 | ||
3b29a101 MT |
275 | |
276 | /* Like qdev_init(), but terminate program via error_report() instead of | |
e23a1b33 MA |
277 | returning an error value. This is okay during machine creation. |
278 | Don't use for hotplug, because there callers need to recover from | |
279 | failure. Exception: if you know the device's init() callback can't | |
280 | fail, then qdev_init_nofail() can't fail either, and is therefore | |
281 | usable even then. But relying on the device implementation that | |
282 | way is somewhat unclean, and best avoided. */ | |
283 | void qdev_init_nofail(DeviceState *dev) | |
284 | { | |
7de3abe5 AL |
285 | const char *typename = object_get_typename(OBJECT(dev)); |
286 | ||
bd6c9a61 | 287 | if (qdev_init(dev) < 0) { |
7de3abe5 | 288 | error_report("Initialization of device %s failed", typename); |
bd6c9a61 MA |
289 | exit(1); |
290 | } | |
e23a1b33 MA |
291 | } |
292 | ||
3418bd25 GH |
293 | void qdev_machine_creation_done(void) |
294 | { | |
295 | /* | |
296 | * ok, initial machine setup is done, starting from now we can | |
297 | * only create hotpluggable devices | |
298 | */ | |
299 | qdev_hotplug = 1; | |
300 | } | |
301 | ||
0ac8ef71 AW |
302 | bool qdev_machine_modified(void) |
303 | { | |
304 | return qdev_hot_added || qdev_hot_removed; | |
305 | } | |
306 | ||
02e2da45 | 307 | BusState *qdev_get_parent_bus(DeviceState *dev) |
aae9460e | 308 | { |
02e2da45 | 309 | return dev->parent_bus; |
aae9460e PB |
310 | } |
311 | ||
aae9460e PB |
312 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) |
313 | { | |
1e5b31e6 PC |
314 | dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler, |
315 | dev, n); | |
316 | dev->num_gpio_in += n; | |
aae9460e PB |
317 | } |
318 | ||
319 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) | |
320 | { | |
321 | assert(dev->num_gpio_out == 0); | |
322 | dev->num_gpio_out = n; | |
323 | dev->gpio_out = pins; | |
324 | } | |
325 | ||
326 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) | |
327 | { | |
328 | assert(n >= 0 && n < dev->num_gpio_in); | |
329 | return dev->gpio_in[n]; | |
330 | } | |
331 | ||
332 | void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) | |
333 | { | |
334 | assert(n >= 0 && n < dev->num_gpio_out); | |
335 | dev->gpio_out[n] = pin; | |
336 | } | |
337 | ||
02e2da45 | 338 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
4d6ae674 | 339 | { |
02e2da45 | 340 | BusState *bus; |
4d6ae674 | 341 | |
72cf2d4f | 342 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
4d6ae674 | 343 | if (strcmp(name, bus->name) == 0) { |
02e2da45 | 344 | return bus; |
4d6ae674 PB |
345 | } |
346 | } | |
347 | return NULL; | |
348 | } | |
349 | ||
0293214b PB |
350 | int qbus_walk_children(BusState *bus, |
351 | qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, | |
352 | qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, | |
353 | void *opaque) | |
81699d8a | 354 | { |
0866aca1 | 355 | BusChild *kid; |
81699d8a AL |
356 | int err; |
357 | ||
0293214b PB |
358 | if (pre_busfn) { |
359 | err = pre_busfn(bus, opaque); | |
81699d8a AL |
360 | if (err) { |
361 | return err; | |
362 | } | |
363 | } | |
364 | ||
0866aca1 | 365 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
0293214b PB |
366 | err = qdev_walk_children(kid->child, |
367 | pre_devfn, pre_busfn, | |
368 | post_devfn, post_busfn, opaque); | |
81699d8a AL |
369 | if (err < 0) { |
370 | return err; | |
371 | } | |
372 | } | |
373 | ||
0293214b PB |
374 | if (post_busfn) { |
375 | err = post_busfn(bus, opaque); | |
376 | if (err) { | |
377 | return err; | |
378 | } | |
379 | } | |
380 | ||
81699d8a AL |
381 | return 0; |
382 | } | |
383 | ||
0293214b PB |
384 | int qdev_walk_children(DeviceState *dev, |
385 | qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, | |
386 | qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, | |
387 | void *opaque) | |
81699d8a AL |
388 | { |
389 | BusState *bus; | |
390 | int err; | |
391 | ||
0293214b PB |
392 | if (pre_devfn) { |
393 | err = pre_devfn(dev, opaque); | |
81699d8a AL |
394 | if (err) { |
395 | return err; | |
396 | } | |
397 | } | |
398 | ||
399 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { | |
0293214b PB |
400 | err = qbus_walk_children(bus, pre_devfn, pre_busfn, |
401 | post_devfn, post_busfn, opaque); | |
81699d8a AL |
402 | if (err < 0) { |
403 | return err; | |
404 | } | |
405 | } | |
406 | ||
0293214b PB |
407 | if (post_devfn) { |
408 | err = post_devfn(dev, opaque); | |
409 | if (err) { | |
410 | return err; | |
411 | } | |
412 | } | |
413 | ||
81699d8a AL |
414 | return 0; |
415 | } | |
416 | ||
a2ee6b4f | 417 | DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
3418bd25 | 418 | { |
0866aca1 AL |
419 | BusChild *kid; |
420 | DeviceState *ret; | |
3418bd25 GH |
421 | BusState *child; |
422 | ||
0866aca1 AL |
423 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
424 | DeviceState *dev = kid->child; | |
425 | ||
426 | if (dev->id && strcmp(dev->id, id) == 0) { | |
3418bd25 | 427 | return dev; |
0866aca1 AL |
428 | } |
429 | ||
3418bd25 GH |
430 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
431 | ret = qdev_find_recursive(child, id); | |
432 | if (ret) { | |
433 | return ret; | |
434 | } | |
435 | } | |
436 | } | |
437 | return NULL; | |
438 | } | |
439 | ||
013e1182 | 440 | static void qbus_realize(BusState *bus, DeviceState *parent, const char *name) |
02e2da45 | 441 | { |
ac7d1ba6 | 442 | const char *typename = object_get_typename(OBJECT(bus)); |
61de3676 | 443 | BusClass *bc; |
d271de9f | 444 | char *buf; |
61de3676 | 445 | int i, len, bus_id; |
02e2da45 | 446 | |
013e1182 PB |
447 | bus->parent = parent; |
448 | ||
449 | if (name) { | |
450 | bus->name = g_strdup(name); | |
ac7d1ba6 | 451 | } else if (bus->parent && bus->parent->id) { |
61de3676 AG |
452 | /* parent device has id -> use it plus parent-bus-id for bus name */ |
453 | bus_id = bus->parent->num_child_bus; | |
454 | ||
ac7d1ba6 | 455 | len = strlen(bus->parent->id) + 16; |
7267c094 | 456 | buf = g_malloc(len); |
61de3676 | 457 | snprintf(buf, len, "%s.%d", bus->parent->id, bus_id); |
d271de9f GH |
458 | bus->name = buf; |
459 | } else { | |
61de3676 AG |
460 | /* no id -> use lowercase bus type plus global bus-id for bus name */ |
461 | bc = BUS_GET_CLASS(bus); | |
462 | bus_id = bc->automatic_ids++; | |
463 | ||
0d936928 | 464 | len = strlen(typename) + 16; |
7267c094 | 465 | buf = g_malloc(len); |
61de3676 AG |
466 | len = snprintf(buf, len, "%s.%d", typename, bus_id); |
467 | for (i = 0; i < len; i++) { | |
bb87ece5 | 468 | buf[i] = qemu_tolower(buf[i]); |
61de3676 | 469 | } |
d271de9f GH |
470 | bus->name = buf; |
471 | } | |
472 | ||
ac7d1ba6 AL |
473 | if (bus->parent) { |
474 | QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling); | |
475 | bus->parent->num_child_bus++; | |
476 | object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL); | |
b09995ae | 477 | object_unref(OBJECT(bus)); |
8185d216 | 478 | } else if (bus != sysbus_get_default()) { |
80376c3f IY |
479 | /* TODO: once all bus devices are qdevified, |
480 | only reset handler for main_system_bus should be registered here. */ | |
481 | qemu_register_reset(qbus_reset_all_fn, bus); | |
02e2da45 | 482 | } |
cd739fb6 GH |
483 | } |
484 | ||
6853d27a PB |
485 | static void bus_unparent(Object *obj) |
486 | { | |
487 | BusState *bus = BUS(obj); | |
488 | BusChild *kid; | |
489 | ||
490 | while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) { | |
491 | DeviceState *dev = kid->child; | |
02a5c4c9 | 492 | object_unparent(OBJECT(dev)); |
6853d27a PB |
493 | } |
494 | if (bus->parent) { | |
495 | QLIST_REMOVE(bus, sibling); | |
496 | bus->parent->num_child_bus--; | |
497 | bus->parent = NULL; | |
498 | } else { | |
499 | assert(bus != sysbus_get_default()); /* main_system_bus is never freed */ | |
500 | qemu_unregister_reset(qbus_reset_all_fn, bus); | |
501 | } | |
502 | } | |
503 | ||
fb17dfe0 | 504 | void qbus_create_inplace(void *bus, size_t size, const char *typename, |
0d936928 | 505 | DeviceState *parent, const char *name) |
cd739fb6 | 506 | { |
213f0c4f | 507 | object_initialize(bus, size, typename); |
013e1182 | 508 | qbus_realize(bus, parent, name); |
02e2da45 | 509 | } |
cae4956e | 510 | |
0d936928 | 511 | BusState *qbus_create(const char *typename, DeviceState *parent, const char *name) |
2da8bb92 | 512 | { |
cd739fb6 GH |
513 | BusState *bus; |
514 | ||
0d936928 | 515 | bus = BUS(object_new(typename)); |
013e1182 | 516 | qbus_realize(bus, parent, name); |
ac7d1ba6 | 517 | |
02e2da45 | 518 | return bus; |
2da8bb92 IY |
519 | } |
520 | ||
0d936928 AL |
521 | static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev) |
522 | { | |
523 | BusClass *bc = BUS_GET_CLASS(bus); | |
524 | ||
525 | if (bc->get_fw_dev_path) { | |
526 | return bc->get_fw_dev_path(dev); | |
131ec1bd | 527 | } |
0d936928 AL |
528 | |
529 | return NULL; | |
131ec1bd GH |
530 | } |
531 | ||
1ca4d09a GN |
532 | static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size) |
533 | { | |
534 | int l = 0; | |
535 | ||
536 | if (dev && dev->parent_bus) { | |
537 | char *d; | |
538 | l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size); | |
0d936928 AL |
539 | d = bus_get_fw_dev_path(dev->parent_bus, dev); |
540 | if (d) { | |
1ca4d09a | 541 | l += snprintf(p + l, size - l, "%s", d); |
7267c094 | 542 | g_free(d); |
1ca4d09a | 543 | } else { |
bbfa18fc | 544 | return l; |
1ca4d09a GN |
545 | } |
546 | } | |
547 | l += snprintf(p + l , size - l, "/"); | |
548 | ||
549 | return l; | |
550 | } | |
551 | ||
552 | char* qdev_get_fw_dev_path(DeviceState *dev) | |
553 | { | |
554 | char path[128]; | |
555 | int l; | |
556 | ||
557 | l = qdev_get_fw_dev_path_helper(dev, path, 128); | |
558 | ||
559 | path[l-1] = '\0'; | |
560 | ||
a5cf8262 | 561 | return g_strdup(path); |
1ca4d09a | 562 | } |
85ed303b | 563 | |
09e5ab63 | 564 | char *qdev_get_dev_path(DeviceState *dev) |
85ed303b | 565 | { |
0d936928 | 566 | BusClass *bc; |
09e5ab63 AL |
567 | |
568 | if (!dev || !dev->parent_bus) { | |
569 | return NULL; | |
570 | } | |
571 | ||
0d936928 AL |
572 | bc = BUS_GET_CLASS(dev->parent_bus); |
573 | if (bc->get_dev_path) { | |
574 | return bc->get_dev_path(dev); | |
09e5ab63 AL |
575 | } |
576 | ||
577 | return NULL; | |
44677ded | 578 | } |
a5296ca9 AL |
579 | |
580 | /** | |
581 | * Legacy property handling | |
582 | */ | |
583 | ||
57c9fafe | 584 | static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque, |
a5296ca9 AL |
585 | const char *name, Error **errp) |
586 | { | |
57c9fafe | 587 | DeviceState *dev = DEVICE(obj); |
a5296ca9 AL |
588 | Property *prop = opaque; |
589 | ||
e3cb6ba6 PB |
590 | char buffer[1024]; |
591 | char *ptr = buffer; | |
a5296ca9 | 592 | |
e3cb6ba6 PB |
593 | prop->info->print(dev, prop, buffer, sizeof(buffer)); |
594 | visit_type_str(v, &ptr, name, errp); | |
a5296ca9 AL |
595 | } |
596 | ||
a5296ca9 AL |
597 | /** |
598 | * @qdev_add_legacy_property - adds a legacy property | |
599 | * | |
600 | * Do not use this is new code! Properties added through this interface will | |
ca2cc788 | 601 | * be given names and types in the "legacy" namespace. |
a5296ca9 | 602 | * |
68ee3569 PB |
603 | * Legacy properties are string versions of other OOM properties. The format |
604 | * of the string depends on the property type. | |
a5296ca9 AL |
605 | */ |
606 | void qdev_property_add_legacy(DeviceState *dev, Property *prop, | |
607 | Error **errp) | |
608 | { | |
7ce7ffe0 | 609 | gchar *name; |
a5296ca9 | 610 | |
f3be016d | 611 | /* Register pointer properties as legacy properties */ |
03ff7770 | 612 | if (!prop->info->print && prop->info->get) { |
68ee3569 PB |
613 | return; |
614 | } | |
f3be016d | 615 | |
ca2cc788 | 616 | name = g_strdup_printf("legacy-%s", prop->name); |
7ce7ffe0 | 617 | object_property_add(OBJECT(dev), name, "str", |
68ee3569 | 618 | prop->info->print ? qdev_get_legacy_property : prop->info->get, |
03ff7770 | 619 | NULL, |
57c9fafe AL |
620 | NULL, |
621 | prop, errp); | |
a5296ca9 | 622 | |
ca2cc788 PB |
623 | g_free(name); |
624 | } | |
625 | ||
626 | /** | |
627 | * @qdev_property_add_static - add a @Property to a device. | |
628 | * | |
629 | * Static properties access data in a struct. The actual type of the | |
630 | * property and the field depends on the property type. | |
631 | */ | |
632 | void qdev_property_add_static(DeviceState *dev, Property *prop, | |
633 | Error **errp) | |
634 | { | |
fdae245f PB |
635 | Error *local_err = NULL; |
636 | Object *obj = OBJECT(dev); | |
637 | ||
d822979b PB |
638 | /* |
639 | * TODO qdev_prop_ptr does not have getters or setters. It must | |
640 | * go now that it can be replaced with links. The test should be | |
641 | * removed along with it: all static properties are read/write. | |
642 | */ | |
643 | if (!prop->info->get && !prop->info->set) { | |
644 | return; | |
645 | } | |
646 | ||
fdae245f | 647 | object_property_add(obj, prop->name, prop->info->name, |
57c9fafe | 648 | prop->info->get, prop->info->set, |
dd0ba250 | 649 | prop->info->release, |
fdae245f PB |
650 | prop, &local_err); |
651 | ||
652 | if (local_err) { | |
653 | error_propagate(errp, local_err); | |
654 | return; | |
655 | } | |
656 | if (prop->qtype == QTYPE_NONE) { | |
657 | return; | |
658 | } | |
659 | ||
660 | if (prop->qtype == QTYPE_QBOOL) { | |
5433a0a8 | 661 | object_property_set_bool(obj, prop->defval, prop->name, &error_abort); |
fdae245f PB |
662 | } else if (prop->info->enum_table) { |
663 | object_property_set_str(obj, prop->info->enum_table[prop->defval], | |
5433a0a8 | 664 | prop->name, &error_abort); |
fdae245f | 665 | } else if (prop->qtype == QTYPE_QINT) { |
5433a0a8 | 666 | object_property_set_int(obj, prop->defval, prop->name, &error_abort); |
fdae245f | 667 | } |
6a146eba | 668 | } |
1de81d28 | 669 | |
249d4172 AF |
670 | static bool device_get_realized(Object *obj, Error **err) |
671 | { | |
672 | DeviceState *dev = DEVICE(obj); | |
673 | return dev->realized; | |
674 | } | |
675 | ||
676 | static void device_set_realized(Object *obj, bool value, Error **err) | |
677 | { | |
678 | DeviceState *dev = DEVICE(obj); | |
679 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
680 | Error *local_err = NULL; | |
681 | ||
1a37eca1 IM |
682 | if (dev->hotplugged && !dc->hotpluggable) { |
683 | error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj)); | |
684 | return; | |
685 | } | |
686 | ||
249d4172 | 687 | if (value && !dev->realized) { |
249d4172 AF |
688 | if (!obj->parent && local_err == NULL) { |
689 | static int unattached_count; | |
690 | gchar *name = g_strdup_printf("device[%d]", unattached_count++); | |
691 | ||
692 | object_property_add_child(container_get(qdev_get_machine(), | |
693 | "/unattached"), | |
694 | name, obj, &local_err); | |
695 | g_free(name); | |
696 | } | |
697 | ||
a7ddba52 IM |
698 | if (dc->realize) { |
699 | dc->realize(dev, &local_err); | |
700 | } | |
701 | ||
5e954943 IM |
702 | if (dev->parent_bus && dev->parent_bus->hotplug_handler && |
703 | local_err == NULL) { | |
704 | hotplug_handler_plug(dev->parent_bus->hotplug_handler, | |
705 | dev, &local_err); | |
706 | } | |
707 | ||
249d4172 AF |
708 | if (qdev_get_vmsd(dev) && local_err == NULL) { |
709 | vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, | |
710 | dev->instance_id_alias, | |
711 | dev->alias_required_for_version); | |
712 | } | |
713 | if (dev->hotplugged && local_err == NULL) { | |
714 | device_reset(dev); | |
715 | } | |
716 | } else if (!value && dev->realized) { | |
fe6c2117 AF |
717 | if (qdev_get_vmsd(dev)) { |
718 | vmstate_unregister(dev, qdev_get_vmsd(dev), dev); | |
719 | } | |
249d4172 AF |
720 | if (dc->unrealize) { |
721 | dc->unrealize(dev, &local_err); | |
722 | } | |
723 | } | |
724 | ||
725 | if (local_err != NULL) { | |
726 | error_propagate(err, local_err); | |
727 | return; | |
728 | } | |
729 | ||
730 | dev->realized = value; | |
731 | } | |
732 | ||
1a37eca1 IM |
733 | static bool device_get_hotpluggable(Object *obj, Error **err) |
734 | { | |
735 | DeviceClass *dc = DEVICE_GET_CLASS(obj); | |
736 | DeviceState *dev = DEVICE(obj); | |
737 | ||
2b81b35f AF |
738 | return dc->hotpluggable && (dev->parent_bus == NULL || |
739 | dev->parent_bus->allow_hotplug); | |
1a37eca1 IM |
740 | } |
741 | ||
9674bfe4 AL |
742 | static void device_initfn(Object *obj) |
743 | { | |
744 | DeviceState *dev = DEVICE(obj); | |
bce54474 | 745 | ObjectClass *class; |
9674bfe4 AL |
746 | Property *prop; |
747 | ||
748 | if (qdev_hotplug) { | |
749 | dev->hotplugged = 1; | |
750 | qdev_hot_added = true; | |
751 | } | |
752 | ||
753 | dev->instance_id_alias = -1; | |
7983c8a3 | 754 | dev->realized = false; |
9674bfe4 | 755 | |
249d4172 AF |
756 | object_property_add_bool(obj, "realized", |
757 | device_get_realized, device_set_realized, NULL); | |
1a37eca1 IM |
758 | object_property_add_bool(obj, "hotpluggable", |
759 | device_get_hotpluggable, NULL, NULL); | |
249d4172 | 760 | |
bce54474 PB |
761 | class = object_get_class(OBJECT(dev)); |
762 | do { | |
763 | for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) { | |
5433a0a8 PC |
764 | qdev_property_add_legacy(dev, prop, &error_abort); |
765 | qdev_property_add_static(dev, prop, &error_abort); | |
bce54474 | 766 | } |
bce54474 PB |
767 | class = object_class_get_parent(class); |
768 | } while (class != object_class_by_name(TYPE_DEVICE)); | |
9674bfe4 | 769 | |
f968fc68 | 770 | object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS, |
5433a0a8 | 771 | (Object **)&dev->parent_bus, &error_abort); |
9674bfe4 AL |
772 | } |
773 | ||
99a0b036 EH |
774 | static void device_post_init(Object *obj) |
775 | { | |
5433a0a8 | 776 | qdev_prop_set_globals(DEVICE(obj), &error_abort); |
99a0b036 EH |
777 | } |
778 | ||
60adba37 AL |
779 | /* Unlink device from bus and free the structure. */ |
780 | static void device_finalize(Object *obj) | |
781 | { | |
782 | DeviceState *dev = DEVICE(obj); | |
06f7f2bb PB |
783 | if (dev->opts) { |
784 | qemu_opts_del(dev->opts); | |
60adba37 | 785 | } |
60adba37 AL |
786 | } |
787 | ||
bce54474 PB |
788 | static void device_class_base_init(ObjectClass *class, void *data) |
789 | { | |
790 | DeviceClass *klass = DEVICE_CLASS(class); | |
791 | ||
792 | /* We explicitly look up properties in the superclasses, | |
793 | * so do not propagate them to the subclasses. | |
794 | */ | |
795 | klass->props = NULL; | |
60adba37 AL |
796 | } |
797 | ||
5d5b24d0 | 798 | static void device_unparent(Object *obj) |
667d22d1 PB |
799 | { |
800 | DeviceState *dev = DEVICE(obj); | |
06f7f2bb | 801 | BusState *bus; |
0402a5d6 | 802 | QObject *event_data; |
b1ee5829 | 803 | bool have_realized = dev->realized; |
667d22d1 | 804 | |
06f7f2bb PB |
805 | while (dev->num_child_bus) { |
806 | bus = QLIST_FIRST(&dev->child_bus); | |
6780a22c | 807 | object_unparent(OBJECT(bus)); |
06f7f2bb PB |
808 | } |
809 | if (dev->realized) { | |
fe6c2117 | 810 | object_property_set_bool(obj, false, "realized", NULL); |
06f7f2bb PB |
811 | } |
812 | if (dev->parent_bus) { | |
5d5b24d0 | 813 | bus_remove_child(dev->parent_bus, dev); |
62d7ba66 PB |
814 | object_unref(OBJECT(dev->parent_bus)); |
815 | dev->parent_bus = NULL; | |
5d5b24d0 | 816 | } |
0402a5d6 | 817 | |
b1ee5829 AL |
818 | /* Only send event if the device had been completely realized */ |
819 | if (have_realized) { | |
820 | gchar *path = object_get_canonical_path(OBJECT(dev)); | |
821 | ||
822 | if (dev->id) { | |
823 | event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }", | |
824 | dev->id, path); | |
825 | } else { | |
826 | event_data = qobject_from_jsonf("{ 'path': %s }", path); | |
827 | } | |
828 | monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data); | |
829 | qobject_decref(event_data); | |
830 | g_free(path); | |
0402a5d6 | 831 | } |
667d22d1 PB |
832 | } |
833 | ||
834 | static void device_class_init(ObjectClass *class, void *data) | |
835 | { | |
249d4172 AF |
836 | DeviceClass *dc = DEVICE_CLASS(class); |
837 | ||
5d5b24d0 | 838 | class->unparent = device_unparent; |
249d4172 | 839 | dc->realize = device_realize; |
fe6c2117 | 840 | dc->unrealize = device_unrealize; |
267a3264 IM |
841 | |
842 | /* by default all devices were considered as hotpluggable, | |
843 | * so with intent to check it in generic qdev_unplug() / | |
844 | * device_set_realized() functions make every device | |
845 | * hotpluggable. Devices that shouldn't be hotpluggable, | |
846 | * should override it in their class_init() | |
847 | */ | |
848 | dc->hotpluggable = true; | |
667d22d1 PB |
849 | } |
850 | ||
94afdadc AL |
851 | void device_reset(DeviceState *dev) |
852 | { | |
853 | DeviceClass *klass = DEVICE_GET_CLASS(dev); | |
854 | ||
855 | if (klass->reset) { | |
856 | klass->reset(dev); | |
857 | } | |
858 | } | |
859 | ||
f05f6b4a PB |
860 | Object *qdev_get_machine(void) |
861 | { | |
862 | static Object *dev; | |
863 | ||
864 | if (dev == NULL) { | |
dfe47e70 | 865 | dev = container_get(object_get_root(), "/machine"); |
f05f6b4a PB |
866 | } |
867 | ||
868 | return dev; | |
869 | } | |
870 | ||
8c43a6f0 | 871 | static const TypeInfo device_type_info = { |
32fea402 AL |
872 | .name = TYPE_DEVICE, |
873 | .parent = TYPE_OBJECT, | |
874 | .instance_size = sizeof(DeviceState), | |
9674bfe4 | 875 | .instance_init = device_initfn, |
99a0b036 | 876 | .instance_post_init = device_post_init, |
60adba37 | 877 | .instance_finalize = device_finalize, |
bce54474 | 878 | .class_base_init = device_class_base_init, |
667d22d1 | 879 | .class_init = device_class_init, |
32fea402 AL |
880 | .abstract = true, |
881 | .class_size = sizeof(DeviceClass), | |
882 | }; | |
883 | ||
ac7d1ba6 AL |
884 | static void qbus_initfn(Object *obj) |
885 | { | |
886 | BusState *bus = BUS(obj); | |
887 | ||
888 | QTAILQ_INIT(&bus->children); | |
0ee4de6c IM |
889 | object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY, |
890 | TYPE_HOTPLUG_HANDLER, | |
891 | (Object **)&bus->hotplug_handler, NULL); | |
ac7d1ba6 AL |
892 | } |
893 | ||
bbfa18fc AK |
894 | static char *default_bus_get_fw_dev_path(DeviceState *dev) |
895 | { | |
896 | return g_strdup(object_get_typename(OBJECT(dev))); | |
897 | } | |
898 | ||
6853d27a PB |
899 | static void bus_class_init(ObjectClass *class, void *data) |
900 | { | |
bbfa18fc AK |
901 | BusClass *bc = BUS_CLASS(class); |
902 | ||
6853d27a | 903 | class->unparent = bus_unparent; |
bbfa18fc | 904 | bc->get_fw_dev_path = default_bus_get_fw_dev_path; |
6853d27a PB |
905 | } |
906 | ||
ac7d1ba6 AL |
907 | static void qbus_finalize(Object *obj) |
908 | { | |
909 | BusState *bus = BUS(obj); | |
ac7d1ba6 | 910 | |
ac7d1ba6 AL |
911 | g_free((char *)bus->name); |
912 | } | |
913 | ||
0d936928 AL |
914 | static const TypeInfo bus_info = { |
915 | .name = TYPE_BUS, | |
916 | .parent = TYPE_OBJECT, | |
917 | .instance_size = sizeof(BusState), | |
918 | .abstract = true, | |
919 | .class_size = sizeof(BusClass), | |
ac7d1ba6 AL |
920 | .instance_init = qbus_initfn, |
921 | .instance_finalize = qbus_finalize, | |
6853d27a | 922 | .class_init = bus_class_init, |
0d936928 AL |
923 | }; |
924 | ||
83f7d43a | 925 | static void qdev_register_types(void) |
32fea402 | 926 | { |
0d936928 | 927 | type_register_static(&bus_info); |
32fea402 AL |
928 | type_register_static(&device_type_info); |
929 | } | |
930 | ||
83f7d43a | 931 | type_init(qdev_register_types) |