spapr_drc.c 35.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
 *
 * Copyright IBM Corp. 2014
 *
 * Authors:
 *  Michael Roth      <mdroth@linux.vnet.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

P
Peter Maydell 已提交
13
#include "qemu/osdep.h"
14
#include "qapi/error.h"
15
#include "cpu.h"
16
#include "qemu/cutils.h"
17 18 19 20 21
#include "hw/ppc/spapr_drc.h"
#include "qom/object.h"
#include "hw/qdev.h"
#include "qapi/visitor.h"
#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 27

#define DRC_CONTAINER_PATH "/dr-connector"
#define DRC_INDEX_TYPE_SHIFT 28
28
#define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
29

D
David Gibson 已提交
30 31 32 33 34 35 36
sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc)
{
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);

    return 1 << drck->typeshift;
}

37
uint32_t spapr_drc_index(sPAPRDRConnector *drc)
38
{
D
David Gibson 已提交
39 40
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);

41 42 43 44
    /* no set format for a drc index: it only needs to be globally
     * unique. this is how we encode the DRC type on bare-metal
     * however, so might as well do that here
     */
D
David Gibson 已提交
45 46
    return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
        | (drc->id & DRC_INDEX_ID_MASK);
47 48
}

49 50
static uint32_t set_isolation_state(sPAPRDRConnector *drc,
                                    sPAPRDRIsolationState state)
51
{
52
    trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
53

54 55 56 57 58 59 60 61 62 63
    /* if the guest is configuring a device attached to this DRC, we
     * should reset the configuration state at this point since it may
     * no longer be reliable (guest released device and needs to start
     * over, or unplug occurred so the FDT is no longer valid)
     */
    if (state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
        g_free(drc->ccs);
        drc->ccs = NULL;
    }

64
    if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
S
Stefan Weil 已提交
65
        /* cannot unisolate a non-existent resource, and, or resources
66 67 68 69 70 71 72 73
         * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
         */
        if (!drc->dev ||
            drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
            return RTAS_OUT_NO_SUCH_INDICATOR;
        }
    }

B
Bharata B Rao 已提交
74 75 76 77 78 79 80 81 82 83
    /*
     * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
     * belong to a DIMM device that is marked for removal.
     *
     * Currently the guest userspace tool drmgr that drives the memory
     * hotplug/unplug will just try to remove a set of 'removable' LMBs
     * in response to a hot unplug request that is based on drc-count.
     * If the LMB being removed doesn't belong to a DIMM device that is
     * actually being unplugged, fail the isolation request here.
     */
D
David Gibson 已提交
84
    if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
B
Bharata B Rao 已提交
85 86 87 88 89 90
        if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
             !drc->awaiting_release) {
            return RTAS_OUT_HW_ERROR;
        }
    }

91 92 93 94 95 96 97 98 99 100 101 102
    drc->isolation_state = state;

    if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
        /* if we're awaiting release, but still in an unconfigured state,
         * it's likely the guest is still in the process of configuring
         * the device and is transitioning the devices to an ISOLATED
         * state as a part of that process. so we only complete the
         * removal when this transition happens for a device in a
         * configured state, as suggested by the state diagram from
         * PAPR+ 2.7, 13.4
         */
        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 112 113
            }
        }
        drc->configured = false;
    }

114
    return RTAS_OUT_SUCCESS;
115 116
}

117 118
static uint32_t set_allocation_state(sPAPRDRConnector *drc,
                                     sPAPRDRAllocationState state)
119
{
120
    trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
121

122 123 124 125 126 127 128 129 130 131 132
    if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
        /* if there's no resource/device associated with the DRC, there's
         * no way for us to put it in an allocation state consistent with
         * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
         * result in an RTAS return code of -3 / "no such indicator"
         */
        if (!drc->dev) {
            return RTAS_OUT_NO_SUCH_INDICATOR;
        }
    }

D
David Gibson 已提交
133
    if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
134 135 136
        drc->allocation_state = state;
        if (drc->awaiting_release &&
            drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
137 138
            uint32_t drc_index = spapr_drc_index(drc);
            trace_spapr_drc_set_allocation_state_finalizing(drc_index);
139
            spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
140 141
        } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
            drc->awaiting_allocation = false;
