Basic qdev infrastructure.
[qemu/ar7.git] / hw / qdev.c
blobeaa30f430b8ee3c33404f3214f50e05677f6d155
1 /*
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
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. */
29 #include "qdev.h"
30 #include "sysemu.h"
32 struct DeviceProperty {
33 const char *name;
34 union {
35 int i;
36 void *ptr;
37 } value;
38 DeviceProperty *next;
41 struct DeviceType {
42 const char *name;
43 qdev_initfn init;
44 void *opaque;
45 int size;
46 DeviceType *next;
49 static DeviceType *device_type_list;
51 /* Register a new device type. */
52 DeviceType *qdev_register(const char *name, int size, qdev_initfn init,
53 void *opaque)
55 DeviceType *t;
57 assert(size >= sizeof(DeviceState));
59 t = qemu_mallocz(sizeof(DeviceType));
60 t->next = device_type_list;
61 device_type_list = t;
62 t->name = qemu_strdup(name);
63 t->size = size;
64 t->init = init;
65 t->opaque = opaque;
67 return t;
70 /* Create a new device. This only initializes the device state structure
71 and allows properties to be set. qdev_init should be called to
72 initialize the actual device emulation. */
73 DeviceState *qdev_create(void *bus, const char *name)
75 DeviceType *t;
76 DeviceState *dev;
78 for (t = device_type_list; t; t = t->next) {
79 if (strcmp(t->name, name) == 0) {
80 break;
83 if (!t) {
84 fprintf(stderr, "Unknown device '%s'\n", name);
85 exit(1);
88 dev = qemu_mallocz(t->size);
89 dev->name = name;
90 dev->type = t;
91 dev->bus = bus;
92 return dev;
95 /* Initialize a device. Device properties should be set before calling
96 this function. IRQs and MMIO regions should be connected/mapped after
97 calling this function. */
98 void qdev_init(DeviceState *dev)
100 dev->type->init(dev, dev->type->opaque);
103 static DeviceProperty *create_prop(DeviceState *dev, const char *name)
105 DeviceProperty *prop;
107 /* TODO: Check for duplicate properties. */
108 prop = qemu_mallocz(sizeof(*prop));
109 prop->name = qemu_strdup(name);
110 prop->next = dev->props;
111 dev->props = prop;
113 return prop;
116 void qdev_set_prop_int(DeviceState *dev, const char *name, int value)
118 DeviceProperty *prop;
120 prop = create_prop(dev, name);
121 prop->value.i = value;
124 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
126 DeviceProperty *prop;
128 prop = create_prop(dev, name);
129 prop->value.ptr = value;
133 qemu_irq qdev_get_irq_sink(DeviceState *dev, int n)
135 assert(n >= 0 && n < dev->num_irq_sink);
136 return dev->irq_sink[n];
139 /* Register device IRQ sinks. */
140 void qdev_init_irq_sink(DeviceState *dev, qemu_irq_handler handler, int nirq)
142 dev->num_irq_sink = nirq;
143 dev->irq_sink = qemu_allocate_irqs(handler, dev, nirq);
146 /* Get a character (serial) device interface. */
147 CharDriverState *qdev_init_chardev(DeviceState *dev)
149 static int next_serial;
150 static int next_virtconsole;
151 /* FIXME: This is a nasty hack that needs to go away. */
152 if (strncmp(dev->name, "virtio", 6) == 0) {
153 return virtcon_hds[next_virtconsole++];
154 } else {
155 return serial_hds[next_serial++];
159 void *qdev_get_bus(DeviceState *dev)
161 return dev->bus;
164 static DeviceProperty *find_prop(DeviceState *dev, const char *name)
166 DeviceProperty *prop;
168 for (prop = dev->props; prop; prop = prop->next) {
169 if (strcmp(prop->name, name) == 0) {
170 return prop;
173 return NULL;
176 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
178 DeviceProperty *prop;
180 prop = find_prop(dev, name);
181 if (!prop)
182 return def;
184 return prop->value.i;
187 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
189 DeviceProperty *prop;
191 prop = find_prop(dev, name);
192 assert(prop);
193 return prop->value.ptr;
196 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
198 assert(dev->num_gpio_in == 0);
199 dev->num_gpio_in = n;
200 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
203 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
205 assert(dev->num_gpio_out == 0);
206 dev->num_gpio_out = n;
207 dev->gpio_out = pins;
210 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
212 assert(n >= 0 && n < dev->num_gpio_in);
213 return dev->gpio_in[n];
216 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
218 assert(n >= 0 && n < dev->num_gpio_out);
219 dev->gpio_out[n] = pin;
222 static int next_block_unit[IF_COUNT];
224 /* Get a block device. This should only be used for single-drive devices
225 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
226 appropriate bus. */
227 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
229 int unit = next_block_unit[type]++;
230 int index;
232 index = drive_get_index(type, 0, unit);
233 if (index == -1) {
234 return NULL;
236 return drives_table[index].bdrv;