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 void set_configured(sPAPRDRConnector *drc)
204 trace_spapr_drc_set_configured(get_index(drc));
206 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
207 /* guest should be not configuring an isolated device */
208 trace_spapr_drc_set_configured_skipping(get_index(drc));
211 drc->configured = true;
214 /* has the guest been notified of device attachment? */
215 static void set_signalled(sPAPRDRConnector *drc)
217 drc->signalled = true;
221 * dr-entity-sense sensor value
222 * returned via get-sensor-state RTAS calls
223 * as expected by state diagram in PAPR+ 2.7, 13.4
224 * based on the current allocation/indicator/power states
225 * for the DR connector.
227 static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
230 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
231 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
232 /* for logical DR, we return a state of UNUSABLE
233 * iff the allocation state UNUSABLE.
234 * Otherwise, report the state as USABLE/PRESENT,
235 * as we would for PCI.
237 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
239 /* this assumes all PCI devices are assigned to
240 * a 'live insertion' power domain, where QEMU
241 * manages power state automatically as opposed
242 * to the guest. present, non-PCI resources are
243 * unaffected by power state.
245 *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
248 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
249 /* PCI devices, and only PCI devices, use EMPTY
250 * in cases where we'd otherwise use UNUSABLE
252 *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
254 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
258 trace_spapr_drc_entity_sense(get_index(drc), *state);
259 return RTAS_OUT_SUCCESS;
262 static void prop_get_index(Object *obj, Visitor *v, const char *name,
263 void *opaque, Error **errp)
265 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
266 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
267 uint32_t value = (uint32_t)drck->get_index(drc);
268 visit_type_uint32(v, name, &value, errp);
271 static void prop_get_type(Object *obj, Visitor *v, const char *name,
272 void *opaque, Error **errp)
274 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
275 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
276 uint32_t value = (uint32_t)drck->get_type(drc);
277 visit_type_uint32(v, name, &value, errp);
280 static char *prop_get_name(Object *obj, Error **errp)
282 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
283 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
284 return g_strdup(drck->get_name(drc));
287 static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
288 void *opaque, Error **errp)
290 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
291 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
294 drck->entity_sense(drc, &value);
295 visit_type_uint32(v, name, &value, errp);
298 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
299 void *opaque, Error **errp)
301 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
303 int fdt_offset_next, fdt_offset, fdt_depth;
307 visit_type_null(v, NULL, errp);
312 fdt_offset = drc->fdt_start_offset;
316 const char *name = NULL;
317 const struct fdt_property *prop = NULL;
318 int prop_len = 0, name_len = 0;
321 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
325 name = fdt_get_name(fdt, fdt_offset, &name_len);
326 visit_start_struct(v, name, NULL, 0, &err);
328 error_propagate(errp, err);
333 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
334 g_assert(fdt_depth > 0);
335 visit_check_struct(v, &err);
336 visit_end_struct(v, NULL);
338 error_propagate(errp, err);
345 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
346 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
347 visit_start_list(v, name, NULL, 0, &err);
349 error_propagate(errp, err);
352 for (i = 0; i < prop_len; i++) {
353 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
355 error_propagate(errp, err);
359 visit_check_list(v, &err);
360 visit_end_list(v, NULL);
362 error_propagate(errp, err);
368 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
370 fdt_offset = fdt_offset_next;
371 } while (fdt_depth != 0);
374 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
375 int fdt_start_offset, bool coldplug, Error **errp)
377 trace_spapr_drc_attach(get_index(drc));
379 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
380 error_setg(errp, "an attached device is still awaiting release");
383 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
384 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
386 g_assert(fdt || coldplug);
388 /* NOTE: setting initial isolation state to UNISOLATED means we can't
389 * detach unless guest has a userspace/kernel that moves this state
390 * back to ISOLATED in response to an unplug event, or this is done
391 * manually by the admin prior. if we force things while the guest
392 * may be accessing the device, we can easily crash the guest, so we
393 * we defer completion of removal in such cases to the reset() hook.
395 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
396 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
398 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
402 drc->fdt_start_offset = fdt_start_offset;
403 drc->configured = coldplug;
404 /* 'logical' DR resources such as memory/cpus are in some cases treated
405 * as a pool of resources from which the guest is free to choose from
406 * based on only a count. for resources that can be assigned in this
407 * fashion, we must assume the resource is signalled immediately
408 * since a single hotplug request might make an arbitrary number of
409 * such attached resources available to the guest, as opposed to
410 * 'physical' DR resources such as PCI where each device/resource is
411 * signalled individually.
413 drc->signalled = (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI)
416 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
417 drc->awaiting_allocation = true;
420 object_property_add_link(OBJECT(drc), "device",
421 object_get_typename(OBJECT(drc->dev)),
422 (Object **)(&drc->dev),
426 static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
428 trace_spapr_drc_detach(get_index(drc));
430 /* if we've signalled device presence to the guest, or if the guest
431 * has gone ahead and configured the device (via manually-executed
432 * device add via drmgr in guest, namely), we need to wait
433 * for the guest to quiesce the device before completing detach.
434 * Otherwise, we can assume the guest hasn't seen it and complete the
435 * detach immediately. Note that there is a small race window
436 * just before, or during, configuration, which is this context
437 * refers mainly to fetching the device tree via RTAS.
438 * During this window the device access will be arbitrated by
439 * associated DRC, which will simply fail the RTAS calls as invalid.
440 * This is recoverable within guest and current implementations of
441 * drmgr should be able to cope.
443 if (!drc->signalled && !drc->configured) {
444 /* if the guest hasn't seen the device we can't rely on it to
445 * set it back to an isolated state via RTAS, so do it here manually
447 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
450 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
451 trace_spapr_drc_awaiting_isolated(get_index(drc));
452 drc->awaiting_release = true;
456 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
457 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
458 trace_spapr_drc_awaiting_unusable(get_index(drc));
459 drc->awaiting_release = true;
463 if (drc->awaiting_allocation) {
464 if (!drc->awaiting_allocation_skippable) {
465 drc->awaiting_release = true;
466 trace_spapr_drc_awaiting_allocation(get_index(drc));
471 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
473 /* Calling release callbacks based on drc->type. */
475 case SPAPR_DR_CONNECTOR_TYPE_CPU:
476 spapr_core_release(drc->dev);
478 case SPAPR_DR_CONNECTOR_TYPE_PCI:
479 spapr_phb_remove_pci_device_cb(drc->dev);
481 case SPAPR_DR_CONNECTOR_TYPE_LMB:
482 spapr_lmb_release(drc->dev);
484 case SPAPR_DR_CONNECTOR_TYPE_PHB:
485 case SPAPR_DR_CONNECTOR_TYPE_VIO:
490 drc->awaiting_release = false;
491 drc->awaiting_allocation_skippable = false;
494 drc->fdt_start_offset = 0;
495 object_property_del(OBJECT(drc), "device", NULL);
499 static bool release_pending(sPAPRDRConnector *drc)
501 return drc->awaiting_release;
504 static void reset(DeviceState *d)
506 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
507 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
508 sPAPRDREntitySense state;
510 trace_spapr_drc_reset(drck->get_index(drc));
511 /* immediately upon reset we can safely assume DRCs whose devices
512 * are pending removal can be safely removed, and that they will
513 * subsequently be left in an ISOLATED state. move the DRC to this
514 * state in these cases (which will in turn complete any pending
517 if (drc->awaiting_release) {
518 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
519 /* generally this should also finalize the removal, but if the device
520 * hasn't yet been configured we normally defer removal under the
521 * assumption that this transition is taking place as part of device
522 * configuration. so check if we're still waiting after this, and
523 * force removal if we are
525 if (drc->awaiting_release) {
526 drck->detach(drc, DEVICE(drc->dev), NULL);
529 /* non-PCI devices may be awaiting a transition to UNUSABLE */
530 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
531 drc->awaiting_release) {
532 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
536 drck->entity_sense(drc, &state);
537 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
538 drck->set_signalled(drc);
542 static bool spapr_drc_needed(void *opaque)
544 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
545 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
547 sPAPRDREntitySense value;
548 drck->entity_sense(drc, &value);
550 /* If no dev is plugged in there is no need to migrate the DRC state */
551 if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
556 * If there is dev plugged in, we need to migrate the DRC state when
557 * it is different from cold-plugged state
560 case SPAPR_DR_CONNECTOR_TYPE_PCI:
561 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
562 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
563 drc->configured && drc->signalled && !drc->awaiting_release);
565 case SPAPR_DR_CONNECTOR_TYPE_CPU:
566 case SPAPR_DR_CONNECTOR_TYPE_LMB:
567 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
568 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) &&
569 drc->configured && drc->signalled && !drc->awaiting_release);
571 case SPAPR_DR_CONNECTOR_TYPE_PHB:
572 case SPAPR_DR_CONNECTOR_TYPE_VIO:
579 static const VMStateDescription vmstate_spapr_drc = {
582 .minimum_version_id = 1,
583 .needed = spapr_drc_needed,
584 .fields = (VMStateField []) {
585 VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
586 VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
587 VMSTATE_UINT32(indicator_state, sPAPRDRConnector),
588 VMSTATE_BOOL(configured, sPAPRDRConnector),
589 VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
590 VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
591 VMSTATE_BOOL(signalled, sPAPRDRConnector),
592 VMSTATE_END_OF_LIST()
596 static void realize(DeviceState *d, Error **errp)
598 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
599 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
600 Object *root_container;
605 trace_spapr_drc_realize(drck->get_index(drc));
606 /* NOTE: we do this as part of realize/unrealize due to the fact
607 * that the guest will communicate with the DRC via RTAS calls
608 * referencing the global DRC index. By unlinking the DRC
609 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
610 * inaccessible by the guest, since lookups rely on this path
611 * existing in the composition tree
613 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
614 snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
615 child_name = object_get_canonical_path_component(OBJECT(drc));
616 trace_spapr_drc_realize_child(drck->get_index(drc), child_name);
617 object_property_add_alias(root_container, link_name,
618 drc->owner, child_name, &err);
620 error_report_err(err);
621 object_unref(OBJECT(drc));
624 vmstate_register(DEVICE(drc), drck->get_index(drc), &vmstate_spapr_drc,
626 trace_spapr_drc_realize_complete(drck->get_index(drc));
629 static void unrealize(DeviceState *d, Error **errp)
631 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
632 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
633 Object *root_container;
637 trace_spapr_drc_unrealize(drck->get_index(drc));
638 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
639 snprintf(name, sizeof(name), "%x", drck->get_index(drc));
640 object_property_del(root_container, name, &err);
642 error_report_err(err);
643 object_unref(OBJECT(drc));
647 sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
648 sPAPRDRConnectorType type,
651 sPAPRDRConnector *drc =
652 SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
660 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
661 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
662 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
665 /* human-readable name for a DRC to encode into the DT
666 * description. this is mainly only used within a guest in place
667 * of the unique DRC index.
669 * in the case of VIO/PCI devices, it corresponds to a
670 * "location code" that maps a logical device/function (DRC index)
671 * to a physical (or virtual in the case of VIO) location in the
672 * system by chaining together the "location label" for each
673 * encapsulating component.
675 * since this is more to do with diagnosing physical hardware
676 * issues than guest compatibility, we choose location codes/DRC
677 * names that adhere to the documented format, but avoid encoding
678 * the entire topology information into the label/code, instead
679 * just using the location codes based on the labels for the
680 * endpoints (VIO/PCI adaptor connectors), which is basically
681 * just "C" followed by an integer ID.
683 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
684 * location codes as documented by PAPR+ v2.7, 12.3.1.5
687 case SPAPR_DR_CONNECTOR_TYPE_CPU:
688 drc->name = g_strdup_printf("CPU %d", id);
690 case SPAPR_DR_CONNECTOR_TYPE_PHB:
691 drc->name = g_strdup_printf("PHB %d", id);
693 case SPAPR_DR_CONNECTOR_TYPE_VIO:
694 case SPAPR_DR_CONNECTOR_TYPE_PCI:
695 drc->name = g_strdup_printf("C%d", id);
697 case SPAPR_DR_CONNECTOR_TYPE_LMB:
698 drc->name = g_strdup_printf("LMB %d", id);
704 /* PCI slot always start in a USABLE state, and stay there */
705 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
706 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
712 static void spapr_dr_connector_instance_init(Object *obj)
714 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
716 object_property_add_uint32_ptr(obj, "isolation-state",
717 &drc->isolation_state, NULL);
718 object_property_add_uint32_ptr(obj, "indicator-state",
719 &drc->indicator_state, NULL);
720 object_property_add_uint32_ptr(obj, "allocation-state",
721 &drc->allocation_state, NULL);
722 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
723 object_property_add(obj, "index", "uint32", prop_get_index,
724 NULL, NULL, NULL, NULL);
725 object_property_add(obj, "connector_type", "uint32", prop_get_type,
726 NULL, NULL, NULL, NULL);
727 object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
728 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
729 NULL, NULL, NULL, NULL);
730 object_property_add(obj, "fdt", "struct", prop_get_fdt,
731 NULL, NULL, NULL, NULL);
734 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
736 DeviceClass *dk = DEVICE_CLASS(k);
737 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
740 dk->realize = realize;
741 dk->unrealize = unrealize;
742 drck->set_isolation_state = set_isolation_state;
743 drck->set_indicator_state = set_indicator_state;
744 drck->set_allocation_state = set_allocation_state;
745 drck->get_index = get_index;
746 drck->get_type = get_type;
747 drck->get_name = get_name;
748 drck->set_configured = set_configured;
749 drck->entity_sense = entity_sense;
750 drck->attach = attach;
751 drck->detach = detach;
752 drck->release_pending = release_pending;
753 drck->set_signalled = set_signalled;
755 * Reason: it crashes FIXME find and document the real reason
757 dk->user_creatable = false;
760 static const TypeInfo spapr_dr_connector_info = {
761 .name = TYPE_SPAPR_DR_CONNECTOR,
762 .parent = TYPE_DEVICE,
763 .instance_size = sizeof(sPAPRDRConnector),
764 .instance_init = spapr_dr_connector_instance_init,
765 .class_size = sizeof(sPAPRDRConnectorClass),
766 .class_init = spapr_dr_connector_class_init,
769 /* helper functions for external users */
771 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
776 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
777 obj = object_resolve_path(name, NULL);
779 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
782 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
785 return spapr_dr_connector_by_index(
786 (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
787 (id & DRC_INDEX_ID_MASK));
790 /* generate a string the describes the DRC to encode into the
793 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
795 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
798 case SPAPR_DR_CONNECTOR_TYPE_CPU:
800 case SPAPR_DR_CONNECTOR_TYPE_PHB:
802 case SPAPR_DR_CONNECTOR_TYPE_VIO:
804 case SPAPR_DR_CONNECTOR_TYPE_PCI:
806 case SPAPR_DR_CONNECTOR_TYPE_LMB:
816 * spapr_drc_populate_dt
818 * @fdt: libfdt device tree
819 * @path: path in the DT to generate properties
820 * @owner: parent Object/DeviceState for which to generate DRC
822 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
823 * to the types of DRCs to generate entries for
825 * generate OF properties to describe DRC topology/indices to guests
827 * as documented in PAPR+ v2.1, 13.5.2
829 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
830 uint32_t drc_type_mask)
832 Object *root_container;
833 ObjectProperty *prop;
834 ObjectPropertyIterator iter;
835 uint32_t drc_count = 0;
836 GArray *drc_indexes, *drc_power_domains;
837 GString *drc_names, *drc_types;
840 /* the first entry of each properties is a 32-bit integer encoding
841 * the number of elements in the array. we won't know this until
842 * we complete the iteration through all the matching DRCs, but
843 * reserve the space now and set the offsets accordingly so we
844 * can fill them in later.
846 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
847 drc_indexes = g_array_set_size(drc_indexes, 1);
848 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
849 drc_power_domains = g_array_set_size(drc_power_domains, 1);
850 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
851 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
853 /* aliases for all DRConnector objects will be rooted in QOM
854 * composition tree at DRC_CONTAINER_PATH
856 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
858 object_property_iter_init(&iter, root_container);
859 while ((prop = object_property_iter_next(&iter))) {
861 sPAPRDRConnector *drc;
862 sPAPRDRConnectorClass *drck;
863 uint32_t drc_index, drc_power_domain;
865 if (!strstart(prop->type, "link<", NULL)) {
869 obj = object_property_get_link(root_container, prop->name, NULL);
870 drc = SPAPR_DR_CONNECTOR(obj);
871 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
873 if (owner && (drc->owner != owner)) {
877 if ((drc->type & drc_type_mask) == 0) {
883 /* ibm,drc-indexes */
884 drc_index = cpu_to_be32(drck->get_index(drc));
885 g_array_append_val(drc_indexes, drc_index);
887 /* ibm,drc-power-domains */
888 drc_power_domain = cpu_to_be32(-1);
889 g_array_append_val(drc_power_domains, drc_power_domain);
892 drc_names = g_string_append(drc_names, drck->get_name(drc));
893 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
896 drc_types = g_string_append(drc_types,
897 spapr_drc_get_type_str(drc->type));
898 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
901 /* now write the drc count into the space we reserved at the
902 * beginning of the arrays previously
904 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
905 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
906 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
907 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
909 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
911 drc_indexes->len * sizeof(uint32_t));
913 error_report("Couldn't create ibm,drc-indexes property");
917 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
918 drc_power_domains->data,
919 drc_power_domains->len * sizeof(uint32_t));
921 error_report("Couldn't finalize ibm,drc-power-domains property");
925 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
926 drc_names->str, drc_names->len);
928 error_report("Couldn't finalize ibm,drc-names property");
932 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
933 drc_types->str, drc_types->len);
935 error_report("Couldn't finalize ibm,drc-types property");
940 g_array_free(drc_indexes, true);
941 g_array_free(drc_power_domains, true);
942 g_string_free(drc_names, true);
943 g_string_free(drc_types, true);
952 static bool sensor_type_is_dr(uint32_t sensor_type)
954 switch (sensor_type) {
955 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
956 case RTAS_SENSOR_TYPE_DR:
957 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
964 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
965 uint32_t token, uint32_t nargs,
966 target_ulong args, uint32_t nret,
969 uint32_t sensor_type;
970 uint32_t sensor_index;
971 uint32_t sensor_state;
972 uint32_t ret = RTAS_OUT_SUCCESS;
973 sPAPRDRConnector *drc;
974 sPAPRDRConnectorClass *drck;
976 if (nargs != 3 || nret != 1) {
977 ret = RTAS_OUT_PARAM_ERROR;
981 sensor_type = rtas_ld(args, 0);
982 sensor_index = rtas_ld(args, 1);
983 sensor_state = rtas_ld(args, 2);
985 if (!sensor_type_is_dr(sensor_type)) {
986 goto out_unimplemented;
989 /* if this is a DR sensor we can assume sensor_index == drc_index */
990 drc = spapr_dr_connector_by_index(sensor_index);
992 trace_spapr_rtas_set_indicator_invalid(sensor_index);
993 ret = RTAS_OUT_PARAM_ERROR;
996 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
998 switch (sensor_type) {
999 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1000 /* if the guest is configuring a device attached to this
1001 * DRC, we should reset the configuration state at this
1002 * point since it may no longer be reliable (guest released
1003 * device and needs to start over, or unplug occurred so
1004 * the FDT is no longer valid)
1006 if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
1007 sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr,
1010 spapr_ccs_remove(spapr, ccs);
1013 ret = drck->set_isolation_state(drc, sensor_state);
1015 case RTAS_SENSOR_TYPE_DR:
1016 ret = drck->set_indicator_state(drc, sensor_state);
1018 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1019 ret = drck->set_allocation_state(drc, sensor_state);
1022 goto out_unimplemented;
1026 rtas_st(rets, 0, ret);
1030 /* currently only DR-related sensors are implemented */
1031 trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
1032 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
1035 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
1036 uint32_t token, uint32_t nargs,
1037 target_ulong args, uint32_t nret,
1040 uint32_t sensor_type;
1041 uint32_t sensor_index;
1042 uint32_t sensor_state = 0;
1043 sPAPRDRConnector *drc;
1044 sPAPRDRConnectorClass *drck;
1045 uint32_t ret = RTAS_OUT_SUCCESS;
1047 if (nargs != 2 || nret != 2) {
1048 ret = RTAS_OUT_PARAM_ERROR;
1052 sensor_type = rtas_ld(args, 0);
1053 sensor_index = rtas_ld(args, 1);
1055 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1056 /* currently only DR-related sensors are implemented */
1057 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1059 ret = RTAS_OUT_NOT_SUPPORTED;
1063 drc = spapr_dr_connector_by_index(sensor_index);
1065 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1066 ret = RTAS_OUT_PARAM_ERROR;
1069 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1070 ret = drck->entity_sense(drc, &sensor_state);
1073 rtas_st(rets, 0, ret);
1074 rtas_st(rets, 1, sensor_state);
1077 /* configure-connector work area offsets, int32_t units for field
1078 * indexes, bytes for field offset/len values.
1080 * as documented by PAPR+ v2.7, 13.5.3.5
1082 #define CC_IDX_NODE_NAME_OFFSET 2
1083 #define CC_IDX_PROP_NAME_OFFSET 2
1084 #define CC_IDX_PROP_LEN 3
1085 #define CC_IDX_PROP_DATA_OFFSET 4
1086 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1087 #define CC_WA_LEN 4096
1089 static void configure_connector_st(target_ulong addr, target_ulong offset,
1090 const void *buf, size_t len)
1092 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1093 buf, MIN(len, CC_WA_LEN - offset));
1096 void spapr_ccs_reset_hook(void *opaque)
1098 sPAPRMachineState *spapr = opaque;
1099 sPAPRConfigureConnectorState *ccs, *ccs_tmp;
1101 QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) {
1102 spapr_ccs_remove(spapr, ccs);
1106 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1107 sPAPRMachineState *spapr,
1108 uint32_t token, uint32_t nargs,
1109 target_ulong args, uint32_t nret,
1115 sPAPRDRConnector *drc;
1116 sPAPRDRConnectorClass *drck;
1117 sPAPRConfigureConnectorState *ccs;
1118 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1121 if (nargs != 2 || nret != 1) {
1122 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1126 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1128 drc_index = rtas_ld(wa_addr, 0);
1129 drc = spapr_dr_connector_by_index(drc_index);
1131 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1132 rc = RTAS_OUT_PARAM_ERROR;
1136 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1138 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1139 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1143 ccs = spapr_ccs_find(spapr, drc_index);
1145 ccs = g_new0(sPAPRConfigureConnectorState, 1);
1146 ccs->fdt_offset = drc->fdt_start_offset;
1147 ccs->drc_index = drc_index;
1148 spapr_ccs_add(spapr, ccs);
1154 const struct fdt_property *prop;
1155 int fdt_offset_next, prop_len;
1157 tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1160 case FDT_BEGIN_NODE:
1162 name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1164 /* provide the name of the next OF node */
1165 wa_offset = CC_VAL_DATA_OFFSET;
1166 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1167 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1168 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1172 if (ccs->fdt_depth == 0) {
1173 /* done sending the device tree, don't need to track
1176 drck->set_configured(drc);
1177 spapr_ccs_remove(spapr, ccs);
1179 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1181 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1185 prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1187 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1189 /* provide the name of the next OF property */
1190 wa_offset = CC_VAL_DATA_OFFSET;
1191 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1192 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1194 /* provide the length and value of the OF property. data gets
1195 * placed immediately after NULL terminator of the OF property's
1198 wa_offset += strlen(name) + 1,
1199 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1200 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1201 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1202 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1205 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1207 /* keep seeking for an actionable tag */
1211 ccs->fdt_offset = fdt_offset_next;
1213 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1217 rtas_st(rets, 0, rc);
1220 static void spapr_drc_register_types(void)
1222 type_register_static(&spapr_dr_connector_info);
1224 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1225 rtas_set_indicator);
1226 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1227 rtas_get_sensor_state);
1228 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1229 rtas_ibm_configure_connector);
1231 type_init(spapr_drc_register_types)