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