PageRenderTime 33ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/qemu-xen/hw/qdev.h

https://gitlab.com/wkyu/gxen
C Header | 332 lines | 256 code | 51 blank | 25 comment | 0 complexity | f231e13fa8d41610f2a557d6bf65e18f MD5 | raw file
  1. #ifndef QDEV_H
  2. #define QDEV_H
  3. #include "hw.h"
  4. #include "qemu-queue.h"
  5. #include "qemu-char.h"
  6. #include "qemu-option.h"
  7. typedef struct Property Property;
  8. typedef struct PropertyInfo PropertyInfo;
  9. typedef struct CompatProperty CompatProperty;
  10. typedef struct DeviceInfo DeviceInfo;
  11. typedef struct BusState BusState;
  12. typedef struct BusInfo BusInfo;
  13. enum DevState {
  14. DEV_STATE_CREATED = 1,
  15. DEV_STATE_INITIALIZED,
  16. };
  17. enum {
  18. DEV_NVECTORS_UNSPECIFIED = -1,
  19. };
  20. /* This structure should not be accessed directly. We declare it here
  21. so that it can be embedded in individual device state structures. */
  22. struct DeviceState {
  23. const char *id;
  24. enum DevState state;
  25. QemuOpts *opts;
  26. int hotplugged;
  27. DeviceInfo *info;
  28. BusState *parent_bus;
  29. int num_gpio_out;
  30. qemu_irq *gpio_out;
  31. int num_gpio_in;
  32. qemu_irq *gpio_in;
  33. QLIST_HEAD(, BusState) child_bus;
  34. int num_child_bus;
  35. QTAILQ_ENTRY(DeviceState) sibling;
  36. int instance_id_alias;
  37. int alias_required_for_version;
  38. };
  39. typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
  40. typedef char *(*bus_get_dev_path)(DeviceState *dev);
  41. /*
  42. * This callback is used to create Open Firmware device path in accordance with
  43. * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
  44. * can be found here http://playground.sun.com/1275/bindings/.
  45. */
  46. typedef char *(*bus_get_fw_dev_path)(DeviceState *dev);
  47. typedef int (qbus_resetfn)(BusState *bus);
  48. struct BusInfo {
  49. const char *name;
  50. size_t size;
  51. bus_dev_printfn print_dev;
  52. bus_get_dev_path get_dev_path;
  53. bus_get_fw_dev_path get_fw_dev_path;
  54. qbus_resetfn *reset;
  55. Property *props;
  56. };
  57. struct BusState {
  58. DeviceState *parent;
  59. BusInfo *info;
  60. const char *name;
  61. int allow_hotplug;
  62. int qdev_allocated;
  63. QTAILQ_HEAD(ChildrenHead, DeviceState) children;
  64. QLIST_ENTRY(BusState) sibling;
  65. };
  66. struct Property {
  67. const char *name;
  68. PropertyInfo *info;
  69. int offset;
  70. int bitnr;
  71. void *defval;
  72. };
  73. enum PropertyType {
  74. PROP_TYPE_UNSPEC = 0,
  75. PROP_TYPE_UINT8,
  76. PROP_TYPE_UINT16,
  77. PROP_TYPE_UINT32,
  78. PROP_TYPE_INT32,
  79. PROP_TYPE_UINT64,
  80. PROP_TYPE_TADDR,
  81. PROP_TYPE_MACADDR,
  82. PROP_TYPE_DRIVE,
  83. PROP_TYPE_CHR,
  84. PROP_TYPE_STRING,
  85. PROP_TYPE_NETDEV,
  86. PROP_TYPE_VLAN,
  87. PROP_TYPE_PTR,
  88. PROP_TYPE_BIT,
  89. };
  90. struct PropertyInfo {
  91. const char *name;
  92. size_t size;
  93. enum PropertyType type;
  94. int (*parse)(DeviceState *dev, Property *prop, const char *str);
  95. int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
  96. void (*free)(DeviceState *dev, Property *prop);
  97. };
  98. typedef struct GlobalProperty {
  99. const char *driver;
  100. const char *property;
  101. const char *value;
  102. QTAILQ_ENTRY(GlobalProperty) next;
  103. } GlobalProperty;
  104. /*** Board API. This should go away once we have a machine config file. ***/
  105. DeviceState *qdev_create(BusState *bus, const char *name);
  106. DeviceState *qdev_try_create(BusState *bus, const char *name);
  107. int qdev_device_help(QemuOpts *opts);
  108. DeviceState *qdev_device_add(QemuOpts *opts);
  109. int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
  110. void qdev_init_nofail(DeviceState *dev);
  111. void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
  112. int required_for_version);
  113. int qdev_unplug(DeviceState *dev);
  114. void qdev_free(DeviceState *dev);
  115. int qdev_simple_unplug_cb(DeviceState *dev);
  116. void qdev_machine_creation_done(void);
  117. bool qdev_machine_modified(void);
  118. qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
  119. void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
  120. BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
  121. /*** Device API. ***/
  122. typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
  123. typedef int (*qdev_event)(DeviceState *dev);
  124. typedef void (*qdev_resetfn)(DeviceState *dev);
  125. struct DeviceInfo {
  126. const char *name;
  127. const char *fw_name;
  128. const char *alias;
  129. const char *desc;
  130. size_t size;
  131. Property *props;
  132. int no_user;
  133. /* callbacks */
  134. qdev_resetfn reset;
  135. /* device state */
  136. const VMStateDescription *vmsd;
  137. /* Private to qdev / bus. */
  138. qdev_initfn init;
  139. qdev_event unplug;
  140. qdev_event exit;
  141. BusInfo *bus_info;
  142. struct DeviceInfo *next;
  143. };
  144. extern DeviceInfo *device_info_list;
  145. void qdev_register(DeviceInfo *info);
  146. /* Register device properties. */
  147. /* GPIO inputs also double as IRQ sinks. */
  148. void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
  149. void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
  150. CharDriverState *qdev_init_chardev(DeviceState *dev);
  151. BusState *qdev_get_parent_bus(DeviceState *dev);
  152. /*** BUS API. ***/
  153. DeviceState *qdev_find_recursive(BusState *bus, const char *id);
  154. /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
  155. typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
  156. typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
  157. void qbus_create_inplace(BusState *bus, BusInfo *info,
  158. DeviceState *parent, const char *name);
  159. BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
  160. /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
  161. * < 0 if either devfn or busfn terminate walk somewhere in cursion,
  162. * 0 otherwise. */
  163. int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
  164. qbus_walkerfn *busfn, void *opaque);
  165. int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
  166. qbus_walkerfn *busfn, void *opaque);
  167. void qdev_reset_all(DeviceState *dev);
  168. void qbus_reset_all_fn(void *opaque);
  169. void qbus_free(BusState *bus);
  170. #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
  171. /* This should go away once we get rid of the NULL bus hack */
  172. BusState *sysbus_get_default(void);
  173. /*** monitor commands ***/
  174. void do_info_qtree(Monitor *mon);
  175. void do_info_qdm(Monitor *mon);
  176. int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
  177. int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
  178. /*** qdev-properties.c ***/
  179. extern PropertyInfo qdev_prop_bit;
  180. extern PropertyInfo qdev_prop_uint8;
  181. extern PropertyInfo qdev_prop_uint16;
  182. extern PropertyInfo qdev_prop_uint32;
  183. extern PropertyInfo qdev_prop_int32;
  184. extern PropertyInfo qdev_prop_uint64;
  185. extern PropertyInfo qdev_prop_hex8;
  186. extern PropertyInfo qdev_prop_hex32;
  187. extern PropertyInfo qdev_prop_hex64;
  188. extern PropertyInfo qdev_prop_string;
  189. extern PropertyInfo qdev_prop_chr;
  190. extern PropertyInfo qdev_prop_ptr;
  191. extern PropertyInfo qdev_prop_macaddr;
  192. extern PropertyInfo qdev_prop_drive;
  193. extern PropertyInfo qdev_prop_netdev;
  194. extern PropertyInfo qdev_prop_vlan;
  195. extern PropertyInfo qdev_prop_pci_devfn;
  196. #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
  197. .name = (_name), \
  198. .info = &(_prop), \
  199. .offset = offsetof(_state, _field) \
  200. + type_check(_type,typeof_field(_state, _field)), \
  201. }
  202. #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
  203. .name = (_name), \
  204. .info = &(_prop), \
  205. .offset = offsetof(_state, _field) \
  206. + type_check(_type,typeof_field(_state, _field)), \
  207. .defval = (_type[]) { _defval }, \
  208. }
  209. #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
  210. .name = (_name), \
  211. .info = &(qdev_prop_bit), \
  212. .bitnr = (_bit), \
  213. .offset = offsetof(_state, _field) \
  214. + type_check(uint32_t,typeof_field(_state, _field)), \
  215. .defval = (bool[]) { (_defval) }, \
  216. }
  217. #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
  218. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
  219. #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
  220. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
  221. #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
  222. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
  223. #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
  224. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
  225. #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
  226. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
  227. #define DEFINE_PROP_HEX8(_n, _s, _f, _d) \
  228. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
  229. #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
  230. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
  231. #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
  232. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
  233. #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
  234. DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
  235. #define DEFINE_PROP_PTR(_n, _s, _f) \
  236. DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
  237. #define DEFINE_PROP_CHR(_n, _s, _f) \
  238. DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
  239. #define DEFINE_PROP_STRING(_n, _s, _f) \
  240. DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
  241. #define DEFINE_PROP_NETDEV(_n, _s, _f) \
  242. DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
  243. #define DEFINE_PROP_VLAN(_n, _s, _f) \
  244. DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
  245. #define DEFINE_PROP_DRIVE(_n, _s, _f) \
  246. DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
  247. #define DEFINE_PROP_MACADDR(_n, _s, _f) \
  248. DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
  249. #define DEFINE_PROP_END_OF_LIST() \
  250. {}
  251. /* Set properties between creation and init. */
  252. void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
  253. int qdev_prop_exists(DeviceState *dev, const char *name);
  254. int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
  255. void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
  256. void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
  257. void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
  258. void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
  259. void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
  260. void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
  261. void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
  262. void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
  263. void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
  264. void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
  265. void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
  266. int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
  267. void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
  268. void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
  269. /* FIXME: Remove opaque pointer properties. */
  270. void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
  271. void qdev_prop_set_defaults(DeviceState *dev, Property *props);
  272. void qdev_prop_register_global_list(GlobalProperty *props);
  273. void qdev_prop_set_globals(DeviceState *dev);
  274. static inline const char *qdev_fw_name(DeviceState *dev)
  275. {
  276. return dev->info->fw_name ? : dev->info->alias ? : dev->info->name;
  277. }
  278. char *qdev_get_fw_dev_path(DeviceState *dev);
  279. /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
  280. extern struct BusInfo system_bus_info;
  281. #endif