xref: /openbmc/qemu/hw/ppc/spapr_drc.c (revision 0430891c)
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, void *opaque,
219                                   const char *name, 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, &value, name, errp);
225 }
226 
227 static void prop_get_type(Object *obj, Visitor *v, void *opaque,
228                           const char *name, 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, &value, name, 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, void *opaque,
244                                   const char *name, 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, &value, name, errp);
252 }
253 
254 static void prop_get_fdt(Object *obj, Visitor *v, void *opaque,
255                         const char *name, 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, NULL, NULL, name, 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, NULL, NULL, name, 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, (uint8_t *)&prop->data[i], NULL, &err);
313                 if (err) {
314                     error_propagate(errp, err);
315                     return;
316                 }
317             }
318             visit_end_list(v, &err);
319             if (err) {
320                 error_propagate(errp, err);
321                 return;
322             }
323             break;
324         }
325         default:
326             error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
327         }
328         fdt_offset = fdt_offset_next;
329     } while (fdt_depth != 0);
330 }
331 
332 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
333                    int fdt_start_offset, bool coldplug, Error **errp)
334 {
335     DPRINTFN("drc: %x, attach", get_index(drc));
336 
337     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
338         error_setg(errp, "an attached device is still awaiting release");
339         return;
340     }
341     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
342         g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
343     }
344     g_assert(fdt || coldplug);
345 
346     /* NOTE: setting initial isolation state to UNISOLATED means we can't
347      * detach unless guest has a userspace/kernel that moves this state
348      * back to ISOLATED in response to an unplug event, or this is done
349      * manually by the admin prior. if we force things while the guest
350      * may be accessing the device, we can easily crash the guest, so we
351      * we defer completion of removal in such cases to the reset() hook.
352      */
353     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
354         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
355     }
356     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
357 
358     drc->dev = d;
359     drc->fdt = fdt;
360     drc->fdt_start_offset = fdt_start_offset;
361     drc->configured = coldplug;
362 
363     object_property_add_link(OBJECT(drc), "device",
364                              object_get_typename(OBJECT(drc->dev)),
365                              (Object **)(&drc->dev),
366                              NULL, 0, NULL);
367 }
368 
369 static void detach(sPAPRDRConnector *drc, DeviceState *d,
370                    spapr_drc_detach_cb *detach_cb,
371                    void *detach_cb_opaque, Error **errp)
372 {
373     DPRINTFN("drc: %x, detach", get_index(drc));
374 
375     drc->detach_cb = detach_cb;
376     drc->detach_cb_opaque = detach_cb_opaque;
377 
378     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
379         DPRINTFN("awaiting transition to isolated state before removal");
380         drc->awaiting_release = true;
381         return;
382     }
383 
384     if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
385         drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
386         DPRINTFN("awaiting transition to unusable state before removal");
387         drc->awaiting_release = true;
388         return;
389     }
390 
391     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
392 
393     if (drc->detach_cb) {
394         drc->detach_cb(drc->dev, drc->detach_cb_opaque);
395     }
396 
397     drc->awaiting_release = false;
398     g_free(drc->fdt);
399     drc->fdt = NULL;
400     drc->fdt_start_offset = 0;
401     object_property_del(OBJECT(drc), "device", NULL);
402     drc->dev = NULL;
403     drc->detach_cb = NULL;
404     drc->detach_cb_opaque = NULL;
405 }
406 
407 static bool release_pending(sPAPRDRConnector *drc)
408 {
409     return drc->awaiting_release;
410 }
411 
412 static void reset(DeviceState *d)
413 {
414     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
415     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
416 
417     DPRINTFN("drc reset: %x", drck->get_index(drc));
418     /* immediately upon reset we can safely assume DRCs whose devices
419      * are pending removal can be safely removed, and that they will
420      * subsequently be left in an ISOLATED state. move the DRC to this
421      * state in these cases (which will in turn complete any pending
422      * device removals)
423      */
424     if (drc->awaiting_release) {
425         drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
426         /* generally this should also finalize the removal, but if the device
427          * hasn't yet been configured we normally defer removal under the
428          * assumption that this transition is taking place as part of device
429          * configuration. so check if we're still waiting after this, and
430          * force removal if we are
431          */
432         if (drc->awaiting_release) {
433             drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
434                          drc->detach_cb_opaque, NULL);
435         }
436 
437         /* non-PCI devices may be awaiting a transition to UNUSABLE */
438         if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
439             drc->awaiting_release) {
440             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
441         }
442     }
443 }
444 
445 static void realize(DeviceState *d, Error **errp)
446 {
447     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
448     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
449     Object *root_container;
450     char link_name[256];
451     gchar *child_name;
452     Error *err = NULL;
453 
454     DPRINTFN("drc realize: %x", drck->get_index(drc));
455     /* NOTE: we do this as part of realize/unrealize due to the fact
456      * that the guest will communicate with the DRC via RTAS calls
457      * referencing the global DRC index. By unlinking the DRC
458      * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
459      * inaccessible by the guest, since lookups rely on this path
460      * existing in the composition tree
461      */
462     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
463     snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
464     child_name = object_get_canonical_path_component(OBJECT(drc));
465     DPRINTFN("drc child name: %s", child_name);
466     object_property_add_alias(root_container, link_name,
467                               drc->owner, child_name, &err);
468     if (err) {
469         error_report_err(err);
470         object_unref(OBJECT(drc));
471     }
472     g_free(child_name);
473     DPRINTFN("drc realize complete");
474 }
475 
476 static void unrealize(DeviceState *d, Error **errp)
477 {
478     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
479     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
480     Object *root_container;
481     char name[256];
482     Error *err = NULL;
483 
484     DPRINTFN("drc unrealize: %x", drck->get_index(drc));
485     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
486     snprintf(name, sizeof(name), "%x", drck->get_index(drc));
487     object_property_del(root_container, name, &err);
488     if (err) {
489         error_report_err(err);
490         object_unref(OBJECT(drc));
491     }
492 }
493 
494 sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
495                                          sPAPRDRConnectorType type,
496                                          uint32_t id)
497 {
498     sPAPRDRConnector *drc =
499         SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
500     char *prop_name;
501 
502     g_assert(type);
503 
504     drc->type = type;
505     drc->id = id;
506     drc->owner = owner;
507     prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
508     object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
509     object_property_set_bool(OBJECT(drc), true, "realized", NULL);
510     g_free(prop_name);
511 
512     /* human-readable name for a DRC to encode into the DT
513      * description. this is mainly only used within a guest in place
514      * of the unique DRC index.
515      *
516      * in the case of VIO/PCI devices, it corresponds to a
517      * "location code" that maps a logical device/function (DRC index)
518      * to a physical (or virtual in the case of VIO) location in the
519      * system by chaining together the "location label" for each
520      * encapsulating component.
521      *
522      * since this is more to do with diagnosing physical hardware
523      * issues than guest compatibility, we choose location codes/DRC
524      * names that adhere to the documented format, but avoid encoding
525      * the entire topology information into the label/code, instead
526      * just using the location codes based on the labels for the
527      * endpoints (VIO/PCI adaptor connectors), which is basically
528      * just "C" followed by an integer ID.
529      *
530      * DRC names as documented by PAPR+ v2.7, 13.5.2.4
531      * location codes as documented by PAPR+ v2.7, 12.3.1.5
532      */
533     switch (drc->type) {
534     case SPAPR_DR_CONNECTOR_TYPE_CPU:
535         drc->name = g_strdup_printf("CPU %d", id);
536         break;
537     case SPAPR_DR_CONNECTOR_TYPE_PHB:
538         drc->name = g_strdup_printf("PHB %d", id);
539         break;
540     case SPAPR_DR_CONNECTOR_TYPE_VIO:
541     case SPAPR_DR_CONNECTOR_TYPE_PCI:
542         drc->name = g_strdup_printf("C%d", id);
543         break;
544     case SPAPR_DR_CONNECTOR_TYPE_LMB:
545         drc->name = g_strdup_printf("LMB %d", id);
546         break;
547     default:
548         g_assert(false);
549     }
550 
551     /* PCI slot always start in a USABLE state, and stay there */
552     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
553         drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
554     }
555 
556     return drc;
557 }
558 
559 static void spapr_dr_connector_instance_init(Object *obj)
560 {
561     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
562 
563     object_property_add_uint32_ptr(obj, "isolation-state",
564                                    &drc->isolation_state, NULL);
565     object_property_add_uint32_ptr(obj, "indicator-state",
566                                    &drc->indicator_state, NULL);
567     object_property_add_uint32_ptr(obj, "allocation-state",
568                                    &drc->allocation_state, NULL);
569     object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
570     object_property_add(obj, "index", "uint32", prop_get_index,
571                         NULL, NULL, NULL, NULL);
572     object_property_add(obj, "connector_type", "uint32", prop_get_type,
573                         NULL, NULL, NULL, NULL);
574     object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
575     object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
576                         NULL, NULL, NULL, NULL);
577     object_property_add(obj, "fdt", "struct", prop_get_fdt,
578                         NULL, NULL, NULL, NULL);
579 }
580 
581 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
582 {
583     DeviceClass *dk = DEVICE_CLASS(k);
584     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
585 
586     dk->reset = reset;
587     dk->realize = realize;
588     dk->unrealize = unrealize;
589     drck->set_isolation_state = set_isolation_state;
590     drck->set_indicator_state = set_indicator_state;
591     drck->set_allocation_state = set_allocation_state;
592     drck->get_index = get_index;
593     drck->get_type = get_type;
594     drck->get_name = get_name;
595     drck->get_fdt = get_fdt;
596     drck->set_configured = set_configured;
597     drck->entity_sense = entity_sense;
598     drck->attach = attach;
599     drck->detach = detach;
600     drck->release_pending = release_pending;
601     /*
602      * Reason: it crashes FIXME find and document the real reason
603      */
604     dk->cannot_instantiate_with_device_add_yet = true;
605 }
606 
607 static const TypeInfo spapr_dr_connector_info = {
608     .name          = TYPE_SPAPR_DR_CONNECTOR,
609     .parent        = TYPE_DEVICE,
610     .instance_size = sizeof(sPAPRDRConnector),
611     .instance_init = spapr_dr_connector_instance_init,
612     .class_size    = sizeof(sPAPRDRConnectorClass),
613     .class_init    = spapr_dr_connector_class_init,
614 };
615 
616 static void spapr_drc_register_types(void)
617 {
618     type_register_static(&spapr_dr_connector_info);
619 }
620 
621 type_init(spapr_drc_register_types)
622 
623 /* helper functions for external users */
624 
625 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
626 {
627     Object *obj;
628     char name[256];
629 
630     snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
631     obj = object_resolve_path(name, NULL);
632 
633     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
634 }
635 
636 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
637                                            uint32_t id)
638 {
639     return spapr_dr_connector_by_index(
640             (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
641             (id & DRC_INDEX_ID_MASK));
642 }
643 
644 /* generate a string the describes the DRC to encode into the
645  * device tree.
646  *
647  * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
648  */
649 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
650 {
651     switch (type) {
652     case SPAPR_DR_CONNECTOR_TYPE_CPU:
653         return "CPU";
654     case SPAPR_DR_CONNECTOR_TYPE_PHB:
655         return "PHB";
656     case SPAPR_DR_CONNECTOR_TYPE_VIO:
657         return "SLOT";
658     case SPAPR_DR_CONNECTOR_TYPE_PCI:
659         return "28";
660     case SPAPR_DR_CONNECTOR_TYPE_LMB:
661         return "MEM";
662     default:
663         g_assert(false);
664     }
665 
666     return NULL;
667 }
668 
669 /**
670  * spapr_drc_populate_dt
671  *
672  * @fdt: libfdt device tree
673  * @path: path in the DT to generate properties
674  * @owner: parent Object/DeviceState for which to generate DRC
675  *         descriptions for
676  * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
677  *   to the types of DRCs to generate entries for
678  *
679  * generate OF properties to describe DRC topology/indices to guests
680  *
681  * as documented in PAPR+ v2.1, 13.5.2
682  */
683 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
684                           uint32_t drc_type_mask)
685 {
686     Object *root_container;
687     ObjectProperty *prop;
688     ObjectPropertyIterator iter;
689     uint32_t drc_count = 0;
690     GArray *drc_indexes, *drc_power_domains;
691     GString *drc_names, *drc_types;
692     int ret;
693 
694     /* the first entry of each properties is a 32-bit integer encoding
695      * the number of elements in the array. we won't know this until
696      * we complete the iteration through all the matching DRCs, but
697      * reserve the space now and set the offsets accordingly so we
698      * can fill them in later.
699      */
700     drc_indexes = g_array_new(false, true, sizeof(uint32_t));
701     drc_indexes = g_array_set_size(drc_indexes, 1);
702     drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
703     drc_power_domains = g_array_set_size(drc_power_domains, 1);
704     drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
705     drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
706 
707     /* aliases for all DRConnector objects will be rooted in QOM
708      * composition tree at DRC_CONTAINER_PATH
709      */
710     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
711 
712     object_property_iter_init(&iter, root_container);
713     while ((prop = object_property_iter_next(&iter))) {
714         Object *obj;
715         sPAPRDRConnector *drc;
716         sPAPRDRConnectorClass *drck;
717         uint32_t drc_index, drc_power_domain;
718 
719         if (!strstart(prop->type, "link<", NULL)) {
720             continue;
721         }
722 
723         obj = object_property_get_link(root_container, prop->name, NULL);
724         drc = SPAPR_DR_CONNECTOR(obj);
725         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
726 
727         if (owner && (drc->owner != owner)) {
728             continue;
729         }
730 
731         if ((drc->type & drc_type_mask) == 0) {
732             continue;
733         }
734 
735         drc_count++;
736 
737         /* ibm,drc-indexes */
738         drc_index = cpu_to_be32(drck->get_index(drc));
739         g_array_append_val(drc_indexes, drc_index);
740 
741         /* ibm,drc-power-domains */
742         drc_power_domain = cpu_to_be32(-1);
743         g_array_append_val(drc_power_domains, drc_power_domain);
744 
745         /* ibm,drc-names */
746         drc_names = g_string_append(drc_names, drck->get_name(drc));
747         drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
748 
749         /* ibm,drc-types */
750         drc_types = g_string_append(drc_types,
751                                     spapr_drc_get_type_str(drc->type));
752         drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
753     }
754 
755     /* now write the drc count into the space we reserved at the
756      * beginning of the arrays previously
757      */
758     *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
759     *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
760     *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
761     *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
762 
763     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
764                       drc_indexes->data,
765                       drc_indexes->len * sizeof(uint32_t));
766     if (ret) {
767         fprintf(stderr, "Couldn't create ibm,drc-indexes property\n");
768         goto out;
769     }
770 
771     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
772                       drc_power_domains->data,
773                       drc_power_domains->len * sizeof(uint32_t));
774     if (ret) {
775         fprintf(stderr, "Couldn't finalize ibm,drc-power-domains property\n");
776         goto out;
777     }
778 
779     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
780                       drc_names->str, drc_names->len);
781     if (ret) {
782         fprintf(stderr, "Couldn't finalize ibm,drc-names property\n");
783         goto out;
784     }
785 
786     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
787                       drc_types->str, drc_types->len);
788     if (ret) {
789         fprintf(stderr, "Couldn't finalize ibm,drc-types property\n");
790         goto out;
791     }
792 
793 out:
794     g_array_free(drc_indexes, true);
795     g_array_free(drc_power_domains, true);
796     g_string_free(drc_names, true);
797     g_string_free(drc_types, true);
798 
799     return ret;
800 }
801