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