]> Git Repo - qemu.git/blame - hw/char/spapr_vty.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-usb-3' into staging
[qemu.git] / hw / char / spapr_vty.c
CommitLineData
83c9f4ca 1#include "hw/qdev.h"
dccfcd0e 2#include "sysemu/char.h"
0d09e41a
PB
3#include "hw/ppc/spapr.h"
4#include "hw/ppc/spapr_vio.h"
4040ab72
DG
5
6#define VTERM_BUFSIZE 16
7
8typedef struct VIOsPAPRVTYDevice {
9 VIOsPAPRDevice sdev;
10 CharDriverState *chardev;
11 uint32_t in, out;
12 uint8_t buf[VTERM_BUFSIZE];
13} VIOsPAPRVTYDevice;
14
fd506b4f
DG
15#define TYPE_VIO_SPAPR_VTY_DEVICE "spapr-vty"
16#define VIO_SPAPR_VTY_DEVICE(obj) \
17 OBJECT_CHECK(VIOsPAPRVTYDevice, (obj), TYPE_VIO_SPAPR_VTY_DEVICE)
18
4040ab72
DG
19static int vty_can_receive(void *opaque)
20{
fd506b4f 21 VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(opaque);
4040ab72
DG
22
23 return (dev->in - dev->out) < VTERM_BUFSIZE;
24}
25
26static void vty_receive(void *opaque, const uint8_t *buf, int size)
27{
fd506b4f 28 VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(opaque);
4040ab72
DG
29 int i;
30
0201e2da
DG
31 if ((dev->in == dev->out) && size) {
32 /* toggle line to simulate edge interrupt */
a307d594 33 qemu_irq_pulse(spapr_vio_qirq(&dev->sdev));
0201e2da 34 }
4040ab72
DG
35 for (i = 0; i < size; i++) {
36 assert((dev->in - dev->out) < VTERM_BUFSIZE);
37 dev->buf[dev->in++ % VTERM_BUFSIZE] = buf[i];
38 }
39}
40
41static int vty_getchars(VIOsPAPRDevice *sdev, uint8_t *buf, int max)
42{
fd506b4f 43 VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
4040ab72
DG
44 int n = 0;
45
46 while ((n < max) && (dev->out != dev->in)) {
47 buf[n++] = dev->buf[dev->out++ % VTERM_BUFSIZE];
48 }
49
7770b6f7
AB
50 qemu_chr_accept_input(dev->chardev);
51
4040ab72
DG
52 return n;
53}
54
55void vty_putchars(VIOsPAPRDevice *sdev, uint8_t *buf, int len)
56{
fd506b4f 57 VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
4040ab72 58
2cc6e0a1
AL
59 /* FIXME: should check the qemu_chr_fe_write() return value */
60 qemu_chr_fe_write(dev->chardev, buf, len);
4040ab72
DG
61}
62
63static int spapr_vty_init(VIOsPAPRDevice *sdev)
64{
fd506b4f 65 VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev);
4040ab72 66
57285302
ME
67 if (!dev->chardev) {
68 fprintf(stderr, "spapr-vty: Can't create vty without a chardev!\n");
69 exit(1);
70 }
71
4040ab72
DG
72 qemu_chr_add_handlers(dev->chardev, vty_can_receive,
73 vty_receive, NULL, dev);
74
75 return 0;
76}
77
3feef8ad 78/* Forward declaration */
b13ce26d 79static target_ulong h_put_term_char(PowerPCCPU *cpu, sPAPREnvironment *spapr,
4040ab72
DG
80 target_ulong opcode, target_ulong *args)
81{
82 target_ulong reg = args[0];
83 target_ulong len = args[1];
84 target_ulong char0_7 = args[2];
85 target_ulong char8_15 = args[3];
3feef8ad 86 VIOsPAPRDevice *sdev;
4040ab72
DG
87 uint8_t buf[16];
88
3feef8ad 89 sdev = vty_lookup(spapr, reg);
4040ab72
DG
90 if (!sdev) {
91 return H_PARAMETER;
92 }
93
94 if (len > 16) {
95 return H_PARAMETER;
96 }
97
98 *((uint64_t *)buf) = cpu_to_be64(char0_7);
99 *((uint64_t *)buf + 1) = cpu_to_be64(char8_15);
100
101 vty_putchars(sdev, buf, len);
102
103 return H_SUCCESS;
104}
105
b13ce26d 106static target_ulong h_get_term_char(PowerPCCPU *cpu, sPAPREnvironment *spapr,
4040ab72
DG
107 target_ulong opcode, target_ulong *args)
108{
109 target_ulong reg = args[0];
110 target_ulong *len = args + 0;
111 target_ulong *char0_7 = args + 1;
112 target_ulong *char8_15 = args + 2;
3feef8ad 113 VIOsPAPRDevice *sdev;
4040ab72
DG
114 uint8_t buf[16];
115
3feef8ad 116 sdev = vty_lookup(spapr, reg);
4040ab72
DG
117 if (!sdev) {
118 return H_PARAMETER;
119 }
120
121 *len = vty_getchars(sdev, buf, sizeof(buf));
122 if (*len < 16) {
123 memset(buf + *len, 0, 16 - *len);
124 }
125
126 *char0_7 = be64_to_cpu(*((uint64_t *)buf));
127 *char8_15 = be64_to_cpu(*((uint64_t *)buf + 1));
128
129 return H_SUCCESS;
130}
131
d601fac4 132void spapr_vty_create(VIOsPAPRBus *bus, CharDriverState *chardev)
4040ab72
DG
133{
134 DeviceState *dev;
135
136 dev = qdev_create(&bus->bus, "spapr-vty");
4040ab72
DG
137 qdev_prop_set_chr(dev, "chardev", chardev);
138 qdev_init_nofail(dev);
139}
140
3954d33a 141static Property spapr_vty_properties[] = {
ad0ebb91 142 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVTYDevice, sdev),
3954d33a
AL
143 DEFINE_PROP_CHR("chardev", VIOsPAPRVTYDevice, chardev),
144 DEFINE_PROP_END_OF_LIST(),
145};
146
db1b58e9
DG
147static const VMStateDescription vmstate_spapr_vty = {
148 .name = "spapr_vty",
149 .version_id = 1,
150 .minimum_version_id = 1,
151 .minimum_version_id_old = 1,
152 .fields = (VMStateField []) {
153 VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVTYDevice),
154
155 VMSTATE_UINT32(in, VIOsPAPRVTYDevice),
156 VMSTATE_UINT32(out, VIOsPAPRVTYDevice),
157 VMSTATE_BUFFER(buf, VIOsPAPRVTYDevice),
158 VMSTATE_END_OF_LIST()
159 },
160};
161
3954d33a
AL
162static void spapr_vty_class_init(ObjectClass *klass, void *data)
163{
39bffca2 164 DeviceClass *dc = DEVICE_CLASS(klass);
3954d33a
AL
165 VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
166
167 k->init = spapr_vty_init;
168 k->dt_name = "vty";
169 k->dt_type = "serial";
170 k->dt_compatible = "hvterm1";
29fdedfe 171 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
39bffca2 172 dc->props = spapr_vty_properties;
db1b58e9 173 dc->vmsd = &vmstate_spapr_vty;
3954d33a
AL
174}
175
8c43a6f0 176static const TypeInfo spapr_vty_info = {
fd506b4f 177 .name = TYPE_VIO_SPAPR_VTY_DEVICE,
39bffca2
AL
178 .parent = TYPE_VIO_SPAPR_DEVICE,
179 .instance_size = sizeof(VIOsPAPRVTYDevice),
180 .class_init = spapr_vty_class_init,
4040ab72
DG
181};
182
68f3a94c 183VIOsPAPRDevice *spapr_vty_get_default(VIOsPAPRBus *bus)
98331f8a
DG
184{
185 VIOsPAPRDevice *sdev, *selected;
0866aca1 186 BusChild *kid;
98331f8a
DG
187
188 /*
189 * To avoid the console bouncing around we want one VTY to be
190 * the "default". We haven't really got anything to go on, so
191 * arbitrarily choose the one with the lowest reg value.
192 */
193
194 selected = NULL;
0866aca1
AL
195 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
196 DeviceState *iter = kid->child;
197
98331f8a 198 /* Only look at VTY devices */
3954d33a 199 if (!object_dynamic_cast(OBJECT(iter), "spapr-vty")) {
98331f8a
DG
200 continue;
201 }
202
fd506b4f 203 sdev = VIO_SPAPR_DEVICE(iter);
98331f8a
DG
204
205 /* First VTY we've found, so it is selected for now */
206 if (!selected) {
207 selected = sdev;
208 continue;
209 }
210
211 /* Choose VTY with lowest reg value */
212 if (sdev->reg < selected->reg) {
213 selected = sdev;
214 }
215 }
216
217 return selected;
218}
219
5f2e2ba2 220VIOsPAPRDevice *vty_lookup(sPAPREnvironment *spapr, target_ulong reg)
3feef8ad
DG
221{
222 VIOsPAPRDevice *sdev;
223
224 sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
225 if (!sdev && reg == 0) {
3feef8ad 226 /* Hack for kernel early debug, which always specifies reg==0.
98331f8a
DG
227 * We search all VIO devices, and grab the vty with the lowest
228 * reg. This attempts to mimic existing PowerVM behaviour
3feef8ad
DG
229 * (early debug does work there, despite having no vty with
230 * reg==0. */
98331f8a 231 return spapr_vty_get_default(spapr->vio_bus);
3feef8ad
DG
232 }
233
234 return sdev;
235}
236
83f7d43a 237static void spapr_vty_register_types(void)
4040ab72 238{
1fc02533
DG
239 spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char);
240 spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char);
39bffca2 241 type_register_static(&spapr_vty_info);
4040ab72 242}
83f7d43a
AF
243
244type_init(spapr_vty_register_types)
This page took 0.407034 seconds and 4 git commands to generate.