2
0

qdev.c 34 KB

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