spapr_vty.c 5.8 KB
Newer Older
1
#include "hw/qdev.h"
2
#include "sysemu/char.h"
P
Paolo Bonzini 已提交
3 4
#include "hw/ppc/spapr.h"
#include "hw/ppc/spapr_vio.h"
5 6 7 8 9 10 11 12 13 14

#define VTERM_BUFSIZE   16

typedef struct VIOsPAPRVTYDevice {
    VIOsPAPRDevice sdev;
    CharDriverState *chardev;
    uint32_t in, out;
    uint8_t buf[VTERM_BUFSIZE];
} VIOsPAPRVTYDevice;

15 16 17 18
#define TYPE_VIO_SPAPR_VTY_DEVICE "spapr-vty"
#define VIO_SPAPR_VTY_DEVICE(obj) \
     OBJECT_CHECK(VIOsPAPRVTYDevice, (obj), TYPE_VIO_SPAPR_VTY_DEVICE)

19 20
static int vty_can_receive(void *opaque)
{
21
    VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(opaque);
22 23 24 25 26 27

    return (dev->in - dev->out) < VTERM_BUFSIZE;
}

static void vty_receive(void *opaque, const uint8_t *buf, int size)
{
28
    VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(opaque);
29 30
    int i;

31 32
    if ((dev->in == dev->out) && size) {
        /* toggle line to simulate edge interrupt */
33
        qemu_irq_pulse(spapr_vio_qirq(&dev->sdev));
34
    }
35 36 37 38 39 40 41 42
    for (i = 0; i < size; i++) {
        assert((dev->in - dev->out) < VTERM_BUFSIZE);
        dev->buf[dev->in++ % VTERM_BUFSIZE] = buf[i];
    }
}

static int vty_getchars(VIOsPAPRDevice *sdev, uint8_t *buf, int max)
{
43
    VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
44 45 46 47 48 49 50 51 52 53 54
    int n = 0;

    while ((n < max) && (dev->out != dev->in)) {
        buf[n++] = dev->buf[dev->out++ % VTERM_BUFSIZE];
    }

    return n;
}

void vty_putchars(VIOsPAPRDevice *sdev, uint8_t *buf, int len)
{
55
    VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
56

57 58
    /* FIXME: should check the qemu_chr_fe_write() return value */
    qemu_chr_fe_write(dev->chardev, buf, len);
59 60 61 62
}

static int spapr_vty_init(VIOsPAPRDevice *sdev)
{
63
    VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
64

65 66 67 68 69
    if (!dev->chardev) {
        fprintf(stderr, "spapr-vty: Can't create vty without a chardev!\n");
        exit(1);
    }

70 71 72 73 74 75
    qemu_chr_add_handlers(dev->chardev, vty_can_receive,
                          vty_receive, NULL, dev);

    return 0;
}

76
/* Forward declaration */
77
static target_ulong h_put_term_char(PowerPCCPU *cpu, sPAPREnvironment *spapr,
78 79 80 81 82 83
                                    target_ulong opcode, target_ulong *args)
{
    target_ulong reg = args[0];
    target_ulong len = args[1];
    target_ulong char0_7 = args[2];
    target_ulong char8_15 = args[3];
84
    VIOsPAPRDevice *sdev;
85 86
    uint8_t buf[16];

87
    sdev = vty_lookup(spapr, reg);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    if (!sdev) {
        return H_PARAMETER;
    }

    if (len > 16) {
        return H_PARAMETER;
    }

    *((uint64_t *)buf) = cpu_to_be64(char0_7);
    *((uint64_t *)buf + 1) = cpu_to_be64(char8_15);

    vty_putchars(sdev, buf, len);

    return H_SUCCESS;
}