142 143
        }
    }
144
    return RTAS_OUT_SUCCESS;
145 146
}

D
David Gibson 已提交
147
static const char *spapr_drc_name(sPAPRDRConnector *drc)
148
{
D
David Gibson 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);

    /* human-readable name for a DRC to encode into the DT
     * description. this is mainly only used within a guest in place
     * of the unique DRC index.
     *
     * in the case of VIO/PCI devices, it corresponds to a "location
     * code" that maps a logical device/function (DRC index) to a
     * physical (or virtual in the case of VIO) location in the system
     * by chaining together the "location label" for each
     * encapsulating component.
     *
     * since this is more to do with diagnosing physical hardware
     * issues than guest compatibility, we choose location codes/DRC
     * names that adhere to the documented format, but avoid encoding
     * the entire topology information into the label/code, instead
     * just using the location codes based on the labels for the
     * endpoints (VIO/PCI adaptor connectors), which is basically just
     * "C" followed by an integer ID.
     *
     * DRC names as documented by PAPR+ v2.7, 13.5.2.4
     * location codes as documented by PAPR+ v2.7, 12.3.1.5
     */
    return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
173 174 175 176 177 178 179 180 181
}

/*
 * dr-entity-sense sensor value
 * returned via get-sensor-state RTAS calls
 * as expected by state diagram in PAPR+ 2.7, 13.4
 * based on the current allocation/indicator/power states
 * for the DR connector.
 */
182
static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc)
183
{
184 185 186 187 188 189 190 191 192 193 194 195 196 197
    /* this assumes all PCI devices are assigned to a 'live insertion'
     * power domain, where QEMU manages power state automatically as
     * opposed to the guest. present, non-PCI resources are unaffected
     * by power state.
     */
    return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
        : SPAPR_DR_ENTITY_SENSE_EMPTY;
}

static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc)
{
    if (drc->dev
        && (drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE)) {
        return SPAPR_DR_ENTITY_SENSE_PRESENT;
198
    } else {
199
        return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
200 201 202
    }
}

203 204
static void prop_get_index(Object *obj, Visitor *v, const char *name,
                           void *opaque, Error **errp)
205 206
{
    sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
207
    uint32_t value = spapr_drc_index(drc);
208
    visit_type_uint32(v, name, &value, errp);
209 210
}

211 212
static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
                         void *opaque, Error **errp)
213 214
{
    sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
215
    Error *err = NULL;
216 217 218 219
    int fdt_offset_next, fdt_offset, fdt_depth;
    void *fdt;

    if (!drc->fdt) {
220
        visit_type_null(v, NULL, errp);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        return;
    }

    fdt = drc->fdt;
    fdt_offset = drc->fdt_start_offset;
    fdt_depth = 0;

    do {
        const char *name = NULL;
        const struct fdt_property *prop = NULL;
        int prop_len = 0, name_len = 0;
        uint32_t tag;

        tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
        switch (tag) {
        case FDT_BEGIN_NODE:
            fdt_depth++;
            name = fdt_get_name(fdt, fdt_offset, &name_len);
239
            visit_start_struct(v, name, NULL, 0, &err);
240 241 242 243
            if (err) {
                error_propagate(errp, err);
                return;
            }
244 245 246 247
            break;
        case FDT_END_NODE:
            /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
            g_assert(fdt_depth > 0);
248
            visit_check_struct(v, &err);
E
Eric Blake 已提交
249
            visit_end_struct(v, NULL);
250 251 252 253
            if (err) {
                error_propagate(errp, err);
                return;
            }
254 255 256 257 258 259
            fdt_depth--;
            break;
        case FDT_PROP: {
            int i;
            prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
            name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
260
            visit_start_list(v, name, NULL, 0, &err);
261 262 263 264
            if (err) {
                error_propagate(errp, err);
                return;
            }
265
            for (i = 0; i < prop_len; i++) {
266
                visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
267 268 269 270 271
                if (err) {
                    error_propagate(errp, err);
                    return;
                }
            }
272
            visit_check_list(v, &err);
E
Eric Blake 已提交
273
            visit_end_list(v, NULL);
274 275 276 277
            if (err) {
                error_propagate(errp, err);
                return;
            }
278 279 280 281 282 283 284 285 286
            break;
        }
        default:
            error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
        }
        fdt_offset = fdt_offset_next;
    } while (fdt_depth != 0);
}

