spapr_drc: Make device "spapr-dr-connector" unavailable with -device
[qemu/cris-port.git] / hw / ppc / spapr_drc.c
blob404fb3c2b6c7f558a6af040eed3c49859d2e6824
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 "hw/ppc/spapr_drc.h"
14 #include "qom/object.h"
15 #include "hw/qdev.h"
16 #include "qapi/visitor.h"
17 #include "qemu/error-report.h"
18 #include "hw/ppc/spapr.h" /* for RTAS return codes */
20 /* #define DEBUG_SPAPR_DRC */
22 #ifdef DEBUG_SPAPR_DRC
23 #define DPRINTF(fmt, ...) \
24 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
25 #define DPRINTFN(fmt, ...) \
26 do { DPRINTF(fmt, ## __VA_ARGS__); fprintf(stderr, "\n"); } while (0)
27 #else
28 #define DPRINTF(fmt, ...) \
29 do { } while (0)
30 #define DPRINTFN(fmt, ...) \
31 do { } while (0)
32 #endif
34 #define DRC_CONTAINER_PATH "/dr-connector"
35 #define DRC_INDEX_TYPE_SHIFT 28
36 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
38 static sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type)
40 uint32_t shift = 0;
42 /* make sure this isn't SPAPR_DR_CONNECTOR_TYPE_ANY, or some
43 * other wonky value.
45 g_assert(is_power_of_2(type));
47 while (type != (1 << shift)) {
48 shift++;
50 return shift;
53 static uint32_t get_index(sPAPRDRConnector *drc)
55 /* no set format for a drc index: it only needs to be globally
56 * unique. this is how we encode the DRC type on bare-metal
57 * however, so might as well do that here
59 return (get_type_shift(drc->type) << DRC_INDEX_TYPE_SHIFT) |
60 (drc->id & DRC_INDEX_ID_MASK);
63 static uint32_t set_isolation_state(sPAPRDRConnector *drc,
64 sPAPRDRIsolationState state)
66 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
68 DPRINTFN("drc: %x, set_isolation_state: %x", get_index(drc), state);
70 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
71 /* cannot unisolate a non-existant resource, and, or resources
72 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
74 if (!drc->dev ||
75 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
76 return RTAS_OUT_NO_SUCH_INDICATOR;
80 drc->isolation_state = state;
82 if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
83 /* if we're awaiting release, but still in an unconfigured state,
84 * it's likely the guest is still in the process of configuring
85 * the device and is transitioning the devices to an ISOLATED
86 * state as a part of that process. so we only complete the
87 * removal when this transition happens for a device in a
88 * configured state, as suggested by the state diagram from
89 * PAPR+ 2.7, 13.4
91 if (drc->awaiting_release) {
92 if (drc->configured) {
93 DPRINTFN("finalizing device removal");
94 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
95 drc->detach_cb_opaque, NULL);
96 } else {
97 DPRINTFN("deferring device removal on unconfigured device\n");
100 drc->configured = false;
103 return RTAS_OUT_SUCCESS;
106 static uint32_t set_indicator_state(sPAPRDRConnector *drc,
107 sPAPRDRIndicatorState state)
109 DPRINTFN("drc: %x, set_indicator_state: %x", get_index(drc), state);
110 drc->indicator_state = state;
111 return RTAS_OUT_SUCCESS;
114 static uint32_t set_allocation_state(sPAPRDRConnector *drc,
115 sPAPRDRAllocationState state)
117 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
119 DPRINTFN("drc: %x, set_allocation_state: %x", get_index(drc), state);
121 if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
122 /* if there's no resource/device associated with the DRC, there's
123 * no way for us to put it in an allocation state consistent with
124 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
125 * result in an RTAS return code of -3 / "no such indicator"
127 if (!drc->dev) {
128 return RTAS_OUT_NO_SUCH_INDICATOR;
132 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
133 drc->allocation_state = state;
134 if (drc->awaiting_release &&
135 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
136 DPRINTFN("finalizing device removal");
137 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
138 drc->detach_cb_opaque, NULL);
141 return RTAS_OUT_SUCCESS;
144 static uint32_t get_type(sPAPRDRConnector *drc)
146 return drc->type;
149 static const char *get_name(sPAPRDRConnector *drc)
151 return drc->name;
154 static const void *get_fdt(sPAPRDRConnector *drc, int *fdt_start_offset)
156 if (fdt_start_offset) {
157 *fdt_start_offset = drc->fdt_start_offset;
159 return drc->fdt;
162 static void set_configured(sPAPRDRConnector *drc)
164 DPRINTFN("drc: %x, set_configured", get_index(drc));
166 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
167 /* guest should be not configuring an isolated device */
168 DPRINTFN("drc: %x, set_configured: skipping isolated device",
169 get_index(drc));
170 return;
172 drc->configured = true;
176 * dr-entity-sense sensor value
177 * returned via get-sensor-state RTAS calls
178 * as expected by state diagram in PAPR+ 2.7, 13.4
179 * based on the current allocation/indicator/power states
180 * for the DR connector.
182 static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
184 if (drc->dev) {
185 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
186 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
187 /* for logical DR, we return a state of UNUSABLE
188 * iff the allocation state UNUSABLE.
189 * Otherwise, report the state as USABLE/PRESENT,
190 * as we would for PCI.
192 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
193 } else {
194 /* this assumes all PCI devices are assigned to
195 * a 'live insertion' power domain, where QEMU
196 * manages power state automatically as opposed
197 * to the guest. present, non-PCI resources are
198 * unaffected by power state.
200 *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
202 } else {
203 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
204 /* PCI devices, and only PCI devices, use EMPTY
205 * in cases where we'd otherwise use UNUSABLE
207 *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
208 } else {
209 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
213 DPRINTFN("drc: %x, entity_sense: %x", get_index(drc), state);
214 return RTAS_OUT_SUCCESS;
217 static void prop_get_index(Object *obj, Visitor *v, void *opaque,
218 const char *name, Error **errp)
220 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
221 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
222 uint32_t value = (uint32_t)drck->get_index(drc);
223 visit_type_uint32(v, &value, name, errp);
226 static void prop_get_type(Object *obj, Visitor *v, void *opaque,
227 const char *name, Error **errp)
229 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
230 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
231 uint32_t value = (uint32_t)drck->get_type(drc);
232 visit_type_uint32(v, &value, name, errp);
235 static char *prop_get_name(Object *obj, Error **errp)
237 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
238 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
239 return g_strdup(drck->get_name(drc));
242 static void prop_get_entity_sense(Object *obj, Visitor *v, void *opaque,
243 const char *name, Error **errp)
245 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
246 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
247 uint32_t value;
249 drck->entity_sense(drc, &value);
250 visit_type_uint32(v, &value, name, errp);
253 static void prop_get_fdt(Object *obj, Visitor *v, void *opaque,
254 const char *name, Error **errp)
256 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
257 Error *err = NULL;
258 int fdt_offset_next, fdt_offset, fdt_depth;
259 void *fdt;
261 if (!drc->fdt) {
262 return;
265 fdt = drc->fdt;
266 fdt_offset = drc->fdt_start_offset;
267 fdt_depth = 0;
269 do {
270 const char *name = NULL;
271 const struct fdt_property *prop = NULL;
272 int prop_len = 0, name_len = 0;
273 uint32_t tag;
275 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
276 switch (tag) {
277 case FDT_BEGIN_NODE:
278 fdt_depth++;
279 name = fdt_get_name(fdt, fdt_offset, &name_len);
280 visit_start_struct(v, NULL, NULL, name, 0, &err);
281 if (err) {
282 error_propagate(errp, err);
283 return;
285 break;
286 case FDT_END_NODE:
287 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
288 g_assert(fdt_depth > 0);
289 visit_end_struct(v, &err);
290 if (err) {
291 error_propagate(errp, err);
292 return;
294 fdt_depth--;
295 break;
296 case FDT_PROP: {
297 int i;
298 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
299 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
300 visit_start_list(v, name, &err);
301 if (err) {
302 error_propagate(errp, err);
303 return;
305 for (i = 0; i < prop_len; i++) {
306 visit_type_uint8(v, (uint8_t *)&prop->data[i], NULL, &err);
307 if (err) {
308 error_propagate(errp, err);
309 return;
312 visit_end_list(v, &err);
313 if (err) {
314 error_propagate(errp, err);
315 return;
317 break;
319 default:
320 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
322 fdt_offset = fdt_offset_next;
323 } while (fdt_depth != 0);
326 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
327 int fdt_start_offset, bool coldplug, Error **errp)
329 DPRINTFN("drc: %x, attach", get_index(drc));
331 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
332 error_setg(errp, "an attached device is still awaiting release");
333 return;
335 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
336 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
338 g_assert(fdt || coldplug);
340 /* NOTE: setting initial isolation state to UNISOLATED means we can't
341 * detach unless guest has a userspace/kernel that moves this state
342 * back to ISOLATED in response to an unplug event, or this is done
343 * manually by the admin prior. if we force things while the guest
344 * may be accessing the device, we can easily crash the guest, so we
345 * we defer completion of removal in such cases to the reset() hook.
347 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
348 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
350 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
352 drc->dev = d;
353 drc->fdt = fdt;
354 drc->fdt_start_offset = fdt_start_offset;
355 drc->configured = coldplug;
357 object_property_add_link(OBJECT(drc), "device",
358 object_get_typename(OBJECT(drc->dev)),
359 (Object **)(&drc->dev),
360 NULL, 0, NULL);
363 static void detach(sPAPRDRConnector *drc, DeviceState *d,
364 spapr_drc_detach_cb *detach_cb,
365 void *detach_cb_opaque, Error **errp)
367 DPRINTFN("drc: %x, detach", get_index(drc));
369 drc->detach_cb = detach_cb;
370 drc->detach_cb_opaque = detach_cb_opaque;
372 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
373 DPRINTFN("awaiting transition to isolated state before removal");
374 drc->awaiting_release = true;
375 return;
378 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
379 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
380 DPRINTFN("awaiting transition to unusable state before removal");
381 drc->awaiting_release = true;
382 return;
385 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
387 if (drc->detach_cb) {
388 drc->detach_cb(drc->dev, drc->detach_cb_opaque);
391 drc->awaiting_release = false;
392 g_free(drc->fdt);
393 drc->fdt = NULL;
394 drc->fdt_start_offset = 0;
395 object_property_del(OBJECT(drc), "device", NULL);
396 drc->dev = NULL;
397 drc->detach_cb = NULL;
398 drc->detach_cb_opaque = NULL;
401 static bool release_pending(sPAPRDRConnector *drc)
403 return drc->awaiting_release;
406 static void reset(DeviceState *d)
408 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
409 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
411 DPRINTFN("drc reset: %x", drck->get_index(drc));
412 /* immediately upon reset we can safely assume DRCs whose devices
413 * are pending removal can be safely removed, and that they will
414 * subsequently be left in an ISOLATED state. move the DRC to this
415 * state in these cases (which will in turn complete any pending
416 * device removals)
418 if (drc->awaiting_release) {
419 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
420 /* generally this should also finalize the removal, but if the device
421 * hasn't yet been configured we normally defer removal under the
422 * assumption that this transition is taking place as part of device
423 * configuration. so check if we're still waiting after this, and
424 * force removal if we are
426 if (drc->awaiting_release) {
427 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
428 drc->detach_cb_opaque, NULL);
431 /* non-PCI devices may be awaiting a transition to UNUSABLE */
432 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
433 drc->awaiting_release) {
434 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
439 static void realize(DeviceState *d, Error **errp)
441 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
442 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
443 Object *root_container;
444 char link_name[256];
445 gchar *child_name;
446 Error *err = NULL;
448 DPRINTFN("drc realize: %x", drck->get_index(drc));
449 /* NOTE: we do this as part of realize/unrealize due to the fact
450 * that the guest will communicate with the DRC via RTAS calls
451 * referencing the global DRC index. By unlinking the DRC
452 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
453 * inaccessible by the guest, since lookups rely on this path
454 * existing in the composition tree
456 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
457 snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
458 child_name = object_get_canonical_path_component(OBJECT(drc));
459 DPRINTFN("drc child name: %s", child_name);
460 object_property_add_alias(root_container, link_name,
461 drc->owner, child_name, &err);
462 if (err) {
463 error_report("%s", error_get_pretty(err));
464 error_free(err);
465 object_unref(OBJECT(drc));
467 g_free(child_name);
468 DPRINTFN("drc realize complete");
471 static void unrealize(DeviceState *d, Error **errp)
473 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
474 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
475 Object *root_container;
476 char name[256];
477 Error *err = NULL;
479 DPRINTFN("drc unrealize: %x", drck->get_index(drc));
480 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
481 snprintf(name, sizeof(name), "%x", drck->get_index(drc));
482 object_property_del(root_container, name, &err);
483 if (err) {
484 error_report("%s", error_get_pretty(err));
485 error_free(err);
486 object_unref(OBJECT(drc));
490 sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
491 sPAPRDRConnectorType type,
492 uint32_t id)
494 sPAPRDRConnector *drc =
495 SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
496 char *prop_name;
498 g_assert(type);
500 drc->type = type;
501 drc->id = id;
502 drc->owner = owner;
503 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
504 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
505 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
506 g_free(prop_name);
508 /* human-readable name for a DRC to encode into the DT
509 * description. this is mainly only used within a guest in place
510 * of the unique DRC index.
512 * in the case of VIO/PCI devices, it corresponds to a
513 * "location code" that maps a logical device/function (DRC index)
514 * to a physical (or virtual in the case of VIO) location in the
515 * system by chaining together the "location label" for each
516 * encapsulating component.
518 * since this is more to do with diagnosing physical hardware
519 * issues than guest compatibility, we choose location codes/DRC
520 * names that adhere to the documented format, but avoid encoding
521 * the entire topology information into the label/code, instead
522 * just using the location codes based on the labels for the
523 * endpoints (VIO/PCI adaptor connectors), which is basically
524 * just "C" followed by an integer ID.
526 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
527 * location codes as documented by PAPR+ v2.7, 12.3.1.5
529 switch (drc->type) {
530 case SPAPR_DR_CONNECTOR_TYPE_CPU:
531 drc->name = g_strdup_printf("CPU %d", id);
532 break;
533 case SPAPR_DR_CONNECTOR_TYPE_PHB:
534 drc->name = g_strdup_printf("PHB %d", id);
535 break;
536 case SPAPR_DR_CONNECTOR_TYPE_VIO:
537 case SPAPR_DR_CONNECTOR_TYPE_PCI:
538 drc->name = g_strdup_printf("C%d", id);
539 break;
540 case SPAPR_DR_CONNECTOR_TYPE_LMB:
541 drc->name = g_strdup_printf("LMB %d", id);
542 break;
543 default:
544 g_assert(false);
547 /* PCI slot always start in a USABLE state, and stay there */
548 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
549 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
552 return drc;
555 static void spapr_dr_connector_instance_init(Object *obj)
557 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
559 object_property_add_uint32_ptr(obj, "isolation-state",
560 &drc->isolation_state, NULL);
561 object_property_add_uint32_ptr(obj, "indicator-state",
562 &drc->indicator_state, NULL);
563 object_property_add_uint32_ptr(obj, "allocation-state",
564 &drc->allocation_state, NULL);
565 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
566 object_property_add(obj, "index", "uint32", prop_get_index,
567 NULL, NULL, NULL, NULL);
568 object_property_add(obj, "connector_type", "uint32", prop_get_type,
569 NULL, NULL, NULL, NULL);
570 object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
571 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
572 NULL, NULL, NULL, NULL);
573 object_property_add(obj, "fdt", "struct", prop_get_fdt,
574 NULL, NULL, NULL, NULL);
577 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
579 DeviceClass *dk = DEVICE_CLASS(k);
580 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
582 dk->reset = reset;
583 dk->realize = realize;
584 dk->unrealize = unrealize;
585 drck->set_isolation_state = set_isolation_state;
586 drck->set_indicator_state = set_indicator_state;
587 drck->set_allocation_state = set_allocation_state;
588 drck->get_index = get_index;
589 drck->get_type = get_type;
590 drck->get_name = get_name;
591 drck->get_fdt = get_fdt;
592 drck->set_configured = set_configured;
593 drck->entity_sense = entity_sense;
594 drck->attach = attach;
595 drck->detach = detach;
596 drck->release_pending = release_pending;
598 * Reason: it crashes FIXME find and document the real reason
600 dk->cannot_instantiate_with_device_add_yet = true;
603 static const TypeInfo spapr_dr_connector_info = {
604 .name = TYPE_SPAPR_DR_CONNECTOR,
605 .parent = TYPE_DEVICE,
606 .instance_size = sizeof(sPAPRDRConnector),
607 .instance_init = spapr_dr_connector_instance_init,
608 .class_size = sizeof(sPAPRDRConnectorClass),
609 .class_init = spapr_dr_connector_class_init,
612 static void spapr_drc_register_types(void)
614 type_register_static(&spapr_dr_connector_info);
617 type_init(spapr_drc_register_types)
619 /* helper functions for external users */
621 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
623 Object *obj;
624 char name[256];
626 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
627 obj = object_resolve_path(name, NULL);
629 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
632 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
633 uint32_t id)
635 return spapr_dr_connector_by_index(
636 (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
637 (id & DRC_INDEX_ID_MASK));
640 /* generate a string the describes the DRC to encode into the
641 * device tree.
643 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
645 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
647 switch (type) {
648 case SPAPR_DR_CONNECTOR_TYPE_CPU:
649 return "CPU";
650 case SPAPR_DR_CONNECTOR_TYPE_PHB:
651 return "PHB";
652 case SPAPR_DR_CONNECTOR_TYPE_VIO:
653 return "SLOT";
654 case SPAPR_DR_CONNECTOR_TYPE_PCI:
655 return "28";
656 case SPAPR_DR_CONNECTOR_TYPE_LMB:
657 return "MEM";
658 default:
659 g_assert(false);
662 return NULL;
666 * spapr_drc_populate_dt
668 * @fdt: libfdt device tree
669 * @path: path in the DT to generate properties
670 * @owner: parent Object/DeviceState for which to generate DRC
671 * descriptions for
672 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
673 * to the types of DRCs to generate entries for
675 * generate OF properties to describe DRC topology/indices to guests
677 * as documented in PAPR+ v2.1, 13.5.2
679 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
680 uint32_t drc_type_mask)
682 Object *root_container;
683 ObjectProperty *prop;
684 ObjectPropertyIterator *iter;
685 uint32_t drc_count = 0;
686 GArray *drc_indexes, *drc_power_domains;
687 GString *drc_names, *drc_types;
688 int ret;
690 /* the first entry of each properties is a 32-bit integer encoding
691 * the number of elements in the array. we won't know this until
692 * we complete the iteration through all the matching DRCs, but
693 * reserve the space now and set the offsets accordingly so we
694 * can fill them in later.
696 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
697 drc_indexes = g_array_set_size(drc_indexes, 1);
698 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
699 drc_power_domains = g_array_set_size(drc_power_domains, 1);
700 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
701 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
703 /* aliases for all DRConnector objects will be rooted in QOM
704 * composition tree at DRC_CONTAINER_PATH
706 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
708 iter = object_property_iter_init(root_container);
709 while ((prop = object_property_iter_next(iter))) {
710 Object *obj;
711 sPAPRDRConnector *drc;
712 sPAPRDRConnectorClass *drck;
713 uint32_t drc_index, drc_power_domain;
715 if (!strstart(prop->type, "link<", NULL)) {
716 continue;
719 obj = object_property_get_link(root_container, prop->name, NULL);
720 drc = SPAPR_DR_CONNECTOR(obj);
721 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
723 if (owner && (drc->owner != owner)) {
724 continue;
727 if ((drc->type & drc_type_mask) == 0) {
728 continue;
731 drc_count++;
733 /* ibm,drc-indexes */
734 drc_index = cpu_to_be32(drck->get_index(drc));
735 g_array_append_val(drc_indexes, drc_index);
737 /* ibm,drc-power-domains */
738 drc_power_domain = cpu_to_be32(-1);
739 g_array_append_val(drc_power_domains, drc_power_domain);
741 /* ibm,drc-names */
742 drc_names = g_string_append(drc_names, drck->get_name(drc));
743 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
745 /* ibm,drc-types */
746 drc_types = g_string_append(drc_types,
747 spapr_drc_get_type_str(drc->type));
748 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
750 object_property_iter_free(iter);
752 /* now write the drc count into the space we reserved at the
753 * beginning of the arrays previously
755 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
756 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
757 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
758 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
760 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
761 drc_indexes->data,
762 drc_indexes->len * sizeof(uint32_t));
763 if (ret) {
764 fprintf(stderr, "Couldn't create ibm,drc-indexes property\n");
765 goto out;
768 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
769 drc_power_domains->data,
770 drc_power_domains->len * sizeof(uint32_t));
771 if (ret) {
772 fprintf(stderr, "Couldn't finalize ibm,drc-power-domains property\n");
773 goto out;
776 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
777 drc_names->str, drc_names->len);
778 if (ret) {
779 fprintf(stderr, "Couldn't finalize ibm,drc-names property\n");
780 goto out;
783 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
784 drc_types->str, drc_types->len);
785 if (ret) {
786 fprintf(stderr, "Couldn't finalize ibm,drc-types property\n");
787 goto out;
790 out:
791 g_array_free(drc_indexes, true);
792 g_array_free(drc_power_domains, true);
793 g_string_free(drc_names, true);
794 g_string_free(drc_types, true);
796 return ret;