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