2 * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
4 * Copyright IBM Corp. 2014
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
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.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qnull.h"
17 #include "qemu/cutils.h"
18 #include "hw/ppc/spapr_drc.h"
19 #include "qom/object.h"
21 #include "qapi/visitor.h"
22 #include "qemu/error-report.h"
23 #include "hw/ppc/spapr.h" /* for RTAS return codes */
24 #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
27 #define DRC_CONTAINER_PATH "/dr-connector"
28 #define DRC_INDEX_TYPE_SHIFT 28
29 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
31 sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc)
33 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
35 return 1 << drck->typeshift;
38 uint32_t spapr_drc_index(sPAPRDRConnector *drc)
40 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
42 /* no set format for a drc index: it only needs to be globally
43 * unique. this is how we encode the DRC type on bare-metal
44 * however, so might as well do that here
46 return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
47 | (drc->id & DRC_INDEX_ID_MASK);
50 static uint32_t drc_isolate_physical(sPAPRDRConnector *drc)
53 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
54 return RTAS_OUT_SUCCESS; /* Nothing to do */
55 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
56 break; /* see below */
57 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
58 return RTAS_OUT_PARAM_ERROR; /* not allowed */
60 g_assert_not_reached();
63 drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
65 if (drc->unplug_requested) {
66 uint32_t drc_index = spapr_drc_index(drc);
67 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
68 spapr_drc_detach(drc);
71 return RTAS_OUT_SUCCESS;
74 static uint32_t drc_unisolate_physical(sPAPRDRConnector *drc)
77 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
78 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
79 return RTAS_OUT_SUCCESS; /* Nothing to do */
80 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
81 break; /* see below */
83 g_assert_not_reached();
86 /* cannot unisolate a non-existent resource, and, or resources
87 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
91 return RTAS_OUT_NO_SUCH_INDICATOR;
94 drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE;
95 drc->ccs_offset = drc->fdt_start_offset;
98 return RTAS_OUT_SUCCESS;
101 static uint32_t drc_isolate_logical(sPAPRDRConnector *drc)
103 switch (drc->state) {
104 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
105 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
106 return RTAS_OUT_SUCCESS; /* Nothing to do */
107 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
108 break; /* see below */
109 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
110 return RTAS_OUT_PARAM_ERROR; /* not allowed */
112 g_assert_not_reached();
116 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
117 * belong to a DIMM device that is marked for removal.
119 * Currently the guest userspace tool drmgr that drives the memory
120 * hotplug/unplug will just try to remove a set of 'removable' LMBs
121 * in response to a hot unplug request that is based on drc-count.
122 * If the LMB being removed doesn't belong to a DIMM device that is
123 * actually being unplugged, fail the isolation request here.
125 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
126 && !drc->unplug_requested) {
127 return RTAS_OUT_HW_ERROR;
130 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
132 /* if we're awaiting release, but still in an unconfigured state,
133 * it's likely the guest is still in the process of configuring
134 * the device and is transitioning the devices to an ISOLATED
135 * state as a part of that process. so we only complete the
136 * removal when this transition happens for a device in a
137 * configured state, as suggested by the state diagram from PAPR+
140 if (drc->unplug_requested) {
141 uint32_t drc_index = spapr_drc_index(drc);
142 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
143 spapr_drc_detach(drc);
145 return RTAS_OUT_SUCCESS;
148 static uint32_t drc_unisolate_logical(sPAPRDRConnector *drc)
150 switch (drc->state) {
151 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
152 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
153 return RTAS_OUT_SUCCESS; /* Nothing to do */
154 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
155 break; /* see below */
156 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
157 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
159 g_assert_not_reached();
162 /* Move to AVAILABLE state should have ensured device was present */
165 drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
166 drc->ccs_offset = drc->fdt_start_offset;
169 return RTAS_OUT_SUCCESS;
172 static uint32_t drc_set_usable(sPAPRDRConnector *drc)
174 switch (drc->state) {
175 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
176 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
177 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
178 return RTAS_OUT_SUCCESS; /* Nothing to do */
179 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
180 break; /* see below */
182 g_assert_not_reached();
185 /* if there's no resource/device associated with the DRC, there's
186 * no way for us to put it in an allocation state consistent with
187 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
188 * result in an RTAS return code of -3 / "no such indicator"
191 return RTAS_OUT_NO_SUCH_INDICATOR;
193 if (drc->unplug_requested) {
194 /* Don't allow the guest to move a device away from UNUSABLE
195 * state when we want to unplug it */
196 return RTAS_OUT_NO_SUCH_INDICATOR;
199 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
201 return RTAS_OUT_SUCCESS;
204 static uint32_t drc_set_unusable(sPAPRDRConnector *drc)
206 switch (drc->state) {
207 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
208 return RTAS_OUT_SUCCESS; /* Nothing to do */
209 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
210 break; /* see below */
211 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
212 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
213 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
215 g_assert_not_reached();
218 drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
219 if (drc->unplug_requested) {
220 uint32_t drc_index = spapr_drc_index(drc);
221 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
222 spapr_drc_detach(drc);
225 return RTAS_OUT_SUCCESS;
228 static const char *spapr_drc_name(sPAPRDRConnector *drc)
230 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
232 /* human-readable name for a DRC to encode into the DT
233 * description. this is mainly only used within a guest in place
234 * of the unique DRC index.
236 * in the case of VIO/PCI devices, it corresponds to a "location
237 * code" that maps a logical device/function (DRC index) to a
238 * physical (or virtual in the case of VIO) location in the system
239 * by chaining together the "location label" for each
240 * encapsulating component.
242 * since this is more to do with diagnosing physical hardware
243 * issues than guest compatibility, we choose location codes/DRC
244 * names that adhere to the documented format, but avoid encoding
245 * the entire topology information into the label/code, instead
246 * just using the location codes based on the labels for the
247 * endpoints (VIO/PCI adaptor connectors), which is basically just
248 * "C" followed by an integer ID.
250 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
251 * location codes as documented by PAPR+ v2.7, 12.3.1.5
253 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
257 * dr-entity-sense sensor value
258 * returned via get-sensor-state RTAS calls
259 * as expected by state diagram in PAPR+ 2.7, 13.4
260 * based on the current allocation/indicator/power states
261 * for the DR connector.
263 static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc)
265 /* this assumes all PCI devices are assigned to a 'live insertion'
266 * power domain, where QEMU manages power state automatically as
267 * opposed to the guest. present, non-PCI resources are unaffected
270 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
271 : SPAPR_DR_ENTITY_SENSE_EMPTY;
274 static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc)
276 switch (drc->state) {
277 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
278 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
279 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
280 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
281 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
283 return SPAPR_DR_ENTITY_SENSE_PRESENT;
285 g_assert_not_reached();
289 static void prop_get_index(Object *obj, Visitor *v, const char *name,
290 void *opaque, Error **errp)
292 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
293 uint32_t value = spapr_drc_index(drc);
294 visit_type_uint32(v, name, &value, errp);
297 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
298 void *opaque, Error **errp)
300 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
303 int fdt_offset_next, fdt_offset, fdt_depth;
307 visit_type_null(v, NULL, &null, errp);
313 fdt_offset = drc->fdt_start_offset;
317 const char *name = NULL;
318 const struct fdt_property *prop = NULL;
319 int prop_len = 0, name_len = 0;
322 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
326 name = fdt_get_name(fdt, fdt_offset, &name_len);
327 visit_start_struct(v, name, NULL, 0, &err);
329 error_propagate(errp, err);
334 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
335 g_assert(fdt_depth > 0);
336 visit_check_struct(v, &err);
337 visit_end_struct(v, NULL);
339 error_propagate(errp, err);
346 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
347 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
348 visit_start_list(v, name, NULL, 0, &err);
350 error_propagate(errp, err);
353 for (i = 0; i < prop_len; i++) {
354 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
356 error_propagate(errp, err);
360 visit_check_list(v, &err);
361 visit_end_list(v, NULL);
363 error_propagate(errp, err);
369 error_report("device FDT in unexpected state: %d", tag);
372 fdt_offset = fdt_offset_next;
373 } while (fdt_depth != 0);
376 void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
377 int fdt_start_offset, Error **errp)
379 trace_spapr_drc_attach(spapr_drc_index(drc));
382 error_setg(errp, "an attached device is still awaiting release");
385 g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
386 || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
391 drc->fdt_start_offset = fdt_start_offset;
393 object_property_add_link(OBJECT(drc), "device",
394 object_get_typename(OBJECT(drc->dev)),
395 (Object **)(&drc->dev),
399 static void spapr_drc_release(sPAPRDRConnector *drc)
401 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
403 drck->release(drc->dev);
405 drc->unplug_requested = false;
408 drc->fdt_start_offset = 0;
409 object_property_del(OBJECT(drc), "device", &error_abort);
413 void spapr_drc_detach(sPAPRDRConnector *drc)
415 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
417 trace_spapr_drc_detach(spapr_drc_index(drc));
421 drc->unplug_requested = true;
423 if (drc->state != drck->empty_state) {
424 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
428 spapr_drc_release(drc);
431 void spapr_drc_reset(sPAPRDRConnector *drc)
433 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
435 trace_spapr_drc_reset(spapr_drc_index(drc));
437 /* immediately upon reset we can safely assume DRCs whose devices
438 * are pending removal can be safely removed.
440 if (drc->unplug_requested) {
441 spapr_drc_release(drc);
445 /* A device present at reset is ready to go, same as coldplugged */
446 drc->state = drck->ready_state;
448 * Ensure that we are able to send the FDT fragment again
449 * via configure-connector call if the guest requests.
451 drc->ccs_offset = drc->fdt_start_offset;
454 drc->state = drck->empty_state;
455 drc->ccs_offset = -1;
460 bool spapr_drc_needed(void *opaque)
462 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
463 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
465 /* If no dev is plugged in there is no need to migrate the DRC state */
471 * We need to migrate the state if it's not equal to the expected
472 * long-term state, which is the same as the coldplugged initial
474 return (drc->state != drck->ready_state);
477 static const VMStateDescription vmstate_spapr_drc = {
480 .minimum_version_id = 1,
481 .needed = spapr_drc_needed,
482 .fields = (VMStateField []) {
483 VMSTATE_UINT32(state, sPAPRDRConnector),
484 VMSTATE_END_OF_LIST()
488 static void realize(DeviceState *d, Error **errp)
490 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
491 Object *root_container;
496 trace_spapr_drc_realize(spapr_drc_index(drc));
497 /* NOTE: we do this as part of realize/unrealize due to the fact
498 * that the guest will communicate with the DRC via RTAS calls
499 * referencing the global DRC index. By unlinking the DRC
500 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
501 * inaccessible by the guest, since lookups rely on this path
502 * existing in the composition tree
504 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
505 link_name = g_strdup_printf("%x", spapr_drc_index(drc));
506 child_name = object_get_canonical_path_component(OBJECT(drc));
507 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
508 object_property_add_alias(root_container, link_name,
509 drc->owner, child_name, &err);
513 error_propagate(errp, err);
516 vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
518 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
521 static void unrealize(DeviceState *d, Error **errp)
523 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
524 Object *root_container;
527 trace_spapr_drc_unrealize(spapr_drc_index(drc));
528 vmstate_unregister(DEVICE(drc), &vmstate_spapr_drc, drc);
529 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
530 name = g_strdup_printf("%x", spapr_drc_index(drc));
531 object_property_del(root_container, name, errp);
535 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
538 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
543 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
544 spapr_drc_index(drc));
545 object_property_add_child(owner, prop_name, OBJECT(drc), &error_abort);
546 object_unref(OBJECT(drc));
547 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
553 static void spapr_dr_connector_instance_init(Object *obj)
555 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
556 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
558 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
559 object_property_add(obj, "index", "uint32", prop_get_index,
560 NULL, NULL, NULL, NULL);
561 object_property_add(obj, "fdt", "struct", prop_get_fdt,
562 NULL, NULL, NULL, NULL);
563 drc->state = drck->empty_state;
566 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
568 DeviceClass *dk = DEVICE_CLASS(k);
570 dk->realize = realize;
571 dk->unrealize = unrealize;
573 * Reason: it crashes FIXME find and document the real reason
575 dk->user_creatable = false;
578 static bool drc_physical_needed(void *opaque)
580 sPAPRDRCPhysical *drcp = (sPAPRDRCPhysical *)opaque;
581 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(drcp);
583 if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
584 || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
590 static const VMStateDescription vmstate_spapr_drc_physical = {
591 .name = "spapr_drc/physical",
593 .minimum_version_id = 1,
594 .needed = drc_physical_needed,
595 .fields = (VMStateField []) {
596 VMSTATE_UINT32(dr_indicator, sPAPRDRCPhysical),
597 VMSTATE_END_OF_LIST()
601 static void drc_physical_reset(void *opaque)
603 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(opaque);
604 sPAPRDRCPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
607 drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
609 drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
613 static void realize_physical(DeviceState *d, Error **errp)
615 sPAPRDRCPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
616 Error *local_err = NULL;
618 realize(d, &local_err);
620 error_propagate(errp, local_err);
624 vmstate_register(DEVICE(drcp), spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
625 &vmstate_spapr_drc_physical, drcp);
626 qemu_register_reset(drc_physical_reset, drcp);
629 static void unrealize_physical(DeviceState *d, Error **errp)
631 sPAPRDRCPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
632 Error *local_err = NULL;
634 unrealize(d, &local_err);
636 error_propagate(errp, local_err);
640 vmstate_unregister(DEVICE(drcp), &vmstate_spapr_drc_physical, drcp);
641 qemu_unregister_reset(drc_physical_reset, drcp);
644 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
646 DeviceClass *dk = DEVICE_CLASS(k);
647 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
649 dk->realize = realize_physical;
650 dk->unrealize = unrealize_physical;
651 drck->dr_entity_sense = physical_entity_sense;
652 drck->isolate = drc_isolate_physical;
653 drck->unisolate = drc_unisolate_physical;
654 drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
655 drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
658 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
660 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
662 drck->dr_entity_sense = logical_entity_sense;
663 drck->isolate = drc_isolate_logical;
664 drck->unisolate = drc_unisolate_logical;
665 drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
666 drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
669 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
671 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
673 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
674 drck->typename = "CPU";
675 drck->drc_name_prefix = "CPU ";
676 drck->release = spapr_core_release;
679 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
681 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
683 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
684 drck->typename = "28";
685 drck->drc_name_prefix = "C";
686 drck->release = spapr_phb_remove_pci_device_cb;
689 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
691 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
693 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
694 drck->typename = "MEM";
695 drck->drc_name_prefix = "LMB ";
696 drck->release = spapr_lmb_release;
699 static const TypeInfo spapr_dr_connector_info = {
700 .name = TYPE_SPAPR_DR_CONNECTOR,
701 .parent = TYPE_DEVICE,
702 .instance_size = sizeof(sPAPRDRConnector),
703 .instance_init = spapr_dr_connector_instance_init,
704 .class_size = sizeof(sPAPRDRConnectorClass),
705 .class_init = spapr_dr_connector_class_init,
709 static const TypeInfo spapr_drc_physical_info = {
710 .name = TYPE_SPAPR_DRC_PHYSICAL,
711 .parent = TYPE_SPAPR_DR_CONNECTOR,
712 .instance_size = sizeof(sPAPRDRCPhysical),
713 .class_init = spapr_drc_physical_class_init,
717 static const TypeInfo spapr_drc_logical_info = {
718 .name = TYPE_SPAPR_DRC_LOGICAL,
719 .parent = TYPE_SPAPR_DR_CONNECTOR,
720 .class_init = spapr_drc_logical_class_init,
724 static const TypeInfo spapr_drc_cpu_info = {
725 .name = TYPE_SPAPR_DRC_CPU,
726 .parent = TYPE_SPAPR_DRC_LOGICAL,
727 .class_init = spapr_drc_cpu_class_init,
730 static const TypeInfo spapr_drc_pci_info = {
731 .name = TYPE_SPAPR_DRC_PCI,
732 .parent = TYPE_SPAPR_DRC_PHYSICAL,
733 .class_init = spapr_drc_pci_class_init,
736 static const TypeInfo spapr_drc_lmb_info = {
737 .name = TYPE_SPAPR_DRC_LMB,
738 .parent = TYPE_SPAPR_DRC_LOGICAL,
739 .class_init = spapr_drc_lmb_class_init,
742 /* helper functions for external users */
744 sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
749 name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH, index);
750 obj = object_resolve_path(name, NULL);
753 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
756 sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
758 sPAPRDRConnectorClass *drck
759 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
761 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
762 | (id & DRC_INDEX_ID_MASK));
766 * spapr_drc_populate_dt
768 * @fdt: libfdt device tree
769 * @path: path in the DT to generate properties
770 * @owner: parent Object/DeviceState for which to generate DRC
772 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
773 * to the types of DRCs to generate entries for
775 * generate OF properties to describe DRC topology/indices to guests
777 * as documented in PAPR+ v2.1, 13.5.2
779 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
780 uint32_t drc_type_mask)
782 Object *root_container;
783 ObjectProperty *prop;
784 ObjectPropertyIterator iter;
785 uint32_t drc_count = 0;
786 GArray *drc_indexes, *drc_power_domains;
787 GString *drc_names, *drc_types;
790 /* the first entry of each properties is a 32-bit integer encoding
791 * the number of elements in the array. we won't know this until
792 * we complete the iteration through all the matching DRCs, but
793 * reserve the space now and set the offsets accordingly so we
794 * can fill them in later.
796 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
797 drc_indexes = g_array_set_size(drc_indexes, 1);
798 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
799 drc_power_domains = g_array_set_size(drc_power_domains, 1);
800 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
801 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
803 /* aliases for all DRConnector objects will be rooted in QOM
804 * composition tree at DRC_CONTAINER_PATH
806 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
808 object_property_iter_init(&iter, root_container);
809 while ((prop = object_property_iter_next(&iter))) {
811 sPAPRDRConnector *drc;
812 sPAPRDRConnectorClass *drck;
813 uint32_t drc_index, drc_power_domain;
815 if (!strstart(prop->type, "link<", NULL)) {
819 obj = object_property_get_link(root_container, prop->name, NULL);
820 drc = SPAPR_DR_CONNECTOR(obj);
821 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
823 if (owner && (drc->owner != owner)) {
827 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
833 /* ibm,drc-indexes */
834 drc_index = cpu_to_be32(spapr_drc_index(drc));
835 g_array_append_val(drc_indexes, drc_index);
837 /* ibm,drc-power-domains */
838 drc_power_domain = cpu_to_be32(-1);
839 g_array_append_val(drc_power_domains, drc_power_domain);
842 drc_names = g_string_append(drc_names, spapr_drc_name(drc));
843 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
846 drc_types = g_string_append(drc_types, drck->typename);
847 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
850 /* now write the drc count into the space we reserved at the
851 * beginning of the arrays previously
853 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
854 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
855 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
856 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
858 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
860 drc_indexes->len * sizeof(uint32_t));
862 error_report("Couldn't create ibm,drc-indexes property");
866 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
867 drc_power_domains->data,
868 drc_power_domains->len * sizeof(uint32_t));
870 error_report("Couldn't finalize ibm,drc-power-domains property");
874 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
875 drc_names->str, drc_names->len);
877 error_report("Couldn't finalize ibm,drc-names property");
881 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
882 drc_types->str, drc_types->len);
884 error_report("Couldn't finalize ibm,drc-types property");
889 g_array_free(drc_indexes, true);
890 g_array_free(drc_power_domains, true);
891 g_string_free(drc_names, true);
892 g_string_free(drc_types, true);
901 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
903 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
904 sPAPRDRConnectorClass *drck;
907 return RTAS_OUT_NO_SUCH_INDICATOR;
910 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
912 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
915 case SPAPR_DR_ISOLATION_STATE_ISOLATED:
916 return drck->isolate(drc);
918 case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
919 return drck->unisolate(drc);
922 return RTAS_OUT_PARAM_ERROR;
926 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
928 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
930 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
931 return RTAS_OUT_NO_SUCH_INDICATOR;
934 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
937 case SPAPR_DR_ALLOCATION_STATE_USABLE:
938 return drc_set_usable(drc);
940 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
941 return drc_set_unusable(drc);
944 return RTAS_OUT_PARAM_ERROR;
948 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
950 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
952 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
953 return RTAS_OUT_NO_SUCH_INDICATOR;
955 if ((state != SPAPR_DR_INDICATOR_INACTIVE)
956 && (state != SPAPR_DR_INDICATOR_ACTIVE)
957 && (state != SPAPR_DR_INDICATOR_IDENTIFY)
958 && (state != SPAPR_DR_INDICATOR_ACTION)) {
959 return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
962 trace_spapr_drc_set_dr_indicator(idx, state);
963 SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
964 return RTAS_OUT_SUCCESS;
967 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
969 uint32_t nargs, target_ulong args,
970 uint32_t nret, target_ulong rets)
972 uint32_t type, idx, state;
973 uint32_t ret = RTAS_OUT_SUCCESS;
975 if (nargs != 3 || nret != 1) {
976 ret = RTAS_OUT_PARAM_ERROR;
980 type = rtas_ld(args, 0);
981 idx = rtas_ld(args, 1);
982 state = rtas_ld(args, 2);
985 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
986 ret = rtas_set_isolation_state(idx, state);
988 case RTAS_SENSOR_TYPE_DR:
989 ret = rtas_set_dr_indicator(idx, state);
991 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
992 ret = rtas_set_allocation_state(idx, state);
995 ret = RTAS_OUT_NOT_SUPPORTED;
999 rtas_st(rets, 0, ret);
1002 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
1003 uint32_t token, uint32_t nargs,
1004 target_ulong args, uint32_t nret,
1007 uint32_t sensor_type;
1008 uint32_t sensor_index;
1009 uint32_t sensor_state = 0;
1010 sPAPRDRConnector *drc;
1011 sPAPRDRConnectorClass *drck;
1012 uint32_t ret = RTAS_OUT_SUCCESS;
1014 if (nargs != 2 || nret != 2) {
1015 ret = RTAS_OUT_PARAM_ERROR;
1019 sensor_type = rtas_ld(args, 0);
1020 sensor_index = rtas_ld(args, 1);
1022 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1023 /* currently only DR-related sensors are implemented */
1024 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1026 ret = RTAS_OUT_NOT_SUPPORTED;
1030 drc = spapr_drc_by_index(sensor_index);
1032 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1033 ret = RTAS_OUT_PARAM_ERROR;
1036 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1037 sensor_state = drck->dr_entity_sense(drc);
1040 rtas_st(rets, 0, ret);
1041 rtas_st(rets, 1, sensor_state);
1044 /* configure-connector work area offsets, int32_t units for field
1045 * indexes, bytes for field offset/len values.
1047 * as documented by PAPR+ v2.7, 13.5.3.5
1049 #define CC_IDX_NODE_NAME_OFFSET 2
1050 #define CC_IDX_PROP_NAME_OFFSET 2
1051 #define CC_IDX_PROP_LEN 3
1052 #define CC_IDX_PROP_DATA_OFFSET 4
1053 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1054 #define CC_WA_LEN 4096
1056 static void configure_connector_st(target_ulong addr, target_ulong offset,
1057 const void *buf, size_t len)
1059 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1060 buf, MIN(len, CC_WA_LEN - offset));
1063 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1064 sPAPRMachineState *spapr,
1065 uint32_t token, uint32_t nargs,
1066 target_ulong args, uint32_t nret,
1072 sPAPRDRConnector *drc;
1073 sPAPRDRConnectorClass *drck;
1074 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1077 if (nargs != 2 || nret != 1) {
1078 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1082 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1084 drc_index = rtas_ld(wa_addr, 0);
1085 drc = spapr_drc_by_index(drc_index);
1087 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1088 rc = RTAS_OUT_PARAM_ERROR;
1092 if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
1093 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
1094 && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
1095 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
1097 * Need to unisolate the device before configuring
1098 * or it should already be in configured state to
1099 * allow configure-connector be called repeatedly.
1101 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1107 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1112 const struct fdt_property *prop;
1113 int fdt_offset_next, prop_len;
1115 tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
1118 case FDT_BEGIN_NODE:
1120 name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
1122 /* provide the name of the next OF node */
1123 wa_offset = CC_VAL_DATA_OFFSET;
1124 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1125 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1126 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1130 if (drc->ccs_depth == 0) {
1131 uint32_t drc_index = spapr_drc_index(drc);
1133 /* done sending the device tree, move to configured state */
1134 trace_spapr_drc_set_configured(drc_index);
1135 drc->state = drck->ready_state;
1137 * Ensure that we are able to send the FDT fragment
1138 * again via configure-connector call if the guest requests.
1140 drc->ccs_offset = drc->fdt_start_offset;
1142 fdt_offset_next = drc->fdt_start_offset;
1143 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1145 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1149 prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
1151 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1153 /* provide the name of the next OF property */
1154 wa_offset = CC_VAL_DATA_OFFSET;
1155 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1156 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1158 /* provide the length and value of the OF property. data gets
1159 * placed immediately after NULL terminator of the OF property's
1162 wa_offset += strlen(name) + 1,
1163 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1164 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1165 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1166 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1169 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1171 /* keep seeking for an actionable tag */
1174 if (drc->ccs_offset >= 0) {
1175 drc->ccs_offset = fdt_offset_next;
1177 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1181 rtas_st(rets, 0, rc);
1184 static void spapr_drc_register_types(void)
1186 type_register_static(&spapr_dr_connector_info);
1187 type_register_static(&spapr_drc_physical_info);
1188 type_register_static(&spapr_drc_logical_info);
1189 type_register_static(&spapr_drc_cpu_info);
1190 type_register_static(&spapr_drc_pci_info);
1191 type_register_static(&spapr_drc_lmb_info);
1193 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1194 rtas_set_indicator);
1195 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1196 rtas_get_sensor_state);
1197 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1198 rtas_ibm_configure_connector);
1200 type_init(spapr_drc_register_types)