qdev.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. /* 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/qerror.h"
  30. #include "qapi/visitor.h"
  31. #include "qemu/error-report.h"
  32. #include "qemu/option.h"
  33. #include "hw/hotplug.h"
  34. #include "hw/irq.h"
  35. #include "hw/qdev-properties.h"
  36. #include "hw/boards.h"
  37. #include "hw/sysbus.h"
  38. #include "migration/vmstate.h"
  39. bool qdev_hotplug = false;
  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_remove_child(BusState *bus, DeviceState *child)
  48. {
  49. BusChild *kid;
  50. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  51. if (kid->child == child) {
  52. char name[32];
  53. snprintf(name, sizeof(name), "child[%d]", kid->index);
  54. QTAILQ_REMOVE(&bus->children, kid, sibling);
  55. bus->num_children--;
  56. /* This gives back ownership of kid->child back to us. */
  57. object_property_del(OBJECT(bus), name, NULL);
  58. object_unref(OBJECT(kid->child));
  59. g_free(kid);
  60. return;
  61. }
  62. }
  63. }
  64. static void bus_add_child(BusState *bus, DeviceState *child)
  65. {
  66. char name[32];
  67. BusChild *kid = g_malloc0(sizeof(*kid));
  68. bus->num_children++;
  69. kid->index = bus->max_index++;
  70. kid->child = child;
  71. object_ref(OBJECT(kid->child));
  72. QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
  73. /* This transfers ownership of kid->child to the property. */
  74. snprintf(name, sizeof(name), "child[%d]", kid->index);
  75. object_property_add_link(OBJECT(bus), name,
  76. object_get_typename(OBJECT(child)),
  77. (Object **)&kid->child,
  78. NULL, /* read-only property */
  79. 0, /* return ownership on prop deletion */
  80. NULL);
  81. }
  82. void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
  83. {
  84. bool replugging = dev->parent_bus != NULL;
  85. if (replugging) {
  86. /* Keep a reference to the device while it's not plugged into
  87. * any bus, to avoid it potentially evaporating when it is
  88. * dereffed in bus_remove_child().
  89. */
  90. object_ref(OBJECT(dev));
  91. bus_remove_child(dev->parent_bus, dev);
  92. object_unref(OBJECT(dev->parent_bus));
  93. }
  94. dev->parent_bus = bus;
  95. object_ref(OBJECT(bus));
  96. bus_add_child(bus, dev);
  97. if (replugging) {
  98. object_unref(OBJECT(dev));
  99. }
  100. }
  101. /* Create a new device. This only initializes the device state
  102. structure and allows properties to be set. The device still needs
  103. to be realized. See qdev-core.h. */
  104. DeviceState *qdev_create(BusState *bus, const char *name)
  105. {
  106. DeviceState *dev;
  107. dev = qdev_try_create(bus, name);
  108. if (!dev) {
  109. if (bus) {
  110. error_report("Unknown device '%s' for bus '%s'", name,
  111. object_get_typename(OBJECT(bus)));
  112. } else {
  113. error_report("Unknown device '%s' for default sysbus", name);
  114. }
  115. abort();
  116. }
  117. return dev;
  118. }
  119. DeviceState *qdev_try_create(BusState *bus, const char *type)
  120. {
  121. DeviceState *dev;
  122. if (object_class_by_name(type) == NULL) {
  123. return NULL;
  124. }
  125. dev = DEVICE(object_new(type));
  126. if (!dev) {
  127. return NULL;
  128. }
  129. if (!bus) {
  130. /* Assert that the device really is a SysBusDevice before
  131. * we put it onto the sysbus. Non-sysbus devices which aren't
  132. * being put onto a bus should be created with object_new(TYPE_FOO),
  133. * not qdev_create(NULL, TYPE_FOO).
  134. */
  135. g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
  136. bus = sysbus_get_default();
  137. }
  138. qdev_set_parent_bus(dev, bus);
  139. object_unref(OBJECT(dev));
  140. return dev;
  141. }
  142. static QTAILQ_HEAD(, DeviceListener) device_listeners
  143. = QTAILQ_HEAD_INITIALIZER(device_listeners);
  144. enum ListenerDirection { Forward, Reverse };
  145. #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
  146. do { \
  147. DeviceListener *_listener; \
  148. \
  149. switch (_direction) { \
  150. case Forward: \
  151. QTAILQ_FOREACH(_listener, &device_listeners, link) { \
  152. if (_listener->_callback) { \
  153. _listener->_callback(_listener, ##_args); \
  154. } \
  155. } \
  156. break; \
  157. case Reverse: \
  158. QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
  159. link) { \
  160. if (_listener->_callback) { \
  161. _listener->_callback(_listener, ##_args); \
  162. } \
  163. } \
  164. break; \
  165. default: \
  166. abort(); \
  167. } \
  168. } while (0)
  169. static int device_listener_add(DeviceState *dev, void *opaque)
  170. {
  171. DEVICE_LISTENER_CALL(realize, Forward, dev);
  172. return 0;
  173. }
  174. void device_listener_register(DeviceListener *listener)
  175. {
  176. QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
  177. qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
  178. NULL, NULL);
  179. }
  180. void device_listener_unregister(DeviceListener *listener)
  181. {
  182. QTAILQ_REMOVE(&device_listeners, listener, link);
  183. }
  184. bool qdev_should_hide_device(QemuOpts *opts)
  185. {
  186. int rc = -1;
  187. DeviceListener *listener;
  188. QTAILQ_FOREACH(listener, &device_listeners, link) {
  189. if (listener->should_be_hidden) {
  190. /*
  191. * should_be_hidden_will return
  192. * 1 if device matches opts and it should be hidden
  193. * 0 if device matches opts and should not be hidden
  194. * -1 if device doesn't match ops
  195. */
  196. rc = listener->should_be_hidden(listener, opts);
  197. }
  198. if (rc > 0) {
  199. break;
  200. }
  201. }
  202. return rc > 0;
  203. }
  204. void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
  205. int required_for_version)
  206. {
  207. assert(!dev->realized);
  208. dev->instance_id_alias = alias_id;
  209. dev->alias_required_for_version = required_for_version;
  210. }
  211. HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
  212. {
  213. MachineState *machine;
  214. MachineClass *mc;
  215. Object *m_obj = qdev_get_machine();
  216. if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
  217. machine = MACHINE(m_obj);
  218. mc = MACHINE_GET_CLASS(machine);
  219. if (mc->get_hotplug_handler) {
  220. return mc->get_hotplug_handler(machine, dev);
  221. }
  222. }
  223. return NULL;
  224. }
  225. bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
  226. {
  227. MachineState *machine;
  228. MachineClass *mc;
  229. Object *m_obj = qdev_get_machine();
  230. if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
  231. machine = MACHINE(m_obj);
  232. mc = MACHINE_GET_CLASS(machine);
  233. if (mc->hotplug_allowed) {
  234. return mc->hotplug_allowed(machine, dev, errp);
  235. }
  236. }
  237. return true;
  238. }
  239. HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
  240. {
  241. if (dev->parent_bus) {
  242. return dev->parent_bus->hotplug_handler;
  243. }
  244. return NULL;
  245. }
  246. HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
  247. {
  248. HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
  249. if (hotplug_ctrl == NULL && dev->parent_bus) {
  250. hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
  251. }
  252. return hotplug_ctrl;
  253. }
  254. static int qdev_reset_one(DeviceState *dev, void *opaque)
  255. {
  256. device_reset(dev);
  257. return 0;
  258. }
  259. static int qbus_reset_one(BusState *bus, void *opaque)
  260. {
  261. BusClass *bc = BUS_GET_CLASS(bus);
  262. if (bc->reset) {
  263. bc->reset(bus);
  264. }
  265. return 0;
  266. }
  267. void qdev_reset_all(DeviceState *dev)
  268. {
  269. qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
  270. }
  271. void qdev_reset_all_fn(void *opaque)
  272. {
  273. qdev_reset_all(DEVICE(opaque));
  274. }
  275. void qbus_reset_all(BusState *bus)
  276. {
  277. qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
  278. }
  279. void qbus_reset_all_fn(void *opaque)
  280. {
  281. BusState *bus = opaque;
  282. qbus_reset_all(bus);
  283. }
  284. /* can be used as ->unplug() callback for the simple cases */
  285. void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
  286. DeviceState *dev, Error **errp)
  287. {
  288. object_property_set_bool(OBJECT(dev), false, "realized", NULL);
  289. }
  290. /*
  291. * Realize @dev.
  292. * Device properties should be set before calling this function. IRQs
  293. * and MMIO regions should be connected/mapped after calling this
  294. * function.
  295. * On failure, report an error with error_report() and terminate the
  296. * program. This is okay during machine creation. Don't use for
  297. * hotplug, because there callers need to recover from failure.
  298. * Exception: if you know the device's init() callback can't fail,
  299. * then qdev_init_nofail() can't fail either, and is therefore usable
  300. * even then. But relying on the device implementation that way is
  301. * somewhat unclean, and best avoided.
  302. */
  303. void qdev_init_nofail(DeviceState *dev)
  304. {
  305. Error *err = NULL;
  306. assert(!dev->realized);
  307. object_ref(OBJECT(dev));
  308. object_property_set_bool(OBJECT(dev), true, "realized", &err);
  309. if (err) {
  310. error_reportf_err(err, "Initialization of device %s failed: ",
  311. object_get_typename(OBJECT(dev)));
  312. exit(1);
  313. }
  314. object_unref(OBJECT(dev));
  315. }
  316. void qdev_machine_creation_done(void)
  317. {
  318. /*
  319. * ok, initial machine setup is done, starting from now we can
  320. * only create hotpluggable devices
  321. */
  322. qdev_hotplug = true;
  323. }
  324. bool qdev_machine_modified(void)
  325. {
  326. return qdev_hot_added || qdev_hot_removed;
  327. }
  328. BusState *qdev_get_parent_bus(DeviceState *dev)
  329. {
  330. return dev->parent_bus;
  331. }
  332. static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
  333. const char *name)
  334. {
  335. NamedGPIOList *ngl;
  336. QLIST_FOREACH(ngl, &dev->gpios, node) {
  337. /* NULL is a valid and matchable name, otherwise do a normal
  338. * strcmp match.
  339. */
  340. if ((!ngl->name && !name) ||
  341. (name && ngl->name && strcmp(name, ngl->name) == 0)) {
  342. return ngl;
  343. }
  344. }
  345. ngl = g_malloc0(sizeof(*ngl));
  346. ngl->name = g_strdup(name);
  347. QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
  348. return ngl;
  349. }
  350. void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
  351. qemu_irq_handler handler,
  352. void *opaque,
  353. const char *name, int n)
  354. {
  355. int i;
  356. NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
  357. assert(gpio_list->num_out == 0 || !name);
  358. gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
  359. opaque, n);
  360. if (!name) {
  361. name = "unnamed-gpio-in";
  362. }
  363. for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
  364. gchar *propname = g_strdup_printf("%s[%u]", name, i);
  365. object_property_add_child(OBJECT(dev), propname,
  366. OBJECT(gpio_list->in[i]), &error_abort);
  367. g_free(propname);
  368. }
  369. gpio_list->num_in += n;
  370. }
  371. void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
  372. {
  373. qdev_init_gpio_in_named(dev, handler, NULL, n);
  374. }
  375. void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
  376. const char *name, int n)
  377. {
  378. int i;
  379. NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
  380. assert(gpio_list->num_in == 0 || !name);
  381. if (!name) {
  382. name = "unnamed-gpio-out";
  383. }
  384. memset(pins, 0, sizeof(*pins) * n);
  385. for (i = 0; i < n; ++i) {
  386. gchar *propname = g_strdup_printf("%s[%u]", name,
  387. gpio_list->num_out + i);
  388. object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
  389. (Object **)&pins[i],
  390. object_property_allow_set_link,
  391. OBJ_PROP_LINK_STRONG,
  392. &error_abort);
  393. g_free(propname);
  394. }
  395. gpio_list->num_out += n;
  396. }
  397. void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
  398. {
  399. qdev_init_gpio_out_named(dev, pins, NULL, n);
  400. }
  401. qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
  402. {
  403. NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
  404. assert(n >= 0 && n < gpio_list->num_in);
  405. return gpio_list->in[n];
  406. }
  407. qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
  408. {
  409. return qdev_get_gpio_in_named(dev, NULL, n);
  410. }
  411. void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
  412. qemu_irq pin)
  413. {
  414. char *propname = g_strdup_printf("%s[%d]",
  415. name ? name : "unnamed-gpio-out", n);
  416. if (pin) {
  417. /* We need a name for object_property_set_link to work. If the
  418. * object has a parent, object_property_add_child will come back
  419. * with an error without doing anything. If it has none, it will
  420. * never fail. So we can just call it with a NULL Error pointer.
  421. */
  422. object_property_add_child(container_get(qdev_get_machine(),
  423. "/unattached"),
  424. "non-qdev-gpio[*]", OBJECT(pin), NULL);
  425. }
  426. object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
  427. g_free(propname);
  428. }
  429. qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
  430. {
  431. char *propname = g_strdup_printf("%s[%d]",
  432. name ? name : "unnamed-gpio-out", n);
  433. qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
  434. NULL);
  435. return ret;
  436. }
  437. /* disconnect a GPIO output, returning the disconnected input (if any) */
  438. static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
  439. const char *name, int n)
  440. {
  441. char *propname = g_strdup_printf("%s[%d]",
  442. name ? name : "unnamed-gpio-out", n);
  443. qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
  444. NULL);
  445. if (ret) {
  446. object_property_set_link(OBJECT(dev), NULL, propname, NULL);
  447. }
  448. g_free(propname);
  449. return ret;
  450. }
  451. qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
  452. const char *name, int n)
  453. {
  454. qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
  455. qdev_connect_gpio_out_named(dev, name, n, icpt);
  456. return disconnected;
  457. }
  458. void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
  459. {
  460. qdev_connect_gpio_out_named(dev, NULL, n, pin);
  461. }
  462. void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
  463. const char *name)
  464. {
  465. int i;
  466. NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
  467. for (i = 0; i < ngl->num_in; i++) {
  468. const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
  469. char *propname = g_strdup_printf("%s[%d]", nm, i);
  470. object_property_add_alias(OBJECT(container), propname,
  471. OBJECT(dev), propname,
  472. &error_abort);
  473. g_free(propname);
  474. }
  475. for (i = 0; i < ngl->num_out; i++) {
  476. const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
  477. char *propname = g_strdup_printf("%s[%d]", nm, i);
  478. object_property_add_alias(OBJECT(container), propname,
  479. OBJECT(dev), propname,
  480. &error_abort);
  481. g_free(propname);
  482. }
  483. QLIST_REMOVE(ngl, node);
  484. QLIST_INSERT_HEAD(&container->gpios, ngl, node);
  485. }
  486. BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
  487. {
  488. BusState *bus;
  489. Object *child = object_resolve_path_component(OBJECT(dev), name);
  490. bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
  491. if (bus) {
  492. return bus;
  493. }
  494. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  495. if (strcmp(name, bus->name) == 0) {
  496. return bus;
  497. }
  498. }
  499. return NULL;
  500. }
  501. int qdev_walk_children(DeviceState *dev,
  502. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  503. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  504. void *opaque)
  505. {
  506. BusState *bus;
  507. int err;
  508. if (pre_devfn) {
  509. err = pre_devfn(dev, opaque);
  510. if (err) {
  511. return err;
  512. }
  513. }
  514. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  515. err = qbus_walk_children(bus, pre_devfn, pre_busfn,
  516. post_devfn, post_busfn, opaque);
  517. if (err < 0) {
  518. return err;
  519. }
  520. }
  521. if (post_devfn) {
  522. err = post_devfn(dev, opaque);
  523. if (err) {
  524. return err;
  525. }
  526. }
  527. return 0;
  528. }
  529. DeviceState *qdev_find_recursive(BusState *bus, const char *id)
  530. {
  531. BusChild *kid;
  532. DeviceState *ret;
  533. BusState *child;
  534. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  535. DeviceState *dev = kid->child;
  536. if (dev->id && strcmp(dev->id, id) == 0) {
  537. return dev;
  538. }
  539. QLIST_FOREACH(child, &dev->child_bus, sibling) {
  540. ret = qdev_find_recursive(child, id);
  541. if (ret) {
  542. return ret;
  543. }
  544. }
  545. }
  546. return NULL;
  547. }
  548. char *qdev_get_dev_path(DeviceState *dev)
  549. {
  550. BusClass *bc;
  551. if (!dev || !dev->parent_bus) {
  552. return NULL;
  553. }
  554. bc = BUS_GET_CLASS(dev->parent_bus);
  555. if (bc->get_dev_path) {
  556. return bc->get_dev_path(dev);
  557. }
  558. return NULL;
  559. }
  560. /**
  561. * Legacy property handling
  562. */
  563. static void qdev_get_legacy_property(Object *obj, Visitor *v,
  564. const char *name, void *opaque,
  565. Error **errp)
  566. {
  567. DeviceState *dev = DEVICE(obj);
  568. Property *prop = opaque;
  569. char buffer[1024];
  570. char *ptr = buffer;
  571. prop->info->print(dev, prop, buffer, sizeof(buffer));
  572. visit_type_str(v, name, &ptr, errp);
  573. }
  574. /**
  575. * qdev_property_add_legacy:
  576. * @dev: Device to add the property to.
  577. * @prop: The qdev property definition.
  578. * @errp: location to store error information.
  579. *
  580. * Add a legacy QOM property to @dev for qdev property @prop.
  581. * On error, store error in @errp.
  582. *
  583. * Legacy properties are string versions of QOM properties. The format of
  584. * the string depends on the property type. Legacy properties are only
  585. * needed for "info qtree".
  586. *
  587. * Do not use this in new code! QOM Properties added through this interface
  588. * will be given names in the "legacy" namespace.
  589. */
  590. static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
  591. Error **errp)
  592. {
  593. gchar *name;
  594. /* Register pointer properties as legacy properties */
  595. if (!prop->info->print && prop->info->get) {
  596. return;
  597. }
  598. if (prop->info->create) {
  599. return;
  600. }
  601. name = g_strdup_printf("legacy-%s", prop->name);
  602. object_property_add(OBJECT(dev), name, "str",
  603. prop->info->print ? qdev_get_legacy_property : prop->info->get,
  604. NULL,
  605. NULL,
  606. prop, errp);
  607. g_free(name);
  608. }
  609. /**
  610. * qdev_property_add_static:
  611. * @dev: Device to add the property to.
  612. * @prop: The qdev property definition.
  613. * @errp: location to store error information.
  614. *
  615. * Add a static QOM property to @dev for qdev property @prop.
  616. * On error, store error in @errp. Static properties access data in a struct.
  617. * The type of the QOM property is derived from prop->info.
  618. */
  619. void qdev_property_add_static(DeviceState *dev, Property *prop,
  620. Error **errp)
  621. {
  622. Error *local_err = NULL;
  623. Object *obj = OBJECT(dev);
  624. if (prop->info->create) {
  625. prop->info->create(obj, prop, &local_err);
  626. } else {
  627. /*
  628. * TODO qdev_prop_ptr does not have getters or setters. It must
  629. * go now that it can be replaced with links. The test should be
  630. * removed along with it: all static properties are read/write.
  631. */
  632. if (!prop->info->get && !prop->info->set) {
  633. return;
  634. }
  635. object_property_add(obj, prop->name, prop->info->name,
  636. prop->info->get, prop->info->set,
  637. prop->info->release,
  638. prop, &local_err);
  639. }
  640. if (local_err) {
  641. error_propagate(errp, local_err);
  642. return;
  643. }
  644. object_property_set_description(obj, prop->name,
  645. prop->info->description,
  646. &error_abort);
  647. if (prop->set_default) {
  648. prop->info->set_default_value(obj, prop);
  649. }
  650. }
  651. /* @qdev_alias_all_properties - Add alias properties to the source object for
  652. * all qdev properties on the target DeviceState.
  653. */
  654. void qdev_alias_all_properties(DeviceState *target, Object *source)
  655. {
  656. ObjectClass *class;
  657. Property *prop;
  658. class = object_get_class(OBJECT(target));
  659. do {
  660. DeviceClass *dc = DEVICE_CLASS(class);
  661. for (prop = dc->props; prop && prop->name; prop++) {
  662. object_property_add_alias(source, prop->name,
  663. OBJECT(target), prop->name,
  664. &error_abort);
  665. }
  666. class = object_class_get_parent(class);
  667. } while (class != object_class_by_name(TYPE_DEVICE));
  668. }
  669. static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
  670. {
  671. GSList **list = opaque;
  672. DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
  673. TYPE_DEVICE);
  674. if (dev == NULL) {
  675. return 0;
  676. }
  677. if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
  678. *list = g_slist_append(*list, dev);
  679. }
  680. return 0;
  681. }
  682. GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
  683. {
  684. GSList *list = NULL;
  685. object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
  686. return list;
  687. }
  688. static bool device_get_realized(Object *obj, Error **errp)
  689. {
  690. DeviceState *dev = DEVICE(obj);
  691. return dev->realized;
  692. }
  693. static bool check_only_migratable(Object *obj, Error **errp)
  694. {
  695. DeviceClass *dc = DEVICE_GET_CLASS(obj);
  696. if (!vmstate_check_only_migratable(dc->vmsd)) {
  697. error_setg(errp, "Device %s is not migratable, but "
  698. "--only-migratable was specified",
  699. object_get_typename(obj));
  700. return false;
  701. }
  702. return true;
  703. }
  704. static void device_set_realized(Object *obj, bool value, Error **errp)
  705. {
  706. DeviceState *dev = DEVICE(obj);
  707. DeviceClass *dc = DEVICE_GET_CLASS(dev);
  708. HotplugHandler *hotplug_ctrl;
  709. BusState *bus;
  710. Error *local_err = NULL;
  711. bool unattached_parent = false;
  712. static int unattached_count;
  713. if (dev->hotplugged && !dc->hotpluggable) {
  714. error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
  715. return;
  716. }
  717. if (value && !dev->realized) {
  718. if (!check_only_migratable(obj, &local_err)) {
  719. goto fail;
  720. }
  721. if (!obj->parent) {
  722. gchar *name = g_strdup_printf("device[%d]", unattached_count++);
  723. object_property_add_child(container_get(qdev_get_machine(),
  724. "/unattached"),
  725. name, obj, &error_abort);
  726. unattached_parent = true;
  727. g_free(name);
  728. }
  729. hotplug_ctrl = qdev_get_hotplug_handler(dev);
  730. if (hotplug_ctrl) {
  731. hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
  732. if (local_err != NULL) {
  733. goto fail;
  734. }
  735. }
  736. if (dc->realize) {
  737. dc->realize(dev, &local_err);
  738. if (local_err != NULL) {
  739. goto fail;
  740. }
  741. }
  742. DEVICE_LISTENER_CALL(realize, Forward, dev);
  743. /*
  744. * always free/re-initialize here since the value cannot be cleaned up
  745. * in device_unrealize due to its usage later on in the unplug path
  746. */
  747. g_free(dev->canonical_path);
  748. dev->canonical_path = object_get_canonical_path(OBJECT(dev));
  749. if (qdev_get_vmsd(dev)) {
  750. if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
  751. dev->instance_id_alias,
  752. dev->alias_required_for_version,
  753. &local_err) < 0) {
  754. goto post_realize_fail;
  755. }
  756. }
  757. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  758. object_property_set_bool(OBJECT(bus), true, "realized",
  759. &local_err);
  760. if (local_err != NULL) {
  761. goto child_realize_fail;
  762. }
  763. }
  764. if (dev->hotplugged) {
  765. device_reset(dev);
  766. }
  767. dev->pending_deleted_event = false;
  768. if (hotplug_ctrl) {
  769. hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
  770. if (local_err != NULL) {
  771. goto child_realize_fail;
  772. }
  773. }
  774. } else if (!value && dev->realized) {
  775. /* We want local_err to track only the first error */
  776. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  777. object_property_set_bool(OBJECT(bus), false, "realized",
  778. local_err ? NULL : &local_err);
  779. }
  780. if (qdev_get_vmsd(dev)) {
  781. vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
  782. }
  783. if (dc->unrealize) {
  784. dc->unrealize(dev, local_err ? NULL : &local_err);
  785. }
  786. dev->pending_deleted_event = true;
  787. DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
  788. if (local_err != NULL) {
  789. goto fail;
  790. }
  791. }
  792. assert(local_err == NULL);
  793. dev->realized = value;
  794. return;
  795. child_realize_fail:
  796. QLIST_FOREACH(bus, &dev->child_bus, sibling) {
  797. object_property_set_bool(OBJECT(bus), false, "realized",
  798. NULL);
  799. }
  800. if (qdev_get_vmsd(dev)) {
  801. vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
  802. }
  803. post_realize_fail:
  804. g_free(dev->canonical_path);
  805. dev->canonical_path = NULL;
  806. if (dc->unrealize) {
  807. dc->unrealize(dev, NULL);
  808. }
  809. fail:
  810. error_propagate(errp, local_err);
  811. if (unattached_parent) {
  812. object_unparent(OBJECT(dev));
  813. unattached_count--;
  814. }
  815. }
  816. static bool device_get_hotpluggable(Object *obj, Error **errp)
  817. {
  818. DeviceClass *dc = DEVICE_GET_CLASS(obj);
  819. DeviceState *dev = DEVICE(obj);
  820. return dc->hotpluggable && (dev->parent_bus == NULL ||
  821. qbus_is_hotpluggable(dev->parent_bus));
  822. }
  823. static bool device_get_hotplugged(Object *obj, Error **errp)
  824. {
  825. DeviceState *dev = DEVICE(obj);
  826. return dev->hotplugged;
  827. }
  828. static void device_initfn(Object *obj)
  829. {
  830. DeviceState *dev = DEVICE(obj);
  831. ObjectClass *class;
  832. Property *prop;
  833. if (qdev_hotplug) {
  834. dev->hotplugged = 1;
  835. qdev_hot_added = true;
  836. }
  837. dev->instance_id_alias = -1;
  838. dev->realized = false;
  839. dev->allow_unplug_during_migration = false;
  840. object_property_add_bool(obj, "realized",
  841. device_get_realized, device_set_realized, NULL);
  842. object_property_add_bool(obj, "hotpluggable",
  843. device_get_hotpluggable, NULL, NULL);
  844. object_property_add_bool(obj, "hotplugged",
  845. device_get_hotplugged, NULL,
  846. &error_abort);
  847. class = object_get_class(OBJECT(dev));
  848. do {
  849. for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
  850. qdev_property_add_legacy(dev, prop, &error_abort);
  851. qdev_property_add_static(dev, prop, &error_abort);
  852. }
  853. class = object_class_get_parent(class);
  854. } while (class != object_class_by_name(TYPE_DEVICE));
  855. object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
  856. (Object **)&dev->parent_bus, NULL, 0,
  857. &error_abort);
  858. QLIST_INIT(&dev->gpios);
  859. }
  860. static void device_post_init(Object *obj)
  861. {
  862. /*
  863. * Note: ordered so that the user's global properties take
  864. * precedence.
  865. */
  866. object_apply_compat_props(obj);
  867. qdev_prop_set_globals(DEVICE(obj));
  868. }
  869. /* Unlink device from bus and free the structure. */
  870. static void device_finalize(Object *obj)
  871. {
  872. NamedGPIOList *ngl, *next;
  873. DeviceState *dev = DEVICE(obj);
  874. QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
  875. QLIST_REMOVE(ngl, node);
  876. qemu_free_irqs(ngl->in, ngl->num_in);
  877. g_free(ngl->name);
  878. g_free(ngl);
  879. /* ngl->out irqs are owned by the other end and should not be freed
  880. * here
  881. */
  882. }
  883. /* Only send event if the device had been completely realized */
  884. if (dev->pending_deleted_event) {
  885. g_assert(dev->canonical_path);
  886. qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
  887. g_free(dev->canonical_path);
  888. dev->canonical_path = NULL;
  889. }
  890. qemu_opts_del(dev->opts);
  891. }
  892. static void device_class_base_init(ObjectClass *class, void *data)
  893. {
  894. DeviceClass *klass = DEVICE_CLASS(class);
  895. /* We explicitly look up properties in the superclasses,
  896. * so do not propagate them to the subclasses.
  897. */
  898. klass->props = NULL;
  899. }
  900. static void device_unparent(Object *obj)
  901. {
  902. DeviceState *dev = DEVICE(obj);
  903. BusState *bus;
  904. if (dev->realized) {
  905. object_property_set_bool(obj, false, "realized", NULL);
  906. }
  907. while (dev->num_child_bus) {
  908. bus = QLIST_FIRST(&dev->child_bus);
  909. object_unparent(OBJECT(bus));
  910. }
  911. if (dev->parent_bus) {
  912. bus_remove_child(dev->parent_bus, dev);
  913. object_unref(OBJECT(dev->parent_bus));
  914. dev->parent_bus = NULL;
  915. }
  916. }
  917. static char *
  918. device_vmstate_if_get_id(VMStateIf *obj)
  919. {
  920. DeviceState *dev = DEVICE(obj);
  921. return qdev_get_dev_path(dev);
  922. }
  923. static void device_class_init(ObjectClass *class, void *data)
  924. {
  925. DeviceClass *dc = DEVICE_CLASS(class);
  926. VMStateIfClass *vc = VMSTATE_IF_CLASS(class);
  927. class->unparent = device_unparent;
  928. /* by default all devices were considered as hotpluggable,
  929. * so with intent to check it in generic qdev_unplug() /
  930. * device_set_realized() functions make every device
  931. * hotpluggable. Devices that shouldn't be hotpluggable,
  932. * should override it in their class_init()
  933. */
  934. dc->hotpluggable = true;
  935. dc->user_creatable = true;
  936. vc->get_id = device_vmstate_if_get_id;
  937. }
  938. void device_class_set_parent_reset(DeviceClass *dc,
  939. DeviceReset dev_reset,
  940. DeviceReset *parent_reset)
  941. {
  942. *parent_reset = dc->reset;
  943. dc->reset = dev_reset;
  944. }
  945. void device_class_set_parent_realize(DeviceClass *dc,
  946. DeviceRealize dev_realize,
  947. DeviceRealize *parent_realize)
  948. {
  949. *parent_realize = dc->realize;
  950. dc->realize = dev_realize;
  951. }
  952. void device_class_set_parent_unrealize(DeviceClass *dc,
  953. DeviceUnrealize dev_unrealize,
  954. DeviceUnrealize *parent_unrealize)
  955. {
  956. *parent_unrealize = dc->unrealize;
  957. dc->unrealize = dev_unrealize;
  958. }
  959. void device_reset(DeviceState *dev)
  960. {
  961. DeviceClass *klass = DEVICE_GET_CLASS(dev);
  962. if (klass->reset) {
  963. klass->reset(dev);
  964. }
  965. }
  966. Object *qdev_get_machine(void)
  967. {
  968. static Object *dev;
  969. if (dev == NULL) {
  970. dev = container_get(object_get_root(), "/machine");
  971. }
  972. return dev;
  973. }
  974. static const TypeInfo device_type_info = {
  975. .name = TYPE_DEVICE,
  976. .parent = TYPE_OBJECT,
  977. .instance_size = sizeof(DeviceState),
  978. .instance_init = device_initfn,
  979. .instance_post_init = device_post_init,
  980. .instance_finalize = device_finalize,
  981. .class_base_init = device_class_base_init,
  982. .class_init = device_class_init,
  983. .abstract = true,
  984. .class_size = sizeof(DeviceClass),
  985. .interfaces = (InterfaceInfo[]) {
  986. { TYPE_VMSTATE_IF },
  987. { }
  988. }
  989. };
  990. static void qdev_register_types(void)
  991. {
  992. type_register_static(&device_type_info);
  993. }
  994. type_init(qdev_register_types)