Flush icache after dma operations for ia64
[qemu-kvm/fedora.git] / hw / qdev.c
blobd945ff9538869407783a4b973d30f073be8fef5e
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 "net.h"
30 #include "qdev.h"
31 #include "sysemu.h"
33 struct DeviceProperty {
34 const char *name;
35 union {
36 uint64_t i;
37 void *ptr;
38 } value;
39 DeviceProperty *next;
42 struct DeviceType {
43 const char *name;
44 DeviceInfo *info;
45 int size;
46 DeviceType *next;
49 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
50 BusState *main_system_bus;
52 static DeviceType *device_type_list;
54 /* Register a new device type. */
55 void qdev_register(const char *name, int size, DeviceInfo *info)
57 DeviceType *t;
59 assert(size >= sizeof(DeviceState));
61 t = qemu_mallocz(sizeof(DeviceType));
62 t->next = device_type_list;
63 device_type_list = t;
64 t->name = qemu_strdup(name);
65 t->size = size;
66 t->info = info;
69 /* Create a new device. This only initializes the device state structure
70 and allows properties to be set. qdev_init should be called to
71 initialize the actual device emulation. */
72 DeviceState *qdev_create(BusState *bus, const char *name)
74 DeviceType *t;
75 DeviceState *dev;
77 for (t = device_type_list; t; t = t->next) {
78 if (strcmp(t->name, name) == 0) {
79 break;
82 if (!t) {
83 hw_error("Unknown device '%s'\n", name);
86 dev = qemu_mallocz(t->size);
87 dev->name = name;
88 dev->type = t;
90 if (!bus) {
91 /* ???: This assumes system busses have no additional state. */
92 if (!main_system_bus) {
93 main_system_bus = qbus_create(BUS_TYPE_SYSTEM, sizeof(BusState),
94 NULL, "main-system-bus");
96 bus = main_system_bus;
98 if (t->info->bus_type != bus->type) {
99 /* TODO: Print bus type names. */
100 hw_error("Device '%s' on wrong bus type (%d/%d)", name,
101 t->info->bus_type, bus->type);
103 dev->parent_bus = bus;
104 LIST_INSERT_HEAD(&bus->children, dev, sibling);
105 return dev;
108 /* Initialize a device. Device properties should be set before calling
109 this function. IRQs and MMIO regions should be connected/mapped after
110 calling this function. */
111 void qdev_init(DeviceState *dev)
113 dev->type->info->init(dev, dev->type->info);
116 /* Unlink device from bus and free the structure. */
117 void qdev_free(DeviceState *dev)
119 LIST_REMOVE(dev, sibling);
120 free(dev);
123 static DeviceProperty *create_prop(DeviceState *dev, const char *name)
125 DeviceProperty *prop;
127 /* TODO: Check for duplicate properties. */
128 prop = qemu_mallocz(sizeof(*prop));
129 prop->name = qemu_strdup(name);
130 prop->next = dev->props;
131 dev->props = prop;
133 return prop;
136 void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value)
138 DeviceProperty *prop;
140 prop = create_prop(dev, name);
141 prop->value.i = value;
144 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
146 DeviceProperty *prop;
148 prop = create_prop(dev, name);
149 prop->value.ptr = value;
152 void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
154 assert(!dev->nd);
155 dev->nd = nd;
159 /* Get a character (serial) device interface. */
160 CharDriverState *qdev_init_chardev(DeviceState *dev)
162 static int next_serial;
163 static int next_virtconsole;
164 /* FIXME: This is a nasty hack that needs to go away. */
165 if (strncmp(dev->name, "virtio", 6) == 0) {
166 return virtcon_hds[next_virtconsole++];
167 } else {
168 return serial_hds[next_serial++];
172 BusState *qdev_get_parent_bus(DeviceState *dev)
174 return dev->parent_bus;
177 static DeviceProperty *find_prop(DeviceState *dev, const char *name)
179 DeviceProperty *prop;
181 for (prop = dev->props; prop; prop = prop->next) {
182 if (strcmp(prop->name, name) == 0) {
183 return prop;
186 return NULL;
189 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
191 DeviceProperty *prop;
193 prop = find_prop(dev, name);
194 if (!prop)
195 return def;
197 return prop->value.i;
200 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
202 DeviceProperty *prop;
204 prop = find_prop(dev, name);
205 assert(prop);
206 return prop->value.ptr;
209 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
211 assert(dev->num_gpio_in == 0);
212 dev->num_gpio_in = n;
213 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
216 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
218 assert(dev->num_gpio_out == 0);
219 dev->num_gpio_out = n;
220 dev->gpio_out = pins;
223 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
225 assert(n >= 0 && n < dev->num_gpio_in);
226 return dev->gpio_in[n];
229 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
231 assert(n >= 0 && n < dev->num_gpio_out);
232 dev->gpio_out[n] = pin;
235 VLANClientState *qdev_get_vlan_client(DeviceState *dev,
236 IOReadHandler *fd_read,
237 IOCanRWHandler *fd_can_read,
238 NetCleanup *cleanup,
239 void *opaque)
241 NICInfo *nd = dev->nd;
242 assert(nd);
243 return qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
244 fd_read, fd_can_read, cleanup, opaque);
248 void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
250 memcpy(macaddr, dev->nd->macaddr, 6);
253 static int next_block_unit[IF_COUNT];
255 /* Get a block device. This should only be used for single-drive devices
256 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
257 appropriate bus. */
258 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
260 int unit = next_block_unit[type]++;
261 int index;
263 index = drive_get_index(type, 0, unit);
264 if (index == -1) {
265 return NULL;
267 return drives_table[index].bdrv;
270 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
272 BusState *bus;
274 LIST_FOREACH(bus, &dev->child_bus, sibling) {
275 if (strcmp(name, bus->name) == 0) {
276 return bus;
279 return NULL;
282 static int next_scsi_bus;
284 /* Create a scsi bus, and attach devices to it. */
285 /* TODO: Actually create a scsi bus for hotplug to use. */
286 void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
288 int bus = next_scsi_bus++;
289 int unit;
290 int index;
292 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
293 index = drive_get_index(IF_SCSI, bus, unit);
294 if (index == -1) {
295 continue;
297 attach(host, drives_table[index].bdrv, unit);
301 BusState *qbus_create(BusType type, size_t size,
302 DeviceState *parent, const char *name)
304 BusState *bus;
306 bus = qemu_mallocz(size);
307 bus->type = type;
308 bus->parent = parent;
309 bus->name = qemu_strdup(name);
310 LIST_INIT(&bus->children);
311 if (parent) {
312 LIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
314 return bus;