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