blob: 8cd843e763a0172197b38dacaaa91a4743c79ffe [file] [log] [blame]
Paul Brookaae94602009-05-14 22:35:06 +01001#ifndef QDEV_H
2#define QDEV_H
3
4#include "hw.h"
Gerd Hoffmann14b41872009-07-31 12:25:40 +02005#include "sysemu.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +00006#include "qemu-queue.h"
Gerd Hoffmann313feaa2009-08-03 17:35:18 +02007#include "qemu-char.h"
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02008#include "qemu-option.h"
Paul Brookaae94602009-05-14 22:35:06 +01009
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020010typedef struct Property Property;
Paul Brookaae94602009-05-14 22:35:06 +010011
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020012typedef struct PropertyInfo PropertyInfo;
13
Gerd Hoffmannb6b61142009-07-15 13:48:21 +020014typedef struct CompatProperty CompatProperty;
15
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020016typedef struct DeviceInfo DeviceInfo;
Paul Brookaae94602009-05-14 22:35:06 +010017
Paul Brook02e2da42009-05-23 00:05:19 +010018typedef struct BusState BusState;
Paul Brook4d6ae672009-05-14 22:35:06 +010019
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020020typedef struct BusInfo BusInfo;
21
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +020022enum DevState {
23 DEV_STATE_CREATED = 1,
24 DEV_STATE_INITIALIZED,
25};
26
Paul Brookaae94602009-05-14 22:35:06 +010027/* This structure should not be accessed directly. We declare it here
28 so that it can be embedded in individual device state structures. */
Paul Brook02e2da42009-05-23 00:05:19 +010029struct DeviceState {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +020030 const char *id;
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +020031 enum DevState state;
Gerd Hoffmannef80b462009-09-25 21:42:49 +020032 QemuOpts *opts;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020033 int hotplugged;
Gerd Hoffmann042f84d2009-06-30 14:12:09 +020034 DeviceInfo *info;
Paul Brook02e2da42009-05-23 00:05:19 +010035 BusState *parent_bus;
Paul Brookaae94602009-05-14 22:35:06 +010036 int num_gpio_out;
37 qemu_irq *gpio_out;
38 int num_gpio_in;
39 qemu_irq *gpio_in;
Blue Swirl72cf2d42009-09-12 07:36:22 +000040 QLIST_HEAD(, BusState) child_bus;
Gerd Hoffmannd271de92009-07-15 13:59:24 +020041 int num_child_bus;
Paul Brook9d07d752009-05-14 22:35:07 +010042 NICInfo *nd;
Blue Swirl72cf2d42009-09-12 07:36:22 +000043 QLIST_ENTRY(DeviceState) sibling;
Paul Brook02e2da42009-05-23 00:05:19 +010044};
45
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020046typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
47struct BusInfo {
48 const char *name;
49 size_t size;
50 bus_dev_printfn print_dev;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020051 Property *props;
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020052};
Paul Brook02e2da42009-05-23 00:05:19 +010053
54struct BusState {
55 DeviceState *parent;
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020056 BusInfo *info;
Paul Brook02e2da42009-05-23 00:05:19 +010057 const char *name;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +020058 int allow_hotplug;
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +020059 int qdev_allocated;
Blue Swirl72cf2d42009-09-12 07:36:22 +000060 QLIST_HEAD(, DeviceState) children;
61 QLIST_ENTRY(BusState) sibling;
Paul Brookaae94602009-05-14 22:35:06 +010062};
63
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020064struct Property {
65 const char *name;
66 PropertyInfo *info;
67 int offset;
68 void *defval;
69};
70
71enum PropertyType {
72 PROP_TYPE_UNSPEC = 0,
Juan Quintelac7cc1722009-09-29 22:48:25 +020073 PROP_TYPE_UINT8,
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020074 PROP_TYPE_UINT16,
75 PROP_TYPE_UINT32,
Gerd Hoffmann316940b2009-09-10 11:43:25 +020076 PROP_TYPE_INT32,
Blue Swirl5a053d12009-07-21 11:10:41 +000077 PROP_TYPE_UINT64,
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020078 PROP_TYPE_TADDR,
79 PROP_TYPE_MACADDR,
Gerd Hoffmann14b41872009-07-31 12:25:40 +020080 PROP_TYPE_DRIVE,
Gerd Hoffmann313feaa2009-08-03 17:35:18 +020081 PROP_TYPE_CHR,
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020082 PROP_TYPE_PTR,
83};
84
85struct PropertyInfo {
86 const char *name;
87 size_t size;
88 enum PropertyType type;
89 int (*parse)(DeviceState *dev, Property *prop, const char *str);
90 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
91};
92
Gerd Hoffmannb6b61142009-07-15 13:48:21 +020093struct CompatProperty {
94 const char *driver;
95 const char *property;
96 const char *value;
97};
98
Paul Brookaae94602009-05-14 22:35:06 +010099/*** Board API. This should go away once we have a machine config file. ***/
100
Paul Brook02e2da42009-05-23 00:05:19 +0100101DeviceState *qdev_create(BusState *bus, const char *name);
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200102DeviceState *qdev_device_add(QemuOpts *opts);
Markus Armbrusterdde8bbb2009-10-07 01:16:00 +0200103int qdev_init(DeviceState *dev) __attribute__((warn_unused_result));
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200104void qdev_init_nofail(DeviceState *dev);
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200105int qdev_unplug(DeviceState *dev);
Paul Brook02e2da42009-05-23 00:05:19 +0100106void qdev_free(DeviceState *dev);
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200107int qdev_simple_unplug_cb(DeviceState *dev);
108void qdev_machine_creation_done(void);
Paul Brookaae94602009-05-14 22:35:06 +0100109
Paul Brookaae94602009-05-14 22:35:06 +0100110qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
111void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
112
Paul Brook02e2da42009-05-23 00:05:19 +0100113BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
Paul Brook4d6ae672009-05-14 22:35:06 +0100114
Paul Brookaae94602009-05-14 22:35:06 +0100115/*** Device API. ***/
116
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200117typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200118typedef int (*qdev_event)(DeviceState *dev);
Michael S. Tsirkin7f23f812009-09-16 13:40:27 +0300119typedef void (*qdev_resetfn)(DeviceState *dev);
Paul Brookaae94602009-05-14 22:35:06 +0100120
Paul Brook02e2da42009-05-23 00:05:19 +0100121struct DeviceInfo {
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +0200122 const char *name;
Gerd Hoffmann3320e562009-07-15 13:43:33 +0200123 const char *alias;
124 const char *desc;
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +0200125 size_t size;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200126 Property *props;
Gerd Hoffmann3320e562009-07-15 13:43:33 +0200127 int no_user;
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +0200128
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200129 /* callbacks */
Michael S. Tsirkin7f23f812009-09-16 13:40:27 +0300130 qdev_resetfn reset;
Gerd Hoffmann959f7332009-09-01 09:56:12 +0200131
Gerd Hoffmann391a0792009-09-01 09:56:14 +0200132 /* device state */
133 const VMStateDescription *vmsd;
134
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +0200135 /* Private to qdev / bus. */
Paul Brook02e2da42009-05-23 00:05:19 +0100136 qdev_initfn init;
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200137 qdev_event unplug;
138 qdev_event exit;
Gerd Hoffmann10c4c982009-06-30 14:12:08 +0200139 BusInfo *bus_info;
Gerd Hoffmann042f84d2009-06-30 14:12:09 +0200140 struct DeviceInfo *next;
Paul Brook02e2da42009-05-23 00:05:19 +0100141};
142
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +0200143void qdev_register(DeviceInfo *info);
Paul Brookaae94602009-05-14 22:35:06 +0100144
145/* Register device properties. */
Paul Brook067a3dd2009-05-26 14:56:11 +0100146/* GPIO inputs also double as IRQ sinks. */
Paul Brookaae94602009-05-14 22:35:06 +0100147void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
148void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
Paul Brookaae94602009-05-14 22:35:06 +0100149
Paul Brookaae94602009-05-14 22:35:06 +0100150CharDriverState *qdev_init_chardev(DeviceState *dev);
151
Paul Brook02e2da42009-05-23 00:05:19 +0100152BusState *qdev_get_parent_bus(DeviceState *dev);
Paul Brookaae94602009-05-14 22:35:06 +0100153
Stefan Weil979ba182009-09-17 19:06:53 +0200154/* Convert from a base type to a parent type, with compile time checking. */
Paul Brookaae94602009-05-14 22:35:06 +0100155#ifdef __GNUC__
156#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
157 char __attribute__((unused)) offset_must_be_zero[ \
158 -offsetof(type, field)]; \
159 container_of(dev, type, field);}))
160#else
161#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
162#endif
163
Paul Brook02e2da42009-05-23 00:05:19 +0100164/*** BUS API. ***/
165
Gerd Hoffmanncd739fb2009-09-16 22:25:27 +0200166void qbus_create_inplace(BusState *bus, BusInfo *info,
167 DeviceState *parent, const char *name);
Gerd Hoffmann10c4c982009-06-30 14:12:08 +0200168BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
Gerd Hoffmann131ec1b2009-09-25 21:42:34 +0200169void qbus_free(BusState *bus);
Paul Brook02e2da42009-05-23 00:05:19 +0100170
171#define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
172
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100173/*** monitor commands ***/
174
175void do_info_qtree(Monitor *mon);
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +0200176void do_info_qdm(Monitor *mon);
Gerd Hoffmann3418bd22009-09-25 21:42:41 +0200177void do_device_add(Monitor *mon, const QDict *qdict);
178void do_device_del(Monitor *mon, const QDict *qdict);
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100179
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200180/*** qdev-properties.c ***/
181
Juan Quintelac7cc1722009-09-29 22:48:25 +0200182extern PropertyInfo qdev_prop_uint8;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200183extern PropertyInfo qdev_prop_uint16;
184extern PropertyInfo qdev_prop_uint32;
Gerd Hoffmann316940b2009-09-10 11:43:25 +0200185extern PropertyInfo qdev_prop_int32;
Blue Swirl5a053d12009-07-21 11:10:41 +0000186extern PropertyInfo qdev_prop_uint64;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200187extern PropertyInfo qdev_prop_hex32;
Blue Swirl5a053d12009-07-21 11:10:41 +0000188extern PropertyInfo qdev_prop_hex64;
Gerd Hoffmann313feaa2009-08-03 17:35:18 +0200189extern PropertyInfo qdev_prop_chr;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200190extern PropertyInfo qdev_prop_ptr;
191extern PropertyInfo qdev_prop_macaddr;
Gerd Hoffmann14b41872009-07-31 12:25:40 +0200192extern PropertyInfo qdev_prop_drive;
Gerd Hoffmann05cb5fe2009-07-15 13:59:22 +0200193extern PropertyInfo qdev_prop_pci_devfn;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200194
Gerd Hoffmanncf12b952009-08-03 17:35:17 +0200195#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
196 .name = (_name), \
197 .info = &(_prop), \
198 .offset = offsetof(_state, _field) \
199 + type_check(_type,typeof_field(_state, _field)), \
200 }
201#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
202 .name = (_name), \
203 .info = &(_prop), \
204 .offset = offsetof(_state, _field) \
205 + type_check(_type,typeof_field(_state, _field)), \
206 .defval = (_type[]) { _defval }, \
207 }
208
Juan Quintelac7cc1722009-09-29 22:48:25 +0200209#define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
210 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
Gerd Hoffmanncf12b952009-08-03 17:35:17 +0200211#define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
212 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
213#define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
214 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
Gerd Hoffmann316940b2009-09-10 11:43:25 +0200215#define DEFINE_PROP_INT32(_n, _s, _f, _d) \
216 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
Gerd Hoffmanncf12b952009-08-03 17:35:17 +0200217#define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
218 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
219#define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
220 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
221#define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
222 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
223#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
224 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
225
226#define DEFINE_PROP_PTR(_n, _s, _f) \
227 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
Gerd Hoffmann313feaa2009-08-03 17:35:18 +0200228#define DEFINE_PROP_CHR(_n, _s, _f) \
229 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +0200230#define DEFINE_PROP_DRIVE(_n, _s, _f) \
Anthony Liguoric981d392009-08-10 19:27:59 -0500231 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, DriveInfo*)
Gerd Hoffmanncf12b952009-08-03 17:35:17 +0200232#define DEFINE_PROP_MACADDR(_n, _s, _f) \
233 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, uint8_t[6])
234
235#define DEFINE_PROP_END_OF_LIST() \
236 {}
237
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200238/* Set properties between creation and init. */
239void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
240int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
241void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
Juan Quintelac7cc1722009-09-29 22:48:25 +0200242void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200243void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
244void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
Gerd Hoffmann316940b2009-09-10 11:43:25 +0200245void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
Blue Swirl5a053d12009-07-21 11:10:41 +0000246void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
Gerd Hoffmann313feaa2009-08-03 17:35:18 +0200247void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
Gerd Hoffmann14b41872009-07-31 12:25:40 +0200248void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value);
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200249/* FIXME: Remove opaque pointer properties. */
250void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
251void qdev_prop_set_defaults(DeviceState *dev, Property *props);
252
Gerd Hoffmannb6b61142009-07-15 13:48:21 +0200253void qdev_prop_register_compat(CompatProperty *props);
254void qdev_prop_set_compat(DeviceState *dev);
255
Blue Swirla9ff9df2009-07-17 11:18:53 +0000256/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
257extern struct BusInfo system_bus_info;
258
Paul Brookaae94602009-05-14 22:35:06 +0100259#endif