These changes are the raw update to qemu-2.6.
[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 "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 /* has the guest been notified of device attachment? */
180 static void set_signalled(sPAPRDRConnector *drc)
181 {
182     drc->signalled = true;
183 }
184
185 /*
186  * dr-entity-sense sensor value
187  * returned via get-sensor-state RTAS calls
188  * as expected by state diagram in PAPR+ 2.7, 13.4
189  * based on the current allocation/indicator/power states
190  * for the DR connector.
191  */
192 static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
193 {
194     if (drc->dev) {
195         if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
196             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
197             /* for logical DR, we return a state of UNUSABLE
198              * iff the allocation state UNUSABLE.
199              * Otherwise, report the state as USABLE/PRESENT,
200              * as we would for PCI.
201              */
202             *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
203         } else {
204             /* this assumes all PCI devices are assigned to
205              * a 'live insertion' power domain, where QEMU
206              * manages power state automatically as opposed
207              * to the guest. present, non-PCI resources are
208              * unaffected by power state.
209              */
210             *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
211         }
212     } else {
213         if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
214             /* PCI devices, and only PCI devices, use EMPTY
215              * in cases where we'd otherwise use UNUSABLE
216              */
217             *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
218         } else {
219             *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
220         }
221     }
222
223     DPRINTFN("drc: %x, entity_sense: %x", get_index(drc), state);
224     return RTAS_OUT_SUCCESS;
225 }
226
227 static void prop_get_index(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_index(drc);
233     visit_type_uint32(v, name, &value, errp);
234 }
235
236 static void prop_get_type(Object *obj, Visitor *v, const char *name,
237                           void *opaque, Error **errp)
238 {
239     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
240     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
241     uint32_t value = (uint32_t)drck->get_type(drc);
242     visit_type_uint32(v, name, &value, errp);
243 }
244
245 static char *prop_get_name(Object *obj, Error **errp)
246 {
247     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
248     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
249     return g_strdup(drck->get_name(drc));
250 }
251
252 static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
253                                   void *opaque, Error **errp)
254 {
255     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
256     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
257     uint32_t value;
258
259     drck->entity_sense(drc, &value);
260     visit_type_uint32(v, name, &value, errp);
261 }
262
263 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
264                          void *opaque, Error **errp)
265 {
266     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
267     Error *err = NULL;
268     int fdt_offset_next, fdt_offset, fdt_depth;
269     void *fdt;
270
271     if (!drc->fdt) {
272         visit_start_struct(v, name, NULL, 0, &err);
273         if (!err) {
274             visit_end_struct(v, &err);
275         }
276         error_propagate(errp, err);
277         return;
278     }
279
280     fdt = drc->fdt;
281     fdt_offset = drc->fdt_start_offset;
282     fdt_depth = 0;
283
284     do {
285         const char *name = NULL;
286         const struct fdt_property *prop = NULL;
287         int prop_len = 0, name_len = 0;
288         uint32_t tag;
289
290         tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
291         switch (tag) {
292         case FDT_BEGIN_NODE:
293             fdt_depth++;
294             name = fdt_get_name(fdt, fdt_offset, &name_len);
295             visit_start_struct(v, name, NULL, 0, &err);
296             if (err) {
297                 error_propagate(errp, err);
298                 return;
299             }
300             break;
301         case FDT_END_NODE:
302             /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
303             g_assert(fdt_depth > 0);
304             visit_end_struct(v, &err);
305             if (err) {
306                 error_propagate(errp, err);
307                 return;
308             }
309             fdt_depth--;
310             break;
311         case FDT_PROP: {
312             int i;
313             prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
314             name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
315             visit_start_list(v, name, &err);
316             if (err) {
317                 error_propagate(errp, err);
318                 return;
319             }
320             for (i = 0; i < prop_len; i++) {
321                 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
322                 if (err) {
323                     error_propagate(errp, err);
324                     return;
325                 }
326             }
327             visit_end_list(v);
328             break;
329         }
330         default:
331             error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
332         }
333         fdt_offset = fdt_offset_next;
334     } while (fdt_depth != 0);
335 }
336
337 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
338                    int fdt_start_offset, bool coldplug, Error **errp)
339 {
340     DPRINTFN("drc: %x, attach", get_index(drc));
341
342     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
343         error_setg(errp, "an attached device is still awaiting release");
344         return;
345     }
346     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
347         g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
348     }
349     g_assert(fdt || coldplug);
350
351     /* NOTE: setting initial isolation state to UNISOLATED means we can't
352      * detach unless guest has a userspace/kernel that moves this state
353      * back to ISOLATED in response to an unplug event, or this is done
354      * manually by the admin prior. if we force things while the guest
355      * may be accessing the device, we can easily crash the guest, so we
356      * we defer completion of removal in such cases to the reset() hook.
357      */
358     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
359         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
360     }
361     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
362
363     drc->dev = d;
364     drc->fdt = fdt;
365     drc->fdt_start_offset = fdt_start_offset;
366     drc->configured = coldplug;
367     /* 'logical' DR resources such as memory/cpus are in some cases treated
368      * as a pool of resources from which the guest is free to choose from
369      * based on only a count. for resources that can be assigned in this
370      * fashion, we must assume the resource is signalled immediately
371      * since a single hotplug request might make an arbitrary number of
372      * such attached resources available to the guest, as opposed to
373      * 'physical' DR resources such as PCI where each device/resource is
374      * signalled individually.
375      */
376     drc->signalled = (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI)
377                      ? true : coldplug;
378
379     object_property_add_link(OBJECT(drc), "device",
380                              object_get_typename(OBJECT(drc->dev)),
381                              (Object **)(&drc->dev),
382                              NULL, 0, NULL);
383 }
384
385 static void detach(sPAPRDRConnector *drc, DeviceState *d,
386                    spapr_drc_detach_cb *detach_cb,
387                    void *detach_cb_opaque, Error **errp)
388 {
389     DPRINTFN("drc: %x, detach", get_index(drc));
390
391     drc->detach_cb = detach_cb;
392     drc->detach_cb_opaque = detach_cb_opaque;
393
394     /* if we've signalled device presence to the guest, or if the guest
395      * has gone ahead and configured the device (via manually-executed
396      * device add via drmgr in guest, namely), we need to wait
397      * for the guest to quiesce the device before completing detach.
398      * Otherwise, we can assume the guest hasn't seen it and complete the
399      * detach immediately. Note that there is a small race window
400      * just before, or during, configuration, which is this context
401      * refers mainly to fetching the device tree via RTAS.
402      * During this window the device access will be arbitrated by
403      * associated DRC, which will simply fail the RTAS calls as invalid.
404      * This is recoverable within guest and current implementations of
405      * drmgr should be able to cope.
406      */
407     if (!drc->signalled && !drc->configured) {
408         /* if the guest hasn't seen the device we can't rely on it to
409          * set it back to an isolated state via RTAS, so do it here manually
410          */
411         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
412     }
413
414     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
415         DPRINTFN("awaiting transition to isolated state before removal");
416         drc->awaiting_release = true;
417         return;
418     }
419
420     if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
421         drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
422         DPRINTFN("awaiting transition to unusable state before removal");
423         drc->awaiting_release = true;
424         return;
425     }
426
427     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
428
429     if (drc->detach_cb) {
430         drc->detach_cb(drc->dev, drc->detach_cb_opaque);
431     }
432
433     drc->awaiting_release = false;
434     g_free(drc->fdt);
435     drc->fdt = NULL;
436     drc->fdt_start_offset = 0;
437     object_property_del(OBJECT(drc), "device", NULL);
438     drc->dev = NULL;
439     drc->detach_cb = NULL;
440     drc->detach_cb_opaque = NULL;
441 }
442
443 static bool release_pending(sPAPRDRConnector *drc)
444 {
445     return drc->awaiting_release;
446 }
447
448 static void reset(DeviceState *d)
449 {
450     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
451     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
452     sPAPRDREntitySense state;
453
454     DPRINTFN("drc reset: %x", drck->get_index(drc));
455     /* immediately upon reset we can safely assume DRCs whose devices
456      * are pending removal can be safely removed, and that they will
457      * subsequently be left in an ISOLATED state. move the DRC to this
458      * state in these cases (which will in turn complete any pending
459      * device removals)
460      */
461     if (drc->awaiting_release) {
462         drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
463         /* generally this should also finalize the removal, but if the device
464          * hasn't yet been configured we normally defer removal under the
465          * assumption that this transition is taking place as part of device
466          * configuration. so check if we're still waiting after this, and
467          * force removal if we are
468          */
469         if (drc->awaiting_release) {
470             drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
471                          drc->detach_cb_opaque, NULL);
472         }
473
474         /* non-PCI devices may be awaiting a transition to UNUSABLE */
475         if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
476             drc->awaiting_release) {
477             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
478         }
479     }
480
481     drck->entity_sense(drc, &state);
482     if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
483         drck->set_signalled(drc);
484     }
485 }
486
487 static void realize(DeviceState *d, Error **errp)
488 {
489     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
490     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
491     Object *root_container;
492     char link_name[256];
493     gchar *child_name;
494     Error *err = NULL;
495
496     DPRINTFN("drc realize: %x", drck->get_index(drc));
497     /* NOTE: we do this as part of realize/unrealize due to the fact
498      * that the guest will communicate with the DRC via RTAS calls
499      * referencing the global DRC index. By unlinking the DRC
500      * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
501      * inaccessible by the guest, since lookups rely on this path
502      * existing in the composition tree
503      */
504     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
505     snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
506     child_name = object_get_canonical_path_component(OBJECT(drc));
507     DPRINTFN("drc child name: %s", child_name);
508     object_property_add_alias(root_container, link_name,
509                               drc->owner, child_name, &err);
510     if (err) {
511         error_report_err(err);
512         object_unref(OBJECT(drc));
513     }
514     g_free(child_name);
515     DPRINTFN("drc realize complete");
516 }
517
518 static void unrealize(DeviceState *d, Error **errp)
519 {
520     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
521     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
522     Object *root_container;
523     char name[256];
524     Error *err = NULL;
525
526     DPRINTFN("drc unrealize: %x", drck->get_index(drc));
527     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
528     snprintf(name, sizeof(name), "%x", drck->get_index(drc));
529     object_property_del(root_container, name, &err);
530     if (err) {
531         error_report_err(err);
532         object_unref(OBJECT(drc));
533     }
534 }
535
536 sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
537                                          sPAPRDRConnectorType type,
538                                          uint32_t id)
539 {
540     sPAPRDRConnector *drc =
541         SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
542     char *prop_name;
543
544     g_assert(type);
545
546     drc->type = type;
547     drc->id = id;
548     drc->owner = owner;
549     prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
550     object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
551     object_property_set_bool(OBJECT(drc), true, "realized", NULL);
552     g_free(prop_name);
553
554     /* human-readable name for a DRC to encode into the DT
555      * description. this is mainly only used within a guest in place
556      * of the unique DRC index.
557      *
558      * in the case of VIO/PCI devices, it corresponds to a
559      * "location code" that maps a logical device/function (DRC index)
560      * to a physical (or virtual in the case of VIO) location in the
561      * system by chaining together the "location label" for each
562      * encapsulating component.
563      *
564      * since this is more to do with diagnosing physical hardware
565      * issues than guest compatibility, we choose location codes/DRC
566      * names that adhere to the documented format, but avoid encoding
567      * the entire topology information into the label/code, instead
568      * just using the location codes based on the labels for the
569      * endpoints (VIO/PCI adaptor connectors), which is basically
570      * just "C" followed by an integer ID.
571      *
572      * DRC names as documented by PAPR+ v2.7, 13.5.2.4
573      * location codes as documented by PAPR+ v2.7, 12.3.1.5
574      */
575     switch (drc->type) {
576     case SPAPR_DR_CONNECTOR_TYPE_CPU:
577         drc->name = g_strdup_printf("CPU %d", id);
578         break;
579     case SPAPR_DR_CONNECTOR_TYPE_PHB:
580         drc->name = g_strdup_printf("PHB %d", id);
581         break;
582     case SPAPR_DR_CONNECTOR_TYPE_VIO:
583     case SPAPR_DR_CONNECTOR_TYPE_PCI:
584         drc->name = g_strdup_printf("C%d", id);
585         break;
586     case SPAPR_DR_CONNECTOR_TYPE_LMB:
587         drc->name = g_strdup_printf("LMB %d", id);
588         break;
589     default:
590         g_assert(false);
591     }
592
593     /* PCI slot always start in a USABLE state, and stay there */
594     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
595         drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
596     }
597
598     return drc;
599 }
600
601 static void spapr_dr_connector_instance_init(Object *obj)
602 {
603     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
604
605     object_property_add_uint32_ptr(obj, "isolation-state",
606                                    &drc->isolation_state, NULL);
607     object_property_add_uint32_ptr(obj, "indicator-state",
608                                    &drc->indicator_state, NULL);
609     object_property_add_uint32_ptr(obj, "allocation-state",
610                                    &drc->allocation_state, NULL);
611     object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
612     object_property_add(obj, "index", "uint32", prop_get_index,
613                         NULL, NULL, NULL, NULL);
614     object_property_add(obj, "connector_type", "uint32", prop_get_type,
615                         NULL, NULL, NULL, NULL);
616     object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
617     object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
618                         NULL, NULL, NULL, NULL);
619     object_property_add(obj, "fdt", "struct", prop_get_fdt,
620                         NULL, NULL, NULL, NULL);
621 }
622
623 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
624 {
625     DeviceClass *dk = DEVICE_CLASS(k);
626     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
627
628     dk->reset = reset;
629     dk->realize = realize;
630     dk->unrealize = unrealize;
631     drck->set_isolation_state = set_isolation_state;
632     drck->set_indicator_state = set_indicator_state;
633     drck->set_allocation_state = set_allocation_state;
634     drck->get_index = get_index;
635     drck->get_type = get_type;
636     drck->get_name = get_name;
637     drck->get_fdt = get_fdt;
638     drck->set_configured = set_configured;
639     drck->entity_sense = entity_sense;
640     drck->attach = attach;
641     drck->detach = detach;
642     drck->release_pending = release_pending;
643     drck->set_signalled = set_signalled;
644     /*
645      * Reason: it crashes FIXME find and document the real reason
646      */
647     dk->cannot_instantiate_with_device_add_yet = true;
648 }
649
650 static const TypeInfo spapr_dr_connector_info = {
651     .name          = TYPE_SPAPR_DR_CONNECTOR,
652     .parent        = TYPE_DEVICE,
653     .instance_size = sizeof(sPAPRDRConnector),
654     .instance_init = spapr_dr_connector_instance_init,
655     .class_size    = sizeof(sPAPRDRConnectorClass),
656     .class_init    = spapr_dr_connector_class_init,
657 };
658
659 static void spapr_drc_register_types(void)
660 {
661     type_register_static(&spapr_dr_connector_info);
662 }
663
664 type_init(spapr_drc_register_types)
665
666 /* helper functions for external users */
667
668 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
669 {
670     Object *obj;
671     char name[256];
672
673     snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
674     obj = object_resolve_path(name, NULL);
675
676     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
677 }
678
679 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
680                                            uint32_t id)
681 {
682     return spapr_dr_connector_by_index(
683             (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
684             (id & DRC_INDEX_ID_MASK));
685 }
686
687 /* generate a string the describes the DRC to encode into the
688  * device tree.
689  *
690  * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
691  */
692 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
693 {
694     switch (type) {
695     case SPAPR_DR_CONNECTOR_TYPE_CPU:
696         return "CPU";
697     case SPAPR_DR_CONNECTOR_TYPE_PHB:
698         return "PHB";
699     case SPAPR_DR_CONNECTOR_TYPE_VIO:
700         return "SLOT";
701     case SPAPR_DR_CONNECTOR_TYPE_PCI:
702         return "28";
703     case SPAPR_DR_CONNECTOR_TYPE_LMB:
704         return "MEM";
705     default:
706         g_assert(false);
707     }
708
709     return NULL;
710 }
711
712 /**
713  * spapr_drc_populate_dt
714  *
715  * @fdt: libfdt device tree
716  * @path: path in the DT to generate properties
717  * @owner: parent Object/DeviceState for which to generate DRC
718  *         descriptions for
719  * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
720  *   to the types of DRCs to generate entries for
721  *
722  * generate OF properties to describe DRC topology/indices to guests
723  *
724  * as documented in PAPR+ v2.1, 13.5.2
725  */
726 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
727                           uint32_t drc_type_mask)
728 {
729     Object *root_container;
730     ObjectProperty *prop;
731     ObjectPropertyIterator iter;
732     uint32_t drc_count = 0;
733     GArray *drc_indexes, *drc_power_domains;
734     GString *drc_names, *drc_types;
735     int ret;
736
737     /* the first entry of each properties is a 32-bit integer encoding
738      * the number of elements in the array. we won't know this until
739      * we complete the iteration through all the matching DRCs, but
740      * reserve the space now and set the offsets accordingly so we
741      * can fill them in later.
742      */
743     drc_indexes = g_array_new(false, true, sizeof(uint32_t));
744     drc_indexes = g_array_set_size(drc_indexes, 1);
745     drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
746     drc_power_domains = g_array_set_size(drc_power_domains, 1);
747     drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
748     drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
749
750     /* aliases for all DRConnector objects will be rooted in QOM
751      * composition tree at DRC_CONTAINER_PATH
752      */
753     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
754
755     object_property_iter_init(&iter, root_container);
756     while ((prop = object_property_iter_next(&iter))) {
757         Object *obj;
758         sPAPRDRConnector *drc;
759         sPAPRDRConnectorClass *drck;
760         uint32_t drc_index, drc_power_domain;
761
762         if (!strstart(prop->type, "link<", NULL)) {
763             continue;
764         }
765
766         obj = object_property_get_link(root_container, prop->name, NULL);
767         drc = SPAPR_DR_CONNECTOR(obj);
768         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
769
770         if (owner && (drc->owner != owner)) {
771             continue;
772         }
773
774         if ((drc->type & drc_type_mask) == 0) {
775             continue;
776         }
777
778         drc_count++;
779
780         /* ibm,drc-indexes */
781         drc_index = cpu_to_be32(drck->get_index(drc));
782         g_array_append_val(drc_indexes, drc_index);
783
784         /* ibm,drc-power-domains */
785         drc_power_domain = cpu_to_be32(-1);
786         g_array_append_val(drc_power_domains, drc_power_domain);
787
788         /* ibm,drc-names */
789         drc_names = g_string_append(drc_names, drck->get_name(drc));
790         drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
791
792         /* ibm,drc-types */
793         drc_types = g_string_append(drc_types,
794                                     spapr_drc_get_type_str(drc->type));
795         drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
796     }
797
798     /* now write the drc count into the space we reserved at the
799      * beginning of the arrays previously
800      */
801     *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
802     *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
803     *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
804     *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
805
806     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
807                       drc_indexes->data,
808                       drc_indexes->len * sizeof(uint32_t));
809     if (ret) {
810         fprintf(stderr, "Couldn't create ibm,drc-indexes property\n");
811         goto out;
812     }
813
814     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
815                       drc_power_domains->data,
816                       drc_power_domains->len * sizeof(uint32_t));
817     if (ret) {
818         fprintf(stderr, "Couldn't finalize ibm,drc-power-domains property\n");
819         goto out;
820     }
821
822     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
823                       drc_names->str, drc_names->len);
824     if (ret) {
825         fprintf(stderr, "Couldn't finalize ibm,drc-names property\n");
826         goto out;
827     }
828
829     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
830                       drc_types->str, drc_types->len);
831     if (ret) {
832         fprintf(stderr, "Couldn't finalize ibm,drc-types property\n");
833         goto out;
834     }
835
836 out:
837     g_array_free(drc_indexes, true);
838     g_array_free(drc_power_domains, true);
839     g_string_free(drc_names, true);
840     g_string_free(drc_types, true);
841
842     return ret;
843 }