2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/qdev.h"
|
2013-04-08 18:55:25 +04:00
|
|
|
#include "sysemu/char.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/ppc/spapr.h"
|
|
|
|
#include "hw/ppc/spapr_vio.h"
|
2011-04-01 08:15:21 +04:00
|
|
|
|
|
|
|
#define VTERM_BUFSIZE 16
|
|
|
|
|
|
|
|
typedef struct VIOsPAPRVTYDevice {
|
|
|
|
VIOsPAPRDevice sdev;
|
|
|
|
CharDriverState *chardev;
|
|
|
|
uint32_t in, out;
|
|
|
|
uint8_t buf[VTERM_BUFSIZE];
|
|
|
|
} VIOsPAPRVTYDevice;
|
|
|
|
|
2013-04-07 23:08:16 +04:00
|
|
|
#define TYPE_VIO_SPAPR_VTY_DEVICE "spapr-vty"
|
|
|
|
#define VIO_SPAPR_VTY_DEVICE(obj) \
|
|
|
|
OBJECT_CHECK(VIOsPAPRVTYDevice, (obj), TYPE_VIO_SPAPR_VTY_DEVICE)
|
|
|
|
|
2011-04-01 08:15:21 +04:00
|
|
|
static int vty_can_receive(void *opaque)
|
|
|
|
{
|
2013-04-07 23:08:16 +04:00
|
|
|
VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(opaque);
|
2011-04-01 08:15:21 +04:00
|
|
|
|
|
|
|
return (dev->in - dev->out) < VTERM_BUFSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vty_receive(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
2013-04-07 23:08:16 +04:00
|
|
|
VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(opaque);
|
2011-04-01 08:15:21 +04:00
|
|
|
int i;
|
|
|
|
|
2011-04-01 08:15:27 +04:00
|
|
|
if ((dev->in == dev->out) && size) {
|
|
|
|
/* toggle line to simulate edge interrupt */
|
2012-08-07 20:10:32 +04:00
|
|
|
qemu_irq_pulse(spapr_vio_qirq(&dev->sdev));
|
2011-04-01 08:15:27 +04:00
|
|
|
}
|
2011-04-01 08:15:21 +04:00
|
|
|
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)
|
|
|
|
{
|
2013-04-07 23:08:16 +04:00
|
|
|
VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
|
2011-04-01 08:15:21 +04:00
|
|
|
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)
|
|
|
|
{
|
2013-04-07 23:08:16 +04:00
|
|
|
VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
|
2011-04-01 08:15:21 +04:00
|
|
|
|
2011-08-15 20:17:28 +04:00
|
|
|
/* FIXME: should check the qemu_chr_fe_write() return value */
|
|
|
|
qemu_chr_fe_write(dev->chardev, buf, len);
|
2011-04-01 08:15:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int spapr_vty_init(VIOsPAPRDevice *sdev)
|
|
|
|
{
|
2013-04-07 23:08:16 +04:00
|
|
|
VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
|
2011-04-01 08:15:21 +04:00
|
|
|
|
2011-11-13 21:18:59 +04:00
|
|
|
if (!dev->chardev) {
|
|
|
|
fprintf(stderr, "spapr-vty: Can't create vty without a chardev!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-04-01 08:15:21 +04:00
|
|
|
qemu_chr_add_handlers(dev->chardev, vty_can_receive,
|
|
|
|
vty_receive, NULL, dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-13 21:19:01 +04:00
|
|
|
/* Forward declaration */
|
2012-05-03 08:23:01 +04:00
|
|
|
static target_ulong h_put_term_char(PowerPCCPU *cpu, sPAPREnvironment *spapr,
|
2011-04-01 08:15:21 +04:00
|
|
|
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];
|
2011-11-13 21:19:01 +04:00
|
|
|
VIOsPAPRDevice *sdev;
|
2011-04-01 08:15:21 +04:00
|
|
|
uint8_t buf[16];
|
|
|
|
|
2011-11-13 21:19:01 +04:00
|
|
|
sdev = vty_lookup(spapr, reg);
|
2011-04-01 08:15:21 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-03 08:23:01 +04:00
|
|
|
static target_ulong h_get_term_char(PowerPCCPU *cpu, sPAPREnvironment *spapr,
|
2011-04-01 08:15:21 +04:00
|
|
|
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;
|
2011-11-13 21:19:01 +04:00
|
|
|
VIOsPAPRDevice *sdev;
|
2011-04-01 08:15:21 +04:00
|
|
|
uint8_t buf[16];
|
|
|
|
|
2011-11-13 21:19:01 +04:00
|
|
|
sdev = vty_lookup(spapr, reg);
|
2011-04-01 08:15:21 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-04-25 21:55:41 +04:00
|
|
|
void spapr_vty_create(VIOsPAPRBus *bus, CharDriverState *chardev)
|
2011-04-01 08:15:21 +04:00
|
|
|
{
|
|
|
|
DeviceState *dev;
|
|
|
|
|
|
|
|
dev = qdev_create(&bus->bus, "spapr-vty");
|
|
|
|
qdev_prop_set_chr(dev, "chardev", chardev);
|
|
|
|
qdev_init_nofail(dev);
|
|
|
|
}
|
|
|
|
|
2011-12-16 02:31:06 +04:00
|
|
|
static Property spapr_vty_properties[] = {
|
2012-06-27 08:50:44 +04:00
|
|
|
DEFINE_SPAPR_PROPERTIES(VIOsPAPRVTYDevice, sdev),
|
2011-12-16 02:31:06 +04:00
|
|
|
DEFINE_PROP_CHR("chardev", VIOsPAPRVTYDevice, chardev),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2013-07-18 23:32:57 +04:00
|
|
|
static const VMStateDescription vmstate_spapr_vty = {
|
|
|
|
.name = "spapr_vty",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
|
|
|
.fields = (VMStateField []) {
|
|
|
|
VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVTYDevice),
|
|
|
|
|
|
|
|
VMSTATE_UINT32(in, VIOsPAPRVTYDevice),
|
|
|
|
VMSTATE_UINT32(out, VIOsPAPRVTYDevice),
|
|
|
|
VMSTATE_BUFFER(buf, VIOsPAPRVTYDevice),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-12-16 02:31:06 +04:00
|
|
|
static void spapr_vty_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-16 02:31:06 +04:00
|
|
|
VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = spapr_vty_init;
|
|
|
|
k->dt_name = "vty";
|
|
|
|
k->dt_type = "serial";
|
|
|
|
k->dt_compatible = "hvterm1";
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->props = spapr_vty_properties;
|
2013-07-18 23:32:57 +04:00
|
|
|
dc->vmsd = &vmstate_spapr_vty;
|
2011-12-16 02:31:06 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo spapr_vty_info = {
|
2013-04-07 23:08:16 +04:00
|
|
|
.name = TYPE_VIO_SPAPR_VTY_DEVICE,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_VIO_SPAPR_DEVICE,
|
|
|
|
.instance_size = sizeof(VIOsPAPRVTYDevice),
|
|
|
|
.class_init = spapr_vty_class_init,
|
2011-04-01 08:15:21 +04:00
|
|
|
};
|
|
|
|
|
2011-12-13 08:24:34 +04:00
|
|
|
VIOsPAPRDevice *spapr_vty_get_default(VIOsPAPRBus *bus)
|
2011-12-12 22:24:33 +04:00
|
|
|
{
|
|
|
|
VIOsPAPRDevice *sdev, *selected;
|
2011-12-24 01:34:39 +04:00
|
|
|
BusChild *kid;
|
2011-12-12 22:24:33 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2011-12-24 01:34:39 +04:00
|
|
|
QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
|
|
|
|
DeviceState *iter = kid->child;
|
|
|
|
|
2011-12-12 22:24:33 +04:00
|
|
|
/* Only look at VTY devices */
|
2011-12-16 02:31:06 +04:00
|
|
|
if (!object_dynamic_cast(OBJECT(iter), "spapr-vty")) {
|
2011-12-12 22:24:33 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-04-07 23:08:16 +04:00
|
|
|
sdev = VIO_SPAPR_DEVICE(iter);
|
2011-12-12 22:24:33 +04:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2012-04-04 09:02:07 +04:00
|
|
|
VIOsPAPRDevice *vty_lookup(sPAPREnvironment *spapr, target_ulong reg)
|
2011-11-13 21:19:01 +04:00
|
|
|
{
|
|
|
|
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.
|
2011-12-12 22:24:33 +04:00
|
|
|
* We search all VIO devices, and grab the vty with the lowest
|
|
|
|
* reg. This attempts to mimic existing PowerVM behaviour
|
2011-11-13 21:19:01 +04:00
|
|
|
* (early debug does work there, despite having no vty with
|
|
|
|
* reg==0. */
|
2011-12-12 22:24:33 +04:00
|
|
|
return spapr_vty_get_default(spapr->vio_bus);
|
2011-11-13 21:19:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return sdev;
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void spapr_vty_register_types(void)
|
2011-04-01 08:15:21 +04:00
|
|
|
{
|
2011-12-12 22:24:28 +04:00
|
|
|
spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char);
|
|
|
|
spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char);
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&spapr_vty_info);
|
2011-04-01 08:15:21 +04:00
|
|
|
}
|
2012-02-09 18:20:55 +04:00
|
|
|
|
|
|
|
type_init(spapr_vty_register_types)
|