287 288
void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
                      int fdt_start_offset, bool coldplug, Error **errp)
289
{
290
    trace_spapr_drc_attach(spapr_drc_index(drc));
291 292 293 294 295

    if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
        error_setg(errp, "an attached device is still awaiting release");
        return;
    }
D
David Gibson 已提交
296
    if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
297 298 299 300
        g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
    }
    g_assert(fdt || coldplug);

301
    drc->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
302 303 304 305

    drc->dev = d;
    drc->fdt = fdt;
    drc->fdt_start_offset = fdt_start_offset;
306
    drc->configured = coldplug;
307

D
David Gibson 已提交
308
    if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) {
309 310 311
        drc->awaiting_allocation = true;
    }

312 313 314 315 316 317
    object_property_add_link(OBJECT(drc), "device",
                             object_get_typename(OBJECT(drc->dev)),
                             (Object **)(&drc->dev),
                             NULL, 0, NULL);
}

318
static void spapr_drc_release(sPAPRDRConnector *drc)
319
{
320
    drc->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
321

D
David Gibson 已提交
322 323
    /* Calling release callbacks based on spapr_drc_type(drc). */
    switch (spapr_drc_type(drc)) {
324 325 326 327 328 329 330 331 332 333 334 335 336
    case SPAPR_DR_CONNECTOR_TYPE_CPU:
        spapr_core_release(drc->dev);
        break;
    case SPAPR_DR_CONNECTOR_TYPE_PCI:
        spapr_phb_remove_pci_device_cb(drc->dev);
        break;
    case SPAPR_DR_CONNECTOR_TYPE_LMB:
        spapr_lmb_release(drc->dev);
        break;
    case SPAPR_DR_CONNECTOR_TYPE_PHB:
    case SPAPR_DR_CONNECTOR_TYPE_VIO:
    default:
        g_assert(false);
337 338 339 340 341 342 343 344 345 346
    }

    drc->awaiting_release = false;
    g_free(drc->fdt);
    drc->fdt = NULL;
    drc->fdt_start_offset = 0;
    object_property_del(OBJECT(drc), "device", NULL);
    drc->dev = NULL;
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
void spapr_drc_detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
{
    trace_spapr_drc_detach(spapr_drc_index(drc));

    if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
        trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc));
        drc->awaiting_release = true;
        return;
    }

    if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
        drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
        trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc));
        drc->awaiting_release = true;
        return;
    }

    if (drc->awaiting_allocation) {
        drc->awaiting_release = true;
        trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc));
        return;
    }

    spapr_drc_release(drc);
}

373 374 375 376 377 378 379 380 381 382
static bool release_pending(sPAPRDRConnector *drc)
{
    return drc->awaiting_release;
}

static void reset(DeviceState *d)
{
    sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);

383
    trace_spapr_drc_reset(spapr_drc_index(drc));
384 385 386 387

    g_free(drc->ccs);
    drc->ccs = NULL;

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    /* immediately upon reset we can safely assume DRCs whose devices
     * are pending removal can be safely removed, and that they will
     * subsequently be left in an ISOLATED state. move the DRC to this
     * state in these cases (which will in turn complete any pending
     * device removals)
     */
    if (drc->awaiting_release) {
        drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
        /* generally this should also finalize the removal, but if the device
         * hasn't yet been configured we normally defer removal under the
         * assumption that this transition is taking place as part of device
         * configuration. so check if we're still waiting after this, and
         * force removal if we are
         */
        if (drc->awaiting_release) {
403
            spapr_drc_detach(drc, DEVICE(drc->dev), NULL);
404 405 406
        }

        /* non-PCI devices may be awaiting a transition to UNUSABLE */
D
David Gibson 已提交
407
        if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI &&
408 409 410 411 412 413
            drc->awaiting_release) {
            drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
        }
    }
}

