pseries: do not allow memory-less/cpu-less NUMA node
[qemu/ar7.git] / hw / ppc / spapr_drc.c
blob62f1a42592cd2e1319f8ac1103ea651783e2bf28
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 "sysemu/device_tree.h"
26 #include "sysemu/reset.h"
27 #include "trace.h"
29 #define DRC_CONTAINER_PATH "/dr-connector"
30 #define DRC_INDEX_TYPE_SHIFT 28
31 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
33 SpaprDrcType spapr_drc_type(SpaprDrc *drc)
35 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
37 return 1 << drck->typeshift;
40 uint32_t spapr_drc_index(SpaprDrc *drc)
42 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
44 /* no set format for a drc index: it only needs to be globally
45 * unique. this is how we encode the DRC type on bare-metal
46 * however, so might as well do that here
48 return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
49 | (drc->id & DRC_INDEX_ID_MASK);
52 static uint32_t drc_isolate_physical(SpaprDrc *drc)
54 switch (drc->state) {
55 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
56 return RTAS_OUT_SUCCESS; /* Nothing to do */
57 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
58 break; /* see below */
59 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
60 return RTAS_OUT_PARAM_ERROR; /* not allowed */
61 default:
62 g_assert_not_reached();
65 drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
67 if (drc->unplug_requested) {
68 uint32_t drc_index = spapr_drc_index(drc);
69 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
70 spapr_drc_detach(drc);
73 return RTAS_OUT_SUCCESS;
76 static uint32_t drc_unisolate_physical(SpaprDrc *drc)
78 switch (drc->state) {
79 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
80 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
81 return RTAS_OUT_SUCCESS; /* Nothing to do */
82 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
83 break; /* see below */
84 default:
85 g_assert_not_reached();
88 /* cannot unisolate a non-existent resource, and, or resources
89 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
90 * 13.5.3.5)
92 if (!drc->dev) {
93 return RTAS_OUT_NO_SUCH_INDICATOR;
96 drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE;
97 drc->ccs_offset = drc->fdt_start_offset;
98 drc->ccs_depth = 0;
100 return RTAS_OUT_SUCCESS;
103 static uint32_t drc_isolate_logical(SpaprDrc *drc)
105 switch (drc->state) {
106 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
107 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
108 return RTAS_OUT_SUCCESS; /* Nothing to do */
109 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
110 break; /* see below */
111 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
112 return RTAS_OUT_PARAM_ERROR; /* not allowed */
113 default:
114 g_assert_not_reached();
118 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
119 * belong to a DIMM device that is marked for removal.
121 * Currently the guest userspace tool drmgr that drives the memory
122 * hotplug/unplug will just try to remove a set of 'removable' LMBs
123 * in response to a hot unplug request that is based on drc-count.
124 * If the LMB being removed doesn't belong to a DIMM device that is
125 * actually being unplugged, fail the isolation request here.
127 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
128 && !drc->unplug_requested) {
129 return RTAS_OUT_HW_ERROR;
132 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
134 /* if we're awaiting release, but still in an unconfigured state,
135 * it's likely the guest is still in the process of configuring
136 * the device and is transitioning the devices to an ISOLATED
137 * state as a part of that process. so we only complete the
138 * removal when this transition happens for a device in a
139 * configured state, as suggested by the state diagram from PAPR+
140 * 2.7, 13.4
142 if (drc->unplug_requested) {
143 uint32_t drc_index = spapr_drc_index(drc);
144 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
145 spapr_drc_detach(drc);
147 return RTAS_OUT_SUCCESS;
150 static uint32_t drc_unisolate_logical(SpaprDrc *drc)
152 switch (drc->state) {
153 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
154 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
155 return RTAS_OUT_SUCCESS; /* Nothing to do */
156 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
157 break; /* see below */
158 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
159 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
160 default:
161 g_assert_not_reached();
164 /* Move to AVAILABLE state should have ensured device was present */
165 g_assert(drc->dev);
167 drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
168 drc->ccs_offset = drc->fdt_start_offset;
169 drc->ccs_depth = 0;
171 return RTAS_OUT_SUCCESS;
174 static uint32_t drc_set_usable(SpaprDrc *drc)
176 switch (drc->state) {
177 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
178 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
179 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
180 return RTAS_OUT_SUCCESS; /* Nothing to do */
181 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
182 break; /* see below */
183 default:
184 g_assert_not_reached();
187 /* if there's no resource/device associated with the DRC, there's
188 * no way for us to put it in an allocation state consistent with
189 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
190 * result in an RTAS return code of -3 / "no such indicator"
192 if (!drc->dev) {
193 return RTAS_OUT_NO_SUCH_INDICATOR;
195 if (drc->unplug_requested) {
196 /* Don't allow the guest to move a device away from UNUSABLE
197 * state when we want to unplug it */
198 return RTAS_OUT_NO_SUCH_INDICATOR;
201 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
203 return RTAS_OUT_SUCCESS;
206 static uint32_t drc_set_unusable(SpaprDrc *drc)
208 switch (drc->state) {
209 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
210 return RTAS_OUT_SUCCESS; /* Nothing to do */
211 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
212 break; /* see below */
213 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
214 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
215 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
216 default:
217 g_assert_not_reached();
220 drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
221 if (drc->unplug_requested) {
222 uint32_t drc_index = spapr_drc_index(drc);
223 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
224 spapr_drc_detach(drc);
227 return RTAS_OUT_SUCCESS;
230 static char *spapr_drc_name(SpaprDrc *drc)
232 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
234 /* human-readable name for a DRC to encode into the DT
235 * description. this is mainly only used within a guest in place
236 * of the unique DRC index.
238 * in the case of VIO/PCI devices, it corresponds to a "location
239 * code" that maps a logical device/function (DRC index) to a
240 * physical (or virtual in the case of VIO) location in the system
241 * by chaining together the "location label" for each
242 * encapsulating component.
244 * since this is more to do with diagnosing physical hardware
245 * issues than guest compatibility, we choose location codes/DRC
246 * names that adhere to the documented format, but avoid encoding
247 * the entire topology information into the label/code, instead
248 * just using the location codes based on the labels for the
249 * endpoints (VIO/PCI adaptor connectors), which is basically just
250 * "C" followed by an integer ID.
252 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
253 * location codes as documented by PAPR+ v2.7, 12.3.1.5
255 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
259 * dr-entity-sense sensor value
260 * returned via get-sensor-state RTAS calls
261 * as expected by state diagram in PAPR+ 2.7, 13.4
262 * based on the current allocation/indicator/power states
263 * for the DR connector.
265 static SpaprDREntitySense physical_entity_sense(SpaprDrc *drc)
267 /* this assumes all PCI devices are assigned to a 'live insertion'
268 * power domain, where QEMU manages power state automatically as
269 * opposed to the guest. present, non-PCI resources are unaffected
270 * by power state.
272 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
273 : SPAPR_DR_ENTITY_SENSE_EMPTY;
276 static SpaprDREntitySense logical_entity_sense(SpaprDrc *drc)
278 switch (drc->state) {
279 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
280 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
281 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
282 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
283 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
284 g_assert(drc->dev);
285 return SPAPR_DR_ENTITY_SENSE_PRESENT;
286 default:
287 g_assert_not_reached();
291 static void prop_get_index(Object *obj, Visitor *v, const char *name,
292 void *opaque, Error **errp)
294 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
295 uint32_t value = spapr_drc_index(drc);
296 visit_type_uint32(v, name, &value, errp);
299 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
300 void *opaque, Error **errp)
302 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
303 QNull *null = NULL;
304 Error *err = NULL;
305 int fdt_offset_next, fdt_offset, fdt_depth;
306 void *fdt;
308 if (!drc->fdt) {
309 visit_type_null(v, NULL, &null, errp);
310 qobject_unref(null);
311 return;
314 fdt = drc->fdt;
315 fdt_offset = drc->fdt_start_offset;
316 fdt_depth = 0;
318 do {
319 const char *name = NULL;
320 const struct fdt_property *prop = NULL;
321 int prop_len = 0, name_len = 0;
322 uint32_t tag;
324 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
325 switch (tag) {
326 case FDT_BEGIN_NODE:
327 fdt_depth++;
328 name = fdt_get_name(fdt, fdt_offset, &name_len);
329 visit_start_struct(v, name, NULL, 0, &err);
330 if (err) {
331 error_propagate(errp, err);
332 return;
334 break;
335 case FDT_END_NODE:
336 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
337 g_assert(fdt_depth > 0);
338 visit_check_struct(v, &err);
339 visit_end_struct(v, NULL);
340 if (err) {
341 error_propagate(errp, err);
342 return;
344 fdt_depth--;
345 break;
346 case FDT_PROP: {
347 int i;
348 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
349 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
350 visit_start_list(v, name, NULL, 0, &err);
351 if (err) {
352 error_propagate(errp, err);
353 return;
355 for (i = 0; i < prop_len; i++) {
356 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
357 if (err) {
358 error_propagate(errp, err);
359 return;
362 visit_check_list(v, &err);
363 visit_end_list(v, NULL);
364 if (err) {
365 error_propagate(errp, err);
366 return;
368 break;
370 default:
371 error_report("device FDT in unexpected state: %d", tag);
372 abort();
374 fdt_offset = fdt_offset_next;
375 } while (fdt_depth != 0);
378 void spapr_drc_attach(SpaprDrc *drc, DeviceState *d, Error **errp)
380 trace_spapr_drc_attach(spapr_drc_index(drc));
382 if (drc->dev) {
383 error_setg(errp, "an attached device is still awaiting release");
384 return;
386 g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
387 || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
389 drc->dev = d;
391 object_property_add_link(OBJECT(drc), "device",
392 object_get_typename(OBJECT(drc->dev)),
393 (Object **)(&drc->dev),
394 NULL, 0, NULL);
397 static void spapr_drc_release(SpaprDrc *drc)
399 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
401 drck->release(drc->dev);
403 drc->unplug_requested = false;
404 g_free(drc->fdt);
405 drc->fdt = NULL;
406 drc->fdt_start_offset = 0;
407 object_property_del(OBJECT(drc), "device", &error_abort);
408 drc->dev = NULL;
411 void spapr_drc_detach(SpaprDrc *drc)
413 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
415 trace_spapr_drc_detach(spapr_drc_index(drc));
417 g_assert(drc->dev);
419 drc->unplug_requested = true;
421 if (drc->state != drck->empty_state) {
422 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
423 return;
426 spapr_drc_release(drc);
429 void spapr_drc_reset(SpaprDrc *drc)
431 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
433 trace_spapr_drc_reset(spapr_drc_index(drc));
435 /* immediately upon reset we can safely assume DRCs whose devices
436 * are pending removal can be safely removed.
438 if (drc->unplug_requested) {
439 spapr_drc_release(drc);
442 if (drc->dev) {
443 /* A device present at reset is ready to go, same as coldplugged */
444 drc->state = drck->ready_state;
446 * Ensure that we are able to send the FDT fragment again
447 * via configure-connector call if the guest requests.
449 drc->ccs_offset = drc->fdt_start_offset;
450 drc->ccs_depth = 0;
451 } else {
452 drc->state = drck->empty_state;
453 drc->ccs_offset = -1;
454 drc->ccs_depth = -1;
458 bool spapr_drc_needed(void *opaque)
460 SpaprDrc *drc = (SpaprDrc *)opaque;
461 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
463 /* If no dev is plugged in there is no need to migrate the DRC state */
464 if (!drc->dev) {
465 return false;
469 * We need to migrate the state if it's not equal to the expected
470 * long-term state, which is the same as the coldplugged initial
471 * state */
472 return (drc->state != drck->ready_state);
475 static const VMStateDescription vmstate_spapr_drc = {
476 .name = "spapr_drc",
477 .version_id = 1,
478 .minimum_version_id = 1,
479 .needed = spapr_drc_needed,
480 .fields = (VMStateField []) {
481 VMSTATE_UINT32(state, SpaprDrc),
482 VMSTATE_END_OF_LIST()
486 static void realize(DeviceState *d, Error **errp)
488 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
489 Object *root_container;
490 gchar *link_name;
491 gchar *child_name;
492 Error *err = NULL;
494 trace_spapr_drc_realize(spapr_drc_index(drc));
495 /* NOTE: we do this as part of realize/unrealize due to the fact
496 * that the guest will communicate with the DRC via RTAS calls
497 * referencing the global DRC index. By unlinking the DRC
498 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
499 * inaccessible by the guest, since lookups rely on this path
500 * existing in the composition tree
502 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
503 link_name = g_strdup_printf("%x", spapr_drc_index(drc));
504 child_name = object_get_canonical_path_component(OBJECT(drc));
505 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
506 object_property_add_alias(root_container, link_name,
507 drc->owner, child_name, &err);
508 g_free(child_name);
509 g_free(link_name);
510 if (err) {
511 error_propagate(errp, err);
512 return;
514 vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
515 drc);
516 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
519 static void unrealize(DeviceState *d, Error **errp)
521 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
522 Object *root_container;
523 gchar *name;
525 trace_spapr_drc_unrealize(spapr_drc_index(drc));
526 vmstate_unregister(DEVICE(drc), &vmstate_spapr_drc, drc);
527 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
528 name = g_strdup_printf("%x", spapr_drc_index(drc));
529 object_property_del(root_container, name, errp);
530 g_free(name);
533 SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
534 uint32_t id)
536 SpaprDrc *drc = SPAPR_DR_CONNECTOR(object_new(type));
537 char *prop_name;
539 drc->id = id;
540 drc->owner = owner;
541 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
542 spapr_drc_index(drc));
543 object_property_add_child(owner, prop_name, OBJECT(drc), &error_abort);
544 object_unref(OBJECT(drc));
545 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
546 g_free(prop_name);
548 return drc;
551 static void spapr_dr_connector_instance_init(Object *obj)
553 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
554 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
556 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
557 object_property_add(obj, "index", "uint32", prop_get_index,
558 NULL, NULL, NULL, NULL);
559 object_property_add(obj, "fdt", "struct", prop_get_fdt,
560 NULL, NULL, NULL, NULL);
561 drc->state = drck->empty_state;
564 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
566 DeviceClass *dk = DEVICE_CLASS(k);
568 dk->realize = realize;
569 dk->unrealize = unrealize;
571 * Reason: it crashes FIXME find and document the real reason
573 dk->user_creatable = false;
576 static bool drc_physical_needed(void *opaque)
578 SpaprDrcPhysical *drcp = (SpaprDrcPhysical *)opaque;
579 SpaprDrc *drc = SPAPR_DR_CONNECTOR(drcp);
581 if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
582 || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
583 return false;
585 return true;
588 static const VMStateDescription vmstate_spapr_drc_physical = {
589 .name = "spapr_drc/physical",
590 .version_id = 1,
591 .minimum_version_id = 1,
592 .needed = drc_physical_needed,
593 .fields = (VMStateField []) {
594 VMSTATE_UINT32(dr_indicator, SpaprDrcPhysical),
595 VMSTATE_END_OF_LIST()
599 static void drc_physical_reset(void *opaque)
601 SpaprDrc *drc = SPAPR_DR_CONNECTOR(opaque);
602 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
604 if (drc->dev) {
605 drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
606 } else {
607 drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
611 static void realize_physical(DeviceState *d, Error **errp)
613 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
614 Error *local_err = NULL;
616 realize(d, &local_err);
617 if (local_err) {
618 error_propagate(errp, local_err);
619 return;
622 vmstate_register(DEVICE(drcp), spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
623 &vmstate_spapr_drc_physical, drcp);
624 qemu_register_reset(drc_physical_reset, drcp);
627 static void unrealize_physical(DeviceState *d, Error **errp)
629 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
630 Error *local_err = NULL;
632 unrealize(d, &local_err);
633 if (local_err) {
634 error_propagate(errp, local_err);
635 return;
638 vmstate_unregister(DEVICE(drcp), &vmstate_spapr_drc_physical, drcp);
639 qemu_unregister_reset(drc_physical_reset, drcp);
642 static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
644 DeviceClass *dk = DEVICE_CLASS(k);
645 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
647 dk->realize = realize_physical;
648 dk->unrealize = unrealize_physical;
649 drck->dr_entity_sense = physical_entity_sense;
650 drck->isolate = drc_isolate_physical;
651 drck->unisolate = drc_unisolate_physical;
652 drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
653 drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
656 static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
658 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
660 drck->dr_entity_sense = logical_entity_sense;
661 drck->isolate = drc_isolate_logical;
662 drck->unisolate = drc_unisolate_logical;
663 drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
664 drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
667 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
669 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
671 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
672 drck->typename = "CPU";
673 drck->drc_name_prefix = "CPU ";
674 drck->release = spapr_core_release;
675 drck->dt_populate = spapr_core_dt_populate;
678 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
680 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
682 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
683 drck->typename = "28";
684 drck->drc_name_prefix = "C";
685 drck->release = spapr_phb_remove_pci_device_cb;
686 drck->dt_populate = spapr_pci_dt_populate;
689 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
691 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
693 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
694 drck->typename = "MEM";
695 drck->drc_name_prefix = "LMB ";
696 drck->release = spapr_lmb_release;
697 drck->dt_populate = spapr_lmb_dt_populate;
700 static void spapr_drc_phb_class_init(ObjectClass *k, void *data)
702 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
704 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB;
705 drck->typename = "PHB";
706 drck->drc_name_prefix = "PHB ";
707 drck->release = spapr_phb_release;
708 drck->dt_populate = spapr_phb_dt_populate;
711 static const TypeInfo spapr_dr_connector_info = {
712 .name = TYPE_SPAPR_DR_CONNECTOR,
713 .parent = TYPE_DEVICE,
714 .instance_size = sizeof(SpaprDrc),
715 .instance_init = spapr_dr_connector_instance_init,
716 .class_size = sizeof(SpaprDrcClass),
717 .class_init = spapr_dr_connector_class_init,
718 .abstract = true,
721 static const TypeInfo spapr_drc_physical_info = {
722 .name = TYPE_SPAPR_DRC_PHYSICAL,
723 .parent = TYPE_SPAPR_DR_CONNECTOR,
724 .instance_size = sizeof(SpaprDrcPhysical),
725 .class_init = spapr_drc_physical_class_init,
726 .abstract = true,
729 static const TypeInfo spapr_drc_logical_info = {
730 .name = TYPE_SPAPR_DRC_LOGICAL,
731 .parent = TYPE_SPAPR_DR_CONNECTOR,
732 .class_init = spapr_drc_logical_class_init,
733 .abstract = true,
736 static const TypeInfo spapr_drc_cpu_info = {
737 .name = TYPE_SPAPR_DRC_CPU,
738 .parent = TYPE_SPAPR_DRC_LOGICAL,
739 .class_init = spapr_drc_cpu_class_init,
742 static const TypeInfo spapr_drc_pci_info = {
743 .name = TYPE_SPAPR_DRC_PCI,
744 .parent = TYPE_SPAPR_DRC_PHYSICAL,
745 .class_init = spapr_drc_pci_class_init,
748 static const TypeInfo spapr_drc_lmb_info = {
749 .name = TYPE_SPAPR_DRC_LMB,
750 .parent = TYPE_SPAPR_DRC_LOGICAL,
751 .class_init = spapr_drc_lmb_class_init,
754 static const TypeInfo spapr_drc_phb_info = {
755 .name = TYPE_SPAPR_DRC_PHB,
756 .parent = TYPE_SPAPR_DRC_LOGICAL,
757 .instance_size = sizeof(SpaprDrc),
758 .class_init = spapr_drc_phb_class_init,
761 /* helper functions for external users */
763 SpaprDrc *spapr_drc_by_index(uint32_t index)
765 Object *obj;
766 gchar *name;
768 name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH, index);
769 obj = object_resolve_path(name, NULL);
770 g_free(name);
772 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
775 SpaprDrc *spapr_drc_by_id(const char *type, uint32_t id)
777 SpaprDrcClass *drck
778 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
780 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
781 | (id & DRC_INDEX_ID_MASK));
785 * spapr_dt_drc
787 * @fdt: libfdt device tree
788 * @path: path in the DT to generate properties
789 * @owner: parent Object/DeviceState for which to generate DRC
790 * descriptions for
791 * @drc_type_mask: mask of SpaprDrcType values corresponding
792 * to the types of DRCs to generate entries for
794 * generate OF properties to describe DRC topology/indices to guests
796 * as documented in PAPR+ v2.1, 13.5.2
798 int spapr_dt_drc(void *fdt, int offset, Object *owner, uint32_t drc_type_mask)
800 Object *root_container;
801 ObjectProperty *prop;
802 ObjectPropertyIterator iter;
803 uint32_t drc_count = 0;
804 GArray *drc_indexes, *drc_power_domains;
805 GString *drc_names, *drc_types;
806 int ret;
808 /* the first entry of each properties is a 32-bit integer encoding
809 * the number of elements in the array. we won't know this until
810 * we complete the iteration through all the matching DRCs, but
811 * reserve the space now and set the offsets accordingly so we
812 * can fill them in later.
814 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
815 drc_indexes = g_array_set_size(drc_indexes, 1);
816 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
817 drc_power_domains = g_array_set_size(drc_power_domains, 1);
818 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
819 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
821 /* aliases for all DRConnector objects will be rooted in QOM
822 * composition tree at DRC_CONTAINER_PATH
824 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
826 object_property_iter_init(&iter, root_container);
827 while ((prop = object_property_iter_next(&iter))) {
828 Object *obj;
829 SpaprDrc *drc;
830 SpaprDrcClass *drck;
831 char *drc_name = NULL;
832 uint32_t drc_index, drc_power_domain;
834 if (!strstart(prop->type, "link<", NULL)) {
835 continue;
838 obj = object_property_get_link(root_container, prop->name, NULL);
839 drc = SPAPR_DR_CONNECTOR(obj);
840 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
842 if (owner && (drc->owner != owner)) {
843 continue;
846 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
847 continue;
850 drc_count++;
852 /* ibm,drc-indexes */
853 drc_index = cpu_to_be32(spapr_drc_index(drc));
854 g_array_append_val(drc_indexes, drc_index);
856 /* ibm,drc-power-domains */
857 drc_power_domain = cpu_to_be32(-1);
858 g_array_append_val(drc_power_domains, drc_power_domain);
860 /* ibm,drc-names */
861 drc_name = spapr_drc_name(drc);
862 drc_names = g_string_append(drc_names, drc_name);
863 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
864 g_free(drc_name);
866 /* ibm,drc-types */
867 drc_types = g_string_append(drc_types, drck->typename);
868 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
871 /* now write the drc count into the space we reserved at the
872 * beginning of the arrays previously
874 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
875 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
876 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
877 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
879 ret = fdt_setprop(fdt, offset, "ibm,drc-indexes",
880 drc_indexes->data,
881 drc_indexes->len * sizeof(uint32_t));
882 if (ret) {
883 error_report("Couldn't create ibm,drc-indexes property");
884 goto out;
887 ret = fdt_setprop(fdt, offset, "ibm,drc-power-domains",
888 drc_power_domains->data,
889 drc_power_domains->len * sizeof(uint32_t));
890 if (ret) {
891 error_report("Couldn't finalize ibm,drc-power-domains property");
892 goto out;
895 ret = fdt_setprop(fdt, offset, "ibm,drc-names",
896 drc_names->str, drc_names->len);
897 if (ret) {
898 error_report("Couldn't finalize ibm,drc-names property");
899 goto out;
902 ret = fdt_setprop(fdt, offset, "ibm,drc-types",
903 drc_types->str, drc_types->len);
904 if (ret) {
905 error_report("Couldn't finalize ibm,drc-types property");
906 goto out;
909 out:
910 g_array_free(drc_indexes, true);
911 g_array_free(drc_power_domains, true);
912 g_string_free(drc_names, true);
913 g_string_free(drc_types, true);
915 return ret;
919 * RTAS calls
922 static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
924 SpaprDrc *drc = spapr_drc_by_index(idx);
925 SpaprDrcClass *drck;
927 if (!drc) {
928 return RTAS_OUT_NO_SUCH_INDICATOR;
931 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
933 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
935 switch (state) {
936 case SPAPR_DR_ISOLATION_STATE_ISOLATED:
937 return drck->isolate(drc);
939 case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
940 return drck->unisolate(drc);
942 default:
943 return RTAS_OUT_PARAM_ERROR;
947 static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
949 SpaprDrc *drc = spapr_drc_by_index(idx);
951 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
952 return RTAS_OUT_NO_SUCH_INDICATOR;
955 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
957 switch (state) {
958 case SPAPR_DR_ALLOCATION_STATE_USABLE:
959 return drc_set_usable(drc);
961 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
962 return drc_set_unusable(drc);
964 default:
965 return RTAS_OUT_PARAM_ERROR;
969 static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
971 SpaprDrc *drc = spapr_drc_by_index(idx);
973 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
974 return RTAS_OUT_NO_SUCH_INDICATOR;
976 if ((state != SPAPR_DR_INDICATOR_INACTIVE)
977 && (state != SPAPR_DR_INDICATOR_ACTIVE)
978 && (state != SPAPR_DR_INDICATOR_IDENTIFY)
979 && (state != SPAPR_DR_INDICATOR_ACTION)) {
980 return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
983 trace_spapr_drc_set_dr_indicator(idx, state);
984 SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
985 return RTAS_OUT_SUCCESS;
988 static void rtas_set_indicator(PowerPCCPU *cpu, SpaprMachineState *spapr,
989 uint32_t token,
990 uint32_t nargs, target_ulong args,
991 uint32_t nret, target_ulong rets)
993 uint32_t type, idx, state;
994 uint32_t ret = RTAS_OUT_SUCCESS;
996 if (nargs != 3 || nret != 1) {
997 ret = RTAS_OUT_PARAM_ERROR;
998 goto out;
1001 type = rtas_ld(args, 0);
1002 idx = rtas_ld(args, 1);
1003 state = rtas_ld(args, 2);
1005 switch (type) {
1006 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1007 ret = rtas_set_isolation_state(idx, state);
1008 break;
1009 case RTAS_SENSOR_TYPE_DR:
1010 ret = rtas_set_dr_indicator(idx, state);
1011 break;
1012 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1013 ret = rtas_set_allocation_state(idx, state);
1014 break;
1015 default:
1016 ret = RTAS_OUT_NOT_SUPPORTED;
1019 out:
1020 rtas_st(rets, 0, ret);
1023 static void rtas_get_sensor_state(PowerPCCPU *cpu, SpaprMachineState *spapr,
1024 uint32_t token, uint32_t nargs,
1025 target_ulong args, uint32_t nret,
1026 target_ulong rets)
1028 uint32_t sensor_type;
1029 uint32_t sensor_index;
1030 uint32_t sensor_state = 0;
1031 SpaprDrc *drc;
1032 SpaprDrcClass *drck;
1033 uint32_t ret = RTAS_OUT_SUCCESS;
1035 if (nargs != 2 || nret != 2) {
1036 ret = RTAS_OUT_PARAM_ERROR;
1037 goto out;
1040 sensor_type = rtas_ld(args, 0);
1041 sensor_index = rtas_ld(args, 1);
1043 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1044 /* currently only DR-related sensors are implemented */
1045 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1046 sensor_type);
1047 ret = RTAS_OUT_NOT_SUPPORTED;
1048 goto out;
1051 drc = spapr_drc_by_index(sensor_index);
1052 if (!drc) {
1053 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1054 ret = RTAS_OUT_PARAM_ERROR;
1055 goto out;
1057 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1058 sensor_state = drck->dr_entity_sense(drc);
1060 out:
1061 rtas_st(rets, 0, ret);
1062 rtas_st(rets, 1, sensor_state);
1065 /* configure-connector work area offsets, int32_t units for field
1066 * indexes, bytes for field offset/len values.
1068 * as documented by PAPR+ v2.7, 13.5.3.5
1070 #define CC_IDX_NODE_NAME_OFFSET 2
1071 #define CC_IDX_PROP_NAME_OFFSET 2
1072 #define CC_IDX_PROP_LEN 3
1073 #define CC_IDX_PROP_DATA_OFFSET 4
1074 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1075 #define CC_WA_LEN 4096
1077 static void configure_connector_st(target_ulong addr, target_ulong offset,
1078 const void *buf, size_t len)
1080 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1081 buf, MIN(len, CC_WA_LEN - offset));
1084 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1085 SpaprMachineState *spapr,
1086 uint32_t token, uint32_t nargs,
1087 target_ulong args, uint32_t nret,
1088 target_ulong rets)
1090 uint64_t wa_addr;
1091 uint64_t wa_offset;
1092 uint32_t drc_index;
1093 SpaprDrc *drc;
1094 SpaprDrcClass *drck;
1095 SpaprDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1096 int rc;
1098 if (nargs != 2 || nret != 1) {
1099 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1100 return;
1103 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1105 drc_index = rtas_ld(wa_addr, 0);
1106 drc = spapr_drc_by_index(drc_index);
1107 if (!drc) {
1108 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1109 rc = RTAS_OUT_PARAM_ERROR;
1110 goto out;
1113 if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
1114 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
1115 && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
1116 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
1118 * Need to unisolate the device before configuring
1119 * or it should already be in configured state to
1120 * allow configure-connector be called repeatedly.
1122 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1123 goto out;
1126 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1128 if (!drc->fdt) {
1129 Error *local_err = NULL;
1130 void *fdt;
1131 int fdt_size;
1133 fdt = create_device_tree(&fdt_size);
1135 if (drck->dt_populate(drc, spapr, fdt, &drc->fdt_start_offset,
1136 &local_err)) {
1137 g_free(fdt);
1138 error_free(local_err);
1139 rc = SPAPR_DR_CC_RESPONSE_ERROR;
1140 goto out;
1143 drc->fdt = fdt;
1144 drc->ccs_offset = drc->fdt_start_offset;
1145 drc->ccs_depth = 0;
1148 do {
1149 uint32_t tag;
1150 const char *name;
1151 const struct fdt_property *prop;
1152 int fdt_offset_next, prop_len;
1154 tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
1156 switch (tag) {
1157 case FDT_BEGIN_NODE:
1158 drc->ccs_depth++;
1159 name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
1161 /* provide the name of the next OF node */
1162 wa_offset = CC_VAL_DATA_OFFSET;
1163 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1164 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1165 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1166 break;
1167 case FDT_END_NODE:
1168 drc->ccs_depth--;
1169 if (drc->ccs_depth == 0) {
1170 uint32_t drc_index = spapr_drc_index(drc);
1172 /* done sending the device tree, move to configured state */
1173 trace_spapr_drc_set_configured(drc_index);
1174 drc->state = drck->ready_state;
1176 * Ensure that we are able to send the FDT fragment
1177 * again via configure-connector call if the guest requests.
1179 drc->ccs_offset = drc->fdt_start_offset;
1180 drc->ccs_depth = 0;
1181 fdt_offset_next = drc->fdt_start_offset;
1182 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1183 } else {
1184 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1186 break;
1187 case FDT_PROP:
1188 prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
1189 &prop_len);
1190 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1192 /* provide the name of the next OF property */
1193 wa_offset = CC_VAL_DATA_OFFSET;
1194 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1195 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1197 /* provide the length and value of the OF property. data gets
1198 * placed immediately after NULL terminator of the OF property's
1199 * name string
1201 wa_offset += strlen(name) + 1,
1202 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1203 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1204 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1205 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1206 break;
1207 case FDT_END:
1208 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1209 default:
1210 /* keep seeking for an actionable tag */
1211 break;
1213 if (drc->ccs_offset >= 0) {
1214 drc->ccs_offset = fdt_offset_next;
1216 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1218 rc = resp;
1219 out:
1220 rtas_st(rets, 0, rc);
1223 static void spapr_drc_register_types(void)
1225 type_register_static(&spapr_dr_connector_info);
1226 type_register_static(&spapr_drc_physical_info);
1227 type_register_static(&spapr_drc_logical_info);
1228 type_register_static(&spapr_drc_cpu_info);
1229 type_register_static(&spapr_drc_pci_info);
1230 type_register_static(&spapr_drc_lmb_info);
1231 type_register_static(&spapr_drc_phb_info);
1233 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1234 rtas_set_indicator);
1235 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1236 rtas_get_sensor_state);
1237 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1238 rtas_ibm_configure_connector);
1240 type_init(spapr_drc_register_types)