2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
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.
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.
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/>.
20 /* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
33 struct DeviceProperty {
43 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
44 static BusState *main_system_bus;
45 extern struct BusInfo system_bus_info;
47 static DeviceInfo *device_info_list;
49 /* Register a new device type. */
50 void qdev_register(DeviceInfo *info)
52 assert(info->size >= sizeof(DeviceState));
55 info->next = device_info_list;
56 device_info_list = info;
59 /* Create a new device. This only initializes the device state structure
60 and allows properties to be set. qdev_init should be called to
61 initialize the actual device emulation. */
62 DeviceState *qdev_create(BusState *bus, const char *name)
68 if (!main_system_bus) {
69 main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus");
71 bus = main_system_bus;
74 for (info = device_info_list; info != NULL; info = info->next) {
75 if (info->bus_info != bus->info)
77 if (strcmp(info->name, name) != 0)
82 hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
85 dev = qemu_mallocz(info->size);
87 dev->parent_bus = bus;
88 LIST_INSERT_HEAD(&bus->children, dev, sibling);
92 /* Initialize a device. Device properties should be set before calling
93 this function. IRQs and MMIO regions should be connected/mapped after
94 calling this function. */
95 void qdev_init(DeviceState *dev)
97 dev->info->init(dev, dev->info);
100 /* Unlink device from bus and free the structure. */
101 void qdev_free(DeviceState *dev)
103 LIST_REMOVE(dev, sibling);
107 static DeviceProperty *create_prop(DeviceState *dev, const char *name,
110 DeviceProperty *prop;
112 /* TODO: Check for duplicate properties. */
113 prop = qemu_mallocz(sizeof(*prop));
114 prop->name = qemu_strdup(name);
116 prop->next = dev->props;
122 void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value)
124 DeviceProperty *prop;
126 prop = create_prop(dev, name, PROP_TYPE_INT);
127 prop->value.i = value;
130 void qdev_set_prop_dev(DeviceState *dev, const char *name, DeviceState *value)
132 DeviceProperty *prop;
134 prop = create_prop(dev, name, PROP_TYPE_DEV);
135 prop->value.ptr = value;
138 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
140 DeviceProperty *prop;
142 prop = create_prop(dev, name, PROP_TYPE_PTR);
143 prop->value.ptr = value;
146 void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
153 /* Get a character (serial) device interface. */
154 CharDriverState *qdev_init_chardev(DeviceState *dev)
156 static int next_serial;
157 static int next_virtconsole;
158 /* FIXME: This is a nasty hack that needs to go away. */
159 if (strncmp(dev->info->name, "virtio", 6) == 0) {
160 return virtcon_hds[next_virtconsole++];
162 return serial_hds[next_serial++];
166 BusState *qdev_get_parent_bus(DeviceState *dev)
168 return dev->parent_bus;
171 static DeviceProperty *find_prop(DeviceState *dev, const char *name,
174 DeviceProperty *prop;
176 for (prop = dev->props; prop; prop = prop->next) {
177 if (strcmp(prop->name, name) == 0) {
178 assert (prop->type == type);
185 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
187 DeviceProperty *prop;
189 prop = find_prop(dev, name, PROP_TYPE_INT);
194 return prop->value.i;
197 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
199 DeviceProperty *prop;
201 prop = find_prop(dev, name, PROP_TYPE_PTR);
203 return prop->value.ptr;
206 DeviceState *qdev_get_prop_dev(DeviceState *dev, const char *name)
208 DeviceProperty *prop;
210 prop = find_prop(dev, name, PROP_TYPE_DEV);
214 return prop->value.ptr;
217 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
219 assert(dev->num_gpio_in == 0);
220 dev->num_gpio_in = n;
221 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
224 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
226 assert(dev->num_gpio_out == 0);
227 dev->num_gpio_out = n;
228 dev->gpio_out = pins;
231 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
233 assert(n >= 0 && n < dev->num_gpio_in);
234 return dev->gpio_in[n];
237 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
239 assert(n >= 0 && n < dev->num_gpio_out);
240 dev->gpio_out[n] = pin;
243 VLANClientState *qdev_get_vlan_client(DeviceState *dev,
244 NetCanReceive *can_receive,
246 NetReceiveIOV *receive_iov,
250 NICInfo *nd = dev->nd;
252 nd->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name, can_receive,
253 receive, receive_iov, cleanup, opaque);
258 void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
260 memcpy(macaddr, dev->nd->macaddr, 6);
263 static int next_block_unit[IF_COUNT];
265 /* Get a block device. This should only be used for single-drive devices
266 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
268 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
270 int unit = next_block_unit[type]++;
273 index = drive_get_index(type, 0, unit);
277 return drives_table[index].bdrv;
280 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
284 LIST_FOREACH(bus, &dev->child_bus, sibling) {
285 if (strcmp(name, bus->name) == 0) {
292 static int next_scsi_bus;
294 /* Create a scsi bus, and attach devices to it. */
295 /* TODO: Actually create a scsi bus for hotplug to use. */
296 void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
298 int bus = next_scsi_bus++;
302 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
303 index = drive_get_index(IF_SCSI, bus, unit);
307 attach(host, drives_table[index].bdrv, unit);
311 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
315 bus = qemu_mallocz(info->size);
317 bus->parent = parent;
318 bus->name = qemu_strdup(name);
319 LIST_INIT(&bus->children);
321 LIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
326 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
327 static void qbus_print(Monitor *mon, BusState *bus, int indent);
329 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
331 DeviceProperty *prop;
333 qdev_printf("dev: %s\n", dev->info->name);
335 if (dev->num_gpio_in) {
336 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
338 if (dev->num_gpio_out) {
339 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
341 for (prop = dev->props; prop; prop = prop->next) {
342 switch (prop->type) {
344 qdev_printf("prop-int %s 0x%" PRIx64 "\n", prop->name,
348 qdev_printf("prop-ptr %s\n", prop->name);
351 qdev_printf("prop-dev %s %s\n", prop->name,
352 ((DeviceState *)prop->value.ptr)->info->name);
355 qdev_printf("prop-unknown%d %s\n", prop->type, prop->name);
359 if (dev->parent_bus->info->print_dev)
360 dev->parent_bus->info->print_dev(mon, dev, indent);
361 LIST_FOREACH(child, &dev->child_bus, sibling) {
362 qbus_print(mon, child, indent);
366 static void qbus_print(Monitor *mon, BusState *bus, int indent)
368 struct DeviceState *dev;
370 qdev_printf("bus: %s\n", bus->name);
372 qdev_printf("type %s\n", bus->info->name);
373 LIST_FOREACH(dev, &bus->children, sibling) {
374 qdev_print(mon, dev, indent);
379 void do_info_qtree(Monitor *mon)
382 qbus_print(mon, main_system_bus, 0);