414 415 416 417 418
static bool spapr_drc_needed(void *opaque)
{
    sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
    bool rc = false;
419
    sPAPRDREntitySense value = drck->dr_entity_sense(drc);
420 421 422 423 424 425 426 427 428 429

    /* If no dev is plugged in there is no need to migrate the DRC state */
    if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
        return false;
    }

    /*
     * If there is dev plugged in, we need to migrate the DRC state when
     * it is different from cold-plugged state
     */
D
David Gibson 已提交
430
    switch (spapr_drc_type(drc)) {
431 432 433
    case SPAPR_DR_CONNECTOR_TYPE_PCI:
    case SPAPR_DR_CONNECTOR_TYPE_CPU:
    case SPAPR_DR_CONNECTOR_TYPE_LMB:
434 435
        rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
               (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
436
               drc->configured && !drc->awaiting_release);
437 438 439 440
        break;
    case SPAPR_DR_CONNECTOR_TYPE_PHB:
    case SPAPR_DR_CONNECTOR_TYPE_VIO:
    default:
441
        g_assert_not_reached();
442 443 444 445 446 447 448 449 450 451 452 453
    }
    return rc;
}

static const VMStateDescription vmstate_spapr_drc = {
    .name = "spapr_drc",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = spapr_drc_needed,
    .fields  = (VMStateField []) {
        VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
        VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
454
        VMSTATE_UINT32(dr_indicator, sPAPRDRConnector),
455 456 457 458 459 460 461
        VMSTATE_BOOL(configured, sPAPRDRConnector),
        VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
        VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
        VMSTATE_END_OF_LIST()
    }
};

462 463 464 465 466 467 468 469
static void realize(DeviceState *d, Error **errp)
{
    sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
    Object *root_container;
    char link_name[256];
    gchar *child_name;
    Error *err = NULL;

470
    trace_spapr_drc_realize(spapr_drc_index(drc));
471 472 473 474 475 476 477 478
    /* NOTE: we do this as part of realize/unrealize due to the fact
     * that the guest will communicate with the DRC via RTAS calls
     * referencing the global DRC index. By unlinking the DRC
     * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
     * inaccessible by the guest, since lookups rely on this path
     * existing in the composition tree
     */
    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
479
    snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc));
480
    child_name = object_get_canonical_path_component(OBJECT(drc));
481
    trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
482 483 484
    object_property_add_alias(root_container, link_name,
                              drc->owner, child_name, &err);
    if (err) {
485
        error_report_err(err);
486 487
        object_unref(OBJECT(drc));
    }
G
Gonglei 已提交
488
    g_free(child_name);
489
    vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
490
                     drc);
491
    trace_spapr_drc_realize_complete(spapr_drc_index(drc));
492 493 494 495 496 497 498 499 500
}

static void unrealize(DeviceState *d, Error **errp)
{
    sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
    Object *root_container;
    char name[256];
    Error *err = NULL;

501
    trace_spapr_drc_unrealize(spapr_drc_index(drc));
502
    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
503
    snprintf(name, sizeof(name), "%x", spapr_drc_index(drc));
504 505
    object_property_del(root_container, name, &err);
    if (err) {
506
        error_report_err(err);
507 508 509 510
        object_unref(OBJECT(drc));
    }
}

D
David Gibson 已提交
511
sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type,
512 513
                                         uint32_t id)
{
D
David Gibson 已提交
514
    sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type));
515
    char *prop_name;
516 517 518

    drc->id = id;
    drc->owner = owner;
519 520
    prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
                                spapr_drc_index(drc));
521
    object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
522
    object_property_set_bool(OBJECT(drc), true, "realized", NULL);
523
    g_free(prop_name);
524 525

    /* PCI slot always start in a USABLE state, and stay there */
D
David Gibson 已提交
526
    if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) {
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
        drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
    }

    return drc;
}

static void spapr_dr_connector_instance_init(Object *obj)
{
    sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);

    object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
    object_property_add(obj, "index", "uint32", prop_get_index,
                        NULL, NULL, NULL, NULL);
    object_property_add(obj, "fdt", "struct", prop_get_fdt,
                        NULL, NULL, NULL, NULL);
}

