]> Git Repo - qemu.git/blame - hw/qdev.c
Fix elf loader range checking
[qemu.git] / hw / qdev.c
CommitLineData
aae9460e
PB
1/*
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
9d07d757 29#include "net.h"
aae9460e
PB
30#include "qdev.h"
31#include "sysemu.h"
32
33struct DeviceProperty {
34 const char *name;
35 union {
89a740e1 36 uint64_t i;
aae9460e
PB
37 void *ptr;
38 } value;
39 DeviceProperty *next;
40};
41
42struct DeviceType {
43 const char *name;
02e2da45 44 DeviceInfo *info;
aae9460e
PB
45 int size;
46 DeviceType *next;
47};
48
02e2da45
PB
49/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
50BusState *main_system_bus;
4d6ae674 51
aae9460e
PB
52static DeviceType *device_type_list;
53
54/* Register a new device type. */
02e2da45 55void qdev_register(const char *name, int size, DeviceInfo *info)
aae9460e
PB
56{
57 DeviceType *t;
58
59 assert(size >= sizeof(DeviceState));
60
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;
02e2da45 66 t->info = info;
aae9460e
PB
67}
68
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. */
02e2da45 72DeviceState *qdev_create(BusState *bus, const char *name)
aae9460e
PB
73{
74 DeviceType *t;
75 DeviceState *dev;
76
77 for (t = device_type_list; t; t = t->next) {
78 if (strcmp(t->name, name) == 0) {
79 break;
80 }
81 }
82 if (!t) {
02e2da45 83 hw_error("Unknown device '%s'\n", name);
aae9460e
PB
84 }
85
86 dev = qemu_mallocz(t->size);
aae9460e 87 dev->type = t;
02e2da45
PB
88
89 if (!bus) {
90 /* ???: This assumes system busses have no additional state. */
91 if (!main_system_bus) {
92 main_system_bus = qbus_create(BUS_TYPE_SYSTEM, sizeof(BusState),
93 NULL, "main-system-bus");
94 }
95 bus = main_system_bus;
96 }
97 if (t->info->bus_type != bus->type) {
98 /* TODO: Print bus type names. */
99 hw_error("Device '%s' on wrong bus type (%d/%d)", name,
100 t->info->bus_type, bus->type);
101 }
102 dev->parent_bus = bus;
103 LIST_INSERT_HEAD(&bus->children, dev, sibling);
aae9460e
PB
104 return dev;
105}
106
107/* Initialize a device. Device properties should be set before calling
108 this function. IRQs and MMIO regions should be connected/mapped after
109 calling this function. */
110void qdev_init(DeviceState *dev)
111{
02e2da45
PB
112 dev->type->info->init(dev, dev->type->info);
113}
114
115/* Unlink device from bus and free the structure. */
116void qdev_free(DeviceState *dev)
117{
118 LIST_REMOVE(dev, sibling);
119 free(dev);
aae9460e
PB
120}
121
122static DeviceProperty *create_prop(DeviceState *dev, const char *name)
123{
124 DeviceProperty *prop;
125
126 /* TODO: Check for duplicate properties. */
127 prop = qemu_mallocz(sizeof(*prop));
128 prop->name = qemu_strdup(name);
129 prop->next = dev->props;
130 dev->props = prop;
131
132 return prop;
133}
134
89a740e1 135void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value)
aae9460e
PB
136{
137 DeviceProperty *prop;
138
139 prop = create_prop(dev, name);
140 prop->value.i = value;
141}
142
143void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
144{
145 DeviceProperty *prop;
146
147 prop = create_prop(dev, name);
148 prop->value.ptr = value;
149}
150
9d07d757
PB
151void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
152{
153 assert(!dev->nd);
154 dev->nd = nd;
155}
156
aae9460e 157
aae9460e
PB
158/* Get a character (serial) device interface. */
159CharDriverState *qdev_init_chardev(DeviceState *dev)
160{
161 static int next_serial;
162 static int next_virtconsole;
163 /* FIXME: This is a nasty hack that needs to go away. */
aca312af 164 if (strncmp(dev->type->name, "virtio", 6) == 0) {
aae9460e
PB
165 return virtcon_hds[next_virtconsole++];
166 } else {
167 return serial_hds[next_serial++];
168 }
169}
170
02e2da45 171BusState *qdev_get_parent_bus(DeviceState *dev)
aae9460e 172{
02e2da45 173 return dev->parent_bus;
aae9460e
PB
174}
175
176static DeviceProperty *find_prop(DeviceState *dev, const char *name)
177{
178 DeviceProperty *prop;
179
180 for (prop = dev->props; prop; prop = prop->next) {
181 if (strcmp(prop->name, name) == 0) {
182 return prop;
183 }
184 }
185 return NULL;
186}
187
188uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
189{
190 DeviceProperty *prop;
191
192 prop = find_prop(dev, name);
193 if (!prop)
194 return def;
195
196 return prop->value.i;
197}
198
199void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
200{
201 DeviceProperty *prop;
202
203 prop = find_prop(dev, name);
204 assert(prop);
205 return prop->value.ptr;
206}
207
208void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
209{
210 assert(dev->num_gpio_in == 0);
211 dev->num_gpio_in = n;
212 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
213}
214
215void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
216{
217 assert(dev->num_gpio_out == 0);
218 dev->num_gpio_out = n;
219 dev->gpio_out = pins;
220}
221
222qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
223{
224 assert(n >= 0 && n < dev->num_gpio_in);
225 return dev->gpio_in[n];
226}
227
228void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
229{
230 assert(n >= 0 && n < dev->num_gpio_out);
231 dev->gpio_out[n] = pin;
232}
233
9d07d757
PB
234VLANClientState *qdev_get_vlan_client(DeviceState *dev,
235 IOReadHandler *fd_read,
236 IOCanRWHandler *fd_can_read,
237 NetCleanup *cleanup,
238 void *opaque)
239{
240 NICInfo *nd = dev->nd;
241 assert(nd);
242 return qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
243 fd_read, fd_can_read, cleanup, opaque);
244}
245
246
247void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
248{
249 memcpy(macaddr, dev->nd->macaddr, 6);
250}
251
aae9460e
PB
252static int next_block_unit[IF_COUNT];
253
254/* Get a block device. This should only be used for single-drive devices
255 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
256 appropriate bus. */
257BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
258{
259 int unit = next_block_unit[type]++;
260 int index;
261
262 index = drive_get_index(type, 0, unit);
263 if (index == -1) {
264 return NULL;
265 }
266 return drives_table[index].bdrv;
267}
4d6ae674 268
02e2da45 269BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
4d6ae674 270{
02e2da45 271 BusState *bus;
4d6ae674 272
02e2da45 273 LIST_FOREACH(bus, &dev->child_bus, sibling) {
4d6ae674 274 if (strcmp(name, bus->name) == 0) {
02e2da45 275 return bus;
4d6ae674
PB
276 }
277 }
278 return NULL;
279}
280
6f68ecb2
PB
281static int next_scsi_bus;
282
283/* Create a scsi bus, and attach devices to it. */
284/* TODO: Actually create a scsi bus for hotplug to use. */
285void scsi_bus_new(DeviceState *host, SCSIAttachFn attach)
286{
287 int bus = next_scsi_bus++;
288 int unit;
289 int index;
290
291 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
292 index = drive_get_index(IF_SCSI, bus, unit);
293 if (index == -1) {
294 continue;
295 }
296 attach(host, drives_table[index].bdrv, unit);
297 }
298}
02e2da45
PB
299
300BusState *qbus_create(BusType type, size_t size,
301 DeviceState *parent, const char *name)
302{
303 BusState *bus;
304
305 bus = qemu_mallocz(size);
306 bus->type = type;
307 bus->parent = parent;
308 bus->name = qemu_strdup(name);
309 LIST_INIT(&bus->children);
310 if (parent) {
311 LIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
312 }
313 return bus;
314}
This page took 0.066587 seconds and 4 git commands to generate.