blob: 8506d85fac5527a1fa1445b300eba0aace098439 [file] [log] [blame]
Paul Brookaae94602009-05-14 22:35:06 +01001/*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
19 */
20
21/* The theory here is that it should be possible to create a machine without
22 knowledge of specific devices. Historically board init routines have
23 passed a bunch of arguments to each device, requiring the board know
24 exactly which device it is dealing with. This file provides an abstract
25 API for device configuration and initialization. Devices will generally
26 inherit from a particular bus (e.g. PCI or I2C) rather than
27 this API directly. */
28
Paul Brook9d07d752009-05-14 22:35:07 +010029#include "net.h"
Paul Brookaae94602009-05-14 22:35:06 +010030#include "qdev.h"
31#include "sysemu.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010032#include "monitor.h"
Paul Brookaae94602009-05-14 22:35:06 +010033
34struct DeviceProperty {
35 const char *name;
Paul Brook1431b6a2009-06-05 15:52:04 +010036 DevicePropType type;
Paul Brookaae94602009-05-14 22:35:06 +010037 union {
Paul Brook89a740e2009-05-17 14:55:55 +010038 uint64_t i;
Paul Brookaae94602009-05-14 22:35:06 +010039 void *ptr;
40 } value;
41 DeviceProperty *next;
42};
43
44struct DeviceType {
Paul Brook02e2da42009-05-23 00:05:19 +010045 DeviceInfo *info;
Paul Brookaae94602009-05-14 22:35:06 +010046 DeviceType *next;
47};
48
Paul Brook02e2da42009-05-23 00:05:19 +010049/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
Blue Swirlb9aaf7f2009-06-09 18:38:51 +000050static BusState *main_system_bus;
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020051extern struct BusInfo system_bus_info;
Paul Brook4d6ae672009-05-14 22:35:06 +010052
Paul Brookaae94602009-05-14 22:35:06 +010053static DeviceType *device_type_list;
54
55/* Register a new device type. */
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +020056void qdev_register(DeviceInfo *info)
Paul Brookaae94602009-05-14 22:35:06 +010057{
58 DeviceType *t;
59
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +020060 assert(info->size >= sizeof(DeviceState));
Paul Brookaae94602009-05-14 22:35:06 +010061
62 t = qemu_mallocz(sizeof(DeviceType));
63 t->next = device_type_list;
64 device_type_list = t;
Paul Brook02e2da42009-05-23 00:05:19 +010065 t->info = info;
Paul Brookaae94602009-05-14 22:35:06 +010066}
67
68/* Create a new device. This only initializes the device state structure
69 and allows properties to be set. qdev_init should be called to
70 initialize the actual device emulation. */
Paul Brook02e2da42009-05-23 00:05:19 +010071DeviceState *qdev_create(BusState *bus, const char *name)
Paul Brookaae94602009-05-14 22:35:06 +010072{
73 DeviceType *t;
74 DeviceState *dev;
75
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020076 if (!bus) {
77 if (!main_system_bus) {
78 main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus");
Paul Brookaae94602009-05-14 22:35:06 +010079 }
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020080 bus = main_system_bus;
81 }
82
83 for (t = device_type_list; t; t = t->next) {
84 if (t->info->bus_info != bus->info)
85 continue;
86 if (strcmp(t->info->name, name) != 0)
87 continue;
88 break;
Paul Brookaae94602009-05-14 22:35:06 +010089 }
90 if (!t) {
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020091 hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
Paul Brookaae94602009-05-14 22:35:06 +010092 }
93
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +020094 dev = qemu_mallocz(t->info->size);
Paul Brookaae94602009-05-14 22:35:06 +010095 dev->type = t;
Paul Brook02e2da42009-05-23 00:05:19 +010096 dev->parent_bus = bus;
97 LIST_INSERT_HEAD(&bus->children, dev, sibling);
Paul Brookaae94602009-05-14 22:35:06 +010098 return dev;
99}
100
101/* Initialize a device. Device properties should be set before calling
102 this function. IRQs and MMIO regions should be connected/mapped after
103 calling this function. */
104void qdev_init(DeviceState *dev)
105{
Paul Brook02e2da42009-05-23 00:05:19 +0100106 dev->type->info->init(dev, dev->type->info);
107}
108
109/* Unlink device from bus and free the structure. */
110void qdev_free(DeviceState *dev)
111{
112 LIST_REMOVE(dev, sibling);
113 free(dev);
Paul Brookaae94602009-05-14 22:35:06 +0100114}
115
Paul Brook1431b6a2009-06-05 15:52:04 +0100116static DeviceProperty *create_prop(DeviceState *dev, const char *name,
117 DevicePropType type)
Paul Brookaae94602009-05-14 22:35:06 +0100118{
119 DeviceProperty *prop;
120
121 /* TODO: Check for duplicate properties. */
122 prop = qemu_mallocz(sizeof(*prop));
123 prop->name = qemu_strdup(name);
Paul Brook1431b6a2009-06-05 15:52:04 +0100124 prop->type = type;
Paul Brookaae94602009-05-14 22:35:06 +0100125 prop->next = dev->props;
126 dev->props = prop;
127
128 return prop;
129}
130
Paul Brook89a740e2009-05-17 14:55:55 +0100131void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value)
Paul Brookaae94602009-05-14 22:35:06 +0100132{
133 DeviceProperty *prop;
134
Paul Brook1431b6a2009-06-05 15:52:04 +0100135 prop = create_prop(dev, name, PROP_TYPE_INT);
Paul Brookaae94602009-05-14 22:35:06 +0100136 prop->value.i = value;
137}
138
Paul Brook1431b6a2009-06-05 15:52:04 +0100139void qdev_set_prop_dev(DeviceState *dev, const char *name, DeviceState *value)
140{
141 DeviceProperty *prop;
142
143 prop = create_prop(dev, name, PROP_TYPE_DEV);
144 prop->value.ptr = value;
145}
146
Paul Brookaae94602009-05-14 22:35:06 +0100147void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
148{
149 DeviceProperty *prop;
150
Paul Brookdb241f42009-06-06 02:49:32 +0100151 prop = create_prop(dev, name, PROP_TYPE_PTR);
Paul Brookaae94602009-05-14 22:35:06 +0100152 prop->value.ptr = value;
153}
154
Paul Brook9d07d752009-05-14 22:35:07 +0100155void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
156{
157 assert(!dev->nd);
158 dev->nd = nd;
159}
160
Paul Brookaae94602009-05-14 22:35:06 +0100161
Paul Brookaae94602009-05-14 22:35:06 +0100162/* Get a character (serial) device interface. */
163CharDriverState *qdev_init_chardev(DeviceState *dev)
164{
165 static int next_serial;
166 static int next_virtconsole;
167 /* FIXME: This is a nasty hack that needs to go away. */
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +0200168 if (strncmp(dev->type->info->name, "virtio", 6) == 0) {
Paul Brookaae94602009-05-14 22:35:06 +0100169 return virtcon_hds[next_virtconsole++];
170 } else {
171 return serial_hds[next_serial++];
172 }
173}
174
Paul Brook02e2da42009-05-23 00:05:19 +0100175BusState *qdev_get_parent_bus(DeviceState *dev)
Paul Brookaae94602009-05-14 22:35:06 +0100176{
Paul Brook02e2da42009-05-23 00:05:19 +0100177 return dev->parent_bus;
Paul Brookaae94602009-05-14 22:35:06 +0100178}
179
Paul Brook1431b6a2009-06-05 15:52:04 +0100180static DeviceProperty *find_prop(DeviceState *dev, const char *name,
181 DevicePropType type)
Paul Brookaae94602009-05-14 22:35:06 +0100182{
183 DeviceProperty *prop;
184
185 for (prop = dev->props; prop; prop = prop->next) {
186 if (strcmp(prop->name, name) == 0) {
Paul Brook1431b6a2009-06-05 15:52:04 +0100187 assert (prop->type == type);
Paul Brookaae94602009-05-14 22:35:06 +0100188 return prop;
189 }
190 }
191 return NULL;
192}
193
194uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
195{
196 DeviceProperty *prop;
197
Paul Brook1431b6a2009-06-05 15:52:04 +0100198 prop = find_prop(dev, name, PROP_TYPE_INT);
199 if (!prop) {
Paul Brookaae94602009-05-14 22:35:06 +0100200 return def;
Paul Brook1431b6a2009-06-05 15:52:04 +0100201 }
Paul Brookaae94602009-05-14 22:35:06 +0100202
203 return prop->value.i;
204}
205
206void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
207{
208 DeviceProperty *prop;
209
Paul Brook1431b6a2009-06-05 15:52:04 +0100210 prop = find_prop(dev, name, PROP_TYPE_PTR);
Paul Brookaae94602009-05-14 22:35:06 +0100211 assert(prop);
212 return prop->value.ptr;
213}
214
Paul Brook1431b6a2009-06-05 15:52:04 +0100215DeviceState *qdev_get_prop_dev(DeviceState *dev, const char *name)
216{
217 DeviceProperty *prop;
218
219 prop = find_prop(dev, name, PROP_TYPE_DEV);
220 if (!prop) {
221 return NULL;
222 }
223 return prop->value.ptr;
224}
225
Paul Brookaae94602009-05-14 22:35:06 +0100226void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
227{
228 assert(dev->num_gpio_in == 0);
229 dev->num_gpio_in = n;
230 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
231}
232
233void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
234{
235 assert(dev->num_gpio_out == 0);
236 dev->num_gpio_out = n;
237 dev->gpio_out = pins;
238}
239
240qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
241{
242 assert(n >= 0 && n < dev->num_gpio_in);
243 return dev->gpio_in[n];
244}
245
246void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
247{
248 assert(n >= 0 && n < dev->num_gpio_out);
249 dev->gpio_out[n] = pin;
250}
251
Paul Brook9d07d752009-05-14 22:35:07 +0100252VLANClientState *qdev_get_vlan_client(DeviceState *dev,
Mark McLoughlincda90462009-05-18 13:13:16 +0100253 NetCanReceive *can_receive,
254 NetReceive *receive,
255 NetReceiveIOV *receive_iov,
Paul Brook9d07d752009-05-14 22:35:07 +0100256 NetCleanup *cleanup,
257 void *opaque)
258{
259 NICInfo *nd = dev->nd;
260 assert(nd);
Mark McLoughlincda90462009-05-18 13:13:16 +0100261 return qemu_new_vlan_client(nd->vlan, nd->model, nd->name, can_receive,
262 receive, receive_iov, cleanup, opaque);
Paul Brook9d07d752009-05-14 22:35:07 +0100263}
264
265
266void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
267{
268 memcpy(macaddr, dev->nd->macaddr, 6);
269}
270
Paul Brookaae94602009-05-14 22:35:06 +0100271static int next_block_unit[IF_COUNT];
272
273/* Get a block device. This should only be used for single-drive devices
274 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
275 appropriate bus. */
276BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
277{
278 int unit = next_block_unit[type]++;
279 int index;
280
281 index = drive_get_index(type, 0, unit);
282 if (index == -1) {
283 return NULL;
284 }
285 return drives_table[index].bdrv;
286}
Paul Brook4d6ae672009-05-14 22:35:06 +0100287
Paul Brook02e2da42009-05-23 00:05:19 +0100288BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
Paul Brook4d6ae672009-05-14 22:35:06 +0100289{
Paul Brook02e2da42009-05-23 00:05:19 +0100290 BusState *bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100291
Paul Brook02e2da42009-05-23 00:05:19 +0100292 LIST_FOREACH(bus, &dev->child_bus, sibling) {
Paul Brook4d6ae672009-05-14 22:35:06 +0100293 if (strcmp(name, bus->name) == 0) {
Paul Brook02e2da42009-05-23 00:05:19 +0100294 return bus;
Paul Brook4d6ae672009-05-14 22:35:06 +0100295 }
296 }
297 return NULL;
298}
299
Paul Brook6f68ecb2009-05-14 22:35:07 +0100300static int next_scsi_bus;
301
302/* Create a scsi bus, and attach devices to it. */
303/* TODO: Actually create a scsi bus for hotplug to use. */
304void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
305{
306 int bus = next_scsi_bus++;
307 int unit;
308 int index;
309
310 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
311 index = drive_get_index(IF_SCSI, bus, unit);
312 if (index == -1) {
313 continue;
314 }
315 attach(host, drives_table[index].bdrv, unit);
316 }
317}
Paul Brook02e2da42009-05-23 00:05:19 +0100318
Gerd Hoffmann10c4c982009-06-30 14:12:08 +0200319BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
Paul Brook02e2da42009-05-23 00:05:19 +0100320{
321 BusState *bus;
322
Gerd Hoffmann10c4c982009-06-30 14:12:08 +0200323 bus = qemu_mallocz(info->size);
324 bus->info = info;
Paul Brook02e2da42009-05-23 00:05:19 +0100325 bus->parent = parent;
326 bus->name = qemu_strdup(name);
327 LIST_INIT(&bus->children);
328 if (parent) {
329 LIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
330 }
331 return bus;
332}
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100333
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100334#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
335static void qbus_print(Monitor *mon, BusState *bus, int indent);
336
337static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
338{
339 DeviceProperty *prop;
340 BusState *child;
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +0200341 qdev_printf("dev: %s\n", dev->type->info->name);
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100342 indent += 2;
343 if (dev->num_gpio_in) {
344 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
345 }
346 if (dev->num_gpio_out) {
347 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
348 }
349 for (prop = dev->props; prop; prop = prop->next) {
350 switch (prop->type) {
351 case PROP_TYPE_INT:
352 qdev_printf("prop-int %s 0x%" PRIx64 "\n", prop->name,
353 prop->value.i);
354 break;
355 case PROP_TYPE_PTR:
356 qdev_printf("prop-ptr %s\n", prop->name);
357 break;
358 case PROP_TYPE_DEV:
359 qdev_printf("prop-dev %s %s\n", prop->name,
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +0200360 ((DeviceState *)prop->value.ptr)->type->info->name);
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100361 break;
362 default:
363 qdev_printf("prop-unknown%d %s\n", prop->type, prop->name);
364 break;
365 }
366 }
Gerd Hoffmann10c4c982009-06-30 14:12:08 +0200367 if (dev->parent_bus->info->print_dev)
368 dev->parent_bus->info->print_dev(mon, dev, indent);
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100369 LIST_FOREACH(child, &dev->child_bus, sibling) {
370 qbus_print(mon, child, indent);
371 }
372}
373
374static void qbus_print(Monitor *mon, BusState *bus, int indent)
375{
376 struct DeviceState *dev;
377
378 qdev_printf("bus: %s\n", bus->name);
379 indent += 2;
Gerd Hoffmann10c4c982009-06-30 14:12:08 +0200380 qdev_printf("type %s\n", bus->info->name);
Gerd Hoffmanncae49562009-06-05 15:53:17 +0100381 LIST_FOREACH(dev, &bus->children, sibling) {
382 qdev_print(mon, dev, indent);
383 }
384}
385#undef qdev_printf
386
387void do_info_qtree(Monitor *mon)
388{
389 if (main_system_bus)
390 qbus_print(mon, main_system_bus, 0);
391}