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