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