104
static target_ulong h_get_term_char(PowerPCCPU *cpu, sPAPREnvironment *spapr,
105 106 107 108 109 110
                                    target_ulong opcode, target_ulong *args)
{
    target_ulong reg = args[0];
    target_ulong *len = args + 0;
    target_ulong *char0_7 = args + 1;
    target_ulong *char8_15 = args + 2;
111
    VIOsPAPRDevice *sdev;
112 113
    uint8_t buf[16];

114
    sdev = vty_lookup(spapr, reg);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    if (!sdev) {
        return H_PARAMETER;
    }

    *len = vty_getchars(sdev, buf, sizeof(buf));
    if (*len < 16) {
        memset(buf + *len, 0, 16 - *len);
    }

    *char0_7 = be64_to_cpu(*((uint64_t *)buf));
    *char8_15 = be64_to_cpu(*((uint64_t *)buf + 1));

    return H_SUCCESS;
}

130
void spapr_vty_create(VIOsPAPRBus *bus, CharDriverState *chardev)
131 132 133 134 135 136 137 138
{
    DeviceState *dev;

    dev = qdev_create(&bus->bus, "spapr-vty");
    qdev_prop_set_chr(dev, "chardev", chardev);
    qdev_init_nofail(dev);
}

139
static Property spapr_vty_properties[] = {
140
    DEFINE_SPAPR_PROPERTIES(VIOsPAPRVTYDevice, sdev),
141 142 143 144 145 146
    DEFINE_PROP_CHR("chardev", VIOsPAPRVTYDevice, chardev),
    DEFINE_PROP_END_OF_LIST(),
};

static void spapr_vty_class_init(ObjectClass *klass, void *data)
{
147
    DeviceClass *dc = DEVICE_CLASS(klass);
148 149 150 151 152 153
    VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);

    k->init = spapr_vty_init;
    k->dt_name = "vty";
    k->dt_type = "serial";
    k->dt_compatible = "hvterm1";
154
    dc->props = spapr_vty_properties;
155 156
}

157
static const TypeInfo spapr_vty_info = {
158
    .name          = TYPE_VIO_SPAPR_VTY_DEVICE,
159 160 161
    .parent        = TYPE_VIO_SPAPR_DEVICE,
    .instance_size = sizeof(VIOsPAPRVTYDevice),
    .class_init    = spapr_vty_class_init,
162 163
};

164
VIOsPAPRDevice *spapr_vty_get_default(VIOsPAPRBus *bus)
165 166
{
    VIOsPAPRDevice *sdev, *selected;
A
Anthony Liguori 已提交
167
    BusChild *kid;
168 169 170 171 172 173 174 175

    /*
     * To avoid the console bouncing around we want one VTY to be
     * the "default". We haven't really got anything to go on, so
     * arbitrarily choose the one with the lowest reg value.
     */

    selected = NULL;
A
Anthony Liguori 已提交
176 177 178
    QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
        DeviceState *iter = kid->child;

179
        /* Only look at VTY devices */
180
        if (!object_dynamic_cast(OBJECT(iter), "spapr-vty")) {
181 182 183
            continue;
        }

184
        sdev = VIO_SPAPR_DEVICE(iter);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

        /* First VTY we've found, so it is selected for now */
        if (!selected) {
            selected = sdev;
            continue;
        }

        /* Choose VTY with lowest reg value */
        if (sdev->reg < selected->reg) {
            selected = sdev;
        }
    }

    return selected;
}

201
VIOsPAPRDevice *vty_lookup(sPAPREnvironment *spapr, target_ulong reg)
202 203 204 205 206 207
{
    VIOsPAPRDevice *sdev;

    sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
    if (!sdev && reg == 0) {
        /* Hack for kernel early debug, which always specifies reg==0.
208 209
         * We search all VIO devices, and grab the vty with the lowest
         * reg.  This attempts to mimic existing PowerVM behaviour
210 211
         * (early debug does work there, despite having no vty with
         * reg==0. */
212
        return spapr_vty_get_default(spapr->vio_bus);
213 214 215 216 217
    }

    return sdev;
}

A
Andreas Färber 已提交
218
static void spapr_vty_register_types(void)
219
{
D
David Gibson 已提交
220 221
    spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char);
    spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char);
222
    type_register_static(&spapr_vty_info);
223
}
A
Andreas Färber 已提交
224 225

type_init(spapr_vty_register_types)