]> Git Repo - qemu.git/blame - hw/ppc/spapr_drc.c
Merge remote-tracking branch 'remotes/jasowang/tags/net-pull-request' into staging
[qemu.git] / hw / ppc / spapr_drc.c
CommitLineData
bbf5c878
MR
1/*
2 * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
3 *
4 * Copyright IBM Corp. 2014
5 *
6 * Authors:
7 * Michael Roth <[email protected]>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
0d75590d 13#include "qemu/osdep.h"
da34e65c 14#include "qapi/error.h"
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 */
24ac7755 23#include "trace.h"
bbf5c878
MR
24
25#define DRC_CONTAINER_PATH "/dr-connector"
26#define DRC_INDEX_TYPE_SHIFT 28
627c2ef7 27#define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
bbf5c878
MR
28
29static sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type)
30{
31 uint32_t shift = 0;
32
33 /* make sure this isn't SPAPR_DR_CONNECTOR_TYPE_ANY, or some
34 * other wonky value.
35 */
36 g_assert(is_power_of_2(type));
37
38 while (type != (1 << shift)) {
39 shift++;
40 }
41 return shift;
42}
43
44static uint32_t get_index(sPAPRDRConnector *drc)
45{
46 /* no set format for a drc index: it only needs to be globally
47 * unique. this is how we encode the DRC type on bare-metal
48 * however, so might as well do that here
49 */
50 return (get_type_shift(drc->type) << DRC_INDEX_TYPE_SHIFT) |
51 (drc->id & DRC_INDEX_ID_MASK);
52}
53
0cb688d2
MR
54static uint32_t set_isolation_state(sPAPRDRConnector *drc,
55 sPAPRDRIsolationState state)
bbf5c878
MR
56{
57 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
58
24ac7755 59 trace_spapr_drc_set_isolation_state(get_index(drc), state);
bbf5c878 60
9d1852ce 61 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
b12227af 62 /* cannot unisolate a non-existent resource, and, or resources
9d1852ce
MR
63 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
64 */
65 if (!drc->dev ||
66 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
67 return RTAS_OUT_NO_SUCH_INDICATOR;
68 }
69 }
70
cf632463
BR
71 /*
72 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
73 * belong to a DIMM device that is marked for removal.
74 *
75 * Currently the guest userspace tool drmgr that drives the memory
76 * hotplug/unplug will just try to remove a set of 'removable' LMBs
77 * in response to a hot unplug request that is based on drc-count.
78 * If the LMB being removed doesn't belong to a DIMM device that is
79 * actually being unplugged, fail the isolation request here.
80 */
81 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_LMB) {
82 if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
83 !drc->awaiting_release) {
84 return RTAS_OUT_HW_ERROR;
85 }
86 }
87
bbf5c878
MR
88 drc->isolation_state = state;
89
90 if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
91 /* if we're awaiting release, but still in an unconfigured state,
92 * it's likely the guest is still in the process of configuring
93 * the device and is transitioning the devices to an ISOLATED
94 * state as a part of that process. so we only complete the
95 * removal when this transition happens for a device in a
96 * configured state, as suggested by the state diagram from
97 * PAPR+ 2.7, 13.4
98 */
99 if (drc->awaiting_release) {
100 if (drc->configured) {
24ac7755 101 trace_spapr_drc_set_isolation_state_finalizing(get_index(drc));
bbf5c878
MR
102 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
103 drc->detach_cb_opaque, NULL);
104 } else {
24ac7755 105 trace_spapr_drc_set_isolation_state_deferring(get_index(drc));
bbf5c878
MR
106 }
107 }
108 drc->configured = false;
109 }
110
0cb688d2 111 return RTAS_OUT_SUCCESS;
bbf5c878
MR
112}
113
0cb688d2
MR
114static uint32_t set_indicator_state(sPAPRDRConnector *drc,
115 sPAPRDRIndicatorState state)
bbf5c878 116{
24ac7755 117 trace_spapr_drc_set_indicator_state(get_index(drc), state);
bbf5c878 118 drc->indicator_state = state;
0cb688d2 119 return RTAS_OUT_SUCCESS;
bbf5c878
MR
120}
121
0cb688d2
MR
122static uint32_t set_allocation_state(sPAPRDRConnector *drc,
123 sPAPRDRAllocationState state)
bbf5c878
MR
124{
125 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
126
24ac7755 127 trace_spapr_drc_set_allocation_state(get_index(drc), state);
bbf5c878 128
9d1852ce
MR
129 if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
130 /* if there's no resource/device associated with the DRC, there's
131 * no way for us to put it in an allocation state consistent with
132 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
133 * result in an RTAS return code of -3 / "no such indicator"
134 */
135 if (!drc->dev) {
136 return RTAS_OUT_NO_SUCH_INDICATOR;
137 }
138 }
139
bbf5c878
MR
140 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
141 drc->allocation_state = state;
142 if (drc->awaiting_release &&
143 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
24ac7755 144 trace_spapr_drc_set_allocation_state_finalizing(get_index(drc));
bbf5c878
MR
145 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
146 drc->detach_cb_opaque, NULL);
aab99135
BR
147 } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
148 drc->awaiting_allocation = false;
bbf5c878
MR
149 }
150 }
0cb688d2 151 return RTAS_OUT_SUCCESS;
bbf5c878
MR
152}
153
154static uint32_t get_type(sPAPRDRConnector *drc)
155{
156 return drc->type;
157}
158
159static const char *get_name(sPAPRDRConnector *drc)
160{
161 return drc->name;
162}
163
164static const void *get_fdt(sPAPRDRConnector *drc, int *fdt_start_offset)
165{
166 if (fdt_start_offset) {
167 *fdt_start_offset = drc->fdt_start_offset;
168 }
169 return drc->fdt;
170}
171
172static void set_configured(sPAPRDRConnector *drc)
173{
24ac7755 174 trace_spapr_drc_set_configured(get_index(drc));
bbf5c878
MR
175
176 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
177 /* guest should be not configuring an isolated device */
24ac7755 178 trace_spapr_drc_set_configured_skipping(get_index(drc));
bbf5c878
MR
179 return;
180 }
181 drc->configured = true;
182}
183
f40eb921
MR
184/* has the guest been notified of device attachment? */
185static void set_signalled(sPAPRDRConnector *drc)
186{
187 drc->signalled = true;
188}
189
bbf5c878
MR
190/*
191 * dr-entity-sense sensor value
192 * returned via get-sensor-state RTAS calls
193 * as expected by state diagram in PAPR+ 2.7, 13.4
194 * based on the current allocation/indicator/power states
195 * for the DR connector.
196 */
0cb688d2 197static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
bbf5c878 198{
bbf5c878
MR
199 if (drc->dev) {
200 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
201 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
202 /* for logical DR, we return a state of UNUSABLE
203 * iff the allocation state UNUSABLE.
204 * Otherwise, report the state as USABLE/PRESENT,
205 * as we would for PCI.
206 */
0cb688d2 207 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
bbf5c878
MR
208 } else {
209 /* this assumes all PCI devices are assigned to
210 * a 'live insertion' power domain, where QEMU
211 * manages power state automatically as opposed
212 * to the guest. present, non-PCI resources are
213 * unaffected by power state.
214 */
0cb688d2 215 *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
bbf5c878
MR
216 }
217 } else {
218 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
219 /* PCI devices, and only PCI devices, use EMPTY
220 * in cases where we'd otherwise use UNUSABLE
221 */
0cb688d2 222 *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
bbf5c878 223 } else {
0cb688d2 224 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
bbf5c878
MR
225 }
226 }
227
24ac7755 228 trace_spapr_drc_entity_sense(get_index(drc), *state);
0cb688d2 229 return RTAS_OUT_SUCCESS;
bbf5c878
MR
230}
231
d7bce999
EB
232static void prop_get_index(Object *obj, Visitor *v, const char *name,
233 void *opaque, Error **errp)
bbf5c878
MR
234{
235 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
236 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
237 uint32_t value = (uint32_t)drck->get_index(drc);
51e72bc1 238 visit_type_uint32(v, name, &value, errp);
bbf5c878
MR
239}
240
d7bce999
EB
241static void prop_get_type(Object *obj, Visitor *v, const char *name,
242 void *opaque, Error **errp)
bbf5c878
MR
243{
244 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
245 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
246 uint32_t value = (uint32_t)drck->get_type(drc);
51e72bc1 247 visit_type_uint32(v, name, &value, errp);
bbf5c878
MR
248}
249
250static char *prop_get_name(Object *obj, Error **errp)
251{
252 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
253 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
254 return g_strdup(drck->get_name(drc));
255}
256
d7bce999
EB
257static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
258 void *opaque, Error **errp)
bbf5c878
MR
259{
260 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
261 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
0cb688d2
MR
262 uint32_t value;
263
264 drck->entity_sense(drc, &value);
51e72bc1 265 visit_type_uint32(v, name, &value, errp);
bbf5c878
MR
266}
267
d7bce999
EB
268static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
269 void *opaque, Error **errp)
bbf5c878
MR
270{
271 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
c75304a1 272 Error *err = NULL;
bbf5c878
MR
273 int fdt_offset_next, fdt_offset, fdt_depth;
274 void *fdt;
275
276 if (!drc->fdt) {
a543a554 277 visit_type_null(v, NULL, errp);
bbf5c878
MR
278 return;
279 }
280
281 fdt = drc->fdt;
282 fdt_offset = drc->fdt_start_offset;
283 fdt_depth = 0;
284
285 do {
286 const char *name = NULL;
287 const struct fdt_property *prop = NULL;
288 int prop_len = 0, name_len = 0;
289 uint32_t tag;
290
291 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
292 switch (tag) {
293 case FDT_BEGIN_NODE:
294 fdt_depth++;
295 name = fdt_get_name(fdt, fdt_offset, &name_len);
337283df 296 visit_start_struct(v, name, NULL, 0, &err);
c75304a1
MA
297 if (err) {
298 error_propagate(errp, err);
299 return;
300 }
bbf5c878
MR
301 break;
302 case FDT_END_NODE:
303 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
304 g_assert(fdt_depth > 0);
15c2f669 305 visit_check_struct(v, &err);
1158bb2a 306 visit_end_struct(v, NULL);
c75304a1
MA
307 if (err) {
308 error_propagate(errp, err);
309 return;
310 }
bbf5c878
MR
311 fdt_depth--;
312 break;
313 case FDT_PROP: {
314 int i;
315 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
316 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
d9f62dde 317 visit_start_list(v, name, NULL, 0, &err);
c75304a1
MA
318 if (err) {
319 error_propagate(errp, err);
320 return;
321 }
bbf5c878 322 for (i = 0; i < prop_len; i++) {
51e72bc1 323 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
c75304a1
MA
324 if (err) {
325 error_propagate(errp, err);
326 return;
327 }
328 }
a4a1c70d 329 visit_check_list(v, &err);
1158bb2a 330 visit_end_list(v, NULL);
a4a1c70d
MA
331 if (err) {
332 error_propagate(errp, err);
333 return;
334 }
bbf5c878
MR
335 break;
336 }
337 default:
338 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
339 }
340 fdt_offset = fdt_offset_next;
341 } while (fdt_depth != 0);
342}
343
344static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
345 int fdt_start_offset, bool coldplug, Error **errp)
346{
24ac7755 347 trace_spapr_drc_attach(get_index(drc));
bbf5c878
MR
348
349 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
350 error_setg(errp, "an attached device is still awaiting release");
351 return;
352 }
353 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
354 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
355 }
356 g_assert(fdt || coldplug);
357
358 /* NOTE: setting initial isolation state to UNISOLATED means we can't
359 * detach unless guest has a userspace/kernel that moves this state
360 * back to ISOLATED in response to an unplug event, or this is done
361 * manually by the admin prior. if we force things while the guest
362 * may be accessing the device, we can easily crash the guest, so we
363 * we defer completion of removal in such cases to the reset() hook.
364 */
365 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
366 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
367 }
368 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
369
370 drc->dev = d;
371 drc->fdt = fdt;
372 drc->fdt_start_offset = fdt_start_offset;
785652dc 373 drc->configured = coldplug;
df18b2db
MR
374 /* 'logical' DR resources such as memory/cpus are in some cases treated
375 * as a pool of resources from which the guest is free to choose from
376 * based on only a count. for resources that can be assigned in this
377 * fashion, we must assume the resource is signalled immediately
378 * since a single hotplug request might make an arbitrary number of
379 * such attached resources available to the guest, as opposed to
380 * 'physical' DR resources such as PCI where each device/resource is
381 * signalled individually.
382 */
383 drc->signalled = (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI)
384 ? true : coldplug;
bbf5c878 385
aab99135
BR
386 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
387 drc->awaiting_allocation = true;
388 }
389
bbf5c878
MR
390 object_property_add_link(OBJECT(drc), "device",
391 object_get_typename(OBJECT(drc->dev)),
392 (Object **)(&drc->dev),
393 NULL, 0, NULL);
394}
395
396static void detach(sPAPRDRConnector *drc, DeviceState *d,
397 spapr_drc_detach_cb *detach_cb,
398 void *detach_cb_opaque, Error **errp)
399{
24ac7755 400 trace_spapr_drc_detach(get_index(drc));
bbf5c878
MR
401
402 drc->detach_cb = detach_cb;
403 drc->detach_cb_opaque = detach_cb_opaque;
404
f40eb921
MR
405 /* if we've signalled device presence to the guest, or if the guest
406 * has gone ahead and configured the device (via manually-executed
407 * device add via drmgr in guest, namely), we need to wait
408 * for the guest to quiesce the device before completing detach.
409 * Otherwise, we can assume the guest hasn't seen it and complete the
410 * detach immediately. Note that there is a small race window
411 * just before, or during, configuration, which is this context
412 * refers mainly to fetching the device tree via RTAS.
413 * During this window the device access will be arbitrated by
414 * associated DRC, which will simply fail the RTAS calls as invalid.
415 * This is recoverable within guest and current implementations of
416 * drmgr should be able to cope.
417 */
418 if (!drc->signalled && !drc->configured) {
419 /* if the guest hasn't seen the device we can't rely on it to
420 * set it back to an isolated state via RTAS, so do it here manually
421 */
422 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
423 }
424
bbf5c878 425 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
24ac7755 426 trace_spapr_drc_awaiting_isolated(get_index(drc));
bbf5c878
MR
427 drc->awaiting_release = true;
428 return;
429 }
430
431 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
432 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
24ac7755 433 trace_spapr_drc_awaiting_unusable(get_index(drc));
bbf5c878
MR
434 drc->awaiting_release = true;
435 return;
436 }
437
aab99135
BR
438 if (drc->awaiting_allocation) {
439 drc->awaiting_release = true;
24ac7755 440 trace_spapr_drc_awaiting_allocation(get_index(drc));
aab99135
BR
441 return;
442 }
443
bbf5c878
MR
444 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
445
446 if (drc->detach_cb) {
447 drc->detach_cb(drc->dev, drc->detach_cb_opaque);
448 }
449
450 drc->awaiting_release = false;
451 g_free(drc->fdt);
452 drc->fdt = NULL;
453 drc->fdt_start_offset = 0;
454 object_property_del(OBJECT(drc), "device", NULL);
455 drc->dev = NULL;
456 drc->detach_cb = NULL;
457 drc->detach_cb_opaque = NULL;
458}
459
460static bool release_pending(sPAPRDRConnector *drc)
461{
462 return drc->awaiting_release;
463}
464
465static void reset(DeviceState *d)
466{
467 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
468 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
f40eb921 469 sPAPRDREntitySense state;
bbf5c878 470
24ac7755 471 trace_spapr_drc_reset(drck->get_index(drc));
bbf5c878
MR
472 /* immediately upon reset we can safely assume DRCs whose devices
473 * are pending removal can be safely removed, and that they will
474 * subsequently be left in an ISOLATED state. move the DRC to this
475 * state in these cases (which will in turn complete any pending
476 * device removals)
477 */
478 if (drc->awaiting_release) {
479 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
480 /* generally this should also finalize the removal, but if the device
481 * hasn't yet been configured we normally defer removal under the
482 * assumption that this transition is taking place as part of device
483 * configuration. so check if we're still waiting after this, and
484 * force removal if we are
485 */
486 if (drc->awaiting_release) {
487 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb,
488 drc->detach_cb_opaque, NULL);
489 }
490
491 /* non-PCI devices may be awaiting a transition to UNUSABLE */
492 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
493 drc->awaiting_release) {
494 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
495 }
496 }
f40eb921
MR
497
498 drck->entity_sense(drc, &state);
499 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
500 drck->set_signalled(drc);
501 }
bbf5c878
MR
502}
503
504static void realize(DeviceState *d, Error **errp)
505{
506 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
507 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
508 Object *root_container;
509 char link_name[256];
510 gchar *child_name;
511 Error *err = NULL;
512
24ac7755 513 trace_spapr_drc_realize(drck->get_index(drc));
bbf5c878
MR
514 /* NOTE: we do this as part of realize/unrealize due to the fact
515 * that the guest will communicate with the DRC via RTAS calls
516 * referencing the global DRC index. By unlinking the DRC
517 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
518 * inaccessible by the guest, since lookups rely on this path
519 * existing in the composition tree
520 */
521 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
522 snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
523 child_name = object_get_canonical_path_component(OBJECT(drc));
24ac7755 524 trace_spapr_drc_realize_child(drck->get_index(drc), child_name);
bbf5c878
MR
525 object_property_add_alias(root_container, link_name,
526 drc->owner, child_name, &err);
527 if (err) {
4fffeb5e 528 error_report_err(err);
bbf5c878
MR
529 object_unref(OBJECT(drc));
530 }
586d2142 531 g_free(child_name);
24ac7755 532 trace_spapr_drc_realize_complete(drck->get_index(drc));
bbf5c878
MR
533}
534
535static void unrealize(DeviceState *d, Error **errp)
536{
537 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
538 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
539 Object *root_container;
540 char name[256];
541 Error *err = NULL;
542
24ac7755 543 trace_spapr_drc_unrealize(drck->get_index(drc));
bbf5c878
MR
544 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
545 snprintf(name, sizeof(name), "%x", drck->get_index(drc));
546 object_property_del(root_container, name, &err);
547 if (err) {
4fffeb5e 548 error_report_err(err);
bbf5c878
MR
549 object_unref(OBJECT(drc));
550 }
551}
552
553sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
554 sPAPRDRConnectorType type,
555 uint32_t id)
556{
557 sPAPRDRConnector *drc =
558 SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
94649d42 559 char *prop_name;
bbf5c878
MR
560
561 g_assert(type);
562
563 drc->type = type;
564 drc->id = id;
565 drc->owner = owner;
94649d42
DG
566 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
567 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
bbf5c878 568 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
94649d42 569 g_free(prop_name);
bbf5c878
MR
570
571 /* human-readable name for a DRC to encode into the DT
572 * description. this is mainly only used within a guest in place
573 * of the unique DRC index.
574 *
575 * in the case of VIO/PCI devices, it corresponds to a
576 * "location code" that maps a logical device/function (DRC index)
577 * to a physical (or virtual in the case of VIO) location in the
578 * system by chaining together the "location label" for each
579 * encapsulating component.
580 *
581 * since this is more to do with diagnosing physical hardware
582 * issues than guest compatibility, we choose location codes/DRC
583 * names that adhere to the documented format, but avoid encoding
584 * the entire topology information into the label/code, instead
585 * just using the location codes based on the labels for the
586 * endpoints (VIO/PCI adaptor connectors), which is basically
587 * just "C" followed by an integer ID.
588 *
589 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
590 * location codes as documented by PAPR+ v2.7, 12.3.1.5
591 */
592 switch (drc->type) {
593 case SPAPR_DR_CONNECTOR_TYPE_CPU:
594 drc->name = g_strdup_printf("CPU %d", id);
595 break;
596 case SPAPR_DR_CONNECTOR_TYPE_PHB:
597 drc->name = g_strdup_printf("PHB %d", id);
598 break;
599 case SPAPR_DR_CONNECTOR_TYPE_VIO:
600 case SPAPR_DR_CONNECTOR_TYPE_PCI:
601 drc->name = g_strdup_printf("C%d", id);
602 break;
603 case SPAPR_DR_CONNECTOR_TYPE_LMB:
604 drc->name = g_strdup_printf("LMB %d", id);
605 break;
606 default:
607 g_assert(false);
608 }
609
610 /* PCI slot always start in a USABLE state, and stay there */
611 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
612 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
613 }
614
615 return drc;
616}
617
618static void spapr_dr_connector_instance_init(Object *obj)
619{
620 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
621
622 object_property_add_uint32_ptr(obj, "isolation-state",
623 &drc->isolation_state, NULL);
624 object_property_add_uint32_ptr(obj, "indicator-state",
625 &drc->indicator_state, NULL);
626 object_property_add_uint32_ptr(obj, "allocation-state",
627 &drc->allocation_state, NULL);
628 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
629 object_property_add(obj, "index", "uint32", prop_get_index,
630 NULL, NULL, NULL, NULL);
631 object_property_add(obj, "connector_type", "uint32", prop_get_type,
632 NULL, NULL, NULL, NULL);
633 object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
634 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
635 NULL, NULL, NULL, NULL);
636 object_property_add(obj, "fdt", "struct", prop_get_fdt,
637 NULL, NULL, NULL, NULL);
638}
639
640static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
641{
642 DeviceClass *dk = DEVICE_CLASS(k);
643 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
644
645 dk->reset = reset;
646 dk->realize = realize;
647 dk->unrealize = unrealize;
648 drck->set_isolation_state = set_isolation_state;
649 drck->set_indicator_state = set_indicator_state;
650 drck->set_allocation_state = set_allocation_state;
651 drck->get_index = get_index;
652 drck->get_type = get_type;
653 drck->get_name = get_name;
654 drck->get_fdt = get_fdt;
655 drck->set_configured = set_configured;
656 drck->entity_sense = entity_sense;
657 drck->attach = attach;
658 drck->detach = detach;
659 drck->release_pending = release_pending;
f40eb921 660 drck->set_signalled = set_signalled;
c401ae8c
MA
661 /*
662 * Reason: it crashes FIXME find and document the real reason
663 */
664 dk->cannot_instantiate_with_device_add_yet = true;
bbf5c878
MR
665}
666
667static const TypeInfo spapr_dr_connector_info = {
668 .name = TYPE_SPAPR_DR_CONNECTOR,
669 .parent = TYPE_DEVICE,
670 .instance_size = sizeof(sPAPRDRConnector),
671 .instance_init = spapr_dr_connector_instance_init,
672 .class_size = sizeof(sPAPRDRConnectorClass),
673 .class_init = spapr_dr_connector_class_init,
674};
675
676static void spapr_drc_register_types(void)
677{
678 type_register_static(&spapr_dr_connector_info);
679}
680
681type_init(spapr_drc_register_types)
682
683/* helper functions for external users */
684
685sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
686{
687 Object *obj;
688 char name[256];
689
690 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
691 obj = object_resolve_path(name, NULL);
692
693 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
694}
695
696sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
697 uint32_t id)
698{
699 return spapr_dr_connector_by_index(
700 (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
701 (id & DRC_INDEX_ID_MASK));
702}
e4b798bb
MR
703
704/* generate a string the describes the DRC to encode into the
705 * device tree.
706 *
707 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
708 */
709static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
710{
711 switch (type) {
712 case SPAPR_DR_CONNECTOR_TYPE_CPU:
713 return "CPU";
714 case SPAPR_DR_CONNECTOR_TYPE_PHB:
715 return "PHB";
716 case SPAPR_DR_CONNECTOR_TYPE_VIO:
717 return "SLOT";
718 case SPAPR_DR_CONNECTOR_TYPE_PCI:
719 return "28";
720 case SPAPR_DR_CONNECTOR_TYPE_LMB:
721 return "MEM";
722 default:
723 g_assert(false);
724 }
725
726 return NULL;
727}
728
729/**
730 * spapr_drc_populate_dt
731 *
732 * @fdt: libfdt device tree
733 * @path: path in the DT to generate properties
734 * @owner: parent Object/DeviceState for which to generate DRC
735 * descriptions for
736 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
737 * to the types of DRCs to generate entries for
738 *
739 * generate OF properties to describe DRC topology/indices to guests
740 *
741 * as documented in PAPR+ v2.1, 13.5.2
742 */
743int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
744 uint32_t drc_type_mask)
745{
746 Object *root_container;
747 ObjectProperty *prop;
7746abd8 748 ObjectPropertyIterator iter;
e4b798bb
MR
749 uint32_t drc_count = 0;
750 GArray *drc_indexes, *drc_power_domains;
751 GString *drc_names, *drc_types;
752 int ret;
753
754 /* the first entry of each properties is a 32-bit integer encoding
755 * the number of elements in the array. we won't know this until
756 * we complete the iteration through all the matching DRCs, but
757 * reserve the space now and set the offsets accordingly so we
758 * can fill them in later.
759 */
760 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
761 drc_indexes = g_array_set_size(drc_indexes, 1);
762 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
763 drc_power_domains = g_array_set_size(drc_power_domains, 1);
764 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
765 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
766
767 /* aliases for all DRConnector objects will be rooted in QOM
768 * composition tree at DRC_CONTAINER_PATH
769 */
770 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
771
7746abd8
DB
772 object_property_iter_init(&iter, root_container);
773 while ((prop = object_property_iter_next(&iter))) {
e4b798bb
MR
774 Object *obj;
775 sPAPRDRConnector *drc;
776 sPAPRDRConnectorClass *drck;
777 uint32_t drc_index, drc_power_domain;
778
779 if (!strstart(prop->type, "link<", NULL)) {
780 continue;
781 }
782
783 obj = object_property_get_link(root_container, prop->name, NULL);
784 drc = SPAPR_DR_CONNECTOR(obj);
785 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
786
787 if (owner && (drc->owner != owner)) {
788 continue;
789 }
790
791 if ((drc->type & drc_type_mask) == 0) {
792 continue;
793 }
794
795 drc_count++;
796
797 /* ibm,drc-indexes */
798 drc_index = cpu_to_be32(drck->get_index(drc));
799 g_array_append_val(drc_indexes, drc_index);
800
801 /* ibm,drc-power-domains */
802 drc_power_domain = cpu_to_be32(-1);
803 g_array_append_val(drc_power_domains, drc_power_domain);
804
805 /* ibm,drc-names */
806 drc_names = g_string_append(drc_names, drck->get_name(drc));
807 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
808
809 /* ibm,drc-types */
810 drc_types = g_string_append(drc_types,
811 spapr_drc_get_type_str(drc->type));
812 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
813 }
814
815 /* now write the drc count into the space we reserved at the
816 * beginning of the arrays previously
817 */
818 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
819 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
820 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
821 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
822
823 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
824 drc_indexes->data,
825 drc_indexes->len * sizeof(uint32_t));
826 if (ret) {
ce9863b7 827 error_report("Couldn't create ibm,drc-indexes property");
e4b798bb
MR
828 goto out;
829 }
830
831 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
832 drc_power_domains->data,
833 drc_power_domains->len * sizeof(uint32_t));
834 if (ret) {
ce9863b7 835 error_report("Couldn't finalize ibm,drc-power-domains property");
e4b798bb
MR
836 goto out;
837 }
838
839 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
840 drc_names->str, drc_names->len);
841 if (ret) {
ce9863b7 842 error_report("Couldn't finalize ibm,drc-names property");
e4b798bb
MR
843 goto out;
844 }
845
846 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
847 drc_types->str, drc_types->len);
848 if (ret) {
ce9863b7 849 error_report("Couldn't finalize ibm,drc-types property");
e4b798bb
MR
850 goto out;
851 }
852
853out:
854 g_array_free(drc_indexes, true);
855 g_array_free(drc_power_domains, true);
856 g_string_free(drc_names, true);
857 g_string_free(drc_types, true);
858
859 return ret;
860}
This page took 0.264535 seconds and 4 git commands to generate.