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