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