spapr_drc.c: handle hotunplug errors in drc_unisolate_logical()
[qemu.git] / hw / ppc / spapr_drc.c
blob6918e0c9d1487c67826942a59e2f414b6d1e656c
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 void spapr_drc_release(SpaprDrc *drc)
55 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
57 drck->release(drc->dev);
59 drc->unplug_requested = false;
60 g_free(drc->fdt);
61 drc->fdt = NULL;
62 drc->fdt_start_offset = 0;
63 object_property_del(OBJECT(drc), "device");
64 drc->dev = NULL;
67 static uint32_t drc_isolate_physical(SpaprDrc *drc)
69 switch (drc->state) {
70 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
71 return RTAS_OUT_SUCCESS; /* Nothing to do */
72 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
73 break; /* see below */
74 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
75 return RTAS_OUT_PARAM_ERROR; /* not allowed */
76 default:
77 g_assert_not_reached();
80 drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
82 if (drc->unplug_requested) {
83 uint32_t drc_index = spapr_drc_index(drc);
84 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
85 spapr_drc_release(drc);
88 return RTAS_OUT_SUCCESS;
91 static uint32_t drc_unisolate_physical(SpaprDrc *drc)
93 switch (drc->state) {
94 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
95 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
96 return RTAS_OUT_SUCCESS; /* Nothing to do */
97 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
98 break; /* see below */
99 default:
100 g_assert_not_reached();
103 /* cannot unisolate a non-existent resource, and, or resources
104 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
105 * 13.5.3.5)
107 if (!drc->dev) {
108 return RTAS_OUT_NO_SUCH_INDICATOR;
111 drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE;
112 drc->ccs_offset = drc->fdt_start_offset;
113 drc->ccs_depth = 0;
115 return RTAS_OUT_SUCCESS;
118 static uint32_t drc_isolate_logical(SpaprDrc *drc)
120 switch (drc->state) {
121 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
122 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
123 return RTAS_OUT_SUCCESS; /* Nothing to do */
124 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
125 break; /* see below */
126 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
127 return RTAS_OUT_PARAM_ERROR; /* not allowed */
128 default:
129 g_assert_not_reached();
133 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
134 * belong to a DIMM device that is marked for removal.
136 * Currently the guest userspace tool drmgr that drives the memory
137 * hotplug/unplug will just try to remove a set of 'removable' LMBs
138 * in response to a hot unplug request that is based on drc-count.
139 * If the LMB being removed doesn't belong to a DIMM device that is
140 * actually being unplugged, fail the isolation request here.
142 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
143 && !drc->unplug_requested) {
144 return RTAS_OUT_HW_ERROR;
147 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
149 return RTAS_OUT_SUCCESS;
152 static uint32_t drc_unisolate_logical(SpaprDrc *drc)
154 SpaprMachineState *spapr = NULL;
156 switch (drc->state) {
157 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
158 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
160 * Unisolating a logical DRC that was marked for unplug
161 * means that the kernel is refusing the removal.
163 if (drc->unplug_requested && drc->dev) {
164 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
165 spapr = SPAPR_MACHINE(qdev_get_machine());
167 spapr_memory_unplug_rollback(spapr, drc->dev);
170 drc->unplug_requested = false;
171 error_report("Device hotunplug rejected by the guest "
172 "for device %s", drc->dev->id);
175 * TODO: send a QAPI DEVICE_UNPLUG_ERROR event when
176 * it is implemented.
180 return RTAS_OUT_SUCCESS; /* Nothing to do */
181 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
182 break; /* see below */
183 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
184 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
185 default:
186 g_assert_not_reached();
189 /* Move to AVAILABLE state should have ensured device was present */
190 g_assert(drc->dev);
192 drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
193 drc->ccs_offset = drc->fdt_start_offset;
194 drc->ccs_depth = 0;
196 return RTAS_OUT_SUCCESS;
199 static uint32_t drc_set_usable(SpaprDrc *drc)
201 switch (drc->state) {
202 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
203 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
204 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
205 return RTAS_OUT_SUCCESS; /* Nothing to do */
206 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
207 break; /* see below */
208 default:
209 g_assert_not_reached();
212 /* if there's no resource/device associated with the DRC, there's
213 * no way for us to put it in an allocation state consistent with
214 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
215 * result in an RTAS return code of -3 / "no such indicator"
217 if (!drc->dev) {
218 return RTAS_OUT_NO_SUCH_INDICATOR;
220 if (drc->unplug_requested) {
221 /* Don't allow the guest to move a device away from UNUSABLE
222 * state when we want to unplug it */
223 return RTAS_OUT_NO_SUCH_INDICATOR;
226 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
228 return RTAS_OUT_SUCCESS;
231 static uint32_t drc_set_unusable(SpaprDrc *drc)
233 switch (drc->state) {
234 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
235 return RTAS_OUT_SUCCESS; /* Nothing to do */
236 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
237 break; /* see below */
238 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
239 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
240 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
241 default:
242 g_assert_not_reached();
245 drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
246 if (drc->unplug_requested) {
247 uint32_t drc_index = spapr_drc_index(drc);
248 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
249 spapr_drc_release(drc);
252 return RTAS_OUT_SUCCESS;
255 static char *spapr_drc_name(SpaprDrc *drc)
257 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
259 /* human-readable name for a DRC to encode into the DT
260 * description. this is mainly only used within a guest in place
261 * of the unique DRC index.
263 * in the case of VIO/PCI devices, it corresponds to a "location
264 * code" that maps a logical device/function (DRC index) to a
265 * physical (or virtual in the case of VIO) location in the system
266 * by chaining together the "location label" for each
267 * encapsulating component.
269 * since this is more to do with diagnosing physical hardware
270 * issues than guest compatibility, we choose location codes/DRC
271 * names that adhere to the documented format, but avoid encoding
272 * the entire topology information into the label/code, instead
273 * just using the location codes based on the labels for the
274 * endpoints (VIO/PCI adaptor connectors), which is basically just
275 * "C" followed by an integer ID.
277 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
278 * location codes as documented by PAPR+ v2.7, 12.3.1.5
280 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
284 * dr-entity-sense sensor value
285 * returned via get-sensor-state RTAS calls
286 * as expected by state diagram in PAPR+ 2.7, 13.4
287 * based on the current allocation/indicator/power states
288 * for the DR connector.
290 static SpaprDREntitySense physical_entity_sense(SpaprDrc *drc)
292 /* this assumes all PCI devices are assigned to a 'live insertion'
293 * power domain, where QEMU manages power state automatically as
294 * opposed to the guest. present, non-PCI resources are unaffected
295 * by power state.
297 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
298 : SPAPR_DR_ENTITY_SENSE_EMPTY;
301 static SpaprDREntitySense logical_entity_sense(SpaprDrc *drc)
303 switch (drc->state) {
304 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
305 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
306 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
307 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
308 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
309 g_assert(drc->dev);
310 return SPAPR_DR_ENTITY_SENSE_PRESENT;
311 default:
312 g_assert_not_reached();
316 static void prop_get_index(Object *obj, Visitor *v, const char *name,
317 void *opaque, Error **errp)
319 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
320 uint32_t value = spapr_drc_index(drc);
321 visit_type_uint32(v, name, &value, errp);
324 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
325 void *opaque, Error **errp)
327 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
328 QNull *null = NULL;
329 int fdt_offset_next, fdt_offset, fdt_depth;
330 void *fdt;
332 if (!drc->fdt) {
333 visit_type_null(v, NULL, &null, errp);
334 qobject_unref(null);
335 return;
338 fdt = drc->fdt;
339 fdt_offset = drc->fdt_start_offset;
340 fdt_depth = 0;
342 do {
343 const char *name = NULL;
344 const struct fdt_property *prop = NULL;
345 int prop_len = 0, name_len = 0;
346 uint32_t tag;
347 bool ok;
349 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
350 switch (tag) {
351 case FDT_BEGIN_NODE:
352 fdt_depth++;
353 name = fdt_get_name(fdt, fdt_offset, &name_len);
354 if (!visit_start_struct(v, name, NULL, 0, errp)) {
355 return;
357 break;
358 case FDT_END_NODE:
359 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
360 g_assert(fdt_depth > 0);
361 ok = visit_check_struct(v, errp);
362 visit_end_struct(v, NULL);
363 if (!ok) {
364 return;
366 fdt_depth--;
367 break;
368 case FDT_PROP: {
369 int i;
370 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
371 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
372 if (!visit_start_list(v, name, NULL, 0, errp)) {
373 return;
375 for (i = 0; i < prop_len; i++) {
376 if (!visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i],
377 errp)) {
378 return;
381 ok = visit_check_list(v, errp);
382 visit_end_list(v, NULL);
383 if (!ok) {
384 return;
386 break;
388 default:
389 error_report("device FDT in unexpected state: %d", tag);
390 abort();
392 fdt_offset = fdt_offset_next;
393 } while (fdt_depth != 0);
396 void spapr_drc_attach(SpaprDrc *drc, DeviceState *d)
398 trace_spapr_drc_attach(spapr_drc_index(drc));
400 g_assert(!drc->dev);
401 g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
402 || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
404 drc->dev = d;
406 object_property_add_link(OBJECT(drc), "device",
407 object_get_typename(OBJECT(drc->dev)),
408 (Object **)(&drc->dev),
409 NULL, 0);
412 void spapr_drc_unplug_request(SpaprDrc *drc)
414 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
416 trace_spapr_drc_unplug_request(spapr_drc_index(drc));
418 g_assert(drc->dev);
420 drc->unplug_requested = true;
422 if (drc->state != drck->empty_state) {
423 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
424 return;
427 spapr_drc_release(drc);
430 bool spapr_drc_reset(SpaprDrc *drc)
432 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
433 bool unplug_completed = false;
435 trace_spapr_drc_reset(spapr_drc_index(drc));
437 /* immediately upon reset we can safely assume DRCs whose devices
438 * are pending removal can be safely removed.
440 if (drc->unplug_requested) {
441 spapr_drc_release(drc);
442 unplug_completed = true;
445 if (drc->dev) {
446 /* A device present at reset is ready to go, same as coldplugged */
447 drc->state = drck->ready_state;
449 * Ensure that we are able to send the FDT fragment again
450 * via configure-connector call if the guest requests.
452 drc->ccs_offset = drc->fdt_start_offset;
453 drc->ccs_depth = 0;
454 } else {
455 drc->state = drck->empty_state;
456 drc->ccs_offset = -1;
457 drc->ccs_depth = -1;
460 return unplug_completed;
463 static bool spapr_drc_unplug_requested_needed(void *opaque)
465 return spapr_drc_unplug_requested(opaque);
468 static const VMStateDescription vmstate_spapr_drc_unplug_requested = {
469 .name = "spapr_drc/unplug_requested",
470 .version_id = 1,
471 .minimum_version_id = 1,
472 .needed = spapr_drc_unplug_requested_needed,
473 .fields = (VMStateField []) {
474 VMSTATE_BOOL(unplug_requested, SpaprDrc),
475 VMSTATE_END_OF_LIST()
479 static bool spapr_drc_needed(void *opaque)
481 SpaprDrc *drc = opaque;
482 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
485 * If no dev is plugged in there is no need to migrate the DRC state
486 * nor to reset the DRC at CAS.
488 if (!drc->dev) {
489 return false;
493 * We need to reset the DRC at CAS or to migrate the DRC state if it's
494 * not equal to the expected long-term state, which is the same as the
495 * coldplugged initial state, or if an unplug request is pending.
497 return drc->state != drck->ready_state ||
498 spapr_drc_unplug_requested(drc);
501 static const VMStateDescription vmstate_spapr_drc = {
502 .name = "spapr_drc",
503 .version_id = 1,
504 .minimum_version_id = 1,
505 .needed = spapr_drc_needed,
506 .fields = (VMStateField []) {
507 VMSTATE_UINT32(state, SpaprDrc),
508 VMSTATE_END_OF_LIST()
510 .subsections = (const VMStateDescription * []) {
511 &vmstate_spapr_drc_unplug_requested,
512 NULL
516 static void drc_realize(DeviceState *d, Error **errp)
518 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
519 Object *root_container;
520 gchar *link_name;
521 const char *child_name;
523 trace_spapr_drc_realize(spapr_drc_index(drc));
524 /* NOTE: we do this as part of realize/unrealize due to the fact
525 * that the guest will communicate with the DRC via RTAS calls
526 * referencing the global DRC index. By unlinking the DRC
527 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
528 * inaccessible by the guest, since lookups rely on this path
529 * existing in the composition tree
531 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
532 link_name = g_strdup_printf("%x", spapr_drc_index(drc));
533 child_name = object_get_canonical_path_component(OBJECT(drc));
534 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
535 object_property_add_alias(root_container, link_name,
536 drc->owner, child_name);
537 g_free(link_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 Object *root_container;
547 gchar *name;
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 name = g_strdup_printf("%x", spapr_drc_index(drc));
553 object_property_del(root_container, name);
554 g_free(name);
557 SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
558 uint32_t id)
560 SpaprDrc *drc = SPAPR_DR_CONNECTOR(object_new(type));
561 char *prop_name;
563 drc->id = id;
564 drc->owner = owner;
565 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
566 spapr_drc_index(drc));
567 object_property_add_child(owner, prop_name, OBJECT(drc));
568 object_unref(OBJECT(drc));
569 qdev_realize(DEVICE(drc), NULL, NULL);
570 g_free(prop_name);
572 return drc;
575 static void spapr_dr_connector_instance_init(Object *obj)
577 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
578 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
580 object_property_add_uint32_ptr(obj, "id", &drc->id, OBJ_PROP_FLAG_READ);
581 object_property_add(obj, "index", "uint32", prop_get_index,
582 NULL, NULL, NULL);
583 object_property_add(obj, "fdt", "struct", prop_get_fdt,
584 NULL, NULL, NULL);
585 drc->state = drck->empty_state;
588 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
590 DeviceClass *dk = DEVICE_CLASS(k);
592 dk->realize = drc_realize;
593 dk->unrealize = drc_unrealize;
595 * Reason: DR connector needs to be wired to either the machine or to a
596 * PHB in spapr_dr_connector_new().
598 dk->user_creatable = false;
601 static bool drc_physical_needed(void *opaque)
603 SpaprDrcPhysical *drcp = (SpaprDrcPhysical *)opaque;
604 SpaprDrc *drc = SPAPR_DR_CONNECTOR(drcp);
606 if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
607 || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
608 return false;
610 return true;
613 static const VMStateDescription vmstate_spapr_drc_physical = {
614 .name = "spapr_drc/physical",
615 .version_id = 1,
616 .minimum_version_id = 1,
617 .needed = drc_physical_needed,
618 .fields = (VMStateField []) {
619 VMSTATE_UINT32(dr_indicator, SpaprDrcPhysical),
620 VMSTATE_END_OF_LIST()
624 static void drc_physical_reset(void *opaque)
626 SpaprDrc *drc = SPAPR_DR_CONNECTOR(opaque);
627 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
629 if (drc->dev) {
630 drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
631 } else {
632 drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
636 static void realize_physical(DeviceState *d, Error **errp)
638 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
639 Error *local_err = NULL;
641 drc_realize(d, &local_err);
642 if (local_err) {
643 error_propagate(errp, local_err);
644 return;
647 vmstate_register(VMSTATE_IF(drcp),
648 spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
649 &vmstate_spapr_drc_physical, drcp);
650 qemu_register_reset(drc_physical_reset, drcp);
653 static void unrealize_physical(DeviceState *d)
655 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
657 drc_unrealize(d);
658 vmstate_unregister(VMSTATE_IF(drcp), &vmstate_spapr_drc_physical, drcp);
659 qemu_unregister_reset(drc_physical_reset, drcp);
662 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
664 DeviceClass *dk = DEVICE_CLASS(k);
665 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
667 dk->realize = realize_physical;
668 dk->unrealize = unrealize_physical;
669 drck->dr_entity_sense = physical_entity_sense;
670 drck->isolate = drc_isolate_physical;
671 drck->unisolate = drc_unisolate_physical;
672 drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
673 drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
676 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
678 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
680 drck->dr_entity_sense = logical_entity_sense;
681 drck->isolate = drc_isolate_logical;
682 drck->unisolate = drc_unisolate_logical;
683 drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
684 drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
687 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
689 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
691 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
692 drck->typename = "CPU";
693 drck->drc_name_prefix = "CPU ";
694 drck->release = spapr_core_release;
695 drck->dt_populate = spapr_core_dt_populate;
698 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
700 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
702 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
703 drck->typename = "28";
704 drck->drc_name_prefix = "C";
705 drck->release = spapr_phb_remove_pci_device_cb;
706 drck->dt_populate = spapr_pci_dt_populate;
709 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
711 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
713 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
714 drck->typename = "MEM";
715 drck->drc_name_prefix = "LMB ";
716 drck->release = spapr_lmb_release;
717 drck->dt_populate = spapr_lmb_dt_populate;
720 static void spapr_drc_phb_class_init(ObjectClass *k, void *data)
722 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
724 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB;
725 drck->typename = "PHB";
726 drck->drc_name_prefix = "PHB ";
727 drck->release = spapr_phb_release;
728 drck->dt_populate = spapr_phb_dt_populate;
731 static void spapr_drc_pmem_class_init(ObjectClass *k, void *data)
733 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
735 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM;
736 drck->typename = "PMEM";
737 drck->drc_name_prefix = "PMEM ";
738 drck->release = NULL;
739 drck->dt_populate = spapr_pmem_dt_populate;
742 static const TypeInfo spapr_dr_connector_info = {
743 .name = TYPE_SPAPR_DR_CONNECTOR,
744 .parent = TYPE_DEVICE,
745 .instance_size = sizeof(SpaprDrc),
746 .instance_init = spapr_dr_connector_instance_init,
747 .class_size = sizeof(SpaprDrcClass),
748 .class_init = spapr_dr_connector_class_init,
749 .abstract = true,
752 static const TypeInfo spapr_drc_physical_info = {
753 .name = TYPE_SPAPR_DRC_PHYSICAL,
754 .parent = TYPE_SPAPR_DR_CONNECTOR,
755 .instance_size = sizeof(SpaprDrcPhysical),
756 .class_init = spapr_drc_physical_class_init,
757 .abstract = true,
760 static const TypeInfo spapr_drc_logical_info = {
761 .name = TYPE_SPAPR_DRC_LOGICAL,
762 .parent = TYPE_SPAPR_DR_CONNECTOR,
763 .class_init = spapr_drc_logical_class_init,
764 .abstract = true,
767 static const TypeInfo spapr_drc_cpu_info = {
768 .name = TYPE_SPAPR_DRC_CPU,
769 .parent = TYPE_SPAPR_DRC_LOGICAL,
770 .class_init = spapr_drc_cpu_class_init,
773 static const TypeInfo spapr_drc_pci_info = {
774 .name = TYPE_SPAPR_DRC_PCI,
775 .parent = TYPE_SPAPR_DRC_PHYSICAL,
776 .class_init = spapr_drc_pci_class_init,
779 static const TypeInfo spapr_drc_lmb_info = {
780 .name = TYPE_SPAPR_DRC_LMB,
781 .parent = TYPE_SPAPR_DRC_LOGICAL,
782 .class_init = spapr_drc_lmb_class_init,
785 static const TypeInfo spapr_drc_phb_info = {
786 .name = TYPE_SPAPR_DRC_PHB,
787 .parent = TYPE_SPAPR_DRC_LOGICAL,
788 .instance_size = sizeof(SpaprDrc),
789 .class_init = spapr_drc_phb_class_init,
792 static const TypeInfo spapr_drc_pmem_info = {
793 .name = TYPE_SPAPR_DRC_PMEM,
794 .parent = TYPE_SPAPR_DRC_LOGICAL,
795 .class_init = spapr_drc_pmem_class_init,
798 /* helper functions for external users */
800 SpaprDrc *spapr_drc_by_index(uint32_t index)
802 Object *obj;
803 gchar *name;
805 name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH, index);
806 obj = object_resolve_path(name, NULL);
807 g_free(name);
809 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
812 SpaprDrc *spapr_drc_by_id(const char *type, uint32_t id)
814 SpaprDrcClass *drck
815 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
817 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
818 | (id & DRC_INDEX_ID_MASK));
822 * spapr_dt_drc
824 * @fdt: libfdt device tree
825 * @path: path in the DT to generate properties
826 * @owner: parent Object/DeviceState for which to generate DRC
827 * descriptions for
828 * @drc_type_mask: mask of SpaprDrcType values corresponding
829 * to the types of DRCs to generate entries for
831 * generate OF properties to describe DRC topology/indices to guests
833 * as documented in PAPR+ v2.1, 13.5.2
835 int spapr_dt_drc(void *fdt, int offset, Object *owner, uint32_t drc_type_mask)
837 Object *root_container;
838 ObjectProperty *prop;
839 ObjectPropertyIterator iter;
840 uint32_t drc_count = 0;
841 GArray *drc_indexes, *drc_power_domains;
842 GString *drc_names, *drc_types;
843 int ret;
846 * This should really be only called once per node since it overwrites
847 * the OF properties if they already exist.
849 g_assert(!fdt_get_property(fdt, offset, "ibm,drc-indexes", NULL));
851 /* the first entry of each properties is a 32-bit integer encoding
852 * the number of elements in the array. we won't know this until
853 * we complete the iteration through all the matching DRCs, but
854 * reserve the space now and set the offsets accordingly so we
855 * can fill them in later.
857 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
858 drc_indexes = g_array_set_size(drc_indexes, 1);
859 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
860 drc_power_domains = g_array_set_size(drc_power_domains, 1);
861 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
862 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
864 /* aliases for all DRConnector objects will be rooted in QOM
865 * composition tree at DRC_CONTAINER_PATH
867 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
869 object_property_iter_init(&iter, root_container);
870 while ((prop = object_property_iter_next(&iter))) {
871 Object *obj;
872 SpaprDrc *drc;
873 SpaprDrcClass *drck;
874 char *drc_name = NULL;
875 uint32_t drc_index, drc_power_domain;
877 if (!strstart(prop->type, "link<", NULL)) {
878 continue;
881 obj = object_property_get_link(root_container, prop->name,
882 &error_abort);
883 drc = SPAPR_DR_CONNECTOR(obj);
884 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
886 if (owner && (drc->owner != owner)) {
887 continue;
890 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
891 continue;
894 drc_count++;
896 /* ibm,drc-indexes */
897 drc_index = cpu_to_be32(spapr_drc_index(drc));
898 g_array_append_val(drc_indexes, drc_index);
900 /* ibm,drc-power-domains */
901 drc_power_domain = cpu_to_be32(-1);
902 g_array_append_val(drc_power_domains, drc_power_domain);
904 /* ibm,drc-names */
905 drc_name = spapr_drc_name(drc);
906 drc_names = g_string_append(drc_names, drc_name);
907 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
908 g_free(drc_name);
910 /* ibm,drc-types */
911 drc_types = g_string_append(drc_types, drck->typename);
912 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
915 /* now write the drc count into the space we reserved at the
916 * beginning of the arrays previously
918 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
919 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
920 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
921 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
923 ret = fdt_setprop(fdt, offset, "ibm,drc-indexes",
924 drc_indexes->data,
925 drc_indexes->len * sizeof(uint32_t));
926 if (ret) {
927 error_report("Couldn't create ibm,drc-indexes property");
928 goto out;
931 ret = fdt_setprop(fdt, offset, "ibm,drc-power-domains",
932 drc_power_domains->data,
933 drc_power_domains->len * sizeof(uint32_t));
934 if (ret) {
935 error_report("Couldn't finalize ibm,drc-power-domains property");
936 goto out;
939 ret = fdt_setprop(fdt, offset, "ibm,drc-names",
940 drc_names->str, drc_names->len);
941 if (ret) {
942 error_report("Couldn't finalize ibm,drc-names property");
943 goto out;
946 ret = fdt_setprop(fdt, offset, "ibm,drc-types",
947 drc_types->str, drc_types->len);
948 if (ret) {
949 error_report("Couldn't finalize ibm,drc-types property");
950 goto out;
953 out:
954 g_array_free(drc_indexes, true);
955 g_array_free(drc_power_domains, true);
956 g_string_free(drc_names, true);
957 g_string_free(drc_types, true);
959 return ret;
962 void spapr_drc_reset_all(SpaprMachineState *spapr)
964 Object *drc_container;
965 ObjectProperty *prop;
966 ObjectPropertyIterator iter;
968 drc_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
969 restart:
970 object_property_iter_init(&iter, drc_container);
971 while ((prop = object_property_iter_next(&iter))) {
972 SpaprDrc *drc;
974 if (!strstart(prop->type, "link<", NULL)) {
975 continue;
977 drc = SPAPR_DR_CONNECTOR(object_property_get_link(drc_container,
978 prop->name,
979 &error_abort));
982 * This will complete any pending plug/unplug requests.
983 * In case of a unplugged PHB or PCI bridge, this will
984 * cause some DRCs to be destroyed and thus potentially
985 * invalidate the iterator.
987 if (spapr_drc_reset(drc)) {
988 goto restart;
994 * RTAS calls
997 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
999 SpaprDrc *drc = spapr_drc_by_index(idx);
1000 SpaprDrcClass *drck;
1002 if (!drc) {
1003 return RTAS_OUT_NO_SUCH_INDICATOR;
1006 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
1008 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1010 switch (state) {
1011 case SPAPR_DR_ISOLATION_STATE_ISOLATED:
1012 return drck->isolate(drc);
1014 case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
1015 return drck->unisolate(drc);
1017 default:
1018 return RTAS_OUT_PARAM_ERROR;
1022 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
1024 SpaprDrc *drc = spapr_drc_by_index(idx);
1026 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
1027 return RTAS_OUT_NO_SUCH_INDICATOR;
1030 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
1032 switch (state) {
1033 case SPAPR_DR_ALLOCATION_STATE_USABLE:
1034 return drc_set_usable(drc);
1036 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
1037 return drc_set_unusable(drc);
1039 default:
1040 return RTAS_OUT_PARAM_ERROR;
1044 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
1046 SpaprDrc *drc = spapr_drc_by_index(idx);
1048 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
1049 return RTAS_OUT_NO_SUCH_INDICATOR;
1051 if ((state != SPAPR_DR_INDICATOR_INACTIVE)
1052 && (state != SPAPR_DR_INDICATOR_ACTIVE)
1053 && (state != SPAPR_DR_INDICATOR_IDENTIFY)
1054 && (state != SPAPR_DR_INDICATOR_ACTION)) {
1055 return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
1058 trace_spapr_drc_set_dr_indicator(idx, state);
1059 SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
1060 return RTAS_OUT_SUCCESS;
1063 static void rtas_set_indicator(PowerPCCPU *cpu, SpaprMachineState *spapr,
1064 uint32_t token,
1065 uint32_t nargs, target_ulong args,
1066 uint32_t nret, target_ulong rets)
1068 uint32_t type, idx, state;
1069 uint32_t ret = RTAS_OUT_SUCCESS;
1071 if (nargs != 3 || nret != 1) {
1072 ret = RTAS_OUT_PARAM_ERROR;
1073 goto out;
1076 type = rtas_ld(args, 0);
1077 idx = rtas_ld(args, 1);
1078 state = rtas_ld(args, 2);
1080 switch (type) {
1081 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1082 ret = rtas_set_isolation_state(idx, state);
1083 break;
1084 case RTAS_SENSOR_TYPE_DR:
1085 ret = rtas_set_dr_indicator(idx, state);
1086 break;
1087 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1088 ret = rtas_set_allocation_state(idx, state);
1089 break;
1090 default:
1091 ret = RTAS_OUT_NOT_SUPPORTED;
1094 out:
1095 rtas_st(rets, 0, ret);
1098 static void rtas_get_sensor_state(PowerPCCPU *cpu, SpaprMachineState *spapr,
1099 uint32_t token, uint32_t nargs,
1100 target_ulong args, uint32_t nret,
1101 target_ulong rets)
1103 uint32_t sensor_type;
1104 uint32_t sensor_index;
1105 uint32_t sensor_state = 0;
1106 SpaprDrc *drc;
1107 SpaprDrcClass *drck;
1108 uint32_t ret = RTAS_OUT_SUCCESS;
1110 if (nargs != 2 || nret != 2) {
1111 ret = RTAS_OUT_PARAM_ERROR;
1112 goto out;
1115 sensor_type = rtas_ld(args, 0);
1116 sensor_index = rtas_ld(args, 1);
1118 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1119 /* currently only DR-related sensors are implemented */
1120 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1121 sensor_type);
1122 ret = RTAS_OUT_NOT_SUPPORTED;
1123 goto out;
1126 drc = spapr_drc_by_index(sensor_index);
1127 if (!drc) {
1128 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1129 ret = RTAS_OUT_PARAM_ERROR;
1130 goto out;
1132 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1133 sensor_state = drck->dr_entity_sense(drc);
1135 out:
1136 rtas_st(rets, 0, ret);
1137 rtas_st(rets, 1, sensor_state);
1140 /* configure-connector work area offsets, int32_t units for field
1141 * indexes, bytes for field offset/len values.
1143 * as documented by PAPR+ v2.7, 13.5.3.5
1145 #define CC_IDX_NODE_NAME_OFFSET 2
1146 #define CC_IDX_PROP_NAME_OFFSET 2
1147 #define CC_IDX_PROP_LEN 3
1148 #define CC_IDX_PROP_DATA_OFFSET 4
1149 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1150 #define CC_WA_LEN 4096
1152 static void configure_connector_st(target_ulong addr, target_ulong offset,
1153 const void *buf, size_t len)
1155 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1156 buf, MIN(len, CC_WA_LEN - offset));
1159 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1160 SpaprMachineState *spapr,
1161 uint32_t token, uint32_t nargs,
1162 target_ulong args, uint32_t nret,
1163 target_ulong rets)
1165 uint64_t wa_addr;
1166 uint64_t wa_offset;
1167 uint32_t drc_index;
1168 SpaprDrc *drc;
1169 SpaprDrcClass *drck;
1170 SpaprDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1171 int rc;
1173 if (nargs != 2 || nret != 1) {
1174 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1175 return;
1178 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1180 drc_index = rtas_ld(wa_addr, 0);
1181 drc = spapr_drc_by_index(drc_index);
1182 if (!drc) {
1183 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1184 rc = RTAS_OUT_PARAM_ERROR;
1185 goto out;
1188 if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
1189 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
1190 && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
1191 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
1193 * Need to unisolate the device before configuring
1194 * or it should already be in configured state to
1195 * allow configure-connector be called repeatedly.
1197 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1198 goto out;
1201 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1204 * This indicates that the kernel is reconfiguring a LMB due to
1205 * a failed hotunplug. Rollback the DIMM unplug process.
1207 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB &&
1208 drc->unplug_requested) {
1209 spapr_memory_unplug_rollback(spapr, drc->dev);
1212 if (!drc->fdt) {
1213 void *fdt;
1214 int fdt_size;
1216 fdt = create_device_tree(&fdt_size);
1218 if (drck->dt_populate(drc, spapr, fdt, &drc->fdt_start_offset,
1219 NULL)) {
1220 g_free(fdt);
1221 rc = SPAPR_DR_CC_RESPONSE_ERROR;
1222 goto out;
1225 drc->fdt = fdt;
1226 drc->ccs_offset = drc->fdt_start_offset;
1227 drc->ccs_depth = 0;
1230 do {
1231 uint32_t tag;
1232 const char *name;
1233 const struct fdt_property *prop;
1234 int fdt_offset_next, prop_len;
1236 tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
1238 switch (tag) {
1239 case FDT_BEGIN_NODE:
1240 drc->ccs_depth++;
1241 name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
1243 /* provide the name of the next OF node */
1244 wa_offset = CC_VAL_DATA_OFFSET;
1245 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1246 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1247 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1248 break;
1249 case FDT_END_NODE:
1250 drc->ccs_depth--;
1251 if (drc->ccs_depth == 0) {
1252 uint32_t drc_index = spapr_drc_index(drc);
1254 /* done sending the device tree, move to configured state */
1255 trace_spapr_drc_set_configured(drc_index);
1256 drc->state = drck->ready_state;
1258 * Ensure that we are able to send the FDT fragment
1259 * again via configure-connector call if the guest requests.
1261 drc->ccs_offset = drc->fdt_start_offset;
1262 drc->ccs_depth = 0;
1263 fdt_offset_next = drc->fdt_start_offset;
1264 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1265 } else {
1266 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1268 break;
1269 case FDT_PROP:
1270 prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
1271 &prop_len);
1272 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1274 /* provide the name of the next OF property */
1275 wa_offset = CC_VAL_DATA_OFFSET;
1276 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1277 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1279 /* provide the length and value of the OF property. data gets
1280 * placed immediately after NULL terminator of the OF property's
1281 * name string
1283 wa_offset += strlen(name) + 1,
1284 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1285 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1286 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1287 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1288 break;
1289 case FDT_END:
1290 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1291 default:
1292 /* keep seeking for an actionable tag */
1293 break;
1295 if (drc->ccs_offset >= 0) {
1296 drc->ccs_offset = fdt_offset_next;
1298 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1300 rc = resp;
1301 out:
1302 rtas_st(rets, 0, rc);
1305 static void spapr_drc_register_types(void)
1307 type_register_static(&spapr_dr_connector_info);
1308 type_register_static(&spapr_drc_physical_info);
1309 type_register_static(&spapr_drc_logical_info);
1310 type_register_static(&spapr_drc_cpu_info);
1311 type_register_static(&spapr_drc_pci_info);
1312 type_register_static(&spapr_drc_lmb_info);
1313 type_register_static(&spapr_drc_phb_info);
1314 type_register_static(&spapr_drc_pmem_info);
1316 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1317 rtas_set_indicator);
1318 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1319 rtas_get_sensor_state);
1320 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1321 rtas_ibm_configure_connector);
1323 type_init(spapr_drc_register_types)