1 #include "qemu/osdep.h"
2 #include "qemu/error-report.h"
3 #include "qemu/module.h"
4 #include "qapi/error.h"
6 #include "migration/vmstate.h"
7 #include "chardev/char-fe.h"
8 #include "hw/ppc/spapr.h"
9 #include "hw/ppc/spapr_vio.h"
10 #include "hw/qdev-properties.h"
11 #include "qom/object.h"
13 #define VTERM_BUFSIZE 16
19 uint8_t buf
[VTERM_BUFSIZE
];
21 typedef struct SpaprVioVty SpaprVioVty
;
23 #define TYPE_VIO_SPAPR_VTY_DEVICE "spapr-vty"
24 DECLARE_INSTANCE_CHECKER(SpaprVioVty
, VIO_SPAPR_VTY_DEVICE
,
25 TYPE_VIO_SPAPR_VTY_DEVICE
)
27 static int vty_can_receive(void *opaque
)
29 SpaprVioVty
*dev
= VIO_SPAPR_VTY_DEVICE(opaque
);
31 return VTERM_BUFSIZE
- (dev
->in
- dev
->out
);
34 static void vty_receive(void *opaque
, const uint8_t *buf
, int size
)
36 SpaprVioVty
*dev
= VIO_SPAPR_VTY_DEVICE(opaque
);
39 if ((dev
->in
== dev
->out
) && size
) {
40 /* toggle line to simulate edge interrupt */
41 spapr_vio_irq_pulse(&dev
->sdev
);
43 for (i
= 0; i
< size
; i
++) {
44 if (dev
->in
- dev
->out
>= VTERM_BUFSIZE
) {
47 error_report("VTY input buffer exhausted - characters dropped."
48 " (input size = %i)", size
);
53 dev
->buf
[dev
->in
++ % VTERM_BUFSIZE
] = buf
[i
];
57 static int vty_getchars(SpaprVioDevice
*sdev
, uint8_t *buf
, int max
)
59 SpaprVioVty
*dev
= VIO_SPAPR_VTY_DEVICE(sdev
);
62 while ((n
< max
) && (dev
->out
!= dev
->in
)) {
64 * Long ago, PowerVM's vty implementation had a bug where it
65 * inserted a \0 after every \r going to the guest. Existing
66 * guests have a workaround for this which removes every \0
67 * immediately following a \r. To avoid triggering this
68 * workaround, we stop before inserting a \0 if the preceding
69 * character in the output buffer is a \r.
71 if (n
> 0 && (buf
[n
- 1] == '\r') &&
72 (dev
->buf
[dev
->out
% VTERM_BUFSIZE
] == '\0')) {
75 buf
[n
++] = dev
->buf
[dev
->out
++ % VTERM_BUFSIZE
];
78 qemu_chr_fe_accept_input(&dev
->chardev
);
83 void vty_putchars(SpaprVioDevice
*sdev
, uint8_t *buf
, int len
)
85 SpaprVioVty
*dev
= VIO_SPAPR_VTY_DEVICE(sdev
);
87 /* XXX this blocks entire thread. Rewrite to use
88 * qemu_chr_fe_write and background I/O callbacks */
89 qemu_chr_fe_write_all(&dev
->chardev
, buf
, len
);
92 static void spapr_vty_realize(SpaprVioDevice
*sdev
, Error
**errp
)
94 SpaprVioVty
*dev
= VIO_SPAPR_VTY_DEVICE(sdev
);
96 if (!qemu_chr_fe_backend_connected(&dev
->chardev
)) {
97 error_setg(errp
, "chardev property not set");
101 qemu_chr_fe_set_handlers(&dev
->chardev
, vty_can_receive
,
102 vty_receive
, NULL
, NULL
, dev
, NULL
, true);
105 /* Forward declaration */
106 static target_ulong
h_put_term_char(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
107 target_ulong opcode
, target_ulong
*args
)
109 target_ulong reg
= args
[0];
110 target_ulong len
= args
[1];
111 target_ulong char0_7
= args
[2];
112 target_ulong char8_15
= args
[3];
113 SpaprVioDevice
*sdev
;
116 sdev
= vty_lookup(spapr
, reg
);
125 *((uint64_t *)buf
) = cpu_to_be64(char0_7
);
126 *((uint64_t *)buf
+ 1) = cpu_to_be64(char8_15
);
128 vty_putchars(sdev
, buf
, len
);
133 static target_ulong
h_get_term_char(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
134 target_ulong opcode
, target_ulong
*args
)
136 target_ulong reg
= args
[0];
137 target_ulong
*len
= args
+ 0;
138 target_ulong
*char0_7
= args
+ 1;
139 target_ulong
*char8_15
= args
+ 2;
140 SpaprVioDevice
*sdev
;
143 sdev
= vty_lookup(spapr
, reg
);
148 *len
= vty_getchars(sdev
, buf
, sizeof(buf
));
150 memset(buf
+ *len
, 0, 16 - *len
);
153 *char0_7
= be64_to_cpu(*((uint64_t *)buf
));
154 *char8_15
= be64_to_cpu(*((uint64_t *)buf
+ 1));
159 void spapr_vty_create(SpaprVioBus
*bus
, Chardev
*chardev
)
163 dev
= qdev_new("spapr-vty");
164 qdev_prop_set_chr(dev
, "chardev", chardev
);
165 qdev_realize_and_unref(dev
, &bus
->bus
, &error_fatal
);
168 static Property spapr_vty_properties
[] = {
169 DEFINE_SPAPR_PROPERTIES(SpaprVioVty
, sdev
),
170 DEFINE_PROP_CHR("chardev", SpaprVioVty
, chardev
),
171 DEFINE_PROP_END_OF_LIST(),
174 static const VMStateDescription vmstate_spapr_vty
= {
177 .minimum_version_id
= 1,
178 .fields
= (VMStateField
[]) {
179 VMSTATE_SPAPR_VIO(sdev
, SpaprVioVty
),
181 VMSTATE_UINT32(in
, SpaprVioVty
),
182 VMSTATE_UINT32(out
, SpaprVioVty
),
183 VMSTATE_BUFFER(buf
, SpaprVioVty
),
184 VMSTATE_END_OF_LIST()
188 static void spapr_vty_class_init(ObjectClass
*klass
, void *data
)
190 DeviceClass
*dc
= DEVICE_CLASS(klass
);
191 SpaprVioDeviceClass
*k
= VIO_SPAPR_DEVICE_CLASS(klass
);
193 k
->realize
= spapr_vty_realize
;
195 k
->dt_type
= "serial";
196 k
->dt_compatible
= "hvterm1";
197 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
198 device_class_set_props(dc
, spapr_vty_properties
);
199 dc
->vmsd
= &vmstate_spapr_vty
;
202 static const TypeInfo spapr_vty_info
= {
203 .name
= TYPE_VIO_SPAPR_VTY_DEVICE
,
204 .parent
= TYPE_VIO_SPAPR_DEVICE
,
205 .instance_size
= sizeof(SpaprVioVty
),
206 .class_init
= spapr_vty_class_init
,
209 SpaprVioDevice
*spapr_vty_get_default(SpaprVioBus
*bus
)
211 SpaprVioDevice
*sdev
, *selected
;
215 * To avoid the console bouncing around we want one VTY to be
216 * the "default". We haven't really got anything to go on, so
217 * arbitrarily choose the one with the lowest reg value.
221 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
222 DeviceState
*iter
= kid
->child
;
224 /* Only look at VTY devices */
225 if (!object_dynamic_cast(OBJECT(iter
), TYPE_VIO_SPAPR_VTY_DEVICE
)) {
229 sdev
= VIO_SPAPR_DEVICE(iter
);
231 /* First VTY we've found, so it is selected for now */
237 /* Choose VTY with lowest reg value */
238 if (sdev
->reg
< selected
->reg
) {
246 SpaprVioDevice
*vty_lookup(SpaprMachineState
*spapr
, target_ulong reg
)
248 SpaprVioDevice
*sdev
;
250 sdev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
251 if (!sdev
&& reg
== 0) {
252 /* Hack for kernel early debug, which always specifies reg==0.
253 * We search all VIO devices, and grab the vty with the lowest
254 * reg. This attempts to mimic existing PowerVM behaviour
255 * (early debug does work there, despite having no vty with
257 return spapr_vty_get_default(spapr
->vio_bus
);
260 if (!object_dynamic_cast(OBJECT(sdev
), TYPE_VIO_SPAPR_VTY_DEVICE
)) {
267 static void spapr_vty_register_types(void)
269 spapr_register_hypercall(H_PUT_TERM_CHAR
, h_put_term_char
);
270 spapr_register_hypercall(H_GET_TERM_CHAR
, h_get_term_char
);
271 type_register_static(&spapr_vty_info
);
274 type_init(spapr_vty_register_types
)