Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / ppc / spapr_drc.c
blob1484e3209d9ab45555d4ac41d67c990b540c946e
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/qapi-events-qdev.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;
172 if (drc->dev->id) {
173 error_report("Device hotunplug rejected by the guest "
174 "for device %s", drc->dev->id);
177 qapi_event_send_device_unplug_guest_error(drc->dev->id,
178 drc->dev->canonical_path);
181 return RTAS_OUT_SUCCESS; /* Nothing to do */
182 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
183 break; /* see below */
184 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
185 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
186 default:
187 g_assert_not_reached();
190 /* Move to AVAILABLE state should have ensured device was present */
191 g_assert(drc->dev);
193 drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
194 drc->ccs_offset = drc->fdt_start_offset;
195 drc->ccs_depth = 0;
197 return RTAS_OUT_SUCCESS;
200 static uint32_t drc_set_usable(SpaprDrc *drc)
202 switch (drc->state) {
203 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
204 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
205 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
206 return RTAS_OUT_SUCCESS; /* Nothing to do */
207 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
208 break; /* see below */
209 default:
210 g_assert_not_reached();
213 /* if there's no resource/device associated with the DRC, there's
214 * no way for us to put it in an allocation state consistent with
215 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
216 * result in an RTAS return code of -3 / "no such indicator"
218 if (!drc->dev) {
219 return RTAS_OUT_NO_SUCH_INDICATOR;
221 if (drc->unplug_requested) {
222 /* Don't allow the guest to move a device away from UNUSABLE
223 * state when we want to unplug it */
224 return RTAS_OUT_NO_SUCH_INDICATOR;
227 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
229 return RTAS_OUT_SUCCESS;
232 static uint32_t drc_set_unusable(SpaprDrc *drc)
234 switch (drc->state) {
235 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
236 return RTAS_OUT_SUCCESS; /* Nothing to do */
237 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
238 break; /* see below */
239 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
240 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
241 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
242 default:
243 g_assert_not_reached();
246 drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
247 if (drc->unplug_requested) {
248 uint32_t drc_index = spapr_drc_index(drc);
249 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
250 spapr_drc_release(drc);
253 return RTAS_OUT_SUCCESS;
256 static char *spapr_drc_name(SpaprDrc *drc)
258 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
260 /* human-readable name for a DRC to encode into the DT
261 * description. this is mainly only used within a guest in place
262 * of the unique DRC index.
264 * in the case of VIO/PCI devices, it corresponds to a "location
265 * code" that maps a logical device/function (DRC index) to a
266 * physical (or virtual in the case of VIO) location in the system
267 * by chaining together the "location label" for each
268 * encapsulating component.
270 * since this is more to do with diagnosing physical hardware
271 * issues than guest compatibility, we choose location codes/DRC
272 * names that adhere to the documented format, but avoid encoding
273 * the entire topology information into the label/code, instead
274 * just using the location codes based on the labels for the
275 * endpoints (VIO/PCI adaptor connectors), which is basically just
276 * "C" followed by an integer ID.
278 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
279 * location codes as documented by PAPR+ v2.7, 12.3.1.5
281 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
285 * dr-entity-sense sensor value
286 * returned via get-sensor-state RTAS calls
287 * as expected by state diagram in PAPR+ 2.7, 13.4
288 * based on the current allocation/indicator/power states
289 * for the DR connector.
291 static SpaprDREntitySense physical_entity_sense(SpaprDrc *drc)
293 /* this assumes all PCI devices are assigned to a 'live insertion'
294 * power domain, where QEMU manages power state automatically as
295 * opposed to the guest. present, non-PCI resources are unaffected
296 * by power state.
298 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
299 : SPAPR_DR_ENTITY_SENSE_EMPTY;
302 static SpaprDREntitySense logical_entity_sense(SpaprDrc *drc)
304 switch (drc->state) {
305 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
306 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
307 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
308 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
309 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
310 g_assert(drc->dev);
311 return SPAPR_DR_ENTITY_SENSE_PRESENT;
312 default:
313 g_assert_not_reached();
317 static void prop_get_index(Object *obj, Visitor *v, const char *name,
318 void *opaque, Error **errp)
320 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
321 uint32_t value = spapr_drc_index(drc);
322 visit_type_uint32(v, name, &value, errp);
325 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
326 void *opaque, Error **errp)
328 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
329 QNull *null = NULL;
330 int fdt_offset_next, fdt_offset, fdt_depth;
331 void *fdt;
333 if (!drc->fdt) {
334 visit_type_null(v, NULL, &null, errp);
335 qobject_unref(null);
336 return;
339 fdt = drc->fdt;
340 fdt_offset = drc->fdt_start_offset;
341 fdt_depth = 0;
343 do {
344 const char *dt_name = NULL;
345 const struct fdt_property *prop = NULL;
346 int prop_len = 0, name_len = 0;
347 uint32_t tag;
348 bool ok;
350 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
351 switch (tag) {
352 case FDT_BEGIN_NODE:
353 fdt_depth++;
354 dt_name = fdt_get_name(fdt, fdt_offset, &name_len);
355 if (!visit_start_struct(v, dt_name, NULL, 0, errp)) {
356 return;
358 break;
359 case FDT_END_NODE:
360 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
361 g_assert(fdt_depth > 0);
362 ok = visit_check_struct(v, errp);
363 visit_end_struct(v, NULL);
364 if (!ok) {
365 return;
367 fdt_depth--;
368 break;
369 case FDT_PROP: {
370 int i;
371 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
372 dt_name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
373 if (!visit_start_list(v, dt_name, NULL, 0, errp)) {
374 return;
376 for (i = 0; i < prop_len; i++) {
377 if (!visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i],
378 errp)) {
379 return;
382 ok = visit_check_list(v, errp);
383 visit_end_list(v, NULL);
384 if (!ok) {
385 return;
387 break;
389 default:
390 error_report("device FDT in unexpected state: %d", tag);
391 abort();
393 fdt_offset = fdt_offset_next;
394 } while (fdt_depth != 0);
397 void spapr_drc_attach(SpaprDrc *drc, DeviceState *d)
399 trace_spapr_drc_attach(spapr_drc_index(drc));
401 g_assert(!drc->dev);
402 g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
403 || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
405 drc->dev = d;
407 object_property_add_link(OBJECT(drc), "device",
408 object_get_typename(OBJECT(drc->dev)),
409 (Object **)(&drc->dev),
410 NULL, 0);
413 void spapr_drc_unplug_request(SpaprDrc *drc)
415 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
417 trace_spapr_drc_unplug_request(spapr_drc_index(drc));
419 g_assert(drc->dev);
421 drc->unplug_requested = true;
423 if (drc->state != drck->empty_state) {
424 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
425 return;
428 spapr_drc_release(drc);
431 bool spapr_drc_reset(SpaprDrc *drc)
433 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
434 bool unplug_completed = false;
436 trace_spapr_drc_reset(spapr_drc_index(drc));
438 /* immediately upon reset we can safely assume DRCs whose devices
439 * are pending removal can be safely removed.
441 if (drc->unplug_requested) {
442 spapr_drc_release(drc);
443 unplug_completed = true;
446 if (drc->dev) {
447 /* A device present at reset is ready to go, same as coldplugged */
448 drc->state = drck->ready_state;
450 * Ensure that we are able to send the FDT fragment again
451 * via configure-connector call if the guest requests.
453 drc->ccs_offset = drc->fdt_start_offset;
454 drc->ccs_depth = 0;
455 } else {
456 drc->state = drck->empty_state;
457 drc->ccs_offset = -1;
458 drc->ccs_depth = -1;
461 return unplug_completed;
464 static bool spapr_drc_unplug_requested_needed(void *opaque)
466 return spapr_drc_unplug_requested(opaque);
469 static const VMStateDescription vmstate_spapr_drc_unplug_requested = {
470 .name = "spapr_drc/unplug_requested",
471 .version_id = 1,
472 .minimum_version_id = 1,
473 .needed = spapr_drc_unplug_requested_needed,
474 .fields = (const VMStateField []) {
475 VMSTATE_BOOL(unplug_requested, SpaprDrc),
476 VMSTATE_END_OF_LIST()
480 static bool spapr_drc_needed(void *opaque)
482 SpaprDrc *drc = opaque;
483 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
486 * If no dev is plugged in there is no need to migrate the DRC state
487 * nor to reset the DRC at CAS.
489 if (!drc->dev) {
490 return false;
494 * We need to reset the DRC at CAS or to migrate the DRC state if it's
495 * not equal to the expected long-term state, which is the same as the
496 * coldplugged initial state, or if an unplug request is pending.
498 return drc->state != drck->ready_state ||
499 spapr_drc_unplug_requested(drc);
502 static const VMStateDescription vmstate_spapr_drc = {
503 .name = "spapr_drc",
504 .version_id = 1,
505 .minimum_version_id = 1,
506 .needed = spapr_drc_needed,
507 .fields = (const VMStateField []) {
508 VMSTATE_UINT32(state, SpaprDrc),
509 VMSTATE_END_OF_LIST()
511 .subsections = (const VMStateDescription * const []) {
512 &vmstate_spapr_drc_unplug_requested,
513 NULL
517 static void drc_realize(DeviceState *d, Error **errp)
519 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
520 g_autofree gchar *link_name = g_strdup_printf("%x", spapr_drc_index(drc));
521 Object *root_container;
522 const char *child_name;
524 trace_spapr_drc_realize(spapr_drc_index(drc));
525 /* NOTE: we do this as part of realize/unrealize due to the fact
526 * that the guest will communicate with the DRC via RTAS calls
527 * referencing the global DRC index. By unlinking the DRC
528 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
529 * inaccessible by the guest, since lookups rely on this path
530 * existing in the composition tree
532 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
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 vmstate_register(VMSTATE_IF(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
538 drc);
539 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
542 static void drc_unrealize(DeviceState *d)
544 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
545 g_autofree gchar *name = g_strdup_printf("%x", spapr_drc_index(drc));
546 Object *root_container;
548 trace_spapr_drc_unrealize(spapr_drc_index(drc));
549 vmstate_unregister(VMSTATE_IF(drc), &vmstate_spapr_drc, drc);
550 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
551 object_property_del(root_container, name);
554 SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
555 uint32_t id)
557 SpaprDrc *drc = SPAPR_DR_CONNECTOR(object_new(type));
558 g_autofree char *prop_name = NULL;
560 drc->id = id;
561 drc->owner = owner;
562 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
563 spapr_drc_index(drc));
564 object_property_add_child(owner, prop_name, OBJECT(drc));
565 object_unref(OBJECT(drc));
566 qdev_realize(DEVICE(drc), NULL, NULL);
568 return drc;
571 static void spapr_dr_connector_instance_init(Object *obj)
573 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
574 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
576 object_property_add_uint32_ptr(obj, "id", &drc->id, OBJ_PROP_FLAG_READ);
577 object_property_add(obj, "index", "uint32", prop_get_index,
578 NULL, NULL, NULL);
579 object_property_add(obj, "fdt", "struct", prop_get_fdt,
580 NULL, NULL, NULL);
581 drc->state = drck->empty_state;
584 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
586 DeviceClass *dk = DEVICE_CLASS(k);
588 dk->realize = drc_realize;
589 dk->unrealize = drc_unrealize;
591 * Reason: DR connector needs to be wired to either the machine or to a
592 * PHB in spapr_dr_connector_new().
594 dk->user_creatable = false;
597 static bool drc_physical_needed(void *opaque)
599 SpaprDrcPhysical *drcp = (SpaprDrcPhysical *)opaque;
600 SpaprDrc *drc = SPAPR_DR_CONNECTOR(drcp);
602 if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
603 || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
604 return false;
606 return true;
609 static const VMStateDescription vmstate_spapr_drc_physical = {
610 .name = "spapr_drc/physical",
611 .version_id = 1,
612 .minimum_version_id = 1,
613 .needed = drc_physical_needed,
614 .fields = (const VMStateField []) {
615 VMSTATE_UINT32(dr_indicator, SpaprDrcPhysical),
616 VMSTATE_END_OF_LIST()
620 static void drc_physical_reset(void *opaque)
622 SpaprDrc *drc = SPAPR_DR_CONNECTOR(opaque);
623 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
625 if (drc->dev) {
626 drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
627 } else {
628 drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
632 static void realize_physical(DeviceState *d, Error **errp)
634 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
635 Error *local_err = NULL;
637 drc_realize(d, &local_err);
638 if (local_err) {
639 error_propagate(errp, local_err);
640 return;
643 vmstate_register(VMSTATE_IF(drcp),
644 spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
645 &vmstate_spapr_drc_physical, drcp);
646 qemu_register_reset(drc_physical_reset, drcp);
649 static void unrealize_physical(DeviceState *d)
651 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
653 drc_unrealize(d);
654 vmstate_unregister(VMSTATE_IF(drcp), &vmstate_spapr_drc_physical, drcp);
655 qemu_unregister_reset(drc_physical_reset, drcp);
658 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
660 DeviceClass *dk = DEVICE_CLASS(k);
661 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
663 dk->realize = realize_physical;
664 dk->unrealize = unrealize_physical;
665 drck->dr_entity_sense = physical_entity_sense;
666 drck->isolate = drc_isolate_physical;
667 drck->unisolate = drc_unisolate_physical;
668 drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
669 drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
672 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
674 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
676 drck->dr_entity_sense = logical_entity_sense;
677 drck->isolate = drc_isolate_logical;
678 drck->unisolate = drc_unisolate_logical;
679 drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
680 drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
683 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
685 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
687 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
688 drck->typename = "CPU";
689 drck->drc_name_prefix = "CPU ";
690 drck->release = spapr_core_release;
691 drck->dt_populate = spapr_core_dt_populate;
694 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
696 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
698 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
699 drck->typename = "28";
700 drck->drc_name_prefix = "C";
701 drck->release = spapr_phb_remove_pci_device_cb;
702 drck->dt_populate = spapr_pci_dt_populate;
705 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
707 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
709 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
710 drck->typename = "MEM";
711 drck->drc_name_prefix = "LMB ";
712 drck->release = spapr_lmb_release;
713 drck->dt_populate = spapr_lmb_dt_populate;
716 static void spapr_drc_phb_class_init(ObjectClass *k, void *data)
718 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
720 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB;
721 drck->typename = "PHB";
722 drck->drc_name_prefix = "PHB ";
723 drck->release = spapr_phb_release;
724 drck->dt_populate = spapr_phb_dt_populate;
727 static void spapr_drc_pmem_class_init(ObjectClass *k, void *data)
729 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
731 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM;
732 drck->typename = "PMEM";
733 drck->drc_name_prefix = "PMEM ";
734 drck->release = NULL;
735 drck->dt_populate = spapr_pmem_dt_populate;
738 static const TypeInfo spapr_dr_connector_info = {
739 .name = TYPE_SPAPR_DR_CONNECTOR,
740 .parent = TYPE_DEVICE,
741 .instance_size = sizeof(SpaprDrc),
742 .instance_init = spapr_dr_connector_instance_init,
743 .class_size = sizeof(SpaprDrcClass),
744 .class_init = spapr_dr_connector_class_init,
745 .abstract = true,
748 static const TypeInfo spapr_drc_physical_info = {
749 .name = TYPE_SPAPR_DRC_PHYSICAL,
750 .parent = TYPE_SPAPR_DR_CONNECTOR,
751 .instance_size = sizeof(SpaprDrcPhysical),
752 .class_init = spapr_drc_physical_class_init,
753 .abstract = true,
756 static const TypeInfo spapr_drc_logical_info = {
757 .name = TYPE_SPAPR_DRC_LOGICAL,
758 .parent = TYPE_SPAPR_DR_CONNECTOR,
759 .class_init = spapr_drc_logical_class_init,
760 .abstract = true,
763 static const TypeInfo spapr_drc_cpu_info = {
764 .name = TYPE_SPAPR_DRC_CPU,
765 .parent = TYPE_SPAPR_DRC_LOGICAL,
766 .class_init = spapr_drc_cpu_class_init,
769 static const TypeInfo spapr_drc_pci_info = {
770 .name = TYPE_SPAPR_DRC_PCI,
771 .parent = TYPE_SPAPR_DRC_PHYSICAL,
772 .class_init = spapr_drc_pci_class_init,
775 static const TypeInfo spapr_drc_lmb_info = {
776 .name = TYPE_SPAPR_DRC_LMB,
777 .parent = TYPE_SPAPR_DRC_LOGICAL,
778 .class_init = spapr_drc_lmb_class_init,
781 static const TypeInfo spapr_drc_phb_info = {
782 .name = TYPE_SPAPR_DRC_PHB,
783 .parent = TYPE_SPAPR_DRC_LOGICAL,
784 .instance_size = sizeof(SpaprDrc),
785 .class_init = spapr_drc_phb_class_init,
788 static const TypeInfo spapr_drc_pmem_info = {
789 .name = TYPE_SPAPR_DRC_PMEM,
790 .parent = TYPE_SPAPR_DRC_LOGICAL,
791 .class_init = spapr_drc_pmem_class_init,
794 /* helper functions for external users */
796 SpaprDrc *spapr_drc_by_index(uint32_t index)
798 Object *obj;
799 g_autofree gchar *name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH,
800 index);
801 obj = object_resolve_path(name, NULL);
803 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
806 SpaprDrc *spapr_drc_by_id(const char *type, uint32_t id)
808 SpaprDrcClass *drck
809 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
811 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
812 | (id & DRC_INDEX_ID_MASK));
816 * spapr_dt_drc
818 * @fdt: libfdt device tree
819 * @path: path in the DT to generate properties
820 * @owner: parent Object/DeviceState for which to generate DRC
821 * descriptions for
822 * @drc_type_mask: mask of SpaprDrcType values corresponding
823 * to the types of DRCs to generate entries for
825 * generate OF properties to describe DRC topology/indices to guests
827 * as documented in PAPR+ v2.1, 13.5.2
829 int spapr_dt_drc(void *fdt, int offset, Object *owner, uint32_t drc_type_mask)
831 Object *root_container;
832 ObjectProperty *prop;
833 ObjectPropertyIterator iter;
834 uint32_t drc_count = 0;
835 g_autoptr(GArray) drc_indexes = g_array_new(false, true,
836 sizeof(uint32_t));
837 g_autoptr(GArray) drc_power_domains = g_array_new(false, true,
838 sizeof(uint32_t));
839 g_autoptr(GString) drc_names = g_string_set_size(g_string_new(NULL),
840 sizeof(uint32_t));
841 g_autoptr(GString) drc_types = g_string_set_size(g_string_new(NULL),
842 sizeof(uint32_t));
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_set_size(drc_indexes, 1);
858 drc_power_domains = g_array_set_size(drc_power_domains, 1);
860 /* aliases for all DRConnector objects will be rooted in QOM
861 * composition tree at DRC_CONTAINER_PATH
863 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
865 object_property_iter_init(&iter, root_container);
866 while ((prop = object_property_iter_next(&iter))) {
867 Object *obj;
868 SpaprDrc *drc;
869 SpaprDrcClass *drck;
870 g_autofree char *drc_name = NULL;
871 uint32_t drc_index, drc_power_domain;
873 if (!strstart(prop->type, "link<", NULL)) {
874 continue;
877 obj = object_property_get_link(root_container, prop->name,
878 &error_abort);
879 drc = SPAPR_DR_CONNECTOR(obj);
880 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
882 if (owner && (drc->owner != owner)) {
883 continue;
886 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
887 continue;
890 drc_count++;
892 /* ibm,drc-indexes */
893 drc_index = cpu_to_be32(spapr_drc_index(drc));
894 g_array_append_val(drc_indexes, drc_index);
896 /* ibm,drc-power-domains */
897 drc_power_domain = cpu_to_be32(-1);
898 g_array_append_val(drc_power_domains, drc_power_domain);
900 /* ibm,drc-names */
901 drc_name = spapr_drc_name(drc);
902 drc_names = g_string_append(drc_names, drc_name);
903 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
905 /* ibm,drc-types */
906 drc_types = g_string_append(drc_types, drck->typename);
907 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
910 /* now write the drc count into the space we reserved at the
911 * beginning of the arrays previously
913 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
914 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
915 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
916 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
918 ret = fdt_setprop(fdt, offset, "ibm,drc-indexes",
919 drc_indexes->data,
920 drc_indexes->len * sizeof(uint32_t));
921 if (ret) {
922 error_report("Couldn't create ibm,drc-indexes property");
923 return ret;
926 ret = fdt_setprop(fdt, offset, "ibm,drc-power-domains",
927 drc_power_domains->data,
928 drc_power_domains->len * sizeof(uint32_t));
929 if (ret) {
930 error_report("Couldn't finalize ibm,drc-power-domains property");
931 return ret;
934 ret = fdt_setprop(fdt, offset, "ibm,drc-names",
935 drc_names->str, drc_names->len);
936 if (ret) {
937 error_report("Couldn't finalize ibm,drc-names property");
938 return ret;
941 ret = fdt_setprop(fdt, offset, "ibm,drc-types",
942 drc_types->str, drc_types->len);
943 if (ret) {
944 error_report("Couldn't finalize ibm,drc-types property");
947 return ret;
950 void spapr_drc_reset_all(SpaprMachineState *spapr)
952 Object *drc_container;
953 ObjectProperty *prop;
954 ObjectPropertyIterator iter;
956 drc_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
957 restart:
958 object_property_iter_init(&iter, drc_container);
959 while ((prop = object_property_iter_next(&iter))) {
960 SpaprDrc *drc;
962 if (!strstart(prop->type, "link<", NULL)) {
963 continue;
965 drc = SPAPR_DR_CONNECTOR(object_property_get_link(drc_container,
966 prop->name,
967 &error_abort));
970 * This will complete any pending plug/unplug requests.
971 * In case of a unplugged PHB or PCI bridge, this will
972 * cause some DRCs to be destroyed and thus potentially
973 * invalidate the iterator.
975 if (spapr_drc_reset(drc)) {
976 goto restart;
982 * RTAS calls
985 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
987 SpaprDrc *drc = spapr_drc_by_index(idx);
988 SpaprDrcClass *drck;
990 if (!drc) {
991 return RTAS_OUT_NO_SUCH_INDICATOR;
994 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
996 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
998 switch (state) {
999 case SPAPR_DR_ISOLATION_STATE_ISOLATED:
1000 return drck->isolate(drc);
1002 case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
1003 return drck->unisolate(drc);
1005 default:
1006 return RTAS_OUT_PARAM_ERROR;
1010 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
1012 SpaprDrc *drc = spapr_drc_by_index(idx);
1014 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
1015 return RTAS_OUT_NO_SUCH_INDICATOR;
1018 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
1020 switch (state) {
1021 case SPAPR_DR_ALLOCATION_STATE_USABLE:
1022 return drc_set_usable(drc);
1024 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
1025 return drc_set_unusable(drc);
1027 default:
1028 return RTAS_OUT_PARAM_ERROR;
1032 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
1034 SpaprDrc *drc = spapr_drc_by_index(idx);
1036 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
1037 return RTAS_OUT_NO_SUCH_INDICATOR;
1039 if ((state != SPAPR_DR_INDICATOR_INACTIVE)
1040 && (state != SPAPR_DR_INDICATOR_ACTIVE)
1041 && (state != SPAPR_DR_INDICATOR_IDENTIFY)
1042 && (state != SPAPR_DR_INDICATOR_ACTION)) {
1043 return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
1046 trace_spapr_drc_set_dr_indicator(idx, state);
1047 SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
1048 return RTAS_OUT_SUCCESS;
1051 static void rtas_set_indicator(PowerPCCPU *cpu, SpaprMachineState *spapr,
1052 uint32_t token,
1053 uint32_t nargs, target_ulong args,
1054 uint32_t nret, target_ulong rets)
1056 uint32_t type, idx, state;
1057 uint32_t ret = RTAS_OUT_SUCCESS;
1059 if (nargs != 3 || nret != 1) {
1060 ret = RTAS_OUT_PARAM_ERROR;
1061 goto out;
1064 type = rtas_ld(args, 0);
1065 idx = rtas_ld(args, 1);
1066 state = rtas_ld(args, 2);
1068 switch (type) {
1069 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1070 ret = rtas_set_isolation_state(idx, state);
1071 break;
1072 case RTAS_SENSOR_TYPE_DR:
1073 ret = rtas_set_dr_indicator(idx, state);
1074 break;
1075 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1076 ret = rtas_set_allocation_state(idx, state);
1077 break;
1078 default:
1079 ret = RTAS_OUT_NOT_SUPPORTED;
1082 out:
1083 rtas_st(rets, 0, ret);
1086 static void rtas_get_sensor_state(PowerPCCPU *cpu, SpaprMachineState *spapr,
1087 uint32_t token, uint32_t nargs,
1088 target_ulong args, uint32_t nret,
1089 target_ulong rets)
1091 uint32_t sensor_type;
1092 uint32_t sensor_index;
1093 uint32_t sensor_state = 0;
1094 SpaprDrc *drc;
1095 SpaprDrcClass *drck;
1096 uint32_t ret = RTAS_OUT_SUCCESS;
1098 if (nargs != 2 || nret != 2) {
1099 ret = RTAS_OUT_PARAM_ERROR;
1100 goto out;
1103 sensor_type = rtas_ld(args, 0);
1104 sensor_index = rtas_ld(args, 1);
1106 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1107 /* currently only DR-related sensors are implemented */
1108 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1109 sensor_type);
1110 ret = RTAS_OUT_NOT_SUPPORTED;
1111 goto out;
1114 drc = spapr_drc_by_index(sensor_index);
1115 if (!drc) {
1116 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1117 ret = RTAS_OUT_PARAM_ERROR;
1118 goto out;
1120 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1121 sensor_state = drck->dr_entity_sense(drc);
1123 out:
1124 rtas_st(rets, 0, ret);
1125 rtas_st(rets, 1, sensor_state);
1128 /* configure-connector work area offsets, int32_t units for field
1129 * indexes, bytes for field offset/len values.
1131 * as documented by PAPR+ v2.7, 13.5.3.5
1133 #define CC_IDX_NODE_NAME_OFFSET 2
1134 #define CC_IDX_PROP_NAME_OFFSET 2
1135 #define CC_IDX_PROP_LEN 3
1136 #define CC_IDX_PROP_DATA_OFFSET 4
1137 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1138 #define CC_WA_LEN 4096
1140 static void configure_connector_st(target_ulong addr, target_ulong offset,
1141 const void *buf, size_t len)
1143 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1144 buf, MIN(len, CC_WA_LEN - offset));
1147 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1148 SpaprMachineState *spapr,
1149 uint32_t token, uint32_t nargs,
1150 target_ulong args, uint32_t nret,
1151 target_ulong rets)
1153 uint64_t wa_addr;
1154 uint64_t wa_offset;
1155 uint32_t drc_index;
1156 SpaprDrc *drc;
1157 SpaprDrcClass *drck;
1158 SpaprDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1159 int rc;
1161 if (nargs != 2 || nret != 1) {
1162 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1163 return;
1166 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1168 drc_index = rtas_ld(wa_addr, 0);
1169 drc = spapr_drc_by_index(drc_index);
1170 if (!drc) {
1171 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1172 rc = RTAS_OUT_PARAM_ERROR;
1173 goto out;
1176 if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
1177 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
1178 && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
1179 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
1181 * Need to unisolate the device before configuring
1182 * or it should already be in configured state to
1183 * allow configure-connector be called repeatedly.
1185 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1186 goto out;
1189 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1192 * This indicates that the kernel is reconfiguring a LMB due to
1193 * a failed hotunplug. Rollback the DIMM unplug process.
1195 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB &&
1196 drc->unplug_requested) {
1197 spapr_memory_unplug_rollback(spapr, drc->dev);
1200 if (!drc->fdt) {
1201 void *fdt;
1202 int fdt_size;
1204 fdt = create_device_tree(&fdt_size);
1206 if (drck->dt_populate(drc, spapr, fdt, &drc->fdt_start_offset,
1207 NULL)) {
1208 g_free(fdt);
1209 rc = SPAPR_DR_CC_RESPONSE_ERROR;
1210 goto out;
1213 drc->fdt = fdt;
1214 drc->ccs_offset = drc->fdt_start_offset;
1215 drc->ccs_depth = 0;
1218 do {
1219 uint32_t tag;
1220 const char *name;
1221 const struct fdt_property *prop;
1222 int fdt_offset_next, prop_len;
1224 tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
1226 switch (tag) {
1227 case FDT_BEGIN_NODE:
1228 drc->ccs_depth++;
1229 name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
1231 /* provide the name of the next OF node */
1232 wa_offset = CC_VAL_DATA_OFFSET;
1233 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1234 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1235 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1236 break;
1237 case FDT_END_NODE:
1238 drc->ccs_depth--;
1239 if (drc->ccs_depth == 0) {
1240 /* done sending the device tree, move to configured state */
1241 trace_spapr_drc_set_configured(drc_index);
1242 drc->state = drck->ready_state;
1244 * Ensure that we are able to send the FDT fragment
1245 * again via configure-connector call if the guest requests.
1247 drc->ccs_offset = drc->fdt_start_offset;
1248 drc->ccs_depth = 0;
1249 fdt_offset_next = drc->fdt_start_offset;
1250 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1251 } else {
1252 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1254 break;
1255 case FDT_PROP:
1256 prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
1257 &prop_len);
1258 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1260 /* provide the name of the next OF property */
1261 wa_offset = CC_VAL_DATA_OFFSET;
1262 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1263 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1265 /* provide the length and value of the OF property. data gets
1266 * placed immediately after NULL terminator of the OF property's
1267 * name string
1269 wa_offset += strlen(name) + 1,
1270 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1271 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1272 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1273 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1274 break;
1275 case FDT_END:
1276 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1277 default:
1278 /* keep seeking for an actionable tag */
1279 break;
1281 if (drc->ccs_offset >= 0) {
1282 drc->ccs_offset = fdt_offset_next;
1284 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1286 rc = resp;
1287 out:
1288 rtas_st(rets, 0, rc);
1291 static void spapr_drc_register_types(void)
1293 type_register_static(&spapr_dr_connector_info);
1294 type_register_static(&spapr_drc_physical_info);
1295 type_register_static(&spapr_drc_logical_info);
1296 type_register_static(&spapr_drc_cpu_info);
1297 type_register_static(&spapr_drc_pci_info);
1298 type_register_static(&spapr_drc_lmb_info);
1299 type_register_static(&spapr_drc_phb_info);
1300 type_register_static(&spapr_drc_pmem_info);
1302 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1303 rtas_set_indicator);
1304 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1305 rtas_get_sensor_state);
1306 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1307 rtas_ibm_configure_connector);
1309 type_init(spapr_drc_register_types)