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