hostmem: Ignore ENOSYS while setting MPOL_DEFAULT
[qemu/ar7.git] / hw / core / qdev-properties-system.c
blob921e799dbb98cbb73212537dd48489b78d8fd129
1 /*
2 * qdev property parsing and global properties
3 * (parts specific for qemu-system-*)
5 * This file is based on code from hw/qdev-properties.c from
6 * commit 074a86fccd185616469dfcdc0e157f438aebba18,
7 * Copyright (c) Gerd Hoffmann <kraxel@redhat.com> and other contributors.
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "net/net.h"
14 #include "hw/qdev.h"
15 #include "qapi/qmp/qerror.h"
16 #include "sysemu/block-backend.h"
17 #include "sysemu/blockdev.h"
18 #include "hw/block/block.h"
19 #include "net/hub.h"
20 #include "qapi/visitor.h"
21 #include "sysemu/char.h"
22 #include "sysemu/iothread.h"
24 static void get_pointer(Object *obj, Visitor *v, Property *prop,
25 char *(*print)(void *ptr),
26 const char *name, Error **errp)
28 DeviceState *dev = DEVICE(obj);
29 void **ptr = qdev_get_prop_ptr(dev, prop);
30 char *p;
32 p = *ptr ? print(*ptr) : g_strdup("");
33 visit_type_str(v, &p, name, errp);
34 g_free(p);
37 static void set_pointer(Object *obj, Visitor *v, Property *prop,
38 void (*parse)(DeviceState *dev, const char *str,
39 void **ptr, const char *propname,
40 Error **errp),
41 const char *name, Error **errp)
43 DeviceState *dev = DEVICE(obj);
44 Error *local_err = NULL;
45 void **ptr = qdev_get_prop_ptr(dev, prop);
46 char *str;
48 if (dev->realized) {
49 qdev_prop_set_after_realize(dev, name, errp);
50 return;
53 visit_type_str(v, &str, name, &local_err);
54 if (local_err) {
55 error_propagate(errp, local_err);
56 return;
58 if (!*str) {
59 g_free(str);
60 *ptr = NULL;
61 return;
63 parse(dev, str, ptr, prop->name, errp);
64 g_free(str);
67 /* --- drive --- */
69 static void parse_drive(DeviceState *dev, const char *str, void **ptr,
70 const char *propname, Error **errp)
72 BlockBackend *blk;
74 blk = blk_by_name(str);
75 if (!blk) {
76 error_setg(errp, "Property '%s.%s' can't find value '%s'",
77 object_get_typename(OBJECT(dev)), propname, str);
78 return;
80 if (blk_attach_dev(blk, dev) < 0) {
81 DriveInfo *dinfo = blk_legacy_dinfo(blk);
83 if (dinfo->type != IF_NONE) {
84 error_setg(errp, "Drive '%s' is already in use because "
85 "it has been automatically connected to another "
86 "device (did you need 'if=none' in the drive options?)",
87 str);
88 } else {
89 error_setg(errp, "Drive '%s' is already in use by another device",
90 str);
92 return;
94 *ptr = blk;
97 static void release_drive(Object *obj, const char *name, void *opaque)
99 DeviceState *dev = DEVICE(obj);
100 Property *prop = opaque;
101 BlockBackend **ptr = qdev_get_prop_ptr(dev, prop);
103 if (*ptr) {
104 blk_detach_dev(*ptr, dev);
105 blockdev_auto_del(*ptr);
109 static char *print_drive(void *ptr)
111 return g_strdup(blk_name(ptr));
114 static void get_drive(Object *obj, Visitor *v, void *opaque,
115 const char *name, Error **errp)
117 get_pointer(obj, v, opaque, print_drive, name, errp);
120 static void set_drive(Object *obj, Visitor *v, void *opaque,
121 const char *name, Error **errp)
123 set_pointer(obj, v, opaque, parse_drive, name, errp);
126 PropertyInfo qdev_prop_drive = {
127 .name = "str",
128 .description = "ID of a drive to use as a backend",
129 .get = get_drive,
130 .set = set_drive,
131 .release = release_drive,
134 /* --- character device --- */
136 static void parse_chr(DeviceState *dev, const char *str, void **ptr,
137 const char *propname, Error **errp)
139 CharDriverState *chr = qemu_chr_find(str);
140 if (chr == NULL) {
141 error_setg(errp, "Property '%s.%s' can't find value '%s'",
142 object_get_typename(OBJECT(dev)), propname, str);
143 return;
145 if (qemu_chr_fe_claim(chr) != 0) {
146 error_setg(errp, "Property '%s.%s' can't take value '%s', it's in use",
147 object_get_typename(OBJECT(dev)), propname, str);
148 return;
150 *ptr = chr;
153 static void release_chr(Object *obj, const char *name, void *opaque)
155 DeviceState *dev = DEVICE(obj);
156 Property *prop = opaque;
157 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
158 CharDriverState *chr = *ptr;
160 if (chr) {
161 qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL);
162 qemu_chr_fe_release(chr);
167 static char *print_chr(void *ptr)
169 CharDriverState *chr = ptr;
170 const char *val = chr->label ? chr->label : "";
172 return g_strdup(val);
175 static void get_chr(Object *obj, Visitor *v, void *opaque,
176 const char *name, Error **errp)
178 get_pointer(obj, v, opaque, print_chr, name, errp);
181 static void set_chr(Object *obj, Visitor *v, void *opaque,
182 const char *name, Error **errp)
184 set_pointer(obj, v, opaque, parse_chr, name, errp);
187 PropertyInfo qdev_prop_chr = {
188 .name = "str",
189 .description = "ID of a chardev to use as a backend",
190 .get = get_chr,
191 .set = set_chr,
192 .release = release_chr,
195 /* --- netdev device --- */
196 static void get_netdev(Object *obj, Visitor *v, void *opaque,
197 const char *name, Error **errp)
199 DeviceState *dev = DEVICE(obj);
200 Property *prop = opaque;
201 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
202 char *p = g_strdup(peers_ptr->ncs[0] ? peers_ptr->ncs[0]->name : "");
204 visit_type_str(v, &p, name, errp);
205 g_free(p);
208 static void set_netdev(Object *obj, Visitor *v, void *opaque,
209 const char *name, Error **errp)
211 DeviceState *dev = DEVICE(obj);
212 Property *prop = opaque;
213 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
214 NetClientState **ncs = peers_ptr->ncs;
215 NetClientState *peers[MAX_QUEUE_NUM];
216 Error *local_err = NULL;
217 int queues, err = 0, i = 0;
218 char *str;
220 if (dev->realized) {
221 qdev_prop_set_after_realize(dev, name, errp);
222 return;
225 visit_type_str(v, &str, name, &local_err);
226 if (local_err) {
227 error_propagate(errp, local_err);
228 return;
231 queues = qemu_find_net_clients_except(str, peers,
232 NET_CLIENT_OPTIONS_KIND_NIC,
233 MAX_QUEUE_NUM);
234 if (queues == 0) {
235 err = -ENOENT;
236 goto out;
239 if (queues > MAX_QUEUE_NUM) {
240 error_setg(errp, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
241 str, queues, MAX_QUEUE_NUM);
242 goto out;
245 for (i = 0; i < queues; i++) {
246 if (peers[i] == NULL) {
247 err = -ENOENT;
248 goto out;
251 if (peers[i]->peer) {
252 err = -EEXIST;
253 goto out;
256 if (ncs[i]) {
257 err = -EINVAL;
258 goto out;
261 ncs[i] = peers[i];
262 ncs[i]->queue_index = i;
265 peers_ptr->queues = queues;
267 out:
268 error_set_from_qdev_prop_error(errp, err, dev, prop, str);
269 g_free(str);
272 PropertyInfo qdev_prop_netdev = {
273 .name = "str",
274 .description = "ID of a netdev to use as a backend",
275 .get = get_netdev,
276 .set = set_netdev,
279 /* --- vlan --- */
281 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
283 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
285 if (*ptr) {
286 int id;
287 if (!net_hub_id_for_client(*ptr, &id)) {
288 return snprintf(dest, len, "%d", id);
292 return snprintf(dest, len, "<null>");
295 static void get_vlan(Object *obj, Visitor *v, void *opaque,
296 const char *name, Error **errp)
298 DeviceState *dev = DEVICE(obj);
299 Property *prop = opaque;
300 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
301 int32_t id = -1;
303 if (*ptr) {
304 int hub_id;
305 if (!net_hub_id_for_client(*ptr, &hub_id)) {
306 id = hub_id;
310 visit_type_int32(v, &id, name, errp);
313 static void set_vlan(Object *obj, Visitor *v, void *opaque,
314 const char *name, Error **errp)
316 DeviceState *dev = DEVICE(obj);
317 Property *prop = opaque;
318 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
319 NetClientState **ptr = &peers_ptr->ncs[0];
320 Error *local_err = NULL;
321 int32_t id;
322 NetClientState *hubport;
324 if (dev->realized) {
325 qdev_prop_set_after_realize(dev, name, errp);
326 return;
329 visit_type_int32(v, &id, name, &local_err);
330 if (local_err) {
331 error_propagate(errp, local_err);
332 return;
334 if (id == -1) {
335 *ptr = NULL;
336 return;
338 if (*ptr) {
339 error_set_from_qdev_prop_error(errp, -EINVAL, dev, prop, name);
340 return;
343 hubport = net_hub_port_find(id);
344 if (!hubport) {
345 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
346 name, prop->info->name);
347 return;
349 *ptr = hubport;
352 PropertyInfo qdev_prop_vlan = {
353 .name = "int32",
354 .description = "Integer VLAN id to connect to",
355 .print = print_vlan,
356 .get = get_vlan,
357 .set = set_vlan,
360 void qdev_prop_set_drive(DeviceState *dev, const char *name,
361 BlockBackend *value, Error **errp)
363 object_property_set_str(OBJECT(dev), value ? blk_name(value) : "",
364 name, errp);
367 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name,
368 BlockBackend *value)
370 Error *err = NULL;
372 qdev_prop_set_drive(dev, name, value, &err);
373 if (err) {
374 error_report_err(err);
375 exit(1);
379 void qdev_prop_set_chr(DeviceState *dev, const char *name,
380 CharDriverState *value)
382 assert(!value || value->label);
383 object_property_set_str(OBJECT(dev),
384 value ? value->label : "", name, &error_abort);
387 void qdev_prop_set_netdev(DeviceState *dev, const char *name,
388 NetClientState *value)
390 assert(!value || value->name);
391 object_property_set_str(OBJECT(dev),
392 value ? value->name : "", name, &error_abort);
395 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
397 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
398 if (nd->netdev) {
399 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
401 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
402 object_property_find(OBJECT(dev), "vectors", NULL)) {
403 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
405 nd->instantiated = 1;
408 static int qdev_add_one_global(void *opaque, QemuOpts *opts, Error **errp)
410 GlobalProperty *g;
412 g = g_malloc0(sizeof(*g));
413 g->driver = qemu_opt_get(opts, "driver");
414 g->property = qemu_opt_get(opts, "property");
415 g->value = qemu_opt_get(opts, "value");
416 g->user_provided = true;
417 qdev_prop_register_global(g);
418 return 0;
421 void qemu_add_globals(void)
423 qemu_opts_foreach(qemu_find_opts("global"),
424 qdev_add_one_global, NULL, NULL);