spapr: Clean up RMA size calculation
[qemu/ar7.git] / hw / core / qdev-properties.c
blob2047114fca1e408de6e158674ebbb6c628a99215
1 #include "qemu/osdep.h"
2 #include "net/net.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"
13 #include "net/hub.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,
19 Error **errp)
21 if (dev->id) {
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)));
25 } else {
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,
33 const char *name,
34 Object *val, Error **errp)
36 DeviceState *dev = DEVICE(obj);
38 if (dev->realized) {
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)
47 void *ptr = dev;
48 ptr += prop->offset;
49 return ptr;
52 static void get_enum(Object *obj, Visitor *v, const char *name, void *opaque,
53 Error **errp)
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,
63 Error **errp)
65 DeviceState *dev = DEVICE(obj);
66 Property *prop = opaque;
67 int *ptr = qdev_get_prop_ptr(dev, prop);
69 if (dev->realized) {
70 qdev_prop_set_after_realize(dev, name, errp);
71 return;
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));
83 /* Bit */
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);
95 if (val) {
96 *p |= mask;
97 } else {
98 *p &= ~mask;
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;
119 bool value;
121 if (dev->realized) {
122 qdev_prop_set_after_realize(dev, name, errp);
123 return;
126 visit_type_bool(v, name, &value, &local_err);
127 if (local_err) {
128 error_propagate(errp, local_err);
129 return;
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 = {
140 .name = "bool",
141 .description = "on/off",
142 .get = prop_get_bit,
143 .set = prop_set_bit,
144 .set_default_value = set_default_value_bool,
147 /* Bit64 */
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);
159 if (val) {
160 *p |= mask;
161 } else {
162 *p &= ~mask;
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;
183 bool value;
185 if (dev->realized) {
186 qdev_prop_set_after_realize(dev, name, errp);
187 return;
190 visit_type_bool(v, name, &value, &local_err);
191 if (local_err) {
192 error_propagate(errp, local_err);
193 return;
195 bit64_prop_set(dev, prop, value);
198 const PropertyInfo qdev_prop_bit64 = {
199 .name = "bool",
200 .description = "on/off",
201 .get = prop_get_bit64,
202 .set = prop_set_bit64,
203 .set_default_value = set_default_value_bool,
206 /* --- bool --- */
208 static void get_bool(Object *obj, Visitor *v, const char *name, void *opaque,
209 Error **errp)
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,
219 Error **errp)
221 DeviceState *dev = DEVICE(obj);
222 Property *prop = opaque;
223 bool *ptr = qdev_get_prop_ptr(dev, prop);
225 if (dev->realized) {
226 qdev_prop_set_after_realize(dev, name, errp);
227 return;
230 visit_type_bool(v, name, ptr, errp);
233 const PropertyInfo qdev_prop_bool = {
234 .name = "bool",
235 .get = get_bool,
236 .set = set_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,
243 Error **errp)
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,
253 Error **errp)
255 DeviceState *dev = DEVICE(obj);
256 Property *prop = opaque;
257 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
259 if (dev->realized) {
260 qdev_prop_set_after_realize(dev, name, errp);
261 return;
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 = {
278 .name = "uint8",
279 .get = get_uint8,
280 .set = set_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);
303 if (dev->realized) {
304 qdev_prop_set_after_realize(dev, name, errp);
305 return;
308 visit_type_uint16(v, name, ptr, errp);
311 const PropertyInfo qdev_prop_uint16 = {
312 .name = "uint16",
313 .get = get_uint16,
314 .set = set_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);
337 if (dev->realized) {
338 qdev_prop_set_after_realize(dev, name, errp);
339 return;
342 visit_type_uint32(v, name, ptr, errp);
345 static void get_int32(Object *obj, Visitor *v, const char *name, void *opaque,
346 Error **errp)
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,
356 Error **errp)
358 DeviceState *dev = DEVICE(obj);
359 Property *prop = opaque;
360 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
362 if (dev->realized) {
363 qdev_prop_set_after_realize(dev, name, errp);
364 return;
367 visit_type_int32(v, name, ptr, errp);
370 const PropertyInfo qdev_prop_uint32 = {
371 .name = "uint32",
372 .get = get_uint32,
373 .set = set_uint32,
374 .set_default_value = set_default_value_uint,
377 const PropertyInfo qdev_prop_int32 = {
378 .name = "int32",
379 .get = get_int32,
380 .set = set_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);
403 if (dev->realized) {
404 qdev_prop_set_after_realize(dev, name, errp);
405 return;
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);
428 if (dev->realized) {
429 qdev_prop_set_after_realize(dev, name, errp);
430 return;
433 visit_type_int64(v, name, ptr, errp);
436 const PropertyInfo qdev_prop_uint64 = {
437 .name = "uint64",
438 .get = get_uint64,
439 .set = set_uint64,
440 .set_default_value = set_default_value_uint,
443 const PropertyInfo qdev_prop_int64 = {
444 .name = "int64",
445 .get = get_int64,
446 .set = set_int64,
447 .set_default_value = set_default_value_int,
450 /* --- string --- */
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);
465 if (!*ptr) {
466 char *str = (char *)"";
467 visit_type_str(v, name, &str, errp);
468 } else {
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;
480 char *str;
482 if (dev->realized) {
483 qdev_prop_set_after_realize(dev, name, errp);
484 return;
487 visit_type_str(v, name, &str, &local_err);
488 if (local_err) {
489 error_propagate(errp, local_err);
490 return;
492 g_free(*ptr);
493 *ptr = str;
496 const PropertyInfo qdev_prop_string = {
497 .name = "str",
498 .release = release_string,
499 .get = get_string,
500 .set = set_string,
503 /* --- mac address --- */
506 * accepted syntax versions:
507 * 01:02:03:04:05:06
508 * 01-02-03-04-05-06
510 static void get_mac(Object *obj, Visitor *v, const char *name, void *opaque,
511 Error **errp)
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];
517 char *p = buffer;
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,
527 Error **errp)
529 DeviceState *dev = DEVICE(obj);
530 Property *prop = opaque;
531 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
532 Error *local_err = NULL;
533 int i, pos;
534 char *str, *p;
536 if (dev->realized) {
537 qdev_prop_set_after_realize(dev, name, errp);
538 return;
541 visit_type_str(v, name, &str, &local_err);
542 if (local_err) {
543 error_propagate(errp, local_err);
544 return;
547 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
548 if (!qemu_isxdigit(str[pos])) {
549 goto inval;
551 if (!qemu_isxdigit(str[pos+1])) {
552 goto inval;
554 if (i == 5) {
555 if (str[pos+2] != '\0') {
556 goto inval;
558 } else {
559 if (str[pos+2] != ':' && str[pos+2] != '-') {
560 goto inval;
563 mac->a[i] = strtol(str+pos, &p, 16);
565 g_free(str);
566 return;
568 inval:
569 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
570 g_free(str);
573 const PropertyInfo qdev_prop_macaddr = {
574 .name = "str",
575 .description = "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56",
576 .get = get_mac,
577 .set = set_mac,
580 /* --- on/off/auto --- */
582 const PropertyInfo qdev_prop_on_off_auto = {
583 .name = "OnOffAuto",
584 .description = "on/off/auto",
585 .enum_table = &OnOffAuto_lookup,
586 .get = get_enum,
587 .set = set_enum,
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,
598 .get = get_enum,
599 .set = set_enum,
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,
612 .get = get_enum,
613 .set = set_enum,
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,
626 .get = get_enum,
627 .set = set_enum,
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,
638 .get = get_enum,
639 .set = set_enum,
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, "
648 "none/zlib/zstd",
649 .enum_table = &MultiFDCompression_lookup,
650 .get = get_enum,
651 .set = set_enum,
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;
668 char *str;
670 if (dev->realized) {
671 qdev_prop_set_after_realize(dev, name, errp);
672 return;
675 visit_type_str(v, name, &str, &local_err);
676 if (local_err) {
677 error_free(local_err);
678 local_err = NULL;
679 visit_type_int32(v, name, &value, &local_err);
680 if (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");
685 } else {
686 *ptr = value;
688 return;
691 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
692 fn = 0;
693 if (sscanf(str, "%x%n", &slot, &n) != 1) {
694 goto invalid;
697 if (str[n] != '\0' || fn > 7 || slot > 31) {
698 goto invalid;
700 *ptr = slot << 3 | fn;
701 g_free(str);
702 return;
704 invalid:
705 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
706 g_free(str);
709 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest,
710 size_t len)
712 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
714 if (*ptr == -1) {
715 return snprintf(dest, len, "<unset>");
716 } else {
717 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
721 const PropertyInfo qdev_prop_pci_devfn = {
722 .name = "int32",
723 .description = "Slot and optional function number, example: 06.0 or 06",
724 .print = print_pci_devfn,
725 .get = get_int32,
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;
742 if (dev->realized) {
743 qdev_prop_set_after_realize(dev, name, errp);
744 return;
747 visit_type_uint16(v, name, &value, &local_err);
748 if (local_err) {
749 error_propagate(errp, local_err);
750 return;
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);
756 return;
759 /* We rely on power-of-2 blocksizes for bitmasks */
760 if ((value & (value - 1)) != 0) {
761 error_setg(errp,
762 "Property %s.%s doesn't take value '%" PRId64 "', it's not a power of 2",
763 dev->id ?: "", name, (int64_t)value);
764 return;
767 *ptr = value;
770 const PropertyInfo qdev_prop_blocksize = {
771 .name = "uint16",
772 .description = "A power of two between 512 and 32768",
773 .get = get_uint16,
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";
787 char *p = buffer;
788 int rc = 0;
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;
814 char *str, *p;
815 char *e;
816 unsigned long val;
817 unsigned long dom = 0, bus = 0;
818 unsigned int slot = 0, func = 0;
820 if (dev->realized) {
821 qdev_prop_set_after_realize(dev, name, errp);
822 return;
825 visit_type_str(v, name, &str, &local_err);
826 if (local_err) {
827 error_propagate(errp, local_err);
828 return;
831 p = str;
832 val = strtoul(p, &e, 16);
833 if (e == p || *e != ':') {
834 goto inval;
836 bus = val;
838 p = e + 1;
839 val = strtoul(p, &e, 16);
840 if (e == p) {
841 goto inval;
843 if (*e == ':') {
844 dom = bus;
845 bus = val;
846 p = e + 1;
847 val = strtoul(p, &e, 16);
848 if (e == p) {
849 goto inval;
852 slot = val;
854 if (*e != '.') {
855 goto inval;
857 p = e + 1;
858 val = strtoul(p, &e, 10);
859 if (e == p) {
860 goto inval;
862 func = val;
864 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
865 goto inval;
868 if (*e) {
869 goto inval;
872 addr->domain = dom;
873 addr->bus = bus;
874 addr->slot = slot;
875 addr->function = func;
877 g_free(str);
878 return;
880 inval:
881 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
882 g_free(str);
885 const PropertyInfo qdev_prop_pci_host_devaddr = {
886 .name = "str",
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,
893 /* --- UUID --- */
895 static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
896 Error **errp)
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];
902 char *p = buffer;
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,
912 Error **errp)
914 DeviceState *dev = DEVICE(obj);
915 Property *prop = opaque;
916 QemuUUID *uuid = qdev_get_prop_ptr(dev, prop);
917 Error *local_err = NULL;
918 char *str;
920 if (dev->realized) {
921 qdev_prop_set_after_realize(dev, name, errp);
922 return;
925 visit_type_str(v, name, &str, &local_err);
926 if (local_err) {
927 error_propagate(errp, local_err);
928 return;
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);
936 g_free(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 = {
945 .name = "str",
946 .description = "UUID (aka GUID) or \"" UUID_VALUE_AUTO
947 "\" for random value (default)",
948 .get = get_uuid,
949 .set = set_uuid,
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.
961 typedef struct {
962 struct Property prop;
963 char *propname;
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;
974 if (p->release) {
975 p->release(obj, name, opaque);
977 g_free(p->propname);
978 g_free(p);
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;
994 void *eltptr;
995 const char *arrayname;
996 int i;
998 if (dev->realized) {
999 qdev_prop_set_after_realize(dev, name, errp);
1000 return;
1002 if (*alenptr) {
1003 error_setg(errp, "array size property %s may not be set more than once",
1004 name);
1005 return;
1007 visit_type_uint32(v, name, alenptr, &local_err);
1008 if (local_err) {
1009 error_propagate(errp, local_err);
1010 return;
1012 if (!*alenptr) {
1013 return;
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,
1046 arrayprop, &local_err);
1047 if (local_err) {
1048 error_propagate(errp, local_err);
1049 return;
1054 const PropertyInfo qdev_prop_arraylen = {
1055 .name = "uint32",
1056 .get = get_uint32,
1057 .set = set_prop_arraylen,
1058 .set_default_value = set_default_value_uint,
1061 /* --- public helpers --- */
1063 static Property *qdev_prop_walk(Property *props, const char *name)
1065 if (!props) {
1066 return NULL;
1068 while (props->name) {
1069 if (strcmp(props->name, name) == 0) {
1070 return props;
1072 props++;
1074 return NULL;
1077 static Property *qdev_prop_find(DeviceState *dev, const char *name)
1079 ObjectClass *class;
1080 Property *prop;
1082 /* device properties */
1083 class = object_get_class(OBJECT(dev));
1084 do {
1085 prop = qdev_prop_walk(DEVICE_CLASS(class)->props_, name);
1086 if (prop) {
1087 return prop;
1089 class = object_class_get_parent(class);
1090 } while (class != object_class_by_name(TYPE_DEVICE));
1092 return NULL;
1095 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
1096 Property *prop, const char *value)
1098 switch (ret) {
1099 case -EEXIST:
1100 error_setg(errp, "Property '%s.%s' can't take value '%s', it's in use",
1101 object_get_typename(OBJECT(dev)), prop->name, value);
1102 break;
1103 default:
1104 case -EINVAL:
1105 error_setg(errp, QERR_PROPERTY_VALUE_BAD,
1106 object_get_typename(OBJECT(dev)), prop->name, value);
1107 break;
1108 case -ENOENT:
1109 error_setg(errp, "Property '%s.%s' can't find value '%s'",
1110 object_get_typename(OBJECT(dev)), prop->name, value);
1111 break;
1112 case 0:
1113 break;
1117 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
1119 object_property_set_bool(OBJECT(dev), value, name, &error_abort);
1122 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
1124 object_property_set_int(OBJECT(dev), value, name, &error_abort);
1127 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
1129 object_property_set_int(OBJECT(dev), value, name, &error_abort);
1132 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
1134 object_property_set_int(OBJECT(dev), value, name, &error_abort);
1137 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
1139 object_property_set_int(OBJECT(dev), value, name, &error_abort);
1142 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
1144 object_property_set_int(OBJECT(dev), value, name, &error_abort);
1147 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value)
1149 object_property_set_str(OBJECT(dev), value, name, &error_abort);
1152 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
1153 const uint8_t *value)
1155 char str[2 * 6 + 5 + 1];
1156 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
1157 value[0], value[1], value[2], value[3], value[4], value[5]);
1159 object_property_set_str(OBJECT(dev), str, name, &error_abort);
1162 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
1164 Property *prop;
1166 prop = qdev_prop_find(dev, name);
1167 object_property_set_str(OBJECT(dev),
1168 qapi_enum_lookup(prop->info->enum_table, value),
1169 name, &error_abort);
1172 static GPtrArray *global_props(void)
1174 static GPtrArray *gp;
1176 if (!gp) {
1177 gp = g_ptr_array_new();
1180 return gp;
1183 void qdev_prop_register_global(GlobalProperty *prop)
1185 g_ptr_array_add(global_props(), prop);
1188 int qdev_prop_check_globals(void)
1190 int i, ret = 0;
1192 for (i = 0; i < global_props()->len; i++) {
1193 GlobalProperty *prop;
1194 ObjectClass *oc;
1195 DeviceClass *dc;
1197 prop = g_ptr_array_index(global_props(), i);
1198 if (prop->used) {
1199 continue;
1201 oc = object_class_by_name(prop->driver);
1202 oc = object_class_dynamic_cast(oc, TYPE_DEVICE);
1203 if (!oc) {
1204 warn_report("global %s.%s has invalid class name",
1205 prop->driver, prop->property);
1206 ret = 1;
1207 continue;
1209 dc = DEVICE_CLASS(oc);
1210 if (!dc->hotpluggable && !prop->used) {
1211 warn_report("global %s.%s=%s not used",
1212 prop->driver, prop->property, prop->value);
1213 ret = 1;
1214 continue;
1217 return ret;
1220 void qdev_prop_set_globals(DeviceState *dev)
1222 object_apply_global_props(OBJECT(dev), global_props(),
1223 dev->hotplugged ? NULL : &error_fatal);
1226 /* --- 64bit unsigned int 'size' type --- */
1228 static void get_size(Object *obj, Visitor *v, const char *name, void *opaque,
1229 Error **errp)
1231 DeviceState *dev = DEVICE(obj);
1232 Property *prop = opaque;
1233 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
1235 visit_type_size(v, name, ptr, errp);
1238 static void set_size(Object *obj, Visitor *v, const char *name, void *opaque,
1239 Error **errp)
1241 DeviceState *dev = DEVICE(obj);
1242 Property *prop = opaque;
1243 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
1245 visit_type_size(v, name, ptr, errp);
1248 const PropertyInfo qdev_prop_size = {
1249 .name = "size",
1250 .get = get_size,
1251 .set = set_size,
1252 .set_default_value = set_default_value_uint,
1255 /* --- object link property --- */
1257 static void create_link_property(ObjectClass *oc, Property *prop, Error **errp)
1259 object_class_property_add_link(oc, prop->name, prop->link_type,
1260 prop->offset,
1261 qdev_prop_allow_set_link_before_realize,
1262 OBJ_PROP_LINK_STRONG,
1263 errp);
1266 const PropertyInfo qdev_prop_link = {
1267 .name = "link",
1268 .create = create_link_property,
1271 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
1273 const PropertyInfo qdev_prop_off_auto_pcibar = {
1274 .name = "OffAutoPCIBAR",
1275 .description = "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
1276 .enum_table = &OffAutoPCIBAR_lookup,
1277 .get = get_enum,
1278 .set = set_enum,
1279 .set_default_value = set_default_value_enum,
1282 /* --- PCIELinkSpeed 2_5/5/8/16 -- */
1284 static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
1285 void *opaque, Error **errp)
1287 DeviceState *dev = DEVICE(obj);
1288 Property *prop = opaque;
1289 PCIExpLinkSpeed *p = qdev_get_prop_ptr(dev, prop);
1290 int speed;
1292 switch (*p) {
1293 case QEMU_PCI_EXP_LNK_2_5GT:
1294 speed = PCIE_LINK_SPEED_2_5;
1295 break;
1296 case QEMU_PCI_EXP_LNK_5GT:
1297 speed = PCIE_LINK_SPEED_5;
1298 break;
1299 case QEMU_PCI_EXP_LNK_8GT:
1300 speed = PCIE_LINK_SPEED_8;
1301 break;
1302 case QEMU_PCI_EXP_LNK_16GT:
1303 speed = PCIE_LINK_SPEED_16;
1304 break;
1305 default:
1306 /* Unreachable */
1307 abort();
1310 visit_type_enum(v, prop->name, &speed, prop->info->enum_table, errp);
1313 static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
1314 void *opaque, Error **errp)
1316 DeviceState *dev = DEVICE(obj);
1317 Property *prop = opaque;
1318 PCIExpLinkSpeed *p = qdev_get_prop_ptr(dev, prop);
1319 int speed;
1320 Error *local_err = NULL;
1322 if (dev->realized) {
1323 qdev_prop_set_after_realize(dev, name, errp);
1324 return;
1327 visit_type_enum(v, prop->name, &speed, prop->info->enum_table, &local_err);
1328 if (local_err) {
1329 error_propagate(errp, local_err);
1330 return;
1333 switch (speed) {
1334 case PCIE_LINK_SPEED_2_5:
1335 *p = QEMU_PCI_EXP_LNK_2_5GT;
1336 break;
1337 case PCIE_LINK_SPEED_5:
1338 *p = QEMU_PCI_EXP_LNK_5GT;
1339 break;
1340 case PCIE_LINK_SPEED_8:
1341 *p = QEMU_PCI_EXP_LNK_8GT;
1342 break;
1343 case PCIE_LINK_SPEED_16:
1344 *p = QEMU_PCI_EXP_LNK_16GT;
1345 break;
1346 default:
1347 /* Unreachable */
1348 abort();
1352 const PropertyInfo qdev_prop_pcie_link_speed = {
1353 .name = "PCIELinkSpeed",
1354 .description = "2_5/5/8/16",
1355 .enum_table = &PCIELinkSpeed_lookup,
1356 .get = get_prop_pcielinkspeed,
1357 .set = set_prop_pcielinkspeed,
1358 .set_default_value = set_default_value_enum,
1361 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
1363 static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1364 void *opaque, Error **errp)
1366 DeviceState *dev = DEVICE(obj);
1367 Property *prop = opaque;
1368 PCIExpLinkWidth *p = qdev_get_prop_ptr(dev, prop);
1369 int width;
1371 switch (*p) {
1372 case QEMU_PCI_EXP_LNK_X1:
1373 width = PCIE_LINK_WIDTH_1;
1374 break;
1375 case QEMU_PCI_EXP_LNK_X2:
1376 width = PCIE_LINK_WIDTH_2;
1377 break;
1378 case QEMU_PCI_EXP_LNK_X4:
1379 width = PCIE_LINK_WIDTH_4;
1380 break;
1381 case QEMU_PCI_EXP_LNK_X8:
1382 width = PCIE_LINK_WIDTH_8;
1383 break;
1384 case QEMU_PCI_EXP_LNK_X12:
1385 width = PCIE_LINK_WIDTH_12;
1386 break;
1387 case QEMU_PCI_EXP_LNK_X16:
1388 width = PCIE_LINK_WIDTH_16;
1389 break;
1390 case QEMU_PCI_EXP_LNK_X32:
1391 width = PCIE_LINK_WIDTH_32;
1392 break;
1393 default:
1394 /* Unreachable */
1395 abort();
1398 visit_type_enum(v, prop->name, &width, prop->info->enum_table, errp);
1401 static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1402 void *opaque, Error **errp)
1404 DeviceState *dev = DEVICE(obj);
1405 Property *prop = opaque;
1406 PCIExpLinkWidth *p = qdev_get_prop_ptr(dev, prop);
1407 int width;
1408 Error *local_err = NULL;
1410 if (dev->realized) {
1411 qdev_prop_set_after_realize(dev, name, errp);
1412 return;
1415 visit_type_enum(v, prop->name, &width, prop->info->enum_table, &local_err);
1416 if (local_err) {
1417 error_propagate(errp, local_err);
1418 return;
1421 switch (width) {
1422 case PCIE_LINK_WIDTH_1:
1423 *p = QEMU_PCI_EXP_LNK_X1;
1424 break;
1425 case PCIE_LINK_WIDTH_2:
1426 *p = QEMU_PCI_EXP_LNK_X2;
1427 break;
1428 case PCIE_LINK_WIDTH_4:
1429 *p = QEMU_PCI_EXP_LNK_X4;
1430 break;
1431 case PCIE_LINK_WIDTH_8:
1432 *p = QEMU_PCI_EXP_LNK_X8;
1433 break;
1434 case PCIE_LINK_WIDTH_12:
1435 *p = QEMU_PCI_EXP_LNK_X12;
1436 break;
1437 case PCIE_LINK_WIDTH_16:
1438 *p = QEMU_PCI_EXP_LNK_X16;
1439 break;
1440 case PCIE_LINK_WIDTH_32:
1441 *p = QEMU_PCI_EXP_LNK_X32;
1442 break;
1443 default:
1444 /* Unreachable */
1445 abort();
1449 const PropertyInfo qdev_prop_pcie_link_width = {
1450 .name = "PCIELinkWidth",
1451 .description = "1/2/4/8/12/16/32",
1452 .enum_table = &PCIELinkWidth_lookup,
1453 .get = get_prop_pcielinkwidth,
1454 .set = set_prop_pcielinkwidth,
1455 .set_default_value = set_default_value_enum,