1 #ifndef QDEV_CORE_H 2 #define QDEV_CORE_H 3 4 #include "hw/irq.h" 5 #include "qemu/queue.h" 6 7 typedef struct DeviceType DeviceType; 8 9 typedef struct DeviceProperty DeviceProperty; 10 11 /* This structure should not be accessed directly. We declare it here 12 so that it can be embedded in individual device state structures. */ 13 struct DeviceState { 14 DeviceType *type; 15 BusState *parent_bus; 16 DeviceProperty *props; 17 int num_gpio_out; 18 qemu_irq *gpio_out; 19 int num_gpio_in; 20 qemu_irq *gpio_in; 21 QLIST_HEAD(, BusState) child_bus; 22 NICInfo *nd; 23 QLIST_ENTRY(DeviceState) sibling; 24 }; 25 26 typedef enum { 27 BUS_TYPE_SYSTEM, 28 BUS_TYPE_PCI, 29 BUS_TYPE_SCSI, 30 BUS_TYPE_I2C, 31 BUS_TYPE_SSI 32 } BusType; 33 34 struct BusState { 35 DeviceState *parent; 36 const char *name; 37 BusType type; 38 QLIST_HEAD(, DeviceState) children; 39 QLIST_ENTRY(BusState) sibling; 40 }; 41 42 /*** Board API. This should go away once we have a machine config file. ***/ 43 44 DeviceState *qdev_create(BusState *bus, const char *name); 45 void qdev_init(DeviceState *dev); 46 void qdev_free(DeviceState *dev); 47 48 /* Set properties between creation and init. */ 49 void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value); 50 void qdev_set_prop_dev(DeviceState *dev, const char *name, DeviceState *value); 51 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value); 52 void qdev_set_netdev(DeviceState *dev, NICInfo *nd); 53 54 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n); 55 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin); 56 57 BusState *qdev_get_child_bus(DeviceState *dev, const char *name); 58 59 /*** Device API. ***/ 60 61 typedef enum { 62 PROP_TYPE_INT, 63 PROP_TYPE_PTR, 64 PROP_TYPE_DEV 65 } DevicePropType; 66 67 typedef struct { 68 const char *name; 69 DevicePropType type; 70 } DevicePropList; 71 72 typedef struct DeviceInfo DeviceInfo; 73 74 typedef void (*qdev_initfn)(DeviceState *dev, DeviceInfo *info); 75 typedef void (*SCSIAttachFn)(DeviceState *host, BlockDriverState *bdrv, 76 int unit); 77 78 struct DeviceInfo { 79 const char *name; 80 size_t size; 81 DevicePropList *props; 82 83 /* Private to qdev / bus. */ 84 qdev_initfn init; 85 BusType bus_type; 86 }; 87 88 void qdev_register(DeviceInfo *info); 89 90 /* Register device properties. */ 91 /* GPIO inputs also double as IRQ sinks. */ 92 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n); 93 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n); 94 95 void scsi_bus_new(DeviceState *host, SCSIAttachFn attach); 96 97 CharDriverState *qdev_init_chardev(DeviceState *dev); 98 99 BusState *qdev_get_parent_bus(DeviceState *dev); 100 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def); 101 DeviceState *qdev_get_prop_dev(DeviceState *dev, const char *name); 102 /* FIXME: Remove opaque pointer properties. */ 103 void *qdev_get_prop_ptr(DeviceState *dev, const char *name); 104 105 /* Convery from a base type to a parent type, with compile time checking. */ 106 #ifdef __GNUC__ 107 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ 108 char __attribute__((unused)) offset_must_be_zero[ \ 109 -offsetof(type, field)]; \ 110 container_of(dev, type, field);})) 111 #else 112 #define DO_UPCAST(type, field, dev) container_of(dev, type, field) 113 #endif 114 115 /*** BUS API. ***/ 116 117 BusState *qbus_create(BusType type, size_t size, 118 DeviceState *parent, const char *name); 119 120 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev) 121 122 /*** monitor commands ***/ 123 124 void do_info_qtree(Monitor *mon); 125 void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent); 126 127 char *qdev_get_dev_path(DeviceState *dev); 128 129 #endif // QDEV_CORE_H 130