xref: /openbmc/qemu/hw/core/qdev-properties.c (revision 7d8cbbab)
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"
17 
18 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
19                                   Error **errp)
20 {
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)));
29     }
30 }
31 
32 void qdev_prop_allow_set_link_before_realize(const Object *obj,
33                                              const char *name,
34                                              Object *val, Error **errp)
35 {
36     DeviceState *dev = DEVICE(obj);
37 
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));
42     }
43 }
44 
45 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
46 {
47     void *ptr = dev;
48     ptr += prop->offset;
49     return ptr;
50 }
51 
52 static void get_enum(Object *obj, Visitor *v, const char *name, void *opaque,
53                      Error **errp)
54 {
55     DeviceState *dev = DEVICE(obj);
56     Property *prop = opaque;
57     int *ptr = qdev_get_prop_ptr(dev, prop);
58 
59     visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
60 }
61 
62 static void set_enum(Object *obj, Visitor *v, const char *name, void *opaque,
63                      Error **errp)
64 {
65     DeviceState *dev = DEVICE(obj);
66     Property *prop = opaque;
67     int *ptr = qdev_get_prop_ptr(dev, prop);
68 
69     if (dev->realized) {
70         qdev_prop_set_after_realize(dev, name, errp);
71         return;
72     }
73 
74     visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
75 }
76 
77 static void set_default_value_enum(ObjectProperty *op, const Property *prop)
78 {
79     object_property_set_default_str(op,
80         qapi_enum_lookup(prop->info->enum_table, prop->defval.i));
81 }
82 
83 /* Bit */
84 
85 static uint32_t qdev_get_prop_mask(Property *prop)
86 {
87     assert(prop->info == &qdev_prop_bit);
88     return 0x1 << prop->bitnr;
89 }
90 
91 static void bit_prop_set(DeviceState *dev, Property *props, bool val)
92 {
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;
99     }
100 }
101 
102 static void prop_get_bit(Object *obj, Visitor *v, const char *name,
103                          void *opaque, Error **errp)
104 {
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;
109 
110     visit_type_bool(v, name, &value, errp);
111 }
112 
113 static void prop_set_bit(Object *obj, Visitor *v, const char *name,
114                          void *opaque, Error **errp)
115 {
116     DeviceState *dev = DEVICE(obj);
117     Property *prop = opaque;
118     Error *local_err = NULL;
119     bool value;
120 
121     if (dev->realized) {
122         qdev_prop_set_after_realize(dev, name, errp);
123         return;
124     }
125 
126     visit_type_bool(v, name, &value, &local_err);
127     if (local_err) {
128         error_propagate(errp, local_err);
129         return;
130     }
131     bit_prop_set(dev, prop, value);
132 }
133 
134 static void set_default_value_bool(ObjectProperty *op, const Property *prop)
135 {
136     object_property_set_default_bool(op, prop->defval.u);
137 }
138 
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,
145 };
146 
147 /* Bit64 */
148 
149 static uint64_t qdev_get_prop_mask64(Property *prop)
150 {
151     assert(prop->info == &qdev_prop_bit64);
152     return 0x1ull << prop->bitnr;
153 }
154 
155 static void bit64_prop_set(DeviceState *dev, Property *props, bool val)
156 {
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;
163     }
164 }
165 
166 static void prop_get_bit64(Object *obj, Visitor *v, const char *name,
167                            void *opaque, Error **errp)
168 {
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;
173 
174     visit_type_bool(v, name, &value, errp);
175 }
176 
177 static void prop_set_bit64(Object *obj, Visitor *v, const char *name,
178                            void *opaque, Error **errp)
179 {
180     DeviceState *dev = DEVICE(obj);
181     Property *prop = opaque;
182     Error *local_err = NULL;
183     bool value;
184 
185     if (dev->realized) {
186         qdev_prop_set_after_realize(dev, name, errp);
187         return;
188     }
189 
190     visit_type_bool(v, name, &value, &local_err);
191     if (local_err) {
192         error_propagate(errp, local_err);
193         return;
194     }
195     bit64_prop_set(dev, prop, value);
196 }
197 
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,
204 };
205 
206 /* --- bool --- */
207 
208 static void get_bool(Object *obj, Visitor *v, const char *name, void *opaque,
209                      Error **errp)
210 {
211     DeviceState *dev = DEVICE(obj);
212     Property *prop = opaque;
213     bool *ptr = qdev_get_prop_ptr(dev, prop);
214 
215     visit_type_bool(v, name, ptr, errp);
216 }
217 
218 static void set_bool(Object *obj, Visitor *v, const char *name, void *opaque,
219                      Error **errp)
220 {
221     DeviceState *dev = DEVICE(obj);
222     Property *prop = opaque;
223     bool *ptr = qdev_get_prop_ptr(dev, prop);
224 
225     if (dev->realized) {
226         qdev_prop_set_after_realize(dev, name, errp);
227         return;
228     }
229 
230     visit_type_bool(v, name, ptr, errp);
231 }
232 
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,
238 };
239 
240 /* --- 8bit integer --- */
241 
242 static void get_uint8(Object *obj, Visitor *v, const char *name, void *opaque,
243                       Error **errp)
244 {
245     DeviceState *dev = DEVICE(obj);
246     Property *prop = opaque;
247     uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
248 
249     visit_type_uint8(v, name, ptr, errp);
250 }
251 
252 static void set_uint8(Object *obj, Visitor *v, const char *name, void *opaque,
253                       Error **errp)
254 {
255     DeviceState *dev = DEVICE(obj);
256     Property *prop = opaque;
257     uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
258 
259     if (dev->realized) {
260         qdev_prop_set_after_realize(dev, name, errp);
261         return;
262     }
263 
264     visit_type_uint8(v, name, ptr, errp);
265 }
266 
267 static void set_default_value_int(ObjectProperty *op, const Property *prop)
268 {
269     object_property_set_default_int(op, prop->defval.i);
270 }
271 
272 static void set_default_value_uint(ObjectProperty *op, const Property *prop)
273 {
274     object_property_set_default_uint(op, prop->defval.u);
275 }
276 
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,
282 };
283 
284 /* --- 16bit integer --- */
285 
286 static void get_uint16(Object *obj, Visitor *v, const char *name,
287                        void *opaque, Error **errp)
288 {
289     DeviceState *dev = DEVICE(obj);
290     Property *prop = opaque;
291     uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
292 
293     visit_type_uint16(v, name, ptr, errp);
294 }
295 
296 static void set_uint16(Object *obj, Visitor *v, const char *name,
297                        void *opaque, Error **errp)
298 {
299     DeviceState *dev = DEVICE(obj);
300     Property *prop = opaque;
301     uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
302 
303     if (dev->realized) {
304         qdev_prop_set_after_realize(dev, name, errp);
305         return;
306     }
307 
308     visit_type_uint16(v, name, ptr, errp);
309 }
310 
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,
316 };
317 
318 /* --- 32bit integer --- */
319 
320 static void get_uint32(Object *obj, Visitor *v, const char *name,
321                        void *opaque, Error **errp)
322 {
323     DeviceState *dev = DEVICE(obj);
324     Property *prop = opaque;
325     uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
326 
327     visit_type_uint32(v, name, ptr, errp);
328 }
329 
330 static void set_uint32(Object *obj, Visitor *v, const char *name,
331                        void *opaque, Error **errp)
332 {
333     DeviceState *dev = DEVICE(obj);
334     Property *prop = opaque;
335     uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
336 
337     if (dev->realized) {
338         qdev_prop_set_after_realize(dev, name, errp);
339         return;
340     }
341 
342     visit_type_uint32(v, name, ptr, errp);
343 }
344 
345 static void get_int32(Object *obj, Visitor *v, const char *name, void *opaque,
346                       Error **errp)
347 {
348     DeviceState *dev = DEVICE(obj);
349     Property *prop = opaque;
350     int32_t *ptr = qdev_get_prop_ptr(dev, prop);
351 
352     visit_type_int32(v, name, ptr, errp);
353 }
354 
355 static void set_int32(Object *obj, Visitor *v, const char *name, void *opaque,
356                       Error **errp)
357 {
358     DeviceState *dev = DEVICE(obj);
359     Property *prop = opaque;
360     int32_t *ptr = qdev_get_prop_ptr(dev, prop);
361 
362     if (dev->realized) {
363         qdev_prop_set_after_realize(dev, name, errp);
364         return;
365     }
366 
367     visit_type_int32(v, name, ptr, errp);
368 }
369 
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,
375 };
376 
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,
382 };
383 
384 /* --- 64bit integer --- */
385 
386 static void get_uint64(Object *obj, Visitor *v, const char *name,
387                        void *opaque, Error **errp)
388 {
389     DeviceState *dev = DEVICE(obj);
390     Property *prop = opaque;
391     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
392 
393     visit_type_uint64(v, name, ptr, errp);
394 }
395 
396 static void set_uint64(Object *obj, Visitor *v, const char *name,
397                        void *opaque, Error **errp)
398 {
399     DeviceState *dev = DEVICE(obj);
400     Property *prop = opaque;
401     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
402 
403     if (dev->realized) {
404         qdev_prop_set_after_realize(dev, name, errp);
405         return;
406     }
407 
408     visit_type_uint64(v, name, ptr, errp);
409 }
410 
411 static void get_int64(Object *obj, Visitor *v, const char *name,
412                       void *opaque, Error **errp)
413 {
414     DeviceState *dev = DEVICE(obj);
415     Property *prop = opaque;
416     int64_t *ptr = qdev_get_prop_ptr(dev, prop);
417 
418     visit_type_int64(v, name, ptr, errp);
419 }
420 
421 static void set_int64(Object *obj, Visitor *v, const char *name,
422                       void *opaque, Error **errp)
423 {
424     DeviceState *dev = DEVICE(obj);
425     Property *prop = opaque;
426     int64_t *ptr = qdev_get_prop_ptr(dev, prop);
427 
428     if (dev->realized) {
429         qdev_prop_set_after_realize(dev, name, errp);
430         return;
431     }
432 
433     visit_type_int64(v, name, ptr, errp);
434 }
435 
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,
441 };
442 
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,
448 };
449 
450 /* --- string --- */
451 
452 static void release_string(Object *obj, const char *name, void *opaque)
453 {
454     Property *prop = opaque;
455     g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj), prop));
456 }
457 
458 static void get_string(Object *obj, Visitor *v, const char *name,
459                        void *opaque, Error **errp)
460 {
461     DeviceState *dev = DEVICE(obj);
462     Property *prop = opaque;
463     char **ptr = qdev_get_prop_ptr(dev, prop);
464 
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);
470     }
471 }
472 
473 static void set_string(Object *obj, Visitor *v, const char *name,
474                        void *opaque, Error **errp)
475 {
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;
481 
482     if (dev->realized) {
483         qdev_prop_set_after_realize(dev, name, errp);
484         return;
485     }
486 
487     visit_type_str(v, name, &str, &local_err);
488     if (local_err) {
489         error_propagate(errp, local_err);
490         return;
491     }
492     g_free(*ptr);
493     *ptr = str;
494 }
495 
496 const PropertyInfo qdev_prop_string = {
497     .name  = "str",
498     .release = release_string,
499     .get   = get_string,
500     .set   = set_string,
501 };
502 
503 /* --- mac address --- */
504 
505 /*
506  * accepted syntax versions:
507  *   01:02:03:04:05:06
508  *   01-02-03-04-05-06
509  */
510 static void get_mac(Object *obj, Visitor *v, const char *name, void *opaque,
511                     Error **errp)
512 {
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;
518 
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]);
522 
523     visit_type_str(v, name, &p, errp);
524 }
525 
526 static void set_mac(Object *obj, Visitor *v, const char *name, void *opaque,
527                     Error **errp)
528 {
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;
535 
536     if (dev->realized) {
537         qdev_prop_set_after_realize(dev, name, errp);
538         return;
539     }
540 
541     visit_type_str(v, name, &str, &local_err);
542     if (local_err) {
543         error_propagate(errp, local_err);
544         return;
545     }
546 
547     for (i = 0, pos = 0; i < 6; i++, pos += 3) {
548         if (!qemu_isxdigit(str[pos])) {
549             goto inval;
550         }
551         if (!qemu_isxdigit(str[pos+1])) {
552             goto inval;
553         }
554         if (i == 5) {
555             if (str[pos+2] != '\0') {
556                 goto inval;
557             }
558         } else {
559             if (str[pos+2] != ':' && str[pos+2] != '-') {
560                 goto inval;
561             }
562         }
563         mac->a[i] = strtol(str+pos, &p, 16);
564     }
565     g_free(str);
566     return;
567 
568 inval:
569     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
570     g_free(str);
571 }
572 
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,
578 };
579 
580 /* --- on/off/auto --- */
581 
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,
589 };
590 
591 /* --- lost tick policy --- */
592 
593 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
594 
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,
601 };
602 
603 /* --- Block device error handling policy --- */
604 
605 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError) != sizeof(int));
606 
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,
615 };
616 
617 /* --- BIOS CHS translation */
618 
619 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation) != sizeof(int));
620 
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,
629 };
630 
631 /* --- FDC default drive types */
632 
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,
641 };
642 
643 /* --- MultiFDCompression --- */
644 
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,
653 };
654 
655 /* --- pci address --- */
656 
657 /*
658  * bus-local address, i.e. "$slot" or "$slot.$fn"
659  */
660 static void set_pci_devfn(Object *obj, Visitor *v, const char *name,
661                           void *opaque, Error **errp)
662 {
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;
669 
670     if (dev->realized) {
671         qdev_prop_set_after_realize(dev, name, errp);
672         return;
673     }
674 
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;
687         }
688         return;
689     }
690 
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;
695         }
696     }
697     if (str[n] != '\0' || fn > 7 || slot > 31) {
698         goto invalid;
699     }
700     *ptr = slot << 3 | fn;
701     g_free(str);
702     return;
703 
704 invalid:
705     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
706     g_free(str);
707 }
708 
709 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest,
710                            size_t len)
711 {
712     int32_t *ptr = qdev_get_prop_ptr(dev, prop);
713 
714     if (*ptr == -1) {
715         return snprintf(dest, len, "<unset>");
716     } else {
717         return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
718     }
719 }
720 
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,
728 };
729 
730 /* --- blocksize --- */
731 
732 static void set_blocksize(Object *obj, Visitor *v, const char *name,
733                           void *opaque, Error **errp)
734 {
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;
741 
742     if (dev->realized) {
743         qdev_prop_set_after_realize(dev, name, errp);
744         return;
745     }
746 
747     visit_type_uint16(v, name, &value, &local_err);
748     if (local_err) {
749         error_propagate(errp, local_err);
750         return;
751     }
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;
757     }
758 
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;
765     }
766 
767     *ptr = value;
768 }
769 
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,
776 };
777 
778 /* --- pci host address --- */
779 
780 static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
781                                  void *opaque, Error **errp)
782 {
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;
789 
790     /*
791      * Catch "invalid" device reference from vfio-pci and allow the
792      * default buffer representing the non-existent device to be used.
793      */
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);
798     }
799 
800     visit_type_str(v, name, &p, errp);
801 }
802 
803 /*
804  * Parse [<domain>:]<bus>:<slot>.<func>
805  *   if <domain> is not supplied, it's assumed to be 0.
806  */
807 static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
808                                  void *opaque, Error **errp)
809 {
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;
819 
820     if (dev->realized) {
821         qdev_prop_set_after_realize(dev, name, errp);
822         return;
823     }
824 
825     visit_type_str(v, name, &str, &local_err);
826     if (local_err) {
827         error_propagate(errp, local_err);
828         return;
829     }
830 
831     p = str;
832     val = strtoul(p, &e, 16);
833     if (e == p || *e != ':') {
834         goto inval;
835     }
836     bus = val;
837 
838     p = e + 1;
839     val = strtoul(p, &e, 16);
840     if (e == p) {
841         goto inval;
842     }
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;
850         }
851     }
852     slot = val;
853 
854     if (*e != '.') {
855         goto inval;
856     }
857     p = e + 1;
858     val = strtoul(p, &e, 10);
859     if (e == p) {
860         goto inval;
861     }
862     func = val;
863 
864     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
865         goto inval;
866     }
867 
868     if (*e) {
869         goto inval;
870     }
871 
872     addr->domain = dom;
873     addr->bus = bus;
874     addr->slot = slot;
875     addr->function = func;
876 
877     g_free(str);
878     return;
879 
880 inval:
881     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
882     g_free(str);
883 }
884 
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,
891 };
892 
893 /* --- UUID --- */
894 
895 static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
896                      Error **errp)
897 {
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;
903 
904     qemu_uuid_unparse(uuid, buffer);
905 
906     visit_type_str(v, name, &p, errp);
907 }
908 
909 #define UUID_VALUE_AUTO        "auto"
910 
911 static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
912                     Error **errp)
913 {
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;
919 
920     if (dev->realized) {
921         qdev_prop_set_after_realize(dev, name, errp);
922         return;
923     }
924 
925     visit_type_str(v, name, &str, &local_err);
926     if (local_err) {
927         error_propagate(errp, local_err);
928         return;
929     }
930 
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);
935     }
936     g_free(str);
937 }
938 
939 static void set_default_uuid_auto(ObjectProperty *op, const Property *prop)
940 {
941     object_property_set_default_str(op, UUID_VALUE_AUTO);
942 }
943 
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,
951 };
952 
953 /* --- support for array properties --- */
954 
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.
960  */
961 typedef struct {
962     struct Property prop;
963     char *propname;
964     ObjectPropertyRelease *release;
965 } ArrayElementProperty;
966 
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.
970  */
971 static void array_element_release(Object *obj, const char *name, void *opaque)
972 {
973     ArrayElementProperty *p = opaque;
974     if (p->release) {
975         p->release(obj, name, opaque);
976     }
977     g_free(p->propname);
978     g_free(p);
979 }
980 
981 static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
982                               void *opaque, Error **errp)
983 {
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.
988      */
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;
997 
998     if (dev->realized) {
999         qdev_prop_set_after_realize(dev, name, errp);
1000         return;
1001     }
1002     if (*alenptr) {
1003         error_setg(errp, "array size property %s may not be set more than once",
1004                    name);
1005         return;
1006     }
1007     visit_type_uint32(v, name, alenptr, &local_err);
1008     if (local_err) {
1009         error_propagate(errp, local_err);
1010         return;
1011     }
1012     if (!*alenptr) {
1013         return;
1014     }
1015 
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.
1018      */
1019     assert(strncmp(name, PROP_ARRAY_LEN_PREFIX,
1020                    strlen(PROP_ARRAY_LEN_PREFIX)) == 0);
1021     arrayname = name + strlen(PROP_ARRAY_LEN_PREFIX);
1022 
1023     /* Note that it is the responsibility of the individual device's deinit
1024      * to free the array proper.
1025      */
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.
1038          */
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);
1047     }
1048 }
1049 
1050 const PropertyInfo qdev_prop_arraylen = {
1051     .name = "uint32",
1052     .get = get_uint32,
1053     .set = set_prop_arraylen,
1054     .set_default_value = set_default_value_uint,
1055 };
1056 
1057 /* --- public helpers --- */
1058 
1059 static Property *qdev_prop_walk(Property *props, const char *name)
1060 {
1061     if (!props) {
1062         return NULL;
1063     }
1064     while (props->name) {
1065         if (strcmp(props->name, name) == 0) {
1066             return props;
1067         }
1068         props++;
1069     }
1070     return NULL;
1071 }
1072 
1073 static Property *qdev_prop_find(DeviceState *dev, const char *name)
1074 {
1075     ObjectClass *class;
1076     Property *prop;
1077 
1078     /* device properties */
1079     class = object_get_class(OBJECT(dev));
1080     do {
1081         prop = qdev_prop_walk(DEVICE_CLASS(class)->props_, name);
1082         if (prop) {
1083             return prop;
1084         }
1085         class = object_class_get_parent(class);
1086     } while (class != object_class_by_name(TYPE_DEVICE));
1087 
1088     return NULL;
1089 }
1090 
1091 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
1092                                     Property *prop, const char *value)
1093 {
1094     switch (ret) {
1095     case -EEXIST:
1096         error_setg(errp, "Property '%s.%s' can't take value '%s', it's in use",
1097                   object_get_typename(OBJECT(dev)), prop->name, value);
1098         break;
1099     default:
1100     case -EINVAL:
1101         error_setg(errp, QERR_PROPERTY_VALUE_BAD,
1102                    object_get_typename(OBJECT(dev)), prop->name, value);
1103         break;
1104     case -ENOENT:
1105         error_setg(errp, "Property '%s.%s' can't find value '%s'",
1106                   object_get_typename(OBJECT(dev)), prop->name, value);
1107         break;
1108     case 0:
1109         break;
1110     }
1111 }
1112 
1113 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
1114 {
1115     object_property_set_bool(OBJECT(dev), value, name, &error_abort);
1116 }
1117 
1118 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
1119 {
1120     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1121 }
1122 
1123 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
1124 {
1125     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1126 }
1127 
1128 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
1129 {
1130     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1131 }
1132 
1133 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
1134 {
1135     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1136 }
1137 
1138 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
1139 {
1140     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1141 }
1142 
1143 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value)
1144 {
1145     object_property_set_str(OBJECT(dev), value, name, &error_abort);
1146 }
1147 
1148 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
1149                            const uint8_t *value)
1150 {
1151     char str[2 * 6 + 5 + 1];
1152     snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
1153              value[0], value[1], value[2], value[3], value[4], value[5]);
1154 
1155     object_property_set_str(OBJECT(dev), str, name, &error_abort);
1156 }
1157 
1158 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
1159 {
1160     Property *prop;
1161 
1162     prop = qdev_prop_find(dev, name);
1163     object_property_set_str(OBJECT(dev),
1164                             qapi_enum_lookup(prop->info->enum_table, value),
1165                             name, &error_abort);
1166 }
1167 
1168 static GPtrArray *global_props(void)
1169 {
1170     static GPtrArray *gp;
1171 
1172     if (!gp) {
1173         gp = g_ptr_array_new();
1174     }
1175 
1176     return gp;
1177 }
1178 
1179 void qdev_prop_register_global(GlobalProperty *prop)
1180 {
1181     g_ptr_array_add(global_props(), prop);
1182 }
1183 
1184 int qdev_prop_check_globals(void)
1185 {
1186     int i, ret = 0;
1187 
1188     for (i = 0; i < global_props()->len; i++) {
1189         GlobalProperty *prop;
1190         ObjectClass *oc;
1191         DeviceClass *dc;
1192 
1193         prop = g_ptr_array_index(global_props(), i);
1194         if (prop->used) {
1195             continue;
1196         }
1197         oc = object_class_by_name(prop->driver);
1198         oc = object_class_dynamic_cast(oc, TYPE_DEVICE);
1199         if (!oc) {
1200             warn_report("global %s.%s has invalid class name",
1201                         prop->driver, prop->property);
1202             ret = 1;
1203             continue;
1204         }
1205         dc = DEVICE_CLASS(oc);
1206         if (!dc->hotpluggable && !prop->used) {
1207             warn_report("global %s.%s=%s not used",
1208                         prop->driver, prop->property, prop->value);
1209             ret = 1;
1210             continue;
1211         }
1212     }
1213     return ret;
1214 }
1215 
1216 void qdev_prop_set_globals(DeviceState *dev)
1217 {
1218     object_apply_global_props(OBJECT(dev), global_props(),
1219                               dev->hotplugged ? NULL : &error_fatal);
1220 }
1221 
1222 /* --- 64bit unsigned int 'size' type --- */
1223 
1224 static void get_size(Object *obj, Visitor *v, const char *name, void *opaque,
1225                      Error **errp)
1226 {
1227     DeviceState *dev = DEVICE(obj);
1228     Property *prop = opaque;
1229     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
1230 
1231     visit_type_size(v, name, ptr, errp);
1232 }
1233 
1234 static void set_size(Object *obj, Visitor *v, const char *name, void *opaque,
1235                      Error **errp)
1236 {
1237     DeviceState *dev = DEVICE(obj);
1238     Property *prop = opaque;
1239     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
1240 
1241     visit_type_size(v, name, ptr, errp);
1242 }
1243 
1244 const PropertyInfo qdev_prop_size = {
1245     .name  = "size",
1246     .get = get_size,
1247     .set = set_size,
1248     .set_default_value = set_default_value_uint,
1249 };
1250 
1251 /* --- object link property --- */
1252 
1253 static void create_link_property(ObjectClass *oc, Property *prop)
1254 {
1255     object_class_property_add_link(oc, prop->name, prop->link_type,
1256                                    prop->offset,
1257                                    qdev_prop_allow_set_link_before_realize,
1258                                    OBJ_PROP_LINK_STRONG);
1259 }
1260 
1261 const PropertyInfo qdev_prop_link = {
1262     .name = "link",
1263     .create = create_link_property,
1264 };
1265 
1266 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
1267 
1268 const PropertyInfo qdev_prop_off_auto_pcibar = {
1269     .name = "OffAutoPCIBAR",
1270     .description = "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
1271     .enum_table = &OffAutoPCIBAR_lookup,
1272     .get = get_enum,
1273     .set = set_enum,
1274     .set_default_value = set_default_value_enum,
1275 };
1276 
1277 /* --- PCIELinkSpeed 2_5/5/8/16 -- */
1278 
1279 static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
1280                                    void *opaque, Error **errp)
1281 {
1282     DeviceState *dev = DEVICE(obj);
1283     Property *prop = opaque;
1284     PCIExpLinkSpeed *p = qdev_get_prop_ptr(dev, prop);
1285     int speed;
1286 
1287     switch (*p) {
1288     case QEMU_PCI_EXP_LNK_2_5GT:
1289         speed = PCIE_LINK_SPEED_2_5;
1290         break;
1291     case QEMU_PCI_EXP_LNK_5GT:
1292         speed = PCIE_LINK_SPEED_5;
1293         break;
1294     case QEMU_PCI_EXP_LNK_8GT:
1295         speed = PCIE_LINK_SPEED_8;
1296         break;
1297     case QEMU_PCI_EXP_LNK_16GT:
1298         speed = PCIE_LINK_SPEED_16;
1299         break;
1300     default:
1301         /* Unreachable */
1302         abort();
1303     }
1304 
1305     visit_type_enum(v, prop->name, &speed, prop->info->enum_table, errp);
1306 }
1307 
1308 static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
1309                                    void *opaque, Error **errp)
1310 {
1311     DeviceState *dev = DEVICE(obj);
1312     Property *prop = opaque;
1313     PCIExpLinkSpeed *p = qdev_get_prop_ptr(dev, prop);
1314     int speed;
1315     Error *local_err = NULL;
1316 
1317     if (dev->realized) {
1318         qdev_prop_set_after_realize(dev, name, errp);
1319         return;
1320     }
1321 
1322     visit_type_enum(v, prop->name, &speed, prop->info->enum_table, &local_err);
1323     if (local_err) {
1324         error_propagate(errp, local_err);
1325         return;
1326     }
1327 
1328     switch (speed) {
1329     case PCIE_LINK_SPEED_2_5:
1330         *p = QEMU_PCI_EXP_LNK_2_5GT;
1331         break;
1332     case PCIE_LINK_SPEED_5:
1333         *p = QEMU_PCI_EXP_LNK_5GT;
1334         break;
1335     case PCIE_LINK_SPEED_8:
1336         *p = QEMU_PCI_EXP_LNK_8GT;
1337         break;
1338     case PCIE_LINK_SPEED_16:
1339         *p = QEMU_PCI_EXP_LNK_16GT;
1340         break;
1341     default:
1342         /* Unreachable */
1343         abort();
1344     }
1345 }
1346 
1347 const PropertyInfo qdev_prop_pcie_link_speed = {
1348     .name = "PCIELinkSpeed",
1349     .description = "2_5/5/8/16",
1350     .enum_table = &PCIELinkSpeed_lookup,
1351     .get = get_prop_pcielinkspeed,
1352     .set = set_prop_pcielinkspeed,
1353     .set_default_value = set_default_value_enum,
1354 };
1355 
1356 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
1357 
1358 static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1359                                    void *opaque, Error **errp)
1360 {
1361     DeviceState *dev = DEVICE(obj);
1362     Property *prop = opaque;
1363     PCIExpLinkWidth *p = qdev_get_prop_ptr(dev, prop);
1364     int width;
1365 
1366     switch (*p) {
1367     case QEMU_PCI_EXP_LNK_X1:
1368         width = PCIE_LINK_WIDTH_1;
1369         break;
1370     case QEMU_PCI_EXP_LNK_X2:
1371         width = PCIE_LINK_WIDTH_2;
1372         break;
1373     case QEMU_PCI_EXP_LNK_X4:
1374         width = PCIE_LINK_WIDTH_4;
1375         break;
1376     case QEMU_PCI_EXP_LNK_X8:
1377         width = PCIE_LINK_WIDTH_8;
1378         break;
1379     case QEMU_PCI_EXP_LNK_X12:
1380         width = PCIE_LINK_WIDTH_12;
1381         break;
1382     case QEMU_PCI_EXP_LNK_X16:
1383         width = PCIE_LINK_WIDTH_16;
1384         break;
1385     case QEMU_PCI_EXP_LNK_X32:
1386         width = PCIE_LINK_WIDTH_32;
1387         break;
1388     default:
1389         /* Unreachable */
1390         abort();
1391     }
1392 
1393     visit_type_enum(v, prop->name, &width, prop->info->enum_table, errp);
1394 }
1395 
1396 static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1397                                    void *opaque, Error **errp)
1398 {
1399     DeviceState *dev = DEVICE(obj);
1400     Property *prop = opaque;
1401     PCIExpLinkWidth *p = qdev_get_prop_ptr(dev, prop);
1402     int width;
1403     Error *local_err = NULL;
1404 
1405     if (dev->realized) {
1406         qdev_prop_set_after_realize(dev, name, errp);
1407         return;
1408     }
1409 
1410     visit_type_enum(v, prop->name, &width, prop->info->enum_table, &local_err);
1411     if (local_err) {
1412         error_propagate(errp, local_err);
1413         return;
1414     }
1415 
1416     switch (width) {
1417     case PCIE_LINK_WIDTH_1:
1418         *p = QEMU_PCI_EXP_LNK_X1;
1419         break;
1420     case PCIE_LINK_WIDTH_2:
1421         *p = QEMU_PCI_EXP_LNK_X2;
1422         break;
1423     case PCIE_LINK_WIDTH_4:
1424         *p = QEMU_PCI_EXP_LNK_X4;
1425         break;
1426     case PCIE_LINK_WIDTH_8:
1427         *p = QEMU_PCI_EXP_LNK_X8;
1428         break;
1429     case PCIE_LINK_WIDTH_12:
1430         *p = QEMU_PCI_EXP_LNK_X12;
1431         break;
1432     case PCIE_LINK_WIDTH_16:
1433         *p = QEMU_PCI_EXP_LNK_X16;
1434         break;
1435     case PCIE_LINK_WIDTH_32:
1436         *p = QEMU_PCI_EXP_LNK_X32;
1437         break;
1438     default:
1439         /* Unreachable */
1440         abort();
1441     }
1442 }
1443 
1444 const PropertyInfo qdev_prop_pcie_link_width = {
1445     .name = "PCIELinkWidth",
1446     .description = "1/2/4/8/12/16/32",
1447     .enum_table = &PCIELinkWidth_lookup,
1448     .get = get_prop_pcielinkwidth,
1449     .set = set_prop_pcielinkwidth,
1450     .set_default_value = set_default_value_enum,
1451 };
1452