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