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 sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc)
60 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
62 return 1 << drck->typeshift;
65 uint32_t spapr_drc_index(sPAPRDRConnector *drc)
67 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
69 /* no set format for a drc index: it only needs to be globally
70 * unique. this is how we encode the DRC type on bare-metal
71 * however, so might as well do that here
73 return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
74 | (drc->id & DRC_INDEX_ID_MASK);
77 static uint32_t set_isolation_state(sPAPRDRConnector *drc,
78 sPAPRDRIsolationState state)
80 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
82 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
84 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
85 /* cannot unisolate a non-existent resource, and, or resources
86 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
89 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
90 return RTAS_OUT_NO_SUCH_INDICATOR;
95 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
96 * belong to a DIMM device that is marked for removal.
98 * Currently the guest userspace tool drmgr that drives the memory
99 * hotplug/unplug will just try to remove a set of 'removable' LMBs
100 * in response to a hot unplug request that is based on drc-count.
101 * If the LMB being removed doesn't belong to a DIMM device that is
102 * actually being unplugged, fail the isolation request here.
104 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
105 if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
106 !drc->awaiting_release) {
107 return RTAS_OUT_HW_ERROR;
111 drc->isolation_state = state;
113 if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
114 /* if we're awaiting release, but still in an unconfigured state,
115 * it's likely the guest is still in the process of configuring
116 * the device and is transitioning the devices to an ISOLATED
117 * state as a part of that process. so we only complete the
118 * removal when this transition happens for a device in a
119 * configured state, as suggested by the state diagram from
122 if (drc->awaiting_release) {
123 uint32_t drc_index = spapr_drc_index(drc);
124 if (drc->configured) {
125 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
126 drck->detach(drc, DEVICE(drc->dev), NULL);
128 trace_spapr_drc_set_isolation_state_deferring(drc_index);
131 drc->configured = false;
134 return RTAS_OUT_SUCCESS;
137 static uint32_t set_indicator_state(sPAPRDRConnector *drc,
138 sPAPRDRIndicatorState state)
140 trace_spapr_drc_set_indicator_state(spapr_drc_index(drc), state);
141 drc->indicator_state = state;
142 return RTAS_OUT_SUCCESS;
145 static uint32_t set_allocation_state(sPAPRDRConnector *drc,
146 sPAPRDRAllocationState state)
148 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
150 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
152 if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
153 /* if there's no resource/device associated with the DRC, there's
154 * no way for us to put it in an allocation state consistent with
155 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
156 * result in an RTAS return code of -3 / "no such indicator"
159 return RTAS_OUT_NO_SUCH_INDICATOR;
161 if (drc->awaiting_release && drc->awaiting_allocation) {
162 /* kernel is acknowledging a previous hotplug event
163 * while we are already removing it.
164 * it's safe to ignore awaiting_allocation here since we know the
165 * situation is predicated on the guest either already having done
166 * so (boot-time hotplug), or never being able to acquire in the
167 * first place (hotplug followed by immediate unplug).
169 drc->awaiting_allocation_skippable = true;
170 return RTAS_OUT_NO_SUCH_INDICATOR;
174 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
175 drc->allocation_state = state;
176 if (drc->awaiting_release &&
177 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
178 uint32_t drc_index = spapr_drc_index(drc);
179 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
180 drck->detach(drc, DEVICE(drc->dev), NULL);
181 } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
182 drc->awaiting_allocation = false;
185 return RTAS_OUT_SUCCESS;
188 static const char *get_name(sPAPRDRConnector *drc)
193 /* has the guest been notified of device attachment? */
194 static void set_signalled(sPAPRDRConnector *drc)
196 drc->signalled = true;
200 * dr-entity-sense sensor value
201 * returned via get-sensor-state RTAS calls
202 * as expected by state diagram in PAPR+ 2.7, 13.4
203 * based on the current allocation/indicator/power states
204 * for the DR connector.
206 static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
209 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
210 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
211 /* for logical DR, we return a state of UNUSABLE
212 * iff the allocation state UNUSABLE.
213 * Otherwise, report the state as USABLE/PRESENT,
214 * as we would for PCI.
216 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
218 /* this assumes all PCI devices are assigned to
219 * a 'live insertion' power domain, where QEMU
220 * manages power state automatically as opposed
221 * to the guest. present, non-PCI resources are
222 * unaffected by power state.
224 *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
227 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
228 /* PCI devices, and only PCI devices, use EMPTY
229 * in cases where we'd otherwise use UNUSABLE
231 *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
233 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
237 trace_spapr_drc_entity_sense(spapr_drc_index(drc), *state);
238 return RTAS_OUT_SUCCESS;
241 static void prop_get_index(Object *obj, Visitor *v, const char *name,
242 void *opaque, Error **errp)
244 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
245 uint32_t value = spapr_drc_index(drc);
246 visit_type_uint32(v, name, &value, errp);
249 static void prop_get_type(Object *obj, Visitor *v, const char *name,
250 void *opaque, Error **errp)
252 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
253 uint32_t value = (uint32_t)spapr_drc_type(drc);
254 visit_type_uint32(v, name, &value, errp);
257 static char *prop_get_name(Object *obj, Error **errp)
259 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
260 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
261 return g_strdup(drck->get_name(drc));
264 static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
265 void *opaque, Error **errp)
267 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
268 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
271 drck->entity_sense(drc, &value);
272 visit_type_uint32(v, name, &value, errp);
275 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
276 void *opaque, Error **errp)
278 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
280 int fdt_offset_next, fdt_offset, fdt_depth;
284 visit_type_null(v, NULL, errp);
289 fdt_offset = drc->fdt_start_offset;
293 const char *name = NULL;
294 const struct fdt_property *prop = NULL;
295 int prop_len = 0, name_len = 0;
298 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
302 name = fdt_get_name(fdt, fdt_offset, &name_len);
303 visit_start_struct(v, name, NULL, 0, &err);
305 error_propagate(errp, err);
310 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
311 g_assert(fdt_depth > 0);
312 visit_check_struct(v, &err);
313 visit_end_struct(v, NULL);
315 error_propagate(errp, err);
322 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
323 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
324 visit_start_list(v, name, NULL, 0, &err);
326 error_propagate(errp, err);
329 for (i = 0; i < prop_len; i++) {
330 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
332 error_propagate(errp, err);
336 visit_check_list(v, &err);
337 visit_end_list(v, NULL);
339 error_propagate(errp, err);
345 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
347 fdt_offset = fdt_offset_next;
348 } while (fdt_depth != 0);
351 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
352 int fdt_start_offset, bool coldplug, Error **errp)
354 trace_spapr_drc_attach(spapr_drc_index(drc));
356 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
357 error_setg(errp, "an attached device is still awaiting release");
360 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
361 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
363 g_assert(fdt || coldplug);
365 /* NOTE: setting initial isolation state to UNISOLATED means we can't
366 * detach unless guest has a userspace/kernel that moves this state
367 * back to ISOLATED in response to an unplug event, or this is done
368 * manually by the admin prior. if we force things while the guest
369 * may be accessing the device, we can easily crash the guest, so we
370 * we defer completion of removal in such cases to the reset() hook.
372 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
373 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
375 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
379 drc->fdt_start_offset = fdt_start_offset;
380 drc->configured = coldplug;
381 /* 'logical' DR resources such as memory/cpus are in some cases treated
382 * as a pool of resources from which the guest is free to choose from
383 * based on only a count. for resources that can be assigned in this
384 * fashion, we must assume the resource is signalled immediately
385 * since a single hotplug request might make an arbitrary number of
386 * such attached resources available to the guest, as opposed to
387 * 'physical' DR resources such as PCI where each device/resource is
388 * signalled individually.
390 drc->signalled = (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI)
393 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
394 drc->awaiting_allocation = true;
397 object_property_add_link(OBJECT(drc), "device",
398 object_get_typename(OBJECT(drc->dev)),
399 (Object **)(&drc->dev),
403 static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
405 trace_spapr_drc_detach(spapr_drc_index(drc));
407 /* if we've signalled device presence to the guest, or if the guest
408 * has gone ahead and configured the device (via manually-executed
409 * device add via drmgr in guest, namely), we need to wait
410 * for the guest to quiesce the device before completing detach.
411 * Otherwise, we can assume the guest hasn't seen it and complete the
412 * detach immediately. Note that there is a small race window
413 * just before, or during, configuration, which is this context
414 * refers mainly to fetching the device tree via RTAS.
415 * During this window the device access will be arbitrated by
416 * associated DRC, which will simply fail the RTAS calls as invalid.
417 * This is recoverable within guest and current implementations of
418 * drmgr should be able to cope.
420 if (!drc->signalled && !drc->configured) {
421 /* if the guest hasn't seen the device we can't rely on it to
422 * set it back to an isolated state via RTAS, so do it here manually
424 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
427 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
428 trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
429 drc->awaiting_release = true;
433 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
434 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
435 trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
436 drc->awaiting_release = true;
440 if (drc->awaiting_allocation) {
441 if (!drc->awaiting_allocation_skippable) {
442 drc->awaiting_release = true;
443 trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
448 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
450 /* Calling release callbacks based on spapr_drc_type(drc). */
451 switch (spapr_drc_type(drc)) {
452 case SPAPR_DR_CONNECTOR_TYPE_CPU:
453 spapr_core_release(drc->dev);
455 case SPAPR_DR_CONNECTOR_TYPE_PCI:
456 spapr_phb_remove_pci_device_cb(drc->dev);
458 case SPAPR_DR_CONNECTOR_TYPE_LMB:
459 spapr_lmb_release(drc->dev);
461 case SPAPR_DR_CONNECTOR_TYPE_PHB:
462 case SPAPR_DR_CONNECTOR_TYPE_VIO:
467 drc->awaiting_release = false;
468 drc->awaiting_allocation_skippable = false;
471 drc->fdt_start_offset = 0;
472 object_property_del(OBJECT(drc), "device", NULL);
476 static bool release_pending(sPAPRDRConnector *drc)
478 return drc->awaiting_release;
481 static void reset(DeviceState *d)
483 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
484 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
485 sPAPRDREntitySense state;
487 trace_spapr_drc_reset(spapr_drc_index(drc));
488 /* immediately upon reset we can safely assume DRCs whose devices
489 * are pending removal can be safely removed, and that they will
490 * subsequently be left in an ISOLATED state. move the DRC to this
491 * state in these cases (which will in turn complete any pending
494 if (drc->awaiting_release) {
495 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
496 /* generally this should also finalize the removal, but if the device
497 * hasn't yet been configured we normally defer removal under the
498 * assumption that this transition is taking place as part of device
499 * configuration. so check if we're still waiting after this, and
500 * force removal if we are
502 if (drc->awaiting_release) {
503 drck->detach(drc, DEVICE(drc->dev), NULL);
506 /* non-PCI devices may be awaiting a transition to UNUSABLE */
507 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
508 drc->awaiting_release) {
509 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
513 drck->entity_sense(drc, &state);
514 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
515 drck->set_signalled(drc);
519 static bool spapr_drc_needed(void *opaque)
521 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
522 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
524 sPAPRDREntitySense value;
525 drck->entity_sense(drc, &value);
527 /* If no dev is plugged in there is no need to migrate the DRC state */
528 if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
533 * If there is dev plugged in, we need to migrate the DRC state when
534 * it is different from cold-plugged state
536 switch (spapr_drc_type(drc)) {
537 case SPAPR_DR_CONNECTOR_TYPE_PCI:
538 case SPAPR_DR_CONNECTOR_TYPE_CPU:
539 case SPAPR_DR_CONNECTOR_TYPE_LMB:
540 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
541 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
542 drc->configured && drc->signalled && !drc->awaiting_release);
544 case SPAPR_DR_CONNECTOR_TYPE_PHB:
545 case SPAPR_DR_CONNECTOR_TYPE_VIO:
547 g_assert_not_reached();
552 static const VMStateDescription vmstate_spapr_drc = {
555 .minimum_version_id = 1,
556 .needed = spapr_drc_needed,
557 .fields = (VMStateField []) {
558 VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
559 VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
560 VMSTATE_UINT32(indicator_state, sPAPRDRConnector),
561 VMSTATE_BOOL(configured, sPAPRDRConnector),
562 VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
563 VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
564 VMSTATE_BOOL(signalled, sPAPRDRConnector),
565 VMSTATE_END_OF_LIST()
569 static void realize(DeviceState *d, Error **errp)
571 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
572 Object *root_container;
577 trace_spapr_drc_realize(spapr_drc_index(drc));
578 /* NOTE: we do this as part of realize/unrealize due to the fact
579 * that the guest will communicate with the DRC via RTAS calls
580 * referencing the global DRC index. By unlinking the DRC
581 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
582 * inaccessible by the guest, since lookups rely on this path
583 * existing in the composition tree
585 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
586 snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
587 child_name = object_get_canonical_path_component(OBJECT(drc));
588 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
589 object_property_add_alias(root_container, link_name,
590 drc->owner, child_name, &err);
592 error_report_err(err);
593 object_unref(OBJECT(drc));
596 vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
598 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
601 static void unrealize(DeviceState *d, Error **errp)
603 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
604 Object *root_container;
608 trace_spapr_drc_unrealize(spapr_drc_index(drc));
609 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
610 snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
611 object_property_del(root_container, name, &err);
613 error_report_err(err);
614 object_unref(OBJECT(drc));
618 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
621 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
626 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
627 spapr_drc_index(drc));
628 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
629 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
632 /* human-readable name for a DRC to encode into the DT
633 * description. this is mainly only used within a guest in place
634 * of the unique DRC index.
636 * in the case of VIO/PCI devices, it corresponds to a
637 * "location code" that maps a logical device/function (DRC index)
638 * to a physical (or virtual in the case of VIO) location in the
639 * system by chaining together the "location label" for each
640 * encapsulating component.
642 * since this is more to do with diagnosing physical hardware
643 * issues than guest compatibility, we choose location codes/DRC
644 * names that adhere to the documented format, but avoid encoding
645 * the entire topology information into the label/code, instead
646 * just using the location codes based on the labels for the
647 * endpoints (VIO/PCI adaptor connectors), which is basically
648 * just "C" followed by an integer ID.
650 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
651 * location codes as documented by PAPR+ v2.7, 12.3.1.5
653 switch (spapr_drc_type(drc)) {
654 case SPAPR_DR_CONNECTOR_TYPE_CPU:
655 drc->name = g_strdup_printf("CPU %d", id);
657 case SPAPR_DR_CONNECTOR_TYPE_PHB:
658 drc->name = g_strdup_printf("PHB %d", id);
660 case SPAPR_DR_CONNECTOR_TYPE_VIO:
661 case SPAPR_DR_CONNECTOR_TYPE_PCI:
662 drc->name = g_strdup_printf("C%d", id);
664 case SPAPR_DR_CONNECTOR_TYPE_LMB:
665 drc->name = g_strdup_printf("LMB %d", id);
671 /* PCI slot always start in a USABLE state, and stay there */
672 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
673 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
679 static void spapr_dr_connector_instance_init(Object *obj)
681 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
683 object_property_add_uint32_ptr(obj, "isolation-state",
684 &drc->isolation_state, NULL);
685 object_property_add_uint32_ptr(obj, "indicator-state",
686 &drc->indicator_state, NULL);
687 object_property_add_uint32_ptr(obj, "allocation-state",
688 &drc->allocation_state, NULL);
689 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
690 object_property_add(obj, "index", "uint32", prop_get_index,
691 NULL, NULL, NULL, NULL);
692 object_property_add(obj, "connector_type", "uint32", prop_get_type,
693 NULL, NULL, NULL, NULL);
694 object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
695 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
696 NULL, NULL, NULL, NULL);
697 object_property_add(obj, "fdt", "struct", prop_get_fdt,
698 NULL, NULL, NULL, NULL);
701 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
703 DeviceClass *dk = DEVICE_CLASS(k);
704 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
707 dk->realize = realize;
708 dk->unrealize = unrealize;
709 drck->set_isolation_state = set_isolation_state;
710 drck->set_indicator_state = set_indicator_state;
711 drck->set_allocation_state = set_allocation_state;
712 drck->get_name = get_name;
713 drck->entity_sense = entity_sense;
714 drck->attach = attach;
715 drck->detach = detach;
716 drck->release_pending = release_pending;
717 drck->set_signalled = set_signalled;
719 * Reason: it crashes FIXME find and document the real reason
721 dk->user_creatable = false;
724 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
726 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
728 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
731 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
733 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
735 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
738 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
740 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
742 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
745 static const TypeInfo spapr_dr_connector_info = {
746 .name = TYPE_SPAPR_DR_CONNECTOR,
747 .parent = TYPE_DEVICE,
748 .instance_size = sizeof(sPAPRDRConnector),
749 .instance_init = spapr_dr_connector_instance_init,
750 .class_size = sizeof(sPAPRDRConnectorClass),
751 .class_init = spapr_dr_connector_class_init,
755 static const TypeInfo spapr_drc_physical_info = {
756 .name = TYPE_SPAPR_DRC_PHYSICAL,
757 .parent = TYPE_SPAPR_DR_CONNECTOR,
758 .instance_size = sizeof(sPAPRDRConnector),
762 static const TypeInfo spapr_drc_logical_info = {
763 .name = TYPE_SPAPR_DRC_LOGICAL,
764 .parent = TYPE_SPAPR_DR_CONNECTOR,
765 .instance_size = sizeof(sPAPRDRConnector),
769 static const TypeInfo spapr_drc_cpu_info = {
770 .name = TYPE_SPAPR_DRC_CPU,
771 .parent = TYPE_SPAPR_DRC_LOGICAL,
772 .instance_size = sizeof(sPAPRDRConnector),
773 .class_init = spapr_drc_cpu_class_init,
776 static const TypeInfo spapr_drc_pci_info = {
777 .name = TYPE_SPAPR_DRC_PCI,
778 .parent = TYPE_SPAPR_DRC_PHYSICAL,
779 .instance_size = sizeof(sPAPRDRConnector),
780 .class_init = spapr_drc_pci_class_init,
783 static const TypeInfo spapr_drc_lmb_info = {
784 .name = TYPE_SPAPR_DRC_LMB,
785 .parent = TYPE_SPAPR_DRC_LOGICAL,
786 .instance_size = sizeof(sPAPRDRConnector),
787 .class_init = spapr_drc_lmb_class_init,
790 /* helper functions for external users */
792 sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
797 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
798 obj = object_resolve_path(name, NULL);
800 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
803 sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
805 sPAPRDRConnectorClass *drck
806 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
808 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
809 | (id & DRC_INDEX_ID_MASK));
812 /* generate a string the describes the DRC to encode into the
815 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
817 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
820 case SPAPR_DR_CONNECTOR_TYPE_CPU:
822 case SPAPR_DR_CONNECTOR_TYPE_PHB:
824 case SPAPR_DR_CONNECTOR_TYPE_VIO:
826 case SPAPR_DR_CONNECTOR_TYPE_PCI:
828 case SPAPR_DR_CONNECTOR_TYPE_LMB:
838 * spapr_drc_populate_dt
840 * @fdt: libfdt device tree
841 * @path: path in the DT to generate properties
842 * @owner: parent Object/DeviceState for which to generate DRC
844 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
845 * to the types of DRCs to generate entries for
847 * generate OF properties to describe DRC topology/indices to guests
849 * as documented in PAPR+ v2.1, 13.5.2
851 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
852 uint32_t drc_type_mask)
854 Object *root_container;
855 ObjectProperty *prop;
856 ObjectPropertyIterator iter;
857 uint32_t drc_count = 0;
858 GArray *drc_indexes, *drc_power_domains;
859 GString *drc_names, *drc_types;
862 /* the first entry of each properties is a 32-bit integer encoding
863 * the number of elements in the array. we won't know this until
864 * we complete the iteration through all the matching DRCs, but
865 * reserve the space now and set the offsets accordingly so we
866 * can fill them in later.
868 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
869 drc_indexes = g_array_set_size(drc_indexes, 1);
870 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
871 drc_power_domains = g_array_set_size(drc_power_domains, 1);
872 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
873 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
875 /* aliases for all DRConnector objects will be rooted in QOM
876 * composition tree at DRC_CONTAINER_PATH
878 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
880 object_property_iter_init(&iter, root_container);
881 while ((prop = object_property_iter_next(&iter))) {
883 sPAPRDRConnector *drc;
884 sPAPRDRConnectorClass *drck;
885 uint32_t drc_index, drc_power_domain;
887 if (!strstart(prop->type, "link<", NULL)) {
891 obj = object_property_get_link(root_container, prop->name, NULL);
892 drc = SPAPR_DR_CONNECTOR(obj);
893 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
895 if (owner && (drc->owner != owner)) {
899 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
905 /* ibm,drc-indexes */
906 drc_index = cpu_to_be32(spapr_drc_index(drc));
907 g_array_append_val(drc_indexes, drc_index);
909 /* ibm,drc-power-domains */
910 drc_power_domain = cpu_to_be32(-1);
911 g_array_append_val(drc_power_domains, drc_power_domain);
914 drc_names = g_string_append(drc_names, drck->get_name(drc));
915 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
918 drc_types = g_string_append(drc_types,
919 spapr_drc_get_type_str(spapr_drc_type(drc)));
920 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
923 /* now write the drc count into the space we reserved at the
924 * beginning of the arrays previously
926 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
927 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
928 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
929 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
931 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
933 drc_indexes->len * sizeof(uint32_t));
935 error_report("Couldn't create ibm,drc-indexes property");
939 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
940 drc_power_domains->data,
941 drc_power_domains->len * sizeof(uint32_t));
943 error_report("Couldn't finalize ibm,drc-power-domains property");
947 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
948 drc_names->str, drc_names->len);
950 error_report("Couldn't finalize ibm,drc-names property");
954 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
955 drc_types->str, drc_types->len);
957 error_report("Couldn't finalize ibm,drc-types property");
962 g_array_free(drc_indexes, true);
963 g_array_free(drc_power_domains, true);
964 g_string_free(drc_names, true);
965 g_string_free(drc_types, true);
974 static bool sensor_type_is_dr(uint32_t sensor_type)
976 switch (sensor_type) {
977 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
978 case RTAS_SENSOR_TYPE_DR:
979 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
986 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
987 uint32_t token, uint32_t nargs,
988 target_ulong args, uint32_t nret,
991 uint32_t sensor_type;
992 uint32_t sensor_index;
993 uint32_t sensor_state;
994 uint32_t ret = RTAS_OUT_SUCCESS;
995 sPAPRDRConnector *drc;
996 sPAPRDRConnectorClass *drck;
998 if (nargs != 3 || nret != 1) {
999 ret = RTAS_OUT_PARAM_ERROR;
1003 sensor_type = rtas_ld(args, 0);
1004 sensor_index = rtas_ld(args, 1);
1005 sensor_state = rtas_ld(args, 2);
1007 if (!sensor_type_is_dr(sensor_type)) {
1008 goto out_unimplemented;
1011 /* if this is a DR sensor we can assume sensor_index == drc_index */
1012 drc = spapr_drc_by_index(sensor_index);
1014 trace_spapr_rtas_set_indicator_invalid(sensor_index);
1015 ret = RTAS_OUT_PARAM_ERROR;
1018 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1020 switch (sensor_type) {
1021 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1022 /* if the guest is configuring a device attached to this
1023 * DRC, we should reset the configuration state at this
1024 * point since it may no longer be reliable (guest released
1025 * device and needs to start over, or unplug occurred so
1026 * the FDT is no longer valid)
1028 if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
1029 sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr,
1032 spapr_ccs_remove(spapr, ccs);
1035 ret = drck->set_isolation_state(drc, sensor_state);
1037 case RTAS_SENSOR_TYPE_DR:
1038 ret = drck->set_indicator_state(drc, sensor_state);
1040 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1041 ret = drck->set_allocation_state(drc, sensor_state);
1044 goto out_unimplemented;
1048 rtas_st(rets, 0, ret);
1052 /* currently only DR-related sensors are implemented */
1053 trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
1054 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
1057 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
1058 uint32_t token, uint32_t nargs,
1059 target_ulong args, uint32_t nret,
1062 uint32_t sensor_type;
1063 uint32_t sensor_index;
1064 uint32_t sensor_state = 0;
1065 sPAPRDRConnector *drc;
1066 sPAPRDRConnectorClass *drck;
1067 uint32_t ret = RTAS_OUT_SUCCESS;
1069 if (nargs != 2 || nret != 2) {
1070 ret = RTAS_OUT_PARAM_ERROR;
1074 sensor_type = rtas_ld(args, 0);
1075 sensor_index = rtas_ld(args, 1);
1077 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1078 /* currently only DR-related sensors are implemented */
1079 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1081 ret = RTAS_OUT_NOT_SUPPORTED;
1085 drc = spapr_drc_by_index(sensor_index);
1087 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1088 ret = RTAS_OUT_PARAM_ERROR;
1091 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1092 ret = drck->entity_sense(drc, &sensor_state);
1095 rtas_st(rets, 0, ret);
1096 rtas_st(rets, 1, sensor_state);
1099 /* configure-connector work area offsets, int32_t units for field
1100 * indexes, bytes for field offset/len values.
1102 * as documented by PAPR+ v2.7, 13.5.3.5
1104 #define CC_IDX_NODE_NAME_OFFSET 2
1105 #define CC_IDX_PROP_NAME_OFFSET 2
1106 #define CC_IDX_PROP_LEN 3
1107 #define CC_IDX_PROP_DATA_OFFSET 4
1108 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1109 #define CC_WA_LEN 4096
1111 static void configure_connector_st(target_ulong addr, target_ulong offset,
1112 const void *buf, size_t len)
1114 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1115 buf, MIN(len, CC_WA_LEN - offset));
1118 void spapr_ccs_reset_hook(void *opaque)
1120 sPAPRMachineState *spapr = opaque;
1121 sPAPRConfigureConnectorState *ccs, *ccs_tmp;
1123 QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) {
1124 spapr_ccs_remove(spapr, ccs);
1128 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1129 sPAPRMachineState *spapr,
1130 uint32_t token, uint32_t nargs,
1131 target_ulong args, uint32_t nret,
1137 sPAPRDRConnector *drc;
1138 sPAPRConfigureConnectorState *ccs;
1139 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1142 if (nargs != 2 || nret != 1) {
1143 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1147 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1149 drc_index = rtas_ld(wa_addr, 0);
1150 drc = spapr_drc_by_index(drc_index);
1152 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1153 rc = RTAS_OUT_PARAM_ERROR;
1158 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1159 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1163 ccs = spapr_ccs_find(spapr, drc_index);
1165 ccs = g_new0(sPAPRConfigureConnectorState, 1);
1166 ccs->fdt_offset = drc->fdt_start_offset;
1167 ccs->drc_index = drc_index;
1168 spapr_ccs_add(spapr, ccs);
1174 const struct fdt_property *prop;
1175 int fdt_offset_next, prop_len;
1177 tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1180 case FDT_BEGIN_NODE:
1182 name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1184 /* provide the name of the next OF node */
1185 wa_offset = CC_VAL_DATA_OFFSET;
1186 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1187 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1188 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1192 if (ccs->fdt_depth == 0) {
1193 sPAPRDRIsolationState state = drc->isolation_state;
1194 uint32_t drc_index = spapr_drc_index(drc);
1195 /* done sending the device tree, don't need to track
1198 trace_spapr_drc_set_configured(drc_index);
1199 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
1200 drc->configured = true;
1202 /* guest should be not configuring an isolated device */
1203 trace_spapr_drc_set_configured_skipping(drc_index);
1205 spapr_ccs_remove(spapr, ccs);
1207 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1209 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1213 prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1215 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1217 /* provide the name of the next OF property */
1218 wa_offset = CC_VAL_DATA_OFFSET;
1219 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1220 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1222 /* provide the length and value of the OF property. data gets
1223 * placed immediately after NULL terminator of the OF property's
1226 wa_offset += strlen(name) + 1,
1227 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1228 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1229 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1230 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1233 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1235 /* keep seeking for an actionable tag */
1239 ccs->fdt_offset = fdt_offset_next;
1241 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1245 rtas_st(rets, 0, rc);
1248 static void spapr_drc_register_types(void)
1250 type_register_static(&spapr_dr_connector_info);
1251 type_register_static(&spapr_drc_physical_info);
1252 type_register_static(&spapr_drc_logical_info);
1253 type_register_static(&spapr_drc_cpu_info);
1254 type_register_static(&spapr_drc_pci_info);
1255 type_register_static(&spapr_drc_lmb_info);
1257 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1258 rtas_set_indicator);
1259 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1260 rtas_get_sensor_state);
1261 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1262 rtas_ibm_configure_connector);
1264 type_init(spapr_drc_register_types)