static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
{
    DeviceClass *dk = DEVICE_CLASS(k);
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);

    dk->reset = reset;
    dk->realize = realize;
    dk->unrealize = unrealize;
    drck->set_isolation_state = set_isolation_state;
    drck->set_allocation_state = set_allocation_state;
    drck->release_pending = release_pending;
555 556 557
    /*
     * Reason: it crashes FIXME find and document the real reason
     */
558
    dk->user_creatable = false;
559 560
}

561 562 563 564 565 566 567 568 569 570 571 572 573 574
static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
{
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);

    drck->dr_entity_sense = physical_entity_sense;
}

static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
{
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);

    drck->dr_entity_sense = logical_entity_sense;
}

D
David Gibson 已提交
575 576 577 578 579
static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
{
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);

    drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
580
    drck->typename = "CPU";
D
David Gibson 已提交
581
    drck->drc_name_prefix = "CPU ";
D
David Gibson 已提交
582 583 584 585 586 587 588
}

static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
{
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);

    drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
589
    drck->typename = "28";
D
David Gibson 已提交
590
    drck->drc_name_prefix = "C";
D
David Gibson 已提交
591 592 593 594 595 596 597
}

static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
{
    sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);

    drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
598
    drck->typename = "MEM";
D
David Gibson 已提交
599
    drck->drc_name_prefix = "LMB ";
D
David Gibson 已提交
600 601
}

602 603 604 605 606 607 608
static const TypeInfo spapr_dr_connector_info = {
    .name          = TYPE_SPAPR_DR_CONNECTOR,
    .parent        = TYPE_DEVICE,
    .instance_size = sizeof(sPAPRDRConnector),
    .instance_init = spapr_dr_connector_instance_init,
    .class_size    = sizeof(sPAPRDRConnectorClass),
    .class_init    = spapr_dr_connector_class_init,
D
David Gibson 已提交
609 610 611 612 613 614 615
    .abstract      = true,
};

static const TypeInfo spapr_drc_physical_info = {
    .name          = TYPE_SPAPR_DRC_PHYSICAL,
    .parent        = TYPE_SPAPR_DR_CONNECTOR,
    .instance_size = sizeof(sPAPRDRConnector),
616
    .class_init    = spapr_drc_physical_class_init,
D
David Gibson 已提交
617 618 619 620 621 622 623
    .abstract      = true,
};

static const TypeInfo spapr_drc_logical_info = {
    .name          = TYPE_SPAPR_DRC_LOGICAL,
    .parent        = TYPE_SPAPR_DR_CONNECTOR,
    .instance_size = sizeof(sPAPRDRConnector),
624
    .class_init    = spapr_drc_logical_class_init,
D
David Gibson 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    .abstract      = true,
};

static const TypeInfo spapr_drc_cpu_info = {
    .name          = TYPE_SPAPR_DRC_CPU,
    .parent        = TYPE_SPAPR_DRC_LOGICAL,
    .instance_size = sizeof(sPAPRDRConnector),
    .class_init    = spapr_drc_cpu_class_init,
};

static const TypeInfo spapr_drc_pci_info = {
    .name          = TYPE_SPAPR_DRC_PCI,
    .parent        = TYPE_SPAPR_DRC_PHYSICAL,
    .instance_size = sizeof(sPAPRDRConnector),
    .class_init    = spapr_drc_pci_class_init,
};

static const TypeInfo spapr_drc_lmb_info = {
    .name          = TYPE_SPAPR_DRC_LMB,
    .parent        = TYPE_SPAPR_DRC_LOGICAL,
    .instance_size = sizeof(sPAPRDRConnector),
    .class_init    = spapr_drc_lmb_class_init,
647 648 649 650
};

/* helper functions for external users */

651
sPAPRDRConnector *spapr_drc_by_index(uint32_t index)
652 653 654 655 656 657 658 659 660 661
{
    Object *obj;
    char name[256];

    snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
    obj = object_resolve_path(name, NULL);

    return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
}

