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