spapr: Eliminate spapr_drc_get_type_str()
[qemu/ar7.git] / hw / ppc / spapr_drc.c
blob06df5d0b1343b86b87aed27abf813c50cee825b5
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 "cpu.h"
16 #include "qemu/cutils.h"
17 #include "hw/ppc/spapr_drc.h"
18 #include "qom/object.h"
19 #include "hw/qdev.h"
20 #include "qapi/visitor.h"
21 #include "qemu/error-report.h"
22 #include "hw/ppc/spapr.h" /* for RTAS return codes */
23 #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
24 #include "trace.h"
26 #define DRC_CONTAINER_PATH "/dr-connector"
27 #define DRC_INDEX_TYPE_SHIFT 28
28 #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
30 sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc)
32 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
34 return 1 << drck->typeshift;
37 uint32_t spapr_drc_index(sPAPRDRConnector *drc)
39 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
41 /* no set format for a drc index: it only needs to be globally
42 * unique. this is how we encode the DRC type on bare-metal
43 * however, so might as well do that here
45 return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
46 | (drc->id & DRC_INDEX_ID_MASK);
49 static uint32_t set_isolation_state(sPAPRDRConnector *drc,
50 sPAPRDRIsolationState state)
52 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
54 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
56 /* if the guest is configuring a device attached to this DRC, we
57 * should reset the configuration state at this point since it may
58 * no longer be reliable (guest released device and needs to start
59 * over, or unplug occurred so the FDT is no longer valid)
61 if (state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
62 g_free(drc->ccs);
63 drc->ccs = NULL;
66 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
67 /* cannot unisolate a non-existent resource, and, or resources
68 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
70 if (!drc->dev ||
71 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
72 return RTAS_OUT_NO_SUCH_INDICATOR;
77 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
78 * belong to a DIMM device that is marked for removal.
80 * Currently the guest userspace tool drmgr that drives the memory
81 * hotplug/unplug will just try to remove a set of 'removable' LMBs
82 * in response to a hot unplug request that is based on drc-count.
83 * If the LMB being removed doesn't belong to a DIMM device that is
84 * actually being unplugged, fail the isolation request here.
86 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
87 if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
88 !drc->awaiting_release) {
89 return RTAS_OUT_HW_ERROR;
93 drc->isolation_state = state;
95 if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
96 /* if we're awaiting release, but still in an unconfigured state,
97 * it's likely the guest is still in the process of configuring
98 * the device and is transitioning the devices to an ISOLATED
99 * state as a part of that process. so we only complete the
100 * removal when this transition happens for a device in a
101 * configured state, as suggested by the state diagram from
102 * PAPR+ 2.7, 13.4
104 if (drc->awaiting_release) {
105 uint32_t drc_index = spapr_drc_index(drc);
106 if (drc->configured) {
107 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
108 drck->detach(drc, DEVICE(drc->dev), NULL);
109 } else {
110 trace_spapr_drc_set_isolation_state_deferring(drc_index);
113 drc->configured = false;
116 return RTAS_OUT_SUCCESS;
119 static uint32_t set_indicator_state(sPAPRDRConnector *drc,
120 sPAPRDRIndicatorState state)
122 trace_spapr_drc_set_indicator_state(spapr_drc_index(drc), state);
123 drc->indicator_state = state;
124 return RTAS_OUT_SUCCESS;
127 static uint32_t set_allocation_state(sPAPRDRConnector *drc,
128 sPAPRDRAllocationState state)
130 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
132 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
134 if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
135 /* if there's no resource/device associated with the DRC, there's
136 * no way for us to put it in an allocation state consistent with
137 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
138 * result in an RTAS return code of -3 / "no such indicator"
140 if (!drc->dev) {
141 return RTAS_OUT_NO_SUCH_INDICATOR;
143 if (drc->awaiting_release && drc->awaiting_allocation) {
144 /* kernel is acknowledging a previous hotplug event
145 * while we are already removing it.
146 * it's safe to ignore awaiting_allocation here since we know the
147 * situation is predicated on the guest either already having done
148 * so (boot-time hotplug), or never being able to acquire in the
149 * first place (hotplug followed by immediate unplug).
151 drc->awaiting_allocation_skippable = true;
152 return RTAS_OUT_NO_SUCH_INDICATOR;
156 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
157 drc->allocation_state = state;
158 if (drc->awaiting_release &&
159 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
160 uint32_t drc_index = spapr_drc_index(drc);
161 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
162 drck->detach(drc, DEVICE(drc->dev), NULL);
163 } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
164 drc->awaiting_allocation = false;
167 return RTAS_OUT_SUCCESS;
170 static const char *get_name(sPAPRDRConnector *drc)
172 return drc->name;
175 /* has the guest been notified of device attachment? */
176 static void set_signalled(sPAPRDRConnector *drc)
178 drc->signalled = true;
182 * dr-entity-sense sensor value
183 * returned via get-sensor-state RTAS calls
184 * as expected by state diagram in PAPR+ 2.7, 13.4
185 * based on the current allocation/indicator/power states
186 * for the DR connector.
188 static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
190 if (drc->dev) {
191 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
192 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
193 /* for logical DR, we return a state of UNUSABLE
194 * iff the allocation state UNUSABLE.
195 * Otherwise, report the state as USABLE/PRESENT,
196 * as we would for PCI.
198 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
199 } else {
200 /* this assumes all PCI devices are assigned to
201 * a 'live insertion' power domain, where QEMU
202 * manages power state automatically as opposed
203 * to the guest. present, non-PCI resources are
204 * unaffected by power state.
206 *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
208 } else {
209 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
210 /* PCI devices, and only PCI devices, use EMPTY
211 * in cases where we'd otherwise use UNUSABLE
213 *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
214 } else {
215 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
219 trace_spapr_drc_entity_sense(spapr_drc_index(drc), *state);
220 return RTAS_OUT_SUCCESS;
223 static void prop_get_index(Object *obj, Visitor *v, const char *name,
224 void *opaque, Error **errp)
226 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
227 uint32_t value = spapr_drc_index(drc);
228 visit_type_uint32(v, name, &value, errp);
231 static void prop_get_type(Object *obj, Visitor *v, const char *name,
232 void *opaque, Error **errp)
234 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
235 uint32_t value = (uint32_t)spapr_drc_type(drc);
236 visit_type_uint32(v, name, &value, errp);
239 static char *prop_get_name(Object *obj, Error **errp)
241 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
242 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
243 return g_strdup(drck->get_name(drc));
246 static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
247 void *opaque, Error **errp)
249 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
250 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
251 uint32_t value;
253 drck->entity_sense(drc, &value);
254 visit_type_uint32(v, name, &value, errp);
257 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
258 void *opaque, Error **errp)
260 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
261 Error *err = NULL;
262 int fdt_offset_next, fdt_offset, fdt_depth;
263 void *fdt;
265 if (!drc->fdt) {
266 visit_type_null(v, NULL, errp);
267 return;
270 fdt = drc->fdt;
271 fdt_offset = drc->fdt_start_offset;
272 fdt_depth = 0;
274 do {
275 const char *name = NULL;
276 const struct fdt_property *prop = NULL;
277 int prop_len = 0, name_len = 0;
278 uint32_t tag;
280 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
281 switch (tag) {
282 case FDT_BEGIN_NODE:
283 fdt_depth++;
284 name = fdt_get_name(fdt, fdt_offset, &name_len);
285 visit_start_struct(v, name, NULL, 0, &err);
286 if (err) {
287 error_propagate(errp, err);
288 return;
290 break;
291 case FDT_END_NODE:
292 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
293 g_assert(fdt_depth > 0);
294 visit_check_struct(v, &err);
295 visit_end_struct(v, NULL);
296 if (err) {
297 error_propagate(errp, err);
298 return;
300 fdt_depth--;
301 break;
302 case FDT_PROP: {
303 int i;
304 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
305 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
306 visit_start_list(v, name, NULL, 0, &err);
307 if (err) {
308 error_propagate(errp, err);
309 return;
311 for (i = 0; i < prop_len; i++) {
312 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
313 if (err) {
314 error_propagate(errp, err);
315 return;
318 visit_check_list(v, &err);
319 visit_end_list(v, NULL);
320 if (err) {
321 error_propagate(errp, err);
322 return;
324 break;
326 default:
327 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
329 fdt_offset = fdt_offset_next;
330 } while (fdt_depth != 0);
333 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
334 int fdt_start_offset, bool coldplug, Error **errp)
336 trace_spapr_drc_attach(spapr_drc_index(drc));
338 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
339 error_setg(errp, "an attached device is still awaiting release");
340 return;
342 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
343 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
345 g_assert(fdt || coldplug);
347 /* NOTE: setting initial isolation state to UNISOLATED means we can't
348 * detach unless guest has a userspace/kernel that moves this state
349 * back to ISOLATED in response to an unplug event, or this is done
350 * manually by the admin prior. if we force things while the guest
351 * may be accessing the device, we can easily crash the guest, so we
352 * we defer completion of removal in such cases to the reset() hook.
354 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
355 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
357 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
359 drc->dev = d;
360 drc->fdt = fdt;
361 drc->fdt_start_offset = fdt_start_offset;
362 drc->configured = coldplug;
363 /* 'logical' DR resources such as memory/cpus are in some cases treated
364 * as a pool of resources from which the guest is free to choose from
365 * based on only a count. for resources that can be assigned in this
366 * fashion, we must assume the resource is signalled immediately
367 * since a single hotplug request might make an arbitrary number of
368 * such attached resources available to the guest, as opposed to
369 * 'physical' DR resources such as PCI where each device/resource is
370 * signalled individually.
372 drc->signalled = (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI)
373 ? true : coldplug;
375 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
376 drc->awaiting_allocation = true;
379 object_property_add_link(OBJECT(drc), "device",
380 object_get_typename(OBJECT(drc->dev)),
381 (Object **)(&drc->dev),
382 NULL, 0, NULL);
385 static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
387 trace_spapr_drc_detach(spapr_drc_index(drc));
389 /* if we've signalled device presence to the guest, or if the guest
390 * has gone ahead and configured the device (via manually-executed
391 * device add via drmgr in guest, namely), we need to wait
392 * for the guest to quiesce the device before completing detach.
393 * Otherwise, we can assume the guest hasn't seen it and complete the
394 * detach immediately. Note that there is a small race window
395 * just before, or during, configuration, which is this context
396 * refers mainly to fetching the device tree via RTAS.
397 * During this window the device access will be arbitrated by
398 * associated DRC, which will simply fail the RTAS calls as invalid.
399 * This is recoverable within guest and current implementations of
400 * drmgr should be able to cope.
402 if (!drc->signalled && !drc->configured) {
403 /* if the guest hasn't seen the device we can't rely on it to
404 * set it back to an isolated state via RTAS, so do it here manually
406 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
409 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
410 trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
411 drc->awaiting_release = true;
412 return;
415 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
416 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
417 trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
418 drc->awaiting_release = true;
419 return;
422 if (drc->awaiting_allocation) {
423 if (!drc->awaiting_allocation_skippable) {
424 drc->awaiting_release = true;
425 trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
426 return;
430 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
432 /* Calling release callbacks based on spapr_drc_type(drc). */
433 switch (spapr_drc_type(drc)) {
434 case SPAPR_DR_CONNECTOR_TYPE_CPU:
435 spapr_core_release(drc->dev);
436 break;
437 case SPAPR_DR_CONNECTOR_TYPE_PCI:
438 spapr_phb_remove_pci_device_cb(drc->dev);
439 break;
440 case SPAPR_DR_CONNECTOR_TYPE_LMB:
441 spapr_lmb_release(drc->dev);
442 break;
443 case SPAPR_DR_CONNECTOR_TYPE_PHB:
444 case SPAPR_DR_CONNECTOR_TYPE_VIO:
445 default:
446 g_assert(false);
449 drc->awaiting_release = false;
450 drc->awaiting_allocation_skippable = false;
451 g_free(drc->fdt);
452 drc->fdt = NULL;
453 drc->fdt_start_offset = 0;
454 object_property_del(OBJECT(drc), "device", NULL);
455 drc->dev = NULL;
458 static bool release_pending(sPAPRDRConnector *drc)
460 return drc->awaiting_release;
463 static void reset(DeviceState *d)
465 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
466 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
467 sPAPRDREntitySense state;
469 trace_spapr_drc_reset(spapr_drc_index(drc));
471 g_free(drc->ccs);
472 drc->ccs = NULL;
474 /* immediately upon reset we can safely assume DRCs whose devices
475 * are pending removal can be safely removed, and that they will
476 * subsequently be left in an ISOLATED state. move the DRC to this
477 * state in these cases (which will in turn complete any pending
478 * device removals)
480 if (drc->awaiting_release) {
481 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
482 /* generally this should also finalize the removal, but if the device
483 * hasn't yet been configured we normally defer removal under the
484 * assumption that this transition is taking place as part of device
485 * configuration. so check if we're still waiting after this, and
486 * force removal if we are
488 if (drc->awaiting_release) {
489 drck->detach(drc, DEVICE(drc->dev), NULL);
492 /* non-PCI devices may be awaiting a transition to UNUSABLE */
493 if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
494 drc->awaiting_release) {
495 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
499 drck->entity_sense(drc, &state);
500 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
501 drck->set_signalled(drc);
505 static bool spapr_drc_needed(void *opaque)
507 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
508 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
509 bool rc = false;
510 sPAPRDREntitySense value;
511 drck->entity_sense(drc, &value);
513 /* If no dev is plugged in there is no need to migrate the DRC state */
514 if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
515 return false;
519 * If there is dev plugged in, we need to migrate the DRC state when
520 * it is different from cold-plugged state
522 switch (spapr_drc_type(drc)) {
523 case SPAPR_DR_CONNECTOR_TYPE_PCI:
524 case SPAPR_DR_CONNECTOR_TYPE_CPU:
525 case SPAPR_DR_CONNECTOR_TYPE_LMB:
526 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
527 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
528 drc->configured && drc->signalled && !drc->awaiting_release);
529 break;
530 case SPAPR_DR_CONNECTOR_TYPE_PHB:
531 case SPAPR_DR_CONNECTOR_TYPE_VIO:
532 default:
533 g_assert_not_reached();
535 return rc;
538 static const VMStateDescription vmstate_spapr_drc = {
539 .name = "spapr_drc",
540 .version_id = 1,
541 .minimum_version_id = 1,
542 .needed = spapr_drc_needed,
543 .fields = (VMStateField []) {
544 VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
545 VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
546 VMSTATE_UINT32(indicator_state, sPAPRDRConnector),
547 VMSTATE_BOOL(configured, sPAPRDRConnector),
548 VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
549 VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
550 VMSTATE_BOOL(signalled, sPAPRDRConnector),
551 VMSTATE_END_OF_LIST()
555 static void realize(DeviceState *d, Error **errp)
557 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
558 Object *root_container;
559 char link_name[256];
560 gchar *child_name;
561 Error *err = NULL;
563 trace_spapr_drc_realize(spapr_drc_index(drc));
564 /* NOTE: we do this as part of realize/unrealize due to the fact
565 * that the guest will communicate with the DRC via RTAS calls
566 * referencing the global DRC index. By unlinking the DRC
567 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
568 * inaccessible by the guest, since lookups rely on this path
569 * existing in the composition tree
571 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
572 snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
573 child_name = object_get_canonical_path_component(OBJECT(drc));
574 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
575 object_property_add_alias(root_container, link_name,
576 drc->owner, child_name, &err);
577 if (err) {
578 error_report_err(err);
579 object_unref(OBJECT(drc));
581 g_free(child_name);
582 vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
583 drc);
584 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
587 static void unrealize(DeviceState *d, Error **errp)
589 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
590 Object *root_container;
591 char name[256];
592 Error *err = NULL;
594 trace_spapr_drc_unrealize(spapr_drc_index(drc));
595 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
596 snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
597 object_property_del(root_container, name, &err);
598 if (err) {
599 error_report_err(err);
600 object_unref(OBJECT(drc));
604 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
605 uint32_t id)
607 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
608 char *prop_name;
610 drc->id = id;
611 drc->owner = owner;
612 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
613 spapr_drc_index(drc));
614 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
615 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
616 g_free(prop_name);
618 /* human-readable name for a DRC to encode into the DT
619 * description. this is mainly only used within a guest in place
620 * of the unique DRC index.
622 * in the case of VIO/PCI devices, it corresponds to a
623 * "location code" that maps a logical device/function (DRC index)
624 * to a physical (or virtual in the case of VIO) location in the
625 * system by chaining together the "location label" for each
626 * encapsulating component.
628 * since this is more to do with diagnosing physical hardware
629 * issues than guest compatibility, we choose location codes/DRC
630 * names that adhere to the documented format, but avoid encoding
631 * the entire topology information into the label/code, instead
632 * just using the location codes based on the labels for the
633 * endpoints (VIO/PCI adaptor connectors), which is basically
634 * just "C" followed by an integer ID.
636 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
637 * location codes as documented by PAPR+ v2.7, 12.3.1.5
639 switch (spapr_drc_type(drc)) {
640 case SPAPR_DR_CONNECTOR_TYPE_CPU:
641 drc->name = g_strdup_printf("CPU %d", id);
642 break;
643 case SPAPR_DR_CONNECTOR_TYPE_PHB:
644 drc->name = g_strdup_printf("PHB %d", id);
645 break;
646 case SPAPR_DR_CONNECTOR_TYPE_VIO:
647 case SPAPR_DR_CONNECTOR_TYPE_PCI:
648 drc->name = g_strdup_printf("C%d", id);
649 break;
650 case SPAPR_DR_CONNECTOR_TYPE_LMB:
651 drc->name = g_strdup_printf("LMB %d", id);
652 break;
653 default:
654 g_assert(false);
657 /* PCI slot always start in a USABLE state, and stay there */
658 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
659 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
662 return drc;
665 static void spapr_dr_connector_instance_init(Object *obj)
667 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
669 object_property_add_uint32_ptr(obj, "isolation-state",
670 &drc->isolation_state, NULL);
671 object_property_add_uint32_ptr(obj, "indicator-state",
672 &drc->indicator_state, NULL);
673 object_property_add_uint32_ptr(obj, "allocation-state",
674 &drc->allocation_state, NULL);
675 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
676 object_property_add(obj, "index", "uint32", prop_get_index,
677 NULL, NULL, NULL, NULL);
678 object_property_add(obj, "connector_type", "uint32", prop_get_type,
679 NULL, NULL, NULL, NULL);
680 object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
681 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
682 NULL, NULL, NULL, NULL);
683 object_property_add(obj, "fdt", "struct", prop_get_fdt,
684 NULL, NULL, NULL, NULL);
687 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
689 DeviceClass *dk = DEVICE_CLASS(k);
690 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
692 dk->reset = reset;
693 dk->realize = realize;
694 dk->unrealize = unrealize;
695 drck->set_isolation_state = set_isolation_state;
696 drck->set_indicator_state = set_indicator_state;
697 drck->set_allocation_state = set_allocation_state;
698 drck->get_name = get_name;
699 drck->entity_sense = entity_sense;
700 drck->attach = attach;
701 drck->detach = detach;
702 drck->release_pending = release_pending;
703 drck->set_signalled = set_signalled;
705 * Reason: it crashes FIXME find and document the real reason
707 dk->user_creatable = false;
710 static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
712 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
714 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
715 drck->typename = "CPU";
718 static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
720 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
722 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
723 drck->typename = "28";
726 static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
728 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
730 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
731 drck->typename = "MEM";
734 static const TypeInfo spapr_dr_connector_info = {
735 .name = TYPE_SPAPR_DR_CONNECTOR,
736 .parent = TYPE_DEVICE,
737 .instance_size = sizeof(sPAPRDRConnector),
738 .instance_init = spapr_dr_connector_instance_init,
739 .class_size = sizeof(sPAPRDRConnectorClass),
740 .class_init = spapr_dr_connector_class_init,
741 .abstract = true,
744 static const TypeInfo spapr_drc_physical_info = {
745 .name = TYPE_SPAPR_DRC_PHYSICAL,
746 .parent = TYPE_SPAPR_DR_CONNECTOR,
747 .instance_size = sizeof(sPAPRDRConnector),
748 .abstract = true,
751 static const TypeInfo spapr_drc_logical_info = {
752 .name = TYPE_SPAPR_DRC_LOGICAL,
753 .parent = TYPE_SPAPR_DR_CONNECTOR,
754 .instance_size = sizeof(sPAPRDRConnector),
755 .abstract = true,
758 static const TypeInfo spapr_drc_cpu_info = {
759 .name = TYPE_SPAPR_DRC_CPU,
760 .parent = TYPE_SPAPR_DRC_LOGICAL,
761 .instance_size = sizeof(sPAPRDRConnector),
762 .class_init = spapr_drc_cpu_class_init,
765 static const TypeInfo spapr_drc_pci_info = {
766 .name = TYPE_SPAPR_DRC_PCI,
767 .parent = TYPE_SPAPR_DRC_PHYSICAL,
768 .instance_size = sizeof(sPAPRDRConnector),
769 .class_init = spapr_drc_pci_class_init,
772 static const TypeInfo spapr_drc_lmb_info = {
773 .name = TYPE_SPAPR_DRC_LMB,
774 .parent = TYPE_SPAPR_DRC_LOGICAL,
775 .instance_size = sizeof(sPAPRDRConnector),
776 .class_init = spapr_drc_lmb_class_init,
779 /* helper functions for external users */
781 sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
783 Object *obj;
784 char name[256];
786 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
787 obj = object_resolve_path(name, NULL);
789 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
792 sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
794 sPAPRDRConnectorClass *drck
795 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
797 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
798 | (id & DRC_INDEX_ID_MASK));
802 * spapr_drc_populate_dt
804 * @fdt: libfdt device tree
805 * @path: path in the DT to generate properties
806 * @owner: parent Object/DeviceState for which to generate DRC
807 * descriptions for
808 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
809 * to the types of DRCs to generate entries for
811 * generate OF properties to describe DRC topology/indices to guests
813 * as documented in PAPR+ v2.1, 13.5.2
815 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
816 uint32_t drc_type_mask)
818 Object *root_container;
819 ObjectProperty *prop;
820 ObjectPropertyIterator iter;
821 uint32_t drc_count = 0;
822 GArray *drc_indexes, *drc_power_domains;
823 GString *drc_names, *drc_types;
824 int ret;
826 /* the first entry of each properties is a 32-bit integer encoding
827 * the number of elements in the array. we won't know this until
828 * we complete the iteration through all the matching DRCs, but
829 * reserve the space now and set the offsets accordingly so we
830 * can fill them in later.
832 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
833 drc_indexes = g_array_set_size(drc_indexes, 1);
834 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
835 drc_power_domains = g_array_set_size(drc_power_domains, 1);
836 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
837 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
839 /* aliases for all DRConnector objects will be rooted in QOM
840 * composition tree at DRC_CONTAINER_PATH
842 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
844 object_property_iter_init(&iter, root_container);
845 while ((prop = object_property_iter_next(&iter))) {
846 Object *obj;
847 sPAPRDRConnector *drc;
848 sPAPRDRConnectorClass *drck;
849 uint32_t drc_index, drc_power_domain;
851 if (!strstart(prop->type, "link<", NULL)) {
852 continue;
855 obj = object_property_get_link(root_container, prop->name, NULL);
856 drc = SPAPR_DR_CONNECTOR(obj);
857 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
859 if (owner && (drc->owner != owner)) {
860 continue;
863 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
864 continue;
867 drc_count++;
869 /* ibm,drc-indexes */
870 drc_index = cpu_to_be32(spapr_drc_index(drc));
871 g_array_append_val(drc_indexes, drc_index);
873 /* ibm,drc-power-domains */
874 drc_power_domain = cpu_to_be32(-1);
875 g_array_append_val(drc_power_domains, drc_power_domain);
877 /* ibm,drc-names */
878 drc_names = g_string_append(drc_names, drck->get_name(drc));
879 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
881 /* ibm,drc-types */
882 drc_types = g_string_append(drc_types, drck->typename);
883 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
886 /* now write the drc count into the space we reserved at the
887 * beginning of the arrays previously
889 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
890 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
891 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
892 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
894 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
895 drc_indexes->data,
896 drc_indexes->len * sizeof(uint32_t));
897 if (ret) {
898 error_report("Couldn't create ibm,drc-indexes property");
899 goto out;
902 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
903 drc_power_domains->data,
904 drc_power_domains->len * sizeof(uint32_t));
905 if (ret) {
906 error_report("Couldn't finalize ibm,drc-power-domains property");
907 goto out;
910 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
911 drc_names->str, drc_names->len);
912 if (ret) {
913 error_report("Couldn't finalize ibm,drc-names property");
914 goto out;
917 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
918 drc_types->str, drc_types->len);
919 if (ret) {
920 error_report("Couldn't finalize ibm,drc-types property");
921 goto out;
924 out:
925 g_array_free(drc_indexes, true);
926 g_array_free(drc_power_domains, true);
927 g_string_free(drc_names, true);
928 g_string_free(drc_types, true);
930 return ret;
934 * RTAS calls
937 static bool sensor_type_is_dr(uint32_t sensor_type)
939 switch (sensor_type) {
940 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
941 case RTAS_SENSOR_TYPE_DR:
942 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
943 return true;
946 return false;
949 static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
950 uint32_t token, uint32_t nargs,
951 target_ulong args, uint32_t nret,
952 target_ulong rets)
954 uint32_t sensor_type;
955 uint32_t sensor_index;
956 uint32_t sensor_state;
957 uint32_t ret = RTAS_OUT_SUCCESS;
958 sPAPRDRConnector *drc;
959 sPAPRDRConnectorClass *drck;
961 if (nargs != 3 || nret != 1) {
962 ret = RTAS_OUT_PARAM_ERROR;
963 goto out;
966 sensor_type = rtas_ld(args, 0);
967 sensor_index = rtas_ld(args, 1);
968 sensor_state = rtas_ld(args, 2);
970 if (!sensor_type_is_dr(sensor_type)) {
971 goto out_unimplemented;
974 /* if this is a DR sensor we can assume sensor_index == drc_index */
975 drc = spapr_drc_by_index(sensor_index);
976 if (!drc) {
977 trace_spapr_rtas_set_indicator_invalid(sensor_index);
978 ret = RTAS_OUT_PARAM_ERROR;
979 goto out;
981 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
983 switch (sensor_type) {
984 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
985 ret = drck->set_isolation_state(drc, sensor_state);
986 break;
987 case RTAS_SENSOR_TYPE_DR:
988 ret = drck->set_indicator_state(drc, sensor_state);
989 break;
990 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
991 ret = drck->set_allocation_state(drc, sensor_state);
992 break;
993 default:
994 goto out_unimplemented;
997 out:
998 rtas_st(rets, 0, ret);
999 return;
1001 out_unimplemented:
1002 /* currently only DR-related sensors are implemented */
1003 trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
1004 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
1007 static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
1008 uint32_t token, uint32_t nargs,
1009 target_ulong args, uint32_t nret,
1010 target_ulong rets)
1012 uint32_t sensor_type;
1013 uint32_t sensor_index;
1014 uint32_t sensor_state = 0;
1015 sPAPRDRConnector *drc;
1016 sPAPRDRConnectorClass *drck;
1017 uint32_t ret = RTAS_OUT_SUCCESS;
1019 if (nargs != 2 || nret != 2) {
1020 ret = RTAS_OUT_PARAM_ERROR;
1021 goto out;
1024 sensor_type = rtas_ld(args, 0);
1025 sensor_index = rtas_ld(args, 1);
1027 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1028 /* currently only DR-related sensors are implemented */
1029 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1030 sensor_type);
1031 ret = RTAS_OUT_NOT_SUPPORTED;
1032 goto out;
1035 drc = spapr_drc_by_index(sensor_index);
1036 if (!drc) {
1037 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1038 ret = RTAS_OUT_PARAM_ERROR;
1039 goto out;
1041 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1042 ret = drck->entity_sense(drc, &sensor_state);
1044 out:
1045 rtas_st(rets, 0, ret);
1046 rtas_st(rets, 1, sensor_state);
1049 /* configure-connector work area offsets, int32_t units for field
1050 * indexes, bytes for field offset/len values.
1052 * as documented by PAPR+ v2.7, 13.5.3.5
1054 #define CC_IDX_NODE_NAME_OFFSET 2
1055 #define CC_IDX_PROP_NAME_OFFSET 2
1056 #define CC_IDX_PROP_LEN 3
1057 #define CC_IDX_PROP_DATA_OFFSET 4
1058 #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1059 #define CC_WA_LEN 4096
1061 static void configure_connector_st(target_ulong addr, target_ulong offset,
1062 const void *buf, size_t len)
1064 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1065 buf, MIN(len, CC_WA_LEN - offset));
1068 static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1069 sPAPRMachineState *spapr,
1070 uint32_t token, uint32_t nargs,
1071 target_ulong args, uint32_t nret,
1072 target_ulong rets)
1074 uint64_t wa_addr;
1075 uint64_t wa_offset;
1076 uint32_t drc_index;
1077 sPAPRDRConnector *drc;
1078 sPAPRConfigureConnectorState *ccs;
1079 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1080 int rc;
1082 if (nargs != 2 || nret != 1) {
1083 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1084 return;
1087 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1089 drc_index = rtas_ld(wa_addr, 0);
1090 drc = spapr_drc_by_index(drc_index);
1091 if (!drc) {
1092 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1093 rc = RTAS_OUT_PARAM_ERROR;
1094 goto out;
1097 if (!drc->fdt) {
1098 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1099 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1100 goto out;
1103 ccs = drc->ccs;
1104 if (!ccs) {
1105 ccs = g_new0(sPAPRConfigureConnectorState, 1);
1106 ccs->fdt_offset = drc->fdt_start_offset;
1107 drc->ccs = ccs;
1110 do {
1111 uint32_t tag;
1112 const char *name;
1113 const struct fdt_property *prop;
1114 int fdt_offset_next, prop_len;
1116 tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1118 switch (tag) {
1119 case FDT_BEGIN_NODE:
1120 ccs->fdt_depth++;
1121 name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1123 /* provide the name of the next OF node */
1124 wa_offset = CC_VAL_DATA_OFFSET;
1125 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1126 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1127 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1128 break;
1129 case FDT_END_NODE:
1130 ccs->fdt_depth--;
1131 if (ccs->fdt_depth == 0) {
1132 sPAPRDRIsolationState state = drc->isolation_state;
1133 uint32_t drc_index = spapr_drc_index(drc);
1134 /* done sending the device tree, don't need to track
1135 * the state anymore
1137 trace_spapr_drc_set_configured(drc_index);
1138 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
1139 drc->configured = true;
1140 } else {
1141 /* guest should be not configuring an isolated device */
1142 trace_spapr_drc_set_configured_skipping(drc_index);
1144 g_free(ccs);
1145 drc->ccs = NULL;
1146 ccs = NULL;
1147 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1148 } else {
1149 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1151 break;
1152 case FDT_PROP:
1153 prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1154 &prop_len);
1155 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1157 /* provide the name of the next OF property */
1158 wa_offset = CC_VAL_DATA_OFFSET;
1159 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1160 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1162 /* provide the length and value of the OF property. data gets
1163 * placed immediately after NULL terminator of the OF property's
1164 * name string
1166 wa_offset += strlen(name) + 1,
1167 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1168 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1169 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1170 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1171 break;
1172 case FDT_END:
1173 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1174 default:
1175 /* keep seeking for an actionable tag */
1176 break;
1178 if (ccs) {
1179 ccs->fdt_offset = fdt_offset_next;
1181 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1183 rc = resp;
1184 out:
1185 rtas_st(rets, 0, rc);
1188 static void spapr_drc_register_types(void)
1190 type_register_static(&spapr_dr_connector_info);
1191 type_register_static(&spapr_drc_physical_info);
1192 type_register_static(&spapr_drc_logical_info);
1193 type_register_static(&spapr_drc_cpu_info);
1194 type_register_static(&spapr_drc_pci_info);
1195 type_register_static(&spapr_drc_lmb_info);
1197 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1198 rtas_set_indicator);
1199 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1200 rtas_get_sensor_state);
1201 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1202 rtas_ibm_configure_connector);
1204 type_init(spapr_drc_register_types)