spapr: Cleanups relating to DRC awaiting_release field
[qemu/kevin.git] / hw / ppc / spapr_drc.c
blob03ef75c70caaae50e7f71b86cceda623df562d85
1 /*
2 * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
4 * Copyright IBM Corp. 2014
6 * Authors:
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "cpu.h"
16 #include "qemu/cutils.h"
17 #include "hw/ppc/spapr_drc.h"
18 #include "qom/object.h"
19 #include "hw/qdev.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 */
24 #include "trace.h"
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 drc_isolate_physical(sPAPRDRConnector *drc)
51 /* if the guest is configuring a device attached to this DRC, we
52 * should reset the configuration state at this point since it may
53 * no longer be reliable (guest released device and needs to start
54 * over, or unplug occurred so the FDT is no longer valid)
56 g_free(drc->ccs);
57 drc->ccs = NULL;
59 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
61 /* if we're awaiting release, but still in an unconfigured state,
62 * it's likely the guest is still in the process of configuring
63 * the device and is transitioning the devices to an ISOLATED
64 * state as a part of that process. so we only complete the
65 * removal when this transition happens for a device in a
66 * configured state, as suggested by the state diagram from PAPR+
67 * 2.7, 13.4
69 if (drc->unplug_requested) {
70 uint32_t drc_index = spapr_drc_index(drc);
71 if (drc->configured) {
72 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
73 spapr_drc_detach(drc);
74 } else {
75 trace_spapr_drc_set_isolation_state_deferring(drc_index);
78 drc->configured = false;
80 return RTAS_OUT_SUCCESS;
83 static uint32_t drc_unisolate_physical(sPAPRDRConnector *drc)
85 /* cannot unisolate a non-existent resource, and, or resources
86 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
87 * 13.5.3.5)
89 if (!drc->dev) {
90 return RTAS_OUT_NO_SUCH_INDICATOR;
93 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
95 return RTAS_OUT_SUCCESS;
98 static uint32_t drc_isolate_logical(sPAPRDRConnector *drc)
100 /* if the guest is configuring a device attached to this DRC, we
101 * should reset the configuration state at this point since it may
102 * no longer be reliable (guest released device and needs to start
103 * over, or unplug occurred so the FDT is no longer valid)
105 g_free(drc->ccs);
106 drc->ccs = NULL;
109 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
110 * belong to a DIMM device that is marked for removal.
112 * Currently the guest userspace tool drmgr that drives the memory
113 * hotplug/unplug will just try to remove a set of 'removable' LMBs
114 * in response to a hot unplug request that is based on drc-count.
115 * If the LMB being removed doesn't belong to a DIMM device that is
116 * actually being unplugged, fail the isolation request here.
118 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
119 && !drc->unplug_requested) {
120 return RTAS_OUT_HW_ERROR;
123 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
125 /* if we're awaiting release, but still in an unconfigured state,
126 * it's likely the guest is still in the process of configuring
127 * the device and is transitioning the devices to an ISOLATED
128 * state as a part of that process. so we only complete the
129 * removal when this transition happens for a device in a
130 * configured state, as suggested by the state diagram from PAPR+
131 * 2.7, 13.4
133 if (drc->unplug_requested) {
134 uint32_t drc_index = spapr_drc_index(drc);
135 if (drc->configured) {
136 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
137 spapr_drc_detach(drc);
138 } else {
139 trace_spapr_drc_set_isolation_state_deferring(drc_index);
142 drc->configured = false;
144 return RTAS_OUT_SUCCESS;
147 static uint32_t drc_unisolate_logical(sPAPRDRConnector *drc)
149 /* cannot unisolate a non-existent resource, and, or resources
150 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
151 * 13.5.3.5)
153 if (!drc->dev ||
154 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
155 return RTAS_OUT_NO_SUCH_INDICATOR;
158 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
160 return RTAS_OUT_SUCCESS;
163 static uint32_t drc_set_usable(sPAPRDRConnector *drc)
165 /* if there's no resource/device associated with the DRC, there's
166 * no way for us to put it in an allocation state consistent with
167 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
168 * result in an RTAS return code of -3 / "no such indicator"
170 if (!drc->dev) {
171 return RTAS_OUT_NO_SUCH_INDICATOR;
173 if (drc->unplug_requested) {
174 /* Don't allow the guest to move a device away from UNUSABLE
175 * state when we want to unplug it */
176 return RTAS_OUT_NO_SUCH_INDICATOR;
179 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
181 return RTAS_OUT_SUCCESS;
184 static uint32_t drc_set_unusable(sPAPRDRConnector *drc)
186 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_UNUSABLE;
187 if (drc->unplug_requested) {
188 uint32_t drc_index = spapr_drc_index(drc);
189 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
190 spapr_drc_detach(drc);
193 return RTAS_OUT_SUCCESS;
196 static const char *spapr_drc_name(sPAPRDRConnector *drc)
198 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
200 /* human-readable name for a DRC to encode into the DT
201 * description. this is mainly only used within a guest in place
202 * of the unique DRC index.
204 * in the case of VIO/PCI devices, it corresponds to a "location
205 * code" that maps a logical device/function (DRC index) to a
206 * physical (or virtual in the case of VIO) location in the system
207 * by chaining together the "location label" for each
208 * encapsulating component.
210 * since this is more to do with diagnosing physical hardware
211 * issues than guest compatibility, we choose location codes/DRC
212 * names that adhere to the documented format, but avoid encoding
213 * the entire topology information into the label/code, instead
214 * just using the location codes based on the labels for the
215 * endpoints (VIO/PCI adaptor connectors), which is basically just
216 * "C" followed by an integer ID.
218 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
219 * location codes as documented by PAPR+ v2.7, 12.3.1.5
221 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
225 * dr-entity-sense sensor value
226 * returned via get-sensor-state RTAS calls
227 * as expected by state diagram in PAPR+ 2.7, 13.4
228 * based on the current allocation/indicator/power states
229 * for the DR connector.
231 static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc)
233 /* this assumes all PCI devices are assigned to a 'live insertion'
234 * power domain, where QEMU manages power state automatically as
235 * opposed to the guest. present, non-PCI resources are unaffected
236 * by power state.
238 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
239 : SPAPR_DR_ENTITY_SENSE_EMPTY;
242 static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc)
244 if (drc->dev
245 && (drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE)) {
246 return SPAPR_DR_ENTITY_SENSE_PRESENT;
247 } else {
248 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
252 static void prop_get_index(Object *obj, Visitor *v, const char *name,
253 void *opaque, Error **errp)
255 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
256 uint32_t value = spapr_drc_index(drc);
257 visit_type_uint32(v, name, &value, errp);
260 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
261 void *opaque, Error **errp)
263 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
264 Error *err = NULL;
265 int fdt_offset_next, fdt_offset, fdt_depth;
266 void *fdt;
268 if (!drc->fdt) {
269 visit_type_null(v, NULL, errp);
270 return;
273 fdt = drc->fdt;
274 fdt_offset = drc->fdt_start_offset;
275 fdt_depth = 0;
277 do {
278 const char *name = NULL;
279 const struct fdt_property *prop = NULL;
280 int prop_len = 0, name_len = 0;
281 uint32_t tag;
283 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
284 switch (tag) {
285 case FDT_BEGIN_NODE:
286 fdt_depth++;
287 name = fdt_get_name(fdt, fdt_offset, &name_len);
288 visit_start_struct(v, name, NULL, 0, &err);
289 if (err) {
290 error_propagate(errp, err);
291 return;
293 break;
294 case FDT_END_NODE:
295 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
296 g_assert(fdt_depth > 0);
297 visit_check_struct(v, &err);
298 visit_end_struct(v, NULL);
299 if (err) {
300 error_propagate(errp, err);
301 return;
303 fdt_depth--;
304 break;
305 case FDT_PROP: {
306 int i;
307 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
308 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
309 visit_start_list(v, name, NULL, 0, &err);
310 if (err) {
311 error_propagate(errp, err);
312 return;
314 for (i = 0; i < prop_len; i++) {
315 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
316 if (err) {
317 error_propagate(errp, err);
318 return;
321 visit_check_list(v, &err);
322 visit_end_list(v, NULL);
323 if (err) {
324 error_propagate(errp, err);
325 return;
327 break;
329 default:
330 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
332 fdt_offset = fdt_offset_next;
333 } while (fdt_depth != 0);
336 void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
337 int fdt_start_offset, Error **errp)
339 trace_spapr_drc_attach(spapr_drc_index(drc));
341 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
342 error_setg(errp, "an attached device is still awaiting release");
343 return;
345 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
346 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
348 g_assert(fdt);
350 drc->dev = d;
351 drc->fdt = fdt;
352 drc->fdt_start_offset = fdt_start_offset;
354 object_property_add_link(OBJECT(drc), "device",
355 object_get_typename(OBJECT(drc->dev)),
356 (Object **)(&drc->dev),
357 NULL, 0, NULL);
360 static void spapr_drc_release(sPAPRDRConnector *drc)
362 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
364 drck->release(drc->dev);
366 drc->unplug_requested = false;
367 g_free(drc->fdt);
368 drc->fdt = NULL;
369 drc->fdt_start_offset = 0;
370 object_property_del(OBJECT(drc), "device", &error_abort);
371 drc->dev = NULL;
374 void spapr_drc_detach(sPAPRDRConnector *drc)
376 trace_spapr_drc_detach(spapr_drc_index(drc));
378 drc->unplug_requested = true;
380 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
381 trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
382 return;
385 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
386 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
387 trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
388 return;
391 spapr_drc_release(drc);
394 void spapr_drc_reset(sPAPRDRConnector *drc)
396 trace_spapr_drc_reset(spapr_drc_index(drc));
398 g_free(drc->ccs);
399 drc->ccs = NULL;
401 /* immediately upon reset we can safely assume DRCs whose devices
402 * are pending removal can be safely removed.
404 if (drc->unplug_requested) {
405 spapr_drc_release(drc);
408 if (drc->dev) {
409 /* A device present at reset is coldplugged */
410 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
411 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
412 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
414 drc->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
415 } else {
416 /* Otherwise device is absent, but might be hotplugged */
417 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
418 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
419 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_UNUSABLE;
421 drc->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
425 static void drc_reset(void *opaque)
427 spapr_drc_reset(SPAPR_DR_CONNECTOR(opaque));
430 static bool spapr_drc_needed(void *opaque)
432 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
433 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
434 bool rc = false;
435 sPAPRDREntitySense value = drck->dr_entity_sense(drc);
437 /* If no dev is plugged in there is no need to migrate the DRC state */
438 if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
439 return false;
443 * If there is dev plugged in, we need to migrate the DRC state when
444 * it is different from cold-plugged state
446 switch (spapr_drc_type(drc)) {
447 case SPAPR_DR_CONNECTOR_TYPE_PCI:
448 case SPAPR_DR_CONNECTOR_TYPE_CPU:
449 case SPAPR_DR_CONNECTOR_TYPE_LMB:
450 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
451 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
452 drc->configured);
453 break;
454 case SPAPR_DR_CONNECTOR_TYPE_PHB:
455 case SPAPR_DR_CONNECTOR_TYPE_VIO:
456 default:
457 g_assert_not_reached();
459 return rc;
462 static const VMStateDescription vmstate_spapr_drc = {
463 .name = "spapr_drc",
464 .version_id = 1,
465 .minimum_version_id = 1,
466 .needed = spapr_drc_needed,
467 .fields = (VMStateField []) {
468 VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
469 VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
470 VMSTATE_UINT32(dr_indicator, sPAPRDRConnector),
471 VMSTATE_BOOL(configured, sPAPRDRConnector),
472 VMSTATE_END_OF_LIST()
476 static void realize(DeviceState *d, Error **errp)
478 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
479 Object *root_container;
480 char link_name[256];
481 gchar *child_name;
482 Error *err = NULL;
484 trace_spapr_drc_realize(spapr_drc_index(drc));
485 /* NOTE: we do this as part of realize/unrealize due to the fact
486 * that the guest will communicate with the DRC via RTAS calls
487 * referencing the global DRC index. By unlinking the DRC
488 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
489 * inaccessible by the guest, since lookups rely on this path
490 * existing in the composition tree
492 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
493 snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
494 child_name = object_get_canonical_path_component(OBJECT(drc));
495 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
496 object_property_add_alias(root_container, link_name,
497 drc->owner, child_name, &err);
498 if (err) {
499 error_report_err(err);
500 object_unref(OBJECT(drc));
502 g_free(child_name);
503 vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
504 drc);
505 qemu_register_reset(drc_reset, drc);
506 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
509 static void unrealize(DeviceState *d, Error **errp)
511 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
512 Object *root_container;
513 char name[256];
514 Error *err = NULL;
516 trace_spapr_drc_unrealize(spapr_drc_index(drc));
517 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
518 snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
519 object_property_del(root_container, name, &err);
520 if (err) {
521 error_report_err(err);
522 object_unref(OBJECT(drc));
526 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
527 uint32_t id)
529 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
530 char *prop_name;
532 drc->id = id;
533 drc->owner = owner;
534 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
535 spapr_drc_index(drc));
536 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
537 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
538 g_free(prop_name);
540 /* PCI slot always start in a USABLE state, and stay there */
541 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
542 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
545 return drc;
548 static void spapr_dr_connector_instance_init(Object *obj)
550 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
552 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
553 object_property_add(obj, "index", "uint32", prop_get_index,
554 NULL, NULL, NULL, NULL);
555 object_property_add(obj, "fdt", "struct", prop_get_fdt,
556 NULL, NULL, NULL, NULL);
559 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
561 DeviceClass *dk = DEVICE_CLASS(k);
563 dk->realize = realize;
564 dk->unrealize = unrealize;
566 * Reason: it crashes FIXME find and document the real reason
568 dk->user_creatable = false;
571 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
573 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
575 drck->dr_entity_sense = physical_entity_sense;
576 drck->isolate = drc_isolate_physical;
577 drck->unisolate = drc_unisolate_physical;
580 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
582 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
584 drck->dr_entity_sense = logical_entity_sense;
585 drck->isolate = drc_isolate_logical;
586 drck->unisolate = drc_unisolate_logical;
589 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
591 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
593 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
594 drck->typename = "CPU";
595 drck->drc_name_prefix = "CPU ";
596 drck->release = spapr_core_release;
599 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
601 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
603 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
604 drck->typename = "28";
605 drck->drc_name_prefix = "C";
606 drck->release = spapr_phb_remove_pci_device_cb;
609 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
611 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
613 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
614 drck->typename = "MEM";
615 drck->drc_name_prefix = "LMB ";
616 drck->release = spapr_lmb_release;
619 static const TypeInfo spapr_dr_connector_info = {
620 .name = TYPE_SPAPR_DR_CONNECTOR,
621 .parent = TYPE_DEVICE,
622 .instance_size = sizeof(sPAPRDRConnector),
623 .instance_init = spapr_dr_connector_instance_init,
624 .class_size = sizeof(sPAPRDRConnectorClass),
625 .class_init = spapr_dr_connector_class_init,
626 .abstract = true,
629 static const TypeInfo spapr_drc_physical_info = {
630 .name = TYPE_SPAPR_DRC_PHYSICAL,
631 .parent = TYPE_SPAPR_DR_CONNECTOR,
632 .class_init = spapr_drc_physical_class_init,
633 .abstract = true,
636 static const TypeInfo spapr_drc_logical_info = {
637 .name = TYPE_SPAPR_DRC_LOGICAL,
638 .parent = TYPE_SPAPR_DR_CONNECTOR,
639 .class_init = spapr_drc_logical_class_init,
640 .abstract = true,
643 static const TypeInfo spapr_drc_cpu_info = {
644 .name = TYPE_SPAPR_DRC_CPU,
645 .parent = TYPE_SPAPR_DRC_LOGICAL,
646 .class_init = spapr_drc_cpu_class_init,
649 static const TypeInfo spapr_drc_pci_info = {
650 .name = TYPE_SPAPR_DRC_PCI,
651 .parent = TYPE_SPAPR_DRC_PHYSICAL,
652 .class_init = spapr_drc_pci_class_init,
655 static const TypeInfo spapr_drc_lmb_info = {
656 .name = TYPE_SPAPR_DRC_LMB,
657 .parent = TYPE_SPAPR_DRC_LOGICAL,
658 .class_init = spapr_drc_lmb_class_init,
661 /* helper functions for external users */
663 sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
665 Object *obj;
666 char name[256];
668 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
669 obj = object_resolve_path(name, NULL);
671 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
674 sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
676 sPAPRDRConnectorClass *drck
677 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
679 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
680 | (id & DRC_INDEX_ID_MASK));
684 * spapr_drc_populate_dt
686 * @fdt: libfdt device tree
687 * @path: path in the DT to generate properties
688 * @owner: parent Object/DeviceState for which to generate DRC
689 * descriptions for
690 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
691 * to the types of DRCs to generate entries for
693 * generate OF properties to describe DRC topology/indices to guests
695 * as documented in PAPR+ v2.1, 13.5.2
697 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
698 uint32_t drc_type_mask)
700 Object *root_container;
701 ObjectProperty *prop;
702 ObjectPropertyIterator iter;
703 uint32_t drc_count = 0;
704 GArray *drc_indexes, *drc_power_domains;
705 GString *drc_names, *drc_types;
706 int ret;
708 /* the first entry of each properties is a 32-bit integer encoding
709 * the number of elements in the array. we won't know this until
710 * we complete the iteration through all the matching DRCs, but
711 * reserve the space now and set the offsets accordingly so we
712 * can fill them in later.
714 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
715 drc_indexes = g_array_set_size(drc_indexes, 1);
716 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
717 drc_power_domains = g_array_set_size(drc_power_domains, 1);
718 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
719 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
721 /* aliases for all DRConnector objects will be rooted in QOM
722 * composition tree at DRC_CONTAINER_PATH
724 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
726 object_property_iter_init(&iter, root_container);
727 while ((prop = object_property_iter_next(&iter))) {
728 Object *obj;
729 sPAPRDRConnector *drc;
730 sPAPRDRConnectorClass *drck;
731 uint32_t drc_index, drc_power_domain;
733 if (!strstart(prop->type, "link<", NULL)) {
734 continue;
737 obj = object_property_get_link(root_container, prop->name, NULL);
738 drc = SPAPR_DR_CONNECTOR(obj);
739 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
741 if (owner && (drc->owner != owner)) {
742 continue;
745 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
746 continue;
749 drc_count++;
751 /* ibm,drc-indexes */
752 drc_index = cpu_to_be32(spapr_drc_index(drc));
753 g_array_append_val(drc_indexes, drc_index);
755 /* ibm,drc-power-domains */
756 drc_power_domain = cpu_to_be32(-1);
757 g_array_append_val(drc_power_domains, drc_power_domain);
759 /* ibm,drc-names */
760 drc_names = g_string_append(drc_names, spapr_drc_name(drc));
761 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
763 /* ibm,drc-types */
764 drc_types = g_string_append(drc_types, drck->typename);
765 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
768 /* now write the drc count into the space we reserved at the
769 * beginning of the arrays previously
771 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
772 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
773 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
774 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
776 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
777 drc_indexes->data,
778 drc_indexes->len * sizeof(uint32_t));
779 if (ret) {
780 error_report("Couldn't create ibm,drc-indexes property");
781 goto out;
784 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
785 drc_power_domains->data,
786 drc_power_domains->len * sizeof(uint32_t));
787 if (ret) {
788 error_report("Couldn't finalize ibm,drc-power-domains property");
789 goto out;
792 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
793 drc_names->str, drc_names->len);
794 if (ret) {
795 error_report("Couldn't finalize ibm,drc-names property");
796 goto out;
799 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
800 drc_types->str, drc_types->len);
801 if (ret) {
802 error_report("Couldn't finalize ibm,drc-types property");
803 goto out;
806 out:
807 g_array_free(drc_indexes, true);
808 g_array_free(drc_power_domains, true);
809 g_string_free(drc_names, true);
810 g_string_free(drc_types, true);
812 return ret;
816 * RTAS calls
819 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
821 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
822 sPAPRDRConnectorClass *drck;
824 if (!drc) {
825 return RTAS_OUT_NO_SUCH_INDICATOR;
828 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
830 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
832 switch (state) {
833 case SPAPR_DR_ISOLATION_STATE_ISOLATED:
834 return drck->isolate(drc);
836 case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
837 return drck->unisolate(drc);
839 default:
840 return RTAS_OUT_PARAM_ERROR;
844 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
846 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
848 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
849 return RTAS_OUT_NO_SUCH_INDICATOR;
852 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
854 switch (state) {
855 case SPAPR_DR_ALLOCATION_STATE_USABLE:
856 return drc_set_usable(drc);
858 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
859 return drc_set_unusable(drc);
861 default:
862 return RTAS_OUT_PARAM_ERROR;
866 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
868 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
870 if (!drc) {
871 return RTAS_OUT_PARAM_ERROR;
874 trace_spapr_drc_set_dr_indicator(idx, state);
875 drc->dr_indicator = state;
876 return RTAS_OUT_SUCCESS;
879 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
880 uint32_t token,
881 uint32_t nargs, target_ulong args,
882 uint32_t nret, target_ulong rets)
884 uint32_t type, idx, state;
885 uint32_t ret = RTAS_OUT_SUCCESS;
887 if (nargs != 3 || nret != 1) {
888 ret = RTAS_OUT_PARAM_ERROR;
889 goto out;
892 type = rtas_ld(args, 0);
893 idx = rtas_ld(args, 1);
894 state = rtas_ld(args, 2);
896 switch (type) {
897 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
898 ret = rtas_set_isolation_state(idx, state);
899 break;
900 case RTAS_SENSOR_TYPE_DR:
901 ret = rtas_set_dr_indicator(idx, state);
902 break;
903 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
904 ret = rtas_set_allocation_state(idx, state);
905 break;
906 default:
907 ret = RTAS_OUT_NOT_SUPPORTED;
910 out:
911 rtas_st(rets, 0, ret);
914 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
915 uint32_t token, uint32_t nargs,
916 target_ulong args, uint32_t nret,
917 target_ulong rets)
919 uint32_t sensor_type;
920 uint32_t sensor_index;
921 uint32_t sensor_state = 0;
922 sPAPRDRConnector *drc;
923 sPAPRDRConnectorClass *drck;
924 uint32_t ret = RTAS_OUT_SUCCESS;
926 if (nargs != 2 || nret != 2) {
927 ret = RTAS_OUT_PARAM_ERROR;
928 goto out;
931 sensor_type = rtas_ld(args, 0);
932 sensor_index = rtas_ld(args, 1);
934 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
935 /* currently only DR-related sensors are implemented */
936 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
937 sensor_type);
938 ret = RTAS_OUT_NOT_SUPPORTED;
939 goto out;
942 drc = spapr_drc_by_index(sensor_index);
943 if (!drc) {
944 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
945 ret = RTAS_OUT_PARAM_ERROR;
946 goto out;
948 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
949 sensor_state = drck->dr_entity_sense(drc);
951 out:
952 rtas_st(rets, 0, ret);
953 rtas_st(rets, 1, sensor_state);
956 /* configure-connector work area offsets, int32_t units for field
957 * indexes, bytes for field offset/len values.
959 * as documented by PAPR+ v2.7, 13.5.3.5
961 #define CC_IDX_NODE_NAME_OFFSET 2
962 #define CC_IDX_PROP_NAME_OFFSET 2
963 #define CC_IDX_PROP_LEN 3
964 #define CC_IDX_PROP_DATA_OFFSET 4
965 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
966 #define CC_WA_LEN 4096
968 static void configure_connector_st(target_ulong addr, target_ulong offset,
969 const void *buf, size_t len)
971 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
972 buf, MIN(len, CC_WA_LEN - offset));
975 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
976 sPAPRMachineState *spapr,
977 uint32_t token, uint32_t nargs,
978 target_ulong args, uint32_t nret,
979 target_ulong rets)
981 uint64_t wa_addr;
982 uint64_t wa_offset;
983 uint32_t drc_index;
984 sPAPRDRConnector *drc;
985 sPAPRConfigureConnectorState *ccs;
986 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
987 int rc;
989 if (nargs != 2 || nret != 1) {
990 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
991 return;
994 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
996 drc_index = rtas_ld(wa_addr, 0);
997 drc = spapr_drc_by_index(drc_index);
998 if (!drc) {
999 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1000 rc = RTAS_OUT_PARAM_ERROR;
1001 goto out;
1004 if (!drc->fdt) {
1005 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1006 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1007 goto out;
1010 ccs = drc->ccs;
1011 if (!ccs) {
1012 ccs = g_new0(sPAPRConfigureConnectorState, 1);
1013 ccs->fdt_offset = drc->fdt_start_offset;
1014 drc->ccs = ccs;
1017 do {
1018 uint32_t tag;
1019 const char *name;
1020 const struct fdt_property *prop;
1021 int fdt_offset_next, prop_len;
1023 tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1025 switch (tag) {
1026 case FDT_BEGIN_NODE:
1027 ccs->fdt_depth++;
1028 name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1030 /* provide the name of the next OF node */
1031 wa_offset = CC_VAL_DATA_OFFSET;
1032 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1033 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1034 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1035 break;
1036 case FDT_END_NODE:
1037 ccs->fdt_depth--;
1038 if (ccs->fdt_depth == 0) {
1039 sPAPRDRIsolationState state = drc->isolation_state;
1040 uint32_t drc_index = spapr_drc_index(drc);
1041 /* done sending the device tree, don't need to track
1042 * the state anymore
1044 trace_spapr_drc_set_configured(drc_index);
1045 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
1046 drc->configured = true;
1047 } else {
1048 /* guest should be not configuring an isolated device */
1049 trace_spapr_drc_set_configured_skipping(drc_index);
1051 g_free(ccs);
1052 drc->ccs = NULL;
1053 ccs = NULL;
1054 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1055 } else {
1056 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1058 break;
1059 case FDT_PROP:
1060 prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1061 &prop_len);
1062 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1064 /* provide the name of the next OF property */
1065 wa_offset = CC_VAL_DATA_OFFSET;
1066 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1067 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1069 /* provide the length and value of the OF property. data gets
1070 * placed immediately after NULL terminator of the OF property's
1071 * name string
1073 wa_offset += strlen(name) + 1,
1074 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1075 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1076 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1077 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1078 break;
1079 case FDT_END:
1080 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1081 default:
1082 /* keep seeking for an actionable tag */
1083 break;
1085 if (ccs) {
1086 ccs->fdt_offset = fdt_offset_next;
1088 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1090 rc = resp;
1091 out:
1092 rtas_st(rets, 0, rc);
1095 static void spapr_drc_register_types(void)
1097 type_register_static(&spapr_dr_connector_info);
1098 type_register_static(&spapr_drc_physical_info);
1099 type_register_static(&spapr_drc_logical_info);
1100 type_register_static(&spapr_drc_cpu_info);
1101 type_register_static(&spapr_drc_pci_info);
1102 type_register_static(&spapr_drc_lmb_info);
1104 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1105 rtas_set_indicator);
1106 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1107 rtas_get_sensor_state);
1108 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1109 rtas_ibm_configure_connector);
1111 type_init(spapr_drc_register_types)