662
sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id)
663
{
664 665 666 667 668
    sPAPRDRConnectorClass *drck
        = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));

    return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
                              | (id & DRC_INDEX_ID_MASK));
669
}
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689

/**
 * spapr_drc_populate_dt
 *
 * @fdt: libfdt device tree
 * @path: path in the DT to generate properties
 * @owner: parent Object/DeviceState for which to generate DRC
 *         descriptions for
 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
 *   to the types of DRCs to generate entries for
 *
 * generate OF properties to describe DRC topology/indices to guests
 *
 * as documented in PAPR+ v2.1, 13.5.2
 */
int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
                          uint32_t drc_type_mask)
{
    Object *root_container;
    ObjectProperty *prop;
690
    ObjectPropertyIterator iter;
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    uint32_t drc_count = 0;
    GArray *drc_indexes, *drc_power_domains;
    GString *drc_names, *drc_types;
    int ret;

    /* the first entry of each properties is a 32-bit integer encoding
     * the number of elements in the array. we won't know this until
     * we complete the iteration through all the matching DRCs, but
     * reserve the space now and set the offsets accordingly so we
     * can fill them in later.
     */
    drc_indexes = g_array_new(false, true, sizeof(uint32_t));
    drc_indexes = g_array_set_size(drc_indexes, 1);
    drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
    drc_power_domains = g_array_set_size(drc_power_domains, 1);
    drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
    drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));

    /* aliases for all DRConnector objects will be rooted in QOM
     * composition tree at DRC_CONTAINER_PATH
     */
    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);

714 715
    object_property_iter_init(&iter, root_container);
    while ((prop = object_property_iter_next(&iter))) {
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
        Object *obj;
        sPAPRDRConnector *drc;
        sPAPRDRConnectorClass *drck;
        uint32_t drc_index, drc_power_domain;

        if (!strstart(prop->type, "link<", NULL)) {
            continue;
        }

        obj = object_property_get_link(root_container, prop->name, NULL);
        drc = SPAPR_DR_CONNECTOR(obj);
        drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);

        if (owner && (drc->owner != owner)) {
            continue;
        }

D
David Gibson 已提交
733
        if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
734 735 736 737 738 739
            continue;
        }

        drc_count++;

        /* ibm,drc-indexes */
740
        drc_index = cpu_to_be32(spapr_drc_index(drc));
741 742 743 744 745 746 747
        g_array_append_val(drc_indexes, drc_index);

        /* ibm,drc-power-domains */
        drc_power_domain = cpu_to_be32(-1);
        g_array_append_val(drc_power_domains, drc_power_domain);

        /* ibm,drc-names */
D
David Gibson 已提交
748
        drc_names = g_string_append(drc_names, spapr_drc_name(drc));
749 750 751
        drc_names = g_string_insert_len(drc_names, -1, "\0", 1);

        /* ibm,drc-types */
752
        drc_types = g_string_append(drc_types, drck->typename);
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
        drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
    }

    /* now write the drc count into the space we reserved at the
     * beginning of the arrays previously
     */
    *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
    *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
    *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
    *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);

    ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
                      drc_indexes->data,
                      drc_indexes->len * sizeof(uint32_t));
    if (ret) {
768
        error_report("Couldn't create ibm,drc-indexes property");
769 770 771 772 773 774 775
        goto out;
    }

    ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
                      drc_power_domains->data,
                      drc_power_domains->len * sizeof(uint32_t));
    if (ret) {
776
        error_report("Couldn't finalize ibm,drc-power-domains property");
777 778 779 780 781 782
        goto out;
    }

    ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
                      drc_names->str, drc_names->len);
    if (ret) {
783
        error_report("Couldn't finalize ibm,drc-names property");
784 785 786 787 788 789
        goto out;
    }

    ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
                      drc_types->str, drc_types->len);
    if (ret) {
790
        error_report("Couldn't finalize ibm,drc-types property");
791 792 793 794 795 796 797 798 799 800 801
        goto out;
    }

out:
    g_array_free(drc_indexes, true);
    g_array_free(drc_power_domains, true);
    g_string_free(drc_names, true);
    g_string_free(drc_types, true);

    return ret;
}
802 803 804 805 806

