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