]>
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]; | |
61 | } | |
62 | ||
5345fdb4 | 63 | qemu_chr_fe_accept_input(&dev->chardev); |
7770b6f7 | 64 | |
4040ab72 DG |
65 | return n; |
66 | } | |
67 | ||
68 | void vty_putchars(VIOsPAPRDevice *sdev, uint8_t *buf, int len) | |
69 | { | |
fd506b4f | 70 | VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev); |
4040ab72 | 71 | |
6ab3fc32 DB |
72 | /* XXX this blocks entire thread. Rewrite to use |
73 | * qemu_chr_fe_write and background I/O callbacks */ | |
5345fdb4 | 74 | qemu_chr_fe_write_all(&dev->chardev, buf, len); |
4040ab72 DG |
75 | } |
76 | ||
28b07e73 | 77 | static void spapr_vty_realize(VIOsPAPRDevice *sdev, Error **errp) |
4040ab72 | 78 | { |
fd506b4f | 79 | VIOsPAPRVTYDevice *dev = VIO_SPAPR_VTY_DEVICE(sdev); |
4040ab72 | 80 | |
5345fdb4 | 81 | if (!qemu_chr_fe_get_driver(&dev->chardev)) { |
28b07e73 MA |
82 | error_setg(errp, "chardev property not set"); |
83 | return; | |
57285302 ME |
84 | } |
85 | ||
5345fdb4 | 86 | qemu_chr_fe_set_handlers(&dev->chardev, vty_can_receive, |
81517ba3 | 87 | vty_receive, NULL, NULL, dev, NULL, true); |
4040ab72 DG |
88 | } |
89 | ||
3feef8ad | 90 | /* Forward declaration */ |
28e02042 | 91 | static target_ulong h_put_term_char(PowerPCCPU *cpu, sPAPRMachineState *spapr, |
4040ab72 DG |
92 | target_ulong opcode, target_ulong *args) |
93 | { | |
94 | target_ulong reg = args[0]; | |
95 | target_ulong len = args[1]; | |
96 | target_ulong char0_7 = args[2]; | |
97 | target_ulong char8_15 = args[3]; | |
3feef8ad | 98 | VIOsPAPRDevice *sdev; |
4040ab72 DG |
99 | uint8_t buf[16]; |
100 | ||
3feef8ad | 101 | sdev = vty_lookup(spapr, reg); |
4040ab72 DG |
102 | if (!sdev) { |
103 | return H_PARAMETER; | |
104 | } | |
105 | ||
106 | if (len > 16) { | |
107 | return H_PARAMETER; | |
108 | } | |
109 | ||
110 | *((uint64_t *)buf) = cpu_to_be64(char0_7); | |
111 | *((uint64_t *)buf + 1) = cpu_to_be64(char8_15); | |
112 | ||
113 | vty_putchars(sdev, buf, len); | |
114 | ||
115 | return H_SUCCESS; | |
116 | } | |
117 | ||
28e02042 | 118 | static target_ulong h_get_term_char(PowerPCCPU *cpu, sPAPRMachineState *spapr, |
4040ab72 DG |
119 | target_ulong opcode, target_ulong *args) |
120 | { | |
121 | target_ulong reg = args[0]; | |
122 | target_ulong *len = args + 0; | |
123 | target_ulong *char0_7 = args + 1; | |
124 | target_ulong *char8_15 = args + 2; | |
3feef8ad | 125 | VIOsPAPRDevice *sdev; |
4040ab72 DG |
126 | uint8_t buf[16]; |
127 | ||
3feef8ad | 128 | sdev = vty_lookup(spapr, reg); |
4040ab72 DG |
129 | if (!sdev) { |
130 | return H_PARAMETER; | |
131 | } | |
132 | ||
133 | *len = vty_getchars(sdev, buf, sizeof(buf)); | |
134 | if (*len < 16) { | |
135 | memset(buf + *len, 0, 16 - *len); | |
136 | } | |
137 | ||
138 | *char0_7 = be64_to_cpu(*((uint64_t *)buf)); | |
139 | *char8_15 = be64_to_cpu(*((uint64_t *)buf + 1)); | |
140 | ||
141 | return H_SUCCESS; | |
142 | } | |
143 | ||
0ec7b3e7 | 144 | void spapr_vty_create(VIOsPAPRBus *bus, Chardev *chardev) |
4040ab72 DG |
145 | { |
146 | DeviceState *dev; | |
147 | ||
148 | dev = qdev_create(&bus->bus, "spapr-vty"); | |
4040ab72 DG |
149 | qdev_prop_set_chr(dev, "chardev", chardev); |
150 | qdev_init_nofail(dev); | |
151 | } | |
152 | ||
3954d33a | 153 | static Property spapr_vty_properties[] = { |
ad0ebb91 | 154 | DEFINE_SPAPR_PROPERTIES(VIOsPAPRVTYDevice, sdev), |
3954d33a AL |
155 | DEFINE_PROP_CHR("chardev", VIOsPAPRVTYDevice, chardev), |
156 | DEFINE_PROP_END_OF_LIST(), | |
157 | }; | |
158 | ||
db1b58e9 DG |
159 | static const VMStateDescription vmstate_spapr_vty = { |
160 | .name = "spapr_vty", | |
161 | .version_id = 1, | |
162 | .minimum_version_id = 1, | |
3aff6c2f | 163 | .fields = (VMStateField[]) { |
db1b58e9 DG |
164 | VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVTYDevice), |
165 | ||
166 | VMSTATE_UINT32(in, VIOsPAPRVTYDevice), | |
167 | VMSTATE_UINT32(out, VIOsPAPRVTYDevice), | |
168 | VMSTATE_BUFFER(buf, VIOsPAPRVTYDevice), | |
169 | VMSTATE_END_OF_LIST() | |
170 | }, | |
171 | }; | |
172 | ||
3954d33a AL |
173 | static void spapr_vty_class_init(ObjectClass *klass, void *data) |
174 | { | |
39bffca2 | 175 | DeviceClass *dc = DEVICE_CLASS(klass); |
3954d33a AL |
176 | VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass); |
177 | ||
28b07e73 | 178 | k->realize = spapr_vty_realize; |
3954d33a AL |
179 | k->dt_name = "vty"; |
180 | k->dt_type = "serial"; | |
181 | k->dt_compatible = "hvterm1"; | |
29fdedfe | 182 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
39bffca2 | 183 | dc->props = spapr_vty_properties; |
db1b58e9 | 184 | dc->vmsd = &vmstate_spapr_vty; |
3954d33a AL |
185 | } |
186 | ||
8c43a6f0 | 187 | static const TypeInfo spapr_vty_info = { |
fd506b4f | 188 | .name = TYPE_VIO_SPAPR_VTY_DEVICE, |
39bffca2 AL |
189 | .parent = TYPE_VIO_SPAPR_DEVICE, |
190 | .instance_size = sizeof(VIOsPAPRVTYDevice), | |
191 | .class_init = spapr_vty_class_init, | |
4040ab72 DG |
192 | }; |
193 | ||
68f3a94c | 194 | VIOsPAPRDevice *spapr_vty_get_default(VIOsPAPRBus *bus) |
98331f8a DG |
195 | { |
196 | VIOsPAPRDevice *sdev, *selected; | |
0866aca1 | 197 | BusChild *kid; |
98331f8a DG |
198 | |
199 | /* | |
200 | * To avoid the console bouncing around we want one VTY to be | |
201 | * the "default". We haven't really got anything to go on, so | |
202 | * arbitrarily choose the one with the lowest reg value. | |
203 | */ | |
204 | ||
205 | selected = NULL; | |
0866aca1 AL |
206 | QTAILQ_FOREACH(kid, &bus->bus.children, sibling) { |
207 | DeviceState *iter = kid->child; | |
208 | ||
98331f8a | 209 | /* Only look at VTY devices */ |
e275934d | 210 | if (!object_dynamic_cast(OBJECT(iter), TYPE_VIO_SPAPR_VTY_DEVICE)) { |
98331f8a DG |
211 | continue; |
212 | } | |
213 | ||
fd506b4f | 214 | sdev = VIO_SPAPR_DEVICE(iter); |
98331f8a DG |
215 | |
216 | /* First VTY we've found, so it is selected for now */ | |
217 | if (!selected) { | |
218 | selected = sdev; | |
219 | continue; | |
220 | } | |
221 | ||
222 | /* Choose VTY with lowest reg value */ | |
223 | if (sdev->reg < selected->reg) { | |
224 | selected = sdev; | |
225 | } | |
226 | } | |
227 | ||
228 | return selected; | |
229 | } | |
230 | ||
28e02042 | 231 | VIOsPAPRDevice *vty_lookup(sPAPRMachineState *spapr, target_ulong reg) |
3feef8ad DG |
232 | { |
233 | VIOsPAPRDevice *sdev; | |
234 | ||
235 | sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); | |
236 | if (!sdev && reg == 0) { | |
3feef8ad | 237 | /* Hack for kernel early debug, which always specifies reg==0. |
98331f8a DG |
238 | * We search all VIO devices, and grab the vty with the lowest |
239 | * reg. This attempts to mimic existing PowerVM behaviour | |
3feef8ad DG |
240 | * (early debug does work there, despite having no vty with |
241 | * reg==0. */ | |
98331f8a | 242 | return spapr_vty_get_default(spapr->vio_bus); |
3feef8ad DG |
243 | } |
244 | ||
0f888bfa DG |
245 | if (!object_dynamic_cast(OBJECT(sdev), TYPE_VIO_SPAPR_VTY_DEVICE)) { |
246 | return NULL; | |
247 | } | |
248 | ||
3feef8ad DG |
249 | return sdev; |
250 | } | |
251 | ||
83f7d43a | 252 | static void spapr_vty_register_types(void) |
4040ab72 | 253 | { |
1fc02533 DG |
254 | spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char); |
255 | spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char); | |
39bffca2 | 256 | type_register_static(&spapr_vty_info); |
4040ab72 | 257 | } |
83f7d43a AF |
258 | |
259 | type_init(spapr_vty_register_types) |