softmmu/memory_mapping: optimize for RamDiscardManager sections
[qemu.git] / hw / core / qdev-properties-system.c
blobe71f5d64d14b8c68cbebc177d3e4b626b581b052
1 /*
2 * qdev property parsing
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 "qemu/osdep.h"
14 #include "hw/qdev-properties.h"
15 #include "hw/qdev-properties-system.h"
16 #include "qapi/error.h"
17 #include "qapi/visitor.h"
18 #include "qapi/qapi-types-block.h"
19 #include "qapi/qapi-types-machine.h"
20 #include "qapi/qapi-types-migration.h"
21 #include "qapi/qmp/qerror.h"
22 #include "qemu/ctype.h"
23 #include "qemu/cutils.h"
24 #include "qemu/units.h"
25 #include "qemu/uuid.h"
26 #include "qemu/error-report.h"
27 #include "qdev-prop-internal.h"
29 #include "audio/audio.h"
30 #include "chardev/char-fe.h"
31 #include "sysemu/block-backend.h"
32 #include "sysemu/blockdev.h"
33 #include "net/net.h"
34 #include "hw/pci/pci.h"
35 #include "util/block-helpers.h"
37 static bool check_prop_still_unset(Object *obj, const char *name,
38 const void *old_val, const char *new_val,
39 bool allow_override, Error **errp)
41 const GlobalProperty *prop = qdev_find_global_prop(obj, name);
43 if (!old_val || (!prop && allow_override)) {
44 return true;
47 if (prop) {
48 error_setg(errp, "-global %s.%s=... conflicts with %s=%s",
49 prop->driver, prop->property, name, new_val);
50 } else {
51 /* Error message is vague, but a better one would be hard */
52 error_setg(errp, "%s=%s conflicts, and override is not implemented",
53 name, new_val);
55 return false;
59 /* --- drive --- */
61 static void get_drive(Object *obj, Visitor *v, const char *name, void *opaque,
62 Error **errp)
64 Property *prop = opaque;
65 void **ptr = object_field_prop_ptr(obj, prop);
66 const char *value;
67 char *p;
69 if (*ptr) {
70 value = blk_name(*ptr);
71 if (!*value) {
72 BlockDriverState *bs = blk_bs(*ptr);
73 if (bs) {
74 value = bdrv_get_node_name(bs);
77 } else {
78 value = "";
81 p = g_strdup(value);
82 visit_type_str(v, name, &p, errp);
83 g_free(p);
86 static void set_drive_helper(Object *obj, Visitor *v, const char *name,
87 void *opaque, bool iothread, Error **errp)
89 DeviceState *dev = DEVICE(obj);
90 Property *prop = opaque;
91 void **ptr = object_field_prop_ptr(obj, prop);
92 char *str;
93 BlockBackend *blk;
94 bool blk_created = false;
95 int ret;
96 BlockDriverState *bs;
97 AioContext *ctx;
99 if (!visit_type_str(v, name, &str, errp)) {
100 return;
103 if (!check_prop_still_unset(obj, name, *ptr, str, true, errp)) {
104 return;
107 if (*ptr) {
108 /* BlockBackend alread exists. So, we want to change attached node */
109 blk = *ptr;
110 ctx = blk_get_aio_context(blk);
111 bs = bdrv_lookup_bs(NULL, str, errp);
112 if (!bs) {
113 return;
116 if (ctx != bdrv_get_aio_context(bs)) {
117 error_setg(errp, "Different aio context is not supported for new "
118 "node");
121 aio_context_acquire(ctx);
122 blk_replace_bs(blk, bs, errp);
123 aio_context_release(ctx);
124 return;
127 if (!*str) {
128 g_free(str);
129 *ptr = NULL;
130 return;
133 blk = blk_by_name(str);
134 if (!blk) {
135 bs = bdrv_lookup_bs(NULL, str, NULL);
136 if (bs) {
138 * If the device supports iothreads, it will make sure to move the
139 * block node to the right AioContext if necessary (or fail if this
140 * isn't possible because of other users). Devices that are not
141 * aware of iothreads require their BlockBackends to be in the main
142 * AioContext.
144 ctx = iothread ? bdrv_get_aio_context(bs) : qemu_get_aio_context();
145 blk = blk_new(ctx, 0, BLK_PERM_ALL);
146 blk_created = true;
148 ret = blk_insert_bs(blk, bs, errp);
149 if (ret < 0) {
150 goto fail;
154 if (!blk) {
155 error_setg(errp, "Property '%s.%s' can't find value '%s'",
156 object_get_typename(OBJECT(dev)), name, str);
157 goto fail;
159 if (blk_attach_dev(blk, dev) < 0) {
160 DriveInfo *dinfo = blk_legacy_dinfo(blk);
162 if (dinfo && dinfo->type != IF_NONE) {
163 error_setg(errp, "Drive '%s' is already in use because "
164 "it has been automatically connected to another "
165 "device (did you need 'if=none' in the drive options?)",
166 str);
167 } else {
168 error_setg(errp, "Drive '%s' is already in use by another device",
169 str);
171 goto fail;
174 *ptr = blk;
176 fail:
177 if (blk_created) {
178 /* If we need to keep a reference, blk_attach_dev() took it */
179 blk_unref(blk);
182 g_free(str);
185 static void set_drive(Object *obj, Visitor *v, const char *name, void *opaque,
186 Error **errp)
188 set_drive_helper(obj, v, name, opaque, false, errp);
191 static void set_drive_iothread(Object *obj, Visitor *v, const char *name,
192 void *opaque, Error **errp)
194 set_drive_helper(obj, v, name, opaque, true, errp);
197 static void release_drive(Object *obj, const char *name, void *opaque)
199 DeviceState *dev = DEVICE(obj);
200 Property *prop = opaque;
201 BlockBackend **ptr = object_field_prop_ptr(obj, prop);
203 if (*ptr) {
204 AioContext *ctx = blk_get_aio_context(*ptr);
206 aio_context_acquire(ctx);
207 blockdev_auto_del(*ptr);
208 blk_detach_dev(*ptr, dev);
209 aio_context_release(ctx);
213 const PropertyInfo qdev_prop_drive = {
214 .name = "str",
215 .description = "Node name or ID of a block device to use as a backend",
216 .realized_set_allowed = true,
217 .get = get_drive,
218 .set = set_drive,
219 .release = release_drive,
222 const PropertyInfo qdev_prop_drive_iothread = {
223 .name = "str",
224 .description = "Node name or ID of a block device to use as a backend",
225 .realized_set_allowed = true,
226 .get = get_drive,
227 .set = set_drive_iothread,
228 .release = release_drive,
231 /* --- character device --- */
233 static void get_chr(Object *obj, Visitor *v, const char *name, void *opaque,
234 Error **errp)
236 CharBackend *be = object_field_prop_ptr(obj, opaque);
237 char *p;
239 p = g_strdup(be->chr && be->chr->label ? be->chr->label : "");
240 visit_type_str(v, name, &p, errp);
241 g_free(p);
244 static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque,
245 Error **errp)
247 Property *prop = opaque;
248 CharBackend *be = object_field_prop_ptr(obj, prop);
249 Chardev *s;
250 char *str;
252 if (!visit_type_str(v, name, &str, errp)) {
253 return;
257 * TODO Should this really be an error? If no, the old value
258 * needs to be released before we store the new one.
260 if (!check_prop_still_unset(obj, name, be->chr, str, false, errp)) {
261 return;
264 if (!*str) {
265 g_free(str);
266 be->chr = NULL;
267 return;
270 s = qemu_chr_find(str);
271 if (s == NULL) {
272 error_setg(errp, "Property '%s.%s' can't find value '%s'",
273 object_get_typename(obj), name, str);
274 } else if (!qemu_chr_fe_init(be, s, errp)) {
275 error_prepend(errp, "Property '%s.%s' can't take value '%s': ",
276 object_get_typename(obj), name, str);
278 g_free(str);
281 static void release_chr(Object *obj, const char *name, void *opaque)
283 Property *prop = opaque;
284 CharBackend *be = object_field_prop_ptr(obj, prop);
286 qemu_chr_fe_deinit(be, false);
289 const PropertyInfo qdev_prop_chr = {
290 .name = "str",
291 .description = "ID of a chardev to use as a backend",
292 .get = get_chr,
293 .set = set_chr,
294 .release = release_chr,
297 /* --- mac address --- */
300 * accepted syntax versions:
301 * 01:02:03:04:05:06
302 * 01-02-03-04-05-06
304 static void get_mac(Object *obj, Visitor *v, const char *name, void *opaque,
305 Error **errp)
307 Property *prop = opaque;
308 MACAddr *mac = object_field_prop_ptr(obj, prop);
309 char buffer[2 * 6 + 5 + 1];
310 char *p = buffer;
312 snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x",
313 mac->a[0], mac->a[1], mac->a[2],
314 mac->a[3], mac->a[4], mac->a[5]);
316 visit_type_str(v, name, &p, errp);
319 static void set_mac(Object *obj, Visitor *v, const char *name, void *opaque,
320 Error **errp)
322 Property *prop = opaque;
323 MACAddr *mac = object_field_prop_ptr(obj, prop);
324 int i, pos;
325 char *str;
326 const char *p;
328 if (!visit_type_str(v, name, &str, errp)) {
329 return;
332 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
333 long val;
335 if (!qemu_isxdigit(str[pos])) {
336 goto inval;
338 if (!qemu_isxdigit(str[pos + 1])) {
339 goto inval;
341 if (i == 5) {
342 if (str[pos + 2] != '\0') {
343 goto inval;
345 } else {
346 if (str[pos + 2] != ':' && str[pos + 2] != '-') {
347 goto inval;
350 if (qemu_strtol(str + pos, &p, 16, &val) < 0 || val > 0xff) {
351 goto inval;
353 mac->a[i] = val;
355 g_free(str);
356 return;
358 inval:
359 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
360 g_free(str);
363 const PropertyInfo qdev_prop_macaddr = {
364 .name = "str",
365 .description = "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56",
366 .get = get_mac,
367 .set = set_mac,
370 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
371 const uint8_t *value)
373 char str[2 * 6 + 5 + 1];
374 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
375 value[0], value[1], value[2], value[3], value[4], value[5]);
377 object_property_set_str(OBJECT(dev), name, str, &error_abort);
380 /* --- netdev device --- */
381 static void get_netdev(Object *obj, Visitor *v, const char *name,
382 void *opaque, Error **errp)
384 Property *prop = opaque;
385 NICPeers *peers_ptr = object_field_prop_ptr(obj, prop);
386 char *p = g_strdup(peers_ptr->ncs[0] ? peers_ptr->ncs[0]->name : "");
388 visit_type_str(v, name, &p, errp);
389 g_free(p);
392 static void set_netdev(Object *obj, Visitor *v, const char *name,
393 void *opaque, Error **errp)
395 Property *prop = opaque;
396 NICPeers *peers_ptr = object_field_prop_ptr(obj, prop);
397 NetClientState **ncs = peers_ptr->ncs;
398 NetClientState *peers[MAX_QUEUE_NUM];
399 int queues, err = 0, i = 0;
400 char *str;
402 if (!visit_type_str(v, name, &str, errp)) {
403 return;
406 queues = qemu_find_net_clients_except(str, peers,
407 NET_CLIENT_DRIVER_NIC,
408 MAX_QUEUE_NUM);
409 if (queues == 0) {
410 err = -ENOENT;
411 goto out;
414 if (queues > MAX_QUEUE_NUM) {
415 error_setg(errp, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
416 str, queues, MAX_QUEUE_NUM);
417 goto out;
420 for (i = 0; i < queues; i++) {
421 if (peers[i]->peer) {
422 err = -EEXIST;
423 goto out;
427 * TODO Should this really be an error? If no, the old value
428 * needs to be released before we store the new one.
430 if (!check_prop_still_unset(obj, name, ncs[i], str, false, errp)) {
431 goto out;
434 ncs[i] = peers[i];
435 ncs[i]->queue_index = i;
438 peers_ptr->queues = queues;
440 out:
441 error_set_from_qdev_prop_error(errp, err, obj, name, str);
442 g_free(str);
445 const PropertyInfo qdev_prop_netdev = {
446 .name = "str",
447 .description = "ID of a netdev to use as a backend",
448 .get = get_netdev,
449 .set = set_netdev,
453 /* --- audiodev --- */
454 static void get_audiodev(Object *obj, Visitor *v, const char* name,
455 void *opaque, Error **errp)
457 Property *prop = opaque;
458 QEMUSoundCard *card = object_field_prop_ptr(obj, prop);
459 char *p = g_strdup(audio_get_id(card));
461 visit_type_str(v, name, &p, errp);
462 g_free(p);
465 static void set_audiodev(Object *obj, Visitor *v, const char* name,
466 void *opaque, Error **errp)
468 Property *prop = opaque;
469 QEMUSoundCard *card = object_field_prop_ptr(obj, prop);
470 AudioState *state;
471 int err = 0;
472 char *str;
474 if (!visit_type_str(v, name, &str, errp)) {
475 return;
478 state = audio_state_by_name(str);
480 if (!state) {
481 err = -ENOENT;
482 goto out;
484 card->state = state;
486 out:
487 error_set_from_qdev_prop_error(errp, err, obj, name, str);
488 g_free(str);
491 const PropertyInfo qdev_prop_audiodev = {
492 .name = "str",
493 .description = "ID of an audiodev to use as a backend",
494 /* release done on shutdown */
495 .get = get_audiodev,
496 .set = set_audiodev,
499 bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
500 BlockBackend *value, Error **errp)
502 const char *ref = "";
504 if (value) {
505 ref = blk_name(value);
506 if (!*ref) {
507 const BlockDriverState *bs = blk_bs(value);
508 if (bs) {
509 ref = bdrv_get_node_name(bs);
514 return object_property_set_str(OBJECT(dev), name, ref, errp);
517 void qdev_prop_set_drive(DeviceState *dev, const char *name,
518 BlockBackend *value)
520 qdev_prop_set_drive_err(dev, name, value, &error_abort);
523 void qdev_prop_set_chr(DeviceState *dev, const char *name,
524 Chardev *value)
526 assert(!value || value->label);
527 object_property_set_str(OBJECT(dev), name, value ? value->label : "",
528 &error_abort);
531 void qdev_prop_set_netdev(DeviceState *dev, const char *name,
532 NetClientState *value)
534 assert(!value || value->name);
535 object_property_set_str(OBJECT(dev), name, value ? value->name : "",
536 &error_abort);
539 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
541 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
542 if (nd->netdev) {
543 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
545 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
546 object_property_find(OBJECT(dev), "vectors")) {
547 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
549 nd->instantiated = 1;
552 /* --- lost tick policy --- */
554 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
556 const PropertyInfo qdev_prop_losttickpolicy = {
557 .name = "LostTickPolicy",
558 .enum_table = &LostTickPolicy_lookup,
559 .get = qdev_propinfo_get_enum,
560 .set = qdev_propinfo_set_enum,
561 .set_default_value = qdev_propinfo_set_default_value_enum,
564 /* --- blocksize --- */
566 static void set_blocksize(Object *obj, Visitor *v, const char *name,
567 void *opaque, Error **errp)
569 DeviceState *dev = DEVICE(obj);
570 Property *prop = opaque;
571 uint32_t *ptr = object_field_prop_ptr(obj, prop);
572 uint64_t value;
573 Error *local_err = NULL;
575 if (!visit_type_size(v, name, &value, errp)) {
576 return;
578 check_block_size(dev->id ? : "", name, value, &local_err);
579 if (local_err) {
580 error_propagate(errp, local_err);
581 return;
583 *ptr = value;
586 const PropertyInfo qdev_prop_blocksize = {
587 .name = "size",
588 .description = "A power of two between " MIN_BLOCK_SIZE_STR
589 " and " MAX_BLOCK_SIZE_STR,
590 .get = qdev_propinfo_get_size32,
591 .set = set_blocksize,
592 .set_default_value = qdev_propinfo_set_default_value_uint,
595 /* --- Block device error handling policy --- */
597 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError) != sizeof(int));
599 const PropertyInfo qdev_prop_blockdev_on_error = {
600 .name = "BlockdevOnError",
601 .description = "Error handling policy, "
602 "report/ignore/enospc/stop/auto",
603 .enum_table = &BlockdevOnError_lookup,
604 .get = qdev_propinfo_get_enum,
605 .set = qdev_propinfo_set_enum,
606 .set_default_value = qdev_propinfo_set_default_value_enum,
609 /* --- BIOS CHS translation */
611 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation) != sizeof(int));
613 const PropertyInfo qdev_prop_bios_chs_trans = {
614 .name = "BiosAtaTranslation",
615 .description = "Logical CHS translation algorithm, "
616 "auto/none/lba/large/rechs",
617 .enum_table = &BiosAtaTranslation_lookup,
618 .get = qdev_propinfo_get_enum,
619 .set = qdev_propinfo_set_enum,
620 .set_default_value = qdev_propinfo_set_default_value_enum,
623 /* --- FDC default drive types */
625 const PropertyInfo qdev_prop_fdc_drive_type = {
626 .name = "FdcDriveType",
627 .description = "FDC drive type, "
628 "144/288/120/none/auto",
629 .enum_table = &FloppyDriveType_lookup,
630 .get = qdev_propinfo_get_enum,
631 .set = qdev_propinfo_set_enum,
632 .set_default_value = qdev_propinfo_set_default_value_enum,
635 /* --- MultiFDCompression --- */
637 const PropertyInfo qdev_prop_multifd_compression = {
638 .name = "MultiFDCompression",
639 .description = "multifd_compression values, "
640 "none/zlib/zstd",
641 .enum_table = &MultiFDCompression_lookup,
642 .get = qdev_propinfo_get_enum,
643 .set = qdev_propinfo_set_enum,
644 .set_default_value = qdev_propinfo_set_default_value_enum,
647 /* --- Reserved Region --- */
650 * Accepted syntax:
651 * <low address>:<high address>:<type>
652 * where low/high addresses are uint64_t in hexadecimal
653 * and type is a non-negative decimal integer
655 static void get_reserved_region(Object *obj, Visitor *v, const char *name,
656 void *opaque, Error **errp)
658 Property *prop = opaque;
659 ReservedRegion *rr = object_field_prop_ptr(obj, prop);
660 char buffer[64];
661 char *p = buffer;
662 int rc;
664 rc = snprintf(buffer, sizeof(buffer), "0x%"PRIx64":0x%"PRIx64":%u",
665 rr->low, rr->high, rr->type);
666 assert(rc < sizeof(buffer));
668 visit_type_str(v, name, &p, errp);
671 static void set_reserved_region(Object *obj, Visitor *v, const char *name,
672 void *opaque, Error **errp)
674 Property *prop = opaque;
675 ReservedRegion *rr = object_field_prop_ptr(obj, prop);
676 Error *local_err = NULL;
677 const char *endptr;
678 char *str;
679 int ret;
681 visit_type_str(v, name, &str, &local_err);
682 if (local_err) {
683 error_propagate(errp, local_err);
684 return;
687 ret = qemu_strtou64(str, &endptr, 16, &rr->low);
688 if (ret) {
689 error_setg(errp, "start address of '%s'"
690 " must be a hexadecimal integer", name);
691 goto out;
693 if (*endptr != ':') {
694 goto separator_error;
697 ret = qemu_strtou64(endptr + 1, &endptr, 16, &rr->high);
698 if (ret) {
699 error_setg(errp, "end address of '%s'"
700 " must be a hexadecimal integer", name);
701 goto out;
703 if (*endptr != ':') {
704 goto separator_error;
707 ret = qemu_strtoui(endptr + 1, &endptr, 10, &rr->type);
708 if (ret) {
709 error_setg(errp, "type of '%s'"
710 " must be a non-negative decimal integer", name);
712 goto out;
714 separator_error:
715 error_setg(errp, "reserved region fields must be separated with ':'");
716 out:
717 g_free(str);
718 return;
721 const PropertyInfo qdev_prop_reserved_region = {
722 .name = "reserved_region",
723 .description = "Reserved Region, example: 0xFEE00000:0xFEEFFFFF:0",
724 .get = get_reserved_region,
725 .set = set_reserved_region,
728 /* --- pci address --- */
731 * bus-local address, i.e. "$slot" or "$slot.$fn"
733 static void set_pci_devfn(Object *obj, Visitor *v, const char *name,
734 void *opaque, Error **errp)
736 Property *prop = opaque;
737 int32_t value, *ptr = object_field_prop_ptr(obj, prop);
738 unsigned int slot, fn, n;
739 char *str;
741 if (!visit_type_str(v, name, &str, NULL)) {
742 if (!visit_type_int32(v, name, &value, errp)) {
743 return;
745 if (value < -1 || value > 255) {
746 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
747 name ? name : "null", "a value between -1 and 255");
748 return;
750 *ptr = value;
751 return;
754 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
755 fn = 0;
756 if (sscanf(str, "%x%n", &slot, &n) != 1) {
757 goto invalid;
760 if (str[n] != '\0' || fn > 7 || slot > 31) {
761 goto invalid;
763 *ptr = slot << 3 | fn;
764 g_free(str);
765 return;
767 invalid:
768 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
769 g_free(str);
772 static int print_pci_devfn(Object *obj, Property *prop, char *dest,
773 size_t len)
775 int32_t *ptr = object_field_prop_ptr(obj, prop);
777 if (*ptr == -1) {
778 return snprintf(dest, len, "<unset>");
779 } else {
780 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
784 const PropertyInfo qdev_prop_pci_devfn = {
785 .name = "int32",
786 .description = "Slot and optional function number, example: 06.0 or 06",
787 .print = print_pci_devfn,
788 .get = qdev_propinfo_get_int32,
789 .set = set_pci_devfn,
790 .set_default_value = qdev_propinfo_set_default_value_int,
793 /* --- pci host address --- */
795 static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
796 void *opaque, Error **errp)
798 Property *prop = opaque;
799 PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
800 char buffer[] = "ffff:ff:ff.f";
801 char *p = buffer;
802 int rc = 0;
805 * Catch "invalid" device reference from vfio-pci and allow the
806 * default buffer representing the non-existent device to be used.
808 if (~addr->domain || ~addr->bus || ~addr->slot || ~addr->function) {
809 rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%0d",
810 addr->domain, addr->bus, addr->slot, addr->function);
811 assert(rc == sizeof(buffer) - 1);
814 visit_type_str(v, name, &p, errp);
818 * Parse [<domain>:]<bus>:<slot>.<func>
819 * if <domain> is not supplied, it's assumed to be 0.
821 static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
822 void *opaque, Error **errp)
824 Property *prop = opaque;
825 PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
826 char *str, *p;
827 char *e;
828 unsigned long val;
829 unsigned long dom = 0, bus = 0;
830 unsigned int slot = 0, func = 0;
832 if (!visit_type_str(v, name, &str, errp)) {
833 return;
836 p = str;
837 val = strtoul(p, &e, 16);
838 if (e == p || *e != ':') {
839 goto inval;
841 bus = val;
843 p = e + 1;
844 val = strtoul(p, &e, 16);
845 if (e == p) {
846 goto inval;
848 if (*e == ':') {
849 dom = bus;
850 bus = val;
851 p = e + 1;
852 val = strtoul(p, &e, 16);
853 if (e == p) {
854 goto inval;
857 slot = val;
859 if (*e != '.') {
860 goto inval;
862 p = e + 1;
863 val = strtoul(p, &e, 10);
864 if (e == p) {
865 goto inval;
867 func = val;
869 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
870 goto inval;
873 if (*e) {
874 goto inval;
877 addr->domain = dom;
878 addr->bus = bus;
879 addr->slot = slot;
880 addr->function = func;
882 g_free(str);
883 return;
885 inval:
886 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
887 g_free(str);
890 const PropertyInfo qdev_prop_pci_host_devaddr = {
891 .name = "str",
892 .description = "Address (bus/device/function) of "
893 "the host device, example: 04:10.0",
894 .get = get_pci_host_devaddr,
895 .set = set_pci_host_devaddr,
898 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
900 const PropertyInfo qdev_prop_off_auto_pcibar = {
901 .name = "OffAutoPCIBAR",
902 .description = "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
903 .enum_table = &OffAutoPCIBAR_lookup,
904 .get = qdev_propinfo_get_enum,
905 .set = qdev_propinfo_set_enum,
906 .set_default_value = qdev_propinfo_set_default_value_enum,
909 /* --- PCIELinkSpeed 2_5/5/8/16 -- */
911 static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
912 void *opaque, Error **errp)
914 Property *prop = opaque;
915 PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
916 int speed;
918 switch (*p) {
919 case QEMU_PCI_EXP_LNK_2_5GT:
920 speed = PCIE_LINK_SPEED_2_5;
921 break;
922 case QEMU_PCI_EXP_LNK_5GT:
923 speed = PCIE_LINK_SPEED_5;
924 break;
925 case QEMU_PCI_EXP_LNK_8GT:
926 speed = PCIE_LINK_SPEED_8;
927 break;
928 case QEMU_PCI_EXP_LNK_16GT:
929 speed = PCIE_LINK_SPEED_16;
930 break;
931 default:
932 /* Unreachable */
933 abort();
936 visit_type_enum(v, name, &speed, prop->info->enum_table, errp);
939 static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
940 void *opaque, Error **errp)
942 Property *prop = opaque;
943 PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
944 int speed;
946 if (!visit_type_enum(v, name, &speed, prop->info->enum_table,
947 errp)) {
948 return;
951 switch (speed) {
952 case PCIE_LINK_SPEED_2_5:
953 *p = QEMU_PCI_EXP_LNK_2_5GT;
954 break;
955 case PCIE_LINK_SPEED_5:
956 *p = QEMU_PCI_EXP_LNK_5GT;
957 break;
958 case PCIE_LINK_SPEED_8:
959 *p = QEMU_PCI_EXP_LNK_8GT;
960 break;
961 case PCIE_LINK_SPEED_16:
962 *p = QEMU_PCI_EXP_LNK_16GT;
963 break;
964 default:
965 /* Unreachable */
966 abort();
970 const PropertyInfo qdev_prop_pcie_link_speed = {
971 .name = "PCIELinkSpeed",
972 .description = "2_5/5/8/16",
973 .enum_table = &PCIELinkSpeed_lookup,
974 .get = get_prop_pcielinkspeed,
975 .set = set_prop_pcielinkspeed,
976 .set_default_value = qdev_propinfo_set_default_value_enum,
979 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
981 static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
982 void *opaque, Error **errp)
984 Property *prop = opaque;
985 PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
986 int width;
988 switch (*p) {
989 case QEMU_PCI_EXP_LNK_X1:
990 width = PCIE_LINK_WIDTH_1;
991 break;
992 case QEMU_PCI_EXP_LNK_X2:
993 width = PCIE_LINK_WIDTH_2;
994 break;
995 case QEMU_PCI_EXP_LNK_X4:
996 width = PCIE_LINK_WIDTH_4;
997 break;
998 case QEMU_PCI_EXP_LNK_X8:
999 width = PCIE_LINK_WIDTH_8;
1000 break;
1001 case QEMU_PCI_EXP_LNK_X12:
1002 width = PCIE_LINK_WIDTH_12;
1003 break;
1004 case QEMU_PCI_EXP_LNK_X16:
1005 width = PCIE_LINK_WIDTH_16;
1006 break;
1007 case QEMU_PCI_EXP_LNK_X32:
1008 width = PCIE_LINK_WIDTH_32;
1009 break;
1010 default:
1011 /* Unreachable */
1012 abort();
1015 visit_type_enum(v, name, &width, prop->info->enum_table, errp);
1018 static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1019 void *opaque, Error **errp)
1021 Property *prop = opaque;
1022 PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
1023 int width;
1025 if (!visit_type_enum(v, name, &width, prop->info->enum_table,
1026 errp)) {
1027 return;
1030 switch (width) {
1031 case PCIE_LINK_WIDTH_1:
1032 *p = QEMU_PCI_EXP_LNK_X1;
1033 break;
1034 case PCIE_LINK_WIDTH_2:
1035 *p = QEMU_PCI_EXP_LNK_X2;
1036 break;
1037 case PCIE_LINK_WIDTH_4:
1038 *p = QEMU_PCI_EXP_LNK_X4;
1039 break;
1040 case PCIE_LINK_WIDTH_8:
1041 *p = QEMU_PCI_EXP_LNK_X8;
1042 break;
1043 case PCIE_LINK_WIDTH_12:
1044 *p = QEMU_PCI_EXP_LNK_X12;
1045 break;
1046 case PCIE_LINK_WIDTH_16:
1047 *p = QEMU_PCI_EXP_LNK_X16;
1048 break;
1049 case PCIE_LINK_WIDTH_32:
1050 *p = QEMU_PCI_EXP_LNK_X32;
1051 break;
1052 default:
1053 /* Unreachable */
1054 abort();
1058 const PropertyInfo qdev_prop_pcie_link_width = {
1059 .name = "PCIELinkWidth",
1060 .description = "1/2/4/8/12/16/32",
1061 .enum_table = &PCIELinkWidth_lookup,
1062 .get = get_prop_pcielinkwidth,
1063 .set = set_prop_pcielinkwidth,
1064 .set_default_value = qdev_propinfo_set_default_value_enum,
1067 /* --- UUID --- */
1069 static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1070 Error **errp)
1072 Property *prop = opaque;
1073 QemuUUID *uuid = object_field_prop_ptr(obj, prop);
1074 char buffer[UUID_FMT_LEN + 1];
1075 char *p = buffer;
1077 qemu_uuid_unparse(uuid, buffer);
1079 visit_type_str(v, name, &p, errp);
1082 #define UUID_VALUE_AUTO "auto"
1084 static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1085 Error **errp)
1087 Property *prop = opaque;
1088 QemuUUID *uuid = object_field_prop_ptr(obj, prop);
1089 char *str;
1091 if (!visit_type_str(v, name, &str, errp)) {
1092 return;
1095 if (!strcmp(str, UUID_VALUE_AUTO)) {
1096 qemu_uuid_generate(uuid);
1097 } else if (qemu_uuid_parse(str, uuid) < 0) {
1098 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
1100 g_free(str);
1103 static void set_default_uuid_auto(ObjectProperty *op, const Property *prop)
1105 object_property_set_default_str(op, UUID_VALUE_AUTO);
1108 const PropertyInfo qdev_prop_uuid = {
1109 .name = "str",
1110 .description = "UUID (aka GUID) or \"" UUID_VALUE_AUTO
1111 "\" for random value (default)",
1112 .get = get_uuid,
1113 .set = set_uuid,
1114 .set_default_value = set_default_uuid_auto,