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"
16 #include "qemu/cutils.h"
17 #include "hw/ppc/spapr_drc.h"
18 #include "qom/object.h"
19 #include "migration/vmstate.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-events-qdev.h"
22 #include "qapi/visitor.h"
23 #include "qemu/error-report.h"
24 #include "hw/ppc/spapr.h" /* for RTAS return codes */
25 #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
26 #include "hw/ppc/spapr_nvdimm.h"
27 #include "sysemu/device_tree.h"
28 #include "sysemu/reset.h"
31 #define DRC_CONTAINER_PATH "/dr-connector"
32 #define DRC_INDEX_TYPE_SHIFT 28
33 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
35 SpaprDrcType
spapr_drc_type(SpaprDrc
*drc
)
37 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
39 return 1 << drck
->typeshift
;
42 uint32_t spapr_drc_index(SpaprDrc
*drc
)
44 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
46 /* no set format for a drc index: it only needs to be globally
47 * unique. this is how we encode the DRC type on bare-metal
48 * however, so might as well do that here
50 return (drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
)
51 | (drc
->id
& DRC_INDEX_ID_MASK
);
54 static void spapr_drc_release(SpaprDrc
*drc
)
56 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
58 drck
->release(drc
->dev
);
60 drc
->unplug_requested
= false;
63 drc
->fdt_start_offset
= 0;
64 object_property_del(OBJECT(drc
), "device");
68 static uint32_t drc_isolate_physical(SpaprDrc
*drc
)
71 case SPAPR_DRC_STATE_PHYSICAL_POWERON
:
72 return RTAS_OUT_SUCCESS
; /* Nothing to do */
73 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
:
74 break; /* see below */
75 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
:
76 return RTAS_OUT_PARAM_ERROR
; /* not allowed */
78 g_assert_not_reached();
81 drc
->state
= SPAPR_DRC_STATE_PHYSICAL_POWERON
;
83 if (drc
->unplug_requested
) {
84 uint32_t drc_index
= spapr_drc_index(drc
);
85 trace_spapr_drc_set_isolation_state_finalizing(drc_index
);
86 spapr_drc_release(drc
);
89 return RTAS_OUT_SUCCESS
;
92 static uint32_t drc_unisolate_physical(SpaprDrc
*drc
)
95 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
:
96 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
:
97 return RTAS_OUT_SUCCESS
; /* Nothing to do */
98 case SPAPR_DRC_STATE_PHYSICAL_POWERON
:
99 break; /* see below */
101 g_assert_not_reached();
104 /* cannot unisolate a non-existent resource, and, or resources
105 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
109 return RTAS_OUT_NO_SUCH_INDICATOR
;
112 drc
->state
= SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
;
113 drc
->ccs_offset
= drc
->fdt_start_offset
;
116 return RTAS_OUT_SUCCESS
;
119 static uint32_t drc_isolate_logical(SpaprDrc
*drc
)
121 switch (drc
->state
) {
122 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
123 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
124 return RTAS_OUT_SUCCESS
; /* Nothing to do */
125 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
126 break; /* see below */
127 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
128 return RTAS_OUT_PARAM_ERROR
; /* not allowed */
130 g_assert_not_reached();
134 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
135 * belong to a DIMM device that is marked for removal.
137 * Currently the guest userspace tool drmgr that drives the memory
138 * hotplug/unplug will just try to remove a set of 'removable' LMBs
139 * in response to a hot unplug request that is based on drc-count.
140 * If the LMB being removed doesn't belong to a DIMM device that is
141 * actually being unplugged, fail the isolation request here.
143 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_LMB
144 && !drc
->unplug_requested
) {
145 return RTAS_OUT_HW_ERROR
;
148 drc
->state
= SPAPR_DRC_STATE_LOGICAL_AVAILABLE
;
150 return RTAS_OUT_SUCCESS
;
153 static uint32_t drc_unisolate_logical(SpaprDrc
*drc
)
155 SpaprMachineState
*spapr
= NULL
;
157 switch (drc
->state
) {
158 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
159 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
161 * Unisolating a logical DRC that was marked for unplug
162 * means that the kernel is refusing the removal.
164 if (drc
->unplug_requested
&& drc
->dev
) {
165 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_LMB
) {
166 spapr
= SPAPR_MACHINE(qdev_get_machine());
168 spapr_memory_unplug_rollback(spapr
, drc
->dev
);
171 drc
->unplug_requested
= false;
174 error_report("Device hotunplug rejected by the guest "
175 "for device %s", drc
->dev
->id
);
178 qapi_event_send_device_unplug_guest_error(!!drc
->dev
->id
,
180 drc
->dev
->canonical_path
);
183 return RTAS_OUT_SUCCESS
; /* Nothing to do */
184 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
185 break; /* see below */
186 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
187 return RTAS_OUT_NO_SUCH_INDICATOR
; /* not allowed */
189 g_assert_not_reached();
192 /* Move to AVAILABLE state should have ensured device was present */
195 drc
->state
= SPAPR_DRC_STATE_LOGICAL_UNISOLATE
;
196 drc
->ccs_offset
= drc
->fdt_start_offset
;
199 return RTAS_OUT_SUCCESS
;
202 static uint32_t drc_set_usable(SpaprDrc
*drc
)
204 switch (drc
->state
) {
205 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
206 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
207 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
208 return RTAS_OUT_SUCCESS
; /* Nothing to do */
209 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
210 break; /* see below */
212 g_assert_not_reached();
215 /* if there's no resource/device associated with the DRC, there's
216 * no way for us to put it in an allocation state consistent with
217 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
218 * result in an RTAS return code of -3 / "no such indicator"
221 return RTAS_OUT_NO_SUCH_INDICATOR
;
223 if (drc
->unplug_requested
) {
224 /* Don't allow the guest to move a device away from UNUSABLE
225 * state when we want to unplug it */
226 return RTAS_OUT_NO_SUCH_INDICATOR
;
229 drc
->state
= SPAPR_DRC_STATE_LOGICAL_AVAILABLE
;
231 return RTAS_OUT_SUCCESS
;
234 static uint32_t drc_set_unusable(SpaprDrc
*drc
)
236 switch (drc
->state
) {
237 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
238 return RTAS_OUT_SUCCESS
; /* Nothing to do */
239 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
240 break; /* see below */
241 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
242 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
243 return RTAS_OUT_NO_SUCH_INDICATOR
; /* not allowed */
245 g_assert_not_reached();
248 drc
->state
= SPAPR_DRC_STATE_LOGICAL_UNUSABLE
;
249 if (drc
->unplug_requested
) {
250 uint32_t drc_index
= spapr_drc_index(drc
);
251 trace_spapr_drc_set_allocation_state_finalizing(drc_index
);
252 spapr_drc_release(drc
);
255 return RTAS_OUT_SUCCESS
;
258 static char *spapr_drc_name(SpaprDrc
*drc
)
260 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
262 /* human-readable name for a DRC to encode into the DT
263 * description. this is mainly only used within a guest in place
264 * of the unique DRC index.
266 * in the case of VIO/PCI devices, it corresponds to a "location
267 * code" that maps a logical device/function (DRC index) to a
268 * physical (or virtual in the case of VIO) location in the system
269 * by chaining together the "location label" for each
270 * encapsulating component.
272 * since this is more to do with diagnosing physical hardware
273 * issues than guest compatibility, we choose location codes/DRC
274 * names that adhere to the documented format, but avoid encoding
275 * the entire topology information into the label/code, instead
276 * just using the location codes based on the labels for the
277 * endpoints (VIO/PCI adaptor connectors), which is basically just
278 * "C" followed by an integer ID.
280 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
281 * location codes as documented by PAPR+ v2.7, 12.3.1.5
283 return g_strdup_printf("%s%d", drck
->drc_name_prefix
, drc
->id
);
287 * dr-entity-sense sensor value
288 * returned via get-sensor-state RTAS calls
289 * as expected by state diagram in PAPR+ 2.7, 13.4
290 * based on the current allocation/indicator/power states
291 * for the DR connector.
293 static SpaprDREntitySense
physical_entity_sense(SpaprDrc
*drc
)
295 /* this assumes all PCI devices are assigned to a 'live insertion'
296 * power domain, where QEMU manages power state automatically as
297 * opposed to the guest. present, non-PCI resources are unaffected
300 return drc
->dev
? SPAPR_DR_ENTITY_SENSE_PRESENT
301 : SPAPR_DR_ENTITY_SENSE_EMPTY
;
304 static SpaprDREntitySense
logical_entity_sense(SpaprDrc
*drc
)
306 switch (drc
->state
) {
307 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
308 return SPAPR_DR_ENTITY_SENSE_UNUSABLE
;
309 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
310 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
311 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
313 return SPAPR_DR_ENTITY_SENSE_PRESENT
;
315 g_assert_not_reached();
319 static void prop_get_index(Object
*obj
, Visitor
*v
, const char *name
,
320 void *opaque
, Error
**errp
)
322 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
323 uint32_t value
= spapr_drc_index(drc
);
324 visit_type_uint32(v
, name
, &value
, errp
);
327 static void prop_get_fdt(Object
*obj
, Visitor
*v
, const char *name
,
328 void *opaque
, Error
**errp
)
330 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
332 int fdt_offset_next
, fdt_offset
, fdt_depth
;
336 visit_type_null(v
, NULL
, &null
, errp
);
342 fdt_offset
= drc
->fdt_start_offset
;
346 const char *name
= NULL
;
347 const struct fdt_property
*prop
= NULL
;
348 int prop_len
= 0, name_len
= 0;
352 tag
= fdt_next_tag(fdt
, fdt_offset
, &fdt_offset_next
);
356 name
= fdt_get_name(fdt
, fdt_offset
, &name_len
);
357 if (!visit_start_struct(v
, name
, NULL
, 0, errp
)) {
362 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
363 g_assert(fdt_depth
> 0);
364 ok
= visit_check_struct(v
, errp
);
365 visit_end_struct(v
, NULL
);
373 prop
= fdt_get_property_by_offset(fdt
, fdt_offset
, &prop_len
);
374 name
= fdt_string(fdt
, fdt32_to_cpu(prop
->nameoff
));
375 if (!visit_start_list(v
, name
, NULL
, 0, errp
)) {
378 for (i
= 0; i
< prop_len
; i
++) {
379 if (!visit_type_uint8(v
, NULL
, (uint8_t *)&prop
->data
[i
],
384 ok
= visit_check_list(v
, errp
);
385 visit_end_list(v
, NULL
);
392 error_report("device FDT in unexpected state: %d", tag
);
395 fdt_offset
= fdt_offset_next
;
396 } while (fdt_depth
!= 0);
399 void spapr_drc_attach(SpaprDrc
*drc
, DeviceState
*d
)
401 trace_spapr_drc_attach(spapr_drc_index(drc
));
404 g_assert((drc
->state
== SPAPR_DRC_STATE_LOGICAL_UNUSABLE
)
405 || (drc
->state
== SPAPR_DRC_STATE_PHYSICAL_POWERON
));
409 object_property_add_link(OBJECT(drc
), "device",
410 object_get_typename(OBJECT(drc
->dev
)),
411 (Object
**)(&drc
->dev
),
415 void spapr_drc_unplug_request(SpaprDrc
*drc
)
417 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
419 trace_spapr_drc_unplug_request(spapr_drc_index(drc
));
423 drc
->unplug_requested
= true;
425 if (drc
->state
!= drck
->empty_state
) {
426 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc
));
430 spapr_drc_release(drc
);
433 bool spapr_drc_reset(SpaprDrc
*drc
)
435 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
436 bool unplug_completed
= false;
438 trace_spapr_drc_reset(spapr_drc_index(drc
));
440 /* immediately upon reset we can safely assume DRCs whose devices
441 * are pending removal can be safely removed.
443 if (drc
->unplug_requested
) {
444 spapr_drc_release(drc
);
445 unplug_completed
= true;
449 /* A device present at reset is ready to go, same as coldplugged */
450 drc
->state
= drck
->ready_state
;
452 * Ensure that we are able to send the FDT fragment again
453 * via configure-connector call if the guest requests.
455 drc
->ccs_offset
= drc
->fdt_start_offset
;
458 drc
->state
= drck
->empty_state
;
459 drc
->ccs_offset
= -1;
463 return unplug_completed
;
466 static bool spapr_drc_unplug_requested_needed(void *opaque
)
468 return spapr_drc_unplug_requested(opaque
);
471 static const VMStateDescription vmstate_spapr_drc_unplug_requested
= {
472 .name
= "spapr_drc/unplug_requested",
474 .minimum_version_id
= 1,
475 .needed
= spapr_drc_unplug_requested_needed
,
476 .fields
= (VMStateField
[]) {
477 VMSTATE_BOOL(unplug_requested
, SpaprDrc
),
478 VMSTATE_END_OF_LIST()
482 static bool spapr_drc_needed(void *opaque
)
484 SpaprDrc
*drc
= opaque
;
485 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
488 * If no dev is plugged in there is no need to migrate the DRC state
489 * nor to reset the DRC at CAS.
496 * We need to reset the DRC at CAS or to migrate the DRC state if it's
497 * not equal to the expected long-term state, which is the same as the
498 * coldplugged initial state, or if an unplug request is pending.
500 return drc
->state
!= drck
->ready_state
||
501 spapr_drc_unplug_requested(drc
);
504 static const VMStateDescription vmstate_spapr_drc
= {
507 .minimum_version_id
= 1,
508 .needed
= spapr_drc_needed
,
509 .fields
= (VMStateField
[]) {
510 VMSTATE_UINT32(state
, SpaprDrc
),
511 VMSTATE_END_OF_LIST()
513 .subsections
= (const VMStateDescription
* []) {
514 &vmstate_spapr_drc_unplug_requested
,
519 static void drc_realize(DeviceState
*d
, Error
**errp
)
521 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(d
);
522 g_autofree gchar
*link_name
= g_strdup_printf("%x", spapr_drc_index(drc
));
523 Object
*root_container
;
524 const char *child_name
;
526 trace_spapr_drc_realize(spapr_drc_index(drc
));
527 /* NOTE: we do this as part of realize/unrealize due to the fact
528 * that the guest will communicate with the DRC via RTAS calls
529 * referencing the global DRC index. By unlinking the DRC
530 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
531 * inaccessible by the guest, since lookups rely on this path
532 * existing in the composition tree
534 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
535 child_name
= object_get_canonical_path_component(OBJECT(drc
));
536 trace_spapr_drc_realize_child(spapr_drc_index(drc
), child_name
);
537 object_property_add_alias(root_container
, link_name
,
538 drc
->owner
, child_name
);
539 vmstate_register(VMSTATE_IF(drc
), spapr_drc_index(drc
), &vmstate_spapr_drc
,
541 trace_spapr_drc_realize_complete(spapr_drc_index(drc
));
544 static void drc_unrealize(DeviceState
*d
)
546 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(d
);
547 g_autofree gchar
*name
= g_strdup_printf("%x", spapr_drc_index(drc
));
548 Object
*root_container
;
550 trace_spapr_drc_unrealize(spapr_drc_index(drc
));
551 vmstate_unregister(VMSTATE_IF(drc
), &vmstate_spapr_drc
, drc
);
552 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
553 object_property_del(root_container
, name
);
556 SpaprDrc
*spapr_dr_connector_new(Object
*owner
, const char *type
,
559 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(object_new(type
));
560 g_autofree
char *prop_name
= NULL
;
564 prop_name
= g_strdup_printf("dr-connector[%"PRIu32
"]",
565 spapr_drc_index(drc
));
566 object_property_add_child(owner
, prop_name
, OBJECT(drc
));
567 object_unref(OBJECT(drc
));
568 qdev_realize(DEVICE(drc
), NULL
, NULL
);
573 static void spapr_dr_connector_instance_init(Object
*obj
)
575 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
576 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
578 object_property_add_uint32_ptr(obj
, "id", &drc
->id
, OBJ_PROP_FLAG_READ
);
579 object_property_add(obj
, "index", "uint32", prop_get_index
,
581 object_property_add(obj
, "fdt", "struct", prop_get_fdt
,
583 drc
->state
= drck
->empty_state
;
586 static void spapr_dr_connector_class_init(ObjectClass
*k
, void *data
)
588 DeviceClass
*dk
= DEVICE_CLASS(k
);
590 dk
->realize
= drc_realize
;
591 dk
->unrealize
= drc_unrealize
;
593 * Reason: DR connector needs to be wired to either the machine or to a
594 * PHB in spapr_dr_connector_new().
596 dk
->user_creatable
= false;
599 static bool drc_physical_needed(void *opaque
)
601 SpaprDrcPhysical
*drcp
= (SpaprDrcPhysical
*)opaque
;
602 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(drcp
);
604 if ((drc
->dev
&& (drcp
->dr_indicator
== SPAPR_DR_INDICATOR_ACTIVE
))
605 || (!drc
->dev
&& (drcp
->dr_indicator
== SPAPR_DR_INDICATOR_INACTIVE
))) {
611 static const VMStateDescription vmstate_spapr_drc_physical
= {
612 .name
= "spapr_drc/physical",
614 .minimum_version_id
= 1,
615 .needed
= drc_physical_needed
,
616 .fields
= (VMStateField
[]) {
617 VMSTATE_UINT32(dr_indicator
, SpaprDrcPhysical
),
618 VMSTATE_END_OF_LIST()
622 static void drc_physical_reset(void *opaque
)
624 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(opaque
);
625 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(drc
);
628 drcp
->dr_indicator
= SPAPR_DR_INDICATOR_ACTIVE
;
630 drcp
->dr_indicator
= SPAPR_DR_INDICATOR_INACTIVE
;
634 static void realize_physical(DeviceState
*d
, Error
**errp
)
636 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(d
);
637 Error
*local_err
= NULL
;
639 drc_realize(d
, &local_err
);
641 error_propagate(errp
, local_err
);
645 vmstate_register(VMSTATE_IF(drcp
),
646 spapr_drc_index(SPAPR_DR_CONNECTOR(drcp
)),
647 &vmstate_spapr_drc_physical
, drcp
);
648 qemu_register_reset(drc_physical_reset
, drcp
);
651 static void unrealize_physical(DeviceState
*d
)
653 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(d
);
656 vmstate_unregister(VMSTATE_IF(drcp
), &vmstate_spapr_drc_physical
, drcp
);
657 qemu_unregister_reset(drc_physical_reset
, drcp
);
660 static void spapr_drc_physical_class_init(ObjectClass
*k
, void *data
)
662 DeviceClass
*dk
= DEVICE_CLASS(k
);
663 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
665 dk
->realize
= realize_physical
;
666 dk
->unrealize
= unrealize_physical
;
667 drck
->dr_entity_sense
= physical_entity_sense
;
668 drck
->isolate
= drc_isolate_physical
;
669 drck
->unisolate
= drc_unisolate_physical
;
670 drck
->ready_state
= SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
;
671 drck
->empty_state
= SPAPR_DRC_STATE_PHYSICAL_POWERON
;
674 static void spapr_drc_logical_class_init(ObjectClass
*k
, void *data
)
676 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
678 drck
->dr_entity_sense
= logical_entity_sense
;
679 drck
->isolate
= drc_isolate_logical
;
680 drck
->unisolate
= drc_unisolate_logical
;
681 drck
->ready_state
= SPAPR_DRC_STATE_LOGICAL_CONFIGURED
;
682 drck
->empty_state
= SPAPR_DRC_STATE_LOGICAL_UNUSABLE
;
685 static void spapr_drc_cpu_class_init(ObjectClass
*k
, void *data
)
687 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
689 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU
;
690 drck
->typename
= "CPU";
691 drck
->drc_name_prefix
= "CPU ";
692 drck
->release
= spapr_core_release
;
693 drck
->dt_populate
= spapr_core_dt_populate
;
696 static void spapr_drc_pci_class_init(ObjectClass
*k
, void *data
)
698 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
700 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI
;
701 drck
->typename
= "28";
702 drck
->drc_name_prefix
= "C";
703 drck
->release
= spapr_phb_remove_pci_device_cb
;
704 drck
->dt_populate
= spapr_pci_dt_populate
;
707 static void spapr_drc_lmb_class_init(ObjectClass
*k
, void *data
)
709 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
711 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB
;
712 drck
->typename
= "MEM";
713 drck
->drc_name_prefix
= "LMB ";
714 drck
->release
= spapr_lmb_release
;
715 drck
->dt_populate
= spapr_lmb_dt_populate
;
718 static void spapr_drc_phb_class_init(ObjectClass
*k
, void *data
)
720 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
722 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB
;
723 drck
->typename
= "PHB";
724 drck
->drc_name_prefix
= "PHB ";
725 drck
->release
= spapr_phb_release
;
726 drck
->dt_populate
= spapr_phb_dt_populate
;
729 static void spapr_drc_pmem_class_init(ObjectClass
*k
, void *data
)
731 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
733 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM
;
734 drck
->typename
= "PMEM";
735 drck
->drc_name_prefix
= "PMEM ";
736 drck
->release
= NULL
;
737 drck
->dt_populate
= spapr_pmem_dt_populate
;
740 static const TypeInfo spapr_dr_connector_info
= {
741 .name
= TYPE_SPAPR_DR_CONNECTOR
,
742 .parent
= TYPE_DEVICE
,
743 .instance_size
= sizeof(SpaprDrc
),
744 .instance_init
= spapr_dr_connector_instance_init
,
745 .class_size
= sizeof(SpaprDrcClass
),
746 .class_init
= spapr_dr_connector_class_init
,
750 static const TypeInfo spapr_drc_physical_info
= {
751 .name
= TYPE_SPAPR_DRC_PHYSICAL
,
752 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
753 .instance_size
= sizeof(SpaprDrcPhysical
),
754 .class_init
= spapr_drc_physical_class_init
,
758 static const TypeInfo spapr_drc_logical_info
= {
759 .name
= TYPE_SPAPR_DRC_LOGICAL
,
760 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
761 .class_init
= spapr_drc_logical_class_init
,
765 static const TypeInfo spapr_drc_cpu_info
= {
766 .name
= TYPE_SPAPR_DRC_CPU
,
767 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
768 .class_init
= spapr_drc_cpu_class_init
,
771 static const TypeInfo spapr_drc_pci_info
= {
772 .name
= TYPE_SPAPR_DRC_PCI
,
773 .parent
= TYPE_SPAPR_DRC_PHYSICAL
,
774 .class_init
= spapr_drc_pci_class_init
,
777 static const TypeInfo spapr_drc_lmb_info
= {
778 .name
= TYPE_SPAPR_DRC_LMB
,
779 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
780 .class_init
= spapr_drc_lmb_class_init
,
783 static const TypeInfo spapr_drc_phb_info
= {
784 .name
= TYPE_SPAPR_DRC_PHB
,
785 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
786 .instance_size
= sizeof(SpaprDrc
),
787 .class_init
= spapr_drc_phb_class_init
,
790 static const TypeInfo spapr_drc_pmem_info
= {
791 .name
= TYPE_SPAPR_DRC_PMEM
,
792 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
793 .class_init
= spapr_drc_pmem_class_init
,
796 /* helper functions for external users */
798 SpaprDrc
*spapr_drc_by_index(uint32_t index
)
801 g_autofree gchar
*name
= g_strdup_printf("%s/%x", DRC_CONTAINER_PATH
,
803 obj
= object_resolve_path(name
, NULL
);
805 return !obj
? NULL
: SPAPR_DR_CONNECTOR(obj
);
808 SpaprDrc
*spapr_drc_by_id(const char *type
, uint32_t id
)
811 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type
));
813 return spapr_drc_by_index(drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
814 | (id
& DRC_INDEX_ID_MASK
));
820 * @fdt: libfdt device tree
821 * @path: path in the DT to generate properties
822 * @owner: parent Object/DeviceState for which to generate DRC
824 * @drc_type_mask: mask of SpaprDrcType values corresponding
825 * to the types of DRCs to generate entries for
827 * generate OF properties to describe DRC topology/indices to guests
829 * as documented in PAPR+ v2.1, 13.5.2
831 int spapr_dt_drc(void *fdt
, int offset
, Object
*owner
, uint32_t drc_type_mask
)
833 Object
*root_container
;
834 ObjectProperty
*prop
;
835 ObjectPropertyIterator iter
;
836 uint32_t drc_count
= 0;
837 g_autoptr(GArray
) drc_indexes
= g_array_new(false, true,
839 g_autoptr(GArray
) drc_power_domains
= g_array_new(false, true,
841 g_autoptr(GString
) drc_names
= g_string_set_size(g_string_new(NULL
),
843 g_autoptr(GString
) drc_types
= g_string_set_size(g_string_new(NULL
),
848 * This should really be only called once per node since it overwrites
849 * the OF properties if they already exist.
851 g_assert(!fdt_get_property(fdt
, offset
, "ibm,drc-indexes", NULL
));
853 /* the first entry of each properties is a 32-bit integer encoding
854 * the number of elements in the array. we won't know this until
855 * we complete the iteration through all the matching DRCs, but
856 * reserve the space now and set the offsets accordingly so we
857 * can fill them in later.
859 drc_indexes
= g_array_set_size(drc_indexes
, 1);
860 drc_power_domains
= g_array_set_size(drc_power_domains
, 1);
862 /* aliases for all DRConnector objects will be rooted in QOM
863 * composition tree at DRC_CONTAINER_PATH
865 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
867 object_property_iter_init(&iter
, root_container
);
868 while ((prop
= object_property_iter_next(&iter
))) {
872 g_autofree
char *drc_name
= NULL
;
873 uint32_t drc_index
, drc_power_domain
;
875 if (!strstart(prop
->type
, "link<", NULL
)) {
879 obj
= object_property_get_link(root_container
, prop
->name
,
881 drc
= SPAPR_DR_CONNECTOR(obj
);
882 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
884 if (owner
&& (drc
->owner
!= owner
)) {
888 if ((spapr_drc_type(drc
) & drc_type_mask
) == 0) {
894 /* ibm,drc-indexes */
895 drc_index
= cpu_to_be32(spapr_drc_index(drc
));
896 g_array_append_val(drc_indexes
, drc_index
);
898 /* ibm,drc-power-domains */
899 drc_power_domain
= cpu_to_be32(-1);
900 g_array_append_val(drc_power_domains
, drc_power_domain
);
903 drc_name
= spapr_drc_name(drc
);
904 drc_names
= g_string_append(drc_names
, drc_name
);
905 drc_names
= g_string_insert_len(drc_names
, -1, "\0", 1);
908 drc_types
= g_string_append(drc_types
, drck
->typename
);
909 drc_types
= g_string_insert_len(drc_types
, -1, "\0", 1);
912 /* now write the drc count into the space we reserved at the
913 * beginning of the arrays previously
915 *(uint32_t *)drc_indexes
->data
= cpu_to_be32(drc_count
);
916 *(uint32_t *)drc_power_domains
->data
= cpu_to_be32(drc_count
);
917 *(uint32_t *)drc_names
->str
= cpu_to_be32(drc_count
);
918 *(uint32_t *)drc_types
->str
= cpu_to_be32(drc_count
);
920 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-indexes",
922 drc_indexes
->len
* sizeof(uint32_t));
924 error_report("Couldn't create ibm,drc-indexes property");
928 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-power-domains",
929 drc_power_domains
->data
,
930 drc_power_domains
->len
* sizeof(uint32_t));
932 error_report("Couldn't finalize ibm,drc-power-domains property");
936 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-names",
937 drc_names
->str
, drc_names
->len
);
939 error_report("Couldn't finalize ibm,drc-names property");
943 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-types",
944 drc_types
->str
, drc_types
->len
);
946 error_report("Couldn't finalize ibm,drc-types property");
952 void spapr_drc_reset_all(SpaprMachineState
*spapr
)
954 Object
*drc_container
;
955 ObjectProperty
*prop
;
956 ObjectPropertyIterator iter
;
958 drc_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
960 object_property_iter_init(&iter
, drc_container
);
961 while ((prop
= object_property_iter_next(&iter
))) {
964 if (!strstart(prop
->type
, "link<", NULL
)) {
967 drc
= SPAPR_DR_CONNECTOR(object_property_get_link(drc_container
,
972 * This will complete any pending plug/unplug requests.
973 * In case of a unplugged PHB or PCI bridge, this will
974 * cause some DRCs to be destroyed and thus potentially
975 * invalidate the iterator.
977 if (spapr_drc_reset(drc
)) {
987 static uint32_t rtas_set_isolation_state(uint32_t idx
, uint32_t state
)
989 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
993 return RTAS_OUT_NO_SUCH_INDICATOR
;
996 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc
), state
);
998 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1001 case SPAPR_DR_ISOLATION_STATE_ISOLATED
:
1002 return drck
->isolate(drc
);
1004 case SPAPR_DR_ISOLATION_STATE_UNISOLATED
:
1005 return drck
->unisolate(drc
);
1008 return RTAS_OUT_PARAM_ERROR
;
1012 static uint32_t rtas_set_allocation_state(uint32_t idx
, uint32_t state
)
1014 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
1016 if (!drc
|| !object_dynamic_cast(OBJECT(drc
), TYPE_SPAPR_DRC_LOGICAL
)) {
1017 return RTAS_OUT_NO_SUCH_INDICATOR
;
1020 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc
), state
);
1023 case SPAPR_DR_ALLOCATION_STATE_USABLE
:
1024 return drc_set_usable(drc
);
1026 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE
:
1027 return drc_set_unusable(drc
);
1030 return RTAS_OUT_PARAM_ERROR
;
1034 static uint32_t rtas_set_dr_indicator(uint32_t idx
, uint32_t state
)
1036 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
1038 if (!drc
|| !object_dynamic_cast(OBJECT(drc
), TYPE_SPAPR_DRC_PHYSICAL
)) {
1039 return RTAS_OUT_NO_SUCH_INDICATOR
;
1041 if ((state
!= SPAPR_DR_INDICATOR_INACTIVE
)
1042 && (state
!= SPAPR_DR_INDICATOR_ACTIVE
)
1043 && (state
!= SPAPR_DR_INDICATOR_IDENTIFY
)
1044 && (state
!= SPAPR_DR_INDICATOR_ACTION
)) {
1045 return RTAS_OUT_PARAM_ERROR
; /* bad state parameter */
1048 trace_spapr_drc_set_dr_indicator(idx
, state
);
1049 SPAPR_DRC_PHYSICAL(drc
)->dr_indicator
= state
;
1050 return RTAS_OUT_SUCCESS
;
1053 static void rtas_set_indicator(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
1055 uint32_t nargs
, target_ulong args
,
1056 uint32_t nret
, target_ulong rets
)
1058 uint32_t type
, idx
, state
;
1059 uint32_t ret
= RTAS_OUT_SUCCESS
;
1061 if (nargs
!= 3 || nret
!= 1) {
1062 ret
= RTAS_OUT_PARAM_ERROR
;
1066 type
= rtas_ld(args
, 0);
1067 idx
= rtas_ld(args
, 1);
1068 state
= rtas_ld(args
, 2);
1071 case RTAS_SENSOR_TYPE_ISOLATION_STATE
:
1072 ret
= rtas_set_isolation_state(idx
, state
);
1074 case RTAS_SENSOR_TYPE_DR
:
1075 ret
= rtas_set_dr_indicator(idx
, state
);
1077 case RTAS_SENSOR_TYPE_ALLOCATION_STATE
:
1078 ret
= rtas_set_allocation_state(idx
, state
);
1081 ret
= RTAS_OUT_NOT_SUPPORTED
;
1085 rtas_st(rets
, 0, ret
);
1088 static void rtas_get_sensor_state(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
1089 uint32_t token
, uint32_t nargs
,
1090 target_ulong args
, uint32_t nret
,
1093 uint32_t sensor_type
;
1094 uint32_t sensor_index
;
1095 uint32_t sensor_state
= 0;
1097 SpaprDrcClass
*drck
;
1098 uint32_t ret
= RTAS_OUT_SUCCESS
;
1100 if (nargs
!= 2 || nret
!= 2) {
1101 ret
= RTAS_OUT_PARAM_ERROR
;
1105 sensor_type
= rtas_ld(args
, 0);
1106 sensor_index
= rtas_ld(args
, 1);
1108 if (sensor_type
!= RTAS_SENSOR_TYPE_ENTITY_SENSE
) {
1109 /* currently only DR-related sensors are implemented */
1110 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index
,
1112 ret
= RTAS_OUT_NOT_SUPPORTED
;
1116 drc
= spapr_drc_by_index(sensor_index
);
1118 trace_spapr_rtas_get_sensor_state_invalid(sensor_index
);
1119 ret
= RTAS_OUT_PARAM_ERROR
;
1122 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1123 sensor_state
= drck
->dr_entity_sense(drc
);
1126 rtas_st(rets
, 0, ret
);
1127 rtas_st(rets
, 1, sensor_state
);
1130 /* configure-connector work area offsets, int32_t units for field
1131 * indexes, bytes for field offset/len values.
1133 * as documented by PAPR+ v2.7, 13.5.3.5
1135 #define CC_IDX_NODE_NAME_OFFSET 2
1136 #define CC_IDX_PROP_NAME_OFFSET 2
1137 #define CC_IDX_PROP_LEN 3
1138 #define CC_IDX_PROP_DATA_OFFSET 4
1139 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1140 #define CC_WA_LEN 4096
1142 static void configure_connector_st(target_ulong addr
, target_ulong offset
,
1143 const void *buf
, size_t len
)
1145 cpu_physical_memory_write(ppc64_phys_to_real(addr
+ offset
),
1146 buf
, MIN(len
, CC_WA_LEN
- offset
));
1149 static void rtas_ibm_configure_connector(PowerPCCPU
*cpu
,
1150 SpaprMachineState
*spapr
,
1151 uint32_t token
, uint32_t nargs
,
1152 target_ulong args
, uint32_t nret
,
1159 SpaprDrcClass
*drck
;
1160 SpaprDRCCResponse resp
= SPAPR_DR_CC_RESPONSE_CONTINUE
;
1163 if (nargs
!= 2 || nret
!= 1) {
1164 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
1168 wa_addr
= ((uint64_t)rtas_ld(args
, 1) << 32) | rtas_ld(args
, 0);
1170 drc_index
= rtas_ld(wa_addr
, 0);
1171 drc
= spapr_drc_by_index(drc_index
);
1173 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index
);
1174 rc
= RTAS_OUT_PARAM_ERROR
;
1178 if ((drc
->state
!= SPAPR_DRC_STATE_LOGICAL_UNISOLATE
)
1179 && (drc
->state
!= SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
)
1180 && (drc
->state
!= SPAPR_DRC_STATE_LOGICAL_CONFIGURED
)
1181 && (drc
->state
!= SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
)) {
1183 * Need to unisolate the device before configuring
1184 * or it should already be in configured state to
1185 * allow configure-connector be called repeatedly.
1187 rc
= SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE
;
1191 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1194 * This indicates that the kernel is reconfiguring a LMB due to
1195 * a failed hotunplug. Rollback the DIMM unplug process.
1197 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_LMB
&&
1198 drc
->unplug_requested
) {
1199 spapr_memory_unplug_rollback(spapr
, drc
->dev
);
1206 fdt
= create_device_tree(&fdt_size
);
1208 if (drck
->dt_populate(drc
, spapr
, fdt
, &drc
->fdt_start_offset
,
1211 rc
= SPAPR_DR_CC_RESPONSE_ERROR
;
1216 drc
->ccs_offset
= drc
->fdt_start_offset
;
1223 const struct fdt_property
*prop
;
1224 int fdt_offset_next
, prop_len
;
1226 tag
= fdt_next_tag(drc
->fdt
, drc
->ccs_offset
, &fdt_offset_next
);
1229 case FDT_BEGIN_NODE
:
1231 name
= fdt_get_name(drc
->fdt
, drc
->ccs_offset
, NULL
);
1233 /* provide the name of the next OF node */
1234 wa_offset
= CC_VAL_DATA_OFFSET
;
1235 rtas_st(wa_addr
, CC_IDX_NODE_NAME_OFFSET
, wa_offset
);
1236 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1237 resp
= SPAPR_DR_CC_RESPONSE_NEXT_CHILD
;
1241 if (drc
->ccs_depth
== 0) {
1242 uint32_t drc_index
= spapr_drc_index(drc
);
1244 /* done sending the device tree, move to configured state */
1245 trace_spapr_drc_set_configured(drc_index
);
1246 drc
->state
= drck
->ready_state
;
1248 * Ensure that we are able to send the FDT fragment
1249 * again via configure-connector call if the guest requests.
1251 drc
->ccs_offset
= drc
->fdt_start_offset
;
1253 fdt_offset_next
= drc
->fdt_start_offset
;
1254 resp
= SPAPR_DR_CC_RESPONSE_SUCCESS
;
1256 resp
= SPAPR_DR_CC_RESPONSE_PREV_PARENT
;
1260 prop
= fdt_get_property_by_offset(drc
->fdt
, drc
->ccs_offset
,
1262 name
= fdt_string(drc
->fdt
, fdt32_to_cpu(prop
->nameoff
));
1264 /* provide the name of the next OF property */
1265 wa_offset
= CC_VAL_DATA_OFFSET
;
1266 rtas_st(wa_addr
, CC_IDX_PROP_NAME_OFFSET
, wa_offset
);
1267 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1269 /* provide the length and value of the OF property. data gets
1270 * placed immediately after NULL terminator of the OF property's
1273 wa_offset
+= strlen(name
) + 1,
1274 rtas_st(wa_addr
, CC_IDX_PROP_LEN
, prop_len
);
1275 rtas_st(wa_addr
, CC_IDX_PROP_DATA_OFFSET
, wa_offset
);
1276 configure_connector_st(wa_addr
, wa_offset
, prop
->data
, prop_len
);
1277 resp
= SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY
;
1280 resp
= SPAPR_DR_CC_RESPONSE_ERROR
;
1282 /* keep seeking for an actionable tag */
1285 if (drc
->ccs_offset
>= 0) {
1286 drc
->ccs_offset
= fdt_offset_next
;
1288 } while (resp
== SPAPR_DR_CC_RESPONSE_CONTINUE
);
1292 rtas_st(rets
, 0, rc
);
1295 static void spapr_drc_register_types(void)
1297 type_register_static(&spapr_dr_connector_info
);
1298 type_register_static(&spapr_drc_physical_info
);
1299 type_register_static(&spapr_drc_logical_info
);
1300 type_register_static(&spapr_drc_cpu_info
);
1301 type_register_static(&spapr_drc_pci_info
);
1302 type_register_static(&spapr_drc_lmb_info
);
1303 type_register_static(&spapr_drc_phb_info
);
1304 type_register_static(&spapr_drc_pmem_info
);
1306 spapr_rtas_register(RTAS_SET_INDICATOR
, "set-indicator",
1307 rtas_set_indicator
);
1308 spapr_rtas_register(RTAS_GET_SENSOR_STATE
, "get-sensor-state",
1309 rtas_get_sensor_state
);
1310 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR
, "ibm,configure-connector",
1311 rtas_ibm_configure_connector
);
1313 type_init(spapr_drc_register_types
)