]> git.proxmox.com Git - qemu.git/blame - hw/spapr_vty.c
hw/ac97: Use AC97_Record_Gain_Mute not AC97_Line_In_Volume_Mute
[qemu.git] / hw / spapr_vty.c
CommitLineData
4040ab72
DG
1#include "qdev.h"
2#include "qemu-char.h"
3#include "hw/spapr.h"
4#include "hw/spapr_vio.h"
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
15static int vty_can_receive(void *opaque)
16{
17 VIOsPAPRVTYDevice *dev = (VIOsPAPRVTYDevice *)opaque;
18
19 return (dev->in - dev->out) < VTERM_BUFSIZE;
20}
21
22static void vty_receive(void *opaque, const uint8_t *buf, int size)
23{
24 VIOsPAPRVTYDevice *dev = (VIOsPAPRVTYDevice *)opaque;
25 int i;
26
0201e2da
DG
27 if ((dev->in == dev->out) && size) {
28 /* toggle line to simulate edge interrupt */
29 qemu_irq_pulse(dev->sdev.qirq);
30 }
4040ab72
DG
31 for (i = 0; i < size; i++) {
32 assert((dev->in - dev->out) < VTERM_BUFSIZE);
33 dev->buf[dev->in++ % VTERM_BUFSIZE] = buf[i];
34 }
35}
36
37static int vty_getchars(VIOsPAPRDevice *sdev, uint8_t *buf, int max)
38{
39 VIOsPAPRVTYDevice *dev = (VIOsPAPRVTYDevice *)sdev;
40 int n = 0;
41
42 while ((n < max) && (dev->out != dev->in)) {
43 buf[n++] = dev->buf[dev->out++ % VTERM_BUFSIZE];
44 }
45
46 return n;
47}
48
49void vty_putchars(VIOsPAPRDevice *sdev, uint8_t *buf, int len)
50{
51 VIOsPAPRVTYDevice *dev = (VIOsPAPRVTYDevice *)sdev;
52
2cc6e0a1
AL
53 /* FIXME: should check the qemu_chr_fe_write() return value */
54 qemu_chr_fe_write(dev->chardev, buf, len);
4040ab72
DG
55}
56
57static int spapr_vty_init(VIOsPAPRDevice *sdev)
58{
59 VIOsPAPRVTYDevice *dev = (VIOsPAPRVTYDevice *)sdev;
60
57285302
ME
61 if (!dev->chardev) {
62 fprintf(stderr, "spapr-vty: Can't create vty without a chardev!\n");
63 exit(1);
64 }
65
4040ab72
DG
66 qemu_chr_add_handlers(dev->chardev, vty_can_receive,
67 vty_receive, NULL, dev);
68
69 return 0;
70}
71
3feef8ad 72/* Forward declaration */
e2684c0b 73static target_ulong h_put_term_char(CPUPPCState *env, sPAPREnvironment *spapr,
4040ab72
DG
74 target_ulong opcode, target_ulong *args)
75{
76 target_ulong reg = args[0];
77 target_ulong len = args[1];
78 target_ulong char0_7 = args[2];
79 target_ulong char8_15 = args[3];
3feef8ad 80 VIOsPAPRDevice *sdev;
4040ab72
DG
81 uint8_t buf[16];
82
3feef8ad 83 sdev = vty_lookup(spapr, reg);
4040ab72
DG
84 if (!sdev) {
85 return H_PARAMETER;
86 }
87
88 if (len > 16) {
89 return H_PARAMETER;
90 }
91
92 *((uint64_t *)buf) = cpu_to_be64(char0_7);
93 *((uint64_t *)buf + 1) = cpu_to_be64(char8_15);
94
95 vty_putchars(sdev, buf, len);
96
97 return H_SUCCESS;
98}
99
e2684c0b 100static target_ulong h_get_term_char(CPUPPCState *env, sPAPREnvironment *spapr,
4040ab72
DG
101 target_ulong opcode, target_ulong *args)
102{
103 target_ulong reg = args[0];
104 target_ulong *len = args + 0;
105 target_ulong *char0_7 = args + 1;
106 target_ulong *char8_15 = args + 2;
3feef8ad 107 VIOsPAPRDevice *sdev;
4040ab72
DG
108 uint8_t buf[16];
109
3feef8ad 110 sdev = vty_lookup(spapr, reg);
4040ab72
DG
111 if (!sdev) {
112 return H_PARAMETER;
113 }
114
115 *len = vty_getchars(sdev, buf, sizeof(buf));
116 if (*len < 16) {
117 memset(buf + *len, 0, 16 - *len);
118 }
119
120 *char0_7 = be64_to_cpu(*((uint64_t *)buf));
121 *char8_15 = be64_to_cpu(*((uint64_t *)buf + 1));
122
123 return H_SUCCESS;
124}
125
d601fac4 126void spapr_vty_create(VIOsPAPRBus *bus, CharDriverState *chardev)
4040ab72
DG
127{
128 DeviceState *dev;
129
130 dev = qdev_create(&bus->bus, "spapr-vty");
4040ab72
DG
131 qdev_prop_set_chr(dev, "chardev", chardev);
132 qdev_init_nofail(dev);
133}
134
3954d33a 135static Property spapr_vty_properties[] = {
d601fac4 136 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVTYDevice, sdev, 0),
3954d33a
AL
137 DEFINE_PROP_CHR("chardev", VIOsPAPRVTYDevice, chardev),
138 DEFINE_PROP_END_OF_LIST(),
139};
140
141static void spapr_vty_class_init(ObjectClass *klass, void *data)
142{
39bffca2 143 DeviceClass *dc = DEVICE_CLASS(klass);
3954d33a
AL
144 VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
145
146 k->init = spapr_vty_init;
147 k->dt_name = "vty";
148 k->dt_type = "serial";
149 k->dt_compatible = "hvterm1";
39bffca2 150 dc->props = spapr_vty_properties;
3954d33a
AL
151}
152
39bffca2
AL
153static TypeInfo spapr_vty_info = {
154 .name = "spapr-vty",
155 .parent = TYPE_VIO_SPAPR_DEVICE,
156 .instance_size = sizeof(VIOsPAPRVTYDevice),
157 .class_init = spapr_vty_class_init,
4040ab72
DG
158};
159
68f3a94c 160VIOsPAPRDevice *spapr_vty_get_default(VIOsPAPRBus *bus)
98331f8a
DG
161{
162 VIOsPAPRDevice *sdev, *selected;
163 DeviceState *iter;
164
165 /*
166 * To avoid the console bouncing around we want one VTY to be
167 * the "default". We haven't really got anything to go on, so
168 * arbitrarily choose the one with the lowest reg value.
169 */
170
171 selected = NULL;
172 QTAILQ_FOREACH(iter, &bus->bus.children, sibling) {
173 /* Only look at VTY devices */
3954d33a 174 if (!object_dynamic_cast(OBJECT(iter), "spapr-vty")) {
98331f8a
DG
175 continue;
176 }
177
178 sdev = DO_UPCAST(VIOsPAPRDevice, qdev, iter);
179
180 /* First VTY we've found, so it is selected for now */
181 if (!selected) {
182 selected = sdev;
183 continue;
184 }
185
186 /* Choose VTY with lowest reg value */
187 if (sdev->reg < selected->reg) {
188 selected = sdev;
189 }
190 }
191
192 return selected;
193}
194
5f2e2ba2 195VIOsPAPRDevice *vty_lookup(sPAPREnvironment *spapr, target_ulong reg)
3feef8ad
DG
196{
197 VIOsPAPRDevice *sdev;
198
199 sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
200 if (!sdev && reg == 0) {
3feef8ad 201 /* Hack for kernel early debug, which always specifies reg==0.
98331f8a
DG
202 * We search all VIO devices, and grab the vty with the lowest
203 * reg. This attempts to mimic existing PowerVM behaviour
3feef8ad
DG
204 * (early debug does work there, despite having no vty with
205 * reg==0. */
98331f8a 206 return spapr_vty_get_default(spapr->vio_bus);
3feef8ad
DG
207 }
208
209 return sdev;
210}
211
83f7d43a 212static void spapr_vty_register_types(void)
4040ab72 213{
1fc02533
DG
214 spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char);
215 spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char);
39bffca2 216 type_register_static(&spapr_vty_info);
4040ab72 217}
83f7d43a
AF
218
219type_init(spapr_vty_register_types)