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 "sysemu/device_tree.h"
26 #include "sysemu/reset.h"
29 #define DRC_CONTAINER_PATH "/dr-connector"
30 #define DRC_INDEX_TYPE_SHIFT 28
31 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
33 SpaprDrcType
spapr_drc_type(SpaprDrc
*drc
)
35 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
37 return 1 << drck
->typeshift
;
40 uint32_t spapr_drc_index(SpaprDrc
*drc
)
42 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
44 /* no set format for a drc index: it only needs to be globally
45 * unique. this is how we encode the DRC type on bare-metal
46 * however, so might as well do that here
48 return (drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
)
49 | (drc
->id
& DRC_INDEX_ID_MASK
);
52 static uint32_t drc_isolate_physical(SpaprDrc
*drc
)
55 case SPAPR_DRC_STATE_PHYSICAL_POWERON
:
56 return RTAS_OUT_SUCCESS
; /* Nothing to do */
57 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
:
58 break; /* see below */
59 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
:
60 return RTAS_OUT_PARAM_ERROR
; /* not allowed */
62 g_assert_not_reached();
65 drc
->state
= SPAPR_DRC_STATE_PHYSICAL_POWERON
;
67 if (drc
->unplug_requested
) {
68 uint32_t drc_index
= spapr_drc_index(drc
);
69 trace_spapr_drc_set_isolation_state_finalizing(drc_index
);
70 spapr_drc_detach(drc
);
73 return RTAS_OUT_SUCCESS
;
76 static uint32_t drc_unisolate_physical(SpaprDrc
*drc
)
79 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
:
80 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
:
81 return RTAS_OUT_SUCCESS
; /* Nothing to do */
82 case SPAPR_DRC_STATE_PHYSICAL_POWERON
:
83 break; /* see below */
85 g_assert_not_reached();
88 /* cannot unisolate a non-existent resource, and, or resources
89 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
93 return RTAS_OUT_NO_SUCH_INDICATOR
;
96 drc
->state
= SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
;
97 drc
->ccs_offset
= drc
->fdt_start_offset
;
100 return RTAS_OUT_SUCCESS
;
103 static uint32_t drc_isolate_logical(SpaprDrc
*drc
)
105 switch (drc
->state
) {
106 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
107 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
108 return RTAS_OUT_SUCCESS
; /* Nothing to do */
109 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
110 break; /* see below */
111 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
112 return RTAS_OUT_PARAM_ERROR
; /* not allowed */
114 g_assert_not_reached();
118 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
119 * belong to a DIMM device that is marked for removal.
121 * Currently the guest userspace tool drmgr that drives the memory
122 * hotplug/unplug will just try to remove a set of 'removable' LMBs
123 * in response to a hot unplug request that is based on drc-count.
124 * If the LMB being removed doesn't belong to a DIMM device that is
125 * actually being unplugged, fail the isolation request here.
127 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_LMB
128 && !drc
->unplug_requested
) {
129 return RTAS_OUT_HW_ERROR
;
132 drc
->state
= SPAPR_DRC_STATE_LOGICAL_AVAILABLE
;
134 /* if we're awaiting release, but still in an unconfigured state,
135 * it's likely the guest is still in the process of configuring
136 * the device and is transitioning the devices to an ISOLATED
137 * state as a part of that process. so we only complete the
138 * removal when this transition happens for a device in a
139 * configured state, as suggested by the state diagram from PAPR+
142 if (drc
->unplug_requested
) {
143 uint32_t drc_index
= spapr_drc_index(drc
);
144 trace_spapr_drc_set_isolation_state_finalizing(drc_index
);
145 spapr_drc_detach(drc
);
147 return RTAS_OUT_SUCCESS
;
150 static uint32_t drc_unisolate_logical(SpaprDrc
*drc
)
152 switch (drc
->state
) {
153 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
154 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
155 return RTAS_OUT_SUCCESS
; /* Nothing to do */
156 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
157 break; /* see below */
158 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
159 return RTAS_OUT_NO_SUCH_INDICATOR
; /* not allowed */
161 g_assert_not_reached();
164 /* Move to AVAILABLE state should have ensured device was present */
167 drc
->state
= SPAPR_DRC_STATE_LOGICAL_UNISOLATE
;
168 drc
->ccs_offset
= drc
->fdt_start_offset
;
171 return RTAS_OUT_SUCCESS
;
174 static uint32_t drc_set_usable(SpaprDrc
*drc
)
176 switch (drc
->state
) {
177 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
178 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
179 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
180 return RTAS_OUT_SUCCESS
; /* Nothing to do */
181 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
182 break; /* see below */
184 g_assert_not_reached();
187 /* if there's no resource/device associated with the DRC, there's
188 * no way for us to put it in an allocation state consistent with
189 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
190 * result in an RTAS return code of -3 / "no such indicator"
193 return RTAS_OUT_NO_SUCH_INDICATOR
;
195 if (drc
->unplug_requested
) {
196 /* Don't allow the guest to move a device away from UNUSABLE
197 * state when we want to unplug it */
198 return RTAS_OUT_NO_SUCH_INDICATOR
;
201 drc
->state
= SPAPR_DRC_STATE_LOGICAL_AVAILABLE
;
203 return RTAS_OUT_SUCCESS
;
206 static uint32_t drc_set_unusable(SpaprDrc
*drc
)
208 switch (drc
->state
) {
209 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
210 return RTAS_OUT_SUCCESS
; /* Nothing to do */
211 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
212 break; /* see below */
213 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
214 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
215 return RTAS_OUT_NO_SUCH_INDICATOR
; /* not allowed */
217 g_assert_not_reached();
220 drc
->state
= SPAPR_DRC_STATE_LOGICAL_UNUSABLE
;
221 if (drc
->unplug_requested
) {
222 uint32_t drc_index
= spapr_drc_index(drc
);
223 trace_spapr_drc_set_allocation_state_finalizing(drc_index
);
224 spapr_drc_detach(drc
);
227 return RTAS_OUT_SUCCESS
;
230 static char *spapr_drc_name(SpaprDrc
*drc
)
232 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
234 /* human-readable name for a DRC to encode into the DT
235 * description. this is mainly only used within a guest in place
236 * of the unique DRC index.
238 * in the case of VIO/PCI devices, it corresponds to a "location
239 * code" that maps a logical device/function (DRC index) to a
240 * physical (or virtual in the case of VIO) location in the system
241 * by chaining together the "location label" for each
242 * encapsulating component.
244 * since this is more to do with diagnosing physical hardware
245 * issues than guest compatibility, we choose location codes/DRC
246 * names that adhere to the documented format, but avoid encoding
247 * the entire topology information into the label/code, instead
248 * just using the location codes based on the labels for the
249 * endpoints (VIO/PCI adaptor connectors), which is basically just
250 * "C" followed by an integer ID.
252 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
253 * location codes as documented by PAPR+ v2.7, 12.3.1.5
255 return g_strdup_printf("%s%d", drck
->drc_name_prefix
, drc
->id
);
259 * dr-entity-sense sensor value
260 * returned via get-sensor-state RTAS calls
261 * as expected by state diagram in PAPR+ 2.7, 13.4
262 * based on the current allocation/indicator/power states
263 * for the DR connector.
265 static SpaprDREntitySense
physical_entity_sense(SpaprDrc
*drc
)
267 /* this assumes all PCI devices are assigned to a 'live insertion'
268 * power domain, where QEMU manages power state automatically as
269 * opposed to the guest. present, non-PCI resources are unaffected
272 return drc
->dev
? SPAPR_DR_ENTITY_SENSE_PRESENT
273 : SPAPR_DR_ENTITY_SENSE_EMPTY
;
276 static SpaprDREntitySense
logical_entity_sense(SpaprDrc
*drc
)
278 switch (drc
->state
) {
279 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
280 return SPAPR_DR_ENTITY_SENSE_UNUSABLE
;
281 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
282 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
283 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
285 return SPAPR_DR_ENTITY_SENSE_PRESENT
;
287 g_assert_not_reached();
291 static void prop_get_index(Object
*obj
, Visitor
*v
, const char *name
,
292 void *opaque
, Error
**errp
)
294 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
295 uint32_t value
= spapr_drc_index(drc
);
296 visit_type_uint32(v
, name
, &value
, errp
);
299 static void prop_get_fdt(Object
*obj
, Visitor
*v
, const char *name
,
300 void *opaque
, Error
**errp
)
302 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
305 int fdt_offset_next
, fdt_offset
, fdt_depth
;
309 visit_type_null(v
, NULL
, &null
, errp
);
315 fdt_offset
= drc
->fdt_start_offset
;
319 const char *name
= NULL
;
320 const struct fdt_property
*prop
= NULL
;
321 int prop_len
= 0, name_len
= 0;
324 tag
= fdt_next_tag(fdt
, fdt_offset
, &fdt_offset_next
);
328 name
= fdt_get_name(fdt
, fdt_offset
, &name_len
);
329 visit_start_struct(v
, name
, NULL
, 0, &err
);
331 error_propagate(errp
, err
);
336 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
337 g_assert(fdt_depth
> 0);
338 visit_check_struct(v
, &err
);
339 visit_end_struct(v
, NULL
);
341 error_propagate(errp
, err
);
348 prop
= fdt_get_property_by_offset(fdt
, fdt_offset
, &prop_len
);
349 name
= fdt_string(fdt
, fdt32_to_cpu(prop
->nameoff
));
350 visit_start_list(v
, name
, NULL
, 0, &err
);
352 error_propagate(errp
, err
);
355 for (i
= 0; i
< prop_len
; i
++) {
356 visit_type_uint8(v
, NULL
, (uint8_t *)&prop
->data
[i
], &err
);
358 error_propagate(errp
, err
);
362 visit_check_list(v
, &err
);
363 visit_end_list(v
, NULL
);
365 error_propagate(errp
, err
);
371 error_report("device FDT in unexpected state: %d", tag
);
374 fdt_offset
= fdt_offset_next
;
375 } while (fdt_depth
!= 0);
378 void spapr_drc_attach(SpaprDrc
*drc
, DeviceState
*d
, Error
**errp
)
380 trace_spapr_drc_attach(spapr_drc_index(drc
));
383 error_setg(errp
, "an attached device is still awaiting release");
386 g_assert((drc
->state
== SPAPR_DRC_STATE_LOGICAL_UNUSABLE
)
387 || (drc
->state
== SPAPR_DRC_STATE_PHYSICAL_POWERON
));
391 object_property_add_link(OBJECT(drc
), "device",
392 object_get_typename(OBJECT(drc
->dev
)),
393 (Object
**)(&drc
->dev
),
397 static void spapr_drc_release(SpaprDrc
*drc
)
399 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
401 drck
->release(drc
->dev
);
403 drc
->unplug_requested
= false;
406 drc
->fdt_start_offset
= 0;
407 object_property_del(OBJECT(drc
), "device", &error_abort
);
411 void spapr_drc_detach(SpaprDrc
*drc
)
413 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
415 trace_spapr_drc_detach(spapr_drc_index(drc
));
419 drc
->unplug_requested
= true;
421 if (drc
->state
!= drck
->empty_state
) {
422 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc
));
426 spapr_drc_release(drc
);
429 void spapr_drc_reset(SpaprDrc
*drc
)
431 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
433 trace_spapr_drc_reset(spapr_drc_index(drc
));
435 /* immediately upon reset we can safely assume DRCs whose devices
436 * are pending removal can be safely removed.
438 if (drc
->unplug_requested
) {
439 spapr_drc_release(drc
);
443 /* A device present at reset is ready to go, same as coldplugged */
444 drc
->state
= drck
->ready_state
;
446 * Ensure that we are able to send the FDT fragment again
447 * via configure-connector call if the guest requests.
449 drc
->ccs_offset
= drc
->fdt_start_offset
;
452 drc
->state
= drck
->empty_state
;
453 drc
->ccs_offset
= -1;
458 bool spapr_drc_needed(void *opaque
)
460 SpaprDrc
*drc
= (SpaprDrc
*)opaque
;
461 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
463 /* If no dev is plugged in there is no need to migrate the DRC state */
469 * We need to migrate the state if it's not equal to the expected
470 * long-term state, which is the same as the coldplugged initial
472 return (drc
->state
!= drck
->ready_state
);
475 static const VMStateDescription vmstate_spapr_drc
= {
478 .minimum_version_id
= 1,
479 .needed
= spapr_drc_needed
,
480 .fields
= (VMStateField
[]) {
481 VMSTATE_UINT32(state
, SpaprDrc
),
482 VMSTATE_END_OF_LIST()
486 static void realize(DeviceState
*d
, Error
**errp
)
488 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(d
);
489 Object
*root_container
;
494 trace_spapr_drc_realize(spapr_drc_index(drc
));
495 /* NOTE: we do this as part of realize/unrealize due to the fact
496 * that the guest will communicate with the DRC via RTAS calls
497 * referencing the global DRC index. By unlinking the DRC
498 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
499 * inaccessible by the guest, since lookups rely on this path
500 * existing in the composition tree
502 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
503 link_name
= g_strdup_printf("%x", spapr_drc_index(drc
));
504 child_name
= object_get_canonical_path_component(OBJECT(drc
));
505 trace_spapr_drc_realize_child(spapr_drc_index(drc
), child_name
);
506 object_property_add_alias(root_container
, link_name
,
507 drc
->owner
, child_name
, &err
);
511 error_propagate(errp
, err
);
514 vmstate_register(VMSTATE_IF(drc
), spapr_drc_index(drc
), &vmstate_spapr_drc
,
516 trace_spapr_drc_realize_complete(spapr_drc_index(drc
));
519 static void unrealize(DeviceState
*d
, Error
**errp
)
521 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(d
);
522 Object
*root_container
;
525 trace_spapr_drc_unrealize(spapr_drc_index(drc
));
526 vmstate_unregister(VMSTATE_IF(drc
), &vmstate_spapr_drc
, drc
);
527 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
528 name
= g_strdup_printf("%x", spapr_drc_index(drc
));
529 object_property_del(root_container
, name
, errp
);
533 SpaprDrc
*spapr_dr_connector_new(Object
*owner
, const char *type
,
536 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(object_new(type
));
541 prop_name
= g_strdup_printf("dr-connector[%"PRIu32
"]",
542 spapr_drc_index(drc
));
543 object_property_add_child(owner
, prop_name
, OBJECT(drc
), &error_abort
);
544 object_unref(OBJECT(drc
));
545 object_property_set_bool(OBJECT(drc
), true, "realized", NULL
);
551 static void spapr_dr_connector_instance_init(Object
*obj
)
553 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
554 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
556 object_property_add_uint32_ptr(obj
, "id", &drc
->id
, NULL
);
557 object_property_add(obj
, "index", "uint32", prop_get_index
,
558 NULL
, NULL
, NULL
, NULL
);
559 object_property_add(obj
, "fdt", "struct", prop_get_fdt
,
560 NULL
, NULL
, NULL
, NULL
);
561 drc
->state
= drck
->empty_state
;
564 static void spapr_dr_connector_class_init(ObjectClass
*k
, void *data
)
566 DeviceClass
*dk
= DEVICE_CLASS(k
);
568 dk
->realize
= realize
;
569 dk
->unrealize
= unrealize
;
571 * Reason: it crashes FIXME find and document the real reason
573 dk
->user_creatable
= false;
576 static bool drc_physical_needed(void *opaque
)
578 SpaprDrcPhysical
*drcp
= (SpaprDrcPhysical
*)opaque
;
579 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(drcp
);
581 if ((drc
->dev
&& (drcp
->dr_indicator
== SPAPR_DR_INDICATOR_ACTIVE
))
582 || (!drc
->dev
&& (drcp
->dr_indicator
== SPAPR_DR_INDICATOR_INACTIVE
))) {
588 static const VMStateDescription vmstate_spapr_drc_physical
= {
589 .name
= "spapr_drc/physical",
591 .minimum_version_id
= 1,
592 .needed
= drc_physical_needed
,
593 .fields
= (VMStateField
[]) {
594 VMSTATE_UINT32(dr_indicator
, SpaprDrcPhysical
),
595 VMSTATE_END_OF_LIST()
599 static void drc_physical_reset(void *opaque
)
601 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(opaque
);
602 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(drc
);
605 drcp
->dr_indicator
= SPAPR_DR_INDICATOR_ACTIVE
;
607 drcp
->dr_indicator
= SPAPR_DR_INDICATOR_INACTIVE
;
611 static void realize_physical(DeviceState
*d
, Error
**errp
)
613 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(d
);
614 Error
*local_err
= NULL
;
616 realize(d
, &local_err
);
618 error_propagate(errp
, local_err
);
622 vmstate_register(VMSTATE_IF(drcp
),
623 spapr_drc_index(SPAPR_DR_CONNECTOR(drcp
)),
624 &vmstate_spapr_drc_physical
, drcp
);
625 qemu_register_reset(drc_physical_reset
, drcp
);
628 static void unrealize_physical(DeviceState
*d
, Error
**errp
)
630 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(d
);
631 Error
*local_err
= NULL
;
633 unrealize(d
, &local_err
);
635 error_propagate(errp
, local_err
);
639 vmstate_unregister(VMSTATE_IF(drcp
), &vmstate_spapr_drc_physical
, drcp
);
640 qemu_unregister_reset(drc_physical_reset
, drcp
);
643 static void spapr_drc_physical_class_init(ObjectClass
*k
, void *data
)
645 DeviceClass
*dk
= DEVICE_CLASS(k
);
646 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
648 dk
->realize
= realize_physical
;
649 dk
->unrealize
= unrealize_physical
;
650 drck
->dr_entity_sense
= physical_entity_sense
;
651 drck
->isolate
= drc_isolate_physical
;
652 drck
->unisolate
= drc_unisolate_physical
;
653 drck
->ready_state
= SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
;
654 drck
->empty_state
= SPAPR_DRC_STATE_PHYSICAL_POWERON
;
657 static void spapr_drc_logical_class_init(ObjectClass
*k
, void *data
)
659 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
661 drck
->dr_entity_sense
= logical_entity_sense
;
662 drck
->isolate
= drc_isolate_logical
;
663 drck
->unisolate
= drc_unisolate_logical
;
664 drck
->ready_state
= SPAPR_DRC_STATE_LOGICAL_CONFIGURED
;
665 drck
->empty_state
= SPAPR_DRC_STATE_LOGICAL_UNUSABLE
;
668 static void spapr_drc_cpu_class_init(ObjectClass
*k
, void *data
)
670 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
672 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU
;
673 drck
->typename
= "CPU";
674 drck
->drc_name_prefix
= "CPU ";
675 drck
->release
= spapr_core_release
;
676 drck
->dt_populate
= spapr_core_dt_populate
;
679 static void spapr_drc_pci_class_init(ObjectClass
*k
, void *data
)
681 SpaprDrcClass
*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
;
687 drck
->dt_populate
= spapr_pci_dt_populate
;
690 static void spapr_drc_lmb_class_init(ObjectClass
*k
, void *data
)
692 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
694 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB
;
695 drck
->typename
= "MEM";
696 drck
->drc_name_prefix
= "LMB ";
697 drck
->release
= spapr_lmb_release
;
698 drck
->dt_populate
= spapr_lmb_dt_populate
;
701 static void spapr_drc_phb_class_init(ObjectClass
*k
, void *data
)
703 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
705 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB
;
706 drck
->typename
= "PHB";
707 drck
->drc_name_prefix
= "PHB ";
708 drck
->release
= spapr_phb_release
;
709 drck
->dt_populate
= spapr_phb_dt_populate
;
712 static const TypeInfo spapr_dr_connector_info
= {
713 .name
= TYPE_SPAPR_DR_CONNECTOR
,
714 .parent
= TYPE_DEVICE
,
715 .instance_size
= sizeof(SpaprDrc
),
716 .instance_init
= spapr_dr_connector_instance_init
,
717 .class_size
= sizeof(SpaprDrcClass
),
718 .class_init
= spapr_dr_connector_class_init
,
722 static const TypeInfo spapr_drc_physical_info
= {
723 .name
= TYPE_SPAPR_DRC_PHYSICAL
,
724 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
725 .instance_size
= sizeof(SpaprDrcPhysical
),
726 .class_init
= spapr_drc_physical_class_init
,
730 static const TypeInfo spapr_drc_logical_info
= {
731 .name
= TYPE_SPAPR_DRC_LOGICAL
,
732 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
733 .class_init
= spapr_drc_logical_class_init
,
737 static const TypeInfo spapr_drc_cpu_info
= {
738 .name
= TYPE_SPAPR_DRC_CPU
,
739 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
740 .class_init
= spapr_drc_cpu_class_init
,
743 static const TypeInfo spapr_drc_pci_info
= {
744 .name
= TYPE_SPAPR_DRC_PCI
,
745 .parent
= TYPE_SPAPR_DRC_PHYSICAL
,
746 .class_init
= spapr_drc_pci_class_init
,
749 static const TypeInfo spapr_drc_lmb_info
= {
750 .name
= TYPE_SPAPR_DRC_LMB
,
751 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
752 .class_init
= spapr_drc_lmb_class_init
,
755 static const TypeInfo spapr_drc_phb_info
= {
756 .name
= TYPE_SPAPR_DRC_PHB
,
757 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
758 .instance_size
= sizeof(SpaprDrc
),
759 .class_init
= spapr_drc_phb_class_init
,
762 /* helper functions for external users */
764 SpaprDrc
*spapr_drc_by_index(uint32_t index
)
769 name
= g_strdup_printf("%s/%x", DRC_CONTAINER_PATH
, index
);
770 obj
= object_resolve_path(name
, NULL
);
773 return !obj
? NULL
: SPAPR_DR_CONNECTOR(obj
);
776 SpaprDrc
*spapr_drc_by_id(const char *type
, uint32_t id
)
779 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type
));
781 return spapr_drc_by_index(drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
782 | (id
& DRC_INDEX_ID_MASK
));
788 * @fdt: libfdt device tree
789 * @path: path in the DT to generate properties
790 * @owner: parent Object/DeviceState for which to generate DRC
792 * @drc_type_mask: mask of SpaprDrcType values corresponding
793 * to the types of DRCs to generate entries for
795 * generate OF properties to describe DRC topology/indices to guests
797 * as documented in PAPR+ v2.1, 13.5.2
799 int spapr_dt_drc(void *fdt
, int offset
, Object
*owner
, uint32_t drc_type_mask
)
801 Object
*root_container
;
802 ObjectProperty
*prop
;
803 ObjectPropertyIterator iter
;
804 uint32_t drc_count
= 0;
805 GArray
*drc_indexes
, *drc_power_domains
;
806 GString
*drc_names
, *drc_types
;
809 /* the first entry of each properties is a 32-bit integer encoding
810 * the number of elements in the array. we won't know this until
811 * we complete the iteration through all the matching DRCs, but
812 * reserve the space now and set the offsets accordingly so we
813 * can fill them in later.
815 drc_indexes
= g_array_new(false, true, sizeof(uint32_t));
816 drc_indexes
= g_array_set_size(drc_indexes
, 1);
817 drc_power_domains
= g_array_new(false, true, sizeof(uint32_t));
818 drc_power_domains
= g_array_set_size(drc_power_domains
, 1);
819 drc_names
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
820 drc_types
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
822 /* aliases for all DRConnector objects will be rooted in QOM
823 * composition tree at DRC_CONTAINER_PATH
825 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
827 object_property_iter_init(&iter
, root_container
);
828 while ((prop
= object_property_iter_next(&iter
))) {
832 char *drc_name
= NULL
;
833 uint32_t drc_index
, drc_power_domain
;
835 if (!strstart(prop
->type
, "link<", NULL
)) {
839 obj
= object_property_get_link(root_container
, prop
->name
, NULL
);
840 drc
= SPAPR_DR_CONNECTOR(obj
);
841 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
843 if (owner
&& (drc
->owner
!= owner
)) {
847 if ((spapr_drc_type(drc
) & drc_type_mask
) == 0) {
853 /* ibm,drc-indexes */
854 drc_index
= cpu_to_be32(spapr_drc_index(drc
));
855 g_array_append_val(drc_indexes
, drc_index
);
857 /* ibm,drc-power-domains */
858 drc_power_domain
= cpu_to_be32(-1);
859 g_array_append_val(drc_power_domains
, drc_power_domain
);
862 drc_name
= spapr_drc_name(drc
);
863 drc_names
= g_string_append(drc_names
, drc_name
);
864 drc_names
= g_string_insert_len(drc_names
, -1, "\0", 1);
868 drc_types
= g_string_append(drc_types
, drck
->typename
);
869 drc_types
= g_string_insert_len(drc_types
, -1, "\0", 1);
872 /* now write the drc count into the space we reserved at the
873 * beginning of the arrays previously
875 *(uint32_t *)drc_indexes
->data
= cpu_to_be32(drc_count
);
876 *(uint32_t *)drc_power_domains
->data
= cpu_to_be32(drc_count
);
877 *(uint32_t *)drc_names
->str
= cpu_to_be32(drc_count
);
878 *(uint32_t *)drc_types
->str
= cpu_to_be32(drc_count
);
880 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-indexes",
882 drc_indexes
->len
* sizeof(uint32_t));
884 error_report("Couldn't create ibm,drc-indexes property");
888 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-power-domains",
889 drc_power_domains
->data
,
890 drc_power_domains
->len
* sizeof(uint32_t));
892 error_report("Couldn't finalize ibm,drc-power-domains property");
896 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-names",
897 drc_names
->str
, drc_names
->len
);
899 error_report("Couldn't finalize ibm,drc-names property");
903 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-types",
904 drc_types
->str
, drc_types
->len
);
906 error_report("Couldn't finalize ibm,drc-types property");
911 g_array_free(drc_indexes
, true);
912 g_array_free(drc_power_domains
, true);
913 g_string_free(drc_names
, true);
914 g_string_free(drc_types
, true);
923 static uint32_t rtas_set_isolation_state(uint32_t idx
, uint32_t state
)
925 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
929 return RTAS_OUT_NO_SUCH_INDICATOR
;
932 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc
), state
);
934 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
937 case SPAPR_DR_ISOLATION_STATE_ISOLATED
:
938 return drck
->isolate(drc
);
940 case SPAPR_DR_ISOLATION_STATE_UNISOLATED
:
941 return drck
->unisolate(drc
);
944 return RTAS_OUT_PARAM_ERROR
;
948 static uint32_t rtas_set_allocation_state(uint32_t idx
, uint32_t state
)
950 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
952 if (!drc
|| !object_dynamic_cast(OBJECT(drc
), TYPE_SPAPR_DRC_LOGICAL
)) {
953 return RTAS_OUT_NO_SUCH_INDICATOR
;
956 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc
), state
);
959 case SPAPR_DR_ALLOCATION_STATE_USABLE
:
960 return drc_set_usable(drc
);
962 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE
:
963 return drc_set_unusable(drc
);
966 return RTAS_OUT_PARAM_ERROR
;
970 static uint32_t rtas_set_dr_indicator(uint32_t idx
, uint32_t state
)
972 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
974 if (!drc
|| !object_dynamic_cast(OBJECT(drc
), TYPE_SPAPR_DRC_PHYSICAL
)) {
975 return RTAS_OUT_NO_SUCH_INDICATOR
;
977 if ((state
!= SPAPR_DR_INDICATOR_INACTIVE
)
978 && (state
!= SPAPR_DR_INDICATOR_ACTIVE
)
979 && (state
!= SPAPR_DR_INDICATOR_IDENTIFY
)
980 && (state
!= SPAPR_DR_INDICATOR_ACTION
)) {
981 return RTAS_OUT_PARAM_ERROR
; /* bad state parameter */
984 trace_spapr_drc_set_dr_indicator(idx
, state
);
985 SPAPR_DRC_PHYSICAL(drc
)->dr_indicator
= state
;
986 return RTAS_OUT_SUCCESS
;
989 static void rtas_set_indicator(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
991 uint32_t nargs
, target_ulong args
,
992 uint32_t nret
, target_ulong rets
)
994 uint32_t type
, idx
, state
;
995 uint32_t ret
= RTAS_OUT_SUCCESS
;
997 if (nargs
!= 3 || nret
!= 1) {
998 ret
= RTAS_OUT_PARAM_ERROR
;
1002 type
= rtas_ld(args
, 0);
1003 idx
= rtas_ld(args
, 1);
1004 state
= rtas_ld(args
, 2);
1007 case RTAS_SENSOR_TYPE_ISOLATION_STATE
:
1008 ret
= rtas_set_isolation_state(idx
, state
);
1010 case RTAS_SENSOR_TYPE_DR
:
1011 ret
= rtas_set_dr_indicator(idx
, state
);
1013 case RTAS_SENSOR_TYPE_ALLOCATION_STATE
:
1014 ret
= rtas_set_allocation_state(idx
, state
);
1017 ret
= RTAS_OUT_NOT_SUPPORTED
;
1021 rtas_st(rets
, 0, ret
);
1024 static void rtas_get_sensor_state(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
1025 uint32_t token
, uint32_t nargs
,
1026 target_ulong args
, uint32_t nret
,
1029 uint32_t sensor_type
;
1030 uint32_t sensor_index
;
1031 uint32_t sensor_state
= 0;
1033 SpaprDrcClass
*drck
;
1034 uint32_t ret
= RTAS_OUT_SUCCESS
;
1036 if (nargs
!= 2 || nret
!= 2) {
1037 ret
= RTAS_OUT_PARAM_ERROR
;
1041 sensor_type
= rtas_ld(args
, 0);
1042 sensor_index
= rtas_ld(args
, 1);
1044 if (sensor_type
!= RTAS_SENSOR_TYPE_ENTITY_SENSE
) {
1045 /* currently only DR-related sensors are implemented */
1046 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index
,
1048 ret
= RTAS_OUT_NOT_SUPPORTED
;
1052 drc
= spapr_drc_by_index(sensor_index
);
1054 trace_spapr_rtas_get_sensor_state_invalid(sensor_index
);
1055 ret
= RTAS_OUT_PARAM_ERROR
;
1058 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1059 sensor_state
= drck
->dr_entity_sense(drc
);
1062 rtas_st(rets
, 0, ret
);
1063 rtas_st(rets
, 1, sensor_state
);
1066 /* configure-connector work area offsets, int32_t units for field
1067 * indexes, bytes for field offset/len values.
1069 * as documented by PAPR+ v2.7, 13.5.3.5
1071 #define CC_IDX_NODE_NAME_OFFSET 2
1072 #define CC_IDX_PROP_NAME_OFFSET 2
1073 #define CC_IDX_PROP_LEN 3
1074 #define CC_IDX_PROP_DATA_OFFSET 4
1075 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1076 #define CC_WA_LEN 4096
1078 static void configure_connector_st(target_ulong addr
, target_ulong offset
,
1079 const void *buf
, size_t len
)
1081 cpu_physical_memory_write(ppc64_phys_to_real(addr
+ offset
),
1082 buf
, MIN(len
, CC_WA_LEN
- offset
));
1085 static void rtas_ibm_configure_connector(PowerPCCPU
*cpu
,
1086 SpaprMachineState
*spapr
,
1087 uint32_t token
, uint32_t nargs
,
1088 target_ulong args
, uint32_t nret
,
1095 SpaprDrcClass
*drck
;
1096 SpaprDRCCResponse resp
= SPAPR_DR_CC_RESPONSE_CONTINUE
;
1099 if (nargs
!= 2 || nret
!= 1) {
1100 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
1104 wa_addr
= ((uint64_t)rtas_ld(args
, 1) << 32) | rtas_ld(args
, 0);
1106 drc_index
= rtas_ld(wa_addr
, 0);
1107 drc
= spapr_drc_by_index(drc_index
);
1109 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index
);
1110 rc
= RTAS_OUT_PARAM_ERROR
;
1114 if ((drc
->state
!= SPAPR_DRC_STATE_LOGICAL_UNISOLATE
)
1115 && (drc
->state
!= SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
)
1116 && (drc
->state
!= SPAPR_DRC_STATE_LOGICAL_CONFIGURED
)
1117 && (drc
->state
!= SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
)) {
1119 * Need to unisolate the device before configuring
1120 * or it should already be in configured state to
1121 * allow configure-connector be called repeatedly.
1123 rc
= SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE
;
1127 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1130 Error
*local_err
= NULL
;
1134 fdt
= create_device_tree(&fdt_size
);
1136 if (drck
->dt_populate(drc
, spapr
, fdt
, &drc
->fdt_start_offset
,
1139 error_free(local_err
);
1140 rc
= SPAPR_DR_CC_RESPONSE_ERROR
;
1145 drc
->ccs_offset
= drc
->fdt_start_offset
;
1152 const struct fdt_property
*prop
;
1153 int fdt_offset_next
, prop_len
;
1155 tag
= fdt_next_tag(drc
->fdt
, drc
->ccs_offset
, &fdt_offset_next
);
1158 case FDT_BEGIN_NODE
:
1160 name
= fdt_get_name(drc
->fdt
, drc
->ccs_offset
, NULL
);
1162 /* provide the name of the next OF node */
1163 wa_offset
= CC_VAL_DATA_OFFSET
;
1164 rtas_st(wa_addr
, CC_IDX_NODE_NAME_OFFSET
, wa_offset
);
1165 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1166 resp
= SPAPR_DR_CC_RESPONSE_NEXT_CHILD
;
1170 if (drc
->ccs_depth
== 0) {
1171 uint32_t drc_index
= spapr_drc_index(drc
);
1173 /* done sending the device tree, move to configured state */
1174 trace_spapr_drc_set_configured(drc_index
);
1175 drc
->state
= drck
->ready_state
;
1177 * Ensure that we are able to send the FDT fragment
1178 * again via configure-connector call if the guest requests.
1180 drc
->ccs_offset
= drc
->fdt_start_offset
;
1182 fdt_offset_next
= drc
->fdt_start_offset
;
1183 resp
= SPAPR_DR_CC_RESPONSE_SUCCESS
;
1185 resp
= SPAPR_DR_CC_RESPONSE_PREV_PARENT
;
1189 prop
= fdt_get_property_by_offset(drc
->fdt
, drc
->ccs_offset
,
1191 name
= fdt_string(drc
->fdt
, fdt32_to_cpu(prop
->nameoff
));
1193 /* provide the name of the next OF property */
1194 wa_offset
= CC_VAL_DATA_OFFSET
;
1195 rtas_st(wa_addr
, CC_IDX_PROP_NAME_OFFSET
, wa_offset
);
1196 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1198 /* provide the length and value of the OF property. data gets
1199 * placed immediately after NULL terminator of the OF property's
1202 wa_offset
+= strlen(name
) + 1,
1203 rtas_st(wa_addr
, CC_IDX_PROP_LEN
, prop_len
);
1204 rtas_st(wa_addr
, CC_IDX_PROP_DATA_OFFSET
, wa_offset
);
1205 configure_connector_st(wa_addr
, wa_offset
, prop
->data
, prop_len
);
1206 resp
= SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY
;
1209 resp
= SPAPR_DR_CC_RESPONSE_ERROR
;
1211 /* keep seeking for an actionable tag */
1214 if (drc
->ccs_offset
>= 0) {
1215 drc
->ccs_offset
= fdt_offset_next
;
1217 } while (resp
== SPAPR_DR_CC_RESPONSE_CONTINUE
);
1221 rtas_st(rets
, 0, rc
);
1224 static void spapr_drc_register_types(void)
1226 type_register_static(&spapr_dr_connector_info
);
1227 type_register_static(&spapr_drc_physical_info
);
1228 type_register_static(&spapr_drc_logical_info
);
1229 type_register_static(&spapr_drc_cpu_info
);
1230 type_register_static(&spapr_drc_pci_info
);
1231 type_register_static(&spapr_drc_lmb_info
);
1232 type_register_static(&spapr_drc_phb_info
);
1234 spapr_rtas_register(RTAS_SET_INDICATOR
, "set-indicator",
1235 rtas_set_indicator
);
1236 spapr_rtas_register(RTAS_GET_SENSOR_STATE
, "get-sensor-state",
1237 rtas_get_sensor_state
);
1238 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR
, "ibm,configure-connector",
1239 rtas_ibm_configure_connector
);
1241 type_init(spapr_drc_register_types
)