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