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