1 #include "qemu/osdep.h"
3 #include "hw/qdev-properties.h"
4 #include "qapi/error.h"
5 #include "hw/pci/pci.h"
6 #include "qapi/qapi-types-block.h"
7 #include "qapi/qapi-types-misc.h"
8 #include "qapi/qmp/qerror.h"
9 #include "qemu/ctype.h"
10 #include "qemu/error-report.h"
11 #include "qapi/qapi-types-migration.h"
12 #include "hw/block/block.h"
14 #include "qapi/visitor.h"
15 #include "chardev/char.h"
16 #include "qemu/uuid.h"
18 void qdev_prop_set_after_realize(DeviceState
*dev
, const char *name
,
22 error_setg(errp
, "Attempt to set property '%s' on device '%s' "
23 "(type '%s') after it was realized", name
, dev
->id
,
24 object_get_typename(OBJECT(dev
)));
26 error_setg(errp
, "Attempt to set property '%s' on anonymous device "
27 "(type '%s') after it was realized", name
,
28 object_get_typename(OBJECT(dev
)));
32 void qdev_prop_allow_set_link_before_realize(const Object
*obj
,
34 Object
*val
, Error
**errp
)
36 DeviceState
*dev
= DEVICE(obj
);
39 error_setg(errp
, "Attempt to set link property '%s' on device '%s' "
40 "(type '%s') after it was realized",
41 name
, dev
->id
, object_get_typename(obj
));
45 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
52 static void get_enum(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
55 DeviceState
*dev
= DEVICE(obj
);
56 Property
*prop
= opaque
;
57 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
59 visit_type_enum(v
, prop
->name
, ptr
, prop
->info
->enum_table
, errp
);
62 static void set_enum(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
65 DeviceState
*dev
= DEVICE(obj
);
66 Property
*prop
= opaque
;
67 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
70 qdev_prop_set_after_realize(dev
, name
, errp
);
74 visit_type_enum(v
, prop
->name
, ptr
, prop
->info
->enum_table
, errp
);
77 static void set_default_value_enum(ObjectProperty
*op
, const Property
*prop
)
79 object_property_set_default_str(op
,
80 qapi_enum_lookup(prop
->info
->enum_table
, prop
->defval
.i
));
85 static uint32_t qdev_get_prop_mask(Property
*prop
)
87 assert(prop
->info
== &qdev_prop_bit
);
88 return 0x1 << prop
->bitnr
;
91 static void bit_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
93 uint32_t *p
= qdev_get_prop_ptr(dev
, props
);
94 uint32_t mask
= qdev_get_prop_mask(props
);
102 static void prop_get_bit(Object
*obj
, Visitor
*v
, const char *name
,
103 void *opaque
, Error
**errp
)
105 DeviceState
*dev
= DEVICE(obj
);
106 Property
*prop
= opaque
;
107 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
108 bool value
= (*p
& qdev_get_prop_mask(prop
)) != 0;
110 visit_type_bool(v
, name
, &value
, errp
);
113 static void prop_set_bit(Object
*obj
, Visitor
*v
, const char *name
,
114 void *opaque
, Error
**errp
)
116 DeviceState
*dev
= DEVICE(obj
);
117 Property
*prop
= opaque
;
118 Error
*local_err
= NULL
;
122 qdev_prop_set_after_realize(dev
, name
, errp
);
126 visit_type_bool(v
, name
, &value
, &local_err
);
128 error_propagate(errp
, local_err
);
131 bit_prop_set(dev
, prop
, value
);
134 static void set_default_value_bool(ObjectProperty
*op
, const Property
*prop
)
136 object_property_set_default_bool(op
, prop
->defval
.u
);
139 const PropertyInfo qdev_prop_bit
= {
141 .description
= "on/off",
144 .set_default_value
= set_default_value_bool
,
149 static uint64_t qdev_get_prop_mask64(Property
*prop
)
151 assert(prop
->info
== &qdev_prop_bit64
);
152 return 0x1ull
<< prop
->bitnr
;
155 static void bit64_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
157 uint64_t *p
= qdev_get_prop_ptr(dev
, props
);
158 uint64_t mask
= qdev_get_prop_mask64(props
);
166 static void prop_get_bit64(Object
*obj
, Visitor
*v
, const char *name
,
167 void *opaque
, Error
**errp
)
169 DeviceState
*dev
= DEVICE(obj
);
170 Property
*prop
= opaque
;
171 uint64_t *p
= qdev_get_prop_ptr(dev
, prop
);
172 bool value
= (*p
& qdev_get_prop_mask64(prop
)) != 0;
174 visit_type_bool(v
, name
, &value
, errp
);
177 static void prop_set_bit64(Object
*obj
, Visitor
*v
, const char *name
,
178 void *opaque
, Error
**errp
)
180 DeviceState
*dev
= DEVICE(obj
);
181 Property
*prop
= opaque
;
182 Error
*local_err
= NULL
;
186 qdev_prop_set_after_realize(dev
, name
, errp
);
190 visit_type_bool(v
, name
, &value
, &local_err
);
192 error_propagate(errp
, local_err
);
195 bit64_prop_set(dev
, prop
, value
);
198 const PropertyInfo qdev_prop_bit64
= {
200 .description
= "on/off",
201 .get
= prop_get_bit64
,
202 .set
= prop_set_bit64
,
203 .set_default_value
= set_default_value_bool
,
208 static void get_bool(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
211 DeviceState
*dev
= DEVICE(obj
);
212 Property
*prop
= opaque
;
213 bool *ptr
= qdev_get_prop_ptr(dev
, prop
);
215 visit_type_bool(v
, name
, ptr
, errp
);
218 static void set_bool(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
221 DeviceState
*dev
= DEVICE(obj
);
222 Property
*prop
= opaque
;
223 bool *ptr
= qdev_get_prop_ptr(dev
, prop
);
226 qdev_prop_set_after_realize(dev
, name
, errp
);
230 visit_type_bool(v
, name
, ptr
, errp
);
233 const PropertyInfo qdev_prop_bool
= {
237 .set_default_value
= set_default_value_bool
,
240 /* --- 8bit integer --- */
242 static void get_uint8(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
245 DeviceState
*dev
= DEVICE(obj
);
246 Property
*prop
= opaque
;
247 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
249 visit_type_uint8(v
, name
, ptr
, errp
);
252 static void set_uint8(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
255 DeviceState
*dev
= DEVICE(obj
);
256 Property
*prop
= opaque
;
257 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
260 qdev_prop_set_after_realize(dev
, name
, errp
);
264 visit_type_uint8(v
, name
, ptr
, errp
);
267 static void set_default_value_int(ObjectProperty
*op
, const Property
*prop
)
269 object_property_set_default_int(op
, prop
->defval
.i
);
272 static void set_default_value_uint(ObjectProperty
*op
, const Property
*prop
)
274 object_property_set_default_uint(op
, prop
->defval
.u
);
277 const PropertyInfo qdev_prop_uint8
= {
281 .set_default_value
= set_default_value_uint
,
284 /* --- 16bit integer --- */
286 static void get_uint16(Object
*obj
, Visitor
*v
, const char *name
,
287 void *opaque
, Error
**errp
)
289 DeviceState
*dev
= DEVICE(obj
);
290 Property
*prop
= opaque
;
291 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
293 visit_type_uint16(v
, name
, ptr
, errp
);
296 static void set_uint16(Object
*obj
, Visitor
*v
, const char *name
,
297 void *opaque
, Error
**errp
)
299 DeviceState
*dev
= DEVICE(obj
);
300 Property
*prop
= opaque
;
301 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
304 qdev_prop_set_after_realize(dev
, name
, errp
);
308 visit_type_uint16(v
, name
, ptr
, errp
);
311 const PropertyInfo qdev_prop_uint16
= {
315 .set_default_value
= set_default_value_uint
,
318 /* --- 32bit integer --- */
320 static void get_uint32(Object
*obj
, Visitor
*v
, const char *name
,
321 void *opaque
, Error
**errp
)
323 DeviceState
*dev
= DEVICE(obj
);
324 Property
*prop
= opaque
;
325 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
327 visit_type_uint32(v
, name
, ptr
, errp
);
330 static void set_uint32(Object
*obj
, Visitor
*v
, const char *name
,
331 void *opaque
, Error
**errp
)
333 DeviceState
*dev
= DEVICE(obj
);
334 Property
*prop
= opaque
;
335 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
338 qdev_prop_set_after_realize(dev
, name
, errp
);
342 visit_type_uint32(v
, name
, ptr
, errp
);
345 static void get_int32(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
348 DeviceState
*dev
= DEVICE(obj
);
349 Property
*prop
= opaque
;
350 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
352 visit_type_int32(v
, name
, ptr
, errp
);
355 static void set_int32(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
358 DeviceState
*dev
= DEVICE(obj
);
359 Property
*prop
= opaque
;
360 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
363 qdev_prop_set_after_realize(dev
, name
, errp
);
367 visit_type_int32(v
, name
, ptr
, errp
);
370 const PropertyInfo qdev_prop_uint32
= {
374 .set_default_value
= set_default_value_uint
,
377 const PropertyInfo qdev_prop_int32
= {
381 .set_default_value
= set_default_value_int
,
384 /* --- 64bit integer --- */
386 static void get_uint64(Object
*obj
, Visitor
*v
, const char *name
,
387 void *opaque
, Error
**errp
)
389 DeviceState
*dev
= DEVICE(obj
);
390 Property
*prop
= opaque
;
391 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
393 visit_type_uint64(v
, name
, ptr
, errp
);
396 static void set_uint64(Object
*obj
, Visitor
*v
, const char *name
,
397 void *opaque
, Error
**errp
)
399 DeviceState
*dev
= DEVICE(obj
);
400 Property
*prop
= opaque
;
401 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
404 qdev_prop_set_after_realize(dev
, name
, errp
);
408 visit_type_uint64(v
, name
, ptr
, errp
);
411 static void get_int64(Object
*obj
, Visitor
*v
, const char *name
,
412 void *opaque
, Error
**errp
)
414 DeviceState
*dev
= DEVICE(obj
);
415 Property
*prop
= opaque
;
416 int64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
418 visit_type_int64(v
, name
, ptr
, errp
);
421 static void set_int64(Object
*obj
, Visitor
*v
, const char *name
,
422 void *opaque
, Error
**errp
)
424 DeviceState
*dev
= DEVICE(obj
);
425 Property
*prop
= opaque
;
426 int64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
429 qdev_prop_set_after_realize(dev
, name
, errp
);
433 visit_type_int64(v
, name
, ptr
, errp
);
436 const PropertyInfo qdev_prop_uint64
= {
440 .set_default_value
= set_default_value_uint
,
443 const PropertyInfo qdev_prop_int64
= {
447 .set_default_value
= set_default_value_int
,
452 static void release_string(Object
*obj
, const char *name
, void *opaque
)
454 Property
*prop
= opaque
;
455 g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj
), prop
));
458 static void get_string(Object
*obj
, Visitor
*v
, const char *name
,
459 void *opaque
, Error
**errp
)
461 DeviceState
*dev
= DEVICE(obj
);
462 Property
*prop
= opaque
;
463 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
466 char *str
= (char *)"";
467 visit_type_str(v
, name
, &str
, errp
);
469 visit_type_str(v
, name
, ptr
, errp
);
473 static void set_string(Object
*obj
, Visitor
*v
, const char *name
,
474 void *opaque
, Error
**errp
)
476 DeviceState
*dev
= DEVICE(obj
);
477 Property
*prop
= opaque
;
478 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
479 Error
*local_err
= NULL
;
483 qdev_prop_set_after_realize(dev
, name
, errp
);
487 visit_type_str(v
, name
, &str
, &local_err
);
489 error_propagate(errp
, local_err
);
496 const PropertyInfo qdev_prop_string
= {
498 .release
= release_string
,
503 /* --- mac address --- */
506 * accepted syntax versions:
510 static void get_mac(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
513 DeviceState
*dev
= DEVICE(obj
);
514 Property
*prop
= opaque
;
515 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
516 char buffer
[2 * 6 + 5 + 1];
519 snprintf(buffer
, sizeof(buffer
), "%02x:%02x:%02x:%02x:%02x:%02x",
520 mac
->a
[0], mac
->a
[1], mac
->a
[2],
521 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
523 visit_type_str(v
, name
, &p
, errp
);
526 static void set_mac(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
529 DeviceState
*dev
= DEVICE(obj
);
530 Property
*prop
= opaque
;
531 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
532 Error
*local_err
= NULL
;
537 qdev_prop_set_after_realize(dev
, name
, errp
);
541 visit_type_str(v
, name
, &str
, &local_err
);
543 error_propagate(errp
, local_err
);
547 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
548 if (!qemu_isxdigit(str
[pos
])) {
551 if (!qemu_isxdigit(str
[pos
+1])) {
555 if (str
[pos
+2] != '\0') {
559 if (str
[pos
+2] != ':' && str
[pos
+2] != '-') {
563 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
569 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
573 const PropertyInfo qdev_prop_macaddr
= {
575 .description
= "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56",
580 /* --- on/off/auto --- */
582 const PropertyInfo qdev_prop_on_off_auto
= {
584 .description
= "on/off/auto",
585 .enum_table
= &OnOffAuto_lookup
,
588 .set_default_value
= set_default_value_enum
,
591 /* --- lost tick policy --- */
593 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy
) != sizeof(int));
595 const PropertyInfo qdev_prop_losttickpolicy
= {
596 .name
= "LostTickPolicy",
597 .enum_table
= &LostTickPolicy_lookup
,
600 .set_default_value
= set_default_value_enum
,
603 /* --- Block device error handling policy --- */
605 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError
) != sizeof(int));
607 const PropertyInfo qdev_prop_blockdev_on_error
= {
608 .name
= "BlockdevOnError",
609 .description
= "Error handling policy, "
610 "report/ignore/enospc/stop/auto",
611 .enum_table
= &BlockdevOnError_lookup
,
614 .set_default_value
= set_default_value_enum
,
617 /* --- BIOS CHS translation */
619 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation
) != sizeof(int));
621 const PropertyInfo qdev_prop_bios_chs_trans
= {
622 .name
= "BiosAtaTranslation",
623 .description
= "Logical CHS translation algorithm, "
624 "auto/none/lba/large/rechs",
625 .enum_table
= &BiosAtaTranslation_lookup
,
628 .set_default_value
= set_default_value_enum
,
631 /* --- FDC default drive types */
633 const PropertyInfo qdev_prop_fdc_drive_type
= {
634 .name
= "FdcDriveType",
635 .description
= "FDC drive type, "
636 "144/288/120/none/auto",
637 .enum_table
= &FloppyDriveType_lookup
,
640 .set_default_value
= set_default_value_enum
,
643 /* --- MultiFDCompression --- */
645 const PropertyInfo qdev_prop_multifd_compression
= {
646 .name
= "MultiFDCompression",
647 .description
= "multifd_compression values, "
649 .enum_table
= &MultiFDCompression_lookup
,
652 .set_default_value
= set_default_value_enum
,
655 /* --- pci address --- */
658 * bus-local address, i.e. "$slot" or "$slot.$fn"
660 static void set_pci_devfn(Object
*obj
, Visitor
*v
, const char *name
,
661 void *opaque
, Error
**errp
)
663 DeviceState
*dev
= DEVICE(obj
);
664 Property
*prop
= opaque
;
665 int32_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
666 unsigned int slot
, fn
, n
;
667 Error
*local_err
= NULL
;
671 qdev_prop_set_after_realize(dev
, name
, errp
);
675 visit_type_str(v
, name
, &str
, &local_err
);
677 error_free(local_err
);
679 visit_type_int32(v
, name
, &value
, &local_err
);
681 error_propagate(errp
, local_err
);
682 } else if (value
< -1 || value
> 255) {
683 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
684 name
? name
: "null", "pci_devfn");
691 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
693 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
697 if (str
[n
] != '\0' || fn
> 7 || slot
> 31) {
700 *ptr
= slot
<< 3 | fn
;
705 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
709 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
,
712 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
715 return snprintf(dest
, len
, "<unset>");
717 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
721 const PropertyInfo qdev_prop_pci_devfn
= {
723 .description
= "Slot and optional function number, example: 06.0 or 06",
724 .print
= print_pci_devfn
,
726 .set
= set_pci_devfn
,
727 .set_default_value
= set_default_value_int
,
730 /* --- blocksize --- */
732 static void set_blocksize(Object
*obj
, Visitor
*v
, const char *name
,
733 void *opaque
, Error
**errp
)
735 DeviceState
*dev
= DEVICE(obj
);
736 Property
*prop
= opaque
;
737 uint16_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
738 Error
*local_err
= NULL
;
739 const int64_t min
= 512;
740 const int64_t max
= 32768;
743 qdev_prop_set_after_realize(dev
, name
, errp
);
747 visit_type_uint16(v
, name
, &value
, &local_err
);
749 error_propagate(errp
, local_err
);
752 /* value of 0 means "unset" */
753 if (value
&& (value
< min
|| value
> max
)) {
754 error_setg(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
755 dev
->id
? : "", name
, (int64_t)value
, min
, max
);
759 /* We rely on power-of-2 blocksizes for bitmasks */
760 if ((value
& (value
- 1)) != 0) {
762 "Property %s.%s doesn't take value '%" PRId64
"', it's not a power of 2",
763 dev
->id
?: "", name
, (int64_t)value
);
770 const PropertyInfo qdev_prop_blocksize
= {
772 .description
= "A power of two between 512 and 32768",
774 .set
= set_blocksize
,
775 .set_default_value
= set_default_value_uint
,
778 /* --- pci host address --- */
780 static void get_pci_host_devaddr(Object
*obj
, Visitor
*v
, const char *name
,
781 void *opaque
, Error
**errp
)
783 DeviceState
*dev
= DEVICE(obj
);
784 Property
*prop
= opaque
;
785 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
786 char buffer
[] = "ffff:ff:ff.f";
791 * Catch "invalid" device reference from vfio-pci and allow the
792 * default buffer representing the non-existent device to be used.
794 if (~addr
->domain
|| ~addr
->bus
|| ~addr
->slot
|| ~addr
->function
) {
795 rc
= snprintf(buffer
, sizeof(buffer
), "%04x:%02x:%02x.%0d",
796 addr
->domain
, addr
->bus
, addr
->slot
, addr
->function
);
797 assert(rc
== sizeof(buffer
) - 1);
800 visit_type_str(v
, name
, &p
, errp
);
804 * Parse [<domain>:]<bus>:<slot>.<func>
805 * if <domain> is not supplied, it's assumed to be 0.
807 static void set_pci_host_devaddr(Object
*obj
, Visitor
*v
, const char *name
,
808 void *opaque
, Error
**errp
)
810 DeviceState
*dev
= DEVICE(obj
);
811 Property
*prop
= opaque
;
812 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
813 Error
*local_err
= NULL
;
817 unsigned long dom
= 0, bus
= 0;
818 unsigned int slot
= 0, func
= 0;
821 qdev_prop_set_after_realize(dev
, name
, errp
);
825 visit_type_str(v
, name
, &str
, &local_err
);
827 error_propagate(errp
, local_err
);
832 val
= strtoul(p
, &e
, 16);
833 if (e
== p
|| *e
!= ':') {
839 val
= strtoul(p
, &e
, 16);
847 val
= strtoul(p
, &e
, 16);
858 val
= strtoul(p
, &e
, 10);
864 if (dom
> 0xffff || bus
> 0xff || slot
> 0x1f || func
> 7) {
875 addr
->function
= func
;
881 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
885 const PropertyInfo qdev_prop_pci_host_devaddr
= {
887 .description
= "Address (bus/device/function) of "
888 "the host device, example: 04:10.0",
889 .get
= get_pci_host_devaddr
,
890 .set
= set_pci_host_devaddr
,
895 static void get_uuid(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
898 DeviceState
*dev
= DEVICE(obj
);
899 Property
*prop
= opaque
;
900 QemuUUID
*uuid
= qdev_get_prop_ptr(dev
, prop
);
901 char buffer
[UUID_FMT_LEN
+ 1];
904 qemu_uuid_unparse(uuid
, buffer
);
906 visit_type_str(v
, name
, &p
, errp
);
909 #define UUID_VALUE_AUTO "auto"
911 static void set_uuid(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
914 DeviceState
*dev
= DEVICE(obj
);
915 Property
*prop
= opaque
;
916 QemuUUID
*uuid
= qdev_get_prop_ptr(dev
, prop
);
917 Error
*local_err
= NULL
;
921 qdev_prop_set_after_realize(dev
, name
, errp
);
925 visit_type_str(v
, name
, &str
, &local_err
);
927 error_propagate(errp
, local_err
);
931 if (!strcmp(str
, UUID_VALUE_AUTO
)) {
932 qemu_uuid_generate(uuid
);
933 } else if (qemu_uuid_parse(str
, uuid
) < 0) {
934 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
939 static void set_default_uuid_auto(ObjectProperty
*op
, const Property
*prop
)
941 object_property_set_default_str(op
, UUID_VALUE_AUTO
);
944 const PropertyInfo qdev_prop_uuid
= {
946 .description
= "UUID (aka GUID) or \"" UUID_VALUE_AUTO
947 "\" for random value (default)",
950 .set_default_value
= set_default_uuid_auto
,
953 /* --- support for array properties --- */
955 /* Used as an opaque for the object properties we add for each
956 * array element. Note that the struct Property must be first
957 * in the struct so that a pointer to this works as the opaque
958 * for the underlying element's property hooks as well as for
959 * our own release callback.
962 struct Property prop
;
964 ObjectPropertyRelease
*release
;
965 } ArrayElementProperty
;
967 /* object property release callback for array element properties:
968 * we call the underlying element's property release hook, and
969 * then free the memory we allocated when we added the property.
971 static void array_element_release(Object
*obj
, const char *name
, void *opaque
)
973 ArrayElementProperty
*p
= opaque
;
975 p
->release(obj
, name
, opaque
);
981 static void set_prop_arraylen(Object
*obj
, Visitor
*v
, const char *name
,
982 void *opaque
, Error
**errp
)
984 /* Setter for the property which defines the length of a
985 * variable-sized property array. As well as actually setting the
986 * array-length field in the device struct, we have to create the
987 * array itself and dynamically add the corresponding properties.
989 DeviceState
*dev
= DEVICE(obj
);
990 Property
*prop
= opaque
;
991 uint32_t *alenptr
= qdev_get_prop_ptr(dev
, prop
);
992 void **arrayptr
= (void *)dev
+ prop
->arrayoffset
;
993 Error
*local_err
= NULL
;
995 const char *arrayname
;
999 qdev_prop_set_after_realize(dev
, name
, errp
);
1003 error_setg(errp
, "array size property %s may not be set more than once",
1007 visit_type_uint32(v
, name
, alenptr
, &local_err
);
1009 error_propagate(errp
, local_err
);
1016 /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix;
1017 * strip it off so we can get the name of the array itself.
1019 assert(strncmp(name
, PROP_ARRAY_LEN_PREFIX
,
1020 strlen(PROP_ARRAY_LEN_PREFIX
)) == 0);
1021 arrayname
= name
+ strlen(PROP_ARRAY_LEN_PREFIX
);
1023 /* Note that it is the responsibility of the individual device's deinit
1024 * to free the array proper.
1026 *arrayptr
= eltptr
= g_malloc0(*alenptr
* prop
->arrayfieldsize
);
1027 for (i
= 0; i
< *alenptr
; i
++, eltptr
+= prop
->arrayfieldsize
) {
1028 char *propname
= g_strdup_printf("%s[%d]", arrayname
, i
);
1029 ArrayElementProperty
*arrayprop
= g_new0(ArrayElementProperty
, 1);
1030 arrayprop
->release
= prop
->arrayinfo
->release
;
1031 arrayprop
->propname
= propname
;
1032 arrayprop
->prop
.info
= prop
->arrayinfo
;
1033 arrayprop
->prop
.name
= propname
;
1034 /* This ugly piece of pointer arithmetic sets up the offset so
1035 * that when the underlying get/set hooks call qdev_get_prop_ptr
1036 * they get the right answer despite the array element not actually
1037 * being inside the device struct.
1039 arrayprop
->prop
.offset
= eltptr
- (void *)dev
;
1040 assert(qdev_get_prop_ptr(dev
, &arrayprop
->prop
) == eltptr
);
1041 object_property_add(obj
, propname
,
1042 arrayprop
->prop
.info
->name
,
1043 arrayprop
->prop
.info
->get
,
1044 arrayprop
->prop
.info
->set
,
1045 array_element_release
,
1050 const PropertyInfo qdev_prop_arraylen
= {
1053 .set
= set_prop_arraylen
,
1054 .set_default_value
= set_default_value_uint
,
1057 /* --- public helpers --- */
1059 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
1064 while (props
->name
) {
1065 if (strcmp(props
->name
, name
) == 0) {
1073 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
1078 /* device properties */
1079 class = object_get_class(OBJECT(dev
));
1081 prop
= qdev_prop_walk(DEVICE_CLASS(class)->props_
, name
);
1085 class = object_class_get_parent(class);
1086 } while (class != object_class_by_name(TYPE_DEVICE
));
1091 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
1092 Property
*prop
, const char *value
)
1096 error_setg(errp
, "Property '%s.%s' can't take value '%s', it's in use",
1097 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1101 error_setg(errp
, QERR_PROPERTY_VALUE_BAD
,
1102 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1105 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
1106 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1113 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
1115 object_property_set_bool(OBJECT(dev
), value
, name
, &error_abort
);
1118 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
1120 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1123 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
1125 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1128 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
1130 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1133 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
1135 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1138 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
1140 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1143 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, const char *value
)
1145 object_property_set_str(OBJECT(dev
), value
, name
, &error_abort
);
1148 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
,
1149 const uint8_t *value
)
1151 char str
[2 * 6 + 5 + 1];
1152 snprintf(str
, sizeof(str
), "%02x:%02x:%02x:%02x:%02x:%02x",
1153 value
[0], value
[1], value
[2], value
[3], value
[4], value
[5]);
1155 object_property_set_str(OBJECT(dev
), str
, name
, &error_abort
);
1158 void qdev_prop_set_enum(DeviceState
*dev
, const char *name
, int value
)
1162 prop
= qdev_prop_find(dev
, name
);
1163 object_property_set_str(OBJECT(dev
),
1164 qapi_enum_lookup(prop
->info
->enum_table
, value
),
1165 name
, &error_abort
);
1168 static GPtrArray
*global_props(void)
1170 static GPtrArray
*gp
;
1173 gp
= g_ptr_array_new();
1179 void qdev_prop_register_global(GlobalProperty
*prop
)
1181 g_ptr_array_add(global_props(), prop
);
1184 int qdev_prop_check_globals(void)
1188 for (i
= 0; i
< global_props()->len
; i
++) {
1189 GlobalProperty
*prop
;
1193 prop
= g_ptr_array_index(global_props(), i
);
1197 oc
= object_class_by_name(prop
->driver
);
1198 oc
= object_class_dynamic_cast(oc
, TYPE_DEVICE
);
1200 warn_report("global %s.%s has invalid class name",
1201 prop
->driver
, prop
->property
);
1205 dc
= DEVICE_CLASS(oc
);
1206 if (!dc
->hotpluggable
&& !prop
->used
) {
1207 warn_report("global %s.%s=%s not used",
1208 prop
->driver
, prop
->property
, prop
->value
);
1216 void qdev_prop_set_globals(DeviceState
*dev
)
1218 object_apply_global_props(OBJECT(dev
), global_props(),
1219 dev
->hotplugged
? NULL
: &error_fatal
);
1222 /* --- 64bit unsigned int 'size' type --- */
1224 static void get_size(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1227 DeviceState
*dev
= DEVICE(obj
);
1228 Property
*prop
= opaque
;
1229 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1231 visit_type_size(v
, name
, ptr
, errp
);
1234 static void set_size(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1237 DeviceState
*dev
= DEVICE(obj
);
1238 Property
*prop
= opaque
;
1239 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1241 visit_type_size(v
, name
, ptr
, errp
);
1244 const PropertyInfo qdev_prop_size
= {
1248 .set_default_value
= set_default_value_uint
,
1251 /* --- object link property --- */
1253 static void create_link_property(ObjectClass
*oc
, Property
*prop
)
1255 object_class_property_add_link(oc
, prop
->name
, prop
->link_type
,
1257 qdev_prop_allow_set_link_before_realize
,
1258 OBJ_PROP_LINK_STRONG
);
1261 const PropertyInfo qdev_prop_link
= {
1263 .create
= create_link_property
,
1266 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
1268 const PropertyInfo qdev_prop_off_auto_pcibar
= {
1269 .name
= "OffAutoPCIBAR",
1270 .description
= "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
1271 .enum_table
= &OffAutoPCIBAR_lookup
,
1274 .set_default_value
= set_default_value_enum
,
1277 /* --- PCIELinkSpeed 2_5/5/8/16 -- */
1279 static void get_prop_pcielinkspeed(Object
*obj
, Visitor
*v
, const char *name
,
1280 void *opaque
, Error
**errp
)
1282 DeviceState
*dev
= DEVICE(obj
);
1283 Property
*prop
= opaque
;
1284 PCIExpLinkSpeed
*p
= qdev_get_prop_ptr(dev
, prop
);
1288 case QEMU_PCI_EXP_LNK_2_5GT
:
1289 speed
= PCIE_LINK_SPEED_2_5
;
1291 case QEMU_PCI_EXP_LNK_5GT
:
1292 speed
= PCIE_LINK_SPEED_5
;
1294 case QEMU_PCI_EXP_LNK_8GT
:
1295 speed
= PCIE_LINK_SPEED_8
;
1297 case QEMU_PCI_EXP_LNK_16GT
:
1298 speed
= PCIE_LINK_SPEED_16
;
1305 visit_type_enum(v
, prop
->name
, &speed
, prop
->info
->enum_table
, errp
);
1308 static void set_prop_pcielinkspeed(Object
*obj
, Visitor
*v
, const char *name
,
1309 void *opaque
, Error
**errp
)
1311 DeviceState
*dev
= DEVICE(obj
);
1312 Property
*prop
= opaque
;
1313 PCIExpLinkSpeed
*p
= qdev_get_prop_ptr(dev
, prop
);
1315 Error
*local_err
= NULL
;
1317 if (dev
->realized
) {
1318 qdev_prop_set_after_realize(dev
, name
, errp
);
1322 visit_type_enum(v
, prop
->name
, &speed
, prop
->info
->enum_table
, &local_err
);
1324 error_propagate(errp
, local_err
);
1329 case PCIE_LINK_SPEED_2_5
:
1330 *p
= QEMU_PCI_EXP_LNK_2_5GT
;
1332 case PCIE_LINK_SPEED_5
:
1333 *p
= QEMU_PCI_EXP_LNK_5GT
;
1335 case PCIE_LINK_SPEED_8
:
1336 *p
= QEMU_PCI_EXP_LNK_8GT
;
1338 case PCIE_LINK_SPEED_16
:
1339 *p
= QEMU_PCI_EXP_LNK_16GT
;
1347 const PropertyInfo qdev_prop_pcie_link_speed
= {
1348 .name
= "PCIELinkSpeed",
1349 .description
= "2_5/5/8/16",
1350 .enum_table
= &PCIELinkSpeed_lookup
,
1351 .get
= get_prop_pcielinkspeed
,
1352 .set
= set_prop_pcielinkspeed
,
1353 .set_default_value
= set_default_value_enum
,
1356 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
1358 static void get_prop_pcielinkwidth(Object
*obj
, Visitor
*v
, const char *name
,
1359 void *opaque
, Error
**errp
)
1361 DeviceState
*dev
= DEVICE(obj
);
1362 Property
*prop
= opaque
;
1363 PCIExpLinkWidth
*p
= qdev_get_prop_ptr(dev
, prop
);
1367 case QEMU_PCI_EXP_LNK_X1
:
1368 width
= PCIE_LINK_WIDTH_1
;
1370 case QEMU_PCI_EXP_LNK_X2
:
1371 width
= PCIE_LINK_WIDTH_2
;
1373 case QEMU_PCI_EXP_LNK_X4
:
1374 width
= PCIE_LINK_WIDTH_4
;
1376 case QEMU_PCI_EXP_LNK_X8
:
1377 width
= PCIE_LINK_WIDTH_8
;
1379 case QEMU_PCI_EXP_LNK_X12
:
1380 width
= PCIE_LINK_WIDTH_12
;
1382 case QEMU_PCI_EXP_LNK_X16
:
1383 width
= PCIE_LINK_WIDTH_16
;
1385 case QEMU_PCI_EXP_LNK_X32
:
1386 width
= PCIE_LINK_WIDTH_32
;
1393 visit_type_enum(v
, prop
->name
, &width
, prop
->info
->enum_table
, errp
);
1396 static void set_prop_pcielinkwidth(Object
*obj
, Visitor
*v
, const char *name
,
1397 void *opaque
, Error
**errp
)
1399 DeviceState
*dev
= DEVICE(obj
);
1400 Property
*prop
= opaque
;
1401 PCIExpLinkWidth
*p
= qdev_get_prop_ptr(dev
, prop
);
1403 Error
*local_err
= NULL
;
1405 if (dev
->realized
) {
1406 qdev_prop_set_after_realize(dev
, name
, errp
);
1410 visit_type_enum(v
, prop
->name
, &width
, prop
->info
->enum_table
, &local_err
);
1412 error_propagate(errp
, local_err
);
1417 case PCIE_LINK_WIDTH_1
:
1418 *p
= QEMU_PCI_EXP_LNK_X1
;
1420 case PCIE_LINK_WIDTH_2
:
1421 *p
= QEMU_PCI_EXP_LNK_X2
;
1423 case PCIE_LINK_WIDTH_4
:
1424 *p
= QEMU_PCI_EXP_LNK_X4
;
1426 case PCIE_LINK_WIDTH_8
:
1427 *p
= QEMU_PCI_EXP_LNK_X8
;
1429 case PCIE_LINK_WIDTH_12
:
1430 *p
= QEMU_PCI_EXP_LNK_X12
;
1432 case PCIE_LINK_WIDTH_16
:
1433 *p
= QEMU_PCI_EXP_LNK_X16
;
1435 case PCIE_LINK_WIDTH_32
:
1436 *p
= QEMU_PCI_EXP_LNK_X32
;
1444 const PropertyInfo qdev_prop_pcie_link_width
= {
1445 .name
= "PCIELinkWidth",
1446 .description
= "1/2/4/8/12/16/32",
1447 .enum_table
= &PCIELinkWidth_lookup
,
1448 .get
= get_prop_pcielinkwidth
,
1449 .set
= set_prop_pcielinkwidth
,
1450 .set_default_value
= set_default_value_enum
,