strongarm ioctl rename fallout
[qemu/aliguori.git] / hw / spapr_vty.c
blob70d89cf19d253f061ce3d83384ec44a553c8a413
1 #include "qdev.h"
2 #include "qemu-char.h"
3 #include "hw/spapr.h"
4 #include "hw/spapr_vio.h"
6 #define VTERM_BUFSIZE 16
8 typedef struct VIOsPAPRVTYDevice {
9 VIOsPAPRDevice sdev;
10 CharDriverState *chardev;
11 uint32_t in, out;
12 uint8_t buf[VTERM_BUFSIZE];
13 } VIOsPAPRVTYDevice;
15 static void vty_update_handlers(VIOsPAPRVTYDevice *dev);
17 static int vty_can_receive(VIOsPAPRVTYDevice *dev)
19 return (dev->in - dev->out) < VTERM_BUFSIZE;
22 static void vty_receive(VIOsPAPRVTYDevice *dev, const uint8_t *buf, int size)
24 int i;
26 if ((dev->in == dev->out) && size) {
27 /* toggle line to simulate edge interrupt */
28 qemu_irq_pulse(dev->sdev.qirq);
30 for (i = 0; i < size; i++) {
31 assert((dev->in - dev->out) < VTERM_BUFSIZE);
32 dev->buf[dev->in++ % VTERM_BUFSIZE] = buf[i];
34 vty_update_handlers(dev);
37 static void vty_receive_handler(void *opaque)
39 VIOsPAPRVTYDevice *dev = opaque;
40 uint8_t buf[32];
41 int size;
43 size = vty_can_receive(dev);
44 size = MIN(size, sizeof(buf));
45 size = qemu_chr_fe_read(dev->chardev, buf, size);
47 vty_receive(dev, buf, size);
50 static int vty_getchars(VIOsPAPRDevice *sdev, uint8_t *buf, int max)
52 VIOsPAPRVTYDevice *dev = (VIOsPAPRVTYDevice *)sdev;
53 int n = 0;
55 while ((n < max) && (dev->out != dev->in)) {
56 buf[n++] = dev->buf[dev->out++ % VTERM_BUFSIZE];
58 vty_update_handlers(dev);
60 return n;
63 void vty_putchars(VIOsPAPRDevice *sdev, uint8_t *buf, int len)
65 VIOsPAPRVTYDevice *dev = (VIOsPAPRVTYDevice *)sdev;
67 /* FIXME: should check the qemu_chr_fe_write() return value */
68 qemu_chr_fe_write(dev->chardev, buf, len);
71 static void vty_update_handlers(VIOsPAPRVTYDevice *dev)
73 if (vty_can_receive(dev) > 0) {
74 qemu_chr_fe_set_handlers(dev->chardev, vty_receive_handler,
75 NULL, NULL, dev);
76 } else {
77 qemu_chr_fe_set_handlers(dev->chardev, NULL, NULL, NULL, dev);
81 static int spapr_vty_init(VIOsPAPRDevice *sdev)
83 VIOsPAPRVTYDevice *dev = (VIOsPAPRVTYDevice *)sdev;
85 qemu_chr_fe_open(dev->chardev);
86 vty_update_handlers(dev);
88 return 0;
91 static target_ulong h_put_term_char(CPUState *env, sPAPREnvironment *spapr,
92 target_ulong opcode, target_ulong *args)
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];
98 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
99 uint8_t buf[16];
101 if (!sdev) {
102 return H_PARAMETER;
105 if (len > 16) {
106 return H_PARAMETER;
109 *((uint64_t *)buf) = cpu_to_be64(char0_7);
110 *((uint64_t *)buf + 1) = cpu_to_be64(char8_15);
112 vty_putchars(sdev, buf, len);
114 return H_SUCCESS;
117 static target_ulong h_get_term_char(CPUState *env, sPAPREnvironment *spapr,
118 target_ulong opcode, target_ulong *args)
120 target_ulong reg = args[0];
121 target_ulong *len = args + 0;
122 target_ulong *char0_7 = args + 1;
123 target_ulong *char8_15 = args + 2;
124 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
125 uint8_t buf[16];
127 if (!sdev) {
128 return H_PARAMETER;
131 *len = vty_getchars(sdev, buf, sizeof(buf));
132 if (*len < 16) {
133 memset(buf + *len, 0, 16 - *len);
136 *char0_7 = be64_to_cpu(*((uint64_t *)buf));
137 *char8_15 = be64_to_cpu(*((uint64_t *)buf + 1));
139 return H_SUCCESS;
142 void spapr_vty_create(VIOsPAPRBus *bus,
143 uint32_t reg, CharDriverState *chardev,
144 qemu_irq qirq, uint32_t vio_irq_num)
146 DeviceState *dev;
147 VIOsPAPRDevice *sdev;
149 dev = qdev_create(&bus->bus, "spapr-vty");
150 qdev_prop_set_uint32(dev, "reg", reg);
151 qdev_prop_set_chr(dev, "chardev", chardev);
152 qdev_init_nofail(dev);
153 sdev = (VIOsPAPRDevice *)dev;
154 sdev->qirq = qirq;
155 sdev->vio_irq_num = vio_irq_num;
158 static void vty_hcalls(VIOsPAPRBus *bus)
160 spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char);
161 spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char);
164 static VIOsPAPRDeviceInfo spapr_vty = {
165 .init = spapr_vty_init,
166 .dt_name = "vty",
167 .dt_type = "serial",
168 .dt_compatible = "hvterm1",
169 .hcalls = vty_hcalls,
170 .qdev.name = "spapr-vty",
171 .qdev.size = sizeof(VIOsPAPRVTYDevice),
172 .qdev.props = (Property[]) {
173 DEFINE_PROP_UINT32("reg", VIOsPAPRDevice, reg, 0),
174 DEFINE_PROP_CHR("chardev", VIOsPAPRVTYDevice, chardev),
175 DEFINE_PROP_END_OF_LIST(),
179 static void spapr_vty_register(void)
181 spapr_vio_bus_register_withprop(&spapr_vty);
183 device_init(spapr_vty_register);