Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / ppc / spapr_drc.c
1 /*
2  * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
3  *
4  * Copyright IBM Corp. 2014
5  *
6  * Authors:
7  *  Michael Roth      <mdroth@linux.vnet.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12
13 #include "hw/ppc/spapr_drc.h"
14 #include "qom/object.h"
15 #include "hw/qdev.h"
16 #include "qapi/visitor.h"
17 #include "qemu/error-report.h"
18
19 /* #define DEBUG_SPAPR_DRC */
20
21 #ifdef DEBUG_SPAPR_DRC
22 #define DPRINTF(fmt, ...) \
23     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
24 #define DPRINTFN(fmt, ...) \
25     do { DPRINTF(fmt, ## __VA_ARGS__); fprintf(stderr, "\n"); } while (0)
26 #else
27 #define DPRINTF(fmt, ...) \
28     do { } while (0)
29 #define DPRINTFN(fmt, ...) \
30     do { } while (0)
31 #endif
32
33 #define DRC_CONTAINER_PATH "/dr-connector"
34 #define DRC_INDEX_TYPE_SHIFT 28
35 #define DRC_INDEX_ID_MASK (~(~0 << DRC_INDEX_TYPE_SHIFT))
36
37 static sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type)
38 {
39     uint32_t shift = 0;
40
41     /* make sure this isn't SPAPR_DR_CONNECTOR_TYPE_ANY, or some
42      * other wonky value.
43      */
44     g_assert(is_power_of_2(type));
45
46     while (type != (1 << shift)) {
47         shift++;
48     }
49     return shift;
50 }
51
52 static uint32_t get_index(sPAPRDRConnector *drc)
53 {
54     /* no set format for a drc index: it only needs to be globally
55      * unique. this is how we encode the DRC type on bare-metal
56      * however, so might as well do that here
57      */
58     return (get_type_shift(drc->type) << DRC_INDEX_TYPE_SHIFT) |
59             (drc->id & DRC_INDEX_ID_MASK);
60 }
61
62 static int set_isolation_state(sPAPRDRConnector *drc,
63                                sPAPRDRIsolationState state)
64 {
65     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
66
67     DPRINTFN("drc: %x, set_isolation_state: %x", get_index(drc), state);
68
69     drc->isolation_state = state;
70
71     if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
72         /* if we're awaiting release, but still in an unconfigured state,
73          * it's likely the guest is still in the process of configuring
74          * the device and is transitioning the devices to an ISOLATED
75          * state as a part of that process. so we only complete the
76          * removal when this transition happens for a device in a
77          * configured state, as suggested by the state diagram from
78          * PAPR+ 2.7, 13.4
79          */
80         if (drc->awaiting_release) {
81             if (drc->configured) {
82                 DPRINTFN("finalizing device removal");
83                 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
84                              drc->detach_cb_opaque, NULL);
85             } else {
86                 DPRINTFN("deferring device removal on unconfigured device\n");
87             }
88         }
89         drc->configured = false;
90     }
91
92     return 0;
93 }
94
95 static int set_indicator_state(sPAPRDRConnector *drc,
96                                sPAPRDRIndicatorState state)
97 {
98     DPRINTFN("drc: %x, set_indicator_state: %x", get_index(drc), state);
99     drc->indicator_state = state;
100     return 0;
101 }
102
103 static int set_allocation_state(sPAPRDRConnector *drc,
104                                 sPAPRDRAllocationState state)
105 {
106     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
107
108     DPRINTFN("drc: %x, set_allocation_state: %x", get_index(drc), state);
109
110     if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
111         drc->allocation_state = state;
112         if (drc->awaiting_release &&
113             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
114             DPRINTFN("finalizing device removal");
115             drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
116                          drc->detach_cb_opaque, NULL);
117         }
118     }
119     return 0;
120 }
121
122 static uint32_t get_type(sPAPRDRConnector *drc)
123 {
124     return drc->type;
125 }
126
127 static const char *get_name(sPAPRDRConnector *drc)
128 {
129     return drc->name;
130 }
131
132 static const void *get_fdt(sPAPRDRConnector *drc, int *fdt_start_offset)
133 {
134     if (fdt_start_offset) {
135         *fdt_start_offset = drc->fdt_start_offset;
136     }
137     return drc->fdt;
138 }
139
140 static void set_configured(sPAPRDRConnector *drc)
141 {
142     DPRINTFN("drc: %x, set_configured", get_index(drc));
143
144     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
145         /* guest should be not configuring an isolated device */
146         DPRINTFN("drc: %x, set_configured: skipping isolated device",
147                  get_index(drc));
148         return;
149     }
150     drc->configured = true;
151 }
152
153 /*
154  * dr-entity-sense sensor value
155  * returned via get-sensor-state RTAS calls
156  * as expected by state diagram in PAPR+ 2.7, 13.4
157  * based on the current allocation/indicator/power states
158  * for the DR connector.
159  */
160 static sPAPRDREntitySense entity_sense(sPAPRDRConnector *drc)
161 {
162     sPAPRDREntitySense state;
163
164     if (drc->dev) {
165         if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
166             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
167             /* for logical DR, we return a state of UNUSABLE
168              * iff the allocation state UNUSABLE.
169              * Otherwise, report the state as USABLE/PRESENT,
170              * as we would for PCI.
171              */
172             state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
173         } else {
174             /* this assumes all PCI devices are assigned to
175              * a 'live insertion' power domain, where QEMU
176              * manages power state automatically as opposed
177              * to the guest. present, non-PCI resources are
178              * unaffected by power state.
179              */
180             state = SPAPR_DR_ENTITY_SENSE_PRESENT;
181         }
182     } else {
183         if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
184             /* PCI devices, and only PCI devices, use EMPTY
185              * in cases where we'd otherwise use UNUSABLE
186              */
187             state = SPAPR_DR_ENTITY_SENSE_EMPTY;
188         } else {
189             state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
190         }
191     }
192
193     DPRINTFN("drc: %x, entity_sense: %x", get_index(drc), state);
194     return state;
195 }
196
197 static void prop_get_index(Object *obj, Visitor *v, void *opaque,
198                                   const char *name, Error **errp)
199 {
200     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
201     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
202     uint32_t value = (uint32_t)drck->get_index(drc);
203     visit_type_uint32(v, &value, name, errp);
204 }
205
206 static void prop_get_type(Object *obj, Visitor *v, void *opaque,
207                           const char *name, Error **errp)
208 {
209     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
210     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
211     uint32_t value = (uint32_t)drck->get_type(drc);
212     visit_type_uint32(v, &value, name, errp);
213 }
214
215 static char *prop_get_name(Object *obj, Error **errp)
216 {
217     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
218     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
219     return g_strdup(drck->get_name(drc));
220 }
221
222 static void prop_get_entity_sense(Object *obj, Visitor *v, void *opaque,
223                                   const char *name, Error **errp)
224 {
225     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
226     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
227     uint32_t value = (uint32_t)drck->entity_sense(drc);
228     visit_type_uint32(v, &value, name, errp);
229 }
230
231 static void prop_get_fdt(Object *obj, Visitor *v, void *opaque,
232                         const char *name, Error **errp)
233 {
234     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
235     int fdt_offset_next, fdt_offset, fdt_depth;
236     void *fdt;
237
238     if (!drc->fdt) {
239         return;
240     }
241
242     fdt = drc->fdt;
243     fdt_offset = drc->fdt_start_offset;
244     fdt_depth = 0;
245
246     do {
247         const char *name = NULL;
248         const struct fdt_property *prop = NULL;
249         int prop_len = 0, name_len = 0;
250         uint32_t tag;
251
252         tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
253         switch (tag) {
254         case FDT_BEGIN_NODE:
255             fdt_depth++;
256             name = fdt_get_name(fdt, fdt_offset, &name_len);
257             visit_start_struct(v, NULL, NULL, name, 0, NULL);
258             break;
259         case FDT_END_NODE:
260             /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
261             g_assert(fdt_depth > 0);
262             visit_end_struct(v, NULL);
263             fdt_depth--;
264             break;
265         case FDT_PROP: {
266             int i;
267             prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
268             name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
269             visit_start_list(v, name, NULL);
270             for (i = 0; i < prop_len; i++) {
271                 visit_type_uint8(v, (uint8_t *)&prop->data[i], NULL, NULL);
272
273             }
274             visit_end_list(v, NULL);
275             break;
276         }
277         default:
278             error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
279         }
280         fdt_offset = fdt_offset_next;
281     } while (fdt_depth != 0);
282 }
283
284 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
285                    int fdt_start_offset, bool coldplug, Error **errp)
286 {
287     DPRINTFN("drc: %x, attach", get_index(drc));
288
289     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
290         error_setg(errp, "an attached device is still awaiting release");
291         return;
292     }
293     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
294         g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
295     }
296     g_assert(fdt || coldplug);
297
298     /* NOTE: setting initial isolation state to UNISOLATED means we can't
299      * detach unless guest has a userspace/kernel that moves this state
300      * back to ISOLATED in response to an unplug event, or this is done
301      * manually by the admin prior. if we force things while the guest
302      * may be accessing the device, we can easily crash the guest, so we
303      * we defer completion of removal in such cases to the reset() hook.
304      */
305     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
306         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
307     }
308     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
309
310     drc->dev = d;
311     drc->fdt = fdt;
312     drc->fdt_start_offset = fdt_start_offset;
313     drc->configured = false;
314
315     object_property_add_link(OBJECT(drc), "device",
316                              object_get_typename(OBJECT(drc->dev)),
317                              (Object **)(&drc->dev),
318                              NULL, 0, NULL);
319 }
320
321 static void detach(sPAPRDRConnector *drc, DeviceState *d,
322                    spapr_drc_detach_cb *detach_cb,
323                    void *detach_cb_opaque, Error **errp)
324 {
325     DPRINTFN("drc: %x, detach", get_index(drc));
326
327     drc->detach_cb = detach_cb;
328     drc->detach_cb_opaque = detach_cb_opaque;
329
330     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
331         DPRINTFN("awaiting transition to isolated state before removal");
332         drc->awaiting_release = true;
333         return;
334     }
335
336     if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
337         drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
338         DPRINTFN("awaiting transition to unusable state before removal");
339         drc->awaiting_release = true;
340         return;
341     }
342
343     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
344
345     if (drc->detach_cb) {
346         drc->detach_cb(drc->dev, drc->detach_cb_opaque);
347     }
348
349     drc->awaiting_release = false;
350     g_free(drc->fdt);
351     drc->fdt = NULL;
352     drc->fdt_start_offset = 0;
353     object_property_del(OBJECT(drc), "device", NULL);
354     drc->dev = NULL;
355     drc->detach_cb = NULL;
356     drc->detach_cb_opaque = NULL;
357 }
358
359 static bool release_pending(sPAPRDRConnector *drc)
360 {
361     return drc->awaiting_release;
362 }
363
364 static void reset(DeviceState *d)
365 {
366     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
367     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
368
369     DPRINTFN("drc reset: %x", drck->get_index(drc));
370     /* immediately upon reset we can safely assume DRCs whose devices
371      * are pending removal can be safely removed, and that they will
372      * subsequently be left in an ISOLATED state. move the DRC to this
373      * state in these cases (which will in turn complete any pending
374      * device removals)
375      */
376     if (drc->awaiting_release) {
377         drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
378         /* generally this should also finalize the removal, but if the device
379          * hasn't yet been configured we normally defer removal under the
380          * assumption that this transition is taking place as part of device
381          * configuration. so check if we're still waiting after this, and
382          * force removal if we are
383          */
384         if (drc->awaiting_release) {
385             drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
386                          drc->detach_cb_opaque, NULL);
387         }
388
389         /* non-PCI devices may be awaiting a transition to UNUSABLE */
390         if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
391             drc->awaiting_release) {
392             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
393         }
394     }
395 }
396
397 static void realize(DeviceState *d, Error **errp)
398 {
399     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
400     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
401     Object *root_container;
402     char link_name[256];
403     gchar *child_name;
404     Error *err = NULL;
405
406     DPRINTFN("drc realize: %x", drck->get_index(drc));
407     /* NOTE: we do this as part of realize/unrealize due to the fact
408      * that the guest will communicate with the DRC via RTAS calls
409      * referencing the global DRC index. By unlinking the DRC
410      * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
411      * inaccessible by the guest, since lookups rely on this path
412      * existing in the composition tree
413      */
414     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
415     snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
416     child_name = object_get_canonical_path_component(OBJECT(drc));
417     DPRINTFN("drc child name: %s", child_name);
418     object_property_add_alias(root_container, link_name,
419                               drc->owner, child_name, &err);
420     if (err) {
421         error_report("%s", error_get_pretty(err));
422         error_free(err);
423         object_unref(OBJECT(drc));
424     }
425     g_free(child_name);
426     DPRINTFN("drc realize complete");
427 }
428
429 static void unrealize(DeviceState *d, Error **errp)
430 {
431     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
432     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
433     Object *root_container;
434     char name[256];
435     Error *err = NULL;
436
437     DPRINTFN("drc unrealize: %x", drck->get_index(drc));
438     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
439     snprintf(name, sizeof(name), "%x", drck->get_index(drc));
440     object_property_del(root_container, name, &err);
441     if (err) {
442         error_report("%s", error_get_pretty(err));
443         error_free(err);
444         object_unref(OBJECT(drc));
445     }
446 }
447
448 sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
449                                          sPAPRDRConnectorType type,
450                                          uint32_t id)
451 {
452     sPAPRDRConnector *drc =
453         SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
454
455     g_assert(type);
456
457     drc->type = type;
458     drc->id = id;
459     drc->owner = owner;
460     object_property_add_child(owner, "dr-connector[*]", OBJECT(drc), NULL);
461     object_property_set_bool(OBJECT(drc), true, "realized", NULL);
462
463     /* human-readable name for a DRC to encode into the DT
464      * description. this is mainly only used within a guest in place
465      * of the unique DRC index.
466      *
467      * in the case of VIO/PCI devices, it corresponds to a
468      * "location code" that maps a logical device/function (DRC index)
469      * to a physical (or virtual in the case of VIO) location in the
470      * system by chaining together the "location label" for each
471      * encapsulating component.
472      *
473      * since this is more to do with diagnosing physical hardware
474      * issues than guest compatibility, we choose location codes/DRC
475      * names that adhere to the documented format, but avoid encoding
476      * the entire topology information into the label/code, instead
477      * just using the location codes based on the labels for the
478      * endpoints (VIO/PCI adaptor connectors), which is basically
479      * just "C" followed by an integer ID.
480      *
481      * DRC names as documented by PAPR+ v2.7, 13.5.2.4
482      * location codes as documented by PAPR+ v2.7, 12.3.1.5
483      */
484     switch (drc->type) {
485     case SPAPR_DR_CONNECTOR_TYPE_CPU:
486         drc->name = g_strdup_printf("CPU %d", id);
487         break;
488     case SPAPR_DR_CONNECTOR_TYPE_PHB:
489         drc->name = g_strdup_printf("PHB %d", id);
490         break;
491     case SPAPR_DR_CONNECTOR_TYPE_VIO:
492     case SPAPR_DR_CONNECTOR_TYPE_PCI:
493         drc->name = g_strdup_printf("C%d", id);
494         break;
495     case SPAPR_DR_CONNECTOR_TYPE_LMB:
496         drc->name = g_strdup_printf("LMB %d", id);
497         break;
498     default:
499         g_assert(false);
500     }
501
502     /* PCI slot always start in a USABLE state, and stay there */
503     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
504         drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
505     }
506
507     return drc;
508 }
509
510 static void spapr_dr_connector_instance_init(Object *obj)
511 {
512     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
513
514     object_property_add_uint32_ptr(obj, "isolation-state",
515                                    &drc->isolation_state, NULL);
516     object_property_add_uint32_ptr(obj, "indicator-state",
517                                    &drc->indicator_state, NULL);
518     object_property_add_uint32_ptr(obj, "allocation-state",
519                                    &drc->allocation_state, NULL);
520     object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
521     object_property_add(obj, "index", "uint32", prop_get_index,
522                         NULL, NULL, NULL, NULL);
523     object_property_add(obj, "connector_type", "uint32", prop_get_type,
524                         NULL, NULL, NULL, NULL);
525     object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
526     object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
527                         NULL, NULL, NULL, NULL);
528     object_property_add(obj, "fdt", "struct", prop_get_fdt,
529                         NULL, NULL, NULL, NULL);
530 }
531
532 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
533 {
534     DeviceClass *dk = DEVICE_CLASS(k);
535     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
536
537     dk->reset = reset;
538     dk->realize = realize;
539     dk->unrealize = unrealize;
540     drck->set_isolation_state = set_isolation_state;
541     drck->set_indicator_state = set_indicator_state;
542     drck->set_allocation_state = set_allocation_state;
543     drck->get_index = get_index;
544     drck->get_type = get_type;
545     drck->get_name = get_name;
546     drck->get_fdt = get_fdt;
547     drck->set_configured = set_configured;
548     drck->entity_sense = entity_sense;
549     drck->attach = attach;
550     drck->detach = detach;
551     drck->release_pending = release_pending;
552 }
553
554 static const TypeInfo spapr_dr_connector_info = {
555     .name          = TYPE_SPAPR_DR_CONNECTOR,
556     .parent        = TYPE_DEVICE,
557     .instance_size = sizeof(sPAPRDRConnector),
558     .instance_init = spapr_dr_connector_instance_init,
559     .class_size    = sizeof(sPAPRDRConnectorClass),
560     .class_init    = spapr_dr_connector_class_init,
561 };
562
563 static void spapr_drc_register_types(void)
564 {
565     type_register_static(&spapr_dr_connector_info);
566 }
567
568 type_init(spapr_drc_register_types)
569
570 /* helper functions for external users */
571
572 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
573 {
574     Object *obj;
575     char name[256];
576
577     snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
578     obj = object_resolve_path(name, NULL);
579
580     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
581 }
582
583 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
584                                            uint32_t id)
585 {
586     return spapr_dr_connector_by_index(
587             (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
588             (id & DRC_INDEX_ID_MASK));
589 }
590
591 /* generate a string the describes the DRC to encode into the
592  * device tree.
593  *
594  * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
595  */
596 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
597 {
598     switch (type) {
599     case SPAPR_DR_CONNECTOR_TYPE_CPU:
600         return "CPU";
601     case SPAPR_DR_CONNECTOR_TYPE_PHB:
602         return "PHB";
603     case SPAPR_DR_CONNECTOR_TYPE_VIO:
604         return "SLOT";
605     case SPAPR_DR_CONNECTOR_TYPE_PCI:
606         return "28";
607     case SPAPR_DR_CONNECTOR_TYPE_LMB:
608         return "MEM";
609     default:
610         g_assert(false);
611     }
612
613     return NULL;
614 }
615
616 /**
617  * spapr_drc_populate_dt
618  *
619  * @fdt: libfdt device tree
620  * @path: path in the DT to generate properties
621  * @owner: parent Object/DeviceState for which to generate DRC
622  *         descriptions for
623  * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
624  *   to the types of DRCs to generate entries for
625  *
626  * generate OF properties to describe DRC topology/indices to guests
627  *
628  * as documented in PAPR+ v2.1, 13.5.2
629  */
630 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
631                           uint32_t drc_type_mask)
632 {
633     Object *root_container;
634     ObjectProperty *prop;
635     uint32_t drc_count = 0;
636     GArray *drc_indexes, *drc_power_domains;
637     GString *drc_names, *drc_types;
638     int ret;
639
640     /* the first entry of each properties is a 32-bit integer encoding
641      * the number of elements in the array. we won't know this until
642      * we complete the iteration through all the matching DRCs, but
643      * reserve the space now and set the offsets accordingly so we
644      * can fill them in later.
645      */
646     drc_indexes = g_array_new(false, true, sizeof(uint32_t));
647     drc_indexes = g_array_set_size(drc_indexes, 1);
648     drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
649     drc_power_domains = g_array_set_size(drc_power_domains, 1);
650     drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
651     drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
652
653     /* aliases for all DRConnector objects will be rooted in QOM
654      * composition tree at DRC_CONTAINER_PATH
655      */
656     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
657
658     QTAILQ_FOREACH(prop, &root_container->properties, node) {
659         Object *obj;
660         sPAPRDRConnector *drc;
661         sPAPRDRConnectorClass *drck;
662         uint32_t drc_index, drc_power_domain;
663
664         if (!strstart(prop->type, "link<", NULL)) {
665             continue;
666         }
667
668         obj = object_property_get_link(root_container, prop->name, NULL);
669         drc = SPAPR_DR_CONNECTOR(obj);
670         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
671
672         if (owner && (drc->owner != owner)) {
673             continue;
674         }
675
676         if ((drc->type & drc_type_mask) == 0) {
677             continue;
678         }
679
680         drc_count++;
681
682         /* ibm,drc-indexes */
683         drc_index = cpu_to_be32(drck->get_index(drc));
684         g_array_append_val(drc_indexes, drc_index);
685
686         /* ibm,drc-power-domains */
687         drc_power_domain = cpu_to_be32(-1);
688         g_array_append_val(drc_power_domains, drc_power_domain);
689
690         /* ibm,drc-names */
691         drc_names = g_string_append(drc_names, drck->get_name(drc));
692         drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
693
694         /* ibm,drc-types */
695         drc_types = g_string_append(drc_types,
696                                     spapr_drc_get_type_str(drc->type));
697         drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
698     }
699
700     /* now write the drc count into the space we reserved at the
701      * beginning of the arrays previously
702      */
703     *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
704     *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
705     *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
706     *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
707
708     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
709                       drc_indexes->data,
710                       drc_indexes->len * sizeof(uint32_t));
711     if (ret) {
712         fprintf(stderr, "Couldn't create ibm,drc-indexes property\n");
713         goto out;
714     }
715
716     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
717                       drc_power_domains->data,
718                       drc_power_domains->len * sizeof(uint32_t));
719     if (ret) {
720         fprintf(stderr, "Couldn't finalize ibm,drc-power-domains property\n");
721         goto out;
722     }
723
724     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
725                       drc_names->str, drc_names->len);
726     if (ret) {
727         fprintf(stderr, "Couldn't finalize ibm,drc-names property\n");
728         goto out;
729     }
730
731     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
732                       drc_types->str, drc_types->len);
733     if (ret) {
734         fprintf(stderr, "Couldn't finalize ibm,drc-types property\n");
735         goto out;
736     }
737
738 out:
739     g_array_free(drc_indexes, true);
740     g_array_free(drc_power_domains, true);
741     g_string_free(drc_names, true);
742     g_string_free(drc_types, true);
743
744     return ret;
745 }