qapi qdev qom: Elide redundant has_FOO in generated C
[qemu.git] / hw / ppc / spapr_drc.c
blob4923435a8b5be8d7b79c90f748f144404cbcb1a9
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 "qemu/cutils.h"
17 #include "hw/ppc/spapr_drc.h"
18 #include "qom/object.h"
19 #include "migration/vmstate.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-events-qdev.h"
22 #include "qapi/visitor.h"
23 #include "qemu/error-report.h"
24 #include "hw/ppc/spapr.h" /* for RTAS return codes */
25 #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
26 #include "hw/ppc/spapr_nvdimm.h"
27 #include "sysemu/device_tree.h"
28 #include "sysemu/reset.h"
29 #include "trace.h"
31 #define DRC_CONTAINER_PATH "/dr-connector"
32 #define DRC_INDEX_TYPE_SHIFT 28
33 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
35 SpaprDrcType spapr_drc_type(SpaprDrc *drc)
37 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
39 return 1 << drck->typeshift;
42 uint32_t spapr_drc_index(SpaprDrc *drc)
44 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
46 /* no set format for a drc index: it only needs to be globally
47 * unique. this is how we encode the DRC type on bare-metal
48 * however, so might as well do that here
50 return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
51 | (drc->id & DRC_INDEX_ID_MASK);
54 static void spapr_drc_release(SpaprDrc *drc)
56 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
58 drck->release(drc->dev);
60 drc->unplug_requested = false;
61 g_free(drc->fdt);
62 drc->fdt = NULL;
63 drc->fdt_start_offset = 0;
64 object_property_del(OBJECT(drc), "device");
65 drc->dev = NULL;
68 static uint32_t drc_isolate_physical(SpaprDrc *drc)
70 switch (drc->state) {
71 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
72 return RTAS_OUT_SUCCESS; /* Nothing to do */
73 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
74 break; /* see below */
75 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
76 return RTAS_OUT_PARAM_ERROR; /* not allowed */
77 default:
78 g_assert_not_reached();
81 drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
83 if (drc->unplug_requested) {
84 uint32_t drc_index = spapr_drc_index(drc);
85 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
86 spapr_drc_release(drc);
89 return RTAS_OUT_SUCCESS;
92 static uint32_t drc_unisolate_physical(SpaprDrc *drc)
94 switch (drc->state) {
95 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
96 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
97 return RTAS_OUT_SUCCESS; /* Nothing to do */
98 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
99 break; /* see below */
100 default:
101 g_assert_not_reached();
104 /* cannot unisolate a non-existent resource, and, or resources
105 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
106 * 13.5.3.5)
108 if (!drc->dev) {
109 return RTAS_OUT_NO_SUCH_INDICATOR;
112 drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE;
113 drc->ccs_offset = drc->fdt_start_offset;
114 drc->ccs_depth = 0;
116 return RTAS_OUT_SUCCESS;
119 static uint32_t drc_isolate_logical(SpaprDrc *drc)
121 switch (drc->state) {
122 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
123 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
124 return RTAS_OUT_SUCCESS; /* Nothing to do */
125 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
126 break; /* see below */
127 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
128 return RTAS_OUT_PARAM_ERROR; /* not allowed */
129 default:
130 g_assert_not_reached();
134 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
135 * belong to a DIMM device that is marked for removal.
137 * Currently the guest userspace tool drmgr that drives the memory
138 * hotplug/unplug will just try to remove a set of 'removable' LMBs
139 * in response to a hot unplug request that is based on drc-count.
140 * If the LMB being removed doesn't belong to a DIMM device that is
141 * actually being unplugged, fail the isolation request here.
143 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
144 && !drc->unplug_requested) {
145 return RTAS_OUT_HW_ERROR;
148 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
150 return RTAS_OUT_SUCCESS;
153 static uint32_t drc_unisolate_logical(SpaprDrc *drc)
155 SpaprMachineState *spapr = NULL;
157 switch (drc->state) {
158 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
159 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
161 * Unisolating a logical DRC that was marked for unplug
162 * means that the kernel is refusing the removal.
164 if (drc->unplug_requested && drc->dev) {
165 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
166 spapr = SPAPR_MACHINE(qdev_get_machine());
168 spapr_memory_unplug_rollback(spapr, drc->dev);
171 drc->unplug_requested = false;
173 if (drc->dev->id) {
174 error_report("Device hotunplug rejected by the guest "
175 "for device %s", drc->dev->id);
178 qapi_event_send_device_unplug_guest_error(drc->dev->id,
179 drc->dev->canonical_path);
182 return RTAS_OUT_SUCCESS; /* Nothing to do */
183 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
184 break; /* see below */
185 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
186 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
187 default:
188 g_assert_not_reached();
191 /* Move to AVAILABLE state should have ensured device was present */
192 g_assert(drc->dev);
194 drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
195 drc->ccs_offset = drc->fdt_start_offset;
196 drc->ccs_depth = 0;
198 return RTAS_OUT_SUCCESS;
201 static uint32_t drc_set_usable(SpaprDrc *drc)
203 switch (drc->state) {
204 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
205 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
206 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
207 return RTAS_OUT_SUCCESS; /* Nothing to do */
208 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
209 break; /* see below */
210 default:
211 g_assert_not_reached();
214 /* if there's no resource/device associated with the DRC, there's
215 * no way for us to put it in an allocation state consistent with
216 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
217 * result in an RTAS return code of -3 / "no such indicator"
219 if (!drc->dev) {
220 return RTAS_OUT_NO_SUCH_INDICATOR;
222 if (drc->unplug_requested) {
223 /* Don't allow the guest to move a device away from UNUSABLE
224 * state when we want to unplug it */
225 return RTAS_OUT_NO_SUCH_INDICATOR;
228 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
230 return RTAS_OUT_SUCCESS;
233 static uint32_t drc_set_unusable(SpaprDrc *drc)
235 switch (drc->state) {
236 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
237 return RTAS_OUT_SUCCESS; /* Nothing to do */
238 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
239 break; /* see below */
240 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
241 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
242 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
243 default:
244 g_assert_not_reached();
247 drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
248 if (drc->unplug_requested) {
249 uint32_t drc_index = spapr_drc_index(drc);
250 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
251 spapr_drc_release(drc);
254 return RTAS_OUT_SUCCESS;
257 static char *spapr_drc_name(SpaprDrc *drc)
259 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
261 /* human-readable name for a DRC to encode into the DT
262 * description. this is mainly only used within a guest in place
263 * of the unique DRC index.
265 * in the case of VIO/PCI devices, it corresponds to a "location
266 * code" that maps a logical device/function (DRC index) to a
267 * physical (or virtual in the case of VIO) location in the system
268 * by chaining together the "location label" for each
269 * encapsulating component.
271 * since this is more to do with diagnosing physical hardware
272 * issues than guest compatibility, we choose location codes/DRC
273 * names that adhere to the documented format, but avoid encoding
274 * the entire topology information into the label/code, instead
275 * just using the location codes based on the labels for the
276 * endpoints (VIO/PCI adaptor connectors), which is basically just
277 * "C" followed by an integer ID.
279 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
280 * location codes as documented by PAPR+ v2.7, 12.3.1.5
282 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
286 * dr-entity-sense sensor value
287 * returned via get-sensor-state RTAS calls
288 * as expected by state diagram in PAPR+ 2.7, 13.4
289 * based on the current allocation/indicator/power states
290 * for the DR connector.
292 static SpaprDREntitySense physical_entity_sense(SpaprDrc *drc)
294 /* this assumes all PCI devices are assigned to a 'live insertion'
295 * power domain, where QEMU manages power state automatically as
296 * opposed to the guest. present, non-PCI resources are unaffected
297 * by power state.
299 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
300 : SPAPR_DR_ENTITY_SENSE_EMPTY;
303 static SpaprDREntitySense logical_entity_sense(SpaprDrc *drc)
305 switch (drc->state) {
306 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
307 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
308 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
309 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
310 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
311 g_assert(drc->dev);
312 return SPAPR_DR_ENTITY_SENSE_PRESENT;
313 default:
314 g_assert_not_reached();
318 static void prop_get_index(Object *obj, Visitor *v, const char *name,
319 void *opaque, Error **errp)
321 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
322 uint32_t value = spapr_drc_index(drc);
323 visit_type_uint32(v, name, &value, errp);
326 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
327 void *opaque, Error **errp)
329 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
330 QNull *null = NULL;
331 int fdt_offset_next, fdt_offset, fdt_depth;
332 void *fdt;
334 if (!drc->fdt) {
335 visit_type_null(v, NULL, &null, errp);
336 qobject_unref(null);
337 return;
340 fdt = drc->fdt;
341 fdt_offset = drc->fdt_start_offset;
342 fdt_depth = 0;
344 do {
345 const char *name = NULL;
346 const struct fdt_property *prop = NULL;
347 int prop_len = 0, name_len = 0;
348 uint32_t tag;
349 bool ok;
351 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
352 switch (tag) {
353 case FDT_BEGIN_NODE:
354 fdt_depth++;
355 name = fdt_get_name(fdt, fdt_offset, &name_len);
356 if (!visit_start_struct(v, name, NULL, 0, errp)) {
357 return;
359 break;
360 case FDT_END_NODE:
361 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
362 g_assert(fdt_depth > 0);
363 ok = visit_check_struct(v, errp);
364 visit_end_struct(v, NULL);
365 if (!ok) {
366 return;
368 fdt_depth--;
369 break;
370 case FDT_PROP: {
371 int i;
372 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
373 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
374 if (!visit_start_list(v, name, NULL, 0, errp)) {
375 return;
377 for (i = 0; i < prop_len; i++) {
378 if (!visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i],
379 errp)) {
380 return;
383 ok = visit_check_list(v, errp);
384 visit_end_list(v, NULL);
385 if (!ok) {
386 return;
388 break;
390 default:
391 error_report("device FDT in unexpected state: %d", tag);
392 abort();
394 fdt_offset = fdt_offset_next;
395 } while (fdt_depth != 0);
398 void spapr_drc_attach(SpaprDrc *drc, DeviceState *d)
400 trace_spapr_drc_attach(spapr_drc_index(drc));
402 g_assert(!drc->dev);
403 g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
404 || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
406 drc->dev = d;
408 object_property_add_link(OBJECT(drc), "device",
409 object_get_typename(OBJECT(drc->dev)),
410 (Object **)(&drc->dev),
411 NULL, 0);
414 void spapr_drc_unplug_request(SpaprDrc *drc)
416 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
418 trace_spapr_drc_unplug_request(spapr_drc_index(drc));
420 g_assert(drc->dev);
422 drc->unplug_requested = true;
424 if (drc->state != drck->empty_state) {
425 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
426 return;
429 spapr_drc_release(drc);
432 bool spapr_drc_reset(SpaprDrc *drc)
434 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
435 bool unplug_completed = false;
437 trace_spapr_drc_reset(spapr_drc_index(drc));
439 /* immediately upon reset we can safely assume DRCs whose devices
440 * are pending removal can be safely removed.
442 if (drc->unplug_requested) {
443 spapr_drc_release(drc);
444 unplug_completed = true;
447 if (drc->dev) {
448 /* A device present at reset is ready to go, same as coldplugged */
449 drc->state = drck->ready_state;
451 * Ensure that we are able to send the FDT fragment again
452 * via configure-connector call if the guest requests.
454 drc->ccs_offset = drc->fdt_start_offset;
455 drc->ccs_depth = 0;
456 } else {
457 drc->state = drck->empty_state;
458 drc->ccs_offset = -1;
459 drc->ccs_depth = -1;
462 return unplug_completed;
465 static bool spapr_drc_unplug_requested_needed(void *opaque)
467 return spapr_drc_unplug_requested(opaque);
470 static const VMStateDescription vmstate_spapr_drc_unplug_requested = {
471 .name = "spapr_drc/unplug_requested",
472 .version_id = 1,
473 .minimum_version_id = 1,
474 .needed = spapr_drc_unplug_requested_needed,
475 .fields = (VMStateField []) {
476 VMSTATE_BOOL(unplug_requested, SpaprDrc),
477 VMSTATE_END_OF_LIST()
481 static bool spapr_drc_needed(void *opaque)
483 SpaprDrc *drc = opaque;
484 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
487 * If no dev is plugged in there is no need to migrate the DRC state
488 * nor to reset the DRC at CAS.
490 if (!drc->dev) {
491 return false;
495 * We need to reset the DRC at CAS or to migrate the DRC state if it's
496 * not equal to the expected long-term state, which is the same as the
497 * coldplugged initial state, or if an unplug request is pending.
499 return drc->state != drck->ready_state ||
500 spapr_drc_unplug_requested(drc);
503 static const VMStateDescription vmstate_spapr_drc = {
504 .name = "spapr_drc",
505 .version_id = 1,
506 .minimum_version_id = 1,
507 .needed = spapr_drc_needed,
508 .fields = (VMStateField []) {
509 VMSTATE_UINT32(state, SpaprDrc),
510 VMSTATE_END_OF_LIST()
512 .subsections = (const VMStateDescription * []) {
513 &vmstate_spapr_drc_unplug_requested,
514 NULL
518 static void drc_realize(DeviceState *d, Error **errp)
520 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
521 g_autofree gchar *link_name = g_strdup_printf("%x", spapr_drc_index(drc));
522 Object *root_container;
523 const char *child_name;
525 trace_spapr_drc_realize(spapr_drc_index(drc));
526 /* NOTE: we do this as part of realize/unrealize due to the fact
527 * that the guest will communicate with the DRC via RTAS calls
528 * referencing the global DRC index. By unlinking the DRC
529 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
530 * inaccessible by the guest, since lookups rely on this path
531 * existing in the composition tree
533 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
534 child_name = object_get_canonical_path_component(OBJECT(drc));
535 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
536 object_property_add_alias(root_container, link_name,
537 drc->owner, child_name);
538 vmstate_register(VMSTATE_IF(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
539 drc);
540 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
543 static void drc_unrealize(DeviceState *d)
545 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
546 g_autofree gchar *name = g_strdup_printf("%x", spapr_drc_index(drc));
547 Object *root_container;
549 trace_spapr_drc_unrealize(spapr_drc_index(drc));
550 vmstate_unregister(VMSTATE_IF(drc), &vmstate_spapr_drc, drc);
551 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
552 object_property_del(root_container, name);
555 SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
556 uint32_t id)
558 SpaprDrc *drc = SPAPR_DR_CONNECTOR(object_new(type));
559 g_autofree char *prop_name = NULL;
561 drc->id = id;
562 drc->owner = owner;
563 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
564 spapr_drc_index(drc));
565 object_property_add_child(owner, prop_name, OBJECT(drc));
566 object_unref(OBJECT(drc));
567 qdev_realize(DEVICE(drc), NULL, NULL);
569 return drc;
572 static void spapr_dr_connector_instance_init(Object *obj)
574 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
575 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
577 object_property_add_uint32_ptr(obj, "id", &drc->id, OBJ_PROP_FLAG_READ);
578 object_property_add(obj, "index", "uint32", prop_get_index,
579 NULL, NULL, NULL);
580 object_property_add(obj, "fdt", "struct", prop_get_fdt,
581 NULL, NULL, NULL);
582 drc->state = drck->empty_state;
585 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
587 DeviceClass *dk = DEVICE_CLASS(k);
589 dk->realize = drc_realize;
590 dk->unrealize = drc_unrealize;
592 * Reason: DR connector needs to be wired to either the machine or to a
593 * PHB in spapr_dr_connector_new().
595 dk->user_creatable = false;
598 static bool drc_physical_needed(void *opaque)
600 SpaprDrcPhysical *drcp = (SpaprDrcPhysical *)opaque;
601 SpaprDrc *drc = SPAPR_DR_CONNECTOR(drcp);
603 if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
604 || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
605 return false;
607 return true;
610 static const VMStateDescription vmstate_spapr_drc_physical = {
611 .name = "spapr_drc/physical",
612 .version_id = 1,
613 .minimum_version_id = 1,
614 .needed = drc_physical_needed,
615 .fields = (VMStateField []) {
616 VMSTATE_UINT32(dr_indicator, SpaprDrcPhysical),
617 VMSTATE_END_OF_LIST()
621 static void drc_physical_reset(void *opaque)
623 SpaprDrc *drc = SPAPR_DR_CONNECTOR(opaque);
624 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
626 if (drc->dev) {
627 drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
628 } else {
629 drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
633 static void realize_physical(DeviceState *d, Error **errp)
635 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
636 Error *local_err = NULL;
638 drc_realize(d, &local_err);
639 if (local_err) {
640 error_propagate(errp, local_err);
641 return;
644 vmstate_register(VMSTATE_IF(drcp),
645 spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
646 &vmstate_spapr_drc_physical, drcp);
647 qemu_register_reset(drc_physical_reset, drcp);
650 static void unrealize_physical(DeviceState *d)
652 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
654 drc_unrealize(d);
655 vmstate_unregister(VMSTATE_IF(drcp), &vmstate_spapr_drc_physical, drcp);
656 qemu_unregister_reset(drc_physical_reset, drcp);
659 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
661 DeviceClass *dk = DEVICE_CLASS(k);
662 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
664 dk->realize = realize_physical;
665 dk->unrealize = unrealize_physical;
666 drck->dr_entity_sense = physical_entity_sense;
667 drck->isolate = drc_isolate_physical;
668 drck->unisolate = drc_unisolate_physical;
669 drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
670 drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
673 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
675 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
677 drck->dr_entity_sense = logical_entity_sense;
678 drck->isolate = drc_isolate_logical;
679 drck->unisolate = drc_unisolate_logical;
680 drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
681 drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
684 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
686 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
688 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
689 drck->typename = "CPU";
690 drck->drc_name_prefix = "CPU ";
691 drck->release = spapr_core_release;
692 drck->dt_populate = spapr_core_dt_populate;
695 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
697 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
699 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
700 drck->typename = "28";
701 drck->drc_name_prefix = "C";
702 drck->release = spapr_phb_remove_pci_device_cb;
703 drck->dt_populate = spapr_pci_dt_populate;
706 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
708 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
710 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
711 drck->typename = "MEM";
712 drck->drc_name_prefix = "LMB ";
713 drck->release = spapr_lmb_release;
714 drck->dt_populate = spapr_lmb_dt_populate;
717 static void spapr_drc_phb_class_init(ObjectClass *k, void *data)
719 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
721 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB;
722 drck->typename = "PHB";
723 drck->drc_name_prefix = "PHB ";
724 drck->release = spapr_phb_release;
725 drck->dt_populate = spapr_phb_dt_populate;
728 static void spapr_drc_pmem_class_init(ObjectClass *k, void *data)
730 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
732 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM;
733 drck->typename = "PMEM";
734 drck->drc_name_prefix = "PMEM ";
735 drck->release = NULL;
736 drck->dt_populate = spapr_pmem_dt_populate;
739 static const TypeInfo spapr_dr_connector_info = {
740 .name = TYPE_SPAPR_DR_CONNECTOR,
741 .parent = TYPE_DEVICE,
742 .instance_size = sizeof(SpaprDrc),
743 .instance_init = spapr_dr_connector_instance_init,
744 .class_size = sizeof(SpaprDrcClass),
745 .class_init = spapr_dr_connector_class_init,
746 .abstract = true,
749 static const TypeInfo spapr_drc_physical_info = {
750 .name = TYPE_SPAPR_DRC_PHYSICAL,
751 .parent = TYPE_SPAPR_DR_CONNECTOR,
752 .instance_size = sizeof(SpaprDrcPhysical),
753 .class_init = spapr_drc_physical_class_init,
754 .abstract = true,
757 static const TypeInfo spapr_drc_logical_info = {
758 .name = TYPE_SPAPR_DRC_LOGICAL,
759 .parent = TYPE_SPAPR_DR_CONNECTOR,
760 .class_init = spapr_drc_logical_class_init,
761 .abstract = true,
764 static const TypeInfo spapr_drc_cpu_info = {
765 .name = TYPE_SPAPR_DRC_CPU,
766 .parent = TYPE_SPAPR_DRC_LOGICAL,
767 .class_init = spapr_drc_cpu_class_init,
770 static const TypeInfo spapr_drc_pci_info = {
771 .name = TYPE_SPAPR_DRC_PCI,
772 .parent = TYPE_SPAPR_DRC_PHYSICAL,
773 .class_init = spapr_drc_pci_class_init,
776 static const TypeInfo spapr_drc_lmb_info = {
777 .name = TYPE_SPAPR_DRC_LMB,
778 .parent = TYPE_SPAPR_DRC_LOGICAL,
779 .class_init = spapr_drc_lmb_class_init,
782 static const TypeInfo spapr_drc_phb_info = {
783 .name = TYPE_SPAPR_DRC_PHB,
784 .parent = TYPE_SPAPR_DRC_LOGICAL,
785 .instance_size = sizeof(SpaprDrc),
786 .class_init = spapr_drc_phb_class_init,
789 static const TypeInfo spapr_drc_pmem_info = {
790 .name = TYPE_SPAPR_DRC_PMEM,
791 .parent = TYPE_SPAPR_DRC_LOGICAL,
792 .class_init = spapr_drc_pmem_class_init,
795 /* helper functions for external users */
797 SpaprDrc *spapr_drc_by_index(uint32_t index)
799 Object *obj;
800 g_autofree gchar *name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH,
801 index);
802 obj = object_resolve_path(name, NULL);
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 g_autoptr(GArray) drc_indexes = g_array_new(false, true,
837 sizeof(uint32_t));
838 g_autoptr(GArray) drc_power_domains = g_array_new(false, true,
839 sizeof(uint32_t));
840 g_autoptr(GString) drc_names = g_string_set_size(g_string_new(NULL),
841 sizeof(uint32_t));
842 g_autoptr(GString) drc_types = g_string_set_size(g_string_new(NULL),
843 sizeof(uint32_t));
844 int ret;
847 * This should really be only called once per node since it overwrites
848 * the OF properties if they already exist.
850 g_assert(!fdt_get_property(fdt, offset, "ibm,drc-indexes", NULL));
852 /* the first entry of each properties is a 32-bit integer encoding
853 * the number of elements in the array. we won't know this until
854 * we complete the iteration through all the matching DRCs, but
855 * reserve the space now and set the offsets accordingly so we
856 * can fill them in later.
858 drc_indexes = g_array_set_size(drc_indexes, 1);
859 drc_power_domains = g_array_set_size(drc_power_domains, 1);
861 /* aliases for all DRConnector objects will be rooted in QOM
862 * composition tree at DRC_CONTAINER_PATH
864 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
866 object_property_iter_init(&iter, root_container);
867 while ((prop = object_property_iter_next(&iter))) {
868 Object *obj;
869 SpaprDrc *drc;
870 SpaprDrcClass *drck;
871 g_autofree char *drc_name = NULL;
872 uint32_t drc_index, drc_power_domain;
874 if (!strstart(prop->type, "link<", NULL)) {
875 continue;
878 obj = object_property_get_link(root_container, prop->name,
879 &error_abort);
880 drc = SPAPR_DR_CONNECTOR(obj);
881 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
883 if (owner && (drc->owner != owner)) {
884 continue;
887 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
888 continue;
891 drc_count++;
893 /* ibm,drc-indexes */
894 drc_index = cpu_to_be32(spapr_drc_index(drc));
895 g_array_append_val(drc_indexes, drc_index);
897 /* ibm,drc-power-domains */
898 drc_power_domain = cpu_to_be32(-1);
899 g_array_append_val(drc_power_domains, drc_power_domain);
901 /* ibm,drc-names */
902 drc_name = spapr_drc_name(drc);
903 drc_names = g_string_append(drc_names, drc_name);
904 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
906 /* ibm,drc-types */
907 drc_types = g_string_append(drc_types, drck->typename);
908 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
911 /* now write the drc count into the space we reserved at the
912 * beginning of the arrays previously
914 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
915 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
916 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
917 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
919 ret = fdt_setprop(fdt, offset, "ibm,drc-indexes",
920 drc_indexes->data,
921 drc_indexes->len * sizeof(uint32_t));
922 if (ret) {
923 error_report("Couldn't create ibm,drc-indexes property");
924 return ret;
927 ret = fdt_setprop(fdt, offset, "ibm,drc-power-domains",
928 drc_power_domains->data,
929 drc_power_domains->len * sizeof(uint32_t));
930 if (ret) {
931 error_report("Couldn't finalize ibm,drc-power-domains property");
932 return ret;
935 ret = fdt_setprop(fdt, offset, "ibm,drc-names",
936 drc_names->str, drc_names->len);
937 if (ret) {
938 error_report("Couldn't finalize ibm,drc-names property");
939 return ret;
942 ret = fdt_setprop(fdt, offset, "ibm,drc-types",
943 drc_types->str, drc_types->len);
944 if (ret) {
945 error_report("Couldn't finalize ibm,drc-types property");
948 return ret;
951 void spapr_drc_reset_all(SpaprMachineState *spapr)
953 Object *drc_container;
954 ObjectProperty *prop;
955 ObjectPropertyIterator iter;
957 drc_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
958 restart:
959 object_property_iter_init(&iter, drc_container);
960 while ((prop = object_property_iter_next(&iter))) {
961 SpaprDrc *drc;
963 if (!strstart(prop->type, "link<", NULL)) {
964 continue;
966 drc = SPAPR_DR_CONNECTOR(object_property_get_link(drc_container,
967 prop->name,
968 &error_abort));
971 * This will complete any pending plug/unplug requests.
972 * In case of a unplugged PHB or PCI bridge, this will
973 * cause some DRCs to be destroyed and thus potentially
974 * invalidate the iterator.
976 if (spapr_drc_reset(drc)) {
977 goto restart;
983 * RTAS calls
986 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
988 SpaprDrc *drc = spapr_drc_by_index(idx);
989 SpaprDrcClass *drck;
991 if (!drc) {
992 return RTAS_OUT_NO_SUCH_INDICATOR;
995 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
997 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
999 switch (state) {
1000 case SPAPR_DR_ISOLATION_STATE_ISOLATED:
1001 return drck->isolate(drc);
1003 case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
1004 return drck->unisolate(drc);
1006 default:
1007 return RTAS_OUT_PARAM_ERROR;
1011 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
1013 SpaprDrc *drc = spapr_drc_by_index(idx);
1015 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
1016 return RTAS_OUT_NO_SUCH_INDICATOR;
1019 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
1021 switch (state) {
1022 case SPAPR_DR_ALLOCATION_STATE_USABLE:
1023 return drc_set_usable(drc);
1025 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
1026 return drc_set_unusable(drc);
1028 default:
1029 return RTAS_OUT_PARAM_ERROR;
1033 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
1035 SpaprDrc *drc = spapr_drc_by_index(idx);
1037 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
1038 return RTAS_OUT_NO_SUCH_INDICATOR;
1040 if ((state != SPAPR_DR_INDICATOR_INACTIVE)
1041 && (state != SPAPR_DR_INDICATOR_ACTIVE)
1042 && (state != SPAPR_DR_INDICATOR_IDENTIFY)
1043 && (state != SPAPR_DR_INDICATOR_ACTION)) {
1044 return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
1047 trace_spapr_drc_set_dr_indicator(idx, state);
1048 SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
1049 return RTAS_OUT_SUCCESS;
1052 static void rtas_set_indicator(PowerPCCPU *cpu, SpaprMachineState *spapr,
1053 uint32_t token,
1054 uint32_t nargs, target_ulong args,
1055 uint32_t nret, target_ulong rets)
1057 uint32_t type, idx, state;
1058 uint32_t ret = RTAS_OUT_SUCCESS;
1060 if (nargs != 3 || nret != 1) {
1061 ret = RTAS_OUT_PARAM_ERROR;
1062 goto out;
1065 type = rtas_ld(args, 0);
1066 idx = rtas_ld(args, 1);
1067 state = rtas_ld(args, 2);
1069 switch (type) {
1070 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1071 ret = rtas_set_isolation_state(idx, state);
1072 break;
1073 case RTAS_SENSOR_TYPE_DR:
1074 ret = rtas_set_dr_indicator(idx, state);
1075 break;
1076 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1077 ret = rtas_set_allocation_state(idx, state);
1078 break;
1079 default:
1080 ret = RTAS_OUT_NOT_SUPPORTED;
1083 out:
1084 rtas_st(rets, 0, ret);
1087 static void rtas_get_sensor_state(PowerPCCPU *cpu, SpaprMachineState *spapr,
1088 uint32_t token, uint32_t nargs,
1089 target_ulong args, uint32_t nret,
1090 target_ulong rets)
1092 uint32_t sensor_type;
1093 uint32_t sensor_index;
1094 uint32_t sensor_state = 0;
1095 SpaprDrc *drc;
1096 SpaprDrcClass *drck;
1097 uint32_t ret = RTAS_OUT_SUCCESS;
1099 if (nargs != 2 || nret != 2) {
1100 ret = RTAS_OUT_PARAM_ERROR;
1101 goto out;
1104 sensor_type = rtas_ld(args, 0);
1105 sensor_index = rtas_ld(args, 1);
1107 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1108 /* currently only DR-related sensors are implemented */
1109 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1110 sensor_type);
1111 ret = RTAS_OUT_NOT_SUPPORTED;
1112 goto out;
1115 drc = spapr_drc_by_index(sensor_index);
1116 if (!drc) {
1117 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1118 ret = RTAS_OUT_PARAM_ERROR;
1119 goto out;
1121 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1122 sensor_state = drck->dr_entity_sense(drc);
1124 out:
1125 rtas_st(rets, 0, ret);
1126 rtas_st(rets, 1, sensor_state);
1129 /* configure-connector work area offsets, int32_t units for field
1130 * indexes, bytes for field offset/len values.
1132 * as documented by PAPR+ v2.7, 13.5.3.5
1134 #define CC_IDX_NODE_NAME_OFFSET 2
1135 #define CC_IDX_PROP_NAME_OFFSET 2
1136 #define CC_IDX_PROP_LEN 3
1137 #define CC_IDX_PROP_DATA_OFFSET 4
1138 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1139 #define CC_WA_LEN 4096
1141 static void configure_connector_st(target_ulong addr, target_ulong offset,
1142 const void *buf, size_t len)
1144 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1145 buf, MIN(len, CC_WA_LEN - offset));
1148 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1149 SpaprMachineState *spapr,
1150 uint32_t token, uint32_t nargs,
1151 target_ulong args, uint32_t nret,
1152 target_ulong rets)
1154 uint64_t wa_addr;
1155 uint64_t wa_offset;
1156 uint32_t drc_index;
1157 SpaprDrc *drc;
1158 SpaprDrcClass *drck;
1159 SpaprDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1160 int rc;
1162 if (nargs != 2 || nret != 1) {
1163 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1164 return;
1167 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1169 drc_index = rtas_ld(wa_addr, 0);
1170 drc = spapr_drc_by_index(drc_index);
1171 if (!drc) {
1172 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1173 rc = RTAS_OUT_PARAM_ERROR;
1174 goto out;
1177 if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
1178 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
1179 && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
1180 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
1182 * Need to unisolate the device before configuring
1183 * or it should already be in configured state to
1184 * allow configure-connector be called repeatedly.
1186 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1187 goto out;
1190 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1193 * This indicates that the kernel is reconfiguring a LMB due to
1194 * a failed hotunplug. Rollback the DIMM unplug process.
1196 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB &&
1197 drc->unplug_requested) {
1198 spapr_memory_unplug_rollback(spapr, drc->dev);
1201 if (!drc->fdt) {
1202 void *fdt;
1203 int fdt_size;
1205 fdt = create_device_tree(&fdt_size);
1207 if (drck->dt_populate(drc, spapr, fdt, &drc->fdt_start_offset,
1208 NULL)) {
1209 g_free(fdt);
1210 rc = SPAPR_DR_CC_RESPONSE_ERROR;
1211 goto out;
1214 drc->fdt = fdt;
1215 drc->ccs_offset = drc->fdt_start_offset;
1216 drc->ccs_depth = 0;
1219 do {
1220 uint32_t tag;
1221 const char *name;
1222 const struct fdt_property *prop;
1223 int fdt_offset_next, prop_len;
1225 tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
1227 switch (tag) {
1228 case FDT_BEGIN_NODE:
1229 drc->ccs_depth++;
1230 name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
1232 /* provide the name of the next OF node */
1233 wa_offset = CC_VAL_DATA_OFFSET;
1234 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1235 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1236 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1237 break;
1238 case FDT_END_NODE:
1239 drc->ccs_depth--;
1240 if (drc->ccs_depth == 0) {
1241 uint32_t drc_index = spapr_drc_index(drc);
1243 /* done sending the device tree, move to configured state */
1244 trace_spapr_drc_set_configured(drc_index);
1245 drc->state = drck->ready_state;
1247 * Ensure that we are able to send the FDT fragment
1248 * again via configure-connector call if the guest requests.
1250 drc->ccs_offset = drc->fdt_start_offset;
1251 drc->ccs_depth = 0;
1252 fdt_offset_next = drc->fdt_start_offset;
1253 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1254 } else {
1255 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1257 break;
1258 case FDT_PROP:
1259 prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
1260 &prop_len);
1261 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1263 /* provide the name of the next OF property */
1264 wa_offset = CC_VAL_DATA_OFFSET;
1265 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1266 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1268 /* provide the length and value of the OF property. data gets
1269 * placed immediately after NULL terminator of the OF property's
1270 * name string
1272 wa_offset += strlen(name) + 1,
1273 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1274 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1275 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1276 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1277 break;
1278 case FDT_END:
1279 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1280 default:
1281 /* keep seeking for an actionable tag */
1282 break;
1284 if (drc->ccs_offset >= 0) {
1285 drc->ccs_offset = fdt_offset_next;
1287 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1289 rc = resp;
1290 out:
1291 rtas_st(rets, 0, rc);
1294 static void spapr_drc_register_types(void)
1296 type_register_static(&spapr_dr_connector_info);
1297 type_register_static(&spapr_drc_physical_info);
1298 type_register_static(&spapr_drc_logical_info);
1299 type_register_static(&spapr_drc_cpu_info);
1300 type_register_static(&spapr_drc_pci_info);
1301 type_register_static(&spapr_drc_lmb_info);
1302 type_register_static(&spapr_drc_phb_info);
1303 type_register_static(&spapr_drc_pmem_info);
1305 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1306 rtas_set_indicator);
1307 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1308 rtas_get_sensor_state);
1309 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1310 rtas_ibm_configure_connector);
1312 type_init(spapr_drc_register_types)