1 #include "qemu/osdep.h"
4 #include "qapi/error.h"
5 #include "hw/pci/pci.h"
6 #include "qapi/qmp/qerror.h"
7 #include "qemu/error-report.h"
8 #include "sysemu/block-backend.h"
9 #include "hw/block/block.h"
11 #include "qapi/visitor.h"
12 #include "chardev/char.h"
14 void qdev_prop_set_after_realize(DeviceState
*dev
, const char *name
,
18 error_setg(errp
, "Attempt to set property '%s' on device '%s' "
19 "(type '%s') after it was realized", name
, dev
->id
,
20 object_get_typename(OBJECT(dev
)));
22 error_setg(errp
, "Attempt to set property '%s' on anonymous device "
23 "(type '%s') after it was realized", name
,
24 object_get_typename(OBJECT(dev
)));
28 void qdev_prop_allow_set_link_before_realize(const Object
*obj
,
30 Object
*val
, Error
**errp
)
32 DeviceState
*dev
= DEVICE(obj
);
35 error_setg(errp
, "Attempt to set link property '%s' on device '%s' "
36 "(type '%s') after it was realized",
37 name
, dev
->id
, object_get_typename(obj
));
41 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
48 static void get_enum(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
51 DeviceState
*dev
= DEVICE(obj
);
52 Property
*prop
= opaque
;
53 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
55 visit_type_enum(v
, prop
->name
, ptr
, prop
->info
->enum_table
, errp
);
58 static void set_enum(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
61 DeviceState
*dev
= DEVICE(obj
);
62 Property
*prop
= opaque
;
63 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
66 qdev_prop_set_after_realize(dev
, name
, errp
);
70 visit_type_enum(v
, prop
->name
, ptr
, prop
->info
->enum_table
, errp
);
73 static void set_default_value_enum(Object
*obj
, const Property
*prop
)
75 object_property_set_str(obj
, prop
->info
->enum_table
[prop
->defval
.i
],
76 prop
->name
, &error_abort
);
81 static uint32_t qdev_get_prop_mask(Property
*prop
)
83 assert(prop
->info
== &qdev_prop_bit
);
84 return 0x1 << prop
->bitnr
;
87 static void bit_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
89 uint32_t *p
= qdev_get_prop_ptr(dev
, props
);
90 uint32_t mask
= qdev_get_prop_mask(props
);
98 static void prop_get_bit(Object
*obj
, Visitor
*v
, const char *name
,
99 void *opaque
, Error
**errp
)
101 DeviceState
*dev
= DEVICE(obj
);
102 Property
*prop
= opaque
;
103 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
104 bool value
= (*p
& qdev_get_prop_mask(prop
)) != 0;
106 visit_type_bool(v
, name
, &value
, errp
);
109 static void prop_set_bit(Object
*obj
, Visitor
*v
, const char *name
,
110 void *opaque
, Error
**errp
)
112 DeviceState
*dev
= DEVICE(obj
);
113 Property
*prop
= opaque
;
114 Error
*local_err
= NULL
;
118 qdev_prop_set_after_realize(dev
, name
, errp
);
122 visit_type_bool(v
, name
, &value
, &local_err
);
124 error_propagate(errp
, local_err
);
127 bit_prop_set(dev
, prop
, value
);
130 static void set_default_value_bool(Object
*obj
, const Property
*prop
)
132 object_property_set_bool(obj
, prop
->defval
.u
, prop
->name
, &error_abort
);
135 const PropertyInfo qdev_prop_bit
= {
137 .description
= "on/off",
140 .set_default_value
= set_default_value_bool
,
145 static uint64_t qdev_get_prop_mask64(Property
*prop
)
147 assert(prop
->info
== &qdev_prop_bit64
);
148 return 0x1ull
<< prop
->bitnr
;
151 static void bit64_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
153 uint64_t *p
= qdev_get_prop_ptr(dev
, props
);
154 uint64_t mask
= qdev_get_prop_mask64(props
);
162 static void prop_get_bit64(Object
*obj
, Visitor
*v
, const char *name
,
163 void *opaque
, Error
**errp
)
165 DeviceState
*dev
= DEVICE(obj
);
166 Property
*prop
= opaque
;
167 uint64_t *p
= qdev_get_prop_ptr(dev
, prop
);
168 bool value
= (*p
& qdev_get_prop_mask64(prop
)) != 0;
170 visit_type_bool(v
, name
, &value
, errp
);
173 static void prop_set_bit64(Object
*obj
, Visitor
*v
, const char *name
,
174 void *opaque
, Error
**errp
)
176 DeviceState
*dev
= DEVICE(obj
);
177 Property
*prop
= opaque
;
178 Error
*local_err
= NULL
;
182 qdev_prop_set_after_realize(dev
, name
, errp
);
186 visit_type_bool(v
, name
, &value
, &local_err
);
188 error_propagate(errp
, local_err
);
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(Object
*obj
, const Property
*prop
)
265 object_property_set_int(obj
, prop
->defval
.i
, prop
->name
, &error_abort
);
268 static void set_default_value_uint(Object
*obj
, const Property
*prop
)
270 object_property_set_uint(obj
, prop
->defval
.u
, prop
->name
, &error_abort
);
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
);
475 Error
*local_err
= NULL
;
479 qdev_prop_set_after_realize(dev
, name
, errp
);
483 visit_type_str(v
, name
, &str
, &local_err
);
485 error_propagate(errp
, local_err
);
492 const PropertyInfo qdev_prop_string
= {
494 .release
= release_string
,
499 /* --- pointer --- */
501 /* Not a proper property, just for dirty hacks. TODO Remove it! */
502 const PropertyInfo qdev_prop_ptr
= {
506 /* --- mac address --- */
509 * accepted syntax versions:
513 static void get_mac(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
516 DeviceState
*dev
= DEVICE(obj
);
517 Property
*prop
= opaque
;
518 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
519 char buffer
[2 * 6 + 5 + 1];
522 snprintf(buffer
, sizeof(buffer
), "%02x:%02x:%02x:%02x:%02x:%02x",
523 mac
->a
[0], mac
->a
[1], mac
->a
[2],
524 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
526 visit_type_str(v
, name
, &p
, errp
);
529 static void set_mac(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
532 DeviceState
*dev
= DEVICE(obj
);
533 Property
*prop
= opaque
;
534 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
535 Error
*local_err
= NULL
;
540 qdev_prop_set_after_realize(dev
, name
, errp
);
544 visit_type_str(v
, name
, &str
, &local_err
);
546 error_propagate(errp
, local_err
);
550 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
551 if (!qemu_isxdigit(str
[pos
])) {
554 if (!qemu_isxdigit(str
[pos
+1])) {
558 if (str
[pos
+2] != '\0') {
562 if (str
[pos
+2] != ':' && str
[pos
+2] != '-') {
566 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
572 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
576 const PropertyInfo qdev_prop_macaddr
= {
578 .description
= "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56",
583 /* --- on/off/auto --- */
585 const PropertyInfo qdev_prop_on_off_auto
= {
587 .description
= "on/off/auto",
588 .enum_table
= OnOffAuto_lookup
,
591 .set_default_value
= set_default_value_enum
,
594 /* --- lost tick policy --- */
596 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy
) != sizeof(int));
598 const PropertyInfo qdev_prop_losttickpolicy
= {
599 .name
= "LostTickPolicy",
600 .enum_table
= LostTickPolicy_lookup
,
603 .set_default_value
= set_default_value_enum
,
606 /* --- Block device error handling policy --- */
608 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError
) != sizeof(int));
610 const PropertyInfo qdev_prop_blockdev_on_error
= {
611 .name
= "BlockdevOnError",
612 .description
= "Error handling policy, "
613 "report/ignore/enospc/stop/auto",
614 .enum_table
= BlockdevOnError_lookup
,
617 .set_default_value
= set_default_value_enum
,
620 /* --- BIOS CHS translation */
622 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation
) != sizeof(int));
624 const PropertyInfo qdev_prop_bios_chs_trans
= {
625 .name
= "BiosAtaTranslation",
626 .description
= "Logical CHS translation algorithm, "
627 "auto/none/lba/large/rechs",
628 .enum_table
= BiosAtaTranslation_lookup
,
631 .set_default_value
= set_default_value_enum
,
634 /* --- FDC default drive types */
636 const PropertyInfo qdev_prop_fdc_drive_type
= {
637 .name
= "FdcDriveType",
638 .description
= "FDC drive type, "
639 "144/288/120/none/auto",
640 .enum_table
= FloppyDriveType_lookup
,
643 .set_default_value
= set_default_value_enum
,
646 /* --- pci address --- */
649 * bus-local address, i.e. "$slot" or "$slot.$fn"
651 static void set_pci_devfn(Object
*obj
, Visitor
*v
, const char *name
,
652 void *opaque
, Error
**errp
)
654 DeviceState
*dev
= DEVICE(obj
);
655 Property
*prop
= opaque
;
656 int32_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
657 unsigned int slot
, fn
, n
;
658 Error
*local_err
= NULL
;
662 qdev_prop_set_after_realize(dev
, name
, errp
);
666 visit_type_str(v
, name
, &str
, &local_err
);
668 error_free(local_err
);
670 visit_type_int32(v
, name
, &value
, &local_err
);
672 error_propagate(errp
, local_err
);
673 } else if (value
< -1 || value
> 255) {
674 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
675 name
? name
: "null", "pci_devfn");
682 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
684 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
688 if (str
[n
] != '\0' || fn
> 7 || slot
> 31) {
691 *ptr
= slot
<< 3 | fn
;
696 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
700 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
,
703 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
706 return snprintf(dest
, len
, "<unset>");
708 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
712 const PropertyInfo qdev_prop_pci_devfn
= {
714 .description
= "Slot and optional function number, example: 06.0 or 06",
715 .print
= print_pci_devfn
,
717 .set
= set_pci_devfn
,
718 .set_default_value
= set_default_value_int
,
721 /* --- blocksize --- */
723 static void set_blocksize(Object
*obj
, Visitor
*v
, const char *name
,
724 void *opaque
, Error
**errp
)
726 DeviceState
*dev
= DEVICE(obj
);
727 Property
*prop
= opaque
;
728 uint16_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
729 Error
*local_err
= NULL
;
730 const int64_t min
= 512;
731 const int64_t max
= 32768;
734 qdev_prop_set_after_realize(dev
, name
, errp
);
738 visit_type_uint16(v
, name
, &value
, &local_err
);
740 error_propagate(errp
, local_err
);
743 /* value of 0 means "unset" */
744 if (value
&& (value
< min
|| value
> max
)) {
745 error_setg(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
746 dev
->id
? : "", name
, (int64_t)value
, min
, max
);
750 /* We rely on power-of-2 blocksizes for bitmasks */
751 if ((value
& (value
- 1)) != 0) {
753 "Property %s.%s doesn't take value '%" PRId64
"', it's not a power of 2",
754 dev
->id
?: "", name
, (int64_t)value
);
761 const PropertyInfo qdev_prop_blocksize
= {
763 .description
= "A power of two between 512 and 32768",
765 .set
= set_blocksize
,
766 .set_default_value
= set_default_value_uint
,
769 /* --- pci host address --- */
771 static void get_pci_host_devaddr(Object
*obj
, Visitor
*v
, const char *name
,
772 void *opaque
, Error
**errp
)
774 DeviceState
*dev
= DEVICE(obj
);
775 Property
*prop
= opaque
;
776 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
777 char buffer
[] = "ffff:ff:ff.f";
782 * Catch "invalid" device reference from vfio-pci and allow the
783 * default buffer representing the non-existent device to be used.
785 if (~addr
->domain
|| ~addr
->bus
|| ~addr
->slot
|| ~addr
->function
) {
786 rc
= snprintf(buffer
, sizeof(buffer
), "%04x:%02x:%02x.%0d",
787 addr
->domain
, addr
->bus
, addr
->slot
, addr
->function
);
788 assert(rc
== sizeof(buffer
) - 1);
791 visit_type_str(v
, name
, &p
, errp
);
795 * Parse [<domain>:]<bus>:<slot>.<func>
796 * if <domain> is not supplied, it's assumed to be 0.
798 static void set_pci_host_devaddr(Object
*obj
, Visitor
*v
, const char *name
,
799 void *opaque
, Error
**errp
)
801 DeviceState
*dev
= DEVICE(obj
);
802 Property
*prop
= opaque
;
803 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
804 Error
*local_err
= NULL
;
808 unsigned long dom
= 0, bus
= 0;
809 unsigned int slot
= 0, func
= 0;
812 qdev_prop_set_after_realize(dev
, name
, errp
);
816 visit_type_str(v
, name
, &str
, &local_err
);
818 error_propagate(errp
, local_err
);
823 val
= strtoul(p
, &e
, 16);
824 if (e
== p
|| *e
!= ':') {
830 val
= strtoul(p
, &e
, 16);
838 val
= strtoul(p
, &e
, 16);
849 val
= strtoul(p
, &e
, 10);
855 if (dom
> 0xffff || bus
> 0xff || slot
> 0x1f || func
> 7) {
866 addr
->function
= func
;
872 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
876 const PropertyInfo qdev_prop_pci_host_devaddr
= {
878 .description
= "Address (bus/device/function) of "
879 "the host device, example: 04:10.0",
880 .get
= get_pci_host_devaddr
,
881 .set
= set_pci_host_devaddr
,
884 /* --- support for array properties --- */
886 /* Used as an opaque for the object properties we add for each
887 * array element. Note that the struct Property must be first
888 * in the struct so that a pointer to this works as the opaque
889 * for the underlying element's property hooks as well as for
890 * our own release callback.
893 struct Property prop
;
895 ObjectPropertyRelease
*release
;
896 } ArrayElementProperty
;
898 /* object property release callback for array element properties:
899 * we call the underlying element's property release hook, and
900 * then free the memory we allocated when we added the property.
902 static void array_element_release(Object
*obj
, const char *name
, void *opaque
)
904 ArrayElementProperty
*p
= opaque
;
906 p
->release(obj
, name
, opaque
);
912 static void set_prop_arraylen(Object
*obj
, Visitor
*v
, const char *name
,
913 void *opaque
, Error
**errp
)
915 /* Setter for the property which defines the length of a
916 * variable-sized property array. As well as actually setting the
917 * array-length field in the device struct, we have to create the
918 * array itself and dynamically add the corresponding properties.
920 DeviceState
*dev
= DEVICE(obj
);
921 Property
*prop
= opaque
;
922 uint32_t *alenptr
= qdev_get_prop_ptr(dev
, prop
);
923 void **arrayptr
= (void *)dev
+ prop
->arrayoffset
;
924 Error
*local_err
= NULL
;
926 const char *arrayname
;
930 qdev_prop_set_after_realize(dev
, name
, errp
);
934 error_setg(errp
, "array size property %s may not be set more than once",
938 visit_type_uint32(v
, name
, alenptr
, &local_err
);
940 error_propagate(errp
, local_err
);
947 /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix;
948 * strip it off so we can get the name of the array itself.
950 assert(strncmp(name
, PROP_ARRAY_LEN_PREFIX
,
951 strlen(PROP_ARRAY_LEN_PREFIX
)) == 0);
952 arrayname
= name
+ strlen(PROP_ARRAY_LEN_PREFIX
);
954 /* Note that it is the responsibility of the individual device's deinit
955 * to free the array proper.
957 *arrayptr
= eltptr
= g_malloc0(*alenptr
* prop
->arrayfieldsize
);
958 for (i
= 0; i
< *alenptr
; i
++, eltptr
+= prop
->arrayfieldsize
) {
959 char *propname
= g_strdup_printf("%s[%d]", arrayname
, i
);
960 ArrayElementProperty
*arrayprop
= g_new0(ArrayElementProperty
, 1);
961 arrayprop
->release
= prop
->arrayinfo
->release
;
962 arrayprop
->propname
= propname
;
963 arrayprop
->prop
.info
= prop
->arrayinfo
;
964 arrayprop
->prop
.name
= propname
;
965 /* This ugly piece of pointer arithmetic sets up the offset so
966 * that when the underlying get/set hooks call qdev_get_prop_ptr
967 * they get the right answer despite the array element not actually
968 * being inside the device struct.
970 arrayprop
->prop
.offset
= eltptr
- (void *)dev
;
971 assert(qdev_get_prop_ptr(dev
, &arrayprop
->prop
) == eltptr
);
972 object_property_add(obj
, propname
,
973 arrayprop
->prop
.info
->name
,
974 arrayprop
->prop
.info
->get
,
975 arrayprop
->prop
.info
->set
,
976 array_element_release
,
977 arrayprop
, &local_err
);
979 error_propagate(errp
, local_err
);
985 const PropertyInfo qdev_prop_arraylen
= {
988 .set
= set_prop_arraylen
,
989 .set_default_value
= set_default_value_uint
,
992 /* --- public helpers --- */
994 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
999 while (props
->name
) {
1000 if (strcmp(props
->name
, name
) == 0) {
1008 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
1013 /* device properties */
1014 class = object_get_class(OBJECT(dev
));
1016 prop
= qdev_prop_walk(DEVICE_CLASS(class)->props
, name
);
1020 class = object_class_get_parent(class);
1021 } while (class != object_class_by_name(TYPE_DEVICE
));
1026 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
1027 Property
*prop
, const char *value
)
1031 error_setg(errp
, "Property '%s.%s' can't take value '%s', it's in use",
1032 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1036 error_setg(errp
, QERR_PROPERTY_VALUE_BAD
,
1037 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1040 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
1041 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1048 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
1050 object_property_set_bool(OBJECT(dev
), value
, name
, &error_abort
);
1053 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
1055 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1058 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
1060 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1063 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
1065 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1068 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
1070 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1073 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
1075 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1078 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, const char *value
)
1080 object_property_set_str(OBJECT(dev
), value
, name
, &error_abort
);
1083 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
,
1084 const uint8_t *value
)
1086 char str
[2 * 6 + 5 + 1];
1087 snprintf(str
, sizeof(str
), "%02x:%02x:%02x:%02x:%02x:%02x",
1088 value
[0], value
[1], value
[2], value
[3], value
[4], value
[5]);
1090 object_property_set_str(OBJECT(dev
), str
, name
, &error_abort
);
1093 void qdev_prop_set_enum(DeviceState
*dev
, const char *name
, int value
)
1097 prop
= qdev_prop_find(dev
, name
);
1098 object_property_set_str(OBJECT(dev
), prop
->info
->enum_table
[value
],
1099 name
, &error_abort
);
1102 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
1107 prop
= qdev_prop_find(dev
, name
);
1108 assert(prop
&& prop
->info
== &qdev_prop_ptr
);
1109 ptr
= qdev_get_prop_ptr(dev
, prop
);
1113 static GList
*global_props
;
1115 void qdev_prop_register_global(GlobalProperty
*prop
)
1117 global_props
= g_list_append(global_props
, prop
);
1120 void register_compat_prop(const char *driver
,
1121 const char *property
,
1124 GlobalProperty
*p
= g_new0(GlobalProperty
, 1);
1126 /* Any compat_props must never cause error */
1127 p
->errp
= &error_abort
;
1129 p
->property
= property
;
1131 qdev_prop_register_global(p
);
1134 void register_compat_props_array(GlobalProperty
*prop
)
1136 for (; prop
&& prop
->driver
; prop
++) {
1137 register_compat_prop(prop
->driver
, prop
->property
, prop
->value
);
1141 void qdev_prop_register_global_list(GlobalProperty
*props
)
1145 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
1146 qdev_prop_register_global(props
+i
);
1150 int qdev_prop_check_globals(void)
1155 for (l
= global_props
; l
; l
= l
->next
) {
1156 GlobalProperty
*prop
= l
->data
;
1162 if (!prop
->user_provided
) {
1165 oc
= object_class_by_name(prop
->driver
);
1166 oc
= object_class_dynamic_cast(oc
, TYPE_DEVICE
);
1168 warn_report("global %s.%s has invalid class name",
1169 prop
->driver
, prop
->property
);
1173 dc
= DEVICE_CLASS(oc
);
1174 if (!dc
->hotpluggable
&& !prop
->used
) {
1175 warn_report("global %s.%s=%s not used",
1176 prop
->driver
, prop
->property
, prop
->value
);
1184 void qdev_prop_set_globals(DeviceState
*dev
)
1188 for (l
= global_props
; l
; l
= l
->next
) {
1189 GlobalProperty
*prop
= l
->data
;
1192 if (object_dynamic_cast(OBJECT(dev
), prop
->driver
) == NULL
) {
1196 object_property_parse(OBJECT(dev
), prop
->value
, prop
->property
, &err
);
1198 error_prepend(&err
, "can't apply global %s.%s=%s: ",
1199 prop
->driver
, prop
->property
, prop
->value
);
1200 if (!dev
->hotplugged
&& prop
->errp
) {
1201 error_propagate(prop
->errp
, err
);
1203 assert(prop
->user_provided
);
1204 warn_report_err(err
);
1210 /* --- 64bit unsigned int 'size' type --- */
1212 static void get_size(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1215 DeviceState
*dev
= DEVICE(obj
);
1216 Property
*prop
= opaque
;
1217 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1219 visit_type_size(v
, name
, ptr
, errp
);
1222 static void set_size(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1225 DeviceState
*dev
= DEVICE(obj
);
1226 Property
*prop
= opaque
;
1227 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1229 visit_type_size(v
, name
, ptr
, errp
);
1232 const PropertyInfo qdev_prop_size
= {
1236 .set_default_value
= set_default_value_uint
,
1239 /* --- object link property --- */
1241 static void create_link_property(Object
*obj
, Property
*prop
, Error
**errp
)
1243 Object
**child
= qdev_get_prop_ptr(DEVICE(obj
), prop
);
1245 object_property_add_link(obj
, prop
->name
, prop
->link_type
,
1247 qdev_prop_allow_set_link_before_realize
,
1248 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1252 const PropertyInfo qdev_prop_link
= {
1254 .create
= create_link_property
,