1/*
2 * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
3 *
4 * Copyright IBM Corp. 2014
5 *
6 * Authors:
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
8 *
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.
11 */
12
13#include "qemu/osdep.h"
14#include "qapi/error.h"
15#include "qapi/qmp/qnull.h"
16#include "cpu.h"
17#include "qemu/cutils.h"
18#include "hw/ppc/spapr_drc.h"
19#include "qom/object.h"
20#include "migration/vmstate.h"
21#include "qapi/visitor.h"
22#include "qemu/error-report.h"
23#include "hw/ppc/spapr.h" /* for RTAS return codes */
24#include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
25#include "sysemu/device_tree.h"
26#include "sysemu/reset.h"
27#include "trace.h"
28
29#define DRC_CONTAINER_PATH "/dr-connector"
30#define DRC_INDEX_TYPE_SHIFT 28
31#define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
32
33SpaprDrcType spapr_drc_type(SpaprDrc *drc)
34{
35 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
36
37 return 1 << drck->typeshift;
38}
39
40uint32_t spapr_drc_index(SpaprDrc *drc)
41{
42 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
43
44 /* no set format for a drc index: it only needs to be globally
45 * unique. this is how we encode the DRC type on bare-metal
46 * however, so might as well do that here
47 */
48 return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
49 | (drc->id & DRC_INDEX_ID_MASK);
50}
51
52static uint32_t drc_isolate_physical(SpaprDrc *drc)
53{
54 switch (drc->state) {
55 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
56 return RTAS_OUT_SUCCESS; /* Nothing to do */
57 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
58 break; /* see below */
59 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
60 return RTAS_OUT_PARAM_ERROR; /* not allowed */
61 default:
62 g_assert_not_reached();
63 }
64
65 drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
66
67 if (drc->unplug_requested) {
68 uint32_t drc_index = spapr_drc_index(drc);
69 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
70 spapr_drc_detach(drc);
71 }
72
73 return RTAS_OUT_SUCCESS;
74}
75
76static uint32_t drc_unisolate_physical(SpaprDrc *drc)
77{
78 switch (drc->state) {
79 case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
80 case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
81 return RTAS_OUT_SUCCESS; /* Nothing to do */
82 case SPAPR_DRC_STATE_PHYSICAL_POWERON:
83 break; /* see below */
84 default:
85 g_assert_not_reached();
86 }
87
88 /* cannot unisolate a non-existent resource, and, or resources
89 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
90 * 13.5.3.5)
91 */
92 if (!drc->dev) {
93 return RTAS_OUT_NO_SUCH_INDICATOR;
94 }
95
96 drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE;
97 drc->ccs_offset = drc->fdt_start_offset;
98 drc->ccs_depth = 0;
99
100 return RTAS_OUT_SUCCESS;
101}
102
103static uint32_t drc_isolate_logical(SpaprDrc *drc)
104{
105 switch (drc->state) {
106 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
107 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
108 return RTAS_OUT_SUCCESS; /* Nothing to do */
109 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
110 break; /* see below */
111 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
112 return RTAS_OUT_PARAM_ERROR; /* not allowed */
113 default:
114 g_assert_not_reached();
115 }
116
117 /*
118 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
119 * belong to a DIMM device that is marked for removal.
120 *
121 * Currently the guest userspace tool drmgr that drives the memory
122 * hotplug/unplug will just try to remove a set of 'removable' LMBs
123 * in response to a hot unplug request that is based on drc-count.
124 * If the LMB being removed doesn't belong to a DIMM device that is
125 * actually being unplugged, fail the isolation request here.
126 */
127 if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
128 && !drc->unplug_requested) {
129 return RTAS_OUT_HW_ERROR;
130 }
131
132 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
133
134 /* if we're awaiting release, but still in an unconfigured state,
135 * it's likely the guest is still in the process of configuring
136 * the device and is transitioning the devices to an ISOLATED
137 * state as a part of that process. so we only complete the
138 * removal when this transition happens for a device in a
139 * configured state, as suggested by the state diagram from PAPR+
140 * 2.7, 13.4
141 */
142 if (drc->unplug_requested) {
143 uint32_t drc_index = spapr_drc_index(drc);
144 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
145 spapr_drc_detach(drc);
146 }
147 return RTAS_OUT_SUCCESS;
148}
149
150static uint32_t drc_unisolate_logical(SpaprDrc *drc)
151{
152 switch (drc->state) {
153 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
154 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
155 return RTAS_OUT_SUCCESS; /* Nothing to do */
156 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
157 break; /* see below */
158 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
159 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
160 default:
161 g_assert_not_reached();
162 }
163
164 /* Move to AVAILABLE state should have ensured device was present */
165 g_assert(drc->dev);
166
167 drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
168 drc->ccs_offset = drc->fdt_start_offset;
169 drc->ccs_depth = 0;
170
171 return RTAS_OUT_SUCCESS;
172}
173
174static uint32_t drc_set_usable(SpaprDrc *drc)
175{
176 switch (drc->state) {
177 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
178 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
179 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
180 return RTAS_OUT_SUCCESS; /* Nothing to do */
181 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
182 break; /* see below */
183 default:
184 g_assert_not_reached();
185 }
186
187 /* if there's no resource/device associated with the DRC, there's
188 * no way for us to put it in an allocation state consistent with
189 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
190 * result in an RTAS return code of -3 / "no such indicator"
191 */
192 if (!drc->dev) {
193 return RTAS_OUT_NO_SUCH_INDICATOR;
194 }
195 if (drc->unplug_requested) {
196 /* Don't allow the guest to move a device away from UNUSABLE
197 * state when we want to unplug it */
198 return RTAS_OUT_NO_SUCH_INDICATOR;
199 }
200
201 drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
202
203 return RTAS_OUT_SUCCESS;
204}
205
206static uint32_t drc_set_unusable(SpaprDrc *drc)
207{
208 switch (drc->state) {
209 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
210 return RTAS_OUT_SUCCESS; /* Nothing to do */
211 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
212 break; /* see below */
213 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
214 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
215 return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
216 default:
217 g_assert_not_reached();
218 }
219
220 drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
221 if (drc->unplug_requested) {
222 uint32_t drc_index = spapr_drc_index(drc);
223 trace_spapr_drc_set_allocation_state_finalizing(drc_index);
224 spapr_drc_detach(drc);
225 }
226
227 return RTAS_OUT_SUCCESS;
228}
229
230static char *spapr_drc_name(SpaprDrc *drc)
231{
232 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
233
234 /* human-readable name for a DRC to encode into the DT
235 * description. this is mainly only used within a guest in place
236 * of the unique DRC index.
237 *
238 * in the case of VIO/PCI devices, it corresponds to a "location
239 * code" that maps a logical device/function (DRC index) to a
240 * physical (or virtual in the case of VIO) location in the system
241 * by chaining together the "location label" for each
242 * encapsulating component.
243 *
244 * since this is more to do with diagnosing physical hardware
245 * issues than guest compatibility, we choose location codes/DRC
246 * names that adhere to the documented format, but avoid encoding
247 * the entire topology information into the label/code, instead
248 * just using the location codes based on the labels for the
249 * endpoints (VIO/PCI adaptor connectors), which is basically just
250 * "C" followed by an integer ID.
251 *
252 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
253 * location codes as documented by PAPR+ v2.7, 12.3.1.5
254 */
255 return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
256}
257
258/*
259 * dr-entity-sense sensor value
260 * returned via get-sensor-state RTAS calls
261 * as expected by state diagram in PAPR+ 2.7, 13.4
262 * based on the current allocation/indicator/power states
263 * for the DR connector.
264 */
265static SpaprDREntitySense physical_entity_sense(SpaprDrc *drc)
266{
267 /* this assumes all PCI devices are assigned to a 'live insertion'
268 * power domain, where QEMU manages power state automatically as
269 * opposed to the guest. present, non-PCI resources are unaffected
270 * by power state.
271 */
272 return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
273 : SPAPR_DR_ENTITY_SENSE_EMPTY;
274}
275
276static SpaprDREntitySense logical_entity_sense(SpaprDrc *drc)
277{
278 switch (drc->state) {
279 case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
280 return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
281 case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
282 case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
283 case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
284 g_assert(drc->dev);
285 return SPAPR_DR_ENTITY_SENSE_PRESENT;
286 default:
287 g_assert_not_reached();
288 }
289}
290
291static void prop_get_index(Object *obj, Visitor *v, const char *name,
292 void *opaque, Error **errp)
293{
294 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
295 uint32_t value = spapr_drc_index(drc);
296 visit_type_uint32(v, name, &value, errp);
297}
298
299static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
300 void *opaque, Error **errp)
301{
302 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
303 QNull *null = NULL;
304 Error *err = NULL;
305 int fdt_offset_next, fdt_offset, fdt_depth;
306 void *fdt;
307
308 if (!drc->fdt) {
309 visit_type_null(v, NULL, &null, errp);
310 qobject_unref(null);
311 return;
312 }
313
314 fdt = drc->fdt;
315 fdt_offset = drc->fdt_start_offset;
316 fdt_depth = 0;
317
318 do {
319 const char *name = NULL;
320 const struct fdt_property *prop = NULL;
321 int prop_len = 0, name_len = 0;
322 uint32_t tag;
323
324 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
325 switch (tag) {
326 case FDT_BEGIN_NODE:
327 fdt_depth++;
328 name = fdt_get_name(fdt, fdt_offset, &name_len);
329 visit_start_struct(v, name, NULL, 0, &err);
330 if (err) {
331 error_propagate(errp, err);
332 return;
333 }
334 break;
335 case FDT_END_NODE:
336 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
337 g_assert(fdt_depth > 0);
338 visit_check_struct(v, &err);
339 visit_end_struct(v, NULL);
340 if (err) {
341 error_propagate(errp, err);
342 return;
343 }
344 fdt_depth--;
345 break;
346 case FDT_PROP: {
347 int i;
348 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
349 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
350 visit_start_list(v, name, NULL, 0, &err);
351 if (err) {
352 error_propagate(errp, err);
353 return;
354 }
355 for (i = 0; i < prop_len; i++) {
356 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
357 if (err) {
358 error_propagate(errp, err);
359 return;
360 }
361 }
362 visit_check_list(v, &err);
363 visit_end_list(v, NULL);
364 if (err) {
365 error_propagate(errp, err);
366 return;
367 }
368 break;
369 }
370 default:
371 error_report("device FDT in unexpected state: %d", tag);
372 abort();
373 }
374 fdt_offset = fdt_offset_next;
375 } while (fdt_depth != 0);
376}
377
378void spapr_drc_attach(SpaprDrc *drc, DeviceState *d, Error **errp)
379{
380 trace_spapr_drc_attach(spapr_drc_index(drc));
381
382 if (drc->dev) {
383 error_setg(errp, "an attached device is still awaiting release");
384 return;
385 }
386 g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
387 || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
388
389 drc->dev = d;
390
391 object_property_add_link(OBJECT(drc), "device",
392 object_get_typename(OBJECT(drc->dev)),
393 (Object **)(&drc->dev),
394 NULL, 0, NULL);
395}
396
397static void spapr_drc_release(SpaprDrc *drc)
398{
399 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
400
401 drck->release(drc->dev);
402
403 drc->unplug_requested = false;
404 g_free(drc->fdt);
405 drc->fdt = NULL;
406 drc->fdt_start_offset = 0;
407 object_property_del(OBJECT(drc), "device", &error_abort);
408 drc->dev = NULL;
409}
410
411void spapr_drc_detach(SpaprDrc *drc)
412{
413 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
414
415 trace_spapr_drc_detach(spapr_drc_index(drc));
416
417 g_assert(drc->dev);
418
419 drc->unplug_requested = true;
420
421 if (drc->state != drck->empty_state) {
422 trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
423 return;
424 }
425
426 spapr_drc_release(drc);
427}
428
429void spapr_drc_reset(SpaprDrc *drc)
430{
431 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
432
433 trace_spapr_drc_reset(spapr_drc_index(drc));
434
435 /* immediately upon reset we can safely assume DRCs whose devices
436 * are pending removal can be safely removed.
437 */
438 if (drc->unplug_requested) {
439 spapr_drc_release(drc);
440 }
441
442 if (drc->dev) {
443 /* A device present at reset is ready to go, same as coldplugged */
444 drc->state = drck->ready_state;
445 /*
446 * Ensure that we are able to send the FDT fragment again
447 * via configure-connector call if the guest requests.
448 */
449 drc->ccs_offset = drc->fdt_start_offset;
450 drc->ccs_depth = 0;
451 } else {
452 drc->state = drck->empty_state;
453 drc->ccs_offset = -1;
454 drc->ccs_depth = -1;
455 }
456}
457
458bool spapr_drc_needed(void *opaque)
459{
460 SpaprDrc *drc = (SpaprDrc *)opaque;
461 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
462
463 /* If no dev is plugged in there is no need to migrate the DRC state */
464 if (!drc->dev) {
465 return false;
466 }
467
468 /*
469 * We need to migrate the state if it's not equal to the expected
470 * long-term state, which is the same as the coldplugged initial
471 * state */
472 return (drc->state != drck->ready_state);
473}
474
475static const VMStateDescription vmstate_spapr_drc = {
476 .name = "spapr_drc",
477 .version_id = 1,
478 .minimum_version_id = 1,
479 .needed = spapr_drc_needed,
480 .fields = (VMStateField []) {
481 VMSTATE_UINT32(state, SpaprDrc),
482 VMSTATE_END_OF_LIST()
483 }
484};
485
486static void realize(DeviceState *d, Error **errp)
487{
488 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
489 Object *root_container;
490 gchar *link_name;
491 gchar *child_name;
492 Error *err = NULL;
493
494 trace_spapr_drc_realize(spapr_drc_index(drc));
495 /* NOTE: we do this as part of realize/unrealize due to the fact
496 * that the guest will communicate with the DRC via RTAS calls
497 * referencing the global DRC index. By unlinking the DRC
498 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
499 * inaccessible by the guest, since lookups rely on this path
500 * existing in the composition tree
501 */
502 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
503 link_name = g_strdup_printf("%x", spapr_drc_index(drc));
504 child_name = object_get_canonical_path_component(OBJECT(drc));
505 trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
506 object_property_add_alias(root_container, link_name,
507 drc->owner, child_name, &err);
508 g_free(child_name);
509 g_free(link_name);
510 if (err) {
511 error_propagate(errp, err);
512 return;
513 }
514 vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
515 drc);
516 trace_spapr_drc_realize_complete(spapr_drc_index(drc));
517}
518
519static void unrealize(DeviceState *d, Error **errp)
520{
521 SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
522 Object *root_container;
523 gchar *name;
524
525 trace_spapr_drc_unrealize(spapr_drc_index(drc));
526 vmstate_unregister(DEVICE(drc), &vmstate_spapr_drc, drc);
527 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
528 name = g_strdup_printf("%x", spapr_drc_index(drc));
529 object_property_del(root_container, name, errp);
530 g_free(name);
531}
532
533SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
534 uint32_t id)
535{
536 SpaprDrc *drc = SPAPR_DR_CONNECTOR(object_new(type));
537 char *prop_name;
538
539 drc->id = id;
540 drc->owner = owner;
541 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
542 spapr_drc_index(drc));
543 object_property_add_child(owner, prop_name, OBJECT(drc), &error_abort);
544 object_unref(OBJECT(drc));
545 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
546 g_free(prop_name);
547
548 return drc;
549}
550
551static void spapr_dr_connector_instance_init(Object *obj)
552{
553 SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
554 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
555
556 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
557 object_property_add(obj, "index", "uint32", prop_get_index,
558 NULL, NULL, NULL, NULL);
559 object_property_add(obj, "fdt", "struct", prop_get_fdt,
560 NULL, NULL, NULL, NULL);
561 drc->state = drck->empty_state;
562}
563
564static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
565{
566 DeviceClass *dk = DEVICE_CLASS(k);
567
568 dk->realize = realize;
569 dk->unrealize = unrealize;
570 /*
571 * Reason: it crashes FIXME find and document the real reason
572 */
573 dk->user_creatable = false;
574}
575
576static bool drc_physical_needed(void *opaque)
577{
578 SpaprDrcPhysical *drcp = (SpaprDrcPhysical *)opaque;
579 SpaprDrc *drc = SPAPR_DR_CONNECTOR(drcp);
580
581 if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
582 || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
583 return false;
584 }
585 return true;
586}
587
588static const VMStateDescription vmstate_spapr_drc_physical = {
589 .name = "spapr_drc/physical",
590 .version_id = 1,
591 .minimum_version_id = 1,
592 .needed = drc_physical_needed,
593 .fields = (VMStateField []) {
594 VMSTATE_UINT32(dr_indicator, SpaprDrcPhysical),
595 VMSTATE_END_OF_LIST()
596 }
597};
598
599static void drc_physical_reset(void *opaque)
600{
601 SpaprDrc *drc = SPAPR_DR_CONNECTOR(opaque);
602 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
603
604 if (drc->dev) {
605 drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
606 } else {
607 drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
608 }
609}
610
611static void realize_physical(DeviceState *d, Error **errp)
612{
613 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
614 Error *local_err = NULL;
615
616 realize(d, &local_err);
617 if (local_err) {
618 error_propagate(errp, local_err);
619 return;
620 }
621
622 vmstate_register(DEVICE(drcp), spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
623 &vmstate_spapr_drc_physical, drcp);
624 qemu_register_reset(drc_physical_reset, drcp);
625}
626
627static void unrealize_physical(DeviceState *d, Error **errp)
628{
629 SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
630 Error *local_err = NULL;
631
632 unrealize(d, &local_err);
633 if (local_err) {
634 error_propagate(errp, local_err);
635 return;
636 }
637
638 vmstate_unregister(DEVICE(drcp), &vmstate_spapr_drc_physical, drcp);
639 qemu_unregister_reset(drc_physical_reset, drcp);
640}
641
642static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
643{
644 DeviceClass *dk = DEVICE_CLASS(k);
645 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
646
647 dk->realize = realize_physical;
648 dk->unrealize = unrealize_physical;
649 drck->dr_entity_sense = physical_entity_sense;
650 drck->isolate = drc_isolate_physical;
651 drck->unisolate = drc_unisolate_physical;
652 drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
653 drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
654}
655
656static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
657{
658 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
659
660 drck->dr_entity_sense = logical_entity_sense;
661 drck->isolate = drc_isolate_logical;
662 drck->unisolate = drc_unisolate_logical;
663 drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
664 drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
665}
666
667static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
668{
669 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
670
671 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
672 drck->typename = "CPU";
673 drck->drc_name_prefix = "CPU ";
674 drck->release = spapr_core_release;
675 drck->dt_populate = spapr_core_dt_populate;
676}
677
678static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
679{
680 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
681
682 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
683 drck->typename = "28";
684 drck->drc_name_prefix = "C";
685 drck->release = spapr_phb_remove_pci_device_cb;
686 drck->dt_populate = spapr_pci_dt_populate;
687}
688
689static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
690{
691 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
692
693 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
694 drck->typename = "MEM";
695 drck->drc_name_prefix = "LMB ";
696 drck->release = spapr_lmb_release;
697 drck->dt_populate = spapr_lmb_dt_populate;
698}
699
700static void spapr_drc_phb_class_init(ObjectClass *k, void *data)
701{
702 SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
703
704 drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB;
705 drck->typename = "PHB";
706 drck->drc_name_prefix = "PHB ";
707 drck->release = spapr_phb_release;
708 drck->dt_populate = spapr_phb_dt_populate;
709}
710
711static const TypeInfo spapr_dr_connector_info = {
712 .name = TYPE_SPAPR_DR_CONNECTOR,
713 .parent = TYPE_DEVICE,
714 .instance_size = sizeof(SpaprDrc),
715 .instance_init = spapr_dr_connector_instance_init,
716 .class_size = sizeof(SpaprDrcClass),
717 .class_init = spapr_dr_connector_class_init,
718 .abstract = true,
719};
720
721static const TypeInfo spapr_drc_physical_info = {
722 .name = TYPE_SPAPR_DRC_PHYSICAL,
723 .parent = TYPE_SPAPR_DR_CONNECTOR,
724 .instance_size = sizeof(SpaprDrcPhysical),
725 .class_init = spapr_drc_physical_class_init,
726 .abstract = true,
727};
728
729static const TypeInfo spapr_drc_logical_info = {
730 .name = TYPE_SPAPR_DRC_LOGICAL,
731 .parent = TYPE_SPAPR_DR_CONNECTOR,
732 .class_init = spapr_drc_logical_class_init,
733 .abstract = true,
734};
735
736static const TypeInfo spapr_drc_cpu_info = {
737 .name = TYPE_SPAPR_DRC_CPU,
738 .parent = TYPE_SPAPR_DRC_LOGICAL,
739 .class_init = spapr_drc_cpu_class_init,
740};
741
742static const TypeInfo spapr_drc_pci_info = {
743 .name = TYPE_SPAPR_DRC_PCI,
744 .parent = TYPE_SPAPR_DRC_PHYSICAL,
745 .class_init = spapr_drc_pci_class_init,
746};
747
748static const TypeInfo spapr_drc_lmb_info = {
749 .name = TYPE_SPAPR_DRC_LMB,
750 .parent = TYPE_SPAPR_DRC_LOGICAL,
751 .class_init = spapr_drc_lmb_class_init,
752};
753
754static const TypeInfo spapr_drc_phb_info = {
755 .name = TYPE_SPAPR_DRC_PHB,
756 .parent = TYPE_SPAPR_DRC_LOGICAL,
757 .instance_size = sizeof(SpaprDrc),
758 .class_init = spapr_drc_phb_class_init,
759};
760
761/* helper functions for external users */
762
763SpaprDrc *spapr_drc_by_index(uint32_t index)
764{
765 Object *obj;
766 gchar *name;
767
768 name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH, index);
769 obj = object_resolve_path(name, NULL);
770 g_free(name);
771
772 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
773}
774
775SpaprDrc *spapr_drc_by_id(const char *type, uint32_t id)
776{
777 SpaprDrcClass *drck
778 = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
779
780 return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
781 | (id & DRC_INDEX_ID_MASK));
782}
783
784/**
785 * spapr_dt_drc
786 *
787 * @fdt: libfdt device tree
788 * @path: path in the DT to generate properties
789 * @owner: parent Object/DeviceState for which to generate DRC
790 * descriptions for
791 * @drc_type_mask: mask of SpaprDrcType values corresponding
792 * to the types of DRCs to generate entries for
793 *
794 * generate OF properties to describe DRC topology/indices to guests
795 *
796 * as documented in PAPR+ v2.1, 13.5.2
797 */
798int spapr_dt_drc(void *fdt, int offset, Object *owner, uint32_t drc_type_mask)
799{
800 Object *root_container;
801 ObjectProperty *prop;
802 ObjectPropertyIterator iter;
803 uint32_t drc_count = 0;
804 GArray *drc_indexes, *drc_power_domains;
805 GString *drc_names, *drc_types;
806 int ret;
807
808 /* the first entry of each properties is a 32-bit integer encoding
809 * the number of elements in the array. we won't know this until
810 * we complete the iteration through all the matching DRCs, but
811 * reserve the space now and set the offsets accordingly so we
812 * can fill them in later.
813 */
814 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
815 drc_indexes = g_array_set_size(drc_indexes, 1);
816 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
817 drc_power_domains = g_array_set_size(drc_power_domains, 1);
818 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
819 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
820
821 /* aliases for all DRConnector objects will be rooted in QOM
822 * composition tree at DRC_CONTAINER_PATH
823 */
824 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
825
826 object_property_iter_init(&iter, root_container);
827 while ((prop = object_property_iter_next(&iter))) {
828 Object *obj;
829 SpaprDrc *drc;
830 SpaprDrcClass *drck;
831 char *drc_name = NULL;
832 uint32_t drc_index, drc_power_domain;
833
834 if (!strstart(prop->type, "link<", NULL)) {
835 continue;
836 }
837
838 obj = object_property_get_link(root_container, prop->name, NULL);
839 drc = SPAPR_DR_CONNECTOR(obj);
840 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
841
842 if (owner && (drc->owner != owner)) {
843 continue;
844 }
845
846 if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
847 continue;
848 }
849
850 drc_count++;
851
852 /* ibm,drc-indexes */
853 drc_index = cpu_to_be32(spapr_drc_index(drc));
854 g_array_append_val(drc_indexes, drc_index);
855
856 /* ibm,drc-power-domains */
857 drc_power_domain = cpu_to_be32(-1);
858 g_array_append_val(drc_power_domains, drc_power_domain);
859
860 /* ibm,drc-names */
861 drc_name = spapr_drc_name(drc);
862 drc_names = g_string_append(drc_names, drc_name);
863 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
864 g_free(drc_name);
865
866 /* ibm,drc-types */
867 drc_types = g_string_append(drc_types, drck->typename);
868 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
869 }
870
871 /* now write the drc count into the space we reserved at the
872 * beginning of the arrays previously
873 */
874 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
875 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
876 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
877 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
878
879 ret = fdt_setprop(fdt, offset, "ibm,drc-indexes",
880 drc_indexes->data,
881 drc_indexes->len * sizeof(uint32_t));
882 if (ret) {
883 error_report("Couldn't create ibm,drc-indexes property");
884 goto out;
885 }
886
887 ret = fdt_setprop(fdt, offset, "ibm,drc-power-domains",
888 drc_power_domains->data,
889 drc_power_domains->len * sizeof(uint32_t));
890 if (ret) {
891 error_report("Couldn't finalize ibm,drc-power-domains property");
892 goto out;
893 }
894
895 ret = fdt_setprop(fdt, offset, "ibm,drc-names",
896 drc_names->str, drc_names->len);
897 if (ret) {
898 error_report("Couldn't finalize ibm,drc-names property");
899 goto out;
900 }
901
902 ret = fdt_setprop(fdt, offset, "ibm,drc-types",
903 drc_types->str, drc_types->len);
904 if (ret) {
905 error_report("Couldn't finalize ibm,drc-types property");
906 goto out;
907 }
908
909out:
910 g_array_free(drc_indexes, true);
911 g_array_free(drc_power_domains, true);
912 g_string_free(drc_names, true);
913 g_string_free(drc_types, true);
914
915 return ret;
916}
917
918/*
919 * RTAS calls
920 */
921
922static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
923{
924 SpaprDrc *drc = spapr_drc_by_index(idx);
925 SpaprDrcClass *drck;
926
927 if (!drc) {
928 return RTAS_OUT_NO_SUCH_INDICATOR;
929 }
930
931 trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
932
933 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
934
935 switch (state) {
936 case SPAPR_DR_ISOLATION_STATE_ISOLATED:
937 return drck->isolate(drc);
938
939 case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
940 return drck->unisolate(drc);
941
942 default:
943 return RTAS_OUT_PARAM_ERROR;
944 }
945}
946
947static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
948{
949 SpaprDrc *drc = spapr_drc_by_index(idx);
950
951 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
952 return RTAS_OUT_NO_SUCH_INDICATOR;
953 }
954
955 trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
956
957 switch (state) {
958 case SPAPR_DR_ALLOCATION_STATE_USABLE:
959 return drc_set_usable(drc);
960
961 case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
962 return drc_set_unusable(drc);
963
964 default:
965 return RTAS_OUT_PARAM_ERROR;
966 }
967}
968
969static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
970{
971 SpaprDrc *drc = spapr_drc_by_index(idx);
972
973 if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
974 return RTAS_OUT_NO_SUCH_INDICATOR;
975 }
976 if ((state != SPAPR_DR_INDICATOR_INACTIVE)
977 && (state != SPAPR_DR_INDICATOR_ACTIVE)
978 && (state != SPAPR_DR_INDICATOR_IDENTIFY)
979 && (state != SPAPR_DR_INDICATOR_ACTION)) {
980 return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
981 }
982
983 trace_spapr_drc_set_dr_indicator(idx, state);
984 SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
985 return RTAS_OUT_SUCCESS;
986}
987
988static void rtas_set_indicator(PowerPCCPU *cpu, SpaprMachineState *spapr,
989 uint32_t token,
990 uint32_t nargs, target_ulong args,
991 uint32_t nret, target_ulong rets)
992{
993 uint32_t type, idx, state;
994 uint32_t ret = RTAS_OUT_SUCCESS;
995
996 if (nargs != 3 || nret != 1) {
997 ret = RTAS_OUT_PARAM_ERROR;
998 goto out;
999 }
1000
1001 type = rtas_ld(args, 0);
1002 idx = rtas_ld(args, 1);
1003 state = rtas_ld(args, 2);
1004
1005 switch (type) {
1006 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1007 ret = rtas_set_isolation_state(idx, state);
1008 break;
1009 case RTAS_SENSOR_TYPE_DR:
1010 ret = rtas_set_dr_indicator(idx, state);
1011 break;
1012 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1013 ret = rtas_set_allocation_state(idx, state);
1014 break;
1015 default:
1016 ret = RTAS_OUT_NOT_SUPPORTED;
1017 }
1018
1019out:
1020 rtas_st(rets, 0, ret);
1021}
1022
1023static void rtas_get_sensor_state(PowerPCCPU *cpu, SpaprMachineState *spapr,
1024 uint32_t token, uint32_t nargs,
1025 target_ulong args, uint32_t nret,
1026 target_ulong rets)
1027{
1028 uint32_t sensor_type;
1029 uint32_t sensor_index;
1030 uint32_t sensor_state = 0;
1031 SpaprDrc *drc;
1032 SpaprDrcClass *drck;
1033 uint32_t ret = RTAS_OUT_SUCCESS;
1034
1035 if (nargs != 2 || nret != 2) {
1036 ret = RTAS_OUT_PARAM_ERROR;
1037 goto out;
1038 }
1039
1040 sensor_type = rtas_ld(args, 0);
1041 sensor_index = rtas_ld(args, 1);
1042
1043 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1044 /* currently only DR-related sensors are implemented */
1045 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1046 sensor_type);
1047 ret = RTAS_OUT_NOT_SUPPORTED;
1048 goto out;
1049 }
1050
1051 drc = spapr_drc_by_index(sensor_index);
1052 if (!drc) {
1053 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1054 ret = RTAS_OUT_PARAM_ERROR;
1055 goto out;
1056 }
1057 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1058 sensor_state = drck->dr_entity_sense(drc);
1059
1060out:
1061 rtas_st(rets, 0, ret);
1062 rtas_st(rets, 1, sensor_state);
1063}
1064
1065/* configure-connector work area offsets, int32_t units for field
1066 * indexes, bytes for field offset/len values.
1067 *
1068 * as documented by PAPR+ v2.7, 13.5.3.5
1069 */
1070#define CC_IDX_NODE_NAME_OFFSET 2
1071#define CC_IDX_PROP_NAME_OFFSET 2
1072#define CC_IDX_PROP_LEN 3
1073#define CC_IDX_PROP_DATA_OFFSET 4
1074#define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1075#define CC_WA_LEN 4096
1076
1077static void configure_connector_st(target_ulong addr, target_ulong offset,
1078 const void *buf, size_t len)
1079{
1080 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1081 buf, MIN(len, CC_WA_LEN - offset));
1082}
1083
1084static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1085 SpaprMachineState *spapr,
1086 uint32_t token, uint32_t nargs,
1087 target_ulong args, uint32_t nret,
1088 target_ulong rets)
1089{
1090 uint64_t wa_addr;
1091 uint64_t wa_offset;
1092 uint32_t drc_index;
1093 SpaprDrc *drc;
1094 SpaprDrcClass *drck;
1095 SpaprDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1096 int rc;
1097
1098 if (nargs != 2 || nret != 1) {
1099 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1100 return;
1101 }
1102
1103 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1104
1105 drc_index = rtas_ld(wa_addr, 0);
1106 drc = spapr_drc_by_index(drc_index);
1107 if (!drc) {
1108 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1109 rc = RTAS_OUT_PARAM_ERROR;
1110 goto out;
1111 }
1112
1113 if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
1114 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
1115 && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
1116 && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
1117 /*
1118 * Need to unisolate the device before configuring
1119 * or it should already be in configured state to
1120 * allow configure-connector be called repeatedly.
1121 */
1122 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1123 goto out;
1124 }
1125
1126 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1127
1128 if (!drc->fdt) {
1129 Error *local_err = NULL;
1130 void *fdt;
1131 int fdt_size;
1132
1133 fdt = create_device_tree(&fdt_size);
1134
1135 if (drck->dt_populate(drc, spapr, fdt, &drc->fdt_start_offset,
1136 &local_err)) {
1137 g_free(fdt);
1138 error_free(local_err);
1139 rc = SPAPR_DR_CC_RESPONSE_ERROR;
1140 goto out;
1141 }
1142
1143 drc->fdt = fdt;
1144 drc->ccs_offset = drc->fdt_start_offset;
1145 drc->ccs_depth = 0;
1146 }
1147
1148 do {
1149 uint32_t tag;
1150 const char *name;
1151 const struct fdt_property *prop;
1152 int fdt_offset_next, prop_len;
1153
1154 tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
1155
1156 switch (tag) {
1157 case FDT_BEGIN_NODE:
1158 drc->ccs_depth++;
1159 name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
1160
1161 /* provide the name of the next OF node */
1162 wa_offset = CC_VAL_DATA_OFFSET;
1163 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1164 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1165 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1166 break;
1167 case FDT_END_NODE:
1168 drc->ccs_depth--;
1169 if (drc->ccs_depth == 0) {
1170 uint32_t drc_index = spapr_drc_index(drc);
1171
1172 /* done sending the device tree, move to configured state */
1173 trace_spapr_drc_set_configured(drc_index);
1174 drc->state = drck->ready_state;
1175 /*
1176 * Ensure that we are able to send the FDT fragment
1177 * again via configure-connector call if the guest requests.
1178 */
1179 drc->ccs_offset = drc->fdt_start_offset;
1180 drc->ccs_depth = 0;
1181 fdt_offset_next = drc->fdt_start_offset;
1182 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1183 } else {
1184 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1185 }
1186 break;
1187 case FDT_PROP:
1188 prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
1189 &prop_len);
1190 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1191
1192 /* provide the name of the next OF property */
1193 wa_offset = CC_VAL_DATA_OFFSET;
1194 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1195 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1196
1197 /* provide the length and value of the OF property. data gets
1198 * placed immediately after NULL terminator of the OF property's
1199 * name string
1200 */
1201 wa_offset += strlen(name) + 1,
1202 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1203 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1204 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1205 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1206 break;
1207 case FDT_END:
1208 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1209 default:
1210 /* keep seeking for an actionable tag */
1211 break;
1212 }
1213 if (drc->ccs_offset >= 0) {
1214 drc->ccs_offset = fdt_offset_next;
1215 }
1216 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1217
1218 rc = resp;
1219out:
1220 rtas_st(rets, 0, rc);
1221}
1222
1223static void spapr_drc_register_types(void)
1224{
1225 type_register_static(&spapr_dr_connector_info);
1226 type_register_static(&spapr_drc_physical_info);
1227 type_register_static(&spapr_drc_logical_info);
1228 type_register_static(&spapr_drc_cpu_info);
1229 type_register_static(&spapr_drc_pci_info);
1230 type_register_static(&spapr_drc_lmb_info);
1231 type_register_static(&spapr_drc_phb_info);
1232
1233 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1234 rtas_set_indicator);
1235 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1236 rtas_get_sensor_state);
1237 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1238 rtas_ibm_configure_connector);
1239}
1240type_init(spapr_drc_register_types)
1241