2 * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
4 * Copyright IBM Corp. 2014
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 static sPAPRConfigureConnectorState *spapr_ccs_find(sPAPRMachineState *spapr,
33 sPAPRConfigureConnectorState *ccs = NULL;
35 QTAILQ_FOREACH(ccs, &spapr->ccs_list, next) {
36 if (ccs->drc_index == drc_index) {
44 static void spapr_ccs_add(sPAPRMachineState *spapr,
45 sPAPRConfigureConnectorState *ccs)
47 g_assert(!spapr_ccs_find(spapr, ccs->drc_index));
48 QTAILQ_INSERT_HEAD(&spapr->ccs_list, ccs, next);
51 static void spapr_ccs_remove(sPAPRMachineState *spapr,
52 sPAPRConfigureConnectorState *ccs)
54 QTAILQ_REMOVE(&spapr->ccs_list, ccs, next);
58 static sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type)
62 /* make sure this isn't SPAPR_DR_CONNECTOR_TYPE_ANY, or some
65 g_assert(is_power_of_2(type));
67 while (type != (1 << shift)) {
73 static uint32_t get_index(sPAPRDRConnector *drc)
75 /* no set format for a drc index: it only needs to be globally
76 * unique. this is how we encode the DRC type on bare-metal
77 * however, so might as well do that here
79 return (get_type_shift(drc->type) << DRC_INDEX_TYPE_SHIFT) |
80 (drc->id & DRC_INDEX_ID_MASK);
83 static uint32_t set_isolation_state(sPAPRDRConnector *drc,
84 sPAPRDRIsolationState state)
86 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
88 trace_spapr_drc_set_isolation_state(get_index(drc), state);
90 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
91 /* cannot unisolate a non-existent resource, and, or resources
92 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
95 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
96 return RTAS_OUT_NO_SUCH_INDICATOR;
101 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
102 * belong to a DIMM device that is marked for removal.
104 * Currently the guest userspace tool drmgr that drives the memory
105 * hotplug/unplug will just try to remove a set of 'removable' LMBs
106 * in response to a hot unplug request that is based on drc-count.
107 * If the LMB being removed doesn't belong to a DIMM device that is
108 * actually being unplugged, fail the isolation request here.
110 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_LMB) {
111 if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
112 !drc->awaiting_release) {
113 return RTAS_OUT_HW_ERROR;
117 drc->isolation_state = state;
119 if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
120 /* if we're awaiting release, but still in an unconfigured state,
121 * it's likely the guest is still in the process of configuring
122 * the device and is transitioning the devices to an ISOLATED
123 * state as a part of that process. so we only complete the
124 * removal when this transition happens for a device in a
125 * configured state, as suggested by the state diagram from
128 if (drc->awaiting_release) {
129 if (drc->configured) {
130 trace_spapr_drc_set_isolation_state_finalizing(get_index(drc));
131 drck->detach(drc, DEVICE(drc->dev), NULL);
133 trace_spapr_drc_set_isolation_state_deferring(get_index(drc));
136 drc->configured = false;
139 return RTAS_OUT_SUCCESS;
142 static uint32_t set_indicator_state(sPAPRDRConnector *drc,
143 sPAPRDRIndicatorState state)
145 trace_spapr_drc_set_indicator_state(get_index(drc), state);
146 drc->indicator_state = state;
147 return RTAS_OUT_SUCCESS;
150 static uint32_t set_allocation_state(sPAPRDRConnector *drc,
151 sPAPRDRAllocationState state)
153 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
155 trace_spapr_drc_set_allocation_state(get_index(drc), state);
157 if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
158 /* if there's no resource/device associated with the DRC, there's
159 * no way for us to put it in an allocation state consistent with
160 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
161 * result in an RTAS return code of -3 / "no such indicator"
164 return RTAS_OUT_NO_SUCH_INDICATOR;
166 if (drc->awaiting_release && drc->awaiting_allocation) {
167 /* kernel is acknowledging a previous hotplug event
168 * while we are already removing it.
169 * it's safe to ignore awaiting_allocation here since we know the
170 * situation is predicated on the guest either already having done
171 * so (boot-time hotplug), or never being able to acquire in the
172 * first place (hotplug followed by immediate unplug).
174 drc->awaiting_allocation_skippable = true;
175 return RTAS_OUT_NO_SUCH_INDICATOR;
179 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
180 drc->allocation_state = state;
181 if (drc->awaiting_release &&
182 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
183 trace_spapr_drc_set_allocation_state_finalizing(get_index(drc));
184 drck->detach(drc, DEVICE(drc->dev), NULL);
185 } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
186 drc->awaiting_allocation = false;
189 return RTAS_OUT_SUCCESS;
192 static uint32_t get_type(sPAPRDRConnector *drc)
197 static const char *get_name(sPAPRDRConnector *drc)
202 static const void *get_fdt(sPAPRDRConnector *drc, int *fdt_start_offset)
204 if (fdt_start_offset) {
205 *fdt_start_offset = drc->fdt_start_offset;
210 static void set_configured(sPAPRDRConnector *drc)
212 trace_spapr_drc_set_configured(get_index(drc));
214 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
215 /* guest should be not configuring an isolated device */
216 trace_spapr_drc_set_configured_skipping(get_index(drc));
219 drc->configured = true;
222 /* has the guest been notified of device attachment? */
223 static void set_signalled(sPAPRDRConnector *drc)
225 drc->signalled = true;
229 * dr-entity-sense sensor value
230 * returned via get-sensor-state RTAS calls
231 * as expected by state diagram in PAPR+ 2.7, 13.4
232 * based on the current allocation/indicator/power states
233 * for the DR connector.
235 static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
238 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
239 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
240 /* for logical DR, we return a state of UNUSABLE
241 * iff the allocation state UNUSABLE.
242 * Otherwise, report the state as USABLE/PRESENT,
243 * as we would for PCI.
245 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
247 /* this assumes all PCI devices are assigned to
248 * a 'live insertion' power domain, where QEMU
249 * manages power state automatically as opposed
250 * to the guest. present, non-PCI resources are
251 * unaffected by power state.
253 *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
256 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
257 /* PCI devices, and only PCI devices, use EMPTY
258 * in cases where we'd otherwise use UNUSABLE
260 *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
262 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
266 trace_spapr_drc_entity_sense(get_index(drc), *state);
267 return RTAS_OUT_SUCCESS;
270 static void prop_get_index(Object *obj, Visitor *v, const char *name,
271 void *opaque, Error **errp)
273 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
274 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
275 uint32_t value = (uint32_t)drck->get_index(drc);
276 visit_type_uint32(v, name, &value, errp);
279 static void prop_get_type(Object *obj, Visitor *v, const char *name,
280 void *opaque, Error **errp)
282 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
283 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
284 uint32_t value = (uint32_t)drck->get_type(drc);
285 visit_type_uint32(v, name, &value, errp);
288 static char *prop_get_name(Object *obj, Error **errp)
290 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
291 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
292 return g_strdup(drck->get_name(drc));
295 static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
296 void *opaque, Error **errp)
298 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
299 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
302 drck->entity_sense(drc, &value);
303 visit_type_uint32(v, name, &value, errp);
306 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
307 void *opaque, Error **errp)
309 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
311 int fdt_offset_next, fdt_offset, fdt_depth;
315 visit_type_null(v, NULL, errp);
320 fdt_offset = drc->fdt_start_offset;
324 const char *name = NULL;
325 const struct fdt_property *prop = NULL;
326 int prop_len = 0, name_len = 0;
329 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
333 name = fdt_get_name(fdt, fdt_offset, &name_len);
334 visit_start_struct(v, name, NULL, 0, &err);
336 error_propagate(errp, err);
341 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
342 g_assert(fdt_depth > 0);
343 visit_check_struct(v, &err);
344 visit_end_struct(v, NULL);
346 error_propagate(errp, err);
353 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
354 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
355 visit_start_list(v, name, NULL, 0, &err);
357 error_propagate(errp, err);
360 for (i = 0; i < prop_len; i++) {
361 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
363 error_propagate(errp, err);
367 visit_check_list(v, &err);
368 visit_end_list(v, NULL);
370 error_propagate(errp, err);
376 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
378 fdt_offset = fdt_offset_next;
379 } while (fdt_depth != 0);
382 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
383 int fdt_start_offset, bool coldplug, Error **errp)
385 trace_spapr_drc_attach(get_index(drc));
387 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
388 error_setg(errp, "an attached device is still awaiting release");
391 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
392 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
394 g_assert(fdt || coldplug);
396 /* NOTE: setting initial isolation state to UNISOLATED means we can't
397 * detach unless guest has a userspace/kernel that moves this state
398 * back to ISOLATED in response to an unplug event, or this is done
399 * manually by the admin prior. if we force things while the guest
400 * may be accessing the device, we can easily crash the guest, so we
401 * we defer completion of removal in such cases to the reset() hook.
403 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
404 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
406 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
410 drc->fdt_start_offset = fdt_start_offset;
411 drc->configured = coldplug;
412 /* 'logical' DR resources such as memory/cpus are in some cases treated
413 * as a pool of resources from which the guest is free to choose from
414 * based on only a count. for resources that can be assigned in this
415 * fashion, we must assume the resource is signalled immediately
416 * since a single hotplug request might make an arbitrary number of
417 * such attached resources available to the guest, as opposed to
418 * 'physical' DR resources such as PCI where each device/resource is
419 * signalled individually.
421 drc->signalled = (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI)
424 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
425 drc->awaiting_allocation = true;
428 object_property_add_link(OBJECT(drc), "device",
429 object_get_typename(OBJECT(drc->dev)),
430 (Object **)(&drc->dev),
434 static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
436 trace_spapr_drc_detach(get_index(drc));
438 /* if we've signalled device presence to the guest, or if the guest
439 * has gone ahead and configured the device (via manually-executed
440 * device add via drmgr in guest, namely), we need to wait
441 * for the guest to quiesce the device before completing detach.
442 * Otherwise, we can assume the guest hasn't seen it and complete the
443 * detach immediately. Note that there is a small race window
444 * just before, or during, configuration, which is this context
445 * refers mainly to fetching the device tree via RTAS.
446 * During this window the device access will be arbitrated by
447 * associated DRC, which will simply fail the RTAS calls as invalid.
448 * This is recoverable within guest and current implementations of
449 * drmgr should be able to cope.
451 if (!drc->signalled && !drc->configured) {
452 /* if the guest hasn't seen the device we can't rely on it to
453 * set it back to an isolated state via RTAS, so do it here manually
455 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
458 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
459 trace_spapr_drc_awaiting_isolated(get_index(drc));
460 drc->awaiting_release = true;
464 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
465 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
466 trace_spapr_drc_awaiting_unusable(get_index(drc));
467 drc->awaiting_release = true;
471 if (drc->awaiting_allocation) {
472 if (!drc->awaiting_allocation_skippable) {
473 drc->awaiting_release = true;
474 trace_spapr_drc_awaiting_allocation(get_index(drc));
479 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
481 /* Calling release callbacks based on drc->type. */
483 case SPAPR_DR_CONNECTOR_TYPE_CPU:
484 spapr_core_release(drc->dev);
486 case SPAPR_DR_CONNECTOR_TYPE_PCI:
487 spapr_phb_remove_pci_device_cb(drc->dev);
489 case SPAPR_DR_CONNECTOR_TYPE_LMB:
490 spapr_lmb_release(drc->dev);
492 case SPAPR_DR_CONNECTOR_TYPE_PHB:
493 case SPAPR_DR_CONNECTOR_TYPE_VIO:
498 drc->awaiting_release = false;
499 drc->awaiting_allocation_skippable = false;
502 drc->fdt_start_offset = 0;
503 object_property_del(OBJECT(drc), "device", NULL);
507 static bool release_pending(sPAPRDRConnector *drc)
509 return drc->awaiting_release;
512 static void reset(DeviceState *d)
514 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
515 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
516 sPAPRDREntitySense state;
518 trace_spapr_drc_reset(drck->get_index(drc));
519 /* immediately upon reset we can safely assume DRCs whose devices
520 * are pending removal can be safely removed, and that they will
521 * subsequently be left in an ISOLATED state. move the DRC to this
522 * state in these cases (which will in turn complete any pending
525 if (drc->awaiting_release) {
526 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
527 /* generally this should also finalize the removal, but if the device
528 * hasn't yet been configured we normally defer removal under the
529 * assumption that this transition is taking place as part of device
530 * configuration. so check if we're still waiting after this, and
531 * force removal if we are
533 if (drc->awaiting_release) {
534 drck->detach(drc, DEVICE(drc->dev), NULL);
537 /* non-PCI devices may be awaiting a transition to UNUSABLE */
538 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
539 drc->awaiting_release) {
540 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
544 drck->entity_sense(drc, &state);
545 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
546 drck->set_signalled(drc);
550 static bool spapr_drc_needed(void *opaque)
552 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
553 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
555 sPAPRDREntitySense value;
556 drck->entity_sense(drc, &value);
558 /* If no dev is plugged in there is no need to migrate the DRC state */
559 if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
564 * If there is dev plugged in, we need to migrate the DRC state when
565 * it is different from cold-plugged state
568 case SPAPR_DR_CONNECTOR_TYPE_PCI:
569 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
570 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
571 drc->configured && drc->signalled && !drc->awaiting_release);
573 case SPAPR_DR_CONNECTOR_TYPE_CPU:
574 case SPAPR_DR_CONNECTOR_TYPE_LMB:
575 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
576 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) &&
577 drc->configured && drc->signalled && !drc->awaiting_release);
579 case SPAPR_DR_CONNECTOR_TYPE_PHB:
580 case SPAPR_DR_CONNECTOR_TYPE_VIO:
587 static const VMStateDescription vmstate_spapr_drc = {
590 .minimum_version_id = 1,
591 .needed = spapr_drc_needed,
592 .fields = (VMStateField []) {
593 VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
594 VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
595 VMSTATE_UINT32(indicator_state, sPAPRDRConnector),
596 VMSTATE_BOOL(configured, sPAPRDRConnector),
597 VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
598 VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
599 VMSTATE_BOOL(signalled, sPAPRDRConnector),
600 VMSTATE_END_OF_LIST()
604 static void realize(DeviceState *d, Error **errp)
606 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
607 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
608 Object *root_container;
613 trace_spapr_drc_realize(drck->get_index(drc));
614 /* NOTE: we do this as part of realize/unrealize due to the fact
615 * that the guest will communicate with the DRC via RTAS calls
616 * referencing the global DRC index. By unlinking the DRC
617 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
618 * inaccessible by the guest, since lookups rely on this path
619 * existing in the composition tree
621 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
622 snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
623 child_name = object_get_canonical_path_component(OBJECT(drc));
624 trace_spapr_drc_realize_child(drck->get_index(drc), child_name);
625 object_property_add_alias(root_container, link_name,
626 drc->owner, child_name, &err);
628 error_report_err(err);
629 object_unref(OBJECT(drc));
632 vmstate_register(DEVICE(drc), drck->get_index(drc), &vmstate_spapr_drc,
634 trace_spapr_drc_realize_complete(drck->get_index(drc));
637 static void unrealize(DeviceState *d, Error **errp)
639 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
640 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
641 Object *root_container;
645 trace_spapr_drc_unrealize(drck->get_index(drc));
646 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
647 snprintf(name, sizeof(name), "%x", drck->get_index(drc));
648 object_property_del(root_container, name, &err);
650 error_report_err(err);
651 object_unref(OBJECT(drc));
655 sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
656 sPAPRDRConnectorType type,
659 sPAPRDRConnector *drc =
660 SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
668 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
669 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
670 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
673 /* human-readable name for a DRC to encode into the DT
674 * description. this is mainly only used within a guest in place
675 * of the unique DRC index.
677 * in the case of VIO/PCI devices, it corresponds to a
678 * "location code" that maps a logical device/function (DRC index)
679 * to a physical (or virtual in the case of VIO) location in the
680 * system by chaining together the "location label" for each
681 * encapsulating component.
683 * since this is more to do with diagnosing physical hardware
684 * issues than guest compatibility, we choose location codes/DRC
685 * names that adhere to the documented format, but avoid encoding
686 * the entire topology information into the label/code, instead
687 * just using the location codes based on the labels for the
688 * endpoints (VIO/PCI adaptor connectors), which is basically
689 * just "C" followed by an integer ID.
691 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
692 * location codes as documented by PAPR+ v2.7, 12.3.1.5
695 case SPAPR_DR_CONNECTOR_TYPE_CPU:
696 drc->name = g_strdup_printf("CPU %d", id);
698 case SPAPR_DR_CONNECTOR_TYPE_PHB:
699 drc->name = g_strdup_printf("PHB %d", id);
701 case SPAPR_DR_CONNECTOR_TYPE_VIO:
702 case SPAPR_DR_CONNECTOR_TYPE_PCI:
703 drc->name = g_strdup_printf("C%d", id);
705 case SPAPR_DR_CONNECTOR_TYPE_LMB:
706 drc->name = g_strdup_printf("LMB %d", id);
712 /* PCI slot always start in a USABLE state, and stay there */
713 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
714 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
720 static void spapr_dr_connector_instance_init(Object *obj)
722 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
724 object_property_add_uint32_ptr(obj, "isolation-state",
725 &drc->isolation_state, NULL);
726 object_property_add_uint32_ptr(obj, "indicator-state",
727 &drc->indicator_state, NULL);
728 object_property_add_uint32_ptr(obj, "allocation-state",
729 &drc->allocation_state, NULL);
730 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
731 object_property_add(obj, "index", "uint32", prop_get_index,
732 NULL, NULL, NULL, NULL);
733 object_property_add(obj, "connector_type", "uint32", prop_get_type,
734 NULL, NULL, NULL, NULL);
735 object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
736 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
737 NULL, NULL, NULL, NULL);
738 object_property_add(obj, "fdt", "struct", prop_get_fdt,
739 NULL, NULL, NULL, NULL);
742 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
744 DeviceClass *dk = DEVICE_CLASS(k);
745 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
748 dk->realize = realize;
749 dk->unrealize = unrealize;
750 drck->set_isolation_state = set_isolation_state;
751 drck->set_indicator_state = set_indicator_state;
752 drck->set_allocation_state = set_allocation_state;
753 drck->get_index = get_index;
754 drck->get_type = get_type;
755 drck->get_name = get_name;
756 drck->get_fdt = get_fdt;
757 drck->set_configured = set_configured;
758 drck->entity_sense = entity_sense;
759 drck->attach = attach;
760 drck->detach = detach;
761 drck->release_pending = release_pending;
762 drck->set_signalled = set_signalled;
764 * Reason: it crashes FIXME find and document the real reason
766 dk->user_creatable = false;
769 static const TypeInfo spapr_dr_connector_info = {
770 .name = TYPE_SPAPR_DR_CONNECTOR,
771 .parent = TYPE_DEVICE,
772 .instance_size = sizeof(sPAPRDRConnector),
773 .instance_init = spapr_dr_connector_instance_init,
774 .class_size = sizeof(sPAPRDRConnectorClass),
775 .class_init = spapr_dr_connector_class_init,
778 /* helper functions for external users */
780 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
785 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
786 obj = object_resolve_path(name, NULL);
788 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
791 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
794 return spapr_dr_connector_by_index(
795 (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
796 (id & DRC_INDEX_ID_MASK));
799 /* generate a string the describes the DRC to encode into the
802 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
804 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
807 case SPAPR_DR_CONNECTOR_TYPE_CPU:
809 case SPAPR_DR_CONNECTOR_TYPE_PHB:
811 case SPAPR_DR_CONNECTOR_TYPE_VIO:
813 case SPAPR_DR_CONNECTOR_TYPE_PCI:
815 case SPAPR_DR_CONNECTOR_TYPE_LMB:
825 * spapr_drc_populate_dt
827 * @fdt: libfdt device tree
828 * @path: path in the DT to generate properties
829 * @owner: parent Object/DeviceState for which to generate DRC
831 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
832 * to the types of DRCs to generate entries for
834 * generate OF properties to describe DRC topology/indices to guests
836 * as documented in PAPR+ v2.1, 13.5.2
838 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
839 uint32_t drc_type_mask)
841 Object *root_container;
842 ObjectProperty *prop;
843 ObjectPropertyIterator iter;
844 uint32_t drc_count = 0;
845 GArray *drc_indexes, *drc_power_domains;
846 GString *drc_names, *drc_types;
849 /* the first entry of each properties is a 32-bit integer encoding
850 * the number of elements in the array. we won't know this until
851 * we complete the iteration through all the matching DRCs, but
852 * reserve the space now and set the offsets accordingly so we
853 * can fill them in later.
855 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
856 drc_indexes = g_array_set_size(drc_indexes, 1);
857 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
858 drc_power_domains = g_array_set_size(drc_power_domains, 1);
859 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
860 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
862 /* aliases for all DRConnector objects will be rooted in QOM
863 * composition tree at DRC_CONTAINER_PATH
865 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
867 object_property_iter_init(&iter, root_container);
868 while ((prop = object_property_iter_next(&iter))) {
870 sPAPRDRConnector *drc;
871 sPAPRDRConnectorClass *drck;
872 uint32_t drc_index, drc_power_domain;
874 if (!strstart(prop->type, "link<", NULL)) {
878 obj = object_property_get_link(root_container, prop->name, NULL);
879 drc = SPAPR_DR_CONNECTOR(obj);
880 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
882 if (owner && (drc->owner != owner)) {
886 if ((drc->type & drc_type_mask) == 0) {
892 /* ibm,drc-indexes */
893 drc_index = cpu_to_be32(drck->get_index(drc));
894 g_array_append_val(drc_indexes, drc_index);
896 /* ibm,drc-power-domains */
897 drc_power_domain = cpu_to_be32(-1);
898 g_array_append_val(drc_power_domains, drc_power_domain);
901 drc_names = g_string_append(drc_names, drck->get_name(drc));
902 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
905 drc_types = g_string_append(drc_types,
906 spapr_drc_get_type_str(drc->type));
907 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
910 /* now write the drc count into the space we reserved at the
911 * beginning of the arrays previously
913 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
914 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
915 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
916 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
918 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
920 drc_indexes->len * sizeof(uint32_t));
922 error_report("Couldn't create ibm,drc-indexes property");
926 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
927 drc_power_domains->data,
928 drc_power_domains->len * sizeof(uint32_t));
930 error_report("Couldn't finalize ibm,drc-power-domains property");
934 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
935 drc_names->str, drc_names->len);
937 error_report("Couldn't finalize ibm,drc-names property");
941 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
942 drc_types->str, drc_types->len);
944 error_report("Couldn't finalize ibm,drc-types property");
949 g_array_free(drc_indexes, true);
950 g_array_free(drc_power_domains, true);
951 g_string_free(drc_names, true);
952 g_string_free(drc_types, true);
961 static bool sensor_type_is_dr(uint32_t sensor_type)
963 switch (sensor_type) {
964 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
965 case RTAS_SENSOR_TYPE_DR:
966 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
973 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
974 uint32_t token, uint32_t nargs,
975 target_ulong args, uint32_t nret,
978 uint32_t sensor_type;
979 uint32_t sensor_index;
980 uint32_t sensor_state;
981 uint32_t ret = RTAS_OUT_SUCCESS;
982 sPAPRDRConnector *drc;
983 sPAPRDRConnectorClass *drck;
985 if (nargs != 3 || nret != 1) {
986 ret = RTAS_OUT_PARAM_ERROR;
990 sensor_type = rtas_ld(args, 0);
991 sensor_index = rtas_ld(args, 1);
992 sensor_state = rtas_ld(args, 2);
994 if (!sensor_type_is_dr(sensor_type)) {
995 goto out_unimplemented;
998 /* if this is a DR sensor we can assume sensor_index == drc_index */
999 drc = spapr_dr_connector_by_index(sensor_index);
1001 trace_spapr_rtas_set_indicator_invalid(sensor_index);
1002 ret = RTAS_OUT_PARAM_ERROR;
1005 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1007 switch (sensor_type) {
1008 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1009 /* if the guest is configuring a device attached to this
1010 * DRC, we should reset the configuration state at this
1011 * point since it may no longer be reliable (guest released
1012 * device and needs to start over, or unplug occurred so
1013 * the FDT is no longer valid)
1015 if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
1016 sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr,
1019 spapr_ccs_remove(spapr, ccs);
1022 ret = drck->set_isolation_state(drc, sensor_state);
1024 case RTAS_SENSOR_TYPE_DR:
1025 ret = drck->set_indicator_state(drc, sensor_state);
1027 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1028 ret = drck->set_allocation_state(drc, sensor_state);
1031 goto out_unimplemented;
1035 rtas_st(rets, 0, ret);
1039 /* currently only DR-related sensors are implemented */
1040 trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
1041 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
1044 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
1045 uint32_t token, uint32_t nargs,
1046 target_ulong args, uint32_t nret,
1049 uint32_t sensor_type;
1050 uint32_t sensor_index;
1051 uint32_t sensor_state = 0;
1052 sPAPRDRConnector *drc;
1053 sPAPRDRConnectorClass *drck;
1054 uint32_t ret = RTAS_OUT_SUCCESS;
1056 if (nargs != 2 || nret != 2) {
1057 ret = RTAS_OUT_PARAM_ERROR;
1061 sensor_type = rtas_ld(args, 0);
1062 sensor_index = rtas_ld(args, 1);
1064 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1065 /* currently only DR-related sensors are implemented */
1066 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1068 ret = RTAS_OUT_NOT_SUPPORTED;
1072 drc = spapr_dr_connector_by_index(sensor_index);
1074 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1075 ret = RTAS_OUT_PARAM_ERROR;
1078 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1079 ret = drck->entity_sense(drc, &sensor_state);
1082 rtas_st(rets, 0, ret);
1083 rtas_st(rets, 1, sensor_state);
1086 /* configure-connector work area offsets, int32_t units for field
1087 * indexes, bytes for field offset/len values.
1089 * as documented by PAPR+ v2.7, 13.5.3.5
1091 #define CC_IDX_NODE_NAME_OFFSET 2
1092 #define CC_IDX_PROP_NAME_OFFSET 2
1093 #define CC_IDX_PROP_LEN 3
1094 #define CC_IDX_PROP_DATA_OFFSET 4
1095 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1096 #define CC_WA_LEN 4096
1098 static void configure_connector_st(target_ulong addr, target_ulong offset,
1099 const void *buf, size_t len)
1101 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1102 buf, MIN(len, CC_WA_LEN - offset));
1105 void spapr_ccs_reset_hook(void *opaque)
1107 sPAPRMachineState *spapr = opaque;
1108 sPAPRConfigureConnectorState *ccs, *ccs_tmp;
1110 QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) {
1111 spapr_ccs_remove(spapr, ccs);
1115 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1116 sPAPRMachineState *spapr,
1117 uint32_t token, uint32_t nargs,
1118 target_ulong args, uint32_t nret,
1124 sPAPRDRConnector *drc;
1125 sPAPRDRConnectorClass *drck;
1126 sPAPRConfigureConnectorState *ccs;
1127 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1131 if (nargs != 2 || nret != 1) {
1132 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1136 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1138 drc_index = rtas_ld(wa_addr, 0);
1139 drc = spapr_dr_connector_by_index(drc_index);
1141 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1142 rc = RTAS_OUT_PARAM_ERROR;
1146 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1147 fdt = drck->get_fdt(drc, NULL);
1149 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1150 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1154 ccs = spapr_ccs_find(spapr, drc_index);
1156 ccs = g_new0(sPAPRConfigureConnectorState, 1);
1157 (void)drck->get_fdt(drc, &ccs->fdt_offset);
1158 ccs->drc_index = drc_index;
1159 spapr_ccs_add(spapr, ccs);
1165 const struct fdt_property *prop;
1166 int fdt_offset_next, prop_len;
1168 tag = fdt_next_tag(fdt, ccs->fdt_offset, &fdt_offset_next);
1171 case FDT_BEGIN_NODE:
1173 name = fdt_get_name(fdt, ccs->fdt_offset, NULL);
1175 /* provide the name of the next OF node */
1176 wa_offset = CC_VAL_DATA_OFFSET;
1177 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1178 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1179 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1183 if (ccs->fdt_depth == 0) {
1184 /* done sending the device tree, don't need to track
1187 drck->set_configured(drc);
1188 spapr_ccs_remove(spapr, ccs);
1190 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1192 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1196 prop = fdt_get_property_by_offset(fdt, ccs->fdt_offset,
1198 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
1200 /* provide the name of the next OF property */
1201 wa_offset = CC_VAL_DATA_OFFSET;
1202 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1203 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1205 /* provide the length and value of the OF property. data gets
1206 * placed immediately after NULL terminator of the OF property's
1209 wa_offset += strlen(name) + 1,
1210 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1211 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1212 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1213 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1216 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1218 /* keep seeking for an actionable tag */
1222 ccs->fdt_offset = fdt_offset_next;
1224 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1228 rtas_st(rets, 0, rc);
1231 static void spapr_drc_register_types(void)
1233 type_register_static(&spapr_dr_connector_info);
1235 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1236 rtas_set_indicator);
1237 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1238 rtas_get_sensor_state);
1239 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1240 rtas_ibm_configure_connector);
1242 type_init(spapr_drc_register_types)