spapr: Split DRC release from DRC detach
[qemu/kevin.git] / hw / ppc / spapr_drc.c
blob8a2b8f5f659453b0e384d274b57ad9c26a38d2e6
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 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) {
60 g_free(drc->ccs);
61 drc->ccs = NULL;
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)
68 if (!drc->dev ||
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
100 * PAPR+ 2.7, 13.4
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);
107 } else {
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"
128 if (!drc->dev) {
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);
176 * dr-entity-sense sensor value
177 * returned via get-sensor-state RTAS calls
178 * as expected by state diagram in PAPR+ 2.7, 13.4
179 * based on the current allocation/indicator/power states
180 * for the DR connector.
182 static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc)
184 /* this assumes all PCI devices are assigned to a 'live insertion'
185 * power domain, where QEMU manages power state automatically as
186 * opposed to the guest. present, non-PCI resources are unaffected
187 * by power state.
189 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
190 : SPAPR_DR_ENTITY_SENSE_EMPTY;
193 static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc)
195 if (drc->dev
196 && (drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE)) {
197 return SPAPR_DR_ENTITY_SENSE_PRESENT;
198 } else {
199 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
203 static void prop_get_index(Object *obj, Visitor *v, const char *name,
204 void *opaque, Error **errp)
206 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
207 uint32_t value = spapr_drc_index(drc);
208 visit_type_uint32(v, name, &value, errp);
211 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
212 void *opaque, Error **errp)
214 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
215 Error *err = NULL;
216 int fdt_offset_next, fdt_offset, fdt_depth;
217 void *fdt;
219 if (!drc->fdt) {
220 visit_type_null(v, NULL, errp);
221 return;
224 fdt = drc->fdt;
225 fdt_offset = drc->fdt_start_offset;
226 fdt_depth = 0;
228 do {
229 const char *name = NULL;
230 const struct fdt_property *prop = NULL;
231 int prop_len = 0, name_len = 0;
232 uint32_t tag;
234 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
235 switch (tag) {
236 case FDT_BEGIN_NODE:
237 fdt_depth++;
238 name = fdt_get_name(fdt, fdt_offset, &name_len);
239 visit_start_struct(v, name, NULL, 0, &err);
240 if (err) {
241 error_propagate(errp, err);
242 return;
244 break;
245 case FDT_END_NODE:
246 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
247 g_assert(fdt_depth > 0);
248 visit_check_struct(v, &err);
249 visit_end_struct(v, NULL);
250 if (err) {
251 error_propagate(errp, err);
252 return;
254 fdt_depth--;
255 break;
256 case FDT_PROP: {
257 int i;
258 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
259 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
260 visit_start_list(v, name, NULL, 0, &err);
261 if (err) {
262 error_propagate(errp, err);
263 return;
265 for (i = 0; i < prop_len; i++) {
266 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
267 if (err) {
268 error_propagate(errp, err);
269 return;
272 visit_check_list(v, &err);
273 visit_end_list(v, NULL);
274 if (err) {
275 error_propagate(errp, err);
276 return;
278 break;
280 default:
281 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
283 fdt_offset = fdt_offset_next;
284 } while (fdt_depth != 0);
287 void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
288 int fdt_start_offset, bool coldplug, Error **errp)
290 trace_spapr_drc_attach(spapr_drc_index(drc));
292 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
293 error_setg(errp, "an attached device is still awaiting release");
294 return;
296 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
297 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
299 g_assert(fdt || coldplug);
301 drc->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
303 drc->dev = d;
304 drc->fdt = fdt;
305 drc->fdt_start_offset = fdt_start_offset;
306 drc->configured = coldplug;
308 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
309 drc->awaiting_allocation = true;
312 object_property_add_link(OBJECT(drc), "device",
313 object_get_typename(OBJECT(drc->dev)),
314 (Object **)(&drc->dev),
315 NULL, 0, NULL);
318 static void spapr_drc_release(sPAPRDRConnector *drc)
320 drc->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
322 /* Calling release callbacks based on spapr_drc_type(drc). */
323 switch (spapr_drc_type(drc)) {
324 case SPAPR_DR_CONNECTOR_TYPE_CPU:
325 spapr_core_release(drc->dev);
326 break;
327 case SPAPR_DR_CONNECTOR_TYPE_PCI:
328 spapr_phb_remove_pci_device_cb(drc->dev);
329 break;
330 case SPAPR_DR_CONNECTOR_TYPE_LMB:
331 spapr_lmb_release(drc->dev);
332 break;
333 case SPAPR_DR_CONNECTOR_TYPE_PHB:
334 case SPAPR_DR_CONNECTOR_TYPE_VIO:
335 default:
336 g_assert(false);
339 drc->awaiting_release = false;
340 g_free(drc->fdt);
341 drc->fdt = NULL;
342 drc->fdt_start_offset = 0;
343 object_property_del(OBJECT(drc), "device", NULL);
344 drc->dev = NULL;
347 void spapr_drc_detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
349 trace_spapr_drc_detach(spapr_drc_index(drc));
351 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
352 trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
353 drc->awaiting_release = true;
354 return;
357 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
358 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
359 trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
360 drc->awaiting_release = true;
361 return;
364 if (drc->awaiting_allocation) {
365 drc->awaiting_release = true;
366 trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
367 return;
370 spapr_drc_release(drc);
373 static bool release_pending(sPAPRDRConnector *drc)
375 return drc->awaiting_release;
378 static void reset(DeviceState *d)
380 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
381 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
383 trace_spapr_drc_reset(spapr_drc_index(drc));
385 g_free(drc->ccs);
386 drc->ccs = NULL;
388 /* immediately upon reset we can safely assume DRCs whose devices
389 * are pending removal can be safely removed, and that they will
390 * subsequently be left in an ISOLATED state. move the DRC to this
391 * state in these cases (which will in turn complete any pending
392 * device removals)
394 if (drc->awaiting_release) {
395 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
396 /* generally this should also finalize the removal, but if the device
397 * hasn't yet been configured we normally defer removal under the
398 * assumption that this transition is taking place as part of device
399 * configuration. so check if we're still waiting after this, and
400 * force removal if we are
402 if (drc->awaiting_release) {
403 spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
406 /* non-PCI devices may be awaiting a transition to UNUSABLE */
407 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
408 drc->awaiting_release) {
409 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
414 static bool spapr_drc_needed(void *opaque)
416 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
417 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
418 bool rc = false;
419 sPAPRDREntitySense value = drck->dr_entity_sense(drc);
421 /* If no dev is plugged in there is no need to migrate the DRC state */
422 if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
423 return false;
427 * If there is dev plugged in, we need to migrate the DRC state when
428 * it is different from cold-plugged state
430 switch (spapr_drc_type(drc)) {
431 case SPAPR_DR_CONNECTOR_TYPE_PCI:
432 case SPAPR_DR_CONNECTOR_TYPE_CPU:
433 case SPAPR_DR_CONNECTOR_TYPE_LMB:
434 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
435 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
436 drc->configured && !drc->awaiting_release);
437 break;
438 case SPAPR_DR_CONNECTOR_TYPE_PHB:
439 case SPAPR_DR_CONNECTOR_TYPE_VIO:
440 default:
441 g_assert_not_reached();
443 return rc;
446 static const VMStateDescription vmstate_spapr_drc = {
447 .name = "spapr_drc",
448 .version_id = 1,
449 .minimum_version_id = 1,
450 .needed = spapr_drc_needed,
451 .fields = (VMStateField []) {
452 VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
453 VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
454 VMSTATE_UINT32(dr_indicator, sPAPRDRConnector),
455 VMSTATE_BOOL(configured, sPAPRDRConnector),
456 VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
457 VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
458 VMSTATE_END_OF_LIST()
462 static void realize(DeviceState *d, Error **errp)
464 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
465 Object *root_container;
466 char link_name[256];
467 gchar *child_name;
468 Error *err = NULL;
470 trace_spapr_drc_realize(spapr_drc_index(drc));
471 /* NOTE: we do this as part of realize/unrealize due to the fact
472 * that the guest will communicate with the DRC via RTAS calls
473 * referencing the global DRC index. By unlinking the DRC
474 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
475 * inaccessible by the guest, since lookups rely on this path
476 * existing in the composition tree
478 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
479 snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
480 child_name = object_get_canonical_path_component(OBJECT(drc));
481 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
482 object_property_add_alias(root_container, link_name,
483 drc->owner, child_name, &err);
484 if (err) {
485 error_report_err(err);
486 object_unref(OBJECT(drc));
488 g_free(child_name);
489 vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
490 drc);
491 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
494 static void unrealize(DeviceState *d, Error **errp)
496 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
497 Object *root_container;
498 char name[256];
499 Error *err = NULL;
501 trace_spapr_drc_unrealize(spapr_drc_index(drc));
502 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
503 snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
504 object_property_del(root_container, name, &err);
505 if (err) {
506 error_report_err(err);
507 object_unref(OBJECT(drc));
511 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
512 uint32_t id)
514 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
515 char *prop_name;
517 drc->id = id;
518 drc->owner = owner;
519 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
520 spapr_drc_index(drc));
521 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
522 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
523 g_free(prop_name);
525 /* PCI slot always start in a USABLE state, and stay there */
526 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
527 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
530 return drc;
533 static void spapr_dr_connector_instance_init(Object *obj)
535 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
537 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
538 object_property_add(obj, "index", "uint32", prop_get_index,
539 NULL, NULL, NULL, NULL);
540 object_property_add(obj, "fdt", "struct", prop_get_fdt,
541 NULL, NULL, NULL, NULL);
544 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
546 DeviceClass *dk = DEVICE_CLASS(k);
547 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
549 dk->reset = reset;
550 dk->realize = realize;
551 dk->unrealize = unrealize;
552 drck->set_isolation_state = set_isolation_state;
553 drck->set_allocation_state = set_allocation_state;
554 drck->release_pending = release_pending;
556 * Reason: it crashes FIXME find and document the real reason
558 dk->user_creatable = false;
561 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
563 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
565 drck->dr_entity_sense = physical_entity_sense;
568 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
570 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
572 drck->dr_entity_sense = logical_entity_sense;
575 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
577 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
579 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
580 drck->typename = "CPU";
581 drck->drc_name_prefix = "CPU ";
584 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
586 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
588 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
589 drck->typename = "28";
590 drck->drc_name_prefix = "C";
593 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
595 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
597 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
598 drck->typename = "MEM";
599 drck->drc_name_prefix = "LMB ";
602 static const TypeInfo spapr_dr_connector_info = {
603 .name = TYPE_SPAPR_DR_CONNECTOR,
604 .parent = TYPE_DEVICE,
605 .instance_size = sizeof(sPAPRDRConnector),
606 .instance_init = spapr_dr_connector_instance_init,
607 .class_size = sizeof(sPAPRDRConnectorClass),
608 .class_init = spapr_dr_connector_class_init,
609 .abstract = true,
612 static const TypeInfo spapr_drc_physical_info = {
613 .name = TYPE_SPAPR_DRC_PHYSICAL,
614 .parent = TYPE_SPAPR_DR_CONNECTOR,
615 .instance_size = sizeof(sPAPRDRConnector),
616 .class_init = spapr_drc_physical_class_init,
617 .abstract = true,
620 static const TypeInfo spapr_drc_logical_info = {
621 .name = TYPE_SPAPR_DRC_LOGICAL,
622 .parent = TYPE_SPAPR_DR_CONNECTOR,
623 .instance_size = sizeof(sPAPRDRConnector),
624 .class_init = spapr_drc_logical_class_init,
625 .abstract = true,
628 static const TypeInfo spapr_drc_cpu_info = {
629 .name = TYPE_SPAPR_DRC_CPU,
630 .parent = TYPE_SPAPR_DRC_LOGICAL,
631 .instance_size = sizeof(sPAPRDRConnector),
632 .class_init = spapr_drc_cpu_class_init,
635 static const TypeInfo spapr_drc_pci_info = {
636 .name = TYPE_SPAPR_DRC_PCI,
637 .parent = TYPE_SPAPR_DRC_PHYSICAL,
638 .instance_size = sizeof(sPAPRDRConnector),
639 .class_init = spapr_drc_pci_class_init,
642 static const TypeInfo spapr_drc_lmb_info = {
643 .name = TYPE_SPAPR_DRC_LMB,
644 .parent = TYPE_SPAPR_DRC_LOGICAL,
645 .instance_size = sizeof(sPAPRDRConnector),
646 .class_init = spapr_drc_lmb_class_init,
649 /* helper functions for external users */
651 sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
653 Object *obj;
654 char name[256];
656 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
657 obj = object_resolve_path(name, NULL);
659 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
662 sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
664 sPAPRDRConnectorClass *drck
665 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
667 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
668 | (id & DRC_INDEX_ID_MASK));
672 * spapr_drc_populate_dt
674 * @fdt: libfdt device tree
675 * @path: path in the DT to generate properties
676 * @owner: parent Object/DeviceState for which to generate DRC
677 * descriptions for
678 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
679 * to the types of DRCs to generate entries for
681 * generate OF properties to describe DRC topology/indices to guests
683 * as documented in PAPR+ v2.1, 13.5.2
685 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
686 uint32_t drc_type_mask)
688 Object *root_container;
689 ObjectProperty *prop;
690 ObjectPropertyIterator iter;
691 uint32_t drc_count = 0;
692 GArray *drc_indexes, *drc_power_domains;
693 GString *drc_names, *drc_types;
694 int ret;
696 /* the first entry of each properties is a 32-bit integer encoding
697 * the number of elements in the array. we won't know this until
698 * we complete the iteration through all the matching DRCs, but
699 * reserve the space now and set the offsets accordingly so we
700 * can fill them in later.
702 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
703 drc_indexes = g_array_set_size(drc_indexes, 1);
704 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
705 drc_power_domains = g_array_set_size(drc_power_domains, 1);
706 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
707 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
709 /* aliases for all DRConnector objects will be rooted in QOM
710 * composition tree at DRC_CONTAINER_PATH
712 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
714 object_property_iter_init(&iter, root_container);
715 while ((prop = object_property_iter_next(&iter))) {
716 Object *obj;
717 sPAPRDRConnector *drc;
718 sPAPRDRConnectorClass *drck;
719 uint32_t drc_index, drc_power_domain;
721 if (!strstart(prop->type, "link<", NULL)) {
722 continue;
725 obj = object_property_get_link(root_container, prop->name, NULL);
726 drc = SPAPR_DR_CONNECTOR(obj);
727 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
729 if (owner && (drc->owner != owner)) {
730 continue;
733 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
734 continue;
737 drc_count++;
739 /* ibm,drc-indexes */
740 drc_index = cpu_to_be32(spapr_drc_index(drc));
741 g_array_append_val(drc_indexes, drc_index);
743 /* ibm,drc-power-domains */
744 drc_power_domain = cpu_to_be32(-1);
745 g_array_append_val(drc_power_domains, drc_power_domain);
747 /* ibm,drc-names */
748 drc_names = g_string_append(drc_names, spapr_drc_name(drc));
749 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
751 /* ibm,drc-types */
752 drc_types = g_string_append(drc_types, drck->typename);
753 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
756 /* now write the drc count into the space we reserved at the
757 * beginning of the arrays previously
759 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
760 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
761 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
762 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
764 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
765 drc_indexes->data,
766 drc_indexes->len * sizeof(uint32_t));
767 if (ret) {
768 error_report("Couldn't create ibm,drc-indexes property");
769 goto out;
772 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
773 drc_power_domains->data,
774 drc_power_domains->len * sizeof(uint32_t));
775 if (ret) {
776 error_report("Couldn't finalize ibm,drc-power-domains property");
777 goto out;
780 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
781 drc_names->str, drc_names->len);
782 if (ret) {
783 error_report("Couldn't finalize ibm,drc-names property");
784 goto out;
787 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
788 drc_types->str, drc_types->len);
789 if (ret) {
790 error_report("Couldn't finalize ibm,drc-types property");
791 goto out;
794 out:
795 g_array_free(drc_indexes, true);
796 g_array_free(drc_power_domains, true);
797 g_string_free(drc_names, true);
798 g_string_free(drc_types, true);
800 return ret;
804 * RTAS calls
807 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
809 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
810 sPAPRDRConnectorClass *drck;
812 if (!drc) {
813 return RTAS_OUT_PARAM_ERROR;
816 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
817 return drck->set_isolation_state(drc, state);
820 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
822 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
823 sPAPRDRConnectorClass *drck;
825 if (!drc) {
826 return RTAS_OUT_PARAM_ERROR;
829 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
830 return drck->set_allocation_state(drc, state);
833 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
835 sPAPRDRConnector *drc = spapr_drc_by_index(idx);
837 if (!drc) {
838 return RTAS_OUT_PARAM_ERROR;
841 trace_spapr_drc_set_dr_indicator(idx, state);
842 drc->dr_indicator = state;
843 return RTAS_OUT_SUCCESS;
846 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
847 uint32_t token,
848 uint32_t nargs, target_ulong args,
849 uint32_t nret, target_ulong rets)
851 uint32_t type, idx, state;
852 uint32_t ret = RTAS_OUT_SUCCESS;
854 if (nargs != 3 || nret != 1) {
855 ret = RTAS_OUT_PARAM_ERROR;
856 goto out;
859 type = rtas_ld(args, 0);
860 idx = rtas_ld(args, 1);
861 state = rtas_ld(args, 2);
863 switch (type) {
864 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
865 ret = rtas_set_isolation_state(idx, state);
866 break;
867 case RTAS_SENSOR_TYPE_DR:
868 ret = rtas_set_dr_indicator(idx, state);
869 break;
870 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
871 ret = rtas_set_allocation_state(idx, state);
872 break;
873 default:
874 ret = RTAS_OUT_NOT_SUPPORTED;
877 out:
878 rtas_st(rets, 0, ret);
881 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
882 uint32_t token, uint32_t nargs,
883 target_ulong args, uint32_t nret,
884 target_ulong rets)
886 uint32_t sensor_type;
887 uint32_t sensor_index;
888 uint32_t sensor_state = 0;
889 sPAPRDRConnector *drc;
890 sPAPRDRConnectorClass *drck;
891 uint32_t ret = RTAS_OUT_SUCCESS;
893 if (nargs != 2 || nret != 2) {
894 ret = RTAS_OUT_PARAM_ERROR;
895 goto out;
898 sensor_type = rtas_ld(args, 0);
899 sensor_index = rtas_ld(args, 1);
901 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
902 /* currently only DR-related sensors are implemented */
903 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
904 sensor_type);
905 ret = RTAS_OUT_NOT_SUPPORTED;
906 goto out;
909 drc = spapr_drc_by_index(sensor_index);
910 if (!drc) {
911 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
912 ret = RTAS_OUT_PARAM_ERROR;
913 goto out;
915 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
916 sensor_state = drck->dr_entity_sense(drc);
918 out:
919 rtas_st(rets, 0, ret);
920 rtas_st(rets, 1, sensor_state);
923 /* configure-connector work area offsets, int32_t units for field
924 * indexes, bytes for field offset/len values.
926 * as documented by PAPR+ v2.7, 13.5.3.5
928 #define CC_IDX_NODE_NAME_OFFSET 2
929 #define CC_IDX_PROP_NAME_OFFSET 2
930 #define CC_IDX_PROP_LEN 3
931 #define CC_IDX_PROP_DATA_OFFSET 4
932 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
933 #define CC_WA_LEN 4096
935 static void configure_connector_st(target_ulong addr, target_ulong offset,
936 const void *buf, size_t len)
938 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
939 buf, MIN(len, CC_WA_LEN - offset));
942 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
943 sPAPRMachineState *spapr,
944 uint32_t token, uint32_t nargs,
945 target_ulong args, uint32_t nret,
946 target_ulong rets)
948 uint64_t wa_addr;
949 uint64_t wa_offset;
950 uint32_t drc_index;
951 sPAPRDRConnector *drc;
952 sPAPRConfigureConnectorState *ccs;
953 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
954 int rc;
956 if (nargs != 2 || nret != 1) {
957 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
958 return;
961 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
963 drc_index = rtas_ld(wa_addr, 0);
964 drc = spapr_drc_by_index(drc_index);
965 if (!drc) {
966 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
967 rc = RTAS_OUT_PARAM_ERROR;
968 goto out;
971 if (!drc->fdt) {
972 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
973 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
974 goto out;
977 ccs = drc->ccs;
978 if (!ccs) {
979 ccs = g_new0(sPAPRConfigureConnectorState, 1);
980 ccs->fdt_offset = drc->fdt_start_offset;
981 drc->ccs = ccs;
984 do {
985 uint32_t tag;
986 const char *name;
987 const struct fdt_property *prop;
988 int fdt_offset_next, prop_len;
990 tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
992 switch (tag) {
993 case FDT_BEGIN_NODE:
994 ccs->fdt_depth++;
995 name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
997 /* provide the name of the next OF node */
998 wa_offset = CC_VAL_DATA_OFFSET;
999 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1000 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1001 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1002 break;
1003 case FDT_END_NODE:
1004 ccs->fdt_depth--;
1005 if (ccs->fdt_depth == 0) {
1006 sPAPRDRIsolationState state = drc->isolation_state;
1007 uint32_t drc_index = spapr_drc_index(drc);
1008 /* done sending the device tree, don't need to track
1009 * the state anymore
1011 trace_spapr_drc_set_configured(drc_index);
1012 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
1013 drc->configured = true;
1014 } else {
1015 /* guest should be not configuring an isolated device */
1016 trace_spapr_drc_set_configured_skipping(drc_index);
1018 g_free(ccs);
1019 drc->ccs = NULL;
1020 ccs = NULL;
1021 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1022 } else {
1023 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1025 break;
1026 case FDT_PROP:
1027 prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1028 &prop_len);
1029 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1031 /* provide the name of the next OF property */
1032 wa_offset = CC_VAL_DATA_OFFSET;
1033 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1034 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1036 /* provide the length and value of the OF property. data gets
1037 * placed immediately after NULL terminator of the OF property's
1038 * name string
1040 wa_offset += strlen(name) + 1,
1041 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1042 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1043 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1044 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1045 break;
1046 case FDT_END:
1047 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1048 default:
1049 /* keep seeking for an actionable tag */
1050 break;
1052 if (ccs) {
1053 ccs->fdt_offset = fdt_offset_next;
1055 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1057 rc = resp;
1058 out:
1059 rtas_st(rets, 0, rc);
1062 static void spapr_drc_register_types(void)
1064 type_register_static(&spapr_dr_connector_info);
1065 type_register_static(&spapr_drc_physical_info);
1066 type_register_static(&spapr_drc_logical_info);
1067 type_register_static(&spapr_drc_cpu_info);
1068 type_register_static(&spapr_drc_pci_info);
1069 type_register_static(&spapr_drc_lmb_info);
1071 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1072 rtas_set_indicator);
1073 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1074 rtas_get_sensor_state);
1075 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1076 rtas_ibm_configure_connector);
1078 type_init(spapr_drc_register_types)