docs/fuzz: add information about useful libFuzzer flags
[qemu/ar7.git] / hw / ppc / spapr_drc.c
blob43d12bc33ac2a6debaae572dc7e8886eee068f4d
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 "qapi/qmp/qnull.h"
16 #include "cpu.h"
17 #include "qemu/cutils.h"
18 #include "hw/ppc/spapr_drc.h"
19 #include "qom/object.h"
20 #include "migration/vmstate.h"
21 #include "qapi/visitor.h"
22 #include "qemu/error-report.h"
23 #include "hw/ppc/spapr.h" /* for RTAS return codes */
24 #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
25 #include "hw/ppc/spapr_nvdimm.h"
26 #include "sysemu/device_tree.h"
27 #include "sysemu/reset.h"
28 #include "trace.h"
30 #define DRC_CONTAINER_PATH "/dr-connector"
31 #define DRC_INDEX_TYPE_SHIFT 28
32 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
34 SpaprDrcType spapr_drc_type(SpaprDrc *drc)
36 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
38 return 1 << drck->typeshift;
41 uint32_t spapr_drc_index(SpaprDrc *drc)
43 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
45 /* no set format for a drc index: it only needs to be globally
46 * unique. this is how we encode the DRC type on bare-metal
47 * however, so might as well do that here
49 return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
50 | (drc->id & DRC_INDEX_ID_MASK);
53 static uint32_t drc_isolate_physical(SpaprDrc *drc)
55 switch (drc->state) {
56 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
57 return RTAS_OUT_SUCCESS; /* Nothing to do */
58 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
59 break; /* see below */
60 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
61 return RTAS_OUT_PARAM_ERROR; /* not allowed */
62 default:
63 g_assert_not_reached();
66 drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
68 if (drc->unplug_requested) {
69 uint32_t drc_index = spapr_drc_index(drc);
70 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
71 spapr_drc_detach(drc);
74 return RTAS_OUT_SUCCESS;
77 static uint32_t drc_unisolate_physical(SpaprDrc *drc)
79 switch (drc->state) {
80 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
81 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
82 return RTAS_OUT_SUCCESS; /* Nothing to do */
83 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
84 break; /* see below */
85 default:
86 g_assert_not_reached();
89 /* cannot unisolate a non-existent resource, and, or resources
90 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
91 * 13.5.3.5)
93 if (!drc->dev) {
94 return RTAS_OUT_NO_SUCH_INDICATOR;
97 drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE;
98 drc->ccs_offset = drc->fdt_start_offset;
99 drc->ccs_depth = 0;
101 return RTAS_OUT_SUCCESS;
104 static uint32_t drc_isolate_logical(SpaprDrc *drc)
106 switch (drc->state) {
107 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
108 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
109 return RTAS_OUT_SUCCESS; /* Nothing to do */
110 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
111 break; /* see below */
112 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
113 return RTAS_OUT_PARAM_ERROR; /* not allowed */
114 default:
115 g_assert_not_reached();
119 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
120 * belong to a DIMM device that is marked for removal.
122 * Currently the guest userspace tool drmgr that drives the memory
123 * hotplug/unplug will just try to remove a set of 'removable' LMBs
124 * in response to a hot unplug request that is based on drc-count.
125 * If the LMB being removed doesn't belong to a DIMM device that is
126 * actually being unplugged, fail the isolation request here.
128 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
129 && !drc->unplug_requested) {
130 return RTAS_OUT_HW_ERROR;
133 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
135 /* if we're awaiting release, but still in an unconfigured state,
136 * it's likely the guest is still in the process of configuring
137 * the device and is transitioning the devices to an ISOLATED
138 * state as a part of that process. so we only complete the
139 * removal when this transition happens for a device in a
140 * configured state, as suggested by the state diagram from PAPR+
141 * 2.7, 13.4
143 if (drc->unplug_requested) {
144 uint32_t drc_index = spapr_drc_index(drc);
145 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
146 spapr_drc_detach(drc);
148 return RTAS_OUT_SUCCESS;
151 static uint32_t drc_unisolate_logical(SpaprDrc *drc)
153 switch (drc->state) {
154 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
155 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
156 return RTAS_OUT_SUCCESS; /* Nothing to do */
157 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
158 break; /* see below */
159 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
160 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
161 default:
162 g_assert_not_reached();
165 /* Move to AVAILABLE state should have ensured device was present */
166 g_assert(drc->dev);
168 drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
169 drc->ccs_offset = drc->fdt_start_offset;
170 drc->ccs_depth = 0;
172 return RTAS_OUT_SUCCESS;
175 static uint32_t drc_set_usable(SpaprDrc *drc)
177 switch (drc->state) {
178 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
179 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
180 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
181 return RTAS_OUT_SUCCESS; /* Nothing to do */
182 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
183 break; /* see below */
184 default:
185 g_assert_not_reached();
188 /* if there's no resource/device associated with the DRC, there's
189 * no way for us to put it in an allocation state consistent with
190 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
191 * result in an RTAS return code of -3 / "no such indicator"
193 if (!drc->dev) {
194 return RTAS_OUT_NO_SUCH_INDICATOR;
196 if (drc->unplug_requested) {
197 /* Don't allow the guest to move a device away from UNUSABLE
198 * state when we want to unplug it */
199 return RTAS_OUT_NO_SUCH_INDICATOR;
202 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
204 return RTAS_OUT_SUCCESS;
207 static uint32_t drc_set_unusable(SpaprDrc *drc)
209 switch (drc->state) {
210 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
211 return RTAS_OUT_SUCCESS; /* Nothing to do */
212 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
213 break; /* see below */
214 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
215 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
216 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
217 default:
218 g_assert_not_reached();
221 drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
222 if (drc->unplug_requested) {
223 uint32_t drc_index = spapr_drc_index(drc);
224 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
225 spapr_drc_detach(drc);
228 return RTAS_OUT_SUCCESS;
231 static char *spapr_drc_name(SpaprDrc *drc)
233 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
235 /* human-readable name for a DRC to encode into the DT
236 * description. this is mainly only used within a guest in place
237 * of the unique DRC index.
239 * in the case of VIO/PCI devices, it corresponds to a "location
240 * code" that maps a logical device/function (DRC index) to a
241 * physical (or virtual in the case of VIO) location in the system
242 * by chaining together the "location label" for each
243 * encapsulating component.
245 * since this is more to do with diagnosing physical hardware
246 * issues than guest compatibility, we choose location codes/DRC
247 * names that adhere to the documented format, but avoid encoding
248 * the entire topology information into the label/code, instead
249 * just using the location codes based on the labels for the
250 * endpoints (VIO/PCI adaptor connectors), which is basically just
251 * "C" followed by an integer ID.
253 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
254 * location codes as documented by PAPR+ v2.7, 12.3.1.5
256 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
260 * dr-entity-sense sensor value
261 * returned via get-sensor-state RTAS calls
262 * as expected by state diagram in PAPR+ 2.7, 13.4
263 * based on the current allocation/indicator/power states
264 * for the DR connector.
266 static SpaprDREntitySense physical_entity_sense(SpaprDrc *drc)
268 /* this assumes all PCI devices are assigned to a 'live insertion'
269 * power domain, where QEMU manages power state automatically as
270 * opposed to the guest. present, non-PCI resources are unaffected
271 * by power state.
273 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
274 : SPAPR_DR_ENTITY_SENSE_EMPTY;
277 static SpaprDREntitySense logical_entity_sense(SpaprDrc *drc)
279 switch (drc->state) {
280 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
281 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
282 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
283 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
284 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
285 g_assert(drc->dev);
286 return SPAPR_DR_ENTITY_SENSE_PRESENT;
287 default:
288 g_assert_not_reached();
292 static void prop_get_index(Object *obj, Visitor *v, const char *name,
293 void *opaque, Error **errp)
295 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
296 uint32_t value = spapr_drc_index(drc);
297 visit_type_uint32(v, name, &value, errp);
300 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
301 void *opaque, Error **errp)
303 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
304 QNull *null = NULL;
305 Error *err = NULL;
306 int fdt_offset_next, fdt_offset, fdt_depth;
307 void *fdt;
309 if (!drc->fdt) {
310 visit_type_null(v, NULL, &null, errp);
311 qobject_unref(null);
312 return;
315 fdt = drc->fdt;
316 fdt_offset = drc->fdt_start_offset;
317 fdt_depth = 0;
319 do {
320 const char *name = NULL;
321 const struct fdt_property *prop = NULL;
322 int prop_len = 0, name_len = 0;
323 uint32_t tag;
325 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
326 switch (tag) {
327 case FDT_BEGIN_NODE:
328 fdt_depth++;
329 name = fdt_get_name(fdt, fdt_offset, &name_len);
330 if (!visit_start_struct(v, name, NULL, 0, errp)) {
331 return;
333 break;
334 case FDT_END_NODE:
335 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
336 g_assert(fdt_depth > 0);
337 visit_check_struct(v, &err);
338 visit_end_struct(v, NULL);
339 if (err) {
340 error_propagate(errp, err);
341 return;
343 fdt_depth--;
344 break;
345 case FDT_PROP: {
346 int i;
347 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
348 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
349 if (!visit_start_list(v, name, NULL, 0, errp)) {
350 return;
352 for (i = 0; i < prop_len; i++) {
353 if (!visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i],
354 errp)) {
355 return;
358 visit_check_list(v, &err);
359 visit_end_list(v, NULL);
360 if (err) {
361 error_propagate(errp, err);
362 return;
364 break;
366 default:
367 error_report("device FDT in unexpected state: %d", tag);
368 abort();
370 fdt_offset = fdt_offset_next;
371 } while (fdt_depth != 0);
374 void spapr_drc_attach(SpaprDrc *drc, DeviceState *d, Error **errp)
376 trace_spapr_drc_attach(spapr_drc_index(drc));
378 if (drc->dev) {
379 error_setg(errp, "an attached device is still awaiting release");
380 return;
382 g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
383 || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
385 drc->dev = d;
387 object_property_add_link(OBJECT(drc), "device",
388 object_get_typename(OBJECT(drc->dev)),
389 (Object **)(&drc->dev),
390 NULL, 0);
393 static void spapr_drc_release(SpaprDrc *drc)
395 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
397 drck->release(drc->dev);
399 drc->unplug_requested = false;
400 g_free(drc->fdt);
401 drc->fdt = NULL;
402 drc->fdt_start_offset = 0;
403 object_property_del(OBJECT(drc), "device");
404 drc->dev = NULL;
407 void spapr_drc_detach(SpaprDrc *drc)
409 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
411 trace_spapr_drc_detach(spapr_drc_index(drc));
413 g_assert(drc->dev);
415 drc->unplug_requested = true;
417 if (drc->state != drck->empty_state) {
418 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
419 return;
422 spapr_drc_release(drc);
425 void spapr_drc_reset(SpaprDrc *drc)
427 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
429 trace_spapr_drc_reset(spapr_drc_index(drc));
431 /* immediately upon reset we can safely assume DRCs whose devices
432 * are pending removal can be safely removed.
434 if (drc->unplug_requested) {
435 spapr_drc_release(drc);
438 if (drc->dev) {
439 /* A device present at reset is ready to go, same as coldplugged */
440 drc->state = drck->ready_state;
442 * Ensure that we are able to send the FDT fragment again
443 * via configure-connector call if the guest requests.
445 drc->ccs_offset = drc->fdt_start_offset;
446 drc->ccs_depth = 0;
447 } else {
448 drc->state = drck->empty_state;
449 drc->ccs_offset = -1;
450 drc->ccs_depth = -1;
454 static bool spapr_drc_unplug_requested_needed(void *opaque)
456 return spapr_drc_unplug_requested(opaque);
459 static const VMStateDescription vmstate_spapr_drc_unplug_requested = {
460 .name = "spapr_drc/unplug_requested",
461 .version_id = 1,
462 .minimum_version_id = 1,
463 .needed = spapr_drc_unplug_requested_needed,
464 .fields = (VMStateField []) {
465 VMSTATE_BOOL(unplug_requested, SpaprDrc),
466 VMSTATE_END_OF_LIST()
470 bool spapr_drc_transient(SpaprDrc *drc)
472 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
475 * If no dev is plugged in there is no need to migrate the DRC state
476 * nor to reset the DRC at CAS.
478 if (!drc->dev) {
479 return false;
483 * We need to reset the DRC at CAS or to migrate the DRC state if it's
484 * not equal to the expected long-term state, which is the same as the
485 * coldplugged initial state, or if an unplug request is pending.
487 return drc->state != drck->ready_state ||
488 spapr_drc_unplug_requested(drc);
491 static bool spapr_drc_needed(void *opaque)
493 return spapr_drc_transient(opaque);
496 static const VMStateDescription vmstate_spapr_drc = {
497 .name = "spapr_drc",
498 .version_id = 1,
499 .minimum_version_id = 1,
500 .needed = spapr_drc_needed,
501 .fields = (VMStateField []) {
502 VMSTATE_UINT32(state, SpaprDrc),
503 VMSTATE_END_OF_LIST()
505 .subsections = (const VMStateDescription * []) {
506 &vmstate_spapr_drc_unplug_requested,
507 NULL
511 static void realize(DeviceState *d, Error **errp)
513 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
514 Object *root_container;
515 gchar *link_name;
516 char *child_name;
518 trace_spapr_drc_realize(spapr_drc_index(drc));
519 /* NOTE: we do this as part of realize/unrealize due to the fact
520 * that the guest will communicate with the DRC via RTAS calls
521 * referencing the global DRC index. By unlinking the DRC
522 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
523 * inaccessible by the guest, since lookups rely on this path
524 * existing in the composition tree
526 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
527 link_name = g_strdup_printf("%x", spapr_drc_index(drc));
528 child_name = object_get_canonical_path_component(OBJECT(drc));
529 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
530 object_property_add_alias(root_container, link_name,
531 drc->owner, child_name);
532 g_free(child_name);
533 g_free(link_name);
534 vmstate_register(VMSTATE_IF(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
535 drc);
536 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
539 static void unrealize(DeviceState *d)
541 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
542 Object *root_container;
543 gchar *name;
545 trace_spapr_drc_unrealize(spapr_drc_index(drc));
546 vmstate_unregister(VMSTATE_IF(drc), &vmstate_spapr_drc, drc);
547 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
548 name = g_strdup_printf("%x", spapr_drc_index(drc));
549 object_property_del(root_container, name);
550 g_free(name);
553 SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
554 uint32_t id)
556 SpaprDrc *drc = SPAPR_DR_CONNECTOR(object_new(type));
557 char *prop_name;
559 drc->id = id;
560 drc->owner = owner;
561 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
562 spapr_drc_index(drc));
563 object_property_add_child(owner, prop_name, OBJECT(drc));
564 object_unref(OBJECT(drc));
565 qdev_realize(DEVICE(drc), NULL, NULL);
566 g_free(prop_name);
568 return drc;
571 static void spapr_dr_connector_instance_init(Object *obj)
573 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
574 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
576 object_property_add_uint32_ptr(obj, "id", &drc->id, OBJ_PROP_FLAG_READ);
577 object_property_add(obj, "index", "uint32", prop_get_index,
578 NULL, NULL, NULL);
579 object_property_add(obj, "fdt", "struct", prop_get_fdt,
580 NULL, NULL, NULL);
581 drc->state = drck->empty_state;
584 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
586 DeviceClass *dk = DEVICE_CLASS(k);
588 dk->realize = realize;
589 dk->unrealize = unrealize;
591 * Reason: it crashes FIXME find and document the real reason
593 dk->user_creatable = false;
596 static bool drc_physical_needed(void *opaque)
598 SpaprDrcPhysical *drcp = (SpaprDrcPhysical *)opaque;
599 SpaprDrc *drc = SPAPR_DR_CONNECTOR(drcp);
601 if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
602 || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
603 return false;
605 return true;
608 static const VMStateDescription vmstate_spapr_drc_physical = {
609 .name = "spapr_drc/physical",
610 .version_id = 1,
611 .minimum_version_id = 1,
612 .needed = drc_physical_needed,
613 .fields = (VMStateField []) {
614 VMSTATE_UINT32(dr_indicator, SpaprDrcPhysical),
615 VMSTATE_END_OF_LIST()
619 static void drc_physical_reset(void *opaque)
621 SpaprDrc *drc = SPAPR_DR_CONNECTOR(opaque);
622 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
624 if (drc->dev) {
625 drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
626 } else {
627 drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
631 static void realize_physical(DeviceState *d, Error **errp)
633 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
634 Error *local_err = NULL;
636 realize(d, &local_err);
637 if (local_err) {
638 error_propagate(errp, local_err);
639 return;
642 vmstate_register(VMSTATE_IF(drcp),
643 spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
644 &vmstate_spapr_drc_physical, drcp);
645 qemu_register_reset(drc_physical_reset, drcp);
648 static void unrealize_physical(DeviceState *d)
650 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
652 unrealize(d);
653 vmstate_unregister(VMSTATE_IF(drcp), &vmstate_spapr_drc_physical, drcp);
654 qemu_unregister_reset(drc_physical_reset, drcp);
657 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
659 DeviceClass *dk = DEVICE_CLASS(k);
660 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
662 dk->realize = realize_physical;
663 dk->unrealize = unrealize_physical;
664 drck->dr_entity_sense = physical_entity_sense;
665 drck->isolate = drc_isolate_physical;
666 drck->unisolate = drc_unisolate_physical;
667 drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
668 drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
671 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
673 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
675 drck->dr_entity_sense = logical_entity_sense;
676 drck->isolate = drc_isolate_logical;
677 drck->unisolate = drc_unisolate_logical;
678 drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
679 drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
682 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
684 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
686 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
687 drck->typename = "CPU";
688 drck->drc_name_prefix = "CPU ";
689 drck->release = spapr_core_release;
690 drck->dt_populate = spapr_core_dt_populate;
693 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
695 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
697 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
698 drck->typename = "28";
699 drck->drc_name_prefix = "C";
700 drck->release = spapr_phb_remove_pci_device_cb;
701 drck->dt_populate = spapr_pci_dt_populate;
704 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
706 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
708 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
709 drck->typename = "MEM";
710 drck->drc_name_prefix = "LMB ";
711 drck->release = spapr_lmb_release;
712 drck->dt_populate = spapr_lmb_dt_populate;
715 static void spapr_drc_phb_class_init(ObjectClass *k, void *data)
717 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
719 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB;
720 drck->typename = "PHB";
721 drck->drc_name_prefix = "PHB ";
722 drck->release = spapr_phb_release;
723 drck->dt_populate = spapr_phb_dt_populate;
726 static void spapr_drc_pmem_class_init(ObjectClass *k, void *data)
728 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
730 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM;
731 drck->typename = "PMEM";
732 drck->drc_name_prefix = "PMEM ";
733 drck->release = NULL;
734 drck->dt_populate = spapr_pmem_dt_populate;
737 static const TypeInfo spapr_dr_connector_info = {
738 .name = TYPE_SPAPR_DR_CONNECTOR,
739 .parent = TYPE_DEVICE,
740 .instance_size = sizeof(SpaprDrc),
741 .instance_init = spapr_dr_connector_instance_init,
742 .class_size = sizeof(SpaprDrcClass),
743 .class_init = spapr_dr_connector_class_init,
744 .abstract = true,
747 static const TypeInfo spapr_drc_physical_info = {
748 .name = TYPE_SPAPR_DRC_PHYSICAL,
749 .parent = TYPE_SPAPR_DR_CONNECTOR,
750 .instance_size = sizeof(SpaprDrcPhysical),
751 .class_init = spapr_drc_physical_class_init,
752 .abstract = true,
755 static const TypeInfo spapr_drc_logical_info = {
756 .name = TYPE_SPAPR_DRC_LOGICAL,
757 .parent = TYPE_SPAPR_DR_CONNECTOR,
758 .class_init = spapr_drc_logical_class_init,
759 .abstract = true,
762 static const TypeInfo spapr_drc_cpu_info = {
763 .name = TYPE_SPAPR_DRC_CPU,
764 .parent = TYPE_SPAPR_DRC_LOGICAL,
765 .class_init = spapr_drc_cpu_class_init,
768 static const TypeInfo spapr_drc_pci_info = {
769 .name = TYPE_SPAPR_DRC_PCI,
770 .parent = TYPE_SPAPR_DRC_PHYSICAL,
771 .class_init = spapr_drc_pci_class_init,
774 static const TypeInfo spapr_drc_lmb_info = {
775 .name = TYPE_SPAPR_DRC_LMB,
776 .parent = TYPE_SPAPR_DRC_LOGICAL,
777 .class_init = spapr_drc_lmb_class_init,
780 static const TypeInfo spapr_drc_phb_info = {
781 .name = TYPE_SPAPR_DRC_PHB,
782 .parent = TYPE_SPAPR_DRC_LOGICAL,
783 .instance_size = sizeof(SpaprDrc),
784 .class_init = spapr_drc_phb_class_init,
787 static const TypeInfo spapr_drc_pmem_info = {
788 .name = TYPE_SPAPR_DRC_PMEM,
789 .parent = TYPE_SPAPR_DRC_LOGICAL,
790 .class_init = spapr_drc_pmem_class_init,
793 /* helper functions for external users */
795 SpaprDrc *spapr_drc_by_index(uint32_t index)
797 Object *obj;
798 gchar *name;
800 name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH, index);
801 obj = object_resolve_path(name, NULL);
802 g_free(name);
804 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
807 SpaprDrc *spapr_drc_by_id(const char *type, uint32_t id)
809 SpaprDrcClass *drck
810 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
812 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
813 | (id & DRC_INDEX_ID_MASK));
817 * spapr_dt_drc
819 * @fdt: libfdt device tree
820 * @path: path in the DT to generate properties
821 * @owner: parent Object/DeviceState for which to generate DRC
822 * descriptions for
823 * @drc_type_mask: mask of SpaprDrcType values corresponding
824 * to the types of DRCs to generate entries for
826 * generate OF properties to describe DRC topology/indices to guests
828 * as documented in PAPR+ v2.1, 13.5.2
830 int spapr_dt_drc(void *fdt, int offset, Object *owner, uint32_t drc_type_mask)
832 Object *root_container;
833 ObjectProperty *prop;
834 ObjectPropertyIterator iter;
835 uint32_t drc_count = 0;
836 GArray *drc_indexes, *drc_power_domains;
837 GString *drc_names, *drc_types;
838 int ret;
840 /* the first entry of each properties is a 32-bit integer encoding
841 * the number of elements in the array. we won't know this until
842 * we complete the iteration through all the matching DRCs, but
843 * reserve the space now and set the offsets accordingly so we
844 * can fill them in later.
846 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
847 drc_indexes = g_array_set_size(drc_indexes, 1);
848 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
849 drc_power_domains = g_array_set_size(drc_power_domains, 1);
850 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
851 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
853 /* aliases for all DRConnector objects will be rooted in QOM
854 * composition tree at DRC_CONTAINER_PATH
856 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
858 object_property_iter_init(&iter, root_container);
859 while ((prop = object_property_iter_next(&iter))) {
860 Object *obj;
861 SpaprDrc *drc;
862 SpaprDrcClass *drck;
863 char *drc_name = NULL;
864 uint32_t drc_index, drc_power_domain;
866 if (!strstart(prop->type, "link<", NULL)) {
867 continue;
870 obj = object_property_get_link(root_container, prop->name,
871 &error_abort);
872 drc = SPAPR_DR_CONNECTOR(obj);
873 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
875 if (owner && (drc->owner != owner)) {
876 continue;
879 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
880 continue;
883 drc_count++;
885 /* ibm,drc-indexes */
886 drc_index = cpu_to_be32(spapr_drc_index(drc));
887 g_array_append_val(drc_indexes, drc_index);
889 /* ibm,drc-power-domains */
890 drc_power_domain = cpu_to_be32(-1);
891 g_array_append_val(drc_power_domains, drc_power_domain);
893 /* ibm,drc-names */
894 drc_name = spapr_drc_name(drc);
895 drc_names = g_string_append(drc_names, drc_name);
896 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
897 g_free(drc_name);
899 /* ibm,drc-types */
900 drc_types = g_string_append(drc_types, drck->typename);
901 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
904 /* now write the drc count into the space we reserved at the
905 * beginning of the arrays previously
907 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
908 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
909 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
910 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
912 ret = fdt_setprop(fdt, offset, "ibm,drc-indexes",
913 drc_indexes->data,
914 drc_indexes->len * sizeof(uint32_t));
915 if (ret) {
916 error_report("Couldn't create ibm,drc-indexes property");
917 goto out;
920 ret = fdt_setprop(fdt, offset, "ibm,drc-power-domains",
921 drc_power_domains->data,
922 drc_power_domains->len * sizeof(uint32_t));
923 if (ret) {
924 error_report("Couldn't finalize ibm,drc-power-domains property");
925 goto out;
928 ret = fdt_setprop(fdt, offset, "ibm,drc-names",
929 drc_names->str, drc_names->len);
930 if (ret) {
931 error_report("Couldn't finalize ibm,drc-names property");
932 goto out;
935 ret = fdt_setprop(fdt, offset, "ibm,drc-types",
936 drc_types->str, drc_types->len);
937 if (ret) {
938 error_report("Couldn't finalize ibm,drc-types property");
939 goto out;
942 out:
943 g_array_free(drc_indexes, true);
944 g_array_free(drc_power_domains, true);
945 g_string_free(drc_names, true);
946 g_string_free(drc_types, true);
948 return ret;
952 * RTAS calls
955 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
957 SpaprDrc *drc = spapr_drc_by_index(idx);
958 SpaprDrcClass *drck;
960 if (!drc) {
961 return RTAS_OUT_NO_SUCH_INDICATOR;
964 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
966 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
968 switch (state) {
969 case SPAPR_DR_ISOLATION_STATE_ISOLATED:
970 return drck->isolate(drc);
972 case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
973 return drck->unisolate(drc);
975 default:
976 return RTAS_OUT_PARAM_ERROR;
980 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
982 SpaprDrc *drc = spapr_drc_by_index(idx);
984 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
985 return RTAS_OUT_NO_SUCH_INDICATOR;
988 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
990 switch (state) {
991 case SPAPR_DR_ALLOCATION_STATE_USABLE:
992 return drc_set_usable(drc);
994 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
995 return drc_set_unusable(drc);
997 default:
998 return RTAS_OUT_PARAM_ERROR;
1002 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
1004 SpaprDrc *drc = spapr_drc_by_index(idx);
1006 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
1007 return RTAS_OUT_NO_SUCH_INDICATOR;
1009 if ((state != SPAPR_DR_INDICATOR_INACTIVE)
1010 && (state != SPAPR_DR_INDICATOR_ACTIVE)
1011 && (state != SPAPR_DR_INDICATOR_IDENTIFY)
1012 && (state != SPAPR_DR_INDICATOR_ACTION)) {
1013 return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
1016 trace_spapr_drc_set_dr_indicator(idx, state);
1017 SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
1018 return RTAS_OUT_SUCCESS;
1021 static void rtas_set_indicator(PowerPCCPU *cpu, SpaprMachineState *spapr,
1022 uint32_t token,
1023 uint32_t nargs, target_ulong args,
1024 uint32_t nret, target_ulong rets)
1026 uint32_t type, idx, state;
1027 uint32_t ret = RTAS_OUT_SUCCESS;
1029 if (nargs != 3 || nret != 1) {
1030 ret = RTAS_OUT_PARAM_ERROR;
1031 goto out;
1034 type = rtas_ld(args, 0);
1035 idx = rtas_ld(args, 1);
1036 state = rtas_ld(args, 2);
1038 switch (type) {
1039 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1040 ret = rtas_set_isolation_state(idx, state);
1041 break;
1042 case RTAS_SENSOR_TYPE_DR:
1043 ret = rtas_set_dr_indicator(idx, state);
1044 break;
1045 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1046 ret = rtas_set_allocation_state(idx, state);
1047 break;
1048 default:
1049 ret = RTAS_OUT_NOT_SUPPORTED;
1052 out:
1053 rtas_st(rets, 0, ret);
1056 static void rtas_get_sensor_state(PowerPCCPU *cpu, SpaprMachineState *spapr,
1057 uint32_t token, uint32_t nargs,
1058 target_ulong args, uint32_t nret,
1059 target_ulong rets)
1061 uint32_t sensor_type;
1062 uint32_t sensor_index;
1063 uint32_t sensor_state = 0;
1064 SpaprDrc *drc;
1065 SpaprDrcClass *drck;
1066 uint32_t ret = RTAS_OUT_SUCCESS;
1068 if (nargs != 2 || nret != 2) {
1069 ret = RTAS_OUT_PARAM_ERROR;
1070 goto out;
1073 sensor_type = rtas_ld(args, 0);
1074 sensor_index = rtas_ld(args, 1);
1076 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1077 /* currently only DR-related sensors are implemented */
1078 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1079 sensor_type);
1080 ret = RTAS_OUT_NOT_SUPPORTED;
1081 goto out;
1084 drc = spapr_drc_by_index(sensor_index);
1085 if (!drc) {
1086 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1087 ret = RTAS_OUT_PARAM_ERROR;
1088 goto out;
1090 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1091 sensor_state = drck->dr_entity_sense(drc);
1093 out:
1094 rtas_st(rets, 0, ret);
1095 rtas_st(rets, 1, sensor_state);
1098 /* configure-connector work area offsets, int32_t units for field
1099 * indexes, bytes for field offset/len values.
1101 * as documented by PAPR+ v2.7, 13.5.3.5
1103 #define CC_IDX_NODE_NAME_OFFSET 2
1104 #define CC_IDX_PROP_NAME_OFFSET 2
1105 #define CC_IDX_PROP_LEN 3
1106 #define CC_IDX_PROP_DATA_OFFSET 4
1107 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1108 #define CC_WA_LEN 4096
1110 static void configure_connector_st(target_ulong addr, target_ulong offset,
1111 const void *buf, size_t len)
1113 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1114 buf, MIN(len, CC_WA_LEN - offset));
1117 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1118 SpaprMachineState *spapr,
1119 uint32_t token, uint32_t nargs,
1120 target_ulong args, uint32_t nret,
1121 target_ulong rets)
1123 uint64_t wa_addr;
1124 uint64_t wa_offset;
1125 uint32_t drc_index;
1126 SpaprDrc *drc;
1127 SpaprDrcClass *drck;
1128 SpaprDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1129 int rc;
1131 if (nargs != 2 || nret != 1) {
1132 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1133 return;
1136 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1138 drc_index = rtas_ld(wa_addr, 0);
1139 drc = spapr_drc_by_index(drc_index);
1140 if (!drc) {
1141 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1142 rc = RTAS_OUT_PARAM_ERROR;
1143 goto out;
1146 if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
1147 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
1148 && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
1149 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
1151 * Need to unisolate the device before configuring
1152 * or it should already be in configured state to
1153 * allow configure-connector be called repeatedly.
1155 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1156 goto out;
1159 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1161 if (!drc->fdt) {
1162 void *fdt;
1163 int fdt_size;
1165 fdt = create_device_tree(&fdt_size);
1167 if (drck->dt_populate(drc, spapr, fdt, &drc->fdt_start_offset,
1168 NULL)) {
1169 g_free(fdt);
1170 rc = SPAPR_DR_CC_RESPONSE_ERROR;
1171 goto out;
1174 drc->fdt = fdt;
1175 drc->ccs_offset = drc->fdt_start_offset;
1176 drc->ccs_depth = 0;
1179 do {
1180 uint32_t tag;
1181 const char *name;
1182 const struct fdt_property *prop;
1183 int fdt_offset_next, prop_len;
1185 tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
1187 switch (tag) {
1188 case FDT_BEGIN_NODE:
1189 drc->ccs_depth++;
1190 name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
1192 /* provide the name of the next OF node */
1193 wa_offset = CC_VAL_DATA_OFFSET;
1194 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1195 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1196 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1197 break;
1198 case FDT_END_NODE:
1199 drc->ccs_depth--;
1200 if (drc->ccs_depth == 0) {
1201 uint32_t drc_index = spapr_drc_index(drc);
1203 /* done sending the device tree, move to configured state */
1204 trace_spapr_drc_set_configured(drc_index);
1205 drc->state = drck->ready_state;
1207 * Ensure that we are able to send the FDT fragment
1208 * again via configure-connector call if the guest requests.
1210 drc->ccs_offset = drc->fdt_start_offset;
1211 drc->ccs_depth = 0;
1212 fdt_offset_next = drc->fdt_start_offset;
1213 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1214 } else {
1215 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1217 break;
1218 case FDT_PROP:
1219 prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
1220 &prop_len);
1221 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1223 /* provide the name of the next OF property */
1224 wa_offset = CC_VAL_DATA_OFFSET;
1225 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1226 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1228 /* provide the length and value of the OF property. data gets
1229 * placed immediately after NULL terminator of the OF property's
1230 * name string
1232 wa_offset += strlen(name) + 1,
1233 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1234 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1235 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1236 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1237 break;
1238 case FDT_END:
1239 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1240 default:
1241 /* keep seeking for an actionable tag */
1242 break;
1244 if (drc->ccs_offset >= 0) {
1245 drc->ccs_offset = fdt_offset_next;
1247 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1249 rc = resp;
1250 out:
1251 rtas_st(rets, 0, rc);
1254 static void spapr_drc_register_types(void)
1256 type_register_static(&spapr_dr_connector_info);
1257 type_register_static(&spapr_drc_physical_info);
1258 type_register_static(&spapr_drc_logical_info);
1259 type_register_static(&spapr_drc_cpu_info);
1260 type_register_static(&spapr_drc_pci_info);
1261 type_register_static(&spapr_drc_lmb_info);
1262 type_register_static(&spapr_drc_phb_info);
1263 type_register_static(&spapr_drc_pmem_info);
1265 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1266 rtas_set_indicator);
1267 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1268 rtas_get_sensor_state);
1269 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1270 rtas_ibm_configure_connector);
1272 type_init(spapr_drc_register_types)