1 #ifndef QDEV_CORE_H 2 #define QDEV_CORE_H 3 4 #include "qemu/queue.h" 5 #include "qemu/option.h" 6 #include "qemu/typedefs.h" 7 #include "qemu/bitmap.h" 8 #include "qom/object.h" 9 #include "hw/irq.h" 10 #include "qapi/error.h" 11 12 enum { 13 DEV_NVECTORS_UNSPECIFIED = -1, 14 }; 15 16 #define TYPE_DEVICE "device" 17 #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE) 18 #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE) 19 #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE) 20 21 typedef enum DeviceCategory { 22 DEVICE_CATEGORY_BRIDGE, 23 DEVICE_CATEGORY_USB, 24 DEVICE_CATEGORY_STORAGE, 25 DEVICE_CATEGORY_NETWORK, 26 DEVICE_CATEGORY_INPUT, 27 DEVICE_CATEGORY_DISPLAY, 28 DEVICE_CATEGORY_SOUND, 29 DEVICE_CATEGORY_MISC, 30 DEVICE_CATEGORY_MAX 31 } DeviceCategory; 32 33 static inline const char *qdev_category_get_name(DeviceCategory category) 34 { 35 static const char *category_names[DEVICE_CATEGORY_MAX] = { 36 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub", 37 [DEVICE_CATEGORY_USB] = "USB", 38 [DEVICE_CATEGORY_STORAGE] = "Storage", 39 [DEVICE_CATEGORY_NETWORK] = "Network", 40 [DEVICE_CATEGORY_INPUT] = "Input", 41 [DEVICE_CATEGORY_DISPLAY] = "Display", 42 [DEVICE_CATEGORY_SOUND] = "Sound", 43 [DEVICE_CATEGORY_MISC] = "Misc", 44 }; 45 46 return category_names[category]; 47 }; 48 49 typedef int (*qdev_initfn)(DeviceState *dev); 50 typedef int (*qdev_event)(DeviceState *dev); 51 typedef void (*qdev_resetfn)(DeviceState *dev); 52 typedef void (*DeviceRealize)(DeviceState *dev, Error **errp); 53 typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp); 54 55 struct VMStateDescription; 56 57 /** 58 * DeviceClass: 59 * @props: Properties accessing state fields. 60 * @realize: Callback function invoked when the #DeviceState:realized 61 * property is changed to %true. The default invokes @init if not %NULL. 62 * @unrealize: Callback function invoked when the #DeviceState:realized 63 * property is changed to %false. 64 * @init: Callback function invoked when the #DeviceState::realized property 65 * is changed to %true. Deprecated, new types inheriting directly from 66 * TYPE_DEVICE should use @realize instead, new leaf types should consult 67 * their respective parent type. 68 * 69 * # Realization # 70 * Devices are constructed in two stages, 71 * 1) object instantiation via object_initialize() and 72 * 2) device realization via #DeviceState:realized property. 73 * The former may not fail (it might assert or exit), the latter may return 74 * error information to the caller and must be re-entrant. 75 * Trivial field initializations should go into #TypeInfo.instance_init. 76 * Operations depending on @props static properties should go into @realize. 77 * After successful realization, setting static properties will fail. 78 * 79 * As an interim step, the #DeviceState:realized property is set by deprecated 80 * functions qdev_init() and qdev_init_nofail(). 81 * In the future, devices will propagate this state change to their children 82 * and along busses they expose. 83 * The point in time will be deferred to machine creation, so that values 84 * set in @realize will not be introspectable beforehand. Therefore devices 85 * must not create children during @realize; they should initialize them via 86 * object_initialize() in their own #TypeInfo.instance_init and forward the 87 * realization events appropriately. 88 * 89 * The @init callback is considered private to a particular bus implementation 90 * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an 91 * "init" callback on their parent class instead. 92 * 93 * Any type may override the @realize and/or @unrealize callbacks but needs 94 * to call the parent type's implementation if keeping their functionality 95 * is desired. Refer to QOM documentation for further discussion and examples. 96 * 97 * <note> 98 * <para> 99 * If a type derived directly from TYPE_DEVICE implements @realize, it does 100 * not need to implement @init and therefore does not need to store and call 101 * #DeviceClass' default @realize callback. 102 * For other types consult the documentation and implementation of the 103 * respective parent types. 104 * </para> 105 * </note> 106 */ 107 typedef struct DeviceClass { 108 /*< private >*/ 109 ObjectClass parent_class; 110 /*< public >*/ 111 112 DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX); 113 const char *fw_name; 114 const char *desc; 115 Property *props; 116 int no_user; 117 118 /* callbacks */ 119 void (*reset)(DeviceState *dev); 120 DeviceRealize realize; 121 DeviceUnrealize unrealize; 122 123 /* device state */ 124 const struct VMStateDescription *vmsd; 125 126 /* Private to qdev / bus. */ 127 qdev_initfn init; /* TODO remove, once users are converted to realize */ 128 qdev_event unplug; 129 qdev_event exit; /* TODO remove, once users are converted to unrealize */ 130 const char *bus_type; 131 } DeviceClass; 132 133 /** 134 * DeviceState: 135 * @realized: Indicates whether the device has been fully constructed. 136 * 137 * This structure should not be accessed directly. We declare it here 138 * so that it can be embedded in individual device state structures. 139 */ 140 struct DeviceState { 141 /*< private >*/ 142 Object parent_obj; 143 /*< public >*/ 144 145 const char *id; 146 bool realized; 147 QemuOpts *opts; 148 int hotplugged; 149 BusState *parent_bus; 150 int num_gpio_out; 151 qemu_irq *gpio_out; 152 int num_gpio_in; 153 qemu_irq *gpio_in; 154 QLIST_HEAD(, BusState) child_bus; 155 int num_child_bus; 156 int instance_id_alias; 157 int alias_required_for_version; 158 }; 159 160 #define TYPE_BUS "bus" 161 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS) 162 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS) 163 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS) 164 165 struct BusClass { 166 ObjectClass parent_class; 167 168 /* FIXME first arg should be BusState */ 169 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent); 170 char *(*get_dev_path)(DeviceState *dev); 171 /* 172 * This callback is used to create Open Firmware device path in accordance 173 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus 174 * bindings can be found at http://playground.sun.com/1275/bindings/. 175 */ 176 char *(*get_fw_dev_path)(DeviceState *dev); 177 int (*reset)(BusState *bus); 178 /* maximum devices allowed on the bus, 0: no limit. */ 179 int max_dev; 180 }; 181 182 typedef struct BusChild { 183 DeviceState *child; 184 int index; 185 QTAILQ_ENTRY(BusChild) sibling; 186 } BusChild; 187 188 /** 189 * BusState: 190 */ 191 struct BusState { 192 Object obj; 193 DeviceState *parent; 194 const char *name; 195 int allow_hotplug; 196 int max_index; 197 QTAILQ_HEAD(ChildrenHead, BusChild) children; 198 QLIST_ENTRY(BusState) sibling; 199 }; 200 201 struct Property { 202 const char *name; 203 PropertyInfo *info; 204 int offset; 205 uint8_t bitnr; 206 uint8_t qtype; 207 int64_t defval; 208 int arrayoffset; 209 PropertyInfo *arrayinfo; 210 int arrayfieldsize; 211 }; 212 213 struct PropertyInfo { 214 const char *name; 215 const char *legacy_name; 216 const char **enum_table; 217 int (*parse)(DeviceState *dev, Property *prop, const char *str); 218 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len); 219 ObjectPropertyAccessor *get; 220 ObjectPropertyAccessor *set; 221 ObjectPropertyRelease *release; 222 }; 223 224 typedef struct GlobalProperty { 225 const char *driver; 226 const char *property; 227 const char *value; 228 QTAILQ_ENTRY(GlobalProperty) next; 229 } GlobalProperty; 230 231 /*** Board API. This should go away once we have a machine config file. ***/ 232 233 DeviceState *qdev_create(BusState *bus, const char *name); 234 DeviceState *qdev_try_create(BusState *bus, const char *name); 235 int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT; 236 void qdev_init_nofail(DeviceState *dev); 237 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, 238 int required_for_version); 239 void qdev_unplug(DeviceState *dev, Error **errp); 240 void qdev_free(DeviceState *dev); 241 int qdev_simple_unplug_cb(DeviceState *dev); 242 void qdev_machine_creation_done(void); 243 bool qdev_machine_modified(void); 244 245 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n); 246 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin); 247 248 BusState *qdev_get_child_bus(DeviceState *dev, const char *name); 249 250 /*** Device API. ***/ 251 252 /* Register device properties. */ 253 /* GPIO inputs also double as IRQ sinks. */ 254 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n); 255 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n); 256 257 BusState *qdev_get_parent_bus(DeviceState *dev); 258 259 /*** BUS API. ***/ 260 261 DeviceState *qdev_find_recursive(BusState *bus, const char *id); 262 263 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */ 264 typedef int (qbus_walkerfn)(BusState *bus, void *opaque); 265 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque); 266 267 void qbus_create_inplace(void *bus, const char *typename, 268 DeviceState *parent, const char *name); 269 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name); 270 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion, 271 * < 0 if either devfn or busfn terminate walk somewhere in cursion, 272 * 0 otherwise. */ 273 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, 274 qbus_walkerfn *busfn, void *opaque); 275 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, 276 qbus_walkerfn *busfn, void *opaque); 277 void qdev_reset_all(DeviceState *dev); 278 279 /** 280 * @qbus_reset_all: 281 * @bus: Bus to be reset. 282 * 283 * Reset @bus and perform a bus-level ("hard") reset of all devices connected 284 * to it, including recursive processing of all buses below @bus itself. A 285 * hard reset means that qbus_reset_all will reset all state of the device. 286 * For PCI devices, for example, this will include the base address registers 287 * or configuration space. 288 */ 289 void qbus_reset_all(BusState *bus); 290 void qbus_reset_all_fn(void *opaque); 291 292 void qbus_free(BusState *bus); 293 294 /* This should go away once we get rid of the NULL bus hack */ 295 BusState *sysbus_get_default(void); 296 297 char *qdev_get_fw_dev_path(DeviceState *dev); 298 299 /** 300 * @qdev_machine_init 301 * 302 * Initialize platform devices before machine init. This is a hack until full 303 * support for composition is added. 304 */ 305 void qdev_machine_init(void); 306 307 /** 308 * @device_reset 309 * 310 * Reset a single device (by calling the reset method). 311 */ 312 void device_reset(DeviceState *dev); 313 314 const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev); 315 316 const char *qdev_fw_name(DeviceState *dev); 317 318 Object *qdev_get_machine(void); 319 320 /* FIXME: make this a link<> */ 321 void qdev_set_parent_bus(DeviceState *dev, BusState *bus); 322 323 extern int qdev_hotplug; 324 325 char *qdev_get_dev_path(DeviceState *dev); 326 327 #endif 328