xref: /openbmc/qemu/hw/core/qdev-properties.c (revision 0ce46ab5)
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 "hw/block/block.h"
12 #include "net/hub.h"
13 #include "qapi/visitor.h"
14 #include "chardev/char.h"
15 #include "qemu/uuid.h"
16 
17 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
18                                   Error **errp)
19 {
20     if (dev->id) {
21         error_setg(errp, "Attempt to set property '%s' on device '%s' "
22                    "(type '%s') after it was realized", name, dev->id,
23                    object_get_typename(OBJECT(dev)));
24     } else {
25         error_setg(errp, "Attempt to set property '%s' on anonymous device "
26                    "(type '%s') after it was realized", name,
27                    object_get_typename(OBJECT(dev)));
28     }
29 }
30 
31 void qdev_prop_allow_set_link_before_realize(const Object *obj,
32                                              const char *name,
33                                              Object *val, Error **errp)
34 {
35     DeviceState *dev = DEVICE(obj);
36 
37     if (dev->realized) {
38         error_setg(errp, "Attempt to set link property '%s' on device '%s' "
39                    "(type '%s') after it was realized",
40                    name, dev->id, object_get_typename(obj));
41     }
42 }
43 
44 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
45 {
46     void *ptr = dev;
47     ptr += prop->offset;
48     return ptr;
49 }
50 
51 static void get_enum(Object *obj, Visitor *v, const char *name, void *opaque,
52                      Error **errp)
53 {
54     DeviceState *dev = DEVICE(obj);
55     Property *prop = opaque;
56     int *ptr = qdev_get_prop_ptr(dev, prop);
57 
58     visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
59 }
60 
61 static void set_enum(Object *obj, Visitor *v, const char *name, void *opaque,
62                      Error **errp)
63 {
64     DeviceState *dev = DEVICE(obj);
65     Property *prop = opaque;
66     int *ptr = qdev_get_prop_ptr(dev, prop);
67 
68     if (dev->realized) {
69         qdev_prop_set_after_realize(dev, name, errp);
70         return;
71     }
72 
73     visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
74 }
75 
76 static void set_default_value_enum(Object *obj, const Property *prop)
77 {
78     object_property_set_str(obj,
79                             qapi_enum_lookup(prop->info->enum_table,
80                                              prop->defval.i),
81                             prop->name, &error_abort);
82 }
83 
84 /* Bit */
85 
86 static uint32_t qdev_get_prop_mask(Property *prop)
87 {
88     assert(prop->info == &qdev_prop_bit);
89     return 0x1 << prop->bitnr;
90 }
91 
92 static void bit_prop_set(DeviceState *dev, Property *props, bool val)
93 {
94     uint32_t *p = qdev_get_prop_ptr(dev, props);
95     uint32_t mask = qdev_get_prop_mask(props);
96     if (val) {
97         *p |= mask;
98     } else {
99         *p &= ~mask;
100     }
101 }
102 
103 static void prop_get_bit(Object *obj, Visitor *v, const char *name,
104                          void *opaque, Error **errp)
105 {
106     DeviceState *dev = DEVICE(obj);
107     Property *prop = opaque;
108     uint32_t *p = qdev_get_prop_ptr(dev, prop);
109     bool value = (*p & qdev_get_prop_mask(prop)) != 0;
110 
111     visit_type_bool(v, name, &value, errp);
112 }
113 
114 static void prop_set_bit(Object *obj, Visitor *v, const char *name,
115                          void *opaque, Error **errp)
116 {
117     DeviceState *dev = DEVICE(obj);
118     Property *prop = opaque;
119     Error *local_err = NULL;
120     bool value;
121 
122     if (dev->realized) {
123         qdev_prop_set_after_realize(dev, name, errp);
124         return;
125     }
126 
127     visit_type_bool(v, name, &value, &local_err);
128     if (local_err) {
129         error_propagate(errp, local_err);
130         return;
131     }
132     bit_prop_set(dev, prop, value);
133 }
134 
135 static void set_default_value_bool(Object *obj, const Property *prop)
136 {
137     object_property_set_bool(obj, prop->defval.u, prop->name, &error_abort);
138 }
139 
140 const PropertyInfo qdev_prop_bit = {
141     .name  = "bool",
142     .description = "on/off",
143     .get   = prop_get_bit,
144     .set   = prop_set_bit,
145     .set_default_value = set_default_value_bool,
146 };
147 
148 /* Bit64 */
149 
150 static uint64_t qdev_get_prop_mask64(Property *prop)
151 {
152     assert(prop->info == &qdev_prop_bit64);
153     return 0x1ull << prop->bitnr;
154 }
155 
156 static void bit64_prop_set(DeviceState *dev, Property *props, bool val)
157 {
158     uint64_t *p = qdev_get_prop_ptr(dev, props);
159     uint64_t mask = qdev_get_prop_mask64(props);
160     if (val) {
161         *p |= mask;
162     } else {
163         *p &= ~mask;
164     }
165 }
166 
167 static void prop_get_bit64(Object *obj, Visitor *v, const char *name,
168                            void *opaque, Error **errp)
169 {
170     DeviceState *dev = DEVICE(obj);
171     Property *prop = opaque;
172     uint64_t *p = qdev_get_prop_ptr(dev, prop);
173     bool value = (*p & qdev_get_prop_mask64(prop)) != 0;
174 
175     visit_type_bool(v, name, &value, errp);
176 }
177 
178 static void prop_set_bit64(Object *obj, Visitor *v, const char *name,
179                            void *opaque, Error **errp)
180 {
181     DeviceState *dev = DEVICE(obj);
182     Property *prop = opaque;
183     Error *local_err = NULL;
184     bool value;
185 
186     if (dev->realized) {
187         qdev_prop_set_after_realize(dev, name, errp);
188         return;
189     }
190 
191     visit_type_bool(v, name, &value, &local_err);
192     if (local_err) {
193         error_propagate(errp, local_err);
194         return;
195     }
196     bit64_prop_set(dev, prop, value);
197 }
198 
199 const PropertyInfo qdev_prop_bit64 = {
200     .name  = "bool",
201     .description = "on/off",
202     .get   = prop_get_bit64,
203     .set   = prop_set_bit64,
204     .set_default_value = set_default_value_bool,
205 };
206 
207 /* --- bool --- */
208 
209 static void get_bool(Object *obj, Visitor *v, const char *name, void *opaque,
210                      Error **errp)
211 {
212     DeviceState *dev = DEVICE(obj);
213     Property *prop = opaque;
214     bool *ptr = qdev_get_prop_ptr(dev, prop);
215 
216     visit_type_bool(v, name, ptr, errp);
217 }
218 
219 static void set_bool(Object *obj, Visitor *v, const char *name, void *opaque,
220                      Error **errp)
221 {
222     DeviceState *dev = DEVICE(obj);
223     Property *prop = opaque;
224     bool *ptr = qdev_get_prop_ptr(dev, prop);
225 
226     if (dev->realized) {
227         qdev_prop_set_after_realize(dev, name, errp);
228         return;
229     }
230 
231     visit_type_bool(v, name, ptr, errp);
232 }
233 
234 const PropertyInfo qdev_prop_bool = {
235     .name  = "bool",
236     .get   = get_bool,
237     .set   = set_bool,
238     .set_default_value = set_default_value_bool,
239 };
240 
241 /* --- 8bit integer --- */
242 
243 static void get_uint8(Object *obj, Visitor *v, const char *name, void *opaque,
244                       Error **errp)
245 {
246     DeviceState *dev = DEVICE(obj);
247     Property *prop = opaque;
248     uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
249 
250     visit_type_uint8(v, name, ptr, errp);
251 }
252 
253 static void set_uint8(Object *obj, Visitor *v, const char *name, void *opaque,
254                       Error **errp)
255 {
256     DeviceState *dev = DEVICE(obj);
257     Property *prop = opaque;
258     uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
259 
260     if (dev->realized) {
261         qdev_prop_set_after_realize(dev, name, errp);
262         return;
263     }
264 
265     visit_type_uint8(v, name, ptr, errp);
266 }
267 
268 static void set_default_value_int(Object *obj, const Property *prop)
269 {
270     object_property_set_int(obj, prop->defval.i, prop->name, &error_abort);
271 }
272 
273 static void set_default_value_uint(Object *obj, const Property *prop)
274 {
275     object_property_set_uint(obj, prop->defval.u, prop->name, &error_abort);
276 }
277 
278 const PropertyInfo qdev_prop_uint8 = {
279     .name  = "uint8",
280     .get   = get_uint8,
281     .set   = set_uint8,
282     .set_default_value = set_default_value_uint,
283 };
284 
285 /* --- 16bit integer --- */
286 
287 static void get_uint16(Object *obj, Visitor *v, const char *name,
288                        void *opaque, Error **errp)
289 {
290     DeviceState *dev = DEVICE(obj);
291     Property *prop = opaque;
292     uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
293 
294     visit_type_uint16(v, name, ptr, errp);
295 }
296 
297 static void set_uint16(Object *obj, Visitor *v, const char *name,
298                        void *opaque, Error **errp)
299 {
300     DeviceState *dev = DEVICE(obj);
301     Property *prop = opaque;
302     uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
303 
304     if (dev->realized) {
305         qdev_prop_set_after_realize(dev, name, errp);
306         return;
307     }
308 
309     visit_type_uint16(v, name, ptr, errp);
310 }
311 
312 const PropertyInfo qdev_prop_uint16 = {
313     .name  = "uint16",
314     .get   = get_uint16,
315     .set   = set_uint16,
316     .set_default_value = set_default_value_uint,
317 };
318 
319 /* --- 32bit integer --- */
320 
321 static void get_uint32(Object *obj, Visitor *v, const char *name,
322                        void *opaque, Error **errp)
323 {
324     DeviceState *dev = DEVICE(obj);
325     Property *prop = opaque;
326     uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
327 
328     visit_type_uint32(v, name, ptr, errp);
329 }
330 
331 static void set_uint32(Object *obj, Visitor *v, const char *name,
332                        void *opaque, Error **errp)
333 {
334     DeviceState *dev = DEVICE(obj);
335     Property *prop = opaque;
336     uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
337 
338     if (dev->realized) {
339         qdev_prop_set_after_realize(dev, name, errp);
340         return;
341     }
342 
343     visit_type_uint32(v, name, ptr, errp);
344 }
345 
346 static void get_int32(Object *obj, Visitor *v, const char *name, void *opaque,
347                       Error **errp)
348 {
349     DeviceState *dev = DEVICE(obj);
350     Property *prop = opaque;
351     int32_t *ptr = qdev_get_prop_ptr(dev, prop);
352 
353     visit_type_int32(v, name, ptr, errp);
354 }
355 
356 static void set_int32(Object *obj, Visitor *v, const char *name, void *opaque,
357                       Error **errp)
358 {
359     DeviceState *dev = DEVICE(obj);
360     Property *prop = opaque;
361     int32_t *ptr = qdev_get_prop_ptr(dev, prop);
362 
363     if (dev->realized) {
364         qdev_prop_set_after_realize(dev, name, errp);
365         return;
366     }
367 
368     visit_type_int32(v, name, ptr, errp);
369 }
370 
371 const PropertyInfo qdev_prop_uint32 = {
372     .name  = "uint32",
373     .get   = get_uint32,
374     .set   = set_uint32,
375     .set_default_value = set_default_value_uint,
376 };
377 
378 const PropertyInfo qdev_prop_int32 = {
379     .name  = "int32",
380     .get   = get_int32,
381     .set   = set_int32,
382     .set_default_value = set_default_value_int,
383 };
384 
385 /* --- 64bit integer --- */
386 
387 static void get_uint64(Object *obj, Visitor *v, const char *name,
388                        void *opaque, Error **errp)
389 {
390     DeviceState *dev = DEVICE(obj);
391     Property *prop = opaque;
392     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
393 
394     visit_type_uint64(v, name, ptr, errp);
395 }
396 
397 static void set_uint64(Object *obj, Visitor *v, const char *name,
398                        void *opaque, Error **errp)
399 {
400     DeviceState *dev = DEVICE(obj);
401     Property *prop = opaque;
402     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
403 
404     if (dev->realized) {
405         qdev_prop_set_after_realize(dev, name, errp);
406         return;
407     }
408 
409     visit_type_uint64(v, name, ptr, errp);
410 }
411 
412 static void get_int64(Object *obj, Visitor *v, const char *name,
413                       void *opaque, Error **errp)
414 {
415     DeviceState *dev = DEVICE(obj);
416     Property *prop = opaque;
417     int64_t *ptr = qdev_get_prop_ptr(dev, prop);
418 
419     visit_type_int64(v, name, ptr, errp);
420 }
421 
422 static void set_int64(Object *obj, Visitor *v, const char *name,
423                       void *opaque, Error **errp)
424 {
425     DeviceState *dev = DEVICE(obj);
426     Property *prop = opaque;
427     int64_t *ptr = qdev_get_prop_ptr(dev, prop);
428 
429     if (dev->realized) {
430         qdev_prop_set_after_realize(dev, name, errp);
431         return;
432     }
433 
434     visit_type_int64(v, name, ptr, errp);
435 }
436 
437 const PropertyInfo qdev_prop_uint64 = {
438     .name  = "uint64",
439     .get   = get_uint64,
440     .set   = set_uint64,
441     .set_default_value = set_default_value_uint,
442 };
443 
444 const PropertyInfo qdev_prop_int64 = {
445     .name  = "int64",
446     .get   = get_int64,
447     .set   = set_int64,
448     .set_default_value = set_default_value_int,
449 };
450 
451 /* --- string --- */
452 
453 static void release_string(Object *obj, const char *name, void *opaque)
454 {
455     Property *prop = opaque;
456     g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj), prop));
457 }
458 
459 static void get_string(Object *obj, Visitor *v, const char *name,
460                        void *opaque, Error **errp)
461 {
462     DeviceState *dev = DEVICE(obj);
463     Property *prop = opaque;
464     char **ptr = qdev_get_prop_ptr(dev, prop);
465 
466     if (!*ptr) {
467         char *str = (char *)"";
468         visit_type_str(v, name, &str, errp);
469     } else {
470         visit_type_str(v, name, ptr, errp);
471     }
472 }
473 
474 static void set_string(Object *obj, Visitor *v, const char *name,
475                        void *opaque, Error **errp)
476 {
477     DeviceState *dev = DEVICE(obj);
478     Property *prop = opaque;
479     char **ptr = qdev_get_prop_ptr(dev, prop);
480     Error *local_err = NULL;
481     char *str;
482 
483     if (dev->realized) {
484         qdev_prop_set_after_realize(dev, name, errp);
485         return;
486     }
487 
488     visit_type_str(v, name, &str, &local_err);
489     if (local_err) {
490         error_propagate(errp, local_err);
491         return;
492     }
493     g_free(*ptr);
494     *ptr = str;
495 }
496 
497 const PropertyInfo qdev_prop_string = {
498     .name  = "str",
499     .release = release_string,
500     .get   = get_string,
501     .set   = set_string,
502 };
503 
504 /* --- mac address --- */
505 
506 /*
507  * accepted syntax versions:
508  *   01:02:03:04:05:06
509  *   01-02-03-04-05-06
510  */
511 static void get_mac(Object *obj, Visitor *v, const char *name, void *opaque,
512                     Error **errp)
513 {
514     DeviceState *dev = DEVICE(obj);
515     Property *prop = opaque;
516     MACAddr *mac = qdev_get_prop_ptr(dev, prop);
517     char buffer[2 * 6 + 5 + 1];
518     char *p = buffer;
519 
520     snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x",
521              mac->a[0], mac->a[1], mac->a[2],
522              mac->a[3], mac->a[4], mac->a[5]);
523 
524     visit_type_str(v, name, &p, errp);
525 }
526 
527 static void set_mac(Object *obj, Visitor *v, const char *name, void *opaque,
528                     Error **errp)
529 {
530     DeviceState *dev = DEVICE(obj);
531     Property *prop = opaque;
532     MACAddr *mac = qdev_get_prop_ptr(dev, prop);
533     Error *local_err = NULL;
534     int i, pos;
535     char *str, *p;
536 
537     if (dev->realized) {
538         qdev_prop_set_after_realize(dev, name, errp);
539         return;
540     }
541 
542     visit_type_str(v, name, &str, &local_err);
543     if (local_err) {
544         error_propagate(errp, local_err);
545         return;
546     }
547 
548     for (i = 0, pos = 0; i < 6; i++, pos += 3) {
549         if (!qemu_isxdigit(str[pos])) {
550             goto inval;
551         }
552         if (!qemu_isxdigit(str[pos+1])) {
553             goto inval;
554         }
555         if (i == 5) {
556             if (str[pos+2] != '\0') {
557                 goto inval;
558             }
559         } else {
560             if (str[pos+2] != ':' && str[pos+2] != '-') {
561                 goto inval;
562             }
563         }
564         mac->a[i] = strtol(str+pos, &p, 16);
565     }
566     g_free(str);
567     return;
568 
569 inval:
570     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
571     g_free(str);
572 }
573 
574 const PropertyInfo qdev_prop_macaddr = {
575     .name  = "str",
576     .description = "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56",
577     .get   = get_mac,
578     .set   = set_mac,
579 };
580 
581 /* --- on/off/auto --- */
582 
583 const PropertyInfo qdev_prop_on_off_auto = {
584     .name = "OnOffAuto",
585     .description = "on/off/auto",
586     .enum_table = &OnOffAuto_lookup,
587     .get = get_enum,
588     .set = set_enum,
589     .set_default_value = set_default_value_enum,
590 };
591 
592 /* --- lost tick policy --- */
593 
594 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
595 
596 const PropertyInfo qdev_prop_losttickpolicy = {
597     .name  = "LostTickPolicy",
598     .enum_table  = &LostTickPolicy_lookup,
599     .get   = get_enum,
600     .set   = set_enum,
601     .set_default_value = set_default_value_enum,
602 };
603 
604 /* --- Block device error handling policy --- */
605 
606 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError) != sizeof(int));
607 
608 const PropertyInfo qdev_prop_blockdev_on_error = {
609     .name = "BlockdevOnError",
610     .description = "Error handling policy, "
611                    "report/ignore/enospc/stop/auto",
612     .enum_table = &BlockdevOnError_lookup,
613     .get = get_enum,
614     .set = set_enum,
615     .set_default_value = set_default_value_enum,
616 };
617 
618 /* --- BIOS CHS translation */
619 
620 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation) != sizeof(int));
621 
622 const PropertyInfo qdev_prop_bios_chs_trans = {
623     .name = "BiosAtaTranslation",
624     .description = "Logical CHS translation algorithm, "
625                    "auto/none/lba/large/rechs",
626     .enum_table = &BiosAtaTranslation_lookup,
627     .get = get_enum,
628     .set = set_enum,
629     .set_default_value = set_default_value_enum,
630 };
631 
632 /* --- FDC default drive types */
633 
634 const PropertyInfo qdev_prop_fdc_drive_type = {
635     .name = "FdcDriveType",
636     .description = "FDC drive type, "
637                    "144/288/120/none/auto",
638     .enum_table = &FloppyDriveType_lookup,
639     .get = get_enum,
640     .set = set_enum,
641     .set_default_value = set_default_value_enum,
642 };
643 
644 /* --- pci address --- */
645 
646 /*
647  * bus-local address, i.e. "$slot" or "$slot.$fn"
648  */
649 static void set_pci_devfn(Object *obj, Visitor *v, const char *name,
650                           void *opaque, Error **errp)
651 {
652     DeviceState *dev = DEVICE(obj);
653     Property *prop = opaque;
654     int32_t value, *ptr = qdev_get_prop_ptr(dev, prop);
655     unsigned int slot, fn, n;
656     Error *local_err = NULL;
657     char *str;
658 
659     if (dev->realized) {
660         qdev_prop_set_after_realize(dev, name, errp);
661         return;
662     }
663 
664     visit_type_str(v, name, &str, &local_err);
665     if (local_err) {
666         error_free(local_err);
667         local_err = NULL;
668         visit_type_int32(v, name, &value, &local_err);
669         if (local_err) {
670             error_propagate(errp, local_err);
671         } else if (value < -1 || value > 255) {
672             error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
673                        name ? name : "null", "pci_devfn");
674         } else {
675             *ptr = value;
676         }
677         return;
678     }
679 
680     if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
681         fn = 0;
682         if (sscanf(str, "%x%n", &slot, &n) != 1) {
683             goto invalid;
684         }
685     }
686     if (str[n] != '\0' || fn > 7 || slot > 31) {
687         goto invalid;
688     }
689     *ptr = slot << 3 | fn;
690     g_free(str);
691     return;
692 
693 invalid:
694     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
695     g_free(str);
696 }
697 
698 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest,
699                            size_t len)
700 {
701     int32_t *ptr = qdev_get_prop_ptr(dev, prop);
702 
703     if (*ptr == -1) {
704         return snprintf(dest, len, "<unset>");
705     } else {
706         return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
707     }
708 }
709 
710 const PropertyInfo qdev_prop_pci_devfn = {
711     .name  = "int32",
712     .description = "Slot and optional function number, example: 06.0 or 06",
713     .print = print_pci_devfn,
714     .get   = get_int32,
715     .set   = set_pci_devfn,
716     .set_default_value = set_default_value_int,
717 };
718 
719 /* --- blocksize --- */
720 
721 static void set_blocksize(Object *obj, Visitor *v, const char *name,
722                           void *opaque, Error **errp)
723 {
724     DeviceState *dev = DEVICE(obj);
725     Property *prop = opaque;
726     uint16_t value, *ptr = qdev_get_prop_ptr(dev, prop);
727     Error *local_err = NULL;
728     const int64_t min = 512;
729     const int64_t max = 32768;
730 
731     if (dev->realized) {
732         qdev_prop_set_after_realize(dev, name, errp);
733         return;
734     }
735 
736     visit_type_uint16(v, name, &value, &local_err);
737     if (local_err) {
738         error_propagate(errp, local_err);
739         return;
740     }
741     /* value of 0 means "unset" */
742     if (value && (value < min || value > max)) {
743         error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
744                    dev->id ? : "", name, (int64_t)value, min, max);
745         return;
746     }
747 
748     /* We rely on power-of-2 blocksizes for bitmasks */
749     if ((value & (value - 1)) != 0) {
750         error_setg(errp,
751                   "Property %s.%s doesn't take value '%" PRId64 "', it's not a power of 2",
752                   dev->id ?: "", name, (int64_t)value);
753         return;
754     }
755 
756     *ptr = value;
757 }
758 
759 const PropertyInfo qdev_prop_blocksize = {
760     .name  = "uint16",
761     .description = "A power of two between 512 and 32768",
762     .get   = get_uint16,
763     .set   = set_blocksize,
764     .set_default_value = set_default_value_uint,
765 };
766 
767 /* --- pci host address --- */
768 
769 static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
770                                  void *opaque, Error **errp)
771 {
772     DeviceState *dev = DEVICE(obj);
773     Property *prop = opaque;
774     PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop);
775     char buffer[] = "ffff:ff:ff.f";
776     char *p = buffer;
777     int rc = 0;
778 
779     /*
780      * Catch "invalid" device reference from vfio-pci and allow the
781      * default buffer representing the non-existent device to be used.
782      */
783     if (~addr->domain || ~addr->bus || ~addr->slot || ~addr->function) {
784         rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%0d",
785                       addr->domain, addr->bus, addr->slot, addr->function);
786         assert(rc == sizeof(buffer) - 1);
787     }
788 
789     visit_type_str(v, name, &p, errp);
790 }
791 
792 /*
793  * Parse [<domain>:]<bus>:<slot>.<func>
794  *   if <domain> is not supplied, it's assumed to be 0.
795  */
796 static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
797                                  void *opaque, Error **errp)
798 {
799     DeviceState *dev = DEVICE(obj);
800     Property *prop = opaque;
801     PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop);
802     Error *local_err = NULL;
803     char *str, *p;
804     char *e;
805     unsigned long val;
806     unsigned long dom = 0, bus = 0;
807     unsigned int slot = 0, func = 0;
808 
809     if (dev->realized) {
810         qdev_prop_set_after_realize(dev, name, errp);
811         return;
812     }
813 
814     visit_type_str(v, name, &str, &local_err);
815     if (local_err) {
816         error_propagate(errp, local_err);
817         return;
818     }
819 
820     p = str;
821     val = strtoul(p, &e, 16);
822     if (e == p || *e != ':') {
823         goto inval;
824     }
825     bus = val;
826 
827     p = e + 1;
828     val = strtoul(p, &e, 16);
829     if (e == p) {
830         goto inval;
831     }
832     if (*e == ':') {
833         dom = bus;
834         bus = val;
835         p = e + 1;
836         val = strtoul(p, &e, 16);
837         if (e == p) {
838             goto inval;
839         }
840     }
841     slot = val;
842 
843     if (*e != '.') {
844         goto inval;
845     }
846     p = e + 1;
847     val = strtoul(p, &e, 10);
848     if (e == p) {
849         goto inval;
850     }
851     func = val;
852 
853     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
854         goto inval;
855     }
856 
857     if (*e) {
858         goto inval;
859     }
860 
861     addr->domain = dom;
862     addr->bus = bus;
863     addr->slot = slot;
864     addr->function = func;
865 
866     g_free(str);
867     return;
868 
869 inval:
870     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
871     g_free(str);
872 }
873 
874 const PropertyInfo qdev_prop_pci_host_devaddr = {
875     .name = "str",
876     .description = "Address (bus/device/function) of "
877                    "the host device, example: 04:10.0",
878     .get = get_pci_host_devaddr,
879     .set = set_pci_host_devaddr,
880 };
881 
882 /* --- UUID --- */
883 
884 static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
885                      Error **errp)
886 {
887     DeviceState *dev = DEVICE(obj);
888     Property *prop = opaque;
889     QemuUUID *uuid = qdev_get_prop_ptr(dev, prop);
890     char buffer[UUID_FMT_LEN + 1];
891     char *p = buffer;
892 
893     qemu_uuid_unparse(uuid, buffer);
894 
895     visit_type_str(v, name, &p, errp);
896 }
897 
898 #define UUID_VALUE_AUTO        "auto"
899 
900 static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
901                     Error **errp)
902 {
903     DeviceState *dev = DEVICE(obj);
904     Property *prop = opaque;
905     QemuUUID *uuid = qdev_get_prop_ptr(dev, prop);
906     Error *local_err = NULL;
907     char *str;
908 
909     if (dev->realized) {
910         qdev_prop_set_after_realize(dev, name, errp);
911         return;
912     }
913 
914     visit_type_str(v, name, &str, &local_err);
915     if (local_err) {
916         error_propagate(errp, local_err);
917         return;
918     }
919 
920     if (!strcmp(str, UUID_VALUE_AUTO)) {
921         qemu_uuid_generate(uuid);
922     } else if (qemu_uuid_parse(str, uuid) < 0) {
923         error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
924     }
925     g_free(str);
926 }
927 
928 static void set_default_uuid_auto(Object *obj, const Property *prop)
929 {
930     object_property_set_str(obj, UUID_VALUE_AUTO, prop->name, &error_abort);
931 }
932 
933 const PropertyInfo qdev_prop_uuid = {
934     .name  = "str",
935     .description = "UUID (aka GUID) or \"" UUID_VALUE_AUTO
936         "\" for random value (default)",
937     .get   = get_uuid,
938     .set   = set_uuid,
939     .set_default_value = set_default_uuid_auto,
940 };
941 
942 /* --- support for array properties --- */
943 
944 /* Used as an opaque for the object properties we add for each
945  * array element. Note that the struct Property must be first
946  * in the struct so that a pointer to this works as the opaque
947  * for the underlying element's property hooks as well as for
948  * our own release callback.
949  */
950 typedef struct {
951     struct Property prop;
952     char *propname;
953     ObjectPropertyRelease *release;
954 } ArrayElementProperty;
955 
956 /* object property release callback for array element properties:
957  * we call the underlying element's property release hook, and
958  * then free the memory we allocated when we added the property.
959  */
960 static void array_element_release(Object *obj, const char *name, void *opaque)
961 {
962     ArrayElementProperty *p = opaque;
963     if (p->release) {
964         p->release(obj, name, opaque);
965     }
966     g_free(p->propname);
967     g_free(p);
968 }
969 
970 static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
971                               void *opaque, Error **errp)
972 {
973     /* Setter for the property which defines the length of a
974      * variable-sized property array. As well as actually setting the
975      * array-length field in the device struct, we have to create the
976      * array itself and dynamically add the corresponding properties.
977      */
978     DeviceState *dev = DEVICE(obj);
979     Property *prop = opaque;
980     uint32_t *alenptr = qdev_get_prop_ptr(dev, prop);
981     void **arrayptr = (void *)dev + prop->arrayoffset;
982     Error *local_err = NULL;
983     void *eltptr;
984     const char *arrayname;
985     int i;
986 
987     if (dev->realized) {
988         qdev_prop_set_after_realize(dev, name, errp);
989         return;
990     }
991     if (*alenptr) {
992         error_setg(errp, "array size property %s may not be set more than once",
993                    name);
994         return;
995     }
996     visit_type_uint32(v, name, alenptr, &local_err);
997     if (local_err) {
998         error_propagate(errp, local_err);
999         return;
1000     }
1001     if (!*alenptr) {
1002         return;
1003     }
1004 
1005     /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix;
1006      * strip it off so we can get the name of the array itself.
1007      */
1008     assert(strncmp(name, PROP_ARRAY_LEN_PREFIX,
1009                    strlen(PROP_ARRAY_LEN_PREFIX)) == 0);
1010     arrayname = name + strlen(PROP_ARRAY_LEN_PREFIX);
1011 
1012     /* Note that it is the responsibility of the individual device's deinit
1013      * to free the array proper.
1014      */
1015     *arrayptr = eltptr = g_malloc0(*alenptr * prop->arrayfieldsize);
1016     for (i = 0; i < *alenptr; i++, eltptr += prop->arrayfieldsize) {
1017         char *propname = g_strdup_printf("%s[%d]", arrayname, i);
1018         ArrayElementProperty *arrayprop = g_new0(ArrayElementProperty, 1);
1019         arrayprop->release = prop->arrayinfo->release;
1020         arrayprop->propname = propname;
1021         arrayprop->prop.info = prop->arrayinfo;
1022         arrayprop->prop.name = propname;
1023         /* This ugly piece of pointer arithmetic sets up the offset so
1024          * that when the underlying get/set hooks call qdev_get_prop_ptr
1025          * they get the right answer despite the array element not actually
1026          * being inside the device struct.
1027          */
1028         arrayprop->prop.offset = eltptr - (void *)dev;
1029         assert(qdev_get_prop_ptr(dev, &arrayprop->prop) == eltptr);
1030         object_property_add(obj, propname,
1031                             arrayprop->prop.info->name,
1032                             arrayprop->prop.info->get,
1033                             arrayprop->prop.info->set,
1034                             array_element_release,
1035                             arrayprop, &local_err);
1036         if (local_err) {
1037             error_propagate(errp, local_err);
1038             return;
1039         }
1040     }
1041 }
1042 
1043 const PropertyInfo qdev_prop_arraylen = {
1044     .name = "uint32",
1045     .get = get_uint32,
1046     .set = set_prop_arraylen,
1047     .set_default_value = set_default_value_uint,
1048 };
1049 
1050 /* --- public helpers --- */
1051 
1052 static Property *qdev_prop_walk(Property *props, const char *name)
1053 {
1054     if (!props) {
1055         return NULL;
1056     }
1057     while (props->name) {
1058         if (strcmp(props->name, name) == 0) {
1059             return props;
1060         }
1061         props++;
1062     }
1063     return NULL;
1064 }
1065 
1066 static Property *qdev_prop_find(DeviceState *dev, const char *name)
1067 {
1068     ObjectClass *class;
1069     Property *prop;
1070 
1071     /* device properties */
1072     class = object_get_class(OBJECT(dev));
1073     do {
1074         prop = qdev_prop_walk(DEVICE_CLASS(class)->props, name);
1075         if (prop) {
1076             return prop;
1077         }
1078         class = object_class_get_parent(class);
1079     } while (class != object_class_by_name(TYPE_DEVICE));
1080 
1081     return NULL;
1082 }
1083 
1084 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
1085                                     Property *prop, const char *value)
1086 {
1087     switch (ret) {
1088     case -EEXIST:
1089         error_setg(errp, "Property '%s.%s' can't take value '%s', it's in use",
1090                   object_get_typename(OBJECT(dev)), prop->name, value);
1091         break;
1092     default:
1093     case -EINVAL:
1094         error_setg(errp, QERR_PROPERTY_VALUE_BAD,
1095                    object_get_typename(OBJECT(dev)), prop->name, value);
1096         break;
1097     case -ENOENT:
1098         error_setg(errp, "Property '%s.%s' can't find value '%s'",
1099                   object_get_typename(OBJECT(dev)), prop->name, value);
1100         break;
1101     case 0:
1102         break;
1103     }
1104 }
1105 
1106 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
1107 {
1108     object_property_set_bool(OBJECT(dev), value, name, &error_abort);
1109 }
1110 
1111 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
1112 {
1113     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1114 }
1115 
1116 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
1117 {
1118     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1119 }
1120 
1121 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
1122 {
1123     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1124 }
1125 
1126 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
1127 {
1128     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1129 }
1130 
1131 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
1132 {
1133     object_property_set_int(OBJECT(dev), value, name, &error_abort);
1134 }
1135 
1136 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value)
1137 {
1138     object_property_set_str(OBJECT(dev), value, name, &error_abort);
1139 }
1140 
1141 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
1142                            const uint8_t *value)
1143 {
1144     char str[2 * 6 + 5 + 1];
1145     snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
1146              value[0], value[1], value[2], value[3], value[4], value[5]);
1147 
1148     object_property_set_str(OBJECT(dev), str, name, &error_abort);
1149 }
1150 
1151 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
1152 {
1153     Property *prop;
1154 
1155     prop = qdev_prop_find(dev, name);
1156     object_property_set_str(OBJECT(dev),
1157                             qapi_enum_lookup(prop->info->enum_table, value),
1158                             name, &error_abort);
1159 }
1160 
1161 static GPtrArray *global_props(void)
1162 {
1163     static GPtrArray *gp;
1164 
1165     if (!gp) {
1166         gp = g_ptr_array_new();
1167     }
1168 
1169     return gp;
1170 }
1171 
1172 void qdev_prop_register_global(GlobalProperty *prop)
1173 {
1174     g_ptr_array_add(global_props(), prop);
1175 }
1176 
1177 int qdev_prop_check_globals(void)
1178 {
1179     int i, ret = 0;
1180 
1181     for (i = 0; i < global_props()->len; i++) {
1182         GlobalProperty *prop;
1183         ObjectClass *oc;
1184         DeviceClass *dc;
1185 
1186         prop = g_ptr_array_index(global_props(), i);
1187         if (prop->used) {
1188             continue;
1189         }
1190         oc = object_class_by_name(prop->driver);
1191         oc = object_class_dynamic_cast(oc, TYPE_DEVICE);
1192         if (!oc) {
1193             warn_report("global %s.%s has invalid class name",
1194                         prop->driver, prop->property);
1195             ret = 1;
1196             continue;
1197         }
1198         dc = DEVICE_CLASS(oc);
1199         if (!dc->hotpluggable && !prop->used) {
1200             warn_report("global %s.%s=%s not used",
1201                         prop->driver, prop->property, prop->value);
1202             ret = 1;
1203             continue;
1204         }
1205     }
1206     return ret;
1207 }
1208 
1209 void qdev_prop_set_globals(DeviceState *dev)
1210 {
1211     object_apply_global_props(OBJECT(dev), global_props(),
1212                               dev->hotplugged ? NULL : &error_fatal);
1213 }
1214 
1215 /* --- 64bit unsigned int 'size' type --- */
1216 
1217 static void get_size(Object *obj, Visitor *v, const char *name, void *opaque,
1218                      Error **errp)
1219 {
1220     DeviceState *dev = DEVICE(obj);
1221     Property *prop = opaque;
1222     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
1223 
1224     visit_type_size(v, name, ptr, errp);
1225 }
1226 
1227 static void set_size(Object *obj, Visitor *v, const char *name, void *opaque,
1228                      Error **errp)
1229 {
1230     DeviceState *dev = DEVICE(obj);
1231     Property *prop = opaque;
1232     uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
1233 
1234     visit_type_size(v, name, ptr, errp);
1235 }
1236 
1237 const PropertyInfo qdev_prop_size = {
1238     .name  = "size",
1239     .get = get_size,
1240     .set = set_size,
1241     .set_default_value = set_default_value_uint,
1242 };
1243 
1244 /* --- object link property --- */
1245 
1246 static void create_link_property(Object *obj, Property *prop, Error **errp)
1247 {
1248     Object **child = qdev_get_prop_ptr(DEVICE(obj), prop);
1249 
1250     object_property_add_link(obj, prop->name, prop->link_type,
1251                              child,
1252                              qdev_prop_allow_set_link_before_realize,
1253                              OBJ_PROP_LINK_STRONG,
1254                              errp);
1255 }
1256 
1257 const PropertyInfo qdev_prop_link = {
1258     .name = "link",
1259     .create = create_link_property,
1260 };
1261 
1262 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
1263 
1264 const PropertyInfo qdev_prop_off_auto_pcibar = {
1265     .name = "OffAutoPCIBAR",
1266     .description = "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
1267     .enum_table = &OffAutoPCIBAR_lookup,
1268     .get = get_enum,
1269     .set = set_enum,
1270     .set_default_value = set_default_value_enum,
1271 };
1272 
1273 /* --- PCIELinkSpeed 2_5/5/8/16 -- */
1274 
1275 static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
1276                                    void *opaque, Error **errp)
1277 {
1278     DeviceState *dev = DEVICE(obj);
1279     Property *prop = opaque;
1280     PCIExpLinkSpeed *p = qdev_get_prop_ptr(dev, prop);
1281     int speed;
1282 
1283     switch (*p) {
1284     case QEMU_PCI_EXP_LNK_2_5GT:
1285         speed = PCIE_LINK_SPEED_2_5;
1286         break;
1287     case QEMU_PCI_EXP_LNK_5GT:
1288         speed = PCIE_LINK_SPEED_5;
1289         break;
1290     case QEMU_PCI_EXP_LNK_8GT:
1291         speed = PCIE_LINK_SPEED_8;
1292         break;
1293     case QEMU_PCI_EXP_LNK_16GT:
1294         speed = PCIE_LINK_SPEED_16;
1295         break;
1296     default:
1297         /* Unreachable */
1298         abort();
1299     }
1300 
1301     visit_type_enum(v, prop->name, &speed, prop->info->enum_table, errp);
1302 }
1303 
1304 static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
1305                                    void *opaque, Error **errp)
1306 {
1307     DeviceState *dev = DEVICE(obj);
1308     Property *prop = opaque;
1309     PCIExpLinkSpeed *p = qdev_get_prop_ptr(dev, prop);
1310     int speed;
1311     Error *local_err = NULL;
1312 
1313     if (dev->realized) {
1314         qdev_prop_set_after_realize(dev, name, errp);
1315         return;
1316     }
1317 
1318     visit_type_enum(v, prop->name, &speed, prop->info->enum_table, &local_err);
1319     if (local_err) {
1320         error_propagate(errp, local_err);
1321         return;
1322     }
1323 
1324     switch (speed) {
1325     case PCIE_LINK_SPEED_2_5:
1326         *p = QEMU_PCI_EXP_LNK_2_5GT;
1327         break;
1328     case PCIE_LINK_SPEED_5:
1329         *p = QEMU_PCI_EXP_LNK_5GT;
1330         break;
1331     case PCIE_LINK_SPEED_8:
1332         *p = QEMU_PCI_EXP_LNK_8GT;
1333         break;
1334     case PCIE_LINK_SPEED_16:
1335         *p = QEMU_PCI_EXP_LNK_16GT;
1336         break;
1337     default:
1338         /* Unreachable */
1339         abort();
1340     }
1341 }
1342 
1343 const PropertyInfo qdev_prop_pcie_link_speed = {
1344     .name = "PCIELinkSpeed",
1345     .description = "2_5/5/8/16",
1346     .enum_table = &PCIELinkSpeed_lookup,
1347     .get = get_prop_pcielinkspeed,
1348     .set = set_prop_pcielinkspeed,
1349     .set_default_value = set_default_value_enum,
1350 };
1351 
1352 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
1353 
1354 static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1355                                    void *opaque, Error **errp)
1356 {
1357     DeviceState *dev = DEVICE(obj);
1358     Property *prop = opaque;
1359     PCIExpLinkWidth *p = qdev_get_prop_ptr(dev, prop);
1360     int width;
1361 
1362     switch (*p) {
1363     case QEMU_PCI_EXP_LNK_X1:
1364         width = PCIE_LINK_WIDTH_1;
1365         break;
1366     case QEMU_PCI_EXP_LNK_X2:
1367         width = PCIE_LINK_WIDTH_2;
1368         break;
1369     case QEMU_PCI_EXP_LNK_X4:
1370         width = PCIE_LINK_WIDTH_4;
1371         break;
1372     case QEMU_PCI_EXP_LNK_X8:
1373         width = PCIE_LINK_WIDTH_8;
1374         break;
1375     case QEMU_PCI_EXP_LNK_X12:
1376         width = PCIE_LINK_WIDTH_12;
1377         break;
1378     case QEMU_PCI_EXP_LNK_X16:
1379         width = PCIE_LINK_WIDTH_16;
1380         break;
1381     case QEMU_PCI_EXP_LNK_X32:
1382         width = PCIE_LINK_WIDTH_32;
1383         break;
1384     default:
1385         /* Unreachable */
1386         abort();
1387     }
1388 
1389     visit_type_enum(v, prop->name, &width, prop->info->enum_table, errp);
1390 }
1391 
1392 static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1393                                    void *opaque, Error **errp)
1394 {
1395     DeviceState *dev = DEVICE(obj);
1396     Property *prop = opaque;
1397     PCIExpLinkWidth *p = qdev_get_prop_ptr(dev, prop);
1398     int width;
1399     Error *local_err = NULL;
1400 
1401     if (dev->realized) {
1402         qdev_prop_set_after_realize(dev, name, errp);
1403         return;
1404     }
1405 
1406     visit_type_enum(v, prop->name, &width, prop->info->enum_table, &local_err);
1407     if (local_err) {
1408         error_propagate(errp, local_err);
1409         return;
1410     }
1411 
1412     switch (width) {
1413     case PCIE_LINK_WIDTH_1:
1414         *p = QEMU_PCI_EXP_LNK_X1;
1415         break;
1416     case PCIE_LINK_WIDTH_2:
1417         *p = QEMU_PCI_EXP_LNK_X2;
1418         break;
1419     case PCIE_LINK_WIDTH_4:
1420         *p = QEMU_PCI_EXP_LNK_X4;
1421         break;
1422     case PCIE_LINK_WIDTH_8:
1423         *p = QEMU_PCI_EXP_LNK_X8;
1424         break;
1425     case PCIE_LINK_WIDTH_12:
1426         *p = QEMU_PCI_EXP_LNK_X12;
1427         break;
1428     case PCIE_LINK_WIDTH_16:
1429         *p = QEMU_PCI_EXP_LNK_X16;
1430         break;
1431     case PCIE_LINK_WIDTH_32:
1432         *p = QEMU_PCI_EXP_LNK_X32;
1433         break;
1434     default:
1435         /* Unreachable */
1436         abort();
1437     }
1438 }
1439 
1440 const PropertyInfo qdev_prop_pcie_link_width = {
1441     .name = "PCIELinkWidth",
1442     .description = "1/2/4/8/12/16/32",
1443     .enum_table = &PCIELinkWidth_lookup,
1444     .get = get_prop_pcielinkwidth,
1445     .set = set_prop_pcielinkwidth,
1446     .set_default_value = set_default_value_enum,
1447 };
1448