]>
Commit | Line | Data |
---|---|---|
bbf5c878 MR |
1 | /* |
2 | * QEMU SPAPR Dynamic Reconfiguration Connector Implementation | |
3 | * | |
4 | * Copyright IBM Corp. 2014 | |
5 | * | |
6 | * Authors: | |
7 | * Michael Roth <[email protected]> | |
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 | ||
0d75590d | 13 | #include "qemu/osdep.h" |
da34e65c | 14 | #include "qapi/error.h" |
15280c36 | 15 | #include "qapi/qmp/qnull.h" |
4771d756 | 16 | #include "cpu.h" |
f348b6d1 | 17 | #include "qemu/cutils.h" |
bbf5c878 MR |
18 | #include "hw/ppc/spapr_drc.h" |
19 | #include "qom/object.h" | |
20 | #include "hw/qdev.h" | |
21 | #include "qapi/visitor.h" | |
22 | #include "qemu/error-report.h" | |
0cb688d2 | 23 | #include "hw/ppc/spapr.h" /* for RTAS return codes */ |
31834723 | 24 | #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */ |
24ac7755 | 25 | #include "trace.h" |
bbf5c878 MR |
26 | |
27 | #define DRC_CONTAINER_PATH "/dr-connector" | |
28 | #define DRC_INDEX_TYPE_SHIFT 28 | |
627c2ef7 | 29 | #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1) |
bbf5c878 | 30 | |
2d335818 DG |
31 | sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc) |
32 | { | |
33 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
34 | ||
35 | return 1 << drck->typeshift; | |
36 | } | |
37 | ||
0b55aa91 | 38 | uint32_t spapr_drc_index(sPAPRDRConnector *drc) |
bbf5c878 | 39 | { |
2d335818 DG |
40 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); |
41 | ||
bbf5c878 MR |
42 | /* no set format for a drc index: it only needs to be globally |
43 | * unique. this is how we encode the DRC type on bare-metal | |
44 | * however, so might as well do that here | |
45 | */ | |
2d335818 DG |
46 | return (drck->typeshift << DRC_INDEX_TYPE_SHIFT) |
47 | | (drc->id & DRC_INDEX_ID_MASK); | |
bbf5c878 MR |
48 | } |
49 | ||
0dfabd39 | 50 | static uint32_t drc_isolate_physical(sPAPRDRConnector *drc) |
bbf5c878 | 51 | { |
9d4c0f4f DG |
52 | switch (drc->state) { |
53 | case SPAPR_DRC_STATE_PHYSICAL_POWERON: | |
54 | return RTAS_OUT_SUCCESS; /* Nothing to do */ | |
55 | case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED: | |
56 | break; /* see below */ | |
57 | case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE: | |
58 | return RTAS_OUT_PARAM_ERROR; /* not allowed */ | |
59 | default: | |
60 | g_assert_not_reached(); | |
61 | } | |
62 | ||
9d4c0f4f | 63 | drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON; |
0dfabd39 | 64 | |
f1c52354 | 65 | if (drc->unplug_requested) { |
0dfabd39 | 66 | uint32_t drc_index = spapr_drc_index(drc); |
9d4c0f4f DG |
67 | trace_spapr_drc_set_isolation_state_finalizing(drc_index); |
68 | spapr_drc_detach(drc); | |
9d1852ce | 69 | } |
0dfabd39 DG |
70 | |
71 | return RTAS_OUT_SUCCESS; | |
72 | } | |
73 | ||
74 | static uint32_t drc_unisolate_physical(sPAPRDRConnector *drc) | |
75 | { | |
9d4c0f4f DG |
76 | switch (drc->state) { |
77 | case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE: | |
78 | case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED: | |
79 | return RTAS_OUT_SUCCESS; /* Nothing to do */ | |
80 | case SPAPR_DRC_STATE_PHYSICAL_POWERON: | |
81 | break; /* see below */ | |
82 | default: | |
83 | g_assert_not_reached(); | |
84 | } | |
85 | ||
0dfabd39 DG |
86 | /* cannot unisolate a non-existent resource, and, or resources |
87 | * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, | |
88 | * 13.5.3.5) | |
89 | */ | |
90 | if (!drc->dev) { | |
91 | return RTAS_OUT_NO_SUCH_INDICATOR; | |
92 | } | |
93 | ||
9d4c0f4f | 94 | drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE; |
4445b1d2 DG |
95 | drc->ccs_offset = drc->fdt_start_offset; |
96 | drc->ccs_depth = 0; | |
0dfabd39 DG |
97 | |
98 | return RTAS_OUT_SUCCESS; | |
99 | } | |
100 | ||
101 | static uint32_t drc_isolate_logical(sPAPRDRConnector *drc) | |
102 | { | |
9d4c0f4f DG |
103 | switch (drc->state) { |
104 | case SPAPR_DRC_STATE_LOGICAL_AVAILABLE: | |
105 | case SPAPR_DRC_STATE_LOGICAL_UNUSABLE: | |
106 | return RTAS_OUT_SUCCESS; /* Nothing to do */ | |
107 | case SPAPR_DRC_STATE_LOGICAL_CONFIGURED: | |
108 | break; /* see below */ | |
109 | case SPAPR_DRC_STATE_LOGICAL_UNISOLATE: | |
110 | return RTAS_OUT_PARAM_ERROR; /* not allowed */ | |
111 | default: | |
112 | g_assert_not_reached(); | |
113 | } | |
114 | ||
cf632463 BR |
115 | /* |
116 | * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't | |
117 | * belong to a DIMM device that is marked for removal. | |
118 | * | |
119 | * Currently the guest userspace tool drmgr that drives the memory | |
120 | * hotplug/unplug will just try to remove a set of 'removable' LMBs | |
121 | * in response to a hot unplug request that is based on drc-count. | |
122 | * If the LMB being removed doesn't belong to a DIMM device that is | |
123 | * actually being unplugged, fail the isolation request here. | |
124 | */ | |
0dfabd39 | 125 | if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB |
f1c52354 | 126 | && !drc->unplug_requested) { |
0dfabd39 | 127 | return RTAS_OUT_HW_ERROR; |
cf632463 BR |
128 | } |
129 | ||
9d4c0f4f | 130 | drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE; |
bbf5c878 | 131 | |
0dfabd39 DG |
132 | /* if we're awaiting release, but still in an unconfigured state, |
133 | * it's likely the guest is still in the process of configuring | |
134 | * the device and is transitioning the devices to an ISOLATED | |
135 | * state as a part of that process. so we only complete the | |
136 | * removal when this transition happens for a device in a | |
137 | * configured state, as suggested by the state diagram from PAPR+ | |
138 | * 2.7, 13.4 | |
139 | */ | |
f1c52354 | 140 | if (drc->unplug_requested) { |
0dfabd39 | 141 | uint32_t drc_index = spapr_drc_index(drc); |
9d4c0f4f DG |
142 | trace_spapr_drc_set_isolation_state_finalizing(drc_index); |
143 | spapr_drc_detach(drc); | |
bbf5c878 | 144 | } |
0dfabd39 DG |
145 | return RTAS_OUT_SUCCESS; |
146 | } | |
147 | ||
148 | static uint32_t drc_unisolate_logical(sPAPRDRConnector *drc) | |
149 | { | |
9d4c0f4f DG |
150 | switch (drc->state) { |
151 | case SPAPR_DRC_STATE_LOGICAL_UNISOLATE: | |
152 | case SPAPR_DRC_STATE_LOGICAL_CONFIGURED: | |
153 | return RTAS_OUT_SUCCESS; /* Nothing to do */ | |
154 | case SPAPR_DRC_STATE_LOGICAL_AVAILABLE: | |
155 | break; /* see below */ | |
156 | case SPAPR_DRC_STATE_LOGICAL_UNUSABLE: | |
157 | return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */ | |
158 | default: | |
159 | g_assert_not_reached(); | |
0dfabd39 DG |
160 | } |
161 | ||
9d4c0f4f DG |
162 | /* Move to AVAILABLE state should have ensured device was present */ |
163 | g_assert(drc->dev); | |
bbf5c878 | 164 | |
9d4c0f4f | 165 | drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE; |
4445b1d2 DG |
166 | drc->ccs_offset = drc->fdt_start_offset; |
167 | drc->ccs_depth = 0; | |
168 | ||
0cb688d2 | 169 | return RTAS_OUT_SUCCESS; |
bbf5c878 MR |
170 | } |
171 | ||
61736732 | 172 | static uint32_t drc_set_usable(sPAPRDRConnector *drc) |
bbf5c878 | 173 | { |
9d4c0f4f DG |
174 | switch (drc->state) { |
175 | case SPAPR_DRC_STATE_LOGICAL_AVAILABLE: | |
176 | case SPAPR_DRC_STATE_LOGICAL_UNISOLATE: | |
177 | case SPAPR_DRC_STATE_LOGICAL_CONFIGURED: | |
178 | return RTAS_OUT_SUCCESS; /* Nothing to do */ | |
179 | case SPAPR_DRC_STATE_LOGICAL_UNUSABLE: | |
180 | break; /* see below */ | |
181 | default: | |
182 | g_assert_not_reached(); | |
183 | } | |
184 | ||
61736732 DG |
185 | /* if there's no resource/device associated with the DRC, there's |
186 | * no way for us to put it in an allocation state consistent with | |
187 | * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should | |
188 | * result in an RTAS return code of -3 / "no such indicator" | |
189 | */ | |
190 | if (!drc->dev) { | |
191 | return RTAS_OUT_NO_SUCH_INDICATOR; | |
192 | } | |
f1c52354 | 193 | if (drc->unplug_requested) { |
82a93a1d DG |
194 | /* Don't allow the guest to move a device away from UNUSABLE |
195 | * state when we want to unplug it */ | |
61736732 | 196 | return RTAS_OUT_NO_SUCH_INDICATOR; |
9d1852ce MR |
197 | } |
198 | ||
9d4c0f4f | 199 | drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE; |
61736732 DG |
200 | |
201 | return RTAS_OUT_SUCCESS; | |
202 | } | |
203 | ||
204 | static uint32_t drc_set_unusable(sPAPRDRConnector *drc) | |
205 | { | |
9d4c0f4f DG |
206 | switch (drc->state) { |
207 | case SPAPR_DRC_STATE_LOGICAL_UNUSABLE: | |
208 | return RTAS_OUT_SUCCESS; /* Nothing to do */ | |
209 | case SPAPR_DRC_STATE_LOGICAL_AVAILABLE: | |
210 | break; /* see below */ | |
211 | case SPAPR_DRC_STATE_LOGICAL_UNISOLATE: | |
212 | case SPAPR_DRC_STATE_LOGICAL_CONFIGURED: | |
213 | return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */ | |
214 | default: | |
215 | g_assert_not_reached(); | |
216 | } | |
217 | ||
218 | drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE; | |
f1c52354 | 219 | if (drc->unplug_requested) { |
61736732 DG |
220 | uint32_t drc_index = spapr_drc_index(drc); |
221 | trace_spapr_drc_set_allocation_state_finalizing(drc_index); | |
a8dc47fd | 222 | spapr_drc_detach(drc); |
bbf5c878 | 223 | } |
61736732 | 224 | |
0cb688d2 | 225 | return RTAS_OUT_SUCCESS; |
bbf5c878 MR |
226 | } |
227 | ||
79808336 | 228 | static const char *spapr_drc_name(sPAPRDRConnector *drc) |
bbf5c878 | 229 | { |
79808336 DG |
230 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); |
231 | ||
232 | /* human-readable name for a DRC to encode into the DT | |
233 | * description. this is mainly only used within a guest in place | |
234 | * of the unique DRC index. | |
235 | * | |
236 | * in the case of VIO/PCI devices, it corresponds to a "location | |
237 | * code" that maps a logical device/function (DRC index) to a | |
238 | * physical (or virtual in the case of VIO) location in the system | |
239 | * by chaining together the "location label" for each | |
240 | * encapsulating component. | |
241 | * | |
242 | * since this is more to do with diagnosing physical hardware | |
243 | * issues than guest compatibility, we choose location codes/DRC | |
244 | * names that adhere to the documented format, but avoid encoding | |
245 | * the entire topology information into the label/code, instead | |
246 | * just using the location codes based on the labels for the | |
247 | * endpoints (VIO/PCI adaptor connectors), which is basically just | |
248 | * "C" followed by an integer ID. | |
249 | * | |
250 | * DRC names as documented by PAPR+ v2.7, 13.5.2.4 | |
251 | * location codes as documented by PAPR+ v2.7, 12.3.1.5 | |
252 | */ | |
253 | return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id); | |
bbf5c878 MR |
254 | } |
255 | ||
bbf5c878 MR |
256 | /* |
257 | * dr-entity-sense sensor value | |
258 | * returned via get-sensor-state RTAS calls | |
259 | * as expected by state diagram in PAPR+ 2.7, 13.4 | |
260 | * based on the current allocation/indicator/power states | |
261 | * for the DR connector. | |
262 | */ | |
f224d35b | 263 | static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc) |
bbf5c878 | 264 | { |
f224d35b DG |
265 | /* this assumes all PCI devices are assigned to a 'live insertion' |
266 | * power domain, where QEMU manages power state automatically as | |
267 | * opposed to the guest. present, non-PCI resources are unaffected | |
268 | * by power state. | |
269 | */ | |
270 | return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT | |
271 | : SPAPR_DR_ENTITY_SENSE_EMPTY; | |
272 | } | |
273 | ||
274 | static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc) | |
275 | { | |
9d4c0f4f DG |
276 | switch (drc->state) { |
277 | case SPAPR_DRC_STATE_LOGICAL_UNUSABLE: | |
f224d35b | 278 | return SPAPR_DR_ENTITY_SENSE_UNUSABLE; |
9d4c0f4f DG |
279 | case SPAPR_DRC_STATE_LOGICAL_AVAILABLE: |
280 | case SPAPR_DRC_STATE_LOGICAL_UNISOLATE: | |
281 | case SPAPR_DRC_STATE_LOGICAL_CONFIGURED: | |
282 | g_assert(drc->dev); | |
283 | return SPAPR_DR_ENTITY_SENSE_PRESENT; | |
284 | default: | |
285 | g_assert_not_reached(); | |
bbf5c878 | 286 | } |
bbf5c878 MR |
287 | } |
288 | ||
d7bce999 EB |
289 | static void prop_get_index(Object *obj, Visitor *v, const char *name, |
290 | void *opaque, Error **errp) | |
bbf5c878 MR |
291 | { |
292 | sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); | |
0b55aa91 | 293 | uint32_t value = spapr_drc_index(drc); |
51e72bc1 | 294 | visit_type_uint32(v, name, &value, errp); |
bbf5c878 MR |
295 | } |
296 | ||
d7bce999 EB |
297 | static void prop_get_fdt(Object *obj, Visitor *v, const char *name, |
298 | void *opaque, Error **errp) | |
bbf5c878 MR |
299 | { |
300 | sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); | |
d2f95f4d | 301 | QNull *null = NULL; |
c75304a1 | 302 | Error *err = NULL; |
bbf5c878 MR |
303 | int fdt_offset_next, fdt_offset, fdt_depth; |
304 | void *fdt; | |
305 | ||
306 | if (!drc->fdt) { | |
d2f95f4d MA |
307 | visit_type_null(v, NULL, &null, errp); |
308 | QDECREF(null); | |
bbf5c878 MR |
309 | return; |
310 | } | |
311 | ||
312 | fdt = drc->fdt; | |
313 | fdt_offset = drc->fdt_start_offset; | |
314 | fdt_depth = 0; | |
315 | ||
316 | do { | |
317 | const char *name = NULL; | |
318 | const struct fdt_property *prop = NULL; | |
319 | int prop_len = 0, name_len = 0; | |
320 | uint32_t tag; | |
321 | ||
322 | tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next); | |
323 | switch (tag) { | |
324 | case FDT_BEGIN_NODE: | |
325 | fdt_depth++; | |
326 | name = fdt_get_name(fdt, fdt_offset, &name_len); | |
337283df | 327 | visit_start_struct(v, name, NULL, 0, &err); |
c75304a1 MA |
328 | if (err) { |
329 | error_propagate(errp, err); | |
330 | return; | |
331 | } | |
bbf5c878 MR |
332 | break; |
333 | case FDT_END_NODE: | |
334 | /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */ | |
335 | g_assert(fdt_depth > 0); | |
15c2f669 | 336 | visit_check_struct(v, &err); |
1158bb2a | 337 | visit_end_struct(v, NULL); |
c75304a1 MA |
338 | if (err) { |
339 | error_propagate(errp, err); | |
340 | return; | |
341 | } | |
bbf5c878 MR |
342 | fdt_depth--; |
343 | break; | |
344 | case FDT_PROP: { | |
345 | int i; | |
346 | prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len); | |
347 | name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); | |
d9f62dde | 348 | visit_start_list(v, name, NULL, 0, &err); |
c75304a1 MA |
349 | if (err) { |
350 | error_propagate(errp, err); | |
351 | return; | |
352 | } | |
bbf5c878 | 353 | for (i = 0; i < prop_len; i++) { |
51e72bc1 | 354 | visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err); |
c75304a1 MA |
355 | if (err) { |
356 | error_propagate(errp, err); | |
357 | return; | |
358 | } | |
359 | } | |
a4a1c70d | 360 | visit_check_list(v, &err); |
1158bb2a | 361 | visit_end_list(v, NULL); |
a4a1c70d MA |
362 | if (err) { |
363 | error_propagate(errp, err); | |
364 | return; | |
365 | } | |
bbf5c878 MR |
366 | break; |
367 | } | |
368 | default: | |
369 | error_setg(&error_abort, "device FDT in unexpected state: %d", tag); | |
370 | } | |
371 | fdt_offset = fdt_offset_next; | |
372 | } while (fdt_depth != 0); | |
373 | } | |
374 | ||
0be4e886 | 375 | void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt, |
5c1da812 | 376 | int fdt_start_offset, Error **errp) |
bbf5c878 | 377 | { |
0b55aa91 | 378 | trace_spapr_drc_attach(spapr_drc_index(drc)); |
bbf5c878 | 379 | |
9d4c0f4f | 380 | if (drc->dev) { |
bbf5c878 MR |
381 | error_setg(errp, "an attached device is still awaiting release"); |
382 | return; | |
383 | } | |
9d4c0f4f DG |
384 | g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE) |
385 | || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON)); | |
5c1da812 | 386 | g_assert(fdt); |
bbf5c878 | 387 | |
bbf5c878 MR |
388 | drc->dev = d; |
389 | drc->fdt = fdt; | |
390 | drc->fdt_start_offset = fdt_start_offset; | |
bbf5c878 MR |
391 | |
392 | object_property_add_link(OBJECT(drc), "device", | |
393 | object_get_typename(OBJECT(drc->dev)), | |
394 | (Object **)(&drc->dev), | |
395 | NULL, 0, NULL); | |
396 | } | |
397 | ||
9c914e53 | 398 | static void spapr_drc_release(sPAPRDRConnector *drc) |
bbf5c878 | 399 | { |
6b762f29 DG |
400 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); |
401 | ||
402 | drck->release(drc->dev); | |
bbf5c878 | 403 | |
f1c52354 | 404 | drc->unplug_requested = false; |
bbf5c878 MR |
405 | g_free(drc->fdt); |
406 | drc->fdt = NULL; | |
407 | drc->fdt_start_offset = 0; | |
ba50822f | 408 | object_property_del(OBJECT(drc), "device", &error_abort); |
bbf5c878 | 409 | drc->dev = NULL; |
bbf5c878 MR |
410 | } |
411 | ||
a8dc47fd | 412 | void spapr_drc_detach(sPAPRDRConnector *drc) |
9c914e53 | 413 | { |
9d4c0f4f DG |
414 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); |
415 | ||
9c914e53 DG |
416 | trace_spapr_drc_detach(spapr_drc_index(drc)); |
417 | ||
9d4c0f4f | 418 | g_assert(drc->dev); |
a8dc47fd | 419 | |
9d4c0f4f | 420 | drc->unplug_requested = true; |
9c914e53 | 421 | |
9d4c0f4f DG |
422 | if (drc->state != drck->empty_state) { |
423 | trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc)); | |
9c914e53 DG |
424 | return; |
425 | } | |
426 | ||
9c914e53 DG |
427 | spapr_drc_release(drc); |
428 | } | |
429 | ||
94fd9cba | 430 | void spapr_drc_reset(sPAPRDRConnector *drc) |
bbf5c878 | 431 | { |
9d4c0f4f DG |
432 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); |
433 | ||
0b55aa91 | 434 | trace_spapr_drc_reset(spapr_drc_index(drc)); |
b8fdd530 | 435 | |
bbf5c878 | 436 | /* immediately upon reset we can safely assume DRCs whose devices |
4f9242fc | 437 | * are pending removal can be safely removed. |
bbf5c878 | 438 | */ |
f1c52354 | 439 | if (drc->unplug_requested) { |
4f9242fc DG |
440 | spapr_drc_release(drc); |
441 | } | |
442 | ||
4f9242fc | 443 | if (drc->dev) { |
9d4c0f4f DG |
444 | /* A device present at reset is ready to go, same as coldplugged */ |
445 | drc->state = drck->ready_state; | |
188bfe1b BR |
446 | /* |
447 | * Ensure that we are able to send the FDT fragment again | |
448 | * via configure-connector call if the guest requests. | |
449 | */ | |
450 | drc->ccs_offset = drc->fdt_start_offset; | |
451 | drc->ccs_depth = 0; | |
4f9242fc | 452 | } else { |
9d4c0f4f | 453 | drc->state = drck->empty_state; |
188bfe1b BR |
454 | drc->ccs_offset = -1; |
455 | drc->ccs_depth = -1; | |
bbf5c878 MR |
456 | } |
457 | } | |
458 | ||
10f12e64 | 459 | bool spapr_drc_needed(void *opaque) |
a50919dd DHB |
460 | { |
461 | sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque; | |
462 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
a50919dd DHB |
463 | |
464 | /* If no dev is plugged in there is no need to migrate the DRC state */ | |
c618e300 | 465 | if (!drc->dev) { |
a50919dd DHB |
466 | return false; |
467 | } | |
468 | ||
469 | /* | |
9d4c0f4f DG |
470 | * We need to migrate the state if it's not equal to the expected |
471 | * long-term state, which is the same as the coldplugged initial | |
472 | * state */ | |
473 | return (drc->state != drck->ready_state); | |
a50919dd DHB |
474 | } |
475 | ||
476 | static const VMStateDescription vmstate_spapr_drc = { | |
477 | .name = "spapr_drc", | |
478 | .version_id = 1, | |
479 | .minimum_version_id = 1, | |
480 | .needed = spapr_drc_needed, | |
481 | .fields = (VMStateField []) { | |
9d4c0f4f | 482 | VMSTATE_UINT32(state, sPAPRDRConnector), |
a50919dd DHB |
483 | VMSTATE_END_OF_LIST() |
484 | } | |
485 | }; | |
486 | ||
bbf5c878 MR |
487 | static void realize(DeviceState *d, Error **errp) |
488 | { | |
489 | sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); | |
bbf5c878 | 490 | Object *root_container; |
f5babeac | 491 | gchar *link_name; |
bbf5c878 MR |
492 | gchar *child_name; |
493 | Error *err = NULL; | |
494 | ||
0b55aa91 | 495 | trace_spapr_drc_realize(spapr_drc_index(drc)); |
bbf5c878 MR |
496 | /* NOTE: we do this as part of realize/unrealize due to the fact |
497 | * that the guest will communicate with the DRC via RTAS calls | |
498 | * referencing the global DRC index. By unlinking the DRC | |
499 | * from DRC_CONTAINER_PATH/<drc_index> we effectively make it | |
500 | * inaccessible by the guest, since lookups rely on this path | |
501 | * existing in the composition tree | |
502 | */ | |
503 | root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); | |
f5babeac | 504 | link_name = g_strdup_printf("%x", spapr_drc_index(drc)); |
bbf5c878 | 505 | child_name = object_get_canonical_path_component(OBJECT(drc)); |
0b55aa91 | 506 | trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name); |
bbf5c878 MR |
507 | object_property_add_alias(root_container, link_name, |
508 | drc->owner, child_name, &err); | |
bf26ae32 | 509 | g_free(child_name); |
f5babeac | 510 | g_free(link_name); |
bbf5c878 | 511 | if (err) { |
bf26ae32 GK |
512 | error_propagate(errp, err); |
513 | return; | |
bbf5c878 | 514 | } |
0b55aa91 | 515 | vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc, |
a50919dd | 516 | drc); |
0b55aa91 | 517 | trace_spapr_drc_realize_complete(spapr_drc_index(drc)); |
bbf5c878 MR |
518 | } |
519 | ||
520 | static void unrealize(DeviceState *d, Error **errp) | |
521 | { | |
522 | sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); | |
bbf5c878 | 523 | Object *root_container; |
f5babeac | 524 | gchar *name; |
bbf5c878 | 525 | |
0b55aa91 | 526 | trace_spapr_drc_unrealize(spapr_drc_index(drc)); |
bf26ae32 | 527 | vmstate_unregister(DEVICE(drc), &vmstate_spapr_drc, drc); |
bbf5c878 | 528 | root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); |
f5babeac | 529 | name = g_strdup_printf("%x", spapr_drc_index(drc)); |
bf26ae32 | 530 | object_property_del(root_container, name, errp); |
f5babeac | 531 | g_free(name); |
bbf5c878 MR |
532 | } |
533 | ||
2d335818 | 534 | sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type, |
bbf5c878 MR |
535 | uint32_t id) |
536 | { | |
2d335818 | 537 | sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type)); |
94649d42 | 538 | char *prop_name; |
bbf5c878 | 539 | |
bbf5c878 MR |
540 | drc->id = id; |
541 | drc->owner = owner; | |
0b55aa91 DG |
542 | prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", |
543 | spapr_drc_index(drc)); | |
325837ca | 544 | object_property_add_child(owner, prop_name, OBJECT(drc), &error_abort); |
f3f41030 | 545 | object_unref(OBJECT(drc)); |
bbf5c878 | 546 | object_property_set_bool(OBJECT(drc), true, "realized", NULL); |
94649d42 | 547 | g_free(prop_name); |
bbf5c878 | 548 | |
bbf5c878 MR |
549 | return drc; |
550 | } | |
551 | ||
552 | static void spapr_dr_connector_instance_init(Object *obj) | |
553 | { | |
554 | sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); | |
9d4c0f4f | 555 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); |
bbf5c878 | 556 | |
bbf5c878 MR |
557 | object_property_add_uint32_ptr(obj, "id", &drc->id, NULL); |
558 | object_property_add(obj, "index", "uint32", prop_get_index, | |
559 | NULL, NULL, NULL, NULL); | |
bbf5c878 MR |
560 | object_property_add(obj, "fdt", "struct", prop_get_fdt, |
561 | NULL, NULL, NULL, NULL); | |
9d4c0f4f | 562 | drc->state = drck->empty_state; |
bbf5c878 MR |
563 | } |
564 | ||
565 | static void spapr_dr_connector_class_init(ObjectClass *k, void *data) | |
566 | { | |
567 | DeviceClass *dk = DEVICE_CLASS(k); | |
bbf5c878 | 568 | |
bbf5c878 MR |
569 | dk->realize = realize; |
570 | dk->unrealize = unrealize; | |
c401ae8c MA |
571 | /* |
572 | * Reason: it crashes FIXME find and document the real reason | |
573 | */ | |
e90f2a8c | 574 | dk->user_creatable = false; |
bbf5c878 MR |
575 | } |
576 | ||
67fea71b DG |
577 | static bool drc_physical_needed(void *opaque) |
578 | { | |
579 | sPAPRDRCPhysical *drcp = (sPAPRDRCPhysical *)opaque; | |
580 | sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(drcp); | |
581 | ||
582 | if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE)) | |
583 | || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) { | |
584 | return false; | |
585 | } | |
586 | return true; | |
587 | } | |
588 | ||
589 | static const VMStateDescription vmstate_spapr_drc_physical = { | |
590 | .name = "spapr_drc/physical", | |
591 | .version_id = 1, | |
592 | .minimum_version_id = 1, | |
593 | .needed = drc_physical_needed, | |
594 | .fields = (VMStateField []) { | |
595 | VMSTATE_UINT32(dr_indicator, sPAPRDRCPhysical), | |
596 | VMSTATE_END_OF_LIST() | |
597 | } | |
598 | }; | |
599 | ||
600 | static void drc_physical_reset(void *opaque) | |
601 | { | |
602 | sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(opaque); | |
603 | sPAPRDRCPhysical *drcp = SPAPR_DRC_PHYSICAL(drc); | |
604 | ||
605 | if (drc->dev) { | |
606 | drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE; | |
607 | } else { | |
608 | drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE; | |
609 | } | |
610 | } | |
611 | ||
612 | static void realize_physical(DeviceState *d, Error **errp) | |
613 | { | |
614 | sPAPRDRCPhysical *drcp = SPAPR_DRC_PHYSICAL(d); | |
615 | Error *local_err = NULL; | |
616 | ||
617 | realize(d, &local_err); | |
618 | if (local_err) { | |
619 | error_propagate(errp, local_err); | |
620 | return; | |
621 | } | |
622 | ||
623 | vmstate_register(DEVICE(drcp), spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)), | |
624 | &vmstate_spapr_drc_physical, drcp); | |
625 | qemu_register_reset(drc_physical_reset, drcp); | |
626 | } | |
627 | ||
379ae096 GK |
628 | static void unrealize_physical(DeviceState *d, Error **errp) |
629 | { | |
630 | sPAPRDRCPhysical *drcp = SPAPR_DRC_PHYSICAL(d); | |
631 | Error *local_err = NULL; | |
632 | ||
633 | unrealize(d, &local_err); | |
634 | if (local_err) { | |
635 | error_propagate(errp, local_err); | |
636 | return; | |
637 | } | |
638 | ||
639 | vmstate_unregister(DEVICE(drcp), &vmstate_spapr_drc_physical, drcp); | |
640 | qemu_unregister_reset(drc_physical_reset, drcp); | |
641 | } | |
642 | ||
f224d35b DG |
643 | static void spapr_drc_physical_class_init(ObjectClass *k, void *data) |
644 | { | |
67fea71b | 645 | DeviceClass *dk = DEVICE_CLASS(k); |
f224d35b DG |
646 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); |
647 | ||
67fea71b | 648 | dk->realize = realize_physical; |
379ae096 | 649 | dk->unrealize = unrealize_physical; |
f224d35b | 650 | drck->dr_entity_sense = physical_entity_sense; |
0dfabd39 DG |
651 | drck->isolate = drc_isolate_physical; |
652 | drck->unisolate = drc_unisolate_physical; | |
9d4c0f4f DG |
653 | drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED; |
654 | drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON; | |
f224d35b DG |
655 | } |
656 | ||
657 | static void spapr_drc_logical_class_init(ObjectClass *k, void *data) | |
658 | { | |
659 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); | |
660 | ||
661 | drck->dr_entity_sense = logical_entity_sense; | |
0dfabd39 DG |
662 | drck->isolate = drc_isolate_logical; |
663 | drck->unisolate = drc_unisolate_logical; | |
9d4c0f4f DG |
664 | drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED; |
665 | drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE; | |
f224d35b DG |
666 | } |
667 | ||
2d335818 DG |
668 | static void spapr_drc_cpu_class_init(ObjectClass *k, void *data) |
669 | { | |
670 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); | |
671 | ||
672 | drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU; | |
1693ea16 | 673 | drck->typename = "CPU"; |
79808336 | 674 | drck->drc_name_prefix = "CPU "; |
6b762f29 | 675 | drck->release = spapr_core_release; |
2d335818 DG |
676 | } |
677 | ||
678 | static void spapr_drc_pci_class_init(ObjectClass *k, void *data) | |
679 | { | |
680 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); | |
681 | ||
682 | drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI; | |
1693ea16 | 683 | drck->typename = "28"; |
79808336 | 684 | drck->drc_name_prefix = "C"; |
6b762f29 | 685 | drck->release = spapr_phb_remove_pci_device_cb; |
2d335818 DG |
686 | } |
687 | ||
688 | static void spapr_drc_lmb_class_init(ObjectClass *k, void *data) | |
689 | { | |
690 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); | |
691 | ||
692 | drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB; | |
1693ea16 | 693 | drck->typename = "MEM"; |
79808336 | 694 | drck->drc_name_prefix = "LMB "; |
6b762f29 | 695 | drck->release = spapr_lmb_release; |
2d335818 DG |
696 | } |
697 | ||
bbf5c878 MR |
698 | static const TypeInfo spapr_dr_connector_info = { |
699 | .name = TYPE_SPAPR_DR_CONNECTOR, | |
700 | .parent = TYPE_DEVICE, | |
701 | .instance_size = sizeof(sPAPRDRConnector), | |
702 | .instance_init = spapr_dr_connector_instance_init, | |
703 | .class_size = sizeof(sPAPRDRConnectorClass), | |
704 | .class_init = spapr_dr_connector_class_init, | |
2d335818 DG |
705 | .abstract = true, |
706 | }; | |
707 | ||
708 | static const TypeInfo spapr_drc_physical_info = { | |
709 | .name = TYPE_SPAPR_DRC_PHYSICAL, | |
710 | .parent = TYPE_SPAPR_DR_CONNECTOR, | |
67fea71b | 711 | .instance_size = sizeof(sPAPRDRCPhysical), |
f224d35b | 712 | .class_init = spapr_drc_physical_class_init, |
2d335818 DG |
713 | .abstract = true, |
714 | }; | |
715 | ||
716 | static const TypeInfo spapr_drc_logical_info = { | |
717 | .name = TYPE_SPAPR_DRC_LOGICAL, | |
718 | .parent = TYPE_SPAPR_DR_CONNECTOR, | |
f224d35b | 719 | .class_init = spapr_drc_logical_class_init, |
2d335818 DG |
720 | .abstract = true, |
721 | }; | |
722 | ||
723 | static const TypeInfo spapr_drc_cpu_info = { | |
724 | .name = TYPE_SPAPR_DRC_CPU, | |
725 | .parent = TYPE_SPAPR_DRC_LOGICAL, | |
2d335818 DG |
726 | .class_init = spapr_drc_cpu_class_init, |
727 | }; | |
728 | ||
729 | static const TypeInfo spapr_drc_pci_info = { | |
730 | .name = TYPE_SPAPR_DRC_PCI, | |
731 | .parent = TYPE_SPAPR_DRC_PHYSICAL, | |
2d335818 DG |
732 | .class_init = spapr_drc_pci_class_init, |
733 | }; | |
734 | ||
735 | static const TypeInfo spapr_drc_lmb_info = { | |
736 | .name = TYPE_SPAPR_DRC_LMB, | |
737 | .parent = TYPE_SPAPR_DRC_LOGICAL, | |
2d335818 | 738 | .class_init = spapr_drc_lmb_class_init, |
bbf5c878 MR |
739 | }; |
740 | ||
bbf5c878 MR |
741 | /* helper functions for external users */ |
742 | ||
fbf55397 | 743 | sPAPRDRConnector *spapr_drc_by_index(uint32_t index) |
bbf5c878 MR |
744 | { |
745 | Object *obj; | |
f5babeac | 746 | gchar *name; |
bbf5c878 | 747 | |
f5babeac | 748 | name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH, index); |
bbf5c878 | 749 | obj = object_resolve_path(name, NULL); |
f5babeac | 750 | g_free(name); |
bbf5c878 MR |
751 | |
752 | return !obj ? NULL : SPAPR_DR_CONNECTOR(obj); | |
753 | } | |
754 | ||
fbf55397 | 755 | sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id) |
bbf5c878 | 756 | { |
fbf55397 DG |
757 | sPAPRDRConnectorClass *drck |
758 | = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type)); | |
759 | ||
760 | return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT | |
761 | | (id & DRC_INDEX_ID_MASK)); | |
bbf5c878 | 762 | } |
e4b798bb | 763 | |
e4b798bb MR |
764 | /** |
765 | * spapr_drc_populate_dt | |
766 | * | |
767 | * @fdt: libfdt device tree | |
768 | * @path: path in the DT to generate properties | |
769 | * @owner: parent Object/DeviceState for which to generate DRC | |
770 | * descriptions for | |
771 | * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding | |
772 | * to the types of DRCs to generate entries for | |
773 | * | |
774 | * generate OF properties to describe DRC topology/indices to guests | |
775 | * | |
776 | * as documented in PAPR+ v2.1, 13.5.2 | |
777 | */ | |
778 | int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner, | |
779 | uint32_t drc_type_mask) | |
780 | { | |
781 | Object *root_container; | |
782 | ObjectProperty *prop; | |
7746abd8 | 783 | ObjectPropertyIterator iter; |
e4b798bb MR |
784 | uint32_t drc_count = 0; |
785 | GArray *drc_indexes, *drc_power_domains; | |
786 | GString *drc_names, *drc_types; | |
787 | int ret; | |
788 | ||
789 | /* the first entry of each properties is a 32-bit integer encoding | |
790 | * the number of elements in the array. we won't know this until | |
791 | * we complete the iteration through all the matching DRCs, but | |
792 | * reserve the space now and set the offsets accordingly so we | |
793 | * can fill them in later. | |
794 | */ | |
795 | drc_indexes = g_array_new(false, true, sizeof(uint32_t)); | |
796 | drc_indexes = g_array_set_size(drc_indexes, 1); | |
797 | drc_power_domains = g_array_new(false, true, sizeof(uint32_t)); | |
798 | drc_power_domains = g_array_set_size(drc_power_domains, 1); | |
799 | drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); | |
800 | drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); | |
801 | ||
802 | /* aliases for all DRConnector objects will be rooted in QOM | |
803 | * composition tree at DRC_CONTAINER_PATH | |
804 | */ | |
805 | root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); | |
806 | ||
7746abd8 DB |
807 | object_property_iter_init(&iter, root_container); |
808 | while ((prop = object_property_iter_next(&iter))) { | |
e4b798bb MR |
809 | Object *obj; |
810 | sPAPRDRConnector *drc; | |
811 | sPAPRDRConnectorClass *drck; | |
812 | uint32_t drc_index, drc_power_domain; | |
813 | ||
814 | if (!strstart(prop->type, "link<", NULL)) { | |
815 | continue; | |
816 | } | |
817 | ||
818 | obj = object_property_get_link(root_container, prop->name, NULL); | |
819 | drc = SPAPR_DR_CONNECTOR(obj); | |
820 | drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
821 | ||
822 | if (owner && (drc->owner != owner)) { | |
823 | continue; | |
824 | } | |
825 | ||
2d335818 | 826 | if ((spapr_drc_type(drc) & drc_type_mask) == 0) { |
e4b798bb MR |
827 | continue; |
828 | } | |
829 | ||
830 | drc_count++; | |
831 | ||
832 | /* ibm,drc-indexes */ | |
0b55aa91 | 833 | drc_index = cpu_to_be32(spapr_drc_index(drc)); |
e4b798bb MR |
834 | g_array_append_val(drc_indexes, drc_index); |
835 | ||
836 | /* ibm,drc-power-domains */ | |
837 | drc_power_domain = cpu_to_be32(-1); | |
838 | g_array_append_val(drc_power_domains, drc_power_domain); | |
839 | ||
840 | /* ibm,drc-names */ | |
79808336 | 841 | drc_names = g_string_append(drc_names, spapr_drc_name(drc)); |
e4b798bb MR |
842 | drc_names = g_string_insert_len(drc_names, -1, "\0", 1); |
843 | ||
844 | /* ibm,drc-types */ | |
1693ea16 | 845 | drc_types = g_string_append(drc_types, drck->typename); |
e4b798bb MR |
846 | drc_types = g_string_insert_len(drc_types, -1, "\0", 1); |
847 | } | |
848 | ||
849 | /* now write the drc count into the space we reserved at the | |
850 | * beginning of the arrays previously | |
851 | */ | |
852 | *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count); | |
853 | *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count); | |
854 | *(uint32_t *)drc_names->str = cpu_to_be32(drc_count); | |
855 | *(uint32_t *)drc_types->str = cpu_to_be32(drc_count); | |
856 | ||
857 | ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes", | |
858 | drc_indexes->data, | |
859 | drc_indexes->len * sizeof(uint32_t)); | |
860 | if (ret) { | |
ce9863b7 | 861 | error_report("Couldn't create ibm,drc-indexes property"); |
e4b798bb MR |
862 | goto out; |
863 | } | |
864 | ||
865 | ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains", | |
866 | drc_power_domains->data, | |
867 | drc_power_domains->len * sizeof(uint32_t)); | |
868 | if (ret) { | |
ce9863b7 | 869 | error_report("Couldn't finalize ibm,drc-power-domains property"); |
e4b798bb MR |
870 | goto out; |
871 | } | |
872 | ||
873 | ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names", | |
874 | drc_names->str, drc_names->len); | |
875 | if (ret) { | |
ce9863b7 | 876 | error_report("Couldn't finalize ibm,drc-names property"); |
e4b798bb MR |
877 | goto out; |
878 | } | |
879 | ||
880 | ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types", | |
881 | drc_types->str, drc_types->len); | |
882 | if (ret) { | |
ce9863b7 | 883 | error_report("Couldn't finalize ibm,drc-types property"); |
e4b798bb MR |
884 | goto out; |
885 | } | |
886 | ||
887 | out: | |
888 | g_array_free(drc_indexes, true); | |
889 | g_array_free(drc_power_domains, true); | |
890 | g_string_free(drc_names, true); | |
891 | g_string_free(drc_types, true); | |
892 | ||
893 | return ret; | |
894 | } | |
b89b3d39 DG |
895 | |
896 | /* | |
897 | * RTAS calls | |
898 | */ | |
899 | ||
7b7258f8 | 900 | static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state) |
b89b3d39 | 901 | { |
7b7258f8 DG |
902 | sPAPRDRConnector *drc = spapr_drc_by_index(idx); |
903 | sPAPRDRConnectorClass *drck; | |
904 | ||
905 | if (!drc) { | |
0dfabd39 | 906 | return RTAS_OUT_NO_SUCH_INDICATOR; |
b89b3d39 DG |
907 | } |
908 | ||
0dfabd39 DG |
909 | trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state); |
910 | ||
7b7258f8 | 911 | drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); |
0dfabd39 DG |
912 | |
913 | switch (state) { | |
914 | case SPAPR_DR_ISOLATION_STATE_ISOLATED: | |
915 | return drck->isolate(drc); | |
916 | ||
917 | case SPAPR_DR_ISOLATION_STATE_UNISOLATED: | |
918 | return drck->unisolate(drc); | |
919 | ||
920 | default: | |
921 | return RTAS_OUT_PARAM_ERROR; | |
922 | } | |
b89b3d39 DG |
923 | } |
924 | ||
7b7258f8 | 925 | static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state) |
b89b3d39 | 926 | { |
7b7258f8 | 927 | sPAPRDRConnector *drc = spapr_drc_by_index(idx); |
b89b3d39 | 928 | |
61736732 DG |
929 | if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) { |
930 | return RTAS_OUT_NO_SUCH_INDICATOR; | |
b89b3d39 DG |
931 | } |
932 | ||
61736732 DG |
933 | trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state); |
934 | ||
935 | switch (state) { | |
936 | case SPAPR_DR_ALLOCATION_STATE_USABLE: | |
937 | return drc_set_usable(drc); | |
938 | ||
939 | case SPAPR_DR_ALLOCATION_STATE_UNUSABLE: | |
940 | return drc_set_unusable(drc); | |
941 | ||
942 | default: | |
943 | return RTAS_OUT_PARAM_ERROR; | |
944 | } | |
7b7258f8 | 945 | } |
b89b3d39 | 946 | |
cd74d27e | 947 | static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state) |
7b7258f8 DG |
948 | { |
949 | sPAPRDRConnector *drc = spapr_drc_by_index(idx); | |
b89b3d39 | 950 | |
67fea71b DG |
951 | if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) { |
952 | return RTAS_OUT_NO_SUCH_INDICATOR; | |
953 | } | |
954 | if ((state != SPAPR_DR_INDICATOR_INACTIVE) | |
955 | && (state != SPAPR_DR_INDICATOR_ACTIVE) | |
956 | && (state != SPAPR_DR_INDICATOR_IDENTIFY) | |
957 | && (state != SPAPR_DR_INDICATOR_ACTION)) { | |
958 | return RTAS_OUT_PARAM_ERROR; /* bad state parameter */ | |
7b7258f8 DG |
959 | } |
960 | ||
cd74d27e | 961 | trace_spapr_drc_set_dr_indicator(idx, state); |
67fea71b | 962 | SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state; |
cd74d27e | 963 | return RTAS_OUT_SUCCESS; |
7b7258f8 DG |
964 | } |
965 | ||
966 | static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr, | |
967 | uint32_t token, | |
968 | uint32_t nargs, target_ulong args, | |
969 | uint32_t nret, target_ulong rets) | |
970 | { | |
971 | uint32_t type, idx, state; | |
972 | uint32_t ret = RTAS_OUT_SUCCESS; | |
973 | ||
974 | if (nargs != 3 || nret != 1) { | |
b89b3d39 DG |
975 | ret = RTAS_OUT_PARAM_ERROR; |
976 | goto out; | |
977 | } | |
b89b3d39 | 978 | |
7b7258f8 DG |
979 | type = rtas_ld(args, 0); |
980 | idx = rtas_ld(args, 1); | |
981 | state = rtas_ld(args, 2); | |
982 | ||
983 | switch (type) { | |
b89b3d39 | 984 | case RTAS_SENSOR_TYPE_ISOLATION_STATE: |
7b7258f8 | 985 | ret = rtas_set_isolation_state(idx, state); |
b89b3d39 DG |
986 | break; |
987 | case RTAS_SENSOR_TYPE_DR: | |
cd74d27e | 988 | ret = rtas_set_dr_indicator(idx, state); |
b89b3d39 DG |
989 | break; |
990 | case RTAS_SENSOR_TYPE_ALLOCATION_STATE: | |
7b7258f8 | 991 | ret = rtas_set_allocation_state(idx, state); |
b89b3d39 DG |
992 | break; |
993 | default: | |
7b7258f8 | 994 | ret = RTAS_OUT_NOT_SUPPORTED; |
b89b3d39 DG |
995 | } |
996 | ||
997 | out: | |
998 | rtas_st(rets, 0, ret); | |
b89b3d39 DG |
999 | } |
1000 | ||
1001 | static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr, | |
1002 | uint32_t token, uint32_t nargs, | |
1003 | target_ulong args, uint32_t nret, | |
1004 | target_ulong rets) | |
1005 | { | |
1006 | uint32_t sensor_type; | |
1007 | uint32_t sensor_index; | |
1008 | uint32_t sensor_state = 0; | |
1009 | sPAPRDRConnector *drc; | |
1010 | sPAPRDRConnectorClass *drck; | |
1011 | uint32_t ret = RTAS_OUT_SUCCESS; | |
1012 | ||
1013 | if (nargs != 2 || nret != 2) { | |
1014 | ret = RTAS_OUT_PARAM_ERROR; | |
1015 | goto out; | |
1016 | } | |
1017 | ||
1018 | sensor_type = rtas_ld(args, 0); | |
1019 | sensor_index = rtas_ld(args, 1); | |
1020 | ||
1021 | if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) { | |
1022 | /* currently only DR-related sensors are implemented */ | |
1023 | trace_spapr_rtas_get_sensor_state_not_supported(sensor_index, | |
1024 | sensor_type); | |
1025 | ret = RTAS_OUT_NOT_SUPPORTED; | |
1026 | goto out; | |
1027 | } | |
1028 | ||
fbf55397 | 1029 | drc = spapr_drc_by_index(sensor_index); |
b89b3d39 DG |
1030 | if (!drc) { |
1031 | trace_spapr_rtas_get_sensor_state_invalid(sensor_index); | |
1032 | ret = RTAS_OUT_PARAM_ERROR; | |
1033 | goto out; | |
1034 | } | |
1035 | drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
f224d35b | 1036 | sensor_state = drck->dr_entity_sense(drc); |
b89b3d39 DG |
1037 | |
1038 | out: | |
1039 | rtas_st(rets, 0, ret); | |
1040 | rtas_st(rets, 1, sensor_state); | |
1041 | } | |
1042 | ||
1043 | /* configure-connector work area offsets, int32_t units for field | |
1044 | * indexes, bytes for field offset/len values. | |
1045 | * | |
1046 | * as documented by PAPR+ v2.7, 13.5.3.5 | |
1047 | */ | |
1048 | #define CC_IDX_NODE_NAME_OFFSET 2 | |
1049 | #define CC_IDX_PROP_NAME_OFFSET 2 | |
1050 | #define CC_IDX_PROP_LEN 3 | |
1051 | #define CC_IDX_PROP_DATA_OFFSET 4 | |
1052 | #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4) | |
1053 | #define CC_WA_LEN 4096 | |
1054 | ||
1055 | static void configure_connector_st(target_ulong addr, target_ulong offset, | |
1056 | const void *buf, size_t len) | |
1057 | { | |
1058 | cpu_physical_memory_write(ppc64_phys_to_real(addr + offset), | |
1059 | buf, MIN(len, CC_WA_LEN - offset)); | |
1060 | } | |
1061 | ||
b89b3d39 DG |
1062 | static void rtas_ibm_configure_connector(PowerPCCPU *cpu, |
1063 | sPAPRMachineState *spapr, | |
1064 | uint32_t token, uint32_t nargs, | |
1065 | target_ulong args, uint32_t nret, | |
1066 | target_ulong rets) | |
1067 | { | |
1068 | uint64_t wa_addr; | |
1069 | uint64_t wa_offset; | |
1070 | uint32_t drc_index; | |
1071 | sPAPRDRConnector *drc; | |
9d4c0f4f | 1072 | sPAPRDRConnectorClass *drck; |
b89b3d39 DG |
1073 | sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE; |
1074 | int rc; | |
b89b3d39 DG |
1075 | |
1076 | if (nargs != 2 || nret != 1) { | |
1077 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); | |
1078 | return; | |
1079 | } | |
1080 | ||
1081 | wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0); | |
1082 | ||
1083 | drc_index = rtas_ld(wa_addr, 0); | |
fbf55397 | 1084 | drc = spapr_drc_by_index(drc_index); |
b89b3d39 DG |
1085 | if (!drc) { |
1086 | trace_spapr_rtas_ibm_configure_connector_invalid(drc_index); | |
1087 | rc = RTAS_OUT_PARAM_ERROR; | |
1088 | goto out; | |
1089 | } | |
1090 | ||
9d4c0f4f | 1091 | if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE) |
188bfe1b BR |
1092 | && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE) |
1093 | && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED) | |
1094 | && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) { | |
1095 | /* | |
1096 | * Need to unisolate the device before configuring | |
1097 | * or it should already be in configured state to | |
1098 | * allow configure-connector be called repeatedly. | |
1099 | */ | |
b89b3d39 DG |
1100 | rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE; |
1101 | goto out; | |
1102 | } | |
1103 | ||
9d4c0f4f DG |
1104 | g_assert(drc->fdt); |
1105 | ||
1106 | drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
1107 | ||
b89b3d39 DG |
1108 | do { |
1109 | uint32_t tag; | |
1110 | const char *name; | |
1111 | const struct fdt_property *prop; | |
1112 | int fdt_offset_next, prop_len; | |
1113 | ||
4445b1d2 | 1114 | tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next); |
b89b3d39 DG |
1115 | |
1116 | switch (tag) { | |
1117 | case FDT_BEGIN_NODE: | |
4445b1d2 DG |
1118 | drc->ccs_depth++; |
1119 | name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL); | |
b89b3d39 DG |
1120 | |
1121 | /* provide the name of the next OF node */ | |
1122 | wa_offset = CC_VAL_DATA_OFFSET; | |
1123 | rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset); | |
1124 | configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1); | |
1125 | resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD; | |
1126 | break; | |
1127 | case FDT_END_NODE: | |
4445b1d2 DG |
1128 | drc->ccs_depth--; |
1129 | if (drc->ccs_depth == 0) { | |
0b55aa91 | 1130 | uint32_t drc_index = spapr_drc_index(drc); |
9d4c0f4f DG |
1131 | |
1132 | /* done sending the device tree, move to configured state */ | |
0b55aa91 | 1133 | trace_spapr_drc_set_configured(drc_index); |
9d4c0f4f | 1134 | drc->state = drck->ready_state; |
188bfe1b BR |
1135 | /* |
1136 | * Ensure that we are able to send the FDT fragment | |
1137 | * again via configure-connector call if the guest requests. | |
1138 | */ | |
1139 | drc->ccs_offset = drc->fdt_start_offset; | |
1140 | drc->ccs_depth = 0; | |
1141 | fdt_offset_next = drc->fdt_start_offset; | |
b89b3d39 DG |
1142 | resp = SPAPR_DR_CC_RESPONSE_SUCCESS; |
1143 | } else { | |
1144 | resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT; | |
1145 | } | |
1146 | break; | |
1147 | case FDT_PROP: | |
4445b1d2 | 1148 | prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset, |
b89b3d39 | 1149 | &prop_len); |
88af6ea5 | 1150 | name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff)); |
b89b3d39 DG |
1151 | |
1152 | /* provide the name of the next OF property */ | |
1153 | wa_offset = CC_VAL_DATA_OFFSET; | |
1154 | rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset); | |
1155 | configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1); | |
1156 | ||
1157 | /* provide the length and value of the OF property. data gets | |
1158 | * placed immediately after NULL terminator of the OF property's | |
1159 | * name string | |
1160 | */ | |
1161 | wa_offset += strlen(name) + 1, | |
1162 | rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len); | |
1163 | rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset); | |
1164 | configure_connector_st(wa_addr, wa_offset, prop->data, prop_len); | |
1165 | resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY; | |
1166 | break; | |
1167 | case FDT_END: | |
1168 | resp = SPAPR_DR_CC_RESPONSE_ERROR; | |
1169 | default: | |
1170 | /* keep seeking for an actionable tag */ | |
1171 | break; | |
1172 | } | |
4445b1d2 DG |
1173 | if (drc->ccs_offset >= 0) { |
1174 | drc->ccs_offset = fdt_offset_next; | |
b89b3d39 DG |
1175 | } |
1176 | } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE); | |
1177 | ||
1178 | rc = resp; | |
1179 | out: | |
1180 | rtas_st(rets, 0, rc); | |
1181 | } | |
1182 | ||
1183 | static void spapr_drc_register_types(void) | |
1184 | { | |
1185 | type_register_static(&spapr_dr_connector_info); | |
2d335818 DG |
1186 | type_register_static(&spapr_drc_physical_info); |
1187 | type_register_static(&spapr_drc_logical_info); | |
1188 | type_register_static(&spapr_drc_cpu_info); | |
1189 | type_register_static(&spapr_drc_pci_info); | |
1190 | type_register_static(&spapr_drc_lmb_info); | |
b89b3d39 DG |
1191 | |
1192 | spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator", | |
1193 | rtas_set_indicator); | |
1194 | spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state", | |
1195 | rtas_get_sensor_state); | |
1196 | spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector", | |
1197 | rtas_ibm_configure_connector); | |
1198 | } | |
1199 | type_init(spapr_drc_register_types) |