migration: do floating-point division
[qemu.git] / hw / ppc / spapr_drc.c
blobf34bc04c480e306204c17af45a31dd94b617c074
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 int fdt_offset_next, fdt_offset, fdt_depth;
258 void *fdt;
260 if (!drc->fdt) {
261 return;
264 fdt = drc->fdt;
265 fdt_offset = drc->fdt_start_offset;
266 fdt_depth = 0;
268 do {
269 const char *name = NULL;
270 const struct fdt_property *prop = NULL;
271 int prop_len = 0, name_len = 0;
272 uint32_t tag;
274 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
275 switch (tag) {
276 case FDT_BEGIN_NODE:
277 fdt_depth++;
278 name = fdt_get_name(fdt, fdt_offset, &name_len);
279 visit_start_struct(v, NULL, NULL, name, 0, NULL);
280 break;
281 case FDT_END_NODE:
282 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
283 g_assert(fdt_depth > 0);
284 visit_end_struct(v, NULL);
285 fdt_depth--;
286 break;
287 case FDT_PROP: {
288 int i;
289 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
290 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
291 visit_start_list(v, name, NULL);
292 for (i = 0; i < prop_len; i++) {
293 visit_type_uint8(v, (uint8_t *)&prop->data[i], NULL, NULL);
296 visit_end_list(v, NULL);
297 break;
299 default:
300 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
302 fdt_offset = fdt_offset_next;
303 } while (fdt_depth != 0);
306 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
307 int fdt_start_offset, bool coldplug, Error **errp)
309 DPRINTFN("drc: %x, attach", get_index(drc));
311 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
312 error_setg(errp, "an attached device is still awaiting release");
313 return;
315 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
316 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
318 g_assert(fdt || coldplug);
320 /* NOTE: setting initial isolation state to UNISOLATED means we can't
321 * detach unless guest has a userspace/kernel that moves this state
322 * back to ISOLATED in response to an unplug event, or this is done
323 * manually by the admin prior. if we force things while the guest
324 * may be accessing the device, we can easily crash the guest, so we
325 * we defer completion of removal in such cases to the reset() hook.
327 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
328 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
330 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
332 drc->dev = d;
333 drc->fdt = fdt;
334 drc->fdt_start_offset = fdt_start_offset;
335 drc->configured = coldplug;
337 object_property_add_link(OBJECT(drc), "device",
338 object_get_typename(OBJECT(drc->dev)),
339 (Object **)(&drc->dev),
340 NULL, 0, NULL);
343 static void detach(sPAPRDRConnector *drc, DeviceState *d,
344 spapr_drc_detach_cb *detach_cb,
345 void *detach_cb_opaque, Error **errp)
347 DPRINTFN("drc: %x, detach", get_index(drc));
349 drc->detach_cb = detach_cb;
350 drc->detach_cb_opaque = detach_cb_opaque;
352 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
353 DPRINTFN("awaiting transition to isolated state before removal");
354 drc->awaiting_release = true;
355 return;
358 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
359 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
360 DPRINTFN("awaiting transition to unusable state before removal");
361 drc->awaiting_release = true;
362 return;
365 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
367 if (drc->detach_cb) {
368 drc->detach_cb(drc->dev, drc->detach_cb_opaque);
371 drc->awaiting_release = false;
372 g_free(drc->fdt);
373 drc->fdt = NULL;
374 drc->fdt_start_offset = 0;
375 object_property_del(OBJECT(drc), "device", NULL);
376 drc->dev = NULL;
377 drc->detach_cb = NULL;
378 drc->detach_cb_opaque = NULL;
381 static bool release_pending(sPAPRDRConnector *drc)
383 return drc->awaiting_release;
386 static void reset(DeviceState *d)
388 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
389 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
391 DPRINTFN("drc reset: %x", drck->get_index(drc));
392 /* immediately upon reset we can safely assume DRCs whose devices
393 * are pending removal can be safely removed, and that they will
394 * subsequently be left in an ISOLATED state. move the DRC to this
395 * state in these cases (which will in turn complete any pending
396 * device removals)
398 if (drc->awaiting_release) {
399 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
400 /* generally this should also finalize the removal, but if the device
401 * hasn't yet been configured we normally defer removal under the
402 * assumption that this transition is taking place as part of device
403 * configuration. so check if we're still waiting after this, and
404 * force removal if we are
406 if (drc->awaiting_release) {
407 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
408 drc->detach_cb_opaque, NULL);
411 /* non-PCI devices may be awaiting a transition to UNUSABLE */
412 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
413 drc->awaiting_release) {
414 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
419 static void realize(DeviceState *d, Error **errp)
421 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
422 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
423 Object *root_container;
424 char link_name[256];
425 gchar *child_name;
426 Error *err = NULL;
428 DPRINTFN("drc realize: %x", drck->get_index(drc));
429 /* NOTE: we do this as part of realize/unrealize due to the fact
430 * that the guest will communicate with the DRC via RTAS calls
431 * referencing the global DRC index. By unlinking the DRC
432 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
433 * inaccessible by the guest, since lookups rely on this path
434 * existing in the composition tree
436 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
437 snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
438 child_name = object_get_canonical_path_component(OBJECT(drc));
439 DPRINTFN("drc child name: %s", child_name);
440 object_property_add_alias(root_container, link_name,
441 drc->owner, child_name, &err);
442 if (err) {
443 error_report("%s", error_get_pretty(err));
444 error_free(err);
445 object_unref(OBJECT(drc));
447 g_free(child_name);
448 DPRINTFN("drc realize complete");
451 static void unrealize(DeviceState *d, Error **errp)
453 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
454 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
455 Object *root_container;
456 char name[256];
457 Error *err = NULL;
459 DPRINTFN("drc unrealize: %x", drck->get_index(drc));
460 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
461 snprintf(name, sizeof(name), "%x", drck->get_index(drc));
462 object_property_del(root_container, name, &err);
463 if (err) {
464 error_report("%s", error_get_pretty(err));
465 error_free(err);
466 object_unref(OBJECT(drc));
470 sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
471 sPAPRDRConnectorType type,
472 uint32_t id)
474 sPAPRDRConnector *drc =
475 SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
476 char *prop_name;
478 g_assert(type);
480 drc->type = type;
481 drc->id = id;
482 drc->owner = owner;
483 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
484 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
485 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
486 g_free(prop_name);
488 /* human-readable name for a DRC to encode into the DT
489 * description. this is mainly only used within a guest in place
490 * of the unique DRC index.
492 * in the case of VIO/PCI devices, it corresponds to a
493 * "location code" that maps a logical device/function (DRC index)
494 * to a physical (or virtual in the case of VIO) location in the
495 * system by chaining together the "location label" for each
496 * encapsulating component.
498 * since this is more to do with diagnosing physical hardware
499 * issues than guest compatibility, we choose location codes/DRC
500 * names that adhere to the documented format, but avoid encoding
501 * the entire topology information into the label/code, instead
502 * just using the location codes based on the labels for the
503 * endpoints (VIO/PCI adaptor connectors), which is basically
504 * just "C" followed by an integer ID.
506 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
507 * location codes as documented by PAPR+ v2.7, 12.3.1.5
509 switch (drc->type) {
510 case SPAPR_DR_CONNECTOR_TYPE_CPU:
511 drc->name = g_strdup_printf("CPU %d", id);
512 break;
513 case SPAPR_DR_CONNECTOR_TYPE_PHB:
514 drc->name = g_strdup_printf("PHB %d", id);
515 break;
516 case SPAPR_DR_CONNECTOR_TYPE_VIO:
517 case SPAPR_DR_CONNECTOR_TYPE_PCI:
518 drc->name = g_strdup_printf("C%d", id);
519 break;
520 case SPAPR_DR_CONNECTOR_TYPE_LMB:
521 drc->name = g_strdup_printf("LMB %d", id);
522 break;
523 default:
524 g_assert(false);
527 /* PCI slot always start in a USABLE state, and stay there */
528 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
529 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
532 return drc;
535 static void spapr_dr_connector_instance_init(Object *obj)
537 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
539 object_property_add_uint32_ptr(obj, "isolation-state",
540 &drc->isolation_state, NULL);
541 object_property_add_uint32_ptr(obj, "indicator-state",
542 &drc->indicator_state, NULL);
543 object_property_add_uint32_ptr(obj, "allocation-state",
544 &drc->allocation_state, NULL);
545 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
546 object_property_add(obj, "index", "uint32", prop_get_index,
547 NULL, NULL, NULL, NULL);
548 object_property_add(obj, "connector_type", "uint32", prop_get_type,
549 NULL, NULL, NULL, NULL);
550 object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
551 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
552 NULL, NULL, NULL, NULL);
553 object_property_add(obj, "fdt", "struct", prop_get_fdt,
554 NULL, NULL, NULL, NULL);
557 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
559 DeviceClass *dk = DEVICE_CLASS(k);
560 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
562 dk->reset = reset;
563 dk->realize = realize;
564 dk->unrealize = unrealize;
565 drck->set_isolation_state = set_isolation_state;
566 drck->set_indicator_state = set_indicator_state;
567 drck->set_allocation_state = set_allocation_state;
568 drck->get_index = get_index;
569 drck->get_type = get_type;
570 drck->get_name = get_name;
571 drck->get_fdt = get_fdt;
572 drck->set_configured = set_configured;
573 drck->entity_sense = entity_sense;
574 drck->attach = attach;
575 drck->detach = detach;
576 drck->release_pending = release_pending;
579 static const TypeInfo spapr_dr_connector_info = {
580 .name = TYPE_SPAPR_DR_CONNECTOR,
581 .parent = TYPE_DEVICE,
582 .instance_size = sizeof(sPAPRDRConnector),
583 .instance_init = spapr_dr_connector_instance_init,
584 .class_size = sizeof(sPAPRDRConnectorClass),
585 .class_init = spapr_dr_connector_class_init,
588 static void spapr_drc_register_types(void)
590 type_register_static(&spapr_dr_connector_info);
593 type_init(spapr_drc_register_types)
595 /* helper functions for external users */
597 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
599 Object *obj;
600 char name[256];
602 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
603 obj = object_resolve_path(name, NULL);
605 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
608 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
609 uint32_t id)
611 return spapr_dr_connector_by_index(
612 (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
613 (id & DRC_INDEX_ID_MASK));
616 /* generate a string the describes the DRC to encode into the
617 * device tree.
619 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
621 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
623 switch (type) {
624 case SPAPR_DR_CONNECTOR_TYPE_CPU:
625 return "CPU";
626 case SPAPR_DR_CONNECTOR_TYPE_PHB:
627 return "PHB";
628 case SPAPR_DR_CONNECTOR_TYPE_VIO:
629 return "SLOT";
630 case SPAPR_DR_CONNECTOR_TYPE_PCI:
631 return "28";
632 case SPAPR_DR_CONNECTOR_TYPE_LMB:
633 return "MEM";
634 default:
635 g_assert(false);
638 return NULL;
642 * spapr_drc_populate_dt
644 * @fdt: libfdt device tree
645 * @path: path in the DT to generate properties
646 * @owner: parent Object/DeviceState for which to generate DRC
647 * descriptions for
648 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
649 * to the types of DRCs to generate entries for
651 * generate OF properties to describe DRC topology/indices to guests
653 * as documented in PAPR+ v2.1, 13.5.2
655 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
656 uint32_t drc_type_mask)
658 Object *root_container;
659 ObjectProperty *prop;
660 ObjectPropertyIterator *iter;
661 uint32_t drc_count = 0;
662 GArray *drc_indexes, *drc_power_domains;
663 GString *drc_names, *drc_types;
664 int ret;
666 /* the first entry of each properties is a 32-bit integer encoding
667 * the number of elements in the array. we won't know this until
668 * we complete the iteration through all the matching DRCs, but
669 * reserve the space now and set the offsets accordingly so we
670 * can fill them in later.
672 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
673 drc_indexes = g_array_set_size(drc_indexes, 1);
674 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
675 drc_power_domains = g_array_set_size(drc_power_domains, 1);
676 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
677 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
679 /* aliases for all DRConnector objects will be rooted in QOM
680 * composition tree at DRC_CONTAINER_PATH
682 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
684 iter = object_property_iter_init(root_container);
685 while ((prop = object_property_iter_next(iter))) {
686 Object *obj;
687 sPAPRDRConnector *drc;
688 sPAPRDRConnectorClass *drck;
689 uint32_t drc_index, drc_power_domain;
691 if (!strstart(prop->type, "link<", NULL)) {
692 continue;
695 obj = object_property_get_link(root_container, prop->name, NULL);
696 drc = SPAPR_DR_CONNECTOR(obj);
697 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
699 if (owner && (drc->owner != owner)) {
700 continue;
703 if ((drc->type & drc_type_mask) == 0) {
704 continue;
707 drc_count++;
709 /* ibm,drc-indexes */
710 drc_index = cpu_to_be32(drck->get_index(drc));
711 g_array_append_val(drc_indexes, drc_index);
713 /* ibm,drc-power-domains */
714 drc_power_domain = cpu_to_be32(-1);
715 g_array_append_val(drc_power_domains, drc_power_domain);
717 /* ibm,drc-names */
718 drc_names = g_string_append(drc_names, drck->get_name(drc));
719 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
721 /* ibm,drc-types */
722 drc_types = g_string_append(drc_types,
723 spapr_drc_get_type_str(drc->type));
724 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
726 object_property_iter_free(iter);
728 /* now write the drc count into the space we reserved at the
729 * beginning of the arrays previously
731 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
732 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
733 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
734 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
736 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
737 drc_indexes->data,
738 drc_indexes->len * sizeof(uint32_t));
739 if (ret) {
740 fprintf(stderr, "Couldn't create ibm,drc-indexes property\n");
741 goto out;
744 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
745 drc_power_domains->data,
746 drc_power_domains->len * sizeof(uint32_t));
747 if (ret) {
748 fprintf(stderr, "Couldn't finalize ibm,drc-power-domains property\n");
749 goto out;
752 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
753 drc_names->str, drc_names->len);
754 if (ret) {
755 fprintf(stderr, "Couldn't finalize ibm,drc-names property\n");
756 goto out;
759 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
760 drc_types->str, drc_types->len);
761 if (ret) {
762 fprintf(stderr, "Couldn't finalize ibm,drc-types property\n");
763 goto out;
766 out:
767 g_array_free(drc_indexes, true);
768 g_array_free(drc_power_domains, true);
769 g_string_free(drc_names, true);
770 g_string_free(drc_types, true);
772 return ret;