]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/char/spapr_vty.c
Use OBJECT_DECLARE_SIMPLE_TYPE when possible
[thirdparty/qemu.git] / hw / char / spapr_vty.c
CommitLineData
0d75590d 1#include "qemu/osdep.h"
7bacfd7f 2#include "qemu/error-report.h"
0b8fa32f 3#include "qemu/module.h"
da34e65c 4#include "qapi/error.h"
4771d756 5#include "cpu.h"
d6454270 6#include "migration/vmstate.h"
4d43a603 7#include "chardev/char-fe.h"
0d09e41a
PB
8#include "hw/ppc/spapr.h"
9#include "hw/ppc/spapr_vio.h"
a27bd6c7 10#include "hw/qdev-properties.h"
db1015e9 11#include "qom/object.h"
4040ab72
DG
12
13#define VTERM_BUFSIZE 16
14
db1015e9 15struct SpaprVioVty {
ce2918cb 16 SpaprVioDevice sdev;
becdfa00 17 CharBackend chardev;
4040ab72
DG
18 uint32_t in, out;
19 uint8_t buf[VTERM_BUFSIZE];
db1015e9 20};
4040ab72 21
fd506b4f 22#define TYPE_VIO_SPAPR_VTY_DEVICE "spapr-vty"
8063396b 23OBJECT_DECLARE_SIMPLE_TYPE(SpaprVioVty, VIO_SPAPR_VTY_DEVICE)
fd506b4f 24
4040ab72
DG
25static int vty_can_receive(void *opaque)
26{
ce2918cb 27 SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(opaque);
4040ab72 28
8a273cbe 29 return VTERM_BUFSIZE - (dev->in - dev->out);
4040ab72
DG
30}
31
32static void vty_receive(void *opaque, const uint8_t *buf, int size)
33{
ce2918cb 34 SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(opaque);
4040ab72
DG
35 int i;
36
0201e2da
DG
37 if ((dev->in == dev->out) && size) {
38 /* toggle line to simulate edge interrupt */
7678b74a 39 spapr_vio_irq_pulse(&dev->sdev);
0201e2da 40 }
4040ab72 41 for (i = 0; i < size; i++) {
7bacfd7f
TH
42 if (dev->in - dev->out >= VTERM_BUFSIZE) {
43 static bool reported;
44 if (!reported) {
45 error_report("VTY input buffer exhausted - characters dropped."
46 " (input size = %i)", size);
47 reported = true;
48 }
49 break;
50 }
4040ab72
DG
51 dev->buf[dev->in++ % VTERM_BUFSIZE] = buf[i];
52 }
53}
54
ce2918cb 55static int vty_getchars(SpaprVioDevice *sdev, uint8_t *buf, int max)
4040ab72 56{
ce2918cb 57 SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev);
4040ab72
DG
58 int n = 0;
59
60 while ((n < max) && (dev->out != dev->in)) {
fd38b162
PM
61 /*
62 * Long ago, PowerVM's vty implementation had a bug where it
63 * inserted a \0 after every \r going to the guest. Existing
64 * guests have a workaround for this which removes every \0
65 * immediately following a \r. To avoid triggering this
66 * workaround, we stop before inserting a \0 if the preceding
67 * character in the output buffer is a \r.
68 */
69 if (n > 0 && (buf[n - 1] == '\r') &&
70 (dev->buf[dev->out % VTERM_BUFSIZE] == '\0')) {
71 break;
6c3bc244 72 }
fd38b162 73 buf[n++] = dev->buf[dev->out++ % VTERM_BUFSIZE];
4040ab72
DG
74 }
75
5345fdb4 76 qemu_chr_fe_accept_input(&dev->chardev);
7770b6f7 77
4040ab72
DG
78 return n;
79}
80
ce2918cb 81void vty_putchars(SpaprVioDevice *sdev, uint8_t *buf, int len)
4040ab72 82{
ce2918cb 83 SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev);
4040ab72 84
6ab3fc32
DB
85 /* XXX this blocks entire thread. Rewrite to use
86 * qemu_chr_fe_write and background I/O callbacks */
5345fdb4 87 qemu_chr_fe_write_all(&dev->chardev, buf, len);
4040ab72
DG
88}
89
ce2918cb 90static void spapr_vty_realize(SpaprVioDevice *sdev, Error **errp)
4040ab72 91{
ce2918cb 92 SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev);
4040ab72 93
30650701 94 if (!qemu_chr_fe_backend_connected(&dev->chardev)) {
28b07e73
MA
95 error_setg(errp, "chardev property not set");
96 return;
57285302
ME
97 }
98
5345fdb4 99 qemu_chr_fe_set_handlers(&dev->chardev, vty_can_receive,
81517ba3 100 vty_receive, NULL, NULL, dev, NULL, true);
4040ab72
DG
101}
102
3feef8ad 103/* Forward declaration */
ce2918cb 104static target_ulong h_put_term_char(PowerPCCPU *cpu, SpaprMachineState *spapr,
4040ab72
DG
105 target_ulong opcode, target_ulong *args)
106{
107 target_ulong reg = args[0];
108 target_ulong len = args[1];
109 target_ulong char0_7 = args[2];
110 target_ulong char8_15 = args[3];
ce2918cb 111 SpaprVioDevice *sdev;
4040ab72
DG
112 uint8_t buf[16];
113
3feef8ad 114 sdev = vty_lookup(spapr, reg);
4040ab72
DG
115 if (!sdev) {
116 return H_PARAMETER;
117 }
118
119 if (len > 16) {
120 return H_PARAMETER;
121 }
122
123 *((uint64_t *)buf) = cpu_to_be64(char0_7);
124 *((uint64_t *)buf + 1) = cpu_to_be64(char8_15);
125
126 vty_putchars(sdev, buf, len);
127
128 return H_SUCCESS;
129}
130
ce2918cb 131static target_ulong h_get_term_char(PowerPCCPU *cpu, SpaprMachineState *spapr,
4040ab72
DG
132 target_ulong opcode, target_ulong *args)
133{
134 target_ulong reg = args[0];
135 target_ulong *len = args + 0;
136 target_ulong *char0_7 = args + 1;
137 target_ulong *char8_15 = args + 2;
ce2918cb 138 SpaprVioDevice *sdev;
4040ab72
DG
139 uint8_t buf[16];
140
3feef8ad 141 sdev = vty_lookup(spapr, reg);
4040ab72
DG
142 if (!sdev) {
143 return H_PARAMETER;
144 }
145
146 *len = vty_getchars(sdev, buf, sizeof(buf));
147 if (*len < 16) {
148 memset(buf + *len, 0, 16 - *len);
149 }
150
151 *char0_7 = be64_to_cpu(*((uint64_t *)buf));
152 *char8_15 = be64_to_cpu(*((uint64_t *)buf + 1));
153
154 return H_SUCCESS;
155}
156
ce2918cb 157void spapr_vty_create(SpaprVioBus *bus, Chardev *chardev)
4040ab72
DG
158{
159 DeviceState *dev;
160
3e80f690 161 dev = qdev_new("spapr-vty");
4040ab72 162 qdev_prop_set_chr(dev, "chardev", chardev);
3e80f690 163 qdev_realize_and_unref(dev, &bus->bus, &error_fatal);
4040ab72
DG
164}
165
3954d33a 166static Property spapr_vty_properties[] = {
ce2918cb
DG
167 DEFINE_SPAPR_PROPERTIES(SpaprVioVty, sdev),
168 DEFINE_PROP_CHR("chardev", SpaprVioVty, chardev),
3954d33a
AL
169 DEFINE_PROP_END_OF_LIST(),
170};
171
db1b58e9
DG
172static const VMStateDescription vmstate_spapr_vty = {
173 .name = "spapr_vty",
174 .version_id = 1,
175 .minimum_version_id = 1,
3aff6c2f 176 .fields = (VMStateField[]) {
ce2918cb 177 VMSTATE_SPAPR_VIO(sdev, SpaprVioVty),
db1b58e9 178
ce2918cb
DG
179 VMSTATE_UINT32(in, SpaprVioVty),
180 VMSTATE_UINT32(out, SpaprVioVty),
181 VMSTATE_BUFFER(buf, SpaprVioVty),
db1b58e9
DG
182 VMSTATE_END_OF_LIST()
183 },
184};
185
3954d33a
AL
186static void spapr_vty_class_init(ObjectClass *klass, void *data)
187{
39bffca2 188 DeviceClass *dc = DEVICE_CLASS(klass);
ce2918cb 189 SpaprVioDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
3954d33a 190
28b07e73 191 k->realize = spapr_vty_realize;
3954d33a
AL
192 k->dt_name = "vty";
193 k->dt_type = "serial";
194 k->dt_compatible = "hvterm1";
29fdedfe 195 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
4f67d30b 196 device_class_set_props(dc, spapr_vty_properties);
db1b58e9 197 dc->vmsd = &vmstate_spapr_vty;
3954d33a
AL
198}
199
8c43a6f0 200static const TypeInfo spapr_vty_info = {
fd506b4f 201 .name = TYPE_VIO_SPAPR_VTY_DEVICE,
39bffca2 202 .parent = TYPE_VIO_SPAPR_DEVICE,
ce2918cb 203 .instance_size = sizeof(SpaprVioVty),
39bffca2 204 .class_init = spapr_vty_class_init,
4040ab72
DG
205};
206
ce2918cb 207SpaprVioDevice *spapr_vty_get_default(SpaprVioBus *bus)
98331f8a 208{
ce2918cb 209 SpaprVioDevice *sdev, *selected;
0866aca1 210 BusChild *kid;
98331f8a
DG
211
212 /*
213 * To avoid the console bouncing around we want one VTY to be
214 * the "default". We haven't really got anything to go on, so
215 * arbitrarily choose the one with the lowest reg value.
216 */
217
218 selected = NULL;
0866aca1
AL
219 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
220 DeviceState *iter = kid->child;
221
98331f8a 222 /* Only look at VTY devices */
e275934d 223 if (!object_dynamic_cast(OBJECT(iter), TYPE_VIO_SPAPR_VTY_DEVICE)) {
98331f8a
DG
224 continue;
225 }
226
fd506b4f 227 sdev = VIO_SPAPR_DEVICE(iter);
98331f8a
DG
228
229 /* First VTY we've found, so it is selected for now */
230 if (!selected) {
231 selected = sdev;
232 continue;
233 }
234
235 /* Choose VTY with lowest reg value */
236 if (sdev->reg < selected->reg) {
237 selected = sdev;
238 }
239 }
240
241 return selected;
242}
243
ce2918cb 244SpaprVioDevice *vty_lookup(SpaprMachineState *spapr, target_ulong reg)
3feef8ad 245{
ce2918cb 246 SpaprVioDevice *sdev;
3feef8ad
DG
247
248 sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
249 if (!sdev && reg == 0) {
3feef8ad 250 /* Hack for kernel early debug, which always specifies reg==0.
98331f8a
DG
251 * We search all VIO devices, and grab the vty with the lowest
252 * reg. This attempts to mimic existing PowerVM behaviour
3feef8ad
DG
253 * (early debug does work there, despite having no vty with
254 * reg==0. */
98331f8a 255 return spapr_vty_get_default(spapr->vio_bus);
3feef8ad
DG
256 }
257
0f888bfa
DG
258 if (!object_dynamic_cast(OBJECT(sdev), TYPE_VIO_SPAPR_VTY_DEVICE)) {
259 return NULL;
260 }
261
3feef8ad
DG
262 return sdev;
263}
264
83f7d43a 265static void spapr_vty_register_types(void)
4040ab72 266{
1fc02533
DG
267 spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char);
268 spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char);
39bffca2 269 type_register_static(&spapr_vty_info);
4040ab72 270}
83f7d43a
AF
271
272type_init(spapr_vty_register_types)