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