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