]> Git Repo - qemu.git/blob - hw/ppc/spapr_drc.c
Merge remote-tracking branch 'dgibson/tags/ppc-for-2.10-20170525' into staging
[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 sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type)
31 {
32     uint32_t shift = 0;
33
34     /* make sure this isn't SPAPR_DR_CONNECTOR_TYPE_ANY, or some
35      * other wonky value.
36      */
37     g_assert(is_power_of_2(type));
38
39     while (type != (1 << shift)) {
40         shift++;
41     }
42     return shift;
43 }
44
45 static uint32_t get_index(sPAPRDRConnector *drc)
46 {
47     /* no set format for a drc index: it only needs to be globally
48      * unique. this is how we encode the DRC type on bare-metal
49      * however, so might as well do that here
50      */
51     return (get_type_shift(drc->type) << DRC_INDEX_TYPE_SHIFT) |
52             (drc->id & DRC_INDEX_ID_MASK);
53 }
54
55 static uint32_t set_isolation_state(sPAPRDRConnector *drc,
56                                     sPAPRDRIsolationState state)
57 {
58     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
59
60     trace_spapr_drc_set_isolation_state(get_index(drc), state);
61
62     if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
63         /* cannot unisolate a non-existent resource, and, or resources
64          * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
65          */
66         if (!drc->dev ||
67             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
68             return RTAS_OUT_NO_SUCH_INDICATOR;
69         }
70     }
71
72     /*
73      * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
74      * belong to a DIMM device that is marked for removal.
75      *
76      * Currently the guest userspace tool drmgr that drives the memory
77      * hotplug/unplug will just try to remove a set of 'removable' LMBs
78      * in response to a hot unplug request that is based on drc-count.
79      * If the LMB being removed doesn't belong to a DIMM device that is
80      * actually being unplugged, fail the isolation request here.
81      */
82     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_LMB) {
83         if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
84              !drc->awaiting_release) {
85             return RTAS_OUT_HW_ERROR;
86         }
87     }
88
89     drc->isolation_state = state;
90
91     if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
92         /* if we're awaiting release, but still in an unconfigured state,
93          * it's likely the guest is still in the process of configuring
94          * the device and is transitioning the devices to an ISOLATED
95          * state as a part of that process. so we only complete the
96          * removal when this transition happens for a device in a
97          * configured state, as suggested by the state diagram from
98          * PAPR+ 2.7, 13.4
99          */
100         if (drc->awaiting_release) {
101             if (drc->configured) {
102                 trace_spapr_drc_set_isolation_state_finalizing(get_index(drc));
103                 drck->detach(drc, DEVICE(drc->dev), NULL);
104             } else {
105                 trace_spapr_drc_set_isolation_state_deferring(get_index(drc));
106             }
107         }
108         drc->configured = false;
109     }
110
111     return RTAS_OUT_SUCCESS;
112 }
113
114 static uint32_t set_indicator_state(sPAPRDRConnector *drc,
115                                     sPAPRDRIndicatorState state)
116 {
117     trace_spapr_drc_set_indicator_state(get_index(drc), state);
118     drc->indicator_state = state;
119     return RTAS_OUT_SUCCESS;
120 }
121
122 static uint32_t set_allocation_state(sPAPRDRConnector *drc,
123                                      sPAPRDRAllocationState state)
124 {
125     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
126
127     trace_spapr_drc_set_allocation_state(get_index(drc), state);
128
129     if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
130         /* if there's no resource/device associated with the DRC, there's
131          * no way for us to put it in an allocation state consistent with
132          * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
133          * result in an RTAS return code of -3 / "no such indicator"
134          */
135         if (!drc->dev) {
136             return RTAS_OUT_NO_SUCH_INDICATOR;
137         }
138         if (drc->awaiting_release && drc->awaiting_allocation) {
139             /* kernel is acknowledging a previous hotplug event
140              * while we are already removing it.
141              * it's safe to ignore awaiting_allocation here since we know the
142              * situation is predicated on the guest either already having done
143              * so (boot-time hotplug), or never being able to acquire in the
144              * first place (hotplug followed by immediate unplug).
145              */
146             drc->awaiting_allocation_skippable = true;
147             return RTAS_OUT_NO_SUCH_INDICATOR;
148         }
149     }
150
151     if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
152         drc->allocation_state = state;
153         if (drc->awaiting_release &&
154             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
155             trace_spapr_drc_set_allocation_state_finalizing(get_index(drc));
156             drck->detach(drc, DEVICE(drc->dev), NULL);
157         } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
158             drc->awaiting_allocation = false;
159         }
160     }
161     return RTAS_OUT_SUCCESS;
162 }
163
164 static uint32_t get_type(sPAPRDRConnector *drc)
165 {
166     return drc->type;
167 }
168
169 static const char *get_name(sPAPRDRConnector *drc)
170 {
171     return drc->name;
172 }
173
174 static const void *get_fdt(sPAPRDRConnector *drc, int *fdt_start_offset)
175 {
176     if (fdt_start_offset) {
177         *fdt_start_offset = drc->fdt_start_offset;
178     }
179     return drc->fdt;
180 }
181
182 static void set_configured(sPAPRDRConnector *drc)
183 {
184     trace_spapr_drc_set_configured(get_index(drc));
185
186     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
187         /* guest should be not configuring an isolated device */
188         trace_spapr_drc_set_configured_skipping(get_index(drc));
189         return;
190     }
191     drc->configured = true;
192 }
193
194 /* has the guest been notified of device attachment? */
195 static void set_signalled(sPAPRDRConnector *drc)
196 {
197     drc->signalled = true;
198 }
199
200 /*
201  * dr-entity-sense sensor value
202  * returned via get-sensor-state RTAS calls
203  * as expected by state diagram in PAPR+ 2.7, 13.4
204  * based on the current allocation/indicator/power states
205  * for the DR connector.
206  */
207 static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
208 {
209     if (drc->dev) {
210         if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
211             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
212             /* for logical DR, we return a state of UNUSABLE
213              * iff the allocation state UNUSABLE.
214              * Otherwise, report the state as USABLE/PRESENT,
215              * as we would for PCI.
216              */
217             *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
218         } else {
219             /* this assumes all PCI devices are assigned to
220              * a 'live insertion' power domain, where QEMU
221              * manages power state automatically as opposed
222              * to the guest. present, non-PCI resources are
223              * unaffected by power state.
224              */
225             *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
226         }
227     } else {
228         if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
229             /* PCI devices, and only PCI devices, use EMPTY
230              * in cases where we'd otherwise use UNUSABLE
231              */
232             *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
233         } else {
234             *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
235         }
236     }
237
238     trace_spapr_drc_entity_sense(get_index(drc), *state);
239     return RTAS_OUT_SUCCESS;
240 }
241
242 static void prop_get_index(Object *obj, Visitor *v, const char *name,
243                            void *opaque, Error **errp)
244 {
245     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
246     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
247     uint32_t value = (uint32_t)drck->get_index(drc);
248     visit_type_uint32(v, name, &value, errp);
249 }
250
251 static void prop_get_type(Object *obj, Visitor *v, const char *name,
252                           void *opaque, Error **errp)
253 {
254     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
255     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
256     uint32_t value = (uint32_t)drck->get_type(drc);
257     visit_type_uint32(v, name, &value, errp);
258 }
259
260 static char *prop_get_name(Object *obj, Error **errp)
261 {
262     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
263     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
264     return g_strdup(drck->get_name(drc));
265 }
266
267 static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
268                                   void *opaque, Error **errp)
269 {
270     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
271     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
272     uint32_t value;
273
274     drck->entity_sense(drc, &value);
275     visit_type_uint32(v, name, &value, errp);
276 }
277
278 static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
279                          void *opaque, Error **errp)
280 {
281     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
282     Error *err = NULL;
283     int fdt_offset_next, fdt_offset, fdt_depth;
284     void *fdt;
285
286     if (!drc->fdt) {
287         visit_type_null(v, NULL, errp);
288         return;
289     }
290
291     fdt = drc->fdt;
292     fdt_offset = drc->fdt_start_offset;
293     fdt_depth = 0;
294
295     do {
296         const char *name = NULL;
297         const struct fdt_property *prop = NULL;
298         int prop_len = 0, name_len = 0;
299         uint32_t tag;
300
301         tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
302         switch (tag) {
303         case FDT_BEGIN_NODE:
304             fdt_depth++;
305             name = fdt_get_name(fdt, fdt_offset, &name_len);
306             visit_start_struct(v, name, NULL, 0, &err);
307             if (err) {
308                 error_propagate(errp, err);
309                 return;
310             }
311             break;
312         case FDT_END_NODE:
313             /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
314             g_assert(fdt_depth > 0);
315             visit_check_struct(v, &err);
316             visit_end_struct(v, NULL);
317             if (err) {
318                 error_propagate(errp, err);
319                 return;
320             }
321             fdt_depth--;
322             break;
323         case FDT_PROP: {
324             int i;
325             prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
326             name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
327             visit_start_list(v, name, NULL, 0, &err);
328             if (err) {
329                 error_propagate(errp, err);
330                 return;
331             }
332             for (i = 0; i < prop_len; i++) {
333                 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
334                 if (err) {
335                     error_propagate(errp, err);
336                     return;
337                 }
338             }
339             visit_check_list(v, &err);
340             visit_end_list(v, NULL);
341             if (err) {
342                 error_propagate(errp, err);
343                 return;
344             }
345             break;
346         }
347         default:
348             error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
349         }
350         fdt_offset = fdt_offset_next;
351     } while (fdt_depth != 0);
352 }
353
354 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
355                    int fdt_start_offset, bool coldplug, Error **errp)
356 {
357     trace_spapr_drc_attach(get_index(drc));
358
359     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
360         error_setg(errp, "an attached device is still awaiting release");
361         return;
362     }
363     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
364         g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
365     }
366     g_assert(fdt || coldplug);
367
368     /* NOTE: setting initial isolation state to UNISOLATED means we can't
369      * detach unless guest has a userspace/kernel that moves this state
370      * back to ISOLATED in response to an unplug event, or this is done
371      * manually by the admin prior. if we force things while the guest
372      * may be accessing the device, we can easily crash the guest, so we
373      * we defer completion of removal in such cases to the reset() hook.
374      */
375     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
376         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
377     }
378     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
379
380     drc->dev = d;
381     drc->fdt = fdt;
382     drc->fdt_start_offset = fdt_start_offset;
383     drc->configured = coldplug;
384     /* 'logical' DR resources such as memory/cpus are in some cases treated
385      * as a pool of resources from which the guest is free to choose from
386      * based on only a count. for resources that can be assigned in this
387      * fashion, we must assume the resource is signalled immediately
388      * since a single hotplug request might make an arbitrary number of
389      * such attached resources available to the guest, as opposed to
390      * 'physical' DR resources such as PCI where each device/resource is
391      * signalled individually.
392      */
393     drc->signalled = (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI)
394                      ? true : coldplug;
395
396     if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
397         drc->awaiting_allocation = true;
398     }
399
400     object_property_add_link(OBJECT(drc), "device",
401                              object_get_typename(OBJECT(drc->dev)),
402                              (Object **)(&drc->dev),
403                              NULL, 0, NULL);
404 }
405
406 static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
407 {
408     trace_spapr_drc_detach(get_index(drc));
409
410     /* if we've signalled device presence to the guest, or if the guest
411      * has gone ahead and configured the device (via manually-executed
412      * device add via drmgr in guest, namely), we need to wait
413      * for the guest to quiesce the device before completing detach.
414      * Otherwise, we can assume the guest hasn't seen it and complete the
415      * detach immediately. Note that there is a small race window
416      * just before, or during, configuration, which is this context
417      * refers mainly to fetching the device tree via RTAS.
418      * During this window the device access will be arbitrated by
419      * associated DRC, which will simply fail the RTAS calls as invalid.
420      * This is recoverable within guest and current implementations of
421      * drmgr should be able to cope.
422      */
423     if (!drc->signalled && !drc->configured) {
424         /* if the guest hasn't seen the device we can't rely on it to
425          * set it back to an isolated state via RTAS, so do it here manually
426          */
427         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
428     }
429
430     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
431         trace_spapr_drc_awaiting_isolated(get_index(drc));
432         drc->awaiting_release = true;
433         return;
434     }
435
436     if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
437         drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
438         trace_spapr_drc_awaiting_unusable(get_index(drc));
439         drc->awaiting_release = true;
440         return;
441     }
442
443     if (drc->awaiting_allocation) {
444         if (!drc->awaiting_allocation_skippable) {
445             drc->awaiting_release = true;
446             trace_spapr_drc_awaiting_allocation(get_index(drc));
447             return;
448         }
449     }
450
451     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
452
453     /* Calling release callbacks based on drc->type. */
454     switch (drc->type) {
455     case SPAPR_DR_CONNECTOR_TYPE_CPU:
456         spapr_core_release(drc->dev);
457         break;
458     case SPAPR_DR_CONNECTOR_TYPE_PCI:
459         spapr_phb_remove_pci_device_cb(drc->dev);
460         break;
461     case SPAPR_DR_CONNECTOR_TYPE_LMB:
462         spapr_lmb_release(drc->dev);
463         break;
464     case SPAPR_DR_CONNECTOR_TYPE_PHB:
465     case SPAPR_DR_CONNECTOR_TYPE_VIO:
466     default:
467         g_assert(false);
468     }
469
470     drc->awaiting_release = false;
471     drc->awaiting_allocation_skippable = false;
472     g_free(drc->fdt);
473     drc->fdt = NULL;
474     drc->fdt_start_offset = 0;
475     object_property_del(OBJECT(drc), "device", NULL);
476     drc->dev = NULL;
477 }
478
479 static bool release_pending(sPAPRDRConnector *drc)
480 {
481     return drc->awaiting_release;
482 }
483
484 static void reset(DeviceState *d)
485 {
486     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
487     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
488     sPAPRDREntitySense state;
489
490     trace_spapr_drc_reset(drck->get_index(drc));
491     /* immediately upon reset we can safely assume DRCs whose devices
492      * are pending removal can be safely removed, and that they will
493      * subsequently be left in an ISOLATED state. move the DRC to this
494      * state in these cases (which will in turn complete any pending
495      * device removals)
496      */
497     if (drc->awaiting_release) {
498         drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
499         /* generally this should also finalize the removal, but if the device
500          * hasn't yet been configured we normally defer removal under the
501          * assumption that this transition is taking place as part of device
502          * configuration. so check if we're still waiting after this, and
503          * force removal if we are
504          */
505         if (drc->awaiting_release) {
506             drck->detach(drc, DEVICE(drc->dev), NULL);
507         }
508
509         /* non-PCI devices may be awaiting a transition to UNUSABLE */
510         if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
511             drc->awaiting_release) {
512             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
513         }
514     }
515
516     drck->entity_sense(drc, &state);
517     if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
518         drck->set_signalled(drc);
519     }
520 }
521
522 static bool spapr_drc_needed(void *opaque)
523 {
524     sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
525     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
526     bool rc = false;
527     sPAPRDREntitySense value;
528     drck->entity_sense(drc, &value);
529
530     /* If no dev is plugged in there is no need to migrate the DRC state */
531     if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
532         return false;
533     }
534
535     /*
536      * If there is dev plugged in, we need to migrate the DRC state when
537      * it is different from cold-plugged state
538      */
539     switch (drc->type) {
540     case SPAPR_DR_CONNECTOR_TYPE_PCI:
541         rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
542                (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
543                drc->configured && drc->signalled && !drc->awaiting_release);
544         break;
545     case SPAPR_DR_CONNECTOR_TYPE_CPU:
546     case SPAPR_DR_CONNECTOR_TYPE_LMB:
547         rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
548                (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) &&
549                drc->configured && drc->signalled && !drc->awaiting_release);
550         break;
551     case SPAPR_DR_CONNECTOR_TYPE_PHB:
552     case SPAPR_DR_CONNECTOR_TYPE_VIO:
553     default:
554         g_assert(false);
555     }
556     return rc;
557 }
558
559 static const VMStateDescription vmstate_spapr_drc = {
560     .name = "spapr_drc",
561     .version_id = 1,
562     .minimum_version_id = 1,
563     .needed = spapr_drc_needed,
564     .fields  = (VMStateField []) {
565         VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
566         VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
567         VMSTATE_UINT32(indicator_state, sPAPRDRConnector),
568         VMSTATE_BOOL(configured, sPAPRDRConnector),
569         VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
570         VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
571         VMSTATE_BOOL(signalled, sPAPRDRConnector),
572         VMSTATE_END_OF_LIST()
573     }
574 };
575
576 static void realize(DeviceState *d, Error **errp)
577 {
578     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
579     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
580     Object *root_container;
581     char link_name[256];
582     gchar *child_name;
583     Error *err = NULL;
584
585     trace_spapr_drc_realize(drck->get_index(drc));
586     /* NOTE: we do this as part of realize/unrealize due to the fact
587      * that the guest will communicate with the DRC via RTAS calls
588      * referencing the global DRC index. By unlinking the DRC
589      * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
590      * inaccessible by the guest, since lookups rely on this path
591      * existing in the composition tree
592      */
593     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
594     snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
595     child_name = object_get_canonical_path_component(OBJECT(drc));
596     trace_spapr_drc_realize_child(drck->get_index(drc), child_name);
597     object_property_add_alias(root_container, link_name,
598                               drc->owner, child_name, &err);
599     if (err) {
600         error_report_err(err);
601         object_unref(OBJECT(drc));
602     }
603     g_free(child_name);
604     vmstate_register(DEVICE(drc), drck->get_index(drc), &vmstate_spapr_drc,
605                      drc);
606     trace_spapr_drc_realize_complete(drck->get_index(drc));
607 }
608
609 static void unrealize(DeviceState *d, Error **errp)
610 {
611     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
612     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
613     Object *root_container;
614     char name[256];
615     Error *err = NULL;
616
617     trace_spapr_drc_unrealize(drck->get_index(drc));
618     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
619     snprintf(name, sizeof(name), "%x", drck->get_index(drc));
620     object_property_del(root_container, name, &err);
621     if (err) {
622         error_report_err(err);
623         object_unref(OBJECT(drc));
624     }
625 }
626
627 sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
628                                          sPAPRDRConnectorType type,
629                                          uint32_t id)
630 {
631     sPAPRDRConnector *drc =
632         SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
633     char *prop_name;
634
635     g_assert(type);
636
637     drc->type = type;
638     drc->id = id;
639     drc->owner = owner;
640     prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
641     object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
642     object_property_set_bool(OBJECT(drc), true, "realized", NULL);
643     g_free(prop_name);
644
645     /* human-readable name for a DRC to encode into the DT
646      * description. this is mainly only used within a guest in place
647      * of the unique DRC index.
648      *
649      * in the case of VIO/PCI devices, it corresponds to a
650      * "location code" that maps a logical device/function (DRC index)
651      * to a physical (or virtual in the case of VIO) location in the
652      * system by chaining together the "location label" for each
653      * encapsulating component.
654      *
655      * since this is more to do with diagnosing physical hardware
656      * issues than guest compatibility, we choose location codes/DRC
657      * names that adhere to the documented format, but avoid encoding
658      * the entire topology information into the label/code, instead
659      * just using the location codes based on the labels for the
660      * endpoints (VIO/PCI adaptor connectors), which is basically
661      * just "C" followed by an integer ID.
662      *
663      * DRC names as documented by PAPR+ v2.7, 13.5.2.4
664      * location codes as documented by PAPR+ v2.7, 12.3.1.5
665      */
666     switch (drc->type) {
667     case SPAPR_DR_CONNECTOR_TYPE_CPU:
668         drc->name = g_strdup_printf("CPU %d", id);
669         break;
670     case SPAPR_DR_CONNECTOR_TYPE_PHB:
671         drc->name = g_strdup_printf("PHB %d", id);
672         break;
673     case SPAPR_DR_CONNECTOR_TYPE_VIO:
674     case SPAPR_DR_CONNECTOR_TYPE_PCI:
675         drc->name = g_strdup_printf("C%d", id);
676         break;
677     case SPAPR_DR_CONNECTOR_TYPE_LMB:
678         drc->name = g_strdup_printf("LMB %d", id);
679         break;
680     default:
681         g_assert(false);
682     }
683
684     /* PCI slot always start in a USABLE state, and stay there */
685     if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
686         drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
687     }
688
689     return drc;
690 }
691
692 static void spapr_dr_connector_instance_init(Object *obj)
693 {
694     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
695
696     object_property_add_uint32_ptr(obj, "isolation-state",
697                                    &drc->isolation_state, NULL);
698     object_property_add_uint32_ptr(obj, "indicator-state",
699                                    &drc->indicator_state, NULL);
700     object_property_add_uint32_ptr(obj, "allocation-state",
701                                    &drc->allocation_state, NULL);
702     object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
703     object_property_add(obj, "index", "uint32", prop_get_index,
704                         NULL, NULL, NULL, NULL);
705     object_property_add(obj, "connector_type", "uint32", prop_get_type,
706                         NULL, NULL, NULL, NULL);
707     object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
708     object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
709                         NULL, NULL, NULL, NULL);
710     object_property_add(obj, "fdt", "struct", prop_get_fdt,
711                         NULL, NULL, NULL, NULL);
712 }
713
714 static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
715 {
716     DeviceClass *dk = DEVICE_CLASS(k);
717     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
718
719     dk->reset = reset;
720     dk->realize = realize;
721     dk->unrealize = unrealize;
722     drck->set_isolation_state = set_isolation_state;
723     drck->set_indicator_state = set_indicator_state;
724     drck->set_allocation_state = set_allocation_state;
725     drck->get_index = get_index;
726     drck->get_type = get_type;
727     drck->get_name = get_name;
728     drck->get_fdt = get_fdt;
729     drck->set_configured = set_configured;
730     drck->entity_sense = entity_sense;
731     drck->attach = attach;
732     drck->detach = detach;
733     drck->release_pending = release_pending;
734     drck->set_signalled = set_signalled;
735     /*
736      * Reason: it crashes FIXME find and document the real reason
737      */
738     dk->user_creatable = false;
739 }
740
741 static const TypeInfo spapr_dr_connector_info = {
742     .name          = TYPE_SPAPR_DR_CONNECTOR,
743     .parent        = TYPE_DEVICE,
744     .instance_size = sizeof(sPAPRDRConnector),
745     .instance_init = spapr_dr_connector_instance_init,
746     .class_size    = sizeof(sPAPRDRConnectorClass),
747     .class_init    = spapr_dr_connector_class_init,
748 };
749
750 static void spapr_drc_register_types(void)
751 {
752     type_register_static(&spapr_dr_connector_info);
753 }
754
755 type_init(spapr_drc_register_types)
756
757 /* helper functions for external users */
758
759 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
760 {
761     Object *obj;
762     char name[256];
763
764     snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
765     obj = object_resolve_path(name, NULL);
766
767     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
768 }
769
770 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
771                                            uint32_t id)
772 {
773     return spapr_dr_connector_by_index(
774             (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
775             (id & DRC_INDEX_ID_MASK));
776 }
777
778 /* generate a string the describes the DRC to encode into the
779  * device tree.
780  *
781  * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
782  */
783 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
784 {
785     switch (type) {
786     case SPAPR_DR_CONNECTOR_TYPE_CPU:
787         return "CPU";
788     case SPAPR_DR_CONNECTOR_TYPE_PHB:
789         return "PHB";
790     case SPAPR_DR_CONNECTOR_TYPE_VIO:
791         return "SLOT";
792     case SPAPR_DR_CONNECTOR_TYPE_PCI:
793         return "28";
794     case SPAPR_DR_CONNECTOR_TYPE_LMB:
795         return "MEM";
796     default:
797         g_assert(false);
798     }
799
800     return NULL;
801 }
802
803 /**
804  * spapr_drc_populate_dt
805  *
806  * @fdt: libfdt device tree
807  * @path: path in the DT to generate properties
808  * @owner: parent Object/DeviceState for which to generate DRC
809  *         descriptions for
810  * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
811  *   to the types of DRCs to generate entries for
812  *
813  * generate OF properties to describe DRC topology/indices to guests
814  *
815  * as documented in PAPR+ v2.1, 13.5.2
816  */
817 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
818                           uint32_t drc_type_mask)
819 {
820     Object *root_container;
821     ObjectProperty *prop;
822     ObjectPropertyIterator iter;
823     uint32_t drc_count = 0;
824     GArray *drc_indexes, *drc_power_domains;
825     GString *drc_names, *drc_types;
826     int ret;
827
828     /* the first entry of each properties is a 32-bit integer encoding
829      * the number of elements in the array. we won't know this until
830      * we complete the iteration through all the matching DRCs, but
831      * reserve the space now and set the offsets accordingly so we
832      * can fill them in later.
833      */
834     drc_indexes = g_array_new(false, true, sizeof(uint32_t));
835     drc_indexes = g_array_set_size(drc_indexes, 1);
836     drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
837     drc_power_domains = g_array_set_size(drc_power_domains, 1);
838     drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
839     drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
840
841     /* aliases for all DRConnector objects will be rooted in QOM
842      * composition tree at DRC_CONTAINER_PATH
843      */
844     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
845
846     object_property_iter_init(&iter, root_container);
847     while ((prop = object_property_iter_next(&iter))) {
848         Object *obj;
849         sPAPRDRConnector *drc;
850         sPAPRDRConnectorClass *drck;
851         uint32_t drc_index, drc_power_domain;
852
853         if (!strstart(prop->type, "link<", NULL)) {
854             continue;
855         }
856
857         obj = object_property_get_link(root_container, prop->name, NULL);
858         drc = SPAPR_DR_CONNECTOR(obj);
859         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
860
861         if (owner && (drc->owner != owner)) {
862             continue;
863         }
864
865         if ((drc->type & drc_type_mask) == 0) {
866             continue;
867         }
868
869         drc_count++;
870
871         /* ibm,drc-indexes */
872         drc_index = cpu_to_be32(drck->get_index(drc));
873         g_array_append_val(drc_indexes, drc_index);
874
875         /* ibm,drc-power-domains */
876         drc_power_domain = cpu_to_be32(-1);
877         g_array_append_val(drc_power_domains, drc_power_domain);
878
879         /* ibm,drc-names */
880         drc_names = g_string_append(drc_names, drck->get_name(drc));
881         drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
882
883         /* ibm,drc-types */
884         drc_types = g_string_append(drc_types,
885                                     spapr_drc_get_type_str(drc->type));
886         drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
887     }
888
889     /* now write the drc count into the space we reserved at the
890      * beginning of the arrays previously
891      */
892     *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
893     *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
894     *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
895     *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
896
897     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
898                       drc_indexes->data,
899                       drc_indexes->len * sizeof(uint32_t));
900     if (ret) {
901         error_report("Couldn't create ibm,drc-indexes property");
902         goto out;
903     }
904
905     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
906                       drc_power_domains->data,
907                       drc_power_domains->len * sizeof(uint32_t));
908     if (ret) {
909         error_report("Couldn't finalize ibm,drc-power-domains property");
910         goto out;
911     }
912
913     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
914                       drc_names->str, drc_names->len);
915     if (ret) {
916         error_report("Couldn't finalize ibm,drc-names property");
917         goto out;
918     }
919
920     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
921                       drc_types->str, drc_types->len);
922     if (ret) {
923         error_report("Couldn't finalize ibm,drc-types property");
924         goto out;
925     }
926
927 out:
928     g_array_free(drc_indexes, true);
929     g_array_free(drc_power_domains, true);
930     g_string_free(drc_names, true);
931     g_string_free(drc_types, true);
932
933     return ret;
934 }
This page took 0.077341 seconds and 4 git commands to generate.