]>
Commit | Line | Data |
---|---|---|
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 | 15 | struct SpaprVioVty { |
ce2918cb | 16 | SpaprVioDevice sdev; |
becdfa00 | 17 | CharBackend chardev; |
4040ab72 DG |
18 | uint32_t in, out; |
19 | uint8_t buf[VTERM_BUFSIZE]; | |
db1015e9 EH |
20 | }; |
21 | typedef struct SpaprVioVty SpaprVioVty; | |
4040ab72 | 22 | |
fd506b4f | 23 | #define TYPE_VIO_SPAPR_VTY_DEVICE "spapr-vty" |
8110fa1d EH |
24 | DECLARE_INSTANCE_CHECKER(SpaprVioVty, VIO_SPAPR_VTY_DEVICE, |
25 | TYPE_VIO_SPAPR_VTY_DEVICE) | |
fd506b4f | 26 | |
4040ab72 DG |
27 | static int vty_can_receive(void *opaque) |
28 | { | |
ce2918cb | 29 | SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(opaque); |
4040ab72 | 30 | |
8a273cbe | 31 | return VTERM_BUFSIZE - (dev->in - dev->out); |
4040ab72 DG |
32 | } |
33 | ||
34 | static void vty_receive(void *opaque, const uint8_t *buf, int size) | |
35 | { | |
ce2918cb | 36 | SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(opaque); |
4040ab72 DG |
37 | int i; |
38 | ||
0201e2da DG |
39 | if ((dev->in == dev->out) && size) { |
40 | /* toggle line to simulate edge interrupt */ | |
7678b74a | 41 | spapr_vio_irq_pulse(&dev->sdev); |
0201e2da | 42 | } |
4040ab72 | 43 | for (i = 0; i < size; i++) { |
7bacfd7f TH |
44 | if (dev->in - dev->out >= VTERM_BUFSIZE) { |
45 | static bool reported; | |
46 | if (!reported) { | |
47 | error_report("VTY input buffer exhausted - characters dropped." | |
48 | " (input size = %i)", size); | |
49 | reported = true; | |
50 | } | |
51 | break; | |
52 | } | |
4040ab72 DG |
53 | dev->buf[dev->in++ % VTERM_BUFSIZE] = buf[i]; |
54 | } | |
55 | } | |
56 | ||
ce2918cb | 57 | static int vty_getchars(SpaprVioDevice *sdev, uint8_t *buf, int max) |
4040ab72 | 58 | { |
ce2918cb | 59 | SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev); |
4040ab72 DG |
60 | int n = 0; |
61 | ||
62 | while ((n < max) && (dev->out != dev->in)) { | |
fd38b162 PM |
63 | /* |
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. | |
70 | */ | |
71 | if (n > 0 && (buf[n - 1] == '\r') && | |
72 | (dev->buf[dev->out % VTERM_BUFSIZE] == '\0')) { | |
73 | break; | |
6c3bc244 | 74 | } |
fd38b162 | 75 | buf[n++] = dev->buf[dev->out++ % VTERM_BUFSIZE]; |
4040ab72 DG |
76 | } |
77 | ||
5345fdb4 | 78 | qemu_chr_fe_accept_input(&dev->chardev); |
7770b6f7 | 79 | |
4040ab72 DG |
80 | return n; |
81 | } | |
82 | ||
ce2918cb | 83 | void vty_putchars(SpaprVioDevice *sdev, uint8_t *buf, int len) |
4040ab72 | 84 | { |
ce2918cb | 85 | SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev); |
4040ab72 | 86 | |
6ab3fc32 DB |
87 | /* XXX this blocks entire thread. Rewrite to use |
88 | * qemu_chr_fe_write and background I/O callbacks */ | |
5345fdb4 | 89 | qemu_chr_fe_write_all(&dev->chardev, buf, len); |
4040ab72 DG |
90 | } |
91 | ||
ce2918cb | 92 | static void spapr_vty_realize(SpaprVioDevice *sdev, Error **errp) |
4040ab72 | 93 | { |
ce2918cb | 94 | SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev); |
4040ab72 | 95 | |
30650701 | 96 | if (!qemu_chr_fe_backend_connected(&dev->chardev)) { |
28b07e73 MA |
97 | error_setg(errp, "chardev property not set"); |
98 | return; | |
57285302 ME |
99 | } |
100 | ||
5345fdb4 | 101 | qemu_chr_fe_set_handlers(&dev->chardev, vty_can_receive, |
81517ba3 | 102 | vty_receive, NULL, NULL, dev, NULL, true); |
4040ab72 DG |
103 | } |
104 | ||
3feef8ad | 105 | /* Forward declaration */ |
ce2918cb | 106 | static target_ulong h_put_term_char(PowerPCCPU *cpu, SpaprMachineState *spapr, |
4040ab72 DG |
107 | target_ulong opcode, target_ulong *args) |
108 | { | |
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]; | |
ce2918cb | 113 | SpaprVioDevice *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 | if (len > 16) { | |
122 | return H_PARAMETER; | |
123 | } | |
124 | ||
125 | *((uint64_t *)buf) = cpu_to_be64(char0_7); | |
126 | *((uint64_t *)buf + 1) = cpu_to_be64(char8_15); | |
127 | ||
128 | vty_putchars(sdev, buf, len); | |
129 | ||
130 | return H_SUCCESS; | |
131 | } | |
132 | ||
ce2918cb | 133 | static target_ulong h_get_term_char(PowerPCCPU *cpu, SpaprMachineState *spapr, |
4040ab72 DG |
134 | target_ulong opcode, target_ulong *args) |
135 | { | |
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; | |
ce2918cb | 140 | SpaprVioDevice *sdev; |
4040ab72 DG |
141 | uint8_t buf[16]; |
142 | ||
3feef8ad | 143 | sdev = vty_lookup(spapr, reg); |
4040ab72 DG |
144 | if (!sdev) { |
145 | return H_PARAMETER; | |
146 | } | |
147 | ||
148 | *len = vty_getchars(sdev, buf, sizeof(buf)); | |
149 | if (*len < 16) { | |
150 | memset(buf + *len, 0, 16 - *len); | |
151 | } | |
152 | ||
153 | *char0_7 = be64_to_cpu(*((uint64_t *)buf)); | |
154 | *char8_15 = be64_to_cpu(*((uint64_t *)buf + 1)); | |
155 | ||
156 | return H_SUCCESS; | |
157 | } | |
158 | ||
ce2918cb | 159 | void spapr_vty_create(SpaprVioBus *bus, Chardev *chardev) |
4040ab72 DG |
160 | { |
161 | DeviceState *dev; | |
162 | ||
3e80f690 | 163 | dev = qdev_new("spapr-vty"); |
4040ab72 | 164 | qdev_prop_set_chr(dev, "chardev", chardev); |
3e80f690 | 165 | qdev_realize_and_unref(dev, &bus->bus, &error_fatal); |
4040ab72 DG |
166 | } |
167 | ||
3954d33a | 168 | static Property spapr_vty_properties[] = { |
ce2918cb DG |
169 | DEFINE_SPAPR_PROPERTIES(SpaprVioVty, sdev), |
170 | DEFINE_PROP_CHR("chardev", SpaprVioVty, chardev), | |
3954d33a AL |
171 | DEFINE_PROP_END_OF_LIST(), |
172 | }; | |
173 | ||
db1b58e9 DG |
174 | static const VMStateDescription vmstate_spapr_vty = { |
175 | .name = "spapr_vty", | |
176 | .version_id = 1, | |
177 | .minimum_version_id = 1, | |
3aff6c2f | 178 | .fields = (VMStateField[]) { |
ce2918cb | 179 | VMSTATE_SPAPR_VIO(sdev, SpaprVioVty), |
db1b58e9 | 180 | |
ce2918cb DG |
181 | VMSTATE_UINT32(in, SpaprVioVty), |
182 | VMSTATE_UINT32(out, SpaprVioVty), | |
183 | VMSTATE_BUFFER(buf, SpaprVioVty), | |
db1b58e9 DG |
184 | VMSTATE_END_OF_LIST() |
185 | }, | |
186 | }; | |
187 | ||
3954d33a AL |
188 | static void spapr_vty_class_init(ObjectClass *klass, void *data) |
189 | { | |
39bffca2 | 190 | DeviceClass *dc = DEVICE_CLASS(klass); |
ce2918cb | 191 | SpaprVioDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass); |
3954d33a | 192 | |
28b07e73 | 193 | k->realize = spapr_vty_realize; |
3954d33a AL |
194 | k->dt_name = "vty"; |
195 | k->dt_type = "serial"; | |
196 | k->dt_compatible = "hvterm1"; | |
29fdedfe | 197 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
4f67d30b | 198 | device_class_set_props(dc, spapr_vty_properties); |
db1b58e9 | 199 | dc->vmsd = &vmstate_spapr_vty; |
3954d33a AL |
200 | } |
201 | ||
8c43a6f0 | 202 | static const TypeInfo spapr_vty_info = { |
fd506b4f | 203 | .name = TYPE_VIO_SPAPR_VTY_DEVICE, |
39bffca2 | 204 | .parent = TYPE_VIO_SPAPR_DEVICE, |
ce2918cb | 205 | .instance_size = sizeof(SpaprVioVty), |
39bffca2 | 206 | .class_init = spapr_vty_class_init, |
4040ab72 DG |
207 | }; |
208 | ||
ce2918cb | 209 | SpaprVioDevice *spapr_vty_get_default(SpaprVioBus *bus) |
98331f8a | 210 | { |
ce2918cb | 211 | SpaprVioDevice *sdev, *selected; |
0866aca1 | 212 | BusChild *kid; |
98331f8a DG |
213 | |
214 | /* | |
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. | |
218 | */ | |
219 | ||
220 | selected = NULL; | |
0866aca1 AL |
221 | QTAILQ_FOREACH(kid, &bus->bus.children, sibling) { |
222 | DeviceState *iter = kid->child; | |
223 | ||
98331f8a | 224 | /* Only look at VTY devices */ |
e275934d | 225 | if (!object_dynamic_cast(OBJECT(iter), TYPE_VIO_SPAPR_VTY_DEVICE)) { |
98331f8a DG |
226 | continue; |
227 | } | |
228 | ||
fd506b4f | 229 | sdev = VIO_SPAPR_DEVICE(iter); |
98331f8a DG |
230 | |
231 | /* First VTY we've found, so it is selected for now */ | |
232 | if (!selected) { | |
233 | selected = sdev; | |
234 | continue; | |
235 | } | |
236 | ||
237 | /* Choose VTY with lowest reg value */ | |
238 | if (sdev->reg < selected->reg) { | |
239 | selected = sdev; | |
240 | } | |
241 | } | |
242 | ||
243 | return selected; | |
244 | } | |
245 | ||
ce2918cb | 246 | SpaprVioDevice *vty_lookup(SpaprMachineState *spapr, target_ulong reg) |
3feef8ad | 247 | { |
ce2918cb | 248 | SpaprVioDevice *sdev; |
3feef8ad DG |
249 | |
250 | sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); | |
251 | if (!sdev && reg == 0) { | |
3feef8ad | 252 | /* Hack for kernel early debug, which always specifies reg==0. |
98331f8a DG |
253 | * We search all VIO devices, and grab the vty with the lowest |
254 | * reg. This attempts to mimic existing PowerVM behaviour | |
3feef8ad DG |
255 | * (early debug does work there, despite having no vty with |
256 | * reg==0. */ | |
98331f8a | 257 | return spapr_vty_get_default(spapr->vio_bus); |
3feef8ad DG |
258 | } |
259 | ||
0f888bfa DG |
260 | if (!object_dynamic_cast(OBJECT(sdev), TYPE_VIO_SPAPR_VTY_DEVICE)) { |
261 | return NULL; | |
262 | } | |
263 | ||
3feef8ad DG |
264 | return sdev; |
265 | } | |
266 | ||
83f7d43a | 267 | static void spapr_vty_register_types(void) |
4040ab72 | 268 | { |
1fc02533 DG |
269 | spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char); |
270 | spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char); | |
39bffca2 | 271 | type_register_static(&spapr_vty_info); |
4040ab72 | 272 | } |
83f7d43a AF |
273 | |
274 | type_init(spapr_vty_register_types) |