/*
 * RTAS calls
 */

807
static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
808
{
809 810 811 812 813
    sPAPRDRConnector *drc = spapr_drc_by_index(idx);
    sPAPRDRConnectorClass *drck;

    if (!drc) {
        return RTAS_OUT_PARAM_ERROR;
814 815
    }

816 817
    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
    return drck->set_isolation_state(drc, state);
818 819
}

820
static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
821
{
822
    sPAPRDRConnector *drc = spapr_drc_by_index(idx);
823 824
    sPAPRDRConnectorClass *drck;

825 826
    if (!drc) {
        return RTAS_OUT_PARAM_ERROR;
827 828
    }

829 830 831
    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
    return drck->set_allocation_state(drc, state);
}
832

833
static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
834 835
{
    sPAPRDRConnector *drc = spapr_drc_by_index(idx);
836 837

    if (!drc) {
838 839 840
        return RTAS_OUT_PARAM_ERROR;
    }

841 842 843
    trace_spapr_drc_set_dr_indicator(idx, state);
    drc->dr_indicator = state;
    return RTAS_OUT_SUCCESS;
844 845 846 847 848 849 850 851 852 853 854
}

static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
                               uint32_t token,
                               uint32_t nargs, target_ulong args,
                               uint32_t nret, target_ulong rets)
{
    uint32_t type, idx, state;
    uint32_t ret = RTAS_OUT_SUCCESS;

    if (nargs != 3 || nret != 1) {
855 856 857 858
        ret = RTAS_OUT_PARAM_ERROR;
        goto out;
    }

859 860 861 862 863
    type = rtas_ld(args, 0);
    idx = rtas_ld(args, 1);
    state = rtas_ld(args, 2);

    switch (type) {
864
    case RTAS_SENSOR_TYPE_ISOLATION_STATE:
865
        ret = rtas_set_isolation_state(idx, state);
866 867
        break;
    case RTAS_SENSOR_TYPE_DR:
868
        ret = rtas_set_dr_indicator(idx, state);
869 870
        break;
    case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
871
        ret = rtas_set_allocation_state(idx, state);
872 873
        break;
    default:
874
        ret = RTAS_OUT_NOT_SUPPORTED;
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    }

out:
    rtas_st(rets, 0, ret);
}

static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
                                  uint32_t token, uint32_t nargs,
                                  target_ulong args, uint32_t nret,
                                  target_ulong rets)
{
    uint32_t sensor_type;
    uint32_t sensor_index;
    uint32_t sensor_state = 0;
    sPAPRDRConnector *drc;
    sPAPRDRConnectorClass *drck;
    uint32_t ret = RTAS_OUT_SUCCESS;

    if (nargs != 2 || nret != 2) {
        ret = RTAS_OUT_PARAM_ERROR;
        goto out;
    }

    sensor_type = rtas_ld(args, 0);
    sensor_index = rtas_ld(args, 1);

    if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
        /* currently only DR-related sensors are implemented */
        trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
                                                        sensor_type);
        ret = RTAS_OUT_NOT_SUPPORTED;
        goto out;
    }

909
    drc = spapr_drc_by_index(sensor_index);
910 911 912 913 914 915
    if (!drc) {
        trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
        ret = RTAS_OUT_PARAM_ERROR;
        goto out;
    }
    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
916
    sensor_state = drck->dr_entity_sense(drc);
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

out:
    rtas_st(rets, 0, ret);
    rtas_st(rets, 1, sensor_state);
}

/* configure-connector work area offsets, int32_t units for field
 * indexes, bytes for field offset/len values.
 *
 * as documented by PAPR+ v2.7, 13.5.3.5
 */
#define CC_IDX_NODE_NAME_OFFSET 2
#define CC_IDX_PROP_NAME_OFFSET 2
#define CC_IDX_PROP_LEN 3
#define CC_IDX_PROP_DATA_OFFSET 4
#define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
#define CC_WA_LEN 4096

static void configure_connector_st(target_ulong addr, target_ulong offset,
                                   const void *buf, size_t len)
{
    cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
                              buf, MIN(len, CC_WA_LEN - offset));
}

