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