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"
20 #include "migration/vmstate.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 */
25 #include "hw/ppc/spapr_nvdimm.h"
26 #include "sysemu/device_tree.h"
27 #include "sysemu/reset.h"
30 #define DRC_CONTAINER_PATH "/dr-connector"
31 #define DRC_INDEX_TYPE_SHIFT 28
32 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
34 SpaprDrcType
spapr_drc_type(SpaprDrc
*drc
)
36 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
38 return 1 << drck
->typeshift
;
41 uint32_t spapr_drc_index(SpaprDrc
*drc
)
43 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
45 /* no set format for a drc index: it only needs to be globally
46 * unique. this is how we encode the DRC type on bare-metal
47 * however, so might as well do that here
49 return (drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
)
50 | (drc
->id
& DRC_INDEX_ID_MASK
);
53 static uint32_t drc_isolate_physical(SpaprDrc
*drc
)
56 case SPAPR_DRC_STATE_PHYSICAL_POWERON
:
57 return RTAS_OUT_SUCCESS
; /* Nothing to do */
58 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
:
59 break; /* see below */
60 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
:
61 return RTAS_OUT_PARAM_ERROR
; /* not allowed */
63 g_assert_not_reached();
66 drc
->state
= SPAPR_DRC_STATE_PHYSICAL_POWERON
;
68 if (drc
->unplug_requested
) {
69 uint32_t drc_index
= spapr_drc_index(drc
);
70 trace_spapr_drc_set_isolation_state_finalizing(drc_index
);
71 spapr_drc_detach(drc
);
74 return RTAS_OUT_SUCCESS
;
77 static uint32_t drc_unisolate_physical(SpaprDrc
*drc
)
80 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
:
81 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
:
82 return RTAS_OUT_SUCCESS
; /* Nothing to do */
83 case SPAPR_DRC_STATE_PHYSICAL_POWERON
:
84 break; /* see below */
86 g_assert_not_reached();
89 /* cannot unisolate a non-existent resource, and, or resources
90 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
94 return RTAS_OUT_NO_SUCH_INDICATOR
;
97 drc
->state
= SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
;
98 drc
->ccs_offset
= drc
->fdt_start_offset
;
101 return RTAS_OUT_SUCCESS
;
104 static uint32_t drc_isolate_logical(SpaprDrc
*drc
)
106 switch (drc
->state
) {
107 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
108 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
109 return RTAS_OUT_SUCCESS
; /* Nothing to do */
110 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
111 break; /* see below */
112 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
113 return RTAS_OUT_PARAM_ERROR
; /* not allowed */
115 g_assert_not_reached();
119 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
120 * belong to a DIMM device that is marked for removal.
122 * Currently the guest userspace tool drmgr that drives the memory
123 * hotplug/unplug will just try to remove a set of 'removable' LMBs
124 * in response to a hot unplug request that is based on drc-count.
125 * If the LMB being removed doesn't belong to a DIMM device that is
126 * actually being unplugged, fail the isolation request here.
128 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_LMB
129 && !drc
->unplug_requested
) {
130 return RTAS_OUT_HW_ERROR
;
133 drc
->state
= SPAPR_DRC_STATE_LOGICAL_AVAILABLE
;
135 /* if we're awaiting release, but still in an unconfigured state,
136 * it's likely the guest is still in the process of configuring
137 * the device and is transitioning the devices to an ISOLATED
138 * state as a part of that process. so we only complete the
139 * removal when this transition happens for a device in a
140 * configured state, as suggested by the state diagram from PAPR+
143 if (drc
->unplug_requested
) {
144 uint32_t drc_index
= spapr_drc_index(drc
);
145 trace_spapr_drc_set_isolation_state_finalizing(drc_index
);
146 spapr_drc_detach(drc
);
148 return RTAS_OUT_SUCCESS
;
151 static uint32_t drc_unisolate_logical(SpaprDrc
*drc
)
153 switch (drc
->state
) {
154 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
155 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
156 return RTAS_OUT_SUCCESS
; /* Nothing to do */
157 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
158 break; /* see below */
159 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
160 return RTAS_OUT_NO_SUCH_INDICATOR
; /* not allowed */
162 g_assert_not_reached();
165 /* Move to AVAILABLE state should have ensured device was present */
168 drc
->state
= SPAPR_DRC_STATE_LOGICAL_UNISOLATE
;
169 drc
->ccs_offset
= drc
->fdt_start_offset
;
172 return RTAS_OUT_SUCCESS
;
175 static uint32_t drc_set_usable(SpaprDrc
*drc
)
177 switch (drc
->state
) {
178 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
179 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
180 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
181 return RTAS_OUT_SUCCESS
; /* Nothing to do */
182 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
183 break; /* see below */
185 g_assert_not_reached();
188 /* if there's no resource/device associated with the DRC, there's
189 * no way for us to put it in an allocation state consistent with
190 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
191 * result in an RTAS return code of -3 / "no such indicator"
194 return RTAS_OUT_NO_SUCH_INDICATOR
;
196 if (drc
->unplug_requested
) {
197 /* Don't allow the guest to move a device away from UNUSABLE
198 * state when we want to unplug it */
199 return RTAS_OUT_NO_SUCH_INDICATOR
;
202 drc
->state
= SPAPR_DRC_STATE_LOGICAL_AVAILABLE
;
204 return RTAS_OUT_SUCCESS
;
207 static uint32_t drc_set_unusable(SpaprDrc
*drc
)
209 switch (drc
->state
) {
210 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
211 return RTAS_OUT_SUCCESS
; /* Nothing to do */
212 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
213 break; /* see below */
214 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
215 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
216 return RTAS_OUT_NO_SUCH_INDICATOR
; /* not allowed */
218 g_assert_not_reached();
221 drc
->state
= SPAPR_DRC_STATE_LOGICAL_UNUSABLE
;
222 if (drc
->unplug_requested
) {
223 uint32_t drc_index
= spapr_drc_index(drc
);
224 trace_spapr_drc_set_allocation_state_finalizing(drc_index
);
225 spapr_drc_detach(drc
);
228 return RTAS_OUT_SUCCESS
;
231 static char *spapr_drc_name(SpaprDrc
*drc
)
233 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
235 /* human-readable name for a DRC to encode into the DT
236 * description. this is mainly only used within a guest in place
237 * of the unique DRC index.
239 * in the case of VIO/PCI devices, it corresponds to a "location
240 * code" that maps a logical device/function (DRC index) to a
241 * physical (or virtual in the case of VIO) location in the system
242 * by chaining together the "location label" for each
243 * encapsulating component.
245 * since this is more to do with diagnosing physical hardware
246 * issues than guest compatibility, we choose location codes/DRC
247 * names that adhere to the documented format, but avoid encoding
248 * the entire topology information into the label/code, instead
249 * just using the location codes based on the labels for the
250 * endpoints (VIO/PCI adaptor connectors), which is basically just
251 * "C" followed by an integer ID.
253 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
254 * location codes as documented by PAPR+ v2.7, 12.3.1.5
256 return g_strdup_printf("%s%d", drck
->drc_name_prefix
, drc
->id
);
260 * dr-entity-sense sensor value
261 * returned via get-sensor-state RTAS calls
262 * as expected by state diagram in PAPR+ 2.7, 13.4
263 * based on the current allocation/indicator/power states
264 * for the DR connector.
266 static SpaprDREntitySense
physical_entity_sense(SpaprDrc
*drc
)
268 /* this assumes all PCI devices are assigned to a 'live insertion'
269 * power domain, where QEMU manages power state automatically as
270 * opposed to the guest. present, non-PCI resources are unaffected
273 return drc
->dev
? SPAPR_DR_ENTITY_SENSE_PRESENT
274 : SPAPR_DR_ENTITY_SENSE_EMPTY
;
277 static SpaprDREntitySense
logical_entity_sense(SpaprDrc
*drc
)
279 switch (drc
->state
) {
280 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
281 return SPAPR_DR_ENTITY_SENSE_UNUSABLE
;
282 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
283 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
284 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
286 return SPAPR_DR_ENTITY_SENSE_PRESENT
;
288 g_assert_not_reached();
292 static void prop_get_index(Object
*obj
, Visitor
*v
, const char *name
,
293 void *opaque
, Error
**errp
)
295 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
296 uint32_t value
= spapr_drc_index(drc
);
297 visit_type_uint32(v
, name
, &value
, errp
);
300 static void prop_get_fdt(Object
*obj
, Visitor
*v
, const char *name
,
301 void *opaque
, Error
**errp
)
303 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
306 int fdt_offset_next
, fdt_offset
, fdt_depth
;
310 visit_type_null(v
, NULL
, &null
, errp
);
316 fdt_offset
= drc
->fdt_start_offset
;
320 const char *name
= NULL
;
321 const struct fdt_property
*prop
= NULL
;
322 int prop_len
= 0, name_len
= 0;
325 tag
= fdt_next_tag(fdt
, fdt_offset
, &fdt_offset_next
);
329 name
= fdt_get_name(fdt
, fdt_offset
, &name_len
);
330 visit_start_struct(v
, name
, NULL
, 0, &err
);
332 error_propagate(errp
, err
);
337 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
338 g_assert(fdt_depth
> 0);
339 visit_check_struct(v
, &err
);
340 visit_end_struct(v
, NULL
);
342 error_propagate(errp
, err
);
349 prop
= fdt_get_property_by_offset(fdt
, fdt_offset
, &prop_len
);
350 name
= fdt_string(fdt
, fdt32_to_cpu(prop
->nameoff
));
351 visit_start_list(v
, name
, NULL
, 0, &err
);
353 error_propagate(errp
, err
);
356 for (i
= 0; i
< prop_len
; i
++) {
357 visit_type_uint8(v
, NULL
, (uint8_t *)&prop
->data
[i
], &err
);
359 error_propagate(errp
, err
);
363 visit_check_list(v
, &err
);
364 visit_end_list(v
, NULL
);
366 error_propagate(errp
, err
);
372 error_report("device FDT in unexpected state: %d", tag
);
375 fdt_offset
= fdt_offset_next
;
376 } while (fdt_depth
!= 0);
379 void spapr_drc_attach(SpaprDrc
*drc
, DeviceState
*d
, Error
**errp
)
381 trace_spapr_drc_attach(spapr_drc_index(drc
));
384 error_setg(errp
, "an attached device is still awaiting release");
387 g_assert((drc
->state
== SPAPR_DRC_STATE_LOGICAL_UNUSABLE
)
388 || (drc
->state
== SPAPR_DRC_STATE_PHYSICAL_POWERON
));
392 object_property_add_link(OBJECT(drc
), "device",
393 object_get_typename(OBJECT(drc
->dev
)),
394 (Object
**)(&drc
->dev
),
398 static void spapr_drc_release(SpaprDrc
*drc
)
400 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
402 drck
->release(drc
->dev
);
404 drc
->unplug_requested
= false;
407 drc
->fdt_start_offset
= 0;
408 object_property_del(OBJECT(drc
), "device", &error_abort
);
412 void spapr_drc_detach(SpaprDrc
*drc
)
414 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
416 trace_spapr_drc_detach(spapr_drc_index(drc
));
420 drc
->unplug_requested
= true;
422 if (drc
->state
!= drck
->empty_state
) {
423 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc
));
427 spapr_drc_release(drc
);
430 void spapr_drc_reset(SpaprDrc
*drc
)
432 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
434 trace_spapr_drc_reset(spapr_drc_index(drc
));
436 /* immediately upon reset we can safely assume DRCs whose devices
437 * are pending removal can be safely removed.
439 if (drc
->unplug_requested
) {
440 spapr_drc_release(drc
);
444 /* A device present at reset is ready to go, same as coldplugged */
445 drc
->state
= drck
->ready_state
;
447 * Ensure that we are able to send the FDT fragment again
448 * via configure-connector call if the guest requests.
450 drc
->ccs_offset
= drc
->fdt_start_offset
;
453 drc
->state
= drck
->empty_state
;
454 drc
->ccs_offset
= -1;
459 static bool spapr_drc_unplug_requested_needed(void *opaque
)
461 return spapr_drc_unplug_requested(opaque
);
464 static const VMStateDescription vmstate_spapr_drc_unplug_requested
= {
465 .name
= "spapr_drc/unplug_requested",
467 .minimum_version_id
= 1,
468 .needed
= spapr_drc_unplug_requested_needed
,
469 .fields
= (VMStateField
[]) {
470 VMSTATE_BOOL(unplug_requested
, SpaprDrc
),
471 VMSTATE_END_OF_LIST()
475 bool spapr_drc_transient(SpaprDrc
*drc
)
477 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
480 * If no dev is plugged in there is no need to migrate the DRC state
481 * nor to reset the DRC at CAS.
488 * We need to reset the DRC at CAS or to migrate the DRC state if it's
489 * not equal to the expected long-term state, which is the same as the
490 * coldplugged initial state, or if an unplug request is pending.
492 return drc
->state
!= drck
->ready_state
||
493 spapr_drc_unplug_requested(drc
);
496 static bool spapr_drc_needed(void *opaque
)
498 return spapr_drc_transient(opaque
);
501 static const VMStateDescription vmstate_spapr_drc
= {
504 .minimum_version_id
= 1,
505 .needed
= spapr_drc_needed
,
506 .fields
= (VMStateField
[]) {
507 VMSTATE_UINT32(state
, SpaprDrc
),
508 VMSTATE_END_OF_LIST()
510 .subsections
= (const VMStateDescription
* []) {
511 &vmstate_spapr_drc_unplug_requested
,
516 static void realize(DeviceState
*d
, Error
**errp
)
518 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(d
);
519 Object
*root_container
;
524 trace_spapr_drc_realize(spapr_drc_index(drc
));
525 /* NOTE: we do this as part of realize/unrealize due to the fact
526 * that the guest will communicate with the DRC via RTAS calls
527 * referencing the global DRC index. By unlinking the DRC
528 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
529 * inaccessible by the guest, since lookups rely on this path
530 * existing in the composition tree
532 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
533 link_name
= g_strdup_printf("%x", spapr_drc_index(drc
));
534 child_name
= object_get_canonical_path_component(OBJECT(drc
));
535 trace_spapr_drc_realize_child(spapr_drc_index(drc
), child_name
);
536 object_property_add_alias(root_container
, link_name
,
537 drc
->owner
, child_name
, &err
);
541 error_propagate(errp
, err
);
544 vmstate_register(VMSTATE_IF(drc
), spapr_drc_index(drc
), &vmstate_spapr_drc
,
546 trace_spapr_drc_realize_complete(spapr_drc_index(drc
));
549 static void unrealize(DeviceState
*d
, Error
**errp
)
551 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(d
);
552 Object
*root_container
;
555 trace_spapr_drc_unrealize(spapr_drc_index(drc
));
556 vmstate_unregister(VMSTATE_IF(drc
), &vmstate_spapr_drc
, drc
);
557 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
558 name
= g_strdup_printf("%x", spapr_drc_index(drc
));
559 object_property_del(root_container
, name
, errp
);
563 SpaprDrc
*spapr_dr_connector_new(Object
*owner
, const char *type
,
566 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(object_new(type
));
571 prop_name
= g_strdup_printf("dr-connector[%"PRIu32
"]",
572 spapr_drc_index(drc
));
573 object_property_add_child(owner
, prop_name
, OBJECT(drc
), &error_abort
);
574 object_unref(OBJECT(drc
));
575 object_property_set_bool(OBJECT(drc
), true, "realized", NULL
);
581 static void spapr_dr_connector_instance_init(Object
*obj
)
583 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
584 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
586 object_property_add_uint32_ptr(obj
, "id", &drc
->id
, NULL
);
587 object_property_add(obj
, "index", "uint32", prop_get_index
,
588 NULL
, NULL
, NULL
, NULL
);
589 object_property_add(obj
, "fdt", "struct", prop_get_fdt
,
590 NULL
, NULL
, NULL
, NULL
);
591 drc
->state
= drck
->empty_state
;
594 static void spapr_dr_connector_class_init(ObjectClass
*k
, void *data
)
596 DeviceClass
*dk
= DEVICE_CLASS(k
);
598 dk
->realize
= realize
;
599 dk
->unrealize
= unrealize
;
601 * Reason: it crashes FIXME find and document the real reason
603 dk
->user_creatable
= false;
606 static bool drc_physical_needed(void *opaque
)
608 SpaprDrcPhysical
*drcp
= (SpaprDrcPhysical
*)opaque
;
609 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(drcp
);
611 if ((drc
->dev
&& (drcp
->dr_indicator
== SPAPR_DR_INDICATOR_ACTIVE
))
612 || (!drc
->dev
&& (drcp
->dr_indicator
== SPAPR_DR_INDICATOR_INACTIVE
))) {
618 static const VMStateDescription vmstate_spapr_drc_physical
= {
619 .name
= "spapr_drc/physical",
621 .minimum_version_id
= 1,
622 .needed
= drc_physical_needed
,
623 .fields
= (VMStateField
[]) {
624 VMSTATE_UINT32(dr_indicator
, SpaprDrcPhysical
),
625 VMSTATE_END_OF_LIST()
629 static void drc_physical_reset(void *opaque
)
631 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(opaque
);
632 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(drc
);
635 drcp
->dr_indicator
= SPAPR_DR_INDICATOR_ACTIVE
;
637 drcp
->dr_indicator
= SPAPR_DR_INDICATOR_INACTIVE
;
641 static void realize_physical(DeviceState
*d
, Error
**errp
)
643 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(d
);
644 Error
*local_err
= NULL
;
646 realize(d
, &local_err
);
648 error_propagate(errp
, local_err
);
652 vmstate_register(VMSTATE_IF(drcp
),
653 spapr_drc_index(SPAPR_DR_CONNECTOR(drcp
)),
654 &vmstate_spapr_drc_physical
, drcp
);
655 qemu_register_reset(drc_physical_reset
, drcp
);
658 static void unrealize_physical(DeviceState
*d
, Error
**errp
)
660 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(d
);
661 Error
*local_err
= NULL
;
663 unrealize(d
, &local_err
);
665 error_propagate(errp
, local_err
);
669 vmstate_unregister(VMSTATE_IF(drcp
), &vmstate_spapr_drc_physical
, drcp
);
670 qemu_unregister_reset(drc_physical_reset
, drcp
);
673 static void spapr_drc_physical_class_init(ObjectClass
*k
, void *data
)
675 DeviceClass
*dk
= DEVICE_CLASS(k
);
676 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
678 dk
->realize
= realize_physical
;
679 dk
->unrealize
= unrealize_physical
;
680 drck
->dr_entity_sense
= physical_entity_sense
;
681 drck
->isolate
= drc_isolate_physical
;
682 drck
->unisolate
= drc_unisolate_physical
;
683 drck
->ready_state
= SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
;
684 drck
->empty_state
= SPAPR_DRC_STATE_PHYSICAL_POWERON
;
687 static void spapr_drc_logical_class_init(ObjectClass
*k
, void *data
)
689 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
691 drck
->dr_entity_sense
= logical_entity_sense
;
692 drck
->isolate
= drc_isolate_logical
;
693 drck
->unisolate
= drc_unisolate_logical
;
694 drck
->ready_state
= SPAPR_DRC_STATE_LOGICAL_CONFIGURED
;
695 drck
->empty_state
= SPAPR_DRC_STATE_LOGICAL_UNUSABLE
;
698 static void spapr_drc_cpu_class_init(ObjectClass
*k
, void *data
)
700 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
702 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU
;
703 drck
->typename
= "CPU";
704 drck
->drc_name_prefix
= "CPU ";
705 drck
->release
= spapr_core_release
;
706 drck
->dt_populate
= spapr_core_dt_populate
;
709 static void spapr_drc_pci_class_init(ObjectClass
*k
, void *data
)
711 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
713 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI
;
714 drck
->typename
= "28";
715 drck
->drc_name_prefix
= "C";
716 drck
->release
= spapr_phb_remove_pci_device_cb
;
717 drck
->dt_populate
= spapr_pci_dt_populate
;
720 static void spapr_drc_lmb_class_init(ObjectClass
*k
, void *data
)
722 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
724 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB
;
725 drck
->typename
= "MEM";
726 drck
->drc_name_prefix
= "LMB ";
727 drck
->release
= spapr_lmb_release
;
728 drck
->dt_populate
= spapr_lmb_dt_populate
;
731 static void spapr_drc_phb_class_init(ObjectClass
*k
, void *data
)
733 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
735 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB
;
736 drck
->typename
= "PHB";
737 drck
->drc_name_prefix
= "PHB ";
738 drck
->release
= spapr_phb_release
;
739 drck
->dt_populate
= spapr_phb_dt_populate
;
742 static void spapr_drc_pmem_class_init(ObjectClass
*k
, void *data
)
744 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
746 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM
;
747 drck
->typename
= "PMEM";
748 drck
->drc_name_prefix
= "PMEM ";
749 drck
->release
= NULL
;
750 drck
->dt_populate
= spapr_pmem_dt_populate
;
753 static const TypeInfo spapr_dr_connector_info
= {
754 .name
= TYPE_SPAPR_DR_CONNECTOR
,
755 .parent
= TYPE_DEVICE
,
756 .instance_size
= sizeof(SpaprDrc
),
757 .instance_init
= spapr_dr_connector_instance_init
,
758 .class_size
= sizeof(SpaprDrcClass
),
759 .class_init
= spapr_dr_connector_class_init
,
763 static const TypeInfo spapr_drc_physical_info
= {
764 .name
= TYPE_SPAPR_DRC_PHYSICAL
,
765 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
766 .instance_size
= sizeof(SpaprDrcPhysical
),
767 .class_init
= spapr_drc_physical_class_init
,
771 static const TypeInfo spapr_drc_logical_info
= {
772 .name
= TYPE_SPAPR_DRC_LOGICAL
,
773 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
774 .class_init
= spapr_drc_logical_class_init
,
778 static const TypeInfo spapr_drc_cpu_info
= {
779 .name
= TYPE_SPAPR_DRC_CPU
,
780 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
781 .class_init
= spapr_drc_cpu_class_init
,
784 static const TypeInfo spapr_drc_pci_info
= {
785 .name
= TYPE_SPAPR_DRC_PCI
,
786 .parent
= TYPE_SPAPR_DRC_PHYSICAL
,
787 .class_init
= spapr_drc_pci_class_init
,
790 static const TypeInfo spapr_drc_lmb_info
= {
791 .name
= TYPE_SPAPR_DRC_LMB
,
792 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
793 .class_init
= spapr_drc_lmb_class_init
,
796 static const TypeInfo spapr_drc_phb_info
= {
797 .name
= TYPE_SPAPR_DRC_PHB
,
798 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
799 .instance_size
= sizeof(SpaprDrc
),
800 .class_init
= spapr_drc_phb_class_init
,
803 static const TypeInfo spapr_drc_pmem_info
= {
804 .name
= TYPE_SPAPR_DRC_PMEM
,
805 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
806 .class_init
= spapr_drc_pmem_class_init
,
809 /* helper functions for external users */
811 SpaprDrc
*spapr_drc_by_index(uint32_t index
)
816 name
= g_strdup_printf("%s/%x", DRC_CONTAINER_PATH
, index
);
817 obj
= object_resolve_path(name
, NULL
);
820 return !obj
? NULL
: SPAPR_DR_CONNECTOR(obj
);
823 SpaprDrc
*spapr_drc_by_id(const char *type
, uint32_t id
)
826 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type
));
828 return spapr_drc_by_index(drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
829 | (id
& DRC_INDEX_ID_MASK
));
835 * @fdt: libfdt device tree
836 * @path: path in the DT to generate properties
837 * @owner: parent Object/DeviceState for which to generate DRC
839 * @drc_type_mask: mask of SpaprDrcType values corresponding
840 * to the types of DRCs to generate entries for
842 * generate OF properties to describe DRC topology/indices to guests
844 * as documented in PAPR+ v2.1, 13.5.2
846 int spapr_dt_drc(void *fdt
, int offset
, Object
*owner
, uint32_t drc_type_mask
)
848 Object
*root_container
;
849 ObjectProperty
*prop
;
850 ObjectPropertyIterator iter
;
851 uint32_t drc_count
= 0;
852 GArray
*drc_indexes
, *drc_power_domains
;
853 GString
*drc_names
, *drc_types
;
856 /* the first entry of each properties is a 32-bit integer encoding
857 * the number of elements in the array. we won't know this until
858 * we complete the iteration through all the matching DRCs, but
859 * reserve the space now and set the offsets accordingly so we
860 * can fill them in later.
862 drc_indexes
= g_array_new(false, true, sizeof(uint32_t));
863 drc_indexes
= g_array_set_size(drc_indexes
, 1);
864 drc_power_domains
= g_array_new(false, true, sizeof(uint32_t));
865 drc_power_domains
= g_array_set_size(drc_power_domains
, 1);
866 drc_names
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
867 drc_types
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
869 /* aliases for all DRConnector objects will be rooted in QOM
870 * composition tree at DRC_CONTAINER_PATH
872 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
874 object_property_iter_init(&iter
, root_container
);
875 while ((prop
= object_property_iter_next(&iter
))) {
879 char *drc_name
= NULL
;
880 uint32_t drc_index
, drc_power_domain
;
882 if (!strstart(prop
->type
, "link<", NULL
)) {
886 obj
= object_property_get_link(root_container
, prop
->name
, NULL
);
887 drc
= SPAPR_DR_CONNECTOR(obj
);
888 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
890 if (owner
&& (drc
->owner
!= owner
)) {
894 if ((spapr_drc_type(drc
) & drc_type_mask
) == 0) {
900 /* ibm,drc-indexes */
901 drc_index
= cpu_to_be32(spapr_drc_index(drc
));
902 g_array_append_val(drc_indexes
, drc_index
);
904 /* ibm,drc-power-domains */
905 drc_power_domain
= cpu_to_be32(-1);
906 g_array_append_val(drc_power_domains
, drc_power_domain
);
909 drc_name
= spapr_drc_name(drc
);
910 drc_names
= g_string_append(drc_names
, drc_name
);
911 drc_names
= g_string_insert_len(drc_names
, -1, "\0", 1);
915 drc_types
= g_string_append(drc_types
, drck
->typename
);
916 drc_types
= g_string_insert_len(drc_types
, -1, "\0", 1);
919 /* now write the drc count into the space we reserved at the
920 * beginning of the arrays previously
922 *(uint32_t *)drc_indexes
->data
= cpu_to_be32(drc_count
);
923 *(uint32_t *)drc_power_domains
->data
= cpu_to_be32(drc_count
);
924 *(uint32_t *)drc_names
->str
= cpu_to_be32(drc_count
);
925 *(uint32_t *)drc_types
->str
= cpu_to_be32(drc_count
);
927 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-indexes",
929 drc_indexes
->len
* sizeof(uint32_t));
931 error_report("Couldn't create ibm,drc-indexes property");
935 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-power-domains",
936 drc_power_domains
->data
,
937 drc_power_domains
->len
* sizeof(uint32_t));
939 error_report("Couldn't finalize ibm,drc-power-domains property");
943 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-names",
944 drc_names
->str
, drc_names
->len
);
946 error_report("Couldn't finalize ibm,drc-names property");
950 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-types",
951 drc_types
->str
, drc_types
->len
);
953 error_report("Couldn't finalize ibm,drc-types property");
958 g_array_free(drc_indexes
, true);
959 g_array_free(drc_power_domains
, true);
960 g_string_free(drc_names
, true);
961 g_string_free(drc_types
, true);
970 static uint32_t rtas_set_isolation_state(uint32_t idx
, uint32_t state
)
972 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
976 return RTAS_OUT_NO_SUCH_INDICATOR
;
979 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc
), state
);
981 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
984 case SPAPR_DR_ISOLATION_STATE_ISOLATED
:
985 return drck
->isolate(drc
);
987 case SPAPR_DR_ISOLATION_STATE_UNISOLATED
:
988 return drck
->unisolate(drc
);
991 return RTAS_OUT_PARAM_ERROR
;
995 static uint32_t rtas_set_allocation_state(uint32_t idx
, uint32_t state
)
997 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
999 if (!drc
|| !object_dynamic_cast(OBJECT(drc
), TYPE_SPAPR_DRC_LOGICAL
)) {
1000 return RTAS_OUT_NO_SUCH_INDICATOR
;
1003 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc
), state
);
1006 case SPAPR_DR_ALLOCATION_STATE_USABLE
:
1007 return drc_set_usable(drc
);
1009 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE
:
1010 return drc_set_unusable(drc
);
1013 return RTAS_OUT_PARAM_ERROR
;
1017 static uint32_t rtas_set_dr_indicator(uint32_t idx
, uint32_t state
)
1019 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
1021 if (!drc
|| !object_dynamic_cast(OBJECT(drc
), TYPE_SPAPR_DRC_PHYSICAL
)) {
1022 return RTAS_OUT_NO_SUCH_INDICATOR
;
1024 if ((state
!= SPAPR_DR_INDICATOR_INACTIVE
)
1025 && (state
!= SPAPR_DR_INDICATOR_ACTIVE
)
1026 && (state
!= SPAPR_DR_INDICATOR_IDENTIFY
)
1027 && (state
!= SPAPR_DR_INDICATOR_ACTION
)) {
1028 return RTAS_OUT_PARAM_ERROR
; /* bad state parameter */
1031 trace_spapr_drc_set_dr_indicator(idx
, state
);
1032 SPAPR_DRC_PHYSICAL(drc
)->dr_indicator
= state
;
1033 return RTAS_OUT_SUCCESS
;
1036 static void rtas_set_indicator(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
1038 uint32_t nargs
, target_ulong args
,
1039 uint32_t nret
, target_ulong rets
)
1041 uint32_t type
, idx
, state
;
1042 uint32_t ret
= RTAS_OUT_SUCCESS
;
1044 if (nargs
!= 3 || nret
!= 1) {
1045 ret
= RTAS_OUT_PARAM_ERROR
;
1049 type
= rtas_ld(args
, 0);
1050 idx
= rtas_ld(args
, 1);
1051 state
= rtas_ld(args
, 2);
1054 case RTAS_SENSOR_TYPE_ISOLATION_STATE
:
1055 ret
= rtas_set_isolation_state(idx
, state
);
1057 case RTAS_SENSOR_TYPE_DR
:
1058 ret
= rtas_set_dr_indicator(idx
, state
);
1060 case RTAS_SENSOR_TYPE_ALLOCATION_STATE
:
1061 ret
= rtas_set_allocation_state(idx
, state
);
1064 ret
= RTAS_OUT_NOT_SUPPORTED
;
1068 rtas_st(rets
, 0, ret
);
1071 static void rtas_get_sensor_state(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
1072 uint32_t token
, uint32_t nargs
,
1073 target_ulong args
, uint32_t nret
,
1076 uint32_t sensor_type
;
1077 uint32_t sensor_index
;
1078 uint32_t sensor_state
= 0;
1080 SpaprDrcClass
*drck
;
1081 uint32_t ret
= RTAS_OUT_SUCCESS
;
1083 if (nargs
!= 2 || nret
!= 2) {
1084 ret
= RTAS_OUT_PARAM_ERROR
;
1088 sensor_type
= rtas_ld(args
, 0);
1089 sensor_index
= rtas_ld(args
, 1);
1091 if (sensor_type
!= RTAS_SENSOR_TYPE_ENTITY_SENSE
) {
1092 /* currently only DR-related sensors are implemented */
1093 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index
,
1095 ret
= RTAS_OUT_NOT_SUPPORTED
;
1099 drc
= spapr_drc_by_index(sensor_index
);
1101 trace_spapr_rtas_get_sensor_state_invalid(sensor_index
);
1102 ret
= RTAS_OUT_PARAM_ERROR
;
1105 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1106 sensor_state
= drck
->dr_entity_sense(drc
);
1109 rtas_st(rets
, 0, ret
);
1110 rtas_st(rets
, 1, sensor_state
);
1113 /* configure-connector work area offsets, int32_t units for field
1114 * indexes, bytes for field offset/len values.
1116 * as documented by PAPR+ v2.7, 13.5.3.5
1118 #define CC_IDX_NODE_NAME_OFFSET 2
1119 #define CC_IDX_PROP_NAME_OFFSET 2
1120 #define CC_IDX_PROP_LEN 3
1121 #define CC_IDX_PROP_DATA_OFFSET 4
1122 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1123 #define CC_WA_LEN 4096
1125 static void configure_connector_st(target_ulong addr
, target_ulong offset
,
1126 const void *buf
, size_t len
)
1128 cpu_physical_memory_write(ppc64_phys_to_real(addr
+ offset
),
1129 buf
, MIN(len
, CC_WA_LEN
- offset
));
1132 static void rtas_ibm_configure_connector(PowerPCCPU
*cpu
,
1133 SpaprMachineState
*spapr
,
1134 uint32_t token
, uint32_t nargs
,
1135 target_ulong args
, uint32_t nret
,
1142 SpaprDrcClass
*drck
;
1143 SpaprDRCCResponse resp
= SPAPR_DR_CC_RESPONSE_CONTINUE
;
1146 if (nargs
!= 2 || nret
!= 1) {
1147 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
1151 wa_addr
= ((uint64_t)rtas_ld(args
, 1) << 32) | rtas_ld(args
, 0);
1153 drc_index
= rtas_ld(wa_addr
, 0);
1154 drc
= spapr_drc_by_index(drc_index
);
1156 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index
);
1157 rc
= RTAS_OUT_PARAM_ERROR
;
1161 if ((drc
->state
!= SPAPR_DRC_STATE_LOGICAL_UNISOLATE
)
1162 && (drc
->state
!= SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
)
1163 && (drc
->state
!= SPAPR_DRC_STATE_LOGICAL_CONFIGURED
)
1164 && (drc
->state
!= SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
)) {
1166 * Need to unisolate the device before configuring
1167 * or it should already be in configured state to
1168 * allow configure-connector be called repeatedly.
1170 rc
= SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE
;
1174 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1177 Error
*local_err
= NULL
;
1181 fdt
= create_device_tree(&fdt_size
);
1183 if (drck
->dt_populate(drc
, spapr
, fdt
, &drc
->fdt_start_offset
,
1186 error_free(local_err
);
1187 rc
= SPAPR_DR_CC_RESPONSE_ERROR
;
1192 drc
->ccs_offset
= drc
->fdt_start_offset
;
1199 const struct fdt_property
*prop
;
1200 int fdt_offset_next
, prop_len
;
1202 tag
= fdt_next_tag(drc
->fdt
, drc
->ccs_offset
, &fdt_offset_next
);
1205 case FDT_BEGIN_NODE
:
1207 name
= fdt_get_name(drc
->fdt
, drc
->ccs_offset
, NULL
);
1209 /* provide the name of the next OF node */
1210 wa_offset
= CC_VAL_DATA_OFFSET
;
1211 rtas_st(wa_addr
, CC_IDX_NODE_NAME_OFFSET
, wa_offset
);
1212 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1213 resp
= SPAPR_DR_CC_RESPONSE_NEXT_CHILD
;
1217 if (drc
->ccs_depth
== 0) {
1218 uint32_t drc_index
= spapr_drc_index(drc
);
1220 /* done sending the device tree, move to configured state */
1221 trace_spapr_drc_set_configured(drc_index
);
1222 drc
->state
= drck
->ready_state
;
1224 * Ensure that we are able to send the FDT fragment
1225 * again via configure-connector call if the guest requests.
1227 drc
->ccs_offset
= drc
->fdt_start_offset
;
1229 fdt_offset_next
= drc
->fdt_start_offset
;
1230 resp
= SPAPR_DR_CC_RESPONSE_SUCCESS
;
1232 resp
= SPAPR_DR_CC_RESPONSE_PREV_PARENT
;
1236 prop
= fdt_get_property_by_offset(drc
->fdt
, drc
->ccs_offset
,
1238 name
= fdt_string(drc
->fdt
, fdt32_to_cpu(prop
->nameoff
));
1240 /* provide the name of the next OF property */
1241 wa_offset
= CC_VAL_DATA_OFFSET
;
1242 rtas_st(wa_addr
, CC_IDX_PROP_NAME_OFFSET
, wa_offset
);
1243 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1245 /* provide the length and value of the OF property. data gets
1246 * placed immediately after NULL terminator of the OF property's
1249 wa_offset
+= strlen(name
) + 1,
1250 rtas_st(wa_addr
, CC_IDX_PROP_LEN
, prop_len
);
1251 rtas_st(wa_addr
, CC_IDX_PROP_DATA_OFFSET
, wa_offset
);
1252 configure_connector_st(wa_addr
, wa_offset
, prop
->data
, prop_len
);
1253 resp
= SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY
;
1256 resp
= SPAPR_DR_CC_RESPONSE_ERROR
;
1258 /* keep seeking for an actionable tag */
1261 if (drc
->ccs_offset
>= 0) {
1262 drc
->ccs_offset
= fdt_offset_next
;
1264 } while (resp
== SPAPR_DR_CC_RESPONSE_CONTINUE
);
1268 rtas_st(rets
, 0, rc
);
1271 static void spapr_drc_register_types(void)
1273 type_register_static(&spapr_dr_connector_info
);
1274 type_register_static(&spapr_drc_physical_info
);
1275 type_register_static(&spapr_drc_logical_info
);
1276 type_register_static(&spapr_drc_cpu_info
);
1277 type_register_static(&spapr_drc_pci_info
);
1278 type_register_static(&spapr_drc_lmb_info
);
1279 type_register_static(&spapr_drc_phb_info
);
1280 type_register_static(&spapr_drc_pmem_info
);
1282 spapr_rtas_register(RTAS_SET_INDICATOR
, "set-indicator",
1283 rtas_set_indicator
);
1284 spapr_rtas_register(RTAS_GET_SENSOR_STATE
, "get-sensor-state",
1285 rtas_get_sensor_state
);
1286 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR
, "ibm,configure-connector",
1287 rtas_ibm_configure_connector
);
1289 type_init(spapr_drc_register_types
)