2
0

qdev.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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.1 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. /* The theory here is that it should be possible to create a machine without
  20. knowledge of specific devices. Historically board init routines have
  21. passed a bunch of arguments to each device, requiring the board know
  22. exactly which device it is dealing with. This file provides an abstract
  23. API for device configuration and initialization. Devices will generally
  24. inherit from a particular bus (e.g. PCI or I2C) rather than
  25. this API directly. */
  26. #include "qemu/osdep.h"
  27. #include "qapi/error.h"
  28. #include "qapi/qapi-events-qdev.h"
  29. #include "qapi/qmp/qdict.h"
  30. #include "qapi/visitor.h"
  31. #include "qemu/error-report.h"
  32. #include "qemu/option.h"
  33. #include "hw/irq.h"
  34. #include "hw/qdev-properties.h"
  35. #include "hw/boards.h"
  36. #include "hw/sysbus.h"
  37. #include "hw/qdev-clock.h"
  38. #include "migration/vmstate.h"
  39. #include "trace.h"
  40. static bool qdev_hot_added = false;
  41. bool qdev_hot_removed = false;
  42. const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
  43. {
  44. DeviceClass *dc = DEVICE_GET_CLASS(dev);
  45. return dc->vmsd;
  46. }
  47. static void bus_free_bus_child(BusChild *kid)
  48. {
  49. object_unref(OBJECT(kid->child));
  50. g_free(kid);
  51. }
  52. static void bus_remove_child(BusState *bus, DeviceState *child)
  53. {
  54. BusChild *kid;
  55. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  56. if (kid->child == child) {
  57. char name[32];
  58. snprintf(name, sizeof(name), "child[%d]", kid->index);
  59. QTAILQ_REMOVE_RCU(&bus->children, kid, sibling);
  60. bus->num_children--;
  61. /* This gives back ownership of kid->child back to us. */
  62. object_property_del(OBJECT(bus), name);
  63. /* free the bus kid, when it is safe to do so*/
  64. call_rcu(kid, bus_free_bus_child, rcu);
  65. break;
  66. }
  67. }
  68. }
  69. static void bus_add_child(BusState *bus, DeviceState *child)
  70. {
  71. char name[32];
  72. BusChild *kid = g_malloc0(sizeof(*kid));
  73. bus->num_children++;
  74. kid->index = bus->max_index++;
  75. kid->child = child;
  76. object_ref(OBJECT(kid->child));
  77. QTAILQ_INSERT_HEAD_RCU(&bus->children, kid, sibling);
  78. /* This transfers ownership of kid->child to the property. */
  79. snprintf(name, sizeof(name), "child[%d]", kid->index);
  80. object_property_add_link(OBJECT(bus), name,
  81. object_get_typename(OBJECT(child)),
  82. (Object **)&kid->child,
  83. NULL, /* read-only property */
  84. 0);
  85. }
  86. static bool bus_check_address(BusState *bus, DeviceState *child, Error **errp)
  87. {
  88. BusClass *bc = BUS_GET_CLASS(bus);
  89. return !bc->check_address || bc->check_address(bus, child, errp);
  90. }
  91. bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp)
  92. {
  93. BusState *old_parent_bus = dev->parent_bus;
  94. DeviceClass *dc = DEVICE_GET_CLASS(dev);
  95. assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type));
  96. if (!bus_check_address(bus, dev, errp)) {
  97. return false;
  98. }
  99. if (old_parent_bus) {
  100. trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)),
  101. old_parent_bus, object_get_typename(OBJECT(old_parent_bus)),
  102. OBJECT(bus), object_get_typename(OBJECT(bus)));
  103. /*
  104. * Keep a reference to the device while it's not plugged into
  105. * any bus, to avoid it potentially evaporating when it is
  106. * dereffed in bus_remove_child().
  107. * Also keep the ref of the parent bus until the end, so that
  108. * we can safely call resettable_change_parent() below.
  109. */
  110. object_ref(OBJECT(dev));
  111. bus_remove_child(dev->parent_bus, dev);
  112. }
  113. dev->parent_bus = bus;
  114. object_ref(OBJECT(bus));
  115. bus_add_child(bus, dev);
  116. if (dev->realized) {
  117. resettable_change_parent(OBJECT(dev), OBJECT(bus),
  118. OBJECT(old_parent_bus));
  119. }
  120. if (old_parent_bus) {
  121. object_unref(OBJECT(old_parent_bus));
  122. object_unref(OBJECT(dev));
  123. }
  124. return true;
  125. }
  126. DeviceState *qdev_new(const char *name)
  127. {
  128. ObjectClass *oc = object_class_by_name(name);
  129. #ifdef CONFIG_MODULES
  130. if (!oc) {
  131. int rv = module_load_qom(name, &error_fatal);
  132. if (rv > 0) {
  133. oc = object_class_by_name(name);
  134. } else {
  135. error_report("could not find a module for type '%s'", name);
  136. exit(1);
  137. }
  138. }
  139. #endif
  140. if (!oc) {
  141. error_report("unknown type '%s'", name);
  142. abort();
  143. }
  144. return DEVICE(object_new(name));
  145. }
  146. DeviceState *qdev_try_new(const char *name)
  147. {
  148. if (!module_object_class_by_name(name)) {
  149. return NULL;
  150. }
  151. return DEVICE(object_new(name));
  152. }
  153. static QTAILQ_HEAD(, DeviceListener) device_listeners
  154. = QTAILQ_HEAD_INITIALIZER(device_listeners);
  155. enum ListenerDirection { Forward, Reverse };
  156. #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
  157. do { \
  158. DeviceListener *_listener; \
  159. \
  160. switch (_direction) { \
  161. case Forward: \
  162. QTAILQ_FOREACH(_listener, &device_listeners, link) { \
  163. if (_listener->_callback) { \
  164. _listener->_callback(_listener, ##_args); \
  165. } \
  166. } \
  167. break; \
  168. case Reverse: \
  169. QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
  170. link) { \
  171. if (_listener->_callback) { \
  172. _listener->_callback(_listener, ##_args); \
  173. } \
  174. } \
  175. break; \
  176. default: \
  177. abort(); \
  178. } \
  179. } while (0)
  180. static int device_listener_add(DeviceState *dev, void *opaque)
  181. {
  182. DEVICE_LISTENER_CALL(realize, Forward, dev);
  183. return 0;
  184. }
  185. void device_listener_register(DeviceListener *listener)
  186. {
  187. QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
  188. qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
  189. NULL, NULL);
  190. }
  191. void device_listener_unregister(DeviceListener *listener)
  192. {
  193. QTAILQ_REMOVE(&device_listeners, listener, link);
  194. }
  195. bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp)
  196. {
  197. ERRP_GUARD();
  198. DeviceListener *listener;
  199. QTAILQ_FOREACH(listener, &device_listeners, link) {
  200. if (listener->hide_device) {
  201. if (listener->hide_device(listener, opts, from_json, errp)) {
  202. return true;
  203. } else if (*errp) {
  204. return false;
  205. }
  206. }
  207. }
  208. return false;
  209. }
  210. void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
  211. int required_for_version)
  212. {
  213. assert(!dev->realized);
  214. dev->instance_id_alias = alias_id;
  215. dev->alias_required_for_version = required_for_version;
  216. }
  217. void device_cold_reset(DeviceState *dev)
  218. {
  219. resettable_reset(OBJECT(dev), RESET_TYPE_COLD);
  220. }
  221. bool device_is_in_reset(DeviceState *dev)
  222. {
  223. return resettable_is_in_reset(OBJECT(dev));
  224. }
  225. static ResettableState *device_get_reset_state(Object *obj)
  226. {
  227. DeviceState *dev = DEVICE(obj);
  228. return &dev->reset;
  229. }
  230. static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
  231. void *opaque, ResetType type)
  232. {
  233. DeviceState *dev = DEVICE(obj);
  234. BusState *bus;
  235. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  236. cb(OBJECT(bus), opaque, type);
  237. }
  238. }
  239. bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
  240. {
  241. assert(!dev->realized && !dev->parent_bus);
  242. if (bus) {
  243. if (!qdev_set_parent_bus(dev, bus, errp)) {
  244. return false;
  245. }
  246. } else {
  247. assert(!DEVICE_GET_CLASS(dev)->bus_type);
  248. }
  249. return object_property_set_bool(OBJECT(dev), "realized", true, errp);
  250. }
  251. bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp)
  252. {
  253. bool ret;
  254. ret = qdev_realize(dev, bus, errp);
  255. object_unref(OBJECT(dev));
  256. return ret;
  257. }
  258. void qdev_unrealize(DeviceState *dev)
  259. {
  260. object_property_set_bool(OBJECT(dev), "realized", false, &error_abort);
  261. }
  262. static int qdev_assert_realized_properly_cb(Object *obj, void *opaque)
  263. {
  264. DeviceState *dev = DEVICE(object_dynamic_cast(obj, TYPE_DEVICE));
  265. DeviceClass *dc;
  266. if (dev) {
  267. dc = DEVICE_GET_CLASS(dev);
  268. assert(dev->realized);
  269. assert(dev->parent_bus || !dc->bus_type);
  270. }
  271. return 0;
  272. }
  273. void qdev_assert_realized_properly(void)
  274. {
  275. object_child_foreach_recursive(object_get_root(),
  276. qdev_assert_realized_properly_cb, NULL);
  277. }
  278. bool qdev_machine_modified(void)
  279. {
  280. return qdev_hot_added || qdev_hot_removed;
  281. }
  282. BusState *qdev_get_parent_bus(const DeviceState *dev)
  283. {
  284. return dev->parent_bus;
  285. }
  286. BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
  287. {
  288. BusState *bus;
  289. Object *child = object_resolve_path_component(OBJECT(dev), name);
  290. bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
  291. if (bus) {
  292. return bus;
  293. }
  294. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  295. if (strcmp(name, bus->name) == 0) {
  296. return bus;
  297. }
  298. }
  299. return NULL;
  300. }
  301. int qdev_walk_children(DeviceState *dev,
  302. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  303. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  304. void *opaque)
  305. {
  306. BusState *bus;
  307. int err;
  308. if (pre_devfn) {
  309. err = pre_devfn(dev, opaque);
  310. if (err) {
  311. return err;
  312. }
  313. }
  314. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  315. err = qbus_walk_children(bus, pre_devfn, pre_busfn,
  316. post_devfn, post_busfn, opaque);
  317. if (err < 0) {
  318. return err;
  319. }
  320. }
  321. if (post_devfn) {
  322. err = post_devfn(dev, opaque);
  323. if (err) {
  324. return err;
  325. }
  326. }
  327. return 0;
  328. }
  329. DeviceState *qdev_find_recursive(BusState *bus, const char *id)
  330. {
  331. BusChild *kid;
  332. DeviceState *ret;
  333. BusState *child;
  334. WITH_RCU_READ_LOCK_GUARD() {
  335. QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
  336. DeviceState *dev = kid->child;
  337. if (dev->id && strcmp(dev->id, id) == 0) {
  338. return dev;
  339. }
  340. QLIST_FOREACH(child, &dev->child_bus, sibling) {
  341. ret = qdev_find_recursive(child, id);
  342. if (ret) {
  343. return ret;
  344. }
  345. }
  346. }
  347. }
  348. return NULL;
  349. }
  350. char *qdev_get_dev_path(DeviceState *dev)
  351. {
  352. BusClass *bc;
  353. if (!dev || !dev->parent_bus) {
  354. return NULL;
  355. }
  356. bc = BUS_GET_CLASS(dev->parent_bus);
  357. if (bc->get_dev_path) {
  358. return bc->get_dev_path(dev);
  359. }
  360. return NULL;
  361. }
  362. void qdev_add_unplug_blocker(DeviceState *dev, Error *reason)
  363. {
  364. dev->unplug_blockers = g_slist_prepend(dev->unplug_blockers, reason);
  365. }
  366. void qdev_del_unplug_blocker(DeviceState *dev, Error *reason)
  367. {
  368. dev->unplug_blockers = g_slist_remove(dev->unplug_blockers, reason);
  369. }
  370. bool qdev_unplug_blocked(DeviceState *dev, Error **errp)
  371. {
  372. if (dev->unplug_blockers) {
  373. error_propagate(errp, error_copy(dev->unplug_blockers->data));
  374. return true;
  375. }
  376. return false;
  377. }
  378. static bool device_get_realized(Object *obj, Error **errp)
  379. {
  380. DeviceState *dev = DEVICE(obj);
  381. return dev->realized;
  382. }
  383. static bool check_only_migratable(Object *obj, Error **errp)
  384. {
  385. DeviceClass *dc = DEVICE_GET_CLASS(obj);
  386. if (!vmstate_check_only_migratable(dc->vmsd)) {
  387. error_setg(errp, "Device %s is not migratable, but "
  388. "--only-migratable was specified",
  389. object_get_typename(obj));
  390. return false;
  391. }
  392. return true;
  393. }
  394. static void device_set_realized(Object *obj, bool value, Error **errp)
  395. {
  396. DeviceState *dev = DEVICE(obj);
  397. DeviceClass *dc = DEVICE_GET_CLASS(dev);
  398. HotplugHandler *hotplug_ctrl;
  399. BusState *bus;
  400. NamedClockList *ncl;
  401. Error *local_err = NULL;
  402. bool unattached_parent = false;
  403. static int unattached_count;
  404. if (dev->hotplugged && !dc->hotpluggable) {
  405. error_setg(errp, "Device '%s' does not support hotplugging",
  406. object_get_typename(obj));
  407. return;
  408. }
  409. if (value && !dev->realized) {
  410. if (!check_only_migratable(obj, errp)) {
  411. goto fail;
  412. }
  413. if (!obj->parent) {
  414. gchar *name = g_strdup_printf("device[%d]", unattached_count++);
  415. object_property_add_child(container_get(qdev_get_machine(),
  416. "/unattached"),
  417. name, obj);
  418. unattached_parent = true;
  419. g_free(name);
  420. }
  421. hotplug_ctrl = qdev_get_hotplug_handler(dev);
  422. if (hotplug_ctrl) {
  423. hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
  424. if (local_err != NULL) {
  425. goto fail;
  426. }
  427. }
  428. if (dc->realize) {
  429. dc->realize(dev, &local_err);
  430. if (local_err != NULL) {
  431. goto fail;
  432. }
  433. }
  434. DEVICE_LISTENER_CALL(realize, Forward, dev);
  435. /*
  436. * always free/re-initialize here since the value cannot be cleaned up
  437. * in device_unrealize due to its usage later on in the unplug path
  438. */
  439. g_free(dev->canonical_path);
  440. dev->canonical_path = object_get_canonical_path(OBJECT(dev));
  441. QLIST_FOREACH(ncl, &dev->clocks, node) {
  442. if (ncl->alias) {
  443. continue;
  444. } else {
  445. clock_setup_canonical_path(ncl->clock);
  446. }
  447. }
  448. if (qdev_get_vmsd(dev)) {
  449. if (vmstate_register_with_alias_id(VMSTATE_IF(dev),
  450. VMSTATE_INSTANCE_ID_ANY,
  451. qdev_get_vmsd(dev), dev,
  452. dev->instance_id_alias,
  453. dev->alias_required_for_version,
  454. &local_err) < 0) {
  455. goto post_realize_fail;
  456. }
  457. }
  458. /*
  459. * Clear the reset state, in case the object was previously unrealized
  460. * with a dirty state.
  461. */
  462. resettable_state_clear(&dev->reset);
  463. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  464. if (!qbus_realize(bus, errp)) {
  465. goto child_realize_fail;
  466. }
  467. }
  468. if (dev->hotplugged) {
  469. /*
  470. * Reset the device, as well as its subtree which, at this point,
  471. * should be realized too.
  472. */
  473. resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD);
  474. resettable_change_parent(OBJECT(dev), OBJECT(dev->parent_bus),
  475. NULL);
  476. resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD);
  477. }
  478. dev->pending_deleted_event = false;
  479. if (hotplug_ctrl) {
  480. hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
  481. if (local_err != NULL) {
  482. goto child_realize_fail;
  483. }
  484. }
  485. qatomic_store_release(&dev->realized, value);
  486. } else if (!value && dev->realized) {
  487. /*
  488. * Change the value so that any concurrent users are aware
  489. * that the device is going to be unrealized
  490. *
  491. * TODO: change .realized property to enum that states
  492. * each phase of the device realization/unrealization
  493. */
  494. qatomic_set(&dev->realized, value);
  495. /*
  496. * Ensure that concurrent users see this update prior to
  497. * any other changes done by unrealize.
  498. */
  499. smp_wmb();
  500. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  501. qbus_unrealize(bus);
  502. }
  503. if (qdev_get_vmsd(dev)) {
  504. vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
  505. }
  506. if (dc->unrealize) {
  507. dc->unrealize(dev);
  508. }
  509. dev->pending_deleted_event = true;
  510. DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
  511. }
  512. assert(local_err == NULL);
  513. return;
  514. child_realize_fail:
  515. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  516. qbus_unrealize(bus);
  517. }
  518. if (qdev_get_vmsd(dev)) {
  519. vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
  520. }
  521. post_realize_fail:
  522. g_free(dev->canonical_path);
  523. dev->canonical_path = NULL;
  524. if (dc->unrealize) {
  525. dc->unrealize(dev);
  526. }
  527. fail:
  528. error_propagate(errp, local_err);
  529. if (unattached_parent) {
  530. /*
  531. * Beware, this doesn't just revert
  532. * object_property_add_child(), it also runs bus_remove()!
  533. */
  534. object_unparent(OBJECT(dev));
  535. unattached_count--;
  536. }
  537. }
  538. static bool device_get_hotpluggable(Object *obj, Error **errp)
  539. {
  540. DeviceClass *dc = DEVICE_GET_CLASS(obj);
  541. DeviceState *dev = DEVICE(obj);
  542. return dc->hotpluggable && (dev->parent_bus == NULL ||
  543. qbus_is_hotpluggable(dev->parent_bus));
  544. }
  545. static bool device_get_hotplugged(Object *obj, Error **errp)
  546. {
  547. DeviceState *dev = DEVICE(obj);
  548. return dev->hotplugged;
  549. }
  550. static void device_initfn(Object *obj)
  551. {
  552. DeviceState *dev = DEVICE(obj);
  553. if (phase_check(PHASE_MACHINE_READY)) {
  554. dev->hotplugged = 1;
  555. qdev_hot_added = true;
  556. }
  557. dev->instance_id_alias = -1;
  558. dev->realized = false;
  559. dev->allow_unplug_during_migration = false;
  560. QLIST_INIT(&dev->gpios);
  561. QLIST_INIT(&dev->clocks);
  562. }
  563. static void device_post_init(Object *obj)
  564. {
  565. /*
  566. * Note: ordered so that the user's global properties take
  567. * precedence.
  568. */
  569. object_apply_compat_props(obj);
  570. qdev_prop_set_globals(DEVICE(obj));
  571. }
  572. /* Unlink device from bus and free the structure. */
  573. static void device_finalize(Object *obj)
  574. {
  575. NamedGPIOList *ngl, *next;
  576. DeviceState *dev = DEVICE(obj);
  577. g_assert(!dev->unplug_blockers);
  578. QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
  579. QLIST_REMOVE(ngl, node);
  580. qemu_free_irqs(ngl->in, ngl->num_in);
  581. g_free(ngl->name);
  582. g_free(ngl);
  583. /* ngl->out irqs are owned by the other end and should not be freed
  584. * here
  585. */
  586. }
  587. qdev_finalize_clocklist(dev);
  588. /* Only send event if the device had been completely realized */
  589. if (dev->pending_deleted_event) {
  590. g_assert(dev->canonical_path);
  591. qapi_event_send_device_deleted(dev->id, dev->canonical_path);
  592. g_free(dev->canonical_path);
  593. dev->canonical_path = NULL;
  594. }
  595. qobject_unref(dev->opts);
  596. g_free(dev->id);
  597. }
  598. static void device_class_base_init(ObjectClass *class, void *data)
  599. {
  600. DeviceClass *klass = DEVICE_CLASS(class);
  601. /* We explicitly look up properties in the superclasses,
  602. * so do not propagate them to the subclasses.
  603. */
  604. klass->props_ = NULL;
  605. }
  606. static void device_unparent(Object *obj)
  607. {
  608. DeviceState *dev = DEVICE(obj);
  609. BusState *bus;
  610. if (dev->realized) {
  611. qdev_unrealize(dev);
  612. }
  613. while (dev->num_child_bus) {
  614. bus = QLIST_FIRST(&dev->child_bus);
  615. object_unparent(OBJECT(bus));
  616. }
  617. if (dev->parent_bus) {
  618. bus_remove_child(dev->parent_bus, dev);
  619. object_unref(OBJECT(dev->parent_bus));
  620. dev->parent_bus = NULL;
  621. }
  622. }
  623. static char *
  624. device_vmstate_if_get_id(VMStateIf *obj)
  625. {
  626. DeviceState *dev = DEVICE(obj);
  627. return qdev_get_dev_path(dev);
  628. }
  629. /**
  630. * device_phases_reset:
  631. * Transition reset method for devices to allow moving
  632. * smoothly from legacy reset method to multi-phases
  633. */
  634. static void device_phases_reset(DeviceState *dev)
  635. {
  636. ResettableClass *rc = RESETTABLE_GET_CLASS(dev);
  637. if (rc->phases.enter) {
  638. rc->phases.enter(OBJECT(dev), RESET_TYPE_COLD);
  639. }
  640. if (rc->phases.hold) {
  641. rc->phases.hold(OBJECT(dev), RESET_TYPE_COLD);
  642. }
  643. if (rc->phases.exit) {
  644. rc->phases.exit(OBJECT(dev), RESET_TYPE_COLD);
  645. }
  646. }
  647. static void device_transitional_reset(Object *obj)
  648. {
  649. DeviceClass *dc = DEVICE_GET_CLASS(obj);
  650. /*
  651. * This will call either @device_phases_reset (for multi-phases transitioned
  652. * devices) or a device's specific method for not-yet transitioned devices.
  653. * In both case, it does not reset children.
  654. */
  655. if (dc->reset) {
  656. dc->reset(DEVICE(obj));
  657. }
  658. }
  659. /**
  660. * device_get_transitional_reset:
  661. * check if the device's class is ready for multi-phase
  662. */
  663. static ResettableTrFunction device_get_transitional_reset(Object *obj)
  664. {
  665. DeviceClass *dc = DEVICE_GET_CLASS(obj);
  666. if (dc->reset != device_phases_reset) {
  667. /*
  668. * dc->reset has been overridden by a subclass,
  669. * the device is not ready for multi phase yet.
  670. */
  671. return device_transitional_reset;
  672. }
  673. return NULL;
  674. }
  675. static void device_class_init(ObjectClass *class, void *data)
  676. {
  677. DeviceClass *dc = DEVICE_CLASS(class);
  678. VMStateIfClass *vc = VMSTATE_IF_CLASS(class);
  679. ResettableClass *rc = RESETTABLE_CLASS(class);
  680. class->unparent = device_unparent;
  681. /* by default all devices were considered as hotpluggable,
  682. * so with intent to check it in generic qdev_unplug() /
  683. * device_set_realized() functions make every device
  684. * hotpluggable. Devices that shouldn't be hotpluggable,
  685. * should override it in their class_init()
  686. */
  687. dc->hotpluggable = true;
  688. dc->user_creatable = true;
  689. vc->get_id = device_vmstate_if_get_id;
  690. rc->get_state = device_get_reset_state;
  691. rc->child_foreach = device_reset_child_foreach;
  692. /*
  693. * @device_phases_reset is put as the default reset method below, allowing
  694. * to do the multi-phase transition from base classes to leaf classes. It
  695. * allows a legacy-reset Device class to extend a multi-phases-reset
  696. * Device class for the following reason:
  697. * + If a base class B has been moved to multi-phase, then it does not
  698. * override this default reset method and may have defined phase methods.
  699. * + A child class C (extending class B) which uses
  700. * device_class_set_parent_reset() (or similar means) to override the
  701. * reset method will still work as expected. @device_phases_reset function
  702. * will be registered as the parent reset method and effectively call
  703. * parent reset phases.
  704. */
  705. device_class_set_legacy_reset(dc, device_phases_reset);
  706. rc->get_transitional_function = device_get_transitional_reset;
  707. object_class_property_add_bool(class, "realized",
  708. device_get_realized, device_set_realized);
  709. object_class_property_add_bool(class, "hotpluggable",
  710. device_get_hotpluggable, NULL);
  711. object_class_property_add_bool(class, "hotplugged",
  712. device_get_hotplugged, NULL);
  713. object_class_property_add_link(class, "parent_bus", TYPE_BUS,
  714. offsetof(DeviceState, parent_bus), NULL, 0);
  715. }
  716. void device_class_set_legacy_reset(DeviceClass *dc, DeviceReset dev_reset)
  717. {
  718. dc->reset = dev_reset;
  719. }
  720. void device_class_set_parent_realize(DeviceClass *dc,
  721. DeviceRealize dev_realize,
  722. DeviceRealize *parent_realize)
  723. {
  724. *parent_realize = dc->realize;
  725. dc->realize = dev_realize;
  726. }
  727. void device_class_set_parent_unrealize(DeviceClass *dc,
  728. DeviceUnrealize dev_unrealize,
  729. DeviceUnrealize *parent_unrealize)
  730. {
  731. *parent_unrealize = dc->unrealize;
  732. dc->unrealize = dev_unrealize;
  733. }
  734. Object *qdev_get_machine(void)
  735. {
  736. static Object *dev;
  737. if (dev == NULL) {
  738. dev = container_get(object_get_root(), "/machine");
  739. }
  740. return dev;
  741. }
  742. char *qdev_get_human_name(DeviceState *dev)
  743. {
  744. g_assert(dev != NULL);
  745. return dev->id ?
  746. g_strdup(dev->id) : object_get_canonical_path(OBJECT(dev));
  747. }
  748. static MachineInitPhase machine_phase;
  749. bool phase_check(MachineInitPhase phase)
  750. {
  751. return machine_phase >= phase;
  752. }
  753. void phase_advance(MachineInitPhase phase)
  754. {
  755. assert(machine_phase == phase - 1);
  756. machine_phase = phase;
  757. }
  758. static const TypeInfo device_type_info = {
  759. .name = TYPE_DEVICE,
  760. .parent = TYPE_OBJECT,
  761. .instance_size = sizeof(DeviceState),
  762. .instance_init = device_initfn,
  763. .instance_post_init = device_post_init,
  764. .instance_finalize = device_finalize,
  765. .class_base_init = device_class_base_init,
  766. .class_init = device_class_init,
  767. .abstract = true,
  768. .class_size = sizeof(DeviceClass),
  769. .interfaces = (InterfaceInfo[]) {
  770. { TYPE_VMSTATE_IF },
  771. { TYPE_RESETTABLE_INTERFACE },
  772. { }
  773. }
  774. };
  775. static void qdev_register_types(void)
  776. {
  777. type_register_static(&device_type_info);
  778. }
  779. type_init(qdev_register_types)