xref: /openbmc/qemu/hw/ppc/spapr_drc.c (revision 1ff7b531)
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 sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc)
31 {
32     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
33 
34     return 1 << drck->typeshift;
35 }
36 
37 uint32_t spapr_drc_index(sPAPRDRConnector *drc)
38 {
39     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
40 
41     /* no set format for a drc index: it only needs to be globally
42      * unique. this is how we encode the DRC type on bare-metal
43      * however, so might as well do that here
44      */
45     return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
46         | (drc->id & DRC_INDEX_ID_MASK);
47 }
48 
49 static uint32_t drc_isolate_physical(sPAPRDRConnector *drc)
50 {
51     /* if the guest is configuring a device attached to this DRC, we
52      * should reset the configuration state at this point since it may
53      * no longer be reliable (guest released device and needs to start
54      * over, or unplug occurred so the FDT is no longer valid)
55      */
56     g_free(drc->ccs);
57     drc->ccs = NULL;
58 
59     drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
60 
61     /* if we're awaiting release, but still in an unconfigured state,
62      * it's likely the guest is still in the process of configuring
63      * the device and is transitioning the devices to an ISOLATED
64      * state as a part of that process. so we only complete the
65      * removal when this transition happens for a device in a
66      * configured state, as suggested by the state diagram from PAPR+
67      * 2.7, 13.4
68      */
69     if (drc->awaiting_release) {
70         uint32_t drc_index = spapr_drc_index(drc);
71         if (drc->configured) {
72             trace_spapr_drc_set_isolation_state_finalizing(drc_index);
73             spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
74         } else {
75             trace_spapr_drc_set_isolation_state_deferring(drc_index);
76         }
77     }
78     drc->configured = false;
79 
80     return RTAS_OUT_SUCCESS;
81 }
82 
83 static uint32_t drc_unisolate_physical(sPAPRDRConnector *drc)
84 {
85     /* cannot unisolate a non-existent resource, and, or resources
86      * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
87      * 13.5.3.5)
88      */
89     if (!drc->dev) {
90         return RTAS_OUT_NO_SUCH_INDICATOR;
91     }
92 
93     drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
94 
95     return RTAS_OUT_SUCCESS;
96 }
97 
98 static uint32_t drc_isolate_logical(sPAPRDRConnector *drc)
99 {
100     /* if the guest is configuring a device attached to this DRC, we
101      * should reset the configuration state at this point since it may
102      * no longer be reliable (guest released device and needs to start
103      * over, or unplug occurred so the FDT is no longer valid)
104      */
105     g_free(drc->ccs);
106     drc->ccs = NULL;
107 
108     /*
109      * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
110      * belong to a DIMM device that is marked for removal.
111      *
112      * Currently the guest userspace tool drmgr that drives the memory
113      * hotplug/unplug will just try to remove a set of 'removable' LMBs
114      * in response to a hot unplug request that is based on drc-count.
115      * If the LMB being removed doesn't belong to a DIMM device that is
116      * actually being unplugged, fail the isolation request here.
117      */
118     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
119         && !drc->awaiting_release) {
120         return RTAS_OUT_HW_ERROR;
121     }
122 
123     drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
124 
125     /* if we're awaiting release, but still in an unconfigured state,
126      * it's likely the guest is still in the process of configuring
127      * the device and is transitioning the devices to an ISOLATED
128      * state as a part of that process. so we only complete the
129      * removal when this transition happens for a device in a
130      * configured state, as suggested by the state diagram from PAPR+
131      * 2.7, 13.4
132      */
133     if (drc->awaiting_release) {
134         uint32_t drc_index = spapr_drc_index(drc);
135         if (drc->configured) {
136             trace_spapr_drc_set_isolation_state_finalizing(drc_index);
137             spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
138         } else {
139             trace_spapr_drc_set_isolation_state_deferring(drc_index);
140         }
141     }
142     drc->configured = false;
143 
144     return RTAS_OUT_SUCCESS;
145 }
146 
147 static uint32_t drc_unisolate_logical(sPAPRDRConnector *drc)
148 {
149     /* cannot unisolate a non-existent resource, and, or resources
150      * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
151      * 13.5.3.5)
152      */
153     if (!drc->dev ||
154         drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
155         return RTAS_OUT_NO_SUCH_INDICATOR;
156     }
157 
158     drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
159 
160     return RTAS_OUT_SUCCESS;
161 }
162 
163 static uint32_t drc_set_usable(sPAPRDRConnector *drc)
164 {
165     /* if there's no resource/device associated with the DRC, there's
166      * no way for us to put it in an allocation state consistent with
167      * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
168      * result in an RTAS return code of -3 / "no such indicator"
169      */
170     if (!drc->dev) {
171         return RTAS_OUT_NO_SUCH_INDICATOR;
172     }
173     if (drc->awaiting_release && drc->awaiting_allocation) {
174         /* kernel is acknowledging a previous hotplug event
175          * while we are already removing it.
176          * it's safe to ignore awaiting_allocation here since we know the
177          * situation is predicated on the guest either already having done
178          * so (boot-time hotplug), or never being able to acquire in the
179          * first place (hotplug followed by immediate unplug).
180          */
181         return RTAS_OUT_NO_SUCH_INDICATOR;
182     }
183 
184     drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
185     drc->awaiting_allocation = false;
186 
187     return RTAS_OUT_SUCCESS;
188 }
189 
190 static uint32_t drc_set_unusable(sPAPRDRConnector *drc)
191 {
192     drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_UNUSABLE;
193     if (drc->awaiting_release) {
194         uint32_t drc_index = spapr_drc_index(drc);
195         trace_spapr_drc_set_allocation_state_finalizing(drc_index);
196         spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
197     }
198 
199     return RTAS_OUT_SUCCESS;
200 }
201 
202 static const char *spapr_drc_name(sPAPRDRConnector *drc)
203 {
204     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
205 
206     /* human-readable name for a DRC to encode into the DT
207      * description. this is mainly only used within a guest in place
208      * of the unique DRC index.
209      *
210      * in the case of VIO/PCI devices, it corresponds to a "location
211      * code" that maps a logical device/function (DRC index) to a
212      * physical (or virtual in the case of VIO) location in the system
213      * by chaining together the "location label" for each
214      * encapsulating component.
215      *
216      * since this is more to do with diagnosing physical hardware
217      * issues than guest compatibility, we choose location codes/DRC
218      * names that adhere to the documented format, but avoid encoding
219      * the entire topology information into the label/code, instead
220      * just using the location codes based on the labels for the
221      * endpoints (VIO/PCI adaptor connectors), which is basically just
222      * "C" followed by an integer ID.
223      *
224      * DRC names as documented by PAPR+ v2.7, 13.5.2.4
225      * location codes as documented by PAPR+ v2.7, 12.3.1.5
226      */
227     return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
228 }
229 
230 /*
231  * dr-entity-sense sensor value
232  * returned via get-sensor-state RTAS calls
233  * as expected by state diagram in PAPR+ 2.7, 13.4
234  * based on the current allocation/indicator/power states
235  * for the DR connector.
236  */
237 static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc)
238 {
239     /* this assumes all PCI devices are assigned to a 'live insertion'
240      * power domain, where QEMU manages power state automatically as
241      * opposed to the guest. present, non-PCI resources are unaffected
242      * by power state.
243      */
244     return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
245         : SPAPR_DR_ENTITY_SENSE_EMPTY;
246 }
247 
248 static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc)
249 {
250     if (drc->dev
251         && (drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE)) {
252         return SPAPR_DR_ENTITY_SENSE_PRESENT;
253     } else {
254         return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
255     }
256 }
257 
258 static void prop_get_index(Object *obj, Visitor *v, const char *name,
259                            void *opaque, Error **errp)
260 {
261     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
262     uint32_t value = spapr_drc_index(drc);
263     visit_type_uint32(v, name, &value, errp);
264 }
265 
266 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
267                          void *opaque, Error **errp)
268 {
269     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
270     Error *err = NULL;
271     int fdt_offset_next, fdt_offset, fdt_depth;
272     void *fdt;
273 
274     if (!drc->fdt) {
275         visit_type_null(v, NULL, errp);
276         return;
277     }
278 
279     fdt = drc->fdt;
280     fdt_offset = drc->fdt_start_offset;
281     fdt_depth = 0;
282 
283     do {
284         const char *name = NULL;
285         const struct fdt_property *prop = NULL;
286         int prop_len = 0, name_len = 0;
287         uint32_t tag;
288 
289         tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
290         switch (tag) {
291         case FDT_BEGIN_NODE:
292             fdt_depth++;
293             name = fdt_get_name(fdt, fdt_offset, &name_len);
294             visit_start_struct(v, name, NULL, 0, &err);
295             if (err) {
296                 error_propagate(errp, err);
297                 return;
298             }
299             break;
300         case FDT_END_NODE:
301             /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
302             g_assert(fdt_depth > 0);
303             visit_check_struct(v, &err);
304             visit_end_struct(v, NULL);
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, NULL, 0, &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_check_list(v, &err);
328             visit_end_list(v, NULL);
329             if (err) {
330                 error_propagate(errp, err);
331                 return;
332             }
333             break;
334         }
335         default:
336             error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
337         }
338         fdt_offset = fdt_offset_next;
339     } while (fdt_depth != 0);
340 }
341 
342 void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
343                       int fdt_start_offset, Error **errp)
344 {
345     trace_spapr_drc_attach(spapr_drc_index(drc));
346 
347     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
348         error_setg(errp, "an attached device is still awaiting release");
349         return;
350     }
351     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
352         g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
353     }
354     g_assert(fdt);
355 
356     drc->dev = d;
357     drc->fdt = fdt;
358     drc->fdt_start_offset = fdt_start_offset;
359 
360     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
361         drc->awaiting_allocation = true;
362     }
363 
364     object_property_add_link(OBJECT(drc), "device",
365                              object_get_typename(OBJECT(drc->dev)),
366                              (Object **)(&drc->dev),
367                              NULL, 0, NULL);
368 }
369 
370 static void spapr_drc_release(sPAPRDRConnector *drc)
371 {
372     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
373 
374     drck->release(drc->dev);
375 
376     drc->awaiting_release = false;
377     g_free(drc->fdt);
378     drc->fdt = NULL;
379     drc->fdt_start_offset = 0;
380     object_property_del(OBJECT(drc), "device", NULL);
381     drc->dev = NULL;
382 }
383 
384 void spapr_drc_detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
385 {
386     trace_spapr_drc_detach(spapr_drc_index(drc));
387 
388     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
389         trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
390         drc->awaiting_release = true;
391         return;
392     }
393 
394     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
395         drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
396         trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
397         drc->awaiting_release = true;
398         return;
399     }
400 
401     if (drc->awaiting_allocation) {
402         drc->awaiting_release = true;
403         trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
404         return;
405     }
406 
407     spapr_drc_release(drc);
408 }
409 
410 static bool release_pending(sPAPRDRConnector *drc)
411 {
412     return drc->awaiting_release;
413 }
414 
415 static void drc_reset(void *opaque)
416 {
417     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(opaque);
418 
419     trace_spapr_drc_reset(spapr_drc_index(drc));
420 
421     g_free(drc->ccs);
422     drc->ccs = NULL;
423 
424     /* immediately upon reset we can safely assume DRCs whose devices
425      * are pending removal can be safely removed.
426      */
427     if (drc->awaiting_release) {
428         spapr_drc_release(drc);
429     }
430 
431     drc->awaiting_allocation = false;
432 
433     if (drc->dev) {
434         /* A device present at reset is coldplugged */
435         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
436         if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
437             drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
438         }
439         drc->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
440     } else {
441         /* Otherwise device is absent, but might be hotplugged */
442         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
443         if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
444             drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_UNUSABLE;
445         }
446         drc->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
447     }
448 }
449 
450 static bool spapr_drc_needed(void *opaque)
451 {
452     sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
453     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
454     bool rc = false;
455     sPAPRDREntitySense value = drck->dr_entity_sense(drc);
456 
457     /* If no dev is plugged in there is no need to migrate the DRC state */
458     if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
459         return false;
460     }
461 
462     /*
463      * If there is dev plugged in, we need to migrate the DRC state when
464      * it is different from cold-plugged state
465      */
466     switch (spapr_drc_type(drc)) {
467     case SPAPR_DR_CONNECTOR_TYPE_PCI:
468     case SPAPR_DR_CONNECTOR_TYPE_CPU:
469     case SPAPR_DR_CONNECTOR_TYPE_LMB:
470         rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
471                (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
472                drc->configured && !drc->awaiting_release);
473         break;
474     case SPAPR_DR_CONNECTOR_TYPE_PHB:
475     case SPAPR_DR_CONNECTOR_TYPE_VIO:
476     default:
477         g_assert_not_reached();
478     }
479     return rc;
480 }
481 
482 static const VMStateDescription vmstate_spapr_drc = {
483     .name = "spapr_drc",
484     .version_id = 1,
485     .minimum_version_id = 1,
486     .needed = spapr_drc_needed,
487     .fields  = (VMStateField []) {
488         VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
489         VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
490         VMSTATE_UINT32(dr_indicator, sPAPRDRConnector),
491         VMSTATE_BOOL(configured, sPAPRDRConnector),
492         VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
493         VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
494         VMSTATE_END_OF_LIST()
495     }
496 };
497 
498 static void realize(DeviceState *d, Error **errp)
499 {
500     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
501     Object *root_container;
502     char link_name[256];
503     gchar *child_name;
504     Error *err = NULL;
505 
506     trace_spapr_drc_realize(spapr_drc_index(drc));
507     /* NOTE: we do this as part of realize/unrealize due to the fact
508      * that the guest will communicate with the DRC via RTAS calls
509      * referencing the global DRC index. By unlinking the DRC
510      * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
511      * inaccessible by the guest, since lookups rely on this path
512      * existing in the composition tree
513      */
514     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
515     snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
516     child_name = object_get_canonical_path_component(OBJECT(drc));
517     trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
518     object_property_add_alias(root_container, link_name,
519                               drc->owner, child_name, &err);
520     if (err) {
521         error_report_err(err);
522         object_unref(OBJECT(drc));
523     }
524     g_free(child_name);
525     vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
526                      drc);
527     qemu_register_reset(drc_reset, drc);
528     trace_spapr_drc_realize_complete(spapr_drc_index(drc));
529 }
530 
531 static void unrealize(DeviceState *d, Error **errp)
532 {
533     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
534     Object *root_container;
535     char name[256];
536     Error *err = NULL;
537 
538     trace_spapr_drc_unrealize(spapr_drc_index(drc));
539     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
540     snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
541     object_property_del(root_container, name, &err);
542     if (err) {
543         error_report_err(err);
544         object_unref(OBJECT(drc));
545     }
546 }
547 
548 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
549                                          uint32_t id)
550 {
551     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
552     char *prop_name;
553 
554     drc->id = id;
555     drc->owner = owner;
556     prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
557                                 spapr_drc_index(drc));
558     object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
559     object_property_set_bool(OBJECT(drc), true, "realized", NULL);
560     g_free(prop_name);
561 
562     /* PCI slot always start in a USABLE state, and stay there */
563     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
564         drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
565     }
566 
567     return drc;
568 }
569 
570 static void spapr_dr_connector_instance_init(Object *obj)
571 {
572     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
573 
574     object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
575     object_property_add(obj, "index", "uint32", prop_get_index,
576                         NULL, NULL, NULL, NULL);
577     object_property_add(obj, "fdt", "struct", prop_get_fdt,
578                         NULL, NULL, NULL, NULL);
579 }
580 
581 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
582 {
583     DeviceClass *dk = DEVICE_CLASS(k);
584     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
585 
586     dk->realize = realize;
587     dk->unrealize = unrealize;
588     drck->release_pending = release_pending;
589     /*
590      * Reason: it crashes FIXME find and document the real reason
591      */
592     dk->user_creatable = false;
593 }
594 
595 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
596 {
597     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
598 
599     drck->dr_entity_sense = physical_entity_sense;
600     drck->isolate = drc_isolate_physical;
601     drck->unisolate = drc_unisolate_physical;
602 }
603 
604 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
605 {
606     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
607 
608     drck->dr_entity_sense = logical_entity_sense;
609     drck->isolate = drc_isolate_logical;
610     drck->unisolate = drc_unisolate_logical;
611 }
612 
613 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
614 {
615     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
616 
617     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
618     drck->typename = "CPU";
619     drck->drc_name_prefix = "CPU ";
620     drck->release = spapr_core_release;
621 }
622 
623 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
624 {
625     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
626 
627     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
628     drck->typename = "28";
629     drck->drc_name_prefix = "C";
630     drck->release = spapr_phb_remove_pci_device_cb;
631 }
632 
633 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
634 {
635     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
636 
637     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
638     drck->typename = "MEM";
639     drck->drc_name_prefix = "LMB ";
640     drck->release = spapr_lmb_release;
641 }
642 
643 static const TypeInfo spapr_dr_connector_info = {
644     .name          = TYPE_SPAPR_DR_CONNECTOR,
645     .parent        = TYPE_DEVICE,
646     .instance_size = sizeof(sPAPRDRConnector),
647     .instance_init = spapr_dr_connector_instance_init,
648     .class_size    = sizeof(sPAPRDRConnectorClass),
649     .class_init    = spapr_dr_connector_class_init,
650     .abstract      = true,
651 };
652 
653 static const TypeInfo spapr_drc_physical_info = {
654     .name          = TYPE_SPAPR_DRC_PHYSICAL,
655     .parent        = TYPE_SPAPR_DR_CONNECTOR,
656     .instance_size = sizeof(sPAPRDRConnector),
657     .class_init    = spapr_drc_physical_class_init,
658     .abstract      = true,
659 };
660 
661 static const TypeInfo spapr_drc_logical_info = {
662     .name          = TYPE_SPAPR_DRC_LOGICAL,
663     .parent        = TYPE_SPAPR_DR_CONNECTOR,
664     .instance_size = sizeof(sPAPRDRConnector),
665     .class_init    = spapr_drc_logical_class_init,
666     .abstract      = true,
667 };
668 
669 static const TypeInfo spapr_drc_cpu_info = {
670     .name          = TYPE_SPAPR_DRC_CPU,
671     .parent        = TYPE_SPAPR_DRC_LOGICAL,
672     .instance_size = sizeof(sPAPRDRConnector),
673     .class_init    = spapr_drc_cpu_class_init,
674 };
675 
676 static const TypeInfo spapr_drc_pci_info = {
677     .name          = TYPE_SPAPR_DRC_PCI,
678     .parent        = TYPE_SPAPR_DRC_PHYSICAL,
679     .instance_size = sizeof(sPAPRDRConnector),
680     .class_init    = spapr_drc_pci_class_init,
681 };
682 
683 static const TypeInfo spapr_drc_lmb_info = {
684     .name          = TYPE_SPAPR_DRC_LMB,
685     .parent        = TYPE_SPAPR_DRC_LOGICAL,
686     .instance_size = sizeof(sPAPRDRConnector),
687     .class_init    = spapr_drc_lmb_class_init,
688 };
689 
690 /* helper functions for external users */
691 
692 sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
693 {
694     Object *obj;
695     char name[256];
696 
697     snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
698     obj = object_resolve_path(name, NULL);
699 
700     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
701 }
702 
703 sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
704 {
705     sPAPRDRConnectorClass *drck
706         = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
707 
708     return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
709                               | (id & DRC_INDEX_ID_MASK));
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 ((spapr_drc_type(drc) & drc_type_mask) == 0) {
775             continue;
776         }
777 
778         drc_count++;
779 
780         /* ibm,drc-indexes */
781         drc_index = cpu_to_be32(spapr_drc_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, spapr_drc_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, drck->typename);
794         drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
795     }
796 
797     /* now write the drc count into the space we reserved at the
798      * beginning of the arrays previously
799      */
800     *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
801     *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
802     *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
803     *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
804 
805     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
806                       drc_indexes->data,
807                       drc_indexes->len * sizeof(uint32_t));
808     if (ret) {
809         error_report("Couldn't create ibm,drc-indexes property");
810         goto out;
811     }
812 
813     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
814                       drc_power_domains->data,
815                       drc_power_domains->len * sizeof(uint32_t));
816     if (ret) {
817         error_report("Couldn't finalize ibm,drc-power-domains property");
818         goto out;
819     }
820 
821     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
822                       drc_names->str, drc_names->len);
823     if (ret) {
824         error_report("Couldn't finalize ibm,drc-names property");
825         goto out;
826     }
827 
828     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
829                       drc_types->str, drc_types->len);
830     if (ret) {
831         error_report("Couldn't finalize ibm,drc-types property");
832         goto out;
833     }
834 
835 out:
836     g_array_free(drc_indexes, true);
837     g_array_free(drc_power_domains, true);
838     g_string_free(drc_names, true);
839     g_string_free(drc_types, true);
840 
841     return ret;
842 }
843 
844 /*
845  * RTAS calls
846  */
847 
848 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
849 {
850     sPAPRDRConnector *drc = spapr_drc_by_index(idx);
851     sPAPRDRConnectorClass *drck;
852 
853     if (!drc) {
854         return RTAS_OUT_NO_SUCH_INDICATOR;
855     }
856 
857     trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
858 
859     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
860 
861     switch (state) {
862     case SPAPR_DR_ISOLATION_STATE_ISOLATED:
863         return drck->isolate(drc);
864 
865     case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
866         return drck->unisolate(drc);
867 
868     default:
869         return RTAS_OUT_PARAM_ERROR;
870     }
871 }
872 
873 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
874 {
875     sPAPRDRConnector *drc = spapr_drc_by_index(idx);
876 
877     if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
878         return RTAS_OUT_NO_SUCH_INDICATOR;
879     }
880 
881     trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
882 
883     switch (state) {
884     case SPAPR_DR_ALLOCATION_STATE_USABLE:
885         return drc_set_usable(drc);
886 
887     case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
888         return drc_set_unusable(drc);
889 
890     default:
891         return RTAS_OUT_PARAM_ERROR;
892     }
893 }
894 
895 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
896 {
897     sPAPRDRConnector *drc = spapr_drc_by_index(idx);
898 
899     if (!drc) {
900         return RTAS_OUT_PARAM_ERROR;
901     }
902 
903     trace_spapr_drc_set_dr_indicator(idx, state);
904     drc->dr_indicator = state;
905     return RTAS_OUT_SUCCESS;
906 }
907 
908 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
909                                uint32_t token,
910                                uint32_t nargs, target_ulong args,
911                                uint32_t nret, target_ulong rets)
912 {
913     uint32_t type, idx, state;
914     uint32_t ret = RTAS_OUT_SUCCESS;
915 
916     if (nargs != 3 || nret != 1) {
917         ret = RTAS_OUT_PARAM_ERROR;
918         goto out;
919     }
920 
921     type = rtas_ld(args, 0);
922     idx = rtas_ld(args, 1);
923     state = rtas_ld(args, 2);
924 
925     switch (type) {
926     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
927         ret = rtas_set_isolation_state(idx, state);
928         break;
929     case RTAS_SENSOR_TYPE_DR:
930         ret = rtas_set_dr_indicator(idx, state);
931         break;
932     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
933         ret = rtas_set_allocation_state(idx, state);
934         break;
935     default:
936         ret = RTAS_OUT_NOT_SUPPORTED;
937     }
938 
939 out:
940     rtas_st(rets, 0, ret);
941 }
942 
943 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
944                                   uint32_t token, uint32_t nargs,
945                                   target_ulong args, uint32_t nret,
946                                   target_ulong rets)
947 {
948     uint32_t sensor_type;
949     uint32_t sensor_index;
950     uint32_t sensor_state = 0;
951     sPAPRDRConnector *drc;
952     sPAPRDRConnectorClass *drck;
953     uint32_t ret = RTAS_OUT_SUCCESS;
954 
955     if (nargs != 2 || nret != 2) {
956         ret = RTAS_OUT_PARAM_ERROR;
957         goto out;
958     }
959 
960     sensor_type = rtas_ld(args, 0);
961     sensor_index = rtas_ld(args, 1);
962 
963     if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
964         /* currently only DR-related sensors are implemented */
965         trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
966                                                         sensor_type);
967         ret = RTAS_OUT_NOT_SUPPORTED;
968         goto out;
969     }
970 
971     drc = spapr_drc_by_index(sensor_index);
972     if (!drc) {
973         trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
974         ret = RTAS_OUT_PARAM_ERROR;
975         goto out;
976     }
977     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
978     sensor_state = drck->dr_entity_sense(drc);
979 
980 out:
981     rtas_st(rets, 0, ret);
982     rtas_st(rets, 1, sensor_state);
983 }
984 
985 /* configure-connector work area offsets, int32_t units for field
986  * indexes, bytes for field offset/len values.
987  *
988  * as documented by PAPR+ v2.7, 13.5.3.5
989  */
990 #define CC_IDX_NODE_NAME_OFFSET 2
991 #define CC_IDX_PROP_NAME_OFFSET 2
992 #define CC_IDX_PROP_LEN 3
993 #define CC_IDX_PROP_DATA_OFFSET 4
994 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
995 #define CC_WA_LEN 4096
996 
997 static void configure_connector_st(target_ulong addr, target_ulong offset,
998                                    const void *buf, size_t len)
999 {
1000     cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1001                               buf, MIN(len, CC_WA_LEN - offset));
1002 }
1003 
1004 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1005                                          sPAPRMachineState *spapr,
1006                                          uint32_t token, uint32_t nargs,
1007                                          target_ulong args, uint32_t nret,
1008                                          target_ulong rets)
1009 {
1010     uint64_t wa_addr;
1011     uint64_t wa_offset;
1012     uint32_t drc_index;
1013     sPAPRDRConnector *drc;
1014     sPAPRConfigureConnectorState *ccs;
1015     sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1016     int rc;
1017 
1018     if (nargs != 2 || nret != 1) {
1019         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1020         return;
1021     }
1022 
1023     wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1024 
1025     drc_index = rtas_ld(wa_addr, 0);
1026     drc = spapr_drc_by_index(drc_index);
1027     if (!drc) {
1028         trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1029         rc = RTAS_OUT_PARAM_ERROR;
1030         goto out;
1031     }
1032 
1033     if (!drc->fdt) {
1034         trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1035         rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1036         goto out;
1037     }
1038 
1039     ccs = drc->ccs;
1040     if (!ccs) {
1041         ccs = g_new0(sPAPRConfigureConnectorState, 1);
1042         ccs->fdt_offset = drc->fdt_start_offset;
1043         drc->ccs = ccs;
1044     }
1045 
1046     do {
1047         uint32_t tag;
1048         const char *name;
1049         const struct fdt_property *prop;
1050         int fdt_offset_next, prop_len;
1051 
1052         tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1053 
1054         switch (tag) {
1055         case FDT_BEGIN_NODE:
1056             ccs->fdt_depth++;
1057             name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1058 
1059             /* provide the name of the next OF node */
1060             wa_offset = CC_VAL_DATA_OFFSET;
1061             rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1062             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1063             resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1064             break;
1065         case FDT_END_NODE:
1066             ccs->fdt_depth--;
1067             if (ccs->fdt_depth == 0) {
1068                 sPAPRDRIsolationState state = drc->isolation_state;
1069                 uint32_t drc_index = spapr_drc_index(drc);
1070                 /* done sending the device tree, don't need to track
1071                  * the state anymore
1072                  */
1073                 trace_spapr_drc_set_configured(drc_index);
1074                 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
1075                     drc->configured = true;
1076                 } else {
1077                     /* guest should be not configuring an isolated device */
1078                     trace_spapr_drc_set_configured_skipping(drc_index);
1079                 }
1080                 g_free(ccs);
1081                 drc->ccs = NULL;
1082                 ccs = NULL;
1083                 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1084             } else {
1085                 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1086             }
1087             break;
1088         case FDT_PROP:
1089             prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1090                                               &prop_len);
1091             name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1092 
1093             /* provide the name of the next OF property */
1094             wa_offset = CC_VAL_DATA_OFFSET;
1095             rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1096             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1097 
1098             /* provide the length and value of the OF property. data gets
1099              * placed immediately after NULL terminator of the OF property's
1100              * name string
1101              */
1102             wa_offset += strlen(name) + 1,
1103             rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1104             rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1105             configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1106             resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1107             break;
1108         case FDT_END:
1109             resp = SPAPR_DR_CC_RESPONSE_ERROR;
1110         default:
1111             /* keep seeking for an actionable tag */
1112             break;
1113         }
1114         if (ccs) {
1115             ccs->fdt_offset = fdt_offset_next;
1116         }
1117     } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1118 
1119     rc = resp;
1120 out:
1121     rtas_st(rets, 0, rc);
1122 }
1123 
1124 static void spapr_drc_register_types(void)
1125 {
1126     type_register_static(&spapr_dr_connector_info);
1127     type_register_static(&spapr_drc_physical_info);
1128     type_register_static(&spapr_drc_logical_info);
1129     type_register_static(&spapr_drc_cpu_info);
1130     type_register_static(&spapr_drc_pci_info);
1131     type_register_static(&spapr_drc_lmb_info);
1132 
1133     spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1134                         rtas_set_indicator);
1135     spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1136                         rtas_get_sensor_state);
1137     spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1138                         rtas_ibm_configure_connector);
1139 }
1140 type_init(spapr_drc_register_types)
1141