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"
17 #include "qemu/units.h"
18 #include "qemu/cutils.h"
20 void qdev_prop_set_after_realize(DeviceState
*dev
, const char *name
,
24 error_setg(errp
, "Attempt to set property '%s' on device '%s' "
25 "(type '%s') after it was realized", name
, dev
->id
,
26 object_get_typename(OBJECT(dev
)));
28 error_setg(errp
, "Attempt to set property '%s' on anonymous device "
29 "(type '%s') after it was realized", name
,
30 object_get_typename(OBJECT(dev
)));
34 void qdev_prop_allow_set_link_before_realize(const Object
*obj
,
36 Object
*val
, Error
**errp
)
38 DeviceState
*dev
= DEVICE(obj
);
41 error_setg(errp
, "Attempt to set link property '%s' on device '%s' "
42 "(type '%s') after it was realized",
43 name
, dev
->id
, object_get_typename(obj
));
47 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
54 static void get_enum(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
57 DeviceState
*dev
= DEVICE(obj
);
58 Property
*prop
= opaque
;
59 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
61 visit_type_enum(v
, prop
->name
, ptr
, prop
->info
->enum_table
, errp
);
64 static void set_enum(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
67 DeviceState
*dev
= DEVICE(obj
);
68 Property
*prop
= opaque
;
69 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
72 qdev_prop_set_after_realize(dev
, name
, errp
);
76 visit_type_enum(v
, prop
->name
, ptr
, prop
->info
->enum_table
, errp
);
79 static void set_default_value_enum(ObjectProperty
*op
, const Property
*prop
)
81 object_property_set_default_str(op
,
82 qapi_enum_lookup(prop
->info
->enum_table
, prop
->defval
.i
));
87 static uint32_t qdev_get_prop_mask(Property
*prop
)
89 assert(prop
->info
== &qdev_prop_bit
);
90 return 0x1 << prop
->bitnr
;
93 static void bit_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
95 uint32_t *p
= qdev_get_prop_ptr(dev
, props
);
96 uint32_t mask
= qdev_get_prop_mask(props
);
104 static void prop_get_bit(Object
*obj
, Visitor
*v
, const char *name
,
105 void *opaque
, Error
**errp
)
107 DeviceState
*dev
= DEVICE(obj
);
108 Property
*prop
= opaque
;
109 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
110 bool value
= (*p
& qdev_get_prop_mask(prop
)) != 0;
112 visit_type_bool(v
, name
, &value
, errp
);
115 static void prop_set_bit(Object
*obj
, Visitor
*v
, const char *name
,
116 void *opaque
, Error
**errp
)
118 DeviceState
*dev
= DEVICE(obj
);
119 Property
*prop
= opaque
;
123 qdev_prop_set_after_realize(dev
, name
, errp
);
127 if (!visit_type_bool(v
, name
, &value
, errp
)) {
130 bit_prop_set(dev
, prop
, value
);
133 static void set_default_value_bool(ObjectProperty
*op
, const Property
*prop
)
135 object_property_set_default_bool(op
, prop
->defval
.u
);
138 const PropertyInfo qdev_prop_bit
= {
140 .description
= "on/off",
143 .set_default_value
= set_default_value_bool
,
148 static uint64_t qdev_get_prop_mask64(Property
*prop
)
150 assert(prop
->info
== &qdev_prop_bit64
);
151 return 0x1ull
<< prop
->bitnr
;
154 static void bit64_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
156 uint64_t *p
= qdev_get_prop_ptr(dev
, props
);
157 uint64_t mask
= qdev_get_prop_mask64(props
);
165 static void prop_get_bit64(Object
*obj
, Visitor
*v
, const char *name
,
166 void *opaque
, Error
**errp
)
168 DeviceState
*dev
= DEVICE(obj
);
169 Property
*prop
= opaque
;
170 uint64_t *p
= qdev_get_prop_ptr(dev
, prop
);
171 bool value
= (*p
& qdev_get_prop_mask64(prop
)) != 0;
173 visit_type_bool(v
, name
, &value
, errp
);
176 static void prop_set_bit64(Object
*obj
, Visitor
*v
, const char *name
,
177 void *opaque
, Error
**errp
)
179 DeviceState
*dev
= DEVICE(obj
);
180 Property
*prop
= opaque
;
184 qdev_prop_set_after_realize(dev
, name
, errp
);
188 if (!visit_type_bool(v
, name
, &value
, errp
)) {
191 bit64_prop_set(dev
, prop
, value
);
194 const PropertyInfo qdev_prop_bit64
= {
196 .description
= "on/off",
197 .get
= prop_get_bit64
,
198 .set
= prop_set_bit64
,
199 .set_default_value
= set_default_value_bool
,
204 static void get_bool(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
207 DeviceState
*dev
= DEVICE(obj
);
208 Property
*prop
= opaque
;
209 bool *ptr
= qdev_get_prop_ptr(dev
, prop
);
211 visit_type_bool(v
, name
, ptr
, errp
);
214 static void set_bool(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
217 DeviceState
*dev
= DEVICE(obj
);
218 Property
*prop
= opaque
;
219 bool *ptr
= qdev_get_prop_ptr(dev
, prop
);
222 qdev_prop_set_after_realize(dev
, name
, errp
);
226 visit_type_bool(v
, name
, ptr
, errp
);
229 const PropertyInfo qdev_prop_bool
= {
233 .set_default_value
= set_default_value_bool
,
236 /* --- 8bit integer --- */
238 static void get_uint8(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
241 DeviceState
*dev
= DEVICE(obj
);
242 Property
*prop
= opaque
;
243 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
245 visit_type_uint8(v
, name
, ptr
, errp
);
248 static void set_uint8(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
251 DeviceState
*dev
= DEVICE(obj
);
252 Property
*prop
= opaque
;
253 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
256 qdev_prop_set_after_realize(dev
, name
, errp
);
260 visit_type_uint8(v
, name
, ptr
, errp
);
263 static void set_default_value_int(ObjectProperty
*op
, const Property
*prop
)
265 object_property_set_default_int(op
, prop
->defval
.i
);
268 static void set_default_value_uint(ObjectProperty
*op
, const Property
*prop
)
270 object_property_set_default_uint(op
, prop
->defval
.u
);
273 const PropertyInfo qdev_prop_uint8
= {
277 .set_default_value
= set_default_value_uint
,
280 /* --- 16bit integer --- */
282 static void get_uint16(Object
*obj
, Visitor
*v
, const char *name
,
283 void *opaque
, Error
**errp
)
285 DeviceState
*dev
= DEVICE(obj
);
286 Property
*prop
= opaque
;
287 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
289 visit_type_uint16(v
, name
, ptr
, errp
);
292 static void set_uint16(Object
*obj
, Visitor
*v
, const char *name
,
293 void *opaque
, Error
**errp
)
295 DeviceState
*dev
= DEVICE(obj
);
296 Property
*prop
= opaque
;
297 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
300 qdev_prop_set_after_realize(dev
, name
, errp
);
304 visit_type_uint16(v
, name
, ptr
, errp
);
307 const PropertyInfo qdev_prop_uint16
= {
311 .set_default_value
= set_default_value_uint
,
314 /* --- 32bit integer --- */
316 static void get_uint32(Object
*obj
, Visitor
*v
, const char *name
,
317 void *opaque
, Error
**errp
)
319 DeviceState
*dev
= DEVICE(obj
);
320 Property
*prop
= opaque
;
321 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
323 visit_type_uint32(v
, name
, ptr
, errp
);
326 static void set_uint32(Object
*obj
, Visitor
*v
, const char *name
,
327 void *opaque
, Error
**errp
)
329 DeviceState
*dev
= DEVICE(obj
);
330 Property
*prop
= opaque
;
331 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
334 qdev_prop_set_after_realize(dev
, name
, errp
);
338 visit_type_uint32(v
, name
, ptr
, errp
);
341 static void get_int32(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
344 DeviceState
*dev
= DEVICE(obj
);
345 Property
*prop
= opaque
;
346 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
348 visit_type_int32(v
, name
, ptr
, errp
);
351 static void set_int32(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
354 DeviceState
*dev
= DEVICE(obj
);
355 Property
*prop
= opaque
;
356 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
359 qdev_prop_set_after_realize(dev
, name
, errp
);
363 visit_type_int32(v
, name
, ptr
, errp
);
366 const PropertyInfo qdev_prop_uint32
= {
370 .set_default_value
= set_default_value_uint
,
373 const PropertyInfo qdev_prop_int32
= {
377 .set_default_value
= set_default_value_int
,
380 /* --- 64bit integer --- */
382 static void get_uint64(Object
*obj
, Visitor
*v
, const char *name
,
383 void *opaque
, Error
**errp
)
385 DeviceState
*dev
= DEVICE(obj
);
386 Property
*prop
= opaque
;
387 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
389 visit_type_uint64(v
, name
, ptr
, errp
);
392 static void set_uint64(Object
*obj
, Visitor
*v
, const char *name
,
393 void *opaque
, Error
**errp
)
395 DeviceState
*dev
= DEVICE(obj
);
396 Property
*prop
= opaque
;
397 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
400 qdev_prop_set_after_realize(dev
, name
, errp
);
404 visit_type_uint64(v
, name
, ptr
, errp
);
407 static void get_int64(Object
*obj
, Visitor
*v
, const char *name
,
408 void *opaque
, Error
**errp
)
410 DeviceState
*dev
= DEVICE(obj
);
411 Property
*prop
= opaque
;
412 int64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
414 visit_type_int64(v
, name
, ptr
, errp
);
417 static void set_int64(Object
*obj
, Visitor
*v
, const char *name
,
418 void *opaque
, Error
**errp
)
420 DeviceState
*dev
= DEVICE(obj
);
421 Property
*prop
= opaque
;
422 int64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
425 qdev_prop_set_after_realize(dev
, name
, errp
);
429 visit_type_int64(v
, name
, ptr
, errp
);
432 const PropertyInfo qdev_prop_uint64
= {
436 .set_default_value
= set_default_value_uint
,
439 const PropertyInfo qdev_prop_int64
= {
443 .set_default_value
= set_default_value_int
,
448 static void release_string(Object
*obj
, const char *name
, void *opaque
)
450 Property
*prop
= opaque
;
451 g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj
), prop
));
454 static void get_string(Object
*obj
, Visitor
*v
, const char *name
,
455 void *opaque
, Error
**errp
)
457 DeviceState
*dev
= DEVICE(obj
);
458 Property
*prop
= opaque
;
459 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
462 char *str
= (char *)"";
463 visit_type_str(v
, name
, &str
, errp
);
465 visit_type_str(v
, name
, ptr
, errp
);
469 static void set_string(Object
*obj
, Visitor
*v
, const char *name
,
470 void *opaque
, Error
**errp
)
472 DeviceState
*dev
= DEVICE(obj
);
473 Property
*prop
= opaque
;
474 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
478 qdev_prop_set_after_realize(dev
, name
, errp
);
482 if (!visit_type_str(v
, name
, &str
, errp
)) {
489 const PropertyInfo qdev_prop_string
= {
491 .release
= release_string
,
496 /* --- mac address --- */
499 * accepted syntax versions:
503 static void get_mac(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
506 DeviceState
*dev
= DEVICE(obj
);
507 Property
*prop
= opaque
;
508 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
509 char buffer
[2 * 6 + 5 + 1];
512 snprintf(buffer
, sizeof(buffer
), "%02x:%02x:%02x:%02x:%02x:%02x",
513 mac
->a
[0], mac
->a
[1], mac
->a
[2],
514 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
516 visit_type_str(v
, name
, &p
, errp
);
519 static void set_mac(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
522 DeviceState
*dev
= DEVICE(obj
);
523 Property
*prop
= opaque
;
524 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
529 qdev_prop_set_after_realize(dev
, name
, errp
);
533 if (!visit_type_str(v
, name
, &str
, errp
)) {
537 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
538 if (!qemu_isxdigit(str
[pos
])) {
541 if (!qemu_isxdigit(str
[pos
+1])) {
545 if (str
[pos
+2] != '\0') {
549 if (str
[pos
+2] != ':' && str
[pos
+2] != '-') {
553 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
559 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
563 const PropertyInfo qdev_prop_macaddr
= {
565 .description
= "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56",
570 /* --- Reserved Region --- */
574 * <low address>:<high address>:<type>
575 * where low/high addresses are uint64_t in hexadecimal
576 * and type is a non-negative decimal integer
578 static void get_reserved_region(Object
*obj
, Visitor
*v
, const char *name
,
579 void *opaque
, Error
**errp
)
581 DeviceState
*dev
= DEVICE(obj
);
582 Property
*prop
= opaque
;
583 ReservedRegion
*rr
= qdev_get_prop_ptr(dev
, prop
);
588 rc
= snprintf(buffer
, sizeof(buffer
), "0x%"PRIx64
":0x%"PRIx64
":%u",
589 rr
->low
, rr
->high
, rr
->type
);
590 assert(rc
< sizeof(buffer
));
592 visit_type_str(v
, name
, &p
, errp
);
595 static void set_reserved_region(Object
*obj
, Visitor
*v
, const char *name
,
596 void *opaque
, Error
**errp
)
598 DeviceState
*dev
= DEVICE(obj
);
599 Property
*prop
= opaque
;
600 ReservedRegion
*rr
= qdev_get_prop_ptr(dev
, prop
);
601 Error
*local_err
= NULL
;
607 qdev_prop_set_after_realize(dev
, name
, errp
);
611 visit_type_str(v
, name
, &str
, &local_err
);
613 error_propagate(errp
, local_err
);
617 ret
= qemu_strtou64(str
, &endptr
, 16, &rr
->low
);
619 error_setg(errp
, "start address of '%s'"
620 " must be a hexadecimal integer", name
);
623 if (*endptr
!= ':') {
624 goto separator_error
;
627 ret
= qemu_strtou64(endptr
+ 1, &endptr
, 16, &rr
->high
);
629 error_setg(errp
, "end address of '%s'"
630 " must be a hexadecimal integer", name
);
633 if (*endptr
!= ':') {
634 goto separator_error
;
637 ret
= qemu_strtoui(endptr
+ 1, &endptr
, 10, &rr
->type
);
639 error_setg(errp
, "type of '%s'"
640 " must be a non-negative decimal integer", name
);
645 error_setg(errp
, "reserved region fields must be separated with ':'");
651 const PropertyInfo qdev_prop_reserved_region
= {
652 .name
= "reserved_region",
653 .description
= "Reserved Region, example: 0xFEE00000:0xFEEFFFFF:0",
654 .get
= get_reserved_region
,
655 .set
= set_reserved_region
,
658 /* --- on/off/auto --- */
660 const PropertyInfo qdev_prop_on_off_auto
= {
662 .description
= "on/off/auto",
663 .enum_table
= &OnOffAuto_lookup
,
666 .set_default_value
= set_default_value_enum
,
669 /* --- lost tick policy --- */
671 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy
) != sizeof(int));
673 const PropertyInfo qdev_prop_losttickpolicy
= {
674 .name
= "LostTickPolicy",
675 .enum_table
= &LostTickPolicy_lookup
,
678 .set_default_value
= set_default_value_enum
,
681 /* --- Block device error handling policy --- */
683 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError
) != sizeof(int));
685 const PropertyInfo qdev_prop_blockdev_on_error
= {
686 .name
= "BlockdevOnError",
687 .description
= "Error handling policy, "
688 "report/ignore/enospc/stop/auto",
689 .enum_table
= &BlockdevOnError_lookup
,
692 .set_default_value
= set_default_value_enum
,
695 /* --- BIOS CHS translation */
697 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation
) != sizeof(int));
699 const PropertyInfo qdev_prop_bios_chs_trans
= {
700 .name
= "BiosAtaTranslation",
701 .description
= "Logical CHS translation algorithm, "
702 "auto/none/lba/large/rechs",
703 .enum_table
= &BiosAtaTranslation_lookup
,
706 .set_default_value
= set_default_value_enum
,
709 /* --- FDC default drive types */
711 const PropertyInfo qdev_prop_fdc_drive_type
= {
712 .name
= "FdcDriveType",
713 .description
= "FDC drive type, "
714 "144/288/120/none/auto",
715 .enum_table
= &FloppyDriveType_lookup
,
718 .set_default_value
= set_default_value_enum
,
721 /* --- MultiFDCompression --- */
723 const PropertyInfo qdev_prop_multifd_compression
= {
724 .name
= "MultiFDCompression",
725 .description
= "multifd_compression values, "
727 .enum_table
= &MultiFDCompression_lookup
,
730 .set_default_value
= set_default_value_enum
,
733 /* --- pci address --- */
736 * bus-local address, i.e. "$slot" or "$slot.$fn"
738 static void set_pci_devfn(Object
*obj
, Visitor
*v
, const char *name
,
739 void *opaque
, Error
**errp
)
741 DeviceState
*dev
= DEVICE(obj
);
742 Property
*prop
= opaque
;
743 int32_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
744 unsigned int slot
, fn
, n
;
748 qdev_prop_set_after_realize(dev
, name
, errp
);
752 if (!visit_type_str(v
, name
, &str
, NULL
)) {
753 if (!visit_type_int32(v
, name
, &value
, errp
)) {
756 if (value
< -1 || value
> 255) {
757 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
758 name
? name
: "null", "pci_devfn");
765 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
767 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
771 if (str
[n
] != '\0' || fn
> 7 || slot
> 31) {
774 *ptr
= slot
<< 3 | fn
;
779 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
783 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
,
786 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
789 return snprintf(dest
, len
, "<unset>");
791 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
795 const PropertyInfo qdev_prop_pci_devfn
= {
797 .description
= "Slot and optional function number, example: 06.0 or 06",
798 .print
= print_pci_devfn
,
800 .set
= set_pci_devfn
,
801 .set_default_value
= set_default_value_int
,
804 /* --- 32bit unsigned int 'size' type --- */
806 static void get_size32(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
809 DeviceState
*dev
= DEVICE(obj
);
810 Property
*prop
= opaque
;
811 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
812 uint64_t value
= *ptr
;
814 visit_type_size(v
, name
, &value
, errp
);
817 static void set_size32(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
820 DeviceState
*dev
= DEVICE(obj
);
821 Property
*prop
= opaque
;
822 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
826 qdev_prop_set_after_realize(dev
, name
, errp
);
830 if (!visit_type_size(v
, name
, &value
, errp
)) {
834 if (value
> UINT32_MAX
) {
836 "Property %s.%s doesn't take value %" PRIu64
838 dev
->id
? : "", name
, value
, UINT32_MAX
);
845 const PropertyInfo qdev_prop_size32
= {
849 .set_default_value
= set_default_value_uint
,
852 /* --- blocksize --- */
854 /* lower limit is sector size */
855 #define MIN_BLOCK_SIZE 512
856 #define MIN_BLOCK_SIZE_STR "512 B"
858 * upper limit is arbitrary, 2 MiB looks sufficient for all sensible uses, and
859 * matches qcow2 cluster size limit
861 #define MAX_BLOCK_SIZE (2 * MiB)
862 #define MAX_BLOCK_SIZE_STR "2 MiB"
864 static void set_blocksize(Object
*obj
, Visitor
*v
, const char *name
,
865 void *opaque
, Error
**errp
)
867 DeviceState
*dev
= DEVICE(obj
);
868 Property
*prop
= opaque
;
869 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
873 qdev_prop_set_after_realize(dev
, name
, errp
);
877 if (!visit_type_size(v
, name
, &value
, errp
)) {
880 /* value of 0 means "unset" */
881 if (value
&& (value
< MIN_BLOCK_SIZE
|| value
> MAX_BLOCK_SIZE
)) {
883 "Property %s.%s doesn't take value %" PRIu64
884 " (minimum: " MIN_BLOCK_SIZE_STR
885 ", maximum: " MAX_BLOCK_SIZE_STR
")",
886 dev
->id
? : "", name
, value
);
890 /* We rely on power-of-2 blocksizes for bitmasks */
891 if ((value
& (value
- 1)) != 0) {
893 "Property %s.%s doesn't take value '%" PRId64
"', it's not a power of 2",
894 dev
->id
?: "", name
, (int64_t)value
);
901 const PropertyInfo qdev_prop_blocksize
= {
903 .description
= "A power of two between " MIN_BLOCK_SIZE_STR
904 " and " MAX_BLOCK_SIZE_STR
,
906 .set
= set_blocksize
,
907 .set_default_value
= set_default_value_uint
,
910 /* --- pci host address --- */
912 static void get_pci_host_devaddr(Object
*obj
, Visitor
*v
, const char *name
,
913 void *opaque
, Error
**errp
)
915 DeviceState
*dev
= DEVICE(obj
);
916 Property
*prop
= opaque
;
917 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
918 char buffer
[] = "ffff:ff:ff.f";
923 * Catch "invalid" device reference from vfio-pci and allow the
924 * default buffer representing the non-existent device to be used.
926 if (~addr
->domain
|| ~addr
->bus
|| ~addr
->slot
|| ~addr
->function
) {
927 rc
= snprintf(buffer
, sizeof(buffer
), "%04x:%02x:%02x.%0d",
928 addr
->domain
, addr
->bus
, addr
->slot
, addr
->function
);
929 assert(rc
== sizeof(buffer
) - 1);
932 visit_type_str(v
, name
, &p
, errp
);
936 * Parse [<domain>:]<bus>:<slot>.<func>
937 * if <domain> is not supplied, it's assumed to be 0.
939 static void set_pci_host_devaddr(Object
*obj
, Visitor
*v
, const char *name
,
940 void *opaque
, Error
**errp
)
942 DeviceState
*dev
= DEVICE(obj
);
943 Property
*prop
= opaque
;
944 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
948 unsigned long dom
= 0, bus
= 0;
949 unsigned int slot
= 0, func
= 0;
952 qdev_prop_set_after_realize(dev
, name
, errp
);
956 if (!visit_type_str(v
, name
, &str
, errp
)) {
961 val
= strtoul(p
, &e
, 16);
962 if (e
== p
|| *e
!= ':') {
968 val
= strtoul(p
, &e
, 16);
976 val
= strtoul(p
, &e
, 16);
987 val
= strtoul(p
, &e
, 10);
993 if (dom
> 0xffff || bus
> 0xff || slot
> 0x1f || func
> 7) {
1004 addr
->function
= func
;
1010 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
1014 const PropertyInfo qdev_prop_pci_host_devaddr
= {
1016 .description
= "Address (bus/device/function) of "
1017 "the host device, example: 04:10.0",
1018 .get
= get_pci_host_devaddr
,
1019 .set
= set_pci_host_devaddr
,
1024 static void get_uuid(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1027 DeviceState
*dev
= DEVICE(obj
);
1028 Property
*prop
= opaque
;
1029 QemuUUID
*uuid
= qdev_get_prop_ptr(dev
, prop
);
1030 char buffer
[UUID_FMT_LEN
+ 1];
1033 qemu_uuid_unparse(uuid
, buffer
);
1035 visit_type_str(v
, name
, &p
, errp
);
1038 #define UUID_VALUE_AUTO "auto"
1040 static void set_uuid(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1043 DeviceState
*dev
= DEVICE(obj
);
1044 Property
*prop
= opaque
;
1045 QemuUUID
*uuid
= qdev_get_prop_ptr(dev
, prop
);
1048 if (dev
->realized
) {
1049 qdev_prop_set_after_realize(dev
, name
, errp
);
1053 if (!visit_type_str(v
, name
, &str
, errp
)) {
1057 if (!strcmp(str
, UUID_VALUE_AUTO
)) {
1058 qemu_uuid_generate(uuid
);
1059 } else if (qemu_uuid_parse(str
, uuid
) < 0) {
1060 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
1065 static void set_default_uuid_auto(ObjectProperty
*op
, const Property
*prop
)
1067 object_property_set_default_str(op
, UUID_VALUE_AUTO
);
1070 const PropertyInfo qdev_prop_uuid
= {
1072 .description
= "UUID (aka GUID) or \"" UUID_VALUE_AUTO
1073 "\" for random value (default)",
1076 .set_default_value
= set_default_uuid_auto
,
1079 /* --- support for array properties --- */
1081 /* Used as an opaque for the object properties we add for each
1082 * array element. Note that the struct Property must be first
1083 * in the struct so that a pointer to this works as the opaque
1084 * for the underlying element's property hooks as well as for
1085 * our own release callback.
1088 struct Property prop
;
1090 ObjectPropertyRelease
*release
;
1091 } ArrayElementProperty
;
1093 /* object property release callback for array element properties:
1094 * we call the underlying element's property release hook, and
1095 * then free the memory we allocated when we added the property.
1097 static void array_element_release(Object
*obj
, const char *name
, void *opaque
)
1099 ArrayElementProperty
*p
= opaque
;
1101 p
->release(obj
, name
, opaque
);
1103 g_free(p
->propname
);
1107 static void set_prop_arraylen(Object
*obj
, Visitor
*v
, const char *name
,
1108 void *opaque
, Error
**errp
)
1110 /* Setter for the property which defines the length of a
1111 * variable-sized property array. As well as actually setting the
1112 * array-length field in the device struct, we have to create the
1113 * array itself and dynamically add the corresponding properties.
1115 DeviceState
*dev
= DEVICE(obj
);
1116 Property
*prop
= opaque
;
1117 uint32_t *alenptr
= qdev_get_prop_ptr(dev
, prop
);
1118 void **arrayptr
= (void *)dev
+ prop
->arrayoffset
;
1120 const char *arrayname
;
1123 if (dev
->realized
) {
1124 qdev_prop_set_after_realize(dev
, name
, errp
);
1128 error_setg(errp
, "array size property %s may not be set more than once",
1132 if (!visit_type_uint32(v
, name
, alenptr
, errp
)) {
1139 /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix;
1140 * strip it off so we can get the name of the array itself.
1142 assert(strncmp(name
, PROP_ARRAY_LEN_PREFIX
,
1143 strlen(PROP_ARRAY_LEN_PREFIX
)) == 0);
1144 arrayname
= name
+ strlen(PROP_ARRAY_LEN_PREFIX
);
1146 /* Note that it is the responsibility of the individual device's deinit
1147 * to free the array proper.
1149 *arrayptr
= eltptr
= g_malloc0(*alenptr
* prop
->arrayfieldsize
);
1150 for (i
= 0; i
< *alenptr
; i
++, eltptr
+= prop
->arrayfieldsize
) {
1151 char *propname
= g_strdup_printf("%s[%d]", arrayname
, i
);
1152 ArrayElementProperty
*arrayprop
= g_new0(ArrayElementProperty
, 1);
1153 arrayprop
->release
= prop
->arrayinfo
->release
;
1154 arrayprop
->propname
= propname
;
1155 arrayprop
->prop
.info
= prop
->arrayinfo
;
1156 arrayprop
->prop
.name
= propname
;
1157 /* This ugly piece of pointer arithmetic sets up the offset so
1158 * that when the underlying get/set hooks call qdev_get_prop_ptr
1159 * they get the right answer despite the array element not actually
1160 * being inside the device struct.
1162 arrayprop
->prop
.offset
= eltptr
- (void *)dev
;
1163 assert(qdev_get_prop_ptr(dev
, &arrayprop
->prop
) == eltptr
);
1164 object_property_add(obj
, propname
,
1165 arrayprop
->prop
.info
->name
,
1166 arrayprop
->prop
.info
->get
,
1167 arrayprop
->prop
.info
->set
,
1168 array_element_release
,
1173 const PropertyInfo qdev_prop_arraylen
= {
1176 .set
= set_prop_arraylen
,
1177 .set_default_value
= set_default_value_uint
,
1180 /* --- public helpers --- */
1182 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
1187 while (props
->name
) {
1188 if (strcmp(props
->name
, name
) == 0) {
1196 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
1201 /* device properties */
1202 class = object_get_class(OBJECT(dev
));
1204 prop
= qdev_prop_walk(DEVICE_CLASS(class)->props_
, name
);
1208 class = object_class_get_parent(class);
1209 } while (class != object_class_by_name(TYPE_DEVICE
));
1214 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
1215 Property
*prop
, const char *value
)
1219 error_setg(errp
, "Property '%s.%s' can't take value '%s', it's in use",
1220 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1224 error_setg(errp
, QERR_PROPERTY_VALUE_BAD
,
1225 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1228 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
1229 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1236 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
1238 object_property_set_bool(OBJECT(dev
), name
, value
, &error_abort
);
1241 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
1243 object_property_set_int(OBJECT(dev
), name
, value
, &error_abort
);
1246 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
1248 object_property_set_int(OBJECT(dev
), name
, value
, &error_abort
);
1251 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
1253 object_property_set_int(OBJECT(dev
), name
, value
, &error_abort
);
1256 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
1258 object_property_set_int(OBJECT(dev
), name
, value
, &error_abort
);
1261 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
1263 object_property_set_int(OBJECT(dev
), name
, value
, &error_abort
);
1266 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, const char *value
)
1268 object_property_set_str(OBJECT(dev
), name
, value
, &error_abort
);
1271 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
,
1272 const uint8_t *value
)
1274 char str
[2 * 6 + 5 + 1];
1275 snprintf(str
, sizeof(str
), "%02x:%02x:%02x:%02x:%02x:%02x",
1276 value
[0], value
[1], value
[2], value
[3], value
[4], value
[5]);
1278 object_property_set_str(OBJECT(dev
), name
, str
, &error_abort
);
1281 void qdev_prop_set_enum(DeviceState
*dev
, const char *name
, int value
)
1285 prop
= qdev_prop_find(dev
, name
);
1286 object_property_set_str(OBJECT(dev
), name
,
1287 qapi_enum_lookup(prop
->info
->enum_table
, value
),
1291 static GPtrArray
*global_props(void)
1293 static GPtrArray
*gp
;
1296 gp
= g_ptr_array_new();
1302 void qdev_prop_register_global(GlobalProperty
*prop
)
1304 g_ptr_array_add(global_props(), prop
);
1307 const GlobalProperty
*qdev_find_global_prop(DeviceState
*dev
,
1310 GPtrArray
*props
= global_props();
1311 const GlobalProperty
*p
;
1314 for (i
= 0; i
< props
->len
; i
++) {
1315 p
= g_ptr_array_index(props
, i
);
1316 if (object_dynamic_cast(OBJECT(dev
), p
->driver
)
1317 && !strcmp(p
->property
, name
)) {
1324 int qdev_prop_check_globals(void)
1328 for (i
= 0; i
< global_props()->len
; i
++) {
1329 GlobalProperty
*prop
;
1333 prop
= g_ptr_array_index(global_props(), i
);
1337 oc
= object_class_by_name(prop
->driver
);
1338 oc
= object_class_dynamic_cast(oc
, TYPE_DEVICE
);
1340 warn_report("global %s.%s has invalid class name",
1341 prop
->driver
, prop
->property
);
1345 dc
= DEVICE_CLASS(oc
);
1346 if (!dc
->hotpluggable
&& !prop
->used
) {
1347 warn_report("global %s.%s=%s not used",
1348 prop
->driver
, prop
->property
, prop
->value
);
1356 void qdev_prop_set_globals(DeviceState
*dev
)
1358 object_apply_global_props(OBJECT(dev
), global_props(),
1359 dev
->hotplugged
? NULL
: &error_fatal
);
1362 /* --- 64bit unsigned int 'size' type --- */
1364 static void get_size(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1367 DeviceState
*dev
= DEVICE(obj
);
1368 Property
*prop
= opaque
;
1369 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1371 visit_type_size(v
, name
, ptr
, errp
);
1374 static void set_size(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1377 DeviceState
*dev
= DEVICE(obj
);
1378 Property
*prop
= opaque
;
1379 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1381 visit_type_size(v
, name
, ptr
, errp
);
1384 const PropertyInfo qdev_prop_size
= {
1388 .set_default_value
= set_default_value_uint
,
1391 /* --- object link property --- */
1393 static void create_link_property(ObjectClass
*oc
, Property
*prop
)
1395 object_class_property_add_link(oc
, prop
->name
, prop
->link_type
,
1397 qdev_prop_allow_set_link_before_realize
,
1398 OBJ_PROP_LINK_STRONG
);
1401 const PropertyInfo qdev_prop_link
= {
1403 .create
= create_link_property
,
1406 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
1408 const PropertyInfo qdev_prop_off_auto_pcibar
= {
1409 .name
= "OffAutoPCIBAR",
1410 .description
= "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
1411 .enum_table
= &OffAutoPCIBAR_lookup
,
1414 .set_default_value
= set_default_value_enum
,
1417 /* --- PCIELinkSpeed 2_5/5/8/16 -- */
1419 static void get_prop_pcielinkspeed(Object
*obj
, Visitor
*v
, const char *name
,
1420 void *opaque
, Error
**errp
)
1422 DeviceState
*dev
= DEVICE(obj
);
1423 Property
*prop
= opaque
;
1424 PCIExpLinkSpeed
*p
= qdev_get_prop_ptr(dev
, prop
);
1428 case QEMU_PCI_EXP_LNK_2_5GT
:
1429 speed
= PCIE_LINK_SPEED_2_5
;
1431 case QEMU_PCI_EXP_LNK_5GT
:
1432 speed
= PCIE_LINK_SPEED_5
;
1434 case QEMU_PCI_EXP_LNK_8GT
:
1435 speed
= PCIE_LINK_SPEED_8
;
1437 case QEMU_PCI_EXP_LNK_16GT
:
1438 speed
= PCIE_LINK_SPEED_16
;
1445 visit_type_enum(v
, prop
->name
, &speed
, prop
->info
->enum_table
, errp
);
1448 static void set_prop_pcielinkspeed(Object
*obj
, Visitor
*v
, const char *name
,
1449 void *opaque
, Error
**errp
)
1451 DeviceState
*dev
= DEVICE(obj
);
1452 Property
*prop
= opaque
;
1453 PCIExpLinkSpeed
*p
= qdev_get_prop_ptr(dev
, prop
);
1456 if (dev
->realized
) {
1457 qdev_prop_set_after_realize(dev
, name
, errp
);
1461 if (!visit_type_enum(v
, prop
->name
, &speed
, prop
->info
->enum_table
,
1467 case PCIE_LINK_SPEED_2_5
:
1468 *p
= QEMU_PCI_EXP_LNK_2_5GT
;
1470 case PCIE_LINK_SPEED_5
:
1471 *p
= QEMU_PCI_EXP_LNK_5GT
;
1473 case PCIE_LINK_SPEED_8
:
1474 *p
= QEMU_PCI_EXP_LNK_8GT
;
1476 case PCIE_LINK_SPEED_16
:
1477 *p
= QEMU_PCI_EXP_LNK_16GT
;
1485 const PropertyInfo qdev_prop_pcie_link_speed
= {
1486 .name
= "PCIELinkSpeed",
1487 .description
= "2_5/5/8/16",
1488 .enum_table
= &PCIELinkSpeed_lookup
,
1489 .get
= get_prop_pcielinkspeed
,
1490 .set
= set_prop_pcielinkspeed
,
1491 .set_default_value
= set_default_value_enum
,
1494 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
1496 static void get_prop_pcielinkwidth(Object
*obj
, Visitor
*v
, const char *name
,
1497 void *opaque
, Error
**errp
)
1499 DeviceState
*dev
= DEVICE(obj
);
1500 Property
*prop
= opaque
;
1501 PCIExpLinkWidth
*p
= qdev_get_prop_ptr(dev
, prop
);
1505 case QEMU_PCI_EXP_LNK_X1
:
1506 width
= PCIE_LINK_WIDTH_1
;
1508 case QEMU_PCI_EXP_LNK_X2
:
1509 width
= PCIE_LINK_WIDTH_2
;
1511 case QEMU_PCI_EXP_LNK_X4
:
1512 width
= PCIE_LINK_WIDTH_4
;
1514 case QEMU_PCI_EXP_LNK_X8
:
1515 width
= PCIE_LINK_WIDTH_8
;
1517 case QEMU_PCI_EXP_LNK_X12
:
1518 width
= PCIE_LINK_WIDTH_12
;
1520 case QEMU_PCI_EXP_LNK_X16
:
1521 width
= PCIE_LINK_WIDTH_16
;
1523 case QEMU_PCI_EXP_LNK_X32
:
1524 width
= PCIE_LINK_WIDTH_32
;
1531 visit_type_enum(v
, prop
->name
, &width
, prop
->info
->enum_table
, errp
);
1534 static void set_prop_pcielinkwidth(Object
*obj
, Visitor
*v
, const char *name
,
1535 void *opaque
, Error
**errp
)
1537 DeviceState
*dev
= DEVICE(obj
);
1538 Property
*prop
= opaque
;
1539 PCIExpLinkWidth
*p
= qdev_get_prop_ptr(dev
, prop
);
1542 if (dev
->realized
) {
1543 qdev_prop_set_after_realize(dev
, name
, errp
);
1547 if (!visit_type_enum(v
, prop
->name
, &width
, prop
->info
->enum_table
,
1553 case PCIE_LINK_WIDTH_1
:
1554 *p
= QEMU_PCI_EXP_LNK_X1
;
1556 case PCIE_LINK_WIDTH_2
:
1557 *p
= QEMU_PCI_EXP_LNK_X2
;
1559 case PCIE_LINK_WIDTH_4
:
1560 *p
= QEMU_PCI_EXP_LNK_X4
;
1562 case PCIE_LINK_WIDTH_8
:
1563 *p
= QEMU_PCI_EXP_LNK_X8
;
1565 case PCIE_LINK_WIDTH_12
:
1566 *p
= QEMU_PCI_EXP_LNK_X12
;
1568 case PCIE_LINK_WIDTH_16
:
1569 *p
= QEMU_PCI_EXP_LNK_X16
;
1571 case PCIE_LINK_WIDTH_32
:
1572 *p
= QEMU_PCI_EXP_LNK_X32
;
1580 const PropertyInfo qdev_prop_pcie_link_width
= {
1581 .name
= "PCIELinkWidth",
1582 .description
= "1/2/4/8/12/16/32",
1583 .enum_table
= &PCIELinkWidth_lookup
,
1584 .get
= get_prop_pcielinkwidth
,
1585 .set
= set_prop_pcielinkwidth
,
1586 .set_default_value
= set_default_value_enum
,