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"
16 #include "qemu/cutils.h"
17 #include "hw/ppc/spapr_drc.h"
18 #include "qom/object.h"
20 #include "qapi/visitor.h"
21 #include "qemu/error-report.h"
22 #include "hw/ppc/spapr.h" /* for RTAS return codes */
23 #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
26 #define DRC_CONTAINER_PATH "/dr-connector"
27 #define DRC_INDEX_TYPE_SHIFT 28
28 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
30 sPAPRDRConnectorType
spapr_drc_type(sPAPRDRConnector
*drc
)
32 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
34 return 1 << drck
->typeshift
;
37 uint32_t spapr_drc_index(sPAPRDRConnector
*drc
)
39 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
41 /* no set format for a drc index: it only needs to be globally
42 * unique. this is how we encode the DRC type on bare-metal
43 * however, so might as well do that here
45 return (drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
)
46 | (drc
->id
& DRC_INDEX_ID_MASK
);
49 static uint32_t set_isolation_state(sPAPRDRConnector
*drc
,
50 sPAPRDRIsolationState state
)
52 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc
), state
);
54 /* if the guest is configuring a device attached to this DRC, we
55 * should reset the configuration state at this point since it may
56 * no longer be reliable (guest released device and needs to start
57 * over, or unplug occurred so the FDT is no longer valid)
59 if (state
== SPAPR_DR_ISOLATION_STATE_ISOLATED
) {
64 if (state
== SPAPR_DR_ISOLATION_STATE_UNISOLATED
) {
65 /* cannot unisolate a non-existent resource, and, or resources
66 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
69 drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_UNUSABLE
) {
70 return RTAS_OUT_NO_SUCH_INDICATOR
;
75 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
76 * belong to a DIMM device that is marked for removal.
78 * Currently the guest userspace tool drmgr that drives the memory
79 * hotplug/unplug will just try to remove a set of 'removable' LMBs
80 * in response to a hot unplug request that is based on drc-count.
81 * If the LMB being removed doesn't belong to a DIMM device that is
82 * actually being unplugged, fail the isolation request here.
84 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_LMB
) {
85 if ((state
== SPAPR_DR_ISOLATION_STATE_ISOLATED
) &&
86 !drc
->awaiting_release
) {
87 return RTAS_OUT_HW_ERROR
;
91 drc
->isolation_state
= state
;
93 if (drc
->isolation_state
== SPAPR_DR_ISOLATION_STATE_ISOLATED
) {
94 /* if we're awaiting release, but still in an unconfigured state,
95 * it's likely the guest is still in the process of configuring
96 * the device and is transitioning the devices to an ISOLATED
97 * state as a part of that process. so we only complete the
98 * removal when this transition happens for a device in a
99 * configured state, as suggested by the state diagram from
102 if (drc
->awaiting_release
) {
103 uint32_t drc_index
= spapr_drc_index(drc
);
104 if (drc
->configured
) {
105 trace_spapr_drc_set_isolation_state_finalizing(drc_index
);
106 spapr_drc_detach(drc
, DEVICE(drc
->dev
), NULL
);
108 trace_spapr_drc_set_isolation_state_deferring(drc_index
);
111 drc
->configured
= false;
114 return RTAS_OUT_SUCCESS
;
117 static uint32_t set_allocation_state(sPAPRDRConnector
*drc
,
118 sPAPRDRAllocationState state
)
120 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc
), state
);
122 if (state
== SPAPR_DR_ALLOCATION_STATE_USABLE
) {
123 /* if there's no resource/device associated with the DRC, there's
124 * no way for us to put it in an allocation state consistent with
125 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
126 * result in an RTAS return code of -3 / "no such indicator"
129 return RTAS_OUT_NO_SUCH_INDICATOR
;
133 if (spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PCI
) {
134 drc
->allocation_state
= state
;
135 if (drc
->awaiting_release
&&
136 drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_UNUSABLE
) {
137 uint32_t drc_index
= spapr_drc_index(drc
);
138 trace_spapr_drc_set_allocation_state_finalizing(drc_index
);
139 spapr_drc_detach(drc
, DEVICE(drc
->dev
), NULL
);
140 } else if (drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_USABLE
) {
141 drc
->awaiting_allocation
= false;
144 return RTAS_OUT_SUCCESS
;
147 static const char *spapr_drc_name(sPAPRDRConnector
*drc
)
149 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
151 /* human-readable name for a DRC to encode into the DT
152 * description. this is mainly only used within a guest in place
153 * of the unique DRC index.
155 * in the case of VIO/PCI devices, it corresponds to a "location
156 * code" that maps a logical device/function (DRC index) to a
157 * physical (or virtual in the case of VIO) location in the system
158 * by chaining together the "location label" for each
159 * encapsulating component.
161 * since this is more to do with diagnosing physical hardware
162 * issues than guest compatibility, we choose location codes/DRC
163 * names that adhere to the documented format, but avoid encoding
164 * the entire topology information into the label/code, instead
165 * just using the location codes based on the labels for the
166 * endpoints (VIO/PCI adaptor connectors), which is basically just
167 * "C" followed by an integer ID.
169 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
170 * location codes as documented by PAPR+ v2.7, 12.3.1.5
172 return g_strdup_printf("%s%d", drck
->drc_name_prefix
, drc
->id
);
175 /* has the guest been notified of device attachment? */
176 static void set_signalled(sPAPRDRConnector
*drc
)
178 drc
->signalled
= true;
182 * dr-entity-sense sensor value
183 * returned via get-sensor-state RTAS calls
184 * as expected by state diagram in PAPR+ 2.7, 13.4
185 * based on the current allocation/indicator/power states
186 * for the DR connector.
188 static sPAPRDREntitySense
physical_entity_sense(sPAPRDRConnector
*drc
)
190 /* this assumes all PCI devices are assigned to a 'live insertion'
191 * power domain, where QEMU manages power state automatically as
192 * opposed to the guest. present, non-PCI resources are unaffected
195 return drc
->dev
? SPAPR_DR_ENTITY_SENSE_PRESENT
196 : SPAPR_DR_ENTITY_SENSE_EMPTY
;
199 static sPAPRDREntitySense
logical_entity_sense(sPAPRDRConnector
*drc
)
202 && (drc
->allocation_state
!= SPAPR_DR_ALLOCATION_STATE_UNUSABLE
)) {
203 return SPAPR_DR_ENTITY_SENSE_PRESENT
;
205 return SPAPR_DR_ENTITY_SENSE_UNUSABLE
;
209 static void prop_get_index(Object
*obj
, Visitor
*v
, const char *name
,
210 void *opaque
, Error
**errp
)
212 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
213 uint32_t value
= spapr_drc_index(drc
);
214 visit_type_uint32(v
, name
, &value
, errp
);
217 static void prop_get_fdt(Object
*obj
, Visitor
*v
, const char *name
,
218 void *opaque
, Error
**errp
)
220 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
222 int fdt_offset_next
, fdt_offset
, fdt_depth
;
226 visit_type_null(v
, NULL
, errp
);
231 fdt_offset
= drc
->fdt_start_offset
;
235 const char *name
= NULL
;
236 const struct fdt_property
*prop
= NULL
;
237 int prop_len
= 0, name_len
= 0;
240 tag
= fdt_next_tag(fdt
, fdt_offset
, &fdt_offset_next
);
244 name
= fdt_get_name(fdt
, fdt_offset
, &name_len
);
245 visit_start_struct(v
, name
, NULL
, 0, &err
);
247 error_propagate(errp
, err
);
252 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
253 g_assert(fdt_depth
> 0);
254 visit_check_struct(v
, &err
);
255 visit_end_struct(v
, NULL
);
257 error_propagate(errp
, err
);
264 prop
= fdt_get_property_by_offset(fdt
, fdt_offset
, &prop_len
);
265 name
= fdt_string(fdt
, fdt32_to_cpu(prop
->nameoff
));
266 visit_start_list(v
, name
, NULL
, 0, &err
);
268 error_propagate(errp
, err
);
271 for (i
= 0; i
< prop_len
; i
++) {
272 visit_type_uint8(v
, NULL
, (uint8_t *)&prop
->data
[i
], &err
);
274 error_propagate(errp
, err
);
278 visit_check_list(v
, &err
);
279 visit_end_list(v
, NULL
);
281 error_propagate(errp
, err
);
287 error_setg(&error_abort
, "device FDT in unexpected state: %d", tag
);
289 fdt_offset
= fdt_offset_next
;
290 } while (fdt_depth
!= 0);
293 void spapr_drc_attach(sPAPRDRConnector
*drc
, DeviceState
*d
, void *fdt
,
294 int fdt_start_offset
, bool coldplug
, Error
**errp
)
296 trace_spapr_drc_attach(spapr_drc_index(drc
));
298 if (drc
->isolation_state
!= SPAPR_DR_ISOLATION_STATE_ISOLATED
) {
299 error_setg(errp
, "an attached device is still awaiting release");
302 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_PCI
) {
303 g_assert(drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_USABLE
);
305 g_assert(fdt
|| coldplug
);
307 drc
->dr_indicator
= SPAPR_DR_INDICATOR_ACTIVE
;
311 drc
->fdt_start_offset
= fdt_start_offset
;
312 drc
->configured
= coldplug
;
313 /* 'logical' DR resources such as memory/cpus are in some cases treated
314 * as a pool of resources from which the guest is free to choose from
315 * based on only a count. for resources that can be assigned in this
316 * fashion, we must assume the resource is signalled immediately
317 * since a single hotplug request might make an arbitrary number of
318 * such attached resources available to the guest, as opposed to
319 * 'physical' DR resources such as PCI where each device/resource is
320 * signalled individually.
322 drc
->signalled
= (spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PCI
)
325 if (spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PCI
) {
326 drc
->awaiting_allocation
= true;
329 object_property_add_link(OBJECT(drc
), "device",
330 object_get_typename(OBJECT(drc
->dev
)),
331 (Object
**)(&drc
->dev
),
335 void spapr_drc_detach(sPAPRDRConnector
*drc
, DeviceState
*d
, Error
**errp
)
337 trace_spapr_drc_detach(spapr_drc_index(drc
));
339 /* if we've signalled device presence to the guest, or if the guest
340 * has gone ahead and configured the device (via manually-executed
341 * device add via drmgr in guest, namely), we need to wait
342 * for the guest to quiesce the device before completing detach.
343 * Otherwise, we can assume the guest hasn't seen it and complete the
344 * detach immediately. Note that there is a small race window
345 * just before, or during, configuration, which is this context
346 * refers mainly to fetching the device tree via RTAS.
347 * During this window the device access will be arbitrated by
348 * associated DRC, which will simply fail the RTAS calls as invalid.
349 * This is recoverable within guest and current implementations of
350 * drmgr should be able to cope.
352 if (!drc
->signalled
&& !drc
->configured
) {
353 /* if the guest hasn't seen the device we can't rely on it to
354 * set it back to an isolated state via RTAS, so do it here manually
356 drc
->isolation_state
= SPAPR_DR_ISOLATION_STATE_ISOLATED
;
359 if (drc
->isolation_state
!= SPAPR_DR_ISOLATION_STATE_ISOLATED
) {
360 trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc
));
361 drc
->awaiting_release
= true;
365 if (spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PCI
&&
366 drc
->allocation_state
!= SPAPR_DR_ALLOCATION_STATE_UNUSABLE
) {
367 trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc
));
368 drc
->awaiting_release
= true;
372 if (drc
->awaiting_allocation
) {
373 drc
->awaiting_release
= true;
374 trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc
));
378 drc
->dr_indicator
= SPAPR_DR_INDICATOR_INACTIVE
;
380 /* Calling release callbacks based on spapr_drc_type(drc). */
381 switch (spapr_drc_type(drc
)) {
382 case SPAPR_DR_CONNECTOR_TYPE_CPU
:
383 spapr_core_release(drc
->dev
);
385 case SPAPR_DR_CONNECTOR_TYPE_PCI
:
386 spapr_phb_remove_pci_device_cb(drc
->dev
);
388 case SPAPR_DR_CONNECTOR_TYPE_LMB
:
389 spapr_lmb_release(drc
->dev
);
391 case SPAPR_DR_CONNECTOR_TYPE_PHB
:
392 case SPAPR_DR_CONNECTOR_TYPE_VIO
:
397 drc
->awaiting_release
= false;
400 drc
->fdt_start_offset
= 0;
401 object_property_del(OBJECT(drc
), "device", NULL
);
405 static bool release_pending(sPAPRDRConnector
*drc
)
407 return drc
->awaiting_release
;
410 static void reset(DeviceState
*d
)
412 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(d
);
413 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
415 trace_spapr_drc_reset(spapr_drc_index(drc
));
420 /* immediately upon reset we can safely assume DRCs whose devices
421 * are pending removal can be safely removed, and that they will
422 * subsequently be left in an ISOLATED state. move the DRC to this
423 * state in these cases (which will in turn complete any pending
426 if (drc
->awaiting_release
) {
427 drck
->set_isolation_state(drc
, SPAPR_DR_ISOLATION_STATE_ISOLATED
);
428 /* generally this should also finalize the removal, but if the device
429 * hasn't yet been configured we normally defer removal under the
430 * assumption that this transition is taking place as part of device
431 * configuration. so check if we're still waiting after this, and
432 * force removal if we are
434 if (drc
->awaiting_release
) {
435 spapr_drc_detach(drc
, DEVICE(drc
->dev
), NULL
);
438 /* non-PCI devices may be awaiting a transition to UNUSABLE */
439 if (spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PCI
&&
440 drc
->awaiting_release
) {
441 drck
->set_allocation_state(drc
, SPAPR_DR_ALLOCATION_STATE_UNUSABLE
);
445 if (drck
->dr_entity_sense(drc
) == SPAPR_DR_ENTITY_SENSE_PRESENT
) {
446 drck
->set_signalled(drc
);
450 static bool spapr_drc_needed(void *opaque
)
452 sPAPRDRConnector
*drc
= (sPAPRDRConnector
*)opaque
;
453 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
455 sPAPRDREntitySense value
= drck
->dr_entity_sense(drc
);
457 /* If no dev is plugged in there is no need to migrate the DRC state */
458 if (value
!= SPAPR_DR_ENTITY_SENSE_PRESENT
) {
463 * If there is dev plugged in, we need to migrate the DRC state when
464 * it is different from cold-plugged state
466 switch (spapr_drc_type(drc
)) {
467 case SPAPR_DR_CONNECTOR_TYPE_PCI
:
468 case SPAPR_DR_CONNECTOR_TYPE_CPU
:
469 case SPAPR_DR_CONNECTOR_TYPE_LMB
:
470 rc
= !((drc
->isolation_state
== SPAPR_DR_ISOLATION_STATE_UNISOLATED
) &&
471 (drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_USABLE
) &&
472 drc
->configured
&& drc
->signalled
&& !drc
->awaiting_release
);
474 case SPAPR_DR_CONNECTOR_TYPE_PHB
:
475 case SPAPR_DR_CONNECTOR_TYPE_VIO
:
477 g_assert_not_reached();
482 static const VMStateDescription vmstate_spapr_drc
= {
485 .minimum_version_id
= 1,
486 .needed
= spapr_drc_needed
,
487 .fields
= (VMStateField
[]) {
488 VMSTATE_UINT32(isolation_state
, sPAPRDRConnector
),
489 VMSTATE_UINT32(allocation_state
, sPAPRDRConnector
),
490 VMSTATE_UINT32(dr_indicator
, sPAPRDRConnector
),
491 VMSTATE_BOOL(configured
, sPAPRDRConnector
),
492 VMSTATE_BOOL(awaiting_release
, sPAPRDRConnector
),
493 VMSTATE_BOOL(awaiting_allocation
, sPAPRDRConnector
),
494 VMSTATE_BOOL(signalled
, sPAPRDRConnector
),
495 VMSTATE_END_OF_LIST()
499 static void realize(DeviceState
*d
, Error
**errp
)
501 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(d
);
502 Object
*root_container
;
507 trace_spapr_drc_realize(spapr_drc_index(drc
));
508 /* NOTE: we do this as part of realize/unrealize due to the fact
509 * that the guest will communicate with the DRC via RTAS calls
510 * referencing the global DRC index. By unlinking the DRC
511 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
512 * inaccessible by the guest, since lookups rely on this path
513 * existing in the composition tree
515 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
516 snprintf(link_name
, sizeof(link_name
), "%x", spapr_drc_index(drc
));
517 child_name
= object_get_canonical_path_component(OBJECT(drc
));
518 trace_spapr_drc_realize_child(spapr_drc_index(drc
), child_name
);
519 object_property_add_alias(root_container
, link_name
,
520 drc
->owner
, child_name
, &err
);
522 error_report_err(err
);
523 object_unref(OBJECT(drc
));
526 vmstate_register(DEVICE(drc
), spapr_drc_index(drc
), &vmstate_spapr_drc
,
528 trace_spapr_drc_realize_complete(spapr_drc_index(drc
));
531 static void unrealize(DeviceState
*d
, Error
**errp
)
533 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(d
);
534 Object
*root_container
;
538 trace_spapr_drc_unrealize(spapr_drc_index(drc
));
539 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
540 snprintf(name
, sizeof(name
), "%x", spapr_drc_index(drc
));
541 object_property_del(root_container
, name
, &err
);
543 error_report_err(err
);
544 object_unref(OBJECT(drc
));
548 sPAPRDRConnector
*spapr_dr_connector_new(Object
*owner
, const char *type
,
551 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(object_new(type
));
556 prop_name
= g_strdup_printf("dr-connector[%"PRIu32
"]",
557 spapr_drc_index(drc
));
558 object_property_add_child(owner
, prop_name
, OBJECT(drc
), NULL
);
559 object_property_set_bool(OBJECT(drc
), true, "realized", NULL
);
562 /* PCI slot always start in a USABLE state, and stay there */
563 if (spapr_drc_type(drc
) == SPAPR_DR_CONNECTOR_TYPE_PCI
) {
564 drc
->allocation_state
= SPAPR_DR_ALLOCATION_STATE_USABLE
;
570 static void spapr_dr_connector_instance_init(Object
*obj
)
572 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
574 object_property_add_uint32_ptr(obj
, "id", &drc
->id
, NULL
);
575 object_property_add(obj
, "index", "uint32", prop_get_index
,
576 NULL
, NULL
, NULL
, NULL
);
577 object_property_add(obj
, "fdt", "struct", prop_get_fdt
,
578 NULL
, NULL
, NULL
, NULL
);
581 static void spapr_dr_connector_class_init(ObjectClass
*k
, void *data
)
583 DeviceClass
*dk
= DEVICE_CLASS(k
);
584 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
587 dk
->realize
= realize
;
588 dk
->unrealize
= unrealize
;
589 drck
->set_isolation_state
= set_isolation_state
;
590 drck
->set_allocation_state
= set_allocation_state
;
591 drck
->release_pending
= release_pending
;
592 drck
->set_signalled
= set_signalled
;
594 * Reason: it crashes FIXME find and document the real reason
596 dk
->user_creatable
= false;
599 static void spapr_drc_physical_class_init(ObjectClass
*k
, void *data
)
601 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
603 drck
->dr_entity_sense
= physical_entity_sense
;
606 static void spapr_drc_logical_class_init(ObjectClass
*k
, void *data
)
608 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
610 drck
->dr_entity_sense
= logical_entity_sense
;
613 static void spapr_drc_cpu_class_init(ObjectClass
*k
, void *data
)
615 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
617 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU
;
618 drck
->typename
= "CPU";
619 drck
->drc_name_prefix
= "CPU ";
622 static void spapr_drc_pci_class_init(ObjectClass
*k
, void *data
)
624 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
626 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI
;
627 drck
->typename
= "28";
628 drck
->drc_name_prefix
= "C";
631 static void spapr_drc_lmb_class_init(ObjectClass
*k
, void *data
)
633 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
635 drck
->typeshift
= SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB
;
636 drck
->typename
= "MEM";
637 drck
->drc_name_prefix
= "LMB ";
640 static const TypeInfo spapr_dr_connector_info
= {
641 .name
= TYPE_SPAPR_DR_CONNECTOR
,
642 .parent
= TYPE_DEVICE
,
643 .instance_size
= sizeof(sPAPRDRConnector
),
644 .instance_init
= spapr_dr_connector_instance_init
,
645 .class_size
= sizeof(sPAPRDRConnectorClass
),
646 .class_init
= spapr_dr_connector_class_init
,
650 static const TypeInfo spapr_drc_physical_info
= {
651 .name
= TYPE_SPAPR_DRC_PHYSICAL
,
652 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
653 .instance_size
= sizeof(sPAPRDRConnector
),
654 .class_init
= spapr_drc_physical_class_init
,
658 static const TypeInfo spapr_drc_logical_info
= {
659 .name
= TYPE_SPAPR_DRC_LOGICAL
,
660 .parent
= TYPE_SPAPR_DR_CONNECTOR
,
661 .instance_size
= sizeof(sPAPRDRConnector
),
662 .class_init
= spapr_drc_logical_class_init
,
666 static const TypeInfo spapr_drc_cpu_info
= {
667 .name
= TYPE_SPAPR_DRC_CPU
,
668 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
669 .instance_size
= sizeof(sPAPRDRConnector
),
670 .class_init
= spapr_drc_cpu_class_init
,
673 static const TypeInfo spapr_drc_pci_info
= {
674 .name
= TYPE_SPAPR_DRC_PCI
,
675 .parent
= TYPE_SPAPR_DRC_PHYSICAL
,
676 .instance_size
= sizeof(sPAPRDRConnector
),
677 .class_init
= spapr_drc_pci_class_init
,
680 static const TypeInfo spapr_drc_lmb_info
= {
681 .name
= TYPE_SPAPR_DRC_LMB
,
682 .parent
= TYPE_SPAPR_DRC_LOGICAL
,
683 .instance_size
= sizeof(sPAPRDRConnector
),
684 .class_init
= spapr_drc_lmb_class_init
,
687 /* helper functions for external users */
689 sPAPRDRConnector
*spapr_drc_by_index(uint32_t index
)
694 snprintf(name
, sizeof(name
), "%s/%x", DRC_CONTAINER_PATH
, index
);
695 obj
= object_resolve_path(name
, NULL
);
697 return !obj
? NULL
: SPAPR_DR_CONNECTOR(obj
);
700 sPAPRDRConnector
*spapr_drc_by_id(const char *type
, uint32_t id
)
702 sPAPRDRConnectorClass
*drck
703 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type
));
705 return spapr_drc_by_index(drck
->typeshift
<< DRC_INDEX_TYPE_SHIFT
706 | (id
& DRC_INDEX_ID_MASK
));
710 * spapr_drc_populate_dt
712 * @fdt: libfdt device tree
713 * @path: path in the DT to generate properties
714 * @owner: parent Object/DeviceState for which to generate DRC
716 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
717 * to the types of DRCs to generate entries for
719 * generate OF properties to describe DRC topology/indices to guests
721 * as documented in PAPR+ v2.1, 13.5.2
723 int spapr_drc_populate_dt(void *fdt
, int fdt_offset
, Object
*owner
,
724 uint32_t drc_type_mask
)
726 Object
*root_container
;
727 ObjectProperty
*prop
;
728 ObjectPropertyIterator iter
;
729 uint32_t drc_count
= 0;
730 GArray
*drc_indexes
, *drc_power_domains
;
731 GString
*drc_names
, *drc_types
;
734 /* the first entry of each properties is a 32-bit integer encoding
735 * the number of elements in the array. we won't know this until
736 * we complete the iteration through all the matching DRCs, but
737 * reserve the space now and set the offsets accordingly so we
738 * can fill them in later.
740 drc_indexes
= g_array_new(false, true, sizeof(uint32_t));
741 drc_indexes
= g_array_set_size(drc_indexes
, 1);
742 drc_power_domains
= g_array_new(false, true, sizeof(uint32_t));
743 drc_power_domains
= g_array_set_size(drc_power_domains
, 1);
744 drc_names
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
745 drc_types
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
747 /* aliases for all DRConnector objects will be rooted in QOM
748 * composition tree at DRC_CONTAINER_PATH
750 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
752 object_property_iter_init(&iter
, root_container
);
753 while ((prop
= object_property_iter_next(&iter
))) {
755 sPAPRDRConnector
*drc
;
756 sPAPRDRConnectorClass
*drck
;
757 uint32_t drc_index
, drc_power_domain
;
759 if (!strstart(prop
->type
, "link<", NULL
)) {
763 obj
= object_property_get_link(root_container
, prop
->name
, NULL
);
764 drc
= SPAPR_DR_CONNECTOR(obj
);
765 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
767 if (owner
&& (drc
->owner
!= owner
)) {
771 if ((spapr_drc_type(drc
) & drc_type_mask
) == 0) {
777 /* ibm,drc-indexes */
778 drc_index
= cpu_to_be32(spapr_drc_index(drc
));
779 g_array_append_val(drc_indexes
, drc_index
);
781 /* ibm,drc-power-domains */
782 drc_power_domain
= cpu_to_be32(-1);
783 g_array_append_val(drc_power_domains
, drc_power_domain
);
786 drc_names
= g_string_append(drc_names
, spapr_drc_name(drc
));
787 drc_names
= g_string_insert_len(drc_names
, -1, "\0", 1);
790 drc_types
= g_string_append(drc_types
, drck
->typename
);
791 drc_types
= g_string_insert_len(drc_types
, -1, "\0", 1);
794 /* now write the drc count into the space we reserved at the
795 * beginning of the arrays previously
797 *(uint32_t *)drc_indexes
->data
= cpu_to_be32(drc_count
);
798 *(uint32_t *)drc_power_domains
->data
= cpu_to_be32(drc_count
);
799 *(uint32_t *)drc_names
->str
= cpu_to_be32(drc_count
);
800 *(uint32_t *)drc_types
->str
= cpu_to_be32(drc_count
);
802 ret
= fdt_setprop(fdt
, fdt_offset
, "ibm,drc-indexes",
804 drc_indexes
->len
* sizeof(uint32_t));
806 error_report("Couldn't create ibm,drc-indexes property");
810 ret
= fdt_setprop(fdt
, fdt_offset
, "ibm,drc-power-domains",
811 drc_power_domains
->data
,
812 drc_power_domains
->len
* sizeof(uint32_t));
814 error_report("Couldn't finalize ibm,drc-power-domains property");
818 ret
= fdt_setprop(fdt
, fdt_offset
, "ibm,drc-names",
819 drc_names
->str
, drc_names
->len
);
821 error_report("Couldn't finalize ibm,drc-names property");
825 ret
= fdt_setprop(fdt
, fdt_offset
, "ibm,drc-types",
826 drc_types
->str
, drc_types
->len
);
828 error_report("Couldn't finalize ibm,drc-types property");
833 g_array_free(drc_indexes
, true);
834 g_array_free(drc_power_domains
, true);
835 g_string_free(drc_names
, true);
836 g_string_free(drc_types
, true);
845 static uint32_t rtas_set_isolation_state(uint32_t idx
, uint32_t state
)
847 sPAPRDRConnector
*drc
= spapr_drc_by_index(idx
);
848 sPAPRDRConnectorClass
*drck
;
851 return RTAS_OUT_PARAM_ERROR
;
854 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
855 return drck
->set_isolation_state(drc
, state
);
858 static uint32_t rtas_set_allocation_state(uint32_t idx
, uint32_t state
)
860 sPAPRDRConnector
*drc
= spapr_drc_by_index(idx
);
861 sPAPRDRConnectorClass
*drck
;
864 return RTAS_OUT_PARAM_ERROR
;
867 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
868 return drck
->set_allocation_state(drc
, state
);
871 static uint32_t rtas_set_dr_indicator(uint32_t idx
, uint32_t state
)
873 sPAPRDRConnector
*drc
= spapr_drc_by_index(idx
);
876 return RTAS_OUT_PARAM_ERROR
;
879 trace_spapr_drc_set_dr_indicator(idx
, state
);
880 drc
->dr_indicator
= state
;
881 return RTAS_OUT_SUCCESS
;
884 static void rtas_set_indicator(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
886 uint32_t nargs
, target_ulong args
,
887 uint32_t nret
, target_ulong rets
)
889 uint32_t type
, idx
, state
;
890 uint32_t ret
= RTAS_OUT_SUCCESS
;
892 if (nargs
!= 3 || nret
!= 1) {
893 ret
= RTAS_OUT_PARAM_ERROR
;
897 type
= rtas_ld(args
, 0);
898 idx
= rtas_ld(args
, 1);
899 state
= rtas_ld(args
, 2);
902 case RTAS_SENSOR_TYPE_ISOLATION_STATE
:
903 ret
= rtas_set_isolation_state(idx
, state
);
905 case RTAS_SENSOR_TYPE_DR
:
906 ret
= rtas_set_dr_indicator(idx
, state
);
908 case RTAS_SENSOR_TYPE_ALLOCATION_STATE
:
909 ret
= rtas_set_allocation_state(idx
, state
);
912 ret
= RTAS_OUT_NOT_SUPPORTED
;
916 rtas_st(rets
, 0, ret
);
919 static void rtas_get_sensor_state(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
920 uint32_t token
, uint32_t nargs
,
921 target_ulong args
, uint32_t nret
,
924 uint32_t sensor_type
;
925 uint32_t sensor_index
;
926 uint32_t sensor_state
= 0;
927 sPAPRDRConnector
*drc
;
928 sPAPRDRConnectorClass
*drck
;
929 uint32_t ret
= RTAS_OUT_SUCCESS
;
931 if (nargs
!= 2 || nret
!= 2) {
932 ret
= RTAS_OUT_PARAM_ERROR
;
936 sensor_type
= rtas_ld(args
, 0);
937 sensor_index
= rtas_ld(args
, 1);
939 if (sensor_type
!= RTAS_SENSOR_TYPE_ENTITY_SENSE
) {
940 /* currently only DR-related sensors are implemented */
941 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index
,
943 ret
= RTAS_OUT_NOT_SUPPORTED
;
947 drc
= spapr_drc_by_index(sensor_index
);
949 trace_spapr_rtas_get_sensor_state_invalid(sensor_index
);
950 ret
= RTAS_OUT_PARAM_ERROR
;
953 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
954 sensor_state
= drck
->dr_entity_sense(drc
);
957 rtas_st(rets
, 0, ret
);
958 rtas_st(rets
, 1, sensor_state
);
961 /* configure-connector work area offsets, int32_t units for field
962 * indexes, bytes for field offset/len values.
964 * as documented by PAPR+ v2.7, 13.5.3.5
966 #define CC_IDX_NODE_NAME_OFFSET 2
967 #define CC_IDX_PROP_NAME_OFFSET 2
968 #define CC_IDX_PROP_LEN 3
969 #define CC_IDX_PROP_DATA_OFFSET 4
970 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
971 #define CC_WA_LEN 4096
973 static void configure_connector_st(target_ulong addr
, target_ulong offset
,
974 const void *buf
, size_t len
)
976 cpu_physical_memory_write(ppc64_phys_to_real(addr
+ offset
),
977 buf
, MIN(len
, CC_WA_LEN
- offset
));
980 static void rtas_ibm_configure_connector(PowerPCCPU
*cpu
,
981 sPAPRMachineState
*spapr
,
982 uint32_t token
, uint32_t nargs
,
983 target_ulong args
, uint32_t nret
,
989 sPAPRDRConnector
*drc
;
990 sPAPRConfigureConnectorState
*ccs
;
991 sPAPRDRCCResponse resp
= SPAPR_DR_CC_RESPONSE_CONTINUE
;
994 if (nargs
!= 2 || nret
!= 1) {
995 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
999 wa_addr
= ((uint64_t)rtas_ld(args
, 1) << 32) | rtas_ld(args
, 0);
1001 drc_index
= rtas_ld(wa_addr
, 0);
1002 drc
= spapr_drc_by_index(drc_index
);
1004 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index
);
1005 rc
= RTAS_OUT_PARAM_ERROR
;
1010 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index
);
1011 rc
= SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE
;
1017 ccs
= g_new0(sPAPRConfigureConnectorState
, 1);
1018 ccs
->fdt_offset
= drc
->fdt_start_offset
;
1025 const struct fdt_property
*prop
;
1026 int fdt_offset_next
, prop_len
;
1028 tag
= fdt_next_tag(drc
->fdt
, ccs
->fdt_offset
, &fdt_offset_next
);
1031 case FDT_BEGIN_NODE
:
1033 name
= fdt_get_name(drc
->fdt
, ccs
->fdt_offset
, NULL
);
1035 /* provide the name of the next OF node */
1036 wa_offset
= CC_VAL_DATA_OFFSET
;
1037 rtas_st(wa_addr
, CC_IDX_NODE_NAME_OFFSET
, wa_offset
);
1038 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1039 resp
= SPAPR_DR_CC_RESPONSE_NEXT_CHILD
;
1043 if (ccs
->fdt_depth
== 0) {
1044 sPAPRDRIsolationState state
= drc
->isolation_state
;
1045 uint32_t drc_index
= spapr_drc_index(drc
);
1046 /* done sending the device tree, don't need to track
1049 trace_spapr_drc_set_configured(drc_index
);
1050 if (state
== SPAPR_DR_ISOLATION_STATE_UNISOLATED
) {
1051 drc
->configured
= true;
1053 /* guest should be not configuring an isolated device */
1054 trace_spapr_drc_set_configured_skipping(drc_index
);
1059 resp
= SPAPR_DR_CC_RESPONSE_SUCCESS
;
1061 resp
= SPAPR_DR_CC_RESPONSE_PREV_PARENT
;
1065 prop
= fdt_get_property_by_offset(drc
->fdt
, ccs
->fdt_offset
,
1067 name
= fdt_string(drc
->fdt
, fdt32_to_cpu(prop
->nameoff
));
1069 /* provide the name of the next OF property */
1070 wa_offset
= CC_VAL_DATA_OFFSET
;
1071 rtas_st(wa_addr
, CC_IDX_PROP_NAME_OFFSET
, wa_offset
);
1072 configure_connector_st(wa_addr
, wa_offset
, name
, strlen(name
) + 1);
1074 /* provide the length and value of the OF property. data gets
1075 * placed immediately after NULL terminator of the OF property's
1078 wa_offset
+= strlen(name
) + 1,
1079 rtas_st(wa_addr
, CC_IDX_PROP_LEN
, prop_len
);
1080 rtas_st(wa_addr
, CC_IDX_PROP_DATA_OFFSET
, wa_offset
);
1081 configure_connector_st(wa_addr
, wa_offset
, prop
->data
, prop_len
);
1082 resp
= SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY
;
1085 resp
= SPAPR_DR_CC_RESPONSE_ERROR
;
1087 /* keep seeking for an actionable tag */
1091 ccs
->fdt_offset
= fdt_offset_next
;
1093 } while (resp
== SPAPR_DR_CC_RESPONSE_CONTINUE
);
1097 rtas_st(rets
, 0, rc
);
1100 static void spapr_drc_register_types(void)
1102 type_register_static(&spapr_dr_connector_info
);
1103 type_register_static(&spapr_drc_physical_info
);
1104 type_register_static(&spapr_drc_logical_info
);
1105 type_register_static(&spapr_drc_cpu_info
);
1106 type_register_static(&spapr_drc_pci_info
);
1107 type_register_static(&spapr_drc_lmb_info
);
1109 spapr_rtas_register(RTAS_SET_INDICATOR
, "set-indicator",
1110 rtas_set_indicator
);
1111 spapr_rtas_register(RTAS_GET_SENSOR_STATE
, "get-sensor-state",
1112 rtas_get_sensor_state
);
1113 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR
, "ibm,configure-connector",
1114 rtas_ibm_configure_connector
);
1116 type_init(spapr_drc_register_types
)