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