static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
                                         sPAPRMachineState *spapr,
                                         uint32_t token, uint32_t nargs,
                                         target_ulong args, uint32_t nret,
                                         target_ulong rets)
{
    uint64_t wa_addr;
    uint64_t wa_offset;
    uint32_t drc_index;
    sPAPRDRConnector *drc;
    sPAPRConfigureConnectorState *ccs;
    sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
    int rc;

    if (nargs != 2 || nret != 1) {
        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
        return;
    }

    wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);

    drc_index = rtas_ld(wa_addr, 0);
964
    drc = spapr_drc_by_index(drc_index);
965 966 967 968 969 970
    if (!drc) {
        trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
        rc = RTAS_OUT_PARAM_ERROR;
        goto out;
    }

D
David Gibson 已提交
971
    if (!drc->fdt) {
972 973 974 975 976
        trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
        rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
        goto out;
    }

977
    ccs = drc->ccs;
978 979
    if (!ccs) {
        ccs = g_new0(sPAPRConfigureConnectorState, 1);
D
David Gibson 已提交
980
        ccs->fdt_offset = drc->fdt_start_offset;
981
        drc->ccs = ccs;
982 983 984 985 986 987 988 989
    }

    do {
        uint32_t tag;
        const char *name;
        const struct fdt_property *prop;
        int fdt_offset_next, prop_len;

D
David Gibson 已提交
990
        tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
991 992 993 994

        switch (tag) {
        case FDT_BEGIN_NODE:
            ccs->fdt_depth++;
D
David Gibson 已提交
995
            name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
996 997 998 999 1000 1001 1002 1003 1004 1005

            /* provide the name of the next OF node */
            wa_offset = CC_VAL_DATA_OFFSET;
            rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
            configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
            resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
            break;
        case FDT_END_NODE:
            ccs->fdt_depth--;
            if (ccs->fdt_depth == 0) {
1006
                sPAPRDRIsolationState state = drc->isolation_state;
1007
                uint32_t drc_index = spapr_drc_index(drc);
1008 1009 1010
                /* done sending the device tree, don't need to track
                 * the state anymore
                 */
1011
                trace_spapr_drc_set_configured(drc_index);
1012 1013 1014 1015
                if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
                    drc->configured = true;
                } else {
                    /* guest should be not configuring an isolated device */
1016
                    trace_spapr_drc_set_configured_skipping(drc_index);
1017
                }
1018 1019
                g_free(ccs);
                drc->ccs = NULL;
1020 1021 1022 1023 1024 1025 1026
                ccs = NULL;
                resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
            } else {
                resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
            }
            break;
        case FDT_PROP:
D
David Gibson 已提交
1027
            prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
1028
                                              &prop_len);
D
David Gibson 已提交
1029
            name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

            /* provide the name of the next OF property */
            wa_offset = CC_VAL_DATA_OFFSET;
            rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
            configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);

            /* provide the length and value of the OF property. data gets
             * placed immediately after NULL terminator of the OF property's
             * name string
             */
            wa_offset += strlen(name) + 1,
            rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
            rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
            configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
            resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
            break;
        case FDT_END:
            resp = SPAPR_DR_CC_RESPONSE_ERROR;
        default:
            /* keep seeking for an actionable tag */
            break;
        }
        if (ccs) {
            ccs->fdt_offset = fdt_offset_next;
        }
    } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);

    rc = resp;
out:
    rtas_st(rets, 0, rc);
}

static void spapr_drc_register_types(void)
{
    type_register_static(&spapr_dr_connector_info);
D
David Gibson 已提交
1065 1066 1067 1068 1069
    type_register_static(&spapr_drc_physical_info);
    type_register_static(&spapr_drc_logical_info);
    type_register_static(&spapr_drc_cpu_info);
    type_register_static(&spapr_drc_pci_info);
    type_register_static(&spapr_drc_lmb_info);
1070 1071 1072 1073 1074 1075 1076 1077 1078

    spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
                        rtas_set_indicator);
    spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
                        rtas_get_sensor_state);
    spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
                        rtas_ibm_configure_connector);
}
type_init(spapr_drc_register_types)