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 void spapr_drc_release(SpaprDrc
*drc
)
55 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
57 drck
->release(drc
->dev
);
59 drc
->unplug_requested
= false;
60 timer_del(drc
->unplug_timeout_timer
);
64 drc
->fdt_start_offset
= 0;
65 object_property_del(OBJECT(drc
), "device");
69 static uint32_t drc_isolate_physical(SpaprDrc
*drc
)
72 case SPAPR_DRC_STATE_PHYSICAL_POWERON
:
73 return RTAS_OUT_SUCCESS
; /* Nothing to do */
74 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
:
75 break; /* see below */
76 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
:
77 return RTAS_OUT_PARAM_ERROR
; /* not allowed */
79 g_assert_not_reached();
82 drc
->state
= SPAPR_DRC_STATE_PHYSICAL_POWERON
;
84 if (drc
->unplug_requested
) {
85 uint32_t drc_index
= spapr_drc_index(drc
);
86 trace_spapr_drc_set_isolation_state_finalizing(drc_index
);
87 spapr_drc_release(drc
);
90 return RTAS_OUT_SUCCESS
;
93 static uint32_t drc_unisolate_physical(SpaprDrc
*drc
)
96 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
:
97 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
:
98 return RTAS_OUT_SUCCESS
; /* Nothing to do */
99 case SPAPR_DRC_STATE_PHYSICAL_POWERON
:
100 break; /* see below */
102 g_assert_not_reached();
105 /* cannot unisolate a non-existent resource, and, or resources
106 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
110 return RTAS_OUT_NO_SUCH_INDICATOR
;
113 drc
->state
= SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
;
114 drc
->ccs_offset
= drc
->fdt_start_offset
;
117 return RTAS_OUT_SUCCESS
;
120 static uint32_t drc_isolate_logical(SpaprDrc
*drc
)
122 switch (drc
->state
) {
123 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
124 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
125 return RTAS_OUT_SUCCESS
; /* Nothing to do */
126 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
127 break; /* see below */
128 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
129 return RTAS_OUT_PARAM_ERROR
; /* not allowed */
131 g_assert_not_reached();
135 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
136 * belong to a DIMM device that is marked for removal.
138 * Currently the guest userspace tool drmgr that drives the memory
139 * hotplug/unplug will just try to remove a set of 'removable' LMBs
140 * in response to a hot unplug request that is based on drc-count.
141 * If the LMB being removed doesn't belong to a DIMM device that is
142 * actually being unplugged, fail the isolation request here.
144 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_LMB
145 && !drc
->unplug_requested
) {
146 return RTAS_OUT_HW_ERROR
;
149 drc
->state
= SPAPR_DRC_STATE_LOGICAL_AVAILABLE
;
151 return RTAS_OUT_SUCCESS
;
154 static uint32_t drc_unisolate_logical(SpaprDrc
*drc
)
156 switch (drc
->state
) {
157 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
158 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
159 return RTAS_OUT_SUCCESS
; /* Nothing to do */
160 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
161 break; /* see below */
162 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
163 return RTAS_OUT_NO_SUCH_INDICATOR
; /* not allowed */
165 g_assert_not_reached();
168 /* Move to AVAILABLE state should have ensured device was present */
171 drc
->state
= SPAPR_DRC_STATE_LOGICAL_UNISOLATE
;
172 drc
->ccs_offset
= drc
->fdt_start_offset
;
175 return RTAS_OUT_SUCCESS
;
178 static uint32_t drc_set_usable(SpaprDrc
*drc
)
180 switch (drc
->state
) {
181 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
182 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
183 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
184 return RTAS_OUT_SUCCESS
; /* Nothing to do */
185 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
186 break; /* see below */
188 g_assert_not_reached();
191 /* if there's no resource/device associated with the DRC, there's
192 * no way for us to put it in an allocation state consistent with
193 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
194 * result in an RTAS return code of -3 / "no such indicator"
197 return RTAS_OUT_NO_SUCH_INDICATOR
;
199 if (drc
->unplug_requested
) {
200 /* Don't allow the guest to move a device away from UNUSABLE
201 * state when we want to unplug it */
202 return RTAS_OUT_NO_SUCH_INDICATOR
;
205 drc
->state
= SPAPR_DRC_STATE_LOGICAL_AVAILABLE
;
207 return RTAS_OUT_SUCCESS
;
210 static uint32_t drc_set_unusable(SpaprDrc
*drc
)
212 switch (drc
->state
) {
213 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
214 return RTAS_OUT_SUCCESS
; /* Nothing to do */
215 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
216 break; /* see below */
217 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
218 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
219 return RTAS_OUT_NO_SUCH_INDICATOR
; /* not allowed */
221 g_assert_not_reached();
224 drc
->state
= SPAPR_DRC_STATE_LOGICAL_UNUSABLE
;
225 if (drc
->unplug_requested
) {
226 uint32_t drc_index
= spapr_drc_index(drc
);
227 trace_spapr_drc_set_allocation_state_finalizing(drc_index
);
228 spapr_drc_release(drc
);
231 return RTAS_OUT_SUCCESS
;
234 static char *spapr_drc_name(SpaprDrc
*drc
)
236 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
238 /* human-readable name for a DRC to encode into the DT
239 * description. this is mainly only used within a guest in place
240 * of the unique DRC index.
242 * in the case of VIO/PCI devices, it corresponds to a "location
243 * code" that maps a logical device/function (DRC index) to a
244 * physical (or virtual in the case of VIO) location in the system
245 * by chaining together the "location label" for each
246 * encapsulating component.
248 * since this is more to do with diagnosing physical hardware
249 * issues than guest compatibility, we choose location codes/DRC
250 * names that adhere to the documented format, but avoid encoding
251 * the entire topology information into the label/code, instead
252 * just using the location codes based on the labels for the
253 * endpoints (VIO/PCI adaptor connectors), which is basically just
254 * "C" followed by an integer ID.
256 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
257 * location codes as documented by PAPR+ v2.7, 12.3.1.5
259 return g_strdup_printf("%s%d", drck
->drc_name_prefix
, drc
->id
);
263 * dr-entity-sense sensor value
264 * returned via get-sensor-state RTAS calls
265 * as expected by state diagram in PAPR+ 2.7, 13.4
266 * based on the current allocation/indicator/power states
267 * for the DR connector.
269 static SpaprDREntitySense
physical_entity_sense(SpaprDrc
*drc
)
271 /* this assumes all PCI devices are assigned to a 'live insertion'
272 * power domain, where QEMU manages power state automatically as
273 * opposed to the guest. present, non-PCI resources are unaffected
276 return drc
->dev
? SPAPR_DR_ENTITY_SENSE_PRESENT
277 : SPAPR_DR_ENTITY_SENSE_EMPTY
;
280 static SpaprDREntitySense
logical_entity_sense(SpaprDrc
*drc
)
282 switch (drc
->state
) {
283 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE
:
284 return SPAPR_DR_ENTITY_SENSE_UNUSABLE
;
285 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE
:
286 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE
:
287 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED
:
289 return SPAPR_DR_ENTITY_SENSE_PRESENT
;
291 g_assert_not_reached();
295 static void prop_get_index(Object
*obj
, Visitor
*v
, const char *name
,
296 void *opaque
, Error
**errp
)
298 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
299 uint32_t value
= spapr_drc_index(drc
);
300 visit_type_uint32(v
, name
, &value
, errp
);
303 static void prop_get_fdt(Object
*obj
, Visitor
*v
, const char *name
,
304 void *opaque
, Error
**errp
)
306 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
308 int fdt_offset_next
, fdt_offset
, fdt_depth
;
312 visit_type_null(v
, NULL
, &null
, errp
);
318 fdt_offset
= drc
->fdt_start_offset
;
322 const char *name
= NULL
;
323 const struct fdt_property
*prop
= NULL
;
324 int prop_len
= 0, name_len
= 0;
328 tag
= fdt_next_tag(fdt
, fdt_offset
, &fdt_offset_next
);
332 name
= fdt_get_name(fdt
, fdt_offset
, &name_len
);
333 if (!visit_start_struct(v
, name
, NULL
, 0, errp
)) {
338 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
339 g_assert(fdt_depth
> 0);
340 ok
= visit_check_struct(v
, errp
);
341 visit_end_struct(v
, NULL
);
349 prop
= fdt_get_property_by_offset(fdt
, fdt_offset
, &prop_len
);
350 name
= fdt_string(fdt
, fdt32_to_cpu(prop
->nameoff
));
351 if (!visit_start_list(v
, name
, NULL
, 0, errp
)) {
354 for (i
= 0; i
< prop_len
; i
++) {
355 if (!visit_type_uint8(v
, NULL
, (uint8_t *)&prop
->data
[i
],
360 ok
= visit_check_list(v
, errp
);
361 visit_end_list(v
, NULL
);
368 error_report("device FDT in unexpected state: %d", tag
);
371 fdt_offset
= fdt_offset_next
;
372 } while (fdt_depth
!= 0);
375 static void spapr_drc_start_unplug_timeout_timer(SpaprDrc
*drc
)
377 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
379 if (drck
->unplug_timeout_seconds
!= 0) {
380 timer_mod(drc
->unplug_timeout_timer
,
381 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
382 drck
->unplug_timeout_seconds
* 1000);
386 void spapr_drc_attach(SpaprDrc
*drc
, DeviceState
*d
)
388 trace_spapr_drc_attach(spapr_drc_index(drc
));
391 g_assert((drc
->state
== SPAPR_DRC_STATE_LOGICAL_UNUSABLE
)
392 || (drc
->state
== SPAPR_DRC_STATE_PHYSICAL_POWERON
));
396 object_property_add_link(OBJECT(drc
), "device",
397 object_get_typename(OBJECT(drc
->dev
)),
398 (Object
**)(&drc
->dev
),
402 void spapr_drc_unplug_request(SpaprDrc
*drc
)
404 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
406 trace_spapr_drc_unplug_request(spapr_drc_index(drc
));
410 drc
->unplug_requested
= true;
412 spapr_drc_start_unplug_timeout_timer(drc
);
414 if (drc
->state
!= drck
->empty_state
) {
415 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc
));
419 spapr_drc_release(drc
);
422 int spapr_drc_unplug_timeout_remaining_sec(SpaprDrc
*drc
)
424 if (drc
->unplug_requested
) {
425 return timer_deadline_ms(drc
->unplug_timeout_timer
) / 1000;
431 bool spapr_drc_reset(SpaprDrc
*drc
)
433 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
434 bool unplug_completed
= false;
436 trace_spapr_drc_reset(spapr_drc_index(drc
));
438 /* immediately upon reset we can safely assume DRCs whose devices
439 * are pending removal can be safely removed.
441 if (drc
->unplug_requested
) {
442 spapr_drc_release(drc
);
443 unplug_completed
= true;
447 /* A device present at reset is ready to go, same as coldplugged */
448 drc
->state
= drck
->ready_state
;
450 * Ensure that we are able to send the FDT fragment again
451 * via configure-connector call if the guest requests.
453 drc
->ccs_offset
= drc
->fdt_start_offset
;
456 drc
->state
= drck
->empty_state
;
457 drc
->ccs_offset
= -1;
461 return unplug_completed
;
464 static bool spapr_drc_unplug_requested_needed(void *opaque
)
466 return spapr_drc_unplug_requested(opaque
);
469 static const VMStateDescription vmstate_spapr_drc_unplug_requested
= {
470 .name
= "spapr_drc/unplug_requested",
472 .minimum_version_id
= 1,
473 .needed
= spapr_drc_unplug_requested_needed
,
474 .fields
= (VMStateField
[]) {
475 VMSTATE_BOOL(unplug_requested
, SpaprDrc
),
476 VMSTATE_END_OF_LIST()
480 static bool spapr_drc_needed(void *opaque
)
482 SpaprDrc
*drc
= opaque
;
483 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
486 * If no dev is plugged in there is no need to migrate the DRC state
487 * nor to reset the DRC at CAS.
494 * We need to reset the DRC at CAS or to migrate the DRC state if it's
495 * not equal to the expected long-term state, which is the same as the
496 * coldplugged initial state, or if an unplug request is pending.
498 return drc
->state
!= drck
->ready_state
||
499 spapr_drc_unplug_requested(drc
);
502 static int spapr_drc_post_load(void *opaque
, int version_id
)
504 SpaprDrc
*drc
= opaque
;
506 if (drc
->unplug_requested
) {
507 spapr_drc_start_unplug_timeout_timer(drc
);
513 static const VMStateDescription vmstate_spapr_drc
= {
516 .minimum_version_id
= 1,
517 .needed
= spapr_drc_needed
,
518 .post_load
= spapr_drc_post_load
,
519 .fields
= (VMStateField
[]) {
520 VMSTATE_UINT32(state
, SpaprDrc
),
521 VMSTATE_END_OF_LIST()
523 .subsections
= (const VMStateDescription
* []) {
524 &vmstate_spapr_drc_unplug_requested
,
529 static void drc_unplug_timeout_cb(void *opaque
)
531 SpaprDrc
*drc
= opaque
;
533 if (drc
->unplug_requested
) {
534 drc
->unplug_requested
= false;
538 static void drc_realize(DeviceState
*d
, Error
**errp
)
540 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(d
);
541 Object
*root_container
;
543 const char *child_name
;
545 trace_spapr_drc_realize(spapr_drc_index(drc
));
546 /* NOTE: we do this as part of realize/unrealize due to the fact
547 * that the guest will communicate with the DRC via RTAS calls
548 * referencing the global DRC index. By unlinking the DRC
549 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
550 * inaccessible by the guest, since lookups rely on this path
551 * existing in the composition tree
553 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
554 link_name
= g_strdup_printf("%x", spapr_drc_index(drc
));
555 child_name
= object_get_canonical_path_component(OBJECT(drc
));
556 trace_spapr_drc_realize_child(spapr_drc_index(drc
), child_name
);
557 object_property_add_alias(root_container
, link_name
,
558 drc
->owner
, child_name
);
561 drc
->unplug_timeout_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
562 drc_unplug_timeout_cb
,
565 vmstate_register(VMSTATE_IF(drc
), spapr_drc_index(drc
), &vmstate_spapr_drc
,
567 trace_spapr_drc_realize_complete(spapr_drc_index(drc
));
570 static void drc_unrealize(DeviceState
*d
)
572 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(d
);
573 Object
*root_container
;
576 trace_spapr_drc_unrealize(spapr_drc_index(drc
));
577 vmstate_unregister(VMSTATE_IF(drc
), &vmstate_spapr_drc
, drc
);
578 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
579 name
= g_strdup_printf("%x", spapr_drc_index(drc
));
580 object_property_del(root_container
, name
);
582 timer_free(drc
->unplug_timeout_timer
);
585 SpaprDrc
*spapr_dr_connector_new(Object
*owner
, const char *type
,
588 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(object_new(type
));
593 prop_name
= g_strdup_printf("dr-connector[%"PRIu32
"]",
594 spapr_drc_index(drc
));
595 object_property_add_child(owner
, prop_name
, OBJECT(drc
));
596 object_unref(OBJECT(drc
));
597 qdev_realize(DEVICE(drc
), NULL
, NULL
);
603 static void spapr_dr_connector_instance_init(Object
*obj
)
605 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(obj
);
606 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
608 object_property_add_uint32_ptr(obj
, "id", &drc
->id
, OBJ_PROP_FLAG_READ
);
609 object_property_add(obj
, "index", "uint32", prop_get_index
,
611 object_property_add(obj
, "fdt", "struct", prop_get_fdt
,
613 drc
->state
= drck
->empty_state
;
616 static void spapr_dr_connector_class_init(ObjectClass
*k
, void *data
)
618 DeviceClass
*dk
= DEVICE_CLASS(k
);
620 dk
->realize
= drc_realize
;
621 dk
->unrealize
= drc_unrealize
;
623 * Reason: DR connector needs to be wired to either the machine or to a
624 * PHB in spapr_dr_connector_new().
626 dk
->user_creatable
= false;
629 static bool drc_physical_needed(void *opaque
)
631 SpaprDrcPhysical
*drcp
= (SpaprDrcPhysical
*)opaque
;
632 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(drcp
);
634 if ((drc
->dev
&& (drcp
->dr_indicator
== SPAPR_DR_INDICATOR_ACTIVE
))
635 || (!drc
->dev
&& (drcp
->dr_indicator
== SPAPR_DR_INDICATOR_INACTIVE
))) {
641 static const VMStateDescription vmstate_spapr_drc_physical
= {
642 .name
= "spapr_drc/physical",
644 .minimum_version_id
= 1,
645 .needed
= drc_physical_needed
,
646 .fields
= (VMStateField
[]) {
647 VMSTATE_UINT32(dr_indicator
, SpaprDrcPhysical
),
648 VMSTATE_END_OF_LIST()
652 static void drc_physical_reset(void *opaque
)
654 SpaprDrc
*drc
= SPAPR_DR_CONNECTOR(opaque
);
655 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(drc
);
658 drcp
->dr_indicator
= SPAPR_DR_INDICATOR_ACTIVE
;
660 drcp
->dr_indicator
= SPAPR_DR_INDICATOR_INACTIVE
;
664 static void realize_physical(DeviceState
*d
, Error
**errp
)
666 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(d
);
667 Error
*local_err
= NULL
;
669 drc_realize(d
, &local_err
);
671 error_propagate(errp
, local_err
);
675 vmstate_register(VMSTATE_IF(drcp
),
676 spapr_drc_index(SPAPR_DR_CONNECTOR(drcp
)),
677 &vmstate_spapr_drc_physical
, drcp
);
678 qemu_register_reset(drc_physical_reset
, drcp
);
681 static void unrealize_physical(DeviceState
*d
)
683 SpaprDrcPhysical
*drcp
= SPAPR_DRC_PHYSICAL(d
);
686 vmstate_unregister(VMSTATE_IF(drcp
), &vmstate_spapr_drc_physical
, drcp
);
687 qemu_unregister_reset(drc_physical_reset
, drcp
);
690 static void spapr_drc_physical_class_init(ObjectClass
*k
, void *data
)
692 DeviceClass
*dk
= DEVICE_CLASS(k
);
693 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
695 dk
->realize
= realize_physical
;
696 dk
->unrealize
= unrealize_physical
;
697 drck
->dr_entity_sense
= physical_entity_sense
;
698 drck
->isolate
= drc_isolate_physical
;
699 drck
->unisolate
= drc_unisolate_physical
;
700 drck
->ready_state
= SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
;
701 drck
->empty_state
= SPAPR_DRC_STATE_PHYSICAL_POWERON
;
704 static void spapr_drc_logical_class_init(ObjectClass
*k
, void *data
)
706 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
708 drck
->dr_entity_sense
= logical_entity_sense
;
709 drck
->isolate
= drc_isolate_logical
;
710 drck
->unisolate
= drc_unisolate_logical
;
711 drck
->ready_state
= SPAPR_DRC_STATE_LOGICAL_CONFIGURED
;
712 drck
->empty_state
= SPAPR_DRC_STATE_LOGICAL_UNUSABLE
;
715 static void spapr_drc_cpu_class_init(ObjectClass
*k
, void *data
)
717 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
719 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU
;
720 drck
->typename
= "CPU";
721 drck
->drc_name_prefix
= "CPU ";
722 drck
->release
= spapr_core_release
;
723 drck
->dt_populate
= spapr_core_dt_populate
;
724 drck
->unplug_timeout_seconds
= 15;
727 static void spapr_drc_pci_class_init(ObjectClass
*k
, void *data
)
729 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
731 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI
;
732 drck
->typename
= "28";
733 drck
->drc_name_prefix
= "C";
734 drck
->release
= spapr_phb_remove_pci_device_cb
;
735 drck
->dt_populate
= spapr_pci_dt_populate
;
738 static void spapr_drc_lmb_class_init(ObjectClass
*k
, void *data
)
740 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
742 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB
;
743 drck
->typename
= "MEM";
744 drck
->drc_name_prefix
= "LMB ";
745 drck
->release
= spapr_lmb_release
;
746 drck
->dt_populate
= spapr_lmb_dt_populate
;
749 static void spapr_drc_phb_class_init(ObjectClass
*k
, void *data
)
751 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
753 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB
;
754 drck
->typename
= "PHB";
755 drck
->drc_name_prefix
= "PHB ";
756 drck
->release
= spapr_phb_release
;
757 drck
->dt_populate
= spapr_phb_dt_populate
;
760 static void spapr_drc_pmem_class_init(ObjectClass
*k
, void *data
)
762 SpaprDrcClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
764 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM
;
765 drck
->typename
= "PMEM";
766 drck
->drc_name_prefix
= "PMEM ";
767 drck
->release
= NULL
;
768 drck
->dt_populate
= spapr_pmem_dt_populate
;
771 static const TypeInfo spapr_dr_connector_info
= {
772 .name
= TYPE_SPAPR_DR_CONNECTOR
,
773 .parent
= TYPE_DEVICE
,
774 .instance_size
= sizeof(SpaprDrc
),
775 .instance_init
= spapr_dr_connector_instance_init
,
776 .class_size
= sizeof(SpaprDrcClass
),
777 .class_init
= spapr_dr_connector_class_init
,
781 static const TypeInfo spapr_drc_physical_info
= {
782 .name
= TYPE_SPAPR_DRC_PHYSICAL
,
783 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
784 .instance_size
= sizeof(SpaprDrcPhysical
),
785 .class_init
= spapr_drc_physical_class_init
,
789 static const TypeInfo spapr_drc_logical_info
= {
790 .name
= TYPE_SPAPR_DRC_LOGICAL
,
791 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
792 .class_init
= spapr_drc_logical_class_init
,
796 static const TypeInfo spapr_drc_cpu_info
= {
797 .name
= TYPE_SPAPR_DRC_CPU
,
798 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
799 .class_init
= spapr_drc_cpu_class_init
,
802 static const TypeInfo spapr_drc_pci_info
= {
803 .name
= TYPE_SPAPR_DRC_PCI
,
804 .parent
= TYPE_SPAPR_DRC_PHYSICAL
,
805 .class_init
= spapr_drc_pci_class_init
,
808 static const TypeInfo spapr_drc_lmb_info
= {
809 .name
= TYPE_SPAPR_DRC_LMB
,
810 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
811 .class_init
= spapr_drc_lmb_class_init
,
814 static const TypeInfo spapr_drc_phb_info
= {
815 .name
= TYPE_SPAPR_DRC_PHB
,
816 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
817 .instance_size
= sizeof(SpaprDrc
),
818 .class_init
= spapr_drc_phb_class_init
,
821 static const TypeInfo spapr_drc_pmem_info
= {
822 .name
= TYPE_SPAPR_DRC_PMEM
,
823 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
824 .class_init
= spapr_drc_pmem_class_init
,
827 /* helper functions for external users */
829 SpaprDrc
*spapr_drc_by_index(uint32_t index
)
834 name
= g_strdup_printf("%s/%x", DRC_CONTAINER_PATH
, index
);
835 obj
= object_resolve_path(name
, NULL
);
838 return !obj
? NULL
: SPAPR_DR_CONNECTOR(obj
);
841 SpaprDrc
*spapr_drc_by_id(const char *type
, uint32_t id
)
844 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type
));
846 return spapr_drc_by_index(drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
847 | (id
& DRC_INDEX_ID_MASK
));
853 * @fdt: libfdt device tree
854 * @path: path in the DT to generate properties
855 * @owner: parent Object/DeviceState for which to generate DRC
857 * @drc_type_mask: mask of SpaprDrcType values corresponding
858 * to the types of DRCs to generate entries for
860 * generate OF properties to describe DRC topology/indices to guests
862 * as documented in PAPR+ v2.1, 13.5.2
864 int spapr_dt_drc(void *fdt
, int offset
, Object
*owner
, uint32_t drc_type_mask
)
866 Object
*root_container
;
867 ObjectProperty
*prop
;
868 ObjectPropertyIterator iter
;
869 uint32_t drc_count
= 0;
870 GArray
*drc_indexes
, *drc_power_domains
;
871 GString
*drc_names
, *drc_types
;
875 * This should really be only called once per node since it overwrites
876 * the OF properties if they already exist.
878 g_assert(!fdt_get_property(fdt
, offset
, "ibm,drc-indexes", NULL
));
880 /* the first entry of each properties is a 32-bit integer encoding
881 * the number of elements in the array. we won't know this until
882 * we complete the iteration through all the matching DRCs, but
883 * reserve the space now and set the offsets accordingly so we
884 * can fill them in later.
886 drc_indexes
= g_array_new(false, true, sizeof(uint32_t));
887 drc_indexes
= g_array_set_size(drc_indexes
, 1);
888 drc_power_domains
= g_array_new(false, true, sizeof(uint32_t));
889 drc_power_domains
= g_array_set_size(drc_power_domains
, 1);
890 drc_names
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
891 drc_types
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
893 /* aliases for all DRConnector objects will be rooted in QOM
894 * composition tree at DRC_CONTAINER_PATH
896 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
898 object_property_iter_init(&iter
, root_container
);
899 while ((prop
= object_property_iter_next(&iter
))) {
903 char *drc_name
= NULL
;
904 uint32_t drc_index
, drc_power_domain
;
906 if (!strstart(prop
->type
, "link<", NULL
)) {
910 obj
= object_property_get_link(root_container
, prop
->name
,
912 drc
= SPAPR_DR_CONNECTOR(obj
);
913 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
915 if (owner
&& (drc
->owner
!= owner
)) {
919 if ((spapr_drc_type(drc
) & drc_type_mask
) == 0) {
925 /* ibm,drc-indexes */
926 drc_index
= cpu_to_be32(spapr_drc_index(drc
));
927 g_array_append_val(drc_indexes
, drc_index
);
929 /* ibm,drc-power-domains */
930 drc_power_domain
= cpu_to_be32(-1);
931 g_array_append_val(drc_power_domains
, drc_power_domain
);
934 drc_name
= spapr_drc_name(drc
);
935 drc_names
= g_string_append(drc_names
, drc_name
);
936 drc_names
= g_string_insert_len(drc_names
, -1, "\0", 1);
940 drc_types
= g_string_append(drc_types
, drck
->typename
);
941 drc_types
= g_string_insert_len(drc_types
, -1, "\0", 1);
944 /* now write the drc count into the space we reserved at the
945 * beginning of the arrays previously
947 *(uint32_t *)drc_indexes
->data
= cpu_to_be32(drc_count
);
948 *(uint32_t *)drc_power_domains
->data
= cpu_to_be32(drc_count
);
949 *(uint32_t *)drc_names
->str
= cpu_to_be32(drc_count
);
950 *(uint32_t *)drc_types
->str
= cpu_to_be32(drc_count
);
952 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-indexes",
954 drc_indexes
->len
* sizeof(uint32_t));
956 error_report("Couldn't create ibm,drc-indexes property");
960 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-power-domains",
961 drc_power_domains
->data
,
962 drc_power_domains
->len
* sizeof(uint32_t));
964 error_report("Couldn't finalize ibm,drc-power-domains property");
968 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-names",
969 drc_names
->str
, drc_names
->len
);
971 error_report("Couldn't finalize ibm,drc-names property");
975 ret
= fdt_setprop(fdt
, offset
, "ibm,drc-types",
976 drc_types
->str
, drc_types
->len
);
978 error_report("Couldn't finalize ibm,drc-types property");
983 g_array_free(drc_indexes
, true);
984 g_array_free(drc_power_domains
, true);
985 g_string_free(drc_names
, true);
986 g_string_free(drc_types
, true);
991 void spapr_drc_reset_all(SpaprMachineState
*spapr
)
993 Object
*drc_container
;
994 ObjectProperty
*prop
;
995 ObjectPropertyIterator iter
;
997 drc_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
999 object_property_iter_init(&iter
, drc_container
);
1000 while ((prop
= object_property_iter_next(&iter
))) {
1003 if (!strstart(prop
->type
, "link<", NULL
)) {
1006 drc
= SPAPR_DR_CONNECTOR(object_property_get_link(drc_container
,
1011 * This will complete any pending plug/unplug requests.
1012 * In case of a unplugged PHB or PCI bridge, this will
1013 * cause some DRCs to be destroyed and thus potentially
1014 * invalidate the iterator.
1016 if (spapr_drc_reset(drc
)) {
1026 static uint32_t rtas_set_isolation_state(uint32_t idx
, uint32_t state
)
1028 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
1029 SpaprDrcClass
*drck
;
1032 return RTAS_OUT_NO_SUCH_INDICATOR
;
1035 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc
), state
);
1037 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1040 case SPAPR_DR_ISOLATION_STATE_ISOLATED
:
1041 return drck
->isolate(drc
);
1043 case SPAPR_DR_ISOLATION_STATE_UNISOLATED
:
1044 return drck
->unisolate(drc
);
1047 return RTAS_OUT_PARAM_ERROR
;
1051 static uint32_t rtas_set_allocation_state(uint32_t idx
, uint32_t state
)
1053 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
1055 if (!drc
|| !object_dynamic_cast(OBJECT(drc
), TYPE_SPAPR_DRC_LOGICAL
)) {
1056 return RTAS_OUT_NO_SUCH_INDICATOR
;
1059 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc
), state
);
1062 case SPAPR_DR_ALLOCATION_STATE_USABLE
:
1063 return drc_set_usable(drc
);
1065 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE
:
1066 return drc_set_unusable(drc
);
1069 return RTAS_OUT_PARAM_ERROR
;
1073 static uint32_t rtas_set_dr_indicator(uint32_t idx
, uint32_t state
)
1075 SpaprDrc
*drc
= spapr_drc_by_index(idx
);
1077 if (!drc
|| !object_dynamic_cast(OBJECT(drc
), TYPE_SPAPR_DRC_PHYSICAL
)) {
1078 return RTAS_OUT_NO_SUCH_INDICATOR
;
1080 if ((state
!= SPAPR_DR_INDICATOR_INACTIVE
)
1081 && (state
!= SPAPR_DR_INDICATOR_ACTIVE
)
1082 && (state
!= SPAPR_DR_INDICATOR_IDENTIFY
)
1083 && (state
!= SPAPR_DR_INDICATOR_ACTION
)) {
1084 return RTAS_OUT_PARAM_ERROR
; /* bad state parameter */
1087 trace_spapr_drc_set_dr_indicator(idx
, state
);
1088 SPAPR_DRC_PHYSICAL(drc
)->dr_indicator
= state
;
1089 return RTAS_OUT_SUCCESS
;
1092 static void rtas_set_indicator(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
1094 uint32_t nargs
, target_ulong args
,
1095 uint32_t nret
, target_ulong rets
)
1097 uint32_t type
, idx
, state
;
1098 uint32_t ret
= RTAS_OUT_SUCCESS
;
1100 if (nargs
!= 3 || nret
!= 1) {
1101 ret
= RTAS_OUT_PARAM_ERROR
;
1105 type
= rtas_ld(args
, 0);
1106 idx
= rtas_ld(args
, 1);
1107 state
= rtas_ld(args
, 2);
1110 case RTAS_SENSOR_TYPE_ISOLATION_STATE
:
1111 ret
= rtas_set_isolation_state(idx
, state
);
1113 case RTAS_SENSOR_TYPE_DR
:
1114 ret
= rtas_set_dr_indicator(idx
, state
);
1116 case RTAS_SENSOR_TYPE_ALLOCATION_STATE
:
1117 ret
= rtas_set_allocation_state(idx
, state
);
1120 ret
= RTAS_OUT_NOT_SUPPORTED
;
1124 rtas_st(rets
, 0, ret
);
1127 static void rtas_get_sensor_state(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
1128 uint32_t token
, uint32_t nargs
,
1129 target_ulong args
, uint32_t nret
,
1132 uint32_t sensor_type
;
1133 uint32_t sensor_index
;
1134 uint32_t sensor_state
= 0;
1136 SpaprDrcClass
*drck
;
1137 uint32_t ret
= RTAS_OUT_SUCCESS
;
1139 if (nargs
!= 2 || nret
!= 2) {
1140 ret
= RTAS_OUT_PARAM_ERROR
;
1144 sensor_type
= rtas_ld(args
, 0);
1145 sensor_index
= rtas_ld(args
, 1);
1147 if (sensor_type
!= RTAS_SENSOR_TYPE_ENTITY_SENSE
) {
1148 /* currently only DR-related sensors are implemented */
1149 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index
,
1151 ret
= RTAS_OUT_NOT_SUPPORTED
;
1155 drc
= spapr_drc_by_index(sensor_index
);
1157 trace_spapr_rtas_get_sensor_state_invalid(sensor_index
);
1158 ret
= RTAS_OUT_PARAM_ERROR
;
1161 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1162 sensor_state
= drck
->dr_entity_sense(drc
);
1165 rtas_st(rets
, 0, ret
);
1166 rtas_st(rets
, 1, sensor_state
);
1169 /* configure-connector work area offsets, int32_t units for field
1170 * indexes, bytes for field offset/len values.
1172 * as documented by PAPR+ v2.7, 13.5.3.5
1174 #define CC_IDX_NODE_NAME_OFFSET 2
1175 #define CC_IDX_PROP_NAME_OFFSET 2
1176 #define CC_IDX_PROP_LEN 3
1177 #define CC_IDX_PROP_DATA_OFFSET 4
1178 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1179 #define CC_WA_LEN 4096
1181 static void configure_connector_st(target_ulong addr
, target_ulong offset
,
1182 const void *buf
, size_t len
)
1184 cpu_physical_memory_write(ppc64_phys_to_real(addr
+ offset
),
1185 buf
, MIN(len
, CC_WA_LEN
- offset
));
1188 static void rtas_ibm_configure_connector(PowerPCCPU
*cpu
,
1189 SpaprMachineState
*spapr
,
1190 uint32_t token
, uint32_t nargs
,
1191 target_ulong args
, uint32_t nret
,
1198 SpaprDrcClass
*drck
;
1199 SpaprDRCCResponse resp
= SPAPR_DR_CC_RESPONSE_CONTINUE
;
1202 if (nargs
!= 2 || nret
!= 1) {
1203 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
1207 wa_addr
= ((uint64_t)rtas_ld(args
, 1) << 32) | rtas_ld(args
, 0);
1209 drc_index
= rtas_ld(wa_addr
, 0);
1210 drc
= spapr_drc_by_index(drc_index
);
1212 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index
);
1213 rc
= RTAS_OUT_PARAM_ERROR
;
1217 if ((drc
->state
!= SPAPR_DRC_STATE_LOGICAL_UNISOLATE
)
1218 && (drc
->state
!= SPAPR_DRC_STATE_PHYSICAL_UNISOLATE
)
1219 && (drc
->state
!= SPAPR_DRC_STATE_LOGICAL_CONFIGURED
)
1220 && (drc
->state
!= SPAPR_DRC_STATE_PHYSICAL_CONFIGURED
)) {
1222 * Need to unisolate the device before configuring
1223 * or it should already be in configured state to
1224 * allow configure-connector be called repeatedly.
1226 rc
= SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE
;
1230 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
1233 * This indicates that the kernel is reconfiguring a LMB due to
1234 * a failed hotunplug. Rollback the DIMM unplug process.
1236 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_LMB
&&
1237 drc
->unplug_requested
) {
1238 spapr_memory_unplug_rollback(spapr
, drc
->dev
);
1245 fdt
= create_device_tree(&fdt_size
);
1247 if (drck
->dt_populate(drc
, spapr
, fdt
, &drc
->fdt_start_offset
,
1250 rc
= SPAPR_DR_CC_RESPONSE_ERROR
;
1255 drc
->ccs_offset
= drc
->fdt_start_offset
;
1262 const struct fdt_property
*prop
;
1263 int fdt_offset_next
, prop_len
;
1265 tag
= fdt_next_tag(drc
->fdt
, drc
->ccs_offset
, &fdt_offset_next
);
1268 case FDT_BEGIN_NODE
:
1270 name
= fdt_get_name(drc
->fdt
, drc
->ccs_offset
, NULL
);
1272 /* provide the name of the next OF node */
1273 wa_offset
= CC_VAL_DATA_OFFSET
;
1274 rtas_st(wa_addr
, CC_IDX_NODE_NAME_OFFSET
, wa_offset
);
1275 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1276 resp
= SPAPR_DR_CC_RESPONSE_NEXT_CHILD
;
1280 if (drc
->ccs_depth
== 0) {
1281 uint32_t drc_index
= spapr_drc_index(drc
);
1283 /* done sending the device tree, move to configured state */
1284 trace_spapr_drc_set_configured(drc_index
);
1285 drc
->state
= drck
->ready_state
;
1287 * Ensure that we are able to send the FDT fragment
1288 * again via configure-connector call if the guest requests.
1290 drc
->ccs_offset
= drc
->fdt_start_offset
;
1292 fdt_offset_next
= drc
->fdt_start_offset
;
1293 resp
= SPAPR_DR_CC_RESPONSE_SUCCESS
;
1295 resp
= SPAPR_DR_CC_RESPONSE_PREV_PARENT
;
1299 prop
= fdt_get_property_by_offset(drc
->fdt
, drc
->ccs_offset
,
1301 name
= fdt_string(drc
->fdt
, fdt32_to_cpu(prop
->nameoff
));
1303 /* provide the name of the next OF property */
1304 wa_offset
= CC_VAL_DATA_OFFSET
;
1305 rtas_st(wa_addr
, CC_IDX_PROP_NAME_OFFSET
, wa_offset
);
1306 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1308 /* provide the length and value of the OF property. data gets
1309 * placed immediately after NULL terminator of the OF property's
1312 wa_offset
+= strlen(name
) + 1,
1313 rtas_st(wa_addr
, CC_IDX_PROP_LEN
, prop_len
);
1314 rtas_st(wa_addr
, CC_IDX_PROP_DATA_OFFSET
, wa_offset
);
1315 configure_connector_st(wa_addr
, wa_offset
, prop
->data
, prop_len
);
1316 resp
= SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY
;
1319 resp
= SPAPR_DR_CC_RESPONSE_ERROR
;
1321 /* keep seeking for an actionable tag */
1324 if (drc
->ccs_offset
>= 0) {
1325 drc
->ccs_offset
= fdt_offset_next
;
1327 } while (resp
== SPAPR_DR_CC_RESPONSE_CONTINUE
);
1331 rtas_st(rets
, 0, rc
);
1334 static void spapr_drc_register_types(void)
1336 type_register_static(&spapr_dr_connector_info
);
1337 type_register_static(&spapr_drc_physical_info
);
1338 type_register_static(&spapr_drc_logical_info
);
1339 type_register_static(&spapr_drc_cpu_info
);
1340 type_register_static(&spapr_drc_pci_info
);
1341 type_register_static(&spapr_drc_lmb_info
);
1342 type_register_static(&spapr_drc_phb_info
);
1343 type_register_static(&spapr_drc_pmem_info
);
1345 spapr_rtas_register(RTAS_SET_INDICATOR
, "set-indicator",
1346 rtas_set_indicator
);
1347 spapr_rtas_register(RTAS_GET_SENSOR_STATE
, "get-sensor-state",
1348 rtas_get_sensor_state
);
1349 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR
, "ibm,configure-connector",
1350 rtas_ibm_configure_connector
);
1352 type_init(spapr_drc_register_types
)