xref: /openbmc/qemu/include/hw/ppc/spapr_drc.h (revision 6598f0cd)
1 /*
2  * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
3  *
4  * Copyright IBM Corp. 2014
5  *
6  * Authors:
7  *  Michael Roth      <mdroth@linux.vnet.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #ifndef HW_SPAPR_DRC_H
14 #define HW_SPAPR_DRC_H
15 
16 #include <libfdt.h>
17 #include "qapi/qapi-types-run-state.h"
18 #include "qom/object.h"
19 #include "sysemu/sysemu.h"
20 #include "hw/qdev.h"
21 
22 #define TYPE_SPAPR_DR_CONNECTOR "spapr-dr-connector"
23 #define SPAPR_DR_CONNECTOR_GET_CLASS(obj) \
24         OBJECT_GET_CLASS(sPAPRDRConnectorClass, obj, TYPE_SPAPR_DR_CONNECTOR)
25 #define SPAPR_DR_CONNECTOR_CLASS(klass) \
26         OBJECT_CLASS_CHECK(sPAPRDRConnectorClass, klass, \
27                            TYPE_SPAPR_DR_CONNECTOR)
28 #define SPAPR_DR_CONNECTOR(obj) OBJECT_CHECK(sPAPRDRConnector, (obj), \
29                                              TYPE_SPAPR_DR_CONNECTOR)
30 
31 #define TYPE_SPAPR_DRC_PHYSICAL "spapr-drc-physical"
32 #define SPAPR_DRC_PHYSICAL_GET_CLASS(obj) \
33         OBJECT_GET_CLASS(sPAPRDRConnectorClass, obj, TYPE_SPAPR_DRC_PHYSICAL)
34 #define SPAPR_DRC_PHYSICAL_CLASS(klass) \
35         OBJECT_CLASS_CHECK(sPAPRDRConnectorClass, klass, \
36                            TYPE_SPAPR_DRC_PHYSICAL)
37 #define SPAPR_DRC_PHYSICAL(obj) OBJECT_CHECK(sPAPRDRCPhysical, (obj), \
38                                              TYPE_SPAPR_DRC_PHYSICAL)
39 
40 #define TYPE_SPAPR_DRC_LOGICAL "spapr-drc-logical"
41 #define SPAPR_DRC_LOGICAL_GET_CLASS(obj) \
42         OBJECT_GET_CLASS(sPAPRDRConnectorClass, obj, TYPE_SPAPR_DRC_LOGICAL)
43 #define SPAPR_DRC_LOGICAL_CLASS(klass) \
44         OBJECT_CLASS_CHECK(sPAPRDRConnectorClass, klass, \
45                            TYPE_SPAPR_DRC_LOGICAL)
46 #define SPAPR_DRC_LOGICAL(obj) OBJECT_CHECK(sPAPRDRConnector, (obj), \
47                                              TYPE_SPAPR_DRC_LOGICAL)
48 
49 #define TYPE_SPAPR_DRC_CPU "spapr-drc-cpu"
50 #define SPAPR_DRC_CPU_GET_CLASS(obj) \
51         OBJECT_GET_CLASS(sPAPRDRConnectorClass, obj, TYPE_SPAPR_DRC_CPU)
52 #define SPAPR_DRC_CPU_CLASS(klass) \
53         OBJECT_CLASS_CHECK(sPAPRDRConnectorClass, klass, TYPE_SPAPR_DRC_CPU)
54 #define SPAPR_DRC_CPU(obj) OBJECT_CHECK(sPAPRDRConnector, (obj), \
55                                         TYPE_SPAPR_DRC_CPU)
56 
57 #define TYPE_SPAPR_DRC_PCI "spapr-drc-pci"
58 #define SPAPR_DRC_PCI_GET_CLASS(obj) \
59         OBJECT_GET_CLASS(sPAPRDRConnectorClass, obj, TYPE_SPAPR_DRC_PCI)
60 #define SPAPR_DRC_PCI_CLASS(klass) \
61         OBJECT_CLASS_CHECK(sPAPRDRConnectorClass, klass, TYPE_SPAPR_DRC_PCI)
62 #define SPAPR_DRC_PCI(obj) OBJECT_CHECK(sPAPRDRConnector, (obj), \
63                                         TYPE_SPAPR_DRC_PCI)
64 
65 #define TYPE_SPAPR_DRC_LMB "spapr-drc-lmb"
66 #define SPAPR_DRC_LMB_GET_CLASS(obj) \
67         OBJECT_GET_CLASS(sPAPRDRConnectorClass, obj, TYPE_SPAPR_DRC_LMB)
68 #define SPAPR_DRC_LMB_CLASS(klass) \
69         OBJECT_CLASS_CHECK(sPAPRDRConnectorClass, klass, TYPE_SPAPR_DRC_LMB)
70 #define SPAPR_DRC_LMB(obj) OBJECT_CHECK(sPAPRDRConnector, (obj), \
71                                         TYPE_SPAPR_DRC_LMB)
72 
73 /*
74  * Various hotplug types managed by sPAPRDRConnector
75  *
76  * these are somewhat arbitrary, but to make things easier
77  * when generating DRC indexes later we've aligned the bit
78  * positions with the values used to assign DRC indexes on
79  * pSeries. we use those values as bit shifts to allow for
80  * the OR'ing of these values in various QEMU routines, but
81  * for values exposed to the guest (via DRC indexes for
82  * instance) we will use the shift amounts.
83  */
84 typedef enum {
85     SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU = 1,
86     SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB = 2,
87     SPAPR_DR_CONNECTOR_TYPE_SHIFT_VIO = 3,
88     SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI = 4,
89     SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB = 8,
90 } sPAPRDRConnectorTypeShift;
91 
92 typedef enum {
93     SPAPR_DR_CONNECTOR_TYPE_ANY = ~0,
94     SPAPR_DR_CONNECTOR_TYPE_CPU = 1 << SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU,
95     SPAPR_DR_CONNECTOR_TYPE_PHB = 1 << SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB,
96     SPAPR_DR_CONNECTOR_TYPE_VIO = 1 << SPAPR_DR_CONNECTOR_TYPE_SHIFT_VIO,
97     SPAPR_DR_CONNECTOR_TYPE_PCI = 1 << SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI,
98     SPAPR_DR_CONNECTOR_TYPE_LMB = 1 << SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB,
99 } sPAPRDRConnectorType;
100 
101 /*
102  * set via set-indicator RTAS calls
103  * as documented by PAPR+ 2.7 13.5.3.4, Table 177
104  *
105  * isolated: put device under firmware control
106  * unisolated: claim OS control of device (may or may not be in use)
107  */
108 typedef enum {
109     SPAPR_DR_ISOLATION_STATE_ISOLATED   = 0,
110     SPAPR_DR_ISOLATION_STATE_UNISOLATED = 1
111 } sPAPRDRIsolationState;
112 
113 /*
114  * set via set-indicator RTAS calls
115  * as documented by PAPR+ 2.7 13.5.3.4, Table 177
116  *
117  * unusable: mark device as unavailable to OS
118  * usable: mark device as available to OS
119  * exchange: (currently unused)
120  * recover: (currently unused)
121  */
122 typedef enum {
123     SPAPR_DR_ALLOCATION_STATE_UNUSABLE  = 0,
124     SPAPR_DR_ALLOCATION_STATE_USABLE    = 1,
125     SPAPR_DR_ALLOCATION_STATE_EXCHANGE  = 2,
126     SPAPR_DR_ALLOCATION_STATE_RECOVER   = 3
127 } sPAPRDRAllocationState;
128 
129 /*
130  * DR-indicator (LED/visual indicator)
131  *
132  * set via set-indicator RTAS calls
133  * as documented by PAPR+ 2.7 13.5.3.4, Table 177,
134  * and PAPR+ 2.7 13.5.4.1, Table 180
135  *
136  * inactive: hotpluggable entity inactive and safely removable
137  * active: hotpluggable entity in use and not safely removable
138  * identify: (currently unused)
139  * action: (currently unused)
140  */
141 typedef enum {
142     SPAPR_DR_INDICATOR_INACTIVE   = 0,
143     SPAPR_DR_INDICATOR_ACTIVE     = 1,
144     SPAPR_DR_INDICATOR_IDENTIFY   = 2,
145     SPAPR_DR_INDICATOR_ACTION     = 3,
146 } sPAPRDRIndicatorState;
147 
148 /*
149  * returned via get-sensor-state RTAS calls
150  * as documented by PAPR+ 2.7 13.5.3.3, Table 175:
151  *
152  * empty: connector slot empty (e.g. empty hotpluggable PCI slot)
153  * present: connector slot populated and device available to OS
154  * unusable: device not currently available to OS
155  * exchange: (currently unused)
156  * recover: (currently unused)
157  */
158 typedef enum {
159     SPAPR_DR_ENTITY_SENSE_EMPTY     = 0,
160     SPAPR_DR_ENTITY_SENSE_PRESENT   = 1,
161     SPAPR_DR_ENTITY_SENSE_UNUSABLE  = 2,
162     SPAPR_DR_ENTITY_SENSE_EXCHANGE  = 3,
163     SPAPR_DR_ENTITY_SENSE_RECOVER   = 4,
164 } sPAPRDREntitySense;
165 
166 typedef enum {
167     SPAPR_DR_CC_RESPONSE_NEXT_SIB         = 1, /* currently unused */
168     SPAPR_DR_CC_RESPONSE_NEXT_CHILD       = 2,
169     SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY    = 3,
170     SPAPR_DR_CC_RESPONSE_PREV_PARENT      = 4,
171     SPAPR_DR_CC_RESPONSE_SUCCESS          = 0,
172     SPAPR_DR_CC_RESPONSE_ERROR            = -1,
173     SPAPR_DR_CC_RESPONSE_CONTINUE         = -2,
174     SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE = -9003,
175 } sPAPRDRCCResponse;
176 
177 typedef enum {
178     /*
179      * Values come from Fig. 12 in LoPAPR section 13.4
180      *
181      * These are exposed in the migration stream, so don't change
182      * them.
183      */
184     SPAPR_DRC_STATE_INVALID             = 0,
185     SPAPR_DRC_STATE_LOGICAL_UNUSABLE    = 1,
186     SPAPR_DRC_STATE_LOGICAL_AVAILABLE   = 2,
187     SPAPR_DRC_STATE_LOGICAL_UNISOLATE   = 3,
188     SPAPR_DRC_STATE_LOGICAL_CONFIGURED  = 4,
189     SPAPR_DRC_STATE_PHYSICAL_AVAILABLE  = 5,
190     SPAPR_DRC_STATE_PHYSICAL_POWERON    = 6,
191     SPAPR_DRC_STATE_PHYSICAL_UNISOLATE  = 7,
192     SPAPR_DRC_STATE_PHYSICAL_CONFIGURED = 8,
193 } sPAPRDRCState;
194 
195 typedef struct sPAPRDRConnector {
196     /*< private >*/
197     DeviceState parent;
198 
199     uint32_t id;
200     Object *owner;
201 
202     uint32_t state;
203 
204     /* RTAS ibm,configure-connector state */
205     /* (only valid in UNISOLATE state) */
206     int ccs_offset;
207     int ccs_depth;
208 
209     /* device pointer, via link property */
210     DeviceState *dev;
211     bool unplug_requested;
212     void *fdt;
213     int fdt_start_offset;
214 } sPAPRDRConnector;
215 
216 typedef struct sPAPRDRConnectorClass {
217     /*< private >*/
218     DeviceClass parent;
219     sPAPRDRCState empty_state;
220     sPAPRDRCState ready_state;
221 
222     /*< public >*/
223     sPAPRDRConnectorTypeShift typeshift;
224     const char *typename; /* used in device tree, PAPR 13.5.2.6 & C.6.1 */
225     const char *drc_name_prefix; /* used other places in device tree */
226 
227     sPAPRDREntitySense (*dr_entity_sense)(sPAPRDRConnector *drc);
228     uint32_t (*isolate)(sPAPRDRConnector *drc);
229     uint32_t (*unisolate)(sPAPRDRConnector *drc);
230     void (*release)(DeviceState *dev);
231 } sPAPRDRConnectorClass;
232 
233 typedef struct sPAPRDRCPhysical {
234     /*< private >*/
235     sPAPRDRConnector parent;
236 
237     /* DR-indicator */
238     uint32_t dr_indicator;
239 } sPAPRDRCPhysical;
240 
241 static inline bool spapr_drc_hotplugged(DeviceState *dev)
242 {
243     return dev->hotplugged && !runstate_check(RUN_STATE_INMIGRATE);
244 }
245 
246 void spapr_drc_reset(sPAPRDRConnector *drc);
247 
248 uint32_t spapr_drc_index(sPAPRDRConnector *drc);
249 sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc);
250 
251 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
252                                          uint32_t id);
253 sPAPRDRConnector *spapr_drc_by_index(uint32_t index);
254 sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id);
255 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
256                           uint32_t drc_type_mask);
257 
258 void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
259                       int fdt_start_offset, Error **errp);
260 void spapr_drc_detach(sPAPRDRConnector *drc);
261 bool spapr_drc_needed(void *opaque);
262 
263 static inline bool spapr_drc_unplug_requested(sPAPRDRConnector *drc)
264 {
265     return drc->unplug_requested;
266 }
267 
268 #endif /* HW_SPAPR_DRC_H */
269