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