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_type_null(v, NULL, errp); 273 return; 274 } 275 276 fdt = drc->fdt; 277 fdt_offset = drc->fdt_start_offset; 278 fdt_depth = 0; 279 280 do { 281 const char *name = NULL; 282 const struct fdt_property *prop = NULL; 283 int prop_len = 0, name_len = 0; 284 uint32_t tag; 285 286 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next); 287 switch (tag) { 288 case FDT_BEGIN_NODE: 289 fdt_depth++; 290 name = fdt_get_name(fdt, fdt_offset, &name_len); 291 visit_start_struct(v, name, NULL, 0, &err); 292 if (err) { 293 error_propagate(errp, err); 294 return; 295 } 296 break; 297 case FDT_END_NODE: 298 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */ 299 g_assert(fdt_depth > 0); 300 visit_check_struct(v, &err); 301 visit_end_struct(v); 302 if (err) { 303 error_propagate(errp, err); 304 return; 305 } 306 fdt_depth--; 307 break; 308 case FDT_PROP: { 309 int i; 310 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len); 311 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); 312 visit_start_list(v, name, NULL, 0, &err); 313 if (err) { 314 error_propagate(errp, err); 315 return; 316 } 317 for (i = 0; i < prop_len; i++) { 318 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err); 319 if (err) { 320 error_propagate(errp, err); 321 return; 322 } 323 } 324 visit_end_list(v); 325 break; 326 } 327 default: 328 error_setg(&error_abort, "device FDT in unexpected state: %d", tag); 329 } 330 fdt_offset = fdt_offset_next; 331 } while (fdt_depth != 0); 332 } 333 334 static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt, 335 int fdt_start_offset, bool coldplug, Error **errp) 336 { 337 DPRINTFN("drc: %x, attach", get_index(drc)); 338 339 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) { 340 error_setg(errp, "an attached device is still awaiting release"); 341 return; 342 } 343 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) { 344 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE); 345 } 346 g_assert(fdt || coldplug); 347 348 /* NOTE: setting initial isolation state to UNISOLATED means we can't 349 * detach unless guest has a userspace/kernel that moves this state 350 * back to ISOLATED in response to an unplug event, or this is done 351 * manually by the admin prior. if we force things while the guest 352 * may be accessing the device, we can easily crash the guest, so we 353 * we defer completion of removal in such cases to the reset() hook. 354 */ 355 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) { 356 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED; 357 } 358 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE; 359 360 drc->dev = d; 361 drc->fdt = fdt; 362 drc->fdt_start_offset = fdt_start_offset; 363 drc->configured = coldplug; 364 /* 'logical' DR resources such as memory/cpus are in some cases treated 365 * as a pool of resources from which the guest is free to choose from 366 * based on only a count. for resources that can be assigned in this 367 * fashion, we must assume the resource is signalled immediately 368 * since a single hotplug request might make an arbitrary number of 369 * such attached resources available to the guest, as opposed to 370 * 'physical' DR resources such as PCI where each device/resource is 371 * signalled individually. 372 */ 373 drc->signalled = (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) 374 ? true : coldplug; 375 376 object_property_add_link(OBJECT(drc), "device", 377 object_get_typename(OBJECT(drc->dev)), 378 (Object **)(&drc->dev), 379 NULL, 0, NULL); 380 } 381 382 static void detach(sPAPRDRConnector *drc, DeviceState *d, 383 spapr_drc_detach_cb *detach_cb, 384 void *detach_cb_opaque, Error **errp) 385 { 386 DPRINTFN("drc: %x, detach", get_index(drc)); 387 388 drc->detach_cb = detach_cb; 389 drc->detach_cb_opaque = detach_cb_opaque; 390 391 /* if we've signalled device presence to the guest, or if the guest 392 * has gone ahead and configured the device (via manually-executed 393 * device add via drmgr in guest, namely), we need to wait 394 * for the guest to quiesce the device before completing detach. 395 * Otherwise, we can assume the guest hasn't seen it and complete the 396 * detach immediately. Note that there is a small race window 397 * just before, or during, configuration, which is this context 398 * refers mainly to fetching the device tree via RTAS. 399 * During this window the device access will be arbitrated by 400 * associated DRC, which will simply fail the RTAS calls as invalid. 401 * This is recoverable within guest and current implementations of 402 * drmgr should be able to cope. 403 */ 404 if (!drc->signalled && !drc->configured) { 405 /* if the guest hasn't seen the device we can't rely on it to 406 * set it back to an isolated state via RTAS, so do it here manually 407 */ 408 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED; 409 } 410 411 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) { 412 DPRINTFN("awaiting transition to isolated state before removal"); 413 drc->awaiting_release = true; 414 return; 415 } 416 417 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI && 418 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) { 419 DPRINTFN("awaiting transition to unusable state before removal"); 420 drc->awaiting_release = true; 421 return; 422 } 423 424 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE; 425 426 if (drc->detach_cb) { 427 drc->detach_cb(drc->dev, drc->detach_cb_opaque); 428 } 429 430 drc->awaiting_release = false; 431 g_free(drc->fdt); 432 drc->fdt = NULL; 433 drc->fdt_start_offset = 0; 434 object_property_del(OBJECT(drc), "device", NULL); 435 drc->dev = NULL; 436 drc->detach_cb = NULL; 437 drc->detach_cb_opaque = NULL; 438 } 439 440 static bool release_pending(sPAPRDRConnector *drc) 441 { 442 return drc->awaiting_release; 443 } 444 445 static void reset(DeviceState *d) 446 { 447 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 448 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 449 sPAPRDREntitySense state; 450 451 DPRINTFN("drc reset: %x", drck->get_index(drc)); 452 /* immediately upon reset we can safely assume DRCs whose devices 453 * are pending removal can be safely removed, and that they will 454 * subsequently be left in an ISOLATED state. move the DRC to this 455 * state in these cases (which will in turn complete any pending 456 * device removals) 457 */ 458 if (drc->awaiting_release) { 459 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED); 460 /* generally this should also finalize the removal, but if the device 461 * hasn't yet been configured we normally defer removal under the 462 * assumption that this transition is taking place as part of device 463 * configuration. so check if we're still waiting after this, and 464 * force removal if we are 465 */ 466 if (drc->awaiting_release) { 467 drck->detach(drc, DEVICE(drc->dev), drc->detach_cb, 468 drc->detach_cb_opaque, NULL); 469 } 470 471 /* non-PCI devices may be awaiting a transition to UNUSABLE */ 472 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI && 473 drc->awaiting_release) { 474 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE); 475 } 476 } 477 478 drck->entity_sense(drc, &state); 479 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) { 480 drck->set_signalled(drc); 481 } 482 } 483 484 static void realize(DeviceState *d, Error **errp) 485 { 486 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 487 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 488 Object *root_container; 489 char link_name[256]; 490 gchar *child_name; 491 Error *err = NULL; 492 493 DPRINTFN("drc realize: %x", drck->get_index(drc)); 494 /* NOTE: we do this as part of realize/unrealize due to the fact 495 * that the guest will communicate with the DRC via RTAS calls 496 * referencing the global DRC index. By unlinking the DRC 497 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it 498 * inaccessible by the guest, since lookups rely on this path 499 * existing in the composition tree 500 */ 501 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 502 snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc)); 503 child_name = object_get_canonical_path_component(OBJECT(drc)); 504 DPRINTFN("drc child name: %s", child_name); 505 object_property_add_alias(root_container, link_name, 506 drc->owner, child_name, &err); 507 if (err) { 508 error_report_err(err); 509 object_unref(OBJECT(drc)); 510 } 511 g_free(child_name); 512 DPRINTFN("drc realize complete"); 513 } 514 515 static void unrealize(DeviceState *d, Error **errp) 516 { 517 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 518 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 519 Object *root_container; 520 char name[256]; 521 Error *err = NULL; 522 523 DPRINTFN("drc unrealize: %x", drck->get_index(drc)); 524 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 525 snprintf(name, sizeof(name), "%x", drck->get_index(drc)); 526 object_property_del(root_container, name, &err); 527 if (err) { 528 error_report_err(err); 529 object_unref(OBJECT(drc)); 530 } 531 } 532 533 sPAPRDRConnector *spapr_dr_connector_new(Object *owner, 534 sPAPRDRConnectorType type, 535 uint32_t id) 536 { 537 sPAPRDRConnector *drc = 538 SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR)); 539 char *prop_name; 540 541 g_assert(type); 542 543 drc->type = type; 544 drc->id = id; 545 drc->owner = owner; 546 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc)); 547 object_property_add_child(owner, prop_name, OBJECT(drc), NULL); 548 object_property_set_bool(OBJECT(drc), true, "realized", NULL); 549 g_free(prop_name); 550 551 /* human-readable name for a DRC to encode into the DT 552 * description. this is mainly only used within a guest in place 553 * of the unique DRC index. 554 * 555 * in the case of VIO/PCI devices, it corresponds to a 556 * "location code" that maps a logical device/function (DRC index) 557 * to a physical (or virtual in the case of VIO) location in the 558 * system by chaining together the "location label" for each 559 * encapsulating component. 560 * 561 * since this is more to do with diagnosing physical hardware 562 * issues than guest compatibility, we choose location codes/DRC 563 * names that adhere to the documented format, but avoid encoding 564 * the entire topology information into the label/code, instead 565 * just using the location codes based on the labels for the 566 * endpoints (VIO/PCI adaptor connectors), which is basically 567 * just "C" followed by an integer ID. 568 * 569 * DRC names as documented by PAPR+ v2.7, 13.5.2.4 570 * location codes as documented by PAPR+ v2.7, 12.3.1.5 571 */ 572 switch (drc->type) { 573 case SPAPR_DR_CONNECTOR_TYPE_CPU: 574 drc->name = g_strdup_printf("CPU %d", id); 575 break; 576 case SPAPR_DR_CONNECTOR_TYPE_PHB: 577 drc->name = g_strdup_printf("PHB %d", id); 578 break; 579 case SPAPR_DR_CONNECTOR_TYPE_VIO: 580 case SPAPR_DR_CONNECTOR_TYPE_PCI: 581 drc->name = g_strdup_printf("C%d", id); 582 break; 583 case SPAPR_DR_CONNECTOR_TYPE_LMB: 584 drc->name = g_strdup_printf("LMB %d", id); 585 break; 586 default: 587 g_assert(false); 588 } 589 590 /* PCI slot always start in a USABLE state, and stay there */ 591 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) { 592 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE; 593 } 594 595 return drc; 596 } 597 598 static void spapr_dr_connector_instance_init(Object *obj) 599 { 600 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 601 602 object_property_add_uint32_ptr(obj, "isolation-state", 603 &drc->isolation_state, NULL); 604 object_property_add_uint32_ptr(obj, "indicator-state", 605 &drc->indicator_state, NULL); 606 object_property_add_uint32_ptr(obj, "allocation-state", 607 &drc->allocation_state, NULL); 608 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL); 609 object_property_add(obj, "index", "uint32", prop_get_index, 610 NULL, NULL, NULL, NULL); 611 object_property_add(obj, "connector_type", "uint32", prop_get_type, 612 NULL, NULL, NULL, NULL); 613 object_property_add_str(obj, "name", prop_get_name, NULL, NULL); 614 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense, 615 NULL, NULL, NULL, NULL); 616 object_property_add(obj, "fdt", "struct", prop_get_fdt, 617 NULL, NULL, NULL, NULL); 618 } 619 620 static void spapr_dr_connector_class_init(ObjectClass *k, void *data) 621 { 622 DeviceClass *dk = DEVICE_CLASS(k); 623 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); 624 625 dk->reset = reset; 626 dk->realize = realize; 627 dk->unrealize = unrealize; 628 drck->set_isolation_state = set_isolation_state; 629 drck->set_indicator_state = set_indicator_state; 630 drck->set_allocation_state = set_allocation_state; 631 drck->get_index = get_index; 632 drck->get_type = get_type; 633 drck->get_name = get_name; 634 drck->get_fdt = get_fdt; 635 drck->set_configured = set_configured; 636 drck->entity_sense = entity_sense; 637 drck->attach = attach; 638 drck->detach = detach; 639 drck->release_pending = release_pending; 640 drck->set_signalled = set_signalled; 641 /* 642 * Reason: it crashes FIXME find and document the real reason 643 */ 644 dk->cannot_instantiate_with_device_add_yet = true; 645 } 646 647 static const TypeInfo spapr_dr_connector_info = { 648 .name = TYPE_SPAPR_DR_CONNECTOR, 649 .parent = TYPE_DEVICE, 650 .instance_size = sizeof(sPAPRDRConnector), 651 .instance_init = spapr_dr_connector_instance_init, 652 .class_size = sizeof(sPAPRDRConnectorClass), 653 .class_init = spapr_dr_connector_class_init, 654 }; 655 656 static void spapr_drc_register_types(void) 657 { 658 type_register_static(&spapr_dr_connector_info); 659 } 660 661 type_init(spapr_drc_register_types) 662 663 /* helper functions for external users */ 664 665 sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index) 666 { 667 Object *obj; 668 char name[256]; 669 670 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index); 671 obj = object_resolve_path(name, NULL); 672 673 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj); 674 } 675 676 sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type, 677 uint32_t id) 678 { 679 return spapr_dr_connector_by_index( 680 (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) | 681 (id & DRC_INDEX_ID_MASK)); 682 } 683 684 /* generate a string the describes the DRC to encode into the 685 * device tree. 686 * 687 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1 688 */ 689 static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type) 690 { 691 switch (type) { 692 case SPAPR_DR_CONNECTOR_TYPE_CPU: 693 return "CPU"; 694 case SPAPR_DR_CONNECTOR_TYPE_PHB: 695 return "PHB"; 696 case SPAPR_DR_CONNECTOR_TYPE_VIO: 697 return "SLOT"; 698 case SPAPR_DR_CONNECTOR_TYPE_PCI: 699 return "28"; 700 case SPAPR_DR_CONNECTOR_TYPE_LMB: 701 return "MEM"; 702 default: 703 g_assert(false); 704 } 705 706 return NULL; 707 } 708 709 /** 710 * spapr_drc_populate_dt 711 * 712 * @fdt: libfdt device tree 713 * @path: path in the DT to generate properties 714 * @owner: parent Object/DeviceState for which to generate DRC 715 * descriptions for 716 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding 717 * to the types of DRCs to generate entries for 718 * 719 * generate OF properties to describe DRC topology/indices to guests 720 * 721 * as documented in PAPR+ v2.1, 13.5.2 722 */ 723 int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner, 724 uint32_t drc_type_mask) 725 { 726 Object *root_container; 727 ObjectProperty *prop; 728 ObjectPropertyIterator iter; 729 uint32_t drc_count = 0; 730 GArray *drc_indexes, *drc_power_domains; 731 GString *drc_names, *drc_types; 732 int ret; 733 734 /* the first entry of each properties is a 32-bit integer encoding 735 * the number of elements in the array. we won't know this until 736 * we complete the iteration through all the matching DRCs, but 737 * reserve the space now and set the offsets accordingly so we 738 * can fill them in later. 739 */ 740 drc_indexes = g_array_new(false, true, sizeof(uint32_t)); 741 drc_indexes = g_array_set_size(drc_indexes, 1); 742 drc_power_domains = g_array_new(false, true, sizeof(uint32_t)); 743 drc_power_domains = g_array_set_size(drc_power_domains, 1); 744 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); 745 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); 746 747 /* aliases for all DRConnector objects will be rooted in QOM 748 * composition tree at DRC_CONTAINER_PATH 749 */ 750 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 751 752 object_property_iter_init(&iter, root_container); 753 while ((prop = object_property_iter_next(&iter))) { 754 Object *obj; 755 sPAPRDRConnector *drc; 756 sPAPRDRConnectorClass *drck; 757 uint32_t drc_index, drc_power_domain; 758 759 if (!strstart(prop->type, "link<", NULL)) { 760 continue; 761 } 762 763 obj = object_property_get_link(root_container, prop->name, NULL); 764 drc = SPAPR_DR_CONNECTOR(obj); 765 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 766 767 if (owner && (drc->owner != owner)) { 768 continue; 769 } 770 771 if ((drc->type & drc_type_mask) == 0) { 772 continue; 773 } 774 775 drc_count++; 776 777 /* ibm,drc-indexes */ 778 drc_index = cpu_to_be32(drck->get_index(drc)); 779 g_array_append_val(drc_indexes, drc_index); 780 781 /* ibm,drc-power-domains */ 782 drc_power_domain = cpu_to_be32(-1); 783 g_array_append_val(drc_power_domains, drc_power_domain); 784 785 /* ibm,drc-names */ 786 drc_names = g_string_append(drc_names, drck->get_name(drc)); 787 drc_names = g_string_insert_len(drc_names, -1, "\0", 1); 788 789 /* ibm,drc-types */ 790 drc_types = g_string_append(drc_types, 791 spapr_drc_get_type_str(drc->type)); 792 drc_types = g_string_insert_len(drc_types, -1, "\0", 1); 793 } 794 795 /* now write the drc count into the space we reserved at the 796 * beginning of the arrays previously 797 */ 798 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count); 799 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count); 800 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count); 801 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count); 802 803 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes", 804 drc_indexes->data, 805 drc_indexes->len * sizeof(uint32_t)); 806 if (ret) { 807 fprintf(stderr, "Couldn't create ibm,drc-indexes property\n"); 808 goto out; 809 } 810 811 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains", 812 drc_power_domains->data, 813 drc_power_domains->len * sizeof(uint32_t)); 814 if (ret) { 815 fprintf(stderr, "Couldn't finalize ibm,drc-power-domains property\n"); 816 goto out; 817 } 818 819 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names", 820 drc_names->str, drc_names->len); 821 if (ret) { 822 fprintf(stderr, "Couldn't finalize ibm,drc-names property\n"); 823 goto out; 824 } 825 826 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types", 827 drc_types->str, drc_types->len); 828 if (ret) { 829 fprintf(stderr, "Couldn't finalize ibm,drc-types property\n"); 830 goto out; 831 } 832 833 out: 834 g_array_free(drc_indexes, true); 835 g_array_free(drc_power_domains, true); 836 g_string_free(drc_names, true); 837 g_string_free(drc_types, true); 838 839 return ret; 840 } 841