xref: /qemu/hw/ppc/spapr_drc.c (revision f8dc29834c6b90ef05ac9463624ad0a680feb379)
1bbf5c878SMichael Roth /*
2bbf5c878SMichael Roth  * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
3bbf5c878SMichael Roth  *
4bbf5c878SMichael Roth  * Copyright IBM Corp. 2014
5bbf5c878SMichael Roth  *
6bbf5c878SMichael Roth  * Authors:
7bbf5c878SMichael Roth  *  Michael Roth      <mdroth@linux.vnet.ibm.com>
8bbf5c878SMichael Roth  *
9bbf5c878SMichael Roth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10bbf5c878SMichael Roth  * See the COPYING file in the top-level directory.
11bbf5c878SMichael Roth  */
12bbf5c878SMichael Roth 
130d75590dSPeter Maydell #include "qemu/osdep.h"
14da34e65cSMarkus Armbruster #include "qapi/error.h"
154771d756SPaolo Bonzini #include "cpu.h"
16f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
17bbf5c878SMichael Roth #include "hw/ppc/spapr_drc.h"
18bbf5c878SMichael Roth #include "qom/object.h"
19bbf5c878SMichael Roth #include "hw/qdev.h"
20bbf5c878SMichael Roth #include "qapi/visitor.h"
21bbf5c878SMichael Roth #include "qemu/error-report.h"
220cb688d2SMichael Roth #include "hw/ppc/spapr.h" /* for RTAS return codes */
2331834723SDaniel Henrique Barboza #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
2424ac7755SLaurent Vivier #include "trace.h"
25bbf5c878SMichael Roth 
26bbf5c878SMichael Roth #define DRC_CONTAINER_PATH "/dr-connector"
27bbf5c878SMichael Roth #define DRC_INDEX_TYPE_SHIFT 28
28627c2ef7SDavid Gibson #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
29bbf5c878SMichael Roth 
302d335818SDavid Gibson sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc)
312d335818SDavid Gibson {
322d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
332d335818SDavid Gibson 
342d335818SDavid Gibson     return 1 << drck->typeshift;
352d335818SDavid Gibson }
362d335818SDavid Gibson 
370b55aa91SDavid Gibson uint32_t spapr_drc_index(sPAPRDRConnector *drc)
38bbf5c878SMichael Roth {
392d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
402d335818SDavid Gibson 
41bbf5c878SMichael Roth     /* no set format for a drc index: it only needs to be globally
42bbf5c878SMichael Roth      * unique. this is how we encode the DRC type on bare-metal
43bbf5c878SMichael Roth      * however, so might as well do that here
44bbf5c878SMichael Roth      */
452d335818SDavid Gibson     return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
462d335818SDavid Gibson         | (drc->id & DRC_INDEX_ID_MASK);
47bbf5c878SMichael Roth }
48bbf5c878SMichael Roth 
490dfabd39SDavid Gibson static uint32_t drc_isolate_physical(sPAPRDRConnector *drc)
50bbf5c878SMichael Roth {
51b8fdd530SDavid Gibson     /* if the guest is configuring a device attached to this DRC, we
52b8fdd530SDavid Gibson      * should reset the configuration state at this point since it may
53b8fdd530SDavid Gibson      * no longer be reliable (guest released device and needs to start
54b8fdd530SDavid Gibson      * over, or unplug occurred so the FDT is no longer valid)
55b8fdd530SDavid Gibson      */
56b8fdd530SDavid Gibson     g_free(drc->ccs);
57b8fdd530SDavid Gibson     drc->ccs = NULL;
58b8fdd530SDavid Gibson 
590dfabd39SDavid Gibson     drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
609d1852ceSMichael Roth 
61bbf5c878SMichael Roth     /* if we're awaiting release, but still in an unconfigured state,
62bbf5c878SMichael Roth      * it's likely the guest is still in the process of configuring
63bbf5c878SMichael Roth      * the device and is transitioning the devices to an ISOLATED
64bbf5c878SMichael Roth      * state as a part of that process. so we only complete the
65bbf5c878SMichael Roth      * removal when this transition happens for a device in a
660dfabd39SDavid Gibson      * configured state, as suggested by the state diagram from PAPR+
670dfabd39SDavid Gibson      * 2.7, 13.4
68bbf5c878SMichael Roth      */
69bbf5c878SMichael Roth     if (drc->awaiting_release) {
700b55aa91SDavid Gibson         uint32_t drc_index = spapr_drc_index(drc);
71bbf5c878SMichael Roth         if (drc->configured) {
720b55aa91SDavid Gibson             trace_spapr_drc_set_isolation_state_finalizing(drc_index);
730be4e886SDavid Gibson             spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
74bbf5c878SMichael Roth         } else {
750b55aa91SDavid Gibson             trace_spapr_drc_set_isolation_state_deferring(drc_index);
76bbf5c878SMichael Roth         }
77bbf5c878SMichael Roth     }
78bbf5c878SMichael Roth     drc->configured = false;
790dfabd39SDavid Gibson 
800dfabd39SDavid Gibson     return RTAS_OUT_SUCCESS;
81bbf5c878SMichael Roth }
82bbf5c878SMichael Roth 
830dfabd39SDavid Gibson static uint32_t drc_unisolate_physical(sPAPRDRConnector *drc)
840dfabd39SDavid Gibson {
850dfabd39SDavid Gibson     /* cannot unisolate a non-existent resource, and, or resources
860dfabd39SDavid Gibson      * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
870dfabd39SDavid Gibson      * 13.5.3.5)
880dfabd39SDavid Gibson      */
890dfabd39SDavid Gibson     if (!drc->dev) {
900dfabd39SDavid Gibson         return RTAS_OUT_NO_SUCH_INDICATOR;
910dfabd39SDavid Gibson     }
920dfabd39SDavid Gibson 
930dfabd39SDavid Gibson     drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
940dfabd39SDavid Gibson 
950dfabd39SDavid Gibson     return RTAS_OUT_SUCCESS;
960dfabd39SDavid Gibson }
970dfabd39SDavid Gibson 
980dfabd39SDavid Gibson static uint32_t drc_isolate_logical(sPAPRDRConnector *drc)
990dfabd39SDavid Gibson {
1000dfabd39SDavid Gibson     /* if the guest is configuring a device attached to this DRC, we
1010dfabd39SDavid Gibson      * should reset the configuration state at this point since it may
1020dfabd39SDavid Gibson      * no longer be reliable (guest released device and needs to start
1030dfabd39SDavid Gibson      * over, or unplug occurred so the FDT is no longer valid)
1040dfabd39SDavid Gibson      */
1050dfabd39SDavid Gibson     g_free(drc->ccs);
1060dfabd39SDavid Gibson     drc->ccs = NULL;
1070dfabd39SDavid Gibson 
1080dfabd39SDavid Gibson     /*
1090dfabd39SDavid Gibson      * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
1100dfabd39SDavid Gibson      * belong to a DIMM device that is marked for removal.
1110dfabd39SDavid Gibson      *
1120dfabd39SDavid Gibson      * Currently the guest userspace tool drmgr that drives the memory
1130dfabd39SDavid Gibson      * hotplug/unplug will just try to remove a set of 'removable' LMBs
1140dfabd39SDavid Gibson      * in response to a hot unplug request that is based on drc-count.
1150dfabd39SDavid Gibson      * If the LMB being removed doesn't belong to a DIMM device that is
1160dfabd39SDavid Gibson      * actually being unplugged, fail the isolation request here.
1170dfabd39SDavid Gibson      */
1180dfabd39SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
1190dfabd39SDavid Gibson         && !drc->awaiting_release) {
1200dfabd39SDavid Gibson         return RTAS_OUT_HW_ERROR;
1210dfabd39SDavid Gibson     }
1220dfabd39SDavid Gibson 
1230dfabd39SDavid Gibson     drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
1240dfabd39SDavid Gibson 
1250dfabd39SDavid Gibson     /* if we're awaiting release, but still in an unconfigured state,
1260dfabd39SDavid Gibson      * it's likely the guest is still in the process of configuring
1270dfabd39SDavid Gibson      * the device and is transitioning the devices to an ISOLATED
1280dfabd39SDavid Gibson      * state as a part of that process. so we only complete the
1290dfabd39SDavid Gibson      * removal when this transition happens for a device in a
1300dfabd39SDavid Gibson      * configured state, as suggested by the state diagram from PAPR+
1310dfabd39SDavid Gibson      * 2.7, 13.4
1320dfabd39SDavid Gibson      */
1330dfabd39SDavid Gibson     if (drc->awaiting_release) {
1340dfabd39SDavid Gibson         uint32_t drc_index = spapr_drc_index(drc);
1350dfabd39SDavid Gibson         if (drc->configured) {
1360dfabd39SDavid Gibson             trace_spapr_drc_set_isolation_state_finalizing(drc_index);
1370dfabd39SDavid Gibson             spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
1380dfabd39SDavid Gibson         } else {
1390dfabd39SDavid Gibson             trace_spapr_drc_set_isolation_state_deferring(drc_index);
1400dfabd39SDavid Gibson         }
1410dfabd39SDavid Gibson     }
1420dfabd39SDavid Gibson     drc->configured = false;
1430dfabd39SDavid Gibson 
1440dfabd39SDavid Gibson     return RTAS_OUT_SUCCESS;
1450dfabd39SDavid Gibson }
1460dfabd39SDavid Gibson 
1470dfabd39SDavid Gibson static uint32_t drc_unisolate_logical(sPAPRDRConnector *drc)
1480dfabd39SDavid Gibson {
1490dfabd39SDavid Gibson     /* cannot unisolate a non-existent resource, and, or resources
1500dfabd39SDavid Gibson      * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
1510dfabd39SDavid Gibson      * 13.5.3.5)
1520dfabd39SDavid Gibson      */
1530dfabd39SDavid Gibson     if (!drc->dev ||
1540dfabd39SDavid Gibson         drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
1550dfabd39SDavid Gibson         return RTAS_OUT_NO_SUCH_INDICATOR;
1560dfabd39SDavid Gibson     }
1570dfabd39SDavid Gibson 
1580dfabd39SDavid Gibson     drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
1590dfabd39SDavid Gibson 
1600cb688d2SMichael Roth     return RTAS_OUT_SUCCESS;
161bbf5c878SMichael Roth }
162bbf5c878SMichael Roth 
16361736732SDavid Gibson static uint32_t drc_set_usable(sPAPRDRConnector *drc)
164bbf5c878SMichael Roth {
1659d1852ceSMichael Roth     /* if there's no resource/device associated with the DRC, there's
1669d1852ceSMichael Roth      * no way for us to put it in an allocation state consistent with
1679d1852ceSMichael Roth      * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
1689d1852ceSMichael Roth      * result in an RTAS return code of -3 / "no such indicator"
1699d1852ceSMichael Roth      */
1709d1852ceSMichael Roth     if (!drc->dev) {
1719d1852ceSMichael Roth         return RTAS_OUT_NO_SUCH_INDICATOR;
1729d1852ceSMichael Roth     }
17361736732SDavid Gibson     if (drc->awaiting_release && drc->awaiting_allocation) {
17461736732SDavid Gibson         /* kernel is acknowledging a previous hotplug event
17561736732SDavid Gibson          * while we are already removing it.
17661736732SDavid Gibson          * it's safe to ignore awaiting_allocation here since we know the
17761736732SDavid Gibson          * situation is predicated on the guest either already having done
17861736732SDavid Gibson          * so (boot-time hotplug), or never being able to acquire in the
17961736732SDavid Gibson          * first place (hotplug followed by immediate unplug).
18061736732SDavid Gibson          */
18161736732SDavid Gibson         return RTAS_OUT_NO_SUCH_INDICATOR;
1829d1852ceSMichael Roth     }
1839d1852ceSMichael Roth 
18461736732SDavid Gibson     drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
18561736732SDavid Gibson     drc->awaiting_allocation = false;
18661736732SDavid Gibson 
18761736732SDavid Gibson     return RTAS_OUT_SUCCESS;
18861736732SDavid Gibson }
18961736732SDavid Gibson 
19061736732SDavid Gibson static uint32_t drc_set_unusable(sPAPRDRConnector *drc)
19161736732SDavid Gibson {
19261736732SDavid Gibson     drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_UNUSABLE;
19361736732SDavid Gibson     if (drc->awaiting_release) {
1940b55aa91SDavid Gibson         uint32_t drc_index = spapr_drc_index(drc);
1950b55aa91SDavid Gibson         trace_spapr_drc_set_allocation_state_finalizing(drc_index);
1960be4e886SDavid Gibson         spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
197bbf5c878SMichael Roth     }
19861736732SDavid Gibson 
1990cb688d2SMichael Roth     return RTAS_OUT_SUCCESS;
200bbf5c878SMichael Roth }
201bbf5c878SMichael Roth 
20279808336SDavid Gibson static const char *spapr_drc_name(sPAPRDRConnector *drc)
203bbf5c878SMichael Roth {
20479808336SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
20579808336SDavid Gibson 
20679808336SDavid Gibson     /* human-readable name for a DRC to encode into the DT
20779808336SDavid Gibson      * description. this is mainly only used within a guest in place
20879808336SDavid Gibson      * of the unique DRC index.
20979808336SDavid Gibson      *
21079808336SDavid Gibson      * in the case of VIO/PCI devices, it corresponds to a "location
21179808336SDavid Gibson      * code" that maps a logical device/function (DRC index) to a
21279808336SDavid Gibson      * physical (or virtual in the case of VIO) location in the system
21379808336SDavid Gibson      * by chaining together the "location label" for each
21479808336SDavid Gibson      * encapsulating component.
21579808336SDavid Gibson      *
21679808336SDavid Gibson      * since this is more to do with diagnosing physical hardware
21779808336SDavid Gibson      * issues than guest compatibility, we choose location codes/DRC
21879808336SDavid Gibson      * names that adhere to the documented format, but avoid encoding
21979808336SDavid Gibson      * the entire topology information into the label/code, instead
22079808336SDavid Gibson      * just using the location codes based on the labels for the
22179808336SDavid Gibson      * endpoints (VIO/PCI adaptor connectors), which is basically just
22279808336SDavid Gibson      * "C" followed by an integer ID.
22379808336SDavid Gibson      *
22479808336SDavid Gibson      * DRC names as documented by PAPR+ v2.7, 13.5.2.4
22579808336SDavid Gibson      * location codes as documented by PAPR+ v2.7, 12.3.1.5
22679808336SDavid Gibson      */
22779808336SDavid Gibson     return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
228bbf5c878SMichael Roth }
229bbf5c878SMichael Roth 
230bbf5c878SMichael Roth /*
231bbf5c878SMichael Roth  * dr-entity-sense sensor value
232bbf5c878SMichael Roth  * returned via get-sensor-state RTAS calls
233bbf5c878SMichael Roth  * as expected by state diagram in PAPR+ 2.7, 13.4
234bbf5c878SMichael Roth  * based on the current allocation/indicator/power states
235bbf5c878SMichael Roth  * for the DR connector.
236bbf5c878SMichael Roth  */
237f224d35bSDavid Gibson static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc)
238bbf5c878SMichael Roth {
239f224d35bSDavid Gibson     /* this assumes all PCI devices are assigned to a 'live insertion'
240f224d35bSDavid Gibson      * power domain, where QEMU manages power state automatically as
241f224d35bSDavid Gibson      * opposed to the guest. present, non-PCI resources are unaffected
242f224d35bSDavid Gibson      * by power state.
243bbf5c878SMichael Roth      */
244f224d35bSDavid Gibson     return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
245f224d35bSDavid Gibson         : SPAPR_DR_ENTITY_SENSE_EMPTY;
246bbf5c878SMichael Roth }
247bbf5c878SMichael Roth 
248f224d35bSDavid Gibson static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc)
249f224d35bSDavid Gibson {
250f224d35bSDavid Gibson     if (drc->dev
251f224d35bSDavid Gibson         && (drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE)) {
252f224d35bSDavid Gibson         return SPAPR_DR_ENTITY_SENSE_PRESENT;
253f224d35bSDavid Gibson     } else {
254f224d35bSDavid Gibson         return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
255f224d35bSDavid Gibson     }
256bbf5c878SMichael Roth }
257bbf5c878SMichael Roth 
258d7bce999SEric Blake static void prop_get_index(Object *obj, Visitor *v, const char *name,
259d7bce999SEric Blake                            void *opaque, Error **errp)
260bbf5c878SMichael Roth {
261bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
2620b55aa91SDavid Gibson     uint32_t value = spapr_drc_index(drc);
26351e72bc1SEric Blake     visit_type_uint32(v, name, &value, errp);
264bbf5c878SMichael Roth }
265bbf5c878SMichael Roth 
266d7bce999SEric Blake static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
267d7bce999SEric Blake                          void *opaque, Error **errp)
268bbf5c878SMichael Roth {
269bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
270c75304a1SMarkus Armbruster     Error *err = NULL;
271bbf5c878SMichael Roth     int fdt_offset_next, fdt_offset, fdt_depth;
272bbf5c878SMichael Roth     void *fdt;
273bbf5c878SMichael Roth 
274bbf5c878SMichael Roth     if (!drc->fdt) {
275a543a554SEric Blake         visit_type_null(v, NULL, errp);
276bbf5c878SMichael Roth         return;
277bbf5c878SMichael Roth     }
278bbf5c878SMichael Roth 
279bbf5c878SMichael Roth     fdt = drc->fdt;
280bbf5c878SMichael Roth     fdt_offset = drc->fdt_start_offset;
281bbf5c878SMichael Roth     fdt_depth = 0;
282bbf5c878SMichael Roth 
283bbf5c878SMichael Roth     do {
284bbf5c878SMichael Roth         const char *name = NULL;
285bbf5c878SMichael Roth         const struct fdt_property *prop = NULL;
286bbf5c878SMichael Roth         int prop_len = 0, name_len = 0;
287bbf5c878SMichael Roth         uint32_t tag;
288bbf5c878SMichael Roth 
289bbf5c878SMichael Roth         tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
290bbf5c878SMichael Roth         switch (tag) {
291bbf5c878SMichael Roth         case FDT_BEGIN_NODE:
292bbf5c878SMichael Roth             fdt_depth++;
293bbf5c878SMichael Roth             name = fdt_get_name(fdt, fdt_offset, &name_len);
294337283dfSEric Blake             visit_start_struct(v, name, NULL, 0, &err);
295c75304a1SMarkus Armbruster             if (err) {
296c75304a1SMarkus Armbruster                 error_propagate(errp, err);
297c75304a1SMarkus Armbruster                 return;
298c75304a1SMarkus Armbruster             }
299bbf5c878SMichael Roth             break;
300bbf5c878SMichael Roth         case FDT_END_NODE:
301bbf5c878SMichael Roth             /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
302bbf5c878SMichael Roth             g_assert(fdt_depth > 0);
30315c2f669SEric Blake             visit_check_struct(v, &err);
3041158bb2aSEric Blake             visit_end_struct(v, NULL);
305c75304a1SMarkus Armbruster             if (err) {
306c75304a1SMarkus Armbruster                 error_propagate(errp, err);
307c75304a1SMarkus Armbruster                 return;
308c75304a1SMarkus Armbruster             }
309bbf5c878SMichael Roth             fdt_depth--;
310bbf5c878SMichael Roth             break;
311bbf5c878SMichael Roth         case FDT_PROP: {
312bbf5c878SMichael Roth             int i;
313bbf5c878SMichael Roth             prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
314bbf5c878SMichael Roth             name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
315d9f62ddeSEric Blake             visit_start_list(v, name, NULL, 0, &err);
316c75304a1SMarkus Armbruster             if (err) {
317c75304a1SMarkus Armbruster                 error_propagate(errp, err);
318c75304a1SMarkus Armbruster                 return;
319bbf5c878SMichael Roth             }
320c75304a1SMarkus Armbruster             for (i = 0; i < prop_len; i++) {
32151e72bc1SEric Blake                 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
322c75304a1SMarkus Armbruster                 if (err) {
323c75304a1SMarkus Armbruster                     error_propagate(errp, err);
324c75304a1SMarkus Armbruster                     return;
325c75304a1SMarkus Armbruster                 }
326c75304a1SMarkus Armbruster             }
327a4a1c70dSMarkus Armbruster             visit_check_list(v, &err);
3281158bb2aSEric Blake             visit_end_list(v, NULL);
329a4a1c70dSMarkus Armbruster             if (err) {
330a4a1c70dSMarkus Armbruster                 error_propagate(errp, err);
331a4a1c70dSMarkus Armbruster                 return;
332a4a1c70dSMarkus Armbruster             }
333bbf5c878SMichael Roth             break;
334bbf5c878SMichael Roth         }
335bbf5c878SMichael Roth         default:
336bbf5c878SMichael Roth             error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
337bbf5c878SMichael Roth         }
338bbf5c878SMichael Roth         fdt_offset = fdt_offset_next;
339bbf5c878SMichael Roth     } while (fdt_depth != 0);
340bbf5c878SMichael Roth }
341bbf5c878SMichael Roth 
3420be4e886SDavid Gibson void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
343bbf5c878SMichael Roth                       int fdt_start_offset, bool coldplug, Error **errp)
344bbf5c878SMichael Roth {
3450b55aa91SDavid Gibson     trace_spapr_drc_attach(spapr_drc_index(drc));
346bbf5c878SMichael Roth 
347bbf5c878SMichael Roth     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
348bbf5c878SMichael Roth         error_setg(errp, "an attached device is still awaiting release");
349bbf5c878SMichael Roth         return;
350bbf5c878SMichael Roth     }
3512d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
352bbf5c878SMichael Roth         g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
353bbf5c878SMichael Roth     }
354bbf5c878SMichael Roth     g_assert(fdt || coldplug);
355bbf5c878SMichael Roth 
356bbf5c878SMichael Roth     drc->dev = d;
357bbf5c878SMichael Roth     drc->fdt = fdt;
358bbf5c878SMichael Roth     drc->fdt_start_offset = fdt_start_offset;
359785652dcSLaurent Vivier     drc->configured = coldplug;
360bbf5c878SMichael Roth 
3612d335818SDavid Gibson     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
362aab99135SBharata B Rao         drc->awaiting_allocation = true;
363aab99135SBharata B Rao     }
364aab99135SBharata B Rao 
365bbf5c878SMichael Roth     object_property_add_link(OBJECT(drc), "device",
366bbf5c878SMichael Roth                              object_get_typename(OBJECT(drc->dev)),
367bbf5c878SMichael Roth                              (Object **)(&drc->dev),
368bbf5c878SMichael Roth                              NULL, 0, NULL);
369bbf5c878SMichael Roth }
370bbf5c878SMichael Roth 
3719c914e53SDavid Gibson static void spapr_drc_release(sPAPRDRConnector *drc)
372bbf5c878SMichael Roth {
3732d335818SDavid Gibson     /* Calling release callbacks based on spapr_drc_type(drc). */
3742d335818SDavid Gibson     switch (spapr_drc_type(drc)) {
37531834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_CPU:
37631834723SDaniel Henrique Barboza         spapr_core_release(drc->dev);
37731834723SDaniel Henrique Barboza         break;
37831834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PCI:
37931834723SDaniel Henrique Barboza         spapr_phb_remove_pci_device_cb(drc->dev);
38031834723SDaniel Henrique Barboza         break;
38131834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_LMB:
38231834723SDaniel Henrique Barboza         spapr_lmb_release(drc->dev);
38331834723SDaniel Henrique Barboza         break;
38431834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PHB:
38531834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_VIO:
38631834723SDaniel Henrique Barboza     default:
38731834723SDaniel Henrique Barboza         g_assert(false);
388bbf5c878SMichael Roth     }
389bbf5c878SMichael Roth 
390bbf5c878SMichael Roth     drc->awaiting_release = false;
391bbf5c878SMichael Roth     g_free(drc->fdt);
392bbf5c878SMichael Roth     drc->fdt = NULL;
393bbf5c878SMichael Roth     drc->fdt_start_offset = 0;
394bbf5c878SMichael Roth     object_property_del(OBJECT(drc), "device", NULL);
395bbf5c878SMichael Roth     drc->dev = NULL;
396bbf5c878SMichael Roth }
397bbf5c878SMichael Roth 
3989c914e53SDavid Gibson void spapr_drc_detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
3999c914e53SDavid Gibson {
4009c914e53SDavid Gibson     trace_spapr_drc_detach(spapr_drc_index(drc));
4019c914e53SDavid Gibson 
4029c914e53SDavid Gibson     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
4039c914e53SDavid Gibson         trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
4049c914e53SDavid Gibson         drc->awaiting_release = true;
4059c914e53SDavid Gibson         return;
4069c914e53SDavid Gibson     }
4079c914e53SDavid Gibson 
4089c914e53SDavid Gibson     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
4099c914e53SDavid Gibson         drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
4109c914e53SDavid Gibson         trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
4119c914e53SDavid Gibson         drc->awaiting_release = true;
4129c914e53SDavid Gibson         return;
4139c914e53SDavid Gibson     }
4149c914e53SDavid Gibson 
4159c914e53SDavid Gibson     if (drc->awaiting_allocation) {
4169c914e53SDavid Gibson         drc->awaiting_release = true;
4179c914e53SDavid Gibson         trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
4189c914e53SDavid Gibson         return;
4199c914e53SDavid Gibson     }
4209c914e53SDavid Gibson 
4219c914e53SDavid Gibson     spapr_drc_release(drc);
4229c914e53SDavid Gibson }
4239c914e53SDavid Gibson 
424bbf5c878SMichael Roth static bool release_pending(sPAPRDRConnector *drc)
425bbf5c878SMichael Roth {
426bbf5c878SMichael Roth     return drc->awaiting_release;
427bbf5c878SMichael Roth }
428bbf5c878SMichael Roth 
429bbf5c878SMichael Roth static void reset(DeviceState *d)
430bbf5c878SMichael Roth {
431bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
432bbf5c878SMichael Roth 
4330b55aa91SDavid Gibson     trace_spapr_drc_reset(spapr_drc_index(drc));
434b8fdd530SDavid Gibson 
435b8fdd530SDavid Gibson     g_free(drc->ccs);
436b8fdd530SDavid Gibson     drc->ccs = NULL;
437b8fdd530SDavid Gibson 
438bbf5c878SMichael Roth     /* immediately upon reset we can safely assume DRCs whose devices
4394f9242fcSDavid Gibson      * are pending removal can be safely removed.
440bbf5c878SMichael Roth      */
441bbf5c878SMichael Roth     if (drc->awaiting_release) {
4424f9242fcSDavid Gibson         spapr_drc_release(drc);
443bbf5c878SMichael Roth     }
444bbf5c878SMichael Roth 
4454f9242fcSDavid Gibson     drc->awaiting_allocation = false;
4464f9242fcSDavid Gibson 
4474f9242fcSDavid Gibson     if (drc->dev) {
4484f9242fcSDavid Gibson         /* A device present at reset is coldplugged */
4494f9242fcSDavid Gibson         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
4504f9242fcSDavid Gibson         if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
4514f9242fcSDavid Gibson             drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
4524f9242fcSDavid Gibson         }
453*f8dc2983SDavid Gibson         drc->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
4544f9242fcSDavid Gibson     } else {
4554f9242fcSDavid Gibson         /* Otherwise device is absent, but might be hotplugged */
4564f9242fcSDavid Gibson         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
4574f9242fcSDavid Gibson         if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
4584f9242fcSDavid Gibson             drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_UNUSABLE;
459bbf5c878SMichael Roth         }
460*f8dc2983SDavid Gibson         drc->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
461bbf5c878SMichael Roth     }
462bbf5c878SMichael Roth }
463bbf5c878SMichael Roth 
464a50919ddSDaniel Henrique Barboza static bool spapr_drc_needed(void *opaque)
465a50919ddSDaniel Henrique Barboza {
466a50919ddSDaniel Henrique Barboza     sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
467a50919ddSDaniel Henrique Barboza     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
468a50919ddSDaniel Henrique Barboza     bool rc = false;
469f224d35bSDavid Gibson     sPAPRDREntitySense value = drck->dr_entity_sense(drc);
470a50919ddSDaniel Henrique Barboza 
471a50919ddSDaniel Henrique Barboza     /* If no dev is plugged in there is no need to migrate the DRC state */
472a50919ddSDaniel Henrique Barboza     if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
473a50919ddSDaniel Henrique Barboza         return false;
474a50919ddSDaniel Henrique Barboza     }
475a50919ddSDaniel Henrique Barboza 
476a50919ddSDaniel Henrique Barboza     /*
477a50919ddSDaniel Henrique Barboza      * If there is dev plugged in, we need to migrate the DRC state when
478a50919ddSDaniel Henrique Barboza      * it is different from cold-plugged state
479a50919ddSDaniel Henrique Barboza      */
4802d335818SDavid Gibson     switch (spapr_drc_type(drc)) {
481a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PCI:
482a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_CPU:
483a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_LMB:
484a32e900bSGreg Kurz         rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
485a32e900bSGreg Kurz                (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
486307b7715SDavid Gibson                drc->configured && !drc->awaiting_release);
487a50919ddSDaniel Henrique Barboza         break;
488a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PHB:
489a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_VIO:
490a50919ddSDaniel Henrique Barboza     default:
491a32e900bSGreg Kurz         g_assert_not_reached();
492a50919ddSDaniel Henrique Barboza     }
493a50919ddSDaniel Henrique Barboza     return rc;
494a50919ddSDaniel Henrique Barboza }
495a50919ddSDaniel Henrique Barboza 
496a50919ddSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_drc = {
497a50919ddSDaniel Henrique Barboza     .name = "spapr_drc",
498a50919ddSDaniel Henrique Barboza     .version_id = 1,
499a50919ddSDaniel Henrique Barboza     .minimum_version_id = 1,
500a50919ddSDaniel Henrique Barboza     .needed = spapr_drc_needed,
501a50919ddSDaniel Henrique Barboza     .fields  = (VMStateField []) {
502a50919ddSDaniel Henrique Barboza         VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
503a50919ddSDaniel Henrique Barboza         VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
504cd74d27eSDavid Gibson         VMSTATE_UINT32(dr_indicator, sPAPRDRConnector),
505a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(configured, sPAPRDRConnector),
506a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
507a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
508a50919ddSDaniel Henrique Barboza         VMSTATE_END_OF_LIST()
509a50919ddSDaniel Henrique Barboza     }
510a50919ddSDaniel Henrique Barboza };
511a50919ddSDaniel Henrique Barboza 
512bbf5c878SMichael Roth static void realize(DeviceState *d, Error **errp)
513bbf5c878SMichael Roth {
514bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
515bbf5c878SMichael Roth     Object *root_container;
516bbf5c878SMichael Roth     char link_name[256];
517bbf5c878SMichael Roth     gchar *child_name;
518bbf5c878SMichael Roth     Error *err = NULL;
519bbf5c878SMichael Roth 
5200b55aa91SDavid Gibson     trace_spapr_drc_realize(spapr_drc_index(drc));
521bbf5c878SMichael Roth     /* NOTE: we do this as part of realize/unrealize due to the fact
522bbf5c878SMichael Roth      * that the guest will communicate with the DRC via RTAS calls
523bbf5c878SMichael Roth      * referencing the global DRC index. By unlinking the DRC
524bbf5c878SMichael Roth      * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
525bbf5c878SMichael Roth      * inaccessible by the guest, since lookups rely on this path
526bbf5c878SMichael Roth      * existing in the composition tree
527bbf5c878SMichael Roth      */
528bbf5c878SMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
5290b55aa91SDavid Gibson     snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
530bbf5c878SMichael Roth     child_name = object_get_canonical_path_component(OBJECT(drc));
5310b55aa91SDavid Gibson     trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
532bbf5c878SMichael Roth     object_property_add_alias(root_container, link_name,
533bbf5c878SMichael Roth                               drc->owner, child_name, &err);
534bbf5c878SMichael Roth     if (err) {
5354fffeb5eSMarkus Armbruster         error_report_err(err);
536bbf5c878SMichael Roth         object_unref(OBJECT(drc));
537bbf5c878SMichael Roth     }
538586d2142SGonglei     g_free(child_name);
5390b55aa91SDavid Gibson     vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
540a50919ddSDaniel Henrique Barboza                      drc);
5410b55aa91SDavid Gibson     trace_spapr_drc_realize_complete(spapr_drc_index(drc));
542bbf5c878SMichael Roth }
543bbf5c878SMichael Roth 
544bbf5c878SMichael Roth static void unrealize(DeviceState *d, Error **errp)
545bbf5c878SMichael Roth {
546bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
547bbf5c878SMichael Roth     Object *root_container;
548bbf5c878SMichael Roth     char name[256];
549bbf5c878SMichael Roth     Error *err = NULL;
550bbf5c878SMichael Roth 
5510b55aa91SDavid Gibson     trace_spapr_drc_unrealize(spapr_drc_index(drc));
552bbf5c878SMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
5530b55aa91SDavid Gibson     snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
554bbf5c878SMichael Roth     object_property_del(root_container, name, &err);
555bbf5c878SMichael Roth     if (err) {
5564fffeb5eSMarkus Armbruster         error_report_err(err);
557bbf5c878SMichael Roth         object_unref(OBJECT(drc));
558bbf5c878SMichael Roth     }
559bbf5c878SMichael Roth }
560bbf5c878SMichael Roth 
5612d335818SDavid Gibson sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
562bbf5c878SMichael Roth                                          uint32_t id)
563bbf5c878SMichael Roth {
5642d335818SDavid Gibson     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
56594649d42SDavid Gibson     char *prop_name;
566bbf5c878SMichael Roth 
567bbf5c878SMichael Roth     drc->id = id;
568bbf5c878SMichael Roth     drc->owner = owner;
5690b55aa91SDavid Gibson     prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
5700b55aa91SDavid Gibson                                 spapr_drc_index(drc));
57194649d42SDavid Gibson     object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
572bbf5c878SMichael Roth     object_property_set_bool(OBJECT(drc), true, "realized", NULL);
57394649d42SDavid Gibson     g_free(prop_name);
574bbf5c878SMichael Roth 
575bbf5c878SMichael Roth     /* PCI slot always start in a USABLE state, and stay there */
5762d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
577bbf5c878SMichael Roth         drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
578bbf5c878SMichael Roth     }
579bbf5c878SMichael Roth 
580bbf5c878SMichael Roth     return drc;
581bbf5c878SMichael Roth }
582bbf5c878SMichael Roth 
583bbf5c878SMichael Roth static void spapr_dr_connector_instance_init(Object *obj)
584bbf5c878SMichael Roth {
585bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
586bbf5c878SMichael Roth 
587bbf5c878SMichael Roth     object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
588bbf5c878SMichael Roth     object_property_add(obj, "index", "uint32", prop_get_index,
589bbf5c878SMichael Roth                         NULL, NULL, NULL, NULL);
590bbf5c878SMichael Roth     object_property_add(obj, "fdt", "struct", prop_get_fdt,
591bbf5c878SMichael Roth                         NULL, NULL, NULL, NULL);
592bbf5c878SMichael Roth }
593bbf5c878SMichael Roth 
594bbf5c878SMichael Roth static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
595bbf5c878SMichael Roth {
596bbf5c878SMichael Roth     DeviceClass *dk = DEVICE_CLASS(k);
597bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
598bbf5c878SMichael Roth 
599bbf5c878SMichael Roth     dk->reset = reset;
600bbf5c878SMichael Roth     dk->realize = realize;
601bbf5c878SMichael Roth     dk->unrealize = unrealize;
602bbf5c878SMichael Roth     drck->release_pending = release_pending;
603c401ae8cSMarkus Armbruster     /*
604c401ae8cSMarkus Armbruster      * Reason: it crashes FIXME find and document the real reason
605c401ae8cSMarkus Armbruster      */
606e90f2a8cSEduardo Habkost     dk->user_creatable = false;
607bbf5c878SMichael Roth }
608bbf5c878SMichael Roth 
609f224d35bSDavid Gibson static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
610f224d35bSDavid Gibson {
611f224d35bSDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
612f224d35bSDavid Gibson 
613f224d35bSDavid Gibson     drck->dr_entity_sense = physical_entity_sense;
6140dfabd39SDavid Gibson     drck->isolate = drc_isolate_physical;
6150dfabd39SDavid Gibson     drck->unisolate = drc_unisolate_physical;
616f224d35bSDavid Gibson }
617f224d35bSDavid Gibson 
618f224d35bSDavid Gibson static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
619f224d35bSDavid Gibson {
620f224d35bSDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
621f224d35bSDavid Gibson 
622f224d35bSDavid Gibson     drck->dr_entity_sense = logical_entity_sense;
6230dfabd39SDavid Gibson     drck->isolate = drc_isolate_logical;
6240dfabd39SDavid Gibson     drck->unisolate = drc_unisolate_logical;
625f224d35bSDavid Gibson }
626f224d35bSDavid Gibson 
6272d335818SDavid Gibson static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
6282d335818SDavid Gibson {
6292d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
6302d335818SDavid Gibson 
6312d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
6321693ea16SDavid Gibson     drck->typename = "CPU";
63379808336SDavid Gibson     drck->drc_name_prefix = "CPU ";
6342d335818SDavid Gibson }
6352d335818SDavid Gibson 
6362d335818SDavid Gibson static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
6372d335818SDavid Gibson {
6382d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
6392d335818SDavid Gibson 
6402d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
6411693ea16SDavid Gibson     drck->typename = "28";
64279808336SDavid Gibson     drck->drc_name_prefix = "C";
6432d335818SDavid Gibson }
6442d335818SDavid Gibson 
6452d335818SDavid Gibson static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
6462d335818SDavid Gibson {
6472d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
6482d335818SDavid Gibson 
6492d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
6501693ea16SDavid Gibson     drck->typename = "MEM";
65179808336SDavid Gibson     drck->drc_name_prefix = "LMB ";
6522d335818SDavid Gibson }
6532d335818SDavid Gibson 
654bbf5c878SMichael Roth static const TypeInfo spapr_dr_connector_info = {
655bbf5c878SMichael Roth     .name          = TYPE_SPAPR_DR_CONNECTOR,
656bbf5c878SMichael Roth     .parent        = TYPE_DEVICE,
657bbf5c878SMichael Roth     .instance_size = sizeof(sPAPRDRConnector),
658bbf5c878SMichael Roth     .instance_init = spapr_dr_connector_instance_init,
659bbf5c878SMichael Roth     .class_size    = sizeof(sPAPRDRConnectorClass),
660bbf5c878SMichael Roth     .class_init    = spapr_dr_connector_class_init,
6612d335818SDavid Gibson     .abstract      = true,
6622d335818SDavid Gibson };
6632d335818SDavid Gibson 
6642d335818SDavid Gibson static const TypeInfo spapr_drc_physical_info = {
6652d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_PHYSICAL,
6662d335818SDavid Gibson     .parent        = TYPE_SPAPR_DR_CONNECTOR,
6672d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
668f224d35bSDavid Gibson     .class_init    = spapr_drc_physical_class_init,
6692d335818SDavid Gibson     .abstract      = true,
6702d335818SDavid Gibson };
6712d335818SDavid Gibson 
6722d335818SDavid Gibson static const TypeInfo spapr_drc_logical_info = {
6732d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_LOGICAL,
6742d335818SDavid Gibson     .parent        = TYPE_SPAPR_DR_CONNECTOR,
6752d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
676f224d35bSDavid Gibson     .class_init    = spapr_drc_logical_class_init,
6772d335818SDavid Gibson     .abstract      = true,
6782d335818SDavid Gibson };
6792d335818SDavid Gibson 
6802d335818SDavid Gibson static const TypeInfo spapr_drc_cpu_info = {
6812d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_CPU,
6822d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_LOGICAL,
6832d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
6842d335818SDavid Gibson     .class_init    = spapr_drc_cpu_class_init,
6852d335818SDavid Gibson };
6862d335818SDavid Gibson 
6872d335818SDavid Gibson static const TypeInfo spapr_drc_pci_info = {
6882d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_PCI,
6892d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_PHYSICAL,
6902d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
6912d335818SDavid Gibson     .class_init    = spapr_drc_pci_class_init,
6922d335818SDavid Gibson };
6932d335818SDavid Gibson 
6942d335818SDavid Gibson static const TypeInfo spapr_drc_lmb_info = {
6952d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_LMB,
6962d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_LOGICAL,
6972d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
6982d335818SDavid Gibson     .class_init    = spapr_drc_lmb_class_init,
699bbf5c878SMichael Roth };
700bbf5c878SMichael Roth 
701bbf5c878SMichael Roth /* helper functions for external users */
702bbf5c878SMichael Roth 
703fbf55397SDavid Gibson sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
704bbf5c878SMichael Roth {
705bbf5c878SMichael Roth     Object *obj;
706bbf5c878SMichael Roth     char name[256];
707bbf5c878SMichael Roth 
708bbf5c878SMichael Roth     snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
709bbf5c878SMichael Roth     obj = object_resolve_path(name, NULL);
710bbf5c878SMichael Roth 
711bbf5c878SMichael Roth     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
712bbf5c878SMichael Roth }
713bbf5c878SMichael Roth 
714fbf55397SDavid Gibson sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
715bbf5c878SMichael Roth {
716fbf55397SDavid Gibson     sPAPRDRConnectorClass *drck
717fbf55397SDavid Gibson         = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
718fbf55397SDavid Gibson 
719fbf55397SDavid Gibson     return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
720fbf55397SDavid Gibson                               | (id & DRC_INDEX_ID_MASK));
721bbf5c878SMichael Roth }
722e4b798bbSMichael Roth 
723e4b798bbSMichael Roth /**
724e4b798bbSMichael Roth  * spapr_drc_populate_dt
725e4b798bbSMichael Roth  *
726e4b798bbSMichael Roth  * @fdt: libfdt device tree
727e4b798bbSMichael Roth  * @path: path in the DT to generate properties
728e4b798bbSMichael Roth  * @owner: parent Object/DeviceState for which to generate DRC
729e4b798bbSMichael Roth  *         descriptions for
730e4b798bbSMichael Roth  * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
731e4b798bbSMichael Roth  *   to the types of DRCs to generate entries for
732e4b798bbSMichael Roth  *
733e4b798bbSMichael Roth  * generate OF properties to describe DRC topology/indices to guests
734e4b798bbSMichael Roth  *
735e4b798bbSMichael Roth  * as documented in PAPR+ v2.1, 13.5.2
736e4b798bbSMichael Roth  */
737e4b798bbSMichael Roth int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
738e4b798bbSMichael Roth                           uint32_t drc_type_mask)
739e4b798bbSMichael Roth {
740e4b798bbSMichael Roth     Object *root_container;
741e4b798bbSMichael Roth     ObjectProperty *prop;
7427746abd8SDaniel P. Berrange     ObjectPropertyIterator iter;
743e4b798bbSMichael Roth     uint32_t drc_count = 0;
744e4b798bbSMichael Roth     GArray *drc_indexes, *drc_power_domains;
745e4b798bbSMichael Roth     GString *drc_names, *drc_types;
746e4b798bbSMichael Roth     int ret;
747e4b798bbSMichael Roth 
748e4b798bbSMichael Roth     /* the first entry of each properties is a 32-bit integer encoding
749e4b798bbSMichael Roth      * the number of elements in the array. we won't know this until
750e4b798bbSMichael Roth      * we complete the iteration through all the matching DRCs, but
751e4b798bbSMichael Roth      * reserve the space now and set the offsets accordingly so we
752e4b798bbSMichael Roth      * can fill them in later.
753e4b798bbSMichael Roth      */
754e4b798bbSMichael Roth     drc_indexes = g_array_new(false, true, sizeof(uint32_t));
755e4b798bbSMichael Roth     drc_indexes = g_array_set_size(drc_indexes, 1);
756e4b798bbSMichael Roth     drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
757e4b798bbSMichael Roth     drc_power_domains = g_array_set_size(drc_power_domains, 1);
758e4b798bbSMichael Roth     drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
759e4b798bbSMichael Roth     drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
760e4b798bbSMichael Roth 
761e4b798bbSMichael Roth     /* aliases for all DRConnector objects will be rooted in QOM
762e4b798bbSMichael Roth      * composition tree at DRC_CONTAINER_PATH
763e4b798bbSMichael Roth      */
764e4b798bbSMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
765e4b798bbSMichael Roth 
7667746abd8SDaniel P. Berrange     object_property_iter_init(&iter, root_container);
7677746abd8SDaniel P. Berrange     while ((prop = object_property_iter_next(&iter))) {
768e4b798bbSMichael Roth         Object *obj;
769e4b798bbSMichael Roth         sPAPRDRConnector *drc;
770e4b798bbSMichael Roth         sPAPRDRConnectorClass *drck;
771e4b798bbSMichael Roth         uint32_t drc_index, drc_power_domain;
772e4b798bbSMichael Roth 
773e4b798bbSMichael Roth         if (!strstart(prop->type, "link<", NULL)) {
774e4b798bbSMichael Roth             continue;
775e4b798bbSMichael Roth         }
776e4b798bbSMichael Roth 
777e4b798bbSMichael Roth         obj = object_property_get_link(root_container, prop->name, NULL);
778e4b798bbSMichael Roth         drc = SPAPR_DR_CONNECTOR(obj);
779e4b798bbSMichael Roth         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
780e4b798bbSMichael Roth 
781e4b798bbSMichael Roth         if (owner && (drc->owner != owner)) {
782e4b798bbSMichael Roth             continue;
783e4b798bbSMichael Roth         }
784e4b798bbSMichael Roth 
7852d335818SDavid Gibson         if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
786e4b798bbSMichael Roth             continue;
787e4b798bbSMichael Roth         }
788e4b798bbSMichael Roth 
789e4b798bbSMichael Roth         drc_count++;
790e4b798bbSMichael Roth 
791e4b798bbSMichael Roth         /* ibm,drc-indexes */
7920b55aa91SDavid Gibson         drc_index = cpu_to_be32(spapr_drc_index(drc));
793e4b798bbSMichael Roth         g_array_append_val(drc_indexes, drc_index);
794e4b798bbSMichael Roth 
795e4b798bbSMichael Roth         /* ibm,drc-power-domains */
796e4b798bbSMichael Roth         drc_power_domain = cpu_to_be32(-1);
797e4b798bbSMichael Roth         g_array_append_val(drc_power_domains, drc_power_domain);
798e4b798bbSMichael Roth 
799e4b798bbSMichael Roth         /* ibm,drc-names */
80079808336SDavid Gibson         drc_names = g_string_append(drc_names, spapr_drc_name(drc));
801e4b798bbSMichael Roth         drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
802e4b798bbSMichael Roth 
803e4b798bbSMichael Roth         /* ibm,drc-types */
8041693ea16SDavid Gibson         drc_types = g_string_append(drc_types, drck->typename);
805e4b798bbSMichael Roth         drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
806e4b798bbSMichael Roth     }
807e4b798bbSMichael Roth 
808e4b798bbSMichael Roth     /* now write the drc count into the space we reserved at the
809e4b798bbSMichael Roth      * beginning of the arrays previously
810e4b798bbSMichael Roth      */
811e4b798bbSMichael Roth     *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
812e4b798bbSMichael Roth     *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
813e4b798bbSMichael Roth     *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
814e4b798bbSMichael Roth     *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
815e4b798bbSMichael Roth 
816e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
817e4b798bbSMichael Roth                       drc_indexes->data,
818e4b798bbSMichael Roth                       drc_indexes->len * sizeof(uint32_t));
819e4b798bbSMichael Roth     if (ret) {
820ce9863b7SCédric Le Goater         error_report("Couldn't create ibm,drc-indexes property");
821e4b798bbSMichael Roth         goto out;
822e4b798bbSMichael Roth     }
823e4b798bbSMichael Roth 
824e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
825e4b798bbSMichael Roth                       drc_power_domains->data,
826e4b798bbSMichael Roth                       drc_power_domains->len * sizeof(uint32_t));
827e4b798bbSMichael Roth     if (ret) {
828ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-power-domains property");
829e4b798bbSMichael Roth         goto out;
830e4b798bbSMichael Roth     }
831e4b798bbSMichael Roth 
832e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
833e4b798bbSMichael Roth                       drc_names->str, drc_names->len);
834e4b798bbSMichael Roth     if (ret) {
835ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-names property");
836e4b798bbSMichael Roth         goto out;
837e4b798bbSMichael Roth     }
838e4b798bbSMichael Roth 
839e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
840e4b798bbSMichael Roth                       drc_types->str, drc_types->len);
841e4b798bbSMichael Roth     if (ret) {
842ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-types property");
843e4b798bbSMichael Roth         goto out;
844e4b798bbSMichael Roth     }
845e4b798bbSMichael Roth 
846e4b798bbSMichael Roth out:
847e4b798bbSMichael Roth     g_array_free(drc_indexes, true);
848e4b798bbSMichael Roth     g_array_free(drc_power_domains, true);
849e4b798bbSMichael Roth     g_string_free(drc_names, true);
850e4b798bbSMichael Roth     g_string_free(drc_types, true);
851e4b798bbSMichael Roth 
852e4b798bbSMichael Roth     return ret;
853e4b798bbSMichael Roth }
854b89b3d39SDavid Gibson 
855b89b3d39SDavid Gibson /*
856b89b3d39SDavid Gibson  * RTAS calls
857b89b3d39SDavid Gibson  */
858b89b3d39SDavid Gibson 
8597b7258f8SDavid Gibson static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
860b89b3d39SDavid Gibson {
8617b7258f8SDavid Gibson     sPAPRDRConnector *drc = spapr_drc_by_index(idx);
8627b7258f8SDavid Gibson     sPAPRDRConnectorClass *drck;
8637b7258f8SDavid Gibson 
8647b7258f8SDavid Gibson     if (!drc) {
8650dfabd39SDavid Gibson         return RTAS_OUT_NO_SUCH_INDICATOR;
866b89b3d39SDavid Gibson     }
867b89b3d39SDavid Gibson 
8680dfabd39SDavid Gibson     trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
8690dfabd39SDavid Gibson 
8707b7258f8SDavid Gibson     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
8710dfabd39SDavid Gibson 
8720dfabd39SDavid Gibson     switch (state) {
8730dfabd39SDavid Gibson     case SPAPR_DR_ISOLATION_STATE_ISOLATED:
8740dfabd39SDavid Gibson         return drck->isolate(drc);
8750dfabd39SDavid Gibson 
8760dfabd39SDavid Gibson     case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
8770dfabd39SDavid Gibson         return drck->unisolate(drc);
8780dfabd39SDavid Gibson 
8790dfabd39SDavid Gibson     default:
8800dfabd39SDavid Gibson         return RTAS_OUT_PARAM_ERROR;
8810dfabd39SDavid Gibson     }
8827b7258f8SDavid Gibson }
8837b7258f8SDavid Gibson 
8847b7258f8SDavid Gibson static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
8857b7258f8SDavid Gibson {
8867b7258f8SDavid Gibson     sPAPRDRConnector *drc = spapr_drc_by_index(idx);
8877b7258f8SDavid Gibson 
88861736732SDavid Gibson     if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
88961736732SDavid Gibson         return RTAS_OUT_NO_SUCH_INDICATOR;
8907b7258f8SDavid Gibson     }
8917b7258f8SDavid Gibson 
89261736732SDavid Gibson     trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
89361736732SDavid Gibson 
89461736732SDavid Gibson     switch (state) {
89561736732SDavid Gibson     case SPAPR_DR_ALLOCATION_STATE_USABLE:
89661736732SDavid Gibson         return drc_set_usable(drc);
89761736732SDavid Gibson 
89861736732SDavid Gibson     case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
89961736732SDavid Gibson         return drc_set_unusable(drc);
90061736732SDavid Gibson 
90161736732SDavid Gibson     default:
90261736732SDavid Gibson         return RTAS_OUT_PARAM_ERROR;
90361736732SDavid Gibson     }
9047b7258f8SDavid Gibson }
9057b7258f8SDavid Gibson 
906cd74d27eSDavid Gibson static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
9077b7258f8SDavid Gibson {
9087b7258f8SDavid Gibson     sPAPRDRConnector *drc = spapr_drc_by_index(idx);
9097b7258f8SDavid Gibson 
9107b7258f8SDavid Gibson     if (!drc) {
9117b7258f8SDavid Gibson         return RTAS_OUT_PARAM_ERROR;
9127b7258f8SDavid Gibson     }
9137b7258f8SDavid Gibson 
914cd74d27eSDavid Gibson     trace_spapr_drc_set_dr_indicator(idx, state);
915cd74d27eSDavid Gibson     drc->dr_indicator = state;
916cd74d27eSDavid Gibson     return RTAS_OUT_SUCCESS;
917b89b3d39SDavid Gibson }
918b89b3d39SDavid Gibson 
919b89b3d39SDavid Gibson static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
9207b7258f8SDavid Gibson                                uint32_t token,
9217b7258f8SDavid Gibson                                uint32_t nargs, target_ulong args,
9227b7258f8SDavid Gibson                                uint32_t nret, target_ulong rets)
923b89b3d39SDavid Gibson {
9247b7258f8SDavid Gibson     uint32_t type, idx, state;
925b89b3d39SDavid Gibson     uint32_t ret = RTAS_OUT_SUCCESS;
926b89b3d39SDavid Gibson 
927b89b3d39SDavid Gibson     if (nargs != 3 || nret != 1) {
928b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
929b89b3d39SDavid Gibson         goto out;
930b89b3d39SDavid Gibson     }
931b89b3d39SDavid Gibson 
9327b7258f8SDavid Gibson     type = rtas_ld(args, 0);
9337b7258f8SDavid Gibson     idx = rtas_ld(args, 1);
9347b7258f8SDavid Gibson     state = rtas_ld(args, 2);
935b89b3d39SDavid Gibson 
9367b7258f8SDavid Gibson     switch (type) {
937b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
9387b7258f8SDavid Gibson         ret = rtas_set_isolation_state(idx, state);
939b89b3d39SDavid Gibson         break;
940b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_DR:
941cd74d27eSDavid Gibson         ret = rtas_set_dr_indicator(idx, state);
942b89b3d39SDavid Gibson         break;
943b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
9447b7258f8SDavid Gibson         ret = rtas_set_allocation_state(idx, state);
945b89b3d39SDavid Gibson         break;
946b89b3d39SDavid Gibson     default:
9477b7258f8SDavid Gibson         ret = RTAS_OUT_NOT_SUPPORTED;
948b89b3d39SDavid Gibson     }
949b89b3d39SDavid Gibson 
950b89b3d39SDavid Gibson out:
951b89b3d39SDavid Gibson     rtas_st(rets, 0, ret);
952b89b3d39SDavid Gibson }
953b89b3d39SDavid Gibson 
954b89b3d39SDavid Gibson static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
955b89b3d39SDavid Gibson                                   uint32_t token, uint32_t nargs,
956b89b3d39SDavid Gibson                                   target_ulong args, uint32_t nret,
957b89b3d39SDavid Gibson                                   target_ulong rets)
958b89b3d39SDavid Gibson {
959b89b3d39SDavid Gibson     uint32_t sensor_type;
960b89b3d39SDavid Gibson     uint32_t sensor_index;
961b89b3d39SDavid Gibson     uint32_t sensor_state = 0;
962b89b3d39SDavid Gibson     sPAPRDRConnector *drc;
963b89b3d39SDavid Gibson     sPAPRDRConnectorClass *drck;
964b89b3d39SDavid Gibson     uint32_t ret = RTAS_OUT_SUCCESS;
965b89b3d39SDavid Gibson 
966b89b3d39SDavid Gibson     if (nargs != 2 || nret != 2) {
967b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
968b89b3d39SDavid Gibson         goto out;
969b89b3d39SDavid Gibson     }
970b89b3d39SDavid Gibson 
971b89b3d39SDavid Gibson     sensor_type = rtas_ld(args, 0);
972b89b3d39SDavid Gibson     sensor_index = rtas_ld(args, 1);
973b89b3d39SDavid Gibson 
974b89b3d39SDavid Gibson     if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
975b89b3d39SDavid Gibson         /* currently only DR-related sensors are implemented */
976b89b3d39SDavid Gibson         trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
977b89b3d39SDavid Gibson                                                         sensor_type);
978b89b3d39SDavid Gibson         ret = RTAS_OUT_NOT_SUPPORTED;
979b89b3d39SDavid Gibson         goto out;
980b89b3d39SDavid Gibson     }
981b89b3d39SDavid Gibson 
982fbf55397SDavid Gibson     drc = spapr_drc_by_index(sensor_index);
983b89b3d39SDavid Gibson     if (!drc) {
984b89b3d39SDavid Gibson         trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
985b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
986b89b3d39SDavid Gibson         goto out;
987b89b3d39SDavid Gibson     }
988b89b3d39SDavid Gibson     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
989f224d35bSDavid Gibson     sensor_state = drck->dr_entity_sense(drc);
990b89b3d39SDavid Gibson 
991b89b3d39SDavid Gibson out:
992b89b3d39SDavid Gibson     rtas_st(rets, 0, ret);
993b89b3d39SDavid Gibson     rtas_st(rets, 1, sensor_state);
994b89b3d39SDavid Gibson }
995b89b3d39SDavid Gibson 
996b89b3d39SDavid Gibson /* configure-connector work area offsets, int32_t units for field
997b89b3d39SDavid Gibson  * indexes, bytes for field offset/len values.
998b89b3d39SDavid Gibson  *
999b89b3d39SDavid Gibson  * as documented by PAPR+ v2.7, 13.5.3.5
1000b89b3d39SDavid Gibson  */
1001b89b3d39SDavid Gibson #define CC_IDX_NODE_NAME_OFFSET 2
1002b89b3d39SDavid Gibson #define CC_IDX_PROP_NAME_OFFSET 2
1003b89b3d39SDavid Gibson #define CC_IDX_PROP_LEN 3
1004b89b3d39SDavid Gibson #define CC_IDX_PROP_DATA_OFFSET 4
1005b89b3d39SDavid Gibson #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1006b89b3d39SDavid Gibson #define CC_WA_LEN 4096
1007b89b3d39SDavid Gibson 
1008b89b3d39SDavid Gibson static void configure_connector_st(target_ulong addr, target_ulong offset,
1009b89b3d39SDavid Gibson                                    const void *buf, size_t len)
1010b89b3d39SDavid Gibson {
1011b89b3d39SDavid Gibson     cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1012b89b3d39SDavid Gibson                               buf, MIN(len, CC_WA_LEN - offset));
1013b89b3d39SDavid Gibson }
1014b89b3d39SDavid Gibson 
1015b89b3d39SDavid Gibson static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1016b89b3d39SDavid Gibson                                          sPAPRMachineState *spapr,
1017b89b3d39SDavid Gibson                                          uint32_t token, uint32_t nargs,
1018b89b3d39SDavid Gibson                                          target_ulong args, uint32_t nret,
1019b89b3d39SDavid Gibson                                          target_ulong rets)
1020b89b3d39SDavid Gibson {
1021b89b3d39SDavid Gibson     uint64_t wa_addr;
1022b89b3d39SDavid Gibson     uint64_t wa_offset;
1023b89b3d39SDavid Gibson     uint32_t drc_index;
1024b89b3d39SDavid Gibson     sPAPRDRConnector *drc;
1025b89b3d39SDavid Gibson     sPAPRConfigureConnectorState *ccs;
1026b89b3d39SDavid Gibson     sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1027b89b3d39SDavid Gibson     int rc;
1028b89b3d39SDavid Gibson 
1029b89b3d39SDavid Gibson     if (nargs != 2 || nret != 1) {
1030b89b3d39SDavid Gibson         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1031b89b3d39SDavid Gibson         return;
1032b89b3d39SDavid Gibson     }
1033b89b3d39SDavid Gibson 
1034b89b3d39SDavid Gibson     wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1035b89b3d39SDavid Gibson 
1036b89b3d39SDavid Gibson     drc_index = rtas_ld(wa_addr, 0);
1037fbf55397SDavid Gibson     drc = spapr_drc_by_index(drc_index);
1038b89b3d39SDavid Gibson     if (!drc) {
1039b89b3d39SDavid Gibson         trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1040b89b3d39SDavid Gibson         rc = RTAS_OUT_PARAM_ERROR;
1041b89b3d39SDavid Gibson         goto out;
1042b89b3d39SDavid Gibson     }
1043b89b3d39SDavid Gibson 
104488af6ea5SDavid Gibson     if (!drc->fdt) {
1045b89b3d39SDavid Gibson         trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1046b89b3d39SDavid Gibson         rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1047b89b3d39SDavid Gibson         goto out;
1048b89b3d39SDavid Gibson     }
1049b89b3d39SDavid Gibson 
1050b8fdd530SDavid Gibson     ccs = drc->ccs;
1051b89b3d39SDavid Gibson     if (!ccs) {
1052b89b3d39SDavid Gibson         ccs = g_new0(sPAPRConfigureConnectorState, 1);
105388af6ea5SDavid Gibson         ccs->fdt_offset = drc->fdt_start_offset;
1054b8fdd530SDavid Gibson         drc->ccs = ccs;
1055b89b3d39SDavid Gibson     }
1056b89b3d39SDavid Gibson 
1057b89b3d39SDavid Gibson     do {
1058b89b3d39SDavid Gibson         uint32_t tag;
1059b89b3d39SDavid Gibson         const char *name;
1060b89b3d39SDavid Gibson         const struct fdt_property *prop;
1061b89b3d39SDavid Gibson         int fdt_offset_next, prop_len;
1062b89b3d39SDavid Gibson 
106388af6ea5SDavid Gibson         tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1064b89b3d39SDavid Gibson 
1065b89b3d39SDavid Gibson         switch (tag) {
1066b89b3d39SDavid Gibson         case FDT_BEGIN_NODE:
1067b89b3d39SDavid Gibson             ccs->fdt_depth++;
106888af6ea5SDavid Gibson             name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1069b89b3d39SDavid Gibson 
1070b89b3d39SDavid Gibson             /* provide the name of the next OF node */
1071b89b3d39SDavid Gibson             wa_offset = CC_VAL_DATA_OFFSET;
1072b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1073b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1074b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1075b89b3d39SDavid Gibson             break;
1076b89b3d39SDavid Gibson         case FDT_END_NODE:
1077b89b3d39SDavid Gibson             ccs->fdt_depth--;
1078b89b3d39SDavid Gibson             if (ccs->fdt_depth == 0) {
10794f65ce00SDavid Gibson                 sPAPRDRIsolationState state = drc->isolation_state;
10800b55aa91SDavid Gibson                 uint32_t drc_index = spapr_drc_index(drc);
1081b89b3d39SDavid Gibson                 /* done sending the device tree, don't need to track
1082b89b3d39SDavid Gibson                  * the state anymore
1083b89b3d39SDavid Gibson                  */
10840b55aa91SDavid Gibson                 trace_spapr_drc_set_configured(drc_index);
10854f65ce00SDavid Gibson                 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
10864f65ce00SDavid Gibson                     drc->configured = true;
10874f65ce00SDavid Gibson                 } else {
10884f65ce00SDavid Gibson                     /* guest should be not configuring an isolated device */
10890b55aa91SDavid Gibson                     trace_spapr_drc_set_configured_skipping(drc_index);
10904f65ce00SDavid Gibson                 }
1091b8fdd530SDavid Gibson                 g_free(ccs);
1092b8fdd530SDavid Gibson                 drc->ccs = NULL;
1093b89b3d39SDavid Gibson                 ccs = NULL;
1094b89b3d39SDavid Gibson                 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1095b89b3d39SDavid Gibson             } else {
1096b89b3d39SDavid Gibson                 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1097b89b3d39SDavid Gibson             }
1098b89b3d39SDavid Gibson             break;
1099b89b3d39SDavid Gibson         case FDT_PROP:
110088af6ea5SDavid Gibson             prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1101b89b3d39SDavid Gibson                                               &prop_len);
110288af6ea5SDavid Gibson             name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1103b89b3d39SDavid Gibson 
1104b89b3d39SDavid Gibson             /* provide the name of the next OF property */
1105b89b3d39SDavid Gibson             wa_offset = CC_VAL_DATA_OFFSET;
1106b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1107b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1108b89b3d39SDavid Gibson 
1109b89b3d39SDavid Gibson             /* provide the length and value of the OF property. data gets
1110b89b3d39SDavid Gibson              * placed immediately after NULL terminator of the OF property's
1111b89b3d39SDavid Gibson              * name string
1112b89b3d39SDavid Gibson              */
1113b89b3d39SDavid Gibson             wa_offset += strlen(name) + 1,
1114b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1115b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1116b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1117b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1118b89b3d39SDavid Gibson             break;
1119b89b3d39SDavid Gibson         case FDT_END:
1120b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_ERROR;
1121b89b3d39SDavid Gibson         default:
1122b89b3d39SDavid Gibson             /* keep seeking for an actionable tag */
1123b89b3d39SDavid Gibson             break;
1124b89b3d39SDavid Gibson         }
1125b89b3d39SDavid Gibson         if (ccs) {
1126b89b3d39SDavid Gibson             ccs->fdt_offset = fdt_offset_next;
1127b89b3d39SDavid Gibson         }
1128b89b3d39SDavid Gibson     } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1129b89b3d39SDavid Gibson 
1130b89b3d39SDavid Gibson     rc = resp;
1131b89b3d39SDavid Gibson out:
1132b89b3d39SDavid Gibson     rtas_st(rets, 0, rc);
1133b89b3d39SDavid Gibson }
1134b89b3d39SDavid Gibson 
1135b89b3d39SDavid Gibson static void spapr_drc_register_types(void)
1136b89b3d39SDavid Gibson {
1137b89b3d39SDavid Gibson     type_register_static(&spapr_dr_connector_info);
11382d335818SDavid Gibson     type_register_static(&spapr_drc_physical_info);
11392d335818SDavid Gibson     type_register_static(&spapr_drc_logical_info);
11402d335818SDavid Gibson     type_register_static(&spapr_drc_cpu_info);
11412d335818SDavid Gibson     type_register_static(&spapr_drc_pci_info);
11422d335818SDavid Gibson     type_register_static(&spapr_drc_lmb_info);
1143b89b3d39SDavid Gibson 
1144b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1145b89b3d39SDavid Gibson                         rtas_set_indicator);
1146b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1147b89b3d39SDavid Gibson                         rtas_get_sensor_state);
1148b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1149b89b3d39SDavid Gibson                         rtas_ibm_configure_connector);
1150b89b3d39SDavid Gibson }
1151b89b3d39SDavid Gibson type_init(spapr_drc_register_types)
1152