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 int vty_can_receive(void *opaque
)
17 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)opaque
;
19 return (dev
->in
- dev
->out
) < VTERM_BUFSIZE
;
22 static void vty_receive(void *opaque
, const uint8_t *buf
, int size
)
24 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)opaque
;
27 if ((dev
->in
== dev
->out
) && size
) {
28 /* toggle line to simulate edge interrupt */
29 qemu_irq_pulse(dev
->sdev
.qirq
);
31 for (i
= 0; i
< size
; i
++) {
32 assert((dev
->in
- dev
->out
) < VTERM_BUFSIZE
);
33 dev
->buf
[dev
->in
++ % VTERM_BUFSIZE
] = buf
[i
];
37 static int vty_getchars(VIOsPAPRDevice
*sdev
, uint8_t *buf
, int max
)
39 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)sdev
;
42 while ((n
< max
) && (dev
->out
!= dev
->in
)) {
43 buf
[n
++] = dev
->buf
[dev
->out
++ % VTERM_BUFSIZE
];
49 void vty_putchars(VIOsPAPRDevice
*sdev
, uint8_t *buf
, int len
)
51 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)sdev
;
53 /* FIXME: should check the qemu_chr_fe_write() return value */
54 qemu_chr_fe_write(dev
->chardev
, buf
, len
);
57 static int spapr_vty_init(VIOsPAPRDevice
*sdev
)
59 VIOsPAPRVTYDevice
*dev
= (VIOsPAPRVTYDevice
*)sdev
;
62 fprintf(stderr
, "spapr-vty: Can't create vty without a chardev!\n");
66 qemu_chr_add_handlers(dev
->chardev
, vty_can_receive
,
67 vty_receive
, NULL
, dev
);
72 /* Forward declaration */
73 static target_ulong
h_put_term_char(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
74 target_ulong opcode
, target_ulong
*args
)
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];
83 sdev
= vty_lookup(spapr
, reg
);
92 *((uint64_t *)buf
) = cpu_to_be64(char0_7
);
93 *((uint64_t *)buf
+ 1) = cpu_to_be64(char8_15
);
95 vty_putchars(sdev
, buf
, len
);
100 static target_ulong
h_get_term_char(CPUPPCState
*env
, sPAPREnvironment
*spapr
,
101 target_ulong opcode
, target_ulong
*args
)
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;
107 VIOsPAPRDevice
*sdev
;
110 sdev
= vty_lookup(spapr
, reg
);
115 *len
= vty_getchars(sdev
, buf
, sizeof(buf
));
117 memset(buf
+ *len
, 0, 16 - *len
);
120 *char0_7
= be64_to_cpu(*((uint64_t *)buf
));
121 *char8_15
= be64_to_cpu(*((uint64_t *)buf
+ 1));
126 void spapr_vty_create(VIOsPAPRBus
*bus
, uint32_t reg
, CharDriverState
*chardev
)
130 dev
= qdev_create(&bus
->bus
, "spapr-vty");
131 qdev_prop_set_uint32(dev
, "reg", reg
);
132 qdev_prop_set_chr(dev
, "chardev", chardev
);
133 qdev_init_nofail(dev
);
136 static Property spapr_vty_properties
[] = {
137 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVTYDevice
, sdev
, SPAPR_VTY_BASE_ADDRESS
, 0),
138 DEFINE_PROP_CHR("chardev", VIOsPAPRVTYDevice
, chardev
),
139 DEFINE_PROP_END_OF_LIST(),
142 static void spapr_vty_class_init(ObjectClass
*klass
, void *data
)
144 DeviceClass
*dc
= DEVICE_CLASS(klass
);
145 VIOsPAPRDeviceClass
*k
= VIO_SPAPR_DEVICE_CLASS(klass
);
147 k
->init
= spapr_vty_init
;
149 k
->dt_type
= "serial";
150 k
->dt_compatible
= "hvterm1";
151 dc
->props
= spapr_vty_properties
;
154 static TypeInfo spapr_vty_info
= {
156 .parent
= TYPE_VIO_SPAPR_DEVICE
,
157 .instance_size
= sizeof(VIOsPAPRVTYDevice
),
158 .class_init
= spapr_vty_class_init
,
161 VIOsPAPRDevice
*spapr_vty_get_default(VIOsPAPRBus
*bus
)
163 VIOsPAPRDevice
*sdev
, *selected
;
167 * To avoid the console bouncing around we want one VTY to be
168 * the "default". We haven't really got anything to go on, so
169 * arbitrarily choose the one with the lowest reg value.
173 QTAILQ_FOREACH(iter
, &bus
->bus
.children
, sibling
) {
174 /* Only look at VTY devices */
175 if (!object_dynamic_cast(OBJECT(iter
), "spapr-vty")) {
179 sdev
= DO_UPCAST(VIOsPAPRDevice
, qdev
, iter
);
181 /* First VTY we've found, so it is selected for now */
187 /* Choose VTY with lowest reg value */
188 if (sdev
->reg
< selected
->reg
) {
196 VIOsPAPRDevice
*vty_lookup(sPAPREnvironment
*spapr
, target_ulong reg
)
198 VIOsPAPRDevice
*sdev
;
200 sdev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
201 if (!sdev
&& reg
== 0) {
202 /* Hack for kernel early debug, which always specifies reg==0.
203 * We search all VIO devices, and grab the vty with the lowest
204 * reg. This attempts to mimic existing PowerVM behaviour
205 * (early debug does work there, despite having no vty with
207 return spapr_vty_get_default(spapr
->vio_bus
);
213 static void spapr_vty_register_types(void)
215 spapr_register_hypercall(H_PUT_TERM_CHAR
, h_put_term_char
);
216 spapr_register_hypercall(H_GET_TERM_CHAR
, h_get_term_char
);
217 type_register_static(&spapr_vty_info
);
220 type_init(spapr_vty_register_types
)