4 #include "hw/spapr_vio.h"
6 #define VTERM_BUFSIZE 16
8 typedef struct VIOsPAPRVTYDevice
{
10 CharDriverState
*chardev
;
12 uint8_t buf
[VTERM_BUFSIZE
];
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
)
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
;
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
;
55 while ((n
< max
) && (dev
->out
!= dev
->in
)) {
56 buf
[n
++] = dev
->buf
[dev
->out
++ % VTERM_BUFSIZE
];
58 vty_update_handlers(dev
);
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
,
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
);
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
);
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
);
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
);
131 *len
= vty_getchars(sdev
, buf
, sizeof(buf
));
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));
142 void spapr_vty_create(VIOsPAPRBus
*bus
,
143 uint32_t reg
, CharDriverState
*chardev
,
144 qemu_irq qirq
, uint32_t vio_irq_num
)
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
;
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
,
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
);