2 * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
4 * Copyright IBM Corp. 2014
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"
16 #include "qemu/cutils.h"
17 #include "hw/ppc/spapr_drc.h"
18 #include "qom/object.h"
20 #include "qapi/visitor.h"
21 #include "qemu/error-report.h"
22 #include "hw/ppc/spapr.h" /* for RTAS return codes */
24 /* #define DEBUG_SPAPR_DRC */
26 #ifdef DEBUG_SPAPR_DRC
27 #define DPRINTF(fmt, ...) \
28 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
29 #define DPRINTFN(fmt, ...) \
30 do { DPRINTF(fmt, ## __VA_ARGS__); fprintf(stderr, "\n"); } while (0)
32 #define DPRINTF(fmt, ...) \
34 #define DPRINTFN(fmt, ...) \
38 #define DRC_CONTAINER_PATH "/dr-connector"
39 #define DRC_INDEX_TYPE_SHIFT 28
40 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
42 static sPAPRDRConnectorTypeShift
get_type_shift(sPAPRDRConnectorType type
)
46 /* make sure this isn't SPAPR_DR_CONNECTOR_TYPE_ANY, or some
49 g_assert(is_power_of_2(type
));
51 while (type
!= (1 << shift
)) {
57 static uint32_t get_index(sPAPRDRConnector
*drc
)
59 /* no set format for a drc index: it only needs to be globally
60 * unique. this is how we encode the DRC type on bare-metal
61 * however, so might as well do that here
63 return (get_type_shift(drc
->type
) << DRC_INDEX_TYPE_SHIFT
) |
64 (drc
->id
& DRC_INDEX_ID_MASK
);
67 static uint32_t set_isolation_state(sPAPRDRConnector
*drc
,
68 sPAPRDRIsolationState state
)
70 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
72 DPRINTFN("drc: %x, set_isolation_state: %x", get_index(drc
), state
);
74 if (state
== SPAPR_DR_ISOLATION_STATE_UNISOLATED
) {
75 /* cannot unisolate a non-existant resource, and, or resources
76 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
79 drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_UNUSABLE
) {
80 return RTAS_OUT_NO_SUCH_INDICATOR
;
84 drc
->isolation_state
= state
;
86 if (drc
->isolation_state
== SPAPR_DR_ISOLATION_STATE_ISOLATED
) {
87 /* if we're awaiting release, but still in an unconfigured state,
88 * it's likely the guest is still in the process of configuring
89 * the device and is transitioning the devices to an ISOLATED
90 * state as a part of that process. so we only complete the
91 * removal when this transition happens for a device in a
92 * configured state, as suggested by the state diagram from
95 if (drc
->awaiting_release
) {
96 if (drc
->configured
) {
97 DPRINTFN("finalizing device removal");
98 drck
->detach(drc
, DEVICE(drc
->dev
), drc
->detach_cb
,
99 drc
->detach_cb_opaque
, NULL
);
101 DPRINTFN("deferring device removal on unconfigured device\n");
104 drc
->configured
= false;
107 return RTAS_OUT_SUCCESS
;
110 static uint32_t set_indicator_state(sPAPRDRConnector
*drc
,
111 sPAPRDRIndicatorState state
)
113 DPRINTFN("drc: %x, set_indicator_state: %x", get_index(drc
), state
);
114 drc
->indicator_state
= state
;
115 return RTAS_OUT_SUCCESS
;
118 static uint32_t set_allocation_state(sPAPRDRConnector
*drc
,
119 sPAPRDRAllocationState state
)
121 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
123 DPRINTFN("drc: %x, set_allocation_state: %x", get_index(drc
), state
);
125 if (state
== SPAPR_DR_ALLOCATION_STATE_USABLE
) {
126 /* if there's no resource/device associated with the DRC, there's
127 * no way for us to put it in an allocation state consistent with
128 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
129 * result in an RTAS return code of -3 / "no such indicator"
132 return RTAS_OUT_NO_SUCH_INDICATOR
;
136 if (drc
->type
!= SPAPR_DR_CONNECTOR_TYPE_PCI
) {
137 drc
->allocation_state
= state
;
138 if (drc
->awaiting_release
&&
139 drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_UNUSABLE
) {
140 DPRINTFN("finalizing device removal");
141 drck
->detach(drc
, DEVICE(drc
->dev
), drc
->detach_cb
,
142 drc
->detach_cb_opaque
, NULL
);
145 return RTAS_OUT_SUCCESS
;
148 static uint32_t get_type(sPAPRDRConnector
*drc
)
153 static const char *get_name(sPAPRDRConnector
*drc
)
158 static const void *get_fdt(sPAPRDRConnector
*drc
, int *fdt_start_offset
)
160 if (fdt_start_offset
) {
161 *fdt_start_offset
= drc
->fdt_start_offset
;
166 static void set_configured(sPAPRDRConnector
*drc
)
168 DPRINTFN("drc: %x, set_configured", get_index(drc
));
170 if (drc
->isolation_state
!= SPAPR_DR_ISOLATION_STATE_UNISOLATED
) {
171 /* guest should be not configuring an isolated device */
172 DPRINTFN("drc: %x, set_configured: skipping isolated device",
176 drc
->configured
= true;
179 /* has the guest been notified of device attachment? */
180 static void set_signalled(sPAPRDRConnector
*drc
)
182 drc
->signalled
= true;
186 * dr-entity-sense sensor value
187 * returned via get-sensor-state RTAS calls
188 * as expected by state diagram in PAPR+ 2.7, 13.4
189 * based on the current allocation/indicator/power states
190 * for the DR connector.
192 static uint32_t entity_sense(sPAPRDRConnector
*drc
, sPAPRDREntitySense
*state
)
195 if (drc
->type
!= SPAPR_DR_CONNECTOR_TYPE_PCI
&&
196 drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_UNUSABLE
) {
197 /* for logical DR, we return a state of UNUSABLE
198 * iff the allocation state UNUSABLE.
199 * Otherwise, report the state as USABLE/PRESENT,
200 * as we would for PCI.
202 *state
= SPAPR_DR_ENTITY_SENSE_UNUSABLE
;
204 /* this assumes all PCI devices are assigned to
205 * a 'live insertion' power domain, where QEMU
206 * manages power state automatically as opposed
207 * to the guest. present, non-PCI resources are
208 * unaffected by power state.
210 *state
= SPAPR_DR_ENTITY_SENSE_PRESENT
;
213 if (drc
->type
== SPAPR_DR_CONNECTOR_TYPE_PCI
) {
214 /* PCI devices, and only PCI devices, use EMPTY
215 * in cases where we'd otherwise use UNUSABLE
217 *state
= SPAPR_DR_ENTITY_SENSE_EMPTY
;
219 *state
= SPAPR_DR_ENTITY_SENSE_UNUSABLE
;
223 DPRINTFN("drc: %x, entity_sense: %x", get_index(drc
), state
);
224 return RTAS_OUT_SUCCESS
;
227 static void prop_get_index(Object
*obj
, Visitor
*v
, const char *name
,
228 void *opaque
, Error
**errp
)
230 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
231 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
232 uint32_t value
= (uint32_t)drck
->get_index(drc
);
233 visit_type_uint32(v
, name
, &value
, errp
);
236 static void prop_get_type(Object
*obj
, Visitor
*v
, const char *name
,
237 void *opaque
, Error
**errp
)
239 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
240 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
241 uint32_t value
= (uint32_t)drck
->get_type(drc
);
242 visit_type_uint32(v
, name
, &value
, errp
);
245 static char *prop_get_name(Object
*obj
, Error
**errp
)
247 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
248 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
249 return g_strdup(drck
->get_name(drc
));
252 static void prop_get_entity_sense(Object
*obj
, Visitor
*v
, const char *name
,
253 void *opaque
, Error
**errp
)
255 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
256 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
259 drck
->entity_sense(drc
, &value
);
260 visit_type_uint32(v
, name
, &value
, errp
);
263 static void prop_get_fdt(Object
*obj
, Visitor
*v
, const char *name
,
264 void *opaque
, Error
**errp
)
266 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
268 int fdt_offset_next
, fdt_offset
, fdt_depth
;
272 visit_start_struct(v
, name
, NULL
, 0, &err
);
274 visit_end_struct(v
, &err
);
276 error_propagate(errp
, err
);
281 fdt_offset
= drc
->fdt_start_offset
;
285 const char *name
= NULL
;
286 const struct fdt_property
*prop
= NULL
;
287 int prop_len
= 0, name_len
= 0;
290 tag
= fdt_next_tag(fdt
, fdt_offset
, &fdt_offset_next
);
294 name
= fdt_get_name(fdt
, fdt_offset
, &name_len
);
295 visit_start_struct(v
, name
, NULL
, 0, &err
);
297 error_propagate(errp
, err
);
302 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
303 g_assert(fdt_depth
> 0);
304 visit_end_struct(v
, &err
);
306 error_propagate(errp
, err
);
313 prop
= fdt_get_property_by_offset(fdt
, fdt_offset
, &prop_len
);
314 name
= fdt_string(fdt
, fdt32_to_cpu(prop
->nameoff
));
315 visit_start_list(v
, name
, &err
);
317 error_propagate(errp
, err
);
320 for (i
= 0; i
< prop_len
; i
++) {
321 visit_type_uint8(v
, NULL
, (uint8_t *)&prop
->data
[i
], &err
);
323 error_propagate(errp
, err
);
331 error_setg(&error_abort
, "device FDT in unexpected state: %d", tag
);
333 fdt_offset
= fdt_offset_next
;
334 } while (fdt_depth
!= 0);
337 static void attach(sPAPRDRConnector
*drc
, DeviceState
*d
, void *fdt
,
338 int fdt_start_offset
, bool coldplug
, Error
**errp
)
340 DPRINTFN("drc: %x, attach", get_index(drc
));
342 if (drc
->isolation_state
!= SPAPR_DR_ISOLATION_STATE_ISOLATED
) {
343 error_setg(errp
, "an attached device is still awaiting release");
346 if (drc
->type
== SPAPR_DR_CONNECTOR_TYPE_PCI
) {
347 g_assert(drc
->allocation_state
== SPAPR_DR_ALLOCATION_STATE_USABLE
);
349 g_assert(fdt
|| coldplug
);
351 /* NOTE: setting initial isolation state to UNISOLATED means we can't
352 * detach unless guest has a userspace/kernel that moves this state
353 * back to ISOLATED in response to an unplug event, or this is done
354 * manually by the admin prior. if we force things while the guest
355 * may be accessing the device, we can easily crash the guest, so we
356 * we defer completion of removal in such cases to the reset() hook.
358 if (drc
->type
== SPAPR_DR_CONNECTOR_TYPE_PCI
) {
359 drc
->isolation_state
= SPAPR_DR_ISOLATION_STATE_UNISOLATED
;
361 drc
->indicator_state
= SPAPR_DR_INDICATOR_STATE_ACTIVE
;
365 drc
->fdt_start_offset
= fdt_start_offset
;
366 drc
->configured
= coldplug
;
367 /* 'logical' DR resources such as memory/cpus are in some cases treated
368 * as a pool of resources from which the guest is free to choose from
369 * based on only a count. for resources that can be assigned in this
370 * fashion, we must assume the resource is signalled immediately
371 * since a single hotplug request might make an arbitrary number of
372 * such attached resources available to the guest, as opposed to
373 * 'physical' DR resources such as PCI where each device/resource is
374 * signalled individually.
376 drc
->signalled
= (drc
->type
!= SPAPR_DR_CONNECTOR_TYPE_PCI
)
379 object_property_add_link(OBJECT(drc
), "device",
380 object_get_typename(OBJECT(drc
->dev
)),
381 (Object
**)(&drc
->dev
),
385 static void detach(sPAPRDRConnector
*drc
, DeviceState
*d
,
386 spapr_drc_detach_cb
*detach_cb
,
387 void *detach_cb_opaque
, Error
**errp
)
389 DPRINTFN("drc: %x, detach", get_index(drc
));
391 drc
->detach_cb
= detach_cb
;
392 drc
->detach_cb_opaque
= detach_cb_opaque
;
394 /* if we've signalled device presence to the guest, or if the guest
395 * has gone ahead and configured the device (via manually-executed
396 * device add via drmgr in guest, namely), we need to wait
397 * for the guest to quiesce the device before completing detach.
398 * Otherwise, we can assume the guest hasn't seen it and complete the
399 * detach immediately. Note that there is a small race window
400 * just before, or during, configuration, which is this context
401 * refers mainly to fetching the device tree via RTAS.
402 * During this window the device access will be arbitrated by
403 * associated DRC, which will simply fail the RTAS calls as invalid.
404 * This is recoverable within guest and current implementations of
405 * drmgr should be able to cope.
407 if (!drc
->signalled
&& !drc
->configured
) {
408 /* if the guest hasn't seen the device we can't rely on it to
409 * set it back to an isolated state via RTAS, so do it here manually
411 drc
->isolation_state
= SPAPR_DR_ISOLATION_STATE_ISOLATED
;
414 if (drc
->isolation_state
!= SPAPR_DR_ISOLATION_STATE_ISOLATED
) {
415 DPRINTFN("awaiting transition to isolated state before removal");
416 drc
->awaiting_release
= true;
420 if (drc
->type
!= SPAPR_DR_CONNECTOR_TYPE_PCI
&&
421 drc
->allocation_state
!= SPAPR_DR_ALLOCATION_STATE_UNUSABLE
) {
422 DPRINTFN("awaiting transition to unusable state before removal");
423 drc
->awaiting_release
= true;
427 drc
->indicator_state
= SPAPR_DR_INDICATOR_STATE_INACTIVE
;
429 if (drc
->detach_cb
) {
430 drc
->detach_cb(drc
->dev
, drc
->detach_cb_opaque
);
433 drc
->awaiting_release
= false;
436 drc
->fdt_start_offset
= 0;
437 object_property_del(OBJECT(drc
), "device", NULL
);
439 drc
->detach_cb
= NULL
;
440 drc
->detach_cb_opaque
= NULL
;
443 static bool release_pending(sPAPRDRConnector
*drc
)
445 return drc
->awaiting_release
;
448 static void reset(DeviceState
*d
)
450 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(d
);
451 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
452 sPAPRDREntitySense state
;
454 DPRINTFN("drc reset: %x", drck
->get_index(drc
));
455 /* immediately upon reset we can safely assume DRCs whose devices
456 * are pending removal can be safely removed, and that they will
457 * subsequently be left in an ISOLATED state. move the DRC to this
458 * state in these cases (which will in turn complete any pending
461 if (drc
->awaiting_release
) {
462 drck
->set_isolation_state(drc
, SPAPR_DR_ISOLATION_STATE_ISOLATED
);
463 /* generally this should also finalize the removal, but if the device
464 * hasn't yet been configured we normally defer removal under the
465 * assumption that this transition is taking place as part of device
466 * configuration. so check if we're still waiting after this, and
467 * force removal if we are
469 if (drc
->awaiting_release
) {
470 drck
->detach(drc
, DEVICE(drc
->dev
), drc
->detach_cb
,
471 drc
->detach_cb_opaque
, NULL
);
474 /* non-PCI devices may be awaiting a transition to UNUSABLE */
475 if (drc
->type
!= SPAPR_DR_CONNECTOR_TYPE_PCI
&&
476 drc
->awaiting_release
) {
477 drck
->set_allocation_state(drc
, SPAPR_DR_ALLOCATION_STATE_UNUSABLE
);
481 drck
->entity_sense(drc
, &state
);
482 if (state
== SPAPR_DR_ENTITY_SENSE_PRESENT
) {
483 drck
->set_signalled(drc
);
487 static void realize(DeviceState
*d
, Error
**errp
)
489 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(d
);
490 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
491 Object
*root_container
;
496 DPRINTFN("drc realize: %x", drck
->get_index(drc
));
497 /* NOTE: we do this as part of realize/unrealize due to the fact
498 * that the guest will communicate with the DRC via RTAS calls
499 * referencing the global DRC index. By unlinking the DRC
500 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
501 * inaccessible by the guest, since lookups rely on this path
502 * existing in the composition tree
504 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
505 snprintf(link_name
, sizeof(link_name
), "%x", drck
->get_index(drc
));
506 child_name
= object_get_canonical_path_component(OBJECT(drc
));
507 DPRINTFN("drc child name: %s", child_name
);
508 object_property_add_alias(root_container
, link_name
,
509 drc
->owner
, child_name
, &err
);
511 error_report_err(err
);
512 object_unref(OBJECT(drc
));
515 DPRINTFN("drc realize complete");
518 static void unrealize(DeviceState
*d
, Error
**errp
)
520 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(d
);
521 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
522 Object
*root_container
;
526 DPRINTFN("drc unrealize: %x", drck
->get_index(drc
));
527 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
528 snprintf(name
, sizeof(name
), "%x", drck
->get_index(drc
));
529 object_property_del(root_container
, name
, &err
);
531 error_report_err(err
);
532 object_unref(OBJECT(drc
));
536 sPAPRDRConnector
*spapr_dr_connector_new(Object
*owner
,
537 sPAPRDRConnectorType type
,
540 sPAPRDRConnector
*drc
=
541 SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR
));
549 prop_name
= g_strdup_printf("dr-connector[%"PRIu32
"]", get_index(drc
));
550 object_property_add_child(owner
, prop_name
, OBJECT(drc
), NULL
);
551 object_property_set_bool(OBJECT(drc
), true, "realized", NULL
);
554 /* human-readable name for a DRC to encode into the DT
555 * description. this is mainly only used within a guest in place
556 * of the unique DRC index.
558 * in the case of VIO/PCI devices, it corresponds to a
559 * "location code" that maps a logical device/function (DRC index)
560 * to a physical (or virtual in the case of VIO) location in the
561 * system by chaining together the "location label" for each
562 * encapsulating component.
564 * since this is more to do with diagnosing physical hardware
565 * issues than guest compatibility, we choose location codes/DRC
566 * names that adhere to the documented format, but avoid encoding
567 * the entire topology information into the label/code, instead
568 * just using the location codes based on the labels for the
569 * endpoints (VIO/PCI adaptor connectors), which is basically
570 * just "C" followed by an integer ID.
572 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
573 * location codes as documented by PAPR+ v2.7, 12.3.1.5
576 case SPAPR_DR_CONNECTOR_TYPE_CPU
:
577 drc
->name
= g_strdup_printf("CPU %d", id
);
579 case SPAPR_DR_CONNECTOR_TYPE_PHB
:
580 drc
->name
= g_strdup_printf("PHB %d", id
);
582 case SPAPR_DR_CONNECTOR_TYPE_VIO
:
583 case SPAPR_DR_CONNECTOR_TYPE_PCI
:
584 drc
->name
= g_strdup_printf("C%d", id
);
586 case SPAPR_DR_CONNECTOR_TYPE_LMB
:
587 drc
->name
= g_strdup_printf("LMB %d", id
);
593 /* PCI slot always start in a USABLE state, and stay there */
594 if (drc
->type
== SPAPR_DR_CONNECTOR_TYPE_PCI
) {
595 drc
->allocation_state
= SPAPR_DR_ALLOCATION_STATE_USABLE
;
601 static void spapr_dr_connector_instance_init(Object
*obj
)
603 sPAPRDRConnector
*drc
= SPAPR_DR_CONNECTOR(obj
);
605 object_property_add_uint32_ptr(obj
, "isolation-state",
606 &drc
->isolation_state
, NULL
);
607 object_property_add_uint32_ptr(obj
, "indicator-state",
608 &drc
->indicator_state
, NULL
);
609 object_property_add_uint32_ptr(obj
, "allocation-state",
610 &drc
->allocation_state
, NULL
);
611 object_property_add_uint32_ptr(obj
, "id", &drc
->id
, NULL
);
612 object_property_add(obj
, "index", "uint32", prop_get_index
,
613 NULL
, NULL
, NULL
, NULL
);
614 object_property_add(obj
, "connector_type", "uint32", prop_get_type
,
615 NULL
, NULL
, NULL
, NULL
);
616 object_property_add_str(obj
, "name", prop_get_name
, NULL
, NULL
);
617 object_property_add(obj
, "entity-sense", "uint32", prop_get_entity_sense
,
618 NULL
, NULL
, NULL
, NULL
);
619 object_property_add(obj
, "fdt", "struct", prop_get_fdt
,
620 NULL
, NULL
, NULL
, NULL
);
623 static void spapr_dr_connector_class_init(ObjectClass
*k
, void *data
)
625 DeviceClass
*dk
= DEVICE_CLASS(k
);
626 sPAPRDRConnectorClass
*drck
= SPAPR_DR_CONNECTOR_CLASS(k
);
629 dk
->realize
= realize
;
630 dk
->unrealize
= unrealize
;
631 drck
->set_isolation_state
= set_isolation_state
;
632 drck
->set_indicator_state
= set_indicator_state
;
633 drck
->set_allocation_state
= set_allocation_state
;
634 drck
->get_index
= get_index
;
635 drck
->get_type
= get_type
;
636 drck
->get_name
= get_name
;
637 drck
->get_fdt
= get_fdt
;
638 drck
->set_configured
= set_configured
;
639 drck
->entity_sense
= entity_sense
;
640 drck
->attach
= attach
;
641 drck
->detach
= detach
;
642 drck
->release_pending
= release_pending
;
643 drck
->set_signalled
= set_signalled
;
645 * Reason: it crashes FIXME find and document the real reason
647 dk
->cannot_instantiate_with_device_add_yet
= true;
650 static const TypeInfo spapr_dr_connector_info
= {
651 .name
= TYPE_SPAPR_DR_CONNECTOR
,
652 .parent
= TYPE_DEVICE
,
653 .instance_size
= sizeof(sPAPRDRConnector
),
654 .instance_init
= spapr_dr_connector_instance_init
,
655 .class_size
= sizeof(sPAPRDRConnectorClass
),
656 .class_init
= spapr_dr_connector_class_init
,
659 static void spapr_drc_register_types(void)
661 type_register_static(&spapr_dr_connector_info
);
664 type_init(spapr_drc_register_types
)
666 /* helper functions for external users */
668 sPAPRDRConnector
*spapr_dr_connector_by_index(uint32_t index
)
673 snprintf(name
, sizeof(name
), "%s/%x", DRC_CONTAINER_PATH
, index
);
674 obj
= object_resolve_path(name
, NULL
);
676 return !obj
? NULL
: SPAPR_DR_CONNECTOR(obj
);
679 sPAPRDRConnector
*spapr_dr_connector_by_id(sPAPRDRConnectorType type
,
682 return spapr_dr_connector_by_index(
683 (get_type_shift(type
) << DRC_INDEX_TYPE_SHIFT
) |
684 (id
& DRC_INDEX_ID_MASK
));
687 /* generate a string the describes the DRC to encode into the
690 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
692 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type
)
695 case SPAPR_DR_CONNECTOR_TYPE_CPU
:
697 case SPAPR_DR_CONNECTOR_TYPE_PHB
:
699 case SPAPR_DR_CONNECTOR_TYPE_VIO
:
701 case SPAPR_DR_CONNECTOR_TYPE_PCI
:
703 case SPAPR_DR_CONNECTOR_TYPE_LMB
:
713 * spapr_drc_populate_dt
715 * @fdt: libfdt device tree
716 * @path: path in the DT to generate properties
717 * @owner: parent Object/DeviceState for which to generate DRC
719 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
720 * to the types of DRCs to generate entries for
722 * generate OF properties to describe DRC topology/indices to guests
724 * as documented in PAPR+ v2.1, 13.5.2
726 int spapr_drc_populate_dt(void *fdt
, int fdt_offset
, Object
*owner
,
727 uint32_t drc_type_mask
)
729 Object
*root_container
;
730 ObjectProperty
*prop
;
731 ObjectPropertyIterator iter
;
732 uint32_t drc_count
= 0;
733 GArray
*drc_indexes
, *drc_power_domains
;
734 GString
*drc_names
, *drc_types
;
737 /* the first entry of each properties is a 32-bit integer encoding
738 * the number of elements in the array. we won't know this until
739 * we complete the iteration through all the matching DRCs, but
740 * reserve the space now and set the offsets accordingly so we
741 * can fill them in later.
743 drc_indexes
= g_array_new(false, true, sizeof(uint32_t));
744 drc_indexes
= g_array_set_size(drc_indexes
, 1);
745 drc_power_domains
= g_array_new(false, true, sizeof(uint32_t));
746 drc_power_domains
= g_array_set_size(drc_power_domains
, 1);
747 drc_names
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
748 drc_types
= g_string_set_size(g_string_new(NULL
), sizeof(uint32_t));
750 /* aliases for all DRConnector objects will be rooted in QOM
751 * composition tree at DRC_CONTAINER_PATH
753 root_container
= container_get(object_get_root(), DRC_CONTAINER_PATH
);
755 object_property_iter_init(&iter
, root_container
);
756 while ((prop
= object_property_iter_next(&iter
))) {
758 sPAPRDRConnector
*drc
;
759 sPAPRDRConnectorClass
*drck
;
760 uint32_t drc_index
, drc_power_domain
;
762 if (!strstart(prop
->type
, "link<", NULL
)) {
766 obj
= object_property_get_link(root_container
, prop
->name
, NULL
);
767 drc
= SPAPR_DR_CONNECTOR(obj
);
768 drck
= SPAPR_DR_CONNECTOR_GET_CLASS(drc
);
770 if (owner
&& (drc
->owner
!= owner
)) {
774 if ((drc
->type
& drc_type_mask
) == 0) {
780 /* ibm,drc-indexes */
781 drc_index
= cpu_to_be32(drck
->get_index(drc
));
782 g_array_append_val(drc_indexes
, drc_index
);
784 /* ibm,drc-power-domains */
785 drc_power_domain
= cpu_to_be32(-1);
786 g_array_append_val(drc_power_domains
, drc_power_domain
);
789 drc_names
= g_string_append(drc_names
, drck
->get_name(drc
));
790 drc_names
= g_string_insert_len(drc_names
, -1, "\0", 1);
793 drc_types
= g_string_append(drc_types
,
794 spapr_drc_get_type_str(drc
->type
));
795 drc_types
= g_string_insert_len(drc_types
, -1, "\0", 1);
798 /* now write the drc count into the space we reserved at the
799 * beginning of the arrays previously
801 *(uint32_t *)drc_indexes
->data
= cpu_to_be32(drc_count
);
802 *(uint32_t *)drc_power_domains
->data
= cpu_to_be32(drc_count
);
803 *(uint32_t *)drc_names
->str
= cpu_to_be32(drc_count
);
804 *(uint32_t *)drc_types
->str
= cpu_to_be32(drc_count
);
806 ret
= fdt_setprop(fdt
, fdt_offset
, "ibm,drc-indexes",
808 drc_indexes
->len
* sizeof(uint32_t));
810 fprintf(stderr
, "Couldn't create ibm,drc-indexes property\n");
814 ret
= fdt_setprop(fdt
, fdt_offset
, "ibm,drc-power-domains",
815 drc_power_domains
->data
,
816 drc_power_domains
->len
* sizeof(uint32_t));
818 fprintf(stderr
, "Couldn't finalize ibm,drc-power-domains property\n");
822 ret
= fdt_setprop(fdt
, fdt_offset
, "ibm,drc-names",
823 drc_names
->str
, drc_names
->len
);
825 fprintf(stderr
, "Couldn't finalize ibm,drc-names property\n");
829 ret
= fdt_setprop(fdt
, fdt_offset
, "ibm,drc-types",
830 drc_types
->str
, drc_types
->len
);
832 fprintf(stderr
, "Couldn't finalize ibm,drc-types property\n");
837 g_array_free(drc_indexes
, true);
838 g_array_free(drc_power_domains
, true);
839 g_string_free(drc_names
, true);
840 g_string_free(drc_types
, true);