xref: /qemu/hw/ppc/spapr_drc.c (revision b8fdd530be3450940130b63d930bb0aee1538e7e)
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 
56*b8fdd530SDavid Gibson     /* if the guest is configuring a device attached to this DRC, we
57*b8fdd530SDavid Gibson      * should reset the configuration state at this point since it may
58*b8fdd530SDavid Gibson      * no longer be reliable (guest released device and needs to start
59*b8fdd530SDavid Gibson      * over, or unplug occurred so the FDT is no longer valid)
60*b8fdd530SDavid Gibson      */
61*b8fdd530SDavid Gibson     if (state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
62*b8fdd530SDavid Gibson         g_free(drc->ccs);
63*b8fdd530SDavid Gibson         drc->ccs = NULL;
64*b8fdd530SDavid Gibson     }
65*b8fdd530SDavid 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  */
1880cb688d2SMichael Roth static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
189bbf5c878SMichael Roth {
190bbf5c878SMichael Roth     if (drc->dev) {
1912d335818SDavid Gibson         if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
192bbf5c878SMichael Roth             drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
193bbf5c878SMichael Roth             /* for logical DR, we return a state of UNUSABLE
194bbf5c878SMichael Roth              * iff the allocation state UNUSABLE.
195bbf5c878SMichael Roth              * Otherwise, report the state as USABLE/PRESENT,
196bbf5c878SMichael Roth              * as we would for PCI.
197bbf5c878SMichael Roth              */
1980cb688d2SMichael Roth             *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
199bbf5c878SMichael Roth         } else {
200bbf5c878SMichael Roth             /* this assumes all PCI devices are assigned to
201bbf5c878SMichael Roth              * a 'live insertion' power domain, where QEMU
202bbf5c878SMichael Roth              * manages power state automatically as opposed
203bbf5c878SMichael Roth              * to the guest. present, non-PCI resources are
204bbf5c878SMichael Roth              * unaffected by power state.
205bbf5c878SMichael Roth              */
2060cb688d2SMichael Roth             *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
207bbf5c878SMichael Roth         }
208bbf5c878SMichael Roth     } else {
2092d335818SDavid Gibson         if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
210bbf5c878SMichael Roth             /* PCI devices, and only PCI devices, use EMPTY
211bbf5c878SMichael Roth              * in cases where we'd otherwise use UNUSABLE
212bbf5c878SMichael Roth              */
2130cb688d2SMichael Roth             *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
214bbf5c878SMichael Roth         } else {
2150cb688d2SMichael Roth             *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
216bbf5c878SMichael Roth         }
217bbf5c878SMichael Roth     }
218bbf5c878SMichael Roth 
2190b55aa91SDavid Gibson     trace_spapr_drc_entity_sense(spapr_drc_index(drc), *state);
2200cb688d2SMichael Roth     return RTAS_OUT_SUCCESS;
221bbf5c878SMichael Roth }
222bbf5c878SMichael Roth 
223d7bce999SEric Blake static void prop_get_index(Object *obj, Visitor *v, const char *name,
224d7bce999SEric Blake                            void *opaque, Error **errp)
225bbf5c878SMichael Roth {
226bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
2270b55aa91SDavid Gibson     uint32_t value = spapr_drc_index(drc);
22851e72bc1SEric Blake     visit_type_uint32(v, name, &value, errp);
229bbf5c878SMichael Roth }
230bbf5c878SMichael Roth 
231d7bce999SEric Blake static void prop_get_type(Object *obj, Visitor *v, const char *name,
232d7bce999SEric Blake                           void *opaque, Error **errp)
233bbf5c878SMichael Roth {
234bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
2350b55aa91SDavid Gibson     uint32_t value = (uint32_t)spapr_drc_type(drc);
23651e72bc1SEric Blake     visit_type_uint32(v, name, &value, errp);
237bbf5c878SMichael Roth }
238bbf5c878SMichael Roth 
239bbf5c878SMichael Roth static char *prop_get_name(Object *obj, Error **errp)
240bbf5c878SMichael Roth {
241bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
242bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
243bbf5c878SMichael Roth     return g_strdup(drck->get_name(drc));
244bbf5c878SMichael Roth }
245bbf5c878SMichael Roth 
246d7bce999SEric Blake static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
247d7bce999SEric Blake                                   void *opaque, Error **errp)
248bbf5c878SMichael Roth {
249bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
250bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2510cb688d2SMichael Roth     uint32_t value;
2520cb688d2SMichael Roth 
2530cb688d2SMichael Roth     drck->entity_sense(drc, &value);
25451e72bc1SEric Blake     visit_type_uint32(v, name, &value, errp);
255bbf5c878SMichael Roth }
256bbf5c878SMichael Roth 
257d7bce999SEric Blake static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
258d7bce999SEric Blake                          void *opaque, Error **errp)
259bbf5c878SMichael Roth {
260bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
261c75304a1SMarkus Armbruster     Error *err = NULL;
262bbf5c878SMichael Roth     int fdt_offset_next, fdt_offset, fdt_depth;
263bbf5c878SMichael Roth     void *fdt;
264bbf5c878SMichael Roth 
265bbf5c878SMichael Roth     if (!drc->fdt) {
266a543a554SEric Blake         visit_type_null(v, NULL, errp);
267bbf5c878SMichael Roth         return;
268bbf5c878SMichael Roth     }
269bbf5c878SMichael Roth 
270bbf5c878SMichael Roth     fdt = drc->fdt;
271bbf5c878SMichael Roth     fdt_offset = drc->fdt_start_offset;
272bbf5c878SMichael Roth     fdt_depth = 0;
273bbf5c878SMichael Roth 
274bbf5c878SMichael Roth     do {
275bbf5c878SMichael Roth         const char *name = NULL;
276bbf5c878SMichael Roth         const struct fdt_property *prop = NULL;
277bbf5c878SMichael Roth         int prop_len = 0, name_len = 0;
278bbf5c878SMichael Roth         uint32_t tag;
279bbf5c878SMichael Roth 
280bbf5c878SMichael Roth         tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
281bbf5c878SMichael Roth         switch (tag) {
282bbf5c878SMichael Roth         case FDT_BEGIN_NODE:
283bbf5c878SMichael Roth             fdt_depth++;
284bbf5c878SMichael Roth             name = fdt_get_name(fdt, fdt_offset, &name_len);
285337283dfSEric Blake             visit_start_struct(v, name, NULL, 0, &err);
286c75304a1SMarkus Armbruster             if (err) {
287c75304a1SMarkus Armbruster                 error_propagate(errp, err);
288c75304a1SMarkus Armbruster                 return;
289c75304a1SMarkus Armbruster             }
290bbf5c878SMichael Roth             break;
291bbf5c878SMichael Roth         case FDT_END_NODE:
292bbf5c878SMichael Roth             /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
293bbf5c878SMichael Roth             g_assert(fdt_depth > 0);
29415c2f669SEric Blake             visit_check_struct(v, &err);
2951158bb2aSEric Blake             visit_end_struct(v, NULL);
296c75304a1SMarkus Armbruster             if (err) {
297c75304a1SMarkus Armbruster                 error_propagate(errp, err);
298c75304a1SMarkus Armbruster                 return;
299c75304a1SMarkus Armbruster             }
300bbf5c878SMichael Roth             fdt_depth--;
301bbf5c878SMichael Roth             break;
302bbf5c878SMichael Roth         case FDT_PROP: {
303bbf5c878SMichael Roth             int i;
304bbf5c878SMichael Roth             prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
305bbf5c878SMichael Roth             name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
306d9f62ddeSEric Blake             visit_start_list(v, name, NULL, 0, &err);
307c75304a1SMarkus Armbruster             if (err) {
308c75304a1SMarkus Armbruster                 error_propagate(errp, err);
309c75304a1SMarkus Armbruster                 return;
310bbf5c878SMichael Roth             }
311c75304a1SMarkus Armbruster             for (i = 0; i < prop_len; i++) {
31251e72bc1SEric Blake                 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
313c75304a1SMarkus Armbruster                 if (err) {
314c75304a1SMarkus Armbruster                     error_propagate(errp, err);
315c75304a1SMarkus Armbruster                     return;
316c75304a1SMarkus Armbruster                 }
317c75304a1SMarkus Armbruster             }
318a4a1c70dSMarkus Armbruster             visit_check_list(v, &err);
3191158bb2aSEric Blake             visit_end_list(v, NULL);
320a4a1c70dSMarkus Armbruster             if (err) {
321a4a1c70dSMarkus Armbruster                 error_propagate(errp, err);
322a4a1c70dSMarkus Armbruster                 return;
323a4a1c70dSMarkus Armbruster             }
324bbf5c878SMichael Roth             break;
325bbf5c878SMichael Roth         }
326bbf5c878SMichael Roth         default:
327bbf5c878SMichael Roth             error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
328bbf5c878SMichael Roth         }
329bbf5c878SMichael Roth         fdt_offset = fdt_offset_next;
330bbf5c878SMichael Roth     } while (fdt_depth != 0);
331bbf5c878SMichael Roth }
332bbf5c878SMichael Roth 
333bbf5c878SMichael Roth static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
334bbf5c878SMichael Roth                    int fdt_start_offset, bool coldplug, Error **errp)
335bbf5c878SMichael Roth {
3360b55aa91SDavid Gibson     trace_spapr_drc_attach(spapr_drc_index(drc));
337bbf5c878SMichael Roth 
338bbf5c878SMichael Roth     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
339bbf5c878SMichael Roth         error_setg(errp, "an attached device is still awaiting release");
340bbf5c878SMichael Roth         return;
341bbf5c878SMichael Roth     }
3422d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
343bbf5c878SMichael Roth         g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
344bbf5c878SMichael Roth     }
345bbf5c878SMichael Roth     g_assert(fdt || coldplug);
346bbf5c878SMichael Roth 
347bbf5c878SMichael Roth     /* NOTE: setting initial isolation state to UNISOLATED means we can't
348bbf5c878SMichael Roth      * detach unless guest has a userspace/kernel that moves this state
349bbf5c878SMichael Roth      * back to ISOLATED in response to an unplug event, or this is done
350bbf5c878SMichael Roth      * manually by the admin prior. if we force things while the guest
351bbf5c878SMichael Roth      * may be accessing the device, we can easily crash the guest, so we
352bbf5c878SMichael Roth      * we defer completion of removal in such cases to the reset() hook.
353bbf5c878SMichael Roth      */
3542d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
355bbf5c878SMichael Roth         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
356bbf5c878SMichael Roth     }
357bbf5c878SMichael Roth     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
358bbf5c878SMichael Roth 
359bbf5c878SMichael Roth     drc->dev = d;
360bbf5c878SMichael Roth     drc->fdt = fdt;
361bbf5c878SMichael Roth     drc->fdt_start_offset = fdt_start_offset;
362785652dcSLaurent Vivier     drc->configured = coldplug;
363df18b2dbSMichael Roth     /* 'logical' DR resources such as memory/cpus are in some cases treated
364df18b2dbSMichael Roth      * as a pool of resources from which the guest is free to choose from
365df18b2dbSMichael Roth      * based on only a count. for resources that can be assigned in this
366df18b2dbSMichael Roth      * fashion, we must assume the resource is signalled immediately
367df18b2dbSMichael Roth      * since a single hotplug request might make an arbitrary number of
368df18b2dbSMichael Roth      * such attached resources available to the guest, as opposed to
369df18b2dbSMichael Roth      * 'physical' DR resources such as PCI where each device/resource is
370df18b2dbSMichael Roth      * signalled individually.
371df18b2dbSMichael Roth      */
3722d335818SDavid Gibson     drc->signalled = (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI)
373df18b2dbSMichael Roth                      ? true : coldplug;
374bbf5c878SMichael Roth 
3752d335818SDavid Gibson     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
376aab99135SBharata B Rao         drc->awaiting_allocation = true;
377aab99135SBharata B Rao     }
378aab99135SBharata B Rao 
379bbf5c878SMichael Roth     object_property_add_link(OBJECT(drc), "device",
380bbf5c878SMichael Roth                              object_get_typename(OBJECT(drc->dev)),
381bbf5c878SMichael Roth                              (Object **)(&drc->dev),
382bbf5c878SMichael Roth                              NULL, 0, NULL);
383bbf5c878SMichael Roth }
384bbf5c878SMichael Roth 
38531834723SDaniel Henrique Barboza static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
386bbf5c878SMichael Roth {
3870b55aa91SDavid Gibson     trace_spapr_drc_detach(spapr_drc_index(drc));
388bbf5c878SMichael Roth 
389f40eb921SMichael Roth     /* if we've signalled device presence to the guest, or if the guest
390f40eb921SMichael Roth      * has gone ahead and configured the device (via manually-executed
391f40eb921SMichael Roth      * device add via drmgr in guest, namely), we need to wait
392f40eb921SMichael Roth      * for the guest to quiesce the device before completing detach.
393f40eb921SMichael Roth      * Otherwise, we can assume the guest hasn't seen it and complete the
394f40eb921SMichael Roth      * detach immediately. Note that there is a small race window
395f40eb921SMichael Roth      * just before, or during, configuration, which is this context
396f40eb921SMichael Roth      * refers mainly to fetching the device tree via RTAS.
397f40eb921SMichael Roth      * During this window the device access will be arbitrated by
398f40eb921SMichael Roth      * associated DRC, which will simply fail the RTAS calls as invalid.
399f40eb921SMichael Roth      * This is recoverable within guest and current implementations of
400f40eb921SMichael Roth      * drmgr should be able to cope.
401f40eb921SMichael Roth      */
402f40eb921SMichael Roth     if (!drc->signalled && !drc->configured) {
403f40eb921SMichael Roth         /* if the guest hasn't seen the device we can't rely on it to
404f40eb921SMichael Roth          * set it back to an isolated state via RTAS, so do it here manually
405f40eb921SMichael Roth          */
406f40eb921SMichael Roth         drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
407f40eb921SMichael Roth     }
408f40eb921SMichael Roth 
409bbf5c878SMichael Roth     if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
4100b55aa91SDavid Gibson         trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
411bbf5c878SMichael Roth         drc->awaiting_release = true;
412bbf5c878SMichael Roth         return;
413bbf5c878SMichael Roth     }
414bbf5c878SMichael Roth 
4152d335818SDavid Gibson     if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
416bbf5c878SMichael Roth         drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
4170b55aa91SDavid Gibson         trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
418bbf5c878SMichael Roth         drc->awaiting_release = true;
419bbf5c878SMichael Roth         return;
420bbf5c878SMichael Roth     }
421bbf5c878SMichael Roth 
422aab99135SBharata B Rao     if (drc->awaiting_allocation) {
423fe6824d1SLaurent Vivier         if (!drc->awaiting_allocation_skippable) {
424aab99135SBharata B Rao             drc->awaiting_release = true;
4250b55aa91SDavid Gibson             trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
426aab99135SBharata B Rao             return;
427aab99135SBharata B Rao         }
428fe6824d1SLaurent Vivier     }
429aab99135SBharata B Rao 
430bbf5c878SMichael Roth     drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
431bbf5c878SMichael Roth 
4322d335818SDavid Gibson     /* Calling release callbacks based on spapr_drc_type(drc). */
4332d335818SDavid Gibson     switch (spapr_drc_type(drc)) {
43431834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_CPU:
43531834723SDaniel Henrique Barboza         spapr_core_release(drc->dev);
43631834723SDaniel Henrique Barboza         break;
43731834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PCI:
43831834723SDaniel Henrique Barboza         spapr_phb_remove_pci_device_cb(drc->dev);
43931834723SDaniel Henrique Barboza         break;
44031834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_LMB:
44131834723SDaniel Henrique Barboza         spapr_lmb_release(drc->dev);
44231834723SDaniel Henrique Barboza         break;
44331834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PHB:
44431834723SDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_VIO:
44531834723SDaniel Henrique Barboza     default:
44631834723SDaniel Henrique Barboza         g_assert(false);
447bbf5c878SMichael Roth     }
448bbf5c878SMichael Roth 
449bbf5c878SMichael Roth     drc->awaiting_release = false;
450fe6824d1SLaurent Vivier     drc->awaiting_allocation_skippable = false;
451bbf5c878SMichael Roth     g_free(drc->fdt);
452bbf5c878SMichael Roth     drc->fdt = NULL;
453bbf5c878SMichael Roth     drc->fdt_start_offset = 0;
454bbf5c878SMichael Roth     object_property_del(OBJECT(drc), "device", NULL);
455bbf5c878SMichael Roth     drc->dev = NULL;
456bbf5c878SMichael Roth }
457bbf5c878SMichael Roth 
458bbf5c878SMichael Roth static bool release_pending(sPAPRDRConnector *drc)
459bbf5c878SMichael Roth {
460bbf5c878SMichael Roth     return drc->awaiting_release;
461bbf5c878SMichael Roth }
462bbf5c878SMichael Roth 
463bbf5c878SMichael Roth static void reset(DeviceState *d)
464bbf5c878SMichael Roth {
465bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
466bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
467f40eb921SMichael Roth     sPAPRDREntitySense state;
468bbf5c878SMichael Roth 
4690b55aa91SDavid Gibson     trace_spapr_drc_reset(spapr_drc_index(drc));
470*b8fdd530SDavid Gibson 
471*b8fdd530SDavid Gibson     g_free(drc->ccs);
472*b8fdd530SDavid Gibson     drc->ccs = NULL;
473*b8fdd530SDavid Gibson 
474bbf5c878SMichael Roth     /* immediately upon reset we can safely assume DRCs whose devices
475bbf5c878SMichael Roth      * are pending removal can be safely removed, and that they will
476bbf5c878SMichael Roth      * subsequently be left in an ISOLATED state. move the DRC to this
477bbf5c878SMichael Roth      * state in these cases (which will in turn complete any pending
478bbf5c878SMichael Roth      * device removals)
479bbf5c878SMichael Roth      */
480bbf5c878SMichael Roth     if (drc->awaiting_release) {
481bbf5c878SMichael Roth         drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
482bbf5c878SMichael Roth         /* generally this should also finalize the removal, but if the device
483bbf5c878SMichael Roth          * hasn't yet been configured we normally defer removal under the
484bbf5c878SMichael Roth          * assumption that this transition is taking place as part of device
485bbf5c878SMichael Roth          * configuration. so check if we're still waiting after this, and
486bbf5c878SMichael Roth          * force removal if we are
487bbf5c878SMichael Roth          */
488bbf5c878SMichael Roth         if (drc->awaiting_release) {
48931834723SDaniel Henrique Barboza             drck->detach(drc, DEVICE(drc->dev), NULL);
490bbf5c878SMichael Roth         }
491bbf5c878SMichael Roth 
492bbf5c878SMichael Roth         /* non-PCI devices may be awaiting a transition to UNUSABLE */
4932d335818SDavid Gibson         if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
494bbf5c878SMichael Roth             drc->awaiting_release) {
495bbf5c878SMichael Roth             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
496bbf5c878SMichael Roth         }
497bbf5c878SMichael Roth     }
498f40eb921SMichael Roth 
499f40eb921SMichael Roth     drck->entity_sense(drc, &state);
500f40eb921SMichael Roth     if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
501f40eb921SMichael Roth         drck->set_signalled(drc);
502f40eb921SMichael Roth     }
503bbf5c878SMichael Roth }
504bbf5c878SMichael Roth 
505a50919ddSDaniel Henrique Barboza static bool spapr_drc_needed(void *opaque)
506a50919ddSDaniel Henrique Barboza {
507a50919ddSDaniel Henrique Barboza     sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
508a50919ddSDaniel Henrique Barboza     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
509a50919ddSDaniel Henrique Barboza     bool rc = false;
510a50919ddSDaniel Henrique Barboza     sPAPRDREntitySense value;
511a50919ddSDaniel Henrique Barboza     drck->entity_sense(drc, &value);
512a50919ddSDaniel Henrique Barboza 
513a50919ddSDaniel Henrique Barboza     /* If no dev is plugged in there is no need to migrate the DRC state */
514a50919ddSDaniel Henrique Barboza     if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
515a50919ddSDaniel Henrique Barboza         return false;
516a50919ddSDaniel Henrique Barboza     }
517a50919ddSDaniel Henrique Barboza 
518a50919ddSDaniel Henrique Barboza     /*
519a50919ddSDaniel Henrique Barboza      * If there is dev plugged in, we need to migrate the DRC state when
520a50919ddSDaniel Henrique Barboza      * it is different from cold-plugged state
521a50919ddSDaniel Henrique Barboza      */
5222d335818SDavid Gibson     switch (spapr_drc_type(drc)) {
523a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PCI:
524a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_CPU:
525a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_LMB:
526a32e900bSGreg Kurz         rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
527a32e900bSGreg Kurz                (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
528a50919ddSDaniel Henrique Barboza                drc->configured && drc->signalled && !drc->awaiting_release);
529a50919ddSDaniel Henrique Barboza         break;
530a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_PHB:
531a50919ddSDaniel Henrique Barboza     case SPAPR_DR_CONNECTOR_TYPE_VIO:
532a50919ddSDaniel Henrique Barboza     default:
533a32e900bSGreg Kurz         g_assert_not_reached();
534a50919ddSDaniel Henrique Barboza     }
535a50919ddSDaniel Henrique Barboza     return rc;
536a50919ddSDaniel Henrique Barboza }
537a50919ddSDaniel Henrique Barboza 
538a50919ddSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_drc = {
539a50919ddSDaniel Henrique Barboza     .name = "spapr_drc",
540a50919ddSDaniel Henrique Barboza     .version_id = 1,
541a50919ddSDaniel Henrique Barboza     .minimum_version_id = 1,
542a50919ddSDaniel Henrique Barboza     .needed = spapr_drc_needed,
543a50919ddSDaniel Henrique Barboza     .fields  = (VMStateField []) {
544a50919ddSDaniel Henrique Barboza         VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
545a50919ddSDaniel Henrique Barboza         VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
546a50919ddSDaniel Henrique Barboza         VMSTATE_UINT32(indicator_state, sPAPRDRConnector),
547a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(configured, sPAPRDRConnector),
548a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
549a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
550a50919ddSDaniel Henrique Barboza         VMSTATE_BOOL(signalled, sPAPRDRConnector),
551a50919ddSDaniel Henrique Barboza         VMSTATE_END_OF_LIST()
552a50919ddSDaniel Henrique Barboza     }
553a50919ddSDaniel Henrique Barboza };
554a50919ddSDaniel Henrique Barboza 
555bbf5c878SMichael Roth static void realize(DeviceState *d, Error **errp)
556bbf5c878SMichael Roth {
557bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
558bbf5c878SMichael Roth     Object *root_container;
559bbf5c878SMichael Roth     char link_name[256];
560bbf5c878SMichael Roth     gchar *child_name;
561bbf5c878SMichael Roth     Error *err = NULL;
562bbf5c878SMichael Roth 
5630b55aa91SDavid Gibson     trace_spapr_drc_realize(spapr_drc_index(drc));
564bbf5c878SMichael Roth     /* NOTE: we do this as part of realize/unrealize due to the fact
565bbf5c878SMichael Roth      * that the guest will communicate with the DRC via RTAS calls
566bbf5c878SMichael Roth      * referencing the global DRC index. By unlinking the DRC
567bbf5c878SMichael Roth      * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
568bbf5c878SMichael Roth      * inaccessible by the guest, since lookups rely on this path
569bbf5c878SMichael Roth      * existing in the composition tree
570bbf5c878SMichael Roth      */
571bbf5c878SMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
5720b55aa91SDavid Gibson     snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
573bbf5c878SMichael Roth     child_name = object_get_canonical_path_component(OBJECT(drc));
5740b55aa91SDavid Gibson     trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
575bbf5c878SMichael Roth     object_property_add_alias(root_container, link_name,
576bbf5c878SMichael Roth                               drc->owner, child_name, &err);
577bbf5c878SMichael Roth     if (err) {
5784fffeb5eSMarkus Armbruster         error_report_err(err);
579bbf5c878SMichael Roth         object_unref(OBJECT(drc));
580bbf5c878SMichael Roth     }
581586d2142SGonglei     g_free(child_name);
5820b55aa91SDavid Gibson     vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
583a50919ddSDaniel Henrique Barboza                      drc);
5840b55aa91SDavid Gibson     trace_spapr_drc_realize_complete(spapr_drc_index(drc));
585bbf5c878SMichael Roth }
586bbf5c878SMichael Roth 
587bbf5c878SMichael Roth static void unrealize(DeviceState *d, Error **errp)
588bbf5c878SMichael Roth {
589bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
590bbf5c878SMichael Roth     Object *root_container;
591bbf5c878SMichael Roth     char name[256];
592bbf5c878SMichael Roth     Error *err = NULL;
593bbf5c878SMichael Roth 
5940b55aa91SDavid Gibson     trace_spapr_drc_unrealize(spapr_drc_index(drc));
595bbf5c878SMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
5960b55aa91SDavid Gibson     snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
597bbf5c878SMichael Roth     object_property_del(root_container, name, &err);
598bbf5c878SMichael Roth     if (err) {
5994fffeb5eSMarkus Armbruster         error_report_err(err);
600bbf5c878SMichael Roth         object_unref(OBJECT(drc));
601bbf5c878SMichael Roth     }
602bbf5c878SMichael Roth }
603bbf5c878SMichael Roth 
6042d335818SDavid Gibson sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
605bbf5c878SMichael Roth                                          uint32_t id)
606bbf5c878SMichael Roth {
6072d335818SDavid Gibson     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
60894649d42SDavid Gibson     char *prop_name;
609bbf5c878SMichael Roth 
610bbf5c878SMichael Roth     drc->id = id;
611bbf5c878SMichael Roth     drc->owner = owner;
6120b55aa91SDavid Gibson     prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
6130b55aa91SDavid Gibson                                 spapr_drc_index(drc));
61494649d42SDavid Gibson     object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
615bbf5c878SMichael Roth     object_property_set_bool(OBJECT(drc), true, "realized", NULL);
61694649d42SDavid Gibson     g_free(prop_name);
617bbf5c878SMichael Roth 
618bbf5c878SMichael Roth     /* human-readable name for a DRC to encode into the DT
619bbf5c878SMichael Roth      * description. this is mainly only used within a guest in place
620bbf5c878SMichael Roth      * of the unique DRC index.
621bbf5c878SMichael Roth      *
622bbf5c878SMichael Roth      * in the case of VIO/PCI devices, it corresponds to a
623bbf5c878SMichael Roth      * "location code" that maps a logical device/function (DRC index)
624bbf5c878SMichael Roth      * to a physical (or virtual in the case of VIO) location in the
625bbf5c878SMichael Roth      * system by chaining together the "location label" for each
626bbf5c878SMichael Roth      * encapsulating component.
627bbf5c878SMichael Roth      *
628bbf5c878SMichael Roth      * since this is more to do with diagnosing physical hardware
629bbf5c878SMichael Roth      * issues than guest compatibility, we choose location codes/DRC
630bbf5c878SMichael Roth      * names that adhere to the documented format, but avoid encoding
631bbf5c878SMichael Roth      * the entire topology information into the label/code, instead
632bbf5c878SMichael Roth      * just using the location codes based on the labels for the
633bbf5c878SMichael Roth      * endpoints (VIO/PCI adaptor connectors), which is basically
634bbf5c878SMichael Roth      * just "C" followed by an integer ID.
635bbf5c878SMichael Roth      *
636bbf5c878SMichael Roth      * DRC names as documented by PAPR+ v2.7, 13.5.2.4
637bbf5c878SMichael Roth      * location codes as documented by PAPR+ v2.7, 12.3.1.5
638bbf5c878SMichael Roth      */
6392d335818SDavid Gibson     switch (spapr_drc_type(drc)) {
640bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_CPU:
641bbf5c878SMichael Roth         drc->name = g_strdup_printf("CPU %d", id);
642bbf5c878SMichael Roth         break;
643bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_PHB:
644bbf5c878SMichael Roth         drc->name = g_strdup_printf("PHB %d", id);
645bbf5c878SMichael Roth         break;
646bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_VIO:
647bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_PCI:
648bbf5c878SMichael Roth         drc->name = g_strdup_printf("C%d", id);
649bbf5c878SMichael Roth         break;
650bbf5c878SMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_LMB:
651bbf5c878SMichael Roth         drc->name = g_strdup_printf("LMB %d", id);
652bbf5c878SMichael Roth         break;
653bbf5c878SMichael Roth     default:
654bbf5c878SMichael Roth         g_assert(false);
655bbf5c878SMichael Roth     }
656bbf5c878SMichael Roth 
657bbf5c878SMichael Roth     /* PCI slot always start in a USABLE state, and stay there */
6582d335818SDavid Gibson     if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
659bbf5c878SMichael Roth         drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
660bbf5c878SMichael Roth     }
661bbf5c878SMichael Roth 
662bbf5c878SMichael Roth     return drc;
663bbf5c878SMichael Roth }
664bbf5c878SMichael Roth 
665bbf5c878SMichael Roth static void spapr_dr_connector_instance_init(Object *obj)
666bbf5c878SMichael Roth {
667bbf5c878SMichael Roth     sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
668bbf5c878SMichael Roth 
669bbf5c878SMichael Roth     object_property_add_uint32_ptr(obj, "isolation-state",
670bbf5c878SMichael Roth                                    &drc->isolation_state, NULL);
671bbf5c878SMichael Roth     object_property_add_uint32_ptr(obj, "indicator-state",
672bbf5c878SMichael Roth                                    &drc->indicator_state, NULL);
673bbf5c878SMichael Roth     object_property_add_uint32_ptr(obj, "allocation-state",
674bbf5c878SMichael Roth                                    &drc->allocation_state, NULL);
675bbf5c878SMichael Roth     object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
676bbf5c878SMichael Roth     object_property_add(obj, "index", "uint32", prop_get_index,
677bbf5c878SMichael Roth                         NULL, NULL, NULL, NULL);
678bbf5c878SMichael Roth     object_property_add(obj, "connector_type", "uint32", prop_get_type,
679bbf5c878SMichael Roth                         NULL, NULL, NULL, NULL);
680bbf5c878SMichael Roth     object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
681bbf5c878SMichael Roth     object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
682bbf5c878SMichael Roth                         NULL, NULL, NULL, NULL);
683bbf5c878SMichael Roth     object_property_add(obj, "fdt", "struct", prop_get_fdt,
684bbf5c878SMichael Roth                         NULL, NULL, NULL, NULL);
685bbf5c878SMichael Roth }
686bbf5c878SMichael Roth 
687bbf5c878SMichael Roth static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
688bbf5c878SMichael Roth {
689bbf5c878SMichael Roth     DeviceClass *dk = DEVICE_CLASS(k);
690bbf5c878SMichael Roth     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
691bbf5c878SMichael Roth 
692bbf5c878SMichael Roth     dk->reset = reset;
693bbf5c878SMichael Roth     dk->realize = realize;
694bbf5c878SMichael Roth     dk->unrealize = unrealize;
695bbf5c878SMichael Roth     drck->set_isolation_state = set_isolation_state;
696bbf5c878SMichael Roth     drck->set_indicator_state = set_indicator_state;
697bbf5c878SMichael Roth     drck->set_allocation_state = set_allocation_state;
698bbf5c878SMichael Roth     drck->get_name = get_name;
699bbf5c878SMichael Roth     drck->entity_sense = entity_sense;
700bbf5c878SMichael Roth     drck->attach = attach;
701bbf5c878SMichael Roth     drck->detach = detach;
702bbf5c878SMichael Roth     drck->release_pending = release_pending;
703f40eb921SMichael Roth     drck->set_signalled = set_signalled;
704c401ae8cSMarkus Armbruster     /*
705c401ae8cSMarkus Armbruster      * Reason: it crashes FIXME find and document the real reason
706c401ae8cSMarkus Armbruster      */
707e90f2a8cSEduardo Habkost     dk->user_creatable = false;
708bbf5c878SMichael Roth }
709bbf5c878SMichael Roth 
7102d335818SDavid Gibson static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
7112d335818SDavid Gibson {
7122d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
7132d335818SDavid Gibson 
7142d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
7152d335818SDavid Gibson }
7162d335818SDavid Gibson 
7172d335818SDavid Gibson static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
7182d335818SDavid Gibson {
7192d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
7202d335818SDavid Gibson 
7212d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
7222d335818SDavid Gibson }
7232d335818SDavid Gibson 
7242d335818SDavid Gibson static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
7252d335818SDavid Gibson {
7262d335818SDavid Gibson     sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
7272d335818SDavid Gibson 
7282d335818SDavid Gibson     drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
7292d335818SDavid Gibson }
7302d335818SDavid Gibson 
731bbf5c878SMichael Roth static const TypeInfo spapr_dr_connector_info = {
732bbf5c878SMichael Roth     .name          = TYPE_SPAPR_DR_CONNECTOR,
733bbf5c878SMichael Roth     .parent        = TYPE_DEVICE,
734bbf5c878SMichael Roth     .instance_size = sizeof(sPAPRDRConnector),
735bbf5c878SMichael Roth     .instance_init = spapr_dr_connector_instance_init,
736bbf5c878SMichael Roth     .class_size    = sizeof(sPAPRDRConnectorClass),
737bbf5c878SMichael Roth     .class_init    = spapr_dr_connector_class_init,
7382d335818SDavid Gibson     .abstract      = true,
7392d335818SDavid Gibson };
7402d335818SDavid Gibson 
7412d335818SDavid Gibson static const TypeInfo spapr_drc_physical_info = {
7422d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_PHYSICAL,
7432d335818SDavid Gibson     .parent        = TYPE_SPAPR_DR_CONNECTOR,
7442d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
7452d335818SDavid Gibson     .abstract      = true,
7462d335818SDavid Gibson };
7472d335818SDavid Gibson 
7482d335818SDavid Gibson static const TypeInfo spapr_drc_logical_info = {
7492d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_LOGICAL,
7502d335818SDavid Gibson     .parent        = TYPE_SPAPR_DR_CONNECTOR,
7512d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
7522d335818SDavid Gibson     .abstract      = true,
7532d335818SDavid Gibson };
7542d335818SDavid Gibson 
7552d335818SDavid Gibson static const TypeInfo spapr_drc_cpu_info = {
7562d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_CPU,
7572d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_LOGICAL,
7582d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
7592d335818SDavid Gibson     .class_init    = spapr_drc_cpu_class_init,
7602d335818SDavid Gibson };
7612d335818SDavid Gibson 
7622d335818SDavid Gibson static const TypeInfo spapr_drc_pci_info = {
7632d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_PCI,
7642d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_PHYSICAL,
7652d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
7662d335818SDavid Gibson     .class_init    = spapr_drc_pci_class_init,
7672d335818SDavid Gibson };
7682d335818SDavid Gibson 
7692d335818SDavid Gibson static const TypeInfo spapr_drc_lmb_info = {
7702d335818SDavid Gibson     .name          = TYPE_SPAPR_DRC_LMB,
7712d335818SDavid Gibson     .parent        = TYPE_SPAPR_DRC_LOGICAL,
7722d335818SDavid Gibson     .instance_size = sizeof(sPAPRDRConnector),
7732d335818SDavid Gibson     .class_init    = spapr_drc_lmb_class_init,
774bbf5c878SMichael Roth };
775bbf5c878SMichael Roth 
776bbf5c878SMichael Roth /* helper functions for external users */
777bbf5c878SMichael Roth 
778fbf55397SDavid Gibson sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
779bbf5c878SMichael Roth {
780bbf5c878SMichael Roth     Object *obj;
781bbf5c878SMichael Roth     char name[256];
782bbf5c878SMichael Roth 
783bbf5c878SMichael Roth     snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
784bbf5c878SMichael Roth     obj = object_resolve_path(name, NULL);
785bbf5c878SMichael Roth 
786bbf5c878SMichael Roth     return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
787bbf5c878SMichael Roth }
788bbf5c878SMichael Roth 
789fbf55397SDavid Gibson sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
790bbf5c878SMichael Roth {
791fbf55397SDavid Gibson     sPAPRDRConnectorClass *drck
792fbf55397SDavid Gibson         = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
793fbf55397SDavid Gibson 
794fbf55397SDavid Gibson     return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
795fbf55397SDavid Gibson                               | (id & DRC_INDEX_ID_MASK));
796bbf5c878SMichael Roth }
797e4b798bbSMichael Roth 
798e4b798bbSMichael Roth /* generate a string the describes the DRC to encode into the
799e4b798bbSMichael Roth  * device tree.
800e4b798bbSMichael Roth  *
801e4b798bbSMichael Roth  * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
802e4b798bbSMichael Roth  */
803e4b798bbSMichael Roth static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
804e4b798bbSMichael Roth {
805e4b798bbSMichael Roth     switch (type) {
806e4b798bbSMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_CPU:
807e4b798bbSMichael Roth         return "CPU";
808e4b798bbSMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_PHB:
809e4b798bbSMichael Roth         return "PHB";
810e4b798bbSMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_VIO:
811e4b798bbSMichael Roth         return "SLOT";
812e4b798bbSMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_PCI:
813e4b798bbSMichael Roth         return "28";
814e4b798bbSMichael Roth     case SPAPR_DR_CONNECTOR_TYPE_LMB:
815e4b798bbSMichael Roth         return "MEM";
816e4b798bbSMichael Roth     default:
817e4b798bbSMichael Roth         g_assert(false);
818e4b798bbSMichael Roth     }
819e4b798bbSMichael Roth 
820e4b798bbSMichael Roth     return NULL;
821e4b798bbSMichael Roth }
822e4b798bbSMichael Roth 
823e4b798bbSMichael Roth /**
824e4b798bbSMichael Roth  * spapr_drc_populate_dt
825e4b798bbSMichael Roth  *
826e4b798bbSMichael Roth  * @fdt: libfdt device tree
827e4b798bbSMichael Roth  * @path: path in the DT to generate properties
828e4b798bbSMichael Roth  * @owner: parent Object/DeviceState for which to generate DRC
829e4b798bbSMichael Roth  *         descriptions for
830e4b798bbSMichael Roth  * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
831e4b798bbSMichael Roth  *   to the types of DRCs to generate entries for
832e4b798bbSMichael Roth  *
833e4b798bbSMichael Roth  * generate OF properties to describe DRC topology/indices to guests
834e4b798bbSMichael Roth  *
835e4b798bbSMichael Roth  * as documented in PAPR+ v2.1, 13.5.2
836e4b798bbSMichael Roth  */
837e4b798bbSMichael Roth int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
838e4b798bbSMichael Roth                           uint32_t drc_type_mask)
839e4b798bbSMichael Roth {
840e4b798bbSMichael Roth     Object *root_container;
841e4b798bbSMichael Roth     ObjectProperty *prop;
8427746abd8SDaniel P. Berrange     ObjectPropertyIterator iter;
843e4b798bbSMichael Roth     uint32_t drc_count = 0;
844e4b798bbSMichael Roth     GArray *drc_indexes, *drc_power_domains;
845e4b798bbSMichael Roth     GString *drc_names, *drc_types;
846e4b798bbSMichael Roth     int ret;
847e4b798bbSMichael Roth 
848e4b798bbSMichael Roth     /* the first entry of each properties is a 32-bit integer encoding
849e4b798bbSMichael Roth      * the number of elements in the array. we won't know this until
850e4b798bbSMichael Roth      * we complete the iteration through all the matching DRCs, but
851e4b798bbSMichael Roth      * reserve the space now and set the offsets accordingly so we
852e4b798bbSMichael Roth      * can fill them in later.
853e4b798bbSMichael Roth      */
854e4b798bbSMichael Roth     drc_indexes = g_array_new(false, true, sizeof(uint32_t));
855e4b798bbSMichael Roth     drc_indexes = g_array_set_size(drc_indexes, 1);
856e4b798bbSMichael Roth     drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
857e4b798bbSMichael Roth     drc_power_domains = g_array_set_size(drc_power_domains, 1);
858e4b798bbSMichael Roth     drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
859e4b798bbSMichael Roth     drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
860e4b798bbSMichael Roth 
861e4b798bbSMichael Roth     /* aliases for all DRConnector objects will be rooted in QOM
862e4b798bbSMichael Roth      * composition tree at DRC_CONTAINER_PATH
863e4b798bbSMichael Roth      */
864e4b798bbSMichael Roth     root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
865e4b798bbSMichael Roth 
8667746abd8SDaniel P. Berrange     object_property_iter_init(&iter, root_container);
8677746abd8SDaniel P. Berrange     while ((prop = object_property_iter_next(&iter))) {
868e4b798bbSMichael Roth         Object *obj;
869e4b798bbSMichael Roth         sPAPRDRConnector *drc;
870e4b798bbSMichael Roth         sPAPRDRConnectorClass *drck;
871e4b798bbSMichael Roth         uint32_t drc_index, drc_power_domain;
872e4b798bbSMichael Roth 
873e4b798bbSMichael Roth         if (!strstart(prop->type, "link<", NULL)) {
874e4b798bbSMichael Roth             continue;
875e4b798bbSMichael Roth         }
876e4b798bbSMichael Roth 
877e4b798bbSMichael Roth         obj = object_property_get_link(root_container, prop->name, NULL);
878e4b798bbSMichael Roth         drc = SPAPR_DR_CONNECTOR(obj);
879e4b798bbSMichael Roth         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
880e4b798bbSMichael Roth 
881e4b798bbSMichael Roth         if (owner && (drc->owner != owner)) {
882e4b798bbSMichael Roth             continue;
883e4b798bbSMichael Roth         }
884e4b798bbSMichael Roth 
8852d335818SDavid Gibson         if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
886e4b798bbSMichael Roth             continue;
887e4b798bbSMichael Roth         }
888e4b798bbSMichael Roth 
889e4b798bbSMichael Roth         drc_count++;
890e4b798bbSMichael Roth 
891e4b798bbSMichael Roth         /* ibm,drc-indexes */
8920b55aa91SDavid Gibson         drc_index = cpu_to_be32(spapr_drc_index(drc));
893e4b798bbSMichael Roth         g_array_append_val(drc_indexes, drc_index);
894e4b798bbSMichael Roth 
895e4b798bbSMichael Roth         /* ibm,drc-power-domains */
896e4b798bbSMichael Roth         drc_power_domain = cpu_to_be32(-1);
897e4b798bbSMichael Roth         g_array_append_val(drc_power_domains, drc_power_domain);
898e4b798bbSMichael Roth 
899e4b798bbSMichael Roth         /* ibm,drc-names */
900e4b798bbSMichael Roth         drc_names = g_string_append(drc_names, drck->get_name(drc));
901e4b798bbSMichael Roth         drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
902e4b798bbSMichael Roth 
903e4b798bbSMichael Roth         /* ibm,drc-types */
904e4b798bbSMichael Roth         drc_types = g_string_append(drc_types,
9052d335818SDavid Gibson                                     spapr_drc_get_type_str(spapr_drc_type(drc)));
906e4b798bbSMichael Roth         drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
907e4b798bbSMichael Roth     }
908e4b798bbSMichael Roth 
909e4b798bbSMichael Roth     /* now write the drc count into the space we reserved at the
910e4b798bbSMichael Roth      * beginning of the arrays previously
911e4b798bbSMichael Roth      */
912e4b798bbSMichael Roth     *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
913e4b798bbSMichael Roth     *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
914e4b798bbSMichael Roth     *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
915e4b798bbSMichael Roth     *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
916e4b798bbSMichael Roth 
917e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
918e4b798bbSMichael Roth                       drc_indexes->data,
919e4b798bbSMichael Roth                       drc_indexes->len * sizeof(uint32_t));
920e4b798bbSMichael Roth     if (ret) {
921ce9863b7SCédric Le Goater         error_report("Couldn't create ibm,drc-indexes property");
922e4b798bbSMichael Roth         goto out;
923e4b798bbSMichael Roth     }
924e4b798bbSMichael Roth 
925e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
926e4b798bbSMichael Roth                       drc_power_domains->data,
927e4b798bbSMichael Roth                       drc_power_domains->len * sizeof(uint32_t));
928e4b798bbSMichael Roth     if (ret) {
929ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-power-domains property");
930e4b798bbSMichael Roth         goto out;
931e4b798bbSMichael Roth     }
932e4b798bbSMichael Roth 
933e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
934e4b798bbSMichael Roth                       drc_names->str, drc_names->len);
935e4b798bbSMichael Roth     if (ret) {
936ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-names property");
937e4b798bbSMichael Roth         goto out;
938e4b798bbSMichael Roth     }
939e4b798bbSMichael Roth 
940e4b798bbSMichael Roth     ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
941e4b798bbSMichael Roth                       drc_types->str, drc_types->len);
942e4b798bbSMichael Roth     if (ret) {
943ce9863b7SCédric Le Goater         error_report("Couldn't finalize ibm,drc-types property");
944e4b798bbSMichael Roth         goto out;
945e4b798bbSMichael Roth     }
946e4b798bbSMichael Roth 
947e4b798bbSMichael Roth out:
948e4b798bbSMichael Roth     g_array_free(drc_indexes, true);
949e4b798bbSMichael Roth     g_array_free(drc_power_domains, true);
950e4b798bbSMichael Roth     g_string_free(drc_names, true);
951e4b798bbSMichael Roth     g_string_free(drc_types, true);
952e4b798bbSMichael Roth 
953e4b798bbSMichael Roth     return ret;
954e4b798bbSMichael Roth }
955b89b3d39SDavid Gibson 
956b89b3d39SDavid Gibson /*
957b89b3d39SDavid Gibson  * RTAS calls
958b89b3d39SDavid Gibson  */
959b89b3d39SDavid Gibson 
960b89b3d39SDavid Gibson static bool sensor_type_is_dr(uint32_t sensor_type)
961b89b3d39SDavid Gibson {
962b89b3d39SDavid Gibson     switch (sensor_type) {
963b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
964b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_DR:
965b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
966b89b3d39SDavid Gibson         return true;
967b89b3d39SDavid Gibson     }
968b89b3d39SDavid Gibson 
969b89b3d39SDavid Gibson     return false;
970b89b3d39SDavid Gibson }
971b89b3d39SDavid Gibson 
972b89b3d39SDavid Gibson static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
973b89b3d39SDavid Gibson                                uint32_t token, uint32_t nargs,
974b89b3d39SDavid Gibson                                target_ulong args, uint32_t nret,
975b89b3d39SDavid Gibson                                target_ulong rets)
976b89b3d39SDavid Gibson {
977b89b3d39SDavid Gibson     uint32_t sensor_type;
978b89b3d39SDavid Gibson     uint32_t sensor_index;
979b89b3d39SDavid Gibson     uint32_t sensor_state;
980b89b3d39SDavid Gibson     uint32_t ret = RTAS_OUT_SUCCESS;
981b89b3d39SDavid Gibson     sPAPRDRConnector *drc;
982b89b3d39SDavid Gibson     sPAPRDRConnectorClass *drck;
983b89b3d39SDavid Gibson 
984b89b3d39SDavid Gibson     if (nargs != 3 || nret != 1) {
985b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
986b89b3d39SDavid Gibson         goto out;
987b89b3d39SDavid Gibson     }
988b89b3d39SDavid Gibson 
989b89b3d39SDavid Gibson     sensor_type = rtas_ld(args, 0);
990b89b3d39SDavid Gibson     sensor_index = rtas_ld(args, 1);
991b89b3d39SDavid Gibson     sensor_state = rtas_ld(args, 2);
992b89b3d39SDavid Gibson 
993b89b3d39SDavid Gibson     if (!sensor_type_is_dr(sensor_type)) {
994b89b3d39SDavid Gibson         goto out_unimplemented;
995b89b3d39SDavid Gibson     }
996b89b3d39SDavid Gibson 
997b89b3d39SDavid Gibson     /* if this is a DR sensor we can assume sensor_index == drc_index */
998fbf55397SDavid Gibson     drc = spapr_drc_by_index(sensor_index);
999b89b3d39SDavid Gibson     if (!drc) {
1000b89b3d39SDavid Gibson         trace_spapr_rtas_set_indicator_invalid(sensor_index);
1001b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
1002b89b3d39SDavid Gibson         goto out;
1003b89b3d39SDavid Gibson     }
1004b89b3d39SDavid Gibson     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1005b89b3d39SDavid Gibson 
1006b89b3d39SDavid Gibson     switch (sensor_type) {
1007b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1008b89b3d39SDavid Gibson         ret = drck->set_isolation_state(drc, sensor_state);
1009b89b3d39SDavid Gibson         break;
1010b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_DR:
1011b89b3d39SDavid Gibson         ret = drck->set_indicator_state(drc, sensor_state);
1012b89b3d39SDavid Gibson         break;
1013b89b3d39SDavid Gibson     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1014b89b3d39SDavid Gibson         ret = drck->set_allocation_state(drc, sensor_state);
1015b89b3d39SDavid Gibson         break;
1016b89b3d39SDavid Gibson     default:
1017b89b3d39SDavid Gibson         goto out_unimplemented;
1018b89b3d39SDavid Gibson     }
1019b89b3d39SDavid Gibson 
1020b89b3d39SDavid Gibson out:
1021b89b3d39SDavid Gibson     rtas_st(rets, 0, ret);
1022b89b3d39SDavid Gibson     return;
1023b89b3d39SDavid Gibson 
1024b89b3d39SDavid Gibson out_unimplemented:
1025b89b3d39SDavid Gibson     /* currently only DR-related sensors are implemented */
1026b89b3d39SDavid Gibson     trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
1027b89b3d39SDavid Gibson     rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
1028b89b3d39SDavid Gibson }
1029b89b3d39SDavid Gibson 
1030b89b3d39SDavid Gibson static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
1031b89b3d39SDavid Gibson                                   uint32_t token, uint32_t nargs,
1032b89b3d39SDavid Gibson                                   target_ulong args, uint32_t nret,
1033b89b3d39SDavid Gibson                                   target_ulong rets)
1034b89b3d39SDavid Gibson {
1035b89b3d39SDavid Gibson     uint32_t sensor_type;
1036b89b3d39SDavid Gibson     uint32_t sensor_index;
1037b89b3d39SDavid Gibson     uint32_t sensor_state = 0;
1038b89b3d39SDavid Gibson     sPAPRDRConnector *drc;
1039b89b3d39SDavid Gibson     sPAPRDRConnectorClass *drck;
1040b89b3d39SDavid Gibson     uint32_t ret = RTAS_OUT_SUCCESS;
1041b89b3d39SDavid Gibson 
1042b89b3d39SDavid Gibson     if (nargs != 2 || nret != 2) {
1043b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
1044b89b3d39SDavid Gibson         goto out;
1045b89b3d39SDavid Gibson     }
1046b89b3d39SDavid Gibson 
1047b89b3d39SDavid Gibson     sensor_type = rtas_ld(args, 0);
1048b89b3d39SDavid Gibson     sensor_index = rtas_ld(args, 1);
1049b89b3d39SDavid Gibson 
1050b89b3d39SDavid Gibson     if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1051b89b3d39SDavid Gibson         /* currently only DR-related sensors are implemented */
1052b89b3d39SDavid Gibson         trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1053b89b3d39SDavid Gibson                                                         sensor_type);
1054b89b3d39SDavid Gibson         ret = RTAS_OUT_NOT_SUPPORTED;
1055b89b3d39SDavid Gibson         goto out;
1056b89b3d39SDavid Gibson     }
1057b89b3d39SDavid Gibson 
1058fbf55397SDavid Gibson     drc = spapr_drc_by_index(sensor_index);
1059b89b3d39SDavid Gibson     if (!drc) {
1060b89b3d39SDavid Gibson         trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1061b89b3d39SDavid Gibson         ret = RTAS_OUT_PARAM_ERROR;
1062b89b3d39SDavid Gibson         goto out;
1063b89b3d39SDavid Gibson     }
1064b89b3d39SDavid Gibson     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1065b89b3d39SDavid Gibson     ret = drck->entity_sense(drc, &sensor_state);
1066b89b3d39SDavid Gibson 
1067b89b3d39SDavid Gibson out:
1068b89b3d39SDavid Gibson     rtas_st(rets, 0, ret);
1069b89b3d39SDavid Gibson     rtas_st(rets, 1, sensor_state);
1070b89b3d39SDavid Gibson }
1071b89b3d39SDavid Gibson 
1072b89b3d39SDavid Gibson /* configure-connector work area offsets, int32_t units for field
1073b89b3d39SDavid Gibson  * indexes, bytes for field offset/len values.
1074b89b3d39SDavid Gibson  *
1075b89b3d39SDavid Gibson  * as documented by PAPR+ v2.7, 13.5.3.5
1076b89b3d39SDavid Gibson  */
1077b89b3d39SDavid Gibson #define CC_IDX_NODE_NAME_OFFSET 2
1078b89b3d39SDavid Gibson #define CC_IDX_PROP_NAME_OFFSET 2
1079b89b3d39SDavid Gibson #define CC_IDX_PROP_LEN 3
1080b89b3d39SDavid Gibson #define CC_IDX_PROP_DATA_OFFSET 4
1081b89b3d39SDavid Gibson #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1082b89b3d39SDavid Gibson #define CC_WA_LEN 4096
1083b89b3d39SDavid Gibson 
1084b89b3d39SDavid Gibson static void configure_connector_st(target_ulong addr, target_ulong offset,
1085b89b3d39SDavid Gibson                                    const void *buf, size_t len)
1086b89b3d39SDavid Gibson {
1087b89b3d39SDavid Gibson     cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1088b89b3d39SDavid Gibson                               buf, MIN(len, CC_WA_LEN - offset));
1089b89b3d39SDavid Gibson }
1090b89b3d39SDavid Gibson 
1091b89b3d39SDavid Gibson static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1092b89b3d39SDavid Gibson                                          sPAPRMachineState *spapr,
1093b89b3d39SDavid Gibson                                          uint32_t token, uint32_t nargs,
1094b89b3d39SDavid Gibson                                          target_ulong args, uint32_t nret,
1095b89b3d39SDavid Gibson                                          target_ulong rets)
1096b89b3d39SDavid Gibson {
1097b89b3d39SDavid Gibson     uint64_t wa_addr;
1098b89b3d39SDavid Gibson     uint64_t wa_offset;
1099b89b3d39SDavid Gibson     uint32_t drc_index;
1100b89b3d39SDavid Gibson     sPAPRDRConnector *drc;
1101b89b3d39SDavid Gibson     sPAPRConfigureConnectorState *ccs;
1102b89b3d39SDavid Gibson     sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1103b89b3d39SDavid Gibson     int rc;
1104b89b3d39SDavid Gibson 
1105b89b3d39SDavid Gibson     if (nargs != 2 || nret != 1) {
1106b89b3d39SDavid Gibson         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1107b89b3d39SDavid Gibson         return;
1108b89b3d39SDavid Gibson     }
1109b89b3d39SDavid Gibson 
1110b89b3d39SDavid Gibson     wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1111b89b3d39SDavid Gibson 
1112b89b3d39SDavid Gibson     drc_index = rtas_ld(wa_addr, 0);
1113fbf55397SDavid Gibson     drc = spapr_drc_by_index(drc_index);
1114b89b3d39SDavid Gibson     if (!drc) {
1115b89b3d39SDavid Gibson         trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1116b89b3d39SDavid Gibson         rc = RTAS_OUT_PARAM_ERROR;
1117b89b3d39SDavid Gibson         goto out;
1118b89b3d39SDavid Gibson     }
1119b89b3d39SDavid Gibson 
112088af6ea5SDavid Gibson     if (!drc->fdt) {
1121b89b3d39SDavid Gibson         trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1122b89b3d39SDavid Gibson         rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1123b89b3d39SDavid Gibson         goto out;
1124b89b3d39SDavid Gibson     }
1125b89b3d39SDavid Gibson 
1126*b8fdd530SDavid Gibson     ccs = drc->ccs;
1127b89b3d39SDavid Gibson     if (!ccs) {
1128b89b3d39SDavid Gibson         ccs = g_new0(sPAPRConfigureConnectorState, 1);
112988af6ea5SDavid Gibson         ccs->fdt_offset = drc->fdt_start_offset;
1130*b8fdd530SDavid Gibson         drc->ccs = ccs;
1131b89b3d39SDavid Gibson     }
1132b89b3d39SDavid Gibson 
1133b89b3d39SDavid Gibson     do {
1134b89b3d39SDavid Gibson         uint32_t tag;
1135b89b3d39SDavid Gibson         const char *name;
1136b89b3d39SDavid Gibson         const struct fdt_property *prop;
1137b89b3d39SDavid Gibson         int fdt_offset_next, prop_len;
1138b89b3d39SDavid Gibson 
113988af6ea5SDavid Gibson         tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
1140b89b3d39SDavid Gibson 
1141b89b3d39SDavid Gibson         switch (tag) {
1142b89b3d39SDavid Gibson         case FDT_BEGIN_NODE:
1143b89b3d39SDavid Gibson             ccs->fdt_depth++;
114488af6ea5SDavid Gibson             name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
1145b89b3d39SDavid Gibson 
1146b89b3d39SDavid Gibson             /* provide the name of the next OF node */
1147b89b3d39SDavid Gibson             wa_offset = CC_VAL_DATA_OFFSET;
1148b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1149b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1150b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1151b89b3d39SDavid Gibson             break;
1152b89b3d39SDavid Gibson         case FDT_END_NODE:
1153b89b3d39SDavid Gibson             ccs->fdt_depth--;
1154b89b3d39SDavid Gibson             if (ccs->fdt_depth == 0) {
11554f65ce00SDavid Gibson                 sPAPRDRIsolationState state = drc->isolation_state;
11560b55aa91SDavid Gibson                 uint32_t drc_index = spapr_drc_index(drc);
1157b89b3d39SDavid Gibson                 /* done sending the device tree, don't need to track
1158b89b3d39SDavid Gibson                  * the state anymore
1159b89b3d39SDavid Gibson                  */
11600b55aa91SDavid Gibson                 trace_spapr_drc_set_configured(drc_index);
11614f65ce00SDavid Gibson                 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
11624f65ce00SDavid Gibson                     drc->configured = true;
11634f65ce00SDavid Gibson                 } else {
11644f65ce00SDavid Gibson                     /* guest should be not configuring an isolated device */
11650b55aa91SDavid Gibson                     trace_spapr_drc_set_configured_skipping(drc_index);
11664f65ce00SDavid Gibson                 }
1167*b8fdd530SDavid Gibson                 g_free(ccs);
1168*b8fdd530SDavid Gibson                 drc->ccs = NULL;
1169b89b3d39SDavid Gibson                 ccs = NULL;
1170b89b3d39SDavid Gibson                 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1171b89b3d39SDavid Gibson             } else {
1172b89b3d39SDavid Gibson                 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1173b89b3d39SDavid Gibson             }
1174b89b3d39SDavid Gibson             break;
1175b89b3d39SDavid Gibson         case FDT_PROP:
117688af6ea5SDavid Gibson             prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1177b89b3d39SDavid Gibson                                               &prop_len);
117888af6ea5SDavid Gibson             name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1179b89b3d39SDavid Gibson 
1180b89b3d39SDavid Gibson             /* provide the name of the next OF property */
1181b89b3d39SDavid Gibson             wa_offset = CC_VAL_DATA_OFFSET;
1182b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1183b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1184b89b3d39SDavid Gibson 
1185b89b3d39SDavid Gibson             /* provide the length and value of the OF property. data gets
1186b89b3d39SDavid Gibson              * placed immediately after NULL terminator of the OF property's
1187b89b3d39SDavid Gibson              * name string
1188b89b3d39SDavid Gibson              */
1189b89b3d39SDavid Gibson             wa_offset += strlen(name) + 1,
1190b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1191b89b3d39SDavid Gibson             rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1192b89b3d39SDavid Gibson             configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1193b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1194b89b3d39SDavid Gibson             break;
1195b89b3d39SDavid Gibson         case FDT_END:
1196b89b3d39SDavid Gibson             resp = SPAPR_DR_CC_RESPONSE_ERROR;
1197b89b3d39SDavid Gibson         default:
1198b89b3d39SDavid Gibson             /* keep seeking for an actionable tag */
1199b89b3d39SDavid Gibson             break;
1200b89b3d39SDavid Gibson         }
1201b89b3d39SDavid Gibson         if (ccs) {
1202b89b3d39SDavid Gibson             ccs->fdt_offset = fdt_offset_next;
1203b89b3d39SDavid Gibson         }
1204b89b3d39SDavid Gibson     } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1205b89b3d39SDavid Gibson 
1206b89b3d39SDavid Gibson     rc = resp;
1207b89b3d39SDavid Gibson out:
1208b89b3d39SDavid Gibson     rtas_st(rets, 0, rc);
1209b89b3d39SDavid Gibson }
1210b89b3d39SDavid Gibson 
1211b89b3d39SDavid Gibson static void spapr_drc_register_types(void)
1212b89b3d39SDavid Gibson {
1213b89b3d39SDavid Gibson     type_register_static(&spapr_dr_connector_info);
12142d335818SDavid Gibson     type_register_static(&spapr_drc_physical_info);
12152d335818SDavid Gibson     type_register_static(&spapr_drc_logical_info);
12162d335818SDavid Gibson     type_register_static(&spapr_drc_cpu_info);
12172d335818SDavid Gibson     type_register_static(&spapr_drc_pci_info);
12182d335818SDavid Gibson     type_register_static(&spapr_drc_lmb_info);
1219b89b3d39SDavid Gibson 
1220b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1221b89b3d39SDavid Gibson                         rtas_set_indicator);
1222b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1223b89b3d39SDavid Gibson                         rtas_get_sensor_state);
1224b89b3d39SDavid Gibson     spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1225b89b3d39SDavid Gibson                         rtas_ibm_configure_connector);
1226b89b3d39SDavid Gibson }
1227b89b3d39SDavid Gibson type_init(spapr_drc_register_types)
1228