xref: /qemu/hw/ppc/spapr_drc.c (revision f224d35be9fb971bf64b569b99ce2a582156bbf2)
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 
490cb688d2SMichael Roth static uint32_t set_isolation_state(sPAPRDRConnector *drc,
50bbf5c878SMichael Roth                                     sPAPRDRIsolationState state)
51bbf5c878SMichael Roth {
52bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
53bbf5c878SMichael Roth 
540b55aa91SDavid Gibson     trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
55bbf5c878SMichael Roth 
56b8fdd530SDavid Gibson     /* if the guest is configuring a device attached to this DRC, we
57b8fdd530SDavid Gibson      * should reset the configuration state at this point since it may
58b8fdd530SDavid Gibson      * no longer be reliable (guest released device and needs to start
59b8fdd530SDavid Gibson      * over, or unplug occurred so the FDT is no longer valid)
60b8fdd530SDavid Gibson      */
61b8fdd530SDavid Gibson     if (state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
62b8fdd530SDavid Gibson         g_free(drc->ccs);
63b8fdd530SDavid Gibson         drc->ccs = NULL;
64b8fdd530SDavid Gibson     }
65b8fdd530SDavid Gibson 
669d1852ceSMichael Roth     if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
67b12227afSStefan Weil         /* cannot unisolate a non-existent resource, and, or resources
689d1852ceSMichael Roth          * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
699d1852ceSMichael Roth          */
709d1852ceSMichael Roth         if (!drc->dev ||
719d1852ceSMichael Roth             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
729d1852ceSMichael Roth             return RTAS_OUT_NO_SUCH_INDICATOR;
739d1852ceSMichael Roth         }
749d1852ceSMichael Roth     }
759d1852ceSMichael Roth 
76cf632463SBharata B Rao     /*
77cf632463SBharata B Rao      * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
78cf632463SBharata B Rao      * belong to a DIMM device that is marked for removal.
79cf632463SBharata B Rao      *
80cf632463SBharata B Rao      * Currently the guest userspace tool drmgr that drives the memory
81cf632463SBharata B Rao      * hotplug/unplug will just try to remove a set of 'removable' LMBs
82cf632463SBharata B Rao      * in response to a hot unplug request that is based on drc-count.
83cf632463SBharata B Rao      * If the LMB being removed doesn't belong to a DIMM device that is
84cf632463SBharata B Rao      * actually being unplugged, fail the isolation request here.
85cf632463SBharata B Rao      */
862d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
87cf632463SBharata B Rao         if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
88cf632463SBharata B Rao              !drc->awaiting_release) {
89cf632463SBharata B Rao             return RTAS_OUT_HW_ERROR;
90cf632463SBharata B Rao         }
91cf632463SBharata B Rao     }
92cf632463SBharata B Rao 
93bbf5c878SMichael Roth     drc->isolation_state = state;
94bbf5c878SMichael Roth 
95bbf5c878SMichael Roth     if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
96bbf5c878SMichael Roth         /* if we're awaiting release, but still in an unconfigured state,
97bbf5c878SMichael Roth          * it's likely the guest is still in the process of configuring
98bbf5c878SMichael Roth          * the device and is transitioning the devices to an ISOLATED
99bbf5c878SMichael Roth          * state as a part of that process. so we only complete the
100bbf5c878SMichael Roth          * removal when this transition happens for a device in a
101bbf5c878SMichael Roth          * configured state, as suggested by the state diagram from
102bbf5c878SMichael Roth          * PAPR+ 2.7, 13.4
103bbf5c878SMichael Roth          */
104bbf5c878SMichael Roth         if (drc->awaiting_release) {
1050b55aa91SDavid Gibson             uint32_t drc_index = spapr_drc_index(drc);
106bbf5c878SMichael Roth             if (drc->configured) {
1070b55aa91SDavid Gibson                 trace_spapr_drc_set_isolation_state_finalizing(drc_index);
10831834723SDaniel Henrique Barboza                 drck->detach(drc, DEVICE(drc->dev), NULL);
109bbf5c878SMichael Roth             } else {
1100b55aa91SDavid Gibson                 trace_spapr_drc_set_isolation_state_deferring(drc_index);
111bbf5c878SMichael Roth             }
112bbf5c878SMichael Roth         }
113bbf5c878SMichael Roth         drc->configured = false;
114bbf5c878SMichael Roth     }
115bbf5c878SMichael Roth 
1160cb688d2SMichael Roth     return RTAS_OUT_SUCCESS;
117bbf5c878SMichael Roth }
118bbf5c878SMichael Roth 
1190cb688d2SMichael Roth static uint32_t set_indicator_state(sPAPRDRConnector *drc,
120bbf5c878SMichael Roth                                     sPAPRDRIndicatorState state)
121bbf5c878SMichael Roth {
1220b55aa91SDavid Gibson     trace_spapr_drc_set_indicator_state(spapr_drc_index(drc), state);
123bbf5c878SMichael Roth     drc->indicator_state = state;
1240cb688d2SMichael Roth     return RTAS_OUT_SUCCESS;
125bbf5c878SMichael Roth }
126bbf5c878SMichael Roth 
1270cb688d2SMichael Roth static uint32_t set_allocation_state(sPAPRDRConnector *drc,
128bbf5c878SMichael Roth                                      sPAPRDRAllocationState state)
129bbf5c878SMichael Roth {
130bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
131bbf5c878SMichael Roth 
1320b55aa91SDavid Gibson     trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
133bbf5c878SMichael Roth 
1349d1852ceSMichael Roth     if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
1359d1852ceSMichael Roth         /* if there's no resource/device associated with the DRC, there's
1369d1852ceSMichael Roth          * no way for us to put it in an allocation state consistent with
1379d1852ceSMichael Roth          * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
1389d1852ceSMichael Roth          * result in an RTAS return code of -3 / "no such indicator"
1399d1852ceSMichael Roth          */
1409d1852ceSMichael Roth         if (!drc->dev) {
1419d1852ceSMichael Roth             return RTAS_OUT_NO_SUCH_INDICATOR;
1429d1852ceSMichael Roth         }
143fe6824d1SLaurent Vivier         if (drc->awaiting_release && drc->awaiting_allocation) {
144fe6824d1SLaurent Vivier             /* kernel is acknowledging a previous hotplug event
145fe6824d1SLaurent Vivier              * while we are already removing it.
146fe6824d1SLaurent Vivier              * it's safe to ignore awaiting_allocation here since we know the
147fe6824d1SLaurent Vivier              * situation is predicated on the guest either already having done
148fe6824d1SLaurent Vivier              * so (boot-time hotplug), or never being able to acquire in the
149fe6824d1SLaurent Vivier              * first place (hotplug followed by immediate unplug).
150fe6824d1SLaurent Vivier              */
151fe6824d1SLaurent Vivier             drc->awaiting_allocation_skippable = true;
152fe6824d1SLaurent Vivier             return RTAS_OUT_NO_SUCH_INDICATOR;
153fe6824d1SLaurent Vivier         }
1549d1852ceSMichael Roth     }
1559d1852ceSMichael Roth 
1562d335818SDavid Gibson     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
157bbf5c878SMichael Roth         drc->allocation_state = state;
158bbf5c878SMichael Roth         if (drc->awaiting_release &&
159bbf5c878SMichael Roth             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
1600b55aa91SDavid Gibson             uint32_t drc_index = spapr_drc_index(drc);
1610b55aa91SDavid Gibson             trace_spapr_drc_set_allocation_state_finalizing(drc_index);
16231834723SDaniel Henrique Barboza             drck->detach(drc, DEVICE(drc->dev), NULL);
163aab99135SBharata B Rao         } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
164aab99135SBharata B Rao             drc->awaiting_allocation = false;
165bbf5c878SMichael Roth         }
166bbf5c878SMichael Roth     }
1670cb688d2SMichael Roth     return RTAS_OUT_SUCCESS;
168bbf5c878SMichael Roth }
169bbf5c878SMichael Roth 
170bbf5c878SMichael Roth static const char *get_name(sPAPRDRConnector *drc)
171bbf5c878SMichael Roth {
172bbf5c878SMichael Roth     return drc->name;
173bbf5c878SMichael Roth }
174bbf5c878SMichael Roth 
175f40eb921SMichael Roth /* has the guest been notified of device attachment? */
176f40eb921SMichael Roth static void set_signalled(sPAPRDRConnector *drc)
177f40eb921SMichael Roth {
178f40eb921SMichael Roth     drc->signalled = true;
179f40eb921SMichael Roth }
180f40eb921SMichael Roth 
181bbf5c878SMichael Roth /*
182bbf5c878SMichael Roth  * dr-entity-sense sensor value
183bbf5c878SMichael Roth  * returned via get-sensor-state RTAS calls
184bbf5c878SMichael Roth  * as expected by state diagram in PAPR+ 2.7, 13.4
185bbf5c878SMichael Roth  * based on the current allocation/indicator/power states
186bbf5c878SMichael Roth  * for the DR connector.
187bbf5c878SMichael Roth  */
188*f224d35bSDavid Gibson static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc)
189bbf5c878SMichael Roth {
190*f224d35bSDavid Gibson     /* this assumes all PCI devices are assigned to a 'live insertion'
191*f224d35bSDavid Gibson      * power domain, where QEMU manages power state automatically as
192*f224d35bSDavid Gibson      * opposed to the guest. present, non-PCI resources are unaffected
193*f224d35bSDavid Gibson      * by power state.
194bbf5c878SMichael Roth      */
195*f224d35bSDavid Gibson     return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
196*f224d35bSDavid Gibson         : SPAPR_DR_ENTITY_SENSE_EMPTY;
197bbf5c878SMichael Roth }
198bbf5c878SMichael Roth 
199*f224d35bSDavid Gibson static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc)
200*f224d35bSDavid Gibson {
201*f224d35bSDavid Gibson     if (drc->dev
202*f224d35bSDavid Gibson         && (drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE)) {
203*f224d35bSDavid Gibson         return SPAPR_DR_ENTITY_SENSE_PRESENT;
204*f224d35bSDavid Gibson     } else {
205*f224d35bSDavid Gibson         return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
206*f224d35bSDavid Gibson     }
207bbf5c878SMichael Roth }
208bbf5c878SMichael Roth 
209d7bce999SEric Blake static void prop_get_index(Object *obj, Visitor *v, const char *name,
210d7bce999SEric Blake                            void *opaque, Error **errp)
211bbf5c878SMichael Roth {
212bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
2130b55aa91SDavid Gibson     uint32_t value = spapr_drc_index(drc);
21451e72bc1SEric Blake     visit_type_uint32(v, name, &value, errp);
215bbf5c878SMichael Roth }
216bbf5c878SMichael Roth 
217bbf5c878SMichael Roth static char *prop_get_name(Object *obj, Error **errp)
218bbf5c878SMichael Roth {
219bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
220bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
221bbf5c878SMichael Roth     return g_strdup(drck->get_name(drc));
222bbf5c878SMichael Roth }
223bbf5c878SMichael Roth 
224d7bce999SEric Blake static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
225d7bce999SEric Blake                          void *opaque, Error **errp)
226bbf5c878SMichael Roth {
227bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
228c75304a1SMarkus Armbruster     Error *err = NULL;
229bbf5c878SMichael Roth     int fdt_offset_next, fdt_offset, fdt_depth;
230bbf5c878SMichael Roth     void *fdt;
231bbf5c878SMichael Roth 
232bbf5c878SMichael Roth     if (!drc->fdt) {
233a543a554SEric Blake         visit_type_null(v, NULL, errp);
234bbf5c878SMichael Roth         return;
235bbf5c878SMichael Roth     }
236bbf5c878SMichael Roth 
237bbf5c878SMichael Roth     fdt = drc->fdt;
238bbf5c878SMichael Roth     fdt_offset = drc->fdt_start_offset;
239bbf5c878SMichael Roth     fdt_depth = 0;
240bbf5c878SMichael Roth 
241bbf5c878SMichael Roth     do {
242bbf5c878SMichael Roth         const char *name = NULL;
243bbf5c878SMichael Roth         const struct fdt_property *prop = NULL;
244bbf5c878SMichael Roth         int prop_len = 0, name_len = 0;
245bbf5c878SMichael Roth         uint32_t tag;
246bbf5c878SMichael Roth 
247bbf5c878SMichael Roth         tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
248bbf5c878SMichael Roth         switch (tag) {
249bbf5c878SMichael Roth         case FDT_BEGIN_NODE:
250bbf5c878SMichael Roth             fdt_depth++;
251bbf5c878SMichael Roth             name = fdt_get_name(fdt, fdt_offset, &name_len);
252337283dfSEric Blake             visit_start_struct(v, name, NULL, 0, &err);
253c75304a1SMarkus Armbruster             if (err) {
254c75304a1SMarkus Armbruster                 error_propagate(errp, err);
255c75304a1SMarkus Armbruster                 return;
256c75304a1SMarkus Armbruster             }
257bbf5c878SMichael Roth             break;
258bbf5c878SMichael Roth         case FDT_END_NODE:
259bbf5c878SMichael Roth             /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
260bbf5c878SMichael Roth             g_assert(fdt_depth > 0);
26115c2f669SEric Blake             visit_check_struct(v, &err);
2621158bb2aSEric Blake             visit_end_struct(v, NULL);
263c75304a1SMarkus Armbruster             if (err) {
264c75304a1SMarkus Armbruster                 error_propagate(errp, err);
265c75304a1SMarkus Armbruster                 return;
266c75304a1SMarkus Armbruster             }
267bbf5c878SMichael Roth             fdt_depth--;
268bbf5c878SMichael Roth             break;
269bbf5c878SMichael Roth         case FDT_PROP: {
270bbf5c878SMichael Roth             int i;
271bbf5c878SMichael Roth             prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
272bbf5c878SMichael Roth             name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
273d9f62ddeSEric Blake             visit_start_list(v, name, NULL, 0, &err);
274c75304a1SMarkus Armbruster             if (err) {
275c75304a1SMarkus Armbruster                 error_propagate(errp, err);
276c75304a1SMarkus Armbruster                 return;
277bbf5c878SMichael Roth             }
278c75304a1SMarkus Armbruster             for (i = 0; i < prop_len; i++) {
27951e72bc1SEric Blake                 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
280c75304a1SMarkus Armbruster                 if (err) {
281c75304a1SMarkus Armbruster                     error_propagate(errp, err);
282c75304a1SMarkus Armbruster                     return;
283c75304a1SMarkus Armbruster                 }
284c75304a1SMarkus Armbruster             }
285a4a1c70dSMarkus Armbruster             visit_check_list(v, &err);
2861158bb2aSEric Blake             visit_end_list(v, NULL);
287a4a1c70dSMarkus Armbruster             if (err) {
288a4a1c70dSMarkus Armbruster                 error_propagate(errp, err);
289a4a1c70dSMarkus Armbruster                 return;
290a4a1c70dSMarkus Armbruster             }
291bbf5c878SMichael Roth             break;
292bbf5c878SMichael Roth         }
293bbf5c878SMichael Roth         default:
294bbf5c878SMichael Roth             error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
295bbf5c878SMichael Roth         }
296bbf5c878SMichael Roth         fdt_offset = fdt_offset_next;
297bbf5c878SMichael Roth     } while (fdt_depth != 0);
298bbf5c878SMichael Roth }
299bbf5c878SMichael Roth 
300bbf5c878SMichael Roth static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
301bbf5c878SMichael Roth                    int fdt_start_offset, bool coldplug, Error **errp)
302bbf5c878SMichael Roth {
3030b55aa91SDavid Gibson     trace_spapr_drc_attach(spapr_drc_index(drc));
304bbf5c878SMichael Roth 
305bbf5c878SMichael Roth     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
306bbf5c878SMichael Roth         error_setg(errp, "an attached device is still awaiting release");
307bbf5c878SMichael Roth         return;
308bbf5c878SMichael Roth     }
3092d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
310bbf5c878SMichael Roth         g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
311bbf5c878SMichael Roth     }
312bbf5c878SMichael Roth     g_assert(fdt || coldplug);
313bbf5c878SMichael Roth 
314bbf5c878SMichael Roth     /* NOTE: setting initial isolation state to UNISOLATED means we can't
315bbf5c878SMichael Roth      * detach unless guest has a userspace/kernel that moves this state
316bbf5c878SMichael Roth      * back to ISOLATED in response to an unplug event, or this is done
317bbf5c878SMichael Roth      * manually by the admin prior. if we force things while the guest
318bbf5c878SMichael Roth      * may be accessing the device, we can easily crash the guest, so we
319bbf5c878SMichael Roth      * we defer completion of removal in such cases to the reset() hook.
320bbf5c878SMichael Roth      */
3212d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
322bbf5c878SMichael Roth         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
323bbf5c878SMichael Roth     }
324bbf5c878SMichael Roth     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
325bbf5c878SMichael Roth 
326bbf5c878SMichael Roth     drc->dev = d;
327bbf5c878SMichael Roth     drc->fdt = fdt;
328bbf5c878SMichael Roth     drc->fdt_start_offset = fdt_start_offset;
329785652dcSLaurent Vivier     drc->configured = coldplug;
330df18b2dbSMichael Roth     /* 'logical' DR resources such as memory/cpus are in some cases treated
331df18b2dbSMichael Roth      * as a pool of resources from which the guest is free to choose from
332df18b2dbSMichael Roth      * based on only a count. for resources that can be assigned in this
333df18b2dbSMichael Roth      * fashion, we must assume the resource is signalled immediately
334df18b2dbSMichael Roth      * since a single hotplug request might make an arbitrary number of
335df18b2dbSMichael Roth      * such attached resources available to the guest, as opposed to
336df18b2dbSMichael Roth      * 'physical' DR resources such as PCI where each device/resource is
337df18b2dbSMichael Roth      * signalled individually.
338df18b2dbSMichael Roth      */
3392d335818SDavid Gibson     drc->signalled = (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI)
340df18b2dbSMichael Roth                      ? true : coldplug;
341bbf5c878SMichael Roth 
3422d335818SDavid Gibson     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
343aab99135SBharata B Rao         drc->awaiting_allocation = true;
344aab99135SBharata B Rao     }
345aab99135SBharata B Rao 
346bbf5c878SMichael Roth     object_property_add_link(OBJECT(drc), "device",
347bbf5c878SMichael Roth                              object_get_typename(OBJECT(drc->dev)),
348bbf5c878SMichael Roth                              (Object **)(&drc->dev),
349bbf5c878SMichael Roth                              NULL, 0, NULL);
350bbf5c878SMichael Roth }
351bbf5c878SMichael Roth 
35231834723SDaniel Henrique Barboza static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
353bbf5c878SMichael Roth {
3540b55aa91SDavid Gibson     trace_spapr_drc_detach(spapr_drc_index(drc));
355bbf5c878SMichael Roth 
356f40eb921SMichael Roth     /* if we've signalled device presence to the guest, or if the guest
357f40eb921SMichael Roth      * has gone ahead and configured the device (via manually-executed
358f40eb921SMichael Roth      * device add via drmgr in guest, namely), we need to wait
359f40eb921SMichael Roth      * for the guest to quiesce the device before completing detach.
360f40eb921SMichael Roth      * Otherwise, we can assume the guest hasn't seen it and complete the
361f40eb921SMichael Roth      * detach immediately. Note that there is a small race window
362f40eb921SMichael Roth      * just before, or during, configuration, which is this context
363f40eb921SMichael Roth      * refers mainly to fetching the device tree via RTAS.
364f40eb921SMichael Roth      * During this window the device access will be arbitrated by
365f40eb921SMichael Roth      * associated DRC, which will simply fail the RTAS calls as invalid.
366f40eb921SMichael Roth      * This is recoverable within guest and current implementations of
367f40eb921SMichael Roth      * drmgr should be able to cope.
368f40eb921SMichael Roth      */
369f40eb921SMichael Roth     if (!drc->signalled && !drc->configured) {
370f40eb921SMichael Roth         /* if the guest hasn't seen the device we can't rely on it to
371f40eb921SMichael Roth          * set it back to an isolated state via RTAS, so do it here manually
372f40eb921SMichael Roth          */
373f40eb921SMichael Roth         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
374f40eb921SMichael Roth     }
375f40eb921SMichael Roth 
376bbf5c878SMichael Roth     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
3770b55aa91SDavid Gibson         trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
378bbf5c878SMichael Roth         drc->awaiting_release = true;
379bbf5c878SMichael Roth         return;
380bbf5c878SMichael Roth     }
381bbf5c878SMichael Roth 
3822d335818SDavid Gibson     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
383bbf5c878SMichael Roth         drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
3840b55aa91SDavid Gibson         trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
385bbf5c878SMichael Roth         drc->awaiting_release = true;
386bbf5c878SMichael Roth         return;
387bbf5c878SMichael Roth     }
388bbf5c878SMichael Roth 
389aab99135SBharata B Rao     if (drc->awaiting_allocation) {
390fe6824d1SLaurent Vivier         if (!drc->awaiting_allocation_skippable) {
391aab99135SBharata B Rao             drc->awaiting_release = true;
3920b55aa91SDavid Gibson             trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
393aab99135SBharata B Rao             return;
394aab99135SBharata B Rao         }
395fe6824d1SLaurent Vivier     }
396aab99135SBharata B Rao 
397bbf5c878SMichael Roth     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
398bbf5c878SMichael Roth 
3992d335818SDavid Gibson     /* Calling release callbacks based on spapr_drc_type(drc). */
4002d335818SDavid Gibson     switch (spapr_drc_type(drc)) {
40131834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_CPU:
40231834723SDaniel Henrique Barboza         spapr_core_release(drc->dev);
40331834723SDaniel Henrique Barboza         break;
40431834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PCI:
40531834723SDaniel Henrique Barboza         spapr_phb_remove_pci_device_cb(drc->dev);
40631834723SDaniel Henrique Barboza         break;
40731834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_LMB:
40831834723SDaniel Henrique Barboza         spapr_lmb_release(drc->dev);
40931834723SDaniel Henrique Barboza         break;
41031834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PHB:
41131834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_VIO:
41231834723SDaniel Henrique Barboza     default:
41331834723SDaniel Henrique Barboza         g_assert(false);
414bbf5c878SMichael Roth     }
415bbf5c878SMichael Roth 
416bbf5c878SMichael Roth     drc->awaiting_release = false;
417fe6824d1SLaurent Vivier     drc->awaiting_allocation_skippable = false;
418bbf5c878SMichael Roth     g_free(drc->fdt);
419bbf5c878SMichael Roth     drc->fdt = NULL;
420bbf5c878SMichael Roth     drc->fdt_start_offset = 0;
421bbf5c878SMichael Roth     object_property_del(OBJECT(drc), "device", NULL);
422bbf5c878SMichael Roth     drc->dev = NULL;
423bbf5c878SMichael Roth }
424bbf5c878SMichael Roth 
425bbf5c878SMichael Roth static bool release_pending(sPAPRDRConnector *drc)
426bbf5c878SMichael Roth {
427bbf5c878SMichael Roth     return drc->awaiting_release;
428bbf5c878SMichael Roth }
429bbf5c878SMichael Roth 
430bbf5c878SMichael Roth static void reset(DeviceState *d)
431bbf5c878SMichael Roth {
432bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
433bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
434bbf5c878SMichael Roth 
4350b55aa91SDavid Gibson     trace_spapr_drc_reset(spapr_drc_index(drc));
436b8fdd530SDavid Gibson 
437b8fdd530SDavid Gibson     g_free(drc->ccs);
438b8fdd530SDavid Gibson     drc->ccs = NULL;
439b8fdd530SDavid Gibson 
440bbf5c878SMichael Roth     /* immediately upon reset we can safely assume DRCs whose devices
441bbf5c878SMichael Roth      * are pending removal can be safely removed, and that they will
442bbf5c878SMichael Roth      * subsequently be left in an ISOLATED state. move the DRC to this
443bbf5c878SMichael Roth      * state in these cases (which will in turn complete any pending
444bbf5c878SMichael Roth      * device removals)
445bbf5c878SMichael Roth      */
446bbf5c878SMichael Roth     if (drc->awaiting_release) {
447bbf5c878SMichael Roth         drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
448bbf5c878SMichael Roth         /* generally this should also finalize the removal, but if the device
449bbf5c878SMichael Roth          * hasn't yet been configured we normally defer removal under the
450bbf5c878SMichael Roth          * assumption that this transition is taking place as part of device
451bbf5c878SMichael Roth          * configuration. so check if we're still waiting after this, and
452bbf5c878SMichael Roth          * force removal if we are
453bbf5c878SMichael Roth          */
454bbf5c878SMichael Roth         if (drc->awaiting_release) {
45531834723SDaniel Henrique Barboza             drck->detach(drc, DEVICE(drc->dev), NULL);
456bbf5c878SMichael Roth         }
457bbf5c878SMichael Roth 
458bbf5c878SMichael Roth         /* non-PCI devices may be awaiting a transition to UNUSABLE */
4592d335818SDavid Gibson         if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
460bbf5c878SMichael Roth             drc->awaiting_release) {
461bbf5c878SMichael Roth             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
462bbf5c878SMichael Roth         }
463bbf5c878SMichael Roth     }
464f40eb921SMichael Roth 
465*f224d35bSDavid Gibson     if (drck->dr_entity_sense(drc) == SPAPR_DR_ENTITY_SENSE_PRESENT) {
466f40eb921SMichael Roth         drck->set_signalled(drc);
467f40eb921SMichael Roth     }
468bbf5c878SMichael Roth }
469bbf5c878SMichael Roth 
470a50919ddSDaniel Henrique Barboza static bool spapr_drc_needed(void *opaque)
471a50919ddSDaniel Henrique Barboza {
472a50919ddSDaniel Henrique Barboza     sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
473a50919ddSDaniel Henrique Barboza     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
474a50919ddSDaniel Henrique Barboza     bool rc = false;
475*f224d35bSDavid Gibson     sPAPRDREntitySense value = drck->dr_entity_sense(drc);
476a50919ddSDaniel Henrique Barboza 
477a50919ddSDaniel Henrique Barboza     /* If no dev is plugged in there is no need to migrate the DRC state */
478a50919ddSDaniel Henrique Barboza     if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
479a50919ddSDaniel Henrique Barboza         return false;
480a50919ddSDaniel Henrique Barboza     }
481a50919ddSDaniel Henrique Barboza 
482a50919ddSDaniel Henrique Barboza     /*
483a50919ddSDaniel Henrique Barboza      * If there is dev plugged in, we need to migrate the DRC state when
484a50919ddSDaniel Henrique Barboza      * it is different from cold-plugged state
485a50919ddSDaniel Henrique Barboza      */
4862d335818SDavid Gibson     switch (spapr_drc_type(drc)) {
487a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PCI:
488a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_CPU:
489a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_LMB:
490a32e900bSGreg Kurz         rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
491a32e900bSGreg Kurz                (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
492a50919ddSDaniel Henrique Barboza                drc->configured && drc->signalled && !drc->awaiting_release);
493a50919ddSDaniel Henrique Barboza         break;
494a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PHB:
495a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_VIO:
496a50919ddSDaniel Henrique Barboza     default:
497a32e900bSGreg Kurz         g_assert_not_reached();
498a50919ddSDaniel Henrique Barboza     }
499a50919ddSDaniel Henrique Barboza     return rc;
500a50919ddSDaniel Henrique Barboza }
501a50919ddSDaniel Henrique Barboza 
502a50919ddSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_drc = {
503a50919ddSDaniel Henrique Barboza     .name = "spapr_drc",
504a50919ddSDaniel Henrique Barboza     .version_id = 1,
505a50919ddSDaniel Henrique Barboza     .minimum_version_id = 1,
506a50919ddSDaniel Henrique Barboza     .needed = spapr_drc_needed,
507a50919ddSDaniel Henrique Barboza     .fields  = (VMStateField []) {
508a50919ddSDaniel Henrique Barboza         VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
509a50919ddSDaniel Henrique Barboza         VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
510a50919ddSDaniel Henrique Barboza         VMSTATE_UINT32(indicator_state, sPAPRDRConnector),
511a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(configured, sPAPRDRConnector),
512a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
513a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
514a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(signalled, sPAPRDRConnector),
515a50919ddSDaniel Henrique Barboza         VMSTATE_END_OF_LIST()
516a50919ddSDaniel Henrique Barboza     }
517a50919ddSDaniel Henrique Barboza };
518a50919ddSDaniel Henrique Barboza 
519bbf5c878SMichael Roth static void realize(DeviceState *d, Error **errp)
520bbf5c878SMichael Roth {
521bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
522bbf5c878SMichael Roth     Object *root_container;
523bbf5c878SMichael Roth     char link_name[256];
524bbf5c878SMichael Roth     gchar *child_name;
525bbf5c878SMichael Roth     Error *err = NULL;
526bbf5c878SMichael Roth 
5270b55aa91SDavid Gibson     trace_spapr_drc_realize(spapr_drc_index(drc));
528bbf5c878SMichael Roth     /* NOTE: we do this as part of realize/unrealize due to the fact
529bbf5c878SMichael Roth      * that the guest will communicate with the DRC via RTAS calls
530bbf5c878SMichael Roth      * referencing the global DRC index. By unlinking the DRC
531bbf5c878SMichael Roth      * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
532bbf5c878SMichael Roth      * inaccessible by the guest, since lookups rely on this path
533bbf5c878SMichael Roth      * existing in the composition tree
534bbf5c878SMichael Roth      */
535bbf5c878SMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
5360b55aa91SDavid Gibson     snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
537bbf5c878SMichael Roth     child_name = object_get_canonical_path_component(OBJECT(drc));
5380b55aa91SDavid Gibson     trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
539bbf5c878SMichael Roth     object_property_add_alias(root_container, link_name,
540bbf5c878SMichael Roth                               drc->owner, child_name, &err);
541bbf5c878SMichael Roth     if (err) {
5424fffeb5eSMarkus Armbruster         error_report_err(err);
543bbf5c878SMichael Roth         object_unref(OBJECT(drc));
544bbf5c878SMichael Roth     }
545586d2142SGonglei     g_free(child_name);
5460b55aa91SDavid Gibson     vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
547a50919ddSDaniel Henrique Barboza                      drc);
5480b55aa91SDavid Gibson     trace_spapr_drc_realize_complete(spapr_drc_index(drc));
549bbf5c878SMichael Roth }
550bbf5c878SMichael Roth 
551bbf5c878SMichael Roth static void unrealize(DeviceState *d, Error **errp)
552bbf5c878SMichael Roth {
553bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
554bbf5c878SMichael Roth     Object *root_container;
555bbf5c878SMichael Roth     char name[256];
556bbf5c878SMichael Roth     Error *err = NULL;
557bbf5c878SMichael Roth 
5580b55aa91SDavid Gibson     trace_spapr_drc_unrealize(spapr_drc_index(drc));
559bbf5c878SMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
5600b55aa91SDavid Gibson     snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
561bbf5c878SMichael Roth     object_property_del(root_container, name, &err);
562bbf5c878SMichael Roth     if (err) {
5634fffeb5eSMarkus Armbruster         error_report_err(err);
564bbf5c878SMichael Roth         object_unref(OBJECT(drc));
565bbf5c878SMichael Roth     }
566bbf5c878SMichael Roth }
567bbf5c878SMichael Roth 
5682d335818SDavid Gibson sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
569bbf5c878SMichael Roth                                          uint32_t id)
570bbf5c878SMichael Roth {
5712d335818SDavid Gibson     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
57294649d42SDavid Gibson     char *prop_name;
573bbf5c878SMichael Roth 
574bbf5c878SMichael Roth     drc->id = id;
575bbf5c878SMichael Roth     drc->owner = owner;
5760b55aa91SDavid Gibson     prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
5770b55aa91SDavid Gibson                                 spapr_drc_index(drc));
57894649d42SDavid Gibson     object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
579bbf5c878SMichael Roth     object_property_set_bool(OBJECT(drc), true, "realized", NULL);
58094649d42SDavid Gibson     g_free(prop_name);
581bbf5c878SMichael Roth 
582bbf5c878SMichael Roth     /* human-readable name for a DRC to encode into the DT
583bbf5c878SMichael Roth      * description. this is mainly only used within a guest in place
584bbf5c878SMichael Roth      * of the unique DRC index.
585bbf5c878SMichael Roth      *
586bbf5c878SMichael Roth      * in the case of VIO/PCI devices, it corresponds to a
587bbf5c878SMichael Roth      * "location code" that maps a logical device/function (DRC index)
588bbf5c878SMichael Roth      * to a physical (or virtual in the case of VIO) location in the
589bbf5c878SMichael Roth      * system by chaining together the "location label" for each
590bbf5c878SMichael Roth      * encapsulating component.
591bbf5c878SMichael Roth      *
592bbf5c878SMichael Roth      * since this is more to do with diagnosing physical hardware
593bbf5c878SMichael Roth      * issues than guest compatibility, we choose location codes/DRC
594bbf5c878SMichael Roth      * names that adhere to the documented format, but avoid encoding
595bbf5c878SMichael Roth      * the entire topology information into the label/code, instead
596bbf5c878SMichael Roth      * just using the location codes based on the labels for the
597bbf5c878SMichael Roth      * endpoints (VIO/PCI adaptor connectors), which is basically
598bbf5c878SMichael Roth      * just "C" followed by an integer ID.
599bbf5c878SMichael Roth      *
600bbf5c878SMichael Roth      * DRC names as documented by PAPR+ v2.7, 13.5.2.4
601bbf5c878SMichael Roth      * location codes as documented by PAPR+ v2.7, 12.3.1.5
602bbf5c878SMichael Roth      */
6032d335818SDavid Gibson     switch (spapr_drc_type(drc)) {
604bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_CPU:
605bbf5c878SMichael Roth         drc->name = g_strdup_printf("CPU %d", id);
606bbf5c878SMichael Roth         break;
607bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_PHB:
608bbf5c878SMichael Roth         drc->name = g_strdup_printf("PHB %d", id);
609bbf5c878SMichael Roth         break;
610bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_VIO:
611bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_PCI:
612bbf5c878SMichael Roth         drc->name = g_strdup_printf("C%d", id);
613bbf5c878SMichael Roth         break;
614bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_LMB:
615bbf5c878SMichael Roth         drc->name = g_strdup_printf("LMB %d", id);
616bbf5c878SMichael Roth         break;
617bbf5c878SMichael Roth     default:
618bbf5c878SMichael Roth         g_assert(false);
619bbf5c878SMichael Roth     }
620bbf5c878SMichael Roth 
621bbf5c878SMichael Roth     /* PCI slot always start in a USABLE state, and stay there */
6222d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
623bbf5c878SMichael Roth         drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
624bbf5c878SMichael Roth     }
625bbf5c878SMichael Roth 
626bbf5c878SMichael Roth     return drc;
627bbf5c878SMichael Roth }
628bbf5c878SMichael Roth 
629bbf5c878SMichael Roth static void spapr_dr_connector_instance_init(Object *obj)
630bbf5c878SMichael Roth {
631bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
632bbf5c878SMichael Roth 
633bbf5c878SMichael Roth     object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
634bbf5c878SMichael Roth     object_property_add(obj, "index", "uint32", prop_get_index,
635bbf5c878SMichael Roth                         NULL, NULL, NULL, NULL);
636bbf5c878SMichael Roth     object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
637bbf5c878SMichael Roth     object_property_add(obj, "fdt", "struct", prop_get_fdt,
638bbf5c878SMichael Roth                         NULL, NULL, NULL, NULL);
639bbf5c878SMichael Roth }
640bbf5c878SMichael Roth 
641bbf5c878SMichael Roth static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
642bbf5c878SMichael Roth {
643bbf5c878SMichael Roth     DeviceClass *dk = DEVICE_CLASS(k);
644bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
645bbf5c878SMichael Roth 
646bbf5c878SMichael Roth     dk->reset = reset;
647bbf5c878SMichael Roth     dk->realize = realize;
648bbf5c878SMichael Roth     dk->unrealize = unrealize;
649bbf5c878SMichael Roth     drck->set_isolation_state = set_isolation_state;
650bbf5c878SMichael Roth     drck->set_indicator_state = set_indicator_state;
651bbf5c878SMichael Roth     drck->set_allocation_state = set_allocation_state;
652bbf5c878SMichael Roth     drck->get_name = get_name;
653bbf5c878SMichael Roth     drck->attach = attach;
654bbf5c878SMichael Roth     drck->detach = detach;
655bbf5c878SMichael Roth     drck->release_pending = release_pending;
656f40eb921SMichael Roth     drck->set_signalled = set_signalled;
657c401ae8cSMarkus Armbruster     /*
658c401ae8cSMarkus Armbruster      * Reason: it crashes FIXME find and document the real reason
659c401ae8cSMarkus Armbruster      */
660e90f2a8cSEduardo Habkost     dk->user_creatable = false;
661bbf5c878SMichael Roth }
662bbf5c878SMichael Roth 
663*f224d35bSDavid Gibson static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
664*f224d35bSDavid Gibson {
665*f224d35bSDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
666*f224d35bSDavid Gibson 
667*f224d35bSDavid Gibson     drck->dr_entity_sense = physical_entity_sense;
668*f224d35bSDavid Gibson }
669*f224d35bSDavid Gibson 
670*f224d35bSDavid Gibson static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
671*f224d35bSDavid Gibson {
672*f224d35bSDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
673*f224d35bSDavid Gibson 
674*f224d35bSDavid Gibson     drck->dr_entity_sense = logical_entity_sense;
675*f224d35bSDavid Gibson }
676*f224d35bSDavid Gibson 
6772d335818SDavid Gibson static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
6782d335818SDavid Gibson {
6792d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
6802d335818SDavid Gibson 
6812d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
6821693ea16SDavid Gibson     drck->typename = "CPU";
6832d335818SDavid Gibson }
6842d335818SDavid Gibson 
6852d335818SDavid Gibson static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
6862d335818SDavid Gibson {
6872d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
6882d335818SDavid Gibson 
6892d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
6901693ea16SDavid Gibson     drck->typename = "28";
6912d335818SDavid Gibson }
6922d335818SDavid Gibson 
6932d335818SDavid Gibson static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
6942d335818SDavid Gibson {
6952d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
6962d335818SDavid Gibson 
6972d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
6981693ea16SDavid Gibson     drck->typename = "MEM";
6992d335818SDavid Gibson }
7002d335818SDavid Gibson 
701bbf5c878SMichael Roth static const TypeInfo spapr_dr_connector_info = {
702bbf5c878SMichael Roth     .name          = TYPE_SPAPR_DR_CONNECTOR,
703bbf5c878SMichael Roth     .parent        = TYPE_DEVICE,
704bbf5c878SMichael Roth     .instance_size = sizeof(sPAPRDRConnector),
705bbf5c878SMichael Roth     .instance_init = spapr_dr_connector_instance_init,
706bbf5c878SMichael Roth     .class_size    = sizeof(sPAPRDRConnectorClass),
707bbf5c878SMichael Roth     .class_init    = spapr_dr_connector_class_init,
7082d335818SDavid Gibson     .abstract      = true,
7092d335818SDavid Gibson };
7102d335818SDavid Gibson 
7112d335818SDavid Gibson static const TypeInfo spapr_drc_physical_info = {
7122d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_PHYSICAL,
7132d335818SDavid Gibson     .parent        = TYPE_SPAPR_DR_CONNECTOR,
7142d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
715*f224d35bSDavid Gibson     .class_init    = spapr_drc_physical_class_init,
7162d335818SDavid Gibson     .abstract      = true,
7172d335818SDavid Gibson };
7182d335818SDavid Gibson 
7192d335818SDavid Gibson static const TypeInfo spapr_drc_logical_info = {
7202d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_LOGICAL,
7212d335818SDavid Gibson     .parent        = TYPE_SPAPR_DR_CONNECTOR,
7222d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
723*f224d35bSDavid Gibson     .class_init    = spapr_drc_logical_class_init,
7242d335818SDavid Gibson     .abstract      = true,
7252d335818SDavid Gibson };
7262d335818SDavid Gibson 
7272d335818SDavid Gibson static const TypeInfo spapr_drc_cpu_info = {
7282d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_CPU,
7292d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_LOGICAL,
7302d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
7312d335818SDavid Gibson     .class_init    = spapr_drc_cpu_class_init,
7322d335818SDavid Gibson };
7332d335818SDavid Gibson 
7342d335818SDavid Gibson static const TypeInfo spapr_drc_pci_info = {
7352d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_PCI,
7362d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_PHYSICAL,
7372d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
7382d335818SDavid Gibson     .class_init    = spapr_drc_pci_class_init,
7392d335818SDavid Gibson };
7402d335818SDavid Gibson 
7412d335818SDavid Gibson static const TypeInfo spapr_drc_lmb_info = {
7422d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_LMB,
7432d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_LOGICAL,
7442d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
7452d335818SDavid Gibson     .class_init    = spapr_drc_lmb_class_init,
746bbf5c878SMichael Roth };
747bbf5c878SMichael Roth 
748bbf5c878SMichael Roth /* helper functions for external users */
749bbf5c878SMichael Roth 
750fbf55397SDavid Gibson sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
751bbf5c878SMichael Roth {
752bbf5c878SMichael Roth     Object *obj;
753bbf5c878SMichael Roth     char name[256];
754bbf5c878SMichael Roth 
755bbf5c878SMichael Roth     snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
756bbf5c878SMichael Roth     obj = object_resolve_path(name, NULL);
757bbf5c878SMichael Roth 
758bbf5c878SMichael Roth     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
759bbf5c878SMichael Roth }
760bbf5c878SMichael Roth 
761fbf55397SDavid Gibson sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
762bbf5c878SMichael Roth {
763fbf55397SDavid Gibson     sPAPRDRConnectorClass *drck
764fbf55397SDavid Gibson         = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
765fbf55397SDavid Gibson 
766fbf55397SDavid Gibson     return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
767fbf55397SDavid Gibson                               | (id & DRC_INDEX_ID_MASK));
768bbf5c878SMichael Roth }
769e4b798bbSMichael Roth 
770e4b798bbSMichael Roth /**
771e4b798bbSMichael Roth  * spapr_drc_populate_dt
772e4b798bbSMichael Roth  *
773e4b798bbSMichael Roth  * @fdt: libfdt device tree
774e4b798bbSMichael Roth  * @path: path in the DT to generate properties
775e4b798bbSMichael Roth  * @owner: parent Object/DeviceState for which to generate DRC
776e4b798bbSMichael Roth  *         descriptions for
777e4b798bbSMichael Roth  * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
778e4b798bbSMichael Roth  *   to the types of DRCs to generate entries for
779e4b798bbSMichael Roth  *
780e4b798bbSMichael Roth  * generate OF properties to describe DRC topology/indices to guests
781e4b798bbSMichael Roth  *
782e4b798bbSMichael Roth  * as documented in PAPR+ v2.1, 13.5.2
783e4b798bbSMichael Roth  */
784e4b798bbSMichael Roth int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
785e4b798bbSMichael Roth                           uint32_t drc_type_mask)
786e4b798bbSMichael Roth {
787e4b798bbSMichael Roth     Object *root_container;
788e4b798bbSMichael Roth     ObjectProperty *prop;
7897746abd8SDaniel P. Berrange     ObjectPropertyIterator iter;
790e4b798bbSMichael Roth     uint32_t drc_count = 0;
791e4b798bbSMichael Roth     GArray *drc_indexes, *drc_power_domains;
792e4b798bbSMichael Roth     GString *drc_names, *drc_types;
793e4b798bbSMichael Roth     int ret;
794e4b798bbSMichael Roth 
795e4b798bbSMichael Roth     /* the first entry of each properties is a 32-bit integer encoding
796e4b798bbSMichael Roth      * the number of elements in the array. we won't know this until
797e4b798bbSMichael Roth      * we complete the iteration through all the matching DRCs, but
798e4b798bbSMichael Roth      * reserve the space now and set the offsets accordingly so we
799e4b798bbSMichael Roth      * can fill them in later.
800e4b798bbSMichael Roth      */
801e4b798bbSMichael Roth     drc_indexes = g_array_new(false, true, sizeof(uint32_t));
802e4b798bbSMichael Roth     drc_indexes = g_array_set_size(drc_indexes, 1);
803e4b798bbSMichael Roth     drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
804e4b798bbSMichael Roth     drc_power_domains = g_array_set_size(drc_power_domains, 1);
805e4b798bbSMichael Roth     drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
806e4b798bbSMichael Roth     drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
807e4b798bbSMichael Roth 
808e4b798bbSMichael Roth     /* aliases for all DRConnector objects will be rooted in QOM
809e4b798bbSMichael Roth      * composition tree at DRC_CONTAINER_PATH
810e4b798bbSMichael Roth      */
811e4b798bbSMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
812e4b798bbSMichael Roth 
8137746abd8SDaniel P. Berrange     object_property_iter_init(&iter, root_container);
8147746abd8SDaniel P. Berrange     while ((prop = object_property_iter_next(&iter))) {
815e4b798bbSMichael Roth         Object *obj;
816e4b798bbSMichael Roth         sPAPRDRConnector *drc;
817e4b798bbSMichael Roth         sPAPRDRConnectorClass *drck;
818e4b798bbSMichael Roth         uint32_t drc_index, drc_power_domain;
819e4b798bbSMichael Roth 
820e4b798bbSMichael Roth         if (!strstart(prop->type, "link<", NULL)) {
821e4b798bbSMichael Roth             continue;
822e4b798bbSMichael Roth         }
823e4b798bbSMichael Roth 
824e4b798bbSMichael Roth         obj = object_property_get_link(root_container, prop->name, NULL);
825e4b798bbSMichael Roth         drc = SPAPR_DR_CONNECTOR(obj);
826e4b798bbSMichael Roth         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
827e4b798bbSMichael Roth 
828e4b798bbSMichael Roth         if (owner && (drc->owner != owner)) {
829e4b798bbSMichael Roth             continue;
830e4b798bbSMichael Roth         }
831e4b798bbSMichael Roth 
8322d335818SDavid Gibson         if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
833e4b798bbSMichael Roth             continue;
834e4b798bbSMichael Roth         }
835e4b798bbSMichael Roth 
836e4b798bbSMichael Roth         drc_count++;
837e4b798bbSMichael Roth 
838e4b798bbSMichael Roth         /* ibm,drc-indexes */
8390b55aa91SDavid Gibson         drc_index = cpu_to_be32(spapr_drc_index(drc));
840e4b798bbSMichael Roth         g_array_append_val(drc_indexes, drc_index);
841e4b798bbSMichael Roth 
842e4b798bbSMichael Roth         /* ibm,drc-power-domains */
843e4b798bbSMichael Roth         drc_power_domain = cpu_to_be32(-1);
844e4b798bbSMichael Roth         g_array_append_val(drc_power_domains, drc_power_domain);
845e4b798bbSMichael Roth 
846e4b798bbSMichael Roth         /* ibm,drc-names */
847e4b798bbSMichael Roth         drc_names = g_string_append(drc_names, drck->get_name(drc));
848e4b798bbSMichael Roth         drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
849e4b798bbSMichael Roth 
850e4b798bbSMichael Roth         /* ibm,drc-types */
8511693ea16SDavid Gibson         drc_types = g_string_append(drc_types, drck->typename);
852e4b798bbSMichael Roth         drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
853e4b798bbSMichael Roth     }
854e4b798bbSMichael Roth 
855e4b798bbSMichael Roth     /* now write the drc count into the space we reserved at the
856e4b798bbSMichael Roth      * beginning of the arrays previously
857e4b798bbSMichael Roth      */
858e4b798bbSMichael Roth     *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
859e4b798bbSMichael Roth     *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
860e4b798bbSMichael Roth     *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
861e4b798bbSMichael Roth     *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
862e4b798bbSMichael Roth 
863e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
864e4b798bbSMichael Roth                       drc_indexes->data,
865e4b798bbSMichael Roth                       drc_indexes->len * sizeof(uint32_t));
866e4b798bbSMichael Roth     if (ret) {
867ce9863b7SCédric Le Goater         error_report("Couldn't create ibm,drc-indexes property");
868e4b798bbSMichael Roth         goto out;
869e4b798bbSMichael Roth     }
870e4b798bbSMichael Roth 
871e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
872e4b798bbSMichael Roth                       drc_power_domains->data,
873e4b798bbSMichael Roth                       drc_power_domains->len * sizeof(uint32_t));
874e4b798bbSMichael Roth     if (ret) {
875ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-power-domains property");
876e4b798bbSMichael Roth         goto out;
877e4b798bbSMichael Roth     }
878e4b798bbSMichael Roth 
879e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
880e4b798bbSMichael Roth                       drc_names->str, drc_names->len);
881e4b798bbSMichael Roth     if (ret) {
882ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-names property");
883e4b798bbSMichael Roth         goto out;
884e4b798bbSMichael Roth     }
885e4b798bbSMichael Roth 
886e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
887e4b798bbSMichael Roth                       drc_types->str, drc_types->len);
888e4b798bbSMichael Roth     if (ret) {
889ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-types property");
890e4b798bbSMichael Roth         goto out;
891e4b798bbSMichael Roth     }
892e4b798bbSMichael Roth 
893e4b798bbSMichael Roth out:
894e4b798bbSMichael Roth     g_array_free(drc_indexes, true);
895e4b798bbSMichael Roth     g_array_free(drc_power_domains, true);
896e4b798bbSMichael Roth     g_string_free(drc_names, true);
897e4b798bbSMichael Roth     g_string_free(drc_types, true);
898e4b798bbSMichael Roth 
899e4b798bbSMichael Roth     return ret;
900e4b798bbSMichael Roth }
901b89b3d39SDavid Gibson 
902b89b3d39SDavid Gibson /*
903b89b3d39SDavid Gibson  * RTAS calls
904b89b3d39SDavid Gibson  */
905b89b3d39SDavid Gibson 
906b89b3d39SDavid Gibson static bool sensor_type_is_dr(uint32_t sensor_type)
907b89b3d39SDavid Gibson {
908b89b3d39SDavid Gibson     switch (sensor_type) {
909b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
910b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_DR:
911b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
912b89b3d39SDavid Gibson         return true;
913b89b3d39SDavid Gibson     }
914b89b3d39SDavid Gibson 
915b89b3d39SDavid Gibson     return false;
916b89b3d39SDavid Gibson }
917b89b3d39SDavid Gibson 
918b89b3d39SDavid Gibson static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
919b89b3d39SDavid Gibson                                uint32_t token, uint32_t nargs,
920b89b3d39SDavid Gibson                                target_ulong args, uint32_t nret,
921b89b3d39SDavid Gibson                                target_ulong rets)
922b89b3d39SDavid Gibson {
923b89b3d39SDavid Gibson     uint32_t sensor_type;
924b89b3d39SDavid Gibson     uint32_t sensor_index;
925b89b3d39SDavid Gibson     uint32_t sensor_state;
926b89b3d39SDavid Gibson     uint32_t ret = RTAS_OUT_SUCCESS;
927b89b3d39SDavid Gibson     sPAPRDRConnector *drc;
928b89b3d39SDavid Gibson     sPAPRDRConnectorClass *drck;
929b89b3d39SDavid Gibson 
930b89b3d39SDavid Gibson     if (nargs != 3 || nret != 1) {
931b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
932b89b3d39SDavid Gibson         goto out;
933b89b3d39SDavid Gibson     }
934b89b3d39SDavid Gibson 
935b89b3d39SDavid Gibson     sensor_type = rtas_ld(args, 0);
936b89b3d39SDavid Gibson     sensor_index = rtas_ld(args, 1);
937b89b3d39SDavid Gibson     sensor_state = rtas_ld(args, 2);
938b89b3d39SDavid Gibson 
939b89b3d39SDavid Gibson     if (!sensor_type_is_dr(sensor_type)) {
940b89b3d39SDavid Gibson         goto out_unimplemented;
941b89b3d39SDavid Gibson     }
942b89b3d39SDavid Gibson 
943b89b3d39SDavid Gibson     /* if this is a DR sensor we can assume sensor_index == drc_index */
944fbf55397SDavid Gibson     drc = spapr_drc_by_index(sensor_index);
945b89b3d39SDavid Gibson     if (!drc) {
946b89b3d39SDavid Gibson         trace_spapr_rtas_set_indicator_invalid(sensor_index);
947b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
948b89b3d39SDavid Gibson         goto out;
949b89b3d39SDavid Gibson     }
950b89b3d39SDavid Gibson     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
951b89b3d39SDavid Gibson 
952b89b3d39SDavid Gibson     switch (sensor_type) {
953b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
954b89b3d39SDavid Gibson         ret = drck->set_isolation_state(drc, sensor_state);
955b89b3d39SDavid Gibson         break;
956b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_DR:
957b89b3d39SDavid Gibson         ret = drck->set_indicator_state(drc, sensor_state);
958b89b3d39SDavid Gibson         break;
959b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
960b89b3d39SDavid Gibson         ret = drck->set_allocation_state(drc, sensor_state);
961b89b3d39SDavid Gibson         break;
962b89b3d39SDavid Gibson     default:
963b89b3d39SDavid Gibson         goto out_unimplemented;
964b89b3d39SDavid Gibson     }
965b89b3d39SDavid Gibson 
966b89b3d39SDavid Gibson out:
967b89b3d39SDavid Gibson     rtas_st(rets, 0, ret);
968b89b3d39SDavid Gibson     return;
969b89b3d39SDavid Gibson 
970b89b3d39SDavid Gibson out_unimplemented:
971b89b3d39SDavid Gibson     /* currently only DR-related sensors are implemented */
972b89b3d39SDavid Gibson     trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
973b89b3d39SDavid Gibson     rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
974b89b3d39SDavid Gibson }
975b89b3d39SDavid Gibson 
976b89b3d39SDavid Gibson static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
977b89b3d39SDavid Gibson                                   uint32_t token, uint32_t nargs,
978b89b3d39SDavid Gibson                                   target_ulong args, uint32_t nret,
979b89b3d39SDavid Gibson                                   target_ulong rets)
980b89b3d39SDavid Gibson {
981b89b3d39SDavid Gibson     uint32_t sensor_type;
982b89b3d39SDavid Gibson     uint32_t sensor_index;
983b89b3d39SDavid Gibson     uint32_t sensor_state = 0;
984b89b3d39SDavid Gibson     sPAPRDRConnector *drc;
985b89b3d39SDavid Gibson     sPAPRDRConnectorClass *drck;
986b89b3d39SDavid Gibson     uint32_t ret = RTAS_OUT_SUCCESS;
987b89b3d39SDavid Gibson 
988b89b3d39SDavid Gibson     if (nargs != 2 || nret != 2) {
989b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
990b89b3d39SDavid Gibson         goto out;
991b89b3d39SDavid Gibson     }
992b89b3d39SDavid Gibson 
993b89b3d39SDavid Gibson     sensor_type = rtas_ld(args, 0);
994b89b3d39SDavid Gibson     sensor_index = rtas_ld(args, 1);
995b89b3d39SDavid Gibson 
996b89b3d39SDavid Gibson     if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
997b89b3d39SDavid Gibson         /* currently only DR-related sensors are implemented */
998b89b3d39SDavid Gibson         trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
999b89b3d39SDavid Gibson                                                         sensor_type);
1000b89b3d39SDavid Gibson         ret = RTAS_OUT_NOT_SUPPORTED;
1001b89b3d39SDavid Gibson         goto out;
1002b89b3d39SDavid Gibson     }
1003b89b3d39SDavid Gibson 
1004fbf55397SDavid Gibson     drc = spapr_drc_by_index(sensor_index);
1005b89b3d39SDavid Gibson     if (!drc) {
1006b89b3d39SDavid Gibson         trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1007b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
1008b89b3d39SDavid Gibson         goto out;
1009b89b3d39SDavid Gibson     }
1010b89b3d39SDavid Gibson     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1011*f224d35bSDavid Gibson     sensor_state = drck->dr_entity_sense(drc);
1012b89b3d39SDavid Gibson 
1013b89b3d39SDavid Gibson out:
1014b89b3d39SDavid Gibson     rtas_st(rets, 0, ret);
1015b89b3d39SDavid Gibson     rtas_st(rets, 1, sensor_state);
1016b89b3d39SDavid Gibson }
1017b89b3d39SDavid Gibson 
1018b89b3d39SDavid Gibson /* configure-connector work area offsets, int32_t units for field
1019b89b3d39SDavid Gibson  * indexes, bytes for field offset/len values.
1020b89b3d39SDavid Gibson  *
1021b89b3d39SDavid Gibson  * as documented by PAPR+ v2.7, 13.5.3.5
1022b89b3d39SDavid Gibson  */
1023b89b3d39SDavid Gibson #define CC_IDX_NODE_NAME_OFFSET 2
1024b89b3d39SDavid Gibson #define CC_IDX_PROP_NAME_OFFSET 2
1025b89b3d39SDavid Gibson #define CC_IDX_PROP_LEN 3
1026b89b3d39SDavid Gibson #define CC_IDX_PROP_DATA_OFFSET 4
1027b89b3d39SDavid Gibson #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1028b89b3d39SDavid Gibson #define CC_WA_LEN 4096
1029b89b3d39SDavid Gibson 
1030b89b3d39SDavid Gibson static void configure_connector_st(target_ulong addr, target_ulong offset,
1031b89b3d39SDavid Gibson                                    const void *buf, size_t len)
1032b89b3d39SDavid Gibson {
1033b89b3d39SDavid Gibson     cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1034b89b3d39SDavid Gibson                               buf, MIN(len, CC_WA_LEN - offset));
1035b89b3d39SDavid Gibson }
1036b89b3d39SDavid Gibson 
1037b89b3d39SDavid Gibson static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1038b89b3d39SDavid Gibson                                          sPAPRMachineState *spapr,
1039b89b3d39SDavid Gibson                                          uint32_t token, uint32_t nargs,
1040b89b3d39SDavid Gibson                                          target_ulong args, uint32_t nret,
1041b89b3d39SDavid Gibson                                          target_ulong rets)
1042b89b3d39SDavid Gibson {
1043b89b3d39SDavid Gibson     uint64_t wa_addr;
1044b89b3d39SDavid Gibson     uint64_t wa_offset;
1045b89b3d39SDavid Gibson     uint32_t drc_index;
1046b89b3d39SDavid Gibson     sPAPRDRConnector *drc;
1047b89b3d39SDavid Gibson     sPAPRConfigureConnectorState *ccs;
1048b89b3d39SDavid Gibson     sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1049b89b3d39SDavid Gibson     int rc;
1050b89b3d39SDavid Gibson 
1051b89b3d39SDavid Gibson     if (nargs != 2 || nret != 1) {
1052b89b3d39SDavid Gibson         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1053b89b3d39SDavid Gibson         return;
1054b89b3d39SDavid Gibson     }
1055b89b3d39SDavid Gibson 
1056b89b3d39SDavid Gibson     wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1057b89b3d39SDavid Gibson 
1058b89b3d39SDavid Gibson     drc_index = rtas_ld(wa_addr, 0);
1059fbf55397SDavid Gibson     drc = spapr_drc_by_index(drc_index);
1060b89b3d39SDavid Gibson     if (!drc) {
1061b89b3d39SDavid Gibson         trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1062b89b3d39SDavid Gibson         rc = RTAS_OUT_PARAM_ERROR;
1063b89b3d39SDavid Gibson         goto out;
1064b89b3d39SDavid Gibson     }
1065b89b3d39SDavid Gibson 
106688af6ea5SDavid Gibson     if (!drc->fdt) {
1067b89b3d39SDavid Gibson         trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1068b89b3d39SDavid Gibson         rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1069b89b3d39SDavid Gibson         goto out;
1070b89b3d39SDavid Gibson     }
1071b89b3d39SDavid Gibson 
1072b8fdd530SDavid Gibson     ccs = drc->ccs;
1073b89b3d39SDavid Gibson     if (!ccs) {
1074b89b3d39SDavid Gibson         ccs = g_new0(sPAPRConfigureConnectorState, 1);
107588af6ea5SDavid Gibson         ccs->fdt_offset = drc->fdt_start_offset;
1076b8fdd530SDavid Gibson         drc->ccs = ccs;
1077b89b3d39SDavid Gibson     }
1078b89b3d39SDavid Gibson 
1079b89b3d39SDavid Gibson     do {
1080b89b3d39SDavid Gibson         uint32_t tag;
1081b89b3d39SDavid Gibson         const char *name;
1082b89b3d39SDavid Gibson         const struct fdt_property *prop;
1083b89b3d39SDavid Gibson         int fdt_offset_next, prop_len;
1084b89b3d39SDavid Gibson 
108588af6ea5SDavid Gibson         tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1086b89b3d39SDavid Gibson 
1087b89b3d39SDavid Gibson         switch (tag) {
1088b89b3d39SDavid Gibson         case FDT_BEGIN_NODE:
1089b89b3d39SDavid Gibson             ccs->fdt_depth++;
109088af6ea5SDavid Gibson             name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1091b89b3d39SDavid Gibson 
1092b89b3d39SDavid Gibson             /* provide the name of the next OF node */
1093b89b3d39SDavid Gibson             wa_offset = CC_VAL_DATA_OFFSET;
1094b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1095b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1096b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1097b89b3d39SDavid Gibson             break;
1098b89b3d39SDavid Gibson         case FDT_END_NODE:
1099b89b3d39SDavid Gibson             ccs->fdt_depth--;
1100b89b3d39SDavid Gibson             if (ccs->fdt_depth == 0) {
11014f65ce00SDavid Gibson                 sPAPRDRIsolationState state = drc->isolation_state;
11020b55aa91SDavid Gibson                 uint32_t drc_index = spapr_drc_index(drc);
1103b89b3d39SDavid Gibson                 /* done sending the device tree, don't need to track
1104b89b3d39SDavid Gibson                  * the state anymore
1105b89b3d39SDavid Gibson                  */
11060b55aa91SDavid Gibson                 trace_spapr_drc_set_configured(drc_index);
11074f65ce00SDavid Gibson                 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
11084f65ce00SDavid Gibson                     drc->configured = true;
11094f65ce00SDavid Gibson                 } else {
11104f65ce00SDavid Gibson                     /* guest should be not configuring an isolated device */
11110b55aa91SDavid Gibson                     trace_spapr_drc_set_configured_skipping(drc_index);
11124f65ce00SDavid Gibson                 }
1113b8fdd530SDavid Gibson                 g_free(ccs);
1114b8fdd530SDavid Gibson                 drc->ccs = NULL;
1115b89b3d39SDavid Gibson                 ccs = NULL;
1116b89b3d39SDavid Gibson                 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1117b89b3d39SDavid Gibson             } else {
1118b89b3d39SDavid Gibson                 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1119b89b3d39SDavid Gibson             }
1120b89b3d39SDavid Gibson             break;
1121b89b3d39SDavid Gibson         case FDT_PROP:
112288af6ea5SDavid Gibson             prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1123b89b3d39SDavid Gibson                                               &prop_len);
112488af6ea5SDavid Gibson             name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1125b89b3d39SDavid Gibson 
1126b89b3d39SDavid Gibson             /* provide the name of the next OF property */
1127b89b3d39SDavid Gibson             wa_offset = CC_VAL_DATA_OFFSET;
1128b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1129b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1130b89b3d39SDavid Gibson 
1131b89b3d39SDavid Gibson             /* provide the length and value of the OF property. data gets
1132b89b3d39SDavid Gibson              * placed immediately after NULL terminator of the OF property's
1133b89b3d39SDavid Gibson              * name string
1134b89b3d39SDavid Gibson              */
1135b89b3d39SDavid Gibson             wa_offset += strlen(name) + 1,
1136b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1137b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1138b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1139b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1140b89b3d39SDavid Gibson             break;
1141b89b3d39SDavid Gibson         case FDT_END:
1142b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_ERROR;
1143b89b3d39SDavid Gibson         default:
1144b89b3d39SDavid Gibson             /* keep seeking for an actionable tag */
1145b89b3d39SDavid Gibson             break;
1146b89b3d39SDavid Gibson         }
1147b89b3d39SDavid Gibson         if (ccs) {
1148b89b3d39SDavid Gibson             ccs->fdt_offset = fdt_offset_next;
1149b89b3d39SDavid Gibson         }
1150b89b3d39SDavid Gibson     } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1151b89b3d39SDavid Gibson 
1152b89b3d39SDavid Gibson     rc = resp;
1153b89b3d39SDavid Gibson out:
1154b89b3d39SDavid Gibson     rtas_st(rets, 0, rc);
1155b89b3d39SDavid Gibson }
1156b89b3d39SDavid Gibson 
1157b89b3d39SDavid Gibson static void spapr_drc_register_types(void)
1158b89b3d39SDavid Gibson {
1159b89b3d39SDavid Gibson     type_register_static(&spapr_dr_connector_info);
11602d335818SDavid Gibson     type_register_static(&spapr_drc_physical_info);
11612d335818SDavid Gibson     type_register_static(&spapr_drc_logical_info);
11622d335818SDavid Gibson     type_register_static(&spapr_drc_cpu_info);
11632d335818SDavid Gibson     type_register_static(&spapr_drc_pci_info);
11642d335818SDavid Gibson     type_register_static(&spapr_drc_lmb_info);
1165b89b3d39SDavid Gibson 
1166b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1167b89b3d39SDavid Gibson                         rtas_set_indicator);
1168b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1169b89b3d39SDavid Gibson                         rtas_get_sensor_state);
1170b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1171b89b3d39SDavid Gibson                         rtas_ibm_configure_connector);
1172b89b3d39SDavid Gibson }
1173b89b3d39SDavid Gibson type_init(spapr_drc_register_types)
1174