qemu/hw/ppc/spapr_rtas.c
<<
>>
Prefs
   1/*
   2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
   3 *
   4 * Hypercall based emulated RTAS
   5 *
   6 * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
   7 *
   8 * Permission is hereby granted, free of charge, to any person obtaining a copy
   9 * of this software and associated documentation files (the "Software"), to deal
  10 * in the Software without restriction, including without limitation the rights
  11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12 * copies of the Software, and to permit persons to whom the Software is
  13 * furnished to do so, subject to the following conditions:
  14 *
  15 * The above copyright notice and this permission notice shall be included in
  16 * all copies or substantial portions of the Software.
  17 *
  18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24 * THE SOFTWARE.
  25 *
  26 */
  27#include "qemu/osdep.h"
  28#include "cpu.h"
  29#include "sysemu/sysemu.h"
  30#include "sysemu/char.h"
  31#include "hw/qdev.h"
  32#include "sysemu/device_tree.h"
  33#include "sysemu/cpus.h"
  34
  35#include "hw/ppc/spapr.h"
  36#include "hw/ppc/spapr_vio.h"
  37#include "qapi-event.h"
  38#include "hw/boards.h"
  39
  40#include <libfdt.h>
  41#include "hw/ppc/spapr_drc.h"
  42#include "qemu/cutils.h"
  43
  44/* #define DEBUG_SPAPR */
  45
  46#ifdef DEBUG_SPAPR
  47#define DPRINTF(fmt, ...) \
  48    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
  49#else
  50#define DPRINTF(fmt, ...) \
  51    do { } while (0)
  52#endif
  53
  54static sPAPRConfigureConnectorState *spapr_ccs_find(sPAPRMachineState *spapr,
  55                                                    uint32_t drc_index)
  56{
  57    sPAPRConfigureConnectorState *ccs = NULL;
  58
  59    QTAILQ_FOREACH(ccs, &spapr->ccs_list, next) {
  60        if (ccs->drc_index == drc_index) {
  61            break;
  62        }
  63    }
  64
  65    return ccs;
  66}
  67
  68static void spapr_ccs_add(sPAPRMachineState *spapr,
  69                          sPAPRConfigureConnectorState *ccs)
  70{
  71    g_assert(!spapr_ccs_find(spapr, ccs->drc_index));
  72    QTAILQ_INSERT_HEAD(&spapr->ccs_list, ccs, next);
  73}
  74
  75static void spapr_ccs_remove(sPAPRMachineState *spapr,
  76                             sPAPRConfigureConnectorState *ccs)
  77{
  78    QTAILQ_REMOVE(&spapr->ccs_list, ccs, next);
  79    g_free(ccs);
  80}
  81
  82void spapr_ccs_reset_hook(void *opaque)
  83{
  84    sPAPRMachineState *spapr = opaque;
  85    sPAPRConfigureConnectorState *ccs, *ccs_tmp;
  86
  87    QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) {
  88        spapr_ccs_remove(spapr, ccs);
  89    }
  90}
  91
  92static void rtas_display_character(PowerPCCPU *cpu, sPAPRMachineState *spapr,
  93                                   uint32_t token, uint32_t nargs,
  94                                   target_ulong args,
  95                                   uint32_t nret, target_ulong rets)
  96{
  97    uint8_t c = rtas_ld(args, 0);
  98    VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
  99
 100    if (!sdev) {
 101        rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
 102    } else {
 103        vty_putchars(sdev, &c, sizeof(c));
 104        rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 105    }
 106}
 107
 108static void rtas_power_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 109                           uint32_t token, uint32_t nargs, target_ulong args,
 110                           uint32_t nret, target_ulong rets)
 111{
 112    if (nargs != 2 || nret != 1) {
 113        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 114        return;
 115    }
 116    qemu_system_shutdown_request();
 117    cpu_stop_current();
 118    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 119}
 120
 121static void rtas_system_reboot(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 122                               uint32_t token, uint32_t nargs,
 123                               target_ulong args,
 124                               uint32_t nret, target_ulong rets)
 125{
 126    if (nargs != 0 || nret != 1) {
 127        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 128        return;
 129    }
 130    qemu_system_reset_request();
 131    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 132}
 133
 134static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
 135                                         sPAPRMachineState *spapr,
 136                                         uint32_t token, uint32_t nargs,
 137                                         target_ulong args,
 138                                         uint32_t nret, target_ulong rets)
 139{
 140    target_ulong id;
 141    PowerPCCPU *cpu;
 142
 143    if (nargs != 1 || nret != 2) {
 144        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 145        return;
 146    }
 147
 148    id = rtas_ld(args, 0);
 149    cpu = ppc_get_vcpu_by_dt_id(id);
 150    if (cpu != NULL) {
 151        if (CPU(cpu)->halted) {
 152            rtas_st(rets, 1, 0);
 153        } else {
 154            rtas_st(rets, 1, 2);
 155        }
 156
 157        rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 158        return;
 159    }
 160
 161    /* Didn't find a matching cpu */
 162    rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 163}
 164
 165static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPRMachineState *spapr,
 166                           uint32_t token, uint32_t nargs,
 167                           target_ulong args,
 168                           uint32_t nret, target_ulong rets)
 169{
 170    target_ulong id, start, r3;
 171    PowerPCCPU *cpu;
 172
 173    if (nargs != 3 || nret != 1) {
 174        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 175        return;
 176    }
 177
 178    id = rtas_ld(args, 0);
 179    start = rtas_ld(args, 1);
 180    r3 = rtas_ld(args, 2);
 181
 182    cpu = ppc_get_vcpu_by_dt_id(id);
 183    if (cpu != NULL) {
 184        CPUState *cs = CPU(cpu);
 185        CPUPPCState *env = &cpu->env;
 186
 187        if (!cs->halted) {
 188            rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
 189            return;
 190        }
 191
 192        /* This will make sure qemu state is up to date with kvm, and
 193         * mark it dirty so our changes get flushed back before the
 194         * new cpu enters */
 195        kvm_cpu_synchronize_state(cs);
 196
 197        env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
 198        env->nip = start;
 199        env->gpr[3] = r3;
 200        cs->halted = 0;
 201
 202        qemu_cpu_kick(cs);
 203
 204        rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 205        return;
 206    }
 207
 208    /* Didn't find a matching cpu */
 209    rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 210}
 211
 212static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 213                           uint32_t token, uint32_t nargs,
 214                           target_ulong args,
 215                           uint32_t nret, target_ulong rets)
 216{
 217    CPUState *cs = CPU(cpu);
 218    CPUPPCState *env = &cpu->env;
 219
 220    cs->halted = 1;
 221    qemu_cpu_kick(cs);
 222    /*
 223     * While stopping a CPU, the guest calls H_CPPR which
 224     * effectively disables interrupts on XICS level.
 225     * However decrementer interrupts in TCG can still
 226     * wake the CPU up so here we disable interrupts in MSR
 227     * as well.
 228     * As rtas_start_cpu() resets the whole MSR anyway, there is
 229     * no need to bother with specific bits, we just clear it.
 230     */
 231    env->msr = 0;
 232}
 233
 234static inline int sysparm_st(target_ulong addr, target_ulong len,
 235                             const void *val, uint16_t vallen)
 236{
 237    hwaddr phys = ppc64_phys_to_real(addr);
 238
 239    if (len < 2) {
 240        return RTAS_OUT_SYSPARM_PARAM_ERROR;
 241    }
 242    stw_be_phys(&address_space_memory, phys, vallen);
 243    cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
 244    return RTAS_OUT_SUCCESS;
 245}
 246
 247static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
 248                                          sPAPRMachineState *spapr,
 249                                          uint32_t token, uint32_t nargs,
 250                                          target_ulong args,
 251                                          uint32_t nret, target_ulong rets)
 252{
 253    target_ulong parameter = rtas_ld(args, 0);
 254    target_ulong buffer = rtas_ld(args, 1);
 255    target_ulong length = rtas_ld(args, 2);
 256    target_ulong ret;
 257
 258    switch (parameter) {
 259    case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
 260        char *param_val = g_strdup_printf("MaxEntCap=%d,"
 261                                          "DesMem=%llu,"
 262                                          "DesProcs=%d,"
 263                                          "MaxPlatProcs=%d",
 264                                          max_cpus,
 265                                          current_machine->ram_size / M_BYTE,
 266                                          smp_cpus,
 267                                          max_cpus);
 268        ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
 269        g_free(param_val);
 270        break;
 271    }
 272    case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
 273        uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
 274
 275        ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
 276        break;
 277    }
 278    case RTAS_SYSPARM_UUID:
 279        ret = sysparm_st(buffer, length, qemu_uuid, (qemu_uuid_set ? 16 : 0));
 280        break;
 281    default:
 282        ret = RTAS_OUT_NOT_SUPPORTED;
 283    }
 284
 285    rtas_st(rets, 0, ret);
 286}
 287
 288static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
 289                                          sPAPRMachineState *spapr,
 290                                          uint32_t token, uint32_t nargs,
 291                                          target_ulong args,
 292                                          uint32_t nret, target_ulong rets)
 293{
 294    target_ulong parameter = rtas_ld(args, 0);
 295    target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
 296
 297    switch (parameter) {
 298    case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
 299    case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
 300    case RTAS_SYSPARM_UUID:
 301        ret = RTAS_OUT_NOT_AUTHORIZED;
 302        break;
 303    }
 304
 305    rtas_st(rets, 0, ret);
 306}
 307
 308static void rtas_ibm_os_term(PowerPCCPU *cpu,
 309                            sPAPRMachineState *spapr,
 310                            uint32_t token, uint32_t nargs,
 311                            target_ulong args,
 312                            uint32_t nret, target_ulong rets)
 313{
 314    target_ulong ret = 0;
 315
 316    qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
 317
 318    rtas_st(rets, 0, ret);
 319}
 320
 321static void rtas_set_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 322                                 uint32_t token, uint32_t nargs,
 323                                 target_ulong args, uint32_t nret,
 324                                 target_ulong rets)
 325{
 326    int32_t power_domain;
 327
 328    if (nargs != 2 || nret != 2) {
 329        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 330        return;
 331    }
 332
 333    /* we currently only use a single, "live insert" powerdomain for
 334     * hotplugged/dlpar'd resources, so the power is always live/full (100)
 335     */
 336    power_domain = rtas_ld(args, 0);
 337    if (power_domain != -1) {
 338        rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
 339        return;
 340    }
 341
 342    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 343    rtas_st(rets, 1, 100);
 344}
 345
 346static void rtas_get_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 347                                  uint32_t token, uint32_t nargs,
 348                                  target_ulong args, uint32_t nret,
 349                                  target_ulong rets)
 350{
 351    int32_t power_domain;
 352
 353    if (nargs != 1 || nret != 2) {
 354        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 355        return;
 356    }
 357
 358    /* we currently only use a single, "live insert" powerdomain for
 359     * hotplugged/dlpar'd resources, so the power is always live/full (100)
 360     */
 361    power_domain = rtas_ld(args, 0);
 362    if (power_domain != -1) {
 363        rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
 364        return;
 365    }
 366
 367    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 368    rtas_st(rets, 1, 100);
 369}
 370
 371static bool sensor_type_is_dr(uint32_t sensor_type)
 372{
 373    switch (sensor_type) {
 374    case RTAS_SENSOR_TYPE_ISOLATION_STATE:
 375    case RTAS_SENSOR_TYPE_DR:
 376    case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
 377        return true;
 378    }
 379
 380    return false;
 381}
 382
 383static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 384                               uint32_t token, uint32_t nargs,
 385                               target_ulong args, uint32_t nret,
 386                               target_ulong rets)
 387{
 388    uint32_t sensor_type;
 389    uint32_t sensor_index;
 390    uint32_t sensor_state;
 391    uint32_t ret = RTAS_OUT_SUCCESS;
 392    sPAPRDRConnector *drc;
 393    sPAPRDRConnectorClass *drck;
 394
 395    if (nargs != 3 || nret != 1) {
 396        ret = RTAS_OUT_PARAM_ERROR;
 397        goto out;
 398    }
 399
 400    sensor_type = rtas_ld(args, 0);
 401    sensor_index = rtas_ld(args, 1);
 402    sensor_state = rtas_ld(args, 2);
 403
 404    if (!sensor_type_is_dr(sensor_type)) {
 405        goto out_unimplemented;
 406    }
 407
 408    /* if this is a DR sensor we can assume sensor_index == drc_index */
 409    drc = spapr_dr_connector_by_index(sensor_index);
 410    if (!drc) {
 411        DPRINTF("rtas_set_indicator: invalid sensor/DRC index: %xh\n",
 412                sensor_index);
 413        ret = RTAS_OUT_PARAM_ERROR;
 414        goto out;
 415    }
 416    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 417
 418    switch (sensor_type) {
 419    case RTAS_SENSOR_TYPE_ISOLATION_STATE:
 420        /* if the guest is configuring a device attached to this
 421         * DRC, we should reset the configuration state at this
 422         * point since it may no longer be reliable (guest released
 423         * device and needs to start over, or unplug occurred so
 424         * the FDT is no longer valid)
 425         */
 426        if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
 427            sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr,
 428                                                               sensor_index);
 429            if (ccs) {
 430                spapr_ccs_remove(spapr, ccs);
 431            }
 432        }
 433        ret = drck->set_isolation_state(drc, sensor_state);
 434        break;
 435    case RTAS_SENSOR_TYPE_DR:
 436        ret = drck->set_indicator_state(drc, sensor_state);
 437        break;
 438    case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
 439        ret = drck->set_allocation_state(drc, sensor_state);
 440        break;
 441    default:
 442        goto out_unimplemented;
 443    }
 444
 445out:
 446    rtas_st(rets, 0, ret);
 447    return;
 448
 449out_unimplemented:
 450    /* currently only DR-related sensors are implemented */
 451    DPRINTF("rtas_set_indicator: sensor/indicator not implemented: %d\n",
 452            sensor_type);
 453    rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
 454}
 455
 456static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 457                                  uint32_t token, uint32_t nargs,
 458                                  target_ulong args, uint32_t nret,
 459                                  target_ulong rets)
 460{
 461    uint32_t sensor_type;
 462    uint32_t sensor_index;
 463    uint32_t sensor_state = 0;
 464    sPAPRDRConnector *drc;
 465    sPAPRDRConnectorClass *drck;
 466    uint32_t ret = RTAS_OUT_SUCCESS;
 467
 468    if (nargs != 2 || nret != 2) {
 469        ret = RTAS_OUT_PARAM_ERROR;
 470        goto out;
 471    }
 472
 473    sensor_type = rtas_ld(args, 0);
 474    sensor_index = rtas_ld(args, 1);
 475
 476    if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
 477        /* currently only DR-related sensors are implemented */
 478        DPRINTF("rtas_get_sensor_state: sensor/indicator not implemented: %d\n",
 479                sensor_type);
 480        ret = RTAS_OUT_NOT_SUPPORTED;
 481        goto out;
 482    }
 483
 484    drc = spapr_dr_connector_by_index(sensor_index);
 485    if (!drc) {
 486        DPRINTF("rtas_get_sensor_state: invalid sensor/DRC index: %xh\n",
 487                sensor_index);
 488        ret = RTAS_OUT_PARAM_ERROR;
 489        goto out;
 490    }
 491    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 492    ret = drck->entity_sense(drc, &sensor_state);
 493
 494out:
 495    rtas_st(rets, 0, ret);
 496    rtas_st(rets, 1, sensor_state);
 497}
 498
 499/* configure-connector work area offsets, int32_t units for field
 500 * indexes, bytes for field offset/len values.
 501 *
 502 * as documented by PAPR+ v2.7, 13.5.3.5
 503 */
 504#define CC_IDX_NODE_NAME_OFFSET 2
 505#define CC_IDX_PROP_NAME_OFFSET 2
 506#define CC_IDX_PROP_LEN 3
 507#define CC_IDX_PROP_DATA_OFFSET 4
 508#define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
 509#define CC_WA_LEN 4096
 510
 511static void configure_connector_st(target_ulong addr, target_ulong offset,
 512                                   const void *buf, size_t len)
 513{
 514    cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
 515                              buf, MIN(len, CC_WA_LEN - offset));
 516}
 517
 518static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
 519                                         sPAPRMachineState *spapr,
 520                                         uint32_t token, uint32_t nargs,
 521                                         target_ulong args, uint32_t nret,
 522                                         target_ulong rets)
 523{
 524    uint64_t wa_addr;
 525    uint64_t wa_offset;
 526    uint32_t drc_index;
 527    sPAPRDRConnector *drc;
 528    sPAPRDRConnectorClass *drck;
 529    sPAPRConfigureConnectorState *ccs;
 530    sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
 531    int rc;
 532    const void *fdt;
 533
 534    if (nargs != 2 || nret != 1) {
 535        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 536        return;
 537    }
 538
 539    wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
 540
 541    drc_index = rtas_ld(wa_addr, 0);
 542    drc = spapr_dr_connector_by_index(drc_index);
 543    if (!drc) {
 544        DPRINTF("rtas_ibm_configure_connector: invalid DRC index: %xh\n",
 545                drc_index);
 546        rc = RTAS_OUT_PARAM_ERROR;
 547        goto out;
 548    }
 549
 550    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 551    fdt = drck->get_fdt(drc, NULL);
 552    if (!fdt) {
 553        DPRINTF("rtas_ibm_configure_connector: Missing FDT for DRC index: %xh\n",
 554                drc_index);
 555        rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
 556        goto out;
 557    }
 558
 559    ccs = spapr_ccs_find(spapr, drc_index);
 560    if (!ccs) {
 561        ccs = g_new0(sPAPRConfigureConnectorState, 1);
 562        (void)drck->get_fdt(drc, &ccs->fdt_offset);
 563        ccs->drc_index = drc_index;
 564        spapr_ccs_add(spapr, ccs);
 565    }
 566
 567    do {
 568        uint32_t tag;
 569        const char *name;
 570        const struct fdt_property *prop;
 571        int fdt_offset_next, prop_len;
 572
 573        tag = fdt_next_tag(fdt, ccs->fdt_offset, &fdt_offset_next);
 574
 575        switch (tag) {
 576        case FDT_BEGIN_NODE:
 577            ccs->fdt_depth++;
 578            name = fdt_get_name(fdt, ccs->fdt_offset, NULL);
 579
 580            /* provide the name of the next OF node */
 581            wa_offset = CC_VAL_DATA_OFFSET;
 582            rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
 583            configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
 584            resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
 585            break;
 586        case FDT_END_NODE:
 587            ccs->fdt_depth--;
 588            if (ccs->fdt_depth == 0) {
 589                /* done sending the device tree, don't need to track
 590                 * the state anymore
 591                 */
 592                drck->set_configured(drc);
 593                spapr_ccs_remove(spapr, ccs);
 594                ccs = NULL;
 595                resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
 596            } else {
 597                resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
 598            }
 599            break;
 600        case FDT_PROP:
 601            prop = fdt_get_property_by_offset(fdt, ccs->fdt_offset,
 602                                              &prop_len);
 603            name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
 604
 605            /* provide the name of the next OF property */
 606            wa_offset = CC_VAL_DATA_OFFSET;
 607            rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
 608            configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
 609
 610            /* provide the length and value of the OF property. data gets
 611             * placed immediately after NULL terminator of the OF property's
 612             * name string
 613             */
 614            wa_offset += strlen(name) + 1,
 615            rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
 616            rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
 617            configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
 618            resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
 619            break;
 620        case FDT_END:
 621            resp = SPAPR_DR_CC_RESPONSE_ERROR;
 622        default:
 623            /* keep seeking for an actionable tag */
 624            break;
 625        }
 626        if (ccs) {
 627            ccs->fdt_offset = fdt_offset_next;
 628        }
 629    } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
 630
 631    rc = resp;
 632out:
 633    rtas_st(rets, 0, rc);
 634}
 635
 636static struct rtas_call {
 637    const char *name;
 638    spapr_rtas_fn fn;
 639} rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
 640
 641target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 642                             uint32_t token, uint32_t nargs, target_ulong args,
 643                             uint32_t nret, target_ulong rets)
 644{
 645    if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
 646        struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
 647
 648        if (call->fn) {
 649            call->fn(cpu, spapr, token, nargs, args, nret, rets);
 650            return H_SUCCESS;
 651        }
 652    }
 653
 654    /* HACK: Some Linux early debug code uses RTAS display-character,
 655     * but assumes the token value is 0xa (which it is on some real
 656     * machines) without looking it up in the device tree.  This
 657     * special case makes this work */
 658    if (token == 0xa) {
 659        rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
 660        return H_SUCCESS;
 661    }
 662
 663    hcall_dprintf("Unknown RTAS token 0x%x\n", token);
 664    rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 665    return H_PARAMETER;
 666}
 667
 668void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
 669{
 670    assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
 671
 672    token -= RTAS_TOKEN_BASE;
 673
 674    assert(!rtas_table[token].name);
 675
 676    rtas_table[token].name = name;
 677    rtas_table[token].fn = fn;
 678}
 679
 680int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr,
 681                                 hwaddr rtas_size)
 682{
 683    int ret;
 684    int i;
 685    uint32_t lrdr_capacity[5];
 686    MachineState *machine = MACHINE(qdev_get_machine());
 687    sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
 688    uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
 689                                memory_region_size(&spapr->hotplug_memory.mr);
 690
 691    ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size);
 692    if (ret < 0) {
 693        fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n",
 694                fdt_strerror(ret));
 695        return ret;
 696    }
 697
 698    ret = qemu_fdt_setprop_cell(fdt, "/rtas", "linux,rtas-base",
 699                                rtas_addr);
 700    if (ret < 0) {
 701        fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n",
 702                fdt_strerror(ret));
 703        return ret;
 704    }
 705
 706    ret = qemu_fdt_setprop_cell(fdt, "/rtas", "linux,rtas-entry",
 707                                rtas_addr);
 708    if (ret < 0) {
 709        fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n",
 710                fdt_strerror(ret));
 711        return ret;
 712    }
 713
 714    ret = qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-size",
 715                                rtas_size);
 716    if (ret < 0) {
 717        fprintf(stderr, "Couldn't add rtas-size property: %s\n",
 718                fdt_strerror(ret));
 719        return ret;
 720    }
 721
 722    for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
 723        struct rtas_call *call = &rtas_table[i];
 724
 725        if (!call->name) {
 726            continue;
 727        }
 728
 729        ret = qemu_fdt_setprop_cell(fdt, "/rtas", call->name,
 730                                    i + RTAS_TOKEN_BASE);
 731        if (ret < 0) {
 732            fprintf(stderr, "Couldn't add rtas token for %s: %s\n",
 733                    call->name, fdt_strerror(ret));
 734            return ret;
 735        }
 736
 737    }
 738
 739    lrdr_capacity[0] = cpu_to_be32(max_hotplug_addr >> 32);
 740    lrdr_capacity[1] = cpu_to_be32(max_hotplug_addr & 0xffffffff);
 741    lrdr_capacity[2] = 0;
 742    lrdr_capacity[3] = cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE);
 743    lrdr_capacity[4] = cpu_to_be32(max_cpus/smp_threads);
 744    ret = qemu_fdt_setprop(fdt, "/rtas", "ibm,lrdr-capacity", lrdr_capacity,
 745                     sizeof(lrdr_capacity));
 746    if (ret < 0) {
 747        fprintf(stderr, "Couldn't add ibm,lrdr-capacity rtas property\n");
 748        return ret;
 749    }
 750
 751    return 0;
 752}
 753
 754static void core_rtas_register_types(void)
 755{
 756    spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
 757                        rtas_display_character);
 758    spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
 759    spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
 760                        rtas_system_reboot);
 761    spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
 762                        rtas_query_cpu_stopped_state);
 763    spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
 764    spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
 765    spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
 766                        "ibm,get-system-parameter",
 767                        rtas_ibm_get_system_parameter);
 768    spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
 769                        "ibm,set-system-parameter",
 770                        rtas_ibm_set_system_parameter);
 771    spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
 772                        rtas_ibm_os_term);
 773    spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
 774                        rtas_set_power_level);
 775    spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
 776                        rtas_get_power_level);
 777    spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
 778                        rtas_set_indicator);
 779    spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
 780                        rtas_get_sensor_state);
 781    spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
 782                        rtas_ibm_configure_connector);
 783}
 784
 785type_init(core_rtas_register_types)
 786