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 sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc)
32 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
34 return 1 << drck->typeshift;
37 uint32_t spapr_drc_index(sPAPRDRConnector *drc)
39 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
41 /* no set format for a drc index: it only needs to be globally
42 * unique. this is how we encode the DRC type on bare-metal
43 * however, so might as well do that here
45 return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
46 | (drc->id & DRC_INDEX_ID_MASK);
49 static uint32_t set_isolation_state(sPAPRDRConnector *drc,
50 sPAPRDRIsolationState state)
52 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
54 /* if the guest is configuring a device attached to this DRC, we
55 * should reset the configuration state at this point since it may
56 * no longer be reliable (guest released device and needs to start
57 * over, or unplug occurred so the FDT is no longer valid)
59 if (state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
64 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
65 /* cannot unisolate a non-existent resource, and, or resources
66 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
69 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
70 return RTAS_OUT_NO_SUCH_INDICATOR;
75 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
76 * belong to a DIMM device that is marked for removal.
78 * Currently the guest userspace tool drmgr that drives the memory
79 * hotplug/unplug will just try to remove a set of 'removable' LMBs
80 * in response to a hot unplug request that is based on drc-count.
81 * If the LMB being removed doesn't belong to a DIMM device that is
82 * actually being unplugged, fail the isolation request here.
84 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
85 if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
86 !drc->awaiting_release) {
87 return RTAS_OUT_HW_ERROR;
91 drc->isolation_state = state;
93 if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
94 /* if we're awaiting release, but still in an unconfigured state,
95 * it's likely the guest is still in the process of configuring
96 * the device and is transitioning the devices to an ISOLATED
97 * state as a part of that process. so we only complete the
98 * removal when this transition happens for a device in a
99 * configured state, as suggested by the state diagram from
102 if (drc->awaiting_release) {
103 uint32_t drc_index = spapr_drc_index(drc);
104 if (drc->configured) {
105 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
106 spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
108 trace_spapr_drc_set_isolation_state_deferring(drc_index);
111 drc->configured = false;
114 return RTAS_OUT_SUCCESS;
117 static uint32_t set_allocation_state(sPAPRDRConnector *drc,
118 sPAPRDRAllocationState state)
120 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
122 if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
123 /* if there's no resource/device associated with the DRC, there's
124 * no way for us to put it in an allocation state consistent with
125 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
126 * result in an RTAS return code of -3 / "no such indicator"
129 return RTAS_OUT_NO_SUCH_INDICATOR;
133 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
134 drc->allocation_state = state;
135 if (drc->awaiting_release &&
136 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
137 uint32_t drc_index = spapr_drc_index(drc);
138 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
139 spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
140 } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
141 drc->awaiting_allocation = false;
144 return RTAS_OUT_SUCCESS;
147 static const char *spapr_drc_name(sPAPRDRConnector *drc)
149 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
151 /* human-readable name for a DRC to encode into the DT
152 * description. this is mainly only used within a guest in place
153 * of the unique DRC index.
155 * in the case of VIO/PCI devices, it corresponds to a "location
156 * code" that maps a logical device/function (DRC index) to a
157 * physical (or virtual in the case of VIO) location in the system
158 * by chaining together the "location label" for each
159 * encapsulating component.
161 * since this is more to do with diagnosing physical hardware
162 * issues than guest compatibility, we choose location codes/DRC
163 * names that adhere to the documented format, but avoid encoding
164 * the entire topology information into the label/code, instead
165 * just using the location codes based on the labels for the
166 * endpoints (VIO/PCI adaptor connectors), which is basically just
167 * "C" followed by an integer ID.
169 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
170 * location codes as documented by PAPR+ v2.7, 12.3.1.5
172 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
175 /* has the guest been notified of device attachment? */
176 static void set_signalled(sPAPRDRConnector *drc)
178 drc->signalled = true;
182 * dr-entity-sense sensor value
183 * returned via get-sensor-state RTAS calls
184 * as expected by state diagram in PAPR+ 2.7, 13.4
185 * based on the current allocation/indicator/power states
186 * for the DR connector.
188 static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc)
190 /* this assumes all PCI devices are assigned to a 'live insertion'
191 * power domain, where QEMU manages power state automatically as
192 * opposed to the guest. present, non-PCI resources are unaffected
195 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
196 : SPAPR_DR_ENTITY_SENSE_EMPTY;
199 static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc)
202 && (drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE)) {
203 return SPAPR_DR_ENTITY_SENSE_PRESENT;
205 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
209 static void prop_get_index(Object *obj, Visitor *v, const char *name,
210 void *opaque, Error **errp)
212 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
213 uint32_t value = spapr_drc_index(drc);
214 visit_type_uint32(v, name, &value, errp);
217 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
218 void *opaque, Error **errp)
220 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
222 int fdt_offset_next, fdt_offset, fdt_depth;
226 visit_type_null(v, NULL, errp);
231 fdt_offset = drc->fdt_start_offset;
235 const char *name = NULL;
236 const struct fdt_property *prop = NULL;
237 int prop_len = 0, name_len = 0;
240 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
244 name = fdt_get_name(fdt, fdt_offset, &name_len);
245 visit_start_struct(v, name, NULL, 0, &err);
247 error_propagate(errp, err);
252 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
253 g_assert(fdt_depth > 0);
254 visit_check_struct(v, &err);
255 visit_end_struct(v, NULL);
257 error_propagate(errp, err);
264 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
265 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
266 visit_start_list(v, name, NULL, 0, &err);
268 error_propagate(errp, err);
271 for (i = 0; i < prop_len; i++) {
272 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
274 error_propagate(errp, err);
278 visit_check_list(v, &err);
279 visit_end_list(v, NULL);
281 error_propagate(errp, err);
287 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
289 fdt_offset = fdt_offset_next;
290 } while (fdt_depth != 0);
293 void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
294 int fdt_start_offset, bool coldplug, Error **errp)
296 trace_spapr_drc_attach(spapr_drc_index(drc));
298 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
299 error_setg(errp, "an attached device is still awaiting release");
302 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
303 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
305 g_assert(fdt || coldplug);
307 /* NOTE: setting initial isolation state to UNISOLATED means we can't
308 * detach unless guest has a userspace/kernel that moves this state
309 * back to ISOLATED in response to an unplug event, or this is done
310 * manually by the admin prior. if we force things while the guest
311 * may be accessing the device, we can easily crash the guest, so we
312 * we defer completion of removal in such cases to the reset() hook.
314 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
315 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
317 drc->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
321 drc->fdt_start_offset = fdt_start_offset;
322 drc->configured = coldplug;
323 /* 'logical' DR resources such as memory/cpus are in some cases treated
324 * as a pool of resources from which the guest is free to choose from
325 * based on only a count. for resources that can be assigned in this
326 * fashion, we must assume the resource is signalled immediately
327 * since a single hotplug request might make an arbitrary number of
328 * such attached resources available to the guest, as opposed to
329 * 'physical' DR resources such as PCI where each device/resource is
330 * signalled individually.
332 drc->signalled = (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI)
335 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
336 drc->awaiting_allocation = true;
339 object_property_add_link(OBJECT(drc), "device",
340 object_get_typename(OBJECT(drc->dev)),
341 (Object **)(&drc->dev),
345 void spapr_drc_detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
347 trace_spapr_drc_detach(spapr_drc_index(drc));
349 /* if we've signalled device presence to the guest, or if the guest
350 * has gone ahead and configured the device (via manually-executed
351 * device add via drmgr in guest, namely), we need to wait
352 * for the guest to quiesce the device before completing detach.
353 * Otherwise, we can assume the guest hasn't seen it and complete the
354 * detach immediately. Note that there is a small race window
355 * just before, or during, configuration, which is this context
356 * refers mainly to fetching the device tree via RTAS.
357 * During this window the device access will be arbitrated by
358 * associated DRC, which will simply fail the RTAS calls as invalid.
359 * This is recoverable within guest and current implementations of
360 * drmgr should be able to cope.
362 if (!drc->signalled && !drc->configured) {
363 /* if the guest hasn't seen the device we can't rely on it to
364 * set it back to an isolated state via RTAS, so do it here manually
366 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
369 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
370 trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
371 drc->awaiting_release = true;
375 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
376 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
377 trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
378 drc->awaiting_release = true;
382 if (drc->awaiting_allocation) {
383 drc->awaiting_release = true;
384 trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
388 drc->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
390 /* Calling release callbacks based on spapr_drc_type(drc). */
391 switch (spapr_drc_type(drc)) {
392 case SPAPR_DR_CONNECTOR_TYPE_CPU:
393 spapr_core_release(drc->dev);
395 case SPAPR_DR_CONNECTOR_TYPE_PCI:
396 spapr_phb_remove_pci_device_cb(drc->dev);
398 case SPAPR_DR_CONNECTOR_TYPE_LMB:
399 spapr_lmb_release(drc->dev);
401 case SPAPR_DR_CONNECTOR_TYPE_PHB:
402 case SPAPR_DR_CONNECTOR_TYPE_VIO:
407 drc->awaiting_release = false;
410 drc->fdt_start_offset = 0;
411 object_property_del(OBJECT(drc), "device", NULL);
415 static bool release_pending(sPAPRDRConnector *drc)
417 return drc->awaiting_release;
420 static void reset(DeviceState *d)
422 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
423 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
425 trace_spapr_drc_reset(spapr_drc_index(drc));
430 /* immediately upon reset we can safely assume DRCs whose devices
431 * are pending removal can be safely removed, and that they will
432 * subsequently be left in an ISOLATED state. move the DRC to this
433 * state in these cases (which will in turn complete any pending
436 if (drc->awaiting_release) {
437 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
438 /* generally this should also finalize the removal, but if the device
439 * hasn't yet been configured we normally defer removal under the
440 * assumption that this transition is taking place as part of device
441 * configuration. so check if we're still waiting after this, and
442 * force removal if we are
444 if (drc->awaiting_release) {
445 spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
448 /* non-PCI devices may be awaiting a transition to UNUSABLE */
449 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
450 drc->awaiting_release) {
451 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
455 if (drck->dr_entity_sense(drc) == SPAPR_DR_ENTITY_SENSE_PRESENT) {
456 drck->set_signalled(drc);
460 static bool spapr_drc_needed(void *opaque)
462 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
463 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
465 sPAPRDREntitySense value = drck->dr_entity_sense(drc);
467 /* If no dev is plugged in there is no need to migrate the DRC state */
468 if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
473 * If there is dev plugged in, we need to migrate the DRC state when
474 * it is different from cold-plugged state
476 switch (spapr_drc_type(drc)) {
477 case SPAPR_DR_CONNECTOR_TYPE_PCI:
478 case SPAPR_DR_CONNECTOR_TYPE_CPU:
479 case SPAPR_DR_CONNECTOR_TYPE_LMB:
480 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
481 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
482 drc->configured && drc->signalled && !drc->awaiting_release);
484 case SPAPR_DR_CONNECTOR_TYPE_PHB:
485 case SPAPR_DR_CONNECTOR_TYPE_VIO:
487 g_assert_not_reached();
492 static const VMStateDescription vmstate_spapr_drc = {
495 .minimum_version_id = 1,
496 .needed = spapr_drc_needed,
497 .fields = (VMStateField []) {
498 VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
499 VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
500 VMSTATE_UINT32(dr_indicator, sPAPRDRConnector),
501 VMSTATE_BOOL(configured, sPAPRDRConnector),
502 VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
503 VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
504 VMSTATE_BOOL(signalled, sPAPRDRConnector),
505 VMSTATE_END_OF_LIST()
509 static void realize(DeviceState *d, Error **errp)
511 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
512 Object *root_container;
517 trace_spapr_drc_realize(spapr_drc_index(drc));
518 /* NOTE: we do this as part of realize/unrealize due to the fact
519 * that the guest will communicate with the DRC via RTAS calls
520 * referencing the global DRC index. By unlinking the DRC
521 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
522 * inaccessible by the guest, since lookups rely on this path
523 * existing in the composition tree
525 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
526 snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
527 child_name = object_get_canonical_path_component(OBJECT(drc));
528 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
529 object_property_add_alias(root_container, link_name,
530 drc->owner, child_name, &err);
532 error_report_err(err);
533 object_unref(OBJECT(drc));
536 vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
538 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
541 static void unrealize(DeviceState *d, Error **errp)
543 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
544 Object *root_container;
548 trace_spapr_drc_unrealize(spapr_drc_index(drc));
549 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
550 snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
551 object_property_del(root_container, name, &err);
553 error_report_err(err);
554 object_unref(OBJECT(drc));
558 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
561 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
566 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
567 spapr_drc_index(drc));
568 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
569 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
572 /* PCI slot always start in a USABLE state, and stay there */
573 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
574 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
580 static void spapr_dr_connector_instance_init(Object *obj)
582 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
584 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
585 object_property_add(obj, "index", "uint32", prop_get_index,
586 NULL, NULL, NULL, NULL);
587 object_property_add(obj, "fdt", "struct", prop_get_fdt,
588 NULL, NULL, NULL, NULL);
591 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
593 DeviceClass *dk = DEVICE_CLASS(k);
594 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
597 dk->realize = realize;
598 dk->unrealize = unrealize;
599 drck->set_isolation_state = set_isolation_state;
600 drck->set_allocation_state = set_allocation_state;
601 drck->release_pending = release_pending;
602 drck->set_signalled = set_signalled;
604 * Reason: it crashes FIXME find and document the real reason
606 dk->user_creatable = false;
609 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
611 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
613 drck->dr_entity_sense = physical_entity_sense;
616 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
618 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
620 drck->dr_entity_sense = logical_entity_sense;
623 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
625 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
627 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
628 drck->typename = "CPU";
629 drck->drc_name_prefix = "CPU ";
632 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
634 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
636 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
637 drck->typename = "28";
638 drck->drc_name_prefix = "C";
641 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
643 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
645 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
646 drck->typename = "MEM";
647 drck->drc_name_prefix = "LMB ";
650 static const TypeInfo spapr_dr_connector_info = {
651 .name = TYPE_SPAPR_DR_CONNECTOR,
652 .parent = TYPE_DEVICE,
653 .instance_size = sizeof(sPAPRDRConnector),
654 .instance_init = spapr_dr_connector_instance_init,
655 .class_size = sizeof(sPAPRDRConnectorClass),
656 .class_init = spapr_dr_connector_class_init,
660 static const TypeInfo spapr_drc_physical_info = {
661 .name = TYPE_SPAPR_DRC_PHYSICAL,
662 .parent = TYPE_SPAPR_DR_CONNECTOR,
663 .instance_size = sizeof(sPAPRDRConnector),
664 .class_init = spapr_drc_physical_class_init,
668 static const TypeInfo spapr_drc_logical_info = {
669 .name = TYPE_SPAPR_DRC_LOGICAL,
670 .parent = TYPE_SPAPR_DR_CONNECTOR,
671 .instance_size = sizeof(sPAPRDRConnector),
672 .class_init = spapr_drc_logical_class_init,
676 static const TypeInfo spapr_drc_cpu_info = {
677 .name = TYPE_SPAPR_DRC_CPU,
678 .parent = TYPE_SPAPR_DRC_LOGICAL,
679 .instance_size = sizeof(sPAPRDRConnector),
680 .class_init = spapr_drc_cpu_class_init,
683 static const TypeInfo spapr_drc_pci_info = {
684 .name = TYPE_SPAPR_DRC_PCI,
685 .parent = TYPE_SPAPR_DRC_PHYSICAL,
686 .instance_size = sizeof(sPAPRDRConnector),
687 .class_init = spapr_drc_pci_class_init,
690 static const TypeInfo spapr_drc_lmb_info = {
691 .name = TYPE_SPAPR_DRC_LMB,
692 .parent = TYPE_SPAPR_DRC_LOGICAL,
693 .instance_size = sizeof(sPAPRDRConnector),
694 .class_init = spapr_drc_lmb_class_init,
697 /* helper functions for external users */
699 sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
704 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
705 obj = object_resolve_path(name, NULL);
707 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
710 sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
712 sPAPRDRConnectorClass *drck
713 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
715 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
716 | (id & DRC_INDEX_ID_MASK));
720 * spapr_drc_populate_dt
722 * @fdt: libfdt device tree
723 * @path: path in the DT to generate properties
724 * @owner: parent Object/DeviceState for which to generate DRC
726 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
727 * to the types of DRCs to generate entries for
729 * generate OF properties to describe DRC topology/indices to guests
731 * as documented in PAPR+ v2.1, 13.5.2
733 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
734 uint32_t drc_type_mask)
736 Object *root_container;
737 ObjectProperty *prop;
738 ObjectPropertyIterator iter;
739 uint32_t drc_count = 0;
740 GArray *drc_indexes, *drc_power_domains;
741 GString *drc_names, *drc_types;
744 /* the first entry of each properties is a 32-bit integer encoding
745 * the number of elements in the array. we won't know this until
746 * we complete the iteration through all the matching DRCs, but
747 * reserve the space now and set the offsets accordingly so we
748 * can fill them in later.
750 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
751 drc_indexes = g_array_set_size(drc_indexes, 1);
752 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
753 drc_power_domains = g_array_set_size(drc_power_domains, 1);
754 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
755 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
757 /* aliases for all DRConnector objects will be rooted in QOM
758 * composition tree at DRC_CONTAINER_PATH
760 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
762 object_property_iter_init(&iter, root_container);
763 while ((prop = object_property_iter_next(&iter))) {
765 sPAPRDRConnector *drc;
766 sPAPRDRConnectorClass *drck;
767 uint32_t drc_index, drc_power_domain;
769 if (!strstart(prop->type, "link<", NULL)) {
773 obj = object_property_get_link(root_container, prop->name, NULL);
774 drc = SPAPR_DR_CONNECTOR(obj);
775 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
777 if (owner && (drc->owner != owner)) {
781 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
787 /* ibm,drc-indexes */
788 drc_index = cpu_to_be32(spapr_drc_index(drc));
789 g_array_append_val(drc_indexes, drc_index);
791 /* ibm,drc-power-domains */
792 drc_power_domain = cpu_to_be32(-1);
793 g_array_append_val(drc_power_domains, drc_power_domain);
796 drc_names = g_string_append(drc_names, spapr_drc_name(drc));
797 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
800 drc_types = g_string_append(drc_types, drck->typename);
801 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
804 /* now write the drc count into the space we reserved at the
805 * beginning of the arrays previously
807 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
808 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
809 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
810 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
812 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
814 drc_indexes->len * sizeof(uint32_t));
816 error_report("Couldn't create ibm,drc-indexes property");
820 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
821 drc_power_domains->data,
822 drc_power_domains->len * sizeof(uint32_t));
824 error_report("Couldn't finalize ibm,drc-power-domains property");
828 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
829 drc_names->str, drc_names->len);
831 error_report("Couldn't finalize ibm,drc-names property");
835 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
836 drc_types->str, drc_types->len);
838 error_report("Couldn't finalize ibm,drc-types property");
843 g_array_free(drc_indexes, true);
844 g_array_free(drc_power_domains, true);
845 g_string_free(drc_names, true);
846 g_string_free(drc_types, true);
855 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
857 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
858 sPAPRDRConnectorClass *drck;
861 return RTAS_OUT_PARAM_ERROR;
864 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
865 return drck->set_isolation_state(drc, state);
868 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
870 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
871 sPAPRDRConnectorClass *drck;
874 return RTAS_OUT_PARAM_ERROR;
877 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
878 return drck->set_allocation_state(drc, state);
881 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
883 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
886 return RTAS_OUT_PARAM_ERROR;
889 trace_spapr_drc_set_dr_indicator(idx, state);
890 drc->dr_indicator = state;
891 return RTAS_OUT_SUCCESS;
894 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
896 uint32_t nargs, target_ulong args,
897 uint32_t nret, target_ulong rets)
899 uint32_t type, idx, state;
900 uint32_t ret = RTAS_OUT_SUCCESS;
902 if (nargs != 3 || nret != 1) {
903 ret = RTAS_OUT_PARAM_ERROR;
907 type = rtas_ld(args, 0);
908 idx = rtas_ld(args, 1);
909 state = rtas_ld(args, 2);
912 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
913 ret = rtas_set_isolation_state(idx, state);
915 case RTAS_SENSOR_TYPE_DR:
916 ret = rtas_set_dr_indicator(idx, state);
918 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
919 ret = rtas_set_allocation_state(idx, state);
922 ret = RTAS_OUT_NOT_SUPPORTED;
926 rtas_st(rets, 0, ret);
929 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
930 uint32_t token, uint32_t nargs,
931 target_ulong args, uint32_t nret,
934 uint32_t sensor_type;
935 uint32_t sensor_index;
936 uint32_t sensor_state = 0;
937 sPAPRDRConnector *drc;
938 sPAPRDRConnectorClass *drck;
939 uint32_t ret = RTAS_OUT_SUCCESS;
941 if (nargs != 2 || nret != 2) {
942 ret = RTAS_OUT_PARAM_ERROR;
946 sensor_type = rtas_ld(args, 0);
947 sensor_index = rtas_ld(args, 1);
949 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
950 /* currently only DR-related sensors are implemented */
951 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
953 ret = RTAS_OUT_NOT_SUPPORTED;
957 drc = spapr_drc_by_index(sensor_index);
959 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
960 ret = RTAS_OUT_PARAM_ERROR;
963 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
964 sensor_state = drck->dr_entity_sense(drc);
967 rtas_st(rets, 0, ret);
968 rtas_st(rets, 1, sensor_state);
971 /* configure-connector work area offsets, int32_t units for field
972 * indexes, bytes for field offset/len values.
974 * as documented by PAPR+ v2.7, 13.5.3.5
976 #define CC_IDX_NODE_NAME_OFFSET 2
977 #define CC_IDX_PROP_NAME_OFFSET 2
978 #define CC_IDX_PROP_LEN 3
979 #define CC_IDX_PROP_DATA_OFFSET 4
980 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
981 #define CC_WA_LEN 4096
983 static void configure_connector_st(target_ulong addr, target_ulong offset,
984 const void *buf, size_t len)
986 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
987 buf, MIN(len, CC_WA_LEN - offset));
990 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
991 sPAPRMachineState *spapr,
992 uint32_t token, uint32_t nargs,
993 target_ulong args, uint32_t nret,
999 sPAPRDRConnector *drc;
1000 sPAPRConfigureConnectorState *ccs;
1001 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1004 if (nargs != 2 || nret != 1) {
1005 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1009 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1011 drc_index = rtas_ld(wa_addr, 0);
1012 drc = spapr_drc_by_index(drc_index);
1014 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1015 rc = RTAS_OUT_PARAM_ERROR;
1020 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1021 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1027 ccs = g_new0(sPAPRConfigureConnectorState, 1);
1028 ccs->fdt_offset = drc->fdt_start_offset;
1035 const struct fdt_property *prop;
1036 int fdt_offset_next, prop_len;
1038 tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1041 case FDT_BEGIN_NODE:
1043 name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1045 /* provide the name of the next OF node */
1046 wa_offset = CC_VAL_DATA_OFFSET;
1047 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1048 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1049 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1053 if (ccs->fdt_depth == 0) {
1054 sPAPRDRIsolationState state = drc->isolation_state;
1055 uint32_t drc_index = spapr_drc_index(drc);
1056 /* done sending the device tree, don't need to track
1059 trace_spapr_drc_set_configured(drc_index);
1060 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
1061 drc->configured = true;
1063 /* guest should be not configuring an isolated device */
1064 trace_spapr_drc_set_configured_skipping(drc_index);
1069 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1071 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1075 prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1077 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1079 /* provide the name of the next OF property */
1080 wa_offset = CC_VAL_DATA_OFFSET;
1081 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1082 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1084 /* provide the length and value of the OF property. data gets
1085 * placed immediately after NULL terminator of the OF property's
1088 wa_offset += strlen(name) + 1,
1089 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1090 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1091 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1092 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1095 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1097 /* keep seeking for an actionable tag */
1101 ccs->fdt_offset = fdt_offset_next;
1103 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1107 rtas_st(rets, 0, rc);
1110 static void spapr_drc_register_types(void)
1112 type_register_static(&spapr_dr_connector_info);
1113 type_register_static(&spapr_drc_physical_info);
1114 type_register_static(&spapr_drc_logical_info);
1115 type_register_static(&spapr_drc_cpu_info);
1116 type_register_static(&spapr_drc_pci_info);
1117 type_register_static(&spapr_drc_lmb_info);
1119 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1120 rtas_set_indicator);
1121 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1122 rtas_get_sensor_state);
1123 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1124 rtas_ibm_configure_connector);
1126 type_init(spapr_drc_register_types)