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